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