11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * sr.c Copyright (C) 1992 David Giller 31da177e4SLinus Torvalds * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * adapted from: 61da177e4SLinus Torvalds * sd.c Copyright (C) 1992 Drew Eckhardt 71da177e4SLinus Torvalds * Linux scsi disk driver by 81da177e4SLinus Torvalds * Drew Eckhardt <drew@colorado.edu> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Modified by Eric Youngdale ericy@andante.org to 111da177e4SLinus Torvalds * add scatter-gather, multiple outstanding request, and other 121da177e4SLinus Torvalds * enhancements. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Modified by Eric Youngdale eric@andante.org to support loadable 151da177e4SLinus Torvalds * low-level scsi drivers. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to 181da177e4SLinus Torvalds * provide auto-eject. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the 211da177e4SLinus Torvalds * generic cdrom interface 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() 241da177e4SLinus Torvalds * interface, capabilities probe additions, ioctl cleanups, etc. 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM 291da177e4SLinus Torvalds * transparently and lose the GHOST hack 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 321da177e4SLinus Torvalds * check resource allocation in sr_init and some cleanups 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include <linux/module.h> 361da177e4SLinus Torvalds #include <linux/fs.h> 371da177e4SLinus Torvalds #include <linux/kernel.h> 381da177e4SLinus Torvalds #include <linux/sched.h> 391da177e4SLinus Torvalds #include <linux/mm.h> 401da177e4SLinus Torvalds #include <linux/bio.h> 411da177e4SLinus Torvalds #include <linux/string.h> 421da177e4SLinus Torvalds #include <linux/errno.h> 431da177e4SLinus Torvalds #include <linux/cdrom.h> 441da177e4SLinus Torvalds #include <linux/interrupt.h> 451da177e4SLinus Torvalds #include <linux/init.h> 461da177e4SLinus Torvalds #include <linux/blkdev.h> 471da177e4SLinus Torvalds #include <asm/uaccess.h> 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #include <scsi/scsi.h> 501da177e4SLinus Torvalds #include <scsi/scsi_dbg.h> 511da177e4SLinus Torvalds #include <scsi/scsi_device.h> 521da177e4SLinus Torvalds #include <scsi/scsi_driver.h> 531da177e4SLinus Torvalds #include <scsi/scsi_eh.h> 541da177e4SLinus Torvalds #include <scsi/scsi_host.h> 551da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ 561da177e4SLinus Torvalds #include <scsi/scsi_request.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #include "scsi_logging.h" 591da177e4SLinus Torvalds #include "sr.h" 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #define SR_DISKS 256 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #define MAX_RETRIES 3 651da177e4SLinus Torvalds #define SR_TIMEOUT (30 * HZ) 661da177e4SLinus Torvalds #define SR_CAPABILITIES \ 671da177e4SLinus Torvalds (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ 681da177e4SLinus Torvalds CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ 691da177e4SLinus Torvalds CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \ 701da177e4SLinus Torvalds CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ 711da177e4SLinus Torvalds CDC_MRW|CDC_MRW_W|CDC_RAM) 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds static int sr_probe(struct device *); 741da177e4SLinus Torvalds static int sr_remove(struct device *); 751da177e4SLinus Torvalds static int sr_init_command(struct scsi_cmnd *); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static struct scsi_driver sr_template = { 781da177e4SLinus Torvalds .owner = THIS_MODULE, 791da177e4SLinus Torvalds .gendrv = { 801da177e4SLinus Torvalds .name = "sr", 811da177e4SLinus Torvalds .probe = sr_probe, 821da177e4SLinus Torvalds .remove = sr_remove, 831da177e4SLinus Torvalds }, 841da177e4SLinus Torvalds .init_command = sr_init_command, 851da177e4SLinus Torvalds }; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; 881da177e4SLinus Torvalds static DEFINE_SPINLOCK(sr_index_lock); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* This semaphore is used to mediate the 0->1 reference get in the 911da177e4SLinus Torvalds * face of object destruction (i.e. we can't allow a get on an 921da177e4SLinus Torvalds * object after last put) */ 931da177e4SLinus Torvalds static DECLARE_MUTEX(sr_ref_sem); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *, int); 961da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *); 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *); 991da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *); 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static int sr_media_change(struct cdrom_device_info *, int); 1021da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *, struct packet_command *); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static struct cdrom_device_ops sr_dops = { 1051da177e4SLinus Torvalds .open = sr_open, 1061da177e4SLinus Torvalds .release = sr_release, 1071da177e4SLinus Torvalds .drive_status = sr_drive_status, 1081da177e4SLinus Torvalds .media_changed = sr_media_change, 1091da177e4SLinus Torvalds .tray_move = sr_tray_move, 1101da177e4SLinus Torvalds .lock_door = sr_lock_door, 1111da177e4SLinus Torvalds .select_speed = sr_select_speed, 1121da177e4SLinus Torvalds .get_last_session = sr_get_last_session, 1131da177e4SLinus Torvalds .get_mcn = sr_get_mcn, 1141da177e4SLinus Torvalds .reset = sr_reset, 1151da177e4SLinus Torvalds .audio_ioctl = sr_audio_ioctl, 1161da177e4SLinus Torvalds .dev_ioctl = sr_dev_ioctl, 1171da177e4SLinus Torvalds .capability = SR_CAPABILITIES, 1181da177e4SLinus Torvalds .generic_packet = sr_packet, 1191da177e4SLinus Torvalds }; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref); 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd(struct gendisk *disk) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds return container_of(disk->private_data, struct scsi_cd, driver); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * The get and put routines for the struct scsi_cd. Note this entity 1301da177e4SLinus Torvalds * has a scsi_device pointer and owns a reference to this. 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct scsi_cd *cd = NULL; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds down(&sr_ref_sem); 1371da177e4SLinus Torvalds if (disk->private_data == NULL) 1381da177e4SLinus Torvalds goto out; 1391da177e4SLinus Torvalds cd = scsi_cd(disk); 1401da177e4SLinus Torvalds kref_get(&cd->kref); 1411da177e4SLinus Torvalds if (scsi_device_get(cd->device)) 1421da177e4SLinus Torvalds goto out_put; 1431da177e4SLinus Torvalds goto out; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds out_put: 1461da177e4SLinus Torvalds kref_put(&cd->kref, sr_kref_release); 1471da177e4SLinus Torvalds cd = NULL; 1481da177e4SLinus Torvalds out: 1491da177e4SLinus Torvalds up(&sr_ref_sem); 1501da177e4SLinus Torvalds return cd; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static inline void scsi_cd_put(struct scsi_cd *cd) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds struct scsi_device *sdev = cd->device; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds down(&sr_ref_sem); 1581da177e4SLinus Torvalds kref_put(&cd->kref, sr_kref_release); 1591da177e4SLinus Torvalds scsi_device_put(sdev); 1601da177e4SLinus Torvalds up(&sr_ref_sem); 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * This function checks to see if the media has been changed in the 1651da177e4SLinus Torvalds * CDROM drive. It is possible that we have already sensed a change, 1661da177e4SLinus Torvalds * or the drive may have sensed one and not yet reported it. We must 1671da177e4SLinus Torvalds * be ready for either case. This function always reports the current 1681da177e4SLinus Torvalds * value of the changed bit. If flag is 0, then the changed bit is reset. 1691da177e4SLinus Torvalds * This function could be done as an ioctl, but we would need to have 1701da177e4SLinus Torvalds * an inode for that to work, and we do not always have one. 1711da177e4SLinus Torvalds */ 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds int sr_media_change(struct cdrom_device_info *cdi, int slot) 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds struct scsi_cd *cd = cdi->handle; 1761da177e4SLinus Torvalds int retval; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds if (CDSL_CURRENT != slot) { 1791da177e4SLinus Torvalds /* no changer support */ 1801da177e4SLinus Torvalds return -EINVAL; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES); 1841da177e4SLinus Torvalds if (retval) { 1851da177e4SLinus Torvalds /* Unable to test, unit probably not ready. This usually 1861da177e4SLinus Torvalds * means there is no disc in the drive. Mark as changed, 1871da177e4SLinus Torvalds * and we will figure it out later once the drive is 1881da177e4SLinus Torvalds * available again. */ 1891da177e4SLinus Torvalds cd->device->changed = 1; 1901da177e4SLinus Torvalds return 1; /* This will force a flush, if called from 1911da177e4SLinus Torvalds * check_disk_change */ 1921da177e4SLinus Torvalds }; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds retval = cd->device->changed; 1951da177e4SLinus Torvalds cd->device->changed = 0; 1961da177e4SLinus Torvalds /* If the disk changed, the capacity will now be different, 1971da177e4SLinus Torvalds * so we force a re-read of this information */ 1981da177e4SLinus Torvalds if (retval) { 1991da177e4SLinus Torvalds /* check multisession offset etc */ 2001da177e4SLinus Torvalds sr_cd_check(cdi); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* 2031da177e4SLinus Torvalds * If the disk changed, the capacity will now be different, 2041da177e4SLinus Torvalds * so we force a re-read of this information 2051da177e4SLinus Torvalds * Force 2048 for the sector size so that filesystems won't 2061da177e4SLinus Torvalds * be trying to use something that is too small if the disc 2071da177e4SLinus Torvalds * has changed. 2081da177e4SLinus Torvalds */ 2091da177e4SLinus Torvalds cd->needs_sector_size = 1; 2101da177e4SLinus Torvalds cd->device->sector_size = 2048; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds return retval; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* 2161da177e4SLinus Torvalds * rw_intr is the interrupt routine for the device driver. 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * It will be notified on the end of a SCSI read / write, and will take on 2191da177e4SLinus Torvalds * of several actions based on success or failure. 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds static void rw_intr(struct scsi_cmnd * SCpnt) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds int result = SCpnt->result; 2241da177e4SLinus Torvalds int this_count = SCpnt->bufflen; 2251da177e4SLinus Torvalds int good_bytes = (result == 0 ? this_count : 0); 2261da177e4SLinus Torvalds int block_sectors = 0; 2271da177e4SLinus Torvalds long error_sector; 2281da177e4SLinus Torvalds struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds #ifdef DEBUG 2311da177e4SLinus Torvalds printk("sr.c done: %x\n", result); 2321da177e4SLinus Torvalds #endif 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* 2351da177e4SLinus Torvalds * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial 2361da177e4SLinus Torvalds * success. Since this is a relatively rare error condition, no 2371da177e4SLinus Torvalds * care is taken to avoid unnecessary additional work such as 2381da177e4SLinus Torvalds * memcpy's that could be avoided. 2391da177e4SLinus Torvalds */ 2401da177e4SLinus Torvalds if (driver_byte(result) != 0 && /* An error occurred */ 2411da177e4SLinus Torvalds (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ 2421da177e4SLinus Torvalds switch (SCpnt->sense_buffer[2]) { 2431da177e4SLinus Torvalds case MEDIUM_ERROR: 2441da177e4SLinus Torvalds case VOLUME_OVERFLOW: 2451da177e4SLinus Torvalds case ILLEGAL_REQUEST: 2461da177e4SLinus Torvalds if (!(SCpnt->sense_buffer[0] & 0x90)) 2471da177e4SLinus Torvalds break; 2481da177e4SLinus Torvalds if (!blk_fs_request(SCpnt->request)) 2491da177e4SLinus Torvalds break; 2501da177e4SLinus Torvalds error_sector = (SCpnt->sense_buffer[3] << 24) | 2511da177e4SLinus Torvalds (SCpnt->sense_buffer[4] << 16) | 2521da177e4SLinus Torvalds (SCpnt->sense_buffer[5] << 8) | 2531da177e4SLinus Torvalds SCpnt->sense_buffer[6]; 2541da177e4SLinus Torvalds if (SCpnt->request->bio != NULL) 2551da177e4SLinus Torvalds block_sectors = 2561da177e4SLinus Torvalds bio_sectors(SCpnt->request->bio); 2571da177e4SLinus Torvalds if (block_sectors < 4) 2581da177e4SLinus Torvalds block_sectors = 4; 2591da177e4SLinus Torvalds if (cd->device->sector_size == 2048) 2601da177e4SLinus Torvalds error_sector <<= 2; 2611da177e4SLinus Torvalds error_sector &= ~(block_sectors - 1); 2621da177e4SLinus Torvalds good_bytes = (error_sector - SCpnt->request->sector) << 9; 2631da177e4SLinus Torvalds if (good_bytes < 0 || good_bytes >= this_count) 2641da177e4SLinus Torvalds good_bytes = 0; 2651da177e4SLinus Torvalds /* 2661da177e4SLinus Torvalds * The SCSI specification allows for the value 2671da177e4SLinus Torvalds * returned by READ CAPACITY to be up to 75 2K 2681da177e4SLinus Torvalds * sectors past the last readable block. 2691da177e4SLinus Torvalds * Therefore, if we hit a medium error within the 2701da177e4SLinus Torvalds * last 75 2K sectors, we decrease the saved size 2711da177e4SLinus Torvalds * value. 2721da177e4SLinus Torvalds */ 2731da177e4SLinus Torvalds if (error_sector < get_capacity(cd->disk) && 2741da177e4SLinus Torvalds cd->capacity - error_sector < 4 * 75) 2751da177e4SLinus Torvalds set_capacity(cd->disk, error_sector); 2761da177e4SLinus Torvalds break; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds case RECOVERED_ERROR: 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* 2811da177e4SLinus Torvalds * An error occured, but it recovered. Inform the 2821da177e4SLinus Torvalds * user, but make sure that it's not treated as a 2831da177e4SLinus Torvalds * hard error. 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds scsi_print_sense("sr", SCpnt); 2861da177e4SLinus Torvalds SCpnt->result = 0; 2871da177e4SLinus Torvalds SCpnt->sense_buffer[0] = 0x0; 2881da177e4SLinus Torvalds good_bytes = this_count; 2891da177e4SLinus Torvalds break; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds default: 2921da177e4SLinus Torvalds break; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* 2971da177e4SLinus Torvalds * This calls the generic completion function, now that we know 2981da177e4SLinus Torvalds * how many actual sectors finished, and how many sectors we need 2991da177e4SLinus Torvalds * to say have failed. 3001da177e4SLinus Torvalds */ 3011da177e4SLinus Torvalds scsi_io_completion(SCpnt, good_bytes, block_sectors << 9); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds static int sr_init_command(struct scsi_cmnd * SCpnt) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds int block=0, this_count, s_size, timeout = SR_TIMEOUT; 3071da177e4SLinus Torvalds struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n", 3101da177e4SLinus Torvalds cd->disk->disk_name, block)); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds if (!cd->device || !scsi_device_online(cd->device)) { 3131da177e4SLinus Torvalds SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", 3141da177e4SLinus Torvalds SCpnt->request->nr_sectors)); 3151da177e4SLinus Torvalds SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); 3161da177e4SLinus Torvalds return 0; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds if (cd->device->changed) { 3201da177e4SLinus Torvalds /* 3211da177e4SLinus Torvalds * quietly refuse to do anything to a changed disc until the 3221da177e4SLinus Torvalds * changed bit has been reset 3231da177e4SLinus Torvalds */ 3241da177e4SLinus Torvalds return 0; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* 3281da177e4SLinus Torvalds * these are already setup, just copy cdb basically 3291da177e4SLinus Torvalds */ 3301da177e4SLinus Torvalds if (SCpnt->request->flags & REQ_BLOCK_PC) { 3311da177e4SLinus Torvalds struct request *rq = SCpnt->request; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd)) 3341da177e4SLinus Torvalds return 0; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd)); 3371da177e4SLinus Torvalds if (!rq->data_len) 3381da177e4SLinus Torvalds SCpnt->sc_data_direction = DMA_NONE; 3391da177e4SLinus Torvalds else if (rq_data_dir(rq) == WRITE) 3401da177e4SLinus Torvalds SCpnt->sc_data_direction = DMA_TO_DEVICE; 3411da177e4SLinus Torvalds else 3421da177e4SLinus Torvalds SCpnt->sc_data_direction = DMA_FROM_DEVICE; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds this_count = rq->data_len; 3451da177e4SLinus Torvalds if (rq->timeout) 3461da177e4SLinus Torvalds timeout = rq->timeout; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds SCpnt->transfersize = rq->data_len; 3491da177e4SLinus Torvalds goto queue; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds if (!(SCpnt->request->flags & REQ_CMD)) { 3531da177e4SLinus Torvalds blk_dump_rq_flags(SCpnt->request, "sr unsup command"); 3541da177e4SLinus Torvalds return 0; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * we do lazy blocksize switching (when reading XA sectors, 3591da177e4SLinus Torvalds * see CDROMREADMODE2 ioctl) 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds s_size = cd->device->sector_size; 3621da177e4SLinus Torvalds if (s_size > 2048) { 3631da177e4SLinus Torvalds if (!in_interrupt()) 3641da177e4SLinus Torvalds sr_set_blocklength(cd, 2048); 3651da177e4SLinus Torvalds else 3661da177e4SLinus Torvalds printk("sr: can't switch blocksize: in interrupt\n"); 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (s_size != 512 && s_size != 1024 && s_size != 2048) { 3701da177e4SLinus Torvalds printk("sr: bad sector size %d\n", s_size); 3711da177e4SLinus Torvalds return 0; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (rq_data_dir(SCpnt->request) == WRITE) { 3751da177e4SLinus Torvalds if (!cd->device->writeable) 3761da177e4SLinus Torvalds return 0; 3771da177e4SLinus Torvalds SCpnt->cmnd[0] = WRITE_10; 3781da177e4SLinus Torvalds SCpnt->sc_data_direction = DMA_TO_DEVICE; 3791da177e4SLinus Torvalds cd->cdi.media_written = 1; 3801da177e4SLinus Torvalds } else if (rq_data_dir(SCpnt->request) == READ) { 3811da177e4SLinus Torvalds SCpnt->cmnd[0] = READ_10; 3821da177e4SLinus Torvalds SCpnt->sc_data_direction = DMA_FROM_DEVICE; 3831da177e4SLinus Torvalds } else { 3841da177e4SLinus Torvalds blk_dump_rq_flags(SCpnt->request, "Unknown sr command"); 3851da177e4SLinus Torvalds return 0; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds { 3891da177e4SLinus Torvalds struct scatterlist *sg = SCpnt->request_buffer; 3901da177e4SLinus Torvalds int i, size = 0; 3911da177e4SLinus Torvalds for (i = 0; i < SCpnt->use_sg; i++) 3921da177e4SLinus Torvalds size += sg[i].length; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (size != SCpnt->request_bufflen && SCpnt->use_sg) { 3951da177e4SLinus Torvalds printk(KERN_ERR "sr: mismatch count %d, bytes %d\n", 3961da177e4SLinus Torvalds size, SCpnt->request_bufflen); 3971da177e4SLinus Torvalds if (SCpnt->request_bufflen > size) 3981da177e4SLinus Torvalds SCpnt->request_bufflen = SCpnt->bufflen = size; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* 4031da177e4SLinus Torvalds * request doesn't start on hw block boundary, add scatter pads 4041da177e4SLinus Torvalds */ 4051da177e4SLinus Torvalds if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) || 4061da177e4SLinus Torvalds (SCpnt->request_bufflen % s_size)) { 4071da177e4SLinus Torvalds printk("sr: unaligned transfer\n"); 4081da177e4SLinus Torvalds return 0; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", 4151da177e4SLinus Torvalds cd->cdi.name, 4161da177e4SLinus Torvalds (rq_data_dir(SCpnt->request) == WRITE) ? 4171da177e4SLinus Torvalds "writing" : "reading", 4181da177e4SLinus Torvalds this_count, SCpnt->request->nr_sectors)); 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds SCpnt->cmnd[1] = 0; 4211da177e4SLinus Torvalds block = (unsigned int)SCpnt->request->sector / (s_size >> 9); 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds if (this_count > 0xffff) { 4241da177e4SLinus Torvalds this_count = 0xffff; 4251da177e4SLinus Torvalds SCpnt->request_bufflen = SCpnt->bufflen = 4261da177e4SLinus Torvalds this_count * s_size; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; 4301da177e4SLinus Torvalds SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; 4311da177e4SLinus Torvalds SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; 4321da177e4SLinus Torvalds SCpnt->cmnd[5] = (unsigned char) block & 0xff; 4331da177e4SLinus Torvalds SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; 4341da177e4SLinus Torvalds SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; 4351da177e4SLinus Torvalds SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* 4381da177e4SLinus Torvalds * We shouldn't disconnect in the middle of a sector, so with a dumb 4391da177e4SLinus Torvalds * host adapter, it's safe to assume that we can at least transfer 4401da177e4SLinus Torvalds * this many bytes between each connect / disconnect. 4411da177e4SLinus Torvalds */ 4421da177e4SLinus Torvalds SCpnt->transfersize = cd->device->sector_size; 4431da177e4SLinus Torvalds SCpnt->underflow = this_count << 9; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds queue: 4461da177e4SLinus Torvalds SCpnt->allowed = MAX_RETRIES; 4471da177e4SLinus Torvalds SCpnt->timeout_per_command = timeout; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* 4501da177e4SLinus Torvalds * This is the completion routine we use. This is matched in terms 4511da177e4SLinus Torvalds * of capability to this function. 4521da177e4SLinus Torvalds */ 4531da177e4SLinus Torvalds SCpnt->done = rw_intr; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * This indicates that the command is ready from our end to be 4571da177e4SLinus Torvalds * queued. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds return 1; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds static int sr_block_open(struct inode *inode, struct file *file) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds struct gendisk *disk = inode->i_bdev->bd_disk; 4651da177e4SLinus Torvalds struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); 4661da177e4SLinus Torvalds int ret = 0; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds if(!(cd = scsi_cd_get(disk))) 4691da177e4SLinus Torvalds return -ENXIO; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if((ret = cdrom_open(&cd->cdi, inode, file)) != 0) 4721da177e4SLinus Torvalds scsi_cd_put(cd); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds return ret; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds static int sr_block_release(struct inode *inode, struct file *file) 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds int ret; 4801da177e4SLinus Torvalds struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); 4811da177e4SLinus Torvalds ret = cdrom_release(&cd->cdi, file); 4821da177e4SLinus Torvalds if(ret) 4831da177e4SLinus Torvalds return ret; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds scsi_cd_put(cd); 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds return 0; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd, 4911da177e4SLinus Torvalds unsigned long arg) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); 4941da177e4SLinus Torvalds struct scsi_device *sdev = cd->device; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* 4971da177e4SLinus Torvalds * Send SCSI addressing ioctls directly to mid level, send other 4981da177e4SLinus Torvalds * ioctls to cdrom/block level. 4991da177e4SLinus Torvalds */ 5001da177e4SLinus Torvalds switch (cmd) { 5011da177e4SLinus Torvalds case SCSI_IOCTL_GET_IDLUN: 5021da177e4SLinus Torvalds case SCSI_IOCTL_GET_BUS_NUMBER: 5031da177e4SLinus Torvalds return scsi_ioctl(sdev, cmd, (void __user *)arg); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg); 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds static int sr_block_media_changed(struct gendisk *disk) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds struct scsi_cd *cd = scsi_cd(disk); 5111da177e4SLinus Torvalds return cdrom_media_changed(&cd->cdi); 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds static struct block_device_operations sr_bdops = 5151da177e4SLinus Torvalds { 5161da177e4SLinus Torvalds .owner = THIS_MODULE, 5171da177e4SLinus Torvalds .open = sr_block_open, 5181da177e4SLinus Torvalds .release = sr_block_release, 5191da177e4SLinus Torvalds .ioctl = sr_block_ioctl, 5201da177e4SLinus Torvalds .media_changed = sr_block_media_changed, 5211da177e4SLinus Torvalds /* 5221da177e4SLinus Torvalds * No compat_ioctl for now because sr_block_ioctl never 5231da177e4SLinus Torvalds * seems to pass arbitary ioctls down to host drivers. 5241da177e4SLinus Torvalds */ 5251da177e4SLinus Torvalds }; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *cdi, int purpose) 5281da177e4SLinus Torvalds { 5291da177e4SLinus Torvalds struct scsi_cd *cd = cdi->handle; 5301da177e4SLinus Torvalds struct scsi_device *sdev = cd->device; 5311da177e4SLinus Torvalds int retval; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* 5341da177e4SLinus Torvalds * If the device is in error recovery, wait until it is done. 5351da177e4SLinus Torvalds * If the device is offline, then disallow any access to it. 5361da177e4SLinus Torvalds */ 5371da177e4SLinus Torvalds retval = -ENXIO; 5381da177e4SLinus Torvalds if (!scsi_block_when_processing_errors(sdev)) 5391da177e4SLinus Torvalds goto error_out; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds /* 5421da177e4SLinus Torvalds * If this device did not have media in the drive at boot time, then 5431da177e4SLinus Torvalds * we would have been unable to get the sector size. Check to see if 5441da177e4SLinus Torvalds * this is the case, and try again. 5451da177e4SLinus Torvalds */ 5461da177e4SLinus Torvalds if (cd->needs_sector_size) 5471da177e4SLinus Torvalds get_sectorsize(cd); 5481da177e4SLinus Torvalds return 0; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds error_out: 5511da177e4SLinus Torvalds return retval; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *cdi) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds struct scsi_cd *cd = cdi->handle; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds if (cd->device->sector_size > 2048) 5591da177e4SLinus Torvalds sr_set_blocklength(cd, 2048); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds static int sr_probe(struct device *dev) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 5661da177e4SLinus Torvalds struct gendisk *disk; 5671da177e4SLinus Torvalds struct scsi_cd *cd; 5681da177e4SLinus Torvalds int minor, error; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds error = -ENODEV; 5711da177e4SLinus Torvalds if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM) 5721da177e4SLinus Torvalds goto fail; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds error = -ENOMEM; 5751da177e4SLinus Torvalds cd = kmalloc(sizeof(*cd), GFP_KERNEL); 5761da177e4SLinus Torvalds if (!cd) 5771da177e4SLinus Torvalds goto fail; 5781da177e4SLinus Torvalds memset(cd, 0, sizeof(*cd)); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds kref_init(&cd->kref); 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds disk = alloc_disk(1); 5831da177e4SLinus Torvalds if (!disk) 5841da177e4SLinus Torvalds goto fail_free; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds spin_lock(&sr_index_lock); 5871da177e4SLinus Torvalds minor = find_first_zero_bit(sr_index_bits, SR_DISKS); 5881da177e4SLinus Torvalds if (minor == SR_DISKS) { 5891da177e4SLinus Torvalds spin_unlock(&sr_index_lock); 5901da177e4SLinus Torvalds error = -EBUSY; 5911da177e4SLinus Torvalds goto fail_put; 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds __set_bit(minor, sr_index_bits); 5941da177e4SLinus Torvalds spin_unlock(&sr_index_lock); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds disk->major = SCSI_CDROM_MAJOR; 5971da177e4SLinus Torvalds disk->first_minor = minor; 5981da177e4SLinus Torvalds sprintf(disk->disk_name, "sr%d", minor); 5991da177e4SLinus Torvalds disk->fops = &sr_bdops; 6001da177e4SLinus Torvalds disk->flags = GENHD_FL_CD; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds cd->device = sdev; 6031da177e4SLinus Torvalds cd->disk = disk; 6041da177e4SLinus Torvalds cd->driver = &sr_template; 6051da177e4SLinus Torvalds cd->disk = disk; 6061da177e4SLinus Torvalds cd->capacity = 0x1fffff; 6071da177e4SLinus Torvalds cd->needs_sector_size = 1; 6081da177e4SLinus Torvalds cd->device->changed = 1; /* force recheck CD type */ 6091da177e4SLinus Torvalds cd->use = 1; 6101da177e4SLinus Torvalds cd->readcd_known = 0; 6111da177e4SLinus Torvalds cd->readcd_cdda = 0; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds cd->cdi.ops = &sr_dops; 6141da177e4SLinus Torvalds cd->cdi.handle = cd; 6151da177e4SLinus Torvalds cd->cdi.mask = 0; 6161da177e4SLinus Torvalds cd->cdi.capacity = 1; 6171da177e4SLinus Torvalds sprintf(cd->cdi.name, "sr%d", minor); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds sdev->sector_size = 2048; /* A guess, just in case */ 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* FIXME: need to handle a get_capabilities failure properly ?? */ 6221da177e4SLinus Torvalds get_capabilities(cd); 6231da177e4SLinus Torvalds sr_vendor_init(cd); 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds snprintf(disk->devfs_name, sizeof(disk->devfs_name), 6261da177e4SLinus Torvalds "%s/cd", sdev->devfs_name); 6271da177e4SLinus Torvalds disk->driverfs_dev = &sdev->sdev_gendev; 6281da177e4SLinus Torvalds set_capacity(disk, cd->capacity); 6291da177e4SLinus Torvalds disk->private_data = &cd->driver; 6301da177e4SLinus Torvalds disk->queue = sdev->request_queue; 6311da177e4SLinus Torvalds cd->cdi.disk = disk; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds if (register_cdrom(&cd->cdi)) 6341da177e4SLinus Torvalds goto fail_put; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds dev_set_drvdata(dev, cd); 6371da177e4SLinus Torvalds disk->flags |= GENHD_FL_REMOVABLE; 6381da177e4SLinus Torvalds add_disk(disk); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds printk(KERN_DEBUG 6411da177e4SLinus Torvalds "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n", 6421da177e4SLinus Torvalds cd->cdi.name, sdev->host->host_no, sdev->channel, 6431da177e4SLinus Torvalds sdev->id, sdev->lun); 6441da177e4SLinus Torvalds return 0; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds fail_put: 6471da177e4SLinus Torvalds put_disk(disk); 6481da177e4SLinus Torvalds fail_free: 6491da177e4SLinus Torvalds kfree(cd); 6501da177e4SLinus Torvalds fail: 6511da177e4SLinus Torvalds return error; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *cd) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds unsigned char cmd[10]; 6581da177e4SLinus Torvalds unsigned char *buffer; 6591da177e4SLinus Torvalds int the_result, retries = 3; 6601da177e4SLinus Torvalds int sector_size; 6611da177e4SLinus Torvalds struct scsi_request *SRpnt = NULL; 6621da177e4SLinus Torvalds request_queue_t *queue; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 6651da177e4SLinus Torvalds if (!buffer) 6661da177e4SLinus Torvalds goto Enomem; 6671da177e4SLinus Torvalds SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL); 6681da177e4SLinus Torvalds if (!SRpnt) 6691da177e4SLinus Torvalds goto Enomem; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds do { 6721da177e4SLinus Torvalds cmd[0] = READ_CAPACITY; 6731da177e4SLinus Torvalds memset((void *) &cmd[1], 0, 9); 6741da177e4SLinus Torvalds /* Mark as really busy */ 6751da177e4SLinus Torvalds SRpnt->sr_request->rq_status = RQ_SCSI_BUSY; 6761da177e4SLinus Torvalds SRpnt->sr_cmd_len = 0; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds memset(buffer, 0, 8); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* Do the command and wait.. */ 6811da177e4SLinus Torvalds SRpnt->sr_data_direction = DMA_FROM_DEVICE; 6821da177e4SLinus Torvalds scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer, 6831da177e4SLinus Torvalds 8, SR_TIMEOUT, MAX_RETRIES); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds the_result = SRpnt->sr_result; 6861da177e4SLinus Torvalds retries--; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds } while (the_result && retries); 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds scsi_release_request(SRpnt); 6921da177e4SLinus Torvalds SRpnt = NULL; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds if (the_result) { 6951da177e4SLinus Torvalds cd->capacity = 0x1fffff; 6961da177e4SLinus Torvalds sector_size = 2048; /* A guess, just in case */ 6971da177e4SLinus Torvalds cd->needs_sector_size = 1; 6981da177e4SLinus Torvalds } else { 6991da177e4SLinus Torvalds #if 0 7001da177e4SLinus Torvalds if (cdrom_get_last_written(&cd->cdi, 7011da177e4SLinus Torvalds &cd->capacity)) 7021da177e4SLinus Torvalds #endif 7031da177e4SLinus Torvalds cd->capacity = 1 + ((buffer[0] << 24) | 7041da177e4SLinus Torvalds (buffer[1] << 16) | 7051da177e4SLinus Torvalds (buffer[2] << 8) | 7061da177e4SLinus Torvalds buffer[3]); 7071da177e4SLinus Torvalds sector_size = (buffer[4] << 24) | 7081da177e4SLinus Torvalds (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; 7091da177e4SLinus Torvalds switch (sector_size) { 7101da177e4SLinus Torvalds /* 7111da177e4SLinus Torvalds * HP 4020i CD-Recorder reports 2340 byte sectors 7121da177e4SLinus Torvalds * Philips CD-Writers report 2352 byte sectors 7131da177e4SLinus Torvalds * 7141da177e4SLinus Torvalds * Use 2k sectors for them.. 7151da177e4SLinus Torvalds */ 7161da177e4SLinus Torvalds case 0: 7171da177e4SLinus Torvalds case 2340: 7181da177e4SLinus Torvalds case 2352: 7191da177e4SLinus Torvalds sector_size = 2048; 7201da177e4SLinus Torvalds /* fall through */ 7211da177e4SLinus Torvalds case 2048: 7221da177e4SLinus Torvalds cd->capacity *= 4; 7231da177e4SLinus Torvalds /* fall through */ 7241da177e4SLinus Torvalds case 512: 7251da177e4SLinus Torvalds break; 7261da177e4SLinus Torvalds default: 7271da177e4SLinus Torvalds printk("%s: unsupported sector size %d.\n", 7281da177e4SLinus Torvalds cd->cdi.name, sector_size); 7291da177e4SLinus Torvalds cd->capacity = 0; 7301da177e4SLinus Torvalds cd->needs_sector_size = 1; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds cd->device->sector_size = sector_size; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /* 7361da177e4SLinus Torvalds * Add this so that we have the ability to correctly gauge 7371da177e4SLinus Torvalds * what the device is capable of. 7381da177e4SLinus Torvalds */ 7391da177e4SLinus Torvalds cd->needs_sector_size = 0; 7401da177e4SLinus Torvalds set_capacity(cd->disk, cd->capacity); 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds queue = cd->device->request_queue; 7441da177e4SLinus Torvalds blk_queue_hardsect_size(queue, sector_size); 7451da177e4SLinus Torvalds out: 7461da177e4SLinus Torvalds kfree(buffer); 7471da177e4SLinus Torvalds return; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds Enomem: 7501da177e4SLinus Torvalds cd->capacity = 0x1fffff; 7511da177e4SLinus Torvalds sector_size = 2048; /* A guess, just in case */ 7521da177e4SLinus Torvalds cd->needs_sector_size = 1; 7531da177e4SLinus Torvalds if (SRpnt) 7541da177e4SLinus Torvalds scsi_release_request(SRpnt); 7551da177e4SLinus Torvalds goto out; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *cd) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds unsigned char *buffer; 7611da177e4SLinus Torvalds struct scsi_mode_data data; 7621da177e4SLinus Torvalds struct scsi_request *SRpnt; 7631da177e4SLinus Torvalds unsigned char cmd[MAX_COMMAND_SIZE]; 7641da177e4SLinus Torvalds unsigned int the_result; 7651da177e4SLinus Torvalds int retries, rc, n; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds static char *loadmech[] = 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds "caddy", 7701da177e4SLinus Torvalds "tray", 7711da177e4SLinus Torvalds "pop-up", 7721da177e4SLinus Torvalds "", 7731da177e4SLinus Torvalds "changer", 7741da177e4SLinus Torvalds "cartridge changer", 7751da177e4SLinus Torvalds "", 7761da177e4SLinus Torvalds "" 7771da177e4SLinus Torvalds }; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* allocate a request for the TEST_UNIT_READY */ 7801da177e4SLinus Torvalds SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL); 7811da177e4SLinus Torvalds if (!SRpnt) { 7821da177e4SLinus Torvalds printk(KERN_WARNING "(get_capabilities:) Request allocation " 7831da177e4SLinus Torvalds "failure.\n"); 7841da177e4SLinus Torvalds return; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /* allocate transfer buffer */ 7881da177e4SLinus Torvalds buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 7891da177e4SLinus Torvalds if (!buffer) { 7901da177e4SLinus Torvalds printk(KERN_ERR "sr: out of memory.\n"); 7911da177e4SLinus Torvalds scsi_release_request(SRpnt); 7921da177e4SLinus Torvalds return; 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION 7961da177e4SLinus Torvalds * conditions are gone, or a timeout happens 7971da177e4SLinus Torvalds */ 7981da177e4SLinus Torvalds retries = 0; 7991da177e4SLinus Torvalds do { 8001da177e4SLinus Torvalds memset((void *)cmd, 0, MAX_COMMAND_SIZE); 8011da177e4SLinus Torvalds cmd[0] = TEST_UNIT_READY; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds SRpnt->sr_cmd_len = 0; 8041da177e4SLinus Torvalds SRpnt->sr_sense_buffer[0] = 0; 8051da177e4SLinus Torvalds SRpnt->sr_sense_buffer[2] = 0; 8061da177e4SLinus Torvalds SRpnt->sr_data_direction = DMA_NONE; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds scsi_wait_req (SRpnt, (void *) cmd, buffer, 8091da177e4SLinus Torvalds 0, SR_TIMEOUT, MAX_RETRIES); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds the_result = SRpnt->sr_result; 8121da177e4SLinus Torvalds retries++; 8131da177e4SLinus Torvalds } while (retries < 5 && 8141da177e4SLinus Torvalds (!scsi_status_is_good(the_result) || 8151da177e4SLinus Torvalds ((driver_byte(the_result) & DRIVER_SENSE) && 8161da177e4SLinus Torvalds SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION))); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* ask for mode page 0x2a */ 8191da177e4SLinus Torvalds rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128, 820*1cf72699SJames Bottomley SR_TIMEOUT, 3, &data, NULL); 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (!scsi_status_is_good(rc)) { 8231da177e4SLinus Torvalds /* failed, drive doesn't have capabilities mode page */ 8241da177e4SLinus Torvalds cd->cdi.speed = 1; 8251da177e4SLinus Torvalds cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R | 8261da177e4SLinus Torvalds CDC_DVD | CDC_DVD_RAM | 8271da177e4SLinus Torvalds CDC_SELECT_DISC | CDC_SELECT_SPEED); 8281da177e4SLinus Torvalds scsi_release_request(SRpnt); 8291da177e4SLinus Torvalds kfree(buffer); 8301da177e4SLinus Torvalds printk("%s: scsi-1 drive\n", cd->cdi.name); 8311da177e4SLinus Torvalds return; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds n = data.header_length + data.block_descriptor_length; 8351da177e4SLinus Torvalds cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176; 8361da177e4SLinus Torvalds cd->readcd_known = 1; 8371da177e4SLinus Torvalds cd->readcd_cdda = buffer[n + 5] & 0x01; 8381da177e4SLinus Torvalds /* print some capability bits */ 8391da177e4SLinus Torvalds printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name, 8401da177e4SLinus Torvalds ((buffer[n + 14] << 8) + buffer[n + 15]) / 176, 8411da177e4SLinus Torvalds cd->cdi.speed, 8421da177e4SLinus Torvalds buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */ 8431da177e4SLinus Torvalds buffer[n + 3] & 0x20 ? "dvd-ram " : "", 8441da177e4SLinus Torvalds buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */ 8451da177e4SLinus Torvalds buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */ 8461da177e4SLinus Torvalds buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */ 8471da177e4SLinus Torvalds loadmech[buffer[n + 6] >> 5]); 8481da177e4SLinus Torvalds if ((buffer[n + 6] >> 5) == 0) 8491da177e4SLinus Torvalds /* caddy drives can't close tray... */ 8501da177e4SLinus Torvalds cd->cdi.mask |= CDC_CLOSE_TRAY; 8511da177e4SLinus Torvalds if ((buffer[n + 2] & 0x8) == 0) 8521da177e4SLinus Torvalds /* not a DVD drive */ 8531da177e4SLinus Torvalds cd->cdi.mask |= CDC_DVD; 8541da177e4SLinus Torvalds if ((buffer[n + 3] & 0x20) == 0) 8551da177e4SLinus Torvalds /* can't write DVD-RAM media */ 8561da177e4SLinus Torvalds cd->cdi.mask |= CDC_DVD_RAM; 8571da177e4SLinus Torvalds if ((buffer[n + 3] & 0x10) == 0) 8581da177e4SLinus Torvalds /* can't write DVD-R media */ 8591da177e4SLinus Torvalds cd->cdi.mask |= CDC_DVD_R; 8601da177e4SLinus Torvalds if ((buffer[n + 3] & 0x2) == 0) 8611da177e4SLinus Torvalds /* can't write CD-RW media */ 8621da177e4SLinus Torvalds cd->cdi.mask |= CDC_CD_RW; 8631da177e4SLinus Torvalds if ((buffer[n + 3] & 0x1) == 0) 8641da177e4SLinus Torvalds /* can't write CD-R media */ 8651da177e4SLinus Torvalds cd->cdi.mask |= CDC_CD_R; 8661da177e4SLinus Torvalds if ((buffer[n + 6] & 0x8) == 0) 8671da177e4SLinus Torvalds /* can't eject */ 8681da177e4SLinus Torvalds cd->cdi.mask |= CDC_OPEN_TRAY; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds if ((buffer[n + 6] >> 5) == mechtype_individual_changer || 8711da177e4SLinus Torvalds (buffer[n + 6] >> 5) == mechtype_cartridge_changer) 8721da177e4SLinus Torvalds cd->cdi.capacity = 8731da177e4SLinus Torvalds cdrom_number_of_slots(&cd->cdi); 8741da177e4SLinus Torvalds if (cd->cdi.capacity <= 1) 8751da177e4SLinus Torvalds /* not a changer */ 8761da177e4SLinus Torvalds cd->cdi.mask |= CDC_SELECT_DISC; 8771da177e4SLinus Torvalds /*else I don't think it can close its tray 8781da177e4SLinus Torvalds cd->cdi.mask |= CDC_CLOSE_TRAY; */ 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /* 8811da177e4SLinus Torvalds * if DVD-RAM, MRW-W or CD-RW, we are randomly writable 8821da177e4SLinus Torvalds */ 8831da177e4SLinus Torvalds if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) != 8841da177e4SLinus Torvalds (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) { 8851da177e4SLinus Torvalds cd->device->writeable = 1; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds scsi_release_request(SRpnt); 8891da177e4SLinus Torvalds kfree(buffer); 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds /* 8931da177e4SLinus Torvalds * sr_packet() is the entry point for the generic commands generated 8941da177e4SLinus Torvalds * by the Uniform CD-ROM layer. 8951da177e4SLinus Torvalds */ 8961da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *cdi, 8971da177e4SLinus Torvalds struct packet_command *cgc) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds if (cgc->timeout <= 0) 9001da177e4SLinus Torvalds cgc->timeout = IOCTL_TIMEOUT; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds sr_do_ioctl(cdi->handle, cgc); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds return cgc->stat; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds /** 9081da177e4SLinus Torvalds * sr_kref_release - Called to free the scsi_cd structure 9091da177e4SLinus Torvalds * @kref: pointer to embedded kref 9101da177e4SLinus Torvalds * 9111da177e4SLinus Torvalds * sr_ref_sem must be held entering this routine. Because it is 9121da177e4SLinus Torvalds * called on last put, you should always use the scsi_cd_get() 9131da177e4SLinus Torvalds * scsi_cd_put() helpers which manipulate the semaphore directly 9141da177e4SLinus Torvalds * and never do a direct kref_put(). 9151da177e4SLinus Torvalds **/ 9161da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref) 9171da177e4SLinus Torvalds { 9181da177e4SLinus Torvalds struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); 9191da177e4SLinus Torvalds struct gendisk *disk = cd->disk; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds spin_lock(&sr_index_lock); 9221da177e4SLinus Torvalds clear_bit(disk->first_minor, sr_index_bits); 9231da177e4SLinus Torvalds spin_unlock(&sr_index_lock); 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds unregister_cdrom(&cd->cdi); 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds disk->private_data = NULL; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds put_disk(disk); 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds kfree(cd); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds static int sr_remove(struct device *dev) 9351da177e4SLinus Torvalds { 9361da177e4SLinus Torvalds struct scsi_cd *cd = dev_get_drvdata(dev); 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds del_gendisk(cd->disk); 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds down(&sr_ref_sem); 9411da177e4SLinus Torvalds kref_put(&cd->kref, sr_kref_release); 9421da177e4SLinus Torvalds up(&sr_ref_sem); 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds return 0; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds static int __init init_sr(void) 9481da177e4SLinus Torvalds { 9491da177e4SLinus Torvalds int rc; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds rc = register_blkdev(SCSI_CDROM_MAJOR, "sr"); 9521da177e4SLinus Torvalds if (rc) 9531da177e4SLinus Torvalds return rc; 9541da177e4SLinus Torvalds return scsi_register_driver(&sr_template.gendrv); 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds static void __exit exit_sr(void) 9581da177e4SLinus Torvalds { 9591da177e4SLinus Torvalds scsi_unregister_driver(&sr_template.gendrv); 9601da177e4SLinus Torvalds unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds module_init(init_sr); 9641da177e4SLinus Torvalds module_exit(exit_sr); 9651da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 966