xref: /linux/drivers/input/keyboard/atkbd.c (revision 3d0f0fa0cb554541e10cb8cb84104e4b10828468)
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/moduleparam.h>
231da177e4SLinus Torvalds #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/interrupt.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/input.h>
271da177e4SLinus Torvalds #include <linux/serio.h>
281da177e4SLinus Torvalds #include <linux/workqueue.h>
291da177e4SLinus Torvalds #include <linux/libps2.h>
3033d3f07aSIngo Molnar #include <linux/mutex.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 __obsolete_setup("atkbd_set=");
671da177e4SLinus Torvalds __obsolete_setup("atkbd_reset");
681da177e4SLinus Torvalds __obsolete_setup("atkbd_softrepeat=");
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /*
711da177e4SLinus Torvalds  * Scancode to keycode tables. These are just the default setting, and
721da177e4SLinus Torvalds  * are loadable via an userland utility.
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static unsigned char atkbd_set2_keycode[512] = {
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #ifdef CONFIG_KEYBOARD_ATKBD_HP_KEYCODES
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* XXX: need a more general approach */
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #include "hpps2atkbd.h"	/* include the keyboard scancodes */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #else
841da177e4SLinus Torvalds 	  0, 67, 65, 63, 61, 59, 60, 88,  0, 68, 66, 64, 62, 15, 41,117,
851da177e4SLinus Torvalds 	  0, 56, 42, 93, 29, 16,  2,  0,  0,  0, 44, 31, 30, 17,  3,  0,
861da177e4SLinus Torvalds 	  0, 46, 45, 32, 18,  5,  4, 95,  0, 57, 47, 33, 20, 19,  6,183,
871da177e4SLinus Torvalds 	  0, 49, 48, 35, 34, 21,  7,184,  0,  0, 50, 36, 22,  8,  9,185,
881da177e4SLinus Torvalds 	  0, 51, 37, 23, 24, 11, 10,  0,  0, 52, 53, 38, 39, 25, 12,  0,
891da177e4SLinus Torvalds 	  0, 89, 40,  0, 26, 13,  0,  0, 58, 54, 28, 27,  0, 43,  0, 85,
901da177e4SLinus Torvalds 	  0, 86, 91, 90, 92,  0, 14, 94,  0, 79,124, 75, 71,121,  0,  0,
911da177e4SLinus Torvalds 	 82, 83, 80, 76, 77, 72,  1, 69, 87, 78, 81, 74, 55, 73, 70, 99,
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
941da177e4SLinus Torvalds 	217,100,255,  0, 97,165,  0,  0,156,  0,  0,  0,  0,  0,  0,125,
951da177e4SLinus Torvalds 	173,114,  0,113,  0,  0,  0,126,128,  0,  0,140,  0,  0,  0,127,
961da177e4SLinus Torvalds 	159,  0,115,  0,164,  0,  0,116,158,  0,150,166,  0,  0,  0,142,
971da177e4SLinus Torvalds 	157,  0,  0,  0,  0,  0,  0,  0,155,  0, 98,  0,  0,163,  0,  0,
981da177e4SLinus Torvalds 	226,  0,  0,  0,  0,  0,  0,  0,  0,255, 96,  0,  0,  0,143,  0,
991da177e4SLinus Torvalds 	  0,  0,  0,  0,  0,  0,  0,  0,  0,107,  0,105,102,  0,  0,112,
1001da177e4SLinus Torvalds 	110,111,108,112,106,103,  0,119,  0,118,109,  0, 99,104,119,  0,
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	  0,  0,  0, 65, 99,
1031da177e4SLinus Torvalds #endif
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static unsigned char atkbd_set3_keycode[512] = {
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	  0,  0,  0,  0,  0,  0,  0, 59,  1,138,128,129,130, 15, 41, 60,
1091da177e4SLinus Torvalds 	131, 29, 42, 86, 58, 16,  2, 61,133, 56, 44, 31, 30, 17,  3, 62,
1101da177e4SLinus Torvalds 	134, 46, 45, 32, 18,  5,  4, 63,135, 57, 47, 33, 20, 19,  6, 64,
1111da177e4SLinus Torvalds 	136, 49, 48, 35, 34, 21,  7, 65,137,100, 50, 36, 22,  8,  9, 66,
1121da177e4SLinus Torvalds 	125, 51, 37, 23, 24, 11, 10, 67,126, 52, 53, 38, 39, 25, 12, 68,
1131da177e4SLinus Torvalds 	113,114, 40, 43, 26, 13, 87, 99, 97, 54, 28, 27, 43, 43, 88, 70,
1141da177e4SLinus Torvalds 	108,105,119,103,111,107, 14,110,  0, 79,106, 75, 71,109,102,104,
1151da177e4SLinus Torvalds 	 82, 83, 80, 76, 77, 72, 69, 98,  0, 96, 81,  0, 78, 73, 55,183,
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	184,185,186,187, 74, 94, 92, 93,  0,  0,  0,125,126,127,112,  0,
1181da177e4SLinus Torvalds 	  0,139,150,163,165,115,152,150,166,140,160,154,113,114,167,168,
1191da177e4SLinus Torvalds 	148,149,147,140
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds static unsigned char atkbd_unxlate_table[128] = {
1231da177e4SLinus Torvalds           0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
1241da177e4SLinus Torvalds          21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
1251da177e4SLinus Torvalds          35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
1261da177e4SLinus Torvalds          50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88,  5,  6,  4, 12,  3,
1271da177e4SLinus Torvalds          11,  2, 10,  1,  9,119,126,108,117,125,123,107,115,116,121,105,
1281da177e4SLinus Torvalds         114,122,112,113,127, 96, 97,120,  7, 15, 23, 31, 39, 47, 55, 63,
1291da177e4SLinus Torvalds          71, 79, 86, 94,  8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
1301da177e4SLinus Torvalds          19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define ATKBD_CMD_SETLEDS	0x10ed
1341da177e4SLinus Torvalds #define ATKBD_CMD_GSCANSET	0x11f0
1351da177e4SLinus Torvalds #define ATKBD_CMD_SSCANSET	0x10f0
1361da177e4SLinus Torvalds #define ATKBD_CMD_GETID		0x02f2
1371da177e4SLinus Torvalds #define ATKBD_CMD_SETREP	0x10f3
1381da177e4SLinus Torvalds #define ATKBD_CMD_ENABLE	0x00f4
1391da177e4SLinus Torvalds #define ATKBD_CMD_RESET_DIS	0x00f5
1401da177e4SLinus Torvalds #define ATKBD_CMD_SETALL_MBR	0x00fa
1411da177e4SLinus Torvalds #define ATKBD_CMD_RESET_BAT	0x02ff
1421da177e4SLinus Torvalds #define ATKBD_CMD_RESEND	0x00fe
1431da177e4SLinus Torvalds #define ATKBD_CMD_EX_ENABLE	0x10ea
1441da177e4SLinus Torvalds #define ATKBD_CMD_EX_SETLEDS	0x20eb
1451da177e4SLinus Torvalds #define ATKBD_CMD_OK_GETID	0x02e8
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds #define ATKBD_RET_ACK		0xfa
1481da177e4SLinus Torvalds #define ATKBD_RET_NAK		0xfe
1491da177e4SLinus Torvalds #define ATKBD_RET_BAT		0xaa
1501da177e4SLinus Torvalds #define ATKBD_RET_EMUL0		0xe0
1511da177e4SLinus Torvalds #define ATKBD_RET_EMUL1		0xe1
1521da177e4SLinus Torvalds #define ATKBD_RET_RELEASE	0xf0
1530ae051a1SDmitry Torokhov #define ATKBD_RET_HANJA		0xf1
1540ae051a1SDmitry Torokhov #define ATKBD_RET_HANGEUL	0xf2
1551da177e4SLinus Torvalds #define ATKBD_RET_ERR		0xff
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #define ATKBD_KEY_UNKNOWN	  0
1581da177e4SLinus Torvalds #define ATKBD_KEY_NULL		255
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds #define ATKBD_SCR_1		254
1611da177e4SLinus Torvalds #define ATKBD_SCR_2		253
1621da177e4SLinus Torvalds #define ATKBD_SCR_4		252
1631da177e4SLinus Torvalds #define ATKBD_SCR_8		251
1641da177e4SLinus Torvalds #define ATKBD_SCR_CLICK		250
1651da177e4SLinus Torvalds #define ATKBD_SCR_LEFT		249
1661da177e4SLinus Torvalds #define ATKBD_SCR_RIGHT		248
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds #define ATKBD_SPECIAL		248
1691da177e4SLinus Torvalds 
1700d4c8597SDmitry Torokhov #define ATKBD_LED_EVENT_BIT	0
1710d4c8597SDmitry Torokhov #define ATKBD_REP_EVENT_BIT	1
1720d4c8597SDmitry Torokhov 
1730ae051a1SDmitry Torokhov #define ATKBD_XL_ERR		0x01
1740ae051a1SDmitry Torokhov #define ATKBD_XL_BAT		0x02
1750ae051a1SDmitry Torokhov #define ATKBD_XL_ACK		0x04
1760ae051a1SDmitry Torokhov #define ATKBD_XL_NAK		0x08
1770ae051a1SDmitry Torokhov #define ATKBD_XL_HANGEUL	0x10
1780ae051a1SDmitry Torokhov #define ATKBD_XL_HANJA		0x20
1790ae051a1SDmitry Torokhov 
1801da177e4SLinus Torvalds static struct {
1811da177e4SLinus Torvalds 	unsigned char keycode;
1821da177e4SLinus Torvalds 	unsigned char set2;
1831da177e4SLinus Torvalds } atkbd_scroll_keys[] = {
1841da177e4SLinus Torvalds 	{ ATKBD_SCR_1,     0xc5 },
1855212dd58SVojtech Pavlik 	{ ATKBD_SCR_2,     0x9d },
1865212dd58SVojtech Pavlik 	{ ATKBD_SCR_4,     0xa4 },
1875212dd58SVojtech Pavlik 	{ ATKBD_SCR_8,     0x9b },
1881da177e4SLinus Torvalds 	{ ATKBD_SCR_CLICK, 0xe0 },
1891da177e4SLinus Torvalds 	{ ATKBD_SCR_LEFT,  0xcb },
1901da177e4SLinus Torvalds 	{ ATKBD_SCR_RIGHT, 0xd2 },
1911da177e4SLinus Torvalds };
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds  * The atkbd control structure
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds struct atkbd {
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	struct ps2dev ps2dev;
2003c42f0c3SDmitry Torokhov 	struct input_dev *dev;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/* Written only during init */
2031da177e4SLinus Torvalds 	char name[64];
2041da177e4SLinus Torvalds 	char phys[32];
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	unsigned short id;
2071da177e4SLinus Torvalds 	unsigned char keycode[512];
2081da177e4SLinus Torvalds 	unsigned char set;
2091da177e4SLinus Torvalds 	unsigned char translated;
2101da177e4SLinus Torvalds 	unsigned char extra;
2111da177e4SLinus Torvalds 	unsigned char write;
2121da177e4SLinus Torvalds 	unsigned char softrepeat;
2131da177e4SLinus Torvalds 	unsigned char softraw;
2141da177e4SLinus Torvalds 	unsigned char scroll;
2151da177e4SLinus Torvalds 	unsigned char enabled;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Accessed only from interrupt */
2181da177e4SLinus Torvalds 	unsigned char emul;
2191da177e4SLinus Torvalds 	unsigned char resend;
2201da177e4SLinus Torvalds 	unsigned char release;
2210ae051a1SDmitry Torokhov 	unsigned long xl_bit;
2221da177e4SLinus Torvalds 	unsigned int last;
2231da177e4SLinus Torvalds 	unsigned long time;
2240d4c8597SDmitry Torokhov 
2250d4c8597SDmitry Torokhov 	struct work_struct event_work;
22633d3f07aSIngo Molnar 	struct mutex event_mutex;
2270d4c8597SDmitry Torokhov 	unsigned long event_mask;
2281da177e4SLinus Torvalds };
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf,
2311da177e4SLinus Torvalds 				ssize_t (*handler)(struct atkbd *, char *));
2321da177e4SLinus Torvalds static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count,
2331da177e4SLinus Torvalds 				ssize_t (*handler)(struct atkbd *, const char *, size_t));
2341da177e4SLinus Torvalds #define ATKBD_DEFINE_ATTR(_name)						\
2351da177e4SLinus Torvalds static ssize_t atkbd_show_##_name(struct atkbd *, char *);			\
2361da177e4SLinus Torvalds static ssize_t atkbd_set_##_name(struct atkbd *, const char *, size_t);		\
237e404e274SYani Ioannou static ssize_t atkbd_do_show_##_name(struct device *d, struct device_attribute *attr, char *b)			\
2381da177e4SLinus Torvalds {										\
2391da177e4SLinus Torvalds 	return atkbd_attr_show_helper(d, b, atkbd_show_##_name);		\
2401da177e4SLinus Torvalds }										\
241e404e274SYani Ioannou static ssize_t atkbd_do_set_##_name(struct device *d, struct device_attribute *attr, const char *b, size_t s)	\
2421da177e4SLinus Torvalds {										\
2431da177e4SLinus Torvalds 	return atkbd_attr_set_helper(d, b, s, atkbd_set_##_name);		\
2441da177e4SLinus Torvalds }										\
2451da177e4SLinus Torvalds static struct device_attribute atkbd_attr_##_name =				\
2461da177e4SLinus Torvalds 	__ATTR(_name, S_IWUSR | S_IRUGO, atkbd_do_show_##_name, atkbd_do_set_##_name);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(extra);
2491da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(scroll);
2501da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(set);
2511da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(softrepeat);
2521da177e4SLinus Torvalds ATKBD_DEFINE_ATTR(softraw);
2531da177e4SLinus Torvalds 
2540ae051a1SDmitry Torokhov static const unsigned int xl_table[] = {
2550ae051a1SDmitry Torokhov 	ATKBD_RET_BAT, ATKBD_RET_ERR, ATKBD_RET_ACK,
2560ae051a1SDmitry Torokhov 	ATKBD_RET_NAK, ATKBD_RET_HANJA, ATKBD_RET_HANGEUL,
2570ae051a1SDmitry Torokhov };
2581da177e4SLinus Torvalds 
2590ae051a1SDmitry Torokhov /*
2600ae051a1SDmitry Torokhov  * Checks if we should mangle the scancode to extract 'release' bit
2610ae051a1SDmitry Torokhov  * in translated mode.
2620ae051a1SDmitry Torokhov  */
2630ae051a1SDmitry Torokhov static int atkbd_need_xlate(unsigned long xl_bit, unsigned char code)
2641da177e4SLinus Torvalds {
2650ae051a1SDmitry Torokhov 	int i;
2660ae051a1SDmitry Torokhov 
2670ae051a1SDmitry Torokhov 	if (code == ATKBD_RET_EMUL0 || code == ATKBD_RET_EMUL1)
2680ae051a1SDmitry Torokhov 		return 0;
2690ae051a1SDmitry Torokhov 
2700ae051a1SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(xl_table); i++)
2710ae051a1SDmitry Torokhov 		if (code == xl_table[i])
2720ae051a1SDmitry Torokhov 			return test_bit(i, &xl_bit);
2730ae051a1SDmitry Torokhov 
2740ae051a1SDmitry Torokhov 	return 1;
2750ae051a1SDmitry Torokhov }
2760ae051a1SDmitry Torokhov 
2770ae051a1SDmitry Torokhov /*
2780ae051a1SDmitry Torokhov  * Calculates new value of xl_bit so the driver can distinguish
2790ae051a1SDmitry Torokhov  * between make/break pair of scancodes for select keys and PS/2
2800ae051a1SDmitry Torokhov  * protocol responses.
2810ae051a1SDmitry Torokhov  */
2820ae051a1SDmitry Torokhov static void atkbd_calculate_xl_bit(struct atkbd *atkbd, unsigned char code)
2830ae051a1SDmitry Torokhov {
2840ae051a1SDmitry Torokhov 	int i;
2850ae051a1SDmitry Torokhov 
2860ae051a1SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(xl_table); i++) {
2870ae051a1SDmitry Torokhov 		if (!((code ^ xl_table[i]) & 0x7f)) {
2880ae051a1SDmitry Torokhov 			if (code & 0x80)
2890ae051a1SDmitry Torokhov 				__clear_bit(i, &atkbd->xl_bit);
2900ae051a1SDmitry Torokhov 			else
2910ae051a1SDmitry Torokhov 				__set_bit(i, &atkbd->xl_bit);
2920ae051a1SDmitry Torokhov 			break;
2930ae051a1SDmitry Torokhov 		}
2940ae051a1SDmitry Torokhov 	}
2950ae051a1SDmitry Torokhov }
2960ae051a1SDmitry Torokhov 
2970ae051a1SDmitry Torokhov /*
2980ae051a1SDmitry Torokhov  * Encode the scancode, 0xe0 prefix, and high bit into a single integer,
2990ae051a1SDmitry Torokhov  * keeping kernel 2.4 compatibility for set 2
3000ae051a1SDmitry Torokhov  */
3010ae051a1SDmitry Torokhov static unsigned int atkbd_compat_scancode(struct atkbd *atkbd, unsigned int code)
3020ae051a1SDmitry Torokhov {
3030ae051a1SDmitry Torokhov 	if (atkbd->set == 3) {
3040ae051a1SDmitry Torokhov 		if (atkbd->emul == 1)
3050ae051a1SDmitry Torokhov 			code |= 0x100;
3060ae051a1SDmitry Torokhov         } else {
3070ae051a1SDmitry Torokhov 		code = (code & 0x7f) | ((code & 0x80) << 1);
3080ae051a1SDmitry Torokhov 		if (atkbd->emul == 1)
3090ae051a1SDmitry Torokhov 			code |= 0x80;
3100ae051a1SDmitry Torokhov 	}
3110ae051a1SDmitry Torokhov 
3120ae051a1SDmitry Torokhov 	return code;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds /*
3161da177e4SLinus Torvalds  * atkbd_interrupt(). Here takes place processing of data received from
3171da177e4SLinus Torvalds  * the keyboard into events.
3181da177e4SLinus Torvalds  */
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
3211da177e4SLinus Torvalds 			unsigned int flags, struct pt_regs *regs)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct atkbd *atkbd = serio_get_drvdata(serio);
3240ae051a1SDmitry Torokhov 	struct input_dev *dev = atkbd->dev;
3251da177e4SLinus Torvalds 	unsigned int code = data;
3260ae051a1SDmitry Torokhov 	int scroll = 0, hscroll = 0, click = -1, add_release_event = 0;
3271da177e4SLinus Torvalds 	int value;
3280ae051a1SDmitry Torokhov 	unsigned char keycode;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds #ifdef ATKBD_DEBUG
3311da177e4SLinus Torvalds 	printk(KERN_DEBUG "atkbd.c: Received %02x flags %02x\n", data, flags);
3321da177e4SLinus Torvalds #endif
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds #if !defined(__i386__) && !defined (__x86_64__)
3351da177e4SLinus Torvalds 	if ((flags & (SERIO_FRAME | SERIO_PARITY)) && (~flags & SERIO_TIMEOUT) && !atkbd->resend && atkbd->write) {
3361da177e4SLinus Torvalds 		printk(KERN_WARNING "atkbd.c: frame/parity error: %02x\n", flags);
3371da177e4SLinus Torvalds 		serio_write(serio, ATKBD_CMD_RESEND);
3381da177e4SLinus Torvalds 		atkbd->resend = 1;
3391da177e4SLinus Torvalds 		goto out;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if (!flags && data == ATKBD_RET_ACK)
3431da177e4SLinus Torvalds 		atkbd->resend = 0;
3441da177e4SLinus Torvalds #endif
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_ACK))
3471da177e4SLinus Torvalds 		if  (ps2_handle_ack(&atkbd->ps2dev, data))
3481da177e4SLinus Torvalds 			goto out;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_CMD))
3511da177e4SLinus Torvalds 		if  (ps2_handle_response(&atkbd->ps2dev, data))
3521da177e4SLinus Torvalds 			goto out;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	if (!atkbd->enabled)
3551da177e4SLinus Torvalds 		goto out;
3561da177e4SLinus Torvalds 
3570ae051a1SDmitry Torokhov 	input_event(dev, EV_MSC, MSC_RAW, code);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (atkbd->translated) {
3601da177e4SLinus Torvalds 
3610ae051a1SDmitry Torokhov 		if (atkbd->emul || atkbd_need_xlate(atkbd->xl_bit, code)) {
3621da177e4SLinus Torvalds 			atkbd->release = code >> 7;
3631da177e4SLinus Torvalds 			code &= 0x7f;
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 
3660ae051a1SDmitry Torokhov 		if (!atkbd->emul)
3670ae051a1SDmitry Torokhov 			atkbd_calculate_xl_bit(atkbd, data);
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	switch (code) {
3711da177e4SLinus Torvalds 		case ATKBD_RET_BAT:
3721da177e4SLinus Torvalds 			atkbd->enabled = 0;
373dbc26344SDmitry Torokhov 			serio_reconnect(atkbd->ps2dev.serio);
3741da177e4SLinus Torvalds 			goto out;
3751da177e4SLinus Torvalds 		case ATKBD_RET_EMUL0:
3761da177e4SLinus Torvalds 			atkbd->emul = 1;
3771da177e4SLinus Torvalds 			goto out;
3781da177e4SLinus Torvalds 		case ATKBD_RET_EMUL1:
3791da177e4SLinus Torvalds 			atkbd->emul = 2;
3801da177e4SLinus Torvalds 			goto out;
3811da177e4SLinus Torvalds 		case ATKBD_RET_RELEASE:
3821da177e4SLinus Torvalds 			atkbd->release = 1;
3831da177e4SLinus Torvalds 			goto out;
3840ae051a1SDmitry Torokhov 		case ATKBD_RET_ACK:
3850ae051a1SDmitry Torokhov 		case ATKBD_RET_NAK:
3860ae051a1SDmitry Torokhov 			printk(KERN_WARNING "atkbd.c: Spurious %s on %s. "
3870ae051a1SDmitry Torokhov 			       "Some program might be trying access hardware directly.\n",
3880ae051a1SDmitry Torokhov 			       data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys);
3890ae051a1SDmitry Torokhov 			goto out;
390b9ab58ddSJerome Pinot 		case ATKBD_RET_HANGEUL:
3911da177e4SLinus Torvalds 		case ATKBD_RET_HANJA:
3920ae051a1SDmitry Torokhov 			/*
3930ae051a1SDmitry Torokhov 			 * These keys do not report release and thus need to be
3940ae051a1SDmitry Torokhov 			 * flagged properly
3950ae051a1SDmitry Torokhov 			 */
3960ae051a1SDmitry Torokhov 			add_release_event = 1;
3970ae051a1SDmitry Torokhov 			break;
3981da177e4SLinus Torvalds 		case ATKBD_RET_ERR:
3991da177e4SLinus Torvalds 			printk(KERN_DEBUG "atkbd.c: Keyboard on %s reports too many keys pressed.\n", serio->phys);
4001da177e4SLinus Torvalds 			goto out;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
4030ae051a1SDmitry Torokhov 	code = atkbd_compat_scancode(atkbd, code);
4040ae051a1SDmitry Torokhov 
4050ae051a1SDmitry Torokhov 	if (atkbd->emul && --atkbd->emul)
4061da177e4SLinus Torvalds 		goto out;
4071da177e4SLinus Torvalds 
4080ae051a1SDmitry Torokhov 	keycode = atkbd->keycode[code];
4091da177e4SLinus Torvalds 
4100ae051a1SDmitry Torokhov 	if (keycode != ATKBD_KEY_NULL)
4110ae051a1SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
4120ae051a1SDmitry Torokhov 
4130ae051a1SDmitry Torokhov 	switch (keycode) {
4141da177e4SLinus Torvalds 		case ATKBD_KEY_NULL:
4151da177e4SLinus Torvalds 			break;
4161da177e4SLinus Torvalds 		case ATKBD_KEY_UNKNOWN:
4170ae051a1SDmitry Torokhov 			printk(KERN_WARNING
4180ae051a1SDmitry Torokhov 			       "atkbd.c: Unknown key %s (%s set %d, code %#x on %s).\n",
4191da177e4SLinus Torvalds 			       atkbd->release ? "released" : "pressed",
4201da177e4SLinus Torvalds 			       atkbd->translated ? "translated" : "raw",
4211da177e4SLinus Torvalds 			       atkbd->set, code, serio->phys);
4220ae051a1SDmitry Torokhov 			printk(KERN_WARNING
4230ae051a1SDmitry Torokhov 			       "atkbd.c: Use 'setkeycodes %s%02x <keycode>' to make it known.\n",
4241da177e4SLinus Torvalds 			       code & 0x80 ? "e0" : "", code & 0x7f);
4250ae051a1SDmitry Torokhov 			input_sync(dev);
4261da177e4SLinus Torvalds 			break;
4271da177e4SLinus Torvalds 		case ATKBD_SCR_1:
4281da177e4SLinus Torvalds 			scroll = 1 - atkbd->release * 2;
4291da177e4SLinus Torvalds 			break;
4301da177e4SLinus Torvalds 		case ATKBD_SCR_2:
4311da177e4SLinus Torvalds 			scroll = 2 - atkbd->release * 4;
4321da177e4SLinus Torvalds 			break;
4331da177e4SLinus Torvalds 		case ATKBD_SCR_4:
4341da177e4SLinus Torvalds 			scroll = 4 - atkbd->release * 8;
4351da177e4SLinus Torvalds 			break;
4361da177e4SLinus Torvalds 		case ATKBD_SCR_8:
4371da177e4SLinus Torvalds 			scroll = 8 - atkbd->release * 16;
4381da177e4SLinus Torvalds 			break;
4391da177e4SLinus Torvalds 		case ATKBD_SCR_CLICK:
4401da177e4SLinus Torvalds 			click = !atkbd->release;
4411da177e4SLinus Torvalds 			break;
4421da177e4SLinus Torvalds 		case ATKBD_SCR_LEFT:
4431da177e4SLinus Torvalds 			hscroll = -1;
4441da177e4SLinus Torvalds 			break;
4451da177e4SLinus Torvalds 		case ATKBD_SCR_RIGHT:
4461da177e4SLinus Torvalds 			hscroll = 1;
4471da177e4SLinus Torvalds 			break;
4481da177e4SLinus Torvalds 		default:
4490ae051a1SDmitry Torokhov 			if (atkbd->release) {
4500ae051a1SDmitry Torokhov 				value = 0;
4511da177e4SLinus Torvalds 				atkbd->last = 0;
4520ae051a1SDmitry Torokhov 			} else if (!atkbd->softrepeat && test_bit(keycode, dev->key)) {
4530ae051a1SDmitry Torokhov 				/* Workaround Toshiba laptop multiple keypress */
4540ae051a1SDmitry Torokhov 				value = time_before(jiffies, atkbd->time) && atkbd->last == code ? 1 : 2;
4550ae051a1SDmitry Torokhov 			} else {
4561da177e4SLinus Torvalds 				value = 1;
4570ae051a1SDmitry Torokhov 				atkbd->last = code;
4580ae051a1SDmitry Torokhov 				atkbd->time = jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]) / 2;
4591da177e4SLinus Torvalds 			}
4601da177e4SLinus Torvalds 
4610ae051a1SDmitry Torokhov 			input_regs(dev, regs);
462f8b4c46cSDmitry Torokhov 			input_event(dev, EV_KEY, keycode, value);
4630ae051a1SDmitry Torokhov 			input_sync(dev);
4640ae051a1SDmitry Torokhov 
4650ae051a1SDmitry Torokhov 			if (value && add_release_event) {
4660ae051a1SDmitry Torokhov 				input_report_key(dev, keycode, 0);
4670ae051a1SDmitry Torokhov 				input_sync(dev);
4680ae051a1SDmitry Torokhov 			}
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (atkbd->scroll) {
4720ae051a1SDmitry Torokhov 		input_regs(dev, regs);
4731da177e4SLinus Torvalds 		if (click != -1)
4740ae051a1SDmitry Torokhov 			input_report_key(dev, BTN_MIDDLE, click);
4750ae051a1SDmitry Torokhov 		input_report_rel(dev, REL_WHEEL, scroll);
4760ae051a1SDmitry Torokhov 		input_report_rel(dev, REL_HWHEEL, hscroll);
4770ae051a1SDmitry Torokhov 		input_sync(dev);
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	atkbd->release = 0;
4811da177e4SLinus Torvalds out:
4821da177e4SLinus Torvalds 	return IRQ_HANDLED;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
485*3d0f0fa0SDmitry Torokhov static int atkbd_set_repeat_rate(struct atkbd *atkbd)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	const short period[32] =
4881da177e4SLinus Torvalds 		{ 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,  83,  92, 100, 109, 116, 125,
4891da177e4SLinus Torvalds 		 133, 149, 167, 182, 200, 217, 232, 250, 270, 303, 333, 370, 400, 435, 470, 500 };
4901da177e4SLinus Torvalds 	const short delay[4] =
4911da177e4SLinus Torvalds 		{ 250, 500, 750, 1000 };
4920d4c8597SDmitry Torokhov 
493*3d0f0fa0SDmitry Torokhov 	struct input_dev *dev = atkbd->dev;
494*3d0f0fa0SDmitry Torokhov 	unsigned char param;
495*3d0f0fa0SDmitry Torokhov 	int i = 0, j = 0;
496*3d0f0fa0SDmitry Torokhov 
497*3d0f0fa0SDmitry Torokhov 	while (i < ARRAY_SIZE(period) - 1 && period[i] < dev->rep[REP_PERIOD])
498*3d0f0fa0SDmitry Torokhov 		i++;
499*3d0f0fa0SDmitry Torokhov 	dev->rep[REP_PERIOD] = period[i];
500*3d0f0fa0SDmitry Torokhov 
501*3d0f0fa0SDmitry Torokhov 	while (j < ARRAY_SIZE(period) - 1 && delay[j] < dev->rep[REP_DELAY])
502*3d0f0fa0SDmitry Torokhov 		j++;
503*3d0f0fa0SDmitry Torokhov 	dev->rep[REP_DELAY] = delay[j];
504*3d0f0fa0SDmitry Torokhov 
505*3d0f0fa0SDmitry Torokhov 	param = i | (j << 5);
506*3d0f0fa0SDmitry Torokhov 	return ps2_command(&atkbd->ps2dev, &param, ATKBD_CMD_SETREP);
507*3d0f0fa0SDmitry Torokhov }
508*3d0f0fa0SDmitry Torokhov 
509*3d0f0fa0SDmitry Torokhov static int atkbd_set_leds(struct atkbd *atkbd)
510*3d0f0fa0SDmitry Torokhov {
5110d4c8597SDmitry Torokhov 	struct input_dev *dev = atkbd->dev;
5121da177e4SLinus Torvalds 	unsigned char param[2];
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)
5151da177e4SLinus Torvalds 		 | (test_bit(LED_NUML,    dev->led) ? 2 : 0)
5161da177e4SLinus Torvalds 		 | (test_bit(LED_CAPSL,   dev->led) ? 4 : 0);
517*3d0f0fa0SDmitry Torokhov 	if (ps2_command(&atkbd->ps2dev, param, ATKBD_CMD_SETLEDS))
518*3d0f0fa0SDmitry Torokhov 		return -1;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	if (atkbd->extra) {
5211da177e4SLinus Torvalds 		param[0] = 0;
5221da177e4SLinus Torvalds 		param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0)
5231da177e4SLinus Torvalds 			 | (test_bit(LED_SLEEP,   dev->led) ? 0x02 : 0)
5241da177e4SLinus Torvalds 			 | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0)
5251da177e4SLinus Torvalds 			 | (test_bit(LED_MISC,    dev->led) ? 0x10 : 0)
5261da177e4SLinus Torvalds 			 | (test_bit(LED_MUTE,    dev->led) ? 0x20 : 0);
527*3d0f0fa0SDmitry Torokhov 		if (ps2_command(&atkbd->ps2dev, param, ATKBD_CMD_EX_SETLEDS))
528*3d0f0fa0SDmitry Torokhov 			return -1;
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
531*3d0f0fa0SDmitry Torokhov 	return 0;
5320d4c8597SDmitry Torokhov }
5330d4c8597SDmitry Torokhov 
534*3d0f0fa0SDmitry Torokhov /*
535*3d0f0fa0SDmitry Torokhov  * atkbd_event_work() is used to complete processing of events that
536*3d0f0fa0SDmitry Torokhov  * can not be processed by input_event() which is often called from
537*3d0f0fa0SDmitry Torokhov  * interrupt context.
538*3d0f0fa0SDmitry Torokhov  */
539*3d0f0fa0SDmitry Torokhov 
540*3d0f0fa0SDmitry Torokhov static void atkbd_event_work(void *data)
541*3d0f0fa0SDmitry Torokhov {
542*3d0f0fa0SDmitry Torokhov 	struct atkbd *atkbd = data;
543*3d0f0fa0SDmitry Torokhov 
544*3d0f0fa0SDmitry Torokhov 	mutex_lock(&atkbd->event_mutex);
545*3d0f0fa0SDmitry Torokhov 
546*3d0f0fa0SDmitry Torokhov 	if (test_and_clear_bit(ATKBD_LED_EVENT_BIT, &atkbd->event_mask))
547*3d0f0fa0SDmitry Torokhov 		atkbd_set_leds(atkbd);
548*3d0f0fa0SDmitry Torokhov 
549*3d0f0fa0SDmitry Torokhov 	if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask))
550*3d0f0fa0SDmitry Torokhov 		atkbd_set_repeat_rate(atkbd);
551*3d0f0fa0SDmitry Torokhov 
55233d3f07aSIngo Molnar 	mutex_unlock(&atkbd->event_mutex);
5530d4c8597SDmitry Torokhov }
5540d4c8597SDmitry Torokhov 
5550d4c8597SDmitry Torokhov /*
5560d4c8597SDmitry Torokhov  * Event callback from the input module. Events that change the state of
5570d4c8597SDmitry Torokhov  * the hardware are processed here. If action can not be performed in
5580d4c8597SDmitry Torokhov  * interrupt context it is offloaded to atkbd_event_work.
5590d4c8597SDmitry Torokhov  */
5600d4c8597SDmitry Torokhov 
5610d4c8597SDmitry Torokhov static int atkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
5620d4c8597SDmitry Torokhov {
5630d4c8597SDmitry Torokhov 	struct atkbd *atkbd = dev->private;
5640d4c8597SDmitry Torokhov 
5650d4c8597SDmitry Torokhov 	if (!atkbd->write)
5660d4c8597SDmitry Torokhov 		return -1;
5670d4c8597SDmitry Torokhov 
5680d4c8597SDmitry Torokhov 	switch (type) {
5690d4c8597SDmitry Torokhov 
5700d4c8597SDmitry Torokhov 		case EV_LED:
5710d4c8597SDmitry Torokhov 			set_bit(ATKBD_LED_EVENT_BIT, &atkbd->event_mask);
5720d4c8597SDmitry Torokhov 			wmb();
5730d4c8597SDmitry Torokhov 			schedule_work(&atkbd->event_work);
5740d4c8597SDmitry Torokhov 			return 0;
5750d4c8597SDmitry Torokhov 
5760d4c8597SDmitry Torokhov 		case EV_REP:
5770d4c8597SDmitry Torokhov 
5780d4c8597SDmitry Torokhov 			if (!atkbd->softrepeat) {
5790d4c8597SDmitry Torokhov 				set_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask);
5800d4c8597SDmitry Torokhov 				wmb();
5810d4c8597SDmitry Torokhov 				schedule_work(&atkbd->event_work);
5820d4c8597SDmitry Torokhov 			}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 			return 0;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	return -1;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /*
5911da177e4SLinus Torvalds  * atkbd_enable() signals that interrupt handler is allowed to
5921da177e4SLinus Torvalds  * generate input events.
5931da177e4SLinus Torvalds  */
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds static inline void atkbd_enable(struct atkbd *atkbd)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	serio_pause_rx(atkbd->ps2dev.serio);
5981da177e4SLinus Torvalds 	atkbd->enabled = 1;
5991da177e4SLinus Torvalds 	serio_continue_rx(atkbd->ps2dev.serio);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds /*
6031da177e4SLinus Torvalds  * atkbd_disable() tells input handler that all incoming data except
6041da177e4SLinus Torvalds  * for ACKs and command response should be dropped.
6051da177e4SLinus Torvalds  */
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds static inline void atkbd_disable(struct atkbd *atkbd)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	serio_pause_rx(atkbd->ps2dev.serio);
6101da177e4SLinus Torvalds 	atkbd->enabled = 0;
6111da177e4SLinus Torvalds 	serio_continue_rx(atkbd->ps2dev.serio);
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /*
6151da177e4SLinus Torvalds  * atkbd_probe() probes for an AT keyboard on a serio port.
6161da177e4SLinus Torvalds  */
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds static int atkbd_probe(struct atkbd *atkbd)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &atkbd->ps2dev;
6211da177e4SLinus Torvalds 	unsigned char param[2];
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds /*
6241da177e4SLinus Torvalds  * Some systems, where the bit-twiddling when testing the io-lines of the
6251da177e4SLinus Torvalds  * controller may confuse the keyboard need a full reset of the keyboard. On
6261da177e4SLinus Torvalds  * these systems the BIOS also usually doesn't do it for us.
6271da177e4SLinus Torvalds  */
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	if (atkbd_reset)
6301da177e4SLinus Torvalds 		if (ps2_command(ps2dev, NULL, ATKBD_CMD_RESET_BAT))
6311da177e4SLinus Torvalds 			printk(KERN_WARNING "atkbd.c: keyboard reset failed on %s\n", ps2dev->serio->phys);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /*
6341da177e4SLinus Torvalds  * Then we check the keyboard ID. We should get 0xab83 under normal conditions.
6351da177e4SLinus Torvalds  * Some keyboards report different values, but the first byte is always 0xab or
6361da177e4SLinus Torvalds  * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this
6371da177e4SLinus Torvalds  * should make sure we don't try to set the LEDs on it.
6381da177e4SLinus Torvalds  */
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	param[0] = param[1] = 0xa5;	/* initialize with invalid values */
6411da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, ATKBD_CMD_GETID)) {
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /*
6441da177e4SLinus Torvalds  * If the get ID command failed, we check if we can at least set the LEDs on
6451da177e4SLinus Torvalds  * the keyboard. This should work on every keyboard out there. It also turns
6461da177e4SLinus Torvalds  * the LEDs off, which we want anyway.
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds 		param[0] = 0;
6491da177e4SLinus Torvalds 		if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS))
6501da177e4SLinus Torvalds 			return -1;
6511da177e4SLinus Torvalds 		atkbd->id = 0xabba;
6521da177e4SLinus Torvalds 		return 0;
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	if (param[0] != 0xab && param[0] != 0xac &&	/* Regular and NCD Sun keyboards */
6561da177e4SLinus Torvalds 	    param[0] != 0x2b && param[0] != 0x5d &&	/* Trust keyboard, raw and translated */
6571da177e4SLinus Torvalds 	    param[0] != 0x60 && param[0] != 0x47)	/* NMB SGI keyboard, raw and translated */
6581da177e4SLinus Torvalds 		return -1;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	atkbd->id = (param[0] << 8) | param[1];
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	if (atkbd->id == 0xaca1 && atkbd->translated) {
6631da177e4SLinus Torvalds 		printk(KERN_ERR "atkbd.c: NCD terminal keyboards are only supported on non-translating\n");
6641da177e4SLinus Torvalds 		printk(KERN_ERR "atkbd.c: controllers. Use i8042.direct=1 to disable translation.\n");
6651da177e4SLinus Torvalds 		return -1;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds /*
6721da177e4SLinus Torvalds  * atkbd_select_set checks if a keyboard has a working Set 3 support, and
6731da177e4SLinus Torvalds  * sets it into that. Unfortunately there are keyboards that can be switched
6741da177e4SLinus Torvalds  * to Set 3, but don't work well in that (BTC Multimedia ...)
6751da177e4SLinus Torvalds  */
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds static int atkbd_select_set(struct atkbd *atkbd, int target_set, int allow_extra)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &atkbd->ps2dev;
6801da177e4SLinus Torvalds 	unsigned char param[2];
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	atkbd->extra = 0;
6831da177e4SLinus Torvalds /*
6841da177e4SLinus Torvalds  * For known special keyboards we can go ahead and set the correct set.
6851da177e4SLinus Torvalds  * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and
6861da177e4SLinus Torvalds  * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards.
6871da177e4SLinus Torvalds  */
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	if (atkbd->translated)
6901da177e4SLinus Torvalds 		return 2;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	if (atkbd->id == 0xaca1) {
6931da177e4SLinus Torvalds 		param[0] = 3;
6941da177e4SLinus Torvalds 		ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET);
6951da177e4SLinus Torvalds 		return 3;
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (allow_extra) {
6991da177e4SLinus Torvalds 		param[0] = 0x71;
7001da177e4SLinus Torvalds 		if (!ps2_command(ps2dev, param, ATKBD_CMD_EX_ENABLE)) {
7011da177e4SLinus Torvalds 			atkbd->extra = 1;
7021da177e4SLinus Torvalds 			return 2;
7031da177e4SLinus Torvalds 		}
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (target_set != 3)
7071da177e4SLinus Torvalds 		return 2;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	if (!ps2_command(ps2dev, param, ATKBD_CMD_OK_GETID)) {
7101da177e4SLinus Torvalds 		atkbd->id = param[0] << 8 | param[1];
7111da177e4SLinus Torvalds 		return 2;
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	param[0] = 3;
7151da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET))
7161da177e4SLinus Torvalds 		return 2;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	param[0] = 0;
7191da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, ATKBD_CMD_GSCANSET))
7201da177e4SLinus Torvalds 		return 2;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	if (param[0] != 3) {
7231da177e4SLinus Torvalds 		param[0] = 2;
7241da177e4SLinus Torvalds 		if (ps2_command(ps2dev, param, ATKBD_CMD_SSCANSET))
7251da177e4SLinus Torvalds 		return 2;
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	ps2_command(ps2dev, param, ATKBD_CMD_SETALL_MBR);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	return 3;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds static int atkbd_activate(struct atkbd *atkbd)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	struct ps2dev *ps2dev = &atkbd->ps2dev;
7361da177e4SLinus Torvalds 	unsigned char param[1];
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /*
7391da177e4SLinus Torvalds  * Set the LEDs to a defined state.
7401da177e4SLinus Torvalds  */
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	param[0] = 0;
7431da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, ATKBD_CMD_SETLEDS))
7441da177e4SLinus Torvalds 		return -1;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /*
7471da177e4SLinus Torvalds  * Set autorepeat to fastest possible.
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	param[0] = 0;
7511da177e4SLinus Torvalds 	if (ps2_command(ps2dev, param, ATKBD_CMD_SETREP))
7521da177e4SLinus Torvalds 		return -1;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /*
7551da177e4SLinus Torvalds  * Enable the keyboard to receive keystrokes.
7561da177e4SLinus Torvalds  */
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	if (ps2_command(ps2dev, NULL, ATKBD_CMD_ENABLE)) {
7591da177e4SLinus Torvalds 		printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n",
7601da177e4SLinus Torvalds 			ps2dev->serio->phys);
7611da177e4SLinus Torvalds 		return -1;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	return 0;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds /*
7681da177e4SLinus Torvalds  * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a
7691da177e4SLinus Torvalds  * reboot.
7701da177e4SLinus Torvalds  */
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds static void atkbd_cleanup(struct serio *serio)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	struct atkbd *atkbd = serio_get_drvdata(serio);
7751da177e4SLinus Torvalds 	ps2_command(&atkbd->ps2dev, NULL, ATKBD_CMD_RESET_BAT);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds /*
7801da177e4SLinus Torvalds  * atkbd_disconnect() closes and frees.
7811da177e4SLinus Torvalds  */
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds static void atkbd_disconnect(struct serio *serio)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct atkbd *atkbd = serio_get_drvdata(serio);
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	atkbd_disable(atkbd);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	/* make sure we don't have a command in flight */
790fbd568a3SPaul E. McKenney 	synchronize_sched();  /* Allow atkbd_interrupt()s to complete. */
7911da177e4SLinus Torvalds 	flush_scheduled_work();
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	device_remove_file(&serio->dev, &atkbd_attr_extra);
7941da177e4SLinus Torvalds 	device_remove_file(&serio->dev, &atkbd_attr_scroll);
7951da177e4SLinus Torvalds 	device_remove_file(&serio->dev, &atkbd_attr_set);
7961da177e4SLinus Torvalds 	device_remove_file(&serio->dev, &atkbd_attr_softrepeat);
7971da177e4SLinus Torvalds 	device_remove_file(&serio->dev, &atkbd_attr_softraw);
7981da177e4SLinus Torvalds 
7993c42f0c3SDmitry Torokhov 	input_unregister_device(atkbd->dev);
8001da177e4SLinus Torvalds 	serio_close(serio);
8011da177e4SLinus Torvalds 	serio_set_drvdata(serio, NULL);
8021da177e4SLinus Torvalds 	kfree(atkbd);
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /*
8073c42f0c3SDmitry Torokhov  * atkbd_set_keycode_table() initializes keyboard's keycode table
8081da177e4SLinus Torvalds  * according to the selected scancode set
8091da177e4SLinus Torvalds  */
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds static void atkbd_set_keycode_table(struct atkbd *atkbd)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	int i, j;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	memset(atkbd->keycode, 0, sizeof(atkbd->keycode));
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	if (atkbd->translated) {
8181da177e4SLinus Torvalds 		for (i = 0; i < 128; i++) {
8191da177e4SLinus Torvalds 			atkbd->keycode[i] = atkbd_set2_keycode[atkbd_unxlate_table[i]];
8201da177e4SLinus Torvalds 			atkbd->keycode[i | 0x80] = atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80];
8211da177e4SLinus Torvalds 			if (atkbd->scroll)
8221da177e4SLinus Torvalds 				for (j = 0; j < ARRAY_SIZE(atkbd_scroll_keys); j++)
8231da177e4SLinus Torvalds 					if ((atkbd_unxlate_table[i] | 0x80) == atkbd_scroll_keys[j].set2)
8241da177e4SLinus Torvalds 						atkbd->keycode[i | 0x80] = atkbd_scroll_keys[j].keycode;
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 	} else if (atkbd->set == 3) {
8271da177e4SLinus Torvalds 		memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));
8281da177e4SLinus Torvalds 	} else {
8291da177e4SLinus Torvalds 		memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 		if (atkbd->scroll)
8321da177e4SLinus Torvalds 			for (i = 0; i < ARRAY_SIZE(atkbd_scroll_keys); i++)
8331da177e4SLinus Torvalds 				atkbd->keycode[atkbd_scroll_keys[i].set2] = atkbd_scroll_keys[i].keycode;
8341da177e4SLinus Torvalds 	}
8350ae051a1SDmitry Torokhov 
8360ae051a1SDmitry Torokhov 	atkbd->keycode[atkbd_compat_scancode(atkbd, ATKBD_RET_HANGEUL)] = KEY_HANGUEL;
8370ae051a1SDmitry Torokhov 	atkbd->keycode[atkbd_compat_scancode(atkbd, ATKBD_RET_HANJA)] = KEY_HANJA;
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds /*
8411da177e4SLinus Torvalds  * atkbd_set_device_attrs() sets up keyboard's input device structure
8421da177e4SLinus Torvalds  */
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds static void atkbd_set_device_attrs(struct atkbd *atkbd)
8451da177e4SLinus Torvalds {
8463c42f0c3SDmitry Torokhov 	struct input_dev *input_dev = atkbd->dev;
8471da177e4SLinus Torvalds 	int i;
8481da177e4SLinus Torvalds 
8493c42f0c3SDmitry Torokhov 	if (atkbd->extra)
850ea08c6faSDmitry Torokhov 		snprintf(atkbd->name, sizeof(atkbd->name),
851ea08c6faSDmitry Torokhov 			 "AT Set 2 Extra keyboard");
8523c42f0c3SDmitry Torokhov 	else
853ea08c6faSDmitry Torokhov 		snprintf(atkbd->name, sizeof(atkbd->name),
854ea08c6faSDmitry Torokhov 			 "AT %s Set %d keyboard",
8553c42f0c3SDmitry Torokhov 			 atkbd->translated ? "Translated" : "Raw", atkbd->set);
8561da177e4SLinus Torvalds 
857ea08c6faSDmitry Torokhov 	snprintf(atkbd->phys, sizeof(atkbd->phys),
858ea08c6faSDmitry Torokhov 		 "%s/input0", atkbd->ps2dev.serio->phys);
8591da177e4SLinus Torvalds 
8603c42f0c3SDmitry Torokhov 	input_dev->name = atkbd->name;
8613c42f0c3SDmitry Torokhov 	input_dev->phys = atkbd->phys;
8623c42f0c3SDmitry Torokhov 	input_dev->id.bustype = BUS_I8042;
8633c42f0c3SDmitry Torokhov 	input_dev->id.vendor = 0x0001;
8643c42f0c3SDmitry Torokhov 	input_dev->id.product = atkbd->translated ? 1 : atkbd->set;
8653c42f0c3SDmitry Torokhov 	input_dev->id.version = atkbd->id;
8663c42f0c3SDmitry Torokhov 	input_dev->event = atkbd_event;
8673c42f0c3SDmitry Torokhov 	input_dev->private = atkbd;
8683c42f0c3SDmitry Torokhov 	input_dev->cdev.dev = &atkbd->ps2dev.serio->dev;
8691da177e4SLinus Torvalds 
8703c42f0c3SDmitry Torokhov 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_MSC);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	if (atkbd->write) {
8733c42f0c3SDmitry Torokhov 		input_dev->evbit[0] |= BIT(EV_LED);
8743c42f0c3SDmitry Torokhov 		input_dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
8751da177e4SLinus Torvalds 	}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	if (atkbd->extra)
8783c42f0c3SDmitry Torokhov 		input_dev->ledbit[0] |= BIT(LED_COMPOSE) | BIT(LED_SUSPEND) |
8791da177e4SLinus Torvalds 					BIT(LED_SLEEP) | BIT(LED_MUTE) | BIT(LED_MISC);
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (!atkbd->softrepeat) {
8823c42f0c3SDmitry Torokhov 		input_dev->rep[REP_DELAY] = 250;
8833c42f0c3SDmitry Torokhov 		input_dev->rep[REP_PERIOD] = 33;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
8863c42f0c3SDmitry Torokhov 	input_dev->mscbit[0] = atkbd->softraw ? BIT(MSC_SCAN) : BIT(MSC_RAW) | BIT(MSC_SCAN);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (atkbd->scroll) {
8893c42f0c3SDmitry Torokhov 		input_dev->evbit[0] |= BIT(EV_REL);
8903c42f0c3SDmitry Torokhov 		input_dev->relbit[0] = BIT(REL_WHEEL) | BIT(REL_HWHEEL);
8913c42f0c3SDmitry Torokhov 		set_bit(BTN_MIDDLE, input_dev->keybit);
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8943c42f0c3SDmitry Torokhov 	input_dev->keycode = atkbd->keycode;
8953c42f0c3SDmitry Torokhov 	input_dev->keycodesize = sizeof(unsigned char);
8963c42f0c3SDmitry Torokhov 	input_dev->keycodemax = ARRAY_SIZE(atkbd_set2_keycode);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	for (i = 0; i < 512; i++)
8991da177e4SLinus Torvalds 		if (atkbd->keycode[i] && atkbd->keycode[i] < ATKBD_SPECIAL)
9003c42f0c3SDmitry Torokhov 			set_bit(atkbd->keycode[i], input_dev->keybit);
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /*
9041da177e4SLinus Torvalds  * atkbd_connect() is called when the serio module finds an interface
9051da177e4SLinus Torvalds  * that isn't handled yet by an appropriate device driver. We check if
9061da177e4SLinus Torvalds  * there is an AT keyboard out there and if yes, we register ourselves
9071da177e4SLinus Torvalds  * to the input module.
9081da177e4SLinus Torvalds  */
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds static int atkbd_connect(struct serio *serio, struct serio_driver *drv)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	struct atkbd *atkbd;
9133c42f0c3SDmitry Torokhov 	struct input_dev *dev;
9143c42f0c3SDmitry Torokhov 	int err = -ENOMEM;
9151da177e4SLinus Torvalds 
9163c42f0c3SDmitry Torokhov 	atkbd = kzalloc(sizeof(struct atkbd), GFP_KERNEL);
9173c42f0c3SDmitry Torokhov 	dev = input_allocate_device();
9183c42f0c3SDmitry Torokhov 	if (!atkbd || !dev)
9193c42f0c3SDmitry Torokhov 		goto fail;
9201da177e4SLinus Torvalds 
9213c42f0c3SDmitry Torokhov 	atkbd->dev = dev;
9221da177e4SLinus Torvalds 	ps2_init(&atkbd->ps2dev, serio);
9230d4c8597SDmitry Torokhov 	INIT_WORK(&atkbd->event_work, atkbd_event_work, atkbd);
92433d3f07aSIngo Molnar 	mutex_init(&atkbd->event_mutex);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	switch (serio->id.type) {
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 		case SERIO_8042_XL:
9291da177e4SLinus Torvalds 			atkbd->translated = 1;
9301da177e4SLinus Torvalds 		case SERIO_8042:
9311da177e4SLinus Torvalds 			if (serio->write)
9321da177e4SLinus Torvalds 				atkbd->write = 1;
9331da177e4SLinus Torvalds 			break;
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	atkbd->softraw = atkbd_softraw;
9371da177e4SLinus Torvalds 	atkbd->softrepeat = atkbd_softrepeat;
9381da177e4SLinus Torvalds 	atkbd->scroll = atkbd_scroll;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	if (atkbd->softrepeat)
9411da177e4SLinus Torvalds 		atkbd->softraw = 1;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	serio_set_drvdata(serio, atkbd);
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	err = serio_open(serio, drv);
9463c42f0c3SDmitry Torokhov 	if (err)
9473c42f0c3SDmitry Torokhov 		goto fail;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (atkbd->write) {
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 		if (atkbd_probe(atkbd)) {
9521da177e4SLinus Torvalds 			serio_close(serio);
9533c42f0c3SDmitry Torokhov 			err = -ENODEV;
9543c42f0c3SDmitry Torokhov 			goto fail;
9551da177e4SLinus Torvalds 		}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 		atkbd->set = atkbd_select_set(atkbd, atkbd_set, atkbd_extra);
9581da177e4SLinus Torvalds 		atkbd_activate(atkbd);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	} else {
9611da177e4SLinus Torvalds 		atkbd->set = 2;
9621da177e4SLinus Torvalds 		atkbd->id = 0xab00;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	atkbd_set_keycode_table(atkbd);
9661da177e4SLinus Torvalds 	atkbd_set_device_attrs(atkbd);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	device_create_file(&serio->dev, &atkbd_attr_extra);
9691da177e4SLinus Torvalds 	device_create_file(&serio->dev, &atkbd_attr_scroll);
9701da177e4SLinus Torvalds 	device_create_file(&serio->dev, &atkbd_attr_set);
9711da177e4SLinus Torvalds 	device_create_file(&serio->dev, &atkbd_attr_softrepeat);
9721da177e4SLinus Torvalds 	device_create_file(&serio->dev, &atkbd_attr_softraw);
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	atkbd_enable(atkbd);
9751da177e4SLinus Torvalds 
9763c42f0c3SDmitry Torokhov 	input_register_device(atkbd->dev);
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	return 0;
9793c42f0c3SDmitry Torokhov 
9803c42f0c3SDmitry Torokhov  fail:	serio_set_drvdata(serio, NULL);
9813c42f0c3SDmitry Torokhov 	input_free_device(dev);
9823c42f0c3SDmitry Torokhov 	kfree(atkbd);
9833c42f0c3SDmitry Torokhov 	return err;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds /*
9871da177e4SLinus Torvalds  * atkbd_reconnect() tries to restore keyboard into a sane state and is
9881da177e4SLinus Torvalds  * most likely called on resume.
9891da177e4SLinus Torvalds  */
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static int atkbd_reconnect(struct serio *serio)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	struct atkbd *atkbd = serio_get_drvdata(serio);
9941da177e4SLinus Torvalds 	struct serio_driver *drv = serio->drv;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (!atkbd || !drv) {
9971da177e4SLinus Torvalds 		printk(KERN_DEBUG "atkbd: reconnect request, but serio is disconnected, ignoring...\n");
9981da177e4SLinus Torvalds 		return -1;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	atkbd_disable(atkbd);
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	if (atkbd->write) {
10041da177e4SLinus Torvalds 		if (atkbd_probe(atkbd))
10051da177e4SLinus Torvalds 			return -1;
10061da177e4SLinus Torvalds 		if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra))
10071da177e4SLinus Torvalds 			return -1;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 		atkbd_activate(atkbd);
10101da177e4SLinus Torvalds 
1011*3d0f0fa0SDmitry Torokhov /*
1012*3d0f0fa0SDmitry Torokhov  * Restore repeat rate and LEDs (that were reset by atkbd_activate)
1013*3d0f0fa0SDmitry Torokhov  * to pre-resume state
1014*3d0f0fa0SDmitry Torokhov  */
1015*3d0f0fa0SDmitry Torokhov 		if (!atkbd->softrepeat)
1016*3d0f0fa0SDmitry Torokhov 			atkbd_set_repeat_rate(atkbd);
1017*3d0f0fa0SDmitry Torokhov 		atkbd_set_leds(atkbd);
10181da177e4SLinus Torvalds 	}
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	atkbd_enable(atkbd);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	return 0;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds static struct serio_device_id atkbd_serio_ids[] = {
10261da177e4SLinus Torvalds 	{
10271da177e4SLinus Torvalds 		.type	= SERIO_8042,
10281da177e4SLinus Torvalds 		.proto	= SERIO_ANY,
10291da177e4SLinus Torvalds 		.id	= SERIO_ANY,
10301da177e4SLinus Torvalds 		.extra	= SERIO_ANY,
10311da177e4SLinus Torvalds 	},
10321da177e4SLinus Torvalds 	{
10331da177e4SLinus Torvalds 		.type	= SERIO_8042_XL,
10341da177e4SLinus Torvalds 		.proto	= SERIO_ANY,
10351da177e4SLinus Torvalds 		.id	= SERIO_ANY,
10361da177e4SLinus Torvalds 		.extra	= SERIO_ANY,
10371da177e4SLinus Torvalds 	},
10381da177e4SLinus Torvalds 	{
10391da177e4SLinus Torvalds 		.type	= SERIO_RS232,
10401da177e4SLinus Torvalds 		.proto	= SERIO_PS2SER,
10411da177e4SLinus Torvalds 		.id	= SERIO_ANY,
10421da177e4SLinus Torvalds 		.extra	= SERIO_ANY,
10431da177e4SLinus Torvalds 	},
10441da177e4SLinus Torvalds 	{ 0 }
10451da177e4SLinus Torvalds };
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, atkbd_serio_ids);
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds static struct serio_driver atkbd_drv = {
10501da177e4SLinus Torvalds 	.driver		= {
10511da177e4SLinus Torvalds 		.name	= "atkbd",
10521da177e4SLinus Torvalds 	},
10531da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
10541da177e4SLinus Torvalds 	.id_table	= atkbd_serio_ids,
10551da177e4SLinus Torvalds 	.interrupt	= atkbd_interrupt,
10561da177e4SLinus Torvalds 	.connect	= atkbd_connect,
10571da177e4SLinus Torvalds 	.reconnect	= atkbd_reconnect,
10581da177e4SLinus Torvalds 	.disconnect	= atkbd_disconnect,
10591da177e4SLinus Torvalds 	.cleanup	= atkbd_cleanup,
10601da177e4SLinus Torvalds };
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf,
10631da177e4SLinus Torvalds 				ssize_t (*handler)(struct atkbd *, char *))
10641da177e4SLinus Torvalds {
10651da177e4SLinus Torvalds 	struct serio *serio = to_serio_port(dev);
10661da177e4SLinus Torvalds 	int retval;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	retval = serio_pin_driver(serio);
10691da177e4SLinus Torvalds 	if (retval)
10701da177e4SLinus Torvalds 		return retval;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	if (serio->drv != &atkbd_drv) {
10731da177e4SLinus Torvalds 		retval = -ENODEV;
10741da177e4SLinus Torvalds 		goto out;
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	retval = handler((struct atkbd *)serio_get_drvdata(serio), buf);
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds out:
10801da177e4SLinus Torvalds 	serio_unpin_driver(serio);
10811da177e4SLinus Torvalds 	return retval;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count,
10851da177e4SLinus Torvalds 				ssize_t (*handler)(struct atkbd *, const char *, size_t))
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct serio *serio = to_serio_port(dev);
10881da177e4SLinus Torvalds 	struct atkbd *atkbd;
10891da177e4SLinus Torvalds 	int retval;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	retval = serio_pin_driver(serio);
10921da177e4SLinus Torvalds 	if (retval)
10931da177e4SLinus Torvalds 		return retval;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	if (serio->drv != &atkbd_drv) {
10961da177e4SLinus Torvalds 		retval = -ENODEV;
10971da177e4SLinus Torvalds 		goto out;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	atkbd = serio_get_drvdata(serio);
11011da177e4SLinus Torvalds 	atkbd_disable(atkbd);
11021da177e4SLinus Torvalds 	retval = handler(atkbd, buf, count);
11031da177e4SLinus Torvalds 	atkbd_enable(atkbd);
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds out:
11061da177e4SLinus Torvalds 	serio_unpin_driver(serio);
11071da177e4SLinus Torvalds 	return retval;
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", atkbd->extra ? 1 : 0);
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds static ssize_t atkbd_set_extra(struct atkbd *atkbd, const char *buf, size_t count)
11161da177e4SLinus Torvalds {
11173c42f0c3SDmitry Torokhov 	struct input_dev *new_dev;
11181da177e4SLinus Torvalds 	unsigned long value;
11191da177e4SLinus Torvalds 	char *rest;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	if (!atkbd->write)
11221da177e4SLinus Torvalds 		return -EIO;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	value = simple_strtoul(buf, &rest, 10);
11251da177e4SLinus Torvalds 	if (*rest || value > 1)
11261da177e4SLinus Torvalds 		return -EINVAL;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if (atkbd->extra != value) {
11293c42f0c3SDmitry Torokhov 		/*
11303c42f0c3SDmitry Torokhov 		 * Since device's properties will change we need to
11313c42f0c3SDmitry Torokhov 		 * unregister old device. But allocate new one first
11323c42f0c3SDmitry Torokhov 		 * to make sure we have it.
11333c42f0c3SDmitry Torokhov 		 */
11343c42f0c3SDmitry Torokhov 		if (!(new_dev = input_allocate_device()))
11353c42f0c3SDmitry Torokhov 			return -ENOMEM;
11363c42f0c3SDmitry Torokhov 		input_unregister_device(atkbd->dev);
11373c42f0c3SDmitry Torokhov 		atkbd->dev = new_dev;
11381da177e4SLinus Torvalds 		atkbd->set = atkbd_select_set(atkbd, atkbd->set, value);
11391da177e4SLinus Torvalds 		atkbd_activate(atkbd);
11401da177e4SLinus Torvalds 		atkbd_set_device_attrs(atkbd);
11413c42f0c3SDmitry Torokhov 		input_register_device(atkbd->dev);
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 	return count;
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds static ssize_t atkbd_show_scroll(struct atkbd *atkbd, char *buf)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", atkbd->scroll ? 1 : 0);
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds static ssize_t atkbd_set_scroll(struct atkbd *atkbd, const char *buf, size_t count)
11521da177e4SLinus Torvalds {
11533c42f0c3SDmitry Torokhov 	struct input_dev *new_dev;
11541da177e4SLinus Torvalds 	unsigned long value;
11551da177e4SLinus Torvalds 	char *rest;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	value = simple_strtoul(buf, &rest, 10);
11581da177e4SLinus Torvalds 	if (*rest || value > 1)
11591da177e4SLinus Torvalds 		return -EINVAL;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	if (atkbd->scroll != value) {
11623c42f0c3SDmitry Torokhov 		if (!(new_dev = input_allocate_device()))
11633c42f0c3SDmitry Torokhov 			return -ENOMEM;
11643c42f0c3SDmitry Torokhov 		input_unregister_device(atkbd->dev);
11653c42f0c3SDmitry Torokhov 		atkbd->dev = new_dev;
11661da177e4SLinus Torvalds 		atkbd->scroll = value;
11671da177e4SLinus Torvalds 		atkbd_set_keycode_table(atkbd);
11681da177e4SLinus Torvalds 		atkbd_set_device_attrs(atkbd);
11693c42f0c3SDmitry Torokhov 		input_register_device(atkbd->dev);
11701da177e4SLinus Torvalds 	}
11711da177e4SLinus Torvalds 	return count;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds static ssize_t atkbd_show_set(struct atkbd *atkbd, char *buf)
11751da177e4SLinus Torvalds {
11761da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", atkbd->set);
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds static ssize_t atkbd_set_set(struct atkbd *atkbd, const char *buf, size_t count)
11801da177e4SLinus Torvalds {
11813c42f0c3SDmitry Torokhov 	struct input_dev *new_dev;
11821da177e4SLinus Torvalds 	unsigned long value;
11831da177e4SLinus Torvalds 	char *rest;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	if (!atkbd->write)
11861da177e4SLinus Torvalds 		return -EIO;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	value = simple_strtoul(buf, &rest, 10);
11891da177e4SLinus Torvalds 	if (*rest || (value != 2 && value != 3))
11901da177e4SLinus Torvalds 		return -EINVAL;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	if (atkbd->set != value) {
11933c42f0c3SDmitry Torokhov 		if (!(new_dev = input_allocate_device()))
11943c42f0c3SDmitry Torokhov 			return -ENOMEM;
11953c42f0c3SDmitry Torokhov 		input_unregister_device(atkbd->dev);
11963c42f0c3SDmitry Torokhov 		atkbd->dev = new_dev;
11971da177e4SLinus Torvalds 		atkbd->set = atkbd_select_set(atkbd, value, atkbd->extra);
11981da177e4SLinus Torvalds 		atkbd_activate(atkbd);
11991da177e4SLinus Torvalds 		atkbd_set_keycode_table(atkbd);
12001da177e4SLinus Torvalds 		atkbd_set_device_attrs(atkbd);
12013c42f0c3SDmitry Torokhov 		input_register_device(atkbd->dev);
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 	return count;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds static ssize_t atkbd_show_softrepeat(struct atkbd *atkbd, char *buf)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", atkbd->softrepeat ? 1 : 0);
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds static ssize_t atkbd_set_softrepeat(struct atkbd *atkbd, const char *buf, size_t count)
12121da177e4SLinus Torvalds {
12133c42f0c3SDmitry Torokhov 	struct input_dev *new_dev;
12141da177e4SLinus Torvalds 	unsigned long value;
12151da177e4SLinus Torvalds 	char *rest;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	if (!atkbd->write)
12181da177e4SLinus Torvalds 		return -EIO;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	value = simple_strtoul(buf, &rest, 10);
12211da177e4SLinus Torvalds 	if (*rest || value > 1)
12221da177e4SLinus Torvalds 		return -EINVAL;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	if (atkbd->softrepeat != value) {
12253c42f0c3SDmitry Torokhov 		if (!(new_dev = input_allocate_device()))
12263c42f0c3SDmitry Torokhov 			return -ENOMEM;
12273c42f0c3SDmitry Torokhov 		input_unregister_device(atkbd->dev);
12283c42f0c3SDmitry Torokhov 		atkbd->dev = new_dev;
12291da177e4SLinus Torvalds 		atkbd->softrepeat = value;
12301da177e4SLinus Torvalds 		if (atkbd->softrepeat)
12311da177e4SLinus Torvalds 			atkbd->softraw = 1;
12321da177e4SLinus Torvalds 		atkbd_set_device_attrs(atkbd);
12333c42f0c3SDmitry Torokhov 		input_register_device(atkbd->dev);
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 	return count;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds static ssize_t atkbd_show_softraw(struct atkbd *atkbd, char *buf)
12401da177e4SLinus Torvalds {
12411da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", atkbd->softraw ? 1 : 0);
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds static ssize_t atkbd_set_softraw(struct atkbd *atkbd, const char *buf, size_t count)
12451da177e4SLinus Torvalds {
12463c42f0c3SDmitry Torokhov 	struct input_dev *new_dev;
12471da177e4SLinus Torvalds 	unsigned long value;
12481da177e4SLinus Torvalds 	char *rest;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	value = simple_strtoul(buf, &rest, 10);
12511da177e4SLinus Torvalds 	if (*rest || value > 1)
12521da177e4SLinus Torvalds 		return -EINVAL;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	if (atkbd->softraw != value) {
12553c42f0c3SDmitry Torokhov 		if (!(new_dev = input_allocate_device()))
12563c42f0c3SDmitry Torokhov 			return -ENOMEM;
12573c42f0c3SDmitry Torokhov 		input_unregister_device(atkbd->dev);
12583c42f0c3SDmitry Torokhov 		atkbd->dev = new_dev;
12591da177e4SLinus Torvalds 		atkbd->softraw = value;
12601da177e4SLinus Torvalds 		atkbd_set_device_attrs(atkbd);
12613c42f0c3SDmitry Torokhov 		input_register_device(atkbd->dev);
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 	return count;
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds static int __init atkbd_init(void)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	serio_register_driver(&atkbd_drv);
12701da177e4SLinus Torvalds 	return 0;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds static void __exit atkbd_exit(void)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	serio_unregister_driver(&atkbd_drv);
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds module_init(atkbd_init);
12791da177e4SLinus Torvalds module_exit(atkbd_exit);
1280