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