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