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