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