| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  sr.c Copyright (C) 1992 David Giller | 
|  | 3 | *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale | 
|  | 4 | * | 
|  | 5 | *  adapted from: | 
|  | 6 | *      sd.c Copyright (C) 1992 Drew Eckhardt | 
|  | 7 | *      Linux scsi disk driver by | 
|  | 8 | *              Drew Eckhardt <drew@colorado.edu> | 
|  | 9 | * | 
|  | 10 | *	Modified by Eric Youngdale ericy@andante.org to | 
|  | 11 | *	add scatter-gather, multiple outstanding request, and other | 
|  | 12 | *	enhancements. | 
|  | 13 | * | 
|  | 14 | *      Modified by Eric Youngdale eric@andante.org to support loadable | 
|  | 15 | *      low-level scsi drivers. | 
|  | 16 | * | 
|  | 17 | *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to | 
|  | 18 | *      provide auto-eject. | 
|  | 19 | * | 
|  | 20 | *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the | 
|  | 21 | *      generic cdrom interface | 
|  | 22 | * | 
|  | 23 | *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() | 
|  | 24 | *      interface, capabilities probe additions, ioctl cleanups, etc. | 
|  | 25 | * | 
|  | 26 | *	Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs | 
|  | 27 | * | 
|  | 28 | *	Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM | 
|  | 29 | *	transparently and lose the GHOST hack | 
|  | 30 | * | 
|  | 31 | *	Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> | 
|  | 32 | *	check resource allocation in sr_init and some cleanups | 
|  | 33 | */ | 
|  | 34 |  | 
|  | 35 | #include <linux/module.h> | 
|  | 36 | #include <linux/fs.h> | 
|  | 37 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/mm.h> | 
|  | 39 | #include <linux/bio.h> | 
|  | 40 | #include <linux/string.h> | 
|  | 41 | #include <linux/errno.h> | 
|  | 42 | #include <linux/cdrom.h> | 
|  | 43 | #include <linux/interrupt.h> | 
|  | 44 | #include <linux/init.h> | 
|  | 45 | #include <linux/blkdev.h> | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 46 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <asm/uaccess.h> | 
|  | 48 |  | 
|  | 49 | #include <scsi/scsi.h> | 
|  | 50 | #include <scsi/scsi_dbg.h> | 
|  | 51 | #include <scsi/scsi_device.h> | 
|  | 52 | #include <scsi/scsi_driver.h> | 
| James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame] | 53 | #include <scsi/scsi_cmnd.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <scsi/scsi_eh.h> | 
|  | 55 | #include <scsi/scsi_host.h> | 
|  | 56 | #include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | #include "scsi_logging.h" | 
|  | 59 | #include "sr.h" | 
|  | 60 |  | 
|  | 61 |  | 
| Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 62 | MODULE_DESCRIPTION("SCSI cdrom (sr) driver"); | 
|  | 63 | MODULE_LICENSE("GPL"); | 
|  | 64 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR); | 
| Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 65 | MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM); | 
|  | 66 | MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM); | 
| Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 67 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define SR_DISKS	256 | 
|  | 69 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #define SR_CAPABILITIES \ | 
|  | 71 | (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ | 
|  | 72 | CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ | 
| Christoph Hellwig | 6a2900b | 2006-03-23 03:00:15 -0800 | [diff] [blame] | 73 | CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ | 
|  | 75 | CDC_MRW|CDC_MRW_W|CDC_RAM) | 
|  | 76 |  | 
|  | 77 | static int sr_probe(struct device *); | 
|  | 78 | static int sr_remove(struct device *); | 
| Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 79 | static int sr_done(struct scsi_cmnd *); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | static struct scsi_driver sr_template = { | 
|  | 82 | .owner			= THIS_MODULE, | 
|  | 83 | .gendrv = { | 
|  | 84 | .name   	= "sr", | 
|  | 85 | .probe		= sr_probe, | 
|  | 86 | .remove		= sr_remove, | 
|  | 87 | }, | 
| Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 88 | .done			= sr_done, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; | 
|  | 90 |  | 
|  | 91 | static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; | 
|  | 92 | static DEFINE_SPINLOCK(sr_index_lock); | 
|  | 93 |  | 
|  | 94 | /* This semaphore is used to mediate the 0->1 reference get in the | 
|  | 95 | * face of object destruction (i.e. we can't allow a get on an | 
|  | 96 | * object after last put) */ | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 97 | static DEFINE_MUTEX(sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
|  | 99 | static int sr_open(struct cdrom_device_info *, int); | 
|  | 100 | static void sr_release(struct cdrom_device_info *); | 
|  | 101 |  | 
|  | 102 | static void get_sectorsize(struct scsi_cd *); | 
|  | 103 | static void get_capabilities(struct scsi_cd *); | 
|  | 104 |  | 
|  | 105 | static int sr_media_change(struct cdrom_device_info *, int); | 
|  | 106 | static int sr_packet(struct cdrom_device_info *, struct packet_command *); | 
|  | 107 |  | 
|  | 108 | static struct cdrom_device_ops sr_dops = { | 
|  | 109 | .open			= sr_open, | 
|  | 110 | .release	 	= sr_release, | 
|  | 111 | .drive_status	 	= sr_drive_status, | 
|  | 112 | .media_changed		= sr_media_change, | 
|  | 113 | .tray_move		= sr_tray_move, | 
|  | 114 | .lock_door		= sr_lock_door, | 
|  | 115 | .select_speed		= sr_select_speed, | 
|  | 116 | .get_last_session	= sr_get_last_session, | 
|  | 117 | .get_mcn		= sr_get_mcn, | 
|  | 118 | .reset			= sr_reset, | 
|  | 119 | .audio_ioctl		= sr_audio_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .capability		= SR_CAPABILITIES, | 
|  | 121 | .generic_packet		= sr_packet, | 
|  | 122 | }; | 
|  | 123 |  | 
|  | 124 | static void sr_kref_release(struct kref *kref); | 
|  | 125 |  | 
|  | 126 | static inline struct scsi_cd *scsi_cd(struct gendisk *disk) | 
|  | 127 | { | 
|  | 128 | return container_of(disk->private_data, struct scsi_cd, driver); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | /* | 
|  | 132 | * The get and put routines for the struct scsi_cd.  Note this entity | 
|  | 133 | * has a scsi_device pointer and owns a reference to this. | 
|  | 134 | */ | 
|  | 135 | static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) | 
|  | 136 | { | 
|  | 137 | struct scsi_cd *cd = NULL; | 
|  | 138 |  | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 139 | mutex_lock(&sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (disk->private_data == NULL) | 
|  | 141 | goto out; | 
|  | 142 | cd = scsi_cd(disk); | 
|  | 143 | kref_get(&cd->kref); | 
|  | 144 | if (scsi_device_get(cd->device)) | 
|  | 145 | goto out_put; | 
|  | 146 | goto out; | 
|  | 147 |  | 
|  | 148 | out_put: | 
|  | 149 | kref_put(&cd->kref, sr_kref_release); | 
|  | 150 | cd = NULL; | 
|  | 151 | out: | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 152 | mutex_unlock(&sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return cd; | 
|  | 154 | } | 
|  | 155 |  | 
| Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 156 | static void scsi_cd_put(struct scsi_cd *cd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { | 
|  | 158 | struct scsi_device *sdev = cd->device; | 
|  | 159 |  | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 160 | mutex_lock(&sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | kref_put(&cd->kref, sr_kref_release); | 
|  | 162 | scsi_device_put(sdev); | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 163 | mutex_unlock(&sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| James Bottomley | 38582a6 | 2008-02-06 13:01:58 -0600 | [diff] [blame] | 166 | /* identical to scsi_test_unit_ready except that it doesn't | 
|  | 167 | * eat the NOT_READY returns for removable media */ | 
|  | 168 | int sr_test_unit_ready(struct scsi_device *sdev, struct scsi_sense_hdr *sshdr) | 
|  | 169 | { | 
|  | 170 | int retries = MAX_RETRIES; | 
|  | 171 | int the_result; | 
|  | 172 | u8 cmd[] = {TEST_UNIT_READY, 0, 0, 0, 0, 0 }; | 
|  | 173 |  | 
|  | 174 | /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION | 
|  | 175 | * conditions are gone, or a timeout happens | 
|  | 176 | */ | 
|  | 177 | do { | 
|  | 178 | the_result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, | 
|  | 179 | 0, sshdr, SR_TIMEOUT, | 
|  | 180 | retries--); | 
|  | 181 |  | 
|  | 182 | } while (retries > 0 && | 
|  | 183 | (!scsi_status_is_good(the_result) || | 
|  | 184 | (scsi_sense_valid(sshdr) && | 
|  | 185 | sshdr->sense_key == UNIT_ATTENTION))); | 
|  | 186 | return the_result; | 
|  | 187 | } | 
|  | 188 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* | 
|  | 190 | * This function checks to see if the media has been changed in the | 
|  | 191 | * CDROM drive.  It is possible that we have already sensed a change, | 
|  | 192 | * or the drive may have sensed one and not yet reported it.  We must | 
|  | 193 | * be ready for either case. This function always reports the current | 
|  | 194 | * value of the changed bit.  If flag is 0, then the changed bit is reset. | 
|  | 195 | * This function could be done as an ioctl, but we would need to have | 
|  | 196 | * an inode for that to work, and we do not always have one. | 
|  | 197 | */ | 
|  | 198 |  | 
| Adrian Bunk | 44818ef | 2007-07-09 11:59:59 -0700 | [diff] [blame] | 199 | static int sr_media_change(struct cdrom_device_info *cdi, int slot) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { | 
|  | 201 | struct scsi_cd *cd = cdi->handle; | 
|  | 202 | int retval; | 
| James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 203 | struct scsi_sense_hdr *sshdr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 |  | 
|  | 205 | if (CDSL_CURRENT != slot) { | 
|  | 206 | /* no changer support */ | 
|  | 207 | return -EINVAL; | 
|  | 208 | } | 
|  | 209 |  | 
| James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 210 | sshdr =  kzalloc(sizeof(*sshdr), GFP_KERNEL); | 
| James Bottomley | 38582a6 | 2008-02-06 13:01:58 -0600 | [diff] [blame] | 211 | retval = sr_test_unit_ready(cd->device, sshdr); | 
| James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 212 | if (retval || (scsi_sense_valid(sshdr) && | 
|  | 213 | /* 0x3a is medium not present */ | 
|  | 214 | sshdr->asc == 0x3a)) { | 
|  | 215 | /* Media not present or unable to test, unit probably not | 
|  | 216 | * ready. This usually means there is no disc in the drive. | 
|  | 217 | * Mark as changed, and we will figure it out later once | 
|  | 218 | * the drive is available again. | 
|  | 219 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | cd->device->changed = 1; | 
| Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 221 | /* This will force a flush, if called from check_disk_change */ | 
|  | 222 | retval = 1; | 
|  | 223 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | }; | 
|  | 225 |  | 
|  | 226 | retval = cd->device->changed; | 
|  | 227 | cd->device->changed = 0; | 
|  | 228 | /* If the disk changed, the capacity will now be different, | 
|  | 229 | * so we force a re-read of this information */ | 
|  | 230 | if (retval) { | 
|  | 231 | /* check multisession offset etc */ | 
|  | 232 | sr_cd_check(cdi); | 
| Pete Zaitcev | 51490c8 | 2005-07-05 18:18:08 -0700 | [diff] [blame] | 233 | get_sectorsize(cd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } | 
| Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 235 |  | 
|  | 236 | out: | 
|  | 237 | /* Notify userspace, that media has changed. */ | 
|  | 238 | if (retval != cd->previous_state) | 
|  | 239 | sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE, | 
|  | 240 | GFP_KERNEL); | 
|  | 241 | cd->previous_state = retval; | 
| James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 242 | kfree(sshdr); | 
| Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 243 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return retval; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | /* | 
| Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 248 | * sr_done is the interrupt routine for the device driver. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | * | 
| Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 250 | * It will be notified on the end of a SCSI read / write, and will take one | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | * of several actions based on success or failure. | 
|  | 252 | */ | 
| Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 253 | static int sr_done(struct scsi_cmnd *SCpnt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { | 
|  | 255 | int result = SCpnt->result; | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 256 | int this_count = scsi_bufflen(SCpnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | int good_bytes = (result == 0 ? this_count : 0); | 
|  | 258 | int block_sectors = 0; | 
|  | 259 | long error_sector; | 
|  | 260 | struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); | 
|  | 261 |  | 
|  | 262 | #ifdef DEBUG | 
|  | 263 | printk("sr.c done: %x\n", result); | 
|  | 264 | #endif | 
|  | 265 |  | 
|  | 266 | /* | 
|  | 267 | * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial | 
|  | 268 | * success.  Since this is a relatively rare error condition, no | 
|  | 269 | * care is taken to avoid unnecessary additional work such as | 
|  | 270 | * memcpy's that could be avoided. | 
|  | 271 | */ | 
|  | 272 | if (driver_byte(result) != 0 &&		/* An error occurred */ | 
|  | 273 | (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ | 
|  | 274 | switch (SCpnt->sense_buffer[2]) { | 
|  | 275 | case MEDIUM_ERROR: | 
|  | 276 | case VOLUME_OVERFLOW: | 
|  | 277 | case ILLEGAL_REQUEST: | 
|  | 278 | if (!(SCpnt->sense_buffer[0] & 0x90)) | 
|  | 279 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | error_sector = (SCpnt->sense_buffer[3] << 24) | | 
|  | 281 | (SCpnt->sense_buffer[4] << 16) | | 
|  | 282 | (SCpnt->sense_buffer[5] << 8) | | 
|  | 283 | SCpnt->sense_buffer[6]; | 
|  | 284 | if (SCpnt->request->bio != NULL) | 
|  | 285 | block_sectors = | 
|  | 286 | bio_sectors(SCpnt->request->bio); | 
|  | 287 | if (block_sectors < 4) | 
|  | 288 | block_sectors = 4; | 
|  | 289 | if (cd->device->sector_size == 2048) | 
|  | 290 | error_sector <<= 2; | 
|  | 291 | error_sector &= ~(block_sectors - 1); | 
|  | 292 | good_bytes = (error_sector - SCpnt->request->sector) << 9; | 
|  | 293 | if (good_bytes < 0 || good_bytes >= this_count) | 
|  | 294 | good_bytes = 0; | 
|  | 295 | /* | 
|  | 296 | * The SCSI specification allows for the value | 
|  | 297 | * returned by READ CAPACITY to be up to 75 2K | 
|  | 298 | * sectors past the last readable block. | 
|  | 299 | * Therefore, if we hit a medium error within the | 
|  | 300 | * last 75 2K sectors, we decrease the saved size | 
|  | 301 | * value. | 
|  | 302 | */ | 
|  | 303 | if (error_sector < get_capacity(cd->disk) && | 
|  | 304 | cd->capacity - error_sector < 4 * 75) | 
|  | 305 | set_capacity(cd->disk, error_sector); | 
|  | 306 | break; | 
|  | 307 |  | 
|  | 308 | case RECOVERED_ERROR: | 
|  | 309 |  | 
|  | 310 | /* | 
|  | 311 | * An error occured, but it recovered.  Inform the | 
|  | 312 | * user, but make sure that it's not treated as a | 
|  | 313 | * hard error. | 
|  | 314 | */ | 
|  | 315 | scsi_print_sense("sr", SCpnt); | 
|  | 316 | SCpnt->result = 0; | 
|  | 317 | SCpnt->sense_buffer[0] = 0x0; | 
|  | 318 | good_bytes = this_count; | 
|  | 319 | break; | 
|  | 320 |  | 
|  | 321 | default: | 
|  | 322 | break; | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 326 | return good_bytes; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 329 | static int sr_prep_fn(struct request_queue *q, struct request *rq) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { | 
|  | 331 | int block=0, this_count, s_size, timeout = SR_TIMEOUT; | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 332 | struct scsi_cd *cd; | 
|  | 333 | struct scsi_cmnd *SCpnt; | 
|  | 334 | struct scsi_device *sdp = q->queuedata; | 
|  | 335 | int ret; | 
|  | 336 |  | 
|  | 337 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { | 
|  | 338 | ret = scsi_setup_blk_pc_cmnd(sdp, rq); | 
|  | 339 | goto out; | 
|  | 340 | } else if (rq->cmd_type != REQ_TYPE_FS) { | 
|  | 341 | ret = BLKPREP_KILL; | 
|  | 342 | goto out; | 
|  | 343 | } | 
|  | 344 | ret = scsi_setup_fs_cmnd(sdp, rq); | 
|  | 345 | if (ret != BLKPREP_OK) | 
|  | 346 | goto out; | 
|  | 347 | SCpnt = rq->special; | 
|  | 348 | cd = scsi_cd(rq->rq_disk); | 
|  | 349 |  | 
|  | 350 | /* from here on until we're complete, any goto out | 
|  | 351 | * is used for a killable error condition */ | 
|  | 352 | ret = BLKPREP_KILL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 |  | 
|  | 354 | SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n", | 
|  | 355 | cd->disk->disk_name, block)); | 
|  | 356 |  | 
|  | 357 | if (!cd->device || !scsi_device_online(cd->device)) { | 
|  | 358 | SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 359 | rq->nr_sectors)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 361 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
|  | 364 | if (cd->device->changed) { | 
|  | 365 | /* | 
|  | 366 | * quietly refuse to do anything to a changed disc until the | 
|  | 367 | * changed bit has been reset | 
|  | 368 | */ | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 369 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
|  | 372 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * we do lazy blocksize switching (when reading XA sectors, | 
|  | 374 | * see CDROMREADMODE2 ioctl) | 
|  | 375 | */ | 
|  | 376 | s_size = cd->device->sector_size; | 
|  | 377 | if (s_size > 2048) { | 
|  | 378 | if (!in_interrupt()) | 
|  | 379 | sr_set_blocklength(cd, 2048); | 
|  | 380 | else | 
|  | 381 | printk("sr: can't switch blocksize: in interrupt\n"); | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | if (s_size != 512 && s_size != 1024 && s_size != 2048) { | 
| Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 385 | scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size); | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 386 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } | 
|  | 388 |  | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 389 | if (rq_data_dir(rq) == WRITE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (!cd->device->writeable) | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 391 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | SCpnt->cmnd[0] = WRITE_10; | 
|  | 393 | SCpnt->sc_data_direction = DMA_TO_DEVICE; | 
|  | 394 | cd->cdi.media_written = 1; | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 395 | } else if (rq_data_dir(rq) == READ) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | SCpnt->cmnd[0] = READ_10; | 
|  | 397 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; | 
|  | 398 | } else { | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 399 | blk_dump_rq_flags(rq, "Unknown sr command"); | 
|  | 400 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
|  | 403 | { | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 404 | struct scatterlist *sg; | 
|  | 405 | int i, size = 0, sg_count = scsi_sg_count(SCpnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 |  | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 407 | scsi_for_each_sg(SCpnt, sg, sg_count, i) | 
|  | 408 | size += sg->length; | 
|  | 409 |  | 
|  | 410 | if (size != scsi_bufflen(SCpnt)) { | 
| Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 411 | scmd_printk(KERN_ERR, SCpnt, | 
|  | 412 | "mismatch count %d, bytes %d\n", | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 413 | size, scsi_bufflen(SCpnt)); | 
|  | 414 | if (scsi_bufflen(SCpnt) > size) | 
|  | 415 | SCpnt->sdb.length = size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | /* | 
|  | 420 | * request doesn't start on hw block boundary, add scatter pads | 
|  | 421 | */ | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 422 | if (((unsigned int)rq->sector % (s_size >> 9)) || | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 423 | (scsi_bufflen(SCpnt) % s_size)) { | 
| Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 424 | scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n"); | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 425 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } | 
|  | 427 |  | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 428 | this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 |  | 
|  | 430 |  | 
|  | 431 | SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", | 
|  | 432 | cd->cdi.name, | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 433 | (rq_data_dir(rq) == WRITE) ? | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | "writing" : "reading", | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 435 | this_count, rq->nr_sectors)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 |  | 
|  | 437 | SCpnt->cmnd[1] = 0; | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 438 | block = (unsigned int)rq->sector / (s_size >> 9); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 |  | 
|  | 440 | if (this_count > 0xffff) { | 
|  | 441 | this_count = 0xffff; | 
| Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 442 | SCpnt->sdb.length = this_count * s_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } | 
|  | 444 |  | 
|  | 445 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; | 
|  | 446 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; | 
|  | 447 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; | 
|  | 448 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; | 
|  | 449 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; | 
|  | 450 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; | 
|  | 451 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; | 
|  | 452 |  | 
|  | 453 | /* | 
|  | 454 | * We shouldn't disconnect in the middle of a sector, so with a dumb | 
|  | 455 | * host adapter, it's safe to assume that we can at least transfer | 
|  | 456 | * this many bytes between each connect / disconnect. | 
|  | 457 | */ | 
|  | 458 | SCpnt->transfersize = cd->device->sector_size; | 
|  | 459 | SCpnt->underflow = this_count << 9; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | SCpnt->allowed = MAX_RETRIES; | 
|  | 461 | SCpnt->timeout_per_command = timeout; | 
|  | 462 |  | 
|  | 463 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | * This indicates that the command is ready from our end to be | 
|  | 465 | * queued. | 
|  | 466 | */ | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 467 | ret = BLKPREP_OK; | 
|  | 468 | out: | 
|  | 469 | return scsi_prep_return(q, rq, ret); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } | 
|  | 471 |  | 
|  | 472 | static int sr_block_open(struct inode *inode, struct file *file) | 
|  | 473 | { | 
|  | 474 | struct gendisk *disk = inode->i_bdev->bd_disk; | 
| Jayachandran C | 80d904c | 2005-10-18 17:11:16 -0700 | [diff] [blame] | 475 | struct scsi_cd *cd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | int ret = 0; | 
|  | 477 |  | 
|  | 478 | if(!(cd = scsi_cd_get(disk))) | 
|  | 479 | return -ENXIO; | 
|  | 480 |  | 
|  | 481 | if((ret = cdrom_open(&cd->cdi, inode, file)) != 0) | 
|  | 482 | scsi_cd_put(cd); | 
|  | 483 |  | 
|  | 484 | return ret; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | static int sr_block_release(struct inode *inode, struct file *file) | 
|  | 488 | { | 
|  | 489 | int ret; | 
|  | 490 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); | 
|  | 491 | ret = cdrom_release(&cd->cdi, file); | 
|  | 492 | if(ret) | 
|  | 493 | return ret; | 
|  | 494 |  | 
|  | 495 | scsi_cd_put(cd); | 
|  | 496 |  | 
|  | 497 | return 0; | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd, | 
|  | 501 | unsigned long arg) | 
|  | 502 | { | 
|  | 503 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); | 
|  | 504 | struct scsi_device *sdev = cd->device; | 
| Christoph Hellwig | 6a2900b | 2006-03-23 03:00:15 -0800 | [diff] [blame] | 505 | void __user *argp = (void __user *)arg; | 
|  | 506 | int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 |  | 
| Christoph Hellwig | 6a2900b | 2006-03-23 03:00:15 -0800 | [diff] [blame] | 508 | /* | 
|  | 509 | * Send SCSI addressing ioctls directly to mid level, send other | 
|  | 510 | * ioctls to cdrom/block level. | 
|  | 511 | */ | 
|  | 512 | switch (cmd) { | 
|  | 513 | case SCSI_IOCTL_GET_IDLUN: | 
|  | 514 | case SCSI_IOCTL_GET_BUS_NUMBER: | 
|  | 515 | return scsi_ioctl(sdev, cmd, argp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } | 
| Christoph Hellwig | 6a2900b | 2006-03-23 03:00:15 -0800 | [diff] [blame] | 517 |  | 
|  | 518 | ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg); | 
| Tejun Heo | 6397256 | 2007-01-02 17:41:04 +0900 | [diff] [blame] | 519 | if (ret != -ENOSYS) | 
| Christoph Hellwig | 6a2900b | 2006-03-23 03:00:15 -0800 | [diff] [blame] | 520 | return ret; | 
|  | 521 |  | 
|  | 522 | /* | 
|  | 523 | * ENODEV means that we didn't recognise the ioctl, or that we | 
|  | 524 | * cannot execute it in the current device state.  In either | 
|  | 525 | * case fall through to scsi_ioctl, which will return ENDOEV again | 
|  | 526 | * if it doesn't recognise the ioctl | 
|  | 527 | */ | 
|  | 528 | ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL); | 
|  | 529 | if (ret != -ENODEV) | 
|  | 530 | return ret; | 
|  | 531 | return scsi_ioctl(sdev, cmd, argp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } | 
|  | 533 |  | 
|  | 534 | static int sr_block_media_changed(struct gendisk *disk) | 
|  | 535 | { | 
|  | 536 | struct scsi_cd *cd = scsi_cd(disk); | 
|  | 537 | return cdrom_media_changed(&cd->cdi); | 
|  | 538 | } | 
|  | 539 |  | 
|  | 540 | static struct block_device_operations sr_bdops = | 
|  | 541 | { | 
|  | 542 | .owner		= THIS_MODULE, | 
|  | 543 | .open		= sr_block_open, | 
|  | 544 | .release	= sr_block_release, | 
|  | 545 | .ioctl		= sr_block_ioctl, | 
|  | 546 | .media_changed	= sr_block_media_changed, | 
|  | 547 | /* | 
|  | 548 | * No compat_ioctl for now because sr_block_ioctl never | 
|  | 549 | * seems to pass arbitary ioctls down to host drivers. | 
|  | 550 | */ | 
|  | 551 | }; | 
|  | 552 |  | 
|  | 553 | static int sr_open(struct cdrom_device_info *cdi, int purpose) | 
|  | 554 | { | 
|  | 555 | struct scsi_cd *cd = cdi->handle; | 
|  | 556 | struct scsi_device *sdev = cd->device; | 
|  | 557 | int retval; | 
|  | 558 |  | 
|  | 559 | /* | 
|  | 560 | * If the device is in error recovery, wait until it is done. | 
|  | 561 | * If the device is offline, then disallow any access to it. | 
|  | 562 | */ | 
|  | 563 | retval = -ENXIO; | 
|  | 564 | if (!scsi_block_when_processing_errors(sdev)) | 
|  | 565 | goto error_out; | 
|  | 566 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | return 0; | 
|  | 568 |  | 
|  | 569 | error_out: | 
|  | 570 | return retval; | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | static void sr_release(struct cdrom_device_info *cdi) | 
|  | 574 | { | 
|  | 575 | struct scsi_cd *cd = cdi->handle; | 
|  | 576 |  | 
|  | 577 | if (cd->device->sector_size > 2048) | 
|  | 578 | sr_set_blocklength(cd, 2048); | 
|  | 579 |  | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | static int sr_probe(struct device *dev) | 
|  | 583 | { | 
|  | 584 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 585 | struct gendisk *disk; | 
|  | 586 | struct scsi_cd *cd; | 
|  | 587 | int minor, error; | 
|  | 588 |  | 
|  | 589 | error = -ENODEV; | 
|  | 590 | if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM) | 
|  | 591 | goto fail; | 
|  | 592 |  | 
|  | 593 | error = -ENOMEM; | 
| Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 594 | cd = kzalloc(sizeof(*cd), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (!cd) | 
|  | 596 | goto fail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 |  | 
|  | 598 | kref_init(&cd->kref); | 
|  | 599 |  | 
|  | 600 | disk = alloc_disk(1); | 
|  | 601 | if (!disk) | 
|  | 602 | goto fail_free; | 
|  | 603 |  | 
|  | 604 | spin_lock(&sr_index_lock); | 
|  | 605 | minor = find_first_zero_bit(sr_index_bits, SR_DISKS); | 
|  | 606 | if (minor == SR_DISKS) { | 
|  | 607 | spin_unlock(&sr_index_lock); | 
|  | 608 | error = -EBUSY; | 
|  | 609 | goto fail_put; | 
|  | 610 | } | 
|  | 611 | __set_bit(minor, sr_index_bits); | 
|  | 612 | spin_unlock(&sr_index_lock); | 
|  | 613 |  | 
|  | 614 | disk->major = SCSI_CDROM_MAJOR; | 
|  | 615 | disk->first_minor = minor; | 
|  | 616 | sprintf(disk->disk_name, "sr%d", minor); | 
|  | 617 | disk->fops = &sr_bdops; | 
|  | 618 | disk->flags = GENHD_FL_CD; | 
|  | 619 |  | 
|  | 620 | cd->device = sdev; | 
|  | 621 | cd->disk = disk; | 
|  | 622 | cd->driver = &sr_template; | 
|  | 623 | cd->disk = disk; | 
|  | 624 | cd->capacity = 0x1fffff; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | cd->device->changed = 1;	/* force recheck CD type */ | 
|  | 626 | cd->use = 1; | 
|  | 627 | cd->readcd_known = 0; | 
|  | 628 | cd->readcd_cdda = 0; | 
|  | 629 |  | 
|  | 630 | cd->cdi.ops = &sr_dops; | 
|  | 631 | cd->cdi.handle = cd; | 
|  | 632 | cd->cdi.mask = 0; | 
|  | 633 | cd->cdi.capacity = 1; | 
|  | 634 | sprintf(cd->cdi.name, "sr%d", minor); | 
|  | 635 |  | 
|  | 636 | sdev->sector_size = 2048;	/* A guess, just in case */ | 
|  | 637 |  | 
|  | 638 | /* FIXME: need to handle a get_capabilities failure properly ?? */ | 
|  | 639 | get_capabilities(cd); | 
| James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 640 | blk_queue_prep_rq(sdev->request_queue, sr_prep_fn); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | sr_vendor_init(cd); | 
|  | 642 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | disk->driverfs_dev = &sdev->sdev_gendev; | 
|  | 644 | set_capacity(disk, cd->capacity); | 
|  | 645 | disk->private_data = &cd->driver; | 
|  | 646 | disk->queue = sdev->request_queue; | 
|  | 647 | cd->cdi.disk = disk; | 
|  | 648 |  | 
|  | 649 | if (register_cdrom(&cd->cdi)) | 
|  | 650 | goto fail_put; | 
|  | 651 |  | 
|  | 652 | dev_set_drvdata(dev, cd); | 
|  | 653 | disk->flags |= GENHD_FL_REMOVABLE; | 
|  | 654 | add_disk(disk); | 
|  | 655 |  | 
| James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 656 | sdev_printk(KERN_DEBUG, sdev, | 
|  | 657 | "Attached scsi CD-ROM %s\n", cd->cdi.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return 0; | 
|  | 659 |  | 
|  | 660 | fail_put: | 
|  | 661 | put_disk(disk); | 
|  | 662 | fail_free: | 
|  | 663 | kfree(cd); | 
|  | 664 | fail: | 
|  | 665 | return error; | 
|  | 666 | } | 
|  | 667 |  | 
|  | 668 |  | 
|  | 669 | static void get_sectorsize(struct scsi_cd *cd) | 
|  | 670 | { | 
|  | 671 | unsigned char cmd[10]; | 
|  | 672 | unsigned char *buffer; | 
|  | 673 | int the_result, retries = 3; | 
|  | 674 | int sector_size; | 
| Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 675 | struct request_queue *queue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 |  | 
|  | 677 | buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); | 
|  | 678 | if (!buffer) | 
|  | 679 | goto Enomem; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 |  | 
|  | 681 | do { | 
|  | 682 | cmd[0] = READ_CAPACITY; | 
|  | 683 | memset((void *) &cmd[1], 0, 9); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | memset(buffer, 0, 8); | 
|  | 685 |  | 
|  | 686 | /* Do the command and wait.. */ | 
| James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame] | 687 | the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE, | 
|  | 688 | buffer, 8, NULL, SR_TIMEOUT, | 
|  | 689 | MAX_RETRIES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | retries--; | 
|  | 692 |  | 
|  | 693 | } while (the_result && retries); | 
|  | 694 |  | 
|  | 695 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (the_result) { | 
|  | 697 | cd->capacity = 0x1fffff; | 
|  | 698 | sector_size = 2048;	/* A guess, just in case */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } else { | 
|  | 700 | #if 0 | 
|  | 701 | if (cdrom_get_last_written(&cd->cdi, | 
|  | 702 | &cd->capacity)) | 
|  | 703 | #endif | 
|  | 704 | cd->capacity = 1 + ((buffer[0] << 24) | | 
|  | 705 | (buffer[1] << 16) | | 
|  | 706 | (buffer[2] << 8) | | 
|  | 707 | buffer[3]); | 
|  | 708 | sector_size = (buffer[4] << 24) | | 
|  | 709 | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; | 
|  | 710 | switch (sector_size) { | 
|  | 711 | /* | 
|  | 712 | * HP 4020i CD-Recorder reports 2340 byte sectors | 
|  | 713 | * Philips CD-Writers report 2352 byte sectors | 
|  | 714 | * | 
|  | 715 | * Use 2k sectors for them.. | 
|  | 716 | */ | 
|  | 717 | case 0: | 
|  | 718 | case 2340: | 
|  | 719 | case 2352: | 
|  | 720 | sector_size = 2048; | 
|  | 721 | /* fall through */ | 
|  | 722 | case 2048: | 
|  | 723 | cd->capacity *= 4; | 
|  | 724 | /* fall through */ | 
|  | 725 | case 512: | 
|  | 726 | break; | 
|  | 727 | default: | 
|  | 728 | printk("%s: unsupported sector size %d.\n", | 
|  | 729 | cd->cdi.name, sector_size); | 
|  | 730 | cd->capacity = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } | 
|  | 732 |  | 
|  | 733 | cd->device->sector_size = sector_size; | 
|  | 734 |  | 
|  | 735 | /* | 
|  | 736 | * Add this so that we have the ability to correctly gauge | 
|  | 737 | * what the device is capable of. | 
|  | 738 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | set_capacity(cd->disk, cd->capacity); | 
|  | 740 | } | 
|  | 741 |  | 
|  | 742 | queue = cd->device->request_queue; | 
|  | 743 | blk_queue_hardsect_size(queue, sector_size); | 
|  | 744 | out: | 
|  | 745 | kfree(buffer); | 
|  | 746 | return; | 
|  | 747 |  | 
|  | 748 | Enomem: | 
|  | 749 | cd->capacity = 0x1fffff; | 
| Pete Zaitcev | 51490c8 | 2005-07-05 18:18:08 -0700 | [diff] [blame] | 750 | cd->device->sector_size = 2048;	/* A guess, just in case */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | goto out; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | static void get_capabilities(struct scsi_cd *cd) | 
|  | 755 | { | 
|  | 756 | unsigned char *buffer; | 
|  | 757 | struct scsi_mode_data data; | 
| James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame] | 758 | struct scsi_sense_hdr sshdr; | 
| James Bottomley | 38582a6 | 2008-02-06 13:01:58 -0600 | [diff] [blame] | 759 | int rc, n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 |  | 
| Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 761 | static const char *loadmech[] = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { | 
|  | 763 | "caddy", | 
|  | 764 | "tray", | 
|  | 765 | "pop-up", | 
|  | 766 | "", | 
|  | 767 | "changer", | 
|  | 768 | "cartridge changer", | 
|  | 769 | "", | 
|  | 770 | "" | 
|  | 771 | }; | 
|  | 772 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 |  | 
|  | 774 | /* allocate transfer buffer */ | 
|  | 775 | buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); | 
|  | 776 | if (!buffer) { | 
|  | 777 | printk(KERN_ERR "sr: out of memory.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return; | 
|  | 779 | } | 
|  | 780 |  | 
| James Bottomley | 38582a6 | 2008-02-06 13:01:58 -0600 | [diff] [blame] | 781 | /* eat unit attentions */ | 
|  | 782 | sr_test_unit_ready(cd->device, &sshdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 |  | 
|  | 784 | /* ask for mode page 0x2a */ | 
|  | 785 | rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128, | 
| James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 786 | SR_TIMEOUT, 3, &data, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 |  | 
|  | 788 | if (!scsi_status_is_good(rc)) { | 
|  | 789 | /* failed, drive doesn't have capabilities mode page */ | 
|  | 790 | cd->cdi.speed = 1; | 
|  | 791 | cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R | | 
| Chuck Ebbert | bcc1e38 | 2006-01-11 23:58:40 -0500 | [diff] [blame] | 792 | CDC_DVD | CDC_DVD_RAM | | 
|  | 793 | CDC_SELECT_DISC | CDC_SELECT_SPEED | | 
|  | 794 | CDC_MRW | CDC_MRW_W | CDC_RAM); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | kfree(buffer); | 
|  | 796 | printk("%s: scsi-1 drive\n", cd->cdi.name); | 
|  | 797 | return; | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | n = data.header_length + data.block_descriptor_length; | 
|  | 801 | cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176; | 
|  | 802 | cd->readcd_known = 1; | 
|  | 803 | cd->readcd_cdda = buffer[n + 5] & 0x01; | 
|  | 804 | /* print some capability bits */ | 
|  | 805 | printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name, | 
|  | 806 | ((buffer[n + 14] << 8) + buffer[n + 15]) / 176, | 
|  | 807 | cd->cdi.speed, | 
|  | 808 | buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */ | 
|  | 809 | buffer[n + 3] & 0x20 ? "dvd-ram " : "", | 
|  | 810 | buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */ | 
|  | 811 | buffer[n + 4] & 0x20 ? "xa/form2 " : "",	/* can read xa/from2 */ | 
|  | 812 | buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */ | 
|  | 813 | loadmech[buffer[n + 6] >> 5]); | 
|  | 814 | if ((buffer[n + 6] >> 5) == 0) | 
|  | 815 | /* caddy drives can't close tray... */ | 
|  | 816 | cd->cdi.mask |= CDC_CLOSE_TRAY; | 
|  | 817 | if ((buffer[n + 2] & 0x8) == 0) | 
|  | 818 | /* not a DVD drive */ | 
|  | 819 | cd->cdi.mask |= CDC_DVD; | 
|  | 820 | if ((buffer[n + 3] & 0x20) == 0) | 
|  | 821 | /* can't write DVD-RAM media */ | 
|  | 822 | cd->cdi.mask |= CDC_DVD_RAM; | 
|  | 823 | if ((buffer[n + 3] & 0x10) == 0) | 
|  | 824 | /* can't write DVD-R media */ | 
|  | 825 | cd->cdi.mask |= CDC_DVD_R; | 
|  | 826 | if ((buffer[n + 3] & 0x2) == 0) | 
|  | 827 | /* can't write CD-RW media */ | 
|  | 828 | cd->cdi.mask |= CDC_CD_RW; | 
|  | 829 | if ((buffer[n + 3] & 0x1) == 0) | 
|  | 830 | /* can't write CD-R media */ | 
|  | 831 | cd->cdi.mask |= CDC_CD_R; | 
|  | 832 | if ((buffer[n + 6] & 0x8) == 0) | 
|  | 833 | /* can't eject */ | 
|  | 834 | cd->cdi.mask |= CDC_OPEN_TRAY; | 
|  | 835 |  | 
|  | 836 | if ((buffer[n + 6] >> 5) == mechtype_individual_changer || | 
|  | 837 | (buffer[n + 6] >> 5) == mechtype_cartridge_changer) | 
|  | 838 | cd->cdi.capacity = | 
|  | 839 | cdrom_number_of_slots(&cd->cdi); | 
|  | 840 | if (cd->cdi.capacity <= 1) | 
|  | 841 | /* not a changer */ | 
|  | 842 | cd->cdi.mask |= CDC_SELECT_DISC; | 
|  | 843 | /*else    I don't think it can close its tray | 
|  | 844 | cd->cdi.mask |= CDC_CLOSE_TRAY; */ | 
|  | 845 |  | 
|  | 846 | /* | 
|  | 847 | * if DVD-RAM, MRW-W or CD-RW, we are randomly writable | 
|  | 848 | */ | 
|  | 849 | if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) != | 
|  | 850 | (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) { | 
|  | 851 | cd->device->writeable = 1; | 
|  | 852 | } | 
|  | 853 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | kfree(buffer); | 
|  | 855 | } | 
|  | 856 |  | 
|  | 857 | /* | 
|  | 858 | * sr_packet() is the entry point for the generic commands generated | 
|  | 859 | * by the Uniform CD-ROM layer. | 
|  | 860 | */ | 
|  | 861 | static int sr_packet(struct cdrom_device_info *cdi, | 
|  | 862 | struct packet_command *cgc) | 
|  | 863 | { | 
|  | 864 | if (cgc->timeout <= 0) | 
|  | 865 | cgc->timeout = IOCTL_TIMEOUT; | 
|  | 866 |  | 
|  | 867 | sr_do_ioctl(cdi->handle, cgc); | 
|  | 868 |  | 
|  | 869 | return cgc->stat; | 
|  | 870 | } | 
|  | 871 |  | 
|  | 872 | /** | 
|  | 873 | *	sr_kref_release - Called to free the scsi_cd structure | 
|  | 874 | *	@kref: pointer to embedded kref | 
|  | 875 | * | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 876 | *	sr_ref_mutex must be held entering this routine.  Because it is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | *	called on last put, you should always use the scsi_cd_get() | 
|  | 878 | *	scsi_cd_put() helpers which manipulate the semaphore directly | 
|  | 879 | *	and never do a direct kref_put(). | 
|  | 880 | **/ | 
|  | 881 | static void sr_kref_release(struct kref *kref) | 
|  | 882 | { | 
|  | 883 | struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); | 
|  | 884 | struct gendisk *disk = cd->disk; | 
|  | 885 |  | 
|  | 886 | spin_lock(&sr_index_lock); | 
|  | 887 | clear_bit(disk->first_minor, sr_index_bits); | 
|  | 888 | spin_unlock(&sr_index_lock); | 
|  | 889 |  | 
|  | 890 | unregister_cdrom(&cd->cdi); | 
|  | 891 |  | 
|  | 892 | disk->private_data = NULL; | 
|  | 893 |  | 
|  | 894 | put_disk(disk); | 
|  | 895 |  | 
|  | 896 | kfree(cd); | 
|  | 897 | } | 
|  | 898 |  | 
|  | 899 | static int sr_remove(struct device *dev) | 
|  | 900 | { | 
|  | 901 | struct scsi_cd *cd = dev_get_drvdata(dev); | 
|  | 902 |  | 
|  | 903 | del_gendisk(cd->disk); | 
|  | 904 |  | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 905 | mutex_lock(&sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | kref_put(&cd->kref, sr_kref_release); | 
| Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 907 | mutex_unlock(&sr_ref_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 |  | 
|  | 909 | return 0; | 
|  | 910 | } | 
|  | 911 |  | 
|  | 912 | static int __init init_sr(void) | 
|  | 913 | { | 
|  | 914 | int rc; | 
|  | 915 |  | 
|  | 916 | rc = register_blkdev(SCSI_CDROM_MAJOR, "sr"); | 
|  | 917 | if (rc) | 
|  | 918 | return rc; | 
| Akinobu Mita | da3962f | 2007-06-26 00:39:33 +0900 | [diff] [blame] | 919 | rc = scsi_register_driver(&sr_template.gendrv); | 
|  | 920 | if (rc) | 
|  | 921 | unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); | 
|  | 922 |  | 
|  | 923 | return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } | 
|  | 925 |  | 
|  | 926 | static void __exit exit_sr(void) | 
|  | 927 | { | 
|  | 928 | scsi_unregister_driver(&sr_template.gendrv); | 
|  | 929 | unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); | 
|  | 930 | } | 
|  | 931 |  | 
|  | 932 | module_init(init_sr); | 
|  | 933 | module_exit(exit_sr); | 
|  | 934 | MODULE_LICENSE("GPL"); |