2 * Copyright (c) 1998 - 2004 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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
42 #include <sys/cdrio.h>
43 #include <sys/dvdio.h>
45 #include <sys/fcntl.h>
47 #include <sys/ctype.h>
49 #include <sys/taskqueue.h>
51 #include <machine/bus.h>
52 #include <geom/geom.h>
53 #include <dev/ata/ata-all.h>
54 #include <dev/ata/atapi-cd.h>
57 static void acd_detach(struct ata_device *);
58 static void acd_start(struct ata_device *);
59 static struct acd_softc *acd_init_lun(struct ata_device *);
60 static void acd_geom_create(void *, int);
61 static void acd_set_ioparm(struct acd_softc *);
62 static void acd_describe(struct acd_softc *);
63 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
64 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
65 static int acd_geom_access(struct g_provider *, int, int, int);
66 static g_ioctl_t acd_geom_ioctl;
67 static void acd_geom_start(struct bio *);
68 static void acd_done(struct ata_request *);
69 static void acd_read_toc(struct acd_softc *);
70 static int acd_play(struct acd_softc *, int, int);
71 static int acd_setchan(struct acd_softc *, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
72 static void acd_select_slot(struct acd_softc *);
73 static int acd_init_writer(struct acd_softc *, int);
74 static int acd_fixate(struct acd_softc *, int);
75 static int acd_init_track(struct acd_softc *, struct cdr_track *);
76 static int acd_flush(struct acd_softc *);
77 static int acd_read_track_info(struct acd_softc *, int32_t, struct acd_track_info *);
78 static int acd_get_progress(struct acd_softc *, int *);
79 static int acd_send_cue(struct acd_softc *, struct cdr_cuesheet *);
80 static int acd_report_key(struct acd_softc *, struct dvd_authinfo *);
81 static int acd_send_key(struct acd_softc *, struct dvd_authinfo *);
82 static int acd_read_structure(struct acd_softc *, struct dvd_struct *);
83 static int acd_tray(struct acd_softc *, int);
84 static int acd_blank(struct acd_softc *, int);
85 static int acd_prevent_allow(struct acd_softc *, int);
86 static int acd_start_stop(struct acd_softc *, int);
87 static int acd_pause_resume(struct acd_softc *, int);
88 static int acd_mode_sense(struct acd_softc *, int, caddr_t, int);
89 static int acd_mode_select(struct acd_softc *, caddr_t, int);
90 static int acd_set_speed(struct acd_softc *, int, int);
91 static void acd_get_cap(struct acd_softc *);
92 static int acd_read_format_caps(struct acd_softc *, struct cdr_format_capacities *);
93 static int acd_format(struct acd_softc *, struct cdr_format_params *);
94 static int acd_test_ready(struct ata_device *);
97 static u_int32_t acd_lun_map = 0;
98 static MALLOC_DEFINE(M_ACD, "ACD driver", "ATAPI CD driver buffers");
99 static struct g_class acd_class = {
101 .version = G_VERSION,
102 .access = acd_geom_access,
103 .ioctl = acd_geom_ioctl,
104 .start = acd_geom_start,
106 DECLARE_GEOM_CLASS(acd_class, acd);
109 acd_attach(struct ata_device *atadev)
111 struct acd_softc *cdp;
114 if ((cdp = acd_init_lun(atadev)) == NULL) {
115 ata_prtdev(atadev, "out of memory\n");
119 ata_set_name(atadev, "acd", cdp->lun);
122 /* if this is a changer device, allocate the neeeded lun's */
123 if ((cdp->cap.mechanism & MST_MECH_MASK) == MST_MECH_CHANGER) {
124 int8_t ccb[16] = { ATAPI_MECH_STATUS, 0, 0, 0, 0, 0, 0, 0,
125 sizeof(struct changer)>>8, sizeof(struct changer),
128 if (!(chp = malloc(sizeof(struct changer), M_ACD, M_NOWAIT | M_ZERO))) {
129 ata_prtdev(atadev, "out of memory\n");
133 if (!ata_atapicmd(cdp->device, ccb, (caddr_t)chp,
134 sizeof(struct changer), ATA_R_READ, 60)) {
135 struct acd_softc *tmpcdp = cdp;
136 struct acd_softc **cdparr;
140 chp->table_length = htons(chp->table_length);
141 if (!(cdparr = malloc(sizeof(struct acd_softc) * chp->slots,
143 ata_prtdev(atadev, "out of memory\n");
148 for (count = 0; count < chp->slots; count++) {
150 tmpcdp = acd_init_lun(atadev);
152 ata_prtdev(atadev, "out of memory\n");
156 cdparr[count] = tmpcdp;
157 tmpcdp->driver = cdparr;
158 tmpcdp->slot = count;
159 tmpcdp->changer_info = chp;
160 g_post_event(acd_geom_create, tmpcdp, M_WAITOK, NULL);
162 if (!(name = malloc(strlen(atadev->name) + 2, M_ACD, M_NOWAIT))) {
163 ata_prtdev(atadev, "out of memory\n");
167 strcpy(name, atadev->name);
169 ata_free_name(atadev);
170 ata_set_name(atadev, name, cdp->lun + cdp->changer_info->slots - 1);
175 g_post_event(acd_geom_create, cdp, M_WAITOK, NULL);
177 /* setup the function ptrs */
178 atadev->detach = acd_detach;
179 atadev->start = acd_start;
182 /* announce we are here */
187 acd_geom_detach(void *arg, int flag)
189 struct ata_device *atadev = arg;
190 struct acd_softc *cdp = atadev->softc;
193 g_wither_geom(cdp->gp, ENXIO);
194 if (cdp->changer_info) {
195 for (subdev = 0; subdev < cdp->changer_info->slots; subdev++) {
196 if (cdp->driver[subdev] == cdp)
198 mtx_lock(&cdp->driver[subdev]->queue_mtx);
199 bioq_flush(&cdp->driver[subdev]->queue, NULL, ENXIO);
200 mtx_unlock(&cdp->driver[subdev]->queue_mtx);
201 ata_free_lun(&acd_lun_map, cdp->driver[subdev]->lun);
202 free(cdp->driver[subdev], M_ACD);
204 free(cdp->driver, M_ACD);
205 free(cdp->changer_info, M_ACD);
207 mtx_lock(&cdp->queue_mtx);
208 bioq_flush(&cdp->queue, NULL, ENXIO);
209 mtx_unlock(&cdp->queue_mtx);
210 mtx_destroy(&cdp->queue_mtx);
211 ata_prtdev(atadev, "WARNING - removed from configuration\n");
212 ata_free_name(atadev);
213 ata_free_lun(&acd_lun_map, cdp->lun);
214 atadev->attach = NULL;
215 atadev->detach = NULL;
216 atadev->start = NULL;
217 atadev->softc = NULL;
223 acd_detach(struct ata_device *atadev)
225 g_waitfor_event(acd_geom_detach, atadev, M_WAITOK, NULL);
228 static struct acd_softc *
229 acd_init_lun(struct ata_device *atadev)
231 struct acd_softc *cdp;
233 if (!(cdp = malloc(sizeof(struct acd_softc), M_ACD, M_NOWAIT | M_ZERO)))
235 bioq_init(&cdp->queue);
236 mtx_init(&cdp->queue_mtx, "ATAPI CD bioqueue lock", NULL, MTX_DEF);
237 cdp->device = atadev;
238 cdp->lun = ata_get_lun(&acd_lun_map);
239 cdp->block_size = 2048;
241 cdp->changer_info = NULL;
246 acd_geom_create(void *arg, int flag)
248 struct acd_softc *cdp;
250 struct g_provider *pp;
254 gp = g_new_geomf(&acd_class, "acd%d", cdp->lun);
257 pp = g_new_providerf(gp, "acd%d", cdp->lun);
260 g_error_provider(pp, 0);
261 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
266 acd_set_ioparm(struct acd_softc *cdp)
269 if (cdp->device->channel->dma)
270 cdp->iomax = min(cdp->device->channel->dma->max_iosize, 65534);
272 cdp->iomax = min(DFLTPHYS, 65534);
276 acd_describe(struct acd_softc *cdp)
282 ata_prtdev(cdp->device, "<%.40s/%.8s> %s drive at ata%d as %s\n",
283 cdp->device->param->model, cdp->device->param->revision,
284 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
285 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
286 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
287 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
288 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM" : "CDROM",
289 device_get_unit(cdp->device->channel->dev),
290 (cdp->device->unit == ATA_MASTER) ? "master" : "slave");
292 ata_prtdev(cdp->device, "%s", "");
293 if (cdp->cap.cur_read_speed) {
294 printf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
295 if (cdp->cap.max_read_speed)
296 printf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
297 if ((cdp->cap.cur_write_speed) &&
298 (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
299 MST_WRITE_DVDR | MST_WRITE_DVDRAM))) {
300 printf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
301 if (cdp->cap.max_write_speed)
302 printf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
306 if (cdp->cap.buf_size) {
307 printf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
310 printf("%s %s\n", comma ? "," : "", ata_mode2str(cdp->device->mode));
312 ata_prtdev(cdp->device, "Reads:");
314 if (cdp->cap.media & MST_READ_CDR) {
315 printf(" CDR"); comma = 1;
317 if (cdp->cap.media & MST_READ_CDRW) {
318 printf("%s CDRW", comma ? "," : ""); comma = 1;
320 if (cdp->cap.capabilities & MST_READ_CDDA) {
321 if (cdp->cap.capabilities & MST_CDDA_STREAM)
322 printf("%s CDDA stream", comma ? "," : "");
324 printf("%s CDDA", comma ? "," : "");
327 if (cdp->cap.media & MST_READ_DVDROM) {
328 printf("%s DVDROM", comma ? "," : ""); comma = 1;
330 if (cdp->cap.media & MST_READ_DVDR) {
331 printf("%s DVDR", comma ? "," : ""); comma = 1;
333 if (cdp->cap.media & MST_READ_DVDRAM) {
334 printf("%s DVDRAM", comma ? "," : ""); comma = 1;
336 if (cdp->cap.media & MST_READ_PACKET)
337 printf("%s packet", comma ? "," : "");
340 ata_prtdev(cdp->device, "Writes:");
341 if (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
342 MST_WRITE_DVDR | MST_WRITE_DVDRAM)) {
344 if (cdp->cap.media & MST_WRITE_CDR) {
345 printf(" CDR" ); comma = 1;
347 if (cdp->cap.media & MST_WRITE_CDRW) {
348 printf("%s CDRW", comma ? "," : ""); comma = 1;
350 if (cdp->cap.media & MST_WRITE_DVDR) {
351 printf("%s DVDR", comma ? "," : ""); comma = 1;
353 if (cdp->cap.media & MST_WRITE_DVDRAM) {
354 printf("%s DVDRAM", comma ? "," : ""); comma = 1;
356 if (cdp->cap.media & MST_WRITE_TEST) {
357 printf("%s test write", comma ? "," : ""); comma = 1;
359 if (cdp->cap.capabilities & MST_BURNPROOF)
360 printf("%s burnproof", comma ? "," : "");
363 if (cdp->cap.capabilities & MST_AUDIO_PLAY) {
364 ata_prtdev(cdp->device, "Audio: ");
365 if (cdp->cap.capabilities & MST_AUDIO_PLAY)
367 if (cdp->cap.max_vol_levels)
368 printf(", %d volume levels", cdp->cap.max_vol_levels);
371 ata_prtdev(cdp->device, "Mechanism: ");
372 switch (cdp->cap.mechanism & MST_MECH_MASK) {
374 mechanism = "caddy"; break;
376 mechanism = "tray"; break;
378 mechanism = "popup"; break;
379 case MST_MECH_CHANGER:
380 mechanism = "changer"; break;
381 case MST_MECH_CARTRIDGE:
382 mechanism = "cartridge"; break;
384 mechanism = 0; break;
387 printf("%s%s", (cdp->cap.mechanism & MST_EJECT) ?
388 "ejectable " : "", mechanism);
389 else if (cdp->cap.mechanism & MST_EJECT)
392 if (cdp->cap.mechanism & MST_LOCKABLE)
393 printf((cdp->cap.mechanism & MST_LOCKED) ? ", locked":", unlocked");
394 if (cdp->cap.mechanism & MST_PREVENT)
395 printf(", lock protected");
398 if ((cdp->cap.mechanism & MST_MECH_MASK) != MST_MECH_CHANGER) {
399 ata_prtdev(cdp->device, "Medium: ");
400 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
402 printf("CD-ROM "); break;
404 printf("CD-R "); break;
406 printf("CD-RW "); break;
408 printf("door open"); break;
410 printf("no/blank disc"); break;
412 printf("medium format error"); break;
414 if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
415 switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
417 printf("120mm data disc"); break;
419 printf("120mm audio disc"); break;
421 printf("120mm data/audio disc"); break;
423 printf("120mm photo disc"); break;
425 printf("80mm data disc"); break;
427 printf("80mm audio disc"); break;
429 printf("80mm data/audio disc"); break;
431 printf("80mm photo disc"); break;
433 switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
435 printf("unknown"); break;
438 printf("blank"); break;
442 printf("unknown (0x%x)", cdp->cap.medium_type); break;
449 ata_prtdev(cdp->device, "%s ",
450 (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
451 (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
452 (cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
453 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
454 (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM" : "CDROM");
455 if (cdp->changer_info)
456 printf("with %d CD changer ", cdp->changer_info->slots);
457 printf("<%.40s/%.8s> at ata%d-%s %s\n",
458 cdp->device->param->model, cdp->device->param->revision,
459 device_get_unit(cdp->device->channel->dev),
460 (cdp->device->unit == ATA_MASTER) ? "master" : "slave",
461 ata_mode2str(cdp->device->mode) );
466 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
470 *m = lba / (60 * 75);
476 static __inline u_int32_t
477 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
479 return (m * 60 + s) * 75 + f - 150;
483 acd_geom_access(struct g_provider *pp, int dr, int dw, int de)
485 struct acd_softc *cdp;
486 struct ata_request *request;
487 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
488 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
489 int timeout = 60, track;
492 cdp = pp->geom->softc;
493 if (cdp->device->flags & ATA_D_DETACHING)
496 if (!(request = ata_alloc_request()))
499 /* wait if drive is not finished loading the medium */
501 bzero(request, sizeof(struct ata_request));
502 request->device = cdp->device;
503 request->driver = cdp;
504 bcopy(ccb, request->u.atapi.ccb, 16);
505 request->flags = ATA_R_ATAPI;
506 request->timeout = 5;
507 ata_queue_request(request);
508 if (!request->error &&
509 (request->u.atapi.sense_data.sense_key == 2 ||
510 request->u.atapi.sense_data.sense_key == 7) &&
511 request->u.atapi.sense_data.asc == 4 &&
512 request->u.atapi.sense_data.ascq == 1)
513 tsleep(&timeout, PRIBIO, "acdld", hz / 2);
517 ata_free_request(request);
520 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
521 acd_select_slot(cdp);
522 tsleep(&cdp->changer_info, PRIBIO, "acdopn", 0);
524 acd_prevent_allow(cdp, 1);
525 cdp->flags |= F_LOCKED;
529 if (dr + pp->acr == 0) {
530 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
531 acd_select_slot(cdp);
532 tsleep(&cdp->changer_info, PRIBIO, "acdclo", 0);
534 acd_prevent_allow(cdp, 0);
535 cdp->flags &= ~F_LOCKED;
538 if ((track = pp->index)) {
539 pp->sectorsize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
540 pp->mediasize = ntohl(cdp->toc.tab[track].addr.lba) -
541 ntohl(cdp->toc.tab[track - 1].addr.lba);
544 pp->sectorsize = cdp->block_size;
545 pp->mediasize = cdp->disk_size;
547 pp->mediasize *= pp->sectorsize;
553 acd_geom_ioctl(struct g_provider *pp, u_long cmd, void *addr, int fflag, struct thread *td)
555 struct acd_softc *cdp = pp->geom->softc;
556 int error = 0, nocopyout;
561 if (cdp->changer_info && cdp->slot != cdp->changer_info->current_slot) {
562 acd_select_slot(cdp);
563 tsleep(&cdp->changer_info, PRIBIO, "acdctl", 0);
565 if (cdp->device->flags & ATA_D_MEDIA_CHANGED)
568 acd_test_ready(cdp->device);
573 acd_prevent_allow(cdp, 1);
574 cdp->flags |= F_LOCKED;
581 error = acd_pause_resume(cdp, 1);
585 error = acd_pause_resume(cdp, 0);
589 error = acd_start_stop(cdp, 1);
593 error = acd_start_stop(cdp, 0);
597 error = acd_prevent_allow(cdp, 0);
598 cdp->flags &= ~F_LOCKED;
602 error = acd_prevent_allow(cdp, 1);
603 cdp->flags |= F_LOCKED;
610 error = acd_test_ready(cdp->device);
618 error = acd_tray(cdp, 0);
624 error = acd_tray(cdp, 1);
627 case CDIOREADTOCHEADER:
628 if (!cdp->toc.hdr.ending_track) {
632 bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr));
635 case CDIOREADTOCENTRYS:
637 struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr;
638 struct toc *toc = &cdp->toc;
639 int starting_track = te->starting_track;
642 if (!toc->hdr.ending_track) {
647 if (te->data_len < sizeof(toc->tab[0]) ||
648 (te->data_len % sizeof(toc->tab[0])) != 0 ||
649 (te->address_format != CD_MSF_FORMAT &&
650 te->address_format != CD_LBA_FORMAT)) {
656 starting_track = toc->hdr.starting_track;
657 else if (starting_track == 170)
658 starting_track = toc->hdr.ending_track + 1;
659 else if (starting_track < toc->hdr.starting_track ||
660 starting_track > toc->hdr.ending_track + 1) {
665 len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
667 if (te->data_len < len)
669 if (len > sizeof(toc->tab)) {
674 if (te->address_format == CD_MSF_FORMAT) {
675 struct cd_toc_entry *entry;
677 if (!(toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT))) {
681 bcopy(&cdp->toc, toc, sizeof(struct toc));
682 entry = toc->tab + (toc->hdr.ending_track + 1 -
683 toc->hdr.starting_track) + 1;
684 while (--entry >= toc->tab)
685 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
686 &entry->addr.msf.second, &entry->addr.msf.frame);
688 error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
690 if (te->address_format == CD_MSF_FORMAT)
695 case CDIOREADTOCENTRY:
697 struct ioc_read_toc_single_entry *te =
698 (struct ioc_read_toc_single_entry *)addr;
699 struct toc *toc = &cdp->toc;
700 u_char track = te->track;
702 if (!toc->hdr.ending_track) {
707 if (te->address_format != CD_MSF_FORMAT &&
708 te->address_format != CD_LBA_FORMAT) {
714 track = toc->hdr.starting_track;
715 else if (track == 170)
716 track = toc->hdr.ending_track + 1;
717 else if (track < toc->hdr.starting_track ||
718 track > toc->hdr.ending_track + 1) {
723 if (te->address_format == CD_MSF_FORMAT) {
724 struct cd_toc_entry *entry;
726 if (!(toc = malloc(sizeof(struct toc), M_ACD, M_NOWAIT))) {
730 bcopy(&cdp->toc, toc, sizeof(struct toc));
731 entry = toc->tab + (track - toc->hdr.starting_track);
732 lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
733 &entry->addr.msf.second, &entry->addr.msf.frame);
735 bcopy(toc->tab + track - toc->hdr.starting_track,
736 &te->entry, sizeof(struct cd_toc_entry));
737 if (te->address_format == CD_MSF_FORMAT)
742 case CDIOCREADSUBCHANNEL_SYSSPACE:
745 case CDIOCREADSUBCHANNEL:
747 struct ioc_read_subchannel *args =
748 (struct ioc_read_subchannel *)addr;
750 int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
751 sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
752 0, 0, 0, 0, 0, 0, 0 };
754 if (args->data_len > sizeof(struct cd_sub_channel_info) ||
755 args->data_len < sizeof(struct cd_sub_channel_header)) {
760 format = args->data_format;
761 if ((format != CD_CURRENT_POSITION) &&
762 (format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
767 ccb[1] = args->address_format & CD_MSF_FORMAT;
769 if ((error = ata_atapicmd(cdp->device, ccb, (caddr_t)&cdp->subchan,
770 sizeof(cdp->subchan), ATA_R_READ, 10)))
773 if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
774 if (cdp->subchan.header.audio_status == 0x11) {
780 if (format == CD_TRACK_INFO)
781 ccb[6] = args->track;
783 if ((error = ata_atapicmd(cdp->device, ccb,
784 (caddr_t)&cdp->subchan,
785 sizeof(cdp->subchan),ATA_R_READ,10))){
789 if (nocopyout == 0) {
790 error = copyout(&cdp->subchan, args->data, args->data_len);
793 bcopy(&cdp->subchan, args->data, args->data_len);
800 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
804 msf2lba(args->start_m, args->start_s, args->start_f),
805 msf2lba(args->end_m, args->end_s, args->end_f));
809 case CDIOCPLAYBLOCKS:
811 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
813 error = acd_play(cdp, args->blk, args->blk + args->len);
817 case CDIOCPLAYTRACKS:
819 struct ioc_play_track *args = (struct ioc_play_track *)addr;
822 if (!cdp->toc.hdr.ending_track) {
826 if (args->end_track < cdp->toc.hdr.ending_track + 1)
828 if (args->end_track > cdp->toc.hdr.ending_track + 1)
829 args->end_track = cdp->toc.hdr.ending_track + 1;
830 t1 = args->start_track - cdp->toc.hdr.starting_track;
831 t2 = args->end_track - cdp->toc.hdr.starting_track;
832 if (t1 < 0 || t2 < 0 ||
833 t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
837 error = acd_play(cdp, ntohl(cdp->toc.tab[t1].addr.lba),
838 ntohl(cdp->toc.tab[t2].addr.lba));
844 struct ioc_vol *arg = (struct ioc_vol *)addr;
846 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
847 (caddr_t)&cdp->au, sizeof(cdp->au))))
850 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
854 arg->vol[0] = cdp->au.port[0].volume;
855 arg->vol[1] = cdp->au.port[1].volume;
856 arg->vol[2] = cdp->au.port[2].volume;
857 arg->vol[3] = cdp->au.port[3].volume;
863 struct ioc_vol *arg = (struct ioc_vol *)addr;
865 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE,
866 (caddr_t)&cdp->au, sizeof(cdp->au))))
868 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
872 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE_MASK,
873 (caddr_t)&cdp->aumask,
874 sizeof(cdp->aumask))))
876 cdp->au.data_length = 0;
877 cdp->au.port[0].channels = CHANNEL_0;
878 cdp->au.port[1].channels = CHANNEL_1;
879 cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
880 cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
881 cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
882 cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
883 error = acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
889 struct ioc_patch *arg = (struct ioc_patch *)addr;
891 error = acd_setchan(cdp, arg->patch[0], arg->patch[1],
892 arg->patch[2], arg->patch[3]);
897 error = acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
901 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0);
905 error = acd_setchan(cdp, 0, 0, 0, 0);
909 error = acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0);
913 error = acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0);
917 error = acd_blank(cdp, (*(int *)addr));
920 case CDRIOCNEXTWRITEABLEADDR:
922 struct acd_track_info track_info;
924 if ((error = acd_read_track_info(cdp, 0xff, &track_info)))
927 if (!track_info.nwa_valid) {
931 *(int*)addr = track_info.next_writeable_addr;
935 case CDRIOCINITWRITER:
936 error = acd_init_writer(cdp, (*(int *)addr));
939 case CDRIOCINITTRACK:
940 error = acd_init_track(cdp, (struct cdr_track *)addr);
944 error = acd_flush(cdp);
948 error = acd_fixate(cdp, (*(int *)addr));
951 case CDRIOCREADSPEED:
953 int speed = *(int *)addr;
955 /* Preserve old behavior: units in multiples of CDROM speed */
958 error = acd_set_speed(cdp, speed, CDR_MAX_SPEED);
962 case CDRIOCWRITESPEED:
964 int speed = *(int *)addr;
968 error = acd_set_speed(cdp, CDR_MAX_SPEED, speed);
972 case CDRIOCGETBLOCKSIZE:
973 *(int *)addr = cdp->block_size;
976 case CDRIOCSETBLOCKSIZE:
977 cdp->block_size = *(int *)addr;
978 pp->sectorsize = cdp->block_size; /* hack for GEOM SOS */
982 case CDRIOCGETPROGRESS:
983 error = acd_get_progress(cdp, (int *)addr);
987 error = acd_send_cue(cdp, (struct cdr_cuesheet *)addr);
990 case CDRIOCREADFORMATCAPS:
991 error = acd_read_format_caps(cdp, (struct cdr_format_capacities *)addr);
995 error = acd_format(cdp, (struct cdr_format_params *)addr);
998 case DVDIOCREPORTKEY:
999 if (cdp->cap.media & MST_READ_DVDROM)
1000 error = acd_report_key(cdp, (struct dvd_authinfo *)addr);
1006 if (cdp->cap.media & MST_READ_DVDROM)
1007 error = acd_send_key(cdp, (struct dvd_authinfo *)addr);
1012 case DVDIOCREADSTRUCTURE:
1013 if (cdp->cap.media & MST_READ_DVDROM)
1014 error = acd_read_structure(cdp, (struct dvd_struct *)addr);
1026 acd_geom_start(struct bio *bp)
1028 struct acd_softc *cdp = bp->bio_to->geom->softc;
1030 if (cdp->device->flags & ATA_D_DETACHING) {
1031 g_io_deliver(bp, ENXIO);
1035 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE) {
1036 g_io_deliver(bp, EOPNOTSUPP);
1040 if (bp->bio_cmd == BIO_READ && cdp->disk_size == -1) {
1041 g_io_deliver(bp, EIO);
1045 /* GEOM classes must do their own request limiting */
1046 if (bp->bio_length <= cdp->iomax) {
1047 mtx_lock(&cdp->queue_mtx);
1048 bp->bio_pblkno = bp->bio_offset / bp->bio_to->sectorsize;
1049 bioq_disksort(&cdp->queue, bp);
1050 mtx_unlock(&cdp->queue_mtx);
1053 u_int pos, size = cdp->iomax - cdp->iomax % bp->bio_to->sectorsize;
1056 for (pos = 0; pos < bp->bio_length; pos += size) {
1057 if (!(bp2 = g_clone_bio(bp))) {
1058 bp->bio_error = ENOMEM;
1061 bp2->bio_done = g_std_done;
1062 bp2->bio_to = bp->bio_to;
1063 bp2->bio_offset += pos;
1064 bp2->bio_data += pos;
1065 bp2->bio_length = MIN(size, bp->bio_length - pos);
1066 mtx_lock(&cdp->queue_mtx);
1067 bp2->bio_pblkno = bp2->bio_offset / bp2->bio_to->sectorsize;
1068 bioq_disksort(&cdp->queue, bp2);
1069 mtx_unlock(&cdp->queue_mtx);
1072 ata_start(cdp->device->channel);
1076 acd_start(struct ata_device *atadev)
1078 struct acd_softc *cdp = atadev->softc;
1080 struct ata_request *request;
1081 u_int32_t lba, lastlba, count;
1083 int track, blocksize;
1085 if (cdp->changer_info) {
1088 cdp = cdp->driver[cdp->changer_info->current_slot];
1089 mtx_lock(&cdp->queue_mtx);
1090 bp = bioq_first(&cdp->queue);
1091 mtx_unlock(&cdp->queue_mtx);
1093 /* check for work pending on any other slot */
1094 for (i = 0; i < cdp->changer_info->slots; i++) {
1095 if (i == cdp->changer_info->current_slot)
1097 mtx_lock(&cdp->queue_mtx);
1098 if (bioq_first(&(cdp->driver[i]->queue))) {
1099 if (!bp || time_second > (cdp->timestamp + 10)) {
1100 mtx_unlock(&cdp->queue_mtx);
1101 acd_select_slot(cdp->driver[i]);
1105 mtx_unlock(&cdp->queue_mtx);
1109 mtx_lock(&cdp->queue_mtx);
1110 bp = bioq_first(&cdp->queue);
1112 bioq_remove(&cdp->queue, bp);
1113 mtx_unlock(&cdp->queue_mtx);
1117 /* reject all queued entries if media changed */
1118 if (cdp->device->flags & ATA_D_MEDIA_CHANGED) {
1119 g_io_deliver(bp, EIO);
1123 bzero(ccb, sizeof(ccb));
1125 track = bp->bio_to->index;
1128 blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
1129 lastlba = ntohl(cdp->toc.tab[track].addr.lba);
1130 lba = bp->bio_offset / blocksize;
1131 lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
1134 blocksize = cdp->block_size;
1135 lastlba = cdp->disk_size;
1136 lba = bp->bio_offset / blocksize;
1139 count = bp->bio_length / blocksize;
1141 if (bp->bio_cmd == BIO_READ) {
1142 /* if transfer goes beyond range adjust it to be within limits */
1143 if (lba + count > lastlba) {
1144 /* if we are entirely beyond EOM return EOF */
1145 if (lastlba <= lba) {
1146 g_io_deliver(bp, 0);
1149 count = lastlba - lba;
1151 switch (blocksize) {
1153 ccb[0] = ATAPI_READ_BIG;
1157 ccb[0] = ATAPI_READ_CD;
1162 ccb[0] = ATAPI_READ_CD;
1167 ccb[0] = ATAPI_WRITE_BIG;
1178 if (!(request = ata_alloc_request())) {
1179 g_io_deliver(bp, ENOMEM);
1182 request->device = atadev;
1183 request->driver = bp;
1184 bcopy(ccb, request->u.atapi.ccb,
1185 (request->device->param->config & ATA_PROTO_MASK) ==
1186 ATA_PROTO_ATAPI_12 ? 16 : 12);
1187 request->data = bp->bio_data;
1188 request->bytecount = count * blocksize;
1189 request->transfersize = min(request->bytecount, 65534);
1190 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
1191 request->retries = 2;
1192 request->callback = acd_done;
1193 request->flags = ATA_R_ATAPI;
1194 if (request->device->mode >= ATA_DMA)
1195 request->flags |= ATA_R_DMA;
1196 switch (bp->bio_cmd) {
1198 request->flags |= ATA_R_READ;
1201 request->flags |= ATA_R_WRITE;
1204 ata_prtdev(atadev, "unknown BIO operation\n");
1205 ata_free_request(request);
1206 g_io_deliver(bp, EIO);
1209 ata_queue_request(request);
1213 acd_done(struct ata_request *request)
1215 struct bio *bp = request->driver;
1217 /* finish up transfer */
1218 bp->bio_completed = request->donecount;
1219 g_io_deliver(bp, request->result);
1220 ata_free_request(request);
1224 acd_read_toc(struct acd_softc *cdp)
1226 int track, ntracks, len;
1229 struct g_provider *pp;
1231 if (acd_test_ready(cdp->device))
1234 if (!(cdp->device->flags & ATA_D_MEDIA_CHANGED))
1237 cdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
1238 bzero(&cdp->toc, sizeof(cdp->toc));
1239 bzero(ccb, sizeof(ccb));
1240 cdp->disk_size = -1; /* hack for GEOM SOS */
1242 len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1243 ccb[0] = ATAPI_READ_TOC;
1246 if (ata_atapicmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1247 ATA_R_READ | ATA_R_QUIET, 30)) {
1248 bzero(&cdp->toc, sizeof(cdp->toc));
1251 ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1252 if (ntracks <= 0 || ntracks > MAXTRK) {
1253 bzero(&cdp->toc, sizeof(cdp->toc));
1257 len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1258 bzero(ccb, sizeof(ccb));
1259 ccb[0] = ATAPI_READ_TOC;
1262 if (ata_atapicmd(cdp->device, ccb, (caddr_t)&cdp->toc, len,
1263 ATA_R_READ | ATA_R_QUIET, 30)) {
1264 bzero(&cdp->toc, sizeof(cdp->toc));
1267 cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1269 cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1270 acd_set_ioparm(cdp);
1271 bzero(ccb, sizeof(ccb));
1272 ccb[0] = ATAPI_READ_CAPACITY;
1273 if (ata_atapicmd(cdp->device, ccb, (caddr_t)sizes, sizeof(sizes),
1274 ATA_R_READ | ATA_R_QUIET, 30)) {
1275 bzero(&cdp->toc, sizeof(cdp->toc));
1278 cdp->disk_size = ntohl(sizes[0]) + 1;
1280 for (track = 1; track <= ntracks; track ++) {
1281 if (cdp->pp[track] != NULL)
1283 pp = g_new_providerf(cdp->gp, "acd%dt%02d", cdp->lun, track);
1285 cdp->pp[track] = pp;
1286 g_error_provider(pp, 0);
1288 for (; track < MAXTRK; track ++) {
1289 if (cdp->pp[track] == NULL)
1291 cdp->pp[track]->flags |= G_PF_WITHER;
1292 g_orphan_provider(cdp->pp[track], ENXIO);
1293 cdp->pp[track] = NULL;
1297 if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1298 ata_prtdev(cdp->device, "(%d sectors (%d bytes)), %d tracks ",
1299 cdp->disk_size, cdp->block_size,
1300 cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1);
1301 if (cdp->toc.tab[0].control & 4)
1302 printf("%dMB\n", cdp->disk_size / 512);
1304 printf("%d:%d audio\n",
1305 cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1311 acd_play(struct acd_softc *cdp, int start, int end)
1315 bzero(ccb, sizeof(ccb));
1316 ccb[0] = ATAPI_PLAY_MSF;
1317 lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1318 lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1319 return ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 10);
1323 acd_setchan(struct acd_softc *cdp,
1324 u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1328 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1331 if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1333 cdp->au.data_length = 0;
1334 cdp->au.port[0].channels = c0;
1335 cdp->au.port[1].channels = c1;
1336 cdp->au.port[2].channels = c2;
1337 cdp->au.port[3].channels = c3;
1338 return acd_mode_select(cdp, (caddr_t)&cdp->au, sizeof(cdp->au));
1342 acd_load_done(struct ata_request *request)
1344 struct acd_softc *cdp = request->driver;
1346 /* finish the slot select and wakeup caller */
1347 cdp->changer_info->current_slot = cdp->slot;
1348 cdp->driver[cdp->changer_info->current_slot]->timestamp = time_second;
1349 wakeup(&cdp->changer_info);
1353 acd_unload_done(struct ata_request *request)
1355 struct acd_softc *cdp = request->driver;
1356 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, 0, 0, 0,
1357 cdp->slot, 0, 0, 0, 0, 0, 0, 0 };
1359 /* load the wanted slot */
1360 bcopy(ccb, request->u.atapi.ccb,
1361 (request->device->param->config & ATA_PROTO_MASK) ==
1362 ATA_PROTO_ATAPI_12 ? 16 : 12);
1363 request->callback = acd_load_done;
1364 ata_queue_request(request);
1368 acd_select_slot(struct acd_softc *cdp)
1370 struct ata_request *request;
1371 int8_t ccb[16] = { ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, 0, 0, 0,
1372 cdp->changer_info->current_slot, 0, 0, 0, 0, 0, 0, 0 };
1374 /* unload the current media from player */
1375 if (!(request = ata_alloc_request()))
1378 request->device = cdp->device;
1379 request->driver = cdp;
1380 bcopy(ccb, request->u.atapi.ccb,
1381 (request->device->param->config & ATA_PROTO_MASK) ==
1382 ATA_PROTO_ATAPI_12 ? 16 : 12);
1383 request->timeout = 30;
1384 request->callback = acd_unload_done;
1385 request->flags |= (ATA_R_ATAPI | ATA_R_IMMEDIATE);
1386 ata_queue_request(request);
1390 acd_init_writer(struct acd_softc *cdp, int test_write)
1394 bzero(ccb, sizeof(ccb));
1395 ccb[0] = ATAPI_REZERO;
1396 ata_atapicmd(cdp->device, ccb, NULL, 0, ATA_R_QUIET, 60);
1397 ccb[0] = ATAPI_SEND_OPC_INFO;
1399 ata_atapicmd(cdp->device, ccb, NULL, 0, ATA_R_QUIET, 30);
1404 acd_fixate(struct acd_softc *cdp, int multisession)
1406 int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1407 0, 0, 0, 0, 0, 0, 0, 0 };
1408 int timeout = 5*60*2;
1410 struct write_param param;
1412 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1413 (caddr_t)¶m, sizeof(param))))
1416 param.data_length = 0;
1418 param.session_type = CDR_SESS_MULTI;
1420 param.session_type = CDR_SESS_NONE;
1422 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1425 error = ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 30);
1429 /* some drives just return ready, wait for the expected fixate time */
1430 if ((error = acd_test_ready(cdp->device)) != EBUSY) {
1431 timeout = timeout / (cdp->cap.cur_write_speed / 177);
1432 tsleep(&error, PRIBIO, "acdfix", timeout * hz / 2);
1433 return acd_test_ready(cdp->device);
1436 while (timeout-- > 0) {
1437 if ((error = acd_get_progress(cdp, &dummy)))
1439 if ((error = acd_test_ready(cdp->device)) != EBUSY)
1441 tsleep(&error, PRIBIO, "acdcld", hz / 2);
1447 acd_init_track(struct acd_softc *cdp, struct cdr_track *track)
1449 struct write_param param;
1452 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1453 (caddr_t)¶m, sizeof(param))))
1456 param.data_length = 0;
1457 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1458 param.page_length = 0x32;
1459 param.test_write = track->test_write ? 1 : 0;
1460 param.write_type = CDR_WTYPE_TRACK;
1461 param.session_type = CDR_SESS_NONE;
1463 param.packet_size = 0;
1465 if (cdp->cap.capabilities & MST_BURNPROOF)
1466 param.burnproof = 1;
1468 switch (track->datablock_type) {
1472 param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1474 param.track_mode = CDR_TMODE_AUDIO;
1475 cdp->block_size = 2352;
1476 param.datablock_type = CDR_DB_RAW;
1477 param.session_format = CDR_SESS_CDROM;
1480 case CDR_DB_ROM_MODE1:
1481 cdp->block_size = 2048;
1482 param.track_mode = CDR_TMODE_DATA;
1483 param.datablock_type = CDR_DB_ROM_MODE1;
1484 param.session_format = CDR_SESS_CDROM;
1487 case CDR_DB_ROM_MODE2:
1488 cdp->block_size = 2336;
1489 param.track_mode = CDR_TMODE_DATA;
1490 param.datablock_type = CDR_DB_ROM_MODE2;
1491 param.session_format = CDR_SESS_CDROM;
1494 case CDR_DB_XA_MODE1:
1495 cdp->block_size = 2048;
1496 param.track_mode = CDR_TMODE_DATA;
1497 param.datablock_type = CDR_DB_XA_MODE1;
1498 param.session_format = CDR_SESS_CDROM_XA;
1501 case CDR_DB_XA_MODE2_F1:
1502 cdp->block_size = 2056;
1503 param.track_mode = CDR_TMODE_DATA;
1504 param.datablock_type = CDR_DB_XA_MODE2_F1;
1505 param.session_format = CDR_SESS_CDROM_XA;
1508 case CDR_DB_XA_MODE2_F2:
1509 cdp->block_size = 2324;
1510 param.track_mode = CDR_TMODE_DATA;
1511 param.datablock_type = CDR_DB_XA_MODE2_F2;
1512 param.session_format = CDR_SESS_CDROM_XA;
1515 case CDR_DB_XA_MODE2_MIX:
1516 cdp->block_size = 2332;
1517 param.track_mode = CDR_TMODE_DATA;
1518 param.datablock_type = CDR_DB_XA_MODE2_MIX;
1519 param.session_format = CDR_SESS_CDROM_XA;
1522 acd_set_ioparm(cdp);
1523 return acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10);
1527 acd_flush(struct acd_softc *cdp)
1529 int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1530 0, 0, 0, 0, 0, 0, 0, 0 };
1532 return ata_atapicmd(cdp->device, ccb, NULL, 0, ATA_R_QUIET, 60);
1536 acd_read_track_info(struct acd_softc *cdp,
1537 int32_t lba, struct acd_track_info *info)
1539 int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1540 lba>>24, lba>>16, lba>>8, lba, 0,
1541 sizeof(*info)>>8, sizeof(*info),
1542 0, 0, 0, 0, 0, 0, 0 };
1545 if ((error = ata_atapicmd(cdp->device, ccb, (caddr_t)info, sizeof(*info),
1548 info->track_start_addr = ntohl(info->track_start_addr);
1549 info->next_writeable_addr = ntohl(info->next_writeable_addr);
1550 info->free_blocks = ntohl(info->free_blocks);
1551 info->fixed_packet_size = ntohl(info->fixed_packet_size);
1552 info->track_length = ntohl(info->track_length);
1557 acd_get_progress(struct acd_softc *cdp, int *finished)
1559 int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1560 0, 0, 0, 0, 0, 0, 0, 0 };
1561 struct ata_request *request;
1564 if (!(request = ata_alloc_request()))
1567 request->device = cdp->device;
1568 request->driver = cdp;
1569 bcopy(ccb, request->u.atapi.ccb, 16);
1570 request->data = dummy;
1571 request->bytecount = sizeof(dummy);
1572 request->transfersize = min(request->bytecount, 65534);
1573 request->flags = ATA_R_ATAPI | ATA_R_READ;
1574 request->timeout = 30;
1575 ata_queue_request(request);
1576 if (!request->error && request->u.atapi.sense_data.sksv)
1577 *finished = ((request->u.atapi.sense_data.sk_specific2 |
1578 (request->u.atapi.sense_data.sk_specific1<<8))*100)/65535;
1581 ata_free_request(request);
1586 acd_send_cue(struct acd_softc *cdp, struct cdr_cuesheet *cuesheet)
1588 struct write_param param;
1589 int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1590 cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1591 0, 0, 0, 0, 0, 0, 0 };
1598 if ((error = acd_mode_sense(cdp, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1599 (caddr_t)¶m, sizeof(param))))
1602 param.data_length = 0;
1603 param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1604 param.page_length = 0x32;
1605 param.test_write = cuesheet->test_write ? 1 : 0;
1606 param.write_type = CDR_WTYPE_SESSION;
1607 param.session_type = cuesheet->session_type;
1609 param.packet_size = 0;
1610 param.track_mode = CDR_TMODE_AUDIO;
1611 param.datablock_type = CDR_DB_RAW;
1612 param.session_format = cuesheet->session_format;
1613 if (cdp->cap.capabilities & MST_BURNPROOF)
1614 param.burnproof = 1;
1616 if ((error = acd_mode_select(cdp, (caddr_t)¶m, param.page_length + 10)))
1619 if (!(buffer = malloc(cuesheet->len, M_ACD, M_NOWAIT)))
1622 if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len))) {
1624 printf("acd: cuesheet lenght = %d\n", cuesheet->len);
1625 for (i=0; i<cuesheet->len; i++)
1627 printf(" %02x", buffer[i]);
1629 printf("\n%02x", buffer[i]);
1632 error = ata_atapicmd(cdp->device, ccb, buffer, cuesheet->len, 0, 30);
1634 free(buffer, M_ACD);
1639 acd_report_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1641 struct dvd_miscauth *d = NULL;
1647 switch (ai->format) {
1648 case DVD_REPORT_AGID:
1649 case DVD_REPORT_ASF:
1650 case DVD_REPORT_RPC:
1653 case DVD_REPORT_KEY1:
1656 case DVD_REPORT_TITLE_KEY:
1660 case DVD_REPORT_CHALLENGE:
1663 case DVD_INVALIDATE_AGID:
1670 bzero(ccb, sizeof(ccb));
1671 ccb[0] = ATAPI_REPORT_KEY;
1672 ccb[2] = (lba >> 24) & 0xff;
1673 ccb[3] = (lba >> 16) & 0xff;
1674 ccb[4] = (lba >> 8) & 0xff;
1675 ccb[5] = lba & 0xff;
1676 ccb[8] = (length >> 8) & 0xff;
1677 ccb[9] = length & 0xff;
1678 ccb[10] = (ai->agid << 6) | ai->format;
1681 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1683 d->length = htons(length - 2);
1686 error = ata_atapicmd(cdp->device, ccb, (caddr_t)d, length,
1687 ai->format == DVD_INVALIDATE_AGID ? 0 : ATA_R_READ,10);
1693 switch (ai->format) {
1694 case DVD_REPORT_AGID:
1695 ai->agid = d->data[3] >> 6;
1698 case DVD_REPORT_CHALLENGE:
1699 bcopy(&d->data[0], &ai->keychal[0], 10);
1702 case DVD_REPORT_KEY1:
1703 bcopy(&d->data[0], &ai->keychal[0], 5);
1706 case DVD_REPORT_TITLE_KEY:
1707 ai->cpm = (d->data[0] >> 7);
1708 ai->cp_sec = (d->data[0] >> 6) & 0x1;
1709 ai->cgms = (d->data[0] >> 4) & 0x3;
1710 bcopy(&d->data[1], &ai->keychal[0], 5);
1713 case DVD_REPORT_ASF:
1714 ai->asf = d->data[3] & 1;
1717 case DVD_REPORT_RPC:
1718 ai->reg_type = (d->data[0] >> 6);
1719 ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1720 ai->user_rsts = d->data[0] & 0x7;
1721 ai->region = d->data[1];
1722 ai->rpc_scheme = d->data[2];
1725 case DVD_INVALIDATE_AGID:
1736 acd_send_key(struct acd_softc *cdp, struct dvd_authinfo *ai)
1738 struct dvd_miscauth *d;
1743 switch (ai->format) {
1744 case DVD_SEND_CHALLENGE:
1746 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1748 bcopy(ai->keychal, &d->data[0], 10);
1753 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1755 bcopy(&ai->keychal[0], &d->data[0], 5);
1760 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1762 d->data[0] = ai->region;
1769 bzero(ccb, sizeof(ccb));
1770 ccb[0] = ATAPI_SEND_KEY;
1771 ccb[8] = (length >> 8) & 0xff;
1772 ccb[9] = length & 0xff;
1773 ccb[10] = (ai->agid << 6) | ai->format;
1774 d->length = htons(length - 2);
1775 error = ata_atapicmd(cdp->device, ccb, (caddr_t)d, length, 0, 10);
1781 acd_read_structure(struct acd_softc *cdp, struct dvd_struct *s)
1783 struct dvd_miscauth *d;
1789 case DVD_STRUCT_PHYSICAL:
1793 case DVD_STRUCT_COPYRIGHT:
1797 case DVD_STRUCT_DISCKEY:
1801 case DVD_STRUCT_BCA:
1805 case DVD_STRUCT_MANUFACT:
1809 case DVD_STRUCT_DDS:
1810 case DVD_STRUCT_PRERECORDED:
1811 case DVD_STRUCT_UNIQUEID:
1812 case DVD_STRUCT_LIST:
1813 case DVD_STRUCT_CMI:
1814 case DVD_STRUCT_RMD_LAST:
1815 case DVD_STRUCT_RMD_RMA:
1816 case DVD_STRUCT_DCB:
1823 if (!(d = malloc(length, M_ACD, M_NOWAIT | M_ZERO)))
1825 d->length = htons(length - 2);
1827 bzero(ccb, sizeof(ccb));
1828 ccb[0] = ATAPI_READ_STRUCTURE;
1829 ccb[6] = s->layer_num;
1831 ccb[8] = (length >> 8) & 0xff;
1832 ccb[9] = length & 0xff;
1833 ccb[10] = s->agid << 6;
1834 error = ata_atapicmd(cdp->device, ccb, (caddr_t)d, length, ATA_R_READ, 30);
1840 switch (s->format) {
1841 case DVD_STRUCT_PHYSICAL: {
1842 struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1844 layer->book_type = d->data[0] >> 4;
1845 layer->book_version = d->data[0] & 0xf;
1846 layer->disc_size = d->data[1] >> 4;
1847 layer->max_rate = d->data[1] & 0xf;
1848 layer->nlayers = (d->data[2] >> 5) & 3;
1849 layer->track_path = (d->data[2] >> 4) & 1;
1850 layer->layer_type = d->data[2] & 0xf;
1851 layer->linear_density = d->data[3] >> 4;
1852 layer->track_density = d->data[3] & 0xf;
1853 layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1854 layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1855 layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1856 layer->bca = d->data[16] >> 7;
1860 case DVD_STRUCT_COPYRIGHT:
1861 s->cpst = d->data[0];
1862 s->rmi = d->data[0];
1865 case DVD_STRUCT_DISCKEY:
1866 bcopy(&d->data[0], &s->data[0], 2048);
1869 case DVD_STRUCT_BCA:
1870 s->length = ntohs(d->length);
1871 bcopy(&d->data[0], &s->data[0], s->length);
1874 case DVD_STRUCT_MANUFACT:
1875 s->length = ntohs(d->length);
1876 bcopy(&d->data[0], &s->data[0], s->length);
1887 acd_tray(struct acd_softc *cdp, int close)
1891 if (cdp->cap.mechanism & MST_EJECT) {
1893 if (!(error = acd_start_stop(cdp, 3))) {
1895 acd_prevent_allow(cdp, 1);
1896 cdp->flags |= F_LOCKED;
1900 acd_start_stop(cdp, 0);
1901 acd_prevent_allow(cdp, 0);
1902 cdp->flags &= ~F_LOCKED;
1903 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1904 error = acd_start_stop(cdp, 2);
1911 acd_blank(struct acd_softc *cdp, int blanktype)
1913 int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1914 0, 0, 0, 0, 0, 0, 0, 0 };
1916 cdp->device->flags |= ATA_D_MEDIA_CHANGED;
1917 return ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 30);
1921 acd_prevent_allow(struct acd_softc *cdp, int lock)
1923 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1924 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1926 return ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 30);
1930 acd_start_stop(struct acd_softc *cdp, int start)
1932 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1933 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1935 return ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 30);
1939 acd_pause_resume(struct acd_softc *cdp, int pause)
1941 int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1942 0, 0, 0, 0, 0, 0, 0 };
1944 return ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 30);
1948 acd_mode_sense(struct acd_softc *cdp, int page, caddr_t pagebuf, int pagesize)
1950 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1951 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1954 error = ata_atapicmd(cdp->device, ccb, pagebuf, pagesize, ATA_R_READ, 10);
1956 atapi_dump("acd: mode sense ", pagebuf, pagesize);
1962 acd_mode_select(struct acd_softc *cdp, caddr_t pagebuf, int pagesize)
1964 int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1965 pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1968 ata_prtdev(cdp->device,
1969 "modeselect pagesize=%d\n", pagesize);
1970 atapi_dump("mode select ", pagebuf, pagesize);
1972 return ata_atapicmd(cdp->device, ccb, pagebuf, pagesize, 0, 30);
1976 acd_set_speed(struct acd_softc *cdp, int rdspeed, int wrspeed)
1978 int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1979 wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1982 error = ata_atapicmd(cdp->device, ccb, NULL, 0, 0, 30);
1989 acd_get_cap(struct acd_softc *cdp)
1993 /* get drive capabilities, some bugridden drives needs this repeated */
1994 for (count = 0 ; count < 5 ; count++) {
1995 if (!acd_mode_sense(cdp, ATAPI_CDROM_CAP_PAGE,
1996 (caddr_t)&cdp->cap, sizeof(cdp->cap)) &&
1997 cdp->cap.page_code == ATAPI_CDROM_CAP_PAGE) {
1998 cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1999 cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
2000 cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
2001 cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed),177);
2002 cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
2003 cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
2009 acd_read_format_caps(struct acd_softc *cdp, struct cdr_format_capacities *caps)
2011 int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
2012 (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
2013 sizeof(struct cdr_format_capacities) & 0xff,
2014 0, 0, 0, 0, 0, 0, 0 };
2016 return ata_atapicmd(cdp->device, ccb, (caddr_t)caps,
2017 sizeof(struct cdr_format_capacities), ATA_R_READ, 30);
2021 acd_format(struct acd_softc *cdp, struct cdr_format_params* params)
2024 int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
2027 error = ata_atapicmd(cdp->device, ccb, (u_int8_t *)params,
2028 sizeof(struct cdr_format_params), 0, 30);
2033 acd_test_ready(struct ata_device *atadev)
2035 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
2036 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2038 return ata_atapicmd(atadev, ccb, NULL, 0, 0, 30);