blob: b40cf1bb4e19b3d8421fe705ec24bde258382fc0 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * AT and PS/2 keyboard driver
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 */
12
13/*
14 * This driver can handle standard AT keyboards and PS/2 keyboards in
15 * Translated and Raw Set 2 and Set 3, as well as AT keyboards on dumb
16 * input-only controllers and AT keyboards connected over a one way RS232
17 * converter.
18 */
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/init.h>
25#include <linux/input.h>
26#include <linux/serio.h>
27#include <linux/workqueue.h>
28#include <linux/libps2.h>
29#include <linux/mutex.h>
30#include <linux/dmi.h>
31
32#define DRIVER_DESC "AT and PS/2 keyboard driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
38static int atkbd_set = 2;
39module_param_named(set, atkbd_set, int, 0);
40MODULE_PARM_DESC(set, "Select keyboard code set (2 = default, 3 = PS/2 native)");
41
42#if defined(__i386__) || defined(__x86_64__) || defined(__hppa__)
43static bool atkbd_reset;
44#else
45static bool atkbd_reset = true;
46#endif
47module_param_named(reset, atkbd_reset, bool, 0);
48MODULE_PARM_DESC(reset, "Reset keyboard during initialization");
49
50static bool atkbd_softrepeat;
51module_param_named(softrepeat, atkbd_softrepeat, bool, 0);
52MODULE_PARM_DESC(softrepeat, "Use software keyboard repeat");
53
54static bool atkbd_softraw = true;
55module_param_named(softraw, atkbd_softraw, bool, 0);
56MODULE_PARM_DESC(softraw, "Use software generated rawmode");
57
58static bool atkbd_scroll;
59module_param_named(scroll, atkbd_scroll, bool, 0);
60MODULE_PARM_DESC(scroll, "Enable scroll-wheel on MS Office and similar keyboards");
61
62static bool atkbd_extra;
63module_param_named(extra, atkbd_extra, bool, 0);
64MODULE_PARM_DESC(extra, "Enable extra LEDs and keys on IBM RapidAcces, EzKey and similar keyboards");
65
66static bool atkbd_terminal;
67module_param_named(terminal, atkbd_terminal, bool, 0);
68MODULE_PARM_DESC(terminal, "Enable break codes on an IBM Terminal keyboard connected via AT/PS2");
69
70/*
71 * Scancode to keycode tables. These are just the default setting, and
72 * are loadable via a userland utility.
73 */
74
75#define ATKBD_KEYMAP_SIZE 512
76
77static const unsigned short atkbd_set2_keycode[ATKBD_KEYMAP_SIZE] = {
78
79#ifdef CONFIG_KEYBOARD_ATKBD_HP_KEYCODES
80
81/* XXX: need a more general approach */
82
83#include "hpps2atkbd.h" /* include the keyboard scancodes */
84
85#else
86 0, 67, 65, 63, 61, 59, 60, 88, 0, 68, 66, 64, 62, 15, 41,117,
87 0, 56, 42, 93, 29, 16, 2, 0, 0, 0, 44, 31, 30, 17, 3, 0,
88 0, 46, 45, 32, 18, 5, 4, 95, 0, 57, 47, 33, 20, 19, 6,183,
89 0, 49, 48, 35, 34, 21, 7,184, 0, 0, 50, 36, 22, 8, 9,185,
90 0, 51, 37, 23, 24, 11, 10, 0, 0, 52, 53, 38, 39, 25, 12, 0,
91 0, 89, 40, 0, 26, 13, 0, 0, 58, 54, 28, 27, 0, 43, 0, 85,
92 0, 86, 91, 90, 92, 0, 14, 94, 0, 79,124, 75, 71,121, 0, 0,
93 82, 83, 80, 76, 77, 72, 1, 69, 87, 78, 81, 74, 55, 73, 70, 99,
94
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 217,100,255, 0, 97,165, 0, 0,156, 0, 0, 0, 0, 0, 0,125,
97 173,114, 0,113, 0, 0, 0,126,128, 0, 0,140, 0, 0, 0,127,
98 159, 0,115, 0,164, 0, 0,116,158, 0,172,166, 0, 0, 0,142,
99 157, 0, 0, 0, 0, 0, 0, 0,155, 0, 98, 0, 0,163, 0, 0,
100 226, 0, 0, 0, 0, 0, 0, 0, 0,255, 96, 0, 0, 0,143, 0,
101 0, 0, 0, 0, 0, 0, 0, 0, 0,107, 0,105,102, 0, 0,112,
102 110,111,108,112,106,103, 0,119, 0,118,109, 0, 99,104,119, 0,
103
104 0, 0, 0, 65, 99,
105#endif
106};
107
108static const unsigned short atkbd_set3_keycode[ATKBD_KEYMAP_SIZE] = {
109
110 0, 0, 0, 0, 0, 0, 0, 59, 1,138,128,129,130, 15, 41, 60,
111 131, 29, 42, 86, 58, 16, 2, 61,133, 56, 44, 31, 30, 17, 3, 62,
112 134, 46, 45, 32, 18, 5, 4, 63,135, 57, 47, 33, 20, 19, 6, 64,
113 136, 49, 48, 35, 34, 21, 7, 65,137,100, 50, 36, 22, 8, 9, 66,
114 125, 51, 37, 23, 24, 11, 10, 67,126, 52, 53, 38, 39, 25, 12, 68,
115 113,114, 40, 43, 26, 13, 87, 99, 97, 54, 28, 27, 43, 43, 88, 70,
116 108,105,119,103,111,107, 14,110, 0, 79,106, 75, 71,109,102,104,
117 82, 83, 80, 76, 77, 72, 69, 98, 0, 96, 81, 0, 78, 73, 55,183,
118
119 184,185,186,187, 74, 94, 92, 93, 0, 0, 0,125,126,127,112, 0,
120 0,139,172,163,165,115,152,172,166,140,160,154,113,114,167,168,
121 148,149,147,140
122};
123
124static const unsigned short atkbd_unxlate_table[128] = {
125 0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
126 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
127 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
128 50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88, 5, 6, 4, 12, 3,
129 11, 2, 10, 1, 9,119,126,108,117,125,123,107,115,116,121,105,
130 114,122,112,113,127, 96, 97,120, 7, 15, 23, 31, 39, 47, 55, 63,
131 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
132 19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
133};
134
135#define ATKBD_CMD_SETLEDS 0x10ed
136#define ATKBD_CMD_GSCANSET 0x11f0
137#define ATKBD_CMD_SSCANSET 0x10f0
138#define ATKBD_CMD_GETID 0x02f2
139#define ATKBD_CMD_SETREP 0x10f3
140#define ATKBD_CMD_ENABLE 0x00f4
141#define ATKBD_CMD_RESET_DIS 0x00f5 /* Reset to defaults and disable */
142#define ATKBD_CMD_RESET_DEF 0x00f6 /* Reset to defaults */
143#define ATKBD_CMD_SETALL_MB 0x00f8 /* Set all keys to give break codes */
144#define ATKBD_CMD_SETALL_MBR 0x00fa /* ... and repeat */
145#define ATKBD_CMD_RESET_BAT 0x02ff
146#define ATKBD_CMD_RESEND 0x00fe
147#define ATKBD_CMD_EX_ENABLE 0x10ea
148#define ATKBD_CMD_EX_SETLEDS 0x20eb
149#define ATKBD_CMD_OK_GETID 0x02e8
150
151#define ATKBD_RET_ACK 0xfa
152#define ATKBD_RET_NAK 0xfe
153#define ATKBD_RET_BAT 0xaa
154#define ATKBD_RET_EMUL0 0xe0
155#define ATKBD_RET_EMUL1 0xe1
156#define ATKBD_RET_RELEASE 0xf0
157#define ATKBD_RET_HANJA 0xf1
158#define ATKBD_RET_HANGEUL 0xf2
159#define ATKBD_RET_ERR 0xff
160
161#define ATKBD_KEY_UNKNOWN 0
162#define ATKBD_KEY_NULL 255
163
164#define ATKBD_SCR_1 0xfffe
165#define ATKBD_SCR_2 0xfffd
166#define ATKBD_SCR_4 0xfffc
167#define ATKBD_SCR_8 0xfffb
168#define ATKBD_SCR_CLICK 0xfffa
169#define ATKBD_SCR_LEFT 0xfff9
170#define ATKBD_SCR_RIGHT 0xfff8
171
172#define ATKBD_SPECIAL ATKBD_SCR_RIGHT
173
174#define ATKBD_LED_EVENT_BIT 0
175#define ATKBD_REP_EVENT_BIT 1
176
177#define ATKBD_XL_ERR 0x01
178#define ATKBD_XL_BAT 0x02
179#define ATKBD_XL_ACK 0x04
180#define ATKBD_XL_NAK 0x08
181#define ATKBD_XL_HANGEUL 0x10
182#define ATKBD_XL_HANJA 0x20
183
184static const struct {
185 unsigned short keycode;
186 unsigned char set2;
187} atkbd_scroll_keys[] = {
188 { ATKBD_SCR_1, 0xc5 },
189 { ATKBD_SCR_2, 0x9d },
190 { ATKBD_SCR_4, 0xa4 },
191 { ATKBD_SCR_8, 0x9b },
192 { ATKBD_SCR_CLICK, 0xe0 },
193 { ATKBD_SCR_LEFT, 0xcb },
194 { ATKBD_SCR_RIGHT, 0xd2 },
195};
196
197/*
198 * The atkbd control structure
199 */
200
201struct atkbd {
202
203 struct ps2dev ps2dev;
204 struct input_dev *dev;
205
206 /* Written only during init */
207 char name[64];
208 char phys[32];
209
210 unsigned short id;
211 unsigned short keycode[ATKBD_KEYMAP_SIZE];
212 DECLARE_BITMAP(force_release_mask, ATKBD_KEYMAP_SIZE);
213 unsigned char set;
214 bool translated;
215 bool extra;
216 bool write;
217 bool softrepeat;
218 bool softraw;
219 bool scroll;
220 bool enabled;
221
222 /* Accessed only from interrupt */
223 unsigned char emul;
224 bool resend;
225 bool release;
226 unsigned long xl_bit;
227 unsigned int last;
228 unsigned long time;
229 unsigned long err_count;
230
231 struct delayed_work event_work;
232 unsigned long event_jiffies;
233 unsigned long event_mask;
234
235 /* Serializes reconnect(), attr->set() and event work */
236 struct mutex mutex;
237};
238
239/*
240 * System-specific keymap fixup routine
241 */
242static void (*atkbd_platform_fixup)(struct atkbd *, const void *data);
243static void *atkbd_platform_fixup_data;
244static unsigned int (*atkbd_platform_scancode_fixup)(struct atkbd *, unsigned int);
245
246static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf,
247 ssize_t (*handler)(struct atkbd *, char *));
248static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count,
249 ssize_t (*handler)(struct atkbd *, const char *, size_t));
250#define ATKBD_DEFINE_ATTR(_name) \
251static ssize_t atkbd_show_##_name(struct atkbd *, char *); \
252static ssize_t atkbd_set_##_name(struct atkbd *, const char *, size_t); \
253static ssize_t atkbd_do_show_##_name(struct device *d, \
254 struct device_attribute *attr, char *b) \
255{ \
256 return atkbd_attr_show_helper(d, b, atkbd_show_##_name); \
257} \
258static ssize_t atkbd_do_set_##_name(struct device *d, \
259 struct device_attribute *attr, const char *b, size_t s) \
260{ \
261 return atkbd_attr_set_helper(d, b, s, atkbd_set_##_name); \
262} \
263static struct device_attribute atkbd_attr_##_name = \
264 __ATTR(_name, S_IWUSR | S_IRUGO, atkbd_do_show_##_name, atkbd_do_set_##_name);
265
266ATKBD_DEFINE_ATTR(extra);
267ATKBD_DEFINE_ATTR(force_release);
268ATKBD_DEFINE_ATTR(scroll);
269ATKBD_DEFINE_ATTR(set);
270ATKBD_DEFINE_ATTR(softrepeat);
271ATKBD_DEFINE_ATTR(softraw);
272
273#define ATKBD_DEFINE_RO_ATTR(_name) \
274static ssize_t atkbd_show_##_name(struct atkbd *, char *); \
275static ssize_t atkbd_do_show_##_name(struct device *d, \
276 struct device_attribute *attr, char *b) \
277{ \
278 return atkbd_attr_show_helper(d, b, atkbd_show_##_name); \
279} \
280static struct device_attribute atkbd_attr_##_name = \
281 __ATTR(_name, S_IRUGO, atkbd_do_show_##_name, NULL);
282
283ATKBD_DEFINE_RO_ATTR(err_count);
284
285static struct attribute *atkbd_attributes[] = {
286 &atkbd_attr_extra.attr,
287 &atkbd_attr_force_release.attr,
288 &atkbd_attr_scroll.attr,
289 &atkbd_attr_set.attr,
290 &atkbd_attr_softrepeat.attr,
291 &atkbd_attr_softraw.attr,
292 &atkbd_attr_err_count.attr,
293 NULL
294};
295
296static struct attribute_group atkbd_attribute_group = {
297 .attrs = atkbd_attributes,
298};
299
300static const unsigned int xl_table[] = {
301 ATKBD_RET_BAT, ATKBD_RET_ERR, ATKBD_RET_ACK,
302 ATKBD_RET_NAK, ATKBD_RET_HANJA, ATKBD_RET_HANGEUL,
303};
304
305/*
306 * Checks if we should mangle the scancode to extract 'release' bit
307 * in translated mode.
308 */
309static bool atkbd_need_xlate(unsigned long xl_bit, unsigned char code)
310{
311 int i;
312
313 if (code == ATKBD_RET_EMUL0 || code == ATKBD_RET_EMUL1)
314 return false;
315
316 for (i = 0; i < ARRAY_SIZE(xl_table); i++)
317 if (code == xl_table[i])
318 return test_bit(i, &xl_bit);
319
320 return true;
321}
322
323/*
324 * Calculates new value of xl_bit so the driver can distinguish
325 * between make/break pair of scancodes for select keys and PS/2
326 * protocol responses.
327 */
328static void atkbd_calculate_xl_bit(struct atkbd *atkbd, unsigned char code)
329{
330 int i;
331
332 for (i = 0; i < ARRAY_SIZE(xl_table); i++) {
333 if (!((code ^ xl_table[i]) & 0x7f)) {
334 if (code & 0x80)
335 __clear_bit(i, &atkbd->xl_bit);
336 else
337 __set_bit(i, &atkbd->xl_bit);
338 break;
339 }
340 }
341}
342
343/*
344 * Encode the scancode, 0xe0 prefix, and high bit into a single integer,
345 * keeping kernel 2.4 compatibility for set 2
346 */
347static unsigned int atkbd_compat_scancode(struct atkbd *atkbd, unsigned int code)
348{
349 if (atkbd->set == 3) {
350 if (atkbd->emul == 1)
351 code |= 0x100;
352 } else {
353 code = (code & 0x7f) | ((code & 0x80) << 1);
354 if (atkbd->emul == 1)
355 code |= 0x80;
356 }
357
358 return code;
359}
360
361/*
362 * atkbd_interrupt(). Here takes place processing of data received from
363 * the keyboard into events.
364 */
365
366static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
367 unsigned int flags)
368{
369 struct atkbd *atkbd = serio_get_drvdata(serio);
370 struct input_dev *dev = NULL;
371 unsigned int code = data;
372 int scroll = 0, hscroll = 0, click = -1;
373 int value;
374 unsigned short keycode;
375 if(!atkbd)
376 return -EINVAL;
377
378 dev = atkbd->dev;
379
380 dev_dbg(&serio->dev, "Received %02x flags %02x\n", data, flags);
381
382#if !defined(__i386__) && !defined (__x86_64__)
383 if ((flags & (SERIO_FRAME | SERIO_PARITY)) && (~flags & SERIO_TIMEOUT) && !atkbd->resend && atkbd->write) {
384 dev_warn(&serio->dev, "Frame/parity error: %02x\n", flags);
385 serio_write(serio, ATKBD_CMD_RESEND);
386 atkbd->resend = true;
387 goto out;
388 }
389
390 if (!flags && data == ATKBD_RET_ACK)
391 atkbd->resend = false;
392#endif
393
394 if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_ACK))
395 if (ps2_handle_ack(&atkbd->ps2dev, data))
396 goto out;
397
398 if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_CMD))
399 if (ps2_handle_response(&atkbd->ps2dev, data))
400 goto out;
401
402 if (!atkbd->enabled)
403 goto out;
404
405 input_event(dev, EV_MSC, MSC_RAW, code);
406
407 if (atkbd_platform_scancode_fixup)
408 code = atkbd_platform_scancode_fixup(atkbd, code);
409
410 if (atkbd->translated) {
411
412 if (atkbd->emul || atkbd_need_xlate(atkbd->xl_bit, code)) {
413 atkbd->release = code >> 7;
414 code &= 0x7f;
415 }
416
417 if (!atkbd->emul)
418 atkbd_calculate_xl_bit(atkbd, data);
419 }
420
421 switch (code) {
422 case ATKBD_RET_BAT:
423 atkbd->enabled = false;
424 serio_reconnect(atkbd->ps2dev.serio);
425 goto out;
426 case ATKBD_RET_EMUL0:
427 atkbd->emul = 1;
428 goto out;
429 case ATKBD_RET_EMUL1:
430 atkbd->emul = 2;
431 goto out;
432 case ATKBD_RET_RELEASE:
433 atkbd->release = true;
434 goto out;
435 case ATKBD_RET_ACK:
436 case ATKBD_RET_NAK:
437 if (printk_ratelimit())
438 dev_warn(&serio->dev,
439 "Spurious %s on %s. "
440 "Some program might be trying access hardware directly.\n",
441 data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys);
442 goto out;
443 case ATKBD_RET_ERR:
444 atkbd->err_count++;
445 dev_dbg(&serio->dev, "Keyboard on %s reports too many keys pressed.\n",
446 serio->phys);
447 goto out;
448 }
449
450 code = atkbd_compat_scancode(atkbd, code);
451
452 if (atkbd->emul && --atkbd->emul)
453 goto out;
454
455 keycode = atkbd->keycode[code];
456
457 if (keycode != ATKBD_KEY_NULL)
458 input_event(dev, EV_MSC, MSC_SCAN, code);
459
460 switch (keycode) {
461 case ATKBD_KEY_NULL:
462 break;
463 case ATKBD_KEY_UNKNOWN:
464 dev_warn(&serio->dev,
465 "Unknown key %s (%s set %d, code %#x on %s).\n",
466 atkbd->release ? "released" : "pressed",
467 atkbd->translated ? "translated" : "raw",
468 atkbd->set, code, serio->phys);
469 dev_warn(&serio->dev,
470 "Use 'setkeycodes %s%02x <keycode>' to make it known.\n",
471 code & 0x80 ? "e0" : "", code & 0x7f);
472 input_sync(dev);
473 break;
474 case ATKBD_SCR_1:
475 scroll = 1;
476 break;
477 case ATKBD_SCR_2:
478 scroll = 2;
479 break;
480 case ATKBD_SCR_4:
481 scroll = 4;
482 break;
483 case ATKBD_SCR_8:
484 scroll = 8;
485 break;
486 case ATKBD_SCR_CLICK:
487 click = !atkbd->release;
488 break;
489 case ATKBD_SCR_LEFT:
490 hscroll = -1;
491 break;
492 case ATKBD_SCR_RIGHT:
493 hscroll = 1;
494 break;
495 default:
496 if (atkbd->release) {
497 value = 0;
498 atkbd->last = 0;
499 } else if (!atkbd->softrepeat && test_bit(keycode, dev->key)) {
500 /* Workaround Toshiba laptop multiple keypress */
501 value = time_before(jiffies, atkbd->time) && atkbd->last == code ? 1 : 2;
502 } else {
503 value = 1;
504 atkbd->last = code;
505 atkbd->time = jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]) / 2;
506 }
507
508 input_event(dev, EV_KEY, keycode, value);
509 input_sync(dev);
510
511 if (value && test_bit(code, atkbd->force_release_mask)) {
512 input_report_key(dev, keycode, 0);
513 input_sync(dev);
514 }
515 }
516
517 if (atkbd->scroll) {
518 if (click != -1)
519 input_report_key(dev, BTN_MIDDLE, click);
520 input_report_rel(dev, REL_WHEEL,
521 atkbd->release ? -scroll : scroll);
522 input_report_rel(dev, REL_HWHEEL, hscroll);
523 input_sync(dev);
524 }
525
526 atkbd->release = false;
527out:
528 return IRQ_HANDLED;
529}
530
531static int atkbd_set_repeat_rate(struct atkbd *atkbd)
532{
533 const short period[32] =
534 { 33, 37, 42, 46, 50, 54, 58, 63, 67, 75, 83, 92, 100, 109, 116, 125,
535 133, 149, 167, 182, 200, 217, 232, 250, 270, 303, 333, 370, 400, 435, 470, 500 };
536 const short delay[4] =
537 { 250, 500, 750, 1000 };
538
539 struct input_dev *dev = atkbd->dev;
540 unsigned char param;
541 int i = 0, j = 0;
542
543 while (i < ARRAY_SIZE(period) - 1 && period[i] < dev->rep[REP_PERIOD])
544 i++;
545 dev->rep[REP_PERIOD] = period[i];
546
547 while (j < ARRAY_SIZE(delay) - 1 && delay[j] < dev->rep[REP_DELAY])
548 j++;
549 dev->rep[REP_DELAY] = delay[j];
550
551 param = i | (j << 5);
552 return ps2_command(&atkbd->ps2dev, &param, ATKBD_CMD_SETREP);
553}
554
555static int atkbd_set_leds(struct atkbd *atkbd)
556{
557 struct input_dev *dev = atkbd->dev;
558 unsigned char param[2];
559
560 param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)
561 | (test_bit(LED_NUML, dev->led) ? 2 : 0)
562 | (test_bit(LED_CAPSL, dev->led) ? 4 : 0);
563 if (ps2_command(&atkbd->ps2dev, param, ATKBD_CMD_SETLEDS))
564 return -1;
565
566 if (atkbd->extra) {
567 param[0] = 0;
568 param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0)
569 | (test_bit(LED_SLEEP, dev->led) ? 0x02 : 0)
570 | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0)
571 | (test_bit(LED_MISC, dev->led) ? 0x10 : 0)
572 | (test_bit(LED_MUTE, dev->led) ? 0x20 : 0);
573 if (ps2_command(&atkbd->ps2dev, param, ATKBD_CMD_EX_SETLEDS))
574 return -1;
575 }
576
577 return 0;
578}
579
580/*
581 * atkbd_event_work() is used to complete processing of events that
582 * can not be processed by input_event() which is often called from
583 * interrupt context.
584 */
585
586static void atkbd_event_work(struct work_struct *work)
587{
588 struct atkbd *atkbd = container_of(work, struct atkbd, event_work.work);
589
590 mutex_lock(&atkbd->mutex);
591
592 if (!atkbd->enabled) {
593 /*
594 * Serio ports are resumed asynchronously so while driver core
595 * thinks that device is already fully operational in reality
596 * it may not be ready yet. In this case we need to keep
597 * rescheduling till reconnect completes.
598 */
599 schedule_delayed_work(&atkbd->event_work,
600 msecs_to_jiffies(100));
601 } else {
602 if (test_and_clear_bit(ATKBD_LED_EVENT_BIT, &atkbd->event_mask))
603 atkbd_set_leds(atkbd);
604
605 if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask))
606 atkbd_set_repeat_rate(atkbd);
607 }
608
609 mutex_unlock(&atkbd->mutex);
610}
611
612/*
613 * Schedule switch for execution. We need to throttle requests,
614 * otherwise keyboard may become unresponsive.
615 */
616static void atkbd_schedule_event_work(struct atkbd *atkbd, int event_bit)
617{
618 unsigned long delay = msecs_to_jiffies(50);
619
620 if (time_after(jiffies, atkbd->event_jiffies + delay))
621 delay = 0;
622
623 atkbd->event_jiffies = jiffies;
624 set_bit(event_bit, &atkbd->event_mask);
625 mb();
626 schedule_delayed_work(&atkbd->event_work, delay);
627}
628
629/*
630 * Event callback from the input module. Events that change the state of
631 * the hardware are processed here. If action can not be performed in
632 * interrupt context it is offloaded to atkbd_event_work.
633 */
634
635static int atkbd_event(struct input_dev *dev,
636 unsigned int type, unsigned int code, int value)
637{
638 struct atkbd *atkbd = input_get_drvdata(dev);
639
640 if (!atkbd ||(!atkbd->write))
641 return -1;
642
643 switch (type) {
644
645 case EV_LED:
646 atkbd_schedule_event_work(atkbd, ATKBD_LED_EVENT_BIT);
647 return 0;
648
649 case EV_REP:
650 if (!atkbd->softrepeat)
651 atkbd_schedule_event_work(atkbd, ATKBD_REP_EVENT_BIT);
652 return 0;
653
654 default:
655 return -1;
656 }
657}
658
659/*
660 * atkbd_enable() signals that interrupt handler is allowed to
661 * generate input events.
662 */
663
664static inline void atkbd_enable(struct atkbd *atkbd)
665{
666 serio_pause_rx(atkbd->ps2dev.serio);
667 atkbd->enabled = true;
668 serio_continue_rx(atkbd->ps2dev.serio);
669}
670
671/*
672 * atkbd_disable() tells input handler that all incoming data except
673 * for ACKs and command response should be dropped.
674 */
675
676static inline void atkbd_disable(struct atkbd *atkbd)
677{
678 serio_pause_rx(atkbd->ps2dev.serio);
679 atkbd->enabled = false;
680 serio_continue_rx(atkbd->ps2dev.serio);
681}
682
683/*
684 * atkbd_probe() probes for an AT keyboard on a serio port.
685 */
686
687static int atkbd_probe(struct atkbd *atkbd)
688{
689 struct ps2dev *ps2dev = &atkbd->ps2dev;
690 unsigned char param[2];
691
692/*
693 * Some systems, where the bit-twiddling when testing the io-lines of the
694 * controller may confuse the keyboard need a full reset of the keyboard. On
695 * these systems the BIOS also usually doesn't do it for us.
696 */
697
698 if (atkbd_reset)
699 if (ps2_command(ps2dev, NULL, ATKBD_CMD_RESET_BAT))
700 dev_warn(&ps2dev->serio->dev,
701 "keyboard reset failed on %s\n",
702 ps2dev->serio->phys);
703
704/*
705 * Then we check the keyboard ID. We should get 0xab83 under normal conditions.
706 * Some keyboards report different values, but the first byte is always 0xab or
707 * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this
708 * should make sure we don't try to set the LEDs on it.
709 */
710
711 param[0] = param[1] = 0xa5; /* initialize with invalid values */
712 if (ps2_command(ps2dev, param, ATKBD_CMD_GETID)) {
713
714/*
715 * If the get ID command failed, we check if we can at least set the LEDs on
716 * the keyboard. This should work on every keyboard out there. It also turns
717 * the LEDs off, which we want anyway.
718 */
719 param[0] = 0;
720 if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS))
721 return -1;
722 atkbd->id = 0xabba;
723 return 0;
724 }
725
726 if (!ps2_is_keyboard_id(param[0]))
727 return -1;
728
729 atkbd->id = (param[0] << 8) | param[1];
730
731 if (atkbd->id == 0xaca1 && atkbd->translated) {
732 dev_err(&ps2dev->serio->dev,
733 "NCD terminal keyboards are only supported on non-translating controlelrs. "
734 "Use i8042.direct=1 to disable translation.\n");
735 return -1;
736 }
737
738 return 0;
739}
740
741/*
742 * atkbd_select_set checks if a keyboard has a working Set 3 support, and
743 * sets it into that. Unfortunately there are keyboards that can be switched
744 * to Set 3, but don't work well in that (BTC Multimedia ...)
745 */
746
747static int atkbd_select_set(struct atkbd *atkbd, int target_set, int allow_extra)
748{
749 struct ps2dev *ps2dev = &atkbd->ps2dev;
750 unsigned char param[2];
751
752 atkbd->extra = false;
753/*
754 * For known special keyboards we can go ahead and set the correct set.
755 * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and
756 * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards.
757 */
758
759 if (atkbd->translated)
760 return 2;
761
762 if (atkbd->id == 0xaca1) {
763 param[0] = 3;
764 ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET);
765 return 3;
766 }
767
768 if (allow_extra) {
769 param[0] = 0x71;
770 if (!ps2_command(ps2dev, param, ATKBD_CMD_EX_ENABLE)) {
771 atkbd->extra = true;
772 return 2;
773 }
774 }
775
776 if (atkbd_terminal) {
777 ps2_command(ps2dev, param, ATKBD_CMD_SETALL_MB);
778 return 3;
779 }
780
781 if (target_set != 3)
782 return 2;
783
784 if (!ps2_command(ps2dev, param, ATKBD_CMD_OK_GETID)) {
785 atkbd->id = param[0] << 8 | param[1];
786 return 2;
787 }
788
789 param[0] = 3;
790 if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET))
791 return 2;
792
793 param[0] = 0;
794 if (ps2_command(ps2dev, param, ATKBD_CMD_GSCANSET))
795 return 2;
796
797 if (param[0] != 3) {
798 param[0] = 2;
799 if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET))
800 return 2;
801 }
802
803 ps2_command(ps2dev, param, ATKBD_CMD_SETALL_MBR);
804
805 return 3;
806}
807
808static int atkbd_reset_state(struct atkbd *atkbd)
809{
810 struct ps2dev *ps2dev = &atkbd->ps2dev;
811 unsigned char param[1];
812
813/*
814 * Set the LEDs to a predefined state (all off).
815 */
816
817 param[0] = 0;
818 if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS))
819 return -1;
820
821/*
822 * Set autorepeat to fastest possible.
823 */
824
825 param[0] = 0;
826 if (ps2_command(ps2dev, param, ATKBD_CMD_SETREP))
827 return -1;
828
829 return 0;
830}
831
832static int atkbd_activate(struct atkbd *atkbd)
833{
834 struct ps2dev *ps2dev = &atkbd->ps2dev;
835
836/*
837 * Enable the keyboard to receive keystrokes.
838 */
839
840 if (ps2_command(ps2dev, NULL, ATKBD_CMD_ENABLE)) {
841 dev_err(&ps2dev->serio->dev,
842 "Failed to enable keyboard on %s\n",
843 ps2dev->serio->phys);
844 return -1;
845 }
846
847 return 0;
848}
849
850/*
851 * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a
852 * reboot.
853 */
854
855static void atkbd_cleanup(struct serio *serio)
856{
857 struct atkbd *atkbd = serio_get_drvdata(serio);
858
859 if(!atkbd)
860 return;
861
862 atkbd_disable(atkbd);
863 ps2_command(&atkbd->ps2dev, NULL, ATKBD_CMD_RESET_DEF);
864}
865
866
867/*
868 * atkbd_disconnect() closes and frees.
869 */
870
871static void atkbd_disconnect(struct serio *serio)
872{
873 struct atkbd *atkbd = serio_get_drvdata(serio);
874
875 if(!atkbd)
876 return;
877
878 sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group);
879
880 atkbd_disable(atkbd);
881
882 input_unregister_device(atkbd->dev);
883
884 /*
885 * Make sure we don't have a command in flight.
886 * Note that since atkbd->enabled is false event work will keep
887 * rescheduling itself until it gets canceled and will not try
888 * accessing freed input device or serio port.
889 */
890 cancel_delayed_work_sync(&atkbd->event_work);
891
892 serio_close(serio);
893 serio_set_drvdata(serio, NULL);
894 kfree(atkbd);
895}
896
897/*
898 * generate release events for the keycodes given in data
899 */
900static void atkbd_apply_forced_release_keylist(struct atkbd* atkbd,
901 const void *data)
902{
903 const unsigned int *keys = data;
904 unsigned int i;
905
906 if (atkbd->set == 2)
907 for (i = 0; keys[i] != -1U; i++)
908 __set_bit(keys[i], atkbd->force_release_mask);
909}
910
911/*
912 * Most special keys (Fn+F?) on Dell laptops do not generate release
913 * events so we have to do it ourselves.
914 */
915static unsigned int atkbd_dell_laptop_forced_release_keys[] = {
916 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8f, 0x93, -1U
917};
918
919/*
920 * Perform fixup for HP system that doesn't generate release
921 * for its video switch
922 */
923static unsigned int atkbd_hp_forced_release_keys[] = {
924 0x94, -1U
925};
926
927/*
928 * Samsung NC10,NC20 with Fn+F? key release not working
929 */
930static unsigned int atkbd_samsung_forced_release_keys[] = {
931 0x82, 0x83, 0x84, 0x86, 0x88, 0x89, 0xb3, 0xf7, 0xf9, -1U
932};
933
934/*
935 * Amilo Pi 3525 key release for Fn+Volume keys not working
936 */
937static unsigned int atkbd_amilo_pi3525_forced_release_keys[] = {
938 0x20, 0xa0, 0x2e, 0xae, 0x30, 0xb0, -1U
939};
940
941/*
942 * Amilo Xi 3650 key release for light touch bar not working
943 */
944static unsigned int atkbd_amilo_xi3650_forced_release_keys[] = {
945 0x67, 0xed, 0x90, 0xa2, 0x99, 0xa4, 0xae, 0xb0, -1U
946};
947
948/*
949 * Soltech TA12 system with broken key release on volume keys and mute key
950 */
951static unsigned int atkdb_soltech_ta12_forced_release_keys[] = {
952 0xa0, 0xae, 0xb0, -1U
953};
954
955/*
956 * Many notebooks don't send key release event for volume up/down
957 * keys, with key list below common among them
958 */
959static unsigned int atkbd_volume_forced_release_keys[] = {
960 0xae, 0xb0, -1U
961};
962
963/*
964 * OQO 01+ multimedia keys (64--66) generate e0 6x upon release whereas
965 * they should be generating e4-e6 (0x80 | code).
966 */
967static unsigned int atkbd_oqo_01plus_scancode_fixup(struct atkbd *atkbd,
968 unsigned int code)
969{
970 if (atkbd->translated && atkbd->emul == 1 &&
971 (code == 0x64 || code == 0x65 || code == 0x66)) {
972 atkbd->emul = 0;
973 code |= 0x80;
974 }
975
976 return code;
977}
978
979/*
980 * atkbd_set_keycode_table() initializes keyboard's keycode table
981 * according to the selected scancode set
982 */
983
984static void atkbd_set_keycode_table(struct atkbd *atkbd)
985{
986 unsigned int scancode;
987 int i, j;
988
989 memset(atkbd->keycode, 0, sizeof(atkbd->keycode));
990 bitmap_zero(atkbd->force_release_mask, ATKBD_KEYMAP_SIZE);
991
992 if (atkbd->translated) {
993 for (i = 0; i < 128; i++) {
994 scancode = atkbd_unxlate_table[i];
995 atkbd->keycode[i] = atkbd_set2_keycode[scancode];
996 atkbd->keycode[i | 0x80] = atkbd_set2_keycode[scancode | 0x80];
997 if (atkbd->scroll)
998 for (j = 0; j < ARRAY_SIZE(atkbd_scroll_keys); j++)
999 if ((scancode | 0x80) == atkbd_scroll_keys[j].set2)
1000 atkbd->keycode[i | 0x80] = atkbd_scroll_keys[j].keycode;
1001 }
1002 } else if (atkbd->set == 3) {
1003 memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));
1004 } else {
1005 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
1006
1007 if (atkbd->scroll)
1008 for (i = 0; i < ARRAY_SIZE(atkbd_scroll_keys); i++) {
1009 scancode = atkbd_scroll_keys[i].set2;
1010 atkbd->keycode[scancode] = atkbd_scroll_keys[i].keycode;
1011 }
1012 }
1013
1014/*
1015 * HANGEUL and HANJA keys do not send release events so we need to
1016 * generate such events ourselves
1017 */
1018 scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANGEUL);
1019 atkbd->keycode[scancode] = KEY_HANGEUL;
1020 __set_bit(scancode, atkbd->force_release_mask);
1021
1022 scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANJA);
1023 atkbd->keycode[scancode] = KEY_HANJA;
1024 __set_bit(scancode, atkbd->force_release_mask);
1025
1026/*
1027 * Perform additional fixups
1028 */
1029 if (atkbd_platform_fixup)
1030 atkbd_platform_fixup(atkbd, atkbd_platform_fixup_data);
1031}
1032
1033/*
1034 * atkbd_set_device_attrs() sets up keyboard's input device structure
1035 */
1036
1037static void atkbd_set_device_attrs(struct atkbd *atkbd)
1038{
1039 struct input_dev *input_dev = atkbd->dev;
1040 int i;
1041
1042 if (atkbd->extra)
1043 snprintf(atkbd->name, sizeof(atkbd->name),
1044 "AT Set 2 Extra keyboard");
1045 else
1046 snprintf(atkbd->name, sizeof(atkbd->name),
1047 "AT %s Set %d keyboard",
1048 atkbd->translated ? "Translated" : "Raw", atkbd->set);
1049
1050 snprintf(atkbd->phys, sizeof(atkbd->phys),
1051 "%s/input0", atkbd->ps2dev.serio->phys);
1052
1053 input_dev->name = atkbd->name;
1054 input_dev->phys = atkbd->phys;
1055 input_dev->id.bustype = BUS_I8042;
1056 input_dev->id.vendor = 0x0001;
1057 input_dev->id.product = atkbd->translated ? 1 : atkbd->set;
1058 input_dev->id.version = atkbd->id;
1059 input_dev->event = atkbd_event;
1060 input_dev->dev.parent = &atkbd->ps2dev.serio->dev;
1061
1062 input_set_drvdata(input_dev, atkbd);
1063
1064 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
1065 BIT_MASK(EV_MSC);
1066
1067 if (atkbd->write) {
1068 input_dev->evbit[0] |= BIT_MASK(EV_LED);
1069 input_dev->ledbit[0] = BIT_MASK(LED_NUML) |
1070 BIT_MASK(LED_CAPSL) | BIT_MASK(LED_SCROLLL);
1071 }
1072
1073 if (atkbd->extra)
1074 input_dev->ledbit[0] |= BIT_MASK(LED_COMPOSE) |
1075 BIT_MASK(LED_SUSPEND) | BIT_MASK(LED_SLEEP) |
1076 BIT_MASK(LED_MUTE) | BIT_MASK(LED_MISC);
1077
1078 if (!atkbd->softrepeat) {
1079 input_dev->rep[REP_DELAY] = 250;
1080 input_dev->rep[REP_PERIOD] = 33;
1081 }
1082
1083 input_dev->mscbit[0] = atkbd->softraw ? BIT_MASK(MSC_SCAN) :
1084 BIT_MASK(MSC_RAW) | BIT_MASK(MSC_SCAN);
1085
1086 if (atkbd->scroll) {
1087 input_dev->evbit[0] |= BIT_MASK(EV_REL);
1088 input_dev->relbit[0] = BIT_MASK(REL_WHEEL) |
1089 BIT_MASK(REL_HWHEEL);
1090 __set_bit(BTN_MIDDLE, input_dev->keybit);
1091 }
1092
1093 input_dev->keycode = atkbd->keycode;
1094 input_dev->keycodesize = sizeof(unsigned short);
1095 input_dev->keycodemax = ARRAY_SIZE(atkbd_set2_keycode);
1096
1097 for (i = 0; i < ATKBD_KEYMAP_SIZE; i++) {
1098 if (atkbd->keycode[i] != KEY_RESERVED &&
1099 atkbd->keycode[i] != ATKBD_KEY_NULL &&
1100 atkbd->keycode[i] < ATKBD_SPECIAL) {
1101 __set_bit(atkbd->keycode[i], input_dev->keybit);
1102 }
1103 }
1104}
1105
1106/*
1107 * atkbd_connect() is called when the serio module finds an interface
1108 * that isn't handled yet by an appropriate device driver. We check if
1109 * there is an AT keyboard out there and if yes, we register ourselves
1110 * to the input module.
1111 */
1112
1113static int atkbd_connect(struct serio *serio, struct serio_driver *drv)
1114{
1115 struct atkbd *atkbd;
1116 struct input_dev *dev;
1117 int err = -ENOMEM;
1118
1119 atkbd = kzalloc(sizeof(struct atkbd), GFP_KERNEL);
1120 dev = input_allocate_device();
1121 if (!atkbd || !dev)
1122 goto fail1;
1123
1124 atkbd->dev = dev;
1125 ps2_init(&atkbd->ps2dev, serio);
1126 INIT_DELAYED_WORK(&atkbd->event_work, atkbd_event_work);
1127 mutex_init(&atkbd->mutex);
1128
1129 switch (serio->id.type) {
1130
1131 case SERIO_8042_XL:
1132 atkbd->translated = true;
1133 /* Fall through */
1134
1135 case SERIO_8042:
1136 if (serio->write)
1137 atkbd->write = true;
1138 break;
1139 }
1140
1141 atkbd->softraw = atkbd_softraw;
1142 atkbd->softrepeat = atkbd_softrepeat;
1143 atkbd->scroll = atkbd_scroll;
1144
1145 if (atkbd->softrepeat)
1146 atkbd->softraw = true;
1147
1148 serio_set_drvdata(serio, atkbd);
1149
1150 err = serio_open(serio, drv);
1151 if (err)
1152 goto fail2;
1153
1154 if (atkbd->write) {
1155
1156 if (atkbd_probe(atkbd)) {
1157 err = -ENODEV;
1158 goto fail3;
1159 }
1160
1161 atkbd->set = atkbd_select_set(atkbd, atkbd_set, atkbd_extra);
1162 atkbd_reset_state(atkbd);
1163 atkbd_activate(atkbd);
1164
1165 } else {
1166 atkbd->set = 2;
1167 atkbd->id = 0xab00;
1168 }
1169
1170 atkbd_set_keycode_table(atkbd);
1171 atkbd_set_device_attrs(atkbd);
1172
1173 err = sysfs_create_group(&serio->dev.kobj, &atkbd_attribute_group);
1174 if (err)
1175 goto fail3;
1176
1177 atkbd_enable(atkbd);
1178
1179 err = input_register_device(atkbd->dev);
1180 if (err)
1181 goto fail4;
1182
1183 return 0;
1184
1185 fail4: sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group);
1186 fail3: serio_close(serio);
1187 fail2: serio_set_drvdata(serio, NULL);
1188 fail1: input_free_device(dev);
1189 kfree(atkbd);
1190 return err;
1191}
1192
1193/*
1194 * atkbd_reconnect() tries to restore keyboard into a sane state and is
1195 * most likely called on resume.
1196 */
1197
1198static int atkbd_reconnect(struct serio *serio)
1199{
1200 struct atkbd *atkbd = serio_get_drvdata(serio);
1201 struct serio_driver *drv = serio->drv;
1202 int retval = -1;
1203
1204 if (!atkbd || !drv) {
1205 dev_dbg(&serio->dev,
1206 "reconnect request, but serio is disconnected, ignoring...\n");
1207 return -1;
1208 }
1209
1210 mutex_lock(&atkbd->mutex);
1211
1212 atkbd_disable(atkbd);
1213
1214 if (atkbd->write) {
1215 if (atkbd_probe(atkbd))
1216 goto out;
1217
1218 if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra))
1219 goto out;
1220
1221 atkbd_activate(atkbd);
1222
1223 /*
1224 * Restore LED state and repeat rate. While input core
1225 * will do this for us at resume time reconnect may happen
1226 * because user requested it via sysfs or simply because
1227 * keyboard was unplugged and plugged in again so we need
1228 * to do it ourselves here.
1229 */
1230 atkbd_set_leds(atkbd);
1231 if (!atkbd->softrepeat)
1232 atkbd_set_repeat_rate(atkbd);
1233
1234 }
1235
1236 atkbd_enable(atkbd);
1237 retval = 0;
1238
1239 out:
1240 mutex_unlock(&atkbd->mutex);
1241 return retval;
1242}
1243
1244static struct serio_device_id atkbd_serio_ids[] = {
1245 {
1246 .type = SERIO_8042,
1247 .proto = SERIO_ANY,
1248 .id = SERIO_ANY,
1249 .extra = SERIO_ANY,
1250 },
1251 {
1252 .type = SERIO_8042_XL,
1253 .proto = SERIO_ANY,
1254 .id = SERIO_ANY,
1255 .extra = SERIO_ANY,
1256 },
1257 {
1258 .type = SERIO_RS232,
1259 .proto = SERIO_PS2SER,
1260 .id = SERIO_ANY,
1261 .extra = SERIO_ANY,
1262 },
1263 { 0 }
1264};
1265
1266MODULE_DEVICE_TABLE(serio, atkbd_serio_ids);
1267
1268static struct serio_driver atkbd_drv = {
1269 .driver = {
1270 .name = "atkbd",
1271 },
1272 .description = DRIVER_DESC,
1273 .id_table = atkbd_serio_ids,
1274 .interrupt = atkbd_interrupt,
1275 .connect = atkbd_connect,
1276 .reconnect = atkbd_reconnect,
1277 .disconnect = atkbd_disconnect,
1278 .cleanup = atkbd_cleanup,
1279};
1280
1281static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf,
1282 ssize_t (*handler)(struct atkbd *, char *))
1283{
1284 struct serio *serio = to_serio_port(dev);
1285 struct atkbd *atkbd = serio_get_drvdata(serio);
1286
1287 if(!atkbd)
1288 return -EINVAL;
1289
1290 return handler(atkbd, buf);
1291}
1292
1293static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count,
1294 ssize_t (*handler)(struct atkbd *, const char *, size_t))
1295{
1296 struct serio *serio = to_serio_port(dev);
1297 struct atkbd *atkbd = serio_get_drvdata(serio);
1298 int retval;
1299
1300 if(!atkbd)
1301 return -EINVAL;
1302 retval = mutex_lock_interruptible(&atkbd->mutex);
1303 if (retval)
1304 return retval;
1305
1306 atkbd_disable(atkbd);
1307 retval = handler(atkbd, buf, count);
1308 atkbd_enable(atkbd);
1309
1310 mutex_unlock(&atkbd->mutex);
1311
1312 return retval;
1313}
1314
1315static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf)
1316{
1317 return sprintf(buf, "%d\n", atkbd->extra ? 1 : 0);
1318}
1319
1320static ssize_t atkbd_set_extra(struct atkbd *atkbd, const char *buf, size_t count)
1321{
1322 struct input_dev *old_dev, *new_dev;
1323 unsigned int value;
1324 int err;
1325 bool old_extra;
1326 unsigned char old_set;
1327
1328 if (!atkbd->write)
1329 return -EIO;
1330
1331 err = kstrtouint(buf, 10, &value);
1332 if (err)
1333 return err;
1334
1335 if (value > 1)
1336 return -EINVAL;
1337
1338 if (atkbd->extra != value) {
1339 /*
1340 * Since device's properties will change we need to
1341 * unregister old device. But allocate and register
1342 * new one first to make sure we have it.
1343 */
1344 old_dev = atkbd->dev;
1345 old_extra = atkbd->extra;
1346 old_set = atkbd->set;
1347
1348 new_dev = input_allocate_device();
1349 if (!new_dev)
1350 return -ENOMEM;
1351
1352 atkbd->dev = new_dev;
1353 atkbd->set = atkbd_select_set(atkbd, atkbd->set, value);
1354 atkbd_reset_state(atkbd);
1355 atkbd_activate(atkbd);
1356 atkbd_set_keycode_table(atkbd);
1357 atkbd_set_device_attrs(atkbd);
1358
1359 err = input_register_device(atkbd->dev);
1360 if (err) {
1361 input_free_device(new_dev);
1362
1363 atkbd->dev = old_dev;
1364 atkbd->set = atkbd_select_set(atkbd, old_set, old_extra);
1365 atkbd_set_keycode_table(atkbd);
1366 atkbd_set_device_attrs(atkbd);
1367
1368 return err;
1369 }
1370 input_unregister_device(old_dev);
1371
1372 }
1373 return count;
1374}
1375
1376static ssize_t atkbd_show_force_release(struct atkbd *atkbd, char *buf)
1377{
1378 size_t len = bitmap_scnlistprintf(buf, PAGE_SIZE - 2,
1379 atkbd->force_release_mask, ATKBD_KEYMAP_SIZE);
1380
1381 buf[len++] = '\n';
1382 buf[len] = '\0';
1383
1384 return len;
1385}
1386
1387static ssize_t atkbd_set_force_release(struct atkbd *atkbd,
1388 const char *buf, size_t count)
1389{
1390 /* 64 bytes on stack should be acceptable */
1391 DECLARE_BITMAP(new_mask, ATKBD_KEYMAP_SIZE);
1392 int err;
1393
1394 err = bitmap_parselist(buf, new_mask, ATKBD_KEYMAP_SIZE);
1395 if (err)
1396 return err;
1397
1398 memcpy(atkbd->force_release_mask, new_mask, sizeof(atkbd->force_release_mask));
1399 return count;
1400}
1401
1402
1403static ssize_t atkbd_show_scroll(struct atkbd *atkbd, char *buf)
1404{
1405 return sprintf(buf, "%d\n", atkbd->scroll ? 1 : 0);
1406}
1407
1408static ssize_t atkbd_set_scroll(struct atkbd *atkbd, const char *buf, size_t count)
1409{
1410 struct input_dev *old_dev, *new_dev;
1411 unsigned int value;
1412 int err;
1413 bool old_scroll;
1414
1415 err = kstrtouint(buf, 10, &value);
1416 if (err)
1417 return err;
1418
1419 if (value > 1)
1420 return -EINVAL;
1421
1422 if (atkbd->scroll != value) {
1423 old_dev = atkbd->dev;
1424 old_scroll = atkbd->scroll;
1425
1426 new_dev = input_allocate_device();
1427 if (!new_dev)
1428 return -ENOMEM;
1429
1430 atkbd->dev = new_dev;
1431 atkbd->scroll = value;
1432 atkbd_set_keycode_table(atkbd);
1433 atkbd_set_device_attrs(atkbd);
1434
1435 err = input_register_device(atkbd->dev);
1436 if (err) {
1437 input_free_device(new_dev);
1438
1439 atkbd->scroll = old_scroll;
1440 atkbd->dev = old_dev;
1441 atkbd_set_keycode_table(atkbd);
1442 atkbd_set_device_attrs(atkbd);
1443
1444 return err;
1445 }
1446 input_unregister_device(old_dev);
1447 }
1448 return count;
1449}
1450
1451static ssize_t atkbd_show_set(struct atkbd *atkbd, char *buf)
1452{
1453 return sprintf(buf, "%d\n", atkbd->set);
1454}
1455
1456static ssize_t atkbd_set_set(struct atkbd *atkbd, const char *buf, size_t count)
1457{
1458 struct input_dev *old_dev, *new_dev;
1459 unsigned int value;
1460 int err;
1461 unsigned char old_set;
1462 bool old_extra;
1463
1464 if (!atkbd->write)
1465 return -EIO;
1466
1467 err = kstrtouint(buf, 10, &value);
1468 if (err)
1469 return err;
1470
1471 if (value != 2 && value != 3)
1472 return -EINVAL;
1473
1474 if (atkbd->set != value) {
1475 old_dev = atkbd->dev;
1476 old_extra = atkbd->extra;
1477 old_set = atkbd->set;
1478
1479 new_dev = input_allocate_device();
1480 if (!new_dev)
1481 return -ENOMEM;
1482
1483 atkbd->dev = new_dev;
1484 atkbd->set = atkbd_select_set(atkbd, value, atkbd->extra);
1485 atkbd_reset_state(atkbd);
1486 atkbd_activate(atkbd);
1487 atkbd_set_keycode_table(atkbd);
1488 atkbd_set_device_attrs(atkbd);
1489
1490 err = input_register_device(atkbd->dev);
1491 if (err) {
1492 input_free_device(new_dev);
1493
1494 atkbd->dev = old_dev;
1495 atkbd->set = atkbd_select_set(atkbd, old_set, old_extra);
1496 atkbd_set_keycode_table(atkbd);
1497 atkbd_set_device_attrs(atkbd);
1498
1499 return err;
1500 }
1501 input_unregister_device(old_dev);
1502 }
1503 return count;
1504}
1505
1506static ssize_t atkbd_show_softrepeat(struct atkbd *atkbd, char *buf)
1507{
1508 return sprintf(buf, "%d\n", atkbd->softrepeat ? 1 : 0);
1509}
1510
1511static ssize_t atkbd_set_softrepeat(struct atkbd *atkbd, const char *buf, size_t count)
1512{
1513 struct input_dev *old_dev, *new_dev;
1514 unsigned int value;
1515 int err;
1516 bool old_softrepeat, old_softraw;
1517
1518 if (!atkbd->write)
1519 return -EIO;
1520
1521 err = kstrtouint(buf, 10, &value);
1522 if (err)
1523 return err;
1524
1525 if (value > 1)
1526 return -EINVAL;
1527
1528 if (atkbd->softrepeat != value) {
1529 old_dev = atkbd->dev;
1530 old_softrepeat = atkbd->softrepeat;
1531 old_softraw = atkbd->softraw;
1532
1533 new_dev = input_allocate_device();
1534 if (!new_dev)
1535 return -ENOMEM;
1536
1537 atkbd->dev = new_dev;
1538 atkbd->softrepeat = value;
1539 if (atkbd->softrepeat)
1540 atkbd->softraw = true;
1541 atkbd_set_device_attrs(atkbd);
1542
1543 err = input_register_device(atkbd->dev);
1544 if (err) {
1545 input_free_device(new_dev);
1546
1547 atkbd->dev = old_dev;
1548 atkbd->softrepeat = old_softrepeat;
1549 atkbd->softraw = old_softraw;
1550 atkbd_set_device_attrs(atkbd);
1551
1552 return err;
1553 }
1554 input_unregister_device(old_dev);
1555 }
1556 return count;
1557}
1558
1559
1560static ssize_t atkbd_show_softraw(struct atkbd *atkbd, char *buf)
1561{
1562 return sprintf(buf, "%d\n", atkbd->softraw ? 1 : 0);
1563}
1564
1565static ssize_t atkbd_set_softraw(struct atkbd *atkbd, const char *buf, size_t count)
1566{
1567 struct input_dev *old_dev, *new_dev;
1568 unsigned int value;
1569 int err;
1570 bool old_softraw;
1571
1572 err = kstrtouint(buf, 10, &value);
1573 if (err)
1574 return err;
1575
1576 if (value > 1)
1577 return -EINVAL;
1578
1579 if (atkbd->softraw != value) {
1580 old_dev = atkbd->dev;
1581 old_softraw = atkbd->softraw;
1582
1583 new_dev = input_allocate_device();
1584 if (!new_dev)
1585 return -ENOMEM;
1586
1587 atkbd->dev = new_dev;
1588 atkbd->softraw = value;
1589 atkbd_set_device_attrs(atkbd);
1590
1591 err = input_register_device(atkbd->dev);
1592 if (err) {
1593 input_free_device(new_dev);
1594
1595 atkbd->dev = old_dev;
1596 atkbd->softraw = old_softraw;
1597 atkbd_set_device_attrs(atkbd);
1598
1599 return err;
1600 }
1601 input_unregister_device(old_dev);
1602 }
1603 return count;
1604}
1605
1606static ssize_t atkbd_show_err_count(struct atkbd *atkbd, char *buf)
1607{
1608 return sprintf(buf, "%lu\n", atkbd->err_count);
1609}
1610
1611static int __init atkbd_setup_forced_release(const struct dmi_system_id *id)
1612{
1613 atkbd_platform_fixup = atkbd_apply_forced_release_keylist;
1614 atkbd_platform_fixup_data = id->driver_data;
1615
1616 return 1;
1617}
1618
1619static int __init atkbd_setup_scancode_fixup(const struct dmi_system_id *id)
1620{
1621 atkbd_platform_scancode_fixup = id->driver_data;
1622
1623 return 1;
1624}
1625
1626static const struct dmi_system_id atkbd_dmi_quirk_table[] __initconst = {
1627 {
1628 .matches = {
1629 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1630 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1631 },
1632 .callback = atkbd_setup_forced_release,
1633 .driver_data = atkbd_dell_laptop_forced_release_keys,
1634 },
1635 {
1636 .matches = {
1637 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
1638 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1639 },
1640 .callback = atkbd_setup_forced_release,
1641 .driver_data = atkbd_dell_laptop_forced_release_keys,
1642 },
1643 {
1644 .matches = {
1645 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1646 DMI_MATCH(DMI_PRODUCT_NAME, "HP 2133"),
1647 },
1648 .callback = atkbd_setup_forced_release,
1649 .driver_data = atkbd_hp_forced_release_keys,
1650 },
1651 {
1652 .matches = {
1653 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1654 DMI_MATCH(DMI_PRODUCT_NAME, "Pavilion ZV6100"),
1655 },
1656 .callback = atkbd_setup_forced_release,
1657 .driver_data = atkbd_volume_forced_release_keys,
1658 },
1659 {
1660 .matches = {
1661 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1662 DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4000"),
1663 },
1664 .callback = atkbd_setup_forced_release,
1665 .driver_data = atkbd_volume_forced_release_keys,
1666 },
1667 {
1668 .matches = {
1669 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1670 DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4100"),
1671 },
1672 .callback = atkbd_setup_forced_release,
1673 .driver_data = atkbd_volume_forced_release_keys,
1674 },
1675 {
1676 .matches = {
1677 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1678 DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4200"),
1679 },
1680 .callback = atkbd_setup_forced_release,
1681 .driver_data = atkbd_volume_forced_release_keys,
1682 },
1683 {
1684 /* Inventec Symphony */
1685 .matches = {
1686 DMI_MATCH(DMI_SYS_VENDOR, "INVENTEC"),
1687 DMI_MATCH(DMI_PRODUCT_NAME, "SYMPHONY 6.0/7.0"),
1688 },
1689 .callback = atkbd_setup_forced_release,
1690 .driver_data = atkbd_volume_forced_release_keys,
1691 },
1692 {
1693 /* Samsung NC10 */
1694 .matches = {
1695 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1696 DMI_MATCH(DMI_PRODUCT_NAME, "NC10"),
1697 },
1698 .callback = atkbd_setup_forced_release,
1699 .driver_data = atkbd_samsung_forced_release_keys,
1700 },
1701 {
1702 /* Samsung NC20 */
1703 .matches = {
1704 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1705 DMI_MATCH(DMI_PRODUCT_NAME, "NC20"),
1706 },
1707 .callback = atkbd_setup_forced_release,
1708 .driver_data = atkbd_samsung_forced_release_keys,
1709 },
1710 {
1711 /* Samsung SQ45S70S */
1712 .matches = {
1713 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1714 DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"),
1715 },
1716 .callback = atkbd_setup_forced_release,
1717 .driver_data = atkbd_samsung_forced_release_keys,
1718 },
1719 {
1720 /* Fujitsu Amilo PA 1510 */
1721 .matches = {
1722 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
1723 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pa 1510"),
1724 },
1725 .callback = atkbd_setup_forced_release,
1726 .driver_data = atkbd_volume_forced_release_keys,
1727 },
1728 {
1729 /* Fujitsu Amilo Pi 3525 */
1730 .matches = {
1731 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
1732 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pi 3525"),
1733 },
1734 .callback = atkbd_setup_forced_release,
1735 .driver_data = atkbd_amilo_pi3525_forced_release_keys,
1736 },
1737 {
1738 /* Fujitsu Amilo Xi 3650 */
1739 .matches = {
1740 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
1741 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xi 3650"),
1742 },
1743 .callback = atkbd_setup_forced_release,
1744 .driver_data = atkbd_amilo_xi3650_forced_release_keys,
1745 },
1746 {
1747 .matches = {
1748 DMI_MATCH(DMI_SYS_VENDOR, "Soltech Corporation"),
1749 DMI_MATCH(DMI_PRODUCT_NAME, "TA12"),
1750 },
1751 .callback = atkbd_setup_forced_release,
1752 .driver_data = atkdb_soltech_ta12_forced_release_keys,
1753 },
1754 {
1755 /* OQO Model 01+ */
1756 .matches = {
1757 DMI_MATCH(DMI_SYS_VENDOR, "OQO"),
1758 DMI_MATCH(DMI_PRODUCT_NAME, "ZEPTO"),
1759 },
1760 .callback = atkbd_setup_scancode_fixup,
1761 .driver_data = atkbd_oqo_01plus_scancode_fixup,
1762 },
1763 { }
1764};
1765
1766static int __init atkbd_init(void)
1767{
1768 dmi_check_system(atkbd_dmi_quirk_table);
1769
1770 return serio_register_driver(&atkbd_drv);
1771}
1772
1773static void __exit atkbd_exit(void)
1774{
1775 serio_unregister_driver(&atkbd_drv);
1776}
1777
1778module_init(atkbd_init);
1779module_exit(atkbd_exit);