xref: /netbsd-src/sys/dev/scsipi/cd.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: cd.c,v 1.124 1999/02/28 17:14:57 explorer 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/scsiio.h>
74 #include <sys/proc.h>
75 #include <sys/conf.h>
76 #if NRND > 0
77 #include <sys/rnd.h>
78 #endif
79 
80 #include <dev/scsipi/scsipi_all.h>
81 #include <dev/scsipi/scsipi_cd.h>
82 #include <dev/scsipi/scsipi_disk.h>	/* rw_big and start_stop come */
83 					/* from there */
84 #include <dev/scsipi/scsi_disk.h>	/* rw comes from there */
85 #include <dev/scsipi/scsipiconf.h>
86 #include <dev/scsipi/cdvar.h>
87 
88 #include "cd.h"		/* NCD_SCSIBUS and NCD_ATAPIBUS come from here */
89 
90 #define	CDOUTSTANDING	4
91 
92 #define	CDUNIT(z)			DISKUNIT(z)
93 #define	CDPART(z)			DISKPART(z)
94 #define	MAKECDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
95 
96 #define MAXTRACK	99
97 #define CD_BLOCK_OFFSET	150
98 #define CD_FRAMES	75
99 #define CD_SECS		60
100 
101 struct cd_toc {
102 	struct ioc_toc_header header;
103 	struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */
104 						 /* leadout */
105 };
106 
107 int	cdlock __P((struct cd_softc *));
108 void	cdunlock __P((struct cd_softc *));
109 void	cdstart __P((void *));
110 void	cdminphys __P((struct buf *));
111 void	cdgetdefaultlabel __P((struct cd_softc *, struct disklabel *));
112 void	cdgetdisklabel __P((struct cd_softc *));
113 void	cddone __P((struct scsipi_xfer *));
114 u_long	cd_size __P((struct cd_softc *, int));
115 void	lba2msf __P((u_long, u_char *, u_char *, u_char *));
116 u_long	msf2lba __P((u_char, u_char, u_char));
117 int	cd_play __P((struct cd_softc *, int, int));
118 int	cd_play_tracks __P((struct cd_softc *, int, int, int, int));
119 int	cd_play_msf __P((struct cd_softc *, int, int, int, int, int, int));
120 int	cd_pause __P((struct cd_softc *, int));
121 int	cd_reset __P((struct cd_softc *));
122 int	cd_read_subchannel __P((struct cd_softc *, int, int, int,
123 	    struct cd_sub_channel_info *, int));
124 int	cd_read_toc __P((struct cd_softc *, int, int, void *, int, int));
125 int	cd_get_parms __P((struct cd_softc *, int));
126 int	cd_load_toc __P((struct cd_softc *, struct cd_toc *));
127 
128 extern struct cfdriver cd_cd;
129 
130 struct dkdriver cddkdriver = { cdstrategy };
131 
132 struct scsipi_device cd_switch = {
133 	NULL,			/* use default error handler */
134 	cdstart,		/* we have a queue, which is started by this */
135 	NULL,			/* we do not have an async handler */
136 	cddone,			/* deal with stats at interrupt time */
137 };
138 
139 /*
140  * The routine called by the low level scsi routine when it discovers
141  * A device suitable for this driver
142  */
143 void
144 cdattach(parent, cd, sc_link, ops)
145 	struct device *parent;
146 	struct cd_softc *cd;
147 	struct scsipi_link *sc_link;
148 	const struct cd_ops *ops;
149 {
150 	SC_DEBUG(sc_link, SDEV_DB2, ("cdattach: "));
151 
152 	/*
153 	 * Store information needed to contact our base driver
154 	 */
155 	cd->sc_link = sc_link;
156 	cd->sc_ops = ops;
157 	sc_link->device = &cd_switch;
158 	sc_link->device_softc = cd;
159 	if (sc_link->openings > CDOUTSTANDING)
160 		sc_link->openings = CDOUTSTANDING;
161 
162 	/*
163 	 * Initialize and attach the disk structure.
164 	 */
165   	cd->sc_dk.dk_driver = &cddkdriver;
166 	cd->sc_dk.dk_name = cd->sc_dev.dv_xname;
167 	disk_attach(&cd->sc_dk);
168 
169 #if !defined(i386)
170 	dk_establish(&cd->sc_dk, &cd->sc_dev);		/* XXX */
171 #endif
172 
173 	printf("\n");
174 
175 #if NRND > 0
176 	rnd_attach_source(&cd->rnd_source, cd->sc_dev.dv_xname,
177 			  RND_TYPE_DISK, 0);
178 #endif
179 }
180 
181 /*
182  * Wait interruptibly for an exclusive lock.
183  *
184  * XXX
185  * Several drivers do this; it should be abstracted and made MP-safe.
186  */
187 int
188 cdlock(cd)
189 	struct cd_softc *cd;
190 {
191 	int error;
192 
193 	while ((cd->flags & CDF_LOCKED) != 0) {
194 		cd->flags |= CDF_WANTED;
195 		if ((error = tsleep(cd, PRIBIO | PCATCH, "cdlck", 0)) != 0)
196 			return (error);
197 	}
198 	cd->flags |= CDF_LOCKED;
199 	return (0);
200 }
201 
202 /*
203  * Unlock and wake up any waiters.
204  */
205 void
206 cdunlock(cd)
207 	struct cd_softc *cd;
208 {
209 
210 	cd->flags &= ~CDF_LOCKED;
211 	if ((cd->flags & CDF_WANTED) != 0) {
212 		cd->flags &= ~CDF_WANTED;
213 		wakeup(cd);
214 	}
215 }
216 
217 /*
218  * open the device. Make sure the partition info is a up-to-date as can be.
219  */
220 int
221 cdopen(dev, flag, fmt, p)
222 	dev_t dev;
223 	int flag, fmt;
224 	struct proc *p;
225 {
226 	struct cd_softc *cd;
227 	struct scsipi_link *sc_link;
228 	int unit, part;
229 	int error;
230 
231 	unit = CDUNIT(dev);
232 	if (unit >= cd_cd.cd_ndevs)
233 		return (ENXIO);
234 	cd = cd_cd.cd_devs[unit];
235 	if (cd == NULL)
236 		return (ENXIO);
237 
238 	sc_link = cd->sc_link;
239 	part = CDPART(dev);
240 
241 	SC_DEBUG(sc_link, SDEV_DB1,
242 	    ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
243 	    cd_cd.cd_ndevs, CDPART(dev)));
244 
245 	/*
246 	 * If this is the first open of this device, add a reference
247 	 * to the adapter.
248 	 */
249 	if (cd->sc_dk.dk_openmask == 0 &&
250 	    (error = scsipi_adapter_addref(sc_link)) != 0)
251 		return (error);
252 
253 	if ((error = cdlock(cd)) != 0)
254 		goto bad4;
255 
256 	if ((sc_link->flags & SDEV_OPEN) != 0) {
257 		/*
258 		 * If any partition is open, but the disk has been invalidated,
259 		 * disallow further opens.
260 		 */
261 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0 &&
262 			(part != RAW_PART || fmt != S_IFCHR )) {
263 			error = EIO;
264 			goto bad3;
265 		}
266 	} else {
267 		/* Check that it is still responding and ok. */
268 		error = scsipi_test_unit_ready(sc_link,
269 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
270 		    SCSI_IGNORE_NOT_READY);
271 		SC_DEBUG(sc_link, SDEV_DB1,
272 		    ("cdopen: scsipi_test_unit_ready, error=%d\n", error));
273 		if (error)
274 			goto bad3;
275 
276 		/*
277 		 * Start the pack spinning if necessary. Always allow the
278 		 * raw parition to be opened, for raw IOCTLs. Data transfers
279 		 * will check for SDEV_MEDIA_LOADED.
280 		 */
281 		error = scsipi_start(sc_link, SSS_START,
282 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
283 		    SCSI_SILENT);
284 		SC_DEBUG(sc_link, SDEV_DB1,
285 		    ("cdopen: scsipi_start, error=%d\n", error));
286 		if (error) {
287 			if (part != RAW_PART || fmt != S_IFCHR)
288 				goto bad3;
289 			else
290 				goto out;
291 		}
292 
293 		sc_link->flags |= SDEV_OPEN;
294 
295 		/* Lock the pack in. */
296 		error = scsipi_prevent(sc_link, PR_PREVENT,
297 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
298 		SC_DEBUG(sc_link, SDEV_DB1,
299 		    ("cdopen: scsipi_prevent, error=%d\n", error));
300 		if (error)
301 			goto bad;
302 
303 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
304 			sc_link->flags |= SDEV_MEDIA_LOADED;
305 
306 			/* Load the physical device parameters. */
307 			if (cd_get_parms(cd, 0) != 0) {
308 				error = ENXIO;
309 				goto bad2;
310 			}
311 			SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
312 
313 			/* Fabricate a disk label. */
314 			cdgetdisklabel(cd);
315 			SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated "));
316 		}
317 	}
318 
319 	/* Check that the partition exists. */
320 	if (part != RAW_PART &&
321 	    (part >= cd->sc_dk.dk_label->d_npartitions ||
322 	    cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
323 		error = ENXIO;
324 		goto bad;
325 	}
326 
327 out:	/* Insure only one open at a time. */
328 	switch (fmt) {
329 	case S_IFCHR:
330 		cd->sc_dk.dk_copenmask |= (1 << part);
331 		break;
332 	case S_IFBLK:
333 		cd->sc_dk.dk_bopenmask |= (1 << part);
334 		break;
335 	}
336 	cd->sc_dk.dk_openmask =
337 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
338 
339 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
340 	cdunlock(cd);
341 	return (0);
342 
343 bad2:
344 	sc_link->flags &= ~SDEV_MEDIA_LOADED;
345 
346 bad:
347 	if (cd->sc_dk.dk_openmask == 0) {
348 		scsipi_prevent(sc_link, PR_ALLOW,
349 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
350 		sc_link->flags &= ~SDEV_OPEN;
351 	}
352 
353 bad3:
354 	cdunlock(cd);
355 bad4:
356 	if (cd->sc_dk.dk_openmask == 0)
357 		scsipi_adapter_delref(sc_link);
358 	return (error);
359 }
360 
361 /*
362  * close the device.. only called if we are the LAST
363  * occurence of an open device
364  */
365 int
366 cdclose(dev, flag, fmt, p)
367 	dev_t dev;
368 	int flag, fmt;
369 	struct proc *p;
370 {
371 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
372 	int part = CDPART(dev);
373 	int error;
374 
375 	if ((error = cdlock(cd)) != 0)
376 		return (error);
377 
378 	switch (fmt) {
379 	case S_IFCHR:
380 		cd->sc_dk.dk_copenmask &= ~(1 << part);
381 		break;
382 	case S_IFBLK:
383 		cd->sc_dk.dk_bopenmask &= ~(1 << part);
384 		break;
385 	}
386 	cd->sc_dk.dk_openmask =
387 	    cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask;
388 
389 	if (cd->sc_dk.dk_openmask == 0) {
390 		scsipi_wait_drain(cd->sc_link);
391 
392 		scsipi_prevent(cd->sc_link, PR_ALLOW,
393 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
394 		cd->sc_link->flags &= ~SDEV_OPEN;
395 
396 		scsipi_wait_drain(cd->sc_link);
397 
398 		scsipi_adapter_delref(cd->sc_link);
399 	}
400 
401 	cdunlock(cd);
402 	return (0);
403 }
404 
405 /*
406  * Actually translate the requested transfer into one the physical driver can
407  * understand.  The transfer is described by a buf and will include only one
408  * physical transfer.
409  */
410 void
411 cdstrategy(bp)
412 	struct buf *bp;
413 {
414 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
415 	int opri;
416 
417 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdstrategy "));
418 	SC_DEBUG(cd->sc_link, SDEV_DB1,
419 	    ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
420 	/*
421 	 * If the device has been made invalid, error out
422 	 * maybe the media changed
423 	 */
424 	if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
425 		if (cd->sc_link->flags & SDEV_OPEN)
426 			bp->b_error = EIO;
427 		else
428 			bp->b_error = ENODEV;
429 		goto bad;
430 	}
431 	/*
432 	 * The transfer must be a whole number of blocks, offset must not
433 	 * be negative.
434 	 */
435 	if ((bp->b_bcount % cd->sc_dk.dk_label->d_secsize) != 0 ||
436 	    bp->b_blkno < 0 ) {
437 		bp->b_error = EINVAL;
438 		goto bad;
439 	}
440 	/*
441 	 * If it's a null transfer, return immediately
442 	 */
443 	if (bp->b_bcount == 0)
444 		goto done;
445 
446 	/*
447 	 * Do bounds checking, adjust transfer. if error, process.
448 	 * If end of partition, just return.
449 	 */
450 	if (CDPART(bp->b_dev) != RAW_PART &&
451 	    bounds_check_with_label(bp, cd->sc_dk.dk_label,
452 	    (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0)
453 		goto done;
454 
455 	opri = splbio();
456 
457 	/*
458 	 * Place it in the queue of disk activities for this disk
459 	 */
460 	disksort(&cd->buf_queue, bp);
461 
462 	/*
463 	 * Tell the device to get going on the transfer if it's
464 	 * not doing anything, otherwise just wait for completion
465 	 */
466 	cdstart(cd);
467 
468 	splx(opri);
469 	return;
470 
471 bad:
472 	bp->b_flags |= B_ERROR;
473 done:
474 	/*
475 	 * Correctly set the buf to indicate a completed xfer
476 	 */
477 	bp->b_resid = bp->b_bcount;
478 	biodone(bp);
479 }
480 
481 /*
482  * cdstart looks to see if there is a buf waiting for the device
483  * and that the device is not already busy. If both are true,
484  * It deques the buf and creates a scsi command to perform the
485  * transfer in the buf. The transfer request will call scsipi_done
486  * on completion, which will in turn call this routine again
487  * so that the next queued transfer is performed.
488  * The bufs are queued by the strategy routine (cdstrategy)
489  *
490  * This routine is also called after other non-queued requests
491  * have been made of the scsi driver, to ensure that the queue
492  * continues to be drained.
493  *
494  * must be called at the correct (highish) spl level
495  * cdstart() is called at splbio from cdstrategy and scsipi_done
496  */
497 void
498 cdstart(v)
499 	register void *v;
500 {
501 	register struct cd_softc *cd = v;
502 	register struct scsipi_link *sc_link = cd->sc_link;
503 	struct disklabel *lp = cd->sc_dk.dk_label;
504 	struct buf *bp = 0;
505 	struct buf *dp;
506 	struct scsipi_rw_big cmd_big;
507 #if NCD_SCSIBUS > 0
508 	struct scsi_rw cmd_small;
509 #endif
510 	struct scsipi_generic *cmdp;
511 	int blkno, nblks, cmdlen;
512 	struct partition *p;
513 
514 	SC_DEBUG(sc_link, SDEV_DB2, ("cdstart "));
515 	/*
516 	 * Check if the device has room for another command
517 	 */
518 	while (sc_link->openings > 0) {
519 		/*
520 		 * there is excess capacity, but a special waits
521 		 * It'll need the adapter as soon as we clear out of the
522 		 * way and let it run (user level wait).
523 		 */
524 		if (sc_link->flags & SDEV_WAITING) {
525 			sc_link->flags &= ~SDEV_WAITING;
526 			wakeup((caddr_t)sc_link);
527 			return;
528 		}
529 
530 		/*
531 		 * See if there is a buf with work for us to do..
532 		 */
533 		dp = &cd->buf_queue;
534 		if ((bp = dp->b_actf) == NULL)	/* yes, an assign */
535 			return;
536 		dp->b_actf = bp->b_actf;
537 
538 		/*
539 		 * If the device has become invalid, abort all the
540 		 * reads and writes until all files have been closed and
541 		 * re-opened
542 		 */
543 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
544 			bp->b_error = EIO;
545 			bp->b_flags |= B_ERROR;
546 			bp->b_resid = bp->b_bcount;
547 			biodone(bp);
548 			continue;
549 		}
550 
551 		/*
552 		 * We have a buf, now we should make a command
553 		 *
554 		 * First, translate the block to absolute and put it in terms
555 		 * of the logical blocksize of the device.
556 		 */
557 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
558 		if (CDPART(bp->b_dev) != RAW_PART) {
559 			p = &lp->d_partitions[CDPART(bp->b_dev)];
560 			blkno += p->p_offset;
561 		}
562 		nblks = howmany(bp->b_bcount, lp->d_secsize);
563 
564 #if NCD_SCSIBUS > 0
565 		/*
566 		 *  Fill out the scsi command.  If the transfer will
567 		 *  fit in a "small" cdb, use it.
568 		 */
569 		if (((blkno & 0x1fffff) == blkno) &&
570 		    ((nblks & 0xff) == nblks) && sc_link->type == BUS_SCSI) {
571 			/*
572 			 * We can fit in a small cdb.
573 			 */
574 			bzero(&cmd_small, sizeof(cmd_small));
575 			cmd_small.opcode = (bp->b_flags & B_READ) ?
576 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
577 			_lto3b(blkno, cmd_small.addr);
578 			cmd_small.length = nblks & 0xff;
579 			cmdlen = sizeof(cmd_small);
580 			cmdp = (struct scsipi_generic *)&cmd_small;
581 		} else
582 #endif
583 		{
584 			/*
585 			 * Need a large cdb.
586 			 */
587 			bzero(&cmd_big, sizeof(cmd_big));
588 			cmd_big.opcode = (bp->b_flags & B_READ) ?
589 			    READ_BIG : WRITE_BIG;
590 			_lto4b(blkno, cmd_big.addr);
591 			_lto2b(nblks, cmd_big.length);
592 			cmdlen = sizeof(cmd_big);
593 			cmdp = (struct scsipi_generic *)&cmd_big;
594 		}
595 
596 		/* Instrumentation. */
597 		disk_busy(&cd->sc_dk);
598 
599 		/*
600 		 * Call the routine that chats with the adapter.
601 		 * Note: we cannot sleep as we may be an interrupt
602 		 */
603 		if (scsipi_command(sc_link, cmdp, cmdlen, (u_char *)bp->b_data,
604 		    bp->b_bcount, CDRETRIES, 30000, bp, SCSI_NOSLEEP |
605 		    ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT))) {
606 			disk_unbusy(&cd->sc_dk, 0);
607 			printf("%s: not queued", cd->sc_dev.dv_xname);
608 		}
609 	}
610 }
611 
612 void
613 cddone(xs)
614 	struct scsipi_xfer *xs;
615 {
616 	struct cd_softc *cd = xs->sc_link->device_softc;
617 
618 	if (xs->bp != NULL) {
619 		disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
620 #if NRND > 0
621 		rnd_add_uint32(&cd->rnd_source, xs->bp->b_blkno);
622 #endif
623 	}
624 }
625 
626 void
627 cdminphys(bp)
628 	struct buf *bp;
629 {
630 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)];
631 	long max;
632 
633 	/*
634 	 * If the device is ancient, we want to make sure that
635 	 * the transfer fits into a 6-byte cdb.
636 	 *
637 	 * XXX Note that the SCSI-I spec says that 256-block transfers
638 	 * are allowed in a 6-byte read/write, and are specified
639 	 * by settng the "length" to 0.  However, we're conservative
640 	 * here, allowing only 255-block transfers in case an
641 	 * ancient device gets confused by length == 0.  A length of 0
642 	 * in a 10-byte read/write actually means 0 blocks.
643 	 */
644 	if (cd->flags & CDF_ANCIENT) {
645 		max = cd->sc_dk.dk_label->d_secsize * 0xff;
646 
647 		if (bp->b_bcount > max)
648 			bp->b_bcount = max;
649 	}
650 
651 	(*cd->sc_link->adapter->scsipi_minphys)(bp);
652 }
653 
654 int
655 cdread(dev, uio, ioflag)
656 	dev_t dev;
657 	struct uio *uio;
658 	int ioflag;
659 {
660 
661 	return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio));
662 }
663 
664 int
665 cdwrite(dev, uio, ioflag)
666 	dev_t dev;
667 	struct uio *uio;
668 	int ioflag;
669 {
670 
671 	return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio));
672 }
673 
674 /*
675  * conversion between minute-seconde-frame and logical block adress
676  * adresses format
677  */
678 void
679 lba2msf (lba, m, s, f)
680 	u_long lba;
681 	u_char *m, *s, *f;
682 {
683 	u_long tmp;
684 
685 	tmp = lba + CD_BLOCK_OFFSET;	/* offset of first logical frame */
686 	tmp &= 0xffffff;		/* negative lbas use only 24 bits */
687 	*m = tmp / (CD_SECS * CD_FRAMES);
688 	tmp %= (CD_SECS * CD_FRAMES);
689 	*s = tmp / CD_FRAMES;
690 	*f = tmp % CD_FRAMES;
691 }
692 
693 u_long
694 msf2lba (m, s, f)
695 	u_char m, s, f;
696 {
697 
698 	return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET);
699 }
700 
701 
702 /*
703  * Perform special action on behalf of the user.
704  * Knows about the internals of this device
705  */
706 int
707 cdioctl(dev, cmd, addr, flag, p)
708 	dev_t dev;
709 	u_long cmd;
710 	caddr_t addr;
711 	int flag;
712 	struct proc *p;
713 {
714 	struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)];
715 	int part = CDPART(dev);
716 	int error;
717 
718 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdioctl 0x%lx ", cmd));
719 
720 	/*
721 	 * If the device is not valid, some IOCTLs can still be
722 	 * handled on the raw partition. Check this here.
723 	 */
724 	if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
725 		switch (cmd) {
726 		case DIOCWLABEL:
727 		case DIOCLOCK:
728 		case ODIOCEJECT:
729 		case DIOCEJECT:
730 		case SCIOCIDENTIFY:
731 		case OSCIOCIDENTIFY:
732 		case SCIOCCOMMAND:
733 		case SCIOCDEBUG:
734 		case CDIOCGETVOL:
735 		case CDIOCSETVOL:
736 		case CDIOCSETMONO:
737 		case CDIOCSETSTEREO:
738 		case CDIOCSETMUTE:
739 		case CDIOCSETLEFT:
740 		case CDIOCSETRIGHT:
741 		case CDIOCCLOSE:
742 		case CDIOCEJECT:
743 		case CDIOCALLOW:
744 		case CDIOCPREVENT:
745 		case CDIOCSETDEBUG:
746 		case CDIOCCLRDEBUG:
747 		case CDIOCRESET:
748 		case SCIOCRESET:
749 		case CDIOCLOADUNLOAD:
750 			if (part == RAW_PART)
751 				break;
752 		/* FALLTHROUGH */
753 		default:
754 			if ((cd->sc_link->flags & SDEV_OPEN) == 0)
755 				return (ENODEV);
756 			else
757 				return (EIO);
758 		}
759 	}
760 
761 	switch (cmd) {
762 	case DIOCGDINFO:
763 		*(struct disklabel *)addr = *(cd->sc_dk.dk_label);
764 		return (0);
765 
766 	case DIOCGPART:
767 		((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label;
768 		((struct partinfo *)addr)->part =
769 		    &cd->sc_dk.dk_label->d_partitions[part];
770 		return (0);
771 
772 	case DIOCWDINFO:
773 	case DIOCSDINFO:
774 		if ((flag & FWRITE) == 0)
775 			return (EBADF);
776 
777 		if ((error = cdlock(cd)) != 0)
778 			return (error);
779 		cd->flags |= CDF_LABELLING;
780 
781 		error = setdisklabel(cd->sc_dk.dk_label,
782 		    (struct disklabel *)addr, /*cd->sc_dk.dk_openmask : */0,
783 		    cd->sc_dk.dk_cpulabel);
784 		if (error == 0) {
785 			/* XXX ? */
786 		}
787 
788 		cd->flags &= ~CDF_LABELLING;
789 		cdunlock(cd);
790 		return (error);
791 
792 	case DIOCWLABEL:
793 		return (EBADF);
794 
795 	case DIOCGDEFLABEL:
796 		cdgetdefaultlabel(cd, (struct disklabel *)addr);
797 		return (0);
798 
799 	case CDIOCPLAYTRACKS: {
800 		struct ioc_play_track *args = (struct ioc_play_track *)addr;
801 
802 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
803 			return (error);
804 		return (cd_play_tracks(cd, args->start_track,
805 		    args->start_index, args->end_track, args->end_index));
806 	}
807 	case CDIOCPLAYMSF: {
808 		struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
809 
810 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
811 			return (error);
812 		return (cd_play_msf(cd, args->start_m, args->start_s,
813 		    args->start_f, args->end_m, args->end_s, args->end_f));
814 	}
815 	case CDIOCPLAYBLOCKS: {
816 		struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
817 
818 		if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0)
819 			return (error);
820 		return (cd_play(cd, args->blk, args->len));
821 	}
822 	case CDIOCREADSUBCHANNEL: {
823 		struct ioc_read_subchannel *args =
824 		    (struct ioc_read_subchannel *)addr;
825 		struct cd_sub_channel_info data;
826 		int len = args->data_len;
827 
828 		if (len > sizeof(data) ||
829 		    len < sizeof(struct cd_sub_channel_header))
830 			return (EINVAL);
831 		error = cd_read_subchannel(cd, args->address_format,
832 		    args->data_format, args->track, &data, len);
833 		if (error)
834 			return (error);
835 		len = min(len, _2btol(data.header.data_len) +
836 		    sizeof(struct cd_sub_channel_header));
837 		return (copyout(&data, args->data, len));
838 	}
839 	case CDIOREADTOCHEADER: {
840 		struct ioc_toc_header th;
841 
842 		if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 0)) != 0)
843 			return (error);
844 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
845 #if BYTE_ORDER == BIG_ENDIAN
846 			bswap((u_int8_t *)&th.len, sizeof(th.len));
847 #endif
848 		} else
849 			th.len = ntohs(th.len);
850 		bcopy(&th, addr, sizeof(th));
851 		return (0);
852 	}
853 	case CDIOREADTOCENTRYS: {
854 		struct cd_toc toc;
855 		struct ioc_read_toc_entry *te =
856 		    (struct ioc_read_toc_entry *)addr;
857 		struct ioc_toc_header *th;
858 		struct cd_toc_entry *cte;
859 		int len = te->data_len;
860 		int ntracks;
861 
862 		th = &toc.header;
863 
864 		if (len > sizeof(toc.entries) ||
865 		    len < sizeof(struct cd_toc_entry))
866 			return (EINVAL);
867 		error = cd_read_toc(cd, te->address_format, te->starting_track,
868 		    &toc, len + sizeof(struct ioc_toc_header), 0);
869 		if (error)
870 			return (error);
871 		if (te->address_format == CD_LBA_FORMAT)
872 			for (ntracks =
873 			    th->ending_track - th->starting_track + 1;
874 			    ntracks >= 0; ntracks--) {
875 				cte = &toc.entries[ntracks];
876 				cte->addr_type = CD_LBA_FORMAT;
877 				if (cd->sc_link->quirks & ADEV_LITTLETOC) {
878 #if BYTE_ORDER == BIG_ENDIAN
879 					bswap((u_int8_t*)&cte->addr,
880 					    sizeof(cte->addr));
881 #endif
882 				} else
883 					cte->addr.lba = ntohl(cte->addr.lba);
884 			}
885 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
886 #if BYTE_ORDER == BIG_ENDIAN
887 			bswap((u_int8_t*)&th->len, sizeof(th->len));
888 #endif
889 		} else
890 			th->len = ntohs(th->len);
891 		len = min(len, th->len - (sizeof(th->starting_track) +
892 		    sizeof(th->ending_track)));
893 		return (copyout(toc.entries, te->data, len));
894 	}
895 	case CDIOREADMSADDR: {
896 		struct cd_toc toc;
897 		int sessno = *(int*)addr;
898 		struct cd_toc_entry *cte;
899 
900 		if (sessno != 0)
901 			return (EINVAL);
902 
903 		error = cd_read_toc(cd, 0, 0, &toc,
904 		  sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
905 		  0x40 /* control word for "get MS info" */);
906 
907 		if (error)
908 			return (error);
909 
910 		cte = &toc.entries[0];
911 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
912 #if BYTE_ORDER == BIG_ENDIAN
913 			bswap((u_int8_t*)&cte->addr, sizeof(cte->addr));
914 #endif
915 		} else
916 			cte->addr.lba = ntohl(cte->addr.lba);
917 		if (cd->sc_link->quirks & ADEV_LITTLETOC) {
918 #if BYTE_ORDER == BIG_ENDIAN
919 			bswap((u_int8_t*)&toc.header.len, sizeof(toc.header.len));
920 #endif
921 		} else
922 			toc.header.len = ntohs(toc.header.len);
923 
924 		*(int*)addr = (toc.header.len >= 10 && cte->track > 1) ?
925 			cte->addr.lba : 0;
926 		return 0;
927 	}
928 	case CDIOCSETPATCH: {
929 		struct ioc_patch *arg = (struct ioc_patch *)addr;
930 
931 		return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0],
932 		    arg->patch[1], arg->patch[2], arg->patch[3], 0));
933 	}
934 	case CDIOCGETVOL: {
935 		struct ioc_vol *arg = (struct ioc_vol *)addr;
936 
937 		return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0));
938 	}
939 	case CDIOCSETVOL: {
940 		struct ioc_vol *arg = (struct ioc_vol *)addr;
941 
942 		return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0));
943 	}
944 
945 	case CDIOCSETMONO:
946 		return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL,
947 		    BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
948 
949 	case CDIOCSETSTEREO:
950 		return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL,
951 		    RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
952 
953 	case CDIOCSETMUTE:
954 		return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL,
955 		    MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
956 
957 	case CDIOCSETLEFT:
958 		return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL,
959 		    LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
960 
961 	case CDIOCSETRIGHT:
962 		return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL,
963 		    RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0));
964 
965 	case CDIOCRESUME:
966 		return (cd_pause(cd, PA_RESUME));
967 	case CDIOCPAUSE:
968 		return (cd_pause(cd, PA_PAUSE));
969 	case CDIOCSTART:
970 		return (scsipi_start(cd->sc_link, SSS_START, 0));
971 	case CDIOCSTOP:
972 		return (scsipi_start(cd->sc_link, SSS_STOP, 0));
973 	case CDIOCCLOSE:
974 		return (scsipi_start(cd->sc_link, SSS_START|SSS_LOEJ,
975 		    SCSI_IGNORE_MEDIA_CHANGE));
976 	case DIOCEJECT:
977 		if (*(int *)addr == 0) {
978 			/*
979 			 * Don't force eject: check that we are the only
980 			 * partition open. If so, unlock it.
981 			 */
982 			if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
983 			    cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask ==
984 			    cd->sc_dk.dk_openmask) {
985 				error =  scsipi_prevent(cd->sc_link, PR_ALLOW,
986 				    SCSI_IGNORE_NOT_READY);
987 				if (error)
988 					return (error);
989 			} else {
990 				return (EBUSY);
991 			}
992 		}
993 		/* FALLTHROUGH */
994 	case CDIOCEJECT: /* FALLTHROUGH */
995 	case ODIOCEJECT:
996 		return (scsipi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0));
997 	case CDIOCALLOW:
998 		return (scsipi_prevent(cd->sc_link, PR_ALLOW, 0));
999 	case CDIOCPREVENT:
1000 		return (scsipi_prevent(cd->sc_link, PR_PREVENT, 0));
1001 	case DIOCLOCK:
1002 		return (scsipi_prevent(cd->sc_link,
1003 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1004 	case CDIOCSETDEBUG:
1005 		cd->sc_link->flags |= (SDEV_DB1 | SDEV_DB2);
1006 		return (0);
1007 	case CDIOCCLRDEBUG:
1008 		cd->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2);
1009 		return (0);
1010 	case CDIOCRESET:
1011 	case SCIOCRESET:
1012 		return (cd_reset(cd));
1013 	case CDIOCLOADUNLOAD: {
1014 		struct ioc_load_unload *args = (struct ioc_load_unload *)addr;
1015 
1016 		return ((*cd->sc_ops->cdo_load_unload)(cd, args->options,
1017 			args->slot));
1018 	}
1019 
1020 	default:
1021 		if (part != RAW_PART)
1022 			return (ENOTTY);
1023 		return (scsipi_do_ioctl(cd->sc_link, dev, cmd, addr, flag, p));
1024 	}
1025 
1026 #ifdef DIAGNOSTIC
1027 	panic("cdioctl: impossible");
1028 #endif
1029 }
1030 
1031 void
1032 cdgetdefaultlabel(cd, lp)
1033 	struct cd_softc *cd;
1034 	struct disklabel *lp;
1035 {
1036 
1037 	bzero(lp, sizeof(struct disklabel));
1038 
1039 	lp->d_secsize = cd->params.blksize;
1040 	lp->d_ntracks = 1;
1041 	lp->d_nsectors = 100;
1042 	lp->d_ncylinders = (cd->params.disksize / 100) + 1;
1043 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1044 
1045 	switch (cd->sc_link->type) {
1046 #if NCD_SCSIBUS > 0
1047 	    case BUS_SCSI:
1048 		lp->d_type = DTYPE_SCSI;
1049 		break;
1050 #endif
1051 #if NCD_ATAPIBUS > 0
1052 	    case BUS_ATAPI:
1053 		lp->d_type = DTYPE_ATAPI;
1054 		break;
1055 #endif
1056 	}
1057 	strncpy(lp->d_typename, cd->name, 16);
1058 	strncpy(lp->d_packname, "fictitious", 16);
1059 	lp->d_secperunit = cd->params.disksize;
1060 	lp->d_rpm = 300;
1061 	lp->d_interleave = 1;
1062 	lp->d_flags = D_REMOVABLE;
1063 
1064 	lp->d_partitions[0].p_offset = 0;
1065 	lp->d_partitions[0].p_size =
1066 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1067 	lp->d_partitions[0].p_fstype = FS_ISO9660;
1068 	lp->d_partitions[RAW_PART].p_offset = 0;
1069 	lp->d_partitions[RAW_PART].p_size =
1070 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1071 	lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
1072 	lp->d_npartitions = RAW_PART + 1;
1073 
1074 	lp->d_magic = DISKMAGIC;
1075 	lp->d_magic2 = DISKMAGIC;
1076 	lp->d_checksum = dkcksum(lp);
1077 }
1078 
1079 /*
1080  * Load the label information on the named device
1081  * Actually fabricate a disklabel
1082  *
1083  * EVENTUALLY take information about different
1084  * data tracks from the TOC and put it in the disklabel
1085  */
1086 void
1087 cdgetdisklabel(cd)
1088 	struct cd_softc *cd;
1089 {
1090 	struct disklabel *lp = cd->sc_dk.dk_label;
1091 
1092 	bzero(cd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
1093 
1094 	cdgetdefaultlabel(cd, lp);
1095 }
1096 
1097 /*
1098  * Find out from the device what it's capacity is
1099  */
1100 u_long
1101 cd_size(cd, flags)
1102 	struct cd_softc *cd;
1103 	int flags;
1104 {
1105 	struct scsipi_read_cd_cap_data rdcap;
1106 	struct scsipi_read_cd_capacity scsipi_cmd;
1107 	int blksize;
1108 	u_long size;
1109 
1110 	if (cd->sc_link->quirks & ADEV_NOCAPACITY) {
1111 		/*
1112 		 * the drive doesn't support the READ_CD_CAPACITY command
1113 		 * use a fake size
1114 		 */
1115 		cd->params.blksize = 2048;
1116 		cd->params.disksize = 400000;
1117 		return (400000);
1118 	}
1119 
1120 	/*
1121 	 * make up a scsi command and ask the scsi driver to do
1122 	 * it for you.
1123 	 */
1124 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1125 	scsipi_cmd.opcode = READ_CD_CAPACITY;
1126 
1127 	/*
1128 	 * If the command works, interpret the result as a 4 byte
1129 	 * number of blocks and a blocksize
1130 	 */
1131 	if (scsipi_command(cd->sc_link,
1132 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1133 	    (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 20000, NULL,
1134 	    flags | SCSI_DATA_IN) != 0)
1135 		return (0);
1136 
1137 	blksize = _4btol(rdcap.length);
1138 	if ((blksize < 512) || ((blksize & 511) != 0))
1139 		blksize = 2048;	/* some drives lie ! */
1140 	cd->params.blksize = blksize;
1141 
1142 	size = _4btol(rdcap.addr) + 1;
1143 	if (size < 100)
1144 		size = 400000;	/* ditto */
1145 	cd->params.disksize = size;
1146 
1147 	SC_DEBUG(cd->sc_link, SDEV_DB2, ("cd_size: %d %ld\n", blksize, size));
1148 	return (size);
1149 }
1150 
1151 /*
1152  * Get scsi driver to send a "start playing" command
1153  */
1154 int
1155 cd_play(cd, blkno, nblks)
1156 	struct cd_softc *cd;
1157 	int blkno, nblks;
1158 {
1159 	struct scsipi_play scsipi_cmd;
1160 
1161 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1162 	scsipi_cmd.opcode = PLAY;
1163 	_lto4b(blkno, scsipi_cmd.blk_addr);
1164 	_lto2b(nblks, scsipi_cmd.xfer_len);
1165 	return (scsipi_command(cd->sc_link,
1166 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1167 	    0, 0, CDRETRIES, 200000, NULL, 0));
1168 }
1169 
1170 /*
1171  * Get scsi driver to send a "start playing" command
1172  */
1173 int
1174 cd_play_tracks(cd, strack, sindex, etrack, eindex)
1175 	struct cd_softc *cd;
1176 	int strack, sindex, etrack, eindex;
1177 {
1178 	struct cd_toc toc;
1179 	int error;
1180 
1181 	if (!etrack)
1182 		return (EIO);
1183 	if (strack > etrack)
1184 		return (EINVAL);
1185 
1186 	if ((error = cd_load_toc(cd, &toc)) != 0)
1187 		return (error);
1188 
1189 	if (++etrack > (toc.header.ending_track+1))
1190 		etrack = toc.header.ending_track+1;
1191 
1192 	strack -= toc.header.starting_track;
1193 	etrack -= toc.header.starting_track;
1194 	if (strack < 0)
1195 		return (EINVAL);
1196 
1197 	return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute,
1198 	    toc.entries[strack].addr.msf.second,
1199 	    toc.entries[strack].addr.msf.frame,
1200 	    toc.entries[etrack].addr.msf.minute,
1201 	    toc.entries[etrack].addr.msf.second,
1202 	    toc.entries[etrack].addr.msf.frame));
1203 }
1204 
1205 /*
1206  * Get scsi driver to send a "play msf" command
1207  */
1208 int
1209 cd_play_msf(cd, startm, starts, startf, endm, ends, endf)
1210 	struct cd_softc *cd;
1211 	int startm, starts, startf, endm, ends, endf;
1212 {
1213 	struct scsipi_play_msf scsipi_cmd;
1214 
1215 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1216 	scsipi_cmd.opcode = PLAY_MSF;
1217 	scsipi_cmd.start_m = startm;
1218 	scsipi_cmd.start_s = starts;
1219 	scsipi_cmd.start_f = startf;
1220 	scsipi_cmd.end_m = endm;
1221 	scsipi_cmd.end_s = ends;
1222 	scsipi_cmd.end_f = endf;
1223 	return (scsipi_command(cd->sc_link,
1224 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1225 	    0, 0, CDRETRIES, 2000, NULL, 0));
1226 }
1227 
1228 /*
1229  * Get scsi driver to send a "start up" command
1230  */
1231 int
1232 cd_pause(cd, go)
1233 	struct cd_softc *cd;
1234 	int go;
1235 {
1236 	struct scsipi_pause scsipi_cmd;
1237 
1238 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1239 	scsipi_cmd.opcode = PAUSE;
1240 	scsipi_cmd.resume = go & 0xff;
1241 	return (scsipi_command(cd->sc_link,
1242 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1243 	    0, 0, CDRETRIES, 2000, NULL, 0));
1244 }
1245 
1246 /*
1247  * Get scsi driver to send a "RESET" command
1248  */
1249 int
1250 cd_reset(cd)
1251 	struct cd_softc *cd;
1252 {
1253 
1254 	return (scsipi_command(cd->sc_link, 0, 0, 0, 0,
1255 	    CDRETRIES, 2000, NULL, SCSI_RESET));
1256 }
1257 
1258 /*
1259  * Read subchannel
1260  */
1261 int
1262 cd_read_subchannel(cd, mode, format, track, data, len)
1263 	struct cd_softc *cd;
1264 	int mode, format, track, len;
1265 	struct cd_sub_channel_info *data;
1266 {
1267 	struct scsipi_read_subchannel scsipi_cmd;
1268 
1269 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1270 	scsipi_cmd.opcode = READ_SUBCHANNEL;
1271 	if (mode == CD_MSF_FORMAT)
1272 		scsipi_cmd.byte2 |= CD_MSF;
1273 	scsipi_cmd.byte3 = SRS_SUBQ;
1274 	scsipi_cmd.subchan_format = format;
1275 	scsipi_cmd.track = track;
1276 	_lto2b(len, scsipi_cmd.data_len);
1277 	return (scsipi_command(cd->sc_link,
1278 	    (struct scsipi_generic *)&scsipi_cmd,
1279 	    sizeof(struct scsipi_read_subchannel), (u_char *)data, len,
1280 	    CDRETRIES, 5000, NULL, SCSI_DATA_IN|SCSI_SILENT));
1281 }
1282 
1283 /*
1284  * Read table of contents
1285  */
1286 int
1287 cd_read_toc(cd, mode, start, data, len, control)
1288 	struct cd_softc *cd;
1289 	int mode, start, len, control;
1290 	void *data;
1291 {
1292 	struct scsipi_read_toc scsipi_cmd;
1293 	int ntoc;
1294 
1295 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1296 #if 0
1297 	if (len != sizeof(struct ioc_toc_header))
1298 		ntoc = ((len) - sizeof(struct ioc_toc_header)) /
1299 		    sizeof(struct cd_toc_entry);
1300 	else
1301 #endif
1302 	ntoc = len;
1303 	scsipi_cmd.opcode = READ_TOC;
1304 	if (mode == CD_MSF_FORMAT)
1305 		scsipi_cmd.byte2 |= CD_MSF;
1306 	scsipi_cmd.from_track = start;
1307 	_lto2b(ntoc, scsipi_cmd.data_len);
1308 	scsipi_cmd.control = control;
1309 	return (scsipi_command(cd->sc_link,
1310 	    (struct scsipi_generic *)&scsipi_cmd,
1311 	    sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES,
1312 	    5000, NULL, SCSI_DATA_IN));
1313 }
1314 
1315 int
1316 cd_load_toc(cd, toc)
1317 	struct cd_softc *cd;
1318 	struct cd_toc *toc;
1319 {
1320 	int ntracks, len, error;
1321 
1322 	if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 0)) != 0)
1323 		return (error);
1324 
1325 	ntracks = toc->header.ending_track - toc->header.starting_track + 1;
1326 	len = (ntracks + 1) * sizeof(struct cd_toc_entry) +
1327 	    sizeof(toc->header);
1328 	if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 0)) != 0)
1329 		return (error);
1330 	return (0);
1331 }
1332 
1333 /*
1334  * Get the scsi driver to send a full inquiry to the device and use the
1335  * results to fill out the disk parameter structure.
1336  */
1337 int
1338 cd_get_parms(cd, flags)
1339 	struct cd_softc *cd;
1340 	int flags;
1341 {
1342 
1343 	/*
1344 	 * give a number of sectors so that sec * trks * cyls
1345 	 * is <= disk_size
1346 	 */
1347 	if (cd_size(cd, flags) == 0)
1348 		return (ENXIO);
1349 	return (0);
1350 }
1351 
1352 int
1353 cdsize(dev)
1354 	dev_t dev;
1355 {
1356 
1357 	/* CD-ROMs are read-only. */
1358 	return (-1);
1359 }
1360 
1361 int
1362 cddump(dev, blkno, va, size)
1363 	dev_t dev;
1364 	daddr_t blkno;
1365 	caddr_t va;
1366 	size_t size;
1367 {
1368 
1369 	/* Not implemented. */
1370 	return (ENXIO);
1371 }
1372