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.37 2008/01/06 16:55:49 swildner Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
43 #include <sys/diskslice.h>
44 #include <sys/devicestat.h>
46 #include <sys/cdrio.h>
47 #include <sys/dvdio.h>
48 #include <sys/fcntl.h>
50 #include <sys/ctype.h>
52 #include <sys/thread2.h>
55 #include "atapi-all.h"
58 /* device structures */
59 static d_open_t acdopen;
60 static d_close_t acdclose;
61 static d_ioctl_t acdioctl;
62 static d_strategy_t acdstrategy;
64 static struct dev_ops acd_ops = {
65 { "acd", 117, D_DISK | D_TRACKCLOSE },
71 .d_strategy = acdstrategy,
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);
122 ata_command(atadev, ATA_C_ATAPI_RESET, 0, 0, 0, ATA_IMMEDIATE);
125 /* if this is a changer device, allocate the neeeded lun's */
126 if (cdp->cap.mech == MST_MECH_CHANGER) {
127 int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0,
128 sizeof(struct changer)>>8, sizeof(struct changer),
131 chp = kmalloc(sizeof(struct changer), M_ACD, M_WAITOK | M_ZERO);
132 if (!atapi_queue_cmd(cdp->device, ccb, (caddr_t)chp,
133 sizeof(struct changer),
134 ATPR_F_READ, 60, NULL, NULL)) {
135 struct acd_softc *tmpcdp = cdp;
136 struct acd_softc **cdparr;
140 chp->table_length = htons(chp->table_length);
141 cdparr = kmalloc(sizeof(struct acd_softc) * chp->slots,
143 for (count = 0; count < chp->slots; count++) {
145 tmpcdp = acd_init_lun(atadev);
147 ata_prtdev(atadev, "out of memory\n");
151 cdparr[count] = tmpcdp;
152 tmpcdp->driver = cdparr;
153 tmpcdp->slot = count;
154 tmpcdp->changer_info = chp;
155 acd_make_dev(tmpcdp);
156 devstat_add_entry(tmpcdp->stats, "acd", tmpcdp->lun, DEV_BSIZE,
157 DEVSTAT_NO_ORDERED_TAGS,
158 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
159 DEVSTAT_PRIORITY_CD);
161 name = kmalloc(strlen(atadev->name) + 2, M_ACD, M_WAITOK);
162 strcpy(name, atadev->name);
164 ata_free_name(atadev);
165 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
171 devstat_add_entry(cdp->stats, "acd", cdp->lun, DEV_BSIZE,
172 DEVSTAT_NO_ORDERED_TAGS,
173 DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
174 DEVSTAT_PRIORITY_CD);
177 atadev->driver = cdp;
182 acddetach(struct ata_device *atadev)
184 struct acd_softc *cdp = atadev->driver;
185 struct acd_devlist *entry;
189 if (cdp->changer_info) {
190 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
191 if (cdp->driver[subdev] == cdp)
193 while ((bio = bioq_first(&cdp->driver[subdev]->bio_queue))) {
194 bioq_remove(&cdp->driver[subdev]->bio_queue, bio);
195 bio->bio_buf->b_flags |= B_ERROR;
196 bio->bio_buf->b_error = ENXIO;
199 release_dev(cdp->driver[subdev]->dev);
200 while ((entry = TAILQ_FIRST(&cdp->driver[subdev]->dev_list))) {
201 release_dev(entry->dev);
202 TAILQ_REMOVE(&cdp->driver[subdev]->dev_list, entry, chain);
205 devstat_remove_entry(cdp->driver[subdev]->stats);
206 kfree(cdp->driver[subdev]->stats, M_ACD);
207 ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
208 kfree(cdp->driver[subdev], M_ACD);
210 kfree(cdp->driver, M_ACD);
211 kfree(cdp->changer_info, M_ACD);
213 while ((bio = bioq_first(&cdp->bio_queue))) {
214 bio->bio_buf->b_flags |= B_ERROR;
215 bio->bio_buf->b_error = ENXIO;
218 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
219 release_dev(entry->dev);
220 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
223 disk_invalidate(&cdp->disk);
224 disk_destroy(&cdp->disk);
225 devstat_remove_entry(cdp->stats);
226 kfree(cdp->stats, M_ACD);
227 ata_free_name(atadev);
228 ata_free_lun(&acd_lun_map, cdp->lun);
230 atadev->driver = NULL;
233 static struct acd_softc *
234 acd_init_lun(struct ata_device *atadev)
236 struct acd_softc *cdp;
238 cdp = kmalloc(sizeof(struct acd_softc), M_ACD, M_WAITOK | M_ZERO);
239 TAILQ_INIT(&cdp->dev_list);
240 bioq_init(&cdp->bio_queue);
241 cdp->device = atadev;
242 cdp->lun = ata_get_lun(&acd_lun_map);
243 cdp->block_size = 2048;
245 cdp->changer_info = NULL;
246 cdp->stats = kmalloc(sizeof(struct devstat), M_ACD, M_WAITOK | M_ZERO);
251 acd_make_dev(struct acd_softc *cdp)
255 dev = disk_create(cdp->lun, &cdp->disk, &acd_ops);
258 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
263 acd_set_ioparm(struct acd_softc *cdp)
265 struct disk_info info;
267 cdp->dev->si_iosize_max =
268 ((256*DEV_BSIZE)/cdp->block_size)*cdp->block_size;
269 cdp->dev->si_bsize_phys = cdp->block_size;
270 bzero(&info, sizeof(info));
271 info.d_media_blksize = cdp->block_size;
272 info.d_media_blocks = cdp->disk_size;
273 info.d_secpertrack = 100;
275 info.d_ncylinders = cdp->disk_size / info.d_secpertrack / info.d_nheads + 1;
276 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
277 info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL | DSO_COMPATPARTA |
279 disk_setdiskinfo(&cdp->disk, &info);
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 kprintf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
302 if (cdp->cap.max_read_speed)
303 kprintf(" (%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 kprintf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
308 if (cdp->cap.max_write_speed)
309 kprintf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
313 if (cdp->cap.buf_size) {
314 kprintf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
317 kprintf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
319 ata_prtdev(cdp->device, "Reads:");
321 if (cdp->cap.read_cdr) {
322 kprintf(" CD-R"); comma = 1;
324 if (cdp->cap.read_cdrw) {
325 kprintf("%s CD-RW", comma ? "," : ""); comma = 1;
327 if (cdp->cap.cd_da) {
328 if (cdp->cap.cd_da_stream)
329 kprintf("%s CD-DA stream", comma ? "," : "");
331 kprintf("%s CD-DA", comma ? "," : "");
334 if (cdp->cap.read_dvdrom) {
335 kprintf("%s DVD-ROM", comma ? "," : ""); comma = 1;
337 if (cdp->cap.read_dvdr) {
338 kprintf("%s DVD-R", comma ? "," : ""); comma = 1;
340 if (cdp->cap.read_dvdram) {
341 kprintf("%s DVD-RAM", comma ? "," : ""); comma = 1;
343 if (cdp->cap.read_packet)
344 kprintf("%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 kprintf(" CD-R" ); comma = 1;
354 if (cdp->cap.write_cdrw) {
355 kprintf("%s CD-RW", comma ? "," : ""); comma = 1;
357 if (cdp->cap.write_dvdr) {
358 kprintf("%s DVD-R", comma ? "," : ""); comma = 1;
360 if (cdp->cap.write_dvdram) {
361 kprintf("%s DVD-RAM", comma ? "," : ""); comma = 1;
363 if (cdp->cap.test_write) {
364 kprintf("%s test write", comma ? "," : ""); comma = 1;
366 if (cdp->cap.burnproof)
367 kprintf("%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 kprintf(", %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 kprintf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism);
395 else if (cdp->cap.eject)
396 kprintf("ejectable");
399 kprintf(cdp->cap.locked ? ", locked" : ", unlocked");
400 if (cdp->cap.prevent)
401 kprintf(", 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 kprintf("CD-ROM "); break;
410 kprintf("CD-R "); break;
412 kprintf("CD-RW "); break;
414 kprintf("door open"); break;
416 kprintf("no/blank disc"); break;
418 kprintf("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 kprintf("120mm data disc"); break;
425 kprintf("120mm audio disc"); break;
427 kprintf("120mm data/audio disc"); break;
429 kprintf("120mm photo disc"); break;
431 kprintf("80mm data disc"); break;
433 kprintf("80mm audio disc"); break;
435 kprintf("80mm data/audio disc"); break;
437 kprintf("80mm photo disc"); break;
439 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
441 kprintf("unknown"); break;
444 kprintf("blank"); break;
448 kprintf("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 kprintf("with %d CD changer ", cdp->changer_info->slots);
465 kprintf("<%.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(struct dev_open_args *ap)
492 cdev_t dev = ap->a_head.a_dev;
493 struct acd_softc *cdp = dev->si_drv1;
499 if (ap->a_oflags & 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, 0, "acdld", hz / 2);
516 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
517 acd_select_slot(cdp);
518 tsleep(&cdp->changer_info, 0, "acdopn", 0);
520 acd_prevent_allow(cdp, 1);
521 cdp->flags |= F_LOCKED;
527 acdclose(struct dev_close_args *ap)
529 cdev_t dev = ap->a_head.a_dev;
530 struct acd_softc *cdp = dev->si_drv1;
535 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
536 acd_select_slot(cdp);
537 tsleep(&cdp->changer_info, 0, "acdclo", 0);
539 acd_prevent_allow(cdp, 0);
540 cdp->flags &= ~F_LOCKED;
545 acdioctl(struct dev_ioctl_args *ap)
547 cdev_t dev = ap->a_head.a_dev;
548 struct acd_softc *cdp = dev->si_drv1;
554 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
555 acd_select_slot(cdp);
556 tsleep(&cdp->changer_info, 0, "acdctl", 0);
558 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
561 atapi_test_ready(cdp->device);
566 acd_prevent_allow(cdp, 1);
567 cdp->flags |= F_LOCKED;
573 error = acd_pause_resume(cdp, 1);
577 error = acd_pause_resume(cdp, 0);
581 error = acd_start_stop(cdp, 1);
585 error = acd_start_stop(cdp, 0);
589 error = acd_prevent_allow(cdp, 0);
590 cdp->flags &= ~F_LOCKED;
594 error = acd_prevent_allow(cdp, 1);
595 cdp->flags |= F_LOCKED;
599 ; /* note: if no proc EPERM will be returned */
600 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
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, ap->a_data, sizeof(cdp->toc.hdr));
628 case CDIOREADTOCENTRYS:
630 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)ap->a_data;
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 = kmalloc(sizeof(struct toc), M_ACD, M_WAITOK | 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 *)ap->a_data;
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 = kmalloc(sizeof(struct toc), M_ACD, M_WAITOK | 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 *)ap->a_data;
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 *)ap->a_data;
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 *)ap->a_data;
794 error = acd_play(cdp, args->blk, args->blk + args->len);
798 case CDIOCPLAYTRACKS:
800 struct ioc_play_track *args = (struct ioc_play_track *)ap->a_data;
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 *)ap->a_data;
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 buffer = kmalloc(CD_BUFFER_BLOCKS * 2352, M_ACD, M_WAITOK);
856 bzero(ccb, sizeof(ccb));
861 blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames;
862 size = blocks * 2352;
864 ccb[0] = ATAPI_READ_CD;
872 if ((error = atapi_queue_cmd(cdp->device, ccb, buffer, size,
873 ATPR_F_READ, 30, NULL,NULL)))
876 if ((error = copyout(buffer, ubuf, size)))
883 kfree(buffer, M_ACD);
884 if (args->address_format == CD_LBA_FORMAT)
885 args->address.lba = lba;
886 else if (args->address_format == CD_MSF_FORMAT)
887 lba2msf(lba, &args->address.msf.minute,
888 &args->address.msf.second,
889 &args->address.msf.frame);
895 struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
897 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
898 (caddr_t)&cdp->au, sizeof(cdp->au))))
901 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
905 arg->vol[0] = cdp->au.port[0].volume;
906 arg->vol[1] = cdp->au.port[1].volume;
907 arg->vol[2] = cdp->au.port[2].volume;
908 arg->vol[3] = cdp->au.port[3].volume;
914 struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
916 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
917 (caddr_t)&cdp->au, sizeof(cdp->au))))
919 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
923 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
924 (caddr_t)&cdp->aumask,
925 sizeof(cdp->aumask))))
927 cdp->au.data_length = 0;
928 cdp->au.port[0].channels = CHANNEL_0;
929 cdp->au.port[1].channels = CHANNEL_1;
930 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
931 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
932 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
933 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
934 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
939 struct ioc_patch *arg = (struct ioc_patch *)ap->a_data;
941 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
942 arg->patch[2], arg->patch[3]);
947 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
951 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
955 error = acd_setchan(cdp, 0, 0, 0, 0);
959 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
963 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
967 error = acd_blank(cdp, (*(int *)ap->a_data));
970 case CDRIOCNEXTWRITEABLEADDR:
972 struct acd_track_info track_info;
974 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
977 if (!track_info.nwa_valid) {
981 *(int*)ap->a_data = track_info.next_writeable_addr;
985 case CDRIOCINITWRITER:
986 error = acd_init_writer(cdp, (*(int *)ap->a_data));
989 case CDRIOCINITTRACK:
990 error = acd_init_track(cdp, (struct cdr_track *)ap->a_data);
994 error = acd_flush(cdp);
998 error = acd_fixate(cdp, (*(int *)ap->a_data));
1001 case CDRIOCREADSPEED:
1003 int speed = *(int *)ap->a_data;
1005 /* Preserve old behavior: units in multiples of CDROM speed */
1008 error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
1012 case CDRIOCWRITESPEED:
1014 int speed = *(int *)ap->a_data;
1018 error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
1022 case CDRIOCGETBLOCKSIZE:
1023 *(int *)ap->a_data = cdp->block_size;
1026 case CDRIOCSETBLOCKSIZE:
1027 cdp->block_size = *(int *)ap->a_data;
1028 acd_set_ioparm(cdp);
1031 case CDRIOCGETPROGRESS:
1032 error = acd_get_progress(cdp, (int *)ap->a_data);
1036 error = acd_send_cue(cdp, (struct cdr_cuesheet *)ap->a_data);
1039 case DVDIOCREPORTKEY:
1040 if (!cdp->cap.read_dvdrom)
1043 error = acd_report_key(cdp, (struct dvd_authinfo *)ap->a_data);
1047 if (!cdp->cap.read_dvdrom)
1050 error = acd_send_key(cdp, (struct dvd_authinfo *)ap->a_data);
1053 case DVDIOCREADSTRUCTURE:
1054 if (!cdp->cap.read_dvdrom)
1057 error = acd_read_structure(cdp, (struct dvd_struct *)ap->a_data);
1067 acdstrategy(struct dev_strategy_args *ap)
1069 cdev_t dev = ap->a_head.a_dev;
1070 struct bio *bio = ap->a_bio;
1071 struct buf *bp = bio->bio_buf;
1072 struct acd_softc *cdp = dev->si_drv1;
1074 if (cdp->device->flags & ATA_D_DETACHING) {
1075 bp->b_flags |= B_ERROR;
1076 bp->b_error = ENXIO;
1081 /* if it's a null transfer, return immediatly. */
1082 if (bp->b_bcount == 0) {
1088 KKASSERT(bio->bio_offset != NOOFFSET);
1089 bio->bio_driver_info = dev;
1090 bp->b_resid = bp->b_bcount;
1093 bioqdisksort(&cdp->bio_queue, bio);
1095 ata_start(cdp->device->channel);
1100 acd_start(struct ata_device *atadev)
1102 struct acd_softc *cdp = atadev->driver;
1103 struct bio *bio = bioq_first(&cdp->bio_queue);
1106 u_int32_t lba, lastlba, count;
1108 int track, blocksize;
1110 if (cdp->changer_info) {
1113 cdp = cdp->driver[cdp->changer_info->current_slot];
1114 bio = bioq_first(&cdp->bio_queue);
1116 /* check for work pending on any other slot */
1117 for (i = 0; i < cdp->changer_info->slots; i++) {
1118 if (i == cdp->changer_info->current_slot)
1120 if (bioq_first(&(cdp->driver[i]->bio_queue))) {
1121 if (bio == NULL || time_second > (cdp->timestamp + 10)) {
1122 acd_select_slot(cdp->driver[i]);
1130 bioq_remove(&cdp->bio_queue, bio);
1131 dev = bio->bio_driver_info;
1134 /* reject all queued entries if media changed */
1135 if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1136 bp->b_flags |= B_ERROR;
1143 * Special track access is via the high 8 bits of bio_offset
1144 * (128-254). The first track is 129. 128 is used for direct
1145 * raw CD device access which bypasses the disk layer entirely
1146 * (so e.g. writes by burncd don't error out in the disk layer).
1148 track = (bio->bio_offset >> 56) & 127;
1151 if (track > MAXTRK) {
1152 bp->b_flags |= B_ERROR;
1157 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1158 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1159 lba = (bio->bio_offset & 0x00FFFFFFFFFFFFFFULL) / blocksize;
1160 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1162 blocksize = cdp->block_size;
1163 lastlba = cdp->disk_size;
1164 lba = (bio->bio_offset & 0x00FFFFFFFFFFFFFFULL) / blocksize;
1166 bzero(ccb, sizeof(ccb));
1168 if (bp->b_bcount % blocksize != 0) {
1169 bp->b_flags |= B_ERROR;
1170 bp->b_error = EINVAL;
1174 count = bp->b_bcount / blocksize;
1176 if (bp->b_cmd == BUF_CMD_READ) {
1177 /* if transfer goes beyond range adjust it to be within limits */
1178 if (lba + count > lastlba) {
1179 /* if we are entirely beyond EOM return EOF */
1180 if (lastlba <= lba) {
1181 bp->b_resid = bp->b_bcount;
1185 count = lastlba - lba;
1187 switch (blocksize) {
1189 ccb[0] = ATAPI_READ_BIG;
1193 ccb[0] = ATAPI_READ_CD;
1198 ccb[0] = ATAPI_READ_CD;
1203 ccb[0] = ATAPI_WRITE_BIG;
1214 devstat_start_transaction(cdp->stats);
1215 bio->bio_caller_info1.ptr = cdp;
1216 atapi_queue_cmd(cdp->device, ccb, bp->b_data, count * blocksize,
1217 ((bp->b_cmd == BUF_CMD_READ) ? ATPR_F_READ : 0),
1218 (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30, acd_done, bio);
1222 acd_done(struct atapi_request *request)
1224 struct bio *bio = request->driver;
1225 struct buf *bp = bio->bio_buf;
1226 struct acd_softc *cdp = bio->bio_caller_info1.ptr;
1228 if (request->error) {
1229 bp->b_error = request->error;
1230 bp->b_flags |= B_ERROR;
1232 bp->b_resid = bp->b_bcount - request->donecount;
1234 devstat_end_transaction_buf(cdp->stats, bp);
1240 acd_read_toc(struct acd_softc *cdp)
1242 struct acd_devlist *entry;
1243 int track, ntracks, len;
1247 bzero(&cdp->toc, sizeof(cdp->toc));
1248 bzero(ccb, sizeof(ccb));
1250 if (atapi_test_ready(cdp->device) != 0)
1253 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1255 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1256 ccb[0] = ATAPI_READ_TOC;
1259 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1260 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1261 bzero(&cdp->toc, sizeof(cdp->toc));
1264 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1265 if (ntracks <= 0 || ntracks > MAXTRK) {
1266 bzero(&cdp->toc, sizeof(cdp->toc));
1270 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1271 bzero(ccb, sizeof(ccb));
1272 ccb[0] = ATAPI_READ_TOC;
1275 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1276 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1277 bzero(&cdp->toc, sizeof(cdp->toc));
1280 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1282 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1284 bzero(ccb, sizeof(ccb));
1285 ccb[0] = ATAPI_READ_CAPACITY;
1286 if (atapi_queue_cmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1287 ATPR_F_READ | ATPR_F_QUIET, 30, NULL, NULL)) {
1288 bzero(&cdp->toc, sizeof(cdp->toc));
1289 acd_set_ioparm(cdp);
1292 cdp->disk_size = ntohl(sizes[0]) + 1;
1293 acd_set_ioparm(cdp);
1295 while ((entry = TAILQ_FIRST(&cdp->dev_list))) {
1296 destroy_dev(entry->dev);
1297 TAILQ_REMOVE(&cdp->dev_list, entry, chain);
1298 kfree(entry, M_ACD);
1300 for (track = 1; track <= ntracks; track ++) {
1303 ksprintf(name, "acd%dt%d", cdp->lun, track);
1304 entry = kmalloc(sizeof(struct acd_devlist), M_ACD, M_WAITOK | M_ZERO);
1305 entry->dev = make_dev(&acd_ops,
1306 dkmakepart(track + 128) | dkmakeunit(cdp->lun) |
1307 dkmakeslice(WHOLE_DISK_SLICE),
1308 0, 0, 0644, name, NULL);
1309 entry->dev->si_drv1 = cdp->dev->si_drv1;
1310 reference_dev(entry->dev);
1311 TAILQ_INSERT_TAIL(&cdp->dev_list, entry, chain);
1315 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1316 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1317 cdp->disk_size, cdp->block_size,
1318 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1319 if (cdp->toc.tab[0].control & 4)
1320 kprintf("%dMB\n", cdp->disk_size / 512);
1322 kprintf("%d:%d audio\n",
1323 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1329 acd_play(struct acd_softc *cdp, int start, int end)
1333 bzero(ccb, sizeof(ccb));
1334 ccb[0] = ATAPI_PLAY_MSF;
1335 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1336 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1337 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
1341 acd_setchan(struct acd_softc *cdp,
1342 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1346 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1349 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1351 cdp->au.data_length = 0;
1352 cdp->au.port[0].channels = c0;
1353 cdp->au.port[1].channels = c1;
1354 cdp->au.port[2].channels = c2;
1355 cdp->au.port[3].channels = c3;
1356 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1360 acd_select_done1(struct atapi_request *request)
1362 struct acd_softc *cdp = request->driver;
1364 cdp->changer_info->current_slot = cdp->slot;
1365 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1366 wakeup(&cdp->changer_info);
1371 acd_select_done(struct atapi_request *request)
1373 struct acd_softc *cdp = request->driver;
1374 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1375 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1377 /* load the wanted slot */
1378 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1379 acd_select_done1, cdp);
1384 acd_select_slot(struct acd_softc *cdp)
1386 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1387 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1389 /* unload the current media from player */
1390 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_AT_HEAD, 30,
1391 acd_select_done, cdp);
1395 acd_init_writer(struct acd_softc *cdp, int test_write)
1399 bzero(ccb, sizeof(ccb));
1400 ccb[0] = ATAPI_REZERO;
1401 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60, NULL, NULL);
1402 ccb[0] = ATAPI_SEND_OPC_INFO;
1404 atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 30, NULL, NULL);
1409 acd_fixate(struct acd_softc *cdp, int multisession)
1411 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1412 0, 0, 0, 0, 0, 0, 0, 0 };
1413 int timeout = 5*60*2;
1415 struct write_param param;
1417 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1418 (caddr_t)¶m, sizeof(param))))
1421 param.data_length = 0;
1423 param.session_type = CDR_SESS_MULTI;
1425 param.session_type = CDR_SESS_NONE;
1427 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1430 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1434 /* some drives just return ready, wait for the expected fixate time */
1435 if ((error = atapi_test_ready(cdp->device)) != EBUSY) {
1436 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1437 tsleep(&error, 0, "acdfix", timeout * hz / 2);
1438 return atapi_test_ready(cdp->device);
1441 while (timeout-- > 0) {
1442 if ((error = atapi_test_ready(cdp->device)) != EBUSY)
1444 tsleep(&error, 0, "acdcld", hz/2);
1450 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1452 struct write_param param;
1455 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1456 (caddr_t)¶m, sizeof(param))))
1459 param.data_length = 0;
1460 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1461 param.page_length = 0x32;
1462 param.test_write = track->test_write ? 1 : 0;
1463 param.write_type = CDR_WTYPE_TRACK;
1464 param.session_type = CDR_SESS_NONE;
1466 param.packet_size = 0;
1468 if (cdp->cap.burnproof)
1469 param.burnproof = 1;
1471 switch (track->datablock_type) {
1475 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1477 param.track_mode = CDR_TMODE_AUDIO;
1478 cdp->block_size = 2352;
1479 param.datablock_type = CDR_DB_RAW;
1480 param.session_format = CDR_SESS_CDROM;
1483 case CDR_DB_ROM_MODE1:
1484 cdp->block_size = 2048;
1485 param.track_mode = CDR_TMODE_DATA;
1486 param.datablock_type = CDR_DB_ROM_MODE1;
1487 param.session_format = CDR_SESS_CDROM;
1490 case CDR_DB_ROM_MODE2:
1491 cdp->block_size = 2336;
1492 param.track_mode = CDR_TMODE_DATA;
1493 param.datablock_type = CDR_DB_ROM_MODE2;
1494 param.session_format = CDR_SESS_CDROM;
1497 case CDR_DB_XA_MODE1:
1498 cdp->block_size = 2048;
1499 param.track_mode = CDR_TMODE_DATA;
1500 param.datablock_type = CDR_DB_XA_MODE1;
1501 param.session_format = CDR_SESS_CDROM_XA;
1504 case CDR_DB_XA_MODE2_F1:
1505 cdp->block_size = 2056;
1506 param.track_mode = CDR_TMODE_DATA;
1507 param.datablock_type = CDR_DB_XA_MODE2_F1;
1508 param.session_format = CDR_SESS_CDROM_XA;
1511 case CDR_DB_XA_MODE2_F2:
1512 cdp->block_size = 2324;
1513 param.track_mode = CDR_TMODE_DATA;
1514 param.datablock_type = CDR_DB_XA_MODE2_F2;
1515 param.session_format = CDR_SESS_CDROM_XA;
1518 case CDR_DB_XA_MODE2_MIX:
1519 cdp->block_size = 2332;
1520 param.track_mode = CDR_TMODE_DATA;
1521 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1522 param.session_format = CDR_SESS_CDROM_XA;
1525 acd_set_ioparm(cdp);
1526 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1530 acd_flush(struct acd_softc *cdp)
1532 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1533 0, 0, 0, 0, 0, 0, 0, 0 };
1535 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, ATPR_F_QUIET, 60,
1540 acd_read_track_info(struct acd_softc *cdp,
1541 int32_t lba, struct acd_track_info *info)
1543 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1544 lba>>24, lba>>16, lba>>8, lba,
1546 sizeof(*info)>>8, sizeof(*info),
1547 0, 0, 0, 0, 0, 0, 0 };
1550 if ((error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1551 ATPR_F_READ, 30, NULL, NULL)))
1553 info->track_start_addr = ntohl(info->track_start_addr);
1554 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1555 info->free_blocks = ntohl(info->free_blocks);
1556 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1557 info->track_length = ntohl(info->track_length);
1562 acd_get_progress(struct acd_softc *cdp, int *finished)
1564 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1565 0, 0, 0, 0, 0, 0, 0, 0 };
1566 struct atapi_reqsense *sense = cdp->device->result;
1569 if (atapi_test_ready(cdp->device) != EBUSY) {
1570 if (atapi_queue_cmd(cdp->device, ccb, tmp, sizeof(tmp),
1571 ATPR_F_READ, 30, NULL, NULL) != EBUSY) {
1578 ((sense->sk_specific2 | (sense->sk_specific1 << 8)) * 100) / 65535;
1585 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1587 struct write_param param;
1588 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1589 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1590 0, 0, 0, 0, 0, 0, 0 };
1597 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1598 (caddr_t)¶m, sizeof(param))))
1600 param.data_length = 0;
1601 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1602 param.page_length = 0x32;
1603 param.test_write = cuesheet->test_write ? 1 : 0;
1604 param.write_type = CDR_WTYPE_SESSION;
1605 param.session_type = cuesheet->session_type;
1607 param.packet_size = 0;
1608 param.track_mode = CDR_TMODE_AUDIO;
1609 param.datablock_type = CDR_DB_RAW;
1610 param.session_format = cuesheet->session_format;
1611 if (cdp->cap.burnproof)
1612 param.burnproof = 1;
1613 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1616 buffer = kmalloc(cuesheet->len, M_ACD, M_WAITOK);
1617 if ((error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1620 kprintf("acd: cuesheet lenght = %d\n", cuesheet->len);
1621 for (i=0; i<cuesheet->len; i++)
1623 kprintf(" %02x", buffer[i]);
1625 kprintf("\n%02x", buffer[i]);
1628 error = atapi_queue_cmd(cdp->device, ccb, buffer, cuesheet->len, 0,
1630 kfree(buffer, M_ACD);
1635 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1637 struct dvd_miscauth *d;
1643 /* this is common even for ai->format == DVD_INVALIDATE_AGID */
1644 bzero(ccb, sizeof(ccb));
1645 ccb[0] = ATAPI_REPORT_KEY;
1646 ccb[2] = (lba >> 24) & 0xff;
1647 ccb[3] = (lba >> 16) & 0xff;
1648 ccb[4] = (lba >> 8) & 0xff;
1649 ccb[5] = lba & 0xff;
1650 ccb[10] = (ai->agid << 6) | ai->format;
1652 switch (ai->format) {
1653 case DVD_REPORT_AGID:
1654 case DVD_REPORT_ASF:
1655 case DVD_REPORT_RPC:
1658 case DVD_REPORT_KEY1:
1661 case DVD_REPORT_TITLE_KEY:
1665 case DVD_REPORT_CHALLENGE:
1668 case DVD_INVALIDATE_AGID:
1669 return(atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 10, NULL, NULL));
1674 ccb[8] = (length >> 8) & 0xff;
1675 ccb[9] = length & 0xff;
1677 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1678 d->length = htons(length - 2);
1680 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length,
1681 ATPR_F_READ, 10, NULL, NULL);
1687 switch (ai->format) {
1688 case DVD_REPORT_AGID:
1689 ai->agid = d->data[3] >> 6;
1692 case DVD_REPORT_CHALLENGE:
1693 bcopy(&d->data[0], &ai->keychal[0], 10);
1696 case DVD_REPORT_KEY1:
1697 bcopy(&d->data[0], &ai->keychal[0], 5);
1700 case DVD_REPORT_TITLE_KEY:
1701 ai->cpm = (d->data[0] >> 7);
1702 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1703 ai->cgms = (d->data[0] >> 4) & 0x3;
1704 bcopy(&d->data[1], &ai->keychal[0], 5);
1707 case DVD_REPORT_ASF:
1708 ai->asf = d->data[3] & 1;
1711 case DVD_REPORT_RPC:
1712 ai->reg_type = (d->data[0] >> 6);
1713 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1714 ai->user_rsts = d->data[0] & 0x7;
1715 ai->region = d->data[1];
1716 ai->rpc_scheme = d->data[2];
1719 case DVD_INVALIDATE_AGID:
1731 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1733 struct dvd_miscauth *d;
1738 switch (ai->format) {
1739 case DVD_SEND_CHALLENGE:
1741 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1742 bcopy(ai->keychal, &d->data[0], 10);
1747 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1748 bcopy(&ai->keychal[0], &d->data[0], 5);
1753 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1754 d->data[0] = ai->region;
1761 bzero(ccb, sizeof(ccb));
1762 ccb[0] = ATAPI_SEND_KEY;
1763 ccb[8] = (length >> 8) & 0xff;
1764 ccb[9] = length & 0xff;
1765 ccb[10] = (ai->agid << 6) | ai->format;
1766 d->length = htons(length - 2);
1767 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, 0,
1774 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1776 struct dvd_miscauth *d;
1782 case DVD_STRUCT_PHYSICAL:
1786 case DVD_STRUCT_COPYRIGHT:
1790 case DVD_STRUCT_DISCKEY:
1794 case DVD_STRUCT_BCA:
1798 case DVD_STRUCT_MANUFACT:
1802 case DVD_STRUCT_DDS:
1803 case DVD_STRUCT_PRERECORDED:
1804 case DVD_STRUCT_UNIQUEID:
1805 case DVD_STRUCT_LIST:
1806 case DVD_STRUCT_CMI:
1807 case DVD_STRUCT_RMD_LAST:
1808 case DVD_STRUCT_RMD_RMA:
1809 case DVD_STRUCT_DCB:
1816 d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1817 d->length = htons(length - 2);
1819 bzero(ccb, sizeof(ccb));
1820 ccb[0] = ATAPI_READ_STRUCTURE;
1821 ccb[6] = s->layer_num;
1823 ccb[8] = (length >> 8) & 0xff;
1824 ccb[9] = length & 0xff;
1825 ccb[10] = s->agid << 6;
1826 error = atapi_queue_cmd(cdp->device, ccb, (caddr_t)d, length, ATPR_F_READ,
1833 switch (s->format) {
1834 case DVD_STRUCT_PHYSICAL: {
1835 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1837 layer->book_type = d->data[0] >> 4;
1838 layer->book_version = d->data[0] & 0xf;
1839 layer->disc_size = d->data[1] >> 4;
1840 layer->max_rate = d->data[1] & 0xf;
1841 layer->nlayers = (d->data[2] >> 5) & 3;
1842 layer->track_path = (d->data[2] >> 4) & 1;
1843 layer->layer_type = d->data[2] & 0xf;
1844 layer->linear_density = d->data[3] >> 4;
1845 layer->track_density = d->data[3] & 0xf;
1846 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1847 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1848 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1849 layer->bca = d->data[16] >> 7;
1853 case DVD_STRUCT_COPYRIGHT:
1854 s->cpst = d->data[0];
1855 s->rmi = d->data[0];
1858 case DVD_STRUCT_DISCKEY:
1859 bcopy(&d->data[0], &s->data[0], 2048);
1862 case DVD_STRUCT_BCA:
1863 s->length = ntohs(d->length);
1864 bcopy(&d->data[0], &s->data[0], s->length);
1867 case DVD_STRUCT_MANUFACT:
1868 s->length = ntohs(d->length);
1869 bcopy(&d->data[0], &s->data[0], s->length);
1880 acd_eject(struct acd_softc *cdp, int close)
1884 if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
1887 if ((error = acd_start_stop(cdp, 3)))
1890 acd_prevent_allow(cdp, 1);
1891 cdp->flags |= F_LOCKED;
1898 acd_prevent_allow(cdp, 0);
1899 cdp->flags &= ~F_LOCKED;
1900 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1901 return acd_start_stop(cdp, 2);
1905 acd_blank(struct acd_softc *cdp, int blanktype)
1907 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1908 0, 0, 0, 0, 0, 0, 0, 0 };
1910 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1911 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1915 acd_prevent_allow(struct acd_softc *cdp, int lock)
1917 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1918 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1920 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1924 acd_start_stop(struct acd_softc *cdp, int start)
1926 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1927 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1929 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1933 acd_pause_resume(struct acd_softc *cdp, int pause)
1935 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1936 0, 0, 0, 0, 0, 0, 0 };
1938 return atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1942 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1944 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1945 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1948 error = atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
1951 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1957 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1959 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1960 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1963 ata_prtdev(cdp->device,
1964 "modeselect pagesize=%d\n", pagesize);
1965 atapi_dump("mode select ", pagebuf, pagesize);
1967 return atapi_queue_cmd(cdp->device, ccb, pagebuf, pagesize, 0,
1972 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
1974 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1975 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1978 error = atapi_queue_cmd(cdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
1985 acd_get_cap(struct acd_softc *cdp)
1989 /* get drive capabilities, some drives needs this repeated */
1990 while (retry-- && acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
1991 (caddr_t)&cdp->cap, sizeof(cdp->cap)))
1993 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1994 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1995 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1996 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed), 177);
1997 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1998 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);