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.24 2006/09/05 00:55:37 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 <machine/bus.h>
51 #include <sys/thread2.h>
53 #include "atapi-all.h"
56 /* device structures */
57 static d_open_t acdopen;
58 static d_close_t acdclose;
59 static d_ioctl_t acdioctl;
60 static d_strategy_t acdstrategy;
62 static struct dev_ops acd_ops = {
63 { "acd", 117, D_DISK | D_TRACKCLOSE },
69 .d_strategy = acdstrategy,
73 static struct acd_softc *acd_init_lun(struct ata_device *);
74 static void acd_make_dev(struct acd_softc *);
75 static void acd_set_ioparm(struct acd_softc *);
76 static void acd_describe(struct acd_softc *);
77 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
78 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
79 static int acd_done(struct atapi_request *);
80 static void acd_read_toc(struct acd_softc *);
81 static int acd_play(struct acd_softc *, int, int);
82 static int acd_setchan(struct acd_softc *, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
83 static void acd_select_slot(struct acd_softc *);
84 static int acd_init_writer(struct acd_softc *, int);
85 static int acd_fixate(struct acd_softc *, int);
86 static int acd_init_track(struct acd_softc *, struct cdr_track *);
87 static int acd_flush(struct acd_softc *);
88 static int acd_read_track_info(struct acd_softc *, int32_t, struct acd_track_info *);
89 static int acd_get_progress(struct acd_softc *, int *);
90 static int acd_send_cue(struct acd_softc *, struct cdr_cuesheet *);
91 static int acd_report_key(struct acd_softc *, struct dvd_authinfo *);
92 static int acd_send_key(struct acd_softc *, struct dvd_authinfo *);
93 static int acd_read_structure(struct acd_softc *, struct dvd_struct *);
94 static int acd_eject(struct acd_softc *, int);
95 static int acd_blank(struct acd_softc *, int);
96 static int acd_prevent_allow(struct acd_softc *, int);
97 static int acd_start_stop(struct acd_softc *, int);
98 static int acd_pause_resume(struct acd_softc *, int);
99 static int acd_mode_sense(struct acd_softc *, int, caddr_t, int);
100 static int acd_mode_select(struct acd_softc *, caddr_t, int);
101 static int acd_set_speed(struct acd_softc *, int, int);
102 static void acd_get_cap(struct acd_softc *);
105 static u_int32_t acd_lun_map = 0;
106 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
109 acdattach(struct ata_device *atadev)
111 struct acd_softc *cdp;
114 if ((cdp = acd_init_lun(atadev)) == NULL) {
115 ata_prtdev(atadev, "acd: out of memory\n");
119 ata_set_name(atadev, "acd", cdp->lun);
120 ata_command(atadev, ATA_C_ATAPI_RESET, 0, 0, 0, ATA_IMMEDIATE);
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 = kmalloc(sizeof(struct changer), M_ACD, M_WAITOK | M_ZERO);
130 if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp,
131 sizeof(struct changer),
132 ATPR_F_READ, 60, NULL, NULL)) {
133 struct acd_softc *tmpcdp = cdp;
134 struct acd_softc **cdparr;
138 chp->table_length = htons(chp->table_length);
139 cdparr = kmalloc(sizeof(struct acd_softc) * chp->slots,
141 for (count = 0; count < chp->slots; count++) {
143 tmpcdp = acd_init_lun(atadev);
145 ata_prtdev(atadev, "out of memory\n");
149 cdparr[count] = tmpcdp;
150 tmpcdp->driver = cdparr;
151 tmpcdp->slot = count;
152 tmpcdp->changer_info = chp;
153 acd_make_dev(tmpcdp);
154 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
155 DEVSTAT_NO_ORDERED_TAGS,
156 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
157 DEVSTAT_PRIORITY_CD);
159 name = kmalloc(strlen(atadev->name) + 2, M_ACD, M_WAITOK);
160 strcpy(name, atadev->name);
162 ata_free_name(atadev);
163 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
169 devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
170 DEVSTAT_NO_ORDERED_TAGS,
171 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
172 DEVSTAT_PRIORITY_CD);
175 atadev->driver = cdp;
180 acddetach(struct ata_device *atadev)
182 struct acd_softc *cdp = atadev->driver;
183 struct acd_devlist *entry;
187 if (cdp->changer_info) {
188 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
189 if (cdp->driver[subdev] == cdp)
191 while ((bio = bioq_first(&cdp->driver[subdev]->bio_queue))) {
192 bioq_remove(&cdp->driver[subdev]->bio_queue, bio);
193 bio->bio_buf->b_flags |= B_ERROR;
194 bio->bio_buf->b_error = ENXIO;
197 release_dev(cdp->driver[subdev]->dev);
198 while ((entry = TAILQ_FIRST(&cdp->driver[subdev]->dev_list))) {
199 release_dev(entry->dev);
200 TAILQ_REMOVE(&cdp->driver[subdev]->dev_list, entry, chain);
203 devstat_remove_entry(cdp->driver[subdev]->stats);
204 kfree(cdp->driver[subdev]->stats, M_ACD);
205 ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
206 kfree(cdp->driver[subdev], M_ACD);
208 kfree(cdp->driver, M_ACD);
209 kfree(cdp->changer_info, M_ACD);
211 while ((bio = bioq_first(&cdp->bio_queue))) {
212 bio->bio_buf->b_flags |= B_ERROR;
213 bio->bio_buf->b_error = ENXIO;
216 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
217 release_dev(entry->dev);
218 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
221 release_dev(cdp->dev);
222 devstat_remove_entry(cdp->stats);
223 dev_ops_remove(&acd_ops, dkunitmask(), dkmakeunit(cdp->lun));
224 kfree(cdp->stats, M_ACD);
225 ata_free_name(atadev);
226 ata_free_lun(&acd_lun_map, cdp->lun);
228 atadev->driver = NULL;
231 static struct acd_softc *
232 acd_init_lun(struct ata_device *atadev)
234 struct acd_softc *cdp;
236 cdp = kmalloc(sizeof(struct acd_softc), M_ACD, M_WAITOK | M_ZERO);
237 TAILQ_INIT(&cdp->dev_list);
238 bioq_init(&cdp->bio_queue);
239 cdp->device = atadev;
240 cdp->lun = ata_get_lun(&acd_lun_map);
241 cdp->block_size = 2048;
243 cdp->changer_info = NULL;
244 cdp->stats = kmalloc(sizeof(struct devstat), M_ACD, M_WAITOK | M_ZERO);
249 acd_make_dev(struct acd_softc *cdp)
253 dev_ops_add(&acd_ops, dkunitmask(), dkmakeunit(cdp->lun));
254 dev = make_dev(&acd_ops, dkmakeminor(cdp->lun, 0, 0),
255 UID_ROOT, GID_OPERATOR, 0644, "acd%d", cdp->lun);
259 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
264 acd_set_ioparm(struct acd_softc *cdp)
266 cdp->dev->si_iosize_max = ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
267 cdp->dev->si_bsize_phys = cdp->block_size;
271 acd_describe(struct acd_softc *cdp)
277 ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
278 cdp->device->param->model, cdp->device->param->revision,
279 (cdp->cap.write_dvdr) ? "DVD-R" :
280 (cdp->cap.write_dvdram) ? "DVD-RAM" :
281 (cdp->cap.write_cdrw) ? "CD-RW" :
282 (cdp->cap.write_cdr) ? "CD-R" :
283 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM",
284 device_get_unit(cdp->device->channel->dev),
285 (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
287 ata_prtdev(cdp->device, "%s", "");
288 if (cdp->cap.cur_read_speed) {
289 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
290 if (cdp->cap.max_read_speed)
291 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
292 if ((cdp->cap.cur_write_speed) &&
293 (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
294 cdp->cap.write_dvdr || cdp->cap.write_dvdram)) {
295 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
296 if (cdp->cap.max_write_speed)
297 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
301 if (cdp->cap.buf_size) {
302 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
305 printf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
307 ata_prtdev(cdp->device, "Reads:");
309 if (cdp->cap.read_cdr) {
310 printf(" CD-R"); comma = 1;
312 if (cdp->cap.read_cdrw) {
313 printf("%s CD-RW", comma ? "," : ""); comma = 1;
315 if (cdp->cap.cd_da) {
316 if (cdp->cap.cd_da_stream)
317 printf("%s CD-DA stream", comma ? "," : "");
319 printf("%s CD-DA", comma ? "," : "");
322 if (cdp->cap.read_dvdrom) {
323 printf("%s DVD-ROM", comma ? "," : ""); comma = 1;
325 if (cdp->cap.read_dvdr) {
326 printf("%s DVD-R", comma ? "," : ""); comma = 1;
328 if (cdp->cap.read_dvdram) {
329 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
331 if (cdp->cap.read_packet)
332 printf("%s packet", comma ? "," : "");
335 ata_prtdev(cdp->device, "Writes:");
336 if (cdp->cap.write_cdr || cdp->cap.write_cdrw ||
337 cdp->cap.write_dvdr || cdp->cap.write_dvdram) {
339 if (cdp->cap.write_cdr) {
340 printf(" CD-R" ); comma = 1;
342 if (cdp->cap.write_cdrw) {
343 printf("%s CD-RW", comma ? "," : ""); comma = 1;
345 if (cdp->cap.write_dvdr) {
346 printf("%s DVD-R", comma ? "," : ""); comma = 1;
348 if (cdp->cap.write_dvdram) {
349 printf("%s DVD-RAM", comma ? "," : ""); comma = 1;
351 if (cdp->cap.test_write) {
352 printf("%s test write", comma ? "," : ""); comma = 1;
354 if (cdp->cap.burnproof)
355 printf("%s burnproof", comma ? "," : "");
358 if (cdp->cap.audio_play) {
359 ata_prtdev(cdp->device, "Audio: ");
360 if (cdp->cap.audio_play)
362 if (cdp->cap.max_vol_levels)
363 printf(", %d volume levels", cdp->cap.max_vol_levels);
366 ata_prtdev(cdp->device, "Mechanism: ");
367 switch (cdp->cap.mech) {
369 mechanism = "caddy"; break;
371 mechanism = "tray"; break;
373 mechanism = "popup"; break;
374 case MST_MECH_CHANGER:
375 mechanism = "changer"; break;
376 case MST_MECH_CARTRIDGE:
377 mechanism = "cartridge"; break;
379 mechanism = 0; break;
382 printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
383 else if (cdp->cap.eject)
387 printf(cdp->cap.locked ? ", locked" : ", unlocked");
388 if (cdp->cap.prevent)
389 printf(", lock protected");
392 if (cdp->cap.mech != MST_MECH_CHANGER) {
393 ata_prtdev(cdp->device, "Medium: ");
394 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
396 printf("CD-ROM "); break;
398 printf("CD-R "); break;
400 printf("CD-RW "); break;
402 printf("door open"); break;
404 printf("no/blank disc"); break;
406 printf("medium format error"); break;
408 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
409 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
411 printf("120mm data disc"); break;
413 printf("120mm audio disc"); break;
415 printf("120mm data/audio disc"); break;
417 printf("120mm photo disc"); break;
419 printf("80mm data disc"); break;
421 printf("80mm audio disc"); break;
423 printf("80mm data/audio disc"); break;
425 printf("80mm photo disc"); break;
427 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
429 printf("unknown"); break;
432 printf("blank"); break;
436 printf("unknown (0x%x)", cdp->cap.medium_type); break;
443 ata_prtdev(cdp->device, "%s ",
444 (cdp->cap.write_dvdr) ? "DVD-R" :
445 (cdp->cap.write_dvdram) ? "DVD-RAM" :
446 (cdp->cap.write_cdrw) ? "CD-RW" :
447 (cdp->cap.write_cdr) ? "CD-R" :
448 (cdp->cap.read_dvdrom) ? "DVD-ROM" : "CDROM");
450 if (cdp->changer_info)
451 printf("with %d CD changer ", cdp->changer_info->slots);
453 printf("<%.40s> at ata%d-%s %s\n", cdp->device->param->model,
454 device_get_unit(cdp->device->channel->dev),
455 (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
456 ata_mode2str(cdp->device->mode) );
461 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
465 *m = lba / (60 * 75);
471 static __inline u_int32_t
472 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
474 return (m * 60 + s) * 75 + f - 150;
478 acdopen(struct dev_open_args *ap)
480 dev_t dev = ap->a_head.a_dev;
481 struct acd_softc *cdp = dev->si_drv1;
487 if (ap->a_oflags & FWRITE) {
488 if (count_dev(dev) > 1)
492 /* wait if drive is not finished loading the medium */
494 struct atapi_reqsense *sense = cdp->device->result;
496 if (!atapi_test_ready(cdp->device))
498 if (sense->sense_key == 2 && sense->asc == 4 && sense->ascq == 1)
499 tsleep(&timeout, 0, "acdld", hz / 2);
504 if (count_dev(dev) == 1) {
505 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
506 acd_select_slot(cdp);
507 tsleep(&cdp->changer_info, 0, "acdopn", 0);
509 acd_prevent_allow(cdp, 1);
510 cdp->flags |= F_LOCKED;
517 acdclose(struct dev_close_args *ap)
519 dev_t dev = ap->a_head.a_dev;
520 struct acd_softc *cdp = dev->si_drv1;
525 if (count_dev(dev) == 1) {
526 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
527 acd_select_slot(cdp);
528 tsleep(&cdp->changer_info, 0, "acdclo", 0);
530 acd_prevent_allow(cdp, 0);
531 cdp->flags &= ~F_LOCKED;
537 acdioctl(struct dev_ioctl_args *ap)
539 dev_t dev = ap->a_head.a_dev;
540 struct acd_softc *cdp = dev->si_drv1;
546 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
547 acd_select_slot(cdp);
548 tsleep(&cdp->changer_info, 0, "acdctl", 0);
550 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
553 atapi_test_ready(cdp->device);
558 acd_prevent_allow(cdp, 1);
559 cdp->flags |= F_LOCKED;
565 error = acd_pause_resume(cdp, 1);
569 error = acd_pause_resume(cdp, 0);
573 error = acd_start_stop(cdp, 1);
577 error = acd_start_stop(cdp, 0);
581 error = acd_prevent_allow(cdp, 0);
582 cdp->flags &= ~F_LOCKED;
586 error = acd_prevent_allow(cdp, 1);
587 cdp->flags |= F_LOCKED;
591 ; /* note: if no proc EPERM will be returned */
592 error = suser_cred(ap->a_cred, 0);
595 error = atapi_test_ready(cdp->device);
599 if (count_dev(dev) > 1) {
603 error = acd_eject(cdp, 0);
607 if (count_dev(dev) > 1)
609 error = acd_eject(cdp, 1);
612 case CDIOREADTOCHEADER:
613 if (!cdp->toc.hdr.ending_track) {
617 bcopy(&cdp->toc.hdr, ap->a_data, sizeof(cdp->toc.hdr));
620 case CDIOREADTOCENTRYS:
622 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)ap->a_data;
623 struct toc *toc = &cdp->toc;
624 int starting_track = te->starting_track;
627 if (!toc->hdr.ending_track) {
632 if (te->data_len < sizeof(toc->tab[0]) ||
633 (te->data_len % sizeof(toc->tab[0])) != 0 ||
634 (te->address_format != CD_MSF_FORMAT &&
635 te->address_format != CD_LBA_FORMAT)) {
641 starting_track = toc->hdr.starting_track;
642 else if (starting_track == 170)
643 starting_track = toc->hdr.ending_track + 1;
644 else if (starting_track < toc->hdr.starting_track ||
645 starting_track > toc->hdr.ending_track + 1) {
650 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
652 if (te->data_len < len)
654 if (len > sizeof(toc->tab)) {
659 if (te->address_format == CD_MSF_FORMAT) {
660 struct cd_toc_entry *entry;
662 toc = kmalloc(sizeof(struct toc), M_ACD, M_WAITOK | M_ZERO);
663 bcopy(&cdp->toc, toc, sizeof(struct toc));
664 entry = toc->tab + (toc->hdr.ending_track + 1 -
665 toc->hdr.starting_track) + 1;
666 while (--entry >= toc->tab)
667 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
668 &entry->addr.msf.second, &entry->addr.msf.frame);
670 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
672 if (te->address_format == CD_MSF_FORMAT)
676 case CDIOREADTOCENTRY:
678 struct ioc_read_toc_single_entry *te =
679 (struct ioc_read_toc_single_entry *)ap->a_data;
680 struct toc *toc = &cdp->toc;
681 u_char track = te->track;
683 if (!toc->hdr.ending_track) {
688 if (te->address_format != CD_MSF_FORMAT &&
689 te->address_format != CD_LBA_FORMAT) {
695 track = toc->hdr.starting_track;
696 else if (track == 170)
697 track = toc->hdr.ending_track + 1;
698 else if (track < toc->hdr.starting_track ||
699 track > toc->hdr.ending_track + 1) {
704 if (te->address_format == CD_MSF_FORMAT) {
705 struct cd_toc_entry *entry;
707 toc = kmalloc(sizeof(struct toc), M_ACD, M_WAITOK | M_ZERO);
708 bcopy(&cdp->toc, toc, sizeof(struct toc));
710 entry = toc->tab + (track - toc->hdr.starting_track);
711 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
712 &entry->addr.msf.second, &entry->addr.msf.frame);
714 bcopy(toc->tab + track - toc->hdr.starting_track,
715 &te->entry, sizeof(struct cd_toc_entry));
716 if (te->address_format == CD_MSF_FORMAT)
721 case CDIOCREADSUBCHANNEL:
723 struct ioc_read_subchannel *args =
724 (struct ioc_read_subchannel *)ap->a_data;
726 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
727 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
728 0, 0, 0, 0, 0, 0, 0 };
730 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
731 args->data_len < sizeof(struct cd_sub_channel_header)) {
736 format=args->data_format;
737 if ((format != CD_CURRENT_POSITION) &&
738 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
743 ccb[1] = args->address_format & CD_MSF_FORMAT;
745 if ((error = atapi_queue_cmd(cdp->device,ccb,(caddr_t)&cdp->subchan,
746 sizeof(cdp->subchan), ATPR_F_READ, 10,
750 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
751 if (cdp->subchan.header.audio_status == 0x11) {
757 if (format == CD_TRACK_INFO)
758 ccb[6] = args->track;
760 if ((error = atapi_queue_cmd(cdp->device, ccb,
761 (caddr_t)&cdp->subchan,
762 sizeof(cdp->subchan), ATPR_F_READ,
767 error = copyout(&cdp->subchan, args->data, args->data_len);
773 struct ioc_play_msf *args = (struct ioc_play_msf *)ap->a_data;
777 msf2lba(args->start_m, args->start_s, args->start_f),
778 msf2lba(args->end_m, args->end_s, args->end_f));
782 case CDIOCPLAYBLOCKS:
784 struct ioc_play_blocks *args = (struct ioc_play_blocks *)ap->a_data;
786 error = acd_play(cdp, args->blk, args->blk + args->len);
790 case CDIOCPLAYTRACKS:
792 struct ioc_play_track *args = (struct ioc_play_track *)ap->a_data;
795 if (!cdp->toc.hdr.ending_track) {
799 if (args->end_track < cdp->toc.hdr.ending_track + 1)
801 if (args->end_track > cdp->toc.hdr.ending_track + 1)
802 args->end_track = cdp->toc.hdr.ending_track + 1;
803 t1 = args->start_track - cdp->toc.hdr.starting_track;
804 t2 = args->end_track - cdp->toc.hdr.starting_track;
805 if (t1 < 0 || t2 < 0 ||
806 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
810 error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
811 ntohl(cdp->toc.tab[t2].addr.lba));
817 struct ioc_read_audio *args = (struct ioc_read_audio *)ap->a_data;
819 caddr_t buffer, ubuf = args->buffer;
823 if (!cdp->toc.hdr.ending_track) {
828 if ((frames = args->nframes) < 0) {
833 if (args->address_format == CD_LBA_FORMAT)
834 lba = args->address.lba;
835 else if (args->address_format == CD_MSF_FORMAT)
836 lba = msf2lba(args->address.msf.minute,
837 args->address.msf.second,
838 args->address.msf.frame);
844 #ifndef CD_BUFFER_BLOCKS
845 #define CD_BUFFER_BLOCKS 13
847 if (!(buffer = kmalloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_WAITOK))){
851 bzero(ccb, sizeof(ccb));
856 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
857 size = blocks * 2352;
859 ccb[0] = ATAPI_READ_CD;
867 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size,
868 ATPR_F_READ, 30, NULL,NULL)))
871 if ((error = copyout(buffer, ubuf, size)))
878 kfree(buffer, M_ACD);
879 if (args->address_format == CD_LBA_FORMAT)
880 args->address.lba = lba;
881 else if (args->address_format == CD_MSF_FORMAT)
882 lba2msf(lba, &args->address.msf.minute,
883 &args->address.msf.second,
884 &args->address.msf.frame);
890 struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
892 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
893 (caddr_t)&cdp->au, sizeof(cdp->au))))
896 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
900 arg->vol[0] = cdp->au.port[0].volume;
901 arg->vol[1] = cdp->au.port[1].volume;
902 arg->vol[2] = cdp->au.port[2].volume;
903 arg->vol[3] = cdp->au.port[3].volume;
909 struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
911 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
912 (caddr_t)&cdp->au, sizeof(cdp->au))))
914 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
918 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
919 (caddr_t)&cdp->aumask,
920 sizeof(cdp->aumask))))
922 cdp->au.data_length = 0;
923 cdp->au.port[0].channels = CHANNEL_0;
924 cdp->au.port[1].channels = CHANNEL_1;
925 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
926 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
927 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
928 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
929 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
934 struct ioc_patch *arg = (struct ioc_patch *)ap->a_data;
936 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
937 arg->patch[2], arg->patch[3]);
942 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
946 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
950 error = acd_setchan(cdp, 0, 0, 0, 0);
954 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
958 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
962 error = acd_blank(cdp, (*(int *)ap->a_data));
965 case CDRIOCNEXTWRITEABLEADDR:
967 struct acd_track_info track_info;
969 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
972 if (!track_info.nwa_valid) {
976 *(int*)ap->a_data = track_info.next_writeable_addr;
980 case CDRIOCINITWRITER:
981 error = acd_init_writer(cdp, (*(int *)ap->a_data));
984 case CDRIOCINITTRACK:
985 error = acd_init_track(cdp, (struct cdr_track *)ap->a_data);
989 error = acd_flush(cdp);
993 error = acd_fixate(cdp, (*(int *)ap->a_data));
996 case CDRIOCREADSPEED:
998 int speed = *(int *)ap->a_data;
1000 /* Preserve old behavior: units in multiples of CDROM speed */
1003 error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
1007 case CDRIOCWRITESPEED:
1009 int speed = *(int *)ap->a_data;
1013 error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
1017 case CDRIOCGETBLOCKSIZE:
1018 *(int *)ap->a_data = cdp->block_size;
1021 case CDRIOCSETBLOCKSIZE:
1022 cdp->block_size = *(int *)ap->a_data;
1023 acd_set_ioparm(cdp);
1026 case CDRIOCGETPROGRESS:
1027 error = acd_get_progress(cdp, (int *)ap->a_data);
1031 error = acd_send_cue(cdp, (struct cdr_cuesheet *)ap->a_data);
1034 case DVDIOCREPORTKEY:
1035 if (!cdp->cap.read_dvdrom)
1038 error = acd_report_key(cdp, (struct dvd_authinfo *)ap->a_data);
1042 if (!cdp->cap.read_dvdrom)
1045 error = acd_send_key(cdp, (struct dvd_authinfo *)ap->a_data);
1048 case DVDIOCREADSTRUCTURE:
1049 if (!cdp->cap.read_dvdrom)
1052 error = acd_read_structure(cdp, (struct dvd_struct *)ap->a_data);
1056 *(struct disklabel *)ap->a_data = cdp->disklabel;
1061 if ((ap->a_fflag & FWRITE) == 0)
1064 error = setdisklabel(&cdp->disklabel, (struct disklabel *)ap->a_data, 0);
1072 ((struct partinfo *)ap->a_data)->disklab = &cdp->disklabel;
1073 ((struct partinfo *)ap->a_data)->part = &cdp->disklabel.d_partitions[0];
1083 acdstrategy(struct dev_strategy_args *ap)
1085 dev_t dev = ap->a_head.a_dev;
1086 struct bio *bio = ap->a_bio;
1087 struct buf *bp = bio->bio_buf;
1088 struct acd_softc *cdp = dev->si_drv1;
1090 if (cdp->device->flags & ATA_D_DETACHING) {
1091 bp->b_flags |= B_ERROR;
1092 bp->b_error = ENXIO;
1097 /* if it's a null transfer, return immediatly. */
1098 if (bp->b_bcount == 0) {
1104 KKASSERT(bio->bio_offset != NOOFFSET);
1105 bio->bio_driver_info = dev;
1106 bp->b_resid = bp->b_bcount;
1109 bioqdisksort(&cdp->bio_queue, bio);
1111 ata_start(cdp->device->channel);
1116 acd_start(struct ata_device *atadev)
1118 struct acd_softc *cdp = atadev->driver;
1119 struct bio *bio = bioq_first(&cdp->bio_queue);
1122 u_int32_t lba, lastlba, count;
1124 int track, blocksize;
1126 if (cdp->changer_info) {
1129 cdp = cdp->driver[cdp->changer_info->current_slot];
1130 bio = bioq_first(&cdp->bio_queue);
1132 /* check for work pending on any other slot */
1133 for (i = 0; i < cdp->changer_info->slots; i++) {
1134 if (i == cdp->changer_info->current_slot)
1136 if (bioq_first(&(cdp->driver[i]->bio_queue))) {
1137 if (bio == NULL || time_second > (cdp->timestamp + 10)) {
1138 acd_select_slot(cdp->driver[i]);
1146 bioq_remove(&cdp->bio_queue, bio);
1147 dev = bio->bio_driver_info;
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 = (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 lba = bio->bio_offset / blocksize;
1166 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1169 blocksize = cdp->block_size;
1170 lastlba = cdp->disk_size;
1171 lba = bio->bio_offset / blocksize;
1174 if (bp->b_bcount % blocksize != 0) {
1175 bp->b_flags |= B_ERROR;
1176 bp->b_error = EINVAL;
1180 count = bp->b_bcount / blocksize;
1182 if (bp->b_cmd == BUF_CMD_READ) {
1183 /* if transfer goes beyond range adjust it to be within limits */
1184 if (lba + count > lastlba) {
1185 /* if we are entirely beyond EOM return EOF */
1186 if (lastlba <= lba) {
1187 bp->b_resid = bp->b_bcount;
1191 count = lastlba - lba;
1193 switch (blocksize) {
1195 ccb[0] = ATAPI_READ_BIG;
1199 ccb[0] = ATAPI_READ_CD;
1204 ccb[0] = ATAPI_READ_CD;
1209 ccb[0] = ATAPI_WRITE_BIG;
1220 devstat_start_transaction(cdp->stats);
1221 bio->bio_caller_info1.ptr = cdp;
1222 atapi_queue_cmd(cdp->device, ccb, bp->b_data, count * blocksize,
1223 ((bp->b_cmd == BUF_CMD_READ) ? ATPR_F_READ : 0),
1224 (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bio);
1228 acd_done(struct atapi_request *request)
1230 struct bio *bio = request->driver;
1231 struct buf *bp = bio->bio_buf;
1232 struct acd_softc *cdp = bio->bio_caller_info1.ptr;
1234 if (request->error) {
1235 bp->b_error = request->error;
1236 bp->b_flags |= B_ERROR;
1238 bp->b_resid = bp->b_bcount - request->donecount;
1240 devstat_end_transaction_buf(cdp->stats, bp);
1246 acd_read_toc(struct acd_softc *cdp)
1248 struct acd_devlist *entry;
1249 int track, ntracks, len;
1253 bzero(&cdp->toc, sizeof(cdp->toc));
1254 bzero(ccb, sizeof(ccb));
1256 if (atapi_test_ready(cdp->device) != 0)
1259 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1261 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1262 ccb[0] = ATAPI_READ_TOC;
1265 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1266 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1267 bzero(&cdp->toc, sizeof(cdp->toc));
1270 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1271 if (ntracks <= 0 || ntracks > MAXTRK) {
1272 bzero(&cdp->toc, sizeof(cdp->toc));
1276 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1277 bzero(ccb, sizeof(ccb));
1278 ccb[0] = ATAPI_READ_TOC;
1281 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1282 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1283 bzero(&cdp->toc, sizeof(cdp->toc));
1286 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1288 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1289 acd_set_ioparm(cdp);
1290 bzero(ccb, sizeof(ccb));
1291 ccb[0] = ATAPI_READ_CAPACITY;
1292 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1293 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1294 bzero(&cdp->toc, sizeof(cdp->toc));
1297 cdp->disk_size = ntohl(sizes[0]) + 1;
1299 bzero(&cdp->disklabel, sizeof(struct disklabel));
1300 strncpy(cdp->disklabel.d_typename, " ",
1301 sizeof(cdp->disklabel.d_typename));
1302 strncpy(cdp->disklabel.d_typename, cdp->device->name,
1303 min(strlen(cdp->device->name),sizeof(cdp->disklabel.d_typename)-1));
1304 strncpy(cdp->disklabel.d_packname, "unknown ",
1305 sizeof(cdp->disklabel.d_packname));
1306 cdp->disklabel.d_secsize = cdp->block_size;
1307 cdp->disklabel.d_nsectors = 100;
1308 cdp->disklabel.d_ntracks = 1;
1309 cdp->disklabel.d_ncylinders = (cdp->disk_size / 100) + 1;
1310 cdp->disklabel.d_secpercyl = 100;
1311 cdp->disklabel.d_secperunit = cdp->disk_size;
1312 cdp->disklabel.d_rpm = 300;
1313 cdp->disklabel.d_interleave = 1;
1314 cdp->disklabel.d_flags = D_REMOVABLE;
1315 cdp->disklabel.d_npartitions = 1;
1316 cdp->disklabel.d_partitions[0].p_offset = 0;
1317 cdp->disklabel.d_partitions[0].p_size = cdp->disk_size;
1318 cdp->disklabel.d_partitions[0].p_fstype = FS_BSDFFS;
1319 cdp->disklabel.d_magic = DISKMAGIC;
1320 cdp->disklabel.d_magic2 = DISKMAGIC;
1321 cdp->disklabel.d_checksum = dkcksum(&cdp->disklabel);
1323 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1324 destroy_dev(entry->dev);
1325 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1326 kfree(entry, M_ACD);
1328 for (track = 1; track <= ntracks; track ++) {
1331 sprintf(name, "acd%dt%d", cdp->lun, track);
1332 entry = kmalloc(sizeof(struct acd_devlist), M_ACD, M_WAITOK | M_ZERO);
1333 entry->dev = make_dev(&acd_ops, (cdp->lun << 3) | (track << 16),
1334 0, 0, 0644, name, NULL);
1335 entry->dev->si_drv1 = cdp->dev->si_drv1;
1336 reference_dev(entry->dev);
1337 TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1341 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1342 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1343 cdp->disk_size, cdp->block_size,
1344 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1345 if (cdp->toc.tab[0].control & 4)
1346 printf("%dMB\n", cdp->disk_size / 512);
1348 printf("%d:%d audio\n",
1349 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1355 acd_play(struct acd_softc *cdp, int start, int end)
1359 bzero(ccb, sizeof(ccb));
1360 ccb[0] = ATAPI_PLAY_MSF;
1361 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1362 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1363 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1367 acd_setchan(struct acd_softc *cdp,
1368 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1372 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1375 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1377 cdp->au.data_length = 0;
1378 cdp->au.port[0].channels = c0;
1379 cdp->au.port[1].channels = c1;
1380 cdp->au.port[2].channels = c2;
1381 cdp->au.port[3].channels = c3;
1382 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1386 acd_select_done1(struct atapi_request *request)
1388 struct acd_softc *cdp = request->driver;
1390 cdp->changer_info->current_slot = cdp->slot;
1391 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1392 wakeup(&cdp->changer_info);
1397 acd_select_done(struct atapi_request *request)
1399 struct acd_softc *cdp = request->driver;
1400 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1401 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1403 /* load the wanted slot */
1404 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1405 acd_select_done1, cdp);
1410 acd_select_slot(struct acd_softc *cdp)
1412 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1413 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1415 /* unload the current media from player */
1416 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1417 acd_select_done, cdp);
1421 acd_init_writer(struct acd_softc *cdp, int test_write)
1425 bzero(ccb, sizeof(ccb));
1426 ccb[0] = ATAPI_REZERO;
1427 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1428 ccb[0] = ATAPI_SEND_OPC_INFO;
1430 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1435 acd_fixate(struct acd_softc *cdp, int multisession)
1437 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1438 0, 0, 0, 0, 0, 0, 0, 0 };
1439 int timeout = 5*60*2;
1441 struct write_param param;
1443 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1444 (caddr_t)¶m, sizeof(param))))
1447 param.data_length = 0;
1449 param.session_type = CDR_SESS_MULTI;
1451 param.session_type = CDR_SESS_NONE;
1453 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1456 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1460 /* some drives just return ready, wait for the expected fixate time */
1461 if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1462 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1463 tsleep(&error, 0, "acdfix", timeout * hz / 2);
1464 return atapi_test_ready(cdp->device);
1467 while (timeout-- > 0) {
1468 if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1470 tsleep(&error, 0, "acdcld", hz/2);
1476 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1478 struct write_param param;
1481 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1482 (caddr_t)¶m, sizeof(param))))
1485 param.data_length = 0;
1486 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1487 param.page_length = 0x32;
1488 param.test_write = track->test_write ? 1 : 0;
1489 param.write_type = CDR_WTYPE_TRACK;
1490 param.session_type = CDR_SESS_NONE;
1492 param.packet_size = 0;
1494 if (cdp->cap.burnproof)
1495 param.burnproof = 1;
1497 switch (track->datablock_type) {
1501 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1503 param.track_mode = CDR_TMODE_AUDIO;
1504 cdp->block_size = 2352;
1505 param.datablock_type = CDR_DB_RAW;
1506 param.session_format = CDR_SESS_CDROM;
1509 case CDR_DB_ROM_MODE1:
1510 cdp->block_size = 2048;
1511 param.track_mode = CDR_TMODE_DATA;
1512 param.datablock_type = CDR_DB_ROM_MODE1;
1513 param.session_format = CDR_SESS_CDROM;
1516 case CDR_DB_ROM_MODE2:
1517 cdp->block_size = 2336;
1518 param.track_mode = CDR_TMODE_DATA;
1519 param.datablock_type = CDR_DB_ROM_MODE2;
1520 param.session_format = CDR_SESS_CDROM;
1523 case CDR_DB_XA_MODE1:
1524 cdp->block_size = 2048;
1525 param.track_mode = CDR_TMODE_DATA;
1526 param.datablock_type = CDR_DB_XA_MODE1;
1527 param.session_format = CDR_SESS_CDROM_XA;
1530 case CDR_DB_XA_MODE2_F1:
1531 cdp->block_size = 2056;
1532 param.track_mode = CDR_TMODE_DATA;
1533 param.datablock_type = CDR_DB_XA_MODE2_F1;
1534 param.session_format = CDR_SESS_CDROM_XA;
1537 case CDR_DB_XA_MODE2_F2:
1538 cdp->block_size = 2324;
1539 param.track_mode = CDR_TMODE_DATA;
1540 param.datablock_type = CDR_DB_XA_MODE2_F2;
1541 param.session_format = CDR_SESS_CDROM_XA;
1544 case CDR_DB_XA_MODE2_MIX:
1545 cdp->block_size = 2332;
1546 param.track_mode = CDR_TMODE_DATA;
1547 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1548 param.session_format = CDR_SESS_CDROM_XA;
1551 acd_set_ioparm(cdp);
1552 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1556 acd_flush(struct acd_softc *cdp)
1558 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1559 0, 0, 0, 0, 0, 0, 0, 0 };
1561 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1566 acd_read_track_info(struct acd_softc *cdp,
1567 int32_t lba, struct acd_track_info *info)
1569 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1570 lba>>24, lba>>16, lba>>8, lba,
1572 sizeof(*info)>>8, sizeof(*info),
1573 0, 0, 0, 0, 0, 0, 0 };
1576 if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1577 ATPR_F_READ, 30, NULL, NULL)))
1579 info->track_start_addr = ntohl(info->track_start_addr);
1580 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1581 info->free_blocks = ntohl(info->free_blocks);
1582 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1583 info->track_length = ntohl(info->track_length);
1588 acd_get_progress(struct acd_softc *cdp, int *finished)
1590 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1591 0, 0, 0, 0, 0, 0, 0, 0 };
1592 struct atapi_reqsense *sense = cdp->device->result;
1595 if (atapi_test_ready(cdp->device) != EBUSY) {
1596 if (atapi_queue_cmd(cdp->device, ccb, tmp, sizeof(tmp),
1597 ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1604 ((sense->sk_specific2 | (sense->sk_specific1 << 8)) * 100) / 65535;
1611 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1613 struct write_param param;
1614 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1615 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1616 0, 0, 0, 0, 0, 0, 0 };
1623 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1624 (caddr_t)¶m, sizeof(param))))
1626 param.data_length = 0;
1627 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1628 param.page_length = 0x32;
1629 param.test_write = cuesheet->test_write ? 1 : 0;
1630 param.write_type = CDR_WTYPE_SESSION;
1631 param.session_type = cuesheet->session_type;
1633 param.packet_size = 0;
1634 param.track_mode = CDR_TMODE_AUDIO;
1635 param.datablock_type = CDR_DB_RAW;
1636 param.session_format = cuesheet->session_format;
1637 if (cdp->cap.burnproof)
1638 param.burnproof = 1;
1639 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1642 buffer = kmalloc(cuesheet->len, M_ACD, M_WAITOK);
1645 if ((error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1648 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1649 for (i=0; i<cuesheet->len; i++)
1651 printf(" %02x", buffer[i]);
1653 printf("\n%02x", buffer[i]);
1656 error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1658 kfree(buffer, M_ACD);
1663 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1665 struct dvd_miscauth *d;
1671 /* this is common even for ai->format == DVD_INVALIDATE_AGID */
1672 bzero(ccb, sizeof(ccb));
1673 ccb[0] = ATAPI_REPORT_KEY;
1674 ccb[2] = (lba >> 24) & 0xff;
1675 ccb[3] = (lba >> 16) & 0xff;
1676 ccb[4] = (lba >> 8) & 0xff;
1677 ccb[5] = lba & 0xff;
1678 ccb[10] = (ai->agid << 6) | ai->format;
1680 switch (ai->format) {
1681 case DVD_REPORT_AGID:
1682 case DVD_REPORT_ASF:
1683 case DVD_REPORT_RPC:
1686 case DVD_REPORT_KEY1:
1689 case DVD_REPORT_TITLE_KEY:
1693 case DVD_REPORT_CHALLENGE:
1696 case DVD_INVALIDATE_AGID:
1697 return(atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL));
1702 ccb[8] = (length >> 8) & 0xff;
1703 ccb[9] = length & 0xff;
1705 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1706 d->length = htons(length - 2);
1708 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1709 ATPR_F_READ, 10, NULL, NULL);
1715 switch (ai->format) {
1716 case DVD_REPORT_AGID:
1717 ai->agid = d->data[3] >> 6;
1720 case DVD_REPORT_CHALLENGE:
1721 bcopy(&d->data[0], &ai->keychal[0], 10);
1724 case DVD_REPORT_KEY1:
1725 bcopy(&d->data[0], &ai->keychal[0], 5);
1728 case DVD_REPORT_TITLE_KEY:
1729 ai->cpm = (d->data[0] >> 7);
1730 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1731 ai->cgms = (d->data[0] >> 4) & 0x3;
1732 bcopy(&d->data[1], &ai->keychal[0], 5);
1735 case DVD_REPORT_ASF:
1736 ai->asf = d->data[3] & 1;
1739 case DVD_REPORT_RPC:
1740 ai->reg_type = (d->data[0] >> 6);
1741 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1742 ai->user_rsts = d->data[0] & 0x7;
1743 ai->region = d->data[1];
1744 ai->rpc_scheme = d->data[2];
1747 case DVD_INVALIDATE_AGID:
1759 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1761 struct dvd_miscauth *d;
1766 switch (ai->format) {
1767 case DVD_SEND_CHALLENGE:
1769 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1770 bcopy(ai->keychal, &d->data[0], 10);
1775 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1776 bcopy(&ai->keychal[0], &d->data[0], 5);
1781 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1782 d->data[0] = ai->region;
1789 bzero(ccb, sizeof(ccb));
1790 ccb[0] = ATAPI_SEND_KEY;
1791 ccb[8] = (length >> 8) & 0xff;
1792 ccb[9] = length & 0xff;
1793 ccb[10] = (ai->agid << 6) | ai->format;
1794 d->length = htons(length - 2);
1795 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1802 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1804 struct dvd_miscauth *d;
1810 case DVD_STRUCT_PHYSICAL:
1814 case DVD_STRUCT_COPYRIGHT:
1818 case DVD_STRUCT_DISCKEY:
1822 case DVD_STRUCT_BCA:
1826 case DVD_STRUCT_MANUFACT:
1830 case DVD_STRUCT_DDS:
1831 case DVD_STRUCT_PRERECORDED:
1832 case DVD_STRUCT_UNIQUEID:
1833 case DVD_STRUCT_LIST:
1834 case DVD_STRUCT_CMI:
1835 case DVD_STRUCT_RMD_LAST:
1836 case DVD_STRUCT_RMD_RMA:
1837 case DVD_STRUCT_DCB:
1844 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1845 d->length = htons(length - 2);
1847 bzero(ccb, sizeof(ccb));
1848 ccb[0] = ATAPI_READ_STRUCTURE;
1849 ccb[6] = s->layer_num;
1851 ccb[8] = (length >> 8) & 0xff;
1852 ccb[9] = length & 0xff;
1853 ccb[10] = s->agid << 6;
1854 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1861 switch (s->format) {
1862 case DVD_STRUCT_PHYSICAL: {
1863 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1865 layer->book_type = d->data[0] >> 4;
1866 layer->book_version = d->data[0] & 0xf;
1867 layer->disc_size = d->data[1] >> 4;
1868 layer->max_rate = d->data[1] & 0xf;
1869 layer->nlayers = (d->data[2] >> 5) & 3;
1870 layer->track_path = (d->data[2] >> 4) & 1;
1871 layer->layer_type = d->data[2] & 0xf;
1872 layer->linear_density = d->data[3] >> 4;
1873 layer->track_density = d->data[3] & 0xf;
1874 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1875 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1876 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1877 layer->bca = d->data[16] >> 7;
1881 case DVD_STRUCT_COPYRIGHT:
1882 s->cpst = d->data[0];
1883 s->rmi = d->data[0];
1886 case DVD_STRUCT_DISCKEY:
1887 bcopy(&d->data[0], &s->data[0], 2048);
1890 case DVD_STRUCT_BCA:
1891 s->length = ntohs(d->length);
1892 bcopy(&d->data[0], &s->data[0], s->length);
1895 case DVD_STRUCT_MANUFACT:
1896 s->length = ntohs(d->length);
1897 bcopy(&d->data[0], &s->data[0], s->length);
1908 acd_eject(struct acd_softc *cdp, int close)
1912 if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1915 if ((error = acd_start_stop(cdp, 3)))
1918 acd_prevent_allow(cdp, 1);
1919 cdp->flags |= F_LOCKED;
1926 acd_prevent_allow(cdp, 0);
1927 cdp->flags &= ~F_LOCKED;
1928 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1929 return acd_start_stop(cdp, 2);
1933 acd_blank(struct acd_softc *cdp, int blanktype)
1935 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1936 0, 0, 0, 0, 0, 0, 0, 0 };
1938 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1939 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1943 acd_prevent_allow(struct acd_softc *cdp, int lock)
1945 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1946 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1948 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1952 acd_start_stop(struct acd_softc *cdp, int start)
1954 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1955 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1957 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1961 acd_pause_resume(struct acd_softc *cdp, int pause)
1963 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1964 0, 0, 0, 0, 0, 0, 0 };
1966 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1970 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1972 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1973 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1976 error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1979 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1985 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1987 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1988 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1991 ata_prtdev(cdp->device,
1992 "modeselect pagesize=%d\n", pagesize);
1993 atapi_dump("mode select ", pagebuf, pagesize);
1995 return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
2000 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
2002 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
2003 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2006 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
2013 acd_get_cap(struct acd_softc *cdp)
2017 /* get drive capabilities, some drives needs this repeated */
2018 while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
2019 (caddr_t)&cdp->cap, sizeof(cdp->cap)))
2021 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
2022 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
2023 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
2024 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
2025 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
2026 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);