MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / nios2nommu / drivers / altps2.c
blob4a6523c37a9a88ba5ab954eb0d539223e381305c
1 /*
2 * altera DE2 PS/2
4 * linux/drivers/input/serio/sa1111ps2.c
6 * Copyright (C) 2002 Russell King
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/input.h>
15 #include <linux/serio.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/delay.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
23 #include <asm/io.h>
24 #include <asm/system.h>
27 struct ps2if {
28 struct serio *io;
29 struct platform_device *dev;
30 unsigned base;
31 unsigned irq;
35 * Read all bytes waiting in the PS2 port. There should be
36 * at the most one, but we loop for safety. If there was a
37 * framing error, we have to manually clear the status.
39 static irqreturn_t ps2_rxint(int irq, void *dev_id)
41 struct ps2if *ps2if = dev_id;
42 unsigned int status;
43 int handled = IRQ_NONE;
45 while ((status = inl(ps2if->base)) & 0xffff0000) {
46 serio_interrupt(ps2if->io, status & 0xff, 0);
47 handled = IRQ_HANDLED;
49 return handled;
53 * Write a byte to the PS2 port. We have to wait for the
54 * port to indicate that the transmitter is empty.
56 static int ps2_write(struct serio *io, unsigned char val)
58 struct ps2if *ps2if = io->port_data;
59 outl(val,ps2if->base);
60 // should check command send error
61 if (inl(ps2if->base+4) & (1<<10))
63 // printk("ps2 write error %02x\n",val);
65 return 0;
68 static int ps2_open(struct serio *io)
70 struct ps2if *ps2if = io->port_data;
71 int ret;
73 ret = request_irq(ps2if->irq, ps2_rxint, 0,
74 "altps2", ps2if);
75 if (ret) {
76 printk(KERN_ERR "altps2: could not allocate IRQ%d: %d\n",
77 ps2if->irq, ret);
78 return ret;
80 outl(1,ps2if->base+4); // enable rx irq
81 return 0;
84 static void ps2_close(struct serio *io)
86 struct ps2if *ps2if = io->port_data;
87 outl(0,ps2if->base); // disable rx irq
88 free_irq(ps2if->irq, ps2if);
92 * Add one device to this driver.
94 static int ps2_probe(struct platform_device *dev)
96 struct ps2if *ps2if;
97 struct serio *serio;
98 unsigned int status;
99 int ret;
101 ps2if = kmalloc(sizeof(struct ps2if), GFP_KERNEL);
102 serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
103 if (!ps2if || !serio) {
104 ret = -ENOMEM;
105 goto free;
108 memset(ps2if, 0, sizeof(struct ps2if));
109 memset(serio, 0, sizeof(struct serio));
111 serio->id.type = SERIO_8042;
112 serio->write = ps2_write;
113 serio->open = ps2_open;
114 serio->close = ps2_close;
115 strlcpy(serio->name, dev->dev.bus_id, sizeof(serio->name));
116 strlcpy(serio->phys, dev->dev.bus_id, sizeof(serio->phys));
117 serio->port_data = ps2if;
118 serio->dev.parent = &dev->dev;
119 ps2if->io = serio;
120 ps2if->dev = dev;
121 platform_set_drvdata(dev, ps2if);
124 * Request the physical region for this PS2 port.
126 if (dev->num_resources < 2) {
127 ret = -ENODEV;
128 goto out;
130 if (!request_mem_region(dev->resource[0].start,
132 "altps2")) {
133 ret = -EBUSY;
134 goto free;
136 ps2if->base = dev->resource[0].start;
137 ps2if->irq = dev->resource[1].start;
138 printk("altps2 : base %08x irq %d\n",ps2if->base,ps2if->irq);
139 // clear fifo
140 while ((status = inl(ps2if->base)) & 0xffff0000) {
143 serio_register_port(ps2if->io);
144 return 0;
146 out:
147 release_mem_region(dev->resource[0].start,4);
148 free:
149 platform_set_drvdata(dev, NULL);
150 kfree(ps2if);
151 kfree(serio);
152 return ret;
156 * Remove one device from this driver.
158 static int ps2_remove(struct platform_device *dev)
160 struct ps2if *ps2if = platform_get_drvdata(dev);
162 platform_set_drvdata(dev, NULL);
163 serio_unregister_port(ps2if->io);
164 release_mem_region(dev->resource[0].start,4);
166 kfree(ps2if);
168 return 0;
172 * Our device driver structure
174 static struct platform_driver ps2_driver = {
175 .probe = ps2_probe,
176 .remove = ps2_remove,
177 .driver = {
178 .name = "altps2",
182 static int __init ps2_init(void)
184 return platform_driver_register(&ps2_driver);
187 static void __exit ps2_exit(void)
189 platform_driver_unregister(&ps2_driver);
192 module_init(ps2_init);
193 module_exit(ps2_exit);