1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1997 Justin T. Gibbs. 5 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Kenneth D. Merry. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions, and the following disclaimer, 13 * without modification, immediately at the beginning of the file. 14 * 2. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /*- 31 * Portions of this driver taken from the original FreeBSD cd driver. 32 * Written by Julian Elischer (julian@tfs.com) 33 * for TRW Financial Systems for use under the MACH(2.5) operating system. 34 * 35 * TRW Financial Systems, in accordance with their agreement with Carnegie 36 * Mellon University, makes this software available to CMU to distribute 37 * or use in any manner that they see fit as long as this message is kept with 38 * the software. For this reason TFS also grants any other persons or 39 * organisations permission to use or modify this software. 40 * 41 * TFS supplies this software to be publicly redistributed 42 * on the understanding that TFS is not responsible for the correct 43 * functioning of this software in any circumstances. 44 * 45 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 46 * 47 * from: cd.c,v 1.83 1997/05/04 15:24:22 joerg Exp $ 48 */ 49 50 #include "opt_cd.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/kernel.h> 55 #include <sys/bio.h> 56 #include <sys/conf.h> 57 #include <sys/disk.h> 58 #include <sys/malloc.h> 59 #include <sys/cdio.h> 60 #include <sys/cdrio.h> 61 #include <sys/dvdio.h> 62 #include <sys/devicestat.h> 63 #include <sys/proc.h> 64 #include <sys/sbuf.h> 65 #include <sys/sysctl.h> 66 #include <sys/taskqueue.h> 67 #include <geom/geom_disk.h> 68 69 #include <cam/cam.h> 70 #include <cam/cam_ccb.h> 71 #include <cam/cam_periph.h> 72 #include <cam/cam_xpt_periph.h> 73 #include <cam/cam_queue.h> 74 #include <cam/cam_sim.h> 75 76 #include <cam/scsi/scsi_message.h> 77 #include <cam/scsi/scsi_da.h> 78 #include <cam/scsi/scsi_cd.h> 79 80 #define LEADOUT 0xaa /* leadout toc entry */ 81 82 struct cd_params { 83 uint32_t blksize; 84 u_long disksize; 85 }; 86 87 typedef enum { 88 CD_Q_NONE = 0x00, 89 CD_Q_NO_TOUCH = 0x01, 90 CD_Q_BCD_TRACKS = 0x02, 91 CD_Q_10_BYTE_ONLY = 0x10, 92 CD_Q_RETRY_BUSY = 0x40 93 } cd_quirks; 94 95 #define CD_Q_BIT_STRING \ 96 "\020" \ 97 "\001NO_TOUCH" \ 98 "\002BCD_TRACKS" \ 99 "\00510_BYTE_ONLY" \ 100 "\007RETRY_BUSY" 101 102 typedef enum { 103 CD_FLAG_NEW_DISC = 0x0002, 104 CD_FLAG_DISC_LOCKED = 0x0004, 105 CD_FLAG_DISC_REMOVABLE = 0x0008, 106 CD_FLAG_ACTIVE = 0x0080, 107 CD_FLAG_SCHED_ON_COMP = 0x0100, 108 CD_FLAG_RETRY_UA = 0x0200, 109 CD_FLAG_VALID_MEDIA = 0x0400, 110 CD_FLAG_VALID_TOC = 0x0800, 111 CD_FLAG_SCTX_INIT = 0x1000, 112 CD_FLAG_MEDIA_WAIT = 0x2000, 113 CD_FLAG_MEDIA_SCAN_ACT = 0x4000 114 } cd_flags; 115 116 typedef enum { 117 CD_CCB_PROBE = 0x01, 118 CD_CCB_BUFFER_IO = 0x02, 119 CD_CCB_TUR = 0x03, 120 CD_CCB_MEDIA_PREVENT = 0x04, 121 CD_CCB_MEDIA_ALLOW = 0x05, 122 CD_CCB_MEDIA_SIZE = 0x06, 123 CD_CCB_MEDIA_TOC_HDR = 0x07, 124 CD_CCB_MEDIA_TOC_FULL = 0x08, 125 CD_CCB_MEDIA_TOC_LEAD = 0x09, 126 CD_CCB_TYPE_MASK = 0x0F, 127 CD_CCB_RETRY_UA = 0x10 128 } cd_ccb_state; 129 130 #define ccb_state ppriv_field0 131 #define ccb_bp ppriv_ptr1 132 133 /* 134 * According to the MMC-6 spec, 6.25.3.2.11, the lead-out is reported by 135 * READ_TOC as logical track 170, so at most 169 tracks may be reported. 136 */ 137 struct cd_tocdata { 138 struct ioc_toc_header header; 139 struct cd_toc_entry entries[170]; 140 }; 141 142 struct cd_toc_single { 143 struct ioc_toc_header header; 144 struct cd_toc_entry entry; 145 }; 146 147 typedef enum { 148 CD_STATE_PROBE, 149 CD_STATE_NORMAL, 150 CD_STATE_MEDIA_PREVENT, 151 CD_STATE_MEDIA_ALLOW, 152 CD_STATE_MEDIA_SIZE, 153 CD_STATE_MEDIA_TOC_HDR, 154 CD_STATE_MEDIA_TOC_FULL, 155 CD_STATE_MEDIA_TOC_LEAD 156 } cd_state; 157 158 struct cd_softc { 159 cam_pinfo pinfo; 160 cd_state state; 161 volatile cd_flags flags; 162 struct bio_queue_head bio_queue; 163 LIST_HEAD(, ccb_hdr) pending_ccbs; 164 struct cd_params params; 165 cd_quirks quirks; 166 struct cam_periph *periph; 167 int minimum_command_size; 168 int outstanding_cmds; 169 int tur; 170 struct task sysctl_task; 171 struct sysctl_ctx_list sysctl_ctx; 172 struct sysctl_oid *sysctl_tree; 173 STAILQ_HEAD(, cd_mode_params) mode_queue; 174 struct cd_tocdata toc; 175 int toc_read_len; 176 struct cd_toc_single leadout; 177 struct disk *disk; 178 struct callout mediapoll_c; 179 180 #define CD_ANNOUNCETMP_SZ 120 181 char announce_temp[CD_ANNOUNCETMP_SZ]; 182 #define CD_ANNOUNCE_SZ 400 183 char announce_buf[CD_ANNOUNCE_SZ]; 184 }; 185 186 struct cd_page_sizes { 187 int page; 188 int page_size; 189 }; 190 191 static struct cd_page_sizes cd_page_size_table[] = 192 { 193 { AUDIO_PAGE, sizeof(struct cd_audio_page)} 194 }; 195 196 struct cd_quirk_entry { 197 struct scsi_inquiry_pattern inq_pat; 198 cd_quirks quirks; 199 }; 200 201 /* 202 * NOTE ON 10_BYTE_ONLY quirks: Any 10_BYTE_ONLY quirks MUST be because 203 * your device hangs when it gets a 10 byte command. Adding a quirk just 204 * to get rid of the informative diagnostic message is not acceptable. All 205 * 10_BYTE_ONLY quirks must be documented in full in a PR (which should be 206 * referenced in a comment along with the quirk) , and must be approved by 207 * ken@FreeBSD.org. Any quirks added that don't adhere to this policy may 208 * be removed until the submitter can explain why they are needed. 209 * 10_BYTE_ONLY quirks will be removed (as they will no longer be necessary) 210 * when the CAM_NEW_TRAN_CODE work is done. 211 */ 212 static struct cd_quirk_entry cd_quirk_table[] = 213 { 214 { 215 { T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"}, 216 /* quirks */ CD_Q_BCD_TRACKS 217 }, 218 { 219 /* 220 * VMware returns BUSY status when storage has transient 221 * connectivity problems, so better wait. 222 */ 223 {T_CDROM, SIP_MEDIA_REMOVABLE, "NECVMWar", "VMware IDE CDR10", "*"}, 224 /*quirks*/ CD_Q_RETRY_BUSY 225 } 226 }; 227 228 #ifdef COMPAT_FREEBSD32 229 struct ioc_read_toc_entry32 { 230 u_char address_format; 231 u_char starting_track; 232 u_short data_len; 233 uint32_t data; /* (struct cd_toc_entry *) */ 234 }; 235 #define CDIOREADTOCENTRYS_32 \ 236 _IOC_NEWTYPE(CDIOREADTOCENTRYS, struct ioc_read_toc_entry32) 237 #endif 238 239 static disk_open_t cdopen; 240 static disk_close_t cdclose; 241 static disk_ioctl_t cdioctl; 242 static disk_strategy_t cdstrategy; 243 244 static periph_init_t cdinit; 245 static periph_ctor_t cdregister; 246 static periph_dtor_t cdcleanup; 247 static periph_start_t cdstart; 248 static periph_oninv_t cdoninvalidate; 249 static void cdasync(void *callback_arg, uint32_t code, 250 struct cam_path *path, void *arg); 251 static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS); 252 static int cdrunccb(union ccb *ccb, 253 int (*error_routine)(union ccb *ccb, 254 uint32_t cam_flags, 255 uint32_t sense_flags), 256 uint32_t cam_flags, uint32_t sense_flags); 257 static void cddone(struct cam_periph *periph, 258 union ccb *start_ccb); 259 static union cd_pages *cdgetpage(struct cd_mode_params *mode_params); 260 static int cdgetpagesize(int page_num); 261 static void cdprevent(struct cam_periph *periph, int action); 262 static void cdmediaprobedone(struct cam_periph *periph); 263 static int cdcheckmedia(struct cam_periph *periph, bool do_wait); 264 static int cd6byteworkaround(union ccb *ccb); 265 static int cderror(union ccb *ccb, uint32_t cam_flags, 266 uint32_t sense_flags); 267 static int cdreadtoc(struct cam_periph *periph, uint32_t mode, 268 uint32_t start, uint8_t *data, 269 uint32_t len, uint32_t sense_flags); 270 static int cdgetmode(struct cam_periph *periph, 271 struct cd_mode_params *data, uint32_t page); 272 static int cdsetmode(struct cam_periph *periph, 273 struct cd_mode_params *data); 274 static int cdplay(struct cam_periph *periph, uint32_t blk, 275 uint32_t len); 276 static int cdreadsubchannel(struct cam_periph *periph, 277 uint32_t mode, uint32_t format, 278 int track, 279 struct cd_sub_channel_info *data, 280 uint32_t len); 281 static int cdplaymsf(struct cam_periph *periph, uint32_t startm, 282 uint32_t starts, uint32_t startf, 283 uint32_t endm, uint32_t ends, 284 uint32_t endf); 285 static int cdplaytracks(struct cam_periph *periph, 286 uint32_t strack, uint32_t sindex, 287 uint32_t etrack, uint32_t eindex); 288 static int cdpause(struct cam_periph *periph, uint32_t go); 289 static int cdstopunit(struct cam_periph *periph, uint32_t eject); 290 static int cdstartunit(struct cam_periph *periph, int load); 291 static int cdsetspeed(struct cam_periph *periph, 292 uint32_t rdspeed, uint32_t wrspeed); 293 static int cdreportkey(struct cam_periph *periph, 294 struct dvd_authinfo *authinfo); 295 static int cdsendkey(struct cam_periph *periph, 296 struct dvd_authinfo *authinfo); 297 static int cdreaddvdstructure(struct cam_periph *periph, 298 struct dvd_struct *dvdstruct); 299 static callout_func_t cdmediapoll; 300 301 static struct periph_driver cddriver = 302 { 303 cdinit, "cd", 304 TAILQ_HEAD_INITIALIZER(cddriver.units), /* generation */ 0 305 }; 306 307 PERIPHDRIVER_DECLARE(cd, cddriver); 308 309 #ifndef CD_DEFAULT_POLL_PERIOD 310 #define CD_DEFAULT_POLL_PERIOD 3 311 #endif 312 #ifndef CD_DEFAULT_RETRY 313 #define CD_DEFAULT_RETRY 4 314 #endif 315 #ifndef CD_DEFAULT_TIMEOUT 316 #define CD_DEFAULT_TIMEOUT 30000 317 #endif 318 319 static int cd_poll_period = CD_DEFAULT_POLL_PERIOD; 320 static int cd_retry_count = CD_DEFAULT_RETRY; 321 static int cd_timeout = CD_DEFAULT_TIMEOUT; 322 323 static SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 324 "CAM CDROM driver"); 325 SYSCTL_INT(_kern_cam_cd, OID_AUTO, poll_period, CTLFLAG_RWTUN, 326 &cd_poll_period, 0, "Media polling period in seconds"); 327 SYSCTL_INT(_kern_cam_cd, OID_AUTO, retry_count, CTLFLAG_RWTUN, 328 &cd_retry_count, 0, "Normal I/O retry count"); 329 SYSCTL_INT(_kern_cam_cd, OID_AUTO, timeout, CTLFLAG_RWTUN, 330 &cd_timeout, 0, "Timeout, in us, for read operations"); 331 332 static MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers"); 333 334 static void 335 cdinit(void) 336 { 337 cam_status status; 338 339 /* 340 * Install a global async callback. This callback will 341 * receive async callbacks like "new device found". 342 */ 343 status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL); 344 345 if (status != CAM_REQ_CMP) { 346 printf("cd: Failed to attach master async callback " 347 "due to status 0x%x!\n", status); 348 } 349 } 350 351 /* 352 * Callback from GEOM, called when it has finished cleaning up its 353 * resources. 354 */ 355 static void 356 cddiskgonecb(struct disk *dp) 357 { 358 struct cam_periph *periph; 359 360 periph = (struct cam_periph *)dp->d_drv1; 361 cam_periph_release(periph); 362 } 363 364 static void 365 cdoninvalidate(struct cam_periph *periph) 366 { 367 struct cd_softc *softc; 368 369 cam_periph_assert(periph, MA_OWNED); 370 softc = (struct cd_softc *)periph->softc; 371 372 /* 373 * De-register any async callbacks. 374 */ 375 xpt_register_async(0, cdasync, periph, periph->path); 376 377 /* 378 * Return all queued I/O with ENXIO. 379 * XXX Handle any transactions queued to the card 380 * with XPT_ABORT_CCB. 381 */ 382 bioq_flush(&softc->bio_queue, NULL, ENXIO); 383 384 disk_gone(softc->disk); 385 } 386 387 static void 388 cdcleanup(struct cam_periph *periph) 389 { 390 struct cd_softc *softc; 391 392 softc = (struct cd_softc *)periph->softc; 393 394 cam_periph_unlock(periph); 395 if ((softc->flags & CD_FLAG_SCTX_INIT) != 0 396 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { 397 xpt_print(periph->path, "can't remove sysctl context\n"); 398 } 399 400 callout_drain(&softc->mediapoll_c); 401 disk_destroy(softc->disk); 402 free(softc, M_DEVBUF); 403 cam_periph_lock(periph); 404 } 405 406 static void 407 cdasync(void *callback_arg, uint32_t code, 408 struct cam_path *path, void *arg) 409 { 410 struct cam_periph *periph; 411 struct cd_softc *softc; 412 413 periph = (struct cam_periph *)callback_arg; 414 switch (code) { 415 case AC_FOUND_DEVICE: 416 { 417 struct ccb_getdev *cgd; 418 cam_status status; 419 420 cgd = (struct ccb_getdev *)arg; 421 if (cgd == NULL) 422 break; 423 424 if (cgd->protocol != PROTO_SCSI) 425 break; 426 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED) 427 break; 428 if (SID_TYPE(&cgd->inq_data) != T_CDROM 429 && SID_TYPE(&cgd->inq_data) != T_WORM) 430 break; 431 432 /* 433 * Allocate a peripheral instance for 434 * this device and start the probe 435 * process. 436 */ 437 status = cam_periph_alloc(cdregister, cdoninvalidate, 438 cdcleanup, cdstart, 439 "cd", CAM_PERIPH_BIO, 440 path, cdasync, 441 AC_FOUND_DEVICE, cgd); 442 443 if (status != CAM_REQ_CMP 444 && status != CAM_REQ_INPROG) 445 printf("cdasync: Unable to attach new device " 446 "due to status 0x%x\n", status); 447 448 return; 449 } 450 case AC_UNIT_ATTENTION: 451 { 452 union ccb *ccb; 453 int error_code, sense_key, asc, ascq; 454 455 softc = (struct cd_softc *)periph->softc; 456 ccb = (union ccb *)arg; 457 458 /* 459 * Handle all media change UNIT ATTENTIONs except 460 * our own, as they will be handled by cderror(). 461 */ 462 if (xpt_path_periph(ccb->ccb_h.path) != periph && 463 scsi_extract_sense_ccb(ccb, 464 &error_code, &sense_key, &asc, &ascq)) { 465 /* 28/0: NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 466 if (asc == 0x28 && ascq == 0x00) 467 disk_media_changed(softc->disk, M_NOWAIT); 468 } 469 break; 470 } 471 case AC_SCSI_AEN: 472 cam_periph_assert(periph, MA_OWNED); 473 softc = (struct cd_softc *)periph->softc; 474 if (softc->state == CD_STATE_NORMAL && !softc->tur) { 475 if (cam_periph_acquire(periph) == 0) { 476 softc->tur = 1; 477 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 478 } 479 } 480 /* FALLTHROUGH */ 481 case AC_SENT_BDR: 482 case AC_BUS_RESET: 483 { 484 struct ccb_hdr *ccbh; 485 486 cam_periph_assert(periph, MA_OWNED); 487 softc = (struct cd_softc *)periph->softc; 488 /* 489 * Don't fail on the expected unit attention 490 * that will occur. 491 */ 492 softc->flags |= CD_FLAG_RETRY_UA; 493 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) 494 ccbh->ccb_state |= CD_CCB_RETRY_UA; 495 break; 496 } 497 default: 498 break; 499 } 500 501 cam_periph_async(periph, code, path, arg); 502 } 503 504 static void 505 cdsysctlinit(void *context, int pending) 506 { 507 struct cam_periph *periph; 508 struct cd_softc *softc; 509 char tmpstr[32], tmpstr2[16]; 510 511 periph = (struct cam_periph *)context; 512 if (cam_periph_acquire(periph) != 0) 513 return; 514 515 softc = (struct cd_softc *)periph->softc; 516 snprintf(tmpstr, sizeof(tmpstr), "CAM CD unit %d", periph->unit_number); 517 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 518 519 sysctl_ctx_init(&softc->sysctl_ctx); 520 cam_periph_lock(periph); 521 softc->flags |= CD_FLAG_SCTX_INIT; 522 cam_periph_unlock(periph); 523 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx, 524 SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO, 525 tmpstr2, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, 526 "device_index"); 527 528 if (softc->sysctl_tree == NULL) { 529 printf("cdsysctlinit: unable to allocate sysctl tree\n"); 530 cam_periph_release(periph); 531 return; 532 } 533 534 /* 535 * Now register the sysctl handler, so the user can the value on 536 * the fly. 537 */ 538 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 539 OID_AUTO, "minimum_cmd_size", 540 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 541 &softc->minimum_command_size, 0, cdcmdsizesysctl, "I", 542 "Minimum CDB size"); 543 544 cam_periph_release(periph); 545 } 546 547 /* 548 * We have a handler function for this so we can check the values when the 549 * user sets them, instead of every time we look at them. 550 */ 551 static int 552 cdcmdsizesysctl(SYSCTL_HANDLER_ARGS) 553 { 554 int error, value; 555 556 value = *(int *)arg1; 557 558 error = sysctl_handle_int(oidp, &value, 0, req); 559 560 if ((error != 0) 561 || (req->newptr == NULL)) 562 return (error); 563 564 /* 565 * The only real values we can have here are 6 or 10. I don't 566 * really forsee having 12 be an option at any time in the future. 567 * So if the user sets something less than or equal to 6, we'll set 568 * it to 6. If he sets something greater than 6, we'll set it to 10. 569 * 570 * I suppose we could just return an error here for the wrong values, 571 * but I don't think it's necessary to do so, as long as we can 572 * determine the user's intent without too much trouble. 573 */ 574 if (value < 6) 575 value = 6; 576 else if (value > 6) 577 value = 10; 578 579 *(int *)arg1 = value; 580 581 return (0); 582 } 583 584 static cam_status 585 cdregister(struct cam_periph *periph, void *arg) 586 { 587 struct cd_softc *softc; 588 struct ccb_pathinq cpi; 589 struct ccb_getdev *cgd; 590 char tmpstr[80]; 591 caddr_t match; 592 593 cgd = (struct ccb_getdev *)arg; 594 if (cgd == NULL) { 595 printf("cdregister: no getdev CCB, can't register device\n"); 596 return(CAM_REQ_CMP_ERR); 597 } 598 599 softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF, 600 M_NOWAIT | M_ZERO); 601 if (softc == NULL) { 602 printf("cdregister: Unable to probe new device. " 603 "Unable to allocate softc\n"); 604 return(CAM_REQ_CMP_ERR); 605 } 606 607 LIST_INIT(&softc->pending_ccbs); 608 STAILQ_INIT(&softc->mode_queue); 609 softc->state = CD_STATE_PROBE; 610 bioq_init(&softc->bio_queue); 611 if (SID_IS_REMOVABLE(&cgd->inq_data)) 612 softc->flags |= CD_FLAG_DISC_REMOVABLE; 613 614 periph->softc = softc; 615 softc->periph = periph; 616 617 /* 618 * See if this device has any quirks. 619 */ 620 match = cam_quirkmatch((caddr_t)&cgd->inq_data, 621 (caddr_t)cd_quirk_table, 622 nitems(cd_quirk_table), 623 sizeof(*cd_quirk_table), scsi_inquiry_match); 624 625 if (match != NULL) 626 softc->quirks = ((struct cd_quirk_entry *)match)->quirks; 627 else 628 softc->quirks = CD_Q_NONE; 629 630 /* Check if the SIM does not want 6 byte commands */ 631 xpt_path_inq(&cpi, periph->path); 632 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) 633 softc->quirks |= CD_Q_10_BYTE_ONLY; 634 635 TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph); 636 637 /* The default is 6 byte commands, unless quirked otherwise */ 638 if (softc->quirks & CD_Q_10_BYTE_ONLY) 639 softc->minimum_command_size = 10; 640 else 641 softc->minimum_command_size = 6; 642 643 /* 644 * Take a reference on the periph while cdstart is called to finish the 645 * probe. The reference will be dropped in cddone at the end of probe. 646 */ 647 (void)cam_periph_acquire(periph); 648 cam_periph_unlock(periph); 649 /* 650 * Load the user's default, if any. 651 */ 652 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size", 653 periph->unit_number); 654 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size); 655 656 /* 6 and 10 are the only permissible values here. */ 657 if (softc->minimum_command_size < 6) 658 softc->minimum_command_size = 6; 659 else if (softc->minimum_command_size > 6) 660 softc->minimum_command_size = 10; 661 662 /* 663 * We need to register the statistics structure for this device, 664 * but we don't have the blocksize yet for it. So, we register 665 * the structure and indicate that we don't have the blocksize 666 * yet. Unlike other SCSI peripheral drivers, we explicitly set 667 * the device type here to be CDROM, rather than just ORing in 668 * the device type. This is because this driver can attach to either 669 * CDROM or WORM devices, and we want this peripheral driver to 670 * show up in the devstat list as a CD peripheral driver, not a 671 * WORM peripheral driver. WORM drives will also have the WORM 672 * driver attached to them. 673 */ 674 softc->disk = disk_alloc(); 675 softc->disk->d_devstat = devstat_new_entry("cd", 676 periph->unit_number, 0, 677 DEVSTAT_BS_UNAVAILABLE, 678 DEVSTAT_TYPE_CDROM | 679 XPORT_DEVSTAT_TYPE(cpi.transport), 680 DEVSTAT_PRIORITY_CD); 681 softc->disk->d_open = cdopen; 682 softc->disk->d_close = cdclose; 683 softc->disk->d_strategy = cdstrategy; 684 softc->disk->d_gone = cddiskgonecb; 685 softc->disk->d_ioctl = cdioctl; 686 softc->disk->d_name = "cd"; 687 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor, 688 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr)); 689 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr)); 690 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)], 691 cgd->inq_data.product, sizeof(cgd->inq_data.product), 692 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr)); 693 softc->disk->d_unit = periph->unit_number; 694 softc->disk->d_drv1 = periph; 695 if (cpi.maxio == 0) 696 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */ 697 else if (cpi.maxio > maxphys) 698 softc->disk->d_maxsize = maxphys; /* for safety */ 699 else 700 softc->disk->d_maxsize = cpi.maxio; 701 softc->disk->d_flags = 0; 702 softc->disk->d_hba_vendor = cpi.hba_vendor; 703 softc->disk->d_hba_device = cpi.hba_device; 704 softc->disk->d_hba_subvendor = cpi.hba_subvendor; 705 softc->disk->d_hba_subdevice = cpi.hba_subdevice; 706 snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment), 707 "%s%d", cpi.dev_name, cpi.unit_number); 708 cam_periph_lock(periph); 709 710 /* 711 * Add an async callback so that we get 712 * notified if this device goes away. 713 */ 714 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE | 715 AC_SCSI_AEN | AC_UNIT_ATTENTION, cdasync, periph, periph->path); 716 717 /* 718 * Schedule a periodic media polling events. 719 */ 720 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0); 721 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) && 722 (cgd->inq_flags & SID_AEN) == 0 && 723 cd_poll_period != 0) { 724 callout_reset_sbt(&softc->mediapoll_c, cd_poll_period * SBT_1S, 725 0, cdmediapoll, periph, C_PREL(1)); 726 } 727 728 /* Released after probe when disk_create() call pass it to GEOM. */ 729 cam_periph_hold_boot(periph); 730 731 xpt_schedule(periph, CAM_PRIORITY_DEV); 732 return(CAM_REQ_CMP); 733 } 734 735 static int 736 cdopen(struct disk *dp) 737 { 738 struct cam_periph *periph; 739 int error; 740 741 periph = (struct cam_periph *)dp->d_drv1; 742 743 if (cam_periph_acquire(periph) != 0) 744 return(ENXIO); 745 746 cam_periph_lock(periph); 747 748 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 749 cam_periph_release_locked(periph); 750 cam_periph_unlock(periph); 751 return (error); 752 } 753 754 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 755 ("cdopen\n")); 756 757 /* 758 * Check for media, and set the appropriate flags. We don't bail 759 * if we don't have media, but then we don't allow anything but the 760 * CDIOCEJECT/CDIOCCLOSE ioctls if there is no media. 761 */ 762 cdcheckmedia(periph, /*do_wait*/ true); 763 764 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n")); 765 cam_periph_unhold(periph); 766 767 cam_periph_unlock(periph); 768 769 return (0); 770 } 771 772 static int 773 cdclose(struct disk *dp) 774 { 775 struct cam_periph *periph; 776 struct cd_softc *softc; 777 778 periph = (struct cam_periph *)dp->d_drv1; 779 softc = (struct cd_softc *)periph->softc; 780 781 cam_periph_lock(periph); 782 if (cam_periph_hold(periph, PRIBIO) != 0) { 783 cam_periph_unlock(periph); 784 cam_periph_release(periph); 785 return (0); 786 } 787 788 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 789 ("cdclose\n")); 790 791 if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0) 792 cdprevent(periph, PR_ALLOW); 793 794 /* 795 * Since we're closing this CD, mark the blocksize as unavailable. 796 * It will be marked as available when the CD is opened again. 797 */ 798 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE; 799 800 /* 801 * We'll check the media and toc again at the next open(). 802 */ 803 softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC); 804 805 cam_periph_unhold(periph); 806 cam_periph_release_locked(periph); 807 cam_periph_unlock(periph); 808 809 return (0); 810 } 811 812 static int 813 cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb, 814 uint32_t cam_flags, 815 uint32_t sense_flags), 816 uint32_t cam_flags, uint32_t sense_flags) 817 { 818 struct cd_softc *softc; 819 struct cam_periph *periph; 820 int error; 821 822 periph = xpt_path_periph(ccb->ccb_h.path); 823 softc = (struct cd_softc *)periph->softc; 824 825 error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags, 826 softc->disk->d_devstat); 827 828 return(error); 829 } 830 831 /* 832 * Actually translate the requested transfer into one the physical driver 833 * can understand. The transfer is described by a buf and will include 834 * only one physical transfer. 835 */ 836 static void 837 cdstrategy(struct bio *bp) 838 { 839 struct cam_periph *periph; 840 struct cd_softc *softc; 841 842 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 843 cam_periph_lock(periph); 844 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 845 ("cdstrategy(%p)\n", bp)); 846 847 softc = (struct cd_softc *)periph->softc; 848 849 /* 850 * If the device has been made invalid, error out 851 */ 852 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 853 cam_periph_unlock(periph); 854 biofinish(bp, NULL, ENXIO); 855 return; 856 } 857 858 /* 859 * Place it in the queue of disk activities for this disk 860 */ 861 bioq_disksort(&softc->bio_queue, bp); 862 863 /* 864 * If we don't know that we have valid media, schedule the media 865 * check first. The I/O will get executed after the media check. 866 */ 867 if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 868 cdcheckmedia(periph, /*do_wait*/ false); 869 else 870 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 871 872 cam_periph_unlock(periph); 873 return; 874 } 875 876 static void 877 cdstart(struct cam_periph *periph, union ccb *start_ccb) 878 { 879 struct cd_softc *softc; 880 struct bio *bp; 881 struct ccb_scsiio *csio; 882 883 cam_periph_assert(periph, MA_OWNED); 884 softc = (struct cd_softc *)periph->softc; 885 886 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n")); 887 888 switch (softc->state) { 889 case CD_STATE_NORMAL: 890 { 891 bp = bioq_first(&softc->bio_queue); 892 if (bp == NULL) { 893 if (softc->tur) { 894 softc->tur = 0; 895 csio = &start_ccb->csio; 896 scsi_test_unit_ready(csio, 897 /*retries*/ cd_retry_count, 898 cddone, 899 MSG_SIMPLE_Q_TAG, 900 SSD_FULL_SIZE, 901 cd_timeout); 902 start_ccb->ccb_h.ccb_bp = NULL; 903 start_ccb->ccb_h.ccb_state = CD_CCB_TUR; 904 xpt_action(start_ccb); 905 } else 906 xpt_release_ccb(start_ccb); 907 } else { 908 if (softc->tur) { 909 softc->tur = 0; 910 cam_periph_release_locked(periph); 911 } 912 bioq_remove(&softc->bio_queue, bp); 913 914 if ((bp->bio_cmd != BIO_READ) && 915 (bp->bio_cmd != BIO_WRITE)) { 916 biofinish(bp, NULL, EOPNOTSUPP); 917 xpt_release_ccb(start_ccb); 918 return; 919 } 920 921 scsi_read_write(&start_ccb->csio, 922 /*retries*/ cd_retry_count, 923 /* cbfcnp */ cddone, 924 MSG_SIMPLE_Q_TAG, 925 /* read */bp->bio_cmd == BIO_READ ? 926 SCSI_RW_READ : SCSI_RW_WRITE, 927 /* byte2 */ 0, 928 /* minimum_cmd_size */ 10, 929 /* lba */ bp->bio_offset / 930 softc->params.blksize, 931 bp->bio_bcount / softc->params.blksize, 932 /* data_ptr */ bp->bio_data, 933 /* dxfer_len */ bp->bio_bcount, 934 /* sense_len */ cd_retry_count ? 935 SSD_FULL_SIZE : SF_NO_PRINT, 936 /* timeout */ cd_timeout); 937 /* Use READ CD command for audio tracks. */ 938 if (softc->params.blksize == 2352) { 939 start_ccb->csio.cdb_io.cdb_bytes[0] = READ_CD; 940 start_ccb->csio.cdb_io.cdb_bytes[9] = 0xf8; 941 start_ccb->csio.cdb_io.cdb_bytes[10] = 0; 942 start_ccb->csio.cdb_io.cdb_bytes[11] = 0; 943 start_ccb->csio.cdb_len = 12; 944 } 945 start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO; 946 947 LIST_INSERT_HEAD(&softc->pending_ccbs, 948 &start_ccb->ccb_h, periph_links.le); 949 softc->outstanding_cmds++; 950 951 /* We expect a unit attention from this device */ 952 if ((softc->flags & CD_FLAG_RETRY_UA) != 0) { 953 start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA; 954 softc->flags &= ~CD_FLAG_RETRY_UA; 955 } 956 957 start_ccb->ccb_h.ccb_bp = bp; 958 bp = bioq_first(&softc->bio_queue); 959 960 xpt_action(start_ccb); 961 } 962 if (bp != NULL || softc->tur) { 963 /* Have more work to do, so ensure we stay scheduled */ 964 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 965 } 966 break; 967 } 968 case CD_STATE_PROBE: 969 case CD_STATE_MEDIA_SIZE: 970 { 971 struct scsi_read_capacity_data *rcap; 972 973 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap), 974 M_SCSICD, M_NOWAIT | M_ZERO); 975 if (rcap == NULL) { 976 xpt_print(periph->path, 977 "%s: Couldn't malloc read_capacity data\n", 978 __func__); 979 xpt_release_ccb(start_ccb); 980 /* 981 * We can't probe because we can't allocate memory, 982 * so invalidate the peripheral. The system probably 983 * has larger problems at this stage. If we've 984 * already probed (and are re-probing capacity), we 985 * don't need to invalidate. 986 * 987 * XXX KDM need to reset probe state and kick out 988 * pending I/O. 989 */ 990 if (softc->state == CD_STATE_PROBE) 991 cam_periph_invalidate(periph); 992 break; 993 } 994 995 /* 996 * Set the default capacity and sector size to something that 997 * GEOM can handle. This will get reset when a read capacity 998 * completes successfully. 999 */ 1000 softc->disk->d_sectorsize = 2048; 1001 softc->disk->d_mediasize = 0; 1002 1003 csio = &start_ccb->csio; 1004 scsi_read_capacity(csio, 1005 /*retries*/ cd_retry_count, 1006 cddone, 1007 MSG_SIMPLE_Q_TAG, 1008 rcap, 1009 SSD_FULL_SIZE, 1010 /*timeout*/20000); 1011 start_ccb->ccb_h.ccb_bp = NULL; 1012 if (softc->state == CD_STATE_PROBE) 1013 start_ccb->ccb_h.ccb_state = CD_CCB_PROBE; 1014 else 1015 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_SIZE; 1016 xpt_action(start_ccb); 1017 break; 1018 } 1019 case CD_STATE_MEDIA_ALLOW: 1020 case CD_STATE_MEDIA_PREVENT: 1021 { 1022 /* 1023 * If the CD is already locked, we don't need to do this. 1024 * Move on to the capacity check. 1025 */ 1026 if (softc->state == CD_STATE_MEDIA_PREVENT 1027 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0) { 1028 softc->state = CD_STATE_MEDIA_SIZE; 1029 xpt_release_ccb(start_ccb); 1030 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1031 break; 1032 } 1033 1034 scsi_prevent(&start_ccb->csio, 1035 /*retries*/ cd_retry_count, 1036 /*cbfcnp*/ cddone, 1037 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1038 /*action*/ (softc->state == CD_STATE_MEDIA_ALLOW) ? 1039 PR_ALLOW : PR_PREVENT, 1040 /*sense_len*/ SSD_FULL_SIZE, 1041 /*timeout*/ 60000); 1042 1043 start_ccb->ccb_h.ccb_bp = NULL; 1044 if (softc->state == CD_STATE_MEDIA_ALLOW) 1045 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_ALLOW; 1046 else 1047 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_PREVENT; 1048 xpt_action(start_ccb); 1049 break; 1050 } 1051 case CD_STATE_MEDIA_TOC_HDR: { 1052 struct ioc_toc_header *toch; 1053 1054 bzero(&softc->toc, sizeof(softc->toc)); 1055 1056 toch = &softc->toc.header; 1057 1058 scsi_read_toc(&start_ccb->csio, 1059 /*retries*/ cd_retry_count, 1060 /*cbfcnp*/ cddone, 1061 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1062 /*byte1_flags*/ 0, 1063 /*format*/ SRTOC_FORMAT_TOC, 1064 /*track*/ 0, 1065 /*data_ptr*/ (uint8_t *)toch, 1066 /*dxfer_len*/ sizeof(*toch), 1067 /*sense_len*/ SSD_FULL_SIZE, 1068 /*timeout*/ 50000); 1069 start_ccb->ccb_h.ccb_bp = NULL; 1070 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_HDR; 1071 xpt_action(start_ccb); 1072 break; 1073 } 1074 case CD_STATE_MEDIA_TOC_FULL: { 1075 bzero(&softc->toc, sizeof(softc->toc)); 1076 1077 scsi_read_toc(&start_ccb->csio, 1078 /*retries*/ cd_retry_count, 1079 /*cbfcnp*/ cddone, 1080 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1081 /*byte1_flags*/ 0, 1082 /*format*/ SRTOC_FORMAT_TOC, 1083 /*track*/ 0, 1084 /*data_ptr*/ (uint8_t *)&softc->toc, 1085 /*dxfer_len*/ softc->toc_read_len ? 1086 softc->toc_read_len : 1087 sizeof(softc->toc), 1088 /*sense_len*/ SSD_FULL_SIZE, 1089 /*timeout*/ 50000); 1090 start_ccb->ccb_h.ccb_bp = NULL; 1091 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_FULL; 1092 xpt_action(start_ccb); 1093 break; 1094 } 1095 case CD_STATE_MEDIA_TOC_LEAD: { 1096 struct cd_toc_single *leadout; 1097 1098 leadout = &softc->leadout; 1099 bzero(leadout, sizeof(*leadout)); 1100 1101 scsi_read_toc(&start_ccb->csio, 1102 /*retries*/ cd_retry_count, 1103 /*cbfcnp*/ cddone, 1104 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1105 /*byte1_flags*/ CD_MSF, 1106 /*format*/ SRTOC_FORMAT_TOC, 1107 /*track*/ LEADOUT, 1108 /*data_ptr*/ (uint8_t *)leadout, 1109 /*dxfer_len*/ sizeof(*leadout), 1110 /*sense_len*/ SSD_FULL_SIZE, 1111 /*timeout*/ 50000); 1112 start_ccb->ccb_h.ccb_bp = NULL; 1113 start_ccb->ccb_h.ccb_state = CD_CCB_MEDIA_TOC_LEAD; 1114 xpt_action(start_ccb); 1115 break; 1116 } 1117 } 1118 } 1119 1120 static void 1121 cddone(struct cam_periph *periph, union ccb *done_ccb) 1122 { 1123 struct cd_softc *softc; 1124 struct ccb_scsiio *csio; 1125 1126 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n")); 1127 1128 cam_periph_assert(periph, MA_OWNED); 1129 softc = (struct cd_softc *)periph->softc; 1130 csio = &done_ccb->csio; 1131 1132 switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) { 1133 case CD_CCB_BUFFER_IO: 1134 { 1135 struct bio *bp; 1136 int error; 1137 1138 bp = (struct bio *)done_ccb->ccb_h.ccb_bp; 1139 error = 0; 1140 1141 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1142 int sf; 1143 1144 if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0) 1145 sf = SF_RETRY_UA; 1146 else 1147 sf = 0; 1148 1149 error = cderror(done_ccb, CAM_RETRY_SELTO, sf); 1150 if (error == ERESTART) { 1151 /* 1152 * A retry was scheuled, so 1153 * just return. 1154 */ 1155 return; 1156 } 1157 } 1158 1159 if (error != 0) { 1160 xpt_print(periph->path, 1161 "cddone: got error %#x back\n", error); 1162 bioq_flush(&softc->bio_queue, NULL, EIO); 1163 bp->bio_resid = bp->bio_bcount; 1164 bp->bio_error = error; 1165 bp->bio_flags |= BIO_ERROR; 1166 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1167 cam_release_devq(done_ccb->ccb_h.path, 1168 /*relsim_flags*/0, 1169 /*reduction*/0, 1170 /*timeout*/0, 1171 /*getcount_only*/0); 1172 1173 } else { 1174 bp->bio_resid = csio->resid; 1175 bp->bio_error = 0; 1176 if (bp->bio_resid != 0) { 1177 /* 1178 * Short transfer ??? 1179 * XXX: not sure this is correct for partial 1180 * transfers at EOM 1181 */ 1182 bp->bio_flags |= BIO_ERROR; 1183 } 1184 } 1185 1186 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1187 softc->outstanding_cmds--; 1188 1189 biofinish(bp, NULL, 0); 1190 break; 1191 } 1192 case CD_CCB_PROBE: 1193 { 1194 struct scsi_read_capacity_data *rdcap; 1195 char *announce_buf; 1196 struct cd_params *cdp; 1197 int error; 1198 1199 cdp = &softc->params; 1200 announce_buf = softc->announce_temp; 1201 bzero(announce_buf, CD_ANNOUNCETMP_SZ); 1202 1203 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1204 1205 cdp->disksize = scsi_4btoul (rdcap->addr) + 1; 1206 cdp->blksize = scsi_4btoul (rdcap->length); 1207 1208 /* 1209 * Retry any UNIT ATTENTION type errors. They 1210 * are expected at boot. 1211 */ 1212 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP || 1213 (error = cderror(done_ccb, CAM_RETRY_SELTO, 1214 SF_RETRY_UA | SF_NO_PRINT)) == 0) { 1215 snprintf(announce_buf, CD_ANNOUNCETMP_SZ, 1216 "%juMB (%ju %u byte sectors)", 1217 ((uintmax_t)cdp->disksize * cdp->blksize) / 1218 (1024 * 1024), 1219 (uintmax_t)cdp->disksize, cdp->blksize); 1220 } else { 1221 if (error == ERESTART) { 1222 /* 1223 * A retry was scheuled, so 1224 * just return. 1225 */ 1226 return; 1227 } else { 1228 int asc, ascq; 1229 int sense_key, error_code; 1230 int have_sense; 1231 cam_status status; 1232 struct ccb_getdev cgd; 1233 1234 /* Don't wedge this device's queue */ 1235 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1236 cam_release_devq(done_ccb->ccb_h.path, 1237 /*relsim_flags*/0, 1238 /*reduction*/0, 1239 /*timeout*/0, 1240 /*getcount_only*/0); 1241 1242 status = done_ccb->ccb_h.status; 1243 1244 bzero(&cgd, sizeof(cgd)); 1245 xpt_setup_ccb(&cgd.ccb_h, 1246 done_ccb->ccb_h.path, 1247 CAM_PRIORITY_NORMAL); 1248 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1249 xpt_action((union ccb *)&cgd); 1250 1251 if (scsi_extract_sense_ccb(done_ccb, 1252 &error_code, &sense_key, &asc, &ascq)) 1253 have_sense = TRUE; 1254 else 1255 have_sense = FALSE; 1256 1257 /* 1258 * Attach to anything that claims to be a 1259 * CDROM or WORM device, as long as it 1260 * doesn't return a "Logical unit not 1261 * supported" (0x25) error. 1262 * 1263 * 25/0: LOGICAL UNIT NOT SUPPORTED 1264 */ 1265 if ((have_sense) && (asc != 0x25) 1266 && (error_code == SSD_CURRENT_ERROR 1267 || error_code == SSD_DESC_CURRENT_ERROR)) { 1268 const char *sense_key_desc; 1269 const char *asc_desc; 1270 1271 scsi_sense_desc(sense_key, asc, ascq, 1272 &cgd.inq_data, 1273 &sense_key_desc, 1274 &asc_desc); 1275 snprintf(announce_buf, 1276 CD_ANNOUNCETMP_SZ, 1277 "Attempt to query device " 1278 "size failed: %s, %s", 1279 sense_key_desc, 1280 asc_desc); 1281 } else if ((have_sense == 0) 1282 && ((status & CAM_STATUS_MASK) == 1283 CAM_SCSI_STATUS_ERROR) 1284 && (csio->scsi_status == 1285 SCSI_STATUS_BUSY)) { 1286 snprintf(announce_buf, 1287 CD_ANNOUNCETMP_SZ, 1288 "Attempt to query device " 1289 "size failed: SCSI Status: %s", 1290 scsi_status_string(csio)); 1291 } else if (SID_TYPE(&cgd.inq_data) == T_CDROM) { 1292 /* 1293 * We only print out an error for 1294 * CDROM type devices. For WORM 1295 * devices, we don't print out an 1296 * error since a few WORM devices 1297 * don't support CDROM commands. 1298 * If we have sense information, go 1299 * ahead and print it out. 1300 * Otherwise, just say that we 1301 * couldn't attach. 1302 */ 1303 1304 /* 1305 * Just print out the error, not 1306 * the full probe message, when we 1307 * don't attach. 1308 */ 1309 if (have_sense) 1310 scsi_sense_print( 1311 &done_ccb->csio); 1312 else { 1313 xpt_print(periph->path, 1314 "got CAM status %#x\n", 1315 done_ccb->ccb_h.status); 1316 } 1317 xpt_print(periph->path, "fatal error, " 1318 "failed to attach to device\n"); 1319 /* 1320 * Invalidate this peripheral. 1321 */ 1322 cam_periph_invalidate(periph); 1323 1324 announce_buf = NULL; 1325 } else { 1326 /* 1327 * Invalidate this peripheral. 1328 */ 1329 cam_periph_invalidate(periph); 1330 announce_buf = NULL; 1331 } 1332 } 1333 } 1334 free(rdcap, M_SCSICD); 1335 if (announce_buf != NULL) { 1336 struct sbuf sb; 1337 1338 sbuf_new(&sb, softc->announce_buf, CD_ANNOUNCE_SZ, 1339 SBUF_FIXEDLEN); 1340 xpt_announce_periph_sbuf(periph, &sb, announce_buf); 1341 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks, 1342 CD_Q_BIT_STRING); 1343 sbuf_finish(&sb); 1344 sbuf_putbuf(&sb); 1345 1346 /* 1347 * Create our sysctl variables, now that we know 1348 * we have successfully attached. 1349 */ 1350 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task); 1351 } 1352 softc->state = CD_STATE_NORMAL; 1353 /* 1354 * Since our peripheral may be invalidated by an error 1355 * above or an external event, we must release our CCB 1356 * before releasing the probe lock on the peripheral. 1357 * The peripheral will only go away once the last lock 1358 * is removed, and we need it around for the CCB release 1359 * operation. 1360 */ 1361 xpt_release_ccb(done_ccb); 1362 1363 /* 1364 * We'll release this reference once GEOM calls us back via 1365 * cddiskgonecb(), telling us that our provider has been freed. 1366 */ 1367 if (cam_periph_acquire(periph) == 0) 1368 disk_create(softc->disk, DISK_VERSION); 1369 1370 cam_periph_release_boot(periph); 1371 cam_periph_release_locked(periph); 1372 return; 1373 } 1374 case CD_CCB_TUR: 1375 { 1376 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1377 if (cderror(done_ccb, CAM_RETRY_SELTO, 1378 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == 1379 ERESTART) 1380 return; 1381 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1382 cam_release_devq(done_ccb->ccb_h.path, 1383 /*relsim_flags*/0, 1384 /*reduction*/0, 1385 /*timeout*/0, 1386 /*getcount_only*/0); 1387 } 1388 xpt_release_ccb(done_ccb); 1389 cam_periph_release_locked(periph); 1390 return; 1391 } 1392 case CD_CCB_MEDIA_ALLOW: 1393 case CD_CCB_MEDIA_PREVENT: 1394 { 1395 int error; 1396 int is_prevent; 1397 1398 error = 0; 1399 1400 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1401 error = cderror(done_ccb, CAM_RETRY_SELTO, 1402 SF_RETRY_UA | SF_NO_PRINT); 1403 } 1404 if (error == ERESTART) 1405 return; 1406 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1407 cam_release_devq(done_ccb->ccb_h.path, 1408 /*relsim_flags*/0, 1409 /*reduction*/0, 1410 /*timeout*/0, 1411 /*getcount_only*/0); 1412 1413 /* 1414 * Note that just like the original cdcheckmedia(), we do 1415 * a prevent without failing the whole operation if the 1416 * prevent fails. We try, but keep going if it doesn't 1417 * work. 1418 */ 1419 1420 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) == 1421 CD_CCB_MEDIA_PREVENT) 1422 is_prevent = 1; 1423 else 1424 is_prevent = 0; 1425 1426 xpt_release_ccb(done_ccb); 1427 1428 if (is_prevent != 0) { 1429 if (error == 0) 1430 softc->flags |= CD_FLAG_DISC_LOCKED; 1431 else 1432 softc->flags &= ~CD_FLAG_DISC_LOCKED; 1433 softc->state = CD_STATE_MEDIA_SIZE; 1434 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1435 } else { 1436 if (error == 0) 1437 softc->flags &= ~CD_FLAG_DISC_LOCKED; 1438 softc->state = CD_STATE_NORMAL; 1439 if (bioq_first(&softc->bio_queue) != NULL) 1440 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1441 } 1442 return; 1443 } 1444 case CD_CCB_MEDIA_SIZE: 1445 { 1446 struct scsi_read_capacity_data *rdcap; 1447 int error; 1448 1449 error = 0; 1450 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1451 error = cderror(done_ccb, CAM_RETRY_SELTO, 1452 SF_RETRY_UA | SF_NO_PRINT); 1453 } 1454 if (error == ERESTART) 1455 return; 1456 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1457 cam_release_devq(done_ccb->ccb_h.path, 1458 /*relsim_flags*/0, 1459 /*reduction*/0, 1460 /*timeout*/0, 1461 /*getcount_only*/0); 1462 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; 1463 1464 if (error == 0) { 1465 softc->params.disksize =scsi_4btoul(rdcap->addr) + 1; 1466 softc->params.blksize = scsi_4btoul(rdcap->length); 1467 1468 /* Make sure we got at least some block size. */ 1469 if (softc->params.blksize == 0) 1470 error = EIO; 1471 /* 1472 * SCSI-3 mandates that the reported blocksize shall be 1473 * 2048. Older drives sometimes report funny values, 1474 * trim it down to 2048, or other parts of the kernel 1475 * will get confused. 1476 * 1477 * XXX we leave drives alone that might report 512 1478 * bytes, as well as drives reporting more weird 1479 * sizes like perhaps 4K. 1480 */ 1481 if (softc->params.blksize > 2048 1482 && softc->params.blksize <= 2352) 1483 softc->params.blksize = 2048; 1484 } 1485 free(rdcap, M_SCSICD); 1486 1487 if (error == 0) { 1488 softc->disk->d_sectorsize = softc->params.blksize; 1489 softc->disk->d_mediasize = 1490 (off_t)softc->params.blksize * 1491 softc->params.disksize; 1492 softc->flags |= CD_FLAG_VALID_MEDIA; 1493 softc->state = CD_STATE_MEDIA_TOC_HDR; 1494 } else { 1495 softc->flags &= ~(CD_FLAG_VALID_MEDIA | 1496 CD_FLAG_VALID_TOC); 1497 bioq_flush(&softc->bio_queue, NULL, EINVAL); 1498 softc->state = CD_STATE_MEDIA_ALLOW; 1499 cdmediaprobedone(periph); 1500 } 1501 xpt_release_ccb(done_ccb); 1502 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1503 return; 1504 } 1505 case CD_CCB_MEDIA_TOC_HDR: 1506 case CD_CCB_MEDIA_TOC_FULL: 1507 case CD_CCB_MEDIA_TOC_LEAD: 1508 { 1509 int error; 1510 struct ioc_toc_header *toch; 1511 int num_entries; 1512 int cdindex; 1513 1514 error = 0; 1515 1516 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1517 error = cderror(done_ccb, CAM_RETRY_SELTO, 1518 SF_RETRY_UA | SF_NO_PRINT); 1519 } 1520 if (error == ERESTART) 1521 return; 1522 1523 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1524 cam_release_devq(done_ccb->ccb_h.path, 1525 /*relsim_flags*/0, 1526 /*reduction*/0, 1527 /*timeout*/0, 1528 /*getcount_only*/0); 1529 1530 /* 1531 * We will get errors here for media that doesn't have a table 1532 * of contents. According to the MMC-3 spec: "When a Read 1533 * TOC/PMA/ATIP command is presented for a DDCD/CD-R/RW media, 1534 * where the first TOC has not been recorded (no complete 1535 * session) and the Format codes 0000b, 0001b, or 0010b are 1536 * specified, this command shall be rejected with an INVALID 1537 * FIELD IN CDB. Devices that are not capable of reading an 1538 * incomplete session on DDC/CD-R/RW media shall report 1539 * CANNOT READ MEDIUM - INCOMPATIBLE FORMAT." 1540 * 1541 * So this isn't fatal if we can't read the table of contents, 1542 * it just means that the user won't be able to issue the 1543 * play tracks ioctl, and likely lots of other stuff won't 1544 * work either. They need to burn the CD before we can do 1545 * a whole lot with it. So we don't print anything here if 1546 * we get an error back. 1547 * 1548 * We also bail out if the drive doesn't at least give us 1549 * the full TOC header. 1550 */ 1551 if ((error != 0) 1552 || ((csio->dxfer_len - csio->resid) < 1553 sizeof(struct ioc_toc_header))) { 1554 softc->flags &= ~CD_FLAG_VALID_TOC; 1555 bzero(&softc->toc, sizeof(softc->toc)); 1556 /* 1557 * Failing the TOC read is not an error. 1558 */ 1559 softc->state = CD_STATE_NORMAL; 1560 xpt_release_ccb(done_ccb); 1561 1562 cdmediaprobedone(periph); 1563 1564 /* 1565 * Go ahead and schedule I/O execution if there is 1566 * anything in the queue. It'll probably get 1567 * kicked out with an error. 1568 */ 1569 if (bioq_first(&softc->bio_queue) != NULL) 1570 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1571 return; 1572 } 1573 1574 /* 1575 * Note that this is NOT the storage location used for the 1576 * leadout! 1577 */ 1578 toch = &softc->toc.header; 1579 1580 if (softc->quirks & CD_Q_BCD_TRACKS) { 1581 toch->starting_track = bcd2bin(toch->starting_track); 1582 toch->ending_track = bcd2bin(toch->ending_track); 1583 } 1584 1585 /* Number of TOC entries, plus leadout */ 1586 num_entries = toch->ending_track - toch->starting_track + 2; 1587 cdindex = toch->starting_track + num_entries - 1; 1588 1589 if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) == 1590 CD_CCB_MEDIA_TOC_HDR) { 1591 if (num_entries <= 0 || 1592 num_entries > nitems(softc->toc.entries)) { 1593 softc->flags &= ~CD_FLAG_VALID_TOC; 1594 bzero(&softc->toc, sizeof(softc->toc)); 1595 /* 1596 * Failing the TOC read is not an error. 1597 */ 1598 softc->state = CD_STATE_NORMAL; 1599 xpt_release_ccb(done_ccb); 1600 1601 cdmediaprobedone(periph); 1602 1603 /* 1604 * Go ahead and schedule I/O execution if 1605 * there is anything in the queue. It'll 1606 * probably get kicked out with an error. 1607 */ 1608 if (bioq_first(&softc->bio_queue) != NULL) 1609 xpt_schedule(periph, 1610 CAM_PRIORITY_NORMAL); 1611 } else { 1612 softc->toc_read_len = num_entries * 1613 sizeof(struct cd_toc_entry); 1614 softc->toc_read_len += sizeof(*toch); 1615 1616 softc->state = CD_STATE_MEDIA_TOC_FULL; 1617 xpt_release_ccb(done_ccb); 1618 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1619 } 1620 1621 return; 1622 } else if ((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) == 1623 CD_CCB_MEDIA_TOC_LEAD) { 1624 struct cd_toc_single *leadout; 1625 1626 leadout = (struct cd_toc_single *)csio->data_ptr; 1627 softc->toc.entries[cdindex - toch->starting_track] = 1628 leadout->entry; 1629 } else if (((done_ccb->ccb_h.ccb_state & CD_CCB_TYPE_MASK) == 1630 CD_CCB_MEDIA_TOC_FULL) 1631 && (cdindex == toch->ending_track + 1)) { 1632 /* 1633 * XXX KDM is this necessary? Probably only if the 1634 * drive doesn't return leadout information with the 1635 * table of contents. 1636 */ 1637 softc->state = CD_STATE_MEDIA_TOC_LEAD; 1638 xpt_release_ccb(done_ccb); 1639 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1640 return; 1641 } 1642 1643 if (softc->quirks & CD_Q_BCD_TRACKS) { 1644 for (cdindex = 0; cdindex < num_entries - 1; cdindex++){ 1645 softc->toc.entries[cdindex].track = 1646 bcd2bin(softc->toc.entries[cdindex].track); 1647 } 1648 } 1649 1650 softc->flags |= CD_FLAG_VALID_TOC; 1651 /* If the first track is audio, correct sector size. */ 1652 if ((softc->toc.entries[0].control & 4) == 0) { 1653 softc->disk->d_sectorsize =softc->params.blksize = 2352; 1654 softc->disk->d_mediasize = 1655 (off_t)softc->params.blksize * 1656 softc->params.disksize; 1657 } 1658 softc->state = CD_STATE_NORMAL; 1659 1660 /* 1661 * We unconditionally (re)set the blocksize each time the 1662 * CD device is opened. This is because the CD can change, 1663 * and therefore the blocksize might change. 1664 * XXX problems here if some slice or partition is still 1665 * open with the old size? 1666 */ 1667 if ((softc->disk->d_devstat->flags & DEVSTAT_BS_UNAVAILABLE)!=0) 1668 softc->disk->d_devstat->flags &= 1669 ~DEVSTAT_BS_UNAVAILABLE; 1670 softc->disk->d_devstat->block_size = softc->params.blksize; 1671 1672 xpt_release_ccb(done_ccb); 1673 1674 cdmediaprobedone(periph); 1675 1676 if (bioq_first(&softc->bio_queue) != NULL) 1677 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1678 return; 1679 } 1680 default: 1681 break; 1682 } 1683 xpt_release_ccb(done_ccb); 1684 } 1685 1686 static union cd_pages * 1687 cdgetpage(struct cd_mode_params *mode_params) 1688 { 1689 union cd_pages *page; 1690 1691 if (mode_params->cdb_size == 10) 1692 page = (union cd_pages *)find_mode_page_10( 1693 (struct scsi_mode_header_10 *)mode_params->mode_buf); 1694 else 1695 page = (union cd_pages *)find_mode_page_6( 1696 (struct scsi_mode_header_6 *)mode_params->mode_buf); 1697 1698 return (page); 1699 } 1700 1701 static int 1702 cdgetpagesize(int page_num) 1703 { 1704 u_int i; 1705 1706 for (i = 0; i < nitems(cd_page_size_table); i++) { 1707 if (cd_page_size_table[i].page == page_num) 1708 return (cd_page_size_table[i].page_size); 1709 } 1710 1711 return (-1); 1712 } 1713 1714 static struct cd_toc_entry * 1715 te_data_get_ptr(void *irtep, u_long cmd) 1716 { 1717 union { 1718 struct ioc_read_toc_entry irte; 1719 #ifdef COMPAT_FREEBSD32 1720 struct ioc_read_toc_entry32 irte32; 1721 #endif 1722 } *irteup; 1723 1724 irteup = irtep; 1725 switch (IOCPARM_LEN(cmd)) { 1726 case sizeof(irteup->irte): 1727 return (irteup->irte.data); 1728 #ifdef COMPAT_FREEBSD32 1729 case sizeof(irteup->irte32): 1730 return ((struct cd_toc_entry *)(uintptr_t)irteup->irte32.data); 1731 #endif 1732 default: 1733 panic("Unhandled ioctl command %ld", cmd); 1734 } 1735 } 1736 1737 static int 1738 cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 1739 { 1740 1741 struct cam_periph *periph; 1742 struct cd_softc *softc; 1743 int error = 0; 1744 1745 periph = (struct cam_periph *)dp->d_drv1; 1746 cam_periph_lock(periph); 1747 1748 softc = (struct cd_softc *)periph->softc; 1749 1750 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 1751 ("cdioctl(%#lx)\n", cmd)); 1752 1753 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) { 1754 cam_periph_unlock(periph); 1755 cam_periph_release(periph); 1756 return (error); 1757 } 1758 1759 /* 1760 * If we don't have media loaded, check for it. If still don't 1761 * have media loaded, we can only do a load or eject. 1762 * 1763 * We only care whether media is loaded if this is a cd-specific ioctl 1764 * (thus the IOCGROUP check below). Note that this will break if 1765 * anyone adds any ioctls into the switch statement below that don't 1766 * have their ioctl group set to 'c'. 1767 */ 1768 if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0) 1769 && ((cmd != CDIOCCLOSE) 1770 && (cmd != CDIOCEJECT)) 1771 && (IOCGROUP(cmd) == 'c')) { 1772 error = cdcheckmedia(periph, /*do_wait*/ true); 1773 if (error != 0) { 1774 cam_periph_unhold(periph); 1775 cam_periph_unlock(periph); 1776 return (error); 1777 } 1778 } 1779 /* 1780 * Drop the lock here so later mallocs can use WAITOK. The periph 1781 * is essentially locked still with the cam_periph_hold call above. 1782 */ 1783 cam_periph_unlock(periph); 1784 1785 switch (cmd) { 1786 case CDIOCPLAYTRACKS: 1787 { 1788 struct ioc_play_track *args 1789 = (struct ioc_play_track *) addr; 1790 struct cd_mode_params params; 1791 union cd_pages *page; 1792 1793 params.alloc_len = sizeof(union cd_mode_data_6_10); 1794 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1795 M_WAITOK | M_ZERO); 1796 1797 cam_periph_lock(periph); 1798 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1799 ("trying to do CDIOCPLAYTRACKS\n")); 1800 1801 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1802 if (error) { 1803 free(params.mode_buf, M_SCSICD); 1804 cam_periph_unlock(periph); 1805 break; 1806 } 1807 page = cdgetpage(¶ms); 1808 1809 page->audio.flags &= ~CD_PA_SOTC; 1810 page->audio.flags |= CD_PA_IMMED; 1811 error = cdsetmode(periph, ¶ms); 1812 free(params.mode_buf, M_SCSICD); 1813 if (error) { 1814 cam_periph_unlock(periph); 1815 break; 1816 } 1817 1818 /* 1819 * This was originally implemented with the PLAY 1820 * AUDIO TRACK INDEX command, but that command was 1821 * deprecated after SCSI-2. Most (all?) SCSI CDROM 1822 * drives support it but ATAPI and ATAPI-derivative 1823 * drives don't seem to support it. So we keep a 1824 * cache of the table of contents and translate 1825 * track numbers to MSF format. 1826 */ 1827 if (softc->flags & CD_FLAG_VALID_TOC) { 1828 union msf_lba *sentry, *eentry; 1829 struct ioc_toc_header *th; 1830 int st, et; 1831 1832 th = &softc->toc.header; 1833 if (args->end_track < th->ending_track + 1) 1834 args->end_track++; 1835 if (args->end_track > th->ending_track + 1) 1836 args->end_track = th->ending_track + 1; 1837 st = args->start_track - th->starting_track; 1838 et = args->end_track - th->starting_track; 1839 if (st < 0 || et < 0 || 1840 st > th->ending_track - th->starting_track || 1841 et > th->ending_track - th->starting_track) { 1842 error = EINVAL; 1843 cam_periph_unlock(periph); 1844 break; 1845 } 1846 sentry = &softc->toc.entries[st].addr; 1847 eentry = &softc->toc.entries[et].addr; 1848 error = cdplaymsf(periph, 1849 sentry->msf.minute, 1850 sentry->msf.second, 1851 sentry->msf.frame, 1852 eentry->msf.minute, 1853 eentry->msf.second, 1854 eentry->msf.frame); 1855 } else { 1856 /* 1857 * If we don't have a valid TOC, try the 1858 * play track index command. It is part of 1859 * the SCSI-2 spec, but was removed in the 1860 * MMC specs. ATAPI and ATAPI-derived 1861 * drives don't support it. 1862 */ 1863 if (softc->quirks & CD_Q_BCD_TRACKS) { 1864 args->start_track = 1865 bin2bcd(args->start_track); 1866 args->end_track = 1867 bin2bcd(args->end_track); 1868 } 1869 error = cdplaytracks(periph, 1870 args->start_track, 1871 args->start_index, 1872 args->end_track, 1873 args->end_index); 1874 } 1875 cam_periph_unlock(periph); 1876 } 1877 break; 1878 case CDIOCPLAYMSF: 1879 { 1880 struct ioc_play_msf *args 1881 = (struct ioc_play_msf *) addr; 1882 struct cd_mode_params params; 1883 union cd_pages *page; 1884 1885 params.alloc_len = sizeof(union cd_mode_data_6_10); 1886 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1887 M_WAITOK | M_ZERO); 1888 1889 cam_periph_lock(periph); 1890 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1891 ("trying to do CDIOCPLAYMSF\n")); 1892 1893 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1894 if (error) { 1895 free(params.mode_buf, M_SCSICD); 1896 cam_periph_unlock(periph); 1897 break; 1898 } 1899 page = cdgetpage(¶ms); 1900 1901 page->audio.flags &= ~CD_PA_SOTC; 1902 page->audio.flags |= CD_PA_IMMED; 1903 error = cdsetmode(periph, ¶ms); 1904 free(params.mode_buf, M_SCSICD); 1905 if (error) { 1906 cam_periph_unlock(periph); 1907 break; 1908 } 1909 error = cdplaymsf(periph, 1910 args->start_m, 1911 args->start_s, 1912 args->start_f, 1913 args->end_m, 1914 args->end_s, 1915 args->end_f); 1916 cam_periph_unlock(periph); 1917 } 1918 break; 1919 case CDIOCPLAYBLOCKS: 1920 { 1921 struct ioc_play_blocks *args 1922 = (struct ioc_play_blocks *) addr; 1923 struct cd_mode_params params; 1924 union cd_pages *page; 1925 1926 params.alloc_len = sizeof(union cd_mode_data_6_10); 1927 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 1928 M_WAITOK | M_ZERO); 1929 1930 cam_periph_lock(periph); 1931 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1932 ("trying to do CDIOCPLAYBLOCKS\n")); 1933 1934 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 1935 if (error) { 1936 free(params.mode_buf, M_SCSICD); 1937 cam_periph_unlock(periph); 1938 break; 1939 } 1940 page = cdgetpage(¶ms); 1941 1942 page->audio.flags &= ~CD_PA_SOTC; 1943 page->audio.flags |= CD_PA_IMMED; 1944 error = cdsetmode(periph, ¶ms); 1945 free(params.mode_buf, M_SCSICD); 1946 if (error) { 1947 cam_periph_unlock(periph); 1948 break; 1949 } 1950 error = cdplay(periph, args->blk, args->len); 1951 cam_periph_unlock(periph); 1952 } 1953 break; 1954 case CDIOCREADSUBCHANNEL: 1955 { 1956 struct ioc_read_subchannel *args 1957 = (struct ioc_read_subchannel *) addr; 1958 struct cd_sub_channel_info *data; 1959 uint32_t len = args->data_len; 1960 1961 data = malloc(sizeof(struct cd_sub_channel_info), 1962 M_SCSICD, M_WAITOK | M_ZERO); 1963 1964 cam_periph_lock(periph); 1965 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 1966 ("trying to do CDIOCREADSUBCHANNEL\n")); 1967 1968 if ((len > sizeof(struct cd_sub_channel_info)) || 1969 (len < sizeof(struct cd_sub_channel_header))) { 1970 printf( 1971 "scsi_cd: cdioctl: " 1972 "cdioreadsubchannel: error, len=%d\n", 1973 len); 1974 error = EINVAL; 1975 free(data, M_SCSICD); 1976 cam_periph_unlock(periph); 1977 break; 1978 } 1979 1980 if (softc->quirks & CD_Q_BCD_TRACKS) 1981 args->track = bin2bcd(args->track); 1982 1983 error = cdreadsubchannel(periph, args->address_format, 1984 args->data_format, args->track, data, len); 1985 1986 if (error) { 1987 free(data, M_SCSICD); 1988 cam_periph_unlock(periph); 1989 break; 1990 } 1991 if (softc->quirks & CD_Q_BCD_TRACKS) 1992 data->what.track_info.track_number = 1993 bcd2bin(data->what.track_info.track_number); 1994 len = min(len, ((data->header.data_len[0] << 8) + 1995 data->header.data_len[1] + 1996 sizeof(struct cd_sub_channel_header))); 1997 cam_periph_unlock(periph); 1998 error = copyout(data, args->data, len); 1999 free(data, M_SCSICD); 2000 } 2001 break; 2002 2003 case CDIOREADTOCHEADER: 2004 { 2005 struct ioc_toc_header *th; 2006 2007 th = malloc(sizeof(struct ioc_toc_header), M_SCSICD, 2008 M_WAITOK | M_ZERO); 2009 2010 cam_periph_lock(periph); 2011 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2012 ("trying to do CDIOREADTOCHEADER\n")); 2013 2014 error = cdreadtoc(periph, 0, 0, (uint8_t *)th, 2015 sizeof (*th), /*sense_flags*/SF_NO_PRINT); 2016 if (error) { 2017 free(th, M_SCSICD); 2018 cam_periph_unlock(periph); 2019 break; 2020 } 2021 if (softc->quirks & CD_Q_BCD_TRACKS) { 2022 /* we are going to have to convert the BCD 2023 * encoding on the cd to what is expected 2024 */ 2025 th->starting_track = 2026 bcd2bin(th->starting_track); 2027 th->ending_track = bcd2bin(th->ending_track); 2028 } 2029 th->len = ntohs(th->len); 2030 bcopy(th, addr, sizeof(*th)); 2031 free(th, M_SCSICD); 2032 cam_periph_unlock(periph); 2033 } 2034 break; 2035 case CDIOREADTOCENTRYS: 2036 #ifdef COMPAT_FREEBSD32 2037 case CDIOREADTOCENTRYS_32: 2038 #endif 2039 { 2040 struct cd_tocdata *data; 2041 struct cd_toc_single *lead; 2042 struct ioc_read_toc_entry *te = 2043 (struct ioc_read_toc_entry *) addr; 2044 struct ioc_toc_header *th; 2045 uint32_t len, readlen, idx, num; 2046 uint32_t starting_track = te->starting_track; 2047 2048 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2049 lead = malloc(sizeof(*lead), M_SCSICD, M_WAITOK | M_ZERO); 2050 2051 cam_periph_lock(periph); 2052 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2053 ("trying to do CDIOREADTOCENTRYS\n")); 2054 2055 if (te->data_len < sizeof(struct cd_toc_entry) 2056 || (te->data_len % sizeof(struct cd_toc_entry)) != 0 2057 || (te->address_format != CD_MSF_FORMAT 2058 && te->address_format != CD_LBA_FORMAT)) { 2059 error = EINVAL; 2060 printf("scsi_cd: error in readtocentries, " 2061 "returning EINVAL\n"); 2062 free(data, M_SCSICD); 2063 free(lead, M_SCSICD); 2064 cam_periph_unlock(periph); 2065 break; 2066 } 2067 2068 th = &data->header; 2069 error = cdreadtoc(periph, 0, 0, (uint8_t *)th, 2070 sizeof (*th), /*sense_flags*/0); 2071 if (error) { 2072 free(data, M_SCSICD); 2073 free(lead, M_SCSICD); 2074 cam_periph_unlock(periph); 2075 break; 2076 } 2077 2078 if (softc->quirks & CD_Q_BCD_TRACKS) { 2079 /* we are going to have to convert the BCD 2080 * encoding on the cd to what is expected 2081 */ 2082 th->starting_track = 2083 bcd2bin(th->starting_track); 2084 th->ending_track = bcd2bin(th->ending_track); 2085 } 2086 2087 if (starting_track == 0) 2088 starting_track = th->starting_track; 2089 else if (starting_track == LEADOUT) 2090 starting_track = th->ending_track + 1; 2091 else if (starting_track < th->starting_track || 2092 starting_track > th->ending_track + 1) { 2093 printf("scsi_cd: error in readtocentries, " 2094 "returning EINVAL\n"); 2095 free(data, M_SCSICD); 2096 free(lead, M_SCSICD); 2097 cam_periph_unlock(periph); 2098 error = EINVAL; 2099 break; 2100 } 2101 2102 /* calculate reading length without leadout entry */ 2103 readlen = (th->ending_track - starting_track + 1) * 2104 sizeof(struct cd_toc_entry); 2105 2106 /* and with leadout entry */ 2107 len = readlen + sizeof(struct cd_toc_entry); 2108 if (te->data_len < len) { 2109 len = te->data_len; 2110 if (readlen > len) 2111 readlen = len; 2112 } 2113 if (len > sizeof(data->entries)) { 2114 printf("scsi_cd: error in readtocentries, " 2115 "returning EINVAL\n"); 2116 error = EINVAL; 2117 free(data, M_SCSICD); 2118 free(lead, M_SCSICD); 2119 cam_periph_unlock(periph); 2120 break; 2121 } 2122 num = len / sizeof(struct cd_toc_entry); 2123 2124 if (readlen > 0) { 2125 error = cdreadtoc(periph, te->address_format, 2126 starting_track, 2127 (uint8_t *)data, 2128 readlen + sizeof (*th), 2129 /*sense_flags*/0); 2130 if (error) { 2131 free(data, M_SCSICD); 2132 free(lead, M_SCSICD); 2133 cam_periph_unlock(periph); 2134 break; 2135 } 2136 } 2137 2138 /* make leadout entry if needed */ 2139 idx = starting_track + num - 1; 2140 if (softc->quirks & CD_Q_BCD_TRACKS) 2141 th->ending_track = bcd2bin(th->ending_track); 2142 if (idx == th->ending_track + 1) { 2143 error = cdreadtoc(periph, te->address_format, 2144 LEADOUT, (uint8_t *)lead, 2145 sizeof(*lead), 2146 /*sense_flags*/0); 2147 if (error) { 2148 free(data, M_SCSICD); 2149 free(lead, M_SCSICD); 2150 cam_periph_unlock(periph); 2151 break; 2152 } 2153 data->entries[idx - starting_track] = 2154 lead->entry; 2155 } 2156 if (softc->quirks & CD_Q_BCD_TRACKS) { 2157 for (idx = 0; idx < num - 1; idx++) { 2158 data->entries[idx].track = 2159 bcd2bin(data->entries[idx].track); 2160 } 2161 } 2162 2163 cam_periph_unlock(periph); 2164 error = copyout(data->entries, te_data_get_ptr(te, cmd), 2165 len); 2166 free(data, M_SCSICD); 2167 free(lead, M_SCSICD); 2168 } 2169 break; 2170 case CDIOREADTOCENTRY: 2171 { 2172 struct cd_toc_single *data; 2173 struct ioc_read_toc_single_entry *te = 2174 (struct ioc_read_toc_single_entry *) addr; 2175 struct ioc_toc_header *th; 2176 uint32_t track; 2177 2178 data = malloc(sizeof(*data), M_SCSICD, M_WAITOK | M_ZERO); 2179 2180 cam_periph_lock(periph); 2181 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2182 ("trying to do CDIOREADTOCENTRY\n")); 2183 2184 if (te->address_format != CD_MSF_FORMAT 2185 && te->address_format != CD_LBA_FORMAT) { 2186 printf("error in readtocentry, " 2187 " returning EINVAL\n"); 2188 free(data, M_SCSICD); 2189 error = EINVAL; 2190 cam_periph_unlock(periph); 2191 break; 2192 } 2193 2194 th = &data->header; 2195 error = cdreadtoc(periph, 0, 0, (uint8_t *)th, 2196 sizeof (*th), /*sense_flags*/0); 2197 if (error) { 2198 free(data, M_SCSICD); 2199 cam_periph_unlock(periph); 2200 break; 2201 } 2202 2203 if (softc->quirks & CD_Q_BCD_TRACKS) { 2204 /* we are going to have to convert the BCD 2205 * encoding on the cd to what is expected 2206 */ 2207 th->starting_track = 2208 bcd2bin(th->starting_track); 2209 th->ending_track = bcd2bin(th->ending_track); 2210 } 2211 track = te->track; 2212 if (track == 0) 2213 track = th->starting_track; 2214 else if (track == LEADOUT) 2215 /* OK */; 2216 else if (track < th->starting_track || 2217 track > th->ending_track + 1) { 2218 printf("error in readtocentry, " 2219 " returning EINVAL\n"); 2220 free(data, M_SCSICD); 2221 error = EINVAL; 2222 cam_periph_unlock(periph); 2223 break; 2224 } 2225 2226 error = cdreadtoc(periph, te->address_format, track, 2227 (uint8_t *)data, sizeof(*data), 2228 /*sense_flags*/0); 2229 if (error) { 2230 free(data, M_SCSICD); 2231 cam_periph_unlock(periph); 2232 break; 2233 } 2234 2235 if (softc->quirks & CD_Q_BCD_TRACKS) 2236 data->entry.track = bcd2bin(data->entry.track); 2237 bcopy(&data->entry, &te->entry, 2238 sizeof(struct cd_toc_entry)); 2239 free(data, M_SCSICD); 2240 cam_periph_unlock(periph); 2241 } 2242 break; 2243 case CDIOCSETPATCH: 2244 { 2245 struct ioc_patch *arg = (struct ioc_patch *)addr; 2246 struct cd_mode_params params; 2247 union cd_pages *page; 2248 2249 params.alloc_len = sizeof(union cd_mode_data_6_10); 2250 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2251 M_WAITOK | M_ZERO); 2252 2253 cam_periph_lock(periph); 2254 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2255 ("trying to do CDIOCSETPATCH\n")); 2256 2257 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2258 if (error) { 2259 free(params.mode_buf, M_SCSICD); 2260 cam_periph_unlock(periph); 2261 break; 2262 } 2263 page = cdgetpage(¶ms); 2264 2265 page->audio.port[LEFT_PORT].channels = 2266 arg->patch[0]; 2267 page->audio.port[RIGHT_PORT].channels = 2268 arg->patch[1]; 2269 page->audio.port[2].channels = arg->patch[2]; 2270 page->audio.port[3].channels = arg->patch[3]; 2271 error = cdsetmode(periph, ¶ms); 2272 free(params.mode_buf, M_SCSICD); 2273 cam_periph_unlock(periph); 2274 } 2275 break; 2276 case CDIOCGETVOL: 2277 { 2278 struct ioc_vol *arg = (struct ioc_vol *) addr; 2279 struct cd_mode_params params; 2280 union cd_pages *page; 2281 2282 params.alloc_len = sizeof(union cd_mode_data_6_10); 2283 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2284 M_WAITOK | M_ZERO); 2285 2286 cam_periph_lock(periph); 2287 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2288 ("trying to do CDIOCGETVOL\n")); 2289 2290 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2291 if (error) { 2292 free(params.mode_buf, M_SCSICD); 2293 cam_periph_unlock(periph); 2294 break; 2295 } 2296 page = cdgetpage(¶ms); 2297 2298 arg->vol[LEFT_PORT] = 2299 page->audio.port[LEFT_PORT].volume; 2300 arg->vol[RIGHT_PORT] = 2301 page->audio.port[RIGHT_PORT].volume; 2302 arg->vol[2] = page->audio.port[2].volume; 2303 arg->vol[3] = page->audio.port[3].volume; 2304 free(params.mode_buf, M_SCSICD); 2305 cam_periph_unlock(periph); 2306 } 2307 break; 2308 case CDIOCSETVOL: 2309 { 2310 struct ioc_vol *arg = (struct ioc_vol *) addr; 2311 struct cd_mode_params params; 2312 union cd_pages *page; 2313 2314 params.alloc_len = sizeof(union cd_mode_data_6_10); 2315 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2316 M_WAITOK | M_ZERO); 2317 2318 cam_periph_lock(periph); 2319 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2320 ("trying to do CDIOCSETVOL\n")); 2321 2322 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2323 if (error) { 2324 free(params.mode_buf, M_SCSICD); 2325 cam_periph_unlock(periph); 2326 break; 2327 } 2328 page = cdgetpage(¶ms); 2329 2330 page->audio.port[LEFT_PORT].channels = CHANNEL_0; 2331 page->audio.port[LEFT_PORT].volume = 2332 arg->vol[LEFT_PORT]; 2333 page->audio.port[RIGHT_PORT].channels = CHANNEL_1; 2334 page->audio.port[RIGHT_PORT].volume = 2335 arg->vol[RIGHT_PORT]; 2336 page->audio.port[2].volume = arg->vol[2]; 2337 page->audio.port[3].volume = arg->vol[3]; 2338 error = cdsetmode(periph, ¶ms); 2339 cam_periph_unlock(periph); 2340 free(params.mode_buf, M_SCSICD); 2341 } 2342 break; 2343 case CDIOCSETMONO: 2344 { 2345 struct cd_mode_params params; 2346 union cd_pages *page; 2347 2348 params.alloc_len = sizeof(union cd_mode_data_6_10); 2349 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2350 M_WAITOK | M_ZERO); 2351 2352 cam_periph_lock(periph); 2353 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2354 ("trying to do CDIOCSETMONO\n")); 2355 2356 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2357 if (error) { 2358 free(params.mode_buf, M_SCSICD); 2359 cam_periph_unlock(periph); 2360 break; 2361 } 2362 page = cdgetpage(¶ms); 2363 2364 page->audio.port[LEFT_PORT].channels = 2365 LEFT_CHANNEL | RIGHT_CHANNEL; 2366 page->audio.port[RIGHT_PORT].channels = 2367 LEFT_CHANNEL | RIGHT_CHANNEL; 2368 page->audio.port[2].channels = 0; 2369 page->audio.port[3].channels = 0; 2370 error = cdsetmode(periph, ¶ms); 2371 cam_periph_unlock(periph); 2372 free(params.mode_buf, M_SCSICD); 2373 } 2374 break; 2375 case CDIOCSETSTEREO: 2376 { 2377 struct cd_mode_params params; 2378 union cd_pages *page; 2379 2380 params.alloc_len = sizeof(union cd_mode_data_6_10); 2381 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2382 M_WAITOK | M_ZERO); 2383 2384 cam_periph_lock(periph); 2385 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2386 ("trying to do CDIOCSETSTEREO\n")); 2387 2388 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2389 if (error) { 2390 free(params.mode_buf, M_SCSICD); 2391 cam_periph_unlock(periph); 2392 break; 2393 } 2394 page = cdgetpage(¶ms); 2395 2396 page->audio.port[LEFT_PORT].channels = 2397 LEFT_CHANNEL; 2398 page->audio.port[RIGHT_PORT].channels = 2399 RIGHT_CHANNEL; 2400 page->audio.port[2].channels = 0; 2401 page->audio.port[3].channels = 0; 2402 error = cdsetmode(periph, ¶ms); 2403 free(params.mode_buf, M_SCSICD); 2404 cam_periph_unlock(periph); 2405 } 2406 break; 2407 case CDIOCSETMUTE: 2408 { 2409 struct cd_mode_params params; 2410 union cd_pages *page; 2411 2412 params.alloc_len = sizeof(union cd_mode_data_6_10); 2413 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2414 M_WAITOK | M_ZERO); 2415 2416 cam_periph_lock(periph); 2417 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2418 ("trying to do CDIOCSETMUTE\n")); 2419 2420 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2421 if (error) { 2422 free(params.mode_buf, M_SCSICD); 2423 cam_periph_unlock(periph); 2424 break; 2425 } 2426 page = cdgetpage(¶ms); 2427 2428 page->audio.port[LEFT_PORT].channels = 0; 2429 page->audio.port[RIGHT_PORT].channels = 0; 2430 page->audio.port[2].channels = 0; 2431 page->audio.port[3].channels = 0; 2432 error = cdsetmode(periph, ¶ms); 2433 free(params.mode_buf, M_SCSICD); 2434 cam_periph_unlock(periph); 2435 } 2436 break; 2437 case CDIOCSETLEFT: 2438 { 2439 struct cd_mode_params params; 2440 union cd_pages *page; 2441 2442 params.alloc_len = sizeof(union cd_mode_data_6_10); 2443 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2444 M_WAITOK | M_ZERO); 2445 2446 cam_periph_lock(periph); 2447 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2448 ("trying to do CDIOCSETLEFT\n")); 2449 2450 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2451 if (error) { 2452 free(params.mode_buf, M_SCSICD); 2453 cam_periph_unlock(periph); 2454 break; 2455 } 2456 page = cdgetpage(¶ms); 2457 2458 page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL; 2459 page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL; 2460 page->audio.port[2].channels = 0; 2461 page->audio.port[3].channels = 0; 2462 error = cdsetmode(periph, ¶ms); 2463 free(params.mode_buf, M_SCSICD); 2464 cam_periph_unlock(periph); 2465 } 2466 break; 2467 case CDIOCSETRIGHT: 2468 { 2469 struct cd_mode_params params; 2470 union cd_pages *page; 2471 2472 params.alloc_len = sizeof(union cd_mode_data_6_10); 2473 params.mode_buf = malloc(params.alloc_len, M_SCSICD, 2474 M_WAITOK | M_ZERO); 2475 2476 cam_periph_lock(periph); 2477 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 2478 ("trying to do CDIOCSETRIGHT\n")); 2479 2480 error = cdgetmode(periph, ¶ms, AUDIO_PAGE); 2481 if (error) { 2482 free(params.mode_buf, M_SCSICD); 2483 cam_periph_unlock(periph); 2484 break; 2485 } 2486 page = cdgetpage(¶ms); 2487 2488 page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL; 2489 page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL; 2490 page->audio.port[2].channels = 0; 2491 page->audio.port[3].channels = 0; 2492 error = cdsetmode(periph, ¶ms); 2493 free(params.mode_buf, M_SCSICD); 2494 cam_periph_unlock(periph); 2495 } 2496 break; 2497 case CDIOCRESUME: 2498 cam_periph_lock(periph); 2499 error = cdpause(periph, 1); 2500 cam_periph_unlock(periph); 2501 break; 2502 case CDIOCPAUSE: 2503 cam_periph_lock(periph); 2504 error = cdpause(periph, 0); 2505 cam_periph_unlock(periph); 2506 break; 2507 case CDIOCSTART: 2508 cam_periph_lock(periph); 2509 error = cdstartunit(periph, 0); 2510 cam_periph_unlock(periph); 2511 break; 2512 case CDIOCCLOSE: 2513 cam_periph_lock(periph); 2514 error = cdstartunit(periph, 1); 2515 cam_periph_unlock(periph); 2516 break; 2517 case CDIOCSTOP: 2518 cam_periph_lock(periph); 2519 error = cdstopunit(periph, 0); 2520 cam_periph_unlock(periph); 2521 break; 2522 case CDIOCEJECT: 2523 cam_periph_lock(periph); 2524 error = cdstopunit(periph, 1); 2525 cam_periph_unlock(periph); 2526 break; 2527 case CDIOCALLOW: 2528 cam_periph_lock(periph); 2529 cdprevent(periph, PR_ALLOW); 2530 cam_periph_unlock(periph); 2531 break; 2532 case CDIOCPREVENT: 2533 cam_periph_lock(periph); 2534 cdprevent(periph, PR_PREVENT); 2535 cam_periph_unlock(periph); 2536 break; 2537 case CDIOCSETDEBUG: 2538 /* sc_link->flags |= (SDEV_DB1 | SDEV_DB2); */ 2539 error = ENOTTY; 2540 break; 2541 case CDIOCCLRDEBUG: 2542 /* sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2); */ 2543 error = ENOTTY; 2544 break; 2545 case CDIOCRESET: 2546 /* return (cd_reset(periph)); */ 2547 error = ENOTTY; 2548 break; 2549 case CDRIOCREADSPEED: 2550 cam_periph_lock(periph); 2551 error = cdsetspeed(periph, *(uint32_t *)addr, CDR_MAX_SPEED); 2552 cam_periph_unlock(periph); 2553 break; 2554 case CDRIOCWRITESPEED: 2555 cam_periph_lock(periph); 2556 error = cdsetspeed(periph, CDR_MAX_SPEED, *(uint32_t *)addr); 2557 cam_periph_unlock(periph); 2558 break; 2559 case CDRIOCGETBLOCKSIZE: 2560 *(int *)addr = softc->params.blksize; 2561 break; 2562 case CDRIOCSETBLOCKSIZE: 2563 if (*(int *)addr <= 0) { 2564 error = EINVAL; 2565 break; 2566 } 2567 softc->disk->d_sectorsize = softc->params.blksize = *(int *)addr; 2568 break; 2569 case DVDIOCSENDKEY: 2570 case DVDIOCREPORTKEY: { 2571 struct dvd_authinfo *authinfo; 2572 2573 authinfo = (struct dvd_authinfo *)addr; 2574 2575 if (cmd == DVDIOCREPORTKEY) 2576 error = cdreportkey(periph, authinfo); 2577 else 2578 error = cdsendkey(periph, authinfo); 2579 break; 2580 } 2581 case DVDIOCREADSTRUCTURE: { 2582 struct dvd_struct *dvdstruct; 2583 2584 dvdstruct = (struct dvd_struct *)addr; 2585 2586 error = cdreaddvdstructure(periph, dvdstruct); 2587 2588 break; 2589 } 2590 default: 2591 cam_periph_lock(periph); 2592 error = cam_periph_ioctl(periph, cmd, addr, cderror); 2593 cam_periph_unlock(periph); 2594 break; 2595 } 2596 2597 cam_periph_lock(periph); 2598 cam_periph_unhold(periph); 2599 2600 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n")); 2601 if (error && bootverbose) { 2602 printf("scsi_cd.c::ioctl cmd=%08lx error=%d\n", cmd, error); 2603 } 2604 cam_periph_unlock(periph); 2605 2606 return (error); 2607 } 2608 2609 static void 2610 cdprevent(struct cam_periph *periph, int action) 2611 { 2612 union ccb *ccb; 2613 struct cd_softc *softc; 2614 int error; 2615 2616 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n")); 2617 2618 cam_periph_assert(periph, MA_OWNED); 2619 softc = (struct cd_softc *)periph->softc; 2620 2621 if (((action == PR_ALLOW) 2622 && (softc->flags & CD_FLAG_DISC_LOCKED) == 0) 2623 || ((action == PR_PREVENT) 2624 && (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) { 2625 return; 2626 } 2627 2628 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2629 2630 scsi_prevent(&ccb->csio, 2631 /*retries*/ cd_retry_count, 2632 /*cbfcnp*/NULL, 2633 MSG_SIMPLE_Q_TAG, 2634 action, 2635 SSD_FULL_SIZE, 2636 /* timeout */60000); 2637 2638 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2639 /*sense_flags*/SF_RETRY_UA|SF_NO_PRINT); 2640 2641 xpt_release_ccb(ccb); 2642 2643 if (error == 0) { 2644 if (action == PR_ALLOW) 2645 softc->flags &= ~CD_FLAG_DISC_LOCKED; 2646 else 2647 softc->flags |= CD_FLAG_DISC_LOCKED; 2648 } 2649 } 2650 2651 static void 2652 cdmediaprobedone(struct cam_periph *periph) 2653 { 2654 struct cd_softc *softc; 2655 2656 cam_periph_assert(periph, MA_OWNED); 2657 softc = (struct cd_softc *)periph->softc; 2658 2659 softc->flags &= ~CD_FLAG_MEDIA_SCAN_ACT; 2660 2661 if ((softc->flags & CD_FLAG_MEDIA_WAIT) != 0) { 2662 softc->flags &= ~CD_FLAG_MEDIA_WAIT; 2663 wakeup(&softc->toc); 2664 } 2665 cam_periph_release_locked(periph); 2666 } 2667 2668 /* 2669 * XXX: the disk media and sector size is only really able to change 2670 * XXX: while the device is closed. 2671 */ 2672 2673 static int 2674 cdcheckmedia(struct cam_periph *periph, bool do_wait) 2675 { 2676 struct cd_softc *softc; 2677 int error; 2678 2679 cam_periph_assert(periph, MA_OWNED); 2680 softc = (struct cd_softc *)periph->softc; 2681 error = 0; 2682 2683 /* Released by cdmediaprobedone(). */ 2684 error = cam_periph_acquire(periph); 2685 if (error != 0) 2686 return (error); 2687 2688 if (do_wait) 2689 softc->flags |= CD_FLAG_MEDIA_WAIT; 2690 if ((softc->flags & CD_FLAG_MEDIA_SCAN_ACT) == 0) { 2691 softc->state = CD_STATE_MEDIA_PREVENT; 2692 softc->flags |= CD_FLAG_MEDIA_SCAN_ACT; 2693 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 2694 } 2695 if (!do_wait) 2696 return (0); 2697 2698 error = msleep(&softc->toc, cam_periph_mtx(periph), PRIBIO,"cdmedia",0); 2699 2700 /* 2701 * Check to see whether we have a valid size from the media. We 2702 * may or may not have a valid TOC. 2703 */ 2704 if (error == 0 && (softc->flags & CD_FLAG_VALID_MEDIA) == 0) 2705 error = EINVAL; 2706 2707 return (error); 2708 } 2709 2710 static int 2711 cd6byteworkaround(union ccb *ccb) 2712 { 2713 uint8_t *cdb; 2714 struct cam_periph *periph; 2715 struct cd_softc *softc; 2716 struct cd_mode_params *params; 2717 int frozen, found; 2718 2719 periph = xpt_path_periph(ccb->ccb_h.path); 2720 softc = (struct cd_softc *)periph->softc; 2721 2722 cdb = ccb->csio.cdb_io.cdb_bytes; 2723 2724 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) 2725 || ((cdb[0] != MODE_SENSE_6) 2726 && (cdb[0] != MODE_SELECT_6))) 2727 return (0); 2728 2729 /* 2730 * Because there is no convenient place to stash the overall 2731 * cd_mode_params structure pointer, we have to grab it like this. 2732 * This means that ALL MODE_SENSE and MODE_SELECT requests in the 2733 * cd(4) driver MUST go through cdgetmode() and cdsetmode()! 2734 * 2735 * XXX It would be nice if, at some point, we could increase the 2736 * number of available peripheral private pointers. Both pointers 2737 * are currently used in most every peripheral driver. 2738 */ 2739 found = 0; 2740 2741 STAILQ_FOREACH(params, &softc->mode_queue, links) { 2742 if (params->mode_buf == ccb->csio.data_ptr) { 2743 found = 1; 2744 break; 2745 } 2746 } 2747 2748 /* 2749 * This shouldn't happen. All mode sense and mode select 2750 * operations in the cd(4) driver MUST go through cdgetmode() and 2751 * cdsetmode()! 2752 */ 2753 if (found == 0) { 2754 xpt_print(periph->path, 2755 "mode buffer not found in mode queue!\n"); 2756 return (0); 2757 } 2758 2759 params->cdb_size = 10; 2760 softc->minimum_command_size = 10; 2761 xpt_print(ccb->ccb_h.path, 2762 "%s(6) failed, increasing minimum CDB size to 10 bytes\n", 2763 (cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT"); 2764 2765 if (cdb[0] == MODE_SENSE_6) { 2766 struct scsi_mode_sense_10 ms10; 2767 struct scsi_mode_sense_6 *ms6; 2768 int len; 2769 2770 ms6 = (struct scsi_mode_sense_6 *)cdb; 2771 2772 bzero(&ms10, sizeof(ms10)); 2773 ms10.opcode = MODE_SENSE_10; 2774 ms10.byte2 = ms6->byte2; 2775 ms10.page = ms6->page; 2776 2777 /* 2778 * 10 byte mode header, block descriptor, 2779 * sizeof(union cd_pages) 2780 */ 2781 len = sizeof(struct cd_mode_data_10); 2782 ccb->csio.dxfer_len = len; 2783 2784 scsi_ulto2b(len, ms10.length); 2785 ms10.control = ms6->control; 2786 bcopy(&ms10, cdb, 10); 2787 ccb->csio.cdb_len = 10; 2788 } else { 2789 struct scsi_mode_select_10 ms10; 2790 struct scsi_mode_select_6 *ms6; 2791 struct scsi_mode_header_6 *header6; 2792 struct scsi_mode_header_10 *header10; 2793 struct scsi_mode_page_header *page_header; 2794 int blk_desc_len, page_num, page_size, len; 2795 2796 ms6 = (struct scsi_mode_select_6 *)cdb; 2797 2798 bzero(&ms10, sizeof(ms10)); 2799 ms10.opcode = MODE_SELECT_10; 2800 ms10.byte2 = ms6->byte2; 2801 2802 header6 = (struct scsi_mode_header_6 *)params->mode_buf; 2803 header10 = (struct scsi_mode_header_10 *)params->mode_buf; 2804 2805 page_header = find_mode_page_6(header6); 2806 page_num = page_header->page_code; 2807 2808 blk_desc_len = header6->blk_desc_len; 2809 2810 page_size = cdgetpagesize(page_num); 2811 2812 if (page_size != (page_header->page_length + 2813 sizeof(*page_header))) 2814 page_size = page_header->page_length + 2815 sizeof(*page_header); 2816 2817 len = sizeof(*header10) + blk_desc_len + page_size; 2818 2819 len = min(params->alloc_len, len); 2820 2821 /* 2822 * Since the 6 byte parameter header is shorter than the 10 2823 * byte parameter header, we need to copy the actual mode 2824 * page data, and the block descriptor, if any, so things wind 2825 * up in the right place. The regions will overlap, but 2826 * bcopy() does the right thing. 2827 */ 2828 bcopy(params->mode_buf + sizeof(*header6), 2829 params->mode_buf + sizeof(*header10), 2830 len - sizeof(*header10)); 2831 2832 /* Make sure these fields are set correctly. */ 2833 scsi_ulto2b(0, header10->data_length); 2834 header10->medium_type = 0; 2835 scsi_ulto2b(blk_desc_len, header10->blk_desc_len); 2836 2837 ccb->csio.dxfer_len = len; 2838 2839 scsi_ulto2b(len, ms10.length); 2840 ms10.control = ms6->control; 2841 bcopy(&ms10, cdb, 10); 2842 ccb->csio.cdb_len = 10; 2843 } 2844 2845 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 2846 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2847 xpt_action(ccb); 2848 if (frozen) { 2849 cam_release_devq(ccb->ccb_h.path, 2850 /*relsim_flags*/0, 2851 /*openings*/0, 2852 /*timeout*/0, 2853 /*getcount_only*/0); 2854 } 2855 2856 return (ERESTART); 2857 } 2858 2859 static int 2860 cderror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags) 2861 { 2862 struct cd_softc *softc; 2863 struct cam_periph *periph; 2864 int error, error_code, sense_key, asc, ascq; 2865 2866 periph = xpt_path_periph(ccb->ccb_h.path); 2867 softc = (struct cd_softc *)periph->softc; 2868 2869 cam_periph_assert(periph, MA_OWNED); 2870 2871 /* 2872 * We use a status of CAM_REQ_INVALID as shorthand -- if a 6 byte 2873 * CDB comes back with this particular error, try transforming it 2874 * into the 10 byte version. 2875 */ 2876 error = 0; 2877 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { 2878 error = cd6byteworkaround(ccb); 2879 } else if (scsi_extract_sense_ccb(ccb, 2880 &error_code, &sense_key, &asc, &ascq)) { 2881 if (sense_key == SSD_KEY_ILLEGAL_REQUEST) { 2882 error = cd6byteworkaround(ccb); 2883 } else if (sense_key == SSD_KEY_UNIT_ATTENTION && 2884 asc == 0x28 && ascq == 0x00) { 2885 /* 28/0: NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 2886 disk_media_changed(softc->disk, M_NOWAIT); 2887 } else if (sense_key == SSD_KEY_NOT_READY && 2888 asc == 0x3a && (softc->flags & CD_FLAG_VALID_MEDIA)) { 2889 /* 3a/0: MEDIUM NOT PRESENT */ 2890 /* 3a/1: MEDIUM NOT PRESENT - TRAY CLOSED */ 2891 /* 3a/2: MEDIUM NOT PRESENT - TRAY OPEN */ 2892 /* 3a/3: MEDIUM NOT PRESENT - LOADABLE */ 2893 /* 3a/4: MEDIUM NOT PRESENT - MEDIUM AUXILIARY MEMORY ACCESSIBLE */ 2894 softc->flags &= ~CD_FLAG_VALID_MEDIA; 2895 disk_media_gone(softc->disk, M_NOWAIT); 2896 } 2897 } 2898 2899 if (error == ERESTART) 2900 return (error); 2901 2902 /* 2903 * XXX 2904 * Until we have a better way of doing pack validation, 2905 * don't treat UAs as errors. 2906 */ 2907 sense_flags |= SF_RETRY_UA; 2908 2909 if (softc->quirks & CD_Q_RETRY_BUSY) 2910 sense_flags |= SF_RETRY_BUSY; 2911 return (cam_periph_error(ccb, cam_flags, sense_flags)); 2912 } 2913 2914 static void 2915 cdmediapoll(void *arg) 2916 { 2917 struct cam_periph *periph = arg; 2918 struct cd_softc *softc = periph->softc; 2919 2920 if (softc->state == CD_STATE_NORMAL && !softc->tur && 2921 softc->outstanding_cmds == 0) { 2922 if (cam_periph_acquire(periph) == 0) { 2923 softc->tur = 1; 2924 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 2925 } 2926 } 2927 2928 /* Queue us up again */ 2929 if (cd_poll_period != 0) { 2930 callout_schedule_sbt(&softc->mediapoll_c, 2931 cd_poll_period * SBT_1S, 0, C_PREL(1)); 2932 } 2933 } 2934 2935 /* 2936 * Read table of contents 2937 */ 2938 static int 2939 cdreadtoc(struct cam_periph *periph, uint32_t mode, uint32_t start, 2940 uint8_t *data, uint32_t len, uint32_t sense_flags) 2941 { 2942 struct ccb_scsiio *csio; 2943 union ccb *ccb; 2944 int error; 2945 2946 error = 0; 2947 2948 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2949 2950 csio = &ccb->csio; 2951 2952 scsi_read_toc(csio, 2953 /* retries */ cd_retry_count, 2954 /* cbfcnp */ NULL, 2955 /* tag_action */ MSG_SIMPLE_Q_TAG, 2956 /* byte1_flags */ (mode == CD_MSF_FORMAT) ? CD_MSF : 0, 2957 /* format */ SRTOC_FORMAT_TOC, 2958 /* track*/ start, 2959 /* data_ptr */ data, 2960 /* dxfer_len */ len, 2961 /* sense_len */ SSD_FULL_SIZE, 2962 /* timeout */ 50000); 2963 2964 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 2965 /*sense_flags*/SF_RETRY_UA | sense_flags); 2966 2967 xpt_release_ccb(ccb); 2968 2969 return(error); 2970 } 2971 2972 static int 2973 cdreadsubchannel(struct cam_periph *periph, uint32_t mode, 2974 uint32_t format, int track, 2975 struct cd_sub_channel_info *data, uint32_t len) 2976 { 2977 struct scsi_read_subchannel *scsi_cmd; 2978 struct ccb_scsiio *csio; 2979 union ccb *ccb; 2980 int error; 2981 2982 error = 0; 2983 2984 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 2985 2986 csio = &ccb->csio; 2987 2988 cam_fill_csio(csio, 2989 /* retries */ cd_retry_count, 2990 /* cbfcnp */ NULL, 2991 /* flags */ CAM_DIR_IN, 2992 /* tag_action */ MSG_SIMPLE_Q_TAG, 2993 /* data_ptr */ (uint8_t *)data, 2994 /* dxfer_len */ len, 2995 /* sense_len */ SSD_FULL_SIZE, 2996 sizeof(struct scsi_read_subchannel), 2997 /* timeout */ 50000); 2998 2999 scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes; 3000 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3001 3002 scsi_cmd->op_code = READ_SUBCHANNEL; 3003 if (mode == CD_MSF_FORMAT) 3004 scsi_cmd->byte1 |= CD_MSF; 3005 scsi_cmd->byte2 = SRS_SUBQ; 3006 scsi_cmd->subchan_format = format; 3007 scsi_cmd->track = track; 3008 scsi_ulto2b(len, (uint8_t *)scsi_cmd->data_len); 3009 scsi_cmd->control = 0; 3010 3011 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3012 /*sense_flags*/SF_RETRY_UA); 3013 3014 xpt_release_ccb(ccb); 3015 3016 return(error); 3017 } 3018 3019 /* 3020 * All MODE_SENSE requests in the cd(4) driver MUST go through this 3021 * routine. See comments in cd6byteworkaround() for details. 3022 */ 3023 static int 3024 cdgetmode(struct cam_periph *periph, struct cd_mode_params *data, 3025 uint32_t page) 3026 { 3027 struct ccb_scsiio *csio; 3028 struct cd_softc *softc; 3029 union ccb *ccb; 3030 int param_len; 3031 int error; 3032 3033 softc = (struct cd_softc *)periph->softc; 3034 3035 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3036 3037 csio = &ccb->csio; 3038 3039 data->cdb_size = softc->minimum_command_size; 3040 if (data->cdb_size < 10) 3041 param_len = sizeof(struct cd_mode_data); 3042 else 3043 param_len = sizeof(struct cd_mode_data_10); 3044 3045 /* Don't say we've got more room than we actually allocated */ 3046 param_len = min(param_len, data->alloc_len); 3047 3048 scsi_mode_sense_len(csio, 3049 /* retries */ cd_retry_count, 3050 /* cbfcnp */ NULL, 3051 /* tag_action */ MSG_SIMPLE_Q_TAG, 3052 /* dbd */ 0, 3053 /* page_code */ SMS_PAGE_CTRL_CURRENT, 3054 /* page */ page, 3055 /* param_buf */ data->mode_buf, 3056 /* param_len */ param_len, 3057 /* minimum_cmd_size */ softc->minimum_command_size, 3058 /* sense_len */ SSD_FULL_SIZE, 3059 /* timeout */ 50000); 3060 3061 /* 3062 * It would be nice not to have to do this, but there's no 3063 * available pointer in the CCB that would allow us to stuff the 3064 * mode params structure in there and retrieve it in 3065 * cd6byteworkaround(), so we can set the cdb size. The cdb size 3066 * lets the caller know what CDB size we ended up using, so they 3067 * can find the actual mode page offset. 3068 */ 3069 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3070 3071 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3072 /*sense_flags*/SF_RETRY_UA); 3073 3074 xpt_release_ccb(ccb); 3075 3076 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3077 3078 /* 3079 * This is a bit of belt-and-suspenders checking, but if we run 3080 * into a situation where the target sends back multiple block 3081 * descriptors, we might not have enough space in the buffer to 3082 * see the whole mode page. Better to return an error than 3083 * potentially access memory beyond our malloced region. 3084 */ 3085 if (error == 0) { 3086 uint32_t data_len; 3087 3088 if (data->cdb_size == 10) { 3089 struct scsi_mode_header_10 *hdr10; 3090 3091 hdr10 = (struct scsi_mode_header_10 *)data->mode_buf; 3092 data_len = scsi_2btoul(hdr10->data_length); 3093 data_len += sizeof(hdr10->data_length); 3094 } else { 3095 struct scsi_mode_header_6 *hdr6; 3096 3097 hdr6 = (struct scsi_mode_header_6 *)data->mode_buf; 3098 data_len = hdr6->data_length; 3099 data_len += sizeof(hdr6->data_length); 3100 } 3101 3102 /* 3103 * Complain if there is more mode data available than we 3104 * allocated space for. This could potentially happen if 3105 * we miscalculated the page length for some reason, if the 3106 * drive returns multiple block descriptors, or if it sets 3107 * the data length incorrectly. 3108 */ 3109 if (data_len > data->alloc_len) { 3110 xpt_print(periph->path, "allocated modepage %d length " 3111 "%d < returned length %d\n", page, data->alloc_len, 3112 data_len); 3113 error = ENOSPC; 3114 } 3115 } 3116 return (error); 3117 } 3118 3119 /* 3120 * All MODE_SELECT requests in the cd(4) driver MUST go through this 3121 * routine. See comments in cd6byteworkaround() for details. 3122 */ 3123 static int 3124 cdsetmode(struct cam_periph *periph, struct cd_mode_params *data) 3125 { 3126 struct ccb_scsiio *csio; 3127 struct cd_softc *softc; 3128 union ccb *ccb; 3129 int cdb_size, param_len; 3130 int error; 3131 3132 softc = (struct cd_softc *)periph->softc; 3133 3134 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3135 3136 csio = &ccb->csio; 3137 3138 error = 0; 3139 3140 /* 3141 * If the data is formatted for the 10 byte version of the mode 3142 * select parameter list, we need to use the 10 byte CDB. 3143 * Otherwise, we use whatever the stored minimum command size. 3144 */ 3145 if (data->cdb_size == 10) 3146 cdb_size = data->cdb_size; 3147 else 3148 cdb_size = softc->minimum_command_size; 3149 3150 if (cdb_size >= 10) { 3151 struct scsi_mode_header_10 *mode_header; 3152 uint32_t data_len; 3153 3154 mode_header = (struct scsi_mode_header_10 *)data->mode_buf; 3155 3156 data_len = scsi_2btoul(mode_header->data_length); 3157 3158 scsi_ulto2b(0, mode_header->data_length); 3159 /* 3160 * SONY drives do not allow a mode select with a medium_type 3161 * value that has just been returned by a mode sense; use a 3162 * medium_type of 0 (Default) instead. 3163 */ 3164 mode_header->medium_type = 0; 3165 3166 /* 3167 * Pass back whatever the drive passed to us, plus the size 3168 * of the data length field. 3169 */ 3170 param_len = data_len + sizeof(mode_header->data_length); 3171 3172 } else { 3173 struct scsi_mode_header_6 *mode_header; 3174 3175 mode_header = (struct scsi_mode_header_6 *)data->mode_buf; 3176 3177 param_len = mode_header->data_length + 1; 3178 3179 mode_header->data_length = 0; 3180 /* 3181 * SONY drives do not allow a mode select with a medium_type 3182 * value that has just been returned by a mode sense; use a 3183 * medium_type of 0 (Default) instead. 3184 */ 3185 mode_header->medium_type = 0; 3186 } 3187 3188 /* Don't say we've got more room than we actually allocated */ 3189 param_len = min(param_len, data->alloc_len); 3190 3191 scsi_mode_select_len(csio, 3192 /* retries */ cd_retry_count, 3193 /* cbfcnp */ NULL, 3194 /* tag_action */ MSG_SIMPLE_Q_TAG, 3195 /* scsi_page_fmt */ 1, 3196 /* save_pages */ 0, 3197 /* param_buf */ data->mode_buf, 3198 /* param_len */ param_len, 3199 /* minimum_cmd_size */ cdb_size, 3200 /* sense_len */ SSD_FULL_SIZE, 3201 /* timeout */ 50000); 3202 3203 /* See comments in cdgetmode() and cd6byteworkaround(). */ 3204 STAILQ_INSERT_TAIL(&softc->mode_queue, data, links); 3205 3206 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3207 /*sense_flags*/SF_RETRY_UA); 3208 3209 xpt_release_ccb(ccb); 3210 3211 STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links); 3212 3213 return (error); 3214 } 3215 3216 static int 3217 cdplay(struct cam_periph *periph, uint32_t blk, uint32_t len) 3218 { 3219 struct ccb_scsiio *csio; 3220 union ccb *ccb; 3221 int error; 3222 uint8_t cdb_len; 3223 3224 error = 0; 3225 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3226 csio = &ccb->csio; 3227 /* 3228 * Use the smallest possible command to perform the operation. 3229 */ 3230 if ((len & 0xffff0000) == 0) { 3231 /* 3232 * We can fit in a 10 byte cdb. 3233 */ 3234 struct scsi_play_10 *scsi_cmd; 3235 3236 scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes; 3237 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3238 scsi_cmd->op_code = PLAY_10; 3239 scsi_ulto4b(blk, (uint8_t *)scsi_cmd->blk_addr); 3240 scsi_ulto2b(len, (uint8_t *)scsi_cmd->xfer_len); 3241 cdb_len = sizeof(*scsi_cmd); 3242 } else { 3243 struct scsi_play_12 *scsi_cmd; 3244 3245 scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes; 3246 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3247 scsi_cmd->op_code = PLAY_12; 3248 scsi_ulto4b(blk, (uint8_t *)scsi_cmd->blk_addr); 3249 scsi_ulto4b(len, (uint8_t *)scsi_cmd->xfer_len); 3250 cdb_len = sizeof(*scsi_cmd); 3251 } 3252 cam_fill_csio(csio, 3253 /*retries*/ cd_retry_count, 3254 /*cbfcnp*/NULL, 3255 /*flags*/CAM_DIR_NONE, 3256 MSG_SIMPLE_Q_TAG, 3257 /*dataptr*/NULL, 3258 /*datalen*/0, 3259 /*sense_len*/SSD_FULL_SIZE, 3260 cdb_len, 3261 /*timeout*/50 * 1000); 3262 3263 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3264 /*sense_flags*/SF_RETRY_UA); 3265 3266 xpt_release_ccb(ccb); 3267 3268 return(error); 3269 } 3270 3271 static int 3272 cdplaymsf(struct cam_periph *periph, uint32_t startm, uint32_t starts, 3273 uint32_t startf, uint32_t endm, uint32_t ends, uint32_t endf) 3274 { 3275 struct scsi_play_msf *scsi_cmd; 3276 struct ccb_scsiio *csio; 3277 union ccb *ccb; 3278 int error; 3279 3280 error = 0; 3281 3282 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3283 3284 csio = &ccb->csio; 3285 3286 cam_fill_csio(csio, 3287 /* retries */ cd_retry_count, 3288 /* cbfcnp */ NULL, 3289 /* flags */ CAM_DIR_NONE, 3290 /* tag_action */ MSG_SIMPLE_Q_TAG, 3291 /* data_ptr */ NULL, 3292 /* dxfer_len */ 0, 3293 /* sense_len */ SSD_FULL_SIZE, 3294 sizeof(struct scsi_play_msf), 3295 /* timeout */ 50000); 3296 3297 scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes; 3298 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3299 3300 scsi_cmd->op_code = PLAY_MSF; 3301 scsi_cmd->start_m = startm; 3302 scsi_cmd->start_s = starts; 3303 scsi_cmd->start_f = startf; 3304 scsi_cmd->end_m = endm; 3305 scsi_cmd->end_s = ends; 3306 scsi_cmd->end_f = endf; 3307 3308 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3309 /*sense_flags*/SF_RETRY_UA); 3310 3311 xpt_release_ccb(ccb); 3312 3313 return(error); 3314 } 3315 3316 static int 3317 cdplaytracks(struct cam_periph *periph, uint32_t strack, uint32_t sindex, 3318 uint32_t etrack, uint32_t eindex) 3319 { 3320 struct scsi_play_track *scsi_cmd; 3321 struct ccb_scsiio *csio; 3322 union ccb *ccb; 3323 int error; 3324 3325 error = 0; 3326 3327 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3328 3329 csio = &ccb->csio; 3330 3331 cam_fill_csio(csio, 3332 /* retries */ cd_retry_count, 3333 /* cbfcnp */ NULL, 3334 /* flags */ CAM_DIR_NONE, 3335 /* tag_action */ MSG_SIMPLE_Q_TAG, 3336 /* data_ptr */ NULL, 3337 /* dxfer_len */ 0, 3338 /* sense_len */ SSD_FULL_SIZE, 3339 sizeof(struct scsi_play_track), 3340 /* timeout */ 50000); 3341 3342 scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes; 3343 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3344 3345 scsi_cmd->op_code = PLAY_TRACK; 3346 scsi_cmd->start_track = strack; 3347 scsi_cmd->start_index = sindex; 3348 scsi_cmd->end_track = etrack; 3349 scsi_cmd->end_index = eindex; 3350 3351 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3352 /*sense_flags*/SF_RETRY_UA); 3353 3354 xpt_release_ccb(ccb); 3355 3356 return(error); 3357 } 3358 3359 static int 3360 cdpause(struct cam_periph *periph, uint32_t go) 3361 { 3362 struct scsi_pause *scsi_cmd; 3363 struct ccb_scsiio *csio; 3364 union ccb *ccb; 3365 int error; 3366 3367 error = 0; 3368 3369 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3370 3371 csio = &ccb->csio; 3372 3373 cam_fill_csio(csio, 3374 /* retries */ cd_retry_count, 3375 /* cbfcnp */ NULL, 3376 /* flags */ CAM_DIR_NONE, 3377 /* tag_action */ MSG_SIMPLE_Q_TAG, 3378 /* data_ptr */ NULL, 3379 /* dxfer_len */ 0, 3380 /* sense_len */ SSD_FULL_SIZE, 3381 sizeof(struct scsi_pause), 3382 /* timeout */ 50000); 3383 3384 scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes; 3385 bzero (scsi_cmd, sizeof(*scsi_cmd)); 3386 3387 scsi_cmd->op_code = PAUSE; 3388 scsi_cmd->resume = go; 3389 3390 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3391 /*sense_flags*/SF_RETRY_UA); 3392 3393 xpt_release_ccb(ccb); 3394 3395 return(error); 3396 } 3397 3398 static int 3399 cdstartunit(struct cam_periph *periph, int load) 3400 { 3401 union ccb *ccb; 3402 int error; 3403 3404 error = 0; 3405 3406 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3407 3408 scsi_start_stop(&ccb->csio, 3409 /* retries */ cd_retry_count, 3410 /* cbfcnp */ NULL, 3411 /* tag_action */ MSG_SIMPLE_Q_TAG, 3412 /* start */ TRUE, 3413 /* load_eject */ load, 3414 /* immediate */ FALSE, 3415 /* sense_len */ SSD_FULL_SIZE, 3416 /* timeout */ 50000); 3417 3418 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3419 /*sense_flags*/SF_RETRY_UA); 3420 3421 xpt_release_ccb(ccb); 3422 3423 return(error); 3424 } 3425 3426 static int 3427 cdstopunit(struct cam_periph *periph, uint32_t eject) 3428 { 3429 union ccb *ccb; 3430 int error; 3431 3432 error = 0; 3433 3434 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3435 3436 scsi_start_stop(&ccb->csio, 3437 /* retries */ cd_retry_count, 3438 /* cbfcnp */ NULL, 3439 /* tag_action */ MSG_SIMPLE_Q_TAG, 3440 /* start */ FALSE, 3441 /* load_eject */ eject, 3442 /* immediate */ FALSE, 3443 /* sense_len */ SSD_FULL_SIZE, 3444 /* timeout */ 50000); 3445 3446 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3447 /*sense_flags*/SF_RETRY_UA); 3448 3449 xpt_release_ccb(ccb); 3450 3451 return(error); 3452 } 3453 3454 static int 3455 cdsetspeed(struct cam_periph *periph, uint32_t rdspeed, uint32_t wrspeed) 3456 { 3457 struct scsi_set_speed *scsi_cmd; 3458 struct ccb_scsiio *csio; 3459 union ccb *ccb; 3460 int error; 3461 3462 error = 0; 3463 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3464 csio = &ccb->csio; 3465 3466 /* Preserve old behavior: units in multiples of CDROM speed */ 3467 if (rdspeed < 177) 3468 rdspeed *= 177; 3469 if (wrspeed < 177) 3470 wrspeed *= 177; 3471 3472 cam_fill_csio(csio, 3473 /* retries */ cd_retry_count, 3474 /* cbfcnp */ NULL, 3475 /* flags */ CAM_DIR_NONE, 3476 /* tag_action */ MSG_SIMPLE_Q_TAG, 3477 /* data_ptr */ NULL, 3478 /* dxfer_len */ 0, 3479 /* sense_len */ SSD_FULL_SIZE, 3480 sizeof(struct scsi_set_speed), 3481 /* timeout */ 50000); 3482 3483 scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes; 3484 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3485 3486 scsi_cmd->opcode = SET_CD_SPEED; 3487 scsi_ulto2b(rdspeed, scsi_cmd->readspeed); 3488 scsi_ulto2b(wrspeed, scsi_cmd->writespeed); 3489 3490 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3491 /*sense_flags*/SF_RETRY_UA); 3492 3493 xpt_release_ccb(ccb); 3494 3495 return(error); 3496 } 3497 3498 static int 3499 cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3500 { 3501 union ccb *ccb; 3502 uint8_t *databuf; 3503 uint32_t lba; 3504 int error; 3505 int length; 3506 3507 error = 0; 3508 databuf = NULL; 3509 lba = 0; 3510 3511 switch (authinfo->format) { 3512 case DVD_REPORT_AGID: 3513 length = sizeof(struct scsi_report_key_data_agid); 3514 break; 3515 case DVD_REPORT_CHALLENGE: 3516 length = sizeof(struct scsi_report_key_data_challenge); 3517 break; 3518 case DVD_REPORT_KEY1: 3519 length = sizeof(struct scsi_report_key_data_key1_key2); 3520 break; 3521 case DVD_REPORT_TITLE_KEY: 3522 length = sizeof(struct scsi_report_key_data_title); 3523 /* The lba field is only set for the title key */ 3524 lba = authinfo->lba; 3525 break; 3526 case DVD_REPORT_ASF: 3527 length = sizeof(struct scsi_report_key_data_asf); 3528 break; 3529 case DVD_REPORT_RPC: 3530 length = sizeof(struct scsi_report_key_data_rpc); 3531 break; 3532 case DVD_INVALIDATE_AGID: 3533 length = 0; 3534 break; 3535 default: 3536 return (EINVAL); 3537 } 3538 3539 if (length != 0) { 3540 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3541 } else 3542 databuf = NULL; 3543 3544 cam_periph_lock(periph); 3545 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3546 3547 scsi_report_key(&ccb->csio, 3548 /* retries */ cd_retry_count, 3549 /* cbfcnp */ NULL, 3550 /* tag_action */ MSG_SIMPLE_Q_TAG, 3551 /* lba */ lba, 3552 /* agid */ authinfo->agid, 3553 /* key_format */ authinfo->format, 3554 /* data_ptr */ databuf, 3555 /* dxfer_len */ length, 3556 /* sense_len */ SSD_FULL_SIZE, 3557 /* timeout */ 50000); 3558 3559 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3560 /*sense_flags*/SF_RETRY_UA); 3561 3562 if (error != 0) 3563 goto bailout; 3564 3565 if (ccb->csio.resid != 0) { 3566 xpt_print(periph->path, "warning, residual for report key " 3567 "command is %d\n", ccb->csio.resid); 3568 } 3569 3570 switch(authinfo->format) { 3571 case DVD_REPORT_AGID: { 3572 struct scsi_report_key_data_agid *agid_data; 3573 3574 agid_data = (struct scsi_report_key_data_agid *)databuf; 3575 3576 authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >> 3577 RKD_AGID_SHIFT; 3578 break; 3579 } 3580 case DVD_REPORT_CHALLENGE: { 3581 struct scsi_report_key_data_challenge *chal_data; 3582 3583 chal_data = (struct scsi_report_key_data_challenge *)databuf; 3584 3585 bcopy(chal_data->challenge_key, authinfo->keychal, 3586 min(sizeof(chal_data->challenge_key), 3587 sizeof(authinfo->keychal))); 3588 break; 3589 } 3590 case DVD_REPORT_KEY1: { 3591 struct scsi_report_key_data_key1_key2 *key1_data; 3592 3593 key1_data = (struct scsi_report_key_data_key1_key2 *)databuf; 3594 3595 bcopy(key1_data->key1, authinfo->keychal, 3596 min(sizeof(key1_data->key1), sizeof(authinfo->keychal))); 3597 break; 3598 } 3599 case DVD_REPORT_TITLE_KEY: { 3600 struct scsi_report_key_data_title *title_data; 3601 3602 title_data = (struct scsi_report_key_data_title *)databuf; 3603 3604 authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >> 3605 RKD_TITLE_CPM_SHIFT; 3606 authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >> 3607 RKD_TITLE_CP_SEC_SHIFT; 3608 authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >> 3609 RKD_TITLE_CMGS_SHIFT; 3610 bcopy(title_data->title_key, authinfo->keychal, 3611 min(sizeof(title_data->title_key), 3612 sizeof(authinfo->keychal))); 3613 break; 3614 } 3615 case DVD_REPORT_ASF: { 3616 struct scsi_report_key_data_asf *asf_data; 3617 3618 asf_data = (struct scsi_report_key_data_asf *)databuf; 3619 3620 authinfo->asf = asf_data->success & RKD_ASF_SUCCESS; 3621 break; 3622 } 3623 case DVD_REPORT_RPC: { 3624 struct scsi_report_key_data_rpc *rpc_data; 3625 3626 rpc_data = (struct scsi_report_key_data_rpc *)databuf; 3627 3628 authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >> 3629 RKD_RPC_TYPE_SHIFT; 3630 authinfo->vend_rsts = 3631 (rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >> 3632 RKD_RPC_VENDOR_RESET_SHIFT; 3633 authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK; 3634 authinfo->region = rpc_data->region_mask; 3635 authinfo->rpc_scheme = rpc_data->rpc_scheme1; 3636 break; 3637 } 3638 case DVD_INVALIDATE_AGID: 3639 break; 3640 default: 3641 /* This should be impossible, since we checked above */ 3642 error = EINVAL; 3643 goto bailout; 3644 break; /* NOTREACHED */ 3645 } 3646 3647 bailout: 3648 xpt_release_ccb(ccb); 3649 cam_periph_unlock(periph); 3650 3651 if (databuf != NULL) 3652 free(databuf, M_DEVBUF); 3653 3654 return(error); 3655 } 3656 3657 static int 3658 cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo) 3659 { 3660 union ccb *ccb; 3661 uint8_t *databuf; 3662 int length; 3663 int error; 3664 3665 error = 0; 3666 databuf = NULL; 3667 3668 switch(authinfo->format) { 3669 case DVD_SEND_CHALLENGE: { 3670 struct scsi_report_key_data_challenge *challenge_data; 3671 3672 length = sizeof(*challenge_data); 3673 3674 challenge_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3675 3676 databuf = (uint8_t *)challenge_data; 3677 3678 scsi_ulto2b(length - sizeof(challenge_data->data_len), 3679 challenge_data->data_len); 3680 3681 bcopy(authinfo->keychal, challenge_data->challenge_key, 3682 min(sizeof(authinfo->keychal), 3683 sizeof(challenge_data->challenge_key))); 3684 break; 3685 } 3686 case DVD_SEND_KEY2: { 3687 struct scsi_report_key_data_key1_key2 *key2_data; 3688 3689 length = sizeof(*key2_data); 3690 3691 key2_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3692 3693 databuf = (uint8_t *)key2_data; 3694 3695 scsi_ulto2b(length - sizeof(key2_data->data_len), 3696 key2_data->data_len); 3697 3698 bcopy(authinfo->keychal, key2_data->key1, 3699 min(sizeof(authinfo->keychal), sizeof(key2_data->key1))); 3700 3701 break; 3702 } 3703 case DVD_SEND_RPC: { 3704 struct scsi_send_key_data_rpc *rpc_data; 3705 3706 length = sizeof(*rpc_data); 3707 3708 rpc_data = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3709 3710 databuf = (uint8_t *)rpc_data; 3711 3712 scsi_ulto2b(length - sizeof(rpc_data->data_len), 3713 rpc_data->data_len); 3714 3715 rpc_data->region_code = authinfo->region; 3716 break; 3717 } 3718 default: 3719 return (EINVAL); 3720 } 3721 3722 cam_periph_lock(periph); 3723 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3724 3725 scsi_send_key(&ccb->csio, 3726 /* retries */ cd_retry_count, 3727 /* cbfcnp */ NULL, 3728 /* tag_action */ MSG_SIMPLE_Q_TAG, 3729 /* agid */ authinfo->agid, 3730 /* key_format */ authinfo->format, 3731 /* data_ptr */ databuf, 3732 /* dxfer_len */ length, 3733 /* sense_len */ SSD_FULL_SIZE, 3734 /* timeout */ 50000); 3735 3736 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3737 /*sense_flags*/SF_RETRY_UA); 3738 3739 xpt_release_ccb(ccb); 3740 cam_periph_unlock(periph); 3741 3742 if (databuf != NULL) 3743 free(databuf, M_DEVBUF); 3744 3745 return(error); 3746 } 3747 3748 static int 3749 cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct) 3750 { 3751 union ccb *ccb; 3752 uint8_t *databuf; 3753 uint32_t address; 3754 int error; 3755 int length; 3756 3757 error = 0; 3758 databuf = NULL; 3759 /* The address is reserved for many of the formats */ 3760 address = 0; 3761 3762 switch(dvdstruct->format) { 3763 case DVD_STRUCT_PHYSICAL: 3764 length = sizeof(struct scsi_read_dvd_struct_data_physical); 3765 break; 3766 case DVD_STRUCT_COPYRIGHT: 3767 length = sizeof(struct scsi_read_dvd_struct_data_copyright); 3768 break; 3769 case DVD_STRUCT_DISCKEY: 3770 length = sizeof(struct scsi_read_dvd_struct_data_disc_key); 3771 break; 3772 case DVD_STRUCT_BCA: 3773 length = sizeof(struct scsi_read_dvd_struct_data_bca); 3774 break; 3775 case DVD_STRUCT_MANUFACT: 3776 length = sizeof(struct scsi_read_dvd_struct_data_manufacturer); 3777 break; 3778 case DVD_STRUCT_CMI: 3779 return (ENODEV); 3780 case DVD_STRUCT_PROTDISCID: 3781 length = sizeof(struct scsi_read_dvd_struct_data_prot_discid); 3782 break; 3783 case DVD_STRUCT_DISCKEYBLOCK: 3784 length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk); 3785 break; 3786 case DVD_STRUCT_DDS: 3787 length = sizeof(struct scsi_read_dvd_struct_data_dds); 3788 break; 3789 case DVD_STRUCT_MEDIUM_STAT: 3790 length = sizeof(struct scsi_read_dvd_struct_data_medium_status); 3791 break; 3792 case DVD_STRUCT_SPARE_AREA: 3793 length = sizeof(struct scsi_read_dvd_struct_data_spare_area); 3794 break; 3795 case DVD_STRUCT_RMD_LAST: 3796 return (ENODEV); 3797 case DVD_STRUCT_RMD_RMA: 3798 return (ENODEV); 3799 case DVD_STRUCT_PRERECORDED: 3800 length = sizeof(struct scsi_read_dvd_struct_data_leadin); 3801 break; 3802 case DVD_STRUCT_UNIQUEID: 3803 length = sizeof(struct scsi_read_dvd_struct_data_disc_id); 3804 break; 3805 case DVD_STRUCT_DCB: 3806 return (ENODEV); 3807 case DVD_STRUCT_LIST: 3808 /* 3809 * This is the maximum allocation length for the READ DVD 3810 * STRUCTURE command. There's nothing in the MMC3 spec 3811 * that indicates a limit in the amount of data that can 3812 * be returned from this call, other than the limits 3813 * imposed by the 2-byte length variables. 3814 */ 3815 length = 65535; 3816 break; 3817 default: 3818 return (EINVAL); 3819 } 3820 3821 if (length != 0) { 3822 databuf = malloc(length, M_DEVBUF, M_WAITOK | M_ZERO); 3823 } else 3824 databuf = NULL; 3825 3826 cam_periph_lock(periph); 3827 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 3828 3829 scsi_read_dvd_structure(&ccb->csio, 3830 /* retries */ cd_retry_count, 3831 /* cbfcnp */ NULL, 3832 /* tag_action */ MSG_SIMPLE_Q_TAG, 3833 /* lba */ address, 3834 /* layer_number */ dvdstruct->layer_num, 3835 /* key_format */ dvdstruct->format, 3836 /* agid */ dvdstruct->agid, 3837 /* data_ptr */ databuf, 3838 /* dxfer_len */ length, 3839 /* sense_len */ SSD_FULL_SIZE, 3840 /* timeout */ 50000); 3841 3842 error = cdrunccb(ccb, cderror, /*cam_flags*/CAM_RETRY_SELTO, 3843 /*sense_flags*/SF_RETRY_UA); 3844 3845 if (error != 0) 3846 goto bailout; 3847 3848 switch(dvdstruct->format) { 3849 case DVD_STRUCT_PHYSICAL: { 3850 struct scsi_read_dvd_struct_data_layer_desc *inlayer; 3851 struct dvd_layer *outlayer; 3852 struct scsi_read_dvd_struct_data_physical *phys_data; 3853 3854 phys_data = 3855 (struct scsi_read_dvd_struct_data_physical *)databuf; 3856 inlayer = &phys_data->layer_desc; 3857 outlayer = (struct dvd_layer *)&dvdstruct->data; 3858 3859 dvdstruct->length = sizeof(*inlayer); 3860 3861 outlayer->book_type = (inlayer->book_type_version & 3862 RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT; 3863 outlayer->book_version = (inlayer->book_type_version & 3864 RDSD_BOOK_VERSION_MASK); 3865 outlayer->disc_size = (inlayer->disc_size_max_rate & 3866 RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT; 3867 outlayer->max_rate = (inlayer->disc_size_max_rate & 3868 RDSD_MAX_RATE_MASK); 3869 outlayer->nlayers = (inlayer->layer_info & 3870 RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT; 3871 outlayer->track_path = (inlayer->layer_info & 3872 RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT; 3873 outlayer->layer_type = (inlayer->layer_info & 3874 RDSD_LAYER_TYPE_MASK); 3875 outlayer->linear_density = (inlayer->density & 3876 RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT; 3877 outlayer->track_density = (inlayer->density & 3878 RDSD_TRACK_DENSITY_MASK); 3879 outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >> 3880 RDSD_BCA_SHIFT; 3881 outlayer->start_sector = scsi_3btoul(inlayer->main_data_start); 3882 outlayer->end_sector = scsi_3btoul(inlayer->main_data_end); 3883 outlayer->end_sector_l0 = 3884 scsi_3btoul(inlayer->end_sector_layer0); 3885 break; 3886 } 3887 case DVD_STRUCT_COPYRIGHT: { 3888 struct scsi_read_dvd_struct_data_copyright *copy_data; 3889 3890 copy_data = (struct scsi_read_dvd_struct_data_copyright *) 3891 databuf; 3892 3893 dvdstruct->cpst = copy_data->cps_type; 3894 dvdstruct->rmi = copy_data->region_info; 3895 dvdstruct->length = 0; 3896 3897 break; 3898 } 3899 default: 3900 /* 3901 * Tell the user what the overall length is, no matter 3902 * what we can actually fit in the data buffer. 3903 */ 3904 dvdstruct->length = length - ccb->csio.resid - 3905 sizeof(struct scsi_read_dvd_struct_data_header); 3906 3907 /* 3908 * But only actually copy out the smaller of what we read 3909 * in or what the structure can take. 3910 */ 3911 bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header), 3912 dvdstruct->data, 3913 min(sizeof(dvdstruct->data), dvdstruct->length)); 3914 break; 3915 } 3916 3917 bailout: 3918 xpt_release_ccb(ccb); 3919 cam_periph_unlock(periph); 3920 3921 if (databuf != NULL) 3922 free(databuf, M_DEVBUF); 3923 3924 return(error); 3925 } 3926 3927 void 3928 scsi_report_key(struct ccb_scsiio *csio, uint32_t retries, 3929 void (*cbfcnp)(struct cam_periph *, union ccb *), 3930 uint8_t tag_action, uint32_t lba, uint8_t agid, 3931 uint8_t key_format, uint8_t *data_ptr, uint32_t dxfer_len, 3932 uint8_t sense_len, uint32_t timeout) 3933 { 3934 struct scsi_report_key *scsi_cmd; 3935 3936 scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes; 3937 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3938 scsi_cmd->opcode = REPORT_KEY; 3939 scsi_ulto4b(lba, scsi_cmd->lba); 3940 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 3941 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 3942 (key_format & RK_KF_KEYFORMAT_MASK); 3943 3944 cam_fill_csio(csio, 3945 retries, 3946 cbfcnp, 3947 /*flags*/ (dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN, 3948 tag_action, 3949 /*data_ptr*/ data_ptr, 3950 /*dxfer_len*/ dxfer_len, 3951 sense_len, 3952 sizeof(*scsi_cmd), 3953 timeout); 3954 } 3955 3956 void 3957 scsi_send_key(struct ccb_scsiio *csio, uint32_t retries, 3958 void (*cbfcnp)(struct cam_periph *, union ccb *), 3959 uint8_t tag_action, uint8_t agid, uint8_t key_format, 3960 uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len, 3961 uint32_t timeout) 3962 { 3963 struct scsi_send_key *scsi_cmd; 3964 3965 scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes; 3966 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3967 scsi_cmd->opcode = SEND_KEY; 3968 3969 scsi_ulto2b(dxfer_len, scsi_cmd->param_len); 3970 scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) | 3971 (key_format & RK_KF_KEYFORMAT_MASK); 3972 3973 cam_fill_csio(csio, 3974 retries, 3975 cbfcnp, 3976 /*flags*/ CAM_DIR_OUT, 3977 tag_action, 3978 /*data_ptr*/ data_ptr, 3979 /*dxfer_len*/ dxfer_len, 3980 sense_len, 3981 sizeof(*scsi_cmd), 3982 timeout); 3983 } 3984 3985 void 3986 scsi_read_dvd_structure(struct ccb_scsiio *csio, uint32_t retries, 3987 void (*cbfcnp)(struct cam_periph *, union ccb *), 3988 uint8_t tag_action, uint32_t address, 3989 uint8_t layer_number, uint8_t format, uint8_t agid, 3990 uint8_t *data_ptr, uint32_t dxfer_len, 3991 uint8_t sense_len, uint32_t timeout) 3992 { 3993 struct scsi_read_dvd_structure *scsi_cmd; 3994 3995 scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes; 3996 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3997 scsi_cmd->opcode = READ_DVD_STRUCTURE; 3998 3999 scsi_ulto4b(address, scsi_cmd->address); 4000 scsi_cmd->layer_number = layer_number; 4001 scsi_cmd->format = format; 4002 scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len); 4003 /* The AGID is the top two bits of this byte */ 4004 scsi_cmd->agid = agid << 6; 4005 4006 cam_fill_csio(csio, 4007 retries, 4008 cbfcnp, 4009 /*flags*/ CAM_DIR_IN, 4010 tag_action, 4011 /*data_ptr*/ data_ptr, 4012 /*dxfer_len*/ dxfer_len, 4013 sense_len, 4014 sizeof(*scsi_cmd), 4015 timeout); 4016 } 4017 4018 void 4019 scsi_read_toc(struct ccb_scsiio *csio, uint32_t retries, 4020 void (*cbfcnp)(struct cam_periph *, union ccb *), 4021 uint8_t tag_action, uint8_t byte1_flags, uint8_t format, 4022 uint8_t track, uint8_t *data_ptr, uint32_t dxfer_len, 4023 int sense_len, int timeout) 4024 { 4025 struct scsi_read_toc *scsi_cmd; 4026 4027 scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes; 4028 bzero(scsi_cmd, sizeof(*scsi_cmd)); 4029 scsi_cmd->op_code = READ_TOC; 4030 4031 /* 4032 * The structure is counting from 1, the function counting from 0. 4033 * The spec counts from 0. In MMC-6, there is only one flag, the 4034 * MSF flag. But we put the whole byte in for a bit a future-proofing. 4035 */ 4036 scsi_cmd->byte2 = byte1_flags; 4037 scsi_cmd->format = format; 4038 scsi_cmd->from_track = track; 4039 scsi_ulto2b(dxfer_len, scsi_cmd->data_len); 4040 4041 cam_fill_csio(csio, 4042 /* retries */ retries, 4043 /* cbfcnp */ cbfcnp, 4044 /* flags */ CAM_DIR_IN, 4045 /* tag_action */ tag_action, 4046 /* data_ptr */ data_ptr, 4047 /* dxfer_len */ dxfer_len, 4048 /* sense_len */ sense_len, 4049 sizeof(*scsi_cmd), 4050 /* timeout */ timeout); 4051 } 4052