blob: 50ba492502035afe85d0bd62899b02c132022c63 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define SR_CAPABILITIES \
71 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
72 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
Christoph Hellwig6a2900b2006-03-23 03:00:15 -080073 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
75 CDC_MRW|CDC_MRW_W|CDC_RAM)
76
77static int sr_probe(struct device *);
78static int sr_remove(struct device *);
Linus Torvalds7b3d9542008-01-06 10:17:12 -080079static int sr_done(struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static struct scsi_driver sr_template = {
82 .owner = THIS_MODULE,
83 .gendrv = {
84 .name = "sr",
85 .probe = sr_probe,
86 .remove = sr_remove,
87 },
Linus Torvalds7b3d9542008-01-06 10:17:12 -080088 .done = sr_done,
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;
James Bottomley001aac22007-12-02 19:10:40 +0200180 struct scsi_sense_hdr *sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (CDSL_CURRENT != slot) {
183 /* no changer support */
184 return -EINVAL;
185 }
186
James Bottomley001aac22007-12-02 19:10:40 +0200187 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
188 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES,
189 sshdr);
190 if (retval || (scsi_sense_valid(sshdr) &&
191 /* 0x3a is medium not present */
192 sshdr->asc == 0x3a)) {
193 /* Media not present or unable to test, unit probably not
194 * ready. This usually means there is no disc in the drive.
195 * Mark as changed, and we will figure it out later once
196 * the drive is available again.
197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 cd->device->changed = 1;
Kay Sievers285e9672007-08-14 14:10:39 +0200199 /* This will force a flush, if called from check_disk_change */
200 retval = 1;
201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 };
203
204 retval = cd->device->changed;
205 cd->device->changed = 0;
206 /* If the disk changed, the capacity will now be different,
207 * so we force a re-read of this information */
208 if (retval) {
209 /* check multisession offset etc */
210 sr_cd_check(cdi);
Pete Zaitcev51490c82005-07-05 18:18:08 -0700211 get_sectorsize(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Kay Sievers285e9672007-08-14 14:10:39 +0200213
214out:
215 /* Notify userspace, that media has changed. */
216 if (retval != cd->previous_state)
217 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
218 GFP_KERNEL);
219 cd->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200220 kfree(sshdr);
Kay Sievers285e9672007-08-14 14:10:39 +0200221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return retval;
223}
224
225/*
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800226 * sr_done is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800228 * It will be notified on the end of a SCSI read / write, and will take one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * of several actions based on success or failure.
230 */
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800231static int sr_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int result = SCpnt->result;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200234 int this_count = scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int good_bytes = (result == 0 ? this_count : 0);
236 int block_sectors = 0;
237 long error_sector;
238 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
239
240#ifdef DEBUG
241 printk("sr.c done: %x\n", result);
242#endif
243
244 /*
245 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
246 * success. Since this is a relatively rare error condition, no
247 * care is taken to avoid unnecessary additional work such as
248 * memcpy's that could be avoided.
249 */
250 if (driver_byte(result) != 0 && /* An error occurred */
251 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
252 switch (SCpnt->sense_buffer[2]) {
253 case MEDIUM_ERROR:
254 case VOLUME_OVERFLOW:
255 case ILLEGAL_REQUEST:
256 if (!(SCpnt->sense_buffer[0] & 0x90))
257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 error_sector = (SCpnt->sense_buffer[3] << 24) |
259 (SCpnt->sense_buffer[4] << 16) |
260 (SCpnt->sense_buffer[5] << 8) |
261 SCpnt->sense_buffer[6];
262 if (SCpnt->request->bio != NULL)
263 block_sectors =
264 bio_sectors(SCpnt->request->bio);
265 if (block_sectors < 4)
266 block_sectors = 4;
267 if (cd->device->sector_size == 2048)
268 error_sector <<= 2;
269 error_sector &= ~(block_sectors - 1);
270 good_bytes = (error_sector - SCpnt->request->sector) << 9;
271 if (good_bytes < 0 || good_bytes >= this_count)
272 good_bytes = 0;
273 /*
274 * The SCSI specification allows for the value
275 * returned by READ CAPACITY to be up to 75 2K
276 * sectors past the last readable block.
277 * Therefore, if we hit a medium error within the
278 * last 75 2K sectors, we decrease the saved size
279 * value.
280 */
281 if (error_sector < get_capacity(cd->disk) &&
282 cd->capacity - error_sector < 4 * 75)
283 set_capacity(cd->disk, error_sector);
284 break;
285
286 case RECOVERED_ERROR:
287
288 /*
289 * An error occured, but it recovered. Inform the
290 * user, but make sure that it's not treated as a
291 * hard error.
292 */
293 scsi_print_sense("sr", SCpnt);
294 SCpnt->result = 0;
295 SCpnt->sense_buffer[0] = 0x0;
296 good_bytes = this_count;
297 break;
298
299 default:
300 break;
301 }
302 }
303
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800304 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500307static int sr_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500310 struct scsi_cd *cd;
311 struct scsi_cmnd *SCpnt;
312 struct scsi_device *sdp = q->queuedata;
313 int ret;
314
315 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
316 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
317 goto out;
318 } else if (rq->cmd_type != REQ_TYPE_FS) {
319 ret = BLKPREP_KILL;
320 goto out;
321 }
322 ret = scsi_setup_fs_cmnd(sdp, rq);
323 if (ret != BLKPREP_OK)
324 goto out;
325 SCpnt = rq->special;
326 cd = scsi_cd(rq->rq_disk);
327
328 /* from here on until we're complete, any goto out
329 * is used for a killable error condition */
330 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
333 cd->disk->disk_name, block));
334
335 if (!cd->device || !scsi_device_online(cd->device)) {
336 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500337 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 if (cd->device->changed) {
343 /*
344 * quietly refuse to do anything to a changed disc until the
345 * changed bit has been reset
346 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * we do lazy blocksize switching (when reading XA sectors,
352 * see CDROMREADMODE2 ioctl)
353 */
354 s_size = cd->device->sector_size;
355 if (s_size > 2048) {
356 if (!in_interrupt())
357 sr_set_blocklength(cd, 2048);
358 else
359 printk("sr: can't switch blocksize: in interrupt\n");
360 }
361
362 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400363 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500367 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!cd->device->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 SCpnt->cmnd[0] = WRITE_10;
371 SCpnt->sc_data_direction = DMA_TO_DEVICE;
372 cd->cdi.media_written = 1;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500373 } else if (rq_data_dir(rq) == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 SCpnt->cmnd[0] = READ_10;
375 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
376 } else {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500377 blk_dump_rq_flags(rq, "Unknown sr command");
378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200382 struct scatterlist *sg;
383 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200385 scsi_for_each_sg(SCpnt, sg, sg_count, i)
386 size += sg->length;
387
388 if (size != scsi_bufflen(SCpnt)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400389 scmd_printk(KERN_ERR, SCpnt,
390 "mismatch count %d, bytes %d\n",
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200391 size, scsi_bufflen(SCpnt));
392 if (scsi_bufflen(SCpnt) > size)
393 SCpnt->sdb.length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 }
396
397 /*
398 * request doesn't start on hw block boundary, add scatter pads
399 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500400 if (((unsigned int)rq->sector % (s_size >> 9)) ||
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200401 (scsi_bufflen(SCpnt) % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400402 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500403 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200406 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408
409 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
410 cd->cdi.name,
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500411 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 "writing" : "reading",
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500413 this_count, rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 SCpnt->cmnd[1] = 0;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500416 block = (unsigned int)rq->sector / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (this_count > 0xffff) {
419 this_count = 0xffff;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200420 SCpnt->sdb.length = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
424 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
425 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
426 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
427 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
428 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
429 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
430
431 /*
432 * We shouldn't disconnect in the middle of a sector, so with a dumb
433 * host adapter, it's safe to assume that we can at least transfer
434 * this many bytes between each connect / disconnect.
435 */
436 SCpnt->transfersize = cd->device->sector_size;
437 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 SCpnt->allowed = MAX_RETRIES;
439 SCpnt->timeout_per_command = timeout;
440
441 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * This indicates that the command is ready from our end to be
443 * queued.
444 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500445 ret = BLKPREP_OK;
446 out:
447 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450static int sr_block_open(struct inode *inode, struct file *file)
451{
452 struct gendisk *disk = inode->i_bdev->bd_disk;
Jayachandran C80d904c2005-10-18 17:11:16 -0700453 struct scsi_cd *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int ret = 0;
455
456 if(!(cd = scsi_cd_get(disk)))
457 return -ENXIO;
458
459 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
460 scsi_cd_put(cd);
461
462 return ret;
463}
464
465static int sr_block_release(struct inode *inode, struct file *file)
466{
467 int ret;
468 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
469 ret = cdrom_release(&cd->cdi, file);
470 if(ret)
471 return ret;
472
473 scsi_cd_put(cd);
474
475 return 0;
476}
477
478static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
479 unsigned long arg)
480{
481 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
482 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800483 void __user *argp = (void __user *)arg;
484 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800486 /*
487 * Send SCSI addressing ioctls directly to mid level, send other
488 * ioctls to cdrom/block level.
489 */
490 switch (cmd) {
491 case SCSI_IOCTL_GET_IDLUN:
492 case SCSI_IOCTL_GET_BUS_NUMBER:
493 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800495
496 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900497 if (ret != -ENOSYS)
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800498 return ret;
499
500 /*
501 * ENODEV means that we didn't recognise the ioctl, or that we
502 * cannot execute it in the current device state. In either
503 * case fall through to scsi_ioctl, which will return ENDOEV again
504 * if it doesn't recognise the ioctl
505 */
506 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
507 if (ret != -ENODEV)
508 return ret;
509 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512static int sr_block_media_changed(struct gendisk *disk)
513{
514 struct scsi_cd *cd = scsi_cd(disk);
515 return cdrom_media_changed(&cd->cdi);
516}
517
518static struct block_device_operations sr_bdops =
519{
520 .owner = THIS_MODULE,
521 .open = sr_block_open,
522 .release = sr_block_release,
523 .ioctl = sr_block_ioctl,
524 .media_changed = sr_block_media_changed,
525 /*
526 * No compat_ioctl for now because sr_block_ioctl never
527 * seems to pass arbitary ioctls down to host drivers.
528 */
529};
530
531static int sr_open(struct cdrom_device_info *cdi, int purpose)
532{
533 struct scsi_cd *cd = cdi->handle;
534 struct scsi_device *sdev = cd->device;
535 int retval;
536
537 /*
538 * If the device is in error recovery, wait until it is done.
539 * If the device is offline, then disallow any access to it.
540 */
541 retval = -ENXIO;
542 if (!scsi_block_when_processing_errors(sdev))
543 goto error_out;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
546
547error_out:
548 return retval;
549}
550
551static void sr_release(struct cdrom_device_info *cdi)
552{
553 struct scsi_cd *cd = cdi->handle;
554
555 if (cd->device->sector_size > 2048)
556 sr_set_blocklength(cd, 2048);
557
558}
559
560static int sr_probe(struct device *dev)
561{
562 struct scsi_device *sdev = to_scsi_device(dev);
563 struct gendisk *disk;
564 struct scsi_cd *cd;
565 int minor, error;
566
567 error = -ENODEV;
568 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
569 goto fail;
570
571 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500572 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!cd)
574 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 kref_init(&cd->kref);
577
578 disk = alloc_disk(1);
579 if (!disk)
580 goto fail_free;
581
582 spin_lock(&sr_index_lock);
583 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
584 if (minor == SR_DISKS) {
585 spin_unlock(&sr_index_lock);
586 error = -EBUSY;
587 goto fail_put;
588 }
589 __set_bit(minor, sr_index_bits);
590 spin_unlock(&sr_index_lock);
591
592 disk->major = SCSI_CDROM_MAJOR;
593 disk->first_minor = minor;
594 sprintf(disk->disk_name, "sr%d", minor);
595 disk->fops = &sr_bdops;
596 disk->flags = GENHD_FL_CD;
597
598 cd->device = sdev;
599 cd->disk = disk;
600 cd->driver = &sr_template;
601 cd->disk = disk;
602 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 cd->device->changed = 1; /* force recheck CD type */
604 cd->use = 1;
605 cd->readcd_known = 0;
606 cd->readcd_cdda = 0;
607
608 cd->cdi.ops = &sr_dops;
609 cd->cdi.handle = cd;
610 cd->cdi.mask = 0;
611 cd->cdi.capacity = 1;
612 sprintf(cd->cdi.name, "sr%d", minor);
613
614 sdev->sector_size = 2048; /* A guess, just in case */
615
616 /* FIXME: need to handle a get_capabilities failure properly ?? */
617 get_capabilities(cd);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500618 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sr_vendor_init(cd);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 disk->driverfs_dev = &sdev->sdev_gendev;
622 set_capacity(disk, cd->capacity);
623 disk->private_data = &cd->driver;
624 disk->queue = sdev->request_queue;
625 cd->cdi.disk = disk;
626
627 if (register_cdrom(&cd->cdi))
628 goto fail_put;
629
630 dev_set_drvdata(dev, cd);
631 disk->flags |= GENHD_FL_REMOVABLE;
632 add_disk(disk);
633
James Bottomley9ccfc752005-10-02 11:45:08 -0500634 sdev_printk(KERN_DEBUG, sdev,
635 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
637
638fail_put:
639 put_disk(disk);
640fail_free:
641 kfree(cd);
642fail:
643 return error;
644}
645
646
647static void get_sectorsize(struct scsi_cd *cd)
648{
649 unsigned char cmd[10];
650 unsigned char *buffer;
651 int the_result, retries = 3;
652 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200653 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
656 if (!buffer)
657 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 do {
660 cmd[0] = READ_CAPACITY;
661 memset((void *) &cmd[1], 0, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 memset(buffer, 0, 8);
663
664 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500665 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
666 buffer, 8, NULL, SR_TIMEOUT,
667 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 retries--;
670
671 } while (the_result && retries);
672
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (the_result) {
675 cd->capacity = 0x1fffff;
676 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 } else {
678#if 0
679 if (cdrom_get_last_written(&cd->cdi,
680 &cd->capacity))
681#endif
682 cd->capacity = 1 + ((buffer[0] << 24) |
683 (buffer[1] << 16) |
684 (buffer[2] << 8) |
685 buffer[3]);
686 sector_size = (buffer[4] << 24) |
687 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
688 switch (sector_size) {
689 /*
690 * HP 4020i CD-Recorder reports 2340 byte sectors
691 * Philips CD-Writers report 2352 byte sectors
692 *
693 * Use 2k sectors for them..
694 */
695 case 0:
696 case 2340:
697 case 2352:
698 sector_size = 2048;
699 /* fall through */
700 case 2048:
701 cd->capacity *= 4;
702 /* fall through */
703 case 512:
704 break;
705 default:
706 printk("%s: unsupported sector size %d.\n",
707 cd->cdi.name, sector_size);
708 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 cd->device->sector_size = sector_size;
712
713 /*
714 * Add this so that we have the ability to correctly gauge
715 * what the device is capable of.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 set_capacity(cd->disk, cd->capacity);
718 }
719
720 queue = cd->device->request_queue;
721 blk_queue_hardsect_size(queue, sector_size);
722out:
723 kfree(buffer);
724 return;
725
726Enomem:
727 cd->capacity = 0x1fffff;
Pete Zaitcev51490c82005-07-05 18:18:08 -0700728 cd->device->sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto out;
730}
731
732static void get_capabilities(struct scsi_cd *cd)
733{
734 unsigned char *buffer;
735 struct scsi_mode_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unsigned char cmd[MAX_COMMAND_SIZE];
James Bottomley820732b2005-06-12 22:21:29 -0500737 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned int the_result;
739 int retries, rc, n;
740
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100741 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 {
743 "caddy",
744 "tray",
745 "pop-up",
746 "",
747 "changer",
748 "cartridge changer",
749 "",
750 ""
751 };
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* allocate transfer buffer */
755 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
756 if (!buffer) {
757 printk(KERN_ERR "sr: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return;
759 }
760
761 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
762 * conditions are gone, or a timeout happens
763 */
764 retries = 0;
765 do {
766 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
767 cmd[0] = TEST_UNIT_READY;
768
James Bottomley820732b2005-06-12 22:21:29 -0500769 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
770 0, &sshdr, SR_TIMEOUT,
771 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 retries++;
774 } while (retries < 5 &&
775 (!scsi_status_is_good(the_result) ||
James Bottomley820732b2005-06-12 22:21:29 -0500776 (scsi_sense_valid(&sshdr) &&
777 sshdr.sense_key == UNIT_ATTENTION)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* ask for mode page 0x2a */
780 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500781 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (!scsi_status_is_good(rc)) {
784 /* failed, drive doesn't have capabilities mode page */
785 cd->cdi.speed = 1;
786 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500787 CDC_DVD | CDC_DVD_RAM |
788 CDC_SELECT_DISC | CDC_SELECT_SPEED |
789 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 kfree(buffer);
791 printk("%s: scsi-1 drive\n", cd->cdi.name);
792 return;
793 }
794
795 n = data.header_length + data.block_descriptor_length;
796 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
797 cd->readcd_known = 1;
798 cd->readcd_cdda = buffer[n + 5] & 0x01;
799 /* print some capability bits */
800 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
801 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
802 cd->cdi.speed,
803 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
804 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
805 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
806 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
807 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
808 loadmech[buffer[n + 6] >> 5]);
809 if ((buffer[n + 6] >> 5) == 0)
810 /* caddy drives can't close tray... */
811 cd->cdi.mask |= CDC_CLOSE_TRAY;
812 if ((buffer[n + 2] & 0x8) == 0)
813 /* not a DVD drive */
814 cd->cdi.mask |= CDC_DVD;
815 if ((buffer[n + 3] & 0x20) == 0)
816 /* can't write DVD-RAM media */
817 cd->cdi.mask |= CDC_DVD_RAM;
818 if ((buffer[n + 3] & 0x10) == 0)
819 /* can't write DVD-R media */
820 cd->cdi.mask |= CDC_DVD_R;
821 if ((buffer[n + 3] & 0x2) == 0)
822 /* can't write CD-RW media */
823 cd->cdi.mask |= CDC_CD_RW;
824 if ((buffer[n + 3] & 0x1) == 0)
825 /* can't write CD-R media */
826 cd->cdi.mask |= CDC_CD_R;
827 if ((buffer[n + 6] & 0x8) == 0)
828 /* can't eject */
829 cd->cdi.mask |= CDC_OPEN_TRAY;
830
831 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
832 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
833 cd->cdi.capacity =
834 cdrom_number_of_slots(&cd->cdi);
835 if (cd->cdi.capacity <= 1)
836 /* not a changer */
837 cd->cdi.mask |= CDC_SELECT_DISC;
838 /*else I don't think it can close its tray
839 cd->cdi.mask |= CDC_CLOSE_TRAY; */
840
841 /*
842 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
843 */
844 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
845 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
846 cd->device->writeable = 1;
847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree(buffer);
850}
851
852/*
853 * sr_packet() is the entry point for the generic commands generated
854 * by the Uniform CD-ROM layer.
855 */
856static int sr_packet(struct cdrom_device_info *cdi,
857 struct packet_command *cgc)
858{
859 if (cgc->timeout <= 0)
860 cgc->timeout = IOCTL_TIMEOUT;
861
862 sr_do_ioctl(cdi->handle, cgc);
863
864 return cgc->stat;
865}
866
867/**
868 * sr_kref_release - Called to free the scsi_cd structure
869 * @kref: pointer to embedded kref
870 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100871 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * called on last put, you should always use the scsi_cd_get()
873 * scsi_cd_put() helpers which manipulate the semaphore directly
874 * and never do a direct kref_put().
875 **/
876static void sr_kref_release(struct kref *kref)
877{
878 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
879 struct gendisk *disk = cd->disk;
880
881 spin_lock(&sr_index_lock);
882 clear_bit(disk->first_minor, sr_index_bits);
883 spin_unlock(&sr_index_lock);
884
885 unregister_cdrom(&cd->cdi);
886
887 disk->private_data = NULL;
888
889 put_disk(disk);
890
891 kfree(cd);
892}
893
894static int sr_remove(struct device *dev)
895{
896 struct scsi_cd *cd = dev_get_drvdata(dev);
897
898 del_gendisk(cd->disk);
899
Arjan van de Ven0b950672006-01-11 13:16:10 +0100900 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100902 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return 0;
905}
906
907static int __init init_sr(void)
908{
909 int rc;
910
911 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
912 if (rc)
913 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +0900914 rc = scsi_register_driver(&sr_template.gendrv);
915 if (rc)
916 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
917
918 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921static void __exit exit_sr(void)
922{
923 scsi_unregister_driver(&sr_template.gendrv);
924 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
925}
926
927module_init(init_sr);
928module_exit(exit_sr);
929MODULE_LICENSE("GPL");