Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / drivers / input / serio / q40kbd.c
1 /*
2  * $Id: q40kbd.c,v 1.12 2002/02/02 22:26:44 vojtech Exp $
3  *
4  *  Copyright (c) 2000-2001 Vojtech Pavlik
5  *
6  *  Based on the work of:
7  *      Richard Zidlicky <Richard.Zidlicky@stud.informatik.uni-erlangen.de>
8  */
9
10 /*
11  * Q40 PS/2 keyboard controller driver for Linux/m68k
12  */
13
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  * Should you need to contact me, the author, you can do so either by
30  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
32  */
33
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/serio.h>
37 #include <linux/interrupt.h>
38 #include <linux/err.h>
39 #include <linux/bitops.h>
40 #include <linux/platform_device.h>
41
42 #include <asm/io.h>
43 #include <asm/uaccess.h>
44 #include <asm/q40_master.h>
45 #include <asm/irq.h>
46 #include <asm/q40ints.h>
47
48 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
49 MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
50 MODULE_LICENSE("GPL");
51
52 DEFINE_SPINLOCK(q40kbd_lock);
53 static struct serio *q40kbd_port;
54 static struct platform_device *q40kbd_device;
55
56 static irqreturn_t q40kbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
57 {
58         unsigned long flags;
59
60         spin_lock_irqsave(&q40kbd_lock, flags);
61
62         if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
63                 serio_interrupt(q40kbd_port, master_inb(KEYCODE_REG), 0, regs);
64
65         master_outb(-1, KEYBOARD_UNLOCK_REG);
66
67         spin_unlock_irqrestore(&q40kbd_lock, flags);
68
69         return IRQ_HANDLED;
70 }
71
72 /*
73  * q40kbd_flush() flushes all data that may be in the keyboard buffers
74  */
75
76 static void q40kbd_flush(void)
77 {
78         int maxread = 100;
79         unsigned long flags;
80
81         spin_lock_irqsave(&q40kbd_lock, flags);
82
83         while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
84                 master_inb(KEYCODE_REG);
85
86         spin_unlock_irqrestore(&q40kbd_lock, flags);
87 }
88
89 /*
90  * q40kbd_open() is called when a port is open by the higher layer.
91  * It allocates the interrupt and enables in in the chip.
92  */
93
94 static int q40kbd_open(struct serio *port)
95 {
96         q40kbd_flush();
97
98         if (request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0, "q40kbd", NULL)) {
99                 printk(KERN_ERR "q40kbd.c: Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
100                 return -EBUSY;
101         }
102
103         /* off we go */
104         master_outb(-1, KEYBOARD_UNLOCK_REG);
105         master_outb(1, KEY_IRQ_ENABLE_REG);
106
107         return 0;
108 }
109
110 static void q40kbd_close(struct serio *port)
111 {
112         master_outb(0, KEY_IRQ_ENABLE_REG);
113         master_outb(-1, KEYBOARD_UNLOCK_REG);
114         free_irq(Q40_IRQ_KEYBOARD, NULL);
115
116         q40kbd_flush();
117 }
118
119 static int __devinit q40kbd_probe(struct platform_device *dev)
120 {
121         q40kbd_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
122         if (!q40kbd_port)
123                 return -ENOMEM;
124
125         q40kbd_port->id.type    = SERIO_8042;
126         q40kbd_port->open       = q40kbd_open;
127         q40kbd_port->close      = q40kbd_close;
128         q40kbd_port->dev.parent = &dev->dev;
129         strlcpy(q40kbd_port->name, "Q40 Kbd Port", sizeof(q40kbd_port->name));
130         strlcpy(q40kbd_port->phys, "Q40", sizeof(q40kbd_port->phys));
131
132         serio_register_port(q40kbd_port);
133         printk(KERN_INFO "serio: Q40 kbd registered\n");
134
135         return 0;
136 }
137
138 static int __devexit q40kbd_remove(struct platform_device *dev)
139 {
140         serio_unregister_port(q40kbd_port);
141
142         return 0;
143 }
144
145 static struct platform_driver q40kbd_driver = {
146         .driver         = {
147                 .name   = "q40kbd",
148                 .owner  = THIS_MODULE,
149         },
150         .probe          = q40kbd_probe,
151         .remove         = __devexit_p(q40kbd_remove),
152 };
153
154 static int __init q40kbd_init(void)
155 {
156         int error;
157
158         if (!MACH_IS_Q40)
159                 return -EIO;
160
161         error = platform_driver_register(&q40kbd_driver);
162         if (error)
163                 return error;
164
165         q40kbd_device = platform_device_alloc("q40kbd", -1);
166         if (!q40kbd_device)
167                 goto err_unregister_driver;
168
169         error = platform_device_add(q40kbd_device);
170         if (error)
171                 goto err_free_device;
172
173         return 0;
174
175  err_free_device:
176         platform_device_put(q40kbd_device);
177  err_unregister_driver:
178         platform_driver_unregister(&q40kbd_driver);
179         return error;
180 }
181
182 static void __exit q40kbd_exit(void)
183 {
184         platform_device_unregister(q40kbd_device);
185         platform_driver_unregister(&q40kbd_driver);
186 }
187
188 module_init(q40kbd_init);
189 module_exit(q40kbd_exit);