2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
42 #include <sys/cdrio.h>
43 #include <sys/dvdio.h>
45 #include <sys/fcntl.h>
47 #include <sys/ctype.h>
49 #include <sys/taskqueue.h>
51 #include <machine/bus.h>
52 #include <geom/geom.h>
53 #include <dev/ata/ata-all.h>
54 #include <dev/ata/atapi-cd.h>
58 static void acd_geom_attach(void *, int);
59 static void acd_geom_detach(void *, int);
60 static void acd_set_ioparm(device_t);
61 static void acd_describe(device_t);
62 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
63 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
64 static int acd_geom_access(struct g_provider *, int, int, int);
65 static g_ioctl_t acd_geom_ioctl;
66 static void acd_geom_start(struct bio *);
67 static void acd_strategy(struct bio *);
68 static void acd_done(struct ata_request *);
69 static void acd_read_toc(device_t);
70 static int acd_play(device_t, int, int);
71 static int acd_setchan(device_t, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
72 static int acd_init_writer(device_t, int);
73 static int acd_fixate(device_t, int);
74 static int acd_init_track(device_t, struct cdr_track *);
75 static int acd_flush(device_t);
76 static int acd_read_track_info(device_t, int32_t, struct acd_track_info *);
77 static int acd_get_progress(device_t, int *);
78 static int acd_send_cue(device_t, struct cdr_cuesheet *);
79 static int acd_report_key(device_t, struct dvd_authinfo *);
80 static int acd_send_key(device_t, struct dvd_authinfo *);
81 static int acd_read_structure(device_t, struct dvd_struct *);
82 static int acd_tray(device_t, int);
83 static int acd_blank(device_t, int);
84 static int acd_prevent_allow(device_t, int);
85 static int acd_start_stop(device_t, int);
86 static int acd_pause_resume(device_t, int);
87 static int acd_mode_sense(device_t, int, caddr_t, int);
88 static int acd_mode_select(device_t, caddr_t, int);
89 static int acd_set_speed(device_t, int, int);
90 static void acd_get_cap(device_t);
91 static int acd_read_format_caps(device_t, struct cdr_format_capacities *);
92 static int acd_format(device_t, struct cdr_format_params *);
93 static int acd_test_ready(device_t);
96 static MALLOC_DEFINE(M_ACD, "acd_driver", "ATAPI CD driver buffers");
97 static struct g_class acd_class = {
100 .access = acd_geom_access,
101 .ioctl = acd_geom_ioctl,
102 .start = acd_geom_start,
104 //DECLARE_GEOM_CLASS(acd_class, acd);
107 acd_probe(device_t dev)
109 struct ata_device *atadev = device_get_softc(dev);
111 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
112 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_CDROM)
119 acd_attach(device_t dev)
121 struct acd_softc *cdp;
123 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO))) {
124 device_printf(dev, "out of memory\n");
127 cdp->block_size = 2048;
128 device_set_ivars(dev, cdp);
129 ATA_SETMODE(device_get_parent(dev), dev);
130 ata_controlcmd(dev, ATA_DEVICE_RESET, 0, 0, 0);
132 g_post_event(acd_geom_attach, dev, M_WAITOK, NULL);
134 /* announce we are here */
140 acd_detach(device_t dev)
142 g_waitfor_event(acd_geom_detach, dev, M_WAITOK, NULL);
147 acd_shutdown(device_t dev)
149 struct ata_device *atadev = device_get_softc(dev);
151 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
152 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
157 acd_reinit(device_t dev)
159 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
160 struct ata_device *atadev = device_get_softc(dev);
162 /* if detach pending, return error */
163 if (!(ch->devices & (ATA_ATAPI_MASTER << atadev->unit)))
166 ATA_SETMODE(device_get_parent(dev), dev);
171 acd_geom_attach(void *arg, int flag)
173 struct ata_device *atadev = device_get_softc(arg);
174 struct acd_softc *cdp = device_get_ivars(arg);
176 struct g_provider *pp;
179 gp = g_new_geomf(&acd_class, "acd%d", device_get_unit(arg));
182 pp = g_new_providerf(gp, "acd%d", device_get_unit(arg));
185 g_error_provider(pp, 0);
186 atadev->flags |= ATA_D_MEDIA_CHANGED;
191 acd_geom_detach(void *arg, int flag)
193 struct acd_softc *cdp = device_get_ivars(arg);
195 /* signal geom so we dont get any further requests */
196 g_wither_geom(cdp->gp, ENXIO);
198 /* fail requests on the queue and any thats "in flight" for this device */
199 ata_fail_requests(arg);
201 /* dont leave anything behind */
202 device_set_ivars(arg, NULL);
207 acd_geom_ioctl(struct g_provider *pp, u_long cmd, void *addr, int fflag, struct thread *td)
209 device_t dev = pp->geom->softc;
210 struct ata_device *atadev = device_get_softc(dev);
211 struct acd_softc *cdp = device_get_ivars(dev);
212 int error = 0, nocopyout = 0;
217 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
225 acd_prevent_allow(dev, 1);
226 cdp->flags |= F_LOCKED;
234 error = acd_pause_resume(dev, 1);
238 error = acd_pause_resume(dev, 0);
242 error = acd_start_stop(dev, 1);
246 error = acd_start_stop(dev, 0);
250 error = acd_prevent_allow(dev, 0);
251 cdp->flags &= ~F_LOCKED;
255 error = acd_prevent_allow(dev, 1);
256 cdp->flags |= F_LOCKED;
260 * XXXRW: Why does this require privilege?
263 error = priv_check(td, PRIV_DRIVER);
266 error = acd_test_ready(dev);
274 error = acd_tray(dev, 0);
280 error = acd_tray(dev, 1);
283 case CDIOREADTOCHEADER:
284 if (!cdp->toc.hdr.ending_track) {
288 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
291 case CDIOREADTOCENTRYS:
293 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
294 struct toc *toc = &cdp->toc;
295 int starting_track = te->starting_track;
298 if (!toc->hdr.ending_track) {
303 if (te->data_len < sizeof(toc->tab[0]) ||
304 (te->data_len % sizeof(toc->tab[0])) != 0 ||
305 (te->address_format != CD_MSF_FORMAT &&
306 te->address_format != CD_LBA_FORMAT)) {
312 starting_track = toc->hdr.starting_track;
313 else if (starting_track == 170)
314 starting_track = toc->hdr.ending_track + 1;
315 else if (starting_track < toc->hdr.starting_track ||
316 starting_track > toc->hdr.ending_track + 1) {
321 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
323 if (te->data_len < len)
325 if (len > sizeof(toc->tab)) {
330 if (te->address_format == CD_MSF_FORMAT) {
331 struct cd_toc_entry *entry;
333 if (!(toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT))) {
337 bcopy(&cdp->toc, toc, sizeof(struct toc));
338 entry = toc->tab + (toc->hdr.ending_track + 1 -
339 toc->hdr.starting_track) + 1;
340 while (--entry >= toc->tab) {
341 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
342 &entry->addr.msf.second, &entry->addr.msf.frame);
343 entry->addr_type = CD_MSF_FORMAT;
346 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
348 if (te->address_format == CD_MSF_FORMAT)
353 case CDIOREADTOCENTRY:
355 struct ioc_read_toc_single_entry *te =
356 (struct ioc_read_toc_single_entry *)addr;
357 struct toc *toc = &cdp->toc;
358 u_char track = te->track;
360 if (!toc->hdr.ending_track) {
365 if (te->address_format != CD_MSF_FORMAT &&
366 te->address_format != CD_LBA_FORMAT) {
372 track = toc->hdr.starting_track;
373 else if (track == 170)
374 track = toc->hdr.ending_track + 1;
375 else if (track < toc->hdr.starting_track ||
376 track > toc->hdr.ending_track + 1) {
381 if (te->address_format == CD_MSF_FORMAT) {
382 struct cd_toc_entry *entry;
384 if (!(toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT))) {
388 bcopy(&cdp->toc, toc, sizeof(struct toc));
389 entry = toc->tab + (track - toc->hdr.starting_track);
390 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
391 &entry->addr.msf.second, &entry->addr.msf.frame);
393 bcopy(toc->tab + track - toc->hdr.starting_track,
394 &te->entry, sizeof(struct cd_toc_entry));
395 if (te->address_format == CD_MSF_FORMAT)
400 #if __FreeBSD_version > 600008
401 case CDIOCREADSUBCHANNEL_SYSSPACE:
406 case CDIOCREADSUBCHANNEL:
408 struct ioc_read_subchannel *args =
409 (struct ioc_read_subchannel *)addr;
411 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
412 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
413 0, 0, 0, 0, 0, 0, 0 };
415 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
416 args->data_len < sizeof(struct cd_sub_channel_header)) {
421 format = args->data_format;
422 if ((format != CD_CURRENT_POSITION) &&
423 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
428 ccb[1] = args->address_format & CD_MSF_FORMAT;
430 if ((error = ata_atapicmd(dev, ccb, (caddr_t)&cdp->subchan,
431 sizeof(cdp->subchan), ATA_R_READ, 10)))
434 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
435 if (cdp->subchan.header.audio_status == 0x11) {
441 if (format == CD_TRACK_INFO)
442 ccb[6] = args->track;
444 if ((error = ata_atapicmd(dev, ccb, (caddr_t)&cdp->subchan,
445 sizeof(cdp->subchan),ATA_R_READ,10))){
449 if (nocopyout == 0) {
450 error = copyout(&cdp->subchan, args->data, args->data_len);
453 bcopy(&cdp->subchan, args->data, args->data_len);
460 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
464 msf2lba(args->start_m, args->start_s, args->start_f),
465 msf2lba(args->end_m, args->end_s, args->end_f));
469 case CDIOCPLAYBLOCKS:
471 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
473 error = acd_play(dev, args->blk, args->blk + args->len);
477 case CDIOCPLAYTRACKS:
479 struct ioc_play_track *args = (struct ioc_play_track *)addr;
482 if (!cdp->toc.hdr.ending_track) {
486 if (args->end_track < cdp->toc.hdr.ending_track + 1)
488 if (args->end_track > cdp->toc.hdr.ending_track + 1)
489 args->end_track = cdp->toc.hdr.ending_track + 1;
490 t1 = args->start_track - cdp->toc.hdr.starting_track;
491 t2 = args->end_track - cdp->toc.hdr.starting_track;
492 if (t1 < 0 || t2 < 0 ||
493 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
497 error = acd_play(dev, ntohl(cdp->toc.tab[t1].addr.lba),
498 ntohl(cdp->toc.tab[t2].addr.lba));
504 struct ioc_vol *arg = (struct ioc_vol *)addr;
506 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE,
507 (caddr_t)&cdp->au, sizeof(cdp->au))))
510 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
514 arg->vol[0] = cdp->au.port[0].volume;
515 arg->vol[1] = cdp->au.port[1].volume;
516 arg->vol[2] = cdp->au.port[2].volume;
517 arg->vol[3] = cdp->au.port[3].volume;
523 struct ioc_vol *arg = (struct ioc_vol *)addr;
525 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE,
526 (caddr_t)&cdp->au, sizeof(cdp->au))))
528 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
532 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE_MASK,
533 (caddr_t)&cdp->aumask,
534 sizeof(cdp->aumask))))
536 cdp->au.data_length = 0;
537 cdp->au.port[0].channels = CHANNEL_0;
538 cdp->au.port[1].channels = CHANNEL_1;
539 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
540 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
541 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
542 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
543 error = acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
549 struct ioc_patch *arg = (struct ioc_patch *)addr;
551 error = acd_setchan(dev, arg->patch[0], arg->patch[1],
552 arg->patch[2], arg->patch[3]);
557 error = acd_setchan(dev, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
561 error = acd_setchan(dev, CHANNEL_0, CHANNEL_1, 0, 0);
565 error = acd_setchan(dev, 0, 0, 0, 0);
569 error = acd_setchan(dev, CHANNEL_0, CHANNEL_0, 0, 0);
573 error = acd_setchan(dev, CHANNEL_1, CHANNEL_1, 0, 0);
577 error = acd_blank(dev, (*(int *)addr));
580 case CDRIOCNEXTWRITEABLEADDR:
582 struct acd_track_info track_info;
584 if ((error = acd_read_track_info(dev, 0xff, &track_info)))
587 if (!track_info.nwa_valid) {
591 *(int*)addr = track_info.next_writeable_addr;
595 case CDRIOCINITWRITER:
596 error = acd_init_writer(dev, (*(int *)addr));
599 case CDRIOCINITTRACK:
600 error = acd_init_track(dev, (struct cdr_track *)addr);
604 error = acd_flush(dev);
608 error = acd_fixate(dev, (*(int *)addr));
611 case CDRIOCREADSPEED:
613 int speed = *(int *)addr;
615 /* Preserve old behavior: units in multiples of CDROM speed */
618 error = acd_set_speed(dev, speed, CDR_MAX_SPEED);
622 case CDRIOCWRITESPEED:
624 int speed = *(int *)addr;
628 error = acd_set_speed(dev, CDR_MAX_SPEED, speed);
632 case CDRIOCGETBLOCKSIZE:
633 *(int *)addr = cdp->block_size;
636 case CDRIOCSETBLOCKSIZE:
637 cdp->block_size = *(int *)addr;
638 pp->sectorsize = cdp->block_size; /* hack for GEOM SOS */
642 case CDRIOCGETPROGRESS:
643 error = acd_get_progress(dev, (int *)addr);
647 error = acd_send_cue(dev, (struct cdr_cuesheet *)addr);
650 case CDRIOCREADFORMATCAPS:
651 error = acd_read_format_caps(dev, (struct cdr_format_capacities *)addr);
655 error = acd_format(dev, (struct cdr_format_params *)addr);
658 case DVDIOCREPORTKEY:
659 if (cdp->cap.media & MST_READ_DVDROM)
660 error = acd_report_key(dev, (struct dvd_authinfo *)addr);
666 if (cdp->cap.media & MST_READ_DVDROM)
667 error = acd_send_key(dev, (struct dvd_authinfo *)addr);
672 case DVDIOCREADSTRUCTURE:
673 if (cdp->cap.media & MST_READ_DVDROM)
674 error = acd_read_structure(dev, (struct dvd_struct *)addr);
680 error = ata_device_ioctl(dev, cmd, addr);
686 acd_geom_access(struct g_provider *pp, int dr, int dw, int de)
688 device_t dev = pp->geom->softc;
689 struct acd_softc *cdp = device_get_ivars(dev);
690 struct ata_request *request;
691 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
692 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
693 int timeout = 60, track;
695 if (!(request = ata_alloc_request()))
698 /* wait if drive is not finished loading the medium */
701 bcopy(ccb, request->u.atapi.ccb, 16);
702 request->flags = ATA_R_ATAPI;
703 request->timeout = 5;
704 ata_queue_request(request);
705 if (!request->error &&
706 (request->u.atapi.sense.key == 2 ||
707 request->u.atapi.sense.key == 7) &&
708 request->u.atapi.sense.asc == 4 &&
709 request->u.atapi.sense.ascq == 1)
710 pause("acdld", hz / 2);
714 ata_free_request(request);
717 acd_prevent_allow(dev, 1);
718 cdp->flags |= F_LOCKED;
722 if (dr + pp->acr == 0) {
723 acd_prevent_allow(dev, 0);
724 cdp->flags &= ~F_LOCKED;
727 if ((track = pp->index)) {
728 pp->sectorsize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
729 pp->mediasize = ntohl(cdp->toc.tab[track].addr.lba) -
730 ntohl(cdp->toc.tab[track - 1].addr.lba);
733 pp->sectorsize = cdp->block_size;
734 pp->mediasize = cdp->disk_size;
736 pp->mediasize *= pp->sectorsize;
742 acd_geom_start(struct bio *bp)
744 device_t dev = bp->bio_to->geom->softc;
745 struct acd_softc *cdp = device_get_ivars(dev);
747 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE) {
748 g_io_deliver(bp, EOPNOTSUPP);
752 if (bp->bio_cmd == BIO_READ && cdp->disk_size == -1) {
753 g_io_deliver(bp, EIO);
757 /* GEOM classes must do their own request limiting */
758 if (bp->bio_length <= cdp->iomax) {
759 bp->bio_pblkno = bp->bio_offset / bp->bio_to->sectorsize;
763 u_int pos, size = cdp->iomax - cdp->iomax % bp->bio_to->sectorsize;
764 struct bio *bp2, *bp3;
766 if (!(bp2 = g_clone_bio(bp)))
767 g_io_deliver(bp, EIO);
769 for (pos = 0; bp2; pos += size) {
771 bp2->bio_done = g_std_done;
772 bp2->bio_to = bp->bio_to;
773 bp2->bio_offset += pos;
774 bp2->bio_data += pos;
775 bp2->bio_length = bp->bio_length - pos;
776 if (bp2->bio_length > size) {
777 bp2->bio_length = size;
778 if (!(bp3 = g_clone_bio(bp)))
779 bp->bio_error = ENOMEM;
781 bp2->bio_pblkno = bp2->bio_offset / bp2->bio_to->sectorsize;
789 acd_strategy(struct bio *bp)
791 device_t dev = bp->bio_to->geom->softc;
792 struct ata_device *atadev = device_get_softc(dev);
793 struct acd_softc *cdp = device_get_ivars(dev);
794 struct ata_request *request;
795 u_int32_t lba, lastlba, count;
797 int track, blocksize;
799 /* reject all queued entries if media changed */
800 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
801 g_io_deliver(bp, EIO);
805 bzero(ccb, sizeof(ccb));
807 track = bp->bio_to->index;
810 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
811 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
812 lba = bp->bio_offset / blocksize;
813 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
816 blocksize = cdp->block_size;
817 lastlba = cdp->disk_size;
818 lba = bp->bio_offset / blocksize;
821 count = bp->bio_length / blocksize;
823 if (bp->bio_cmd == BIO_READ) {
824 /* if transfer goes beyond range adjust it to be within limits */
825 if (lba + count > lastlba) {
826 /* if we are entirely beyond EOM return EOF */
827 if (lastlba <= lba) {
831 count = lastlba - lba;
835 ccb[0] = ATAPI_READ_BIG;
839 ccb[0] = ATAPI_READ_CD;
844 ccb[0] = ATAPI_READ_CD;
849 ccb[0] = ATAPI_WRITE_BIG;
860 if (!(request = ata_alloc_request())) {
861 g_io_deliver(bp, ENOMEM);
866 bcopy(ccb, request->u.atapi.ccb,
867 (atadev->param.config & ATA_PROTO_MASK) ==
868 ATA_PROTO_ATAPI_12 ? 16 : 12);
869 request->data = bp->bio_data;
870 request->bytecount = count * blocksize;
871 request->transfersize = min(request->bytecount, 65534);
872 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
873 request->retries = 2;
874 request->callback = acd_done;
875 request->flags = ATA_R_ATAPI;
876 if (atadev->mode >= ATA_DMA)
877 request->flags |= ATA_R_DMA;
878 switch (bp->bio_cmd) {
880 request->flags |= ATA_R_READ;
883 request->flags |= ATA_R_WRITE;
886 device_printf(dev, "unknown BIO operation\n");
887 ata_free_request(request);
888 g_io_deliver(bp, EIO);
891 ata_queue_request(request);
895 acd_done(struct ata_request *request)
897 struct bio *bp = request->bio;
899 /* finish up transfer */
900 bp->bio_completed = request->donecount;
901 g_io_deliver(bp, request->result);
902 ata_free_request(request);
906 acd_set_ioparm(device_t dev)
908 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
909 struct acd_softc *cdp = device_get_ivars(dev);
912 max_iosize = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
914 cdp->iomax = min(max_iosize, 65534);
918 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
922 *m = lba / (60 * 75);
929 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
931 return (m * 60 + s) * 75 + f - 150;
935 acd_read_toc(device_t dev)
937 struct ata_device *atadev = device_get_softc(dev);
938 struct acd_softc *cdp = device_get_ivars(dev);
939 struct g_provider *pp;
942 int track, ntracks, len;
944 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
945 bzero(&cdp->toc, sizeof(cdp->toc));
946 cdp->disk_size = -1; /* hack for GEOM SOS */
948 if (acd_test_ready(dev))
951 bzero(ccb, sizeof(ccb));
952 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
953 ccb[0] = ATAPI_READ_TOC;
956 if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
957 ATA_R_READ | ATA_R_QUIET, 30)) {
958 bzero(&cdp->toc, sizeof(cdp->toc));
961 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
962 if (ntracks <= 0 || ntracks > MAXTRK) {
963 bzero(&cdp->toc, sizeof(cdp->toc));
967 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
968 bzero(ccb, sizeof(ccb));
969 ccb[0] = ATAPI_READ_TOC;
972 if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
973 ATA_R_READ | ATA_R_QUIET, 30)) {
974 bzero(&cdp->toc, sizeof(cdp->toc));
977 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
979 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
981 bzero(ccb, sizeof(ccb));
982 ccb[0] = ATAPI_READ_CAPACITY;
983 if (ata_atapicmd(dev, ccb, (caddr_t)sizes, sizeof(sizes),
984 ATA_R_READ | ATA_R_QUIET, 30)) {
985 bzero(&cdp->toc, sizeof(cdp->toc));
988 cdp->disk_size = ntohl(sizes[0]) + 1;
990 for (track = 1; track <= ntracks; track ++) {
991 if (cdp->pp[track] != NULL)
993 pp = g_new_providerf(cdp->gp, "acd%dt%02d", device_get_unit(dev),track);
996 g_error_provider(pp, 0);
998 for (; track < MAXTRK; track ++) {
999 if (cdp->pp[track] == NULL)
1001 cdp->pp[track]->flags |= G_PF_WITHER;
1002 g_orphan_provider(cdp->pp[track], ENXIO);
1003 cdp->pp[track] = NULL;
1007 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1008 device_printf(dev, "(%d sectors (%d bytes)), %d tracks ",
1009 cdp->disk_size, cdp->block_size,
1010 cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track+1);
1011 if (cdp->toc.tab[0].control & 4)
1012 printf("%dMB\n", cdp->disk_size * cdp->block_size / 1048576);
1014 printf("%d:%d audio\n",
1015 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1021 acd_play(device_t dev, int start, int end)
1025 bzero(ccb, sizeof(ccb));
1026 ccb[0] = ATAPI_PLAY_MSF;
1027 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1028 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1029 return ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
1033 acd_setchan(device_t dev, u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1035 struct acd_softc *cdp = device_get_ivars(dev);
1038 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1041 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1043 cdp->au.data_length = 0;
1044 cdp->au.port[0].channels = c0;
1045 cdp->au.port[1].channels = c1;
1046 cdp->au.port[2].channels = c2;
1047 cdp->au.port[3].channels = c3;
1048 return acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
1052 acd_init_writer(device_t dev, int test_write)
1056 bzero(ccb, sizeof(ccb));
1057 ccb[0] = ATAPI_REZERO;
1058 ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1059 ccb[0] = ATAPI_SEND_OPC_INFO;
1061 ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 30);
1066 acd_fixate(device_t dev, int multisession)
1068 struct acd_softc *cdp = device_get_ivars(dev);
1069 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1070 0, 0, 0, 0, 0, 0, 0, 0 };
1071 int timeout = 5*60*2;
1073 struct write_param param;
1075 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1076 (caddr_t)¶m, sizeof(param))))
1079 param.data_length = 0;
1081 param.session_type = CDR_SESS_MULTI;
1083 param.session_type = CDR_SESS_NONE;
1085 if ((error = acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10)))
1088 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1092 /* some drives just return ready, wait for the expected fixate time */
1093 if ((error = acd_test_ready(dev)) != EBUSY) {
1094 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1095 pause("acdfix", timeout * hz / 2);
1096 return acd_test_ready(dev);
1099 while (timeout-- > 0) {
1100 if ((error = acd_get_progress(dev, &dummy)))
1102 if ((error = acd_test_ready(dev)) != EBUSY)
1104 pause("acdcld", hz / 2);
1110 acd_init_track(device_t dev, struct cdr_track *track)
1112 struct acd_softc *cdp = device_get_ivars(dev);
1113 struct write_param param;
1116 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1117 (caddr_t)¶m, sizeof(param))))
1120 param.data_length = 0;
1121 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1122 param.page_length = 0x32;
1123 param.test_write = track->test_write ? 1 : 0;
1124 param.write_type = CDR_WTYPE_TRACK;
1125 param.session_type = CDR_SESS_NONE;
1127 param.packet_size = 0;
1129 if (cdp->cap.capabilities & MST_BURNPROOF)
1130 param.burnproof = 1;
1132 switch (track->datablock_type) {
1136 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1138 param.track_mode = CDR_TMODE_AUDIO;
1139 cdp->block_size = 2352;
1140 param.datablock_type = CDR_DB_RAW;
1141 param.session_format = CDR_SESS_CDROM;
1144 case CDR_DB_ROM_MODE1:
1145 cdp->block_size = 2048;
1146 param.track_mode = CDR_TMODE_DATA;
1147 param.datablock_type = CDR_DB_ROM_MODE1;
1148 param.session_format = CDR_SESS_CDROM;
1151 case CDR_DB_ROM_MODE2:
1152 cdp->block_size = 2336;
1153 param.track_mode = CDR_TMODE_DATA;
1154 param.datablock_type = CDR_DB_ROM_MODE2;
1155 param.session_format = CDR_SESS_CDROM;
1158 case CDR_DB_XA_MODE1:
1159 cdp->block_size = 2048;
1160 param.track_mode = CDR_TMODE_DATA;
1161 param.datablock_type = CDR_DB_XA_MODE1;
1162 param.session_format = CDR_SESS_CDROM_XA;
1165 case CDR_DB_XA_MODE2_F1:
1166 cdp->block_size = 2056;
1167 param.track_mode = CDR_TMODE_DATA;
1168 param.datablock_type = CDR_DB_XA_MODE2_F1;
1169 param.session_format = CDR_SESS_CDROM_XA;
1172 case CDR_DB_XA_MODE2_F2:
1173 cdp->block_size = 2324;
1174 param.track_mode = CDR_TMODE_DATA;
1175 param.datablock_type = CDR_DB_XA_MODE2_F2;
1176 param.session_format = CDR_SESS_CDROM_XA;
1179 case CDR_DB_XA_MODE2_MIX:
1180 cdp->block_size = 2332;
1181 param.track_mode = CDR_TMODE_DATA;
1182 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1183 param.session_format = CDR_SESS_CDROM_XA;
1186 acd_set_ioparm(dev);
1187 return acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10);
1191 acd_flush(device_t dev)
1193 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1194 0, 0, 0, 0, 0, 0, 0, 0 };
1196 return ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1200 acd_read_track_info(device_t dev, int32_t lba, struct acd_track_info *info)
1202 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1203 lba>>24, lba>>16, lba>>8, lba, 0,
1204 sizeof(*info)>>8, sizeof(*info),
1205 0, 0, 0, 0, 0, 0, 0 };
1208 if ((error = ata_atapicmd(dev, ccb, (caddr_t)info, sizeof(*info),
1211 info->track_start_addr = ntohl(info->track_start_addr);
1212 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1213 info->free_blocks = ntohl(info->free_blocks);
1214 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1215 info->track_length = ntohl(info->track_length);
1220 acd_get_progress(device_t dev, int *finished)
1222 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1223 0, 0, 0, 0, 0, 0, 0, 0 };
1224 struct ata_request *request;
1227 if (!(request = ata_alloc_request()))
1231 bcopy(ccb, request->u.atapi.ccb, 16);
1232 request->data = dummy;
1233 request->bytecount = sizeof(dummy);
1234 request->transfersize = min(request->bytecount, 65534);
1235 request->flags = ATA_R_ATAPI | ATA_R_READ;
1236 request->timeout = 30;
1237 ata_queue_request(request);
1238 if (!request->error &&
1239 request->u.atapi.sense.specific & ATA_SENSE_SPEC_VALID)
1240 *finished = ((request->u.atapi.sense.specific2 |
1241 (request->u.atapi.sense.specific1 << 8)) * 100) / 65535;
1244 ata_free_request(request);
1249 acd_send_cue(device_t dev, struct cdr_cuesheet *cuesheet)
1251 struct acd_softc *cdp = device_get_ivars(dev);
1252 struct write_param param;
1253 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1254 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1255 0, 0, 0, 0, 0, 0, 0 };
1259 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1260 (caddr_t)¶m, sizeof(param))))
1263 param.data_length = 0;
1264 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1265 param.page_length = 0x32;
1266 param.test_write = cuesheet->test_write ? 1 : 0;
1267 param.write_type = CDR_WTYPE_SESSION;
1268 param.session_type = cuesheet->session_type;
1270 param.packet_size = 0;
1271 param.track_mode = CDR_TMODE_AUDIO;
1272 param.datablock_type = CDR_DB_RAW;
1273 param.session_format = cuesheet->session_format;
1274 if (cdp->cap.capabilities & MST_BURNPROOF)
1275 param.burnproof = 1;
1277 if ((error = acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10)))
1280 if (!(buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT)))
1283 if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1284 error = ata_atapicmd(dev, ccb, buffer, cuesheet->len, 0, 30);
1285 free(buffer, M_ACD);
1290 acd_report_key(device_t dev, struct dvd_authinfo *ai)
1292 struct dvd_miscauth *d = NULL;
1298 switch (ai->format) {
1299 case DVD_REPORT_AGID:
1300 case DVD_REPORT_ASF:
1301 case DVD_REPORT_RPC:
1304 case DVD_REPORT_KEY1:
1307 case DVD_REPORT_TITLE_KEY:
1311 case DVD_REPORT_CHALLENGE:
1314 case DVD_INVALIDATE_AGID:
1321 bzero(ccb, sizeof(ccb));
1322 ccb[0] = ATAPI_REPORT_KEY;
1323 ccb[2] = (lba >> 24) & 0xff;
1324 ccb[3] = (lba >> 16) & 0xff;
1325 ccb[4] = (lba >> 8) & 0xff;
1326 ccb[5] = lba & 0xff;
1327 ccb[8] = (length >> 8) & 0xff;
1328 ccb[9] = length & 0xff;
1329 ccb[10] = (ai->agid << 6) | ai->format;
1332 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1334 d->length = htons(length - 2);
1337 error = ata_atapicmd(dev, ccb, (caddr_t)d, length,
1338 ai->format == DVD_INVALIDATE_AGID ? 0 : ATA_R_READ,10);
1345 switch (ai->format) {
1346 case DVD_REPORT_AGID:
1347 ai->agid = d->data[3] >> 6;
1350 case DVD_REPORT_CHALLENGE:
1351 bcopy(&d->data[0], &ai->keychal[0], 10);
1354 case DVD_REPORT_KEY1:
1355 bcopy(&d->data[0], &ai->keychal[0], 5);
1358 case DVD_REPORT_TITLE_KEY:
1359 ai->cpm = (d->data[0] >> 7);
1360 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1361 ai->cgms = (d->data[0] >> 4) & 0x3;
1362 bcopy(&d->data[1], &ai->keychal[0], 5);
1365 case DVD_REPORT_ASF:
1366 ai->asf = d->data[3] & 1;
1369 case DVD_REPORT_RPC:
1370 ai->reg_type = (d->data[0] >> 6);
1371 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1372 ai->user_rsts = d->data[0] & 0x7;
1373 ai->region = d->data[1];
1374 ai->rpc_scheme = d->data[2];
1377 case DVD_INVALIDATE_AGID:
1389 acd_send_key(device_t dev, struct dvd_authinfo *ai)
1391 struct dvd_miscauth *d;
1396 switch (ai->format) {
1397 case DVD_SEND_CHALLENGE:
1399 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1401 bcopy(ai->keychal, &d->data[0], 10);
1406 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1408 bcopy(&ai->keychal[0], &d->data[0], 5);
1413 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1415 d->data[0] = ai->region;
1422 bzero(ccb, sizeof(ccb));
1423 ccb[0] = ATAPI_SEND_KEY;
1424 ccb[8] = (length >> 8) & 0xff;
1425 ccb[9] = length & 0xff;
1426 ccb[10] = (ai->agid << 6) | ai->format;
1427 d->length = htons(length - 2);
1428 error = ata_atapicmd(dev, ccb, (caddr_t)d, length, 0, 10);
1434 acd_read_structure(device_t dev, struct dvd_struct *s)
1436 struct dvd_miscauth *d;
1442 case DVD_STRUCT_PHYSICAL:
1446 case DVD_STRUCT_COPYRIGHT:
1450 case DVD_STRUCT_DISCKEY:
1454 case DVD_STRUCT_BCA:
1458 case DVD_STRUCT_MANUFACT:
1462 case DVD_STRUCT_DDS:
1463 case DVD_STRUCT_PRERECORDED:
1464 case DVD_STRUCT_UNIQUEID:
1465 case DVD_STRUCT_LIST:
1466 case DVD_STRUCT_CMI:
1467 case DVD_STRUCT_RMD_LAST:
1468 case DVD_STRUCT_RMD_RMA:
1469 case DVD_STRUCT_DCB:
1476 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1478 d->length = htons(length - 2);
1480 bzero(ccb, sizeof(ccb));
1481 ccb[0] = ATAPI_READ_STRUCTURE;
1482 ccb[6] = s->layer_num;
1484 ccb[8] = (length >> 8) & 0xff;
1485 ccb[9] = length & 0xff;
1486 ccb[10] = s->agid << 6;
1487 error = ata_atapicmd(dev, ccb, (caddr_t)d, length, ATA_R_READ, 30);
1493 switch (s->format) {
1494 case DVD_STRUCT_PHYSICAL: {
1495 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1497 layer->book_type = d->data[0] >> 4;
1498 layer->book_version = d->data[0] & 0xf;
1499 layer->disc_size = d->data[1] >> 4;
1500 layer->max_rate = d->data[1] & 0xf;
1501 layer->nlayers = (d->data[2] >> 5) & 3;
1502 layer->track_path = (d->data[2] >> 4) & 1;
1503 layer->layer_type = d->data[2] & 0xf;
1504 layer->linear_density = d->data[3] >> 4;
1505 layer->track_density = d->data[3] & 0xf;
1506 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1507 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1508 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1509 layer->bca = d->data[16] >> 7;
1513 case DVD_STRUCT_COPYRIGHT:
1514 s->cpst = d->data[0];
1515 s->rmi = d->data[1];
1518 case DVD_STRUCT_DISCKEY:
1519 bcopy(&d->data[0], &s->data[0], 2048);
1522 case DVD_STRUCT_BCA:
1523 s->length = ntohs(d->length);
1524 bcopy(&d->data[0], &s->data[0], s->length);
1527 case DVD_STRUCT_MANUFACT:
1528 s->length = ntohs(d->length);
1529 bcopy(&d->data[0], &s->data[0], s->length);
1540 acd_tray(device_t dev, int close)
1542 struct ata_device *atadev = device_get_softc(dev);
1543 struct acd_softc *cdp = device_get_ivars(dev);
1546 if (cdp->cap.mechanism & MST_EJECT) {
1548 if (!(error = acd_start_stop(dev, 3))) {
1550 acd_prevent_allow(dev, 1);
1551 cdp->flags |= F_LOCKED;
1555 acd_start_stop(dev, 0);
1556 acd_prevent_allow(dev, 0);
1557 cdp->flags &= ~F_LOCKED;
1558 atadev->flags |= ATA_D_MEDIA_CHANGED;
1559 error = acd_start_stop(dev, 2);
1566 acd_blank(device_t dev, int blanktype)
1568 struct ata_device *atadev = device_get_softc(dev);
1569 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1570 0, 0, 0, 0, 0, 0, 0, 0 };
1572 atadev->flags |= ATA_D_MEDIA_CHANGED;
1573 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1577 acd_prevent_allow(device_t dev, int lock)
1579 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1582 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1586 acd_start_stop(device_t dev, int start)
1588 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1591 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1595 acd_pause_resume(device_t dev, int pause)
1597 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1598 0, 0, 0, 0, 0, 0, 0 };
1600 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1604 acd_mode_sense(device_t dev, int page, caddr_t pagebuf, int pagesize)
1606 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1607 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1610 error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
1615 acd_mode_select(device_t dev, caddr_t pagebuf, int pagesize)
1617 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1618 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1620 return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 30);
1624 acd_set_speed(device_t dev, int rdspeed, int wrspeed)
1626 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1627 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1630 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1637 acd_get_cap(device_t dev)
1639 struct acd_softc *cdp = device_get_ivars(dev);
1640 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_CDROM_CAP_PAGE,
1641 0, 0, 0, 0, sizeof(cdp->cap)>>8, sizeof(cdp->cap),
1642 0, 0, 0, 0, 0, 0, 0 };
1645 /* get drive capabilities, some bugridden drives needs this repeated */
1646 for (count = 0 ; count < 5 ; count++) {
1647 if (!ata_atapicmd(dev, ccb, (caddr_t)&cdp->cap, sizeof(cdp->cap),
1648 ATA_R_READ | ATA_R_QUIET, 5)) {
1649 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1650 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1651 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1652 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed),177);
1653 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1654 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
1660 acd_read_format_caps(device_t dev, struct cdr_format_capacities *caps)
1662 int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
1663 (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
1664 sizeof(struct cdr_format_capacities) & 0xff,
1665 0, 0, 0, 0, 0, 0, 0 };
1667 return ata_atapicmd(dev, ccb, (caddr_t)caps,
1668 sizeof(struct cdr_format_capacities), ATA_R_READ, 30);
1672 acd_format(device_t dev, struct cdr_format_params* params)
1674 int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
1678 error = ata_atapicmd(dev, ccb, (u_int8_t *)params,
1679 sizeof(struct cdr_format_params), 0, 30);
1684 acd_test_ready(device_t dev)
1686 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
1687 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1689 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1693 acd_describe(device_t dev)
1695 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1696 struct ata_device *atadev = device_get_softc(dev);
1697 struct acd_softc *cdp = device_get_ivars(dev);
1702 device_printf(dev, "<%.40s/%.8s> %s drive at ata%d as %s\n",
1703 atadev->param.model, atadev->param.revision,
1704 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1705 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1706 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1707 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1708 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM":"CDROM",
1709 device_get_unit(ch->dev), ata_unit2str(atadev));
1711 device_printf(dev, "%s", "");
1712 if (cdp->cap.cur_read_speed) {
1713 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
1714 if (cdp->cap.max_read_speed)
1715 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
1716 if ((cdp->cap.cur_write_speed) &&
1717 (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
1718 MST_WRITE_DVDR | MST_WRITE_DVDRAM))) {
1719 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
1720 if (cdp->cap.max_write_speed)
1721 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
1725 if (cdp->cap.buf_size) {
1726 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
1729 printf("%s %s\n", comma ? "," : "", ata_mode2str(atadev->mode));
1731 device_printf(dev, "Reads:");
1733 if (cdp->cap.media & MST_READ_CDR) {
1734 printf(" CDR"); comma = 1;
1736 if (cdp->cap.media & MST_READ_CDRW) {
1737 printf("%s CDRW", comma ? "," : ""); comma = 1;
1739 if (cdp->cap.capabilities & MST_READ_CDDA) {
1740 if (cdp->cap.capabilities & MST_CDDA_STREAM)
1741 printf("%s CDDA stream", comma ? "," : "");
1743 printf("%s CDDA", comma ? "," : "");
1746 if (cdp->cap.media & MST_READ_DVDROM) {
1747 printf("%s DVDROM", comma ? "," : ""); comma = 1;
1749 if (cdp->cap.media & MST_READ_DVDR) {
1750 printf("%s DVDR", comma ? "," : ""); comma = 1;
1752 if (cdp->cap.media & MST_READ_DVDRAM) {
1753 printf("%s DVDRAM", comma ? "," : ""); comma = 1;
1755 if (cdp->cap.media & MST_READ_PACKET)
1756 printf("%s packet", comma ? "," : "");
1759 device_printf(dev, "Writes:");
1760 if (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
1761 MST_WRITE_DVDR | MST_WRITE_DVDRAM)) {
1763 if (cdp->cap.media & MST_WRITE_CDR) {
1764 printf(" CDR" ); comma = 1;
1766 if (cdp->cap.media & MST_WRITE_CDRW) {
1767 printf("%s CDRW", comma ? "," : ""); comma = 1;
1769 if (cdp->cap.media & MST_WRITE_DVDR) {
1770 printf("%s DVDR", comma ? "," : ""); comma = 1;
1772 if (cdp->cap.media & MST_WRITE_DVDRAM) {
1773 printf("%s DVDRAM", comma ? "," : ""); comma = 1;
1775 if (cdp->cap.media & MST_WRITE_TEST) {
1776 printf("%s test write", comma ? "," : ""); comma = 1;
1778 if (cdp->cap.capabilities & MST_BURNPROOF)
1779 printf("%s burnproof", comma ? "," : "");
1782 if (cdp->cap.capabilities & MST_AUDIO_PLAY) {
1783 device_printf(dev, "Audio: ");
1784 if (cdp->cap.capabilities & MST_AUDIO_PLAY)
1786 if (cdp->cap.max_vol_levels)
1787 printf(", %d volume levels", cdp->cap.max_vol_levels);
1790 device_printf(dev, "Mechanism: ");
1791 switch (cdp->cap.mechanism & MST_MECH_MASK) {
1792 case MST_MECH_CADDY:
1793 mechanism = "caddy"; break;
1795 mechanism = "tray"; break;
1796 case MST_MECH_POPUP:
1797 mechanism = "popup"; break;
1798 case MST_MECH_CHANGER:
1799 mechanism = "changer"; break;
1800 case MST_MECH_CARTRIDGE:
1801 mechanism = "cartridge"; break;
1803 mechanism = 0; break;
1806 printf("%s%s", (cdp->cap.mechanism & MST_EJECT) ?
1807 "ejectable " : "", mechanism);
1808 else if (cdp->cap.mechanism & MST_EJECT)
1809 printf("ejectable");
1811 if (cdp->cap.mechanism & MST_LOCKABLE)
1812 printf((cdp->cap.mechanism & MST_LOCKED) ? ", locked":", unlocked");
1813 if (cdp->cap.mechanism & MST_PREVENT)
1814 printf(", lock protected");
1817 if ((cdp->cap.mechanism & MST_MECH_MASK) != MST_MECH_CHANGER) {
1818 device_printf(dev, "Medium: ");
1819 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1821 printf("CD-ROM "); break;
1823 printf("CD-R "); break;
1825 printf("CD-RW "); break;
1827 printf("DVD "); break;
1829 printf("door open"); break;
1831 printf("no/blank disc"); break;
1833 printf("medium format error"); break;
1835 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
1836 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
1838 printf("120mm data disc"); break;
1840 printf("120mm audio disc"); break;
1842 printf("120mm data/audio disc"); break;
1844 printf("120mm photo disc"); break;
1846 printf("80mm data disc"); break;
1848 printf("80mm audio disc"); break;
1850 printf("80mm data/audio disc"); break;
1852 printf("80mm photo disc"); break;
1854 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1856 printf("unknown"); break;
1859 printf("blank"); break;
1863 printf("unknown (0x%x)", cdp->cap.medium_type); break;
1870 device_printf(dev, "%s ",
1871 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1872 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1873 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1874 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1875 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM" :
1877 if (cdp->changer_info)
1878 printf("with %d CD changer ", cdp->changer_info->slots);
1879 printf("<%.40s/%.8s> at ata%d-%s %s\n",
1880 atadev->param.model, atadev->param.revision,
1881 device_get_unit(ch->dev), ata_unit2str(atadev),
1882 ata_mode2str(atadev->mode) );
1886 static device_method_t acd_methods[] = {
1887 /* device interface */
1888 DEVMETHOD(device_probe, acd_probe),
1889 DEVMETHOD(device_attach, acd_attach),
1890 DEVMETHOD(device_detach, acd_detach),
1891 DEVMETHOD(device_shutdown, acd_shutdown),
1894 DEVMETHOD(ata_reinit, acd_reinit),
1899 static driver_t acd_driver = {
1905 static devclass_t acd_devclass;
1908 acd_modevent(module_t mod, int what, void *arg)
1910 g_modevent(0, what, &acd_class);
1914 DRIVER_MODULE(acd, ata, acd_driver, acd_devclass, acd_modevent, NULL);
1915 MODULE_VERSION(acd, 1);
1916 MODULE_DEPEND(acd, ata, 1, 1, 1);