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);
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);
156 acd_reinit(device_t dev)
158 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
159 struct ata_device *atadev = device_get_softc(dev);
160 struct acd_softc *cdp = device_get_ivars(dev);
162 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
163 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
164 device_set_ivars(dev, NULL);
168 ATA_SETMODE(device_get_parent(dev), dev);
173 acd_geom_attach(void *arg, int flag)
175 struct ata_device *atadev = device_get_softc(arg);
176 struct acd_softc *cdp = device_get_ivars(arg);
178 struct g_provider *pp;
181 gp = g_new_geomf(&acd_class, "acd%d", device_get_unit(arg));
184 pp = g_new_providerf(gp, "acd%d", device_get_unit(arg));
187 g_error_provider(pp, 0);
188 atadev->flags |= ATA_D_MEDIA_CHANGED;
193 acd_geom_detach(void *arg, int flag)
195 struct acd_softc *cdp = device_get_ivars(arg);
197 /* signal geom so we dont get any further requests */
198 g_wither_geom(cdp->gp, ENXIO);
200 /* fail requests on the queue and any thats "in flight" for this device */
201 ata_fail_requests(arg);
203 /* dont leave anything behind */
204 device_set_ivars(arg, NULL);
209 acd_geom_ioctl(struct g_provider *pp, u_long cmd, void *addr, int fflag, struct thread *td)
211 device_t dev = pp->geom->softc;
212 struct ata_device *atadev = device_get_softc(dev);
213 struct acd_softc *cdp = device_get_ivars(dev);
214 int error = 0, nocopyout = 0;
219 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
227 acd_prevent_allow(dev, 1);
228 cdp->flags |= F_LOCKED;
236 error = acd_pause_resume(dev, 1);
240 error = acd_pause_resume(dev, 0);
244 error = acd_start_stop(dev, 1);
248 error = acd_start_stop(dev, 0);
252 error = acd_prevent_allow(dev, 0);
253 cdp->flags &= ~F_LOCKED;
257 error = acd_prevent_allow(dev, 1);
258 cdp->flags |= F_LOCKED;
265 error = acd_test_ready(dev);
273 error = acd_tray(dev, 0);
279 error = acd_tray(dev, 1);
282 case CDIOREADTOCHEADER:
283 if (!cdp->toc.hdr.ending_track) {
287 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
290 case CDIOREADTOCENTRYS:
292 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
293 struct toc *toc = &cdp->toc;
294 int starting_track = te->starting_track;
297 if (!toc->hdr.ending_track) {
302 if (te->data_len < sizeof(toc->tab[0]) ||
303 (te->data_len % sizeof(toc->tab[0])) != 0 ||
304 (te->address_format != CD_MSF_FORMAT &&
305 te->address_format != CD_LBA_FORMAT)) {
311 starting_track = toc->hdr.starting_track;
312 else if (starting_track == 170)
313 starting_track = toc->hdr.ending_track + 1;
314 else if (starting_track < toc->hdr.starting_track ||
315 starting_track > toc->hdr.ending_track + 1) {
320 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
322 if (te->data_len < len)
324 if (len > sizeof(toc->tab)) {
329 if (te->address_format == CD_MSF_FORMAT) {
330 struct cd_toc_entry *entry;
332 if (!(toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT))) {
336 bcopy(&cdp->toc, toc, sizeof(struct toc));
337 entry = toc->tab + (toc->hdr.ending_track + 1 -
338 toc->hdr.starting_track) + 1;
339 while (--entry >= toc->tab)
340 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
341 &entry->addr.msf.second, &entry->addr.msf.frame);
343 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
345 if (te->address_format == CD_MSF_FORMAT)
350 case CDIOREADTOCENTRY:
352 struct ioc_read_toc_single_entry *te =
353 (struct ioc_read_toc_single_entry *)addr;
354 struct toc *toc = &cdp->toc;
355 u_char track = te->track;
357 if (!toc->hdr.ending_track) {
362 if (te->address_format != CD_MSF_FORMAT &&
363 te->address_format != CD_LBA_FORMAT) {
369 track = toc->hdr.starting_track;
370 else if (track == 170)
371 track = toc->hdr.ending_track + 1;
372 else if (track < toc->hdr.starting_track ||
373 track > toc->hdr.ending_track + 1) {
378 if (te->address_format == CD_MSF_FORMAT) {
379 struct cd_toc_entry *entry;
381 if (!(toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT))) {
385 bcopy(&cdp->toc, toc, sizeof(struct toc));
386 entry = toc->tab + (track - toc->hdr.starting_track);
387 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
388 &entry->addr.msf.second, &entry->addr.msf.frame);
390 bcopy(toc->tab + track - toc->hdr.starting_track,
391 &te->entry, sizeof(struct cd_toc_entry));
392 if (te->address_format == CD_MSF_FORMAT)
397 #if __FreeBSD_version > 600008
398 case CDIOCREADSUBCHANNEL_SYSSPACE:
403 case CDIOCREADSUBCHANNEL:
405 struct ioc_read_subchannel *args =
406 (struct ioc_read_subchannel *)addr;
408 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
409 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
410 0, 0, 0, 0, 0, 0, 0 };
412 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
413 args->data_len < sizeof(struct cd_sub_channel_header)) {
418 format = args->data_format;
419 if ((format != CD_CURRENT_POSITION) &&
420 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
425 ccb[1] = args->address_format & CD_MSF_FORMAT;
427 if ((error = ata_atapicmd(dev, ccb, (caddr_t)&cdp->subchan,
428 sizeof(cdp->subchan), ATA_R_READ, 10)))
431 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
432 if (cdp->subchan.header.audio_status == 0x11) {
438 if (format == CD_TRACK_INFO)
439 ccb[6] = args->track;
441 if ((error = ata_atapicmd(dev, ccb, (caddr_t)&cdp->subchan,
442 sizeof(cdp->subchan),ATA_R_READ,10))){
446 if (nocopyout == 0) {
447 error = copyout(&cdp->subchan, args->data, args->data_len);
450 bcopy(&cdp->subchan, args->data, args->data_len);
457 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
461 msf2lba(args->start_m, args->start_s, args->start_f),
462 msf2lba(args->end_m, args->end_s, args->end_f));
466 case CDIOCPLAYBLOCKS:
468 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
470 error = acd_play(dev, args->blk, args->blk + args->len);
474 case CDIOCPLAYTRACKS:
476 struct ioc_play_track *args = (struct ioc_play_track *)addr;
479 if (!cdp->toc.hdr.ending_track) {
483 if (args->end_track < cdp->toc.hdr.ending_track + 1)
485 if (args->end_track > cdp->toc.hdr.ending_track + 1)
486 args->end_track = cdp->toc.hdr.ending_track + 1;
487 t1 = args->start_track - cdp->toc.hdr.starting_track;
488 t2 = args->end_track - cdp->toc.hdr.starting_track;
489 if (t1 < 0 || t2 < 0 ||
490 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
494 error = acd_play(dev, ntohl(cdp->toc.tab[t1].addr.lba),
495 ntohl(cdp->toc.tab[t2].addr.lba));
501 struct ioc_vol *arg = (struct ioc_vol *)addr;
503 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE,
504 (caddr_t)&cdp->au, sizeof(cdp->au))))
507 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
511 arg->vol[0] = cdp->au.port[0].volume;
512 arg->vol[1] = cdp->au.port[1].volume;
513 arg->vol[2] = cdp->au.port[2].volume;
514 arg->vol[3] = cdp->au.port[3].volume;
520 struct ioc_vol *arg = (struct ioc_vol *)addr;
522 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE,
523 (caddr_t)&cdp->au, sizeof(cdp->au))))
525 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
529 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE_MASK,
530 (caddr_t)&cdp->aumask,
531 sizeof(cdp->aumask))))
533 cdp->au.data_length = 0;
534 cdp->au.port[0].channels = CHANNEL_0;
535 cdp->au.port[1].channels = CHANNEL_1;
536 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
537 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
538 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
539 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
540 error = acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
546 struct ioc_patch *arg = (struct ioc_patch *)addr;
548 error = acd_setchan(dev, arg->patch[0], arg->patch[1],
549 arg->patch[2], arg->patch[3]);
554 error = acd_setchan(dev, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
558 error = acd_setchan(dev, CHANNEL_0, CHANNEL_1, 0, 0);
562 error = acd_setchan(dev, 0, 0, 0, 0);
566 error = acd_setchan(dev, CHANNEL_0, CHANNEL_0, 0, 0);
570 error = acd_setchan(dev, CHANNEL_1, CHANNEL_1, 0, 0);
574 error = acd_blank(dev, (*(int *)addr));
577 case CDRIOCNEXTWRITEABLEADDR:
579 struct acd_track_info track_info;
581 if ((error = acd_read_track_info(dev, 0xff, &track_info)))
584 if (!track_info.nwa_valid) {
588 *(int*)addr = track_info.next_writeable_addr;
592 case CDRIOCINITWRITER:
593 error = acd_init_writer(dev, (*(int *)addr));
596 case CDRIOCINITTRACK:
597 error = acd_init_track(dev, (struct cdr_track *)addr);
601 error = acd_flush(dev);
605 error = acd_fixate(dev, (*(int *)addr));
608 case CDRIOCREADSPEED:
610 int speed = *(int *)addr;
612 /* Preserve old behavior: units in multiples of CDROM speed */
615 error = acd_set_speed(dev, speed, CDR_MAX_SPEED);
619 case CDRIOCWRITESPEED:
621 int speed = *(int *)addr;
625 error = acd_set_speed(dev, CDR_MAX_SPEED, speed);
629 case CDRIOCGETBLOCKSIZE:
630 *(int *)addr = cdp->block_size;
633 case CDRIOCSETBLOCKSIZE:
634 cdp->block_size = *(int *)addr;
635 pp->sectorsize = cdp->block_size; /* hack for GEOM SOS */
639 case CDRIOCGETPROGRESS:
640 error = acd_get_progress(dev, (int *)addr);
644 error = acd_send_cue(dev, (struct cdr_cuesheet *)addr);
647 case CDRIOCREADFORMATCAPS:
648 error = acd_read_format_caps(dev, (struct cdr_format_capacities *)addr);
652 error = acd_format(dev, (struct cdr_format_params *)addr);
655 case DVDIOCREPORTKEY:
656 if (cdp->cap.media & MST_READ_DVDROM)
657 error = acd_report_key(dev, (struct dvd_authinfo *)addr);
663 if (cdp->cap.media & MST_READ_DVDROM)
664 error = acd_send_key(dev, (struct dvd_authinfo *)addr);
669 case DVDIOCREADSTRUCTURE:
670 if (cdp->cap.media & MST_READ_DVDROM)
671 error = acd_read_structure(dev, (struct dvd_struct *)addr);
683 acd_geom_access(struct g_provider *pp, int dr, int dw, int de)
685 device_t dev = pp->geom->softc;
686 struct acd_softc *cdp = device_get_ivars(dev);
687 struct ata_request *request;
688 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
689 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
690 int timeout = 60, track;
692 if (!(request = ata_alloc_request()))
695 /* wait if drive is not finished loading the medium */
697 bzero(request, sizeof(struct ata_request));
699 bcopy(ccb, request->u.atapi.ccb, 16);
700 request->flags = ATA_R_ATAPI;
701 request->timeout = 5;
702 ata_queue_request(request);
703 if (!request->error &&
704 (request->u.atapi.sense_data.sense_key == 2 ||
705 request->u.atapi.sense_data.sense_key == 7) &&
706 request->u.atapi.sense_data.asc == 4 &&
707 request->u.atapi.sense_data.ascq == 1)
708 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
712 ata_free_request(request);
715 acd_prevent_allow(dev, 1);
716 cdp->flags |= F_LOCKED;
720 if (dr + pp->acr == 0) {
721 acd_prevent_allow(dev, 0);
722 cdp->flags &= ~F_LOCKED;
725 if ((track = pp->index)) {
726 pp->sectorsize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
727 pp->mediasize = ntohl(cdp->toc.tab[track].addr.lba) -
728 ntohl(cdp->toc.tab[track - 1].addr.lba);
731 pp->sectorsize = cdp->block_size;
732 pp->mediasize = cdp->disk_size;
734 pp->mediasize *= pp->sectorsize;
740 acd_geom_start(struct bio *bp)
742 device_t dev = bp->bio_to->geom->softc;
743 struct acd_softc *cdp = device_get_ivars(dev);
745 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE) {
746 g_io_deliver(bp, EOPNOTSUPP);
750 if (bp->bio_cmd == BIO_READ && cdp->disk_size == -1) {
751 g_io_deliver(bp, EIO);
755 /* GEOM classes must do their own request limiting */
756 if (bp->bio_length <= cdp->iomax) {
757 bp->bio_pblkno = bp->bio_offset / bp->bio_to->sectorsize;
761 u_int pos, size = cdp->iomax - cdp->iomax % bp->bio_to->sectorsize;
764 for (pos = 0; pos < bp->bio_length; pos += size) {
765 if (!(bp2 = g_clone_bio(bp))) {
766 bp->bio_error = ENOMEM;
769 bp2->bio_done = g_std_done;
770 bp2->bio_to = bp->bio_to;
771 bp2->bio_offset += pos;
772 bp2->bio_data += pos;
773 bp2->bio_length = MIN(size, bp->bio_length - pos);
774 bp2->bio_pblkno = bp2->bio_offset / bp2->bio_to->sectorsize;
781 acd_strategy(struct bio *bp)
783 device_t dev = bp->bio_to->geom->softc;
784 struct ata_device *atadev = device_get_softc(dev);
785 struct acd_softc *cdp = device_get_ivars(dev);
786 struct ata_request *request;
787 u_int32_t lba, lastlba, count;
789 int track, blocksize;
791 /* reject all queued entries if media changed */
792 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
793 g_io_deliver(bp, EIO);
797 bzero(ccb, sizeof(ccb));
799 track = bp->bio_to->index;
802 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
803 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
804 lba = bp->bio_offset / blocksize;
805 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
808 blocksize = cdp->block_size;
809 lastlba = cdp->disk_size;
810 lba = bp->bio_offset / blocksize;
813 count = bp->bio_length / blocksize;
815 if (bp->bio_cmd == BIO_READ) {
816 /* if transfer goes beyond range adjust it to be within limits */
817 if (lba + count > lastlba) {
818 /* if we are entirely beyond EOM return EOF */
819 if (lastlba <= lba) {
823 count = lastlba - lba;
827 ccb[0] = ATAPI_READ_BIG;
831 ccb[0] = ATAPI_READ_CD;
836 ccb[0] = ATAPI_READ_CD;
841 ccb[0] = ATAPI_WRITE_BIG;
852 if (!(request = ata_alloc_request())) {
853 g_io_deliver(bp, ENOMEM);
857 request->driver = bp;
858 bcopy(ccb, request->u.atapi.ccb,
859 (atadev->param.config & ATA_PROTO_MASK) ==
860 ATA_PROTO_ATAPI_12 ? 16 : 12);
861 request->data = bp->bio_data;
862 request->bytecount = count * blocksize;
863 request->transfersize = min(request->bytecount, 65534);
864 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
865 request->retries = 2;
866 request->callback = acd_done;
867 request->flags = ATA_R_ATAPI;
868 if (atadev->mode >= ATA_DMA)
869 request->flags |= ATA_R_DMA;
870 switch (bp->bio_cmd) {
872 request->flags |= ATA_R_READ;
875 request->flags |= ATA_R_WRITE;
878 device_printf(dev, "unknown BIO operation\n");
879 ata_free_request(request);
880 g_io_deliver(bp, EIO);
883 ata_queue_request(request);
887 acd_done(struct ata_request *request)
889 struct bio *bp = request->driver;
891 /* finish up transfer */
892 bp->bio_completed = request->donecount;
893 g_io_deliver(bp, request->result);
894 ata_free_request(request);
898 acd_set_ioparm(device_t dev)
900 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
901 struct acd_softc *cdp = device_get_ivars(dev);
904 cdp->iomax = min(ch->dma->max_iosize, 65534);
906 cdp->iomax = min(DFLTPHYS, 65534);
910 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
914 *m = lba / (60 * 75);
921 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
923 return (m * 60 + s) * 75 + f - 150;
927 acd_read_toc(device_t dev)
929 struct ata_device *atadev = device_get_softc(dev);
930 struct acd_softc *cdp = device_get_ivars(dev);
931 struct g_provider *pp;
934 int track, ntracks, len;
936 if (acd_test_ready(dev))
939 if (!(atadev->flags & ATA_D_MEDIA_CHANGED))
942 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
943 bzero(&cdp->toc, sizeof(cdp->toc));
944 bzero(ccb, sizeof(ccb));
945 cdp->disk_size = -1; /* hack for GEOM SOS */
947 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
948 ccb[0] = ATAPI_READ_TOC;
951 if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
952 ATA_R_READ | ATA_R_QUIET, 30)) {
953 bzero(&cdp->toc, sizeof(cdp->toc));
956 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
957 if (ntracks <= 0 || ntracks > MAXTRK) {
958 bzero(&cdp->toc, sizeof(cdp->toc));
962 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
963 bzero(ccb, sizeof(ccb));
964 ccb[0] = ATAPI_READ_TOC;
967 if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
968 ATA_R_READ | ATA_R_QUIET, 30)) {
969 bzero(&cdp->toc, sizeof(cdp->toc));
972 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
974 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
976 bzero(ccb, sizeof(ccb));
977 ccb[0] = ATAPI_READ_CAPACITY;
978 if (ata_atapicmd(dev, ccb, (caddr_t)sizes, sizeof(sizes),
979 ATA_R_READ | ATA_R_QUIET, 30)) {
980 bzero(&cdp->toc, sizeof(cdp->toc));
983 cdp->disk_size = ntohl(sizes[0]) + 1;
985 for (track = 1; track <= ntracks; track ++) {
986 if (cdp->pp[track] != NULL)
988 pp = g_new_providerf(cdp->gp, "acd%dt%02d", device_get_unit(dev),track);
991 g_error_provider(pp, 0);
993 for (; track < MAXTRK; track ++) {
994 if (cdp->pp[track] == NULL)
996 cdp->pp[track]->flags |= G_PF_WITHER;
997 g_orphan_provider(cdp->pp[track], ENXIO);
998 cdp->pp[track] = NULL;
1002 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1003 device_printd(dev, "(%d sectors (%d bytes)), %d tracks ",
1004 cdp->disk_size, cdp->block_size,
1005 cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track+1);
1006 if (cdp->toc.tab[0].control & 4)
1007 printf("%dMB\n", cdp->disk_size / 512);
1009 printf("%d:%d audio\n",
1010 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1016 acd_play(device_t dev, int start, int end)
1020 bzero(ccb, sizeof(ccb));
1021 ccb[0] = ATAPI_PLAY_MSF;
1022 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1023 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1024 return ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
1028 acd_setchan(device_t dev, u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1030 struct acd_softc *cdp = device_get_ivars(dev);
1033 if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1036 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1038 cdp->au.data_length = 0;
1039 cdp->au.port[0].channels = c0;
1040 cdp->au.port[1].channels = c1;
1041 cdp->au.port[2].channels = c2;
1042 cdp->au.port[3].channels = c3;
1043 return acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
1047 acd_init_writer(device_t dev, int test_write)
1051 bzero(ccb, sizeof(ccb));
1052 ccb[0] = ATAPI_REZERO;
1053 ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1054 ccb[0] = ATAPI_SEND_OPC_INFO;
1056 ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 30);
1061 acd_fixate(device_t dev, int multisession)
1063 struct acd_softc *cdp = device_get_ivars(dev);
1064 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1065 0, 0, 0, 0, 0, 0, 0, 0 };
1066 int timeout = 5*60*2;
1068 struct write_param param;
1070 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1071 (caddr_t)¶m, sizeof(param))))
1074 param.data_length = 0;
1076 param.session_type = CDR_SESS_MULTI;
1078 param.session_type = CDR_SESS_NONE;
1080 if ((error = acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10)))
1083 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1087 /* some drives just return ready, wait for the expected fixate time */
1088 if ((error = acd_test_ready(dev)) != EBUSY) {
1089 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1090 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1091 return acd_test_ready(dev);
1094 while (timeout-- > 0) {
1095 if ((error = acd_get_progress(dev, &dummy)))
1097 if ((error = acd_test_ready(dev)) != EBUSY)
1099 tsleep(&error, PRIBIO, "acdcld", hz / 2);
1105 acd_init_track(device_t dev, struct cdr_track *track)
1107 struct acd_softc *cdp = device_get_ivars(dev);
1108 struct write_param param;
1111 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1112 (caddr_t)¶m, sizeof(param))))
1115 param.data_length = 0;
1116 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1117 param.page_length = 0x32;
1118 param.test_write = track->test_write ? 1 : 0;
1119 param.write_type = CDR_WTYPE_TRACK;
1120 param.session_type = CDR_SESS_NONE;
1122 param.packet_size = 0;
1124 if (cdp->cap.capabilities & MST_BURNPROOF)
1125 param.burnproof = 1;
1127 switch (track->datablock_type) {
1131 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1133 param.track_mode = CDR_TMODE_AUDIO;
1134 cdp->block_size = 2352;
1135 param.datablock_type = CDR_DB_RAW;
1136 param.session_format = CDR_SESS_CDROM;
1139 case CDR_DB_ROM_MODE1:
1140 cdp->block_size = 2048;
1141 param.track_mode = CDR_TMODE_DATA;
1142 param.datablock_type = CDR_DB_ROM_MODE1;
1143 param.session_format = CDR_SESS_CDROM;
1146 case CDR_DB_ROM_MODE2:
1147 cdp->block_size = 2336;
1148 param.track_mode = CDR_TMODE_DATA;
1149 param.datablock_type = CDR_DB_ROM_MODE2;
1150 param.session_format = CDR_SESS_CDROM;
1153 case CDR_DB_XA_MODE1:
1154 cdp->block_size = 2048;
1155 param.track_mode = CDR_TMODE_DATA;
1156 param.datablock_type = CDR_DB_XA_MODE1;
1157 param.session_format = CDR_SESS_CDROM_XA;
1160 case CDR_DB_XA_MODE2_F1:
1161 cdp->block_size = 2056;
1162 param.track_mode = CDR_TMODE_DATA;
1163 param.datablock_type = CDR_DB_XA_MODE2_F1;
1164 param.session_format = CDR_SESS_CDROM_XA;
1167 case CDR_DB_XA_MODE2_F2:
1168 cdp->block_size = 2324;
1169 param.track_mode = CDR_TMODE_DATA;
1170 param.datablock_type = CDR_DB_XA_MODE2_F2;
1171 param.session_format = CDR_SESS_CDROM_XA;
1174 case CDR_DB_XA_MODE2_MIX:
1175 cdp->block_size = 2332;
1176 param.track_mode = CDR_TMODE_DATA;
1177 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1178 param.session_format = CDR_SESS_CDROM_XA;
1181 acd_set_ioparm(dev);
1182 return acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10);
1186 acd_flush(device_t dev)
1188 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1189 0, 0, 0, 0, 0, 0, 0, 0 };
1191 return ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1195 acd_read_track_info(device_t dev, int32_t lba, struct acd_track_info *info)
1197 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1198 lba>>24, lba>>16, lba>>8, lba, 0,
1199 sizeof(*info)>>8, sizeof(*info),
1200 0, 0, 0, 0, 0, 0, 0 };
1203 if ((error = ata_atapicmd(dev, ccb, (caddr_t)info, sizeof(*info),
1206 info->track_start_addr = ntohl(info->track_start_addr);
1207 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1208 info->free_blocks = ntohl(info->free_blocks);
1209 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1210 info->track_length = ntohl(info->track_length);
1215 acd_get_progress(device_t dev, int *finished)
1217 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1218 0, 0, 0, 0, 0, 0, 0, 0 };
1219 struct ata_request *request;
1222 if (!(request = ata_alloc_request()))
1226 bcopy(ccb, request->u.atapi.ccb, 16);
1227 request->data = dummy;
1228 request->bytecount = sizeof(dummy);
1229 request->transfersize = min(request->bytecount, 65534);
1230 request->flags = ATA_R_ATAPI | ATA_R_READ;
1231 request->timeout = 30;
1232 ata_queue_request(request);
1233 if (!request->error && request->u.atapi.sense_data.sksv)
1234 *finished = ((request->u.atapi.sense_data.sk_specific2 |
1235 (request->u.atapi.sense_data.sk_specific1<<8))*100)/65535;
1238 ata_free_request(request);
1243 acd_send_cue(device_t dev, struct cdr_cuesheet *cuesheet)
1245 struct acd_softc *cdp = device_get_ivars(dev);
1246 struct write_param param;
1247 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1248 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1249 0, 0, 0, 0, 0, 0, 0 };
1256 if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1257 (caddr_t)¶m, sizeof(param))))
1260 param.data_length = 0;
1261 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1262 param.page_length = 0x32;
1263 param.test_write = cuesheet->test_write ? 1 : 0;
1264 param.write_type = CDR_WTYPE_SESSION;
1265 param.session_type = cuesheet->session_type;
1267 param.packet_size = 0;
1268 param.track_mode = CDR_TMODE_AUDIO;
1269 param.datablock_type = CDR_DB_RAW;
1270 param.session_format = cuesheet->session_format;
1271 if (cdp->cap.capabilities & MST_BURNPROOF)
1272 param.burnproof = 1;
1274 if ((error = acd_mode_select(dev, (caddr_t)¶m, param.page_length + 10)))
1277 if (!(buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT)))
1280 if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len))) {
1282 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1283 for (i=0; i<cuesheet->len; i++)
1285 printf(" %02x", buffer[i]);
1287 printf("\n%02x", buffer[i]);
1290 error = ata_atapicmd(dev, ccb, buffer, cuesheet->len, 0, 30);
1292 free(buffer, M_ACD);
1297 acd_report_key(device_t dev, struct dvd_authinfo *ai)
1299 struct dvd_miscauth *d = NULL;
1305 switch (ai->format) {
1306 case DVD_REPORT_AGID:
1307 case DVD_REPORT_ASF:
1308 case DVD_REPORT_RPC:
1311 case DVD_REPORT_KEY1:
1314 case DVD_REPORT_TITLE_KEY:
1318 case DVD_REPORT_CHALLENGE:
1321 case DVD_INVALIDATE_AGID:
1328 bzero(ccb, sizeof(ccb));
1329 ccb[0] = ATAPI_REPORT_KEY;
1330 ccb[2] = (lba >> 24) & 0xff;
1331 ccb[3] = (lba >> 16) & 0xff;
1332 ccb[4] = (lba >> 8) & 0xff;
1333 ccb[5] = lba & 0xff;
1334 ccb[8] = (length >> 8) & 0xff;
1335 ccb[9] = length & 0xff;
1336 ccb[10] = (ai->agid << 6) | ai->format;
1339 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1341 d->length = htons(length - 2);
1344 error = ata_atapicmd(dev, ccb, (caddr_t)d, length,
1345 ai->format == DVD_INVALIDATE_AGID ? 0 : ATA_R_READ,10);
1351 switch (ai->format) {
1352 case DVD_REPORT_AGID:
1353 ai->agid = d->data[3] >> 6;
1356 case DVD_REPORT_CHALLENGE:
1357 bcopy(&d->data[0], &ai->keychal[0], 10);
1360 case DVD_REPORT_KEY1:
1361 bcopy(&d->data[0], &ai->keychal[0], 5);
1364 case DVD_REPORT_TITLE_KEY:
1365 ai->cpm = (d->data[0] >> 7);
1366 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1367 ai->cgms = (d->data[0] >> 4) & 0x3;
1368 bcopy(&d->data[1], &ai->keychal[0], 5);
1371 case DVD_REPORT_ASF:
1372 ai->asf = d->data[3] & 1;
1375 case DVD_REPORT_RPC:
1376 ai->reg_type = (d->data[0] >> 6);
1377 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1378 ai->user_rsts = d->data[0] & 0x7;
1379 ai->region = d->data[1];
1380 ai->rpc_scheme = d->data[2];
1383 case DVD_INVALIDATE_AGID:
1394 acd_send_key(device_t dev, struct dvd_authinfo *ai)
1396 struct dvd_miscauth *d;
1401 switch (ai->format) {
1402 case DVD_SEND_CHALLENGE:
1404 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1406 bcopy(ai->keychal, &d->data[0], 10);
1411 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1413 bcopy(&ai->keychal[0], &d->data[0], 5);
1418 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1420 d->data[0] = ai->region;
1427 bzero(ccb, sizeof(ccb));
1428 ccb[0] = ATAPI_SEND_KEY;
1429 ccb[8] = (length >> 8) & 0xff;
1430 ccb[9] = length & 0xff;
1431 ccb[10] = (ai->agid << 6) | ai->format;
1432 d->length = htons(length - 2);
1433 error = ata_atapicmd(dev, ccb, (caddr_t)d, length, 0, 10);
1439 acd_read_structure(device_t dev, struct dvd_struct *s)
1441 struct dvd_miscauth *d;
1447 case DVD_STRUCT_PHYSICAL:
1451 case DVD_STRUCT_COPYRIGHT:
1455 case DVD_STRUCT_DISCKEY:
1459 case DVD_STRUCT_BCA:
1463 case DVD_STRUCT_MANUFACT:
1467 case DVD_STRUCT_DDS:
1468 case DVD_STRUCT_PRERECORDED:
1469 case DVD_STRUCT_UNIQUEID:
1470 case DVD_STRUCT_LIST:
1471 case DVD_STRUCT_CMI:
1472 case DVD_STRUCT_RMD_LAST:
1473 case DVD_STRUCT_RMD_RMA:
1474 case DVD_STRUCT_DCB:
1481 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1483 d->length = htons(length - 2);
1485 bzero(ccb, sizeof(ccb));
1486 ccb[0] = ATAPI_READ_STRUCTURE;
1487 ccb[6] = s->layer_num;
1489 ccb[8] = (length >> 8) & 0xff;
1490 ccb[9] = length & 0xff;
1491 ccb[10] = s->agid << 6;
1492 error = ata_atapicmd(dev, ccb, (caddr_t)d, length, ATA_R_READ, 30);
1498 switch (s->format) {
1499 case DVD_STRUCT_PHYSICAL: {
1500 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1502 layer->book_type = d->data[0] >> 4;
1503 layer->book_version = d->data[0] & 0xf;
1504 layer->disc_size = d->data[1] >> 4;
1505 layer->max_rate = d->data[1] & 0xf;
1506 layer->nlayers = (d->data[2] >> 5) & 3;
1507 layer->track_path = (d->data[2] >> 4) & 1;
1508 layer->layer_type = d->data[2] & 0xf;
1509 layer->linear_density = d->data[3] >> 4;
1510 layer->track_density = d->data[3] & 0xf;
1511 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1512 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1513 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1514 layer->bca = d->data[16] >> 7;
1518 case DVD_STRUCT_COPYRIGHT:
1519 s->cpst = d->data[0];
1520 s->rmi = d->data[0];
1523 case DVD_STRUCT_DISCKEY:
1524 bcopy(&d->data[0], &s->data[0], 2048);
1527 case DVD_STRUCT_BCA:
1528 s->length = ntohs(d->length);
1529 bcopy(&d->data[0], &s->data[0], s->length);
1532 case DVD_STRUCT_MANUFACT:
1533 s->length = ntohs(d->length);
1534 bcopy(&d->data[0], &s->data[0], s->length);
1545 acd_tray(device_t dev, int close)
1547 struct ata_device *atadev = device_get_softc(dev);
1548 struct acd_softc *cdp = device_get_ivars(dev);
1551 if (cdp->cap.mechanism & MST_EJECT) {
1553 if (!(error = acd_start_stop(dev, 3))) {
1555 acd_prevent_allow(dev, 1);
1556 cdp->flags |= F_LOCKED;
1560 acd_start_stop(dev, 0);
1561 acd_prevent_allow(dev, 0);
1562 cdp->flags &= ~F_LOCKED;
1563 atadev->flags |= ATA_D_MEDIA_CHANGED;
1564 error = acd_start_stop(dev, 2);
1571 acd_blank(device_t dev, int blanktype)
1573 struct ata_device *atadev = device_get_softc(dev);
1574 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1575 0, 0, 0, 0, 0, 0, 0, 0 };
1577 atadev->flags |= ATA_D_MEDIA_CHANGED;
1578 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1582 acd_prevent_allow(device_t dev, int lock)
1584 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1587 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1591 acd_start_stop(device_t dev, int start)
1593 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1596 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1600 acd_pause_resume(device_t dev, int pause)
1602 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1603 0, 0, 0, 0, 0, 0, 0 };
1605 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1609 acd_mode_sense(device_t dev, int page, caddr_t pagebuf, int pagesize)
1611 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1612 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1615 error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
1617 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1623 acd_mode_select(device_t dev, caddr_t pagebuf, int pagesize)
1625 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1626 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1629 device_printf(dev, "modeselect pagesize=%d\n", pagesize);
1630 atapi_dump("mode select ", pagebuf, pagesize);
1632 return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 30);
1636 acd_set_speed(device_t dev, int rdspeed, int wrspeed)
1638 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1639 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1642 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1649 acd_get_cap(device_t dev)
1651 struct acd_softc *cdp = device_get_ivars(dev);
1654 /* get drive capabilities, some bugridden drives needs this repeated */
1655 for (count = 0 ; count < 5 ; count++) {
1656 if (!acd_mode_sense(dev, ATAPI_CDROM_CAP_PAGE,
1657 (caddr_t)&cdp->cap, sizeof(cdp->cap)) &&
1658 cdp->cap.page_code == ATAPI_CDROM_CAP_PAGE) {
1659 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1660 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1661 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1662 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed),177);
1663 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1664 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
1670 acd_read_format_caps(device_t dev, struct cdr_format_capacities *caps)
1672 int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
1673 (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
1674 sizeof(struct cdr_format_capacities) & 0xff,
1675 0, 0, 0, 0, 0, 0, 0 };
1677 return ata_atapicmd(dev, ccb, (caddr_t)caps,
1678 sizeof(struct cdr_format_capacities), ATA_R_READ, 30);
1682 acd_format(device_t dev, struct cdr_format_params* params)
1684 int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
1688 error = ata_atapicmd(dev, ccb, (u_int8_t *)params,
1689 sizeof(struct cdr_format_params), 0, 30);
1694 acd_test_ready(device_t dev)
1696 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
1697 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1699 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1703 acd_describe(device_t dev)
1705 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1706 struct ata_device *atadev = device_get_softc(dev);
1707 struct acd_softc *cdp = device_get_ivars(dev);
1712 device_printf(dev, "<%.40s/%.8s> %s drive at ata%d as %s\n",
1713 atadev->param.model, atadev->param.revision,
1714 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1715 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1716 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1717 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1718 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM":"CDROM",
1719 device_get_unit(ch->dev),
1720 (atadev->unit == ATA_MASTER) ? "master" : "slave");
1722 device_printf(dev, "%s", "");
1723 if (cdp->cap.cur_read_speed) {
1724 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
1725 if (cdp->cap.max_read_speed)
1726 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
1727 if ((cdp->cap.cur_write_speed) &&
1728 (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
1729 MST_WRITE_DVDR | MST_WRITE_DVDRAM))) {
1730 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
1731 if (cdp->cap.max_write_speed)
1732 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
1736 if (cdp->cap.buf_size) {
1737 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
1740 printf("%s %s\n", comma ? "," : "", ata_mode2str(atadev->mode));
1742 device_printf(dev, "Reads:");
1744 if (cdp->cap.media & MST_READ_CDR) {
1745 printf(" CDR"); comma = 1;
1747 if (cdp->cap.media & MST_READ_CDRW) {
1748 printf("%s CDRW", comma ? "," : ""); comma = 1;
1750 if (cdp->cap.capabilities & MST_READ_CDDA) {
1751 if (cdp->cap.capabilities & MST_CDDA_STREAM)
1752 printf("%s CDDA stream", comma ? "," : "");
1754 printf("%s CDDA", comma ? "," : "");
1757 if (cdp->cap.media & MST_READ_DVDROM) {
1758 printf("%s DVDROM", comma ? "," : ""); comma = 1;
1760 if (cdp->cap.media & MST_READ_DVDR) {
1761 printf("%s DVDR", comma ? "," : ""); comma = 1;
1763 if (cdp->cap.media & MST_READ_DVDRAM) {
1764 printf("%s DVDRAM", comma ? "," : ""); comma = 1;
1766 if (cdp->cap.media & MST_READ_PACKET)
1767 printf("%s packet", comma ? "," : "");
1770 device_printf(dev, "Writes:");
1771 if (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
1772 MST_WRITE_DVDR | MST_WRITE_DVDRAM)) {
1774 if (cdp->cap.media & MST_WRITE_CDR) {
1775 printf(" CDR" ); comma = 1;
1777 if (cdp->cap.media & MST_WRITE_CDRW) {
1778 printf("%s CDRW", comma ? "," : ""); comma = 1;
1780 if (cdp->cap.media & MST_WRITE_DVDR) {
1781 printf("%s DVDR", comma ? "," : ""); comma = 1;
1783 if (cdp->cap.media & MST_WRITE_DVDRAM) {
1784 printf("%s DVDRAM", comma ? "," : ""); comma = 1;
1786 if (cdp->cap.media & MST_WRITE_TEST) {
1787 printf("%s test write", comma ? "," : ""); comma = 1;
1789 if (cdp->cap.capabilities & MST_BURNPROOF)
1790 printf("%s burnproof", comma ? "," : "");
1793 if (cdp->cap.capabilities & MST_AUDIO_PLAY) {
1794 device_printf(dev, "Audio: ");
1795 if (cdp->cap.capabilities & MST_AUDIO_PLAY)
1797 if (cdp->cap.max_vol_levels)
1798 printf(", %d volume levels", cdp->cap.max_vol_levels);
1801 device_printf(dev, "Mechanism: ");
1802 switch (cdp->cap.mechanism & MST_MECH_MASK) {
1803 case MST_MECH_CADDY:
1804 mechanism = "caddy"; break;
1806 mechanism = "tray"; break;
1807 case MST_MECH_POPUP:
1808 mechanism = "popup"; break;
1809 case MST_MECH_CHANGER:
1810 mechanism = "changer"; break;
1811 case MST_MECH_CARTRIDGE:
1812 mechanism = "cartridge"; break;
1814 mechanism = 0; break;
1817 printf("%s%s", (cdp->cap.mechanism & MST_EJECT) ?
1818 "ejectable " : "", mechanism);
1819 else if (cdp->cap.mechanism & MST_EJECT)
1820 printf("ejectable");
1822 if (cdp->cap.mechanism & MST_LOCKABLE)
1823 printf((cdp->cap.mechanism & MST_LOCKED) ? ", locked":", unlocked");
1824 if (cdp->cap.mechanism & MST_PREVENT)
1825 printf(", lock protected");
1828 if ((cdp->cap.mechanism & MST_MECH_MASK) != MST_MECH_CHANGER) {
1829 device_printf(dev, "Medium: ");
1830 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1832 printf("CD-ROM "); break;
1834 printf("CD-R "); break;
1836 printf("CD-RW "); break;
1838 printf("door open"); break;
1840 printf("no/blank disc"); break;
1842 printf("medium format error"); break;
1844 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
1845 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
1847 printf("120mm data disc"); break;
1849 printf("120mm audio disc"); break;
1851 printf("120mm data/audio disc"); break;
1853 printf("120mm photo disc"); break;
1855 printf("80mm data disc"); break;
1857 printf("80mm audio disc"); break;
1859 printf("80mm data/audio disc"); break;
1861 printf("80mm photo disc"); break;
1863 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1865 printf("unknown"); break;
1868 printf("blank"); break;
1872 printf("unknown (0x%x)", cdp->cap.medium_type); break;
1879 device_printf(dev, "%s ",
1880 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1881 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1882 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1883 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1884 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM" :
1886 if (cdp->changer_info)
1887 printf("with %d CD changer ", cdp->changer_info->slots);
1888 printf("<%.40s/%.8s> at ata%d-%s %s\n",
1889 atadev->param.model, atadev->param.revision,
1890 device_get_unit(ch->dev),
1891 (atadev->unit == ATA_MASTER) ? "master" : "slave",
1892 ata_mode2str(atadev->mode) );
1896 static device_method_t acd_methods[] = {
1897 /* device interface */
1898 DEVMETHOD(device_probe, acd_probe),
1899 DEVMETHOD(device_attach, acd_attach),
1900 DEVMETHOD(device_detach, acd_detach),
1901 DEVMETHOD(device_shutdown, acd_shutdown),
1904 DEVMETHOD(ata_reinit, acd_reinit),
1909 static driver_t acd_driver = {
1915 static devclass_t acd_devclass;
1918 acd_modevent(module_t mod, int what, void *arg)
1920 g_modevent(0, what, &acd_class);
1924 DRIVER_MODULE(acd, ata, acd_driver, acd_devclass, acd_modevent, NULL);
1925 MODULE_VERSION(acd, 1);
1926 MODULE_DEPEND(acd, ata, 1, 1, 1);