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 mutex_lock(&sr_mutex);
531 cd = scsi_cd_get(bdev->bd_disk);
532 if (cd) {
533 ret = cdrom_open(&cd->cdi, bdev, mode);
534 if (ret)
535 scsi_cd_put(cd);
536 }
537 mutex_unlock(&sr_mutex);
538 return ret;
539}
540
541static void sr_block_release(struct gendisk *disk, fmode_t mode)
542{
543 struct scsi_cd *cd = scsi_cd(disk);
544 mutex_lock(&sr_mutex);
545 cdrom_release(&cd->cdi, mode);
546 scsi_cd_put(cd);
547 mutex_unlock(&sr_mutex);
548}
549
550static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
551 unsigned long arg)
552{
553 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
554 struct scsi_device *sdev = cd->device;
555 void __user *argp = (void __user *)arg;
556 int ret;
557
558 scsi_autopm_get_device(cd->device);
559
560 mutex_lock(&sr_mutex);
561
562
563
564
565
566 switch (cmd) {
567 case SCSI_IOCTL_GET_IDLUN:
568 case SCSI_IOCTL_GET_BUS_NUMBER:
569 ret = scsi_ioctl(sdev, cmd, argp);
570 goto out;
571 }
572
573 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
574 if (ret != -ENOSYS)
575 goto out;
576
577
578
579
580
581
582
583 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
584 (mode & FMODE_NDELAY) != 0);
585 if (ret != -ENODEV)
586 goto out;
587 ret = scsi_ioctl(sdev, cmd, argp);
588
589out:
590 mutex_unlock(&sr_mutex);
591 scsi_autopm_put_device(cd->device);
592 return ret;
593}
594
595static unsigned int sr_block_check_events(struct gendisk *disk,
596 unsigned int clearing)
597{
598 struct scsi_cd *cd = scsi_cd(disk);
599 unsigned int ret;
600
601 if (atomic_read(&cd->device->disk_events_disable_depth) == 0) {
602 scsi_autopm_get_device(cd->device);
603 ret = cdrom_check_events(&cd->cdi, clearing);
604 scsi_autopm_put_device(cd->device);
605 } else {
606 ret = 0;
607 }
608
609 return ret;
610}
611
612static int sr_block_revalidate_disk(struct gendisk *disk)
613{
614 struct scsi_cd *cd = scsi_cd(disk);
615 struct scsi_sense_hdr sshdr;
616
617 scsi_autopm_get_device(cd->device);
618
619
620 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
621 goto out;
622
623 sr_cd_check(&cd->cdi);
624 get_sectorsize(cd);
625out:
626 scsi_autopm_put_device(cd->device);
627 return 0;
628}
629
630static const struct block_device_operations sr_bdops =
631{
632 .owner = THIS_MODULE,
633 .open = sr_block_open,
634 .release = sr_block_release,
635 .ioctl = sr_block_ioctl,
636 .check_events = sr_block_check_events,
637 .revalidate_disk = sr_block_revalidate_disk,
638
639
640
641
642};
643
644static int sr_open(struct cdrom_device_info *cdi, int purpose)
645{
646 struct scsi_cd *cd = cdi->handle;
647 struct scsi_device *sdev = cd->device;
648 int retval;
649
650
651
652
653
654 retval = -ENXIO;
655 if (!scsi_block_when_processing_errors(sdev))
656 goto error_out;
657
658 return 0;
659
660error_out:
661 return retval;
662}
663
664static void sr_release(struct cdrom_device_info *cdi)
665{
666 struct scsi_cd *cd = cdi->handle;
667
668 if (cd->device->sector_size > 2048)
669 sr_set_blocklength(cd, 2048);
670
671}
672
673static int sr_probe(struct device *dev)
674{
675 struct scsi_device *sdev = to_scsi_device(dev);
676 struct gendisk *disk;
677 struct scsi_cd *cd;
678 int minor, error;
679
680 error = -ENODEV;
681 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
682 goto fail;
683
684 error = -ENOMEM;
685 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
686 if (!cd)
687 goto fail;
688
689 kref_init(&cd->kref);
690
691 disk = alloc_disk(1);
692 if (!disk)
693 goto fail_free;
694
695 spin_lock(&sr_index_lock);
696 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
697 if (minor == SR_DISKS) {
698 spin_unlock(&sr_index_lock);
699 error = -EBUSY;
700 goto fail_put;
701 }
702 __set_bit(minor, sr_index_bits);
703 spin_unlock(&sr_index_lock);
704
705 disk->major = SCSI_CDROM_MAJOR;
706 disk->first_minor = minor;
707 sprintf(disk->disk_name, "sr%d", minor);
708 disk->fops = &sr_bdops;
709 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
710 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
711
712 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
713
714 cd->device = sdev;
715 cd->disk = disk;
716 cd->driver = &sr_template;
717 cd->disk = disk;
718 cd->capacity = 0x1fffff;
719 cd->device->changed = 1;
720 cd->media_present = 1;
721 cd->use = 1;
722 cd->readcd_known = 0;
723 cd->readcd_cdda = 0;
724
725 cd->cdi.ops = &sr_dops;
726 cd->cdi.handle = cd;
727 cd->cdi.mask = 0;
728 cd->cdi.capacity = 1;
729 sprintf(cd->cdi.name, "sr%d", minor);
730
731 sdev->sector_size = 2048;
732
733
734 get_capabilities(cd);
735 sr_vendor_init(cd);
736
737 disk->driverfs_dev = &sdev->sdev_gendev;
738 set_capacity(disk, cd->capacity);
739 disk->private_data = &cd->driver;
740 disk->queue = sdev->request_queue;
741 cd->cdi.disk = disk;
742
743 if (register_cdrom(&cd->cdi))
744 goto fail_put;
745
746 dev_set_drvdata(dev, cd);
747 disk->flags |= GENHD_FL_REMOVABLE;
748 add_disk(disk);
749
750 sdev_printk(KERN_DEBUG, sdev,
751 "Attached scsi CD-ROM %s\n", cd->cdi.name);
752 scsi_autopm_put_device(cd->device);
753
754 return 0;
755
756fail_put:
757 put_disk(disk);
758fail_free:
759 kfree(cd);
760fail:
761 return error;
762}
763
764
765static void get_sectorsize(struct scsi_cd *cd)
766{
767 unsigned char cmd[10];
768 unsigned char buffer[8];
769 int the_result, retries = 3;
770 int sector_size;
771 struct request_queue *queue;
772
773 do {
774 cmd[0] = READ_CAPACITY;
775 memset((void *) &cmd[1], 0, 9);
776 memset(buffer, 0, sizeof(buffer));
777
778
779 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
780 buffer, sizeof(buffer), NULL,
781 SR_TIMEOUT, MAX_RETRIES, NULL);
782
783 retries--;
784
785 } while (the_result && retries);
786
787
788 if (the_result) {
789 cd->capacity = 0x1fffff;
790 sector_size = 2048;
791 } else {
792 long last_written;
793
794 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
795 (buffer[2] << 8) | buffer[3]);
796
797
798
799
800
801
802
803 if (!cdrom_get_last_written(&cd->cdi, &last_written))
804 cd->capacity = max_t(long, cd->capacity, last_written);
805
806 sector_size = (buffer[4] << 24) |
807 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
808 switch (sector_size) {
809
810
811
812
813
814
815 case 0:
816 case 2340:
817 case 2352:
818 sector_size = 2048;
819
820 case 2048:
821 cd->capacity *= 4;
822
823 case 512:
824 break;
825 default:
826 sr_printk(KERN_INFO, cd,
827 "unsupported sector size %d.", sector_size);
828 cd->capacity = 0;
829 }
830
831 cd->device->sector_size = sector_size;
832
833
834
835
836
837 set_capacity(cd->disk, cd->capacity);
838 }
839
840 queue = cd->device->request_queue;
841 blk_queue_logical_block_size(queue, sector_size);
842
843 return;
844}
845
846static void get_capabilities(struct scsi_cd *cd)
847{
848 unsigned char *buffer;
849 struct scsi_mode_data data;
850 struct scsi_sense_hdr sshdr;
851 int rc, n;
852
853 static const char *loadmech[] =
854 {
855 "caddy",
856 "tray",
857 "pop-up",
858 "",
859 "changer",
860 "cartridge changer",
861 "",
862 ""
863 };
864
865
866
867 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
868 if (!buffer) {
869 sr_printk(KERN_ERR, cd, "out of memory.\n");
870 return;
871 }
872
873
874 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
875
876
877 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
878 SR_TIMEOUT, 3, &data, NULL);
879
880 if (!scsi_status_is_good(rc)) {
881
882 cd->cdi.speed = 1;
883 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
884 CDC_DVD | CDC_DVD_RAM |
885 CDC_SELECT_DISC | CDC_SELECT_SPEED |
886 CDC_MRW | CDC_MRW_W | CDC_RAM);
887 kfree(buffer);
888 sr_printk(KERN_INFO, cd, "scsi-1 drive");
889 return;
890 }
891
892 n = data.header_length + data.block_descriptor_length;
893 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
894 cd->readcd_known = 1;
895 cd->readcd_cdda = buffer[n + 5] & 0x01;
896
897 sr_printk(KERN_INFO, cd,
898 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
899 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
900 cd->cdi.speed,
901 buffer[n + 3] & 0x01 ? "writer " : "",
902 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
903 buffer[n + 2] & 0x02 ? "cd/rw " : "",
904 buffer[n + 4] & 0x20 ? "xa/form2 " : "",
905 buffer[n + 5] & 0x01 ? "cdda " : "",
906 loadmech[buffer[n + 6] >> 5]);
907 if ((buffer[n + 6] >> 5) == 0)
908
909 cd->cdi.mask |= CDC_CLOSE_TRAY;
910 if ((buffer[n + 2] & 0x8) == 0)
911
912 cd->cdi.mask |= CDC_DVD;
913 if ((buffer[n + 3] & 0x20) == 0)
914
915 cd->cdi.mask |= CDC_DVD_RAM;
916 if ((buffer[n + 3] & 0x10) == 0)
917
918 cd->cdi.mask |= CDC_DVD_R;
919 if ((buffer[n + 3] & 0x2) == 0)
920
921 cd->cdi.mask |= CDC_CD_RW;
922 if ((buffer[n + 3] & 0x1) == 0)
923
924 cd->cdi.mask |= CDC_CD_R;
925 if ((buffer[n + 6] & 0x8) == 0)
926
927 cd->cdi.mask |= CDC_OPEN_TRAY;
928
929 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
930 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
931 cd->cdi.capacity =
932 cdrom_number_of_slots(&cd->cdi);
933 if (cd->cdi.capacity <= 1)
934
935 cd->cdi.mask |= CDC_SELECT_DISC;
936
937
938
939
940
941
942 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
943 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
944 cd->device->writeable = 1;
945 }
946
947 kfree(buffer);
948}
949
950
951
952
953
954static int sr_packet(struct cdrom_device_info *cdi,
955 struct packet_command *cgc)
956{
957 struct scsi_cd *cd = cdi->handle;
958 struct scsi_device *sdev = cd->device;
959
960 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
961 return -EDRIVE_CANT_DO_THIS;
962
963 if (cgc->timeout <= 0)
964 cgc->timeout = IOCTL_TIMEOUT;
965
966 sr_do_ioctl(cd, cgc);
967
968 return cgc->stat;
969}
970
971
972
973
974
975
976
977
978
979
980static void sr_kref_release(struct kref *kref)
981{
982 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
983 struct gendisk *disk = cd->disk;
984
985 spin_lock(&sr_index_lock);
986 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
987 spin_unlock(&sr_index_lock);
988
989 unregister_cdrom(&cd->cdi);
990
991 disk->private_data = NULL;
992
993 put_disk(disk);
994
995 kfree(cd);
996}
997
998static int sr_remove(struct device *dev)
999{
1000 struct scsi_cd *cd = dev_get_drvdata(dev);
1001
1002 scsi_autopm_get_device(cd->device);
1003
1004 del_gendisk(cd->disk);
1005 dev_set_drvdata(dev, NULL);
1006
1007 mutex_lock(&sr_ref_mutex);
1008 kref_put(&cd->kref, sr_kref_release);
1009 mutex_unlock(&sr_ref_mutex);
1010
1011 return 0;
1012}
1013
1014static int __init init_sr(void)
1015{
1016 int rc;
1017
1018 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1019 if (rc)
1020 return rc;
1021 rc = scsi_register_driver(&sr_template.gendrv);
1022 if (rc)
1023 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1024
1025 return rc;
1026}
1027
1028static void __exit exit_sr(void)
1029{
1030 scsi_unregister_driver(&sr_template.gendrv);
1031 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1032}
1033
1034module_init(init_sr);
1035module_exit(exit_sr);
1036MODULE_LICENSE("GPL");
1037