1 /* $NetBSD: cd.c,v 1.177 2003/02/03 23:50:59 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.177 2003/02/03 23:50:59 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 (bounds_check_with_label(bp, lp, 588 (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0) 589 goto done; 590 591 /* 592 * Now convert the block number to absolute and put it in 593 * terms of the device's logical block size. 594 */ 595 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 596 if (CDPART(bp->b_dev) != RAW_PART) 597 blkno += lp->d_partitions[CDPART(bp->b_dev)].p_offset; 598 599 bp->b_rawblkno = blkno; 600 601 /* 602 * If the disklabel sector size does not match the device 603 * sector size we may need to do some extra work. 604 */ 605 if (lp->d_secsize != cd->params.blksize) { 606 607 /* 608 * If the xfer is not a multiple of the device block size 609 * or it is not block aligned, we need to bounce it. 610 */ 611 if ((bp->b_bcount % cd->params.blksize) != 0 || 612 ((blkno * lp->d_secsize) % cd->params.blksize) != 0) { 613 struct buf *nbp; 614 void *bounce = NULL; 615 long count; 616 617 if ((bp->b_flags & B_READ) == 0) { 618 619 /* XXXX We don't support bouncing writes. */ 620 bp->b_error = EACCES; 621 goto bad; 622 } 623 count = ((blkno * lp->d_secsize) % cd->params.blksize); 624 /* XXX Store starting offset in bp->b_rawblkno */ 625 bp->b_rawblkno = count; 626 627 count += bp->b_bcount; 628 count = roundup(count, cd->params.blksize); 629 630 blkno = ((blkno * lp->d_secsize) / cd->params.blksize); 631 s = splbio(); 632 nbp = pool_get(&bufpool, PR_NOWAIT); 633 splx(s); 634 if (!nbp) { 635 /* No memory -- fail the iop. */ 636 bp->b_error = ENOMEM; 637 goto bad; 638 } 639 bounce = malloc(count, M_DEVBUF, M_NOWAIT); 640 if (!bounce) { 641 /* No memory -- fail the iop. */ 642 s = splbio(); 643 pool_put(&bufpool, nbp); 644 splx(s); 645 bp->b_error = ENOMEM; 646 goto bad; 647 } 648 649 /* Set up the IOP to the bounce buffer. */ 650 nbp->b_error = 0; 651 nbp->b_proc = bp->b_proc; 652 nbp->b_vp = NULLVP; 653 654 nbp->b_bcount = count; 655 nbp->b_bufsize = count; 656 nbp->b_data = bounce; 657 658 LIST_INIT(&nbp->b_dep); 659 nbp->b_rawblkno = blkno; 660 661 /* We need to do a read-modify-write operation */ 662 nbp->b_flags = bp->b_flags | B_READ | B_CALL; 663 nbp->b_iodone = cdbounce; 664 665 /* Put ptr to orig buf in b_private and use new buf */ 666 nbp->b_private = bp; 667 bp = nbp; 668 669 } else { 670 /* Xfer is aligned -- just adjust the start block */ 671 bp->b_rawblkno = (blkno * lp->d_secsize) / 672 cd->params.blksize; 673 } 674 } 675 s = splbio(); 676 677 /* 678 * Place it in the queue of disk activities for this disk. 679 * 680 * XXX Only do disksort() if the current operating mode does not 681 * XXX include tagged queueing. 682 */ 683 BUFQ_PUT(&cd->buf_queue, bp); 684 685 /* 686 * Tell the device to get going on the transfer if it's 687 * not doing anything, otherwise just wait for completion 688 */ 689 cdstart(cd->sc_periph); 690 691 splx(s); 692 return; 693 694 bad: 695 bp->b_flags |= B_ERROR; 696 done: 697 /* 698 * Correctly set the buf to indicate a completed xfer 699 */ 700 bp->b_resid = bp->b_bcount; 701 biodone(bp); 702 } 703 704 /* 705 * cdstart looks to see if there is a buf waiting for the device 706 * and that the device is not already busy. If both are true, 707 * It deques the buf and creates a scsi command to perform the 708 * transfer in the buf. The transfer request will call scsipi_done 709 * on completion, which will in turn call this routine again 710 * so that the next queued transfer is performed. 711 * The bufs are queued by the strategy routine (cdstrategy) 712 * 713 * This routine is also called after other non-queued requests 714 * have been made of the scsi driver, to ensure that the queue 715 * continues to be drained. 716 * 717 * must be called at the correct (highish) spl level 718 * cdstart() is called at splbio from cdstrategy and scsipi_done 719 */ 720 void 721 cdstart(periph) 722 struct scsipi_periph *periph; 723 { 724 struct cd_softc *cd = (void *)periph->periph_dev; 725 struct buf *bp = 0; 726 struct scsipi_rw_big cmd_big; 727 #if NCD_SCSIBUS > 0 728 struct scsi_rw cmd_small; 729 #endif 730 struct scsipi_generic *cmdp; 731 int flags, nblks, cmdlen, error; 732 733 SC_DEBUG(periph, SCSIPI_DB2, ("cdstart ")); 734 /* 735 * Check if the device has room for another command 736 */ 737 while (periph->periph_active < periph->periph_openings) { 738 /* 739 * there is excess capacity, but a special waits 740 * It'll need the adapter as soon as we clear out of the 741 * way and let it run (user level wait). 742 */ 743 if (periph->periph_flags & PERIPH_WAITING) { 744 periph->periph_flags &= ~PERIPH_WAITING; 745 wakeup((caddr_t)periph); 746 return; 747 } 748 749 /* 750 * See if there is a buf with work for us to do.. 751 */ 752 if ((bp = BUFQ_GET(&cd->buf_queue)) == NULL) 753 return; 754 755 /* 756 * If the device has become invalid, abort all the 757 * reads and writes until all files have been closed and 758 * re-opened 759 */ 760 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 761 bp->b_error = EIO; 762 bp->b_flags |= B_ERROR; 763 bp->b_resid = bp->b_bcount; 764 biodone(bp); 765 continue; 766 } 767 768 /* 769 * We have a buf, now we should make a command. 770 */ 771 772 nblks = howmany(bp->b_bcount, cd->params.blksize); 773 774 #if NCD_SCSIBUS > 0 775 /* 776 * Fill out the scsi command. If the transfer will 777 * fit in a "small" cdb, use it. 778 */ 779 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 780 ((nblks & 0xff) == nblks) && 781 !(periph->periph_quirks & PQUIRK_ONLYBIG) && 782 scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_SCSI) { 783 /* 784 * We can fit in a small cdb. 785 */ 786 memset(&cmd_small, 0, sizeof(cmd_small)); 787 cmd_small.opcode = (bp->b_flags & B_READ) ? 788 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND; 789 _lto3b(bp->b_rawblkno, cmd_small.addr); 790 cmd_small.length = nblks & 0xff; 791 cmdlen = sizeof(cmd_small); 792 cmdp = (struct scsipi_generic *)&cmd_small; 793 } else 794 #endif 795 { 796 /* 797 * Need a large cdb. 798 */ 799 memset(&cmd_big, 0, sizeof(cmd_big)); 800 cmd_big.opcode = (bp->b_flags & B_READ) ? 801 READ_BIG : WRITE_BIG; 802 _lto4b(bp->b_rawblkno, cmd_big.addr); 803 _lto2b(nblks, cmd_big.length); 804 cmdlen = sizeof(cmd_big); 805 cmdp = (struct scsipi_generic *)&cmd_big; 806 } 807 808 /* Instrumentation. */ 809 disk_busy(&cd->sc_dk); 810 811 /* 812 * Figure out what flags to use. 813 */ 814 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 815 if (bp->b_flags & B_READ) 816 flags |= XS_CTL_DATA_IN; 817 else 818 flags |= XS_CTL_DATA_OUT; 819 820 /* 821 * Call the routine that chats with the adapter. 822 * Note: we cannot sleep as we may be an interrupt 823 */ 824 error = scsipi_command(periph, cmdp, cmdlen, 825 (u_char *)bp->b_data, bp->b_bcount, 826 CDRETRIES, 30000, bp, flags); 827 if (error) { 828 disk_unbusy(&cd->sc_dk, 0, 0); 829 printf("%s: not queued, error %d\n", 830 cd->sc_dev.dv_xname, error); 831 } 832 } 833 } 834 835 void 836 cddone(xs) 837 struct scsipi_xfer *xs; 838 { 839 struct cd_softc *cd = (void *)xs->xs_periph->periph_dev; 840 841 if (xs->bp != NULL) { 842 disk_unbusy(&cd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid, 843 (xs->bp->b_flags & B_READ)); 844 #if NRND > 0 845 rnd_add_uint32(&cd->rnd_source, xs->bp->b_rawblkno); 846 #endif 847 } 848 } 849 850 void 851 cdbounce(bp) 852 struct buf *bp; 853 { 854 struct buf *obp = (struct buf *)bp->b_private; 855 856 if (bp->b_flags & B_ERROR) { 857 /* EEK propagate the error and free the memory */ 858 goto done; 859 } 860 if (obp->b_flags & B_READ) { 861 /* Copy data to the final destination and free the buf. */ 862 memcpy(obp->b_data, bp->b_data+obp->b_rawblkno, 863 obp->b_bcount); 864 } else { 865 /* 866 * XXXX This is a CD-ROM -- READ ONLY -- why do we bother with 867 * XXXX any of this write stuff? 868 */ 869 if (bp->b_flags & B_READ) { 870 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 871 struct buf *nbp; 872 int s; 873 874 /* Read part of RMW complete. */ 875 memcpy(bp->b_data+obp->b_rawblkno, obp->b_data, 876 obp->b_bcount); 877 878 s = splbio(); 879 880 /* We need to alloc a new buf. */ 881 nbp = pool_get(&bufpool, PR_NOWAIT); 882 if (!nbp) { 883 splx(s); 884 /* No buf available. */ 885 bp->b_flags |= B_ERROR; 886 bp->b_error = ENOMEM; 887 bp->b_resid = bp->b_bcount; 888 } 889 890 /* Set up the IOP to the bounce buffer. */ 891 nbp->b_error = 0; 892 nbp->b_proc = bp->b_proc; 893 nbp->b_vp = NULLVP; 894 895 nbp->b_bcount = bp->b_bcount; 896 nbp->b_bufsize = bp->b_bufsize; 897 nbp->b_data = bp->b_data; 898 899 LIST_INIT(&nbp->b_dep); 900 nbp->b_rawblkno = bp->b_rawblkno; 901 902 /* We need to do a read-modify-write operation */ 903 nbp->b_flags = obp->b_flags | B_CALL; 904 nbp->b_iodone = cdbounce; 905 906 /* Put ptr to orig buf in b_private and use new buf */ 907 nbp->b_private = obp; 908 909 /* 910 * Place it in the queue of disk activities for this 911 * disk. 912 * 913 * XXX Only do disksort() if the current operating mode 914 * XXX does not include tagged queueing. 915 */ 916 BUFQ_PUT(&cd->buf_queue, nbp); 917 918 /* 919 * Tell the device to get going on the transfer if it's 920 * not doing anything, otherwise just wait for 921 * completion 922 */ 923 cdstart(cd->sc_periph); 924 925 splx(s); 926 return; 927 928 } 929 } 930 done: 931 obp->b_flags |= (bp->b_flags&(B_EINTR|B_ERROR)); 932 obp->b_error = bp->b_error; 933 obp->b_resid = bp->b_resid; 934 free(bp->b_data, M_DEVBUF); 935 biodone(obp); 936 } 937 938 int cd_interpret_sense(xs) 939 struct scsipi_xfer *xs; 940 { 941 struct scsipi_periph *periph = xs->xs_periph; 942 struct scsipi_sense_data *sense = &xs->sense.scsi_sense; 943 int retval = EJUSTRETURN; 944 945 /* 946 * If it isn't a extended or extended/deferred error, let 947 * the generic code handle it. 948 */ 949 if ((sense->error_code & SSD_ERRCODE) != 0x70 && 950 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFERRED */ 951 return (retval); 952 } 953 954 /* 955 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit 956 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then 957 * wait a bit for the drive to spin up 958 */ 959 960 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY && 961 sense->add_sense_code == 0x4 && 962 sense->add_sense_code_qual == 0x01) { 963 /* 964 * Sleep for 5 seconds to wait for the drive to spin up 965 */ 966 967 SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD " 968 "spinup\n")); 969 if (!callout_pending(&periph->periph_callout)) 970 scsipi_periph_freeze(periph, 1); 971 callout_reset(&periph->periph_callout, 972 5 * hz, scsipi_periph_timed_thaw, periph); 973 retval = ERESTART; 974 } 975 return (retval); 976 } 977 978 void 979 cdminphys(bp) 980 struct buf *bp; 981 { 982 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 983 long max; 984 985 /* 986 * If the device is ancient, we want to make sure that 987 * the transfer fits into a 6-byte cdb. 988 * 989 * XXX Note that the SCSI-I spec says that 256-block transfers 990 * are allowed in a 6-byte read/write, and are specified 991 * by settng the "length" to 0. However, we're conservative 992 * here, allowing only 255-block transfers in case an 993 * ancient device gets confused by length == 0. A length of 0 994 * in a 10-byte read/write actually means 0 blocks. 995 */ 996 if (cd->flags & CDF_ANCIENT) { 997 max = cd->sc_dk.dk_label->d_secsize * 0xff; 998 999 if (bp->b_bcount > max) 1000 bp->b_bcount = max; 1001 } 1002 1003 (*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp); 1004 } 1005 1006 int 1007 cdread(dev, uio, ioflag) 1008 dev_t dev; 1009 struct uio *uio; 1010 int ioflag; 1011 { 1012 1013 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 1014 } 1015 1016 int 1017 cdwrite(dev, uio, ioflag) 1018 dev_t dev; 1019 struct uio *uio; 1020 int ioflag; 1021 { 1022 1023 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 1024 } 1025 1026 /* 1027 * conversion between minute-seconde-frame and logical block adress 1028 * adresses format 1029 */ 1030 void 1031 lba2msf (lba, m, s, f) 1032 u_long lba; 1033 u_char *m, *s, *f; 1034 { 1035 u_long tmp; 1036 1037 tmp = lba + CD_BLOCK_OFFSET; /* offset of first logical frame */ 1038 tmp &= 0xffffff; /* negative lbas use only 24 bits */ 1039 *m = tmp / (CD_SECS * CD_FRAMES); 1040 tmp %= (CD_SECS * CD_FRAMES); 1041 *s = tmp / CD_FRAMES; 1042 *f = tmp % CD_FRAMES; 1043 } 1044 1045 u_long 1046 msf2lba (m, s, f) 1047 u_char m, s, f; 1048 { 1049 1050 return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET); 1051 } 1052 1053 int 1054 cdreadmsaddr(cd, addr) 1055 struct cd_softc *cd; 1056 int *addr; 1057 { 1058 struct scsipi_periph *periph = cd->sc_periph; 1059 int error; 1060 struct cd_toc toc; 1061 struct cd_toc_entry *cte; 1062 1063 error = cd_read_toc(cd, 0, 0, &toc, 1064 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1065 XS_CTL_DATA_ONSTACK, 1066 0x40 /* control word for "get MS info" */); 1067 1068 if (error) 1069 return (error); 1070 1071 cte = &toc.entries[0]; 1072 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1073 cte->addr.lba = le32toh(cte->addr.lba); 1074 toc.header.len = le16toh(toc.header.len); 1075 } else { 1076 cte->addr.lba = be32toh(cte->addr.lba); 1077 toc.header.len = be16toh(toc.header.len); 1078 } 1079 1080 *addr = (toc.header.len >= 10 && cte->track > 1) ? 1081 cte->addr.lba : 0; 1082 return 0; 1083 } 1084 1085 /* 1086 * Perform special action on behalf of the user. 1087 * Knows about the internals of this device 1088 */ 1089 int 1090 cdioctl(dev, cmd, addr, flag, p) 1091 dev_t dev; 1092 u_long cmd; 1093 caddr_t addr; 1094 int flag; 1095 struct proc *p; 1096 { 1097 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 1098 struct scsipi_periph *periph = cd->sc_periph; 1099 int part = CDPART(dev); 1100 int error = 0; 1101 #ifdef __HAVE_OLD_DISKLABEL 1102 struct disklabel *newlabel = NULL; 1103 #endif 1104 1105 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd)); 1106 1107 /* 1108 * If the device is not valid, some IOCTLs can still be 1109 * handled on the raw partition. Check this here. 1110 */ 1111 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 1112 switch (cmd) { 1113 case DIOCWLABEL: 1114 case DIOCLOCK: 1115 case ODIOCEJECT: 1116 case DIOCEJECT: 1117 case SCIOCIDENTIFY: 1118 case OSCIOCIDENTIFY: 1119 case SCIOCCOMMAND: 1120 case SCIOCDEBUG: 1121 case CDIOCGETVOL: 1122 case CDIOCSETVOL: 1123 case CDIOCSETMONO: 1124 case CDIOCSETSTEREO: 1125 case CDIOCSETMUTE: 1126 case CDIOCSETLEFT: 1127 case CDIOCSETRIGHT: 1128 case CDIOCCLOSE: 1129 case CDIOCEJECT: 1130 case CDIOCALLOW: 1131 case CDIOCPREVENT: 1132 case CDIOCSETDEBUG: 1133 case CDIOCCLRDEBUG: 1134 case CDIOCRESET: 1135 case SCIOCRESET: 1136 case CDIOCLOADUNLOAD: 1137 case DVD_AUTH: 1138 case DVD_READ_STRUCT: 1139 if (part == RAW_PART) 1140 break; 1141 /* FALLTHROUGH */ 1142 default: 1143 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1144 return (ENODEV); 1145 else 1146 return (EIO); 1147 } 1148 } 1149 1150 switch (cmd) { 1151 case DIOCGDINFO: 1152 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 1153 return (0); 1154 #ifdef __HAVE_OLD_DISKLABEL 1155 case ODIOCGDINFO: 1156 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1157 if (newlabel == NULL) 1158 return (EIO); 1159 memcpy(newlabel, cd->sc_dk.dk_label, sizeof (*newlabel)); 1160 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1161 error = ENOTTY; 1162 else 1163 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1164 free(newlabel, M_TEMP); 1165 return error; 1166 #endif 1167 1168 case DIOCGPART: 1169 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 1170 ((struct partinfo *)addr)->part = 1171 &cd->sc_dk.dk_label->d_partitions[part]; 1172 return (0); 1173 1174 case DIOCWDINFO: 1175 case DIOCSDINFO: 1176 #ifdef __HAVE_OLD_DISKLABEL 1177 case ODIOCWDINFO: 1178 case ODIOCSDINFO: 1179 #endif 1180 { 1181 struct disklabel *lp; 1182 1183 if ((flag & FWRITE) == 0) 1184 return (EBADF); 1185 1186 #ifdef __HAVE_OLD_DISKLABEL 1187 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 1188 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1189 if (newlabel == NULL) 1190 return (EIO); 1191 memset(newlabel, 0, sizeof newlabel); 1192 memcpy(newlabel, addr, sizeof (struct olddisklabel)); 1193 lp = newlabel; 1194 } else 1195 #endif 1196 lp = (struct disklabel *)addr; 1197 1198 if ((error = cdlock(cd)) != 0) 1199 goto bad; 1200 cd->flags |= CDF_LABELLING; 1201 1202 error = setdisklabel(cd->sc_dk.dk_label, 1203 lp, /*cd->sc_dk.dk_openmask : */0, 1204 cd->sc_dk.dk_cpulabel); 1205 if (error == 0) { 1206 /* XXX ? */ 1207 } 1208 1209 cd->flags &= ~CDF_LABELLING; 1210 cdunlock(cd); 1211 bad: 1212 #ifdef __HAVE_OLD_DISKLABEL 1213 if (newlabel != NULL) 1214 free(newlabel, M_TEMP); 1215 #endif 1216 return (error); 1217 } 1218 1219 case DIOCWLABEL: 1220 return (EBADF); 1221 1222 case DIOCGDEFLABEL: 1223 cdgetdefaultlabel(cd, (struct disklabel *)addr); 1224 return (0); 1225 1226 #ifdef __HAVE_OLD_DISKLABEL 1227 case ODIOCGDEFLABEL: 1228 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1229 if (newlabel == NULL) 1230 return (EIO); 1231 cdgetdefaultlabel(cd, newlabel); 1232 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1233 error = ENOTTY; 1234 else 1235 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1236 free(newlabel, M_TEMP); 1237 return error; 1238 #endif 1239 1240 case CDIOCPLAYTRACKS: { 1241 struct ioc_play_track *args = (struct ioc_play_track *)addr; 1242 1243 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1244 return (error); 1245 return (cd_play_tracks(cd, args->start_track, 1246 args->start_index, args->end_track, args->end_index)); 1247 } 1248 case CDIOCPLAYMSF: { 1249 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 1250 1251 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1252 return (error); 1253 return (cd_play_msf(cd, args->start_m, args->start_s, 1254 args->start_f, args->end_m, args->end_s, args->end_f)); 1255 } 1256 case CDIOCPLAYBLOCKS: { 1257 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 1258 1259 if ((error = (*cd->sc_ops->cdo_set_pa_immed)(cd, 0)) != 0) 1260 return (error); 1261 return (cd_play(cd, args->blk, args->len)); 1262 } 1263 case CDIOCREADSUBCHANNEL: { 1264 struct ioc_read_subchannel *args = 1265 (struct ioc_read_subchannel *)addr; 1266 struct cd_sub_channel_info data; 1267 u_int len = args->data_len; 1268 1269 if (len > sizeof(data) || 1270 len < sizeof(struct cd_sub_channel_header)) 1271 return (EINVAL); 1272 error = cd_read_subchannel(cd, args->address_format, 1273 args->data_format, args->track, &data, len, 1274 XS_CTL_DATA_ONSTACK); 1275 if (error) 1276 return (error); 1277 len = min(len, _2btol(data.header.data_len) + 1278 sizeof(struct cd_sub_channel_header)); 1279 return (copyout(&data, args->data, len)); 1280 } 1281 case CDIOREADTOCHEADER: { 1282 struct ioc_toc_header th; 1283 1284 if ((error = cd_read_toc(cd, 0, 0, &th, sizeof(th), 1285 XS_CTL_DATA_ONSTACK, 0)) != 0) 1286 return (error); 1287 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1288 th.len = le16toh(th.len); 1289 else 1290 th.len = be16toh(th.len); 1291 memcpy(addr, &th, sizeof(th)); 1292 return (0); 1293 } 1294 case CDIOREADTOCENTRYS: { 1295 struct cd_toc toc; 1296 struct ioc_read_toc_entry *te = 1297 (struct ioc_read_toc_entry *)addr; 1298 struct ioc_toc_header *th; 1299 struct cd_toc_entry *cte; 1300 u_int len = te->data_len; 1301 int ntracks; 1302 1303 th = &toc.header; 1304 1305 if (len > sizeof(toc.entries) || 1306 len < sizeof(struct cd_toc_entry)) 1307 return (EINVAL); 1308 error = cd_read_toc(cd, te->address_format, te->starting_track, 1309 &toc, len + sizeof(struct ioc_toc_header), 1310 XS_CTL_DATA_ONSTACK, 0); 1311 if (error) 1312 return (error); 1313 if (te->address_format == CD_LBA_FORMAT) 1314 for (ntracks = 1315 th->ending_track - th->starting_track + 1; 1316 ntracks >= 0; ntracks--) { 1317 cte = &toc.entries[ntracks]; 1318 cte->addr_type = CD_LBA_FORMAT; 1319 if (periph->periph_quirks & PQUIRK_LITTLETOC) 1320 cte->addr.lba = le32toh(cte->addr.lba); 1321 else 1322 cte->addr.lba = be32toh(cte->addr.lba); 1323 } 1324 if (periph->periph_quirks & PQUIRK_LITTLETOC) 1325 th->len = le16toh(th->len); 1326 else 1327 th->len = be16toh(th->len); 1328 len = min(len, th->len - (sizeof(th->starting_track) + 1329 sizeof(th->ending_track))); 1330 return (copyout(toc.entries, te->data, len)); 1331 } 1332 case CDIOREADMSADDR: { 1333 int sessno = *(int*)addr; 1334 1335 if (sessno != 0) 1336 return (EINVAL); 1337 1338 return (cdreadmsaddr(cd, (int*)addr)); 1339 } 1340 case CDIOCSETPATCH: { 1341 struct ioc_patch *arg = (struct ioc_patch *)addr; 1342 1343 return ((*cd->sc_ops->cdo_setchan)(cd, arg->patch[0], 1344 arg->patch[1], arg->patch[2], arg->patch[3], 0)); 1345 } 1346 case CDIOCGETVOL: { 1347 struct ioc_vol *arg = (struct ioc_vol *)addr; 1348 1349 return ((*cd->sc_ops->cdo_getvol)(cd, arg, 0)); 1350 } 1351 case CDIOCSETVOL: { 1352 struct ioc_vol *arg = (struct ioc_vol *)addr; 1353 1354 return ((*cd->sc_ops->cdo_setvol)(cd, arg, 0)); 1355 } 1356 1357 case CDIOCSETMONO: 1358 return ((*cd->sc_ops->cdo_setchan)(cd, BOTH_CHANNEL, 1359 BOTH_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1360 1361 case CDIOCSETSTEREO: 1362 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1363 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1364 1365 case CDIOCSETMUTE: 1366 return ((*cd->sc_ops->cdo_setchan)(cd, MUTE_CHANNEL, 1367 MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1368 1369 case CDIOCSETLEFT: 1370 return ((*cd->sc_ops->cdo_setchan)(cd, LEFT_CHANNEL, 1371 LEFT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1372 1373 case CDIOCSETRIGHT: 1374 return ((*cd->sc_ops->cdo_setchan)(cd, RIGHT_CHANNEL, 1375 RIGHT_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1376 1377 case CDIOCRESUME: 1378 return (cd_pause(cd, PA_RESUME)); 1379 case CDIOCPAUSE: 1380 return (cd_pause(cd, PA_PAUSE)); 1381 case CDIOCSTART: 1382 return (scsipi_start(periph, SSS_START, 0)); 1383 case CDIOCSTOP: 1384 return (scsipi_start(periph, SSS_STOP, 0)); 1385 case CDIOCCLOSE: 1386 return (scsipi_start(periph, SSS_START|SSS_LOEJ, 1387 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1388 case DIOCEJECT: 1389 if (*(int *)addr == 0) { 1390 /* 1391 * Don't force eject: check that we are the only 1392 * partition open. If so, unlock it. 1393 */ 1394 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1395 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1396 cd->sc_dk.dk_openmask) { 1397 error = scsipi_prevent(periph, PR_ALLOW, 1398 XS_CTL_IGNORE_NOT_READY); 1399 if (error) 1400 return (error); 1401 } else { 1402 return (EBUSY); 1403 } 1404 } 1405 /* FALLTHROUGH */ 1406 case CDIOCEJECT: /* FALLTHROUGH */ 1407 case ODIOCEJECT: 1408 return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1409 case CDIOCALLOW: 1410 return (scsipi_prevent(periph, PR_ALLOW, 0)); 1411 case CDIOCPREVENT: 1412 return (scsipi_prevent(periph, PR_PREVENT, 0)); 1413 case DIOCLOCK: 1414 return (scsipi_prevent(periph, 1415 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0)); 1416 case CDIOCSETDEBUG: 1417 cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2); 1418 return (0); 1419 case CDIOCCLRDEBUG: 1420 cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2); 1421 return (0); 1422 case CDIOCRESET: 1423 case SCIOCRESET: 1424 return (cd_reset(cd)); 1425 case CDIOCLOADUNLOAD: { 1426 struct ioc_load_unload *args = (struct ioc_load_unload *)addr; 1427 1428 return ((*cd->sc_ops->cdo_load_unload)(cd, args->options, 1429 args->slot)); 1430 case DVD_AUTH: 1431 return (dvd_auth(cd, (dvd_authinfo *)addr)); 1432 case DVD_READ_STRUCT: 1433 return (dvd_read_struct(cd, (dvd_struct *)addr)); 1434 } 1435 1436 default: 1437 if (part != RAW_PART) 1438 return (ENOTTY); 1439 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p)); 1440 } 1441 1442 #ifdef DIAGNOSTIC 1443 panic("cdioctl: impossible"); 1444 #endif 1445 } 1446 1447 void 1448 cdgetdefaultlabel(cd, lp) 1449 struct cd_softc *cd; 1450 struct disklabel *lp; 1451 { 1452 int lastsession; 1453 1454 memset(lp, 0, sizeof(struct disklabel)); 1455 1456 lp->d_secsize = cd->params.blksize; 1457 lp->d_ntracks = 1; 1458 lp->d_nsectors = 100; 1459 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1460 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1461 1462 switch (scsipi_periph_bustype(cd->sc_periph)) { 1463 #if NCD_SCSIBUS > 0 1464 case SCSIPI_BUSTYPE_SCSI: 1465 lp->d_type = DTYPE_SCSI; 1466 break; 1467 #endif 1468 #if NCD_ATAPIBUS > 0 1469 case SCSIPI_BUSTYPE_ATAPI: 1470 lp->d_type = DTYPE_ATAPI; 1471 break; 1472 #endif 1473 } 1474 strncpy(lp->d_typename, cd->name, 16); 1475 strncpy(lp->d_packname, "fictitious", 16); 1476 lp->d_secperunit = cd->params.disksize; 1477 lp->d_rpm = 300; 1478 lp->d_interleave = 1; 1479 lp->d_flags = D_REMOVABLE; 1480 1481 if (cdreadmsaddr(cd, &lastsession) != 0) 1482 lastsession = 0; 1483 1484 lp->d_partitions[0].p_offset = 0; 1485 #ifdef notyet /* have to fix bounds_check_with_label() first */ 1486 lp->d_partitions[0].p_size = lp->d_secperunit; 1487 #else 1488 lp->d_partitions[0].p_size = 1489 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1490 #endif 1491 lp->d_partitions[0].p_cdsession = lastsession; 1492 lp->d_partitions[0].p_fstype = FS_ISO9660; 1493 lp->d_partitions[RAW_PART].p_offset = 0; 1494 #ifdef notyet 1495 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 1496 #else 1497 lp->d_partitions[RAW_PART].p_size = 1498 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1499 #endif 1500 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1501 lp->d_npartitions = RAW_PART + 1; 1502 1503 lp->d_magic = DISKMAGIC; 1504 lp->d_magic2 = DISKMAGIC; 1505 lp->d_checksum = dkcksum(lp); 1506 } 1507 1508 /* 1509 * Load the label information on the named device 1510 * Actually fabricate a disklabel 1511 * 1512 * EVENTUALLY take information about different 1513 * data tracks from the TOC and put it in the disklabel 1514 */ 1515 void 1516 cdgetdisklabel(cd) 1517 struct cd_softc *cd; 1518 { 1519 struct disklabel *lp = cd->sc_dk.dk_label; 1520 char *errstring; 1521 1522 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1523 1524 cdgetdefaultlabel(cd, lp); 1525 1526 /* 1527 * Call the generic disklabel extraction routine 1528 */ 1529 errstring = readdisklabel(MAKECDDEV(0, cd->sc_dev.dv_unit, RAW_PART), 1530 cdstrategy, lp, cd->sc_dk.dk_cpulabel); 1531 if (errstring) { 1532 printf("%s: %s\n", cd->sc_dev.dv_xname, errstring); 1533 goto error; 1534 } 1535 return; 1536 1537 error: 1538 /* Reset to default label -- should print a warning */ 1539 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1540 1541 cdgetdefaultlabel(cd, lp); 1542 } 1543 1544 /* 1545 * Find out from the device what it's capacity is 1546 */ 1547 u_long 1548 cd_size(cd, flags) 1549 struct cd_softc *cd; 1550 int flags; 1551 { 1552 struct scsipi_read_cd_cap_data rdcap; 1553 struct scsipi_read_cd_capacity scsipi_cmd; 1554 int blksize; 1555 u_long size; 1556 1557 if (cd->sc_periph->periph_quirks & PQUIRK_NOCAPACITY) { 1558 /* 1559 * the drive doesn't support the READ_CD_CAPACITY command 1560 * use a fake size 1561 */ 1562 cd->params.blksize = 2048; 1563 cd->params.disksize = 400000; 1564 return (400000); 1565 } 1566 1567 /* 1568 * make up a scsi command and ask the scsi driver to do 1569 * it for you. 1570 */ 1571 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1572 scsipi_cmd.opcode = READ_CD_CAPACITY; 1573 1574 /* 1575 * If the command works, interpret the result as a 4 byte 1576 * number of blocks and a blocksize 1577 */ 1578 if (scsipi_command(cd->sc_periph, 1579 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1580 (u_char *)&rdcap, sizeof(rdcap), CDRETRIES, 30000, NULL, 1581 flags | XS_CTL_DATA_IN | XS_CTL_DATA_IN) != 0) 1582 return (0); 1583 1584 blksize = _4btol(rdcap.length); 1585 if ((blksize < 512) || ((blksize & 511) != 0)) 1586 blksize = 2048; /* some drives lie ! */ 1587 cd->params.blksize = blksize; 1588 1589 size = _4btol(rdcap.addr) + 1; 1590 if (size < 100) 1591 size = 400000; /* ditto */ 1592 cd->params.disksize = size; 1593 1594 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, 1595 ("cd_size: %d %ld\n", blksize, size)); 1596 return (size); 1597 } 1598 1599 /* 1600 * Get scsi driver to send a "start playing" command 1601 */ 1602 int 1603 cd_play(cd, blkno, nblks) 1604 struct cd_softc *cd; 1605 int blkno, nblks; 1606 { 1607 struct scsipi_play scsipi_cmd; 1608 1609 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1610 scsipi_cmd.opcode = PLAY; 1611 _lto4b(blkno, scsipi_cmd.blk_addr); 1612 _lto2b(nblks, scsipi_cmd.xfer_len); 1613 return (scsipi_command(cd->sc_periph, 1614 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1615 0, 0, CDRETRIES, 30000, NULL, 0)); 1616 } 1617 1618 /* 1619 * Get scsi driver to send a "start playing" command 1620 */ 1621 int 1622 cd_play_tracks(cd, strack, sindex, etrack, eindex) 1623 struct cd_softc *cd; 1624 int strack, sindex, etrack, eindex; 1625 { 1626 struct cd_toc toc; 1627 int error; 1628 1629 if (!etrack) 1630 return (EIO); 1631 if (strack > etrack) 1632 return (EINVAL); 1633 1634 if ((error = cd_load_toc(cd, &toc, XS_CTL_DATA_ONSTACK)) != 0) 1635 return (error); 1636 1637 if (++etrack > (toc.header.ending_track+1)) 1638 etrack = toc.header.ending_track+1; 1639 1640 strack -= toc.header.starting_track; 1641 etrack -= toc.header.starting_track; 1642 if (strack < 0) 1643 return (EINVAL); 1644 1645 return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute, 1646 toc.entries[strack].addr.msf.second, 1647 toc.entries[strack].addr.msf.frame, 1648 toc.entries[etrack].addr.msf.minute, 1649 toc.entries[etrack].addr.msf.second, 1650 toc.entries[etrack].addr.msf.frame)); 1651 } 1652 1653 /* 1654 * Get scsi driver to send a "play msf" command 1655 */ 1656 int 1657 cd_play_msf(cd, startm, starts, startf, endm, ends, endf) 1658 struct cd_softc *cd; 1659 int startm, starts, startf, endm, ends, endf; 1660 { 1661 struct scsipi_play_msf scsipi_cmd; 1662 1663 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1664 scsipi_cmd.opcode = PLAY_MSF; 1665 scsipi_cmd.start_m = startm; 1666 scsipi_cmd.start_s = starts; 1667 scsipi_cmd.start_f = startf; 1668 scsipi_cmd.end_m = endm; 1669 scsipi_cmd.end_s = ends; 1670 scsipi_cmd.end_f = endf; 1671 return (scsipi_command(cd->sc_periph, 1672 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1673 0, 0, CDRETRIES, 30000, NULL, 0)); 1674 } 1675 1676 /* 1677 * Get scsi driver to send a "start up" command 1678 */ 1679 int 1680 cd_pause(cd, go) 1681 struct cd_softc *cd; 1682 int go; 1683 { 1684 struct scsipi_pause scsipi_cmd; 1685 1686 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1687 scsipi_cmd.opcode = PAUSE; 1688 scsipi_cmd.resume = go & 0xff; 1689 return (scsipi_command(cd->sc_periph, 1690 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 1691 0, 0, CDRETRIES, 30000, NULL, 0)); 1692 } 1693 1694 /* 1695 * Get scsi driver to send a "RESET" command 1696 */ 1697 int 1698 cd_reset(cd) 1699 struct cd_softc *cd; 1700 { 1701 1702 return (scsipi_command(cd->sc_periph, 0, 0, 0, 0, 1703 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1704 } 1705 1706 /* 1707 * Read subchannel 1708 */ 1709 int 1710 cd_read_subchannel(cd, mode, format, track, data, len, flags) 1711 struct cd_softc *cd; 1712 int mode, format, track, len; 1713 struct cd_sub_channel_info *data; 1714 int flags; 1715 { 1716 struct scsipi_read_subchannel scsipi_cmd; 1717 1718 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1719 scsipi_cmd.opcode = READ_SUBCHANNEL; 1720 if (mode == CD_MSF_FORMAT) 1721 scsipi_cmd.byte2 |= CD_MSF; 1722 scsipi_cmd.byte3 = SRS_SUBQ; 1723 scsipi_cmd.subchan_format = format; 1724 scsipi_cmd.track = track; 1725 _lto2b(len, scsipi_cmd.data_len); 1726 return (scsipi_command(cd->sc_periph, 1727 (struct scsipi_generic *)&scsipi_cmd, 1728 sizeof(struct scsipi_read_subchannel), (u_char *)data, len, 1729 CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT)); 1730 } 1731 1732 /* 1733 * Read table of contents 1734 */ 1735 int 1736 cd_read_toc(cd, mode, start, data, len, flags, control) 1737 struct cd_softc *cd; 1738 int mode, start, len, control; 1739 void *data; 1740 int flags; 1741 { 1742 struct scsipi_read_toc scsipi_cmd; 1743 int ntoc; 1744 1745 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1746 #if 0 1747 if (len != sizeof(struct ioc_toc_header)) 1748 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 1749 sizeof(struct cd_toc_entry); 1750 else 1751 #endif 1752 ntoc = len; 1753 scsipi_cmd.opcode = READ_TOC; 1754 if (mode == CD_MSF_FORMAT) 1755 scsipi_cmd.byte2 |= CD_MSF; 1756 scsipi_cmd.from_track = start; 1757 _lto2b(ntoc, scsipi_cmd.data_len); 1758 scsipi_cmd.control = control; 1759 return (scsipi_command(cd->sc_periph, 1760 (struct scsipi_generic *)&scsipi_cmd, 1761 sizeof(struct scsipi_read_toc), (u_char *)data, len, CDRETRIES, 1762 30000, NULL, flags | XS_CTL_DATA_IN)); 1763 } 1764 1765 int 1766 cd_load_toc(cd, toc, flags) 1767 struct cd_softc *cd; 1768 struct cd_toc *toc; 1769 int flags; 1770 { 1771 int ntracks, len, error; 1772 1773 if ((error = cd_read_toc(cd, 0, 0, toc, sizeof(toc->header), 1774 flags, 0)) != 0) 1775 return (error); 1776 1777 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 1778 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 1779 sizeof(toc->header); 1780 if ((error = cd_read_toc(cd, CD_MSF_FORMAT, 0, toc, len, 1781 flags, 0)) != 0) 1782 return (error); 1783 return (0); 1784 } 1785 1786 /* 1787 * Get the scsi driver to send a full inquiry to the device and use the 1788 * results to fill out the disk parameter structure. 1789 */ 1790 int 1791 cd_get_parms(cd, flags) 1792 struct cd_softc *cd; 1793 int flags; 1794 { 1795 1796 /* 1797 * give a number of sectors so that sec * trks * cyls 1798 * is <= disk_size 1799 */ 1800 if (cd_size(cd, flags) == 0) 1801 return (ENXIO); 1802 return (0); 1803 } 1804 1805 int 1806 cdsize(dev) 1807 dev_t dev; 1808 { 1809 1810 /* CD-ROMs are read-only. */ 1811 return (-1); 1812 } 1813 1814 int 1815 cddump(dev, blkno, va, size) 1816 dev_t dev; 1817 daddr_t blkno; 1818 caddr_t va; 1819 size_t size; 1820 { 1821 1822 /* Not implemented. */ 1823 return (ENXIO); 1824 } 1825 1826 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 1827 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 1828 1829 int 1830 dvd_auth(cd, a) 1831 struct cd_softc *cd; 1832 dvd_authinfo *a; 1833 { 1834 struct scsipi_generic cmd; 1835 u_int8_t buf[20]; 1836 int error; 1837 1838 memset(cmd.bytes, 0, 15); 1839 memset(buf, 0, sizeof(buf)); 1840 1841 switch (a->type) { 1842 case DVD_LU_SEND_AGID: 1843 cmd.opcode = GPCMD_REPORT_KEY; 1844 cmd.bytes[8] = 8; 1845 cmd.bytes[9] = 0 | (0 << 6); 1846 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1847 CDRETRIES, 30000, NULL, 1848 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1849 if (error) 1850 return (error); 1851 a->lsa.agid = buf[7] >> 6; 1852 return (0); 1853 1854 case DVD_LU_SEND_CHALLENGE: 1855 cmd.opcode = GPCMD_REPORT_KEY; 1856 cmd.bytes[8] = 16; 1857 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 1858 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1859 CDRETRIES, 30000, NULL, 1860 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1861 if (error) 1862 return (error); 1863 dvd_copy_challenge(a->lsc.chal, &buf[4]); 1864 return (0); 1865 1866 case DVD_LU_SEND_KEY1: 1867 cmd.opcode = GPCMD_REPORT_KEY; 1868 cmd.bytes[8] = 12; 1869 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 1870 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1871 CDRETRIES, 30000, NULL, 1872 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1873 if (error) 1874 return (error); 1875 dvd_copy_key(a->lsk.key, &buf[4]); 1876 return (0); 1877 1878 case DVD_LU_SEND_TITLE_KEY: 1879 cmd.opcode = GPCMD_REPORT_KEY; 1880 _lto4b(a->lstk.lba, &cmd.bytes[1]); 1881 cmd.bytes[8] = 12; 1882 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 1883 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1884 CDRETRIES, 30000, NULL, 1885 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1886 if (error) 1887 return (error); 1888 a->lstk.cpm = (buf[4] >> 7) & 1; 1889 a->lstk.cp_sec = (buf[4] >> 6) & 1; 1890 a->lstk.cgms = (buf[4] >> 4) & 3; 1891 dvd_copy_key(a->lstk.title_key, &buf[5]); 1892 return (0); 1893 1894 case DVD_LU_SEND_ASF: 1895 cmd.opcode = GPCMD_REPORT_KEY; 1896 cmd.bytes[8] = 8; 1897 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 1898 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1899 CDRETRIES, 30000, NULL, 1900 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1901 if (error) 1902 return (error); 1903 a->lsasf.asf = buf[7] & 1; 1904 return (0); 1905 1906 case DVD_HOST_SEND_CHALLENGE: 1907 cmd.opcode = GPCMD_SEND_KEY; 1908 cmd.bytes[8] = 16; 1909 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 1910 buf[1] = 14; 1911 dvd_copy_challenge(&buf[4], a->hsc.chal); 1912 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1913 CDRETRIES, 30000, NULL, 1914 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1915 if (error) 1916 return (error); 1917 a->type = DVD_LU_SEND_KEY1; 1918 return (0); 1919 1920 case DVD_HOST_SEND_KEY2: 1921 cmd.opcode = GPCMD_SEND_KEY; 1922 cmd.bytes[8] = 12; 1923 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 1924 buf[1] = 10; 1925 dvd_copy_key(&buf[4], a->hsk.key); 1926 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 12, 1927 CDRETRIES, 30000, NULL, 1928 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1929 if (error) { 1930 a->type = DVD_AUTH_FAILURE; 1931 return (error); 1932 } 1933 a->type = DVD_AUTH_ESTABLISHED; 1934 return (0); 1935 1936 case DVD_INVALIDATE_AGID: 1937 cmd.opcode = GPCMD_REPORT_KEY; 1938 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 1939 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 16, 1940 CDRETRIES, 30000, NULL, 0); 1941 if (error) 1942 return (error); 1943 return (0); 1944 1945 case DVD_LU_SEND_RPC_STATE: 1946 cmd.opcode = GPCMD_REPORT_KEY; 1947 cmd.bytes[8] = 8; 1948 cmd.bytes[9] = 8 | (0 << 6); 1949 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1950 CDRETRIES, 30000, NULL, 1951 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1952 if (error) 1953 return (error); 1954 a->lrpcs.type = (buf[4] >> 6) & 3; 1955 a->lrpcs.vra = (buf[4] >> 3) & 7; 1956 a->lrpcs.ucca = (buf[4]) & 7; 1957 a->lrpcs.region_mask = buf[5]; 1958 a->lrpcs.rpc_scheme = buf[6]; 1959 return (0); 1960 1961 case DVD_HOST_SEND_RPC_STATE: 1962 cmd.opcode = GPCMD_SEND_KEY; 1963 cmd.bytes[8] = 8; 1964 cmd.bytes[9] = 6 | (0 << 6); 1965 buf[1] = 6; 1966 buf[4] = a->hrpcs.pdrc; 1967 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 8, 1968 CDRETRIES, 30000, NULL, 1969 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1970 if (error) 1971 return (error); 1972 return (0); 1973 1974 default: 1975 return (ENOTTY); 1976 } 1977 } 1978 1979 int 1980 dvd_read_physical(cd, s) 1981 struct cd_softc *cd; 1982 dvd_struct *s; 1983 { 1984 struct scsipi_generic cmd; 1985 u_int8_t buf[4 + 4 * 20], *bufp; 1986 int error; 1987 struct dvd_layer *layer; 1988 int i; 1989 1990 memset(cmd.bytes, 0, 15); 1991 memset(buf, 0, sizeof(buf)); 1992 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1993 cmd.bytes[6] = s->type; 1994 _lto2b(sizeof(buf), &cmd.bytes[7]); 1995 1996 cmd.bytes[5] = s->physical.layer_num; 1997 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 1998 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1999 if (error) 2000 return (error); 2001 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0]; i < 4; 2002 i++, bufp += 20, layer++) { 2003 memset(layer, 0, sizeof(*layer)); 2004 layer->book_version = bufp[0] & 0xf; 2005 layer->book_type = bufp[0] >> 4; 2006 layer->min_rate = bufp[1] & 0xf; 2007 layer->disc_size = bufp[1] >> 4; 2008 layer->layer_type = bufp[2] & 0xf; 2009 layer->track_path = (bufp[2] >> 4) & 1; 2010 layer->nlayers = (bufp[2] >> 5) & 3; 2011 layer->track_density = bufp[3] & 0xf; 2012 layer->linear_density = bufp[3] >> 4; 2013 layer->start_sector = _4btol(&bufp[4]); 2014 layer->end_sector = _4btol(&bufp[8]); 2015 layer->end_sector_l0 = _4btol(&bufp[12]); 2016 layer->bca = bufp[16] >> 7; 2017 } 2018 return (0); 2019 } 2020 2021 int 2022 dvd_read_copyright(cd, s) 2023 struct cd_softc *cd; 2024 dvd_struct *s; 2025 { 2026 struct scsipi_generic cmd; 2027 u_int8_t buf[8]; 2028 int error; 2029 2030 memset(cmd.bytes, 0, 15); 2031 memset(buf, 0, sizeof(buf)); 2032 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2033 cmd.bytes[6] = s->type; 2034 _lto2b(sizeof(buf), &cmd.bytes[7]); 2035 2036 cmd.bytes[5] = s->copyright.layer_num; 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->copyright.cpst = buf[4]; 2042 s->copyright.rmi = buf[5]; 2043 return (0); 2044 } 2045 2046 int 2047 dvd_read_disckey(cd, s) 2048 struct cd_softc *cd; 2049 dvd_struct *s; 2050 { 2051 struct scsipi_generic cmd; 2052 u_int8_t *buf; 2053 int error; 2054 2055 buf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2056 if (buf == NULL) 2057 return EIO; 2058 memset(cmd.bytes, 0, 15); 2059 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2060 cmd.bytes[6] = s->type; 2061 _lto2b(4 + 2048, &cmd.bytes[7]); 2062 2063 cmd.bytes[9] = s->disckey.agid << 6; 2064 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 4 + 2048, 2065 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2066 if (error == 0) 2067 memcpy(s->disckey.value, &buf[4], 2048); 2068 free(buf, M_TEMP); 2069 return error; 2070 } 2071 2072 int 2073 dvd_read_bca(cd, s) 2074 struct cd_softc *cd; 2075 dvd_struct *s; 2076 { 2077 struct scsipi_generic cmd; 2078 u_int8_t buf[4 + 188]; 2079 int error; 2080 2081 memset(cmd.bytes, 0, 15); 2082 memset(buf, 0, sizeof(buf)); 2083 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2084 cmd.bytes[6] = s->type; 2085 _lto2b(sizeof(buf), &cmd.bytes[7]); 2086 2087 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, sizeof(buf), 2088 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2089 if (error) 2090 return (error); 2091 s->bca.len = _2btol(&buf[0]); 2092 if (s->bca.len < 12 || s->bca.len > 188) 2093 return (EIO); 2094 memcpy(s->bca.value, &buf[4], s->bca.len); 2095 return (0); 2096 } 2097 2098 int 2099 dvd_read_manufact(cd, s) 2100 struct cd_softc *cd; 2101 dvd_struct *s; 2102 { 2103 struct scsipi_generic cmd; 2104 u_int8_t *buf; 2105 int error; 2106 2107 buf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2108 if (buf == NULL) 2109 return (EIO); 2110 memset(cmd.bytes, 0, 15); 2111 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2112 cmd.bytes[6] = s->type; 2113 _lto2b(4 + 2048, &cmd.bytes[7]); 2114 2115 error = scsipi_command(cd->sc_periph, &cmd, 12, buf, 4 + 2048, 2116 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2117 if (error == 0) { 2118 s->manufact.len = _2btol(&buf[0]); 2119 if (s->manufact.len >= 0 && s->manufact.len <= 2048) 2120 memcpy(s->manufact.value, &buf[4], s->manufact.len); 2121 else 2122 error = EIO; 2123 } 2124 free(buf, M_TEMP); 2125 return error; 2126 } 2127 2128 int 2129 dvd_read_struct(cd, s) 2130 struct cd_softc *cd; 2131 dvd_struct *s; 2132 { 2133 2134 switch (s->type) { 2135 case DVD_STRUCT_PHYSICAL: 2136 return (dvd_read_physical(cd, s)); 2137 case DVD_STRUCT_COPYRIGHT: 2138 return (dvd_read_copyright(cd, s)); 2139 case DVD_STRUCT_DISCKEY: 2140 return (dvd_read_disckey(cd, s)); 2141 case DVD_STRUCT_BCA: 2142 return (dvd_read_bca(cd, s)); 2143 case DVD_STRUCT_MANUFACT: 2144 return (dvd_read_manufact(cd, s)); 2145 default: 2146 return (EINVAL); 2147 } 2148 } 2149