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/devicestat.h>
42 #include <sys/cdrio.h>
43 #include <sys/dvdio.h>
44 #include <sys/fcntl.h>
46 #include <sys/ctype.h>
47 #include <machine/bus.h>
48 #include <dev/ata/ata-all.h>
49 #include <dev/ata/atapi-all.h>
50 #include <dev/ata/atapi-cd.h>
52 /* device structures */
53 static d_open_t acdopen;
54 static d_close_t acdclose;
55 static d_ioctl_t acdioctl;
56 static d_strategy_t acdstrategy;
57 static struct cdevsw acd_cdevsw = {
61 /* write */ physwrite,
65 /* strategy */ acdstrategy,
70 /* flags */ D_DISK | D_TRACKCLOSE,
74 static struct acd_softc *acd_init_lun(struct ata_device *);
75 static void acd_make_dev(struct acd_softc *);
76 static void acd_set_ioparm(struct acd_softc *);
77 static void acd_describe(struct acd_softc *);
78 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
79 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
80 static int acd_done(struct atapi_request *);
81 static void acd_read_toc(struct acd_softc *);
82 static int acd_play(struct acd_softc *, int, int);
83 static int acd_setchan(struct acd_softc *, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
84 static void acd_select_slot(struct acd_softc *);
85 static int acd_init_writer(struct acd_softc *, int);
86 static int acd_fixate(struct acd_softc *, int);
87 static int acd_init_track(struct acd_softc *, struct cdr_track *);
88 static int acd_flush(struct acd_softc *);
89 static int acd_read_track_info(struct acd_softc *, int32_t, struct acd_track_info *);
90 static int acd_get_progress(struct acd_softc *, int *);
91 static int acd_send_cue(struct acd_softc *, struct cdr_cuesheet *);
92 static int acd_report_key(struct acd_softc *, struct dvd_authinfo *);
93 static int acd_send_key(struct acd_softc *, struct dvd_authinfo *);
94 static int acd_read_structure(struct acd_softc *, struct dvd_struct *);
95 static int acd_eject(struct acd_softc *, int);
96 static int acd_blank(struct acd_softc *, int);
97 static int acd_prevent_allow(struct acd_softc *, int);
98 static int acd_start_stop(struct acd_softc *, int);
99 static int acd_pause_resume(struct acd_softc *, int);
100 static int acd_mode_sense(struct acd_softc *, int, caddr_t, int);
101 static int acd_mode_select(struct acd_softc *, caddr_t, int);
102 static int acd_set_speed(struct acd_softc *, int, int);
103 static void acd_get_cap(struct acd_softc *);
104 static int acd_read_format_caps(struct acd_softc *, struct cdr_format_capacities *);
105 static int acd_format(struct acd_softc *, struct cdr_format_params *);
108 static u_int32_t acd_lun_map = 0;
109 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
112 acdattach(struct ata_device *atadev)
114 struct acd_softc *cdp;
117 if ((cdp = acd_init_lun(atadev)) == NULL) {
118 ata_prtdev(atadev, "acd: out of memory\n");
122 ata_set_name(atadev, "acd", cdp->lun);
125 /* if this is a changer device, allocate the neeeded lun's */
126 if (cdp->cap.mech == MST_MECH_CHANGER) {
127 int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0,
128 sizeof(struct changer)>>8, sizeof(struct changer),
131 chp = malloc(sizeof(struct changer), M_ACD, M_NOWAIT | M_ZERO);
133 ata_prtdev(atadev, "out of memory\n");
137 if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp,
138 sizeof(struct changer),
139 ATPR_F_READ, 60, NULL, NULL)) {
140 struct acd_softc *tmpcdp = cdp;
141 struct acd_softc **cdparr;
145 chp->table_length = htons(chp->table_length);
146 if (!(cdparr = malloc(sizeof(struct acd_softc) * chp->slots,
148 ata_prtdev(atadev, "out of memory\n");
153 for (count = 0; count < chp->slots; count++) {
155 tmpcdp = acd_init_lun(atadev);
157 ata_prtdev(atadev, "out of memory\n");
161 cdparr[count] = tmpcdp;
162 tmpcdp->driver = cdparr;
163 tmpcdp->slot = count;
164 tmpcdp->changer_info = chp;
165 acd_make_dev(tmpcdp);
166 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
167 DEVSTAT_NO_ORDERED_TAGS,
168 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
169 DEVSTAT_PRIORITY_CD);
171 if (!(name = malloc(strlen(atadev->name) + 2, M_ACD, M_NOWAIT)))
172 ata_prtdev(atadev, "out of memory\n");
176 strcpy(name, atadev->name);
178 ata_free_name(atadev);
179 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
185 devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
186 DEVSTAT_NO_ORDERED_TAGS,
187 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
188 DEVSTAT_PRIORITY_CD);
191 atadev->driver = cdp;
196 acddetach(struct ata_device *atadev)
198 struct acd_softc *cdp = atadev->driver;
199 struct acd_devlist *entry;
203 if (cdp->changer_info) {
204 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
205 if (cdp->driver[subdev] == cdp)
207 while ((bp = bioq_first(&cdp->driver[subdev]->queue))) {
208 bioq_remove(&cdp->driver[subdev]->queue, bp);
209 biofinish(bp, NULL, ENXIO);
211 destroy_dev(cdp->driver[subdev]->dev);
212 while ((entry = TAILQ_FIRST(&cdp->driver[subdev]->dev_list))) {
213 destroy_dev(entry->dev);
214 TAILQ_REMOVE(&cdp->driver[subdev]->dev_list, entry, chain);
217 devstat_remove_entry(cdp->driver[subdev]->stats);
218 free(cdp->driver[subdev]->stats, M_ACD);
219 ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
220 free(cdp->driver[subdev], M_ACD);
222 free(cdp->driver, M_ACD);
223 free(cdp->changer_info, M_ACD);
225 while ((bp = bioq_first(&cdp->queue)))
226 biofinish(bp, NULL, ENXIO);
227 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
228 destroy_dev(entry->dev);
229 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
232 destroy_dev(cdp->dev);
233 EVENTHANDLER_DEREGISTER(dev_clone, cdp->clone_evh);
234 devstat_remove_entry(cdp->stats);
235 free(cdp->stats, M_ACD);
236 ata_free_name(atadev);
237 ata_free_lun(&acd_lun_map, cdp->lun);
239 atadev->driver = NULL;
242 static struct acd_softc *
243 acd_init_lun(struct ata_device *atadev)
245 struct acd_softc *cdp;
247 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO)))
249 TAILQ_INIT(&cdp->dev_list);
250 bioq_init(&cdp->queue);
251 cdp->device = atadev;
252 cdp->lun = ata_get_lun(&acd_lun_map);
253 cdp->block_size = 2048;
255 cdp->changer_info = NULL;
256 if (!(cdp->stats = malloc(sizeof(struct devstat), M_ACD,
257 M_NOWAIT | M_ZERO))) {
265 acd_clone(void *arg, char *name, int namelen, dev_t *dev)
267 struct acd_softc *cdp = arg;
273 if (!dev_stdclone(name, &p, "acd", &unit))
275 if (*p != '\0' && strcmp(p, "a") != 0 && strcmp(p, "c") != 0)
277 if (unit == cdp->lun)
278 *dev = makedev(acd_cdevsw.d_maj, cdp->lun);
282 acd_make_dev(struct acd_softc *cdp)
286 dev = make_dev(&acd_cdevsw, cdp->lun,
287 UID_ROOT, GID_OPERATOR, 0644, "acd%d", cdp->lun);
290 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
291 cdp->clone_evh = EVENTHANDLER_REGISTER(dev_clone, acd_clone, cdp, 1000);
296 acd_set_ioparm(struct acd_softc *cdp)
298 cdp->dev->si_iosize_max = ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
299 cdp->dev->si_bsize_phys = cdp->block_size;
303 acd_describe(struct acd_softc *cdp)
309 ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
310 cdp->device->param->model, cdp->device->param->revision,
311 (cdp->cap.write_dvdr) ? "DVD-R" :
312 (cdp->cap.write_dvdram) ? "DVD-RAM" :
313 (cdp->cap.write_cdrw) ? "CD-RW" :
314 (cdp->cap.write_cdr) ? "CD-R" :
315 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM",
316 device_get_unit(cdp->device->channel->dev),
317 (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
319 ata_prtdev(cdp->device, "%s", "");
320 if (cdp->cap.cur_read_speed) {
321 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
322 if (cdp->cap.max_read_speed)
323 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
324 if ((cdp->cap.cur_write_speed) &&
325 (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
326 cdp->cap.write_dvdr || cdp->cap.write_dvdram)) {
327 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
328 if (cdp->cap.max_write_speed)
329 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
333 if (cdp->cap.buf_size) {
334 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
337 printf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
339 ata_prtdev(cdp->device, "Reads:");
341 if (cdp->cap.read_cdr) {
342 printf(" CD-R"); comma = 1;
344 if (cdp->cap.read_cdrw) {
345 printf("%s CD-RW", comma ? "," : ""); comma = 1;
347 if (cdp->cap.cd_da) {
348 if (cdp->cap.cd_da_stream)
349 printf("%s CD-DA stream", comma ? "," : "");
351 printf("%s CD-DA", comma ? "," : "");
354 if (cdp->cap.read_dvdrom) {
355 printf("%s DVD-ROM", comma ? "," : ""); comma = 1;
357 if (cdp->cap.read_dvdr) {
358 printf("%s DVD-R", comma ? "," : ""); comma = 1;
360 if (cdp->cap.read_dvdram) {
361 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
363 if (cdp->cap.read_packet)
364 printf("%s packet", comma ? "," : "");
367 ata_prtdev(cdp->device, "Writes:");
368 if (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
369 cdp->cap.write_dvdr || cdp->cap.write_dvdram) {
371 if (cdp->cap.write_cdr) {
372 printf(" CD-R" ); comma = 1;
374 if (cdp->cap.write_cdrw) {
375 printf("%s CD-RW", comma ? "," : ""); comma = 1;
377 if (cdp->cap.write_dvdr) {
378 printf("%s DVD-R", comma ? "," : ""); comma = 1;
380 if (cdp->cap.write_dvdram) {
381 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
383 if (cdp->cap.test_write) {
384 printf("%s test write", comma ? "," : ""); comma = 1;
386 if (cdp->cap.burnproof)
387 printf("%s burnproof", comma ? "," : "");
390 if (cdp->cap.audio_play) {
391 ata_prtdev(cdp->device, "Audio: ");
392 if (cdp->cap.audio_play)
394 if (cdp->cap.max_vol_levels)
395 printf(", %d volume levels", cdp->cap.max_vol_levels);
398 ata_prtdev(cdp->device, "Mechanism: ");
399 switch (cdp->cap.mech) {
401 mechanism = "caddy"; break;
403 mechanism = "tray"; break;
405 mechanism = "popup"; break;
406 case MST_MECH_CHANGER:
407 mechanism = "changer"; break;
408 case MST_MECH_CARTRIDGE:
409 mechanism = "cartridge"; break;
411 mechanism = 0; break;
414 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
415 else if (cdp->cap.eject)
419 printf(cdp->cap.locked ? ", locked" : ", unlocked");
420 if (cdp->cap.prevent)
421 printf(", lock protected");
424 if (cdp->cap.mech != MST_MECH_CHANGER) {
425 ata_prtdev(cdp->device, "Medium: ");
426 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
428 printf("CD-ROM "); break;
430 printf("CD-R "); break;
432 printf("CD-RW "); break;
434 printf("door open"); break;
436 printf("no/blank disc"); break;
438 printf("medium format error"); break;
440 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
441 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
443 printf("120mm data disc"); break;
445 printf("120mm audio disc"); break;
447 printf("120mm data/audio disc"); break;
449 printf("120mm photo disc"); break;
451 printf("80mm data disc"); break;
453 printf("80mm audio disc"); break;
455 printf("80mm data/audio disc"); break;
457 printf("80mm photo disc"); break;
459 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
461 printf("unknown"); break;
464 printf("blank"); break;
468 printf("unknown (0x%x)", cdp->cap.medium_type); break;
475 ata_prtdev(cdp->device, "%s ",
476 (cdp->cap.write_dvdr) ? "DVD-R" :
477 (cdp->cap.write_dvdram) ? "DVD-RAM" :
478 (cdp->cap.write_cdrw) ? "CD-RW" :
479 (cdp->cap.write_cdr) ? "CD-R" :
480 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM");
482 if (cdp->changer_info)
483 printf("with %d CD changer ", cdp->changer_info->slots);
485 printf("<%.40s> at ata%d-%s %s\n", cdp->device->param->model,
486 device_get_unit(cdp->device->channel->dev),
487 (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
488 ata_mode2str(cdp->device->mode) );
493 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
497 *m = lba / (60 * 75);
503 static __inline u_int32_t
504 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
506 return (m * 60 + s) * 75 + f - 150;
510 acdopen(dev_t dev, int flags, int fmt, struct thread *td)
512 struct acd_softc *cdp = dev->si_drv1;
518 if (flags & FWRITE) {
519 if (count_dev(dev) > 1)
523 /* wait if drive is not finished loading the medium */
525 struct atapi_reqsense *sense = cdp->device->result;
527 if (!atapi_test_ready(cdp->device))
529 if (sense->sense_key == 2 && sense->asc == 4 && sense->ascq == 1)
530 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
535 if (count_dev(dev) == 1) {
536 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
537 acd_select_slot(cdp);
538 tsleep(&cdp->changer_info, PRIBIO, "acdopn", 0);
540 acd_prevent_allow(cdp, 1);
541 cdp->flags |= F_LOCKED;
548 acdclose(dev_t dev, int flags, int fmt, struct thread *td)
550 struct acd_softc *cdp = dev->si_drv1;
555 if (count_dev(dev) == 1) {
556 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
557 acd_select_slot(cdp);
558 tsleep(&cdp->changer_info, PRIBIO, "acdclo", 0);
560 acd_prevent_allow(cdp, 0);
561 cdp->flags &= ~F_LOCKED;
567 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
569 struct acd_softc *cdp = dev->si_drv1;
575 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
576 acd_select_slot(cdp);
577 tsleep(&cdp->changer_info, PRIBIO, "acdctl", 0);
579 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
582 atapi_test_ready(cdp->device);
587 acd_prevent_allow(cdp, 1);
588 cdp->flags |= F_LOCKED;
594 error = acd_pause_resume(cdp, 1);
598 error = acd_pause_resume(cdp, 0);
602 error = acd_start_stop(cdp, 1);
606 error = acd_start_stop(cdp, 0);
610 error = acd_prevent_allow(cdp, 0);
611 cdp->flags &= ~F_LOCKED;
615 error = acd_prevent_allow(cdp, 1);
616 cdp->flags |= F_LOCKED;
623 error = atapi_test_ready(cdp->device);
627 if (count_dev(dev) > 1) {
631 error = acd_eject(cdp, 0);
635 if (count_dev(dev) > 1)
637 error = acd_eject(cdp, 1);
640 case CDIOREADTOCHEADER:
641 if (!cdp->toc.hdr.ending_track) {
645 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
648 case CDIOREADTOCENTRYS:
650 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
651 struct toc *toc = &cdp->toc;
652 int starting_track = te->starting_track;
655 if (!toc->hdr.ending_track) {
660 if (te->data_len < sizeof(toc->tab[0]) ||
661 (te->data_len % sizeof(toc->tab[0])) != 0 ||
662 (te->address_format != CD_MSF_FORMAT &&
663 te->address_format != CD_LBA_FORMAT)) {
669 starting_track = toc->hdr.starting_track;
670 else if (starting_track == 170)
671 starting_track = toc->hdr.ending_track + 1;
672 else if (starting_track < toc->hdr.starting_track ||
673 starting_track > toc->hdr.ending_track + 1) {
678 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
680 if (te->data_len < len)
682 if (len > sizeof(toc->tab)) {
687 if (te->address_format == CD_MSF_FORMAT) {
688 struct cd_toc_entry *entry;
690 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
691 bcopy(&cdp->toc, toc, sizeof(struct toc));
692 entry = toc->tab + (toc->hdr.ending_track + 1 -
693 toc->hdr.starting_track) + 1;
694 while (--entry >= toc->tab)
695 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
696 &entry->addr.msf.second, &entry->addr.msf.frame);
698 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
700 if (te->address_format == CD_MSF_FORMAT)
704 case CDIOREADTOCENTRY:
706 struct ioc_read_toc_single_entry *te =
707 (struct ioc_read_toc_single_entry *)addr;
708 struct toc *toc = &cdp->toc;
709 u_char track = te->track;
711 if (!toc->hdr.ending_track) {
716 if (te->address_format != CD_MSF_FORMAT &&
717 te->address_format != CD_LBA_FORMAT) {
723 track = toc->hdr.starting_track;
724 else if (track == 170)
725 track = toc->hdr.ending_track + 1;
726 else if (track < toc->hdr.starting_track ||
727 track > toc->hdr.ending_track + 1) {
732 if (te->address_format == CD_MSF_FORMAT) {
733 struct cd_toc_entry *entry;
735 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
736 bcopy(&cdp->toc, toc, sizeof(struct toc));
738 entry = toc->tab + (track - toc->hdr.starting_track);
739 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
740 &entry->addr.msf.second, &entry->addr.msf.frame);
742 bcopy(toc->tab + track - toc->hdr.starting_track,
743 &te->entry, sizeof(struct cd_toc_entry));
744 if (te->address_format == CD_MSF_FORMAT)
749 case CDIOCREADSUBCHANNEL:
751 struct ioc_read_subchannel *args =
752 (struct ioc_read_subchannel *)addr;
754 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
755 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
756 0, 0, 0, 0, 0, 0, 0 };
758 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
759 args->data_len < sizeof(struct cd_sub_channel_header)) {
764 format=args->data_format;
765 if ((format != CD_CURRENT_POSITION) &&
766 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
771 ccb[1] = args->address_format & CD_MSF_FORMAT;
773 if ((error = atapi_queue_cmd(cdp->device,ccb,(caddr_t)&cdp->subchan,
774 sizeof(cdp->subchan), ATPR_F_READ, 10,
778 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
779 if (cdp->subchan.header.audio_status == 0x11) {
785 if (format == CD_TRACK_INFO)
786 ccb[6] = args->track;
788 if ((error = atapi_queue_cmd(cdp->device, ccb,
789 (caddr_t)&cdp->subchan,
790 sizeof(cdp->subchan), ATPR_F_READ,
795 error = copyout(&cdp->subchan, args->data, args->data_len);
801 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
805 msf2lba(args->start_m, args->start_s, args->start_f),
806 msf2lba(args->end_m, args->end_s, args->end_f));
810 case CDIOCPLAYBLOCKS:
812 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
814 error = acd_play(cdp, args->blk, args->blk + args->len);
818 case CDIOCPLAYTRACKS:
820 struct ioc_play_track *args = (struct ioc_play_track *)addr;
823 if (!cdp->toc.hdr.ending_track) {
827 if (args->end_track < cdp->toc.hdr.ending_track + 1)
829 if (args->end_track > cdp->toc.hdr.ending_track + 1)
830 args->end_track = cdp->toc.hdr.ending_track + 1;
831 t1 = args->start_track - cdp->toc.hdr.starting_track;
832 t2 = args->end_track - cdp->toc.hdr.starting_track;
833 if (t1 < 0 || t2 < 0 ||
834 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
838 error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
839 ntohl(cdp->toc.tab[t2].addr.lba));
845 struct ioc_read_audio *args = (struct ioc_read_audio *)addr;
847 caddr_t buffer, ubuf = args->buffer;
851 if (!cdp->toc.hdr.ending_track) {
856 if ((frames = args->nframes) < 0) {
861 if (args->address_format == CD_LBA_FORMAT)
862 lba = args->address.lba;
863 else if (args->address_format == CD_MSF_FORMAT)
864 lba = msf2lba(args->address.msf.minute,
865 args->address.msf.second,
866 args->address.msf.frame);
872 #ifndef CD_BUFFER_BLOCKS
873 #define CD_BUFFER_BLOCKS 13
875 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_NOWAIT))){
879 bzero(ccb, sizeof(ccb));
884 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
885 size = blocks * 2352;
887 ccb[0] = ATAPI_READ_CD;
895 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size,
896 ATPR_F_READ, 30, NULL,NULL)))
899 if ((error = copyout(buffer, ubuf, size)))
907 if (args->address_format == CD_LBA_FORMAT)
908 args->address.lba = lba;
909 else if (args->address_format == CD_MSF_FORMAT)
910 lba2msf(lba, &args->address.msf.minute,
911 &args->address.msf.second,
912 &args->address.msf.frame);
918 struct ioc_vol *arg = (struct ioc_vol *)addr;
920 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
921 (caddr_t)&cdp->au, sizeof(cdp->au))))
924 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
928 arg->vol[0] = cdp->au.port[0].volume;
929 arg->vol[1] = cdp->au.port[1].volume;
930 arg->vol[2] = cdp->au.port[2].volume;
931 arg->vol[3] = cdp->au.port[3].volume;
937 struct ioc_vol *arg = (struct ioc_vol *)addr;
939 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
940 (caddr_t)&cdp->au, sizeof(cdp->au))))
942 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
946 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
947 (caddr_t)&cdp->aumask,
948 sizeof(cdp->aumask))))
950 cdp->au.data_length = 0;
951 cdp->au.port[0].channels = CHANNEL_0;
952 cdp->au.port[1].channels = CHANNEL_1;
953 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
954 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
955 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
956 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
957 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
962 struct ioc_patch *arg = (struct ioc_patch *)addr;
964 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
965 arg->patch[2], arg->patch[3]);
970 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
974 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
978 error = acd_setchan(cdp, 0, 0, 0, 0);
982 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
986 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
990 error = acd_blank(cdp, (*(int *)addr));
993 case CDRIOCNEXTWRITEABLEADDR:
995 struct acd_track_info track_info;
997 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
1000 if (!track_info.nwa_valid) {
1004 *(int*)addr = track_info.next_writeable_addr;
1008 case CDRIOCINITWRITER:
1009 error = acd_init_writer(cdp, (*(int *)addr));
1012 case CDRIOCINITTRACK:
1013 error = acd_init_track(cdp, (struct cdr_track *)addr);
1017 error = acd_flush(cdp);
1021 error = acd_fixate(cdp, (*(int *)addr));
1024 case CDRIOCREADSPEED:
1026 int speed = *(int *)addr;
1028 /* Preserve old behavior: units in multiples of CDROM speed */
1031 error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
1035 case CDRIOCWRITESPEED:
1037 int speed = *(int *)addr;
1041 error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
1045 case CDRIOCGETBLOCKSIZE:
1046 *(int *)addr = cdp->block_size;
1049 case CDRIOCSETBLOCKSIZE:
1050 cdp->block_size = *(int *)addr;
1051 acd_set_ioparm(cdp);
1054 case CDRIOCGETPROGRESS:
1055 error = acd_get_progress(cdp, (int *)addr);
1059 error = acd_send_cue(cdp, (struct cdr_cuesheet *)addr);
1062 case CDRIOCREADFORMATCAPS:
1063 error = acd_read_format_caps(cdp, (struct cdr_format_capacities *)addr);
1067 error = acd_format(cdp, (struct cdr_format_params *)addr);
1070 case DVDIOCREPORTKEY:
1071 if (!cdp->cap.read_dvdrom)
1074 error = acd_report_key(cdp, (struct dvd_authinfo *)addr);
1078 if (!cdp->cap.read_dvdrom)
1081 error = acd_send_key(cdp, (struct dvd_authinfo *)addr);
1084 case DVDIOCREADSTRUCTURE:
1085 if (!cdp->cap.read_dvdrom)
1088 error = acd_read_structure(cdp, (struct dvd_struct *)addr);
1098 acdstrategy(struct bio *bp)
1100 struct acd_softc *cdp = bp->bio_dev->si_drv1;
1103 if (cdp->device->flags & ATA_D_DETACHING) {
1104 biofinish(bp, NULL, ENXIO);
1108 /* if it's a null transfer, return immediatly. */
1109 if (bp->bio_bcount == 0) {
1115 bp->bio_pblkno = bp->bio_blkno;
1116 bp->bio_resid = bp->bio_bcount;
1119 bioqdisksort(&cdp->queue, bp);
1121 ata_start(cdp->device->channel);
1125 acd_start(struct ata_device *atadev)
1127 struct acd_softc *cdp = atadev->driver;
1128 struct bio *bp = bioq_first(&cdp->queue);
1129 u_int32_t lba, lastlba, count;
1131 int track, blocksize;
1133 if (cdp->changer_info) {
1136 cdp = cdp->driver[cdp->changer_info->current_slot];
1137 bp = bioq_first(&cdp->queue);
1139 /* check for work pending on any other slot */
1140 for (i = 0; i < cdp->changer_info->slots; i++) {
1141 if (i == cdp->changer_info->current_slot)
1143 if (bioq_first(&(cdp->driver[i]->queue))) {
1144 if (!bp || time_second > (cdp->timestamp + 10)) {
1145 acd_select_slot(cdp->driver[i]);
1153 bioq_remove(&cdp->queue, bp);
1155 /* reject all queued entries if media changed */
1156 if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1157 biofinish(bp, NULL, EIO);
1161 bzero(ccb, sizeof(ccb));
1163 track = (bp->bio_dev->si_udev & 0x00ff0000) >> 16;
1166 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1167 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1168 lba = bp->bio_offset / blocksize;
1169 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1172 blocksize = cdp->block_size;
1173 lastlba = cdp->disk_size;
1174 lba = bp->bio_offset / blocksize;
1177 if (bp->bio_bcount % blocksize != 0) {
1178 biofinish(bp, NULL, EINVAL);
1181 count = bp->bio_bcount / blocksize;
1183 if (bp->bio_cmd == BIO_READ) {
1184 /* if transfer goes beyond range adjust it to be within limits */
1185 if (lba + count > lastlba) {
1186 /* if we are entirely beyond EOM return EOF */
1187 if (lastlba <= lba) {
1188 bp->bio_resid = bp->bio_bcount;
1192 count = lastlba - lba;
1194 switch (blocksize) {
1196 ccb[0] = ATAPI_READ_BIG;
1200 ccb[0] = ATAPI_READ_CD;
1205 ccb[0] = ATAPI_READ_CD;
1210 ccb[0] = ATAPI_WRITE_BIG;
1221 devstat_start_transaction(cdp->stats);
1222 bp->bio_caller1 = cdp;
1223 atapi_queue_cmd(cdp->device, ccb, bp->bio_data, count * blocksize,
1224 bp->bio_cmd == BIO_READ ? ATPR_F_READ : 0,
1225 (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bp);
1229 acd_done(struct atapi_request *request)
1231 struct bio *bp = request->driver;
1232 struct acd_softc *cdp = bp->bio_caller1;
1234 if (request->error) {
1235 bp->bio_error = request->error;
1236 bp->bio_flags |= BIO_ERROR;
1239 bp->bio_resid = bp->bio_bcount - request->donecount;
1240 biofinish(bp, cdp->stats, 0);
1245 acd_read_toc(struct acd_softc *cdp)
1247 struct acd_devlist *entry;
1248 int track, ntracks, len;
1252 bzero(&cdp->toc, sizeof(cdp->toc));
1253 bzero(ccb, sizeof(ccb));
1255 if (atapi_test_ready(cdp->device) != 0)
1258 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1260 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1261 ccb[0] = ATAPI_READ_TOC;
1264 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1265 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1266 bzero(&cdp->toc, sizeof(cdp->toc));
1269 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1270 if (ntracks <= 0 || ntracks > MAXTRK) {
1271 bzero(&cdp->toc, sizeof(cdp->toc));
1275 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1276 bzero(ccb, sizeof(ccb));
1277 ccb[0] = ATAPI_READ_TOC;
1280 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1281 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1282 bzero(&cdp->toc, sizeof(cdp->toc));
1285 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1287 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1288 acd_set_ioparm(cdp);
1289 bzero(ccb, sizeof(ccb));
1290 ccb[0] = ATAPI_READ_CAPACITY;
1291 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1292 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1293 bzero(&cdp->toc, sizeof(cdp->toc));
1296 cdp->disk_size = ntohl(sizes[0]) + 1;
1298 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1299 destroy_dev(entry->dev);
1300 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1303 for (track = 1; track <= ntracks; track ++) {
1306 sprintf(name, "acd%dt%02d", cdp->lun, track);
1307 entry = malloc(sizeof(struct acd_devlist), M_ACD, M_NOWAIT | M_ZERO);
1308 entry->dev = make_dev(&acd_cdevsw, (cdp->lun << 3) | (track << 16),
1309 0, 0, 0644, name, NULL);
1310 entry->dev->si_drv1 = cdp->dev->si_drv1;
1311 TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1315 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1316 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1317 cdp->disk_size, cdp->block_size,
1318 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1319 if (cdp->toc.tab[0].control & 4)
1320 printf("%dMB\n", cdp->disk_size / 512);
1322 printf("%d:%d audio\n",
1323 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1329 acd_play(struct acd_softc *cdp, int start, int end)
1333 bzero(ccb, sizeof(ccb));
1334 ccb[0] = ATAPI_PLAY_MSF;
1335 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1336 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1337 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1341 acd_setchan(struct acd_softc *cdp,
1342 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1346 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1349 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1351 cdp->au.data_length = 0;
1352 cdp->au.port[0].channels = c0;
1353 cdp->au.port[1].channels = c1;
1354 cdp->au.port[2].channels = c2;
1355 cdp->au.port[3].channels = c3;
1356 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1360 acd_select_done1(struct atapi_request *request)
1362 struct acd_softc *cdp = request->driver;
1364 cdp->changer_info->current_slot = cdp->slot;
1365 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1366 wakeup(&cdp->changer_info);
1371 acd_select_done(struct atapi_request *request)
1373 struct acd_softc *cdp = request->driver;
1374 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1375 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1377 /* load the wanted slot */
1378 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1379 acd_select_done1, cdp);
1384 acd_select_slot(struct acd_softc *cdp)
1386 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1387 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1389 /* unload the current media from player */
1390 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1391 acd_select_done, cdp);
1395 acd_init_writer(struct acd_softc *cdp, int test_write)
1399 bzero(ccb, sizeof(ccb));
1400 ccb[0] = ATAPI_REZERO;
1401 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1402 ccb[0] = ATAPI_SEND_OPC_INFO;
1404 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1409 acd_fixate(struct acd_softc *cdp, int multisession)
1411 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1412 0, 0, 0, 0, 0, 0, 0, 0 };
1413 int timeout = 5*60*2;
1415 struct write_param param;
1417 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1418 (caddr_t)¶m, sizeof(param))))
1421 param.data_length = 0;
1423 param.session_type = CDR_SESS_MULTI;
1425 param.session_type = CDR_SESS_NONE;
1427 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1430 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1434 /* some drives just return ready, wait for the expected fixate time */
1435 if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1436 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1437 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1438 return atapi_test_ready(cdp->device);
1441 while (timeout-- > 0) {
1442 if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1444 tsleep(&error, PRIBIO, "acdcld", hz/2);
1450 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1452 struct write_param param;
1455 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1456 (caddr_t)¶m, sizeof(param))))
1459 param.data_length = 0;
1460 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1461 param.page_length = 0x32;
1462 param.test_write = track->test_write ? 1 : 0;
1463 param.write_type = CDR_WTYPE_TRACK;
1464 param.session_type = CDR_SESS_NONE;
1466 param.packet_size = 0;
1468 if (cdp->cap.burnproof)
1469 param.burnproof = 1;
1471 switch (track->datablock_type) {
1475 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1477 param.track_mode = CDR_TMODE_AUDIO;
1478 cdp->block_size = 2352;
1479 param.datablock_type = CDR_DB_RAW;
1480 param.session_format = CDR_SESS_CDROM;
1483 case CDR_DB_ROM_MODE1:
1484 cdp->block_size = 2048;
1485 param.track_mode = CDR_TMODE_DATA;
1486 param.datablock_type = CDR_DB_ROM_MODE1;
1487 param.session_format = CDR_SESS_CDROM;
1490 case CDR_DB_ROM_MODE2:
1491 cdp->block_size = 2336;
1492 param.track_mode = CDR_TMODE_DATA;
1493 param.datablock_type = CDR_DB_ROM_MODE2;
1494 param.session_format = CDR_SESS_CDROM;
1497 case CDR_DB_XA_MODE1:
1498 cdp->block_size = 2048;
1499 param.track_mode = CDR_TMODE_DATA;
1500 param.datablock_type = CDR_DB_XA_MODE1;
1501 param.session_format = CDR_SESS_CDROM_XA;
1504 case CDR_DB_XA_MODE2_F1:
1505 cdp->block_size = 2056;
1506 param.track_mode = CDR_TMODE_DATA;
1507 param.datablock_type = CDR_DB_XA_MODE2_F1;
1508 param.session_format = CDR_SESS_CDROM_XA;
1511 case CDR_DB_XA_MODE2_F2:
1512 cdp->block_size = 2324;
1513 param.track_mode = CDR_TMODE_DATA;
1514 param.datablock_type = CDR_DB_XA_MODE2_F2;
1515 param.session_format = CDR_SESS_CDROM_XA;
1518 case CDR_DB_XA_MODE2_MIX:
1519 cdp->block_size = 2332;
1520 param.track_mode = CDR_TMODE_DATA;
1521 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1522 param.session_format = CDR_SESS_CDROM_XA;
1525 acd_set_ioparm(cdp);
1526 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1530 acd_flush(struct acd_softc *cdp)
1532 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1533 0, 0, 0, 0, 0, 0, 0, 0 };
1535 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1540 acd_read_track_info(struct acd_softc *cdp,
1541 int32_t lba, struct acd_track_info *info)
1543 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1544 lba>>24, lba>>16, lba>>8, lba,
1546 sizeof(*info)>>8, sizeof(*info),
1547 0, 0, 0, 0, 0, 0, 0 };
1550 if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1551 ATPR_F_READ, 30, NULL, NULL)))
1553 info->track_start_addr = ntohl(info->track_start_addr);
1554 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1555 info->free_blocks = ntohl(info->free_blocks);
1556 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1557 info->track_length = ntohl(info->track_length);
1562 acd_get_progress(struct acd_softc *cdp, int *finished)
1564 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1565 0, 0, 0, 0, 0, 0, 0, 0 };
1566 struct atapi_reqsense *sense = cdp->device->result;
1569 if (atapi_queue_cmd(cdp->device, ccb, dummy, sizeof(dummy),
1570 ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1575 *finished = ((sense->sk_specific2|(sense->sk_specific1<<8))*100)/65535;
1582 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1584 struct write_param param;
1585 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1586 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1587 0, 0, 0, 0, 0, 0, 0 };
1594 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1595 (caddr_t)¶m, sizeof(param))))
1598 param.data_length = 0;
1599 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1600 param.page_length = 0x32;
1601 param.test_write = cuesheet->test_write ? 1 : 0;
1602 param.write_type = CDR_WTYPE_SESSION;
1603 param.session_type = cuesheet->session_type;
1605 param.packet_size = 0;
1606 param.track_mode = CDR_TMODE_AUDIO;
1607 param.datablock_type = CDR_DB_RAW;
1608 param.session_format = cuesheet->session_format;
1609 if (cdp->cap.burnproof)
1610 param.burnproof = 1;
1612 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1615 if (!(buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT)))
1618 if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len))) {
1620 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1621 for (i=0; i<cuesheet->len; i++)
1623 printf(" %02x", buffer[i]);
1625 printf("\n%02x", buffer[i]);
1628 error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1631 free(buffer, M_ACD);
1636 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1638 struct dvd_miscauth *d;
1644 switch (ai->format) {
1645 case DVD_REPORT_AGID:
1646 case DVD_REPORT_ASF:
1647 case DVD_REPORT_RPC:
1650 case DVD_REPORT_KEY1:
1653 case DVD_REPORT_TITLE_KEY:
1657 case DVD_REPORT_CHALLENGE:
1660 case DVD_INVALIDATE_AGID:
1667 bzero(ccb, sizeof(ccb));
1668 ccb[0] = ATAPI_REPORT_KEY;
1669 ccb[2] = (lba >> 24) & 0xff;
1670 ccb[3] = (lba >> 16) & 0xff;
1671 ccb[4] = (lba >> 8) & 0xff;
1672 ccb[5] = lba & 0xff;
1673 ccb[8] = (length >> 8) & 0xff;
1674 ccb[9] = length & 0xff;
1675 ccb[10] = (ai->agid << 6) | ai->format;
1677 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1678 d->length = htons(length - 2);
1680 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1681 ai->format == DVD_INVALIDATE_AGID ? 0 : ATPR_F_READ,
1688 switch (ai->format) {
1689 case DVD_REPORT_AGID:
1690 ai->agid = d->data[3] >> 6;
1693 case DVD_REPORT_CHALLENGE:
1694 bcopy(&d->data[0], &ai->keychal[0], 10);
1697 case DVD_REPORT_KEY1:
1698 bcopy(&d->data[0], &ai->keychal[0], 5);
1701 case DVD_REPORT_TITLE_KEY:
1702 ai->cpm = (d->data[0] >> 7);
1703 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1704 ai->cgms = (d->data[0] >> 4) & 0x3;
1705 bcopy(&d->data[1], &ai->keychal[0], 5);
1708 case DVD_REPORT_ASF:
1709 ai->asf = d->data[3] & 1;
1712 case DVD_REPORT_RPC:
1713 ai->reg_type = (d->data[0] >> 6);
1714 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1715 ai->user_rsts = d->data[0] & 0x7;
1716 ai->region = d->data[1];
1717 ai->rpc_scheme = d->data[2];
1720 case DVD_INVALIDATE_AGID:
1731 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1733 struct dvd_miscauth *d;
1738 switch (ai->format) {
1739 case DVD_SEND_CHALLENGE:
1741 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1742 bcopy(ai->keychal, &d->data[0], 10);
1747 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1748 bcopy(&ai->keychal[0], &d->data[0], 5);
1753 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1754 d->data[0] = ai->region;
1761 bzero(ccb, sizeof(ccb));
1762 ccb[0] = ATAPI_SEND_KEY;
1763 ccb[8] = (length >> 8) & 0xff;
1764 ccb[9] = length & 0xff;
1765 ccb[10] = (ai->agid << 6) | ai->format;
1766 d->length = htons(length - 2);
1767 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1774 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1776 struct dvd_miscauth *d;
1782 case DVD_STRUCT_PHYSICAL:
1786 case DVD_STRUCT_COPYRIGHT:
1790 case DVD_STRUCT_DISCKEY:
1794 case DVD_STRUCT_BCA:
1798 case DVD_STRUCT_MANUFACT:
1802 case DVD_STRUCT_DDS:
1803 case DVD_STRUCT_PRERECORDED:
1804 case DVD_STRUCT_UNIQUEID:
1805 case DVD_STRUCT_LIST:
1806 case DVD_STRUCT_CMI:
1807 case DVD_STRUCT_RMD_LAST:
1808 case DVD_STRUCT_RMD_RMA:
1809 case DVD_STRUCT_DCB:
1816 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1817 d->length = htons(length - 2);
1819 bzero(ccb, sizeof(ccb));
1820 ccb[0] = ATAPI_READ_STRUCTURE;
1821 ccb[6] = s->layer_num;
1823 ccb[8] = (length >> 8) & 0xff;
1824 ccb[9] = length & 0xff;
1825 ccb[10] = s->agid << 6;
1826 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1833 switch (s->format) {
1834 case DVD_STRUCT_PHYSICAL: {
1835 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1837 layer->book_type = d->data[0] >> 4;
1838 layer->book_version = d->data[0] & 0xf;
1839 layer->disc_size = d->data[1] >> 4;
1840 layer->max_rate = d->data[1] & 0xf;
1841 layer->nlayers = (d->data[2] >> 5) & 3;
1842 layer->track_path = (d->data[2] >> 4) & 1;
1843 layer->layer_type = d->data[2] & 0xf;
1844 layer->linear_density = d->data[3] >> 4;
1845 layer->track_density = d->data[3] & 0xf;
1846 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1847 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1848 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1849 layer->bca = d->data[16] >> 7;
1853 case DVD_STRUCT_COPYRIGHT:
1854 s->cpst = d->data[0];
1855 s->rmi = d->data[0];
1858 case DVD_STRUCT_DISCKEY:
1859 bcopy(&d->data[0], &s->data[0], 2048);
1862 case DVD_STRUCT_BCA:
1863 s->length = ntohs(d->length);
1864 bcopy(&d->data[0], &s->data[0], s->length);
1867 case DVD_STRUCT_MANUFACT:
1868 s->length = ntohs(d->length);
1869 bcopy(&d->data[0], &s->data[0], s->length);
1880 acd_eject(struct acd_softc *cdp, int close)
1884 if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1887 if ((error = acd_start_stop(cdp, 3)))
1890 acd_prevent_allow(cdp, 1);
1891 cdp->flags |= F_LOCKED;
1898 acd_prevent_allow(cdp, 0);
1899 cdp->flags &= ~F_LOCKED;
1900 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1901 return acd_start_stop(cdp, 2);
1905 acd_blank(struct acd_softc *cdp, int blanktype)
1907 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1908 0, 0, 0, 0, 0, 0, 0, 0 };
1910 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1911 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1915 acd_prevent_allow(struct acd_softc *cdp, int lock)
1917 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1918 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1920 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1924 acd_start_stop(struct acd_softc *cdp, int start)
1926 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1927 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1929 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1933 acd_pause_resume(struct acd_softc *cdp, int pause)
1935 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1936 0, 0, 0, 0, 0, 0, 0 };
1938 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1942 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1944 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1945 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1948 error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1951 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1957 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1959 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1960 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1963 ata_prtdev(cdp->device,
1964 "modeselect pagesize=%d\n", pagesize);
1965 atapi_dump("mode select ", pagebuf, pagesize);
1967 return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
1972 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
1974 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1975 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1978 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1985 acd_get_cap(struct acd_softc *cdp)
1989 /* get drive capabilities, some drives needs this repeated */
1990 while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
1991 (caddr_t)&cdp->cap, sizeof(cdp->cap)))
1993 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1994 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1995 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1996 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
1997 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1998 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
2002 acd_read_format_caps(struct acd_softc *cdp, struct cdr_format_capacities *caps)
2004 int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
2005 (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
2006 sizeof(struct cdr_format_capacities) & 0xff,
2007 0, 0, 0, 0, 0, 0, 0 };
2009 return atapi_queue_cmd(cdp->device, ccb, (caddr_t)caps,
2010 sizeof(struct cdr_format_capacities),
2011 ATPR_F_READ, 30, NULL, NULL);
2015 acd_format(struct acd_softc *cdp, struct cdr_format_params* params)
2018 int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
2021 error = atapi_queue_cmd(cdp->device, ccb, (u_int8_t *)params,
2022 sizeof(struct cdr_format_params), 0, 30, NULL,NULL);