blob: cce0fe4c8a3b91fb86f535fa2ae6d6bf2f79f226 [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
James Bottomley38582a62008-02-06 13:01:58 -0600166/* identical to scsi_test_unit_ready except that it doesn't
167 * eat the NOT_READY returns for removable media */
168int sr_test_unit_ready(struct scsi_device *sdev, struct scsi_sense_hdr *sshdr)
169{
170 int retries = MAX_RETRIES;
171 int the_result;
172 u8 cmd[] = {TEST_UNIT_READY, 0, 0, 0, 0, 0 };
173
174 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
175 * conditions are gone, or a timeout happens
176 */
177 do {
178 the_result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL,
179 0, sshdr, SR_TIMEOUT,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900180 retries--, NULL);
James Bottomleyd1daeab2008-06-10 10:20:53 -0500181 if (scsi_sense_valid(sshdr) &&
182 sshdr->sense_key == UNIT_ATTENTION)
183 sdev->changed = 1;
James Bottomley38582a62008-02-06 13:01:58 -0600184
185 } while (retries > 0 &&
186 (!scsi_status_is_good(the_result) ||
187 (scsi_sense_valid(sshdr) &&
188 sshdr->sense_key == UNIT_ATTENTION)));
189 return the_result;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * This function checks to see if the media has been changed in the
194 * CDROM drive. It is possible that we have already sensed a change,
195 * or the drive may have sensed one and not yet reported it. We must
196 * be ready for either case. This function always reports the current
197 * value of the changed bit. If flag is 0, then the changed bit is reset.
198 * This function could be done as an ioctl, but we would need to have
199 * an inode for that to work, and we do not always have one.
200 */
201
Adrian Bunk44818ef2007-07-09 11:59:59 -0700202static int sr_media_change(struct cdrom_device_info *cdi, int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct scsi_cd *cd = cdi->handle;
205 int retval;
James Bottomley001aac22007-12-02 19:10:40 +0200206 struct scsi_sense_hdr *sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (CDSL_CURRENT != slot) {
209 /* no changer support */
210 return -EINVAL;
211 }
212
James Bottomley001aac22007-12-02 19:10:40 +0200213 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
James Bottomley38582a62008-02-06 13:01:58 -0600214 retval = sr_test_unit_ready(cd->device, sshdr);
James Bottomley001aac22007-12-02 19:10:40 +0200215 if (retval || (scsi_sense_valid(sshdr) &&
216 /* 0x3a is medium not present */
217 sshdr->asc == 0x3a)) {
218 /* Media not present or unable to test, unit probably not
219 * ready. This usually means there is no disc in the drive.
220 * Mark as changed, and we will figure it out later once
221 * the drive is available again.
222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 cd->device->changed = 1;
Kay Sievers285e9672007-08-14 14:10:39 +0200224 /* This will force a flush, if called from check_disk_change */
225 retval = 1;
226 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 };
228
229 retval = cd->device->changed;
230 cd->device->changed = 0;
231 /* If the disk changed, the capacity will now be different,
232 * so we force a re-read of this information */
233 if (retval) {
234 /* check multisession offset etc */
235 sr_cd_check(cdi);
Pete Zaitcev51490c82005-07-05 18:18:08 -0700236 get_sectorsize(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Kay Sievers285e9672007-08-14 14:10:39 +0200238
239out:
240 /* Notify userspace, that media has changed. */
241 if (retval != cd->previous_state)
242 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
243 GFP_KERNEL);
244 cd->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200245 kfree(sshdr);
Kay Sievers285e9672007-08-14 14:10:39 +0200246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return retval;
248}
249
250/*
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800251 * sr_done is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800253 * It will be notified on the end of a SCSI read / write, and will take one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * of several actions based on success or failure.
255 */
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800256static int sr_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int result = SCpnt->result;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200259 int this_count = scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int good_bytes = (result == 0 ? this_count : 0);
261 int block_sectors = 0;
262 long error_sector;
263 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
264
265#ifdef DEBUG
266 printk("sr.c done: %x\n", result);
267#endif
268
269 /*
270 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
271 * success. Since this is a relatively rare error condition, no
272 * care is taken to avoid unnecessary additional work such as
273 * memcpy's that could be avoided.
274 */
275 if (driver_byte(result) != 0 && /* An error occurred */
276 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
277 switch (SCpnt->sense_buffer[2]) {
278 case MEDIUM_ERROR:
279 case VOLUME_OVERFLOW:
280 case ILLEGAL_REQUEST:
281 if (!(SCpnt->sense_buffer[0] & 0x90))
282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 error_sector = (SCpnt->sense_buffer[3] << 24) |
284 (SCpnt->sense_buffer[4] << 16) |
285 (SCpnt->sense_buffer[5] << 8) |
286 SCpnt->sense_buffer[6];
287 if (SCpnt->request->bio != NULL)
288 block_sectors =
289 bio_sectors(SCpnt->request->bio);
290 if (block_sectors < 4)
291 block_sectors = 4;
292 if (cd->device->sector_size == 2048)
293 error_sector <<= 2;
294 error_sector &= ~(block_sectors - 1);
Tejun Heo83096eb2009-05-07 22:24:39 +0900295 good_bytes = (error_sector -
296 blk_rq_pos(SCpnt->request)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (good_bytes < 0 || good_bytes >= this_count)
298 good_bytes = 0;
299 /*
300 * The SCSI specification allows for the value
301 * returned by READ CAPACITY to be up to 75 2K
302 * sectors past the last readable block.
303 * Therefore, if we hit a medium error within the
304 * last 75 2K sectors, we decrease the saved size
305 * value.
306 */
307 if (error_sector < get_capacity(cd->disk) &&
308 cd->capacity - error_sector < 4 * 75)
309 set_capacity(cd->disk, error_sector);
310 break;
311
312 case RECOVERED_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 good_bytes = this_count;
314 break;
315
316 default:
317 break;
318 }
319 }
320
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800321 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500324static int sr_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700326 int block = 0, this_count, s_size;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500327 struct scsi_cd *cd;
328 struct scsi_cmnd *SCpnt;
329 struct scsi_device *sdp = q->queuedata;
330 int ret;
331
332 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
333 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
334 goto out;
335 } else if (rq->cmd_type != REQ_TYPE_FS) {
336 ret = BLKPREP_KILL;
337 goto out;
338 }
339 ret = scsi_setup_fs_cmnd(sdp, rq);
340 if (ret != BLKPREP_OK)
341 goto out;
342 SCpnt = rq->special;
343 cd = scsi_cd(rq->rq_disk);
344
345 /* from here on until we're complete, any goto out
346 * is used for a killable error condition */
347 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
350 cd->disk->disk_name, block));
351
352 if (!cd->device || !scsi_device_online(cd->device)) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900353 SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
354 blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 if (cd->device->changed) {
360 /*
361 * quietly refuse to do anything to a changed disc until the
362 * changed bit has been reset
363 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
367 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * we do lazy blocksize switching (when reading XA sectors,
369 * see CDROMREADMODE2 ioctl)
370 */
371 s_size = cd->device->sector_size;
372 if (s_size > 2048) {
373 if (!in_interrupt())
374 sr_set_blocklength(cd, 2048);
375 else
376 printk("sr: can't switch blocksize: in interrupt\n");
377 }
378
379 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400380 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500384 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!cd->device->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 SCpnt->cmnd[0] = WRITE_10;
388 SCpnt->sc_data_direction = DMA_TO_DEVICE;
389 cd->cdi.media_written = 1;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500390 } else if (rq_data_dir(rq) == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 SCpnt->cmnd[0] = READ_10;
392 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
393 } else {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500394 blk_dump_rq_flags(rq, "Unknown sr command");
395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200399 struct scatterlist *sg;
400 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200402 scsi_for_each_sg(SCpnt, sg, sg_count, i)
403 size += sg->length;
404
405 if (size != scsi_bufflen(SCpnt)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400406 scmd_printk(KERN_ERR, SCpnt,
407 "mismatch count %d, bytes %d\n",
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200408 size, scsi_bufflen(SCpnt));
409 if (scsi_bufflen(SCpnt) > size)
410 SCpnt->sdb.length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
413
414 /*
415 * request doesn't start on hw block boundary, add scatter pads
416 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900417 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200418 (scsi_bufflen(SCpnt) % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400419 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500420 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200423 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425
Tejun Heo83096eb2009-05-07 22:24:39 +0900426 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 cd->cdi.name,
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500428 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 "writing" : "reading",
Tejun Heo83096eb2009-05-07 22:24:39 +0900430 this_count, blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 SCpnt->cmnd[1] = 0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900433 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (this_count > 0xffff) {
436 this_count = 0xffff;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200437 SCpnt->sdb.length = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
441 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
442 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
443 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
444 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
445 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
446 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
447
448 /*
449 * We shouldn't disconnect in the middle of a sector, so with a dumb
450 * host adapter, it's safe to assume that we can at least transfer
451 * this many bytes between each connect / disconnect.
452 */
453 SCpnt->transfersize = cd->device->sector_size;
454 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 SCpnt->allowed = MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * This indicates that the command is ready from our end to be
459 * queued.
460 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500461 ret = BLKPREP_OK;
462 out:
463 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Al Viro40cc51b2008-03-02 10:41:28 -0500466static int sr_block_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Al Viro40cc51b2008-03-02 10:41:28 -0500468 struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk);
469 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Al Viro40cc51b2008-03-02 10:41:28 -0500471 if (cd) {
472 ret = cdrom_open(&cd->cdi, bdev, mode);
473 if (ret)
474 scsi_cd_put(cd);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return ret;
477}
478
Al Viro40cc51b2008-03-02 10:41:28 -0500479static int sr_block_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Al Viro40cc51b2008-03-02 10:41:28 -0500481 struct scsi_cd *cd = scsi_cd(disk);
482 cdrom_release(&cd->cdi, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 scsi_cd_put(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
Al Viro40cc51b2008-03-02 10:41:28 -0500487static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unsigned long arg)
489{
Al Viro40cc51b2008-03-02 10:41:28 -0500490 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800492 void __user *argp = (void __user *)arg;
493 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800495 /*
496 * Send SCSI addressing ioctls directly to mid level, send other
497 * ioctls to cdrom/block level.
498 */
499 switch (cmd) {
500 case SCSI_IOCTL_GET_IDLUN:
501 case SCSI_IOCTL_GET_BUS_NUMBER:
502 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800504
Al Viro40cc51b2008-03-02 10:41:28 -0500505 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900506 if (ret != -ENOSYS)
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800507 return ret;
508
509 /*
510 * ENODEV means that we didn't recognise the ioctl, or that we
511 * cannot execute it in the current device state. In either
512 * case fall through to scsi_ioctl, which will return ENDOEV again
513 * if it doesn't recognise the ioctl
514 */
Al Viro83ff6fe2008-03-02 08:15:49 -0500515 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100516 (mode & FMODE_NDELAY) != 0);
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800517 if (ret != -ENODEV)
518 return ret;
519 return scsi_ioctl(sdev, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static int sr_block_media_changed(struct gendisk *disk)
523{
524 struct scsi_cd *cd = scsi_cd(disk);
525 return cdrom_media_changed(&cd->cdi);
526}
527
528static struct block_device_operations sr_bdops =
529{
530 .owner = THIS_MODULE,
Al Viro40cc51b2008-03-02 10:41:28 -0500531 .open = sr_block_open,
532 .release = sr_block_release,
533 .locked_ioctl = sr_block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .media_changed = sr_block_media_changed,
535 /*
536 * No compat_ioctl for now because sr_block_ioctl never
537 * seems to pass arbitary ioctls down to host drivers.
538 */
539};
540
541static int sr_open(struct cdrom_device_info *cdi, int purpose)
542{
543 struct scsi_cd *cd = cdi->handle;
544 struct scsi_device *sdev = cd->device;
545 int retval;
546
547 /*
548 * If the device is in error recovery, wait until it is done.
549 * If the device is offline, then disallow any access to it.
550 */
551 retval = -ENXIO;
552 if (!scsi_block_when_processing_errors(sdev))
553 goto error_out;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556
557error_out:
558 return retval;
559}
560
561static void sr_release(struct cdrom_device_info *cdi)
562{
563 struct scsi_cd *cd = cdi->handle;
564
565 if (cd->device->sector_size > 2048)
566 sr_set_blocklength(cd, 2048);
567
568}
569
570static int sr_probe(struct device *dev)
571{
572 struct scsi_device *sdev = to_scsi_device(dev);
573 struct gendisk *disk;
574 struct scsi_cd *cd;
575 int minor, error;
576
577 error = -ENODEV;
578 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
579 goto fail;
580
581 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500582 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!cd)
584 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 kref_init(&cd->kref);
587
588 disk = alloc_disk(1);
589 if (!disk)
590 goto fail_free;
591
592 spin_lock(&sr_index_lock);
593 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
594 if (minor == SR_DISKS) {
595 spin_unlock(&sr_index_lock);
596 error = -EBUSY;
597 goto fail_put;
598 }
599 __set_bit(minor, sr_index_bits);
600 spin_unlock(&sr_index_lock);
601
602 disk->major = SCSI_CDROM_MAJOR;
603 disk->first_minor = minor;
604 sprintf(disk->disk_name, "sr%d", minor);
605 disk->fops = &sr_bdops;
606 disk->flags = GENHD_FL_CD;
607
Jens Axboe242f9dc2008-09-14 05:55:09 -0700608 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 cd->device = sdev;
611 cd->disk = disk;
612 cd->driver = &sr_template;
613 cd->disk = disk;
614 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 cd->device->changed = 1; /* force recheck CD type */
Kay Sieversc02e6002008-03-19 13:09:56 +0100616 cd->previous_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 cd->use = 1;
618 cd->readcd_known = 0;
619 cd->readcd_cdda = 0;
620
621 cd->cdi.ops = &sr_dops;
622 cd->cdi.handle = cd;
623 cd->cdi.mask = 0;
624 cd->cdi.capacity = 1;
625 sprintf(cd->cdi.name, "sr%d", minor);
626
627 sdev->sector_size = 2048; /* A guess, just in case */
628
629 /* FIXME: need to handle a get_capabilities failure properly ?? */
630 get_capabilities(cd);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500631 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 sr_vendor_init(cd);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 disk->driverfs_dev = &sdev->sdev_gendev;
635 set_capacity(disk, cd->capacity);
636 disk->private_data = &cd->driver;
637 disk->queue = sdev->request_queue;
638 cd->cdi.disk = disk;
639
640 if (register_cdrom(&cd->cdi))
641 goto fail_put;
642
643 dev_set_drvdata(dev, cd);
644 disk->flags |= GENHD_FL_REMOVABLE;
645 add_disk(disk);
646
James Bottomley9ccfc752005-10-02 11:45:08 -0500647 sdev_printk(KERN_DEBUG, sdev,
648 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650
651fail_put:
652 put_disk(disk);
653fail_free:
654 kfree(cd);
655fail:
656 return error;
657}
658
659
660static void get_sectorsize(struct scsi_cd *cd)
661{
662 unsigned char cmd[10];
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200663 unsigned char buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int the_result, retries = 3;
665 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200666 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 do {
669 cmd[0] = READ_CAPACITY;
670 memset((void *) &cmd[1], 0, 9);
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200671 memset(buffer, 0, sizeof(buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500674 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200675 buffer, sizeof(buffer), NULL,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900676 SR_TIMEOUT, MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 retries--;
679
680 } while (the_result && retries);
681
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (the_result) {
684 cd->capacity = 0x1fffff;
685 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 } else {
687#if 0
688 if (cdrom_get_last_written(&cd->cdi,
689 &cd->capacity))
690#endif
691 cd->capacity = 1 + ((buffer[0] << 24) |
692 (buffer[1] << 16) |
693 (buffer[2] << 8) |
694 buffer[3]);
695 sector_size = (buffer[4] << 24) |
696 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
697 switch (sector_size) {
698 /*
699 * HP 4020i CD-Recorder reports 2340 byte sectors
700 * Philips CD-Writers report 2352 byte sectors
701 *
702 * Use 2k sectors for them..
703 */
704 case 0:
705 case 2340:
706 case 2352:
707 sector_size = 2048;
708 /* fall through */
709 case 2048:
710 cd->capacity *= 4;
711 /* fall through */
712 case 512:
713 break;
714 default:
715 printk("%s: unsupported sector size %d.\n",
716 cd->cdi.name, sector_size);
717 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 cd->device->sector_size = sector_size;
721
722 /*
723 * Add this so that we have the ability to correctly gauge
724 * what the device is capable of.
725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 set_capacity(cd->disk, cd->capacity);
727 }
728
729 queue = cd->device->request_queue;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400730 blk_queue_logical_block_size(queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200732 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static void get_capabilities(struct scsi_cd *cd)
736{
737 unsigned char *buffer;
738 struct scsi_mode_data data;
James Bottomley820732b2005-06-12 22:21:29 -0500739 struct scsi_sense_hdr sshdr;
James Bottomley38582a62008-02-06 13:01:58 -0600740 int rc, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
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
James Bottomley38582a62008-02-06 13:01:58 -0600762 /* eat unit attentions */
763 sr_test_unit_ready(cd->device, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* ask for mode page 0x2a */
766 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500767 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (!scsi_status_is_good(rc)) {
770 /* failed, drive doesn't have capabilities mode page */
771 cd->cdi.speed = 1;
772 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500773 CDC_DVD | CDC_DVD_RAM |
774 CDC_SELECT_DISC | CDC_SELECT_SPEED |
775 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 kfree(buffer);
777 printk("%s: scsi-1 drive\n", cd->cdi.name);
778 return;
779 }
780
781 n = data.header_length + data.block_descriptor_length;
782 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
783 cd->readcd_known = 1;
784 cd->readcd_cdda = buffer[n + 5] & 0x01;
785 /* print some capability bits */
786 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
787 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
788 cd->cdi.speed,
789 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
790 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
791 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
792 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
793 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
794 loadmech[buffer[n + 6] >> 5]);
795 if ((buffer[n + 6] >> 5) == 0)
796 /* caddy drives can't close tray... */
797 cd->cdi.mask |= CDC_CLOSE_TRAY;
798 if ((buffer[n + 2] & 0x8) == 0)
799 /* not a DVD drive */
800 cd->cdi.mask |= CDC_DVD;
801 if ((buffer[n + 3] & 0x20) == 0)
802 /* can't write DVD-RAM media */
803 cd->cdi.mask |= CDC_DVD_RAM;
804 if ((buffer[n + 3] & 0x10) == 0)
805 /* can't write DVD-R media */
806 cd->cdi.mask |= CDC_DVD_R;
807 if ((buffer[n + 3] & 0x2) == 0)
808 /* can't write CD-RW media */
809 cd->cdi.mask |= CDC_CD_RW;
810 if ((buffer[n + 3] & 0x1) == 0)
811 /* can't write CD-R media */
812 cd->cdi.mask |= CDC_CD_R;
813 if ((buffer[n + 6] & 0x8) == 0)
814 /* can't eject */
815 cd->cdi.mask |= CDC_OPEN_TRAY;
816
817 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
818 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
819 cd->cdi.capacity =
820 cdrom_number_of_slots(&cd->cdi);
821 if (cd->cdi.capacity <= 1)
822 /* not a changer */
823 cd->cdi.mask |= CDC_SELECT_DISC;
824 /*else I don't think it can close its tray
825 cd->cdi.mask |= CDC_CLOSE_TRAY; */
826
827 /*
828 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
829 */
830 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
831 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
832 cd->device->writeable = 1;
833 }
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 kfree(buffer);
836}
837
838/*
839 * sr_packet() is the entry point for the generic commands generated
840 * by the Uniform CD-ROM layer.
841 */
842static int sr_packet(struct cdrom_device_info *cdi,
843 struct packet_command *cgc)
844{
845 if (cgc->timeout <= 0)
846 cgc->timeout = IOCTL_TIMEOUT;
847
848 sr_do_ioctl(cdi->handle, cgc);
849
850 return cgc->stat;
851}
852
853/**
854 * sr_kref_release - Called to free the scsi_cd structure
855 * @kref: pointer to embedded kref
856 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100857 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * called on last put, you should always use the scsi_cd_get()
859 * scsi_cd_put() helpers which manipulate the semaphore directly
860 * and never do a direct kref_put().
861 **/
862static void sr_kref_release(struct kref *kref)
863{
864 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
865 struct gendisk *disk = cd->disk;
866
867 spin_lock(&sr_index_lock);
Tejun Heof331c022008-09-03 09:01:48 +0200868 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 spin_unlock(&sr_index_lock);
870
871 unregister_cdrom(&cd->cdi);
872
873 disk->private_data = NULL;
874
875 put_disk(disk);
876
877 kfree(cd);
878}
879
880static int sr_remove(struct device *dev)
881{
882 struct scsi_cd *cd = dev_get_drvdata(dev);
883
Hannes Reineckeb3912772009-06-18 09:57:18 +0200884 blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 del_gendisk(cd->disk);
886
Arjan van de Ven0b950672006-01-11 13:16:10 +0100887 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100889 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 return 0;
892}
893
894static int __init init_sr(void)
895{
896 int rc;
897
898 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
899 if (rc)
900 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +0900901 rc = scsi_register_driver(&sr_template.gendrv);
902 if (rc)
903 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
904
905 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908static void __exit exit_sr(void)
909{
910 scsi_unregister_driver(&sr_template.gendrv);
911 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
912}
913
914module_init(init_sr);
915module_exit(exit_sr);
916MODULE_LICENSE("GPL");