1 /* $NetBSD: cd.c,v 1.153 2001/07/18 18:25:41 thorpej 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_SILENT_NODEV); 370 SC_DEBUG(periph, SCSIPI_DB1, 371 ("cdopen: scsipi_test_unit_ready, error=%d\n", error)); 372 if (error) { 373 if (part != RAW_PART || fmt != S_IFCHR) 374 goto bad3; 375 else 376 goto out; 377 } 378 379 /* 380 * Start the pack spinning if necessary. Always allow the 381 * raw parition to be opened, for raw IOCTLs. Data transfers 382 * will check for SDEV_MEDIA_LOADED. 383 */ 384 error = scsipi_start(periph, SSS_START, 385 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 386 XS_CTL_SILENT); 387 SC_DEBUG(periph, SCSIPI_DB1, 388 ("cdopen: scsipi_start, error=%d\n", error)); 389 if (error) { 390 if (part != RAW_PART || fmt != S_IFCHR) 391 goto bad3; 392 else 393 goto out; 394 } 395 396 periph->periph_flags |= PERIPH_OPEN; 397 398 /* Lock the pack in. */ 399 error = scsipi_prevent(periph, PR_PREVENT, 400 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 401 SC_DEBUG(periph, SCSIPI_DB1, 402 ("cdopen: scsipi_prevent, error=%d\n", error)); 403 if (error) 404 goto bad; 405 406 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 407 periph->periph_flags |= PERIPH_MEDIA_LOADED; 408 409 /* Load the physical device parameters. */ 410 if (cd_get_parms(cd, 0) != 0) { 411 error = ENXIO; 412 goto bad2; 413 } 414 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 415 416 /* Fabricate a disk label. */ 417 cdgetdisklabel(cd); 418 SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel fabricated ")); 419 } 420 } 421 422 /* Check that the partition exists. */ 423 if (part != RAW_PART && 424 (part >= cd->sc_dk.dk_label->d_npartitions || 425 cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 426 error = ENXIO; 427 goto bad; 428 } 429 430 out: /* Insure only one open at a time. */ 431 switch (fmt) { 432 case S_IFCHR: 433 cd->sc_dk.dk_copenmask |= (1 << part); 434 break; 435 case S_IFBLK: 436 cd->sc_dk.dk_bopenmask |= (1 << part); 437 break; 438 } 439 cd->sc_dk.dk_openmask = 440 cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 441 442 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 443 cdunlock(cd); 444 return (0); 445 446 bad2: 447 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 448 449 bad: 450 if (cd->sc_dk.dk_openmask == 0) { 451 scsipi_prevent(periph, PR_ALLOW, 452 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 453 periph->periph_flags &= ~PERIPH_OPEN; 454 } 455 456 bad3: 457 cdunlock(cd); 458 bad4: 459 if (cd->sc_dk.dk_openmask == 0) 460 scsipi_adapter_delref(adapt); 461 return (error); 462 } 463 464 /* 465 * close the device.. only called if we are the LAST 466 * occurence of an open device 467 */ 468 int 469 cdclose(dev, flag, fmt, p) 470 dev_t dev; 471 int flag, fmt; 472 struct proc *p; 473 { 474 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 475 struct scsipi_periph *periph = cd->sc_periph; 476 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 477 int part = CDPART(dev); 478 int error; 479 480 if ((error = cdlock(cd)) != 0) 481 return (error); 482 483 switch (fmt) { 484 case S_IFCHR: 485 cd->sc_dk.dk_copenmask &= ~(1 << part); 486 break; 487 case S_IFBLK: 488 cd->sc_dk.dk_bopenmask &= ~(1 << part); 489 break; 490 } 491 cd->sc_dk.dk_openmask = 492 cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 493 494 if (cd->sc_dk.dk_openmask == 0) { 495 scsipi_wait_drain(periph); 496 497 scsipi_prevent(periph, PR_ALLOW, 498 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 499 XS_CTL_IGNORE_NOT_READY); 500 periph->periph_flags &= ~PERIPH_OPEN; 501 502 scsipi_wait_drain(periph); 503 504 scsipi_adapter_delref(adapt); 505 } 506 507 cdunlock(cd); 508 return (0); 509 } 510 511 /* 512 * Actually translate the requested transfer into one the physical driver can 513 * understand. The transfer is described by a buf and will include only one 514 * physical transfer. 515 */ 516 void 517 cdstrategy(bp) 518 struct buf *bp; 519 { 520 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 521 struct disklabel *lp; 522 struct scsipi_periph *periph = cd->sc_periph; 523 daddr_t blkno; 524 int s; 525 526 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdstrategy ")); 527 SC_DEBUG(cd->sc_periph, SCSIPI_DB1, 528 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 529 /* 530 * If the device has been made invalid, error out 531 * maybe the media changed 532 */ 533 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 534 if (periph->periph_flags & PERIPH_OPEN) 535 bp->b_error = EIO; 536 else 537 bp->b_error = ENODEV; 538 goto bad; 539 } 540 541 lp = cd->sc_dk.dk_label; 542 543 /* 544 * The transfer must be a whole number of blocks, offset must not 545 * be negative. 546 */ 547 if ((bp->b_bcount % lp->d_secsize) != 0 || 548 bp->b_blkno < 0 ) { 549 bp->b_error = EINVAL; 550 goto bad; 551 } 552 /* 553 * If it's a null transfer, return immediately 554 */ 555 if (bp->b_bcount == 0) 556 goto done; 557 558 /* 559 * Do bounds checking, adjust transfer. if error, process. 560 * If end of partition, just return. 561 */ 562 if (CDPART(bp->b_dev) != RAW_PART && 563 bounds_check_with_label(bp, lp, 564 (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0) 565 goto done; 566 567 /* 568 * Now convert the block number to absolute and put it in 569 * terms of the device's logical block size. 570 */ 571 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 572 if (CDPART(bp->b_dev) != RAW_PART) 573 blkno += lp->d_partitions[CDPART(bp->b_dev)].p_offset; 574 575 bp->b_rawblkno = blkno; 576 577 s = splbio(); 578 579 /* 580 * Place it in the queue of disk activities for this disk. 581 * 582 * XXX Only do disksort() if the current operating mode does not 583 * XXX include tagged queueing. 584 */ 585 disksort_blkno(&cd->buf_queue, bp); 586 587 /* 588 * Tell the device to get going on the transfer if it's 589 * not doing anything, otherwise just wait for completion 590 */ 591 cdstart(cd->sc_periph); 592 593 splx(s); 594 return; 595 596 bad: 597 bp->b_flags |= B_ERROR; 598 done: 599 /* 600 * Correctly set the buf to indicate a completed xfer 601 */ 602 bp->b_resid = bp->b_bcount; 603 biodone(bp); 604 } 605 606 /* 607 * cdstart looks to see if there is a buf waiting for the device 608 * and that the device is not already busy. If both are true, 609 * It deques the buf and creates a scsi command to perform the 610 * transfer in the buf. The transfer request will call scsipi_done 611 * on completion, which will in turn call this routine again 612 * so that the next queued transfer is performed. 613 * The bufs are queued by the strategy routine (cdstrategy) 614 * 615 * This routine is also called after other non-queued requests 616 * have been made of the scsi driver, to ensure that the queue 617 * continues to be drained. 618 * 619 * must be called at the correct (highish) spl level 620 * cdstart() is called at splbio from cdstrategy and scsipi_done 621 */ 622 void 623 cdstart(periph) 624 struct scsipi_periph *periph; 625 { 626 struct cd_softc *cd = (void *)periph->periph_dev; 627 struct disklabel *lp = cd->sc_dk.dk_label; 628 struct buf *bp = 0; 629 struct scsipi_rw_big cmd_big; 630 #if NCD_SCSIBUS > 0 631 struct scsi_rw cmd_small; 632 #endif 633 struct scsipi_generic *cmdp; 634 int flags, nblks, cmdlen, error; 635 636 SC_DEBUG(periph, SCSIPI_DB2, ("cdstart ")); 637 /* 638 * Check if the device has room for another command 639 */ 640 while (periph->periph_active < periph->periph_openings) { 641 /* 642 * there is excess capacity, but a special waits 643 * It'll need the adapter as soon as we clear out of the 644 * way and let it run (user level wait). 645 */ 646 if (periph->periph_flags & PERIPH_WAITING) { 647 periph->periph_flags &= ~PERIPH_WAITING; 648 wakeup((caddr_t)periph); 649 return; 650 } 651 652 /* 653 * See if there is a buf with work for us to do.. 654 */ 655 if ((bp = BUFQ_FIRST(&cd->buf_queue)) == NULL) 656 return; 657 BUFQ_REMOVE(&cd->buf_queue, bp); 658 659 /* 660 * If the device has become invalid, abort all the 661 * reads and writes until all files have been closed and 662 * re-opened 663 */ 664 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 665 bp->b_error = EIO; 666 bp->b_flags |= B_ERROR; 667 bp->b_resid = bp->b_bcount; 668 biodone(bp); 669 continue; 670 } 671 672 /* 673 * We have a buf, now we should make a command. 674 */ 675 676 nblks = howmany(bp->b_bcount, lp->d_secsize); 677 678 #if NCD_SCSIBUS > 0 679 /* 680 * Fill out the scsi command. If the transfer will 681 * fit in a "small" cdb, use it. 682 */ 683 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 684 ((nblks & 0xff) == nblks) && 685 !(periph->periph_quirks & PQUIRK_ONLYBIG) && 686 scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_SCSI) { 687 /* 688 * We can fit in a small cdb. 689 */ 690 memset(&cmd_small, 0, sizeof(cmd_small)); 691 cmd_small.opcode = (bp->b_flags & B_READ) ? 692 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND; 693 _lto3b(bp->b_rawblkno, cmd_small.addr); 694 cmd_small.length = nblks & 0xff; 695 cmdlen = sizeof(cmd_small); 696 cmdp = (struct scsipi_generic *)&cmd_small; 697 } else 698 #endif 699 { 700 /* 701 * Need a large cdb. 702 */ 703 memset(&cmd_big, 0, sizeof(cmd_big)); 704 cmd_big.opcode = (bp->b_flags & B_READ) ? 705 READ_BIG : WRITE_BIG; 706 _lto4b(bp->b_rawblkno, cmd_big.addr); 707 _lto2b(nblks, cmd_big.length); 708 cmdlen = sizeof(cmd_big); 709 cmdp = (struct scsipi_generic *)&cmd_big; 710 } 711 712 /* Instrumentation. */ 713 disk_busy(&cd->sc_dk); 714 715 /* 716 * Figure out what flags to use. 717 */ 718 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC; 719 if (bp->b_flags & B_READ) 720 flags |= XS_CTL_DATA_IN; 721 else 722 flags |= XS_CTL_DATA_OUT; 723 if (bp->b_flags & B_ORDERED) 724 flags |= XS_CTL_ORDERED_TAG; 725 else 726 flags |= XS_CTL_SIMPLE_TAG; 727 728 /* 729 * Call the routine that chats with the adapter. 730 * Note: we cannot sleep as we may be an interrupt 731 */ 732 error = scsipi_command(periph, cmdp, cmdlen, 733 (u_char *)bp->b_data, bp->b_bcount, 734 CDRETRIES, 30000, bp, flags); 735 if (error) { 736 disk_unbusy(&cd->sc_dk, 0); 737 printf("%s: not queued, error %d\n", 738 cd->sc_dev.dv_xname, error); 739 } 740 } 741 } 742 743 void 744 cddone(xs) 745 struct scsipi_xfer *xs; 746 { 747 struct cd_softc *cd = (void *)xs->xs_periph->periph_dev; 748 749 if (xs->bp != NULL) { 750 disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 751 #if NRND > 0 752 rnd_add_uint32(&cd->rnd_source, xs->bp->b_rawblkno); 753 #endif 754 } 755 } 756 757 int cd_interpret_sense(xs) 758 struct scsipi_xfer *xs; 759 { 760 struct scsipi_periph *periph = xs->xs_periph; 761 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 762 int retval = EJUSTRETURN; 763 764 /* 765 * If it isn't a extended or extended/deferred error, let 766 * the generic code handle it. 767 */ 768 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 769 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */ 770 return (retval); 771 } 772 773 /* 774 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit 775 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then 776 * wait a bit for the drive to spin up 777 */ 778 779 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 780 sense->add_sense_code == 0x4 && 781 sense->add_sense_code_qual == 0x01) { 782 /* 783 * Sleep for 5 seconds to wait for the drive to spin up 784 */ 785 786 SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD " 787 "spinup\n")); 788 scsipi_periph_freeze(periph, 1); 789 callout_reset(&periph->periph_callout, 790 5 * hz, scsipi_periph_timed_thaw, periph); 791 retval = ERESTART; 792 } 793 return (retval); 794 } 795 796 void 797 cdminphys(bp) 798 struct buf *bp; 799 { 800 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 801 long max; 802 803 /* 804 * If the device is ancient, we want to make sure that 805 * the transfer fits into a 6-byte cdb. 806 * 807 * XXX Note that the SCSI-I spec says that 256-block transfers 808 * are allowed in a 6-byte read/write, and are specified 809 * by settng the "length" to 0. However, we're conservative 810 * here, allowing only 255-block transfers in case an 811 * ancient device gets confused by length == 0. A length of 0 812 * in a 10-byte read/write actually means 0 blocks. 813 */ 814 if (cd->flags & CDF_ANCIENT) { 815 max = cd->sc_dk.dk_label->d_secsize * 0xff; 816 817 if (bp->b_bcount > max) 818 bp->b_bcount = max; 819 } 820 821 (*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp); 822 } 823 824 int 825 cdread(dev, uio, ioflag) 826 dev_t dev; 827 struct uio *uio; 828 int ioflag; 829 { 830 831 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 832 } 833 834 int 835 cdwrite(dev, uio, ioflag) 836 dev_t dev; 837 struct uio *uio; 838 int ioflag; 839 { 840 841 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 842 } 843 844 /* 845 * conversion between minute-seconde-frame and logical block adress 846 * adresses format 847 */ 848 void 849 lba2msf (lba, m, s, f) 850 u_long lba; 851 u_char *m, *s, *f; 852 { 853 u_long tmp; 854 855 tmp = lba + CD_BLOCK_OFFSET; /* offset of first logical frame */ 856 tmp &= 0xffffff; /* negative lbas use only 24 bits */ 857 *m = tmp / (CD_SECS * CD_FRAMES); 858 tmp %= (CD_SECS * CD_FRAMES); 859 *s = tmp / CD_FRAMES; 860 *f = tmp % CD_FRAMES; 861 } 862 863 u_long 864 msf2lba (m, s, f) 865 u_char m, s, f; 866 { 867 868 return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET); 869 } 870 871 872 /* 873 * Perform special action on behalf of the user. 874 * Knows about the internals of this device 875 */ 876 int 877 cdioctl(dev, cmd, addr, flag, p) 878 dev_t dev; 879 u_long cmd; 880 caddr_t addr; 881 int flag; 882 struct proc *p; 883 { 884 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 885 struct scsipi_periph *periph = cd->sc_periph; 886 int part = CDPART(dev); 887 int error; 888 #ifdef __HAVE_OLD_DISKLABEL 889 struct disklabel newlabel; 890 #endif 891 892 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd)); 893 894 /* 895 * If the device is not valid, some IOCTLs can still be 896 * handled on the raw partition. Check this here. 897 */ 898 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 899 switch (cmd) { 900 case DIOCWLABEL: 901 case DIOCLOCK: 902 case ODIOCEJECT: 903 case DIOCEJECT: 904 case SCIOCIDENTIFY: 905 case OSCIOCIDENTIFY: 906 case SCIOCCOMMAND: 907 case SCIOCDEBUG: 908 case CDIOCGETVOL: 909 case CDIOCSETVOL: 910 case CDIOCSETMONO: 911 case CDIOCSETSTEREO: 912 case CDIOCSETMUTE: 913 case CDIOCSETLEFT: 914 case CDIOCSETRIGHT: 915 case CDIOCCLOSE: 916 case CDIOCEJECT: 917 case CDIOCALLOW: 918 case CDIOCPREVENT: 919 case CDIOCSETDEBUG: 920 case CDIOCCLRDEBUG: 921 case CDIOCRESET: 922 case SCIOCRESET: 923 case CDIOCLOADUNLOAD: 924 case DVD_AUTH: 925 case DVD_READ_STRUCT: 926 if (part == RAW_PART) 927 break; 928 /* FALLTHROUGH */ 929 default: 930 if ((periph->periph_flags & PERIPH_OPEN) == 0) 931 return (ENODEV); 932 else 933 return (EIO); 934 } 935 } 936 937 switch (cmd) { 938 case DIOCGDINFO: 939 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 940 return (0); 941 #ifdef __HAVE_OLD_DISKLABEL 942 case ODIOCGDINFO: 943 newlabel = *(cd->sc_dk.dk_label); 944 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 945 return ENOTTY; 946 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 947 return (0); 948 #endif 949 950 case DIOCGPART: 951 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 952 ((struct partinfo *)addr)->part = 953 &cd->sc_dk.dk_label->d_partitions[part]; 954 return (0); 955 956 case DIOCWDINFO: 957 case DIOCSDINFO: 958 #ifdef __HAVE_OLD_DISKLABEL 959 case ODIOCWDINFO: 960 case ODIOCSDINFO: 961 #endif 962 { 963 struct disklabel *lp; 964 965 #ifdef __HAVE_OLD_DISKLABEL 966 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 967 memset(&newlabel, 0, sizeof newlabel); 968 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 969 lp = &newlabel; 970 } else 971 #endif 972 lp = (struct disklabel *)addr; 973 974 if ((flag & FWRITE) == 0) 975 return (EBADF); 976 977 if ((error = cdlock(cd)) != 0) 978 return (error); 979 cd->flags |= CDF_LABELLING; 980 981 error = setdisklabel(cd->sc_dk.dk_label, 982 lp, /*cd->sc_dk.dk_openmask : */0, 983 cd->sc_dk.dk_cpulabel); 984 if (error == 0) { 985 /* XXX ? */ 986 } 987 988 cd->flags &= ~CDF_LABELLING; 989 cdunlock(cd); 990 return (error); 991 } 992 993 case DIOCWLABEL: 994 return (EBADF); 995 996 case DIOCGDEFLABEL: 997 cdgetdefaultlabel(cd, (struct disklabel *)addr); 998 return (0); 999 1000 #ifdef __HAVE_OLD_DISKLABEL 1001 case ODIOCGDEFLABEL: 1002 cdgetdefaultlabel(cd, &newlabel); 1003 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 1004 return ENOTTY; 1005 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 1006 return (0); 1007 #endif 1008 1009 case CDIOCPLAYTRACKS: { 1010 struct ioc_play_track *args = (struct ioc_play_track *)addr; 1011 1012 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1013 return (error); 1014 return (cd_play_tracks(cd, args->start_track, 1015 args->start_index, args->end_track, args->end_index)); 1016 } 1017 case CDIOCPLAYMSF: { 1018 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 1019 1020 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1021 return (error); 1022 return (cd_play_msf(cd, args->start_m, args->start_s, 1023 args->start_f, args->end_m, args->end_s, args->end_f)); 1024 } 1025 case CDIOCPLAYBLOCKS: { 1026 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 1027 1028 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1029 return (error); 1030 return (cd_play(cd, args->blk, args->len)); 1031 } 1032 case CDIOCREADSUBCHANNEL: { 1033 struct ioc_read_subchannel *args = 1034 (struct ioc_read_subchannel *)addr; 1035 struct cd_sub_channel_info data; 1036 int len = args->data_len; 1037 1038 if (len > sizeof(data) || 1039 len < sizeof(struct cd_sub_channel_header)) 1040 return (EINVAL); 1041 error = cd_read_subchannel(cd, args->address_format, 1042 args->data_format, args->track, &data, len, 1043 XS_CTL_DATA_ONSTACK); 1044 if (error) 1045 return (error); 1046 len = min(len, _2btol(data.header.data_len) + 1047 sizeof(struct cd_sub_channel_header)); 1048 return (copyout(&data, args->data, len)); 1049 } 1050 case CDIOREADTOCHEADER: { 1051 struct ioc_toc_header th; 1052 1053 if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 1054 XS_CTL_DATA_ONSTACK, 0)) != 0) 1055 return (error); 1056 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) { 1057 #if BYTE_ORDER == BIG_ENDIAN 1058 bswap((u_int8_t *)&th.len, sizeof(th.len)); 1059 #endif 1060 } else 1061 th.len = ntohs(th.len); 1062 memcpy(addr, &th, sizeof(th)); 1063 return (0); 1064 } 1065 case CDIOREADTOCENTRYS: { 1066 struct cd_toc toc; 1067 struct ioc_read_toc_entry *te = 1068 (struct ioc_read_toc_entry *)addr; 1069 struct ioc_toc_header *th; 1070 struct cd_toc_entry *cte; 1071 int len = te->data_len; 1072 int ntracks; 1073 1074 th = &toc.header; 1075 1076 if (len > sizeof(toc.entries) || 1077 len < sizeof(struct cd_toc_entry)) 1078 return (EINVAL); 1079 error = cd_read_toc(cd, te->address_format, te->starting_track, 1080 &toc, len + sizeof(struct ioc_toc_header), 1081 XS_CTL_DATA_ONSTACK, 0); 1082 if (error) 1083 return (error); 1084 if (te->address_format == CD_LBA_FORMAT) 1085 for (ntracks = 1086 th->ending_track - th->starting_track + 1; 1087 ntracks >= 0; ntracks--) { 1088 cte = &toc.entries[ntracks]; 1089 cte->addr_type = CD_LBA_FORMAT; 1090 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1091 #if BYTE_ORDER == BIG_ENDIAN 1092 bswap((u_int8_t*)&cte->addr, 1093 sizeof(cte->addr)); 1094 #endif 1095 } else 1096 cte->addr.lba = ntohl(cte->addr.lba); 1097 } 1098 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1099 #if BYTE_ORDER == BIG_ENDIAN 1100 bswap((u_int8_t*)&th->len, sizeof(th->len)); 1101 #endif 1102 } else 1103 th->len = ntohs(th->len); 1104 len = min(len, th->len - (sizeof(th->starting_track) + 1105 sizeof(th->ending_track))); 1106 return (copyout(toc.entries, te->data, len)); 1107 } 1108 case CDIOREADMSADDR: { 1109 struct cd_toc toc; 1110 int sessno = *(int*)addr; 1111 struct cd_toc_entry *cte; 1112 1113 if (sessno != 0) 1114 return (EINVAL); 1115 1116 error = cd_read_toc(cd, 0, 0, &toc, 1117 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1118 XS_CTL_DATA_ONSTACK, 1119 0x40 /* control word for "get MS info" */); 1120 1121 if (error) 1122 return (error); 1123 1124 cte = &toc.entries[0]; 1125 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1126 #if BYTE_ORDER == BIG_ENDIAN 1127 bswap((u_int8_t*)&cte->addr, sizeof(cte->addr)); 1128 #endif 1129 } else 1130 cte->addr.lba = ntohl(cte->addr.lba); 1131 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1132 #if BYTE_ORDER == BIG_ENDIAN 1133 bswap((u_int8_t*)&toc.header.len, 1134 sizeof(toc.header.len)); 1135 #endif 1136 } else 1137 toc.header.len = ntohs(toc.header.len); 1138 1139 *(int*)addr = (toc.header.len >= 10 && cte->track > 1) ? 1140 cte->addr.lba : 0; 1141 return 0; 1142 } 1143 case CDIOCSETPATCH: { 1144 struct ioc_patch *arg = (struct ioc_patch *)addr; 1145 1146 return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0], 1147 arg->patch[1], arg->patch[2], arg->patch[3], 0)); 1148 } 1149 case CDIOCGETVOL: { 1150 struct ioc_vol *arg = (struct ioc_vol *)addr; 1151 1152 return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0)); 1153 } 1154 case CDIOCSETVOL: { 1155 struct ioc_vol *arg = (struct ioc_vol *)addr; 1156 1157 return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0)); 1158 } 1159 1160 case CDIOCSETMONO: 1161 return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL, 1162 BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1163 1164 case CDIOCSETSTEREO: 1165 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1166 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1167 1168 case CDIOCSETMUTE: 1169 return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL, 1170 MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1171 1172 case CDIOCSETLEFT: 1173 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1174 LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1175 1176 case CDIOCSETRIGHT: 1177 return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL, 1178 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1179 1180 case CDIOCRESUME: 1181 return (cd_pause(cd, PA_RESUME)); 1182 case CDIOCPAUSE: 1183 return (cd_pause(cd, PA_PAUSE)); 1184 case CDIOCSTART: 1185 return (scsipi_start(periph, SSS_START, 0)); 1186 case CDIOCSTOP: 1187 return (scsipi_start(periph, SSS_STOP, 0)); 1188 case CDIOCCLOSE: 1189 return (scsipi_start(periph, SSS_START|SSS_LOEJ, 1190 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1191 case DIOCEJECT: 1192 if (*(int *)addr == 0) { 1193 /* 1194 * Don't force eject: check that we are the only 1195 * partition open. If so, unlock it. 1196 */ 1197 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1198 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1199 cd->sc_dk.dk_openmask) { 1200 error = scsipi_prevent(periph, PR_ALLOW, 1201 XS_CTL_IGNORE_NOT_READY); 1202 if (error) 1203 return (error); 1204 } else { 1205 return (EBUSY); 1206 } 1207 } 1208 /* FALLTHROUGH */ 1209 case CDIOCEJECT: /* FALLTHROUGH */ 1210 case ODIOCEJECT: 1211 return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1212 case CDIOCALLOW: 1213 return (scsipi_prevent(periph, PR_ALLOW, 0)); 1214 case CDIOCPREVENT: 1215 return (scsipi_prevent(periph, PR_PREVENT, 0)); 1216 case DIOCLOCK: 1217 return (scsipi_prevent(periph, 1218 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1219 case CDIOCSETDEBUG: 1220 cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2); 1221 return (0); 1222 case CDIOCCLRDEBUG: 1223 cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2); 1224 return (0); 1225 case CDIOCRESET: 1226 case SCIOCRESET: 1227 return (cd_reset(cd)); 1228 case CDIOCLOADUNLOAD: { 1229 struct ioc_load_unload *args = (struct ioc_load_unload *)addr; 1230 1231 return ((*cd->sc_ops->cdo_load_unload)(cd, args->options, 1232 args->slot)); 1233 case DVD_AUTH: 1234 return (dvd_auth(cd, (dvd_authinfo *)addr)); 1235 case DVD_READ_STRUCT: 1236 return (dvd_read_struct(cd, (dvd_struct *)addr)); 1237 } 1238 1239 default: 1240 if (part != RAW_PART) 1241 return (ENOTTY); 1242 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p)); 1243 } 1244 1245 #ifdef DIAGNOSTIC 1246 panic("cdioctl: impossible"); 1247 #endif 1248 } 1249 1250 void 1251 cdgetdefaultlabel(cd, lp) 1252 struct cd_softc *cd; 1253 struct disklabel *lp; 1254 { 1255 1256 memset(lp, 0, sizeof(struct disklabel)); 1257 1258 lp->d_secsize = cd->params.blksize; 1259 lp->d_ntracks = 1; 1260 lp->d_nsectors = 100; 1261 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1262 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1263 1264 switch (scsipi_periph_bustype(cd->sc_periph)) { 1265 #if NCD_SCSIBUS > 0 1266 case SCSIPI_BUSTYPE_SCSI: 1267 lp->d_type = DTYPE_SCSI; 1268 break; 1269 #endif 1270 #if NCD_ATAPIBUS > 0 1271 case SCSIPI_BUSTYPE_ATAPI: 1272 lp->d_type = DTYPE_ATAPI; 1273 break; 1274 #endif 1275 } 1276 strncpy(lp->d_typename, cd->name, 16); 1277 strncpy(lp->d_packname, "fictitious", 16); 1278 lp->d_secperunit = cd->params.disksize; 1279 lp->d_rpm = 300; 1280 lp->d_interleave = 1; 1281 lp->d_flags = D_REMOVABLE; 1282 1283 lp->d_partitions[0].p_offset = 0; 1284 lp->d_partitions[0].p_size = 1285 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1286 lp->d_partitions[0].p_fstype = FS_ISO9660; 1287 lp->d_partitions[RAW_PART].p_offset = 0; 1288 lp->d_partitions[RAW_PART].p_size = 1289 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1290 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1291 lp->d_npartitions = RAW_PART + 1; 1292 1293 lp->d_magic = DISKMAGIC; 1294 lp->d_magic2 = DISKMAGIC; 1295 lp->d_checksum = dkcksum(lp); 1296 } 1297 1298 /* 1299 * Load the label information on the named device 1300 * Actually fabricate a disklabel 1301 * 1302 * EVENTUALLY take information about different 1303 * data tracks from the TOC and put it in the disklabel 1304 */ 1305 void 1306 cdgetdisklabel(cd) 1307 struct cd_softc *cd; 1308 { 1309 struct disklabel *lp = cd->sc_dk.dk_label; 1310 1311 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1312 1313 cdgetdefaultlabel(cd, lp); 1314 } 1315 1316 /* 1317 * Find out from the device what it's capacity is 1318 */ 1319 u_long 1320 cd_size(cd, flags) 1321 struct cd_softc *cd; 1322 int flags; 1323 { 1324 struct scsipi_read_cd_cap_data rdcap; 1325 struct scsipi_read_cd_capacity scsipi_cmd; 1326 int blksize; 1327 u_long size; 1328 1329 if (cd->sc_periph->periph_quirks & PQUIRK_NOCAPACITY) { 1330 /* 1331 * the drive doesn't support the READ_CD_CAPACITY command 1332 * use a fake size 1333 */ 1334 cd->params.blksize = 2048; 1335 cd->params.disksize = 400000; 1336 return (400000); 1337 } 1338 1339 /* 1340 * make up a scsi command and ask the scsi driver to do 1341 * it for you. 1342 */ 1343 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1344 scsipi_cmd.opcode = READ_CD_CAPACITY; 1345 1346 /* 1347 * If the command works, interpret the result as a 4 byte 1348 * number of blocks and a blocksize 1349 */ 1350 if (scsipi_command(cd->sc_periph, 1351 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1352 (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL, 1353 flags | XS_CTL_DATA_IN | XS_CTL_DATA_IN) != 0) 1354 return (0); 1355 1356 blksize = _4btol(rdcap.length); 1357 if ((blksize < 512) || ((blksize & 511) != 0)) 1358 blksize = 2048; /* some drives lie ! */ 1359 cd->params.blksize = blksize; 1360 1361 size = _4btol(rdcap.addr) + 1; 1362 if (size < 100) 1363 size = 400000; /* ditto */ 1364 cd->params.disksize = size; 1365 1366 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, 1367 ("cd_size: %d %ld\n", blksize, size)); 1368 return (size); 1369 } 1370 1371 /* 1372 * Get scsi driver to send a "start playing" command 1373 */ 1374 int 1375 cd_play(cd, blkno, nblks) 1376 struct cd_softc *cd; 1377 int blkno, nblks; 1378 { 1379 struct scsipi_play scsipi_cmd; 1380 1381 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1382 scsipi_cmd.opcode = PLAY; 1383 _lto4b(blkno, scsipi_cmd.blk_addr); 1384 _lto2b(nblks, scsipi_cmd.xfer_len); 1385 return (scsipi_command(cd->sc_periph, 1386 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1387 0, 0, CDRETRIES, 30000, NULL, 0)); 1388 } 1389 1390 /* 1391 * Get scsi driver to send a "start playing" command 1392 */ 1393 int 1394 cd_play_tracks(cd, strack, sindex, etrack, eindex) 1395 struct cd_softc *cd; 1396 int strack, sindex, etrack, eindex; 1397 { 1398 struct cd_toc toc; 1399 int error; 1400 1401 if (!etrack) 1402 return (EIO); 1403 if (strack > etrack) 1404 return (EINVAL); 1405 1406 if ((error = cd_load_toc(cd, &toc, XS_CTL_DATA_ONSTACK)) != 0) 1407 return (error); 1408 1409 if (++etrack > (toc.header.ending_track+1)) 1410 etrack = toc.header.ending_track+1; 1411 1412 strack -= toc.header.starting_track; 1413 etrack -= toc.header.starting_track; 1414 if (strack < 0) 1415 return (EINVAL); 1416 1417 return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute, 1418 toc.entries[strack].addr.msf.second, 1419 toc.entries[strack].addr.msf.frame, 1420 toc.entries[etrack].addr.msf.minute, 1421 toc.entries[etrack].addr.msf.second, 1422 toc.entries[etrack].addr.msf.frame)); 1423 } 1424 1425 /* 1426 * Get scsi driver to send a "play msf" command 1427 */ 1428 int 1429 cd_play_msf(cd, startm, starts, startf, endm, ends, endf) 1430 struct cd_softc *cd; 1431 int startm, starts, startf, endm, ends, endf; 1432 { 1433 struct scsipi_play_msf scsipi_cmd; 1434 1435 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1436 scsipi_cmd.opcode = PLAY_MSF; 1437 scsipi_cmd.start_m = startm; 1438 scsipi_cmd.start_s = starts; 1439 scsipi_cmd.start_f = startf; 1440 scsipi_cmd.end_m = endm; 1441 scsipi_cmd.end_s = ends; 1442 scsipi_cmd.end_f = endf; 1443 return (scsipi_command(cd->sc_periph, 1444 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1445 0, 0, CDRETRIES, 30000, NULL, 0)); 1446 } 1447 1448 /* 1449 * Get scsi driver to send a "start up" command 1450 */ 1451 int 1452 cd_pause(cd, go) 1453 struct cd_softc *cd; 1454 int go; 1455 { 1456 struct scsipi_pause scsipi_cmd; 1457 1458 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1459 scsipi_cmd.opcode = PAUSE; 1460 scsipi_cmd.resume = go & 0xff; 1461 return (scsipi_command(cd->sc_periph, 1462 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1463 0, 0, CDRETRIES, 30000, NULL, 0)); 1464 } 1465 1466 /* 1467 * Get scsi driver to send a "RESET" command 1468 */ 1469 int 1470 cd_reset(cd) 1471 struct cd_softc *cd; 1472 { 1473 1474 return (scsipi_command(cd->sc_periph, 0, 0, 0, 0, 1475 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1476 } 1477 1478 /* 1479 * Read subchannel 1480 */ 1481 int 1482 cd_read_subchannel(cd, mode, format, track, data, len, flags) 1483 struct cd_softc *cd; 1484 int mode, format, track, len; 1485 struct cd_sub_channel_info *data; 1486 int flags; 1487 { 1488 struct scsipi_read_subchannel scsipi_cmd; 1489 1490 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1491 scsipi_cmd.opcode = READ_SUBCHANNEL; 1492 if (mode == CD_MSF_FORMAT) 1493 scsipi_cmd.byte2 |= CD_MSF; 1494 scsipi_cmd.byte3 = SRS_SUBQ; 1495 scsipi_cmd.subchan_format = format; 1496 scsipi_cmd.track = track; 1497 _lto2b(len, scsipi_cmd.data_len); 1498 return (scsipi_command(cd->sc_periph, 1499 (struct scsipi_generic *)&scsipi_cmd, 1500 sizeof(struct scsipi_read_subchannel), (u_char *)data, len, 1501 CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT)); 1502 } 1503 1504 /* 1505 * Read table of contents 1506 */ 1507 int 1508 cd_read_toc(cd, mode, start, data, len, flags, control) 1509 struct cd_softc *cd; 1510 int mode, start, len, control; 1511 void *data; 1512 int flags; 1513 { 1514 struct scsipi_read_toc scsipi_cmd; 1515 int ntoc; 1516 1517 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1518 #if 0 1519 if (len != sizeof(struct ioc_toc_header)) 1520 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 1521 sizeof(struct cd_toc_entry); 1522 else 1523 #endif 1524 ntoc = len; 1525 scsipi_cmd.opcode = READ_TOC; 1526 if (mode == CD_MSF_FORMAT) 1527 scsipi_cmd.byte2 |= CD_MSF; 1528 scsipi_cmd.from_track = start; 1529 _lto2b(ntoc, scsipi_cmd.data_len); 1530 scsipi_cmd.control = control; 1531 return (scsipi_command(cd->sc_periph, 1532 (struct scsipi_generic *)&scsipi_cmd, 1533 sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES, 1534 30000, NULL, flags | XS_CTL_DATA_IN)); 1535 } 1536 1537 int 1538 cd_load_toc(cd, toc, flags) 1539 struct cd_softc *cd; 1540 struct cd_toc *toc; 1541 int flags; 1542 { 1543 int ntracks, len, error; 1544 1545 if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 1546 flags, 0)) != 0) 1547 return (error); 1548 1549 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 1550 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 1551 sizeof(toc->header); 1552 if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 1553 flags, 0)) != 0) 1554 return (error); 1555 return (0); 1556 } 1557 1558 /* 1559 * Get the scsi driver to send a full inquiry to the device and use the 1560 * results to fill out the disk parameter structure. 1561 */ 1562 int 1563 cd_get_parms(cd, flags) 1564 struct cd_softc *cd; 1565 int flags; 1566 { 1567 1568 /* 1569 * give a number of sectors so that sec * trks * cyls 1570 * is <= disk_size 1571 */ 1572 if (cd_size(cd, flags) == 0) 1573 return (ENXIO); 1574 return (0); 1575 } 1576 1577 int 1578 cdsize(dev) 1579 dev_t dev; 1580 { 1581 1582 /* CD-ROMs are read-only. */ 1583 return (-1); 1584 } 1585 1586 int 1587 cddump(dev, blkno, va, size) 1588 dev_t dev; 1589 daddr_t blkno; 1590 caddr_t va; 1591 size_t size; 1592 { 1593 1594 /* Not implemented. */ 1595 return (ENXIO); 1596 } 1597 1598 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 1599 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 1600 1601 int 1602 dvd_auth(cd, a) 1603 struct cd_softc *cd; 1604 dvd_authinfo *a; 1605 { 1606 struct scsipi_generic cmd; 1607 u_int8_t buf[20]; 1608 int error; 1609 1610 memset(cmd.bytes, 0, 15); 1611 memset(buf, 0, sizeof(buf)); 1612 1613 switch (a->type) { 1614 case DVD_LU_SEND_AGID: 1615 cmd.opcode = GPCMD_REPORT_KEY; 1616 cmd.bytes[8] = 8; 1617 cmd.bytes[9] = 0 | (0 << 6); 1618 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1619 CDRETRIES, 30000, NULL, 1620 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1621 if (error) 1622 return (error); 1623 a->lsa.agid = buf[7] >> 6; 1624 return (0); 1625 1626 case DVD_LU_SEND_CHALLENGE: 1627 cmd.opcode = GPCMD_REPORT_KEY; 1628 cmd.bytes[8] = 16; 1629 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 1630 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1631 CDRETRIES, 30000, NULL, 1632 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1633 if (error) 1634 return (error); 1635 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1636 return (0); 1637 1638 case DVD_LU_SEND_KEY1: 1639 cmd.opcode = GPCMD_REPORT_KEY; 1640 cmd.bytes[8] = 12; 1641 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 1642 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1643 CDRETRIES, 30000, NULL, 1644 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1645 if (error) 1646 return (error); 1647 dvd_copy_key(a->lsk.key, &buf[4]); 1648 return (0); 1649 1650 case DVD_LU_SEND_TITLE_KEY: 1651 cmd.opcode = GPCMD_REPORT_KEY; 1652 _lto4b(a->lstk.lba, &cmd.bytes[1]); 1653 cmd.bytes[8] = 12; 1654 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 1655 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1656 CDRETRIES, 30000, NULL, 1657 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1658 if (error) 1659 return (error); 1660 a->lstk.cpm = (buf[4] >> 7) & 1; 1661 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1662 a->lstk.cgms = (buf[4] >> 4) & 3; 1663 dvd_copy_key(a->lstk.title_key, &buf[5]); 1664 return (0); 1665 1666 case DVD_LU_SEND_ASF: 1667 cmd.opcode = GPCMD_REPORT_KEY; 1668 cmd.bytes[8] = 8; 1669 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 1670 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1671 CDRETRIES, 30000, NULL, 1672 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1673 if (error) 1674 return (error); 1675 a->lsasf.asf = buf[7] & 1; 1676 return (0); 1677 1678 case DVD_HOST_SEND_CHALLENGE: 1679 cmd.opcode = GPCMD_SEND_KEY; 1680 cmd.bytes[8] = 16; 1681 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 1682 buf[1] = 14; 1683 dvd_copy_challenge(&buf[4], a->hsc.chal); 1684 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1685 CDRETRIES, 30000, NULL, 1686 XS_CTL_DATA_OUT|XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1687 if (error) 1688 return (error); 1689 a->type = DVD_LU_SEND_KEY1; 1690 return (0); 1691 1692 case DVD_HOST_SEND_KEY2: 1693 cmd.opcode = GPCMD_SEND_KEY; 1694 cmd.bytes[8] = 12; 1695 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 1696 buf[1] = 10; 1697 dvd_copy_key(&buf[4], a->hsk.key); 1698 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1699 CDRETRIES, 30000, NULL, 1700 XS_CTL_DATA_OUT|XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1701 if (error) { 1702 a->type = DVD_AUTH_FAILURE; 1703 return (error); 1704 } 1705 a->type = DVD_AUTH_ESTABLISHED; 1706 return (0); 1707 1708 case DVD_INVALIDATE_AGID: 1709 cmd.opcode = GPCMD_REPORT_KEY; 1710 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 1711 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1712 CDRETRIES, 30000, NULL, 0); 1713 if (error) 1714 return (error); 1715 return (0); 1716 1717 default: 1718 return (ENOTTY); 1719 } 1720 } 1721 1722 int 1723 dvd_read_physical(cd, s) 1724 struct cd_softc *cd; 1725 dvd_struct *s; 1726 { 1727 struct scsipi_generic cmd; 1728 u_int8_t buf[4 + 4 * 20], *bufp; 1729 int error; 1730 struct dvd_layer *layer; 1731 int i; 1732 1733 memset(cmd.bytes, 0, 15); 1734 memset(buf, 0, sizeof(buf)); 1735 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1736 cmd.bytes[6] = s->type; 1737 _lto2b(sizeof(buf), &cmd.bytes[7]); 1738 1739 cmd.bytes[5] = s->physical.layer_num; 1740 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1741 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1742 if (error) 1743 return (error); 1744 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4; 1745 i++, bufp += 20, layer++) { 1746 memset(layer, 0, sizeof(*layer)); 1747 layer->book_version = bufp[0] & 0xf; 1748 layer->book_type = bufp[0] >> 4; 1749 layer->min_rate = bufp[1] & 0xf; 1750 layer->disc_size = bufp[1] >> 4; 1751 layer->layer_type = bufp[2] & 0xf; 1752 layer->track_path = (bufp[2] >> 4) & 1; 1753 layer->nlayers = (bufp[2] >> 5) & 3; 1754 layer->track_density = bufp[3] & 0xf; 1755 layer->linear_density = bufp[3] >> 4; 1756 layer->start_sector = _4btol(&bufp[4]); 1757 layer->end_sector = _4btol(&bufp[8]); 1758 layer->end_sector_l0 = _4btol(&bufp[12]); 1759 layer->bca = bufp[16] >> 7; 1760 } 1761 return (0); 1762 } 1763 1764 int 1765 dvd_read_copyright(cd, s) 1766 struct cd_softc *cd; 1767 dvd_struct *s; 1768 { 1769 struct scsipi_generic cmd; 1770 u_int8_t buf[8]; 1771 int error; 1772 1773 memset(cmd.bytes, 0, 15); 1774 memset(buf, 0, sizeof(buf)); 1775 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1776 cmd.bytes[6] = s->type; 1777 _lto2b(sizeof(buf), &cmd.bytes[7]); 1778 1779 cmd.bytes[5] = s->copyright.layer_num; 1780 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1781 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1782 if (error) 1783 return (error); 1784 s->copyright.cpst = buf[4]; 1785 s->copyright.rmi = buf[5]; 1786 return (0); 1787 } 1788 1789 int 1790 dvd_read_disckey(cd, s) 1791 struct cd_softc *cd; 1792 dvd_struct *s; 1793 { 1794 struct scsipi_generic cmd; 1795 u_int8_t buf[4 + 2048]; 1796 int error; 1797 1798 memset(cmd.bytes, 0, 15); 1799 memset(buf, 0, sizeof(buf)); 1800 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1801 cmd.bytes[6] = s->type; 1802 _lto2b(sizeof(buf), &cmd.bytes[7]); 1803 1804 cmd.bytes[9] = s->disckey.agid << 6; 1805 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1806 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1807 if (error) 1808 return (error); 1809 memcpy(s->disckey.value, &buf[4], 2048); 1810 return (0); 1811 } 1812 1813 int 1814 dvd_read_bca(cd, s) 1815 struct cd_softc *cd; 1816 dvd_struct *s; 1817 { 1818 struct scsipi_generic cmd; 1819 u_int8_t buf[4 + 188]; 1820 int error; 1821 1822 memset(cmd.bytes, 0, 15); 1823 memset(buf, 0, sizeof(buf)); 1824 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1825 cmd.bytes[6] = s->type; 1826 _lto2b(sizeof(buf), &cmd.bytes[7]); 1827 1828 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1829 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1830 if (error) 1831 return (error); 1832 s->bca.len = _2btol(&buf[0]); 1833 if (s->bca.len < 12 || s->bca.len > 188) 1834 return (EIO); 1835 memcpy(s->bca.value, &buf[4], s->bca.len); 1836 return (0); 1837 } 1838 1839 int 1840 dvd_read_manufact(cd, s) 1841 struct cd_softc *cd; 1842 dvd_struct *s; 1843 { 1844 struct scsipi_generic cmd; 1845 u_int8_t buf[4 + 2048]; 1846 int error; 1847 1848 memset(cmd.bytes, 0, 15); 1849 memset(buf, 0, sizeof(buf)); 1850 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1851 cmd.bytes[6] = s->type; 1852 _lto2b(sizeof(buf), &cmd.bytes[7]); 1853 1854 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1855 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1856 if (error) 1857 return (error); 1858 s->manufact.len = _2btol(&buf[0]); 1859 if (s->manufact.len < 0 || s->manufact.len > 2048) 1860 return (EIO); 1861 memcpy(s->manufact.value, &buf[4], s->manufact.len); 1862 return (0); 1863 } 1864 1865 int 1866 dvd_read_struct(cd, s) 1867 struct cd_softc *cd; 1868 dvd_struct *s; 1869 { 1870 1871 switch (s->type) { 1872 case DVD_STRUCT_PHYSICAL: 1873 return (dvd_read_physical(cd, s)); 1874 case DVD_STRUCT_COPYRIGHT: 1875 return (dvd_read_copyright(cd, s)); 1876 case DVD_STRUCT_DISCKEY: 1877 return (dvd_read_disckey(cd, s)); 1878 case DVD_STRUCT_BCA: 1879 return (dvd_read_bca(cd, s)); 1880 case DVD_STRUCT_MANUFACT: 1881 return (dvd_read_manufact(cd, s)); 1882 default: 1883 return (EINVAL); 1884 } 1885 } 1886