1 /*
2 * Copyright (c) 1999-2001 Vojtech Pavlik
3 */
4
5 /*
6 * Sun keyboard driver for Linux
7 */
8
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27 */
28
29 #include <linux/delay.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/interrupt.h>
34 #include <linux/input.h>
35 #include <linux/serio.h>
36 #include <linux/workqueue.h>
37
38 #define DRIVER_DESC "Sun keyboard driver"
39
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
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,112,
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[ARRAY_SIZE(sunkbd_keycode)];
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 bool enabled;
85 volatile s8 reset;
86 volatile s8 layout;
87 };
88
89 /*
90 * sunkbd_interrupt() is called by the low level driver when a character
91 * is received.
92 */
93
sunkbd_interrupt(struct serio * serio,unsigned char data,unsigned int flags)94 static irqreturn_t sunkbd_interrupt(struct serio *serio,
95 unsigned char data, unsigned int flags)
96 {
97 struct sunkbd *sunkbd = serio_get_drvdata(serio);
98
99 if (sunkbd->reset <= -1) {
100 /*
101 * If cp[i] is 0xff, sunkbd->reset will stay -1.
102 * The keyboard sends 0xff 0xff 0xID on powerup.
103 */
104 sunkbd->reset = data;
105 wake_up_interruptible(&sunkbd->wait);
106 goto out;
107 }
108
109 if (sunkbd->layout == -1) {
110 sunkbd->layout = data;
111 wake_up_interruptible(&sunkbd->wait);
112 goto out;
113 }
114
115 switch (data) {
116
117 case SUNKBD_RET_RESET:
118 if (sunkbd->enabled)
119 schedule_work(&sunkbd->tq);
120 sunkbd->reset = -1;
121 break;
122
123 case SUNKBD_RET_LAYOUT:
124 sunkbd->layout = -1;
125 break;
126
127 case SUNKBD_RET_ALLUP: /* All keys released */
128 break;
129
130 default:
131 if (!sunkbd->enabled)
132 break;
133
134 if (sunkbd->keycode[data & SUNKBD_KEY]) {
135 input_report_key(sunkbd->dev,
136 sunkbd->keycode[data & SUNKBD_KEY],
137 !(data & SUNKBD_RELEASE));
138 input_sync(sunkbd->dev);
139 } else {
140 printk(KERN_WARNING
141 "sunkbd.c: Unknown key (scancode %#x) %s.\n",
142 data & SUNKBD_KEY,
143 data & SUNKBD_RELEASE ? "released" : "pressed");
144 }
145 }
146 out:
147 return IRQ_HANDLED;
148 }
149
150 /*
151 * sunkbd_event() handles events from the input module.
152 */
153
sunkbd_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)154 static int sunkbd_event(struct input_dev *dev,
155 unsigned int type, unsigned int code, int value)
156 {
157 struct sunkbd *sunkbd = input_get_drvdata(dev);
158
159 switch (type) {
160
161 case EV_LED:
162
163 serio_write(sunkbd->serio, SUNKBD_CMD_SETLED);
164 serio_write(sunkbd->serio,
165 (!!test_bit(LED_CAPSL, dev->led) << 3) |
166 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
167 (!!test_bit(LED_COMPOSE, dev->led) << 1) |
168 !!test_bit(LED_NUML, dev->led));
169 return 0;
170
171 case EV_SND:
172
173 switch (code) {
174
175 case SND_CLICK:
176 serio_write(sunkbd->serio, SUNKBD_CMD_NOCLICK - value);
177 return 0;
178
179 case SND_BELL:
180 serio_write(sunkbd->serio, SUNKBD_CMD_BELLOFF - value);
181 return 0;
182 }
183
184 break;
185 }
186
187 return -1;
188 }
189
190 /*
191 * sunkbd_initialize() checks for a Sun keyboard attached, and determines
192 * its type.
193 */
194
sunkbd_initialize(struct sunkbd * sunkbd)195 static int sunkbd_initialize(struct sunkbd *sunkbd)
196 {
197 sunkbd->reset = -2;
198 serio_write(sunkbd->serio, SUNKBD_CMD_RESET);
199 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
200 if (sunkbd->reset < 0)
201 return -1;
202
203 sunkbd->type = sunkbd->reset;
204
205 if (sunkbd->type == 4) { /* Type 4 keyboard */
206 sunkbd->layout = -2;
207 serio_write(sunkbd->serio, SUNKBD_CMD_LAYOUT);
208 wait_event_interruptible_timeout(sunkbd->wait,
209 sunkbd->layout >= 0, HZ / 4);
210 if (sunkbd->layout < 0)
211 return -1;
212 if (sunkbd->layout & SUNKBD_LAYOUT_5_MASK)
213 sunkbd->type = 5;
214 }
215
216 return 0;
217 }
218
219 /*
220 * sunkbd_set_leds_beeps() sets leds and beeps to a state the computer remembers
221 * they were in.
222 */
223
sunkbd_set_leds_beeps(struct sunkbd * sunkbd)224 static void sunkbd_set_leds_beeps(struct sunkbd *sunkbd)
225 {
226 serio_write(sunkbd->serio, SUNKBD_CMD_SETLED);
227 serio_write(sunkbd->serio,
228 (!!test_bit(LED_CAPSL, sunkbd->dev->led) << 3) |
229 (!!test_bit(LED_SCROLLL, sunkbd->dev->led) << 2) |
230 (!!test_bit(LED_COMPOSE, sunkbd->dev->led) << 1) |
231 !!test_bit(LED_NUML, sunkbd->dev->led));
232 serio_write(sunkbd->serio,
233 SUNKBD_CMD_NOCLICK - !!test_bit(SND_CLICK, sunkbd->dev->snd));
234 serio_write(sunkbd->serio,
235 SUNKBD_CMD_BELLOFF - !!test_bit(SND_BELL, sunkbd->dev->snd));
236 }
237
238
239 /*
240 * sunkbd_reinit() wait for the keyboard reset to complete and restores state
241 * of leds and beeps.
242 */
243
sunkbd_reinit(struct work_struct * work)244 static void sunkbd_reinit(struct work_struct *work)
245 {
246 struct sunkbd *sunkbd = container_of(work, struct sunkbd, tq);
247
248 /*
249 * It is OK that we check sunkbd->enabled without pausing serio,
250 * as we only want to catch true->false transition that will
251 * happen once and we will be woken up for it.
252 */
253 wait_event_interruptible_timeout(sunkbd->wait,
254 sunkbd->reset >= 0 || !sunkbd->enabled,
255 HZ);
256
257 if (sunkbd->reset >= 0 && sunkbd->enabled)
258 sunkbd_set_leds_beeps(sunkbd);
259 }
260
sunkbd_enable(struct sunkbd * sunkbd,bool enable)261 static void sunkbd_enable(struct sunkbd *sunkbd, bool enable)
262 {
263 serio_pause_rx(sunkbd->serio);
264 sunkbd->enabled = enable;
265 serio_continue_rx(sunkbd->serio);
266
267 if (!enable) {
268 wake_up_interruptible(&sunkbd->wait);
269 cancel_work_sync(&sunkbd->tq);
270 }
271 }
272
273 /*
274 * sunkbd_connect() probes for a Sun keyboard and fills the necessary
275 * structures.
276 */
277
sunkbd_connect(struct serio * serio,struct serio_driver * drv)278 static int sunkbd_connect(struct serio *serio, struct serio_driver *drv)
279 {
280 struct sunkbd *sunkbd;
281 struct input_dev *input_dev;
282 int err = -ENOMEM;
283 int i;
284
285 sunkbd = kzalloc(sizeof(struct sunkbd), GFP_KERNEL);
286 input_dev = input_allocate_device();
287 if (!sunkbd || !input_dev)
288 goto fail1;
289
290 sunkbd->serio = serio;
291 sunkbd->dev = input_dev;
292 init_waitqueue_head(&sunkbd->wait);
293 INIT_WORK(&sunkbd->tq, sunkbd_reinit);
294 snprintf(sunkbd->phys, sizeof(sunkbd->phys), "%s/input0", serio->phys);
295
296 serio_set_drvdata(serio, sunkbd);
297
298 err = serio_open(serio, drv);
299 if (err)
300 goto fail2;
301
302 if (sunkbd_initialize(sunkbd) < 0) {
303 err = -ENODEV;
304 goto fail3;
305 }
306
307 snprintf(sunkbd->name, sizeof(sunkbd->name),
308 "Sun Type %d keyboard", sunkbd->type);
309 memcpy(sunkbd->keycode, sunkbd_keycode, sizeof(sunkbd->keycode));
310
311 input_dev->name = sunkbd->name;
312 input_dev->phys = sunkbd->phys;
313 input_dev->id.bustype = BUS_RS232;
314 input_dev->id.vendor = SERIO_SUNKBD;
315 input_dev->id.product = sunkbd->type;
316 input_dev->id.version = 0x0100;
317 input_dev->dev.parent = &serio->dev;
318
319 input_set_drvdata(input_dev, sunkbd);
320
321 input_dev->event = sunkbd_event;
322
323 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
324 BIT_MASK(EV_SND) | BIT_MASK(EV_REP);
325 input_dev->ledbit[0] = BIT_MASK(LED_CAPSL) | BIT_MASK(LED_COMPOSE) |
326 BIT_MASK(LED_SCROLLL) | BIT_MASK(LED_NUML);
327 input_dev->sndbit[0] = BIT_MASK(SND_CLICK) | BIT_MASK(SND_BELL);
328
329 input_dev->keycode = sunkbd->keycode;
330 input_dev->keycodesize = sizeof(unsigned char);
331 input_dev->keycodemax = ARRAY_SIZE(sunkbd_keycode);
332 for (i = 0; i < ARRAY_SIZE(sunkbd_keycode); i++)
333 __set_bit(sunkbd->keycode[i], input_dev->keybit);
334 __clear_bit(KEY_RESERVED, input_dev->keybit);
335
336 sunkbd_enable(sunkbd, true);
337
338 err = input_register_device(sunkbd->dev);
339 if (err)
340 goto fail4;
341
342 return 0;
343
344 fail4: sunkbd_enable(sunkbd, false);
345 fail3: serio_close(serio);
346 fail2: serio_set_drvdata(serio, NULL);
347 fail1: input_free_device(input_dev);
348 kfree(sunkbd);
349 return err;
350 }
351
352 /*
353 * sunkbd_disconnect() unregisters and closes behind us.
354 */
355
sunkbd_disconnect(struct serio * serio)356 static void sunkbd_disconnect(struct serio *serio)
357 {
358 struct sunkbd *sunkbd = serio_get_drvdata(serio);
359
360 sunkbd_enable(sunkbd, false);
361 input_unregister_device(sunkbd->dev);
362 serio_close(serio);
363 serio_set_drvdata(serio, NULL);
364 kfree(sunkbd);
365 }
366
367 static struct serio_device_id sunkbd_serio_ids[] = {
368 {
369 .type = SERIO_RS232,
370 .proto = SERIO_SUNKBD,
371 .id = SERIO_ANY,
372 .extra = SERIO_ANY,
373 },
374 {
375 .type = SERIO_RS232,
376 .proto = SERIO_UNKNOWN, /* sunkbd does probe */
377 .id = SERIO_ANY,
378 .extra = SERIO_ANY,
379 },
380 { 0 }
381 };
382
383 MODULE_DEVICE_TABLE(serio, sunkbd_serio_ids);
384
385 static struct serio_driver sunkbd_drv = {
386 .driver = {
387 .name = "sunkbd",
388 },
389 .description = DRIVER_DESC,
390 .id_table = sunkbd_serio_ids,
391 .interrupt = sunkbd_interrupt,
392 .connect = sunkbd_connect,
393 .disconnect = sunkbd_disconnect,
394 };
395
396 module_serio_driver(sunkbd_drv);
397