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
25#include <linux/delay.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/module.h>
29#include <linux/interrupt.h>
30#include <linux/input.h>
31#include <linux/serio.h>
32#include <linux/workqueue.h>
33
34#define DRIVER_DESC "Sun keyboard driver"
35
36MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
37MODULE_DESCRIPTION(DRIVER_DESC);
38MODULE_LICENSE("GPL");
39
40static unsigned char sunkbd_keycode[128] = {
41 0,128,114,129,115, 59, 60, 68, 61, 87, 62, 88, 63,100, 64,112,
42 65, 66, 67, 56,103,119, 99, 70,105,130,131,108,106, 1, 2, 3,
43 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 41, 14,110,113, 98, 55,
44 116,132, 83,133,102, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
45 26, 27,111,127, 71, 72, 73, 74,134,135,107, 0, 29, 30, 31, 32,
46 33, 34, 35, 36, 37, 38, 39, 40, 43, 28, 96, 75, 76, 77, 82,136,
47 104,137, 69, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,101,
48 79, 80, 81, 0, 0, 0,138, 58,125, 57,126,109, 86, 78
49};
50
51#define SUNKBD_CMD_RESET 0x1
52#define SUNKBD_CMD_BELLON 0x2
53#define SUNKBD_CMD_BELLOFF 0x3
54#define SUNKBD_CMD_CLICK 0xa
55#define SUNKBD_CMD_NOCLICK 0xb
56#define SUNKBD_CMD_SETLED 0xe
57#define SUNKBD_CMD_LAYOUT 0xf
58
59#define SUNKBD_RET_RESET 0xff
60#define SUNKBD_RET_ALLUP 0x7f
61#define SUNKBD_RET_LAYOUT 0xfe
62
63#define SUNKBD_LAYOUT_5_MASK 0x20
64#define SUNKBD_RELEASE 0x80
65#define SUNKBD_KEY 0x7f
66
67/*
68 * Per-keyboard data.
69 */
70
71struct sunkbd {
72 unsigned char keycode[ARRAY_SIZE(sunkbd_keycode)];
73 struct input_dev *dev;
74 struct serio *serio;
75 struct work_struct tq;
76 wait_queue_head_t wait;
77 char name[64];
78 char phys[32];
79 char type;
80 bool enabled;
81 volatile s8 reset;
82 volatile s8 layout;
83};
84
85/*
86 * sunkbd_interrupt() is called by the low level driver when a character
87 * is received.
88 */
89
90static irqreturn_t sunkbd_interrupt(struct serio *serio,
91 unsigned char data, unsigned int flags)
92{
93 struct sunkbd *sunkbd = serio_get_drvdata(serio);
94
95 if (sunkbd->reset <= -1) {
96 /*
97 * If cp[i] is 0xff, sunkbd->reset will stay -1.
98 * The keyboard sends 0xff 0xff 0xID on powerup.
99 */
100 sunkbd->reset = data;
101 wake_up_interruptible(&sunkbd->wait);
102 goto out;
103 }
104
105 if (sunkbd->layout == -1) {
106 sunkbd->layout = data;
107 wake_up_interruptible(&sunkbd->wait);
108 goto out;
109 }
110
111 switch (data) {
112
113 case SUNKBD_RET_RESET:
114 schedule_work(&sunkbd->tq);
115 sunkbd->reset = -1;
116 break;
117
118 case SUNKBD_RET_LAYOUT:
119 sunkbd->layout = -1;
120 break;
121
122 case SUNKBD_RET_ALLUP: /* All keys released */
123 break;
124
125 default:
126 if (!sunkbd->enabled)
127 break;
128
129 if (sunkbd->keycode[data & SUNKBD_KEY]) {
130 input_report_key(sunkbd->dev,
131 sunkbd->keycode[data & SUNKBD_KEY],
132 !(data & SUNKBD_RELEASE));
133 input_sync(sunkbd->dev);
134 } else {
135 printk(KERN_WARNING
136 "sunkbd.c: Unknown key (scancode %#x) %s.\n",
137 data & SUNKBD_KEY,
138 data & SUNKBD_RELEASE ? "released" : "pressed");
139 }
140 }
141out:
142 return IRQ_HANDLED;
143}
144
145/*
146 * sunkbd_event() handles events from the input module.
147 */
148
149static int sunkbd_event(struct input_dev *dev,
150 unsigned int type, unsigned int code, int value)
151{
152 struct sunkbd *sunkbd = input_get_drvdata(dev);
153
154 switch (type) {
155
156 case EV_LED:
157
158 serio_write(sunkbd->serio, SUNKBD_CMD_SETLED);
159 serio_write(sunkbd->serio,
160 (!!test_bit(LED_CAPSL, dev->led) << 3) |
161 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
162 (!!test_bit(LED_COMPOSE, dev->led) << 1) |
163 !!test_bit(LED_NUML, dev->led));
164 return 0;
165
166 case EV_SND:
167
168 switch (code) {
169
170 case SND_CLICK:
171 serio_write(sunkbd->serio, SUNKBD_CMD_NOCLICK - value);
172 return 0;
173
174 case SND_BELL:
175 serio_write(sunkbd->serio, SUNKBD_CMD_BELLOFF - value);
176 return 0;
177 }
178
179 break;
180 }
181
182 return -1;
183}
184
185/*
186 * sunkbd_initialize() checks for a Sun keyboard attached, and determines
187 * its type.
188 */
189
190static int sunkbd_initialize(struct sunkbd *sunkbd)
191{
192 sunkbd->reset = -2;
193 serio_write(sunkbd->serio, SUNKBD_CMD_RESET);
194 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
195 if (sunkbd->reset < 0)
196 return -1;
197
198 sunkbd->type = sunkbd->reset;
199
200 if (sunkbd->type == 4) { /* Type 4 keyboard */
201 sunkbd->layout = -2;
202 serio_write(sunkbd->serio, SUNKBD_CMD_LAYOUT);
203 wait_event_interruptible_timeout(sunkbd->wait,
204 sunkbd->layout >= 0, HZ / 4);
205 if (sunkbd->layout < 0)
206 return -1;
207 if (sunkbd->layout & SUNKBD_LAYOUT_5_MASK)
208 sunkbd->type = 5;
209 }
210
211 return 0;
212}
213
214/*
215 * sunkbd_reinit() sets leds and beeps to a state the computer remembers they
216 * were in.
217 */
218
219static void sunkbd_reinit(struct work_struct *work)
220{
221 struct sunkbd *sunkbd = container_of(work, struct sunkbd, tq);
222
223 wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
224
225 serio_write(sunkbd->serio, SUNKBD_CMD_SETLED);
226 serio_write(sunkbd->serio,
227 (!!test_bit(LED_CAPSL, sunkbd->dev->led) << 3) |
228 (!!test_bit(LED_SCROLLL, sunkbd->dev->led) << 2) |
229 (!!test_bit(LED_COMPOSE, sunkbd->dev->led) << 1) |
230 !!test_bit(LED_NUML, sunkbd->dev->led));
231 serio_write(sunkbd->serio,
232 SUNKBD_CMD_NOCLICK - !!test_bit(SND_CLICK, sunkbd->dev->snd));
233 serio_write(sunkbd->serio,
234 SUNKBD_CMD_BELLOFF - !!test_bit(SND_BELL, sunkbd->dev->snd));
235}
236
237static void sunkbd_enable(struct sunkbd *sunkbd, bool enable)
238{
239 serio_pause_rx(sunkbd->serio);
240 sunkbd->enabled = enable;
241 serio_continue_rx(sunkbd->serio);
242}
243
244/*
245 * sunkbd_connect() probes for a Sun keyboard and fills the necessary
246 * structures.
247 */
248
249static int sunkbd_connect(struct serio *serio, struct serio_driver *drv)
250{
251 struct sunkbd *sunkbd;
252 struct input_dev *input_dev;
253 int err = -ENOMEM;
254 int i;
255
256 sunkbd = kzalloc(sizeof(struct sunkbd), GFP_KERNEL);
257 input_dev = input_allocate_device();
258 if (!sunkbd || !input_dev)
259 goto fail1;
260
261 sunkbd->serio = serio;
262 sunkbd->dev = input_dev;
263 init_waitqueue_head(&sunkbd->wait);
264 INIT_WORK(&sunkbd->tq, sunkbd_reinit);
265 snprintf(sunkbd->phys, sizeof(sunkbd->phys), "%s/input0", serio->phys);
266
267 serio_set_drvdata(serio, sunkbd);
268
269 err = serio_open(serio, drv);
270 if (err)
271 goto fail2;
272
273 if (sunkbd_initialize(sunkbd) < 0) {
274 err = -ENODEV;
275 goto fail3;
276 }
277
278 snprintf(sunkbd->name, sizeof(sunkbd->name),
279 "Sun Type %d keyboard", sunkbd->type);
280 memcpy(sunkbd->keycode, sunkbd_keycode, sizeof(sunkbd->keycode));
281
282 input_dev->name = sunkbd->name;
283 input_dev->phys = sunkbd->phys;
284 input_dev->id.bustype = BUS_RS232;
285 input_dev->id.vendor = SERIO_SUNKBD;
286 input_dev->id.product = sunkbd->type;
287 input_dev->id.version = 0x0100;
288 input_dev->dev.parent = &serio->dev;
289
290 input_set_drvdata(input_dev, sunkbd);
291
292 input_dev->event = sunkbd_event;
293
294 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
295 BIT_MASK(EV_SND) | BIT_MASK(EV_REP);
296 input_dev->ledbit[0] = BIT_MASK(LED_CAPSL) | BIT_MASK(LED_COMPOSE) |
297 BIT_MASK(LED_SCROLLL) | BIT_MASK(LED_NUML);
298 input_dev->sndbit[0] = BIT_MASK(SND_CLICK) | BIT_MASK(SND_BELL);
299
300 input_dev->keycode = sunkbd->keycode;
301 input_dev->keycodesize = sizeof(unsigned char);
302 input_dev->keycodemax = ARRAY_SIZE(sunkbd_keycode);
303 for (i = 0; i < ARRAY_SIZE(sunkbd_keycode); i++)
304 __set_bit(sunkbd->keycode[i], input_dev->keybit);
305 __clear_bit(KEY_RESERVED, input_dev->keybit);
306
307 sunkbd_enable(sunkbd, true);
308
309 err = input_register_device(sunkbd->dev);
310 if (err)
311 goto fail4;
312
313 return 0;
314
315 fail4: sunkbd_enable(sunkbd, false);
316 fail3: serio_close(serio);
317 fail2: serio_set_drvdata(serio, NULL);
318 fail1: input_free_device(input_dev);
319 kfree(sunkbd);
320 return err;
321}
322
323/*
324 * sunkbd_disconnect() unregisters and closes behind us.
325 */
326
327static void sunkbd_disconnect(struct serio *serio)
328{
329 struct sunkbd *sunkbd = serio_get_drvdata(serio);
330
331 sunkbd_enable(sunkbd, false);
332 input_unregister_device(sunkbd->dev);
333 serio_close(serio);
334 serio_set_drvdata(serio, NULL);
335 kfree(sunkbd);
336}
337
338static const struct serio_device_id sunkbd_serio_ids[] = {
339 {
340 .type = SERIO_RS232,
341 .proto = SERIO_SUNKBD,
342 .id = SERIO_ANY,
343 .extra = SERIO_ANY,
344 },
345 {
346 .type = SERIO_RS232,
347 .proto = SERIO_UNKNOWN, /* sunkbd does probe */
348 .id = SERIO_ANY,
349 .extra = SERIO_ANY,
350 },
351 { 0 }
352};
353
354MODULE_DEVICE_TABLE(serio, sunkbd_serio_ids);
355
356static struct serio_driver sunkbd_drv = {
357 .driver = {
358 .name = "sunkbd",
359 },
360 .description = DRIVER_DESC,
361 .id_table = sunkbd_serio_ids,
362 .interrupt = sunkbd_interrupt,
363 .connect = sunkbd_connect,
364 .disconnect = sunkbd_disconnect,
365};
366
367module_serio_driver(sunkbd_drv);
368