Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / sr.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
38#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>
0b950672 46#include <linux/mutex.h>
5a0e3ad6 47#include <linux/slab.h>
6c7f1e2f 48#include <linux/pm_runtime.h>
1da177e4
LT
49#include <asm/uaccess.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_dbg.h>
53#include <scsi/scsi_device.h>
54#include <scsi/scsi_driver.h>
820732b5 55#include <scsi/scsi_cmnd.h>
1da177e4
LT
56#include <scsi/scsi_eh.h>
57#include <scsi/scsi_host.h>
58#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
1da177e4
LT
59
60#include "scsi_logging.h"
61#include "sr.h"
62
63
f018fa55
RH
64MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
65MODULE_LICENSE("GPL");
66MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
d7b8bcb0
MT
67MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
68MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
f018fa55 69
1da177e4
LT
70#define SR_DISKS 256
71
1da177e4
LT
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| \
6a2900b6 75 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
1da177e4
LT
76 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
2a48fc0a 79static DEFINE_MUTEX(sr_mutex);
1da177e4
LT
80static int sr_probe(struct device *);
81static int sr_remove(struct device *);
7b3d9545 82static int sr_done(struct scsi_cmnd *);
6c7f1e2f
AL
83static int sr_runtime_suspend(struct device *dev);
84
85static struct dev_pm_ops sr_pm_ops = {
86 .runtime_suspend = sr_runtime_suspend,
87};
1da177e4
LT
88
89static struct scsi_driver sr_template = {
90 .owner = THIS_MODULE,
91 .gendrv = {
92 .name = "sr",
93 .probe = sr_probe,
94 .remove = sr_remove,
6c7f1e2f 95 .pm = &sr_pm_ops,
1da177e4 96 },
7b3d9545 97 .done = sr_done,
1da177e4
LT
98};
99
100static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
101static DEFINE_SPINLOCK(sr_index_lock);
102
103/* This semaphore is used to mediate the 0->1 reference get in the
104 * face of object destruction (i.e. we can't allow a get on an
105 * object after last put) */
0b950672 106static DEFINE_MUTEX(sr_ref_mutex);
1da177e4
LT
107
108static int sr_open(struct cdrom_device_info *, int);
109static void sr_release(struct cdrom_device_info *);
110
111static void get_sectorsize(struct scsi_cd *);
112static void get_capabilities(struct scsi_cd *);
113
93aae17a
TH
114static unsigned int sr_check_events(struct cdrom_device_info *cdi,
115 unsigned int clearing, int slot);
1da177e4
LT
116static int sr_packet(struct cdrom_device_info *, struct packet_command *);
117
118static struct cdrom_device_ops sr_dops = {
119 .open = sr_open,
120 .release = sr_release,
121 .drive_status = sr_drive_status,
93aae17a 122 .check_events = sr_check_events,
1da177e4
LT
123 .tray_move = sr_tray_move,
124 .lock_door = sr_lock_door,
125 .select_speed = sr_select_speed,
126 .get_last_session = sr_get_last_session,
127 .get_mcn = sr_get_mcn,
128 .reset = sr_reset,
129 .audio_ioctl = sr_audio_ioctl,
1da177e4
LT
130 .capability = SR_CAPABILITIES,
131 .generic_packet = sr_packet,
132};
133
134static void sr_kref_release(struct kref *kref);
135
136static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
137{
138 return container_of(disk->private_data, struct scsi_cd, driver);
139}
140
6c7f1e2f
AL
141static int sr_runtime_suspend(struct device *dev)
142{
143 struct scsi_cd *cd = dev_get_drvdata(dev);
144
3d1df278
AS
145 if (!cd) /* E.g.: runtime suspend following sr_remove() */
146 return 0;
147
6c7f1e2f
AL
148 if (cd->media_present)
149 return -EBUSY;
150 else
151 return 0;
152}
153
1da177e4
LT
154/*
155 * The get and put routines for the struct scsi_cd. Note this entity
156 * has a scsi_device pointer and owns a reference to this.
157 */
158static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
159{
160 struct scsi_cd *cd = NULL;
161
0b950672 162 mutex_lock(&sr_ref_mutex);
1da177e4
LT
163 if (disk->private_data == NULL)
164 goto out;
165 cd = scsi_cd(disk);
166 kref_get(&cd->kref);
167 if (scsi_device_get(cd->device))
168 goto out_put;
6c7f1e2f
AL
169 if (!scsi_autopm_get_device(cd->device))
170 goto out;
1da177e4
LT
171
172 out_put:
173 kref_put(&cd->kref, sr_kref_release);
174 cd = NULL;
175 out:
0b950672 176 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
177 return cd;
178}
179
858119e1 180static void scsi_cd_put(struct scsi_cd *cd)
1da177e4
LT
181{
182 struct scsi_device *sdev = cd->device;
183
0b950672 184 mutex_lock(&sr_ref_mutex);
1da177e4 185 kref_put(&cd->kref, sr_kref_release);
6c7f1e2f 186 scsi_autopm_put_device(sdev);
1da177e4 187 scsi_device_put(sdev);
0b950672 188 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
189}
190
93aae17a
TH
191static unsigned int sr_get_events(struct scsi_device *sdev)
192{
193 u8 buf[8];
194 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
195 1, /* polled */
196 0, 0, /* reserved */
197 1 << 4, /* notification class: media */
198 0, 0, /* reserved */
199 0, sizeof(buf), /* allocation length */
200 0, /* control */
201 };
202 struct event_header *eh = (void *)buf;
203 struct media_event_desc *med = (void *)(buf + 4);
204 struct scsi_sense_hdr sshdr;
205 int result;
206
207 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
208 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
209 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
210 return DISK_EVENT_MEDIA_CHANGE;
211
212 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
213 return 0;
214
215 if (eh->nea || eh->notification_class != 0x4)
216 return 0;
217
218 if (med->media_event_code == 1)
219 return DISK_EVENT_EJECT_REQUEST;
220 else if (med->media_event_code == 2)
221 return DISK_EVENT_MEDIA_CHANGE;
222 return 0;
223}
224
1da177e4 225/*
93aae17a
TH
226 * This function checks to see if the media has been changed or eject
227 * button has been pressed. It is possible that we have already
228 * sensed a change, or the drive may have sensed one and not yet
229 * reported it. The past events are accumulated in sdev->changed and
230 * returned together with the current state.
1da177e4 231 */
93aae17a
TH
232static unsigned int sr_check_events(struct cdrom_device_info *cdi,
233 unsigned int clearing, int slot)
1da177e4
LT
234{
235 struct scsi_cd *cd = cdi->handle;
93aae17a
TH
236 bool last_present;
237 struct scsi_sense_hdr sshdr;
238 unsigned int events;
239 int ret;
1da177e4 240
93aae17a
TH
241 /* no changer support */
242 if (CDSL_CURRENT != slot)
243 return 0;
244
245 events = sr_get_events(cd->device);
79b9677d
KS
246 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
247
248 /*
249 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
250 * for several times in a row. We rely on TUR only for this likely
251 * broken device, to prevent generating incorrect media changed
252 * events for every open().
253 */
254 if (cd->ignore_get_event) {
255 events &= ~DISK_EVENT_MEDIA_CHANGE;
256 goto do_tur;
257 }
258
93aae17a
TH
259 /*
260 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
261 * is being cleared. Note that there are devices which hang
262 * if asked to execute TUR repeatedly.
263 */
79b9677d
KS
264 if (cd->device->changed) {
265 events |= DISK_EVENT_MEDIA_CHANGE;
266 cd->device->changed = 0;
267 cd->tur_changed = true;
268 }
93aae17a 269
79b9677d
KS
270 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
271 return events;
272do_tur:
93aae17a
TH
273 /* let's see whether the media is there with TUR */
274 last_present = cd->media_present;
275 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
1da177e4 276
638428ec
TH
277 /*
278 * Media is considered to be present if TUR succeeds or fails with
279 * sense data indicating something other than media-not-present
280 * (ASC 0x3a).
281 */
93aae17a
TH
282 cd->media_present = scsi_status_is_good(ret) ||
283 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
1da177e4 284
93aae17a 285 if (last_present != cd->media_present)
79b9677d
KS
286 cd->device->changed = 1;
287
93aae17a
TH
288 if (cd->device->changed) {
289 events |= DISK_EVENT_MEDIA_CHANGE;
290 cd->device->changed = 0;
79b9677d
KS
291 cd->tur_changed = true;
292 }
293
294 if (cd->ignore_get_event)
295 return events;
296
297 /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
298 if (!cd->tur_changed) {
299 if (cd->get_event_changed) {
300 if (cd->tur_mismatch++ > 8) {
301 sdev_printk(KERN_WARNING, cd->device,
302 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
303 cd->ignore_get_event = true;
304 }
305 } else {
306 cd->tur_mismatch = 0;
307 }
1da177e4 308 }
79b9677d
KS
309 cd->tur_changed = false;
310 cd->get_event_changed = false;
285e9670 311
93aae17a 312 return events;
1da177e4 313}
93aae17a 314
1da177e4 315/*
7b3d9545 316 * sr_done is the interrupt routine for the device driver.
1da177e4 317 *
7b3d9545 318 * It will be notified on the end of a SCSI read / write, and will take one
1da177e4
LT
319 * of several actions based on success or failure.
320 */
7b3d9545 321static int sr_done(struct scsi_cmnd *SCpnt)
1da177e4
LT
322{
323 int result = SCpnt->result;
30b0c37b 324 int this_count = scsi_bufflen(SCpnt);
1da177e4
LT
325 int good_bytes = (result == 0 ? this_count : 0);
326 int block_sectors = 0;
327 long error_sector;
328 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
329
330#ifdef DEBUG
331 printk("sr.c done: %x\n", result);
332#endif
333
334 /*
335 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
336 * success. Since this is a relatively rare error condition, no
337 * care is taken to avoid unnecessary additional work such as
338 * memcpy's that could be avoided.
339 */
340 if (driver_byte(result) != 0 && /* An error occurred */
341 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
342 switch (SCpnt->sense_buffer[2]) {
343 case MEDIUM_ERROR:
344 case VOLUME_OVERFLOW:
345 case ILLEGAL_REQUEST:
346 if (!(SCpnt->sense_buffer[0] & 0x90))
347 break;
1da177e4
LT
348 error_sector = (SCpnt->sense_buffer[3] << 24) |
349 (SCpnt->sense_buffer[4] << 16) |
350 (SCpnt->sense_buffer[5] << 8) |
351 SCpnt->sense_buffer[6];
352 if (SCpnt->request->bio != NULL)
353 block_sectors =
354 bio_sectors(SCpnt->request->bio);
355 if (block_sectors < 4)
356 block_sectors = 4;
357 if (cd->device->sector_size == 2048)
358 error_sector <<= 2;
359 error_sector &= ~(block_sectors - 1);
83096ebf
TH
360 good_bytes = (error_sector -
361 blk_rq_pos(SCpnt->request)) << 9;
1da177e4
LT
362 if (good_bytes < 0 || good_bytes >= this_count)
363 good_bytes = 0;
364 /*
365 * The SCSI specification allows for the value
366 * returned by READ CAPACITY to be up to 75 2K
367 * sectors past the last readable block.
368 * Therefore, if we hit a medium error within the
369 * last 75 2K sectors, we decrease the saved size
370 * value.
371 */
372 if (error_sector < get_capacity(cd->disk) &&
373 cd->capacity - error_sector < 4 * 75)
374 set_capacity(cd->disk, error_sector);
375 break;
376
377 case RECOVERED_ERROR:
1da177e4
LT
378 good_bytes = this_count;
379 break;
380
381 default:
382 break;
383 }
384 }
385
7b3d9545 386 return good_bytes;
1da177e4
LT
387}
388
7f9a6bc4 389static int sr_prep_fn(struct request_queue *q, struct request *rq)
1da177e4 390{
242f9dcb 391 int block = 0, this_count, s_size;
7f9a6bc4
JB
392 struct scsi_cd *cd;
393 struct scsi_cmnd *SCpnt;
394 struct scsi_device *sdp = q->queuedata;
395 int ret;
396
397 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
398 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
399 goto out;
400 } else if (rq->cmd_type != REQ_TYPE_FS) {
401 ret = BLKPREP_KILL;
402 goto out;
403 }
404 ret = scsi_setup_fs_cmnd(sdp, rq);
405 if (ret != BLKPREP_OK)
406 goto out;
407 SCpnt = rq->special;
408 cd = scsi_cd(rq->rq_disk);
409
410 /* from here on until we're complete, any goto out
411 * is used for a killable error condition */
412 ret = BLKPREP_KILL;
1da177e4
LT
413
414 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
415 cd->disk->disk_name, block));
416
417 if (!cd->device || !scsi_device_online(cd->device)) {
83096ebf
TH
418 SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
419 blk_rq_sectors(rq)));
1da177e4 420 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
7f9a6bc4 421 goto out;
1da177e4
LT
422 }
423
424 if (cd->device->changed) {
425 /*
426 * quietly refuse to do anything to a changed disc until the
427 * changed bit has been reset
428 */
7f9a6bc4 429 goto out;
1da177e4
LT
430 }
431
1da177e4
LT
432 /*
433 * we do lazy blocksize switching (when reading XA sectors,
434 * see CDROMREADMODE2 ioctl)
435 */
436 s_size = cd->device->sector_size;
437 if (s_size > 2048) {
438 if (!in_interrupt())
439 sr_set_blocklength(cd, 2048);
440 else
441 printk("sr: can't switch blocksize: in interrupt\n");
442 }
443
444 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
3bf743e7 445 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
7f9a6bc4 446 goto out;
1da177e4
LT
447 }
448
7f9a6bc4 449 if (rq_data_dir(rq) == WRITE) {
1da177e4 450 if (!cd->device->writeable)
7f9a6bc4 451 goto out;
1da177e4
LT
452 SCpnt->cmnd[0] = WRITE_10;
453 SCpnt->sc_data_direction = DMA_TO_DEVICE;
454 cd->cdi.media_written = 1;
7f9a6bc4 455 } else if (rq_data_dir(rq) == READ) {
1da177e4
LT
456 SCpnt->cmnd[0] = READ_10;
457 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
458 } else {
7f9a6bc4
JB
459 blk_dump_rq_flags(rq, "Unknown sr command");
460 goto out;
1da177e4
LT
461 }
462
463 {
30b0c37b
BH
464 struct scatterlist *sg;
465 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
1da177e4 466
30b0c37b
BH
467 scsi_for_each_sg(SCpnt, sg, sg_count, i)
468 size += sg->length;
469
470 if (size != scsi_bufflen(SCpnt)) {
3bf743e7
JG
471 scmd_printk(KERN_ERR, SCpnt,
472 "mismatch count %d, bytes %d\n",
30b0c37b
BH
473 size, scsi_bufflen(SCpnt));
474 if (scsi_bufflen(SCpnt) > size)
475 SCpnt->sdb.length = size;
1da177e4
LT
476 }
477 }
478
479 /*
480 * request doesn't start on hw block boundary, add scatter pads
481 */
83096ebf 482 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
30b0c37b 483 (scsi_bufflen(SCpnt) % s_size)) {
3bf743e7 484 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
7f9a6bc4 485 goto out;
1da177e4
LT
486 }
487
30b0c37b 488 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
1da177e4
LT
489
490
83096ebf 491 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
1da177e4 492 cd->cdi.name,
7f9a6bc4 493 (rq_data_dir(rq) == WRITE) ?
1da177e4 494 "writing" : "reading",
83096ebf 495 this_count, blk_rq_sectors(rq)));
1da177e4
LT
496
497 SCpnt->cmnd[1] = 0;
83096ebf 498 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
1da177e4
LT
499
500 if (this_count > 0xffff) {
501 this_count = 0xffff;
30b0c37b 502 SCpnt->sdb.length = this_count * s_size;
1da177e4
LT
503 }
504
505 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
506 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
507 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
508 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
509 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
510 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
511 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
512
513 /*
514 * We shouldn't disconnect in the middle of a sector, so with a dumb
515 * host adapter, it's safe to assume that we can at least transfer
516 * this many bytes between each connect / disconnect.
517 */
518 SCpnt->transfersize = cd->device->sector_size;
519 SCpnt->underflow = this_count << 9;
1da177e4 520 SCpnt->allowed = MAX_RETRIES;
1da177e4 521
1da177e4
LT
522 /*
523 * This indicates that the command is ready from our end to be
524 * queued.
525 */
7f9a6bc4
JB
526 ret = BLKPREP_OK;
527 out:
528 return scsi_prep_return(q, rq, ret);
1da177e4
LT
529}
530
40cc51be 531static int sr_block_open(struct block_device *bdev, fmode_t mode)
1da177e4 532{
6e9624b8 533 struct scsi_cd *cd;
40cc51be 534 int ret = -ENXIO;
1da177e4 535
2a48fc0a 536 mutex_lock(&sr_mutex);
6e9624b8 537 cd = scsi_cd_get(bdev->bd_disk);
40cc51be
AV
538 if (cd) {
539 ret = cdrom_open(&cd->cdi, bdev, mode);
540 if (ret)
541 scsi_cd_put(cd);
542 }
2a48fc0a 543 mutex_unlock(&sr_mutex);
1da177e4
LT
544 return ret;
545}
546
db2a144b 547static void sr_block_release(struct gendisk *disk, fmode_t mode)
1da177e4 548{
40cc51be 549 struct scsi_cd *cd = scsi_cd(disk);
2a48fc0a 550 mutex_lock(&sr_mutex);
40cc51be 551 cdrom_release(&cd->cdi, mode);
1da177e4 552 scsi_cd_put(cd);
2a48fc0a 553 mutex_unlock(&sr_mutex);
1da177e4
LT
554}
555
40cc51be 556static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
1da177e4
LT
557 unsigned long arg)
558{
40cc51be 559 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
1da177e4 560 struct scsi_device *sdev = cd->device;
6a2900b6
CH
561 void __user *argp = (void __user *)arg;
562 int ret;
1da177e4 563
6c7f1e2f
AL
564 scsi_autopm_get_device(cd->device);
565
2a48fc0a 566 mutex_lock(&sr_mutex);
8a6cfeb6 567
6a2900b6
CH
568 /*
569 * Send SCSI addressing ioctls directly to mid level, send other
570 * ioctls to cdrom/block level.
571 */
572 switch (cmd) {
573 case SCSI_IOCTL_GET_IDLUN:
574 case SCSI_IOCTL_GET_BUS_NUMBER:
8a6cfeb6
AB
575 ret = scsi_ioctl(sdev, cmd, argp);
576 goto out;
1da177e4 577 }
6a2900b6 578
40cc51be 579 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
6397256b 580 if (ret != -ENOSYS)
8a6cfeb6 581 goto out;
6a2900b6
CH
582
583 /*
584 * ENODEV means that we didn't recognise the ioctl, or that we
585 * cannot execute it in the current device state. In either
586 * case fall through to scsi_ioctl, which will return ENDOEV again
587 * if it doesn't recognise the ioctl
588 */
83ff6fe8 589 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
fd4ce1ac 590 (mode & FMODE_NDELAY) != 0);
6a2900b6 591 if (ret != -ENODEV)
8a6cfeb6
AB
592 goto out;
593 ret = scsi_ioctl(sdev, cmd, argp);
594
595out:
2a48fc0a 596 mutex_unlock(&sr_mutex);
6c7f1e2f 597 scsi_autopm_put_device(cd->device);
8a6cfeb6 598 return ret;
1da177e4
LT
599}
600
93aae17a
TH
601static unsigned int sr_block_check_events(struct gendisk *disk,
602 unsigned int clearing)
1da177e4
LT
603{
604 struct scsi_cd *cd = scsi_cd(disk);
6c7f1e2f
AL
605 unsigned int ret;
606
6f4c827e
AL
607 if (atomic_read(&cd->device->disk_events_disable_depth) == 0) {
608 scsi_autopm_get_device(cd->device);
609 ret = cdrom_check_events(&cd->cdi, clearing);
610 scsi_autopm_put_device(cd->device);
611 } else {
612 ret = 0;
613 }
6c7f1e2f
AL
614
615 return ret;
93aae17a
TH
616}
617
618static int sr_block_revalidate_disk(struct gendisk *disk)
619{
620 struct scsi_cd *cd = scsi_cd(disk);
621 struct scsi_sense_hdr sshdr;
622
6c7f1e2f
AL
623 scsi_autopm_get_device(cd->device);
624
93aae17a
TH
625 /* if the unit is not ready, nothing more to do */
626 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
6c7f1e2f 627 goto out;
93aae17a
TH
628
629 sr_cd_check(&cd->cdi);
630 get_sectorsize(cd);
6c7f1e2f
AL
631out:
632 scsi_autopm_put_device(cd->device);
93aae17a 633 return 0;
1da177e4
LT
634}
635
83d5cde4 636static const struct block_device_operations sr_bdops =
1da177e4
LT
637{
638 .owner = THIS_MODULE,
40cc51be
AV
639 .open = sr_block_open,
640 .release = sr_block_release,
8a6cfeb6 641 .ioctl = sr_block_ioctl,
93aae17a
TH
642 .check_events = sr_block_check_events,
643 .revalidate_disk = sr_block_revalidate_disk,
1da177e4
LT
644 /*
645 * No compat_ioctl for now because sr_block_ioctl never
25985edc 646 * seems to pass arbitrary ioctls down to host drivers.
1da177e4
LT
647 */
648};
649
650static int sr_open(struct cdrom_device_info *cdi, int purpose)
651{
652 struct scsi_cd *cd = cdi->handle;
653 struct scsi_device *sdev = cd->device;
654 int retval;
655
656 /*
657 * If the device is in error recovery, wait until it is done.
658 * If the device is offline, then disallow any access to it.
659 */
660 retval = -ENXIO;
661 if (!scsi_block_when_processing_errors(sdev))
662 goto error_out;
663
1da177e4
LT
664 return 0;
665
666error_out:
667 return retval;
668}
669
670static void sr_release(struct cdrom_device_info *cdi)
671{
672 struct scsi_cd *cd = cdi->handle;
673
674 if (cd->device->sector_size > 2048)
675 sr_set_blocklength(cd, 2048);
676
677}
678
679static int sr_probe(struct device *dev)
680{
681 struct scsi_device *sdev = to_scsi_device(dev);
682 struct gendisk *disk;
683 struct scsi_cd *cd;
684 int minor, error;
685
686 error = -ENODEV;
687 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
688 goto fail;
689
690 error = -ENOMEM;
24669f75 691 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
1da177e4
LT
692 if (!cd)
693 goto fail;
1da177e4
LT
694
695 kref_init(&cd->kref);
696
697 disk = alloc_disk(1);
698 if (!disk)
699 goto fail_free;
700
701 spin_lock(&sr_index_lock);
702 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
703 if (minor == SR_DISKS) {
704 spin_unlock(&sr_index_lock);
705 error = -EBUSY;
706 goto fail_put;
707 }
708 __set_bit(minor, sr_index_bits);
709 spin_unlock(&sr_index_lock);
710
711 disk->major = SCSI_CDROM_MAJOR;
712 disk->first_minor = minor;
713 sprintf(disk->disk_name, "sr%d", minor);
714 disk->fops = &sr_bdops;
d4dc210f 715 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
93aae17a 716 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
1da177e4 717
242f9dcb
JA
718 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
719
1da177e4
LT
720 cd->device = sdev;
721 cd->disk = disk;
722 cd->driver = &sr_template;
723 cd->disk = disk;
724 cd->capacity = 0x1fffff;
1da177e4 725 cd->device->changed = 1; /* force recheck CD type */
93aae17a 726 cd->media_present = 1;
1da177e4
LT
727 cd->use = 1;
728 cd->readcd_known = 0;
729 cd->readcd_cdda = 0;
730
731 cd->cdi.ops = &sr_dops;
732 cd->cdi.handle = cd;
733 cd->cdi.mask = 0;
734 cd->cdi.capacity = 1;
735 sprintf(cd->cdi.name, "sr%d", minor);
736
737 sdev->sector_size = 2048; /* A guess, just in case */
738
739 /* FIXME: need to handle a get_capabilities failure properly ?? */
740 get_capabilities(cd);
7f9a6bc4 741 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
1da177e4
LT
742 sr_vendor_init(cd);
743
1da177e4
LT
744 disk->driverfs_dev = &sdev->sdev_gendev;
745 set_capacity(disk, cd->capacity);
746 disk->private_data = &cd->driver;
747 disk->queue = sdev->request_queue;
748 cd->cdi.disk = disk;
749
750 if (register_cdrom(&cd->cdi))
751 goto fail_put;
752
753 dev_set_drvdata(dev, cd);
754 disk->flags |= GENHD_FL_REMOVABLE;
755 add_disk(disk);
756
9ccfc756
JB
757 sdev_printk(KERN_DEBUG, sdev,
758 "Attached scsi CD-ROM %s\n", cd->cdi.name);
6c7f1e2f
AL
759 scsi_autopm_put_device(cd->device);
760
1da177e4
LT
761 return 0;
762
763fail_put:
764 put_disk(disk);
765fail_free:
766 kfree(cd);
767fail:
768 return error;
769}
770
771
772static void get_sectorsize(struct scsi_cd *cd)
773{
774 unsigned char cmd[10];
62858dac 775 unsigned char buffer[8];
1da177e4
LT
776 int the_result, retries = 3;
777 int sector_size;
165125e1 778 struct request_queue *queue;
1da177e4 779
1da177e4
LT
780 do {
781 cmd[0] = READ_CAPACITY;
782 memset((void *) &cmd[1], 0, 9);
62858dac 783 memset(buffer, 0, sizeof(buffer));
1da177e4
LT
784
785 /* Do the command and wait.. */
820732b5 786 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
62858dac 787 buffer, sizeof(buffer), NULL,
f4f4e47e 788 SR_TIMEOUT, MAX_RETRIES, NULL);
1da177e4 789
1da177e4
LT
790 retries--;
791
792 } while (the_result && retries);
793
794
1da177e4
LT
795 if (the_result) {
796 cd->capacity = 0x1fffff;
797 sector_size = 2048; /* A guess, just in case */
1da177e4 798 } else {
5915136d
TH
799 long last_written;
800
801 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
802 (buffer[2] << 8) | buffer[3]);
803 /*
804 * READ_CAPACITY doesn't return the correct size on
805 * certain UDF media. If last_written is larger, use
806 * it instead.
807 *
808 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
809 */
810 if (!cdrom_get_last_written(&cd->cdi, &last_written))
811 cd->capacity = max_t(long, cd->capacity, last_written);
812
1da177e4
LT
813 sector_size = (buffer[4] << 24) |
814 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
815 switch (sector_size) {
816 /*
817 * HP 4020i CD-Recorder reports 2340 byte sectors
818 * Philips CD-Writers report 2352 byte sectors
819 *
820 * Use 2k sectors for them..
821 */
822 case 0:
823 case 2340:
824 case 2352:
825 sector_size = 2048;
826 /* fall through */
827 case 2048:
828 cd->capacity *= 4;
829 /* fall through */
830 case 512:
831 break;
832 default:
833 printk("%s: unsupported sector size %d.\n",
834 cd->cdi.name, sector_size);
835 cd->capacity = 0;
1da177e4
LT
836 }
837
838 cd->device->sector_size = sector_size;
839
840 /*
841 * Add this so that we have the ability to correctly gauge
842 * what the device is capable of.
843 */
1da177e4
LT
844 set_capacity(cd->disk, cd->capacity);
845 }
846
847 queue = cd->device->request_queue;
e1defc4f 848 blk_queue_logical_block_size(queue, sector_size);
1da177e4 849
62858dac 850 return;
1da177e4
LT
851}
852
853static void get_capabilities(struct scsi_cd *cd)
854{
855 unsigned char *buffer;
856 struct scsi_mode_data data;
820732b5 857 struct scsi_sense_hdr sshdr;
d427ab53 858 unsigned int ms_len = 128;
38582a62 859 int rc, n;
1da177e4 860
0ad78200 861 static const char *loadmech[] =
1da177e4
LT
862 {
863 "caddy",
864 "tray",
865 "pop-up",
866 "",
867 "changer",
868 "cartridge changer",
869 "",
870 ""
871 };
872
1da177e4
LT
873
874 /* allocate transfer buffer */
875 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
876 if (!buffer) {
877 printk(KERN_ERR "sr: out of memory.\n");
1da177e4
LT
878 return;
879 }
880
38582a62 881 /* eat unit attentions */
9f8a2c23 882 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
1da177e4
LT
883
884 /* ask for mode page 0x2a */
d427ab53 885 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
1cf72699 886 SR_TIMEOUT, 3, &data, NULL);
1da177e4 887
d427ab53
MP
888 if (!scsi_status_is_good(rc) || data.length > ms_len ||
889 data.header_length + data.block_descriptor_length > data.length) {
1da177e4
LT
890 /* failed, drive doesn't have capabilities mode page */
891 cd->cdi.speed = 1;
892 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
bcc1e382
CE
893 CDC_DVD | CDC_DVD_RAM |
894 CDC_SELECT_DISC | CDC_SELECT_SPEED |
895 CDC_MRW | CDC_MRW_W | CDC_RAM);
1da177e4
LT
896 kfree(buffer);
897 printk("%s: scsi-1 drive\n", cd->cdi.name);
898 return;
899 }
900
901 n = data.header_length + data.block_descriptor_length;
902 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
903 cd->readcd_known = 1;
904 cd->readcd_cdda = buffer[n + 5] & 0x01;
905 /* print some capability bits */
906 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
907 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
908 cd->cdi.speed,
909 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
910 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
911 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
912 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
913 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
914 loadmech[buffer[n + 6] >> 5]);
915 if ((buffer[n + 6] >> 5) == 0)
916 /* caddy drives can't close tray... */
917 cd->cdi.mask |= CDC_CLOSE_TRAY;
918 if ((buffer[n + 2] & 0x8) == 0)
919 /* not a DVD drive */
920 cd->cdi.mask |= CDC_DVD;
921 if ((buffer[n + 3] & 0x20) == 0)
922 /* can't write DVD-RAM media */
923 cd->cdi.mask |= CDC_DVD_RAM;
924 if ((buffer[n + 3] & 0x10) == 0)
925 /* can't write DVD-R media */
926 cd->cdi.mask |= CDC_DVD_R;
927 if ((buffer[n + 3] & 0x2) == 0)
928 /* can't write CD-RW media */
929 cd->cdi.mask |= CDC_CD_RW;
930 if ((buffer[n + 3] & 0x1) == 0)
931 /* can't write CD-R media */
932 cd->cdi.mask |= CDC_CD_R;
933 if ((buffer[n + 6] & 0x8) == 0)
934 /* can't eject */
935 cd->cdi.mask |= CDC_OPEN_TRAY;
936
937 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
938 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
939 cd->cdi.capacity =
940 cdrom_number_of_slots(&cd->cdi);
941 if (cd->cdi.capacity <= 1)
942 /* not a changer */
943 cd->cdi.mask |= CDC_SELECT_DISC;
944 /*else I don't think it can close its tray
945 cd->cdi.mask |= CDC_CLOSE_TRAY; */
946
947 /*
948 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
949 */
950 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
951 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
952 cd->device->writeable = 1;
953 }
954
1da177e4
LT
955 kfree(buffer);
956}
957
958/*
959 * sr_packet() is the entry point for the generic commands generated
960 * by the Uniform CD-ROM layer.
961 */
962static int sr_packet(struct cdrom_device_info *cdi,
963 struct packet_command *cgc)
964{
8e04d805
HG
965 struct scsi_cd *cd = cdi->handle;
966 struct scsi_device *sdev = cd->device;
967
968 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
969 return -EDRIVE_CANT_DO_THIS;
970
1da177e4
LT
971 if (cgc->timeout <= 0)
972 cgc->timeout = IOCTL_TIMEOUT;
973
8e04d805 974 sr_do_ioctl(cd, cgc);
1da177e4
LT
975
976 return cgc->stat;
977}
978
979/**
980 * sr_kref_release - Called to free the scsi_cd structure
981 * @kref: pointer to embedded kref
982 *
0b950672 983 * sr_ref_mutex must be held entering this routine. Because it is
1da177e4
LT
984 * called on last put, you should always use the scsi_cd_get()
985 * scsi_cd_put() helpers which manipulate the semaphore directly
986 * and never do a direct kref_put().
987 **/
988static void sr_kref_release(struct kref *kref)
989{
990 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
991 struct gendisk *disk = cd->disk;
992
993 spin_lock(&sr_index_lock);
f331c029 994 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
1da177e4
LT
995 spin_unlock(&sr_index_lock);
996
997 unregister_cdrom(&cd->cdi);
998
999 disk->private_data = NULL;
1000
1001 put_disk(disk);
1002
1003 kfree(cd);
1004}
1005
1006static int sr_remove(struct device *dev)
1007{
1008 struct scsi_cd *cd = dev_get_drvdata(dev);
1009
6c7f1e2f
AL
1010 scsi_autopm_get_device(cd->device);
1011
b391277a 1012 blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
1da177e4 1013 del_gendisk(cd->disk);
3d1df278 1014 dev_set_drvdata(dev, NULL);
1da177e4 1015
0b950672 1016 mutex_lock(&sr_ref_mutex);
1da177e4 1017 kref_put(&cd->kref, sr_kref_release);
0b950672 1018 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
1019
1020 return 0;
1021}
1022
1023static int __init init_sr(void)
1024{
1025 int rc;
1026
1027 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1028 if (rc)
1029 return rc;
da3962fe
AM
1030 rc = scsi_register_driver(&sr_template.gendrv);
1031 if (rc)
1032 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1033
1034 return rc;
1da177e4
LT
1035}
1036
1037static void __exit exit_sr(void)
1038{
1039 scsi_unregister_driver(&sr_template.gendrv);
1040 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1041}
1042
1043module_init(init_sr);
1044module_exit(exit_sr);
1045MODULE_LICENSE("GPL");