xref: /netbsd-src/sys/dev/scsipi/cd.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: cd.c,v 1.205 2004/09/09 19:35:30 bouyer Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2001, 2003 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.205 2004/09/09 19:35:30 bouyer 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 #include <dev/scsipi/scsi_all.h>
88 					/* from there */
89 #include <dev/scsipi/scsi_disk.h>	/* rw comes from there */
90 #include <dev/scsipi/scsipiconf.h>
91 #include <dev/scsipi/scsipi_base.h>
92 #include <dev/scsipi/cdvar.h>
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 static int	cdlock(struct cd_softc *);
111 static void	cdunlock(struct cd_softc *);
112 static void	cdstart(struct scsipi_periph *);
113 static void	cdrestart(void *);
114 static void	cdminphys(struct buf *);
115 static void	cdgetdefaultlabel(struct cd_softc *, struct disklabel *);
116 static void	cdgetdisklabel(struct cd_softc *);
117 static void	cddone(struct scsipi_xfer *);
118 static void	cdbounce(struct buf *);
119 static int	cd_interpret_sense(struct scsipi_xfer *);
120 static u_long	cd_size(struct cd_softc *, int);
121 static int	cd_play(struct cd_softc *, int, int);
122 static int	cd_play_tracks(struct cd_softc *, int, int, int, int);
123 static int	cd_play_msf(struct cd_softc *, int, int, int, int, int, int);
124 static int	cd_pause(struct cd_softc *, int);
125 static int	cd_reset(struct cd_softc *);
126 static int	cd_read_subchannel(struct cd_softc *, int, int, int,
127 		    struct cd_sub_channel_info *, int, int);
128 static int	cd_read_toc(struct cd_softc *, int, int, void *, int, int, int);
129 static int	cd_get_parms(struct cd_softc *, int);
130 static int	cd_load_toc(struct cd_softc *, struct cd_toc *, int);
131 static int	cdreadmsaddr(struct cd_softc *, int *);
132 
133 static int	dvd_auth(struct cd_softc *, dvd_authinfo *);
134 static int	dvd_read_physical(struct cd_softc *, dvd_struct *);
135 static int	dvd_read_copyright(struct cd_softc *, dvd_struct *);
136 static int	dvd_read_disckey(struct cd_softc *, dvd_struct *);
137 static int	dvd_read_bca(struct cd_softc *, dvd_struct *);
138 static int	dvd_read_manufact(struct cd_softc *, dvd_struct *);
139 static int	dvd_read_struct(struct cd_softc *, dvd_struct *);
140 
141 static int	cd_mode_sense(struct cd_softc *, u_int8_t, void *, size_t, int,
142 		    int, int *);
143 static int	cd_mode_select(struct cd_softc *, u_int8_t, void *, size_t,
144 		    int, int);
145 static int	cd_setchan(struct cd_softc *, int, int, int, int, int);
146 static int	cd_getvol(struct cd_softc *, struct ioc_vol *, int);
147 static int	cd_setvol(struct cd_softc *, const struct ioc_vol *, int);
148 static int	cd_set_pa_immed(struct cd_softc *, int);
149 static int	cd_load_unload(struct cd_softc *, struct ioc_load_unload *);
150 static int	cd_setblksize(struct cd_softc *);
151 
152 static int	cdmatch(struct device *, struct cfdata *, void *);
153 static void	cdattach(struct device *, struct device *, void *);
154 static int	cdactivate(struct device *, enum devact);
155 static int	cddetach(struct device *, int);
156 
157 CFATTACH_DECL(cd, sizeof(struct cd_softc), cdmatch, cdattach, cddetach,
158     cdactivate);
159 
160 extern struct cfdriver cd_cd;
161 
162 static const struct scsipi_inquiry_pattern cd_patterns[] = {
163 	{T_CDROM, T_REMOV,
164 	 "",         "",                 ""},
165 	{T_WORM, T_REMOV,
166 	 "",         "",                 ""},
167 #if 0
168 	{T_CDROM, T_REMOV, /* more luns */
169 	 "PIONEER ", "CD-ROM DRM-600  ", ""},
170 #endif
171 	{T_DIRECT, T_REMOV,
172 	 "NEC                 CD-ROM DRIVE:260", "", ""},
173 };
174 
175 static dev_type_open(cdopen);
176 static dev_type_close(cdclose);
177 static dev_type_read(cdread);
178 static dev_type_write(cdwrite);
179 static dev_type_ioctl(cdioctl);
180 static dev_type_strategy(cdstrategy);
181 static dev_type_dump(cddump);
182 static dev_type_size(cdsize);
183 
184 const struct bdevsw cd_bdevsw = {
185 	cdopen, cdclose, cdstrategy, cdioctl, cddump, cdsize, D_DISK
186 };
187 
188 const struct cdevsw cd_cdevsw = {
189 	cdopen, cdclose, cdread, cdwrite, cdioctl,
190 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
191 };
192 
193 static struct dkdriver cddkdriver = { cdstrategy };
194 
195 static const struct scsipi_periphsw cd_switch = {
196 	cd_interpret_sense,	/* use our error handler first */
197 	cdstart,		/* we have a queue, which is started by this */
198 	NULL,			/* we do not have an async handler */
199 	cddone,			/* deal with stats at interrupt time */
200 };
201 
202 /*
203  * The routine called by the low level scsi routine when it discovers
204  * A device suitable for this driver
205  */
206 static int
207 cdmatch(struct device *parent, struct cfdata *match, void *aux)
208 {
209 	struct scsipibus_attach_args *sa = aux;
210 	int priority;
211 
212 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
213 	    (caddr_t)cd_patterns, sizeof(cd_patterns) / sizeof(cd_patterns[0]),
214 	    sizeof(cd_patterns[0]), &priority);
215 
216 	return (priority);
217 }
218 
219 static void
220 cdattach(struct device *parent, struct device *self, void *aux)
221 {
222 	struct cd_softc *cd = (void *)self;
223 	struct scsipibus_attach_args *sa = aux;
224 	struct scsipi_periph *periph = sa->sa_periph;
225 
226 	SC_DEBUG(periph, SCSIPI_DB2, ("cdattach: "));
227 
228 	if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI &&
229 	    periph->periph_version == 0)
230 		cd->flags |= CDF_ANCIENT;
231 
232 	bufq_alloc(&cd->buf_queue, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
233 
234 	callout_init(&cd->sc_callout);
235 
236 	/*
237 	 * Store information needed to contact our base driver
238 	 */
239 	cd->sc_periph = periph;
240 
241 	periph->periph_dev = &cd->sc_dev;
242 	periph->periph_switch = &cd_switch;
243 
244 	/*
245 	 * Increase our openings to the maximum-per-periph
246 	 * supported by the adapter.  This will either be
247 	 * clamped down or grown by the adapter if necessary.
248 	 */
249 	periph->periph_openings =
250 	    SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
251 	periph->periph_flags |= PERIPH_GROW_OPENINGS;
252 
253 	/*
254 	 * Initialize and attach the disk structure.
255 	 */
256   	cd->sc_dk.dk_driver = &cddkdriver;
257 	cd->sc_dk.dk_name = cd->sc_dev.dv_xname;
258 	disk_attach(&cd->sc_dk);
259 
260 	printf("\n");
261 
262 #if NRND > 0
263 	rnd_attach_source(&cd->rnd_source, cd->sc_dev.dv_xname,
264 			  RND_TYPE_DISK, 0);
265 #endif
266 }
267 
268 static int
269 cdactivate(struct device *self, enum devact act)
270 {
271 	int rv = 0;
272 
273 	switch (act) {
274 	case DVACT_ACTIVATE:
275 		rv = EOPNOTSUPP;
276 		break;
277 
278 	case DVACT_DEACTIVATE:
279 		/*
280 		 * Nothing to do; we key off the device's DVF_ACTIVE.
281 		 */
282 		break;
283 	}
284 	return (rv);
285 }
286 
287 static int
288 cddetach(struct device *self, int flags)
289 {
290 	struct cd_softc *cd = (struct cd_softc *) self;
291 	struct buf *bp;
292 	int s, bmaj, cmaj, i, mn;
293 
294 	/* locate the major number */
295 	bmaj = bdevsw_lookup_major(&cd_bdevsw);
296 	cmaj = cdevsw_lookup_major(&cd_cdevsw);
297 
298 	/* kill any pending restart */
299 	callout_stop(&cd->sc_callout);
300 
301 	s = splbio();
302 
303 	/* Kill off any queued buffers. */
304 	while ((bp = BUFQ_GET(&cd->buf_queue)) != NULL) {
305 		bp->b_error = EIO;
306 		bp->b_flags |= B_ERROR;
307 		bp->b_resid = bp->b_bcount;
308 		biodone(bp);
309 	}
310 
311 	bufq_free(&cd->buf_queue);
312 
313 	/* Kill off any pending commands. */
314 	scsipi_kill_pending(cd->sc_periph);
315 
316 	splx(s);
317 
318 	/* Nuke the vnodes for any open instances */
319 	for (i = 0; i < MAXPARTITIONS; i++) {
320 		mn = CDMINOR(self->dv_unit, i);
321 		vdevgone(bmaj, mn, mn, VBLK);
322 		vdevgone(cmaj, mn, mn, VCHR);
323 	}
324 
325 	/* Detach from the disk list. */
326 	disk_detach(&cd->sc_dk);
327 
328 #if 0
329 	/* Get rid of the shutdown hook. */
330 	if (cd->sc_sdhook != NULL)
331 		shutdownhook_disestablish(cd->sc_sdhook);
332 #endif
333 
334 #if NRND > 0
335 	/* Unhook the entropy source. */
336 	rnd_detach_source(&cd->rnd_source);
337 #endif
338 
339 	return (0);
340 }
341 
342 /*
343  * Wait interruptibly for an exclusive lock.
344  *
345  * XXX
346  * Several drivers do this; it should be abstracted and made MP-safe.
347  */
348 static int
349 cdlock(struct cd_softc *cd)
350 {
351 	int error;
352 
353 	while ((cd->flags & CDF_LOCKED) != 0) {
354 		cd->flags |= CDF_WANTED;
355 		if ((error = tsleep(cd, PRIBIO | PCATCH, "cdlck", 0)) != 0)
356 			return (error);
357 	}
358 	cd->flags |= CDF_LOCKED;
359 	return (0);
360 }
361 
362 /*
363  * Unlock and wake up any waiters.
364  */
365 static void
366 cdunlock(struct cd_softc *cd)
367 {
368 
369 	cd->flags &= ~CDF_LOCKED;
370 	if ((cd->flags & CDF_WANTED) != 0) {
371 		cd->flags &= ~CDF_WANTED;
372 		wakeup(cd);
373 	}
374 }
375 
376 /*
377  * open the device. Make sure the partition info is a up-to-date as can be.
378  */
379 static int
380 cdopen(dev_t dev, int flag, int fmt, struct proc *p)
381 {
382 	struct cd_softc *cd;
383 	struct scsipi_periph *periph;
384 	struct scsipi_adapter *adapt;
385 	int unit, part;
386 	int error;
387 
388 	unit = CDUNIT(dev);
389 	if (unit >= cd_cd.cd_ndevs)
390 		return (ENXIO);
391 	cd = cd_cd.cd_devs[unit];
392 	if (cd == NULL)
393 		return (ENXIO);
394 
395 	periph = cd->sc_periph;
396 	adapt = periph->periph_channel->chan_adapter;
397 	part = CDPART(dev);
398 
399 	SC_DEBUG(periph, SCSIPI_DB1,
400 	    ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
401 	    cd_cd.cd_ndevs, CDPART(dev)));
402 
403 	/*
404 	 * If this is the first open of this device, add a reference
405 	 * to the adapter.
406 	 */
407 	if (cd->sc_dk.dk_openmask == 0 &&
408 	    (error = scsipi_adapter_addref(adapt)) != 0)
409 		return (error);
410 
411 	if ((error = cdlock(cd)) != 0)
412 		goto bad4;
413 
414 	if ((periph->periph_flags & PERIPH_OPEN) != 0) {
415 		/*
416 		 * If any partition is open, but the disk has been invalidated,
417 		 * disallow further opens.
418 		 */
419 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
420 			(part != RAW_PART || fmt != S_IFCHR )) {
421 			error = EIO;
422 			goto bad3;
423 		}
424 	} else {
425 		int silent;
426 
427 		if (part == RAW_PART && fmt == S_IFCHR)
428 			silent = XS_CTL_SILENT;
429 		else
430 			silent = 0;
431 
432 		/* Check that it is still responding and ok. */
433 		error = scsipi_test_unit_ready(periph,
434 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
435 		    silent);
436 
437 		/*
438 		 * Start the pack spinning if necessary. Always allow the
439 		 * raw parition to be opened, for raw IOCTLs. Data transfers
440 		 * will check for SDEV_MEDIA_LOADED.
441 		 */
442 		if (error == EIO) {
443 			int error2;
444 
445 			error2 = scsipi_start(periph, SSS_START, silent);
446 			switch (error2) {
447 			case 0:
448 				error = 0;
449 				break;
450 			case EIO:
451 			case EINVAL:
452 				break;
453 			default:
454 				error = error2;
455 				break;
456 			}
457 		}
458 		if (error) {
459 			if (silent)
460 				goto out;
461 			goto bad3;
462 		}
463 
464 		periph->periph_flags |= PERIPH_OPEN;
465 
466 		/* Lock the pack in. */
467 		error = scsipi_prevent(periph, PR_PREVENT,
468 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
469 		SC_DEBUG(periph, SCSIPI_DB1,
470 		    ("cdopen: scsipi_prevent, error=%d\n", error));
471 		if (error)
472 			goto bad;
473 
474 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
475 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
476 
477 			/* Load the physical device parameters. */
478 			if (cd_get_parms(cd, 0) != 0) {
479 				error = ENXIO;
480 				goto bad2;
481 			}
482 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
483 
484 			/* Fabricate a disk label. */
485 			cdgetdisklabel(cd);
486 			SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel fabricated "));
487 		}
488 	}
489 
490 	/* Check that the partition exists. */
491 	if (part != RAW_PART &&
492 	    (part >= cd->sc_dk.dk_label->d_npartitions ||
493 	    cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
494 		error = ENXIO;
495 		goto bad;
496 	}
497 
498 out:	/* Insure only one open at a time. */
499 	switch (fmt) {
500 	case S_IFCHR:
501 		cd->sc_dk.dk_copenmask |= (1 << part);
502 		break;
503 	case S_IFBLK:
504 		cd->sc_dk.dk_bopenmask |= (1 << part);
505 		break;
506 	}
507 	cd->sc_dk.dk_openmask =
508 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
509 
510 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
511 	cdunlock(cd);
512 	return (0);
513 
514 bad2:
515 	periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
516 
517 bad:
518 	if (cd->sc_dk.dk_openmask == 0) {
519 		scsipi_prevent(periph, PR_ALLOW,
520 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
521 		periph->periph_flags &= ~PERIPH_OPEN;
522 	}
523 
524 bad3:
525 	cdunlock(cd);
526 bad4:
527 	if (cd->sc_dk.dk_openmask == 0)
528 		scsipi_adapter_delref(adapt);
529 	return (error);
530 }
531 
532 /*
533  * close the device.. only called if we are the LAST
534  * occurence of an open device
535  */
536 static int
537 cdclose(dev_t dev, int flag, int fmt, struct proc *p)
538 {
539 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
540 	struct scsipi_periph *periph = cd->sc_periph;
541 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
542 	int part = CDPART(dev);
543 	int error;
544 
545 	if ((error = cdlock(cd)) != 0)
546 		return (error);
547 
548 	switch (fmt) {
549 	case S_IFCHR:
550 		cd->sc_dk.dk_copenmask &= ~(1 << part);
551 		break;
552 	case S_IFBLK:
553 		cd->sc_dk.dk_bopenmask &= ~(1 << part);
554 		break;
555 	}
556 	cd->sc_dk.dk_openmask =
557 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
558 
559 	if (cd->sc_dk.dk_openmask == 0) {
560 		scsipi_wait_drain(periph);
561 
562 		scsipi_prevent(periph, PR_ALLOW,
563 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
564 		    XS_CTL_IGNORE_NOT_READY);
565 		periph->periph_flags &= ~PERIPH_OPEN;
566 
567 		scsipi_wait_drain(periph);
568 
569 		scsipi_adapter_delref(adapt);
570 	}
571 
572 	cdunlock(cd);
573 	return (0);
574 }
575 
576 /*
577  * Actually translate the requested transfer into one the physical driver can
578  * understand.  The transfer is described by a buf and will include only one
579  * physical transfer.
580  */
581 static void
582 cdstrategy(struct buf *bp)
583 {
584 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
585 	struct disklabel *lp;
586 	struct scsipi_periph *periph = cd->sc_periph;
587 	daddr_t blkno;
588 	int s;
589 
590 	SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdstrategy "));
591 	SC_DEBUG(cd->sc_periph, SCSIPI_DB1,
592 	    ("%ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
593 	/*
594 	 * If the device has been made invalid, error out
595 	 * maybe the media changed
596 	 */
597 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
598 		if (periph->periph_flags & PERIPH_OPEN)
599 			bp->b_error = EIO;
600 		else
601 			bp->b_error = ENODEV;
602 		goto bad;
603 	}
604 
605 	lp = cd->sc_dk.dk_label;
606 
607 	/*
608 	 * The transfer must be a whole number of blocks, offset must not
609 	 * be negative.
610 	 */
611 	if ((bp->b_bcount % lp->d_secsize) != 0 ||
612 	    bp->b_blkno < 0 ) {
613 		bp->b_error = EINVAL;
614 		goto bad;
615 	}
616 	/*
617 	 * If it's a null transfer, return immediately
618 	 */
619 	if (bp->b_bcount == 0)
620 		goto done;
621 
622 	/*
623 	 * Do bounds checking, adjust transfer. if error, process.
624 	 * If end of partition, just return.
625 	 */
626 	if (CDPART(bp->b_dev) == RAW_PART) {
627 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
628 		    cd->params.disksize512) <= 0)
629 			goto done;
630 	} else {
631 		if (bounds_check_with_label(&cd->sc_dk, bp,
632 		    (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0)
633 			goto done;
634 	}
635 
636 	/*
637 	 * Now convert the block number to absolute and put it in
638 	 * terms of the device's logical block size.
639 	 */
640 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
641 	if (CDPART(bp->b_dev) != RAW_PART)
642 		blkno += lp->d_partitions[CDPART(bp->b_dev)].p_offset;
643 
644 	bp->b_rawblkno = blkno;
645 
646 	/*
647 	 * If the disklabel sector size does not match the device
648 	 * sector size we may need to do some extra work.
649 	 */
650 	if (lp->d_secsize != cd->params.blksize) {
651 
652 		/*
653 		 * If the xfer is not a multiple of the device block size
654 		 * or it is not block aligned, we need to bounce it.
655 		 */
656 		if ((bp->b_bcount % cd->params.blksize) != 0 ||
657 			((blkno * lp->d_secsize) % cd->params.blksize) != 0) {
658 			struct buf *nbp;
659 			void *bounce = NULL;
660 			long count;
661 
662 			if ((bp->b_flags & B_READ) == 0) {
663 
664 				/* XXXX We don't support bouncing writes. */
665 				bp->b_error = EACCES;
666 				goto bad;
667 			}
668 			count = ((blkno * lp->d_secsize) % cd->params.blksize);
669 			/* XXX Store starting offset in bp->b_rawblkno */
670 			bp->b_rawblkno = count;
671 
672 			count += bp->b_bcount;
673 			count = roundup(count, cd->params.blksize);
674 
675 			blkno = ((blkno * lp->d_secsize) / cd->params.blksize);
676 			s = splbio();
677 			nbp = pool_get(&bufpool, PR_NOWAIT);
678 			splx(s);
679 			if (!nbp) {
680 				/* No memory -- fail the iop. */
681 				bp->b_error = ENOMEM;
682 				goto bad;
683 			}
684 			bounce = malloc(count, M_DEVBUF, M_NOWAIT);
685 			if (!bounce) {
686 				/* No memory -- fail the iop. */
687 				s = splbio();
688 				pool_put(&bufpool, nbp);
689 				splx(s);
690 				bp->b_error = ENOMEM;
691 				goto bad;
692 			}
693 
694 			/* Set up the IOP to the bounce buffer. */
695 			BUF_INIT(nbp);
696 			nbp->b_error = 0;
697 			nbp->b_proc = bp->b_proc;
698 			nbp->b_vp = NULLVP;
699 
700 			nbp->b_bcount = count;
701 			nbp->b_bufsize = count;
702 			nbp->b_data = bounce;
703 
704 			nbp->b_rawblkno = blkno;
705 
706 			/* We need to do a read-modify-write operation */
707 			nbp->b_flags = bp->b_flags | B_READ | B_CALL;
708 			nbp->b_iodone = cdbounce;
709 
710 			/* Put ptr to orig buf in b_private and use new buf */
711 			nbp->b_private = bp;
712 
713 			BIO_COPYPRIO(nbp, bp);
714 
715 			bp = nbp;
716 
717 		} else {
718 			/* Xfer is aligned -- just adjust the start block */
719 			bp->b_rawblkno = (blkno * lp->d_secsize) /
720 				cd->params.blksize;
721 		}
722 	}
723 	s = splbio();
724 
725 	/*
726 	 * Place it in the queue of disk activities for this disk.
727 	 *
728 	 * XXX Only do disksort() if the current operating mode does not
729 	 * XXX include tagged queueing.
730 	 */
731 	BUFQ_PUT(&cd->buf_queue, bp);
732 
733 	/*
734 	 * Tell the device to get going on the transfer if it's
735 	 * not doing anything, otherwise just wait for completion
736 	 */
737 	cdstart(cd->sc_periph);
738 
739 	splx(s);
740 	return;
741 
742 bad:
743 	bp->b_flags |= B_ERROR;
744 done:
745 	/*
746 	 * Correctly set the buf to indicate a completed xfer
747 	 */
748 	bp->b_resid = bp->b_bcount;
749 	biodone(bp);
750 }
751 
752 /*
753  * cdstart looks to see if there is a buf waiting for the device
754  * and that the device is not already busy. If both are true,
755  * It deques the buf and creates a scsi command to perform the
756  * transfer in the buf. The transfer request will call scsipi_done
757  * on completion, which will in turn call this routine again
758  * so that the next queued transfer is performed.
759  * The bufs are queued by the strategy routine (cdstrategy)
760  *
761  * This routine is also called after other non-queued requests
762  * have been made of the scsi driver, to ensure that the queue
763  * continues to be drained.
764  *
765  * must be called at the correct (highish) spl level
766  * cdstart() is called at splbio from cdstrategy, cdrestart and scsipi_done
767  */
768 static void
769 cdstart(struct scsipi_periph *periph)
770 {
771 	struct cd_softc *cd = (void *)periph->periph_dev;
772 	struct buf *bp = 0;
773 	struct scsipi_rw_big cmd_big;
774 	struct scsi_rw cmd_small;
775 	struct scsipi_generic *cmdp;
776 	struct scsipi_xfer *xs;
777 	int flags, nblks, cmdlen, error;
778 
779 	SC_DEBUG(periph, SCSIPI_DB2, ("cdstart "));
780 	/*
781 	 * Check if the device has room for another command
782 	 */
783 	while (periph->periph_active < periph->periph_openings) {
784 		/*
785 		 * there is excess capacity, but a special waits
786 		 * It'll need the adapter as soon as we clear out of the
787 		 * way and let it run (user level wait).
788 		 */
789 		if (periph->periph_flags & PERIPH_WAITING) {
790 			periph->periph_flags &= ~PERIPH_WAITING;
791 			wakeup((caddr_t)periph);
792 			return;
793 		}
794 
795 		/*
796 		 * If the device has become invalid, abort all the
797 		 * reads and writes until all files have been closed and
798 		 * re-opened
799 		 */
800 		if (__predict_false(
801 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
802 			if ((bp = BUFQ_GET(&cd->buf_queue)) != NULL) {
803 				bp->b_error = EIO;
804 				bp->b_flags |= B_ERROR;
805 				bp->b_resid = bp->b_bcount;
806 				biodone(bp);
807 				continue;
808 			} else {
809 				return;
810 			}
811 		}
812 
813 		/*
814 		 * See if there is a buf with work for us to do..
815 		 */
816 		if ((bp = BUFQ_PEEK(&cd->buf_queue)) == NULL)
817 			return;
818 
819 		/*
820 		 * We have a buf, now we should make a command.
821 		 */
822 
823 		nblks = howmany(bp->b_bcount, cd->params.blksize);
824 
825 		/*
826 		 *  Fill out the scsi command.  If the transfer will
827 		 *  fit in a "small" cdb, use it.
828 		 */
829 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
830 		    ((nblks & 0xff) == nblks) &&
831 		    !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
832 			/*
833 			 * We can fit in a small cdb.
834 			 */
835 			memset(&cmd_small, 0, sizeof(cmd_small));
836 			cmd_small.opcode = (bp->b_flags & B_READ) ?
837 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
838 			_lto3b(bp->b_rawblkno, cmd_small.addr);
839 			cmd_small.length = nblks & 0xff;
840 			cmdlen = sizeof(cmd_small);
841 			cmdp = (struct scsipi_generic *)&cmd_small;
842 		} else {
843 			/*
844 			 * Need a large cdb.
845 			 */
846 			memset(&cmd_big, 0, sizeof(cmd_big));
847 			cmd_big.opcode = (bp->b_flags & B_READ) ?
848 			    READ_BIG : WRITE_BIG;
849 			_lto4b(bp->b_rawblkno, cmd_big.addr);
850 			_lto2b(nblks, cmd_big.length);
851 			cmdlen = sizeof(cmd_big);
852 			cmdp = (struct scsipi_generic *)&cmd_big;
853 		}
854 
855 		/* Instrumentation. */
856 		disk_busy(&cd->sc_dk);
857 
858 		/*
859 		 * Figure out what flags to use.
860 		 */
861 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
862 		if (bp->b_flags & B_READ)
863 			flags |= XS_CTL_DATA_IN;
864 		else
865 			flags |= XS_CTL_DATA_OUT;
866 
867 		/*
868 		 * Call the routine that chats with the adapter.
869 		 * Note: we cannot sleep as we may be an interrupt
870 		 */
871 		xs = scsipi_make_xs(periph, cmdp, cmdlen,
872 		    (u_char *)bp->b_data, bp->b_bcount,
873 		    CDRETRIES, 30000, bp, flags);
874 		if (__predict_false(xs == NULL)) {
875 			/*
876 			 * out of memory. Keep this buffer in the queue, and
877 			 * retry later.
878 			 */
879 			callout_reset(&cd->sc_callout, hz / 2, cdrestart,
880 			    periph);
881 			return;
882 		}
883 		/*
884 		 * need to dequeue the buffer before queuing the command,
885 		 * because cdstart may be called recursively from the
886 		 * HBA driver
887 		 */
888 #ifdef DIAGNOSTIC
889 		if (BUFQ_GET(&cd->buf_queue) != bp)
890 			panic("cdstart(): dequeued wrong buf");
891 #else
892 		BUFQ_GET(&cd->buf_queue);
893 #endif
894 		error = scsipi_command(periph, xs, cmdp, cmdlen,
895 		    (u_char *)bp->b_data, bp->b_bcount,
896 		    CDRETRIES, 30000, bp, flags);
897 		/* with a scsipi_xfer preallocated, scsipi_command can't fail */
898 		KASSERT(error == 0);
899 	}
900 }
901 
902 static void
903 cdrestart(void *v)
904 {
905 	int s = splbio();
906 	cdstart((struct scsipi_periph *)v);
907 	splx(s);
908 }
909 
910 static void
911 cddone(struct scsipi_xfer *xs)
912 {
913 	struct cd_softc *cd = (void *)xs->xs_periph->periph_dev;
914 
915 	if (xs->bp != NULL) {
916 		disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid,
917 		    (xs->bp->b_flags & B_READ));
918 #if NRND > 0
919 		rnd_add_uint32(&cd->rnd_source, xs->bp->b_rawblkno);
920 #endif
921 	}
922 }
923 
924 static void
925 cdbounce(struct buf *bp)
926 {
927 	struct buf *obp = (struct buf *)bp->b_private;
928 
929 	if (bp->b_flags & B_ERROR) {
930 		/* EEK propagate the error and free the memory */
931 		goto done;
932 	}
933 	if (obp->b_flags & B_READ) {
934 		/* Copy data to the final destination and free the buf. */
935 		memcpy(obp->b_data, bp->b_data+obp->b_rawblkno,
936 			obp->b_bcount);
937 	} else {
938 		/*
939 		 * XXXX This is a CD-ROM -- READ ONLY -- why do we bother with
940 		 * XXXX any of this write stuff?
941 		 */
942 		if (bp->b_flags & B_READ) {
943 			struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
944 			struct buf *nbp;
945 			int s;
946 
947 			/* Read part of RMW complete. */
948 			memcpy(bp->b_data+obp->b_rawblkno, obp->b_data,
949 				obp->b_bcount);
950 
951 			s = splbio();
952 
953 			/* We need to alloc a new buf. */
954 			nbp = pool_get(&bufpool, PR_NOWAIT);
955 			if (!nbp) {
956 				splx(s);
957 				/* No buf available. */
958 				bp->b_flags |= B_ERROR;
959 				bp->b_error = ENOMEM;
960 				bp->b_resid = bp->b_bcount;
961 			}
962 
963 			/* Set up the IOP to the bounce buffer. */
964 			BUF_INIT(nbp);
965 			nbp->b_error = 0;
966 			nbp->b_proc = bp->b_proc;
967 			nbp->b_vp = NULLVP;
968 
969 			nbp->b_bcount = bp->b_bcount;
970 			nbp->b_bufsize = bp->b_bufsize;
971 			nbp->b_data = bp->b_data;
972 
973 			nbp->b_rawblkno = bp->b_rawblkno;
974 
975 			/* We need to do a read-modify-write operation */
976 			nbp->b_flags = obp->b_flags | B_CALL;
977 			nbp->b_iodone = cdbounce;
978 
979 			/* Put ptr to orig buf in b_private and use new buf */
980 			nbp->b_private = obp;
981 
982 			/*
983 			 * Place it in the queue of disk activities for this
984 			 * disk.
985 			 *
986 			 * XXX Only do disksort() if the current operating mode
987 			 * XXX does not include tagged queueing.
988 			 */
989 			BUFQ_PUT(&cd->buf_queue, nbp);
990 
991 			/*
992 			 * Tell the device to get going on the transfer if it's
993 			 * not doing anything, otherwise just wait for
994 			 * completion
995 			 */
996 			cdstart(cd->sc_periph);
997 
998 			splx(s);
999 			return;
1000 
1001 		}
1002 	}
1003 done:
1004 	obp->b_flags |= (bp->b_flags&(B_EINTR|B_ERROR));
1005 	obp->b_error = bp->b_error;
1006 	obp->b_resid = bp->b_resid;
1007 	free(bp->b_data, M_DEVBUF);
1008 	biodone(obp);
1009 }
1010 
1011 static int
1012 cd_interpret_sense(struct scsipi_xfer *xs)
1013 {
1014 	struct scsipi_periph *periph = xs->xs_periph;
1015 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
1016 	int retval = EJUSTRETURN;
1017 
1018 	/*
1019 	 * If it isn't a extended or extended/deferred error, let
1020 	 * the generic code handle it.
1021 	 */
1022 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1023 	    (sense->error_code & SSD_ERRCODE) != 0x71) {	/* DEFERRED */
1024 		return (retval);
1025 	}
1026 
1027 	/*
1028 	 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit
1029 	 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then
1030 	 * wait a bit for the drive to spin up
1031 	 */
1032 
1033 	if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1034 	    sense->add_sense_code == 0x4 &&
1035 	    sense->add_sense_code_qual == 0x01)	{
1036 		/*
1037 		 * Sleep for 5 seconds to wait for the drive to spin up
1038 		 */
1039 
1040 		SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD "
1041 						"spinup\n"));
1042 		if (!callout_pending(&periph->periph_callout))
1043 			scsipi_periph_freeze(periph, 1);
1044 		callout_reset(&periph->periph_callout,
1045 		    5 * hz, scsipi_periph_timed_thaw, periph);
1046 		retval = ERESTART;
1047 	}
1048 	return (retval);
1049 }
1050 
1051 static void
1052 cdminphys(struct buf *bp)
1053 {
1054 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
1055 	long max;
1056 
1057 	/*
1058 	 * If the device is ancient, we want to make sure that
1059 	 * the transfer fits into a 6-byte cdb.
1060 	 *
1061 	 * XXX Note that the SCSI-I spec says that 256-block transfers
1062 	 * are allowed in a 6-byte read/write, and are specified
1063 	 * by settng the "length" to 0.  However, we're conservative
1064 	 * here, allowing only 255-block transfers in case an
1065 	 * ancient device gets confused by length == 0.  A length of 0
1066 	 * in a 10-byte read/write actually means 0 blocks.
1067 	 */
1068 	if (cd->flags & CDF_ANCIENT) {
1069 		max = cd->sc_dk.dk_label->d_secsize * 0xff;
1070 
1071 		if (bp->b_bcount > max)
1072 			bp->b_bcount = max;
1073 	}
1074 
1075 	(*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp);
1076 }
1077 
1078 static int
1079 cdread(dev_t dev, struct uio *uio, int ioflag)
1080 {
1081 
1082 	return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio));
1083 }
1084 
1085 static int
1086 cdwrite(dev_t dev, struct uio *uio, int ioflag)
1087 {
1088 
1089 	return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio));
1090 }
1091 
1092 #if 0	/* XXX Not used */
1093 /*
1094  * conversion between minute-seconde-frame and logical block address
1095  * addresses format
1096  */
1097 static void
1098 lba2msf(u_long lba, u_char *m, u_char *s, u_char *f)
1099 {
1100 	u_long tmp;
1101 
1102 	tmp = lba + CD_BLOCK_OFFSET;	/* offset of first logical frame */
1103 	tmp &= 0xffffff;		/* negative lbas use only 24 bits */
1104 	*m = tmp / (CD_SECS * CD_FRAMES);
1105 	tmp %= (CD_SECS * CD_FRAMES);
1106 	*s = tmp / CD_FRAMES;
1107 	*f = tmp % CD_FRAMES;
1108 }
1109 
1110 static u_long
1111 msf2lba(u_char m, u_char s, u_char f)
1112 {
1113 
1114 	return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET);
1115 }
1116 #endif /* XXX Not used */
1117 
1118 static int
1119 cdreadmsaddr(struct cd_softc *cd, int *addr)
1120 {
1121 	struct scsipi_periph *periph = cd->sc_periph;
1122 	int error;
1123 	struct cd_toc toc;
1124 	struct cd_toc_entry *cte;
1125 
1126 	error = cd_read_toc(cd, 0, 0, &toc,
1127 	    sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
1128 	    XS_CTL_DATA_ONSTACK,
1129 	    0x40 /* control word for "get MS info" */);
1130 
1131 	if (error)
1132 		return (error);
1133 
1134 	cte = &toc.entries[0];
1135 	if (periph->periph_quirks & PQUIRK_LITTLETOC) {
1136 		cte->addr.lba = le32toh(cte->addr.lba);
1137 		toc.header.len = le16toh(toc.header.len);
1138 	} else {
1139 		cte->addr.lba = be32toh(cte->addr.lba);
1140 		toc.header.len = be16toh(toc.header.len);
1141 	}
1142 
1143 	*addr = (toc.header.len >= 10 && cte->track > 1) ?
1144 		cte->addr.lba : 0;
1145 	return 0;
1146 }
1147 
1148 /*
1149  * Perform special action on behalf of the user.
1150  * Knows about the internals of this device
1151  */
1152 static int
1153 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1154 {
1155 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
1156 	struct scsipi_periph *periph = cd->sc_periph;
1157 	int part = CDPART(dev);
1158 	int error = 0;
1159 #ifdef __HAVE_OLD_DISKLABEL
1160 	struct disklabel *newlabel = NULL;
1161 #endif
1162 
1163 	SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd));
1164 
1165 	/*
1166 	 * If the device is not valid, some IOCTLs can still be
1167 	 * handled on the raw partition. Check this here.
1168 	 */
1169 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1170 		switch (cmd) {
1171 		case DIOCWLABEL:
1172 		case DIOCLOCK:
1173 		case ODIOCEJECT:
1174 		case DIOCEJECT:
1175 		case SCIOCIDENTIFY:
1176 		case OSCIOCIDENTIFY:
1177 		case SCIOCCOMMAND:
1178 		case SCIOCDEBUG:
1179 		case CDIOCGETVOL:
1180 		case CDIOCSETVOL:
1181 		case CDIOCSETMONO:
1182 		case CDIOCSETSTEREO:
1183 		case CDIOCSETMUTE:
1184 		case CDIOCSETLEFT:
1185 		case CDIOCSETRIGHT:
1186 		case CDIOCCLOSE:
1187 		case CDIOCEJECT:
1188 		case CDIOCALLOW:
1189 		case CDIOCPREVENT:
1190 		case CDIOCSETDEBUG:
1191 		case CDIOCCLRDEBUG:
1192 		case CDIOCRESET:
1193 		case SCIOCRESET:
1194 		case CDIOCLOADUNLOAD:
1195 		case DVD_AUTH:
1196 		case DVD_READ_STRUCT:
1197 			if (part == RAW_PART)
1198 				break;
1199 		/* FALLTHROUGH */
1200 		default:
1201 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
1202 				return (ENODEV);
1203 			else
1204 				return (EIO);
1205 		}
1206 	}
1207 
1208 	switch (cmd) {
1209 	case DIOCGDINFO:
1210 		*(struct disklabel *)addr = *(cd->sc_dk.dk_label);
1211 		return (0);
1212 #ifdef __HAVE_OLD_DISKLABEL
1213 	case ODIOCGDINFO:
1214 		newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK);
1215 		if (newlabel == NULL)
1216 			return (EIO);
1217 		memcpy(newlabel, cd->sc_dk.dk_label, sizeof (*newlabel));
1218 		if (newlabel->d_npartitions > OLDMAXPARTITIONS)
1219 			error = ENOTTY;
1220 		else
1221 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
1222 		free(newlabel, M_TEMP);
1223 		return error;
1224 #endif
1225 
1226 	case DIOCGPART:
1227 		((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label;
1228 		((struct partinfo *)addr)->part =
1229 		    &cd->sc_dk.dk_label->d_partitions[part];
1230 		return (0);
1231 
1232 	case DIOCWDINFO:
1233 	case DIOCSDINFO:
1234 #ifdef __HAVE_OLD_DISKLABEL
1235 	case ODIOCWDINFO:
1236 	case ODIOCSDINFO:
1237 #endif
1238 	{
1239 		struct disklabel *lp;
1240 
1241 		if ((flag & FWRITE) == 0)
1242 			return (EBADF);
1243 
1244 #ifdef __HAVE_OLD_DISKLABEL
1245 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1246 			newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK);
1247 			if (newlabel == NULL)
1248 				return (EIO);
1249 			memset(newlabel, 0, sizeof newlabel);
1250 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
1251 			lp = newlabel;
1252 		} else
1253 #endif
1254 		lp = (struct disklabel *)addr;
1255 
1256 		if ((error = cdlock(cd)) != 0)
1257 			goto bad;
1258 		cd->flags |= CDF_LABELLING;
1259 
1260 		error = setdisklabel(cd->sc_dk.dk_label,
1261 		    lp, /*cd->sc_dk.dk_openmask : */0,
1262 		    cd->sc_dk.dk_cpulabel);
1263 		if (error == 0) {
1264 			/* XXX ? */
1265 		}
1266 
1267 		cd->flags &= ~CDF_LABELLING;
1268 		cdunlock(cd);
1269 bad:
1270 #ifdef __HAVE_OLD_DISKLABEL
1271 		if (newlabel != NULL)
1272 			free(newlabel, M_TEMP);
1273 #endif
1274 		return (error);
1275 	}
1276 
1277 	case DIOCWLABEL:
1278 		return (EBADF);
1279 
1280 	case DIOCGDEFLABEL:
1281 		cdgetdefaultlabel(cd, (struct disklabel *)addr);
1282 		return (0);
1283 
1284 #ifdef __HAVE_OLD_DISKLABEL
1285 	case ODIOCGDEFLABEL:
1286 		newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK);
1287 		if (newlabel == NULL)
1288 			return (EIO);
1289 		cdgetdefaultlabel(cd, newlabel);
1290 		if (newlabel->d_npartitions > OLDMAXPARTITIONS)
1291 			error = ENOTTY;
1292 		else
1293 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
1294 		free(newlabel, M_TEMP);
1295 		return error;
1296 #endif
1297 
1298 	case CDIOCPLAYTRACKS: {
1299 		struct ioc_play_track *args = (struct ioc_play_track *)addr;
1300 
1301 		if ((error = cd_set_pa_immed(cd, 0)) != 0)
1302 			return (error);
1303 		return (cd_play_tracks(cd, args->start_track,
1304 		    args->start_index, args->end_track, args->end_index));
1305 	}
1306 	case CDIOCPLAYMSF: {
1307 		struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
1308 
1309 		if ((error = cd_set_pa_immed(cd, 0)) != 0)
1310 			return (error);
1311 		return (cd_play_msf(cd, args->start_m, args->start_s,
1312 		    args->start_f, args->end_m, args->end_s, args->end_f));
1313 	}
1314 	case CDIOCPLAYBLOCKS: {
1315 		struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
1316 
1317 		if ((error = cd_set_pa_immed(cd, 0)) != 0)
1318 			return (error);
1319 		return (cd_play(cd, args->blk, args->len));
1320 	}
1321 	case CDIOCREADSUBCHANNEL: {
1322 		struct ioc_read_subchannel *args =
1323 		    (struct ioc_read_subchannel *)addr;
1324 		struct cd_sub_channel_info data;
1325 		u_int len = args->data_len;
1326 
1327 		if (len > sizeof(data) ||
1328 		    len < sizeof(struct cd_sub_channel_header))
1329 			return (EINVAL);
1330 		error = cd_read_subchannel(cd, args->address_format,
1331 		    args->data_format, args->track, &data, len,
1332 		    XS_CTL_DATA_ONSTACK);
1333 		if (error)
1334 			return (error);
1335 		len = min(len, _2btol(data.header.data_len) +
1336 		    sizeof(struct cd_sub_channel_header));
1337 		return (copyout(&data, args->data, len));
1338 	}
1339 	case CDIOREADTOCHEADER: {
1340 		struct ioc_toc_header th;
1341 
1342 		if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th),
1343 		    XS_CTL_DATA_ONSTACK, 0)) != 0)
1344 			return (error);
1345 		if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC)
1346 			th.len = le16toh(th.len);
1347 		else
1348 			th.len = be16toh(th.len);
1349 		memcpy(addr, &th, sizeof(th));
1350 		return (0);
1351 	}
1352 	case CDIOREADTOCENTRYS: {
1353 		struct cd_toc toc;
1354 		struct ioc_read_toc_entry *te =
1355 		    (struct ioc_read_toc_entry *)addr;
1356 		struct ioc_toc_header *th;
1357 		struct cd_toc_entry *cte;
1358 		u_int len = te->data_len;
1359 		int ntracks;
1360 
1361 		th = &toc.header;
1362 
1363 		if (len > sizeof(toc.entries) ||
1364 		    len < sizeof(struct cd_toc_entry))
1365 			return (EINVAL);
1366 		error = cd_read_toc(cd, te->address_format, te->starting_track,
1367 		    &toc, len + sizeof(struct ioc_toc_header),
1368 		    XS_CTL_DATA_ONSTACK, 0);
1369 		if (error)
1370 			return (error);
1371 		if (te->address_format == CD_LBA_FORMAT)
1372 			for (ntracks =
1373 			    th->ending_track - th->starting_track + 1;
1374 			    ntracks >= 0; ntracks--) {
1375 				cte = &toc.entries[ntracks];
1376 				cte->addr_type = CD_LBA_FORMAT;
1377 				if (periph->periph_quirks & PQUIRK_LITTLETOC)
1378 					cte->addr.lba = le32toh(cte->addr.lba);
1379 				else
1380 					cte->addr.lba = be32toh(cte->addr.lba);
1381 			}
1382 		if (periph->periph_quirks & PQUIRK_LITTLETOC)
1383 			th->len = le16toh(th->len);
1384 		else
1385 			th->len = be16toh(th->len);
1386 		len = min(len, th->len - (sizeof(th->starting_track) +
1387 		    sizeof(th->ending_track)));
1388 		return (copyout(toc.entries, te->data, len));
1389 	}
1390 	case CDIOREADMSADDR: {
1391 		int sessno = *(int*)addr;
1392 
1393 		if (sessno != 0)
1394 			return (EINVAL);
1395 
1396 		return (cdreadmsaddr(cd, (int*)addr));
1397 	}
1398 	case CDIOCSETPATCH: {
1399 		struct ioc_patch *arg = (struct ioc_patch *)addr;
1400 
1401 		return (cd_setchan(cd, arg->patch[0], arg->patch[1],
1402 		    arg->patch[2], arg->patch[3], 0));
1403 	}
1404 	case CDIOCGETVOL: {
1405 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1406 
1407 		return (cd_getvol(cd, arg, 0));
1408 	}
1409 	case CDIOCSETVOL: {
1410 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1411 
1412 		return (cd_setvol(cd, arg, 0));
1413 	}
1414 
1415 	case CDIOCSETMONO:
1416 		return (cd_setchan(cd, BOTH_CHANNEL, BOTH_CHANNEL,
1417 		    MUTE_CHANNEL, MUTE_CHANNEL, 0));
1418 
1419 	case CDIOCSETSTEREO:
1420 		return (cd_setchan(cd, LEFT_CHANNEL, RIGHT_CHANNEL,
1421 		    MUTE_CHANNEL, MUTE_CHANNEL, 0));
1422 
1423 	case CDIOCSETMUTE:
1424 		return (cd_setchan(cd, MUTE_CHANNEL, MUTE_CHANNEL,
1425 		    MUTE_CHANNEL, MUTE_CHANNEL, 0));
1426 
1427 	case CDIOCSETLEFT:
1428 		return (cd_setchan(cd, LEFT_CHANNEL, LEFT_CHANNEL,
1429 		    MUTE_CHANNEL, MUTE_CHANNEL, 0));
1430 
1431 	case CDIOCSETRIGHT:
1432 		return (cd_setchan(cd, RIGHT_CHANNEL, RIGHT_CHANNEL,
1433 		    MUTE_CHANNEL, MUTE_CHANNEL, 0));
1434 
1435 	case CDIOCRESUME:
1436 		return (cd_pause(cd, PA_RESUME));
1437 	case CDIOCPAUSE:
1438 		return (cd_pause(cd, PA_PAUSE));
1439 	case CDIOCSTART:
1440 		return (scsipi_start(periph, SSS_START, 0));
1441 	case CDIOCSTOP:
1442 		return (scsipi_start(periph, SSS_STOP, 0));
1443 	case CDIOCCLOSE:
1444 		return (scsipi_start(periph, SSS_START|SSS_LOEJ,
1445 		    XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE));
1446 	case DIOCEJECT:
1447 		if (*(int *)addr == 0) {
1448 			/*
1449 			 * Don't force eject: check that we are the only
1450 			 * partition open. If so, unlock it.
1451 			 */
1452 			if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1453 			    cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask ==
1454 			    cd->sc_dk.dk_openmask) {
1455 				error = scsipi_prevent(periph, PR_ALLOW,
1456 				    XS_CTL_IGNORE_NOT_READY);
1457 				if (error)
1458 					return (error);
1459 			} else {
1460 				return (EBUSY);
1461 			}
1462 		}
1463 		/* FALLTHROUGH */
1464 	case CDIOCEJECT: /* FALLTHROUGH */
1465 	case ODIOCEJECT:
1466 		return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1467 	case CDIOCALLOW:
1468 		return (scsipi_prevent(periph, PR_ALLOW, 0));
1469 	case CDIOCPREVENT:
1470 		return (scsipi_prevent(periph, PR_PREVENT, 0));
1471 	case DIOCLOCK:
1472 		return (scsipi_prevent(periph,
1473 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1474 	case CDIOCSETDEBUG:
1475 		cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2);
1476 		return (0);
1477 	case CDIOCCLRDEBUG:
1478 		cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2);
1479 		return (0);
1480 	case CDIOCRESET:
1481 	case SCIOCRESET:
1482 		return (cd_reset(cd));
1483 	case CDIOCLOADUNLOAD:
1484 		return (cd_load_unload(cd, (struct ioc_load_unload *)addr));
1485 	case DVD_AUTH:
1486 		return (dvd_auth(cd, (dvd_authinfo *)addr));
1487 	case DVD_READ_STRUCT:
1488 		return (dvd_read_struct(cd, (dvd_struct *)addr));
1489 
1490 	default:
1491 		if (part != RAW_PART)
1492 			return (ENOTTY);
1493 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
1494 	}
1495 
1496 #ifdef DIAGNOSTIC
1497 	panic("cdioctl: impossible");
1498 #endif
1499 }
1500 
1501 static void
1502 cdgetdefaultlabel(struct cd_softc *cd, struct disklabel *lp)
1503 {
1504 	int lastsession;
1505 
1506 	memset(lp, 0, sizeof(struct disklabel));
1507 
1508 	lp->d_secsize = cd->params.blksize;
1509 	lp->d_ntracks = 1;
1510 	lp->d_nsectors = 100;
1511 	lp->d_ncylinders = (cd->params.disksize / 100) + 1;
1512 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1513 
1514 	switch (scsipi_periph_bustype(cd->sc_periph)) {
1515 	case SCSIPI_BUSTYPE_SCSI:
1516 		lp->d_type = DTYPE_SCSI;
1517 		break;
1518 	case SCSIPI_BUSTYPE_ATAPI:
1519 		lp->d_type = DTYPE_ATAPI;
1520 		break;
1521 	}
1522 	/*
1523 	 * XXX
1524 	 * We could probe the mode pages to figure out what kind of disc it is.
1525 	 * Is this worthwhile?
1526 	 */
1527 	strncpy(lp->d_typename, "mydisc", 16);
1528 	strncpy(lp->d_packname, "fictitious", 16);
1529 	lp->d_secperunit = cd->params.disksize;
1530 	lp->d_rpm = 300;
1531 	lp->d_interleave = 1;
1532 	lp->d_flags = D_REMOVABLE;
1533 
1534 	if (cdreadmsaddr(cd, &lastsession) != 0)
1535 		lastsession = 0;
1536 
1537 	lp->d_partitions[0].p_offset = 0;
1538 #ifdef notyet /* have to fix bounds_check_with_label() first */
1539 	lp->d_partitions[0].p_size = lp->d_secperunit;
1540 #else
1541 	lp->d_partitions[0].p_size =
1542 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1543 #endif
1544 	lp->d_partitions[0].p_cdsession = lastsession;
1545 	lp->d_partitions[0].p_fstype = FS_ISO9660;
1546 	lp->d_partitions[RAW_PART].p_offset = 0;
1547 #ifdef notyet
1548 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1549 #else
1550 	lp->d_partitions[RAW_PART].p_size =
1551 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1552 #endif
1553 	lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
1554 	lp->d_npartitions = RAW_PART + 1;
1555 
1556 	lp->d_magic = DISKMAGIC;
1557 	lp->d_magic2 = DISKMAGIC;
1558 	lp->d_checksum = dkcksum(lp);
1559 }
1560 
1561 /*
1562  * Load the label information on the named device
1563  * Actually fabricate a disklabel
1564  *
1565  * EVENTUALLY take information about different
1566  * data tracks from the TOC and put it in the disklabel
1567  */
1568 static void
1569 cdgetdisklabel(struct cd_softc *cd)
1570 {
1571 	struct disklabel *lp = cd->sc_dk.dk_label;
1572 	const char *errstring;
1573 
1574 	memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1575 
1576 	cdgetdefaultlabel(cd, lp);
1577 
1578 	/*
1579 	 * Call the generic disklabel extraction routine
1580 	 */
1581 	errstring = readdisklabel(MAKECDDEV(0, cd->sc_dev.dv_unit, RAW_PART),
1582 	    cdstrategy, lp, cd->sc_dk.dk_cpulabel);
1583 	if (errstring) {
1584 		printf("%s: %s\n", cd->sc_dev.dv_xname, errstring);
1585 		goto error;
1586 	}
1587 	return;
1588 
1589 error:
1590 	/* Reset to default label -- should print a warning */
1591 	memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1592 
1593 	cdgetdefaultlabel(cd, lp);
1594 }
1595 
1596 /*
1597  * Find out from the device what it's capacity is
1598  */
1599 static u_long
1600 cd_size(struct cd_softc *cd, int flags)
1601 {
1602 	struct scsipi_read_cd_cap_data rdcap;
1603 	struct scsipi_read_cd_capacity scsipi_cmd;
1604 	int blksize;
1605 	u_long size;
1606 
1607 	if (cd->sc_periph->periph_quirks & PQUIRK_NOCAPACITY) {
1608 		/*
1609 		 * the drive doesn't support the READ_CD_CAPACITY command
1610 		 * use a fake size
1611 		 */
1612 		cd->params.blksize = 2048;
1613 		cd->params.disksize = 400000;
1614 		cd->params.disksize512 = 1600000;
1615 		return (400000);
1616 	}
1617 
1618 	/*
1619 	 * make up a scsi command and ask the scsi driver to do
1620 	 * it for you.
1621 	 */
1622 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1623 	scsipi_cmd.opcode = READ_CD_CAPACITY;
1624 
1625 	/*
1626 	 * If the command works, interpret the result as a 4 byte
1627 	 * number of blocks and a blocksize
1628 	 */
1629 	if (scsipi_command(cd->sc_periph, NULL,
1630 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1631 	    (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL,
1632 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK) != 0)
1633 		return (0);
1634 
1635 	blksize = _4btol(rdcap.length);
1636 	if ((blksize < 512) || ((blksize & 511) != 0))
1637 		blksize = 2048;	/* some drives lie ! */
1638 	if (blksize != 2048) {
1639 		if (cd_setblksize(cd) == 0)
1640 			blksize = 2048;
1641 	}
1642 	cd->params.blksize = blksize;
1643 
1644 	size = _4btol(rdcap.addr) + 1;
1645 	if (size < 100)
1646 		size = 400000;	/* ditto */
1647 	cd->params.disksize = size;
1648 	cd->params.disksize512 = ((u_int64_t)cd->params.disksize * blksize) / DEV_BSIZE;
1649 
1650 	SC_DEBUG(cd->sc_periph, SCSIPI_DB2,
1651 	    ("cd_size: %d %ld\n", blksize, size));
1652 	return (size);
1653 }
1654 
1655 /*
1656  * Get scsi driver to send a "start playing" command
1657  */
1658 static int
1659 cd_play(struct cd_softc *cd, int blkno, int nblks)
1660 {
1661 	struct scsipi_play scsipi_cmd;
1662 
1663 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1664 	scsipi_cmd.opcode = PLAY;
1665 	_lto4b(blkno, scsipi_cmd.blk_addr);
1666 	_lto2b(nblks, scsipi_cmd.xfer_len);
1667 	return (scsipi_command(cd->sc_periph, NULL,
1668 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1669 	    0, 0, CDRETRIES, 30000, NULL, 0));
1670 }
1671 
1672 /*
1673  * Get scsi driver to send a "start playing" command
1674  */
1675 static int
1676 cd_play_tracks(struct cd_softc *cd, int strack, int sindex, int etrack,
1677     int eindex)
1678 {
1679 	struct cd_toc toc;
1680 	int error;
1681 
1682 	if (!etrack)
1683 		return (EIO);
1684 	if (strack > etrack)
1685 		return (EINVAL);
1686 
1687 	if ((error = cd_load_toc(cd, &toc, XS_CTL_DATA_ONSTACK)) != 0)
1688 		return (error);
1689 
1690 	if (++etrack > (toc.header.ending_track+1))
1691 		etrack = toc.header.ending_track+1;
1692 
1693 	strack -= toc.header.starting_track;
1694 	etrack -= toc.header.starting_track;
1695 	if (strack < 0)
1696 		return (EINVAL);
1697 
1698 	return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute,
1699 	    toc.entries[strack].addr.msf.second,
1700 	    toc.entries[strack].addr.msf.frame,
1701 	    toc.entries[etrack].addr.msf.minute,
1702 	    toc.entries[etrack].addr.msf.second,
1703 	    toc.entries[etrack].addr.msf.frame));
1704 }
1705 
1706 /*
1707  * Get scsi driver to send a "play msf" command
1708  */
1709 static int
1710 cd_play_msf(struct cd_softc *cd, int startm, int starts, int startf, int endm,
1711     int ends, int endf)
1712 {
1713 	struct scsipi_play_msf scsipi_cmd;
1714 
1715 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1716 	scsipi_cmd.opcode = PLAY_MSF;
1717 	scsipi_cmd.start_m = startm;
1718 	scsipi_cmd.start_s = starts;
1719 	scsipi_cmd.start_f = startf;
1720 	scsipi_cmd.end_m = endm;
1721 	scsipi_cmd.end_s = ends;
1722 	scsipi_cmd.end_f = endf;
1723 	return (scsipi_command(cd->sc_periph, NULL,
1724 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1725 	    0, 0, CDRETRIES, 30000, NULL, 0));
1726 }
1727 
1728 /*
1729  * Get scsi driver to send a "start up" command
1730  */
1731 static int
1732 cd_pause(struct cd_softc *cd, int go)
1733 {
1734 	struct scsipi_pause scsipi_cmd;
1735 
1736 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1737 	scsipi_cmd.opcode = PAUSE;
1738 	scsipi_cmd.resume = go & 0xff;
1739 	return (scsipi_command(cd->sc_periph, NULL,
1740 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1741 	    0, 0, CDRETRIES, 30000, NULL, 0));
1742 }
1743 
1744 /*
1745  * Get scsi driver to send a "RESET" command
1746  */
1747 static int
1748 cd_reset(struct cd_softc *cd)
1749 {
1750 
1751 	return (scsipi_command(cd->sc_periph, NULL, 0, 0, 0, 0,
1752 	    CDRETRIES, 30000, NULL, XS_CTL_RESET));
1753 }
1754 
1755 /*
1756  * Read subchannel
1757  */
1758 static int
1759 cd_read_subchannel(struct cd_softc *cd, int mode, int format, int track,
1760     struct cd_sub_channel_info *data, int len, int flags)
1761 {
1762 	struct scsipi_read_subchannel scsipi_cmd;
1763 
1764 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1765 	scsipi_cmd.opcode = READ_SUBCHANNEL;
1766 	if (mode == CD_MSF_FORMAT)
1767 		scsipi_cmd.byte2 |= CD_MSF;
1768 	scsipi_cmd.byte3 = SRS_SUBQ;
1769 	scsipi_cmd.subchan_format = format;
1770 	scsipi_cmd.track = track;
1771 	_lto2b(len, scsipi_cmd.data_len);
1772 	return (scsipi_command(cd->sc_periph, NULL,
1773 	    (struct scsipi_generic *)&scsipi_cmd,
1774 	    sizeof(struct scsipi_read_subchannel), (u_char *)data, len,
1775 	    CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT));
1776 }
1777 
1778 /*
1779  * Read table of contents
1780  */
1781 static int
1782 cd_read_toc(struct cd_softc *cd, int mode, int start, void *data, int len,
1783     int flags, int control)
1784 {
1785 	struct scsipi_read_toc scsipi_cmd;
1786 	int ntoc;
1787 
1788 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1789 #if 0
1790 	if (len != sizeof(struct ioc_toc_header))
1791 		ntoc = ((len) - sizeof(struct ioc_toc_header)) /
1792 		    sizeof(struct cd_toc_entry);
1793 	else
1794 #endif
1795 	ntoc = len;
1796 	scsipi_cmd.opcode = READ_TOC;
1797 	if (mode == CD_MSF_FORMAT)
1798 		scsipi_cmd.byte2 |= CD_MSF;
1799 	scsipi_cmd.from_track = start;
1800 	_lto2b(ntoc, scsipi_cmd.data_len);
1801 	scsipi_cmd.control = control;
1802 	return (scsipi_command(cd->sc_periph, NULL,
1803 	    (struct scsipi_generic *)&scsipi_cmd,
1804 	    sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES,
1805 	    30000, NULL, flags | XS_CTL_DATA_IN));
1806 }
1807 
1808 static int
1809 cd_load_toc(struct cd_softc *cd, struct cd_toc *toc, int flags)
1810 {
1811 	int ntracks, len, error;
1812 
1813 	if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header),
1814 	    flags, 0)) != 0)
1815 		return (error);
1816 
1817 	ntracks = toc->header.ending_track - toc->header.starting_track + 1;
1818 	len = (ntracks + 1) * sizeof(struct cd_toc_entry) +
1819 	    sizeof(toc->header);
1820 	if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len,
1821 	    flags, 0)) != 0)
1822 		return (error);
1823 	return (0);
1824 }
1825 
1826 /*
1827  * Get the scsi driver to send a full inquiry to the device and use the
1828  * results to fill out the disk parameter structure.
1829  */
1830 static int
1831 cd_get_parms(struct cd_softc *cd, int flags)
1832 {
1833 
1834 	/*
1835 	 * give a number of sectors so that sec * trks * cyls
1836 	 * is <= disk_size
1837 	 */
1838 	if (cd_size(cd, flags) == 0)
1839 		return (ENXIO);
1840 	return (0);
1841 }
1842 
1843 static int
1844 cdsize(dev_t dev)
1845 {
1846 
1847 	/* CD-ROMs are read-only. */
1848 	return (-1);
1849 }
1850 
1851 static int
1852 cddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1853 {
1854 
1855 	/* Not implemented. */
1856 	return (ENXIO);
1857 }
1858 
1859 #define	dvd_copy_key(dst, src)		memcpy((dst), (src), sizeof(dvd_key))
1860 #define	dvd_copy_challenge(dst, src)	memcpy((dst), (src), sizeof(dvd_challenge))
1861 
1862 static int
1863 dvd_auth(struct cd_softc *cd, dvd_authinfo *a)
1864 {
1865 	struct scsipi_generic cmd;
1866 	u_int8_t buf[20];
1867 	int error;
1868 
1869 	memset(cmd.bytes, 0, 15);
1870 	memset(buf, 0, sizeof(buf));
1871 
1872 	switch (a->type) {
1873 	case DVD_LU_SEND_AGID:
1874 		cmd.opcode = GPCMD_REPORT_KEY;
1875 		cmd.bytes[8] = 8;
1876 		cmd.bytes[9] = 0 | (0 << 6);
1877 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 8,
1878 		    CDRETRIES, 30000, NULL,
1879 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1880 		if (error)
1881 			return (error);
1882 		a->lsa.agid = buf[7] >> 6;
1883 		return (0);
1884 
1885 	case DVD_LU_SEND_CHALLENGE:
1886 		cmd.opcode = GPCMD_REPORT_KEY;
1887 		cmd.bytes[8] = 16;
1888 		cmd.bytes[9] = 1 | (a->lsc.agid << 6);
1889 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 16,
1890 		    CDRETRIES, 30000, NULL,
1891 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1892 		if (error)
1893 			return (error);
1894 		dvd_copy_challenge(a->lsc.chal, &buf[4]);
1895 		return (0);
1896 
1897 	case DVD_LU_SEND_KEY1:
1898 		cmd.opcode = GPCMD_REPORT_KEY;
1899 		cmd.bytes[8] = 12;
1900 		cmd.bytes[9] = 2 | (a->lsk.agid << 6);
1901 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 12,
1902 		    CDRETRIES, 30000, NULL,
1903 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1904 		if (error)
1905 			return (error);
1906 		dvd_copy_key(a->lsk.key, &buf[4]);
1907 		return (0);
1908 
1909 	case DVD_LU_SEND_TITLE_KEY:
1910 		cmd.opcode = GPCMD_REPORT_KEY;
1911 		_lto4b(a->lstk.lba, &cmd.bytes[1]);
1912 		cmd.bytes[8] = 12;
1913 		cmd.bytes[9] = 4 | (a->lstk.agid << 6);
1914 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 12,
1915 		    CDRETRIES, 30000, NULL,
1916 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1917 		if (error)
1918 			return (error);
1919 		a->lstk.cpm = (buf[4] >> 7) & 1;
1920 		a->lstk.cp_sec = (buf[4] >> 6) & 1;
1921 		a->lstk.cgms = (buf[4] >> 4) & 3;
1922 		dvd_copy_key(a->lstk.title_key, &buf[5]);
1923 		return (0);
1924 
1925 	case DVD_LU_SEND_ASF:
1926 		cmd.opcode = GPCMD_REPORT_KEY;
1927 		cmd.bytes[8] = 8;
1928 		cmd.bytes[9] = 5 | (a->lsasf.agid << 6);
1929 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 8,
1930 		    CDRETRIES, 30000, NULL,
1931 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1932 		if (error)
1933 			return (error);
1934 		a->lsasf.asf = buf[7] & 1;
1935 		return (0);
1936 
1937 	case DVD_HOST_SEND_CHALLENGE:
1938 		cmd.opcode = GPCMD_SEND_KEY;
1939 		cmd.bytes[8] = 16;
1940 		cmd.bytes[9] = 1 | (a->hsc.agid << 6);
1941 		buf[1] = 14;
1942 		dvd_copy_challenge(&buf[4], a->hsc.chal);
1943 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 16,
1944 		    CDRETRIES, 30000, NULL,
1945 		    XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK);
1946 		if (error)
1947 			return (error);
1948 		a->type = DVD_LU_SEND_KEY1;
1949 		return (0);
1950 
1951 	case DVD_HOST_SEND_KEY2:
1952 		cmd.opcode = GPCMD_SEND_KEY;
1953 		cmd.bytes[8] = 12;
1954 		cmd.bytes[9] = 3 | (a->hsk.agid << 6);
1955 		buf[1] = 10;
1956 		dvd_copy_key(&buf[4], a->hsk.key);
1957 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 12,
1958 		    CDRETRIES, 30000, NULL,
1959 		    XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK);
1960 		if (error) {
1961 			a->type = DVD_AUTH_FAILURE;
1962 			return (error);
1963 		}
1964 		a->type = DVD_AUTH_ESTABLISHED;
1965 		return (0);
1966 
1967 	case DVD_INVALIDATE_AGID:
1968 		cmd.opcode = GPCMD_REPORT_KEY;
1969 		cmd.bytes[9] = 0x3f | (a->lsa.agid << 6);
1970 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 16,
1971 		    CDRETRIES, 30000, NULL, 0);
1972 		if (error)
1973 			return (error);
1974 		return (0);
1975 
1976 	case DVD_LU_SEND_RPC_STATE:
1977 		cmd.opcode = GPCMD_REPORT_KEY;
1978 		cmd.bytes[8] = 8;
1979 		cmd.bytes[9] = 8 | (0 << 6);
1980 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 8,
1981 		    CDRETRIES, 30000, NULL,
1982 		    XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
1983 		if (error)
1984 			return (error);
1985 		a->lrpcs.type = (buf[4] >> 6) & 3;
1986 		a->lrpcs.vra = (buf[4] >> 3) & 7;
1987 		a->lrpcs.ucca = (buf[4]) & 7;
1988 		a->lrpcs.region_mask = buf[5];
1989 		a->lrpcs.rpc_scheme = buf[6];
1990 		return (0);
1991 
1992 	case DVD_HOST_SEND_RPC_STATE:
1993 		cmd.opcode = GPCMD_SEND_KEY;
1994 		cmd.bytes[8] = 8;
1995 		cmd.bytes[9] = 6 | (0 << 6);
1996 		buf[1] = 6;
1997 		buf[4] = a->hrpcs.pdrc;
1998 		error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 8,
1999 		    CDRETRIES, 30000, NULL,
2000 		    XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK);
2001 		if (error)
2002 			return (error);
2003 		return (0);
2004 
2005 	default:
2006 		return (ENOTTY);
2007 	}
2008 }
2009 
2010 static int
2011 dvd_read_physical(struct cd_softc *cd, dvd_struct *s)
2012 {
2013 	struct scsipi_generic cmd;
2014 	u_int8_t buf[4 + 4 * 20], *bufp;
2015 	int error;
2016 	struct dvd_layer *layer;
2017 	int i;
2018 
2019 	memset(cmd.bytes, 0, 15);
2020 	memset(buf, 0, sizeof(buf));
2021 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2022 	cmd.bytes[6] = s->type;
2023 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2024 
2025 	cmd.bytes[5] = s->physical.layer_num;
2026 	error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, sizeof(buf),
2027 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2028 	if (error)
2029 		return (error);
2030 	for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4;
2031 	     i++, bufp += 20, layer++) {
2032 		memset(layer, 0, sizeof(*layer));
2033                 layer->book_version = bufp[0] & 0xf;
2034                 layer->book_type = bufp[0] >> 4;
2035                 layer->min_rate = bufp[1] & 0xf;
2036                 layer->disc_size = bufp[1] >> 4;
2037                 layer->layer_type = bufp[2] & 0xf;
2038                 layer->track_path = (bufp[2] >> 4) & 1;
2039                 layer->nlayers = (bufp[2] >> 5) & 3;
2040                 layer->track_density = bufp[3] & 0xf;
2041                 layer->linear_density = bufp[3] >> 4;
2042                 layer->start_sector = _4btol(&bufp[4]);
2043                 layer->end_sector = _4btol(&bufp[8]);
2044                 layer->end_sector_l0 = _4btol(&bufp[12]);
2045                 layer->bca = bufp[16] >> 7;
2046 	}
2047 	return (0);
2048 }
2049 
2050 static int
2051 dvd_read_copyright(struct cd_softc *cd, dvd_struct *s)
2052 {
2053 	struct scsipi_generic cmd;
2054 	u_int8_t buf[8];
2055 	int error;
2056 
2057 	memset(cmd.bytes, 0, 15);
2058 	memset(buf, 0, sizeof(buf));
2059 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2060 	cmd.bytes[6] = s->type;
2061 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2062 
2063 	cmd.bytes[5] = s->copyright.layer_num;
2064 	error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, sizeof(buf),
2065 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2066 	if (error)
2067 		return (error);
2068 	s->copyright.cpst = buf[4];
2069 	s->copyright.rmi = buf[5];
2070 	return (0);
2071 }
2072 
2073 static int
2074 dvd_read_disckey(struct cd_softc *cd, dvd_struct *s)
2075 {
2076 	struct scsipi_generic cmd;
2077 	u_int8_t *buf;
2078 	int error;
2079 
2080 	buf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO);
2081 	if (buf == NULL)
2082 		return EIO;
2083 	memset(cmd.bytes, 0, 15);
2084 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2085 	cmd.bytes[6] = s->type;
2086 	_lto2b(4 + 2048, &cmd.bytes[7]);
2087 
2088 	cmd.bytes[9] = s->disckey.agid << 6;
2089 	error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 4 + 2048,
2090 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2091 	if (error == 0)
2092 		memcpy(s->disckey.value, &buf[4], 2048);
2093 	free(buf, M_TEMP);
2094 	return error;
2095 }
2096 
2097 static int
2098 dvd_read_bca(struct cd_softc *cd, dvd_struct *s)
2099 {
2100 	struct scsipi_generic cmd;
2101 	u_int8_t buf[4 + 188];
2102 	int error;
2103 
2104 	memset(cmd.bytes, 0, 15);
2105 	memset(buf, 0, sizeof(buf));
2106 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2107 	cmd.bytes[6] = s->type;
2108 	_lto2b(sizeof(buf), &cmd.bytes[7]);
2109 
2110 	error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, sizeof(buf),
2111 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2112 	if (error)
2113 		return (error);
2114 	s->bca.len = _2btol(&buf[0]);
2115 	if (s->bca.len < 12 || s->bca.len > 188)
2116 		return (EIO);
2117 	memcpy(s->bca.value, &buf[4], s->bca.len);
2118 	return (0);
2119 }
2120 
2121 static int
2122 dvd_read_manufact(struct cd_softc *cd, dvd_struct *s)
2123 {
2124 	struct scsipi_generic cmd;
2125 	u_int8_t *buf;
2126 	int error;
2127 
2128 	buf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO);
2129 	if (buf == NULL)
2130 		return (EIO);
2131 	memset(cmd.bytes, 0, 15);
2132 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
2133 	cmd.bytes[6] = s->type;
2134 	_lto2b(4 + 2048, &cmd.bytes[7]);
2135 
2136 	error = scsipi_command(cd->sc_periph, NULL, &cmd, 12, buf, 4 + 2048,
2137 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK);
2138 	if (error == 0) {
2139 		s->manufact.len = _2btol(&buf[0]);
2140 		if (s->manufact.len >= 0 && s->manufact.len <= 2048)
2141 			memcpy(s->manufact.value, &buf[4], s->manufact.len);
2142 		else
2143 			error = EIO;
2144 	}
2145 	free(buf, M_TEMP);
2146 	return error;
2147 }
2148 
2149 static int
2150 dvd_read_struct(struct cd_softc *cd, dvd_struct *s)
2151 {
2152 
2153 	switch (s->type) {
2154 	case DVD_STRUCT_PHYSICAL:
2155 		return (dvd_read_physical(cd, s));
2156 	case DVD_STRUCT_COPYRIGHT:
2157 		return (dvd_read_copyright(cd, s));
2158 	case DVD_STRUCT_DISCKEY:
2159 		return (dvd_read_disckey(cd, s));
2160 	case DVD_STRUCT_BCA:
2161 		return (dvd_read_bca(cd, s));
2162 	case DVD_STRUCT_MANUFACT:
2163 		return (dvd_read_manufact(cd, s));
2164 	default:
2165 		return (EINVAL);
2166 	}
2167 }
2168 
2169 static int
2170 cd_mode_sense(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size,
2171     int page, int flags, int *big)
2172 {
2173 
2174 	if (cd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) {
2175 		*big = 1;
2176 		return scsipi_mode_sense_big(cd->sc_periph, byte2, page, sense,
2177 		    size + sizeof(struct scsipi_mode_header_big),
2178 		    flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000);
2179 	} else {
2180 		*big = 0;
2181 		return scsipi_mode_sense(cd->sc_periph, byte2, page, sense,
2182 		    size + sizeof(struct scsipi_mode_header),
2183 		    flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000);
2184 	}
2185 }
2186 
2187 static int
2188 cd_mode_select(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size,
2189     int flags, int big)
2190 {
2191 
2192 	if (big) {
2193 		struct scsipi_mode_header_big *header = sense;
2194 
2195 		_lto2b(0, header->data_length);
2196 		return scsipi_mode_select_big(cd->sc_periph, byte2, sense,
2197 		    size + sizeof(struct scsipi_mode_header_big),
2198 		    flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000);
2199 	} else {
2200 		struct scsipi_mode_header *header = sense;
2201 
2202 		header->data_length = 0;
2203 		return scsipi_mode_select(cd->sc_periph, byte2, sense,
2204 		    size + sizeof(struct scsipi_mode_header),
2205 		    flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000);
2206 	}
2207 }
2208 
2209 static int
2210 cd_set_pa_immed(struct cd_softc *cd, int flags)
2211 {
2212 	struct {
2213 		union {
2214 			struct scsipi_mode_header small;
2215 			struct scsipi_mode_header_big big;
2216 		} header;
2217 		struct cd_audio_page page;
2218 	} data;
2219 	int error;
2220 	uint8_t oflags;
2221 	int big, byte2;
2222 	struct cd_audio_page *page;
2223 
2224 	byte2 = SMS_DBD;
2225 try_again:
2226 	if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page),
2227 	    AUDIO_PAGE, flags, &big)) != 0) {
2228 		if (byte2 == SMS_DBD) {
2229 			/* Device may not understand DBD; retry without */
2230 			byte2 = 0;
2231 			goto try_again;
2232 		}
2233 		return (error);
2234 	}
2235 
2236 	if (big)
2237 		page = (void *)((u_long)&data.header.big +
2238 				sizeof data.header.big +
2239 				_2btol(data.header.big.blk_desc_len));
2240 	else
2241 		page = (void *)((u_long)&data.header.small +
2242 				sizeof data.header.small +
2243 				data.header.small.blk_desc_len);
2244 
2245 	oflags = page->flags;
2246 	page->flags &= ~CD_PA_SOTC;
2247 	page->flags |= CD_PA_IMMED;
2248 	if (oflags == page->flags)
2249 		return (0);
2250 
2251 	return (cd_mode_select(cd, SMS_PF, &data,
2252 	    sizeof(struct scsipi_mode_page_header) + page->pg_length,
2253 	    flags, big));
2254 }
2255 
2256 static int
2257 cd_setchan(struct cd_softc *cd, int p0, int p1, int p2, int p3, int flags)
2258 {
2259 	struct {
2260 		union {
2261 			struct scsipi_mode_header small;
2262 			struct scsipi_mode_header_big big;
2263 		} header;
2264 		struct cd_audio_page page;
2265 	} data;
2266 	int error;
2267 	int big, byte2;
2268 	struct cd_audio_page *page;
2269 
2270 	byte2 = SMS_DBD;
2271 try_again:
2272 	if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page),
2273 	    AUDIO_PAGE, flags, &big)) != 0) {
2274 		if (byte2 == SMS_DBD) {
2275 			/* Device may not understand DBD; retry without */
2276 			byte2 = 0;
2277 			goto try_again;
2278 		}
2279 		return (error);
2280 	}
2281 
2282 	if (big)
2283 		page = (void *)((u_long)&data.header.big +
2284 				sizeof data.header.big +
2285 				_2btol(data.header.big.blk_desc_len));
2286 	else
2287 		page = (void *)((u_long)&data.header.small +
2288 				sizeof data.header.small +
2289 				data.header.small.blk_desc_len);
2290 
2291 	page->port[0].channels = p0;
2292 	page->port[1].channels = p1;
2293 	page->port[2].channels = p2;
2294 	page->port[3].channels = p3;
2295 
2296 	return (cd_mode_select(cd, SMS_PF, &data,
2297 	    sizeof(struct scsipi_mode_page_header) + page->pg_length,
2298 	    flags, big));
2299 }
2300 
2301 static int
2302 cd_getvol(struct cd_softc *cd, struct ioc_vol *arg, int flags)
2303 {
2304 	struct {
2305 		union {
2306 			struct scsipi_mode_header small;
2307 			struct scsipi_mode_header_big big;
2308 		} header;
2309 		struct cd_audio_page page;
2310 	} data;
2311 	int error;
2312 	int big, byte2;
2313 	struct cd_audio_page *page;
2314 
2315 	byte2 = SMS_DBD;
2316 try_again:
2317 	if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page),
2318 	    AUDIO_PAGE, flags, &big)) != 0) {
2319 		if (byte2 == SMS_DBD) {
2320 			/* Device may not understand DBD; retry without */
2321 			byte2 = 0;
2322 			goto try_again;
2323 		}
2324 		return (error);
2325 	}
2326 
2327 	if (big)
2328 		page = (void *)((u_long)&data.header.big +
2329 				sizeof data.header.big +
2330 				_2btol(data.header.big.blk_desc_len));
2331 	else
2332 		page = (void *)((u_long)&data.header.small +
2333 				sizeof data.header.small +
2334 				data.header.small.blk_desc_len);
2335 
2336 	arg->vol[0] = page->port[0].volume;
2337 	arg->vol[1] = page->port[1].volume;
2338 	arg->vol[2] = page->port[2].volume;
2339 	arg->vol[3] = page->port[3].volume;
2340 
2341 	return (0);
2342 }
2343 
2344 static int
2345 cd_setvol(struct cd_softc *cd, const struct ioc_vol *arg, int flags)
2346 {
2347 	struct {
2348 		union {
2349 			struct scsipi_mode_header small;
2350 			struct scsipi_mode_header_big big;
2351 		} header;
2352 		struct cd_audio_page page;
2353 	} data, mask;
2354 	int error;
2355 	int big, byte2;
2356 	struct cd_audio_page *page, *page2;
2357 
2358 	byte2 = SMS_DBD;
2359 try_again:
2360 	if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page),
2361 	    AUDIO_PAGE, flags, &big)) != 0) {
2362 		if (byte2 == SMS_DBD) {
2363 			/* Device may not understand DBD; retry without */
2364 			byte2 = 0;
2365 			goto try_again;
2366 		}
2367 		return (error);
2368 	}
2369 	if ((error = cd_mode_sense(cd, byte2, &mask, sizeof(mask.page),
2370 	    AUDIO_PAGE|SMS_PAGE_CTRL_CHANGEABLE, flags, &big)) != 0)
2371 		return (error);
2372 
2373 	if (big) {
2374 		page = (void *)((u_long)&data.header.big +
2375 				sizeof data.header.big +
2376 				_2btol(data.header.big.blk_desc_len));
2377 		page2 = (void *)((u_long)&mask.header.big +
2378 				sizeof mask.header.big +
2379 				_2btol(mask.header.big.blk_desc_len));
2380 	} else {
2381 		page = (void *)((u_long)&data.header.small +
2382 				sizeof data.header.small +
2383 				data.header.small.blk_desc_len);
2384 		page2 = (void *)((u_long)&mask.header.small +
2385 				sizeof mask.header.small +
2386 				mask.header.small.blk_desc_len);
2387 	}
2388 
2389 	page->port[0].volume = arg->vol[0] & page2->port[0].volume;
2390 	page->port[1].volume = arg->vol[1] & page2->port[1].volume;
2391 	page->port[2].volume = arg->vol[2] & page2->port[2].volume;
2392 	page->port[3].volume = arg->vol[3] & page2->port[3].volume;
2393 
2394 	page->port[0].channels = CHANNEL_0;
2395 	page->port[1].channels = CHANNEL_1;
2396 
2397 	return (cd_mode_select(cd, SMS_PF, &data,
2398 	    sizeof(struct scsipi_mode_page_header) + page->pg_length,
2399 	    flags, big));
2400 }
2401 
2402 static int
2403 cd_load_unload(struct cd_softc *cd, struct ioc_load_unload *args)
2404 {
2405 	struct scsipi_load_unload scsipi_cmd;
2406 
2407 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
2408 	scsipi_cmd.opcode = LOAD_UNLOAD;
2409 	scsipi_cmd.options = args->options;    /* ioctl uses MMC values */
2410 	scsipi_cmd.slot = args->slot;
2411 
2412 	return (scsipi_command(cd->sc_periph, NULL,
2413 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
2414 	    0, 0, CDRETRIES, 200000, NULL, 0));
2415 }
2416 
2417 static int
2418 cd_setblksize(struct cd_softc *cd)
2419 {
2420 	struct {
2421 		union {
2422 			struct scsipi_mode_header small;
2423 			struct scsipi_mode_header_big big;
2424 		} header;
2425 		struct scsi_blk_desc blk_desc;
2426 	} data;
2427 	int error;
2428 	int big, bsize;
2429 	struct scsi_blk_desc *bdesc;
2430 
2431 	if ((error = cd_mode_sense(cd, 0, &data, sizeof(data.blk_desc), 0, 0,
2432 	    &big)) != 0)
2433 		return (error);
2434 
2435 	if (big) {
2436 		bdesc = (void *)(&data.header.big + 1);
2437 		bsize = _2btol(data.header.big.blk_desc_len);
2438 	} else {
2439 		bdesc = (void *)(&data.header.small + 1);
2440 		bsize = data.header.small.blk_desc_len;
2441 	}
2442 
2443 	if (bsize == 0) {
2444 printf("cd_setblksize: trying to change bsize, but no blk_desc\n");
2445 		return (EINVAL);
2446 	}
2447 	if (_3btol(bdesc->blklen) == 2048) {
2448 printf("cd_setblksize: trying to change bsize, but blk_desc is correct\n");
2449 		return (EINVAL);
2450 	}
2451 
2452 	_lto3b(2048, bdesc->blklen);
2453 
2454 	return (cd_mode_select(cd, SMS_PF, &data, sizeof(data.blk_desc), 0,
2455 	    big));
2456 }
2457