1 /* $NetBSD: cd.c,v 1.136 2000/02/07 20:16:56 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Originally written by Julian Elischer (julian@tfs.com) 41 * for TRW Financial Systems for use under the MACH(2.5) operating system. 42 * 43 * TRW Financial Systems, in accordance with their agreement with Carnegie 44 * Mellon University, makes this software available to CMU to distribute 45 * or use in any manner that they see fit as long as this message is kept with 46 * the software. For this reason TFS also grants any other persons or 47 * organisations permission to use or modify this software. 48 * 49 * TFS supplies this software to be publicly redistributed 50 * on the understanding that TFS is not responsible for the correct 51 * functioning of this software in any circumstances. 52 * 53 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 54 */ 55 56 #include "rnd.h" 57 58 #include <sys/types.h> 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/kernel.h> 62 #include <sys/file.h> 63 #include <sys/stat.h> 64 #include <sys/ioctl.h> 65 #include <sys/buf.h> 66 #include <sys/uio.h> 67 #include <sys/malloc.h> 68 #include <sys/errno.h> 69 #include <sys/device.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/cdio.h> 73 #include <sys/dvdio.h> 74 #include <sys/scsiio.h> 75 #include <sys/proc.h> 76 #include <sys/conf.h> 77 #include <sys/vnode.h> 78 #if NRND > 0 79 #include <sys/rnd.h> 80 #endif 81 82 #include <dev/scsipi/scsipi_all.h> 83 #include <dev/scsipi/scsipi_cd.h> 84 #include <dev/scsipi/scsipi_disk.h> /* rw_big and start_stop come */ 85 /* from there */ 86 #include <dev/scsipi/scsi_disk.h> /* rw comes from there */ 87 #include <dev/scsipi/scsipiconf.h> 88 #include <dev/scsipi/cdvar.h> 89 90 #include "cd.h" /* NCD_SCSIBUS and NCD_ATAPIBUS come from here */ 91 92 #define 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)); 127 int cd_read_toc __P((struct cd_softc *, int, int, void *, int, int)); 128 int cd_get_parms __P((struct cd_softc *, int)); 129 int cd_load_toc __P((struct cd_softc *, struct cd_toc *)); 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 #if !defined(__i386__) && !defined(__vax__) 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 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 register void *v; 604 { 605 register struct cd_softc *cd = v; 606 register 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 /* 666 * We can fit in a small cdb. 667 */ 668 bzero(&cmd_small, sizeof(cmd_small)); 669 cmd_small.opcode = (bp->b_flags & B_READ) ? 670 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND; 671 _lto3b(bp->b_rawblkno, cmd_small.addr); 672 cmd_small.length = nblks & 0xff; 673 cmdlen = sizeof(cmd_small); 674 cmdp = (struct scsipi_generic *)&cmd_small; 675 } else 676 #endif 677 { 678 /* 679 * Need a large cdb. 680 */ 681 bzero(&cmd_big, sizeof(cmd_big)); 682 cmd_big.opcode = (bp->b_flags & B_READ) ? 683 READ_BIG : WRITE_BIG; 684 _lto4b(bp->b_rawblkno, cmd_big.addr); 685 _lto2b(nblks, cmd_big.length); 686 cmdlen = sizeof(cmd_big); 687 cmdp = (struct scsipi_generic *)&cmd_big; 688 } 689 690 /* Instrumentation. */ 691 disk_busy(&cd->sc_dk); 692 693 /* 694 * Call the routine that chats with the adapter. 695 * Note: we cannot sleep as we may be an interrupt 696 * XXX NOSLEEP really needed? 697 */ 698 error = scsipi_command(sc_link, cmdp, cmdlen, 699 (u_char *)bp->b_data, bp->b_bcount, 700 CDRETRIES, 30000, bp, XS_CTL_NOSLEEP | XS_CTL_ASYNC | 701 ((bp->b_flags & B_READ) ? 702 XS_CTL_DATA_IN : XS_CTL_DATA_OUT)); 703 if (error) { 704 disk_unbusy(&cd->sc_dk, 0); 705 printf("%s: not queued, error %d\n", 706 cd->sc_dev.dv_xname, error); 707 } 708 } 709 } 710 711 void 712 cddone(xs) 713 struct scsipi_xfer *xs; 714 { 715 struct cd_softc *cd = xs->sc_link->device_softc; 716 717 if (xs->bp != NULL) { 718 disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 719 #if NRND > 0 720 rnd_add_uint32(&cd->rnd_source, xs->bp->b_rawblkno); 721 #endif 722 } 723 } 724 725 void 726 cdminphys(bp) 727 struct buf *bp; 728 { 729 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 730 long max; 731 732 /* 733 * If the device is ancient, we want to make sure that 734 * the transfer fits into a 6-byte cdb. 735 * 736 * XXX Note that the SCSI-I spec says that 256-block transfers 737 * are allowed in a 6-byte read/write, and are specified 738 * by settng the "length" to 0. However, we're conservative 739 * here, allowing only 255-block transfers in case an 740 * ancient device gets confused by length == 0. A length of 0 741 * in a 10-byte read/write actually means 0 blocks. 742 */ 743 if (cd->flags & CDF_ANCIENT) { 744 max = cd->sc_dk.dk_label->d_secsize * 0xff; 745 746 if (bp->b_bcount > max) 747 bp->b_bcount = max; 748 } 749 750 (*cd->sc_link->adapter->scsipi_minphys)(bp); 751 } 752 753 int 754 cdread(dev, uio, ioflag) 755 dev_t dev; 756 struct uio *uio; 757 int ioflag; 758 { 759 760 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 761 } 762 763 int 764 cdwrite(dev, uio, ioflag) 765 dev_t dev; 766 struct uio *uio; 767 int ioflag; 768 { 769 770 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 771 } 772 773 /* 774 * conversion between minute-seconde-frame and logical block adress 775 * adresses format 776 */ 777 void 778 lba2msf (lba, m, s, f) 779 u_long lba; 780 u_char *m, *s, *f; 781 { 782 u_long tmp; 783 784 tmp = lba + CD_BLOCK_OFFSET; /* offset of first logical frame */ 785 tmp &= 0xffffff; /* negative lbas use only 24 bits */ 786 *m = tmp / (CD_SECS * CD_FRAMES); 787 tmp %= (CD_SECS * CD_FRAMES); 788 *s = tmp / CD_FRAMES; 789 *f = tmp % CD_FRAMES; 790 } 791 792 u_long 793 msf2lba (m, s, f) 794 u_char m, s, f; 795 { 796 797 return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET); 798 } 799 800 801 /* 802 * Perform special action on behalf of the user. 803 * Knows about the internals of this device 804 */ 805 int 806 cdioctl(dev, cmd, addr, flag, p) 807 dev_t dev; 808 u_long cmd; 809 caddr_t addr; 810 int flag; 811 struct proc *p; 812 { 813 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 814 int part = CDPART(dev); 815 int error; 816 817 SC_DEBUG(cd->sc_link, SDEV_DB2, ("cdioctl 0x%lx ", cmd)); 818 819 /* 820 * If the device is not valid, some IOCTLs can still be 821 * handled on the raw partition. Check this here. 822 */ 823 if ((cd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) { 824 switch (cmd) { 825 case DIOCWLABEL: 826 case DIOCLOCK: 827 case ODIOCEJECT: 828 case DIOCEJECT: 829 case SCIOCIDENTIFY: 830 case OSCIOCIDENTIFY: 831 case SCIOCCOMMAND: 832 case SCIOCDEBUG: 833 case CDIOCGETVOL: 834 case CDIOCSETVOL: 835 case CDIOCSETMONO: 836 case CDIOCSETSTEREO: 837 case CDIOCSETMUTE: 838 case CDIOCSETLEFT: 839 case CDIOCSETRIGHT: 840 case CDIOCCLOSE: 841 case CDIOCEJECT: 842 case CDIOCALLOW: 843 case CDIOCPREVENT: 844 case CDIOCSETDEBUG: 845 case CDIOCCLRDEBUG: 846 case CDIOCRESET: 847 case SCIOCRESET: 848 case CDIOCLOADUNLOAD: 849 case DVD_AUTH: 850 case DVD_READ_STRUCT: 851 if (part == RAW_PART) 852 break; 853 /* FALLTHROUGH */ 854 default: 855 if ((cd->sc_link->flags & SDEV_OPEN) == 0) 856 return (ENODEV); 857 else 858 return (EIO); 859 } 860 } 861 862 switch (cmd) { 863 case DIOCGDINFO: 864 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 865 return (0); 866 867 case DIOCGPART: 868 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 869 ((struct partinfo *)addr)->part = 870 &cd->sc_dk.dk_label->d_partitions[part]; 871 return (0); 872 873 case DIOCWDINFO: 874 case DIOCSDINFO: 875 if ((flag & FWRITE) == 0) 876 return (EBADF); 877 878 if ((error = cdlock(cd)) != 0) 879 return (error); 880 cd->flags |= CDF_LABELLING; 881 882 error = setdisklabel(cd->sc_dk.dk_label, 883 (struct disklabel *)addr, /*cd->sc_dk.dk_openmask : */0, 884 cd->sc_dk.dk_cpulabel); 885 if (error == 0) { 886 /* XXX ? */ 887 } 888 889 cd->flags &= ~CDF_LABELLING; 890 cdunlock(cd); 891 return (error); 892 893 case DIOCWLABEL: 894 return (EBADF); 895 896 case DIOCGDEFLABEL: 897 cdgetdefaultlabel(cd, (struct disklabel *)addr); 898 return (0); 899 900 case CDIOCPLAYTRACKS: { 901 struct ioc_play_track *args = (struct ioc_play_track *)addr; 902 903 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 904 return (error); 905 return (cd_play_tracks(cd, args->start_track, 906 args->start_index, args->end_track, args->end_index)); 907 } 908 case CDIOCPLAYMSF: { 909 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 910 911 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 912 return (error); 913 return (cd_play_msf(cd, args->start_m, args->start_s, 914 args->start_f, args->end_m, args->end_s, args->end_f)); 915 } 916 case CDIOCPLAYBLOCKS: { 917 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 918 919 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 920 return (error); 921 return (cd_play(cd, args->blk, args->len)); 922 } 923 case CDIOCREADSUBCHANNEL: { 924 struct ioc_read_subchannel *args = 925 (struct ioc_read_subchannel *)addr; 926 struct cd_sub_channel_info data; 927 int len = args->data_len; 928 929 if (len > sizeof(data) || 930 len < sizeof(struct cd_sub_channel_header)) 931 return (EINVAL); 932 error = cd_read_subchannel(cd, args->address_format, 933 args->data_format, args->track, &data, len); 934 if (error) 935 return (error); 936 len = min(len, _2btol(data.header.data_len) + 937 sizeof(struct cd_sub_channel_header)); 938 return (copyout(&data, args->data, len)); 939 } 940 case CDIOREADTOCHEADER: { 941 struct ioc_toc_header th; 942 943 if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 0)) != 0) 944 return (error); 945 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 946 #if BYTE_ORDER == BIG_ENDIAN 947 bswap((u_int8_t *)&th.len, sizeof(th.len)); 948 #endif 949 } else 950 th.len = ntohs(th.len); 951 bcopy(&th, addr, sizeof(th)); 952 return (0); 953 } 954 case CDIOREADTOCENTRYS: { 955 struct cd_toc toc; 956 struct ioc_read_toc_entry *te = 957 (struct ioc_read_toc_entry *)addr; 958 struct ioc_toc_header *th; 959 struct cd_toc_entry *cte; 960 int len = te->data_len; 961 int ntracks; 962 963 th = &toc.header; 964 965 if (len > sizeof(toc.entries) || 966 len < sizeof(struct cd_toc_entry)) 967 return (EINVAL); 968 error = cd_read_toc(cd, te->address_format, te->starting_track, 969 &toc, len + sizeof(struct ioc_toc_header), 0); 970 if (error) 971 return (error); 972 if (te->address_format == CD_LBA_FORMAT) 973 for (ntracks = 974 th->ending_track - th->starting_track + 1; 975 ntracks >= 0; ntracks--) { 976 cte = &toc.entries[ntracks]; 977 cte->addr_type = CD_LBA_FORMAT; 978 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 979 #if BYTE_ORDER == BIG_ENDIAN 980 bswap((u_int8_t*)&cte->addr, 981 sizeof(cte->addr)); 982 #endif 983 } else 984 cte->addr.lba = ntohl(cte->addr.lba); 985 } 986 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 987 #if BYTE_ORDER == BIG_ENDIAN 988 bswap((u_int8_t*)&th->len, sizeof(th->len)); 989 #endif 990 } else 991 th->len = ntohs(th->len); 992 len = min(len, th->len - (sizeof(th->starting_track) + 993 sizeof(th->ending_track))); 994 return (copyout(toc.entries, te->data, len)); 995 } 996 case CDIOREADMSADDR: { 997 struct cd_toc toc; 998 int sessno = *(int*)addr; 999 struct cd_toc_entry *cte; 1000 1001 if (sessno != 0) 1002 return (EINVAL); 1003 1004 error = cd_read_toc(cd, 0, 0, &toc, 1005 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1006 0x40 /* control word for "get MS info" */); 1007 1008 if (error) 1009 return (error); 1010 1011 cte = &toc.entries[0]; 1012 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 1013 #if BYTE_ORDER == BIG_ENDIAN 1014 bswap((u_int8_t*)&cte->addr, sizeof(cte->addr)); 1015 #endif 1016 } else 1017 cte->addr.lba = ntohl(cte->addr.lba); 1018 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 1019 #if BYTE_ORDER == BIG_ENDIAN 1020 bswap((u_int8_t*)&toc.header.len, sizeof(toc.header.len)); 1021 #endif 1022 } else 1023 toc.header.len = ntohs(toc.header.len); 1024 1025 *(int*)addr = (toc.header.len >= 10 && cte->track > 1) ? 1026 cte->addr.lba : 0; 1027 return 0; 1028 } 1029 case CDIOCSETPATCH: { 1030 struct ioc_patch *arg = (struct ioc_patch *)addr; 1031 1032 return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0], 1033 arg->patch[1], arg->patch[2], arg->patch[3], 0)); 1034 } 1035 case CDIOCGETVOL: { 1036 struct ioc_vol *arg = (struct ioc_vol *)addr; 1037 1038 return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0)); 1039 } 1040 case CDIOCSETVOL: { 1041 struct ioc_vol *arg = (struct ioc_vol *)addr; 1042 1043 return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0)); 1044 } 1045 1046 case CDIOCSETMONO: 1047 return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL, 1048 BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1049 1050 case CDIOCSETSTEREO: 1051 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1052 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1053 1054 case CDIOCSETMUTE: 1055 return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL, 1056 MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1057 1058 case CDIOCSETLEFT: 1059 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1060 LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1061 1062 case CDIOCSETRIGHT: 1063 return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL, 1064 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1065 1066 case CDIOCRESUME: 1067 return (cd_pause(cd, PA_RESUME)); 1068 case CDIOCPAUSE: 1069 return (cd_pause(cd, PA_PAUSE)); 1070 case CDIOCSTART: 1071 return (scsipi_start(cd->sc_link, SSS_START, 0)); 1072 case CDIOCSTOP: 1073 return (scsipi_start(cd->sc_link, SSS_STOP, 0)); 1074 case CDIOCCLOSE: 1075 return (scsipi_start(cd->sc_link, SSS_START|SSS_LOEJ, 1076 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1077 case DIOCEJECT: 1078 if (*(int *)addr == 0) { 1079 /* 1080 * Don't force eject: check that we are the only 1081 * partition open. If so, unlock it. 1082 */ 1083 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1084 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1085 cd->sc_dk.dk_openmask) { 1086 error = scsipi_prevent(cd->sc_link, PR_ALLOW, 1087 XS_CTL_IGNORE_NOT_READY); 1088 if (error) 1089 return (error); 1090 } else { 1091 return (EBUSY); 1092 } 1093 } 1094 /* FALLTHROUGH */ 1095 case CDIOCEJECT: /* FALLTHROUGH */ 1096 case ODIOCEJECT: 1097 return (scsipi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0)); 1098 case CDIOCALLOW: 1099 return (scsipi_prevent(cd->sc_link, PR_ALLOW, 0)); 1100 case CDIOCPREVENT: 1101 return (scsipi_prevent(cd->sc_link, PR_PREVENT, 0)); 1102 case DIOCLOCK: 1103 return (scsipi_prevent(cd->sc_link, 1104 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1105 case CDIOCSETDEBUG: 1106 cd->sc_link->flags |= (SDEV_DB1 | SDEV_DB2); 1107 return (0); 1108 case CDIOCCLRDEBUG: 1109 cd->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); 1110 return (0); 1111 case CDIOCRESET: 1112 case SCIOCRESET: 1113 return (cd_reset(cd)); 1114 case CDIOCLOADUNLOAD: { 1115 struct ioc_load_unload *args = (struct ioc_load_unload *)addr; 1116 1117 return ((*cd->sc_ops->cdo_load_unload)(cd, args->options, 1118 args->slot)); 1119 case DVD_AUTH: 1120 return (dvd_auth(cd, (dvd_authinfo *)addr)); 1121 case DVD_READ_STRUCT: 1122 return (dvd_read_struct(cd, (dvd_struct *)addr)); 1123 } 1124 1125 default: 1126 if (part != RAW_PART) 1127 return (ENOTTY); 1128 return (scsipi_do_ioctl(cd->sc_link, dev, cmd, addr, flag, p)); 1129 } 1130 1131 #ifdef DIAGNOSTIC 1132 panic("cdioctl: impossible"); 1133 #endif 1134 } 1135 1136 void 1137 cdgetdefaultlabel(cd, lp) 1138 struct cd_softc *cd; 1139 struct disklabel *lp; 1140 { 1141 1142 bzero(lp, sizeof(struct disklabel)); 1143 1144 lp->d_secsize = cd->params.blksize; 1145 lp->d_ntracks = 1; 1146 lp->d_nsectors = 100; 1147 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1148 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1149 1150 switch (cd->sc_link->type) { 1151 #if NCD_SCSIBUS > 0 1152 case BUS_SCSI: 1153 lp->d_type = DTYPE_SCSI; 1154 break; 1155 #endif 1156 #if NCD_ATAPIBUS > 0 1157 case BUS_ATAPI: 1158 lp->d_type = DTYPE_ATAPI; 1159 break; 1160 #endif 1161 } 1162 strncpy(lp->d_typename, cd->name, 16); 1163 strncpy(lp->d_packname, "fictitious", 16); 1164 lp->d_secperunit = cd->params.disksize; 1165 lp->d_rpm = 300; 1166 lp->d_interleave = 1; 1167 lp->d_flags = D_REMOVABLE; 1168 1169 lp->d_partitions[0].p_offset = 0; 1170 lp->d_partitions[0].p_size = 1171 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1172 lp->d_partitions[0].p_fstype = FS_ISO9660; 1173 lp->d_partitions[RAW_PART].p_offset = 0; 1174 lp->d_partitions[RAW_PART].p_size = 1175 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1176 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1177 lp->d_npartitions = RAW_PART + 1; 1178 1179 lp->d_magic = DISKMAGIC; 1180 lp->d_magic2 = DISKMAGIC; 1181 lp->d_checksum = dkcksum(lp); 1182 } 1183 1184 /* 1185 * Load the label information on the named device 1186 * Actually fabricate a disklabel 1187 * 1188 * EVENTUALLY take information about different 1189 * data tracks from the TOC and put it in the disklabel 1190 */ 1191 void 1192 cdgetdisklabel(cd) 1193 struct cd_softc *cd; 1194 { 1195 struct disklabel *lp = cd->sc_dk.dk_label; 1196 1197 bzero(cd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel)); 1198 1199 cdgetdefaultlabel(cd, lp); 1200 } 1201 1202 /* 1203 * Find out from the device what it's capacity is 1204 */ 1205 u_long 1206 cd_size(cd, flags) 1207 struct cd_softc *cd; 1208 int flags; 1209 { 1210 struct scsipi_read_cd_cap_data rdcap; 1211 struct scsipi_read_cd_capacity scsipi_cmd; 1212 int blksize; 1213 u_long size; 1214 1215 if (cd->sc_link->quirks & ADEV_NOCAPACITY) { 1216 /* 1217 * the drive doesn't support the READ_CD_CAPACITY command 1218 * use a fake size 1219 */ 1220 cd->params.blksize = 2048; 1221 cd->params.disksize = 400000; 1222 return (400000); 1223 } 1224 1225 /* 1226 * make up a scsi command and ask the scsi driver to do 1227 * it for you. 1228 */ 1229 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1230 scsipi_cmd.opcode = READ_CD_CAPACITY; 1231 1232 /* 1233 * If the command works, interpret the result as a 4 byte 1234 * number of blocks and a blocksize 1235 */ 1236 if (scsipi_command(cd->sc_link, 1237 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1238 (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL, 1239 flags | XS_CTL_DATA_IN) != 0) 1240 return (0); 1241 1242 blksize = _4btol(rdcap.length); 1243 if ((blksize < 512) || ((blksize & 511) != 0)) 1244 blksize = 2048; /* some drives lie ! */ 1245 cd->params.blksize = blksize; 1246 1247 size = _4btol(rdcap.addr) + 1; 1248 if (size < 100) 1249 size = 400000; /* ditto */ 1250 cd->params.disksize = size; 1251 1252 SC_DEBUG(cd->sc_link, SDEV_DB2, ("cd_size: %d %ld\n", blksize, size)); 1253 return (size); 1254 } 1255 1256 /* 1257 * Get scsi driver to send a "start playing" command 1258 */ 1259 int 1260 cd_play(cd, blkno, nblks) 1261 struct cd_softc *cd; 1262 int blkno, nblks; 1263 { 1264 struct scsipi_play scsipi_cmd; 1265 1266 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1267 scsipi_cmd.opcode = PLAY; 1268 _lto4b(blkno, scsipi_cmd.blk_addr); 1269 _lto2b(nblks, scsipi_cmd.xfer_len); 1270 return (scsipi_command(cd->sc_link, 1271 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1272 0, 0, CDRETRIES, 30000, NULL, 0)); 1273 } 1274 1275 /* 1276 * Get scsi driver to send a "start playing" command 1277 */ 1278 int 1279 cd_play_tracks(cd, strack, sindex, etrack, eindex) 1280 struct cd_softc *cd; 1281 int strack, sindex, etrack, eindex; 1282 { 1283 struct cd_toc toc; 1284 int error; 1285 1286 if (!etrack) 1287 return (EIO); 1288 if (strack > etrack) 1289 return (EINVAL); 1290 1291 if ((error = cd_load_toc(cd, &toc)) != 0) 1292 return (error); 1293 1294 if (++etrack > (toc.header.ending_track+1)) 1295 etrack = toc.header.ending_track+1; 1296 1297 strack -= toc.header.starting_track; 1298 etrack -= toc.header.starting_track; 1299 if (strack < 0) 1300 return (EINVAL); 1301 1302 return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute, 1303 toc.entries[strack].addr.msf.second, 1304 toc.entries[strack].addr.msf.frame, 1305 toc.entries[etrack].addr.msf.minute, 1306 toc.entries[etrack].addr.msf.second, 1307 toc.entries[etrack].addr.msf.frame)); 1308 } 1309 1310 /* 1311 * Get scsi driver to send a "play msf" command 1312 */ 1313 int 1314 cd_play_msf(cd, startm, starts, startf, endm, ends, endf) 1315 struct cd_softc *cd; 1316 int startm, starts, startf, endm, ends, endf; 1317 { 1318 struct scsipi_play_msf scsipi_cmd; 1319 1320 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1321 scsipi_cmd.opcode = PLAY_MSF; 1322 scsipi_cmd.start_m = startm; 1323 scsipi_cmd.start_s = starts; 1324 scsipi_cmd.start_f = startf; 1325 scsipi_cmd.end_m = endm; 1326 scsipi_cmd.end_s = ends; 1327 scsipi_cmd.end_f = endf; 1328 return (scsipi_command(cd->sc_link, 1329 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1330 0, 0, CDRETRIES, 30000, NULL, 0)); 1331 } 1332 1333 /* 1334 * Get scsi driver to send a "start up" command 1335 */ 1336 int 1337 cd_pause(cd, go) 1338 struct cd_softc *cd; 1339 int go; 1340 { 1341 struct scsipi_pause scsipi_cmd; 1342 1343 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1344 scsipi_cmd.opcode = PAUSE; 1345 scsipi_cmd.resume = go & 0xff; 1346 return (scsipi_command(cd->sc_link, 1347 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1348 0, 0, CDRETRIES, 30000, NULL, 0)); 1349 } 1350 1351 /* 1352 * Get scsi driver to send a "RESET" command 1353 */ 1354 int 1355 cd_reset(cd) 1356 struct cd_softc *cd; 1357 { 1358 1359 return (scsipi_command(cd->sc_link, 0, 0, 0, 0, 1360 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1361 } 1362 1363 /* 1364 * Read subchannel 1365 */ 1366 int 1367 cd_read_subchannel(cd, mode, format, track, data, len) 1368 struct cd_softc *cd; 1369 int mode, format, track, len; 1370 struct cd_sub_channel_info *data; 1371 { 1372 struct scsipi_read_subchannel scsipi_cmd; 1373 1374 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1375 scsipi_cmd.opcode = READ_SUBCHANNEL; 1376 if (mode == CD_MSF_FORMAT) 1377 scsipi_cmd.byte2 |= CD_MSF; 1378 scsipi_cmd.byte3 = SRS_SUBQ; 1379 scsipi_cmd.subchan_format = format; 1380 scsipi_cmd.track = track; 1381 _lto2b(len, scsipi_cmd.data_len); 1382 return (scsipi_command(cd->sc_link, 1383 (struct scsipi_generic *)&scsipi_cmd, 1384 sizeof(struct scsipi_read_subchannel), (u_char *)data, len, 1385 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_SILENT)); 1386 } 1387 1388 /* 1389 * Read table of contents 1390 */ 1391 int 1392 cd_read_toc(cd, mode, start, data, len, control) 1393 struct cd_softc *cd; 1394 int mode, start, len, control; 1395 void *data; 1396 { 1397 struct scsipi_read_toc scsipi_cmd; 1398 int ntoc; 1399 1400 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1401 #if 0 1402 if (len != sizeof(struct ioc_toc_header)) 1403 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 1404 sizeof(struct cd_toc_entry); 1405 else 1406 #endif 1407 ntoc = len; 1408 scsipi_cmd.opcode = READ_TOC; 1409 if (mode == CD_MSF_FORMAT) 1410 scsipi_cmd.byte2 |= CD_MSF; 1411 scsipi_cmd.from_track = start; 1412 _lto2b(ntoc, scsipi_cmd.data_len); 1413 scsipi_cmd.control = control; 1414 return (scsipi_command(cd->sc_link, 1415 (struct scsipi_generic *)&scsipi_cmd, 1416 sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES, 1417 30000, NULL, XS_CTL_DATA_IN)); 1418 } 1419 1420 int 1421 cd_load_toc(cd, toc) 1422 struct cd_softc *cd; 1423 struct cd_toc *toc; 1424 { 1425 int ntracks, len, error; 1426 1427 if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 0)) != 0) 1428 return (error); 1429 1430 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 1431 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 1432 sizeof(toc->header); 1433 if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 0)) != 0) 1434 return (error); 1435 return (0); 1436 } 1437 1438 /* 1439 * Get the scsi driver to send a full inquiry to the device and use the 1440 * results to fill out the disk parameter structure. 1441 */ 1442 int 1443 cd_get_parms(cd, flags) 1444 struct cd_softc *cd; 1445 int flags; 1446 { 1447 1448 /* 1449 * give a number of sectors so that sec * trks * cyls 1450 * is <= disk_size 1451 */ 1452 if (cd_size(cd, flags) == 0) 1453 return (ENXIO); 1454 return (0); 1455 } 1456 1457 int 1458 cdsize(dev) 1459 dev_t dev; 1460 { 1461 1462 /* CD-ROMs are read-only. */ 1463 return (-1); 1464 } 1465 1466 int 1467 cddump(dev, blkno, va, size) 1468 dev_t dev; 1469 daddr_t blkno; 1470 caddr_t va; 1471 size_t size; 1472 { 1473 1474 /* Not implemented. */ 1475 return (ENXIO); 1476 } 1477 1478 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 1479 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 1480 1481 int 1482 dvd_auth(cd, a) 1483 struct cd_softc *cd; 1484 dvd_authinfo *a; 1485 { 1486 struct scsipi_generic cmd; 1487 u_int8_t buf[20]; 1488 int error; 1489 1490 memset(cmd.bytes, 0, 15); 1491 memset(buf, 0, sizeof(buf)); 1492 1493 switch (a->type) { 1494 case DVD_LU_SEND_AGID: 1495 cmd.opcode = GPCMD_REPORT_KEY; 1496 cmd.bytes[8] = 8; 1497 cmd.bytes[9] = 0 | (0 << 6); 1498 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 8, 1499 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1500 if (error) 1501 return (error); 1502 a->lsa.agid = buf[7] >> 6; 1503 return (0); 1504 1505 case DVD_LU_SEND_CHALLENGE: 1506 cmd.opcode = GPCMD_REPORT_KEY; 1507 cmd.bytes[8] = 16; 1508 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 1509 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16, 1510 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1511 if (error) 1512 return (error); 1513 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1514 return (0); 1515 1516 case DVD_LU_SEND_KEY1: 1517 cmd.opcode = GPCMD_REPORT_KEY; 1518 cmd.bytes[8] = 12; 1519 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 1520 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12, 1521 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1522 if (error) 1523 return (error); 1524 dvd_copy_key(a->lsk.key, &buf[4]); 1525 return (0); 1526 1527 case DVD_LU_SEND_TITLE_KEY: 1528 cmd.opcode = GPCMD_REPORT_KEY; 1529 _lto4b(a->lstk.lba, &cmd.bytes[1]); 1530 cmd.bytes[8] = 12; 1531 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 1532 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12, 1533 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1534 if (error) 1535 return (error); 1536 a->lstk.cpm = (buf[4] >> 7) & 1; 1537 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1538 a->lstk.cgms = (buf[4] >> 4) & 3; 1539 dvd_copy_key(a->lstk.title_key, &buf[5]); 1540 return (0); 1541 1542 case DVD_LU_SEND_ASF: 1543 cmd.opcode = GPCMD_REPORT_KEY; 1544 cmd.bytes[8] = 8; 1545 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 1546 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 8, 1547 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1548 if (error) 1549 return (error); 1550 a->lsasf.asf = buf[7] & 1; 1551 return (0); 1552 1553 case DVD_HOST_SEND_CHALLENGE: 1554 cmd.opcode = GPCMD_SEND_KEY; 1555 cmd.bytes[8] = 16; 1556 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 1557 buf[1] = 14; 1558 dvd_copy_challenge(&buf[4], a->hsc.chal); 1559 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16, 1560 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT|XS_CTL_DATA_IN); 1561 if (error) 1562 return (error); 1563 a->type = DVD_LU_SEND_KEY1; 1564 return (0); 1565 1566 case DVD_HOST_SEND_KEY2: 1567 cmd.opcode = GPCMD_SEND_KEY; 1568 cmd.bytes[8] = 12; 1569 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 1570 buf[1] = 10; 1571 dvd_copy_key(&buf[4], a->hsk.key); 1572 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12, 1573 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT|XS_CTL_DATA_IN); 1574 if (error) { 1575 a->type = DVD_AUTH_FAILURE; 1576 return (error); 1577 } 1578 a->type = DVD_AUTH_ESTABLISHED; 1579 return (0); 1580 1581 case DVD_INVALIDATE_AGID: 1582 cmd.opcode = GPCMD_REPORT_KEY; 1583 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 1584 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16, 1585 CDRETRIES, 30000, NULL, 0); 1586 if (error) 1587 return (error); 1588 return (0); 1589 1590 default: 1591 return (ENOTTY); 1592 } 1593 } 1594 1595 int 1596 dvd_read_physical(cd, s) 1597 struct cd_softc *cd; 1598 dvd_struct *s; 1599 { 1600 struct scsipi_generic cmd; 1601 u_int8_t buf[4 + 4 * 20], *bufp; 1602 int error; 1603 struct dvd_layer *layer; 1604 int i; 1605 1606 memset(cmd.bytes, 0, 15); 1607 memset(buf, 0, sizeof(buf)); 1608 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1609 cmd.bytes[6] = s->type; 1610 _lto2b(sizeof(buf), &cmd.bytes[7]); 1611 1612 cmd.bytes[5] = s->physical.layer_num; 1613 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1614 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1615 if (error) 1616 return (error); 1617 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4; 1618 i++, bufp += 20, layer++) { 1619 memset(layer, 0, sizeof(*layer)); 1620 layer->book_version = bufp[0] & 0xf; 1621 layer->book_type = bufp[0] >> 4; 1622 layer->min_rate = bufp[1] & 0xf; 1623 layer->disc_size = bufp[1] >> 4; 1624 layer->layer_type = bufp[2] & 0xf; 1625 layer->track_path = (bufp[2] >> 4) & 1; 1626 layer->nlayers = (bufp[2] >> 5) & 3; 1627 layer->track_density = bufp[3] & 0xf; 1628 layer->linear_density = bufp[3] >> 4; 1629 layer->start_sector = _4btol(&bufp[4]); 1630 layer->end_sector = _4btol(&bufp[8]); 1631 layer->end_sector_l0 = _4btol(&bufp[12]); 1632 layer->bca = bufp[16] >> 7; 1633 } 1634 return (0); 1635 } 1636 1637 int 1638 dvd_read_copyright(cd, s) 1639 struct cd_softc *cd; 1640 dvd_struct *s; 1641 { 1642 struct scsipi_generic cmd; 1643 u_int8_t buf[8]; 1644 int error; 1645 1646 memset(cmd.bytes, 0, 15); 1647 memset(buf, 0, sizeof(buf)); 1648 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1649 cmd.bytes[6] = s->type; 1650 _lto2b(sizeof(buf), &cmd.bytes[7]); 1651 1652 cmd.bytes[5] = s->copyright.layer_num; 1653 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1654 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1655 if (error) 1656 return (error); 1657 s->copyright.cpst = buf[4]; 1658 s->copyright.rmi = buf[5]; 1659 return (0); 1660 } 1661 1662 int 1663 dvd_read_disckey(cd, s) 1664 struct cd_softc *cd; 1665 dvd_struct *s; 1666 { 1667 struct scsipi_generic cmd; 1668 u_int8_t buf[4 + 2048]; 1669 int error; 1670 1671 memset(cmd.bytes, 0, 15); 1672 memset(buf, 0, sizeof(buf)); 1673 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1674 cmd.bytes[6] = s->type; 1675 _lto2b(sizeof(buf), &cmd.bytes[7]); 1676 1677 cmd.bytes[9] = s->disckey.agid << 6; 1678 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1679 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1680 if (error) 1681 return (error); 1682 memcpy(s->disckey.value, &buf[4], 2048); 1683 return (0); 1684 } 1685 1686 int 1687 dvd_read_bca(cd, s) 1688 struct cd_softc *cd; 1689 dvd_struct *s; 1690 { 1691 struct scsipi_generic cmd; 1692 u_int8_t buf[4 + 188]; 1693 int error; 1694 1695 memset(cmd.bytes, 0, 15); 1696 memset(buf, 0, sizeof(buf)); 1697 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1698 cmd.bytes[6] = s->type; 1699 _lto2b(sizeof(buf), &cmd.bytes[7]); 1700 1701 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1702 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1703 if (error) 1704 return (error); 1705 s->bca.len = _2btol(&buf[0]); 1706 if (s->bca.len < 12 || s->bca.len > 188) 1707 return (EIO); 1708 memcpy(s->bca.value, &buf[4], s->bca.len); 1709 return (0); 1710 } 1711 1712 int 1713 dvd_read_manufact(cd, s) 1714 struct cd_softc *cd; 1715 dvd_struct *s; 1716 { 1717 struct scsipi_generic cmd; 1718 u_int8_t buf[4 + 2048]; 1719 int error; 1720 1721 memset(cmd.bytes, 0, 15); 1722 memset(buf, 0, sizeof(buf)); 1723 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1724 cmd.bytes[6] = s->type; 1725 _lto2b(sizeof(buf), &cmd.bytes[7]); 1726 1727 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1728 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1729 if (error) 1730 return (error); 1731 s->manufact.len = _2btol(&buf[0]); 1732 if (s->manufact.len < 0 || s->manufact.len > 2048) 1733 return (EIO); 1734 memcpy(s->manufact.value, &buf[4], s->manufact.len); 1735 return (0); 1736 } 1737 1738 int 1739 dvd_read_struct(cd, s) 1740 struct cd_softc *cd; 1741 dvd_struct *s; 1742 { 1743 1744 switch (s->type) { 1745 case DVD_STRUCT_PHYSICAL: 1746 return (dvd_read_physical(cd, s)); 1747 case DVD_STRUCT_COPYRIGHT: 1748 return (dvd_read_copyright(cd, s)); 1749 case DVD_STRUCT_DISCKEY: 1750 return (dvd_read_disckey(cd, s)); 1751 case DVD_STRUCT_BCA: 1752 return (dvd_read_bca(cd, s)); 1753 case DVD_STRUCT_MANUFACT: 1754 return (dvd_read_manufact(cd, s)); 1755 default: 1756 return (EINVAL); 1757 } 1758 } 1759