xref: /openbsd-src/sys/scsi/cd.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: cd.c,v 1.146 2009/02/16 21:19:07 miod Exp $	*/
2 /*	$NetBSD: cd.c,v 1.100 1997/04/02 02:29:30 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Charles M. Hannum.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Originally written by Julian Elischer (julian@tfs.com)
35  * for TRW Financial Systems for use under the MACH(2.5) operating system.
36  *
37  * TRW Financial Systems, in accordance with their agreement with Carnegie
38  * Mellon University, makes this software available to CMU to distribute
39  * or use in any manner that they see fit as long as this message is kept with
40  * the software. For this reason TFS also grants any other persons or
41  * organisations permission to use or modify this software.
42  *
43  * TFS supplies this software to be publicly redistributed
44  * on the understanding that TFS is not responsible for the correct
45  * functioning of this software in any circumstances.
46  *
47  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
48  */
49 
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/timeout.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/ioctl.h>
57 #include <sys/mtio.h>
58 #include <sys/buf.h>
59 #include <sys/uio.h>
60 #include <sys/malloc.h>
61 #include <sys/errno.h>
62 #include <sys/device.h>
63 #include <sys/disklabel.h>
64 #include <sys/disk.h>
65 #include <sys/cdio.h>
66 #include <sys/proc.h>
67 #include <sys/conf.h>
68 #include <sys/scsiio.h>
69 #include <sys/vnode.h>
70 
71 #include <scsi/scsi_all.h>
72 #include <scsi/cd.h>
73 #include <scsi/scsi_disk.h>	/* rw_big and start_stop come from there */
74 #include <scsi/scsiconf.h>
75 
76 
77 #include <ufs/ffs/fs.h>		/* for BBSIZE and SBSIZE */
78 
79 #define	CDOUTSTANDING	4
80 
81 #define MAXTRACK	99
82 #define CD_FRAMES	75
83 #define CD_SECS		60
84 
85 struct cd_toc {
86 	struct ioc_toc_header header;
87 	struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */
88 						 /* leadout */
89 };
90 
91 int	cdmatch(struct device *, void *, void *);
92 void	cdattach(struct device *, struct device *, void *);
93 int	cdactivate(struct device *, enum devact);
94 int	cddetach(struct device *, int);
95 
96 void	cdstart(void *);
97 void	cdrestart(void *);
98 void	cdminphys(struct buf *);
99 void	cdgetdisklabel(dev_t, struct cd_softc *, struct disklabel *, int);
100 void	cddone(struct scsi_xfer *);
101 void	cd_kill_buffers(struct cd_softc *);
102 int	cd_setchan(struct cd_softc *, int, int, int, int, int);
103 int	cd_getvol(struct cd_softc *cd, struct ioc_vol *, int);
104 int	cd_setvol(struct cd_softc *, const struct ioc_vol *, int);
105 int	cd_load_unload(struct cd_softc *, int, int);
106 int	cd_set_pa_immed(struct cd_softc *, int);
107 int	cd_play(struct cd_softc *, int, int);
108 int	cd_play_tracks(struct cd_softc *, int, int, int, int);
109 int	cd_play_msf(struct cd_softc *, int, int, int, int, int, int);
110 int	cd_pause(struct cd_softc *, int);
111 int	cd_reset(struct cd_softc *);
112 int	cd_read_subchannel(struct cd_softc *, int, int, int,
113 	    struct cd_sub_channel_info *, int );
114 int	cd_read_toc(struct cd_softc *, int, int, void *, int, int);
115 int	cd_get_parms(struct cd_softc *, int);
116 int	cd_load_toc(struct cd_softc *, struct cd_toc *, int);
117 int	cd_interpret_sense(struct scsi_xfer *);
118 
119 int    dvd_auth(struct cd_softc *, union dvd_authinfo *);
120 int    dvd_read_physical(struct cd_softc *, union dvd_struct *);
121 int    dvd_read_copyright(struct cd_softc *, union dvd_struct *);
122 int    dvd_read_disckey(struct cd_softc *, union dvd_struct *);
123 int    dvd_read_bca(struct cd_softc *, union dvd_struct *);
124 int    dvd_read_manufact(struct cd_softc *, union dvd_struct *);
125 int    dvd_read_struct(struct cd_softc *, union dvd_struct *);
126 
127 void	cd_powerhook(int why, void *arg);
128 
129 #if defined(__macppc__)
130 int	cd_eject(void);
131 #endif
132 
133 struct cfattach cd_ca = {
134 	sizeof(struct cd_softc), cdmatch, cdattach,
135 	cddetach, cdactivate
136 };
137 
138 struct cfdriver cd_cd = {
139 	NULL, "cd", DV_DISK
140 };
141 
142 struct dkdriver cddkdriver = { cdstrategy };
143 
144 struct scsi_device cd_switch = {
145 	cd_interpret_sense,
146 	cdstart,		/* we have a queue, which is started by this */
147 	NULL,			/* we do not have an async handler */
148 	cddone,			/* deal with stats at interrupt time */
149 };
150 
151 const struct scsi_inquiry_pattern cd_patterns[] = {
152 	{T_CDROM, T_REMOV,
153 	 "",         "",                 ""},
154 	{T_WORM, T_REMOV,
155 	 "",         "",                 ""},
156 	{T_DIRECT, T_REMOV,
157 	 "NEC                 CD-ROM DRIVE:260", "", ""},
158 #if 0
159 	{T_CDROM, T_REMOV, /* more luns */
160 	 "PIONEER ", "CD-ROM DRM-600  ", ""},
161 #endif
162 };
163 
164 #define cdlock(softc)   disk_lock(&(softc)->sc_dk)
165 #define cdunlock(softc) disk_unlock(&(softc)->sc_dk)
166 #define cdlookup(unit) (struct cd_softc *)device_lookup(&cd_cd, (unit))
167 
168 int
169 cdmatch(struct device *parent, void *match, void *aux)
170 {
171 	struct scsi_attach_args *sa = aux;
172 	int priority;
173 
174 	scsi_inqmatch(sa->sa_inqbuf, cd_patterns,
175 	    sizeof(cd_patterns)/sizeof(cd_patterns[0]), sizeof(cd_patterns[0]),
176 	    &priority);
177 	return (priority);
178 }
179 
180 /*
181  * The routine called by the low level scsi routine when it discovers
182  * A device suitable for this driver
183  */
184 void
185 cdattach(struct device *parent, struct device *self, void *aux)
186 {
187 	struct scsi_attach_args *sa = aux;
188 	struct scsi_link *sc_link = sa->sa_sc_link;
189 	struct cd_softc *cd = (struct cd_softc *)self;
190 
191 	SC_DEBUG(sc_link, SDEV_DB2, ("cdattach:\n"));
192 
193 	/*
194 	 * Store information needed to contact our base driver
195 	 */
196 	cd->sc_link = sc_link;
197 	sc_link->device = &cd_switch;
198 	sc_link->device_softc = cd;
199 	if (sc_link->openings > CDOUTSTANDING)
200 		sc_link->openings = CDOUTSTANDING;
201 
202 	/*
203 	 * Initialize and attach the disk structure.
204 	 */
205   	cd->sc_dk.dk_driver = &cddkdriver;
206 	cd->sc_dk.dk_name = cd->sc_dev.dv_xname;
207 	disk_attach(&cd->sc_dk);
208 
209 	/*
210 	 * Note if this device is ancient.  This is used in cdminphys().
211 	 */
212 	if (!(sc_link->flags & SDEV_ATAPI) &&
213 	    SCSISPC(sa->sa_inqbuf->version) == 0)
214 		cd->flags |= CDF_ANCIENT;
215 
216 	printf("\n");
217 
218 	timeout_set(&cd->sc_timeout, cdrestart, cd);
219 
220 	if ((cd->sc_cdpwrhook = powerhook_establish(cd_powerhook, cd)) == NULL)
221 		printf("%s: WARNING: unable to establish power hook\n",
222 		    cd->sc_dev.dv_xname);
223 }
224 
225 
226 int
227 cdactivate(struct device *self, enum devact act)
228 {
229 	int rv = 0;
230 
231 	switch (act) {
232 	case DVACT_ACTIVATE:
233 		break;
234 
235 	case DVACT_DEACTIVATE:
236 		/*
237 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
238 		 */
239 		break;
240 	}
241 	return (rv);
242 }
243 
244 
245 int
246 cddetach(struct device *self, int flags)
247 {
248 	struct cd_softc *cd = (struct cd_softc *)self;
249 	int bmaj, cmaj, mn;
250 
251 	cd_kill_buffers(cd);
252 
253 	/* Locate the lowest minor number to be detached. */
254 	mn = DISKMINOR(self->dv_unit, 0);
255 
256 	for (bmaj = 0; bmaj < nblkdev; bmaj++)
257 		if (bdevsw[bmaj].d_open == cdopen)
258 			vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
259 	for (cmaj = 0; cmaj < nchrdev; cmaj++)
260 		if (cdevsw[cmaj].d_open == cdopen)
261 			vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
262 
263 	/* Get rid of the power hook. */
264 	if (cd->sc_cdpwrhook != NULL)
265 		powerhook_disestablish(cd->sc_cdpwrhook);
266 
267 	/* Detach disk. */
268 	disk_detach(&cd->sc_dk);
269 
270 	return (0);
271 }
272 
273 /*
274  * Open the device. Make sure the partition info is as up-to-date as can be.
275  */
276 int
277 cdopen(dev_t dev, int flag, int fmt, struct proc *p)
278 {
279 	struct scsi_link *sc_link;
280 	struct cd_softc *cd;
281 	int error = 0, part, rawopen, unit;
282 
283 	unit = DISKUNIT(dev);
284 	part = DISKPART(dev);
285 
286 	rawopen = (part == RAW_PART) && (fmt == S_IFCHR);
287 
288 	cd = cdlookup(unit);
289 	if (cd == NULL)
290 		return (ENXIO);
291 
292 	sc_link = cd->sc_link;
293 	SC_DEBUG(sc_link, SDEV_DB1,
294 	    ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
295 	    cd_cd.cd_ndevs, part));
296 
297 	if ((error = cdlock(cd)) != 0) {
298 		device_unref(&cd->sc_dev);
299 		return (error);
300 	}
301 
302 	if (cd->sc_dk.dk_openmask != 0) {
303 		/*
304 		 * If any partition is open, but the disk has been invalidated,
305 		 * disallow further opens.
306 		 */
307 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
308 			if (rawopen)
309 				goto out;
310 			error = EIO;
311 			goto bad;
312 		}
313 	} else {
314 		/*
315 		 * Check that it is still responding and ok.  Drive can be in
316 		 * progress of loading media so use increased retries number
317 		 * and don't ignore NOT_READY.
318 		 */
319 
320 		/* Use cd_interpret_sense() now. */
321 		sc_link->flags |= SDEV_OPEN;
322 
323 		error = scsi_test_unit_ready(sc_link, TEST_READY_RETRIES,
324 		    (rawopen ? SCSI_SILENT : 0) | SCSI_IGNORE_ILLEGAL_REQUEST |
325 		    SCSI_IGNORE_MEDIA_CHANGE);
326 
327 		/* Start the cd spinning if necessary. */
328 		if (error == EIO)
329 			error = scsi_start(sc_link, SSS_START,
330 			    SCSI_IGNORE_ILLEGAL_REQUEST |
331 			    SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
332 
333 		if (error) {
334 			if (rawopen) {
335 				error = 0;
336 				goto out;
337 			} else
338 				goto bad;
339 		}
340 
341 		/* Lock the cd in. */
342 		error = scsi_prevent(sc_link, PR_PREVENT,
343 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
344 		    SCSI_SILENT);
345 		if (error)
346 			goto bad;
347 
348 		/* Load the physical device parameters. */
349 		sc_link->flags |= SDEV_MEDIA_LOADED;
350 		if (cd_get_parms(cd, (rawopen ? SCSI_SILENT : 0) |
351 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE)) {
352 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
353 			error = ENXIO;
354 			goto bad;
355 		}
356 		SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded\n"));
357 
358 		/* Fabricate a disk label. */
359 		cdgetdisklabel(dev, cd, cd->sc_dk.dk_label, 0);
360 		SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated\n"));
361 	}
362 
363 	/* Check that the partition exists. */
364 	if (part != RAW_PART && (part >= cd->sc_dk.dk_label->d_npartitions ||
365 	    cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
366 		error = ENXIO;
367 		goto bad;
368 	}
369 
370 out:	/* Insure only one open at a time. */
371 	switch (fmt) {
372 	case S_IFCHR:
373 		cd->sc_dk.dk_copenmask |= (1 << part);
374 		break;
375 	case S_IFBLK:
376 		cd->sc_dk.dk_bopenmask |= (1 << part);
377 		break;
378 	}
379 	cd->sc_dk.dk_openmask = cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
380 	sc_link->flags |= SDEV_OPEN;
381 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
382 
383 	/* It's OK to fall through because dk_openmask is now non-zero. */
384 bad:
385 	if (cd->sc_dk.dk_openmask == 0) {
386 		scsi_prevent(sc_link, PR_ALLOW,
387 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
388 		    SCSI_SILENT);
389 		sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED);
390 	}
391 
392 	cdunlock(cd);
393 	device_unref(&cd->sc_dev);
394 	return (error);
395 }
396 
397 /*
398  * Close the device. Only called if we are the last occurrence of an open
399  * device.
400  */
401 int
402 cdclose(dev_t dev, int flag, int fmt, struct proc *p)
403 {
404 	struct cd_softc *cd;
405 	int part = DISKPART(dev);
406 	int error;
407 
408 	cd = cdlookup(DISKUNIT(dev));
409 	if (cd == NULL)
410 		return ENXIO;
411 
412 	if ((error = cdlock(cd)) != 0) {
413 		device_unref(&cd->sc_dev);
414 		return error;
415 	}
416 
417 	switch (fmt) {
418 	case S_IFCHR:
419 		cd->sc_dk.dk_copenmask &= ~(1 << part);
420 		break;
421 	case S_IFBLK:
422 		cd->sc_dk.dk_bopenmask &= ~(1 << part);
423 		break;
424 	}
425 	cd->sc_dk.dk_openmask = cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
426 
427 	if (cd->sc_dk.dk_openmask == 0) {
428 		/* XXXX Must wait for I/O to complete! */
429 
430 		scsi_prevent(cd->sc_link, PR_ALLOW,
431 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
432 		    SCSI_SILENT);
433 		cd->sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED);
434 
435 		if (cd->sc_link->flags & SDEV_EJECTING) {
436 			scsi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0);
437 
438 			cd->sc_link->flags &= ~SDEV_EJECTING;
439 		}
440 
441 		timeout_del(&cd->sc_timeout);
442 	}
443 
444 	cdunlock(cd);
445 
446 	device_unref(&cd->sc_dev);
447 	return 0;
448 }
449 
450 /*
451  * Actually translate the requested transfer into one the physical driver can
452  * understand.  The transfer is described by a buf and will include only one
453  * physical transfer.
454  */
455 void
456 cdstrategy(struct buf *bp)
457 {
458 	struct cd_softc *cd;
459 	int s;
460 
461 	if ((cd = cdlookup(DISKUNIT(bp->b_dev))) == NULL) {
462 		bp->b_error = ENXIO;
463 		goto bad;
464 	}
465 
466 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdstrategy: %ld bytes @ blk %d\n",
467 	    bp->b_bcount, bp->b_blkno));
468 	/*
469 	 * If the device has been made invalid, error out
470 	 * maybe the media changed, or no media loaded
471 	 */
472 	if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
473 		bp->b_error = EIO;
474 		goto bad;
475 	}
476 	/*
477 	 * The transfer must be a whole number of blocks.
478 	 */
479 	if ((bp->b_bcount % cd->sc_dk.dk_label->d_secsize) != 0) {
480 		bp->b_error = EINVAL;
481 		goto bad;
482 	}
483 	/*
484 	 * If it's a null transfer, return immediately
485 	 */
486 	if (bp->b_bcount == 0)
487 		goto done;
488 
489 	/*
490 	 * Do bounds checking, adjust transfer. if error, process.
491 	 * If end of partition, just return.
492 	 */
493 	if (bounds_check_with_label(bp, cd->sc_dk.dk_label,
494 	    (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0)
495 		goto done;
496 
497 	s = splbio();
498 
499 	/*
500 	 * Place it in the queue of disk activities for this disk
501 	 */
502 	disksort(&cd->buf_queue, bp);
503 
504 	/*
505 	 * Tell the device to get going on the transfer if it's
506 	 * not doing anything, otherwise just wait for completion
507 	 */
508 	cdstart(cd);
509 
510 	device_unref(&cd->sc_dev);
511 	splx(s);
512 	return;
513 
514 bad:
515 	bp->b_flags |= B_ERROR;
516 done:
517 	/*
518 	 * Correctly set the buf to indicate a completed xfer
519 	 */
520 	bp->b_resid = bp->b_bcount;
521 	s = splbio();
522 	biodone(bp);
523 	splx(s);
524 	if (cd != NULL)
525 		device_unref(&cd->sc_dev);
526 }
527 
528 /*
529  * cdstart looks to see if there is a buf waiting for the device
530  * and that the device is not already busy. If both are true,
531  * It deques the buf and creates a scsi command to perform the
532  * transfer in the buf. The transfer request will call scsi_done
533  * on completion, which will in turn call this routine again
534  * so that the next queued transfer is performed.
535  * The bufs are queued by the strategy routine (cdstrategy)
536  *
537  * This routine is also called after other non-queued requests
538  * have been made of the scsi driver, to ensure that the queue
539  * continues to be drained.
540  *
541  * must be called at the correct (highish) spl level
542  * cdstart() is called at splbio from cdstrategy, cdrestart and scsi_done
543  */
544 void
545 cdstart(void *v)
546 {
547 	struct cd_softc *cd = v;
548 	struct scsi_link *sc_link = cd->sc_link;
549 	struct buf *bp = 0;
550 	struct buf *dp;
551 	struct scsi_rw_big cmd_big;
552 	struct scsi_rw cmd_small;
553 	struct scsi_generic *cmdp;
554 	int blkno, nblks, cmdlen, error;
555 	struct partition *p;
556 
557 	splassert(IPL_BIO);
558 
559 	SC_DEBUG(sc_link, SDEV_DB2, ("cdstart\n"));
560 	/*
561 	 * Check if the device has room for another command
562 	 */
563 	while (sc_link->openings > 0) {
564 		/*
565 		 * there is excess capacity, but a special waits
566 		 * It'll need the adapter as soon as we clear out of the
567 		 * way and let it run (user level wait).
568 		 */
569 		if (sc_link->flags & SDEV_WAITING) {
570 			sc_link->flags &= ~SDEV_WAITING;
571 			wakeup((caddr_t)sc_link);
572 			return;
573 		}
574 
575 		/*
576 		 * See if there is a buf with work for us to do..
577 		 */
578 		dp = &cd->buf_queue;
579 		if ((bp = dp->b_actf) == NULL)	/* yes, an assign */
580 			return;
581 		dp->b_actf = bp->b_actf;
582 
583 		/*
584 		 * If the device has become invalid, abort all the
585 		 * reads and writes until all files have been closed and
586 		 * re-opened
587 		 */
588 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
589 			bp->b_error = EIO;
590 			bp->b_flags |= B_ERROR;
591 			bp->b_resid = bp->b_bcount;
592 			biodone(bp);
593 			continue;
594 		}
595 
596 		/*
597 		 * We have a buf, now we should make a command
598 		 *
599 		 * First, translate the block to absolute and put it in terms
600 		 * of the logical blocksize of the device.
601 		 */
602 		blkno =
603 		    bp->b_blkno / (cd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
604 		p = &cd->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
605 		blkno += DL_GETPOFFSET(p);
606 		nblks = howmany(bp->b_bcount, cd->sc_dk.dk_label->d_secsize);
607 
608 		/*
609 		 *  Fill out the scsi command.  If the transfer will
610 		 *  fit in a "small" cdb, use it.
611 		 */
612 		if (!(sc_link->flags & SDEV_ATAPI) &&
613 		    !(sc_link->quirks & SDEV_ONLYBIG) &&
614 		    ((blkno & 0x1fffff) == blkno) &&
615 		    ((nblks & 0xff) == nblks)) {
616 			/*
617 			 * We can fit in a small cdb.
618 			 */
619 			bzero(&cmd_small, sizeof(cmd_small));
620 			cmd_small.opcode = (bp->b_flags & B_READ) ?
621 			    READ_COMMAND : WRITE_COMMAND;
622 			_lto3b(blkno, cmd_small.addr);
623 			cmd_small.length = nblks & 0xff;
624 			cmdlen = sizeof(cmd_small);
625 			cmdp = (struct scsi_generic *)&cmd_small;
626 		} else {
627 			/*
628 			 * Need a large cdb.
629 			 */
630 			bzero(&cmd_big, sizeof(cmd_big));
631 			cmd_big.opcode = (bp->b_flags & B_READ) ?
632 			    READ_BIG : WRITE_BIG;
633 			_lto4b(blkno, cmd_big.addr);
634 			_lto2b(nblks, cmd_big.length);
635 			cmdlen = sizeof(cmd_big);
636 			cmdp = (struct scsi_generic *)&cmd_big;
637 		}
638 
639 		/* Instrumentation. */
640 		disk_busy(&cd->sc_dk);
641 
642 		/*
643 		 * Call the routine that chats with the adapter.
644 		 * Note: we cannot sleep as we may be an interrupt
645 		 */
646 		error = scsi_scsi_cmd(sc_link, cmdp, cmdlen,
647 		    (u_char *) bp->b_data, bp->b_bcount, SCSI_RETRIES, 30000,
648 		    bp, SCSI_NOSLEEP | ((bp->b_flags & B_READ) ? SCSI_DATA_IN :
649 		    SCSI_DATA_OUT));
650 		switch (error) {
651 		case 0:
652 			timeout_del(&cd->sc_timeout);
653 			break;
654 		case EAGAIN:
655 			/*
656 			 * The device can't start another i/o. Try again later.
657 			 */
658 			dp->b_actf = bp;
659 			disk_unbusy(&cd->sc_dk, 0, 0);
660 			timeout_add(&cd->sc_timeout, 1);
661 			return;
662 		default:
663 			disk_unbusy(&cd->sc_dk, 0, 0);
664 			printf("%s: not queued, error %d\n",
665 			    cd->sc_dev.dv_xname, error);
666 			break;
667 		}
668 	}
669 }
670 
671 void
672 cdrestart(void *v)
673 {
674 	int s;
675 
676 	s = splbio();
677 	cdstart(v);
678 	splx(s);
679 }
680 
681 void
682 cddone(struct scsi_xfer *xs)
683 {
684 	struct cd_softc *cd = xs->sc_link->device_softc;
685 
686 	if (xs->bp != NULL)
687 		disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid,
688 		    (xs->bp->b_flags & B_READ));
689 }
690 
691 void
692 cdminphys(struct buf *bp)
693 {
694 	struct cd_softc *cd;
695 	long max;
696 
697 	cd = cdlookup(DISKUNIT(bp->b_dev));
698 	if (cd == NULL)
699 		return;
700 
701 	/*
702 	 * If the device is ancient, we want to make sure that
703 	 * the transfer fits into a 6-byte cdb.
704 	 *
705 	 * XXX Note that the SCSI-I spec says that 256-block transfers
706 	 * are allowed in a 6-byte read/write, and are specified
707 	 * by setting the "length" to 0.  However, we're conservative
708 	 * here, allowing only 255-block transfers in case an
709 	 * ancient device gets confused by length == 0.  A length of 0
710 	 * in a 10-byte read/write actually means 0 blocks.
711 	 */
712 	if (cd->flags & CDF_ANCIENT) {
713 		max = cd->sc_dk.dk_label->d_secsize * 0xff;
714 
715 		if (bp->b_bcount > max)
716 			bp->b_bcount = max;
717 	}
718 
719 	(*cd->sc_link->adapter->scsi_minphys)(bp, cd->sc_link);
720 
721 	device_unref(&cd->sc_dev);
722 }
723 
724 int
725 cdread(dev_t dev, struct uio *uio, int ioflag)
726 {
727 
728 	return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio));
729 }
730 
731 int
732 cdwrite(dev_t dev, struct uio *uio, int ioflag)
733 {
734 
735 	return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio));
736 }
737 
738 /*
739  * Perform special action on behalf of the user.
740  * Knows about the internals of this device
741  */
742 int
743 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
744 {
745 	struct cd_softc *cd;
746 	struct disklabel *lp;
747 	int part = DISKPART(dev);
748 	int error = 0;
749 
750 	cd = cdlookup(DISKUNIT(dev));
751 	if (cd == NULL)
752 		return ENXIO;
753 
754 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdioctl 0x%lx\n", cmd));
755 
756 	/*
757 	 * If the device is not valid.. abandon ship
758 	 */
759 	if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
760 		switch (cmd) {
761 		case DIOCWLABEL:
762 		case DIOCLOCK:
763 		case DIOCEJECT:
764 		case SCIOCIDENTIFY:
765 		case SCIOCCOMMAND:
766 		case SCIOCDEBUG:
767 		case CDIOCLOADUNLOAD:
768 		case SCIOCRESET:
769 		case CDIOCGETVOL:
770 		case CDIOCSETVOL:
771 		case CDIOCSETMONO:
772 		case CDIOCSETSTEREO:
773 		case CDIOCSETMUTE:
774 		case CDIOCSETLEFT:
775 		case CDIOCSETRIGHT:
776 		case CDIOCCLOSE:
777 		case CDIOCEJECT:
778 		case CDIOCALLOW:
779 		case CDIOCPREVENT:
780 		case CDIOCSETDEBUG:
781 		case CDIOCCLRDEBUG:
782 		case CDIOCRESET:
783 		case DVD_AUTH:
784 		case DVD_READ_STRUCT:
785 		case MTIOCTOP:
786 			if (part == RAW_PART)
787 				break;
788 		/* FALLTHROUGH */
789 		default:
790 			if ((cd->sc_link->flags & SDEV_OPEN) == 0)
791 				error = ENODEV;
792 			else
793 				error = EIO;
794 			goto exit;
795 		}
796 	}
797 
798 	switch (cmd) {
799 	case DIOCRLDINFO:
800 		lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
801 		cdgetdisklabel(dev, cd, lp, 0);
802 		bcopy(lp, cd->sc_dk.dk_label, sizeof(*lp));
803 		free(lp, M_TEMP);
804 		break;
805 	case DIOCGDINFO:
806 	case DIOCGPDINFO:
807 		*(struct disklabel *)addr = *(cd->sc_dk.dk_label);
808 		break;
809 
810 	case DIOCGPART:
811 		((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label;
812 		((struct partinfo *)addr)->part =
813 		    &cd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
814 		break;
815 
816 	case DIOCWDINFO:
817 	case DIOCSDINFO:
818 		if ((flag & FWRITE) == 0) {
819 			error = EBADF;
820 			break;
821 		}
822 
823 		if ((error = cdlock(cd)) != 0)
824 			break;
825 
826 		cd->flags |= CDF_LABELLING;
827 
828 		error = setdisklabel(cd->sc_dk.dk_label,
829 		    (struct disklabel *)addr, /*cd->sc_dk.dk_openmask : */0);
830 		if (error == 0) {
831 		}
832 
833 		cd->flags &= ~CDF_LABELLING;
834 		cdunlock(cd);
835 		break;
836 
837 	case DIOCWLABEL:
838 		error = EBADF;
839 		break;
840 
841 	case CDIOCPLAYTRACKS: {
842 		struct ioc_play_track *args = (struct ioc_play_track *)addr;
843 
844 		if ((error = cd_set_pa_immed(cd, 0)) != 0)
845 			break;
846 		error = cd_play_tracks(cd, args->start_track,
847 		    args->start_index, args->end_track, args->end_index);
848 		break;
849 	}
850 	case CDIOCPLAYMSF: {
851 		struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
852 
853 		if ((error = cd_set_pa_immed(cd, 0)) != 0)
854 			break;
855 		error = cd_play_msf(cd, args->start_m, args->start_s,
856 		    args->start_f, args->end_m, args->end_s, args->end_f);
857 		break;
858 	}
859 	case CDIOCPLAYBLOCKS: {
860 		struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
861 
862 		if ((error = cd_set_pa_immed(cd, 0)) != 0)
863 			break;
864 		error = cd_play(cd, args->blk, args->len);
865 		break;
866 	}
867 	case CDIOCREADSUBCHANNEL: {
868 		struct ioc_read_subchannel *args
869 		= (struct ioc_read_subchannel *)addr;
870 		struct cd_sub_channel_info data;
871 		int len = args->data_len;
872 		if (len > sizeof(data) ||
873 		    len < sizeof(struct cd_sub_channel_header)) {
874 			error = EINVAL;
875 			break;
876 		}
877 		error = cd_read_subchannel(cd, args->address_format,
878 					   args->data_format, args->track,
879 					   &data, len);
880 		if (error)
881 			break;
882 		len = min(len, _2btol(data.header.data_len) +
883 		    sizeof(struct cd_sub_channel_header));
884 		error = copyout(&data, args->data, len);
885 		break;
886 	}
887 	case CDIOREADTOCHEADER: {
888 		struct ioc_toc_header th;
889 
890 		if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 0)) != 0)
891 			break;
892 		if (cd->sc_link->quirks & ADEV_LITTLETOC)
893 			th.len = letoh16(th.len);
894 		else
895 			th.len = betoh16(th.len);
896 		bcopy(&th, addr, sizeof(th));
897 		break;
898 	}
899 	case CDIOREADTOCENTRYS:  {
900 		struct cd_toc *toc;
901 		struct ioc_read_toc_entry *te =
902 		    (struct ioc_read_toc_entry *)addr;
903 		struct ioc_toc_header *th;
904 		struct cd_toc_entry *cte;
905 		int len = te->data_len;
906 		int ntracks;
907 
908 		toc = malloc(sizeof(*toc), M_TEMP, M_WAITOK | M_ZERO);
909 
910 		th = &toc->header;
911 
912 		if (len > sizeof(toc->entries) ||
913 		    len < sizeof(struct cd_toc_entry)) {
914 			free(toc, M_TEMP);
915 			error = EINVAL;
916 			break;
917 		}
918 		error = cd_read_toc(cd, te->address_format, te->starting_track,
919 		    toc, len + sizeof(struct ioc_toc_header), 0);
920 		if (error) {
921 			free(toc, M_TEMP);
922 			break;
923 		}
924 		if (te->address_format == CD_LBA_FORMAT)
925 			for (ntracks =
926 			    th->ending_track - th->starting_track + 1;
927 			    ntracks >= 0; ntracks--) {
928 				cte = &toc->entries[ntracks];
929 				cte->addr_type = CD_LBA_FORMAT;
930 				if (cd->sc_link->quirks & ADEV_LITTLETOC) {
931 #if BYTE_ORDER == BIG_ENDIAN
932 					swap16_multi((u_int16_t *)&cte->addr,
933 					    sizeof(cte->addr) / 2);
934 #endif
935 				} else
936 					cte->addr.lba = betoh32(cte->addr.lba);
937 			}
938 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
939 			th->len = letoh16(th->len);
940 		} else
941 			th->len = betoh16(th->len);
942 		len = min(len, th->len - (sizeof(th->starting_track) +
943 		    sizeof(th->ending_track)));
944 
945 		error = copyout(toc->entries, te->data, len);
946 		free(toc, M_TEMP);
947 		break;
948 	}
949 	case CDIOREADMSADDR: {
950 		struct cd_toc *toc;
951 		int sessno = *(int *)addr;
952 		struct cd_toc_entry *cte;
953 
954 		if (sessno != 0) {
955 			error = EINVAL;
956 			break;
957 		}
958 
959 		toc = malloc(sizeof(*toc), M_TEMP, M_WAITOK | M_ZERO);
960 
961 		error = cd_read_toc(cd, 0, 0, toc,
962 		  sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
963 		  0x40 /* control word for "get MS info" */);
964 
965 		if (error) {
966 			free(toc, M_TEMP);
967 			break;
968 		}
969 
970 		cte = &toc->entries[0];
971 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
972 #if BYTE_ORDER == BIG_ENDIAN
973 			swap16_multi((u_int16_t *)&cte->addr,
974 			    sizeof(cte->addr) / 2);
975 #endif
976 		} else
977 			cte->addr.lba = betoh32(cte->addr.lba);
978 		if (cd->sc_link->quirks & ADEV_LITTLETOC)
979 			toc->header.len = letoh16(toc->header.len);
980 		else
981 			toc->header.len = betoh16(toc->header.len);
982 
983 		*(int *)addr = (toc->header.len >= 10 && cte->track > 1) ?
984 			cte->addr.lba : 0;
985 		free(toc, M_TEMP);
986 		break;
987 	}
988 	case CDIOCSETPATCH: {
989 		struct ioc_patch *arg = (struct ioc_patch *)addr;
990 
991 		error = cd_setchan(cd, arg->patch[0], arg->patch[1],
992 		    arg->patch[2], arg->patch[3], 0);
993 		break;
994 	}
995 	case CDIOCGETVOL: {
996 		struct ioc_vol *arg = (struct ioc_vol *)addr;
997 
998 		error = cd_getvol(cd, arg, 0);
999 		break;
1000 	}
1001 	case CDIOCSETVOL: {
1002 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1003 
1004 		error = cd_setvol(cd, arg, 0);
1005 		break;
1006 	}
1007 
1008 	case CDIOCSETMONO:
1009 		error = cd_setchan(cd, BOTH_CHANNEL, BOTH_CHANNEL, MUTE_CHANNEL,
1010 		    MUTE_CHANNEL, 0);
1011 		break;
1012 
1013 	case CDIOCSETSTEREO:
1014 		error = cd_setchan(cd, LEFT_CHANNEL, RIGHT_CHANNEL,
1015 		    MUTE_CHANNEL, MUTE_CHANNEL, 0);
1016 		break;
1017 
1018 	case CDIOCSETMUTE:
1019 		error = cd_setchan(cd, MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL,
1020 		    MUTE_CHANNEL, 0);
1021 		break;
1022 
1023 	case CDIOCSETLEFT:
1024 		error = cd_setchan(cd, LEFT_CHANNEL, LEFT_CHANNEL, MUTE_CHANNEL,
1025 		    MUTE_CHANNEL, 0);
1026 		break;
1027 
1028 	case CDIOCSETRIGHT:
1029 		error = cd_setchan(cd, RIGHT_CHANNEL, RIGHT_CHANNEL,
1030 		    MUTE_CHANNEL, MUTE_CHANNEL, 0);
1031 		break;
1032 
1033 	case CDIOCRESUME:
1034 		error = cd_pause(cd, 1);
1035 		break;
1036 
1037 	case CDIOCPAUSE:
1038 		error = cd_pause(cd, 0);
1039 		break;
1040 	case CDIOCSTART:
1041 		error = scsi_start(cd->sc_link, SSS_START, 0);
1042 		break;
1043 
1044 	case CDIOCSTOP:
1045 		error = scsi_start(cd->sc_link, SSS_STOP, 0);
1046 		break;
1047 
1048 	close_tray:
1049 	case CDIOCCLOSE:
1050 		error = scsi_start(cd->sc_link, SSS_START|SSS_LOEJ,
1051 		    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
1052 		break;
1053 
1054 	case MTIOCTOP:
1055 		if (((struct mtop *)addr)->mt_op == MTRETEN)
1056 			goto close_tray;
1057 		if (((struct mtop *)addr)->mt_op != MTOFFL) {
1058 			error = EIO;
1059 			break;
1060 		}
1061 		/* FALLTHROUGH */
1062 	case CDIOCEJECT: /* FALLTHROUGH */
1063 	case DIOCEJECT:
1064 		cd->sc_link->flags |= SDEV_EJECTING;
1065 		break;
1066 	case CDIOCALLOW:
1067 		error = scsi_prevent(cd->sc_link, PR_ALLOW, 0);
1068 		break;
1069 	case CDIOCPREVENT:
1070 		error = scsi_prevent(cd->sc_link, PR_PREVENT, 0);
1071 		break;
1072 	case DIOCLOCK:
1073 		error = scsi_prevent(cd->sc_link,
1074 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0);
1075 		break;
1076 	case CDIOCSETDEBUG:
1077 		cd->sc_link->flags |= (SDEV_DB1 | SDEV_DB2);
1078 		break;
1079 	case CDIOCCLRDEBUG:
1080 		cd->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2);
1081 		break;
1082 	case CDIOCRESET:
1083 	case SCIOCRESET:
1084 		error = cd_reset(cd);
1085 		break;
1086 	case CDIOCLOADUNLOAD: {
1087 		struct ioc_load_unload *args = (struct ioc_load_unload *)addr;
1088 
1089 		error = cd_load_unload(cd, args->options, args->slot);
1090 		break;
1091 	}
1092 
1093 	case DVD_AUTH:
1094 		error = dvd_auth(cd, (union dvd_authinfo *)addr);
1095 		break;
1096 	case DVD_READ_STRUCT:
1097 		error = dvd_read_struct(cd, (union dvd_struct *)addr);
1098 		break;
1099 	default:
1100 		if (DISKPART(dev) != RAW_PART) {
1101 			error = ENOTTY;
1102 			break;
1103 		}
1104 		error = scsi_do_ioctl(cd->sc_link, dev, cmd, addr, flag, p);
1105 		break;
1106 	}
1107 
1108  exit:
1109 
1110 	device_unref(&cd->sc_dev);
1111 	return (error);
1112 }
1113 
1114 /*
1115  * Load the label information on the named device
1116  * Actually fabricate a disklabel
1117  *
1118  * EVENTUALLY take information about different
1119  * data tracks from the TOC and put it in the disklabel
1120  */
1121 void
1122 cdgetdisklabel(dev_t dev, struct cd_softc *cd, struct disklabel *lp,
1123     int spoofonly)
1124 {
1125 	struct cd_toc *toc;
1126 	char *errstring;
1127 	int tocidx, n, audioonly = 1;
1128 
1129 	bzero(lp, sizeof(struct disklabel));
1130 
1131 	toc = malloc(sizeof(*toc), M_TEMP, M_WAITOK | M_ZERO);
1132 
1133 	lp->d_secsize = cd->params.blksize;
1134 	lp->d_ntracks = 1;
1135 	lp->d_nsectors = 100;
1136 	lp->d_secpercyl = 100;
1137 	lp->d_ncylinders = (cd->params.disksize / 100) + 1;
1138 
1139 	if (cd->sc_link->flags & SDEV_ATAPI) {
1140 		strncpy(lp->d_typename, "ATAPI CD-ROM", sizeof(lp->d_typename));
1141 		lp->d_type = DTYPE_ATAPI;
1142 	} else {
1143 		strncpy(lp->d_typename, "SCSI CD-ROM", sizeof(lp->d_typename));
1144 		lp->d_type = DTYPE_SCSI;
1145 	}
1146 
1147 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1148 	DL_SETDSIZE(lp, cd->params.disksize);
1149 	lp->d_rpm = 300;
1150 	lp->d_interleave = 1;
1151 	lp->d_version = 1;
1152 
1153 	/* XXX - these values for BBSIZE and SBSIZE assume ffs */
1154 	lp->d_bbsize = BBSIZE;
1155 	lp->d_sbsize = SBSIZE;
1156 
1157 	lp->d_magic = DISKMAGIC;
1158 	lp->d_magic2 = DISKMAGIC;
1159 	lp->d_checksum = dkcksum(lp);
1160 
1161 	if (cd_load_toc(cd, toc, CD_LBA_FORMAT)) {
1162 		audioonly = 0; /* No valid TOC found == not an audio CD. */
1163 		goto done;
1164 	}
1165 
1166 	n = toc->header.ending_track - toc->header.starting_track + 1;
1167 	for (tocidx = 0; tocidx < n; tocidx++)
1168 		if (toc->entries[tocidx].control & 4) {
1169 			audioonly = 0; /* Found a non-audio track. */
1170 			goto done;
1171 		}
1172 
1173 done:
1174 	free(toc, M_TEMP);
1175 
1176 	if (!audioonly) {
1177 		errstring = readdisklabel(DISKLABELDEV(dev), cdstrategy, lp,
1178 		    spoofonly);
1179 		/*if (errstring)
1180 			printf("%s: %s\n", cd->sc_dev.dv_xname, errstring);*/
1181 	}
1182 }
1183 
1184 int
1185 cd_setchan(struct cd_softc *cd, int p0, int p1, int p2, int p3, int flags)
1186 {
1187 	union scsi_mode_sense_buf *data;
1188 	struct cd_audio_page *audio = NULL;
1189 	int error, big;
1190 
1191 	data = malloc(sizeof(*data), M_TEMP, M_NOWAIT);
1192 	if (data == NULL)
1193 		return (ENOMEM);
1194 
1195 	error = scsi_do_mode_sense(cd->sc_link, AUDIO_PAGE, data,
1196 	    (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big);
1197 	if (error == 0 && audio == NULL)
1198 		error = EIO;
1199 
1200 	if (error == 0) {
1201 		audio->port[LEFT_PORT].channels = p0;
1202 		audio->port[RIGHT_PORT].channels = p1;
1203 		audio->port[2].channels = p2;
1204 		audio->port[3].channels = p3;
1205 		if (big)
1206 			error = scsi_mode_select_big(cd->sc_link, SMS_PF,
1207 			    &data->hdr_big, flags, 20000);
1208 		else
1209 			error = scsi_mode_select(cd->sc_link, SMS_PF,
1210 			    &data->hdr, flags, 20000);
1211 	}
1212 
1213 	free(data, M_TEMP);
1214 	return (error);
1215 }
1216 
1217 int
1218 cd_getvol(struct cd_softc *cd, struct ioc_vol *arg, int flags)
1219 {
1220 	union scsi_mode_sense_buf *data;
1221 	struct cd_audio_page *audio = NULL;
1222 	int error;
1223 
1224 	data = malloc(sizeof(*data), M_TEMP, M_NOWAIT);
1225 	if (data == NULL)
1226 		return (ENOMEM);
1227 
1228 	error = scsi_do_mode_sense(cd->sc_link, AUDIO_PAGE, data,
1229 	    (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, NULL);
1230 	if (error == 0 && audio == NULL)
1231 		error = EIO;
1232 
1233 	if (error == 0) {
1234 		arg->vol[0] = audio->port[0].volume;
1235 		arg->vol[1] = audio->port[1].volume;
1236 		arg->vol[2] = audio->port[2].volume;
1237 		arg->vol[3] = audio->port[3].volume;
1238 	}
1239 
1240 	free(data, M_TEMP);
1241 	return (0);
1242 }
1243 
1244 int
1245 cd_setvol(struct cd_softc *cd, const struct ioc_vol *arg, int flags)
1246 {
1247 	union scsi_mode_sense_buf *data;
1248 	struct cd_audio_page *audio = NULL;
1249 	u_int8_t mask_volume[4];
1250 	int error, big;
1251 
1252 	data = malloc(sizeof(*data), M_TEMP, M_NOWAIT);
1253 	if (data == NULL)
1254 		return (ENOMEM);
1255 
1256 	error = scsi_do_mode_sense(cd->sc_link,
1257 	    AUDIO_PAGE | SMS_PAGE_CTRL_CHANGEABLE, data, (void **)&audio, NULL,
1258 	    NULL, NULL, sizeof(*audio), flags, NULL);
1259 	if (error == 0 && audio == NULL)
1260 		error = EIO;
1261 	if (error != 0) {
1262 		free(data, M_TEMP);
1263 		return (error);
1264 	}
1265 
1266 	mask_volume[0] = audio->port[0].volume;
1267 	mask_volume[1] = audio->port[1].volume;
1268 	mask_volume[2] = audio->port[2].volume;
1269 	mask_volume[3] = audio->port[3].volume;
1270 
1271 	error = scsi_do_mode_sense(cd->sc_link, AUDIO_PAGE, data,
1272 	    (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big);
1273 	if (error == 0 && audio == NULL)
1274 		error = EIO;
1275 	if (error != 0) {
1276 		free(data, M_TEMP);
1277 		return (error);
1278 	}
1279 
1280 	audio->port[0].volume = arg->vol[0] & mask_volume[0];
1281 	audio->port[1].volume = arg->vol[1] & mask_volume[1];
1282 	audio->port[2].volume = arg->vol[2] & mask_volume[2];
1283 	audio->port[3].volume = arg->vol[3] & mask_volume[3];
1284 
1285 	if (big)
1286 		error = scsi_mode_select_big(cd->sc_link, SMS_PF,
1287 		    &data->hdr_big, flags, 20000);
1288 	else
1289 		error = scsi_mode_select(cd->sc_link, SMS_PF,
1290 		    &data->hdr, flags, 20000);
1291 
1292 	free(data, M_TEMP);
1293 	return (error);
1294 }
1295 
1296 int
1297 cd_load_unload(struct cd_softc *cd, int options, int slot)
1298 {
1299 	struct scsi_load_unload cmd;
1300 
1301 	bzero(&cmd, sizeof(cmd));
1302 	cmd.opcode = LOAD_UNLOAD;
1303 	cmd.options = options;    /* ioctl uses ATAPI values */
1304 	cmd.slot = slot;
1305 
1306 	return (scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&cmd,
1307 	    sizeof(cmd), 0, 0, SCSI_RETRIES, 200000, NULL, 0));
1308 }
1309 
1310 int
1311 cd_set_pa_immed(struct cd_softc *cd, int flags)
1312 {
1313 	union scsi_mode_sense_buf *data;
1314 	struct cd_audio_page *audio = NULL;
1315 	int error, oflags, big;
1316 
1317 	if (cd->sc_link->flags & SDEV_ATAPI)
1318 		/* XXX Noop? */
1319 		return (0);
1320 
1321 	data = malloc(sizeof(*data), M_TEMP, M_NOWAIT);
1322 	if (data == NULL)
1323 		return (ENOMEM);
1324 
1325 	error = scsi_do_mode_sense(cd->sc_link, AUDIO_PAGE, data,
1326 	    (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big);
1327 	if (error == 0 && audio == NULL)
1328 		error = EIO;
1329 
1330 	if (error == 0) {
1331 		oflags = audio->flags;
1332 		audio->flags &= ~CD_PA_SOTC;
1333 		audio->flags |= CD_PA_IMMED;
1334 		if (audio->flags != oflags) {
1335 			if (big)
1336 				error = scsi_mode_select_big(cd->sc_link,
1337 				    SMS_PF, &data->hdr_big, flags,
1338 				    20000);
1339 			else
1340 				error = scsi_mode_select(cd->sc_link, SMS_PF,
1341 				    &data->hdr, flags, 20000);
1342 		}
1343 	}
1344 
1345 	free(data, M_TEMP);
1346 	return (error);
1347 }
1348 
1349 /*
1350  * Get scsi driver to send a "start playing" command
1351  */
1352 int
1353 cd_play(struct cd_softc *cd, int blkno, int nblks)
1354 {
1355 	struct scsi_play scsi_cmd;
1356 
1357 	bzero(&scsi_cmd, sizeof(scsi_cmd));
1358 	scsi_cmd.opcode = PLAY;
1359 	_lto4b(blkno, scsi_cmd.blk_addr);
1360 	_lto2b(nblks, scsi_cmd.xfer_len);
1361 	return (scsi_scsi_cmd(cd->sc_link,
1362 	    (struct scsi_generic *)&scsi_cmd, sizeof(scsi_cmd),
1363 	    0, 0, SCSI_RETRIES, 200000, NULL, 0));
1364 }
1365 
1366 /*
1367  * Get scsi driver to send a "start playing" command
1368  */
1369 int
1370 cd_play_tracks(struct cd_softc *cd, int strack, int sindex, int etrack,
1371     int eindex)
1372 {
1373 	struct cd_toc *toc;
1374 	u_char endf, ends, endm;
1375 	int error;
1376 
1377 	if (!etrack)
1378 		return (EIO);
1379 	if (strack > etrack)
1380 		return (EINVAL);
1381 
1382 	toc = malloc(sizeof(*toc), M_TEMP, M_WAITOK | M_ZERO);
1383 
1384 	if ((error = cd_load_toc(cd, toc, CD_MSF_FORMAT)) != 0)
1385 		goto done;
1386 
1387 	if (++etrack > (toc->header.ending_track+1))
1388 		etrack = toc->header.ending_track+1;
1389 
1390 	strack -= toc->header.starting_track;
1391 	etrack -= toc->header.starting_track;
1392 	if (strack < 0) {
1393 		error = EINVAL;
1394 		goto done;
1395 	}
1396 
1397 	/*
1398 	 * The track ends one frame before the next begins.  The last track
1399 	 * is taken care of by the leadoff track.
1400 	 */
1401 	endm = toc->entries[etrack].addr.msf.minute;
1402 	ends = toc->entries[etrack].addr.msf.second;
1403 	endf = toc->entries[etrack].addr.msf.frame;
1404 	if (endf-- == 0) {
1405 		endf = CD_FRAMES - 1;
1406 		if (ends-- == 0) {
1407 			ends = CD_SECS - 1;
1408 			if (endm-- == 0) {
1409 				error = EINVAL;
1410 				goto done;
1411 			}
1412 		}
1413 	}
1414 
1415 	error = cd_play_msf(cd, toc->entries[strack].addr.msf.minute,
1416 	    toc->entries[strack].addr.msf.second,
1417 	    toc->entries[strack].addr.msf.frame,
1418 	    endm, ends, endf);
1419 
1420 done:
1421 	free(toc, M_TEMP);
1422 	return (error);
1423 }
1424 
1425 /*
1426  * Get scsi driver to send a "play msf" command
1427  */
1428 int
1429 cd_play_msf(struct cd_softc *cd, int startm, int starts, int startf, int endm,
1430     int ends, int endf)
1431 {
1432 	struct scsi_play_msf scsi_cmd;
1433 
1434 	bzero(&scsi_cmd, sizeof(scsi_cmd));
1435 	scsi_cmd.opcode = PLAY_MSF;
1436 	scsi_cmd.start_m = startm;
1437 	scsi_cmd.start_s = starts;
1438 	scsi_cmd.start_f = startf;
1439 	scsi_cmd.end_m = endm;
1440 	scsi_cmd.end_s = ends;
1441 	scsi_cmd.end_f = endf;
1442 	return (scsi_scsi_cmd(cd->sc_link,
1443 	    (struct scsi_generic *)&scsi_cmd, sizeof(scsi_cmd),
1444 	    0, 0, SCSI_RETRIES, 20000, NULL, 0));
1445 }
1446 
1447 /*
1448  * Get scsi driver to send a "start up" command
1449  */
1450 int
1451 cd_pause(struct cd_softc *cd, int go)
1452 {
1453 	struct scsi_pause scsi_cmd;
1454 
1455 	bzero(&scsi_cmd, sizeof(scsi_cmd));
1456 	scsi_cmd.opcode = PAUSE;
1457 	scsi_cmd.resume = go;
1458 	return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd,
1459 	    sizeof(scsi_cmd), 0, 0, SCSI_RETRIES, 2000, NULL, 0);
1460 }
1461 
1462 /*
1463  * Get scsi driver to send a "RESET" command
1464  */
1465 int
1466 cd_reset(struct cd_softc *cd)
1467 {
1468 	return scsi_scsi_cmd(cd->sc_link, 0, 0, 0, 0, SCSI_RETRIES, 2000, NULL,
1469 	    SCSI_RESET);
1470 }
1471 
1472 /*
1473  * Read subchannel
1474  */
1475 int
1476 cd_read_subchannel(struct cd_softc *cd, int mode, int format, int track,
1477     struct cd_sub_channel_info *data, int len)
1478 {
1479 	struct scsi_read_subchannel scsi_cmd;
1480 
1481 	bzero(&scsi_cmd, sizeof(scsi_cmd));
1482 	scsi_cmd.opcode = READ_SUBCHANNEL;
1483 	if (mode == CD_MSF_FORMAT)
1484 		scsi_cmd.byte2 |= CD_MSF;
1485 	scsi_cmd.byte3 = SRS_SUBQ;
1486 	scsi_cmd.subchan_format = format;
1487 	scsi_cmd.track = track;
1488 	_lto2b(len, scsi_cmd.data_len);
1489 	return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd,
1490 	    sizeof(struct scsi_read_subchannel), (u_char *)data, len,
1491 	    SCSI_RETRIES, 5000, NULL, SCSI_DATA_IN|SCSI_SILENT);
1492 }
1493 
1494 /*
1495  * Read table of contents
1496  */
1497 int
1498 cd_read_toc(struct cd_softc *cd, int mode, int start, void *data, int len,
1499     int control)
1500 {
1501 	struct scsi_read_toc scsi_cmd;
1502 
1503 	bzero(&scsi_cmd, sizeof(scsi_cmd));
1504 	bzero(data, len);
1505 
1506 	scsi_cmd.opcode = READ_TOC;
1507 	if (mode == CD_MSF_FORMAT)
1508 		scsi_cmd.byte2 |= CD_MSF;
1509 	scsi_cmd.from_track = start;
1510 	_lto2b(len, scsi_cmd.data_len);
1511 	scsi_cmd.control = control;
1512 
1513 	return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd,
1514 	    sizeof(struct scsi_read_toc), (u_char *)data, len, SCSI_RETRIES,
1515 	    5000, NULL, SCSI_DATA_IN | SCSI_IGNORE_ILLEGAL_REQUEST);
1516 }
1517 
1518 int
1519 cd_load_toc(struct cd_softc *cd, struct cd_toc *toc, int fmt)
1520 {
1521 	int n, len, error;
1522 
1523 	error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 0);
1524 
1525 	if (error == 0) {
1526 		if (toc->header.ending_track < toc->header.starting_track)
1527 			return (EIO);
1528 		/* +2 to account for leading out track. */
1529 		n = toc->header.ending_track - toc->header.starting_track + 2;
1530 		len = n * sizeof(struct cd_toc_entry) + sizeof(toc->header);
1531 		error = cd_read_toc(cd, fmt, 0, toc, len, 0);
1532 	}
1533 
1534 	return (error);
1535 }
1536 
1537 
1538 /*
1539  * Get the scsi driver to send a full inquiry to the device and use the
1540  * results to fill out the disk parameter structure.
1541  */
1542 int
1543 cd_get_parms(struct cd_softc *cd, int flags)
1544 {
1545 	/* Reasonable defaults for drives that don't support READ_CAPACITY */
1546 	cd->params.blksize = 2048;
1547 	cd->params.disksize = 400000;
1548 
1549 	if (cd->sc_link->quirks & ADEV_NOCAPACITY)
1550 		return (0);
1551 
1552 	cd->params.disksize = scsi_size(cd->sc_link, flags,
1553 	    &cd->params.blksize);
1554 
1555 	if ((cd->params.blksize < 512) || ((cd->params.blksize & 511) != 0))
1556 		cd->params.blksize = 2048;	/* some drives lie ! */
1557 
1558 	if (cd->params.disksize < 100)
1559 		cd->params.disksize = 400000;
1560 
1561 	return (0);
1562 }
1563 
1564 daddr64_t
1565 cdsize(dev_t dev)
1566 {
1567 
1568 	/* CD-ROMs are read-only. */
1569 	return -1;
1570 }
1571 
1572 int
1573 cddump(dev_t dev, daddr64_t blkno, caddr_t va, size_t size)
1574 {
1575 	/* Not implemented. */
1576 	return ENXIO;
1577 }
1578 
1579 #define	dvd_copy_key(dst, src)		bcopy((src), (dst), DVD_KEY_SIZE)
1580 #define	dvd_copy_challenge(dst, src)	bcopy((src), (dst), DVD_CHALLENGE_SIZE)
1581 
1582 int
1583 dvd_auth(struct cd_softc *cd, union dvd_authinfo *a)
1584 {
1585 	struct scsi_generic cmd;
1586 	u_int8_t buf[20];
1587 	int error;
1588 
1589 	bzero(cmd.bytes, sizeof(cmd.bytes));
1590 	bzero(buf, sizeof(buf));
1591 
1592 	switch (a->type) {
1593 	case DVD_LU_SEND_AGID:
1594 		cmd.opcode = GPCMD_REPORT_KEY;
1595 		cmd.bytes[8] = 8;
1596 		cmd.bytes[9] = 0 | (0 << 6);
1597 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 8,
1598 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1599 		if (error)
1600 			return (error);
1601 		a->lsa.agid = buf[7] >> 6;
1602 		return (0);
1603 
1604 	case DVD_LU_SEND_CHALLENGE:
1605 		cmd.opcode = GPCMD_REPORT_KEY;
1606 		cmd.bytes[8] = 16;
1607 		cmd.bytes[9] = 1 | (a->lsc.agid << 6);
1608 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 16,
1609 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1610 		if (error)
1611 			return (error);
1612 		dvd_copy_challenge(a->lsc.chal, &buf[4]);
1613 		return (0);
1614 
1615 	case DVD_LU_SEND_KEY1:
1616 		cmd.opcode = GPCMD_REPORT_KEY;
1617 		cmd.bytes[8] = 12;
1618 		cmd.bytes[9] = 2 | (a->lsk.agid << 6);
1619 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 12,
1620 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1621 		if (error)
1622 			return (error);
1623 		dvd_copy_key(a->lsk.key, &buf[4]);
1624 		return (0);
1625 
1626 	case DVD_LU_SEND_TITLE_KEY:
1627 		cmd.opcode = GPCMD_REPORT_KEY;
1628 		_lto4b(a->lstk.lba, &cmd.bytes[1]);
1629 		cmd.bytes[8] = 12;
1630 		cmd.bytes[9] = 4 | (a->lstk.agid << 6);
1631 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 12,
1632 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1633 		if (error)
1634 			return (error);
1635 		a->lstk.cpm = (buf[4] >> 7) & 1;
1636 		a->lstk.cp_sec = (buf[4] >> 6) & 1;
1637 		a->lstk.cgms = (buf[4] >> 4) & 3;
1638 		dvd_copy_key(a->lstk.title_key, &buf[5]);
1639 		return (0);
1640 
1641 	case DVD_LU_SEND_ASF:
1642 		cmd.opcode = GPCMD_REPORT_KEY;
1643 		cmd.bytes[8] = 8;
1644 		cmd.bytes[9] = 5 | (a->lsasf.agid << 6);
1645 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 8,
1646 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1647 		if (error)
1648 			return (error);
1649 		a->lsasf.asf = buf[7] & 1;
1650 		return (0);
1651 
1652 	case DVD_HOST_SEND_CHALLENGE:
1653 		cmd.opcode = GPCMD_SEND_KEY;
1654 		cmd.bytes[8] = 16;
1655 		cmd.bytes[9] = 1 | (a->hsc.agid << 6);
1656 		buf[1] = 14;
1657 		dvd_copy_challenge(&buf[4], a->hsc.chal);
1658 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 16,
1659 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_OUT);
1660 		if (error)
1661 			return (error);
1662 		a->type = DVD_LU_SEND_KEY1;
1663 		return (0);
1664 
1665 	case DVD_HOST_SEND_KEY2:
1666 		cmd.opcode = GPCMD_SEND_KEY;
1667 		cmd.bytes[8] = 12;
1668 		cmd.bytes[9] = 3 | (a->hsk.agid << 6);
1669 		buf[1] = 10;
1670 		dvd_copy_key(&buf[4], a->hsk.key);
1671 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 12,
1672 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_OUT);
1673 		if (error) {
1674 			a->type = DVD_AUTH_FAILURE;
1675 			return (error);
1676 		}
1677 		a->type = DVD_AUTH_ESTABLISHED;
1678 		return (0);
1679 
1680 	case DVD_INVALIDATE_AGID:
1681 		cmd.opcode = GPCMD_REPORT_KEY;
1682 		cmd.bytes[9] = 0x3f | (a->lsa.agid << 6);
1683 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 16,
1684 		    SCSI_RETRIES, 30000, NULL, 0);
1685 		if (error)
1686 			return (error);
1687 		return (0);
1688 
1689 	case DVD_LU_SEND_RPC_STATE:
1690 		cmd.opcode = GPCMD_REPORT_KEY;
1691 		cmd.bytes[8] = 8;
1692 		cmd.bytes[9] = 8 | (0 << 6);
1693 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 8,
1694 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1695 		if (error)
1696 			return (error);
1697 		a->lrpcs.type = (buf[4] >> 6) & 3;
1698 		a->lrpcs.vra = (buf[4] >> 3) & 7;
1699 		a->lrpcs.ucca = (buf[4]) & 7;
1700 		a->lrpcs.region_mask = buf[5];
1701 		a->lrpcs.rpc_scheme = buf[6];
1702 		return (0);
1703 
1704 	case DVD_HOST_SEND_RPC_STATE:
1705 		cmd.opcode = GPCMD_SEND_KEY;
1706 		cmd.bytes[8] = 8;
1707 		cmd.bytes[9] = 6 | (0 << 6);
1708 		buf[1] = 6;
1709 		buf[4] = a->hrpcs.pdrc;
1710 		error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, 8,
1711 		    SCSI_RETRIES, 30000, NULL, SCSI_DATA_OUT);
1712 		if (error)
1713 			return (error);
1714 		return (0);
1715 
1716 	default:
1717 		return (ENOTTY);
1718 	}
1719 }
1720 
1721 int
1722 dvd_read_physical(cd, s)
1723 	struct cd_softc *cd;
1724 	union dvd_struct *s;
1725 {
1726 	struct scsi_generic cmd;
1727 	u_int8_t buf[4 + 4 * 20], *bufp;
1728 	int error;
1729 	struct dvd_layer *layer;
1730 	int i;
1731 
1732 	bzero(cmd.bytes, sizeof(cmd.bytes));
1733 	bzero(buf, sizeof(buf));
1734 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1735 	cmd.bytes[6] = s->type;
1736 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1737 
1738 	cmd.bytes[5] = s->physical.layer_num;
1739 	error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, sizeof(buf),
1740 	    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1741 	if (error)
1742 		return (error);
1743 	for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4;
1744 	    i++, bufp += 20, layer++) {
1745 		bzero(layer, sizeof(*layer));
1746 		layer->book_version = bufp[0] & 0xf;
1747 		layer->book_type = bufp[0] >> 4;
1748 		layer->min_rate = bufp[1] & 0xf;
1749 		layer->disc_size = bufp[1] >> 4;
1750 		layer->layer_type = bufp[2] & 0xf;
1751 		layer->track_path = (bufp[2] >> 4) & 1;
1752 		layer->nlayers = (bufp[2] >> 5) & 3;
1753 		layer->track_density = bufp[3] & 0xf;
1754 		layer->linear_density = bufp[3] >> 4;
1755 		layer->start_sector = _4btol(&bufp[4]);
1756 		layer->end_sector = _4btol(&bufp[8]);
1757 		layer->end_sector_l0 = _4btol(&bufp[12]);
1758 		layer->bca = bufp[16] >> 7;
1759 	}
1760 	return (0);
1761 }
1762 
1763 int
1764 dvd_read_copyright(cd, s)
1765 	struct cd_softc *cd;
1766 	union dvd_struct *s;
1767 {
1768 	struct scsi_generic cmd;
1769 	u_int8_t buf[8];
1770 	int error;
1771 
1772 	bzero(cmd.bytes, sizeof(cmd.bytes));
1773 	bzero(buf, sizeof(buf));
1774 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1775 	cmd.bytes[6] = s->type;
1776 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1777 
1778 	cmd.bytes[5] = s->copyright.layer_num;
1779 	error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, sizeof(buf),
1780 	    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1781 	if (error)
1782 		return (error);
1783 	s->copyright.cpst = buf[4];
1784 	s->copyright.rmi = buf[5];
1785 	return (0);
1786 }
1787 
1788 int
1789 dvd_read_disckey(cd, s)
1790 	struct cd_softc *cd;
1791 	union dvd_struct *s;
1792 {
1793 	struct scsi_read_dvd_structure cmd;
1794 	struct scsi_read_dvd_structure_data *buf;
1795 	int error;
1796 
1797 	buf = malloc(sizeof(*buf), M_TEMP, M_WAITOK | M_ZERO);
1798 	if (buf == NULL)
1799 		return (ENOMEM);
1800 
1801 	bzero(&cmd, sizeof(cmd));
1802 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1803 	cmd.format = s->type;
1804 	cmd.agid = s->disckey.agid << 6;
1805 	_lto2b(sizeof(*buf), cmd.length);
1806 
1807 	error = scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&cmd,
1808 	    sizeof(cmd), (u_char *)buf, sizeof(*buf), SCSI_RETRIES, 30000, NULL,
1809 	    SCSI_DATA_IN);
1810 	if (error == 0)
1811 		bcopy(buf->data, s->disckey.value, sizeof(s->disckey.value));
1812 
1813 	free(buf, M_TEMP);
1814 	return (error);
1815 }
1816 
1817 int
1818 dvd_read_bca(cd, s)
1819 	struct cd_softc *cd;
1820 	union dvd_struct *s;
1821 {
1822 	struct scsi_generic cmd;
1823 	u_int8_t buf[4 + 188];
1824 	int error;
1825 
1826 	bzero(cmd.bytes, sizeof(cmd.bytes));
1827 	bzero(buf, sizeof(buf));
1828 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1829 	cmd.bytes[6] = s->type;
1830 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1831 
1832 	error = scsi_scsi_cmd(cd->sc_link, &cmd, sizeof(cmd), buf, sizeof(buf),
1833 	    SCSI_RETRIES, 30000, NULL, SCSI_DATA_IN);
1834 	if (error)
1835 		return (error);
1836 	s->bca.len = _2btol(&buf[0]);
1837 	if (s->bca.len < 12 || s->bca.len > 188)
1838 		return (EIO);
1839 	bcopy(&buf[4], s->bca.value, s->bca.len);
1840 	return (0);
1841 }
1842 
1843 int
1844 dvd_read_manufact(cd, s)
1845 	struct cd_softc *cd;
1846 	union dvd_struct *s;
1847 {
1848 	struct scsi_read_dvd_structure cmd;
1849 	struct scsi_read_dvd_structure_data *buf;
1850 	int error;
1851 
1852 	buf = malloc(sizeof(*buf), M_TEMP, M_WAITOK | M_ZERO);
1853 	if (buf == NULL)
1854 		return (ENOMEM);
1855 
1856 	bzero(&cmd, sizeof(cmd));
1857 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1858 	cmd.format = s->type;
1859 	_lto2b(sizeof(*buf), cmd.length);
1860 
1861 	error = scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&cmd,
1862 	    sizeof(cmd), (u_char *)buf, sizeof(*buf), SCSI_RETRIES, 30000, NULL,
1863 	    SCSI_DATA_IN);
1864 	if (error == 0) {
1865 		s->manufact.len = _2btol(buf->len);
1866 		if (s->manufact.len >= 0 && s->manufact.len <= 2048)
1867 			bcopy(buf->data, s->manufact.value, s->manufact.len);
1868 		else
1869 			error = EIO;
1870 	}
1871 
1872 	free(buf, M_TEMP);
1873 	return (error);
1874 }
1875 
1876 int
1877 dvd_read_struct(cd, s)
1878 	struct cd_softc *cd;
1879 	union dvd_struct *s;
1880 {
1881 
1882 	switch (s->type) {
1883 	case DVD_STRUCT_PHYSICAL:
1884 		return (dvd_read_physical(cd, s));
1885 	case DVD_STRUCT_COPYRIGHT:
1886 		return (dvd_read_copyright(cd, s));
1887 	case DVD_STRUCT_DISCKEY:
1888 		return (dvd_read_disckey(cd, s));
1889 	case DVD_STRUCT_BCA:
1890 		return (dvd_read_bca(cd, s));
1891 	case DVD_STRUCT_MANUFACT:
1892 		return (dvd_read_manufact(cd, s));
1893 	default:
1894 		return (EINVAL);
1895 	}
1896 }
1897 
1898 void
1899 cd_powerhook(int why, void *arg)
1900 {
1901 	struct cd_softc *cd = arg;
1902 
1903 	/*
1904 	 * When resuming, hardware may have forgotten we locked it. So if
1905 	 * there are any open partitions, lock the CD.
1906 	 */
1907 	if (why == PWR_RESUME && cd->sc_dk.dk_openmask != 0)
1908 		scsi_prevent(cd->sc_link, PR_PREVENT,
1909 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
1910 		    SCSI_SILENT);
1911 }
1912 
1913 int
1914 cd_interpret_sense(struct scsi_xfer *xs)
1915 {
1916 	struct scsi_sense_data *sense = &xs->sense;
1917 	struct scsi_link *sc_link = xs->sc_link;
1918 	u_int8_t skey = sense->flags & SSD_KEY;
1919 	u_int8_t serr = sense->error_code & SSD_ERRCODE;
1920 
1921 	if (((sc_link->flags & SDEV_OPEN) == 0) ||
1922 	    (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED))
1923 		return (EJUSTRETURN); /* let the generic code handle it */
1924 
1925 	/*
1926 	 * We do custom processing in cd for the unit becoming ready
1927 	 * case.  We do not allow xs->retries to be decremented on the
1928 	 * "Unit Becoming Ready" case. This is because CD drives
1929 	 * report "Unit Becoming Ready" when loading media and can
1930 	 * take a long time.  Rather than having a massive timeout for
1931 	 * all operations (which would cause other problems), we allow
1932 	 * operations to wait (but be interruptable with Ctrl-C)
1933 	 * forever as long as the drive is reporting that it is
1934 	 * becoming ready.  All other cases of not being ready are
1935 	 * handled by the default handler.
1936 	 */
1937 	switch(skey) {
1938 	case SKEY_NOT_READY:
1939 		if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
1940 			return (0);
1941 		if (ASC_ASCQ(sense) == SENSE_NOT_READY_BECOMING_READY) {
1942 		    	SC_DEBUG(sc_link, SDEV_DB1, ("not ready: busy (%#x)\n",
1943 			    sense->add_sense_code_qual));
1944 			/* don't count this as a retry */
1945 			xs->retries++;
1946 			return (scsi_delay(xs, 1));
1947 		}
1948 		break;
1949 	/* XXX more to come here for a few other cases */
1950 	default:
1951 		break;
1952 	}
1953 	return (EJUSTRETURN); /* use generic handler in scsi_base */
1954 }
1955 
1956 /*
1957  * Remove unprocessed buffers from queue.
1958  */
1959 void
1960 cd_kill_buffers(struct cd_softc *cd)
1961 {
1962 	struct buf *dp, *bp;
1963 	int s;
1964 
1965 	s = splbio();
1966 	for (dp = &cd->buf_queue; (bp = dp->b_actf) != NULL; ) {
1967 		dp->b_actf = bp->b_actf;
1968 
1969 		bp->b_error = ENXIO;
1970 		bp->b_flags |= B_ERROR;
1971 		biodone(bp);
1972 	}
1973 	splx(s);
1974 }
1975 
1976 #if defined(__macppc__)
1977 int
1978 cd_eject(void)
1979 {
1980 	struct cd_softc *cd;
1981 	int error = 0;
1982 
1983 	if (cd_cd.cd_ndevs == 0 || (cd = cd_cd.cd_devs[0]) == NULL)
1984 		return (ENXIO);
1985 
1986 	if ((error = cdlock(cd)) != 0)
1987 		return (error);
1988 
1989 	if (cd->sc_dk.dk_openmask == 0) {
1990 		cd->sc_link->flags |= SDEV_EJECTING;
1991 
1992 		scsi_prevent(cd->sc_link, PR_ALLOW,
1993 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
1994 		    SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE);
1995 		cd->sc_link->flags &= ~SDEV_MEDIA_LOADED;
1996 
1997 		scsi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0);
1998 
1999 		cd->sc_link->flags &= ~SDEV_EJECTING;
2000 	}
2001 	cdunlock(cd);
2002 
2003 	return (error);
2004 }
2005 #endif
2006