1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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/mutex.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
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
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
84static DEFINE_MUTEX(gdrom_mutex);
85static const struct {
86 int sense_key;
87 const char * const text;
88} sense_texts[] = {
89 {NO_SENSE, "OK"},
90 {RECOVERED_ERROR, "Recovered from error"},
91 {NOT_READY, "Device not ready"},
92 {MEDIUM_ERROR, "Disk not ready"},
93 {HARDWARE_ERROR, "Hardware error"},
94 {ILLEGAL_REQUEST, "Command has failed"},
95 {UNIT_ATTENTION, "Device needs attention - disk may have been changed"},
96 {DATA_PROTECT, "Data protection error"},
97 {ABORTED_COMMAND, "Command aborted"},
98};
99
100static struct platform_device *pd;
101static int gdrom_major;
102static DECLARE_WAIT_QUEUE_HEAD(command_queue);
103static DECLARE_WAIT_QUEUE_HEAD(request_queue);
104
105static DEFINE_SPINLOCK(gdrom_lock);
106static void gdrom_readdisk_dma(struct work_struct *work);
107static DECLARE_WORK(work, gdrom_readdisk_dma);
108static LIST_HEAD(gdrom_deferred);
109
110struct gdromtoc {
111 unsigned int entry[99];
112 unsigned int first, last;
113 unsigned int leadout;
114};
115
116static struct gdrom_unit {
117 struct gendisk *disk;
118 struct cdrom_device_info *cd_info;
119 int status;
120 int pending;
121 int transfer;
122 char disk_type;
123 struct gdromtoc *toc;
124 struct request_queue *gdrom_rq;
125} gd;
126
127struct gdrom_id {
128 char mid;
129 char modid;
130 char verid;
131 char padA[13];
132 char mname[16];
133 char modname[16];
134 char firmver[16];
135 char padB[16];
136};
137
138static int gdrom_getsense(short *bufstring);
139static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
140 struct packet_command *command);
141static int gdrom_hardreset(struct cdrom_device_info *cd_info);
142
143static bool gdrom_is_busy(void)
144{
145 return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) != 0;
146}
147
148static bool gdrom_data_request(void)
149{
150 return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x88) == 8;
151}
152
153static bool gdrom_wait_clrbusy(void)
154{
155 unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
156 while ((__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) &&
157 (time_before(jiffies, timeout)))
158 cpu_relax();
159 return time_before(jiffies, timeout + 1);
160}
161
162static bool gdrom_wait_busy_sleeps(void)
163{
164 unsigned long timeout;
165
166 timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
167 while (!gdrom_is_busy() && time_before(jiffies, timeout))
168 cpu_relax();
169
170 return gdrom_wait_clrbusy();
171}
172
173static void gdrom_identifydevice(void *buf)
174{
175 int c;
176 short *data = buf;
177
178
179
180 if (!gdrom_wait_clrbusy()) {
181 gdrom_getsense(NULL);
182 return;
183 }
184 __raw_writeb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG);
185 if (!gdrom_wait_busy_sleeps()) {
186 gdrom_getsense(NULL);
187 return;
188 }
189
190 for (c = 0; c < 40; c++)
191 data[c] = __raw_readw(GDROM_DATA_REG);
192}
193
194static void gdrom_spicommand(void *spi_string, int buflen)
195{
196 short *cmd = spi_string;
197 unsigned long timeout;
198
199
200 __raw_writeb(0x08, GDROM_ALTSTATUS_REG);
201
202 __raw_writeb(buflen & 0xFF, GDROM_BCL_REG);
203 __raw_writeb((buflen >> 8) & 0xFF, GDROM_BCH_REG);
204
205 __raw_writeb(0, GDROM_INTSEC_REG);
206 __raw_writeb(0, GDROM_SECNUM_REG);
207 __raw_writeb(0, GDROM_ERROR_REG);
208
209 if (!gdrom_wait_clrbusy()) {
210 gdrom_getsense(NULL);
211 return;
212 }
213 timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
214 __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
215 while (!gdrom_data_request() && time_before(jiffies, timeout))
216 cpu_relax();
217 if (!time_before(jiffies, timeout + 1)) {
218 gdrom_getsense(NULL);
219 return;
220 }
221 outsw(GDROM_DATA_REG, cmd, 6);
222}
223
224
225
226
227
228
229
230
231static char gdrom_execute_diagnostic(void)
232{
233 gdrom_hardreset(gd.cd_info);
234 if (!gdrom_wait_clrbusy())
235 return 0;
236 __raw_writeb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG);
237 if (!gdrom_wait_busy_sleeps())
238 return 0;
239 return __raw_readb(GDROM_ERROR_REG);
240}
241
242
243
244
245
246
247static int gdrom_preparedisk_cmd(void)
248{
249 struct packet_command *spin_command;
250 spin_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
251 if (!spin_command)
252 return -ENOMEM;
253 spin_command->cmd[0] = 0x70;
254 spin_command->cmd[2] = 0x1f;
255 spin_command->buflen = 0;
256 gd.pending = 1;
257 gdrom_packetcommand(gd.cd_info, spin_command);
258
259 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
260 GDROM_DEFAULT_TIMEOUT);
261 gd.pending = 0;
262 kfree(spin_command);
263 if (gd.status & 0x01) {
264
265 gdrom_getsense(NULL);
266 return -EIO;
267 }
268 return 0;
269}
270
271
272
273
274
275
276
277
278static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session)
279{
280 int tocsize;
281 struct packet_command *toc_command;
282 int err = 0;
283
284 toc_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
285 if (!toc_command)
286 return -ENOMEM;
287 tocsize = sizeof(struct gdromtoc);
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;
293 if (gd.pending) {
294 err = -EBUSY;
295 goto cleanup_readtoc_final;
296 }
297 gd.pending = 1;
298 gdrom_packetcommand(gd.cd_info, toc_command);
299 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
300 GDROM_DEFAULT_TIMEOUT);
301 if (gd.pending) {
302 err = -EINVAL;
303 goto cleanup_readtoc;
304 }
305 insw(GDROM_DATA_REG, toc, tocsize/2);
306 if (gd.status & 0x01)
307 err = -EINVAL;
308
309cleanup_readtoc:
310 gd.pending = 0;
311cleanup_readtoc_final:
312 kfree(toc_command);
313 return err;
314}
315
316
317static int get_entry_lba(int track)
318{
319 return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET);
320}
321
322static int get_entry_q_ctrl(int track)
323{
324 return (track & 0x000000f0) >> 4;
325}
326
327static int get_entry_track(int track)
328{
329 return (track & 0x0000ff00) >> 8;
330}
331
332static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
333 struct cdrom_multisession *ms_info)
334{
335 int fentry, lentry, track, data, tocuse, err;
336 if (!gd.toc)
337 return -ENOMEM;
338 tocuse = 1;
339
340 err = gdrom_readtoc_cmd(gd.toc, 1);
341
342 if (err) {
343 tocuse = 0;
344 err = gdrom_readtoc_cmd(gd.toc, 0);
345 if (err) {
346 pr_info("Could not get CD table of contents\n");
347 return -ENXIO;
348 }
349 }
350
351 fentry = get_entry_track(gd.toc->first);
352 lentry = get_entry_track(gd.toc->last);
353
354 track = get_entry_track(gd.toc->last);
355 do {
356 data = gd.toc->entry[track - 1];
357 if (get_entry_q_ctrl(data))
358 break;
359 track--;
360 } while (track >= fentry);
361
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);
365 return -ENXIO;
366 }
367
368 ms_info->addr_format = CDROM_LBA;
369 ms_info->addr.lba = get_entry_lba(data);
370 ms_info->xa_flag = 1;
371 return 0;
372}
373
374static int gdrom_open(struct cdrom_device_info *cd_info, int purpose)
375{
376
377 return gdrom_preparedisk_cmd();
378}
379
380
381static void gdrom_release(struct cdrom_device_info *cd_info)
382{
383}
384
385static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore)
386{
387
388 char sense = __raw_readb(GDROM_ERROR_REG);
389 sense &= 0xF0;
390 if (sense == 0)
391 return CDS_DISC_OK;
392 if (sense == 0x20)
393 return CDS_DRIVE_NOT_READY;
394
395 return CDS_NO_INFO;
396}
397
398static unsigned int gdrom_check_events(struct cdrom_device_info *cd_info,
399 unsigned int clearing, int ignore)
400{
401
402 return (__raw_readb(GDROM_ERROR_REG) & 0xF0) == 0x60 ?
403 DISK_EVENT_MEDIA_CHANGE : 0;
404}
405
406
407static int gdrom_hardreset(struct cdrom_device_info *cd_info)
408{
409 int count;
410 __raw_writel(0x1fffff, GDROM_RESET_REG);
411 for (count = 0xa0000000; count < 0xa0200000; count += 4)
412 __raw_readl(count);
413 return 0;
414}
415
416
417
418static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
419 struct packet_command *command)
420{
421 gdrom_spicommand(&command->cmd, command->buflen);
422 return 0;
423}
424
425
426
427
428
429
430
431static int gdrom_getsense(short *bufstring)
432{
433 struct packet_command *sense_command;
434 short sense[5];
435 int sense_key;
436 int err = -EIO;
437
438 sense_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
439 if (!sense_command)
440 return -ENOMEM;
441 sense_command->cmd[0] = 0x13;
442 sense_command->cmd[4] = 10;
443 sense_command->buflen = 10;
444
445
446 if (gd.pending && !gdrom_wait_clrbusy()) {
447 err = -EBUSY;
448 goto cleanup_sense_final;
449 }
450 gd.pending = 1;
451 gdrom_packetcommand(gd.cd_info, sense_command);
452 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
453 GDROM_DEFAULT_TIMEOUT);
454 if (gd.pending)
455 goto cleanup_sense;
456 insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);
457 if (sense[1] & 40) {
458 pr_info("Drive not ready - command aborted\n");
459 goto cleanup_sense;
460 }
461 sense_key = sense[1] & 0x0F;
462 if (sense_key < ARRAY_SIZE(sense_texts))
463 pr_info("%s\n", sense_texts[sense_key].text);
464 else
465 pr_err("Unknown sense key: %d\n", sense_key);
466 if (bufstring)
467 memcpy(bufstring, &sense[4], 2);
468 if (sense_key < 2)
469 err = 0;
470
471cleanup_sense:
472 gd.pending = 0;
473cleanup_sense_final:
474 kfree(sense_command);
475 return err;
476}
477
478static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
479 void *arg)
480{
481 return -EINVAL;
482}
483
484static struct cdrom_device_ops gdrom_ops = {
485 .open = gdrom_open,
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,
492 .capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
493 CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,
494 .n_minors = 1,
495};
496
497static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
498{
499 int ret;
500
501 check_disk_change(bdev);
502
503 mutex_lock(&gdrom_mutex);
504 ret = cdrom_open(gd.cd_info, bdev, mode);
505 mutex_unlock(&gdrom_mutex);
506 return ret;
507}
508
509static void gdrom_bdops_release(struct gendisk *disk, fmode_t mode)
510{
511 mutex_lock(&gdrom_mutex);
512 cdrom_release(gd.cd_info, mode);
513 mutex_unlock(&gdrom_mutex);
514}
515
516static unsigned int gdrom_bdops_check_events(struct gendisk *disk,
517 unsigned int clearing)
518{
519 return cdrom_check_events(gd.cd_info, clearing);
520}
521
522static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode,
523 unsigned cmd, unsigned long arg)
524{
525 int ret;
526
527 mutex_lock(&gdrom_mutex);
528 ret = cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
529 mutex_unlock(&gdrom_mutex);
530
531 return ret;
532}
533
534static const struct block_device_operations gdrom_bdops = {
535 .owner = THIS_MODULE,
536 .open = gdrom_bdops_open,
537 .release = gdrom_bdops_release,
538 .check_events = gdrom_bdops_check_events,
539 .ioctl = gdrom_bdops_ioctl,
540};
541
542static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id)
543{
544 gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG);
545 if (gd.pending != 1)
546 return IRQ_HANDLED;
547 gd.pending = 0;
548 wake_up_interruptible(&command_queue);
549 return IRQ_HANDLED;
550}
551
552static irqreturn_t gdrom_dma_interrupt(int irq, void *dev_id)
553{
554 gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG);
555 if (gd.transfer != 1)
556 return IRQ_HANDLED;
557 gd.transfer = 0;
558 wake_up_interruptible(&request_queue);
559 return IRQ_HANDLED;
560}
561
562static int gdrom_set_interrupt_handlers(void)
563{
564 int err;
565
566 err = request_irq(HW_EVENT_GDROM_CMD, gdrom_command_interrupt,
567 IRQF_DISABLED, "gdrom_command", &gd);
568 if (err)
569 return err;
570 err = request_irq(HW_EVENT_GDROM_DMA, gdrom_dma_interrupt,
571 IRQF_DISABLED, "gdrom_dma", &gd);
572 if (err)
573 free_irq(HW_EVENT_GDROM_CMD, &gd);
574 return err;
575}
576
577
578
579
580
581
582
583
584
585
586
587static void gdrom_readdisk_dma(struct work_struct *work)
588{
589 int err, block, block_cnt;
590 struct packet_command *read_command;
591 struct list_head *elem, *next;
592 struct request *req;
593 unsigned long timeout;
594
595 if (list_empty(&gdrom_deferred))
596 return;
597 read_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
598 if (!read_command)
599 return;
600 read_command->cmd[0] = 0x30;
601 read_command->cmd[1] = 0x20;
602 spin_lock(&gdrom_lock);
603 list_for_each_safe(elem, next, &gdrom_deferred) {
604 req = list_entry(elem, struct request, queuelist);
605 spin_unlock(&gdrom_lock);
606 block = blk_rq_pos(req)/GD_TO_BLK + GD_SESSION_OFFSET;
607 block_cnt = blk_rq_sectors(req)/GD_TO_BLK;
608 __raw_writel(virt_to_phys(req->buffer), GDROM_DMA_STARTADDR_REG);
609 __raw_writel(block_cnt * GDROM_HARD_SECTOR, GDROM_DMA_LENGTH_REG);
610 __raw_writel(1, GDROM_DMA_DIRECTION_REG);
611 __raw_writel(1, GDROM_DMA_ENABLE_REG);
612 read_command->cmd[2] = (block >> 16) & 0xFF;
613 read_command->cmd[3] = (block >> 8) & 0xFF;
614 read_command->cmd[4] = block & 0xFF;
615 read_command->cmd[8] = (block_cnt >> 16) & 0xFF;
616 read_command->cmd[9] = (block_cnt >> 8) & 0xFF;
617 read_command->cmd[10] = block_cnt & 0xFF;
618
619 __raw_writeb(1, GDROM_ERROR_REG);
620
621 __raw_writeb(0, GDROM_SECNUM_REG);
622 __raw_writeb(0, GDROM_BCL_REG);
623 __raw_writeb(0, GDROM_BCH_REG);
624 __raw_writeb(0, GDROM_DSEL_REG);
625 __raw_writeb(0, GDROM_INTSEC_REG);
626
627 timeout = jiffies + HZ / 2;
628 while (gdrom_is_busy() && time_before(jiffies, timeout))
629 cpu_relax();
630 __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
631 timeout = jiffies + HZ / 2;
632
633 while (gdrom_is_busy() && time_before(jiffies, timeout))
634 cpu_relax();
635 gd.pending = 1;
636 gd.transfer = 1;
637 outsw(GDROM_DATA_REG, &read_command->cmd, 6);
638 timeout = jiffies + HZ / 2;
639
640 while (__raw_readb(GDROM_DMA_STATUS_REG) &&
641 time_before(jiffies, timeout))
642 cpu_relax();
643
644 __raw_writeb(1, GDROM_DMA_STATUS_REG);
645 wait_event_interruptible_timeout(request_queue,
646 gd.transfer == 0, GDROM_DEFAULT_TIMEOUT);
647 err = gd.transfer ? -EIO : 0;
648 gd.transfer = 0;
649 gd.pending = 0;
650
651
652 spin_lock(&gdrom_lock);
653 list_del_init(&req->queuelist);
654 __blk_end_request_all(req, err);
655 }
656 spin_unlock(&gdrom_lock);
657 kfree(read_command);
658}
659
660static void gdrom_request(struct request_queue *rq)
661{
662 struct request *req;
663
664 while ((req = blk_fetch_request(rq)) != NULL) {
665 if (req->cmd_type != REQ_TYPE_FS) {
666 printk(KERN_DEBUG "gdrom: Non-fs request ignored\n");
667 __blk_end_request_all(req, -EIO);
668 continue;
669 }
670 if (rq_data_dir(req) != READ) {
671 pr_notice("Read only device - write request ignored\n");
672 __blk_end_request_all(req, -EIO);
673 continue;
674 }
675
676
677
678
679
680 list_add_tail(&req->queuelist, &gdrom_deferred);
681 schedule_work(&work);
682 }
683}
684
685
686static int gdrom_outputversion(void)
687{
688 struct gdrom_id *id;
689 char *model_name, *manuf_name, *firmw_ver;
690 int err = -ENOMEM;
691
692
693 id = kzalloc(sizeof(struct gdrom_id), GFP_KERNEL);
694 if (!id)
695 return err;
696 gdrom_identifydevice(id);
697 model_name = kstrndup(id->modname, 16, GFP_KERNEL);
698 if (!model_name)
699 goto free_id;
700 manuf_name = kstrndup(id->mname, 16, GFP_KERNEL);
701 if (!manuf_name)
702 goto free_model_name;
703 firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL);
704 if (!firmw_ver)
705 goto free_manuf_name;
706 pr_info("%s from %s with firmware %s\n",
707 model_name, manuf_name, firmw_ver);
708 err = 0;
709 kfree(firmw_ver);
710free_manuf_name:
711 kfree(manuf_name);
712free_model_name:
713 kfree(model_name);
714free_id:
715 kfree(id);
716 return err;
717}
718
719
720static int gdrom_init_dma_mode(void)
721{
722 __raw_writeb(0x13, GDROM_ERROR_REG);
723 __raw_writeb(0x22, GDROM_INTSEC_REG);
724 if (!gdrom_wait_clrbusy())
725 return -EBUSY;
726 __raw_writeb(0xEF, GDROM_STATUSCOMMAND_REG);
727 if (!gdrom_wait_busy_sleeps())
728 return -EBUSY;
729
730
731
732
733
734
735
736 __raw_writel(0x8843407F, GDROM_DMA_ACCESS_CTRL_REG);
737 __raw_writel(9, GDROM_DMA_WAIT_REG);
738 return 0;
739}
740
741static void probe_gdrom_setupcd(void)
742{
743 gd.cd_info->ops = &gdrom_ops;
744 gd.cd_info->capacity = 1;
745 strcpy(gd.cd_info->name, GDROM_DEV_NAME);
746 gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
747 CDC_SELECT_DISC;
748}
749
750static void probe_gdrom_setupdisk(void)
751{
752 gd.disk->major = gdrom_major;
753 gd.disk->first_minor = 1;
754 gd.disk->minors = 1;
755 strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
756}
757
758static int probe_gdrom_setupqueue(void)
759{
760 blk_queue_logical_block_size(gd.gdrom_rq, GDROM_HARD_SECTOR);
761
762 blk_queue_max_segments(gd.gdrom_rq, 1);
763
764 blk_queue_max_segment_size(gd.gdrom_rq, 0x40000);
765 gd.disk->queue = gd.gdrom_rq;
766 return gdrom_init_dma_mode();
767}
768
769
770
771
772
773static int probe_gdrom(struct platform_device *devptr)
774{
775 int err;
776
777 if (gdrom_execute_diagnostic() != 1) {
778 pr_warning("ATA Probe for GDROM failed\n");
779 return -ENODEV;
780 }
781
782 if (gdrom_outputversion())
783 return -ENOMEM;
784
785 gdrom_major = register_blkdev(0, GDROM_DEV_NAME);
786 if (gdrom_major <= 0)
787 return gdrom_major;
788 pr_info("Registered with major number %d\n",
789 gdrom_major);
790
791 gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL);
792 if (!gd.cd_info) {
793 err = -ENOMEM;
794 goto probe_fail_no_mem;
795 }
796 probe_gdrom_setupcd();
797 gd.disk = alloc_disk(1);
798 if (!gd.disk) {
799 err = -ENODEV;
800 goto probe_fail_no_disk;
801 }
802 probe_gdrom_setupdisk();
803 if (register_cdrom(gd.cd_info)) {
804 err = -ENODEV;
805 goto probe_fail_cdrom_register;
806 }
807 gd.disk->fops = &gdrom_bdops;
808
809 err = gdrom_set_interrupt_handlers();
810 if (err)
811 goto probe_fail_cmdirq_register;
812 gd.gdrom_rq = blk_init_queue(gdrom_request, &gdrom_lock);
813 if (!gd.gdrom_rq)
814 goto probe_fail_requestq;
815
816 err = probe_gdrom_setupqueue();
817 if (err)
818 goto probe_fail_toc;
819
820 gd.toc = kzalloc(sizeof(struct gdromtoc), GFP_KERNEL);
821 if (!gd.toc)
822 goto probe_fail_toc;
823 add_disk(gd.disk);
824 return 0;
825
826probe_fail_toc:
827 blk_cleanup_queue(gd.gdrom_rq);
828probe_fail_requestq:
829 free_irq(HW_EVENT_GDROM_DMA, &gd);
830 free_irq(HW_EVENT_GDROM_CMD, &gd);
831probe_fail_cmdirq_register:
832probe_fail_cdrom_register:
833 del_gendisk(gd.disk);
834probe_fail_no_disk:
835 kfree(gd.cd_info);
836 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
837 gdrom_major = 0;
838probe_fail_no_mem:
839 pr_warning("Probe failed - error is 0x%X\n", err);
840 return err;
841}
842
843static int remove_gdrom(struct platform_device *devptr)
844{
845 flush_work(&work);
846 blk_cleanup_queue(gd.gdrom_rq);
847 free_irq(HW_EVENT_GDROM_CMD, &gd);
848 free_irq(HW_EVENT_GDROM_DMA, &gd);
849 del_gendisk(gd.disk);
850 if (gdrom_major)
851 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
852 unregister_cdrom(gd.cd_info);
853
854 return 0;
855}
856
857static struct platform_driver gdrom_driver = {
858 .probe = probe_gdrom,
859 .remove = remove_gdrom,
860 .driver = {
861 .name = GDROM_DEV_NAME,
862 },
863};
864
865static int __init init_gdrom(void)
866{
867 int rc;
868 gd.toc = NULL;
869 rc = platform_driver_register(&gdrom_driver);
870 if (rc)
871 return rc;
872 pd = platform_device_register_simple(GDROM_DEV_NAME, -1, NULL, 0);
873 if (IS_ERR(pd)) {
874 platform_driver_unregister(&gdrom_driver);
875 return PTR_ERR(pd);
876 }
877 return 0;
878}
879
880static void __exit exit_gdrom(void)
881{
882 platform_device_unregister(pd);
883 platform_driver_unregister(&gdrom_driver);
884 kfree(gd.toc);
885}
886
887module_init(init_gdrom);
888module_exit(exit_gdrom);
889MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
890MODULE_DESCRIPTION("SEGA Dreamcast GD-ROM Driver");
891MODULE_LICENSE("GPL");
892