1 /* $NetBSD: cd.c,v 1.100 1997/04/02 02:29:30 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles M. Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Originally written by Julian Elischer (julian@tfs.com) 34 * for TRW Financial Systems for use under the MACH(2.5) operating system. 35 * 36 * TRW Financial Systems, in accordance with their agreement with Carnegie 37 * Mellon University, makes this software available to CMU to distribute 38 * or use in any manner that they see fit as long as this message is kept with 39 * the software. For this reason TFS also grants any other persons or 40 * organisations permission to use or modify this software. 41 * 42 * TFS supplies this software to be publicly redistributed 43 * on the understanding that TFS is not responsible for the correct 44 * functioning of this software in any circumstances. 45 * 46 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 47 */ 48 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/file.h> 54 #include <sys/stat.h> 55 #include <sys/ioctl.h> 56 #include <sys/buf.h> 57 #include <sys/uio.h> 58 #include <sys/malloc.h> 59 #include <sys/errno.h> 60 #include <sys/device.h> 61 #include <sys/disklabel.h> 62 #include <sys/disk.h> 63 #include <sys/cdio.h> 64 #include <sys/proc.h> 65 #include <sys/conf.h> 66 67 #include <scsi/scsi_all.h> 68 #include <scsi/scsi_cd.h> 69 #include <scsi/scsi_disk.h> /* rw_big and start_stop come from there */ 70 #include <scsi/scsiconf.h> 71 72 #define CDOUTSTANDING 4 73 #define CDRETRIES 1 74 75 #define CDUNIT(z) DISKUNIT(z) 76 #define CDPART(z) DISKPART(z) 77 #define MAKECDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 78 79 struct cd_softc { 80 struct device sc_dev; 81 struct disk sc_dk; 82 83 int flags; 84 #define CDF_LOCKED 0x01 85 #define CDF_WANTED 0x02 86 #define CDF_WLABEL 0x04 /* label is writable */ 87 #define CDF_LABELLING 0x08 /* writing label */ 88 #define CDF_ANCIENT 0x10 /* disk is ancient; for minphys */ 89 struct scsi_link *sc_link; /* contains our targ, lun, etc. */ 90 struct cd_parms { 91 int blksize; 92 u_long disksize; /* total number sectors */ 93 } params; 94 struct buf buf_queue; 95 }; 96 97 #ifdef __BROKEN_INDIRECT_CONFIG 98 int cdmatch __P((struct device *, void *, void *)); 99 #else 100 int cdmatch __P((struct device *, struct cfdata *, void *)); 101 #endif 102 void cdattach __P((struct device *, struct device *, void *)); 103 int cdlock __P((struct cd_softc *)); 104 void cdunlock __P((struct cd_softc *)); 105 void cdstart __P((void *)); 106 void cdminphys __P((struct buf *)); 107 void cdgetdisklabel __P((struct cd_softc *)); 108 void cddone __P((struct scsi_xfer *)); 109 u_long cd_size __P((struct cd_softc *, int)); 110 int cd_get_mode __P((struct cd_softc *, struct cd_mode_data *, int)); 111 int cd_set_mode __P((struct cd_softc *, struct cd_mode_data *)); 112 int cd_play __P((struct cd_softc *, int, int )); 113 int cd_play_big __P((struct cd_softc *, int, int )); 114 int cd_play_tracks __P((struct cd_softc *, int, int, int, int )); 115 int cd_play_msf __P((struct cd_softc *, int, int, int, int, int, int )); 116 int cd_pause __P((struct cd_softc *, int)); 117 int cd_reset __P((struct cd_softc *)); 118 int cd_read_subchannel __P((struct cd_softc *, int, int, int, 119 struct cd_sub_channel_info *, int )); 120 int cd_read_toc __P((struct cd_softc *, int, int, struct cd_toc_entry *, 121 int )); 122 int cd_get_parms __P((struct cd_softc *, int)); 123 124 struct cfattach cd_ca = { 125 sizeof(struct cd_softc), cdmatch, cdattach 126 }; 127 128 struct cfdriver cd_cd = { 129 NULL, "cd", DV_DISK 130 }; 131 132 struct dkdriver cddkdriver = { cdstrategy }; 133 134 struct scsi_device cd_switch = { 135 NULL, /* use default error handler */ 136 cdstart, /* we have a queue, which is started by this */ 137 NULL, /* we do not have an async handler */ 138 cddone, /* deal with stats at interrupt time */ 139 }; 140 141 struct scsi_inquiry_pattern cd_patterns[] = { 142 {T_CDROM, T_REMOV, 143 "", "", ""}, 144 #if 0 145 {T_CDROM, T_REMOV, /* more luns */ 146 "PIONEER ", "CD-ROM DRM-600 ", ""}, 147 #endif 148 }; 149 150 int 151 cdmatch(parent, match, aux) 152 struct device *parent; 153 #ifdef __BROKEN_INDIRECT_CONFIG 154 void *match; 155 #else 156 struct cfdata *match; 157 #endif 158 void *aux; 159 { 160 struct scsibus_attach_args *sa = aux; 161 int priority; 162 163 (void)scsi_inqmatch(sa->sa_inqbuf, 164 (caddr_t)cd_patterns, sizeof(cd_patterns)/sizeof(cd_patterns[0]), 165 sizeof(cd_patterns[0]), &priority); 166 return (priority); 167 } 168 169 /* 170 * The routine called by the low level scsi routine when it discovers 171 * A device suitable for this driver 172 */ 173 void 174 cdattach(parent, self, aux) 175 struct device *parent, *self; 176 void *aux; 177 { 178 struct cd_softc *cd = (void *)self; 179 struct scsibus_attach_args *sa = aux; 180 struct scsi_link *sc_link = sa->sa_sc_link; 181 182 SC_DEBUG(sc_link, SDEV_DB2, ("cdattach: ")); 183 184 /* 185 * Store information needed to contact our base driver 186 */ 187 cd->sc_link = sc_link; 188 sc_link->device = &cd_switch; 189 sc_link->device_softc = cd; 190 if (sc_link->openings > CDOUTSTANDING) 191 sc_link->openings = CDOUTSTANDING; 192 193 /* 194 * Initialize and attach the disk structure. 195 */ 196 cd->sc_dk.dk_driver = &cddkdriver; 197 cd->sc_dk.dk_name = cd->sc_dev.dv_xname; 198 disk_attach(&cd->sc_dk); 199 200 #if !defined(i386) 201 dk_establish(&cd->sc_dk, &cd->sc_dev); /* XXX */ 202 #endif 203 204 /* 205 * Note if this device is ancient. This is used in cdminphys(). 206 */ 207 if ((sa->sa_inqbuf->version & SID_ANSII) == 0) 208 cd->flags |= CDF_ANCIENT; 209 210 printf("\n"); 211 } 212 213 /* 214 * Wait interruptibly for an exclusive lock. 215 * 216 * XXX 217 * Several drivers do this; it should be abstracted and made MP-safe. 218 */ 219 int 220 cdlock(cd) 221 struct cd_softc *cd; 222 { 223 int error; 224 225 while ((cd->flags & CDF_LOCKED) != 0) { 226 cd->flags |= CDF_WANTED; 227 if ((error = tsleep(cd, PRIBIO | PCATCH, "cdlck", 0)) != 0) 228 return error; 229 } 230 cd->flags |= CDF_LOCKED; 231 return 0; 232 } 233 234 /* 235 * Unlock and wake up any waiters. 236 */ 237 void 238 cdunlock(cd) 239 struct cd_softc *cd; 240 { 241 242 cd->flags &= ~CDF_LOCKED; 243 if ((cd->flags & CDF_WANTED) != 0) { 244 cd->flags &= ~CDF_WANTED; 245 wakeup(cd); 246 } 247 } 248 249 /* 250 * open the device. Make sure the partition info is a up-to-date as can be. 251 */ 252 int 253 cdopen(dev, flag, fmt, p) 254 dev_t dev; 255 int flag, fmt; 256 struct proc *p; 257 { 258 struct cd_softc *cd; 259 struct scsi_link *sc_link; 260 int unit, part; 261 int error; 262 263 unit = CDUNIT(dev); 264 if (unit >= cd_cd.cd_ndevs) 265 return ENXIO; 266 cd = cd_cd.cd_devs[unit]; 267 if (!cd) 268 return ENXIO; 269 270 sc_link = cd->sc_link; 271 272 SC_DEBUG(sc_link, SDEV_DB1, 273 ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 274 cd_cd.cd_ndevs, CDPART(dev))); 275 276 if ((error = cdlock(cd)) != 0) 277 return error; 278 279 if (cd->sc_dk.dk_openmask != 0) { 280 /* 281 * If any partition is open, but the disk has been invalidated, 282 * disallow further opens. 283 */ 284 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 285 error = EIO; 286 goto bad3; 287 } 288 } else { 289 /* Check that it is still responding and ok. */ 290 error = scsi_test_unit_ready(sc_link, 291 SCSI_IGNORE_ILLEGAL_REQUEST | 292 SCSI_IGNORE_MEDIA_CHANGE | 293 SCSI_IGNORE_NOT_READY); 294 if (error) 295 goto bad3; 296 297 /* Start the pack spinning if necessary. */ 298 error = scsi_start(sc_link, SSS_START, 299 SCSI_IGNORE_ILLEGAL_REQUEST | 300 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 301 if (error) 302 goto bad3; 303 304 sc_link->flags |= SDEV_OPEN; 305 306 /* Lock the pack in. */ 307 error = scsi_prevent(sc_link, PR_PREVENT, 308 SCSI_IGNORE_ILLEGAL_REQUEST | 309 SCSI_IGNORE_MEDIA_CHANGE); 310 if (error) 311 goto bad; 312 313 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 314 sc_link->flags |= SDEV_MEDIA_LOADED; 315 316 /* Load the physical device parameters. */ 317 if (cd_get_parms(cd, 0) != 0) { 318 error = ENXIO; 319 goto bad2; 320 } 321 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded ")); 322 323 /* Fabricate a disk label. */ 324 cdgetdisklabel(cd); 325 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated ")); 326 } 327 } 328 329 part = CDPART(dev); 330 331 /* Check that the partition exists. */ 332 if (part != RAW_PART && 333 (part >= cd->sc_dk.dk_label->d_npartitions || 334 cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 335 error = ENXIO; 336 goto bad; 337 } 338 339 /* Insure only one open at a time. */ 340 switch (fmt) { 341 case S_IFCHR: 342 cd->sc_dk.dk_copenmask |= (1 << part); 343 break; 344 case S_IFBLK: 345 cd->sc_dk.dk_bopenmask |= (1 << part); 346 break; 347 } 348 cd->sc_dk.dk_openmask = cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 349 350 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 351 cdunlock(cd); 352 return 0; 353 354 bad2: 355 sc_link->flags &= ~SDEV_MEDIA_LOADED; 356 357 bad: 358 if (cd->sc_dk.dk_openmask == 0) { 359 scsi_prevent(sc_link, PR_ALLOW, 360 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 361 sc_link->flags &= ~SDEV_OPEN; 362 } 363 364 bad3: 365 cdunlock(cd); 366 return error; 367 } 368 369 /* 370 * close the device.. only called if we are the LAST 371 * occurence of an open device 372 */ 373 int 374 cdclose(dev, flag, fmt, p) 375 dev_t dev; 376 int flag, fmt; 377 struct proc *p; 378 { 379 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 380 int part = CDPART(dev); 381 int error; 382 383 if ((error = cdlock(cd)) != 0) 384 return error; 385 386 switch (fmt) { 387 case S_IFCHR: 388 cd->sc_dk.dk_copenmask &= ~(1 << part); 389 break; 390 case S_IFBLK: 391 cd->sc_dk.dk_bopenmask &= ~(1 << part); 392 break; 393 } 394 cd->sc_dk.dk_openmask = cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 395 396 if (cd->sc_dk.dk_openmask == 0) { 397 /* XXXX Must wait for I/O to complete! */ 398 399 scsi_prevent(cd->sc_link, PR_ALLOW, 400 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY); 401 cd->sc_link->flags &= ~SDEV_OPEN; 402 } 403 404 cdunlock(cd); 405 return 0; 406 } 407 408 /* 409 * Actually translate the requested transfer into one the physical driver can 410 * understand. The transfer is described by a buf and will include only one 411 * physical transfer. 412 */ 413 void 414 cdstrategy(bp) 415 struct buf *bp; 416 { 417 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 418 int opri; 419 420 SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdstrategy ")); 421 SC_DEBUG(cd->sc_link, SDEV_DB1, 422 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 423 /* 424 * The transfer must be a whole number of blocks. 425 */ 426 if ((bp->b_bcount % cd->sc_dk.dk_label->d_secsize) != 0) { 427 bp->b_error = EINVAL; 428 goto bad; 429 } 430 /* 431 * If the device has been made invalid, error out 432 * maybe the media changed 433 */ 434 if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 435 bp->b_error = EIO; 436 goto bad; 437 } 438 /* 439 * If it's a null transfer, return immediately 440 */ 441 if (bp->b_bcount == 0) 442 goto done; 443 444 /* 445 * Do bounds checking, adjust transfer. if error, process. 446 * If end of partition, just return. 447 */ 448 if (CDPART(bp->b_dev) != RAW_PART && 449 bounds_check_with_label(bp, cd->sc_dk.dk_label, 450 (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0) 451 goto done; 452 453 opri = splbio(); 454 455 /* 456 * Place it in the queue of disk activities for this disk 457 */ 458 disksort(&cd->buf_queue, bp); 459 460 /* 461 * Tell the device to get going on the transfer if it's 462 * not doing anything, otherwise just wait for completion 463 */ 464 cdstart(cd); 465 466 splx(opri); 467 return; 468 469 bad: 470 bp->b_flags |= B_ERROR; 471 done: 472 /* 473 * Correctly set the buf to indicate a completed xfer 474 */ 475 bp->b_resid = bp->b_bcount; 476 biodone(bp); 477 } 478 479 /* 480 * cdstart looks to see if there is a buf waiting for the device 481 * and that the device is not already busy. If both are true, 482 * It deques the buf and creates a scsi command to perform the 483 * transfer in the buf. The transfer request will call scsi_done 484 * on completion, which will in turn call this routine again 485 * so that the next queued transfer is performed. 486 * The bufs are queued by the strategy routine (cdstrategy) 487 * 488 * This routine is also called after other non-queued requests 489 * have been made of the scsi driver, to ensure that the queue 490 * continues to be drained. 491 * 492 * must be called at the correct (highish) spl level 493 * cdstart() is called at splbio from cdstrategy and scsi_done 494 */ 495 void 496 cdstart(v) 497 register void *v; 498 { 499 register struct cd_softc *cd = v; 500 register struct scsi_link *sc_link = cd->sc_link; 501 struct buf *bp = 0; 502 struct buf *dp; 503 struct scsi_rw_big cmd_big; 504 struct scsi_rw cmd_small; 505 struct scsi_generic *cmdp; 506 int blkno, nblks, cmdlen; 507 struct partition *p; 508 509 SC_DEBUG(sc_link, SDEV_DB2, ("cdstart ")); 510 /* 511 * Check if the device has room for another command 512 */ 513 while (sc_link->openings > 0) { 514 /* 515 * there is excess capacity, but a special waits 516 * It'll need the adapter as soon as we clear out of the 517 * way and let it run (user level wait). 518 */ 519 if (sc_link->flags & SDEV_WAITING) { 520 sc_link->flags &= ~SDEV_WAITING; 521 wakeup((caddr_t)sc_link); 522 return; 523 } 524 525 /* 526 * See if there is a buf with work for us to do.. 527 */ 528 dp = &cd->buf_queue; 529 if ((bp = dp->b_actf) == NULL) /* yes, an assign */ 530 return; 531 dp->b_actf = bp->b_actf; 532 533 /* 534 * If the deivce has become invalid, abort all the 535 * reads and writes until all files have been closed and 536 * re-opened 537 */ 538 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 539 bp->b_error = EIO; 540 bp->b_flags |= B_ERROR; 541 bp->b_resid = bp->b_bcount; 542 biodone(bp); 543 continue; 544 } 545 546 /* 547 * We have a buf, now we should make a command 548 * 549 * First, translate the block to absolute and put it in terms 550 * of the logical blocksize of the device. 551 */ 552 blkno = 553 bp->b_blkno / (cd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 554 if (CDPART(bp->b_dev) != RAW_PART) { 555 p = &cd->sc_dk.dk_label->d_partitions[CDPART(bp->b_dev)]; 556 blkno += p->p_offset; 557 } 558 nblks = howmany(bp->b_bcount, cd->sc_dk.dk_label->d_secsize); 559 560 /* 561 * Fill out the scsi command. If the transfer will 562 * fit in a "small" cdb, use it. 563 */ 564 if (((blkno & 0x1fffff) == blkno) && 565 ((nblks & 0xff) == nblks)) { 566 /* 567 * We can fit in a small cdb. 568 */ 569 bzero(&cmd_small, sizeof(cmd_small)); 570 cmd_small.opcode = (bp->b_flags & B_READ) ? 571 READ_COMMAND : WRITE_COMMAND; 572 _lto3b(blkno, cmd_small.addr); 573 cmd_small.length = nblks & 0xff; 574 cmdlen = sizeof(cmd_small); 575 cmdp = (struct scsi_generic *)&cmd_small; 576 } else { 577 /* 578 * Need a large cdb. 579 */ 580 bzero(&cmd_big, sizeof(cmd_big)); 581 cmd_big.opcode = (bp->b_flags & B_READ) ? 582 READ_BIG : WRITE_BIG; 583 _lto4b(blkno, cmd_big.addr); 584 _lto2b(nblks, cmd_big.length); 585 cmdlen = sizeof(cmd_big); 586 cmdp = (struct scsi_generic *)&cmd_big; 587 } 588 589 /* Instrumentation. */ 590 disk_busy(&cd->sc_dk); 591 592 /* 593 * Call the routine that chats with the adapter. 594 * Note: we cannot sleep as we may be an interrupt 595 */ 596 if (scsi_scsi_cmd(sc_link, cmdp, cmdlen, 597 (u_char *) bp->b_data, bp->b_bcount, 598 CDRETRIES, 30000, bp, SCSI_NOSLEEP | 599 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT))) { 600 disk_unbusy(&cd->sc_dk, 0); 601 printf("%s: not queued", cd->sc_dev.dv_xname); 602 } 603 } 604 } 605 606 void 607 cddone(xs) 608 struct scsi_xfer *xs; 609 { 610 struct cd_softc *cd = xs->sc_link->device_softc; 611 612 if (xs->bp != NULL) 613 disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 614 } 615 616 void 617 cdminphys(bp) 618 struct buf *bp; 619 { 620 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 621 long max; 622 623 /* 624 * If the device is ancient, we want to make sure that 625 * the transfer fits into a 6-byte cdb. 626 * 627 * XXX Note that the SCSI-I spec says that 256-block transfers 628 * are allowed in a 6-byte read/write, and are specified 629 * by settng the "length" to 0. However, we're conservative 630 * here, allowing only 255-block transfers in case an 631 * ancient device gets confused by length == 0. A length of 0 632 * in a 10-byte read/write actually means 0 blocks. 633 */ 634 if (cd->flags & CDF_ANCIENT) { 635 max = cd->sc_dk.dk_label->d_secsize * 0xff; 636 637 if (bp->b_bcount > max) 638 bp->b_bcount = max; 639 } 640 641 (*cd->sc_link->adapter->scsi_minphys)(bp); 642 } 643 644 int 645 cdread(dev, uio, ioflag) 646 dev_t dev; 647 struct uio *uio; 648 int ioflag; 649 { 650 651 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 652 } 653 654 int 655 cdwrite(dev, uio, ioflag) 656 dev_t dev; 657 struct uio *uio; 658 int ioflag; 659 { 660 661 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 662 } 663 664 /* 665 * Perform special action on behalf of the user. 666 * Knows about the internals of this device 667 */ 668 int 669 cdioctl(dev, cmd, addr, flag, p) 670 dev_t dev; 671 u_long cmd; 672 caddr_t addr; 673 int flag; 674 struct proc *p; 675 { 676 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 677 int error; 678 679 SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdioctl 0x%lx ", cmd)); 680 681 /* 682 * If the device is not valid.. abandon ship 683 */ 684 if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 685 return EIO; 686 687 switch (cmd) { 688 case DIOCGDINFO: 689 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 690 return 0; 691 692 case DIOCGPART: 693 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 694 ((struct partinfo *)addr)->part = 695 &cd->sc_dk.dk_label->d_partitions[CDPART(dev)]; 696 return 0; 697 698 case DIOCWDINFO: 699 case DIOCSDINFO: 700 if ((flag & FWRITE) == 0) 701 return EBADF; 702 703 if ((error = cdlock(cd)) != 0) 704 return error; 705 cd->flags |= CDF_LABELLING; 706 707 error = setdisklabel(cd->sc_dk.dk_label, 708 (struct disklabel *)addr, /*cd->sc_dk.dk_openmask : */0, 709 cd->sc_dk.dk_cpulabel); 710 if (error == 0) { 711 } 712 713 cd->flags &= ~CDF_LABELLING; 714 cdunlock(cd); 715 return error; 716 717 case DIOCWLABEL: 718 return EBADF; 719 720 case CDIOCPLAYTRACKS: { 721 struct ioc_play_track *args = (struct ioc_play_track *)addr; 722 struct cd_mode_data data; 723 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 724 return error; 725 data.page.audio.flags &= ~CD_PA_SOTC; 726 data.page.audio.flags |= CD_PA_IMMED; 727 if ((error = cd_set_mode(cd, &data)) != 0) 728 return error; 729 return cd_play_tracks(cd, args->start_track, 730 args->start_index, args->end_track, 731 args->end_index); 732 } 733 case CDIOCPLAYMSF: { 734 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 735 struct cd_mode_data data; 736 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 737 return error; 738 data.page.audio.flags &= ~CD_PA_SOTC; 739 data.page.audio.flags |= CD_PA_IMMED; 740 if ((error = cd_set_mode(cd, &data)) != 0) 741 return error; 742 return cd_play_msf(cd, args->start_m, args->start_s, 743 args->start_f, args->end_m, args->end_s, 744 args->end_f); 745 } 746 case CDIOCPLAYBLOCKS: { 747 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 748 struct cd_mode_data data; 749 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 750 return error; 751 data.page.audio.flags &= ~CD_PA_SOTC; 752 data.page.audio.flags |= CD_PA_IMMED; 753 if ((error = cd_set_mode(cd, &data)) != 0) 754 return error; 755 return cd_play(cd, args->blk, args->len); 756 } 757 case CDIOCREADSUBCHANNEL: { 758 struct ioc_read_subchannel *args 759 = (struct ioc_read_subchannel *)addr; 760 struct cd_sub_channel_info data; 761 int len = args->data_len; 762 if (len > sizeof(data) || 763 len < sizeof(struct cd_sub_channel_header)) 764 return EINVAL; 765 error = cd_read_subchannel(cd, args->address_format, 766 args->data_format, args->track, 767 &data, len); 768 if (error) 769 return error; 770 len = min(len, _2btol(data.header.data_len) + 771 sizeof(struct cd_sub_channel_header)); 772 return copyout(&data, args->data, len); 773 } 774 case CDIOREADTOCHEADER: { 775 struct ioc_toc_header th; 776 if ((error = cd_read_toc(cd, 0, 0, 777 (struct cd_toc_entry *) &th, 778 sizeof(th))) != 0) 779 return error; 780 th.len = ntohs(th.len); 781 bcopy(&th, addr, sizeof(th)); 782 return 0; 783 } 784 case CDIOREADTOCENTRYS: { 785 struct cd_toc { 786 struct ioc_toc_header header; 787 struct cd_toc_entry entries[65]; 788 } data; 789 struct ioc_read_toc_entry *te = 790 (struct ioc_read_toc_entry *)addr; 791 struct ioc_toc_header *th; 792 int len = te->data_len; 793 th = &data.header; 794 795 if (len > sizeof(data.entries) || 796 len < sizeof(struct cd_toc_entry)) 797 return EINVAL; 798 error = cd_read_toc(cd, te->address_format, 799 te->starting_track, 800 (struct cd_toc_entry *)&data, 801 len + sizeof(struct ioc_toc_header)); 802 if (error) 803 return error; 804 len = min(len, ntohs(th->len) - (sizeof(th->starting_track) + 805 sizeof(th->ending_track))); 806 return copyout(data.entries, te->data, len); 807 } 808 case CDIOCSETPATCH: { 809 struct ioc_patch *arg = (struct ioc_patch *)addr; 810 struct cd_mode_data data; 811 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 812 return error; 813 data.page.audio.port[LEFT_PORT].channels = arg->patch[0]; 814 data.page.audio.port[RIGHT_PORT].channels = arg->patch[1]; 815 data.page.audio.port[2].channels = arg->patch[2]; 816 data.page.audio.port[3].channels = arg->patch[3]; 817 return cd_set_mode(cd, &data); 818 } 819 case CDIOCGETVOL: { 820 struct ioc_vol *arg = (struct ioc_vol *)addr; 821 struct cd_mode_data data; 822 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 823 return error; 824 arg->vol[LEFT_PORT] = data.page.audio.port[LEFT_PORT].volume; 825 arg->vol[RIGHT_PORT] = data.page.audio.port[RIGHT_PORT].volume; 826 arg->vol[2] = data.page.audio.port[2].volume; 827 arg->vol[3] = data.page.audio.port[3].volume; 828 return 0; 829 } 830 case CDIOCSETVOL: { 831 struct ioc_vol *arg = (struct ioc_vol *)addr; 832 struct cd_mode_data data; 833 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 834 return error; 835 data.page.audio.port[LEFT_PORT].channels = CHANNEL_0; 836 data.page.audio.port[LEFT_PORT].volume = arg->vol[LEFT_PORT]; 837 data.page.audio.port[RIGHT_PORT].channels = CHANNEL_1; 838 data.page.audio.port[RIGHT_PORT].volume = arg->vol[RIGHT_PORT]; 839 data.page.audio.port[2].volume = arg->vol[2]; 840 data.page.audio.port[3].volume = arg->vol[3]; 841 return cd_set_mode(cd, &data); 842 } 843 case CDIOCSETMONO: { 844 struct cd_mode_data data; 845 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 846 return error; 847 data.page.audio.port[LEFT_PORT].channels = 848 LEFT_CHANNEL | RIGHT_CHANNEL | 4 | 8; 849 data.page.audio.port[RIGHT_PORT].channels = 850 LEFT_CHANNEL | RIGHT_CHANNEL; 851 data.page.audio.port[2].channels = 0; 852 data.page.audio.port[3].channels = 0; 853 return cd_set_mode(cd, &data); 854 } 855 case CDIOCSETSTEREO: { 856 struct cd_mode_data data; 857 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 858 return error; 859 data.page.audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 860 data.page.audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 861 data.page.audio.port[2].channels = 0; 862 data.page.audio.port[3].channels = 0; 863 return cd_set_mode(cd, &data); 864 } 865 case CDIOCSETMUTE: { 866 struct cd_mode_data data; 867 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 868 return error; 869 data.page.audio.port[LEFT_PORT].channels = 0; 870 data.page.audio.port[RIGHT_PORT].channels = 0; 871 data.page.audio.port[2].channels = 0; 872 data.page.audio.port[3].channels = 0; 873 return cd_set_mode(cd, &data); 874 } 875 case CDIOCSETLEFT: { 876 struct cd_mode_data data; 877 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 878 return error; 879 data.page.audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 880 data.page.audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 881 data.page.audio.port[2].channels = 0; 882 data.page.audio.port[3].channels = 0; 883 return cd_set_mode(cd, &data); 884 } 885 case CDIOCSETRIGHT: { 886 struct cd_mode_data data; 887 if ((error = cd_get_mode(cd, &data, AUDIO_PAGE)) != 0) 888 return error; 889 data.page.audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 890 data.page.audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 891 data.page.audio.port[2].channels = 0; 892 data.page.audio.port[3].channels = 0; 893 return cd_set_mode(cd, &data); 894 } 895 case CDIOCRESUME: 896 return cd_pause(cd, 1); 897 case CDIOCPAUSE: 898 return cd_pause(cd, 0); 899 case CDIOCSTART: 900 return scsi_start(cd->sc_link, SSS_START, 0); 901 case CDIOCSTOP: 902 return scsi_start(cd->sc_link, SSS_STOP, 0); 903 case CDIOCEJECT: /* FALLTHROUGH */ 904 case DIOCEJECT: 905 return scsi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0); 906 case CDIOCALLOW: 907 return scsi_prevent(cd->sc_link, PR_ALLOW, 0); 908 case CDIOCPREVENT: 909 return scsi_prevent(cd->sc_link, PR_PREVENT, 0); 910 case DIOCLOCK: 911 return scsi_prevent(cd->sc_link, 912 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0); 913 case CDIOCSETDEBUG: 914 cd->sc_link->flags |= (SDEV_DB1 | SDEV_DB2); 915 return 0; 916 case CDIOCCLRDEBUG: 917 cd->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); 918 return 0; 919 case CDIOCRESET: 920 return cd_reset(cd); 921 922 default: 923 if (CDPART(dev) != RAW_PART) 924 return ENOTTY; 925 return scsi_do_ioctl(cd->sc_link, dev, cmd, addr, flag, p); 926 } 927 928 #ifdef DIAGNOSTIC 929 panic("cdioctl: impossible"); 930 #endif 931 } 932 933 /* 934 * Load the label information on the named device 935 * Actually fabricate a disklabel 936 * 937 * EVENTUALLY take information about different 938 * data tracks from the TOC and put it in the disklabel 939 */ 940 void 941 cdgetdisklabel(cd) 942 struct cd_softc *cd; 943 { 944 struct disklabel *lp = cd->sc_dk.dk_label; 945 946 bzero(lp, sizeof(struct disklabel)); 947 bzero(cd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel)); 948 949 lp->d_secsize = cd->params.blksize; 950 lp->d_ntracks = 1; 951 lp->d_nsectors = 100; 952 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 953 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 954 955 strncpy(lp->d_typename, "SCSI CD-ROM", 16); 956 lp->d_type = DTYPE_SCSI; 957 strncpy(lp->d_packname, "fictitious", 16); 958 lp->d_secperunit = cd->params.disksize; 959 lp->d_rpm = 300; 960 lp->d_interleave = 1; 961 lp->d_flags = D_REMOVABLE; 962 963 lp->d_partitions[0].p_offset = 0; 964 lp->d_partitions[0].p_size = 965 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 966 lp->d_partitions[0].p_fstype = FS_ISO9660; 967 lp->d_partitions[RAW_PART].p_offset = 0; 968 lp->d_partitions[RAW_PART].p_size = 969 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 970 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 971 lp->d_npartitions = RAW_PART + 1; 972 973 lp->d_magic = DISKMAGIC; 974 lp->d_magic2 = DISKMAGIC; 975 lp->d_checksum = dkcksum(lp); 976 } 977 978 /* 979 * Find out from the device what it's capacity is 980 */ 981 u_long 982 cd_size(cd, flags) 983 struct cd_softc *cd; 984 int flags; 985 { 986 struct scsi_read_cd_cap_data rdcap; 987 struct scsi_read_cd_capacity scsi_cmd; 988 int blksize; 989 u_long size; 990 991 /* 992 * make up a scsi command and ask the scsi driver to do 993 * it for you. 994 */ 995 bzero(&scsi_cmd, sizeof(scsi_cmd)); 996 scsi_cmd.opcode = READ_CD_CAPACITY; 997 998 /* 999 * If the command works, interpret the result as a 4 byte 1000 * number of blocks and a blocksize 1001 */ 1002 if (scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1003 sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 1004 20000, NULL, flags | SCSI_DATA_IN) != 0) 1005 return 0; 1006 1007 blksize = _4btol(rdcap.length); 1008 if (blksize < 512) 1009 blksize = 2048; /* some drives lie ! */ 1010 cd->params.blksize = blksize; 1011 1012 size = _4btol(rdcap.addr) + 1; 1013 if (size < 100) 1014 size = 400000; /* ditto */ 1015 cd->params.disksize = size; 1016 1017 return size; 1018 } 1019 1020 /* 1021 * Get the requested page into the buffer given 1022 */ 1023 int 1024 cd_get_mode(cd, data, page) 1025 struct cd_softc *cd; 1026 struct cd_mode_data *data; 1027 int page; 1028 { 1029 struct scsi_mode_sense scsi_cmd; 1030 1031 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1032 bzero(data, sizeof(*data)); 1033 scsi_cmd.opcode = MODE_SENSE; 1034 scsi_cmd.page = page; 1035 scsi_cmd.length = sizeof(*data) & 0xff; 1036 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1037 sizeof(scsi_cmd), (u_char *)data, sizeof(*data), CDRETRIES, 20000, 1038 NULL, SCSI_DATA_IN); 1039 } 1040 1041 /* 1042 * Get the requested page into the buffer given 1043 */ 1044 int 1045 cd_set_mode(cd, data) 1046 struct cd_softc *cd; 1047 struct cd_mode_data *data; 1048 { 1049 struct scsi_mode_select scsi_cmd; 1050 1051 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1052 scsi_cmd.opcode = MODE_SELECT; 1053 scsi_cmd.byte2 |= SMS_PF; 1054 scsi_cmd.length = sizeof(*data) & 0xff; 1055 data->header.data_length = 0; 1056 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1057 sizeof(scsi_cmd), (u_char *)data, sizeof(*data), CDRETRIES, 20000, 1058 NULL, SCSI_DATA_OUT); 1059 } 1060 1061 /* 1062 * Get scsi driver to send a "start playing" command 1063 */ 1064 int 1065 cd_play(cd, blkno, nblks) 1066 struct cd_softc *cd; 1067 int blkno, nblks; 1068 { 1069 struct scsi_play scsi_cmd; 1070 1071 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1072 scsi_cmd.opcode = PLAY; 1073 _lto4b(blkno, scsi_cmd.blk_addr); 1074 _lto2b(nblks, scsi_cmd.xfer_len); 1075 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1076 sizeof(scsi_cmd), 0, 0, CDRETRIES, 200000, NULL, 0); 1077 } 1078 1079 /* 1080 * Get scsi driver to send a "start playing" command 1081 */ 1082 int 1083 cd_play_big(cd, blkno, nblks) 1084 struct cd_softc *cd; 1085 int blkno, nblks; 1086 { 1087 struct scsi_play_big scsi_cmd; 1088 1089 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1090 scsi_cmd.opcode = PLAY_BIG; 1091 _lto4b(blkno, scsi_cmd.blk_addr); 1092 _lto4b(nblks, scsi_cmd.xfer_len); 1093 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1094 sizeof(scsi_cmd), 0, 0, CDRETRIES, 20000, NULL, 0); 1095 } 1096 1097 /* 1098 * Get scsi driver to send a "start playing" command 1099 */ 1100 int 1101 cd_play_tracks(cd, strack, sindex, etrack, eindex) 1102 struct cd_softc *cd; 1103 int strack, sindex, etrack, eindex; 1104 { 1105 struct scsi_play_track scsi_cmd; 1106 1107 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1108 scsi_cmd.opcode = PLAY_TRACK; 1109 scsi_cmd.start_track = strack; 1110 scsi_cmd.start_index = sindex; 1111 scsi_cmd.end_track = etrack; 1112 scsi_cmd.end_index = eindex; 1113 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1114 sizeof(scsi_cmd), 0, 0, CDRETRIES, 20000, NULL, 0); 1115 } 1116 1117 /* 1118 * Get scsi driver to send a "play msf" command 1119 */ 1120 int 1121 cd_play_msf(cd, startm, starts, startf, endm, ends, endf) 1122 struct cd_softc *cd; 1123 int startm, starts, startf, endm, ends, endf; 1124 { 1125 struct scsi_play_msf scsi_cmd; 1126 1127 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1128 scsi_cmd.opcode = PLAY_MSF; 1129 scsi_cmd.start_m = startm; 1130 scsi_cmd.start_s = starts; 1131 scsi_cmd.start_f = startf; 1132 scsi_cmd.end_m = endm; 1133 scsi_cmd.end_s = ends; 1134 scsi_cmd.end_f = endf; 1135 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1136 sizeof(scsi_cmd), 0, 0, CDRETRIES, 2000, NULL, 0); 1137 } 1138 1139 /* 1140 * Get scsi driver to send a "start up" command 1141 */ 1142 int 1143 cd_pause(cd, go) 1144 struct cd_softc *cd; 1145 int go; 1146 { 1147 struct scsi_pause scsi_cmd; 1148 1149 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1150 scsi_cmd.opcode = PAUSE; 1151 scsi_cmd.resume = go; 1152 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1153 sizeof(scsi_cmd), 0, 0, CDRETRIES, 2000, NULL, 0); 1154 } 1155 1156 /* 1157 * Get scsi driver to send a "RESET" command 1158 */ 1159 int 1160 cd_reset(cd) 1161 struct cd_softc *cd; 1162 { 1163 1164 return scsi_scsi_cmd(cd->sc_link, 0, 0, 0, 0, CDRETRIES, 2000, NULL, 1165 SCSI_RESET); 1166 } 1167 1168 /* 1169 * Read subchannel 1170 */ 1171 int 1172 cd_read_subchannel(cd, mode, format, track, data, len) 1173 struct cd_softc *cd; 1174 int mode, format, track, len; 1175 struct cd_sub_channel_info *data; 1176 { 1177 struct scsi_read_subchannel scsi_cmd; 1178 1179 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1180 scsi_cmd.opcode = READ_SUBCHANNEL; 1181 if (mode == CD_MSF_FORMAT) 1182 scsi_cmd.byte2 |= CD_MSF; 1183 scsi_cmd.byte3 = SRS_SUBQ; 1184 scsi_cmd.subchan_format = format; 1185 scsi_cmd.track = track; 1186 _lto2b(len, scsi_cmd.data_len); 1187 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1188 sizeof(struct scsi_read_subchannel), (u_char *)data, len, 1189 CDRETRIES, 5000, NULL, SCSI_DATA_IN|SCSI_SILENT); 1190 } 1191 1192 /* 1193 * Read table of contents 1194 */ 1195 int 1196 cd_read_toc(cd, mode, start, data, len) 1197 struct cd_softc *cd; 1198 int mode, start, len; 1199 struct cd_toc_entry *data; 1200 { 1201 struct scsi_read_toc scsi_cmd; 1202 int ntoc; 1203 1204 bzero(&scsi_cmd, sizeof(scsi_cmd)); 1205 /*if (len!=sizeof(struct ioc_toc_header)) 1206 * ntoc=((len)-sizeof(struct ioc_toc_header))/sizeof(struct cd_toc_entry); 1207 * else */ 1208 ntoc = len; 1209 scsi_cmd.opcode = READ_TOC; 1210 if (mode == CD_MSF_FORMAT) 1211 scsi_cmd.byte2 |= CD_MSF; 1212 scsi_cmd.from_track = start; 1213 _lto2b(ntoc, scsi_cmd.data_len); 1214 return scsi_scsi_cmd(cd->sc_link, (struct scsi_generic *)&scsi_cmd, 1215 sizeof(struct scsi_read_toc), (u_char *)data, len, CDRETRIES, 1216 5000, NULL, SCSI_DATA_IN); 1217 } 1218 1219 /* 1220 * Get the scsi driver to send a full inquiry to the device and use the 1221 * results to fill out the disk parameter structure. 1222 */ 1223 int 1224 cd_get_parms(cd, flags) 1225 struct cd_softc *cd; 1226 int flags; 1227 { 1228 1229 /* 1230 * give a number of sectors so that sec * trks * cyls 1231 * is <= disk_size 1232 */ 1233 if (cd_size(cd, flags) == 0) 1234 return ENXIO; 1235 1236 return 0; 1237 } 1238 1239 int 1240 cdsize(dev) 1241 dev_t dev; 1242 { 1243 1244 /* CD-ROMs are read-only. */ 1245 return -1; 1246 } 1247 1248 int 1249 cddump(dev, blkno, va, size) 1250 dev_t dev; 1251 daddr_t blkno; 1252 caddr_t va; 1253 size_t size; 1254 { 1255 1256 /* Not implemented. */ 1257 return ENXIO; 1258 } 1259