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