1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1999-2001 Vojtech Pavlik
7 * Sun keyboard driver for Linux
10 #include <linux/delay.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/input.h>
16 #include <linux/serio.h>
17 #include <linux/workqueue.h>
19 #define DRIVER_DESC "Sun keyboard driver"
21 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
22 MODULE_DESCRIPTION(DRIVER_DESC
);
23 MODULE_LICENSE("GPL");
25 static unsigned char sunkbd_keycode
[128] = {
26 0,128,114,129,115, 59, 60, 68, 61, 87, 62, 88, 63,100, 64,112,
27 65, 66, 67, 56,103,119, 99, 70,105,130,131,108,106, 1, 2, 3,
28 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 41, 14,110,113, 98, 55,
29 116,132, 83,133,102, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
30 26, 27,111,127, 71, 72, 73, 74,134,135,107, 0, 29, 30, 31, 32,
31 33, 34, 35, 36, 37, 38, 39, 40, 43, 28, 96, 75, 76, 77, 82,136,
32 104,137, 69, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,101,
33 79, 80, 81, 0, 0, 0,138, 58,125, 57,126,109, 86, 78
36 #define SUNKBD_CMD_RESET 0x1
37 #define SUNKBD_CMD_BELLON 0x2
38 #define SUNKBD_CMD_BELLOFF 0x3
39 #define SUNKBD_CMD_CLICK 0xa
40 #define SUNKBD_CMD_NOCLICK 0xb
41 #define SUNKBD_CMD_SETLED 0xe
42 #define SUNKBD_CMD_LAYOUT 0xf
44 #define SUNKBD_RET_RESET 0xff
45 #define SUNKBD_RET_ALLUP 0x7f
46 #define SUNKBD_RET_LAYOUT 0xfe
48 #define SUNKBD_LAYOUT_5_MASK 0x20
49 #define SUNKBD_RELEASE 0x80
50 #define SUNKBD_KEY 0x7f
57 unsigned char keycode
[ARRAY_SIZE(sunkbd_keycode
)];
58 struct input_dev
*dev
;
60 struct work_struct tq
;
61 wait_queue_head_t wait
;
71 * sunkbd_interrupt() is called by the low level driver when a character
75 static irqreturn_t
sunkbd_interrupt(struct serio
*serio
,
76 unsigned char data
, unsigned int flags
)
78 struct sunkbd
*sunkbd
= serio_get_drvdata(serio
);
80 if (sunkbd
->reset
<= -1) {
82 * If cp[i] is 0xff, sunkbd->reset will stay -1.
83 * The keyboard sends 0xff 0xff 0xID on powerup.
86 wake_up_interruptible(&sunkbd
->wait
);
90 if (sunkbd
->layout
== -1) {
91 sunkbd
->layout
= data
;
92 wake_up_interruptible(&sunkbd
->wait
);
98 case SUNKBD_RET_RESET
:
100 schedule_work(&sunkbd
->tq
);
104 case SUNKBD_RET_LAYOUT
:
108 case SUNKBD_RET_ALLUP
: /* All keys released */
112 if (!sunkbd
->enabled
)
115 if (sunkbd
->keycode
[data
& SUNKBD_KEY
]) {
116 input_report_key(sunkbd
->dev
,
117 sunkbd
->keycode
[data
& SUNKBD_KEY
],
118 !(data
& SUNKBD_RELEASE
));
119 input_sync(sunkbd
->dev
);
122 "sunkbd.c: Unknown key (scancode %#x) %s.\n",
124 data
& SUNKBD_RELEASE
? "released" : "pressed");
132 * sunkbd_event() handles events from the input module.
135 static int sunkbd_event(struct input_dev
*dev
,
136 unsigned int type
, unsigned int code
, int value
)
138 struct sunkbd
*sunkbd
= input_get_drvdata(dev
);
144 serio_write(sunkbd
->serio
, SUNKBD_CMD_SETLED
);
145 serio_write(sunkbd
->serio
,
146 (!!test_bit(LED_CAPSL
, dev
->led
) << 3) |
147 (!!test_bit(LED_SCROLLL
, dev
->led
) << 2) |
148 (!!test_bit(LED_COMPOSE
, dev
->led
) << 1) |
149 !!test_bit(LED_NUML
, dev
->led
));
157 serio_write(sunkbd
->serio
, SUNKBD_CMD_NOCLICK
- value
);
161 serio_write(sunkbd
->serio
, SUNKBD_CMD_BELLOFF
- value
);
172 * sunkbd_initialize() checks for a Sun keyboard attached, and determines
176 static int sunkbd_initialize(struct sunkbd
*sunkbd
)
179 serio_write(sunkbd
->serio
, SUNKBD_CMD_RESET
);
180 wait_event_interruptible_timeout(sunkbd
->wait
, sunkbd
->reset
>= 0, HZ
);
181 if (sunkbd
->reset
< 0)
184 sunkbd
->type
= sunkbd
->reset
;
186 if (sunkbd
->type
== 4) { /* Type 4 keyboard */
188 serio_write(sunkbd
->serio
, SUNKBD_CMD_LAYOUT
);
189 wait_event_interruptible_timeout(sunkbd
->wait
,
190 sunkbd
->layout
>= 0, HZ
/ 4);
191 if (sunkbd
->layout
< 0)
193 if (sunkbd
->layout
& SUNKBD_LAYOUT_5_MASK
)
201 * sunkbd_set_leds_beeps() sets leds and beeps to a state the computer remembers
205 static void sunkbd_set_leds_beeps(struct sunkbd
*sunkbd
)
207 serio_write(sunkbd
->serio
, SUNKBD_CMD_SETLED
);
208 serio_write(sunkbd
->serio
,
209 (!!test_bit(LED_CAPSL
, sunkbd
->dev
->led
) << 3) |
210 (!!test_bit(LED_SCROLLL
, sunkbd
->dev
->led
) << 2) |
211 (!!test_bit(LED_COMPOSE
, sunkbd
->dev
->led
) << 1) |
212 !!test_bit(LED_NUML
, sunkbd
->dev
->led
));
213 serio_write(sunkbd
->serio
,
214 SUNKBD_CMD_NOCLICK
- !!test_bit(SND_CLICK
, sunkbd
->dev
->snd
));
215 serio_write(sunkbd
->serio
,
216 SUNKBD_CMD_BELLOFF
- !!test_bit(SND_BELL
, sunkbd
->dev
->snd
));
221 * sunkbd_reinit() wait for the keyboard reset to complete and restores state
225 static void sunkbd_reinit(struct work_struct
*work
)
227 struct sunkbd
*sunkbd
= container_of(work
, struct sunkbd
, tq
);
230 * It is OK that we check sunkbd->enabled without pausing serio,
231 * as we only want to catch true->false transition that will
232 * happen once and we will be woken up for it.
234 wait_event_interruptible_timeout(sunkbd
->wait
,
235 sunkbd
->reset
>= 0 || !sunkbd
->enabled
,
238 if (sunkbd
->reset
>= 0 && sunkbd
->enabled
)
239 sunkbd_set_leds_beeps(sunkbd
);
242 static void sunkbd_enable(struct sunkbd
*sunkbd
, bool enable
)
244 serio_pause_rx(sunkbd
->serio
);
245 sunkbd
->enabled
= enable
;
246 serio_continue_rx(sunkbd
->serio
);
249 wake_up_interruptible(&sunkbd
->wait
);
250 cancel_work_sync(&sunkbd
->tq
);
255 * sunkbd_connect() probes for a Sun keyboard and fills the necessary
259 static int sunkbd_connect(struct serio
*serio
, struct serio_driver
*drv
)
261 struct sunkbd
*sunkbd
;
262 struct input_dev
*input_dev
;
266 sunkbd
= kzalloc(sizeof(*sunkbd
), GFP_KERNEL
);
267 input_dev
= input_allocate_device();
268 if (!sunkbd
|| !input_dev
)
271 sunkbd
->serio
= serio
;
272 sunkbd
->dev
= input_dev
;
273 init_waitqueue_head(&sunkbd
->wait
);
274 INIT_WORK(&sunkbd
->tq
, sunkbd_reinit
);
275 snprintf(sunkbd
->phys
, sizeof(sunkbd
->phys
), "%s/input0", serio
->phys
);
277 serio_set_drvdata(serio
, sunkbd
);
279 err
= serio_open(serio
, drv
);
283 if (sunkbd_initialize(sunkbd
) < 0) {
288 snprintf(sunkbd
->name
, sizeof(sunkbd
->name
),
289 "Sun Type %d keyboard", sunkbd
->type
);
290 memcpy(sunkbd
->keycode
, sunkbd_keycode
, sizeof(sunkbd
->keycode
));
292 input_dev
->name
= sunkbd
->name
;
293 input_dev
->phys
= sunkbd
->phys
;
294 input_dev
->id
.bustype
= BUS_RS232
;
295 input_dev
->id
.vendor
= SERIO_SUNKBD
;
296 input_dev
->id
.product
= sunkbd
->type
;
297 input_dev
->id
.version
= 0x0100;
298 input_dev
->dev
.parent
= &serio
->dev
;
300 input_set_drvdata(input_dev
, sunkbd
);
302 input_dev
->event
= sunkbd_event
;
304 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_LED
) |
305 BIT_MASK(EV_SND
) | BIT_MASK(EV_REP
);
306 input_dev
->ledbit
[0] = BIT_MASK(LED_CAPSL
) | BIT_MASK(LED_COMPOSE
) |
307 BIT_MASK(LED_SCROLLL
) | BIT_MASK(LED_NUML
);
308 input_dev
->sndbit
[0] = BIT_MASK(SND_CLICK
) | BIT_MASK(SND_BELL
);
310 input_dev
->keycode
= sunkbd
->keycode
;
311 input_dev
->keycodesize
= sizeof(unsigned char);
312 input_dev
->keycodemax
= ARRAY_SIZE(sunkbd_keycode
);
313 for (i
= 0; i
< ARRAY_SIZE(sunkbd_keycode
); i
++)
314 __set_bit(sunkbd
->keycode
[i
], input_dev
->keybit
);
315 __clear_bit(KEY_RESERVED
, input_dev
->keybit
);
317 sunkbd_enable(sunkbd
, true);
319 err
= input_register_device(sunkbd
->dev
);
325 fail4
: sunkbd_enable(sunkbd
, false);
326 fail3
: serio_close(serio
);
327 fail2
: serio_set_drvdata(serio
, NULL
);
328 fail1
: input_free_device(input_dev
);
334 * sunkbd_disconnect() unregisters and closes behind us.
337 static void sunkbd_disconnect(struct serio
*serio
)
339 struct sunkbd
*sunkbd
= serio_get_drvdata(serio
);
341 sunkbd_enable(sunkbd
, false);
342 input_unregister_device(sunkbd
->dev
);
344 serio_set_drvdata(serio
, NULL
);
348 static const struct serio_device_id sunkbd_serio_ids
[] = {
351 .proto
= SERIO_SUNKBD
,
357 .proto
= SERIO_UNKNOWN
, /* sunkbd does probe */
364 MODULE_DEVICE_TABLE(serio
, sunkbd_serio_ids
);
366 static struct serio_driver sunkbd_drv
= {
370 .description
= DRIVER_DESC
,
371 .id_table
= sunkbd_serio_ids
,
372 .interrupt
= sunkbd_interrupt
,
373 .connect
= sunkbd_connect
,
374 .disconnect
= sunkbd_disconnect
,
377 module_serio_driver(sunkbd_drv
);