1 /* $NetBSD: cd.c,v 1.271 2007/12/09 20:28:22 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2001, 2003, 2004, 2005 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 * MMC discinfo/trackinfo contributed to the NetBSD Foundation by Reinoud 11 * Zandijk. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /* 43 * Originally written by Julian Elischer (julian@tfs.com) 44 * for TRW Financial Systems for use under the MACH(2.5) operating system. 45 * 46 * TRW Financial Systems, in accordance with their agreement with Carnegie 47 * Mellon University, makes this software available to CMU to distribute 48 * or use in any manner that they see fit as long as this message is kept with 49 * the software. For this reason TFS also grants any other persons or 50 * organisations permission to use or modify this software. 51 * 52 * TFS supplies this software to be publicly redistributed 53 * on the understanding that TFS is not responsible for the correct 54 * functioning of this software in any circumstances. 55 * 56 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 57 */ 58 59 #include <sys/cdefs.h> 60 __KERNEL_RCSID(0, "$NetBSD: cd.c,v 1.271 2007/12/09 20:28:22 jmcneill Exp $"); 61 62 #include "rnd.h" 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/kernel.h> 67 #include <sys/file.h> 68 #include <sys/stat.h> 69 #include <sys/ioctl.h> 70 #include <sys/buf.h> 71 #include <sys/bufq.h> 72 #include <sys/uio.h> 73 #include <sys/malloc.h> 74 #include <sys/errno.h> 75 #include <sys/device.h> 76 #include <sys/disklabel.h> 77 #include <sys/disk.h> 78 #include <sys/cdio.h> 79 #include <sys/dvdio.h> 80 #include <sys/scsiio.h> 81 #include <sys/proc.h> 82 #include <sys/conf.h> 83 #include <sys/vnode.h> 84 #if NRND > 0 85 #include <sys/rnd.h> 86 #endif 87 88 #include <dev/scsipi/scsi_spc.h> 89 #include <dev/scsipi/scsipi_all.h> 90 #include <dev/scsipi/scsipi_cd.h> 91 #include <dev/scsipi/scsipi_disk.h> /* rw_big and start_stop come */ 92 #include <dev/scsipi/scsi_all.h> 93 /* from there */ 94 #include <dev/scsipi/scsi_disk.h> /* rw comes from there */ 95 #include <dev/scsipi/scsipiconf.h> 96 #include <dev/scsipi/scsipi_base.h> 97 #include <dev/scsipi/cdvar.h> 98 99 #define CDUNIT(z) DISKUNIT(z) 100 #define CDPART(z) DISKPART(z) 101 #define CDMINOR(unit, part) DISKMINOR(unit, part) 102 #define MAKECDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 103 104 #define MAXTRACK 99 105 #define CD_BLOCK_OFFSET 150 106 #define CD_FRAMES 75 107 #define CD_SECS 60 108 109 #define CD_TOC_FORM 0 /* formatted TOC, exposed to userland */ 110 #define CD_TOC_MSINFO 1 /* multi-session info */ 111 #define CD_TOC_RAW 2 /* raw TOC as on disc, unprocessed */ 112 #define CD_TOC_PMA 3 /* PMA, used as intermediate (rare use) */ 113 #define CD_TOC_ATIP 4 /* pressed space of recordable */ 114 #define CD_TOC_CDTEXT 5 /* special CD-TEXT, rarely used */ 115 116 struct cd_formatted_toc { 117 struct ioc_toc_header header; 118 struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */ 119 /* leadout */ 120 }; 121 122 struct cdbounce { 123 struct buf *obp; /* original buf */ 124 int doff; /* byte offset in orig. buf */ 125 int soff; /* byte offset in bounce buf */ 126 int resid; /* residual i/o in orig. buf */ 127 int bcount; /* actual obp bytes in bounce */ 128 }; 129 130 static void cdstart(struct scsipi_periph *); 131 static void cdrestart(void *); 132 static void cdminphys(struct buf *); 133 static void cdgetdefaultlabel(struct cd_softc *, struct cd_formatted_toc *, 134 struct disklabel *); 135 static void cdgetdisklabel(struct cd_softc *); 136 static void cddone(struct scsipi_xfer *, int); 137 static void cdbounce(struct buf *); 138 static int cd_interpret_sense(struct scsipi_xfer *); 139 static u_long cd_size(struct cd_softc *, int); 140 static int cd_play(struct cd_softc *, int, int); 141 static int cd_play_tracks(struct cd_softc *, struct cd_formatted_toc *, 142 int, int, int, int); 143 static int cd_play_msf(struct cd_softc *, int, int, int, int, int, int); 144 static int cd_pause(struct cd_softc *, int); 145 static int cd_reset(struct cd_softc *); 146 static int cd_read_subchannel(struct cd_softc *, int, int, int, 147 struct cd_sub_channel_info *, int, int); 148 static int cd_read_toc(struct cd_softc *, int, int, int, 149 struct cd_formatted_toc *, int, int, int); 150 static int cd_get_parms(struct cd_softc *, int); 151 static int cd_load_toc(struct cd_softc *, int, struct cd_formatted_toc *, int); 152 static int cdreadmsaddr(struct cd_softc *, struct cd_formatted_toc *,int *); 153 154 static int dvd_auth(struct cd_softc *, dvd_authinfo *); 155 static int dvd_read_physical(struct cd_softc *, dvd_struct *); 156 static int dvd_read_copyright(struct cd_softc *, dvd_struct *); 157 static int dvd_read_disckey(struct cd_softc *, dvd_struct *); 158 static int dvd_read_bca(struct cd_softc *, dvd_struct *); 159 static int dvd_read_manufact(struct cd_softc *, dvd_struct *); 160 static int dvd_read_struct(struct cd_softc *, dvd_struct *); 161 162 static int cd_mode_sense(struct cd_softc *, u_int8_t, void *, size_t, int, 163 int, int *); 164 static int cd_mode_select(struct cd_softc *, u_int8_t, void *, size_t, 165 int, int); 166 static int cd_setchan(struct cd_softc *, int, int, int, int, int); 167 static int cd_getvol(struct cd_softc *, struct ioc_vol *, int); 168 static int cd_setvol(struct cd_softc *, const struct ioc_vol *, int); 169 static int cd_set_pa_immed(struct cd_softc *, int); 170 static int cd_load_unload(struct cd_softc *, struct ioc_load_unload *); 171 static int cd_setblksize(struct cd_softc *); 172 173 static int cdmatch(struct device *, struct cfdata *, void *); 174 static void cdattach(struct device *, struct device *, void *); 175 static int cdactivate(struct device *, enum devact); 176 static int cddetach(struct device *, int); 177 178 static int mmc_getdiscinfo(struct scsipi_periph *, struct mmc_discinfo *); 179 static int mmc_gettrackinfo(struct scsipi_periph *, struct mmc_trackinfo *); 180 181 CFATTACH_DECL(cd, sizeof(struct cd_softc), cdmatch, cdattach, cddetach, 182 cdactivate); 183 184 extern struct cfdriver cd_cd; 185 186 static const struct scsipi_inquiry_pattern cd_patterns[] = { 187 {T_CDROM, T_REMOV, 188 "", "", ""}, 189 {T_WORM, T_REMOV, 190 "", "", ""}, 191 #if 0 192 {T_CDROM, T_REMOV, /* more luns */ 193 "PIONEER ", "CD-ROM DRM-600 ", ""}, 194 #endif 195 {T_DIRECT, T_REMOV, 196 "NEC CD-ROM DRIVE:260", "", ""}, 197 }; 198 199 static dev_type_open(cdopen); 200 static dev_type_close(cdclose); 201 static dev_type_read(cdread); 202 static dev_type_write(cdwrite); 203 static dev_type_ioctl(cdioctl); 204 static dev_type_strategy(cdstrategy); 205 static dev_type_dump(cddump); 206 static dev_type_size(cdsize); 207 208 const struct bdevsw cd_bdevsw = { 209 cdopen, cdclose, cdstrategy, cdioctl, cddump, cdsize, D_DISK 210 }; 211 212 const struct cdevsw cd_cdevsw = { 213 cdopen, cdclose, cdread, cdwrite, cdioctl, 214 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 215 }; 216 217 static struct dkdriver cddkdriver = { cdstrategy, NULL }; 218 219 static const struct scsipi_periphsw cd_switch = { 220 cd_interpret_sense, /* use our error handler first */ 221 cdstart, /* we have a queue, which is started by this */ 222 NULL, /* we do not have an async handler */ 223 cddone, /* deal with stats at interrupt time */ 224 }; 225 226 /* 227 * The routine called by the low level scsi routine when it discovers 228 * A device suitable for this driver 229 */ 230 static int 231 cdmatch(struct device *parent, struct cfdata *match, 232 void *aux) 233 { 234 struct scsipibus_attach_args *sa = aux; 235 int priority; 236 237 (void)scsipi_inqmatch(&sa->sa_inqbuf, 238 cd_patterns, sizeof(cd_patterns) / sizeof(cd_patterns[0]), 239 sizeof(cd_patterns[0]), &priority); 240 241 return (priority); 242 } 243 244 static void 245 cdattach(struct device *parent, struct device *self, void *aux) 246 { 247 struct cd_softc *cd = device_private(self); 248 struct scsipibus_attach_args *sa = aux; 249 struct scsipi_periph *periph = sa->sa_periph; 250 251 SC_DEBUG(periph, SCSIPI_DB2, ("cdattach: ")); 252 253 mutex_init(&cd->sc_lock, MUTEX_DEFAULT, IPL_NONE); 254 255 if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI && 256 periph->periph_version == 0) 257 cd->flags |= CDF_ANCIENT; 258 259 bufq_alloc(&cd->buf_queue, "disksort", BUFQ_SORT_RAWBLOCK); 260 261 callout_init(&cd->sc_callout, 0); 262 263 /* 264 * Store information needed to contact our base driver 265 */ 266 cd->sc_periph = periph; 267 268 periph->periph_dev = &cd->sc_dev; 269 periph->periph_switch = &cd_switch; 270 271 /* 272 * Increase our openings to the maximum-per-periph 273 * supported by the adapter. This will either be 274 * clamped down or grown by the adapter if necessary. 275 */ 276 periph->periph_openings = 277 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel); 278 periph->periph_flags |= PERIPH_GROW_OPENINGS; 279 280 /* 281 * Initialize and attach the disk structure. 282 */ 283 disk_init(&cd->sc_dk, cd->sc_dev.dv_xname, &cddkdriver); 284 disk_attach(&cd->sc_dk); 285 286 printf("\n"); 287 288 #if NRND > 0 289 rnd_attach_source(&cd->rnd_source, cd->sc_dev.dv_xname, 290 RND_TYPE_DISK, 0); 291 #endif 292 293 if (!pmf_device_register(self, NULL, NULL)) 294 aprint_error_dev(self, "couldn't establish power handler\n"); 295 } 296 297 static int 298 cdactivate(struct device *self, enum devact act) 299 { 300 int rv = 0; 301 302 switch (act) { 303 case DVACT_ACTIVATE: 304 rv = EOPNOTSUPP; 305 break; 306 307 case DVACT_DEACTIVATE: 308 /* 309 * Nothing to do; we key off the device's DVF_ACTIVE. 310 */ 311 break; 312 } 313 return (rv); 314 } 315 316 static int 317 cddetach(struct device *self, int flags) 318 { 319 struct cd_softc *cd = device_private(self); 320 int s, bmaj, cmaj, i, mn; 321 322 /* locate the major number */ 323 bmaj = bdevsw_lookup_major(&cd_bdevsw); 324 cmaj = cdevsw_lookup_major(&cd_cdevsw); 325 /* Nuke the vnodes for any open instances */ 326 for (i = 0; i < MAXPARTITIONS; i++) { 327 mn = CDMINOR(device_unit(self), i); 328 vdevgone(bmaj, mn, mn, VBLK); 329 vdevgone(cmaj, mn, mn, VCHR); 330 } 331 332 /* kill any pending restart */ 333 callout_stop(&cd->sc_callout); 334 335 s = splbio(); 336 337 /* Kill off any queued buffers. */ 338 bufq_drain(cd->buf_queue); 339 340 bufq_free(cd->buf_queue); 341 342 /* Kill off any pending commands. */ 343 scsipi_kill_pending(cd->sc_periph); 344 345 splx(s); 346 347 mutex_destroy(&cd->sc_lock); 348 349 /* Detach from the disk list. */ 350 disk_detach(&cd->sc_dk); 351 disk_destroy(&cd->sc_dk); 352 353 #if 0 354 /* Get rid of the shutdown hook. */ 355 if (cd->sc_sdhook != NULL) 356 shutdownhook_disestablish(cd->sc_sdhook); 357 #endif 358 359 #if NRND > 0 360 /* Unhook the entropy source. */ 361 rnd_detach_source(&cd->rnd_source); 362 #endif 363 364 return (0); 365 } 366 367 /* 368 * open the device. Make sure the partition info is a up-to-date as can be. 369 */ 370 static int 371 cdopen(dev_t dev, int flag, int fmt, struct lwp *l) 372 { 373 struct cd_softc *cd; 374 struct scsipi_periph *periph; 375 struct scsipi_adapter *adapt; 376 int unit, part; 377 int error; 378 int rawpart; 379 380 unit = CDUNIT(dev); 381 if (unit >= cd_cd.cd_ndevs) 382 return (ENXIO); 383 cd = cd_cd.cd_devs[unit]; 384 if (cd == NULL) 385 return (ENXIO); 386 387 periph = cd->sc_periph; 388 adapt = periph->periph_channel->chan_adapter; 389 part = CDPART(dev); 390 391 SC_DEBUG(periph, SCSIPI_DB1, 392 ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit, 393 cd_cd.cd_ndevs, CDPART(dev))); 394 395 /* 396 * If this is the first open of this device, add a reference 397 * to the adapter. 398 */ 399 if (cd->sc_dk.dk_openmask == 0 && 400 (error = scsipi_adapter_addref(adapt)) != 0) 401 return (error); 402 403 mutex_enter(&cd->sc_lock); 404 405 rawpart = (part == RAW_PART && fmt == S_IFCHR); 406 if ((periph->periph_flags & PERIPH_OPEN) != 0) { 407 /* 408 * If any partition is open, but the disk has been invalidated, 409 * disallow further opens. 410 */ 411 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 && 412 !rawpart) { 413 error = EIO; 414 goto bad3; 415 } 416 } else { 417 int silent; 418 419 if (rawpart) 420 silent = XS_CTL_SILENT; 421 else 422 silent = 0; 423 424 /* Check that it is still responding and ok. */ 425 error = scsipi_test_unit_ready(periph, 426 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 427 silent); 428 429 /* 430 * Start the pack spinning if necessary. Always allow the 431 * raw parition to be opened, for raw IOCTLs. Data transfers 432 * will check for SDEV_MEDIA_LOADED. 433 */ 434 if (error == EIO) { 435 int error2; 436 437 error2 = scsipi_start(periph, SSS_START, silent); 438 switch (error2) { 439 case 0: 440 error = 0; 441 break; 442 case EIO: 443 case EINVAL: 444 break; 445 default: 446 error = error2; 447 break; 448 } 449 } 450 if (error) { 451 if (rawpart) 452 goto out; 453 goto bad3; 454 } 455 456 periph->periph_flags |= PERIPH_OPEN; 457 458 /* Lock the pack in. */ 459 error = scsipi_prevent(periph, SPAMR_PREVENT_DT, 460 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 461 SC_DEBUG(periph, SCSIPI_DB1, 462 ("cdopen: scsipi_prevent, error=%d\n", error)); 463 if (error) { 464 if (rawpart) 465 goto out; 466 goto bad; 467 } 468 469 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 470 /* Load the physical device parameters. */ 471 if (cd_get_parms(cd, 0) != 0) { 472 if (rawpart) 473 goto out; 474 error = ENXIO; 475 goto bad; 476 } 477 periph->periph_flags |= PERIPH_MEDIA_LOADED; 478 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded ")); 479 480 /* Fabricate a disk label. */ 481 cdgetdisklabel(cd); 482 SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel fabricated ")); 483 } 484 } 485 486 /* Check that the partition exists. */ 487 if (part != RAW_PART && 488 (part >= cd->sc_dk.dk_label->d_npartitions || 489 cd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 490 error = ENXIO; 491 goto bad; 492 } 493 494 out: /* Insure only one open at a time. */ 495 switch (fmt) { 496 case S_IFCHR: 497 cd->sc_dk.dk_copenmask |= (1 << part); 498 break; 499 case S_IFBLK: 500 cd->sc_dk.dk_bopenmask |= (1 << part); 501 break; 502 } 503 cd->sc_dk.dk_openmask = 504 cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 505 506 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 507 mutex_exit(&cd->sc_lock); 508 return (0); 509 510 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 511 512 bad: 513 if (cd->sc_dk.dk_openmask == 0) { 514 scsipi_prevent(periph, SPAMR_ALLOW, 515 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE); 516 periph->periph_flags &= ~PERIPH_OPEN; 517 } 518 519 bad3: 520 mutex_exit(&cd->sc_lock); 521 if (cd->sc_dk.dk_openmask == 0) 522 scsipi_adapter_delref(adapt); 523 return (error); 524 } 525 526 /* 527 * close the device.. only called if we are the LAST 528 * occurence of an open device 529 */ 530 static int 531 cdclose(dev_t dev, int flag, int fmt, struct lwp *l) 532 { 533 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 534 struct scsipi_periph *periph = cd->sc_periph; 535 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 536 int part = CDPART(dev); 537 538 mutex_enter(&cd->sc_lock); 539 540 switch (fmt) { 541 case S_IFCHR: 542 cd->sc_dk.dk_copenmask &= ~(1 << part); 543 break; 544 case S_IFBLK: 545 cd->sc_dk.dk_bopenmask &= ~(1 << part); 546 break; 547 } 548 cd->sc_dk.dk_openmask = 549 cd->sc_dk.dk_copenmask | cd->sc_dk.dk_bopenmask; 550 551 if (cd->sc_dk.dk_openmask == 0) { 552 scsipi_wait_drain(periph); 553 554 scsipi_prevent(periph, SPAMR_ALLOW, 555 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE | 556 XS_CTL_IGNORE_NOT_READY); 557 periph->periph_flags &= ~PERIPH_OPEN; 558 559 scsipi_wait_drain(periph); 560 561 scsipi_adapter_delref(adapt); 562 } 563 564 mutex_exit(&cd->sc_lock); 565 return (0); 566 } 567 568 /* 569 * Actually translate the requested transfer into one the physical driver can 570 * understand. The transfer is described by a buf and will include only one 571 * physical transfer. 572 */ 573 static void 574 cdstrategy(struct buf *bp) 575 { 576 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 577 struct disklabel *lp; 578 struct scsipi_periph *periph = cd->sc_periph; 579 daddr_t blkno; 580 int s; 581 582 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdstrategy ")); 583 SC_DEBUG(cd->sc_periph, SCSIPI_DB1, 584 ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno)); 585 /* 586 * If the device has been made invalid, error out 587 * maybe the media changed 588 */ 589 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 590 if (periph->periph_flags & PERIPH_OPEN) 591 bp->b_error = EIO; 592 else 593 bp->b_error = ENODEV; 594 goto done; 595 } 596 597 lp = cd->sc_dk.dk_label; 598 599 /* 600 * The transfer must be a whole number of blocks, offset must not 601 * be negative. 602 */ 603 if ((bp->b_bcount % lp->d_secsize) != 0 || 604 bp->b_blkno < 0 ) { 605 bp->b_error = EINVAL; 606 goto done; 607 } 608 /* 609 * If it's a null transfer, return immediately 610 */ 611 if (bp->b_bcount == 0) 612 goto done; 613 614 /* 615 * Do bounds checking, adjust transfer. if error, process. 616 * If end of partition, just return. 617 */ 618 if (CDPART(bp->b_dev) == RAW_PART) { 619 if (bounds_check_with_mediasize(bp, DEV_BSIZE, 620 cd->params.disksize512) <= 0) 621 goto done; 622 } else { 623 if (bounds_check_with_label(&cd->sc_dk, bp, 624 (cd->flags & (CDF_WLABEL|CDF_LABELLING)) != 0) <= 0) 625 goto done; 626 } 627 628 /* 629 * Now convert the block number to absolute and put it in 630 * terms of the device's logical block size. 631 */ 632 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 633 if (CDPART(bp->b_dev) != RAW_PART) 634 blkno += lp->d_partitions[CDPART(bp->b_dev)].p_offset; 635 636 bp->b_rawblkno = blkno; 637 638 /* 639 * If the disklabel sector size does not match the device 640 * sector size we may need to do some extra work. 641 */ 642 if (lp->d_secsize != cd->params.blksize) { 643 644 /* 645 * If the xfer is not a multiple of the device block size 646 * or it is not block aligned, we need to bounce it. 647 */ 648 if ((bp->b_bcount % cd->params.blksize) != 0 || 649 ((blkno * lp->d_secsize) % cd->params.blksize) != 0) { 650 struct cdbounce *bounce; 651 struct buf *nbp; 652 long count; 653 654 if ((bp->b_flags & B_READ) == 0) { 655 656 /* XXXX We don't support bouncing writes. */ 657 bp->b_error = EACCES; 658 goto done; 659 } 660 661 bounce = malloc(sizeof(*bounce), M_DEVBUF, M_NOWAIT); 662 if (!bounce) { 663 /* No memory -- fail the iop. */ 664 bp->b_error = ENOMEM; 665 goto done; 666 } 667 668 bounce->obp = bp; 669 bounce->resid = bp->b_bcount; 670 bounce->doff = 0; 671 count = ((blkno * lp->d_secsize) % cd->params.blksize); 672 bounce->soff = count; 673 count += bp->b_bcount; 674 count = roundup(count, cd->params.blksize); 675 bounce->bcount = bounce->resid; 676 if (count > MAXPHYS) { 677 bounce->bcount = MAXPHYS - bounce->soff; 678 count = MAXPHYS; 679 } 680 681 blkno = ((blkno * lp->d_secsize) / cd->params.blksize); 682 nbp = getiobuf_nowait(); 683 if (!nbp) { 684 /* No memory -- fail the iop. */ 685 free(bounce, M_DEVBUF); 686 bp->b_error = ENOMEM; 687 goto done; 688 } 689 nbp->b_data = malloc(count, M_DEVBUF, M_NOWAIT); 690 if (!nbp->b_data) { 691 /* No memory -- fail the iop. */ 692 free(bounce, M_DEVBUF); 693 putiobuf(nbp); 694 bp->b_error = ENOMEM; 695 goto done; 696 } 697 698 /* Set up the IOP to the bounce buffer. */ 699 nbp->b_error = 0; 700 nbp->b_proc = bp->b_proc; 701 nbp->b_vp = NULLVP; 702 703 nbp->b_bcount = count; 704 nbp->b_bufsize = count; 705 706 nbp->b_rawblkno = blkno; 707 708 nbp->b_flags = bp->b_flags | B_READ | B_CALL; 709 nbp->b_iodone = cdbounce; 710 711 /* store bounce state in b_private and use new buf */ 712 nbp->b_private = bounce; 713 714 BIO_COPYPRIO(nbp, bp); 715 716 bp = nbp; 717 718 } else { 719 /* Xfer is aligned -- just adjust the start block */ 720 bp->b_rawblkno = (blkno * lp->d_secsize) / 721 cd->params.blksize; 722 } 723 } 724 s = splbio(); 725 726 /* 727 * Place it in the queue of disk activities for this disk. 728 * 729 * XXX Only do disksort() if the current operating mode does not 730 * XXX include tagged queueing. 731 */ 732 BUFQ_PUT(cd->buf_queue, bp); 733 734 /* 735 * Tell the device to get going on the transfer if it's 736 * not doing anything, otherwise just wait for completion 737 */ 738 cdstart(cd->sc_periph); 739 740 splx(s); 741 return; 742 743 done: 744 /* 745 * Correctly set the buf to indicate a completed xfer 746 */ 747 bp->b_resid = bp->b_bcount; 748 biodone(bp); 749 } 750 751 /* 752 * cdstart looks to see if there is a buf waiting for the device 753 * and that the device is not already busy. If both are true, 754 * It deques the buf and creates a scsi command to perform the 755 * transfer in the buf. The transfer request will call scsipi_done 756 * on completion, which will in turn call this routine again 757 * so that the next queued transfer is performed. 758 * The bufs are queued by the strategy routine (cdstrategy) 759 * 760 * This routine is also called after other non-queued requests 761 * have been made of the scsi driver, to ensure that the queue 762 * continues to be drained. 763 * 764 * must be called at the correct (highish) spl level 765 * cdstart() is called at splbio from cdstrategy, cdrestart and scsipi_done 766 */ 767 static void 768 cdstart(struct scsipi_periph *periph) 769 { 770 struct cd_softc *cd = (void *)periph->periph_dev; 771 struct buf *bp = 0; 772 struct scsipi_rw_10 cmd_big; 773 struct scsi_rw_6 cmd_small; 774 struct scsipi_generic *cmdp; 775 struct scsipi_xfer *xs; 776 int flags, nblks, cmdlen, error; 777 778 SC_DEBUG(periph, SCSIPI_DB2, ("cdstart ")); 779 /* 780 * Check if the device has room for another command 781 */ 782 while (periph->periph_active < periph->periph_openings) { 783 /* 784 * there is excess capacity, but a special waits 785 * It'll need the adapter as soon as we clear out of the 786 * way and let it run (user level wait). 787 */ 788 if (periph->periph_flags & PERIPH_WAITING) { 789 periph->periph_flags &= ~PERIPH_WAITING; 790 wakeup((void *)periph); 791 return; 792 } 793 794 /* 795 * If the device has become invalid, abort all the 796 * reads and writes until all files have been closed and 797 * re-opened 798 */ 799 if (__predict_false( 800 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) { 801 if ((bp = BUFQ_GET(cd->buf_queue)) != NULL) { 802 bp->b_error = EIO; 803 bp->b_resid = bp->b_bcount; 804 biodone(bp); 805 continue; 806 } else { 807 return; 808 } 809 } 810 811 /* 812 * See if there is a buf with work for us to do.. 813 */ 814 if ((bp = BUFQ_PEEK(cd->buf_queue)) == NULL) 815 return; 816 817 /* 818 * We have a buf, now we should make a command. 819 */ 820 821 nblks = howmany(bp->b_bcount, cd->params.blksize); 822 823 /* 824 * Fill out the scsi command. If the transfer will 825 * fit in a "small" cdb, use it. 826 */ 827 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) && 828 ((nblks & 0xff) == nblks) && 829 !(periph->periph_quirks & PQUIRK_ONLYBIG)) { 830 /* 831 * We can fit in a small cdb. 832 */ 833 memset(&cmd_small, 0, sizeof(cmd_small)); 834 cmd_small.opcode = (bp->b_flags & B_READ) ? 835 SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND; 836 _lto3b(bp->b_rawblkno, cmd_small.addr); 837 cmd_small.length = nblks & 0xff; 838 cmdlen = sizeof(cmd_small); 839 cmdp = (struct scsipi_generic *)&cmd_small; 840 } else { 841 /* 842 * Need a large cdb. 843 */ 844 memset(&cmd_big, 0, sizeof(cmd_big)); 845 cmd_big.opcode = (bp->b_flags & B_READ) ? 846 READ_10 : WRITE_10; 847 _lto4b(bp->b_rawblkno, cmd_big.addr); 848 _lto2b(nblks, cmd_big.length); 849 cmdlen = sizeof(cmd_big); 850 cmdp = (struct scsipi_generic *)&cmd_big; 851 } 852 853 /* Instrumentation. */ 854 disk_busy(&cd->sc_dk); 855 856 /* 857 * Figure out what flags to use. 858 */ 859 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG; 860 if (bp->b_flags & B_READ) 861 flags |= XS_CTL_DATA_IN; 862 else 863 flags |= XS_CTL_DATA_OUT; 864 865 /* 866 * Call the routine that chats with the adapter. 867 * Note: we cannot sleep as we may be an interrupt 868 */ 869 xs = scsipi_make_xs(periph, cmdp, cmdlen, 870 (u_char *)bp->b_data, bp->b_bcount, 871 CDRETRIES, 30000, bp, flags); 872 if (__predict_false(xs == NULL)) { 873 /* 874 * out of memory. Keep this buffer in the queue, and 875 * retry later. 876 */ 877 callout_reset(&cd->sc_callout, hz / 2, cdrestart, 878 periph); 879 return; 880 } 881 /* 882 * need to dequeue the buffer before queuing the command, 883 * because cdstart may be called recursively from the 884 * HBA driver 885 */ 886 #ifdef DIAGNOSTIC 887 if (BUFQ_GET(cd->buf_queue) != bp) 888 panic("cdstart(): dequeued wrong buf"); 889 #else 890 BUFQ_GET(cd->buf_queue); 891 #endif 892 error = scsipi_execute_xs(xs); 893 /* with a scsipi_xfer preallocated, scsipi_command can't fail */ 894 KASSERT(error == 0); 895 } 896 } 897 898 static void 899 cdrestart(void *v) 900 { 901 int s = splbio(); 902 cdstart((struct scsipi_periph *)v); 903 splx(s); 904 } 905 906 static void 907 cddone(struct scsipi_xfer *xs, int error) 908 { 909 struct cd_softc *cd = (void *)xs->xs_periph->periph_dev; 910 struct buf *bp = xs->bp; 911 912 if (bp) { 913 /* note, bp->b_resid is NOT initialised */ 914 bp->b_error = error; 915 bp->b_resid = xs->resid; 916 if (error) { 917 /* on a read/write error bp->b_resid is zero, so fix */ 918 bp->b_resid = bp->b_bcount; 919 } 920 921 disk_unbusy(&cd->sc_dk, bp->b_bcount - bp->b_resid, 922 (bp->b_flags & B_READ)); 923 #if NRND > 0 924 rnd_add_uint32(&cd->rnd_source, bp->b_rawblkno); 925 #endif 926 927 biodone(bp); 928 } 929 } 930 931 static void 932 cdbounce(struct buf *bp) 933 { 934 struct cdbounce *bounce = (struct cdbounce *)bp->b_private; 935 struct buf *obp = bounce->obp; 936 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(obp->b_dev)]; 937 struct disklabel *lp = cd->sc_dk.dk_label; 938 939 if (bp->b_error != 0) { 940 /* EEK propagate the error and free the memory */ 941 goto done; 942 } 943 944 KASSERT(obp->b_flags & B_READ); 945 946 /* copy bounce buffer to final destination */ 947 memcpy((char *)obp->b_data + bounce->doff, 948 (char *)bp->b_data + bounce->soff, bounce->bcount); 949 950 /* check if we need more I/O, i.e. bounce put us over MAXPHYS */ 951 KASSERT(bounce->resid >= bounce->bcount); 952 bounce->resid -= bounce->bcount; 953 if (bounce->resid > 0) { 954 struct buf *nbp; 955 daddr_t blkno; 956 long count; 957 int s; 958 959 blkno = obp->b_rawblkno + 960 ((obp->b_bcount - bounce->resid) / lp->d_secsize); 961 count = ((blkno * lp->d_secsize) % cd->params.blksize); 962 blkno = (blkno * lp->d_secsize) / cd->params.blksize; 963 bounce->soff = count; 964 bounce->doff += bounce->bcount; 965 count += bounce->resid; 966 count = roundup(count, cd->params.blksize); 967 bounce->bcount = bounce->resid; 968 if (count > MAXPHYS) { 969 bounce->bcount = MAXPHYS - bounce->soff; 970 count = MAXPHYS; 971 } 972 973 nbp = getiobuf_nowait(); 974 if (!nbp) { 975 /* No memory -- fail the iop. */ 976 bp->b_error = ENOMEM; 977 goto done; 978 } 979 980 /* Set up the IOP to the bounce buffer. */ 981 nbp->b_error = 0; 982 nbp->b_proc = obp->b_proc; 983 nbp->b_vp = NULLVP; 984 985 nbp->b_bcount = count; 986 nbp->b_bufsize = count; 987 nbp->b_data = bp->b_data; 988 989 nbp->b_rawblkno = blkno; 990 991 nbp->b_flags = obp->b_flags | B_READ | B_CALL; 992 nbp->b_iodone = cdbounce; 993 994 /* store bounce state in b_private and use new buf */ 995 nbp->b_private = bounce; 996 997 BIO_COPYPRIO(nbp, obp); 998 999 bp->b_data = NULL; 1000 putiobuf(bp); 1001 1002 /* enqueue the request and return */ 1003 s = splbio(); 1004 BUFQ_PUT(cd->buf_queue, nbp); 1005 cdstart(cd->sc_periph); 1006 splx(s); 1007 1008 return; 1009 } 1010 1011 done: 1012 obp->b_error = bp->b_error; 1013 obp->b_resid = bp->b_resid; 1014 free(bp->b_data, M_DEVBUF); 1015 free(bounce, M_DEVBUF); 1016 bp->b_data = NULL; 1017 putiobuf(bp); 1018 biodone(obp); 1019 } 1020 1021 static int 1022 cd_interpret_sense(struct scsipi_xfer *xs) 1023 { 1024 struct scsipi_periph *periph = xs->xs_periph; 1025 struct scsi_sense_data *sense = &xs->sense.scsi_sense; 1026 int retval = EJUSTRETURN; 1027 1028 /* 1029 * If it isn't a extended or extended/deferred error, let 1030 * the generic code handle it. 1031 */ 1032 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && 1033 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) 1034 return (retval); 1035 1036 /* 1037 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Logical Unit 1038 * Is In The Process of Becoming Ready" (Sense code 0x04,0x01), then 1039 * wait a bit for the drive to spin up 1040 */ 1041 1042 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY && 1043 sense->asc == 0x4 && 1044 sense->ascq == 0x01) { 1045 /* 1046 * Sleep for 5 seconds to wait for the drive to spin up 1047 */ 1048 1049 SC_DEBUG(periph, SCSIPI_DB1, ("Waiting 5 sec for CD " 1050 "spinup\n")); 1051 if (!callout_pending(&periph->periph_callout)) 1052 scsipi_periph_freeze(periph, 1); 1053 callout_reset(&periph->periph_callout, 1054 5 * hz, scsipi_periph_timed_thaw, periph); 1055 retval = ERESTART; 1056 } 1057 1058 /* 1059 * If we got a "Unit not ready" (SKEY_NOT_READY) and "Long write in 1060 * progress" (Sense code 0x04, 0x08), then wait for the specified 1061 * time 1062 */ 1063 1064 if ((SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY) && 1065 (sense->asc == 0x04) && (sense->ascq == 0x08)) { 1066 /* 1067 * long write in process; we could listen to the delay; but it 1068 * looks like the skey data is not always returned. 1069 */ 1070 /* cd_delay = _2btol(sense->sks.sks_bytes); */ 1071 1072 /* wait for a second and get going again */ 1073 if (!callout_pending(&periph->periph_callout)) 1074 scsipi_periph_freeze(periph, 1); 1075 callout_reset(&periph->periph_callout, 1076 1 * hz, scsipi_periph_timed_thaw, periph); 1077 retval = ERESTART; 1078 } 1079 1080 return (retval); 1081 } 1082 1083 static void 1084 cdminphys(struct buf *bp) 1085 { 1086 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(bp->b_dev)]; 1087 long xmax; 1088 1089 /* 1090 * If the device is ancient, we want to make sure that 1091 * the transfer fits into a 6-byte cdb. 1092 * 1093 * XXX Note that the SCSI-I spec says that 256-block transfers 1094 * are allowed in a 6-byte read/write, and are specified 1095 * by settng the "length" to 0. However, we're conservative 1096 * here, allowing only 255-block transfers in case an 1097 * ancient device gets confused by length == 0. A length of 0 1098 * in a 10-byte read/write actually means 0 blocks. 1099 */ 1100 if (cd->flags & CDF_ANCIENT) { 1101 xmax = cd->sc_dk.dk_label->d_secsize * 0xff; 1102 1103 if (bp->b_bcount > xmax) 1104 bp->b_bcount = xmax; 1105 } 1106 1107 (*cd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp); 1108 } 1109 1110 static int 1111 cdread(dev_t dev, struct uio *uio, int ioflag) 1112 { 1113 return (physio(cdstrategy, NULL, dev, B_READ, cdminphys, uio)); 1114 } 1115 1116 static int 1117 cdwrite(dev_t dev, struct uio *uio, int ioflag) 1118 { 1119 return (physio(cdstrategy, NULL, dev, B_WRITE, cdminphys, uio)); 1120 } 1121 1122 #if 0 /* XXX Not used */ 1123 /* 1124 * conversion between minute-seconde-frame and logical block address 1125 * addresses format 1126 */ 1127 static void 1128 lba2msf(u_long lba, u_char *m, u_char *s, u_char *f) 1129 { 1130 u_long tmp; 1131 1132 tmp = lba + CD_BLOCK_OFFSET; /* offset of first logical frame */ 1133 tmp &= 0xffffff; /* negative lbas use only 24 bits */ 1134 *m = tmp / (CD_SECS * CD_FRAMES); 1135 tmp %= (CD_SECS * CD_FRAMES); 1136 *s = tmp / CD_FRAMES; 1137 *f = tmp % CD_FRAMES; 1138 } 1139 #endif /* XXX Not used */ 1140 1141 /* 1142 * Convert an hour:minute:second:frame address to a logical block adres. In 1143 * theory the number of secs/minute and number of frames/second could be 1144 * configured differently in the device as could the block offset but in 1145 * practice these values are rock solid and most drives don't even allow 1146 * theses values to be changed. 1147 */ 1148 static uint32_t 1149 hmsf2lba(uint8_t h, uint8_t m, uint8_t s, uint8_t f) 1150 { 1151 return (((((uint32_t) h * 60 + m) * CD_SECS) + s) * CD_FRAMES + f) 1152 - CD_BLOCK_OFFSET; 1153 } 1154 1155 static int 1156 cdreadmsaddr(struct cd_softc *cd, struct cd_formatted_toc *toc, int *addr) 1157 { 1158 struct scsipi_periph *periph = cd->sc_periph; 1159 int error; 1160 struct cd_toc_entry *cte; 1161 1162 error = cd_read_toc(cd, CD_TOC_FORM, 0, 0, toc, 1163 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry), 1164 XS_CTL_DATA_ONSTACK, 1165 0x40 /* control word for "get MS info" */); 1166 1167 if (error) 1168 return (error); 1169 1170 cte = &toc->entries[0]; 1171 if (periph->periph_quirks & PQUIRK_LITTLETOC) { 1172 cte->addr.lba = le32toh(cte->addr.lba); 1173 toc->header.len = le16toh(toc->header.len); 1174 } else { 1175 cte->addr.lba = be32toh(cte->addr.lba); 1176 toc->header.len = be16toh(toc->header.len); 1177 } 1178 1179 *addr = (toc->header.len >= 10 && cte->track > 1) ? 1180 cte->addr.lba : 0; 1181 return 0; 1182 } 1183 1184 /* synchronise caches code from sd.c, move to scsipi_ioctl.c ? */ 1185 static int 1186 cdcachesync(struct scsipi_periph *periph, int flags) { 1187 struct scsi_synchronize_cache_10 cmd; 1188 1189 /* 1190 * Issue a SYNCHRONIZE CACHE. MMC devices have to issue with address 0 1191 * and length 0 as it can't synchronise parts of the disc per spec. 1192 * We ignore ILLEGAL REQUEST in the event that the command is not 1193 * supported by the device, and poll for completion so that we know 1194 * that the cache has actually been flushed. 1195 * 1196 * XXX should we handle the PQUIRK_NOSYNCCACHE ? 1197 */ 1198 1199 memset(&cmd, 0, sizeof(cmd)); 1200 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10; 1201 1202 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0, 1203 CDRETRIES, 30000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST)); 1204 } 1205 1206 static int 1207 do_cdioreadentries(struct cd_softc *cd, struct ioc_read_toc_entry *te, 1208 struct cd_formatted_toc *toc) 1209 { 1210 /* READ TOC format 0 command, entries */ 1211 struct ioc_toc_header *th; 1212 struct cd_toc_entry *cte; 1213 u_int len = te->data_len; 1214 int ntracks; 1215 int error; 1216 1217 th = &toc->header; 1218 1219 if (len > sizeof(toc->entries) || 1220 len < sizeof(toc->entries[0])) 1221 return (EINVAL); 1222 error = cd_read_toc(cd, CD_TOC_FORM, te->address_format, 1223 te->starting_track, toc, 1224 sizeof(toc->header) + len, 1225 XS_CTL_DATA_ONSTACK, 0); 1226 if (error) 1227 return (error); 1228 if (te->address_format == CD_LBA_FORMAT) 1229 for (ntracks = 1230 th->ending_track - th->starting_track + 1; 1231 ntracks >= 0; ntracks--) { 1232 cte = &toc->entries[ntracks]; 1233 cte->addr_type = CD_LBA_FORMAT; 1234 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1235 cte->addr.lba = le32toh(cte->addr.lba); 1236 else 1237 cte->addr.lba = be32toh(cte->addr.lba); 1238 } 1239 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1240 th->len = le16toh(th->len); 1241 else 1242 th->len = be16toh(th->len); 1243 return 0; 1244 } 1245 1246 /* 1247 * Perform special action on behalf of the user. 1248 * Knows about the internals of this device 1249 */ 1250 static int 1251 cdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1252 { 1253 struct cd_softc *cd = cd_cd.cd_devs[CDUNIT(dev)]; 1254 struct scsipi_periph *periph = cd->sc_periph; 1255 struct cd_formatted_toc toc; 1256 int part = CDPART(dev); 1257 int error = 0; 1258 int s; 1259 #ifdef __HAVE_OLD_DISKLABEL 1260 struct disklabel *newlabel = NULL; 1261 #endif 1262 1263 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, ("cdioctl 0x%lx ", cmd)); 1264 1265 /* 1266 * If the device is not valid, some IOCTLs can still be 1267 * handled on the raw partition. Check this here. 1268 */ 1269 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) { 1270 switch (cmd) { 1271 case DIOCWLABEL: 1272 case DIOCLOCK: 1273 case ODIOCEJECT: 1274 case DIOCEJECT: 1275 case DIOCCACHESYNC: 1276 case SCIOCIDENTIFY: 1277 case OSCIOCIDENTIFY: 1278 case SCIOCCOMMAND: 1279 case SCIOCDEBUG: 1280 case CDIOCGETVOL: 1281 case CDIOCSETVOL: 1282 case CDIOCSETMONO: 1283 case CDIOCSETSTEREO: 1284 case CDIOCSETMUTE: 1285 case CDIOCSETLEFT: 1286 case CDIOCSETRIGHT: 1287 case CDIOCCLOSE: 1288 case CDIOCEJECT: 1289 case CDIOCALLOW: 1290 case CDIOCPREVENT: 1291 case CDIOCSETDEBUG: 1292 case CDIOCCLRDEBUG: 1293 case CDIOCRESET: 1294 case SCIOCRESET: 1295 case CDIOCLOADUNLOAD: 1296 case DVD_AUTH: 1297 case DVD_READ_STRUCT: 1298 case DIOCGSTRATEGY: 1299 case DIOCSSTRATEGY: 1300 if (part == RAW_PART) 1301 break; 1302 /* FALLTHROUGH */ 1303 default: 1304 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1305 return (ENODEV); 1306 else 1307 return (EIO); 1308 } 1309 } 1310 1311 switch (cmd) { 1312 case DIOCGDINFO: 1313 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 1314 return (0); 1315 #ifdef __HAVE_OLD_DISKLABEL 1316 case ODIOCGDINFO: 1317 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1318 if (newlabel == NULL) 1319 return (EIO); 1320 memcpy(newlabel, cd->sc_dk.dk_label, sizeof (*newlabel)); 1321 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1322 error = ENOTTY; 1323 else 1324 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1325 free(newlabel, M_TEMP); 1326 return error; 1327 #endif 1328 1329 case DIOCGPART: 1330 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 1331 ((struct partinfo *)addr)->part = 1332 &cd->sc_dk.dk_label->d_partitions[part]; 1333 return (0); 1334 1335 case DIOCWDINFO: 1336 case DIOCSDINFO: 1337 #ifdef __HAVE_OLD_DISKLABEL 1338 case ODIOCWDINFO: 1339 case ODIOCSDINFO: 1340 #endif 1341 { 1342 struct disklabel *lp; 1343 1344 if ((flag & FWRITE) == 0) 1345 return (EBADF); 1346 1347 #ifdef __HAVE_OLD_DISKLABEL 1348 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 1349 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1350 if (newlabel == NULL) 1351 return (EIO); 1352 memset(newlabel, 0, sizeof newlabel); 1353 memcpy(newlabel, addr, sizeof (struct olddisklabel)); 1354 lp = newlabel; 1355 } else 1356 #endif 1357 lp = addr; 1358 1359 mutex_enter(&cd->sc_lock); 1360 cd->flags |= CDF_LABELLING; 1361 1362 error = setdisklabel(cd->sc_dk.dk_label, 1363 lp, /*cd->sc_dk.dk_openmask : */0, 1364 cd->sc_dk.dk_cpulabel); 1365 if (error == 0) { 1366 /* XXX ? */ 1367 } 1368 1369 cd->flags &= ~CDF_LABELLING; 1370 mutex_exit(&cd->sc_lock); 1371 #ifdef __HAVE_OLD_DISKLABEL 1372 if (newlabel != NULL) 1373 free(newlabel, M_TEMP); 1374 #endif 1375 return (error); 1376 } 1377 1378 case DIOCWLABEL: 1379 return (EBADF); 1380 1381 case DIOCGDEFLABEL: 1382 cdgetdefaultlabel(cd, &toc, addr); 1383 return (0); 1384 1385 #ifdef __HAVE_OLD_DISKLABEL 1386 case ODIOCGDEFLABEL: 1387 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1388 if (newlabel == NULL) 1389 return (EIO); 1390 cdgetdefaultlabel(cd, &toc, newlabel); 1391 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1392 error = ENOTTY; 1393 else 1394 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1395 free(newlabel, M_TEMP); 1396 return error; 1397 #endif 1398 1399 case CDIOCPLAYTRACKS: { 1400 /* PLAY_MSF command */ 1401 struct ioc_play_track *args = addr; 1402 1403 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1404 return (error); 1405 return (cd_play_tracks(cd, &toc, args->start_track, 1406 args->start_index, args->end_track, args->end_index)); 1407 } 1408 case CDIOCPLAYMSF: { 1409 /* PLAY_MSF command */ 1410 struct ioc_play_msf *args = addr; 1411 1412 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1413 return (error); 1414 return (cd_play_msf(cd, args->start_m, args->start_s, 1415 args->start_f, args->end_m, args->end_s, args->end_f)); 1416 } 1417 case CDIOCPLAYBLOCKS: { 1418 /* PLAY command */ 1419 struct ioc_play_blocks *args = addr; 1420 1421 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1422 return (error); 1423 return (cd_play(cd, args->blk, args->len)); 1424 } 1425 case CDIOCREADSUBCHANNEL: { 1426 /* READ_SUBCHANNEL command */ 1427 struct ioc_read_subchannel *args = addr; 1428 struct cd_sub_channel_info data; 1429 u_int len = args->data_len; 1430 1431 if (len > sizeof(data) || 1432 len < sizeof(struct cd_sub_channel_header)) 1433 return (EINVAL); 1434 error = cd_read_subchannel(cd, args->address_format, 1435 args->data_format, args->track, &data, len, 1436 XS_CTL_DATA_ONSTACK); 1437 if (error) 1438 return (error); 1439 len = min(len, _2btol(data.header.data_len) + 1440 sizeof(struct cd_sub_channel_header)); 1441 return (copyout(&data, args->data, len)); 1442 } 1443 case CDIOCREADSUBCHANNEL_BUF: { 1444 /* As CDIOCREADSUBCHANNEL, but without a 2nd buffer area */ 1445 struct ioc_read_subchannel_buf *args = addr; 1446 if (args->req.data_len != sizeof args->info) 1447 return EINVAL; 1448 return cd_read_subchannel(cd, args->req.address_format, 1449 args->req.data_format, args->req.track, &args->info, 1450 sizeof args->info, XS_CTL_DATA_ONSTACK); 1451 } 1452 case CDIOREADTOCHEADER: { 1453 /* READ TOC format 0 command, static header */ 1454 if ((error = cd_read_toc(cd, CD_TOC_FORM, 0, 0, 1455 &toc, sizeof(toc.header), 1456 XS_CTL_DATA_ONSTACK, 0)) != 0) 1457 return (error); 1458 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1459 toc.header.len = le16toh(toc.header.len); 1460 else 1461 toc.header.len = be16toh(toc.header.len); 1462 memcpy(addr, &toc.header, sizeof(toc.header)); 1463 return (0); 1464 } 1465 case CDIOREADTOCENTRYS: { 1466 struct ioc_read_toc_entry *te = addr; 1467 error = do_cdioreadentries(cd, te, &toc); 1468 if (error != 0) 1469 return error; 1470 return copyout(toc.entries, te->data, min(te->data_len, 1471 toc.header.len - (sizeof(toc.header.starting_track) 1472 + sizeof(toc.header.ending_track)))); 1473 } 1474 case CDIOREADTOCENTRIES_BUF: { 1475 struct ioc_read_toc_entry_buf *te = addr; 1476 error = do_cdioreadentries(cd, &te->req, &toc); 1477 if (error != 0) 1478 return error; 1479 memcpy(te->entry, toc.entries, min(te->req.data_len, 1480 toc.header.len - (sizeof(toc.header.starting_track) 1481 + sizeof(toc.header.ending_track)))); 1482 return 0; 1483 } 1484 case CDIOREADMSADDR: { 1485 /* READ TOC format 0 command, length of first track only */ 1486 int sessno = *(int*)addr; 1487 1488 if (sessno != 0) 1489 return (EINVAL); 1490 1491 return (cdreadmsaddr(cd, &toc, addr)); 1492 } 1493 case CDIOCSETPATCH: { 1494 struct ioc_patch *arg = addr; 1495 1496 return (cd_setchan(cd, arg->patch[0], arg->patch[1], 1497 arg->patch[2], arg->patch[3], 0)); 1498 } 1499 case CDIOCGETVOL: { 1500 /* MODE SENSE command (AUDIO page) */ 1501 struct ioc_vol *arg = addr; 1502 1503 return (cd_getvol(cd, arg, 0)); 1504 } 1505 case CDIOCSETVOL: { 1506 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1507 struct ioc_vol *arg = addr; 1508 1509 return (cd_setvol(cd, arg, 0)); 1510 } 1511 case CDIOCSETMONO: 1512 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1513 return (cd_setchan(cd, BOTH_CHANNEL, BOTH_CHANNEL, 1514 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1515 1516 case CDIOCSETSTEREO: 1517 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1518 return (cd_setchan(cd, LEFT_CHANNEL, RIGHT_CHANNEL, 1519 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1520 1521 case CDIOCSETMUTE: 1522 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1523 return (cd_setchan(cd, MUTE_CHANNEL, MUTE_CHANNEL, 1524 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1525 1526 case CDIOCSETLEFT: 1527 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1528 return (cd_setchan(cd, LEFT_CHANNEL, LEFT_CHANNEL, 1529 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1530 1531 case CDIOCSETRIGHT: 1532 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1533 return (cd_setchan(cd, RIGHT_CHANNEL, RIGHT_CHANNEL, 1534 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1535 1536 case CDIOCRESUME: 1537 /* PAUSE command */ 1538 return (cd_pause(cd, PA_RESUME)); 1539 case CDIOCPAUSE: 1540 /* PAUSE command */ 1541 return (cd_pause(cd, PA_PAUSE)); 1542 case CDIOCSTART: 1543 return (scsipi_start(periph, SSS_START, 0)); 1544 case CDIOCSTOP: 1545 return (scsipi_start(periph, SSS_STOP, 0)); 1546 case CDIOCCLOSE: 1547 return (scsipi_start(periph, SSS_START|SSS_LOEJ, 1548 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1549 case DIOCEJECT: 1550 if (*(int *)addr == 0) { 1551 /* 1552 * Don't force eject: check that we are the only 1553 * partition open. If so, unlock it. 1554 */ 1555 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1556 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1557 cd->sc_dk.dk_openmask) { 1558 error = scsipi_prevent(periph, SPAMR_ALLOW, 1559 XS_CTL_IGNORE_NOT_READY); 1560 if (error) 1561 return (error); 1562 } else { 1563 return (EBUSY); 1564 } 1565 } 1566 /* FALLTHROUGH */ 1567 case CDIOCEJECT: /* FALLTHROUGH */ 1568 case ODIOCEJECT: 1569 return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1570 case DIOCCACHESYNC: 1571 /* SYNCHRONISE CACHES command */ 1572 return (cdcachesync(periph, 0)); 1573 case CDIOCALLOW: 1574 return (scsipi_prevent(periph, SPAMR_ALLOW, 0)); 1575 case CDIOCPREVENT: 1576 return (scsipi_prevent(periph, SPAMR_PREVENT_DT, 0)); 1577 case DIOCLOCK: 1578 return (scsipi_prevent(periph, 1579 (*(int *)addr) ? SPAMR_PREVENT_DT : SPAMR_ALLOW, 0)); 1580 case CDIOCSETDEBUG: 1581 cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2); 1582 return (0); 1583 case CDIOCCLRDEBUG: 1584 cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2); 1585 return (0); 1586 case CDIOCRESET: 1587 case SCIOCRESET: 1588 return (cd_reset(cd)); 1589 case CDIOCLOADUNLOAD: 1590 /* LOAD_UNLOAD command */ 1591 return (cd_load_unload(cd, addr)); 1592 case DVD_AUTH: 1593 /* GPCMD_REPORT_KEY or GPCMD_SEND_KEY command */ 1594 return (dvd_auth(cd, addr)); 1595 case DVD_READ_STRUCT: 1596 /* GPCMD_READ_DVD_STRUCTURE command */ 1597 return (dvd_read_struct(cd, addr)); 1598 case MMCGETDISCINFO: 1599 /* 1600 * GET_CONFIGURATION, READ_DISCINFO, READ_TRACKINFO, 1601 * (READ_TOCf2, READ_CD_CAPACITY and GET_CONFIGURATION) commands 1602 */ 1603 return mmc_getdiscinfo(periph, (struct mmc_discinfo *) addr); 1604 case MMCGETTRACKINFO: 1605 /* READ TOCf2, READ_CD_CAPACITY and READ_TRACKINFO commands */ 1606 return mmc_gettrackinfo(periph, (struct mmc_trackinfo *) addr); 1607 case DIOCGSTRATEGY: 1608 { 1609 struct disk_strategy *dks = addr; 1610 1611 s = splbio(); 1612 strlcpy(dks->dks_name, bufq_getstrategyname(cd->buf_queue), 1613 sizeof(dks->dks_name)); 1614 splx(s); 1615 dks->dks_paramlen = 0; 1616 1617 return 0; 1618 } 1619 case DIOCSSTRATEGY: 1620 { 1621 struct disk_strategy *dks = addr; 1622 struct bufq_state *new; 1623 struct bufq_state *old; 1624 1625 if ((flag & FWRITE) == 0) { 1626 return EBADF; 1627 } 1628 if (dks->dks_param != NULL) { 1629 return EINVAL; 1630 } 1631 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */ 1632 error = bufq_alloc(&new, dks->dks_name, 1633 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK); 1634 if (error) { 1635 return error; 1636 } 1637 s = splbio(); 1638 old = cd->buf_queue; 1639 bufq_move(new, old); 1640 cd->buf_queue = new; 1641 splx(s); 1642 bufq_free(old); 1643 1644 return 0; 1645 } 1646 default: 1647 if (part != RAW_PART) 1648 return (ENOTTY); 1649 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l)); 1650 } 1651 1652 #ifdef DIAGNOSTIC 1653 panic("cdioctl: impossible"); 1654 #endif 1655 } 1656 1657 static void 1658 cdgetdefaultlabel(struct cd_softc *cd, struct cd_formatted_toc *toc, 1659 struct disklabel *lp) 1660 { 1661 int lastsession; 1662 1663 memset(lp, 0, sizeof(struct disklabel)); 1664 1665 lp->d_secsize = cd->params.blksize; 1666 lp->d_ntracks = 1; 1667 lp->d_nsectors = 100; 1668 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1669 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1670 1671 switch (scsipi_periph_bustype(cd->sc_periph)) { 1672 case SCSIPI_BUSTYPE_SCSI: 1673 lp->d_type = DTYPE_SCSI; 1674 break; 1675 case SCSIPI_BUSTYPE_ATAPI: 1676 lp->d_type = DTYPE_ATAPI; 1677 break; 1678 } 1679 /* 1680 * XXX 1681 * We could probe the mode pages to figure out what kind of disc it is. 1682 * Is this worthwhile? 1683 */ 1684 strncpy(lp->d_typename, "mydisc", 16); 1685 strncpy(lp->d_packname, "fictitious", 16); 1686 lp->d_secperunit = cd->params.disksize; 1687 lp->d_rpm = 300; 1688 lp->d_interleave = 1; 1689 lp->d_flags = D_REMOVABLE; 1690 1691 if (cdreadmsaddr(cd, toc, &lastsession) != 0) 1692 lastsession = 0; 1693 1694 lp->d_partitions[0].p_offset = 0; 1695 lp->d_partitions[0].p_size = lp->d_secperunit; 1696 lp->d_partitions[0].p_cdsession = lastsession; 1697 lp->d_partitions[0].p_fstype = FS_ISO9660; 1698 lp->d_partitions[RAW_PART].p_offset = 0; 1699 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 1700 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660; 1701 lp->d_npartitions = RAW_PART + 1; 1702 1703 lp->d_magic = DISKMAGIC; 1704 lp->d_magic2 = DISKMAGIC; 1705 lp->d_checksum = dkcksum(lp); 1706 } 1707 1708 /* 1709 * Load the label information on the named device 1710 * Actually fabricate a disklabel 1711 * 1712 * EVENTUALLY take information about different 1713 * data tracks from the TOC and put it in the disklabel 1714 */ 1715 static void 1716 cdgetdisklabel(struct cd_softc *cd) 1717 { 1718 struct disklabel *lp = cd->sc_dk.dk_label; 1719 struct cd_formatted_toc toc; 1720 const char *errstring; 1721 1722 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1723 1724 cdgetdefaultlabel(cd, &toc, lp); 1725 1726 /* 1727 * Call the generic disklabel extraction routine 1728 */ 1729 errstring = readdisklabel(MAKECDDEV(0, device_unit(&cd->sc_dev), 1730 RAW_PART), cdstrategy, lp, cd->sc_dk.dk_cpulabel); 1731 1732 /* if all went OK, we are passed a NULL error string */ 1733 if (errstring == NULL) 1734 return; 1735 1736 /* Reset to default label -- after printing error and the warning */ 1737 printf("%s: %s\n", cd->sc_dev.dv_xname, errstring); 1738 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1739 cdgetdefaultlabel(cd, &toc, lp); 1740 } 1741 1742 /* 1743 * Reading a discs total capacity is aparently a very difficult issue for the 1744 * SCSI standardisation group. Every disc type seems to have its own 1745 * (re)invented size request method and modifiers. The failsafe way of 1746 * determining the total (max) capacity i.e. not the recorded capacity but the 1747 * total maximum capacity is to request the info on the last track and 1748 * calucate the total size. 1749 * 1750 * For ROM drives, we go for the CD recorded capacity. For recordable devices 1751 * we count. 1752 */ 1753 static int 1754 read_cd_capacity(struct scsipi_periph *periph, u_int *blksize, u_long *size) 1755 { 1756 struct scsipi_read_cd_capacity cap_cmd; 1757 struct scsipi_read_cd_cap_data cap; 1758 struct scsipi_read_discinfo di_cmd; 1759 struct scsipi_read_discinfo_data di; 1760 struct scsipi_read_trackinfo ti_cmd; 1761 struct scsipi_read_trackinfo_data ti; 1762 uint32_t track_start, track_size; 1763 int error, flags, msb, lsb, last_track; 1764 1765 /* if the device doesn't grog capacity, return the dummies */ 1766 if (periph->periph_quirks & PQUIRK_NOCAPACITY) 1767 return 0; 1768 1769 /* first try read CD capacity for blksize and recorded size */ 1770 /* issue the cd capacity request */ 1771 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 1772 memset(&cap_cmd, 0, sizeof(cap_cmd)); 1773 cap_cmd.opcode = READ_CD_CAPACITY; 1774 1775 error = scsipi_command(periph, 1776 (void *) &cap_cmd, sizeof(cap_cmd), 1777 (void *) &cap, sizeof(cap), 1778 CDRETRIES, 30000, NULL, flags); 1779 if (error) 1780 return error; 1781 1782 /* retrieve values and sanity check them */ 1783 *blksize = _4btol(cap.length); 1784 *size = _4btol(cap.addr); 1785 1786 /* blksize is 2048 for CD, but some drives give gibberish */ 1787 if ((*blksize < 512) || ((*blksize & 511) != 0)) 1788 *blksize = 2048; /* some drives lie ! */ 1789 1790 /* recordables have READ_DISCINFO implemented */ 1791 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT; 1792 memset(&di_cmd, 0, sizeof(di_cmd)); 1793 di_cmd.opcode = READ_DISCINFO; 1794 _lto2b(READ_DISCINFO_BIGSIZE, di_cmd.data_len); 1795 1796 error = scsipi_command(periph, 1797 (void *) &di_cmd, sizeof(di_cmd), 1798 (void *) &di, READ_DISCINFO_BIGSIZE, 1799 CDRETRIES, 30000, NULL, flags); 1800 if (error == 0) { 1801 msb = di.last_track_last_session_msb; 1802 lsb = di.last_track_last_session_lsb; 1803 last_track = (msb << 8) | lsb; 1804 1805 /* request info on last track */ 1806 memset(&ti_cmd, 0, sizeof(ti_cmd)); 1807 ti_cmd.opcode = READ_TRACKINFO; 1808 ti_cmd.addr_type = 1; /* on tracknr */ 1809 _lto4b(last_track, ti_cmd.address); /* tracknr */ 1810 _lto2b(sizeof(ti), ti_cmd.data_len); 1811 1812 error = scsipi_command(periph, 1813 (void *) &ti_cmd, sizeof(ti_cmd), 1814 (void *) &ti, sizeof(ti), 1815 CDRETRIES, 30000, NULL, flags); 1816 if (error == 0) { 1817 track_start = _4btol(ti.track_start); 1818 track_size = _4btol(ti.track_size); 1819 1820 /* overwrite only with a sane value */ 1821 if (track_start + track_size >= 100) 1822 *size = track_start + track_size; 1823 } 1824 } 1825 1826 /* sanity check for size */ 1827 if (*size < 100) 1828 *size = 400000; 1829 1830 return 0; 1831 } 1832 1833 /* 1834 * Find out from the device what it's capacity is 1835 */ 1836 static u_long 1837 cd_size(struct cd_softc *cd, int flags) 1838 { 1839 u_int blksize; 1840 u_long size; 1841 int error; 1842 1843 /* set up fake values */ 1844 blksize = 2048; 1845 size = 400000; 1846 1847 /* if this function bounces with an error return fake value */ 1848 error = read_cd_capacity(cd->sc_periph, &blksize, &size); 1849 if (error) 1850 return size; 1851 1852 if (blksize != 2048) { 1853 if (cd_setblksize(cd) == 0) 1854 blksize = 2048; 1855 } 1856 cd->params.blksize = blksize; 1857 cd->params.disksize = size; 1858 cd->params.disksize512 = ((u_int64_t)cd->params.disksize * blksize) / DEV_BSIZE; 1859 1860 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, 1861 ("cd_size: %u %lu\n", blksize, size)); 1862 1863 return size; 1864 } 1865 1866 /* 1867 * Get scsi driver to send a "start playing" command 1868 */ 1869 static int 1870 cd_play(struct cd_softc *cd, int blkno, int nblks) 1871 { 1872 struct scsipi_play cmd; 1873 1874 memset(&cmd, 0, sizeof(cmd)); 1875 cmd.opcode = PLAY; 1876 _lto4b(blkno, cmd.blk_addr); 1877 _lto2b(nblks, cmd.xfer_len); 1878 1879 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1880 CDRETRIES, 30000, NULL, 0)); 1881 } 1882 1883 /* 1884 * Get scsi driver to send a "start playing" command 1885 */ 1886 static int 1887 cd_play_tracks(struct cd_softc *cd, struct cd_formatted_toc *toc, int strack, 1888 int sindex, int etrack, int eindex) 1889 { 1890 int error; 1891 1892 if (!etrack) 1893 return (EIO); 1894 if (strack > etrack) 1895 return (EINVAL); 1896 1897 error = cd_load_toc(cd, CD_TOC_FORM, toc, XS_CTL_DATA_ONSTACK); 1898 if (error) 1899 return (error); 1900 1901 if (++etrack > (toc->header.ending_track+1)) 1902 etrack = toc->header.ending_track+1; 1903 1904 strack -= toc->header.starting_track; 1905 etrack -= toc->header.starting_track; 1906 if (strack < 0) 1907 return (EINVAL); 1908 1909 return (cd_play_msf(cd, toc->entries[strack].addr.msf.minute, 1910 toc->entries[strack].addr.msf.second, 1911 toc->entries[strack].addr.msf.frame, 1912 toc->entries[etrack].addr.msf.minute, 1913 toc->entries[etrack].addr.msf.second, 1914 toc->entries[etrack].addr.msf.frame)); 1915 } 1916 1917 /* 1918 * Get scsi driver to send a "play msf" command 1919 */ 1920 static int 1921 cd_play_msf(struct cd_softc *cd, int startm, int starts, int startf, int endm, 1922 int ends, int endf) 1923 { 1924 struct scsipi_play_msf cmd; 1925 1926 memset(&cmd, 0, sizeof(cmd)); 1927 cmd.opcode = PLAY_MSF; 1928 cmd.start_m = startm; 1929 cmd.start_s = starts; 1930 cmd.start_f = startf; 1931 cmd.end_m = endm; 1932 cmd.end_s = ends; 1933 cmd.end_f = endf; 1934 1935 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1936 CDRETRIES, 30000, NULL, 0)); 1937 } 1938 1939 /* 1940 * Get scsi driver to send a "start up" command 1941 */ 1942 static int 1943 cd_pause(struct cd_softc *cd, int go) 1944 { 1945 struct scsipi_pause cmd; 1946 1947 memset(&cmd, 0, sizeof(cmd)); 1948 cmd.opcode = PAUSE; 1949 cmd.resume = go & 0xff; 1950 1951 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1952 CDRETRIES, 30000, NULL, 0)); 1953 } 1954 1955 /* 1956 * Get scsi driver to send a "RESET" command 1957 */ 1958 static int 1959 cd_reset(struct cd_softc *cd) 1960 { 1961 1962 return (scsipi_command(cd->sc_periph, 0, 0, 0, 0, 1963 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1964 } 1965 1966 /* 1967 * Read subchannel 1968 */ 1969 static int 1970 cd_read_subchannel(struct cd_softc *cd, int mode, int format, int track, 1971 struct cd_sub_channel_info *data, int len, int flags) 1972 { 1973 struct scsipi_read_subchannel cmd; 1974 1975 memset(&cmd, 0, sizeof(cmd)); 1976 cmd.opcode = READ_SUBCHANNEL; 1977 if (mode == CD_MSF_FORMAT) 1978 cmd.byte2 |= CD_MSF; 1979 cmd.byte3 = SRS_SUBQ; 1980 cmd.subchan_format = format; 1981 cmd.track = track; 1982 _lto2b(len, cmd.data_len); 1983 1984 return (scsipi_command(cd->sc_periph, 1985 (void *)&cmd, sizeof(struct scsipi_read_subchannel), 1986 (void *)data, len, 1987 CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT)); 1988 } 1989 1990 /* 1991 * Read table of contents 1992 */ 1993 static int 1994 cd_read_toc(struct cd_softc *cd, int respf, int mode, int start, 1995 struct cd_formatted_toc *toc, int len, int flags, int control) 1996 { 1997 struct scsipi_read_toc cmd; 1998 int ntoc; 1999 2000 memset(&cmd, 0, sizeof(cmd)); 2001 #if 0 2002 if (len != sizeof(struct ioc_toc_header)) 2003 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 2004 sizeof(struct cd_toc_entry); 2005 else 2006 #endif 2007 ntoc = len; 2008 cmd.opcode = READ_TOC; 2009 if (mode == CD_MSF_FORMAT) 2010 cmd.addr_mode |= CD_MSF; 2011 cmd.resp_format = respf; 2012 cmd.from_track = start; 2013 _lto2b(ntoc, cmd.data_len); 2014 cmd.control = control; 2015 2016 return (scsipi_command(cd->sc_periph, 2017 (void *)&cmd, sizeof(cmd), (void *)toc, len, CDRETRIES, 2018 30000, NULL, flags | XS_CTL_DATA_IN)); 2019 } 2020 2021 static int 2022 cd_load_toc(struct cd_softc *cd, int respf, struct cd_formatted_toc *toc, int flags) 2023 { 2024 int ntracks, len, error; 2025 2026 if ((error = cd_read_toc(cd, respf, 0, 0, toc, sizeof(toc->header), 2027 flags, 0)) != 0) 2028 return (error); 2029 2030 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 2031 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 2032 sizeof(toc->header); 2033 if ((error = cd_read_toc(cd, respf, CD_MSF_FORMAT, 0, toc, len, 2034 flags, 0)) != 0) 2035 return (error); 2036 return (0); 2037 } 2038 2039 /* 2040 * Get the scsi driver to send a full inquiry to the device and use the 2041 * results to fill out the disk parameter structure. 2042 */ 2043 static int 2044 cd_get_parms(struct cd_softc *cd, int flags) 2045 { 2046 2047 /* 2048 * give a number of sectors so that sec * trks * cyls 2049 * is <= disk_size 2050 */ 2051 if (cd_size(cd, flags) == 0) 2052 return (ENXIO); 2053 disk_blocksize(&cd->sc_dk, cd->params.blksize); 2054 return (0); 2055 } 2056 2057 static int 2058 cdsize(dev_t dev) 2059 { 2060 2061 /* CD-ROMs are read-only. */ 2062 return (-1); 2063 } 2064 2065 static int 2066 cddump(dev_t dev, daddr_t blkno, void *va, 2067 size_t size) 2068 { 2069 2070 /* Not implemented. */ 2071 return (ENXIO); 2072 } 2073 2074 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 2075 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 2076 2077 static int 2078 dvd_auth(struct cd_softc *cd, dvd_authinfo *a) 2079 { 2080 struct scsipi_generic cmd; 2081 u_int8_t bf[20]; 2082 int error; 2083 2084 memset(cmd.bytes, 0, 15); 2085 memset(bf, 0, sizeof(bf)); 2086 2087 switch (a->type) { 2088 case DVD_LU_SEND_AGID: 2089 cmd.opcode = GPCMD_REPORT_KEY; 2090 cmd.bytes[8] = 8; 2091 cmd.bytes[9] = 0 | (0 << 6); 2092 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2093 CDRETRIES, 30000, NULL, 2094 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2095 if (error) 2096 return (error); 2097 a->lsa.agid = bf[7] >> 6; 2098 return (0); 2099 2100 case DVD_LU_SEND_CHALLENGE: 2101 cmd.opcode = GPCMD_REPORT_KEY; 2102 cmd.bytes[8] = 16; 2103 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 2104 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 2105 CDRETRIES, 30000, NULL, 2106 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2107 if (error) 2108 return (error); 2109 dvd_copy_challenge(a->lsc.chal, &bf[4]); 2110 return (0); 2111 2112 case DVD_LU_SEND_KEY1: 2113 cmd.opcode = GPCMD_REPORT_KEY; 2114 cmd.bytes[8] = 12; 2115 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 2116 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 2117 CDRETRIES, 30000, NULL, 2118 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2119 if (error) 2120 return (error); 2121 dvd_copy_key(a->lsk.key, &bf[4]); 2122 return (0); 2123 2124 case DVD_LU_SEND_TITLE_KEY: 2125 cmd.opcode = GPCMD_REPORT_KEY; 2126 _lto4b(a->lstk.lba, &cmd.bytes[1]); 2127 cmd.bytes[8] = 12; 2128 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 2129 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 2130 CDRETRIES, 30000, NULL, 2131 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2132 if (error) 2133 return (error); 2134 a->lstk.cpm = (bf[4] >> 7) & 1; 2135 a->lstk.cp_sec = (bf[4] >> 6) & 1; 2136 a->lstk.cgms = (bf[4] >> 4) & 3; 2137 dvd_copy_key(a->lstk.title_key, &bf[5]); 2138 return (0); 2139 2140 case DVD_LU_SEND_ASF: 2141 cmd.opcode = GPCMD_REPORT_KEY; 2142 cmd.bytes[8] = 8; 2143 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 2144 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2145 CDRETRIES, 30000, NULL, 2146 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2147 if (error) 2148 return (error); 2149 a->lsasf.asf = bf[7] & 1; 2150 return (0); 2151 2152 case DVD_HOST_SEND_CHALLENGE: 2153 cmd.opcode = GPCMD_SEND_KEY; 2154 cmd.bytes[8] = 16; 2155 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 2156 bf[1] = 14; 2157 dvd_copy_challenge(&bf[4], a->hsc.chal); 2158 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 2159 CDRETRIES, 30000, NULL, 2160 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 2161 if (error) 2162 return (error); 2163 a->type = DVD_LU_SEND_KEY1; 2164 return (0); 2165 2166 case DVD_HOST_SEND_KEY2: 2167 cmd.opcode = GPCMD_SEND_KEY; 2168 cmd.bytes[8] = 12; 2169 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 2170 bf[1] = 10; 2171 dvd_copy_key(&bf[4], a->hsk.key); 2172 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 2173 CDRETRIES, 30000, NULL, 2174 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 2175 if (error) { 2176 a->type = DVD_AUTH_FAILURE; 2177 return (error); 2178 } 2179 a->type = DVD_AUTH_ESTABLISHED; 2180 return (0); 2181 2182 case DVD_INVALIDATE_AGID: 2183 cmd.opcode = GPCMD_REPORT_KEY; 2184 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 2185 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 2186 CDRETRIES, 30000, NULL, 0); 2187 if (error) 2188 return (error); 2189 return (0); 2190 2191 case DVD_LU_SEND_RPC_STATE: 2192 cmd.opcode = GPCMD_REPORT_KEY; 2193 cmd.bytes[8] = 8; 2194 cmd.bytes[9] = 8 | (0 << 6); 2195 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2196 CDRETRIES, 30000, NULL, 2197 XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2198 if (error) 2199 return (error); 2200 a->lrpcs.type = (bf[4] >> 6) & 3; 2201 a->lrpcs.vra = (bf[4] >> 3) & 7; 2202 a->lrpcs.ucca = (bf[4]) & 7; 2203 a->lrpcs.region_mask = bf[5]; 2204 a->lrpcs.rpc_scheme = bf[6]; 2205 return (0); 2206 2207 case DVD_HOST_SEND_RPC_STATE: 2208 cmd.opcode = GPCMD_SEND_KEY; 2209 cmd.bytes[8] = 8; 2210 cmd.bytes[9] = 6 | (0 << 6); 2211 bf[1] = 6; 2212 bf[4] = a->hrpcs.pdrc; 2213 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2214 CDRETRIES, 30000, NULL, 2215 XS_CTL_DATA_OUT|XS_CTL_DATA_ONSTACK); 2216 if (error) 2217 return (error); 2218 return (0); 2219 2220 default: 2221 return (ENOTTY); 2222 } 2223 } 2224 2225 static int 2226 dvd_read_physical(struct cd_softc *cd, dvd_struct *s) 2227 { 2228 struct scsipi_generic cmd; 2229 u_int8_t bf[4 + 4 * 20], *bufp; 2230 int error; 2231 struct dvd_layer *layer; 2232 int i; 2233 2234 memset(cmd.bytes, 0, 15); 2235 memset(bf, 0, sizeof(bf)); 2236 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2237 cmd.bytes[6] = s->type; 2238 _lto2b(sizeof(bf), &cmd.bytes[7]); 2239 2240 cmd.bytes[5] = s->physical.layer_num; 2241 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2242 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2243 if (error) 2244 return (error); 2245 for (i = 0, bufp = &bf[4], layer = &s->physical.layer[0]; i < 4; 2246 i++, bufp += 20, layer++) { 2247 memset(layer, 0, sizeof(*layer)); 2248 layer->book_version = bufp[0] & 0xf; 2249 layer->book_type = bufp[0] >> 4; 2250 layer->min_rate = bufp[1] & 0xf; 2251 layer->disc_size = bufp[1] >> 4; 2252 layer->layer_type = bufp[2] & 0xf; 2253 layer->track_path = (bufp[2] >> 4) & 1; 2254 layer->nlayers = (bufp[2] >> 5) & 3; 2255 layer->track_density = bufp[3] & 0xf; 2256 layer->linear_density = bufp[3] >> 4; 2257 layer->start_sector = _4btol(&bufp[4]); 2258 layer->end_sector = _4btol(&bufp[8]); 2259 layer->end_sector_l0 = _4btol(&bufp[12]); 2260 layer->bca = bufp[16] >> 7; 2261 } 2262 return (0); 2263 } 2264 2265 static int 2266 dvd_read_copyright(struct cd_softc *cd, dvd_struct *s) 2267 { 2268 struct scsipi_generic cmd; 2269 u_int8_t bf[8]; 2270 int error; 2271 2272 memset(cmd.bytes, 0, 15); 2273 memset(bf, 0, sizeof(bf)); 2274 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2275 cmd.bytes[6] = s->type; 2276 _lto2b(sizeof(bf), &cmd.bytes[7]); 2277 2278 cmd.bytes[5] = s->copyright.layer_num; 2279 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2280 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2281 if (error) 2282 return (error); 2283 s->copyright.cpst = bf[4]; 2284 s->copyright.rmi = bf[5]; 2285 return (0); 2286 } 2287 2288 static int 2289 dvd_read_disckey(struct cd_softc *cd, dvd_struct *s) 2290 { 2291 struct scsipi_generic cmd; 2292 u_int8_t *bf; 2293 int error; 2294 2295 bf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2296 if (bf == NULL) 2297 return EIO; 2298 memset(cmd.bytes, 0, 15); 2299 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2300 cmd.bytes[6] = s->type; 2301 _lto2b(4 + 2048, &cmd.bytes[7]); 2302 2303 cmd.bytes[9] = s->disckey.agid << 6; 2304 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 4 + 2048, 2305 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2306 if (error == 0) 2307 memcpy(s->disckey.value, &bf[4], 2048); 2308 free(bf, M_TEMP); 2309 return error; 2310 } 2311 2312 static int 2313 dvd_read_bca(struct cd_softc *cd, dvd_struct *s) 2314 { 2315 struct scsipi_generic cmd; 2316 u_int8_t bf[4 + 188]; 2317 int error; 2318 2319 memset(cmd.bytes, 0, 15); 2320 memset(bf, 0, sizeof(bf)); 2321 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2322 cmd.bytes[6] = s->type; 2323 _lto2b(sizeof(bf), &cmd.bytes[7]); 2324 2325 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2326 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2327 if (error) 2328 return (error); 2329 s->bca.len = _2btol(&bf[0]); 2330 if (s->bca.len < 12 || s->bca.len > 188) 2331 return (EIO); 2332 memcpy(s->bca.value, &bf[4], s->bca.len); 2333 return (0); 2334 } 2335 2336 static int 2337 dvd_read_manufact(struct cd_softc *cd, dvd_struct *s) 2338 { 2339 struct scsipi_generic cmd; 2340 u_int8_t *bf; 2341 int error; 2342 2343 bf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2344 if (bf == NULL) 2345 return (EIO); 2346 memset(cmd.bytes, 0, 15); 2347 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2348 cmd.bytes[6] = s->type; 2349 _lto2b(4 + 2048, &cmd.bytes[7]); 2350 2351 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 4 + 2048, 2352 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN|XS_CTL_DATA_ONSTACK); 2353 if (error == 0) { 2354 s->manufact.len = _2btol(&bf[0]); 2355 if (s->manufact.len >= 0 && s->manufact.len <= 2048) 2356 memcpy(s->manufact.value, &bf[4], s->manufact.len); 2357 else 2358 error = EIO; 2359 } 2360 free(bf, M_TEMP); 2361 return error; 2362 } 2363 2364 static int 2365 dvd_read_struct(struct cd_softc *cd, dvd_struct *s) 2366 { 2367 2368 switch (s->type) { 2369 case DVD_STRUCT_PHYSICAL: 2370 return (dvd_read_physical(cd, s)); 2371 case DVD_STRUCT_COPYRIGHT: 2372 return (dvd_read_copyright(cd, s)); 2373 case DVD_STRUCT_DISCKEY: 2374 return (dvd_read_disckey(cd, s)); 2375 case DVD_STRUCT_BCA: 2376 return (dvd_read_bca(cd, s)); 2377 case DVD_STRUCT_MANUFACT: 2378 return (dvd_read_manufact(cd, s)); 2379 default: 2380 return (EINVAL); 2381 } 2382 } 2383 2384 static int 2385 cd_mode_sense(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size, 2386 int page, int flags, int *big) 2387 { 2388 2389 if (cd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) { 2390 *big = 1; 2391 return scsipi_mode_sense_big(cd->sc_periph, byte2, page, sense, 2392 size + sizeof(struct scsi_mode_parameter_header_10), 2393 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2394 } else { 2395 *big = 0; 2396 return scsipi_mode_sense(cd->sc_periph, byte2, page, sense, 2397 size + sizeof(struct scsi_mode_parameter_header_6), 2398 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2399 } 2400 } 2401 2402 static int 2403 cd_mode_select(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size, 2404 int flags, int big) 2405 { 2406 2407 if (big) { 2408 struct scsi_mode_parameter_header_10 *header = sense; 2409 2410 _lto2b(0, header->data_length); 2411 return scsipi_mode_select_big(cd->sc_periph, byte2, sense, 2412 size + sizeof(struct scsi_mode_parameter_header_10), 2413 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2414 } else { 2415 struct scsi_mode_parameter_header_6 *header = sense; 2416 2417 header->data_length = 0; 2418 return scsipi_mode_select(cd->sc_periph, byte2, sense, 2419 size + sizeof(struct scsi_mode_parameter_header_6), 2420 flags | XS_CTL_DATA_ONSTACK, CDRETRIES, 20000); 2421 } 2422 } 2423 2424 static int 2425 cd_set_pa_immed(struct cd_softc *cd, int flags) 2426 { 2427 struct { 2428 union { 2429 struct scsi_mode_parameter_header_6 small; 2430 struct scsi_mode_parameter_header_10 big; 2431 } header; 2432 struct cd_audio_page page; 2433 } data; 2434 int error; 2435 uint8_t oflags; 2436 int big, byte2; 2437 struct cd_audio_page *page; 2438 2439 byte2 = SMS_DBD; 2440 try_again: 2441 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2442 AUDIO_PAGE, flags, &big)) != 0) { 2443 if (byte2 == SMS_DBD) { 2444 /* Device may not understand DBD; retry without */ 2445 byte2 = 0; 2446 goto try_again; 2447 } 2448 return (error); 2449 } 2450 2451 if (big) 2452 page = (void *)((u_long)&data.header.big + 2453 sizeof data.header.big + 2454 _2btol(data.header.big.blk_desc_len)); 2455 else 2456 page = (void *)((u_long)&data.header.small + 2457 sizeof data.header.small + 2458 data.header.small.blk_desc_len); 2459 2460 oflags = page->flags; 2461 page->flags &= ~CD_PA_SOTC; 2462 page->flags |= CD_PA_IMMED; 2463 if (oflags == page->flags) 2464 return (0); 2465 2466 return (cd_mode_select(cd, SMS_PF, &data, 2467 sizeof(struct scsi_mode_page_header) + page->pg_length, 2468 flags, big)); 2469 } 2470 2471 static int 2472 cd_setchan(struct cd_softc *cd, int p0, int p1, int p2, int p3, int flags) 2473 { 2474 struct { 2475 union { 2476 struct scsi_mode_parameter_header_6 small; 2477 struct scsi_mode_parameter_header_10 big; 2478 } header; 2479 struct cd_audio_page page; 2480 } data; 2481 int error; 2482 int big, byte2; 2483 struct cd_audio_page *page; 2484 2485 byte2 = SMS_DBD; 2486 try_again: 2487 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2488 AUDIO_PAGE, flags, &big)) != 0) { 2489 if (byte2 == SMS_DBD) { 2490 /* Device may not understand DBD; retry without */ 2491 byte2 = 0; 2492 goto try_again; 2493 } 2494 return (error); 2495 } 2496 2497 if (big) 2498 page = (void *)((u_long)&data.header.big + 2499 sizeof data.header.big + 2500 _2btol(data.header.big.blk_desc_len)); 2501 else 2502 page = (void *)((u_long)&data.header.small + 2503 sizeof data.header.small + 2504 data.header.small.blk_desc_len); 2505 2506 page->port[0].channels = p0; 2507 page->port[1].channels = p1; 2508 page->port[2].channels = p2; 2509 page->port[3].channels = p3; 2510 2511 return (cd_mode_select(cd, SMS_PF, &data, 2512 sizeof(struct scsi_mode_page_header) + page->pg_length, 2513 flags, big)); 2514 } 2515 2516 static int 2517 cd_getvol(struct cd_softc *cd, struct ioc_vol *arg, int flags) 2518 { 2519 struct { 2520 union { 2521 struct scsi_mode_parameter_header_6 small; 2522 struct scsi_mode_parameter_header_10 big; 2523 } header; 2524 struct cd_audio_page page; 2525 } data; 2526 int error; 2527 int big, byte2; 2528 struct cd_audio_page *page; 2529 2530 byte2 = SMS_DBD; 2531 try_again: 2532 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2533 AUDIO_PAGE, flags, &big)) != 0) { 2534 if (byte2 == SMS_DBD) { 2535 /* Device may not understand DBD; retry without */ 2536 byte2 = 0; 2537 goto try_again; 2538 } 2539 return (error); 2540 } 2541 2542 if (big) 2543 page = (void *)((u_long)&data.header.big + 2544 sizeof data.header.big + 2545 _2btol(data.header.big.blk_desc_len)); 2546 else 2547 page = (void *)((u_long)&data.header.small + 2548 sizeof data.header.small + 2549 data.header.small.blk_desc_len); 2550 2551 arg->vol[0] = page->port[0].volume; 2552 arg->vol[1] = page->port[1].volume; 2553 arg->vol[2] = page->port[2].volume; 2554 arg->vol[3] = page->port[3].volume; 2555 2556 return (0); 2557 } 2558 2559 static int 2560 cd_setvol(struct cd_softc *cd, const struct ioc_vol *arg, int flags) 2561 { 2562 struct { 2563 union { 2564 struct scsi_mode_parameter_header_6 small; 2565 struct scsi_mode_parameter_header_10 big; 2566 } header; 2567 struct cd_audio_page page; 2568 } data, mask; 2569 int error; 2570 int big, byte2; 2571 struct cd_audio_page *page, *page2; 2572 2573 byte2 = SMS_DBD; 2574 try_again: 2575 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2576 AUDIO_PAGE, flags, &big)) != 0) { 2577 if (byte2 == SMS_DBD) { 2578 /* Device may not understand DBD; retry without */ 2579 byte2 = 0; 2580 goto try_again; 2581 } 2582 return (error); 2583 } 2584 if ((error = cd_mode_sense(cd, byte2, &mask, sizeof(mask.page), 2585 AUDIO_PAGE|SMS_PCTRL_CHANGEABLE, flags, &big)) != 0) 2586 return (error); 2587 2588 if (big) { 2589 page = (void *)((u_long)&data.header.big + 2590 sizeof data.header.big + 2591 _2btol(data.header.big.blk_desc_len)); 2592 page2 = (void *)((u_long)&mask.header.big + 2593 sizeof mask.header.big + 2594 _2btol(mask.header.big.blk_desc_len)); 2595 } else { 2596 page = (void *)((u_long)&data.header.small + 2597 sizeof data.header.small + 2598 data.header.small.blk_desc_len); 2599 page2 = (void *)((u_long)&mask.header.small + 2600 sizeof mask.header.small + 2601 mask.header.small.blk_desc_len); 2602 } 2603 2604 page->port[0].volume = arg->vol[0] & page2->port[0].volume; 2605 page->port[1].volume = arg->vol[1] & page2->port[1].volume; 2606 page->port[2].volume = arg->vol[2] & page2->port[2].volume; 2607 page->port[3].volume = arg->vol[3] & page2->port[3].volume; 2608 2609 page->port[0].channels = CHANNEL_0; 2610 page->port[1].channels = CHANNEL_1; 2611 2612 return (cd_mode_select(cd, SMS_PF, &data, 2613 sizeof(struct scsi_mode_page_header) + page->pg_length, 2614 flags, big)); 2615 } 2616 2617 static int 2618 cd_load_unload(struct cd_softc *cd, struct ioc_load_unload *args) 2619 { 2620 struct scsipi_load_unload cmd; 2621 2622 memset(&cmd, 0, sizeof(cmd)); 2623 cmd.opcode = LOAD_UNLOAD; 2624 cmd.options = args->options; /* ioctl uses MMC values */ 2625 cmd.slot = args->slot; 2626 2627 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 2628 CDRETRIES, 200000, NULL, 0)); 2629 } 2630 2631 static int 2632 cd_setblksize(struct cd_softc *cd) 2633 { 2634 struct { 2635 union { 2636 struct scsi_mode_parameter_header_6 small; 2637 struct scsi_mode_parameter_header_10 big; 2638 } header; 2639 struct scsi_general_block_descriptor blk_desc; 2640 } data; 2641 int error; 2642 int big, bsize; 2643 struct scsi_general_block_descriptor *bdesc; 2644 2645 if ((error = cd_mode_sense(cd, 0, &data, sizeof(data.blk_desc), 0, 0, 2646 &big)) != 0) 2647 return (error); 2648 2649 if (big) { 2650 bdesc = (void *)(&data.header.big + 1); 2651 bsize = _2btol(data.header.big.blk_desc_len); 2652 } else { 2653 bdesc = (void *)(&data.header.small + 1); 2654 bsize = data.header.small.blk_desc_len; 2655 } 2656 2657 if (bsize == 0) { 2658 printf("cd_setblksize: trying to change bsize, but no blk_desc\n"); 2659 return (EINVAL); 2660 } 2661 if (_3btol(bdesc->blklen) == 2048) { 2662 printf("cd_setblksize: trying to change bsize, but blk_desc is correct\n"); 2663 return (EINVAL); 2664 } 2665 2666 _lto3b(2048, bdesc->blklen); 2667 2668 return (cd_mode_select(cd, SMS_PF, &data, sizeof(data.blk_desc), 0, 2669 big)); 2670 } 2671 2672 2673 static int 2674 mmc_profile2class(uint16_t mmc_profile) 2675 { 2676 switch (mmc_profile) { 2677 case 0x01 : /* SCSI discs */ 2678 case 0x02 : 2679 /* this can't happen really, cd.c wouldn't have matched */ 2680 return MMC_CLASS_DISC; 2681 case 0x03 : /* Magneto Optical with sector erase */ 2682 case 0x04 : /* Magneto Optical write once */ 2683 case 0x05 : /* Advance Storage Magneto Optical */ 2684 return MMC_CLASS_MO; 2685 case 0x00 : /* Unknown MMC profile, can also be CD-ROM */ 2686 case 0x08 : /* CD-ROM */ 2687 case 0x09 : /* CD-R */ 2688 case 0x0a : /* CD-RW */ 2689 return MMC_CLASS_CD; 2690 case 0x10 : /* DVD-ROM */ 2691 case 0x11 : /* DVD-R */ 2692 case 0x12 : /* DVD-RAM */ 2693 case 0x13 : /* DVD-RW restricted overwrite */ 2694 case 0x14 : /* DVD-RW sequential */ 2695 case 0x1a : /* DVD+RW */ 2696 case 0x1b : /* DVD+R */ 2697 case 0x2a : /* DVD+RW Dual layer */ 2698 case 0x2b : /* DVD+R Dual layer */ 2699 case 0x50 : /* HD DVD-ROM */ 2700 case 0x51 : /* HD DVD-R */ 2701 case 0x52 : /* HD DVD-RW; DVD-RAM like */ 2702 return MMC_CLASS_DVD; 2703 case 0x40 : /* BD-ROM */ 2704 case 0x41 : /* BD-R Sequential recording (SRM) */ 2705 case 0x42 : /* BD-R Ramdom Recording (RRM) */ 2706 case 0x43 : /* BD-RE */ 2707 return MMC_CLASS_BD; 2708 } 2709 return MMC_CLASS_UNKN; 2710 } 2711 2712 2713 /* 2714 * Drive/media combination is reflected in a series of features that can 2715 * either be current or dormant. We try to make sense out of them to create a 2716 * set of easy to use flags that abstract the device/media capabilities. 2717 */ 2718 2719 static void 2720 mmc_process_feature(struct mmc_discinfo *mmc_discinfo, 2721 uint16_t feature, int cur, uint8_t *rpos) 2722 { 2723 uint32_t blockingnr; 2724 uint64_t flags; 2725 2726 if (cur == 1) { 2727 flags = mmc_discinfo->mmc_cur; 2728 } else { 2729 flags = mmc_discinfo->mmc_cap; 2730 } 2731 2732 switch (feature) { 2733 case 0x0010 : /* random readable feature */ 2734 blockingnr = rpos[5] | (rpos[4] << 8); 2735 if (blockingnr > 1) 2736 flags |= MMC_CAP_PACKET; 2737 2738 /* RW error page */ 2739 break; 2740 case 0x0020 : /* random writable feature */ 2741 flags |= MMC_CAP_RECORDABLE; 2742 flags |= MMC_CAP_REWRITABLE; 2743 blockingnr = rpos[9] | (rpos[8] << 8); 2744 if (blockingnr > 1) 2745 flags |= MMC_CAP_PACKET; 2746 break; 2747 case 0x0021 : /* incremental streaming write feature */ 2748 flags |= MMC_CAP_RECORDABLE; 2749 flags |= MMC_CAP_SEQUENTIAL; 2750 if (cur) 2751 mmc_discinfo->link_block_penalty = rpos[4]; 2752 if (rpos[2] & 1) 2753 flags |= MMC_CAP_ZEROLINKBLK; 2754 break; 2755 case 0x0022 : /* (obsolete) erase support feature */ 2756 flags |= MMC_CAP_RECORDABLE; 2757 flags |= MMC_CAP_ERASABLE; 2758 break; 2759 case 0x0023 : /* formatting media support feature */ 2760 flags |= MMC_CAP_RECORDABLE; 2761 flags |= MMC_CAP_FORMATTABLE; 2762 break; 2763 case 0x0024 : /* hardware assised defect management feature */ 2764 flags |= MMC_CAP_HW_DEFECTFREE; 2765 break; 2766 case 0x0025 : /* write once */ 2767 flags |= MMC_CAP_RECORDABLE; 2768 break; 2769 case 0x0026 : /* restricted overwrite feature */ 2770 flags |= MMC_CAP_RECORDABLE; 2771 flags |= MMC_CAP_REWRITABLE; 2772 flags |= MMC_CAP_STRICTOVERWRITE; 2773 break; 2774 case 0x0028 : /* MRW formatted media support feature */ 2775 flags |= MMC_CAP_MRW; 2776 break; 2777 case 0x002b : /* DVD+R read (and opt. write) support */ 2778 flags |= MMC_CAP_SEQUENTIAL; 2779 if (rpos[0] & 1) /* write support */ 2780 flags |= MMC_CAP_RECORDABLE; 2781 break; 2782 case 0x002c : /* rigid restricted overwrite feature */ 2783 flags |= MMC_CAP_RECORDABLE; 2784 flags |= MMC_CAP_REWRITABLE; 2785 flags |= MMC_CAP_STRICTOVERWRITE; 2786 if (rpos[0] & 1) /* blank bit */ 2787 flags |= MMC_CAP_BLANKABLE; 2788 break; 2789 case 0x002d : /* track at once recording feature */ 2790 flags |= MMC_CAP_RECORDABLE; 2791 flags |= MMC_CAP_SEQUENTIAL; 2792 break; 2793 case 0x002f : /* DVD-R/-RW write feature */ 2794 flags |= MMC_CAP_RECORDABLE; 2795 if (rpos[0] & 2) /* DVD-RW bit */ 2796 flags |= MMC_CAP_BLANKABLE; 2797 break; 2798 case 0x0038 : /* BD-R SRM with pseudo overwrite */ 2799 flags |= MMC_CAP_PSEUDOOVERWRITE; 2800 break; 2801 default : 2802 /* ignore */ 2803 break; 2804 } 2805 2806 if (cur == 1) { 2807 mmc_discinfo->mmc_cur = flags; 2808 } else { 2809 mmc_discinfo->mmc_cap = flags; 2810 } 2811 } 2812 2813 static int 2814 mmc_getdiscinfo_cdrom(struct scsipi_periph *periph, 2815 struct mmc_discinfo *mmc_discinfo) 2816 { 2817 struct scsipi_read_toc gtoc_cmd; 2818 struct scsipi_toc_header *toc_hdr; 2819 struct scsipi_toc_msinfo *toc_msinfo; 2820 const uint32_t buffer_size = 1024; 2821 uint32_t req_size; 2822 uint8_t *buffer; 2823 int error, flags; 2824 2825 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 2826 /* 2827 * Fabricate mmc_discinfo for CD-ROM. Some values are really `dont 2828 * care' but others might be of interest to programs. 2829 */ 2830 2831 mmc_discinfo->disc_state = MMC_STATE_FULL; 2832 mmc_discinfo->last_session_state = MMC_STATE_FULL; 2833 mmc_discinfo->bg_format_state = MMC_BGFSTATE_COMPLETED; 2834 mmc_discinfo->link_block_penalty = 7; /* not relevant */ 2835 2836 /* get number of sessions and first tracknr in last session */ 2837 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 2838 bzero(>oc_cmd, sizeof(gtoc_cmd)); 2839 gtoc_cmd.opcode = READ_TOC; 2840 gtoc_cmd.addr_mode = CD_MSF; /* not relevant */ 2841 gtoc_cmd.resp_format = CD_TOC_MSINFO; /* multisession info */ 2842 gtoc_cmd.from_track = 0; /* reserved, must be 0 */ 2843 req_size = sizeof(*toc_hdr) + sizeof(*toc_msinfo); 2844 _lto2b(req_size, gtoc_cmd.data_len); 2845 2846 error = scsipi_command(periph, 2847 (void *)>oc_cmd, sizeof(gtoc_cmd), 2848 (void *)buffer, req_size, 2849 CDRETRIES, 30000, NULL, flags); 2850 if (error) 2851 goto out; 2852 toc_hdr = (struct scsipi_toc_header *) buffer; 2853 toc_msinfo = (struct scsipi_toc_msinfo *) (buffer + 4); 2854 mmc_discinfo->num_sessions = toc_hdr->last - toc_hdr->first + 1; 2855 mmc_discinfo->first_track = toc_hdr->first; 2856 mmc_discinfo->first_track_last_session = toc_msinfo->tracknr; 2857 2858 /* get last track of last session */ 2859 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 2860 gtoc_cmd.resp_format = CD_TOC_FORM; /* formatted toc */ 2861 req_size = sizeof(*toc_hdr); 2862 _lto2b(req_size, gtoc_cmd.data_len); 2863 2864 error = scsipi_command(periph, 2865 (void *)>oc_cmd, sizeof(gtoc_cmd), 2866 (void *)buffer, req_size, 2867 CDRETRIES, 30000, NULL, flags); 2868 if (error) 2869 goto out; 2870 toc_hdr = (struct scsipi_toc_header *) buffer; 2871 mmc_discinfo->last_track_last_session = toc_hdr->last; 2872 mmc_discinfo->num_tracks = toc_hdr->last - toc_hdr->first + 1; 2873 2874 /* TODO how to handle disc_barcode and disc_id */ 2875 /* done */ 2876 2877 out: 2878 free(buffer, M_TEMP); 2879 return error; 2880 } 2881 2882 static int 2883 mmc_getdiscinfo_dvdrom(struct scsipi_periph *periph, 2884 struct mmc_discinfo *mmc_discinfo) 2885 { 2886 struct scsipi_read_toc gtoc_cmd; 2887 struct scsipi_toc_header toc_hdr; 2888 uint32_t req_size; 2889 int error, flags; 2890 2891 /* 2892 * Fabricate mmc_discinfo for DVD-ROM. Some values are really `dont 2893 * care' but others might be of interest to programs. 2894 */ 2895 2896 mmc_discinfo->disc_state = MMC_STATE_FULL; 2897 mmc_discinfo->last_session_state = MMC_STATE_FULL; 2898 mmc_discinfo->bg_format_state = MMC_BGFSTATE_COMPLETED; 2899 mmc_discinfo->link_block_penalty = 16; /* not relevant */ 2900 2901 /* get number of sessions and first tracknr in last session */ 2902 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 2903 bzero(>oc_cmd, sizeof(gtoc_cmd)); 2904 gtoc_cmd.opcode = READ_TOC; 2905 gtoc_cmd.addr_mode = 0; /* LBA */ 2906 gtoc_cmd.resp_format = CD_TOC_FORM; /* multisession info */ 2907 gtoc_cmd.from_track = 1; /* first track */ 2908 req_size = sizeof(toc_hdr); 2909 _lto2b(req_size, gtoc_cmd.data_len); 2910 2911 error = scsipi_command(periph, 2912 (void *)>oc_cmd, sizeof(gtoc_cmd), 2913 (void *)&toc_hdr, req_size, 2914 CDRETRIES, 30000, NULL, flags); 2915 if (error) 2916 return error; 2917 2918 /* DVD-ROM squashes the track/session space */ 2919 mmc_discinfo->num_sessions = toc_hdr.last - toc_hdr.first + 1; 2920 mmc_discinfo->num_tracks = mmc_discinfo->num_sessions; 2921 mmc_discinfo->first_track = toc_hdr.first; 2922 mmc_discinfo->first_track_last_session = toc_hdr.last; 2923 mmc_discinfo->last_track_last_session = toc_hdr.last; 2924 2925 /* TODO how to handle disc_barcode and disc_id */ 2926 /* done */ 2927 return 0; 2928 } 2929 2930 static int 2931 mmc_getdiscinfo(struct scsipi_periph *periph, 2932 struct mmc_discinfo *mmc_discinfo) 2933 { 2934 struct scsipi_get_configuration gc_cmd; 2935 struct scsipi_get_conf_data *gc; 2936 struct scsipi_get_conf_feature *gcf; 2937 struct scsipi_read_discinfo di_cmd; 2938 struct scsipi_read_discinfo_data di; 2939 const uint32_t buffer_size = 1024; 2940 uint32_t feat_tbl_len, pos; 2941 u_long last_lba; 2942 uint8_t *buffer, *fpos; 2943 int feature, last_feature, features_len, feature_cur, feature_len; 2944 int lsb, msb, error, flags; 2945 2946 feat_tbl_len = buffer_size; 2947 2948 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 2949 2950 /* initialise structure */ 2951 memset(mmc_discinfo, 0, sizeof(struct mmc_discinfo)); 2952 mmc_discinfo->mmc_profile = 0x00; /* unknown */ 2953 mmc_discinfo->mmc_class = MMC_CLASS_UNKN; 2954 mmc_discinfo->mmc_cur = 0; 2955 mmc_discinfo->mmc_cap = 0; 2956 mmc_discinfo->link_block_penalty = 0; 2957 2958 /* determine mmc profile and class */ 2959 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 2960 memset(&gc_cmd, 0, sizeof(gc_cmd)); 2961 gc_cmd.opcode = GET_CONFIGURATION; 2962 _lto2b(GET_CONF_NO_FEATURES_LEN, gc_cmd.data_len); 2963 2964 gc = (struct scsipi_get_conf_data *) buffer; 2965 2966 error = scsipi_command(periph, 2967 (void *)&gc_cmd, sizeof(gc_cmd), 2968 (void *) gc, GET_CONF_NO_FEATURES_LEN, 2969 CDRETRIES, 30000, NULL, flags); 2970 if (error) 2971 goto out; 2972 2973 mmc_discinfo->mmc_profile = _2btol(gc->mmc_profile); 2974 mmc_discinfo->mmc_class = mmc_profile2class(mmc_discinfo->mmc_profile); 2975 2976 /* assume 2048 sector size unless told otherwise */ 2977 mmc_discinfo->sector_size = 2048; 2978 error = read_cd_capacity(periph, &mmc_discinfo->sector_size, &last_lba); 2979 if (error) 2980 goto out; 2981 2982 mmc_discinfo->last_possible_lba = (uint32_t) last_lba - 1; 2983 2984 /* Read in all features to determine device capabilities */ 2985 last_feature = feature = 0; 2986 do { 2987 /* determine mmc profile and class */ 2988 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 2989 memset(&gc_cmd, 0, sizeof(gc_cmd)); 2990 gc_cmd.opcode = GET_CONFIGURATION; 2991 _lto2b(last_feature, gc_cmd.start_at_feature); 2992 _lto2b(feat_tbl_len, gc_cmd.data_len); 2993 2994 error = scsipi_command(periph, 2995 (void *)&gc_cmd, sizeof(gc_cmd), 2996 (void *) gc, feat_tbl_len, 2997 CDRETRIES, 30000, NULL, flags); 2998 if (error) { 2999 /* ieeek... break out of loop... i dunno what to do */ 3000 break; 3001 } 3002 3003 features_len = _4btol(gc->data_len); 3004 3005 pos = 0; 3006 fpos = &gc->feature_desc[0]; 3007 while (pos < features_len - 4) { 3008 gcf = (struct scsipi_get_conf_feature *) fpos; 3009 3010 feature = _2btol(gcf->featurecode); 3011 feature_cur = gcf->flags & 1; 3012 feature_len = gcf->additional_length; 3013 3014 mmc_process_feature(mmc_discinfo, 3015 feature, feature_cur, 3016 gcf->feature_dependent); 3017 3018 last_feature = MAX(last_feature, feature); 3019 #ifdef DIAGNOSTIC 3020 /* assert((feature_len & 3) == 0); */ 3021 if ((feature_len & 3) != 0) { 3022 printf("feature %d having length %d\n", 3023 feature, feature_len); 3024 } 3025 #endif 3026 3027 pos += 4 + feature_len; 3028 fpos += 4 + feature_len; 3029 } 3030 /* unlikely to ever grow past our 1kb buffer */ 3031 } while (features_len >= 0xffff); 3032 3033 #ifdef DEBUG 3034 printf("CD mmc %d, mmc_cur 0x%"PRIx64", mmc_cap 0x%"PRIx64"\n", 3035 mmc_discinfo->mmc_profile, 3036 mmc_discinfo->mmc_cur, mmc_discinfo->mmc_cap); 3037 #endif 3038 3039 /* read in disc state and number of sessions and tracks */ 3040 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT; 3041 memset(&di_cmd, 0, sizeof(di_cmd)); 3042 di_cmd.opcode = READ_DISCINFO; 3043 di_cmd.data_len[1] = READ_DISCINFO_BIGSIZE; 3044 3045 error = scsipi_command(periph, 3046 (void *)&di_cmd, sizeof(di_cmd), 3047 (void *)&di, READ_DISCINFO_BIGSIZE, 3048 CDRETRIES, 30000, NULL, flags); 3049 3050 if (error) { 3051 /* discinfo call failed, emulate for cd-rom/dvd-rom */ 3052 if (mmc_discinfo->mmc_profile == 0x08) /* CD-ROM */ 3053 return mmc_getdiscinfo_cdrom(periph, mmc_discinfo); 3054 if (mmc_discinfo->mmc_profile == 0x10) /* DVD-ROM */ 3055 return mmc_getdiscinfo_dvdrom(periph, mmc_discinfo); 3056 /* CD/DVD drive is violating specs */ 3057 error = EIO; 3058 goto out; 3059 } 3060 3061 /* call went OK */ 3062 mmc_discinfo->disc_state = di.disc_state & 3; 3063 mmc_discinfo->last_session_state = (di.disc_state >> 2) & 3; 3064 mmc_discinfo->bg_format_state = (di.disc_state2 & 3); 3065 3066 lsb = di.num_sessions_lsb; 3067 msb = di.num_sessions_msb; 3068 mmc_discinfo->num_sessions = lsb | (msb << 8); 3069 3070 mmc_discinfo->first_track = di.first_track; 3071 lsb = di.first_track_last_session_lsb; 3072 msb = di.first_track_last_session_msb; 3073 mmc_discinfo->first_track_last_session = lsb | (msb << 8); 3074 lsb = di.last_track_last_session_lsb; 3075 msb = di.last_track_last_session_msb; 3076 mmc_discinfo->last_track_last_session = lsb | (msb << 8); 3077 3078 mmc_discinfo->num_tracks = mmc_discinfo->last_track_last_session - 3079 mmc_discinfo->first_track + 1; 3080 3081 /* set misc. flags and parameters from this disc info */ 3082 if (di.disc_state & 16) 3083 mmc_discinfo->mmc_cur |= MMC_CAP_BLANKABLE; 3084 3085 if (di.disc_state2 & 128) { 3086 mmc_discinfo->disc_id = _4btol(di.discid); 3087 mmc_discinfo->disc_flags |= MMC_DFLAGS_DISCIDVALID; 3088 } 3089 if (di.disc_state2 & 64) { 3090 mmc_discinfo->disc_barcode = _8btol(di.disc_bar_code); 3091 mmc_discinfo->disc_flags |= MMC_DFLAGS_BARCODEVALID; 3092 } 3093 if (di.disc_state2 & 32) 3094 mmc_discinfo->disc_flags |= MMC_DFLAGS_UNRESTRICTED; 3095 3096 if (di.disc_state2 & 16) { 3097 mmc_discinfo->application_code = di.application_code; 3098 mmc_discinfo->disc_flags |= MMC_DFLAGS_APPCODEVALID; 3099 } 3100 3101 /* done */ 3102 3103 out: 3104 free(buffer, M_TEMP); 3105 return error; 3106 } 3107 3108 static int 3109 mmc_gettrackinfo_cdrom(struct scsipi_periph *periph, 3110 struct mmc_trackinfo *trackinfo) 3111 { 3112 struct scsipi_read_toc gtoc_cmd; 3113 struct scsipi_toc_header *toc_hdr; 3114 struct scsipi_toc_rawtoc *rawtoc; 3115 uint32_t track_start, track_end, track_size; 3116 uint32_t last_recorded, next_writable; 3117 uint32_t lba, next_track_start, lead_out; 3118 const uint32_t buffer_size = 4 * 1024; /* worst case TOC estimate */ 3119 uint8_t *buffer; 3120 uint8_t track_sessionnr, last_tracknr, sessionnr, adr, tno, point; 3121 uint8_t tmin, tsec, tframe, pmin, psec, pframe; 3122 int size, req_size; 3123 int error, flags; 3124 3125 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 3126 3127 /* 3128 * Emulate read trackinfo for CD-ROM using the raw-TOC. 3129 * 3130 * Not all information is present and this presents a problem. Track 3131 * starts are known for each track but other values are deducted. 3132 * 3133 * For a complete overview of `magic' values used here, see the 3134 * SCSI/ATAPI MMC documentation. Note that the `magic' values have no 3135 * names, they are specified as numbers. 3136 */ 3137 3138 /* get raw toc to process, first header to check size */ 3139 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT; 3140 bzero(>oc_cmd, sizeof(gtoc_cmd)); 3141 gtoc_cmd.opcode = READ_TOC; 3142 gtoc_cmd.addr_mode = CD_MSF; /* not relevant */ 3143 gtoc_cmd.resp_format = CD_TOC_RAW; /* raw toc */ 3144 gtoc_cmd.from_track = 1; /* first session */ 3145 req_size = sizeof(*toc_hdr); 3146 _lto2b(req_size, gtoc_cmd.data_len); 3147 3148 error = scsipi_command(periph, 3149 (void *)>oc_cmd, sizeof(gtoc_cmd), 3150 (void *)buffer, req_size, 3151 CDRETRIES, 30000, NULL, flags); 3152 if (error) 3153 goto out; 3154 toc_hdr = (struct scsipi_toc_header *) buffer; 3155 if (_2btol(toc_hdr->length) > buffer_size - 2) { 3156 #ifdef DIAGNOSTIC 3157 printf("increase buffersize in mmc_readtrackinfo_cdrom\n"); 3158 #endif 3159 error = ENOBUFS; 3160 goto out; 3161 } 3162 3163 /* read in complete raw toc */ 3164 req_size = _2btol(toc_hdr->length); 3165 _lto2b(req_size, gtoc_cmd.data_len); 3166 3167 error = scsipi_command(periph, 3168 (void *)>oc_cmd, sizeof(gtoc_cmd), 3169 (void *)buffer, req_size, 3170 CDRETRIES, 30000, NULL, flags); 3171 if (error) 3172 goto out; 3173 3174 toc_hdr = (struct scsipi_toc_header *) buffer; 3175 rawtoc = (struct scsipi_toc_rawtoc *) (buffer + 4); 3176 3177 track_start = 0; 3178 track_end = 0; 3179 track_size = 0; 3180 last_recorded = 0; 3181 next_writable = 0; 3182 flags = 0; 3183 3184 last_tracknr = 1; 3185 next_track_start = 0; 3186 track_sessionnr = MAXTRACK; /* by definition */ 3187 lead_out = 0; 3188 3189 size = req_size - sizeof(struct scsipi_toc_header) + 1; 3190 while (size > 0) { 3191 /* get track start and session end */ 3192 tno = rawtoc->tno; 3193 sessionnr = rawtoc->sessionnr; 3194 adr = rawtoc->adrcontrol >> 4; 3195 point = rawtoc->point; 3196 tmin = rawtoc->min; 3197 tsec = rawtoc->sec; 3198 tframe = rawtoc->frame; 3199 pmin = rawtoc->pmin; 3200 psec = rawtoc->psec; 3201 pframe = rawtoc->pframe; 3202 3203 if (tno == 0 && sessionnr && adr == 1) { 3204 lba = hmsf2lba(0, pmin, psec, pframe); 3205 if (point == trackinfo->tracknr) { 3206 track_start = lba; 3207 track_sessionnr = sessionnr; 3208 } 3209 if (point == trackinfo->tracknr + 1) { 3210 /* estimate size */ 3211 track_size = lba - track_start; 3212 next_track_start = lba; 3213 } 3214 if (point == 0xa2) { 3215 lead_out = lba; 3216 } 3217 if (point <= 0x63) { 3218 /* CD's ok, DVD are glued */ 3219 last_tracknr = point; 3220 } 3221 if (sessionnr == track_sessionnr) { 3222 last_recorded = lead_out; 3223 } 3224 } 3225 if (tno == 0 && sessionnr && adr == 5) { 3226 lba = hmsf2lba(0, tmin, tsec, tframe); 3227 if (sessionnr == track_sessionnr) { 3228 next_writable = lba; 3229 } 3230 } 3231 3232 rawtoc++; 3233 size -= sizeof(struct scsipi_toc_rawtoc); 3234 } 3235 3236 /* process found values; some voodoo */ 3237 /* if no tracksize tracknr is the last of the disc */ 3238 if ((track_size == 0) && last_recorded) { 3239 track_size = last_recorded - track_start; 3240 } 3241 /* if last_recorded < tracksize, tracksize is overestimated */ 3242 if (last_recorded) { 3243 if (last_recorded - track_start <= track_size) { 3244 track_size = last_recorded - track_start; 3245 flags |= MMC_TRACKINFO_LRA_VALID; 3246 } 3247 } 3248 /* check if its a the last track of the sector */ 3249 if (next_writable) { 3250 if (next_track_start > next_writable) 3251 flags |= MMC_TRACKINFO_NWA_VALID; 3252 } 3253 3254 /* no flag set -> no values */ 3255 if ((flags & MMC_TRACKINFO_LRA_VALID) == 0) 3256 last_recorded = 0; 3257 if ((flags & MMC_TRACKINFO_NWA_VALID) == 0) 3258 next_writable = 0; 3259 3260 /* fill in */ 3261 /* trackinfo->tracknr preserved */ 3262 trackinfo->sessionnr = track_sessionnr; 3263 trackinfo->track_mode = 7; /* data, incremental */ 3264 trackinfo->data_mode = 8; /* 2048 bytes mode1 */ 3265 3266 trackinfo->flags = flags; 3267 trackinfo->track_start = track_start; 3268 trackinfo->next_writable = next_writable; 3269 trackinfo->free_blocks = 0; 3270 trackinfo->packet_size = 1; 3271 trackinfo->track_size = track_size; 3272 trackinfo->last_recorded = last_recorded; 3273 3274 out: 3275 free(buffer, M_TEMP); 3276 return error; 3277 3278 } 3279 3280 static int 3281 mmc_gettrackinfo_dvdrom(struct scsipi_periph *periph, 3282 struct mmc_trackinfo *trackinfo) 3283 { 3284 struct scsipi_read_toc gtoc_cmd; 3285 struct scsipi_toc_header *toc_hdr; 3286 struct scsipi_toc_formatted *toc; 3287 uint32_t tracknr, track_start, track_size; 3288 uint32_t lba, lead_out; 3289 const uint32_t buffer_size = 4 * 1024; /* worst case TOC estimate */ 3290 uint8_t *buffer; 3291 uint8_t last_tracknr; 3292 int size, req_size; 3293 int error, flags; 3294 3295 3296 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 3297 /* 3298 * Emulate read trackinfo for DVD-ROM. We can't use the raw-TOC as the 3299 * CD-ROM emulation uses since the specification tells us that no such 3300 * thing is defined for DVD's. The reason for this is due to the large 3301 * number of tracks and that would clash with the `magic' values. This 3302 * suxs. 3303 * 3304 * Not all information is present and this presents a problem. 3305 * Track starts are known for each track but other values are 3306 * deducted. 3307 */ 3308 3309 /* get formatted toc to process, first header to check size */ 3310 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT; 3311 bzero(>oc_cmd, sizeof(gtoc_cmd)); 3312 gtoc_cmd.opcode = READ_TOC; 3313 gtoc_cmd.addr_mode = 0; /* lba's please */ 3314 gtoc_cmd.resp_format = CD_TOC_FORM; /* formatted toc */ 3315 gtoc_cmd.from_track = 1; /* first track */ 3316 req_size = sizeof(*toc_hdr); 3317 _lto2b(req_size, gtoc_cmd.data_len); 3318 3319 error = scsipi_command(periph, 3320 (void *)>oc_cmd, sizeof(gtoc_cmd), 3321 (void *)buffer, req_size, 3322 CDRETRIES, 30000, NULL, flags); 3323 if (error) 3324 goto out; 3325 toc_hdr = (struct scsipi_toc_header *) buffer; 3326 if (_2btol(toc_hdr->length) > buffer_size - 2) { 3327 #ifdef DIAGNOSTIC 3328 printf("incease buffersize in mmc_readtrackinfo_dvdrom\n"); 3329 #endif 3330 error = ENOBUFS; 3331 goto out; 3332 } 3333 3334 /* read in complete formatted toc */ 3335 req_size = _2btol(toc_hdr->length); 3336 _lto2b(req_size, gtoc_cmd.data_len); 3337 3338 error = scsipi_command(periph, 3339 (void *)>oc_cmd, sizeof(gtoc_cmd), 3340 (void *)buffer, req_size, 3341 CDRETRIES, 30000, NULL, flags); 3342 if (error) 3343 goto out; 3344 3345 toc_hdr = (struct scsipi_toc_header *) buffer; 3346 toc = (struct scsipi_toc_formatted *) (buffer + 4); 3347 3348 /* as in read disc info, all sessions are converted to tracks */ 3349 /* track 1.. -> offsets, sizes can be (rougly) estimated (16 ECC) */ 3350 /* last track -> we got the size from the lead-out */ 3351 3352 tracknr = 0; 3353 last_tracknr = toc_hdr->last; 3354 track_start = 0; 3355 track_size = 0; 3356 lead_out = 0; 3357 3358 size = req_size - sizeof(struct scsipi_toc_header) + 1; 3359 while (size > 0) { 3360 /* remember, DVD-ROM: tracknr == sessionnr */ 3361 lba = _4btol(toc->msf_lba); 3362 tracknr = toc->tracknr; 3363 if (trackinfo->tracknr == tracknr) { 3364 track_start = lba; 3365 } 3366 if (trackinfo->tracknr == tracknr+1) { 3367 track_size = lba - track_start; 3368 track_size -= 16; /* link block ? */ 3369 } 3370 if (tracknr == 0xAA) { 3371 lead_out = lba; 3372 } 3373 toc++; 3374 size -= sizeof(struct scsipi_toc_formatted); 3375 } 3376 if (trackinfo->tracknr == last_tracknr) { 3377 track_size = lead_out - track_start; 3378 } 3379 3380 /* fill in */ 3381 /* trackinfo->tracknr preserved */ 3382 trackinfo->sessionnr = trackinfo->tracknr; 3383 trackinfo->track_mode = 0; /* unknown */ 3384 trackinfo->data_mode = 8; /* 2048 bytes mode1 */ 3385 3386 trackinfo->flags = 0; 3387 trackinfo->track_start = track_start; 3388 trackinfo->next_writable = 0; 3389 trackinfo->free_blocks = 0; 3390 trackinfo->packet_size = 16; /* standard length 16 blocks ECC */ 3391 trackinfo->track_size = track_size; 3392 trackinfo->last_recorded = 0; 3393 3394 out: 3395 free(buffer, M_TEMP); 3396 return error; 3397 } 3398 3399 static int 3400 mmc_gettrackinfo(struct scsipi_periph *periph, 3401 struct mmc_trackinfo *trackinfo) 3402 { 3403 struct scsipi_read_trackinfo ti_cmd; 3404 struct scsipi_read_trackinfo_data ti; 3405 struct scsipi_get_configuration gc_cmd; 3406 struct scsipi_get_conf_data gc; 3407 int error, flags; 3408 int mmc_profile; 3409 3410 /* set up SCSI call with track number from trackinfo.tracknr */ 3411 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT; 3412 memset(&ti_cmd, 0, sizeof(ti_cmd)); 3413 ti_cmd.opcode = READ_TRACKINFO; 3414 ti_cmd.addr_type = READ_TRACKINFO_ADDR_TRACK; 3415 ti_cmd.data_len[1] = READ_TRACKINFO_RETURNSIZE; 3416 3417 /* trackinfo.tracknr contains number of tracks to query */ 3418 _lto4b(trackinfo->tracknr, ti_cmd.address); 3419 error = scsipi_command(periph, 3420 (void *)&ti_cmd, sizeof(ti_cmd), 3421 (void *)&ti, READ_TRACKINFO_RETURNSIZE, 3422 CDRETRIES, 30000, NULL, flags); 3423 3424 if (error) { 3425 /* trackinfo call failed, emulate for cd-rom/dvd-rom */ 3426 /* first determine mmc profile */ 3427 flags = XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK; 3428 memset(&gc_cmd, 0, sizeof(gc_cmd)); 3429 gc_cmd.opcode = GET_CONFIGURATION; 3430 _lto2b(GET_CONF_NO_FEATURES_LEN, gc_cmd.data_len); 3431 3432 error = scsipi_command(periph, 3433 (void *)&gc_cmd, sizeof(gc_cmd), 3434 (void *)&gc, GET_CONF_NO_FEATURES_LEN, 3435 CDRETRIES, 30000, NULL, flags); 3436 if (error) 3437 return error; 3438 mmc_profile = _2btol(gc.mmc_profile); 3439 3440 /* choose emulation */ 3441 if (mmc_profile == 0x08) /* CD-ROM */ 3442 return mmc_gettrackinfo_cdrom(periph, trackinfo); 3443 if (mmc_profile == 0x10) /* DVD-ROM */ 3444 return mmc_gettrackinfo_dvdrom(periph, trackinfo); 3445 /* CD/DVD drive is violating specs */ 3446 return EIO; 3447 } 3448 3449 /* (re)initialise structure */ 3450 memset(trackinfo, 0, sizeof(struct mmc_trackinfo)); 3451 3452 /* account for short returns screwing up track and session msb */ 3453 if ((ti.data_len[1] | (ti.data_len[0] << 8)) <= 32) { 3454 ti.track_msb = 0; 3455 ti.session_msb = 0; 3456 } 3457 3458 trackinfo->tracknr = ti.track_lsb | (ti.track_msb << 8); 3459 trackinfo->sessionnr = ti.session_lsb | (ti.session_msb << 8); 3460 trackinfo->track_mode = ti.track_info_1 & 0xf; 3461 trackinfo->data_mode = ti.track_info_2 & 0xf; 3462 3463 flags = 0; 3464 if (ti.track_info_1 & 0x10) 3465 flags |= MMC_TRACKINFO_COPY; 3466 if (ti.track_info_1 & 0x20) 3467 flags |= MMC_TRACKINFO_DAMAGED; 3468 if (ti.track_info_2 & 0x10) 3469 flags |= MMC_TRACKINFO_FIXED_PACKET; 3470 if (ti.track_info_2 & 0x20) 3471 flags |= MMC_TRACKINFO_INCREMENTAL; 3472 if (ti.track_info_2 & 0x40) 3473 flags |= MMC_TRACKINFO_BLANK; 3474 if (ti.track_info_2 & 0x80) 3475 flags |= MMC_TRACKINFO_RESERVED; 3476 if (ti.data_valid & 0x01) 3477 flags |= MMC_TRACKINFO_NWA_VALID; 3478 if (ti.data_valid & 0x02) 3479 flags |= MMC_TRACKINFO_LRA_VALID; 3480 3481 trackinfo->flags = flags; 3482 trackinfo->track_start = _4btol(ti.track_start); 3483 trackinfo->next_writable = _4btol(ti.next_writable); 3484 trackinfo->free_blocks = _4btol(ti.free_blocks); 3485 trackinfo->packet_size = _4btol(ti.packet_size); 3486 trackinfo->track_size = _4btol(ti.track_size); 3487 trackinfo->last_recorded = _4btol(ti.last_recorded); 3488 3489 return 0; 3490 } 3491