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 $
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 <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 *);
106 static u_int32_t acd_lun_map = 0;
107 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
110 acdattach(struct ata_device *atadev)
112 struct acd_softc *cdp;
115 if ((cdp = acd_init_lun(atadev)) == NULL) {
116 ata_prtdev(atadev, "acd: out of memory\n");
120 ata_set_name(atadev, "acd", cdp->lun);
123 /* if this is a changer device, allocate the neeeded lun's */
124 if (cdp->cap.mech == MST_MECH_CHANGER) {
125 int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0,
126 sizeof(struct changer)>>8, sizeof(struct changer),
129 chp = malloc(sizeof(struct changer), M_ACD, M_NOWAIT | M_ZERO);
131 ata_prtdev(atadev, "out of memory\n");
135 if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp,
136 sizeof(struct changer),
137 ATPR_F_READ, 60, NULL, NULL)) {
138 struct acd_softc *tmpcdp = cdp;
139 struct acd_softc **cdparr;
143 chp->table_length = htons(chp->table_length);
144 if (!(cdparr = malloc(sizeof(struct acd_softc) * chp->slots,
146 ata_prtdev(atadev, "out of memory\n");
151 for (count = 0; count < chp->slots; count++) {
153 tmpcdp = acd_init_lun(atadev);
155 ata_prtdev(atadev, "out of memory\n");
159 cdparr[count] = tmpcdp;
160 tmpcdp->driver = cdparr;
161 tmpcdp->slot = count;
162 tmpcdp->changer_info = chp;
163 acd_make_dev(tmpcdp);
164 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
165 DEVSTAT_NO_ORDERED_TAGS,
166 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
167 DEVSTAT_PRIORITY_CD);
169 name = malloc(strlen(atadev->name) + 2, M_ACD, M_NOWAIT);
170 strcpy(name, atadev->name);
172 ata_free_name(atadev);
173 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
179 devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
180 DEVSTAT_NO_ORDERED_TAGS,
181 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
182 DEVSTAT_PRIORITY_CD);
185 atadev->driver = cdp;
190 acddetach(struct ata_device *atadev)
192 struct acd_softc *cdp = atadev->driver;
193 struct acd_devlist *entry;
197 if (cdp->changer_info) {
198 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
199 if (cdp->driver[subdev] == cdp)
201 while ((bp = bufq_first(&cdp->driver[subdev]->queue))) {
202 bufq_remove(&cdp->driver[subdev]->queue, bp);
203 bp->b_flags |= B_ERROR;
207 destroy_dev(cdp->driver[subdev]->dev);
208 while ((entry = TAILQ_FIRST(&cdp->driver[subdev]->dev_list))) {
209 destroy_dev(entry->dev);
210 TAILQ_REMOVE(&cdp->driver[subdev]->dev_list, entry, chain);
213 devstat_remove_entry(cdp->driver[subdev]->stats);
214 free(cdp->driver[subdev]->stats, M_ACD);
215 ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
216 free(cdp->driver[subdev], M_ACD);
218 free(cdp->driver, M_ACD);
219 free(cdp->changer_info, M_ACD);
221 while ((bp = bufq_first(&cdp->queue))) {
222 bp->b_flags |= B_ERROR;
226 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
227 destroy_dev(entry->dev);
228 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
231 destroy_dev(cdp->dev);
232 devstat_remove_entry(cdp->stats);
233 free(cdp->stats, M_ACD);
234 ata_free_name(atadev);
235 ata_free_lun(&acd_lun_map, cdp->lun);
237 atadev->driver = NULL;
240 static struct acd_softc *
241 acd_init_lun(struct ata_device *atadev)
243 struct acd_softc *cdp;
245 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO)))
247 TAILQ_INIT(&cdp->dev_list);
248 bufq_init(&cdp->queue);
249 cdp->device = atadev;
250 cdp->lun = ata_get_lun(&acd_lun_map);
251 cdp->block_size = 2048;
253 cdp->changer_info = NULL;
254 if (!(cdp->stats = malloc(sizeof(struct devstat), M_ACD,
255 M_NOWAIT | M_ZERO))) {
263 acd_make_dev(struct acd_softc *cdp)
267 dev = make_dev(&acd_cdevsw, dkmakeminor(cdp->lun, 0, 0),
268 UID_ROOT, GID_OPERATOR, 0644, "acd%d", cdp->lun);
271 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
276 acd_set_ioparm(struct acd_softc *cdp)
278 cdp->dev->si_iosize_max = ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
279 cdp->dev->si_bsize_phys = cdp->block_size;
283 acd_describe(struct acd_softc *cdp)
289 ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
290 cdp->device->param->model, cdp->device->param->revision,
291 (cdp->cap.write_dvdr) ? "DVD-R" :
292 (cdp->cap.write_dvdram) ? "DVD-RAM" :
293 (cdp->cap.write_cdrw) ? "CD-RW" :
294 (cdp->cap.write_cdr) ? "CD-R" :
295 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM",
296 device_get_unit(cdp->device->channel->dev),
297 (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
299 ata_prtdev(cdp->device, "%s", "");
300 if (cdp->cap.cur_read_speed) {
301 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
302 if (cdp->cap.max_read_speed)
303 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
304 if ((cdp->cap.cur_write_speed) &&
305 (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
306 cdp->cap.write_dvdr || cdp->cap.write_dvdram)) {
307 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
308 if (cdp->cap.max_write_speed)
309 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
313 if (cdp->cap.buf_size) {
314 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
317 printf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
319 ata_prtdev(cdp->device, "Reads:");
321 if (cdp->cap.read_cdr) {
322 printf(" CD-R"); comma = 1;
324 if (cdp->cap.read_cdrw) {
325 printf("%s CD-RW", comma ? "," : ""); comma = 1;
327 if (cdp->cap.cd_da) {
328 if (cdp->cap.cd_da_stream)
329 printf("%s CD-DA stream", comma ? "," : "");
331 printf("%s CD-DA", comma ? "," : "");
334 if (cdp->cap.read_dvdrom) {
335 printf("%s DVD-ROM", comma ? "," : ""); comma = 1;
337 if (cdp->cap.read_dvdr) {
338 printf("%s DVD-R", comma ? "," : ""); comma = 1;
340 if (cdp->cap.read_dvdram) {
341 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
343 if (cdp->cap.read_packet)
344 printf("%s packet", comma ? "," : "");
347 ata_prtdev(cdp->device, "Writes:");
348 if (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
349 cdp->cap.write_dvdr || cdp->cap.write_dvdram) {
351 if (cdp->cap.write_cdr) {
352 printf(" CD-R" ); comma = 1;
354 if (cdp->cap.write_cdrw) {
355 printf("%s CD-RW", comma ? "," : ""); comma = 1;
357 if (cdp->cap.write_dvdr) {
358 printf("%s DVD-R", comma ? "," : ""); comma = 1;
360 if (cdp->cap.write_dvdram) {
361 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
363 if (cdp->cap.test_write) {
364 printf("%s test write", comma ? "," : ""); comma = 1;
366 if (cdp->cap.burnproof)
367 printf("%s burnproof", comma ? "," : "");
370 if (cdp->cap.audio_play) {
371 ata_prtdev(cdp->device, "Audio: ");
372 if (cdp->cap.audio_play)
374 if (cdp->cap.max_vol_levels)
375 printf(", %d volume levels", cdp->cap.max_vol_levels);
378 ata_prtdev(cdp->device, "Mechanism: ");
379 switch (cdp->cap.mech) {
381 mechanism = "caddy"; break;
383 mechanism = "tray"; break;
385 mechanism = "popup"; break;
386 case MST_MECH_CHANGER:
387 mechanism = "changer"; break;
388 case MST_MECH_CARTRIDGE:
389 mechanism = "cartridge"; break;
391 mechanism = 0; break;
394 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
395 else if (cdp->cap.eject)
399 printf(cdp->cap.locked ? ", locked" : ", unlocked");
400 if (cdp->cap.prevent)
401 printf(", lock protected");
404 if (cdp->cap.mech != MST_MECH_CHANGER) {
405 ata_prtdev(cdp->device, "Medium: ");
406 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
408 printf("CD-ROM "); break;
410 printf("CD-R "); break;
412 printf("CD-RW "); break;
414 printf("door open"); break;
416 printf("no/blank disc"); break;
418 printf("medium format error"); break;
420 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
421 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
423 printf("120mm data disc"); break;
425 printf("120mm audio disc"); break;
427 printf("120mm data/audio disc"); break;
429 printf("120mm photo disc"); break;
431 printf("80mm data disc"); break;
433 printf("80mm audio disc"); break;
435 printf("80mm data/audio disc"); break;
437 printf("80mm photo disc"); break;
439 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
441 printf("unknown"); break;
444 printf("blank"); break;
448 printf("unknown (0x%x)", cdp->cap.medium_type); break;
455 ata_prtdev(cdp->device, "%s ",
456 (cdp->cap.write_dvdr) ? "DVD-R" :
457 (cdp->cap.write_dvdram) ? "DVD-RAM" :
458 (cdp->cap.write_cdrw) ? "CD-RW" :
459 (cdp->cap.write_cdr) ? "CD-R" :
460 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM");
462 if (cdp->changer_info)
463 printf("with %d CD changer ", cdp->changer_info->slots);
465 printf("<%.40s> at ata%d-%s %s\n", cdp->device->param->model,
466 device_get_unit(cdp->device->channel->dev),
467 (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
468 ata_mode2str(cdp->device->mode) );
473 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
477 *m = lba / (60 * 75);
483 static __inline u_int32_t
484 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
486 return (m * 60 + s) * 75 + f - 150;
490 acdopen(dev_t dev, int flags, int fmt, struct proc *p)
492 struct acd_softc *cdp = dev->si_drv1;
498 if (flags & FWRITE) {
499 if (count_dev(dev) > 1)
503 /* wait if drive is not finished loading the medium */
505 struct atapi_reqsense *sense = cdp->device->result;
507 if (!atapi_test_ready(cdp->device))
509 if (sense->sense_key == 2 && sense->asc == 4 && sense->ascq == 1)
510 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
515 if (count_dev(dev) == 1) {
516 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
517 acd_select_slot(cdp);
518 tsleep(&cdp->changer_info, PRIBIO, "acdopn", 0);
520 acd_prevent_allow(cdp, 1);
521 cdp->flags |= F_LOCKED;
528 acdclose(dev_t dev, int flags, int fmt, struct proc *p)
530 struct acd_softc *cdp = dev->si_drv1;
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, "acdclo", 0);
540 acd_prevent_allow(cdp, 0);
541 cdp->flags &= ~F_LOCKED;
547 acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
549 struct acd_softc *cdp = dev->si_drv1;
555 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
556 acd_select_slot(cdp);
557 tsleep(&cdp->changer_info, PRIBIO, "acdctl", 0);
559 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
562 atapi_test_ready(cdp->device);
567 acd_prevent_allow(cdp, 1);
568 cdp->flags |= F_LOCKED;
574 error = acd_pause_resume(cdp, 1);
578 error = acd_pause_resume(cdp, 0);
582 error = acd_start_stop(cdp, 1);
586 error = acd_start_stop(cdp, 0);
590 error = acd_prevent_allow(cdp, 0);
591 cdp->flags &= ~F_LOCKED;
595 error = acd_prevent_allow(cdp, 1);
596 cdp->flags |= F_LOCKED;
603 error = atapi_test_ready(cdp->device);
607 if (count_dev(dev) > 1) {
611 error = acd_eject(cdp, 0);
615 if (count_dev(dev) > 1)
617 error = acd_eject(cdp, 1);
620 case CDIOREADTOCHEADER:
621 if (!cdp->toc.hdr.ending_track) {
625 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
628 case CDIOREADTOCENTRYS:
630 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
631 struct toc *toc = &cdp->toc;
632 int starting_track = te->starting_track;
635 if (!toc->hdr.ending_track) {
640 if (te->data_len < sizeof(toc->tab[0]) ||
641 (te->data_len % sizeof(toc->tab[0])) != 0 ||
642 (te->address_format != CD_MSF_FORMAT &&
643 te->address_format != CD_LBA_FORMAT)) {
649 starting_track = toc->hdr.starting_track;
650 else if (starting_track == 170)
651 starting_track = toc->hdr.ending_track + 1;
652 else if (starting_track < toc->hdr.starting_track ||
653 starting_track > toc->hdr.ending_track + 1) {
658 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
660 if (te->data_len < len)
662 if (len > sizeof(toc->tab)) {
667 if (te->address_format == CD_MSF_FORMAT) {
668 struct cd_toc_entry *entry;
670 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
671 bcopy(&cdp->toc, toc, sizeof(struct toc));
672 entry = toc->tab + (toc->hdr.ending_track + 1 -
673 toc->hdr.starting_track) + 1;
674 while (--entry >= toc->tab)
675 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
676 &entry->addr.msf.second, &entry->addr.msf.frame);
678 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
680 if (te->address_format == CD_MSF_FORMAT)
684 case CDIOREADTOCENTRY:
686 struct ioc_read_toc_single_entry *te =
687 (struct ioc_read_toc_single_entry *)addr;
688 struct toc *toc = &cdp->toc;
689 u_char track = te->track;
691 if (!toc->hdr.ending_track) {
696 if (te->address_format != CD_MSF_FORMAT &&
697 te->address_format != CD_LBA_FORMAT) {
703 track = toc->hdr.starting_track;
704 else if (track == 170)
705 track = toc->hdr.ending_track + 1;
706 else if (track < toc->hdr.starting_track ||
707 track > toc->hdr.ending_track + 1) {
712 if (te->address_format == CD_MSF_FORMAT) {
713 struct cd_toc_entry *entry;
715 toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT | M_ZERO);
716 bcopy(&cdp->toc, toc, sizeof(struct toc));
718 entry = toc->tab + (track - toc->hdr.starting_track);
719 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
720 &entry->addr.msf.second, &entry->addr.msf.frame);
722 bcopy(toc->tab + track - toc->hdr.starting_track,
723 &te->entry, sizeof(struct cd_toc_entry));
724 if (te->address_format == CD_MSF_FORMAT)
729 case CDIOCREADSUBCHANNEL:
731 struct ioc_read_subchannel *args =
732 (struct ioc_read_subchannel *)addr;
734 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
735 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
736 0, 0, 0, 0, 0, 0, 0 };
738 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
739 args->data_len < sizeof(struct cd_sub_channel_header)) {
744 format=args->data_format;
745 if ((format != CD_CURRENT_POSITION) &&
746 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
751 ccb[1] = args->address_format & CD_MSF_FORMAT;
753 if ((error = atapi_queue_cmd(cdp->device,ccb,(caddr_t)&cdp->subchan,
754 sizeof(cdp->subchan), ATPR_F_READ, 10,
758 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
759 if (cdp->subchan.header.audio_status == 0x11) {
765 if (format == CD_TRACK_INFO)
766 ccb[6] = args->track;
768 if ((error = atapi_queue_cmd(cdp->device, ccb,
769 (caddr_t)&cdp->subchan,
770 sizeof(cdp->subchan), ATPR_F_READ,
775 error = copyout(&cdp->subchan, args->data, args->data_len);
781 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
785 msf2lba(args->start_m, args->start_s, args->start_f),
786 msf2lba(args->end_m, args->end_s, args->end_f));
790 case CDIOCPLAYBLOCKS:
792 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
794 error = acd_play(cdp, args->blk, args->blk + args->len);
798 case CDIOCPLAYTRACKS:
800 struct ioc_play_track *args = (struct ioc_play_track *)addr;
803 if (!cdp->toc.hdr.ending_track) {
807 if (args->end_track < cdp->toc.hdr.ending_track + 1)
809 if (args->end_track > cdp->toc.hdr.ending_track + 1)
810 args->end_track = cdp->toc.hdr.ending_track + 1;
811 t1 = args->start_track - cdp->toc.hdr.starting_track;
812 t2 = args->end_track - cdp->toc.hdr.starting_track;
813 if (t1 < 0 || t2 < 0 ||
814 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
818 error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
819 ntohl(cdp->toc.tab[t2].addr.lba));
825 struct ioc_read_audio *args = (struct ioc_read_audio *)addr;
827 caddr_t buffer, ubuf = args->buffer;
831 if (!cdp->toc.hdr.ending_track) {
836 if ((frames = args->nframes) < 0) {
841 if (args->address_format == CD_LBA_FORMAT)
842 lba = args->address.lba;
843 else if (args->address_format == CD_MSF_FORMAT)
844 lba = msf2lba(args->address.msf.minute,
845 args->address.msf.second,
846 args->address.msf.frame);
852 #ifndef CD_BUFFER_BLOCKS
853 #define CD_BUFFER_BLOCKS 13
855 if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_NOWAIT))){
859 bzero(ccb, sizeof(ccb));
864 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
865 size = blocks * 2352;
867 ccb[0] = ATAPI_READ_CD;
875 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size,
876 ATPR_F_READ, 30, NULL,NULL)))
879 if ((error = copyout(buffer, ubuf, size)))
887 if (args->address_format == CD_LBA_FORMAT)
888 args->address.lba = lba;
889 else if (args->address_format == CD_MSF_FORMAT)
890 lba2msf(lba, &args->address.msf.minute,
891 &args->address.msf.second,
892 &args->address.msf.frame);
898 struct ioc_vol *arg = (struct ioc_vol *)addr;
900 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
901 (caddr_t)&cdp->au, sizeof(cdp->au))))
904 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
908 arg->vol[0] = cdp->au.port[0].volume;
909 arg->vol[1] = cdp->au.port[1].volume;
910 arg->vol[2] = cdp->au.port[2].volume;
911 arg->vol[3] = cdp->au.port[3].volume;
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))))
922 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
926 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
927 (caddr_t)&cdp->aumask,
928 sizeof(cdp->aumask))))
930 cdp->au.data_length = 0;
931 cdp->au.port[0].channels = CHANNEL_0;
932 cdp->au.port[1].channels = CHANNEL_1;
933 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
934 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
935 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
936 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
937 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
942 struct ioc_patch *arg = (struct ioc_patch *)addr;
944 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
945 arg->patch[2], arg->patch[3]);
950 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
954 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
958 error = acd_setchan(cdp, 0, 0, 0, 0);
962 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
966 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
970 error = acd_blank(cdp, (*(int *)addr));
973 case CDRIOCNEXTWRITEABLEADDR:
975 struct acd_track_info track_info;
977 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
980 if (!track_info.nwa_valid) {
984 *(int*)addr = track_info.next_writeable_addr;
988 case CDRIOCINITWRITER:
989 error = acd_init_writer(cdp, (*(int *)addr));
992 case CDRIOCINITTRACK:
993 error = acd_init_track(cdp, (struct cdr_track *)addr);
997 error = acd_flush(cdp);
1001 error = acd_fixate(cdp, (*(int *)addr));
1004 case CDRIOCREADSPEED:
1006 int speed = *(int *)addr;
1008 /* Preserve old behavior: units in multiples of CDROM speed */
1011 error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
1015 case CDRIOCWRITESPEED:
1017 int speed = *(int *)addr;
1021 error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
1025 case CDRIOCGETBLOCKSIZE:
1026 *(int *)addr = cdp->block_size;
1029 case CDRIOCSETBLOCKSIZE:
1030 cdp->block_size = *(int *)addr;
1031 acd_set_ioparm(cdp);
1034 case CDRIOCGETPROGRESS:
1035 error = acd_get_progress(cdp, (int *)addr);
1039 error = acd_send_cue(cdp, (struct cdr_cuesheet *)addr);
1042 case DVDIOCREPORTKEY:
1043 if (!cdp->cap.read_dvdrom)
1046 error = acd_report_key(cdp, (struct dvd_authinfo *)addr);
1050 if (!cdp->cap.read_dvdrom)
1053 error = acd_send_key(cdp, (struct dvd_authinfo *)addr);
1056 case DVDIOCREADSTRUCTURE:
1057 if (!cdp->cap.read_dvdrom)
1060 error = acd_read_structure(cdp, (struct dvd_struct *)addr);
1064 *(struct disklabel *)addr = cdp->disklabel;
1069 if ((flags & FWRITE) == 0)
1072 error = setdisklabel(&cdp->disklabel, (struct disklabel *)addr, 0);
1080 ((struct partinfo *)addr)->disklab = &cdp->disklabel;
1081 ((struct partinfo *)addr)->part = &cdp->disklabel.d_partitions[0];
1091 acdstrategy(struct buf *bp)
1093 struct acd_softc *cdp = bp->b_dev->si_drv1;
1096 if (cdp->device->flags & ATA_D_DETACHING) {
1097 bp->b_flags |= B_ERROR;
1098 bp->b_error = ENXIO;
1103 /* if it's a null transfer, return immediatly. */
1104 if (bp->b_bcount == 0) {
1110 bp->b_pblkno = bp->b_blkno;
1111 bp->b_resid = bp->b_bcount;
1114 bufqdisksort(&cdp->queue, bp);
1116 ata_start(cdp->device->channel);
1120 acd_start(struct ata_device *atadev)
1122 struct acd_softc *cdp = atadev->driver;
1123 struct buf *bp = bufq_first(&cdp->queue);
1124 u_int32_t lba, lastlba, count;
1126 int track, blocksize;
1128 if (cdp->changer_info) {
1131 cdp = cdp->driver[cdp->changer_info->current_slot];
1132 bp = bufq_first(&cdp->queue);
1134 /* check for work pending on any other slot */
1135 for (i = 0; i < cdp->changer_info->slots; i++) {
1136 if (i == cdp->changer_info->current_slot)
1138 if (bufq_first(&(cdp->driver[i]->queue))) {
1139 if (!bp || time_second > (cdp->timestamp + 10)) {
1140 acd_select_slot(cdp->driver[i]);
1148 bufq_remove(&cdp->queue, bp);
1150 /* reject all queued entries if media changed */
1151 if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1152 bp->b_flags |= B_ERROR;
1158 bzero(ccb, sizeof(ccb));
1160 track = (bp->b_dev->si_udev & 0x00ff0000) >> 16;
1163 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1164 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1165 if (bp->b_flags & B_PHYS)
1166 lba = bp->b_offset / blocksize;
1168 lba = bp->b_blkno / (blocksize / DEV_BSIZE);
1169 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1172 blocksize = cdp->block_size;
1173 lastlba = cdp->disk_size;
1174 if (bp->b_flags & B_PHYS)
1175 lba = bp->b_offset / blocksize;
1177 lba = bp->b_blkno / (blocksize / DEV_BSIZE);
1180 if (bp->b_bcount % blocksize != 0) {
1181 bp->b_flags |= B_ERROR;
1182 bp->b_error = EINVAL;
1186 count = bp->b_bcount / blocksize;
1188 if (bp->b_flags & B_READ) {
1189 /* if transfer goes beyond range adjust it to be within limits */
1190 if (lba + count > lastlba) {
1191 /* if we are entirely beyond EOM return EOF */
1192 if (lastlba <= lba) {
1193 bp->b_resid = bp->b_bcount;
1197 count = lastlba - lba;
1199 switch (blocksize) {
1201 ccb[0] = ATAPI_READ_BIG;
1205 ccb[0] = ATAPI_READ_CD;
1210 ccb[0] = ATAPI_READ_CD;
1215 ccb[0] = ATAPI_WRITE_BIG;
1226 devstat_start_transaction(cdp->stats);
1227 bp->b_caller1 = cdp;
1228 atapi_queue_cmd(cdp->device, ccb, bp->b_data, count * blocksize,
1229 bp->b_flags & B_READ ? ATPR_F_READ : 0,
1230 (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bp);
1234 acd_done(struct atapi_request *request)
1236 struct buf *bp = request->driver;
1237 struct acd_softc *cdp = bp->b_caller1;
1239 if (request->error) {
1240 bp->b_error = request->error;
1241 bp->b_flags |= B_ERROR;
1244 bp->b_resid = bp->b_bcount - request->donecount;
1245 devstat_end_transaction_buf(cdp->stats, bp);
1251 acd_read_toc(struct acd_softc *cdp)
1253 struct acd_devlist *entry;
1254 int track, ntracks, len;
1258 bzero(&cdp->toc, sizeof(cdp->toc));
1259 bzero(ccb, sizeof(ccb));
1261 if (atapi_test_ready(cdp->device) != 0)
1264 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1266 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1267 ccb[0] = ATAPI_READ_TOC;
1270 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1271 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1272 bzero(&cdp->toc, sizeof(cdp->toc));
1275 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1276 if (ntracks <= 0 || ntracks > MAXTRK) {
1277 bzero(&cdp->toc, sizeof(cdp->toc));
1281 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1282 bzero(ccb, sizeof(ccb));
1283 ccb[0] = ATAPI_READ_TOC;
1286 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1287 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1288 bzero(&cdp->toc, sizeof(cdp->toc));
1291 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1293 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1294 acd_set_ioparm(cdp);
1295 bzero(ccb, sizeof(ccb));
1296 ccb[0] = ATAPI_READ_CAPACITY;
1297 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1298 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1299 bzero(&cdp->toc, sizeof(cdp->toc));
1302 cdp->disk_size = ntohl(sizes[0]) + 1;
1304 bzero(&cdp->disklabel, sizeof(struct disklabel));
1305 strncpy(cdp->disklabel.d_typename, " ",
1306 sizeof(cdp->disklabel.d_typename));
1307 strncpy(cdp->disklabel.d_typename, cdp->device->name,
1308 min(strlen(cdp->device->name),sizeof(cdp->disklabel.d_typename)-1));
1309 strncpy(cdp->disklabel.d_packname, "unknown ",
1310 sizeof(cdp->disklabel.d_packname));
1311 cdp->disklabel.d_secsize = cdp->block_size;
1312 cdp->disklabel.d_nsectors = 100;
1313 cdp->disklabel.d_ntracks = 1;
1314 cdp->disklabel.d_ncylinders = (cdp->disk_size / 100) + 1;
1315 cdp->disklabel.d_secpercyl = 100;
1316 cdp->disklabel.d_secperunit = cdp->disk_size;
1317 cdp->disklabel.d_rpm = 300;
1318 cdp->disklabel.d_interleave = 1;
1319 cdp->disklabel.d_flags = D_REMOVABLE;
1320 cdp->disklabel.d_npartitions = 1;
1321 cdp->disklabel.d_partitions[0].p_offset = 0;
1322 cdp->disklabel.d_partitions[0].p_size = cdp->disk_size;
1323 cdp->disklabel.d_partitions[0].p_fstype = FS_BSDFFS;
1324 cdp->disklabel.d_magic = DISKMAGIC;
1325 cdp->disklabel.d_magic2 = DISKMAGIC;
1326 cdp->disklabel.d_checksum = dkcksum(&cdp->disklabel);
1328 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1329 destroy_dev(entry->dev);
1330 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1333 for (track = 1; track <= ntracks; track ++) {
1336 sprintf(name, "acd%dt%d", cdp->lun, track);
1337 entry = malloc(sizeof(struct acd_devlist), M_ACD, M_NOWAIT | M_ZERO);
1338 entry->dev = make_dev(&acd_cdevsw, (cdp->lun << 3) | (track << 16),
1339 0, 0, 0644, name, NULL);
1340 entry->dev->si_drv1 = cdp->dev->si_drv1;
1341 TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1345 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1346 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1347 cdp->disk_size, cdp->block_size,
1348 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1349 if (cdp->toc.tab[0].control & 4)
1350 printf("%dMB\n", cdp->disk_size / 512);
1352 printf("%d:%d audio\n",
1353 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1359 acd_play(struct acd_softc *cdp, int start, int end)
1363 bzero(ccb, sizeof(ccb));
1364 ccb[0] = ATAPI_PLAY_MSF;
1365 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1366 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1367 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1371 acd_setchan(struct acd_softc *cdp,
1372 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1376 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1379 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1381 cdp->au.data_length = 0;
1382 cdp->au.port[0].channels = c0;
1383 cdp->au.port[1].channels = c1;
1384 cdp->au.port[2].channels = c2;
1385 cdp->au.port[3].channels = c3;
1386 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1390 acd_select_done1(struct atapi_request *request)
1392 struct acd_softc *cdp = request->driver;
1394 cdp->changer_info->current_slot = cdp->slot;
1395 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1396 wakeup(&cdp->changer_info);
1401 acd_select_done(struct atapi_request *request)
1403 struct acd_softc *cdp = request->driver;
1404 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1405 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1407 /* load the wanted slot */
1408 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1409 acd_select_done1, cdp);
1414 acd_select_slot(struct acd_softc *cdp)
1416 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1417 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1419 /* unload the current media from player */
1420 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1421 acd_select_done, cdp);
1425 acd_init_writer(struct acd_softc *cdp, int test_write)
1429 bzero(ccb, sizeof(ccb));
1430 ccb[0] = ATAPI_REZERO;
1431 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1432 ccb[0] = ATAPI_SEND_OPC_INFO;
1434 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1439 acd_fixate(struct acd_softc *cdp, int multisession)
1441 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1442 0, 0, 0, 0, 0, 0, 0, 0 };
1443 int timeout = 5*60*2;
1445 struct write_param param;
1447 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1448 (caddr_t)¶m, sizeof(param))))
1451 param.data_length = 0;
1453 param.session_type = CDR_SESS_MULTI;
1455 param.session_type = CDR_SESS_NONE;
1457 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1460 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1464 /* some drives just return ready, wait for the expected fixate time */
1465 if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1466 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1467 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1468 return atapi_test_ready(cdp->device);
1471 while (timeout-- > 0) {
1472 if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1474 tsleep(&error, PRIBIO, "acdcld", hz/2);
1480 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1482 struct write_param param;
1485 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1486 (caddr_t)¶m, sizeof(param))))
1489 param.data_length = 0;
1490 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1491 param.page_length = 0x32;
1492 param.test_write = track->test_write ? 1 : 0;
1493 param.write_type = CDR_WTYPE_TRACK;
1494 param.session_type = CDR_SESS_NONE;
1496 param.packet_size = 0;
1498 if (cdp->cap.burnproof)
1499 param.burnproof = 1;
1501 switch (track->datablock_type) {
1505 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1507 param.track_mode = CDR_TMODE_AUDIO;
1508 cdp->block_size = 2352;
1509 param.datablock_type = CDR_DB_RAW;
1510 param.session_format = CDR_SESS_CDROM;
1513 case CDR_DB_ROM_MODE1:
1514 cdp->block_size = 2048;
1515 param.track_mode = CDR_TMODE_DATA;
1516 param.datablock_type = CDR_DB_ROM_MODE1;
1517 param.session_format = CDR_SESS_CDROM;
1520 case CDR_DB_ROM_MODE2:
1521 cdp->block_size = 2336;
1522 param.track_mode = CDR_TMODE_DATA;
1523 param.datablock_type = CDR_DB_ROM_MODE2;
1524 param.session_format = CDR_SESS_CDROM;
1527 case CDR_DB_XA_MODE1:
1528 cdp->block_size = 2048;
1529 param.track_mode = CDR_TMODE_DATA;
1530 param.datablock_type = CDR_DB_XA_MODE1;
1531 param.session_format = CDR_SESS_CDROM_XA;
1534 case CDR_DB_XA_MODE2_F1:
1535 cdp->block_size = 2056;
1536 param.track_mode = CDR_TMODE_DATA;
1537 param.datablock_type = CDR_DB_XA_MODE2_F1;
1538 param.session_format = CDR_SESS_CDROM_XA;
1541 case CDR_DB_XA_MODE2_F2:
1542 cdp->block_size = 2324;
1543 param.track_mode = CDR_TMODE_DATA;
1544 param.datablock_type = CDR_DB_XA_MODE2_F2;
1545 param.session_format = CDR_SESS_CDROM_XA;
1548 case CDR_DB_XA_MODE2_MIX:
1549 cdp->block_size = 2332;
1550 param.track_mode = CDR_TMODE_DATA;
1551 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1552 param.session_format = CDR_SESS_CDROM_XA;
1555 acd_set_ioparm(cdp);
1556 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1560 acd_flush(struct acd_softc *cdp)
1562 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1563 0, 0, 0, 0, 0, 0, 0, 0 };
1565 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1570 acd_read_track_info(struct acd_softc *cdp,
1571 int32_t lba, struct acd_track_info *info)
1573 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1574 lba>>24, lba>>16, lba>>8, lba,
1576 sizeof(*info)>>8, sizeof(*info),
1577 0, 0, 0, 0, 0, 0, 0 };
1580 if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1581 ATPR_F_READ, 30, NULL, NULL)))
1583 info->track_start_addr = ntohl(info->track_start_addr);
1584 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1585 info->free_blocks = ntohl(info->free_blocks);
1586 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1587 info->track_length = ntohl(info->track_length);
1592 acd_get_progress(struct acd_softc *cdp, int *finished)
1594 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1595 0, 0, 0, 0, 0, 0, 0, 0 };
1596 struct atapi_reqsense *sense = cdp->device->result;
1599 if (atapi_test_ready(cdp->device) != EBUSY) {
1600 if (atapi_queue_cmd(cdp->device, ccb, tmp, sizeof(tmp),
1601 ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1608 ((sense->sk_specific2 | (sense->sk_specific1 << 8)) * 100) / 65535;
1615 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1617 struct write_param param;
1618 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1619 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1620 0, 0, 0, 0, 0, 0, 0 };
1627 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1628 (caddr_t)¶m, sizeof(param))))
1630 param.data_length = 0;
1631 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1632 param.page_length = 0x32;
1633 param.test_write = cuesheet->test_write ? 1 : 0;
1634 param.write_type = CDR_WTYPE_SESSION;
1635 param.session_type = cuesheet->session_type;
1637 param.packet_size = 0;
1638 param.track_mode = CDR_TMODE_AUDIO;
1639 param.datablock_type = CDR_DB_RAW;
1640 param.session_format = cuesheet->session_format;
1641 if (cdp->cap.burnproof)
1642 param.burnproof = 1;
1643 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1646 buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT);
1649 if ((error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1652 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1653 for (i=0; i<cuesheet->len; i++)
1655 printf(" %02x", buffer[i]);
1657 printf("\n%02x", buffer[i]);
1660 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);