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