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