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