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