blob: 7702681d93f958c79041e8aa414eb6685980a967 [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 Torvalds7b3d9542008-01-06 10:17:12 -080081static int sr_done(struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct scsi_driver sr_template = {
84 .owner = THIS_MODULE,
85 .gendrv = {
86 .name = "sr",
87 .probe = sr_probe,
88 .remove = sr_remove,
89 },
Linus Torvalds7b3d9542008-01-06 10:17:12 -080090 .done = sr_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
94static DEFINE_SPINLOCK(sr_index_lock);
95
96/* This semaphore is used to mediate the 0->1 reference get in the
97 * face of object destruction (i.e. we can't allow a get on an
98 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +010099static DEFINE_MUTEX(sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static int sr_open(struct cdrom_device_info *, int);
102static void sr_release(struct cdrom_device_info *);
103
104static void get_sectorsize(struct scsi_cd *);
105static void get_capabilities(struct scsi_cd *);
106
107static int sr_media_change(struct cdrom_device_info *, int);
108static int sr_packet(struct cdrom_device_info *, struct packet_command *);
109
110static struct cdrom_device_ops sr_dops = {
111 .open = sr_open,
112 .release = sr_release,
113 .drive_status = sr_drive_status,
114 .media_changed = sr_media_change,
115 .tray_move = sr_tray_move,
116 .lock_door = sr_lock_door,
117 .select_speed = sr_select_speed,
118 .get_last_session = sr_get_last_session,
119 .get_mcn = sr_get_mcn,
120 .reset = sr_reset,
121 .audio_ioctl = sr_audio_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .capability = SR_CAPABILITIES,
123 .generic_packet = sr_packet,
124};
125
126static void sr_kref_release(struct kref *kref);
127
128static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
129{
130 return container_of(disk->private_data, struct scsi_cd, driver);
131}
132
133/*
134 * The get and put routines for the struct scsi_cd. Note this entity
135 * has a scsi_device pointer and owns a reference to this.
136 */
137static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
138{
139 struct scsi_cd *cd = NULL;
140
Arjan van de Ven0b950672006-01-11 13:16:10 +0100141 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (disk->private_data == NULL)
143 goto out;
144 cd = scsi_cd(disk);
145 kref_get(&cd->kref);
146 if (scsi_device_get(cd->device))
147 goto out_put;
148 goto out;
149
150 out_put:
151 kref_put(&cd->kref, sr_kref_release);
152 cd = NULL;
153 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +0100154 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return cd;
156}
157
Arjan van de Ven858119e2006-01-14 13:20:43 -0800158static void scsi_cd_put(struct scsi_cd *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct scsi_device *sdev = cd->device;
161
Arjan van de Ven0b950672006-01-11 13:16:10 +0100162 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 kref_put(&cd->kref, sr_kref_release);
164 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100165 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/*
169 * This function checks to see if the media has been changed in the
170 * CDROM drive. It is possible that we have already sensed a change,
171 * or the drive may have sensed one and not yet reported it. We must
172 * be ready for either case. This function always reports the current
173 * value of the changed bit. If flag is 0, then the changed bit is reset.
174 * This function could be done as an ioctl, but we would need to have
175 * an inode for that to work, and we do not always have one.
176 */
177
Adrian Bunk44818ef2007-07-09 11:59:59 -0700178static int sr_media_change(struct cdrom_device_info *cdi, int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct scsi_cd *cd = cdi->handle;
181 int retval;
182
183 if (CDSL_CURRENT != slot) {
184 /* no changer support */
185 return -EINVAL;
186 }
187
188 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
189 if (retval) {
190 /* Unable to test, unit probably not ready. This usually
191 * means there is no disc in the drive. Mark as changed,
192 * and we will figure it out later once the drive is
193 * available again. */
194 cd->device->changed = 1;
Kay Sievers285e9672007-08-14 14:10:39 +0200195 /* This will force a flush, if called from check_disk_change */
196 retval = 1;
197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 };
199
200 retval = cd->device->changed;
201 cd->device->changed = 0;
202 /* If the disk changed, the capacity will now be different,
203 * so we force a re-read of this information */
204 if (retval) {
205 /* check multisession offset etc */
206 sr_cd_check(cdi);
Pete Zaitcev51490c82005-07-05 18:18:08 -0700207 get_sectorsize(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Kay Sievers285e9672007-08-14 14:10:39 +0200209
210out:
211 /* Notify userspace, that media has changed. */
212 if (retval != cd->previous_state)
213 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
214 GFP_KERNEL);
215 cd->previous_state = retval;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return retval;
218}
219
220/*
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800221 * sr_done is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800223 * It will be notified on the end of a SCSI read / write, and will take one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * of several actions based on success or failure.
225 */
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800226static int sr_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 int result = SCpnt->result;
Christoph Hellwig5d5ff442006-06-03 13:21:13 +0200229 int this_count = SCpnt->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int good_bytes = (result == 0 ? this_count : 0);
231 int block_sectors = 0;
232 long error_sector;
233 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
234
235#ifdef DEBUG
236 printk("sr.c done: %x\n", result);
237#endif
238
239 /*
240 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
241 * success. Since this is a relatively rare error condition, no
242 * care is taken to avoid unnecessary additional work such as
243 * memcpy's that could be avoided.
244 */
245 if (driver_byte(result) != 0 && /* An error occurred */
246 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
247 switch (SCpnt->sense_buffer[2]) {
248 case MEDIUM_ERROR:
249 case VOLUME_OVERFLOW:
250 case ILLEGAL_REQUEST:
251 if (!(SCpnt->sense_buffer[0] & 0x90))
252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 error_sector = (SCpnt->sense_buffer[3] << 24) |
254 (SCpnt->sense_buffer[4] << 16) |
255 (SCpnt->sense_buffer[5] << 8) |
256 SCpnt->sense_buffer[6];
257 if (SCpnt->request->bio != NULL)
258 block_sectors =
259 bio_sectors(SCpnt->request->bio);
260 if (block_sectors < 4)
261 block_sectors = 4;
262 if (cd->device->sector_size == 2048)
263 error_sector <<= 2;
264 error_sector &= ~(block_sectors - 1);
265 good_bytes = (error_sector - SCpnt->request->sector) << 9;
266 if (good_bytes < 0 || good_bytes >= this_count)
267 good_bytes = 0;
268 /*
269 * The SCSI specification allows for the value
270 * returned by READ CAPACITY to be up to 75 2K
271 * sectors past the last readable block.
272 * Therefore, if we hit a medium error within the
273 * last 75 2K sectors, we decrease the saved size
274 * value.
275 */
276 if (error_sector < get_capacity(cd->disk) &&
277 cd->capacity - error_sector < 4 * 75)
278 set_capacity(cd->disk, error_sector);
279 break;
280
281 case RECOVERED_ERROR:
282
283 /*
284 * An error occured, but it recovered. Inform the
285 * user, but make sure that it's not treated as a
286 * hard error.
287 */
288 scsi_print_sense("sr", SCpnt);
289 SCpnt->result = 0;
290 SCpnt->sense_buffer[0] = 0x0;
291 good_bytes = this_count;
292 break;
293
294 default:
295 break;
296 }
297 }
298
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800299 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500302static int sr_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500305 struct scsi_cd *cd;
306 struct scsi_cmnd *SCpnt;
307 struct scsi_device *sdp = q->queuedata;
308 int ret;
309
310 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
311 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
312 goto out;
313 } else if (rq->cmd_type != REQ_TYPE_FS) {
314 ret = BLKPREP_KILL;
315 goto out;
316 }
317 ret = scsi_setup_fs_cmnd(sdp, rq);
318 if (ret != BLKPREP_OK)
319 goto out;
320 SCpnt = rq->special;
321 cd = scsi_cd(rq->rq_disk);
322
323 /* from here on until we're complete, any goto out
324 * is used for a killable error condition */
325 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
328 cd->disk->disk_name, block));
329
330 if (!cd->device || !scsi_device_online(cd->device)) {
331 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500332 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 if (cd->device->changed) {
338 /*
339 * quietly refuse to do anything to a changed disc until the
340 * changed bit has been reset
341 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500342 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * we do lazy blocksize switching (when reading XA sectors,
347 * see CDROMREADMODE2 ioctl)
348 */
349 s_size = cd->device->sector_size;
350 if (s_size > 2048) {
351 if (!in_interrupt())
352 sr_set_blocklength(cd, 2048);
353 else
354 printk("sr: can't switch blocksize: in interrupt\n");
355 }
356
357 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400358 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500362 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!cd->device->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 SCpnt->cmnd[0] = WRITE_10;
366 SCpnt->sc_data_direction = DMA_TO_DEVICE;
367 cd->cdi.media_written = 1;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500368 } else if (rq_data_dir(rq) == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 SCpnt->cmnd[0] = READ_10;
370 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
371 } else {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500372 blk_dump_rq_flags(rq, "Unknown sr command");
373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 {
377 struct scatterlist *sg = SCpnt->request_buffer;
378 int i, size = 0;
379 for (i = 0; i < SCpnt->use_sg; i++)
380 size += sg[i].length;
381
382 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400383 scmd_printk(KERN_ERR, SCpnt,
384 "mismatch count %d, bytes %d\n",
385 size, SCpnt->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (SCpnt->request_bufflen > size)
Christoph Hellwig631c2282006-07-08 20:42:15 +0200387 SCpnt->request_bufflen = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390
391 /*
392 * request doesn't start on hw block boundary, add scatter pads
393 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500394 if (((unsigned int)rq->sector % (s_size >> 9)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 (SCpnt->request_bufflen % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400396 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500397 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
401
402
403 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
404 cd->cdi.name,
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500405 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "writing" : "reading",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500407 this_count, rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 SCpnt->cmnd[1] = 0;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500410 block = (unsigned int)rq->sector / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (this_count > 0xffff) {
413 this_count = 0xffff;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200414 SCpnt->request_bufflen = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
418 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
419 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
420 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
421 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
422 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
423 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
424
425 /*
426 * We shouldn't disconnect in the middle of a sector, so with a dumb
427 * host adapter, it's safe to assume that we can at least transfer
428 * this many bytes between each connect / disconnect.
429 */
430 SCpnt->transfersize = cd->device->sector_size;
431 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 SCpnt->allowed = MAX_RETRIES;
433 SCpnt->timeout_per_command = timeout;
434
435 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * This indicates that the command is ready from our end to be
437 * queued.
438 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500439 ret = BLKPREP_OK;
440 out:
441 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static int sr_block_open(struct inode *inode, struct file *file)
445{
446 struct gendisk *disk = inode->i_bdev->bd_disk;
Jayachandran C80d904c2005-10-18 17:11:16 -0700447 struct scsi_cd *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int ret = 0;
449
450 if(!(cd = scsi_cd_get(disk)))
451 return -ENXIO;
452
453 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
454 scsi_cd_put(cd);
455
456 return ret;
457}
458
459static int sr_block_release(struct inode *inode, struct file *file)
460{
461 int ret;
462 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
463 ret = cdrom_release(&cd->cdi, file);
464 if(ret)
465 return ret;
466
467 scsi_cd_put(cd);
468
469 return 0;
470}
471
472static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
473 unsigned long arg)
474{
475 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
476 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800477 void __user *argp = (void __user *)arg;
478 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800480 /*
481 * Send SCSI addressing ioctls directly to mid level, send other
482 * ioctls to cdrom/block level.
483 */
484 switch (cmd) {
485 case SCSI_IOCTL_GET_IDLUN:
486 case SCSI_IOCTL_GET_BUS_NUMBER:
487 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800489
490 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900491 if (ret != -ENOSYS)
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800492 return ret;
493
494 /*
495 * ENODEV means that we didn't recognise the ioctl, or that we
496 * cannot execute it in the current device state. In either
497 * case fall through to scsi_ioctl, which will return ENDOEV again
498 * if it doesn't recognise the ioctl
499 */
500 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
501 if (ret != -ENODEV)
502 return ret;
503 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506static int sr_block_media_changed(struct gendisk *disk)
507{
508 struct scsi_cd *cd = scsi_cd(disk);
509 return cdrom_media_changed(&cd->cdi);
510}
511
512static struct block_device_operations sr_bdops =
513{
514 .owner = THIS_MODULE,
515 .open = sr_block_open,
516 .release = sr_block_release,
517 .ioctl = sr_block_ioctl,
518 .media_changed = sr_block_media_changed,
519 /*
520 * No compat_ioctl for now because sr_block_ioctl never
521 * seems to pass arbitary ioctls down to host drivers.
522 */
523};
524
525static int sr_open(struct cdrom_device_info *cdi, int purpose)
526{
527 struct scsi_cd *cd = cdi->handle;
528 struct scsi_device *sdev = cd->device;
529 int retval;
530
531 /*
532 * If the device is in error recovery, wait until it is done.
533 * If the device is offline, then disallow any access to it.
534 */
535 retval = -ENXIO;
536 if (!scsi_block_when_processing_errors(sdev))
537 goto error_out;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
540
541error_out:
542 return retval;
543}
544
545static void sr_release(struct cdrom_device_info *cdi)
546{
547 struct scsi_cd *cd = cdi->handle;
548
549 if (cd->device->sector_size > 2048)
550 sr_set_blocklength(cd, 2048);
551
552}
553
554static int sr_probe(struct device *dev)
555{
556 struct scsi_device *sdev = to_scsi_device(dev);
557 struct gendisk *disk;
558 struct scsi_cd *cd;
559 int minor, error;
560
561 error = -ENODEV;
562 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
563 goto fail;
564
565 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500566 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!cd)
568 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 kref_init(&cd->kref);
571
572 disk = alloc_disk(1);
573 if (!disk)
574 goto fail_free;
575
576 spin_lock(&sr_index_lock);
577 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
578 if (minor == SR_DISKS) {
579 spin_unlock(&sr_index_lock);
580 error = -EBUSY;
581 goto fail_put;
582 }
583 __set_bit(minor, sr_index_bits);
584 spin_unlock(&sr_index_lock);
585
586 disk->major = SCSI_CDROM_MAJOR;
587 disk->first_minor = minor;
588 sprintf(disk->disk_name, "sr%d", minor);
589 disk->fops = &sr_bdops;
590 disk->flags = GENHD_FL_CD;
591
592 cd->device = sdev;
593 cd->disk = disk;
594 cd->driver = &sr_template;
595 cd->disk = disk;
596 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 cd->device->changed = 1; /* force recheck CD type */
598 cd->use = 1;
599 cd->readcd_known = 0;
600 cd->readcd_cdda = 0;
601
602 cd->cdi.ops = &sr_dops;
603 cd->cdi.handle = cd;
604 cd->cdi.mask = 0;
605 cd->cdi.capacity = 1;
606 sprintf(cd->cdi.name, "sr%d", minor);
607
608 sdev->sector_size = 2048; /* A guess, just in case */
609
610 /* FIXME: need to handle a get_capabilities failure properly ?? */
611 get_capabilities(cd);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500612 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sr_vendor_init(cd);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 disk->driverfs_dev = &sdev->sdev_gendev;
616 set_capacity(disk, cd->capacity);
617 disk->private_data = &cd->driver;
618 disk->queue = sdev->request_queue;
619 cd->cdi.disk = disk;
620
621 if (register_cdrom(&cd->cdi))
622 goto fail_put;
623
624 dev_set_drvdata(dev, cd);
625 disk->flags |= GENHD_FL_REMOVABLE;
626 add_disk(disk);
627
James Bottomley9ccfc752005-10-02 11:45:08 -0500628 sdev_printk(KERN_DEBUG, sdev,
629 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 0;
631
632fail_put:
633 put_disk(disk);
634fail_free:
635 kfree(cd);
636fail:
637 return error;
638}
639
640
641static void get_sectorsize(struct scsi_cd *cd)
642{
643 unsigned char cmd[10];
644 unsigned char *buffer;
645 int the_result, retries = 3;
646 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200647 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
650 if (!buffer)
651 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 do {
654 cmd[0] = READ_CAPACITY;
655 memset((void *) &cmd[1], 0, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 memset(buffer, 0, 8);
657
658 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500659 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
660 buffer, 8, NULL, SR_TIMEOUT,
661 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 retries--;
664
665 } while (the_result && retries);
666
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (the_result) {
669 cd->capacity = 0x1fffff;
670 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 } else {
672#if 0
673 if (cdrom_get_last_written(&cd->cdi,
674 &cd->capacity))
675#endif
676 cd->capacity = 1 + ((buffer[0] << 24) |
677 (buffer[1] << 16) |
678 (buffer[2] << 8) |
679 buffer[3]);
680 sector_size = (buffer[4] << 24) |
681 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
682 switch (sector_size) {
683 /*
684 * HP 4020i CD-Recorder reports 2340 byte sectors
685 * Philips CD-Writers report 2352 byte sectors
686 *
687 * Use 2k sectors for them..
688 */
689 case 0:
690 case 2340:
691 case 2352:
692 sector_size = 2048;
693 /* fall through */
694 case 2048:
695 cd->capacity *= 4;
696 /* fall through */
697 case 512:
698 break;
699 default:
700 printk("%s: unsupported sector size %d.\n",
701 cd->cdi.name, sector_size);
702 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 cd->device->sector_size = sector_size;
706
707 /*
708 * Add this so that we have the ability to correctly gauge
709 * what the device is capable of.
710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 set_capacity(cd->disk, cd->capacity);
712 }
713
714 queue = cd->device->request_queue;
715 blk_queue_hardsect_size(queue, sector_size);
716out:
717 kfree(buffer);
718 return;
719
720Enomem:
721 cd->capacity = 0x1fffff;
Pete Zaitcev51490c82005-07-05 18:18:08 -0700722 cd->device->sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto out;
724}
725
726static void get_capabilities(struct scsi_cd *cd)
727{
728 unsigned char *buffer;
729 struct scsi_mode_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 unsigned char cmd[MAX_COMMAND_SIZE];
James Bottomley820732b2005-06-12 22:21:29 -0500731 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 unsigned int the_result;
733 int retries, rc, n;
734
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100735 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 {
737 "caddy",
738 "tray",
739 "pop-up",
740 "",
741 "changer",
742 "cartridge changer",
743 "",
744 ""
745 };
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* allocate transfer buffer */
749 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
750 if (!buffer) {
751 printk(KERN_ERR "sr: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return;
753 }
754
755 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
756 * conditions are gone, or a timeout happens
757 */
758 retries = 0;
759 do {
760 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
761 cmd[0] = TEST_UNIT_READY;
762
James Bottomley820732b2005-06-12 22:21:29 -0500763 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
764 0, &sshdr, SR_TIMEOUT,
765 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 retries++;
768 } while (retries < 5 &&
769 (!scsi_status_is_good(the_result) ||
James Bottomley820732b2005-06-12 22:21:29 -0500770 (scsi_sense_valid(&sshdr) &&
771 sshdr.sense_key == UNIT_ATTENTION)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* ask for mode page 0x2a */
774 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500775 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (!scsi_status_is_good(rc)) {
778 /* failed, drive doesn't have capabilities mode page */
779 cd->cdi.speed = 1;
780 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500781 CDC_DVD | CDC_DVD_RAM |
782 CDC_SELECT_DISC | CDC_SELECT_SPEED |
783 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 kfree(buffer);
785 printk("%s: scsi-1 drive\n", cd->cdi.name);
786 return;
787 }
788
789 n = data.header_length + data.block_descriptor_length;
790 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
791 cd->readcd_known = 1;
792 cd->readcd_cdda = buffer[n + 5] & 0x01;
793 /* print some capability bits */
794 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
795 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
796 cd->cdi.speed,
797 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
798 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
799 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
800 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
801 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
802 loadmech[buffer[n + 6] >> 5]);
803 if ((buffer[n + 6] >> 5) == 0)
804 /* caddy drives can't close tray... */
805 cd->cdi.mask |= CDC_CLOSE_TRAY;
806 if ((buffer[n + 2] & 0x8) == 0)
807 /* not a DVD drive */
808 cd->cdi.mask |= CDC_DVD;
809 if ((buffer[n + 3] & 0x20) == 0)
810 /* can't write DVD-RAM media */
811 cd->cdi.mask |= CDC_DVD_RAM;
812 if ((buffer[n + 3] & 0x10) == 0)
813 /* can't write DVD-R media */
814 cd->cdi.mask |= CDC_DVD_R;
815 if ((buffer[n + 3] & 0x2) == 0)
816 /* can't write CD-RW media */
817 cd->cdi.mask |= CDC_CD_RW;
818 if ((buffer[n + 3] & 0x1) == 0)
819 /* can't write CD-R media */
820 cd->cdi.mask |= CDC_CD_R;
821 if ((buffer[n + 6] & 0x8) == 0)
822 /* can't eject */
823 cd->cdi.mask |= CDC_OPEN_TRAY;
824
825 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
826 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
827 cd->cdi.capacity =
828 cdrom_number_of_slots(&cd->cdi);
829 if (cd->cdi.capacity <= 1)
830 /* not a changer */
831 cd->cdi.mask |= CDC_SELECT_DISC;
832 /*else I don't think it can close its tray
833 cd->cdi.mask |= CDC_CLOSE_TRAY; */
834
835 /*
836 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
837 */
838 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
839 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
840 cd->device->writeable = 1;
841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kfree(buffer);
844}
845
846/*
847 * sr_packet() is the entry point for the generic commands generated
848 * by the Uniform CD-ROM layer.
849 */
850static int sr_packet(struct cdrom_device_info *cdi,
851 struct packet_command *cgc)
852{
853 if (cgc->timeout <= 0)
854 cgc->timeout = IOCTL_TIMEOUT;
855
856 sr_do_ioctl(cdi->handle, cgc);
857
858 return cgc->stat;
859}
860
861/**
862 * sr_kref_release - Called to free the scsi_cd structure
863 * @kref: pointer to embedded kref
864 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100865 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * called on last put, you should always use the scsi_cd_get()
867 * scsi_cd_put() helpers which manipulate the semaphore directly
868 * and never do a direct kref_put().
869 **/
870static void sr_kref_release(struct kref *kref)
871{
872 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
873 struct gendisk *disk = cd->disk;
874
875 spin_lock(&sr_index_lock);
876 clear_bit(disk->first_minor, sr_index_bits);
877 spin_unlock(&sr_index_lock);
878
879 unregister_cdrom(&cd->cdi);
880
881 disk->private_data = NULL;
882
883 put_disk(disk);
884
885 kfree(cd);
886}
887
888static int sr_remove(struct device *dev)
889{
890 struct scsi_cd *cd = dev_get_drvdata(dev);
891
892 del_gendisk(cd->disk);
893
Arjan van de Ven0b950672006-01-11 13:16:10 +0100894 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100896 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 return 0;
899}
900
901static 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 Mitada3962f2007-06-26 00:39:33 +0900908 rc = scsi_register_driver(&sr_template.gendrv);
909 if (rc)
910 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
911
912 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static void __exit exit_sr(void)
916{
917 scsi_unregister_driver(&sr_template.gendrv);
918 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
919}
920
921module_init(init_sr);
922module_exit(exit_sr);
923MODULE_LICENSE("GPL");