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