patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / keyboard / sunkbd.c
1 /*
2  * $Id: sunkbd.c,v 1.14 2001/09/25 10:12:07 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  */
6
7 /*
8  * Sun keyboard driver for Linux
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/interrupt.h>
35 #include <linux/init.h>
36 #include <linux/input.h>
37 #include <linux/serio.h>
38 #include <linux/workqueue.h>
39
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("Sun keyboard driver");
42 MODULE_LICENSE("GPL");
43
44 static unsigned char sunkbd_keycode[128] = {
45           0,128,114,129,115, 59, 60, 68, 61, 87, 62, 88, 63,100, 64,  0,
46          65, 66, 67, 56,103,119, 99, 70,105,130,131,108,106,  1,  2,  3,
47           4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 41, 14,110,113, 98, 55,
48         116,132, 83,133,102, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
49          26, 27,111,127, 71, 72, 73, 74,134,135,107,  0, 29, 30, 31, 32,
50          33, 34, 35, 36, 37, 38, 39, 40, 43, 28, 96, 75, 76, 77, 82,136,
51         104,137, 69, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,101,
52          79, 80, 81,  0,  0,  0,138, 58,125, 57,126,109, 86, 78
53 };
54
55 #define SUNKBD_CMD_RESET        0x1
56 #define SUNKBD_CMD_BELLON       0x2
57 #define SUNKBD_CMD_BELLOFF      0x3
58 #define SUNKBD_CMD_CLICK        0xa
59 #define SUNKBD_CMD_NOCLICK      0xb
60 #define SUNKBD_CMD_SETLED       0xe
61 #define SUNKBD_CMD_LAYOUT       0xf
62
63 #define SUNKBD_RET_RESET        0xff
64 #define SUNKBD_RET_ALLUP        0x7f
65 #define SUNKBD_RET_LAYOUT       0xfe
66
67 #define SUNKBD_LAYOUT_5_MASK    0x20
68 #define SUNKBD_RELEASE          0x80
69 #define SUNKBD_KEY              0x7f
70
71 /*
72  * Per-keyboard data.
73  */
74
75 struct sunkbd {
76         unsigned char keycode[128];
77         struct input_dev dev;
78         struct serio *serio;
79         struct work_struct tq;
80         wait_queue_head_t wait;
81         char name[64];
82         char phys[32];
83         char type;
84         volatile char reset;
85         volatile char layout;
86 };
87
88 /*
89  * sunkbd_interrupt() is called by the low level driver when a character
90  * is received.
91  */
92
93 static irqreturn_t sunkbd_interrupt(struct serio *serio,
94                 unsigned char data, unsigned int flags, struct pt_regs *regs)
95 {
96         struct sunkbd* sunkbd = serio->private;
97
98         if (sunkbd->reset <= -1) {              /* If cp[i] is 0xff, sunkbd->reset will stay -1. */
99                 sunkbd->reset = data;           /* The keyboard sends 0xff 0xff 0xID on powerup */
100                 wake_up_interruptible(&sunkbd->wait);
101                 goto out;
102         }
103
104         if (sunkbd->layout == -1) {
105                 sunkbd->layout = data;
106                 wake_up_interruptible(&sunkbd->wait);
107                 goto out;
108         }
109
110         switch (data) {
111
112                 case SUNKBD_RET_RESET:
113                         schedule_work(&sunkbd->tq);
114                         sunkbd->reset = -1;
115                         break;
116
117                 case SUNKBD_RET_LAYOUT:
118                         sunkbd->layout = -1;
119                         break;
120
121                 case SUNKBD_RET_ALLUP: /* All keys released */
122                         break;
123
124                 default:
125                         if (sunkbd->keycode[data & SUNKBD_KEY]) {
126                                 input_regs(&sunkbd->dev, regs);
127                                 input_report_key(&sunkbd->dev, sunkbd->keycode[data & SUNKBD_KEY], !(data & SUNKBD_RELEASE));
128                                 input_sync(&sunkbd->dev);
129                         } else {
130                                 printk(KERN_WARNING "sunkbd.c: Unknown key (scancode %#x) %s.\n",
131                                         data & SUNKBD_KEY, data & SUNKBD_RELEASE ? "released" : "pressed");
132                         }
133         }
134 out:
135         return IRQ_HANDLED;
136 }
137
138 /*
139  * sunkbd_event() handles events from the input module.
140  */
141
142 static int sunkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
143 {
144         struct sunkbd *sunkbd = dev->private;
145
146         switch (type) {
147
148                 case EV_LED:
149
150                         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_SETLED);
151                         sunkbd->serio->write(sunkbd->serio,
152                                 (!!test_bit(LED_CAPSL, dev->led) << 3) | (!!test_bit(LED_SCROLLL, dev->led) << 2) |
153                                 (!!test_bit(LED_COMPOSE, dev->led) << 1) | !!test_bit(LED_NUML, dev->led));
154                         return 0;
155
156                 case EV_SND:
157
158                         switch (code) {
159
160                                 case SND_CLICK:
161                                         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_NOCLICK - value);
162                                         return 0;
163
164                                 case SND_BELL:
165                                         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_BELLOFF - value);
166                                         return 0;
167                         }
168
169                         break;
170         }
171
172         return -1;
173 }
174
175 /*
176  * sunkbd_initialize() checks for a Sun keyboard attached, and determines
177  * its type.
178  */
179
180 static int sunkbd_initialize(struct sunkbd *sunkbd)
181 {
182         sunkbd->reset = -2;
183         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_RESET);
184         wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
185         if (sunkbd->reset <0)
186                 return -1;
187
188         sunkbd->type = sunkbd->reset;
189
190         if (sunkbd->type == 4) {        /* Type 4 keyboard */
191                 sunkbd->layout = -2;
192                 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_LAYOUT);
193                 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->layout >= 0, HZ/4);
194                 if (sunkbd->layout < 0) return -1;
195                 if (sunkbd->layout & SUNKBD_LAYOUT_5_MASK) sunkbd->type = 5;
196         }
197
198         return 0;
199 }
200
201 /*
202  * sunkbd_reinit() sets leds and beeps to a state the computer remembers they
203  * were in.
204  */
205
206 static void sunkbd_reinit(void *data)
207 {
208         struct sunkbd *sunkbd = data;
209
210         wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
211
212         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_SETLED);
213         sunkbd->serio->write(sunkbd->serio,
214                 (!!test_bit(LED_CAPSL, sunkbd->dev.led) << 3) | (!!test_bit(LED_SCROLLL, sunkbd->dev.led) << 2) |
215                 (!!test_bit(LED_COMPOSE, sunkbd->dev.led) << 1) | !!test_bit(LED_NUML, sunkbd->dev.led));
216         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_NOCLICK - !!test_bit(SND_CLICK, sunkbd->dev.snd));
217         sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_BELLOFF - !!test_bit(SND_BELL, sunkbd->dev.snd));
218 }
219
220 /*
221  * sunkbd_connect() probes for a Sun keyboard and fills the necessary structures.
222  */
223
224 static void sunkbd_connect(struct serio *serio, struct serio_dev *dev)
225 {
226         struct sunkbd *sunkbd;
227         int i;
228
229         if ((serio->type & SERIO_TYPE) != SERIO_RS232)
230                 return;
231
232         if ((serio->type & SERIO_PROTO) && (serio->type & SERIO_PROTO) != SERIO_SUNKBD)
233                 return;
234
235         if (!(sunkbd = kmalloc(sizeof(struct sunkbd), GFP_KERNEL)))
236                 return;
237
238         memset(sunkbd, 0, sizeof(struct sunkbd));
239
240         init_input_dev(&sunkbd->dev);
241         init_waitqueue_head(&sunkbd->wait);
242
243         sunkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_SND) | BIT(EV_REP);
244         sunkbd->dev.ledbit[0] = BIT(LED_CAPSL) | BIT(LED_COMPOSE) | BIT(LED_SCROLLL) | BIT(LED_NUML);
245         sunkbd->dev.sndbit[0] = BIT(SND_CLICK) | BIT(SND_BELL);
246
247         sunkbd->serio = serio;
248
249         INIT_WORK(&sunkbd->tq, sunkbd_reinit, sunkbd);
250
251         sunkbd->dev.keycode = sunkbd->keycode;
252         sunkbd->dev.keycodesize = sizeof(unsigned char);
253         sunkbd->dev.keycodemax = ARRAY_SIZE(sunkbd_keycode);
254
255         sunkbd->dev.event = sunkbd_event;
256         sunkbd->dev.private = sunkbd;
257
258         serio->private = sunkbd;
259
260         if (serio_open(serio, dev)) {
261                 kfree(sunkbd);
262                 return;
263         }
264
265         if (sunkbd_initialize(sunkbd) < 0) {
266                 serio_close(serio);
267                 kfree(sunkbd);
268                 return;
269         }
270
271         sprintf(sunkbd->name, "Sun Type %d keyboard", sunkbd->type);
272
273         memcpy(sunkbd->keycode, sunkbd_keycode, sizeof(sunkbd->keycode));
274         for (i = 0; i < 128; i++)
275                 set_bit(sunkbd->keycode[i], sunkbd->dev.keybit);
276         clear_bit(0, sunkbd->dev.keybit);
277
278         sprintf(sunkbd->phys, "%s/input0", serio->phys);
279
280         sunkbd->dev.name = sunkbd->name;
281         sunkbd->dev.phys = sunkbd->phys;
282         sunkbd->dev.id.bustype = BUS_RS232;
283         sunkbd->dev.id.vendor = SERIO_SUNKBD;
284         sunkbd->dev.id.product = sunkbd->type;
285         sunkbd->dev.id.version = 0x0100;
286
287         input_register_device(&sunkbd->dev);
288
289         printk(KERN_INFO "input: %s on %s\n", sunkbd->name, serio->phys);
290 }
291
292 /*
293  * sunkbd_disconnect() unregisters and closes behind us.
294  */
295
296 static void sunkbd_disconnect(struct serio *serio)
297 {
298         struct sunkbd *sunkbd = serio->private;
299         input_unregister_device(&sunkbd->dev);
300         serio_close(serio);
301         kfree(sunkbd);
302 }
303
304 static struct serio_dev sunkbd_dev = {
305         .interrupt =    sunkbd_interrupt,
306         .connect =      sunkbd_connect,
307         .disconnect =   sunkbd_disconnect
308 };
309
310 /*
311  * The functions for insering/removing us as a module.
312  */
313
314 int __init sunkbd_init(void)
315 {
316         serio_register_device(&sunkbd_dev);
317         return 0;
318 }
319
320 void __exit sunkbd_exit(void)
321 {
322         serio_unregister_device(&sunkbd_dev);
323 }
324
325 module_init(sunkbd_init);
326 module_exit(sunkbd_exit);