blob: e36a0901646cb2d2d91be060771946af9d60f75b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2000-2001 Vojtech Pavlik
3 *
4 * Based on the work of:
5 * Richard Zidlicky <Richard.Zidlicky@stud.informatik.uni-erlangen.de>
6 */
7
8/*
9 * Q40 PS/2 keyboard controller driver for Linux/m68k
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/init.h>
34#include <linux/serio.h>
35#include <linux/interrupt.h>
36#include <linux/err.h>
37#include <linux/bitops.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/uaccess.h>
42#include <asm/q40_master.h>
43#include <asm/irq.h>
44#include <asm/q40ints.h>
45
46MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
47MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
48MODULE_LICENSE("GPL");
49
Adrian Bunk4ad88902008-07-03 12:10:52 -040050static DEFINE_SPINLOCK(q40kbd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct serio *q40kbd_port;
52static struct platform_device *q40kbd_device;
53
David Howells7d12e782006-10-05 14:55:46 +010054static irqreturn_t q40kbd_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 unsigned long flags;
57
58 spin_lock_irqsave(&q40kbd_lock, flags);
59
60 if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
David Howells7d12e782006-10-05 14:55:46 +010061 serio_interrupt(q40kbd_port, master_inb(KEYCODE_REG), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 master_outb(-1, KEYBOARD_UNLOCK_REG);
64
65 spin_unlock_irqrestore(&q40kbd_lock, flags);
66
67 return IRQ_HANDLED;
68}
69
70/*
71 * q40kbd_flush() flushes all data that may be in the keyboard buffers
72 */
73
74static void q40kbd_flush(void)
75{
Dmitry Torokhov26421c72005-12-28 01:25:42 -050076 int maxread = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long flags;
78
79 spin_lock_irqsave(&q40kbd_lock, flags);
80
Dmitry Torokhov26421c72005-12-28 01:25:42 -050081 while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
82 master_inb(KEYCODE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 spin_unlock_irqrestore(&q40kbd_lock, flags);
85}
86
87/*
88 * q40kbd_open() is called when a port is open by the higher layer.
89 * It allocates the interrupt and enables in in the chip.
90 */
91
92static int q40kbd_open(struct serio *port)
93{
94 q40kbd_flush();
95
96 if (request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0, "q40kbd", NULL)) {
97 printk(KERN_ERR "q40kbd.c: Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
Dmitry Torokhov26421c72005-12-28 01:25:42 -050098 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500101 /* off we go */
102 master_outb(-1, KEYBOARD_UNLOCK_REG);
103 master_outb(1, KEY_IRQ_ENABLE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108static void q40kbd_close(struct serio *port)
109{
110 master_outb(0, KEY_IRQ_ENABLE_REG);
111 master_outb(-1, KEYBOARD_UNLOCK_REG);
112 free_irq(Q40_IRQ_KEYBOARD, NULL);
113
114 q40kbd_flush();
115}
116
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500117static int __devinit q40kbd_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500119 q40kbd_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
120 if (!q40kbd_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -ENOMEM;
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500122
123 q40kbd_port->id.type = SERIO_8042;
124 q40kbd_port->open = q40kbd_open;
125 q40kbd_port->close = q40kbd_close;
126 q40kbd_port->dev.parent = &dev->dev;
127 strlcpy(q40kbd_port->name, "Q40 Kbd Port", sizeof(q40kbd_port->name));
128 strlcpy(q40kbd_port->phys, "Q40", sizeof(q40kbd_port->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 serio_register_port(q40kbd_port);
131 printk(KERN_INFO "serio: Q40 kbd registered\n");
132
133 return 0;
134}
135
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500136static int __devexit q40kbd_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 serio_unregister_port(q40kbd_port);
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500139
140 return 0;
141}
142
143static struct platform_driver q40kbd_driver = {
144 .driver = {
145 .name = "q40kbd",
146 .owner = THIS_MODULE,
147 },
148 .probe = q40kbd_probe,
149 .remove = __devexit_p(q40kbd_remove),
150};
151
152static int __init q40kbd_init(void)
153{
154 int error;
155
156 if (!MACH_IS_Q40)
Geert Uytterhoevenfd5b4622008-05-18 20:47:18 +0200157 return -ENODEV;
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500158
159 error = platform_driver_register(&q40kbd_driver);
160 if (error)
161 return error;
162
163 q40kbd_device = platform_device_alloc("q40kbd", -1);
164 if (!q40kbd_device)
165 goto err_unregister_driver;
166
167 error = platform_device_add(q40kbd_device);
168 if (error)
169 goto err_free_device;
170
171 return 0;
172
173 err_free_device:
174 platform_device_put(q40kbd_device);
175 err_unregister_driver:
176 platform_driver_unregister(&q40kbd_driver);
177 return error;
178}
179
180static void __exit q40kbd_exit(void)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 platform_device_unregister(q40kbd_device);
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500183 platform_driver_unregister(&q40kbd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186module_init(q40kbd_init);
187module_exit(q40kbd_exit);