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