xref: /netbsd-src/sys/dev/scsipi/cd.c (revision 95d875fb90b1458e4f1de6950286ddcd6644bc61)
1 /*	$NetBSD: cd.c,v 1.134 1999/11/03 20:50:18 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 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 "rnd.h"
57 
58 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/file.h>
63 #include <sys/stat.h>
64 #include <sys/ioctl.h>
65 #include <sys/buf.h>
66 #include <sys/uio.h>
67 #include <sys/malloc.h>
68 #include <sys/errno.h>
69 #include <sys/device.h>
70 #include <sys/disklabel.h>
71 #include <sys/disk.h>
72 #include <sys/cdio.h>
73 #include <sys/dvdio.h>
74 #include <sys/scsiio.h>
75 #include <sys/proc.h>
76 #include <sys/conf.h>
77 #include <sys/vnode.h>
78 #if NRND > 0
79 #include <sys/rnd.h>
80 #endif
81 
82 #include <dev/scsipi/scsipi_all.h>
83 #include <dev/scsipi/scsipi_cd.h>
84 #include <dev/scsipi/scsipi_disk.h>	/* rw_big and start_stop come */
85 					/* from there */
86 #include <dev/scsipi/scsi_disk.h>	/* rw comes from there */
87 #include <dev/scsipi/scsipiconf.h>
88 #include <dev/scsipi/cdvar.h>
89 
90 #include "cd.h"		/* NCD_SCSIBUS and NCD_ATAPIBUS come from here */
91 
92 #define	CDOUTSTANDING	4
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((void *));
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 u_long	cd_size __P((struct cd_softc *, int));
118 void	lba2msf __P((u_long, u_char *, u_char *, u_char *));
119 u_long	msf2lba __P((u_char, u_char, u_char));
120 int	cd_play __P((struct cd_softc *, int, int));
121 int	cd_play_tracks __P((struct cd_softc *, int, int, int, int));
122 int	cd_play_msf __P((struct cd_softc *, int, int, int, int, int, int));
123 int	cd_pause __P((struct cd_softc *, int));
124 int	cd_reset __P((struct cd_softc *));
125 int	cd_read_subchannel __P((struct cd_softc *, int, int, int,
126 	    struct cd_sub_channel_info *, int));
127 int	cd_read_toc __P((struct cd_softc *, int, int, void *, int, int));
128 int	cd_get_parms __P((struct cd_softc *, int));
129 int	cd_load_toc __P((struct cd_softc *, struct cd_toc *));
130 int	dvd_auth __P((struct cd_softc *, dvd_authinfo *));
131 int	dvd_read_physical __P((struct cd_softc *, dvd_struct *));
132 int	dvd_read_copyright __P((struct cd_softc *, dvd_struct *));
133 int	dvd_read_disckey __P((struct cd_softc *, dvd_struct *));
134 int	dvd_read_bca __P((struct cd_softc *, dvd_struct *));
135 int	dvd_read_manufact __P((struct cd_softc *, dvd_struct *));
136 int	dvd_read_struct __P((struct cd_softc *, dvd_struct *));
137 
138 extern struct cfdriver cd_cd;
139 
140 struct dkdriver cddkdriver = { cdstrategy };
141 
142 struct scsipi_device cd_switch = {
143 	NULL,			/* use default error handler */
144 	cdstart,		/* we have a queue, which is started by this */
145 	NULL,			/* we do not have an async handler */
146 	cddone,			/* deal with stats at interrupt time */
147 };
148 
149 /*
150  * The routine called by the low level scsi routine when it discovers
151  * A device suitable for this driver
152  */
153 void
154 cdattach(parent, cd, sc_link, ops)
155 	struct device *parent;
156 	struct cd_softc *cd;
157 	struct scsipi_link *sc_link;
158 	const struct cd_ops *ops;
159 {
160 	SC_DEBUG(sc_link, SDEV_DB2, ("cdattach: "));
161 
162 	/*
163 	 * Store information needed to contact our base driver
164 	 */
165 	cd->sc_link = sc_link;
166 	cd->sc_ops = ops;
167 	sc_link->device = &cd_switch;
168 	sc_link->device_softc = cd;
169 	if (sc_link->openings > CDOUTSTANDING)
170 		sc_link->openings = CDOUTSTANDING;
171 
172 	/*
173 	 * Initialize and attach the disk structure.
174 	 */
175   	cd->sc_dk.dk_driver = &cddkdriver;
176 	cd->sc_dk.dk_name = cd->sc_dev.dv_xname;
177 	disk_attach(&cd->sc_dk);
178 
179 #if !defined(__i386__) && !defined(__vax__)
180 	dk_establish(&cd->sc_dk, &cd->sc_dev);		/* XXX */
181 #endif
182 
183 	printf("\n");
184 
185 #if NRND > 0
186 	rnd_attach_source(&cd->rnd_source, cd->sc_dev.dv_xname,
187 			  RND_TYPE_DISK, 0);
188 #endif
189 }
190 
191 int
192 cdactivate(self, act)
193 	struct device *self;
194 	enum devact act;
195 {
196 	int rv = 0;
197 
198 	switch (act) {
199 	case DVACT_ACTIVATE:
200 		rv = EOPNOTSUPP;
201 		break;
202 
203 	case DVACT_DEACTIVATE:
204 		/*
205 		 * Nothing to do; we key off the device's DVF_ACTIVE.
206 		 */
207 		break;
208 	}
209 	return (rv);
210 }
211 
212 int
213 cddetach(self, flags)
214 	struct device *self;
215 	int flags;
216 {
217 	struct cd_softc *cd = (struct cd_softc *) self;
218 	struct buf *bp;
219 	int s, bmaj, cmaj, mn;
220 
221 	/* locate the major number */
222 	for (bmaj = 0; bmaj <= nblkdev; bmaj++)
223 		if (bdevsw[bmaj].d_open == cdopen)
224 			break;
225 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
226 		if (cdevsw[cmaj].d_open == cdopen)
227 			break;
228 
229 	s = splbio();
230 
231 	/* Kill off any queued buffers. */
232 	while ((bp = cd->buf_queue.b_actf) != NULL) {
233 		cd->buf_queue.b_actf = bp->b_actf;
234 		bp->b_error = EIO;
235 		bp->b_flags |= B_ERROR;
236 		bp->b_resid = bp->b_bcount;
237 		biodone(bp);
238 	}
239 
240 	/* Kill off any pending commands. */
241 	scsipi_kill_pending(cd->sc_link);
242 
243 	splx(s);
244 
245 	/* Nuke the the vnodes for any open instances */
246 	mn = CDMINOR(self->dv_unit, 0);
247 	vdevgone(bmaj, mn, mn + (MAXPARTITIONS - 1), VBLK);
248 	vdevgone(cmaj, mn, mn + (MAXPARTITIONS - 1), VCHR);
249 
250 	/* Detach from the disk list. */
251 	disk_detach(&cd->sc_dk);
252 
253 #if 0
254 	/* Get rid of the shutdown hook. */
255 	if (cd->sc_sdhook != NULL)
256 		shutdownhook_disestablish(cd->sc_sdhook);
257 #endif
258 
259 #if NRND > 0
260 	/* Unhook the entropy source. */
261 	rnd_detach_source(&cd->rnd_source);
262 #endif
263 
264 	return (0);
265 }
266 
267 /*
268  * Wait interruptibly for an exclusive lock.
269  *
270  * XXX
271  * Several drivers do this; it should be abstracted and made MP-safe.
272  */
273 int
274 cdlock(cd)
275 	struct cd_softc *cd;
276 {
277 	int error;
278 
279 	while ((cd->flags & CDF_LOCKED) != 0) {
280 		cd->flags |= CDF_WANTED;
281 		if ((error = tsleep(cd, PRIBIO | PCATCH, "cdlck", 0)) != 0)
282 			return (error);
283 	}
284 	cd->flags |= CDF_LOCKED;
285 	return (0);
286 }
287 
288 /*
289  * Unlock and wake up any waiters.
290  */
291 void
292 cdunlock(cd)
293 	struct cd_softc *cd;
294 {
295 
296 	cd->flags &= ~CDF_LOCKED;
297 	if ((cd->flags & CDF_WANTED) != 0) {
298 		cd->flags &= ~CDF_WANTED;
299 		wakeup(cd);
300 	}
301 }
302 
303 /*
304  * open the device. Make sure the partition info is a up-to-date as can be.
305  */
306 int
307 cdopen(dev, flag, fmt, p)
308 	dev_t dev;
309 	int flag, fmt;
310 	struct proc *p;
311 {
312 	struct cd_softc *cd;
313 	struct scsipi_link *sc_link;
314 	int unit, part;
315 	int error;
316 
317 	unit = CDUNIT(dev);
318 	if (unit >= cd_cd.cd_ndevs)
319 		return (ENXIO);
320 	cd = cd_cd.cd_devs[unit];
321 	if (cd == NULL)
322 		return (ENXIO);
323 
324 	sc_link = cd->sc_link;
325 	part = CDPART(dev);
326 
327 	SC_DEBUG(sc_link, SDEV_DB1,
328 	    ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
329 	    cd_cd.cd_ndevs, CDPART(dev)));
330 
331 	/*
332 	 * If this is the first open of this device, add a reference
333 	 * to the adapter.
334 	 */
335 	if (cd->sc_dk.dk_openmask == 0 &&
336 	    (error = scsipi_adapter_addref(sc_link)) != 0)
337 		return (error);
338 
339 	if ((error = cdlock(cd)) != 0)
340 		goto bad4;
341 
342 	if ((sc_link->flags & SDEV_OPEN) != 0) {
343 		/*
344 		 * If any partition is open, but the disk has been invalidated,
345 		 * disallow further opens.
346 		 */
347 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0 &&
348 			(part != RAW_PART || fmt != S_IFCHR )) {
349 			error = EIO;
350 			goto bad3;
351 		}
352 	} else {
353 		/* Check that it is still responding and ok. */
354 		error = scsipi_test_unit_ready(sc_link,
355 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
356 		    XS_CTL_IGNORE_NOT_READY);
357 		SC_DEBUG(sc_link, SDEV_DB1,
358 		    ("cdopen: scsipi_test_unit_ready, error=%d\n", error));
359 		if (error)
360 			goto bad3;
361 
362 		/*
363 		 * Start the pack spinning if necessary. Always allow the
364 		 * raw parition to be opened, for raw IOCTLs. Data transfers
365 		 * will check for SDEV_MEDIA_LOADED.
366 		 */
367 		error = scsipi_start(sc_link, SSS_START,
368 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
369 		    XS_CTL_SILENT);
370 		SC_DEBUG(sc_link, SDEV_DB1,
371 		    ("cdopen: scsipi_start, error=%d\n", error));
372 		if (error) {
373 			if (part != RAW_PART || fmt != S_IFCHR)
374 				goto bad3;
375 			else
376 				goto out;
377 		}
378 
379 		sc_link->flags |= SDEV_OPEN;
380 
381 		/* Lock the pack in. */
382 		error = scsipi_prevent(sc_link, PR_PREVENT,
383 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
384 		SC_DEBUG(sc_link, SDEV_DB1,
385 		    ("cdopen: scsipi_prevent, error=%d\n", error));
386 		if (error)
387 			goto bad;
388 
389 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
390 			sc_link->flags |= SDEV_MEDIA_LOADED;
391 
392 			/* Load the physical device parameters. */
393 			if (cd_get_parms(cd, 0) != 0) {
394 				error = ENXIO;
395 				goto bad2;
396 			}
397 			SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
398 
399 			/* Fabricate a disk label. */
400 			cdgetdisklabel(cd);
401 			SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated "));
402 		}
403 	}
404 
405 	/* Check that the partition exists. */
406 	if (part != RAW_PART &&
407 	    (part >= cd->sc_dk.dk_label->d_npartitions ||
408 	    cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
409 		error = ENXIO;
410 		goto bad;
411 	}
412 
413 out:	/* Insure only one open at a time. */
414 	switch (fmt) {
415 	case S_IFCHR:
416 		cd->sc_dk.dk_copenmask |= (1 << part);
417 		break;
418 	case S_IFBLK:
419 		cd->sc_dk.dk_bopenmask |= (1 << part);
420 		break;
421 	}
422 	cd->sc_dk.dk_openmask =
423 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
424 
425 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
426 	cdunlock(cd);
427 	return (0);
428 
429 bad2:
430 	sc_link->flags &= ~SDEV_MEDIA_LOADED;
431 
432 bad:
433 	if (cd->sc_dk.dk_openmask == 0) {
434 		scsipi_prevent(sc_link, PR_ALLOW,
435 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
436 		sc_link->flags &= ~SDEV_OPEN;
437 	}
438 
439 bad3:
440 	cdunlock(cd);
441 bad4:
442 	if (cd->sc_dk.dk_openmask == 0)
443 		scsipi_adapter_delref(sc_link);
444 	return (error);
445 }
446 
447 /*
448  * close the device.. only called if we are the LAST
449  * occurence of an open device
450  */
451 int
452 cdclose(dev, flag, fmt, p)
453 	dev_t dev;
454 	int flag, fmt;
455 	struct proc *p;
456 {
457 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
458 	int part = CDPART(dev);
459 	int error;
460 
461 	if ((error = cdlock(cd)) != 0)
462 		return (error);
463 
464 	switch (fmt) {
465 	case S_IFCHR:
466 		cd->sc_dk.dk_copenmask &= ~(1 << part);
467 		break;
468 	case S_IFBLK:
469 		cd->sc_dk.dk_bopenmask &= ~(1 << part);
470 		break;
471 	}
472 	cd->sc_dk.dk_openmask =
473 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
474 
475 	if (cd->sc_dk.dk_openmask == 0) {
476 		scsipi_wait_drain(cd->sc_link);
477 
478 		scsipi_prevent(cd->sc_link, PR_ALLOW,
479 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
480 		    XS_CTL_IGNORE_NOT_READY);
481 		cd->sc_link->flags &= ~SDEV_OPEN;
482 
483 		scsipi_wait_drain(cd->sc_link);
484 
485 		scsipi_adapter_delref(cd->sc_link);
486 	}
487 
488 	cdunlock(cd);
489 	return (0);
490 }
491 
492 /*
493  * Actually translate the requested transfer into one the physical driver can
494  * understand.  The transfer is described by a buf and will include only one
495  * physical transfer.
496  */
497 void
498 cdstrategy(bp)
499 	struct buf *bp;
500 {
501 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
502 	int opri;
503 
504 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdstrategy "));
505 	SC_DEBUG(cd->sc_link, SDEV_DB1,
506 	    ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
507 	/*
508 	 * If the device has been made invalid, error out
509 	 * maybe the media changed
510 	 */
511 	if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
512 		if (cd->sc_link->flags & SDEV_OPEN)
513 			bp->b_error = EIO;
514 		else
515 			bp->b_error = ENODEV;
516 		goto bad;
517 	}
518 	/*
519 	 * The transfer must be a whole number of blocks, offset must not
520 	 * be negative.
521 	 */
522 	if ((bp->b_bcount % cd->sc_dk.dk_label->d_secsize) != 0 ||
523 	    bp->b_blkno < 0 ) {
524 		bp->b_error = EINVAL;
525 		goto bad;
526 	}
527 	/*
528 	 * If it's a null transfer, return immediately
529 	 */
530 	if (bp->b_bcount == 0)
531 		goto done;
532 
533 	/*
534 	 * Do bounds checking, adjust transfer. if error, process.
535 	 * If end of partition, just return.
536 	 */
537 	if (CDPART(bp->b_dev) != RAW_PART &&
538 	    bounds_check_with_label(bp, cd->sc_dk.dk_label,
539 	    (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0)
540 		goto done;
541 
542 	opri = splbio();
543 
544 	/*
545 	 * Place it in the queue of disk activities for this disk
546 	 */
547 	disksort(&cd->buf_queue, bp);
548 
549 	/*
550 	 * Tell the device to get going on the transfer if it's
551 	 * not doing anything, otherwise just wait for completion
552 	 */
553 	cdstart(cd);
554 
555 	splx(opri);
556 	return;
557 
558 bad:
559 	bp->b_flags |= B_ERROR;
560 done:
561 	/*
562 	 * Correctly set the buf to indicate a completed xfer
563 	 */
564 	bp->b_resid = bp->b_bcount;
565 	biodone(bp);
566 }
567 
568 /*
569  * cdstart looks to see if there is a buf waiting for the device
570  * and that the device is not already busy. If both are true,
571  * It deques the buf and creates a scsi command to perform the
572  * transfer in the buf. The transfer request will call scsipi_done
573  * on completion, which will in turn call this routine again
574  * so that the next queued transfer is performed.
575  * The bufs are queued by the strategy routine (cdstrategy)
576  *
577  * This routine is also called after other non-queued requests
578  * have been made of the scsi driver, to ensure that the queue
579  * continues to be drained.
580  *
581  * must be called at the correct (highish) spl level
582  * cdstart() is called at splbio from cdstrategy and scsipi_done
583  */
584 void
585 cdstart(v)
586 	register void *v;
587 {
588 	register struct cd_softc *cd = v;
589 	register struct scsipi_link *sc_link = cd->sc_link;
590 	struct disklabel *lp = cd->sc_dk.dk_label;
591 	struct buf *bp = 0;
592 	struct buf *dp;
593 	struct scsipi_rw_big cmd_big;
594 #if NCD_SCSIBUS > 0
595 	struct scsi_rw cmd_small;
596 #endif
597 	struct scsipi_generic *cmdp;
598 	int blkno, nblks, cmdlen, error;
599 	struct partition *p;
600 
601 	SC_DEBUG(sc_link, SDEV_DB2, ("cdstart "));
602 	/*
603 	 * Check if the device has room for another command
604 	 */
605 	while (sc_link->active < sc_link->openings) {
606 		/*
607 		 * there is excess capacity, but a special waits
608 		 * It'll need the adapter as soon as we clear out of the
609 		 * way and let it run (user level wait).
610 		 */
611 		if (sc_link->flags & SDEV_WAITING) {
612 			sc_link->flags &= ~SDEV_WAITING;
613 			wakeup((caddr_t)sc_link);
614 			return;
615 		}
616 
617 		/*
618 		 * See if there is a buf with work for us to do..
619 		 */
620 		dp = &cd->buf_queue;
621 		if ((bp = dp->b_actf) == NULL)	/* yes, an assign */
622 			return;
623 		dp->b_actf = bp->b_actf;
624 
625 		/*
626 		 * If the device has become invalid, abort all the
627 		 * reads and writes until all files have been closed and
628 		 * re-opened
629 		 */
630 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
631 			bp->b_error = EIO;
632 			bp->b_flags |= B_ERROR;
633 			bp->b_resid = bp->b_bcount;
634 			biodone(bp);
635 			continue;
636 		}
637 
638 		/*
639 		 * We have a buf, now we should make a command
640 		 *
641 		 * First, translate the block to absolute and put it in terms
642 		 * of the logical blocksize of the device.
643 		 */
644 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
645 		if (CDPART(bp->b_dev) != RAW_PART) {
646 			p = &lp->d_partitions[CDPART(bp->b_dev)];
647 			blkno += p->p_offset;
648 		}
649 		nblks = howmany(bp->b_bcount, lp->d_secsize);
650 
651 #if NCD_SCSIBUS > 0
652 		/*
653 		 *  Fill out the scsi command.  If the transfer will
654 		 *  fit in a "small" cdb, use it.
655 		 */
656 		if (((blkno & 0x1fffff) == blkno) &&
657 		    ((nblks & 0xff) == nblks) && sc_link->type == BUS_SCSI) {
658 			/*
659 			 * We can fit in a small cdb.
660 			 */
661 			bzero(&cmd_small, sizeof(cmd_small));
662 			cmd_small.opcode = (bp->b_flags & B_READ) ?
663 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
664 			_lto3b(blkno, cmd_small.addr);
665 			cmd_small.length = nblks & 0xff;
666 			cmdlen = sizeof(cmd_small);
667 			cmdp = (struct scsipi_generic *)&cmd_small;
668 		} else
669 #endif
670 		{
671 			/*
672 			 * Need a large cdb.
673 			 */
674 			bzero(&cmd_big, sizeof(cmd_big));
675 			cmd_big.opcode = (bp->b_flags & B_READ) ?
676 			    READ_BIG : WRITE_BIG;
677 			_lto4b(blkno, cmd_big.addr);
678 			_lto2b(nblks, cmd_big.length);
679 			cmdlen = sizeof(cmd_big);
680 			cmdp = (struct scsipi_generic *)&cmd_big;
681 		}
682 
683 		/* Instrumentation. */
684 		disk_busy(&cd->sc_dk);
685 
686 		/*
687 		 * Call the routine that chats with the adapter.
688 		 * Note: we cannot sleep as we may be an interrupt
689 		 * XXX NOSLEEP really needed?
690 		 */
691 		error = scsipi_command(sc_link, cmdp, cmdlen,
692 		    (u_char *)bp->b_data, bp->b_bcount,
693 		    CDRETRIES, 30000, bp, XS_CTL_NOSLEEP | XS_CTL_ASYNC |
694 		    ((bp->b_flags & B_READ) ?
695 		     XS_CTL_DATA_IN : XS_CTL_DATA_OUT));
696 		if (error) {
697 			disk_unbusy(&cd->sc_dk, 0);
698 			printf("%s: not queued, error %d\n",
699 			    cd->sc_dev.dv_xname, error);
700 		}
701 	}
702 }
703 
704 void
705 cddone(xs)
706 	struct scsipi_xfer *xs;
707 {
708 	struct cd_softc *cd = xs->sc_link->device_softc;
709 
710 	if (xs->bp != NULL) {
711 		disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
712 #if NRND > 0
713 		rnd_add_uint32(&cd->rnd_source, xs->bp->b_blkno);
714 #endif
715 	}
716 }
717 
718 void
719 cdminphys(bp)
720 	struct buf *bp;
721 {
722 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
723 	long max;
724 
725 	/*
726 	 * If the device is ancient, we want to make sure that
727 	 * the transfer fits into a 6-byte cdb.
728 	 *
729 	 * XXX Note that the SCSI-I spec says that 256-block transfers
730 	 * are allowed in a 6-byte read/write, and are specified
731 	 * by settng the "length" to 0.  However, we're conservative
732 	 * here, allowing only 255-block transfers in case an
733 	 * ancient device gets confused by length == 0.  A length of 0
734 	 * in a 10-byte read/write actually means 0 blocks.
735 	 */
736 	if (cd->flags & CDF_ANCIENT) {
737 		max = cd->sc_dk.dk_label->d_secsize * 0xff;
738 
739 		if (bp->b_bcount > max)
740 			bp->b_bcount = max;
741 	}
742 
743 	(*cd->sc_link->adapter->scsipi_minphys)(bp);
744 }
745 
746 int
747 cdread(dev, uio, ioflag)
748 	dev_t dev;
749 	struct uio *uio;
750 	int ioflag;
751 {
752 
753 	return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio));
754 }
755 
756 int
757 cdwrite(dev, uio, ioflag)
758 	dev_t dev;
759 	struct uio *uio;
760 	int ioflag;
761 {
762 
763 	return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio));
764 }
765 
766 /*
767  * conversion between minute-seconde-frame and logical block adress
768  * adresses format
769  */
770 void
771 lba2msf (lba, m, s, f)
772 	u_long lba;
773 	u_char *m, *s, *f;
774 {
775 	u_long tmp;
776 
777 	tmp = lba + CD_BLOCK_OFFSET;	/* offset of first logical frame */
778 	tmp &= 0xffffff;		/* negative lbas use only 24 bits */
779 	*m = tmp / (CD_SECS * CD_FRAMES);
780 	tmp %= (CD_SECS * CD_FRAMES);
781 	*s = tmp / CD_FRAMES;
782 	*f = tmp % CD_FRAMES;
783 }
784 
785 u_long
786 msf2lba (m, s, f)
787 	u_char m, s, f;
788 {
789 
790 	return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET);
791 }
792 
793 
794 /*
795  * Perform special action on behalf of the user.
796  * Knows about the internals of this device
797  */
798 int
799 cdioctl(dev, cmd, addr, flag, p)
800 	dev_t dev;
801 	u_long cmd;
802 	caddr_t addr;
803 	int flag;
804 	struct proc *p;
805 {
806 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
807 	int part = CDPART(dev);
808 	int error;
809 
810 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdioctl 0x%lx ", cmd));
811 
812 	/*
813 	 * If the device is not valid, some IOCTLs can still be
814 	 * handled on the raw partition. Check this here.
815 	 */
816 	if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
817 		switch (cmd) {
818 		case DIOCWLABEL:
819 		case DIOCLOCK:
820 		case ODIOCEJECT:
821 		case DIOCEJECT:
822 		case SCIOCIDENTIFY:
823 		case OSCIOCIDENTIFY:
824 		case SCIOCCOMMAND:
825 		case SCIOCDEBUG:
826 		case CDIOCGETVOL:
827 		case CDIOCSETVOL:
828 		case CDIOCSETMONO:
829 		case CDIOCSETSTEREO:
830 		case CDIOCSETMUTE:
831 		case CDIOCSETLEFT:
832 		case CDIOCSETRIGHT:
833 		case CDIOCCLOSE:
834 		case CDIOCEJECT:
835 		case CDIOCALLOW:
836 		case CDIOCPREVENT:
837 		case CDIOCSETDEBUG:
838 		case CDIOCCLRDEBUG:
839 		case CDIOCRESET:
840 		case SCIOCRESET:
841 		case CDIOCLOADUNLOAD:
842 		case DVD_AUTH:
843 		case DVD_READ_STRUCT:
844 			if (part == RAW_PART)
845 				break;
846 		/* FALLTHROUGH */
847 		default:
848 			if ((cd->sc_link->flags & SDEV_OPEN) == 0)
849 				return (ENODEV);
850 			else
851 				return (EIO);
852 		}
853 	}
854 
855 	switch (cmd) {
856 	case DIOCGDINFO:
857 		*(struct disklabel *)addr = *(cd->sc_dk.dk_label);
858 		return (0);
859 
860 	case DIOCGPART:
861 		((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label;
862 		((struct partinfo *)addr)->part =
863 		    &cd->sc_dk.dk_label->d_partitions[part];
864 		return (0);
865 
866 	case DIOCWDINFO:
867 	case DIOCSDINFO:
868 		if ((flag & FWRITE) == 0)
869 			return (EBADF);
870 
871 		if ((error = cdlock(cd)) != 0)
872 			return (error);
873 		cd->flags |= CDF_LABELLING;
874 
875 		error = setdisklabel(cd->sc_dk.dk_label,
876 		    (struct disklabel *)addr, /*cd->sc_dk.dk_openmask : */0,
877 		    cd->sc_dk.dk_cpulabel);
878 		if (error == 0) {
879 			/* XXX ? */
880 		}
881 
882 		cd->flags &= ~CDF_LABELLING;
883 		cdunlock(cd);
884 		return (error);
885 
886 	case DIOCWLABEL:
887 		return (EBADF);
888 
889 	case DIOCGDEFLABEL:
890 		cdgetdefaultlabel(cd, (struct disklabel *)addr);
891 		return (0);
892 
893 	case CDIOCPLAYTRACKS: {
894 		struct ioc_play_track *args = (struct ioc_play_track *)addr;
895 
896 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
897 			return (error);
898 		return (cd_play_tracks(cd, args->start_track,
899 		    args->start_index, args->end_track, args->end_index));
900 	}
901 	case CDIOCPLAYMSF: {
902 		struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
903 
904 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
905 			return (error);
906 		return (cd_play_msf(cd, args->start_m, args->start_s,
907 		    args->start_f, args->end_m, args->end_s, args->end_f));
908 	}
909 	case CDIOCPLAYBLOCKS: {
910 		struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
911 
912 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
913 			return (error);
914 		return (cd_play(cd, args->blk, args->len));
915 	}
916 	case CDIOCREADSUBCHANNEL: {
917 		struct ioc_read_subchannel *args =
918 		    (struct ioc_read_subchannel *)addr;
919 		struct cd_sub_channel_info data;
920 		int len = args->data_len;
921 
922 		if (len > sizeof(data) ||
923 		    len < sizeof(struct cd_sub_channel_header))
924 			return (EINVAL);
925 		error = cd_read_subchannel(cd, args->address_format,
926 		    args->data_format, args->track, &data, len);
927 		if (error)
928 			return (error);
929 		len = min(len, _2btol(data.header.data_len) +
930 		    sizeof(struct cd_sub_channel_header));
931 		return (copyout(&data, args->data, len));
932 	}
933 	case CDIOREADTOCHEADER: {
934 		struct ioc_toc_header th;
935 
936 		if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 0)) != 0)
937 			return (error);
938 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
939 #if BYTE_ORDER == BIG_ENDIAN
940 			bswap((u_int8_t *)&th.len, sizeof(th.len));
941 #endif
942 		} else
943 			th.len = ntohs(th.len);
944 		bcopy(&th, addr, sizeof(th));
945 		return (0);
946 	}
947 	case CDIOREADTOCENTRYS: {
948 		struct cd_toc toc;
949 		struct ioc_read_toc_entry *te =
950 		    (struct ioc_read_toc_entry *)addr;
951 		struct ioc_toc_header *th;
952 		struct cd_toc_entry *cte;
953 		int len = te->data_len;
954 		int ntracks;
955 
956 		th = &toc.header;
957 
958 		if (len > sizeof(toc.entries) ||
959 		    len < sizeof(struct cd_toc_entry))
960 			return (EINVAL);
961 		error = cd_read_toc(cd, te->address_format, te->starting_track,
962 		    &toc, len + sizeof(struct ioc_toc_header), 0);
963 		if (error)
964 			return (error);
965 		if (te->address_format == CD_LBA_FORMAT)
966 			for (ntracks =
967 			    th->ending_track - th->starting_track + 1;
968 			    ntracks >= 0; ntracks--) {
969 				cte = &toc.entries[ntracks];
970 				cte->addr_type = CD_LBA_FORMAT;
971 				if (cd->sc_link->quirks & ADEV_LITTLETOC) {
972 #if BYTE_ORDER == BIG_ENDIAN
973 					bswap((u_int8_t*)&cte->addr,
974 					    sizeof(cte->addr));
975 #endif
976 				} else
977 					cte->addr.lba = ntohl(cte->addr.lba);
978 			}
979 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
980 #if BYTE_ORDER == BIG_ENDIAN
981 			bswap((u_int8_t*)&th->len, sizeof(th->len));
982 #endif
983 		} else
984 			th->len = ntohs(th->len);
985 		len = min(len, th->len - (sizeof(th->starting_track) +
986 		    sizeof(th->ending_track)));
987 		return (copyout(toc.entries, te->data, len));
988 	}
989 	case CDIOREADMSADDR: {
990 		struct cd_toc toc;
991 		int sessno = *(int*)addr;
992 		struct cd_toc_entry *cte;
993 
994 		if (sessno != 0)
995 			return (EINVAL);
996 
997 		error = cd_read_toc(cd, 0, 0, &toc,
998 		  sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
999 		  0x40 /* control word for "get MS info" */);
1000 
1001 		if (error)
1002 			return (error);
1003 
1004 		cte = &toc.entries[0];
1005 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
1006 #if BYTE_ORDER == BIG_ENDIAN
1007 			bswap((u_int8_t*)&cte->addr, sizeof(cte->addr));
1008 #endif
1009 		} else
1010 			cte->addr.lba = ntohl(cte->addr.lba);
1011 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
1012 #if BYTE_ORDER == BIG_ENDIAN
1013 			bswap((u_int8_t*)&toc.header.len, sizeof(toc.header.len));
1014 #endif
1015 		} else
1016 			toc.header.len = ntohs(toc.header.len);
1017 
1018 		*(int*)addr = (toc.header.len >= 10 && cte->track > 1) ?
1019 			cte->addr.lba : 0;
1020 		return 0;
1021 	}
1022 	case CDIOCSETPATCH: {
1023 		struct ioc_patch *arg = (struct ioc_patch *)addr;
1024 
1025 		return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0],
1026 		    arg->patch[1], arg->patch[2], arg->patch[3], 0));
1027 	}
1028 	case CDIOCGETVOL: {
1029 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1030 
1031 		return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0));
1032 	}
1033 	case CDIOCSETVOL: {
1034 		struct ioc_vol *arg = (struct ioc_vol *)addr;
1035 
1036 		return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0));
1037 	}
1038 
1039 	case CDIOCSETMONO:
1040 		return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL,
1041 		    BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1042 
1043 	case CDIOCSETSTEREO:
1044 		return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL,
1045 		    RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1046 
1047 	case CDIOCSETMUTE:
1048 		return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL,
1049 		    MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1050 
1051 	case CDIOCSETLEFT:
1052 		return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL,
1053 		    LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1054 
1055 	case CDIOCSETRIGHT:
1056 		return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL,
1057 		    RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
1058 
1059 	case CDIOCRESUME:
1060 		return (cd_pause(cd, PA_RESUME));
1061 	case CDIOCPAUSE:
1062 		return (cd_pause(cd, PA_PAUSE));
1063 	case CDIOCSTART:
1064 		return (scsipi_start(cd->sc_link, SSS_START, 0));
1065 	case CDIOCSTOP:
1066 		return (scsipi_start(cd->sc_link, SSS_STOP, 0));
1067 	case CDIOCCLOSE:
1068 		return (scsipi_start(cd->sc_link, SSS_START|SSS_LOEJ,
1069 		    XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE));
1070 	case DIOCEJECT:
1071 		if (*(int *)addr == 0) {
1072 			/*
1073 			 * Don't force eject: check that we are the only
1074 			 * partition open. If so, unlock it.
1075 			 */
1076 			if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1077 			    cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask ==
1078 			    cd->sc_dk.dk_openmask) {
1079 				error =  scsipi_prevent(cd->sc_link, PR_ALLOW,
1080 				    XS_CTL_IGNORE_NOT_READY);
1081 				if (error)
1082 					return (error);
1083 			} else {
1084 				return (EBUSY);
1085 			}
1086 		}
1087 		/* FALLTHROUGH */
1088 	case CDIOCEJECT: /* FALLTHROUGH */
1089 	case ODIOCEJECT:
1090 		return (scsipi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0));
1091 	case CDIOCALLOW:
1092 		return (scsipi_prevent(cd->sc_link, PR_ALLOW, 0));
1093 	case CDIOCPREVENT:
1094 		return (scsipi_prevent(cd->sc_link, PR_PREVENT, 0));
1095 	case DIOCLOCK:
1096 		return (scsipi_prevent(cd->sc_link,
1097 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1098 	case CDIOCSETDEBUG:
1099 		cd->sc_link->flags |= (SDEV_DB1 | SDEV_DB2);
1100 		return (0);
1101 	case CDIOCCLRDEBUG:
1102 		cd->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2);
1103 		return (0);
1104 	case CDIOCRESET:
1105 	case SCIOCRESET:
1106 		return (cd_reset(cd));
1107 	case CDIOCLOADUNLOAD: {
1108 		struct ioc_load_unload *args = (struct ioc_load_unload *)addr;
1109 
1110 		return ((*cd->sc_ops->cdo_load_unload)(cd, args->options,
1111 			args->slot));
1112 	case DVD_AUTH:
1113 		return (dvd_auth(cd, (dvd_authinfo *)addr));
1114 	case DVD_READ_STRUCT:
1115 		return (dvd_read_struct(cd, (dvd_struct *)addr));
1116 	}
1117 
1118 	default:
1119 		if (part != RAW_PART)
1120 			return (ENOTTY);
1121 		return (scsipi_do_ioctl(cd->sc_link, dev, cmd, addr, flag, p));
1122 	}
1123 
1124 #ifdef DIAGNOSTIC
1125 	panic("cdioctl: impossible");
1126 #endif
1127 }
1128 
1129 void
1130 cdgetdefaultlabel(cd, lp)
1131 	struct cd_softc *cd;
1132 	struct disklabel *lp;
1133 {
1134 
1135 	bzero(lp, sizeof(struct disklabel));
1136 
1137 	lp->d_secsize = cd->params.blksize;
1138 	lp->d_ntracks = 1;
1139 	lp->d_nsectors = 100;
1140 	lp->d_ncylinders = (cd->params.disksize / 100) + 1;
1141 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1142 
1143 	switch (cd->sc_link->type) {
1144 #if NCD_SCSIBUS > 0
1145 	    case BUS_SCSI:
1146 		lp->d_type = DTYPE_SCSI;
1147 		break;
1148 #endif
1149 #if NCD_ATAPIBUS > 0
1150 	    case BUS_ATAPI:
1151 		lp->d_type = DTYPE_ATAPI;
1152 		break;
1153 #endif
1154 	}
1155 	strncpy(lp->d_typename, cd->name, 16);
1156 	strncpy(lp->d_packname, "fictitious", 16);
1157 	lp->d_secperunit = cd->params.disksize;
1158 	lp->d_rpm = 300;
1159 	lp->d_interleave = 1;
1160 	lp->d_flags = D_REMOVABLE;
1161 
1162 	lp->d_partitions[0].p_offset = 0;
1163 	lp->d_partitions[0].p_size =
1164 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1165 	lp->d_partitions[0].p_fstype = FS_ISO9660;
1166 	lp->d_partitions[RAW_PART].p_offset = 0;
1167 	lp->d_partitions[RAW_PART].p_size =
1168 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1169 	lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
1170 	lp->d_npartitions = RAW_PART + 1;
1171 
1172 	lp->d_magic = DISKMAGIC;
1173 	lp->d_magic2 = DISKMAGIC;
1174 	lp->d_checksum = dkcksum(lp);
1175 }
1176 
1177 /*
1178  * Load the label information on the named device
1179  * Actually fabricate a disklabel
1180  *
1181  * EVENTUALLY take information about different
1182  * data tracks from the TOC and put it in the disklabel
1183  */
1184 void
1185 cdgetdisklabel(cd)
1186 	struct cd_softc *cd;
1187 {
1188 	struct disklabel *lp = cd->sc_dk.dk_label;
1189 
1190 	bzero(cd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
1191 
1192 	cdgetdefaultlabel(cd, lp);
1193 }
1194 
1195 /*
1196  * Find out from the device what it's capacity is
1197  */
1198 u_long
1199 cd_size(cd, flags)
1200 	struct cd_softc *cd;
1201 	int flags;
1202 {
1203 	struct scsipi_read_cd_cap_data rdcap;
1204 	struct scsipi_read_cd_capacity scsipi_cmd;
1205 	int blksize;
1206 	u_long size;
1207 
1208 	if (cd->sc_link->quirks & ADEV_NOCAPACITY) {
1209 		/*
1210 		 * the drive doesn't support the READ_CD_CAPACITY command
1211 		 * use a fake size
1212 		 */
1213 		cd->params.blksize = 2048;
1214 		cd->params.disksize = 400000;
1215 		return (400000);
1216 	}
1217 
1218 	/*
1219 	 * make up a scsi command and ask the scsi driver to do
1220 	 * it for you.
1221 	 */
1222 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1223 	scsipi_cmd.opcode = READ_CD_CAPACITY;
1224 
1225 	/*
1226 	 * If the command works, interpret the result as a 4 byte
1227 	 * number of blocks and a blocksize
1228 	 */
1229 	if (scsipi_command(cd->sc_link,
1230 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1231 	    (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL,
1232 	    flags | XS_CTL_DATA_IN) != 0)
1233 		return (0);
1234 
1235 	blksize = _4btol(rdcap.length);
1236 	if ((blksize < 512) || ((blksize & 511) != 0))
1237 		blksize = 2048;	/* some drives lie ! */
1238 	cd->params.blksize = blksize;
1239 
1240 	size = _4btol(rdcap.addr) + 1;
1241 	if (size < 100)
1242 		size = 400000;	/* ditto */
1243 	cd->params.disksize = size;
1244 
1245 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cd_size: %d %ld\n", blksize, size));
1246 	return (size);
1247 }
1248 
1249 /*
1250  * Get scsi driver to send a "start playing" command
1251  */
1252 int
1253 cd_play(cd, blkno, nblks)
1254 	struct cd_softc *cd;
1255 	int blkno, nblks;
1256 {
1257 	struct scsipi_play scsipi_cmd;
1258 
1259 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1260 	scsipi_cmd.opcode = PLAY;
1261 	_lto4b(blkno, scsipi_cmd.blk_addr);
1262 	_lto2b(nblks, scsipi_cmd.xfer_len);
1263 	return (scsipi_command(cd->sc_link,
1264 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1265 	    0, 0, CDRETRIES, 30000, NULL, 0));
1266 }
1267 
1268 /*
1269  * Get scsi driver to send a "start playing" command
1270  */
1271 int
1272 cd_play_tracks(cd, strack, sindex, etrack, eindex)
1273 	struct cd_softc *cd;
1274 	int strack, sindex, etrack, eindex;
1275 {
1276 	struct cd_toc toc;
1277 	int error;
1278 
1279 	if (!etrack)
1280 		return (EIO);
1281 	if (strack > etrack)
1282 		return (EINVAL);
1283 
1284 	if ((error = cd_load_toc(cd, &toc)) != 0)
1285 		return (error);
1286 
1287 	if (++etrack > (toc.header.ending_track+1))
1288 		etrack = toc.header.ending_track+1;
1289 
1290 	strack -= toc.header.starting_track;
1291 	etrack -= toc.header.starting_track;
1292 	if (strack < 0)
1293 		return (EINVAL);
1294 
1295 	return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute,
1296 	    toc.entries[strack].addr.msf.second,
1297 	    toc.entries[strack].addr.msf.frame,
1298 	    toc.entries[etrack].addr.msf.minute,
1299 	    toc.entries[etrack].addr.msf.second,
1300 	    toc.entries[etrack].addr.msf.frame));
1301 }
1302 
1303 /*
1304  * Get scsi driver to send a "play msf" command
1305  */
1306 int
1307 cd_play_msf(cd, startm, starts, startf, endm, ends, endf)
1308 	struct cd_softc *cd;
1309 	int startm, starts, startf, endm, ends, endf;
1310 {
1311 	struct scsipi_play_msf scsipi_cmd;
1312 
1313 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1314 	scsipi_cmd.opcode = PLAY_MSF;
1315 	scsipi_cmd.start_m = startm;
1316 	scsipi_cmd.start_s = starts;
1317 	scsipi_cmd.start_f = startf;
1318 	scsipi_cmd.end_m = endm;
1319 	scsipi_cmd.end_s = ends;
1320 	scsipi_cmd.end_f = endf;
1321 	return (scsipi_command(cd->sc_link,
1322 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1323 	    0, 0, CDRETRIES, 30000, NULL, 0));
1324 }
1325 
1326 /*
1327  * Get scsi driver to send a "start up" command
1328  */
1329 int
1330 cd_pause(cd, go)
1331 	struct cd_softc *cd;
1332 	int go;
1333 {
1334 	struct scsipi_pause scsipi_cmd;
1335 
1336 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1337 	scsipi_cmd.opcode = PAUSE;
1338 	scsipi_cmd.resume = go & 0xff;
1339 	return (scsipi_command(cd->sc_link,
1340 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1341 	    0, 0, CDRETRIES, 30000, NULL, 0));
1342 }
1343 
1344 /*
1345  * Get scsi driver to send a "RESET" command
1346  */
1347 int
1348 cd_reset(cd)
1349 	struct cd_softc *cd;
1350 {
1351 
1352 	return (scsipi_command(cd->sc_link, 0, 0, 0, 0,
1353 	    CDRETRIES, 30000, NULL, XS_CTL_RESET));
1354 }
1355 
1356 /*
1357  * Read subchannel
1358  */
1359 int
1360 cd_read_subchannel(cd, mode, format, track, data, len)
1361 	struct cd_softc *cd;
1362 	int mode, format, track, len;
1363 	struct cd_sub_channel_info *data;
1364 {
1365 	struct scsipi_read_subchannel scsipi_cmd;
1366 
1367 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1368 	scsipi_cmd.opcode = READ_SUBCHANNEL;
1369 	if (mode == CD_MSF_FORMAT)
1370 		scsipi_cmd.byte2 |= CD_MSF;
1371 	scsipi_cmd.byte3 = SRS_SUBQ;
1372 	scsipi_cmd.subchan_format = format;
1373 	scsipi_cmd.track = track;
1374 	_lto2b(len, scsipi_cmd.data_len);
1375 	return (scsipi_command(cd->sc_link,
1376 	    (struct scsipi_generic *)&scsipi_cmd,
1377 	    sizeof(struct scsipi_read_subchannel), (u_char *)data, len,
1378 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_SILENT));
1379 }
1380 
1381 /*
1382  * Read table of contents
1383  */
1384 int
1385 cd_read_toc(cd, mode, start, data, len, control)
1386 	struct cd_softc *cd;
1387 	int mode, start, len, control;
1388 	void *data;
1389 {
1390 	struct scsipi_read_toc scsipi_cmd;
1391 	int ntoc;
1392 
1393 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1394 #if 0
1395 	if (len != sizeof(struct ioc_toc_header))
1396 		ntoc = ((len) - sizeof(struct ioc_toc_header)) /
1397 		    sizeof(struct cd_toc_entry);
1398 	else
1399 #endif
1400 	ntoc = len;
1401 	scsipi_cmd.opcode = READ_TOC;
1402 	if (mode == CD_MSF_FORMAT)
1403 		scsipi_cmd.byte2 |= CD_MSF;
1404 	scsipi_cmd.from_track = start;
1405 	_lto2b(ntoc, scsipi_cmd.data_len);
1406 	scsipi_cmd.control = control;
1407 	return (scsipi_command(cd->sc_link,
1408 	    (struct scsipi_generic *)&scsipi_cmd,
1409 	    sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES,
1410 	    30000, NULL, XS_CTL_DATA_IN));
1411 }
1412 
1413 int
1414 cd_load_toc(cd, toc)
1415 	struct cd_softc *cd;
1416 	struct cd_toc *toc;
1417 {
1418 	int ntracks, len, error;
1419 
1420 	if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 0)) != 0)
1421 		return (error);
1422 
1423 	ntracks = toc->header.ending_track - toc->header.starting_track + 1;
1424 	len = (ntracks + 1) * sizeof(struct cd_toc_entry) +
1425 	    sizeof(toc->header);
1426 	if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 0)) != 0)
1427 		return (error);
1428 	return (0);
1429 }
1430 
1431 /*
1432  * Get the scsi driver to send a full inquiry to the device and use the
1433  * results to fill out the disk parameter structure.
1434  */
1435 int
1436 cd_get_parms(cd, flags)
1437 	struct cd_softc *cd;
1438 	int flags;
1439 {
1440 
1441 	/*
1442 	 * give a number of sectors so that sec * trks * cyls
1443 	 * is <= disk_size
1444 	 */
1445 	if (cd_size(cd, flags) == 0)
1446 		return (ENXIO);
1447 	return (0);
1448 }
1449 
1450 int
1451 cdsize(dev)
1452 	dev_t dev;
1453 {
1454 
1455 	/* CD-ROMs are read-only. */
1456 	return (-1);
1457 }
1458 
1459 int
1460 cddump(dev, blkno, va, size)
1461 	dev_t dev;
1462 	daddr_t blkno;
1463 	caddr_t va;
1464 	size_t size;
1465 {
1466 
1467 	/* Not implemented. */
1468 	return (ENXIO);
1469 }
1470 
1471 #define	dvd_copy_key(dst, src)		memcpy((dst), (src), sizeof(dvd_key))
1472 #define	dvd_copy_challenge(dst, src)	memcpy((dst), (src), sizeof(dvd_challenge))
1473 
1474 int
1475 dvd_auth(cd, a)
1476 	struct cd_softc *cd;
1477 	dvd_authinfo *a;
1478 {
1479 	struct scsipi_generic cmd;
1480 	u_int8_t buf[20];
1481 	int error;
1482 
1483 	memset(cmd.bytes, 0, 15);
1484 	memset(buf, 0, sizeof(buf));
1485 
1486 	switch (a->type) {
1487 	case DVD_LU_SEND_AGID:
1488 		cmd.opcode = GPCMD_REPORT_KEY;
1489 		cmd.bytes[8] = 8;
1490 		cmd.bytes[9] = 0 | (0 << 6);
1491 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 8,
1492 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1493 		if (error)
1494 			return (error);
1495 		a->lsa.agid = buf[7] >> 6;
1496 		return (0);
1497 
1498 	case DVD_LU_SEND_CHALLENGE:
1499 		cmd.opcode = GPCMD_REPORT_KEY;
1500 		cmd.bytes[8] = 16;
1501 		cmd.bytes[9] = 1 | (a->lsc.agid << 6);
1502 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16,
1503 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1504 		if (error)
1505 			return (error);
1506 		dvd_copy_challenge(a->lsc.chal, &buf[4]);
1507 		return (0);
1508 
1509 	case DVD_LU_SEND_KEY1:
1510 		cmd.opcode = GPCMD_REPORT_KEY;
1511 		cmd.bytes[8] = 12;
1512 		cmd.bytes[9] = 2 | (a->lsk.agid << 6);
1513 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12,
1514 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1515 		if (error)
1516 			return (error);
1517 		dvd_copy_key(a->lsk.key, &buf[4]);
1518 		return (0);
1519 
1520 	case DVD_LU_SEND_TITLE_KEY:
1521 		cmd.opcode = GPCMD_REPORT_KEY;
1522 		_lto4b(a->lstk.lba, &cmd.bytes[1]);
1523 		cmd.bytes[8] = 12;
1524 		cmd.bytes[9] = 4 | (a->lstk.agid << 6);
1525 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12,
1526 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1527 		if (error)
1528 			return (error);
1529 		a->lstk.cpm = (buf[4] >> 7) & 1;
1530 		a->lstk.cp_sec = (buf[4] >> 6) & 1;
1531 		a->lstk.cgms = (buf[4] >> 4) & 3;
1532 		dvd_copy_key(a->lstk.title_key, &buf[5]);
1533 		return (0);
1534 
1535 	case DVD_LU_SEND_ASF:
1536 		cmd.opcode = GPCMD_REPORT_KEY;
1537 		cmd.bytes[8] = 8;
1538 		cmd.bytes[9] = 5 | (a->lsasf.agid << 6);
1539 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 8,
1540 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1541 		if (error)
1542 			return (error);
1543 		a->lsasf.asf = buf[7] & 1;
1544 		return (0);
1545 
1546 	case DVD_HOST_SEND_CHALLENGE:
1547 		cmd.opcode = GPCMD_SEND_KEY;
1548 		cmd.bytes[8] = 16;
1549 		cmd.bytes[9] = 1 | (a->hsc.agid << 6);
1550 		buf[1] = 14;
1551 		dvd_copy_challenge(&buf[4], a->hsc.chal);
1552 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16,
1553 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT|XS_CTL_DATA_IN);
1554 		if (error)
1555 			return (error);
1556 		a->type = DVD_LU_SEND_KEY1;
1557 		return (0);
1558 
1559 	case DVD_HOST_SEND_KEY2:
1560 		cmd.opcode = GPCMD_SEND_KEY;
1561 		cmd.bytes[8] = 12;
1562 		cmd.bytes[9] = 3 | (a->hsk.agid << 6);
1563 		buf[1] = 10;
1564 		dvd_copy_key(&buf[4], a->hsk.key);
1565 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12,
1566 		    CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT|XS_CTL_DATA_IN);
1567 		if (error) {
1568 			a->type = DVD_AUTH_FAILURE;
1569 			return (error);
1570 		}
1571 		a->type = DVD_AUTH_ESTABLISHED;
1572 		return (0);
1573 
1574 	case DVD_INVALIDATE_AGID:
1575 		cmd.opcode = GPCMD_REPORT_KEY;
1576 		cmd.bytes[9] = 0x3f | (a->lsa.agid << 6);
1577 		error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16,
1578 		    CDRETRIES, 30000, NULL, 0);
1579 		if (error)
1580 			return (error);
1581 		return (0);
1582 
1583 	default:
1584 		return (ENOTTY);
1585 	}
1586 }
1587 
1588 int
1589 dvd_read_physical(cd, s)
1590 	struct cd_softc *cd;
1591 	dvd_struct *s;
1592 {
1593 	struct scsipi_generic cmd;
1594 	u_int8_t buf[4 + 4 * 20], *bufp;
1595 	int error;
1596 	struct dvd_layer *layer;
1597 	int i;
1598 
1599 	memset(cmd.bytes, 0, 15);
1600 	memset(buf, 0, sizeof(buf));
1601 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1602 	cmd.bytes[6] = s->type;
1603 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1604 
1605 	cmd.bytes[5] = s->physical.layer_num;
1606 	error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf),
1607 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1608 	if (error)
1609 		return (error);
1610 	for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4;
1611 	     i++, bufp += 20, layer++) {
1612 		memset(layer, 0, sizeof(*layer));
1613                 layer->book_version = bufp[0] & 0xf;
1614                 layer->book_type = bufp[0] >> 4;
1615                 layer->min_rate = bufp[1] & 0xf;
1616                 layer->disc_size = bufp[1] >> 4;
1617                 layer->layer_type = bufp[2] & 0xf;
1618                 layer->track_path = (bufp[2] >> 4) & 1;
1619                 layer->nlayers = (bufp[2] >> 5) & 3;
1620                 layer->track_density = bufp[3] & 0xf;
1621                 layer->linear_density = bufp[3] >> 4;
1622                 layer->start_sector = _4btol(&bufp[4]);
1623                 layer->end_sector = _4btol(&bufp[8]);
1624                 layer->end_sector_l0 = _4btol(&bufp[12]);
1625                 layer->bca = bufp[16] >> 7;
1626 	}
1627 	return (0);
1628 }
1629 
1630 int
1631 dvd_read_copyright(cd, s)
1632 	struct cd_softc *cd;
1633 	dvd_struct *s;
1634 {
1635 	struct scsipi_generic cmd;
1636 	u_int8_t buf[8];
1637 	int error;
1638 
1639 	memset(cmd.bytes, 0, 15);
1640 	memset(buf, 0, sizeof(buf));
1641 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1642 	cmd.bytes[6] = s->type;
1643 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1644 
1645 	cmd.bytes[5] = s->copyright.layer_num;
1646 	error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf),
1647 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1648 	if (error)
1649 		return (error);
1650 	s->copyright.cpst = buf[4];
1651 	s->copyright.rmi = buf[5];
1652 	return (0);
1653 }
1654 
1655 int
1656 dvd_read_disckey(cd, s)
1657 	struct cd_softc *cd;
1658 	dvd_struct *s;
1659 {
1660 	struct scsipi_generic cmd;
1661 	u_int8_t buf[4 + 2048];
1662 	int error;
1663 
1664 	memset(cmd.bytes, 0, 15);
1665 	memset(buf, 0, sizeof(buf));
1666 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1667 	cmd.bytes[6] = s->type;
1668 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1669 
1670 	cmd.bytes[9] = s->disckey.agid << 6;
1671 	error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf),
1672 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1673 	if (error)
1674 		return (error);
1675 	memcpy(s->disckey.value, &buf[4], 2048);
1676 	return (0);
1677 }
1678 
1679 int
1680 dvd_read_bca(cd, s)
1681 	struct cd_softc *cd;
1682 	dvd_struct *s;
1683 {
1684 	struct scsipi_generic cmd;
1685 	u_int8_t buf[4 + 188];
1686 	int error;
1687 
1688 	memset(cmd.bytes, 0, 15);
1689 	memset(buf, 0, sizeof(buf));
1690 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1691 	cmd.bytes[6] = s->type;
1692 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1693 
1694 	error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf),
1695 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1696 	if (error)
1697 		return (error);
1698 	s->bca.len = _2btol(&buf[0]);
1699 	if (s->bca.len < 12 || s->bca.len > 188)
1700 		return (EIO);
1701 	memcpy(s->bca.value, &buf[4], s->bca.len);
1702 	return (0);
1703 }
1704 
1705 int
1706 dvd_read_manufact(cd, s)
1707 	struct cd_softc *cd;
1708 	dvd_struct *s;
1709 {
1710 	struct scsipi_generic cmd;
1711 	u_int8_t buf[4 + 2048];
1712 	int error;
1713 
1714 	memset(cmd.bytes, 0, 15);
1715 	memset(buf, 0, sizeof(buf));
1716 	cmd.opcode = GPCMD_READ_DVD_STRUCTURE;
1717 	cmd.bytes[6] = s->type;
1718 	_lto2b(sizeof(buf), &cmd.bytes[7]);
1719 
1720 	error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf),
1721 	    CDRETRIES, 30000, NULL, XS_CTL_DATA_IN);
1722 	if (error)
1723 		return (error);
1724 	s->manufact.len = _2btol(&buf[0]);
1725 	if (s->manufact.len < 0 || s->manufact.len > 2048)
1726 		return (EIO);
1727 	memcpy(s->manufact.value, &buf[4], s->manufact.len);
1728 	return (0);
1729 }
1730 
1731 int
1732 dvd_read_struct(cd, s)
1733 	struct cd_softc *cd;
1734 	dvd_struct *s;
1735 {
1736 
1737 	switch (s->type) {
1738 	case DVD_STRUCT_PHYSICAL:
1739 		return (dvd_read_physical(cd, s));
1740 	case DVD_STRUCT_COPYRIGHT:
1741 		return (dvd_read_copyright(cd, s));
1742 	case DVD_STRUCT_DISCKEY:
1743 		return (dvd_read_disckey(cd, s));
1744 	case DVD_STRUCT_BCA:
1745 		return (dvd_read_bca(cd, s));
1746 	case DVD_STRUCT_MANUFACT:
1747 		return (dvd_read_manufact(cd, s));
1748 	default:
1749 		return (EINVAL);
1750 	}
1751 }
1752