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