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