1 /* $NetBSD: cd.c,v 1.299 2010/03/22 16:49:41 martin 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.299 2010/03/22 16:49:41 martin 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 DIOCTUR: 1274 case SCIOCIDENTIFY: 1275 case OSCIOCIDENTIFY: 1276 case SCIOCCOMMAND: 1277 case SCIOCDEBUG: 1278 case CDIOCGETVOL: 1279 case CDIOCSETVOL: 1280 case CDIOCSETMONO: 1281 case CDIOCSETSTEREO: 1282 case CDIOCSETMUTE: 1283 case CDIOCSETLEFT: 1284 case CDIOCSETRIGHT: 1285 case CDIOCCLOSE: 1286 case CDIOCEJECT: 1287 case CDIOCALLOW: 1288 case CDIOCPREVENT: 1289 case CDIOCSETDEBUG: 1290 case CDIOCCLRDEBUG: 1291 case CDIOCRESET: 1292 case SCIOCRESET: 1293 case CDIOCLOADUNLOAD: 1294 case DVD_AUTH: 1295 case DVD_READ_STRUCT: 1296 case DIOCGSTRATEGY: 1297 case DIOCSSTRATEGY: 1298 if (part == RAW_PART) 1299 break; 1300 /* FALLTHROUGH */ 1301 default: 1302 if ((periph->periph_flags & PERIPH_OPEN) == 0) 1303 return (ENODEV); 1304 else 1305 return (EIO); 1306 } 1307 } 1308 1309 error = disk_ioctl(&cd->sc_dk, cmd, addr, flag, l); 1310 if (error != EPASSTHROUGH) 1311 return (error); 1312 1313 switch (cmd) { 1314 case DIOCGDINFO: 1315 *(struct disklabel *)addr = *(cd->sc_dk.dk_label); 1316 return (0); 1317 #ifdef __HAVE_OLD_DISKLABEL 1318 case ODIOCGDINFO: 1319 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1320 if (newlabel == NULL) 1321 return (EIO); 1322 memcpy(newlabel, cd->sc_dk.dk_label, sizeof (*newlabel)); 1323 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1324 error = ENOTTY; 1325 else 1326 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1327 free(newlabel, M_TEMP); 1328 return error; 1329 #endif 1330 1331 case DIOCGPART: 1332 ((struct partinfo *)addr)->disklab = cd->sc_dk.dk_label; 1333 ((struct partinfo *)addr)->part = 1334 &cd->sc_dk.dk_label->d_partitions[part]; 1335 return (0); 1336 1337 case DIOCWDINFO: 1338 case DIOCSDINFO: 1339 #ifdef __HAVE_OLD_DISKLABEL 1340 case ODIOCWDINFO: 1341 case ODIOCSDINFO: 1342 #endif 1343 { 1344 struct disklabel *lp; 1345 1346 if ((flag & FWRITE) == 0) 1347 return (EBADF); 1348 1349 #ifdef __HAVE_OLD_DISKLABEL 1350 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 1351 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1352 if (newlabel == NULL) 1353 return (EIO); 1354 memset(newlabel, 0, sizeof newlabel); 1355 memcpy(newlabel, addr, sizeof (struct olddisklabel)); 1356 lp = newlabel; 1357 } else 1358 #endif 1359 lp = addr; 1360 1361 mutex_enter(&cd->sc_lock); 1362 cd->flags |= CDF_LABELLING; 1363 1364 error = setdisklabel(cd->sc_dk.dk_label, 1365 lp, /*cd->sc_dk.dk_openmask : */0, 1366 cd->sc_dk.dk_cpulabel); 1367 if (error == 0) { 1368 /* XXX ? */ 1369 } 1370 1371 cd->flags &= ~CDF_LABELLING; 1372 mutex_exit(&cd->sc_lock); 1373 #ifdef __HAVE_OLD_DISKLABEL 1374 if (newlabel != NULL) 1375 free(newlabel, M_TEMP); 1376 #endif 1377 return (error); 1378 } 1379 1380 case DIOCWLABEL: 1381 return (EBADF); 1382 1383 case DIOCGDEFLABEL: 1384 cdgetdefaultlabel(cd, &toc, addr); 1385 return (0); 1386 1387 #ifdef __HAVE_OLD_DISKLABEL 1388 case ODIOCGDEFLABEL: 1389 newlabel = malloc(sizeof (*newlabel), M_TEMP, M_WAITOK); 1390 if (newlabel == NULL) 1391 return (EIO); 1392 cdgetdefaultlabel(cd, &toc, newlabel); 1393 if (newlabel->d_npartitions > OLDMAXPARTITIONS) 1394 error = ENOTTY; 1395 else 1396 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1397 free(newlabel, M_TEMP); 1398 return error; 1399 #endif 1400 1401 case DIOCTUR: { 1402 /* test unit ready */ 1403 error = scsipi_test_unit_ready(cd->sc_periph, XS_CTL_SILENT); 1404 *((int*)addr) = (error == 0); 1405 if (error == ENODEV || error == EIO || error == 0) 1406 return 0; 1407 return error; 1408 } 1409 1410 case CDIOCPLAYTRACKS: { 1411 /* PLAY_MSF command */ 1412 struct ioc_play_track *args = addr; 1413 1414 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1415 return (error); 1416 return (cd_play_tracks(cd, &toc, args->start_track, 1417 args->start_index, args->end_track, args->end_index)); 1418 } 1419 case CDIOCPLAYMSF: { 1420 /* PLAY_MSF command */ 1421 struct ioc_play_msf *args = addr; 1422 1423 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1424 return (error); 1425 return (cd_play_msf(cd, args->start_m, args->start_s, 1426 args->start_f, args->end_m, args->end_s, args->end_f)); 1427 } 1428 case CDIOCPLAYBLOCKS: { 1429 /* PLAY command */ 1430 struct ioc_play_blocks *args = addr; 1431 1432 if ((error = cd_set_pa_immed(cd, 0)) != 0) 1433 return (error); 1434 return (cd_play(cd, args->blk, args->len)); 1435 } 1436 case CDIOCREADSUBCHANNEL: { 1437 /* READ_SUBCHANNEL command */ 1438 struct ioc_read_subchannel *args = addr; 1439 struct cd_sub_channel_info data; 1440 u_int len = args->data_len; 1441 1442 if (len > sizeof(data) || 1443 len < sizeof(struct cd_sub_channel_header)) 1444 return (EINVAL); 1445 error = cd_read_subchannel(cd, args->address_format, 1446 args->data_format, args->track, &data, len, 0); 1447 if (error) 1448 return (error); 1449 len = min(len, _2btol(data.header.data_len) + 1450 sizeof(struct cd_sub_channel_header)); 1451 return (copyout(&data, args->data, len)); 1452 } 1453 case CDIOCREADSUBCHANNEL_BUF: { 1454 /* As CDIOCREADSUBCHANNEL, but without a 2nd buffer area */ 1455 struct ioc_read_subchannel_buf *args = addr; 1456 if (args->req.data_len != sizeof args->info) 1457 return EINVAL; 1458 return cd_read_subchannel(cd, args->req.address_format, 1459 args->req.data_format, args->req.track, &args->info, 1460 sizeof(args->info), 0); 1461 } 1462 case CDIOREADTOCHEADER: { 1463 /* READ TOC format 0 command, static header */ 1464 if ((error = cd_read_toc(cd, CD_TOC_FORM, 0, 0, 1465 &toc, sizeof(toc.header), 0, 0)) != 0) 1466 return (error); 1467 if (cd->sc_periph->periph_quirks & PQUIRK_LITTLETOC) 1468 toc.header.len = le16toh(toc.header.len); 1469 else 1470 toc.header.len = be16toh(toc.header.len); 1471 memcpy(addr, &toc.header, sizeof(toc.header)); 1472 return (0); 1473 } 1474 case CDIOREADTOCENTRYS: { 1475 struct ioc_read_toc_entry *te = addr; 1476 error = do_cdioreadentries(cd, te, &toc); 1477 if (error != 0) 1478 return error; 1479 return copyout(toc.entries, te->data, min(te->data_len, 1480 toc.header.len - (sizeof(toc.header.starting_track) 1481 + sizeof(toc.header.ending_track)))); 1482 } 1483 case CDIOREADTOCENTRIES_BUF: { 1484 struct ioc_read_toc_entry_buf *te = addr; 1485 error = do_cdioreadentries(cd, &te->req, &toc); 1486 if (error != 0) 1487 return error; 1488 memcpy(te->entry, toc.entries, min(te->req.data_len, 1489 toc.header.len - (sizeof(toc.header.starting_track) 1490 + sizeof(toc.header.ending_track)))); 1491 return 0; 1492 } 1493 case CDIOREADMSADDR: { 1494 /* READ TOC format 0 command, length of first track only */ 1495 int sessno = *(int*)addr; 1496 1497 if (sessno != 0) 1498 return (EINVAL); 1499 1500 return (cdreadmsaddr(cd, &toc, addr)); 1501 } 1502 case CDIOCSETPATCH: { 1503 struct ioc_patch *arg = addr; 1504 1505 return (cd_setchan(cd, arg->patch[0], arg->patch[1], 1506 arg->patch[2], arg->patch[3], 0)); 1507 } 1508 case CDIOCGETVOL: { 1509 /* MODE SENSE command (AUDIO page) */ 1510 struct ioc_vol *arg = addr; 1511 1512 return (cd_getvol(cd, arg, 0)); 1513 } 1514 case CDIOCSETVOL: { 1515 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1516 struct ioc_vol *arg = addr; 1517 1518 return (cd_setvol(cd, arg, 0)); 1519 } 1520 case CDIOCSETMONO: 1521 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1522 return (cd_setchan(cd, BOTH_CHANNEL, BOTH_CHANNEL, 1523 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1524 1525 case CDIOCSETSTEREO: 1526 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1527 return (cd_setchan(cd, LEFT_CHANNEL, RIGHT_CHANNEL, 1528 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1529 1530 case CDIOCSETMUTE: 1531 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1532 return (cd_setchan(cd, MUTE_CHANNEL, MUTE_CHANNEL, 1533 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1534 1535 case CDIOCSETLEFT: 1536 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1537 return (cd_setchan(cd, LEFT_CHANNEL, LEFT_CHANNEL, 1538 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1539 1540 case CDIOCSETRIGHT: 1541 /* MODE SENSE/MODE SELECT commands (AUDIO page) */ 1542 return (cd_setchan(cd, RIGHT_CHANNEL, RIGHT_CHANNEL, 1543 MUTE_CHANNEL, MUTE_CHANNEL, 0)); 1544 1545 case CDIOCRESUME: 1546 /* PAUSE command */ 1547 return (cd_pause(cd, PA_RESUME)); 1548 case CDIOCPAUSE: 1549 /* PAUSE command */ 1550 return (cd_pause(cd, PA_PAUSE)); 1551 case CDIOCSTART: 1552 return (scsipi_start(periph, SSS_START, 0)); 1553 case CDIOCSTOP: 1554 return (scsipi_start(periph, SSS_STOP, 0)); 1555 case CDIOCCLOSE: 1556 return (scsipi_start(periph, SSS_START|SSS_LOEJ, 1557 XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE)); 1558 case DIOCEJECT: 1559 if (*(int *)addr == 0) { 1560 /* 1561 * Don't force eject: check that we are the only 1562 * partition open. If so, unlock it. 1563 */ 1564 if ((cd->sc_dk.dk_openmask & ~(1 << part)) == 0 && 1565 cd->sc_dk.dk_bopenmask + cd->sc_dk.dk_copenmask == 1566 cd->sc_dk.dk_openmask) { 1567 error = scsipi_prevent(periph, SPAMR_ALLOW, 1568 XS_CTL_IGNORE_NOT_READY); 1569 if (error) 1570 return (error); 1571 } else { 1572 return (EBUSY); 1573 } 1574 } 1575 /* FALLTHROUGH */ 1576 case CDIOCEJECT: /* FALLTHROUGH */ 1577 case ODIOCEJECT: 1578 return (scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0)); 1579 case DIOCCACHESYNC: 1580 /* SYNCHRONISE CACHES command */ 1581 return (cdcachesync(periph, 0)); 1582 case CDIOCALLOW: 1583 return (scsipi_prevent(periph, SPAMR_ALLOW, 0)); 1584 case CDIOCPREVENT: 1585 return (scsipi_prevent(periph, SPAMR_PREVENT_DT, 0)); 1586 case DIOCLOCK: 1587 return (scsipi_prevent(periph, 1588 (*(int *)addr) ? SPAMR_PREVENT_DT : SPAMR_ALLOW, 0)); 1589 case CDIOCSETDEBUG: 1590 cd->sc_periph->periph_dbflags |= (SCSIPI_DB1 | SCSIPI_DB2); 1591 return (0); 1592 case CDIOCCLRDEBUG: 1593 cd->sc_periph->periph_dbflags &= ~(SCSIPI_DB1 | SCSIPI_DB2); 1594 return (0); 1595 case CDIOCRESET: 1596 case SCIOCRESET: 1597 return (cd_reset(cd)); 1598 case CDIOCLOADUNLOAD: 1599 /* LOAD_UNLOAD command */ 1600 return (cd_load_unload(cd, addr)); 1601 case DVD_AUTH: 1602 /* GPCMD_REPORT_KEY or GPCMD_SEND_KEY command */ 1603 return (dvd_auth(cd, addr)); 1604 case DVD_READ_STRUCT: 1605 /* GPCMD_READ_DVD_STRUCTURE command */ 1606 return (dvd_read_struct(cd, addr)); 1607 case MMCGETDISCINFO: 1608 /* 1609 * GET_CONFIGURATION, READ_DISCINFO, READ_TRACKINFO, 1610 * (READ_TOCf2, READ_CD_CAPACITY and GET_CONFIGURATION) commands 1611 */ 1612 return mmc_getdiscinfo(periph, (struct mmc_discinfo *) addr); 1613 case MMCGETTRACKINFO: 1614 /* READ TOCf2, READ_CD_CAPACITY and READ_TRACKINFO commands */ 1615 return mmc_gettrackinfo(periph, (struct mmc_trackinfo *) addr); 1616 case MMCOP: 1617 /* 1618 * CLOSE TRACK/SESSION, RESERVE_TRACK, REPAIR_TRACK, 1619 * SYNCHRONISE_CACHE commands 1620 */ 1621 return mmc_do_op(periph, (struct mmc_op *) addr); 1622 case MMCSETUPWRITEPARAMS : 1623 /* MODE SENSE page 5, MODE_SELECT page 5 commands */ 1624 return mmc_setup_writeparams(periph, (struct mmc_writeparams *) addr); 1625 case DIOCGSTRATEGY: 1626 { 1627 struct disk_strategy *dks = addr; 1628 1629 s = splbio(); 1630 strlcpy(dks->dks_name, bufq_getstrategyname(cd->buf_queue), 1631 sizeof(dks->dks_name)); 1632 splx(s); 1633 dks->dks_paramlen = 0; 1634 1635 return 0; 1636 } 1637 case DIOCSSTRATEGY: 1638 { 1639 struct disk_strategy *dks = addr; 1640 struct bufq_state *new; 1641 struct bufq_state *old; 1642 1643 if ((flag & FWRITE) == 0) { 1644 return EBADF; 1645 } 1646 if (dks->dks_param != NULL) { 1647 return EINVAL; 1648 } 1649 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */ 1650 error = bufq_alloc(&new, dks->dks_name, 1651 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK); 1652 if (error) { 1653 return error; 1654 } 1655 s = splbio(); 1656 old = cd->buf_queue; 1657 bufq_move(new, old); 1658 cd->buf_queue = new; 1659 splx(s); 1660 bufq_free(old); 1661 1662 return 0; 1663 } 1664 default: 1665 if (part != RAW_PART) 1666 return (ENOTTY); 1667 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l)); 1668 } 1669 1670 #ifdef DIAGNOSTIC 1671 panic("cdioctl: impossible"); 1672 #endif 1673 } 1674 1675 static void 1676 cdgetdefaultlabel(struct cd_softc *cd, struct cd_formatted_toc *toc, 1677 struct disklabel *lp) 1678 { 1679 int lastsession; 1680 1681 memset(lp, 0, sizeof(struct disklabel)); 1682 1683 lp->d_secsize = cd->params.blksize; 1684 lp->d_ntracks = 1; 1685 lp->d_nsectors = 100; 1686 lp->d_ncylinders = (cd->params.disksize / 100) + 1; 1687 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1688 1689 switch (scsipi_periph_bustype(cd->sc_periph)) { 1690 case SCSIPI_BUSTYPE_SCSI: 1691 lp->d_type = DTYPE_SCSI; 1692 break; 1693 case SCSIPI_BUSTYPE_ATAPI: 1694 lp->d_type = DTYPE_ATAPI; 1695 break; 1696 } 1697 /* 1698 * XXX 1699 * We could probe the mode pages to figure out what kind of disc it is. 1700 * Is this worthwhile? 1701 */ 1702 strncpy(lp->d_typename, "optical media", 16); 1703 strncpy(lp->d_packname, "fictitious", 16); 1704 lp->d_secperunit = cd->params.disksize; 1705 lp->d_rpm = 300; 1706 lp->d_interleave = 1; 1707 lp->d_flags = D_REMOVABLE | D_SCSI_MMC; 1708 1709 if (cdreadmsaddr(cd, toc, &lastsession) != 0) 1710 lastsession = 0; 1711 1712 lp->d_partitions[0].p_offset = 0; 1713 lp->d_partitions[0].p_size = lp->d_secperunit; 1714 lp->d_partitions[0].p_cdsession = lastsession; 1715 lp->d_partitions[0].p_fstype = FS_ISO9660; 1716 1717 lp->d_partitions[RAW_PART].p_offset = 0; 1718 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 1719 lp->d_partitions[RAW_PART].p_fstype = FS_UDF; 1720 1721 lp->d_npartitions = RAW_PART + 1; 1722 1723 lp->d_magic = DISKMAGIC; 1724 lp->d_magic2 = DISKMAGIC; 1725 lp->d_checksum = dkcksum(lp); 1726 } 1727 1728 /* 1729 * Load the label information on the named device 1730 * Actually fabricate a disklabel 1731 * 1732 * EVENTUALLY take information about different 1733 * data tracks from the TOC and put it in the disklabel 1734 */ 1735 static void 1736 cdgetdisklabel(struct cd_softc *cd) 1737 { 1738 struct disklabel *lp = cd->sc_dk.dk_label; 1739 struct cd_formatted_toc toc; 1740 const char *errstring; 1741 int bmajor; 1742 1743 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1744 1745 cdgetdefaultlabel(cd, &toc, lp); 1746 1747 /* 1748 * Call the generic disklabel extraction routine 1749 * 1750 * bmajor follows ata_raid code 1751 */ 1752 bmajor = devsw_name2blk(device_xname(cd->sc_dev), NULL, 0); 1753 errstring = readdisklabel(MAKECDDEV(bmajor, 1754 device_unit(cd->sc_dev), RAW_PART), 1755 cdstrategy, lp, cd->sc_dk.dk_cpulabel); 1756 1757 /* if all went OK, we are passed a NULL error string */ 1758 if (errstring == NULL) 1759 return; 1760 1761 /* Reset to default label -- after printing error and the warning */ 1762 aprint_error_dev(cd->sc_dev, "%s\n", errstring); 1763 memset(cd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1764 cdgetdefaultlabel(cd, &toc, lp); 1765 } 1766 1767 /* 1768 * Reading a discs total capacity is aparently a very difficult issue for the 1769 * SCSI standardisation group. Every disc type seems to have its own 1770 * (re)invented size request method and modifiers. The failsafe way of 1771 * determining the total (max) capacity i.e. not the recorded capacity but the 1772 * total maximum capacity is to request the info on the last track and 1773 * calucate the total size. 1774 * 1775 * For ROM drives, we go for the CD recorded capacity. For recordable devices 1776 * we count. 1777 */ 1778 static int 1779 read_cd_capacity(struct scsipi_periph *periph, u_int *blksize, u_long *size) 1780 { 1781 struct scsipi_read_cd_capacity cap_cmd; 1782 struct scsipi_read_cd_cap_data cap; 1783 struct scsipi_read_discinfo di_cmd; 1784 struct scsipi_read_discinfo_data di; 1785 struct scsipi_read_trackinfo ti_cmd; 1786 struct scsipi_read_trackinfo_data ti; 1787 uint32_t track_start, track_size; 1788 int error, flags, msb, lsb, last_track; 1789 1790 /* if the device doesn't grog capacity, return the dummies */ 1791 if (periph->periph_quirks & PQUIRK_NOCAPACITY) 1792 return 0; 1793 1794 /* first try read CD capacity for blksize and recorded size */ 1795 /* issue the cd capacity request */ 1796 flags = XS_CTL_DATA_IN; 1797 memset(&cap_cmd, 0, sizeof(cap_cmd)); 1798 cap_cmd.opcode = READ_CD_CAPACITY; 1799 1800 error = scsipi_command(periph, 1801 (void *) &cap_cmd, sizeof(cap_cmd), 1802 (void *) &cap, sizeof(cap), 1803 CDRETRIES, 30000, NULL, flags); 1804 if (error) 1805 return error; 1806 1807 /* retrieve values and sanity check them */ 1808 *blksize = _4btol(cap.length); 1809 *size = _4btol(cap.addr); 1810 1811 /* blksize is 2048 for CD, but some drives give gibberish */ 1812 if ((*blksize < 512) || ((*blksize & 511) != 0)) 1813 *blksize = 2048; /* some drives lie ! */ 1814 1815 /* recordables have READ_DISCINFO implemented */ 1816 flags = XS_CTL_DATA_IN | XS_CTL_SILENT; 1817 memset(&di_cmd, 0, sizeof(di_cmd)); 1818 di_cmd.opcode = READ_DISCINFO; 1819 _lto2b(READ_DISCINFO_BIGSIZE, di_cmd.data_len); 1820 1821 error = scsipi_command(periph, 1822 (void *) &di_cmd, sizeof(di_cmd), 1823 (void *) &di, READ_DISCINFO_BIGSIZE, 1824 CDRETRIES, 30000, NULL, flags); 1825 if (error == 0) { 1826 msb = di.last_track_last_session_msb; 1827 lsb = di.last_track_last_session_lsb; 1828 last_track = (msb << 8) | lsb; 1829 1830 /* request info on last track */ 1831 memset(&ti_cmd, 0, sizeof(ti_cmd)); 1832 ti_cmd.opcode = READ_TRACKINFO; 1833 ti_cmd.addr_type = 1; /* on tracknr */ 1834 _lto4b(last_track, ti_cmd.address); /* tracknr */ 1835 _lto2b(sizeof(ti), ti_cmd.data_len); 1836 1837 error = scsipi_command(periph, 1838 (void *) &ti_cmd, sizeof(ti_cmd), 1839 (void *) &ti, sizeof(ti), 1840 CDRETRIES, 30000, NULL, flags); 1841 if (error == 0) { 1842 track_start = _4btol(ti.track_start); 1843 track_size = _4btol(ti.track_size); 1844 1845 /* overwrite only with a sane value */ 1846 if (track_start + track_size >= 100) 1847 *size = track_start + track_size; 1848 } 1849 } 1850 1851 /* sanity check for size */ 1852 if (*size < 100) 1853 *size = 400000; 1854 1855 return 0; 1856 } 1857 1858 /* 1859 * Find out from the device what it's capacity is 1860 */ 1861 static u_long 1862 cd_size(struct cd_softc *cd, int flags) 1863 { 1864 u_int blksize; 1865 u_long size; 1866 int error; 1867 1868 /* set up fake values */ 1869 blksize = 2048; 1870 size = 400000; 1871 1872 /* if this function bounces with an error return fake value */ 1873 error = read_cd_capacity(cd->sc_periph, &blksize, &size); 1874 if (error) 1875 return size; 1876 1877 if (blksize != 2048) { 1878 if (cd_setblksize(cd) == 0) 1879 blksize = 2048; 1880 } 1881 cd->params.blksize = blksize; 1882 cd->params.disksize = size; 1883 cd->params.disksize512 = ((u_int64_t)cd->params.disksize * blksize) / DEV_BSIZE; 1884 1885 SC_DEBUG(cd->sc_periph, SCSIPI_DB2, 1886 ("cd_size: %u %lu\n", blksize, size)); 1887 1888 return size; 1889 } 1890 1891 /* 1892 * Get scsi driver to send a "start playing" command 1893 */ 1894 static int 1895 cd_play(struct cd_softc *cd, int blkno, int nblks) 1896 { 1897 struct scsipi_play cmd; 1898 1899 memset(&cmd, 0, sizeof(cmd)); 1900 cmd.opcode = PLAY; 1901 _lto4b(blkno, cmd.blk_addr); 1902 _lto2b(nblks, cmd.xfer_len); 1903 1904 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1905 CDRETRIES, 30000, NULL, 0)); 1906 } 1907 1908 /* 1909 * Get scsi driver to send a "start playing" command 1910 */ 1911 static int 1912 cd_play_tracks(struct cd_softc *cd, struct cd_formatted_toc *toc, int strack, 1913 int sindex, int etrack, int eindex) 1914 { 1915 int error; 1916 1917 if (!etrack) 1918 return (EIO); 1919 if (strack > etrack) 1920 return (EINVAL); 1921 1922 error = cd_load_toc(cd, CD_TOC_FORM, toc, 0); 1923 if (error) 1924 return (error); 1925 1926 if (++etrack > (toc->header.ending_track+1)) 1927 etrack = toc->header.ending_track+1; 1928 1929 strack -= toc->header.starting_track; 1930 etrack -= toc->header.starting_track; 1931 if (strack < 0) 1932 return (EINVAL); 1933 1934 return (cd_play_msf(cd, toc->entries[strack].addr.msf.minute, 1935 toc->entries[strack].addr.msf.second, 1936 toc->entries[strack].addr.msf.frame, 1937 toc->entries[etrack].addr.msf.minute, 1938 toc->entries[etrack].addr.msf.second, 1939 toc->entries[etrack].addr.msf.frame)); 1940 } 1941 1942 /* 1943 * Get scsi driver to send a "play msf" command 1944 */ 1945 static int 1946 cd_play_msf(struct cd_softc *cd, int startm, int starts, int startf, int endm, 1947 int ends, int endf) 1948 { 1949 struct scsipi_play_msf cmd; 1950 1951 memset(&cmd, 0, sizeof(cmd)); 1952 cmd.opcode = PLAY_MSF; 1953 cmd.start_m = startm; 1954 cmd.start_s = starts; 1955 cmd.start_f = startf; 1956 cmd.end_m = endm; 1957 cmd.end_s = ends; 1958 cmd.end_f = endf; 1959 1960 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1961 CDRETRIES, 30000, NULL, 0)); 1962 } 1963 1964 /* 1965 * Get scsi driver to send a "start up" command 1966 */ 1967 static int 1968 cd_pause(struct cd_softc *cd, int go) 1969 { 1970 struct scsipi_pause cmd; 1971 1972 memset(&cmd, 0, sizeof(cmd)); 1973 cmd.opcode = PAUSE; 1974 cmd.resume = go & 0xff; 1975 1976 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1977 CDRETRIES, 30000, NULL, 0)); 1978 } 1979 1980 /* 1981 * Get scsi driver to send a "RESET" command 1982 */ 1983 static int 1984 cd_reset(struct cd_softc *cd) 1985 { 1986 1987 return (scsipi_command(cd->sc_periph, 0, 0, 0, 0, 1988 CDRETRIES, 30000, NULL, XS_CTL_RESET)); 1989 } 1990 1991 /* 1992 * Read subchannel 1993 */ 1994 static int 1995 cd_read_subchannel(struct cd_softc *cd, int mode, int format, int track, 1996 struct cd_sub_channel_info *data, int len, int flags) 1997 { 1998 struct scsipi_read_subchannel cmd; 1999 2000 memset(&cmd, 0, sizeof(cmd)); 2001 cmd.opcode = READ_SUBCHANNEL; 2002 if (mode == CD_MSF_FORMAT) 2003 cmd.byte2 |= CD_MSF; 2004 cmd.byte3 = SRS_SUBQ; 2005 cmd.subchan_format = format; 2006 cmd.track = track; 2007 _lto2b(len, cmd.data_len); 2008 2009 return (scsipi_command(cd->sc_periph, 2010 (void *)&cmd, sizeof(struct scsipi_read_subchannel), 2011 (void *)data, len, 2012 CDRETRIES, 30000, NULL, flags | XS_CTL_DATA_IN | XS_CTL_SILENT)); 2013 } 2014 2015 /* 2016 * Read table of contents 2017 */ 2018 static int 2019 cd_read_toc(struct cd_softc *cd, int respf, int mode, int start, 2020 struct cd_formatted_toc *toc, int len, int flags, int control) 2021 { 2022 struct scsipi_read_toc cmd; 2023 int ntoc; 2024 2025 memset(&cmd, 0, sizeof(cmd)); 2026 #if 0 2027 if (len != sizeof(struct ioc_toc_header)) 2028 ntoc = ((len) - sizeof(struct ioc_toc_header)) / 2029 sizeof(struct cd_toc_entry); 2030 else 2031 #endif 2032 ntoc = len; 2033 cmd.opcode = READ_TOC; 2034 if (mode == CD_MSF_FORMAT) 2035 cmd.addr_mode |= CD_MSF; 2036 cmd.resp_format = respf; 2037 cmd.from_track = start; 2038 _lto2b(ntoc, cmd.data_len); 2039 cmd.control = control; 2040 2041 return (scsipi_command(cd->sc_periph, 2042 (void *)&cmd, sizeof(cmd), (void *)toc, len, CDRETRIES, 2043 30000, NULL, flags | XS_CTL_DATA_IN)); 2044 } 2045 2046 static int 2047 cd_load_toc(struct cd_softc *cd, int respf, struct cd_formatted_toc *toc, int flags) 2048 { 2049 int ntracks, len, error; 2050 2051 if ((error = cd_read_toc(cd, respf, 0, 0, toc, sizeof(toc->header), 2052 flags, 0)) != 0) 2053 return (error); 2054 2055 ntracks = toc->header.ending_track - toc->header.starting_track + 1; 2056 len = (ntracks + 1) * sizeof(struct cd_toc_entry) + 2057 sizeof(toc->header); 2058 if ((error = cd_read_toc(cd, respf, CD_MSF_FORMAT, 0, toc, len, 2059 flags, 0)) != 0) 2060 return (error); 2061 return (0); 2062 } 2063 2064 /* 2065 * Get the scsi driver to send a full inquiry to the device and use the 2066 * results to fill out the disk parameter structure. 2067 */ 2068 static int 2069 cd_get_parms(struct cd_softc *cd, int flags) 2070 { 2071 2072 /* 2073 * give a number of sectors so that sec * trks * cyls 2074 * is <= disk_size 2075 */ 2076 if (cd_size(cd, flags) == 0) 2077 return (ENXIO); 2078 disk_blocksize(&cd->sc_dk, cd->params.blksize); 2079 return (0); 2080 } 2081 2082 static int 2083 cdsize(dev_t dev) 2084 { 2085 2086 /* CD-ROMs are read-only. */ 2087 return (-1); 2088 } 2089 2090 static int 2091 cddump(dev_t dev, daddr_t blkno, void *va, size_t size) 2092 { 2093 2094 /* Not implemented. */ 2095 return (ENXIO); 2096 } 2097 2098 #define dvd_copy_key(dst, src) memcpy((dst), (src), sizeof(dvd_key)) 2099 #define dvd_copy_challenge(dst, src) memcpy((dst), (src), sizeof(dvd_challenge)) 2100 2101 static int 2102 dvd_auth(struct cd_softc *cd, dvd_authinfo *a) 2103 { 2104 struct scsipi_generic cmd; 2105 u_int8_t bf[20]; 2106 int error; 2107 2108 memset(cmd.bytes, 0, 15); 2109 memset(bf, 0, sizeof(bf)); 2110 2111 switch (a->type) { 2112 case DVD_LU_SEND_AGID: 2113 cmd.opcode = GPCMD_REPORT_KEY; 2114 cmd.bytes[8] = 8; 2115 cmd.bytes[9] = 0 | (0 << 6); 2116 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2117 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2118 if (error) 2119 return (error); 2120 a->lsa.agid = bf[7] >> 6; 2121 return (0); 2122 2123 case DVD_LU_SEND_CHALLENGE: 2124 cmd.opcode = GPCMD_REPORT_KEY; 2125 cmd.bytes[8] = 16; 2126 cmd.bytes[9] = 1 | (a->lsc.agid << 6); 2127 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 2128 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2129 if (error) 2130 return (error); 2131 dvd_copy_challenge(a->lsc.chal, &bf[4]); 2132 return (0); 2133 2134 case DVD_LU_SEND_KEY1: 2135 cmd.opcode = GPCMD_REPORT_KEY; 2136 cmd.bytes[8] = 12; 2137 cmd.bytes[9] = 2 | (a->lsk.agid << 6); 2138 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 2139 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2140 if (error) 2141 return (error); 2142 dvd_copy_key(a->lsk.key, &bf[4]); 2143 return (0); 2144 2145 case DVD_LU_SEND_TITLE_KEY: 2146 cmd.opcode = GPCMD_REPORT_KEY; 2147 _lto4b(a->lstk.lba, &cmd.bytes[1]); 2148 cmd.bytes[8] = 12; 2149 cmd.bytes[9] = 4 | (a->lstk.agid << 6); 2150 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 2151 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2152 if (error) 2153 return (error); 2154 a->lstk.cpm = (bf[4] >> 7) & 1; 2155 a->lstk.cp_sec = (bf[4] >> 6) & 1; 2156 a->lstk.cgms = (bf[4] >> 4) & 3; 2157 dvd_copy_key(a->lstk.title_key, &bf[5]); 2158 return (0); 2159 2160 case DVD_LU_SEND_ASF: 2161 cmd.opcode = GPCMD_REPORT_KEY; 2162 cmd.bytes[8] = 8; 2163 cmd.bytes[9] = 5 | (a->lsasf.agid << 6); 2164 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2165 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2166 if (error) 2167 return (error); 2168 a->lsasf.asf = bf[7] & 1; 2169 return (0); 2170 2171 case DVD_HOST_SEND_CHALLENGE: 2172 cmd.opcode = GPCMD_SEND_KEY; 2173 cmd.bytes[8] = 16; 2174 cmd.bytes[9] = 1 | (a->hsc.agid << 6); 2175 bf[1] = 14; 2176 dvd_copy_challenge(&bf[4], a->hsc.chal); 2177 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 2178 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT); 2179 if (error) 2180 return (error); 2181 a->type = DVD_LU_SEND_KEY1; 2182 return (0); 2183 2184 case DVD_HOST_SEND_KEY2: 2185 cmd.opcode = GPCMD_SEND_KEY; 2186 cmd.bytes[8] = 12; 2187 cmd.bytes[9] = 3 | (a->hsk.agid << 6); 2188 bf[1] = 10; 2189 dvd_copy_key(&bf[4], a->hsk.key); 2190 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 12, 2191 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT); 2192 if (error) { 2193 a->type = DVD_AUTH_FAILURE; 2194 return (error); 2195 } 2196 a->type = DVD_AUTH_ESTABLISHED; 2197 return (0); 2198 2199 case DVD_INVALIDATE_AGID: 2200 cmd.opcode = GPCMD_REPORT_KEY; 2201 cmd.bytes[9] = 0x3f | (a->lsa.agid << 6); 2202 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 16, 2203 CDRETRIES, 30000, NULL, 0); 2204 if (error) 2205 return (error); 2206 return (0); 2207 2208 case DVD_LU_SEND_RPC_STATE: 2209 cmd.opcode = GPCMD_REPORT_KEY; 2210 cmd.bytes[8] = 8; 2211 cmd.bytes[9] = 8 | (0 << 6); 2212 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2213 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2214 if (error) 2215 return (error); 2216 a->lrpcs.type = (bf[4] >> 6) & 3; 2217 a->lrpcs.vra = (bf[4] >> 3) & 7; 2218 a->lrpcs.ucca = (bf[4]) & 7; 2219 a->lrpcs.region_mask = bf[5]; 2220 a->lrpcs.rpc_scheme = bf[6]; 2221 return (0); 2222 2223 case DVD_HOST_SEND_RPC_STATE: 2224 cmd.opcode = GPCMD_SEND_KEY; 2225 cmd.bytes[8] = 8; 2226 cmd.bytes[9] = 6 | (0 << 6); 2227 bf[1] = 6; 2228 bf[4] = a->hrpcs.pdrc; 2229 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 8, 2230 CDRETRIES, 30000, NULL, XS_CTL_DATA_OUT); 2231 if (error) 2232 return (error); 2233 return (0); 2234 2235 default: 2236 return (ENOTTY); 2237 } 2238 } 2239 2240 static int 2241 dvd_read_physical(struct cd_softc *cd, dvd_struct *s) 2242 { 2243 struct scsipi_generic cmd; 2244 u_int8_t bf[4 + 4 * 20], *bufp; 2245 int error; 2246 struct dvd_layer *layer; 2247 int i; 2248 2249 memset(cmd.bytes, 0, 15); 2250 memset(bf, 0, sizeof(bf)); 2251 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2252 cmd.bytes[6] = s->type; 2253 _lto2b(sizeof(bf), &cmd.bytes[7]); 2254 2255 cmd.bytes[5] = s->physical.layer_num; 2256 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2257 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2258 if (error) 2259 return (error); 2260 for (i = 0, bufp = &bf[4], layer = &s->physical.layer[0]; i < 4; 2261 i++, bufp += 20, layer++) { 2262 memset(layer, 0, sizeof(*layer)); 2263 layer->book_version = bufp[0] & 0xf; 2264 layer->book_type = bufp[0] >> 4; 2265 layer->min_rate = bufp[1] & 0xf; 2266 layer->disc_size = bufp[1] >> 4; 2267 layer->layer_type = bufp[2] & 0xf; 2268 layer->track_path = (bufp[2] >> 4) & 1; 2269 layer->nlayers = (bufp[2] >> 5) & 3; 2270 layer->track_density = bufp[3] & 0xf; 2271 layer->linear_density = bufp[3] >> 4; 2272 layer->start_sector = _4btol(&bufp[4]); 2273 layer->end_sector = _4btol(&bufp[8]); 2274 layer->end_sector_l0 = _4btol(&bufp[12]); 2275 layer->bca = bufp[16] >> 7; 2276 } 2277 return (0); 2278 } 2279 2280 static int 2281 dvd_read_copyright(struct cd_softc *cd, dvd_struct *s) 2282 { 2283 struct scsipi_generic cmd; 2284 u_int8_t bf[8]; 2285 int error; 2286 2287 memset(cmd.bytes, 0, 15); 2288 memset(bf, 0, sizeof(bf)); 2289 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2290 cmd.bytes[6] = s->type; 2291 _lto2b(sizeof(bf), &cmd.bytes[7]); 2292 2293 cmd.bytes[5] = s->copyright.layer_num; 2294 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2295 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2296 if (error) 2297 return (error); 2298 s->copyright.cpst = bf[4]; 2299 s->copyright.rmi = bf[5]; 2300 return (0); 2301 } 2302 2303 static int 2304 dvd_read_disckey(struct cd_softc *cd, dvd_struct *s) 2305 { 2306 struct scsipi_generic cmd; 2307 u_int8_t *bf; 2308 int error; 2309 2310 bf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2311 if (bf == NULL) 2312 return EIO; 2313 memset(cmd.bytes, 0, 15); 2314 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2315 cmd.bytes[6] = s->type; 2316 _lto2b(4 + 2048, &cmd.bytes[7]); 2317 2318 cmd.bytes[9] = s->disckey.agid << 6; 2319 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 4 + 2048, 2320 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2321 if (error == 0) 2322 memcpy(s->disckey.value, &bf[4], 2048); 2323 free(bf, M_TEMP); 2324 return error; 2325 } 2326 2327 static int 2328 dvd_read_bca(struct cd_softc *cd, dvd_struct *s) 2329 { 2330 struct scsipi_generic cmd; 2331 u_int8_t bf[4 + 188]; 2332 int error; 2333 2334 memset(cmd.bytes, 0, 15); 2335 memset(bf, 0, sizeof(bf)); 2336 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2337 cmd.bytes[6] = s->type; 2338 _lto2b(sizeof(bf), &cmd.bytes[7]); 2339 2340 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, sizeof(bf), 2341 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2342 if (error) 2343 return (error); 2344 s->bca.len = _2btol(&bf[0]); 2345 if (s->bca.len < 12 || s->bca.len > 188) 2346 return (EIO); 2347 memcpy(s->bca.value, &bf[4], s->bca.len); 2348 return (0); 2349 } 2350 2351 static int 2352 dvd_read_manufact(struct cd_softc *cd, dvd_struct *s) 2353 { 2354 struct scsipi_generic cmd; 2355 u_int8_t *bf; 2356 int error; 2357 2358 bf = malloc(4 + 2048, M_TEMP, M_WAITOK|M_ZERO); 2359 if (bf == NULL) 2360 return (EIO); 2361 memset(cmd.bytes, 0, 15); 2362 cmd.opcode = GPCMD_READ_DVD_STRUCTURE; 2363 cmd.bytes[6] = s->type; 2364 _lto2b(4 + 2048, &cmd.bytes[7]); 2365 2366 error = scsipi_command(cd->sc_periph, &cmd, 12, bf, 4 + 2048, 2367 CDRETRIES, 30000, NULL, XS_CTL_DATA_IN); 2368 if (error == 0) { 2369 s->manufact.len = _2btol(&bf[0]); 2370 if (s->manufact.len >= 0 && s->manufact.len <= 2048) 2371 memcpy(s->manufact.value, &bf[4], s->manufact.len); 2372 else 2373 error = EIO; 2374 } 2375 free(bf, M_TEMP); 2376 return error; 2377 } 2378 2379 static int 2380 dvd_read_struct(struct cd_softc *cd, dvd_struct *s) 2381 { 2382 2383 switch (s->type) { 2384 case DVD_STRUCT_PHYSICAL: 2385 return (dvd_read_physical(cd, s)); 2386 case DVD_STRUCT_COPYRIGHT: 2387 return (dvd_read_copyright(cd, s)); 2388 case DVD_STRUCT_DISCKEY: 2389 return (dvd_read_disckey(cd, s)); 2390 case DVD_STRUCT_BCA: 2391 return (dvd_read_bca(cd, s)); 2392 case DVD_STRUCT_MANUFACT: 2393 return (dvd_read_manufact(cd, s)); 2394 default: 2395 return (EINVAL); 2396 } 2397 } 2398 2399 static int 2400 cd_mode_sense(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size, 2401 int page, int flags, int *big) 2402 { 2403 2404 if (cd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) { 2405 *big = 1; 2406 return scsipi_mode_sense_big(cd->sc_periph, byte2, page, sense, 2407 size + sizeof(struct scsi_mode_parameter_header_10), 2408 flags, CDRETRIES, 20000); 2409 } else { 2410 *big = 0; 2411 return scsipi_mode_sense(cd->sc_periph, byte2, page, sense, 2412 size + sizeof(struct scsi_mode_parameter_header_6), 2413 flags, CDRETRIES, 20000); 2414 } 2415 } 2416 2417 static int 2418 cd_mode_select(struct cd_softc *cd, u_int8_t byte2, void *sense, size_t size, 2419 int flags, int big) 2420 { 2421 2422 if (big) { 2423 struct scsi_mode_parameter_header_10 *header = sense; 2424 2425 _lto2b(0, header->data_length); 2426 return scsipi_mode_select_big(cd->sc_periph, byte2, sense, 2427 size + sizeof(struct scsi_mode_parameter_header_10), 2428 flags, CDRETRIES, 20000); 2429 } else { 2430 struct scsi_mode_parameter_header_6 *header = sense; 2431 2432 header->data_length = 0; 2433 return scsipi_mode_select(cd->sc_periph, byte2, sense, 2434 size + sizeof(struct scsi_mode_parameter_header_6), 2435 flags, CDRETRIES, 20000); 2436 } 2437 } 2438 2439 static int 2440 cd_set_pa_immed(struct cd_softc *cd, int flags) 2441 { 2442 struct { 2443 union { 2444 struct scsi_mode_parameter_header_6 small; 2445 struct scsi_mode_parameter_header_10 big; 2446 } header; 2447 struct cd_audio_page page; 2448 } data; 2449 int error; 2450 uint8_t oflags; 2451 int big, byte2; 2452 struct cd_audio_page *page; 2453 2454 byte2 = SMS_DBD; 2455 try_again: 2456 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2457 AUDIO_PAGE, flags, &big)) != 0) { 2458 if (byte2 == SMS_DBD) { 2459 /* Device may not understand DBD; retry without */ 2460 byte2 = 0; 2461 goto try_again; 2462 } 2463 return (error); 2464 } 2465 2466 if (big) 2467 page = (void *)((u_long)&data.header.big + 2468 sizeof data.header.big + 2469 _2btol(data.header.big.blk_desc_len)); 2470 else 2471 page = (void *)((u_long)&data.header.small + 2472 sizeof data.header.small + 2473 data.header.small.blk_desc_len); 2474 2475 oflags = page->flags; 2476 page->flags &= ~CD_PA_SOTC; 2477 page->flags |= CD_PA_IMMED; 2478 if (oflags == page->flags) 2479 return (0); 2480 2481 return (cd_mode_select(cd, SMS_PF, &data, 2482 sizeof(struct scsi_mode_page_header) + page->pg_length, 2483 flags, big)); 2484 } 2485 2486 static int 2487 cd_setchan(struct cd_softc *cd, int p0, int p1, int p2, int p3, int flags) 2488 { 2489 struct { 2490 union { 2491 struct scsi_mode_parameter_header_6 small; 2492 struct scsi_mode_parameter_header_10 big; 2493 } header; 2494 struct cd_audio_page page; 2495 } data; 2496 int error; 2497 int big, byte2; 2498 struct cd_audio_page *page; 2499 2500 byte2 = SMS_DBD; 2501 try_again: 2502 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2503 AUDIO_PAGE, flags, &big)) != 0) { 2504 if (byte2 == SMS_DBD) { 2505 /* Device may not understand DBD; retry without */ 2506 byte2 = 0; 2507 goto try_again; 2508 } 2509 return (error); 2510 } 2511 2512 if (big) 2513 page = (void *)((u_long)&data.header.big + 2514 sizeof data.header.big + 2515 _2btol(data.header.big.blk_desc_len)); 2516 else 2517 page = (void *)((u_long)&data.header.small + 2518 sizeof data.header.small + 2519 data.header.small.blk_desc_len); 2520 2521 page->port[0].channels = p0; 2522 page->port[1].channels = p1; 2523 page->port[2].channels = p2; 2524 page->port[3].channels = p3; 2525 2526 return (cd_mode_select(cd, SMS_PF, &data, 2527 sizeof(struct scsi_mode_page_header) + page->pg_length, 2528 flags, big)); 2529 } 2530 2531 static int 2532 cd_getvol(struct cd_softc *cd, struct ioc_vol *arg, int flags) 2533 { 2534 struct { 2535 union { 2536 struct scsi_mode_parameter_header_6 small; 2537 struct scsi_mode_parameter_header_10 big; 2538 } header; 2539 struct cd_audio_page page; 2540 } data; 2541 int error; 2542 int big, byte2; 2543 struct cd_audio_page *page; 2544 2545 byte2 = SMS_DBD; 2546 try_again: 2547 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2548 AUDIO_PAGE, flags, &big)) != 0) { 2549 if (byte2 == SMS_DBD) { 2550 /* Device may not understand DBD; retry without */ 2551 byte2 = 0; 2552 goto try_again; 2553 } 2554 return (error); 2555 } 2556 2557 if (big) 2558 page = (void *)((u_long)&data.header.big + 2559 sizeof data.header.big + 2560 _2btol(data.header.big.blk_desc_len)); 2561 else 2562 page = (void *)((u_long)&data.header.small + 2563 sizeof data.header.small + 2564 data.header.small.blk_desc_len); 2565 2566 arg->vol[0] = page->port[0].volume; 2567 arg->vol[1] = page->port[1].volume; 2568 arg->vol[2] = page->port[2].volume; 2569 arg->vol[3] = page->port[3].volume; 2570 2571 return (0); 2572 } 2573 2574 static int 2575 cd_setvol(struct cd_softc *cd, const struct ioc_vol *arg, int flags) 2576 { 2577 struct { 2578 union { 2579 struct scsi_mode_parameter_header_6 small; 2580 struct scsi_mode_parameter_header_10 big; 2581 } header; 2582 struct cd_audio_page page; 2583 } data, mask; 2584 int error; 2585 int big, byte2; 2586 struct cd_audio_page *page, *page2; 2587 2588 byte2 = SMS_DBD; 2589 try_again: 2590 if ((error = cd_mode_sense(cd, byte2, &data, sizeof(data.page), 2591 AUDIO_PAGE, flags, &big)) != 0) { 2592 if (byte2 == SMS_DBD) { 2593 /* Device may not understand DBD; retry without */ 2594 byte2 = 0; 2595 goto try_again; 2596 } 2597 return (error); 2598 } 2599 if ((error = cd_mode_sense(cd, byte2, &mask, sizeof(mask.page), 2600 AUDIO_PAGE|SMS_PCTRL_CHANGEABLE, flags, &big)) != 0) 2601 return (error); 2602 2603 if (big) { 2604 page = (void *)((u_long)&data.header.big + 2605 sizeof data.header.big + 2606 _2btol(data.header.big.blk_desc_len)); 2607 page2 = (void *)((u_long)&mask.header.big + 2608 sizeof mask.header.big + 2609 _2btol(mask.header.big.blk_desc_len)); 2610 } else { 2611 page = (void *)((u_long)&data.header.small + 2612 sizeof data.header.small + 2613 data.header.small.blk_desc_len); 2614 page2 = (void *)((u_long)&mask.header.small + 2615 sizeof mask.header.small + 2616 mask.header.small.blk_desc_len); 2617 } 2618 2619 page->port[0].volume = arg->vol[0] & page2->port[0].volume; 2620 page->port[1].volume = arg->vol[1] & page2->port[1].volume; 2621 page->port[2].volume = arg->vol[2] & page2->port[2].volume; 2622 page->port[3].volume = arg->vol[3] & page2->port[3].volume; 2623 2624 page->port[0].channels = CHANNEL_0; 2625 page->port[1].channels = CHANNEL_1; 2626 2627 return (cd_mode_select(cd, SMS_PF, &data, 2628 sizeof(struct scsi_mode_page_header) + page->pg_length, 2629 flags, big)); 2630 } 2631 2632 static int 2633 cd_load_unload(struct cd_softc *cd, struct ioc_load_unload *args) 2634 { 2635 struct scsipi_load_unload cmd; 2636 2637 memset(&cmd, 0, sizeof(cmd)); 2638 cmd.opcode = LOAD_UNLOAD; 2639 cmd.options = args->options; /* ioctl uses MMC values */ 2640 cmd.slot = args->slot; 2641 2642 return (scsipi_command(cd->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 2643 CDRETRIES, 200000, NULL, 0)); 2644 } 2645 2646 static int 2647 cd_setblksize(struct cd_softc *cd) 2648 { 2649 struct { 2650 union { 2651 struct scsi_mode_parameter_header_6 small; 2652 struct scsi_mode_parameter_header_10 big; 2653 } header; 2654 struct scsi_general_block_descriptor blk_desc; 2655 } data; 2656 int error; 2657 int big, bsize; 2658 struct scsi_general_block_descriptor *bdesc; 2659 2660 if ((error = cd_mode_sense(cd, 0, &data, sizeof(data.blk_desc), 0, 0, 2661 &big)) != 0) 2662 return (error); 2663 2664 if (big) { 2665 bdesc = (void *)(&data.header.big + 1); 2666 bsize = _2btol(data.header.big.blk_desc_len); 2667 } else { 2668 bdesc = (void *)(&data.header.small + 1); 2669 bsize = data.header.small.blk_desc_len; 2670 } 2671 2672 if (bsize == 0) { 2673 printf("cd_setblksize: trying to change bsize, but no blk_desc\n"); 2674 return (EINVAL); 2675 } 2676 if (_3btol(bdesc->blklen) == 2048) { 2677 printf("cd_setblksize: trying to change bsize, but blk_desc is correct\n"); 2678 return (EINVAL); 2679 } 2680 2681 _lto3b(2048, bdesc->blklen); 2682 2683 return (cd_mode_select(cd, SMS_PF, &data, sizeof(data.blk_desc), 0, 2684 big)); 2685 } 2686 2687 2688 static int 2689 mmc_profile2class(uint16_t mmc_profile) 2690 { 2691 switch (mmc_profile) { 2692 case 0x01 : /* SCSI discs */ 2693 case 0x02 : 2694 /* this can't happen really, cd.c wouldn't have matched */ 2695 return MMC_CLASS_DISC; 2696 case 0x03 : /* Magneto Optical with sector erase */ 2697 case 0x04 : /* Magneto Optical write once */ 2698 case 0x05 : /* Advance Storage Magneto Optical */ 2699 return MMC_CLASS_MO; 2700 case 0x00 : /* Unknown MMC profile, can also be CD-ROM */ 2701 case 0x08 : /* CD-ROM */ 2702 case 0x09 : /* CD-R */ 2703 case 0x0a : /* CD-RW */ 2704 return MMC_CLASS_CD; 2705 case 0x10 : /* DVD-ROM */ 2706 case 0x11 : /* DVD-R */ 2707 case 0x12 : /* DVD-RAM */ 2708 case 0x13 : /* DVD-RW restricted overwrite */ 2709 case 0x14 : /* DVD-RW sequential */ 2710 case 0x1a : /* DVD+RW */ 2711 case 0x1b : /* DVD+R */ 2712 case 0x2a : /* DVD+RW Dual layer */ 2713 case 0x2b : /* DVD+R Dual layer */ 2714 case 0x50 : /* HD DVD-ROM */ 2715 case 0x51 : /* HD DVD-R */ 2716 case 0x52 : /* HD DVD-RW; DVD-RAM like */ 2717 return MMC_CLASS_DVD; 2718 case 0x40 : /* BD-ROM */ 2719 case 0x41 : /* BD-R Sequential recording (SRM) */ 2720 case 0x42 : /* BD-R Ramdom Recording (RRM) */ 2721 case 0x43 : /* BD-RE */ 2722 return MMC_CLASS_BD; 2723 } 2724 return MMC_CLASS_UNKN; 2725 } 2726 2727 2728 /* 2729 * Drive/media combination is reflected in a series of features that can 2730 * either be current or dormant. We try to make sense out of them to create a 2731 * set of easy to use flags that abstract the device/media capabilities. 2732 */ 2733 2734 static void 2735 mmc_process_feature(struct mmc_discinfo *mmc_discinfo, 2736 uint16_t feature, int cur, uint8_t *rpos) 2737 { 2738 uint32_t blockingnr; 2739 uint64_t flags; 2740 2741 if (cur == 1) { 2742 flags = mmc_discinfo->mmc_cur; 2743 } else { 2744 flags = mmc_discinfo->mmc_cap; 2745 } 2746 2747 switch (feature) { 2748 case 0x0010 : /* random readable feature */ 2749 blockingnr = rpos[5] | (rpos[4] << 8); 2750 if (blockingnr > 1) 2751 flags |= MMC_CAP_PACKET; 2752 2753 /* RW error page */ 2754 break; 2755 case 0x0020 : /* random writable feature */ 2756 flags |= MMC_CAP_RECORDABLE; 2757 flags |= MMC_CAP_REWRITABLE; 2758 blockingnr = rpos[9] | (rpos[8] << 8); 2759 if (blockingnr > 1) 2760 flags |= MMC_CAP_PACKET; 2761 break; 2762 case 0x0021 : /* incremental streaming write feature */ 2763 flags |= MMC_CAP_RECORDABLE; 2764 flags |= MMC_CAP_SEQUENTIAL; 2765 if (cur) 2766 mmc_discinfo->link_block_penalty = rpos[4]; 2767 if (rpos[2] & 1) 2768 flags |= MMC_CAP_ZEROLINKBLK; 2769 break; 2770 case 0x0022 : /* (obsolete) erase support feature */ 2771 flags |= MMC_CAP_RECORDABLE; 2772 flags |= MMC_CAP_ERASABLE; 2773 break; 2774 case 0x0023 : /* formatting media support feature */ 2775 flags |= MMC_CAP_RECORDABLE; 2776 flags |= MMC_CAP_FORMATTABLE; 2777 break; 2778 case 0x0024 : /* hardware assised defect management feature */ 2779 flags |= MMC_CAP_HW_DEFECTFREE; 2780 break; 2781 case 0x0025 : /* write once */ 2782 flags |= MMC_CAP_RECORDABLE; 2783 break; 2784 case 0x0026 : /* restricted overwrite feature */ 2785 flags |= MMC_CAP_RECORDABLE; 2786 flags |= MMC_CAP_REWRITABLE; 2787 flags |= MMC_CAP_STRICTOVERWRITE; 2788 break; 2789 case 0x0028 : /* MRW formatted media support feature */ 2790 flags |= MMC_CAP_MRW; 2791 break; 2792 case 0x002b : /* DVD+R read (and opt. write) support */ 2793 flags |= MMC_CAP_SEQUENTIAL; 2794 if (rpos[0] & 1) /* write support */ 2795 flags |= MMC_CAP_RECORDABLE; 2796 break; 2797 case 0x002c : /* rigid restricted overwrite feature */ 2798 flags |= MMC_CAP_RECORDABLE; 2799 flags |= MMC_CAP_REWRITABLE; 2800 flags |= MMC_CAP_STRICTOVERWRITE; 2801 if (rpos[0] & 1) /* blank bit */ 2802 flags |= MMC_CAP_BLANKABLE; 2803 break; 2804 case 0x002d : /* track at once recording feature */ 2805 flags |= MMC_CAP_RECORDABLE; 2806 flags |= MMC_CAP_SEQUENTIAL; 2807 break; 2808 case 0x002f : /* DVD-R/-RW write feature */ 2809 flags |= MMC_CAP_RECORDABLE; 2810 if (rpos[0] & 2) /* DVD-RW bit */ 2811 flags |= MMC_CAP_BLANKABLE; 2812 break; 2813 case 0x0038 : /* BD-R SRM with pseudo overwrite */ 2814 flags |= MMC_CAP_PSEUDOOVERWRITE; 2815 break; 2816 default : 2817 /* ignore */ 2818 break; 2819 } 2820 2821 if (cur == 1) { 2822 mmc_discinfo->mmc_cur = flags; 2823 } else { 2824 mmc_discinfo->mmc_cap = flags; 2825 } 2826 } 2827 2828 static int 2829 mmc_getdiscinfo_cdrom(struct scsipi_periph *periph, 2830 struct mmc_discinfo *mmc_discinfo) 2831 { 2832 struct scsipi_read_toc gtoc_cmd; 2833 struct scsipi_toc_header *toc_hdr; 2834 struct scsipi_toc_msinfo *toc_msinfo; 2835 const uint32_t buffer_size = 1024; 2836 uint32_t req_size; 2837 uint8_t *buffer; 2838 int error, flags; 2839 2840 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 2841 /* 2842 * Fabricate mmc_discinfo for CD-ROM. Some values are really `dont 2843 * care' but others might be of interest to programs. 2844 */ 2845 2846 mmc_discinfo->disc_state = MMC_STATE_FULL; 2847 mmc_discinfo->last_session_state = MMC_STATE_FULL; 2848 mmc_discinfo->bg_format_state = MMC_BGFSTATE_COMPLETED; 2849 mmc_discinfo->link_block_penalty = 7; /* not relevant */ 2850 2851 /* get number of sessions and first tracknr in last session */ 2852 flags = XS_CTL_DATA_IN; 2853 memset(>oc_cmd, 0, sizeof(gtoc_cmd)); 2854 gtoc_cmd.opcode = READ_TOC; 2855 gtoc_cmd.addr_mode = CD_MSF; /* not relevant */ 2856 gtoc_cmd.resp_format = CD_TOC_MSINFO; /* multisession info */ 2857 gtoc_cmd.from_track = 0; /* reserved, must be 0 */ 2858 req_size = sizeof(*toc_hdr) + sizeof(*toc_msinfo); 2859 _lto2b(req_size, gtoc_cmd.data_len); 2860 2861 error = scsipi_command(periph, 2862 (void *)>oc_cmd, sizeof(gtoc_cmd), 2863 (void *)buffer, req_size, 2864 CDRETRIES, 30000, NULL, flags); 2865 if (error) 2866 goto out; 2867 toc_hdr = (struct scsipi_toc_header *) buffer; 2868 toc_msinfo = (struct scsipi_toc_msinfo *) (buffer + 4); 2869 mmc_discinfo->num_sessions = toc_hdr->last - toc_hdr->first + 1; 2870 mmc_discinfo->first_track = toc_hdr->first; 2871 mmc_discinfo->first_track_last_session = toc_msinfo->tracknr; 2872 2873 /* get last track of last session */ 2874 flags = XS_CTL_DATA_IN; 2875 gtoc_cmd.resp_format = CD_TOC_FORM; /* formatted toc */ 2876 req_size = sizeof(*toc_hdr); 2877 _lto2b(req_size, gtoc_cmd.data_len); 2878 2879 error = scsipi_command(periph, 2880 (void *)>oc_cmd, sizeof(gtoc_cmd), 2881 (void *)buffer, req_size, 2882 CDRETRIES, 30000, NULL, flags); 2883 if (error) 2884 goto out; 2885 toc_hdr = (struct scsipi_toc_header *) buffer; 2886 mmc_discinfo->last_track_last_session = toc_hdr->last; 2887 mmc_discinfo->num_tracks = toc_hdr->last - toc_hdr->first + 1; 2888 2889 /* TODO how to handle disc_barcode and disc_id */ 2890 /* done */ 2891 2892 out: 2893 free(buffer, M_TEMP); 2894 return error; 2895 } 2896 2897 static int 2898 mmc_getdiscinfo_dvdrom(struct scsipi_periph *periph, 2899 struct mmc_discinfo *mmc_discinfo) 2900 { 2901 struct scsipi_read_toc gtoc_cmd; 2902 struct scsipi_toc_header toc_hdr; 2903 uint32_t req_size; 2904 int error, flags; 2905 2906 /* 2907 * Fabricate mmc_discinfo for DVD-ROM. Some values are really `dont 2908 * care' but others might be of interest to programs. 2909 */ 2910 2911 mmc_discinfo->disc_state = MMC_STATE_FULL; 2912 mmc_discinfo->last_session_state = MMC_STATE_FULL; 2913 mmc_discinfo->bg_format_state = MMC_BGFSTATE_COMPLETED; 2914 mmc_discinfo->link_block_penalty = 16; /* not relevant */ 2915 2916 /* get number of sessions and first tracknr in last session */ 2917 flags = XS_CTL_DATA_IN; 2918 memset(>oc_cmd, 0, sizeof(gtoc_cmd)); 2919 gtoc_cmd.opcode = READ_TOC; 2920 gtoc_cmd.addr_mode = 0; /* LBA */ 2921 gtoc_cmd.resp_format = CD_TOC_FORM; /* multisession info */ 2922 gtoc_cmd.from_track = 1; /* first track */ 2923 req_size = sizeof(toc_hdr); 2924 _lto2b(req_size, gtoc_cmd.data_len); 2925 2926 error = scsipi_command(periph, 2927 (void *)>oc_cmd, sizeof(gtoc_cmd), 2928 (void *)&toc_hdr, req_size, 2929 CDRETRIES, 30000, NULL, flags); 2930 if (error) 2931 return error; 2932 2933 /* DVD-ROM squashes the track/session space */ 2934 mmc_discinfo->num_sessions = toc_hdr.last - toc_hdr.first + 1; 2935 mmc_discinfo->num_tracks = mmc_discinfo->num_sessions; 2936 mmc_discinfo->first_track = toc_hdr.first; 2937 mmc_discinfo->first_track_last_session = toc_hdr.last; 2938 mmc_discinfo->last_track_last_session = toc_hdr.last; 2939 2940 /* TODO how to handle disc_barcode and disc_id */ 2941 /* done */ 2942 return 0; 2943 } 2944 2945 static int 2946 mmc_getdiscinfo(struct scsipi_periph *periph, 2947 struct mmc_discinfo *mmc_discinfo) 2948 { 2949 struct scsipi_get_configuration gc_cmd; 2950 struct scsipi_get_conf_data *gc; 2951 struct scsipi_get_conf_feature *gcf; 2952 struct scsipi_read_discinfo di_cmd; 2953 struct scsipi_read_discinfo_data di; 2954 const uint32_t buffer_size = 1024; 2955 uint32_t feat_tbl_len, pos; 2956 u_long last_lba; 2957 uint8_t *buffer, *fpos; 2958 int feature, last_feature, features_len, feature_cur, feature_len; 2959 int lsb, msb, error, flags; 2960 2961 feat_tbl_len = buffer_size; 2962 2963 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 2964 2965 /* initialise structure */ 2966 memset(mmc_discinfo, 0, sizeof(struct mmc_discinfo)); 2967 mmc_discinfo->mmc_profile = 0x00; /* unknown */ 2968 mmc_discinfo->mmc_class = MMC_CLASS_UNKN; 2969 mmc_discinfo->mmc_cur = 0; 2970 mmc_discinfo->mmc_cap = 0; 2971 mmc_discinfo->link_block_penalty = 0; 2972 2973 /* determine mmc profile and class */ 2974 flags = XS_CTL_DATA_IN; 2975 memset(&gc_cmd, 0, sizeof(gc_cmd)); 2976 gc_cmd.opcode = GET_CONFIGURATION; 2977 _lto2b(GET_CONF_NO_FEATURES_LEN, gc_cmd.data_len); 2978 2979 gc = (struct scsipi_get_conf_data *) buffer; 2980 2981 error = scsipi_command(periph, 2982 (void *)&gc_cmd, sizeof(gc_cmd), 2983 (void *) gc, GET_CONF_NO_FEATURES_LEN, 2984 CDRETRIES, 30000, NULL, flags); 2985 if (error) 2986 goto out; 2987 2988 mmc_discinfo->mmc_profile = _2btol(gc->mmc_profile); 2989 mmc_discinfo->mmc_class = mmc_profile2class(mmc_discinfo->mmc_profile); 2990 2991 /* assume 2048 sector size unless told otherwise */ 2992 mmc_discinfo->sector_size = 2048; 2993 error = read_cd_capacity(periph, &mmc_discinfo->sector_size, &last_lba); 2994 if (error) 2995 goto out; 2996 2997 mmc_discinfo->last_possible_lba = (uint32_t) last_lba - 1; 2998 2999 /* Read in all features to determine device capabilities */ 3000 last_feature = feature = 0; 3001 do { 3002 /* determine mmc profile and class */ 3003 flags = XS_CTL_DATA_IN; 3004 memset(&gc_cmd, 0, sizeof(gc_cmd)); 3005 gc_cmd.opcode = GET_CONFIGURATION; 3006 _lto2b(last_feature, gc_cmd.start_at_feature); 3007 _lto2b(feat_tbl_len, gc_cmd.data_len); 3008 3009 error = scsipi_command(periph, 3010 (void *)&gc_cmd, sizeof(gc_cmd), 3011 (void *) gc, feat_tbl_len, 3012 CDRETRIES, 30000, NULL, flags); 3013 if (error) { 3014 /* ieeek... break out of loop... i dunno what to do */ 3015 break; 3016 } 3017 3018 features_len = _4btol(gc->data_len); 3019 if (features_len < 4) 3020 break; 3021 3022 pos = 0; 3023 fpos = &gc->feature_desc[0]; 3024 while (pos < features_len - 4) { 3025 gcf = (struct scsipi_get_conf_feature *) fpos; 3026 3027 feature = _2btol(gcf->featurecode); 3028 feature_cur = gcf->flags & 1; 3029 feature_len = gcf->additional_length; 3030 3031 mmc_process_feature(mmc_discinfo, 3032 feature, feature_cur, 3033 gcf->feature_dependent); 3034 3035 last_feature = MAX(last_feature, feature); 3036 #ifdef DIAGNOSTIC 3037 /* assert((feature_len & 3) == 0); */ 3038 if ((feature_len & 3) != 0) { 3039 printf("feature %d having length %d\n", 3040 feature, feature_len); 3041 } 3042 #endif 3043 3044 pos += 4 + feature_len; 3045 fpos += 4 + feature_len; 3046 } 3047 /* unlikely to ever grow past our 1kb buffer */ 3048 } while (features_len >= 0xffff); 3049 3050 /* 3051 * Fixup CD-RW drives that are on crack. 3052 * 3053 * Some drives report the capability to incrementally write 3054 * sequentially on CD-R(W) media... nice, but this should not be 3055 * active for a fixed packet formatted CD-RW media. Other report the 3056 * ability of HW_DEFECTFREE even when the media is NOT MRW 3057 * formatted.... 3058 */ 3059 if (mmc_discinfo->mmc_profile == 0x0a) { 3060 if ((mmc_discinfo->mmc_cur & MMC_CAP_SEQUENTIAL) == 0) 3061 mmc_discinfo->mmc_cur |= MMC_CAP_STRICTOVERWRITE; 3062 if (mmc_discinfo->mmc_cur & MMC_CAP_STRICTOVERWRITE) 3063 mmc_discinfo->mmc_cur &= ~MMC_CAP_SEQUENTIAL; 3064 if (mmc_discinfo->mmc_cur & MMC_CAP_MRW) { 3065 mmc_discinfo->mmc_cur &= ~MMC_CAP_SEQUENTIAL; 3066 mmc_discinfo->mmc_cur &= ~MMC_CAP_STRICTOVERWRITE; 3067 } else { 3068 mmc_discinfo->mmc_cur &= ~MMC_CAP_HW_DEFECTFREE; 3069 } 3070 } 3071 if (mmc_discinfo->mmc_profile == 0x09) { 3072 mmc_discinfo->mmc_cur &= ~MMC_CAP_REWRITABLE; 3073 } 3074 3075 #ifdef DEBUG 3076 printf("CD mmc %d, mmc_cur 0x%"PRIx64", mmc_cap 0x%"PRIx64"\n", 3077 mmc_discinfo->mmc_profile, 3078 mmc_discinfo->mmc_cur, mmc_discinfo->mmc_cap); 3079 #endif 3080 3081 /* read in disc state and number of sessions and tracks */ 3082 flags = XS_CTL_DATA_IN | XS_CTL_SILENT; 3083 memset(&di_cmd, 0, sizeof(di_cmd)); 3084 di_cmd.opcode = READ_DISCINFO; 3085 di_cmd.data_len[1] = READ_DISCINFO_BIGSIZE; 3086 3087 error = scsipi_command(periph, 3088 (void *)&di_cmd, sizeof(di_cmd), 3089 (void *)&di, READ_DISCINFO_BIGSIZE, 3090 CDRETRIES, 30000, NULL, flags); 3091 3092 if (error) { 3093 /* discinfo call failed, emulate for cd-rom/dvd-rom */ 3094 if (mmc_discinfo->mmc_profile == 0x08) /* CD-ROM */ 3095 return mmc_getdiscinfo_cdrom(periph, mmc_discinfo); 3096 if (mmc_discinfo->mmc_profile == 0x10) /* DVD-ROM */ 3097 return mmc_getdiscinfo_dvdrom(periph, mmc_discinfo); 3098 /* CD/DVD drive is violating specs */ 3099 error = EIO; 3100 goto out; 3101 } 3102 3103 /* call went OK */ 3104 mmc_discinfo->disc_state = di.disc_state & 3; 3105 mmc_discinfo->last_session_state = (di.disc_state >> 2) & 3; 3106 mmc_discinfo->bg_format_state = (di.disc_state2 & 3); 3107 3108 lsb = di.num_sessions_lsb; 3109 msb = di.num_sessions_msb; 3110 mmc_discinfo->num_sessions = lsb | (msb << 8); 3111 3112 mmc_discinfo->first_track = di.first_track; 3113 lsb = di.first_track_last_session_lsb; 3114 msb = di.first_track_last_session_msb; 3115 mmc_discinfo->first_track_last_session = lsb | (msb << 8); 3116 lsb = di.last_track_last_session_lsb; 3117 msb = di.last_track_last_session_msb; 3118 mmc_discinfo->last_track_last_session = lsb | (msb << 8); 3119 3120 mmc_discinfo->num_tracks = mmc_discinfo->last_track_last_session - 3121 mmc_discinfo->first_track + 1; 3122 3123 /* set misc. flags and parameters from this disc info */ 3124 if (di.disc_state & 16) 3125 mmc_discinfo->mmc_cur |= MMC_CAP_BLANKABLE; 3126 3127 if (di.disc_state2 & 128) { 3128 mmc_discinfo->disc_id = _4btol(di.discid); 3129 mmc_discinfo->disc_flags |= MMC_DFLAGS_DISCIDVALID; 3130 } 3131 if (di.disc_state2 & 64) { 3132 mmc_discinfo->disc_barcode = _8btol(di.disc_bar_code); 3133 mmc_discinfo->disc_flags |= MMC_DFLAGS_BARCODEVALID; 3134 } 3135 if (di.disc_state2 & 32) 3136 mmc_discinfo->disc_flags |= MMC_DFLAGS_UNRESTRICTED; 3137 3138 if (di.disc_state2 & 16) { 3139 mmc_discinfo->application_code = di.application_code; 3140 mmc_discinfo->disc_flags |= MMC_DFLAGS_APPCODEVALID; 3141 } 3142 3143 /* done */ 3144 3145 out: 3146 free(buffer, M_TEMP); 3147 return error; 3148 } 3149 3150 static int 3151 mmc_gettrackinfo_cdrom(struct scsipi_periph *periph, 3152 struct mmc_trackinfo *trackinfo) 3153 { 3154 struct scsipi_read_toc gtoc_cmd; 3155 struct scsipi_toc_header *toc_hdr; 3156 struct scsipi_toc_rawtoc *rawtoc; 3157 uint32_t track_start, track_end, track_size; 3158 uint32_t last_recorded, next_writable; 3159 uint32_t lba, next_track_start, lead_out; 3160 const uint32_t buffer_size = 4 * 1024; /* worst case TOC estimate */ 3161 uint8_t *buffer; 3162 uint8_t track_sessionnr, last_tracknr, sessionnr, adr, tno, point; 3163 uint8_t control, tmin, tsec, tframe, pmin, psec, pframe; 3164 int size, req_size; 3165 int error, flags; 3166 3167 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 3168 3169 /* 3170 * Emulate read trackinfo for CD-ROM using the raw-TOC. 3171 * 3172 * Not all information is present and this presents a problem. Track 3173 * starts are known for each track but other values are deducted. 3174 * 3175 * For a complete overview of `magic' values used here, see the 3176 * SCSI/ATAPI MMC documentation. Note that the `magic' values have no 3177 * names, they are specified as numbers. 3178 */ 3179 3180 /* get raw toc to process, first header to check size */ 3181 flags = XS_CTL_DATA_IN | XS_CTL_SILENT; 3182 memset(>oc_cmd, 0, sizeof(gtoc_cmd)); 3183 gtoc_cmd.opcode = READ_TOC; 3184 gtoc_cmd.addr_mode = CD_MSF; /* not relevant */ 3185 gtoc_cmd.resp_format = CD_TOC_RAW; /* raw toc */ 3186 gtoc_cmd.from_track = 1; /* first session */ 3187 req_size = sizeof(*toc_hdr); 3188 _lto2b(req_size, gtoc_cmd.data_len); 3189 3190 error = scsipi_command(periph, 3191 (void *)>oc_cmd, sizeof(gtoc_cmd), 3192 (void *)buffer, req_size, 3193 CDRETRIES, 30000, NULL, flags); 3194 if (error) 3195 goto out; 3196 toc_hdr = (struct scsipi_toc_header *) buffer; 3197 if (_2btol(toc_hdr->length) > buffer_size - 2) { 3198 #ifdef DIAGNOSTIC 3199 printf("increase buffersize in mmc_readtrackinfo_cdrom\n"); 3200 #endif 3201 error = ENOBUFS; 3202 goto out; 3203 } 3204 3205 /* read in complete raw toc */ 3206 req_size = _2btol(toc_hdr->length); 3207 req_size = 2*((req_size + 1) / 2); /* for ATAPI */ 3208 _lto2b(req_size, gtoc_cmd.data_len); 3209 3210 error = scsipi_command(periph, 3211 (void *)>oc_cmd, sizeof(gtoc_cmd), 3212 (void *)buffer, req_size, 3213 CDRETRIES, 30000, NULL, flags); 3214 if (error) 3215 goto out; 3216 3217 toc_hdr = (struct scsipi_toc_header *) buffer; 3218 rawtoc = (struct scsipi_toc_rawtoc *) (buffer + 4); 3219 3220 track_start = 0; 3221 track_end = 0; 3222 track_size = 0; 3223 last_recorded = 0; 3224 next_writable = 0; 3225 flags = 0; 3226 3227 last_tracknr = 1; 3228 next_track_start = 0; 3229 track_sessionnr = MAXTRACK; /* by definition */ 3230 lead_out = 0; 3231 3232 size = req_size - sizeof(struct scsipi_toc_header) + 1; 3233 while (size > 0) { 3234 /* get track start and session end */ 3235 tno = rawtoc->tno; 3236 sessionnr = rawtoc->sessionnr; 3237 adr = rawtoc->adrcontrol >> 4; 3238 control = rawtoc->adrcontrol & 0xf; 3239 point = rawtoc->point; 3240 tmin = rawtoc->min; 3241 tsec = rawtoc->sec; 3242 tframe = rawtoc->frame; 3243 pmin = rawtoc->pmin; 3244 psec = rawtoc->psec; 3245 pframe = rawtoc->pframe; 3246 3247 if (tno == 0 && sessionnr && adr == 1) { 3248 lba = hmsf2lba(0, pmin, psec, pframe); 3249 if (point == trackinfo->tracknr) { 3250 track_start = lba; 3251 track_sessionnr = sessionnr; 3252 } 3253 if (point == trackinfo->tracknr + 1) { 3254 /* estimate size */ 3255 track_size = lba - track_start; 3256 next_track_start = lba; 3257 } 3258 if (point == 0xa2) { 3259 lead_out = lba; 3260 } 3261 if (point <= 0x63) { 3262 /* CD's ok, DVD are glued */ 3263 last_tracknr = point; 3264 } 3265 if (sessionnr == track_sessionnr) { 3266 last_recorded = lead_out; 3267 } 3268 } 3269 if (tno == 0 && sessionnr && adr == 5) { 3270 lba = hmsf2lba(0, tmin, tsec, tframe); 3271 if (sessionnr == track_sessionnr) { 3272 next_writable = lba; 3273 } 3274 } 3275 3276 if ((control & (3<<2)) == 4) /* 01xxb */ 3277 flags |= MMC_TRACKINFO_DATA; 3278 if ((control & (1<<2)) == 0) { /* x0xxb */ 3279 flags |= MMC_TRACKINFO_AUDIO; 3280 if (control & 1) /* xxx1b */ 3281 flags |= MMC_TRACKINFO_PRE_EMPH; 3282 } 3283 3284 rawtoc++; 3285 size -= sizeof(struct scsipi_toc_rawtoc); 3286 } 3287 3288 /* process found values; some voodoo */ 3289 /* if no tracksize tracknr is the last of the disc */ 3290 if ((track_size == 0) && last_recorded) { 3291 track_size = last_recorded - track_start; 3292 } 3293 /* if last_recorded < tracksize, tracksize is overestimated */ 3294 if (last_recorded) { 3295 if (last_recorded - track_start <= track_size) { 3296 track_size = last_recorded - track_start; 3297 flags |= MMC_TRACKINFO_LRA_VALID; 3298 } 3299 } 3300 /* check if its a the last track of the sector */ 3301 if (next_writable) { 3302 if (next_track_start > next_writable) 3303 flags |= MMC_TRACKINFO_NWA_VALID; 3304 } 3305 3306 /* no flag set -> no values */ 3307 if ((flags & MMC_TRACKINFO_LRA_VALID) == 0) 3308 last_recorded = 0; 3309 if ((flags & MMC_TRACKINFO_NWA_VALID) == 0) 3310 next_writable = 0; 3311 3312 /* fill in */ 3313 /* trackinfo->tracknr preserved */ 3314 trackinfo->sessionnr = track_sessionnr; 3315 trackinfo->track_mode = 7; /* data, incremental */ 3316 trackinfo->data_mode = 8; /* 2048 bytes mode1 */ 3317 3318 trackinfo->flags = flags; 3319 trackinfo->track_start = track_start; 3320 trackinfo->next_writable = next_writable; 3321 trackinfo->free_blocks = 0; 3322 trackinfo->packet_size = 1; 3323 trackinfo->track_size = track_size; 3324 trackinfo->last_recorded = last_recorded; 3325 3326 out: 3327 free(buffer, M_TEMP); 3328 return error; 3329 3330 } 3331 3332 static int 3333 mmc_gettrackinfo_dvdrom(struct scsipi_periph *periph, 3334 struct mmc_trackinfo *trackinfo) 3335 { 3336 struct scsipi_read_toc gtoc_cmd; 3337 struct scsipi_toc_header *toc_hdr; 3338 struct scsipi_toc_formatted *toc; 3339 uint32_t tracknr, track_start, track_size; 3340 uint32_t lba, lead_out; 3341 const uint32_t buffer_size = 4 * 1024; /* worst case TOC estimate */ 3342 uint8_t *buffer; 3343 uint8_t control, last_tracknr; 3344 int size, req_size; 3345 int error, flags; 3346 3347 3348 buffer = malloc(buffer_size, M_TEMP, M_WAITOK); 3349 /* 3350 * Emulate read trackinfo for DVD-ROM. We can't use the raw-TOC as the 3351 * CD-ROM emulation uses since the specification tells us that no such 3352 * thing is defined for DVD's. The reason for this is due to the large 3353 * number of tracks and that would clash with the `magic' values. This 3354 * suxs. 3355 * 3356 * Not all information is present and this presents a problem. 3357 * Track starts are known for each track but other values are 3358 * deducted. 3359 */ 3360 3361 /* get formatted toc to process, first header to check size */ 3362 flags = XS_CTL_DATA_IN | XS_CTL_SILENT; 3363 memset(>oc_cmd, 0, sizeof(gtoc_cmd)); 3364 gtoc_cmd.opcode = READ_TOC; 3365 gtoc_cmd.addr_mode = 0; /* lba's please */ 3366 gtoc_cmd.resp_format = CD_TOC_FORM; /* formatted toc */ 3367 gtoc_cmd.from_track = 1; /* first track */ 3368 req_size = sizeof(*toc_hdr); 3369 _lto2b(req_size, gtoc_cmd.data_len); 3370 3371 error = scsipi_command(periph, 3372 (void *)>oc_cmd, sizeof(gtoc_cmd), 3373 (void *)buffer, req_size, 3374 CDRETRIES, 30000, NULL, flags); 3375 if (error) 3376 goto out; 3377 toc_hdr = (struct scsipi_toc_header *) buffer; 3378 if (_2btol(toc_hdr->length) > buffer_size - 2) { 3379 #ifdef DIAGNOSTIC 3380 printf("incease buffersize in mmc_readtrackinfo_dvdrom\n"); 3381 #endif 3382 error = ENOBUFS; 3383 goto out; 3384 } 3385 3386 /* read in complete formatted toc */ 3387 req_size = _2btol(toc_hdr->length); 3388 _lto2b(req_size, gtoc_cmd.data_len); 3389 3390 error = scsipi_command(periph, 3391 (void *)>oc_cmd, sizeof(gtoc_cmd), 3392 (void *)buffer, req_size, 3393 CDRETRIES, 30000, NULL, flags); 3394 if (error) 3395 goto out; 3396 3397 toc_hdr = (struct scsipi_toc_header *) buffer; 3398 toc = (struct scsipi_toc_formatted *) (buffer + 4); 3399 3400 /* as in read disc info, all sessions are converted to tracks */ 3401 /* track 1.. -> offsets, sizes can be (rougly) estimated (16 ECC) */ 3402 /* last track -> we got the size from the lead-out */ 3403 3404 tracknr = 0; 3405 last_tracknr = toc_hdr->last; 3406 track_start = 0; 3407 track_size = 0; 3408 lead_out = 0; 3409 flags = 0; 3410 3411 size = req_size - sizeof(struct scsipi_toc_header) + 1; 3412 while (size > 0) { 3413 /* remember, DVD-ROM: tracknr == sessionnr */ 3414 lba = _4btol(toc->msf_lba); 3415 tracknr = toc->tracknr; 3416 control = toc->adrcontrol & 0xf; 3417 3418 if (trackinfo->tracknr == tracknr) { 3419 track_start = lba; 3420 } 3421 if (trackinfo->tracknr == tracknr+1) { 3422 track_size = lba - track_start; 3423 track_size -= 16; /* link block ? */ 3424 } 3425 if (tracknr == 0xAA) { 3426 lead_out = lba; 3427 } 3428 3429 if ((control & (3<<2)) == 4) /* 01xxb */ 3430 flags |= MMC_TRACKINFO_DATA; 3431 if ((control & (1<<2)) == 0) { /* x0xxb */ 3432 flags |= MMC_TRACKINFO_AUDIO; 3433 if (control & (1<<3)) /* 10xxb */ 3434 flags |= MMC_TRACKINFO_AUDIO_4CHAN; 3435 if (control & 1) /* xxx1b */ 3436 flags |= MMC_TRACKINFO_PRE_EMPH; 3437 } 3438 3439 toc++; 3440 size -= sizeof(struct scsipi_toc_formatted); 3441 } 3442 if (trackinfo->tracknr == last_tracknr) { 3443 track_size = lead_out - track_start; 3444 } 3445 3446 /* fill in */ 3447 /* trackinfo->tracknr preserved */ 3448 trackinfo->sessionnr = trackinfo->tracknr; 3449 trackinfo->track_mode = 0; /* unknown */ 3450 trackinfo->data_mode = 8; /* 2048 bytes mode1 */ 3451 3452 trackinfo->flags = flags; 3453 trackinfo->track_start = track_start; 3454 trackinfo->next_writable = 0; 3455 trackinfo->free_blocks = 0; 3456 trackinfo->packet_size = 16; /* standard length 16 blocks ECC */ 3457 trackinfo->track_size = track_size; 3458 trackinfo->last_recorded = 0; 3459 3460 out: 3461 free(buffer, M_TEMP); 3462 return error; 3463 } 3464 3465 static int 3466 mmc_gettrackinfo(struct scsipi_periph *periph, 3467 struct mmc_trackinfo *trackinfo) 3468 { 3469 struct scsipi_read_trackinfo ti_cmd; 3470 struct scsipi_read_trackinfo_data ti; 3471 struct scsipi_get_configuration gc_cmd; 3472 struct scsipi_get_conf_data gc; 3473 int error, flags; 3474 int mmc_profile; 3475 3476 /* set up SCSI call with track number from trackinfo.tracknr */ 3477 flags = XS_CTL_DATA_IN | XS_CTL_SILENT; 3478 memset(&ti_cmd, 0, sizeof(ti_cmd)); 3479 ti_cmd.opcode = READ_TRACKINFO; 3480 ti_cmd.addr_type = READ_TRACKINFO_ADDR_TRACK; 3481 ti_cmd.data_len[1] = READ_TRACKINFO_RETURNSIZE; 3482 3483 /* trackinfo.tracknr contains number of tracks to query */ 3484 _lto4b(trackinfo->tracknr, ti_cmd.address); 3485 error = scsipi_command(periph, 3486 (void *)&ti_cmd, sizeof(ti_cmd), 3487 (void *)&ti, READ_TRACKINFO_RETURNSIZE, 3488 CDRETRIES, 30000, NULL, flags); 3489 3490 if (error) { 3491 /* trackinfo call failed, emulate for cd-rom/dvd-rom */ 3492 /* first determine mmc profile */ 3493 flags = XS_CTL_DATA_IN; 3494 memset(&gc_cmd, 0, sizeof(gc_cmd)); 3495 gc_cmd.opcode = GET_CONFIGURATION; 3496 _lto2b(GET_CONF_NO_FEATURES_LEN, gc_cmd.data_len); 3497 3498 error = scsipi_command(periph, 3499 (void *)&gc_cmd, sizeof(gc_cmd), 3500 (void *)&gc, GET_CONF_NO_FEATURES_LEN, 3501 CDRETRIES, 30000, NULL, flags); 3502 if (error) 3503 return error; 3504 mmc_profile = _2btol(gc.mmc_profile); 3505 3506 /* choose emulation */ 3507 if (mmc_profile == 0x08) /* CD-ROM */ 3508 return mmc_gettrackinfo_cdrom(periph, trackinfo); 3509 if (mmc_profile == 0x10) /* DVD-ROM */ 3510 return mmc_gettrackinfo_dvdrom(periph, trackinfo); 3511 /* CD/DVD drive is violating specs */ 3512 return EIO; 3513 } 3514 3515 /* (re)initialise structure */ 3516 memset(trackinfo, 0, sizeof(struct mmc_trackinfo)); 3517 3518 /* account for short returns screwing up track and session msb */ 3519 if ((ti.data_len[1] | (ti.data_len[0] << 8)) <= 32) { 3520 ti.track_msb = 0; 3521 ti.session_msb = 0; 3522 } 3523 3524 trackinfo->tracknr = ti.track_lsb | (ti.track_msb << 8); 3525 trackinfo->sessionnr = ti.session_lsb | (ti.session_msb << 8); 3526 trackinfo->track_mode = ti.track_info_1 & 0xf; 3527 trackinfo->data_mode = ti.track_info_2 & 0xf; 3528 3529 flags = 0; 3530 if (ti.track_info_1 & 0x10) 3531 flags |= MMC_TRACKINFO_COPY; 3532 if (ti.track_info_1 & 0x20) 3533 flags |= MMC_TRACKINFO_DAMAGED; 3534 if (ti.track_info_2 & 0x10) 3535 flags |= MMC_TRACKINFO_FIXED_PACKET; 3536 if (ti.track_info_2 & 0x20) 3537 flags |= MMC_TRACKINFO_INCREMENTAL; 3538 if (ti.track_info_2 & 0x40) 3539 flags |= MMC_TRACKINFO_BLANK; 3540 if (ti.track_info_2 & 0x80) 3541 flags |= MMC_TRACKINFO_RESERVED; 3542 if (ti.data_valid & 0x01) 3543 flags |= MMC_TRACKINFO_NWA_VALID; 3544 if (ti.data_valid & 0x02) 3545 flags |= MMC_TRACKINFO_LRA_VALID; 3546 if ((trackinfo->track_mode & (3<<2)) == 4) /* 01xxb */ 3547 flags |= MMC_TRACKINFO_DATA; 3548 if ((trackinfo->track_mode & (1<<2)) == 0) { /* x0xxb */ 3549 flags |= MMC_TRACKINFO_AUDIO; 3550 if (trackinfo->track_mode & (1<<3)) /* 10xxb */ 3551 flags |= MMC_TRACKINFO_AUDIO_4CHAN; 3552 if (trackinfo->track_mode & 1) /* xxx1b */ 3553 flags |= MMC_TRACKINFO_PRE_EMPH; 3554 } 3555 3556 trackinfo->flags = flags; 3557 trackinfo->track_start = _4btol(ti.track_start); 3558 trackinfo->next_writable = _4btol(ti.next_writable); 3559 trackinfo->free_blocks = _4btol(ti.free_blocks); 3560 trackinfo->packet_size = _4btol(ti.packet_size); 3561 trackinfo->track_size = _4btol(ti.track_size); 3562 trackinfo->last_recorded = _4btol(ti.last_recorded); 3563 3564 return 0; 3565 } 3566 3567 static int 3568 mmc_doclose(struct scsipi_periph *periph, int param, int func) { 3569 struct scsipi_close_tracksession close_cmd; 3570 int error, flags; 3571 3572 /* set up SCSI call with track number */ 3573 flags = XS_CTL_DATA_OUT; 3574 memset(&close_cmd, 0, sizeof(close_cmd)); 3575 close_cmd.opcode = CLOSE_TRACKSESSION; 3576 close_cmd.function = func; 3577 _lto2b(param, close_cmd.tracksessionnr); 3578 3579 error = scsipi_command(periph, 3580 (void *) &close_cmd, sizeof(close_cmd), 3581 NULL, 0, 3582 CDRETRIES, 120000, NULL, flags); 3583 3584 return error; 3585 } 3586 3587 static int 3588 mmc_do_closetrack(struct scsipi_periph *periph, struct mmc_op *mmc_op) 3589 { 3590 int mmc_profile = mmc_op->mmc_profile; 3591 3592 switch (mmc_profile) { 3593 case 0x12 : /* DVD-RAM */ 3594 case 0x1a : /* DVD+RW */ 3595 case 0x2a : /* DVD+RW Dual layer */ 3596 case 0x42 : /* BD-R Ramdom Recording (RRM) */ 3597 case 0x43 : /* BD-RE */ 3598 case 0x52 : /* HD DVD-RW ; DVD-RAM like */ 3599 return EINVAL; 3600 } 3601 3602 return mmc_doclose(periph, mmc_op->tracknr, 1); 3603 } 3604 3605 static int 3606 mmc_do_close_or_finalise(struct scsipi_periph *periph, struct mmc_op *mmc_op) 3607 { 3608 uint8_t blob[MS5LEN], *page5; 3609 int mmc_profile = mmc_op->mmc_profile; 3610 int func, close, flags; 3611 int error; 3612 3613 close = (mmc_op->operation == MMC_OP_CLOSESESSION); 3614 3615 switch (mmc_profile) { 3616 case 0x09 : /* CD-R */ 3617 case 0x0a : /* CD-RW */ 3618 /* Special case : need to update MS field in mode page 5 */ 3619 memset(blob, 0, sizeof(blob)); 3620 page5 = blob+8; 3621 3622 flags = XS_CTL_DATA_IN; 3623 error = scsipi_mode_sense_big(periph, SMS_PF, 5, 3624 (void *)blob, sizeof(blob), flags, CDRETRIES, 20000); 3625 if (error) 3626 return error; 3627 3628 /* set multi session field when closing a session only */ 3629 page5[3] &= 63; 3630 if (close) 3631 page5[3] |= 3 << 6; 3632 3633 flags = XS_CTL_DATA_OUT; 3634 error = scsipi_mode_select_big(periph, SMS_PF, 3635 (void *)blob, sizeof(blob), flags, CDRETRIES, 20000); 3636 if (error) 3637 return error; 3638 /* and use funtion 2 */ 3639 func = 2; 3640 break; 3641 case 0x11 : /* DVD-R (DL) */ 3642 case 0x13 : /* DVD-RW restricted overwrite */ 3643 case 0x14 : /* DVD-RW sequential */ 3644 func = close ? 2 : 3; 3645 break; 3646 case 0x1b : /* DVD+R */ 3647 case 0x2b : /* DVD+R Dual layer */ 3648 case 0x51 : /* HD DVD-R */ 3649 case 0x41 : /* BD-R Sequential recording (SRM) */ 3650 func = close ? 2 : 6; 3651 break; 3652 case 0x12 : /* DVD-RAM */ 3653 case 0x1a : /* DVD+RW */ 3654 case 0x2a : /* DVD+RW Dual layer */ 3655 case 0x42 : /* BD-R Ramdom Recording (RRM) */ 3656 case 0x43 : /* BD-RE */ 3657 case 0x52 : /* HD DVD-RW; DVD-RAM like */ 3658 return EINVAL; 3659 default: 3660 printf("MMC close/finalise passed wrong device type! (%d)\n", 3661 mmc_profile); 3662 return EINVAL; 3663 } 3664 3665 return mmc_doclose(periph, mmc_op->sessionnr, func); 3666 } 3667 3668 static int 3669 mmc_do_reserve_track(struct scsipi_periph *periph, struct mmc_op *mmc_op) 3670 { 3671 struct scsipi_reserve_track reserve_cmd; 3672 uint32_t extent; 3673 int error, flags; 3674 3675 /* TODO make mmc safeguards? */ 3676 extent = mmc_op->extent; 3677 /* TODO min/max support? */ 3678 3679 /* set up SCSI call with requested space */ 3680 flags = XS_CTL_DATA_OUT; 3681 memset(&reserve_cmd, 0, sizeof(reserve_cmd)); 3682 reserve_cmd.opcode = RESERVE_TRACK; 3683 _lto4b(extent, reserve_cmd.reservation_size); 3684 3685 error = scsipi_command(periph, 3686 (void *) &reserve_cmd, sizeof(reserve_cmd), 3687 NULL, 0, 3688 CDRETRIES, 30000, NULL, flags); 3689 3690 return error; 3691 } 3692 3693 static int 3694 mmc_do_reserve_track_nwa(struct scsipi_periph *periph, struct mmc_op *mmc_op) 3695 { 3696 /* XXX assumes that NWA given is valid */ 3697 switch (mmc_op->mmc_profile) { 3698 case 0x09 : /* CD-R */ 3699 /* XXX unknown boundary checks XXX */ 3700 if (mmc_op->extent <= 152) 3701 return EINVAL; 3702 /* CD-R takes 152 sectors to close track */ 3703 mmc_op->extent -= 152; 3704 return mmc_do_reserve_track(periph, mmc_op); 3705 case 0x11 : /* DVD-R (DL) */ 3706 case 0x1b : /* DVD+R */ 3707 case 0x2b : /* DVD+R Dual layer */ 3708 if (mmc_op->extent % 16) 3709 return EINVAL; 3710 /* upto one ECC block of 16 sectors lost */ 3711 mmc_op->extent -= 16; 3712 return mmc_do_reserve_track(periph, mmc_op); 3713 case 0x41 : /* BD-R Sequential recording (SRM) */ 3714 case 0x51 : /* HD DVD-R */ 3715 if (mmc_op->extent % 32) 3716 return EINVAL; 3717 /* one ECC block of 32 sectors lost (AFAIK) */ 3718 mmc_op->extent -= 32; 3719 return mmc_do_reserve_track(periph, mmc_op); 3720 } 3721 3722 /* unknown behaviour or invalid disc type */ 3723 return EINVAL; 3724 } 3725 3726 static int 3727 mmc_do_repair_track(struct scsipi_periph *periph, struct mmc_op *mmc_op) 3728 { 3729 struct scsipi_repair_track repair_cmd; 3730 int error, flags; 3731 3732 /* TODO make mmc safeguards? */ 3733 3734 /* set up SCSI call with track number */ 3735 flags = XS_CTL_DATA_OUT; 3736 memset(&repair_cmd, 0, sizeof(repair_cmd)); 3737 repair_cmd.opcode = REPAIR_TRACK; 3738 _lto2b(mmc_op->tracknr, repair_cmd.tracknr); 3739 3740 error = scsipi_command(periph, 3741 (void *) &repair_cmd, sizeof(repair_cmd), 3742 NULL, 0, 3743 CDRETRIES, 30000, NULL, flags); 3744 3745 return error; 3746 } 3747 3748 static int 3749 mmc_do_op(struct scsipi_periph *periph, struct mmc_op *mmc_op) 3750 { 3751 /* guard operation value */ 3752 if (mmc_op->operation < 1 || mmc_op->operation > MMC_OP_MAX) 3753 return EINVAL; 3754 3755 /* synchronise cache is special since it doesn't rely on mmc_profile */ 3756 if (mmc_op->operation == MMC_OP_SYNCHRONISECACHE) 3757 return cdcachesync(periph, 0); 3758 3759 /* zero mmc_profile means unknown disc so operations are not defined */ 3760 if (mmc_op->mmc_profile == 0) { 3761 #ifdef DEBUG 3762 printf("mmc_do_op called with mmc_profile = 0\n"); 3763 #endif 3764 return EINVAL; 3765 } 3766 3767 /* do the operations */ 3768 switch (mmc_op->operation) { 3769 case MMC_OP_CLOSETRACK : 3770 return mmc_do_closetrack(periph, mmc_op); 3771 case MMC_OP_CLOSESESSION : 3772 case MMC_OP_FINALISEDISC : 3773 return mmc_do_close_or_finalise(periph, mmc_op); 3774 case MMC_OP_RESERVETRACK : 3775 return mmc_do_reserve_track(periph, mmc_op); 3776 case MMC_OP_RESERVETRACK_NWA : 3777 return mmc_do_reserve_track_nwa(periph, mmc_op); 3778 case MMC_OP_REPAIRTRACK : 3779 return mmc_do_repair_track(periph, mmc_op); 3780 case MMC_OP_UNCLOSELASTSESSION : 3781 /* TODO unclose last session support */ 3782 return EINVAL; 3783 default : 3784 printf("mmc_do_op: unhandled operation %d\n", mmc_op->operation); 3785 } 3786 3787 return EINVAL; 3788 } 3789 3790 static int 3791 mmc_setup_writeparams(struct scsipi_periph *periph, 3792 struct mmc_writeparams *mmc_writeparams) 3793 { 3794 struct mmc_trackinfo trackinfo; 3795 uint8_t blob[MS5LEN]; 3796 uint8_t *page5; 3797 int flags, error; 3798 int track_mode, data_mode; 3799 3800 /* setup mode page 5 for CD only */ 3801 if (mmc_writeparams->mmc_class != MMC_CLASS_CD) 3802 return 0; 3803 3804 memset(blob, 0, sizeof(blob)); 3805 page5 = blob+8; 3806 3807 /* read mode page 5 (with header) */ 3808 flags = XS_CTL_DATA_IN; 3809 error = scsipi_mode_sense_big(periph, SMS_PF, 5, (void *)blob, 3810 sizeof(blob), flags, CDRETRIES, 20000); 3811 if (error) 3812 return error; 3813 3814 /* set page length for reasurance */ 3815 page5[1] = P5LEN; /* page length */ 3816 3817 /* write type packet/incremental */ 3818 page5[2] &= 0xf0; 3819 3820 /* set specified mode parameters */ 3821 track_mode = mmc_writeparams->track_mode; 3822 data_mode = mmc_writeparams->data_mode; 3823 if (track_mode <= 0 || track_mode > 15) 3824 return EINVAL; 3825 if (data_mode < 1 || data_mode > 2) 3826 return EINVAL; 3827 3828 /* if a tracknr is passed, setup according to the track */ 3829 if (mmc_writeparams->tracknr > 0) { 3830 trackinfo.tracknr = mmc_writeparams->tracknr; 3831 error = mmc_gettrackinfo(periph, &trackinfo); 3832 if (error) 3833 return error; 3834 if ((trackinfo.flags & MMC_TRACKINFO_BLANK) == 0) { 3835 track_mode = trackinfo.track_mode; 3836 data_mode = trackinfo.data_mode; 3837 } 3838 mmc_writeparams->blockingnr = trackinfo.packet_size; 3839 } 3840 3841 /* copy track mode and data mode from trackinfo */ 3842 page5[3] &= 16; /* keep only `Copy' bit */ 3843 page5[3] |= (3 << 6) | track_mode; 3844 page5[4] &= 0xf0; /* wipe data block type */ 3845 if (data_mode == 1) { 3846 /* select ISO mode 1 (CD only) */ 3847 page5[4] |= 8; 3848 /* select session format normal disc (CD only) */ 3849 page5[8] = 0; 3850 } else { 3851 /* select ISO mode 2; XA form 1 (CD only) */ 3852 page5[4] |= 10; 3853 /* select session format CD-ROM XA disc (CD only) */ 3854 page5[8] = 0x20; 3855 } 3856 if (mmc_writeparams->mmc_cur & MMC_CAP_SEQUENTIAL) { 3857 if (mmc_writeparams->mmc_cur & MMC_CAP_ZEROLINKBLK) { 3858 /* set BUFE buffer underrun protection */ 3859 page5[2] |= 1<<6; 3860 } 3861 /* allow for multi session */ 3862 page5[3] |= 3 << 6; 3863 } else { 3864 /* select fixed packets */ 3865 page5[3] |= 1<<5; 3866 _lto4b(mmc_writeparams->blockingnr, &(page5[10])); 3867 } 3868 3869 /* write out updated mode page 5 (with header) */ 3870 flags = XS_CTL_DATA_OUT; 3871 error = scsipi_mode_select_big(periph, SMS_PF, (void *)blob, 3872 sizeof(blob), flags, CDRETRIES, 20000); 3873 if (error) 3874 return error; 3875 3876 return 0; 3877 } 3878 3879 static void 3880 cd_set_properties(struct cd_softc *cd) 3881 { 3882 prop_dictionary_t disk_info, odisk_info, geom; 3883 3884 disk_info = prop_dictionary_create(); 3885 3886 geom = prop_dictionary_create(); 3887 3888 prop_dictionary_set_uint64(geom, "sectors-per-unit", 3889 cd->params.disksize); 3890 3891 prop_dictionary_set_uint32(geom, "sector-size", 3892 cd->params.blksize); 3893 3894 prop_dictionary_set(disk_info, "geometry", geom); 3895 prop_object_release(geom); 3896 3897 prop_dictionary_set(device_properties(cd->sc_dev), 3898 "disk-info", disk_info); 3899 3900 /* 3901 * Don't release disk_info here; we keep a reference to it. 3902 * disk_detach() will release it when we go away. 3903 */ 3904 3905 odisk_info = cd->sc_dk.dk_info; 3906 cd->sc_dk.dk_info = disk_info; 3907 if (odisk_info) 3908 prop_object_release(odisk_info); 3909 } 3910