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