xref: /dragonfly/sys/dev/disk/nata/atapi-cd.c (revision 73e0051e)
1 /*-
2  * Copyright (c) 1998 - 2006 S�ren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ata/atapi-cd.c,v 1.196 2007/11/19 21:11:26 sos Exp $
27  * $DragonFly: src/sys/dev/disk/nata/atapi-cd.c,v 1.12 2008/06/27 01:24:46 dillon Exp $
28  */
29 
30 #include "opt_ata.h"
31 
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/buf.h>
35 #include <sys/bus.h>
36 #include <sys/cdio.h>
37 #include <sys/cdrio.h>
38 #include <sys/device.h>
39 #include <sys/devicestat.h>
40 #include <sys/disk.h>
41 #include <sys/dvdio.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/nata.h>
46 #include <sys/proc.h>
47 #include <sys/priv.h>
48 #include <sys/systm.h>
49 
50 #include "ata-all.h"
51 #include "atapi-cd.h"
52 #include "ata_if.h"
53 
54 /* device structure */
55 static	d_open_t	acd_open;
56 static	d_close_t	acd_close;
57 static	d_ioctl_t	acd_ioctl;
58 static	d_strategy_t	acd_strategy;
59 static struct dev_ops acd_ops = {
60 	{ "acd", 117, D_DISK | D_TRACKCLOSE },
61 	.d_open =	acd_open,
62 	.d_close =	acd_close,
63 	.d_read =	physread,
64 	.d_write =	physwrite,
65 	.d_ioctl =	acd_ioctl,
66 	.d_strategy =	acd_strategy,
67 };
68 
69 /* prototypes */
70 static void acd_set_ioparm(device_t);
71 static void acd_describe(device_t);
72 static void lba2msf(u_int32_t, u_int8_t *, u_int8_t *, u_int8_t *);
73 static u_int32_t msf2lba(u_int8_t, u_int8_t, u_int8_t);
74 static void acd_start(device_t, struct bio *);
75 static void acd_done(struct ata_request *);
76 static void acd_read_toc(device_t);
77 #if 0
78 static struct acd_tracknode * acd_make_tracknode(device_t, int);
79 #endif
80 static void acd_destroy_tracknode(device_t, int);
81 static int acd_play(device_t, int, int);
82 static int acd_setchan(device_t, u_int8_t, u_int8_t, u_int8_t, u_int8_t);
83 static int acd_init_writer(device_t, int);
84 static int acd_fixate(device_t, int);
85 static int acd_init_track(device_t, struct cdr_track *);
86 static int acd_flush(device_t);
87 static int acd_read_track_info(device_t, int32_t, struct acd_track_info *);
88 static int acd_get_progress(device_t, int *);
89 static int acd_send_cue(device_t, struct cdr_cuesheet *);
90 static int acd_report_key(device_t, struct dvd_authinfo *);
91 static int acd_send_key(device_t, struct dvd_authinfo *);
92 static int acd_read_structure(device_t, struct dvd_struct *);
93 static int acd_tray(device_t, int);
94 static int acd_blank(device_t, int);
95 static int acd_prevent_allow(device_t, int);
96 static int acd_start_stop(device_t, int);
97 static int acd_pause_resume(device_t, int);
98 static int acd_mode_sense(device_t, int, caddr_t, int);
99 static int acd_mode_select(device_t, caddr_t, int);
100 static int acd_set_speed(device_t, int, int);
101 static void acd_get_cap(device_t);
102 #ifdef ACD_CDR_FORMAT
103 /* not yet ready */
104 static int acd_read_format_caps(device_t, struct cdr_format_capacities *);
105 static int acd_format(device_t, struct cdr_format_params *);
106 #endif /* ACD_CDR_FORMAT */
107 static int acd_test_ready(device_t);
108 
109 /* internal vars */
110 static MALLOC_DEFINE(M_ACD, "acd_driver", "ATAPI CD driver buffers");
111 
112 static int
113 acd_probe(device_t dev)
114 {
115     struct ata_device *atadev = device_get_softc(dev);
116 
117     if ((atadev->param.config & ATA_PROTO_ATAPI) &&
118 	(atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_CDROM)
119 	return 0;
120     else
121 	return ENXIO;
122 }
123 
124 static int
125 acd_attach(device_t dev)
126 {
127     struct ata_device *atadev = device_get_softc(dev);
128     struct acd_softc *cdp;
129     cdev_t cdev;
130 
131     /* XXX TGEN We're not in interrupt context, so we can M_WAITOK and remove
132        the OOM check. */
133     cdp = kmalloc(sizeof(struct acd_softc), M_ACD, M_INTWAIT | M_ZERO);
134     cdp->block_size = 2048;
135     device_set_ivars(dev, cdp);
136     ATA_SETMODE(device_get_parent(dev), dev);
137     ata_controlcmd(dev, ATA_DEVICE_RESET, 0, 0, 0);
138     acd_get_cap(dev);
139 
140     devstat_add_entry(&cdp->stats, "acd", device_get_unit(dev), DEV_BSIZE,
141 		      DEVSTAT_NO_ORDERED_TAGS,
142 		      DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE,
143 		      DEVSTAT_PRIORITY_CD);
144 
145     cdev = disk_create(device_get_unit(dev), &cdp->disk, &acd_ops);
146 #if 0
147     cdev = make_dev(&acd_ops, dkmakeminor(device_get_unit(dev), 0, 0),
148 		      UID_ROOT, GID_OPERATOR, 0644, "acd%d",
149 		      device_get_unit(dev));
150 #endif
151     reference_dev(cdev);
152     cdev->si_drv1 = dev;
153 
154     /*
155      * Even though we do not have media information yet, we have to
156      * tell the disk management layer something or dscheck() will be
157      * unhappy.
158      */
159     cdp->cdev = cdev;
160     acd_set_ioparm(dev);
161     atadev->flags |= ATA_D_MEDIA_CHANGED;
162 
163     /* announce we are here */
164     acd_describe(dev);
165     return 0;
166 }
167 
168 static int
169 acd_detach(device_t dev)
170 {
171     struct acd_softc *cdp = device_get_ivars(dev);
172     int track;
173 
174     /* destroy devices from the system so we don't get any further requests */
175     for (track = 1; track < MAXTRK; track++) {
176 	if (cdp->track[track] == NULL)
177 	    continue;
178 	acd_destroy_tracknode(dev, track);
179     }
180     destroy_dev(cdp->cdev);
181 
182     /* fail requests on the queue and any "in flight" for this device */
183     ata_fail_requests(dev);
184 
185     /* don't leave anything behind */
186     kprintf("devfs: Please check that only the right ATA CD device was removed!!!\n");
187     dev_ops_remove_minor(&acd_ops, /*dkunitmask(), */dkmakeunit(device_get_unit(dev)));
188     disk_invalidate(&cdp->disk);
189     disk_destroy(&cdp->disk);
190     devstat_remove_entry(&cdp->stats);
191     device_set_ivars(dev, NULL);
192     kfree(cdp, M_ACD);
193     return 0;
194 }
195 
196 static void
197 acd_shutdown(device_t dev)
198 {
199     struct ata_device *atadev = device_get_softc(dev);
200 
201     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
202 	ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
203 }
204 
205 static int
206 acd_reinit(device_t dev)
207 {
208     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
209     struct ata_device *atadev = device_get_softc(dev);
210 
211     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
212 	((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
213 	return 1;
214     }
215     ATA_SETMODE(device_get_parent(dev), dev);
216     return 0;
217 }
218 
219 static int
220 acd_open(struct dev_open_args *ap)
221 {
222     device_t dev = ap->a_head.a_dev->si_drv1;
223     /* XXX TGEN Sometimes, we're fed a cdev_t which we didn't create. It
224        doesn't have si_drv1 set, leading to evil NULL derefs. I can actually
225        recover our device_t otherwise, but really, this is a bug, so I'll bail
226        out. */
227     if (!dev)
228         return ENXIO;
229 
230     struct ata_device *atadev = device_get_softc(dev);
231     struct acd_softc *cdp = device_get_ivars(dev);
232     struct ata_request *request;
233     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
234                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
235     int timeout = 60;
236 
237     if (!cdp)
238 	return ENXIO;
239     if (!device_is_attached(dev))
240 	return EBUSY;
241     if (!(request = ata_alloc_request()))
242 	return ENOMEM;
243 
244     /* wait if drive is not finished loading the medium */
245     while(timeout--) {
246 	bzero(request, sizeof(struct ata_request));
247 	request->dev = dev;
248 	bcopy(ccb, request->u.atapi.ccb, 16);
249 	request->flags = ATA_R_ATAPI;
250 	request->timeout = ATA_DEFAULT_TIMEOUT;
251 	ata_queue_request(request);
252 	if (!request->error &&
253 	    (request->u.atapi.sense.key == 2 ||
254 	     request->u.atapi.sense.key == 7) &&
255 	    request->u.atapi.sense.asc == 4 &&
256 	    request->u.atapi.sense.ascq == 1)
257 	    tsleep(&timeout, 0, "acdld", hz / 2);
258 	else
259 	    break;
260     }
261     ata_free_request(request);
262 
263     /*
264      * DragonFly abstracts out the disk layer so our device may not have
265      * a vnode directly associated with it.  count_dev() cannot be used.
266      */
267     if (atadev->opencount == 0) {
268 	acd_prevent_allow(dev, 1);
269 	cdp->flags |= F_LOCKED;
270 	acd_read_toc(dev);
271     }
272     ++atadev->opencount;
273     return 0;
274 }
275 
276 static int
277 acd_close(struct dev_close_args *ap)
278 {
279     device_t dev = ap->a_head.a_dev->si_drv1;
280     struct acd_softc *cdp = device_get_ivars(dev);
281     struct ata_device *atadev = device_get_softc(dev);
282 
283     if (!cdp)
284 	return ENXIO;
285 
286     if (atadev->opencount == 1) {
287 	acd_prevent_allow(dev, 0);
288 	cdp->flags &= ~F_LOCKED;
289     }
290     if (atadev->opencount > 0)
291 	--atadev->opencount;
292     return 0;
293 }
294 
295 static int
296 acd_ioctl(struct dev_ioctl_args *ap)
297 {
298     device_t dev = ap->a_head.a_dev->si_drv1;
299     struct ata_device *atadev = device_get_softc(dev);
300     struct acd_softc *cdp = device_get_ivars(dev);
301     int error = 0, nocopyout = 0;
302 
303     if (!cdp)
304 	return ENXIO;
305 
306     if (atadev->flags & ATA_D_MEDIA_CHANGED) {
307 	switch (ap->a_cmd) {
308 	case CDIOCRESET:
309 	    acd_test_ready(dev);
310 	    break;
311 
312 	default:
313 	    acd_read_toc(dev);
314 	    acd_prevent_allow(dev, 1);
315 	    cdp->flags |= F_LOCKED;
316 	    break;
317 	}
318     }
319 
320     switch (ap->a_cmd) {
321 
322     case CDIOCRESUME:
323 	error = acd_pause_resume(dev, 1);
324 	break;
325 
326     case CDIOCPAUSE:
327 	error = acd_pause_resume(dev, 0);
328 	break;
329 
330     case CDIOCSTART:
331 	error = acd_start_stop(dev, 1);
332 	break;
333 
334     case CDIOCSTOP:
335 	error = acd_start_stop(dev, 0);
336 	break;
337 
338     case CDIOCALLOW:
339 	error = acd_prevent_allow(dev, 0);
340 	cdp->flags &= ~F_LOCKED;
341 	break;
342 
343     case CDIOCPREVENT:
344 	error = acd_prevent_allow(dev, 1);
345 	cdp->flags |= F_LOCKED;
346 	break;
347 
348     case CDIOCRESET:
349 	error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
350 	if (error)
351 	    break;
352 	error = acd_test_ready(dev);
353 	break;
354 
355     case CDIOCEJECT:
356 	if (atadev->opencount > 1) {
357 	    error = EBUSY;
358 	    break;
359 	}
360 	error = acd_tray(dev, 0);
361 	break;
362 
363     case CDIOCCLOSE:
364 	if (atadev->opencount > 1)
365 	    break;
366 	error = acd_tray(dev, 1);
367 	break;
368 
369     case CDIOREADTOCHEADER:
370 	if (!cdp->toc.hdr.ending_track) {
371 	    error = EIO;
372 	    break;
373 	}
374 	bcopy(&cdp->toc.hdr, ap->a_data, sizeof(cdp->toc.hdr));
375 	break;
376 
377     case CDIOREADTOCENTRYS:
378 	{
379 	    struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)ap->a_data;
380 	    struct toc *toc = &cdp->toc;
381 	    int starting_track = te->starting_track;
382 	    int len;
383 
384 	    if (!toc->hdr.ending_track) {
385 		error = EIO;
386 		break;
387 	    }
388 
389 	    if (te->data_len < sizeof(toc->tab[0]) ||
390 		(te->data_len % sizeof(toc->tab[0])) != 0 ||
391 		(te->address_format != CD_MSF_FORMAT &&
392 		te->address_format != CD_LBA_FORMAT)) {
393 		error = EINVAL;
394 		break;
395 	    }
396 
397 	    if (!starting_track)
398 		starting_track = toc->hdr.starting_track;
399 	    else if (starting_track == 170)
400 		starting_track = toc->hdr.ending_track + 1;
401 	    else if (starting_track < toc->hdr.starting_track ||
402 		     starting_track > toc->hdr.ending_track + 1) {
403 		error = EINVAL;
404 		break;
405 	    }
406 
407 	    len = ((toc->hdr.ending_track + 1 - starting_track) + 1) *
408 		  sizeof(toc->tab[0]);
409 	    if (te->data_len < len)
410 		len = te->data_len;
411 	    if (len > sizeof(toc->tab)) {
412 		error = EINVAL;
413 		break;
414 	    }
415 
416 	    if (te->address_format == CD_MSF_FORMAT) {
417 		struct cd_toc_entry *entry;
418 
419 		/* XXX TGEN Use M_WAITOK, not in intr ctx. */
420 		toc = kmalloc(sizeof(struct toc), M_ACD, M_INTWAIT);
421 		bcopy(&cdp->toc, toc, sizeof(struct toc));
422 		entry = toc->tab + (toc->hdr.ending_track + 1 -
423 			toc->hdr.starting_track) + 1;
424 		while (--entry >= toc->tab) {
425 		    lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
426 			    &entry->addr.msf.second, &entry->addr.msf.frame);
427 		    entry->addr_type = CD_MSF_FORMAT;
428 		}
429 	    }
430 	    error = copyout(toc->tab + starting_track - toc->hdr.starting_track,
431 			    te->data, len);
432 	    if (te->address_format == CD_MSF_FORMAT)
433 		kfree(toc, M_ACD);
434 	}
435 	break;
436 
437     case CDIOREADTOCENTRY:
438 	{
439 	    struct ioc_read_toc_single_entry *te =
440 		(struct ioc_read_toc_single_entry *)ap->a_data;
441 	    struct toc *toc = &cdp->toc;
442 	    u_char track = te->track;
443 
444 	    if (!toc->hdr.ending_track) {
445 		error = EIO;
446 		break;
447 	    }
448 
449 	    if (te->address_format != CD_MSF_FORMAT &&
450 		te->address_format != CD_LBA_FORMAT) {
451 		error = EINVAL;
452 		break;
453 	    }
454 
455 	    if (!track)
456 		track = toc->hdr.starting_track;
457 	    else if (track == 170)
458 		track = toc->hdr.ending_track + 1;
459 	    else if (track < toc->hdr.starting_track ||
460 		     track > toc->hdr.ending_track + 1) {
461 		error = EINVAL;
462 		break;
463 	    }
464 
465 	    if (te->address_format == CD_MSF_FORMAT) {
466 		struct cd_toc_entry *entry;
467 
468 		/* XXX TGEN Use M_WAITOK, not in intr ctx. */
469 		toc = kmalloc(sizeof(struct toc), M_ACD, M_INTWAIT);
470 		bcopy(&cdp->toc, toc, sizeof(struct toc));
471 		entry = toc->tab + (track - toc->hdr.starting_track);
472 		lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute,
473 			&entry->addr.msf.second, &entry->addr.msf.frame);
474 	    }
475 	    bcopy(toc->tab + track - toc->hdr.starting_track,
476 		  &te->entry, sizeof(struct cd_toc_entry));
477 	    if (te->address_format == CD_MSF_FORMAT)
478 		kfree(toc, M_ACD);
479 	}
480 	break;
481 
482 /* XXX TGEN Remove this and the rest of the nocopyout logic? */
483 #if __FreeBSD_version > 600008
484     case CDIOCREADSUBCHANNEL_SYSSPACE:
485 	nocopyout = 1;
486 	/* FALLTHROUGH */
487 
488 #endif
489     case CDIOCREADSUBCHANNEL:
490 	{
491 	    struct ioc_read_subchannel *args =
492 		(struct ioc_read_subchannel *)ap->a_data;
493 	    u_int8_t format;
494 	    int8_t ccb[16] = { ATAPI_READ_SUBCHANNEL, 0, 0x40, 1, 0, 0, 0,
495 			       sizeof(cdp->subchan)>>8, sizeof(cdp->subchan),
496 			       0, 0, 0, 0, 0, 0, 0 };
497 
498 	    if (args->data_len > sizeof(struct cd_sub_channel_info) ||
499 		args->data_len < sizeof(struct cd_sub_channel_header)) {
500 		error = EINVAL;
501 		break;
502 	    }
503 
504 	    format = args->data_format;
505 	    if ((format != CD_CURRENT_POSITION) &&
506 		(format != CD_MEDIA_CATALOG) && (format != CD_TRACK_INFO)) {
507 		error = EINVAL;
508 		break;
509 	    }
510 
511 	    ccb[1] = args->address_format & CD_MSF_FORMAT;
512 
513 	    if ((error = ata_atapicmd(dev, ccb, (caddr_t)&cdp->subchan,
514 				      sizeof(cdp->subchan), ATA_R_READ, 10)))
515 		break;
516 
517 	    if ((format == CD_MEDIA_CATALOG) || (format == CD_TRACK_INFO)) {
518 		if (cdp->subchan.header.audio_status == 0x11) {
519 		    error = EINVAL;
520 		    break;
521 		}
522 
523 		ccb[3] = format;
524 		if (format == CD_TRACK_INFO)
525 		    ccb[6] = args->track;
526 
527 		if ((error = ata_atapicmd(dev, ccb, (caddr_t)&cdp->subchan,
528 					  sizeof(cdp->subchan),ATA_R_READ,10))){
529 		    break;
530 		}
531 	    }
532 	    /* XXX TGEN Remove this and the rest of the nocopyout logic? */
533 	    if (nocopyout == 0) {
534 		error = copyout(&cdp->subchan, args->data, args->data_len);
535 	    } else {
536 		error = 0;
537 		bcopy(&cdp->subchan, args->data, args->data_len);
538 	    }
539 	}
540 	break;
541 
542     case CDIOCPLAYMSF:
543 	{
544 	    struct ioc_play_msf *args = (struct ioc_play_msf *)ap->a_data;
545 
546 	    error =
547 		acd_play(dev,
548 			 msf2lba(args->start_m, args->start_s, args->start_f),
549 			 msf2lba(args->end_m, args->end_s, args->end_f));
550 	}
551 	break;
552 
553     case CDIOCPLAYBLOCKS:
554 	{
555 	    struct ioc_play_blocks *args = (struct ioc_play_blocks *)ap->a_data;
556 
557 	    error = acd_play(dev, args->blk, args->blk + args->len);
558 	}
559 	break;
560 
561     case CDIOCPLAYTRACKS:
562 	{
563 	    struct ioc_play_track *args = (struct ioc_play_track *)ap->a_data;
564 	    int t1, t2;
565 
566 	    if (!cdp->toc.hdr.ending_track) {
567 		error = EIO;
568 		break;
569 	    }
570 	    if (args->end_track < cdp->toc.hdr.ending_track + 1)
571 		++args->end_track;
572 	    if (args->end_track > cdp->toc.hdr.ending_track + 1)
573 		args->end_track = cdp->toc.hdr.ending_track + 1;
574 	    t1 = args->start_track - cdp->toc.hdr.starting_track;
575 	    t2 = args->end_track - cdp->toc.hdr.starting_track;
576 	    if (t1 < 0 || t2 < 0 ||
577 		t1 > (cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track)) {
578 		error = EINVAL;
579 		break;
580 	    }
581 	    error = acd_play(dev, ntohl(cdp->toc.tab[t1].addr.lba),
582 			     ntohl(cdp->toc.tab[t2].addr.lba));
583 	}
584 	break;
585 
586     case CDIOCGETVOL:
587 	{
588 	    struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
589 
590 	    if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE,
591 					(caddr_t)&cdp->au, sizeof(cdp->au))))
592 		break;
593 
594 	    if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
595 		error = EIO;
596 		break;
597 	    }
598 	    arg->vol[0] = cdp->au.port[0].volume;
599 	    arg->vol[1] = cdp->au.port[1].volume;
600 	    arg->vol[2] = cdp->au.port[2].volume;
601 	    arg->vol[3] = cdp->au.port[3].volume;
602 	}
603 	break;
604 
605     case CDIOCSETVOL:
606 	{
607 	    struct ioc_vol *arg = (struct ioc_vol *)ap->a_data;
608 
609 	    if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE,
610 					(caddr_t)&cdp->au, sizeof(cdp->au))))
611 		break;
612 	    if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE) {
613 		error = EIO;
614 		break;
615 	    }
616 	    if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE_MASK,
617 					(caddr_t)&cdp->aumask,
618 					sizeof(cdp->aumask))))
619 		break;
620 	    cdp->au.data_length = 0;
621 	    cdp->au.port[0].channels = CHANNEL_0;
622 	    cdp->au.port[1].channels = CHANNEL_1;
623 	    cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume;
624 	    cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume;
625 	    cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume;
626 	    cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume;
627 	    error =  acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
628 	}
629 	break;
630 
631     case CDIOCSETPATCH:
632 	{
633 	    struct ioc_patch *arg = (struct ioc_patch *)ap->a_data;
634 
635 	    error = acd_setchan(dev, arg->patch[0], arg->patch[1],
636 				arg->patch[2], arg->patch[3]);
637 	}
638 	break;
639 
640     case CDIOCSETMONO:
641 	error = acd_setchan(dev, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0,0);
642 	break;
643 
644     case CDIOCSETSTEREO:
645 	error = acd_setchan(dev, CHANNEL_0, CHANNEL_1, 0, 0);
646 	break;
647 
648     case CDIOCSETMUTE:
649 	error = acd_setchan(dev, 0, 0, 0, 0);
650 	break;
651 
652     case CDIOCSETLEFT:
653 	error = acd_setchan(dev, CHANNEL_0, CHANNEL_0, 0, 0);
654 	break;
655 
656     case CDIOCSETRIGHT:
657 	error = acd_setchan(dev, CHANNEL_1, CHANNEL_1, 0, 0);
658 	break;
659 
660     case CDRIOCBLANK:
661 	error = acd_blank(dev, (*(int *)ap->a_data));
662 	break;
663 
664     case CDRIOCNEXTWRITEABLEADDR:
665 	{
666 	    struct acd_track_info track_info;
667 
668 	    if ((error = acd_read_track_info(dev, 0xff, &track_info)))
669 		break;
670 
671 	    if (!track_info.nwa_valid) {
672 		error = EINVAL;
673 		break;
674 	    }
675 	    *(int*)ap->a_data = track_info.next_writeable_addr;
676 	}
677 	break;
678 
679     case CDRIOCINITWRITER:
680 	error = acd_init_writer(dev, (*(int *)ap->a_data));
681 	break;
682 
683     case CDRIOCINITTRACK:
684 	error = acd_init_track(dev, (struct cdr_track *)ap->a_data);
685 	break;
686 
687     case CDRIOCFLUSH:
688 	error = acd_flush(dev);
689 	break;
690 
691     case CDRIOCFIXATE:
692 	error = acd_fixate(dev, (*(int *)ap->a_data));
693 	break;
694 
695     case CDRIOCREADSPEED:
696 	{
697 	    int speed = *(int *)ap->a_data;
698 
699 	    /* Preserve old behavior: units in multiples of CDROM speed */
700 	    if (speed < 177)
701 		speed *= 177;
702 	    error = acd_set_speed(dev, speed, CDR_MAX_SPEED);
703 	}
704 	break;
705 
706     case CDRIOCWRITESPEED:
707 	{
708 	    int speed = *(int *)ap->a_data;
709 
710 	    if (speed < 177)
711 		speed *= 177;
712 	    error = acd_set_speed(dev, CDR_MAX_SPEED, speed);
713 	}
714 	break;
715 
716     case CDRIOCGETBLOCKSIZE:
717 	*(int *)ap->a_data = cdp->block_size;
718 	break;
719 
720     case CDRIOCSETBLOCKSIZE:
721 	cdp->block_size = *(int *)ap->a_data;
722 	acd_set_ioparm(dev);
723 	break;
724 
725     case CDRIOCGETPROGRESS:
726 	error = acd_get_progress(dev, (int *)ap->a_data);
727 	break;
728 
729     case CDRIOCSENDCUE:
730 	error = acd_send_cue(dev, (struct cdr_cuesheet *)ap->a_data);
731 	break;
732 
733 #ifdef ACD_CDR_FORMAT
734     case CDRIOCREADFORMATCAPS:
735 	error = acd_read_format_caps(dev,
736 			(struct cdr_format_capacities *)ap->a_data);
737 	break;
738 
739     case CDRIOCFORMAT:
740 	error = acd_format(dev, (struct cdr_format_params *)ap->a_data);
741 	break;
742 #endif /* ACD_CDR_FORMAT */
743 
744     case DVDIOCREPORTKEY:
745 	if (cdp->cap.media & MST_READ_DVDROM)
746 	    error = acd_report_key(dev, (struct dvd_authinfo *)ap->a_data);
747 	else
748 	    error = EINVAL;
749 	break;
750 
751     case DVDIOCSENDKEY:
752 	if (cdp->cap.media & MST_READ_DVDROM)
753 	    error = acd_send_key(dev, (struct dvd_authinfo *)ap->a_data);
754 	else
755 	    error = EINVAL;
756 	break;
757 
758     case DVDIOCREADSTRUCTURE:
759 	if (cdp->cap.media & MST_READ_DVDROM)
760 	    error = acd_read_structure(dev, (struct dvd_struct *)ap->a_data);
761 	else
762 	    error = EINVAL;
763 	break;
764 
765     default:
766 	error = ata_device_ioctl(dev, ap->a_cmd, ap->a_data);
767     }
768     return error;
769 }
770 
771 static int
772 acd_strategy(struct dev_strategy_args *ap)
773 {
774     device_t dev = ap->a_head.a_dev->si_drv1;
775     struct bio *bp = ap->a_bio;
776     struct buf *bbp = bp->bio_buf;
777 /*    struct ata_device *atadev = device_get_softc(dev);*/
778     struct acd_softc *cdp = device_get_ivars(dev);
779     cdev_t cdev = cdp->cdev;
780 
781     if (bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE) {
782 	bbp->b_flags |= B_ERROR;
783 	bbp->b_error = EOPNOTSUPP;
784 	biodone(bp);
785 	return 0;
786     }
787 
788     if (bbp->b_cmd == BUF_CMD_READ && cdp->disk_size == -1) {
789 	bbp->b_flags |= B_ERROR;
790 	bbp->b_error = EIO;
791 	biodone(bp);
792 	return 0;
793     }
794 
795     KASSERT(bbp->b_bcount != 0, ("acd_strategy: 0-length I/O"));
796 
797     bp->bio_driver_info = cdev;
798     bbp->b_resid = bbp->b_bcount;
799 
800     acd_start(dev, bp);
801     return 0;
802 }
803 
804 /* XXX TGEN Collapse this with acd_strategy()? */
805 static void
806 acd_start(device_t dev, struct bio *bp)
807 {
808     struct buf *bbp = bp->bio_buf;
809     struct ata_device *atadev = device_get_softc(dev);
810     struct acd_softc *cdp = device_get_ivars(dev);
811     struct ata_request *request;
812     cdev_t cdev;
813     u_int32_t lba, lastlba, count;
814     int8_t ccb[16];
815     int track, blocksize;
816 
817     cdev = bp->bio_driver_info;
818 
819     /* reject all queued entries if media changed */
820     if (atadev->flags & ATA_D_MEDIA_CHANGED) {
821 	bbp->b_flags |= B_ERROR;
822 	bbp->b_error = EIO;
823 	biodone(bp);
824 	return;
825     }
826 
827     bzero(ccb, sizeof(ccb));
828 
829     /*
830      * Special track access is via bio_offset (128-255), and direct
831      * raw access via 128, else normal accesses.
832      */
833     track = (bp->bio_offset >> 56) & 127;
834 
835     if (track) {
836 	if (track > MAXTRK) {
837 	    bbp->b_flags |= B_ERROR;
838 	    bbp->b_error = EIO;
839 	    biodone(bp);
840 	    return;
841 	}
842 	blocksize = (cdp->toc.tab[track - 1].control & 4) ? 2048 : 2352;
843 	lastlba = ntohl(cdp->toc.tab[track].addr.lba);
844 	lba = (bp->bio_offset & 0x00FFFFFFFFFFFFFFULL) / blocksize;
845 	lba += ntohl(cdp->toc.tab[track - 1].addr.lba);
846     }
847     else {
848 	blocksize = cdp->block_size;
849 	lastlba = cdp->disk_size;
850 	lba = (bp->bio_offset & 0x00FFFFFFFFFFFFFFULL) / blocksize;
851     }
852 
853     count = bbp->b_bcount / blocksize;
854     KASSERT(count != 0, ("acd_strategy: 0-length I/O %d bytes vs %d blksize",
855 		bbp->b_bcount, blocksize));
856 
857     if (bbp->b_cmd == BUF_CMD_READ) {
858 	/* if transfer goes beyond range adjust it to be within limits */
859 	if (lba + count > lastlba) {
860 	    /* if we are entirely beyond EOM return EOF */
861 	    if (lastlba <= lba) {
862 		bbp->b_resid = bbp->b_bcount;
863 		biodone(bp);
864 		return;
865 	    }
866 	    count = lastlba - lba;
867 	}
868 	switch (blocksize) {
869 	case 2048:
870 	    ccb[0] = ATAPI_READ_BIG;
871 	    break;
872 
873 	case 2352:
874 	    ccb[0] = ATAPI_READ_CD;
875 	    ccb[9] = 0xf8;
876 	    break;
877 
878 	default:
879 	    ccb[0] = ATAPI_READ_CD;
880 	    ccb[9] = 0x10;
881 	}
882     }
883     else
884 	ccb[0] = ATAPI_WRITE_BIG;
885 
886     ccb[1] = 0;
887     ccb[2] = lba>>24;
888     ccb[3] = lba>>16;
889     ccb[4] = lba>>8;
890     ccb[5] = lba;
891     ccb[6] = count>>16;
892     ccb[7] = count>>8;
893     ccb[8] = count;
894 
895     if (!(request = ata_alloc_request())) {
896 	bbp->b_flags |= B_ERROR;
897 	bbp->b_error = ENOMEM;
898 	biodone(bp);
899 	return;
900     }
901     request->dev = dev;
902     request->bio = bp;
903     bcopy(ccb, request->u.atapi.ccb,
904 	  (atadev->param.config & ATA_PROTO_MASK) ==
905 	  ATA_PROTO_ATAPI_12 ? 16 : 12);
906     request->data = bbp->b_data;
907     request->bytecount = count * blocksize;
908     request->transfersize = min(request->bytecount, 65534);
909     request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
910     request->retries = 2;
911     request->callback = acd_done;
912     request->flags = ATA_R_ATAPI;
913     if (atadev->mode >= ATA_DMA)
914 	request->flags |= ATA_R_DMA;
915     switch (bbp->b_cmd) {
916     case BUF_CMD_READ:
917 	request->flags |= ATA_R_READ;
918 	break;
919     case BUF_CMD_WRITE:
920 	request->flags |= ATA_R_WRITE;
921 	break;
922     default:
923 	device_printf(dev, "unknown BUF operation\n");
924 	ata_free_request(request);
925 	bbp->b_flags |= B_ERROR;
926 	bbp->b_error = EIO;
927 	biodone(bp);
928 	return;
929     }
930     devstat_start_transaction(&cdp->stats);
931     ata_queue_request(request);
932 }
933 
934 static void
935 acd_done(struct ata_request *request)
936 {
937     struct acd_softc *cdp = device_get_ivars(request->dev);
938     struct bio *bp = request->bio;
939     struct buf *bbp = bp->bio_buf;
940 
941     /* finish up transfer */
942     if ((bbp->b_error = request->result))
943 	bbp->b_flags |= B_ERROR;
944     bbp->b_resid = bbp->b_bcount - request->donecount;
945     devstat_end_transaction_buf(&cdp->stats, bbp);
946     biodone(bp);
947     ata_free_request(request);
948 }
949 
950 static void
951 acd_set_ioparm(device_t dev)
952 {
953     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
954     struct ata_device *atadev = device_get_softc(dev);
955     struct acd_softc *cdp = device_get_ivars(dev);
956     struct disk_info info;
957 
958     if (ch->dma)
959 	cdp->iomax = min(ch->dma->max_iosize, 65534);
960     else
961 	cdp->iomax = min(DFLTPHYS, 65534);
962 
963     cdp->cdev->si_iosize_max = (cdp->iomax / cdp->block_size) * cdp->block_size;
964     cdp->cdev->si_bsize_phys = cdp->block_size;
965     bzero(&info, sizeof(info));
966     info.d_media_blksize = cdp->block_size;
967     info.d_media_blocks = (cdp->disk_size == -1) ? 0 : cdp->disk_size;
968     info.d_secpertrack = 100;
969     info.d_nheads = 1;
970     info.d_ncylinders = cdp->disk_size / info.d_secpertrack / info.d_nheads + 1;
971     info.d_secpercyl = info.d_secpertrack * info.d_nheads;
972     info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL | DSO_COMPATPARTA |
973 		     DSO_RAWEXTENSIONS;
974     info.d_serialno = atadev->param.serial;
975     disk_setdiskinfo(&cdp->disk, &info);
976 
977 }
978 
979 static void
980 lba2msf(u_int32_t lba, u_int8_t *m, u_int8_t *s, u_int8_t *f)
981 {
982     lba += 150;
983     lba &= 0xffffff;
984     *m = lba / (60 * 75);
985     lba %= (60 * 75);
986     *s = lba / 75;
987     *f = lba % 75;
988 }
989 
990 static u_int32_t
991 msf2lba(u_int8_t m, u_int8_t s, u_int8_t f)
992 {
993     return (m * 60 + s) * 75 + f - 150;
994 }
995 
996 static void
997 acd_read_toc(device_t dev)
998 {
999     struct ata_device *atadev = device_get_softc(dev);
1000     struct acd_softc *cdp = device_get_ivars(dev);
1001     struct acd_tracknode *tracknode;
1002     u_int32_t sizes[2];
1003     int8_t ccb[16];
1004     int track, ntracks, len;
1005 
1006     atadev->flags &= ~ATA_D_MEDIA_CHANGED;
1007     bzero(&cdp->toc, sizeof(cdp->toc));
1008 
1009     if (acd_test_ready(dev))
1010 	return;
1011 
1012     bzero(ccb, sizeof(ccb));
1013     len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry);
1014     ccb[0] = ATAPI_READ_TOC;
1015     ccb[7] = len>>8;
1016     ccb[8] = len;
1017     if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
1018 		     ATA_R_READ | ATA_R_QUIET, 30)) {
1019 	bzero(&cdp->toc, sizeof(cdp->toc));
1020 	return;
1021     }
1022     ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1;
1023     if (ntracks <= 0 || ntracks > MAXTRK) {
1024 	bzero(&cdp->toc, sizeof(cdp->toc));
1025 	return;
1026     }
1027 
1028     len = sizeof(struct ioc_toc_header)+(ntracks+1)*sizeof(struct cd_toc_entry);
1029     bzero(ccb, sizeof(ccb));
1030     ccb[0] = ATAPI_READ_TOC;
1031     ccb[7] = len>>8;
1032     ccb[8] = len;
1033     if (ata_atapicmd(dev, ccb, (caddr_t)&cdp->toc, len,
1034 		     ATA_R_READ | ATA_R_QUIET, 30)) {
1035 	bzero(&cdp->toc, sizeof(cdp->toc));
1036 	return;
1037     }
1038     cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len);
1039 
1040     cdp->block_size = (cdp->toc.tab[0].control & 4) ? 2048 : 2352;
1041     bzero(ccb, sizeof(ccb));
1042     ccb[0] = ATAPI_READ_CAPACITY;
1043     if (ata_atapicmd(dev, ccb, (caddr_t)sizes, sizeof(sizes),
1044 		     ATA_R_READ | ATA_R_QUIET, 30)) {
1045 	bzero(&cdp->toc, sizeof(cdp->toc));
1046 	return;
1047     }
1048     cdp->disk_size = ntohl(sizes[0]) + 1;
1049     acd_set_ioparm(dev);
1050 
1051     for (track = 1; track <= ntracks; track ++) {
1052 	if (cdp->track[track] != NULL)
1053 	    continue;
1054 #if 0
1055 	tracknode = acd_make_tracknode(dev, track);
1056 #endif
1057 	tracknode = NULL;
1058 	cdp->track[track] = tracknode;
1059     }
1060     for (; track < MAXTRK; track ++) {
1061 	if (cdp->track[track] == NULL)
1062 	    continue;
1063 	acd_destroy_tracknode(dev, track);
1064 	cdp->track[track] = NULL;
1065     }
1066 
1067 #ifdef ACD_DEBUG
1068     if (cdp->disk_size && cdp->toc.hdr.ending_track) {
1069 	device_printf(dev, "(%d sectors (%d bytes)), %d tracks ",
1070 		      cdp->disk_size, cdp->block_size,
1071 		      cdp->toc.hdr.ending_track-cdp->toc.hdr.starting_track+1);
1072 	if (cdp->toc.tab[0].control & 4)
1073 	    kprintf("%dMB\n", cdp->disk_size * cdp->block_size / 1048576);
1074 	else
1075 	    kprintf("%d:%d audio\n",
1076 		   cdp->disk_size / 75 / 60, cdp->disk_size / 75 % 60);
1077     }
1078 #endif
1079 }
1080 
1081 /*
1082  * Makes a new device node for the numbered track and returns a struct
1083  * acd_tracknode pointer to be included in the list of tracks available on the
1084  * medium.
1085  */
1086 
1087 #if 0
1088 
1089 static struct acd_tracknode *
1090 acd_make_tracknode(device_t dev, int track)
1091 {
1092     struct acd_softc *cdp = device_get_ivars(dev);
1093     struct acd_tracknode *tracknode;
1094     char name[16];
1095 
1096     ksprintf(name, "acd%dt%d", device_get_unit(dev), track);
1097     tracknode = kmalloc(sizeof(struct acd_tracknode), M_ACD, M_WAITOK | M_ZERO);
1098     tracknode->cdev = make_dev(&acd_ops, (device_get_unit(dev) << 3) |
1099 			      (track << 16), UID_ROOT, GID_OPERATOR, 0644,
1100 			      name, NULL);
1101     tracknode->cdev->si_drv1 = cdp->cdev->si_drv1;
1102     reference_dev(tracknode->cdev);
1103     return tracknode;
1104 }
1105 
1106 #endif
1107 
1108 /*
1109  * Destroys the device node of a numbered track and frees the related struct
1110  * acd_tracknode. It could be done just in acd_read_toc(), but it's nice to
1111  * have a complementary function to acd_make_tracknode().
1112  */
1113 static void
1114 acd_destroy_tracknode(device_t dev, int track)
1115 {
1116     struct acd_softc *cdp = device_get_ivars(dev);
1117     struct acd_tracknode *tracknode;
1118 
1119     tracknode = cdp->track[track];
1120     destroy_dev(tracknode->cdev);
1121     kfree(tracknode, M_ACD);
1122 }
1123 
1124 static int
1125 acd_play(device_t dev, int start, int end)
1126 {
1127     int8_t ccb[16];
1128 
1129     bzero(ccb, sizeof(ccb));
1130     ccb[0] = ATAPI_PLAY_MSF;
1131     lba2msf(start, &ccb[3], &ccb[4], &ccb[5]);
1132     lba2msf(end, &ccb[6], &ccb[7], &ccb[8]);
1133     return ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
1134 }
1135 
1136 static int
1137 acd_setchan(device_t dev, u_int8_t c0, u_int8_t c1, u_int8_t c2, u_int8_t c3)
1138 {
1139     struct acd_softc *cdp = device_get_ivars(dev);
1140     int error;
1141 
1142     if ((error = acd_mode_sense(dev, ATAPI_CDROM_AUDIO_PAGE, (caddr_t)&cdp->au,
1143 				sizeof(cdp->au))))
1144 	return error;
1145     if (cdp->au.page_code != ATAPI_CDROM_AUDIO_PAGE)
1146 	return EIO;
1147     cdp->au.data_length = 0;
1148     cdp->au.port[0].channels = c0;
1149     cdp->au.port[1].channels = c1;
1150     cdp->au.port[2].channels = c2;
1151     cdp->au.port[3].channels = c3;
1152     return acd_mode_select(dev, (caddr_t)&cdp->au, sizeof(cdp->au));
1153 }
1154 
1155 static int
1156 acd_init_writer(device_t dev, int test_write)
1157 {
1158     int8_t ccb[16];
1159 
1160     bzero(ccb, sizeof(ccb));
1161     ccb[0] = ATAPI_REZERO;
1162     ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1163     ccb[0] = ATAPI_SEND_OPC_INFO;
1164     ccb[1] = 0x01;
1165     ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 30);
1166     return 0;
1167 }
1168 
1169 static int
1170 acd_fixate(device_t dev, int multisession)
1171 {
1172     struct acd_softc *cdp = device_get_ivars(dev);
1173     int8_t ccb[16] = { ATAPI_CLOSE_TRACK, 0x01, 0x02, 0, 0, 0, 0, 0,
1174 		       0, 0, 0, 0, 0, 0, 0, 0 };
1175     int timeout = 5*60*2;
1176     int error, dummy;
1177     struct write_param param;
1178 
1179     if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1180 				(caddr_t)&param, sizeof(param))))
1181 	return error;
1182 
1183     param.data_length = 0;
1184     if (multisession)
1185 	param.session_type = CDR_SESS_MULTI;
1186     else
1187 	param.session_type = CDR_SESS_NONE;
1188 
1189     if ((error = acd_mode_select(dev, (caddr_t)&param, param.page_length + 10)))
1190 	return error;
1191 
1192     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1193     if (error)
1194 	return error;
1195 
1196     /* some drives just return ready, wait for the expected fixate time */
1197     if ((error = acd_test_ready(dev)) != EBUSY) {
1198 	timeout = timeout / (cdp->cap.cur_write_speed / 177);
1199 	tsleep(&error, 0, "acdfix", timeout * hz / 2);
1200 	return acd_test_ready(dev);
1201     }
1202 
1203     while (timeout-- > 0) {
1204 	if ((error = acd_get_progress(dev, &dummy)))
1205 	    return error;
1206 	if ((error = acd_test_ready(dev)) != EBUSY)
1207 	    return error;
1208 	tsleep(&error, 0, "acdcld", hz / 2);
1209     }
1210     return EIO;
1211 }
1212 
1213 static int
1214 acd_init_track(device_t dev, struct cdr_track *track)
1215 {
1216     struct acd_softc *cdp = device_get_ivars(dev);
1217     struct write_param param;
1218     int error;
1219 
1220     if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1221 				(caddr_t)&param, sizeof(param))))
1222 	return error;
1223 
1224     param.data_length = 0;
1225     param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1226     param.page_length = 0x32;
1227     param.test_write = track->test_write ? 1 : 0;
1228     param.write_type = CDR_WTYPE_TRACK;
1229     param.session_type = CDR_SESS_NONE;
1230     param.fp = 0;
1231     param.packet_size = 0;
1232 
1233     if (cdp->cap.capabilities & MST_BURNPROOF)
1234 	param.burnproof = 1;
1235 
1236     switch (track->datablock_type) {
1237 
1238     case CDR_DB_RAW:
1239 	if (track->preemp)
1240 	    param.track_mode = CDR_TMODE_AUDIO_PREEMP;
1241 	else
1242 	    param.track_mode = CDR_TMODE_AUDIO;
1243 	cdp->block_size = 2352;
1244 	param.datablock_type = CDR_DB_RAW;
1245 	param.session_format = CDR_SESS_CDROM;
1246 	break;
1247 
1248     case CDR_DB_ROM_MODE1:
1249 	cdp->block_size = 2048;
1250 	param.track_mode = CDR_TMODE_DATA;
1251 	param.datablock_type = CDR_DB_ROM_MODE1;
1252 	param.session_format = CDR_SESS_CDROM;
1253 	break;
1254 
1255     case CDR_DB_ROM_MODE2:
1256 	cdp->block_size = 2336;
1257 	param.track_mode = CDR_TMODE_DATA;
1258 	param.datablock_type = CDR_DB_ROM_MODE2;
1259 	param.session_format = CDR_SESS_CDROM;
1260 	break;
1261 
1262     case CDR_DB_XA_MODE1:
1263 	cdp->block_size = 2048;
1264 	param.track_mode = CDR_TMODE_DATA;
1265 	param.datablock_type = CDR_DB_XA_MODE1;
1266 	param.session_format = CDR_SESS_CDROM_XA;
1267 	break;
1268 
1269     case CDR_DB_XA_MODE2_F1:
1270 	cdp->block_size = 2056;
1271 	param.track_mode = CDR_TMODE_DATA;
1272 	param.datablock_type = CDR_DB_XA_MODE2_F1;
1273 	param.session_format = CDR_SESS_CDROM_XA;
1274 	break;
1275 
1276     case CDR_DB_XA_MODE2_F2:
1277 	cdp->block_size = 2324;
1278 	param.track_mode = CDR_TMODE_DATA;
1279 	param.datablock_type = CDR_DB_XA_MODE2_F2;
1280 	param.session_format = CDR_SESS_CDROM_XA;
1281 	break;
1282 
1283     case CDR_DB_XA_MODE2_MIX:
1284 	cdp->block_size = 2332;
1285 	param.track_mode = CDR_TMODE_DATA;
1286 	param.datablock_type = CDR_DB_XA_MODE2_MIX;
1287 	param.session_format = CDR_SESS_CDROM_XA;
1288 	break;
1289     }
1290     acd_set_ioparm(dev);
1291     return acd_mode_select(dev, (caddr_t)&param, param.page_length + 10);
1292 }
1293 
1294 static int
1295 acd_flush(device_t dev)
1296 {
1297     int8_t ccb[16] = { ATAPI_SYNCHRONIZE_CACHE, 0, 0, 0, 0, 0, 0, 0,
1298 		       0, 0, 0, 0, 0, 0, 0, 0 };
1299 
1300     return ata_atapicmd(dev, ccb, NULL, 0, ATA_R_QUIET, 60);
1301 }
1302 
1303 static int
1304 acd_read_track_info(device_t dev, int32_t lba, struct acd_track_info *info)
1305 {
1306     int8_t ccb[16] = { ATAPI_READ_TRACK_INFO, 1,
1307 		       lba>>24, lba>>16, lba>>8, lba, 0,
1308 		       sizeof(*info)>>8, sizeof(*info),
1309 		       0, 0, 0, 0, 0, 0, 0 };
1310     int error;
1311 
1312     if ((error = ata_atapicmd(dev, ccb, (caddr_t)info, sizeof(*info),
1313 			      ATA_R_READ, 30)))
1314 	return error;
1315     info->track_start_addr = ntohl(info->track_start_addr);
1316     info->next_writeable_addr = ntohl(info->next_writeable_addr);
1317     info->free_blocks = ntohl(info->free_blocks);
1318     info->fixed_packet_size = ntohl(info->fixed_packet_size);
1319     info->track_length = ntohl(info->track_length);
1320     return 0;
1321 }
1322 
1323 static int
1324 acd_get_progress(device_t dev, int *finished)
1325 {
1326     int8_t ccb[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
1327 		       0, 0, 0, 0, 0, 0, 0, 0 };
1328     struct ata_request *request;
1329     int8_t dummy[8];
1330 
1331     if (!(request = ata_alloc_request()))
1332 	return ENOMEM;
1333 
1334     request->dev = dev;
1335     bcopy(ccb, request->u.atapi.ccb, 16);
1336     request->data = dummy;
1337     request->bytecount = sizeof(dummy);
1338     request->transfersize = min(request->bytecount, 65534);
1339     request->flags = ATA_R_ATAPI | ATA_R_READ;
1340     request->timeout = 30;
1341     ata_queue_request(request);
1342     if (!request->error &&
1343 	request->u.atapi.sense.specific & ATA_SENSE_SPEC_VALID)
1344 	*finished = ((request->u.atapi.sense.specific2 |
1345 		     (request->u.atapi.sense.specific1 << 8)) * 100) / 65535;
1346     else
1347 	*finished = 0;
1348     ata_free_request(request);
1349     return 0;
1350 }
1351 
1352 static int
1353 acd_send_cue(device_t dev, struct cdr_cuesheet *cuesheet)
1354 {
1355     struct acd_softc *cdp = device_get_ivars(dev);
1356     struct write_param param;
1357     int8_t ccb[16] = { ATAPI_SEND_CUE_SHEET, 0, 0, 0, 0, 0,
1358 		       cuesheet->len>>16, cuesheet->len>>8, cuesheet->len,
1359 		       0, 0, 0, 0, 0, 0, 0 };
1360     int8_t *buffer;
1361     int32_t error;
1362 
1363     if ((error = acd_mode_sense(dev, ATAPI_CDROM_WRITE_PARAMETERS_PAGE,
1364 				(caddr_t)&param, sizeof(param))))
1365 	return error;
1366 
1367     param.data_length = 0;
1368     param.page_code = ATAPI_CDROM_WRITE_PARAMETERS_PAGE;
1369     param.page_length = 0x32;
1370     param.test_write = cuesheet->test_write ? 1 : 0;
1371     param.write_type = CDR_WTYPE_SESSION;
1372     param.session_type = cuesheet->session_type;
1373     param.fp = 0;
1374     param.packet_size = 0;
1375     param.track_mode = CDR_TMODE_AUDIO;
1376     param.datablock_type = CDR_DB_RAW;
1377     param.session_format = cuesheet->session_format;
1378     if (cdp->cap.capabilities & MST_BURNPROOF)
1379 	param.burnproof = 1;
1380 
1381     if ((error = acd_mode_select(dev, (caddr_t)&param, param.page_length + 10)))
1382 	return error;
1383 
1384     if (!(buffer = kmalloc(cuesheet->len, M_ACD, M_WAITOK | M_NULLOK)))
1385 	return ENOMEM;
1386 
1387     if (!(error = copyin(cuesheet->entries, buffer, cuesheet->len)))
1388 	error = ata_atapicmd(dev, ccb, buffer, cuesheet->len, 0, 30);
1389     kfree(buffer, M_ACD);
1390     return error;
1391 }
1392 
1393 static int
1394 acd_report_key(device_t dev, struct dvd_authinfo *ai)
1395 {
1396     struct dvd_miscauth *d = NULL;
1397     u_int32_t lba = 0;
1398     int16_t length;
1399     int8_t ccb[16];
1400     int error;
1401 
1402     switch (ai->format) {
1403     case DVD_REPORT_AGID:
1404     case DVD_REPORT_ASF:
1405     case DVD_REPORT_RPC:
1406 	length = 8;
1407 	break;
1408     case DVD_REPORT_KEY1:
1409 	length = 12;
1410 	break;
1411     case DVD_REPORT_TITLE_KEY:
1412 	length = 12;
1413 	lba = ai->lba;
1414 	break;
1415     case DVD_REPORT_CHALLENGE:
1416 	length = 16;
1417 	break;
1418     case DVD_INVALIDATE_AGID:
1419 	length = 0;
1420 	break;
1421     default:
1422 	return EINVAL;
1423     }
1424 
1425     bzero(ccb, sizeof(ccb));
1426     ccb[0] = ATAPI_REPORT_KEY;
1427     ccb[2] = (lba >> 24) & 0xff;
1428     ccb[3] = (lba >> 16) & 0xff;
1429     ccb[4] = (lba >> 8) & 0xff;
1430     ccb[5] = lba & 0xff;
1431     ccb[8] = (length >> 8) & 0xff;
1432     ccb[9] = length & 0xff;
1433     ccb[10] = (ai->agid << 6) | ai->format;
1434 
1435     if (length) {
1436 	d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1437 	d->length = htons(length - 2);
1438     }
1439 
1440     error = ata_atapicmd(dev, ccb, (caddr_t)d, length,
1441 			 ai->format == DVD_INVALIDATE_AGID ? 0 : ATA_R_READ,10);
1442     if (error) {
1443 	if (length)
1444 	    kfree(d, M_ACD);
1445 	return error;
1446     }
1447 
1448     switch (ai->format) {
1449     case DVD_REPORT_AGID:
1450 	ai->agid = d->data[3] >> 6;
1451 	break;
1452 
1453     case DVD_REPORT_CHALLENGE:
1454 	bcopy(&d->data[0], &ai->keychal[0], 10);
1455 	break;
1456 
1457     case DVD_REPORT_KEY1:
1458 	bcopy(&d->data[0], &ai->keychal[0], 5);
1459 	break;
1460 
1461     case DVD_REPORT_TITLE_KEY:
1462 	ai->cpm = (d->data[0] >> 7);
1463 	ai->cp_sec = (d->data[0] >> 6) & 0x1;
1464 	ai->cgms = (d->data[0] >> 4) & 0x3;
1465 	bcopy(&d->data[1], &ai->keychal[0], 5);
1466 	break;
1467 
1468     case DVD_REPORT_ASF:
1469 	ai->asf = d->data[3] & 1;
1470 	break;
1471 
1472     case DVD_REPORT_RPC:
1473 	ai->reg_type = (d->data[0] >> 6);
1474 	ai->vend_rsts = (d->data[0] >> 3) & 0x7;
1475 	ai->user_rsts = d->data[0] & 0x7;
1476 	ai->region = d->data[1];
1477 	ai->rpc_scheme = d->data[2];
1478 	break;
1479 
1480     case DVD_INVALIDATE_AGID:
1481 	break;
1482 
1483     default:
1484 	error = EINVAL;
1485     }
1486     if (length)
1487 	kfree(d, M_ACD);
1488     return error;
1489 }
1490 
1491 static int
1492 acd_send_key(device_t dev, struct dvd_authinfo *ai)
1493 {
1494     struct dvd_miscauth *d;
1495     int16_t length;
1496     int8_t ccb[16];
1497     int error;
1498 
1499     switch (ai->format) {
1500     case DVD_SEND_CHALLENGE:
1501 	length = 16;
1502 	d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1503 	bcopy(ai->keychal, &d->data[0], 10);
1504 	break;
1505 
1506     case DVD_SEND_KEY2:
1507 	length = 12;
1508 	d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1509 	bcopy(&ai->keychal[0], &d->data[0], 5);
1510 	break;
1511 
1512     case DVD_SEND_RPC:
1513 	length = 8;
1514 	d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1515 	d->data[0] = ai->region;
1516 	break;
1517 
1518     default:
1519 	return EINVAL;
1520     }
1521 
1522     bzero(ccb, sizeof(ccb));
1523     ccb[0] = ATAPI_SEND_KEY;
1524     ccb[8] = (length >> 8) & 0xff;
1525     ccb[9] = length & 0xff;
1526     ccb[10] = (ai->agid << 6) | ai->format;
1527     d->length = htons(length - 2);
1528     error = ata_atapicmd(dev, ccb, (caddr_t)d, length, 0, 10);
1529     kfree(d, M_ACD);
1530     return error;
1531 }
1532 
1533 static int
1534 acd_read_structure(device_t dev, struct dvd_struct *s)
1535 {
1536     struct dvd_miscauth *d;
1537     u_int16_t length;
1538     int8_t ccb[16];
1539     int error = 0;
1540 
1541     switch(s->format) {
1542     case DVD_STRUCT_PHYSICAL:
1543 	length = 21;
1544 	break;
1545 
1546     case DVD_STRUCT_COPYRIGHT:
1547 	length = 8;
1548 	break;
1549 
1550     case DVD_STRUCT_DISCKEY:
1551 	length = 2052;
1552 	break;
1553 
1554     case DVD_STRUCT_BCA:
1555 	length = 192;
1556 	break;
1557 
1558     case DVD_STRUCT_MANUFACT:
1559 	length = 2052;
1560 	break;
1561 
1562     case DVD_STRUCT_DDS:
1563     case DVD_STRUCT_PRERECORDED:
1564     case DVD_STRUCT_UNIQUEID:
1565     case DVD_STRUCT_LIST:
1566     case DVD_STRUCT_CMI:
1567     case DVD_STRUCT_RMD_LAST:
1568     case DVD_STRUCT_RMD_RMA:
1569     case DVD_STRUCT_DCB:
1570 	return ENOSYS;
1571 
1572     default:
1573 	return EINVAL;
1574     }
1575 
1576     d = kmalloc(length, M_ACD, M_WAITOK | M_ZERO);
1577     d->length = htons(length - 2);
1578 
1579     bzero(ccb, sizeof(ccb));
1580     ccb[0] = ATAPI_READ_STRUCTURE;
1581     ccb[6] = s->layer_num;
1582     ccb[7] = s->format;
1583     ccb[8] = (length >> 8) & 0xff;
1584     ccb[9] = length & 0xff;
1585     ccb[10] = s->agid << 6;
1586     error = ata_atapicmd(dev, ccb, (caddr_t)d, length, ATA_R_READ, 30);
1587     if (error) {
1588 	kfree(d, M_ACD);
1589 	return error;
1590     }
1591 
1592     switch (s->format) {
1593     case DVD_STRUCT_PHYSICAL: {
1594 	struct dvd_layer *layer = (struct dvd_layer *)&s->data[0];
1595 
1596 	layer->book_type = d->data[0] >> 4;
1597 	layer->book_version = d->data[0] & 0xf;
1598 	layer->disc_size = d->data[1] >> 4;
1599 	layer->max_rate = d->data[1] & 0xf;
1600 	layer->nlayers = (d->data[2] >> 5) & 3;
1601 	layer->track_path = (d->data[2] >> 4) & 1;
1602 	layer->layer_type = d->data[2] & 0xf;
1603 	layer->linear_density = d->data[3] >> 4;
1604 	layer->track_density = d->data[3] & 0xf;
1605 	layer->start_sector = d->data[5] << 16 | d->data[6] << 8 | d->data[7];
1606 	layer->end_sector = d->data[9] << 16 | d->data[10] << 8 | d->data[11];
1607 	layer->end_sector_l0 = d->data[13] << 16 | d->data[14] << 8|d->data[15];
1608 	layer->bca = d->data[16] >> 7;
1609 	break;
1610     }
1611 
1612     case DVD_STRUCT_COPYRIGHT:
1613 	s->cpst = d->data[0];
1614 	s->rmi = d->data[1];
1615 	break;
1616 
1617     case DVD_STRUCT_DISCKEY:
1618 	bcopy(&d->data[0], &s->data[0], 2048);
1619 	break;
1620 
1621     case DVD_STRUCT_BCA:
1622 	s->length = ntohs(d->length);
1623 	bcopy(&d->data[0], &s->data[0], s->length);
1624 	break;
1625 
1626     case DVD_STRUCT_MANUFACT:
1627 	s->length = ntohs(d->length);
1628 	bcopy(&d->data[0], &s->data[0], s->length);
1629 	break;
1630 
1631     default:
1632 	error = EINVAL;
1633     }
1634     kfree(d, M_ACD);
1635     return error;
1636 }
1637 
1638 static int
1639 acd_tray(device_t dev, int close)
1640 {
1641     struct ata_device *atadev = device_get_softc(dev);
1642     struct acd_softc *cdp = device_get_ivars(dev);
1643     int error = ENODEV;
1644 
1645     if (cdp->cap.mechanism & MST_EJECT) {
1646 	if (close) {
1647 	    if (!(error = acd_start_stop(dev, 3))) {
1648 		acd_read_toc(dev);
1649 		acd_prevent_allow(dev, 1);
1650 		cdp->flags |= F_LOCKED;
1651 	    }
1652 	}
1653 	else {
1654 	    acd_start_stop(dev, 0);
1655 	    acd_prevent_allow(dev, 0);
1656 	    cdp->flags &= ~F_LOCKED;
1657 	    atadev->flags |= ATA_D_MEDIA_CHANGED;
1658 	    error = acd_start_stop(dev, 2);
1659 	}
1660     }
1661     return error;
1662 }
1663 
1664 static int
1665 acd_blank(device_t dev, int blanktype)
1666 {
1667     struct ata_device *atadev = device_get_softc(dev);
1668     int8_t ccb[16] = { ATAPI_BLANK, 0x10 | (blanktype & 0x7), 0, 0, 0, 0, 0, 0,
1669 		       0, 0, 0, 0, 0, 0, 0, 0 };
1670 
1671     atadev->flags |= ATA_D_MEDIA_CHANGED;
1672     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1673 }
1674 
1675 static int
1676 acd_prevent_allow(device_t dev, int lock)
1677 {
1678     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
1679 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1680 
1681     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1682 }
1683 
1684 static int
1685 acd_start_stop(device_t dev, int start)
1686 {
1687     int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
1688 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1689 
1690     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1691 }
1692 
1693 static int
1694 acd_pause_resume(device_t dev, int pause)
1695 {
1696     int8_t ccb[16] = { ATAPI_PAUSE, 0, 0, 0, 0, 0, 0, 0, pause,
1697 		       0, 0, 0, 0, 0, 0, 0 };
1698 
1699     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1700 }
1701 
1702 static int
1703 acd_mode_sense(device_t dev, int page, caddr_t pagebuf, int pagesize)
1704 {
1705     int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, page, 0, 0, 0, 0,
1706 		       pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1707     int error;
1708 
1709     error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
1710     return error;
1711 }
1712 
1713 static int
1714 acd_mode_select(device_t dev, caddr_t pagebuf, int pagesize)
1715 {
1716     int8_t ccb[16] = { ATAPI_MODE_SELECT_BIG, 0x10, 0, 0, 0, 0, 0,
1717 		     pagesize>>8, pagesize, 0, 0, 0, 0, 0, 0, 0 };
1718 
1719     return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 30);
1720 }
1721 
1722 static int
1723 acd_set_speed(device_t dev, int rdspeed, int wrspeed)
1724 {
1725     int8_t ccb[16] = { ATAPI_SET_SPEED, 0, rdspeed >> 8, rdspeed,
1726 		       wrspeed >> 8, wrspeed, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1727     int error;
1728 
1729     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1730     if (!error)
1731 	acd_get_cap(dev);
1732     return error;
1733 }
1734 
1735 static void
1736 acd_get_cap(device_t dev)
1737 {
1738     struct acd_softc *cdp = device_get_ivars(dev);
1739     int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_CDROM_CAP_PAGE,
1740 		       0, 0, 0, 0, sizeof(cdp->cap)>>8, sizeof(cdp->cap),
1741 		       0, 0, 0, 0, 0, 0, 0 };
1742     int count;
1743 
1744     /* get drive capabilities, some bugridden drives needs this repeated */
1745     for (count = 0 ; count < 5 ; count++) {
1746 	if (!ata_atapicmd(dev, ccb, (caddr_t)&cdp->cap, sizeof(cdp->cap),
1747 			  ATA_R_READ | ATA_R_QUIET, 5)) {
1748 	    cdp->cap.max_read_speed = ntohs(cdp->cap.max_read_speed);
1749 	    cdp->cap.cur_read_speed = ntohs(cdp->cap.cur_read_speed);
1750 	    cdp->cap.max_write_speed = ntohs(cdp->cap.max_write_speed);
1751 	    cdp->cap.cur_write_speed = max(ntohs(cdp->cap.cur_write_speed),177);
1752 	    cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels);
1753 	    cdp->cap.buf_size = ntohs(cdp->cap.buf_size);
1754 	}
1755     }
1756 }
1757 
1758 #ifdef ACD_CDR_FORMAT
1759 static int
1760 acd_read_format_caps(device_t dev, struct cdr_format_capacities *caps)
1761 {
1762     int8_t ccb[16] = { ATAPI_READ_FORMAT_CAPACITIES, 0, 0, 0, 0, 0, 0,
1763 		       (sizeof(struct cdr_format_capacities) >> 8) & 0xff,
1764 		       sizeof(struct cdr_format_capacities) & 0xff,
1765 		       0, 0, 0, 0, 0, 0, 0 };
1766 
1767     return ata_atapicmd(dev, ccb, (caddr_t)caps,
1768 			sizeof(struct cdr_format_capacities), ATA_R_READ, 30);
1769 }
1770 
1771 static int
1772 acd_format(device_t dev, struct cdr_format_params* params)
1773 {
1774     int8_t ccb[16] = { ATAPI_FORMAT, 0x11, 0, 0, 0, 0, 0, 0, 0, 0,
1775 		       0, 0, 0, 0, 0, 0 };
1776     int error;
1777 
1778     error = ata_atapicmd(dev, ccb, (u_int8_t *)params,
1779 			 sizeof(struct cdr_format_params), 0, 30);
1780     return error;
1781 }
1782 #endif /* ACD_CDR_FORMAT */
1783 
1784 static int
1785 acd_test_ready(device_t dev)
1786 {
1787     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
1788 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1789 
1790     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
1791 }
1792 
1793 static void
1794 acd_describe(device_t dev)
1795 {
1796     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1797     struct ata_device *atadev = device_get_softc(dev);
1798     struct acd_softc *cdp = device_get_ivars(dev);
1799     int comma = 0;
1800     char *mechanism;
1801 
1802     if (bootverbose) {
1803 	device_printf(dev, "<%.40s/%.8s> %s drive at ata%d as %s\n",
1804 		      atadev->param.model, atadev->param.revision,
1805 		      (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1806 		       (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1807 			(cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1808 			 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1809 			  (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM":"CDROM",
1810 		      device_get_unit(ch->dev),
1811 		      (atadev->unit == ATA_MASTER) ? "master" : "slave");
1812 
1813 	device_printf(dev, "%s", "");
1814 	if (cdp->cap.cur_read_speed) {
1815 	    kprintf("read %dKB/s", cdp->cap.cur_read_speed * 1000 / 1024);
1816 	    if (cdp->cap.max_read_speed)
1817 		kprintf(" (%dKB/s)", cdp->cap.max_read_speed * 1000 / 1024);
1818 	    if ((cdp->cap.cur_write_speed) &&
1819 		(cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
1820 				   MST_WRITE_DVDR | MST_WRITE_DVDRAM))) {
1821 		kprintf(" write %dKB/s", cdp->cap.cur_write_speed * 1000 / 1024);
1822 		if (cdp->cap.max_write_speed)
1823 		    kprintf(" (%dKB/s)", cdp->cap.max_write_speed * 1000 / 1024);
1824 	    }
1825 	    comma = 1;
1826 	}
1827 	if (cdp->cap.buf_size) {
1828 	    kprintf("%s %dKB buffer", comma ? "," : "", cdp->cap.buf_size);
1829 	    comma = 1;
1830 	}
1831 	kprintf("%s %s\n", comma ? "," : "", ata_mode2str(atadev->mode));
1832 
1833 	device_printf(dev, "Reads:");
1834 	comma = 0;
1835 	if (cdp->cap.media & MST_READ_CDR) {
1836 	    kprintf(" CDR"); comma = 1;
1837 	}
1838 	if (cdp->cap.media & MST_READ_CDRW) {
1839 	    kprintf("%s CDRW", comma ? "," : ""); comma = 1;
1840 	}
1841 	if (cdp->cap.capabilities & MST_READ_CDDA) {
1842 	    if (cdp->cap.capabilities & MST_CDDA_STREAM)
1843 		kprintf("%s CDDA stream", comma ? "," : "");
1844 	    else
1845 		kprintf("%s CDDA", comma ? "," : "");
1846 	    comma = 1;
1847 	}
1848 	if (cdp->cap.media & MST_READ_DVDROM) {
1849 	    kprintf("%s DVDROM", comma ? "," : ""); comma = 1;
1850 	}
1851 	if (cdp->cap.media & MST_READ_DVDR) {
1852 	    kprintf("%s DVDR", comma ? "," : ""); comma = 1;
1853 	}
1854 	if (cdp->cap.media & MST_READ_DVDRAM) {
1855 	    kprintf("%s DVDRAM", comma ? "," : ""); comma = 1;
1856 	}
1857 	if (cdp->cap.media & MST_READ_PACKET)
1858 	    kprintf("%s packet", comma ? "," : "");
1859 
1860 	kprintf("\n");
1861 	device_printf(dev, "Writes:");
1862 	if (cdp->cap.media & (MST_WRITE_CDR | MST_WRITE_CDRW |
1863 			      MST_WRITE_DVDR | MST_WRITE_DVDRAM)) {
1864 	    comma = 0;
1865 	    if (cdp->cap.media & MST_WRITE_CDR) {
1866 		kprintf(" CDR" ); comma = 1;
1867 	    }
1868 	    if (cdp->cap.media & MST_WRITE_CDRW) {
1869 		kprintf("%s CDRW", comma ? "," : ""); comma = 1;
1870 	    }
1871 	    if (cdp->cap.media & MST_WRITE_DVDR) {
1872 		kprintf("%s DVDR", comma ? "," : ""); comma = 1;
1873 	    }
1874 	    if (cdp->cap.media & MST_WRITE_DVDRAM) {
1875 		kprintf("%s DVDRAM", comma ? "," : ""); comma = 1;
1876 	    }
1877 	    if (cdp->cap.media & MST_WRITE_TEST) {
1878 		kprintf("%s test write", comma ? "," : ""); comma = 1;
1879 	    }
1880 	    if (cdp->cap.capabilities & MST_BURNPROOF)
1881 		kprintf("%s burnproof", comma ? "," : "");
1882 	}
1883 	kprintf("\n");
1884 	if (cdp->cap.capabilities & MST_AUDIO_PLAY) {
1885 	    device_printf(dev, "Audio: ");
1886 	    if (cdp->cap.capabilities & MST_AUDIO_PLAY)
1887 		kprintf("play");
1888 	    if (cdp->cap.max_vol_levels)
1889 		kprintf(", %d volume levels", cdp->cap.max_vol_levels);
1890 	    kprintf("\n");
1891 	}
1892 	device_printf(dev, "Mechanism: ");
1893 	switch (cdp->cap.mechanism & MST_MECH_MASK) {
1894 	case MST_MECH_CADDY:
1895 	    mechanism = "caddy"; break;
1896 	case MST_MECH_TRAY:
1897 	    mechanism = "tray"; break;
1898 	case MST_MECH_POPUP:
1899 	    mechanism = "popup"; break;
1900 	case MST_MECH_CHANGER:
1901 	    mechanism = "changer"; break;
1902 	case MST_MECH_CARTRIDGE:
1903 	    mechanism = "cartridge"; break;
1904 	default:
1905 	    mechanism = 0; break;
1906 	}
1907 	if (mechanism)
1908 	    kprintf("%s%s", (cdp->cap.mechanism & MST_EJECT) ?
1909 		   "ejectable " : "", mechanism);
1910 	else if (cdp->cap.mechanism & MST_EJECT)
1911 	    kprintf("ejectable");
1912 
1913 	if (cdp->cap.mechanism & MST_LOCKABLE)
1914 	    kprintf((cdp->cap.mechanism & MST_LOCKED) ? ", locked":", unlocked");
1915 	if (cdp->cap.mechanism & MST_PREVENT)
1916 	    kprintf(", lock protected");
1917 	kprintf("\n");
1918 
1919 	if ((cdp->cap.mechanism & MST_MECH_MASK) != MST_MECH_CHANGER) {
1920 	    device_printf(dev, "Medium: ");
1921 	    switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1922 	    case MST_CDROM:
1923 		kprintf("CD-ROM "); break;
1924 	    case MST_CDR:
1925 		kprintf("CD-R "); break;
1926 	    case MST_CDRW:
1927 		kprintf("CD-RW "); break;
1928 	    case MST_DVD:
1929 	        kprintf("DVD "); break;
1930 	    case MST_DOOR_OPEN:
1931 		kprintf("door open"); break;
1932 	    case MST_NO_DISC:
1933 		kprintf("no/blank disc"); break;
1934 	    case MST_FMT_ERROR:
1935 		kprintf("medium format error"); break;
1936 	    }
1937 	    if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH)<MST_TYPE_MASK_HIGH){
1938 		switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) {
1939 		case MST_DATA_120:
1940 		    kprintf("120mm data disc"); break;
1941 		case MST_AUDIO_120:
1942 		    kprintf("120mm audio disc"); break;
1943 		case MST_COMB_120:
1944 		    kprintf("120mm data/audio disc"); break;
1945 		case MST_PHOTO_120:
1946 		    kprintf("120mm photo disc"); break;
1947 		case MST_DATA_80:
1948 		    kprintf("80mm data disc"); break;
1949 		case MST_AUDIO_80:
1950 		    kprintf("80mm audio disc"); break;
1951 		case MST_COMB_80:
1952 		    kprintf("80mm data/audio disc"); break;
1953 		case MST_PHOTO_80:
1954 		    kprintf("80mm photo disc"); break;
1955 		case MST_FMT_NONE:
1956 		    switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) {
1957 		    case MST_CDROM:
1958 			kprintf("unknown"); break;
1959 		    case MST_CDR:
1960 		    case MST_CDRW:
1961 			kprintf("blank"); break;
1962 		    }
1963 		    break;
1964 		default:
1965 		    kprintf("unknown (0x%x)", cdp->cap.medium_type); break;
1966 		}
1967 	    }
1968 	    kprintf("\n");
1969 	}
1970     }
1971     else {
1972 	device_printf(dev, "%s ",
1973 		      (cdp->cap.media & MST_WRITE_DVDR) ? "DVDR" :
1974 		       (cdp->cap.media & MST_WRITE_DVDRAM) ? "DVDRAM" :
1975 			(cdp->cap.media & MST_WRITE_CDRW) ? "CDRW" :
1976 			 (cdp->cap.media & MST_WRITE_CDR) ? "CDR" :
1977 			  (cdp->cap.media & MST_READ_DVDROM) ? "DVDROM" :
1978 			  "CDROM");
1979 	kprintf("<%.40s/%.8s> at ata%d-%s %s\n",
1980 	       atadev->param.model, atadev->param.revision,
1981 	       device_get_unit(ch->dev),
1982 	       (atadev->unit == ATA_MASTER) ? "master" : "slave",
1983 	       ata_mode2str(atadev->mode) );
1984     }
1985 }
1986 
1987 static device_method_t acd_methods[] = {
1988     /* device interface */
1989     DEVMETHOD(device_probe,     acd_probe),
1990     DEVMETHOD(device_attach,    acd_attach),
1991     DEVMETHOD(device_detach,    acd_detach),
1992     DEVMETHOD(device_shutdown,  acd_shutdown),
1993 
1994     /* ATA methods */
1995     DEVMETHOD(ata_reinit,       acd_reinit),
1996 
1997     { 0, 0 }
1998 };
1999 
2000 static driver_t acd_driver = {
2001     "acd",
2002     acd_methods,
2003     0,
2004 };
2005 
2006 static devclass_t acd_devclass;
2007 
2008 DRIVER_MODULE(acd, ata, acd_driver, acd_devclass, NULL, NULL);
2009 MODULE_VERSION(acd, 1);
2010 MODULE_DEPEND(acd, ata, 1, 1, 1);
2011