Merge branch 'bug-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/josef/btrfs...
[pandora-kernel.git] / drivers / cdrom / gdrom.c
1 /* GD ROM driver for the SEGA Dreamcast
2  * copyright Adrian McMenamin, 2007
3  * With thanks to Marcus Comstedt and Nathan Keynes
4  * for work in reversing PIO and DMA
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/slab.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/cdrom.h>
32 #include <linux/genhd.h>
33 #include <linux/bio.h>
34 #include <linux/blkdev.h>
35 #include <linux/interrupt.h>
36 #include <linux/device.h>
37 #include <linux/smp_lock.h>
38 #include <linux/wait.h>
39 #include <linux/workqueue.h>
40 #include <linux/platform_device.h>
41 #include <scsi/scsi.h>
42 #include <asm/io.h>
43 #include <asm/dma.h>
44 #include <asm/delay.h>
45 #include <mach/dma.h>
46 #include <mach/sysasic.h>
47
48 #define GDROM_DEV_NAME "gdrom"
49 #define GD_SESSION_OFFSET 150
50
51 /* GD Rom commands */
52 #define GDROM_COM_SOFTRESET 0x08
53 #define GDROM_COM_EXECDIAG 0x90
54 #define GDROM_COM_PACKET 0xA0
55 #define GDROM_COM_IDDEV 0xA1
56
57 /* GD Rom registers */
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)
69
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)
77
78 #define GDROM_HARD_SECTOR       2048
79 #define BLOCK_LAYER_SECTOR      512
80 #define GD_TO_BLK               4
81
82 #define GDROM_DEFAULT_TIMEOUT   (HZ * 7)
83
84 static const struct {
85         int sense_key;
86         const char * const text;
87 } sense_texts[] = {
88         {NO_SENSE, "OK"},
89         {RECOVERED_ERROR, "Recovered from error"},
90         {NOT_READY, "Device not ready"},
91         {MEDIUM_ERROR, "Disk not ready"},
92         {HARDWARE_ERROR, "Hardware error"},
93         {ILLEGAL_REQUEST, "Command has failed"},
94         {UNIT_ATTENTION, "Device needs attention - disk may have been changed"},
95         {DATA_PROTECT, "Data protection error"},
96         {ABORTED_COMMAND, "Command aborted"},
97 };
98
99 static struct platform_device *pd;
100 static int gdrom_major;
101 static DECLARE_WAIT_QUEUE_HEAD(command_queue);
102 static DECLARE_WAIT_QUEUE_HEAD(request_queue);
103
104 static DEFINE_SPINLOCK(gdrom_lock);
105 static void gdrom_readdisk_dma(struct work_struct *work);
106 static DECLARE_WORK(work, gdrom_readdisk_dma);
107 static LIST_HEAD(gdrom_deferred);
108
109 struct gdromtoc {
110         unsigned int entry[99];
111         unsigned int first, last;
112         unsigned int leadout;
113 };
114
115 static struct gdrom_unit {
116         struct gendisk *disk;
117         struct cdrom_device_info *cd_info;
118         int status;
119         int pending;
120         int transfer;
121         char disk_type;
122         struct gdromtoc *toc;
123         struct request_queue *gdrom_rq;
124 } gd;
125
126 struct gdrom_id {
127         char mid;
128         char modid;
129         char verid;
130         char padA[13];
131         char mname[16];
132         char modname[16];
133         char firmver[16];
134         char padB[16];
135 };
136
137 static int gdrom_getsense(short *bufstring);
138 static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
139         struct packet_command *command);
140 static int gdrom_hardreset(struct cdrom_device_info *cd_info);
141
142 static bool gdrom_is_busy(void)
143 {
144         return (ctrl_inb(GDROM_ALTSTATUS_REG) & 0x80) != 0;
145 }
146
147 static bool gdrom_data_request(void)
148 {
149         return (ctrl_inb(GDROM_ALTSTATUS_REG) & 0x88) == 8;
150 }
151
152 static bool gdrom_wait_clrbusy(void)
153 {
154         unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
155         while ((ctrl_inb(GDROM_ALTSTATUS_REG) & 0x80) &&
156                 (time_before(jiffies, timeout)))
157                 cpu_relax();
158         return time_before(jiffies, timeout + 1);
159 }
160
161 static bool gdrom_wait_busy_sleeps(void)
162 {
163         unsigned long timeout;
164         /* Wait to get busy first */
165         timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
166         while (!gdrom_is_busy() && time_before(jiffies, timeout))
167                 cpu_relax();
168         /* Now wait for busy to clear */
169         return gdrom_wait_clrbusy();
170 }
171
172 static void gdrom_identifydevice(void *buf)
173 {
174         int c;
175         short *data = buf;
176         /* If the device won't clear it has probably
177         * been hit by a serious failure - but we'll
178         * try to return a sense key even so */
179         if (!gdrom_wait_clrbusy()) {
180                 gdrom_getsense(NULL);
181                 return;
182         }
183         ctrl_outb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG);
184         if (!gdrom_wait_busy_sleeps()) {
185                 gdrom_getsense(NULL);
186                 return;
187         }
188         /* now read in the data */
189         for (c = 0; c < 40; c++)
190                 data[c] = ctrl_inw(GDROM_DATA_REG);
191 }
192
193 static void gdrom_spicommand(void *spi_string, int buflen)
194 {
195         short *cmd = spi_string;
196         unsigned long timeout;
197
198         /* ensure IRQ_WAIT is set */
199         ctrl_outb(0x08, GDROM_ALTSTATUS_REG);
200         /* specify how many bytes we expect back */
201         ctrl_outb(buflen & 0xFF, GDROM_BCL_REG);
202         ctrl_outb((buflen >> 8) & 0xFF, GDROM_BCH_REG);
203         /* other parameters */
204         ctrl_outb(0, GDROM_INTSEC_REG);
205         ctrl_outb(0, GDROM_SECNUM_REG);
206         ctrl_outb(0, GDROM_ERROR_REG);
207         /* Wait until we can go */
208         if (!gdrom_wait_clrbusy()) {
209                 gdrom_getsense(NULL);
210                 return;
211         }
212         timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
213         ctrl_outb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
214         while (!gdrom_data_request() && time_before(jiffies, timeout))
215                 cpu_relax();
216         if (!time_before(jiffies, timeout + 1)) {
217                 gdrom_getsense(NULL);
218                 return;
219         }
220         outsw(GDROM_DATA_REG, cmd, 6);
221 }
222
223
224 /* gdrom_command_executediagnostic:
225  * Used to probe for presence of working GDROM
226  * Restarts GDROM device and then applies standard ATA 3
227  * Execute Diagnostic Command: a return of '1' indicates device 0
228  * present and device 1 absent
229  */
230 static char gdrom_execute_diagnostic(void)
231 {
232         gdrom_hardreset(gd.cd_info);
233         if (!gdrom_wait_clrbusy())
234                 return 0;
235         ctrl_outb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG);
236         if (!gdrom_wait_busy_sleeps())
237                 return 0;
238         return ctrl_inb(GDROM_ERROR_REG);
239 }
240
241 /*
242  * Prepare disk command
243  * byte 0 = 0x70
244  * byte 1 = 0x1f
245  */
246 static int gdrom_preparedisk_cmd(void)
247 {
248         struct packet_command *spin_command;
249         spin_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
250         if (!spin_command)
251                 return -ENOMEM;
252         spin_command->cmd[0] = 0x70;
253         spin_command->cmd[2] = 0x1f;
254         spin_command->buflen = 0;
255         gd.pending = 1;
256         gdrom_packetcommand(gd.cd_info, spin_command);
257         /* 60 second timeout */
258         wait_event_interruptible_timeout(command_queue, gd.pending == 0,
259                 GDROM_DEFAULT_TIMEOUT);
260         gd.pending = 0;
261         kfree(spin_command);
262         if (gd.status & 0x01) {
263                 /* log an error */
264                 gdrom_getsense(NULL);
265                 return -EIO;
266         }
267         return 0;
268 }
269
270 /*
271  * Read TOC command
272  * byte 0 = 0x14
273  * byte 1 = session
274  * byte 3 = sizeof TOC >> 8  ie upper byte
275  * byte 4 = sizeof TOC & 0xff ie lower byte
276  */
277 static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session)
278 {
279         int tocsize;
280         struct packet_command *toc_command;
281         int err = 0;
282
283         toc_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
284         if (!toc_command)
285                 return -ENOMEM;
286         tocsize = sizeof(struct gdromtoc);
287         toc_command->cmd[0] = 0x14;
288         toc_command->cmd[1] = session;
289         toc_command->cmd[3] = tocsize >> 8;
290         toc_command->cmd[4] = tocsize & 0xff;
291         toc_command->buflen = tocsize;
292         if (gd.pending) {
293                 err = -EBUSY;
294                 goto cleanup_readtoc_final;
295         }
296         gd.pending = 1;
297         gdrom_packetcommand(gd.cd_info, toc_command);
298         wait_event_interruptible_timeout(command_queue, gd.pending == 0,
299                 GDROM_DEFAULT_TIMEOUT);
300         if (gd.pending) {
301                 err = -EINVAL;
302                 goto cleanup_readtoc;
303         }
304         insw(GDROM_DATA_REG, toc, tocsize/2);
305         if (gd.status & 0x01)
306                 err = -EINVAL;
307
308 cleanup_readtoc:
309         gd.pending = 0;
310 cleanup_readtoc_final:
311         kfree(toc_command);
312         return err;
313 }
314
315 /* TOC helpers */
316 static int get_entry_lba(int track)
317 {
318         return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET);
319 }
320
321 static int get_entry_q_ctrl(int track)
322 {
323         return (track & 0x000000f0) >> 4;
324 }
325
326 static int get_entry_track(int track)
327 {
328         return (track & 0x0000ff00) >> 8;
329 }
330
331 static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
332         struct cdrom_multisession *ms_info)
333 {
334         int fentry, lentry, track, data, tocuse, err;
335         if (!gd.toc)
336                 return -ENOMEM;
337         tocuse = 1;
338         /* Check if GD-ROM */
339         err = gdrom_readtoc_cmd(gd.toc, 1);
340         /* Not a GD-ROM so check if standard CD-ROM */
341         if (err) {
342                 tocuse = 0;
343                 err = gdrom_readtoc_cmd(gd.toc, 0);
344                 if (err) {
345                         pr_info("Could not get CD table of contents\n");
346                         return -ENXIO;
347                 }
348         }
349
350         fentry = get_entry_track(gd.toc->first);
351         lentry = get_entry_track(gd.toc->last);
352         /* Find the first data track */
353         track = get_entry_track(gd.toc->last);
354         do {
355                 data = gd.toc->entry[track - 1];
356                 if (get_entry_q_ctrl(data))
357                         break;  /* ie a real data track */
358                 track--;
359         } while (track >= fentry);
360
361         if ((track > 100) || (track < get_entry_track(gd.toc->first))) {
362                 pr_info("No data on the last session of the CD\n");
363                 gdrom_getsense(NULL);
364                 return -ENXIO;
365         }
366
367         ms_info->addr_format = CDROM_LBA;
368         ms_info->addr.lba = get_entry_lba(data);
369         ms_info->xa_flag = 1;
370         return 0;
371 }
372
373 static int gdrom_open(struct cdrom_device_info *cd_info, int purpose)
374 {
375         /* spin up the disk */
376         return gdrom_preparedisk_cmd();
377 }
378
379 /* this function is required even if empty */
380 static void gdrom_release(struct cdrom_device_info *cd_info)
381 {
382 }
383
384 static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore)
385 {
386         /* read the sense key */
387         char sense = ctrl_inb(GDROM_ERROR_REG);
388         sense &= 0xF0;
389         if (sense == 0)
390                 return CDS_DISC_OK;
391         if (sense == 0x20)
392                 return CDS_DRIVE_NOT_READY;
393         /* default */
394         return CDS_NO_INFO;
395 }
396
397 static int gdrom_mediachanged(struct cdrom_device_info *cd_info, int ignore)
398 {
399         /* check the sense key */
400         return (ctrl_inb(GDROM_ERROR_REG) & 0xF0) == 0x60;
401 }
402
403 /* reset the G1 bus */
404 static int gdrom_hardreset(struct cdrom_device_info *cd_info)
405 {
406         int count;
407         ctrl_outl(0x1fffff, GDROM_RESET_REG);
408         for (count = 0xa0000000; count < 0xa0200000; count += 4)
409                 ctrl_inl(count);
410         return 0;
411 }
412
413 /* keep the function looking like the universal
414  * CD Rom specification  - returning int */
415 static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
416         struct packet_command *command)
417 {
418         gdrom_spicommand(&command->cmd, command->buflen);
419         return 0;
420 }
421
422 /* Get Sense SPI command
423  * From Marcus Comstedt
424  * cmd = 0x13
425  * cmd + 4 = length of returned buffer
426  * Returns 5 16 bit words
427  */
428 static int gdrom_getsense(short *bufstring)
429 {
430         struct packet_command *sense_command;
431         short sense[5];
432         int sense_key;
433         int err = -EIO;
434
435         sense_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
436         if (!sense_command)
437                 return -ENOMEM;
438         sense_command->cmd[0] = 0x13;
439         sense_command->cmd[4] = 10;
440         sense_command->buflen = 10;
441         /* even if something is pending try to get
442         * the sense key if possible */
443         if (gd.pending && !gdrom_wait_clrbusy()) {
444                 err = -EBUSY;
445                 goto cleanup_sense_final;
446         }
447         gd.pending = 1;
448         gdrom_packetcommand(gd.cd_info, sense_command);
449         wait_event_interruptible_timeout(command_queue, gd.pending == 0,
450                 GDROM_DEFAULT_TIMEOUT);
451         if (gd.pending)
452                 goto cleanup_sense;
453         insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);
454         if (sense[1] & 40) {
455                 pr_info("Drive not ready - command aborted\n");
456                 goto cleanup_sense;
457         }
458         sense_key = sense[1] & 0x0F;
459         if (sense_key < ARRAY_SIZE(sense_texts))
460                 pr_info("%s\n", sense_texts[sense_key].text);
461         else
462                 pr_err("Unknown sense key: %d\n", sense_key);
463         if (bufstring) /* return addional sense data */
464                 memcpy(bufstring, &sense[4], 2);
465         if (sense_key < 2)
466                 err = 0;
467
468 cleanup_sense:
469         gd.pending = 0;
470 cleanup_sense_final:
471         kfree(sense_command);
472         return err;
473 }
474
475 static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
476                              void *arg)
477 {
478         return -EINVAL;
479 }
480
481 static struct cdrom_device_ops gdrom_ops = {
482         .open                   = gdrom_open,
483         .release                = gdrom_release,
484         .drive_status           = gdrom_drivestatus,
485         .media_changed          = gdrom_mediachanged,
486         .get_last_session       = gdrom_get_last_session,
487         .reset                  = gdrom_hardreset,
488         .audio_ioctl            = gdrom_audio_ioctl,
489         .capability             = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
490                                   CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,
491         .n_minors               = 1,
492 };
493
494 static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
495 {
496         int ret;
497         lock_kernel();
498         ret = cdrom_open(gd.cd_info, bdev, mode);
499         unlock_kernel();
500         return ret;
501 }
502
503 static int gdrom_bdops_release(struct gendisk *disk, fmode_t mode)
504 {
505         lock_kernel();
506         cdrom_release(gd.cd_info, mode);
507         unlock_kernel();
508         return 0;
509 }
510
511 static int gdrom_bdops_mediachanged(struct gendisk *disk)
512 {
513         return cdrom_media_changed(gd.cd_info);
514 }
515
516 static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode,
517         unsigned cmd, unsigned long arg)
518 {
519         int ret;
520
521         lock_kernel();
522         ret = cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
523         unlock_kernel();
524
525         return ret;
526 }
527
528 static const struct block_device_operations gdrom_bdops = {
529         .owner                  = THIS_MODULE,
530         .open                   = gdrom_bdops_open,
531         .release                = gdrom_bdops_release,
532         .media_changed          = gdrom_bdops_mediachanged,
533         .ioctl                  = gdrom_bdops_ioctl,
534 };
535
536 static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id)
537 {
538         gd.status = ctrl_inb(GDROM_STATUSCOMMAND_REG);
539         if (gd.pending != 1)
540                 return IRQ_HANDLED;
541         gd.pending = 0;
542         wake_up_interruptible(&command_queue);
543         return IRQ_HANDLED;
544 }
545
546 static irqreturn_t gdrom_dma_interrupt(int irq, void *dev_id)
547 {
548         gd.status = ctrl_inb(GDROM_STATUSCOMMAND_REG);
549         if (gd.transfer != 1)
550                 return IRQ_HANDLED;
551         gd.transfer = 0;
552         wake_up_interruptible(&request_queue);
553         return IRQ_HANDLED;
554 }
555
556 static int __devinit gdrom_set_interrupt_handlers(void)
557 {
558         int err;
559
560         err = request_irq(HW_EVENT_GDROM_CMD, gdrom_command_interrupt,
561                 IRQF_DISABLED, "gdrom_command", &gd);
562         if (err)
563                 return err;
564         err = request_irq(HW_EVENT_GDROM_DMA, gdrom_dma_interrupt,
565                 IRQF_DISABLED, "gdrom_dma", &gd);
566         if (err)
567                 free_irq(HW_EVENT_GDROM_CMD, &gd);
568         return err;
569 }
570
571 /* Implement DMA read using SPI command
572  * 0 -> 0x30
573  * 1 -> mode
574  * 2 -> block >> 16
575  * 3 -> block >> 8
576  * 4 -> block
577  * 8 -> sectors >> 16
578  * 9 -> sectors >> 8
579  * 10 -> sectors
580  */
581 static void gdrom_readdisk_dma(struct work_struct *work)
582 {
583         int err, block, block_cnt;
584         struct packet_command *read_command;
585         struct list_head *elem, *next;
586         struct request *req;
587         unsigned long timeout;
588
589         if (list_empty(&gdrom_deferred))
590                 return;
591         read_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
592         if (!read_command)
593                 return; /* get more memory later? */
594         read_command->cmd[0] = 0x30;
595         read_command->cmd[1] = 0x20;
596         spin_lock(&gdrom_lock);
597         list_for_each_safe(elem, next, &gdrom_deferred) {
598                 req = list_entry(elem, struct request, queuelist);
599                 spin_unlock(&gdrom_lock);
600                 block = blk_rq_pos(req)/GD_TO_BLK + GD_SESSION_OFFSET;
601                 block_cnt = blk_rq_sectors(req)/GD_TO_BLK;
602                 ctrl_outl(virt_to_phys(req->buffer), GDROM_DMA_STARTADDR_REG);
603                 ctrl_outl(block_cnt * GDROM_HARD_SECTOR, GDROM_DMA_LENGTH_REG);
604                 ctrl_outl(1, GDROM_DMA_DIRECTION_REG);
605                 ctrl_outl(1, GDROM_DMA_ENABLE_REG);
606                 read_command->cmd[2] = (block >> 16) & 0xFF;
607                 read_command->cmd[3] = (block >> 8) & 0xFF;
608                 read_command->cmd[4] = block & 0xFF;
609                 read_command->cmd[8] = (block_cnt >> 16) & 0xFF;
610                 read_command->cmd[9] = (block_cnt >> 8) & 0xFF;
611                 read_command->cmd[10] = block_cnt & 0xFF;
612                 /* set for DMA */
613                 ctrl_outb(1, GDROM_ERROR_REG);
614                 /* other registers */
615                 ctrl_outb(0, GDROM_SECNUM_REG);
616                 ctrl_outb(0, GDROM_BCL_REG);
617                 ctrl_outb(0, GDROM_BCH_REG);
618                 ctrl_outb(0, GDROM_DSEL_REG);
619                 ctrl_outb(0, GDROM_INTSEC_REG);
620                 /* Wait for registers to reset after any previous activity */
621                 timeout = jiffies + HZ / 2;
622                 while (gdrom_is_busy() && time_before(jiffies, timeout))
623                         cpu_relax();
624                 ctrl_outb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
625                 timeout = jiffies + HZ / 2;
626                 /* Wait for packet command to finish */
627                 while (gdrom_is_busy() && time_before(jiffies, timeout))
628                         cpu_relax();
629                 gd.pending = 1;
630                 gd.transfer = 1;
631                 outsw(GDROM_DATA_REG, &read_command->cmd, 6);
632                 timeout = jiffies + HZ / 2;
633                 /* Wait for any pending DMA to finish */
634                 while (ctrl_inb(GDROM_DMA_STATUS_REG) &&
635                         time_before(jiffies, timeout))
636                         cpu_relax();
637                 /* start transfer */
638                 ctrl_outb(1, GDROM_DMA_STATUS_REG);
639                 wait_event_interruptible_timeout(request_queue,
640                         gd.transfer == 0, GDROM_DEFAULT_TIMEOUT);
641                 err = gd.transfer ? -EIO : 0;
642                 gd.transfer = 0;
643                 gd.pending = 0;
644                 /* now seek to take the request spinlock
645                 * before handling ending the request */
646                 spin_lock(&gdrom_lock);
647                 list_del_init(&req->queuelist);
648                 __blk_end_request_all(req, err);
649         }
650         spin_unlock(&gdrom_lock);
651         kfree(read_command);
652 }
653
654 static void gdrom_request(struct request_queue *rq)
655 {
656         struct request *req;
657
658         while ((req = blk_fetch_request(rq)) != NULL) {
659                 if (req->cmd_type != REQ_TYPE_FS) {
660                         printk(KERN_DEBUG "gdrom: Non-fs request ignored\n");
661                         __blk_end_request_all(req, -EIO);
662                         continue;
663                 }
664                 if (rq_data_dir(req) != READ) {
665                         pr_notice("Read only device - write request ignored\n");
666                         __blk_end_request_all(req, -EIO);
667                         continue;
668                 }
669
670                 /*
671                  * Add to list of deferred work and then schedule
672                  * workqueue.
673                  */
674                 list_add_tail(&req->queuelist, &gdrom_deferred);
675                 schedule_work(&work);
676         }
677 }
678
679 /* Print string identifying GD ROM device */
680 static int __devinit gdrom_outputversion(void)
681 {
682         struct gdrom_id *id;
683         char *model_name, *manuf_name, *firmw_ver;
684         int err = -ENOMEM;
685
686         /* query device ID */
687         id = kzalloc(sizeof(struct gdrom_id), GFP_KERNEL);
688         if (!id)
689                 return err;
690         gdrom_identifydevice(id);
691         model_name = kstrndup(id->modname, 16, GFP_KERNEL);
692         if (!model_name)
693                 goto free_id;
694         manuf_name = kstrndup(id->mname, 16, GFP_KERNEL);
695         if (!manuf_name)
696                 goto free_model_name;
697         firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL);
698         if (!firmw_ver)
699                 goto free_manuf_name;
700         pr_info("%s from %s with firmware %s\n",
701                 model_name, manuf_name, firmw_ver);
702         err = 0;
703         kfree(firmw_ver);
704 free_manuf_name:
705         kfree(manuf_name);
706 free_model_name:
707         kfree(model_name);
708 free_id:
709         kfree(id);
710         return err;
711 }
712
713 /* set the default mode for DMA transfer */
714 static int __devinit gdrom_init_dma_mode(void)
715 {
716         ctrl_outb(0x13, GDROM_ERROR_REG);
717         ctrl_outb(0x22, GDROM_INTSEC_REG);
718         if (!gdrom_wait_clrbusy())
719                 return -EBUSY;
720         ctrl_outb(0xEF, GDROM_STATUSCOMMAND_REG);
721         if (!gdrom_wait_busy_sleeps())
722                 return -EBUSY;
723         /* Memory protection setting for GDROM DMA
724         * Bits 31 - 16 security: 0x8843
725         * Bits 15 and 7 reserved (0)
726         * Bits 14 - 8 start of transfer range in 1 MB blocks OR'ed with 0x80
727         * Bits 6 - 0 end of transfer range in 1 MB blocks OR'ed with 0x80
728         * (0x40 | 0x80) = start range at 0x0C000000
729         * (0x7F | 0x80) = end range at 0x0FFFFFFF */
730         ctrl_outl(0x8843407F, GDROM_DMA_ACCESS_CTRL_REG);
731         ctrl_outl(9, GDROM_DMA_WAIT_REG); /* DMA word setting */
732         return 0;
733 }
734
735 static void __devinit probe_gdrom_setupcd(void)
736 {
737         gd.cd_info->ops = &gdrom_ops;
738         gd.cd_info->capacity = 1;
739         strcpy(gd.cd_info->name, GDROM_DEV_NAME);
740         gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
741                 CDC_SELECT_DISC;
742 }
743
744 static void __devinit probe_gdrom_setupdisk(void)
745 {
746         gd.disk->major = gdrom_major;
747         gd.disk->first_minor = 1;
748         gd.disk->minors = 1;
749         strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
750 }
751
752 static int __devinit probe_gdrom_setupqueue(void)
753 {
754         blk_queue_logical_block_size(gd.gdrom_rq, GDROM_HARD_SECTOR);
755         /* using DMA so memory will need to be contiguous */
756         blk_queue_max_segments(gd.gdrom_rq, 1);
757         /* set a large max size to get most from DMA */
758         blk_queue_max_segment_size(gd.gdrom_rq, 0x40000);
759         gd.disk->queue = gd.gdrom_rq;
760         return gdrom_init_dma_mode();
761 }
762
763 /*
764  * register this as a block device and as compliant with the
765  * universal CD Rom driver interface
766  */
767 static int __devinit probe_gdrom(struct platform_device *devptr)
768 {
769         int err;
770         /* Start the device */
771         if (gdrom_execute_diagnostic() != 1) {
772                 pr_warning("ATA Probe for GDROM failed\n");
773                 return -ENODEV;
774         }
775         /* Print out firmware ID */
776         if (gdrom_outputversion())
777                 return -ENOMEM;
778         /* Register GDROM */
779         gdrom_major = register_blkdev(0, GDROM_DEV_NAME);
780         if (gdrom_major <= 0)
781                 return gdrom_major;
782         pr_info("Registered with major number %d\n",
783                 gdrom_major);
784         /* Specify basic properties of drive */
785         gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL);
786         if (!gd.cd_info) {
787                 err = -ENOMEM;
788                 goto probe_fail_no_mem;
789         }
790         probe_gdrom_setupcd();
791         gd.disk = alloc_disk(1);
792         if (!gd.disk) {
793                 err = -ENODEV;
794                 goto probe_fail_no_disk;
795         }
796         probe_gdrom_setupdisk();
797         if (register_cdrom(gd.cd_info)) {
798                 err = -ENODEV;
799                 goto probe_fail_cdrom_register;
800         }
801         gd.disk->fops = &gdrom_bdops;
802         /* latch on to the interrupt */
803         err = gdrom_set_interrupt_handlers();
804         if (err)
805                 goto probe_fail_cmdirq_register;
806         gd.gdrom_rq = blk_init_queue(gdrom_request, &gdrom_lock);
807         if (!gd.gdrom_rq)
808                 goto probe_fail_requestq;
809
810         err = probe_gdrom_setupqueue();
811         if (err)
812                 goto probe_fail_toc;
813
814         gd.toc = kzalloc(sizeof(struct gdromtoc), GFP_KERNEL);
815         if (!gd.toc)
816                 goto probe_fail_toc;
817         add_disk(gd.disk);
818         return 0;
819
820 probe_fail_toc:
821         blk_cleanup_queue(gd.gdrom_rq);
822 probe_fail_requestq:
823         free_irq(HW_EVENT_GDROM_DMA, &gd);
824         free_irq(HW_EVENT_GDROM_CMD, &gd);
825 probe_fail_cmdirq_register:
826 probe_fail_cdrom_register:
827         del_gendisk(gd.disk);
828 probe_fail_no_disk:
829         kfree(gd.cd_info);
830         unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
831         gdrom_major = 0;
832 probe_fail_no_mem:
833         pr_warning("Probe failed - error is 0x%X\n", err);
834         return err;
835 }
836
837 static int __devexit remove_gdrom(struct platform_device *devptr)
838 {
839         flush_scheduled_work();
840         blk_cleanup_queue(gd.gdrom_rq);
841         free_irq(HW_EVENT_GDROM_CMD, &gd);
842         free_irq(HW_EVENT_GDROM_DMA, &gd);
843         del_gendisk(gd.disk);
844         if (gdrom_major)
845                 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
846         unregister_cdrom(gd.cd_info);
847
848         return 0;
849 }
850
851 static struct platform_driver gdrom_driver = {
852         .probe = probe_gdrom,
853         .remove = __devexit_p(remove_gdrom),
854         .driver = {
855                         .name = GDROM_DEV_NAME,
856         },
857 };
858
859 static int __init init_gdrom(void)
860 {
861         int rc;
862         gd.toc = NULL;
863         rc = platform_driver_register(&gdrom_driver);
864         if (rc)
865                 return rc;
866         pd = platform_device_register_simple(GDROM_DEV_NAME, -1, NULL, 0);
867         if (IS_ERR(pd)) {
868                 platform_driver_unregister(&gdrom_driver);
869                 return PTR_ERR(pd);
870         }
871         return 0;
872 }
873
874 static void __exit exit_gdrom(void)
875 {
876         platform_device_unregister(pd);
877         platform_driver_unregister(&gdrom_driver);
878         kfree(gd.toc);
879 }
880
881 module_init(init_gdrom);
882 module_exit(exit_gdrom);
883 MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
884 MODULE_DESCRIPTION("SEGA Dreamcast GD-ROM Driver");
885 MODULE_LICENSE("GPL");