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