blob: a0c4e13d4dabbbe70a962e103e78577ce71c6803 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070038#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 Ven0b950672006-01-11 13:16:10 +010046#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#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 Bottomley820732b2005-06-12 22:21:29 -050053#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_eh.h>
55#include <scsi/scsi_host.h>
56#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include "scsi_logging.h"
59#include "sr.h"
60
61
Rene Hermanf018fa52006-03-08 00:14:20 -080062MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63MODULE_LICENSE("GPL");
64MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +040065MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
Rene Hermanf018fa52006-03-08 00:14:20 -080067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define SR_DISKS 256
69
70#define MAX_RETRIES 3
71#define SR_TIMEOUT (30 * HZ)
72#define SR_CAPABILITIES \
73 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
74 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
Christoph Hellwig6a2900b2006-03-23 03:00:15 -080075 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
77 CDC_MRW|CDC_MRW_W|CDC_RAM)
78
79static int sr_probe(struct device *);
80static int sr_remove(struct device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static struct scsi_driver sr_template = {
83 .owner = THIS_MODULE,
84 .gendrv = {
85 .name = "sr",
86 .probe = sr_probe,
87 .remove = sr_remove,
88 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
91static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
92static 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 Ven0b950672006-01-11 13:16:10 +010097static DEFINE_MUTEX(sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99static int sr_open(struct cdrom_device_info *, int);
100static void sr_release(struct cdrom_device_info *);
101
102static void get_sectorsize(struct scsi_cd *);
103static void get_capabilities(struct scsi_cd *);
104
105static int sr_media_change(struct cdrom_device_info *, int);
106static int sr_packet(struct cdrom_device_info *, struct packet_command *);
107
108static 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 Torvalds1da177e2005-04-16 15:20:36 -0700120 .capability = SR_CAPABILITIES,
121 .generic_packet = sr_packet,
122};
123
124static void sr_kref_release(struct kref *kref);
125
126static 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 */
135static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
136{
137 struct scsi_cd *cd = NULL;
138
Arjan van de Ven0b950672006-01-11 13:16:10 +0100139 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 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 Ven0b950672006-01-11 13:16:10 +0100152 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return cd;
154}
155
Arjan van de Ven858119e2006-01-14 13:20:43 -0800156static void scsi_cd_put(struct scsi_cd *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct scsi_device *sdev = cd->device;
159
Arjan van de Ven0b950672006-01-11 13:16:10 +0100160 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 kref_put(&cd->kref, sr_kref_release);
162 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100163 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166/*
167 * This function checks to see if the media has been changed in the
168 * CDROM drive. It is possible that we have already sensed a change,
169 * or the drive may have sensed one and not yet reported it. We must
170 * be ready for either case. This function always reports the current
171 * value of the changed bit. If flag is 0, then the changed bit is reset.
172 * This function could be done as an ioctl, but we would need to have
173 * an inode for that to work, and we do not always have one.
174 */
175
Adrian Bunk44818ef2007-07-09 11:59:59 -0700176static int sr_media_change(struct cdrom_device_info *cdi, int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct scsi_cd *cd = cdi->handle;
179 int retval;
180
181 if (CDSL_CURRENT != slot) {
182 /* no changer support */
183 return -EINVAL;
184 }
185
186 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
187 if (retval) {
188 /* Unable to test, unit probably not ready. This usually
189 * means there is no disc in the drive. Mark as changed,
190 * and we will figure it out later once the drive is
191 * available again. */
192 cd->device->changed = 1;
193 return 1; /* This will force a flush, if called from
194 * check_disk_change */
195 };
196
197 retval = cd->device->changed;
198 cd->device->changed = 0;
199 /* If the disk changed, the capacity will now be different,
200 * so we force a re-read of this information */
201 if (retval) {
202 /* check multisession offset etc */
203 sr_cd_check(cdi);
204
Pete Zaitcev51490c82005-07-05 18:18:08 -0700205 get_sectorsize(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 return retval;
208}
209
210/*
Ingo Molnarac405322008-01-02 17:25:34 +0100211 * rw_intr is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
Ingo Molnarac405322008-01-02 17:25:34 +0100213 * It will be notified on the end of a SCSI read / write, and will take on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * of several actions based on success or failure.
215 */
Ingo Molnarac405322008-01-02 17:25:34 +0100216static void rw_intr(struct scsi_cmnd * SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int result = SCpnt->result;
Christoph Hellwig5d5ff442006-06-03 13:21:13 +0200219 int this_count = SCpnt->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int good_bytes = (result == 0 ? this_count : 0);
221 int block_sectors = 0;
222 long error_sector;
223 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
224
225#ifdef DEBUG
226 printk("sr.c done: %x\n", result);
227#endif
228
229 /*
230 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
231 * success. Since this is a relatively rare error condition, no
232 * care is taken to avoid unnecessary additional work such as
233 * memcpy's that could be avoided.
234 */
235 if (driver_byte(result) != 0 && /* An error occurred */
236 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
237 switch (SCpnt->sense_buffer[2]) {
238 case MEDIUM_ERROR:
239 case VOLUME_OVERFLOW:
240 case ILLEGAL_REQUEST:
241 if (!(SCpnt->sense_buffer[0] & 0x90))
242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 error_sector = (SCpnt->sense_buffer[3] << 24) |
244 (SCpnt->sense_buffer[4] << 16) |
245 (SCpnt->sense_buffer[5] << 8) |
246 SCpnt->sense_buffer[6];
247 if (SCpnt->request->bio != NULL)
248 block_sectors =
249 bio_sectors(SCpnt->request->bio);
250 if (block_sectors < 4)
251 block_sectors = 4;
252 if (cd->device->sector_size == 2048)
253 error_sector <<= 2;
254 error_sector &= ~(block_sectors - 1);
255 good_bytes = (error_sector - SCpnt->request->sector) << 9;
256 if (good_bytes < 0 || good_bytes >= this_count)
257 good_bytes = 0;
258 /*
259 * The SCSI specification allows for the value
260 * returned by READ CAPACITY to be up to 75 2K
261 * sectors past the last readable block.
262 * Therefore, if we hit a medium error within the
263 * last 75 2K sectors, we decrease the saved size
264 * value.
265 */
266 if (error_sector < get_capacity(cd->disk) &&
267 cd->capacity - error_sector < 4 * 75)
268 set_capacity(cd->disk, error_sector);
269 break;
270
271 case RECOVERED_ERROR:
272
273 /*
274 * An error occured, but it recovered. Inform the
275 * user, but make sure that it's not treated as a
276 * hard error.
277 */
278 scsi_print_sense("sr", SCpnt);
279 SCpnt->result = 0;
280 SCpnt->sense_buffer[0] = 0x0;
281 good_bytes = this_count;
282 break;
283
284 default:
285 break;
286 }
287 }
288
Ingo Molnarac405322008-01-02 17:25:34 +0100289 /*
290 * This calls the generic completion function, now that we know
291 * how many actual sectors finished, and how many sectors we need
292 * to say have failed.
293 */
294 scsi_io_completion(SCpnt, good_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500297static int sr_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500300 struct scsi_cd *cd;
301 struct scsi_cmnd *SCpnt;
302 struct scsi_device *sdp = q->queuedata;
303 int ret;
304
305 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
306 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
307 goto out;
308 } else if (rq->cmd_type != REQ_TYPE_FS) {
309 ret = BLKPREP_KILL;
310 goto out;
311 }
312 ret = scsi_setup_fs_cmnd(sdp, rq);
313 if (ret != BLKPREP_OK)
314 goto out;
315 SCpnt = rq->special;
316 cd = scsi_cd(rq->rq_disk);
317
318 /* from here on until we're complete, any goto out
319 * is used for a killable error condition */
320 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
323 cd->disk->disk_name, block));
324
325 if (!cd->device || !scsi_device_online(cd->device)) {
326 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500327 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500329 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 if (cd->device->changed) {
333 /*
334 * quietly refuse to do anything to a changed disc until the
335 * changed bit has been reset
336 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * we do lazy blocksize switching (when reading XA sectors,
342 * see CDROMREADMODE2 ioctl)
343 */
344 s_size = cd->device->sector_size;
345 if (s_size > 2048) {
346 if (!in_interrupt())
347 sr_set_blocklength(cd, 2048);
348 else
349 printk("sr: can't switch blocksize: in interrupt\n");
350 }
351
352 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400353 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500357 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!cd->device->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 SCpnt->cmnd[0] = WRITE_10;
361 SCpnt->sc_data_direction = DMA_TO_DEVICE;
362 cd->cdi.media_written = 1;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500363 } else if (rq_data_dir(rq) == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 SCpnt->cmnd[0] = READ_10;
365 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
366 } else {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500367 blk_dump_rq_flags(rq, "Unknown sr command");
368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 {
372 struct scatterlist *sg = SCpnt->request_buffer;
373 int i, size = 0;
374 for (i = 0; i < SCpnt->use_sg; i++)
375 size += sg[i].length;
376
377 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400378 scmd_printk(KERN_ERR, SCpnt,
379 "mismatch count %d, bytes %d\n",
380 size, SCpnt->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (SCpnt->request_bufflen > size)
Christoph Hellwig631c2282006-07-08 20:42:15 +0200382 SCpnt->request_bufflen = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 }
385
386 /*
387 * request doesn't start on hw block boundary, add scatter pads
388 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500389 if (((unsigned int)rq->sector % (s_size >> 9)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 (SCpnt->request_bufflen % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400391 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
396
397
398 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
399 cd->cdi.name,
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500400 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 "writing" : "reading",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500402 this_count, rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 SCpnt->cmnd[1] = 0;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500405 block = (unsigned int)rq->sector / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (this_count > 0xffff) {
408 this_count = 0xffff;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200409 SCpnt->request_bufflen = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
413 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
414 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
415 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
416 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
417 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
418 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
419
420 /*
421 * We shouldn't disconnect in the middle of a sector, so with a dumb
422 * host adapter, it's safe to assume that we can at least transfer
423 * this many bytes between each connect / disconnect.
424 */
425 SCpnt->transfersize = cd->device->sector_size;
426 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 SCpnt->allowed = MAX_RETRIES;
428 SCpnt->timeout_per_command = timeout;
429
430 /*
Ingo Molnarac405322008-01-02 17:25:34 +0100431 * This is the completion routine we use. This is matched in terms
432 * of capability to this function.
433 */
434 SCpnt->done = rw_intr;
435
436 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * This indicates that the command is ready from our end to be
438 * queued.
439 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500440 ret = BLKPREP_OK;
441 out:
442 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static int sr_block_open(struct inode *inode, struct file *file)
446{
447 struct gendisk *disk = inode->i_bdev->bd_disk;
Jayachandran C80d904c2005-10-18 17:11:16 -0700448 struct scsi_cd *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int ret = 0;
450
451 if(!(cd = scsi_cd_get(disk)))
452 return -ENXIO;
453
454 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
455 scsi_cd_put(cd);
456
457 return ret;
458}
459
460static int sr_block_release(struct inode *inode, struct file *file)
461{
462 int ret;
463 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
464 ret = cdrom_release(&cd->cdi, file);
465 if(ret)
466 return ret;
467
468 scsi_cd_put(cd);
469
470 return 0;
471}
472
473static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
474 unsigned long arg)
475{
476 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
477 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800478 void __user *argp = (void __user *)arg;
479 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800481 /*
482 * Send SCSI addressing ioctls directly to mid level, send other
483 * ioctls to cdrom/block level.
484 */
485 switch (cmd) {
486 case SCSI_IOCTL_GET_IDLUN:
487 case SCSI_IOCTL_GET_BUS_NUMBER:
488 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800490
491 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900492 if (ret != -ENOSYS)
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800493 return ret;
494
495 /*
496 * ENODEV means that we didn't recognise the ioctl, or that we
497 * cannot execute it in the current device state. In either
498 * case fall through to scsi_ioctl, which will return ENDOEV again
499 * if it doesn't recognise the ioctl
500 */
501 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
502 if (ret != -ENODEV)
503 return ret;
504 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static int sr_block_media_changed(struct gendisk *disk)
508{
509 struct scsi_cd *cd = scsi_cd(disk);
510 return cdrom_media_changed(&cd->cdi);
511}
512
513static struct block_device_operations sr_bdops =
514{
515 .owner = THIS_MODULE,
516 .open = sr_block_open,
517 .release = sr_block_release,
518 .ioctl = sr_block_ioctl,
519 .media_changed = sr_block_media_changed,
520 /*
521 * No compat_ioctl for now because sr_block_ioctl never
522 * seems to pass arbitary ioctls down to host drivers.
523 */
524};
525
526static int sr_open(struct cdrom_device_info *cdi, int purpose)
527{
528 struct scsi_cd *cd = cdi->handle;
529 struct scsi_device *sdev = cd->device;
530 int retval;
531
532 /*
533 * If the device is in error recovery, wait until it is done.
534 * If the device is offline, then disallow any access to it.
535 */
536 retval = -ENXIO;
537 if (!scsi_block_when_processing_errors(sdev))
538 goto error_out;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
541
542error_out:
543 return retval;
544}
545
546static void sr_release(struct cdrom_device_info *cdi)
547{
548 struct scsi_cd *cd = cdi->handle;
549
550 if (cd->device->sector_size > 2048)
551 sr_set_blocklength(cd, 2048);
552
553}
554
555static int sr_probe(struct device *dev)
556{
557 struct scsi_device *sdev = to_scsi_device(dev);
558 struct gendisk *disk;
559 struct scsi_cd *cd;
560 int minor, error;
561
562 error = -ENODEV;
563 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
564 goto fail;
565
566 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500567 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!cd)
569 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 kref_init(&cd->kref);
572
573 disk = alloc_disk(1);
574 if (!disk)
575 goto fail_free;
576
577 spin_lock(&sr_index_lock);
578 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
579 if (minor == SR_DISKS) {
580 spin_unlock(&sr_index_lock);
581 error = -EBUSY;
582 goto fail_put;
583 }
584 __set_bit(minor, sr_index_bits);
585 spin_unlock(&sr_index_lock);
586
587 disk->major = SCSI_CDROM_MAJOR;
588 disk->first_minor = minor;
589 sprintf(disk->disk_name, "sr%d", minor);
590 disk->fops = &sr_bdops;
591 disk->flags = GENHD_FL_CD;
592
593 cd->device = sdev;
594 cd->disk = disk;
595 cd->driver = &sr_template;
596 cd->disk = disk;
597 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 cd->device->changed = 1; /* force recheck CD type */
599 cd->use = 1;
600 cd->readcd_known = 0;
601 cd->readcd_cdda = 0;
602
603 cd->cdi.ops = &sr_dops;
604 cd->cdi.handle = cd;
605 cd->cdi.mask = 0;
606 cd->cdi.capacity = 1;
607 sprintf(cd->cdi.name, "sr%d", minor);
608
609 sdev->sector_size = 2048; /* A guess, just in case */
610
611 /* FIXME: need to handle a get_capabilities failure properly ?? */
612 get_capabilities(cd);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500613 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sr_vendor_init(cd);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 disk->driverfs_dev = &sdev->sdev_gendev;
617 set_capacity(disk, cd->capacity);
618 disk->private_data = &cd->driver;
619 disk->queue = sdev->request_queue;
620 cd->cdi.disk = disk;
621
622 if (register_cdrom(&cd->cdi))
623 goto fail_put;
624
625 dev_set_drvdata(dev, cd);
626 disk->flags |= GENHD_FL_REMOVABLE;
627 add_disk(disk);
628
James Bottomley9ccfc752005-10-02 11:45:08 -0500629 sdev_printk(KERN_DEBUG, sdev,
630 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
632
633fail_put:
634 put_disk(disk);
635fail_free:
636 kfree(cd);
637fail:
638 return error;
639}
640
641
642static void get_sectorsize(struct scsi_cd *cd)
643{
644 unsigned char cmd[10];
645 unsigned char *buffer;
646 int the_result, retries = 3;
647 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200648 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
651 if (!buffer)
652 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 do {
655 cmd[0] = READ_CAPACITY;
656 memset((void *) &cmd[1], 0, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 memset(buffer, 0, 8);
658
659 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500660 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
661 buffer, 8, NULL, SR_TIMEOUT,
662 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 retries--;
665
666 } while (the_result && retries);
667
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (the_result) {
670 cd->capacity = 0x1fffff;
671 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 } else {
673#if 0
674 if (cdrom_get_last_written(&cd->cdi,
675 &cd->capacity))
676#endif
677 cd->capacity = 1 + ((buffer[0] << 24) |
678 (buffer[1] << 16) |
679 (buffer[2] << 8) |
680 buffer[3]);
681 sector_size = (buffer[4] << 24) |
682 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
683 switch (sector_size) {
684 /*
685 * HP 4020i CD-Recorder reports 2340 byte sectors
686 * Philips CD-Writers report 2352 byte sectors
687 *
688 * Use 2k sectors for them..
689 */
690 case 0:
691 case 2340:
692 case 2352:
693 sector_size = 2048;
694 /* fall through */
695 case 2048:
696 cd->capacity *= 4;
697 /* fall through */
698 case 512:
699 break;
700 default:
701 printk("%s: unsupported sector size %d.\n",
702 cd->cdi.name, sector_size);
703 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 cd->device->sector_size = sector_size;
707
708 /*
709 * Add this so that we have the ability to correctly gauge
710 * what the device is capable of.
711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 set_capacity(cd->disk, cd->capacity);
713 }
714
715 queue = cd->device->request_queue;
716 blk_queue_hardsect_size(queue, sector_size);
717out:
718 kfree(buffer);
719 return;
720
721Enomem:
722 cd->capacity = 0x1fffff;
Pete Zaitcev51490c82005-07-05 18:18:08 -0700723 cd->device->sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto out;
725}
726
727static void get_capabilities(struct scsi_cd *cd)
728{
729 unsigned char *buffer;
730 struct scsi_mode_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned char cmd[MAX_COMMAND_SIZE];
James Bottomley820732b2005-06-12 22:21:29 -0500732 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 unsigned int the_result;
734 int retries, rc, n;
735
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100736 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 {
738 "caddy",
739 "tray",
740 "pop-up",
741 "",
742 "changer",
743 "cartridge changer",
744 "",
745 ""
746 };
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /* allocate transfer buffer */
750 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
751 if (!buffer) {
752 printk(KERN_ERR "sr: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
754 }
755
756 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
757 * conditions are gone, or a timeout happens
758 */
759 retries = 0;
760 do {
761 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
762 cmd[0] = TEST_UNIT_READY;
763
James Bottomley820732b2005-06-12 22:21:29 -0500764 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
765 0, &sshdr, SR_TIMEOUT,
766 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 retries++;
769 } while (retries < 5 &&
770 (!scsi_status_is_good(the_result) ||
James Bottomley820732b2005-06-12 22:21:29 -0500771 (scsi_sense_valid(&sshdr) &&
772 sshdr.sense_key == UNIT_ATTENTION)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* ask for mode page 0x2a */
775 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500776 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (!scsi_status_is_good(rc)) {
779 /* failed, drive doesn't have capabilities mode page */
780 cd->cdi.speed = 1;
781 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500782 CDC_DVD | CDC_DVD_RAM |
783 CDC_SELECT_DISC | CDC_SELECT_SPEED |
784 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 kfree(buffer);
786 printk("%s: scsi-1 drive\n", cd->cdi.name);
787 return;
788 }
789
790 n = data.header_length + data.block_descriptor_length;
791 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
792 cd->readcd_known = 1;
793 cd->readcd_cdda = buffer[n + 5] & 0x01;
794 /* print some capability bits */
795 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
796 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
797 cd->cdi.speed,
798 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
799 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
800 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
801 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
802 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
803 loadmech[buffer[n + 6] >> 5]);
804 if ((buffer[n + 6] >> 5) == 0)
805 /* caddy drives can't close tray... */
806 cd->cdi.mask |= CDC_CLOSE_TRAY;
807 if ((buffer[n + 2] & 0x8) == 0)
808 /* not a DVD drive */
809 cd->cdi.mask |= CDC_DVD;
810 if ((buffer[n + 3] & 0x20) == 0)
811 /* can't write DVD-RAM media */
812 cd->cdi.mask |= CDC_DVD_RAM;
813 if ((buffer[n + 3] & 0x10) == 0)
814 /* can't write DVD-R media */
815 cd->cdi.mask |= CDC_DVD_R;
816 if ((buffer[n + 3] & 0x2) == 0)
817 /* can't write CD-RW media */
818 cd->cdi.mask |= CDC_CD_RW;
819 if ((buffer[n + 3] & 0x1) == 0)
820 /* can't write CD-R media */
821 cd->cdi.mask |= CDC_CD_R;
822 if ((buffer[n + 6] & 0x8) == 0)
823 /* can't eject */
824 cd->cdi.mask |= CDC_OPEN_TRAY;
825
826 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
827 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
828 cd->cdi.capacity =
829 cdrom_number_of_slots(&cd->cdi);
830 if (cd->cdi.capacity <= 1)
831 /* not a changer */
832 cd->cdi.mask |= CDC_SELECT_DISC;
833 /*else I don't think it can close its tray
834 cd->cdi.mask |= CDC_CLOSE_TRAY; */
835
836 /*
837 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
838 */
839 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
840 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
841 cd->device->writeable = 1;
842 }
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree(buffer);
845}
846
847/*
848 * sr_packet() is the entry point for the generic commands generated
849 * by the Uniform CD-ROM layer.
850 */
851static int sr_packet(struct cdrom_device_info *cdi,
852 struct packet_command *cgc)
853{
854 if (cgc->timeout <= 0)
855 cgc->timeout = IOCTL_TIMEOUT;
856
857 sr_do_ioctl(cdi->handle, cgc);
858
859 return cgc->stat;
860}
861
862/**
863 * sr_kref_release - Called to free the scsi_cd structure
864 * @kref: pointer to embedded kref
865 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100866 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 * called on last put, you should always use the scsi_cd_get()
868 * scsi_cd_put() helpers which manipulate the semaphore directly
869 * and never do a direct kref_put().
870 **/
871static void sr_kref_release(struct kref *kref)
872{
873 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
874 struct gendisk *disk = cd->disk;
875
876 spin_lock(&sr_index_lock);
877 clear_bit(disk->first_minor, sr_index_bits);
878 spin_unlock(&sr_index_lock);
879
880 unregister_cdrom(&cd->cdi);
881
882 disk->private_data = NULL;
883
884 put_disk(disk);
885
886 kfree(cd);
887}
888
889static int sr_remove(struct device *dev)
890{
891 struct scsi_cd *cd = dev_get_drvdata(dev);
892
893 del_gendisk(cd->disk);
894
Arjan van de Ven0b950672006-01-11 13:16:10 +0100895 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100897 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return 0;
900}
901
902static int __init init_sr(void)
903{
904 int rc;
905
906 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
907 if (rc)
908 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +0900909 rc = scsi_register_driver(&sr_template.gendrv);
910 if (rc)
911 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
912
913 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static void __exit exit_sr(void)
917{
918 scsi_unregister_driver(&sr_template.gendrv);
919 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
920}
921
922module_init(init_sr);
923module_exit(exit_sr);
924MODULE_LICENSE("GPL");