2 * $Id: sunkbd.c,v 1.14 2001/09/25 10:12:07 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
8 * Sun keyboard driver for Linux
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.
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.
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
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
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>
40 #define DRIVER_DESC "Sun keyboard driver"
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
46 static unsigned char sunkbd_keycode[128] = {
47 0,128,114,129,115, 59, 60, 68, 61, 87, 62, 88, 63,100, 64,112,
48 65, 66, 67, 56,103,119, 99, 70,105,130,131,108,106, 1, 2, 3,
49 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 41, 14,110,113, 98, 55,
50 116,132, 83,133,102, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
51 26, 27,111,127, 71, 72, 73, 74,134,135,107, 0, 29, 30, 31, 32,
52 33, 34, 35, 36, 37, 38, 39, 40, 43, 28, 96, 75, 76, 77, 82,136,
53 104,137, 69, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,101,
54 79, 80, 81, 0, 0, 0,138, 58,125, 57,126,109, 86, 78
57 #define SUNKBD_CMD_RESET 0x1
58 #define SUNKBD_CMD_BELLON 0x2
59 #define SUNKBD_CMD_BELLOFF 0x3
60 #define SUNKBD_CMD_CLICK 0xa
61 #define SUNKBD_CMD_NOCLICK 0xb
62 #define SUNKBD_CMD_SETLED 0xe
63 #define SUNKBD_CMD_LAYOUT 0xf
65 #define SUNKBD_RET_RESET 0xff
66 #define SUNKBD_RET_ALLUP 0x7f
67 #define SUNKBD_RET_LAYOUT 0xfe
69 #define SUNKBD_LAYOUT_5_MASK 0x20
70 #define SUNKBD_RELEASE 0x80
71 #define SUNKBD_KEY 0x7f
78 unsigned char keycode[128];
79 struct input_dev *dev;
81 struct work_struct tq;
82 wait_queue_head_t wait;
86 unsigned char enabled;
92 * sunkbd_interrupt() is called by the low level driver when a character
96 static irqreturn_t sunkbd_interrupt(struct serio *serio,
97 unsigned char data, unsigned int flags, struct pt_regs *regs)
99 struct sunkbd* sunkbd = serio_get_drvdata(serio);
101 if (sunkbd->reset <= -1) { /* If cp[i] is 0xff, sunkbd->reset will stay -1. */
102 sunkbd->reset = data; /* The keyboard sends 0xff 0xff 0xID on powerup */
103 wake_up_interruptible(&sunkbd->wait);
107 if (sunkbd->layout == -1) {
108 sunkbd->layout = data;
109 wake_up_interruptible(&sunkbd->wait);
115 case SUNKBD_RET_RESET:
116 schedule_work(&sunkbd->tq);
120 case SUNKBD_RET_LAYOUT:
124 case SUNKBD_RET_ALLUP: /* All keys released */
128 if (!sunkbd->enabled)
131 if (sunkbd->keycode[data & SUNKBD_KEY]) {
132 input_regs(sunkbd->dev, regs);
133 input_report_key(sunkbd->dev, sunkbd->keycode[data & SUNKBD_KEY], !(data & SUNKBD_RELEASE));
134 input_sync(sunkbd->dev);
136 printk(KERN_WARNING "sunkbd.c: Unknown key (scancode %#x) %s.\n",
137 data & SUNKBD_KEY, data & SUNKBD_RELEASE ? "released" : "pressed");
145 * sunkbd_event() handles events from the input module.
148 static int sunkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
150 struct sunkbd *sunkbd = dev->private;
156 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_SETLED);
157 sunkbd->serio->write(sunkbd->serio,
158 (!!test_bit(LED_CAPSL, dev->led) << 3) | (!!test_bit(LED_SCROLLL, dev->led) << 2) |
159 (!!test_bit(LED_COMPOSE, dev->led) << 1) | !!test_bit(LED_NUML, dev->led));
167 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_NOCLICK - value);
171 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_BELLOFF - value);
182 * sunkbd_initialize() checks for a Sun keyboard attached, and determines
186 static int sunkbd_initialize(struct sunkbd *sunkbd)
189 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_RESET);
190 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
191 if (sunkbd->reset < 0)
194 sunkbd->type = sunkbd->reset;
196 if (sunkbd->type == 4) { /* Type 4 keyboard */
198 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_LAYOUT);
199 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->layout >= 0, HZ/4);
200 if (sunkbd->layout < 0) return -1;
201 if (sunkbd->layout & SUNKBD_LAYOUT_5_MASK) sunkbd->type = 5;
208 * sunkbd_reinit() sets leds and beeps to a state the computer remembers they
212 static void sunkbd_reinit(void *data)
214 struct sunkbd *sunkbd = data;
216 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
218 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_SETLED);
219 sunkbd->serio->write(sunkbd->serio,
220 (!!test_bit(LED_CAPSL, sunkbd->dev->led) << 3) | (!!test_bit(LED_SCROLLL, sunkbd->dev->led) << 2) |
221 (!!test_bit(LED_COMPOSE, sunkbd->dev->led) << 1) | !!test_bit(LED_NUML, sunkbd->dev->led));
222 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_NOCLICK - !!test_bit(SND_CLICK, sunkbd->dev->snd));
223 sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_BELLOFF - !!test_bit(SND_BELL, sunkbd->dev->snd));
226 static void sunkbd_enable(struct sunkbd *sunkbd, int enable)
228 serio_pause_rx(sunkbd->serio);
230 serio_continue_rx(sunkbd->serio);
234 * sunkbd_connect() probes for a Sun keyboard and fills the necessary structures.
237 static int sunkbd_connect(struct serio *serio, struct serio_driver *drv)
239 struct sunkbd *sunkbd;
240 struct input_dev *input_dev;
244 sunkbd = kzalloc(sizeof(struct sunkbd), GFP_KERNEL);
245 input_dev = input_allocate_device();
246 if (!sunkbd || !input_dev)
249 sunkbd->serio = serio;
250 sunkbd->dev = input_dev;
251 init_waitqueue_head(&sunkbd->wait);
252 INIT_WORK(&sunkbd->tq, sunkbd_reinit, sunkbd);
253 snprintf(sunkbd->phys, sizeof(sunkbd->phys), "%s/input0", serio->phys);
255 serio_set_drvdata(serio, sunkbd);
257 err = serio_open(serio, drv);
261 if (sunkbd_initialize(sunkbd) < 0) {
266 sprintf(sunkbd->name, "Sun Type %d keyboard", sunkbd->type);
267 memcpy(sunkbd->keycode, sunkbd_keycode, sizeof(sunkbd->keycode));
269 input_dev->name = sunkbd->name;
270 input_dev->phys = sunkbd->phys;
271 input_dev->id.bustype = BUS_RS232;
272 input_dev->id.vendor = SERIO_SUNKBD;
273 input_dev->id.product = sunkbd->type;
274 input_dev->id.version = 0x0100;
275 input_dev->cdev.dev = &serio->dev;
276 input_dev->private = sunkbd;
277 input_dev->event = sunkbd_event;
279 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_SND) | BIT(EV_REP);
280 input_dev->ledbit[0] = BIT(LED_CAPSL) | BIT(LED_COMPOSE) | BIT(LED_SCROLLL) | BIT(LED_NUML);
281 input_dev->sndbit[0] = BIT(SND_CLICK) | BIT(SND_BELL);
283 input_dev->keycode = sunkbd->keycode;
284 input_dev->keycodesize = sizeof(unsigned char);
285 input_dev->keycodemax = ARRAY_SIZE(sunkbd_keycode);
286 for (i = 0; i < 128; i++)
287 set_bit(sunkbd->keycode[i], input_dev->keybit);
288 clear_bit(0, input_dev->keybit);
290 sunkbd_enable(sunkbd, 1);
291 input_register_device(sunkbd->dev);
294 fail: serio_set_drvdata(serio, NULL);
295 input_free_device(input_dev);
301 * sunkbd_disconnect() unregisters and closes behind us.
304 static void sunkbd_disconnect(struct serio *serio)
306 struct sunkbd *sunkbd = serio_get_drvdata(serio);
308 sunkbd_enable(sunkbd, 0);
309 input_unregister_device(sunkbd->dev);
311 serio_set_drvdata(serio, NULL);
315 static struct serio_device_id sunkbd_serio_ids[] = {
318 .proto = SERIO_SUNKBD,
324 .proto = SERIO_UNKNOWN, /* sunkbd does probe */
331 MODULE_DEVICE_TABLE(serio, sunkbd_serio_ids);
333 static struct serio_driver sunkbd_drv = {
337 .description = DRIVER_DESC,
338 .id_table = sunkbd_serio_ids,
339 .interrupt = sunkbd_interrupt,
340 .connect = sunkbd_connect,
341 .disconnect = sunkbd_disconnect,
345 * The functions for insering/removing us as a module.
348 static int __init sunkbd_init(void)
350 serio_register_driver(&sunkbd_drv);
354 static void __exit sunkbd_exit(void)
356 serio_unregister_driver(&sunkbd_drv);
359 module_init(sunkbd_init);
360 module_exit(sunkbd_exit);