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