1 /* $NetBSD: cd.c,v 1.162 2002/05/05 15:16:31 bouyer Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2001 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 <sys/cdefs.h> 57 __KERNEL_RCSID(0, "$NetBSD: cd.c,v 1.162 2002/05/05 15:16:31 bouyer Exp $"); 58 59 #include "rnd.h" 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/kernel.h> 64 #include <sys/file.h> 65 #include <sys/stat.h> 66 #include <sys/ioctl.h> 67 #include <sys/buf.h> 68 #include <sys/uio.h> 69 #include <sys/malloc.h> 70 #include <sys/errno.h> 71 #include <sys/device.h> 72 #include <sys/disklabel.h> 73 #include <sys/disk.h> 74 #include <sys/cdio.h> 75 #include <sys/dvdio.h> 76 #include <sys/scsiio.h> 77 #include <sys/proc.h> 78 #include <sys/conf.h> 79 #include <sys/vnode.h> 80 #if NRND > 0 81 #include <sys/rnd.h> 82 #endif 83 84 #include <dev/scsipi/scsipi_all.h> 85 #include <dev/scsipi/scsipi_cd.h> 86 #include <dev/scsipi/scsipi_disk.h> /* rw_big and start_stop come */ 87 /* from there */ 88 #include <dev/scsipi/scsi_disk.h> /* rw comes from there */ 89 #include <dev/scsipi/scsipiconf.h> 90 #include <dev/scsipi/cdvar.h> 91 92 #include "cd.h" /* NCD_SCSIBUS and NCD_ATAPIBUS come from here */ 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((struct scsipi_periph *)); 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 void cdbounce __P((struct buf *)); 118 int cd_interpret_sense __P((struct scsipi_xfer *)); 119 u_long cd_size __P((struct cd_softc *, int)); 120 void lba2msf __P((u_long, u_char *, u_char *, u_char *)); 121 u_long msf2lba __P((u_char, u_char, u_char)); 122 int cd_play __P((struct cd_softc *, int, int)); 123 int cd_play_tracks __P((struct cd_softc *, int, int, int, int)); 124 int cd_play_msf __P((struct cd_softc *, int, int, int, int, int, int)); 125 int cd_pause __P((struct cd_softc *, int)); 126 int cd_reset __P((struct cd_softc *)); 127 int cd_read_subchannel __P((struct cd_softc *, int, int, int, 128 struct cd_sub_channel_info *, int, int)); 129 int cd_read_toc __P((struct cd_softc *, int, int, void *, int, int, int)); 130 int cd_get_parms __P((struct cd_softc *, int)); 131 int cd_load_toc __P((struct cd_softc *, struct cd_toc *, int)); 132 int dvd_auth __P((struct cd_softc *, dvd_authinfo *)); 133 int dvd_read_physical __P((struct cd_softc *, dvd_struct *)); 134 int dvd_read_copyright __P((struct cd_softc *, dvd_struct *)); 135 int dvd_read_disckey __P((struct cd_softc *, dvd_struct *)); 136 int dvd_read_bca __P((struct cd_softc *, dvd_struct *)); 137 int dvd_read_manufact __P((struct cd_softc *, dvd_struct *)); 138 int dvd_read_struct __P((struct cd_softc *, dvd_struct *)); 139 140 extern struct cfdriver cd_cd; 141 142 struct dkdriver cddkdriver = { cdstrategy }; 143 144 const struct scsipi_periphsw cd_switch = { 145 cd_interpret_sense, /* use our error handler first */ 146 cdstart, /* we have a queue, which is started by this */ 147 NULL, /* we do not have an async handler */ 148 cddone, /* deal with stats at interrupt time */ 149 }; 150 151 /* 152 * The routine called by the low level scsi routine when it discovers 153 * A device suitable for this driver 154 */ 155 void 156 cdattach(parent, cd, periph, ops) 157 struct device *parent; 158 struct cd_softc *cd; 159 struct scsipi_periph *periph; 160 const struct cd_ops *ops; 161 { 162 SC_DEBUG(periph, SCSIPI_DB2, ("cdattach: ")); 163 164 BUFQ_INIT(&cd->buf_queue); 165 166 /* 167 * Store information needed to contact our base driver 168 */ 169 cd->sc_periph = periph; 170 cd->sc_ops = ops; 171 172 periph->periph_dev = &cd->sc_dev; 173 periph->periph_switch = &cd_switch; 174 175 /* 176 * Increase our openings to the maximum-per-periph 177 * supported by the adapter. This will either be 178 * clamped down or grown by the adapter if necessary. 179 */ 180 periph->periph_openings = 181 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel); 182 periph->periph_flags |= PERIPH_GROW_OPENINGS; 183 184 /* 185 * Initialize and attach the disk structure. 186 */ 187 cd->sc_dk.dk_driver = &cddkdriver; 188 cd->sc_dk.dk_name = cd->sc_dev.dv_xname; 189 disk_attach(&cd->sc_dk); 190 191 #ifdef __BROKEN_DK_ESTABLISH 192 dk_establish(&cd->sc_dk, &cd->sc_dev); /* XXX */ 193 #endif 194 195 printf("\n"); 196 197 #if NRND > 0 198 rnd_attach_source(&cd->rnd_source, cd->sc_dev.dv_xname, 199 RND_TYPE_DISK, 0); 200 #endif 201 } 202 203 int 204 cdactivate(self, act) 205 struct device *self; 206 enum devact act; 207 { 208 int rv = 0; 209 210 switch (act) { 211 case DVACT_ACTIVATE: 212 rv = EOPNOTSUPP; 213 break; 214 215 case DVACT_DEACTIVATE: 216 /* 217 * Nothing to do; we key off the device's DVF_ACTIVE. 218 */ 219 break; 220 } 221 return (rv); 222 } 223 224 int 225 cddetach(self, flags) 226 struct device *self; 227 int flags; 228 { 229 struct cd_softc *cd = (struct cd_softc *) self; 230 struct buf *bp; 231 int s, bmaj, cmaj, i, mn; 232 233 /* locate the major number */ 234 for (bmaj = 0; bmaj <= nblkdev; bmaj++) 235 if (bdevsw[bmaj].d_open == cdopen) 236 break; 237 for (cmaj = 0; cmaj <= nchrdev; cmaj++) 238 if (cdevsw[cmaj].d_open == cdopen) 239 break; 240 241 s = splbio(); 242 243 /* Kill off any queued buffers. */ 244 while ((bp = BUFQ_FIRST(&cd->buf_queue)) != NULL) { 245 BUFQ_REMOVE(&cd->buf_queue, bp); 246 bp->b_error = EIO; 247 bp->b_flags |= B_ERROR; 248 bp->b_resid = bp->b_bcount; 249 biodone(bp); 250 } 251 252 /* Kill off any pending commands. */ 253 scsipi_kill_pending(cd->sc_periph); 254 255 splx(s); 256 257 /* Nuke the vnodes for any open instances */ 258 for (i = 0; i < MAXPARTITIONS; i++) { 259 mn = CDMINOR(self->dv_unit, i); 260 vdevgone(bmaj, mn, mn, VBLK); 261 vdevgone(cmaj, mn, mn, VCHR); 262 } 263 264 /* Detach from the disk list. */ 265 disk_detach(&cd->sc_dk); 266 267 #if 0 268 /* Get rid of the shutdown hook. */ 269 if (cd->sc_sdhook != NULL) 270 shutdownhook_disestablish(cd->sc_sdhook); 271 #endif 272 273 #if NRND > 0 274 /* Unhook the entropy source. */ 275 rnd_detach_source(&cd->rnd_source); 276 #endif 277 278 return (0); 279 } 280 281 /* 282 * Wait interruptibly for an exclusive lock. 283 * 284 * XXX 285 * Several drivers do this; it should be abstracted and made MP-safe. 286 */ 287 int 288 cdlock(cd) 289 struct cd_softc *cd; 290 { 291 int error; 292 293 while ((cd->flags & CDF_LOCKED) != 0) { 294 cd->flags |= CDF_WANTED; 295 if ((error = tsleep(cd, PRIBIO | PCATCH, "cdlck", 0)) != 0) 296 return (error); 297 } 298 cd->flags |= CDF_LOCKED; 299 return (0); 300 } 301 302 /* 303 * Unlock and wake up any waiters. 304 */ 305 void 306 cdunlock(cd) 307 struct cd_softc *cd; 308 { 309 310 cd->flags &= ~CDF_LOCKED; 311 if ((cd->flags & CDF_WANTED) != 0) { 312 cd->flags &= ~CDF_WANTED; 313 wakeup(cd); 314 } 315 } 316 317 /* 318 * open the device. Make sure the partition info is a up-to-date as can be. 319 */ 320 int 321 cdopen(dev, flag, fmt, p) 322 dev_t dev; 323 int flag, fmt; 324 struct proc *p; 325 { 326 struct cd_softc *cd; 327 struct scsipi_periph *periph; 328 struct scsipi_adapter *adapt; 329 struct cd_sub_channel_info data; 330 int unit, part; 331 int error; 332 333 unit = CDUNIT(dev); 334 if (unit >= cd_cd.cd_ndevs) 335 return (ENXIO); 336 cd = cd_cd.cd_devs[unit]; 337 if (cd == NULL) 338 return (ENXIO); 339 340 periph = cd->sc_periph; 341 adapt = periph->periph_channel->chan_adapter; 342 part = CDPART(dev); 343 344 SC_DEBUG(periph, SCSIPI_DB1, 345 ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 346 cd_cd.cd_ndevs, CDPART(dev))); 347 348 /* 349 * If this is the first open of this device, add a reference 350 * to the adapter. 351 */ 352 if (cd->sc_dk.dk_openmask == 0 && 353 (error = scsipi_adapter_addref(adapt)) != 0) 354 return (error); 355 356 if ((error = cdlock(cd)) != 0) 357 goto bad4; 358 359 if ((periph->periph_flags & PERIPH_OPEN) != 0) { 360 /* 361 * If any partition is open, but the disk has been invalidated, 362 * disallow further opens. 363 */ 364 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 365 (part != RAW_PART || fmt != S_IFCHR )) { 366 error = EIO; 367 goto bad3; 368 } 369 } else { 370 /* Check that it is still responding and ok. */ 371 error = scsipi_test_unit_ready(periph, 372 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 373 XS_CTL_SILENT_NODEV); 374 SC_DEBUG(periph, SCSIPI_DB1, 375 ("cdopen: scsipi_test_unit_ready, error=%d\n", error)); 376 if (error) { 377 if (part != RAW_PART || fmt != S_IFCHR) 378 goto bad3; 379 else 380 goto out; 381 } 382 383 /* Don't try to start the unit if audio is playing. */ 384 error = cd_read_subchannel(cd, CD_LBA_FORMAT, 385 CD_CURRENT_POSITION, 0, &data, sizeof(data), 386 XS_CTL_DATA_ONSTACK); 387 if ((data.header.audio_status != CD_AS_PLAY_IN_PROGRESS && 388 data.header.audio_status != CD_AS_PLAY_PAUSED) || error) { 389 /* 390 * Start the pack spinning if necessary. Always 391 * allow the raw parition to be opened, for raw 392 * IOCTLs. Data transfers will check for 393 * SDEV_MEDIA_LOADED. 394 */ 395 error = scsipi_start(periph, SSS_START, 396 XS_CTL_IGNORE_ILLEGAL_REQUEST | 397 XS_CTL_IGNORE_MEDIA_CHANGE | 398 XS_CTL_SILENT); 399 SC_DEBUG(periph, SCSIPI_DB1, 400 ("cdopen: scsipi_start, error=%d\n", error)); 401 if (error) { 402 if (part != RAW_PART || fmt != S_IFCHR) 403 goto bad3; 404 else 405 goto out; 406 } 407 } 408 409 periph->periph_flags |= PERIPH_OPEN; 410 411 /* Lock the pack in. */ 412 error = scsipi_prevent(periph, PR_PREVENT, 413 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 414 SC_DEBUG(periph, SCSIPI_DB1, 415 ("cdopen: scsipi_prevent, error=%d\n", error)); 416 if (error) 417 goto bad; 418 419 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 420 periph->periph_flags |= PERIPH_MEDIA_LOADED; 421 422 /* Load the physical device parameters. */ 423 if (cd_get_parms(cd, 0) != 0) { 424 error = ENXIO; 425 goto bad2; 426 } 427 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 428 429 /* Fabricate a disk label. */ 430 cdgetdisklabel(cd); 431 SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel fabricated ")); 432 } 433 } 434 435 /* Check that the partition exists. */ 436 if (part != RAW_PART && 437 (part >= cd->sc_dk.dk_label->d_npartitions || 438 cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 439 error = ENXIO; 440 goto bad; 441 } 442 443 out: /* Insure only one open at a time. */ 444 switch (fmt) { 445 case S_IFCHR: 446 cd->sc_dk.dk_copenmask |= (1 << part); 447 break; 448 case S_IFBLK: 449 cd->sc_dk.dk_bopenmask |= (1 << part); 450 break; 451 } 452 cd->sc_dk.dk_openmask = 453 cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 454 455 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 456 cdunlock(cd); 457 return (0); 458 459 bad2: 460 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 461 462 bad: 463 if (cd->sc_dk.dk_openmask == 0) { 464 scsipi_prevent(periph, PR_ALLOW, 465 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 466 periph->periph_flags &= ~PERIPH_OPEN; 467 } 468 469 bad3: 470 cdunlock(cd); 471 bad4: 472 if (cd->sc_dk.dk_openmask == 0) 473 scsipi_adapter_delref(adapt); 474 return (error); 475 } 476 477 /* 478 * close the device.. only called if we are the LAST 479 * occurence of an open device 480 */ 481 int 482 cdclose(dev, flag, fmt, p) 483 dev_t dev; 484 int flag, fmt; 485 struct proc *p; 486 { 487 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 488 struct scsipi_periph *periph = cd->sc_periph; 489 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 490 int part = CDPART(dev); 491 int error; 492 493 if ((error = cdlock(cd)) != 0) 494 return (error); 495 496 switch (fmt) { 497 case S_IFCHR: 498 cd->sc_dk.dk_copenmask &= ~(1 << part); 499 break; 500 case S_IFBLK: 501 cd->sc_dk.dk_bopenmask &= ~(1 << part); 502 break; 503 } 504 cd->sc_dk.dk_openmask = 505 cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 506 507 if (cd->sc_dk.dk_openmask == 0) { 508 scsipi_wait_drain(periph); 509 510 scsipi_prevent(periph, PR_ALLOW, 511 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 512 XS_CTL_IGNORE_NOT_READY); 513 periph->periph_flags &= ~PERIPH_OPEN; 514 515 scsipi_wait_drain(periph); 516 517 scsipi_adapter_delref(adapt); 518 } 519 520 cdunlock(cd); 521 return (0); 522 } 523 524 /* 525 * Actually translate the requested transfer into one the physical driver can 526 * understand. The transfer is described by a buf and will include only one 527 * physical transfer. 528 */ 529 void 530 cdstrategy(bp) 531 struct buf *bp; 532 { 533 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 534 struct disklabel *lp; 535 struct scsipi_periph *periph = cd->sc_periph; 536 daddr_t blkno; 537 int s; 538 539 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdstrategy ")); 540 SC_DEBUG(cd->sc_periph, SCSIPI_DB1, 541 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 542 /* 543 * If the device has been made invalid, error out 544 * maybe the media changed 545 */ 546 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 547 if (periph->periph_flags & PERIPH_OPEN) 548 bp->b_error = EIO; 549 else 550 bp->b_error = ENODEV; 551 goto bad; 552 } 553 554 lp = cd->sc_dk.dk_label; 555 556 /* 557 * The transfer must be a whole number of blocks, offset must not 558 * be negative. 559 */ 560 if ((bp->b_bcount % lp->d_secsize) != 0 || 561 bp->b_blkno < 0 ) { 562 bp->b_error = EINVAL; 563 goto bad; 564 } 565 /* 566 * If it's a null transfer, return immediately 567 */ 568 if (bp->b_bcount == 0) 569 goto done; 570 571 /* 572 * Do bounds checking, adjust transfer. if error, process. 573 * If end of partition, just return. 574 */ 575 if (CDPART(bp->b_dev) != RAW_PART && 576 bounds_check_with_label(bp, lp, 577 (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0) 578 goto done; 579 580 /* 581 * Now convert the block number to absolute and put it in 582 * terms of the device's logical block size. 583 */ 584 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 585 if (CDPART(bp->b_dev) != RAW_PART) 586 blkno += lp->d_partitions[CDPART(bp->b_dev)].p_offset; 587 588 bp->b_rawblkno = blkno; 589 590 /* 591 * If the disklabel sector size does not match the device 592 * sector size we may need to do some extra work. 593 */ 594 if (lp->d_secsize != cd->params.blksize) { 595 596 /* 597 * If the xfer is not a multiple of the device block size 598 * or it is not block aligned, we need to bounce it. 599 */ 600 if ((bp->b_bcount % cd->params.blksize) != 0 || 601 ((blkno * lp->d_secsize) % cd->params.blksize) != 0) { 602 struct buf *nbp; 603 void *bounce = NULL; 604 long count; 605 606 if ((bp->b_flags & B_READ) == 0) { 607 608 /* XXXX We don't support bouncing writes. */ 609 bp->b_error = EACCES; 610 goto bad; 611 } 612 count = ((blkno * lp->d_secsize) % cd->params.blksize); 613 /* XXX Store starting offset in bp->b_rawblkno */ 614 bp->b_rawblkno = count; 615 616 count += bp->b_bcount; 617 count = roundup(count, cd->params.blksize); 618 619 blkno = ((blkno * lp->d_secsize) / cd->params.blksize); 620 s = splbio(); 621 nbp = pool_get(&bufpool, PR_NOWAIT); 622 splx(s); 623 if (!nbp) { 624 /* No memory -- fail the iop. */ 625 bp->b_error = ENOMEM; 626 goto bad; 627 } 628 bounce = malloc(count, M_DEVBUF, M_NOWAIT); 629 if (!bounce) { 630 /* No memory -- fail the iop. */ 631 s = splbio(); 632 pool_put(&bufpool, nbp); 633 splx(s); 634 bp->b_error = ENOMEM; 635 goto bad; 636 } 637 638 /* Set up the IOP to the bounce buffer. */ 639 nbp->b_error = 0; 640 nbp->b_proc = bp->b_proc; 641 nbp->b_vp = NULLVP; 642 643 nbp->b_bcount = count; 644 nbp->b_bufsize = count; 645 nbp->b_data = bounce; 646 647 LIST_INIT(&nbp->b_dep); 648 nbp->b_rawblkno = blkno; 649 650 /* We need to do a read-modify-write operation */ 651 nbp->b_flags = bp->b_flags | B_READ | B_CALL; 652 nbp->b_iodone = cdbounce; 653 654 /* Put ptr to orig buf in b_private and use new buf */ 655 nbp->b_private = bp; 656 bp = nbp; 657 658 } else { 659 /* Xfer is aligned -- just adjust the start block */ 660 bp->b_rawblkno = (blkno * lp->d_secsize) / 661 cd->params.blksize; 662 } 663 } 664 s = splbio(); 665 666 /* 667 * Place it in the queue of disk activities for this disk. 668 * 669 * XXX Only do disksort() if the current operating mode does not 670 * XXX include tagged queueing. 671 */ 672 disksort_blkno(&cd->buf_queue, bp); 673 674 /* 675 * Tell the device to get going on the transfer if it's 676 * not doing anything, otherwise just wait for completion 677 */ 678 cdstart(cd->sc_periph); 679 680 splx(s); 681 return; 682 683 bad: 684 bp->b_flags |= B_ERROR; 685 done: 686 /* 687 * Correctly set the buf to indicate a completed xfer 688 */ 689 bp->b_resid = bp->b_bcount; 690 biodone(bp); 691 } 692 693 /* 694 * cdstart looks to see if there is a buf waiting for the device 695 * and that the device is not already busy. If both are true, 696 * It deques the buf and creates a scsi command to perform the 697 * transfer in the buf. The transfer request will call scsipi_done 698 * on completion, which will in turn call this routine again 699 * so that the next queued transfer is performed. 700 * The bufs are queued by the strategy routine (cdstrategy) 701 * 702 * This routine is also called after other non-queued requests 703 * have been made of the scsi driver, to ensure that the queue 704 * continues to be drained. 705 * 706 * must be called at the correct (highish) spl level 707 * cdstart() is called at splbio from cdstrategy and scsipi_done 708 */ 709 void 710 cdstart(periph) 711 struct scsipi_periph *periph; 712 { 713 struct cd_softc *cd = (void *)periph->periph_dev; 714 struct buf *bp = 0; 715 struct scsipi_rw_big cmd_big; 716 #if NCD_SCSIBUS > 0 717 struct scsi_rw cmd_small; 718 #endif 719 struct scsipi_generic *cmdp; 720 int flags, nblks, cmdlen, error; 721 722 SC_DEBUG(periph, SCSIPI_DB2, ("cdstart ")); 723 /* 724 * Check if the device has room for another command 725 */ 726 while (periph->periph_active < periph->periph_openings) { 727 /* 728 * there is excess capacity, but a special waits 729 * It'll need the adapter as soon as we clear out of the 730 * way and let it run (user level wait). 731 */ 732 if (periph->periph_flags & PERIPH_WAITING) { 733 periph->periph_flags &= ~PERIPH_WAITING; 734 wakeup((caddr_t)periph); 735 return; 736 } 737 738 /* 739 * See if there is a buf with work for us to do.. 740 */ 741 if ((bp = BUFQ_FIRST(&cd->buf_queue)) == NULL) 742 return; 743 BUFQ_REMOVE(&cd->buf_queue, bp); 744 745 /* 746 * If the device has become invalid, abort all the 747 * reads and writes until all files have been closed and 748 * re-opened 749 */ 750 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 751 bp->b_error = EIO; 752 bp->b_flags |= B_ERROR; 753 bp->b_resid = bp->b_bcount; 754 biodone(bp); 755 continue; 756 } 757 758 /* 759 * We have a buf, now we should make a command. 760 */ 761 762 nblks = howmany(bp->b_bcount, cd->params.blksize); 763 764 #if NCD_SCSIBUS > 0 765 /* 766 * Fill out the scsi command. If the transfer will 767 * fit in a "small" cdb, use it. 768 */ 769 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 770 ((nblks & 0xff) == nblks) && 771 !(periph->periph_quirks & PQUIRK_ONLYBIG) && 772 scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_SCSI) { 773 /* 774 * We can fit in a small cdb. 775 */ 776 memset(&cmd_small, 0, sizeof(cmd_small)); 777 cmd_small.opcode = (bp->b_flags & B_READ) ? 778 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND; 779 _lto3b(bp->b_rawblkno, cmd_small.addr); 780 cmd_small.length = nblks & 0xff; 781 cmdlen = sizeof(cmd_small); 782 cmdp = (struct scsipi_generic *)&cmd_small; 783 } else 784 #endif 785 { 786 /* 787 * Need a large cdb. 788 */ 789 memset(&cmd_big, 0, sizeof(cmd_big)); 790 cmd_big.opcode = (bp->b_flags & B_READ) ? 791 READ_BIG : WRITE_BIG; 792 _lto4b(bp->b_rawblkno, cmd_big.addr); 793 _lto2b(nblks, cmd_big.length); 794 cmdlen = sizeof(cmd_big); 795 cmdp = (struct scsipi_generic *)&cmd_big; 796 } 797 798 /* Instrumentation. */ 799 disk_busy(&cd->sc_dk); 800 801 /* 802 * Figure out what flags to use. 803 */ 804 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC; 805 if (bp->b_flags & B_READ) 806 flags |= XS_CTL_DATA_IN; 807 else 808 flags |= XS_CTL_DATA_OUT; 809 if (bp->b_flags & B_ORDERED) 810 flags |= XS_CTL_ORDERED_TAG; 811 else 812 flags |= XS_CTL_SIMPLE_TAG; 813 814 /* 815 * Call the routine that chats with the adapter. 816 * Note: we cannot sleep as we may be an interrupt 817 */ 818 error = scsipi_command(periph, cmdp, cmdlen, 819 (u_char *)bp->b_data, bp->b_bcount, 820 CDRETRIES, 30000, bp, flags); 821 if (error) { 822 disk_unbusy(&cd->sc_dk, 0); 823 printf("%s: not queued, error %d\n", 824 cd->sc_dev.dv_xname, error); 825 } 826 } 827 } 828 829 void 830 cddone(xs) 831 struct scsipi_xfer *xs; 832 { 833 struct cd_softc *cd = (void *)xs->xs_periph->periph_dev; 834 835 if (xs->bp != NULL) { 836 disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid); 837 #if NRND > 0 838 rnd_add_uint32(&cd->rnd_source, xs->bp->b_rawblkno); 839 #endif 840 } 841 } 842 843 void 844 cdbounce(bp) 845 struct buf *bp; 846 { 847 struct buf *obp = (struct buf *)bp->b_private; 848 849 if (bp->b_flags & B_ERROR) { 850 /* EEK propagate the error and free the memory */ 851 goto done; 852 } 853 if (obp->b_flags & B_READ) { 854 /* Copy data to the final destination and free the buf. */ 855 memcpy(obp->b_data, bp->b_data+obp->b_rawblkno, 856 obp->b_bcount); 857 } else { 858 /* 859 * XXXX This is a CD-ROM -- READ ONLY -- why do we bother with 860 * XXXX any of this write stuff? 861 */ 862 if (bp->b_flags & B_READ) { 863 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 864 struct buf *nbp; 865 int s; 866 867 /* Read part of RMW complete. */ 868 memcpy(bp->b_data+obp->b_rawblkno, obp->b_data, 869 obp->b_bcount); 870 871 s = splbio(); 872 873 /* We need to alloc a new buf. */ 874 nbp = pool_get(&bufpool, PR_NOWAIT); 875 if (!nbp) { 876 splx(s); 877 /* No buf available. */ 878 bp->b_flags |= B_ERROR; 879 bp->b_error = ENOMEM; 880 bp->b_resid = bp->b_bcount; 881 } 882 883 /* Set up the IOP to the bounce buffer. */ 884 nbp->b_error = 0; 885 nbp->b_proc = bp->b_proc; 886 nbp->b_vp = NULLVP; 887 888 nbp->b_bcount = bp->b_bcount; 889 nbp->b_bufsize = bp->b_bufsize; 890 nbp->b_data = bp->b_data; 891 892 LIST_INIT(&nbp->b_dep); 893 nbp->b_rawblkno = bp->b_rawblkno; 894 895 /* We need to do a read-modify-write operation */ 896 nbp->b_flags = obp->b_flags | B_CALL; 897 nbp->b_iodone = cdbounce; 898 899 /* Put ptr to orig buf in b_private and use new buf */ 900 nbp->b_private = obp; 901 902 /* 903 * Place it in the queue of disk activities for this 904 * disk. 905 * 906 * XXX Only do disksort() if the current operating mode 907 * XXX does not include tagged queueing. 908 */ 909 disksort_blkno(&cd->buf_queue, nbp); 910 911 /* 912 * Tell the device to get going on the transfer if it's 913 * not doing anything, otherwise just wait for 914 * completion 915 */ 916 cdstart(cd->sc_periph); 917 918 splx(s); 919 return; 920 921 } 922 } 923 done: 924 obp->b_flags |= (bp->b_flags&(B_EINTR|B_ERROR)); 925 obp->b_error = bp->b_error; 926 obp->b_resid = bp->b_resid; 927 free(bp->b_data, M_DEVBUF); 928 biodone(obp); 929 } 930 931 int cd_interpret_sense(xs) 932 struct scsipi_xfer *xs; 933 { 934 struct scsipi_periph *periph = xs->xs_periph; 935 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 936 int retval = EJUSTRETURN; 937 938 /* 939 * If it isn't a extended or extended/deferred error, let 940 * the generic code handle it. 941 */ 942 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 943 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFERRED */ 944 return (retval); 945 } 946 947 /* 948 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit 949 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then 950 * wait a bit for the drive to spin up 951 */ 952 953 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 954 sense->add_sense_code == 0x4 && 955 sense->add_sense_code_qual == 0x01) { 956 /* 957 * Sleep for 5 seconds to wait for the drive to spin up 958 */ 959 960 SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD " 961 "spinup\n")); 962 if (!callout_active(&periph->periph_callout)) 963 scsipi_periph_freeze(periph, 1); 964 callout_reset(&periph->periph_callout, 965 5 * hz, scsipi_periph_timed_thaw, periph); 966 retval = ERESTART; 967 } 968 return (retval); 969 } 970 971 void 972 cdminphys(bp) 973 struct buf *bp; 974 { 975 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 976 long max; 977 978 /* 979 * If the device is ancient, we want to make sure that 980 * the transfer fits into a 6-byte cdb. 981 * 982 * XXX Note that the SCSI-I spec says that 256-block transfers 983 * are allowed in a 6-byte read/write, and are specified 984 * by settng the "length" to 0. However, we're conservative 985 * here, allowing only 255-block transfers in case an 986 * ancient device gets confused by length == 0. A length of 0 987 * in a 10-byte read/write actually means 0 blocks. 988 */ 989 if (cd->flags & CDF_ANCIENT) { 990 max = cd->sc_dk.dk_label->d_secsize * 0xff; 991 992 if (bp->b_bcount > max) 993 bp->b_bcount = max; 994 } 995 996 (*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp); 997 } 998 999 int 1000 cdread(dev, uio, ioflag) 1001 dev_t dev; 1002 struct uio *uio; 1003 int ioflag; 1004 { 1005 1006 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 1007 } 1008 1009 int 1010 cdwrite(dev, uio, ioflag) 1011 dev_t dev; 1012 struct uio *uio; 1013 int ioflag; 1014 { 1015 1016 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 1017 } 1018 1019 /* 1020 * conversion between minute-seconde-frame and logical block adress 1021 * adresses format 1022 */ 1023 void 1024 lba2msf (lba, m, s, f) 1025 u_long lba; 1026 u_char *m, *s, *f; 1027 { 1028 u_long tmp; 1029 1030 tmp = lba + CD_BLOCK_OFFSET; /* offset of first logical frame */ 1031 tmp &= 0xffffff; /* negative lbas use only 24 bits */ 1032 *m = tmp / (CD_SECS * CD_FRAMES); 1033 tmp %= (CD_SECS * CD_FRAMES); 1034 *s = tmp / CD_FRAMES; 1035 *f = tmp % CD_FRAMES; 1036 } 1037 1038 u_long 1039 msf2lba (m, s, f) 1040 u_char m, s, f; 1041 { 1042 1043 return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET); 1044 } 1045 1046 1047 /* 1048 * Perform special action on behalf of the user. 1049 * Knows about the internals of this device 1050 */ 1051 int 1052 cdioctl(dev, cmd, addr, flag, p) 1053 dev_t dev; 1054 u_long cmd; 1055 caddr_t addr; 1056 int flag; 1057 struct proc *p; 1058 { 1059 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 1060 struct scsipi_periph *periph = cd->sc_periph; 1061 int part = CDPART(dev); 1062 int error; 1063 #ifdef __HAVE_OLD_DISKLABEL 1064 struct disklabel newlabel; 1065 #endif 1066 1067 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd)); 1068 1069 /* 1070 * If the device is not valid, some IOCTLs can still be 1071 * handled on the raw partition. Check this here. 1072 */ 1073 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 1074 switch (cmd) { 1075 case DIOCWLABEL: 1076 case DIOCLOCK: 1077 case ODIOCEJECT: 1078 case DIOCEJECT: 1079 case SCIOCIDENTIFY: 1080 case OSCIOCIDENTIFY: 1081 case SCIOCCOMMAND: 1082 case SCIOCDEBUG: 1083 case CDIOCGETVOL: 1084 case CDIOCSETVOL: 1085 case CDIOCSETMONO: 1086 case CDIOCSETSTEREO: 1087 case CDIOCSETMUTE: 1088 case CDIOCSETLEFT: 1089 case CDIOCSETRIGHT: 1090 case CDIOCCLOSE: 1091 case CDIOCEJECT: 1092 case CDIOCALLOW: 1093 case CDIOCPREVENT: 1094 case CDIOCSETDEBUG: 1095 case CDIOCCLRDEBUG: 1096 case CDIOCRESET: 1097 case SCIOCRESET: 1098 case CDIOCLOADUNLOAD: 1099 case DVD_AUTH: 1100 case DVD_READ_STRUCT: 1101 if (part == RAW_PART) 1102 break; 1103 /* FALLTHROUGH */ 1104 default: 1105 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1106 return (ENODEV); 1107 else 1108 return (EIO); 1109 } 1110 } 1111 1112 switch (cmd) { 1113 case DIOCGDINFO: 1114 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 1115 return (0); 1116 #ifdef __HAVE_OLD_DISKLABEL 1117 case ODIOCGDINFO: 1118 newlabel = *(cd->sc_dk.dk_label); 1119 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 1120 return ENOTTY; 1121 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 1122 return (0); 1123 #endif 1124 1125 case DIOCGPART: 1126 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 1127 ((struct partinfo *)addr)->part = 1128 &cd->sc_dk.dk_label->d_partitions[part]; 1129 return (0); 1130 1131 case DIOCWDINFO: 1132 case DIOCSDINFO: 1133 #ifdef __HAVE_OLD_DISKLABEL 1134 case ODIOCWDINFO: 1135 case ODIOCSDINFO: 1136 #endif 1137 { 1138 struct disklabel *lp; 1139 1140 #ifdef __HAVE_OLD_DISKLABEL 1141 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 1142 memset(&newlabel, 0, sizeof newlabel); 1143 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 1144 lp = &newlabel; 1145 } else 1146 #endif 1147 lp = (struct disklabel *)addr; 1148 1149 if ((flag & FWRITE) == 0) 1150 return (EBADF); 1151 1152 if ((error = cdlock(cd)) != 0) 1153 return (error); 1154 cd->flags |= CDF_LABELLING; 1155 1156 error = setdisklabel(cd->sc_dk.dk_label, 1157 lp, /*cd->sc_dk.dk_openmask : */0, 1158 cd->sc_dk.dk_cpulabel); 1159 if (error == 0) { 1160 /* XXX ? */ 1161 } 1162 1163 cd->flags &= ~CDF_LABELLING; 1164 cdunlock(cd); 1165 return (error); 1166 } 1167 1168 case DIOCWLABEL: 1169 return (EBADF); 1170 1171 case DIOCGDEFLABEL: 1172 cdgetdefaultlabel(cd, (struct disklabel *)addr); 1173 return (0); 1174 1175 #ifdef __HAVE_OLD_DISKLABEL 1176 case ODIOCGDEFLABEL: 1177 cdgetdefaultlabel(cd, &newlabel); 1178 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 1179 return ENOTTY; 1180 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 1181 return (0); 1182 #endif 1183 1184 case CDIOCPLAYTRACKS: { 1185 struct ioc_play_track *args = (struct ioc_play_track *)addr; 1186 1187 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1188 return (error); 1189 return (cd_play_tracks(cd, args->start_track, 1190 args->start_index, args->end_track, args->end_index)); 1191 } 1192 case CDIOCPLAYMSF: { 1193 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 1194 1195 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1196 return (error); 1197 return (cd_play_msf(cd, args->start_m, args->start_s, 1198 args->start_f, args->end_m, args->end_s, args->end_f)); 1199 } 1200 case CDIOCPLAYBLOCKS: { 1201 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 1202 1203 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1204 return (error); 1205 return (cd_play(cd, args->blk, args->len)); 1206 } 1207 case CDIOCREADSUBCHANNEL: { 1208 struct ioc_read_subchannel *args = 1209 (struct ioc_read_subchannel *)addr; 1210 struct cd_sub_channel_info data; 1211 int len = args->data_len; 1212 1213 if (len > sizeof(data) || 1214 len < sizeof(struct cd_sub_channel_header)) 1215 return (EINVAL); 1216 error = cd_read_subchannel(cd, args->address_format, 1217 args->data_format, args->track, &data, len, 1218 XS_CTL_DATA_ONSTACK); 1219 if (error) 1220 return (error); 1221 len = min(len, _2btol(data.header.data_len) + 1222 sizeof(struct cd_sub_channel_header)); 1223 return (copyout(&data, args->data, len)); 1224 } 1225 case CDIOREADTOCHEADER: { 1226 struct ioc_toc_header th; 1227 1228 if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 1229 XS_CTL_DATA_ONSTACK, 0)) != 0) 1230 return (error); 1231 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1232 th.len = le16toh(th.len); 1233 else 1234 th.len = be16toh(th.len); 1235 memcpy(addr, &th, sizeof(th)); 1236 return (0); 1237 } 1238 case CDIOREADTOCENTRYS: { 1239 struct cd_toc toc; 1240 struct ioc_read_toc_entry *te = 1241 (struct ioc_read_toc_entry *)addr; 1242 struct ioc_toc_header *th; 1243 struct cd_toc_entry *cte; 1244 int len = te->data_len; 1245 int ntracks; 1246 1247 th = &toc.header; 1248 1249 if (len > sizeof(toc.entries) || 1250 len < sizeof(struct cd_toc_entry)) 1251 return (EINVAL); 1252 error = cd_read_toc(cd, te->address_format, te->starting_track, 1253 &toc, len + sizeof(struct ioc_toc_header), 1254 XS_CTL_DATA_ONSTACK, 0); 1255 if (error) 1256 return (error); 1257 if (te->address_format == CD_LBA_FORMAT) 1258 for (ntracks = 1259 th->ending_track - th->starting_track + 1; 1260 ntracks >= 0; ntracks--) { 1261 cte = &toc.entries[ntracks]; 1262 cte->addr_type = CD_LBA_FORMAT; 1263 if (periph->periph_quirks & PQUIRK_LITTLETOC) 1264 cte->addr.lba = le32toh(cte->addr.lba); 1265 else 1266 cte->addr.lba = be32toh(cte->addr.lba); 1267 } 1268 if (periph->periph_quirks & PQUIRK_LITTLETOC) 1269 th->len = le16toh(th->len); 1270 else 1271 th->len = be16toh(th->len); 1272 len = min(len, th->len - (sizeof(th->starting_track) + 1273 sizeof(th->ending_track))); 1274 return (copyout(toc.entries, te->data, len)); 1275 } 1276 case CDIOREADMSADDR: { 1277 struct cd_toc toc; 1278 int sessno = *(int*)addr; 1279 struct cd_toc_entry *cte; 1280 1281 if (sessno != 0) 1282 return (EINVAL); 1283 1284 error = cd_read_toc(cd, 0, 0, &toc, 1285 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1286 XS_CTL_DATA_ONSTACK, 1287 0x40 /* control word for "get MS info" */); 1288 1289 if (error) 1290 return (error); 1291 1292 cte = &toc.entries[0]; 1293 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1294 cte->addr.lba = le32toh(cte->addr.lba); 1295 toc.header.len = le16toh(toc.header.len); 1296 } else { 1297 cte->addr.lba = be32toh(cte->addr.lba); 1298 toc.header.len = be16toh(toc.header.len); 1299 } 1300 1301 *(int*)addr = (toc.header.len >= 10 && cte->track > 1) ? 1302 cte->addr.lba : 0; 1303 return 0; 1304 } 1305 case CDIOCSETPATCH: { 1306 struct ioc_patch *arg = (struct ioc_patch *)addr; 1307 1308 return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0], 1309 arg->patch[1], arg->patch[2], arg->patch[3], 0)); 1310 } 1311 case CDIOCGETVOL: { 1312 struct ioc_vol *arg = (struct ioc_vol *)addr; 1313 1314 return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0)); 1315 } 1316 case CDIOCSETVOL: { 1317 struct ioc_vol *arg = (struct ioc_vol *)addr; 1318 1319 return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0)); 1320 } 1321 1322 case CDIOCSETMONO: 1323 return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL, 1324 BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1325 1326 case CDIOCSETSTEREO: 1327 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1328 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1329 1330 case CDIOCSETMUTE: 1331 return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL, 1332 MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1333 1334 case CDIOCSETLEFT: 1335 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1336 LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1337 1338 case CDIOCSETRIGHT: 1339 return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL, 1340 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1341 1342 case CDIOCRESUME: 1343 return (cd_pause(cd, PA_RESUME)); 1344 case CDIOCPAUSE: 1345 return (cd_pause(cd, PA_PAUSE)); 1346 case CDIOCSTART: 1347 return (scsipi_start(periph, SSS_START, 0)); 1348 case CDIOCSTOP: 1349 return (scsipi_start(periph, SSS_STOP, 0)); 1350 case CDIOCCLOSE: 1351 return (scsipi_start(periph, SSS_START|SSS_LOEJ, 1352 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1353 case DIOCEJECT: 1354 if (*(int *)addr == 0) { 1355 /* 1356 * Don't force eject: check that we are the only 1357 * partition open. If so, unlock it. 1358 */ 1359 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1360 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1361 cd->sc_dk.dk_openmask) { 1362 error = scsipi_prevent(periph, PR_ALLOW, 1363 XS_CTL_IGNORE_NOT_READY); 1364 if (error) 1365 return (error); 1366 } else { 1367 return (EBUSY); 1368 } 1369 } 1370 /* FALLTHROUGH */ 1371 case CDIOCEJECT: /* FALLTHROUGH */ 1372 case ODIOCEJECT: 1373 return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1374 case CDIOCALLOW: 1375 return (scsipi_prevent(periph, PR_ALLOW, 0)); 1376 case CDIOCPREVENT: 1377 return (scsipi_prevent(periph, PR_PREVENT, 0)); 1378 case DIOCLOCK: 1379 return (scsipi_prevent(periph, 1380 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1381 case CDIOCSETDEBUG: 1382 cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2); 1383 return (0); 1384 case CDIOCCLRDEBUG: 1385 cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2); 1386 return (0); 1387 case CDIOCRESET: 1388 case SCIOCRESET: 1389 return (cd_reset(cd)); 1390 case CDIOCLOADUNLOAD: { 1391 struct ioc_load_unload *args = (struct ioc_load_unload *)addr; 1392 1393 return ((*cd->sc_ops->cdo_load_unload)(cd, args->options, 1394 args->slot)); 1395 case DVD_AUTH: 1396 return (dvd_auth(cd, (dvd_authinfo *)addr)); 1397 case DVD_READ_STRUCT: 1398 return (dvd_read_struct(cd, (dvd_struct *)addr)); 1399 } 1400 1401 default: 1402 if (part != RAW_PART) 1403 return (ENOTTY); 1404 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p)); 1405 } 1406 1407 #ifdef DIAGNOSTIC 1408 panic("cdioctl: impossible"); 1409 #endif 1410 } 1411 1412 void 1413 cdgetdefaultlabel(cd, lp) 1414 struct cd_softc *cd; 1415 struct disklabel *lp; 1416 { 1417 1418 memset(lp, 0, sizeof(struct disklabel)); 1419 1420 lp->d_secsize = cd->params.blksize; 1421 lp->d_ntracks = 1; 1422 lp->d_nsectors = 100; 1423 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1424 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1425 1426 switch (scsipi_periph_bustype(cd->sc_periph)) { 1427 #if NCD_SCSIBUS > 0 1428 case SCSIPI_BUSTYPE_SCSI: 1429 lp->d_type = DTYPE_SCSI; 1430 break; 1431 #endif 1432 #if NCD_ATAPIBUS > 0 1433 case SCSIPI_BUSTYPE_ATAPI: 1434 lp->d_type = DTYPE_ATAPI; 1435 break; 1436 #endif 1437 } 1438 strncpy(lp->d_typename, cd->name, 16); 1439 strncpy(lp->d_packname, "fictitious", 16); 1440 lp->d_secperunit = cd->params.disksize; 1441 lp->d_rpm = 300; 1442 lp->d_interleave = 1; 1443 lp->d_flags = D_REMOVABLE; 1444 1445 lp->d_partitions[0].p_offset = 0; 1446 lp->d_partitions[0].p_size = 1447 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1448 lp->d_partitions[0].p_fstype = FS_ISO9660; 1449 lp->d_partitions[RAW_PART].p_offset = 0; 1450 lp->d_partitions[RAW_PART].p_size = 1451 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1452 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1453 lp->d_npartitions = RAW_PART + 1; 1454 1455 lp->d_magic = DISKMAGIC; 1456 lp->d_magic2 = DISKMAGIC; 1457 lp->d_checksum = dkcksum(lp); 1458 } 1459 1460 /* 1461 * Load the label information on the named device 1462 * Actually fabricate a disklabel 1463 * 1464 * EVENTUALLY take information about different 1465 * data tracks from the TOC and put it in the disklabel 1466 */ 1467 void 1468 cdgetdisklabel(cd) 1469 struct cd_softc *cd; 1470 { 1471 struct disklabel *lp = cd->sc_dk.dk_label; 1472 char *errstring; 1473 1474 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1475 1476 cdgetdefaultlabel(cd, lp); 1477 1478 /* 1479 * Call the generic disklabel extraction routine 1480 */ 1481 errstring = readdisklabel(MAKECDDEV(0, cd->sc_dev.dv_unit, RAW_PART), 1482 cdstrategy, lp, cd->sc_dk.dk_cpulabel); 1483 if (errstring) { 1484 printf("%s: %s\n", cd->sc_dev.dv_xname, errstring); 1485 goto error; 1486 } 1487 return; 1488 1489 error: 1490 /* Reset to default label -- should print a warning */ 1491 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1492 1493 cdgetdefaultlabel(cd, lp); 1494 } 1495 1496 /* 1497 * Find out from the device what it's capacity is 1498 */ 1499 u_long 1500 cd_size(cd, flags) 1501 struct cd_softc *cd; 1502 int flags; 1503 { 1504 struct scsipi_read_cd_cap_data rdcap; 1505 struct scsipi_read_cd_capacity scsipi_cmd; 1506 int blksize; 1507 u_long size; 1508 1509 if (cd->sc_periph->periph_quirks & PQUIRK_NOCAPACITY) { 1510 /* 1511 * the drive doesn't support the READ_CD_CAPACITY command 1512 * use a fake size 1513 */ 1514 cd->params.blksize = 2048; 1515 cd->params.disksize = 400000; 1516 return (400000); 1517 } 1518 1519 /* 1520 * make up a scsi command and ask the scsi driver to do 1521 * it for you. 1522 */ 1523 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1524 scsipi_cmd.opcode = READ_CD_CAPACITY; 1525 1526 /* 1527 * If the command works, interpret the result as a 4 byte 1528 * number of blocks and a blocksize 1529 */ 1530 if (scsipi_command(cd->sc_periph, 1531 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1532 (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL, 1533 flags | XS_CTL_DATA_IN | XS_CTL_DATA_IN) != 0) 1534 return (0); 1535 1536 blksize = _4btol(rdcap.length); 1537 if ((blksize < 512) || ((blksize & 511) != 0)) 1538 blksize = 2048; /* some drives lie ! */ 1539 cd->params.blksize = blksize; 1540 1541 size = _4btol(rdcap.addr) + 1; 1542 if (size < 100) 1543 size = 400000; /* ditto */ 1544 cd->params.disksize = size; 1545 1546 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, 1547 ("cd_size: %d %ld\n", blksize, size)); 1548 return (size); 1549 } 1550 1551 /* 1552 * Get scsi driver to send a "start playing" command 1553 */ 1554 int 1555 cd_play(cd, blkno, nblks) 1556 struct cd_softc *cd; 1557 int blkno, nblks; 1558 { 1559 struct scsipi_play scsipi_cmd; 1560 1561 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1562 scsipi_cmd.opcode = PLAY; 1563 _lto4b(blkno, scsipi_cmd.blk_addr); 1564 _lto2b(nblks, scsipi_cmd.xfer_len); 1565 return (scsipi_command(cd->sc_periph, 1566 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1567 0, 0, CDRETRIES, 30000, NULL, 0)); 1568 } 1569 1570 /* 1571 * Get scsi driver to send a "start playing" command 1572 */ 1573 int 1574 cd_play_tracks(cd, strack, sindex, etrack, eindex) 1575 struct cd_softc *cd; 1576 int strack, sindex, etrack, eindex; 1577 { 1578 struct cd_toc toc; 1579 int error; 1580 1581 if (!etrack) 1582 return (EIO); 1583 if (strack > etrack) 1584 return (EINVAL); 1585 1586 if ((error = cd_load_toc(cd, &toc, XS_CTL_DATA_ONSTACK)) != 0) 1587 return (error); 1588 1589 if (++etrack > (toc.header.ending_track+1)) 1590 etrack = toc.header.ending_track+1; 1591 1592 strack -= toc.header.starting_track; 1593 etrack -= toc.header.starting_track; 1594 if (strack < 0) 1595 return (EINVAL); 1596 1597 return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute, 1598 toc.entries[strack].addr.msf.second, 1599 toc.entries[strack].addr.msf.frame, 1600 toc.entries[etrack].addr.msf.minute, 1601 toc.entries[etrack].addr.msf.second, 1602 toc.entries[etrack].addr.msf.frame)); 1603 } 1604 1605 /* 1606 * Get scsi driver to send a "play msf" command 1607 */ 1608 int 1609 cd_play_msf(cd, startm, starts, startf, endm, ends, endf) 1610 struct cd_softc *cd; 1611 int startm, starts, startf, endm, ends, endf; 1612 { 1613 struct scsipi_play_msf scsipi_cmd; 1614 1615 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1616 scsipi_cmd.opcode = PLAY_MSF; 1617 scsipi_cmd.start_m = startm; 1618 scsipi_cmd.start_s = starts; 1619 scsipi_cmd.start_f = startf; 1620 scsipi_cmd.end_m = endm; 1621 scsipi_cmd.end_s = ends; 1622 scsipi_cmd.end_f = endf; 1623 return (scsipi_command(cd->sc_periph, 1624 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1625 0, 0, CDRETRIES, 30000, NULL, 0)); 1626 } 1627 1628 /* 1629 * Get scsi driver to send a "start up" command 1630 */ 1631 int 1632 cd_pause(cd, go) 1633 struct cd_softc *cd; 1634 int go; 1635 { 1636 struct scsipi_pause scsipi_cmd; 1637 1638 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1639 scsipi_cmd.opcode = PAUSE; 1640 scsipi_cmd.resume = go & 0xff; 1641 return (scsipi_command(cd->sc_periph, 1642 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1643 0, 0, CDRETRIES, 30000, NULL, 0)); 1644 } 1645 1646 /* 1647 * Get scsi driver to send a "RESET" command 1648 */ 1649 int 1650 cd_reset(cd) 1651 struct cd_softc *cd; 1652 { 1653 1654 return (scsipi_command(cd->sc_periph, 0, 0, 0, 0, 1655 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1656 } 1657 1658 /* 1659 * Read subchannel 1660 */ 1661 int 1662 cd_read_subchannel(cd, mode, format, track, data, len, flags) 1663 struct cd_softc *cd; 1664 int mode, format, track, len; 1665 struct cd_sub_channel_info *data; 1666 int flags; 1667 { 1668 struct scsipi_read_subchannel scsipi_cmd; 1669 1670 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1671 scsipi_cmd.opcode = READ_SUBCHANNEL; 1672 if (mode == CD_MSF_FORMAT) 1673 scsipi_cmd.byte2 |= CD_MSF; 1674 scsipi_cmd.byte3 = SRS_SUBQ; 1675 scsipi_cmd.subchan_format = format; 1676 scsipi_cmd.track = track; 1677 _lto2b(len, scsipi_cmd.data_len); 1678 return (scsipi_command(cd->sc_periph, 1679 (struct scsipi_generic *)&scsipi_cmd, 1680 sizeof(struct scsipi_read_subchannel), (u_char *)data, len, 1681 CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT)); 1682 } 1683 1684 /* 1685 * Read table of contents 1686 */ 1687 int 1688 cd_read_toc(cd, mode, start, data, len, flags, control) 1689 struct cd_softc *cd; 1690 int mode, start, len, control; 1691 void *data; 1692 int flags; 1693 { 1694 struct scsipi_read_toc scsipi_cmd; 1695 int ntoc; 1696 1697 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1698 #if 0 1699 if (len != sizeof(struct ioc_toc_header)) 1700 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 1701 sizeof(struct cd_toc_entry); 1702 else 1703 #endif 1704 ntoc = len; 1705 scsipi_cmd.opcode = READ_TOC; 1706 if (mode == CD_MSF_FORMAT) 1707 scsipi_cmd.byte2 |= CD_MSF; 1708 scsipi_cmd.from_track = start; 1709 _lto2b(ntoc, scsipi_cmd.data_len); 1710 scsipi_cmd.control = control; 1711 return (scsipi_command(cd->sc_periph, 1712 (struct scsipi_generic *)&scsipi_cmd, 1713 sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES, 1714 30000, NULL, flags | XS_CTL_DATA_IN)); 1715 } 1716 1717 int 1718 cd_load_toc(cd, toc, flags) 1719 struct cd_softc *cd; 1720 struct cd_toc *toc; 1721 int flags; 1722 { 1723 int ntracks, len, error; 1724 1725 if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 1726 flags, 0)) != 0) 1727 return (error); 1728 1729 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 1730 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 1731 sizeof(toc->header); 1732 if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 1733 flags, 0)) != 0) 1734 return (error); 1735 return (0); 1736 } 1737 1738 /* 1739 * Get the scsi driver to send a full inquiry to the device and use the 1740 * results to fill out the disk parameter structure. 1741 */ 1742 int 1743 cd_get_parms(cd, flags) 1744 struct cd_softc *cd; 1745 int flags; 1746 { 1747 1748 /* 1749 * give a number of sectors so that sec * trks * cyls 1750 * is <= disk_size 1751 */ 1752 if (cd_size(cd, flags) == 0) 1753 return (ENXIO); 1754 return (0); 1755 } 1756 1757 int 1758 cdsize(dev) 1759 dev_t dev; 1760 { 1761 1762 /* CD-ROMs are read-only. */ 1763 return (-1); 1764 } 1765 1766 int 1767 cddump(dev, blkno, va, size) 1768 dev_t dev; 1769 daddr_t blkno; 1770 caddr_t va; 1771 size_t size; 1772 { 1773 1774 /* Not implemented. */ 1775 return (ENXIO); 1776 } 1777 1778 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 1779 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 1780 1781 int 1782 dvd_auth(cd, a) 1783 struct cd_softc *cd; 1784 dvd_authinfo *a; 1785 { 1786 struct scsipi_generic cmd; 1787 u_int8_t buf[20]; 1788 int error; 1789 1790 memset(cmd.bytes, 0, 15); 1791 memset(buf, 0, sizeof(buf)); 1792 1793 switch (a->type) { 1794 case DVD_LU_SEND_AGID: 1795 cmd.opcode = GPCMD_REPORT_KEY; 1796 cmd.bytes[8] = 8; 1797 cmd.bytes[9] = 0 | (0 << 6); 1798 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1799 CDRETRIES, 30000, NULL, 1800 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1801 if (error) 1802 return (error); 1803 a->lsa.agid = buf[7] >> 6; 1804 return (0); 1805 1806 case DVD_LU_SEND_CHALLENGE: 1807 cmd.opcode = GPCMD_REPORT_KEY; 1808 cmd.bytes[8] = 16; 1809 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 1810 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1811 CDRETRIES, 30000, NULL, 1812 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1813 if (error) 1814 return (error); 1815 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1816 return (0); 1817 1818 case DVD_LU_SEND_KEY1: 1819 cmd.opcode = GPCMD_REPORT_KEY; 1820 cmd.bytes[8] = 12; 1821 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 1822 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1823 CDRETRIES, 30000, NULL, 1824 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1825 if (error) 1826 return (error); 1827 dvd_copy_key(a->lsk.key, &buf[4]); 1828 return (0); 1829 1830 case DVD_LU_SEND_TITLE_KEY: 1831 cmd.opcode = GPCMD_REPORT_KEY; 1832 _lto4b(a->lstk.lba, &cmd.bytes[1]); 1833 cmd.bytes[8] = 12; 1834 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 1835 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1836 CDRETRIES, 30000, NULL, 1837 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1838 if (error) 1839 return (error); 1840 a->lstk.cpm = (buf[4] >> 7) & 1; 1841 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1842 a->lstk.cgms = (buf[4] >> 4) & 3; 1843 dvd_copy_key(a->lstk.title_key, &buf[5]); 1844 return (0); 1845 1846 case DVD_LU_SEND_ASF: 1847 cmd.opcode = GPCMD_REPORT_KEY; 1848 cmd.bytes[8] = 8; 1849 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 1850 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1851 CDRETRIES, 30000, NULL, 1852 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1853 if (error) 1854 return (error); 1855 a->lsasf.asf = buf[7] & 1; 1856 return (0); 1857 1858 case DVD_HOST_SEND_CHALLENGE: 1859 cmd.opcode = GPCMD_SEND_KEY; 1860 cmd.bytes[8] = 16; 1861 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 1862 buf[1] = 14; 1863 dvd_copy_challenge(&buf[4], a->hsc.chal); 1864 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1865 CDRETRIES, 30000, NULL, 1866 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1867 if (error) 1868 return (error); 1869 a->type = DVD_LU_SEND_KEY1; 1870 return (0); 1871 1872 case DVD_HOST_SEND_KEY2: 1873 cmd.opcode = GPCMD_SEND_KEY; 1874 cmd.bytes[8] = 12; 1875 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 1876 buf[1] = 10; 1877 dvd_copy_key(&buf[4], a->hsk.key); 1878 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1879 CDRETRIES, 30000, NULL, 1880 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1881 if (error) { 1882 a->type = DVD_AUTH_FAILURE; 1883 return (error); 1884 } 1885 a->type = DVD_AUTH_ESTABLISHED; 1886 return (0); 1887 1888 case DVD_INVALIDATE_AGID: 1889 cmd.opcode = GPCMD_REPORT_KEY; 1890 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 1891 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1892 CDRETRIES, 30000, NULL, 0); 1893 if (error) 1894 return (error); 1895 return (0); 1896 1897 case DVD_LU_SEND_RPC_STATE: 1898 cmd.opcode = GPCMD_REPORT_KEY; 1899 cmd.bytes[8] = 8; 1900 cmd.bytes[9] = 8 | (0 << 6); 1901 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1902 CDRETRIES, 30000, NULL, 1903 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1904 if (error) 1905 return (error); 1906 a->lrpcs.type = (buf[8] >> 6) & 3; 1907 a->lrpcs.vra = (buf[8] >> 3) & 7; 1908 a->lrpcs.ucca = (buf[8]) & 7; 1909 a->lrpcs.region_mask = buf[9]; 1910 a->lrpcs.rpc_scheme = buf[10]; 1911 return (0); 1912 1913 case DVD_HOST_SEND_RPC_STATE: 1914 cmd.opcode = GPCMD_SEND_KEY; 1915 cmd.bytes[8] = 8; 1916 cmd.bytes[9] = 6 | (0 << 6); 1917 buf[1] = 6; 1918 buf[4] = a->hrpcs.pdrc; 1919 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1920 CDRETRIES, 30000, NULL, 1921 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1922 if (error) 1923 return (error); 1924 return (0); 1925 1926 default: 1927 return (ENOTTY); 1928 } 1929 } 1930 1931 int 1932 dvd_read_physical(cd, s) 1933 struct cd_softc *cd; 1934 dvd_struct *s; 1935 { 1936 struct scsipi_generic cmd; 1937 u_int8_t buf[4 + 4 * 20], *bufp; 1938 int error; 1939 struct dvd_layer *layer; 1940 int i; 1941 1942 memset(cmd.bytes, 0, 15); 1943 memset(buf, 0, sizeof(buf)); 1944 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1945 cmd.bytes[6] = s->type; 1946 _lto2b(sizeof(buf), &cmd.bytes[7]); 1947 1948 cmd.bytes[5] = s->physical.layer_num; 1949 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1950 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1951 if (error) 1952 return (error); 1953 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4; 1954 i++, bufp += 20, layer++) { 1955 memset(layer, 0, sizeof(*layer)); 1956 layer->book_version = bufp[0] & 0xf; 1957 layer->book_type = bufp[0] >> 4; 1958 layer->min_rate = bufp[1] & 0xf; 1959 layer->disc_size = bufp[1] >> 4; 1960 layer->layer_type = bufp[2] & 0xf; 1961 layer->track_path = (bufp[2] >> 4) & 1; 1962 layer->nlayers = (bufp[2] >> 5) & 3; 1963 layer->track_density = bufp[3] & 0xf; 1964 layer->linear_density = bufp[3] >> 4; 1965 layer->start_sector = _4btol(&bufp[4]); 1966 layer->end_sector = _4btol(&bufp[8]); 1967 layer->end_sector_l0 = _4btol(&bufp[12]); 1968 layer->bca = bufp[16] >> 7; 1969 } 1970 return (0); 1971 } 1972 1973 int 1974 dvd_read_copyright(cd, s) 1975 struct cd_softc *cd; 1976 dvd_struct *s; 1977 { 1978 struct scsipi_generic cmd; 1979 u_int8_t buf[8]; 1980 int error; 1981 1982 memset(cmd.bytes, 0, 15); 1983 memset(buf, 0, sizeof(buf)); 1984 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1985 cmd.bytes[6] = s->type; 1986 _lto2b(sizeof(buf), &cmd.bytes[7]); 1987 1988 cmd.bytes[5] = s->copyright.layer_num; 1989 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1990 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1991 if (error) 1992 return (error); 1993 s->copyright.cpst = buf[4]; 1994 s->copyright.rmi = buf[5]; 1995 return (0); 1996 } 1997 1998 int 1999 dvd_read_disckey(cd, s) 2000 struct cd_softc *cd; 2001 dvd_struct *s; 2002 { 2003 struct scsipi_generic cmd; 2004 u_int8_t buf[4 + 2048]; 2005 int error; 2006 2007 memset(cmd.bytes, 0, 15); 2008 memset(buf, 0, sizeof(buf)); 2009 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2010 cmd.bytes[6] = s->type; 2011 _lto2b(sizeof(buf), &cmd.bytes[7]); 2012 2013 cmd.bytes[9] = s->disckey.agid << 6; 2014 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 2015 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2016 if (error) 2017 return (error); 2018 memcpy(s->disckey.value, &buf[4], 2048); 2019 return (0); 2020 } 2021 2022 int 2023 dvd_read_bca(cd, s) 2024 struct cd_softc *cd; 2025 dvd_struct *s; 2026 { 2027 struct scsipi_generic cmd; 2028 u_int8_t buf[4 + 188]; 2029 int error; 2030 2031 memset(cmd.bytes, 0, 15); 2032 memset(buf, 0, sizeof(buf)); 2033 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2034 cmd.bytes[6] = s->type; 2035 _lto2b(sizeof(buf), &cmd.bytes[7]); 2036 2037 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 2038 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2039 if (error) 2040 return (error); 2041 s->bca.len = _2btol(&buf[0]); 2042 if (s->bca.len < 12 || s->bca.len > 188) 2043 return (EIO); 2044 memcpy(s->bca.value, &buf[4], s->bca.len); 2045 return (0); 2046 } 2047 2048 int 2049 dvd_read_manufact(cd, s) 2050 struct cd_softc *cd; 2051 dvd_struct *s; 2052 { 2053 struct scsipi_generic cmd; 2054 u_int8_t buf[4 + 2048]; 2055 int error; 2056 2057 memset(cmd.bytes, 0, 15); 2058 memset(buf, 0, sizeof(buf)); 2059 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2060 cmd.bytes[6] = s->type; 2061 _lto2b(sizeof(buf), &cmd.bytes[7]); 2062 2063 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 2064 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2065 if (error) 2066 return (error); 2067 s->manufact.len = _2btol(&buf[0]); 2068 if (s->manufact.len < 0 || s->manufact.len > 2048) 2069 return (EIO); 2070 memcpy(s->manufact.value, &buf[4], s->manufact.len); 2071 return (0); 2072 } 2073 2074 int 2075 dvd_read_struct(cd, s) 2076 struct cd_softc *cd; 2077 dvd_struct *s; 2078 { 2079 2080 switch (s->type) { 2081 case DVD_STRUCT_PHYSICAL: 2082 return (dvd_read_physical(cd, s)); 2083 case DVD_STRUCT_COPYRIGHT: 2084 return (dvd_read_copyright(cd, s)); 2085 case DVD_STRUCT_DISCKEY: 2086 return (dvd_read_disckey(cd, s)); 2087 case DVD_STRUCT_BCA: 2088 return (dvd_read_bca(cd, s)); 2089 case DVD_STRUCT_MANUFACT: 2090 return (dvd_read_manufact(cd, s)); 2091 default: 2092 return (EINVAL); 2093 } 2094 } 2095