1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/module.h>
36#include <linux/fs.h>
37#include <linux/kernel.h>
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>
46#include <linux/mutex.h>
47#include <linux/slab.h>
48#include <linux/pm_runtime.h>
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>
55#include <scsi/scsi_cmnd.h>
56#include <scsi/scsi_eh.h>
57#include <scsi/scsi_host.h>
58#include <scsi/scsi_ioctl.h>
59
60#include "scsi_logging.h"
61#include "sr.h"
62
63
64MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
65MODULE_LICENSE("GPL");
66MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
67MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
68MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
69
70#define SR_DISKS 256
71
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| \
75 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
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
79static DEFINE_MUTEX(sr_mutex);
80static int sr_probe(struct device *);
81static int sr_remove(struct device *);
82static int sr_init_command(struct scsi_cmnd *SCpnt);
83static int sr_done(struct scsi_cmnd *);
84static int sr_runtime_suspend(struct device *dev);
85
86static struct dev_pm_ops sr_pm_ops = {
87 .runtime_suspend = sr_runtime_suspend,
88};
89
90static struct scsi_driver sr_template = {
91 .owner = THIS_MODULE,
92 .gendrv = {
93 .name = "sr",
94 .probe = sr_probe,
95 .remove = sr_remove,
96 .pm = &sr_pm_ops,
97 },
98 .init_command = sr_init_command,
99 .done = sr_done,
100};
101
102static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
103static DEFINE_SPINLOCK(sr_index_lock);
104
105
106
107
108static DEFINE_MUTEX(sr_ref_mutex);
109
110static int sr_open(struct cdrom_device_info *, int);
111static void sr_release(struct cdrom_device_info *);
112
113static void get_sectorsize(struct scsi_cd *);
114static void get_capabilities(struct scsi_cd *);
115
116static unsigned int sr_check_events(struct cdrom_device_info *cdi,
117 unsigned int clearing, int slot);
118static int sr_packet(struct cdrom_device_info *, struct packet_command *);
119
120static struct cdrom_device_ops sr_dops = {
121 .open = sr_open,
122 .release = sr_release,
123 .drive_status = sr_drive_status,
124 .check_events = sr_check_events,
125 .tray_move = sr_tray_move,
126 .lock_door = sr_lock_door,
127 .select_speed = sr_select_speed,
128 .get_last_session = sr_get_last_session,
129 .get_mcn = sr_get_mcn,
130 .reset = sr_reset,
131 .audio_ioctl = sr_audio_ioctl,
132 .capability = SR_CAPABILITIES,
133 .generic_packet = sr_packet,
134};
135
136static void sr_kref_release(struct kref *kref);
137
138static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
139{
140 return container_of(disk->private_data, struct scsi_cd, driver);
141}
142
143static int sr_runtime_suspend(struct device *dev)
144{
145 struct scsi_cd *cd = dev_get_drvdata(dev);
146
147 if (!cd)
148 return 0;
149
150 if (cd->media_present)
151 return -EBUSY;
152 else
153 return 0;
154}
155
156
157
158
159
160static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
161{
162 struct scsi_cd *cd = NULL;
163
164 mutex_lock(&sr_ref_mutex);
165 if (disk->private_data == NULL)
166 goto out;
167 cd = scsi_cd(disk);
168 kref_get(&cd->kref);
169 if (scsi_device_get(cd->device))
170 goto out_put;
171 if (!scsi_autopm_get_device(cd->device))
172 goto out;
173
174 out_put:
175 kref_put(&cd->kref, sr_kref_release);
176 cd = NULL;
177 out:
178 mutex_unlock(&sr_ref_mutex);
179 return cd;
180}
181
182static void scsi_cd_put(struct scsi_cd *cd)
183{
184 struct scsi_device *sdev = cd->device;
185
186 mutex_lock(&sr_ref_mutex);
187 kref_put(&cd->kref, sr_kref_release);
188 scsi_autopm_put_device(sdev);
189 scsi_device_put(sdev);
190 mutex_unlock(&sr_ref_mutex);
191}
192
193static unsigned int sr_get_events(struct scsi_device *sdev)
194{
195 u8 buf[8];
196 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
197 1,
198 0, 0,
199 1 << 4,
200 0, 0,
201 0, sizeof(buf),
202 0,
203 };
204 struct event_header *eh = (void *)buf;
205 struct media_event_desc *med = (void *)(buf + 4);
206 struct scsi_sense_hdr sshdr;
207 int result;
208
209 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
210 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
211 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
212 return DISK_EVENT_MEDIA_CHANGE;
213
214 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
215 return 0;
216
217 if (eh->nea || eh->notification_class != 0x4)
218 return 0;
219
220 if (med->media_event_code == 1)
221 return DISK_EVENT_EJECT_REQUEST;
222 else if (med->media_event_code == 2)
223 return DISK_EVENT_MEDIA_CHANGE;
224 return 0;
225}
226
227
228
229
230
231
232
233
234static unsigned int sr_check_events(struct cdrom_device_info *cdi,
235 unsigned int clearing, int slot)
236{
237 struct scsi_cd *cd = cdi->handle;
238 bool last_present;
239 struct scsi_sense_hdr sshdr;
240 unsigned int events;
241 int ret;
242
243
244 if (CDSL_CURRENT != slot)
245 return 0;
246
247 events = sr_get_events(cd->device);
248 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
249
250
251
252
253
254
255
256 if (cd->ignore_get_event) {
257 events &= ~DISK_EVENT_MEDIA_CHANGE;
258 goto do_tur;
259 }
260
261
262
263
264
265
266 if (cd->device->changed) {
267 events |= DISK_EVENT_MEDIA_CHANGE;
268 cd->device->changed = 0;
269 cd->tur_changed = true;
270 }
271
272 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
273 return events;
274do_tur:
275
276 last_present = cd->media_present;
277 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
278
279
280
281
282
283
284 cd->media_present = scsi_status_is_good(ret) ||
285 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
286
287 if (last_present != cd->media_present)
288 cd->device->changed = 1;
289
290 if (cd->device->changed) {
291 events |= DISK_EVENT_MEDIA_CHANGE;
292 cd->device->changed = 0;
293 cd->tur_changed = true;
294 }
295
296 if (cd->ignore_get_event)
297 return events;
298
299
300 if (!cd->tur_changed) {
301 if (cd->get_event_changed) {
302 if (cd->tur_mismatch++ > 8) {
303 sr_printk(KERN_WARNING, cd,
304 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
305 cd->ignore_get_event = true;
306 }
307 } else {
308 cd->tur_mismatch = 0;
309 }
310 }
311 cd->tur_changed = false;
312 cd->get_event_changed = false;
313
314 return events;
315}
316
317
318
319
320
321
322
323static int sr_done(struct scsi_cmnd *SCpnt)
324{
325 int result = SCpnt->result;
326 int this_count = scsi_bufflen(SCpnt);
327 int good_bytes = (result == 0 ? this_count : 0);
328 int block_sectors = 0;
329 long error_sector;
330 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
331
332#ifdef DEBUG
333 scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
334#endif
335
336
337
338
339
340
341
342 if (driver_byte(result) != 0 &&
343 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
344 switch (SCpnt->sense_buffer[2]) {
345 case MEDIUM_ERROR:
346 case VOLUME_OVERFLOW:
347 case ILLEGAL_REQUEST:
348 if (!(SCpnt->sense_buffer[0] & 0x90))
349 break;
350 error_sector = (SCpnt->sense_buffer[3] << 24) |
351 (SCpnt->sense_buffer[4] << 16) |
352 (SCpnt->sense_buffer[5] << 8) |
353 SCpnt->sense_buffer[6];
354 if (SCpnt->request->bio != NULL)
355 block_sectors =
356 bio_sectors(SCpnt->request->bio);
357 if (block_sectors < 4)
358 block_sectors = 4;
359 if (cd->device->sector_size == 2048)
360 error_sector <<= 2;
361 error_sector &= ~(block_sectors - 1);
362 good_bytes = (error_sector -
363 blk_rq_pos(SCpnt->request)) << 9;
364 if (good_bytes < 0 || good_bytes >= this_count)
365 good_bytes = 0;
366
367
368
369
370
371
372
373
374 if (error_sector < get_capacity(cd->disk) &&
375 cd->capacity - error_sector < 4 * 75)
376 set_capacity(cd->disk, error_sector);
377 break;
378
379 case RECOVERED_ERROR:
380 good_bytes = this_count;
381 break;
382
383 default:
384 break;
385 }
386 }
387
388 return good_bytes;
389}
390
391static int sr_init_command(struct scsi_cmnd *SCpnt)
392{
393 int block = 0, this_count, s_size;
394 struct scsi_cd *cd;
395 struct request *rq = SCpnt->request;
396 int ret;
397
398 ret = scsi_init_io(SCpnt, GFP_ATOMIC);
399 if (ret != BLKPREP_OK)
400 goto out;
401 SCpnt = rq->special;
402 cd = scsi_cd(rq->rq_disk);
403
404
405
406 ret = BLKPREP_KILL;
407
408 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
409 "Doing sr request, block = %d\n", block));
410
411 if (!cd->device || !scsi_device_online(cd->device)) {
412 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
413 "Finishing %u sectors\n", blk_rq_sectors(rq)));
414 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
415 "Retry with 0x%p\n", SCpnt));
416 goto out;
417 }
418
419 if (cd->device->changed) {
420
421
422
423
424 goto out;
425 }
426
427
428
429
430
431 s_size = cd->device->sector_size;
432 if (s_size > 2048) {
433 if (!in_interrupt())
434 sr_set_blocklength(cd, 2048);
435 else
436 scmd_printk(KERN_INFO, SCpnt,
437 "can't switch blocksize: in interrupt\n");
438 }
439
440 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
441 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
442 goto out;
443 }
444
445 if (rq_data_dir(rq) == WRITE) {
446 if (!cd->device->writeable)
447 goto out;
448 SCpnt->cmnd[0] = WRITE_10;
449 cd->cdi.media_written = 1;
450 } else if (rq_data_dir(rq) == READ) {
451 SCpnt->cmnd[0] = READ_10;
452 } else {
453 blk_dump_rq_flags(rq, "Unknown sr command");
454 goto out;
455 }
456
457 {
458 struct scatterlist *sg;
459 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
460
461 scsi_for_each_sg(SCpnt, sg, sg_count, i)
462 size += sg->length;
463
464 if (size != scsi_bufflen(SCpnt)) {
465 scmd_printk(KERN_ERR, SCpnt,
466 "mismatch count %d, bytes %d\n",
467 size, scsi_bufflen(SCpnt));
468 if (scsi_bufflen(SCpnt) > size)
469 SCpnt->sdb.length = size;
470 }
471 }
472
473
474
475
476 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
477 (scsi_bufflen(SCpnt) % s_size)) {
478 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
479 goto out;
480 }
481
482 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
483
484
485 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
486 "%s %d/%u 512 byte blocks.\n",
487 (rq_data_dir(rq) == WRITE) ?
488 "writing" : "reading",
489 this_count, blk_rq_sectors(rq)));
490
491 SCpnt->cmnd[1] = 0;
492 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
493
494 if (this_count > 0xffff) {
495 this_count = 0xffff;
496 SCpnt->sdb.length = this_count * s_size;
497 }
498
499 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
500 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
501 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
502 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
503 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
504 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
505 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
506
507
508
509
510
511
512 SCpnt->transfersize = cd->device->sector_size;
513 SCpnt->underflow = this_count << 9;
514 SCpnt->allowed = MAX_RETRIES;
515
516
517
518
519
520 ret = BLKPREP_OK;
521 out:
522 return ret;
523}
524
525static int sr_block_open(struct block_device *bdev, fmode_t mode)
526{
527 struct scsi_cd *cd;
528 int ret = -ENXIO;
529
530 check_disk_change(bdev);
531
532 mutex_lock(&sr_mutex);
533 cd = scsi_cd_get(bdev->bd_disk);
534 if (cd) {
535 ret = cdrom_open(&cd->cdi, bdev, mode);
536 if (ret)
537 scsi_cd_put(cd);
538 }
539 mutex_unlock(&sr_mutex);
540 return ret;
541}
542
543static void sr_block_release(struct gendisk *disk, fmode_t mode)
544{
545 struct scsi_cd *cd = scsi_cd(disk);
546 mutex_lock(&sr_mutex);
547 cdrom_release(&cd->cdi, mode);
548 scsi_cd_put(cd);
549 mutex_unlock(&sr_mutex);
550}
551
552static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
553 unsigned long arg)
554{
555 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
556 struct scsi_device *sdev = cd->device;
557 void __user *argp = (void __user *)arg;
558 int ret;
559
560 scsi_autopm_get_device(cd->device);
561
562 mutex_lock(&sr_mutex);
563
564
565
566
567
568 switch (cmd) {
569 case SCSI_IOCTL_GET_IDLUN:
570 case SCSI_IOCTL_GET_BUS_NUMBER:
571 ret = scsi_ioctl(sdev, cmd, argp);
572 goto out;
573 }
574
575 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
576 if (ret != -ENOSYS)
577 goto out;
578
579
580
581
582
583
584
585 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
586 (mode & FMODE_NDELAY) != 0);
587 if (ret != -ENODEV)
588 goto out;
589 ret = scsi_ioctl(sdev, cmd, argp);
590
591out:
592 mutex_unlock(&sr_mutex);
593 scsi_autopm_put_device(cd->device);
594 return ret;
595}
596
597static unsigned int sr_block_check_events(struct gendisk *disk,
598 unsigned int clearing)
599{
600 unsigned int ret;
601 struct scsi_cd *cd;
602
603 cd = scsi_cd_get(disk);
604 if (!cd)
605 return 0;
606
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 }
614
615 scsi_cd_put(cd);
616
617 return ret;
618}
619
620static int sr_block_revalidate_disk(struct gendisk *disk)
621{
622 struct scsi_sense_hdr sshdr;
623 struct scsi_cd *cd;
624
625 cd = scsi_cd_get(disk);
626 if (!cd)
627 return -ENXIO;
628
629 scsi_autopm_get_device(cd->device);
630
631
632 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
633 goto out;
634
635 sr_cd_check(&cd->cdi);
636 get_sectorsize(cd);
637out:
638 scsi_autopm_put_device(cd->device);
639 scsi_cd_put(cd);
640 return 0;
641}
642
643static const struct block_device_operations sr_bdops =
644{
645 .owner = THIS_MODULE,
646 .open = sr_block_open,
647 .release = sr_block_release,
648 .ioctl = sr_block_ioctl,
649 .check_events = sr_block_check_events,
650 .revalidate_disk = sr_block_revalidate_disk,
651
652
653
654
655};
656
657static int sr_open(struct cdrom_device_info *cdi, int purpose)
658{
659 struct scsi_cd *cd = cdi->handle;
660 struct scsi_device *sdev = cd->device;
661 int retval;
662
663
664
665
666
667 retval = -ENXIO;
668 if (!scsi_block_when_processing_errors(sdev))
669 goto error_out;
670
671 return 0;
672
673error_out:
674 return retval;
675}
676
677static void sr_release(struct cdrom_device_info *cdi)
678{
679 struct scsi_cd *cd = cdi->handle;
680
681 if (cd->device->sector_size > 2048)
682 sr_set_blocklength(cd, 2048);
683
684}
685
686static int sr_probe(struct device *dev)
687{
688 struct scsi_device *sdev = to_scsi_device(dev);
689 struct gendisk *disk;
690 struct scsi_cd *cd;
691 int minor, error;
692
693 scsi_autopm_get_device(sdev);
694 error = -ENODEV;
695 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
696 goto fail;
697
698 error = -ENOMEM;
699 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
700 if (!cd)
701 goto fail;
702
703 kref_init(&cd->kref);
704
705 disk = alloc_disk(1);
706 if (!disk)
707 goto fail_free;
708
709 spin_lock(&sr_index_lock);
710 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
711 if (minor == SR_DISKS) {
712 spin_unlock(&sr_index_lock);
713 error = -EBUSY;
714 goto fail_put;
715 }
716 __set_bit(minor, sr_index_bits);
717 spin_unlock(&sr_index_lock);
718
719 disk->major = SCSI_CDROM_MAJOR;
720 disk->first_minor = minor;
721 sprintf(disk->disk_name, "sr%d", minor);
722 disk->fops = &sr_bdops;
723 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
724 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
725
726 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
727
728 cd->device = sdev;
729 cd->disk = disk;
730 cd->driver = &sr_template;
731 cd->disk = disk;
732 cd->capacity = 0x1fffff;
733 cd->device->changed = 1;
734 cd->media_present = 1;
735 cd->use = 1;
736 cd->readcd_known = 0;
737 cd->readcd_cdda = 0;
738
739 cd->cdi.ops = &sr_dops;
740 cd->cdi.handle = cd;
741 cd->cdi.mask = 0;
742 cd->cdi.capacity = 1;
743 sprintf(cd->cdi.name, "sr%d", minor);
744
745 sdev->sector_size = 2048;
746
747
748 get_capabilities(cd);
749 sr_vendor_init(cd);
750
751 disk->driverfs_dev = &sdev->sdev_gendev;
752 set_capacity(disk, cd->capacity);
753 disk->private_data = &cd->driver;
754 disk->queue = sdev->request_queue;
755 cd->cdi.disk = disk;
756
757 if (register_cdrom(&cd->cdi))
758 goto fail_put;
759
760 dev_set_drvdata(dev, cd);
761 disk->flags |= GENHD_FL_REMOVABLE;
762 add_disk(disk);
763
764 sdev_printk(KERN_DEBUG, sdev,
765 "Attached scsi CD-ROM %s\n", cd->cdi.name);
766 scsi_autopm_put_device(cd->device);
767
768 return 0;
769
770fail_put:
771 put_disk(disk);
772fail_free:
773 kfree(cd);
774fail:
775 scsi_autopm_put_device(sdev);
776 return error;
777}
778
779
780static void get_sectorsize(struct scsi_cd *cd)
781{
782 unsigned char cmd[10];
783 unsigned char buffer[8];
784 int the_result, retries = 3;
785 int sector_size;
786 struct request_queue *queue;
787
788 do {
789 cmd[0] = READ_CAPACITY;
790 memset((void *) &cmd[1], 0, 9);
791 memset(buffer, 0, sizeof(buffer));
792
793
794 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
795 buffer, sizeof(buffer), NULL,
796 SR_TIMEOUT, MAX_RETRIES, NULL);
797
798 retries--;
799
800 } while (the_result && retries);
801
802
803 if (the_result) {
804 cd->capacity = 0x1fffff;
805 sector_size = 2048;
806 } else {
807 long last_written;
808
809 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
810 (buffer[2] << 8) | buffer[3]);
811
812
813
814
815
816
817
818 if (!cdrom_get_last_written(&cd->cdi, &last_written))
819 cd->capacity = max_t(long, cd->capacity, last_written);
820
821 sector_size = (buffer[4] << 24) |
822 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
823 switch (sector_size) {
824
825
826
827
828
829
830 case 0:
831 case 2340:
832 case 2352:
833 sector_size = 2048;
834
835 case 2048:
836 cd->capacity *= 4;
837
838 case 512:
839 break;
840 default:
841 sr_printk(KERN_INFO, cd,
842 "unsupported sector size %d.", sector_size);
843 cd->capacity = 0;
844 }
845
846 cd->device->sector_size = sector_size;
847
848
849
850
851
852 set_capacity(cd->disk, cd->capacity);
853 }
854
855 queue = cd->device->request_queue;
856 blk_queue_logical_block_size(queue, sector_size);
857
858 return;
859}
860
861static void get_capabilities(struct scsi_cd *cd)
862{
863 unsigned char *buffer;
864 struct scsi_mode_data data;
865 struct scsi_sense_hdr sshdr;
866 int rc, n;
867
868 static const char *loadmech[] =
869 {
870 "caddy",
871 "tray",
872 "pop-up",
873 "",
874 "changer",
875 "cartridge changer",
876 "",
877 ""
878 };
879
880
881
882 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
883 if (!buffer) {
884 sr_printk(KERN_ERR, cd, "out of memory.\n");
885 return;
886 }
887
888
889 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
890
891
892 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
893 SR_TIMEOUT, 3, &data, NULL);
894
895 if (!scsi_status_is_good(rc)) {
896
897 cd->cdi.speed = 1;
898 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
899 CDC_DVD | CDC_DVD_RAM |
900 CDC_SELECT_DISC | CDC_SELECT_SPEED |
901 CDC_MRW | CDC_MRW_W | CDC_RAM);
902 kfree(buffer);
903 sr_printk(KERN_INFO, cd, "scsi-1 drive");
904 return;
905 }
906
907 n = data.header_length + data.block_descriptor_length;
908 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
909 cd->readcd_known = 1;
910 cd->readcd_cdda = buffer[n + 5] & 0x01;
911
912 sr_printk(KERN_INFO, cd,
913 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
914 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
915 cd->cdi.speed,
916 buffer[n + 3] & 0x01 ? "writer " : "",
917 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
918 buffer[n + 2] & 0x02 ? "cd/rw " : "",
919 buffer[n + 4] & 0x20 ? "xa/form2 " : "",
920 buffer[n + 5] & 0x01 ? "cdda " : "",
921 loadmech[buffer[n + 6] >> 5]);
922 if ((buffer[n + 6] >> 5) == 0)
923
924 cd->cdi.mask |= CDC_CLOSE_TRAY;
925 if ((buffer[n + 2] & 0x8) == 0)
926
927 cd->cdi.mask |= CDC_DVD;
928 if ((buffer[n + 3] & 0x20) == 0)
929
930 cd->cdi.mask |= CDC_DVD_RAM;
931 if ((buffer[n + 3] & 0x10) == 0)
932
933 cd->cdi.mask |= CDC_DVD_R;
934 if ((buffer[n + 3] & 0x2) == 0)
935
936 cd->cdi.mask |= CDC_CD_RW;
937 if ((buffer[n + 3] & 0x1) == 0)
938
939 cd->cdi.mask |= CDC_CD_R;
940 if ((buffer[n + 6] & 0x8) == 0)
941
942 cd->cdi.mask |= CDC_OPEN_TRAY;
943
944 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
945 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
946 cd->cdi.capacity =
947 cdrom_number_of_slots(&cd->cdi);
948 if (cd->cdi.capacity <= 1)
949
950 cd->cdi.mask |= CDC_SELECT_DISC;
951
952
953
954
955
956
957 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
958 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
959 cd->device->writeable = 1;
960 }
961
962 kfree(buffer);
963}
964
965
966
967
968
969static int sr_packet(struct cdrom_device_info *cdi,
970 struct packet_command *cgc)
971{
972 struct scsi_cd *cd = cdi->handle;
973 struct scsi_device *sdev = cd->device;
974
975 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
976 return -EDRIVE_CANT_DO_THIS;
977
978 if (cgc->timeout <= 0)
979 cgc->timeout = IOCTL_TIMEOUT;
980
981 sr_do_ioctl(cd, cgc);
982
983 return cgc->stat;
984}
985
986
987
988
989
990
991
992
993
994
995static void sr_kref_release(struct kref *kref)
996{
997 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
998 struct gendisk *disk = cd->disk;
999
1000 spin_lock(&sr_index_lock);
1001 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
1002 spin_unlock(&sr_index_lock);
1003
1004 unregister_cdrom(&cd->cdi);
1005
1006 disk->private_data = NULL;
1007
1008 put_disk(disk);
1009
1010 kfree(cd);
1011}
1012
1013static int sr_remove(struct device *dev)
1014{
1015 struct scsi_cd *cd = dev_get_drvdata(dev);
1016
1017 scsi_autopm_get_device(cd->device);
1018
1019 del_gendisk(cd->disk);
1020 dev_set_drvdata(dev, NULL);
1021
1022 mutex_lock(&sr_ref_mutex);
1023 kref_put(&cd->kref, sr_kref_release);
1024 mutex_unlock(&sr_ref_mutex);
1025
1026 return 0;
1027}
1028
1029static int __init init_sr(void)
1030{
1031 int rc;
1032
1033 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1034 if (rc)
1035 return rc;
1036 rc = scsi_register_driver(&sr_template.gendrv);
1037 if (rc)
1038 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1039
1040 return rc;
1041}
1042
1043static void __exit exit_sr(void)
1044{
1045 scsi_unregister_driver(&sr_template.gendrv);
1046 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1047}
1048
1049module_init(init_sr);
1050module_exit(exit_sr);
1051MODULE_LICENSE("GPL");
1052