1 /* $OpenBSD: cd.c,v 1.207 2011/07/06 04:49:36 matthew Exp $ */ 2 /* $NetBSD: cd.c,v 1.100 1997/04/02 02:29:30 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Charles M. Hannum. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Originally written by Julian Elischer (julian@tfs.com) 35 * for TRW Financial Systems for use under the MACH(2.5) operating system. 36 * 37 * TRW Financial Systems, in accordance with their agreement with Carnegie 38 * Mellon University, makes this software available to CMU to distribute 39 * or use in any manner that they see fit as long as this message is kept with 40 * the software. For this reason TFS also grants any other persons or 41 * organisations permission to use or modify this software. 42 * 43 * TFS supplies this software to be publicly redistributed 44 * on the understanding that TFS is not responsible for the correct 45 * functioning of this software in any circumstances. 46 * 47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 48 */ 49 50 #include <sys/types.h> 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/timeout.h> 54 #include <sys/file.h> 55 #include <sys/stat.h> 56 #include <sys/ioctl.h> 57 #include <sys/mtio.h> 58 #include <sys/buf.h> 59 #include <sys/uio.h> 60 #include <sys/malloc.h> 61 #include <sys/pool.h> 62 #include <sys/errno.h> 63 #include <sys/device.h> 64 #include <sys/disklabel.h> 65 #include <sys/disk.h> 66 #include <sys/cdio.h> 67 #include <sys/proc.h> 68 #include <sys/conf.h> 69 #include <sys/scsiio.h> 70 #include <sys/dkio.h> 71 #include <sys/vnode.h> 72 73 #include <scsi/scsi_all.h> 74 #include <scsi/cd.h> 75 #include <scsi/scsi_disk.h> /* rw_big and start_stop come from there */ 76 #include <scsi/scsiconf.h> 77 78 79 #include <ufs/ffs/fs.h> /* for BBSIZE and SBSIZE */ 80 81 #define CDOUTSTANDING 4 82 83 #define MAXTRACK 99 84 #define CD_FRAMES 75 85 #define CD_SECS 60 86 87 struct cd_toc { 88 struct ioc_toc_header header; 89 struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */ 90 /* leadout */ 91 }; 92 93 int cdmatch(struct device *, void *, void *); 94 void cdattach(struct device *, struct device *, void *); 95 int cdactivate(struct device *, int); 96 int cddetach(struct device *, int); 97 98 struct cd_softc { 99 struct device sc_dev; 100 struct disk sc_dk; 101 102 int sc_flags; 103 #define CDF_ANCIENT 0x10 /* disk is ancient; for minphys */ 104 #define CDF_DYING 0x40 /* dying, when deactivated */ 105 #define CDF_WAITING 0x100 106 struct scsi_link *sc_link; /* contains our targ, lun, etc. */ 107 struct cd_parms { 108 u_int32_t secsize; 109 daddr64_t disksize; /* total number sectors */ 110 } params; 111 struct bufq sc_bufq; 112 struct scsi_xshandler sc_xsh; 113 struct timeout sc_timeout; 114 }; 115 116 void cdstart(struct scsi_xfer *); 117 void cd_buf_done(struct scsi_xfer *); 118 void cdminphys(struct buf *); 119 int cdgetdisklabel(dev_t, struct cd_softc *, struct disklabel *, int); 120 int cd_setchan(struct cd_softc *, int, int, int, int, int); 121 int cd_getvol(struct cd_softc *cd, struct ioc_vol *, int); 122 int cd_setvol(struct cd_softc *, const struct ioc_vol *, int); 123 int cd_load_unload(struct cd_softc *, int, int); 124 int cd_set_pa_immed(struct cd_softc *, int); 125 int cd_play(struct cd_softc *, int, int); 126 int cd_play_tracks(struct cd_softc *, int, int, int, int); 127 int cd_play_msf(struct cd_softc *, int, int, int, int, int, int); 128 int cd_pause(struct cd_softc *, int); 129 int cd_reset(struct cd_softc *); 130 int cd_read_subchannel(struct cd_softc *, int, int, int, 131 struct cd_sub_channel_info *, int ); 132 int cd_read_toc(struct cd_softc *, int, int, void *, int, int); 133 int cd_get_parms(struct cd_softc *, int); 134 int cd_load_toc(struct cd_softc *, struct cd_toc *, int); 135 int cd_interpret_sense(struct scsi_xfer *); 136 137 int dvd_auth(struct cd_softc *, union dvd_authinfo *); 138 int dvd_read_physical(struct cd_softc *, union dvd_struct *); 139 int dvd_read_copyright(struct cd_softc *, union dvd_struct *); 140 int dvd_read_disckey(struct cd_softc *, union dvd_struct *); 141 int dvd_read_bca(struct cd_softc *, union dvd_struct *); 142 int dvd_read_manufact(struct cd_softc *, union dvd_struct *); 143 int dvd_read_struct(struct cd_softc *, union dvd_struct *); 144 145 #if defined(__macppc__) 146 int cd_eject(void); 147 #endif 148 149 struct cfattach cd_ca = { 150 sizeof(struct cd_softc), cdmatch, cdattach, 151 cddetach, cdactivate 152 }; 153 154 struct cfdriver cd_cd = { 155 NULL, "cd", DV_DISK 156 }; 157 158 const struct scsi_inquiry_pattern cd_patterns[] = { 159 {T_CDROM, T_REMOV, 160 "", "", ""}, 161 {T_CDROM, T_FIXED, 162 "", "", ""}, 163 {T_WORM, T_REMOV, 164 "", "", ""}, 165 {T_WORM, T_FIXED, 166 "", "", ""}, 167 {T_DIRECT, T_REMOV, 168 "NEC CD-ROM DRIVE:260", "", ""}, 169 #if 0 170 {T_CDROM, T_REMOV, /* more luns */ 171 "PIONEER ", "CD-ROM DRM-600 ", ""}, 172 #endif 173 }; 174 175 #define cdlookup(unit) (struct cd_softc *)disk_lookup(&cd_cd, (unit)) 176 177 int 178 cdmatch(struct device *parent, void *match, void *aux) 179 { 180 struct scsi_attach_args *sa = aux; 181 int priority; 182 183 scsi_inqmatch(sa->sa_inqbuf, cd_patterns, nitems(cd_patterns), 184 sizeof(cd_patterns[0]), &priority); 185 186 return (priority); 187 } 188 189 /* 190 * The routine called by the low level scsi routine when it discovers 191 * A device suitable for this driver 192 */ 193 void 194 cdattach(struct device *parent, struct device *self, void *aux) 195 { 196 struct cd_softc *sc = (struct cd_softc *)self; 197 struct scsi_attach_args *sa = aux; 198 struct scsi_link *sc_link = sa->sa_sc_link; 199 200 SC_DEBUG(sc_link, SDEV_DB2, ("cdattach:\n")); 201 202 /* 203 * Store information needed to contact our base driver 204 */ 205 sc->sc_link = sc_link; 206 sc_link->interpret_sense = cd_interpret_sense; 207 sc_link->device_softc = sc; 208 if (sc_link->openings > CDOUTSTANDING) 209 sc_link->openings = CDOUTSTANDING; 210 211 /* 212 * Initialize disk structures. 213 */ 214 sc->sc_dk.dk_name = sc->sc_dev.dv_xname; 215 bufq_init(&sc->sc_bufq, BUFQ_DEFAULT); 216 217 /* 218 * Note if this device is ancient. This is used in cdminphys(). 219 */ 220 if (!(sc_link->flags & SDEV_ATAPI) && 221 SCSISPC(sa->sa_inqbuf->version) == 0) 222 sc->sc_flags |= CDF_ANCIENT; 223 224 printf("\n"); 225 226 scsi_xsh_set(&sc->sc_xsh, sc_link, cdstart); 227 timeout_set(&sc->sc_timeout, (void (*)(void *))scsi_xsh_add, 228 &sc->sc_xsh); 229 230 /* Attach disk. */ 231 sc->sc_dk.dk_flags = DKF_NOLABELREAD; 232 disk_attach(&sc->sc_dev, &sc->sc_dk); 233 } 234 235 236 int 237 cdactivate(struct device *self, int act) 238 { 239 struct cd_softc *sc = (struct cd_softc *)self; 240 int rv = 0; 241 242 switch (act) { 243 case DVACT_RESUME: 244 /* 245 * When resuming, hardware may have forgotten we locked it. So if 246 * there are any open partitions, lock the CD. 247 */ 248 if (sc->sc_dk.dk_openmask != 0) 249 scsi_prevent(sc->sc_link, PR_PREVENT, 250 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 251 SCSI_SILENT | SCSI_AUTOCONF); 252 break; 253 case DVACT_DEACTIVATE: 254 sc->sc_flags |= CDF_DYING; 255 scsi_xsh_del(&sc->sc_xsh); 256 break; 257 } 258 return (rv); 259 } 260 261 int 262 cddetach(struct device *self, int flags) 263 { 264 struct cd_softc *sc = (struct cd_softc *)self; 265 266 bufq_drain(&sc->sc_bufq); 267 268 disk_gone(cdopen, self->dv_unit); 269 270 /* Detach disk. */ 271 bufq_destroy(&sc->sc_bufq); 272 disk_detach(&sc->sc_dk); 273 274 return (0); 275 } 276 277 /* 278 * Open the device. Make sure the partition info is as up-to-date as can be. 279 */ 280 int 281 cdopen(dev_t dev, int flag, int fmt, struct proc *p) 282 { 283 struct scsi_link *sc_link; 284 struct cd_softc *sc; 285 int error = 0, part, rawopen, unit; 286 287 unit = DISKUNIT(dev); 288 part = DISKPART(dev); 289 290 rawopen = (part == RAW_PART) && (fmt == S_IFCHR); 291 292 sc = cdlookup(unit); 293 if (sc == NULL) 294 return (ENXIO); 295 if (sc->sc_flags & CDF_DYING) { 296 device_unref(&sc->sc_dev); 297 return (ENXIO); 298 } 299 300 sc_link = sc->sc_link; 301 SC_DEBUG(sc_link, SDEV_DB1, 302 ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 303 cd_cd.cd_ndevs, part)); 304 305 if ((error = disk_lock(&sc->sc_dk)) != 0) { 306 device_unref(&sc->sc_dev); 307 return (error); 308 } 309 310 if (sc->sc_dk.dk_openmask != 0) { 311 /* 312 * If any partition is open, but the disk has been invalidated, 313 * disallow further opens. 314 */ 315 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 316 if (rawopen) 317 goto out; 318 error = EIO; 319 goto bad; 320 } 321 } else { 322 /* 323 * Check that it is still responding and ok. Drive can be in 324 * progress of loading media so use increased retries number 325 * and don't ignore NOT_READY. 326 */ 327 328 /* Use cd_interpret_sense() now. */ 329 sc_link->flags |= SDEV_OPEN; 330 331 error = scsi_test_unit_ready(sc_link, TEST_READY_RETRIES, 332 (rawopen ? SCSI_SILENT : 0) | SCSI_IGNORE_ILLEGAL_REQUEST | 333 SCSI_IGNORE_MEDIA_CHANGE); 334 335 /* Start the cd spinning if necessary. */ 336 if (error == EIO) 337 error = scsi_start(sc_link, SSS_START, 338 SCSI_IGNORE_ILLEGAL_REQUEST | 339 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 340 341 if (error) { 342 if (rawopen) { 343 error = 0; 344 goto out; 345 } else 346 goto bad; 347 } 348 349 /* Lock the cd in. */ 350 error = scsi_prevent(sc_link, PR_PREVENT, 351 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 352 SCSI_SILENT); 353 if (error) 354 goto bad; 355 356 /* Load the physical device parameters. */ 357 sc_link->flags |= SDEV_MEDIA_LOADED; 358 if (cd_get_parms(sc, (rawopen ? SCSI_SILENT : 0) | 359 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE)) { 360 sc_link->flags &= ~SDEV_MEDIA_LOADED; 361 error = ENXIO; 362 goto bad; 363 } 364 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded\n")); 365 366 /* Fabricate a disk label. */ 367 cdgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0); 368 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated\n")); 369 } 370 371 out: 372 if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0) 373 goto bad; 374 375 sc_link->flags |= SDEV_OPEN; 376 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 377 378 /* It's OK to fall through because dk_openmask is now non-zero. */ 379 bad: 380 if (sc->sc_dk.dk_openmask == 0) { 381 scsi_prevent(sc_link, PR_ALLOW, 382 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 383 SCSI_SILENT); 384 sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED); 385 } 386 387 disk_unlock(&sc->sc_dk); 388 device_unref(&sc->sc_dev); 389 return (error); 390 } 391 392 /* 393 * Close the device. Only called if we are the last occurrence of an open 394 * device. 395 */ 396 int 397 cdclose(dev_t dev, int flag, int fmt, struct proc *p) 398 { 399 struct cd_softc *sc; 400 int part = DISKPART(dev); 401 402 sc = cdlookup(DISKUNIT(dev)); 403 if (sc == NULL) 404 return ENXIO; 405 if (sc->sc_flags & CDF_DYING) { 406 device_unref(&sc->sc_dev); 407 return (ENXIO); 408 } 409 410 disk_lock_nointr(&sc->sc_dk); 411 412 disk_closepart(&sc->sc_dk, part, fmt); 413 414 if (sc->sc_dk.dk_openmask == 0) { 415 /* XXXX Must wait for I/O to complete! */ 416 417 scsi_prevent(sc->sc_link, PR_ALLOW, 418 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | 419 SCSI_SILENT); 420 sc->sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED); 421 422 if (sc->sc_link->flags & SDEV_EJECTING) { 423 scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0); 424 425 sc->sc_link->flags &= ~SDEV_EJECTING; 426 } 427 428 timeout_del(&sc->sc_timeout); 429 scsi_xsh_del(&sc->sc_xsh); 430 } 431 432 disk_unlock(&sc->sc_dk); 433 434 device_unref(&sc->sc_dev); 435 return 0; 436 } 437 438 /* 439 * Actually translate the requested transfer into one the physical driver can 440 * understand. The transfer is described by a buf and will include only one 441 * physical transfer. 442 */ 443 void 444 cdstrategy(struct buf *bp) 445 { 446 struct cd_softc *sc; 447 int s; 448 449 sc = cdlookup(DISKUNIT(bp->b_dev)); 450 if (sc == NULL) { 451 bp->b_error = ENXIO; 452 goto bad; 453 } 454 if (sc->sc_flags & CDF_DYING) { 455 bp->b_error = ENXIO; 456 goto bad; 457 } 458 459 SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdstrategy: %ld bytes @ blk %d\n", 460 bp->b_bcount, bp->b_blkno)); 461 /* 462 * If the device has been made invalid, error out 463 * maybe the media changed, or no media loaded 464 */ 465 if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 466 bp->b_error = EIO; 467 goto bad; 468 } 469 470 /* Validate the request. */ 471 if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1) 472 goto done; 473 474 /* Place it in the queue of disk activities for this disk. */ 475 bufq_queue(&sc->sc_bufq, bp); 476 477 /* 478 * Tell the device to get going on the transfer if it's 479 * not doing anything, otherwise just wait for completion 480 */ 481 scsi_xsh_add(&sc->sc_xsh); 482 483 device_unref(&sc->sc_dev); 484 return; 485 486 bad: 487 bp->b_flags |= B_ERROR; 488 bp->b_resid = bp->b_bcount; 489 done: 490 s = splbio(); 491 biodone(bp); 492 splx(s); 493 if (sc != NULL) 494 device_unref(&sc->sc_dev); 495 } 496 497 /* 498 * cdstart looks to see if there is a buf waiting for the device 499 * and that the device is not already busy. If both are true, 500 * It deques the buf and creates a scsi command to perform the 501 * transfer in the buf. The transfer request will call scsi_done 502 * on completion, which will in turn call this routine again 503 * so that the next queued transfer is performed. 504 * The bufs are queued by the strategy routine (cdstrategy) 505 * 506 * This routine is also called after other non-queued requests 507 * have been made of the scsi driver, to ensure that the queue 508 * continues to be drained. 509 * 510 * must be called at the correct (highish) spl level 511 * cdstart() is called at splbio from cdstrategy and scsi_done 512 */ 513 void 514 cdstart(struct scsi_xfer *xs) 515 { 516 struct scsi_link *sc_link = xs->sc_link; 517 struct cd_softc *sc = sc_link->device_softc; 518 struct buf *bp; 519 struct scsi_rw_big *cmd_big; 520 struct scsi_rw *cmd_small; 521 int secno, nsecs; 522 struct partition *p; 523 int read; 524 525 SC_DEBUG(sc_link, SDEV_DB2, ("cdstart\n")); 526 527 if (sc->sc_flags & CDF_DYING) { 528 scsi_xs_put(xs); 529 return; 530 } 531 532 /* 533 * If the device has become invalid, abort all the 534 * reads and writes until all files have been closed and 535 * re-opened 536 */ 537 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 538 bufq_drain(&sc->sc_bufq); 539 scsi_xs_put(xs); 540 return; 541 } 542 543 bp = bufq_dequeue(&sc->sc_bufq); 544 if (bp == NULL) { 545 scsi_xs_put(xs); 546 return; 547 } 548 549 /* 550 * We have a buf, now we should make a command 551 * 552 * First, translate the block to absolute and put it in terms 553 * of the logical blocksize of the device. 554 */ 555 secno = 556 bp->b_blkno / (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE); 557 p = &sc->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)]; 558 secno += DL_GETPOFFSET(p); 559 nsecs = howmany(bp->b_bcount, sc->sc_dk.dk_label->d_secsize); 560 561 read = (bp->b_flags & B_READ); 562 563 /* 564 * Fill out the scsi command. If the transfer will 565 * fit in a "small" cdb, use it. 566 */ 567 if (!(sc_link->flags & SDEV_ATAPI) && 568 !(sc_link->quirks & SDEV_ONLYBIG) && 569 ((secno & 0x1fffff) == secno) && 570 ((nsecs & 0xff) == nsecs)) { 571 /* 572 * We can fit in a small cdb. 573 */ 574 cmd_small = (struct scsi_rw *)xs->cmd; 575 cmd_small->opcode = read ? 576 READ_COMMAND : WRITE_COMMAND; 577 _lto3b(secno, cmd_small->addr); 578 cmd_small->length = nsecs & 0xff; 579 xs->cmdlen = sizeof(*cmd_small); 580 } else { 581 /* 582 * Need a large cdb. 583 */ 584 cmd_big = (struct scsi_rw_big *)xs->cmd; 585 cmd_big->opcode = read ? 586 READ_BIG : WRITE_BIG; 587 _lto4b(secno, cmd_big->addr); 588 _lto2b(nsecs, cmd_big->length); 589 xs->cmdlen = sizeof(*cmd_big); 590 } 591 592 xs->flags |= (read ? SCSI_DATA_IN : SCSI_DATA_OUT); 593 xs->timeout = 30000; 594 xs->data = bp->b_data; 595 xs->datalen = bp->b_bcount; 596 xs->done = cd_buf_done; 597 xs->cookie = bp; 598 xs->bp = bp; 599 600 /* Instrumentation. */ 601 disk_busy(&sc->sc_dk); 602 603 scsi_xs_exec(xs); 604 605 if (ISSET(sc->sc_flags, CDF_WAITING)) 606 CLR(sc->sc_flags, CDF_WAITING); 607 else if (bufq_peek(&sc->sc_bufq)) 608 scsi_xsh_add(&sc->sc_xsh); 609 } 610 611 void 612 cd_buf_done(struct scsi_xfer *xs) 613 { 614 struct cd_softc *sc = xs->sc_link->device_softc; 615 struct buf *bp = xs->cookie; 616 int error, s; 617 618 switch (xs->error) { 619 case XS_NOERROR: 620 bp->b_error = 0; 621 bp->b_resid = xs->resid; 622 break; 623 624 case XS_NO_CCB: 625 /* The adapter is busy, requeue the buf and try it later. */ 626 disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid, 627 bp->b_flags & B_READ); 628 bufq_requeue(&sc->sc_bufq, bp); 629 scsi_xs_put(xs); 630 SET(sc->sc_flags, CDF_WAITING); 631 timeout_add(&sc->sc_timeout, 1); 632 return; 633 634 case XS_SENSE: 635 case XS_SHORTSENSE: 636 #ifdef SCSIDEBUG 637 scsi_sense_print_debug(xs); 638 #endif 639 error = cd_interpret_sense(xs); 640 if (error == 0) { 641 bp->b_error = 0; 642 bp->b_resid = xs->resid; 643 break; 644 } 645 if (error != ERESTART) 646 xs->retries = 0; 647 goto retry; 648 649 case XS_BUSY: 650 if (xs->retries) { 651 if (scsi_delay(xs, 1) != ERESTART) 652 xs->retries = 0; 653 } 654 goto retry; 655 656 case XS_TIMEOUT: 657 retry: 658 if (xs->retries--) { 659 scsi_xs_exec(xs); 660 return; 661 } 662 /* FALLTHROUGH */ 663 664 default: 665 bp->b_error = EIO; 666 bp->b_flags |= B_ERROR; 667 bp->b_resid = bp->b_bcount; 668 break; 669 } 670 671 disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid, 672 bp->b_flags & B_READ); 673 674 s = splbio(); 675 biodone(bp); 676 splx(s); 677 scsi_xs_put(xs); 678 } 679 680 void 681 cdminphys(struct buf *bp) 682 { 683 struct cd_softc *sc; 684 long max; 685 686 sc = cdlookup(DISKUNIT(bp->b_dev)); 687 if (sc == NULL) 688 return; 689 690 /* 691 * If the device is ancient, we want to make sure that 692 * the transfer fits into a 6-byte cdb. 693 * 694 * XXX Note that the SCSI-I spec says that 256-block transfers 695 * are allowed in a 6-byte read/write, and are specified 696 * by setting the "length" to 0. However, we're conservative 697 * here, allowing only 255-block transfers in case an 698 * ancient device gets confused by length == 0. A length of 0 699 * in a 10-byte read/write actually means 0 blocks. 700 */ 701 if (sc->sc_flags & CDF_ANCIENT) { 702 max = sc->sc_dk.dk_label->d_secsize * 0xff; 703 704 if (bp->b_bcount > max) 705 bp->b_bcount = max; 706 } 707 708 (*sc->sc_link->adapter->scsi_minphys)(bp, sc->sc_link); 709 710 device_unref(&sc->sc_dev); 711 } 712 713 int 714 cdread(dev_t dev, struct uio *uio, int ioflag) 715 { 716 717 return (physio(cdstrategy, dev, B_READ, cdminphys, uio)); 718 } 719 720 int 721 cdwrite(dev_t dev, struct uio *uio, int ioflag) 722 { 723 724 return (physio(cdstrategy, dev, B_WRITE, cdminphys, uio)); 725 } 726 727 /* 728 * Perform special action on behalf of the user. 729 * Knows about the internals of this device 730 */ 731 int 732 cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 733 { 734 struct cd_softc *sc; 735 struct disklabel *lp; 736 int part = DISKPART(dev); 737 int error = 0; 738 739 sc = cdlookup(DISKUNIT(dev)); 740 if (sc == NULL) 741 return ENXIO; 742 if (sc->sc_flags & CDF_DYING) { 743 device_unref(&sc->sc_dev); 744 return (ENXIO); 745 } 746 747 SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdioctl 0x%lx\n", cmd)); 748 749 /* 750 * If the device is not valid.. abandon ship 751 */ 752 if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 753 switch (cmd) { 754 case DIOCLOCK: 755 case DIOCEJECT: 756 case SCIOCIDENTIFY: 757 case SCIOCCOMMAND: 758 case SCIOCDEBUG: 759 case CDIOCLOADUNLOAD: 760 case SCIOCRESET: 761 case CDIOCGETVOL: 762 case CDIOCSETVOL: 763 case CDIOCSETMONO: 764 case CDIOCSETSTEREO: 765 case CDIOCSETMUTE: 766 case CDIOCSETLEFT: 767 case CDIOCSETRIGHT: 768 case CDIOCCLOSE: 769 case CDIOCEJECT: 770 case CDIOCALLOW: 771 case CDIOCPREVENT: 772 case CDIOCSETDEBUG: 773 case CDIOCCLRDEBUG: 774 case CDIOCRESET: 775 case DVD_AUTH: 776 case DVD_READ_STRUCT: 777 case MTIOCTOP: 778 if (part == RAW_PART) 779 break; 780 /* FALLTHROUGH */ 781 default: 782 if ((sc->sc_link->flags & SDEV_OPEN) == 0) 783 error = ENODEV; 784 else 785 error = EIO; 786 goto exit; 787 } 788 } 789 790 switch (cmd) { 791 case DIOCRLDINFO: 792 lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK); 793 cdgetdisklabel(dev, sc, lp, 0); 794 bcopy(lp, sc->sc_dk.dk_label, sizeof(*lp)); 795 free(lp, M_TEMP); 796 break; 797 798 case DIOCGPDINFO: 799 cdgetdisklabel(dev, sc, (struct disklabel *)addr, 1); 800 break; 801 802 case DIOCGDINFO: 803 *(struct disklabel *)addr = *(sc->sc_dk.dk_label); 804 break; 805 806 case DIOCGPART: 807 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label; 808 ((struct partinfo *)addr)->part = 809 &sc->sc_dk.dk_label->d_partitions[DISKPART(dev)]; 810 break; 811 812 case DIOCWDINFO: 813 case DIOCSDINFO: 814 if ((flag & FWRITE) == 0) { 815 error = EBADF; 816 break; 817 } 818 819 if ((error = disk_lock(&sc->sc_dk)) != 0) 820 break; 821 822 error = setdisklabel(sc->sc_dk.dk_label, 823 (struct disklabel *)addr, sc->sc_dk.dk_openmask); 824 if (error == 0) { 825 } 826 827 disk_unlock(&sc->sc_dk); 828 break; 829 830 case CDIOCPLAYTRACKS: { 831 struct ioc_play_track *args = (struct ioc_play_track *)addr; 832 833 if ((error = cd_set_pa_immed(sc, 0)) != 0) 834 break; 835 error = cd_play_tracks(sc, args->start_track, 836 args->start_index, args->end_track, args->end_index); 837 break; 838 } 839 case CDIOCPLAYMSF: { 840 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 841 842 if ((error = cd_set_pa_immed(sc, 0)) != 0) 843 break; 844 error = cd_play_msf(sc, args->start_m, args->start_s, 845 args->start_f, args->end_m, args->end_s, args->end_f); 846 break; 847 } 848 case CDIOCPLAYBLOCKS: { 849 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 850 851 if ((error = cd_set_pa_immed(sc, 0)) != 0) 852 break; 853 error = cd_play(sc, args->blk, args->len); 854 break; 855 } 856 case CDIOCREADSUBCHANNEL: { 857 struct ioc_read_subchannel *args = 858 (struct ioc_read_subchannel *)addr; 859 struct cd_sub_channel_info *data; 860 int len = args->data_len; 861 862 if (len > sizeof(*data) || 863 len < sizeof(struct cd_sub_channel_header)) { 864 error = EINVAL; 865 break; 866 } 867 data = dma_alloc(sizeof(*data), PR_WAITOK); 868 error = cd_read_subchannel(sc, args->address_format, 869 args->data_format, args->track, data, len); 870 if (error) { 871 dma_free(data, sizeof(*data)); 872 break; 873 } 874 len = min(len, _2btol(data->header.data_len) + 875 sizeof(struct cd_sub_channel_header)); 876 error = copyout(data, args->data, len); 877 dma_free(data, sizeof(*data)); 878 break; 879 } 880 case CDIOREADTOCHEADER: { 881 struct ioc_toc_header *th; 882 883 th = dma_alloc(sizeof(*th), PR_WAITOK); 884 if ((error = cd_read_toc(sc, 0, 0, th, sizeof(*th), 0)) != 0) { 885 dma_free(th, sizeof(*th)); 886 break; 887 } 888 if (sc->sc_link->quirks & ADEV_LITTLETOC) 889 th->len = letoh16(th->len); 890 else 891 th->len = betoh16(th->len); 892 if (th->len > 0) 893 bcopy(th, addr, sizeof(*th)); 894 else 895 error = EIO; 896 dma_free(th, sizeof(*th)); 897 break; 898 } 899 case CDIOREADTOCENTRYS: { 900 struct cd_toc *toc; 901 struct ioc_read_toc_entry *te = 902 (struct ioc_read_toc_entry *)addr; 903 struct ioc_toc_header *th; 904 struct cd_toc_entry *cte; 905 int len = te->data_len; 906 int ntracks; 907 908 toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO); 909 910 th = &toc->header; 911 912 if (len > sizeof(toc->entries) || 913 len < sizeof(struct cd_toc_entry)) { 914 dma_free(toc, sizeof(*toc)); 915 error = EINVAL; 916 break; 917 } 918 error = cd_read_toc(sc, te->address_format, te->starting_track, 919 toc, len + sizeof(struct ioc_toc_header), 0); 920 if (error) { 921 dma_free(toc, sizeof(*toc)); 922 break; 923 } 924 if (te->address_format == CD_LBA_FORMAT) 925 for (ntracks = 926 th->ending_track - th->starting_track + 1; 927 ntracks >= 0; ntracks--) { 928 cte = &toc->entries[ntracks]; 929 cte->addr_type = CD_LBA_FORMAT; 930 if (sc->sc_link->quirks & ADEV_LITTLETOC) { 931 #if BYTE_ORDER == BIG_ENDIAN 932 swap16_multi((u_int16_t *)&cte->addr, 933 sizeof(cte->addr) / 2); 934 #endif 935 } else 936 cte->addr.lba = betoh32(cte->addr.lba); 937 } 938 if (sc->sc_link->quirks & ADEV_LITTLETOC) { 939 th->len = letoh16(th->len); 940 } else 941 th->len = betoh16(th->len); 942 len = min(len, th->len - (sizeof(th->starting_track) + 943 sizeof(th->ending_track))); 944 945 error = copyout(toc->entries, te->data, len); 946 dma_free(toc, sizeof(*toc)); 947 break; 948 } 949 case CDIOREADMSADDR: { 950 struct cd_toc *toc; 951 int sessno = *(int *)addr; 952 struct cd_toc_entry *cte; 953 954 if (sessno != 0) { 955 error = EINVAL; 956 break; 957 } 958 959 toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO); 960 961 error = cd_read_toc(sc, 0, 0, toc, 962 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 963 0x40 /* control word for "get MS info" */); 964 965 if (error) { 966 dma_free(toc, sizeof(*toc)); 967 break; 968 } 969 970 cte = &toc->entries[0]; 971 if (sc->sc_link->quirks & ADEV_LITTLETOC) { 972 #if BYTE_ORDER == BIG_ENDIAN 973 swap16_multi((u_int16_t *)&cte->addr, 974 sizeof(cte->addr) / 2); 975 #endif 976 } else 977 cte->addr.lba = betoh32(cte->addr.lba); 978 if (sc->sc_link->quirks & ADEV_LITTLETOC) 979 toc->header.len = letoh16(toc->header.len); 980 else 981 toc->header.len = betoh16(toc->header.len); 982 983 *(int *)addr = (toc->header.len >= 10 && cte->track > 1) ? 984 cte->addr.lba : 0; 985 dma_free(toc, sizeof(*toc)); 986 break; 987 } 988 case CDIOCSETPATCH: { 989 struct ioc_patch *arg = (struct ioc_patch *)addr; 990 991 error = cd_setchan(sc, arg->patch[0], arg->patch[1], 992 arg->patch[2], arg->patch[3], 0); 993 break; 994 } 995 case CDIOCGETVOL: { 996 struct ioc_vol *arg = (struct ioc_vol *)addr; 997 998 error = cd_getvol(sc, arg, 0); 999 break; 1000 } 1001 case CDIOCSETVOL: { 1002 struct ioc_vol *arg = (struct ioc_vol *)addr; 1003 1004 error = cd_setvol(sc, arg, 0); 1005 break; 1006 } 1007 1008 case CDIOCSETMONO: 1009 error = cd_setchan(sc, BOTH_CHANNEL, BOTH_CHANNEL, MUTE_CHANNEL, 1010 MUTE_CHANNEL, 0); 1011 break; 1012 1013 case CDIOCSETSTEREO: 1014 error = cd_setchan(sc, LEFT_CHANNEL, RIGHT_CHANNEL, 1015 MUTE_CHANNEL, MUTE_CHANNEL, 0); 1016 break; 1017 1018 case CDIOCSETMUTE: 1019 error = cd_setchan(sc, MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 1020 MUTE_CHANNEL, 0); 1021 break; 1022 1023 case CDIOCSETLEFT: 1024 error = cd_setchan(sc, LEFT_CHANNEL, LEFT_CHANNEL, MUTE_CHANNEL, 1025 MUTE_CHANNEL, 0); 1026 break; 1027 1028 case CDIOCSETRIGHT: 1029 error = cd_setchan(sc, RIGHT_CHANNEL, RIGHT_CHANNEL, 1030 MUTE_CHANNEL, MUTE_CHANNEL, 0); 1031 break; 1032 1033 case CDIOCRESUME: 1034 error = cd_pause(sc, 1); 1035 break; 1036 1037 case CDIOCPAUSE: 1038 error = cd_pause(sc, 0); 1039 break; 1040 case CDIOCSTART: 1041 error = scsi_start(sc->sc_link, SSS_START, 0); 1042 break; 1043 1044 case CDIOCSTOP: 1045 error = scsi_start(sc->sc_link, SSS_STOP, 0); 1046 break; 1047 1048 close_tray: 1049 case CDIOCCLOSE: 1050 error = scsi_start(sc->sc_link, SSS_START|SSS_LOEJ, 1051 SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE); 1052 break; 1053 1054 case MTIOCTOP: 1055 if (((struct mtop *)addr)->mt_op == MTRETEN) 1056 goto close_tray; 1057 if (((struct mtop *)addr)->mt_op != MTOFFL) { 1058 error = EIO; 1059 break; 1060 } 1061 /* FALLTHROUGH */ 1062 case CDIOCEJECT: /* FALLTHROUGH */ 1063 case DIOCEJECT: 1064 sc->sc_link->flags |= SDEV_EJECTING; 1065 break; 1066 case CDIOCALLOW: 1067 error = scsi_prevent(sc->sc_link, PR_ALLOW, 0); 1068 break; 1069 case CDIOCPREVENT: 1070 error = scsi_prevent(sc->sc_link, PR_PREVENT, 0); 1071 break; 1072 case DIOCLOCK: 1073 error = scsi_prevent(sc->sc_link, 1074 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0); 1075 break; 1076 case CDIOCSETDEBUG: 1077 sc->sc_link->flags |= (SDEV_DB1 | SDEV_DB2); 1078 break; 1079 case CDIOCCLRDEBUG: 1080 sc->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); 1081 break; 1082 case CDIOCRESET: 1083 case SCIOCRESET: 1084 error = cd_reset(sc); 1085 break; 1086 case CDIOCLOADUNLOAD: { 1087 struct ioc_load_unload *args = (struct ioc_load_unload *)addr; 1088 1089 error = cd_load_unload(sc, args->options, args->slot); 1090 break; 1091 } 1092 1093 case DVD_AUTH: 1094 error = dvd_auth(sc, (union dvd_authinfo *)addr); 1095 break; 1096 case DVD_READ_STRUCT: 1097 error = dvd_read_struct(sc, (union dvd_struct *)addr); 1098 break; 1099 default: 1100 if (DISKPART(dev) != RAW_PART) { 1101 error = ENOTTY; 1102 break; 1103 } 1104 error = scsi_do_ioctl(sc->sc_link, cmd, addr, flag); 1105 break; 1106 } 1107 1108 exit: 1109 1110 device_unref(&sc->sc_dev); 1111 return (error); 1112 } 1113 1114 /* 1115 * Load the label information on the named device 1116 * Actually fabricate a disklabel 1117 * 1118 * EVENTUALLY take information about different 1119 * data tracks from the TOC and put it in the disklabel 1120 */ 1121 int 1122 cdgetdisklabel(dev_t dev, struct cd_softc *sc, struct disklabel *lp, 1123 int spoofonly) 1124 { 1125 struct cd_toc *toc; 1126 int tocidx, n, audioonly = 1; 1127 1128 bzero(lp, sizeof(struct disklabel)); 1129 1130 lp->d_secsize = sc->params.secsize; 1131 lp->d_ntracks = 1; 1132 lp->d_nsectors = 100; 1133 lp->d_secpercyl = 100; 1134 lp->d_ncylinders = (sc->params.disksize / 100) + 1; 1135 1136 if (sc->sc_link->flags & SDEV_ATAPI) { 1137 strncpy(lp->d_typename, "ATAPI CD-ROM", sizeof(lp->d_typename)); 1138 lp->d_type = DTYPE_ATAPI; 1139 } else { 1140 strncpy(lp->d_typename, "SCSI CD-ROM", sizeof(lp->d_typename)); 1141 lp->d_type = DTYPE_SCSI; 1142 } 1143 1144 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); 1145 DL_SETDSIZE(lp, sc->params.disksize); 1146 lp->d_version = 1; 1147 1148 /* XXX - these values for BBSIZE and SBSIZE assume ffs */ 1149 lp->d_bbsize = BBSIZE; 1150 lp->d_sbsize = SBSIZE; 1151 1152 lp->d_magic = DISKMAGIC; 1153 lp->d_magic2 = DISKMAGIC; 1154 lp->d_checksum = dkcksum(lp); 1155 1156 toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO); 1157 if (cd_load_toc(sc, toc, CD_LBA_FORMAT)) { 1158 audioonly = 0; /* No valid TOC found == not an audio CD. */ 1159 goto done; 1160 } 1161 1162 n = toc->header.ending_track - toc->header.starting_track + 1; 1163 for (tocidx = 0; tocidx < n; tocidx++) 1164 if (toc->entries[tocidx].control & 4) { 1165 audioonly = 0; /* Found a non-audio track. */ 1166 goto done; 1167 } 1168 1169 done: 1170 dma_free(toc, sizeof(*toc)); 1171 1172 if (audioonly) 1173 return (0); 1174 return readdisklabel(DISKLABELDEV(dev), cdstrategy, lp, spoofonly); 1175 } 1176 1177 int 1178 cd_setchan(struct cd_softc *sc, int p0, int p1, int p2, int p3, int flags) 1179 { 1180 union scsi_mode_sense_buf *data; 1181 struct cd_audio_page *audio = NULL; 1182 int error, big; 1183 1184 data = dma_alloc(sizeof(*data), PR_NOWAIT); 1185 if (data == NULL) 1186 return (ENOMEM); 1187 1188 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data, 1189 (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big); 1190 if (error == 0 && audio == NULL) 1191 error = EIO; 1192 1193 if (error == 0) { 1194 audio->port[LEFT_PORT].channels = p0; 1195 audio->port[RIGHT_PORT].channels = p1; 1196 audio->port[2].channels = p2; 1197 audio->port[3].channels = p3; 1198 if (big) 1199 error = scsi_mode_select_big(sc->sc_link, SMS_PF, 1200 &data->hdr_big, flags, 20000); 1201 else 1202 error = scsi_mode_select(sc->sc_link, SMS_PF, 1203 &data->hdr, flags, 20000); 1204 } 1205 1206 dma_free(data, sizeof(*data)); 1207 return (error); 1208 } 1209 1210 int 1211 cd_getvol(struct cd_softc *sc, struct ioc_vol *arg, int flags) 1212 { 1213 union scsi_mode_sense_buf *data; 1214 struct cd_audio_page *audio = NULL; 1215 int error; 1216 1217 data = dma_alloc(sizeof(*data), PR_NOWAIT); 1218 if (data == NULL) 1219 return (ENOMEM); 1220 1221 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data, 1222 (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, NULL); 1223 if (error == 0 && audio == NULL) 1224 error = EIO; 1225 1226 if (error == 0) { 1227 arg->vol[0] = audio->port[0].volume; 1228 arg->vol[1] = audio->port[1].volume; 1229 arg->vol[2] = audio->port[2].volume; 1230 arg->vol[3] = audio->port[3].volume; 1231 } 1232 1233 dma_free(data, sizeof(*data)); 1234 return (0); 1235 } 1236 1237 int 1238 cd_setvol(struct cd_softc *sc, const struct ioc_vol *arg, int flags) 1239 { 1240 union scsi_mode_sense_buf *data; 1241 struct cd_audio_page *audio = NULL; 1242 u_int8_t mask_volume[4]; 1243 int error, big; 1244 1245 data = dma_alloc(sizeof(*data), PR_NOWAIT); 1246 if (data == NULL) 1247 return (ENOMEM); 1248 1249 error = scsi_do_mode_sense(sc->sc_link, 1250 AUDIO_PAGE | SMS_PAGE_CTRL_CHANGEABLE, data, (void **)&audio, NULL, 1251 NULL, NULL, sizeof(*audio), flags, NULL); 1252 if (error == 0 && audio == NULL) 1253 error = EIO; 1254 if (error != 0) { 1255 dma_free(data, sizeof(*data)); 1256 return (error); 1257 } 1258 1259 mask_volume[0] = audio->port[0].volume; 1260 mask_volume[1] = audio->port[1].volume; 1261 mask_volume[2] = audio->port[2].volume; 1262 mask_volume[3] = audio->port[3].volume; 1263 1264 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data, 1265 (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big); 1266 if (error == 0 && audio == NULL) 1267 error = EIO; 1268 if (error != 0) { 1269 dma_free(data, sizeof(*data)); 1270 return (error); 1271 } 1272 1273 audio->port[0].volume = arg->vol[0] & mask_volume[0]; 1274 audio->port[1].volume = arg->vol[1] & mask_volume[1]; 1275 audio->port[2].volume = arg->vol[2] & mask_volume[2]; 1276 audio->port[3].volume = arg->vol[3] & mask_volume[3]; 1277 1278 if (big) 1279 error = scsi_mode_select_big(sc->sc_link, SMS_PF, 1280 &data->hdr_big, flags, 20000); 1281 else 1282 error = scsi_mode_select(sc->sc_link, SMS_PF, 1283 &data->hdr, flags, 20000); 1284 1285 dma_free(data, sizeof(*data)); 1286 return (error); 1287 } 1288 1289 int 1290 cd_load_unload(struct cd_softc *sc, int options, int slot) 1291 { 1292 struct scsi_load_unload *cmd; 1293 struct scsi_xfer *xs; 1294 int error; 1295 1296 xs = scsi_xs_get(sc->sc_link, 0); 1297 if (xs == NULL) 1298 return (ENOMEM); 1299 xs->cmdlen = sizeof(*cmd); 1300 xs->timeout = 200000; 1301 1302 cmd = (struct scsi_load_unload *)xs->cmd; 1303 cmd->opcode = LOAD_UNLOAD; 1304 cmd->options = options; /* ioctl uses ATAPI values */ 1305 cmd->slot = slot; 1306 1307 error = scsi_xs_sync(xs); 1308 scsi_xs_put(xs); 1309 1310 return (error); 1311 } 1312 1313 int 1314 cd_set_pa_immed(struct cd_softc *sc, int flags) 1315 { 1316 union scsi_mode_sense_buf *data; 1317 struct cd_audio_page *audio = NULL; 1318 int error, oflags, big; 1319 1320 if (sc->sc_link->flags & SDEV_ATAPI) 1321 /* XXX Noop? */ 1322 return (0); 1323 1324 data = dma_alloc(sizeof(*data), PR_NOWAIT); 1325 if (data == NULL) 1326 return (ENOMEM); 1327 1328 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data, 1329 (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big); 1330 if (error == 0 && audio == NULL) 1331 error = EIO; 1332 1333 if (error == 0) { 1334 oflags = audio->flags; 1335 audio->flags &= ~CD_PA_SOTC; 1336 audio->flags |= CD_PA_IMMED; 1337 if (audio->flags != oflags) { 1338 if (big) 1339 error = scsi_mode_select_big(sc->sc_link, 1340 SMS_PF, &data->hdr_big, flags, 20000); 1341 else 1342 error = scsi_mode_select(sc->sc_link, SMS_PF, 1343 &data->hdr, flags, 20000); 1344 } 1345 } 1346 1347 dma_free(data, sizeof(*data)); 1348 return (error); 1349 } 1350 1351 /* 1352 * Get scsi driver to send a "start playing" command 1353 */ 1354 int 1355 cd_play(struct cd_softc *sc, int secno, int nsecs) 1356 { 1357 struct scsi_play *cmd; 1358 struct scsi_xfer *xs; 1359 int error; 1360 1361 xs = scsi_xs_get(sc->sc_link, 0); 1362 if (xs == NULL) 1363 return (ENOMEM); 1364 xs->cmdlen = sizeof(*cmd); 1365 xs->timeout = 200000; 1366 1367 cmd = (struct scsi_play *)xs->cmd; 1368 cmd->opcode = PLAY; 1369 _lto4b(secno, cmd->blk_addr); 1370 _lto2b(nsecs, cmd->xfer_len); 1371 1372 error = scsi_xs_sync(xs); 1373 scsi_xs_put(xs); 1374 1375 return (error); 1376 } 1377 1378 /* 1379 * Get scsi driver to send a "start playing" command 1380 */ 1381 int 1382 cd_play_tracks(struct cd_softc *sc, int strack, int sindex, int etrack, 1383 int eindex) 1384 { 1385 struct cd_toc *toc; 1386 u_char endf, ends, endm; 1387 int error; 1388 1389 if (!etrack) 1390 return (EIO); 1391 if (strack > etrack) 1392 return (EINVAL); 1393 1394 toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO); 1395 1396 if ((error = cd_load_toc(sc, toc, CD_MSF_FORMAT)) != 0) 1397 goto done; 1398 1399 if (++etrack > (toc->header.ending_track+1)) 1400 etrack = toc->header.ending_track+1; 1401 1402 strack -= toc->header.starting_track; 1403 etrack -= toc->header.starting_track; 1404 if (strack < 0) { 1405 error = EINVAL; 1406 goto done; 1407 } 1408 1409 /* 1410 * The track ends one frame before the next begins. The last track 1411 * is taken care of by the leadoff track. 1412 */ 1413 endm = toc->entries[etrack].addr.msf.minute; 1414 ends = toc->entries[etrack].addr.msf.second; 1415 endf = toc->entries[etrack].addr.msf.frame; 1416 if (endf-- == 0) { 1417 endf = CD_FRAMES - 1; 1418 if (ends-- == 0) { 1419 ends = CD_SECS - 1; 1420 if (endm-- == 0) { 1421 error = EINVAL; 1422 goto done; 1423 } 1424 } 1425 } 1426 1427 error = cd_play_msf(sc, toc->entries[strack].addr.msf.minute, 1428 toc->entries[strack].addr.msf.second, 1429 toc->entries[strack].addr.msf.frame, 1430 endm, ends, endf); 1431 1432 done: 1433 dma_free(toc, sizeof(*toc)); 1434 return (error); 1435 } 1436 1437 /* 1438 * Get scsi driver to send a "play msf" command 1439 */ 1440 int 1441 cd_play_msf(struct cd_softc *sc, int startm, int starts, int startf, int endm, 1442 int ends, int endf) 1443 { 1444 struct scsi_play_msf *cmd; 1445 struct scsi_xfer *xs; 1446 int error; 1447 1448 xs = scsi_xs_get(sc->sc_link, 0); 1449 if (xs == NULL) 1450 return (ENOMEM); 1451 xs->cmdlen = sizeof(*cmd); 1452 xs->timeout = 20000; 1453 1454 cmd = (struct scsi_play_msf *)xs->cmd; 1455 cmd->opcode = PLAY_MSF; 1456 cmd->start_m = startm; 1457 cmd->start_s = starts; 1458 cmd->start_f = startf; 1459 cmd->end_m = endm; 1460 cmd->end_s = ends; 1461 cmd->end_f = endf; 1462 1463 error = scsi_xs_sync(xs); 1464 scsi_xs_put(xs); 1465 1466 return (error); 1467 } 1468 1469 /* 1470 * Get scsi driver to send a "start up" command 1471 */ 1472 int 1473 cd_pause(struct cd_softc *sc, int go) 1474 { 1475 struct scsi_pause *cmd; 1476 struct scsi_xfer *xs; 1477 int error; 1478 1479 xs = scsi_xs_get(sc->sc_link, 0); 1480 if (xs == NULL) 1481 return (ENOMEM); 1482 xs->cmdlen = sizeof(*cmd); 1483 xs->timeout = 2000; 1484 1485 cmd = (struct scsi_pause *)xs->cmd; 1486 cmd->opcode = PAUSE; 1487 cmd->resume = go; 1488 1489 error = scsi_xs_sync(xs); 1490 scsi_xs_put(xs); 1491 1492 return (error); 1493 } 1494 1495 /* 1496 * Get scsi driver to send a "RESET" command 1497 */ 1498 int 1499 cd_reset(struct cd_softc *sc) 1500 { 1501 struct scsi_xfer *xs; 1502 int error; 1503 1504 xs = scsi_xs_get(sc->sc_link, SCSI_RESET); 1505 if (xs == NULL) 1506 return (ENOMEM); 1507 1508 xs->timeout = 2000; 1509 1510 error = scsi_xs_sync(xs); 1511 scsi_xs_put(xs); 1512 1513 return (error); 1514 } 1515 1516 /* 1517 * Read subchannel 1518 */ 1519 int 1520 cd_read_subchannel(struct cd_softc *sc, int mode, int format, int track, 1521 struct cd_sub_channel_info *data, int len) 1522 { 1523 struct scsi_read_subchannel *cmd; 1524 struct scsi_xfer *xs; 1525 int error; 1526 1527 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN | SCSI_SILENT); 1528 if (xs == NULL) 1529 return (ENOMEM); 1530 xs->cmdlen = sizeof(*cmd); 1531 xs->data = (void *)data; 1532 xs->datalen = len; 1533 xs->timeout = 5000; 1534 1535 cmd = (struct scsi_read_subchannel *)xs->cmd; 1536 cmd->opcode = READ_SUBCHANNEL; 1537 if (mode == CD_MSF_FORMAT) 1538 cmd->byte2 |= CD_MSF; 1539 cmd->byte3 = SRS_SUBQ; 1540 cmd->subchan_format = format; 1541 cmd->track = track; 1542 _lto2b(len, cmd->data_len); 1543 1544 error = scsi_xs_sync(xs); 1545 scsi_xs_put(xs); 1546 1547 return (error); 1548 } 1549 1550 /* 1551 * Read table of contents 1552 */ 1553 int 1554 cd_read_toc(struct cd_softc *sc, int mode, int start, void *data, int len, 1555 int control) 1556 { 1557 struct scsi_read_toc *cmd; 1558 struct scsi_xfer *xs; 1559 int error; 1560 1561 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN | 1562 SCSI_IGNORE_ILLEGAL_REQUEST); 1563 if (xs == NULL) 1564 return (ENOMEM); 1565 xs->cmdlen = sizeof(*cmd); 1566 xs->data = data; 1567 xs->datalen = len; 1568 xs->timeout = 5000; 1569 1570 bzero(data, len); 1571 1572 cmd = (struct scsi_read_toc *)xs->cmd; 1573 cmd->opcode = READ_TOC; 1574 1575 if (mode == CD_MSF_FORMAT) 1576 cmd->byte2 |= CD_MSF; 1577 cmd->from_track = start; 1578 _lto2b(len, cmd->data_len); 1579 cmd->control = control; 1580 1581 error = scsi_xs_sync(xs); 1582 scsi_xs_put(xs); 1583 1584 return (error); 1585 } 1586 1587 int 1588 cd_load_toc(struct cd_softc *sc, struct cd_toc *toc, int fmt) 1589 { 1590 int n, len, error; 1591 1592 error = cd_read_toc(sc, 0, 0, toc, sizeof(toc->header), 0); 1593 1594 if (error == 0) { 1595 if (toc->header.ending_track < toc->header.starting_track) 1596 return (EIO); 1597 /* +2 to account for leading out track. */ 1598 n = toc->header.ending_track - toc->header.starting_track + 2; 1599 len = n * sizeof(struct cd_toc_entry) + sizeof(toc->header); 1600 error = cd_read_toc(sc, fmt, 0, toc, len, 0); 1601 } 1602 1603 return (error); 1604 } 1605 1606 1607 /* 1608 * Get the scsi driver to send a full inquiry to the device and use the 1609 * results to fill out the disk parameter structure. 1610 */ 1611 int 1612 cd_get_parms(struct cd_softc *sc, int flags) 1613 { 1614 /* Reasonable defaults for drives that don't support READ_CAPACITY */ 1615 sc->params.secsize = 2048; 1616 sc->params.disksize = 400000; 1617 1618 if (sc->sc_link->quirks & ADEV_NOCAPACITY) 1619 return (0); 1620 1621 sc->params.disksize = scsi_size(sc->sc_link, flags, 1622 &sc->params.secsize); 1623 1624 if ((sc->params.secsize < 512) || 1625 ((sc->params.secsize & 511) != 0)) 1626 sc->params.secsize = 2048; /* some drives lie ! */ 1627 1628 if (sc->params.disksize < 100) 1629 sc->params.disksize = 400000; 1630 1631 return (0); 1632 } 1633 1634 daddr64_t 1635 cdsize(dev_t dev) 1636 { 1637 1638 /* CD-ROMs are read-only. */ 1639 return -1; 1640 } 1641 1642 int 1643 cddump(dev_t dev, daddr64_t secno, caddr_t va, size_t size) 1644 { 1645 /* Not implemented. */ 1646 return ENXIO; 1647 } 1648 1649 #define dvd_copy_key(dst, src) bcopy((src), (dst), DVD_KEY_SIZE) 1650 #define dvd_copy_challenge(dst, src) bcopy((src), (dst), DVD_CHALLENGE_SIZE) 1651 1652 #define DVD_AUTH_BUFSIZE 20 1653 1654 int 1655 dvd_auth(struct cd_softc *sc, union dvd_authinfo *a) 1656 { 1657 struct scsi_generic *cmd; 1658 struct scsi_xfer *xs; 1659 u_int8_t *buf; 1660 int error; 1661 1662 buf = dma_alloc(DVD_AUTH_BUFSIZE, PR_WAITOK | PR_ZERO); 1663 if (buf == NULL) 1664 return (ENOMEM); 1665 1666 xs = scsi_xs_get(sc->sc_link, 0); 1667 if (xs == NULL) { 1668 error = ENOMEM; 1669 goto done; 1670 } 1671 xs->cmdlen = sizeof(*cmd); 1672 xs->timeout = 30000; 1673 xs->data = buf; 1674 1675 cmd = xs->cmd; 1676 1677 switch (a->type) { 1678 case DVD_LU_SEND_AGID: 1679 cmd->opcode = GPCMD_REPORT_KEY; 1680 cmd->bytes[8] = 8; 1681 cmd->bytes[9] = 0 | (0 << 6); 1682 xs->datalen = 8; 1683 xs->flags |= SCSI_DATA_IN; 1684 1685 error = scsi_xs_sync(xs); 1686 scsi_xs_put(xs); 1687 1688 if (error == 0) 1689 a->lsa.agid = buf[7] >> 6; 1690 break; 1691 1692 case DVD_LU_SEND_CHALLENGE: 1693 cmd->opcode = GPCMD_REPORT_KEY; 1694 cmd->bytes[8] = 16; 1695 cmd->bytes[9] = 1 | (a->lsc.agid << 6); 1696 xs->datalen = 16; 1697 xs->flags |= SCSI_DATA_IN; 1698 1699 error = scsi_xs_sync(xs); 1700 scsi_xs_put(xs); 1701 if (error == 0) 1702 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1703 break; 1704 1705 case DVD_LU_SEND_KEY1: 1706 cmd->opcode = GPCMD_REPORT_KEY; 1707 cmd->bytes[8] = 12; 1708 cmd->bytes[9] = 2 | (a->lsk.agid << 6); 1709 xs->datalen = 12; 1710 xs->flags |= SCSI_DATA_IN; 1711 1712 error = scsi_xs_sync(xs); 1713 scsi_xs_put(xs); 1714 1715 if (error == 0) 1716 dvd_copy_key(a->lsk.key, &buf[4]); 1717 break; 1718 1719 case DVD_LU_SEND_TITLE_KEY: 1720 cmd->opcode = GPCMD_REPORT_KEY; 1721 _lto4b(a->lstk.lba, &cmd->bytes[1]); 1722 cmd->bytes[8] = 12; 1723 cmd->bytes[9] = 4 | (a->lstk.agid << 6); 1724 xs->datalen = 12; 1725 xs->flags |= SCSI_DATA_IN; 1726 1727 error = scsi_xs_sync(xs); 1728 scsi_xs_put(xs); 1729 1730 if (error == 0) { 1731 a->lstk.cpm = (buf[4] >> 7) & 1; 1732 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1733 a->lstk.cgms = (buf[4] >> 4) & 3; 1734 dvd_copy_key(a->lstk.title_key, &buf[5]); 1735 } 1736 break; 1737 1738 case DVD_LU_SEND_ASF: 1739 cmd->opcode = GPCMD_REPORT_KEY; 1740 cmd->bytes[8] = 8; 1741 cmd->bytes[9] = 5 | (a->lsasf.agid << 6); 1742 xs->datalen = 8; 1743 xs->flags |= SCSI_DATA_IN; 1744 1745 error = scsi_xs_sync(xs); 1746 scsi_xs_put(xs); 1747 1748 if (error == 0) 1749 a->lsasf.asf = buf[7] & 1; 1750 break; 1751 1752 case DVD_HOST_SEND_CHALLENGE: 1753 cmd->opcode = GPCMD_SEND_KEY; 1754 cmd->bytes[8] = 16; 1755 cmd->bytes[9] = 1 | (a->hsc.agid << 6); 1756 buf[1] = 14; 1757 dvd_copy_challenge(&buf[4], a->hsc.chal); 1758 xs->datalen = 16; 1759 xs->flags |= SCSI_DATA_OUT; 1760 1761 error = scsi_xs_sync(xs); 1762 scsi_xs_put(xs); 1763 1764 if (error == 0) 1765 a->type = DVD_LU_SEND_KEY1; 1766 break; 1767 1768 case DVD_HOST_SEND_KEY2: 1769 cmd->opcode = GPCMD_SEND_KEY; 1770 cmd->bytes[8] = 12; 1771 cmd->bytes[9] = 3 | (a->hsk.agid << 6); 1772 buf[1] = 10; 1773 dvd_copy_key(&buf[4], a->hsk.key); 1774 xs->datalen = 12; 1775 xs->flags |= SCSI_DATA_OUT; 1776 1777 error = scsi_xs_sync(xs); 1778 scsi_xs_put(xs); 1779 1780 if (error == 0) 1781 a->type = DVD_AUTH_ESTABLISHED; 1782 else 1783 a->type = DVD_AUTH_FAILURE; 1784 break; 1785 1786 case DVD_INVALIDATE_AGID: 1787 cmd->opcode = GPCMD_REPORT_KEY; 1788 cmd->bytes[9] = 0x3f | (a->lsa.agid << 6); 1789 xs->data = NULL; 1790 1791 error = scsi_xs_sync(xs); 1792 scsi_xs_put(xs); 1793 break; 1794 1795 case DVD_LU_SEND_RPC_STATE: 1796 cmd->opcode = GPCMD_REPORT_KEY; 1797 cmd->bytes[8] = 8; 1798 cmd->bytes[9] = 8 | (0 << 6); 1799 xs->datalen = 8; 1800 xs->flags |= SCSI_DATA_IN; 1801 1802 error = scsi_xs_sync(xs); 1803 scsi_xs_put(xs); 1804 1805 if (error == 0) { 1806 a->lrpcs.type = (buf[4] >> 6) & 3; 1807 a->lrpcs.vra = (buf[4] >> 3) & 7; 1808 a->lrpcs.ucca = (buf[4]) & 7; 1809 a->lrpcs.region_mask = buf[5]; 1810 a->lrpcs.rpc_scheme = buf[6]; 1811 } 1812 break; 1813 1814 case DVD_HOST_SEND_RPC_STATE: 1815 cmd->opcode = GPCMD_SEND_KEY; 1816 cmd->bytes[8] = 8; 1817 cmd->bytes[9] = 6 | (0 << 6); 1818 buf[1] = 6; 1819 buf[4] = a->hrpcs.pdrc; 1820 xs->datalen = 8; 1821 xs->flags |= SCSI_DATA_OUT; 1822 1823 error = scsi_xs_sync(xs); 1824 scsi_xs_put(xs); 1825 break; 1826 1827 default: 1828 scsi_xs_put(xs); 1829 error = ENOTTY; 1830 break; 1831 } 1832 done: 1833 dma_free(buf, DVD_AUTH_BUFSIZE); 1834 return (error); 1835 } 1836 1837 #define DVD_READ_PHYSICAL_BUFSIZE (4 + 4 * 20) 1838 int 1839 dvd_read_physical(struct cd_softc *sc, union dvd_struct *s) 1840 { 1841 struct scsi_generic *cmd; 1842 struct dvd_layer *layer; 1843 struct scsi_xfer *xs; 1844 u_int8_t *buf, *bufp; 1845 int error, i; 1846 1847 buf = dma_alloc(DVD_READ_PHYSICAL_BUFSIZE, PR_WAITOK | PR_ZERO); 1848 if (buf == NULL) 1849 return (ENOMEM); 1850 1851 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1852 if (xs == NULL) { 1853 error = ENOMEM; 1854 goto done; 1855 } 1856 xs->cmdlen = sizeof(*cmd); 1857 xs->data = buf; 1858 xs->datalen = DVD_READ_PHYSICAL_BUFSIZE; 1859 xs->timeout = 30000; 1860 1861 cmd = xs->cmd; 1862 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 1863 cmd->bytes[6] = s->type; 1864 _lto2b(xs->datalen, &cmd->bytes[7]); 1865 1866 cmd->bytes[5] = s->physical.layer_num; 1867 1868 error = scsi_xs_sync(xs); 1869 scsi_xs_put(xs); 1870 1871 if (error == 0) { 1872 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; 1873 i < 4; i++, bufp += 20, layer++) { 1874 bzero(layer, sizeof(*layer)); 1875 layer->book_version = bufp[0] & 0xf; 1876 layer->book_type = bufp[0] >> 4; 1877 layer->min_rate = bufp[1] & 0xf; 1878 layer->disc_size = bufp[1] >> 4; 1879 layer->layer_type = bufp[2] & 0xf; 1880 layer->track_path = (bufp[2] >> 4) & 1; 1881 layer->nlayers = (bufp[2] >> 5) & 3; 1882 layer->track_density = bufp[3] & 0xf; 1883 layer->linear_density = bufp[3] >> 4; 1884 layer->start_sector = _4btol(&bufp[4]); 1885 layer->end_sector = _4btol(&bufp[8]); 1886 layer->end_sector_l0 = _4btol(&bufp[12]); 1887 layer->bca = bufp[16] >> 7; 1888 } 1889 } 1890 done: 1891 dma_free(buf, DVD_READ_PHYSICAL_BUFSIZE); 1892 return (error); 1893 } 1894 1895 #define DVD_READ_COPYRIGHT_BUFSIZE 8 1896 int 1897 dvd_read_copyright(struct cd_softc *sc, union dvd_struct *s) 1898 { 1899 struct scsi_generic *cmd; 1900 struct scsi_xfer *xs; 1901 u_int8_t *buf; 1902 int error; 1903 1904 buf = dma_alloc(DVD_READ_COPYRIGHT_BUFSIZE, PR_WAITOK | PR_ZERO); 1905 if (buf == NULL) 1906 return (ENOMEM); 1907 1908 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1909 if (xs == NULL) { 1910 error = ENOMEM; 1911 goto done; 1912 } 1913 xs->cmdlen = sizeof(*cmd); 1914 xs->data = buf; 1915 xs->datalen = DVD_READ_COPYRIGHT_BUFSIZE; 1916 xs->timeout = 30000; 1917 1918 cmd = xs->cmd; 1919 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 1920 cmd->bytes[6] = s->type; 1921 _lto2b(xs->datalen, &cmd->bytes[7]); 1922 1923 cmd->bytes[5] = s->copyright.layer_num; 1924 1925 error = scsi_xs_sync(xs); 1926 scsi_xs_put(xs); 1927 1928 if (error == 0) { 1929 s->copyright.cpst = buf[4]; 1930 s->copyright.rmi = buf[5]; 1931 } 1932 done: 1933 dma_free(buf, DVD_READ_COPYRIGHT_BUFSIZE); 1934 return (error); 1935 } 1936 1937 int 1938 dvd_read_disckey(struct cd_softc *sc, union dvd_struct *s) 1939 { 1940 struct scsi_read_dvd_structure_data *buf; 1941 struct scsi_read_dvd_structure *cmd; 1942 struct scsi_xfer *xs; 1943 int error; 1944 1945 buf = dma_alloc(sizeof(*buf), PR_WAITOK | PR_ZERO); 1946 if (buf == NULL) 1947 return (ENOMEM); 1948 1949 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1950 if (xs == NULL) { 1951 error = ENOMEM; 1952 goto done; 1953 } 1954 xs->cmdlen = sizeof(*cmd); 1955 xs->data = (void *)buf; 1956 xs->datalen = sizeof(*buf); 1957 xs->timeout = 30000; 1958 1959 cmd = (struct scsi_read_dvd_structure *)xs->cmd; 1960 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 1961 cmd->format = s->type; 1962 cmd->agid = s->disckey.agid << 6; 1963 _lto2b(xs->datalen, cmd->length); 1964 1965 error = scsi_xs_sync(xs); 1966 scsi_xs_put(xs); 1967 1968 if (error == 0) 1969 bcopy(buf->data, s->disckey.value, sizeof(s->disckey.value)); 1970 done: 1971 dma_free(buf, sizeof(*buf)); 1972 return (error); 1973 } 1974 1975 #define DVD_READ_BCA_BUFLEN (4 + 188) 1976 1977 int 1978 dvd_read_bca(struct cd_softc *sc, union dvd_struct *s) 1979 { 1980 struct scsi_generic *cmd; 1981 struct scsi_xfer *xs; 1982 u_int8_t *buf; 1983 int error; 1984 1985 buf = dma_alloc(DVD_READ_BCA_BUFLEN, PR_WAITOK | PR_ZERO); 1986 if (buf == NULL) 1987 return (ENOMEM); 1988 1989 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1990 if (xs == NULL) { 1991 error = ENOMEM; 1992 goto done; 1993 } 1994 xs->cmdlen = sizeof(*cmd); 1995 xs->data = buf; 1996 xs->datalen = DVD_READ_BCA_BUFLEN; 1997 xs->timeout = 30000; 1998 1999 cmd = xs->cmd; 2000 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 2001 cmd->bytes[6] = s->type; 2002 _lto2b(xs->datalen, &cmd->bytes[7]); 2003 2004 error = scsi_xs_sync(xs); 2005 scsi_xs_put(xs); 2006 2007 if (error == 0) { 2008 s->bca.len = _2btol(&buf[0]); 2009 if (s->bca.len < 12 || s->bca.len > 188) 2010 return (EIO); 2011 bcopy(&buf[4], s->bca.value, s->bca.len); 2012 } 2013 done: 2014 dma_free(buf, DVD_READ_BCA_BUFLEN); 2015 return (error); 2016 } 2017 2018 int 2019 dvd_read_manufact(struct cd_softc *sc, union dvd_struct *s) 2020 { 2021 struct scsi_read_dvd_structure_data *buf; 2022 struct scsi_read_dvd_structure *cmd; 2023 struct scsi_xfer *xs; 2024 int error; 2025 2026 buf = dma_alloc(sizeof(*buf), PR_WAITOK | PR_ZERO); 2027 if (buf == NULL) 2028 return (ENOMEM); 2029 2030 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 2031 if (xs == NULL) { 2032 error = ENOMEM; 2033 goto done; 2034 } 2035 xs->cmdlen = sizeof(*cmd); 2036 xs->data = (void *)buf; 2037 xs->datalen = sizeof(*buf); 2038 xs->timeout = 30000; 2039 2040 cmd = (struct scsi_read_dvd_structure *)xs->cmd; 2041 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 2042 cmd->format = s->type; 2043 _lto2b(xs->datalen, cmd->length); 2044 2045 error = scsi_xs_sync(xs); 2046 scsi_xs_put(xs); 2047 2048 if (error == 0) { 2049 s->manufact.len = _2btol(buf->len); 2050 if (s->manufact.len >= 0 && s->manufact.len <= 2048) 2051 bcopy(buf->data, s->manufact.value, s->manufact.len); 2052 else 2053 error = EIO; 2054 } 2055 done: 2056 dma_free(buf, sizeof(*buf)); 2057 return (error); 2058 } 2059 2060 int 2061 dvd_read_struct(struct cd_softc *sc, union dvd_struct *s) 2062 { 2063 2064 switch (s->type) { 2065 case DVD_STRUCT_PHYSICAL: 2066 return (dvd_read_physical(sc, s)); 2067 case DVD_STRUCT_COPYRIGHT: 2068 return (dvd_read_copyright(sc, s)); 2069 case DVD_STRUCT_DISCKEY: 2070 return (dvd_read_disckey(sc, s)); 2071 case DVD_STRUCT_BCA: 2072 return (dvd_read_bca(sc, s)); 2073 case DVD_STRUCT_MANUFACT: 2074 return (dvd_read_manufact(sc, s)); 2075 default: 2076 return (EINVAL); 2077 } 2078 } 2079 2080 int 2081 cd_interpret_sense(struct scsi_xfer *xs) 2082 { 2083 struct scsi_sense_data *sense = &xs->sense; 2084 struct scsi_link *sc_link = xs->sc_link; 2085 u_int8_t skey = sense->flags & SSD_KEY; 2086 u_int8_t serr = sense->error_code & SSD_ERRCODE; 2087 2088 if (((sc_link->flags & SDEV_OPEN) == 0) || 2089 (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)) 2090 return (scsi_interpret_sense(xs)); 2091 2092 /* 2093 * We do custom processing in cd for the unit becoming ready 2094 * case. We do not allow xs->retries to be decremented on the 2095 * "Unit Becoming Ready" case. This is because CD drives 2096 * report "Unit Becoming Ready" when loading media and can 2097 * take a long time. Rather than having a massive timeout for 2098 * all operations (which would cause other problems), we allow 2099 * operations to wait (but be interruptable with Ctrl-C) 2100 * forever as long as the drive is reporting that it is 2101 * becoming ready. All other cases of not being ready are 2102 * handled by the default handler. 2103 */ 2104 switch(skey) { 2105 case SKEY_NOT_READY: 2106 if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0) 2107 return (0); 2108 if (ASC_ASCQ(sense) == SENSE_NOT_READY_BECOMING_READY) { 2109 SC_DEBUG(sc_link, SDEV_DB1, ("not ready: busy (%#x)\n", 2110 sense->add_sense_code_qual)); 2111 /* don't count this as a retry */ 2112 xs->retries++; 2113 return (scsi_delay(xs, 1)); 2114 } 2115 break; 2116 /* XXX more to come here for a few other cases */ 2117 default: 2118 break; 2119 } 2120 return (scsi_interpret_sense(xs)); 2121 } 2122 2123 #if defined(__macppc__) 2124 int 2125 cd_eject(void) 2126 { 2127 struct cd_softc *sc; 2128 int error = 0; 2129 2130 if (cd_cd.cd_ndevs == 0 || (sc = cd_cd.cd_devs[0]) == NULL) 2131 return (ENXIO); 2132 2133 if ((error = disk_lock(&sc->sc_dk)) != 0) 2134 return (error); 2135 2136 if (sc->sc_dk.dk_openmask == 0) { 2137 sc->sc_link->flags |= SDEV_EJECTING; 2138 2139 scsi_prevent(sc->sc_link, PR_ALLOW, 2140 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | 2141 SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE); 2142 sc->sc_link->flags &= ~SDEV_MEDIA_LOADED; 2143 2144 scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0); 2145 2146 sc->sc_link->flags &= ~SDEV_EJECTING; 2147 } 2148 disk_unlock(&sc->sc_dk); 2149 2150 return (error); 2151 } 2152 #endif 2153