Revert "powerpc: Rework dma-noncoherent to use generic vmalloc layer"
[deliverable/linux.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>
1da177e4
LT
47#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>
820732b5 53#include <scsi/scsi_cmnd.h>
1da177e4
LT
54#include <scsi/scsi_eh.h>
55#include <scsi/scsi_host.h>
56#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
1da177e4
LT
57
58#include "scsi_logging.h"
59#include "sr.h"
60
61
f018fa55
RH
62MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63MODULE_LICENSE("GPL");
64MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
d7b8bcb0
MT
65MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
f018fa55 67
1da177e4
LT
68#define SR_DISKS 256
69
1da177e4
LT
70#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| \
6a2900b6 73 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
1da177e4
LT
74 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 *);
7b3d9545 79static int sr_done(struct scsi_cmnd *);
1da177e4
LT
80
81static struct scsi_driver sr_template = {
82 .owner = THIS_MODULE,
83 .gendrv = {
84 .name = "sr",
85 .probe = sr_probe,
86 .remove = sr_remove,
87 },
7b3d9545 88 .done = sr_done,
1da177e4
LT
89};
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) */
0b950672 97static DEFINE_MUTEX(sr_ref_mutex);
1da177e4
LT
98
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,
1da177e4
LT
120 .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
0b950672 139 mutex_lock(&sr_ref_mutex);
1da177e4
LT
140 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:
0b950672 152 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
153 return cd;
154}
155
858119e1 156static void scsi_cd_put(struct scsi_cd *cd)
1da177e4
LT
157{
158 struct scsi_device *sdev = cd->device;
159
0b950672 160 mutex_lock(&sr_ref_mutex);
1da177e4
LT
161 kref_put(&cd->kref, sr_kref_release);
162 scsi_device_put(sdev);
0b950672 163 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
164}
165
38582a62
JB
166/* 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,
f4f4e47e 180 retries--, NULL);
d1daeabf
JB
181 if (scsi_sense_valid(sshdr) &&
182 sshdr->sense_key == UNIT_ATTENTION)
183 sdev->changed = 1;
38582a62
JB
184
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
1da177e4
LT
192/*
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
44818efb 202static int sr_media_change(struct cdrom_device_info *cdi, int slot)
1da177e4
LT
203{
204 struct scsi_cd *cd = cdi->handle;
205 int retval;
001aac25 206 struct scsi_sense_hdr *sshdr;
1da177e4
LT
207
208 if (CDSL_CURRENT != slot) {
209 /* no changer support */
210 return -EINVAL;
211 }
212
001aac25 213 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
38582a62 214 retval = sr_test_unit_ready(cd->device, sshdr);
001aac25
JB
215 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 */
1da177e4 223 cd->device->changed = 1;
285e9670
KS
224 /* This will force a flush, if called from check_disk_change */
225 retval = 1;
226 goto out;
1da177e4
LT
227 };
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);
51490c89 236 get_sectorsize(cd);
1da177e4 237 }
285e9670
KS
238
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;
001aac25 245 kfree(sshdr);
285e9670 246
1da177e4
LT
247 return retval;
248}
249
250/*
7b3d9545 251 * sr_done is the interrupt routine for the device driver.
1da177e4 252 *
7b3d9545 253 * It will be notified on the end of a SCSI read / write, and will take one
1da177e4
LT
254 * of several actions based on success or failure.
255 */
7b3d9545 256static int sr_done(struct scsi_cmnd *SCpnt)
1da177e4
LT
257{
258 int result = SCpnt->result;
30b0c37b 259 int this_count = scsi_bufflen(SCpnt);
1da177e4
LT
260 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;
1da177e4
LT
283 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);
295 good_bytes = (error_sector - SCpnt->request->sector) << 9;
296 if (good_bytes < 0 || good_bytes >= this_count)
297 good_bytes = 0;
298 /*
299 * The SCSI specification allows for the value
300 * returned by READ CAPACITY to be up to 75 2K
301 * sectors past the last readable block.
302 * Therefore, if we hit a medium error within the
303 * last 75 2K sectors, we decrease the saved size
304 * value.
305 */
306 if (error_sector < get_capacity(cd->disk) &&
307 cd->capacity - error_sector < 4 * 75)
308 set_capacity(cd->disk, error_sector);
309 break;
310
311 case RECOVERED_ERROR:
1da177e4
LT
312 good_bytes = this_count;
313 break;
314
315 default:
316 break;
317 }
318 }
319
7b3d9545 320 return good_bytes;
1da177e4
LT
321}
322
7f9a6bc4 323static int sr_prep_fn(struct request_queue *q, struct request *rq)
1da177e4 324{
242f9dcb 325 int block = 0, this_count, s_size;
7f9a6bc4
JB
326 struct scsi_cd *cd;
327 struct scsi_cmnd *SCpnt;
328 struct scsi_device *sdp = q->queuedata;
329 int ret;
330
331 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
332 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
333 goto out;
334 } else if (rq->cmd_type != REQ_TYPE_FS) {
335 ret = BLKPREP_KILL;
336 goto out;
337 }
338 ret = scsi_setup_fs_cmnd(sdp, rq);
339 if (ret != BLKPREP_OK)
340 goto out;
341 SCpnt = rq->special;
342 cd = scsi_cd(rq->rq_disk);
343
344 /* from here on until we're complete, any goto out
345 * is used for a killable error condition */
346 ret = BLKPREP_KILL;
1da177e4
LT
347
348 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
349 cd->disk->disk_name, block));
350
351 if (!cd->device || !scsi_device_online(cd->device)) {
352 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
7f9a6bc4 353 rq->nr_sectors));
1da177e4 354 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
7f9a6bc4 355 goto out;
1da177e4
LT
356 }
357
358 if (cd->device->changed) {
359 /*
360 * quietly refuse to do anything to a changed disc until the
361 * changed bit has been reset
362 */
7f9a6bc4 363 goto out;
1da177e4
LT
364 }
365
1da177e4
LT
366 /*
367 * we do lazy blocksize switching (when reading XA sectors,
368 * see CDROMREADMODE2 ioctl)
369 */
370 s_size = cd->device->sector_size;
371 if (s_size > 2048) {
372 if (!in_interrupt())
373 sr_set_blocklength(cd, 2048);
374 else
375 printk("sr: can't switch blocksize: in interrupt\n");
376 }
377
378 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
3bf743e7 379 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
7f9a6bc4 380 goto out;
1da177e4
LT
381 }
382
7f9a6bc4 383 if (rq_data_dir(rq) == WRITE) {
1da177e4 384 if (!cd->device->writeable)
7f9a6bc4 385 goto out;
1da177e4
LT
386 SCpnt->cmnd[0] = WRITE_10;
387 SCpnt->sc_data_direction = DMA_TO_DEVICE;
388 cd->cdi.media_written = 1;
7f9a6bc4 389 } else if (rq_data_dir(rq) == READ) {
1da177e4
LT
390 SCpnt->cmnd[0] = READ_10;
391 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
392 } else {
7f9a6bc4
JB
393 blk_dump_rq_flags(rq, "Unknown sr command");
394 goto out;
1da177e4
LT
395 }
396
397 {
30b0c37b
BH
398 struct scatterlist *sg;
399 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
1da177e4 400
30b0c37b
BH
401 scsi_for_each_sg(SCpnt, sg, sg_count, i)
402 size += sg->length;
403
404 if (size != scsi_bufflen(SCpnt)) {
3bf743e7
JG
405 scmd_printk(KERN_ERR, SCpnt,
406 "mismatch count %d, bytes %d\n",
30b0c37b
BH
407 size, scsi_bufflen(SCpnt));
408 if (scsi_bufflen(SCpnt) > size)
409 SCpnt->sdb.length = size;
1da177e4
LT
410 }
411 }
412
413 /*
414 * request doesn't start on hw block boundary, add scatter pads
415 */
7f9a6bc4 416 if (((unsigned int)rq->sector % (s_size >> 9)) ||
30b0c37b 417 (scsi_bufflen(SCpnt) % s_size)) {
3bf743e7 418 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
7f9a6bc4 419 goto out;
1da177e4
LT
420 }
421
30b0c37b 422 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
1da177e4
LT
423
424
425 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
426 cd->cdi.name,
7f9a6bc4 427 (rq_data_dir(rq) == WRITE) ?
1da177e4 428 "writing" : "reading",
7f9a6bc4 429 this_count, rq->nr_sectors));
1da177e4
LT
430
431 SCpnt->cmnd[1] = 0;
7f9a6bc4 432 block = (unsigned int)rq->sector / (s_size >> 9);
1da177e4
LT
433
434 if (this_count > 0xffff) {
435 this_count = 0xffff;
30b0c37b 436 SCpnt->sdb.length = this_count * s_size;
1da177e4
LT
437 }
438
439 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
440 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
441 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
442 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
443 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
444 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
445 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
446
447 /*
448 * We shouldn't disconnect in the middle of a sector, so with a dumb
449 * host adapter, it's safe to assume that we can at least transfer
450 * this many bytes between each connect / disconnect.
451 */
452 SCpnt->transfersize = cd->device->sector_size;
453 SCpnt->underflow = this_count << 9;
1da177e4 454 SCpnt->allowed = MAX_RETRIES;
1da177e4 455
1da177e4
LT
456 /*
457 * This indicates that the command is ready from our end to be
458 * queued.
459 */
7f9a6bc4
JB
460 ret = BLKPREP_OK;
461 out:
462 return scsi_prep_return(q, rq, ret);
1da177e4
LT
463}
464
40cc51be 465static int sr_block_open(struct block_device *bdev, fmode_t mode)
1da177e4 466{
40cc51be
AV
467 struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk);
468 int ret = -ENXIO;
1da177e4 469
40cc51be
AV
470 if (cd) {
471 ret = cdrom_open(&cd->cdi, bdev, mode);
472 if (ret)
473 scsi_cd_put(cd);
474 }
1da177e4
LT
475 return ret;
476}
477
40cc51be 478static int sr_block_release(struct gendisk *disk, fmode_t mode)
1da177e4 479{
40cc51be
AV
480 struct scsi_cd *cd = scsi_cd(disk);
481 cdrom_release(&cd->cdi, mode);
1da177e4 482 scsi_cd_put(cd);
1da177e4
LT
483 return 0;
484}
485
40cc51be 486static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
1da177e4
LT
487 unsigned long arg)
488{
40cc51be 489 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
1da177e4 490 struct scsi_device *sdev = cd->device;
6a2900b6
CH
491 void __user *argp = (void __user *)arg;
492 int ret;
1da177e4 493
6a2900b6
CH
494 /*
495 * Send SCSI addressing ioctls directly to mid level, send other
496 * ioctls to cdrom/block level.
497 */
498 switch (cmd) {
499 case SCSI_IOCTL_GET_IDLUN:
500 case SCSI_IOCTL_GET_BUS_NUMBER:
501 return scsi_ioctl(sdev, cmd, argp);
1da177e4 502 }
6a2900b6 503
40cc51be 504 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
6397256b 505 if (ret != -ENOSYS)
6a2900b6
CH
506 return ret;
507
508 /*
509 * ENODEV means that we didn't recognise the ioctl, or that we
510 * cannot execute it in the current device state. In either
511 * case fall through to scsi_ioctl, which will return ENDOEV again
512 * if it doesn't recognise the ioctl
513 */
83ff6fe8 514 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
fd4ce1ac 515 (mode & FMODE_NDELAY) != 0);
6a2900b6
CH
516 if (ret != -ENODEV)
517 return ret;
518 return scsi_ioctl(sdev, cmd, argp);
1da177e4
LT
519}
520
521static int sr_block_media_changed(struct gendisk *disk)
522{
523 struct scsi_cd *cd = scsi_cd(disk);
524 return cdrom_media_changed(&cd->cdi);
525}
526
527static struct block_device_operations sr_bdops =
528{
529 .owner = THIS_MODULE,
40cc51be
AV
530 .open = sr_block_open,
531 .release = sr_block_release,
532 .locked_ioctl = sr_block_ioctl,
1da177e4
LT
533 .media_changed = sr_block_media_changed,
534 /*
535 * No compat_ioctl for now because sr_block_ioctl never
536 * seems to pass arbitary ioctls down to host drivers.
537 */
538};
539
540static int sr_open(struct cdrom_device_info *cdi, int purpose)
541{
542 struct scsi_cd *cd = cdi->handle;
543 struct scsi_device *sdev = cd->device;
544 int retval;
545
546 /*
547 * If the device is in error recovery, wait until it is done.
548 * If the device is offline, then disallow any access to it.
549 */
550 retval = -ENXIO;
551 if (!scsi_block_when_processing_errors(sdev))
552 goto error_out;
553
1da177e4
LT
554 return 0;
555
556error_out:
557 return retval;
558}
559
560static void sr_release(struct cdrom_device_info *cdi)
561{
562 struct scsi_cd *cd = cdi->handle;
563
564 if (cd->device->sector_size > 2048)
565 sr_set_blocklength(cd, 2048);
566
567}
568
569static int sr_probe(struct device *dev)
570{
571 struct scsi_device *sdev = to_scsi_device(dev);
572 struct gendisk *disk;
573 struct scsi_cd *cd;
574 int minor, error;
575
576 error = -ENODEV;
577 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
578 goto fail;
579
580 error = -ENOMEM;
24669f75 581 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
1da177e4
LT
582 if (!cd)
583 goto fail;
1da177e4
LT
584
585 kref_init(&cd->kref);
586
587 disk = alloc_disk(1);
588 if (!disk)
589 goto fail_free;
590
591 spin_lock(&sr_index_lock);
592 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
593 if (minor == SR_DISKS) {
594 spin_unlock(&sr_index_lock);
595 error = -EBUSY;
596 goto fail_put;
597 }
598 __set_bit(minor, sr_index_bits);
599 spin_unlock(&sr_index_lock);
600
601 disk->major = SCSI_CDROM_MAJOR;
602 disk->first_minor = minor;
603 sprintf(disk->disk_name, "sr%d", minor);
604 disk->fops = &sr_bdops;
605 disk->flags = GENHD_FL_CD;
606
242f9dcb
JA
607 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
608
1da177e4
LT
609 cd->device = sdev;
610 cd->disk = disk;
611 cd->driver = &sr_template;
612 cd->disk = disk;
613 cd->capacity = 0x1fffff;
1da177e4 614 cd->device->changed = 1; /* force recheck CD type */
c02e6002 615 cd->previous_state = 1;
1da177e4
LT
616 cd->use = 1;
617 cd->readcd_known = 0;
618 cd->readcd_cdda = 0;
619
620 cd->cdi.ops = &sr_dops;
621 cd->cdi.handle = cd;
622 cd->cdi.mask = 0;
623 cd->cdi.capacity = 1;
624 sprintf(cd->cdi.name, "sr%d", minor);
625
626 sdev->sector_size = 2048; /* A guess, just in case */
627
628 /* FIXME: need to handle a get_capabilities failure properly ?? */
629 get_capabilities(cd);
7f9a6bc4 630 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
1da177e4
LT
631 sr_vendor_init(cd);
632
1da177e4
LT
633 disk->driverfs_dev = &sdev->sdev_gendev;
634 set_capacity(disk, cd->capacity);
635 disk->private_data = &cd->driver;
636 disk->queue = sdev->request_queue;
637 cd->cdi.disk = disk;
638
639 if (register_cdrom(&cd->cdi))
640 goto fail_put;
641
642 dev_set_drvdata(dev, cd);
643 disk->flags |= GENHD_FL_REMOVABLE;
644 add_disk(disk);
645
9ccfc756
JB
646 sdev_printk(KERN_DEBUG, sdev,
647 "Attached scsi CD-ROM %s\n", cd->cdi.name);
1da177e4
LT
648 return 0;
649
650fail_put:
651 put_disk(disk);
652fail_free:
653 kfree(cd);
654fail:
655 return error;
656}
657
658
659static void get_sectorsize(struct scsi_cd *cd)
660{
661 unsigned char cmd[10];
62858dac 662 unsigned char buffer[8];
1da177e4
LT
663 int the_result, retries = 3;
664 int sector_size;
165125e1 665 struct request_queue *queue;
1da177e4 666
1da177e4
LT
667 do {
668 cmd[0] = READ_CAPACITY;
669 memset((void *) &cmd[1], 0, 9);
62858dac 670 memset(buffer, 0, sizeof(buffer));
1da177e4
LT
671
672 /* Do the command and wait.. */
820732b5 673 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
62858dac 674 buffer, sizeof(buffer), NULL,
f4f4e47e 675 SR_TIMEOUT, MAX_RETRIES, NULL);
1da177e4 676
1da177e4
LT
677 retries--;
678
679 } while (the_result && retries);
680
681
1da177e4
LT
682 if (the_result) {
683 cd->capacity = 0x1fffff;
684 sector_size = 2048; /* A guess, just in case */
1da177e4
LT
685 } else {
686#if 0
687 if (cdrom_get_last_written(&cd->cdi,
688 &cd->capacity))
689#endif
690 cd->capacity = 1 + ((buffer[0] << 24) |
691 (buffer[1] << 16) |
692 (buffer[2] << 8) |
693 buffer[3]);
694 sector_size = (buffer[4] << 24) |
695 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
696 switch (sector_size) {
697 /*
698 * HP 4020i CD-Recorder reports 2340 byte sectors
699 * Philips CD-Writers report 2352 byte sectors
700 *
701 * Use 2k sectors for them..
702 */
703 case 0:
704 case 2340:
705 case 2352:
706 sector_size = 2048;
707 /* fall through */
708 case 2048:
709 cd->capacity *= 4;
710 /* fall through */
711 case 512:
712 break;
713 default:
714 printk("%s: unsupported sector size %d.\n",
715 cd->cdi.name, sector_size);
716 cd->capacity = 0;
1da177e4
LT
717 }
718
719 cd->device->sector_size = sector_size;
720
721 /*
722 * Add this so that we have the ability to correctly gauge
723 * what the device is capable of.
724 */
1da177e4
LT
725 set_capacity(cd->disk, cd->capacity);
726 }
727
728 queue = cd->device->request_queue;
729 blk_queue_hardsect_size(queue, sector_size);
1da177e4 730
62858dac 731 return;
1da177e4
LT
732}
733
734static void get_capabilities(struct scsi_cd *cd)
735{
736 unsigned char *buffer;
737 struct scsi_mode_data data;
820732b5 738 struct scsi_sense_hdr sshdr;
38582a62 739 int rc, n;
1da177e4 740
0ad78200 741 static const char *loadmech[] =
1da177e4
LT
742 {
743 "caddy",
744 "tray",
745 "pop-up",
746 "",
747 "changer",
748 "cartridge changer",
749 "",
750 ""
751 };
752
1da177e4
LT
753
754 /* allocate transfer buffer */
755 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
756 if (!buffer) {
757 printk(KERN_ERR "sr: out of memory.\n");
1da177e4
LT
758 return;
759 }
760
38582a62
JB
761 /* eat unit attentions */
762 sr_test_unit_ready(cd->device, &sshdr);
1da177e4
LT
763
764 /* ask for mode page 0x2a */
765 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
1cf72699 766 SR_TIMEOUT, 3, &data, NULL);
1da177e4
LT
767
768 if (!scsi_status_is_good(rc)) {
769 /* failed, drive doesn't have capabilities mode page */
770 cd->cdi.speed = 1;
771 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
bcc1e382
CE
772 CDC_DVD | CDC_DVD_RAM |
773 CDC_SELECT_DISC | CDC_SELECT_SPEED |
774 CDC_MRW | CDC_MRW_W | CDC_RAM);
1da177e4
LT
775 kfree(buffer);
776 printk("%s: scsi-1 drive\n", cd->cdi.name);
777 return;
778 }
779
780 n = data.header_length + data.block_descriptor_length;
781 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
782 cd->readcd_known = 1;
783 cd->readcd_cdda = buffer[n + 5] & 0x01;
784 /* print some capability bits */
785 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
786 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
787 cd->cdi.speed,
788 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
789 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
790 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
791 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
792 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
793 loadmech[buffer[n + 6] >> 5]);
794 if ((buffer[n + 6] >> 5) == 0)
795 /* caddy drives can't close tray... */
796 cd->cdi.mask |= CDC_CLOSE_TRAY;
797 if ((buffer[n + 2] & 0x8) == 0)
798 /* not a DVD drive */
799 cd->cdi.mask |= CDC_DVD;
800 if ((buffer[n + 3] & 0x20) == 0)
801 /* can't write DVD-RAM media */
802 cd->cdi.mask |= CDC_DVD_RAM;
803 if ((buffer[n + 3] & 0x10) == 0)
804 /* can't write DVD-R media */
805 cd->cdi.mask |= CDC_DVD_R;
806 if ((buffer[n + 3] & 0x2) == 0)
807 /* can't write CD-RW media */
808 cd->cdi.mask |= CDC_CD_RW;
809 if ((buffer[n + 3] & 0x1) == 0)
810 /* can't write CD-R media */
811 cd->cdi.mask |= CDC_CD_R;
812 if ((buffer[n + 6] & 0x8) == 0)
813 /* can't eject */
814 cd->cdi.mask |= CDC_OPEN_TRAY;
815
816 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
817 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
818 cd->cdi.capacity =
819 cdrom_number_of_slots(&cd->cdi);
820 if (cd->cdi.capacity <= 1)
821 /* not a changer */
822 cd->cdi.mask |= CDC_SELECT_DISC;
823 /*else I don't think it can close its tray
824 cd->cdi.mask |= CDC_CLOSE_TRAY; */
825
826 /*
827 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
828 */
829 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
830 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
831 cd->device->writeable = 1;
832 }
833
1da177e4
LT
834 kfree(buffer);
835}
836
837/*
838 * sr_packet() is the entry point for the generic commands generated
839 * by the Uniform CD-ROM layer.
840 */
841static int sr_packet(struct cdrom_device_info *cdi,
842 struct packet_command *cgc)
843{
844 if (cgc->timeout <= 0)
845 cgc->timeout = IOCTL_TIMEOUT;
846
847 sr_do_ioctl(cdi->handle, cgc);
848
849 return cgc->stat;
850}
851
852/**
853 * sr_kref_release - Called to free the scsi_cd structure
854 * @kref: pointer to embedded kref
855 *
0b950672 856 * sr_ref_mutex must be held entering this routine. Because it is
1da177e4
LT
857 * called on last put, you should always use the scsi_cd_get()
858 * scsi_cd_put() helpers which manipulate the semaphore directly
859 * and never do a direct kref_put().
860 **/
861static void sr_kref_release(struct kref *kref)
862{
863 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
864 struct gendisk *disk = cd->disk;
865
866 spin_lock(&sr_index_lock);
f331c029 867 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
1da177e4
LT
868 spin_unlock(&sr_index_lock);
869
870 unregister_cdrom(&cd->cdi);
871
872 disk->private_data = NULL;
873
874 put_disk(disk);
875
876 kfree(cd);
877}
878
879static int sr_remove(struct device *dev)
880{
881 struct scsi_cd *cd = dev_get_drvdata(dev);
882
883 del_gendisk(cd->disk);
884
0b950672 885 mutex_lock(&sr_ref_mutex);
1da177e4 886 kref_put(&cd->kref, sr_kref_release);
0b950672 887 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
888
889 return 0;
890}
891
892static int __init init_sr(void)
893{
894 int rc;
895
896 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
897 if (rc)
898 return rc;
da3962fe
AM
899 rc = scsi_register_driver(&sr_template.gendrv);
900 if (rc)
901 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
902
903 return rc;
1da177e4
LT
904}
905
906static void __exit exit_sr(void)
907{
908 scsi_unregister_driver(&sr_template.gendrv);
909 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
910}
911
912module_init(init_sr);
913module_exit(exit_sr);
914MODULE_LICENSE("GPL");
This page took 0.443097 seconds and 5 git commands to generate.