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