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