11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * AT and PS/2 keyboard driver 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 1999-2002 Vojtech Pavlik 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 91da177e4SLinus Torvalds * under the terms of the GNU General Public License version 2 as published by 101da177e4SLinus Torvalds * the Free Software Foundation. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * This driver can handle standard AT keyboards and PS/2 keyboards in 151da177e4SLinus Torvalds * Translated and Raw Set 2 and Set 3, as well as AT keyboards on dumb 161da177e4SLinus Torvalds * input-only controllers and AT keyboards connected over a one way RS232 171da177e4SLinus Torvalds * converter. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <linux/delay.h> 211da177e4SLinus Torvalds #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/slab.h> 231da177e4SLinus Torvalds #include <linux/interrupt.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/input.h> 261da177e4SLinus Torvalds #include <linux/serio.h> 271da177e4SLinus Torvalds #include <linux/workqueue.h> 281da177e4SLinus Torvalds #include <linux/libps2.h> 2933d3f07aSIngo Molnar #include <linux/mutex.h> 30554101e3SGiel de Nijs #include <linux/dmi.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #define DRIVER_DESC "AT and PS/2 keyboard driver" 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); 351da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 361da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds static int atkbd_set = 2; 391da177e4SLinus Torvalds module_param_named(set, atkbd_set, int, 0); 401da177e4SLinus Torvalds MODULE_PARM_DESC(set, "Select keyboard code set (2 = default, 3 = PS/2 native)"); 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #if defined(__i386__) || defined(__x86_64__) || defined(__hppa__) 431da177e4SLinus Torvalds static int atkbd_reset; 441da177e4SLinus Torvalds #else 451da177e4SLinus Torvalds static int atkbd_reset = 1; 461da177e4SLinus Torvalds #endif 471da177e4SLinus Torvalds module_param_named(reset, atkbd_reset, bool, 0); 481da177e4SLinus Torvalds MODULE_PARM_DESC(reset, "Reset keyboard during initialization"); 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds static int atkbd_softrepeat; 511da177e4SLinus Torvalds module_param_named(softrepeat, atkbd_softrepeat, bool, 0); 521da177e4SLinus Torvalds MODULE_PARM_DESC(softrepeat, "Use software keyboard repeat"); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds static int atkbd_softraw = 1; 551da177e4SLinus Torvalds module_param_named(softraw, atkbd_softraw, bool, 0); 561da177e4SLinus Torvalds MODULE_PARM_DESC(softraw, "Use software generated rawmode"); 571da177e4SLinus Torvalds 580ae051a1SDmitry Torokhov static int atkbd_scroll; 591da177e4SLinus Torvalds module_param_named(scroll, atkbd_scroll, bool, 0); 601da177e4SLinus Torvalds MODULE_PARM_DESC(scroll, "Enable scroll-wheel on MS Office and similar keyboards"); 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds static int atkbd_extra; 631da177e4SLinus Torvalds module_param_named(extra, atkbd_extra, bool, 0); 641da177e4SLinus Torvalds MODULE_PARM_DESC(extra, "Enable extra LEDs and keys on IBM RapidAcces, EzKey and similar keyboards"); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * Scancode to keycode tables. These are just the default setting, and 680211a9c8SFrederik Schwarzer * are loadable via a userland utility. 691da177e4SLinus Torvalds */ 701da177e4SLinus Torvalds 711ba36e11SDmitry Torokhov #define ATKBD_KEYMAP_SIZE 512 721ba36e11SDmitry Torokhov 731ba36e11SDmitry Torokhov static const unsigned short atkbd_set2_keycode[ATKBD_KEYMAP_SIZE] = { 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds #ifdef CONFIG_KEYBOARD_ATKBD_HP_KEYCODES 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* XXX: need a more general approach */ 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds #include "hpps2atkbd.h" /* include the keyboard scancodes */ 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #else 821da177e4SLinus Torvalds 0, 67, 65, 63, 61, 59, 60, 88, 0, 68, 66, 64, 62, 15, 41,117, 831da177e4SLinus Torvalds 0, 56, 42, 93, 29, 16, 2, 0, 0, 0, 44, 31, 30, 17, 3, 0, 841da177e4SLinus Torvalds 0, 46, 45, 32, 18, 5, 4, 95, 0, 57, 47, 33, 20, 19, 6,183, 851da177e4SLinus Torvalds 0, 49, 48, 35, 34, 21, 7,184, 0, 0, 50, 36, 22, 8, 9,185, 861da177e4SLinus Torvalds 0, 51, 37, 23, 24, 11, 10, 0, 0, 52, 53, 38, 39, 25, 12, 0, 871da177e4SLinus Torvalds 0, 89, 40, 0, 26, 13, 0, 0, 58, 54, 28, 27, 0, 43, 0, 85, 881da177e4SLinus Torvalds 0, 86, 91, 90, 92, 0, 14, 94, 0, 79,124, 75, 71,121, 0, 0, 891da177e4SLinus Torvalds 82, 83, 80, 76, 77, 72, 1, 69, 87, 78, 81, 74, 55, 73, 70, 99, 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 921da177e4SLinus Torvalds 217,100,255, 0, 97,165, 0, 0,156, 0, 0, 0, 0, 0, 0,125, 931da177e4SLinus Torvalds 173,114, 0,113, 0, 0, 0,126,128, 0, 0,140, 0, 0, 0,127, 9472a42f24SHans de Goede 159, 0,115, 0,164, 0, 0,116,158, 0,172,166, 0, 0, 0,142, 951da177e4SLinus Torvalds 157, 0, 0, 0, 0, 0, 0, 0,155, 0, 98, 0, 0,163, 0, 0, 961da177e4SLinus Torvalds 226, 0, 0, 0, 0, 0, 0, 0, 0,255, 96, 0, 0, 0,143, 0, 971da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0,107, 0,105,102, 0, 0,112, 981da177e4SLinus Torvalds 110,111,108,112,106,103, 0,119, 0,118,109, 0, 99,104,119, 0, 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds 0, 0, 0, 65, 99, 1011da177e4SLinus Torvalds #endif 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 1041ba36e11SDmitry Torokhov static const unsigned short atkbd_set3_keycode[ATKBD_KEYMAP_SIZE] = { 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 59, 1,138,128,129,130, 15, 41, 60, 1071da177e4SLinus Torvalds 131, 29, 42, 86, 58, 16, 2, 61,133, 56, 44, 31, 30, 17, 3, 62, 1081da177e4SLinus Torvalds 134, 46, 45, 32, 18, 5, 4, 63,135, 57, 47, 33, 20, 19, 6, 64, 1091da177e4SLinus Torvalds 136, 49, 48, 35, 34, 21, 7, 65,137,100, 50, 36, 22, 8, 9, 66, 1101da177e4SLinus Torvalds 125, 51, 37, 23, 24, 11, 10, 67,126, 52, 53, 38, 39, 25, 12, 68, 1111da177e4SLinus Torvalds 113,114, 40, 43, 26, 13, 87, 99, 97, 54, 28, 27, 43, 43, 88, 70, 1121da177e4SLinus Torvalds 108,105,119,103,111,107, 14,110, 0, 79,106, 75, 71,109,102,104, 1131da177e4SLinus Torvalds 82, 83, 80, 76, 77, 72, 69, 98, 0, 96, 81, 0, 78, 73, 55,183, 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds 184,185,186,187, 74, 94, 92, 93, 0, 0, 0,125,126,127,112, 0, 11672a42f24SHans de Goede 0,139,172,163,165,115,152,172,166,140,160,154,113,114,167,168, 1171da177e4SLinus Torvalds 148,149,147,140 1181da177e4SLinus Torvalds }; 1191da177e4SLinus Torvalds 120f6d65610SDmitry Torokhov static const unsigned short atkbd_unxlate_table[128] = { 1211da177e4SLinus Torvalds 0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13, 1221da177e4SLinus Torvalds 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27, 1231da177e4SLinus Torvalds 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42, 1241da177e4SLinus Torvalds 50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88, 5, 6, 4, 12, 3, 1251da177e4SLinus Torvalds 11, 2, 10, 1, 9,119,126,108,117,125,123,107,115,116,121,105, 1261da177e4SLinus Torvalds 114,122,112,113,127, 96, 97,120, 7, 15, 23, 31, 39, 47, 55, 63, 1271da177e4SLinus Torvalds 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111, 1281da177e4SLinus Torvalds 19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110 1291da177e4SLinus Torvalds }; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds #define ATKBD_CMD_SETLEDS 0x10ed 1321da177e4SLinus Torvalds #define ATKBD_CMD_GSCANSET 0x11f0 1331da177e4SLinus Torvalds #define ATKBD_CMD_SSCANSET 0x10f0 1341da177e4SLinus Torvalds #define ATKBD_CMD_GETID 0x02f2 1351da177e4SLinus Torvalds #define ATKBD_CMD_SETREP 0x10f3 1361da177e4SLinus Torvalds #define ATKBD_CMD_ENABLE 0x00f4 1371da177e4SLinus Torvalds #define ATKBD_CMD_RESET_DIS 0x00f5 1381da177e4SLinus Torvalds #define ATKBD_CMD_SETALL_MBR 0x00fa 1391da177e4SLinus Torvalds #define ATKBD_CMD_RESET_BAT 0x02ff 1401da177e4SLinus Torvalds #define ATKBD_CMD_RESEND 0x00fe 1411da177e4SLinus Torvalds #define ATKBD_CMD_EX_ENABLE 0x10ea 1421da177e4SLinus Torvalds #define ATKBD_CMD_EX_SETLEDS 0x20eb 1431da177e4SLinus Torvalds #define ATKBD_CMD_OK_GETID 0x02e8 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds #define ATKBD_RET_ACK 0xfa 1461da177e4SLinus Torvalds #define ATKBD_RET_NAK 0xfe 1471da177e4SLinus Torvalds #define ATKBD_RET_BAT 0xaa 1481da177e4SLinus Torvalds #define ATKBD_RET_EMUL0 0xe0 1491da177e4SLinus Torvalds #define ATKBD_RET_EMUL1 0xe1 1501da177e4SLinus Torvalds #define ATKBD_RET_RELEASE 0xf0 1510ae051a1SDmitry Torokhov #define ATKBD_RET_HANJA 0xf1 1520ae051a1SDmitry Torokhov #define ATKBD_RET_HANGEUL 0xf2 1531da177e4SLinus Torvalds #define ATKBD_RET_ERR 0xff 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds #define ATKBD_KEY_UNKNOWN 0 1561da177e4SLinus Torvalds #define ATKBD_KEY_NULL 255 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #define ATKBD_SCR_1 254 1591da177e4SLinus Torvalds #define ATKBD_SCR_2 253 1601da177e4SLinus Torvalds #define ATKBD_SCR_4 252 1611da177e4SLinus Torvalds #define ATKBD_SCR_8 251 1621da177e4SLinus Torvalds #define ATKBD_SCR_CLICK 250 1631da177e4SLinus Torvalds #define ATKBD_SCR_LEFT 249 1641da177e4SLinus Torvalds #define ATKBD_SCR_RIGHT 248 1651da177e4SLinus Torvalds 166f6d65610SDmitry Torokhov #define ATKBD_SPECIAL ATKBD_SCR_RIGHT 1671da177e4SLinus Torvalds 1680d4c8597SDmitry Torokhov #define ATKBD_LED_EVENT_BIT 0 1690d4c8597SDmitry Torokhov #define ATKBD_REP_EVENT_BIT 1 1700d4c8597SDmitry Torokhov 1710ae051a1SDmitry Torokhov #define ATKBD_XL_ERR 0x01 1720ae051a1SDmitry Torokhov #define ATKBD_XL_BAT 0x02 1730ae051a1SDmitry Torokhov #define ATKBD_XL_ACK 0x04 1740ae051a1SDmitry Torokhov #define ATKBD_XL_NAK 0x08 1750ae051a1SDmitry Torokhov #define ATKBD_XL_HANGEUL 0x10 1760ae051a1SDmitry Torokhov #define ATKBD_XL_HANJA 0x20 1770ae051a1SDmitry Torokhov 178f6d65610SDmitry Torokhov static const struct { 1791da177e4SLinus Torvalds unsigned char keycode; 1801da177e4SLinus Torvalds unsigned char set2; 1811da177e4SLinus Torvalds } atkbd_scroll_keys[] = { 1821da177e4SLinus Torvalds { ATKBD_SCR_1, 0xc5 }, 1835212dd58SVojtech Pavlik { ATKBD_SCR_2, 0x9d }, 1845212dd58SVojtech Pavlik { ATKBD_SCR_4, 0xa4 }, 1855212dd58SVojtech Pavlik { ATKBD_SCR_8, 0x9b }, 1861da177e4SLinus Torvalds { ATKBD_SCR_CLICK, 0xe0 }, 1871da177e4SLinus Torvalds { ATKBD_SCR_LEFT, 0xcb }, 1881da177e4SLinus Torvalds { ATKBD_SCR_RIGHT, 0xd2 }, 1891da177e4SLinus Torvalds }; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* 1921da177e4SLinus Torvalds * The atkbd control structure 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds struct atkbd { 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds struct ps2dev ps2dev; 1983c42f0c3SDmitry Torokhov struct input_dev *dev; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* Written only during init */ 2011da177e4SLinus Torvalds char name[64]; 2021da177e4SLinus Torvalds char phys[32]; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds unsigned short id; 2051ba36e11SDmitry Torokhov unsigned short keycode[ATKBD_KEYMAP_SIZE]; 2061ba36e11SDmitry Torokhov DECLARE_BITMAP(force_release_mask, ATKBD_KEYMAP_SIZE); 2071da177e4SLinus Torvalds unsigned char set; 2081da177e4SLinus Torvalds unsigned char translated; 2091da177e4SLinus Torvalds unsigned char extra; 2101da177e4SLinus Torvalds unsigned char write; 2111da177e4SLinus Torvalds unsigned char softrepeat; 2121da177e4SLinus Torvalds unsigned char softraw; 2131da177e4SLinus Torvalds unsigned char scroll; 2141da177e4SLinus Torvalds unsigned char enabled; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* Accessed only from interrupt */ 2171da177e4SLinus Torvalds unsigned char emul; 2181da177e4SLinus Torvalds unsigned char resend; 2191da177e4SLinus Torvalds unsigned char release; 2200ae051a1SDmitry Torokhov unsigned long xl_bit; 2211da177e4SLinus Torvalds unsigned int last; 2221da177e4SLinus Torvalds unsigned long time; 22386255d9dSDmitry Torokhov unsigned long err_count; 2240d4c8597SDmitry Torokhov 225da4249c9SDmitry Torokhov struct delayed_work event_work; 226da4249c9SDmitry Torokhov unsigned long event_jiffies; 22733d3f07aSIngo Molnar struct mutex event_mutex; 2280d4c8597SDmitry Torokhov unsigned long event_mask; 2291da177e4SLinus Torvalds }; 2301da177e4SLinus Torvalds 231554101e3SGiel de Nijs /* 232554101e3SGiel de Nijs * System-specific ketymap fixup routine 233554101e3SGiel de Nijs */ 23439191698SDaniel Mierswa static void (*atkbd_platform_fixup)(struct atkbd *, const void *data); 23539191698SDaniel Mierswa static void *atkbd_platform_fixup_data; 236554101e3SGiel de Nijs 2371da177e4SLinus Torvalds static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf, 2381da177e4SLinus Torvalds ssize_t (*handler)(struct atkbd *, char *)); 2391da177e4SLinus Torvalds static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count, 2401da177e4SLinus Torvalds ssize_t (*handler)(struct atkbd *, const char *, size_t)); 2411da177e4SLinus Torvalds #define ATKBD_DEFINE_ATTR(_name) \ 2421da177e4SLinus Torvalds static ssize_t atkbd_show_##_name(struct atkbd *, char *); \ 2431da177e4SLinus Torvalds static ssize_t atkbd_set_##_name(struct atkbd *, const char *, size_t); \ 24486255d9dSDmitry Torokhov static ssize_t atkbd_do_show_##_name(struct device *d, \ 24586255d9dSDmitry Torokhov struct device_attribute *attr, char *b) \ 2461da177e4SLinus Torvalds { \ 2471da177e4SLinus Torvalds return atkbd_attr_show_helper(d, b, atkbd_show_##_name); \ 2481da177e4SLinus Torvalds } \ 24986255d9dSDmitry Torokhov static ssize_t atkbd_do_set_##_name(struct device *d, \ 25086255d9dSDmitry Torokhov struct device_attribute *attr, const char *b, size_t s) \ 2511da177e4SLinus Torvalds { \ 2521da177e4SLinus Torvalds return atkbd_attr_set_helper(d, b, s, atkbd_set_##_name); \ 2531da177e4SLinus Torvalds } \ 2541da177e4SLinus Torvalds static struct device_attribute atkbd_attr_##_name = \ 2551da177e4SLinus Torvalds __ATTR(_name, S_IWUSR | S_IRUGO, atkbd_do_show_##_name, atkbd_do_set_##_name); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(extra); 2581ba36e11SDmitry Torokhov ATKBD_DEFINE_ATTR(force_release); 2591da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(scroll); 2601da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(set); 2611da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(softrepeat); 2621da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(softraw); 2631da177e4SLinus Torvalds 26486255d9dSDmitry Torokhov #define ATKBD_DEFINE_RO_ATTR(_name) \ 26586255d9dSDmitry Torokhov static ssize_t atkbd_show_##_name(struct atkbd *, char *); \ 26686255d9dSDmitry Torokhov static ssize_t atkbd_do_show_##_name(struct device *d, \ 26786255d9dSDmitry Torokhov struct device_attribute *attr, char *b) \ 26886255d9dSDmitry Torokhov { \ 26986255d9dSDmitry Torokhov return atkbd_attr_show_helper(d, b, atkbd_show_##_name); \ 27086255d9dSDmitry Torokhov } \ 27186255d9dSDmitry Torokhov static struct device_attribute atkbd_attr_##_name = \ 27286255d9dSDmitry Torokhov __ATTR(_name, S_IRUGO, atkbd_do_show_##_name, NULL); 27386255d9dSDmitry Torokhov 27486255d9dSDmitry Torokhov ATKBD_DEFINE_RO_ATTR(err_count); 27586255d9dSDmitry Torokhov 27686255d9dSDmitry Torokhov static struct attribute *atkbd_attributes[] = { 27786255d9dSDmitry Torokhov &atkbd_attr_extra.attr, 2781ba36e11SDmitry Torokhov &atkbd_attr_force_release.attr, 27986255d9dSDmitry Torokhov &atkbd_attr_scroll.attr, 28086255d9dSDmitry Torokhov &atkbd_attr_set.attr, 28186255d9dSDmitry Torokhov &atkbd_attr_softrepeat.attr, 28286255d9dSDmitry Torokhov &atkbd_attr_softraw.attr, 28386255d9dSDmitry Torokhov &atkbd_attr_err_count.attr, 28486255d9dSDmitry Torokhov NULL 28586255d9dSDmitry Torokhov }; 28686255d9dSDmitry Torokhov 28786255d9dSDmitry Torokhov static struct attribute_group atkbd_attribute_group = { 28886255d9dSDmitry Torokhov .attrs = atkbd_attributes, 28986255d9dSDmitry Torokhov }; 29086255d9dSDmitry Torokhov 2910ae051a1SDmitry Torokhov static const unsigned int xl_table[] = { 2920ae051a1SDmitry Torokhov ATKBD_RET_BAT, ATKBD_RET_ERR, ATKBD_RET_ACK, 2930ae051a1SDmitry Torokhov ATKBD_RET_NAK, ATKBD_RET_HANJA, ATKBD_RET_HANGEUL, 2940ae051a1SDmitry Torokhov }; 2951da177e4SLinus Torvalds 2960ae051a1SDmitry Torokhov /* 2970ae051a1SDmitry Torokhov * Checks if we should mangle the scancode to extract 'release' bit 2980ae051a1SDmitry Torokhov * in translated mode. 2990ae051a1SDmitry Torokhov */ 3000ae051a1SDmitry Torokhov static int atkbd_need_xlate(unsigned long xl_bit, unsigned char code) 3011da177e4SLinus Torvalds { 3020ae051a1SDmitry Torokhov int i; 3030ae051a1SDmitry Torokhov 3040ae051a1SDmitry Torokhov if (code == ATKBD_RET_EMUL0 || code == ATKBD_RET_EMUL1) 3050ae051a1SDmitry Torokhov return 0; 3060ae051a1SDmitry Torokhov 3070ae051a1SDmitry Torokhov for (i = 0; i < ARRAY_SIZE(xl_table); i++) 3080ae051a1SDmitry Torokhov if (code == xl_table[i]) 3090ae051a1SDmitry Torokhov return test_bit(i, &xl_bit); 3100ae051a1SDmitry Torokhov 3110ae051a1SDmitry Torokhov return 1; 3120ae051a1SDmitry Torokhov } 3130ae051a1SDmitry Torokhov 3140ae051a1SDmitry Torokhov /* 3150ae051a1SDmitry Torokhov * Calculates new value of xl_bit so the driver can distinguish 3160ae051a1SDmitry Torokhov * between make/break pair of scancodes for select keys and PS/2 3170ae051a1SDmitry Torokhov * protocol responses. 3180ae051a1SDmitry Torokhov */ 3190ae051a1SDmitry Torokhov static void atkbd_calculate_xl_bit(struct atkbd *atkbd, unsigned char code) 3200ae051a1SDmitry Torokhov { 3210ae051a1SDmitry Torokhov int i; 3220ae051a1SDmitry Torokhov 3230ae051a1SDmitry Torokhov for (i = 0; i < ARRAY_SIZE(xl_table); i++) { 3240ae051a1SDmitry Torokhov if (!((code ^ xl_table[i]) & 0x7f)) { 3250ae051a1SDmitry Torokhov if (code & 0x80) 3260ae051a1SDmitry Torokhov __clear_bit(i, &atkbd->xl_bit); 3270ae051a1SDmitry Torokhov else 3280ae051a1SDmitry Torokhov __set_bit(i, &atkbd->xl_bit); 3290ae051a1SDmitry Torokhov break; 3300ae051a1SDmitry Torokhov } 3310ae051a1SDmitry Torokhov } 3320ae051a1SDmitry Torokhov } 3330ae051a1SDmitry Torokhov 3340ae051a1SDmitry Torokhov /* 3350ae051a1SDmitry Torokhov * Encode the scancode, 0xe0 prefix, and high bit into a single integer, 3360ae051a1SDmitry Torokhov * keeping kernel 2.4 compatibility for set 2 3370ae051a1SDmitry Torokhov */ 3380ae051a1SDmitry Torokhov static unsigned int atkbd_compat_scancode(struct atkbd *atkbd, unsigned int code) 3390ae051a1SDmitry Torokhov { 3400ae051a1SDmitry Torokhov if (atkbd->set == 3) { 3410ae051a1SDmitry Torokhov if (atkbd->emul == 1) 3420ae051a1SDmitry Torokhov code |= 0x100; 3430ae051a1SDmitry Torokhov } else { 3440ae051a1SDmitry Torokhov code = (code & 0x7f) | ((code & 0x80) << 1); 3450ae051a1SDmitry Torokhov if (atkbd->emul == 1) 3460ae051a1SDmitry Torokhov code |= 0x80; 3470ae051a1SDmitry Torokhov } 3480ae051a1SDmitry Torokhov 3490ae051a1SDmitry Torokhov return code; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* 3531da177e4SLinus Torvalds * atkbd_interrupt(). Here takes place processing of data received from 3541da177e4SLinus Torvalds * the keyboard into events. 3551da177e4SLinus Torvalds */ 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data, 3587d12e780SDavid Howells unsigned int flags) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds struct atkbd *atkbd = serio_get_drvdata(serio); 3610ae051a1SDmitry Torokhov struct input_dev *dev = atkbd->dev; 3621da177e4SLinus Torvalds unsigned int code = data; 363554101e3SGiel de Nijs int scroll = 0, hscroll = 0, click = -1; 3641da177e4SLinus Torvalds int value; 365f6d65610SDmitry Torokhov unsigned short keycode; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds #ifdef ATKBD_DEBUG 3681da177e4SLinus Torvalds printk(KERN_DEBUG "atkbd.c: Received %02x flags %02x\n", data, flags); 3691da177e4SLinus Torvalds #endif 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds #if !defined(__i386__) && !defined (__x86_64__) 3721da177e4SLinus Torvalds if ((flags & (SERIO_FRAME | SERIO_PARITY)) && (~flags & SERIO_TIMEOUT) && !atkbd->resend && atkbd->write) { 3731da177e4SLinus Torvalds printk(KERN_WARNING "atkbd.c: frame/parity error: %02x\n", flags); 3741da177e4SLinus Torvalds serio_write(serio, ATKBD_CMD_RESEND); 3751da177e4SLinus Torvalds atkbd->resend = 1; 3761da177e4SLinus Torvalds goto out; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds if (!flags && data == ATKBD_RET_ACK) 3801da177e4SLinus Torvalds atkbd->resend = 0; 3811da177e4SLinus Torvalds #endif 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_ACK)) 3841da177e4SLinus Torvalds if (ps2_handle_ack(&atkbd->ps2dev, data)) 3851da177e4SLinus Torvalds goto out; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_CMD)) 3881da177e4SLinus Torvalds if (ps2_handle_response(&atkbd->ps2dev, data)) 3891da177e4SLinus Torvalds goto out; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if (!atkbd->enabled) 3921da177e4SLinus Torvalds goto out; 3931da177e4SLinus Torvalds 3940ae051a1SDmitry Torokhov input_event(dev, EV_MSC, MSC_RAW, code); 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds if (atkbd->translated) { 3971da177e4SLinus Torvalds 3980ae051a1SDmitry Torokhov if (atkbd->emul || atkbd_need_xlate(atkbd->xl_bit, code)) { 3991da177e4SLinus Torvalds atkbd->release = code >> 7; 4001da177e4SLinus Torvalds code &= 0x7f; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4030ae051a1SDmitry Torokhov if (!atkbd->emul) 4040ae051a1SDmitry Torokhov atkbd_calculate_xl_bit(atkbd, data); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds switch (code) { 4081da177e4SLinus Torvalds case ATKBD_RET_BAT: 4091da177e4SLinus Torvalds atkbd->enabled = 0; 410dbc26344SDmitry Torokhov serio_reconnect(atkbd->ps2dev.serio); 4111da177e4SLinus Torvalds goto out; 4121da177e4SLinus Torvalds case ATKBD_RET_EMUL0: 4131da177e4SLinus Torvalds atkbd->emul = 1; 4141da177e4SLinus Torvalds goto out; 4151da177e4SLinus Torvalds case ATKBD_RET_EMUL1: 4161da177e4SLinus Torvalds atkbd->emul = 2; 4171da177e4SLinus Torvalds goto out; 4181da177e4SLinus Torvalds case ATKBD_RET_RELEASE: 4191da177e4SLinus Torvalds atkbd->release = 1; 4201da177e4SLinus Torvalds goto out; 4210ae051a1SDmitry Torokhov case ATKBD_RET_ACK: 4220ae051a1SDmitry Torokhov case ATKBD_RET_NAK: 4239f7a60d6SQi Yong if (printk_ratelimit()) 4240ae051a1SDmitry Torokhov printk(KERN_WARNING "atkbd.c: Spurious %s on %s. " 4250ae051a1SDmitry Torokhov "Some program might be trying access hardware directly.\n", 4260ae051a1SDmitry Torokhov data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys); 4270ae051a1SDmitry Torokhov goto out; 4281da177e4SLinus Torvalds case ATKBD_RET_ERR: 42986255d9dSDmitry Torokhov atkbd->err_count++; 43086255d9dSDmitry Torokhov #ifdef ATKBD_DEBUG 4311da177e4SLinus Torvalds printk(KERN_DEBUG "atkbd.c: Keyboard on %s reports too many keys pressed.\n", serio->phys); 43286255d9dSDmitry Torokhov #endif 4331da177e4SLinus Torvalds goto out; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4360ae051a1SDmitry Torokhov code = atkbd_compat_scancode(atkbd, code); 4370ae051a1SDmitry Torokhov 4380ae051a1SDmitry Torokhov if (atkbd->emul && --atkbd->emul) 4391da177e4SLinus Torvalds goto out; 4401da177e4SLinus Torvalds 4410ae051a1SDmitry Torokhov keycode = atkbd->keycode[code]; 4421da177e4SLinus Torvalds 4430ae051a1SDmitry Torokhov if (keycode != ATKBD_KEY_NULL) 4440ae051a1SDmitry Torokhov input_event(dev, EV_MSC, MSC_SCAN, code); 4450ae051a1SDmitry Torokhov 4460ae051a1SDmitry Torokhov switch (keycode) { 4471da177e4SLinus Torvalds case ATKBD_KEY_NULL: 4481da177e4SLinus Torvalds break; 4491da177e4SLinus Torvalds case ATKBD_KEY_UNKNOWN: 4500ae051a1SDmitry Torokhov printk(KERN_WARNING 4510ae051a1SDmitry Torokhov "atkbd.c: Unknown key %s (%s set %d, code %#x on %s).\n", 4521da177e4SLinus Torvalds atkbd->release ? "released" : "pressed", 4531da177e4SLinus Torvalds atkbd->translated ? "translated" : "raw", 4541da177e4SLinus Torvalds atkbd->set, code, serio->phys); 4550ae051a1SDmitry Torokhov printk(KERN_WARNING 4560ae051a1SDmitry Torokhov "atkbd.c: Use 'setkeycodes %s%02x <keycode>' to make it known.\n", 4571da177e4SLinus Torvalds code & 0x80 ? "e0" : "", code & 0x7f); 4580ae051a1SDmitry Torokhov input_sync(dev); 4591da177e4SLinus Torvalds break; 4601da177e4SLinus Torvalds case ATKBD_SCR_1: 4611da177e4SLinus Torvalds scroll = 1 - atkbd->release * 2; 4621da177e4SLinus Torvalds break; 4631da177e4SLinus Torvalds case ATKBD_SCR_2: 4641da177e4SLinus Torvalds scroll = 2 - atkbd->release * 4; 4651da177e4SLinus Torvalds break; 4661da177e4SLinus Torvalds case ATKBD_SCR_4: 4671da177e4SLinus Torvalds scroll = 4 - atkbd->release * 8; 4681da177e4SLinus Torvalds break; 4691da177e4SLinus Torvalds case ATKBD_SCR_8: 4701da177e4SLinus Torvalds scroll = 8 - atkbd->release * 16; 4711da177e4SLinus Torvalds break; 4721da177e4SLinus Torvalds case ATKBD_SCR_CLICK: 4731da177e4SLinus Torvalds click = !atkbd->release; 4741da177e4SLinus Torvalds break; 4751da177e4SLinus Torvalds case ATKBD_SCR_LEFT: 4761da177e4SLinus Torvalds hscroll = -1; 4771da177e4SLinus Torvalds break; 4781da177e4SLinus Torvalds case ATKBD_SCR_RIGHT: 4791da177e4SLinus Torvalds hscroll = 1; 4801da177e4SLinus Torvalds break; 4811da177e4SLinus Torvalds default: 4820ae051a1SDmitry Torokhov if (atkbd->release) { 4830ae051a1SDmitry Torokhov value = 0; 4841da177e4SLinus Torvalds atkbd->last = 0; 4850ae051a1SDmitry Torokhov } else if (!atkbd->softrepeat && test_bit(keycode, dev->key)) { 4860ae051a1SDmitry Torokhov /* Workaround Toshiba laptop multiple keypress */ 4870ae051a1SDmitry Torokhov value = time_before(jiffies, atkbd->time) && atkbd->last == code ? 1 : 2; 4880ae051a1SDmitry Torokhov } else { 4891da177e4SLinus Torvalds value = 1; 4900ae051a1SDmitry Torokhov atkbd->last = code; 4910ae051a1SDmitry Torokhov atkbd->time = jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]) / 2; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494f8b4c46cSDmitry Torokhov input_event(dev, EV_KEY, keycode, value); 4950ae051a1SDmitry Torokhov input_sync(dev); 4960ae051a1SDmitry Torokhov 497554101e3SGiel de Nijs if (value && test_bit(code, atkbd->force_release_mask)) { 4980ae051a1SDmitry Torokhov input_report_key(dev, keycode, 0); 4990ae051a1SDmitry Torokhov input_sync(dev); 5000ae051a1SDmitry Torokhov } 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (atkbd->scroll) { 5041da177e4SLinus Torvalds if (click != -1) 5050ae051a1SDmitry Torokhov input_report_key(dev, BTN_MIDDLE, click); 5060ae051a1SDmitry Torokhov input_report_rel(dev, REL_WHEEL, scroll); 5070ae051a1SDmitry Torokhov input_report_rel(dev, REL_HWHEEL, hscroll); 5080ae051a1SDmitry Torokhov input_sync(dev); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds atkbd->release = 0; 5121da177e4SLinus Torvalds out: 5131da177e4SLinus Torvalds return IRQ_HANDLED; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5163d0f0fa0SDmitry Torokhov static int atkbd_set_repeat_rate(struct atkbd *atkbd) 5171da177e4SLinus Torvalds { 5181da177e4SLinus Torvalds const short period[32] = 5191da177e4SLinus Torvalds { 33, 37, 42, 46, 50, 54, 58, 63, 67, 75, 83, 92, 100, 109, 116, 125, 5201da177e4SLinus Torvalds 133, 149, 167, 182, 200, 217, 232, 250, 270, 303, 333, 370, 400, 435, 470, 500 }; 5211da177e4SLinus Torvalds const short delay[4] = 5221da177e4SLinus Torvalds { 250, 500, 750, 1000 }; 5230d4c8597SDmitry Torokhov 5243d0f0fa0SDmitry Torokhov struct input_dev *dev = atkbd->dev; 5253d0f0fa0SDmitry Torokhov unsigned char param; 5263d0f0fa0SDmitry Torokhov int i = 0, j = 0; 5273d0f0fa0SDmitry Torokhov 5283d0f0fa0SDmitry Torokhov while (i < ARRAY_SIZE(period) - 1 && period[i] < dev->rep[REP_PERIOD]) 5293d0f0fa0SDmitry Torokhov i++; 5303d0f0fa0SDmitry Torokhov dev->rep[REP_PERIOD] = period[i]; 5313d0f0fa0SDmitry Torokhov 5328ea371fbSFlorin Malita while (j < ARRAY_SIZE(delay) - 1 && delay[j] < dev->rep[REP_DELAY]) 5333d0f0fa0SDmitry Torokhov j++; 5343d0f0fa0SDmitry Torokhov dev->rep[REP_DELAY] = delay[j]; 5353d0f0fa0SDmitry Torokhov 5363d0f0fa0SDmitry Torokhov param = i | (j << 5); 5373d0f0fa0SDmitry Torokhov return ps2_command(&atkbd->ps2dev, ¶m, ATKBD_CMD_SETREP); 5383d0f0fa0SDmitry Torokhov } 5393d0f0fa0SDmitry Torokhov 5403d0f0fa0SDmitry Torokhov static int atkbd_set_leds(struct atkbd *atkbd) 5413d0f0fa0SDmitry Torokhov { 5420d4c8597SDmitry Torokhov struct input_dev *dev = atkbd->dev; 5431da177e4SLinus Torvalds unsigned char param[2]; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0) 5461da177e4SLinus Torvalds | (test_bit(LED_NUML, dev->led) ? 2 : 0) 5471da177e4SLinus Torvalds | (test_bit(LED_CAPSL, dev->led) ? 4 : 0); 5483d0f0fa0SDmitry Torokhov if (ps2_command(&atkbd->ps2dev, param, ATKBD_CMD_SETLEDS)) 5493d0f0fa0SDmitry Torokhov return -1; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds if (atkbd->extra) { 5521da177e4SLinus Torvalds param[0] = 0; 5531da177e4SLinus Torvalds param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0) 5541da177e4SLinus Torvalds | (test_bit(LED_SLEEP, dev->led) ? 0x02 : 0) 5551da177e4SLinus Torvalds | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0) 5561da177e4SLinus Torvalds | (test_bit(LED_MISC, dev->led) ? 0x10 : 0) 5571da177e4SLinus Torvalds | (test_bit(LED_MUTE, dev->led) ? 0x20 : 0); 5583d0f0fa0SDmitry Torokhov if (ps2_command(&atkbd->ps2dev, param, ATKBD_CMD_EX_SETLEDS)) 5593d0f0fa0SDmitry Torokhov return -1; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5623d0f0fa0SDmitry Torokhov return 0; 5630d4c8597SDmitry Torokhov } 5640d4c8597SDmitry Torokhov 5653d0f0fa0SDmitry Torokhov /* 5663d0f0fa0SDmitry Torokhov * atkbd_event_work() is used to complete processing of events that 5673d0f0fa0SDmitry Torokhov * can not be processed by input_event() which is often called from 5683d0f0fa0SDmitry Torokhov * interrupt context. 5693d0f0fa0SDmitry Torokhov */ 5703d0f0fa0SDmitry Torokhov 57165f27f38SDavid Howells static void atkbd_event_work(struct work_struct *work) 5723d0f0fa0SDmitry Torokhov { 573da4249c9SDmitry Torokhov struct atkbd *atkbd = container_of(work, struct atkbd, event_work.work); 5743d0f0fa0SDmitry Torokhov 5753d0f0fa0SDmitry Torokhov mutex_lock(&atkbd->event_mutex); 5763d0f0fa0SDmitry Torokhov 577*94dfb0d6SDmitry Torokhov if (!atkbd->enabled) { 578*94dfb0d6SDmitry Torokhov /* 579*94dfb0d6SDmitry Torokhov * Serio ports are resumed asynchronously so while driver core 580*94dfb0d6SDmitry Torokhov * thinks that device is already fully operational in reality 581*94dfb0d6SDmitry Torokhov * it may not be ready yet. In this case we need to keep 582*94dfb0d6SDmitry Torokhov * rescheduling till reconnect completes. 583*94dfb0d6SDmitry Torokhov */ 584*94dfb0d6SDmitry Torokhov schedule_delayed_work(&atkbd->event_work, 585*94dfb0d6SDmitry Torokhov msecs_to_jiffies(100)); 586*94dfb0d6SDmitry Torokhov } else { 5873d0f0fa0SDmitry Torokhov if (test_and_clear_bit(ATKBD_LED_EVENT_BIT, &atkbd->event_mask)) 5883d0f0fa0SDmitry Torokhov atkbd_set_leds(atkbd); 5893d0f0fa0SDmitry Torokhov 5903d0f0fa0SDmitry Torokhov if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask)) 5913d0f0fa0SDmitry Torokhov atkbd_set_repeat_rate(atkbd); 592*94dfb0d6SDmitry Torokhov } 5933d0f0fa0SDmitry Torokhov 59433d3f07aSIngo Molnar mutex_unlock(&atkbd->event_mutex); 5950d4c8597SDmitry Torokhov } 5960d4c8597SDmitry Torokhov 5970d4c8597SDmitry Torokhov /* 598da4249c9SDmitry Torokhov * Schedule switch for execution. We need to throttle requests, 599da4249c9SDmitry Torokhov * otherwise keyboard may become unresponsive. 600da4249c9SDmitry Torokhov */ 601da4249c9SDmitry Torokhov static void atkbd_schedule_event_work(struct atkbd *atkbd, int event_bit) 602da4249c9SDmitry Torokhov { 603da4249c9SDmitry Torokhov unsigned long delay = msecs_to_jiffies(50); 604da4249c9SDmitry Torokhov 605da4249c9SDmitry Torokhov if (time_after(jiffies, atkbd->event_jiffies + delay)) 606da4249c9SDmitry Torokhov delay = 0; 607da4249c9SDmitry Torokhov 608da4249c9SDmitry Torokhov atkbd->event_jiffies = jiffies; 609da4249c9SDmitry Torokhov set_bit(event_bit, &atkbd->event_mask); 610da4249c9SDmitry Torokhov wmb(); 611da4249c9SDmitry Torokhov schedule_delayed_work(&atkbd->event_work, delay); 612da4249c9SDmitry Torokhov } 613da4249c9SDmitry Torokhov 614da4249c9SDmitry Torokhov /* 6150d4c8597SDmitry Torokhov * Event callback from the input module. Events that change the state of 6160d4c8597SDmitry Torokhov * the hardware are processed here. If action can not be performed in 6170d4c8597SDmitry Torokhov * interrupt context it is offloaded to atkbd_event_work. 6180d4c8597SDmitry Torokhov */ 6190d4c8597SDmitry Torokhov 620da4249c9SDmitry Torokhov static int atkbd_event(struct input_dev *dev, 621da4249c9SDmitry Torokhov unsigned int type, unsigned int code, int value) 6220d4c8597SDmitry Torokhov { 623b356872fSDmitry Torokhov struct atkbd *atkbd = input_get_drvdata(dev); 6240d4c8597SDmitry Torokhov 6250d4c8597SDmitry Torokhov if (!atkbd->write) 6260d4c8597SDmitry Torokhov return -1; 6270d4c8597SDmitry Torokhov 6280d4c8597SDmitry Torokhov switch (type) { 6290d4c8597SDmitry Torokhov 6300d4c8597SDmitry Torokhov case EV_LED: 631da4249c9SDmitry Torokhov atkbd_schedule_event_work(atkbd, ATKBD_LED_EVENT_BIT); 6320d4c8597SDmitry Torokhov return 0; 6330d4c8597SDmitry Torokhov 6340d4c8597SDmitry Torokhov case EV_REP: 635da4249c9SDmitry Torokhov if (!atkbd->softrepeat) 636da4249c9SDmitry Torokhov atkbd_schedule_event_work(atkbd, ATKBD_REP_EVENT_BIT); 6371da177e4SLinus Torvalds return 0; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds return -1; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds /* 6441da177e4SLinus Torvalds * atkbd_enable() signals that interrupt handler is allowed to 6451da177e4SLinus Torvalds * generate input events. 6461da177e4SLinus Torvalds */ 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds static inline void atkbd_enable(struct atkbd *atkbd) 6491da177e4SLinus Torvalds { 6501da177e4SLinus Torvalds serio_pause_rx(atkbd->ps2dev.serio); 6511da177e4SLinus Torvalds atkbd->enabled = 1; 6521da177e4SLinus Torvalds serio_continue_rx(atkbd->ps2dev.serio); 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds /* 6561da177e4SLinus Torvalds * atkbd_disable() tells input handler that all incoming data except 6571da177e4SLinus Torvalds * for ACKs and command response should be dropped. 6581da177e4SLinus Torvalds */ 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds static inline void atkbd_disable(struct atkbd *atkbd) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds serio_pause_rx(atkbd->ps2dev.serio); 6631da177e4SLinus Torvalds atkbd->enabled = 0; 6641da177e4SLinus Torvalds serio_continue_rx(atkbd->ps2dev.serio); 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* 6681da177e4SLinus Torvalds * atkbd_probe() probes for an AT keyboard on a serio port. 6691da177e4SLinus Torvalds */ 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds static int atkbd_probe(struct atkbd *atkbd) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds struct ps2dev *ps2dev = &atkbd->ps2dev; 6741da177e4SLinus Torvalds unsigned char param[2]; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * Some systems, where the bit-twiddling when testing the io-lines of the 6781da177e4SLinus Torvalds * controller may confuse the keyboard need a full reset of the keyboard. On 6791da177e4SLinus Torvalds * these systems the BIOS also usually doesn't do it for us. 6801da177e4SLinus Torvalds */ 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds if (atkbd_reset) 6831da177e4SLinus Torvalds if (ps2_command(ps2dev, NULL, ATKBD_CMD_RESET_BAT)) 6841da177e4SLinus Torvalds printk(KERN_WARNING "atkbd.c: keyboard reset failed on %s\n", ps2dev->serio->phys); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * Then we check the keyboard ID. We should get 0xab83 under normal conditions. 6881da177e4SLinus Torvalds * Some keyboards report different values, but the first byte is always 0xab or 6891da177e4SLinus Torvalds * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this 6901da177e4SLinus Torvalds * should make sure we don't try to set the LEDs on it. 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds param[0] = param[1] = 0xa5; /* initialize with invalid values */ 6941da177e4SLinus Torvalds if (ps2_command(ps2dev, param, ATKBD_CMD_GETID)) { 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* 6971da177e4SLinus Torvalds * If the get ID command failed, we check if we can at least set the LEDs on 6981da177e4SLinus Torvalds * the keyboard. This should work on every keyboard out there. It also turns 6991da177e4SLinus Torvalds * the LEDs off, which we want anyway. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds param[0] = 0; 7021da177e4SLinus Torvalds if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS)) 7031da177e4SLinus Torvalds return -1; 7041da177e4SLinus Torvalds atkbd->id = 0xabba; 7051da177e4SLinus Torvalds return 0; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7089807879bSDmitry Torokhov if (!ps2_is_keyboard_id(param[0])) 7091da177e4SLinus Torvalds return -1; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds atkbd->id = (param[0] << 8) | param[1]; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds if (atkbd->id == 0xaca1 && atkbd->translated) { 7141da177e4SLinus Torvalds printk(KERN_ERR "atkbd.c: NCD terminal keyboards are only supported on non-translating\n"); 7151da177e4SLinus Torvalds printk(KERN_ERR "atkbd.c: controllers. Use i8042.direct=1 to disable translation.\n"); 7161da177e4SLinus Torvalds return -1; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds return 0; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* 7231da177e4SLinus Torvalds * atkbd_select_set checks if a keyboard has a working Set 3 support, and 7241da177e4SLinus Torvalds * sets it into that. Unfortunately there are keyboards that can be switched 7251da177e4SLinus Torvalds * to Set 3, but don't work well in that (BTC Multimedia ...) 7261da177e4SLinus Torvalds */ 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds static int atkbd_select_set(struct atkbd *atkbd, int target_set, int allow_extra) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds struct ps2dev *ps2dev = &atkbd->ps2dev; 7311da177e4SLinus Torvalds unsigned char param[2]; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds atkbd->extra = 0; 7341da177e4SLinus Torvalds /* 7351da177e4SLinus Torvalds * For known special keyboards we can go ahead and set the correct set. 7361da177e4SLinus Torvalds * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and 7371da177e4SLinus Torvalds * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards. 7381da177e4SLinus Torvalds */ 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds if (atkbd->translated) 7411da177e4SLinus Torvalds return 2; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds if (atkbd->id == 0xaca1) { 7441da177e4SLinus Torvalds param[0] = 3; 7451da177e4SLinus Torvalds ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET); 7461da177e4SLinus Torvalds return 3; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds if (allow_extra) { 7501da177e4SLinus Torvalds param[0] = 0x71; 7511da177e4SLinus Torvalds if (!ps2_command(ps2dev, param, ATKBD_CMD_EX_ENABLE)) { 7521da177e4SLinus Torvalds atkbd->extra = 1; 7531da177e4SLinus Torvalds return 2; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (target_set != 3) 7581da177e4SLinus Torvalds return 2; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds if (!ps2_command(ps2dev, param, ATKBD_CMD_OK_GETID)) { 7611da177e4SLinus Torvalds atkbd->id = param[0] << 8 | param[1]; 7621da177e4SLinus Torvalds return 2; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds param[0] = 3; 7661da177e4SLinus Torvalds if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET)) 7671da177e4SLinus Torvalds return 2; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds param[0] = 0; 7701da177e4SLinus Torvalds if (ps2_command(ps2dev, param, ATKBD_CMD_GSCANSET)) 7711da177e4SLinus Torvalds return 2; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds if (param[0] != 3) { 7741da177e4SLinus Torvalds param[0] = 2; 7751da177e4SLinus Torvalds if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET)) 7761da177e4SLinus Torvalds return 2; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds ps2_command(ps2dev, param, ATKBD_CMD_SETALL_MBR); 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds return 3; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 784fc0eb28cSDmitry Torokhov static int atkbd_reset_state(struct atkbd *atkbd) 785fc0eb28cSDmitry Torokhov { 786fc0eb28cSDmitry Torokhov struct ps2dev *ps2dev = &atkbd->ps2dev; 787fc0eb28cSDmitry Torokhov unsigned char param[1]; 788fc0eb28cSDmitry Torokhov 789fc0eb28cSDmitry Torokhov /* 790fc0eb28cSDmitry Torokhov * Set the LEDs to a predefined state (all off). 791fc0eb28cSDmitry Torokhov */ 792fc0eb28cSDmitry Torokhov 793fc0eb28cSDmitry Torokhov param[0] = 0; 794fc0eb28cSDmitry Torokhov if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS)) 795fc0eb28cSDmitry Torokhov return -1; 796fc0eb28cSDmitry Torokhov 797fc0eb28cSDmitry Torokhov /* 798fc0eb28cSDmitry Torokhov * Set autorepeat to fastest possible. 799fc0eb28cSDmitry Torokhov */ 800fc0eb28cSDmitry Torokhov 801fc0eb28cSDmitry Torokhov param[0] = 0; 802fc0eb28cSDmitry Torokhov if (ps2_command(ps2dev, param, ATKBD_CMD_SETREP)) 803fc0eb28cSDmitry Torokhov return -1; 804fc0eb28cSDmitry Torokhov 805fc0eb28cSDmitry Torokhov return 0; 806fc0eb28cSDmitry Torokhov } 807fc0eb28cSDmitry Torokhov 8081da177e4SLinus Torvalds static int atkbd_activate(struct atkbd *atkbd) 8091da177e4SLinus Torvalds { 8101da177e4SLinus Torvalds struct ps2dev *ps2dev = &atkbd->ps2dev; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * Enable the keyboard to receive keystrokes. 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds if (ps2_command(ps2dev, NULL, ATKBD_CMD_ENABLE)) { 8171da177e4SLinus Torvalds printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n", 8181da177e4SLinus Torvalds ps2dev->serio->phys); 8191da177e4SLinus Torvalds return -1; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds return 0; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 8261da177e4SLinus Torvalds * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a 8271da177e4SLinus Torvalds * reboot. 8281da177e4SLinus Torvalds */ 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds static void atkbd_cleanup(struct serio *serio) 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds struct atkbd *atkbd = serio_get_drvdata(serio); 83357f5b159SDmitry Torokhov 83457f5b159SDmitry Torokhov atkbd_disable(atkbd); 8351da177e4SLinus Torvalds ps2_command(&atkbd->ps2dev, NULL, ATKBD_CMD_RESET_BAT); 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* 8401da177e4SLinus Torvalds * atkbd_disconnect() closes and frees. 8411da177e4SLinus Torvalds */ 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds static void atkbd_disconnect(struct serio *serio) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds struct atkbd *atkbd = serio_get_drvdata(serio); 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds atkbd_disable(atkbd); 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* make sure we don't have a command in flight */ 850d6d79a78SJiri Pirko cancel_delayed_work_sync(&atkbd->event_work); 8511da177e4SLinus Torvalds 85286255d9dSDmitry Torokhov sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group); 8533c42f0c3SDmitry Torokhov input_unregister_device(atkbd->dev); 8541da177e4SLinus Torvalds serio_close(serio); 8551da177e4SLinus Torvalds serio_set_drvdata(serio, NULL); 8561da177e4SLinus Torvalds kfree(atkbd); 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 859554101e3SGiel de Nijs /* 86039191698SDaniel Mierswa * generate release events for the keycodes given in data 86139191698SDaniel Mierswa */ 86239191698SDaniel Mierswa static void atkbd_apply_forced_release_keylist(struct atkbd* atkbd, 86339191698SDaniel Mierswa const void *data) 86439191698SDaniel Mierswa { 86539191698SDaniel Mierswa const unsigned int *keys = data; 86639191698SDaniel Mierswa unsigned int i; 86739191698SDaniel Mierswa 86839191698SDaniel Mierswa if (atkbd->set == 2) 86939191698SDaniel Mierswa for (i = 0; keys[i] != -1U; i++) 87039191698SDaniel Mierswa __set_bit(keys[i], atkbd->force_release_mask); 87139191698SDaniel Mierswa } 87239191698SDaniel Mierswa 87339191698SDaniel Mierswa /* 87461579ba8SMatthew Garrett * Most special keys (Fn+F?) on Dell laptops do not generate release 875554101e3SGiel de Nijs * events so we have to do it ourselves. 876554101e3SGiel de Nijs */ 87739191698SDaniel Mierswa static unsigned int atkbd_dell_laptop_forced_release_keys[] = { 87839191698SDaniel Mierswa 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8f, 0x93, -1U 879554101e3SGiel de Nijs }; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* 8825a54c011SJiri Kosina * Perform fixup for HP system that doesn't generate release 8835a54c011SJiri Kosina * for its video switch 8845a54c011SJiri Kosina */ 88539191698SDaniel Mierswa static unsigned int atkbd_hp_forced_release_keys[] = { 88639191698SDaniel Mierswa 0x94, -1U 8875a54c011SJiri Kosina }; 8885a54c011SJiri Kosina 8895a54c011SJiri Kosina /* 890a8215b81SMatthew Garrett * Inventec system with broken key release on volume keys 891a8215b81SMatthew Garrett */ 89239191698SDaniel Mierswa static unsigned int atkbd_inventec_forced_release_keys[] = { 89339191698SDaniel Mierswa 0xae, 0xb0, -1U 894a8215b81SMatthew Garrett }; 895a8215b81SMatthew Garrett 896a8215b81SMatthew Garrett /* 897181f6382SRikard Ljungstrand * Perform fixup for HP Pavilion ZV6100 laptop that doesn't generate release 898181f6382SRikard Ljungstrand * for its volume buttons 899181f6382SRikard Ljungstrand */ 90039191698SDaniel Mierswa static unsigned int atkbd_hp_zv6100_forced_release_keys[] = { 90139191698SDaniel Mierswa 0xae, 0xb0, -1U 902181f6382SRikard Ljungstrand }; 903181f6382SRikard Ljungstrand 904181f6382SRikard Ljungstrand /* 9052bcaa6a4SDave Andrews * Perform fixup for HP (Compaq) Presario R4000 R4100 R4200 that don't generate 9062bcaa6a4SDave Andrews * release for their volume buttons 9072bcaa6a4SDave Andrews */ 9082bcaa6a4SDave Andrews static unsigned int atkbd_hp_r4000_forced_release_keys[] = { 9092bcaa6a4SDave Andrews 0xae, 0xb0, -1U 9102bcaa6a4SDave Andrews }; 9112bcaa6a4SDave Andrews 9122bcaa6a4SDave Andrews /* 913e04126c7SBarry Carroll * Samsung NC10,NC20 with Fn+F? key release not working 9144200844bSStuart Hopkins */ 91539191698SDaniel Mierswa static unsigned int atkbd_samsung_forced_release_keys[] = { 91639191698SDaniel Mierswa 0x82, 0x83, 0x84, 0x86, 0x88, 0x89, 0xb3, 0xf7, 0xf9, -1U 9174200844bSStuart Hopkins }; 9184200844bSStuart Hopkins 9194200844bSStuart Hopkins /* 920adcb523eSDaniel Mierswa * The volume up and volume down special keys on a Fujitsu Amilo PA 1510 laptop 921adcb523eSDaniel Mierswa * do not generate release events so we have to do it ourselves. 922adcb523eSDaniel Mierswa */ 923adcb523eSDaniel Mierswa static unsigned int atkbd_amilo_pa1510_forced_release_keys[] = { 924adcb523eSDaniel Mierswa 0xb0, 0xae, -1U 925adcb523eSDaniel Mierswa }; 926adcb523eSDaniel Mierswa 927adcb523eSDaniel Mierswa /* 928f0a14de2SSimon Davie * Amilo Pi 3525 key release for Fn+Volume keys not working 929f0a14de2SSimon Davie */ 930f0a14de2SSimon Davie static unsigned int atkbd_amilo_pi3525_forced_release_keys[] = { 931f0a14de2SSimon Davie 0x20, 0xa0, 0x2e, 0xae, 0x30, 0xb0, -1U 932f0a14de2SSimon Davie }; 933f0a14de2SSimon Davie 934f0a14de2SSimon Davie /* 9359166d0f6SAdrian Batzill * Amilo Xi 3650 key release for light touch bar not working 9369166d0f6SAdrian Batzill */ 9379166d0f6SAdrian Batzill static unsigned int atkbd_amilo_xi3650_forced_release_keys[] = { 9389166d0f6SAdrian Batzill 0x67, 0xed, 0x90, 0xa2, 0x99, 0xa4, 0xae, 0xb0, -1U 9399166d0f6SAdrian Batzill }; 9409166d0f6SAdrian Batzill 9419166d0f6SAdrian Batzill /* 942032e46cbSJerone Young * Soltech TA12 system with broken key release on volume keys and mute key 943032e46cbSJerone Young */ 944032e46cbSJerone Young static unsigned int atkdb_soltech_ta12_forced_release_keys[] = { 945032e46cbSJerone Young 0xa0, 0xae, 0xb0, -1U 946032e46cbSJerone Young }; 947032e46cbSJerone Young 948032e46cbSJerone Young /* 9493c42f0c3SDmitry Torokhov * atkbd_set_keycode_table() initializes keyboard's keycode table 9501da177e4SLinus Torvalds * according to the selected scancode set 9511da177e4SLinus Torvalds */ 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds static void atkbd_set_keycode_table(struct atkbd *atkbd) 9541da177e4SLinus Torvalds { 955554101e3SGiel de Nijs unsigned int scancode; 9561da177e4SLinus Torvalds int i, j; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds memset(atkbd->keycode, 0, sizeof(atkbd->keycode)); 9591ba36e11SDmitry Torokhov bitmap_zero(atkbd->force_release_mask, ATKBD_KEYMAP_SIZE); 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds if (atkbd->translated) { 9621da177e4SLinus Torvalds for (i = 0; i < 128; i++) { 963554101e3SGiel de Nijs scancode = atkbd_unxlate_table[i]; 964554101e3SGiel de Nijs atkbd->keycode[i] = atkbd_set2_keycode[scancode]; 965554101e3SGiel de Nijs atkbd->keycode[i | 0x80] = atkbd_set2_keycode[scancode | 0x80]; 9661da177e4SLinus Torvalds if (atkbd->scroll) 9671da177e4SLinus Torvalds for (j = 0; j < ARRAY_SIZE(atkbd_scroll_keys); j++) 968554101e3SGiel de Nijs if ((scancode | 0x80) == atkbd_scroll_keys[j].set2) 9691da177e4SLinus Torvalds atkbd->keycode[i | 0x80] = atkbd_scroll_keys[j].keycode; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds } else if (atkbd->set == 3) { 9721da177e4SLinus Torvalds memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode)); 9731da177e4SLinus Torvalds } else { 9741da177e4SLinus Torvalds memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode)); 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds if (atkbd->scroll) 977554101e3SGiel de Nijs for (i = 0; i < ARRAY_SIZE(atkbd_scroll_keys); i++) { 978554101e3SGiel de Nijs scancode = atkbd_scroll_keys[i].set2; 979554101e3SGiel de Nijs atkbd->keycode[scancode] = atkbd_scroll_keys[i].keycode; 980554101e3SGiel de Nijs } 9811da177e4SLinus Torvalds } 9820ae051a1SDmitry Torokhov 983554101e3SGiel de Nijs /* 984554101e3SGiel de Nijs * HANGEUL and HANJA keys do not send release events so we need to 985554101e3SGiel de Nijs * generate such events ourselves 986554101e3SGiel de Nijs */ 987554101e3SGiel de Nijs scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANGEUL); 988554101e3SGiel de Nijs atkbd->keycode[scancode] = KEY_HANGEUL; 989554101e3SGiel de Nijs __set_bit(scancode, atkbd->force_release_mask); 990554101e3SGiel de Nijs 991554101e3SGiel de Nijs scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANJA); 992554101e3SGiel de Nijs atkbd->keycode[scancode] = KEY_HANJA; 993554101e3SGiel de Nijs __set_bit(scancode, atkbd->force_release_mask); 994554101e3SGiel de Nijs 995554101e3SGiel de Nijs /* 996554101e3SGiel de Nijs * Perform additional fixups 997554101e3SGiel de Nijs */ 998554101e3SGiel de Nijs if (atkbd_platform_fixup) 99939191698SDaniel Mierswa atkbd_platform_fixup(atkbd, atkbd_platform_fixup_data); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /* 10031da177e4SLinus Torvalds * atkbd_set_device_attrs() sets up keyboard's input device structure 10041da177e4SLinus Torvalds */ 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds static void atkbd_set_device_attrs(struct atkbd *atkbd) 10071da177e4SLinus Torvalds { 10083c42f0c3SDmitry Torokhov struct input_dev *input_dev = atkbd->dev; 10091da177e4SLinus Torvalds int i; 10101da177e4SLinus Torvalds 10113c42f0c3SDmitry Torokhov if (atkbd->extra) 1012ea08c6faSDmitry Torokhov snprintf(atkbd->name, sizeof(atkbd->name), 1013ea08c6faSDmitry Torokhov "AT Set 2 Extra keyboard"); 10143c42f0c3SDmitry Torokhov else 1015ea08c6faSDmitry Torokhov snprintf(atkbd->name, sizeof(atkbd->name), 1016ea08c6faSDmitry Torokhov "AT %s Set %d keyboard", 10173c42f0c3SDmitry Torokhov atkbd->translated ? "Translated" : "Raw", atkbd->set); 10181da177e4SLinus Torvalds 1019ea08c6faSDmitry Torokhov snprintf(atkbd->phys, sizeof(atkbd->phys), 1020ea08c6faSDmitry Torokhov "%s/input0", atkbd->ps2dev.serio->phys); 10211da177e4SLinus Torvalds 10223c42f0c3SDmitry Torokhov input_dev->name = atkbd->name; 10233c42f0c3SDmitry Torokhov input_dev->phys = atkbd->phys; 10243c42f0c3SDmitry Torokhov input_dev->id.bustype = BUS_I8042; 10253c42f0c3SDmitry Torokhov input_dev->id.vendor = 0x0001; 10263c42f0c3SDmitry Torokhov input_dev->id.product = atkbd->translated ? 1 : atkbd->set; 10273c42f0c3SDmitry Torokhov input_dev->id.version = atkbd->id; 10283c42f0c3SDmitry Torokhov input_dev->event = atkbd_event; 1029469ba4dfSDmitry Torokhov input_dev->dev.parent = &atkbd->ps2dev.serio->dev; 10301da177e4SLinus Torvalds 1031b356872fSDmitry Torokhov input_set_drvdata(input_dev, atkbd); 1032b356872fSDmitry Torokhov 10337b19ada2SJiri Slaby input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | 10347b19ada2SJiri Slaby BIT_MASK(EV_MSC); 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds if (atkbd->write) { 10377b19ada2SJiri Slaby input_dev->evbit[0] |= BIT_MASK(EV_LED); 10387b19ada2SJiri Slaby input_dev->ledbit[0] = BIT_MASK(LED_NUML) | 10397b19ada2SJiri Slaby BIT_MASK(LED_CAPSL) | BIT_MASK(LED_SCROLLL); 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds if (atkbd->extra) 10437b19ada2SJiri Slaby input_dev->ledbit[0] |= BIT_MASK(LED_COMPOSE) | 10447b19ada2SJiri Slaby BIT_MASK(LED_SUSPEND) | BIT_MASK(LED_SLEEP) | 10457b19ada2SJiri Slaby BIT_MASK(LED_MUTE) | BIT_MASK(LED_MISC); 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds if (!atkbd->softrepeat) { 10483c42f0c3SDmitry Torokhov input_dev->rep[REP_DELAY] = 250; 10493c42f0c3SDmitry Torokhov input_dev->rep[REP_PERIOD] = 33; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10527b19ada2SJiri Slaby input_dev->mscbit[0] = atkbd->softraw ? BIT_MASK(MSC_SCAN) : 10537b19ada2SJiri Slaby BIT_MASK(MSC_RAW) | BIT_MASK(MSC_SCAN); 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (atkbd->scroll) { 10567b19ada2SJiri Slaby input_dev->evbit[0] |= BIT_MASK(EV_REL); 10577b19ada2SJiri Slaby input_dev->relbit[0] = BIT_MASK(REL_WHEEL) | 10587b19ada2SJiri Slaby BIT_MASK(REL_HWHEEL); 1059f6d65610SDmitry Torokhov __set_bit(BTN_MIDDLE, input_dev->keybit); 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10623c42f0c3SDmitry Torokhov input_dev->keycode = atkbd->keycode; 1063f6d65610SDmitry Torokhov input_dev->keycodesize = sizeof(unsigned short); 10643c42f0c3SDmitry Torokhov input_dev->keycodemax = ARRAY_SIZE(atkbd_set2_keycode); 10651da177e4SLinus Torvalds 10661ba36e11SDmitry Torokhov for (i = 0; i < ATKBD_KEYMAP_SIZE; i++) 10671da177e4SLinus Torvalds if (atkbd->keycode[i] && atkbd->keycode[i] < ATKBD_SPECIAL) 1068f6d65610SDmitry Torokhov __set_bit(atkbd->keycode[i], input_dev->keybit); 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * atkbd_connect() is called when the serio module finds an interface 10731da177e4SLinus Torvalds * that isn't handled yet by an appropriate device driver. We check if 10741da177e4SLinus Torvalds * there is an AT keyboard out there and if yes, we register ourselves 10751da177e4SLinus Torvalds * to the input module. 10761da177e4SLinus Torvalds */ 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds static int atkbd_connect(struct serio *serio, struct serio_driver *drv) 10791da177e4SLinus Torvalds { 10801da177e4SLinus Torvalds struct atkbd *atkbd; 10813c42f0c3SDmitry Torokhov struct input_dev *dev; 10823c42f0c3SDmitry Torokhov int err = -ENOMEM; 10831da177e4SLinus Torvalds 10843c42f0c3SDmitry Torokhov atkbd = kzalloc(sizeof(struct atkbd), GFP_KERNEL); 10853c42f0c3SDmitry Torokhov dev = input_allocate_device(); 10863c42f0c3SDmitry Torokhov if (!atkbd || !dev) 10872b03b60eSDmitry Torokhov goto fail1; 10881da177e4SLinus Torvalds 10893c42f0c3SDmitry Torokhov atkbd->dev = dev; 10901da177e4SLinus Torvalds ps2_init(&atkbd->ps2dev, serio); 1091da4249c9SDmitry Torokhov INIT_DELAYED_WORK(&atkbd->event_work, atkbd_event_work); 109233d3f07aSIngo Molnar mutex_init(&atkbd->event_mutex); 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds switch (serio->id.type) { 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds case SERIO_8042_XL: 10971da177e4SLinus Torvalds atkbd->translated = 1; 10981da177e4SLinus Torvalds case SERIO_8042: 10991da177e4SLinus Torvalds if (serio->write) 11001da177e4SLinus Torvalds atkbd->write = 1; 11011da177e4SLinus Torvalds break; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds atkbd->softraw = atkbd_softraw; 11051da177e4SLinus Torvalds atkbd->softrepeat = atkbd_softrepeat; 11061da177e4SLinus Torvalds atkbd->scroll = atkbd_scroll; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds if (atkbd->softrepeat) 11091da177e4SLinus Torvalds atkbd->softraw = 1; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds serio_set_drvdata(serio, atkbd); 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds err = serio_open(serio, drv); 11143c42f0c3SDmitry Torokhov if (err) 11152b03b60eSDmitry Torokhov goto fail2; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds if (atkbd->write) { 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds if (atkbd_probe(atkbd)) { 11203c42f0c3SDmitry Torokhov err = -ENODEV; 11212b03b60eSDmitry Torokhov goto fail3; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds atkbd->set = atkbd_select_set(atkbd, atkbd_set, atkbd_extra); 1125fc0eb28cSDmitry Torokhov atkbd_reset_state(atkbd); 11261da177e4SLinus Torvalds atkbd_activate(atkbd); 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds } else { 11291da177e4SLinus Torvalds atkbd->set = 2; 11301da177e4SLinus Torvalds atkbd->id = 0xab00; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds atkbd_set_keycode_table(atkbd); 11341da177e4SLinus Torvalds atkbd_set_device_attrs(atkbd); 11351da177e4SLinus Torvalds 11362b03b60eSDmitry Torokhov err = sysfs_create_group(&serio->dev.kobj, &atkbd_attribute_group); 11372b03b60eSDmitry Torokhov if (err) 11382b03b60eSDmitry Torokhov goto fail3; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds atkbd_enable(atkbd); 11411da177e4SLinus Torvalds 11422b03b60eSDmitry Torokhov err = input_register_device(atkbd->dev); 11432b03b60eSDmitry Torokhov if (err) 11442b03b60eSDmitry Torokhov goto fail4; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds return 0; 11473c42f0c3SDmitry Torokhov 11482b03b60eSDmitry Torokhov fail4: sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group); 11492b03b60eSDmitry Torokhov fail3: serio_close(serio); 11502b03b60eSDmitry Torokhov fail2: serio_set_drvdata(serio, NULL); 11512b03b60eSDmitry Torokhov fail1: input_free_device(dev); 11523c42f0c3SDmitry Torokhov kfree(atkbd); 11533c42f0c3SDmitry Torokhov return err; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* 11571da177e4SLinus Torvalds * atkbd_reconnect() tries to restore keyboard into a sane state and is 11581da177e4SLinus Torvalds * most likely called on resume. 11591da177e4SLinus Torvalds */ 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds static int atkbd_reconnect(struct serio *serio) 11621da177e4SLinus Torvalds { 11631da177e4SLinus Torvalds struct atkbd *atkbd = serio_get_drvdata(serio); 11641da177e4SLinus Torvalds struct serio_driver *drv = serio->drv; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds if (!atkbd || !drv) { 11671da177e4SLinus Torvalds printk(KERN_DEBUG "atkbd: reconnect request, but serio is disconnected, ignoring...\n"); 11681da177e4SLinus Torvalds return -1; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds atkbd_disable(atkbd); 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds if (atkbd->write) { 11741da177e4SLinus Torvalds if (atkbd_probe(atkbd)) 11751da177e4SLinus Torvalds return -1; 11761da177e4SLinus Torvalds if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra)) 11771da177e4SLinus Torvalds return -1; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds atkbd_activate(atkbd); 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds atkbd_enable(atkbd); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds return 0; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds static struct serio_device_id atkbd_serio_ids[] = { 11881da177e4SLinus Torvalds { 11891da177e4SLinus Torvalds .type = SERIO_8042, 11901da177e4SLinus Torvalds .proto = SERIO_ANY, 11911da177e4SLinus Torvalds .id = SERIO_ANY, 11921da177e4SLinus Torvalds .extra = SERIO_ANY, 11931da177e4SLinus Torvalds }, 11941da177e4SLinus Torvalds { 11951da177e4SLinus Torvalds .type = SERIO_8042_XL, 11961da177e4SLinus Torvalds .proto = SERIO_ANY, 11971da177e4SLinus Torvalds .id = SERIO_ANY, 11981da177e4SLinus Torvalds .extra = SERIO_ANY, 11991da177e4SLinus Torvalds }, 12001da177e4SLinus Torvalds { 12011da177e4SLinus Torvalds .type = SERIO_RS232, 12021da177e4SLinus Torvalds .proto = SERIO_PS2SER, 12031da177e4SLinus Torvalds .id = SERIO_ANY, 12041da177e4SLinus Torvalds .extra = SERIO_ANY, 12051da177e4SLinus Torvalds }, 12061da177e4SLinus Torvalds { 0 } 12071da177e4SLinus Torvalds }; 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, atkbd_serio_ids); 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds static struct serio_driver atkbd_drv = { 12121da177e4SLinus Torvalds .driver = { 12131da177e4SLinus Torvalds .name = "atkbd", 12141da177e4SLinus Torvalds }, 12151da177e4SLinus Torvalds .description = DRIVER_DESC, 12161da177e4SLinus Torvalds .id_table = atkbd_serio_ids, 12171da177e4SLinus Torvalds .interrupt = atkbd_interrupt, 12181da177e4SLinus Torvalds .connect = atkbd_connect, 12191da177e4SLinus Torvalds .reconnect = atkbd_reconnect, 12201da177e4SLinus Torvalds .disconnect = atkbd_disconnect, 12211da177e4SLinus Torvalds .cleanup = atkbd_cleanup, 12221da177e4SLinus Torvalds }; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf, 12251da177e4SLinus Torvalds ssize_t (*handler)(struct atkbd *, char *)) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds struct serio *serio = to_serio_port(dev); 12281da177e4SLinus Torvalds int retval; 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds retval = serio_pin_driver(serio); 12311da177e4SLinus Torvalds if (retval) 12321da177e4SLinus Torvalds return retval; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds if (serio->drv != &atkbd_drv) { 12351da177e4SLinus Torvalds retval = -ENODEV; 12361da177e4SLinus Torvalds goto out; 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds retval = handler((struct atkbd *)serio_get_drvdata(serio), buf); 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds out: 12421da177e4SLinus Torvalds serio_unpin_driver(serio); 12431da177e4SLinus Torvalds return retval; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count, 12471da177e4SLinus Torvalds ssize_t (*handler)(struct atkbd *, const char *, size_t)) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds struct serio *serio = to_serio_port(dev); 12501da177e4SLinus Torvalds struct atkbd *atkbd; 12511da177e4SLinus Torvalds int retval; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds retval = serio_pin_driver(serio); 12541da177e4SLinus Torvalds if (retval) 12551da177e4SLinus Torvalds return retval; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds if (serio->drv != &atkbd_drv) { 12581da177e4SLinus Torvalds retval = -ENODEV; 12591da177e4SLinus Torvalds goto out; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds atkbd = serio_get_drvdata(serio); 12631da177e4SLinus Torvalds atkbd_disable(atkbd); 12641da177e4SLinus Torvalds retval = handler(atkbd, buf, count); 12651da177e4SLinus Torvalds atkbd_enable(atkbd); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds out: 12681da177e4SLinus Torvalds serio_unpin_driver(serio); 12691da177e4SLinus Torvalds return retval; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds return sprintf(buf, "%d\n", atkbd->extra ? 1 : 0); 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds static ssize_t atkbd_set_extra(struct atkbd *atkbd, const char *buf, size_t count) 12781da177e4SLinus Torvalds { 12792b03b60eSDmitry Torokhov struct input_dev *old_dev, *new_dev; 12801da177e4SLinus Torvalds unsigned long value; 12812b03b60eSDmitry Torokhov int err; 12822b03b60eSDmitry Torokhov unsigned char old_extra, old_set; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (!atkbd->write) 12851da177e4SLinus Torvalds return -EIO; 12861da177e4SLinus Torvalds 1287160f1fefSJoe Rouvier if (strict_strtoul(buf, 10, &value) || value > 1) 12881da177e4SLinus Torvalds return -EINVAL; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds if (atkbd->extra != value) { 12913c42f0c3SDmitry Torokhov /* 12923c42f0c3SDmitry Torokhov * Since device's properties will change we need to 12932b03b60eSDmitry Torokhov * unregister old device. But allocate and register 12942b03b60eSDmitry Torokhov * new one first to make sure we have it. 12953c42f0c3SDmitry Torokhov */ 12962b03b60eSDmitry Torokhov old_dev = atkbd->dev; 12972b03b60eSDmitry Torokhov old_extra = atkbd->extra; 12982b03b60eSDmitry Torokhov old_set = atkbd->set; 12992b03b60eSDmitry Torokhov 13002b03b60eSDmitry Torokhov new_dev = input_allocate_device(); 13012b03b60eSDmitry Torokhov if (!new_dev) 13023c42f0c3SDmitry Torokhov return -ENOMEM; 13032b03b60eSDmitry Torokhov 13043c42f0c3SDmitry Torokhov atkbd->dev = new_dev; 13051da177e4SLinus Torvalds atkbd->set = atkbd_select_set(atkbd, atkbd->set, value); 1306fc0eb28cSDmitry Torokhov atkbd_reset_state(atkbd); 13071da177e4SLinus Torvalds atkbd_activate(atkbd); 13082b03b60eSDmitry Torokhov atkbd_set_keycode_table(atkbd); 13091da177e4SLinus Torvalds atkbd_set_device_attrs(atkbd); 13102b03b60eSDmitry Torokhov 13112b03b60eSDmitry Torokhov err = input_register_device(atkbd->dev); 13122b03b60eSDmitry Torokhov if (err) { 13132b03b60eSDmitry Torokhov input_free_device(new_dev); 13142b03b60eSDmitry Torokhov 13152b03b60eSDmitry Torokhov atkbd->dev = old_dev; 13162b03b60eSDmitry Torokhov atkbd->set = atkbd_select_set(atkbd, old_set, old_extra); 13172b03b60eSDmitry Torokhov atkbd_set_keycode_table(atkbd); 13182b03b60eSDmitry Torokhov atkbd_set_device_attrs(atkbd); 13192b03b60eSDmitry Torokhov 13202b03b60eSDmitry Torokhov return err; 13212b03b60eSDmitry Torokhov } 13222b03b60eSDmitry Torokhov input_unregister_device(old_dev); 13232b03b60eSDmitry Torokhov 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds return count; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281ba36e11SDmitry Torokhov static ssize_t atkbd_show_force_release(struct atkbd *atkbd, char *buf) 13291ba36e11SDmitry Torokhov { 13301ba36e11SDmitry Torokhov size_t len = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, 13311ba36e11SDmitry Torokhov atkbd->force_release_mask, ATKBD_KEYMAP_SIZE); 13321ba36e11SDmitry Torokhov 13331ba36e11SDmitry Torokhov buf[len++] = '\n'; 13341ba36e11SDmitry Torokhov buf[len] = '\0'; 13351ba36e11SDmitry Torokhov 13361ba36e11SDmitry Torokhov return len; 13371ba36e11SDmitry Torokhov } 13381ba36e11SDmitry Torokhov 13391ba36e11SDmitry Torokhov static ssize_t atkbd_set_force_release(struct atkbd *atkbd, 13401ba36e11SDmitry Torokhov const char *buf, size_t count) 13411ba36e11SDmitry Torokhov { 13421ba36e11SDmitry Torokhov /* 64 bytes on stack should be acceptable */ 13431ba36e11SDmitry Torokhov DECLARE_BITMAP(new_mask, ATKBD_KEYMAP_SIZE); 13441ba36e11SDmitry Torokhov int err; 13451ba36e11SDmitry Torokhov 13461ba36e11SDmitry Torokhov err = bitmap_parselist(buf, new_mask, ATKBD_KEYMAP_SIZE); 13471ba36e11SDmitry Torokhov if (err) 13481ba36e11SDmitry Torokhov return err; 13491ba36e11SDmitry Torokhov 13501ba36e11SDmitry Torokhov memcpy(atkbd->force_release_mask, new_mask, sizeof(atkbd->force_release_mask)); 13511ba36e11SDmitry Torokhov return count; 13521ba36e11SDmitry Torokhov } 13531ba36e11SDmitry Torokhov 13541ba36e11SDmitry Torokhov 13551da177e4SLinus Torvalds static ssize_t atkbd_show_scroll(struct atkbd *atkbd, char *buf) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds return sprintf(buf, "%d\n", atkbd->scroll ? 1 : 0); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds static ssize_t atkbd_set_scroll(struct atkbd *atkbd, const char *buf, size_t count) 13611da177e4SLinus Torvalds { 13622b03b60eSDmitry Torokhov struct input_dev *old_dev, *new_dev; 13631da177e4SLinus Torvalds unsigned long value; 13642b03b60eSDmitry Torokhov int err; 13652b03b60eSDmitry Torokhov unsigned char old_scroll; 13661da177e4SLinus Torvalds 1367160f1fefSJoe Rouvier if (strict_strtoul(buf, 10, &value) || value > 1) 13681da177e4SLinus Torvalds return -EINVAL; 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds if (atkbd->scroll != value) { 13712b03b60eSDmitry Torokhov old_dev = atkbd->dev; 13722b03b60eSDmitry Torokhov old_scroll = atkbd->scroll; 13732b03b60eSDmitry Torokhov 13742b03b60eSDmitry Torokhov new_dev = input_allocate_device(); 13752b03b60eSDmitry Torokhov if (!new_dev) 13763c42f0c3SDmitry Torokhov return -ENOMEM; 13772b03b60eSDmitry Torokhov 13783c42f0c3SDmitry Torokhov atkbd->dev = new_dev; 13791da177e4SLinus Torvalds atkbd->scroll = value; 13801da177e4SLinus Torvalds atkbd_set_keycode_table(atkbd); 13811da177e4SLinus Torvalds atkbd_set_device_attrs(atkbd); 13822b03b60eSDmitry Torokhov 13832b03b60eSDmitry Torokhov err = input_register_device(atkbd->dev); 13842b03b60eSDmitry Torokhov if (err) { 13852b03b60eSDmitry Torokhov input_free_device(new_dev); 13862b03b60eSDmitry Torokhov 13872b03b60eSDmitry Torokhov atkbd->scroll = old_scroll; 13882b03b60eSDmitry Torokhov atkbd->dev = old_dev; 13892b03b60eSDmitry Torokhov atkbd_set_keycode_table(atkbd); 13902b03b60eSDmitry Torokhov atkbd_set_device_attrs(atkbd); 13912b03b60eSDmitry Torokhov 13922b03b60eSDmitry Torokhov return err; 13932b03b60eSDmitry Torokhov } 13942b03b60eSDmitry Torokhov input_unregister_device(old_dev); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds return count; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds static ssize_t atkbd_show_set(struct atkbd *atkbd, char *buf) 14001da177e4SLinus Torvalds { 14011da177e4SLinus Torvalds return sprintf(buf, "%d\n", atkbd->set); 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds static ssize_t atkbd_set_set(struct atkbd *atkbd, const char *buf, size_t count) 14051da177e4SLinus Torvalds { 14062b03b60eSDmitry Torokhov struct input_dev *old_dev, *new_dev; 14071da177e4SLinus Torvalds unsigned long value; 14082b03b60eSDmitry Torokhov int err; 14092b03b60eSDmitry Torokhov unsigned char old_set, old_extra; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds if (!atkbd->write) 14121da177e4SLinus Torvalds return -EIO; 14131da177e4SLinus Torvalds 1414160f1fefSJoe Rouvier if (strict_strtoul(buf, 10, &value) || (value != 2 && value != 3)) 14151da177e4SLinus Torvalds return -EINVAL; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds if (atkbd->set != value) { 14182b03b60eSDmitry Torokhov old_dev = atkbd->dev; 14192b03b60eSDmitry Torokhov old_extra = atkbd->extra; 14202b03b60eSDmitry Torokhov old_set = atkbd->set; 14212b03b60eSDmitry Torokhov 14222b03b60eSDmitry Torokhov new_dev = input_allocate_device(); 14232b03b60eSDmitry Torokhov if (!new_dev) 14243c42f0c3SDmitry Torokhov return -ENOMEM; 14252b03b60eSDmitry Torokhov 14263c42f0c3SDmitry Torokhov atkbd->dev = new_dev; 14271da177e4SLinus Torvalds atkbd->set = atkbd_select_set(atkbd, value, atkbd->extra); 14281da177e4SLinus Torvalds atkbd_activate(atkbd); 14291da177e4SLinus Torvalds atkbd_set_keycode_table(atkbd); 14301da177e4SLinus Torvalds atkbd_set_device_attrs(atkbd); 14312b03b60eSDmitry Torokhov 14322b03b60eSDmitry Torokhov err = input_register_device(atkbd->dev); 14332b03b60eSDmitry Torokhov if (err) { 14342b03b60eSDmitry Torokhov input_free_device(new_dev); 14352b03b60eSDmitry Torokhov 14362b03b60eSDmitry Torokhov atkbd->dev = old_dev; 14372b03b60eSDmitry Torokhov atkbd->set = atkbd_select_set(atkbd, old_set, old_extra); 14382b03b60eSDmitry Torokhov atkbd_set_keycode_table(atkbd); 14392b03b60eSDmitry Torokhov atkbd_set_device_attrs(atkbd); 14402b03b60eSDmitry Torokhov 14412b03b60eSDmitry Torokhov return err; 14422b03b60eSDmitry Torokhov } 14432b03b60eSDmitry Torokhov input_unregister_device(old_dev); 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds return count; 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds static ssize_t atkbd_show_softrepeat(struct atkbd *atkbd, char *buf) 14491da177e4SLinus Torvalds { 14501da177e4SLinus Torvalds return sprintf(buf, "%d\n", atkbd->softrepeat ? 1 : 0); 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds static ssize_t atkbd_set_softrepeat(struct atkbd *atkbd, const char *buf, size_t count) 14541da177e4SLinus Torvalds { 14552b03b60eSDmitry Torokhov struct input_dev *old_dev, *new_dev; 14561da177e4SLinus Torvalds unsigned long value; 14572b03b60eSDmitry Torokhov int err; 14582b03b60eSDmitry Torokhov unsigned char old_softrepeat, old_softraw; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds if (!atkbd->write) 14611da177e4SLinus Torvalds return -EIO; 14621da177e4SLinus Torvalds 1463160f1fefSJoe Rouvier if (strict_strtoul(buf, 10, &value) || value > 1) 14641da177e4SLinus Torvalds return -EINVAL; 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds if (atkbd->softrepeat != value) { 14672b03b60eSDmitry Torokhov old_dev = atkbd->dev; 14682b03b60eSDmitry Torokhov old_softrepeat = atkbd->softrepeat; 14692b03b60eSDmitry Torokhov old_softraw = atkbd->softraw; 14702b03b60eSDmitry Torokhov 14712b03b60eSDmitry Torokhov new_dev = input_allocate_device(); 14722b03b60eSDmitry Torokhov if (!new_dev) 14733c42f0c3SDmitry Torokhov return -ENOMEM; 14742b03b60eSDmitry Torokhov 14753c42f0c3SDmitry Torokhov atkbd->dev = new_dev; 14761da177e4SLinus Torvalds atkbd->softrepeat = value; 14771da177e4SLinus Torvalds if (atkbd->softrepeat) 14781da177e4SLinus Torvalds atkbd->softraw = 1; 14791da177e4SLinus Torvalds atkbd_set_device_attrs(atkbd); 14802b03b60eSDmitry Torokhov 14812b03b60eSDmitry Torokhov err = input_register_device(atkbd->dev); 14822b03b60eSDmitry Torokhov if (err) { 14832b03b60eSDmitry Torokhov input_free_device(new_dev); 14842b03b60eSDmitry Torokhov 14852b03b60eSDmitry Torokhov atkbd->dev = old_dev; 14862b03b60eSDmitry Torokhov atkbd->softrepeat = old_softrepeat; 14872b03b60eSDmitry Torokhov atkbd->softraw = old_softraw; 14882b03b60eSDmitry Torokhov atkbd_set_device_attrs(atkbd); 14892b03b60eSDmitry Torokhov 14902b03b60eSDmitry Torokhov return err; 14912b03b60eSDmitry Torokhov } 14922b03b60eSDmitry Torokhov input_unregister_device(old_dev); 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds return count; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds static ssize_t atkbd_show_softraw(struct atkbd *atkbd, char *buf) 14991da177e4SLinus Torvalds { 15001da177e4SLinus Torvalds return sprintf(buf, "%d\n", atkbd->softraw ? 1 : 0); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds static ssize_t atkbd_set_softraw(struct atkbd *atkbd, const char *buf, size_t count) 15041da177e4SLinus Torvalds { 15052b03b60eSDmitry Torokhov struct input_dev *old_dev, *new_dev; 15061da177e4SLinus Torvalds unsigned long value; 15072b03b60eSDmitry Torokhov int err; 15082b03b60eSDmitry Torokhov unsigned char old_softraw; 15091da177e4SLinus Torvalds 1510160f1fefSJoe Rouvier if (strict_strtoul(buf, 10, &value) || value > 1) 15111da177e4SLinus Torvalds return -EINVAL; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds if (atkbd->softraw != value) { 15142b03b60eSDmitry Torokhov old_dev = atkbd->dev; 15152b03b60eSDmitry Torokhov old_softraw = atkbd->softraw; 15162b03b60eSDmitry Torokhov 15172b03b60eSDmitry Torokhov new_dev = input_allocate_device(); 15182b03b60eSDmitry Torokhov if (!new_dev) 15193c42f0c3SDmitry Torokhov return -ENOMEM; 15202b03b60eSDmitry Torokhov 15213c42f0c3SDmitry Torokhov atkbd->dev = new_dev; 15221da177e4SLinus Torvalds atkbd->softraw = value; 15231da177e4SLinus Torvalds atkbd_set_device_attrs(atkbd); 15242b03b60eSDmitry Torokhov 15252b03b60eSDmitry Torokhov err = input_register_device(atkbd->dev); 15262b03b60eSDmitry Torokhov if (err) { 15272b03b60eSDmitry Torokhov input_free_device(new_dev); 15282b03b60eSDmitry Torokhov 15292b03b60eSDmitry Torokhov atkbd->dev = old_dev; 15302b03b60eSDmitry Torokhov atkbd->softraw = old_softraw; 15312b03b60eSDmitry Torokhov atkbd_set_device_attrs(atkbd); 15322b03b60eSDmitry Torokhov 15332b03b60eSDmitry Torokhov return err; 15342b03b60eSDmitry Torokhov } 15352b03b60eSDmitry Torokhov input_unregister_device(old_dev); 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds return count; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 154086255d9dSDmitry Torokhov static ssize_t atkbd_show_err_count(struct atkbd *atkbd, char *buf) 154186255d9dSDmitry Torokhov { 154286255d9dSDmitry Torokhov return sprintf(buf, "%lu\n", atkbd->err_count); 154386255d9dSDmitry Torokhov } 154486255d9dSDmitry Torokhov 154539191698SDaniel Mierswa static int __init atkbd_setup_forced_release(const struct dmi_system_id *id) 1546554101e3SGiel de Nijs { 154739191698SDaniel Mierswa atkbd_platform_fixup = atkbd_apply_forced_release_keylist; 154839191698SDaniel Mierswa atkbd_platform_fixup_data = id->driver_data; 154939191698SDaniel Mierswa 1550554101e3SGiel de Nijs return 0; 1551554101e3SGiel de Nijs } 1552554101e3SGiel de Nijs 1553554101e3SGiel de Nijs static struct dmi_system_id atkbd_dmi_quirk_table[] __initdata = { 1554554101e3SGiel de Nijs { 155561579ba8SMatthew Garrett .ident = "Dell Laptop", 1556554101e3SGiel de Nijs .matches = { 1557554101e3SGiel de Nijs DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 155861579ba8SMatthew Garrett DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */ 1559554101e3SGiel de Nijs }, 156039191698SDaniel Mierswa .callback = atkbd_setup_forced_release, 156139191698SDaniel Mierswa .driver_data = atkbd_dell_laptop_forced_release_keys, 1562554101e3SGiel de Nijs }, 15635a54c011SJiri Kosina { 15642a3ec326SMatthew Garrett .ident = "Dell Laptop", 15652a3ec326SMatthew Garrett .matches = { 15662a3ec326SMatthew Garrett DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"), 15672a3ec326SMatthew Garrett DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */ 15682a3ec326SMatthew Garrett }, 156939191698SDaniel Mierswa .callback = atkbd_setup_forced_release, 157039191698SDaniel Mierswa .driver_data = atkbd_dell_laptop_forced_release_keys, 15712a3ec326SMatthew Garrett }, 15722a3ec326SMatthew Garrett { 15735a54c011SJiri Kosina .ident = "HP 2133", 15745a54c011SJiri Kosina .matches = { 15755a54c011SJiri Kosina DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 15765a54c011SJiri Kosina DMI_MATCH(DMI_PRODUCT_NAME, "HP 2133"), 15775a54c011SJiri Kosina }, 157839191698SDaniel Mierswa .callback = atkbd_setup_forced_release, 157939191698SDaniel Mierswa .driver_data = atkbd_hp_forced_release_keys, 15805a54c011SJiri Kosina }, 1581a8215b81SMatthew Garrett { 1582181f6382SRikard Ljungstrand .ident = "HP Pavilion ZV6100", 1583181f6382SRikard Ljungstrand .matches = { 1584181f6382SRikard Ljungstrand DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1585181f6382SRikard Ljungstrand DMI_MATCH(DMI_PRODUCT_NAME, "Pavilion ZV6100"), 1586181f6382SRikard Ljungstrand }, 158739191698SDaniel Mierswa .callback = atkbd_setup_forced_release, 158839191698SDaniel Mierswa .driver_data = atkbd_hp_zv6100_forced_release_keys, 1589181f6382SRikard Ljungstrand }, 1590181f6382SRikard Ljungstrand { 15912bcaa6a4SDave Andrews .ident = "HP Presario R4000", 15922bcaa6a4SDave Andrews .matches = { 15932bcaa6a4SDave Andrews DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 15942bcaa6a4SDave Andrews DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4000"), 15952bcaa6a4SDave Andrews }, 15962bcaa6a4SDave Andrews .callback = atkbd_setup_forced_release, 15972bcaa6a4SDave Andrews .driver_data = atkbd_hp_r4000_forced_release_keys, 15982bcaa6a4SDave Andrews }, 15992bcaa6a4SDave Andrews { 16002bcaa6a4SDave Andrews .ident = "HP Presario R4100", 16012bcaa6a4SDave Andrews .matches = { 16022bcaa6a4SDave Andrews DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 16032bcaa6a4SDave Andrews DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4100"), 16042bcaa6a4SDave Andrews }, 16052bcaa6a4SDave Andrews .callback = atkbd_setup_forced_release, 16062bcaa6a4SDave Andrews .driver_data = atkbd_hp_r4000_forced_release_keys, 16072bcaa6a4SDave Andrews }, 16082bcaa6a4SDave Andrews { 16092bcaa6a4SDave Andrews .ident = "HP Presario R4200", 16102bcaa6a4SDave Andrews .matches = { 16112bcaa6a4SDave Andrews DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 16122bcaa6a4SDave Andrews DMI_MATCH(DMI_PRODUCT_NAME, "Presario R4200"), 16132bcaa6a4SDave Andrews }, 16142bcaa6a4SDave Andrews .callback = atkbd_setup_forced_release, 16152bcaa6a4SDave Andrews .driver_data = atkbd_hp_r4000_forced_release_keys, 16162bcaa6a4SDave Andrews }, 16172bcaa6a4SDave Andrews { 1618a8215b81SMatthew Garrett .ident = "Inventec Symphony", 1619a8215b81SMatthew Garrett .matches = { 1620a8215b81SMatthew Garrett DMI_MATCH(DMI_SYS_VENDOR, "INVENTEC"), 1621a8215b81SMatthew Garrett DMI_MATCH(DMI_PRODUCT_NAME, "SYMPHONY 6.0/7.0"), 1622a8215b81SMatthew Garrett }, 162339191698SDaniel Mierswa .callback = atkbd_setup_forced_release, 162439191698SDaniel Mierswa .driver_data = atkbd_inventec_forced_release_keys, 1625a8215b81SMatthew Garrett }, 16264200844bSStuart Hopkins { 16274200844bSStuart Hopkins .ident = "Samsung NC10", 16284200844bSStuart Hopkins .matches = { 16294200844bSStuart Hopkins DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 16304200844bSStuart Hopkins DMI_MATCH(DMI_PRODUCT_NAME, "NC10"), 16314200844bSStuart Hopkins }, 163239191698SDaniel Mierswa .callback = atkbd_setup_forced_release, 163339191698SDaniel Mierswa .driver_data = atkbd_samsung_forced_release_keys, 16344200844bSStuart Hopkins }, 1635adcb523eSDaniel Mierswa { 1636e04126c7SBarry Carroll .ident = "Samsung NC20", 1637e04126c7SBarry Carroll .matches = { 1638e04126c7SBarry Carroll DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 1639e04126c7SBarry Carroll DMI_MATCH(DMI_PRODUCT_NAME, "NC20"), 1640e04126c7SBarry Carroll }, 1641e04126c7SBarry Carroll .callback = atkbd_setup_forced_release, 1642e04126c7SBarry Carroll .driver_data = atkbd_samsung_forced_release_keys, 1643e04126c7SBarry Carroll }, 1644e04126c7SBarry Carroll { 1645157f3a3eSDmitry Torokhov .ident = "Samsung SQ45S70S", 1646157f3a3eSDmitry Torokhov .matches = { 1647157f3a3eSDmitry Torokhov DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 1648157f3a3eSDmitry Torokhov DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"), 1649157f3a3eSDmitry Torokhov }, 1650157f3a3eSDmitry Torokhov .callback = atkbd_setup_forced_release, 1651157f3a3eSDmitry Torokhov .driver_data = atkbd_samsung_forced_release_keys, 1652157f3a3eSDmitry Torokhov }, 1653157f3a3eSDmitry Torokhov { 1654adcb523eSDaniel Mierswa .ident = "Fujitsu Amilo PA 1510", 1655adcb523eSDaniel Mierswa .matches = { 1656adcb523eSDaniel Mierswa DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 1657adcb523eSDaniel Mierswa DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pa 1510"), 1658adcb523eSDaniel Mierswa }, 1659adcb523eSDaniel Mierswa .callback = atkbd_setup_forced_release, 1660adcb523eSDaniel Mierswa .driver_data = atkbd_amilo_pa1510_forced_release_keys, 1661adcb523eSDaniel Mierswa }, 16629166d0f6SAdrian Batzill { 1663f0a14de2SSimon Davie .ident = "Fujitsu Amilo Pi 3525", 1664f0a14de2SSimon Davie .matches = { 1665f0a14de2SSimon Davie DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 1666f0a14de2SSimon Davie DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pi 3525"), 1667f0a14de2SSimon Davie }, 1668f0a14de2SSimon Davie .callback = atkbd_setup_forced_release, 1669f0a14de2SSimon Davie .driver_data = atkbd_amilo_pi3525_forced_release_keys, 1670f0a14de2SSimon Davie }, 1671f0a14de2SSimon Davie { 16729166d0f6SAdrian Batzill .ident = "Fujitsu Amilo Xi 3650", 16739166d0f6SAdrian Batzill .matches = { 16749166d0f6SAdrian Batzill DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 16759166d0f6SAdrian Batzill DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xi 3650"), 16769166d0f6SAdrian Batzill }, 16779166d0f6SAdrian Batzill .callback = atkbd_setup_forced_release, 16789166d0f6SAdrian Batzill .driver_data = atkbd_amilo_xi3650_forced_release_keys, 16799166d0f6SAdrian Batzill }, 1680032e46cbSJerone Young { 1681032e46cbSJerone Young .ident = "Soltech Corporation TA12", 1682032e46cbSJerone Young .matches = { 1683032e46cbSJerone Young DMI_MATCH(DMI_SYS_VENDOR, "Soltech Corporation"), 1684032e46cbSJerone Young DMI_MATCH(DMI_PRODUCT_NAME, "TA12"), 1685032e46cbSJerone Young }, 1686032e46cbSJerone Young .callback = atkbd_setup_forced_release, 1687032e46cbSJerone Young .driver_data = atkdb_soltech_ta12_forced_release_keys, 1688032e46cbSJerone Young }, 1689554101e3SGiel de Nijs { } 1690554101e3SGiel de Nijs }; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds static int __init atkbd_init(void) 16931da177e4SLinus Torvalds { 1694554101e3SGiel de Nijs dmi_check_system(atkbd_dmi_quirk_table); 1695554101e3SGiel de Nijs 1696153a9df0SAkinobu Mita return serio_register_driver(&atkbd_drv); 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds static void __exit atkbd_exit(void) 17001da177e4SLinus Torvalds { 17011da177e4SLinus Torvalds serio_unregister_driver(&atkbd_drv); 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds module_init(atkbd_init); 17051da177e4SLinus Torvalds module_exit(atkbd_exit); 1706