xref: /openbmc/linux/drivers/input/serio/q40kbd.c (revision 1da177e4)
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 
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 
47 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
48 MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
49 MODULE_LICENSE("GPL");
50 
51 DEFINE_SPINLOCK(q40kbd_lock);
52 static struct serio *q40kbd_port;
53 static struct platform_device *q40kbd_device;
54 
55 static irqreturn_t q40kbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
56 {
57 	unsigned long flags;
58 
59 	spin_lock_irqsave(&q40kbd_lock, flags);
60 
61 	if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
62 		serio_interrupt(q40kbd_port, master_inb(KEYCODE_REG), 0, regs);
63 
64 	master_outb(-1, KEYBOARD_UNLOCK_REG);
65 
66 	spin_unlock_irqrestore(&q40kbd_lock, flags);
67 
68 	return IRQ_HANDLED;
69 }
70 
71 /*
72  * q40kbd_flush() flushes all data that may be in the keyboard buffers
73  */
74 
75 static void q40kbd_flush(void)
76 {
77  	int maxread = 100;
78 	unsigned long flags;
79 
80 	spin_lock_irqsave(&q40kbd_lock, flags);
81 
82  	while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
83  		master_inb(KEYCODE_REG);
84 
85 	spin_unlock_irqrestore(&q40kbd_lock, flags);
86 }
87 
88 /*
89  * q40kbd_open() is called when a port is open by the higher layer.
90  * It allocates the interrupt and enables in in the chip.
91  */
92 
93 static int q40kbd_open(struct serio *port)
94 {
95 	q40kbd_flush();
96 
97 	if (request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0, "q40kbd", NULL)) {
98 		printk(KERN_ERR "q40kbd.c: Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
99 		return -1;
100 	}
101 
102  	/* off we go */
103  	master_outb(-1, KEYBOARD_UNLOCK_REG);
104  	master_outb(1, KEY_IRQ_ENABLE_REG);
105 
106  	return 0;
107 }
108 
109 static void q40kbd_close(struct serio *port)
110 {
111 	master_outb(0, KEY_IRQ_ENABLE_REG);
112 	master_outb(-1, KEYBOARD_UNLOCK_REG);
113 	free_irq(Q40_IRQ_KEYBOARD, NULL);
114 
115 	q40kbd_flush();
116 }
117 
118 static struct serio * __init q40kbd_allocate_port(void)
119 {
120 	struct serio *serio;
121 
122 	serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
123 	if (serio) {
124 		memset(serio, 0, sizeof(struct serio));
125 		serio->id.type		= SERIO_8042;
126 		serio->open		= q40kbd_open;
127 		serio->close		= q40kbd_close;
128 		serio->dev.parent	= &q40kbd_device->dev;
129 		strlcpy(serio->name, "Q40 Kbd Port", sizeof(serio->name));
130 		strlcpy(serio->phys, "Q40", sizeof(serio->phys));
131 	}
132 
133 	return serio;
134 }
135 
136 static int __init q40kbd_init(void)
137 {
138 	if (!MACH_IS_Q40)
139 		return -EIO;
140 
141 	q40kbd_device = platform_device_register_simple("q40kbd", -1, NULL, 0);
142 	if (IS_ERR(q40kbd_device))
143 		return PTR_ERR(q40kbd_device);
144 
145 	if (!(q40kbd_port = q40kbd_allocate_port())) {
146 		platform_device_unregister(q40kbd_device);
147 		return -ENOMEM;
148 	}
149 
150 	serio_register_port(q40kbd_port);
151 	printk(KERN_INFO "serio: Q40 kbd registered\n");
152 
153 	return 0;
154 }
155 
156 static void __exit q40kbd_exit(void)
157 {
158 	serio_unregister_port(q40kbd_port);
159 	platform_device_unregister(q40kbd_device);
160 }
161 
162 module_init(q40kbd_init);
163 module_exit(q40kbd_exit);
164