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