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