xref: /netbsd/sys/dev/scsipi/cd.c (revision bf9ec67e)
1 /*	$NetBSD: cd.c,v 1.163 2002/05/27 16:42:30 drochner Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Originally written by Julian Elischer (julian@tfs.com)
41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
42  *
43  * TRW Financial Systems, in accordance with their agreement with Carnegie
44  * Mellon University, makes this software available to CMU to distribute
45  * or use in any manner that they see fit as long as this message is kept with
46  * the software. For this reason TFS also grants any other persons or
47  * organisations permission to use or modify this software.
48  *
49  * TFS supplies this software to be publicly redistributed
50  * on the understanding that TFS is not responsible for the correct
51  * functioning of this software in any circumstances.
52  *
53  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
54  */
55 
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: cd.c,v 1.163 2002/05/27 16:42:30 drochner Exp $");
58 
59 #include "rnd.h"
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/file.h>
65 #include <sys/stat.h>
66 #include <sys/ioctl.h>
67 #include <sys/buf.h>
68 #include <sys/uio.h>
69 #include <sys/malloc.h>
70 #include <sys/errno.h>
71 #include <sys/device.h>
72 #include <sys/disklabel.h>
73 #include <sys/disk.h>
74 #include <sys/cdio.h>
75 #include <sys/dvdio.h>
76 #include <sys/scsiio.h>
77 #include <sys/proc.h>
78 #include <sys/conf.h>
79 #include <sys/vnode.h>
80 #if NRND > 0
81 #include <sys/rnd.h>
82 #endif
83 
84 #include <dev/scsipi/scsipi_all.h>
85 #include <dev/scsipi/scsipi_cd.h>
86 #include <dev/scsipi/scsipi_disk.h>	/* rw_big and start_stop come */
87 					/* from there */
88 #include <dev/scsipi/scsi_disk.h>	/* rw comes from there */
89 #include <dev/scsipi/scsipiconf.h>
90 #include <dev/scsipi/cdvar.h>
91 
92 #include "cd.h"		/* NCD_SCSIBUS and NCD_ATAPIBUS come from here */
93 
94 #define	CDUNIT(z)			DISKUNIT(z)
95 #define	CDPART(z)			DISKPART(z)
96 #define	CDMINOR(unit, part)		DISKMINOR(unit, part)
97 #define	MAKECDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
98 
99 #define MAXTRACK	99
100 #define CD_BLOCK_OFFSET	150
101 #define CD_FRAMES	75
102 #define CD_SECS		60
103 
104 struct cd_toc {
105 	struct ioc_toc_header header;
106 	struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */
107 						 /* leadout */
108 };
109 
110 int	cdlock __P((struct cd_softc *));
111 void	cdunlock __P((struct cd_softc *));
112 void	cdstart __P((struct scsipi_periph *));
113 void	cdminphys __P((struct buf *));
114 void	cdgetdefaultlabel __P((struct cd_softc *, struct disklabel *));
115 void	cdgetdisklabel __P((struct cd_softc *));
116 void	cddone __P((struct scsipi_xfer *));
117 void	cdbounce __P((struct buf *));
118 int	cd_interpret_sense __P((struct scsipi_xfer *));
119 u_long	cd_size __P((struct cd_softc *, int));
120 void	lba2msf __P((u_long, u_char *, u_char *, u_char *));
121 u_long	msf2lba __P((u_char, u_char, u_char));
122 int	cd_play __P((struct cd_softc *, int, int));
123 int	cd_play_tracks __P((struct cd_softc *, int, int, int, int));
124 int	cd_play_msf __P((struct cd_softc *, int, int, int, int, int, int));
125 int	cd_pause __P((struct cd_softc *, int));
126 int	cd_reset __P((struct cd_softc *));
127 int	cd_read_subchannel __P((struct cd_softc *, int, int, int,
128 	    struct cd_sub_channel_info *, int, int));
129 int	cd_read_toc __P((struct cd_softc *, int, int, void *, int, int, int));
130 int	cd_get_parms __P((struct cd_softc *, int));
131 int	cd_load_toc __P((struct cd_softc *, struct cd_toc *, int));
132 int	cdreadmsaddr __P((struct cd_softc *, int *));
133 int	dvd_auth __P((struct cd_softc *, dvd_authinfo *));
134 int	dvd_read_physical __P((struct cd_softc *, dvd_struct *));
135 int	dvd_read_copyright __P((struct cd_softc *, dvd_struct *));
136 int	dvd_read_disckey __P((struct cd_softc *, dvd_struct *));
137 int	dvd_read_bca __P((struct cd_softc *, dvd_struct *));
138 int	dvd_read_manufact __P((struct cd_softc *, dvd_struct *));
139 int	dvd_read_struct __P((struct cd_softc *, dvd_struct *));
140 
141 extern struct cfdriver cd_cd;
142 
143 struct dkdriver cddkdriver = { cdstrategy };
144 
145 const struct scsipi_periphsw cd_switch = {
146 	cd_interpret_sense,	/* use our error handler first */
147 	cdstart,		/* we have a queue, which is started by this */
148 	NULL,			/* we do not have an async handler */
149 	cddone,			/* deal with stats at interrupt time */
150 };
151 
152 /*
153  * The routine called by the low level scsi routine when it discovers
154  * A device suitable for this driver
155  */
156 void
157 cdattach(parent, cd, periph, ops)
158 	struct device *parent;
159 	struct cd_softc *cd;
160 	struct scsipi_periph *periph;
161 	const struct cd_ops *ops;
162 {
163 	SC_DEBUG(periph, SCSIPI_DB2, ("cdattach: "));
164 
165 	BUFQ_INIT(&cd->buf_queue);
166 
167 	/*
168 	 * Store information needed to contact our base driver
169 	 */
170 	cd->sc_periph = periph;
171 	cd->sc_ops = ops;
172 
173 	periph->periph_dev = &cd->sc_dev;
174 	periph->periph_switch = &cd_switch;
175 
176 	/*
177 	 * Increase our openings to the maximum-per-periph
178 	 * supported by the adapter.  This will either be
179 	 * clamped down or grown by the adapter if necessary.
180 	 */
181 	periph->periph_openings =
182 	    SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
183 	periph->periph_flags |= PERIPH_GROW_OPENINGS;
184 
185 	/*
186 	 * Initialize and attach the disk structure.
187 	 */
188   	cd->sc_dk.dk_driver = &cddkdriver;
189 	cd->sc_dk.dk_name = cd->sc_dev.dv_xname;
190 	disk_attach(&cd->sc_dk);
191 
192 #ifdef __BROKEN_DK_ESTABLISH
193 	dk_establish(&cd->sc_dk, &cd->sc_dev);		/* XXX */
194 #endif
195 
196 	printf("\n");
197 
198 #if NRND > 0
199 	rnd_attach_source(&cd->rnd_source, cd->sc_dev.dv_xname,
200 			  RND_TYPE_DISK, 0);
201 #endif
202 }
203 
204 int
205 cdactivate(self, act)
206 	struct device *self;
207 	enum devact act;
208 {
209 	int rv = 0;
210 
211 	switch (act) {
212 	case DVACT_ACTIVATE:
213 		rv = EOPNOTSUPP;
214 		break;
215 
216 	case DVACT_DEACTIVATE:
217 		/*
218 		 * Nothing to do; we key off the device's DVF_ACTIVE.
219 		 */
220 		break;
221 	}
222 	return (rv);
223 }
224 
225 int
226 cddetach(self, flags)
227 	struct device *self;
228 	int flags;
229 {
230 	struct cd_softc *cd = (struct cd_softc *) self;
231 	struct buf *bp;
232 	int s, bmaj, cmaj, i, mn;
233 
234 	/* locate the major number */
235 	for (bmaj = 0; bmaj <= nblkdev; bmaj++)
236 		if (bdevsw[bmaj].d_open == cdopen)
237 			break;
238 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
239 		if (cdevsw[cmaj].d_open == cdopen)
240 			break;
241 
242 	s = splbio();
243 
244 	/* Kill off any queued buffers. */
245 	while ((bp = BUFQ_FIRST(&cd->buf_queue)) != NULL) {
246 		BUFQ_REMOVE(&cd->buf_queue, bp);
247 		bp->b_error = EIO;
248 		bp->b_flags |= B_ERROR;
249 		bp->b_resid = bp->b_bcount;
250 		biodone(bp);
251 	}
252 
253 	/* Kill off any pending commands. */
254 	scsipi_kill_pending(cd->sc_periph);
255 
256 	splx(s);
257 
258 	/* Nuke the vnodes for any open instances */
259 	for (i = 0; i < MAXPARTITIONS; i++) {
260 		mn = CDMINOR(self->dv_unit, i);
261 		vdevgone(bmaj, mn, mn, VBLK);
262 		vdevgone(cmaj, mn, mn, VCHR);
263 	}
264 
265 	/* Detach from the disk list. */
266 	disk_detach(&cd->sc_dk);
267 
268 #if 0
269 	/* Get rid of the shutdown hook. */
270 	if (cd->sc_sdhook != NULL)
271 		shutdownhook_disestablish(cd->sc_sdhook);
272 #endif
273 
274 #if NRND > 0
275 	/* Unhook the entropy source. */
276 	rnd_detach_source(&cd->rnd_source);
277 #endif
278 
279 	return (0);
280 }
281 
282 /*
283  * Wait interruptibly for an exclusive lock.
284  *
285  * XXX
286  * Several drivers do this; it should be abstracted and made MP-safe.
287  */
288 int
289 cdlock(cd)
290 	struct cd_softc *cd;
291 {
292 	int error;
293 
294 	while ((cd->flags & CDF_LOCKED) != 0) {
295 		cd->flags |= CDF_WANTED;
296 		if ((error = tsleep(cd, PRIBIO | PCATCH, "cdlck", 0)) != 0)
297 			return (error);
298 	}
299 	cd->flags |= CDF_LOCKED;
300 	return (0);
301 }
302 
303 /*
304  * Unlock and wake up any waiters.
305  */
306 void
307 cdunlock(cd)
308 	struct cd_softc *cd;
309 {
310 
311 	cd->flags &= ~CDF_LOCKED;
312 	if ((cd->flags & CDF_WANTED) != 0) {
313 		cd->flags &= ~CDF_WANTED;
314 		wakeup(cd);
315 	}
316 }
317 
318 /*
319  * open the device. Make sure the partition info is a up-to-date as can be.
320  */
321 int
322 cdopen(dev, flag, fmt, p)
323 	dev_t dev;
324 	int flag, fmt;
325 	struct proc *p;
326 {
327 	struct cd_softc *cd;
328 	struct scsipi_periph *periph;
329 	struct scsipi_adapter *adapt;
330 	struct cd_sub_channel_info data;
331 	int unit, part;
332 	int error;
333 
334 	unit = CDUNIT(dev);
335 	if (unit >= cd_cd.cd_ndevs)
336 		return (ENXIO);
337 	cd = cd_cd.cd_devs[unit];
338 	if (cd == NULL)
339 		return (ENXIO);
340 
341 	periph = cd->sc_periph;
342 	adapt = periph->periph_channel->chan_adapter;
343 	part = CDPART(dev);
344 
345 	SC_DEBUG(periph, SCSIPI_DB1,
346 	    ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
347 	    cd_cd.cd_ndevs, CDPART(dev)));
348 
349 	/*
350 	 * If this is the first open of this device, add a reference
351 	 * to the adapter.
352 	 */
353 	if (cd->sc_dk.dk_openmask == 0 &&
354 	    (error = scsipi_adapter_addref(adapt)) != 0)
355 		return (error);
356 
357 	if ((error = cdlock(cd)) != 0)
358 		goto bad4;
359 
360 	if ((periph->periph_flags & PERIPH_OPEN) != 0) {
361 		/*
362 		 * If any partition is open, but the disk has been invalidated,
363 		 * disallow further opens.
364 		 */
365 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
366 			(part != RAW_PART || fmt != S_IFCHR )) {
367 			error = EIO;
368 			goto bad3;
369 		}
370 	} else {
371 		/* Check that it is still responding and ok. */
372 		error = scsipi_test_unit_ready(periph,
373 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
374 		    XS_CTL_SILENT_NODEV);
375 		SC_DEBUG(periph, SCSIPI_DB1,
376 		    ("cdopen: scsipi_test_unit_ready, error=%d\n", error));
377 		if (error) {
378 			if (part != RAW_PART || fmt != S_IFCHR)
379 				goto bad3;
380 			else
381 				goto out;
382 		}
383 
384 		/* Don't try to start the unit if audio is playing. */
385 		error = cd_read_subchannel(cd, CD_LBA_FORMAT,
386 		    CD_CURRENT_POSITION, 0, &data, sizeof(data),
387 		    XS_CTL_DATA_ONSTACK);
388 		if ((data.header.audio_status != CD_AS_PLAY_IN_PROGRESS &&
389 		    data.header.audio_status != CD_AS_PLAY_PAUSED) || error) {
390 			/*
391 			 * Start the pack spinning if necessary. Always
392 			 * allow the raw parition to be opened, for raw
393 			 * IOCTLs. Data transfers will check for
394 			 * SDEV_MEDIA_LOADED.
395 			 */
396 			error = scsipi_start(periph, SSS_START,
397 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
398 			    XS_CTL_IGNORE_MEDIA_CHANGE |
399 			    XS_CTL_SILENT);
400 			SC_DEBUG(periph, SCSIPI_DB1,
401 			    ("cdopen: scsipi_start, error=%d\n", error));
402 			if (error) {
403 				if (part != RAW_PART || fmt != S_IFCHR)
404 					goto bad3;
405 				else
406 					goto out;
407 			}
408 		}
409 
410 		periph->periph_flags |= PERIPH_OPEN;
411 
412 		/* Lock the pack in. */
413 		error = scsipi_prevent(periph, PR_PREVENT,
414 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
415 		SC_DEBUG(periph, SCSIPI_DB1,
416 		    ("cdopen: scsipi_prevent, error=%d\n", error));
417 		if (error)
418 			goto bad;
419 
420 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
421 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
422 
423 			/* Load the physical device parameters. */
424 			if (cd_get_parms(cd, 0) != 0) {
425 				error = ENXIO;
426 				goto bad2;
427 			}
428 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
429 
430 			/* Fabricate a disk label. */
431 			cdgetdisklabel(cd);
432 			SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel fabricated "));
433 		}
434 	}
435 
436 	/* Check that the partition exists. */
437 	if (part != RAW_PART &&
438 	    (part >= cd->sc_dk.dk_label->d_npartitions ||
439 	    cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
440 		error = ENXIO;
441 		goto bad;
442 	}
443 
444 out:	/* Insure only one open at a time. */
445 	switch (fmt) {
446 	case S_IFCHR:
447 		cd->sc_dk.dk_copenmask |= (1 << part);
448 		break;
449 	case S_IFBLK:
450 		cd->sc_dk.dk_bopenmask |= (1 << part);
451 		break;
452 	}
453 	cd->sc_dk.dk_openmask =
454 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
455 
456 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
457 	cdunlock(cd);
458 	return (0);
459 
460 bad2:
461 	periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
462 
463 bad:
464 	if (cd->sc_dk.dk_openmask == 0) {
465 		scsipi_prevent(periph, PR_ALLOW,
466 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
467 		periph->periph_flags &= ~PERIPH_OPEN;
468 	}
469 
470 bad3:
471 	cdunlock(cd);
472 bad4:
473 	if (cd->sc_dk.dk_openmask == 0)
474 		scsipi_adapter_delref(adapt);
475 	return (error);
476 }
477 
478 /*
479  * close the device.. only called if we are the LAST
480  * occurence of an open device
481  */
482 int
483 cdclose(dev, flag, fmt, p)
484 	dev_t dev;
485 	int flag, fmt;
486 	struct proc *p;
487 {
488 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
489 	struct scsipi_periph *periph = cd->sc_periph;
490 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
491 	int part = CDPART(dev);
492 	int error;
493 
494 	if ((error = cdlock(cd)) != 0)
495 		return (error);
496 
497 	switch (fmt) {
498 	case S_IFCHR:
499 		cd->sc_dk.dk_copenmask &= ~(1 << part);
500 		break;
501 	case S_IFBLK:
502 		cd->sc_dk.dk_bopenmask &= ~(1 << part);
503 		break;
504 	}
505 	cd->sc_dk.dk_openmask =
506 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
507 
508 	if (cd->sc_dk.dk_openmask == 0) {
509 		scsipi_wait_drain(periph);
510 
511 		scsipi_prevent(periph, PR_ALLOW,
512 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
513 		    XS_CTL_IGNORE_NOT_READY);
514 		periph->periph_flags &= ~PERIPH_OPEN;
515 
516 		scsipi_wait_drain(periph);
517 
518 		scsipi_adapter_delref(adapt);
519 	}
520 
521 	cdunlock(cd);
522 	return (0);
523 }
524 
525 /*
526  * Actually translate the requested transfer into one the physical driver can
527  * understand.  The transfer is described by a buf and will include only one
528  * physical transfer.
529  */
530 void
531 cdstrategy(bp)
532 	struct buf *bp;
533 {
534 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
535 	struct disklabel *lp;
536 	struct scsipi_periph *periph = cd->sc_periph;
537 	daddr_t blkno;
538 	int s;
539 
540 	SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdstrategy "));
541 	SC_DEBUG(cd->sc_periph, SCSIPI_DB1,
542 	    ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
543 	/*
544 	 * If the device has been made invalid, error out
545 	 * maybe the media changed
546 	 */
547 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
548 		if (periph->periph_flags & PERIPH_OPEN)
549 			bp->b_error = EIO;
550 		else
551 			bp->b_error = ENODEV;
552 		goto bad;
553 	}
554 
555 	lp = cd->sc_dk.dk_label;
556 
557 	/*
558 	 * The transfer must be a whole number of blocks, offset must not
559 	 * be negative.
560 	 */
561 	if ((bp->b_bcount % lp->d_secsize) != 0 ||
562 	    bp->b_blkno < 0 ) {
563 		bp->b_error = EINVAL;
564 		goto bad;
565 	}
566 	/*
567 	 * If it's a null transfer, return immediately
568 	 */
569 	if (bp->b_bcount == 0)
570 		goto done;
571 
572 	/*
573 	 * Do bounds checking, adjust transfer. if error, process.
574 	 * If end of partition, just return.
575 	 */
576 	if (CDPART(bp->b_dev) != RAW_PART &&
577 	    bounds_check_with_label(bp, lp,
578 	    (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0)
579 		goto done;
580 
581 	/*
582 	 * Now convert the block number to absolute and put it in
583 	 * terms of the device's logical block size.
584 	 */
585 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
586 	if (CDPART(bp->b_dev) != RAW_PART)
587 		blkno += lp->d_partitions[CDPART(bp->b_dev)].p_offset;
588 
589 	bp->b_rawblkno = blkno;
590 
591 	/*
592 	 * If the disklabel sector size does not match the device
593 	 * sector size we may need to do some extra work.
594 	 */
595 	if (lp->d_secsize != cd->params.blksize) {
596 
597 		/*
598 		 * If the xfer is not a multiple of the device block size
599 		 * or it is not block aligned, we need to bounce it.
600 		 */
601 		if ((bp->b_bcount % cd->params.blksize) != 0 ||
602 			((blkno * lp->d_secsize) % cd->params.blksize) != 0) {
603 			struct buf *nbp;
604 			void *bounce = NULL;
605 			long count;
606 
607 			if ((bp->b_flags & B_READ) == 0) {
608 
609 				/* XXXX We don't support bouncing writes. */
610 				bp->b_error = EACCES;
611 				goto bad;
612 			}
613 			count = ((blkno * lp->d_secsize) % cd->params.blksize);
614 			/* XXX Store starting offset in bp->b_rawblkno */
615 			bp->b_rawblkno = count;
616 
617 			count += bp->b_bcount;
618 			count = roundup(count, cd->params.blksize);
619 
620 			blkno = ((blkno * lp->d_secsize) / cd->params.blksize);
621 			s = splbio();
622 			nbp = pool_get(&bufpool, PR_NOWAIT);
623 			splx(s);
624 			if (!nbp) {
625 				/* No memory -- fail the iop. */
626 				bp->b_error = ENOMEM;
627 				goto bad;
628 			}
629 			bounce = malloc(count, M_DEVBUF, M_NOWAIT);
630 			if (!bounce) {
631 				/* No memory -- fail the iop. */
632 				s = splbio();
633 				pool_put(&bufpool, nbp);
634 				splx(s);
635 				bp->b_error = ENOMEM;
636 				goto bad;
637 			}
638 
639 			/* Set up the IOP to the bounce buffer. */
640 			nbp->b_error = 0;
641 			nbp->b_proc = bp->b_proc;
642 			nbp->b_vp = NULLVP;
643 
644 			nbp->b_bcount = count;
645 			nbp->b_bufsize = count;
646 			nbp->b_data = bounce;
647 
648 			LIST_INIT(&nbp->b_dep);
649 			nbp->b_rawblkno = blkno;
650 
651 			/* We need to do a read-modify-write operation */
652 			nbp->b_flags = bp->b_flags | B_READ | B_CALL;
653 			nbp->b_iodone = cdbounce;
654 
655 			/* Put ptr to orig buf in b_private and use new buf */
656 			nbp->b_private = bp;
657 			bp = nbp;
658 
659 		} else {
660 			/* Xfer is aligned -- just adjust the start block */
661 			bp->b_rawblkno = (blkno * lp->d_secsize) /
662 				cd->params.blksize;
663 		}
664 	}
665 	s = splbio();
666 
667 	/*
668 	 * Place it in the queue of disk activities for this disk.
669 	 *
670 	 * XXX Only do disksort() if the current operating mode does not
671 	 * XXX include tagged queueing.
672 	 */
673 	disksort_blkno(&cd->buf_queue, bp);
674 
675 	/*
676 	 * Tell the device to get going on the transfer if it's
677 	 * not doing anything, otherwise just wait for completion
678 	 */
679 	cdstart(cd->sc_periph);
680 
681 	splx(s);
682 	return;
683 
684 bad:
685 	bp->b_flags |= B_ERROR;
686 done:
687 	/*
688 	 * Correctly set the buf to indicate a completed xfer
689 	 */
690 	bp->b_resid = bp->b_bcount;
691 	biodone(bp);
692 }
693 
694 /*
695  * cdstart looks to see if there is a buf waiting for the device
696  * and that the device is not already busy. If both are true,
697  * It deques the buf and creates a scsi command to perform the
698  * transfer in the buf. The transfer request will call scsipi_done
699  * on completion, which will in turn call this routine again
700  * so that the next queued transfer is performed.
701  * The bufs are queued by the strategy routine (cdstrategy)
702  *
703  * This routine is also called after other non-queued requests
704  * have been made of the scsi driver, to ensure that the queue
705  * continues to be drained.
706  *
707  * must be called at the correct (highish) spl level
708  * cdstart() is called at splbio from cdstrategy and scsipi_done
709  */
710 void
711 cdstart(periph)
712 	struct scsipi_periph *periph;
713 {
714 	struct cd_softc *cd = (void *)periph->periph_dev;
715 	struct buf *bp = 0;
716 	struct scsipi_rw_big cmd_big;
717 #if NCD_SCSIBUS > 0
718 	struct scsi_rw cmd_small;
719 #endif
720 	struct scsipi_generic *cmdp;
721 	int flags, nblks, cmdlen, error;
722 
723 	SC_DEBUG(periph, SCSIPI_DB2, ("cdstart "));
724 	/*
725 	 * Check if the device has room for another command
726 	 */
727 	while (periph->periph_active < periph->periph_openings) {
728 		/*
729 		 * there is excess capacity, but a special waits
730 		 * It'll need the adapter as soon as we clear out of the
731 		 * way and let it run (user level wait).
732 		 */
733 		if (periph->periph_flags & PERIPH_WAITING) {
734 			periph->periph_flags &= ~PERIPH_WAITING;
735 			wakeup((caddr_t)periph);
736 			return;
737 		}
738 
739 		/*
740 		 * See if there is a buf with work for us to do..
741 		 */
742 		if ((bp = BUFQ_FIRST(&cd->buf_queue)) == NULL)
743 			return;
744 		BUFQ_REMOVE(&cd->buf_queue, bp);
745 
746 		/*
747 		 * If the device has become invalid, abort all the
748 		 * reads and writes until all files have been closed and
749 		 * re-opened
750 		 */
751 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
752 			bp->b_error = EIO;
753 			bp->b_flags |= B_ERROR;
754 			bp->b_resid = bp->b_bcount;
755 			biodone(bp);
756 			continue;
757 		}
758 
759 		/*
760 		 * We have a buf, now we should make a command.
761 		 */
762 
763 		nblks = howmany(bp->b_bcount, cd->params.blksize);
764 
765 #if NCD_SCSIBUS > 0
766 		/*
767 		 *  Fill out the scsi command.  If the transfer will
768 		 *  fit in a "small" cdb, use it.
769 		 */
770 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
771 		    ((nblks & 0xff) == nblks) &&
772 		    !(periph->periph_quirks & PQUIRK_ONLYBIG) &&
773 		    scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_SCSI) {
774 			/*
775 			 * We can fit in a small cdb.
776 			 */
777 			memset(&cmd_small, 0, sizeof(cmd_small));
778 			cmd_small.opcode = (bp->b_flags & B_READ) ?
779 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
780 			_lto3b(bp->b_rawblkno, cmd_small.addr);
781 			cmd_small.length = nblks & 0xff;
782 			cmdlen = sizeof(cmd_small);
783 			cmdp = (struct scsipi_generic *)&cmd_small;
784 		} else
785 #endif
786 		{
787 			/*
788 			 * Need a large cdb.
789 			 */
790 			memset(&cmd_big, 0, sizeof(cmd_big));
791 			cmd_big.opcode = (bp->b_flags & B_READ) ?
792 			    READ_BIG : WRITE_BIG;
793 			_lto4b(bp->b_rawblkno, cmd_big.addr);
794 			_lto2b(nblks, cmd_big.length);
795 			cmdlen = sizeof(cmd_big);
796 			cmdp = (struct scsipi_generic *)&cmd_big;
797 		}
798 
799 		/* Instrumentation. */
800 		disk_busy(&cd->sc_dk);
801 
802 		/*
803 		 * Figure out what flags to use.
804 		 */
805 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC;
806 		if (bp->b_flags & B_READ)
807 			flags |= XS_CTL_DATA_IN;
808 		else
809 			flags |= XS_CTL_DATA_OUT;
810 		if (bp->b_flags & B_ORDERED)
811 			flags |= XS_CTL_ORDERED_TAG;
812 		else
813 			flags |= XS_CTL_SIMPLE_TAG;
814 
815 		/*
816 		 * Call the routine that chats with the adapter.
817 		 * Note: we cannot sleep as we may be an interrupt
818 		 */
819 		error = scsipi_command(periph, cmdp, cmdlen,
820 		    (u_char *)bp->b_data, bp->b_bcount,
821 		    CDRETRIES, 30000, bp, flags);
822 		if (error) {
823 			disk_unbusy(&cd->sc_dk, 0);
824 			printf("%s: not queued, error %d\n",
825 			    cd->sc_dev.dv_xname, error);
826 		}
827 	}
828 }
829 
830 void
831 cddone(xs)
832 	struct scsipi_xfer *xs;
833 {
834 	struct cd_softc *cd = (void *)xs->xs_periph->periph_dev;
835 
836 	if (xs->bp != NULL) {
837 		disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
838 #if NRND > 0
839 		rnd_add_uint32(&cd->rnd_source, xs->bp->b_rawblkno);
840 #endif
841 	}
842 }
843 
844 void
845 cdbounce(bp)
846 	struct buf *bp;
847 {
848 	struct buf *obp = (struct buf *)bp->b_private;
849 
850 	if (bp->b_flags & B_ERROR) {
851 		/* EEK propagate the error and free the memory */
852 		goto done;
853 	}
854 	if (obp->b_flags & B_READ) {
855 		/* Copy data to the final destination and free the buf. */
856 		memcpy(obp->b_data, bp->b_data+obp->b_rawblkno,
857 			obp->b_bcount);
858 	} else {
859 		/*
860 		 * XXXX This is a CD-ROM -- READ ONLY -- why do we bother with
861 		 * XXXX any of this write stuff?
862 		 */
863 		if (bp->b_flags & B_READ) {
864 			struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
865 			struct buf *nbp;
866 			int s;
867 
868 			/* Read part of RMW complete. */
869 			memcpy(bp->b_data+obp->b_rawblkno, obp->b_data,
870 				obp->b_bcount);
871 
872 			s = splbio();
873 
874 			/* We need to alloc a new buf. */
875 			nbp = pool_get(&bufpool, PR_NOWAIT);
876 			if (!nbp) {
877 				splx(s);
878 				/* No buf available. */
879 				bp->b_flags |= B_ERROR;
880 				bp->b_error = ENOMEM;
881 				bp->b_resid = bp->b_bcount;
882 			}
883 
884 			/* Set up the IOP to the bounce buffer. */
885 			nbp->b_error = 0;
886 			nbp->b_proc = bp->b_proc;
887 			nbp->b_vp = NULLVP;
888 
889 			nbp->b_bcount = bp->b_bcount;
890 			nbp->b_bufsize = bp->b_bufsize;
891 			nbp->b_data = bp->b_data;
892 
893 			LIST_INIT(&nbp->b_dep);
894 			nbp->b_rawblkno = bp->b_rawblkno;
895 
896 			/* We need to do a read-modify-write operation */
897 			nbp->b_flags = obp->b_flags | B_CALL;
898 			nbp->b_iodone = cdbounce;
899 
900 			/* Put ptr to orig buf in b_private and use new buf */
901 			nbp->b_private = obp;
902 
903 			/*
904 			 * Place it in the queue of disk activities for this
905 			 * disk.
906 			 *
907 			 * XXX Only do disksort() if the current operating mode
908 			 * XXX does not include tagged queueing.
909 			 */
910 			disksort_blkno(&cd->buf_queue, nbp);
911 
912 			/*
913 			 * Tell the device to get going on the transfer if it's
914 			 * not doing anything, otherwise just wait for
915 			 * completion
916 			 */
917 			cdstart(cd->sc_periph);
918 
919 			splx(s);
920 			return;
921 
922 		}
923 	}
924 done:
925 	obp->b_flags |= (bp->b_flags&(B_EINTR|B_ERROR));
926 	obp->b_error = bp->b_error;
927 	obp->b_resid = bp->b_resid;
928 	free(bp->b_data, M_DEVBUF);
929 	biodone(obp);
930 }
931 
932 int cd_interpret_sense(xs)
933 	struct scsipi_xfer *xs;
934 {
935 	struct scsipi_periph *periph = xs->xs_periph;
936 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
937 	int retval = EJUSTRETURN;
938 
939 	/*
940 	 * If it isn't a extended or extended/deferred error, let
941 	 * the generic code handle it.
942 	 */
943 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
944 	    (sense->error_code & SSD_ERRCODE) != 0x71) {	/* DEFERRED */
945 		return (retval);
946 	}
947 
948 	/*
949 	 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit
950 	 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then
951 	 * wait a bit for the drive to spin up
952 	 */
953 
954 	if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
955 	    sense->add_sense_code == 0x4 &&
956 	    sense->add_sense_code_qual == 0x01)	{
957 		/*
958 		 * Sleep for 5 seconds to wait for the drive to spin up
959 		 */
960 
961 		SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD "
962 						"spinup\n"));
963 		if (!callout_active(&periph->periph_callout))
964 			scsipi_periph_freeze(periph, 1);
965 		callout_reset(&periph->periph_callout,
966 		    5 * hz, scsipi_periph_timed_thaw, periph);
967 		retval = ERESTART;
968 	}
969 	return (retval);
970 }
971 
972 void
973 cdminphys(bp)
974 	struct buf *bp;
975 {
976 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
977 	long max;
978 
979 	/*
980 	 * If the device is ancient, we want to make sure that
981 	 * the transfer fits into a 6-byte cdb.
982 	 *
983 	 * XXX Note that the SCSI-I spec says that 256-block transfers
984 	 * are allowed in a 6-byte read/write, and are specified
985 	 * by settng the "length" to 0.  However, we're conservative
986 	 * here, allowing only 255-block transfers in case an
987 	 * ancient device gets confused by length == 0.  A length of 0
988 	 * in a 10-byte read/write actually means 0 blocks.
989 	 */
990 	if (cd->flags & CDF_ANCIENT) {
991 		max = cd->sc_dk.dk_label->d_secsize * 0xff;
992 
993 		if (bp->b_bcount > max)
994 			bp->b_bcount = max;
995 	}
996 
997 	(*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp);
998 }
999 
1000 int
1001 cdread(dev, uio, ioflag)
1002 	dev_t dev;
1003 	struct uio *uio;
1004 	int ioflag;
1005 {
1006 
1007 	return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio));
1008 }
1009 
1010 int
1011 cdwrite(dev, uio, ioflag)
1012 	dev_t dev;
1013 	struct uio *uio;
1014 	int ioflag;
1015 {
1016 
1017 	return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio));
1018 }
1019 
1020 /*
1021  * conversion between minute-seconde-frame and logical block adress
1022  * adresses format
1023  */
1024 void
1025 lba2msf (lba, m, s, f)
1026 	u_long lba;
1027 	u_char *m, *s, *f;
1028 {
1029 	u_long tmp;
1030 
1031 	tmp = lba + CD_BLOCK_OFFSET;	/* offset of first logical frame */
1032 	tmp &= 0xffffff;		/* negative lbas use only 24 bits */
1033 	*m = tmp / (CD_SECS * CD_FRAMES);
1034 	tmp %= (CD_SECS * CD_FRAMES);
1035 	*s = tmp / CD_FRAMES;
1036 	*f = tmp % CD_FRAMES;
1037 }
1038 
1039 u_long
1040 msf2lba (m, s, f)
1041 	u_char m, s, f;
1042 {
1043 
1044 	return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET);
1045 }
1046 
1047 int
1048 cdreadmsaddr(cd, addr)
1049 	struct cd_softc *cd;
1050 	int *addr;
1051 {
1052 	struct scsipi_periph *periph = cd->sc_periph;
1053 	int error;
1054 	struct cd_toc toc;
1055 	struct cd_toc_entry *cte;
1056 
1057 	error = cd_read_toc(cd, 0, 0, &toc,
1058 	    sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
1059 	    XS_CTL_DATA_ONSTACK,
1060 	    0x40 /* control word for "get MS info" */);
1061 
1062 	if (error)
1063 		return (error);
1064 
1065 	cte = &toc.entries[0];
1066 	if (periph->periph_quirks & PQUIRK_LITTLETOC) {
1067 		cte->addr.lba = le32toh(cte->addr.lba);
1068 		toc.header.len = le16toh(toc.header.len);
1069 	} else {
1070 		cte->addr.lba = be32toh(cte->addr.lba);
1071 		toc.header.len = be16toh(toc.header.len);
1072 	}
1073 
1074 	*addr = (toc.header.len >= 10 && cte->track > 1) ?
1075 		cte->addr.lba : 0;
1076 	return 0;
1077 }
1078 
1079 /*
1080  * Perform special action on behalf of the user.
1081  * Knows about the internals of this device
1082  */
1083 int
1084 cdioctl(dev, cmd, addr, flag, p)
1085 	dev_t dev;
1086 	u_long cmd;
1087 	caddr_t addr;
1088 	int flag;
1089 	struct proc *p;
1090 {
1091 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
1092 	struct scsipi_periph *periph = cd->sc_periph;
1093 	int part = CDPART(dev);
1094 	int error;
1095 #ifdef __HAVE_OLD_DISKLABEL
1096 	struct disklabel newlabel;
1097 #endif
1098 
1099 	SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd));
1100 
1101 	/*
1102 	 * If the device is not valid, some IOCTLs can still be
1103 	 * handled on the raw partition. Check this here.
1104 	 */
1105 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1106 		switch (cmd) {
1107 		case DIOCWLABEL:
1108 		case DIOCLOCK:
1109 		case ODIOCEJECT:
1110 		case DIOCEJECT:
1111 		case SCIOCIDENTIFY:
1112 		case OSCIOCIDENTIFY:
1113 		case SCIOCCOMMAND:
1114 		case SCIOCDEBUG:
1115 		case CDIOCGETVOL:
1116 		case CDIOCSETVOL:
1117 		case CDIOCSETMONO:
1118 		case CDIOCSETSTEREO:
1119 		case CDIOCSETMUTE:
1120 		case CDIOCSETLEFT:
1121 		case CDIOCSETRIGHT:
1122 		case CDIOCCLOSE:
1123 		case CDIOCEJECT:
1124 		case CDIOCALLOW:
1125 		case CDIOCPREVENT:
1126 		case CDIOCSETDEBUG:
1127 		case CDIOCCLRDEBUG:
1128 		case CDIOCRESET:
1129 		case SCIOCRESET:
1130 		case CDIOCLOADUNLOAD:
1131 		case DVD_AUTH:
1132 		case DVD_READ_STRUCT:
1133 			if (part == RAW_PART)
1134 				break;
1135 		/* FALLTHROUGH */
1136 		default:
1137 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
1138 				return (ENODEV);
1139 			else
1140 				return (EIO);
1141 		}
1142 	}
1143 
1144 	switch (cmd) {
1145 	case DIOCGDINFO:
1146 		*(struct disklabel *)addr = *(cd->sc_dk.dk_label);
1147 		return (0);
1148 #ifdef __HAVE_OLD_DISKLABEL
1149 	case ODIOCGDINFO:
1150 		newlabel = *(cd->sc_dk.dk_label);
1151 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1152 			return ENOTTY;
1153 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1154 		return (0);
1155 #endif
1156 
1157 	case DIOCGPART:
1158 		((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label;
1159 		((struct partinfo *)addr)->part =
1160 		    &cd->sc_dk.dk_label->d_partitions[part];
1161 		return (0);
1162 
1163 	case DIOCWDINFO:
1164 	case DIOCSDINFO:
1165 #ifdef __HAVE_OLD_DISKLABEL
1166 	case ODIOCWDINFO:
1167 	case ODIOCSDINFO:
1168 #endif
1169 	{
1170 		struct disklabel *lp;
1171 
1172 #ifdef __HAVE_OLD_DISKLABEL
1173 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1174 			memset(&newlabel, 0, sizeof newlabel);
1175 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1176 			lp = &newlabel;
1177 		} else
1178 #endif
1179 		lp = (struct disklabel *)addr;
1180 
1181 		if ((flag & FWRITE) == 0)
1182 			return (EBADF);
1183 
1184 		if ((error = cdlock(cd)) != 0)
1185 			return (error);
1186 		cd->flags |= CDF_LABELLING;
1187 
1188 		error = setdisklabel(cd->sc_dk.dk_label,
1189 		    lp, /*cd->sc_dk.dk_openmask : */0,
1190 		    cd->sc_dk.dk_cpulabel);
1191 		if (error == 0) {
1192 			/* XXX ? */
1193 		}
1194 
1195 		cd->flags &= ~CDF_LABELLING;
1196 		cdunlock(cd);
1197 		return (error);
1198 	}
1199 
1200 	case DIOCWLABEL:
1201 		return (EBADF);
1202 
1203 	case DIOCGDEFLABEL:
1204 		cdgetdefaultlabel(cd, (struct disklabel *)addr);
1205 		return (0);
1206 
1207 #ifdef __HAVE_OLD_DISKLABEL
1208 	case ODIOCGDEFLABEL:
1209 		cdgetdefaultlabel(cd, &newlabel);
1210 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1211 			return ENOTTY;
1212 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1213 		return (0);
1214 #endif
1215 
1216 	case CDIOCPLAYTRACKS: {
1217 		struct ioc_play_track *args = (struct ioc_play_track *)addr;
1218 
1219 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
1220 			return (error);
1221 		return (cd_play_tracks(cd, args->start_track,
1222 		    args->start_index, args->end_track, args->end_index));
1223 	}
1224 	case CDIOCPLAYMSF: {
1225 		struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
1226 
1227 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
1228 			return (error);
1229 		return (cd_play_msf(cd, args->start_m, args->start_s,
1230 		    args->start_f, args->end_m, args->end_s, args->end_f));
1231 	}
1232 	case CDIOCPLAYBLOCKS: {
1233 		struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
1234 
1235 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
1236 			return (error);
1237 		return (cd_play(cd, args->blk, args->len));
1238 	}
1239 	case CDIOCREADSUBCHANNEL: {
1240 		struct ioc_read_subchannel *args =
1241 		    (struct ioc_read_subchannel *)addr;
1242 		struct cd_sub_channel_info data;
1243 		int len = args->data_len;
1244 
1245 		if (len > sizeof(data) ||
1246 		    len < sizeof(struct cd_sub_channel_header))
1247 			return (EINVAL);
1248 		error = cd_read_subchannel(cd, args->address_format,
1249 		    args->data_format, args->track, &data, len,
1250 		    XS_CTL_DATA_ONSTACK);
1251 		if (error)
1252 			return (error);
1253 		len = min(len, _2btol(data.header.data_len) +
1254 		    sizeof(struct cd_sub_channel_header));
1255 		return (copyout(&data, args->data, len));
1256 	}
1257 	case CDIOREADTOCHEADER: {
1258 		struct ioc_toc_header th;
1259 
1260 		if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th),
1261 		    XS_CTL_DATA_ONSTACK, 0)) != 0)
1262 			return (error);
1263 		if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC)
1264 			th.len = le16toh(th.len);
1265 		else
1266 			th.len = be16toh(th.len);
1267 		memcpy(addr, &th, sizeof(th));
1268 		return (0);
1269 	}
1270 	case CDIOREADTOCENTRYS: {
1271 		struct cd_toc toc;
1272 		struct ioc_read_toc_entry *te =
1273 		    (struct ioc_read_toc_entry *)addr;
1274 		struct ioc_toc_header *th;
1275 		struct cd_toc_entry *cte;
1276 		int len = te->data_len;
1277 		int ntracks;
1278 
1279 		th = &toc.header;
1280 
1281 		if (len > sizeof(toc.entries) ||
1282 		    len < sizeof(struct cd_toc_entry))
1283 			return (EINVAL);
1284 		error = cd_read_toc(cd, te->address_format, te->starting_track,
1285 		    &toc, len + sizeof(struct ioc_toc_header),
1286 		    XS_CTL_DATA_ONSTACK, 0);
1287 		if (error)
1288 			return (error);
1289 		if (te->address_format == CD_LBA_FORMAT)
1290 			for (ntracks =
1291 			    th->ending_track - th->starting_track + 1;
1292 			    ntracks >= 0; ntracks--) {
1293 				cte = &toc.entries[ntracks];
1294 				cte->addr_type = CD_LBA_FORMAT;
1295 				if (periph->periph_quirks & PQUIRK_LITTLETOC)
1296 					cte->addr.lba = le32toh(cte->addr.lba);
1297 				else
1298 					cte->addr.lba = be32toh(cte->addr.lba);
1299 			}
1300 		if (periph->periph_quirks & PQUIRK_LITTLETOC)
1301 			th->len = le16toh(th->len);
1302 		else
1303 			th->len = be16toh(th->len);
1304 		len = min(len, th->len - (sizeof(th->starting_track) +
1305 		    sizeof(th->ending_track)));
1306 		return (copyout(toc.entries, te->data, len));
1307 	}
1308 	case CDIOREADMSADDR: {
1309 		int sessno = *(int*)addr;
1310 
1311 		if (sessno != 0)
1312 			return (EINVAL);
1313 
1314 		return (cdreadmsaddr(cd, (int*)addr));
1315 	}
1316 	case CDIOCSETPATCH: {
1317 		struct ioc_patch *arg = (struct ioc_patch *)addr;
1318 
1319 		return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0],
1320 		    arg->patch[1], arg->patch[2], arg->patch[3], 0));
1321 	}
1322 	case CDIOCGETVOL: {
1323 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1324 
1325 		return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0));
1326 	}
1327 	case CDIOCSETVOL: {
1328 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1329 
1330 		return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0));
1331 	}
1332 
1333 	case CDIOCSETMONO:
1334 		return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL,
1335 		    BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1336 
1337 	case CDIOCSETSTEREO:
1338 		return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL,
1339 		    RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1340 
1341 	case CDIOCSETMUTE:
1342 		return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL,
1343 		    MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1344 
1345 	case CDIOCSETLEFT:
1346 		return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL,
1347 		    LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1348 
1349 	case CDIOCSETRIGHT:
1350 		return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL,
1351 		    RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1352 
1353 	case CDIOCRESUME:
1354 		return (cd_pause(cd, PA_RESUME));
1355 	case CDIOCPAUSE:
1356 		return (cd_pause(cd, PA_PAUSE));
1357 	case CDIOCSTART:
1358 		return (scsipi_start(periph, SSS_START, 0));
1359 	case CDIOCSTOP:
1360 		return (scsipi_start(periph, SSS_STOP, 0));
1361 	case CDIOCCLOSE:
1362 		return (scsipi_start(periph, SSS_START|SSS_LOEJ,
1363 		    XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE));
1364 	case DIOCEJECT:
1365 		if (*(int *)addr == 0) {
1366 			/*
1367 			 * Don't force eject: check that we are the only
1368 			 * partition open. If so, unlock it.
1369 			 */
1370 			if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1371 			    cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask ==
1372 			    cd->sc_dk.dk_openmask) {
1373 				error = scsipi_prevent(periph, PR_ALLOW,
1374 				    XS_CTL_IGNORE_NOT_READY);
1375 				if (error)
1376 					return (error);
1377 			} else {
1378 				return (EBUSY);
1379 			}
1380 		}
1381 		/* FALLTHROUGH */
1382 	case CDIOCEJECT: /* FALLTHROUGH */
1383 	case ODIOCEJECT:
1384 		return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1385 	case CDIOCALLOW:
1386 		return (scsipi_prevent(periph, PR_ALLOW, 0));
1387 	case CDIOCPREVENT:
1388 		return (scsipi_prevent(periph, PR_PREVENT, 0));
1389 	case DIOCLOCK:
1390 		return (scsipi_prevent(periph,
1391 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1392 	case CDIOCSETDEBUG:
1393 		cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2);
1394 		return (0);
1395 	case CDIOCCLRDEBUG:
1396 		cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2);
1397 		return (0);
1398 	case CDIOCRESET:
1399 	case SCIOCRESET:
1400 		return (cd_reset(cd));
1401 	case CDIOCLOADUNLOAD: {
1402 		struct ioc_load_unload *args = (struct ioc_load_unload *)addr;
1403 
1404 		return ((*cd->sc_ops->cdo_load_unload)(cd, args->options,
1405 			args->slot));
1406 	case DVD_AUTH:
1407 		return (dvd_auth(cd, (dvd_authinfo *)addr));
1408 	case DVD_READ_STRUCT:
1409 		return (dvd_read_struct(cd, (dvd_struct *)addr));
1410 	}
1411 
1412 	default:
1413 		if (part != RAW_PART)
1414 			return (ENOTTY);
1415 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
1416 	}
1417 
1418 #ifdef DIAGNOSTIC
1419 	panic("cdioctl: impossible");
1420 #endif
1421 }
1422 
1423 void
1424 cdgetdefaultlabel(cd, lp)
1425 	struct cd_softc *cd;
1426 	struct disklabel *lp;
1427 {
1428 	int lastsession;
1429 
1430 	memset(lp, 0, sizeof(struct disklabel));
1431 
1432 	lp->d_secsize = cd->params.blksize;
1433 	lp->d_ntracks = 1;
1434 	lp->d_nsectors = 100;
1435 	lp->d_ncylinders = (cd->params.disksize / 100) + 1;
1436 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1437 
1438 	switch (scsipi_periph_bustype(cd->sc_periph)) {
1439 #if NCD_SCSIBUS > 0
1440 	case SCSIPI_BUSTYPE_SCSI:
1441 		lp->d_type = DTYPE_SCSI;
1442 		break;
1443 #endif
1444 #if NCD_ATAPIBUS > 0
1445 	case SCSIPI_BUSTYPE_ATAPI:
1446 		lp->d_type = DTYPE_ATAPI;
1447 		break;
1448 #endif
1449 	}
1450 	strncpy(lp->d_typename, cd->name, 16);
1451 	strncpy(lp->d_packname, "fictitious", 16);
1452 	lp->d_secperunit = cd->params.disksize;
1453 	lp->d_rpm = 300;
1454 	lp->d_interleave = 1;
1455 	lp->d_flags = D_REMOVABLE;
1456 
1457 	if (cdreadmsaddr(cd, &lastsession) != 0)
1458 		lastsession = 0;
1459 
1460 	lp->d_partitions[0].p_offset = 0;
1461 #ifdef notyet /* have to fix bounds_check_with_label() first */
1462 	lp->d_partitions[0].p_size = lp->d_secperunit;
1463 #else
1464 	lp->d_partitions[0].p_size =
1465 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1466 #endif
1467 	lp->d_partitions[0].p_cdsession = lastsession;
1468 	lp->d_partitions[0].p_fstype = FS_ISO9660;
1469 	lp->d_partitions[RAW_PART].p_offset = 0;
1470 #ifdef notyet
1471 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1472 #else
1473 	lp->d_partitions[RAW_PART].p_size =
1474 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1475 #endif
1476 	lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
1477 	lp->d_npartitions = RAW_PART + 1;
1478 
1479 	lp->d_magic = DISKMAGIC;
1480 	lp->d_magic2 = DISKMAGIC;
1481 	lp->d_checksum = dkcksum(lp);
1482 }
1483 
1484 /*
1485  * Load the label information on the named device
1486  * Actually fabricate a disklabel
1487  *
1488  * EVENTUALLY take information about different
1489  * data tracks from the TOC and put it in the disklabel
1490  */
1491 void
1492 cdgetdisklabel(cd)
1493 	struct cd_softc *cd;
1494 {
1495 	struct disklabel *lp = cd->sc_dk.dk_label;
1496 	char *errstring;
1497 
1498 	memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1499 
1500 	cdgetdefaultlabel(cd, lp);
1501 
1502 	/*
1503 	 * Call the generic disklabel extraction routine
1504 	 */
1505 	errstring = readdisklabel(MAKECDDEV(0, cd->sc_dev.dv_unit, RAW_PART),
1506 	    cdstrategy, lp, cd->sc_dk.dk_cpulabel);
1507 	if (errstring) {
1508 		printf("%s: %s\n", cd->sc_dev.dv_xname, errstring);
1509 		goto error;
1510 	}
1511 	return;
1512 
1513 error:
1514 	/* Reset to default label -- should print a warning */
1515 	memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1516 
1517 	cdgetdefaultlabel(cd, lp);
1518 }
1519 
1520 /*
1521  * Find out from the device what it's capacity is
1522  */
1523 u_long
1524 cd_size(cd, flags)
1525 	struct cd_softc *cd;
1526 	int flags;
1527 {
1528 	struct scsipi_read_cd_cap_data rdcap;
1529 	struct scsipi_read_cd_capacity scsipi_cmd;
1530 	int blksize;
1531 	u_long size;
1532 
1533 	if (cd->sc_periph->periph_quirks & PQUIRK_NOCAPACITY) {
1534 		/*
1535 		 * the drive doesn't support the READ_CD_CAPACITY command
1536 		 * use a fake size
1537 		 */
1538 		cd->params.blksize = 2048;
1539 		cd->params.disksize = 400000;
1540 		return (400000);
1541 	}
1542 
1543 	/*
1544 	 * make up a scsi command and ask the scsi driver to do
1545 	 * it for you.
1546 	 */
1547 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1548 	scsipi_cmd.opcode = READ_CD_CAPACITY;
1549 
1550 	/*
1551 	 * If the command works, interpret the result as a 4 byte
1552 	 * number of blocks and a blocksize
1553 	 */
1554 	if (scsipi_command(cd->sc_periph,
1555 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1556 	    (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL,
1557 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_IN) != 0)
1558 		return (0);
1559 
1560 	blksize = _4btol(rdcap.length);
1561 	if ((blksize < 512) || ((blksize & 511) != 0))
1562 		blksize = 2048;	/* some drives lie ! */
1563 	cd->params.blksize = blksize;
1564 
1565 	size = _4btol(rdcap.addr) + 1;
1566 	if (size < 100)
1567 		size = 400000;	/* ditto */
1568 	cd->params.disksize = size;
1569 
1570 	SC_DEBUG(cd->sc_periph, SCSIPI_DB2,
1571 	    ("cd_size: %d %ld\n", blksize, size));
1572 	return (size);
1573 }
1574 
1575 /*
1576  * Get scsi driver to send a "start playing" command
1577  */
1578 int
1579 cd_play(cd, blkno, nblks)
1580 	struct cd_softc *cd;
1581 	int blkno, nblks;
1582 {
1583 	struct scsipi_play scsipi_cmd;
1584 
1585 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1586 	scsipi_cmd.opcode = PLAY;
1587 	_lto4b(blkno, scsipi_cmd.blk_addr);
1588 	_lto2b(nblks, scsipi_cmd.xfer_len);
1589 	return (scsipi_command(cd->sc_periph,
1590 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1591 	    0, 0, CDRETRIES, 30000, NULL, 0));
1592 }
1593 
1594 /*
1595  * Get scsi driver to send a "start playing" command
1596  */
1597 int
1598 cd_play_tracks(cd, strack, sindex, etrack, eindex)
1599 	struct cd_softc *cd;
1600 	int strack, sindex, etrack, eindex;
1601 {
1602 	struct cd_toc toc;
1603 	int error;
1604 
1605 	if (!etrack)
1606 		return (EIO);
1607 	if (strack > etrack)
1608 		return (EINVAL);
1609 
1610 	if ((error = cd_load_toc(cd, &toc, XS_CTL_DATA_ONSTACK)) != 0)
1611 		return (error);
1612 
1613 	if (++etrack > (toc.header.ending_track+1))
1614 		etrack = toc.header.ending_track+1;
1615 
1616 	strack -= toc.header.starting_track;
1617 	etrack -= toc.header.starting_track;
1618 	if (strack < 0)
1619 		return (EINVAL);
1620 
1621 	return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute,
1622 	    toc.entries[strack].addr.msf.second,
1623 	    toc.entries[strack].addr.msf.frame,
1624 	    toc.entries[etrack].addr.msf.minute,
1625 	    toc.entries[etrack].addr.msf.second,
1626 	    toc.entries[etrack].addr.msf.frame));
1627 }
1628 
1629 /*
1630  * Get scsi driver to send a "play msf" command
1631  */
1632 int
1633 cd_play_msf(cd, startm, starts, startf, endm, ends, endf)
1634 	struct cd_softc *cd;
1635 	int startm, starts, startf, endm, ends, endf;
1636 {
1637 	struct scsipi_play_msf scsipi_cmd;
1638 
1639 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1640 	scsipi_cmd.opcode = PLAY_MSF;
1641 	scsipi_cmd.start_m = startm;
1642 	scsipi_cmd.start_s = starts;
1643 	scsipi_cmd.start_f = startf;
1644 	scsipi_cmd.end_m = endm;
1645 	scsipi_cmd.end_s = ends;
1646 	scsipi_cmd.end_f = endf;
1647 	return (scsipi_command(cd->sc_periph,
1648 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1649 	    0, 0, CDRETRIES, 30000, NULL, 0));
1650 }
1651 
1652 /*
1653  * Get scsi driver to send a "start up" command
1654  */
1655 int
1656 cd_pause(cd, go)
1657 	struct cd_softc *cd;
1658 	int go;
1659 {
1660 	struct scsipi_pause scsipi_cmd;
1661 
1662 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1663 	scsipi_cmd.opcode = PAUSE;
1664 	scsipi_cmd.resume = go & 0xff;
1665 	return (scsipi_command(cd->sc_periph,
1666 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1667 	    0, 0, CDRETRIES, 30000, NULL, 0));
1668 }
1669 
1670 /*
1671  * Get scsi driver to send a "RESET" command
1672  */
1673 int
1674 cd_reset(cd)
1675 	struct cd_softc *cd;
1676 {
1677 
1678 	return (scsipi_command(cd->sc_periph, 0, 0, 0, 0,
1679 	    CDRETRIES, 30000, NULL, XS_CTL_RESET));
1680 }
1681 
1682 /*
1683  * Read subchannel
1684  */
1685 int
1686 cd_read_subchannel(cd, mode, format, track, data, len, flags)
1687 	struct cd_softc *cd;
1688 	int mode, format, track, len;
1689 	struct cd_sub_channel_info *data;
1690 	int flags;
1691 {
1692 	struct scsipi_read_subchannel scsipi_cmd;
1693 
1694 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1695 	scsipi_cmd.opcode = READ_SUBCHANNEL;
1696 	if (mode == CD_MSF_FORMAT)
1697 		scsipi_cmd.byte2 |= CD_MSF;
1698 	scsipi_cmd.byte3 = SRS_SUBQ;
1699 	scsipi_cmd.subchan_format = format;
1700 	scsipi_cmd.track = track;
1701 	_lto2b(len, scsipi_cmd.data_len);
1702 	return (scsipi_command(cd->sc_periph,
1703 	    (struct scsipi_generic *)&scsipi_cmd,
1704 	    sizeof(struct scsipi_read_subchannel), (u_char *)data, len,
1705 	    CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT));
1706 }
1707 
1708 /*
1709  * Read table of contents
1710  */
1711 int
1712 cd_read_toc(cd, mode, start, data, len, flags, control)
1713 	struct cd_softc *cd;
1714 	int mode, start, len, control;
1715 	void *data;
1716 	int flags;
1717 {
1718 	struct scsipi_read_toc scsipi_cmd;
1719 	int ntoc;
1720 
1721 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1722 #if 0
1723 	if (len != sizeof(struct ioc_toc_header))
1724 		ntoc = ((len) - sizeof(struct ioc_toc_header)) /
1725 		    sizeof(struct cd_toc_entry);
1726 	else
1727 #endif
1728 	ntoc = len;
1729 	scsipi_cmd.opcode = READ_TOC;
1730 	if (mode == CD_MSF_FORMAT)
1731 		scsipi_cmd.byte2 |= CD_MSF;
1732 	scsipi_cmd.from_track = start;
1733 	_lto2b(ntoc, scsipi_cmd.data_len);
1734 	scsipi_cmd.control = control;
1735 	return (scsipi_command(cd->sc_periph,
1736 	    (struct scsipi_generic *)&scsipi_cmd,
1737 	    sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES,
1738 	    30000, NULL, flags | XS_CTL_DATA_IN));
1739 }
1740 
1741 int
1742 cd_load_toc(cd, toc, flags)
1743 	struct cd_softc *cd;
1744 	struct cd_toc *toc;
1745 	int flags;
1746 {
1747 	int ntracks, len, error;
1748 
1749 	if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header),
1750 	    flags, 0)) != 0)
1751 		return (error);
1752 
1753 	ntracks = toc->header.ending_track - toc->header.starting_track + 1;
1754 	len = (ntracks + 1) * sizeof(struct cd_toc_entry) +
1755 	    sizeof(toc->header);
1756 	if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len,
1757 	    flags, 0)) != 0)
1758 		return (error);
1759 	return (0);
1760 }
1761 
1762 /*
1763  * Get the scsi driver to send a full inquiry to the device and use the
1764  * results to fill out the disk parameter structure.
1765  */
1766 int
1767 cd_get_parms(cd, flags)
1768 	struct cd_softc *cd;
1769 	int flags;
1770 {
1771 
1772 	/*
1773 	 * give a number of sectors so that sec * trks * cyls
1774 	 * is <= disk_size
1775 	 */
1776 	if (cd_size(cd, flags) == 0)
1777 		return (ENXIO);
1778 	return (0);
1779 }
1780 
1781 int
1782 cdsize(dev)
1783 	dev_t dev;
1784 {
1785 
1786 	/* CD-ROMs are read-only. */
1787 	return (-1);
1788 }
1789 
1790 int
1791 cddump(dev, blkno, va, size)
1792 	dev_t dev;
1793 	daddr_t blkno;
1794 	caddr_t va;
1795 	size_t size;
1796 {
1797 
1798 	/* Not implemented. */
1799 	return (ENXIO);
1800 }
1801 
1802 #define	dvd_copy_key(dst, src)		memcpy((dst), (src), sizeof(dvd_key))
1803 #define	dvd_copy_challenge(dst, src)	memcpy((dst), (src), sizeof(dvd_challenge))
1804 
1805 int
1806 dvd_auth(cd, a)
1807 	struct cd_softc *cd;
1808 	dvd_authinfo *a;
1809 {
1810 	struct scsipi_generic cmd;
1811 	u_int8_t buf[20];
1812 	int error;
1813 
1814 	memset(cmd.bytes, 0, 15);
1815 	memset(buf, 0, sizeof(buf));
1816 
1817 	switch (a->type) {
1818 	case DVD_LU_SEND_AGID:
1819 		cmd.opcode = GPCMD_REPORT_KEY;
1820 		cmd.bytes[8] = 8;
1821 		cmd.bytes[9] = 0 | (0 << 6);
1822 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8,
1823 		    CDRETRIES, 30000, NULL,
1824 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1825 		if (error)
1826 			return (error);
1827 		a->lsa.agid = buf[7] >> 6;
1828 		return (0);
1829 
1830 	case DVD_LU_SEND_CHALLENGE:
1831 		cmd.opcode = GPCMD_REPORT_KEY;
1832 		cmd.bytes[8] = 16;
1833 		cmd.bytes[9] = 1 | (a->lsc.agid << 6);
1834 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16,
1835 		    CDRETRIES, 30000, NULL,
1836 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1837 		if (error)
1838 			return (error);
1839 		dvd_copy_challenge(a->lsc.chal, &buf[4]);
1840 		return (0);
1841 
1842 	case DVD_LU_SEND_KEY1:
1843 		cmd.opcode = GPCMD_REPORT_KEY;
1844 		cmd.bytes[8] = 12;
1845 		cmd.bytes[9] = 2 | (a->lsk.agid << 6);
1846 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12,
1847 		    CDRETRIES, 30000, NULL,
1848 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1849 		if (error)
1850 			return (error);
1851 		dvd_copy_key(a->lsk.key, &buf[4]);
1852 		return (0);
1853 
1854 	case DVD_LU_SEND_TITLE_KEY:
1855 		cmd.opcode = GPCMD_REPORT_KEY;
1856 		_lto4b(a->lstk.lba, &cmd.bytes[1]);
1857 		cmd.bytes[8] = 12;
1858 		cmd.bytes[9] = 4 | (a->lstk.agid << 6);
1859 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12,
1860 		    CDRETRIES, 30000, NULL,
1861 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1862 		if (error)
1863 			return (error);
1864 		a->lstk.cpm = (buf[4] >> 7) & 1;
1865 		a->lstk.cp_sec = (buf[4] >> 6) & 1;
1866 		a->lstk.cgms = (buf[4] >> 4) & 3;
1867 		dvd_copy_key(a->lstk.title_key, &buf[5]);
1868 		return (0);
1869 
1870 	case DVD_LU_SEND_ASF:
1871 		cmd.opcode = GPCMD_REPORT_KEY;
1872 		cmd.bytes[8] = 8;
1873 		cmd.bytes[9] = 5 | (a->lsasf.agid << 6);
1874 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8,
1875 		    CDRETRIES, 30000, NULL,
1876 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1877 		if (error)
1878 			return (error);
1879 		a->lsasf.asf = buf[7] & 1;
1880 		return (0);
1881 
1882 	case DVD_HOST_SEND_CHALLENGE:
1883 		cmd.opcode = GPCMD_SEND_KEY;
1884 		cmd.bytes[8] = 16;
1885 		cmd.bytes[9] = 1 | (a->hsc.agid << 6);
1886 		buf[1] = 14;
1887 		dvd_copy_challenge(&buf[4], a->hsc.chal);
1888 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16,
1889 		    CDRETRIES, 30000, NULL,
1890 		    XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK);
1891 		if (error)
1892 			return (error);
1893 		a->type = DVD_LU_SEND_KEY1;
1894 		return (0);
1895 
1896 	case DVD_HOST_SEND_KEY2:
1897 		cmd.opcode = GPCMD_SEND_KEY;
1898 		cmd.bytes[8] = 12;
1899 		cmd.bytes[9] = 3 | (a->hsk.agid << 6);
1900 		buf[1] = 10;
1901 		dvd_copy_key(&buf[4], a->hsk.key);
1902 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12,
1903 		    CDRETRIES, 30000, NULL,
1904 		    XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK);
1905 		if (error) {
1906 			a->type = DVD_AUTH_FAILURE;
1907 			return (error);
1908 		}
1909 		a->type = DVD_AUTH_ESTABLISHED;
1910 		return (0);
1911 
1912 	case DVD_INVALIDATE_AGID:
1913 		cmd.opcode = GPCMD_REPORT_KEY;
1914 		cmd.bytes[9] = 0x3f | (a->lsa.agid << 6);
1915 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16,
1916 		    CDRETRIES, 30000, NULL, 0);
1917 		if (error)
1918 			return (error);
1919 		return (0);
1920 
1921 	case DVD_LU_SEND_RPC_STATE:
1922 		cmd.opcode = GPCMD_REPORT_KEY;
1923 		cmd.bytes[8] = 8;
1924 		cmd.bytes[9] = 8 | (0 << 6);
1925 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8,
1926 		    CDRETRIES, 30000, NULL,
1927 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1928 		if (error)
1929 			return (error);
1930 		a->lrpcs.type = (buf[8] >> 6) & 3;
1931 		a->lrpcs.vra = (buf[8] >> 3) & 7;
1932 		a->lrpcs.ucca = (buf[8]) & 7;
1933 		a->lrpcs.region_mask = buf[9];
1934 		a->lrpcs.rpc_scheme = buf[10];
1935 		return (0);
1936 
1937 	case DVD_HOST_SEND_RPC_STATE:
1938 		cmd.opcode = GPCMD_SEND_KEY;
1939 		cmd.bytes[8] = 8;
1940 		cmd.bytes[9] = 6 | (0 << 6);
1941 		buf[1] = 6;
1942 		buf[4] = a->hrpcs.pdrc;
1943 		error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8,
1944 		    CDRETRIES, 30000, NULL,
1945 		    XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK);
1946 		if (error)
1947 			return (error);
1948 		return (0);
1949 
1950 	default:
1951 		return (ENOTTY);
1952 	}
1953 }
1954 
1955 int
1956 dvd_read_physical(cd, s)
1957 	struct cd_softc *cd;
1958 	dvd_struct *s;
1959 {
1960 	struct scsipi_generic cmd;
1961 	u_int8_t buf[4 + 4 * 20], *bufp;
1962 	int error;
1963 	struct dvd_layer *layer;
1964 	int i;
1965 
1966 	memset(cmd.bytes, 0, 15);
1967 	memset(buf, 0, sizeof(buf));
1968 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1969 	cmd.bytes[6] = s->type;
1970 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1971 
1972 	cmd.bytes[5] = s->physical.layer_num;
1973 	error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf),
1974 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1975 	if (error)
1976 		return (error);
1977 	for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4;
1978 	     i++, bufp += 20, layer++) {
1979 		memset(layer, 0, sizeof(*layer));
1980                 layer->book_version = bufp[0] & 0xf;
1981                 layer->book_type = bufp[0] >> 4;
1982                 layer->min_rate = bufp[1] & 0xf;
1983                 layer->disc_size = bufp[1] >> 4;
1984                 layer->layer_type = bufp[2] & 0xf;
1985                 layer->track_path = (bufp[2] >> 4) & 1;
1986                 layer->nlayers = (bufp[2] >> 5) & 3;
1987                 layer->track_density = bufp[3] & 0xf;
1988                 layer->linear_density = bufp[3] >> 4;
1989                 layer->start_sector = _4btol(&bufp[4]);
1990                 layer->end_sector = _4btol(&bufp[8]);
1991                 layer->end_sector_l0 = _4btol(&bufp[12]);
1992                 layer->bca = bufp[16] >> 7;
1993 	}
1994 	return (0);
1995 }
1996 
1997 int
1998 dvd_read_copyright(cd, s)
1999 	struct cd_softc *cd;
2000 	dvd_struct *s;
2001 {
2002 	struct scsipi_generic cmd;
2003 	u_int8_t buf[8];
2004 	int error;
2005 
2006 	memset(cmd.bytes, 0, 15);
2007 	memset(buf, 0, sizeof(buf));
2008 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2009 	cmd.bytes[6] = s->type;
2010 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2011 
2012 	cmd.bytes[5] = s->copyright.layer_num;
2013 	error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf),
2014 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2015 	if (error)
2016 		return (error);
2017 	s->copyright.cpst = buf[4];
2018 	s->copyright.rmi = buf[5];
2019 	return (0);
2020 }
2021 
2022 int
2023 dvd_read_disckey(cd, s)
2024 	struct cd_softc *cd;
2025 	dvd_struct *s;
2026 {
2027 	struct scsipi_generic cmd;
2028 	u_int8_t buf[4 + 2048];
2029 	int error;
2030 
2031 	memset(cmd.bytes, 0, 15);
2032 	memset(buf, 0, sizeof(buf));
2033 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2034 	cmd.bytes[6] = s->type;
2035 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2036 
2037 	cmd.bytes[9] = s->disckey.agid << 6;
2038 	error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf),
2039 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2040 	if (error)
2041 		return (error);
2042 	memcpy(s->disckey.value, &buf[4], 2048);
2043 	return (0);
2044 }
2045 
2046 int
2047 dvd_read_bca(cd, s)
2048 	struct cd_softc *cd;
2049 	dvd_struct *s;
2050 {
2051 	struct scsipi_generic cmd;
2052 	u_int8_t buf[4 + 188];
2053 	int error;
2054 
2055 	memset(cmd.bytes, 0, 15);
2056 	memset(buf, 0, sizeof(buf));
2057 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2058 	cmd.bytes[6] = s->type;
2059 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2060 
2061 	error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf),
2062 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2063 	if (error)
2064 		return (error);
2065 	s->bca.len = _2btol(&buf[0]);
2066 	if (s->bca.len < 12 || s->bca.len > 188)
2067 		return (EIO);
2068 	memcpy(s->bca.value, &buf[4], s->bca.len);
2069 	return (0);
2070 }
2071 
2072 int
2073 dvd_read_manufact(cd, s)
2074 	struct cd_softc *cd;
2075 	dvd_struct *s;
2076 {
2077 	struct scsipi_generic cmd;
2078 	u_int8_t buf[4 + 2048];
2079 	int error;
2080 
2081 	memset(cmd.bytes, 0, 15);
2082 	memset(buf, 0, sizeof(buf));
2083 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2084 	cmd.bytes[6] = s->type;
2085 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2086 
2087 	error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf),
2088 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2089 	if (error)
2090 		return (error);
2091 	s->manufact.len = _2btol(&buf[0]);
2092 	if (s->manufact.len < 0 || s->manufact.len > 2048)
2093 		return (EIO);
2094 	memcpy(s->manufact.value, &buf[4], s->manufact.len);
2095 	return (0);
2096 }
2097 
2098 int
2099 dvd_read_struct(cd, s)
2100 	struct cd_softc *cd;
2101 	dvd_struct *s;
2102 {
2103 
2104 	switch (s->type) {
2105 	case DVD_STRUCT_PHYSICAL:
2106 		return (dvd_read_physical(cd, s));
2107 	case DVD_STRUCT_COPYRIGHT:
2108 		return (dvd_read_copyright(cd, s));
2109 	case DVD_STRUCT_DISCKEY:
2110 		return (dvd_read_disckey(cd, s));
2111 	case DVD_STRUCT_BCA:
2112 		return (dvd_read_bca(cd, s));
2113 	case DVD_STRUCT_MANUFACT:
2114 		return (dvd_read_manufact(cd, s));
2115 	default:
2116 		return (EINVAL);
2117 	}
2118 }
2119