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