cd.c revision 1.223
1/* $NetBSD: cd.c,v 1.223 2005/05/29 22:00:50 christos 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.223 2005/05/29 22:00:50 christos 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 106struct cd_toc { 107 struct ioc_toc_header header; 108 struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */ 109 /* leadout */ 110}; 111 112static void cdstart(struct scsipi_periph *); 113static void cdrestart(void *); 114static void cdminphys(struct buf *); 115static void cdgetdefaultlabel(struct cd_softc *, struct disklabel *); 116static void cdgetdisklabel(struct cd_softc *); 117static void cddone(struct scsipi_xfer *, int); 118static void cdbounce(struct buf *); 119static int cd_interpret_sense(struct scsipi_xfer *); 120static u_long cd_size(struct cd_softc *, int); 121static int cd_play(struct cd_softc *, int, int); 122static int cd_play_tracks(struct cd_softc *, int, int, int, int); 123static int cd_play_msf(struct cd_softc *, int, int, int, int, int, int); 124static int cd_pause(struct cd_softc *, int); 125static int cd_reset(struct cd_softc *); 126static int cd_read_subchannel(struct cd_softc *, int, int, int, 127 struct cd_sub_channel_info *, int, int); 128static int cd_read_toc_f0(struct cd_softc *, int, int, void *, int, int, int); 129static int cd_get_parms(struct cd_softc *, int); 130static int cd_load_toc(struct cd_softc *, struct cd_toc *, int); 131static int cdreadmsaddr(struct cd_softc *, int *); 132 133static int dvd_auth(struct cd_softc *, dvd_authinfo *); 134static int dvd_read_physical(struct cd_softc *, dvd_struct *); 135static int dvd_read_copyright(struct cd_softc *, dvd_struct *); 136static int dvd_read_disckey(struct cd_softc *, dvd_struct *); 137static int dvd_read_bca(struct cd_softc *, dvd_struct *); 138static int dvd_read_manufact(struct cd_softc *, dvd_struct *); 139static int dvd_read_struct(struct cd_softc *, dvd_struct *); 140 141static int cd_mode_sense(struct cd_softc *, u_int8_t, void *, size_t, int, 142 int, int *); 143static int cd_mode_select(struct cd_softc *, u_int8_t, void *, size_t, 144 int, int); 145static int cd_setchan(struct cd_softc *, int, int, int, int, int); 146static int cd_getvol(struct cd_softc *, struct ioc_vol *, int); 147static int cd_setvol(struct cd_softc *, const struct ioc_vol *, int); 148static int cd_set_pa_immed(struct cd_softc *, int); 149static int cd_load_unload(struct cd_softc *, struct ioc_load_unload *); 150static int cd_setblksize(struct cd_softc *); 151 152static int cdmatch(struct device *, struct cfdata *, void *); 153static void cdattach(struct device *, struct device *, void *); 154static int cdactivate(struct device *, enum devact); 155static int cddetach(struct device *, int); 156 157CFATTACH_DECL(cd, sizeof(struct cd_softc), cdmatch, cdattach, cddetach, 158 cdactivate); 159 160extern struct cfdriver cd_cd; 161 162static 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 175static dev_type_open(cdopen); 176static dev_type_close(cdclose); 177static dev_type_read(cdread); 178static dev_type_write(cdwrite); 179static dev_type_ioctl(cdioctl); 180static dev_type_strategy(cdstrategy); 181static dev_type_dump(cddump); 182static dev_type_size(cdsize); 183 184const struct bdevsw cd_bdevsw = { 185 cdopen, cdclose, cdstrategy, cdioctl, cddump, cdsize, D_DISK 186}; 187 188const struct cdevsw cd_cdevsw = { 189 cdopen, cdclose, cdread, cdwrite, cdioctl, 190 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 191}; 192 193static struct dkdriver cddkdriver = { cdstrategy }; 194 195static 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 */ 206static int 207cdmatch(struct device *parent, struct cfdata *match, void *aux) 208{ 209 struct scsipibus_attach_args *sa = aux; 210 int priority; 211 212 (void)scsipi_inqmatch(&sa->sa_inqbuf, 213 cd_patterns, sizeof(cd_patterns) / sizeof(cd_patterns[0]), 214 sizeof(cd_patterns[0]), &priority); 215 216 return (priority); 217} 218 219static void 220cdattach(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 270static int 271cdactivate(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 289static int 290cddetach(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 */ 343static int 344cdopen(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 462out: /* 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 478bad2: 479 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 480 481bad: 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 488bad3: 489 lockmgr(&cd->sc_lock, LK_RELEASE, NULL); 490bad4: 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 */ 500static int 501cdclose(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 */ 545static void 546cdstrategy(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 706bad: 707 bp->b_flags |= B_ERROR; 708done: 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 */ 732static void 733cdstart(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 864static void 865cdrestart(void *v) 866{ 867 int s = splbio(); 868 cdstart((struct scsipi_periph *)v); 869 splx(s); 870} 871 872static void 873cddone(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 894static void 895cdbounce(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 } 973done: 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 981static int 982cd_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 (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && 993 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) 994 return (retval); 995 996 /* 997 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit 998 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then 999 * wait a bit for the drive to spin up 1000 */ 1001 1002 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY && 1003 sense->asc == 0x4 && 1004 sense->ascq == 0x01) { 1005 /* 1006 * Sleep for 5 seconds to wait for the drive to spin up 1007 */ 1008 1009 SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD " 1010 "spinup\n")); 1011 if (!callout_pending(&periph->periph_callout)) 1012 scsipi_periph_freeze(periph, 1); 1013 callout_reset(&periph->periph_callout, 1014 5 * hz, scsipi_periph_timed_thaw, periph); 1015 retval = ERESTART; 1016 } 1017 return (retval); 1018} 1019 1020static void 1021cdminphys(struct buf *bp) 1022{ 1023 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 1024 long xmax; 1025 1026 /* 1027 * If the device is ancient, we want to make sure that 1028 * the transfer fits into a 6-byte cdb. 1029 * 1030 * XXX Note that the SCSI-I spec says that 256-block transfers 1031 * are allowed in a 6-byte read/write, and are specified 1032 * by settng the "length" to 0. However, we're conservative 1033 * here, allowing only 255-block transfers in case an 1034 * ancient device gets confused by length == 0. A length of 0 1035 * in a 10-byte read/write actually means 0 blocks. 1036 */ 1037 if (cd->flags & CDF_ANCIENT) { 1038 xmax = cd->sc_dk.dk_label->d_secsize * 0xff; 1039 1040 if (bp->b_bcount > xmax) 1041 bp->b_bcount = xmax; 1042 } 1043 1044 (*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp); 1045} 1046 1047static int 1048cdread(dev_t dev, struct uio *uio, int ioflag) 1049{ 1050 1051 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 1052} 1053 1054static int 1055cdwrite(dev_t dev, struct uio *uio, int ioflag) 1056{ 1057 1058 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 1059} 1060 1061#if 0 /* XXX Not used */ 1062/* 1063 * conversion between minute-seconde-frame and logical block address 1064 * addresses format 1065 */ 1066static void 1067lba2msf(u_long lba, u_char *m, u_char *s, u_char *f) 1068{ 1069 u_long tmp; 1070 1071 tmp = lba + CD_BLOCK_OFFSET; /* offset of first logical frame */ 1072 tmp &= 0xffffff; /* negative lbas use only 24 bits */ 1073 *m = tmp / (CD_SECS * CD_FRAMES); 1074 tmp %= (CD_SECS * CD_FRAMES); 1075 *s = tmp / CD_FRAMES; 1076 *f = tmp % CD_FRAMES; 1077} 1078 1079static u_long 1080msf2lba(u_char m, u_char s, u_char f) 1081{ 1082 1083 return ((((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET); 1084} 1085#endif /* XXX Not used */ 1086 1087static int 1088cdreadmsaddr(struct cd_softc *cd, int *addr) 1089{ 1090 struct scsipi_periph *periph = cd->sc_periph; 1091 int error; 1092 struct cd_toc toc; 1093 struct cd_toc_entry *cte; 1094 1095 error = cd_read_toc_f0(cd, 0, 0, &toc, 1096 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1097 XS_CTL_DATA_ONSTACK, 1098 0x40 /* control word for "get MS info" */); 1099 1100 if (error) 1101 return (error); 1102 1103 cte = &toc.entries[0]; 1104 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1105 cte->addr.lba = le32toh(cte->addr.lba); 1106 toc.header.len = le16toh(toc.header.len); 1107 } else { 1108 cte->addr.lba = be32toh(cte->addr.lba); 1109 toc.header.len = be16toh(toc.header.len); 1110 } 1111 1112 *addr = (toc.header.len >= 10 && cte->track > 1) ? 1113 cte->addr.lba : 0; 1114 return 0; 1115} 1116 1117/* 1118 * Perform special action on behalf of the user. 1119 * Knows about the internals of this device 1120 */ 1121static int 1122cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1123{ 1124 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 1125 struct scsipi_periph *periph = cd->sc_periph; 1126 int part = CDPART(dev); 1127 int error = 0; 1128#ifdef __HAVE_OLD_DISKLABEL 1129 struct disklabel *newlabel = NULL; 1130#endif 1131 1132 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd)); 1133 1134 /* 1135 * If the device is not valid, some IOCTLs can still be 1136 * handled on the raw partition. Check this here. 1137 */ 1138 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 1139 switch (cmd) { 1140 case DIOCWLABEL: 1141 case DIOCLOCK: 1142 case ODIOCEJECT: 1143 case DIOCEJECT: 1144 case SCIOCIDENTIFY: 1145 case OSCIOCIDENTIFY: 1146 case SCIOCCOMMAND: 1147 case SCIOCDEBUG: 1148 case CDIOCGETVOL: 1149 case CDIOCSETVOL: 1150 case CDIOCSETMONO: 1151 case CDIOCSETSTEREO: 1152 case CDIOCSETMUTE: 1153 case CDIOCSETLEFT: 1154 case CDIOCSETRIGHT: 1155 case CDIOCCLOSE: 1156 case CDIOCEJECT: 1157 case CDIOCALLOW: 1158 case CDIOCPREVENT: 1159 case CDIOCSETDEBUG: 1160 case CDIOCCLRDEBUG: 1161 case CDIOCRESET: 1162 case SCIOCRESET: 1163 case CDIOCLOADUNLOAD: 1164 case DVD_AUTH: 1165 case DVD_READ_STRUCT: 1166 if (part == RAW_PART) 1167 break; 1168 /* FALLTHROUGH */ 1169 default: 1170 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1171 return (ENODEV); 1172 else 1173 return (EIO); 1174 } 1175 } 1176 1177 switch (cmd) { 1178 case DIOCGDINFO: 1179 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 1180 return (0); 1181#ifdef __HAVE_OLD_DISKLABEL 1182 case ODIOCGDINFO: 1183 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1184 if (newlabel == NULL) 1185 return (EIO); 1186 memcpy(newlabel, cd->sc_dk.dk_label, sizeof (*newlabel)); 1187 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1188 error = ENOTTY; 1189 else 1190 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1191 free(newlabel, M_TEMP); 1192 return error; 1193#endif 1194 1195 case DIOCGPART: 1196 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 1197 ((struct partinfo *)addr)->part = 1198 &cd->sc_dk.dk_label->d_partitions[part]; 1199 return (0); 1200 1201 case DIOCWDINFO: 1202 case DIOCSDINFO: 1203#ifdef __HAVE_OLD_DISKLABEL 1204 case ODIOCWDINFO: 1205 case ODIOCSDINFO: 1206#endif 1207 { 1208 struct disklabel *lp; 1209 1210 if ((flag & FWRITE) == 0) 1211 return (EBADF); 1212 1213#ifdef __HAVE_OLD_DISKLABEL 1214 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 1215 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1216 if (newlabel == NULL) 1217 return (EIO); 1218 memset(newlabel, 0, sizeof newlabel); 1219 memcpy(newlabel, addr, sizeof (struct olddisklabel)); 1220 lp = newlabel; 1221 } else 1222#endif 1223 lp = (struct disklabel *)addr; 1224 1225 if ((error = lockmgr(&cd->sc_lock, LK_EXCLUSIVE, NULL)) != 0) 1226 goto bad; 1227 cd->flags |= CDF_LABELLING; 1228 1229 error = setdisklabel(cd->sc_dk.dk_label, 1230 lp, /*cd->sc_dk.dk_openmask : */0, 1231 cd->sc_dk.dk_cpulabel); 1232 if (error == 0) { 1233 /* XXX ? */ 1234 } 1235 1236 cd->flags &= ~CDF_LABELLING; 1237 lockmgr(&cd->sc_lock, LK_RELEASE, NULL); 1238bad: 1239#ifdef __HAVE_OLD_DISKLABEL 1240 if (newlabel != NULL) 1241 free(newlabel, M_TEMP); 1242#endif 1243 return (error); 1244 } 1245 1246 case DIOCWLABEL: 1247 return (EBADF); 1248 1249 case DIOCGDEFLABEL: 1250 cdgetdefaultlabel(cd, (struct disklabel *)addr); 1251 return (0); 1252 1253#ifdef __HAVE_OLD_DISKLABEL 1254 case ODIOCGDEFLABEL: 1255 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1256 if (newlabel == NULL) 1257 return (EIO); 1258 cdgetdefaultlabel(cd, newlabel); 1259 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1260 error = ENOTTY; 1261 else 1262 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1263 free(newlabel, M_TEMP); 1264 return error; 1265#endif 1266 1267 case CDIOCPLAYTRACKS: { 1268 struct ioc_play_track *args = (struct ioc_play_track *)addr; 1269 1270 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1271 return (error); 1272 return (cd_play_tracks(cd, args->start_track, 1273 args->start_index, args->end_track, args->end_index)); 1274 } 1275 case CDIOCPLAYMSF: { 1276 struct ioc_play_msf *args = (struct ioc_play_msf *)addr; 1277 1278 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1279 return (error); 1280 return (cd_play_msf(cd, args->start_m, args->start_s, 1281 args->start_f, args->end_m, args->end_s, args->end_f)); 1282 } 1283 case CDIOCPLAYBLOCKS: { 1284 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; 1285 1286 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1287 return (error); 1288 return (cd_play(cd, args->blk, args->len)); 1289 } 1290 case CDIOCREADSUBCHANNEL: { 1291 struct ioc_read_subchannel *args = 1292 (struct ioc_read_subchannel *)addr; 1293 struct cd_sub_channel_info data; 1294 u_int len = args->data_len; 1295 1296 if (len > sizeof(data) || 1297 len < sizeof(struct cd_sub_channel_header)) 1298 return (EINVAL); 1299 error = cd_read_subchannel(cd, args->address_format, 1300 args->data_format, args->track, &data, len, 1301 XS_CTL_DATA_ONSTACK); 1302 if (error) 1303 return (error); 1304 len = min(len, _2btol(data.header.data_len) + 1305 sizeof(struct cd_sub_channel_header)); 1306 return (copyout(&data, args->data, len)); 1307 } 1308 case CDIOREADTOCHEADER: { 1309 struct ioc_toc_header th; 1310 1311 if ((error = cd_read_toc_f0(cd, 0, 0, &th, sizeof(th), 1312 XS_CTL_DATA_ONSTACK, 0)) != 0) 1313 return (error); 1314 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1315 th.len = le16toh(th.len); 1316 else 1317 th.len = be16toh(th.len); 1318 memcpy(addr, &th, sizeof(th)); 1319 return (0); 1320 } 1321 case CDIOREADTOCENTRYS: { 1322 struct cd_toc toc; 1323 struct ioc_read_toc_entry *te = 1324 (struct ioc_read_toc_entry *)addr; 1325 struct ioc_toc_header *th; 1326 struct cd_toc_entry *cte; 1327 u_int len = te->data_len; 1328 int ntracks; 1329 1330 th = &toc.header; 1331 1332 if (len > sizeof(toc.entries) || 1333 len < sizeof(struct cd_toc_entry)) 1334 return (EINVAL); 1335 error = cd_read_toc_f0(cd, te->address_format, te->starting_track, 1336 &toc, len + sizeof(struct ioc_toc_header), 1337 XS_CTL_DATA_ONSTACK, 0); 1338 if (error) 1339 return (error); 1340 if (te->address_format == CD_LBA_FORMAT) 1341 for (ntracks = 1342 th->ending_track - th->starting_track + 1; 1343 ntracks >= 0; ntracks--) { 1344 cte = &toc.entries[ntracks]; 1345 cte->addr_type = CD_LBA_FORMAT; 1346 if (periph->periph_quirks & PQUIRK_LITTLETOC) 1347 cte->addr.lba = le32toh(cte->addr.lba); 1348 else 1349 cte->addr.lba = be32toh(cte->addr.lba); 1350 } 1351 if (periph->periph_quirks & PQUIRK_LITTLETOC) 1352 th->len = le16toh(th->len); 1353 else 1354 th->len = be16toh(th->len); 1355 len = min(len, th->len - (sizeof(th->starting_track) + 1356 sizeof(th->ending_track))); 1357 return (copyout(toc.entries, te->data, len)); 1358 } 1359 case CDIOREADMSADDR: { 1360 int sessno = *(int*)addr; 1361 1362 if (sessno != 0) 1363 return (EINVAL); 1364 1365 return (cdreadmsaddr(cd, (int*)addr)); 1366 } 1367 case CDIOCSETPATCH: { 1368 struct ioc_patch *arg = (struct ioc_patch *)addr; 1369 1370 return (cd_setchan(cd, arg->patch[0], arg->patch[1], 1371 arg->patch[2], arg->patch[3], 0)); 1372 } 1373 case CDIOCGETVOL: { 1374 struct ioc_vol *arg = (struct ioc_vol *)addr; 1375 1376 return (cd_getvol(cd, arg, 0)); 1377 } 1378 case CDIOCSETVOL: { 1379 struct ioc_vol *arg = (struct ioc_vol *)addr; 1380 1381 return (cd_setvol(cd, arg, 0)); 1382 } 1383 1384 case CDIOCSETMONO: 1385 return (cd_setchan(cd, BOTH_CHANNEL, BOTH_CHANNEL, 1386 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1387 1388 case CDIOCSETSTEREO: 1389 return (cd_setchan(cd, LEFT_CHANNEL, RIGHT_CHANNEL, 1390 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1391 1392 case CDIOCSETMUTE: 1393 return (cd_setchan(cd, MUTE_CHANNEL, MUTE_CHANNEL, 1394 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1395 1396 case CDIOCSETLEFT: 1397 return (cd_setchan(cd, LEFT_CHANNEL, LEFT_CHANNEL, 1398 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1399 1400 case CDIOCSETRIGHT: 1401 return (cd_setchan(cd, RIGHT_CHANNEL, RIGHT_CHANNEL, 1402 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1403 1404 case CDIOCRESUME: 1405 return (cd_pause(cd, PA_RESUME)); 1406 case CDIOCPAUSE: 1407 return (cd_pause(cd, PA_PAUSE)); 1408 case CDIOCSTART: 1409 return (scsipi_start(periph, SSS_START, 0)); 1410 case CDIOCSTOP: 1411 return (scsipi_start(periph, SSS_STOP, 0)); 1412 case CDIOCCLOSE: 1413 return (scsipi_start(periph, SSS_START|SSS_LOEJ, 1414 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1415 case DIOCEJECT: 1416 if (*(int *)addr == 0) { 1417 /* 1418 * Don't force eject: check that we are the only 1419 * partition open. If so, unlock it. 1420 */ 1421 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1422 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1423 cd->sc_dk.dk_openmask) { 1424 error = scsipi_prevent(periph, SPAMR_ALLOW, 1425 XS_CTL_IGNORE_NOT_READY); 1426 if (error) 1427 return (error); 1428 } else { 1429 return (EBUSY); 1430 } 1431 } 1432 /* FALLTHROUGH */ 1433 case CDIOCEJECT: /* FALLTHROUGH */ 1434 case ODIOCEJECT: 1435 return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1436 case CDIOCALLOW: 1437 return (scsipi_prevent(periph, SPAMR_ALLOW, 0)); 1438 case CDIOCPREVENT: 1439 return (scsipi_prevent(periph, SPAMR_PREVENT_DT, 0)); 1440 case DIOCLOCK: 1441 return (scsipi_prevent(periph, 1442 (*(int *)addr) ? SPAMR_PREVENT_DT : SPAMR_ALLOW, 0)); 1443 case CDIOCSETDEBUG: 1444 cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2); 1445 return (0); 1446 case CDIOCCLRDEBUG: 1447 cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2); 1448 return (0); 1449 case CDIOCRESET: 1450 case SCIOCRESET: 1451 return (cd_reset(cd)); 1452 case CDIOCLOADUNLOAD: 1453 return (cd_load_unload(cd, (struct ioc_load_unload *)addr)); 1454 case DVD_AUTH: 1455 return (dvd_auth(cd, (dvd_authinfo *)addr)); 1456 case DVD_READ_STRUCT: 1457 return (dvd_read_struct(cd, (dvd_struct *)addr)); 1458 1459 default: 1460 if (part != RAW_PART) 1461 return (ENOTTY); 1462 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p)); 1463 } 1464 1465#ifdef DIAGNOSTIC 1466 panic("cdioctl: impossible"); 1467#endif 1468} 1469 1470static void 1471cdgetdefaultlabel(struct cd_softc *cd, struct disklabel *lp) 1472{ 1473 int lastsession; 1474 1475 memset(lp, 0, sizeof(struct disklabel)); 1476 1477 lp->d_secsize = cd->params.blksize; 1478 lp->d_ntracks = 1; 1479 lp->d_nsectors = 100; 1480 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1481 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1482 1483 switch (scsipi_periph_bustype(cd->sc_periph)) { 1484 case SCSIPI_BUSTYPE_SCSI: 1485 lp->d_type = DTYPE_SCSI; 1486 break; 1487 case SCSIPI_BUSTYPE_ATAPI: 1488 lp->d_type = DTYPE_ATAPI; 1489 break; 1490 } 1491 /* 1492 * XXX 1493 * We could probe the mode pages to figure out what kind of disc it is. 1494 * Is this worthwhile? 1495 */ 1496 strncpy(lp->d_typename, "mydisc", 16); 1497 strncpy(lp->d_packname, "fictitious", 16); 1498 lp->d_secperunit = cd->params.disksize; 1499 lp->d_rpm = 300; 1500 lp->d_interleave = 1; 1501 lp->d_flags = D_REMOVABLE; 1502 1503 if (cdreadmsaddr(cd, &lastsession) != 0) 1504 lastsession = 0; 1505 1506 lp->d_partitions[0].p_offset = 0; 1507#ifdef notyet /* have to fix bounds_check_with_label() first */ 1508 lp->d_partitions[0].p_size = lp->d_secperunit; 1509#else 1510 lp->d_partitions[0].p_size = 1511 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1512#endif 1513 lp->d_partitions[0].p_cdsession = lastsession; 1514 lp->d_partitions[0].p_fstype = FS_ISO9660; 1515 lp->d_partitions[RAW_PART].p_offset = 0; 1516#ifdef notyet 1517 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 1518#else 1519 lp->d_partitions[RAW_PART].p_size = 1520 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1521#endif 1522 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1523 lp->d_npartitions = RAW_PART + 1; 1524 1525 lp->d_magic = DISKMAGIC; 1526 lp->d_magic2 = DISKMAGIC; 1527 lp->d_checksum = dkcksum(lp); 1528} 1529 1530/* 1531 * Load the label information on the named device 1532 * Actually fabricate a disklabel 1533 * 1534 * EVENTUALLY take information about different 1535 * data tracks from the TOC and put it in the disklabel 1536 */ 1537static void 1538cdgetdisklabel(struct cd_softc *cd) 1539{ 1540 struct disklabel *lp = cd->sc_dk.dk_label; 1541 const char *errstring; 1542 1543 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1544 1545 cdgetdefaultlabel(cd, lp); 1546 1547 /* 1548 * Call the generic disklabel extraction routine 1549 */ 1550 errstring = readdisklabel(MAKECDDEV(0, cd->sc_dev.dv_unit, RAW_PART), 1551 cdstrategy, lp, cd->sc_dk.dk_cpulabel); 1552 if (errstring) { 1553 printf("%s: %s\n", cd->sc_dev.dv_xname, errstring); 1554 goto error; 1555 } 1556 return; 1557 1558error: 1559 /* Reset to default label -- should print a warning */ 1560 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1561 1562 cdgetdefaultlabel(cd, lp); 1563} 1564 1565/* 1566 * Find out from the device what it's capacity is 1567 */ 1568static u_long 1569cd_size(struct cd_softc *cd, int flags) 1570{ 1571 struct scsipi_read_cd_capacity cmd; 1572 struct scsipi_read_cd_cap_data data; 1573 int blksize; 1574 u_long size; 1575 1576 if (cd->sc_periph->periph_quirks & PQUIRK_NOCAPACITY) { 1577 /* 1578 * the drive doesn't support the READ_CD_CAPACITY command 1579 * use a fake size 1580 */ 1581 cd->params.blksize = 2048; 1582 cd->params.disksize = 400000; 1583 cd->params.disksize512 = 1600000; 1584 return (400000); 1585 } 1586 1587 /* 1588 * make up a scsi command and ask the scsi driver to do 1589 * it for you. 1590 */ 1591 memset(&cmd, 0, sizeof(cmd)); 1592 cmd.opcode = READ_CD_CAPACITY; 1593 1594 /* 1595 * If the command works, interpret the result as a 4 byte 1596 * number of blocks and a blocksize 1597 */ 1598 if (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 1599 (void *)&data, sizeof(data), CDRETRIES, 30000, NULL, 1600 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK) != 0) 1601 return (0); 1602 1603 blksize = _4btol(data.length); 1604 if ((blksize < 512) || ((blksize & 511) != 0)) 1605 blksize = 2048; /* some drives lie ! */ 1606 if (blksize != 2048) { 1607 if (cd_setblksize(cd) == 0) 1608 blksize = 2048; 1609 } 1610 cd->params.blksize = blksize; 1611 1612 size = _4btol(data.addr) + 1; 1613 if (size < 100) 1614 size = 400000; /* ditto */ 1615 cd->params.disksize = size; 1616 cd->params.disksize512 = ((u_int64_t)cd->params.disksize * blksize) / DEV_BSIZE; 1617 1618 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, 1619 ("cd_size: %d %ld\n", blksize, size)); 1620 return (size); 1621} 1622 1623/* 1624 * Get scsi driver to send a "start playing" command 1625 */ 1626static int 1627cd_play(struct cd_softc *cd, int blkno, int nblks) 1628{ 1629 struct scsipi_play cmd; 1630 1631 memset(&cmd, 0, sizeof(cmd)); 1632 cmd.opcode = PLAY; 1633 _lto4b(blkno, cmd.blk_addr); 1634 _lto2b(nblks, cmd.xfer_len); 1635 1636 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1637 CDRETRIES, 30000, NULL, 0)); 1638} 1639 1640/* 1641 * Get scsi driver to send a "start playing" command 1642 */ 1643static int 1644cd_play_tracks(struct cd_softc *cd, int strack, int sindex, int etrack, 1645 int eindex) 1646{ 1647 struct cd_toc toc; 1648 int error; 1649 1650 if (!etrack) 1651 return (EIO); 1652 if (strack > etrack) 1653 return (EINVAL); 1654 1655 if ((error = cd_load_toc(cd, &toc, XS_CTL_DATA_ONSTACK)) != 0) 1656 return (error); 1657 1658 if (++etrack > (toc.header.ending_track+1)) 1659 etrack = toc.header.ending_track+1; 1660 1661 strack -= toc.header.starting_track; 1662 etrack -= toc.header.starting_track; 1663 if (strack < 0) 1664 return (EINVAL); 1665 1666 return (cd_play_msf(cd, toc.entries[strack].addr.msf.minute, 1667 toc.entries[strack].addr.msf.second, 1668 toc.entries[strack].addr.msf.frame, 1669 toc.entries[etrack].addr.msf.minute, 1670 toc.entries[etrack].addr.msf.second, 1671 toc.entries[etrack].addr.msf.frame)); 1672} 1673 1674/* 1675 * Get scsi driver to send a "play msf" command 1676 */ 1677static int 1678cd_play_msf(struct cd_softc *cd, int startm, int starts, int startf, int endm, 1679 int ends, int endf) 1680{ 1681 struct scsipi_play_msf cmd; 1682 1683 memset(&cmd, 0, sizeof(cmd)); 1684 cmd.opcode = PLAY_MSF; 1685 cmd.start_m = startm; 1686 cmd.start_s = starts; 1687 cmd.start_f = startf; 1688 cmd.end_m = endm; 1689 cmd.end_s = ends; 1690 cmd.end_f = endf; 1691 1692 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1693 CDRETRIES, 30000, NULL, 0)); 1694} 1695 1696/* 1697 * Get scsi driver to send a "start up" command 1698 */ 1699static int 1700cd_pause(struct cd_softc *cd, int go) 1701{ 1702 struct scsipi_pause cmd; 1703 1704 memset(&cmd, 0, sizeof(cmd)); 1705 cmd.opcode = PAUSE; 1706 cmd.resume = go & 0xff; 1707 1708 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1709 CDRETRIES, 30000, NULL, 0)); 1710} 1711 1712/* 1713 * Get scsi driver to send a "RESET" command 1714 */ 1715static int 1716cd_reset(struct cd_softc *cd) 1717{ 1718 1719 return (scsipi_command(cd->sc_periph, 0, 0, 0, 0, 1720 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1721} 1722 1723/* 1724 * Read subchannel 1725 */ 1726static int 1727cd_read_subchannel(struct cd_softc *cd, int mode, int format, int track, 1728 struct cd_sub_channel_info *data, int len, int flags) 1729{ 1730 struct scsipi_read_subchannel cmd; 1731 1732 memset(&cmd, 0, sizeof(cmd)); 1733 cmd.opcode = READ_SUBCHANNEL; 1734 if (mode == CD_MSF_FORMAT) 1735 cmd.byte2 |= CD_MSF; 1736 cmd.byte3 = SRS_SUBQ; 1737 cmd.subchan_format = format; 1738 cmd.track = track; 1739 _lto2b(len, cmd.data_len); 1740 1741 return (scsipi_command(cd->sc_periph, 1742 (void *)&cmd, sizeof(struct scsipi_read_subchannel), 1743 (void *)data, len, 1744 CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT)); 1745} 1746 1747/* 1748 * Read table of contents 1749 */ 1750static int 1751cd_read_toc_f0(struct cd_softc *cd, int mode, int start, void *data, int len, 1752 int flags, int control) 1753{ 1754 struct scsipi_read_toc cmd; 1755 int ntoc; 1756 1757 memset(&cmd, 0, sizeof(cmd)); 1758#if 0 1759 if (len != sizeof(struct ioc_toc_header)) 1760 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 1761 sizeof(struct cd_toc_entry); 1762 else 1763#endif 1764 ntoc = len; 1765 cmd.opcode = READ_TOC; 1766 if (mode == CD_MSF_FORMAT) 1767 cmd.addr_mode |= CD_MSF; 1768 cmd.resp_format = 0; 1769 cmd.from_track = start; 1770 _lto2b(ntoc, cmd.data_len); 1771 cmd.control = control; 1772 1773 return (scsipi_command(cd->sc_periph, 1774 (void *)&cmd, sizeof(cmd), (void *)data, len, CDRETRIES, 1775 30000, NULL, flags | XS_CTL_DATA_IN)); 1776} 1777 1778static int 1779cd_load_toc(struct cd_softc *cd, struct cd_toc *toc, int flags) 1780{ 1781 int ntracks, len, error; 1782 1783 if ((error = cd_read_toc_f0(cd, 0, 0, toc, sizeof(toc->header), 1784 flags, 0)) != 0) 1785 return (error); 1786 1787 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 1788 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 1789 sizeof(toc->header); 1790 if ((error = cd_read_toc_f0(cd, CD_MSF_FORMAT, 0, toc, len, 1791 flags, 0)) != 0) 1792 return (error); 1793 return (0); 1794} 1795 1796/* 1797 * Get the scsi driver to send a full inquiry to the device and use the 1798 * results to fill out the disk parameter structure. 1799 */ 1800static int 1801cd_get_parms(struct cd_softc *cd, int flags) 1802{ 1803 1804 /* 1805 * give a number of sectors so that sec * trks * cyls 1806 * is <= disk_size 1807 */ 1808 if (cd_size(cd, flags) == 0) 1809 return (ENXIO); 1810 return (0); 1811} 1812 1813static int 1814cdsize(dev_t dev) 1815{ 1816 1817 /* CD-ROMs are read-only. */ 1818 return (-1); 1819} 1820 1821static int 1822cddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 1823{ 1824 1825 /* Not implemented. */ 1826 return (ENXIO); 1827} 1828 1829#define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 1830#define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 1831 1832static int 1833dvd_auth(struct cd_softc *cd, dvd_authinfo *a) 1834{ 1835 struct scsipi_generic cmd; 1836 u_int8_t bf[20]; 1837 int error; 1838 1839 memset(cmd.bytes, 0, 15); 1840 memset(bf, 0, sizeof(bf)); 1841 1842 switch (a->type) { 1843 case DVD_LU_SEND_AGID: 1844 cmd.opcode = GPCMD_REPORT_KEY; 1845 cmd.bytes[8] = 8; 1846 cmd.bytes[9] = 0 | (0 << 6); 1847 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 1848 CDRETRIES, 30000, NULL, 1849 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1850 if (error) 1851 return (error); 1852 a->lsa.agid = bf[7] >> 6; 1853 return (0); 1854 1855 case DVD_LU_SEND_CHALLENGE: 1856 cmd.opcode = GPCMD_REPORT_KEY; 1857 cmd.bytes[8] = 16; 1858 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 1859 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 1860 CDRETRIES, 30000, NULL, 1861 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1862 if (error) 1863 return (error); 1864 dvd_copy_challenge(a->lsc.chal, &bf[4]); 1865 return (0); 1866 1867 case DVD_LU_SEND_KEY1: 1868 cmd.opcode = GPCMD_REPORT_KEY; 1869 cmd.bytes[8] = 12; 1870 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 1871 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 1872 CDRETRIES, 30000, NULL, 1873 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1874 if (error) 1875 return (error); 1876 dvd_copy_key(a->lsk.key, &bf[4]); 1877 return (0); 1878 1879 case DVD_LU_SEND_TITLE_KEY: 1880 cmd.opcode = GPCMD_REPORT_KEY; 1881 _lto4b(a->lstk.lba, &cmd.bytes[1]); 1882 cmd.bytes[8] = 12; 1883 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 1884 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 1885 CDRETRIES, 30000, NULL, 1886 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1887 if (error) 1888 return (error); 1889 a->lstk.cpm = (bf[4] >> 7) & 1; 1890 a->lstk.cp_sec = (bf[4] >> 6) & 1; 1891 a->lstk.cgms = (bf[4] >> 4) & 3; 1892 dvd_copy_key(a->lstk.title_key, &bf[5]); 1893 return (0); 1894 1895 case DVD_LU_SEND_ASF: 1896 cmd.opcode = GPCMD_REPORT_KEY; 1897 cmd.bytes[8] = 8; 1898 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 1899 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 1900 CDRETRIES, 30000, NULL, 1901 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1902 if (error) 1903 return (error); 1904 a->lsasf.asf = bf[7] & 1; 1905 return (0); 1906 1907 case DVD_HOST_SEND_CHALLENGE: 1908 cmd.opcode = GPCMD_SEND_KEY; 1909 cmd.bytes[8] = 16; 1910 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 1911 bf[1] = 14; 1912 dvd_copy_challenge(&bf[4], a->hsc.chal); 1913 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 1914 CDRETRIES, 30000, NULL, 1915 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1916 if (error) 1917 return (error); 1918 a->type = DVD_LU_SEND_KEY1; 1919 return (0); 1920 1921 case DVD_HOST_SEND_KEY2: 1922 cmd.opcode = GPCMD_SEND_KEY; 1923 cmd.bytes[8] = 12; 1924 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 1925 bf[1] = 10; 1926 dvd_copy_key(&bf[4], a->hsk.key); 1927 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 1928 CDRETRIES, 30000, NULL, 1929 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1930 if (error) { 1931 a->type = DVD_AUTH_FAILURE; 1932 return (error); 1933 } 1934 a->type = DVD_AUTH_ESTABLISHED; 1935 return (0); 1936 1937 case DVD_INVALIDATE_AGID: 1938 cmd.opcode = GPCMD_REPORT_KEY; 1939 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 1940 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 1941 CDRETRIES, 30000, NULL, 0); 1942 if (error) 1943 return (error); 1944 return (0); 1945 1946 case DVD_LU_SEND_RPC_STATE: 1947 cmd.opcode = GPCMD_REPORT_KEY; 1948 cmd.bytes[8] = 8; 1949 cmd.bytes[9] = 8 | (0 << 6); 1950 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 1951 CDRETRIES, 30000, NULL, 1952 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1953 if (error) 1954 return (error); 1955 a->lrpcs.type = (bf[4] >> 6) & 3; 1956 a->lrpcs.vra = (bf[4] >> 3) & 7; 1957 a->lrpcs.ucca = (bf[4]) & 7; 1958 a->lrpcs.region_mask = bf[5]; 1959 a->lrpcs.rpc_scheme = bf[6]; 1960 return (0); 1961 1962 case DVD_HOST_SEND_RPC_STATE: 1963 cmd.opcode = GPCMD_SEND_KEY; 1964 cmd.bytes[8] = 8; 1965 cmd.bytes[9] = 6 | (0 << 6); 1966 bf[1] = 6; 1967 bf[4] = a->hrpcs.pdrc; 1968 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 1969 CDRETRIES, 30000, NULL, 1970 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 1971 if (error) 1972 return (error); 1973 return (0); 1974 1975 default: 1976 return (ENOTTY); 1977 } 1978} 1979 1980static int 1981dvd_read_physical(struct cd_softc *cd, dvd_struct *s) 1982{ 1983 struct scsipi_generic cmd; 1984 u_int8_t bf[4 + 4 * 20], *bufp; 1985 int error; 1986 struct dvd_layer *layer; 1987 int i; 1988 1989 memset(cmd.bytes, 0, 15); 1990 memset(bf, 0, sizeof(bf)); 1991 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 1992 cmd.bytes[6] = s->type; 1993 _lto2b(sizeof(bf), &cmd.bytes[7]); 1994 1995 cmd.bytes[5] = s->physical.layer_num; 1996 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 1997 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 1998 if (error) 1999 return (error); 2000 for (i = 0, bufp = &bf[4], layer = &s->physical.layer[0]; i < 4; 2001 i++, bufp += 20, layer++) { 2002 memset(layer, 0, sizeof(*layer)); 2003 layer->book_version = bufp[0] & 0xf; 2004 layer->book_type = bufp[0] >> 4; 2005 layer->min_rate = bufp[1] & 0xf; 2006 layer->disc_size = bufp[1] >> 4; 2007 layer->layer_type = bufp[2] & 0xf; 2008 layer->track_path = (bufp[2] >> 4) & 1; 2009 layer->nlayers = (bufp[2] >> 5) & 3; 2010 layer->track_density = bufp[3] & 0xf; 2011 layer->linear_density = bufp[3] >> 4; 2012 layer->start_sector = _4btol(&bufp[4]); 2013 layer->end_sector = _4btol(&bufp[8]); 2014 layer->end_sector_l0 = _4btol(&bufp[12]); 2015 layer->bca = bufp[16] >> 7; 2016 } 2017 return (0); 2018} 2019 2020static int 2021dvd_read_copyright(struct cd_softc *cd, dvd_struct *s) 2022{ 2023 struct scsipi_generic cmd; 2024 u_int8_t bf[8]; 2025 int error; 2026 2027 memset(cmd.bytes, 0, 15); 2028 memset(bf, 0, sizeof(bf)); 2029 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2030 cmd.bytes[6] = s->type; 2031 _lto2b(sizeof(bf), &cmd.bytes[7]); 2032 2033 cmd.bytes[5] = s->copyright.layer_num; 2034 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2035 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2036 if (error) 2037 return (error); 2038 s->copyright.cpst = bf[4]; 2039 s->copyright.rmi = bf[5]; 2040 return (0); 2041} 2042 2043static int 2044dvd_read_disckey(struct cd_softc *cd, dvd_struct *s) 2045{ 2046 struct scsipi_generic cmd; 2047 u_int8_t *bf; 2048 int error; 2049 2050 bf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2051 if (bf == NULL) 2052 return EIO; 2053 memset(cmd.bytes, 0, 15); 2054 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2055 cmd.bytes[6] = s->type; 2056 _lto2b(4 + 2048, &cmd.bytes[7]); 2057 2058 cmd.bytes[9] = s->disckey.agid << 6; 2059 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 4 + 2048, 2060 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2061 if (error == 0) 2062 memcpy(s->disckey.value, &bf[4], 2048); 2063 free(bf, M_TEMP); 2064 return error; 2065} 2066 2067static int 2068dvd_read_bca(struct cd_softc *cd, dvd_struct *s) 2069{ 2070 struct scsipi_generic cmd; 2071 u_int8_t bf[4 + 188]; 2072 int error; 2073 2074 memset(cmd.bytes, 0, 15); 2075 memset(bf, 0, sizeof(bf)); 2076 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2077 cmd.bytes[6] = s->type; 2078 _lto2b(sizeof(bf), &cmd.bytes[7]); 2079 2080 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2081 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2082 if (error) 2083 return (error); 2084 s->bca.len = _2btol(&bf[0]); 2085 if (s->bca.len < 12 || s->bca.len > 188) 2086 return (EIO); 2087 memcpy(s->bca.value, &bf[4], s->bca.len); 2088 return (0); 2089} 2090 2091static int 2092dvd_read_manufact(struct cd_softc *cd, dvd_struct *s) 2093{ 2094 struct scsipi_generic cmd; 2095 u_int8_t *bf; 2096 int error; 2097 2098 bf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2099 if (bf == NULL) 2100 return (EIO); 2101 memset(cmd.bytes, 0, 15); 2102 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2103 cmd.bytes[6] = s->type; 2104 _lto2b(4 + 2048, &cmd.bytes[7]); 2105 2106 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 4 + 2048, 2107 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2108 if (error == 0) { 2109 s->manufact.len = _2btol(&bf[0]); 2110 if (s->manufact.len >= 0 && s->manufact.len <= 2048) 2111 memcpy(s->manufact.value, &bf[4], s->manufact.len); 2112 else 2113 error = EIO; 2114 } 2115 free(bf, M_TEMP); 2116 return error; 2117} 2118 2119static int 2120dvd_read_struct(struct cd_softc *cd, dvd_struct *s) 2121{ 2122 2123 switch (s->type) { 2124 case DVD_STRUCT_PHYSICAL: 2125 return (dvd_read_physical(cd, s)); 2126 case DVD_STRUCT_COPYRIGHT: 2127 return (dvd_read_copyright(cd, s)); 2128 case DVD_STRUCT_DISCKEY: 2129 return (dvd_read_disckey(cd, s)); 2130 case DVD_STRUCT_BCA: 2131 return (dvd_read_bca(cd, s)); 2132 case DVD_STRUCT_MANUFACT: 2133 return (dvd_read_manufact(cd, s)); 2134 default: 2135 return (EINVAL); 2136 } 2137} 2138 2139static int 2140cd_mode_sense(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size, 2141 int page, int flags, int *big) 2142{ 2143 2144 if (cd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) { 2145 *big = 1; 2146 return scsipi_mode_sense_big(cd->sc_periph, byte2, page, sense, 2147 size + sizeof(struct scsi_mode_parameter_header_10), 2148 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2149 } else { 2150 *big = 0; 2151 return scsipi_mode_sense(cd->sc_periph, byte2, page, sense, 2152 size + sizeof(struct scsi_mode_parameter_header_6), 2153 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2154 } 2155} 2156 2157static int 2158cd_mode_select(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size, 2159 int flags, int big) 2160{ 2161 2162 if (big) { 2163 struct scsi_mode_parameter_header_10 *header = sense; 2164 2165 _lto2b(0, header->data_length); 2166 return scsipi_mode_select_big(cd->sc_periph, byte2, sense, 2167 size + sizeof(struct scsi_mode_parameter_header_10), 2168 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2169 } else { 2170 struct scsi_mode_parameter_header_6 *header = sense; 2171 2172 header->data_length = 0; 2173 return scsipi_mode_select(cd->sc_periph, byte2, sense, 2174 size + sizeof(struct scsi_mode_parameter_header_6), 2175 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2176 } 2177} 2178 2179static int 2180cd_set_pa_immed(struct cd_softc *cd, int flags) 2181{ 2182 struct { 2183 union { 2184 struct scsi_mode_parameter_header_6 small; 2185 struct scsi_mode_parameter_header_10 big; 2186 } header; 2187 struct cd_audio_page page; 2188 } data; 2189 int error; 2190 uint8_t oflags; 2191 int big, byte2; 2192 struct cd_audio_page *page; 2193 2194 byte2 = SMS_DBD; 2195try_again: 2196 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2197 AUDIO_PAGE, flags, &big)) != 0) { 2198 if (byte2 == SMS_DBD) { 2199 /* Device may not understand DBD; retry without */ 2200 byte2 = 0; 2201 goto try_again; 2202 } 2203 return (error); 2204 } 2205 2206 if (big) 2207 page = (void *)((u_long)&data.header.big + 2208 sizeof data.header.big + 2209 _2btol(data.header.big.blk_desc_len)); 2210 else 2211 page = (void *)((u_long)&data.header.small + 2212 sizeof data.header.small + 2213 data.header.small.blk_desc_len); 2214 2215 oflags = page->flags; 2216 page->flags &= ~CD_PA_SOTC; 2217 page->flags |= CD_PA_IMMED; 2218 if (oflags == page->flags) 2219 return (0); 2220 2221 return (cd_mode_select(cd, SMS_PF, &data, 2222 sizeof(struct scsi_mode_page_header) + page->pg_length, 2223 flags, big)); 2224} 2225 2226static int 2227cd_setchan(struct cd_softc *cd, int p0, int p1, int p2, int p3, int flags) 2228{ 2229 struct { 2230 union { 2231 struct scsi_mode_parameter_header_6 small; 2232 struct scsi_mode_parameter_header_10 big; 2233 } header; 2234 struct cd_audio_page page; 2235 } data; 2236 int error; 2237 int big, byte2; 2238 struct cd_audio_page *page; 2239 2240 byte2 = SMS_DBD; 2241try_again: 2242 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2243 AUDIO_PAGE, flags, &big)) != 0) { 2244 if (byte2 == SMS_DBD) { 2245 /* Device may not understand DBD; retry without */ 2246 byte2 = 0; 2247 goto try_again; 2248 } 2249 return (error); 2250 } 2251 2252 if (big) 2253 page = (void *)((u_long)&data.header.big + 2254 sizeof data.header.big + 2255 _2btol(data.header.big.blk_desc_len)); 2256 else 2257 page = (void *)((u_long)&data.header.small + 2258 sizeof data.header.small + 2259 data.header.small.blk_desc_len); 2260 2261 page->port[0].channels = p0; 2262 page->port[1].channels = p1; 2263 page->port[2].channels = p2; 2264 page->port[3].channels = p3; 2265 2266 return (cd_mode_select(cd, SMS_PF, &data, 2267 sizeof(struct scsi_mode_page_header) + page->pg_length, 2268 flags, big)); 2269} 2270 2271static int 2272cd_getvol(struct cd_softc *cd, struct ioc_vol *arg, int flags) 2273{ 2274 struct { 2275 union { 2276 struct scsi_mode_parameter_header_6 small; 2277 struct scsi_mode_parameter_header_10 big; 2278 } header; 2279 struct cd_audio_page page; 2280 } data; 2281 int error; 2282 int big, byte2; 2283 struct cd_audio_page *page; 2284 2285 byte2 = SMS_DBD; 2286try_again: 2287 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2288 AUDIO_PAGE, flags, &big)) != 0) { 2289 if (byte2 == SMS_DBD) { 2290 /* Device may not understand DBD; retry without */ 2291 byte2 = 0; 2292 goto try_again; 2293 } 2294 return (error); 2295 } 2296 2297 if (big) 2298 page = (void *)((u_long)&data.header.big + 2299 sizeof data.header.big + 2300 _2btol(data.header.big.blk_desc_len)); 2301 else 2302 page = (void *)((u_long)&data.header.small + 2303 sizeof data.header.small + 2304 data.header.small.blk_desc_len); 2305 2306 arg->vol[0] = page->port[0].volume; 2307 arg->vol[1] = page->port[1].volume; 2308 arg->vol[2] = page->port[2].volume; 2309 arg->vol[3] = page->port[3].volume; 2310 2311 return (0); 2312} 2313 2314static int 2315cd_setvol(struct cd_softc *cd, const struct ioc_vol *arg, int flags) 2316{ 2317 struct { 2318 union { 2319 struct scsi_mode_parameter_header_6 small; 2320 struct scsi_mode_parameter_header_10 big; 2321 } header; 2322 struct cd_audio_page page; 2323 } data, mask; 2324 int error; 2325 int big, byte2; 2326 struct cd_audio_page *page, *page2; 2327 2328 byte2 = SMS_DBD; 2329try_again: 2330 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2331 AUDIO_PAGE, flags, &big)) != 0) { 2332 if (byte2 == SMS_DBD) { 2333 /* Device may not understand DBD; retry without */ 2334 byte2 = 0; 2335 goto try_again; 2336 } 2337 return (error); 2338 } 2339 if ((error = cd_mode_sense(cd, byte2, &mask, sizeof(mask.page), 2340 AUDIO_PAGE|SMS_PCTRL_CHANGEABLE, flags, &big)) != 0) 2341 return (error); 2342 2343 if (big) { 2344 page = (void *)((u_long)&data.header.big + 2345 sizeof data.header.big + 2346 _2btol(data.header.big.blk_desc_len)); 2347 page2 = (void *)((u_long)&mask.header.big + 2348 sizeof mask.header.big + 2349 _2btol(mask.header.big.blk_desc_len)); 2350 } else { 2351 page = (void *)((u_long)&data.header.small + 2352 sizeof data.header.small + 2353 data.header.small.blk_desc_len); 2354 page2 = (void *)((u_long)&mask.header.small + 2355 sizeof mask.header.small + 2356 mask.header.small.blk_desc_len); 2357 } 2358 2359 page->port[0].volume = arg->vol[0] & page2->port[0].volume; 2360 page->port[1].volume = arg->vol[1] & page2->port[1].volume; 2361 page->port[2].volume = arg->vol[2] & page2->port[2].volume; 2362 page->port[3].volume = arg->vol[3] & page2->port[3].volume; 2363 2364 page->port[0].channels = CHANNEL_0; 2365 page->port[1].channels = CHANNEL_1; 2366 2367 return (cd_mode_select(cd, SMS_PF, &data, 2368 sizeof(struct scsi_mode_page_header) + page->pg_length, 2369 flags, big)); 2370} 2371 2372static int 2373cd_load_unload(struct cd_softc *cd, struct ioc_load_unload *args) 2374{ 2375 struct scsipi_load_unload cmd; 2376 2377 memset(&cmd, 0, sizeof(cmd)); 2378 cmd.opcode = LOAD_UNLOAD; 2379 cmd.options = args->options; /* ioctl uses MMC values */ 2380 cmd.slot = args->slot; 2381 2382 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 2383 CDRETRIES, 200000, NULL, 0)); 2384} 2385 2386static int 2387cd_setblksize(struct cd_softc *cd) 2388{ 2389 struct { 2390 union { 2391 struct scsi_mode_parameter_header_6 small; 2392 struct scsi_mode_parameter_header_10 big; 2393 } header; 2394 struct scsi_general_block_descriptor blk_desc; 2395 } data; 2396 int error; 2397 int big, bsize; 2398 struct scsi_general_block_descriptor *bdesc; 2399 2400 if ((error = cd_mode_sense(cd, 0, &data, sizeof(data.blk_desc), 0, 0, 2401 &big)) != 0) 2402 return (error); 2403 2404 if (big) { 2405 bdesc = (void *)(&data.header.big + 1); 2406 bsize = _2btol(data.header.big.blk_desc_len); 2407 } else { 2408 bdesc = (void *)(&data.header.small + 1); 2409 bsize = data.header.small.blk_desc_len; 2410 } 2411 2412 if (bsize == 0) { 2413printf("cd_setblksize: trying to change bsize, but no blk_desc\n"); 2414 return (EINVAL); 2415 } 2416 if (_3btol(bdesc->blklen) == 2048) { 2417printf("cd_setblksize: trying to change bsize, but blk_desc is correct\n"); 2418 return (EINVAL); 2419 } 2420 2421 _lto3b(2048, bdesc->blklen); 2422 2423 return (cd_mode_select(cd, SMS_PF, &data, sizeof(data.blk_desc), 0, 2424 big)); 2425} 2426