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