vserver 1.9.3
[linux-2.6.git] / drivers / input / serio / rpckbd.c
1 /*
2  * $Id: rpckbd.c,v 1.7 2001/09/25 10:12:07 vojtech Exp $
3  *
4  *  Copyright (c) 2000-2001 Vojtech Pavlik
5  *  Copyright (c) 2002 Russell King
6  */
7
8 /*
9  * Acorn RiscPC PS/2 keyboard controller driver for Linux/ARM
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Should you need to contact me, the author, you can do so either by
28  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30  */
31
32 #include <linux/module.h>
33 #include <linux/interrupt.h>
34 #include <linux/init.h>
35 #include <linux/serio.h>
36 #include <linux/err.h>
37
38 #include <asm/irq.h>
39 #include <asm/hardware.h>
40 #include <asm/io.h>
41 #include <asm/hardware/iomd.h>
42 #include <asm/system.h>
43
44 MODULE_AUTHOR("Vojtech Pavlik, Russell King");
45 MODULE_DESCRIPTION("Acorn RiscPC PS/2 keyboard controller driver");
46 MODULE_LICENSE("GPL");
47
48 static struct serio *rpckbd_port;
49 static struct platform_device *rpckbd_device;
50
51 static int rpckbd_write(struct serio *port, unsigned char val)
52 {
53         while (!(iomd_readb(IOMD_KCTRL) & (1 << 7)))
54                 cpu_relax();
55
56         iomd_writeb(val, IOMD_KARTTX);
57
58         return 0;
59 }
60
61 static irqreturn_t rpckbd_rx(int irq, void *dev_id, struct pt_regs *regs)
62 {
63         struct serio *port = dev_id;
64         unsigned int byte;
65         int handled = IRQ_NONE;
66
67         while (iomd_readb(IOMD_KCTRL) & (1 << 5)) {
68                 byte = iomd_readb(IOMD_KARTRX);
69
70                 serio_interrupt(port, byte, 0, regs);
71                 handled = IRQ_HANDLED;
72         }
73         return handled;
74 }
75
76 static irqreturn_t rpckbd_tx(int irq, void *dev_id, struct pt_regs *regs)
77 {
78         return IRQ_HANDLED;
79 }
80
81 static int rpckbd_open(struct serio *port)
82 {
83         /* Reset the keyboard state machine. */
84         iomd_writeb(0, IOMD_KCTRL);
85         iomd_writeb(8, IOMD_KCTRL);
86         iomd_readb(IOMD_KARTRX);
87
88         if (request_irq(IRQ_KEYBOARDRX, rpckbd_rx, 0, "rpckbd", port) != 0) {
89                 printk(KERN_ERR "rpckbd.c: Could not allocate keyboard receive IRQ\n");
90                 return -EBUSY;
91         }
92
93         if (request_irq(IRQ_KEYBOARDTX, rpckbd_tx, 0, "rpckbd", port) != 0) {
94                 printk(KERN_ERR "rpckbd.c: Could not allocate keyboard transmit IRQ\n");
95                 free_irq(IRQ_KEYBOARDRX, NULL);
96                 return -EBUSY;
97         }
98
99         return 0;
100 }
101
102 static void rpckbd_close(struct serio *port)
103 {
104         free_irq(IRQ_KEYBOARDRX, port);
105         free_irq(IRQ_KEYBOARDTX, port);
106 }
107
108 /*
109  * Allocate and initialize serio structure for subsequent registration
110  * with serio core.
111  */
112
113 static struct serio * __init rpckbd_allocate_port(void)
114 {
115         struct serio *serio;
116
117         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
118         if (serio) {
119                 memset(serio, 0, sizeof(struct serio));
120                 serio->type             = SERIO_8042;
121                 serio->write            = rpckbd_write;
122                 serio->open             = rpckbd_open;
123                 serio->close            = rpckbd_close;
124                 serio->dev.parent       = &rpckbd_device->dev;
125                 strlcpy(serio->name, "RiscPC PS/2 kbd port", sizeof(serio->name));
126                 strlcpy(serio->phys, "rpckbd/serio0", sizeof(serio->phys));
127         }
128
129         return serio;
130 }
131
132 static int __init rpckbd_init(void)
133 {
134         rpckbd_device = platform_device_register_simple("rpckbd", -1, NULL, 0);
135         if (IS_ERR(rpckbd_device))
136                 return PTR_ERR(rpckbd_device);
137
138         if (!(rpckbd_port = rpckbd_allocate_port())) {
139                 platform_device_unregister(rpckbd_device);
140                 return -ENOMEM;
141         }
142
143         serio_register_port(rpckbd_port);
144         return 0;
145 }
146
147 static void __exit rpckbd_exit(void)
148 {
149         serio_unregister_port(rpckbd_port);
150         platform_device_unregister(rpckbd_device);
151 }
152
153 module_init(rpckbd_init);
154 module_exit(rpckbd_exit);