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