blob: 896be4ab285d53bab06a97f6d885ab6429591894 [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;
James Bottomley001aac22007-12-02 19:10:40 +0200182 struct scsi_sense_hdr *sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 if (CDSL_CURRENT != slot) {
185 /* no changer support */
186 return -EINVAL;
187 }
188
James Bottomley001aac22007-12-02 19:10:40 +0200189 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
190 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES,
191 sshdr);
192 if (retval || (scsi_sense_valid(sshdr) &&
193 /* 0x3a is medium not present */
194 sshdr->asc == 0x3a)) {
195 /* Media not present or unable to test, unit probably not
196 * ready. This usually means there is no disc in the drive.
197 * Mark as changed, and we will figure it out later once
198 * the drive is available again.
199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 cd->device->changed = 1;
Kay Sievers285e9672007-08-14 14:10:39 +0200201 /* This will force a flush, if called from check_disk_change */
202 retval = 1;
203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 };
205
206 retval = cd->device->changed;
207 cd->device->changed = 0;
208 /* If the disk changed, the capacity will now be different,
209 * so we force a re-read of this information */
210 if (retval) {
211 /* check multisession offset etc */
212 sr_cd_check(cdi);
Pete Zaitcev51490c82005-07-05 18:18:08 -0700213 get_sectorsize(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Kay Sievers285e9672007-08-14 14:10:39 +0200215
216out:
217 /* Notify userspace, that media has changed. */
218 if (retval != cd->previous_state)
219 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
220 GFP_KERNEL);
221 cd->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200222 kfree(sshdr);
Kay Sievers285e9672007-08-14 14:10:39 +0200223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return retval;
225}
226
227/*
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800228 * sr_done is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 *
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800230 * It will be notified on the end of a SCSI read / write, and will take one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * of several actions based on success or failure.
232 */
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800233static int sr_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int result = SCpnt->result;
Christoph Hellwig5d5ff442006-06-03 13:21:13 +0200236 int this_count = SCpnt->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int good_bytes = (result == 0 ? this_count : 0);
238 int block_sectors = 0;
239 long error_sector;
240 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
241
242#ifdef DEBUG
243 printk("sr.c done: %x\n", result);
244#endif
245
246 /*
247 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
248 * success. Since this is a relatively rare error condition, no
249 * care is taken to avoid unnecessary additional work such as
250 * memcpy's that could be avoided.
251 */
252 if (driver_byte(result) != 0 && /* An error occurred */
253 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
254 switch (SCpnt->sense_buffer[2]) {
255 case MEDIUM_ERROR:
256 case VOLUME_OVERFLOW:
257 case ILLEGAL_REQUEST:
258 if (!(SCpnt->sense_buffer[0] & 0x90))
259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 error_sector = (SCpnt->sense_buffer[3] << 24) |
261 (SCpnt->sense_buffer[4] << 16) |
262 (SCpnt->sense_buffer[5] << 8) |
263 SCpnt->sense_buffer[6];
264 if (SCpnt->request->bio != NULL)
265 block_sectors =
266 bio_sectors(SCpnt->request->bio);
267 if (block_sectors < 4)
268 block_sectors = 4;
269 if (cd->device->sector_size == 2048)
270 error_sector <<= 2;
271 error_sector &= ~(block_sectors - 1);
272 good_bytes = (error_sector - SCpnt->request->sector) << 9;
273 if (good_bytes < 0 || good_bytes >= this_count)
274 good_bytes = 0;
275 /*
276 * The SCSI specification allows for the value
277 * returned by READ CAPACITY to be up to 75 2K
278 * sectors past the last readable block.
279 * Therefore, if we hit a medium error within the
280 * last 75 2K sectors, we decrease the saved size
281 * value.
282 */
283 if (error_sector < get_capacity(cd->disk) &&
284 cd->capacity - error_sector < 4 * 75)
285 set_capacity(cd->disk, error_sector);
286 break;
287
288 case RECOVERED_ERROR:
289
290 /*
291 * An error occured, but it recovered. Inform the
292 * user, but make sure that it's not treated as a
293 * hard error.
294 */
295 scsi_print_sense("sr", SCpnt);
296 SCpnt->result = 0;
297 SCpnt->sense_buffer[0] = 0x0;
298 good_bytes = this_count;
299 break;
300
301 default:
302 break;
303 }
304 }
305
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800306 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500309static int sr_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500312 struct scsi_cd *cd;
313 struct scsi_cmnd *SCpnt;
314 struct scsi_device *sdp = q->queuedata;
315 int ret;
316
317 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
318 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
319 goto out;
320 } else if (rq->cmd_type != REQ_TYPE_FS) {
321 ret = BLKPREP_KILL;
322 goto out;
323 }
324 ret = scsi_setup_fs_cmnd(sdp, rq);
325 if (ret != BLKPREP_OK)
326 goto out;
327 SCpnt = rq->special;
328 cd = scsi_cd(rq->rq_disk);
329
330 /* from here on until we're complete, any goto out
331 * is used for a killable error condition */
332 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
335 cd->disk->disk_name, block));
336
337 if (!cd->device || !scsi_device_online(cd->device)) {
338 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500339 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 if (cd->device->changed) {
345 /*
346 * quietly refuse to do anything to a changed disc until the
347 * changed bit has been reset
348 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500349 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * we do lazy blocksize switching (when reading XA sectors,
354 * see CDROMREADMODE2 ioctl)
355 */
356 s_size = cd->device->sector_size;
357 if (s_size > 2048) {
358 if (!in_interrupt())
359 sr_set_blocklength(cd, 2048);
360 else
361 printk("sr: can't switch blocksize: in interrupt\n");
362 }
363
364 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400365 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500369 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!cd->device->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 SCpnt->cmnd[0] = WRITE_10;
373 SCpnt->sc_data_direction = DMA_TO_DEVICE;
374 cd->cdi.media_written = 1;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500375 } else if (rq_data_dir(rq) == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 SCpnt->cmnd[0] = READ_10;
377 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
378 } else {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500379 blk_dump_rq_flags(rq, "Unknown sr command");
380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 {
384 struct scatterlist *sg = SCpnt->request_buffer;
385 int i, size = 0;
386 for (i = 0; i < SCpnt->use_sg; i++)
387 size += sg[i].length;
388
389 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400390 scmd_printk(KERN_ERR, SCpnt,
391 "mismatch count %d, bytes %d\n",
392 size, SCpnt->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (SCpnt->request_bufflen > size)
Christoph Hellwig631c2282006-07-08 20:42:15 +0200394 SCpnt->request_bufflen = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397
398 /*
399 * request doesn't start on hw block boundary, add scatter pads
400 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500401 if (((unsigned int)rq->sector % (s_size >> 9)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 (SCpnt->request_bufflen % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400403 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500404 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
408
409
410 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
411 cd->cdi.name,
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500412 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 "writing" : "reading",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500414 this_count, rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 SCpnt->cmnd[1] = 0;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500417 block = (unsigned int)rq->sector / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (this_count > 0xffff) {
420 this_count = 0xffff;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200421 SCpnt->request_bufflen = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
425 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
426 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
427 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
428 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
429 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
430 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
431
432 /*
433 * We shouldn't disconnect in the middle of a sector, so with a dumb
434 * host adapter, it's safe to assume that we can at least transfer
435 * this many bytes between each connect / disconnect.
436 */
437 SCpnt->transfersize = cd->device->sector_size;
438 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 SCpnt->allowed = MAX_RETRIES;
440 SCpnt->timeout_per_command = timeout;
441
442 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * This indicates that the command is ready from our end to be
444 * queued.
445 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500446 ret = BLKPREP_OK;
447 out:
448 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static int sr_block_open(struct inode *inode, struct file *file)
452{
453 struct gendisk *disk = inode->i_bdev->bd_disk;
Jayachandran C80d904c2005-10-18 17:11:16 -0700454 struct scsi_cd *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int ret = 0;
456
457 if(!(cd = scsi_cd_get(disk)))
458 return -ENXIO;
459
460 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
461 scsi_cd_put(cd);
462
463 return ret;
464}
465
466static int sr_block_release(struct inode *inode, struct file *file)
467{
468 int ret;
469 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
470 ret = cdrom_release(&cd->cdi, file);
471 if(ret)
472 return ret;
473
474 scsi_cd_put(cd);
475
476 return 0;
477}
478
479static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
480 unsigned long arg)
481{
482 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
483 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800484 void __user *argp = (void __user *)arg;
485 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800487 /*
488 * Send SCSI addressing ioctls directly to mid level, send other
489 * ioctls to cdrom/block level.
490 */
491 switch (cmd) {
492 case SCSI_IOCTL_GET_IDLUN:
493 case SCSI_IOCTL_GET_BUS_NUMBER:
494 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800496
497 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900498 if (ret != -ENOSYS)
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800499 return ret;
500
501 /*
502 * ENODEV means that we didn't recognise the ioctl, or that we
503 * cannot execute it in the current device state. In either
504 * case fall through to scsi_ioctl, which will return ENDOEV again
505 * if it doesn't recognise the ioctl
506 */
507 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
508 if (ret != -ENODEV)
509 return ret;
510 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513static int sr_block_media_changed(struct gendisk *disk)
514{
515 struct scsi_cd *cd = scsi_cd(disk);
516 return cdrom_media_changed(&cd->cdi);
517}
518
519static struct block_device_operations sr_bdops =
520{
521 .owner = THIS_MODULE,
522 .open = sr_block_open,
523 .release = sr_block_release,
524 .ioctl = sr_block_ioctl,
525 .media_changed = sr_block_media_changed,
526 /*
527 * No compat_ioctl for now because sr_block_ioctl never
528 * seems to pass arbitary ioctls down to host drivers.
529 */
530};
531
532static int sr_open(struct cdrom_device_info *cdi, int purpose)
533{
534 struct scsi_cd *cd = cdi->handle;
535 struct scsi_device *sdev = cd->device;
536 int retval;
537
538 /*
539 * If the device is in error recovery, wait until it is done.
540 * If the device is offline, then disallow any access to it.
541 */
542 retval = -ENXIO;
543 if (!scsi_block_when_processing_errors(sdev))
544 goto error_out;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547
548error_out:
549 return retval;
550}
551
552static void sr_release(struct cdrom_device_info *cdi)
553{
554 struct scsi_cd *cd = cdi->handle;
555
556 if (cd->device->sector_size > 2048)
557 sr_set_blocklength(cd, 2048);
558
559}
560
561static int sr_probe(struct device *dev)
562{
563 struct scsi_device *sdev = to_scsi_device(dev);
564 struct gendisk *disk;
565 struct scsi_cd *cd;
566 int minor, error;
567
568 error = -ENODEV;
569 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
570 goto fail;
571
572 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500573 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!cd)
575 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 kref_init(&cd->kref);
578
579 disk = alloc_disk(1);
580 if (!disk)
581 goto fail_free;
582
583 spin_lock(&sr_index_lock);
584 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
585 if (minor == SR_DISKS) {
586 spin_unlock(&sr_index_lock);
587 error = -EBUSY;
588 goto fail_put;
589 }
590 __set_bit(minor, sr_index_bits);
591 spin_unlock(&sr_index_lock);
592
593 disk->major = SCSI_CDROM_MAJOR;
594 disk->first_minor = minor;
595 sprintf(disk->disk_name, "sr%d", minor);
596 disk->fops = &sr_bdops;
597 disk->flags = GENHD_FL_CD;
598
599 cd->device = sdev;
600 cd->disk = disk;
601 cd->driver = &sr_template;
602 cd->disk = disk;
603 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 cd->device->changed = 1; /* force recheck CD type */
605 cd->use = 1;
606 cd->readcd_known = 0;
607 cd->readcd_cdda = 0;
608
609 cd->cdi.ops = &sr_dops;
610 cd->cdi.handle = cd;
611 cd->cdi.mask = 0;
612 cd->cdi.capacity = 1;
613 sprintf(cd->cdi.name, "sr%d", minor);
614
615 sdev->sector_size = 2048; /* A guess, just in case */
616
617 /* FIXME: need to handle a get_capabilities failure properly ?? */
618 get_capabilities(cd);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500619 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 sr_vendor_init(cd);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 disk->driverfs_dev = &sdev->sdev_gendev;
623 set_capacity(disk, cd->capacity);
624 disk->private_data = &cd->driver;
625 disk->queue = sdev->request_queue;
626 cd->cdi.disk = disk;
627
628 if (register_cdrom(&cd->cdi))
629 goto fail_put;
630
631 dev_set_drvdata(dev, cd);
632 disk->flags |= GENHD_FL_REMOVABLE;
633 add_disk(disk);
634
James Bottomley9ccfc752005-10-02 11:45:08 -0500635 sdev_printk(KERN_DEBUG, sdev,
636 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638
639fail_put:
640 put_disk(disk);
641fail_free:
642 kfree(cd);
643fail:
644 return error;
645}
646
647
648static void get_sectorsize(struct scsi_cd *cd)
649{
650 unsigned char cmd[10];
651 unsigned char *buffer;
652 int the_result, retries = 3;
653 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200654 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
657 if (!buffer)
658 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 do {
661 cmd[0] = READ_CAPACITY;
662 memset((void *) &cmd[1], 0, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 memset(buffer, 0, 8);
664
665 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500666 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
667 buffer, 8, NULL, SR_TIMEOUT,
668 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 retries--;
671
672 } while (the_result && retries);
673
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (the_result) {
676 cd->capacity = 0x1fffff;
677 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 } else {
679#if 0
680 if (cdrom_get_last_written(&cd->cdi,
681 &cd->capacity))
682#endif
683 cd->capacity = 1 + ((buffer[0] << 24) |
684 (buffer[1] << 16) |
685 (buffer[2] << 8) |
686 buffer[3]);
687 sector_size = (buffer[4] << 24) |
688 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
689 switch (sector_size) {
690 /*
691 * HP 4020i CD-Recorder reports 2340 byte sectors
692 * Philips CD-Writers report 2352 byte sectors
693 *
694 * Use 2k sectors for them..
695 */
696 case 0:
697 case 2340:
698 case 2352:
699 sector_size = 2048;
700 /* fall through */
701 case 2048:
702 cd->capacity *= 4;
703 /* fall through */
704 case 512:
705 break;
706 default:
707 printk("%s: unsupported sector size %d.\n",
708 cd->cdi.name, sector_size);
709 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 cd->device->sector_size = sector_size;
713
714 /*
715 * Add this so that we have the ability to correctly gauge
716 * what the device is capable of.
717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 set_capacity(cd->disk, cd->capacity);
719 }
720
721 queue = cd->device->request_queue;
722 blk_queue_hardsect_size(queue, sector_size);
723out:
724 kfree(buffer);
725 return;
726
727Enomem:
728 cd->capacity = 0x1fffff;
Pete Zaitcev51490c82005-07-05 18:18:08 -0700729 cd->device->sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto out;
731}
732
733static void get_capabilities(struct scsi_cd *cd)
734{
735 unsigned char *buffer;
736 struct scsi_mode_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 unsigned char cmd[MAX_COMMAND_SIZE];
James Bottomley820732b2005-06-12 22:21:29 -0500738 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 unsigned int the_result;
740 int retries, rc, n;
741
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100742 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 {
744 "caddy",
745 "tray",
746 "pop-up",
747 "",
748 "changer",
749 "cartridge changer",
750 "",
751 ""
752 };
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* allocate transfer buffer */
756 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
757 if (!buffer) {
758 printk(KERN_ERR "sr: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return;
760 }
761
762 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
763 * conditions are gone, or a timeout happens
764 */
765 retries = 0;
766 do {
767 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
768 cmd[0] = TEST_UNIT_READY;
769
James Bottomley820732b2005-06-12 22:21:29 -0500770 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
771 0, &sshdr, SR_TIMEOUT,
772 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 retries++;
775 } while (retries < 5 &&
776 (!scsi_status_is_good(the_result) ||
James Bottomley820732b2005-06-12 22:21:29 -0500777 (scsi_sense_valid(&sshdr) &&
778 sshdr.sense_key == UNIT_ATTENTION)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* ask for mode page 0x2a */
781 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500782 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (!scsi_status_is_good(rc)) {
785 /* failed, drive doesn't have capabilities mode page */
786 cd->cdi.speed = 1;
787 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500788 CDC_DVD | CDC_DVD_RAM |
789 CDC_SELECT_DISC | CDC_SELECT_SPEED |
790 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 kfree(buffer);
792 printk("%s: scsi-1 drive\n", cd->cdi.name);
793 return;
794 }
795
796 n = data.header_length + data.block_descriptor_length;
797 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
798 cd->readcd_known = 1;
799 cd->readcd_cdda = buffer[n + 5] & 0x01;
800 /* print some capability bits */
801 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
802 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
803 cd->cdi.speed,
804 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
805 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
806 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
807 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
808 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
809 loadmech[buffer[n + 6] >> 5]);
810 if ((buffer[n + 6] >> 5) == 0)
811 /* caddy drives can't close tray... */
812 cd->cdi.mask |= CDC_CLOSE_TRAY;
813 if ((buffer[n + 2] & 0x8) == 0)
814 /* not a DVD drive */
815 cd->cdi.mask |= CDC_DVD;
816 if ((buffer[n + 3] & 0x20) == 0)
817 /* can't write DVD-RAM media */
818 cd->cdi.mask |= CDC_DVD_RAM;
819 if ((buffer[n + 3] & 0x10) == 0)
820 /* can't write DVD-R media */
821 cd->cdi.mask |= CDC_DVD_R;
822 if ((buffer[n + 3] & 0x2) == 0)
823 /* can't write CD-RW media */
824 cd->cdi.mask |= CDC_CD_RW;
825 if ((buffer[n + 3] & 0x1) == 0)
826 /* can't write CD-R media */
827 cd->cdi.mask |= CDC_CD_R;
828 if ((buffer[n + 6] & 0x8) == 0)
829 /* can't eject */
830 cd->cdi.mask |= CDC_OPEN_TRAY;
831
832 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
833 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
834 cd->cdi.capacity =
835 cdrom_number_of_slots(&cd->cdi);
836 if (cd->cdi.capacity <= 1)
837 /* not a changer */
838 cd->cdi.mask |= CDC_SELECT_DISC;
839 /*else I don't think it can close its tray
840 cd->cdi.mask |= CDC_CLOSE_TRAY; */
841
842 /*
843 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
844 */
845 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
846 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
847 cd->device->writeable = 1;
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 kfree(buffer);
851}
852
853/*
854 * sr_packet() is the entry point for the generic commands generated
855 * by the Uniform CD-ROM layer.
856 */
857static int sr_packet(struct cdrom_device_info *cdi,
858 struct packet_command *cgc)
859{
860 if (cgc->timeout <= 0)
861 cgc->timeout = IOCTL_TIMEOUT;
862
863 sr_do_ioctl(cdi->handle, cgc);
864
865 return cgc->stat;
866}
867
868/**
869 * sr_kref_release - Called to free the scsi_cd structure
870 * @kref: pointer to embedded kref
871 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100872 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * called on last put, you should always use the scsi_cd_get()
874 * scsi_cd_put() helpers which manipulate the semaphore directly
875 * and never do a direct kref_put().
876 **/
877static void sr_kref_release(struct kref *kref)
878{
879 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
880 struct gendisk *disk = cd->disk;
881
882 spin_lock(&sr_index_lock);
883 clear_bit(disk->first_minor, sr_index_bits);
884 spin_unlock(&sr_index_lock);
885
886 unregister_cdrom(&cd->cdi);
887
888 disk->private_data = NULL;
889
890 put_disk(disk);
891
892 kfree(cd);
893}
894
895static int sr_remove(struct device *dev)
896{
897 struct scsi_cd *cd = dev_get_drvdata(dev);
898
899 del_gendisk(cd->disk);
900
Arjan van de Ven0b950672006-01-11 13:16:10 +0100901 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100903 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 return 0;
906}
907
908static int __init init_sr(void)
909{
910 int rc;
911
912 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
913 if (rc)
914 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +0900915 rc = scsi_register_driver(&sr_template.gendrv);
916 if (rc)
917 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
918
919 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922static void __exit exit_sr(void)
923{
924 scsi_unregister_driver(&sr_template.gendrv);
925 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
926}
927
928module_init(init_sr);
929module_exit(exit_sr);
930MODULE_LICENSE("GPL");