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