2 * Copyright (c) 1998,1999,2000,2001,2002 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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
40 #include <sys/disklabel.h>
41 #include <sys/devicestat.h>
43 #include <sys/cdrio.h>
44 #include <sys/dvdio.h>
45 #include <sys/fcntl.h>
47 #include <sys/ctype.h>
48 #include <machine/bus.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/atapi-all.h>
51 #include <dev/ata/atapi-cd.h>
53 /* device structures */
54 static d_open_t acdopen;
55 static d_close_t acdclose;
56 static d_ioctl_t acdioctl;
57 static d_strategy_t acdstrategy;
58 static struct cdevsw acd_cdevsw = {
62 /* write */ physwrite,
66 /* strategy */ acdstrategy,
71 /* flags */ D_DISK | D_TRACKCLOSE,
75 static struct acd_softc *acd_init_lun(struct ata_device *);
76 static void acd_make_dev(struct acd_softc *);
77 static void acd_set_ioparm(struct acd_softc *);
78 static void acd_describe(struct acd_softc *);
79 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
80 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
81 static int acd_done(struct atapi_request *);
82 static void acd_read_toc(struct acd_softc *);
83 static int acd_play(struct acd_softc *, int, int);
84 static int acd_setchan(struct acd_softc *, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
85 static void acd_select_slot(struct acd_softc *);
86 static int acd_init_writer(struct acd_softc *, int);
87 static int acd_fixate(struct acd_softc *, int);
88 static int acd_init_track(struct acd_softc *, struct cdr_track *);
89 static int acd_flush(struct acd_softc *);
90 static int acd_read_track_info(struct acd_softc *, int32_t, struct acd_track_info *);
91 static int acd_get_progress(struct acd_softc *, int *);
92 static int acd_send_cue(struct acd_softc *, struct cdr_cuesheet *);
93 static int acd_report_key(struct acd_softc *, struct dvd_authinfo *);
94 static int acd_send_key(struct acd_softc *, struct dvd_authinfo *);
95 static int acd_read_structure(struct acd_softc *, struct dvd_struct *);
96 static int acd_eject(struct acd_softc *, int);
97 static int acd_blank(struct acd_softc *, int);
98 static int acd_prevent_allow(struct acd_softc *, int);
99 static int acd_start_stop(struct acd_softc *, int);
100 static int acd_pause_resume(struct acd_softc *, int);
101 static int acd_mode_sense(struct acd_softc *, int, caddr_t, int);
102 static int acd_mode_select(struct acd_softc *, caddr_t, int);
103 static int acd_set_speed(struct acd_softc *, int, int);
104 static void acd_get_cap(struct acd_softc *);
105 static int acd_read_format_caps(struct acd_softc *, struct cdr_format_capacities *);
106 static int acd_format(struct acd_softc *, struct cdr_format_params *);
109 static u_int32_t acd_lun_map = 0;
110 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
113 acdattach(struct ata_device *atadev)
115 struct acd_softc *cdp;
118 if ((cdp = acd_init_lun(atadev)) == NULL) {
119 ata_prtdev(atadev, "acd: out of memory\n");
123 ata_set_name(atadev, "acd", cdp->lun);
126 /* if this is a changer device, allocate the neeeded lun's */
127 if (cdp->cap.mech == MST_MECH_CHANGER) {
128 int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0,
129 sizeof(struct changer)>>8, sizeof(struct changer),
132 chp = malloc(sizeof(struct changer), M_ACD, M_NOWAIT | M_ZERO);
134 ata_prtdev(atadev, "out of memory\n");
138 if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp,
139 sizeof(struct changer),
140 ATPR_F_READ, 60, NULL, NULL)) {
141 struct acd_softc *tmpcdp = cdp;
142 struct acd_softc **cdparr;
146 chp->table_length = htons(chp->table_length);
147 if (!(cdparr = malloc(sizeof(struct acd_softc) * chp->slots,
149 ata_prtdev(atadev, "out of memory\n");
154 for (count = 0; count < chp->slots; count++) {
156 tmpcdp = acd_init_lun(atadev);
158 ata_prtdev(atadev, "out of memory\n");
162 cdparr[count] = tmpcdp;
163 tmpcdp->driver = cdparr;
164 tmpcdp->slot = count;
165 tmpcdp->changer_info = chp;
166 acd_make_dev(tmpcdp);
167 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
168 DEVSTAT_NO_ORDERED_TAGS,
169 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
170 DEVSTAT_PRIORITY_CD);
172 name = malloc(strlen(atadev->name) + 2, M_ACD, M_NOWAIT);
173 strcpy(name, atadev->name);
175 ata_free_name(atadev);
176 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
182 devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
183 DEVSTAT_NO_ORDERED_TAGS,
184 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
185 DEVSTAT_PRIORITY_CD);
188 atadev->driver = cdp;
193 acddetach(struct ata_device *atadev)
195 struct acd_softc *cdp = atadev->driver;
196 struct acd_devlist *entry;
200 if (cdp->changer_info) {
201 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
202 if (cdp->driver[subdev] == cdp)
204 while ((bp = bioq_first(&cdp->driver[subdev]->queue))) {
205 bioq_remove(&cdp->driver[subdev]->queue, bp);
206 biofinish(bp, NULL, ENXIO);
208 destroy_dev(cdp->driver[subdev]->dev);
209 while ((entry = TAILQ_FIRST(&cdp->driver[subdev]->dev_list))) {
210 destroy_dev(entry->dev);
211 TAILQ_REMOVE(&cdp->driver[subdev]->dev_list, entry, chain);
214 devstat_remove_entry(cdp->driver[subdev]->stats);
215 free(cdp->driver[subdev]->stats, M_ACD);
216 ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
217 free(cdp->driver[subdev], M_ACD);
219 free(cdp->driver, M_ACD);
220 free(cdp->changer_info, M_ACD);
222 while ((bp = bioq_first(&cdp->queue)))
223 biofinish(bp, NULL, ENXIO);
224 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
225 destroy_dev(entry->dev);
226 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
229 destroy_dev(cdp->dev);
230 EVENTHANDLER_DEREGISTER(dev_clone, cdp->clone_evh);
231 devstat_remove_entry(cdp->stats);
232 free(cdp->stats, M_ACD);
233 ata_free_name(atadev);
234 ata_free_lun(&acd_lun_map, cdp->lun);
236 atadev->driver = NULL;
239 static struct acd_softc *
240 acd_init_lun(struct ata_device *atadev)
242 struct acd_softc *cdp;
244 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO)))
246 TAILQ_INIT(&cdp->dev_list);
247 bioq_init(&cdp->queue);
248 cdp->device = atadev;
249 cdp->lun = ata_get_lun(&acd_lun_map);
250 cdp->block_size = 2048;
252 cdp->changer_info = NULL;
253 if (!(cdp->stats = malloc(sizeof(struct devstat), M_ACD,
254 M_NOWAIT | M_ZERO))) {
262 acd_clone(void *arg, char *name, int namelen, dev_t *dev)
264 struct acd_softc *cdp = arg;
270 if (!dev_stdclone(name, &p, "acd", &unit))
272 if (*p != '\0' && strcmp(p, "a") != 0 && strcmp(p, "c") != 0)
274 if (unit == cdp->lun)
275 *dev = makedev(acd_cdevsw.d_maj, dkmakeminor(cdp->lun, 0, 0));
279 acd_make_dev(struct acd_softc *cdp)
283 dev = make_dev(&acd_cdevsw, dkmakeminor(cdp->lun, 0, 0),
284 UID_ROOT, GID_OPERATOR, 0644, "acd%d", cdp->lun);
285 make_dev_alias(dev, "acd%da", cdp->lun);
286 make_dev_alias(dev, "acd%dc", cdp->lun);
289 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
290 cdp->clone_evh = EVENTHANDLER_REGISTER(dev_clone, acd_clone, cdp, 1000);
295 acd_set_ioparm(struct acd_softc *cdp)
297 cdp->dev->si_iosize_max = ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
298 cdp->dev->si_bsize_phys = cdp->block_size;
302 acd_describe(struct acd_softc *cdp)
308 ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
309 cdp->device->param->model, cdp->device->param->revision,
310 (cdp->cap.write_dvdr) ? "DVD-R" :
311 (cdp->cap.write_dvdram) ? "DVD-RAM" :
312 (cdp->cap.write_cdrw) ? "CD-RW" :
313 (cdp->cap.write_cdr) ? "CD-R" :
314 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM",
315 device_get_unit(cdp->device->channel->dev),
316 (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
318 ata_prtdev(cdp->device, "%s", "");
319 if (cdp->cap.cur_read_speed) {
320 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
321 if (cdp->cap.max_read_speed)
322 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
323 if ((cdp->cap.cur_write_speed) &&
324 (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
325 cdp->cap.write_dvdr || cdp->cap.write_dvdram)) {
326 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
327 if (cdp->cap.max_write_speed)
328 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
332 if (cdp->cap.buf_size) {
333 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
336 printf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
338 ata_prtdev(cdp->device, "Reads:");
340 if (cdp->cap.read_cdr) {
341 printf(" CD-R"); comma = 1;
343 if (cdp->cap.read_cdrw) {
344 printf("%s CD-RW", comma ? "," : ""); comma = 1;
346 if (cdp->cap.cd_da) {
347 if (cdp->cap.cd_da_stream)
348 printf("%s CD-DA stream", comma ? "," : "");
350 printf("%s CD-DA", comma ? "," : "");
353 if (cdp->cap.read_dvdrom) {
354 printf("%s DVD-ROM", comma ? "," : ""); comma = 1;
356 if (cdp->cap.read_dvdr) {
357 printf("%s DVD-R", comma ? "," : ""); comma = 1;
359 if (cdp->cap.read_dvdram) {
360 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
362 if (cdp->cap.read_packet)
363 printf("%s packet", comma ? "," : "");
366 ata_prtdev(cdp->device, "Writes:");
367 if (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
368 cdp->cap.write_dvdr || cdp->cap.write_dvdram) {
370 if (cdp->cap.write_cdr) {
371 printf(" CD-R" ); comma = 1;
373 if (cdp->cap.write_cdrw) {
374 printf("%s CD-RW", comma ? "," : ""); comma = 1;
376 if (cdp->cap.write_dvdr) {
377 printf("%s DVD-R", comma ? "," : ""); comma = 1;
379 if (cdp->cap.write_dvdram) {
380 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
382 if (cdp->cap.test_write) {
383 printf("%s test write", comma ? "," : ""); comma = 1;
385 if (cdp->cap.burnproof)
386 printf("%s burnproof", comma ? "," : "");
389 if (cdp->cap.audio_play) {
390 ata_prtdev(cdp->device, "Audio: ");
391 if (cdp->cap.audio_play)
393 if (cdp->cap.max_vol_levels)
394 printf(", %d volume levels", cdp->cap.max_vol_levels);
397 ata_prtdev(cdp->device, "Mechanism: ");
398 switch (cdp->cap.mech) {
400 mechanism = "caddy"; break;
402 mechanism = "tray"; break;
404 mechanism = "popup"; break;
405 case MST_MECH_CHANGER:
406 mechanism = "changer"; break;
407 case MST_MECH_CARTRIDGE:
408 mechanism = "cartridge"; break;
410 mechanism = 0; break;
413 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
414 else if (cdp->cap.eject)
418 printf(cdp->cap.locked ? ", locked" : ", unlocked");
419 if (cdp->cap.prevent)
420 printf(", lock protected");
423 if (cdp->cap.mech != MST_MECH_CHANGER) {
424 ata_prtdev(cdp->device, "Medium: ");
425 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
427 printf("CD-ROM "); break;
429 printf("CD-R "); break;
431 printf("CD-RW "); break;
433 printf("door open"); break;
435 printf("no/blank disc"); break;
437 printf("medium format error"); break;
439 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
440 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
442 printf("120mm data disc"); break;
444 printf("120mm audio disc"); break;
446 printf("120mm data/audio disc"); break;
448 printf("120mm photo disc"); break;
450 printf("80mm data disc"); break;
452 printf("80mm audio disc"); break;
454 printf("80mm data/audio disc"); break;
456 printf("80mm photo disc"); break;
458 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
460 printf("unknown"); break;
463 printf("blank"); break;
467 printf("unknown (0x%x)", cdp->cap.medium_type); break;
474 ata_prtdev(cdp->device, "%s ",
475 (cdp->cap.write_dvdr) ? "DVD-R" :
476 (cdp->cap.write_dvdram) ? "DVD-RAM" :
477 (cdp->cap.write_cdrw) ? "CD-RW" :
478 (cdp->cap.write_cdr) ? "CD-R" :
479 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM");
481 if (cdp->changer_info)
482 printf("with %d CD changer ", cdp->changer_info->slots);
484 printf("<%.40s> at ata%d-%s %s\n", cdp->device->param->model,
485 device_get_unit(cdp->device->channel->dev),
486 (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
487 ata_mode2str(cdp->device->mode) );
492 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
496 *m = lba / (60 * 75);
502 static __inline u_int32_t
503 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
505 return (m * 60 + s) * 75 + f - 150;
509 acdopen(dev_t dev, int flags, int fmt, struct thread *td)
511 struct acd_softc *cdp = dev->si_drv1;
517 if (flags & FWRITE) {
518 if (count_dev(dev) > 1)
522 /* wait if drive is not finished loading the medium */
524 struct atapi_reqsense *sense = cdp->device->result;
526 if (!atapi_test_ready(cdp->device))
528 if (sense->sense_key == 2 && sense->asc == 4 && sense->ascq == 1)
529 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
534 if (count_dev(dev) == 1) {
535 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
536 acd_select_slot(cdp);
537 tsleep(&cdp->changer_info, PRIBIO, "acdopn", 0);
539 acd_prevent_allow(cdp, 1);
540 cdp->flags |= F_LOCKED;
547 acdclose(dev_t dev, int flags, int fmt, struct thread *td)
549 struct acd_softc *cdp = dev->si_drv1;
554 if (count_dev(dev) == 1) {
555 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
556 acd_select_slot(cdp);
557 tsleep(&cdp->changer_info, PRIBIO, "acdclo", 0);
559 acd_prevent_allow(cdp, 0);
560 cdp->flags &= ~F_LOCKED;
566 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
568 struct acd_softc *cdp = dev->si_drv1;
574 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
575 acd_select_slot(cdp);
576 tsleep(&cdp->changer_info, PRIBIO, "acdctl", 0);
578 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
581 atapi_test_ready(cdp->device);
586 acd_prevent_allow(cdp, 1);
587 cdp->flags |= F_LOCKED;
593 error = acd_pause_resume(cdp, 1);
597 error = acd_pause_resume(cdp, 0);
601 error = acd_start_stop(cdp, 1);
605 error = acd_start_stop(cdp, 0);
609 error = acd_prevent_allow(cdp, 0);
610 cdp->flags &= ~F_LOCKED;
614 error = acd_prevent_allow(cdp, 1);
615 cdp->flags |= F_LOCKED;
622 error = atapi_test_ready(cdp->device);
626 if (count_dev(dev) > 1) {
630 error = acd_eject(cdp, 0);
634 if (count_dev(dev) > 1)
636 error = acd_eject(cdp, 1);
639 case CDIOREADTOCHEADER:
640 if (!cdp->toc.hdr.ending_track) {
644 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
647 case CDIOREADTOCENTRYS:
649 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
650 struct toc *toc = &cdp->toc;
651 int starting_track = te->starting_track;
654 if (!toc->hdr.ending_track) {
659 if (te->data_len < sizeof(toc->tab[0]) ||
660 (te->data_len % sizeof(toc->tab[0])) != 0 ||
661 (te->address_format != CD_MSF_FORMAT &&
662 te->address_format != CD_LBA_FORMAT)) {
668 starting_track = toc->hdr.starting_track;
669 else if (starting_track == 170)
670 starting_track = toc->hdr.ending_track + 1;
671 else if (starting_track < toc->hdr.starting_track ||
672 starting_track > toc->hdr.ending_track + 1) {
677 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
679 if (te->data_len < len)
681 if (len > sizeof(toc->tab)) {
686 if (te->address_format == CD_MSF_FORMAT) {
687 struct cd_toc_entry *entry;
689 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
690 bcopy(&cdp->toc, toc, sizeof(struct toc));
691 entry = toc->tab + (toc->hdr.ending_track + 1 -
692 toc->hdr.starting_track) + 1;
693 while (--entry >= toc->tab)
694 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
695 &entry->addr.msf.second, &entry->addr.msf.frame);
697 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
699 if (te->address_format == CD_MSF_FORMAT)
703 case CDIOREADTOCENTRY:
705 struct ioc_read_toc_single_entry *te =
706 (struct ioc_read_toc_single_entry *)addr;
707 struct toc *toc = &cdp->toc;
708 u_char track = te->track;
710 if (!toc->hdr.ending_track) {
715 if (te->address_format != CD_MSF_FORMAT &&
716 te->address_format != CD_LBA_FORMAT) {
722 track = toc->hdr.starting_track;
723 else if (track == 170)
724 track = toc->hdr.ending_track + 1;
725 else if (track < toc->hdr.starting_track ||
726 track > toc->hdr.ending_track + 1) {
731 if (te->address_format == CD_MSF_FORMAT) {
732 struct cd_toc_entry *entry;
734 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
735 bcopy(&cdp->toc, toc, sizeof(struct toc));
737 entry = toc->tab + (track - toc->hdr.starting_track);
738 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
739 &entry->addr.msf.second, &entry->addr.msf.frame);
741 bcopy(toc->tab + track - toc->hdr.starting_track,
742 &te->entry, sizeof(struct cd_toc_entry));
743 if (te->address_format == CD_MSF_FORMAT)
748 case CDIOCREADSUBCHANNEL:
750 struct ioc_read_subchannel *args =
751 (struct ioc_read_subchannel *)addr;
753 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
754 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
755 0, 0, 0, 0, 0, 0, 0 };
757 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
758 args->data_len < sizeof(struct cd_sub_channel_header)) {
763 format=args->data_format;
764 if ((format != CD_CURRENT_POSITION) &&
765 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
770 ccb[1] = args->address_format & CD_MSF_FORMAT;
772 if ((error = atapi_queue_cmd(cdp->device,ccb,(caddr_t)&cdp->subchan,
773 sizeof(cdp->subchan), ATPR_F_READ, 10,
777 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
778 if (cdp->subchan.header.audio_status == 0x11) {
784 if (format == CD_TRACK_INFO)
785 ccb[6] = args->track;
787 if ((error = atapi_queue_cmd(cdp->device, ccb,
788 (caddr_t)&cdp->subchan,
789 sizeof(cdp->subchan), ATPR_F_READ,
794 error = copyout(&cdp->subchan, args->data, args->data_len);
800 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
804 msf2lba(args->start_m, args->start_s, args->start_f),
805 msf2lba(args->end_m, args->end_s, args->end_f));
809 case CDIOCPLAYBLOCKS:
811 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
813 error = acd_play(cdp, args->blk, args->blk + args->len);
817 case CDIOCPLAYTRACKS:
819 struct ioc_play_track *args = (struct ioc_play_track *)addr;
822 if (!cdp->toc.hdr.ending_track) {
826 if (args->end_track < cdp->toc.hdr.ending_track + 1)
828 if (args->end_track > cdp->toc.hdr.ending_track + 1)
829 args->end_track = cdp->toc.hdr.ending_track + 1;
830 t1 = args->start_track - cdp->toc.hdr.starting_track;
831 t2 = args->end_track - cdp->toc.hdr.starting_track;
832 if (t1 < 0 || t2 < 0 ||
833 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
837 error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
838 ntohl(cdp->toc.tab[t2].addr.lba));
844 struct ioc_read_audio *args = (struct ioc_read_audio *)addr;
846 caddr_t buffer, ubuf = args->buffer;
850 if (!cdp->toc.hdr.ending_track) {
855 if ((frames = args->nframes) < 0) {
860 if (args->address_format == CD_LBA_FORMAT)
861 lba = args->address.lba;
862 else if (args->address_format == CD_MSF_FORMAT)
863 lba = msf2lba(args->address.msf.minute,
864 args->address.msf.second,
865 args->address.msf.frame);
871 #ifndef CD_BUFFER_BLOCKS
872 #define CD_BUFFER_BLOCKS 13
874 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_NOWAIT))){
878 bzero(ccb, sizeof(ccb));
883 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
884 size = blocks * 2352;
886 ccb[0] = ATAPI_READ_CD;
894 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size,
895 ATPR_F_READ, 30, NULL,NULL)))
898 if ((error = copyout(buffer, ubuf, size)))
906 if (args->address_format == CD_LBA_FORMAT)
907 args->address.lba = lba;
908 else if (args->address_format == CD_MSF_FORMAT)
909 lba2msf(lba, &args->address.msf.minute,
910 &args->address.msf.second,
911 &args->address.msf.frame);
917 struct ioc_vol *arg = (struct ioc_vol *)addr;
919 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
920 (caddr_t)&cdp->au, sizeof(cdp->au))))
923 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
927 arg->vol[0] = cdp->au.port[0].volume;
928 arg->vol[1] = cdp->au.port[1].volume;
929 arg->vol[2] = cdp->au.port[2].volume;
930 arg->vol[3] = cdp->au.port[3].volume;
936 struct ioc_vol *arg = (struct ioc_vol *)addr;
938 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
939 (caddr_t)&cdp->au, sizeof(cdp->au))))
941 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
945 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
946 (caddr_t)&cdp->aumask,
947 sizeof(cdp->aumask))))
949 cdp->au.data_length = 0;
950 cdp->au.port[0].channels = CHANNEL_0;
951 cdp->au.port[1].channels = CHANNEL_1;
952 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
953 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
954 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
955 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
956 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
961 struct ioc_patch *arg = (struct ioc_patch *)addr;
963 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
964 arg->patch[2], arg->patch[3]);
969 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
973 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
977 error = acd_setchan(cdp, 0, 0, 0, 0);
981 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
985 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
989 error = acd_blank(cdp, (*(int *)addr));
992 case CDRIOCNEXTWRITEABLEADDR:
994 struct acd_track_info track_info;
996 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
999 if (!track_info.nwa_valid) {
1003 *(int*)addr = track_info.next_writeable_addr;
1007 case CDRIOCINITWRITER:
1008 error = acd_init_writer(cdp, (*(int *)addr));
1011 case CDRIOCINITTRACK:
1012 error = acd_init_track(cdp, (struct cdr_track *)addr);
1016 error = acd_flush(cdp);
1020 error = acd_fixate(cdp, (*(int *)addr));
1023 case CDRIOCREADSPEED:
1024 error = acd_set_speed(cdp, *(int *)addr, CDR_MAX_SPEED);
1027 case CDRIOCWRITESPEED:
1028 error = acd_set_speed(cdp, CDR_MAX_SPEED, *(int *)addr);
1031 case CDRIOCGETBLOCKSIZE:
1032 *(int *)addr = cdp->block_size;
1035 case CDRIOCSETBLOCKSIZE:
1036 cdp->block_size = *(int *)addr;
1037 acd_set_ioparm(cdp);
1040 case CDRIOCGETPROGRESS:
1041 error = acd_get_progress(cdp, (int *)addr);
1045 error = acd_send_cue(cdp, (struct cdr_cuesheet *)addr);
1048 case CDRIOCREADFORMATCAPS:
1049 error = acd_read_format_caps(cdp, (struct cdr_format_capacities *)addr);
1053 error = acd_format(cdp, (struct cdr_format_params *)addr);
1056 case DVDIOCREPORTKEY:
1057 if (!cdp->cap.read_dvdrom)
1060 error = acd_report_key(cdp, (struct dvd_authinfo *)addr);
1064 if (!cdp->cap.read_dvdrom)
1067 error = acd_send_key(cdp, (struct dvd_authinfo *)addr);
1070 case DVDIOCREADSTRUCTURE:
1071 if (!cdp->cap.read_dvdrom)
1074 error = acd_read_structure(cdp, (struct dvd_struct *)addr);
1078 *(struct disklabel *)addr = cdp->disklabel;
1083 if ((flags & FWRITE) == 0)
1086 error = setdisklabel(&cdp->disklabel, (struct disklabel *)addr, 0);
1100 acdstrategy(struct bio *bp)
1102 struct acd_softc *cdp = bp->bio_dev->si_drv1;
1105 if (cdp->device->flags & ATA_D_DETACHING) {
1106 biofinish(bp, NULL, ENXIO);
1110 /* if it's a null transfer, return immediatly. */
1111 if (bp->bio_bcount == 0) {
1117 bp->bio_pblkno = bp->bio_blkno;
1118 bp->bio_resid = bp->bio_bcount;
1121 bioqdisksort(&cdp->queue, bp);
1123 ata_start(cdp->device->channel);
1127 acd_start(struct ata_device *atadev)
1129 struct acd_softc *cdp = atadev->driver;
1130 struct bio *bp = bioq_first(&cdp->queue);
1131 u_int32_t lba, lastlba, count;
1133 int track, blocksize;
1135 if (cdp->changer_info) {
1138 cdp = cdp->driver[cdp->changer_info->current_slot];
1139 bp = bioq_first(&cdp->queue);
1141 /* check for work pending on any other slot */
1142 for (i = 0; i < cdp->changer_info->slots; i++) {
1143 if (i == cdp->changer_info->current_slot)
1145 if (bioq_first(&(cdp->driver[i]->queue))) {
1146 if (!bp || time_second > (cdp->timestamp + 10)) {
1147 acd_select_slot(cdp->driver[i]);
1155 bioq_remove(&cdp->queue, bp);
1157 /* reject all queued entries if media changed */
1158 if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1159 biofinish(bp, NULL, EIO);
1163 bzero(ccb, sizeof(ccb));
1165 track = (bp->bio_dev->si_udev & 0x00ff0000) >> 16;
1168 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1169 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1170 lba = bp->bio_offset / blocksize;
1171 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1174 blocksize = cdp->block_size;
1175 lastlba = cdp->disk_size;
1176 lba = bp->bio_offset / blocksize;
1179 if (bp->bio_bcount % blocksize != 0) {
1180 biofinish(bp, NULL, EINVAL);
1183 count = bp->bio_bcount / blocksize;
1185 if (bp->bio_cmd == BIO_READ) {
1186 /* if transfer goes beyond range adjust it to be within limits */
1187 if (lba + count > lastlba) {
1188 /* if we are entirely beyond EOM return EOF */
1189 if (lastlba <= lba) {
1190 bp->bio_resid = bp->bio_bcount;
1194 count = lastlba - lba;
1196 switch (blocksize) {
1198 ccb[0] = ATAPI_READ_BIG;
1202 ccb[0] = ATAPI_READ_CD;
1207 ccb[0] = ATAPI_READ_CD;
1212 ccb[0] = ATAPI_WRITE_BIG;
1223 devstat_start_transaction(cdp->stats);
1224 bp->bio_caller1 = cdp;
1225 atapi_queue_cmd(cdp->device, ccb, bp->bio_data, count * blocksize,
1226 bp->bio_cmd == BIO_READ ? ATPR_F_READ : 0,
1227 (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bp);
1231 acd_done(struct atapi_request *request)
1233 struct bio *bp = request->driver;
1234 struct acd_softc *cdp = bp->bio_caller1;
1236 if (request->error) {
1237 bp->bio_error = request->error;
1238 bp->bio_flags |= BIO_ERROR;
1241 bp->bio_resid = bp->bio_bcount - request->donecount;
1242 biofinish(bp, cdp->stats, 0);
1247 acd_read_toc(struct acd_softc *cdp)
1249 struct acd_devlist *entry;
1250 int track, ntracks, len;
1254 bzero(&cdp->toc, sizeof(cdp->toc));
1255 bzero(ccb, sizeof(ccb));
1257 if (atapi_test_ready(cdp->device) != 0)
1260 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1262 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1263 ccb[0] = ATAPI_READ_TOC;
1266 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1267 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1268 bzero(&cdp->toc, sizeof(cdp->toc));
1271 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1272 if (ntracks <= 0 || ntracks > MAXTRK) {
1273 bzero(&cdp->toc, sizeof(cdp->toc));
1277 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1278 bzero(ccb, sizeof(ccb));
1279 ccb[0] = ATAPI_READ_TOC;
1282 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1283 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1284 bzero(&cdp->toc, sizeof(cdp->toc));
1287 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1289 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1290 acd_set_ioparm(cdp);
1291 bzero(ccb, sizeof(ccb));
1292 ccb[0] = ATAPI_READ_CAPACITY;
1293 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1294 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1295 bzero(&cdp->toc, sizeof(cdp->toc));
1298 cdp->disk_size = ntohl(sizes[0]) + 1;
1300 bzero(&cdp->disklabel, sizeof(struct disklabel));
1301 strncpy(cdp->disklabel.d_typename, " ",
1302 sizeof(cdp->disklabel.d_typename));
1303 strncpy(cdp->disklabel.d_typename, cdp->device->name,
1304 min(strlen(cdp->device->name),sizeof(cdp->disklabel.d_typename)-1));
1305 strncpy(cdp->disklabel.d_packname, "unknown ",
1306 sizeof(cdp->disklabel.d_packname));
1307 cdp->disklabel.d_secsize = cdp->block_size;
1308 cdp->disklabel.d_nsectors = 100;
1309 cdp->disklabel.d_ntracks = 1;
1310 cdp->disklabel.d_ncylinders = (cdp->disk_size / 100) + 1;
1311 cdp->disklabel.d_secpercyl = 100;
1312 cdp->disklabel.d_secperunit = cdp->disk_size;
1313 cdp->disklabel.d_rpm = 300;
1314 cdp->disklabel.d_interleave = 1;
1315 cdp->disklabel.d_flags = D_REMOVABLE;
1316 cdp->disklabel.d_npartitions = 1;
1317 cdp->disklabel.d_partitions[0].p_offset = 0;
1318 cdp->disklabel.d_partitions[0].p_size = cdp->disk_size;
1319 cdp->disklabel.d_partitions[0].p_fstype = FS_BSDFFS;
1320 cdp->disklabel.d_magic = DISKMAGIC;
1321 cdp->disklabel.d_magic2 = DISKMAGIC;
1322 cdp->disklabel.d_checksum = dkcksum(&cdp->disklabel);
1324 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1325 destroy_dev(entry->dev);
1326 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1329 for (track = 1; track <= ntracks; track ++) {
1332 sprintf(name, "acd%dt%02d", cdp->lun, track);
1333 entry = malloc(sizeof(struct acd_devlist), M_ACD, M_NOWAIT | M_ZERO);
1334 entry->dev = make_dev(&acd_cdevsw, (cdp->lun << 3) | (track << 16),
1335 0, 0, 0644, name, NULL);
1336 entry->dev->si_drv1 = cdp->dev->si_drv1;
1337 TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1341 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1342 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1343 cdp->disk_size, cdp->block_size,
1344 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1345 if (cdp->toc.tab[0].control & 4)
1346 printf("%dMB\n", cdp->disk_size / 512);
1348 printf("%d:%d audio\n",
1349 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1355 acd_play(struct acd_softc *cdp, int start, int end)
1359 bzero(ccb, sizeof(ccb));
1360 ccb[0] = ATAPI_PLAY_MSF;
1361 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1362 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1363 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1367 acd_setchan(struct acd_softc *cdp,
1368 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1372 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1375 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1377 cdp->au.data_length = 0;
1378 cdp->au.port[0].channels = c0;
1379 cdp->au.port[1].channels = c1;
1380 cdp->au.port[2].channels = c2;
1381 cdp->au.port[3].channels = c3;
1382 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1386 acd_select_done1(struct atapi_request *request)
1388 struct acd_softc *cdp = request->driver;
1390 cdp->changer_info->current_slot = cdp->slot;
1391 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1392 wakeup(&cdp->changer_info);
1397 acd_select_done(struct atapi_request *request)
1399 struct acd_softc *cdp = request->driver;
1400 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1401 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1403 /* load the wanted slot */
1404 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1405 acd_select_done1, cdp);
1410 acd_select_slot(struct acd_softc *cdp)
1412 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1413 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1415 /* unload the current media from player */
1416 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1417 acd_select_done, cdp);
1421 acd_init_writer(struct acd_softc *cdp, int test_write)
1425 bzero(ccb, sizeof(ccb));
1426 ccb[0] = ATAPI_REZERO;
1427 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1428 ccb[0] = ATAPI_SEND_OPC_INFO;
1430 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1435 acd_fixate(struct acd_softc *cdp, int multisession)
1437 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1438 0, 0, 0, 0, 0, 0, 0, 0 };
1439 int timeout = 5*60*2;
1441 struct write_param param;
1443 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1444 (caddr_t)¶m, sizeof(param))))
1447 param.data_length = 0;
1449 param.session_type = CDR_SESS_MULTI;
1451 param.session_type = CDR_SESS_NONE;
1453 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1456 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1460 /* some drives just return ready, wait for the expected fixate time */
1461 if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1462 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1463 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1464 return atapi_test_ready(cdp->device);
1467 while (timeout-- > 0) {
1468 if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1470 tsleep(&error, PRIBIO, "acdcld", hz/2);
1476 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1478 struct write_param param;
1481 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1482 (caddr_t)¶m, sizeof(param))))
1485 param.data_length = 0;
1486 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1487 param.page_length = 0x32;
1488 param.test_write = track->test_write ? 1 : 0;
1489 param.write_type = CDR_WTYPE_TRACK;
1490 param.session_type = CDR_SESS_NONE;
1492 param.packet_size = 0;
1494 if (cdp->cap.burnproof)
1495 param.burnproof = 1;
1497 switch (track->datablock_type) {
1501 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1503 param.track_mode = CDR_TMODE_AUDIO;
1504 cdp->block_size = 2352;
1505 param.datablock_type = CDR_DB_RAW;
1506 param.session_format = CDR_SESS_CDROM;
1509 case CDR_DB_ROM_MODE1:
1510 cdp->block_size = 2048;
1511 param.track_mode = CDR_TMODE_DATA;
1512 param.datablock_type = CDR_DB_ROM_MODE1;
1513 param.session_format = CDR_SESS_CDROM;
1516 case CDR_DB_ROM_MODE2:
1517 cdp->block_size = 2336;
1518 param.track_mode = CDR_TMODE_DATA;
1519 param.datablock_type = CDR_DB_ROM_MODE2;
1520 param.session_format = CDR_SESS_CDROM;
1523 case CDR_DB_XA_MODE1:
1524 cdp->block_size = 2048;
1525 param.track_mode = CDR_TMODE_DATA;
1526 param.datablock_type = CDR_DB_XA_MODE1;
1527 param.session_format = CDR_SESS_CDROM_XA;
1530 case CDR_DB_XA_MODE2_F1:
1531 cdp->block_size = 2056;
1532 param.track_mode = CDR_TMODE_DATA;
1533 param.datablock_type = CDR_DB_XA_MODE2_F1;
1534 param.session_format = CDR_SESS_CDROM_XA;
1537 case CDR_DB_XA_MODE2_F2:
1538 cdp->block_size = 2324;
1539 param.track_mode = CDR_TMODE_DATA;
1540 param.datablock_type = CDR_DB_XA_MODE2_F2;
1541 param.session_format = CDR_SESS_CDROM_XA;
1544 case CDR_DB_XA_MODE2_MIX:
1545 cdp->block_size = 2332;
1546 param.track_mode = CDR_TMODE_DATA;
1547 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1548 param.session_format = CDR_SESS_CDROM_XA;
1551 acd_set_ioparm(cdp);
1552 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1556 acd_flush(struct acd_softc *cdp)
1558 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1559 0, 0, 0, 0, 0, 0, 0, 0 };
1561 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1566 acd_read_track_info(struct acd_softc *cdp,
1567 int32_t lba, struct acd_track_info *info)
1569 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1570 lba>>24, lba>>16, lba>>8, lba,
1572 sizeof(*info)>>8, sizeof(*info),
1573 0, 0, 0, 0, 0, 0, 0 };
1576 if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1577 ATPR_F_READ, 30, NULL, NULL)))
1579 info->track_start_addr = ntohl(info->track_start_addr);
1580 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1581 info->free_blocks = ntohl(info->free_blocks);
1582 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1583 info->track_length = ntohl(info->track_length);
1588 acd_get_progress(struct acd_softc *cdp, int *finished)
1590 int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
1591 sizeof(struct atapi_reqsense), 0, 0, 0,
1592 0, 0, 0, 0, 0, 0, 0, 0 };
1593 struct atapi_reqsense sense;
1596 error = atapi_queue_cmd(cdp->device, ccb,
1597 (caddr_t)&sense, sizeof(struct atapi_reqsense),
1598 ATPR_F_READ, 30, NULL, NULL);
1606 *finished = ((sense.sk_specific2|(sense.sk_specific1<<8))*100)/65535;
1613 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1615 struct write_param param;
1616 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1617 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1618 0, 0, 0, 0, 0, 0, 0 };
1625 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1626 (caddr_t)¶m, sizeof(param))))
1629 param.data_length = 0;
1630 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1631 param.page_length = 0x32;
1632 param.test_write = cuesheet->test_write ? 1 : 0;
1633 param.write_type = CDR_WTYPE_SESSION;
1634 param.session_type = cuesheet->session_type;
1636 param.packet_size = 0;
1637 param.track_mode = CDR_TMODE_AUDIO;
1638 param.datablock_type = CDR_DB_RAW;
1639 param.session_format = cuesheet->session_format;
1640 if (cdp->cap.burnproof)
1641 param.burnproof = 1;
1643 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1646 if (!(buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT)))
1649 if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len))) {
1651 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1652 for (i=0; i<cuesheet->len; i++)
1654 printf(" %02x", buffer[i]);
1656 printf("\n%02x", buffer[i]);
1659 error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1662 free(buffer, M_ACD);
1667 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1669 struct dvd_miscauth *d;
1675 switch (ai->format) {
1676 case DVD_REPORT_AGID:
1677 case DVD_REPORT_ASF:
1678 case DVD_REPORT_RPC:
1681 case DVD_REPORT_KEY1:
1684 case DVD_REPORT_TITLE_KEY:
1688 case DVD_REPORT_CHALLENGE:
1691 case DVD_INVALIDATE_AGID:
1698 bzero(ccb, sizeof(ccb));
1699 ccb[0] = ATAPI_REPORT_KEY;
1700 ccb[2] = (lba >> 24) & 0xff;
1701 ccb[3] = (lba >> 16) & 0xff;
1702 ccb[4] = (lba >> 8) & 0xff;
1703 ccb[5] = lba & 0xff;
1704 ccb[8] = (length >> 8) & 0xff;
1705 ccb[9] = length & 0xff;
1706 ccb[10] = (ai->agid << 6) | ai->format;
1708 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1709 d->length = htons(length - 2);
1711 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1712 ai->format == DVD_INVALIDATE_AGID ? 0 : ATPR_F_READ,
1719 switch (ai->format) {
1720 case DVD_REPORT_AGID:
1721 ai->agid = d->data[3] >> 6;
1724 case DVD_REPORT_CHALLENGE:
1725 bcopy(&d->data[0], &ai->keychal[0], 10);
1728 case DVD_REPORT_KEY1:
1729 bcopy(&d->data[0], &ai->keychal[0], 5);
1732 case DVD_REPORT_TITLE_KEY:
1733 ai->cpm = (d->data[0] >> 7);
1734 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1735 ai->cgms = (d->data[0] >> 4) & 0x3;
1736 bcopy(&d->data[1], &ai->keychal[0], 5);
1739 case DVD_REPORT_ASF:
1740 ai->asf = d->data[3] & 1;
1743 case DVD_REPORT_RPC:
1744 ai->reg_type = (d->data[0] >> 6);
1745 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1746 ai->user_rsts = d->data[0] & 0x7;
1747 ai->region = d->data[1];
1748 ai->rpc_scheme = d->data[2];
1751 case DVD_INVALIDATE_AGID:
1762 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1764 struct dvd_miscauth *d;
1769 switch (ai->format) {
1770 case DVD_SEND_CHALLENGE:
1772 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1773 bcopy(ai->keychal, &d->data[0], 10);
1778 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1779 bcopy(&ai->keychal[0], &d->data[0], 5);
1784 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1785 d->data[0] = ai->region;
1792 bzero(ccb, sizeof(ccb));
1793 ccb[0] = ATAPI_SEND_KEY;
1794 ccb[8] = (length >> 8) & 0xff;
1795 ccb[9] = length & 0xff;
1796 ccb[10] = (ai->agid << 6) | ai->format;
1797 d->length = htons(length - 2);
1798 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1805 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1807 struct dvd_miscauth *d;
1813 case DVD_STRUCT_PHYSICAL:
1817 case DVD_STRUCT_COPYRIGHT:
1821 case DVD_STRUCT_DISCKEY:
1825 case DVD_STRUCT_BCA:
1829 case DVD_STRUCT_MANUFACT:
1833 case DVD_STRUCT_DDS:
1834 case DVD_STRUCT_PRERECORDED:
1835 case DVD_STRUCT_UNIQUEID:
1836 case DVD_STRUCT_LIST:
1837 case DVD_STRUCT_CMI:
1838 case DVD_STRUCT_RMD_LAST:
1839 case DVD_STRUCT_RMD_RMA:
1840 case DVD_STRUCT_DCB:
1847 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1848 d->length = htons(length - 2);
1850 bzero(ccb, sizeof(ccb));
1851 ccb[0] = ATAPI_READ_STRUCTURE;
1852 ccb[6] = s->layer_num;
1854 ccb[8] = (length >> 8) & 0xff;
1855 ccb[9] = length & 0xff;
1856 ccb[10] = s->agid << 6;
1857 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1864 switch (s->format) {
1865 case DVD_STRUCT_PHYSICAL: {
1866 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1868 layer->book_type = d->data[0] >> 4;
1869 layer->book_version = d->data[0] & 0xf;
1870 layer->disc_size = d->data[1] >> 4;
1871 layer->max_rate = d->data[1] & 0xf;
1872 layer->nlayers = (d->data[2] >> 5) & 3;
1873 layer->track_path = (d->data[2] >> 4) & 1;
1874 layer->layer_type = d->data[2] & 0xf;
1875 layer->linear_density = d->data[3] >> 4;
1876 layer->track_density = d->data[3] & 0xf;
1877 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1878 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1879 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1880 layer->bca = d->data[16] >> 7;
1884 case DVD_STRUCT_COPYRIGHT:
1885 s->cpst = d->data[0];
1886 s->rmi = d->data[0];
1889 case DVD_STRUCT_DISCKEY:
1890 bcopy(&d->data[0], &s->data[0], 2048);
1893 case DVD_STRUCT_BCA:
1894 s->length = ntohs(d->length);
1895 bcopy(&d->data[0], &s->data[0], s->length);
1898 case DVD_STRUCT_MANUFACT:
1899 s->length = ntohs(d->length);
1900 bcopy(&d->data[0], &s->data[0], s->length);
1911 acd_eject(struct acd_softc *cdp, int close)
1915 if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1918 if ((error = acd_start_stop(cdp, 3)))
1921 acd_prevent_allow(cdp, 1);
1922 cdp->flags |= F_LOCKED;
1929 acd_prevent_allow(cdp, 0);
1930 cdp->flags &= ~F_LOCKED;
1931 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1932 return acd_start_stop(cdp, 2);
1936 acd_blank(struct acd_softc *cdp, int blanktype)
1938 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1939 0, 0, 0, 0, 0, 0, 0, 0 };
1941 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1942 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1946 acd_prevent_allow(struct acd_softc *cdp, int lock)
1948 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1949 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1951 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1955 acd_start_stop(struct acd_softc *cdp, int start)
1957 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1958 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1960 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1964 acd_pause_resume(struct acd_softc *cdp, int pause)
1966 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1967 0, 0, 0, 0, 0, 0, 0 };
1969 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1973 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1975 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1976 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1979 error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1982 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1988 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1990 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1991 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1994 ata_prtdev(cdp->device,
1995 "modeselect pagesize=%d\n", pagesize);
1996 atapi_dump("mode select ", pagebuf, pagesize);
1998 return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
2003 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
2005 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
2006 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2009 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
2016 acd_get_cap(struct acd_softc *cdp)
2020 /* get drive capabilities, some drives needs this repeated */
2021 while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
2022 (caddr_t)&cdp->cap, sizeof(cdp->cap)))
2024 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
2025 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
2026 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
2027 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
2028 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
2029 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
2033 acd_read_format_caps(struct acd_softc *cdp, struct cdr_format_capacities *caps)
2035 int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
2036 (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
2037 sizeof(struct cdr_format_capacities) & 0xff,
2038 0, 0, 0, 0, 0, 0, 0 };
2040 return atapi_queue_cmd(cdp->device, ccb, (caddr_t)caps,
2041 sizeof(struct cdr_format_capacities),
2042 ATPR_F_READ, 30, NULL, NULL);
2046 acd_format(struct acd_softc *cdp, struct cdr_format_params* params)
2049 int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
2052 error = atapi_queue_cmd(cdp->device, ccb, (u_int8_t *)params,
2053 sizeof(struct cdr_format_params), 0, 30, NULL,NULL);