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