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