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