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