1 /* $OpenBSD: cd.c,v 1.214 2014/07/12 18:50:25 tedu 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 u_int64_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 u_int64_t cd_size(struct scsi_link *, int, u_int32_t *); 137 138 int dvd_auth(struct cd_softc *, union dvd_authinfo *); 139 int dvd_read_physical(struct cd_softc *, union dvd_struct *); 140 int dvd_read_copyright(struct cd_softc *, union dvd_struct *); 141 int dvd_read_disckey(struct cd_softc *, union dvd_struct *); 142 int dvd_read_bca(struct cd_softc *, union dvd_struct *); 143 int dvd_read_manufact(struct cd_softc *, union dvd_struct *); 144 int dvd_read_struct(struct cd_softc *, union dvd_struct *); 145 146 #if defined(__macppc__) 147 int cd_eject(void); 148 #endif 149 150 struct cfattach cd_ca = { 151 sizeof(struct cd_softc), cdmatch, cdattach, 152 cddetach, cdactivate 153 }; 154 155 struct cfdriver cd_cd = { 156 NULL, "cd", DV_DISK 157 }; 158 159 const struct scsi_inquiry_pattern cd_patterns[] = { 160 {T_CDROM, T_REMOV, 161 "", "", ""}, 162 {T_CDROM, T_FIXED, 163 "", "", ""}, 164 {T_WORM, T_REMOV, 165 "", "", ""}, 166 {T_WORM, T_FIXED, 167 "", "", ""}, 168 {T_DIRECT, T_REMOV, 169 "NEC CD-ROM DRIVE:260", "", ""}, 170 #if 0 171 {T_CDROM, T_REMOV, /* more luns */ 172 "PIONEER ", "CD-ROM DRM-600 ", ""}, 173 #endif 174 }; 175 176 #define cdlookup(unit) (struct cd_softc *)disk_lookup(&cd_cd, (unit)) 177 178 int 179 cdmatch(struct device *parent, void *match, void *aux) 180 { 181 struct scsi_attach_args *sa = aux; 182 int priority; 183 184 scsi_inqmatch(sa->sa_inqbuf, cd_patterns, nitems(cd_patterns), 185 sizeof(cd_patterns[0]), &priority); 186 187 return (priority); 188 } 189 190 /* 191 * The routine called by the low level scsi routine when it discovers 192 * A device suitable for this driver 193 */ 194 void 195 cdattach(struct device *parent, struct device *self, void *aux) 196 { 197 struct cd_softc *sc = (struct cd_softc *)self; 198 struct scsi_attach_args *sa = aux; 199 struct scsi_link *sc_link = sa->sa_sc_link; 200 201 SC_DEBUG(sc_link, SDEV_DB2, ("cdattach:\n")); 202 203 /* 204 * Store information needed to contact our base driver 205 */ 206 sc->sc_link = sc_link; 207 sc_link->interpret_sense = cd_interpret_sense; 208 sc_link->device_softc = sc; 209 if (sc_link->openings > CDOUTSTANDING) 210 sc_link->openings = CDOUTSTANDING; 211 212 /* 213 * Initialize disk structures. 214 */ 215 sc->sc_dk.dk_name = sc->sc_dev.dv_xname; 216 bufq_init(&sc->sc_bufq, BUFQ_DEFAULT); 217 218 /* 219 * Note if this device is ancient. This is used in cdminphys(). 220 */ 221 if (!(sc_link->flags & SDEV_ATAPI) && 222 SCSISPC(sa->sa_inqbuf->version) == 0) 223 sc->sc_flags |= CDF_ANCIENT; 224 225 printf("\n"); 226 227 scsi_xsh_set(&sc->sc_xsh, sc_link, cdstart); 228 timeout_set(&sc->sc_timeout, (void (*)(void *))scsi_xsh_add, 229 &sc->sc_xsh); 230 231 /* Attach disk. */ 232 sc->sc_dk.dk_flags = DKF_NOLABELREAD; 233 disk_attach(&sc->sc_dev, &sc->sc_dk); 234 } 235 236 237 int 238 cdactivate(struct device *self, int act) 239 { 240 struct cd_softc *sc = (struct cd_softc *)self; 241 int rv = 0; 242 243 switch (act) { 244 case DVACT_RESUME: 245 /* 246 * When resuming, hardware may have forgotten we locked it. So if 247 * there are any open partitions, lock the CD. 248 */ 249 if (sc->sc_dk.dk_openmask != 0) 250 scsi_prevent(sc->sc_link, PR_PREVENT, 251 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 252 SCSI_SILENT | SCSI_AUTOCONF); 253 break; 254 case DVACT_DEACTIVATE: 255 sc->sc_flags |= CDF_DYING; 256 scsi_xsh_del(&sc->sc_xsh); 257 break; 258 } 259 return (rv); 260 } 261 262 int 263 cddetach(struct device *self, int flags) 264 { 265 struct cd_softc *sc = (struct cd_softc *)self; 266 267 bufq_drain(&sc->sc_bufq); 268 269 disk_gone(cdopen, self->dv_unit); 270 271 /* Detach disk. */ 272 bufq_destroy(&sc->sc_bufq); 273 disk_detach(&sc->sc_dk); 274 275 return (0); 276 } 277 278 /* 279 * Open the device. Make sure the partition info is as up-to-date as can be. 280 */ 281 int 282 cdopen(dev_t dev, int flag, int fmt, struct proc *p) 283 { 284 struct scsi_link *sc_link; 285 struct cd_softc *sc; 286 int error = 0, part, rawopen, unit; 287 288 unit = DISKUNIT(dev); 289 part = DISKPART(dev); 290 291 rawopen = (part == RAW_PART) && (fmt == S_IFCHR); 292 293 sc = cdlookup(unit); 294 if (sc == NULL) 295 return (ENXIO); 296 if (sc->sc_flags & CDF_DYING) { 297 device_unref(&sc->sc_dev); 298 return (ENXIO); 299 } 300 301 sc_link = sc->sc_link; 302 SC_DEBUG(sc_link, SDEV_DB1, 303 ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 304 cd_cd.cd_ndevs, part)); 305 306 if ((error = disk_lock(&sc->sc_dk)) != 0) { 307 device_unref(&sc->sc_dev); 308 return (error); 309 } 310 311 if (sc->sc_dk.dk_openmask != 0) { 312 /* 313 * If any partition is open, but the disk has been invalidated, 314 * disallow further opens. 315 */ 316 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 317 if (rawopen) 318 goto out; 319 error = EIO; 320 goto bad; 321 } 322 } else { 323 /* 324 * Check that it is still responding and ok. Drive can be in 325 * progress of loading media so use increased retries number 326 * and don't ignore NOT_READY. 327 */ 328 329 /* Use cd_interpret_sense() now. */ 330 sc_link->flags |= SDEV_OPEN; 331 332 error = scsi_test_unit_ready(sc_link, TEST_READY_RETRIES, 333 (rawopen ? SCSI_SILENT : 0) | SCSI_IGNORE_ILLEGAL_REQUEST | 334 SCSI_IGNORE_MEDIA_CHANGE); 335 336 /* Start the cd spinning if necessary. */ 337 if (error == EIO) 338 error = scsi_start(sc_link, SSS_START, 339 SCSI_IGNORE_ILLEGAL_REQUEST | 340 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT); 341 342 if (error) { 343 if (rawopen) { 344 error = 0; 345 goto out; 346 } else 347 goto bad; 348 } 349 350 /* Lock the cd in. */ 351 error = scsi_prevent(sc_link, PR_PREVENT, 352 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 353 SCSI_SILENT); 354 if (error) 355 goto bad; 356 357 /* Load the physical device parameters. */ 358 sc_link->flags |= SDEV_MEDIA_LOADED; 359 if (cd_get_parms(sc, (rawopen ? SCSI_SILENT : 0) | 360 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE)) { 361 sc_link->flags &= ~SDEV_MEDIA_LOADED; 362 error = ENXIO; 363 goto bad; 364 } 365 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded\n")); 366 367 /* Fabricate a disk label. */ 368 cdgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0); 369 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated\n")); 370 } 371 372 out: 373 if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0) 374 goto bad; 375 376 sc_link->flags |= SDEV_OPEN; 377 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 378 379 /* It's OK to fall through because dk_openmask is now non-zero. */ 380 bad: 381 if (sc->sc_dk.dk_openmask == 0) { 382 scsi_prevent(sc_link, PR_ALLOW, 383 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | 384 SCSI_SILENT); 385 sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED); 386 } 387 388 disk_unlock(&sc->sc_dk); 389 device_unref(&sc->sc_dev); 390 return (error); 391 } 392 393 /* 394 * Close the device. Only called if we are the last occurrence of an open 395 * device. 396 */ 397 int 398 cdclose(dev_t dev, int flag, int fmt, struct proc *p) 399 { 400 struct cd_softc *sc; 401 int part = DISKPART(dev); 402 403 sc = cdlookup(DISKUNIT(dev)); 404 if (sc == NULL) 405 return ENXIO; 406 if (sc->sc_flags & CDF_DYING) { 407 device_unref(&sc->sc_dev); 408 return (ENXIO); 409 } 410 411 disk_lock_nointr(&sc->sc_dk); 412 413 disk_closepart(&sc->sc_dk, part, fmt); 414 415 if (sc->sc_dk.dk_openmask == 0) { 416 /* XXXX Must wait for I/O to complete! */ 417 418 scsi_prevent(sc->sc_link, PR_ALLOW, 419 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | 420 SCSI_SILENT); 421 sc->sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED); 422 423 if (sc->sc_link->flags & SDEV_EJECTING) { 424 scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0); 425 426 sc->sc_link->flags &= ~SDEV_EJECTING; 427 } 428 429 timeout_del(&sc->sc_timeout); 430 scsi_xsh_del(&sc->sc_xsh); 431 } 432 433 disk_unlock(&sc->sc_dk); 434 435 device_unref(&sc->sc_dev); 436 return 0; 437 } 438 439 /* 440 * Actually translate the requested transfer into one the physical driver can 441 * understand. The transfer is described by a buf and will include only one 442 * physical transfer. 443 */ 444 void 445 cdstrategy(struct buf *bp) 446 { 447 struct cd_softc *sc; 448 int s; 449 450 sc = cdlookup(DISKUNIT(bp->b_dev)); 451 if (sc == NULL) { 452 bp->b_error = ENXIO; 453 goto bad; 454 } 455 if (sc->sc_flags & CDF_DYING) { 456 bp->b_error = ENXIO; 457 goto bad; 458 } 459 460 SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdstrategy: %ld bytes @ blk %lld\n", 461 bp->b_bcount, (long long)bp->b_blkno)); 462 /* 463 * If the device has been made invalid, error out 464 * maybe the media changed, or no media loaded 465 */ 466 if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 467 bp->b_error = EIO; 468 goto bad; 469 } 470 471 /* Validate the request. */ 472 if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1) 473 goto done; 474 475 /* Place it in the queue of disk activities for this disk. */ 476 bufq_queue(&sc->sc_bufq, bp); 477 478 /* 479 * Tell the device to get going on the transfer if it's 480 * not doing anything, otherwise just wait for completion 481 */ 482 scsi_xsh_add(&sc->sc_xsh); 483 484 device_unref(&sc->sc_dev); 485 return; 486 487 bad: 488 bp->b_flags |= B_ERROR; 489 bp->b_resid = bp->b_bcount; 490 done: 491 s = splbio(); 492 biodone(bp); 493 splx(s); 494 if (sc != NULL) 495 device_unref(&sc->sc_dev); 496 } 497 498 /* 499 * cdstart looks to see if there is a buf waiting for the device 500 * and that the device is not already busy. If both are true, 501 * It deques the buf and creates a scsi command to perform the 502 * transfer in the buf. The transfer request will call scsi_done 503 * on completion, which will in turn call this routine again 504 * so that the next queued transfer is performed. 505 * The bufs are queued by the strategy routine (cdstrategy) 506 * 507 * This routine is also called after other non-queued requests 508 * have been made of the scsi driver, to ensure that the queue 509 * continues to be drained. 510 * 511 * must be called at the correct (highish) spl level 512 * cdstart() is called at splbio from cdstrategy and scsi_done 513 */ 514 void 515 cdstart(struct scsi_xfer *xs) 516 { 517 struct scsi_link *sc_link = xs->sc_link; 518 struct cd_softc *sc = sc_link->device_softc; 519 struct buf *bp; 520 struct scsi_rw_big *cmd_big; 521 struct scsi_rw *cmd_small; 522 u_int64_t secno, nsecs; 523 struct partition *p; 524 int read; 525 526 SC_DEBUG(sc_link, SDEV_DB2, ("cdstart\n")); 527 528 if (sc->sc_flags & CDF_DYING) { 529 scsi_xs_put(xs); 530 return; 531 } 532 533 /* 534 * If the device has become invalid, abort all the 535 * reads and writes until all files have been closed and 536 * re-opened 537 */ 538 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 539 bufq_drain(&sc->sc_bufq); 540 scsi_xs_put(xs); 541 return; 542 } 543 544 bp = bufq_dequeue(&sc->sc_bufq); 545 if (bp == NULL) { 546 scsi_xs_put(xs); 547 return; 548 } 549 550 /* 551 * We have a buf, now we should make a command 552 * 553 * First, translate the block to absolute and put it in terms 554 * of the logical blocksize of the device. 555 */ 556 secno = DL_BLKTOSEC(sc->sc_dk.dk_label, bp->b_blkno); 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, 0); 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 = cd_size(sc->sc_link, flags, &sc->params.secsize); 1622 1623 if ((sc->params.secsize < 512) || 1624 ((sc->params.secsize & 511) != 0)) 1625 sc->params.secsize = 2048; /* some drives lie ! */ 1626 1627 if (sc->params.disksize < 100) 1628 sc->params.disksize = 400000; 1629 1630 return (0); 1631 } 1632 1633 daddr_t 1634 cdsize(dev_t dev) 1635 { 1636 1637 /* CD-ROMs are read-only. */ 1638 return -1; 1639 } 1640 1641 int 1642 cddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 1643 { 1644 /* Not implemented. */ 1645 return ENXIO; 1646 } 1647 1648 #define dvd_copy_key(dst, src) bcopy((src), (dst), DVD_KEY_SIZE) 1649 #define dvd_copy_challenge(dst, src) bcopy((src), (dst), DVD_CHALLENGE_SIZE) 1650 1651 #define DVD_AUTH_BUFSIZE 20 1652 1653 int 1654 dvd_auth(struct cd_softc *sc, union dvd_authinfo *a) 1655 { 1656 struct scsi_generic *cmd; 1657 struct scsi_xfer *xs; 1658 u_int8_t *buf; 1659 int error; 1660 1661 buf = dma_alloc(DVD_AUTH_BUFSIZE, PR_WAITOK | PR_ZERO); 1662 if (buf == NULL) 1663 return (ENOMEM); 1664 1665 xs = scsi_xs_get(sc->sc_link, 0); 1666 if (xs == NULL) { 1667 error = ENOMEM; 1668 goto done; 1669 } 1670 xs->cmdlen = sizeof(*cmd); 1671 xs->timeout = 30000; 1672 xs->data = buf; 1673 1674 cmd = xs->cmd; 1675 1676 switch (a->type) { 1677 case DVD_LU_SEND_AGID: 1678 cmd->opcode = GPCMD_REPORT_KEY; 1679 cmd->bytes[8] = 8; 1680 cmd->bytes[9] = 0 | (0 << 6); 1681 xs->datalen = 8; 1682 xs->flags |= SCSI_DATA_IN; 1683 1684 error = scsi_xs_sync(xs); 1685 scsi_xs_put(xs); 1686 1687 if (error == 0) 1688 a->lsa.agid = buf[7] >> 6; 1689 break; 1690 1691 case DVD_LU_SEND_CHALLENGE: 1692 cmd->opcode = GPCMD_REPORT_KEY; 1693 cmd->bytes[8] = 16; 1694 cmd->bytes[9] = 1 | (a->lsc.agid << 6); 1695 xs->datalen = 16; 1696 xs->flags |= SCSI_DATA_IN; 1697 1698 error = scsi_xs_sync(xs); 1699 scsi_xs_put(xs); 1700 if (error == 0) 1701 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1702 break; 1703 1704 case DVD_LU_SEND_KEY1: 1705 cmd->opcode = GPCMD_REPORT_KEY; 1706 cmd->bytes[8] = 12; 1707 cmd->bytes[9] = 2 | (a->lsk.agid << 6); 1708 xs->datalen = 12; 1709 xs->flags |= SCSI_DATA_IN; 1710 1711 error = scsi_xs_sync(xs); 1712 scsi_xs_put(xs); 1713 1714 if (error == 0) 1715 dvd_copy_key(a->lsk.key, &buf[4]); 1716 break; 1717 1718 case DVD_LU_SEND_TITLE_KEY: 1719 cmd->opcode = GPCMD_REPORT_KEY; 1720 _lto4b(a->lstk.lba, &cmd->bytes[1]); 1721 cmd->bytes[8] = 12; 1722 cmd->bytes[9] = 4 | (a->lstk.agid << 6); 1723 xs->datalen = 12; 1724 xs->flags |= SCSI_DATA_IN; 1725 1726 error = scsi_xs_sync(xs); 1727 scsi_xs_put(xs); 1728 1729 if (error == 0) { 1730 a->lstk.cpm = (buf[4] >> 7) & 1; 1731 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1732 a->lstk.cgms = (buf[4] >> 4) & 3; 1733 dvd_copy_key(a->lstk.title_key, &buf[5]); 1734 } 1735 break; 1736 1737 case DVD_LU_SEND_ASF: 1738 cmd->opcode = GPCMD_REPORT_KEY; 1739 cmd->bytes[8] = 8; 1740 cmd->bytes[9] = 5 | (a->lsasf.agid << 6); 1741 xs->datalen = 8; 1742 xs->flags |= SCSI_DATA_IN; 1743 1744 error = scsi_xs_sync(xs); 1745 scsi_xs_put(xs); 1746 1747 if (error == 0) 1748 a->lsasf.asf = buf[7] & 1; 1749 break; 1750 1751 case DVD_HOST_SEND_CHALLENGE: 1752 cmd->opcode = GPCMD_SEND_KEY; 1753 cmd->bytes[8] = 16; 1754 cmd->bytes[9] = 1 | (a->hsc.agid << 6); 1755 buf[1] = 14; 1756 dvd_copy_challenge(&buf[4], a->hsc.chal); 1757 xs->datalen = 16; 1758 xs->flags |= SCSI_DATA_OUT; 1759 1760 error = scsi_xs_sync(xs); 1761 scsi_xs_put(xs); 1762 1763 if (error == 0) 1764 a->type = DVD_LU_SEND_KEY1; 1765 break; 1766 1767 case DVD_HOST_SEND_KEY2: 1768 cmd->opcode = GPCMD_SEND_KEY; 1769 cmd->bytes[8] = 12; 1770 cmd->bytes[9] = 3 | (a->hsk.agid << 6); 1771 buf[1] = 10; 1772 dvd_copy_key(&buf[4], a->hsk.key); 1773 xs->datalen = 12; 1774 xs->flags |= SCSI_DATA_OUT; 1775 1776 error = scsi_xs_sync(xs); 1777 scsi_xs_put(xs); 1778 1779 if (error == 0) 1780 a->type = DVD_AUTH_ESTABLISHED; 1781 else 1782 a->type = DVD_AUTH_FAILURE; 1783 break; 1784 1785 case DVD_INVALIDATE_AGID: 1786 cmd->opcode = GPCMD_REPORT_KEY; 1787 cmd->bytes[9] = 0x3f | (a->lsa.agid << 6); 1788 xs->data = NULL; 1789 1790 error = scsi_xs_sync(xs); 1791 scsi_xs_put(xs); 1792 break; 1793 1794 case DVD_LU_SEND_RPC_STATE: 1795 cmd->opcode = GPCMD_REPORT_KEY; 1796 cmd->bytes[8] = 8; 1797 cmd->bytes[9] = 8 | (0 << 6); 1798 xs->datalen = 8; 1799 xs->flags |= SCSI_DATA_IN; 1800 1801 error = scsi_xs_sync(xs); 1802 scsi_xs_put(xs); 1803 1804 if (error == 0) { 1805 a->lrpcs.type = (buf[4] >> 6) & 3; 1806 a->lrpcs.vra = (buf[4] >> 3) & 7; 1807 a->lrpcs.ucca = (buf[4]) & 7; 1808 a->lrpcs.region_mask = buf[5]; 1809 a->lrpcs.rpc_scheme = buf[6]; 1810 } 1811 break; 1812 1813 case DVD_HOST_SEND_RPC_STATE: 1814 cmd->opcode = GPCMD_SEND_KEY; 1815 cmd->bytes[8] = 8; 1816 cmd->bytes[9] = 6 | (0 << 6); 1817 buf[1] = 6; 1818 buf[4] = a->hrpcs.pdrc; 1819 xs->datalen = 8; 1820 xs->flags |= SCSI_DATA_OUT; 1821 1822 error = scsi_xs_sync(xs); 1823 scsi_xs_put(xs); 1824 break; 1825 1826 default: 1827 scsi_xs_put(xs); 1828 error = ENOTTY; 1829 break; 1830 } 1831 done: 1832 dma_free(buf, DVD_AUTH_BUFSIZE); 1833 return (error); 1834 } 1835 1836 #define DVD_READ_PHYSICAL_BUFSIZE (4 + 4 * 20) 1837 int 1838 dvd_read_physical(struct cd_softc *sc, union dvd_struct *s) 1839 { 1840 struct scsi_generic *cmd; 1841 struct dvd_layer *layer; 1842 struct scsi_xfer *xs; 1843 u_int8_t *buf, *bufp; 1844 int error, i; 1845 1846 buf = dma_alloc(DVD_READ_PHYSICAL_BUFSIZE, PR_WAITOK | PR_ZERO); 1847 if (buf == NULL) 1848 return (ENOMEM); 1849 1850 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1851 if (xs == NULL) { 1852 error = ENOMEM; 1853 goto done; 1854 } 1855 xs->cmdlen = sizeof(*cmd); 1856 xs->data = buf; 1857 xs->datalen = DVD_READ_PHYSICAL_BUFSIZE; 1858 xs->timeout = 30000; 1859 1860 cmd = xs->cmd; 1861 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 1862 cmd->bytes[6] = s->type; 1863 _lto2b(xs->datalen, &cmd->bytes[7]); 1864 1865 cmd->bytes[5] = s->physical.layer_num; 1866 1867 error = scsi_xs_sync(xs); 1868 scsi_xs_put(xs); 1869 1870 if (error == 0) { 1871 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; 1872 i < 4; i++, bufp += 20, layer++) { 1873 bzero(layer, sizeof(*layer)); 1874 layer->book_version = bufp[0] & 0xf; 1875 layer->book_type = bufp[0] >> 4; 1876 layer->min_rate = bufp[1] & 0xf; 1877 layer->disc_size = bufp[1] >> 4; 1878 layer->layer_type = bufp[2] & 0xf; 1879 layer->track_path = (bufp[2] >> 4) & 1; 1880 layer->nlayers = (bufp[2] >> 5) & 3; 1881 layer->track_density = bufp[3] & 0xf; 1882 layer->linear_density = bufp[3] >> 4; 1883 layer->start_sector = _4btol(&bufp[4]); 1884 layer->end_sector = _4btol(&bufp[8]); 1885 layer->end_sector_l0 = _4btol(&bufp[12]); 1886 layer->bca = bufp[16] >> 7; 1887 } 1888 } 1889 done: 1890 dma_free(buf, DVD_READ_PHYSICAL_BUFSIZE); 1891 return (error); 1892 } 1893 1894 #define DVD_READ_COPYRIGHT_BUFSIZE 8 1895 int 1896 dvd_read_copyright(struct cd_softc *sc, union dvd_struct *s) 1897 { 1898 struct scsi_generic *cmd; 1899 struct scsi_xfer *xs; 1900 u_int8_t *buf; 1901 int error; 1902 1903 buf = dma_alloc(DVD_READ_COPYRIGHT_BUFSIZE, PR_WAITOK | PR_ZERO); 1904 if (buf == NULL) 1905 return (ENOMEM); 1906 1907 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1908 if (xs == NULL) { 1909 error = ENOMEM; 1910 goto done; 1911 } 1912 xs->cmdlen = sizeof(*cmd); 1913 xs->data = buf; 1914 xs->datalen = DVD_READ_COPYRIGHT_BUFSIZE; 1915 xs->timeout = 30000; 1916 1917 cmd = xs->cmd; 1918 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 1919 cmd->bytes[6] = s->type; 1920 _lto2b(xs->datalen, &cmd->bytes[7]); 1921 1922 cmd->bytes[5] = s->copyright.layer_num; 1923 1924 error = scsi_xs_sync(xs); 1925 scsi_xs_put(xs); 1926 1927 if (error == 0) { 1928 s->copyright.cpst = buf[4]; 1929 s->copyright.rmi = buf[5]; 1930 } 1931 done: 1932 dma_free(buf, DVD_READ_COPYRIGHT_BUFSIZE); 1933 return (error); 1934 } 1935 1936 int 1937 dvd_read_disckey(struct cd_softc *sc, union dvd_struct *s) 1938 { 1939 struct scsi_read_dvd_structure_data *buf; 1940 struct scsi_read_dvd_structure *cmd; 1941 struct scsi_xfer *xs; 1942 int error; 1943 1944 buf = dma_alloc(sizeof(*buf), PR_WAITOK | PR_ZERO); 1945 if (buf == NULL) 1946 return (ENOMEM); 1947 1948 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1949 if (xs == NULL) { 1950 error = ENOMEM; 1951 goto done; 1952 } 1953 xs->cmdlen = sizeof(*cmd); 1954 xs->data = (void *)buf; 1955 xs->datalen = sizeof(*buf); 1956 xs->timeout = 30000; 1957 1958 cmd = (struct scsi_read_dvd_structure *)xs->cmd; 1959 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 1960 cmd->format = s->type; 1961 cmd->agid = s->disckey.agid << 6; 1962 _lto2b(xs->datalen, cmd->length); 1963 1964 error = scsi_xs_sync(xs); 1965 scsi_xs_put(xs); 1966 1967 if (error == 0) 1968 bcopy(buf->data, s->disckey.value, sizeof(s->disckey.value)); 1969 done: 1970 dma_free(buf, sizeof(*buf)); 1971 return (error); 1972 } 1973 1974 #define DVD_READ_BCA_BUFLEN (4 + 188) 1975 1976 int 1977 dvd_read_bca(struct cd_softc *sc, union dvd_struct *s) 1978 { 1979 struct scsi_generic *cmd; 1980 struct scsi_xfer *xs; 1981 u_int8_t *buf; 1982 int error; 1983 1984 buf = dma_alloc(DVD_READ_BCA_BUFLEN, PR_WAITOK | PR_ZERO); 1985 if (buf == NULL) 1986 return (ENOMEM); 1987 1988 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 1989 if (xs == NULL) { 1990 error = ENOMEM; 1991 goto done; 1992 } 1993 xs->cmdlen = sizeof(*cmd); 1994 xs->data = buf; 1995 xs->datalen = DVD_READ_BCA_BUFLEN; 1996 xs->timeout = 30000; 1997 1998 cmd = xs->cmd; 1999 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 2000 cmd->bytes[6] = s->type; 2001 _lto2b(xs->datalen, &cmd->bytes[7]); 2002 2003 error = scsi_xs_sync(xs); 2004 scsi_xs_put(xs); 2005 2006 if (error == 0) { 2007 s->bca.len = _2btol(&buf[0]); 2008 if (s->bca.len < 12 || s->bca.len > 188) 2009 return (EIO); 2010 bcopy(&buf[4], s->bca.value, s->bca.len); 2011 } 2012 done: 2013 dma_free(buf, DVD_READ_BCA_BUFLEN); 2014 return (error); 2015 } 2016 2017 int 2018 dvd_read_manufact(struct cd_softc *sc, union dvd_struct *s) 2019 { 2020 struct scsi_read_dvd_structure_data *buf; 2021 struct scsi_read_dvd_structure *cmd; 2022 struct scsi_xfer *xs; 2023 int error; 2024 2025 buf = dma_alloc(sizeof(*buf), PR_WAITOK | PR_ZERO); 2026 if (buf == NULL) 2027 return (ENOMEM); 2028 2029 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 2030 if (xs == NULL) { 2031 error = ENOMEM; 2032 goto done; 2033 } 2034 xs->cmdlen = sizeof(*cmd); 2035 xs->data = (void *)buf; 2036 xs->datalen = sizeof(*buf); 2037 xs->timeout = 30000; 2038 2039 cmd = (struct scsi_read_dvd_structure *)xs->cmd; 2040 cmd->opcode = GPCMD_READ_DVD_STRUCTURE; 2041 cmd->format = s->type; 2042 _lto2b(xs->datalen, cmd->length); 2043 2044 error = scsi_xs_sync(xs); 2045 scsi_xs_put(xs); 2046 2047 if (error == 0) { 2048 s->manufact.len = _2btol(buf->len); 2049 if (s->manufact.len >= 0 && s->manufact.len <= 2048) 2050 bcopy(buf->data, s->manufact.value, s->manufact.len); 2051 else 2052 error = EIO; 2053 } 2054 done: 2055 dma_free(buf, sizeof(*buf)); 2056 return (error); 2057 } 2058 2059 int 2060 dvd_read_struct(struct cd_softc *sc, union dvd_struct *s) 2061 { 2062 2063 switch (s->type) { 2064 case DVD_STRUCT_PHYSICAL: 2065 return (dvd_read_physical(sc, s)); 2066 case DVD_STRUCT_COPYRIGHT: 2067 return (dvd_read_copyright(sc, s)); 2068 case DVD_STRUCT_DISCKEY: 2069 return (dvd_read_disckey(sc, s)); 2070 case DVD_STRUCT_BCA: 2071 return (dvd_read_bca(sc, s)); 2072 case DVD_STRUCT_MANUFACT: 2073 return (dvd_read_manufact(sc, s)); 2074 default: 2075 return (EINVAL); 2076 } 2077 } 2078 2079 int 2080 cd_interpret_sense(struct scsi_xfer *xs) 2081 { 2082 struct scsi_sense_data *sense = &xs->sense; 2083 struct scsi_link *sc_link = xs->sc_link; 2084 u_int8_t skey = sense->flags & SSD_KEY; 2085 u_int8_t serr = sense->error_code & SSD_ERRCODE; 2086 2087 if (((sc_link->flags & SDEV_OPEN) == 0) || 2088 (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)) 2089 return (scsi_interpret_sense(xs)); 2090 2091 /* 2092 * We do custom processing in cd for the unit becoming ready 2093 * case. We do not allow xs->retries to be decremented on the 2094 * "Unit Becoming Ready" case. This is because CD drives 2095 * report "Unit Becoming Ready" when loading media and can 2096 * take a long time. Rather than having a massive timeout for 2097 * all operations (which would cause other problems), we allow 2098 * operations to wait (but be interruptable with Ctrl-C) 2099 * forever as long as the drive is reporting that it is 2100 * becoming ready. All other cases of not being ready are 2101 * handled by the default handler. 2102 */ 2103 switch(skey) { 2104 case SKEY_NOT_READY: 2105 if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0) 2106 return (0); 2107 if (ASC_ASCQ(sense) == SENSE_NOT_READY_BECOMING_READY) { 2108 SC_DEBUG(sc_link, SDEV_DB1, ("not ready: busy (%#x)\n", 2109 sense->add_sense_code_qual)); 2110 /* don't count this as a retry */ 2111 xs->retries++; 2112 return (scsi_delay(xs, 1)); 2113 } 2114 break; 2115 /* XXX more to come here for a few other cases */ 2116 default: 2117 break; 2118 } 2119 return (scsi_interpret_sense(xs)); 2120 } 2121 2122 /* 2123 * Find out from the device what its capacity is. 2124 */ 2125 u_int64_t 2126 cd_size(struct scsi_link *sc_link, int flags, u_int32_t *blksize) 2127 { 2128 struct scsi_read_cap_data_16 *rdcap16; 2129 struct scsi_read_capacity_16 *cmd; 2130 struct scsi_read_cap_data *rdcap; 2131 struct scsi_read_capacity *cmd10; 2132 struct scsi_xfer *xs; 2133 u_int64_t max_addr; 2134 int error; 2135 2136 if (blksize != NULL) 2137 *blksize = 0; 2138 2139 CLR(flags, SCSI_IGNORE_ILLEGAL_REQUEST); 2140 2141 /* 2142 * Start with a READ CAPACITY(10). 2143 */ 2144 rdcap = dma_alloc(sizeof(*rdcap), ((flags & SCSI_NOSLEEP) ? 2145 PR_NOWAIT : PR_WAITOK) | PR_ZERO); 2146 if (rdcap == NULL) 2147 return (0); 2148 2149 xs = scsi_xs_get(sc_link, flags | SCSI_DATA_IN | SCSI_SILENT); 2150 if (xs == NULL) { 2151 dma_free(rdcap, sizeof(*rdcap)); 2152 return (0); 2153 } 2154 xs->cmdlen = sizeof(*cmd10); 2155 xs->data = (void *)rdcap; 2156 xs->datalen = sizeof(*rdcap); 2157 xs->timeout = 20000; 2158 2159 cmd10 = (struct scsi_read_capacity *)xs->cmd; 2160 cmd10->opcode = READ_CAPACITY; 2161 2162 error = scsi_xs_sync(xs); 2163 scsi_xs_put(xs); 2164 2165 if (error) { 2166 SC_DEBUG(sc_link, SDEV_DB1, ("READ CAPACITY error (%#x)\n", 2167 error)); 2168 dma_free(rdcap, sizeof(*rdcap)); 2169 return (0); 2170 } 2171 2172 max_addr = _4btol(rdcap->addr); 2173 if (blksize != NULL) 2174 *blksize = _4btol(rdcap->length); 2175 dma_free(rdcap, sizeof(*rdcap)); 2176 2177 if (SCSISPC(sc_link->inqdata.version) < 3 && max_addr != 0xffffffff) 2178 goto exit; 2179 2180 /* 2181 * SCSI-3 devices, or devices reporting more than 2^32-1 sectors can 2182 * try READ CAPACITY(16). 2183 */ 2184 rdcap16 = dma_alloc(sizeof(*rdcap16), ((flags & SCSI_NOSLEEP) ? 2185 PR_NOWAIT : PR_WAITOK) | PR_ZERO); 2186 if (rdcap16 == NULL) 2187 goto exit; 2188 2189 xs = scsi_xs_get(sc_link, flags | SCSI_DATA_IN | SCSI_SILENT); 2190 if (xs == NULL) { 2191 dma_free(rdcap16, sizeof(*rdcap16)); 2192 goto exit; 2193 } 2194 xs->cmdlen = sizeof(*cmd); 2195 xs->data = (void *)rdcap16; 2196 xs->datalen = sizeof(*rdcap16); 2197 xs->timeout = 20000; 2198 2199 cmd = (struct scsi_read_capacity_16 *)xs->cmd; 2200 cmd->opcode = READ_CAPACITY_16; 2201 cmd->byte2 = SRC16_SERVICE_ACTION; 2202 _lto4b(sizeof(*rdcap16), cmd->length); 2203 2204 error = scsi_xs_sync(xs); 2205 scsi_xs_put(xs); 2206 if (error) { 2207 SC_DEBUG(sc_link, SDEV_DB1, ("READ CAPACITY 16 error (%#x)\n", 2208 error)); 2209 dma_free(rdcap16, sizeof(*rdcap16)); 2210 goto exit; 2211 } 2212 2213 max_addr = _8btol(rdcap16->addr); 2214 if (blksize != NULL) 2215 *blksize = _4btol(rdcap16->length); 2216 /* XXX The other READ CAPACITY(16) info could be stored away. */ 2217 dma_free(rdcap16, sizeof(*rdcap16)); 2218 2219 return (max_addr + 1); 2220 2221 exit: 2222 /* Return READ CAPACITY 10 values. */ 2223 if (max_addr != 0xffffffff) 2224 return (max_addr + 1); 2225 else if (blksize != NULL) 2226 *blksize = 0; 2227 return (0); 2228 } 2229 2230 #if defined(__macppc__) 2231 int 2232 cd_eject(void) 2233 { 2234 struct cd_softc *sc; 2235 int error = 0; 2236 2237 if (cd_cd.cd_ndevs == 0 || (sc = cd_cd.cd_devs[0]) == NULL) 2238 return (ENXIO); 2239 2240 if ((error = disk_lock(&sc->sc_dk)) != 0) 2241 return (error); 2242 2243 if (sc->sc_dk.dk_openmask == 0) { 2244 sc->sc_link->flags |= SDEV_EJECTING; 2245 2246 scsi_prevent(sc->sc_link, PR_ALLOW, 2247 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | 2248 SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE); 2249 sc->sc_link->flags &= ~SDEV_MEDIA_LOADED; 2250 2251 scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0); 2252 2253 sc->sc_link->flags &= ~SDEV_EJECTING; 2254 } 2255 disk_unlock(&sc->sc_dk); 2256 2257 return (error); 2258 } 2259 #endif 2260