1 /* $NetBSD: cd.c,v 1.140 2000/05/30 01:08:23 augustss 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 #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 if (error) 936 return (error); 937 len = min(len, _2btol(data.header.data_len) + 938 sizeof(struct cd_sub_channel_header)); 939 return (copyout(&data, args->data, len)); 940 } 941 case CDIOREADTOCHEADER: { 942 struct ioc_toc_header th; 943 944 if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 0)) != 0) 945 return (error); 946 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 947 #if BYTE_ORDER == BIG_ENDIAN 948 bswap((u_int8_t *)&th.len, sizeof(th.len)); 949 #endif 950 } else 951 th.len = ntohs(th.len); 952 bcopy(&th, addr, sizeof(th)); 953 return (0); 954 } 955 case CDIOREADTOCENTRYS: { 956 struct cd_toc toc; 957 struct ioc_read_toc_entry *te = 958 (struct ioc_read_toc_entry *)addr; 959 struct ioc_toc_header *th; 960 struct cd_toc_entry *cte; 961 int len = te->data_len; 962 int ntracks; 963 964 th = &toc.header; 965 966 if (len > sizeof(toc.entries) || 967 len < sizeof(struct cd_toc_entry)) 968 return (EINVAL); 969 error = cd_read_toc(cd, te->address_format, te->starting_track, 970 &toc, len + sizeof(struct ioc_toc_header), 0); 971 if (error) 972 return (error); 973 if (te->address_format == CD_LBA_FORMAT) 974 for (ntracks = 975 th->ending_track - th->starting_track + 1; 976 ntracks >= 0; ntracks--) { 977 cte = &toc.entries[ntracks]; 978 cte->addr_type = CD_LBA_FORMAT; 979 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 980 #if BYTE_ORDER == BIG_ENDIAN 981 bswap((u_int8_t*)&cte->addr, 982 sizeof(cte->addr)); 983 #endif 984 } else 985 cte->addr.lba = ntohl(cte->addr.lba); 986 } 987 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 988 #if BYTE_ORDER == BIG_ENDIAN 989 bswap((u_int8_t*)&th->len, sizeof(th->len)); 990 #endif 991 } else 992 th->len = ntohs(th->len); 993 len = min(len, th->len - (sizeof(th->starting_track) + 994 sizeof(th->ending_track))); 995 return (copyout(toc.entries, te->data, len)); 996 } 997 case CDIOREADMSADDR: { 998 struct cd_toc toc; 999 int sessno = *(int*)addr; 1000 struct cd_toc_entry *cte; 1001 1002 if (sessno != 0) 1003 return (EINVAL); 1004 1005 error = cd_read_toc(cd, 0, 0, &toc, 1006 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1007 0x40 /* control word for "get MS info" */); 1008 1009 if (error) 1010 return (error); 1011 1012 cte = &toc.entries[0]; 1013 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 1014 #if BYTE_ORDER == BIG_ENDIAN 1015 bswap((u_int8_t*)&cte->addr, sizeof(cte->addr)); 1016 #endif 1017 } else 1018 cte->addr.lba = ntohl(cte->addr.lba); 1019 if (cd->sc_link->quirks & ADEV_LITTLETOC) { 1020 #if BYTE_ORDER == BIG_ENDIAN 1021 bswap((u_int8_t*)&toc.header.len, sizeof(toc.header.len)); 1022 #endif 1023 } else 1024 toc.header.len = ntohs(toc.header.len); 1025 1026 *(int*)addr = (toc.header.len >= 10 && cte->track > 1) ? 1027 cte->addr.lba : 0; 1028 return 0; 1029 } 1030 case CDIOCSETPATCH: { 1031 struct ioc_patch *arg = (struct ioc_patch *)addr; 1032 1033 return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0], 1034 arg->patch[1], arg->patch[2], arg->patch[3], 0)); 1035 } 1036 case CDIOCGETVOL: { 1037 struct ioc_vol *arg = (struct ioc_vol *)addr; 1038 1039 return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0)); 1040 } 1041 case CDIOCSETVOL: { 1042 struct ioc_vol *arg = (struct ioc_vol *)addr; 1043 1044 return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0)); 1045 } 1046 1047 case CDIOCSETMONO: 1048 return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL, 1049 BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1050 1051 case CDIOCSETSTEREO: 1052 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1053 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1054 1055 case CDIOCSETMUTE: 1056 return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL, 1057 MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1058 1059 case CDIOCSETLEFT: 1060 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1061 LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1062 1063 case CDIOCSETRIGHT: 1064 return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL, 1065 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1066 1067 case CDIOCRESUME: 1068 return (cd_pause(cd, PA_RESUME)); 1069 case CDIOCPAUSE: 1070 return (cd_pause(cd, PA_PAUSE)); 1071 case CDIOCSTART: 1072 return (scsipi_start(cd->sc_link, SSS_START, 0)); 1073 case CDIOCSTOP: 1074 return (scsipi_start(cd->sc_link, SSS_STOP, 0)); 1075 case CDIOCCLOSE: 1076 return (scsipi_start(cd->sc_link, SSS_START|SSS_LOEJ, 1077 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1078 case DIOCEJECT: 1079 if (*(int *)addr == 0) { 1080 /* 1081 * Don't force eject: check that we are the only 1082 * partition open. If so, unlock it. 1083 */ 1084 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1085 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1086 cd->sc_dk.dk_openmask) { 1087 error = scsipi_prevent(cd->sc_link, PR_ALLOW, 1088 XS_CTL_IGNORE_NOT_READY); 1089 if (error) 1090 return (error); 1091 } else { 1092 return (EBUSY); 1093 } 1094 } 1095 /* FALLTHROUGH */ 1096 case CDIOCEJECT: /* FALLTHROUGH */ 1097 case ODIOCEJECT: 1098 return (scsipi_start(cd->sc_link, SSS_STOP|SSS_LOEJ, 0)); 1099 case CDIOCALLOW: 1100 return (scsipi_prevent(cd->sc_link, PR_ALLOW, 0)); 1101 case CDIOCPREVENT: 1102 return (scsipi_prevent(cd->sc_link, PR_PREVENT, 0)); 1103 case DIOCLOCK: 1104 return (scsipi_prevent(cd->sc_link, 1105 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1106 case CDIOCSETDEBUG: 1107 cd->sc_link->flags |= (SDEV_DB1 | SDEV_DB2); 1108 return (0); 1109 case CDIOCCLRDEBUG: 1110 cd->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); 1111 return (0); 1112 case CDIOCRESET: 1113 case SCIOCRESET: 1114 return (cd_reset(cd)); 1115 case CDIOCLOADUNLOAD: { 1116 struct ioc_load_unload *args = (struct ioc_load_unload *)addr; 1117 1118 return ((*cd->sc_ops->cdo_load_unload)(cd, args->options, 1119 args->slot)); 1120 case DVD_AUTH: 1121 return (dvd_auth(cd, (dvd_authinfo *)addr)); 1122 case DVD_READ_STRUCT: 1123 return (dvd_read_struct(cd, (dvd_struct *)addr)); 1124 } 1125 1126 default: 1127 if (part != RAW_PART) 1128 return (ENOTTY); 1129 return (scsipi_do_ioctl(cd->sc_link, dev, cmd, addr, flag, p)); 1130 } 1131 1132 #ifdef DIAGNOSTIC 1133 panic("cdioctl: impossible"); 1134 #endif 1135 } 1136 1137 void 1138 cdgetdefaultlabel(cd, lp) 1139 struct cd_softc *cd; 1140 struct disklabel *lp; 1141 { 1142 1143 bzero(lp, sizeof(struct disklabel)); 1144 1145 lp->d_secsize = cd->params.blksize; 1146 lp->d_ntracks = 1; 1147 lp->d_nsectors = 100; 1148 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1149 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1150 1151 switch (cd->sc_link->type) { 1152 #if NCD_SCSIBUS > 0 1153 case BUS_SCSI: 1154 lp->d_type = DTYPE_SCSI; 1155 break; 1156 #endif 1157 #if NCD_ATAPIBUS > 0 1158 case BUS_ATAPI: 1159 lp->d_type = DTYPE_ATAPI; 1160 break; 1161 #endif 1162 } 1163 strncpy(lp->d_typename, cd->name, 16); 1164 strncpy(lp->d_packname, "fictitious", 16); 1165 lp->d_secperunit = cd->params.disksize; 1166 lp->d_rpm = 300; 1167 lp->d_interleave = 1; 1168 lp->d_flags = D_REMOVABLE; 1169 1170 lp->d_partitions[0].p_offset = 0; 1171 lp->d_partitions[0].p_size = 1172 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1173 lp->d_partitions[0].p_fstype = FS_ISO9660; 1174 lp->d_partitions[RAW_PART].p_offset = 0; 1175 lp->d_partitions[RAW_PART].p_size = 1176 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1177 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1178 lp->d_npartitions = RAW_PART + 1; 1179 1180 lp->d_magic = DISKMAGIC; 1181 lp->d_magic2 = DISKMAGIC; 1182 lp->d_checksum = dkcksum(lp); 1183 } 1184 1185 /* 1186 * Load the label information on the named device 1187 * Actually fabricate a disklabel 1188 * 1189 * EVENTUALLY take information about different 1190 * data tracks from the TOC and put it in the disklabel 1191 */ 1192 void 1193 cdgetdisklabel(cd) 1194 struct cd_softc *cd; 1195 { 1196 struct disklabel *lp = cd->sc_dk.dk_label; 1197 1198 bzero(cd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel)); 1199 1200 cdgetdefaultlabel(cd, lp); 1201 } 1202 1203 /* 1204 * Find out from the device what it's capacity is 1205 */ 1206 u_long 1207 cd_size(cd, flags) 1208 struct cd_softc *cd; 1209 int flags; 1210 { 1211 struct scsipi_read_cd_cap_data rdcap; 1212 struct scsipi_read_cd_capacity scsipi_cmd; 1213 int blksize; 1214 u_long size; 1215 1216 if (cd->sc_link->quirks & ADEV_NOCAPACITY) { 1217 /* 1218 * the drive doesn't support the READ_CD_CAPACITY command 1219 * use a fake size 1220 */ 1221 cd->params.blksize = 2048; 1222 cd->params.disksize = 400000; 1223 return (400000); 1224 } 1225 1226 /* 1227 * make up a scsi command and ask the scsi driver to do 1228 * it for you. 1229 */ 1230 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1231 scsipi_cmd.opcode = READ_CD_CAPACITY; 1232 1233 /* 1234 * If the command works, interpret the result as a 4 byte 1235 * number of blocks and a blocksize 1236 */ 1237 if (scsipi_command(cd->sc_link, 1238 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1239 (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL, 1240 flags | XS_CTL_DATA_IN) != 0) 1241 return (0); 1242 1243 blksize = _4btol(rdcap.length); 1244 if ((blksize < 512) || ((blksize & 511) != 0)) 1245 blksize = 2048; /* some drives lie ! */ 1246 cd->params.blksize = blksize; 1247 1248 size = _4btol(rdcap.addr) + 1; 1249 if (size < 100) 1250 size = 400000; /* ditto */ 1251 cd->params.disksize = size; 1252 1253 SC_DEBUG(cd->sc_link, SDEV_DB2, ("cd_size: %d %ld\n", blksize, size)); 1254 return (size); 1255 } 1256 1257 /* 1258 * Get scsi driver to send a "start playing" command 1259 */ 1260 int 1261 cd_play(cd, blkno, nblks) 1262 struct cd_softc *cd; 1263 int blkno, nblks; 1264 { 1265 struct scsipi_play scsipi_cmd; 1266 1267 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1268 scsipi_cmd.opcode = PLAY; 1269 _lto4b(blkno, scsipi_cmd.blk_addr); 1270 _lto2b(nblks, scsipi_cmd.xfer_len); 1271 return (scsipi_command(cd->sc_link, 1272 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1273 0, 0, CDRETRIES, 30000, NULL, 0)); 1274 } 1275 1276 /* 1277 * Get scsi driver to send a "start playing" command 1278 */ 1279 int 1280 cd_play_tracks(cd, strack, sindex, etrack, eindex) 1281 struct cd_softc *cd; 1282 int strack, sindex, etrack, eindex; 1283 { 1284 struct cd_toc toc; 1285 int error; 1286 1287 if (!etrack) 1288 return (EIO); 1289 if (strack > etrack) 1290 return (EINVAL); 1291 1292 if ((error = cd_load_toc(cd, &toc)) != 0) 1293 return (error); 1294 1295 if (++etrack > (toc.header.ending_track+1)) 1296 etrack = toc.header.ending_track+1; 1297 1298 strack -= toc.header.starting_track; 1299 etrack -= toc.header.starting_track; 1300 if (strack < 0) 1301 return (EINVAL); 1302 1303 return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute, 1304 toc.entries[strack].addr.msf.second, 1305 toc.entries[strack].addr.msf.frame, 1306 toc.entries[etrack].addr.msf.minute, 1307 toc.entries[etrack].addr.msf.second, 1308 toc.entries[etrack].addr.msf.frame)); 1309 } 1310 1311 /* 1312 * Get scsi driver to send a "play msf" command 1313 */ 1314 int 1315 cd_play_msf(cd, startm, starts, startf, endm, ends, endf) 1316 struct cd_softc *cd; 1317 int startm, starts, startf, endm, ends, endf; 1318 { 1319 struct scsipi_play_msf scsipi_cmd; 1320 1321 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1322 scsipi_cmd.opcode = PLAY_MSF; 1323 scsipi_cmd.start_m = startm; 1324 scsipi_cmd.start_s = starts; 1325 scsipi_cmd.start_f = startf; 1326 scsipi_cmd.end_m = endm; 1327 scsipi_cmd.end_s = ends; 1328 scsipi_cmd.end_f = endf; 1329 return (scsipi_command(cd->sc_link, 1330 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1331 0, 0, CDRETRIES, 30000, NULL, 0)); 1332 } 1333 1334 /* 1335 * Get scsi driver to send a "start up" command 1336 */ 1337 int 1338 cd_pause(cd, go) 1339 struct cd_softc *cd; 1340 int go; 1341 { 1342 struct scsipi_pause scsipi_cmd; 1343 1344 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1345 scsipi_cmd.opcode = PAUSE; 1346 scsipi_cmd.resume = go & 0xff; 1347 return (scsipi_command(cd->sc_link, 1348 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1349 0, 0, CDRETRIES, 30000, NULL, 0)); 1350 } 1351 1352 /* 1353 * Get scsi driver to send a "RESET" command 1354 */ 1355 int 1356 cd_reset(cd) 1357 struct cd_softc *cd; 1358 { 1359 1360 return (scsipi_command(cd->sc_link, 0, 0, 0, 0, 1361 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1362 } 1363 1364 /* 1365 * Read subchannel 1366 */ 1367 int 1368 cd_read_subchannel(cd, mode, format, track, data, len) 1369 struct cd_softc *cd; 1370 int mode, format, track, len; 1371 struct cd_sub_channel_info *data; 1372 { 1373 struct scsipi_read_subchannel scsipi_cmd; 1374 1375 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1376 scsipi_cmd.opcode = READ_SUBCHANNEL; 1377 if (mode == CD_MSF_FORMAT) 1378 scsipi_cmd.byte2 |= CD_MSF; 1379 scsipi_cmd.byte3 = SRS_SUBQ; 1380 scsipi_cmd.subchan_format = format; 1381 scsipi_cmd.track = track; 1382 _lto2b(len, scsipi_cmd.data_len); 1383 return (scsipi_command(cd->sc_link, 1384 (struct scsipi_generic *)&scsipi_cmd, 1385 sizeof(struct scsipi_read_subchannel), (u_char *)data, len, 1386 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_SILENT)); 1387 } 1388 1389 /* 1390 * Read table of contents 1391 */ 1392 int 1393 cd_read_toc(cd, mode, start, data, len, control) 1394 struct cd_softc *cd; 1395 int mode, start, len, control; 1396 void *data; 1397 { 1398 struct scsipi_read_toc scsipi_cmd; 1399 int ntoc; 1400 1401 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 1402 #if 0 1403 if (len != sizeof(struct ioc_toc_header)) 1404 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 1405 sizeof(struct cd_toc_entry); 1406 else 1407 #endif 1408 ntoc = len; 1409 scsipi_cmd.opcode = READ_TOC; 1410 if (mode == CD_MSF_FORMAT) 1411 scsipi_cmd.byte2 |= CD_MSF; 1412 scsipi_cmd.from_track = start; 1413 _lto2b(ntoc, scsipi_cmd.data_len); 1414 scsipi_cmd.control = control; 1415 return (scsipi_command(cd->sc_link, 1416 (struct scsipi_generic *)&scsipi_cmd, 1417 sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES, 1418 30000, NULL, XS_CTL_DATA_IN)); 1419 } 1420 1421 int 1422 cd_load_toc(cd, toc) 1423 struct cd_softc *cd; 1424 struct cd_toc *toc; 1425 { 1426 int ntracks, len, error; 1427 1428 if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 0)) != 0) 1429 return (error); 1430 1431 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 1432 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 1433 sizeof(toc->header); 1434 if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 0)) != 0) 1435 return (error); 1436 return (0); 1437 } 1438 1439 /* 1440 * Get the scsi driver to send a full inquiry to the device and use the 1441 * results to fill out the disk parameter structure. 1442 */ 1443 int 1444 cd_get_parms(cd, flags) 1445 struct cd_softc *cd; 1446 int flags; 1447 { 1448 1449 /* 1450 * give a number of sectors so that sec * trks * cyls 1451 * is <= disk_size 1452 */ 1453 if (cd_size(cd, flags) == 0) 1454 return (ENXIO); 1455 return (0); 1456 } 1457 1458 int 1459 cdsize(dev) 1460 dev_t dev; 1461 { 1462 1463 /* CD-ROMs are read-only. */ 1464 return (-1); 1465 } 1466 1467 int 1468 cddump(dev, blkno, va, size) 1469 dev_t dev; 1470 daddr_t blkno; 1471 caddr_t va; 1472 size_t size; 1473 { 1474 1475 /* Not implemented. */ 1476 return (ENXIO); 1477 } 1478 1479 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 1480 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 1481 1482 int 1483 dvd_auth(cd, a) 1484 struct cd_softc *cd; 1485 dvd_authinfo *a; 1486 { 1487 struct scsipi_generic cmd; 1488 u_int8_t buf[20]; 1489 int error; 1490 1491 memset(cmd.bytes, 0, 15); 1492 memset(buf, 0, sizeof(buf)); 1493 1494 switch (a->type) { 1495 case DVD_LU_SEND_AGID: 1496 cmd.opcode = GPCMD_REPORT_KEY; 1497 cmd.bytes[8] = 8; 1498 cmd.bytes[9] = 0 | (0 << 6); 1499 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 8, 1500 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1501 if (error) 1502 return (error); 1503 a->lsa.agid = buf[7] >> 6; 1504 return (0); 1505 1506 case DVD_LU_SEND_CHALLENGE: 1507 cmd.opcode = GPCMD_REPORT_KEY; 1508 cmd.bytes[8] = 16; 1509 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 1510 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16, 1511 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1512 if (error) 1513 return (error); 1514 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1515 return (0); 1516 1517 case DVD_LU_SEND_KEY1: 1518 cmd.opcode = GPCMD_REPORT_KEY; 1519 cmd.bytes[8] = 12; 1520 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 1521 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12, 1522 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1523 if (error) 1524 return (error); 1525 dvd_copy_key(a->lsk.key, &buf[4]); 1526 return (0); 1527 1528 case DVD_LU_SEND_TITLE_KEY: 1529 cmd.opcode = GPCMD_REPORT_KEY; 1530 _lto4b(a->lstk.lba, &cmd.bytes[1]); 1531 cmd.bytes[8] = 12; 1532 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 1533 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12, 1534 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1535 if (error) 1536 return (error); 1537 a->lstk.cpm = (buf[4] >> 7) & 1; 1538 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1539 a->lstk.cgms = (buf[4] >> 4) & 3; 1540 dvd_copy_key(a->lstk.title_key, &buf[5]); 1541 return (0); 1542 1543 case DVD_LU_SEND_ASF: 1544 cmd.opcode = GPCMD_REPORT_KEY; 1545 cmd.bytes[8] = 8; 1546 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 1547 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 8, 1548 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1549 if (error) 1550 return (error); 1551 a->lsasf.asf = buf[7] & 1; 1552 return (0); 1553 1554 case DVD_HOST_SEND_CHALLENGE: 1555 cmd.opcode = GPCMD_SEND_KEY; 1556 cmd.bytes[8] = 16; 1557 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 1558 buf[1] = 14; 1559 dvd_copy_challenge(&buf[4], a->hsc.chal); 1560 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16, 1561 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT|XS_CTL_DATA_IN); 1562 if (error) 1563 return (error); 1564 a->type = DVD_LU_SEND_KEY1; 1565 return (0); 1566 1567 case DVD_HOST_SEND_KEY2: 1568 cmd.opcode = GPCMD_SEND_KEY; 1569 cmd.bytes[8] = 12; 1570 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 1571 buf[1] = 10; 1572 dvd_copy_key(&buf[4], a->hsk.key); 1573 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 12, 1574 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT|XS_CTL_DATA_IN); 1575 if (error) { 1576 a->type = DVD_AUTH_FAILURE; 1577 return (error); 1578 } 1579 a->type = DVD_AUTH_ESTABLISHED; 1580 return (0); 1581 1582 case DVD_INVALIDATE_AGID: 1583 cmd.opcode = GPCMD_REPORT_KEY; 1584 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 1585 error = scsipi_command(cd->sc_link, &cmd, 16, buf, 16, 1586 CDRETRIES, 30000, NULL, 0); 1587 if (error) 1588 return (error); 1589 return (0); 1590 1591 default: 1592 return (ENOTTY); 1593 } 1594 } 1595 1596 int 1597 dvd_read_physical(cd, s) 1598 struct cd_softc *cd; 1599 dvd_struct *s; 1600 { 1601 struct scsipi_generic cmd; 1602 u_int8_t buf[4 + 4 * 20], *bufp; 1603 int error; 1604 struct dvd_layer *layer; 1605 int i; 1606 1607 memset(cmd.bytes, 0, 15); 1608 memset(buf, 0, sizeof(buf)); 1609 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1610 cmd.bytes[6] = s->type; 1611 _lto2b(sizeof(buf), &cmd.bytes[7]); 1612 1613 cmd.bytes[5] = s->physical.layer_num; 1614 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1615 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1616 if (error) 1617 return (error); 1618 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4; 1619 i++, bufp += 20, layer++) { 1620 memset(layer, 0, sizeof(*layer)); 1621 layer->book_version = bufp[0] & 0xf; 1622 layer->book_type = bufp[0] >> 4; 1623 layer->min_rate = bufp[1] & 0xf; 1624 layer->disc_size = bufp[1] >> 4; 1625 layer->layer_type = bufp[2] & 0xf; 1626 layer->track_path = (bufp[2] >> 4) & 1; 1627 layer->nlayers = (bufp[2] >> 5) & 3; 1628 layer->track_density = bufp[3] & 0xf; 1629 layer->linear_density = bufp[3] >> 4; 1630 layer->start_sector = _4btol(&bufp[4]); 1631 layer->end_sector = _4btol(&bufp[8]); 1632 layer->end_sector_l0 = _4btol(&bufp[12]); 1633 layer->bca = bufp[16] >> 7; 1634 } 1635 return (0); 1636 } 1637 1638 int 1639 dvd_read_copyright(cd, s) 1640 struct cd_softc *cd; 1641 dvd_struct *s; 1642 { 1643 struct scsipi_generic cmd; 1644 u_int8_t buf[8]; 1645 int error; 1646 1647 memset(cmd.bytes, 0, 15); 1648 memset(buf, 0, sizeof(buf)); 1649 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1650 cmd.bytes[6] = s->type; 1651 _lto2b(sizeof(buf), &cmd.bytes[7]); 1652 1653 cmd.bytes[5] = s->copyright.layer_num; 1654 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1655 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1656 if (error) 1657 return (error); 1658 s->copyright.cpst = buf[4]; 1659 s->copyright.rmi = buf[5]; 1660 return (0); 1661 } 1662 1663 int 1664 dvd_read_disckey(cd, s) 1665 struct cd_softc *cd; 1666 dvd_struct *s; 1667 { 1668 struct scsipi_generic cmd; 1669 u_int8_t buf[4 + 2048]; 1670 int error; 1671 1672 memset(cmd.bytes, 0, 15); 1673 memset(buf, 0, sizeof(buf)); 1674 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1675 cmd.bytes[6] = s->type; 1676 _lto2b(sizeof(buf), &cmd.bytes[7]); 1677 1678 cmd.bytes[9] = s->disckey.agid << 6; 1679 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1680 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1681 if (error) 1682 return (error); 1683 memcpy(s->disckey.value, &buf[4], 2048); 1684 return (0); 1685 } 1686 1687 int 1688 dvd_read_bca(cd, s) 1689 struct cd_softc *cd; 1690 dvd_struct *s; 1691 { 1692 struct scsipi_generic cmd; 1693 u_int8_t buf[4 + 188]; 1694 int error; 1695 1696 memset(cmd.bytes, 0, 15); 1697 memset(buf, 0, sizeof(buf)); 1698 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1699 cmd.bytes[6] = s->type; 1700 _lto2b(sizeof(buf), &cmd.bytes[7]); 1701 1702 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1703 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1704 if (error) 1705 return (error); 1706 s->bca.len = _2btol(&buf[0]); 1707 if (s->bca.len < 12 || s->bca.len > 188) 1708 return (EIO); 1709 memcpy(s->bca.value, &buf[4], s->bca.len); 1710 return (0); 1711 } 1712 1713 int 1714 dvd_read_manufact(cd, s) 1715 struct cd_softc *cd; 1716 dvd_struct *s; 1717 { 1718 struct scsipi_generic cmd; 1719 u_int8_t buf[4 + 2048]; 1720 int error; 1721 1722 memset(cmd.bytes, 0, 15); 1723 memset(buf, 0, sizeof(buf)); 1724 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1725 cmd.bytes[6] = s->type; 1726 _lto2b(sizeof(buf), &cmd.bytes[7]); 1727 1728 error = scsipi_command(cd->sc_link, &cmd, 16, buf, sizeof(buf), 1729 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 1730 if (error) 1731 return (error); 1732 s->manufact.len = _2btol(&buf[0]); 1733 if (s->manufact.len < 0 || s->manufact.len > 2048) 1734 return (EIO); 1735 memcpy(s->manufact.value, &buf[4], s->manufact.len); 1736 return (0); 1737 } 1738 1739 int 1740 dvd_read_struct(cd, s) 1741 struct cd_softc *cd; 1742 dvd_struct *s; 1743 { 1744 1745 switch (s->type) { 1746 case DVD_STRUCT_PHYSICAL: 1747 return (dvd_read_physical(cd, s)); 1748 case DVD_STRUCT_COPYRIGHT: 1749 return (dvd_read_copyright(cd, s)); 1750 case DVD_STRUCT_DISCKEY: 1751 return (dvd_read_disckey(cd, s)); 1752 case DVD_STRUCT_BCA: 1753 return (dvd_read_bca(cd, s)); 1754 case DVD_STRUCT_MANUFACT: 1755 return (dvd_read_manufact(cd, s)); 1756 default: 1757 return (EINVAL); 1758 } 1759 } 1760