2 * Copyright (c) 1998 - 2005 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
43 #include <sys/cdrio.h>
44 #include <sys/dvdio.h>
46 #include <sys/fcntl.h>
48 #include <sys/ctype.h>
50 #include <sys/taskqueue.h>
52 #include <machine/bus.h>
53 #include <geom/geom.h>
54 #include <dev/ata/ata-all.h>
55 #include <dev/ata/atapi-cd.h>
59 static void acd_geom_attach(void *, int);
60 static void acd_geom_detach(void *, int);
61 static void acd_set_ioparm(device_t);
62 static void acd_describe(device_t);
63 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
64 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
65 static int acd_geom_access(struct g_provider *, int, int, int);
66 static g_ioctl_t acd_geom_ioctl;
67 static void acd_geom_start(struct bio *);
68 static void acd_strategy(struct bio *);
69 static void acd_done(struct ata_request *);
70 static void acd_read_toc(device_t);
71 static int acd_play(device_t, int, int);
72 static int acd_setchan(device_t, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
73 static int acd_init_writer(device_t, int);
74 static int acd_fixate(device_t, int);
75 static int acd_init_track(device_t, struct cdr_track *);
76 static int acd_flush(device_t);
77 static int acd_read_track_info(device_t, int32_t, struct acd_track_info *);
78 static int acd_get_progress(device_t, int *);
79 static int acd_send_cue(device_t, struct cdr_cuesheet *);
80 static int acd_report_key(device_t, struct dvd_authinfo *);
81 static int acd_send_key(device_t, struct dvd_authinfo *);
82 static int acd_read_structure(device_t, struct dvd_struct *);
83 static int acd_tray(device_t, int);
84 static int acd_blank(device_t, int);
85 static int acd_prevent_allow(device_t, int);
86 static int acd_start_stop(device_t, int);
87 static int acd_pause_resume(device_t, int);
88 static int acd_mode_sense(device_t, int, caddr_t, int);
89 static int acd_mode_select(device_t, caddr_t, int);
90 static int acd_set_speed(device_t, int, int);
91 static void acd_get_cap(device_t);
92 static int acd_read_format_caps(device_t, struct cdr_format_capacities *);
93 static int acd_format(device_t, struct cdr_format_params *);
94 static int acd_test_ready(device_t);
97 static MALLOC_DEFINE(M_ACD, "acd_driver", "ATAPI CD driver buffers");
98 static struct g_class acd_class = {
100 .version = G_VERSION,
101 .access = acd_geom_access,
102 .ioctl = acd_geom_ioctl,
103 .start = acd_geom_start,
105 //DECLARE_GEOM_CLASS(acd_class, acd);
108 acd_probe(device_t dev)
110 struct ata_device *atadev = device_get_softc(dev);
112 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
113 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_CDROM)
120 acd_attach(device_t dev)
122 struct acd_softc *cdp;
124 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO))) {
125 device_printf(dev, "out of memory\n");
128 cdp->block_size = 2048;
129 device_set_ivars(dev, cdp);
130 ATA_SETMODE(device_get_parent(dev), dev);
131 ata_controlcmd(dev, ATA_DEVICE_RESET, 0, 0, 0);
133 g_post_event(acd_geom_attach, dev, M_WAITOK, NULL);
135 /* announce we are here */
141 acd_detach(device_t dev)
143 g_waitfor_event(acd_geom_detach, dev, M_WAITOK, NULL);
148 acd_shutdown(device_t dev)
150 struct ata_device *atadev = device_get_softc(dev);
152 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
153 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);
161 struct acd_softc *cdp = device_get_ivars(dev);
163 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
164 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
165 device_set_ivars(dev, NULL);
169 ATA_SETMODE(device_get_parent(dev), dev);
174 acd_geom_attach(void *arg, int flag)
176 struct ata_device *atadev = device_get_softc(arg);
177 struct acd_softc *cdp = device_get_ivars(arg);
179 struct g_provider *pp;
182 gp = g_new_geomf(&acd_class, "acd%d", device_get_unit(arg));
185 pp = g_new_providerf(gp, "acd%d", device_get_unit(arg));
188 g_error_provider(pp, 0);
189 atadev->flags |= ATA_D_MEDIA_CHANGED;
194 acd_geom_detach(void *arg, int flag)
196 struct acd_softc *cdp = device_get_ivars(arg);
198 /* signal geom so we dont get any further requests */
199 g_wither_geom(cdp->gp, ENXIO);
201 /* fail requests on the queue and any thats "in flight" for this device */
202 ata_fail_requests(arg);
204 /* dont leave anything behind */
205 device_set_ivars(arg, NULL);
210 acd_geom_ioctl(struct g_provider *pp, u_long cmd, void *addr, int fflag, struct thread *td)
212 device_t dev = pp->geom->softc;
213 struct ata_device *atadev = device_get_softc(dev);
214 struct acd_softc *cdp = device_get_ivars(dev);
215 int error = 0, nocopyout = 0;
220 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
228 acd_prevent_allow(dev, 1);
229 cdp->flags |= F_LOCKED;
237 error = acd_pause_resume(dev, 1);
241 error = acd_pause_resume(dev, 0);
245 error = acd_start_stop(dev, 1);
249 error = acd_start_stop(dev, 0);
253 error = acd_prevent_allow(dev, 0);
254 cdp->flags &= ~F_LOCKED;
258 error = acd_prevent_allow(dev, 1);
259 cdp->flags |= F_LOCKED;
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 */
700 bzero(request, sizeof(struct ata_request));
702 bcopy(ccb, request->u.atapi.ccb, 16);
703 request->flags = ATA_R_ATAPI;
704 request->timeout = 5;
705 ata_queue_request(request);
706 if (!request->error &&
707 (request->u.atapi.sense_data.sense_key == 2 ||
708 request->u.atapi.sense_data.sense_key == 7) &&
709 request->u.atapi.sense_data.asc == 4 &&
710 request->u.atapi.sense_data.ascq == 1)
711 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
715 ata_free_request(request);
718 acd_prevent_allow(dev, 1);
719 cdp->flags |= F_LOCKED;
723 if (dr + pp->acr == 0) {
724 acd_prevent_allow(dev, 0);
725 cdp->flags &= ~F_LOCKED;
728 if ((track = pp->index)) {
729 pp->sectorsize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
730 pp->mediasize = ntohl(cdp->toc.tab[track].addr.lba) -
731 ntohl(cdp->toc.tab[track - 1].addr.lba);
734 pp->sectorsize = cdp->block_size;
735 pp->mediasize = cdp->disk_size;
737 pp->mediasize *= pp->sectorsize;
743 acd_geom_start(struct bio *bp)
745 device_t dev = bp->bio_to->geom->softc;
746 struct acd_softc *cdp = device_get_ivars(dev);
748 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE) {
749 g_io_deliver(bp, EOPNOTSUPP);
753 if (bp->bio_cmd == BIO_READ && cdp->disk_size == -1) {
754 g_io_deliver(bp, EIO);
758 /* GEOM classes must do their own request limiting */
759 if (bp->bio_length <= cdp->iomax) {
760 bp->bio_pblkno = bp->bio_offset / bp->bio_to->sectorsize;
764 u_int pos, size = cdp->iomax - cdp->iomax % bp->bio_to->sectorsize;
765 struct bio *bp2, *bp3;
767 if (!(bp2 = g_clone_bio(bp)))
768 g_io_deliver(bp, EIO);
770 for (pos = 0; bp2; pos += size) {
772 bp2->bio_done = g_std_done;
773 bp2->bio_to = bp->bio_to;
774 bp2->bio_offset += pos;
775 bp2->bio_data += pos;
776 bp2->bio_length = bp->bio_length - pos;
777 if (bp2->bio_length > size) {
778 bp2->bio_length = size;
779 if (!(bp3 = g_clone_bio(bp)))
780 bp->bio_error = ENOMEM;
782 bp2->bio_pblkno = bp2->bio_offset / bp2->bio_to->sectorsize;
790 acd_strategy(struct bio *bp)
792 device_t dev = bp->bio_to->geom->softc;
793 struct ata_device *atadev = device_get_softc(dev);
794 struct acd_softc *cdp = device_get_ivars(dev);
795 struct ata_request *request;
796 u_int32_t lba, lastlba, count;
798 int track, blocksize;
800 /* reject all queued entries if media changed */
801 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
802 g_io_deliver(bp, EIO);
806 bzero(ccb, sizeof(ccb));
808 track = bp->bio_to->index;
811 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
812 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
813 lba = bp->bio_offset / blocksize;
814 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
817 blocksize = cdp->block_size;
818 lastlba = cdp->disk_size;
819 lba = bp->bio_offset / blocksize;
822 count = bp->bio_length / blocksize;
824 if (bp->bio_cmd == BIO_READ) {
825 /* if transfer goes beyond range adjust it to be within limits */
826 if (lba + count > lastlba) {
827 /* if we are entirely beyond EOM return EOF */
828 if (lastlba <= lba) {
832 count = lastlba - lba;
836 ccb[0] = ATAPI_READ_BIG;
840 ccb[0] = ATAPI_READ_CD;
845 ccb[0] = ATAPI_READ_CD;
850 ccb[0] = ATAPI_WRITE_BIG;
861 if (!(request = ata_alloc_request())) {
862 g_io_deliver(bp, ENOMEM);
867 bcopy(ccb, request->u.atapi.ccb,
868 (atadev->param.config & ATA_PROTO_MASK) ==
869 ATA_PROTO_ATAPI_12 ? 16 : 12);
870 request->data = bp->bio_data;
871 request->bytecount = count * blocksize;
872 request->transfersize = min(request->bytecount, 65534);
873 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
874 request->retries = 2;
875 request->callback = acd_done;
876 request->flags = ATA_R_ATAPI;
877 if (atadev->mode >= ATA_DMA)
878 request->flags |= ATA_R_DMA;
879 switch (bp->bio_cmd) {
881 request->flags |= ATA_R_READ;
884 request->flags |= ATA_R_WRITE;
887 device_printf(dev, "unknown BIO operation\n");
888 ata_free_request(request);
889 g_io_deliver(bp, EIO);
892 ata_queue_request(request);
896 acd_done(struct ata_request *request)
898 struct bio *bp = request->bio;
900 /* finish up transfer */
901 bp->bio_completed = request->donecount;
902 g_io_deliver(bp, request->result);
903 ata_free_request(request);
907 acd_set_ioparm(device_t dev)
909 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
910 struct acd_softc *cdp = device_get_ivars(dev);
913 cdp->iomax = min(ch->dma->max_iosize, 65534);
915 cdp->iomax = min(DFLTPHYS, 65534);
919 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
923 *m = lba / (60 * 75);
930 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
932 return (m * 60 + s) * 75 + f - 150;
936 acd_read_toc(device_t dev)
938 struct ata_device *atadev = device_get_softc(dev);
939 struct acd_softc *cdp = device_get_ivars(dev);
940 struct g_provider *pp;
943 int track, ntracks, len;
945 if (!(atadev->flags & ATA_D_MEDIA_CHANGED))
948 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
949 bzero(&cdp->toc, sizeof(cdp->toc));
950 cdp->disk_size = -1; /* hack for GEOM SOS */
952 if (acd_test_ready(dev))
955 bzero(ccb, sizeof(ccb));
956 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
957 ccb[0] = ATAPI_READ_TOC;
960 if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
961 ATA_R_READ | ATA_R_QUIET, 30)) {
962 bzero(&cdp->toc, sizeof(cdp->toc));
965 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
966 if (ntracks <= 0 || ntracks > MAXTRK) {
967 bzero(&cdp->toc, sizeof(cdp->toc));
971 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
972 bzero(ccb, sizeof(ccb));
973 ccb[0] = ATAPI_READ_TOC;
976 if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
977 ATA_R_READ | ATA_R_QUIET, 30)) {
978 bzero(&cdp->toc, sizeof(cdp->toc));
981 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
983 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
985 bzero(ccb, sizeof(ccb));
986 ccb[0] = ATAPI_READ_CAPACITY;
987 if (ata_atapicmd(dev, ccb, (caddr_t)sizes, sizeof(sizes),
988 ATA_R_READ | ATA_R_QUIET, 30)) {
989 bzero(&cdp->toc, sizeof(cdp->toc));
992 cdp->disk_size = ntohl(sizes[0]) + 1;
994 for (track = 1; track <= ntracks; track ++) {
995 if (cdp->pp[track] != NULL)
997 pp = g_new_providerf(cdp->gp, "acd%dt%02d", device_get_unit(dev),track);
1000 g_error_provider(pp, 0);
1002 for (; track < MAXTRK; track ++) {
1003 if (cdp->pp[track] == NULL)
1005 cdp->pp[track]->flags |= G_PF_WITHER;
1006 g_orphan_provider(cdp->pp[track], ENXIO);
1007 cdp->pp[track] = NULL;
1011 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1012 device_printf(dev, "(%d sectors (%d bytes)), %d tracks ",
1013 cdp->disk_size, cdp->block_size,
1014 cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track+1);
1015 if (cdp->toc.tab[0].control & 4)
1016 printf("%dMB\n", cdp->disk_size / 512);
1018 printf("%d:%d audio\n",
1019 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1025 acd_play(device_t dev, int start, int end)
1029 bzero(ccb, sizeof(ccb));
1030 ccb[0] = ATAPI_PLAY_MSF;
1031 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1032 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1033 return ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
1037 acd_setchan(device_t dev, u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1039 struct acd_softc *cdp = device_get_ivars(dev);
1042 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1045 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1047 cdp->au.data_length = 0;
1048 cdp->au.port[0].channels = c0;
1049 cdp->au.port[1].channels = c1;
1050 cdp->au.port[2].channels = c2;
1051 cdp->au.port[3].channels = c3;
1052 return acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
1056 acd_init_writer(device_t dev, int test_write)
1060 bzero(ccb, sizeof(ccb));
1061 ccb[0] = ATAPI_REZERO;
1062 ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1063 ccb[0] = ATAPI_SEND_OPC_INFO;
1065 ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 30);
1070 acd_fixate(device_t dev, int multisession)
1072 struct acd_softc *cdp = device_get_ivars(dev);
1073 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1074 0, 0, 0, 0, 0, 0, 0, 0 };
1075 int timeout = 5*60*2;
1077 struct write_param param;
1079 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1080 (caddr_t)¶m, sizeof(param))))
1083 param.data_length = 0;
1085 param.session_type = CDR_SESS_MULTI;
1087 param.session_type = CDR_SESS_NONE;
1089 if ((error = acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10)))
1092 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1096 /* some drives just return ready, wait for the expected fixate time */
1097 if ((error = acd_test_ready(dev)) != EBUSY) {
1098 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1099 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1100 return acd_test_ready(dev);
1103 while (timeout-- > 0) {
1104 if ((error = acd_get_progress(dev, &dummy)))
1106 if ((error = acd_test_ready(dev)) != EBUSY)
1108 tsleep(&error, PRIBIO, "acdcld", hz / 2);
1114 acd_init_track(device_t dev, struct cdr_track *track)
1116 struct acd_softc *cdp = device_get_ivars(dev);
1117 struct write_param param;
1120 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1121 (caddr_t)¶m, sizeof(param))))
1124 param.data_length = 0;
1125 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1126 param.page_length = 0x32;
1127 param.test_write = track->test_write ? 1 : 0;
1128 param.write_type = CDR_WTYPE_TRACK;
1129 param.session_type = CDR_SESS_NONE;
1131 param.packet_size = 0;
1133 if (cdp->cap.capabilities & MST_BURNPROOF)
1134 param.burnproof = 1;
1136 switch (track->datablock_type) {
1140 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1142 param.track_mode = CDR_TMODE_AUDIO;
1143 cdp->block_size = 2352;
1144 param.datablock_type = CDR_DB_RAW;
1145 param.session_format = CDR_SESS_CDROM;
1148 case CDR_DB_ROM_MODE1:
1149 cdp->block_size = 2048;
1150 param.track_mode = CDR_TMODE_DATA;
1151 param.datablock_type = CDR_DB_ROM_MODE1;
1152 param.session_format = CDR_SESS_CDROM;
1155 case CDR_DB_ROM_MODE2:
1156 cdp->block_size = 2336;
1157 param.track_mode = CDR_TMODE_DATA;
1158 param.datablock_type = CDR_DB_ROM_MODE2;
1159 param.session_format = CDR_SESS_CDROM;
1162 case CDR_DB_XA_MODE1:
1163 cdp->block_size = 2048;
1164 param.track_mode = CDR_TMODE_DATA;
1165 param.datablock_type = CDR_DB_XA_MODE1;
1166 param.session_format = CDR_SESS_CDROM_XA;
1169 case CDR_DB_XA_MODE2_F1:
1170 cdp->block_size = 2056;
1171 param.track_mode = CDR_TMODE_DATA;
1172 param.datablock_type = CDR_DB_XA_MODE2_F1;
1173 param.session_format = CDR_SESS_CDROM_XA;
1176 case CDR_DB_XA_MODE2_F2:
1177 cdp->block_size = 2324;
1178 param.track_mode = CDR_TMODE_DATA;
1179 param.datablock_type = CDR_DB_XA_MODE2_F2;
1180 param.session_format = CDR_SESS_CDROM_XA;
1183 case CDR_DB_XA_MODE2_MIX:
1184 cdp->block_size = 2332;
1185 param.track_mode = CDR_TMODE_DATA;
1186 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1187 param.session_format = CDR_SESS_CDROM_XA;
1190 acd_set_ioparm(dev);
1191 return acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10);
1195 acd_flush(device_t dev)
1197 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1198 0, 0, 0, 0, 0, 0, 0, 0 };
1200 return ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1204 acd_read_track_info(device_t dev, int32_t lba, struct acd_track_info *info)
1206 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1207 lba>>24, lba>>16, lba>>8, lba, 0,
1208 sizeof(*info)>>8, sizeof(*info),
1209 0, 0, 0, 0, 0, 0, 0 };
1212 if ((error = ata_atapicmd(dev, ccb, (caddr_t)info, sizeof(*info),
1215 info->track_start_addr = ntohl(info->track_start_addr);
1216 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1217 info->free_blocks = ntohl(info->free_blocks);
1218 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1219 info->track_length = ntohl(info->track_length);
1224 acd_get_progress(device_t dev, int *finished)
1226 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1227 0, 0, 0, 0, 0, 0, 0, 0 };
1228 struct ata_request *request;
1231 if (!(request = ata_alloc_request()))
1235 bcopy(ccb, request->u.atapi.ccb, 16);
1236 request->data = dummy;
1237 request->bytecount = sizeof(dummy);
1238 request->transfersize = min(request->bytecount, 65534);
1239 request->flags = ATA_R_ATAPI | ATA_R_READ;
1240 request->timeout = 30;
1241 ata_queue_request(request);
1242 if (!request->error && request->u.atapi.sense_data.sksv)
1243 *finished = ((request->u.atapi.sense_data.sk_specific2 |
1244 (request->u.atapi.sense_data.sk_specific1<<8))*100)/65535;
1247 ata_free_request(request);
1252 acd_send_cue(device_t dev, struct cdr_cuesheet *cuesheet)
1254 struct acd_softc *cdp = device_get_ivars(dev);
1255 struct write_param param;
1256 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1257 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1258 0, 0, 0, 0, 0, 0, 0 };
1262 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1263 (caddr_t)¶m, sizeof(param))))
1266 param.data_length = 0;
1267 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1268 param.page_length = 0x32;
1269 param.test_write = cuesheet->test_write ? 1 : 0;
1270 param.write_type = CDR_WTYPE_SESSION;
1271 param.session_type = cuesheet->session_type;
1273 param.packet_size = 0;
1274 param.track_mode = CDR_TMODE_AUDIO;
1275 param.datablock_type = CDR_DB_RAW;
1276 param.session_format = cuesheet->session_format;
1277 if (cdp->cap.capabilities & MST_BURNPROOF)
1278 param.burnproof = 1;
1280 if ((error = acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10)))
1283 if (!(buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT)))
1286 if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1287 error = ata_atapicmd(dev, ccb, buffer, cuesheet->len, 0, 30);
1288 free(buffer, M_ACD);
1293 acd_report_key(device_t dev, struct dvd_authinfo *ai)
1295 struct dvd_miscauth *d = NULL;
1301 switch (ai->format) {
1302 case DVD_REPORT_AGID:
1303 case DVD_REPORT_ASF:
1304 case DVD_REPORT_RPC:
1307 case DVD_REPORT_KEY1:
1310 case DVD_REPORT_TITLE_KEY:
1314 case DVD_REPORT_CHALLENGE:
1317 case DVD_INVALIDATE_AGID:
1324 bzero(ccb, sizeof(ccb));
1325 ccb[0] = ATAPI_REPORT_KEY;
1326 ccb[2] = (lba >> 24) & 0xff;
1327 ccb[3] = (lba >> 16) & 0xff;
1328 ccb[4] = (lba >> 8) & 0xff;
1329 ccb[5] = lba & 0xff;
1330 ccb[8] = (length >> 8) & 0xff;
1331 ccb[9] = length & 0xff;
1332 ccb[10] = (ai->agid << 6) | ai->format;
1335 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1337 d->length = htons(length - 2);
1340 error = ata_atapicmd(dev, ccb, (caddr_t)d, length,
1341 ai->format == DVD_INVALIDATE_AGID ? 0 : ATA_R_READ,10);
1347 switch (ai->format) {
1348 case DVD_REPORT_AGID:
1349 ai->agid = d->data[3] >> 6;
1352 case DVD_REPORT_CHALLENGE:
1353 bcopy(&d->data[0], &ai->keychal[0], 10);
1356 case DVD_REPORT_KEY1:
1357 bcopy(&d->data[0], &ai->keychal[0], 5);
1360 case DVD_REPORT_TITLE_KEY:
1361 ai->cpm = (d->data[0] >> 7);
1362 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1363 ai->cgms = (d->data[0] >> 4) & 0x3;
1364 bcopy(&d->data[1], &ai->keychal[0], 5);
1367 case DVD_REPORT_ASF:
1368 ai->asf = d->data[3] & 1;
1371 case DVD_REPORT_RPC:
1372 ai->reg_type = (d->data[0] >> 6);
1373 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1374 ai->user_rsts = d->data[0] & 0x7;
1375 ai->region = d->data[1];
1376 ai->rpc_scheme = d->data[2];
1379 case DVD_INVALIDATE_AGID:
1390 acd_send_key(device_t dev, struct dvd_authinfo *ai)
1392 struct dvd_miscauth *d;
1397 switch (ai->format) {
1398 case DVD_SEND_CHALLENGE:
1400 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1402 bcopy(ai->keychal, &d->data[0], 10);
1407 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1409 bcopy(&ai->keychal[0], &d->data[0], 5);
1414 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1416 d->data[0] = ai->region;
1423 bzero(ccb, sizeof(ccb));
1424 ccb[0] = ATAPI_SEND_KEY;
1425 ccb[8] = (length >> 8) & 0xff;
1426 ccb[9] = length & 0xff;
1427 ccb[10] = (ai->agid << 6) | ai->format;
1428 d->length = htons(length - 2);
1429 error = ata_atapicmd(dev, ccb, (caddr_t)d, length, 0, 10);
1435 acd_read_structure(device_t dev, struct dvd_struct *s)
1437 struct dvd_miscauth *d;
1443 case DVD_STRUCT_PHYSICAL:
1447 case DVD_STRUCT_COPYRIGHT:
1451 case DVD_STRUCT_DISCKEY:
1455 case DVD_STRUCT_BCA:
1459 case DVD_STRUCT_MANUFACT:
1463 case DVD_STRUCT_DDS:
1464 case DVD_STRUCT_PRERECORDED:
1465 case DVD_STRUCT_UNIQUEID:
1466 case DVD_STRUCT_LIST:
1467 case DVD_STRUCT_CMI:
1468 case DVD_STRUCT_RMD_LAST:
1469 case DVD_STRUCT_RMD_RMA:
1470 case DVD_STRUCT_DCB:
1477 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1479 d->length = htons(length - 2);
1481 bzero(ccb, sizeof(ccb));
1482 ccb[0] = ATAPI_READ_STRUCTURE;
1483 ccb[6] = s->layer_num;
1485 ccb[8] = (length >> 8) & 0xff;
1486 ccb[9] = length & 0xff;
1487 ccb[10] = s->agid << 6;
1488 error = ata_atapicmd(dev, ccb, (caddr_t)d, length, ATA_R_READ, 30);
1494 switch (s->format) {
1495 case DVD_STRUCT_PHYSICAL: {
1496 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1498 layer->book_type = d->data[0] >> 4;
1499 layer->book_version = d->data[0] & 0xf;
1500 layer->disc_size = d->data[1] >> 4;
1501 layer->max_rate = d->data[1] & 0xf;
1502 layer->nlayers = (d->data[2] >> 5) & 3;
1503 layer->track_path = (d->data[2] >> 4) & 1;
1504 layer->layer_type = d->data[2] & 0xf;
1505 layer->linear_density = d->data[3] >> 4;
1506 layer->track_density = d->data[3] & 0xf;
1507 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1508 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1509 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1510 layer->bca = d->data[16] >> 7;
1514 case DVD_STRUCT_COPYRIGHT:
1515 s->cpst = d->data[0];
1516 s->rmi = d->data[0];
1519 case DVD_STRUCT_DISCKEY:
1520 bcopy(&d->data[0], &s->data[0], 2048);
1523 case DVD_STRUCT_BCA:
1524 s->length = ntohs(d->length);
1525 bcopy(&d->data[0], &s->data[0], s->length);
1528 case DVD_STRUCT_MANUFACT:
1529 s->length = ntohs(d->length);
1530 bcopy(&d->data[0], &s->data[0], s->length);
1541 acd_tray(device_t dev, int close)
1543 struct ata_device *atadev = device_get_softc(dev);
1544 struct acd_softc *cdp = device_get_ivars(dev);
1547 if (cdp->cap.mechanism & MST_EJECT) {
1549 if (!(error = acd_start_stop(dev, 3))) {
1551 acd_prevent_allow(dev, 1);
1552 cdp->flags |= F_LOCKED;
1556 acd_start_stop(dev, 0);
1557 acd_prevent_allow(dev, 0);
1558 cdp->flags &= ~F_LOCKED;
1559 atadev->flags |= ATA_D_MEDIA_CHANGED;
1560 error = acd_start_stop(dev, 2);
1567 acd_blank(device_t dev, int blanktype)
1569 struct ata_device *atadev = device_get_softc(dev);
1570 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1571 0, 0, 0, 0, 0, 0, 0, 0 };
1573 atadev->flags |= ATA_D_MEDIA_CHANGED;
1574 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1578 acd_prevent_allow(device_t dev, int lock)
1580 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1583 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1587 acd_start_stop(device_t dev, int start)
1589 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1592 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1596 acd_pause_resume(device_t dev, int pause)
1598 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1599 0, 0, 0, 0, 0, 0, 0 };
1601 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1605 acd_mode_sense(device_t dev, int page, caddr_t pagebuf, int pagesize)
1607 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1608 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1611 error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
1616 acd_mode_select(device_t dev, caddr_t pagebuf, int pagesize)
1618 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1619 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1621 return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 30);
1625 acd_set_speed(device_t dev, int rdspeed, int wrspeed)
1627 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1628 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1631 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1638 acd_get_cap(device_t dev)
1640 struct acd_softc *cdp = device_get_ivars(dev);
1643 /* get drive capabilities, some bugridden drives needs this repeated */
1644 for (count = 0 ; count < 5 ; count++) {
1645 if (!acd_mode_sense(dev, ATAPI_CDROM_CAP_PAGE,
1646 (caddr_t)&cdp->cap, sizeof(cdp->cap)) &&
1647 cdp->cap.page_code == ATAPI_CDROM_CAP_PAGE) {
1648 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1649 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1650 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1651 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed),177);
1652 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1653 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
1659 acd_read_format_caps(device_t dev, struct cdr_format_capacities *caps)
1661 int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
1662 (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
1663 sizeof(struct cdr_format_capacities) & 0xff,
1664 0, 0, 0, 0, 0, 0, 0 };
1666 return ata_atapicmd(dev, ccb, (caddr_t)caps,
1667 sizeof(struct cdr_format_capacities), ATA_R_READ, 30);
1671 acd_format(device_t dev, struct cdr_format_params* params)
1673 int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
1677 error = ata_atapicmd(dev, ccb, (u_int8_t *)params,
1678 sizeof(struct cdr_format_params), 0, 30);
1683 acd_test_ready(device_t dev)
1685 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
1686 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1688 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1692 acd_describe(device_t dev)
1694 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1695 struct ata_device *atadev = device_get_softc(dev);
1696 struct acd_softc *cdp = device_get_ivars(dev);
1701 device_printf(dev, "<%.40s/%.8s> %s drive at ata%d as %s\n",
1702 atadev->param.model, atadev->param.revision,
1703 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1704 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1705 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1706 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1707 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM":"CDROM",
1708 device_get_unit(ch->dev),
1709 (atadev->unit == ATA_MASTER) ? "master" : "slave");
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("door open"); break;
1829 printf("no/blank disc"); break;
1831 printf("medium format error"); break;
1833 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
1834 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
1836 printf("120mm data disc"); break;
1838 printf("120mm audio disc"); break;
1840 printf("120mm data/audio disc"); break;
1842 printf("120mm photo disc"); break;
1844 printf("80mm data disc"); break;
1846 printf("80mm audio disc"); break;
1848 printf("80mm data/audio disc"); break;
1850 printf("80mm photo disc"); break;
1852 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1854 printf("unknown"); break;
1857 printf("blank"); break;
1861 printf("unknown (0x%x)", cdp->cap.medium_type); break;
1868 device_printf(dev, "%s ",
1869 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1870 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1871 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1872 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1873 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM" :
1875 if (cdp->changer_info)
1876 printf("with %d CD changer ", cdp->changer_info->slots);
1877 printf("<%.40s/%.8s> at ata%d-%s %s\n",
1878 atadev->param.model, atadev->param.revision,
1879 device_get_unit(ch->dev),
1880 (atadev->unit == ATA_MASTER) ? "master" : "slave",
1881 ata_mode2str(atadev->mode) );
1885 static device_method_t acd_methods[] = {
1886 /* device interface */
1887 DEVMETHOD(device_probe, acd_probe),
1888 DEVMETHOD(device_attach, acd_attach),
1889 DEVMETHOD(device_detach, acd_detach),
1890 DEVMETHOD(device_shutdown, acd_shutdown),
1893 DEVMETHOD(ata_reinit, acd_reinit),
1898 static driver_t acd_driver = {
1904 static devclass_t acd_devclass;
1907 acd_modevent(module_t mod, int what, void *arg)
1909 g_modevent(0, what, &acd_class);
1913 DRIVER_MODULE(acd, ata, acd_driver, acd_devclass, acd_modevent, NULL);
1914 MODULE_VERSION(acd, 1);
1915 MODULE_DEPEND(acd, ata, 1, 1, 1);