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.
28 * $FreeBSD: src/sys/dev/ata/atapi-cd.c,v 1.48.2.20 2002/11/25 05:30:31 njl Exp $
29 * $DragonFly: src/sys/dev/disk/ata/atapi-cd.c,v 1.2 2003/06/17 04:28:22 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
41 #include <sys/disklabel.h>
42 #include <sys/devicestat.h>
44 #include <sys/cdrio.h>
45 #include <sys/dvdio.h>
46 #include <sys/fcntl.h>
48 #include <sys/ctype.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 *);
107 static u_int32_t acd_lun_map = 0;
108 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
111 acdattach(struct ata_device *atadev)
113 struct acd_softc *cdp;
116 if ((cdp = acd_init_lun(atadev)) == NULL) {
117 ata_prtdev(atadev, "acd: out of memory\n");
121 ata_set_name(atadev, "acd", cdp->lun);
124 /* if this is a changer device, allocate the neeeded lun's */
125 if (cdp->cap.mech == MST_MECH_CHANGER) {
126 int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0,
127 sizeof(struct changer)>>8, sizeof(struct changer),
130 chp = malloc(sizeof(struct changer), M_ACD, M_NOWAIT | M_ZERO);
132 ata_prtdev(atadev, "out of memory\n");
136 if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp,
137 sizeof(struct changer),
138 ATPR_F_READ, 60, NULL, NULL)) {
139 struct acd_softc *tmpcdp = cdp;
140 struct acd_softc **cdparr;
144 chp->table_length = htons(chp->table_length);
145 if (!(cdparr = malloc(sizeof(struct acd_softc) * chp->slots,
147 ata_prtdev(atadev, "out of memory\n");
152 for (count = 0; count < chp->slots; count++) {
154 tmpcdp = acd_init_lun(atadev);
156 ata_prtdev(atadev, "out of memory\n");
160 cdparr[count] = tmpcdp;
161 tmpcdp->driver = cdparr;
162 tmpcdp->slot = count;
163 tmpcdp->changer_info = chp;
164 acd_make_dev(tmpcdp);
165 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
166 DEVSTAT_NO_ORDERED_TAGS,
167 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
168 DEVSTAT_PRIORITY_CD);
170 name = malloc(strlen(atadev->name) + 2, M_ACD, M_NOWAIT);
171 strcpy(name, atadev->name);
173 ata_free_name(atadev);
174 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
180 devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
181 DEVSTAT_NO_ORDERED_TAGS,
182 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
183 DEVSTAT_PRIORITY_CD);
186 atadev->driver = cdp;
191 acddetach(struct ata_device *atadev)
193 struct acd_softc *cdp = atadev->driver;
194 struct acd_devlist *entry;
198 if (cdp->changer_info) {
199 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
200 if (cdp->driver[subdev] == cdp)
202 while ((bp = bufq_first(&cdp->driver[subdev]->queue))) {
203 bufq_remove(&cdp->driver[subdev]->queue, bp);
204 bp->b_flags |= B_ERROR;
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 = bufq_first(&cdp->queue))) {
223 bp->b_flags |= B_ERROR;
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 devstat_remove_entry(cdp->stats);
234 free(cdp->stats, M_ACD);
235 ata_free_name(atadev);
236 ata_free_lun(&acd_lun_map, cdp->lun);
238 atadev->driver = NULL;
241 static struct acd_softc *
242 acd_init_lun(struct ata_device *atadev)
244 struct acd_softc *cdp;
246 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO)))
248 TAILQ_INIT(&cdp->dev_list);
249 bufq_init(&cdp->queue);
250 cdp->device = atadev;
251 cdp->lun = ata_get_lun(&acd_lun_map);
252 cdp->block_size = 2048;
254 cdp->changer_info = NULL;
255 if (!(cdp->stats = malloc(sizeof(struct devstat), M_ACD,
256 M_NOWAIT | M_ZERO))) {
264 acd_make_dev(struct acd_softc *cdp)
268 dev = make_dev(&acd_cdevsw, dkmakeminor(cdp->lun, 0, 0),
269 UID_ROOT, GID_OPERATOR, 0644, "acd%d", cdp->lun);
272 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
277 acd_set_ioparm(struct acd_softc *cdp)
279 cdp->dev->si_iosize_max = ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
280 cdp->dev->si_bsize_phys = cdp->block_size;
284 acd_describe(struct acd_softc *cdp)
290 ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
291 cdp->device->param->model, cdp->device->param->revision,
292 (cdp->cap.write_dvdr) ? "DVD-R" :
293 (cdp->cap.write_dvdram) ? "DVD-RAM" :
294 (cdp->cap.write_cdrw) ? "CD-RW" :
295 (cdp->cap.write_cdr) ? "CD-R" :
296 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM",
297 device_get_unit(cdp->device->channel->dev),
298 (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
300 ata_prtdev(cdp->device, "%s", "");
301 if (cdp->cap.cur_read_speed) {
302 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
303 if (cdp->cap.max_read_speed)
304 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
305 if ((cdp->cap.cur_write_speed) &&
306 (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
307 cdp->cap.write_dvdr || cdp->cap.write_dvdram)) {
308 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
309 if (cdp->cap.max_write_speed)
310 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
314 if (cdp->cap.buf_size) {
315 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
318 printf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
320 ata_prtdev(cdp->device, "Reads:");
322 if (cdp->cap.read_cdr) {
323 printf(" CD-R"); comma = 1;
325 if (cdp->cap.read_cdrw) {
326 printf("%s CD-RW", comma ? "," : ""); comma = 1;
328 if (cdp->cap.cd_da) {
329 if (cdp->cap.cd_da_stream)
330 printf("%s CD-DA stream", comma ? "," : "");
332 printf("%s CD-DA", comma ? "," : "");
335 if (cdp->cap.read_dvdrom) {
336 printf("%s DVD-ROM", comma ? "," : ""); comma = 1;
338 if (cdp->cap.read_dvdr) {
339 printf("%s DVD-R", comma ? "," : ""); comma = 1;
341 if (cdp->cap.read_dvdram) {
342 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
344 if (cdp->cap.read_packet)
345 printf("%s packet", comma ? "," : "");
348 ata_prtdev(cdp->device, "Writes:");
349 if (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
350 cdp->cap.write_dvdr || cdp->cap.write_dvdram) {
352 if (cdp->cap.write_cdr) {
353 printf(" CD-R" ); comma = 1;
355 if (cdp->cap.write_cdrw) {
356 printf("%s CD-RW", comma ? "," : ""); comma = 1;
358 if (cdp->cap.write_dvdr) {
359 printf("%s DVD-R", comma ? "," : ""); comma = 1;
361 if (cdp->cap.write_dvdram) {
362 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
364 if (cdp->cap.test_write) {
365 printf("%s test write", comma ? "," : ""); comma = 1;
367 if (cdp->cap.burnproof)
368 printf("%s burnproof", comma ? "," : "");
371 if (cdp->cap.audio_play) {
372 ata_prtdev(cdp->device, "Audio: ");
373 if (cdp->cap.audio_play)
375 if (cdp->cap.max_vol_levels)
376 printf(", %d volume levels", cdp->cap.max_vol_levels);
379 ata_prtdev(cdp->device, "Mechanism: ");
380 switch (cdp->cap.mech) {
382 mechanism = "caddy"; break;
384 mechanism = "tray"; break;
386 mechanism = "popup"; break;
387 case MST_MECH_CHANGER:
388 mechanism = "changer"; break;
389 case MST_MECH_CARTRIDGE:
390 mechanism = "cartridge"; break;
392 mechanism = 0; break;
395 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
396 else if (cdp->cap.eject)
400 printf(cdp->cap.locked ? ", locked" : ", unlocked");
401 if (cdp->cap.prevent)
402 printf(", lock protected");
405 if (cdp->cap.mech != MST_MECH_CHANGER) {
406 ata_prtdev(cdp->device, "Medium: ");
407 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
409 printf("CD-ROM "); break;
411 printf("CD-R "); break;
413 printf("CD-RW "); break;
415 printf("door open"); break;
417 printf("no/blank disc"); break;
419 printf("medium format error"); break;
421 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
422 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
424 printf("120mm data disc"); break;
426 printf("120mm audio disc"); break;
428 printf("120mm data/audio disc"); break;
430 printf("120mm photo disc"); break;
432 printf("80mm data disc"); break;
434 printf("80mm audio disc"); break;
436 printf("80mm data/audio disc"); break;
438 printf("80mm photo disc"); break;
440 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
442 printf("unknown"); break;
445 printf("blank"); break;
449 printf("unknown (0x%x)", cdp->cap.medium_type); break;
456 ata_prtdev(cdp->device, "%s ",
457 (cdp->cap.write_dvdr) ? "DVD-R" :
458 (cdp->cap.write_dvdram) ? "DVD-RAM" :
459 (cdp->cap.write_cdrw) ? "CD-RW" :
460 (cdp->cap.write_cdr) ? "CD-R" :
461 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM");
463 if (cdp->changer_info)
464 printf("with %d CD changer ", cdp->changer_info->slots);
466 printf("<%.40s> at ata%d-%s %s\n", cdp->device->param->model,
467 device_get_unit(cdp->device->channel->dev),
468 (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
469 ata_mode2str(cdp->device->mode) );
474 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
478 *m = lba / (60 * 75);
484 static __inline u_int32_t
485 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
487 return (m * 60 + s) * 75 + f - 150;
491 acdopen(dev_t dev, int flags, int fmt, struct proc *p)
493 struct acd_softc *cdp = dev->si_drv1;
499 if (flags & FWRITE) {
500 if (count_dev(dev) > 1)
504 /* wait if drive is not finished loading the medium */
506 struct atapi_reqsense *sense = cdp->device->result;
508 if (!atapi_test_ready(cdp->device))
510 if (sense->sense_key == 2 && sense->asc == 4 && sense->ascq == 1)
511 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
516 if (count_dev(dev) == 1) {
517 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
518 acd_select_slot(cdp);
519 tsleep(&cdp->changer_info, PRIBIO, "acdopn", 0);
521 acd_prevent_allow(cdp, 1);
522 cdp->flags |= F_LOCKED;
529 acdclose(dev_t dev, int flags, int fmt, struct proc *p)
531 struct acd_softc *cdp = dev->si_drv1;
536 if (count_dev(dev) == 1) {
537 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
538 acd_select_slot(cdp);
539 tsleep(&cdp->changer_info, PRIBIO, "acdclo", 0);
541 acd_prevent_allow(cdp, 0);
542 cdp->flags &= ~F_LOCKED;
548 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
550 struct acd_softc *cdp = dev->si_drv1;
556 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
557 acd_select_slot(cdp);
558 tsleep(&cdp->changer_info, PRIBIO, "acdctl", 0);
560 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
563 atapi_test_ready(cdp->device);
568 acd_prevent_allow(cdp, 1);
569 cdp->flags |= F_LOCKED;
575 error = acd_pause_resume(cdp, 1);
579 error = acd_pause_resume(cdp, 0);
583 error = acd_start_stop(cdp, 1);
587 error = acd_start_stop(cdp, 0);
591 error = acd_prevent_allow(cdp, 0);
592 cdp->flags &= ~F_LOCKED;
596 error = acd_prevent_allow(cdp, 1);
597 cdp->flags |= F_LOCKED;
604 error = atapi_test_ready(cdp->device);
608 if (count_dev(dev) > 1) {
612 error = acd_eject(cdp, 0);
616 if (count_dev(dev) > 1)
618 error = acd_eject(cdp, 1);
621 case CDIOREADTOCHEADER:
622 if (!cdp->toc.hdr.ending_track) {
626 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
629 case CDIOREADTOCENTRYS:
631 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
632 struct toc *toc = &cdp->toc;
633 int starting_track = te->starting_track;
636 if (!toc->hdr.ending_track) {
641 if (te->data_len < sizeof(toc->tab[0]) ||
642 (te->data_len % sizeof(toc->tab[0])) != 0 ||
643 (te->address_format != CD_MSF_FORMAT &&
644 te->address_format != CD_LBA_FORMAT)) {
650 starting_track = toc->hdr.starting_track;
651 else if (starting_track == 170)
652 starting_track = toc->hdr.ending_track + 1;
653 else if (starting_track < toc->hdr.starting_track ||
654 starting_track > toc->hdr.ending_track + 1) {
659 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
661 if (te->data_len < len)
663 if (len > sizeof(toc->tab)) {
668 if (te->address_format == CD_MSF_FORMAT) {
669 struct cd_toc_entry *entry;
671 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
672 bcopy(&cdp->toc, toc, sizeof(struct toc));
673 entry = toc->tab + (toc->hdr.ending_track + 1 -
674 toc->hdr.starting_track) + 1;
675 while (--entry >= toc->tab)
676 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
677 &entry->addr.msf.second, &entry->addr.msf.frame);
679 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
681 if (te->address_format == CD_MSF_FORMAT)
685 case CDIOREADTOCENTRY:
687 struct ioc_read_toc_single_entry *te =
688 (struct ioc_read_toc_single_entry *)addr;
689 struct toc *toc = &cdp->toc;
690 u_char track = te->track;
692 if (!toc->hdr.ending_track) {
697 if (te->address_format != CD_MSF_FORMAT &&
698 te->address_format != CD_LBA_FORMAT) {
704 track = toc->hdr.starting_track;
705 else if (track == 170)
706 track = toc->hdr.ending_track + 1;
707 else if (track < toc->hdr.starting_track ||
708 track > toc->hdr.ending_track + 1) {
713 if (te->address_format == CD_MSF_FORMAT) {
714 struct cd_toc_entry *entry;
716 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
717 bcopy(&cdp->toc, toc, sizeof(struct toc));
719 entry = toc->tab + (track - toc->hdr.starting_track);
720 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
721 &entry->addr.msf.second, &entry->addr.msf.frame);
723 bcopy(toc->tab + track - toc->hdr.starting_track,
724 &te->entry, sizeof(struct cd_toc_entry));
725 if (te->address_format == CD_MSF_FORMAT)
730 case CDIOCREADSUBCHANNEL:
732 struct ioc_read_subchannel *args =
733 (struct ioc_read_subchannel *)addr;
735 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
736 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
737 0, 0, 0, 0, 0, 0, 0 };
739 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
740 args->data_len < sizeof(struct cd_sub_channel_header)) {
745 format=args->data_format;
746 if ((format != CD_CURRENT_POSITION) &&
747 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
752 ccb[1] = args->address_format & CD_MSF_FORMAT;
754 if ((error = atapi_queue_cmd(cdp->device,ccb,(caddr_t)&cdp->subchan,
755 sizeof(cdp->subchan), ATPR_F_READ, 10,
759 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
760 if (cdp->subchan.header.audio_status == 0x11) {
766 if (format == CD_TRACK_INFO)
767 ccb[6] = args->track;
769 if ((error = atapi_queue_cmd(cdp->device, ccb,
770 (caddr_t)&cdp->subchan,
771 sizeof(cdp->subchan), ATPR_F_READ,
776 error = copyout(&cdp->subchan, args->data, args->data_len);
782 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
786 msf2lba(args->start_m, args->start_s, args->start_f),
787 msf2lba(args->end_m, args->end_s, args->end_f));
791 case CDIOCPLAYBLOCKS:
793 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
795 error = acd_play(cdp, args->blk, args->blk + args->len);
799 case CDIOCPLAYTRACKS:
801 struct ioc_play_track *args = (struct ioc_play_track *)addr;
804 if (!cdp->toc.hdr.ending_track) {
808 if (args->end_track < cdp->toc.hdr.ending_track + 1)
810 if (args->end_track > cdp->toc.hdr.ending_track + 1)
811 args->end_track = cdp->toc.hdr.ending_track + 1;
812 t1 = args->start_track - cdp->toc.hdr.starting_track;
813 t2 = args->end_track - cdp->toc.hdr.starting_track;
814 if (t1 < 0 || t2 < 0 ||
815 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
819 error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
820 ntohl(cdp->toc.tab[t2].addr.lba));
826 struct ioc_read_audio *args = (struct ioc_read_audio *)addr;
828 caddr_t buffer, ubuf = args->buffer;
832 if (!cdp->toc.hdr.ending_track) {
837 if ((frames = args->nframes) < 0) {
842 if (args->address_format == CD_LBA_FORMAT)
843 lba = args->address.lba;
844 else if (args->address_format == CD_MSF_FORMAT)
845 lba = msf2lba(args->address.msf.minute,
846 args->address.msf.second,
847 args->address.msf.frame);
853 #ifndef CD_BUFFER_BLOCKS
854 #define CD_BUFFER_BLOCKS 13
856 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_NOWAIT))){
860 bzero(ccb, sizeof(ccb));
865 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
866 size = blocks * 2352;
868 ccb[0] = ATAPI_READ_CD;
876 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size,
877 ATPR_F_READ, 30, NULL,NULL)))
880 if ((error = copyout(buffer, ubuf, size)))
888 if (args->address_format == CD_LBA_FORMAT)
889 args->address.lba = lba;
890 else if (args->address_format == CD_MSF_FORMAT)
891 lba2msf(lba, &args->address.msf.minute,
892 &args->address.msf.second,
893 &args->address.msf.frame);
899 struct ioc_vol *arg = (struct ioc_vol *)addr;
901 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
902 (caddr_t)&cdp->au, sizeof(cdp->au))))
905 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
909 arg->vol[0] = cdp->au.port[0].volume;
910 arg->vol[1] = cdp->au.port[1].volume;
911 arg->vol[2] = cdp->au.port[2].volume;
912 arg->vol[3] = cdp->au.port[3].volume;
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))))
923 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
927 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
928 (caddr_t)&cdp->aumask,
929 sizeof(cdp->aumask))))
931 cdp->au.data_length = 0;
932 cdp->au.port[0].channels = CHANNEL_0;
933 cdp->au.port[1].channels = CHANNEL_1;
934 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
935 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
936 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
937 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
938 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
943 struct ioc_patch *arg = (struct ioc_patch *)addr;
945 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
946 arg->patch[2], arg->patch[3]);
951 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
955 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
959 error = acd_setchan(cdp, 0, 0, 0, 0);
963 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
967 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
971 error = acd_blank(cdp, (*(int *)addr));
974 case CDRIOCNEXTWRITEABLEADDR:
976 struct acd_track_info track_info;
978 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
981 if (!track_info.nwa_valid) {
985 *(int*)addr = track_info.next_writeable_addr;
989 case CDRIOCINITWRITER:
990 error = acd_init_writer(cdp, (*(int *)addr));
993 case CDRIOCINITTRACK:
994 error = acd_init_track(cdp, (struct cdr_track *)addr);
998 error = acd_flush(cdp);
1002 error = acd_fixate(cdp, (*(int *)addr));
1005 case CDRIOCREADSPEED:
1007 int speed = *(int *)addr;
1009 /* Preserve old behavior: units in multiples of CDROM speed */
1012 error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
1016 case CDRIOCWRITESPEED:
1018 int speed = *(int *)addr;
1022 error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
1026 case CDRIOCGETBLOCKSIZE:
1027 *(int *)addr = cdp->block_size;
1030 case CDRIOCSETBLOCKSIZE:
1031 cdp->block_size = *(int *)addr;
1032 acd_set_ioparm(cdp);
1035 case CDRIOCGETPROGRESS:
1036 error = acd_get_progress(cdp, (int *)addr);
1040 error = acd_send_cue(cdp, (struct cdr_cuesheet *)addr);
1043 case DVDIOCREPORTKEY:
1044 if (!cdp->cap.read_dvdrom)
1047 error = acd_report_key(cdp, (struct dvd_authinfo *)addr);
1051 if (!cdp->cap.read_dvdrom)
1054 error = acd_send_key(cdp, (struct dvd_authinfo *)addr);
1057 case DVDIOCREADSTRUCTURE:
1058 if (!cdp->cap.read_dvdrom)
1061 error = acd_read_structure(cdp, (struct dvd_struct *)addr);
1065 *(struct disklabel *)addr = cdp->disklabel;
1070 if ((flags & FWRITE) == 0)
1073 error = setdisklabel(&cdp->disklabel, (struct disklabel *)addr, 0);
1081 ((struct partinfo *)addr)->disklab = &cdp->disklabel;
1082 ((struct partinfo *)addr)->part = &cdp->disklabel.d_partitions[0];
1092 acdstrategy(struct buf *bp)
1094 struct acd_softc *cdp = bp->b_dev->si_drv1;
1097 if (cdp->device->flags & ATA_D_DETACHING) {
1098 bp->b_flags |= B_ERROR;
1099 bp->b_error = ENXIO;
1104 /* if it's a null transfer, return immediatly. */
1105 if (bp->b_bcount == 0) {
1111 bp->b_pblkno = bp->b_blkno;
1112 bp->b_resid = bp->b_bcount;
1115 bufqdisksort(&cdp->queue, bp);
1117 ata_start(cdp->device->channel);
1121 acd_start(struct ata_device *atadev)
1123 struct acd_softc *cdp = atadev->driver;
1124 struct buf *bp = bufq_first(&cdp->queue);
1125 u_int32_t lba, lastlba, count;
1127 int track, blocksize;
1129 if (cdp->changer_info) {
1132 cdp = cdp->driver[cdp->changer_info->current_slot];
1133 bp = bufq_first(&cdp->queue);
1135 /* check for work pending on any other slot */
1136 for (i = 0; i < cdp->changer_info->slots; i++) {
1137 if (i == cdp->changer_info->current_slot)
1139 if (bufq_first(&(cdp->driver[i]->queue))) {
1140 if (!bp || time_second > (cdp->timestamp + 10)) {
1141 acd_select_slot(cdp->driver[i]);
1149 bufq_remove(&cdp->queue, bp);
1151 /* reject all queued entries if media changed */
1152 if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1153 bp->b_flags |= B_ERROR;
1159 bzero(ccb, sizeof(ccb));
1161 track = (bp->b_dev->si_udev & 0x00ff0000) >> 16;
1164 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1165 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1166 if (bp->b_flags & B_PHYS)
1167 lba = bp->b_offset / blocksize;
1169 lba = bp->b_blkno / (blocksize / DEV_BSIZE);
1170 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1173 blocksize = cdp->block_size;
1174 lastlba = cdp->disk_size;
1175 if (bp->b_flags & B_PHYS)
1176 lba = bp->b_offset / blocksize;
1178 lba = bp->b_blkno / (blocksize / DEV_BSIZE);
1181 if (bp->b_bcount % blocksize != 0) {
1182 bp->b_flags |= B_ERROR;
1183 bp->b_error = EINVAL;
1187 count = bp->b_bcount / blocksize;
1189 if (bp->b_flags & B_READ) {
1190 /* if transfer goes beyond range adjust it to be within limits */
1191 if (lba + count > lastlba) {
1192 /* if we are entirely beyond EOM return EOF */
1193 if (lastlba <= lba) {
1194 bp->b_resid = bp->b_bcount;
1198 count = lastlba - lba;
1200 switch (blocksize) {
1202 ccb[0] = ATAPI_READ_BIG;
1206 ccb[0] = ATAPI_READ_CD;
1211 ccb[0] = ATAPI_READ_CD;
1216 ccb[0] = ATAPI_WRITE_BIG;
1227 devstat_start_transaction(cdp->stats);
1228 bp->b_caller1 = cdp;
1229 atapi_queue_cmd(cdp->device, ccb, bp->b_data, count * blocksize,
1230 bp->b_flags & B_READ ? ATPR_F_READ : 0,
1231 (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bp);
1235 acd_done(struct atapi_request *request)
1237 struct buf *bp = request->driver;
1238 struct acd_softc *cdp = bp->b_caller1;
1240 if (request->error) {
1241 bp->b_error = request->error;
1242 bp->b_flags |= B_ERROR;
1245 bp->b_resid = bp->b_bcount - request->donecount;
1246 devstat_end_transaction_buf(cdp->stats, bp);
1252 acd_read_toc(struct acd_softc *cdp)
1254 struct acd_devlist *entry;
1255 int track, ntracks, len;
1259 bzero(&cdp->toc, sizeof(cdp->toc));
1260 bzero(ccb, sizeof(ccb));
1262 if (atapi_test_ready(cdp->device) != 0)
1265 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1267 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1268 ccb[0] = ATAPI_READ_TOC;
1271 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1272 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1273 bzero(&cdp->toc, sizeof(cdp->toc));
1276 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1277 if (ntracks <= 0 || ntracks > MAXTRK) {
1278 bzero(&cdp->toc, sizeof(cdp->toc));
1282 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1283 bzero(ccb, sizeof(ccb));
1284 ccb[0] = ATAPI_READ_TOC;
1287 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1288 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1289 bzero(&cdp->toc, sizeof(cdp->toc));
1292 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1294 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1295 acd_set_ioparm(cdp);
1296 bzero(ccb, sizeof(ccb));
1297 ccb[0] = ATAPI_READ_CAPACITY;
1298 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1299 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1300 bzero(&cdp->toc, sizeof(cdp->toc));
1303 cdp->disk_size = ntohl(sizes[0]) + 1;
1305 bzero(&cdp->disklabel, sizeof(struct disklabel));
1306 strncpy(cdp->disklabel.d_typename, " ",
1307 sizeof(cdp->disklabel.d_typename));
1308 strncpy(cdp->disklabel.d_typename, cdp->device->name,
1309 min(strlen(cdp->device->name),sizeof(cdp->disklabel.d_typename)-1));
1310 strncpy(cdp->disklabel.d_packname, "unknown ",
1311 sizeof(cdp->disklabel.d_packname));
1312 cdp->disklabel.d_secsize = cdp->block_size;
1313 cdp->disklabel.d_nsectors = 100;
1314 cdp->disklabel.d_ntracks = 1;
1315 cdp->disklabel.d_ncylinders = (cdp->disk_size / 100) + 1;
1316 cdp->disklabel.d_secpercyl = 100;
1317 cdp->disklabel.d_secperunit = cdp->disk_size;
1318 cdp->disklabel.d_rpm = 300;
1319 cdp->disklabel.d_interleave = 1;
1320 cdp->disklabel.d_flags = D_REMOVABLE;
1321 cdp->disklabel.d_npartitions = 1;
1322 cdp->disklabel.d_partitions[0].p_offset = 0;
1323 cdp->disklabel.d_partitions[0].p_size = cdp->disk_size;
1324 cdp->disklabel.d_partitions[0].p_fstype = FS_BSDFFS;
1325 cdp->disklabel.d_magic = DISKMAGIC;
1326 cdp->disklabel.d_magic2 = DISKMAGIC;
1327 cdp->disklabel.d_checksum = dkcksum(&cdp->disklabel);
1329 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1330 destroy_dev(entry->dev);
1331 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1334 for (track = 1; track <= ntracks; track ++) {
1337 sprintf(name, "acd%dt%d", cdp->lun, track);
1338 entry = malloc(sizeof(struct acd_devlist), M_ACD, M_NOWAIT | M_ZERO);
1339 entry->dev = make_dev(&acd_cdevsw, (cdp->lun << 3) | (track << 16),
1340 0, 0, 0644, name, NULL);
1341 entry->dev->si_drv1 = cdp->dev->si_drv1;
1342 TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1346 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1347 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1348 cdp->disk_size, cdp->block_size,
1349 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1350 if (cdp->toc.tab[0].control & 4)
1351 printf("%dMB\n", cdp->disk_size / 512);
1353 printf("%d:%d audio\n",
1354 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1360 acd_play(struct acd_softc *cdp, int start, int end)
1364 bzero(ccb, sizeof(ccb));
1365 ccb[0] = ATAPI_PLAY_MSF;
1366 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1367 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1368 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1372 acd_setchan(struct acd_softc *cdp,
1373 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1377 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1380 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1382 cdp->au.data_length = 0;
1383 cdp->au.port[0].channels = c0;
1384 cdp->au.port[1].channels = c1;
1385 cdp->au.port[2].channels = c2;
1386 cdp->au.port[3].channels = c3;
1387 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1391 acd_select_done1(struct atapi_request *request)
1393 struct acd_softc *cdp = request->driver;
1395 cdp->changer_info->current_slot = cdp->slot;
1396 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1397 wakeup(&cdp->changer_info);
1402 acd_select_done(struct atapi_request *request)
1404 struct acd_softc *cdp = request->driver;
1405 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1406 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1408 /* load the wanted slot */
1409 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1410 acd_select_done1, cdp);
1415 acd_select_slot(struct acd_softc *cdp)
1417 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1418 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1420 /* unload the current media from player */
1421 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1422 acd_select_done, cdp);
1426 acd_init_writer(struct acd_softc *cdp, int test_write)
1430 bzero(ccb, sizeof(ccb));
1431 ccb[0] = ATAPI_REZERO;
1432 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1433 ccb[0] = ATAPI_SEND_OPC_INFO;
1435 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1440 acd_fixate(struct acd_softc *cdp, int multisession)
1442 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1443 0, 0, 0, 0, 0, 0, 0, 0 };
1444 int timeout = 5*60*2;
1446 struct write_param param;
1448 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1449 (caddr_t)¶m, sizeof(param))))
1452 param.data_length = 0;
1454 param.session_type = CDR_SESS_MULTI;
1456 param.session_type = CDR_SESS_NONE;
1458 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1461 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1465 /* some drives just return ready, wait for the expected fixate time */
1466 if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1467 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1468 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1469 return atapi_test_ready(cdp->device);
1472 while (timeout-- > 0) {
1473 if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1475 tsleep(&error, PRIBIO, "acdcld", hz/2);
1481 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1483 struct write_param param;
1486 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1487 (caddr_t)¶m, sizeof(param))))
1490 param.data_length = 0;
1491 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1492 param.page_length = 0x32;
1493 param.test_write = track->test_write ? 1 : 0;
1494 param.write_type = CDR_WTYPE_TRACK;
1495 param.session_type = CDR_SESS_NONE;
1497 param.packet_size = 0;
1499 if (cdp->cap.burnproof)
1500 param.burnproof = 1;
1502 switch (track->datablock_type) {
1506 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1508 param.track_mode = CDR_TMODE_AUDIO;
1509 cdp->block_size = 2352;
1510 param.datablock_type = CDR_DB_RAW;
1511 param.session_format = CDR_SESS_CDROM;
1514 case CDR_DB_ROM_MODE1:
1515 cdp->block_size = 2048;
1516 param.track_mode = CDR_TMODE_DATA;
1517 param.datablock_type = CDR_DB_ROM_MODE1;
1518 param.session_format = CDR_SESS_CDROM;
1521 case CDR_DB_ROM_MODE2:
1522 cdp->block_size = 2336;
1523 param.track_mode = CDR_TMODE_DATA;
1524 param.datablock_type = CDR_DB_ROM_MODE2;
1525 param.session_format = CDR_SESS_CDROM;
1528 case CDR_DB_XA_MODE1:
1529 cdp->block_size = 2048;
1530 param.track_mode = CDR_TMODE_DATA;
1531 param.datablock_type = CDR_DB_XA_MODE1;
1532 param.session_format = CDR_SESS_CDROM_XA;
1535 case CDR_DB_XA_MODE2_F1:
1536 cdp->block_size = 2056;
1537 param.track_mode = CDR_TMODE_DATA;
1538 param.datablock_type = CDR_DB_XA_MODE2_F1;
1539 param.session_format = CDR_SESS_CDROM_XA;
1542 case CDR_DB_XA_MODE2_F2:
1543 cdp->block_size = 2324;
1544 param.track_mode = CDR_TMODE_DATA;
1545 param.datablock_type = CDR_DB_XA_MODE2_F2;
1546 param.session_format = CDR_SESS_CDROM_XA;
1549 case CDR_DB_XA_MODE2_MIX:
1550 cdp->block_size = 2332;
1551 param.track_mode = CDR_TMODE_DATA;
1552 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1553 param.session_format = CDR_SESS_CDROM_XA;
1556 acd_set_ioparm(cdp);
1557 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1561 acd_flush(struct acd_softc *cdp)
1563 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1564 0, 0, 0, 0, 0, 0, 0, 0 };
1566 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1571 acd_read_track_info(struct acd_softc *cdp,
1572 int32_t lba, struct acd_track_info *info)
1574 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1575 lba>>24, lba>>16, lba>>8, lba,
1577 sizeof(*info)>>8, sizeof(*info),
1578 0, 0, 0, 0, 0, 0, 0 };
1581 if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1582 ATPR_F_READ, 30, NULL, NULL)))
1584 info->track_start_addr = ntohl(info->track_start_addr);
1585 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1586 info->free_blocks = ntohl(info->free_blocks);
1587 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1588 info->track_length = ntohl(info->track_length);
1593 acd_get_progress(struct acd_softc *cdp, int *finished)
1595 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1596 0, 0, 0, 0, 0, 0, 0, 0 };
1597 struct atapi_reqsense *sense = cdp->device->result;
1600 if (atapi_test_ready(cdp->device) != EBUSY) {
1601 if (atapi_queue_cmd(cdp->device, ccb, tmp, sizeof(tmp),
1602 ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1609 ((sense->sk_specific2 | (sense->sk_specific1 << 8)) * 100) / 65535;
1616 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1618 struct write_param param;
1619 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1620 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1621 0, 0, 0, 0, 0, 0, 0 };
1628 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1629 (caddr_t)¶m, sizeof(param))))
1631 param.data_length = 0;
1632 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1633 param.page_length = 0x32;
1634 param.test_write = cuesheet->test_write ? 1 : 0;
1635 param.write_type = CDR_WTYPE_SESSION;
1636 param.session_type = cuesheet->session_type;
1638 param.packet_size = 0;
1639 param.track_mode = CDR_TMODE_AUDIO;
1640 param.datablock_type = CDR_DB_RAW;
1641 param.session_format = cuesheet->session_format;
1642 if (cdp->cap.burnproof)
1643 param.burnproof = 1;
1644 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1647 buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT);
1650 if ((error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1653 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1654 for (i=0; i<cuesheet->len; i++)
1656 printf(" %02x", buffer[i]);
1658 printf("\n%02x", buffer[i]);
1661 error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1663 free(buffer, M_ACD);
1668 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1670 struct dvd_miscauth *d;
1676 switch (ai->format) {
1677 case DVD_REPORT_AGID:
1678 case DVD_REPORT_ASF:
1679 case DVD_REPORT_RPC:
1682 case DVD_REPORT_KEY1:
1685 case DVD_REPORT_TITLE_KEY:
1689 case DVD_REPORT_CHALLENGE:
1692 case DVD_INVALIDATE_AGID:
1699 bzero(ccb, sizeof(ccb));
1700 ccb[0] = ATAPI_REPORT_KEY;
1701 ccb[2] = (lba >> 24) & 0xff;
1702 ccb[3] = (lba >> 16) & 0xff;
1703 ccb[4] = (lba >> 8) & 0xff;
1704 ccb[5] = lba & 0xff;
1705 ccb[8] = (length >> 8) & 0xff;
1706 ccb[9] = length & 0xff;
1707 ccb[10] = (ai->agid << 6) | ai->format;
1709 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1710 d->length = htons(length - 2);
1712 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1713 ai->format == DVD_INVALIDATE_AGID ? 0 : ATPR_F_READ,
1720 switch (ai->format) {
1721 case DVD_REPORT_AGID:
1722 ai->agid = d->data[3] >> 6;
1725 case DVD_REPORT_CHALLENGE:
1726 bcopy(&d->data[0], &ai->keychal[0], 10);
1729 case DVD_REPORT_KEY1:
1730 bcopy(&d->data[0], &ai->keychal[0], 5);
1733 case DVD_REPORT_TITLE_KEY:
1734 ai->cpm = (d->data[0] >> 7);
1735 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1736 ai->cgms = (d->data[0] >> 4) & 0x3;
1737 bcopy(&d->data[1], &ai->keychal[0], 5);
1740 case DVD_REPORT_ASF:
1741 ai->asf = d->data[3] & 1;
1744 case DVD_REPORT_RPC:
1745 ai->reg_type = (d->data[0] >> 6);
1746 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1747 ai->user_rsts = d->data[0] & 0x7;
1748 ai->region = d->data[1];
1749 ai->rpc_scheme = d->data[2];
1752 case DVD_INVALIDATE_AGID:
1763 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1765 struct dvd_miscauth *d;
1770 switch (ai->format) {
1771 case DVD_SEND_CHALLENGE:
1773 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1774 bcopy(ai->keychal, &d->data[0], 10);
1779 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1780 bcopy(&ai->keychal[0], &d->data[0], 5);
1785 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1786 d->data[0] = ai->region;
1793 bzero(ccb, sizeof(ccb));
1794 ccb[0] = ATAPI_SEND_KEY;
1795 ccb[8] = (length >> 8) & 0xff;
1796 ccb[9] = length & 0xff;
1797 ccb[10] = (ai->agid << 6) | ai->format;
1798 d->length = htons(length - 2);
1799 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1806 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1808 struct dvd_miscauth *d;
1814 case DVD_STRUCT_PHYSICAL:
1818 case DVD_STRUCT_COPYRIGHT:
1822 case DVD_STRUCT_DISCKEY:
1826 case DVD_STRUCT_BCA:
1830 case DVD_STRUCT_MANUFACT:
1834 case DVD_STRUCT_DDS:
1835 case DVD_STRUCT_PRERECORDED:
1836 case DVD_STRUCT_UNIQUEID:
1837 case DVD_STRUCT_LIST:
1838 case DVD_STRUCT_CMI:
1839 case DVD_STRUCT_RMD_LAST:
1840 case DVD_STRUCT_RMD_RMA:
1841 case DVD_STRUCT_DCB:
1848 d = malloc(length, M_ACD, M_NOWAIT | M_ZERO);
1849 d->length = htons(length - 2);
1851 bzero(ccb, sizeof(ccb));
1852 ccb[0] = ATAPI_READ_STRUCTURE;
1853 ccb[6] = s->layer_num;
1855 ccb[8] = (length >> 8) & 0xff;
1856 ccb[9] = length & 0xff;
1857 ccb[10] = s->agid << 6;
1858 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1865 switch (s->format) {
1866 case DVD_STRUCT_PHYSICAL: {
1867 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1869 layer->book_type = d->data[0] >> 4;
1870 layer->book_version = d->data[0] & 0xf;
1871 layer->disc_size = d->data[1] >> 4;
1872 layer->max_rate = d->data[1] & 0xf;
1873 layer->nlayers = (d->data[2] >> 5) & 3;
1874 layer->track_path = (d->data[2] >> 4) & 1;
1875 layer->layer_type = d->data[2] & 0xf;
1876 layer->linear_density = d->data[3] >> 4;
1877 layer->track_density = d->data[3] & 0xf;
1878 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1879 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1880 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1881 layer->bca = d->data[16] >> 7;
1885 case DVD_STRUCT_COPYRIGHT:
1886 s->cpst = d->data[0];
1887 s->rmi = d->data[0];
1890 case DVD_STRUCT_DISCKEY:
1891 bcopy(&d->data[0], &s->data[0], 2048);
1894 case DVD_STRUCT_BCA:
1895 s->length = ntohs(d->length);
1896 bcopy(&d->data[0], &s->data[0], s->length);
1899 case DVD_STRUCT_MANUFACT:
1900 s->length = ntohs(d->length);
1901 bcopy(&d->data[0], &s->data[0], s->length);
1912 acd_eject(struct acd_softc *cdp, int close)
1916 if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1919 if ((error = acd_start_stop(cdp, 3)))
1922 acd_prevent_allow(cdp, 1);
1923 cdp->flags |= F_LOCKED;
1930 acd_prevent_allow(cdp, 0);
1931 cdp->flags &= ~F_LOCKED;
1932 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1933 return acd_start_stop(cdp, 2);
1937 acd_blank(struct acd_softc *cdp, int blanktype)
1939 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1940 0, 0, 0, 0, 0, 0, 0, 0 };
1942 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1943 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1947 acd_prevent_allow(struct acd_softc *cdp, int lock)
1949 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1950 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1952 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1956 acd_start_stop(struct acd_softc *cdp, int start)
1958 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1959 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1961 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1965 acd_pause_resume(struct acd_softc *cdp, int pause)
1967 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1968 0, 0, 0, 0, 0, 0, 0 };
1970 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1974 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1976 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1977 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1980 error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1983 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1989 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1991 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1992 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1995 ata_prtdev(cdp->device,
1996 "modeselect pagesize=%d\n", pagesize);
1997 atapi_dump("mode select ", pagebuf, pagesize);
1999 return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
2004 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
2006 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
2007 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2010 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
2017 acd_get_cap(struct acd_softc *cdp)
2021 /* get drive capabilities, some drives needs this repeated */
2022 while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
2023 (caddr_t)&cdp->cap, sizeof(cdp->cap)))
2025 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
2026 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
2027 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
2028 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
2029 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
2030 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);