1/* 2 * ATAPI CD-ROM driver. 3 * 4 * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov> 5 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org> 6 * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de> 7 * Copyright (C) 2005, 2007-2009 Bartlomiej Zolnierkiewicz 8 * 9 * May be copied or modified under the terms of the GNU General Public 10 * License. See linux/COPYING for more information. 11 * 12 * See Documentation/cdrom/ide-cd for usage information. 13 * 14 * Suggestions are welcome. Patches that work are more welcome though. ;-) 15 * 16 * Documentation: 17 * Mt. Fuji (SFF8090 version 4) and ATAPI (SFF-8020i rev 2.6) standards. 18 * 19 * For historical changelog please see: 20 * Documentation/ide/ChangeLog.ide-cd.1994-2004 21 */ 22 23#define DRV_NAME "ide-cd" 24#define PFX DRV_NAME ": " 25 26#define IDECD_VERSION "5.00" 27 28#include <linux/module.h> 29#include <linux/types.h> 30#include <linux/kernel.h> 31#include <linux/delay.h> 32#include <linux/timer.h> 33#include <linux/seq_file.h> 34#include <linux/smp_lock.h> 35#include <linux/slab.h> 36#include <linux/interrupt.h> 37#include <linux/errno.h> 38#include <linux/cdrom.h> 39#include <linux/ide.h> 40#include <linux/completion.h> 41#include <linux/mutex.h> 42#include <linux/bcd.h> 43 44/* For SCSI -> ATAPI command conversion */ 45#include <scsi/scsi.h> 46 47#include <linux/irq.h> 48#include <linux/io.h> 49#include <asm/byteorder.h> 50#include <linux/uaccess.h> 51#include <asm/unaligned.h> 52 53#include "ide-cd.h" 54 55static DEFINE_MUTEX(idecd_ref_mutex); 56 57static void ide_cd_release(struct device *); 58 59static struct cdrom_info *ide_cd_get(struct gendisk *disk) 60{ 61 struct cdrom_info *cd = NULL; 62 63 mutex_lock(&idecd_ref_mutex); 64 cd = ide_drv_g(disk, cdrom_info); 65 if (cd) { 66 if (ide_device_get(cd->drive)) 67 cd = NULL; 68 else 69 get_device(&cd->dev); 70 71 } 72 mutex_unlock(&idecd_ref_mutex); 73 return cd; 74} 75 76static void ide_cd_put(struct cdrom_info *cd) 77{ 78 ide_drive_t *drive = cd->drive; 79 80 mutex_lock(&idecd_ref_mutex); 81 put_device(&cd->dev); 82 ide_device_put(drive); 83 mutex_unlock(&idecd_ref_mutex); 84} 85 86/* 87 * Generic packet command support and error handling routines. 88 */ 89 90/* Mark that we've seen a media change and invalidate our internal buffers. */ 91static void cdrom_saw_media_change(ide_drive_t *drive) 92{ 93 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED; 94 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID; 95} 96 97static int cdrom_log_sense(ide_drive_t *drive, struct request *rq) 98{ 99 struct request_sense *sense = &drive->sense_data; 100 int log = 0; 101 102 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET)) 103 return 0; 104 105 ide_debug_log(IDE_DBG_SENSE, "sense_key: 0x%x", sense->sense_key); 106 107 switch (sense->sense_key) { 108 case NO_SENSE: 109 case RECOVERED_ERROR: 110 break; 111 case NOT_READY: 112 /* 113 * don't care about tray state messages for e.g. capacity 114 * commands or in-progress or becoming ready 115 */ 116 if (sense->asc == 0x3a || sense->asc == 0x04) 117 break; 118 log = 1; 119 break; 120 case ILLEGAL_REQUEST: 121 /* 122 * don't log START_STOP unit with LoEj set, since we cannot 123 * reliably check if drive can auto-close 124 */ 125 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24) 126 break; 127 log = 1; 128 break; 129 case UNIT_ATTENTION: 130 /* 131 * Make good and sure we've seen this potential media change. 132 * Some drives (i.e. Creative) fail to present the correct sense 133 * key in the error register. 134 */ 135 cdrom_saw_media_change(drive); 136 break; 137 default: 138 log = 1; 139 break; 140 } 141 return log; 142} 143 144static void cdrom_analyze_sense_data(ide_drive_t *drive, 145 struct request *failed_command) 146{ 147 struct request_sense *sense = &drive->sense_data; 148 struct cdrom_info *info = drive->driver_data; 149 unsigned long sector; 150 unsigned long bio_sectors; 151 152 ide_debug_log(IDE_DBG_SENSE, "error_code: 0x%x, sense_key: 0x%x", 153 sense->error_code, sense->sense_key); 154 155 if (failed_command) 156 ide_debug_log(IDE_DBG_SENSE, "failed cmd: 0x%x", 157 failed_command->cmd[0]); 158 159 if (!cdrom_log_sense(drive, failed_command)) 160 return; 161 162 /* 163 * If a read toc is executed for a CD-R or CD-RW medium where the first 164 * toc has not been recorded yet, it will fail with 05/24/00 (which is a 165 * confusing error) 166 */ 167 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP) 168 if (sense->sense_key == 0x05 && sense->asc == 0x24) 169 return; 170 171 /* current error */ 172 if (sense->error_code == 0x70) { 173 switch (sense->sense_key) { 174 case MEDIUM_ERROR: 175 case VOLUME_OVERFLOW: 176 case ILLEGAL_REQUEST: 177 if (!sense->valid) 178 break; 179 if (failed_command == NULL || 180 failed_command->cmd_type != REQ_TYPE_FS) 181 break; 182 sector = (sense->information[0] << 24) | 183 (sense->information[1] << 16) | 184 (sense->information[2] << 8) | 185 (sense->information[3]); 186 187 if (queue_logical_block_size(drive->queue) == 2048) 188 /* device sector size is 2K */ 189 sector <<= 2; 190 191 bio_sectors = max(bio_sectors(failed_command->bio), 4U); 192 sector &= ~(bio_sectors - 1); 193 194 /* 195 * The SCSI specification allows for the value 196 * returned by READ CAPACITY to be up to 75 2K 197 * sectors past the last readable block. 198 * Therefore, if we hit a medium error within the 199 * last 75 2K sectors, we decrease the saved size 200 * value. 201 */ 202 if (sector < get_capacity(info->disk) && 203 drive->probed_capacity - sector < 4 * 75) 204 set_capacity(info->disk, sector); 205 } 206 } 207 208 ide_cd_log_error(drive->name, failed_command, sense); 209} 210 211static void ide_cd_complete_failed_rq(ide_drive_t *drive, struct request *rq) 212{ 213 /* 214 * For REQ_TYPE_SENSE, "rq->special" points to the original 215 * failed request. Also, the sense data should be read 216 * directly from rq which might be different from the original 217 * sense buffer if it got copied during mapping. 218 */ 219 struct request *failed = (struct request *)rq->special; 220 void *sense = bio_data(rq->bio); 221 222 if (failed) { 223 if (failed->sense) { 224 /* 225 * Sense is always read into drive->sense_data. 226 * Copy back if the failed request has its 227 * sense pointer set. 228 */ 229 memcpy(failed->sense, sense, 18); 230 failed->sense_len = rq->sense_len; 231 } 232 cdrom_analyze_sense_data(drive, failed); 233 234 if (ide_end_rq(drive, failed, -EIO, blk_rq_bytes(failed))) 235 BUG(); 236 } else 237 cdrom_analyze_sense_data(drive, NULL); 238} 239 240 241/* 242 * Allow the drive 5 seconds to recover; some devices will return NOT_READY 243 * while flushing data from cache. 244 * 245 * returns: 0 failed (write timeout expired) 246 * 1 success 247 */ 248static int ide_cd_breathe(ide_drive_t *drive, struct request *rq) 249{ 250 251 struct cdrom_info *info = drive->driver_data; 252 253 if (!rq->errors) 254 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY; 255 256 rq->errors = 1; 257 258 if (time_after(jiffies, info->write_timeout)) 259 return 0; 260 else { 261 struct request_queue *q = drive->queue; 262 unsigned long flags; 263 264 /* 265 * take a breather relying on the unplug timer to kick us again 266 */ 267 268 spin_lock_irqsave(q->queue_lock, flags); 269 blk_plug_device(q); 270 spin_unlock_irqrestore(q->queue_lock, flags); 271 272 return 1; 273 } 274} 275 276/** 277 * Returns: 278 * 0: if the request should be continued. 279 * 1: if the request will be going through error recovery. 280 * 2: if the request should be ended. 281 */ 282static int cdrom_decode_status(ide_drive_t *drive, u8 stat) 283{ 284 ide_hwif_t *hwif = drive->hwif; 285 struct request *rq = hwif->rq; 286 int err, sense_key, do_end_request = 0; 287 288 /* get the IDE error register */ 289 err = ide_read_error(drive); 290 sense_key = err >> 4; 291 292 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, rq->cmd_type: 0x%x, err: 0x%x, " 293 "stat 0x%x", 294 rq->cmd[0], rq->cmd_type, err, stat); 295 296 if (rq->cmd_type == REQ_TYPE_SENSE) { 297 /* 298 * We got an error trying to get sense info from the drive 299 * (probably while trying to recover from a former error). 300 * Just give up. 301 */ 302 rq->cmd_flags |= REQ_FAILED; 303 return 2; 304 } 305 306 /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */ 307 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !rq->errors) 308 rq->errors = SAM_STAT_CHECK_CONDITION; 309 310 if (blk_noretry_request(rq)) 311 do_end_request = 1; 312 313 switch (sense_key) { 314 case NOT_READY: 315 if (rq->cmd_type == REQ_TYPE_FS && rq_data_dir(rq) == WRITE) { 316 if (ide_cd_breathe(drive, rq)) 317 return 1; 318 } else { 319 cdrom_saw_media_change(drive); 320 321 if (rq->cmd_type == REQ_TYPE_FS && 322 !(rq->cmd_flags & REQ_QUIET)) 323 printk(KERN_ERR PFX "%s: tray open\n", 324 drive->name); 325 } 326 do_end_request = 1; 327 break; 328 case UNIT_ATTENTION: 329 cdrom_saw_media_change(drive); 330 331 if (rq->cmd_type != REQ_TYPE_FS) 332 return 0; 333 334 /* 335 * Arrange to retry the request but be sure to give up if we've 336 * retried too many times. 337 */ 338 if (++rq->errors > ERROR_MAX) 339 do_end_request = 1; 340 break; 341 case ILLEGAL_REQUEST: 342 /* 343 * Don't print error message for this condition -- SFF8090i 344 * indicates that 5/24/00 is the correct response to a request 345 * to close the tray if the drive doesn't have that capability. 346 * 347 * cdrom_log_sense() knows this! 348 */ 349 if (rq->cmd[0] == GPCMD_START_STOP_UNIT) 350 break; 351 /* fall-through */ 352 case DATA_PROTECT: 353 /* 354 * No point in retrying after an illegal request or data 355 * protect error. 356 */ 357 if (!(rq->cmd_flags & REQ_QUIET)) 358 ide_dump_status(drive, "command error", stat); 359 do_end_request = 1; 360 break; 361 case MEDIUM_ERROR: 362 /* 363 * No point in re-trying a zillion times on a bad sector. 364 * If we got here the error is not correctable. 365 */ 366 if (!(rq->cmd_flags & REQ_QUIET)) 367 ide_dump_status(drive, "media error " 368 "(bad sector)", stat); 369 do_end_request = 1; 370 break; 371 case BLANK_CHECK: 372 /* disk appears blank? */ 373 if (!(rq->cmd_flags & REQ_QUIET)) 374 ide_dump_status(drive, "media error (blank)", 375 stat); 376 do_end_request = 1; 377 break; 378 default: 379 if (rq->cmd_type != REQ_TYPE_FS) 380 break; 381 if (err & ~ATA_ABORTED) { 382 /* go to the default handler for other errors */ 383 ide_error(drive, "cdrom_decode_status", stat); 384 return 1; 385 } else if (++rq->errors > ERROR_MAX) 386 /* we've racked up too many retries, abort */ 387 do_end_request = 1; 388 } 389 390 if (rq->cmd_type != REQ_TYPE_FS) { 391 rq->cmd_flags |= REQ_FAILED; 392 do_end_request = 1; 393 } 394 395 /* 396 * End a request through request sense analysis when we have sense data. 397 * We need this in order to perform end of media processing. 398 */ 399 if (do_end_request) 400 goto end_request; 401 402 /* if we got a CHECK_CONDITION status, queue a request sense command */ 403 if (stat & ATA_ERR) 404 return ide_queue_sense_rq(drive, NULL) ? 2 : 1; 405 return 1; 406 407end_request: 408 if (stat & ATA_ERR) { 409 hwif->rq = NULL; 410 return ide_queue_sense_rq(drive, rq) ? 2 : 1; 411 } else 412 return 2; 413} 414 415static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct ide_cmd *cmd) 416{ 417 struct request *rq = cmd->rq; 418 419 ide_debug_log(IDE_DBG_FUNC, "rq->cmd[0]: 0x%x", rq->cmd[0]); 420 421 /* 422 * Some of the trailing request sense fields are optional, 423 * and some drives don't send them. Sigh. 424 */ 425 if (rq->cmd[0] == GPCMD_REQUEST_SENSE && 426 cmd->nleft > 0 && cmd->nleft <= 5) 427 cmd->nleft = 0; 428} 429 430int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd, 431 int write, void *buffer, unsigned *bufflen, 432 struct request_sense *sense, int timeout, 433 unsigned int cmd_flags) 434{ 435 struct cdrom_info *info = drive->driver_data; 436 struct request_sense local_sense; 437 int retries = 10; 438 unsigned int flags = 0; 439 440 if (!sense) 441 sense = &local_sense; 442 443 ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x, timeout: %d, " 444 "cmd_flags: 0x%x", 445 cmd[0], write, timeout, cmd_flags); 446 447 /* start of retry loop */ 448 do { 449 struct request *rq; 450 int error; 451 452 rq = blk_get_request(drive->queue, write, __GFP_WAIT); 453 454 memcpy(rq->cmd, cmd, BLK_MAX_CDB); 455 rq->cmd_type = REQ_TYPE_ATA_PC; 456 rq->sense = sense; 457 rq->cmd_flags |= cmd_flags; 458 rq->timeout = timeout; 459 if (buffer) { 460 error = blk_rq_map_kern(drive->queue, rq, buffer, 461 *bufflen, GFP_NOIO); 462 if (error) { 463 blk_put_request(rq); 464 return error; 465 } 466 } 467 468 error = blk_execute_rq(drive->queue, info->disk, rq, 0); 469 470 if (buffer) 471 *bufflen = rq->resid_len; 472 473 flags = rq->cmd_flags; 474 blk_put_request(rq); 475 476 if (flags & REQ_FAILED) { 477 /* 478 * The request failed. Retry if it was due to a unit 479 * attention status (usually means media was changed). 480 */ 481 struct request_sense *reqbuf = sense; 482 483 if (reqbuf->sense_key == UNIT_ATTENTION) 484 cdrom_saw_media_change(drive); 485 else if (reqbuf->sense_key == NOT_READY && 486 reqbuf->asc == 4 && reqbuf->ascq != 4) { 487 /* 488 * The drive is in the process of loading 489 * a disk. Retry, but wait a little to give 490 * the drive time to complete the load. 491 */ 492 ssleep(2); 493 } else { 494 /* otherwise, don't retry */ 495 retries = 0; 496 } 497 --retries; 498 } 499 500 /* end of retry loop */ 501 } while ((flags & REQ_FAILED) && retries >= 0); 502 503 /* return an error if the command failed */ 504 return (flags & REQ_FAILED) ? -EIO : 0; 505} 506 507/* 508 * returns true if rq has been completed 509 */ 510static bool ide_cd_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd) 511{ 512 unsigned int nr_bytes = cmd->nbytes - cmd->nleft; 513 514 if (cmd->tf_flags & IDE_TFLAG_WRITE) 515 nr_bytes -= cmd->last_xfer_len; 516 517 if (nr_bytes > 0) { 518 ide_complete_rq(drive, 0, nr_bytes); 519 return true; 520 } 521 522 return false; 523} 524 525static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive) 526{ 527 ide_hwif_t *hwif = drive->hwif; 528 struct ide_cmd *cmd = &hwif->cmd; 529 struct request *rq = hwif->rq; 530 ide_expiry_t *expiry = NULL; 531 int dma_error = 0, dma, thislen, uptodate = 0; 532 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0; 533 int sense = (rq->cmd_type == REQ_TYPE_SENSE); 534 unsigned int timeout; 535 u16 len; 536 u8 ireason, stat; 537 538 ide_debug_log(IDE_DBG_PC, "cmd: 0x%x, write: 0x%x", rq->cmd[0], write); 539 540 /* check for errors */ 541 dma = drive->dma; 542 if (dma) { 543 drive->dma = 0; 544 drive->waiting_for_dma = 0; 545 dma_error = hwif->dma_ops->dma_end(drive); 546 ide_dma_unmap_sg(drive, cmd); 547 if (dma_error) { 548 printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name, 549 write ? "write" : "read"); 550 ide_dma_off(drive); 551 } 552 } 553 554 /* check status */ 555 stat = hwif->tp_ops->read_status(hwif); 556 557 if (!OK_STAT(stat, 0, BAD_R_STAT)) { 558 rc = cdrom_decode_status(drive, stat); 559 if (rc) { 560 if (rc == 2) 561 goto out_end; 562 return ide_stopped; 563 } 564 } 565 566 /* using dma, transfer is complete now */ 567 if (dma) { 568 if (dma_error) 569 return ide_error(drive, "dma error", stat); 570 uptodate = 1; 571 goto out_end; 572 } 573 574 ide_read_bcount_and_ireason(drive, &len, &ireason); 575 576 thislen = (rq->cmd_type == REQ_TYPE_FS) ? len : cmd->nleft; 577 if (thislen > len) 578 thislen = len; 579 580 ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d", 581 stat, thislen); 582 583 /* If DRQ is clear, the command has completed. */ 584 if ((stat & ATA_DRQ) == 0) { 585 if (rq->cmd_type == REQ_TYPE_FS) { 586 /* 587 * If we're not done reading/writing, complain. 588 * Otherwise, complete the command normally. 589 */ 590 uptodate = 1; 591 if (cmd->nleft > 0) { 592 printk(KERN_ERR PFX "%s: %s: data underrun " 593 "(%u bytes)\n", drive->name, __func__, 594 cmd->nleft); 595 if (!write) 596 rq->cmd_flags |= REQ_FAILED; 597 uptodate = 0; 598 } 599 } else if (rq->cmd_type != REQ_TYPE_BLOCK_PC) { 600 ide_cd_request_sense_fixup(drive, cmd); 601 602 uptodate = cmd->nleft ? 0 : 1; 603 604 /* 605 * suck out the remaining bytes from the drive in an 606 * attempt to complete the data xfer. (see BZ#13399) 607 */ 608 if (!(stat & ATA_ERR) && !uptodate && thislen) { 609 ide_pio_bytes(drive, cmd, write, thislen); 610 uptodate = cmd->nleft ? 0 : 1; 611 } 612 613 if (!uptodate) 614 rq->cmd_flags |= REQ_FAILED; 615 } 616 goto out_end; 617 } 618 619 rc = ide_check_ireason(drive, rq, len, ireason, write); 620 if (rc) 621 goto out_end; 622 623 cmd->last_xfer_len = 0; 624 625 ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, " 626 "ireason: 0x%x", 627 rq->cmd_type, ireason); 628 629 /* transfer data */ 630 while (thislen > 0) { 631 int blen = min_t(int, thislen, cmd->nleft); 632 633 if (cmd->nleft == 0) 634 break; 635 636 ide_pio_bytes(drive, cmd, write, blen); 637 cmd->last_xfer_len += blen; 638 639 thislen -= blen; 640 len -= blen; 641 642 if (sense && write == 0) 643 rq->sense_len += blen; 644 } 645 646 /* pad, if necessary */ 647 if (len > 0) { 648 if (rq->cmd_type != REQ_TYPE_FS || write == 0) 649 ide_pad_transfer(drive, write, len); 650 else { 651 printk(KERN_ERR PFX "%s: confused, missing data\n", 652 drive->name); 653 blk_dump_rq_flags(rq, "cdrom_newpc_intr"); 654 } 655 } 656 657 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { 658 timeout = rq->timeout; 659 } else { 660 timeout = ATAPI_WAIT_PC; 661 if (rq->cmd_type != REQ_TYPE_FS) 662 expiry = ide_cd_expiry; 663 } 664 665 hwif->expiry = expiry; 666 ide_set_handler(drive, cdrom_newpc_intr, timeout); 667 return ide_started; 668 669out_end: 670 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && rc == 0) { 671 rq->resid_len = 0; 672 blk_end_request_all(rq, 0); 673 hwif->rq = NULL; 674 } else { 675 if (sense && uptodate) 676 ide_cd_complete_failed_rq(drive, rq); 677 678 if (rq->cmd_type == REQ_TYPE_FS) { 679 if (cmd->nleft == 0) 680 uptodate = 1; 681 } else { 682 if (uptodate <= 0 && rq->errors == 0) 683 rq->errors = -EIO; 684 } 685 686 if (uptodate == 0 && rq->bio) 687 if (ide_cd_error_cmd(drive, cmd)) 688 return ide_stopped; 689 690 /* make sure it's fully ended */ 691 if (rq->cmd_type != REQ_TYPE_FS) { 692 rq->resid_len -= cmd->nbytes - cmd->nleft; 693 if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE)) 694 rq->resid_len += cmd->last_xfer_len; 695 } 696 697 ide_complete_rq(drive, uptodate ? 0 : -EIO, blk_rq_bytes(rq)); 698 699 if (sense && rc == 2) 700 ide_error(drive, "request sense failure", stat); 701 } 702 return ide_stopped; 703} 704 705static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq) 706{ 707 struct cdrom_info *cd = drive->driver_data; 708 struct request_queue *q = drive->queue; 709 int write = rq_data_dir(rq) == WRITE; 710 unsigned short sectors_per_frame = 711 queue_logical_block_size(q) >> SECTOR_BITS; 712 713 ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, " 714 "secs_per_frame: %u", 715 rq->cmd[0], rq->cmd_flags, sectors_per_frame); 716 717 if (write) { 718 /* disk has become write protected */ 719 if (get_disk_ro(cd->disk)) 720 return ide_stopped; 721 } else { 722 /* 723 * We may be retrying this request after an error. Fix up any 724 * weirdness which might be present in the request packet. 725 */ 726 q->prep_rq_fn(q, rq); 727 } 728 729 /* fs requests *must* be hardware frame aligned */ 730 if ((blk_rq_sectors(rq) & (sectors_per_frame - 1)) || 731 (blk_rq_pos(rq) & (sectors_per_frame - 1))) 732 return ide_stopped; 733 734 /* use DMA, if possible */ 735 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA); 736 737 if (write) 738 cd->devinfo.media_written = 1; 739 740 rq->timeout = ATAPI_WAIT_PC; 741 742 return ide_started; 743} 744 745static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq) 746{ 747 748 ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x", 749 rq->cmd[0], rq->cmd_type); 750 751 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) 752 rq->cmd_flags |= REQ_QUIET; 753 else 754 rq->cmd_flags &= ~REQ_FAILED; 755 756 drive->dma = 0; 757 758 /* sg request */ 759 if (rq->bio) { 760 struct request_queue *q = drive->queue; 761 char *buf = bio_data(rq->bio); 762 unsigned int alignment; 763 764 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA); 765 766 /* 767 * check if dma is safe 768 * 769 * NOTE! The "len" and "addr" checks should possibly have 770 * separate masks. 771 */ 772 alignment = queue_dma_alignment(q) | q->dma_pad_mask; 773 if ((unsigned long)buf & alignment 774 || blk_rq_bytes(rq) & q->dma_pad_mask 775 || object_is_on_stack(buf)) 776 drive->dma = 0; 777 } 778} 779 780static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq, 781 sector_t block) 782{ 783 struct ide_cmd cmd; 784 int uptodate = 0, nsectors; 785 786 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu", 787 rq->cmd[0], (unsigned long long)block); 788 789 if (drive->debug_mask & IDE_DBG_RQ) 790 blk_dump_rq_flags(rq, "ide_cd_do_request"); 791 792 switch (rq->cmd_type) { 793 case REQ_TYPE_FS: 794 if (cdrom_start_rw(drive, rq) == ide_stopped) 795 goto out_end; 796 break; 797 case REQ_TYPE_SENSE: 798 case REQ_TYPE_BLOCK_PC: 799 case REQ_TYPE_ATA_PC: 800 if (!rq->timeout) 801 rq->timeout = ATAPI_WAIT_PC; 802 803 cdrom_do_block_pc(drive, rq); 804 break; 805 case REQ_TYPE_SPECIAL: 806 /* right now this can only be a reset... */ 807 uptodate = 1; 808 goto out_end; 809 default: 810 BUG(); 811 } 812 813 /* prepare sense request for this command */ 814 ide_prep_sense(drive, rq); 815 816 memset(&cmd, 0, sizeof(cmd)); 817 818 if (rq_data_dir(rq)) 819 cmd.tf_flags |= IDE_TFLAG_WRITE; 820 821 cmd.rq = rq; 822 823 if (rq->cmd_type == REQ_TYPE_FS || blk_rq_bytes(rq)) { 824 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq)); 825 ide_map_sg(drive, &cmd); 826 } 827 828 return ide_issue_pc(drive, &cmd); 829out_end: 830 nsectors = blk_rq_sectors(rq); 831 832 if (nsectors == 0) 833 nsectors = 1; 834 835 ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9); 836 837 return ide_stopped; 838} 839 840/* 841 * Ioctl handling. 842 * 843 * Routines which queue packet commands take as a final argument a pointer to a 844 * request_sense struct. If execution of the command results in an error with a 845 * CHECK CONDITION status, this structure will be filled with the results of the 846 * subsequent request sense command. The pointer can also be NULL, in which case 847 * no sense information is returned. 848 */ 849static void msf_from_bcd(struct atapi_msf *msf) 850{ 851 msf->minute = bcd2bin(msf->minute); 852 msf->second = bcd2bin(msf->second); 853 msf->frame = bcd2bin(msf->frame); 854} 855 856int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense) 857{ 858 struct cdrom_info *info = drive->driver_data; 859 struct cdrom_device_info *cdi = &info->devinfo; 860 unsigned char cmd[BLK_MAX_CDB]; 861 862 ide_debug_log(IDE_DBG_FUNC, "enter"); 863 864 memset(cmd, 0, BLK_MAX_CDB); 865 cmd[0] = GPCMD_TEST_UNIT_READY; 866 867 /* 868 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs 869 * instead of supporting the LOAD_UNLOAD opcode. 870 */ 871 cmd[7] = cdi->sanyo_slot % 3; 872 873 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, REQ_QUIET); 874} 875 876static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity, 877 unsigned long *sectors_per_frame, 878 struct request_sense *sense) 879{ 880 struct { 881 __be32 lba; 882 __be32 blocklen; 883 } capbuf; 884 885 int stat; 886 unsigned char cmd[BLK_MAX_CDB]; 887 unsigned len = sizeof(capbuf); 888 u32 blocklen; 889 890 ide_debug_log(IDE_DBG_FUNC, "enter"); 891 892 memset(cmd, 0, BLK_MAX_CDB); 893 cmd[0] = GPCMD_READ_CDVD_CAPACITY; 894 895 stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, sense, 0, 896 REQ_QUIET); 897 if (stat) 898 return stat; 899 900 /* 901 * Sanity check the given block size, in so far as making 902 * sure the sectors_per_frame we give to the caller won't 903 * end up being bogus. 904 */ 905 blocklen = be32_to_cpu(capbuf.blocklen); 906 blocklen = (blocklen >> SECTOR_BITS) << SECTOR_BITS; 907 switch (blocklen) { 908 case 512: 909 case 1024: 910 case 2048: 911 case 4096: 912 break; 913 default: 914 printk_once(KERN_ERR PFX "%s: weird block size %u; " 915 "setting default block size to 2048\n", 916 drive->name, blocklen); 917 blocklen = 2048; 918 break; 919 } 920 921 *capacity = 1 + be32_to_cpu(capbuf.lba); 922 *sectors_per_frame = blocklen >> SECTOR_BITS; 923 924 ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu", 925 *capacity, *sectors_per_frame); 926 927 return 0; 928} 929 930static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag, 931 int format, char *buf, int buflen, 932 struct request_sense *sense) 933{ 934 unsigned char cmd[BLK_MAX_CDB]; 935 936 ide_debug_log(IDE_DBG_FUNC, "enter"); 937 938 memset(cmd, 0, BLK_MAX_CDB); 939 940 cmd[0] = GPCMD_READ_TOC_PMA_ATIP; 941 cmd[6] = trackno; 942 cmd[7] = (buflen >> 8); 943 cmd[8] = (buflen & 0xff); 944 cmd[9] = (format << 6); 945 946 if (msf_flag) 947 cmd[1] = 2; 948 949 return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, sense, 0, REQ_QUIET); 950} 951 952/* Try to read the entire TOC for the disk into our internal buffer. */ 953int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense) 954{ 955 int stat, ntracks, i; 956 struct cdrom_info *info = drive->driver_data; 957 struct cdrom_device_info *cdi = &info->devinfo; 958 struct atapi_toc *toc = info->toc; 959 struct { 960 struct atapi_toc_header hdr; 961 struct atapi_toc_entry ent; 962 } ms_tmp; 963 long last_written; 964 unsigned long sectors_per_frame = SECTORS_PER_FRAME; 965 966 ide_debug_log(IDE_DBG_FUNC, "enter"); 967 968 if (toc == NULL) { 969 /* try to allocate space */ 970 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL); 971 if (toc == NULL) { 972 printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n", 973 drive->name); 974 return -ENOMEM; 975 } 976 info->toc = toc; 977 } 978 979 /* 980 * Check to see if the existing data is still valid. If it is, 981 * just return. 982 */ 983 (void) cdrom_check_status(drive, sense); 984 985 if (drive->atapi_flags & IDE_AFLAG_TOC_VALID) 986 return 0; 987 988 /* try to get the total cdrom capacity and sector size */ 989 stat = cdrom_read_capacity(drive, &toc->capacity, §ors_per_frame, 990 sense); 991 if (stat) 992 toc->capacity = 0x1fffff; 993 994 set_capacity(info->disk, toc->capacity * sectors_per_frame); 995 /* save a private copy of the TOC capacity for error handling */ 996 drive->probed_capacity = toc->capacity * sectors_per_frame; 997 998 blk_queue_logical_block_size(drive->queue, 999 sectors_per_frame << SECTOR_BITS); 1000 1001 /* first read just the header, so we know how long the TOC is */ 1002 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr, 1003 sizeof(struct atapi_toc_header), sense); 1004 if (stat) 1005 return stat; 1006 1007 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) { 1008 toc->hdr.first_track = bcd2bin(toc->hdr.first_track); 1009 toc->hdr.last_track = bcd2bin(toc->hdr.last_track); 1010 } 1011 1012 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1; 1013 if (ntracks <= 0) 1014 return -EIO; 1015 if (ntracks > MAX_TRACKS) 1016 ntracks = MAX_TRACKS; 1017 1018 /* now read the whole schmeer */ 1019 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0, 1020 (char *)&toc->hdr, 1021 sizeof(struct atapi_toc_header) + 1022 (ntracks + 1) * 1023 sizeof(struct atapi_toc_entry), sense); 1024 1025 if (stat && toc->hdr.first_track > 1) { 1026 /* 1027 * Cds with CDI tracks only don't have any TOC entries, despite 1028 * of this the returned values are 1029 * first_track == last_track = number of CDI tracks + 1, 1030 * so that this case is indistinguishable from the same layout 1031 * plus an additional audio track. If we get an error for the 1032 * regular case, we assume a CDI without additional audio 1033 * tracks. In this case the readable TOC is empty (CDI tracks 1034 * are not included) and only holds the Leadout entry. 1035 * 1036 * Heiko Ei��feldt. 1037 */ 1038 ntracks = 0; 1039 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0, 1040 (char *)&toc->hdr, 1041 sizeof(struct atapi_toc_header) + 1042 (ntracks + 1) * 1043 sizeof(struct atapi_toc_entry), 1044 sense); 1045 if (stat) 1046 return stat; 1047 1048 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) { 1049 toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT); 1050 toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT); 1051 } else { 1052 toc->hdr.first_track = CDROM_LEADOUT; 1053 toc->hdr.last_track = CDROM_LEADOUT; 1054 } 1055 } 1056 1057 if (stat) 1058 return stat; 1059 1060 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length); 1061 1062 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) { 1063 toc->hdr.first_track = bcd2bin(toc->hdr.first_track); 1064 toc->hdr.last_track = bcd2bin(toc->hdr.last_track); 1065 } 1066 1067 for (i = 0; i <= ntracks; i++) { 1068 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) { 1069 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) 1070 toc->ent[i].track = bcd2bin(toc->ent[i].track); 1071 msf_from_bcd(&toc->ent[i].addr.msf); 1072 } 1073 toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute, 1074 toc->ent[i].addr.msf.second, 1075 toc->ent[i].addr.msf.frame); 1076 } 1077 1078 if (toc->hdr.first_track != CDROM_LEADOUT) { 1079 /* read the multisession information */ 1080 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp, 1081 sizeof(ms_tmp), sense); 1082 if (stat) 1083 return stat; 1084 1085 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba); 1086 } else { 1087 ms_tmp.hdr.last_track = CDROM_LEADOUT; 1088 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track; 1089 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */ 1090 } 1091 1092 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) { 1093 /* re-read multisession information using MSF format */ 1094 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp, 1095 sizeof(ms_tmp), sense); 1096 if (stat) 1097 return stat; 1098 1099 msf_from_bcd(&ms_tmp.ent.addr.msf); 1100 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute, 1101 ms_tmp.ent.addr.msf.second, 1102 ms_tmp.ent.addr.msf.frame); 1103 } 1104 1105 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track); 1106 1107 /* now try to get the total cdrom capacity */ 1108 stat = cdrom_get_last_written(cdi, &last_written); 1109 if (!stat && (last_written > toc->capacity)) { 1110 toc->capacity = last_written; 1111 set_capacity(info->disk, toc->capacity * sectors_per_frame); 1112 drive->probed_capacity = toc->capacity * sectors_per_frame; 1113 } 1114 1115 /* Remember that we've read this stuff. */ 1116 drive->atapi_flags |= IDE_AFLAG_TOC_VALID; 1117 1118 return 0; 1119} 1120 1121int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf) 1122{ 1123 struct cdrom_info *info = drive->driver_data; 1124 struct cdrom_device_info *cdi = &info->devinfo; 1125 struct packet_command cgc; 1126 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE; 1127 1128 ide_debug_log(IDE_DBG_FUNC, "enter"); 1129 1130 if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0) 1131 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE; 1132 1133 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN); 1134 do { 1135 /* we seem to get stat=0x01,err=0x00 the first time (??) */ 1136 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0); 1137 if (!stat) 1138 break; 1139 } while (--attempts); 1140 return stat; 1141} 1142 1143void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf) 1144{ 1145 struct cdrom_info *cd = drive->driver_data; 1146 u16 curspeed, maxspeed; 1147 1148 ide_debug_log(IDE_DBG_FUNC, "enter"); 1149 1150 if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) { 1151 curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]); 1152 maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]); 1153 } else { 1154 curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]); 1155 maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]); 1156 } 1157 1158 ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u", 1159 curspeed, maxspeed); 1160 1161 cd->current_speed = DIV_ROUND_CLOSEST(curspeed, 176); 1162 cd->max_speed = DIV_ROUND_CLOSEST(maxspeed, 176); 1163} 1164 1165#define IDE_CD_CAPABILITIES \ 1166 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \ 1167 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \ 1168 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \ 1169 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \ 1170 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM) 1171 1172static struct cdrom_device_ops ide_cdrom_dops = { 1173 .open = ide_cdrom_open_real, 1174 .release = ide_cdrom_release_real, 1175 .drive_status = ide_cdrom_drive_status, 1176 .media_changed = ide_cdrom_check_media_change_real, 1177 .tray_move = ide_cdrom_tray_move, 1178 .lock_door = ide_cdrom_lock_door, 1179 .select_speed = ide_cdrom_select_speed, 1180 .get_last_session = ide_cdrom_get_last_session, 1181 .get_mcn = ide_cdrom_get_mcn, 1182 .reset = ide_cdrom_reset, 1183 .audio_ioctl = ide_cdrom_audio_ioctl, 1184 .capability = IDE_CD_CAPABILITIES, 1185 .generic_packet = ide_cdrom_packet, 1186}; 1187 1188static int ide_cdrom_register(ide_drive_t *drive, int nslots) 1189{ 1190 struct cdrom_info *info = drive->driver_data; 1191 struct cdrom_device_info *devinfo = &info->devinfo; 1192 1193 ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots); 1194 1195 devinfo->ops = &ide_cdrom_dops; 1196 devinfo->speed = info->current_speed; 1197 devinfo->capacity = nslots; 1198 devinfo->handle = drive; 1199 strcpy(devinfo->name, drive->name); 1200 1201 if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT) 1202 devinfo->mask |= CDC_SELECT_SPEED; 1203 1204 devinfo->disk = info->disk; 1205 return register_cdrom(devinfo); 1206} 1207 1208static int ide_cdrom_probe_capabilities(ide_drive_t *drive) 1209{ 1210 struct cdrom_info *cd = drive->driver_data; 1211 struct cdrom_device_info *cdi = &cd->devinfo; 1212 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE]; 1213 mechtype_t mechtype; 1214 int nslots = 1; 1215 1216 ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx", 1217 drive->media, drive->atapi_flags); 1218 1219 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R | 1220 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO | 1221 CDC_MO_DRIVE | CDC_RAM); 1222 1223 if (drive->media == ide_optical) { 1224 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM); 1225 printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n", 1226 drive->name); 1227 return nslots; 1228 } 1229 1230 if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) { 1231 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT; 1232 cdi->mask &= ~CDC_PLAY_AUDIO; 1233 return nslots; 1234 } 1235 1236 /* 1237 * We have to cheat a little here. the packet will eventually be queued 1238 * with ide_cdrom_packet(), which extracts the drive from cdi->handle. 1239 * Since this device hasn't been registered with the Uniform layer yet, 1240 * it can't do this. Same goes for cdi->ops. 1241 */ 1242 cdi->handle = drive; 1243 cdi->ops = &ide_cdrom_dops; 1244 1245 if (ide_cdrom_get_capabilities(drive, buf)) 1246 return 0; 1247 1248 if ((buf[8 + 6] & 0x01) == 0) 1249 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; 1250 if (buf[8 + 6] & 0x08) 1251 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT; 1252 if (buf[8 + 3] & 0x01) 1253 cdi->mask &= ~CDC_CD_R; 1254 if (buf[8 + 3] & 0x02) 1255 cdi->mask &= ~(CDC_CD_RW | CDC_RAM); 1256 if (buf[8 + 2] & 0x38) 1257 cdi->mask &= ~CDC_DVD; 1258 if (buf[8 + 3] & 0x20) 1259 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM); 1260 if (buf[8 + 3] & 0x10) 1261 cdi->mask &= ~CDC_DVD_R; 1262 if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK)) 1263 cdi->mask &= ~CDC_PLAY_AUDIO; 1264 1265 mechtype = buf[8 + 6] >> 5; 1266 if (mechtype == mechtype_caddy || 1267 mechtype == mechtype_popup || 1268 (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE)) 1269 cdi->mask |= CDC_CLOSE_TRAY; 1270 1271 if (cdi->sanyo_slot > 0) { 1272 cdi->mask &= ~CDC_SELECT_DISC; 1273 nslots = 3; 1274 } else if (mechtype == mechtype_individual_changer || 1275 mechtype == mechtype_cartridge_changer) { 1276 nslots = cdrom_number_of_slots(cdi); 1277 if (nslots > 1) 1278 cdi->mask &= ~CDC_SELECT_DISC; 1279 } 1280 1281 ide_cdrom_update_speed(drive, buf); 1282 1283 printk(KERN_INFO PFX "%s: ATAPI", drive->name); 1284 1285 /* don't print speed if the drive reported 0 */ 1286 if (cd->max_speed) 1287 printk(KERN_CONT " %dX", cd->max_speed); 1288 1289 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM"); 1290 1291 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0) 1292 printk(KERN_CONT " DVD%s%s", 1293 (cdi->mask & CDC_DVD_R) ? "" : "-R", 1294 (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM"); 1295 1296 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0) 1297 printk(KERN_CONT " CD%s%s", 1298 (cdi->mask & CDC_CD_R) ? "" : "-R", 1299 (cdi->mask & CDC_CD_RW) ? "" : "/RW"); 1300 1301 if ((cdi->mask & CDC_SELECT_DISC) == 0) 1302 printk(KERN_CONT " changer w/%d slots", nslots); 1303 else 1304 printk(KERN_CONT " drive"); 1305 1306 printk(KERN_CONT ", %dkB Cache\n", 1307 be16_to_cpup((__be16 *)&buf[8 + 12])); 1308 1309 return nslots; 1310} 1311 1312/* standard prep_rq_fn that builds 10 byte cmds */ 1313static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq) 1314{ 1315 int hard_sect = queue_logical_block_size(q); 1316 long block = (long)blk_rq_pos(rq) / (hard_sect >> 9); 1317 unsigned long blocks = blk_rq_sectors(rq) / (hard_sect >> 9); 1318 1319 memset(rq->cmd, 0, BLK_MAX_CDB); 1320 1321 if (rq_data_dir(rq) == READ) 1322 rq->cmd[0] = GPCMD_READ_10; 1323 else 1324 rq->cmd[0] = GPCMD_WRITE_10; 1325 1326 /* 1327 * fill in lba 1328 */ 1329 rq->cmd[2] = (block >> 24) & 0xff; 1330 rq->cmd[3] = (block >> 16) & 0xff; 1331 rq->cmd[4] = (block >> 8) & 0xff; 1332 rq->cmd[5] = block & 0xff; 1333 1334 /* 1335 * and transfer length 1336 */ 1337 rq->cmd[7] = (blocks >> 8) & 0xff; 1338 rq->cmd[8] = blocks & 0xff; 1339 rq->cmd_len = 10; 1340 return BLKPREP_OK; 1341} 1342 1343/* 1344 * Most of the SCSI commands are supported directly by ATAPI devices. 1345 * This transform handles the few exceptions. 1346 */ 1347static int ide_cdrom_prep_pc(struct request *rq) 1348{ 1349 u8 *c = rq->cmd; 1350 1351 /* transform 6-byte read/write commands to the 10-byte version */ 1352 if (c[0] == READ_6 || c[0] == WRITE_6) { 1353 c[8] = c[4]; 1354 c[5] = c[3]; 1355 c[4] = c[2]; 1356 c[3] = c[1] & 0x1f; 1357 c[2] = 0; 1358 c[1] &= 0xe0; 1359 c[0] += (READ_10 - READ_6); 1360 rq->cmd_len = 10; 1361 return BLKPREP_OK; 1362 } 1363 1364 /* 1365 * it's silly to pretend we understand 6-byte sense commands, just 1366 * reject with ILLEGAL_REQUEST and the caller should take the 1367 * appropriate action 1368 */ 1369 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) { 1370 rq->errors = ILLEGAL_REQUEST; 1371 return BLKPREP_KILL; 1372 } 1373 1374 return BLKPREP_OK; 1375} 1376 1377static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq) 1378{ 1379 if (rq->cmd_type == REQ_TYPE_FS) 1380 return ide_cdrom_prep_fs(q, rq); 1381 else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) 1382 return ide_cdrom_prep_pc(rq); 1383 1384 return 0; 1385} 1386 1387struct cd_list_entry { 1388 const char *id_model; 1389 const char *id_firmware; 1390 unsigned int cd_flags; 1391}; 1392 1393#ifdef CONFIG_IDE_PROC_FS 1394static sector_t ide_cdrom_capacity(ide_drive_t *drive) 1395{ 1396 unsigned long capacity, sectors_per_frame; 1397 1398 if (cdrom_read_capacity(drive, &capacity, §ors_per_frame, NULL)) 1399 return 0; 1400 1401 return capacity * sectors_per_frame; 1402} 1403 1404static int idecd_capacity_proc_show(struct seq_file *m, void *v) 1405{ 1406 ide_drive_t *drive = m->private; 1407 1408 seq_printf(m, "%llu\n", (long long)ide_cdrom_capacity(drive)); 1409 return 0; 1410} 1411 1412static int idecd_capacity_proc_open(struct inode *inode, struct file *file) 1413{ 1414 return single_open(file, idecd_capacity_proc_show, PDE(inode)->data); 1415} 1416 1417static const struct file_operations idecd_capacity_proc_fops = { 1418 .owner = THIS_MODULE, 1419 .open = idecd_capacity_proc_open, 1420 .read = seq_read, 1421 .llseek = seq_lseek, 1422 .release = single_release, 1423}; 1424 1425static ide_proc_entry_t idecd_proc[] = { 1426 { "capacity", S_IFREG|S_IRUGO, &idecd_capacity_proc_fops }, 1427 {} 1428}; 1429 1430static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive) 1431{ 1432 return idecd_proc; 1433} 1434 1435static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive) 1436{ 1437 return NULL; 1438} 1439#endif 1440 1441static const struct cd_list_entry ide_cd_quirks_list[] = { 1442 /* SCR-3231 doesn't support the SET_CD_SPEED command. */ 1443 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT }, 1444 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */ 1445 { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD | 1446 IDE_AFLAG_PRE_ATAPI12, }, 1447 /* Vertos 300, some versions of this drive like to talk BCD. */ 1448 { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, }, 1449 /* Vertos 600 ESD. */ 1450 { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, }, 1451 /* 1452 * Sanyo 3 CD changer uses a non-standard command for CD changing 1453 * (by default standard ATAPI support for CD changers is used). 1454 */ 1455 { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD }, 1456 { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD }, 1457 { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD }, 1458 /* Stingray 8X CD-ROM. */ 1459 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 }, 1460 /* 1461 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length 1462 * mode sense page capabilities size, but older drives break. 1463 */ 1464 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE }, 1465 { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE }, 1466 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */ 1467 { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS }, 1468 /* 1469 * Some drives used by Apple don't advertise audio play 1470 * but they do support reading TOC & audio datas. 1471 */ 1472 { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK }, 1473 { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK }, 1474 { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK }, 1475 { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK }, 1476 { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK }, 1477 { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK }, 1478 { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE }, 1479 { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE }, 1480 { NULL, NULL, 0 } 1481}; 1482 1483static unsigned int ide_cd_flags(u16 *id) 1484{ 1485 const struct cd_list_entry *cle = ide_cd_quirks_list; 1486 1487 while (cle->id_model) { 1488 if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 && 1489 (cle->id_firmware == NULL || 1490 strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware))) 1491 return cle->cd_flags; 1492 cle++; 1493 } 1494 1495 return 0; 1496} 1497 1498static int ide_cdrom_setup(ide_drive_t *drive) 1499{ 1500 struct cdrom_info *cd = drive->driver_data; 1501 struct cdrom_device_info *cdi = &cd->devinfo; 1502 struct request_queue *q = drive->queue; 1503 u16 *id = drive->id; 1504 char *fw_rev = (char *)&id[ATA_ID_FW_REV]; 1505 int nslots; 1506 1507 ide_debug_log(IDE_DBG_PROBE, "enter"); 1508 1509 blk_queue_prep_rq(q, ide_cdrom_prep_fn); 1510 blk_queue_dma_alignment(q, 31); 1511 blk_queue_update_dma_pad(q, 15); 1512 1513 q->unplug_delay = max((1 * HZ) / 1000, 1); 1514 1515 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED; 1516 drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id); 1517 1518 if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) && 1519 fw_rev[4] == '1' && fw_rev[6] <= '2') 1520 drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD | 1521 IDE_AFLAG_TOCADDR_AS_BCD); 1522 else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) && 1523 fw_rev[4] == '1' && fw_rev[6] <= '2') 1524 drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD; 1525 else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD) 1526 /* 3 => use CD in slot 0 */ 1527 cdi->sanyo_slot = 3; 1528 1529 nslots = ide_cdrom_probe_capabilities(drive); 1530 1531 blk_queue_logical_block_size(q, CD_FRAMESIZE); 1532 1533 if (ide_cdrom_register(drive, nslots)) { 1534 printk(KERN_ERR PFX "%s: %s failed to register device with the" 1535 " cdrom driver.\n", drive->name, __func__); 1536 cd->devinfo.handle = NULL; 1537 return 1; 1538 } 1539 1540 ide_proc_register_driver(drive, cd->driver); 1541 return 0; 1542} 1543 1544static void ide_cd_remove(ide_drive_t *drive) 1545{ 1546 struct cdrom_info *info = drive->driver_data; 1547 1548 ide_debug_log(IDE_DBG_FUNC, "enter"); 1549 1550 ide_proc_unregister_driver(drive, info->driver); 1551 device_del(&info->dev); 1552 del_gendisk(info->disk); 1553 1554 mutex_lock(&idecd_ref_mutex); 1555 put_device(&info->dev); 1556 mutex_unlock(&idecd_ref_mutex); 1557} 1558 1559static void ide_cd_release(struct device *dev) 1560{ 1561 struct cdrom_info *info = to_ide_drv(dev, cdrom_info); 1562 struct cdrom_device_info *devinfo = &info->devinfo; 1563 ide_drive_t *drive = info->drive; 1564 struct gendisk *g = info->disk; 1565 1566 ide_debug_log(IDE_DBG_FUNC, "enter"); 1567 1568 kfree(info->toc); 1569 if (devinfo->handle == drive) 1570 unregister_cdrom(devinfo); 1571 drive->driver_data = NULL; 1572 blk_queue_prep_rq(drive->queue, NULL); 1573 g->private_data = NULL; 1574 put_disk(g); 1575 kfree(info); 1576} 1577 1578static int ide_cd_probe(ide_drive_t *); 1579 1580static struct ide_driver ide_cdrom_driver = { 1581 .gen_driver = { 1582 .owner = THIS_MODULE, 1583 .name = "ide-cdrom", 1584 .bus = &ide_bus_type, 1585 }, 1586 .probe = ide_cd_probe, 1587 .remove = ide_cd_remove, 1588 .version = IDECD_VERSION, 1589 .do_request = ide_cd_do_request, 1590#ifdef CONFIG_IDE_PROC_FS 1591 .proc_entries = ide_cd_proc_entries, 1592 .proc_devsets = ide_cd_proc_devsets, 1593#endif 1594}; 1595 1596static int idecd_open(struct block_device *bdev, fmode_t mode) 1597{ 1598 struct cdrom_info *info; 1599 int rc = -ENXIO; 1600 1601 lock_kernel(); 1602 info = ide_cd_get(bdev->bd_disk); 1603 if (!info) 1604 goto out; 1605 1606 rc = cdrom_open(&info->devinfo, bdev, mode); 1607 if (rc < 0) 1608 ide_cd_put(info); 1609out: 1610 unlock_kernel(); 1611 return rc; 1612} 1613 1614static int idecd_release(struct gendisk *disk, fmode_t mode) 1615{ 1616 struct cdrom_info *info = ide_drv_g(disk, cdrom_info); 1617 1618 lock_kernel(); 1619 cdrom_release(&info->devinfo, mode); 1620 1621 ide_cd_put(info); 1622 unlock_kernel(); 1623 1624 return 0; 1625} 1626 1627static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg) 1628{ 1629 struct packet_command cgc; 1630 char buffer[16]; 1631 int stat; 1632 char spindown; 1633 1634 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char))) 1635 return -EFAULT; 1636 1637 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN); 1638 1639 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0); 1640 if (stat) 1641 return stat; 1642 1643 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f); 1644 return cdrom_mode_select(cdi, &cgc); 1645} 1646 1647static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg) 1648{ 1649 struct packet_command cgc; 1650 char buffer[16]; 1651 int stat; 1652 char spindown; 1653 1654 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN); 1655 1656 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0); 1657 if (stat) 1658 return stat; 1659 1660 spindown = buffer[11] & 0x0f; 1661 if (copy_to_user((void __user *)arg, &spindown, sizeof(char))) 1662 return -EFAULT; 1663 return 0; 1664} 1665 1666static int idecd_locked_ioctl(struct block_device *bdev, fmode_t mode, 1667 unsigned int cmd, unsigned long arg) 1668{ 1669 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info); 1670 int err; 1671 1672 switch (cmd) { 1673 case CDROMSETSPINDOWN: 1674 return idecd_set_spindown(&info->devinfo, arg); 1675 case CDROMGETSPINDOWN: 1676 return idecd_get_spindown(&info->devinfo, arg); 1677 default: 1678 break; 1679 } 1680 1681 err = generic_ide_ioctl(info->drive, bdev, cmd, arg); 1682 if (err == -EINVAL) 1683 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg); 1684 1685 return err; 1686} 1687 1688static int idecd_ioctl(struct block_device *bdev, fmode_t mode, 1689 unsigned int cmd, unsigned long arg) 1690{ 1691 int ret; 1692 1693 lock_kernel(); 1694 ret = idecd_locked_ioctl(bdev, mode, cmd, arg); 1695 unlock_kernel(); 1696 1697 return ret; 1698} 1699 1700 1701static int idecd_media_changed(struct gendisk *disk) 1702{ 1703 struct cdrom_info *info = ide_drv_g(disk, cdrom_info); 1704 return cdrom_media_changed(&info->devinfo); 1705} 1706 1707static int idecd_revalidate_disk(struct gendisk *disk) 1708{ 1709 struct cdrom_info *info = ide_drv_g(disk, cdrom_info); 1710 struct request_sense sense; 1711 1712 ide_cd_read_toc(info->drive, &sense); 1713 1714 return 0; 1715} 1716 1717static const struct block_device_operations idecd_ops = { 1718 .owner = THIS_MODULE, 1719 .open = idecd_open, 1720 .release = idecd_release, 1721 .ioctl = idecd_ioctl, 1722 .media_changed = idecd_media_changed, 1723 .revalidate_disk = idecd_revalidate_disk 1724}; 1725 1726/* module options */ 1727static unsigned long debug_mask; 1728module_param(debug_mask, ulong, 0644); 1729 1730MODULE_DESCRIPTION("ATAPI CD-ROM Driver"); 1731 1732static int ide_cd_probe(ide_drive_t *drive) 1733{ 1734 struct cdrom_info *info; 1735 struct gendisk *g; 1736 struct request_sense sense; 1737 1738 ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x", 1739 drive->driver_req, drive->media); 1740 1741 if (!strstr("ide-cdrom", drive->driver_req)) 1742 goto failed; 1743 1744 if (drive->media != ide_cdrom && drive->media != ide_optical) 1745 goto failed; 1746 1747 drive->debug_mask = debug_mask; 1748 drive->irq_handler = cdrom_newpc_intr; 1749 1750 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL); 1751 if (info == NULL) { 1752 printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n", 1753 drive->name); 1754 goto failed; 1755 } 1756 1757 g = alloc_disk(1 << PARTN_BITS); 1758 if (!g) 1759 goto out_free_cd; 1760 1761 ide_init_disk(g, drive); 1762 1763 info->dev.parent = &drive->gendev; 1764 info->dev.release = ide_cd_release; 1765 dev_set_name(&info->dev, dev_name(&drive->gendev)); 1766 1767 if (device_register(&info->dev)) 1768 goto out_free_disk; 1769 1770 info->drive = drive; 1771 info->driver = &ide_cdrom_driver; 1772 info->disk = g; 1773 1774 g->private_data = &info->driver; 1775 1776 drive->driver_data = info; 1777 1778 g->minors = 1; 1779 g->driverfs_dev = &drive->gendev; 1780 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE; 1781 if (ide_cdrom_setup(drive)) { 1782 put_device(&info->dev); 1783 goto failed; 1784 } 1785 1786 ide_cd_read_toc(drive, &sense); 1787 g->fops = &idecd_ops; 1788 g->flags |= GENHD_FL_REMOVABLE; 1789 add_disk(g); 1790 return 0; 1791 1792out_free_disk: 1793 put_disk(g); 1794out_free_cd: 1795 kfree(info); 1796failed: 1797 return -ENODEV; 1798} 1799 1800static void __exit ide_cdrom_exit(void) 1801{ 1802 driver_unregister(&ide_cdrom_driver.gen_driver); 1803} 1804 1805static int __init ide_cdrom_init(void) 1806{ 1807 printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n"); 1808 return driver_register(&ide_cdrom_driver.gen_driver); 1809} 1810 1811MODULE_ALIAS("ide:*m-cdrom*"); 1812MODULE_ALIAS("ide-cd"); 1813module_init(ide_cdrom_init); 1814module_exit(ide_cdrom_exit); 1815MODULE_LICENSE("GPL"); 1816