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