blob: 0c0df7f73802ea71d44e3cf1cc762280a5f780be [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/io.h>
42#include <asm/uaccess.h>
43#include <asm/q40_master.h>
44#include <asm/irq.h>
45#include <asm/q40ints.h>
46
Dmitry Torokhov409e1542012-01-22 23:27:54 -080047#define DRV_NAME "q40kbd"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
50MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
51MODULE_LICENSE("GPL");
Dmitry Torokhov409e1542012-01-22 23:27:54 -080052MODULE_ALIAS("platform:" DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Dmitry Torokhov409e1542012-01-22 23:27:54 -080054struct q40kbd {
55 struct serio *port;
56 spinlock_t lock;
57};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Howells7d12e782006-10-05 14:55:46 +010059static irqreturn_t q40kbd_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Dmitry Torokhov409e1542012-01-22 23:27:54 -080061 struct q40kbd *q40kbd = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long flags;
63
Dmitry Torokhov409e1542012-01-22 23:27:54 -080064 spin_lock_irqsave(&q40kbd->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
Dmitry Torokhov409e1542012-01-22 23:27:54 -080067 serio_interrupt(q40kbd->port, master_inb(KEYCODE_REG), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 master_outb(-1, KEYBOARD_UNLOCK_REG);
70
Dmitry Torokhov409e1542012-01-22 23:27:54 -080071 spin_unlock_irqrestore(&q40kbd->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 return IRQ_HANDLED;
74}
75
76/*
77 * q40kbd_flush() flushes all data that may be in the keyboard buffers
78 */
79
Dmitry Torokhov409e1542012-01-22 23:27:54 -080080static void q40kbd_flush(struct q40kbd *q40kbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Dmitry Torokhov26421c72005-12-28 01:25:42 -050082 int maxread = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned long flags;
84
Dmitry Torokhov409e1542012-01-22 23:27:54 -080085 spin_lock_irqsave(&q40kbd->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dmitry Torokhov26421c72005-12-28 01:25:42 -050087 while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
88 master_inb(KEYCODE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Dmitry Torokhov409e1542012-01-22 23:27:54 -080090 spin_unlock_irqrestore(&q40kbd->lock, flags);
91}
92
93static void q40kbd_stop(void)
94{
95 master_outb(0, KEY_IRQ_ENABLE_REG);
96 master_outb(-1, KEYBOARD_UNLOCK_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/*
100 * q40kbd_open() is called when a port is open by the higher layer.
101 * It allocates the interrupt and enables in in the chip.
102 */
103
104static int q40kbd_open(struct serio *port)
105{
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800106 struct q40kbd *q40kbd = port->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800108 q40kbd_flush(q40kbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500110 /* off we go */
111 master_outb(-1, KEYBOARD_UNLOCK_REG);
112 master_outb(1, KEY_IRQ_ENABLE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void q40kbd_close(struct serio *port)
118{
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800119 struct q40kbd *q40kbd = port->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800121 q40kbd_stop();
122 q40kbd_flush(q40kbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800125static int __devinit q40kbd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800127 struct q40kbd *q40kbd;
128 struct serio *port;
129 int error;
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500130
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800131 q40kbd = kzalloc(sizeof(struct q40kbd), GFP_KERNEL);
132 port = kzalloc(sizeof(struct serio), GFP_KERNEL);
133 if (!q40kbd || !port) {
134 error = -ENOMEM;
135 goto err_free_mem;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800138 q40kbd->port = port;
139 spin_lock_init(&q40kbd->lock);
140
141 port->id.type = SERIO_8042;
142 port->open = q40kbd_open;
143 port->close = q40kbd_close;
144 port->port_data = q40kbd;
145 port->dev.parent = &pdev->dev;
146 strlcpy(port->name, "Q40 Kbd Port", sizeof(port->name));
147 strlcpy(port->phys, "Q40", sizeof(port->phys));
148
149 q40kbd_stop();
150
151 error = request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0,
152 DRV_NAME, q40kbd);
153 if (error) {
154 dev_err(&pdev->dev, "Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
155 goto err_free_mem;
156 }
157
158 serio_register_port(q40kbd->port);
159
160 platform_set_drvdata(pdev, q40kbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 printk(KERN_INFO "serio: Q40 kbd registered\n");
162
163 return 0;
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800164
165err_free_mem:
166 kfree(port);
167 kfree(q40kbd);
168 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800171static int __devexit q40kbd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800173 struct q40kbd *q40kbd = platform_get_drvdata(pdev);
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500174
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800175 /*
176 * q40kbd_close() will be called as part of unregistering
177 * and will ensure that IRQ is turned off, so it is safe
178 * to unregister port first and free IRQ later.
179 */
180 serio_unregister_port(q40kbd->port);
181 free_irq(Q40_IRQ_KEYBOARD, q40kbd);
182 kfree(q40kbd);
183
184 platform_set_drvdata(pdev, NULL);
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500185 return 0;
186}
187
188static struct platform_driver q40kbd_driver = {
189 .driver = {
190 .name = "q40kbd",
191 .owner = THIS_MODULE,
192 },
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500193 .remove = __devexit_p(q40kbd_remove),
194};
195
196static int __init q40kbd_init(void)
197{
Dmitry Torokhov409e1542012-01-22 23:27:54 -0800198 return platform_driver_probe(&q40kbd_driver, q40kbd_probe);
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500199}
200
201static void __exit q40kbd_exit(void)
202{
Dmitry Torokhov26421c72005-12-28 01:25:42 -0500203 platform_driver_unregister(&q40kbd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206module_init(q40kbd_init);
207module_exit(q40kbd_exit);