22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/slab.h>
31 #include <linux/cdrom.h>
36 #include <linux/device.h>
38 #include <linux/wait.h>
41 #include <scsi/scsi.h>
44 #include <asm/delay.h>
48 #define GDROM_DEV_NAME "gdrom"
49 #define GD_SESSION_OFFSET 150
52 #define GDROM_COM_SOFTRESET 0x08
53 #define GDROM_COM_EXECDIAG 0x90
54 #define GDROM_COM_PACKET 0xA0
55 #define GDROM_COM_IDDEV 0xA1
58 #define GDROM_BASE_REG 0xA05F7000
59 #define GDROM_ALTSTATUS_REG (GDROM_BASE_REG + 0x18)
60 #define GDROM_DATA_REG (GDROM_BASE_REG + 0x80)
61 #define GDROM_ERROR_REG (GDROM_BASE_REG + 0x84)
62 #define GDROM_INTSEC_REG (GDROM_BASE_REG + 0x88)
63 #define GDROM_SECNUM_REG (GDROM_BASE_REG + 0x8C)
64 #define GDROM_BCL_REG (GDROM_BASE_REG + 0x90)
65 #define GDROM_BCH_REG (GDROM_BASE_REG + 0x94)
66 #define GDROM_DSEL_REG (GDROM_BASE_REG + 0x98)
67 #define GDROM_STATUSCOMMAND_REG (GDROM_BASE_REG + 0x9C)
68 #define GDROM_RESET_REG (GDROM_BASE_REG + 0x4E4)
70 #define GDROM_DMA_STARTADDR_REG (GDROM_BASE_REG + 0x404)
71 #define GDROM_DMA_LENGTH_REG (GDROM_BASE_REG + 0x408)
72 #define GDROM_DMA_DIRECTION_REG (GDROM_BASE_REG + 0x40C)
73 #define GDROM_DMA_ENABLE_REG (GDROM_BASE_REG + 0x414)
74 #define GDROM_DMA_STATUS_REG (GDROM_BASE_REG + 0x418)
75 #define GDROM_DMA_WAIT_REG (GDROM_BASE_REG + 0x4A0)
76 #define GDROM_DMA_ACCESS_CTRL_REG (GDROM_BASE_REG + 0x4B8)
78 #define GDROM_HARD_SECTOR 2048
79 #define BLOCK_LAYER_SECTOR 512
82 #define GDROM_DEFAULT_TIMEOUT (HZ * 7)
95 {
UNIT_ATTENTION,
"Device needs attention - disk may have been changed"},
101 static int gdrom_major;
116 static struct gdrom_unit {
117 struct gendisk *disk;
138 static int gdrom_getsense(
short *bufstring);
143 static bool gdrom_is_busy(
void)
148 static bool gdrom_data_request(
void)
153 static bool gdrom_wait_clrbusy(
void)
162 static bool gdrom_wait_busy_sleeps(
void)
164 unsigned long timeout;
167 while (!gdrom_is_busy() &&
time_before(jiffies, timeout))
170 return gdrom_wait_clrbusy();
173 static void gdrom_identifydevice(
void *
buf)
180 if (!gdrom_wait_clrbusy()) {
181 gdrom_getsense(
NULL);
185 if (!gdrom_wait_busy_sleeps()) {
186 gdrom_getsense(
NULL);
190 for (c = 0; c < 40; c++)
194 static void gdrom_spicommand(
void *spi_string,
int buflen)
196 short *
cmd = spi_string;
197 unsigned long timeout;
209 if (!gdrom_wait_clrbusy()) {
210 gdrom_getsense(
NULL);
215 while (!gdrom_data_request() &&
time_before(jiffies, timeout))
218 gdrom_getsense(
NULL);
231 static char gdrom_execute_diagnostic(
void)
233 gdrom_hardreset(gd.cd_info);
234 if (!gdrom_wait_clrbusy())
237 if (!gdrom_wait_busy_sleeps())
247 static int gdrom_preparedisk_cmd(
void)
253 spin_command->
cmd[0] = 0x70;
254 spin_command->
cmd[2] = 0x1f;
257 gdrom_packetcommand(gd.cd_info, spin_command);
260 GDROM_DEFAULT_TIMEOUT);
263 if (gd.status & 0x01) {
265 gdrom_getsense(
NULL);
278 static int gdrom_readtoc_cmd(
struct gdromtoc *toc,
int session)
288 toc_command->
cmd[0] = 0x14;
289 toc_command->
cmd[1] = session;
290 toc_command->
cmd[3] = tocsize >> 8;
291 toc_command->
cmd[4] = tocsize & 0xff;
292 toc_command->
buflen = tocsize;
295 goto cleanup_readtoc_final;
298 gdrom_packetcommand(gd.cd_info, toc_command);
300 GDROM_DEFAULT_TIMEOUT);
303 goto cleanup_readtoc;
306 if (gd.status & 0x01)
311 cleanup_readtoc_final:
317 static int get_entry_lba(
int track)
322 static int get_entry_q_ctrl(
int track)
324 return (track & 0x000000f0) >> 4;
327 static int get_entry_track(
int track)
329 return (track & 0x0000ff00) >> 8;
340 err = gdrom_readtoc_cmd(gd.toc, 1);
344 err = gdrom_readtoc_cmd(gd.toc, 0);
346 pr_info(
"Could not get CD table of contents\n");
351 fentry = get_entry_track(gd.toc->first);
352 lentry = get_entry_track(gd.toc->last);
354 track = get_entry_track(gd.toc->last);
356 data = gd.toc->entry[track - 1];
357 if (get_entry_q_ctrl(data))
360 }
while (track >= fentry);
362 if ((track > 100) || (track < get_entry_track(gd.toc->first))) {
363 pr_info(
"No data on the last session of the CD\n");
364 gdrom_getsense(
NULL);
369 ms_info->
addr.
lba = get_entry_lba(data);
377 return gdrom_preparedisk_cmd();
399 unsigned int clearing,
int ignore)
403 DISK_EVENT_MEDIA_CHANGE : 0;
411 for (count = 0xa0000000; count < 0xa0200000; count += 4)
421 gdrom_spicommand(&command->
cmd, command->
buflen);
431 static int gdrom_getsense(
short *bufstring)
441 sense_command->
cmd[0] = 0x13;
442 sense_command->
cmd[4] = 10;
443 sense_command->
buflen = 10;
446 if (gd.pending && !gdrom_wait_clrbusy()) {
448 goto cleanup_sense_final;
451 gdrom_packetcommand(gd.cd_info, sense_command);
453 GDROM_DEFAULT_TIMEOUT);
458 pr_info(
"Drive not ready - command aborted\n");
461 sense_key = sense[1] & 0x0F;
465 pr_err(
"Unknown sense key: %d\n", sense_key);
467 memcpy(bufstring, &sense[4], 2);
474 kfree(sense_command);
486 .release = gdrom_release,
487 .drive_status = gdrom_drivestatus,
488 .check_events = gdrom_check_events,
489 .get_last_session = gdrom_get_last_session,
490 .reset = gdrom_hardreset,
491 .audio_ioctl = gdrom_audio_ioctl,
506 static int gdrom_bdops_release(
struct gendisk *disk,
fmode_t mode)
514 static unsigned int gdrom_bdops_check_events(
struct gendisk *disk,
515 unsigned int clearing)
521 unsigned cmd,
unsigned long arg)
526 ret =
cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
532 static const struct block_device_operations gdrom_bdops = {
534 .open = gdrom_bdops_open,
535 .release = gdrom_bdops_release,
536 .check_events = gdrom_bdops_check_events,
537 .ioctl = gdrom_bdops_ioctl,
550 static irqreturn_t gdrom_dma_interrupt(
int irq,
void *dev_id)
553 if (gd.transfer != 1)
560 static int __devinit gdrom_set_interrupt_handlers(
void)
591 unsigned long timeout;
593 if (list_empty(&gdrom_deferred))
598 read_command->
cmd[0] = 0x30;
599 read_command->
cmd[1] = 0x20;
600 spin_lock(&gdrom_lock);
603 spin_unlock(&gdrom_lock);
605 block_cnt = blk_rq_sectors(req)/
GD_TO_BLK;
610 read_command->
cmd[2] = (block >> 16) & 0xFF;
611 read_command->
cmd[3] = (block >> 8) & 0xFF;
612 read_command->
cmd[4] = block & 0xFF;
613 read_command->
cmd[8] = (block_cnt >> 16) & 0xFF;
614 read_command->
cmd[9] = (block_cnt >> 8) & 0xFF;
615 read_command->
cmd[10] = block_cnt & 0xFF;
626 while (gdrom_is_busy() &&
time_before(jiffies, timeout))
631 while (gdrom_is_busy() &&
time_before(jiffies, timeout))
644 gd.transfer == 0, GDROM_DEFAULT_TIMEOUT);
645 err = gd.transfer ? -
EIO : 0;
650 spin_lock(&gdrom_lock);
651 list_del_init(&req->queuelist);
654 spin_unlock(&gdrom_lock);
663 if (req->cmd_type != REQ_TYPE_FS) {
668 if (rq_data_dir(req) !=
READ) {
669 pr_notice(
"Read only device - write request ignored\n");
684 static int __devinit gdrom_outputversion(
void)
687 char *model_name, *manuf_name, *firmw_ver;
694 gdrom_identifydevice(
id);
700 goto free_model_name;
703 goto free_manuf_name;
704 pr_info(
"%s from %s with firmware %s\n",
705 model_name, manuf_name, firmw_ver);
718 static int __devinit gdrom_init_dma_mode(
void)
722 if (!gdrom_wait_clrbusy())
725 if (!gdrom_wait_busy_sleeps())
739 static void __devinit probe_gdrom_setupcd(
void)
741 gd.cd_info->ops = &gdrom_ops;
742 gd.cd_info->capacity = 1;
748 static void __devinit probe_gdrom_setupdisk(
void)
750 gd.disk->major = gdrom_major;
751 gd.disk->first_minor = 1;
756 static int __devinit probe_gdrom_setupqueue(
void)
763 gd.disk->queue = gd.gdrom_rq;
764 return gdrom_init_dma_mode();
775 if (gdrom_execute_diagnostic() != 1) {
780 if (gdrom_outputversion())
784 if (gdrom_major <= 0)
786 pr_info(
"Registered with major number %d\n",
792 goto probe_fail_no_mem;
794 probe_gdrom_setupcd();
798 goto probe_fail_no_disk;
800 probe_gdrom_setupdisk();
803 goto probe_fail_cdrom_register;
805 gd.disk->fops = &gdrom_bdops;
807 err = gdrom_set_interrupt_handlers();
809 goto probe_fail_cmdirq_register;
812 goto probe_fail_requestq;
814 err = probe_gdrom_setupqueue();
829 probe_fail_cmdirq_register:
830 probe_fail_cdrom_register:
837 pr_warning(
"Probe failed - error is 0x%X\n", err);
856 .probe = probe_gdrom,
863 static int __init init_gdrom(
void)
878 static void __exit exit_gdrom(
void)