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