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 31MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); 32MODULE_DESCRIPTION(DRIVER_DESC); 33MODULE_LICENSE("GPL"); 34 35static int atkbd_set = 2; 36module_param_named(set, atkbd_set, int, 0); 37MODULE_PARM_DESC(set, "Select keyboard code set (2 = default, 3 = PS/2 native)"); 38 39#if defined(__i386__) || defined(__x86_64__) || defined(__hppa__) 40static bool atkbd_reset; 41#else 42static bool atkbd_reset = true; 43#endif 44module_param_named(reset, atkbd_reset, bool, 0); 45MODULE_PARM_DESC(reset, "Reset keyboard during initialization"); 46 47static bool atkbd_softrepeat; 48module_param_named(softrepeat, atkbd_softrepeat, bool, 0); 49MODULE_PARM_DESC(softrepeat, "Use software keyboard repeat"); 50 51static bool atkbd_softraw = true; 52module_param_named(softraw, atkbd_softraw, bool, 0); 53MODULE_PARM_DESC(softraw, "Use software generated rawmode"); 54 55static bool atkbd_scroll; 56module_param_named(scroll, atkbd_scroll, bool, 0); 57MODULE_PARM_DESC(scroll, "Enable scroll-wheel on MS Office and similar keyboards"); 58 59static bool atkbd_extra; 60module_param_named(extra, atkbd_extra, bool, 0); 61MODULE_PARM_DESC(extra, "Enable extra LEDs and keys on IBM RapidAcces, EzKey and similar keyboards"); 62 63static bool atkbd_terminal; 64module_param_named(terminal, atkbd_terminal, bool, 0); 65MODULE_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 79static 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 110static 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 126static 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 186static 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 203struct 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 */ 247static void (*atkbd_platform_fixup)(struct atkbd *, const void *data); 248static void *atkbd_platform_fixup_data; 249static 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 */ 255static bool atkbd_skip_deactivate; 256 257static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf, 258 ssize_t (*handler)(struct atkbd *, char *)); 259static 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) \ 262static ssize_t atkbd_show_##_name(struct atkbd *, char *); \ 263static ssize_t atkbd_set_##_name(struct atkbd *, const char *, size_t); \ 264static 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} \ 269static 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} \ 274static struct device_attribute atkbd_attr_##_name = \ 275 __ATTR(_name, S_IWUSR | S_IRUGO, atkbd_do_show_##_name, atkbd_do_set_##_name); 276 277ATKBD_DEFINE_ATTR(extra); 278ATKBD_DEFINE_ATTR(force_release); 279ATKBD_DEFINE_ATTR(scroll); 280ATKBD_DEFINE_ATTR(set); 281ATKBD_DEFINE_ATTR(softrepeat); 282ATKBD_DEFINE_ATTR(softraw); 283 284#define ATKBD_DEFINE_RO_ATTR(_name) \ 285static ssize_t atkbd_show_##_name(struct atkbd *, char *); \ 286static 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} \ 291static struct device_attribute atkbd_attr_##_name = \ 292 __ATTR(_name, S_IRUGO, atkbd_do_show_##_name, NULL); 293 294ATKBD_DEFINE_RO_ATTR(err_count); 295ATKBD_DEFINE_RO_ATTR(function_row_physmap); 296 297static 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 309static 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 324static 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 338static struct attribute_group atkbd_attribute_group = { 339 .attrs = atkbd_attributes, 340 .is_visible = atkbd_attr_is_visible, 341}; 342 343static 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 */ 352static 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 */ 371static 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 */ 390static 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 409static 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; 570out: 571 return IRQ_HANDLED; 572} 573 574static 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 598static 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 629static 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 */ 659static 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 678static 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 707static 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 719static 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 726static 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 749static 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#ifdef CONFIG_X86 760static bool atkbd_is_portable_device(void) 761{ 762 static const char * const chassis_types[] = { 763 "8", /* Portable */ 764 "9", /* Laptop */ 765 "10", /* Notebook */ 766 "14", /* Sub-Notebook */ 767 "31", /* Convertible */ 768 "32", /* Detachable */ 769 }; 770 int i; 771 772 for (i = 0; i < ARRAY_SIZE(chassis_types); i++) 773 if (dmi_match(DMI_CHASSIS_TYPE, chassis_types[i])) 774 return true; 775 776 return false; 777} 778 779/* 780 * On many modern laptops ATKBD_CMD_GETID may cause problems, on these laptops 781 * the controller is always in translated mode. In this mode mice/touchpads will 782 * not work. So in this case simply assume a keyboard is connected to avoid 783 * confusing some laptop keyboards. 784 * 785 * Skipping ATKBD_CMD_GETID ends up using a fake keyboard id. Using the standard 786 * 0xab83 id is ok in translated mode, only atkbd_select_set() checks atkbd->id 787 * and in translated mode that is a no-op. 788 */ 789static bool atkbd_skip_getid(struct atkbd *atkbd) 790{ 791 return atkbd->translated && atkbd_is_portable_device(); 792} 793#else 794static inline bool atkbd_skip_getid(struct atkbd *atkbd) { return false; } 795#endif 796 797/* 798 * atkbd_probe() probes for an AT keyboard on a serio port. 799 */ 800 801static int atkbd_probe(struct atkbd *atkbd) 802{ 803 struct ps2dev *ps2dev = &atkbd->ps2dev; 804 unsigned char param[2]; 805 806/* 807 * Some systems, where the bit-twiddling when testing the io-lines of the 808 * controller may confuse the keyboard need a full reset of the keyboard. On 809 * these systems the BIOS also usually doesn't do it for us. 810 */ 811 812 if (atkbd_reset) 813 if (ps2_command(ps2dev, NULL, ATKBD_CMD_RESET_BAT)) 814 dev_warn(&ps2dev->serio->dev, 815 "keyboard reset failed on %s\n", 816 ps2dev->serio->phys); 817 818 if (atkbd_skip_getid(atkbd)) { 819 atkbd->id = 0xab83; 820 return 0; 821 } 822 823/* 824 * Then we check the keyboard ID. We should get 0xab83 under normal conditions. 825 * Some keyboards report different values, but the first byte is always 0xab or 826 * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this 827 * should make sure we don't try to set the LEDs on it. 828 */ 829 830 param[0] = param[1] = 0xa5; /* initialize with invalid values */ 831 if (ps2_command(ps2dev, param, ATKBD_CMD_GETID)) { 832 833/* 834 * If the get ID command failed, we check if we can at least set 835 * the LEDs on the keyboard. This should work on every keyboard out there. 836 * It also turns the LEDs off, which we want anyway. 837 */ 838 param[0] = 0; 839 if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS)) 840 return -1; 841 atkbd->id = 0xabba; 842 return 0; 843 } 844 845 if (!ps2_is_keyboard_id(param[0])) 846 return -1; 847 848 atkbd->id = (param[0] << 8) | param[1]; 849 850 if (atkbd->id == 0xaca1 && atkbd->translated) { 851 dev_err(&ps2dev->serio->dev, 852 "NCD terminal keyboards are only supported on non-translating controllers. " 853 "Use i8042.direct=1 to disable translation.\n"); 854 return -1; 855 } 856 857/* 858 * Make sure nothing is coming from the keyboard and disturbs our 859 * internal state. 860 */ 861 if (!atkbd_skip_deactivate) 862 atkbd_deactivate(atkbd); 863 864 return 0; 865} 866 867/* 868 * atkbd_select_set checks if a keyboard has a working Set 3 support, and 869 * sets it into that. Unfortunately there are keyboards that can be switched 870 * to Set 3, but don't work well in that (BTC Multimedia ...) 871 */ 872 873static int atkbd_select_set(struct atkbd *atkbd, int target_set, int allow_extra) 874{ 875 struct ps2dev *ps2dev = &atkbd->ps2dev; 876 unsigned char param[2]; 877 878 atkbd->extra = false; 879/* 880 * For known special keyboards we can go ahead and set the correct set. 881 * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and 882 * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards. 883 */ 884 885 if (atkbd->translated) 886 return 2; 887 888 if (atkbd->id == 0xaca1) { 889 param[0] = 3; 890 ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET); 891 return 3; 892 } 893 894 if (allow_extra) { 895 param[0] = 0x71; 896 if (!ps2_command(ps2dev, param, ATKBD_CMD_EX_ENABLE)) { 897 atkbd->extra = true; 898 return 2; 899 } 900 } 901 902 if (atkbd_terminal) { 903 ps2_command(ps2dev, param, ATKBD_CMD_SETALL_MB); 904 return 3; 905 } 906 907 if (target_set != 3) 908 return 2; 909 910 if (!ps2_command(ps2dev, param, ATKBD_CMD_OK_GETID)) { 911 atkbd->id = param[0] << 8 | param[1]; 912 return 2; 913 } 914 915 param[0] = 3; 916 if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET)) 917 return 2; 918 919 param[0] = 0; 920 if (ps2_command(ps2dev, param, ATKBD_CMD_GSCANSET)) 921 return 2; 922 923 if (param[0] != 3) { 924 param[0] = 2; 925 if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET)) 926 return 2; 927 } 928 929 ps2_command(ps2dev, param, ATKBD_CMD_SETALL_MBR); 930 931 return 3; 932} 933 934static int atkbd_reset_state(struct atkbd *atkbd) 935{ 936 struct ps2dev *ps2dev = &atkbd->ps2dev; 937 unsigned char param[1]; 938 939/* 940 * Set the LEDs to a predefined state (all off). 941 */ 942 943 param[0] = 0; 944 if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS)) 945 return -1; 946 947/* 948 * Set autorepeat to fastest possible. 949 */ 950 951 param[0] = 0; 952 if (ps2_command(ps2dev, param, ATKBD_CMD_SETREP)) 953 return -1; 954 955 return 0; 956} 957 958/* 959 * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a 960 * reboot. 961 */ 962 963static void atkbd_cleanup(struct serio *serio) 964{ 965 struct atkbd *atkbd = serio_get_drvdata(serio); 966 967 atkbd_disable(atkbd); 968 ps2_command(&atkbd->ps2dev, NULL, ATKBD_CMD_RESET_DEF); 969} 970 971 972/* 973 * atkbd_disconnect() closes and frees. 974 */ 975 976static void atkbd_disconnect(struct serio *serio) 977{ 978 struct atkbd *atkbd = serio_get_drvdata(serio); 979 980 sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group); 981 982 atkbd_disable(atkbd); 983 984 input_unregister_device(atkbd->dev); 985 986 /* 987 * Make sure we don't have a command in flight. 988 * Note that since atkbd->enabled is false event work will keep 989 * rescheduling itself until it gets canceled and will not try 990 * accessing freed input device or serio port. 991 */ 992 cancel_delayed_work_sync(&atkbd->event_work); 993 994 serio_close(serio); 995 serio_set_drvdata(serio, NULL); 996 kfree(atkbd); 997} 998 999/* 1000 * generate release events for the keycodes given in data 1001 */ 1002static void atkbd_apply_forced_release_keylist(struct atkbd* atkbd, 1003 const void *data) 1004{ 1005 const unsigned int *keys = data; 1006 unsigned int i; 1007 1008 if (atkbd->set == 2) 1009 for (i = 0; keys[i] != -1U; i++) 1010 __set_bit(keys[i], atkbd->force_release_mask); 1011} 1012 1013/* 1014 * Most special keys (Fn+F?) on Dell laptops do not generate release 1015 * events so we have to do it ourselves. 1016 */ 1017static unsigned int atkbd_dell_laptop_forced_release_keys[] = { 1018 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8f, 0x93, -1U 1019}; 1020 1021/* 1022 * Perform fixup for HP system that doesn't generate release 1023 * for its video switch 1024 */ 1025static unsigned int atkbd_hp_forced_release_keys[] = { 1026 0x94, -1U 1027}; 1028 1029/* 1030 * Samsung NC10,NC20 with Fn+F? key release not working 1031 */ 1032static unsigned int atkbd_samsung_forced_release_keys[] = { 1033 0x82, 0x83, 0x84, 0x86, 0x88, 0x89, 0xb3, 0xf7, 0xf9, -1U 1034}; 1035 1036/* 1037 * Amilo Pi 3525 key release for Fn+Volume keys not working 1038 */ 1039static unsigned int atkbd_amilo_pi3525_forced_release_keys[] = { 1040 0x20, 0xa0, 0x2e, 0xae, 0x30, 0xb0, -1U 1041}; 1042 1043/* 1044 * Amilo Xi 3650 key release for light touch bar not working 1045 */ 1046static unsigned int atkbd_amilo_xi3650_forced_release_keys[] = { 1047 0x67, 0xed, 0x90, 0xa2, 0x99, 0xa4, 0xae, 0xb0, -1U 1048}; 1049 1050/* 1051 * Soltech TA12 system with broken key release on volume keys and mute key 1052 */ 1053static unsigned int atkdb_soltech_ta12_forced_release_keys[] = { 1054 0xa0, 0xae, 0xb0, -1U 1055}; 1056 1057/* 1058 * Many notebooks don't send key release event for volume up/down 1059 * keys, with key list below common among them 1060 */ 1061static unsigned int atkbd_volume_forced_release_keys[] = { 1062 0xae, 0xb0, -1U 1063}; 1064 1065/* 1066 * OQO 01+ multimedia keys (64--66) generate e0 6x upon release whereas 1067 * they should be generating e4-e6 (0x80 | code). 1068 */ 1069static unsigned int atkbd_oqo_01plus_scancode_fixup(struct atkbd *atkbd, 1070 unsigned int code) 1071{ 1072 if (atkbd->translated && atkbd->emul == 1 && 1073 (code == 0x64 || code == 0x65 || code == 0x66)) { 1074 atkbd->emul = 0; 1075 code |= 0x80; 1076 } 1077 1078 return code; 1079} 1080 1081static int atkbd_get_keymap_from_fwnode(struct atkbd *atkbd) 1082{ 1083 struct device *dev = &atkbd->ps2dev.serio->dev; 1084 int i, n; 1085 u32 *ptr; 1086 u16 scancode, keycode; 1087 1088 /* Parse "linux,keymap" property */ 1089 n = device_property_count_u32(dev, "linux,keymap"); 1090 if (n <= 0 || n > ATKBD_KEYMAP_SIZE) 1091 return -ENXIO; 1092 1093 ptr = kcalloc(n, sizeof(u32), GFP_KERNEL); 1094 if (!ptr) 1095 return -ENOMEM; 1096 1097 if (device_property_read_u32_array(dev, "linux,keymap", ptr, n)) { 1098 dev_err(dev, "problem parsing FW keymap property\n"); 1099 kfree(ptr); 1100 return -EINVAL; 1101 } 1102 1103 memset(atkbd->keycode, 0, sizeof(atkbd->keycode)); 1104 for (i = 0; i < n; i++) { 1105 scancode = SCANCODE(ptr[i]); 1106 keycode = KEYCODE(ptr[i]); 1107 atkbd->keycode[scancode] = keycode; 1108 } 1109 1110 kfree(ptr); 1111 return 0; 1112} 1113 1114/* 1115 * atkbd_set_keycode_table() initializes keyboard's keycode table 1116 * according to the selected scancode set 1117 */ 1118 1119static void atkbd_set_keycode_table(struct atkbd *atkbd) 1120{ 1121 struct device *dev = &atkbd->ps2dev.serio->dev; 1122 unsigned int scancode; 1123 int i, j; 1124 1125 memset(atkbd->keycode, 0, sizeof(atkbd->keycode)); 1126 bitmap_zero(atkbd->force_release_mask, ATKBD_KEYMAP_SIZE); 1127 1128 if (!atkbd_get_keymap_from_fwnode(atkbd)) { 1129 dev_dbg(dev, "Using FW keymap\n"); 1130 } else if (atkbd->translated) { 1131 for (i = 0; i < 128; i++) { 1132 scancode = atkbd_unxlate_table[i]; 1133 atkbd->keycode[i] = atkbd_set2_keycode[scancode]; 1134 atkbd->keycode[i | 0x80] = atkbd_set2_keycode[scancode | 0x80]; 1135 if (atkbd->scroll) 1136 for (j = 0; j < ARRAY_SIZE(atkbd_scroll_keys); j++) 1137 if ((scancode | 0x80) == atkbd_scroll_keys[j].set2) 1138 atkbd->keycode[i | 0x80] = atkbd_scroll_keys[j].keycode; 1139 } 1140 } else if (atkbd->set == 3) { 1141 memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode)); 1142 } else { 1143 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode)); 1144 1145 if (atkbd->scroll) 1146 for (i = 0; i < ARRAY_SIZE(atkbd_scroll_keys); i++) { 1147 scancode = atkbd_scroll_keys[i].set2; 1148 atkbd->keycode[scancode] = atkbd_scroll_keys[i].keycode; 1149 } 1150 } 1151 1152/* 1153 * HANGEUL and HANJA keys do not send release events so we need to 1154 * generate such events ourselves 1155 */ 1156 scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANGEUL); 1157 atkbd->keycode[scancode] = KEY_HANGEUL; 1158 __set_bit(scancode, atkbd->force_release_mask); 1159 1160 scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANJA); 1161 atkbd->keycode[scancode] = KEY_HANJA; 1162 __set_bit(scancode, atkbd->force_release_mask); 1163 1164/* 1165 * Perform additional fixups 1166 */ 1167 if (atkbd_platform_fixup) 1168 atkbd_platform_fixup(atkbd, atkbd_platform_fixup_data); 1169} 1170 1171/* 1172 * atkbd_set_device_attrs() sets up keyboard's input device structure 1173 */ 1174 1175static void atkbd_set_device_attrs(struct atkbd *atkbd) 1176{ 1177 struct input_dev *input_dev = atkbd->dev; 1178 int i; 1179 1180 if (atkbd->extra) 1181 snprintf(atkbd->name, sizeof(atkbd->name), 1182 "AT Set 2 Extra keyboard"); 1183 else 1184 snprintf(atkbd->name, sizeof(atkbd->name), 1185 "AT %s Set %d keyboard", 1186 atkbd->translated ? "Translated" : "Raw", atkbd->set); 1187 1188 snprintf(atkbd->phys, sizeof(atkbd->phys), 1189 "%s/input0", atkbd->ps2dev.serio->phys); 1190 1191 input_dev->name = atkbd->name; 1192 input_dev->phys = atkbd->phys; 1193 input_dev->id.bustype = BUS_I8042; 1194 input_dev->id.vendor = 0x0001; 1195 input_dev->id.product = atkbd->translated ? 1 : atkbd->set; 1196 input_dev->id.version = atkbd->id; 1197 input_dev->event = atkbd_event; 1198 input_dev->dev.parent = &atkbd->ps2dev.serio->dev; 1199 1200 input_set_drvdata(input_dev, atkbd); 1201 1202 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | 1203 BIT_MASK(EV_MSC); 1204 1205 if (atkbd->write) { 1206 input_dev->evbit[0] |= BIT_MASK(EV_LED); 1207 input_dev->ledbit[0] = BIT_MASK(LED_NUML) | 1208 BIT_MASK(LED_CAPSL) | BIT_MASK(LED_SCROLLL); 1209 } 1210 1211 if (atkbd->extra) 1212 input_dev->ledbit[0] |= BIT_MASK(LED_COMPOSE) | 1213 BIT_MASK(LED_SUSPEND) | BIT_MASK(LED_SLEEP) | 1214 BIT_MASK(LED_MUTE) | BIT_MASK(LED_MISC); 1215 1216 if (!atkbd->softrepeat) { 1217 input_dev->rep[REP_DELAY] = 250; 1218 input_dev->rep[REP_PERIOD] = 33; 1219 } 1220 1221 input_dev->mscbit[0] = atkbd->softraw ? BIT_MASK(MSC_SCAN) : 1222 BIT_MASK(MSC_RAW) | BIT_MASK(MSC_SCAN); 1223 1224 if (atkbd->scroll) { 1225 input_dev->evbit[0] |= BIT_MASK(EV_REL); 1226 input_dev->relbit[0] = BIT_MASK(REL_WHEEL) | 1227 BIT_MASK(REL_HWHEEL); 1228 __set_bit(BTN_MIDDLE, input_dev->keybit); 1229 } 1230 1231 input_dev->keycode = atkbd->keycode; 1232 input_dev->keycodesize = sizeof(unsigned short); 1233 input_dev->keycodemax = ARRAY_SIZE(atkbd_set2_keycode); 1234 1235 for (i = 0; i < ATKBD_KEYMAP_SIZE; i++) { 1236 if (atkbd->keycode[i] != KEY_RESERVED && 1237 atkbd->keycode[i] != ATKBD_KEY_NULL && 1238 atkbd->keycode[i] < ATKBD_SPECIAL) { 1239 __set_bit(atkbd->keycode[i], input_dev->keybit); 1240 } 1241 } 1242} 1243 1244static void atkbd_parse_fwnode_data(struct serio *serio) 1245{ 1246 struct atkbd *atkbd = serio_get_drvdata(serio); 1247 struct device *dev = &serio->dev; 1248 int n; 1249 1250 /* Parse "function-row-physmap" property */ 1251 n = device_property_count_u32(dev, "function-row-physmap"); 1252 if (n > 0 && n <= MAX_FUNCTION_ROW_KEYS && 1253 !device_property_read_u32_array(dev, "function-row-physmap", 1254 atkbd->function_row_physmap, n)) { 1255 atkbd->num_function_row_keys = n; 1256 dev_dbg(dev, "FW reported %d function-row key locations\n", n); 1257 } 1258} 1259 1260/* 1261 * atkbd_connect() is called when the serio module finds an interface 1262 * that isn't handled yet by an appropriate device driver. We check if 1263 * there is an AT keyboard out there and if yes, we register ourselves 1264 * to the input module. 1265 */ 1266 1267static int atkbd_connect(struct serio *serio, struct serio_driver *drv) 1268{ 1269 struct atkbd *atkbd; 1270 struct input_dev *dev; 1271 int err = -ENOMEM; 1272 1273 atkbd = kzalloc(sizeof(struct atkbd), GFP_KERNEL); 1274 dev = input_allocate_device(); 1275 if (!atkbd || !dev) 1276 goto fail1; 1277 1278 atkbd->dev = dev; 1279 ps2_init(&atkbd->ps2dev, serio); 1280 INIT_DELAYED_WORK(&atkbd->event_work, atkbd_event_work); 1281 mutex_init(&atkbd->mutex); 1282 1283 switch (serio->id.type) { 1284 1285 case SERIO_8042_XL: 1286 atkbd->translated = true; 1287 fallthrough; 1288 1289 case SERIO_8042: 1290 if (serio->write) 1291 atkbd->write = true; 1292 break; 1293 } 1294 1295 atkbd->softraw = atkbd_softraw; 1296 atkbd->softrepeat = atkbd_softrepeat; 1297 atkbd->scroll = atkbd_scroll; 1298 1299 if (atkbd->softrepeat) 1300 atkbd->softraw = true; 1301 1302 serio_set_drvdata(serio, atkbd); 1303 1304 err = serio_open(serio, drv); 1305 if (err) 1306 goto fail2; 1307 1308 if (atkbd->write) { 1309 1310 if (atkbd_probe(atkbd)) { 1311 err = -ENODEV; 1312 goto fail3; 1313 } 1314 1315 atkbd->set = atkbd_select_set(atkbd, atkbd_set, atkbd_extra); 1316 atkbd_reset_state(atkbd); 1317 1318 } else { 1319 atkbd->set = 2; 1320 atkbd->id = 0xab00; 1321 } 1322 1323 atkbd_parse_fwnode_data(serio); 1324 1325 atkbd_set_keycode_table(atkbd); 1326 atkbd_set_device_attrs(atkbd); 1327 1328 err = sysfs_create_group(&serio->dev.kobj, &atkbd_attribute_group); 1329 if (err) 1330 goto fail3; 1331 1332 atkbd_enable(atkbd); 1333 if (serio->write) 1334 atkbd_activate(atkbd); 1335 1336 err = input_register_device(atkbd->dev); 1337 if (err) 1338 goto fail4; 1339 1340 return 0; 1341 1342 fail4: sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group); 1343 fail3: serio_close(serio); 1344 fail2: serio_set_drvdata(serio, NULL); 1345 fail1: input_free_device(dev); 1346 kfree(atkbd); 1347 return err; 1348} 1349 1350/* 1351 * atkbd_reconnect() tries to restore keyboard into a sane state and is 1352 * most likely called on resume. 1353 */ 1354 1355static int atkbd_reconnect(struct serio *serio) 1356{ 1357 struct atkbd *atkbd = serio_get_drvdata(serio); 1358 struct serio_driver *drv = serio->drv; 1359 int retval = -1; 1360 1361 if (!atkbd || !drv) { 1362 dev_dbg(&serio->dev, 1363 "reconnect request, but serio is disconnected, ignoring...\n"); 1364 return -1; 1365 } 1366 1367 mutex_lock(&atkbd->mutex); 1368 1369 atkbd_disable(atkbd); 1370 1371 if (atkbd->write) { 1372 if (atkbd_probe(atkbd)) 1373 goto out; 1374 1375 if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra)) 1376 goto out; 1377 1378 /* 1379 * Restore LED state and repeat rate. While input core 1380 * will do this for us at resume time reconnect may happen 1381 * because user requested it via sysfs or simply because 1382 * keyboard was unplugged and plugged in again so we need 1383 * to do it ourselves here. 1384 */ 1385 atkbd_set_leds(atkbd); 1386 if (!atkbd->softrepeat) 1387 atkbd_set_repeat_rate(atkbd); 1388 1389 } 1390 1391 /* 1392 * Reset our state machine in case reconnect happened in the middle 1393 * of multi-byte scancode. 1394 */ 1395 atkbd->xl_bit = 0; 1396 atkbd->emul = 0; 1397 1398 atkbd_enable(atkbd); 1399 if (atkbd->write) 1400 atkbd_activate(atkbd); 1401 1402 retval = 0; 1403 1404 out: 1405 mutex_unlock(&atkbd->mutex); 1406 return retval; 1407} 1408 1409static const struct serio_device_id atkbd_serio_ids[] = { 1410 { 1411 .type = SERIO_8042, 1412 .proto = SERIO_ANY, 1413 .id = SERIO_ANY, 1414 .extra = SERIO_ANY, 1415 }, 1416 { 1417 .type = SERIO_8042_XL, 1418 .proto = SERIO_ANY, 1419 .id = SERIO_ANY, 1420 .extra = SERIO_ANY, 1421 }, 1422 { 1423 .type = SERIO_RS232, 1424 .proto = SERIO_PS2SER, 1425 .id = SERIO_ANY, 1426 .extra = SERIO_ANY, 1427 }, 1428 { 0 } 1429}; 1430 1431MODULE_DEVICE_TABLE(serio, atkbd_serio_ids); 1432 1433static struct serio_driver atkbd_drv = { 1434 .driver = { 1435 .name = "atkbd", 1436 }, 1437 .description = DRIVER_DESC, 1438 .id_table = atkbd_serio_ids, 1439 .interrupt = atkbd_interrupt, 1440 .connect = atkbd_connect, 1441 .reconnect = atkbd_reconnect, 1442 .disconnect = atkbd_disconnect, 1443 .cleanup = atkbd_cleanup, 1444}; 1445 1446static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf, 1447 ssize_t (*handler)(struct atkbd *, char *)) 1448{ 1449 struct serio *serio = to_serio_port(dev); 1450 struct atkbd *atkbd = serio_get_drvdata(serio); 1451 1452 return handler(atkbd, buf); 1453} 1454 1455static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count, 1456 ssize_t (*handler)(struct atkbd *, const char *, size_t)) 1457{ 1458 struct serio *serio = to_serio_port(dev); 1459 struct atkbd *atkbd = serio_get_drvdata(serio); 1460 int retval; 1461 1462 retval = mutex_lock_interruptible(&atkbd->mutex); 1463 if (retval) 1464 return retval; 1465 1466 atkbd_disable(atkbd); 1467 retval = handler(atkbd, buf, count); 1468 atkbd_enable(atkbd); 1469 1470 mutex_unlock(&atkbd->mutex); 1471 1472 return retval; 1473} 1474 1475static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf) 1476{ 1477 return sprintf(buf, "%d\n", atkbd->extra ? 1 : 0); 1478} 1479 1480static ssize_t atkbd_set_extra(struct atkbd *atkbd, const char *buf, size_t count) 1481{ 1482 struct input_dev *old_dev, *new_dev; 1483 unsigned int value; 1484 int err; 1485 bool old_extra; 1486 unsigned char old_set; 1487 1488 if (!atkbd->write) 1489 return -EIO; 1490 1491 err = kstrtouint(buf, 10, &value); 1492 if (err) 1493 return err; 1494 1495 if (value > 1) 1496 return -EINVAL; 1497 1498 if (atkbd->extra != value) { 1499 /* 1500 * Since device's properties will change we need to 1501 * unregister old device. But allocate and register 1502 * new one first to make sure we have it. 1503 */ 1504 old_dev = atkbd->dev; 1505 old_extra = atkbd->extra; 1506 old_set = atkbd->set; 1507 1508 new_dev = input_allocate_device(); 1509 if (!new_dev) 1510 return -ENOMEM; 1511 1512 atkbd->dev = new_dev; 1513 atkbd->set = atkbd_select_set(atkbd, atkbd->set, value); 1514 atkbd_reset_state(atkbd); 1515 atkbd_activate(atkbd); 1516 atkbd_set_keycode_table(atkbd); 1517 atkbd_set_device_attrs(atkbd); 1518 1519 err = input_register_device(atkbd->dev); 1520 if (err) { 1521 input_free_device(new_dev); 1522 1523 atkbd->dev = old_dev; 1524 atkbd->set = atkbd_select_set(atkbd, old_set, old_extra); 1525 atkbd_set_keycode_table(atkbd); 1526 atkbd_set_device_attrs(atkbd); 1527 1528 return err; 1529 } 1530 input_unregister_device(old_dev); 1531 1532 } 1533 return count; 1534} 1535 1536static ssize_t atkbd_show_force_release(struct atkbd *atkbd, char *buf) 1537{ 1538 size_t len = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", 1539 ATKBD_KEYMAP_SIZE, atkbd->force_release_mask); 1540 1541 buf[len++] = '\n'; 1542 buf[len] = '\0'; 1543 1544 return len; 1545} 1546 1547static ssize_t atkbd_set_force_release(struct atkbd *atkbd, 1548 const char *buf, size_t count) 1549{ 1550 /* 64 bytes on stack should be acceptable */ 1551 DECLARE_BITMAP(new_mask, ATKBD_KEYMAP_SIZE); 1552 int err; 1553 1554 err = bitmap_parselist(buf, new_mask, ATKBD_KEYMAP_SIZE); 1555 if (err) 1556 return err; 1557 1558 memcpy(atkbd->force_release_mask, new_mask, sizeof(atkbd->force_release_mask)); 1559 return count; 1560} 1561 1562 1563static ssize_t atkbd_show_scroll(struct atkbd *atkbd, char *buf) 1564{ 1565 return sprintf(buf, "%d\n", atkbd->scroll ? 1 : 0); 1566} 1567 1568static ssize_t atkbd_set_scroll(struct atkbd *atkbd, const char *buf, size_t count) 1569{ 1570 struct input_dev *old_dev, *new_dev; 1571 unsigned int value; 1572 int err; 1573 bool old_scroll; 1574 1575 err = kstrtouint(buf, 10, &value); 1576 if (err) 1577 return err; 1578 1579 if (value > 1) 1580 return -EINVAL; 1581 1582 if (atkbd->scroll != value) { 1583 old_dev = atkbd->dev; 1584 old_scroll = atkbd->scroll; 1585 1586 new_dev = input_allocate_device(); 1587 if (!new_dev) 1588 return -ENOMEM; 1589 1590 atkbd->dev = new_dev; 1591 atkbd->scroll = value; 1592 atkbd_set_keycode_table(atkbd); 1593 atkbd_set_device_attrs(atkbd); 1594 1595 err = input_register_device(atkbd->dev); 1596 if (err) { 1597 input_free_device(new_dev); 1598 1599 atkbd->scroll = old_scroll; 1600 atkbd->dev = old_dev; 1601 atkbd_set_keycode_table(atkbd); 1602 atkbd_set_device_attrs(atkbd); 1603 1604 return err; 1605 } 1606 input_unregister_device(old_dev); 1607 } 1608 return count; 1609} 1610 1611static ssize_t atkbd_show_set(struct atkbd *atkbd, char *buf) 1612{ 1613 return sprintf(buf, "%d\n", atkbd->set); 1614} 1615 1616static ssize_t atkbd_set_set(struct atkbd *atkbd, const char *buf, size_t count) 1617{ 1618 struct input_dev *old_dev, *new_dev; 1619 unsigned int value; 1620 int err; 1621 unsigned char old_set; 1622 bool old_extra; 1623 1624 if (!atkbd->write) 1625 return -EIO; 1626 1627 err = kstrtouint(buf, 10, &value); 1628 if (err) 1629 return err; 1630 1631 if (value != 2 && value != 3) 1632 return -EINVAL; 1633 1634 if (atkbd->set != value) { 1635 old_dev = atkbd->dev; 1636 old_extra = atkbd->extra; 1637 old_set = atkbd->set; 1638 1639 new_dev = input_allocate_device(); 1640 if (!new_dev) 1641 return -ENOMEM; 1642 1643 atkbd->dev = new_dev; 1644 atkbd->set = atkbd_select_set(atkbd, value, atkbd->extra); 1645 atkbd_reset_state(atkbd); 1646 atkbd_activate(atkbd); 1647 atkbd_set_keycode_table(atkbd); 1648 atkbd_set_device_attrs(atkbd); 1649 1650 err = input_register_device(atkbd->dev); 1651 if (err) { 1652 input_free_device(new_dev); 1653 1654 atkbd->dev = old_dev; 1655 atkbd->set = atkbd_select_set(atkbd, old_set, old_extra); 1656 atkbd_set_keycode_table(atkbd); 1657 atkbd_set_device_attrs(atkbd); 1658 1659 return err; 1660 } 1661 input_unregister_device(old_dev); 1662 } 1663 return count; 1664} 1665 1666static ssize_t atkbd_show_softrepeat(struct atkbd *atkbd, char *buf) 1667{ 1668 return sprintf(buf, "%d\n", atkbd->softrepeat ? 1 : 0); 1669} 1670 1671static ssize_t atkbd_set_softrepeat(struct atkbd *atkbd, const char *buf, size_t count) 1672{ 1673 struct input_dev *old_dev, *new_dev; 1674 unsigned int value; 1675 int err; 1676 bool old_softrepeat, old_softraw; 1677 1678 if (!atkbd->write) 1679 return -EIO; 1680 1681 err = kstrtouint(buf, 10, &value); 1682 if (err) 1683 return err; 1684 1685 if (value > 1) 1686 return -EINVAL; 1687 1688 if (atkbd->softrepeat != value) { 1689 old_dev = atkbd->dev; 1690 old_softrepeat = atkbd->softrepeat; 1691 old_softraw = atkbd->softraw; 1692 1693 new_dev = input_allocate_device(); 1694 if (!new_dev) 1695 return -ENOMEM; 1696 1697 atkbd->dev = new_dev; 1698 atkbd->softrepeat = value; 1699 if (atkbd->softrepeat) 1700 atkbd->softraw = true; 1701 atkbd_set_device_attrs(atkbd); 1702 1703 err = input_register_device(atkbd->dev); 1704 if (err) { 1705 input_free_device(new_dev); 1706 1707 atkbd->dev = old_dev; 1708 atkbd->softrepeat = old_softrepeat; 1709 atkbd->softraw = old_softraw; 1710 atkbd_set_device_attrs(atkbd); 1711 1712 return err; 1713 } 1714 input_unregister_device(old_dev); 1715 } 1716 return count; 1717} 1718 1719 1720static ssize_t atkbd_show_softraw(struct atkbd *atkbd, char *buf) 1721{ 1722 return sprintf(buf, "%d\n", atkbd->softraw ? 1 : 0); 1723} 1724 1725static ssize_t atkbd_set_softraw(struct atkbd *atkbd, const char *buf, size_t count) 1726{ 1727 struct input_dev *old_dev, *new_dev; 1728 unsigned int value; 1729 int err; 1730 bool old_softraw; 1731 1732 err = kstrtouint(buf, 10, &value); 1733 if (err) 1734 return err; 1735 1736 if (value > 1) 1737 return -EINVAL; 1738 1739 if (atkbd->softraw != value) { 1740 old_dev = atkbd->dev; 1741 old_softraw = atkbd->softraw; 1742 1743 new_dev = input_allocate_device(); 1744 if (!new_dev) 1745 return -ENOMEM; 1746 1747 atkbd->dev = new_dev; 1748 atkbd->softraw = value; 1749 atkbd_set_device_attrs(atkbd); 1750 1751 err = input_register_device(atkbd->dev); 1752 if (err) { 1753 input_free_device(new_dev); 1754 1755 atkbd->dev = old_dev; 1756 atkbd->softraw = old_softraw; 1757 atkbd_set_device_attrs(atkbd); 1758 1759 return err; 1760 } 1761 input_unregister_device(old_dev); 1762 } 1763 return count; 1764} 1765 1766static ssize_t atkbd_show_err_count(struct atkbd *atkbd, char *buf) 1767{ 1768 return sprintf(buf, "%lu\n", atkbd->err_count); 1769} 1770 1771static int __init atkbd_setup_forced_release(const struct dmi_system_id *id) 1772{ 1773 atkbd_platform_fixup = atkbd_apply_forced_release_keylist; 1774 atkbd_platform_fixup_data = id->driver_data; 1775 1776 return 1; 1777} 1778 1779static int __init atkbd_setup_scancode_fixup(const struct dmi_system_id *id) 1780{ 1781 atkbd_platform_scancode_fixup = id->driver_data; 1782 1783 return 1; 1784} 1785 1786static int __init atkbd_deactivate_fixup(const struct dmi_system_id *id) 1787{ 1788 atkbd_skip_deactivate = true; 1789 return 1; 1790} 1791 1792/* 1793 * NOTE: do not add any more "force release" quirks to this table. The 1794 * task of adjusting list of keys that should be "released" automatically 1795 * by the driver is now delegated to userspace tools, such as udev, so 1796 * submit such quirks there. 1797 */ 1798static const struct dmi_system_id atkbd_dmi_quirk_table[] __initconst = { 1799 { 1800 .matches = { 1801 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1802 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */ 1803 }, 1804 .callback = atkbd_setup_forced_release, 1805 .driver_data = atkbd_dell_laptop_forced_release_keys, 1806 }, 1807 { 1808 .matches = { 1809 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"), 1810 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */ 1811 }, 1812 .callback = atkbd_setup_forced_release, 1813 .driver_data = atkbd_dell_laptop_forced_release_keys, 1814 }, 1815 { 1816 .matches = { 1817 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1818 DMI_MATCH(DMI_PRODUCT_NAME, "HP 2133"), 1819 }, 1820 .callback = atkbd_setup_forced_release, 1821 .driver_data = atkbd_hp_forced_release_keys, 1822 }, 1823 { 1824 .matches = { 1825 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1826 DMI_MATCH(DMI_PRODUCT_NAME, "Pavilion ZV6100"), 1827 }, 1828 .callback = atkbd_setup_forced_release, 1829 .driver_data = atkbd_volume_forced_release_keys, 1830 }, 1831 { 1832 .matches = { 1833 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1834 DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4000"), 1835 }, 1836 .callback = atkbd_setup_forced_release, 1837 .driver_data = atkbd_volume_forced_release_keys, 1838 }, 1839 { 1840 .matches = { 1841 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1842 DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4100"), 1843 }, 1844 .callback = atkbd_setup_forced_release, 1845 .driver_data = atkbd_volume_forced_release_keys, 1846 }, 1847 { 1848 .matches = { 1849 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1850 DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4200"), 1851 }, 1852 .callback = atkbd_setup_forced_release, 1853 .driver_data = atkbd_volume_forced_release_keys, 1854 }, 1855 { 1856 /* Inventec Symphony */ 1857 .matches = { 1858 DMI_MATCH(DMI_SYS_VENDOR, "INVENTEC"), 1859 DMI_MATCH(DMI_PRODUCT_NAME, "SYMPHONY 6.0/7.0"), 1860 }, 1861 .callback = atkbd_setup_forced_release, 1862 .driver_data = atkbd_volume_forced_release_keys, 1863 }, 1864 { 1865 /* Samsung NC10 */ 1866 .matches = { 1867 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 1868 DMI_MATCH(DMI_PRODUCT_NAME, "NC10"), 1869 }, 1870 .callback = atkbd_setup_forced_release, 1871 .driver_data = atkbd_samsung_forced_release_keys, 1872 }, 1873 { 1874 /* Samsung NC20 */ 1875 .matches = { 1876 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 1877 DMI_MATCH(DMI_PRODUCT_NAME, "NC20"), 1878 }, 1879 .callback = atkbd_setup_forced_release, 1880 .driver_data = atkbd_samsung_forced_release_keys, 1881 }, 1882 { 1883 /* Samsung SQ45S70S */ 1884 .matches = { 1885 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 1886 DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"), 1887 }, 1888 .callback = atkbd_setup_forced_release, 1889 .driver_data = atkbd_samsung_forced_release_keys, 1890 }, 1891 { 1892 /* Fujitsu Amilo PA 1510 */ 1893 .matches = { 1894 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 1895 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pa 1510"), 1896 }, 1897 .callback = atkbd_setup_forced_release, 1898 .driver_data = atkbd_volume_forced_release_keys, 1899 }, 1900 { 1901 /* Fujitsu Amilo Pi 3525 */ 1902 .matches = { 1903 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 1904 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pi 3525"), 1905 }, 1906 .callback = atkbd_setup_forced_release, 1907 .driver_data = atkbd_amilo_pi3525_forced_release_keys, 1908 }, 1909 { 1910 /* Fujitsu Amilo Xi 3650 */ 1911 .matches = { 1912 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 1913 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xi 3650"), 1914 }, 1915 .callback = atkbd_setup_forced_release, 1916 .driver_data = atkbd_amilo_xi3650_forced_release_keys, 1917 }, 1918 { 1919 .matches = { 1920 DMI_MATCH(DMI_SYS_VENDOR, "Soltech Corporation"), 1921 DMI_MATCH(DMI_PRODUCT_NAME, "TA12"), 1922 }, 1923 .callback = atkbd_setup_forced_release, 1924 .driver_data = atkdb_soltech_ta12_forced_release_keys, 1925 }, 1926 { 1927 /* OQO Model 01+ */ 1928 .matches = { 1929 DMI_MATCH(DMI_SYS_VENDOR, "OQO"), 1930 DMI_MATCH(DMI_PRODUCT_NAME, "ZEPTO"), 1931 }, 1932 .callback = atkbd_setup_scancode_fixup, 1933 .driver_data = atkbd_oqo_01plus_scancode_fixup, 1934 }, 1935 { 1936 .matches = { 1937 DMI_MATCH(DMI_SYS_VENDOR, "LG Electronics"), 1938 }, 1939 .callback = atkbd_deactivate_fixup, 1940 }, 1941 { } 1942}; 1943 1944static int __init atkbd_init(void) 1945{ 1946 dmi_check_system(atkbd_dmi_quirk_table); 1947 1948 return serio_register_driver(&atkbd_drv); 1949} 1950 1951static void __exit atkbd_exit(void) 1952{ 1953 serio_unregister_driver(&atkbd_drv); 1954} 1955 1956module_init(atkbd_init); 1957module_exit(atkbd_exit); 1958