1 /*
2  * $Id: keybdev.c,v 1.3 2000/05/28 17:31:36 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2000 Vojtech Pavlik
5  *
6  *  Input driver to keyboard driver binding.
7  *
8  *  Sponsored by SuSE
9  */
10 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
28  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
29  */
30 
31 #include <linux/config.h>
32 #include <linux/kbd_ll.h>
33 #include <linux/input.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/tty.h>
37 #include <linux/module.h>
38 #include <linux/kbd_kern.h>
39 
40 #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(__alpha__) || \
41     defined(__mips__) || defined(CONFIG_SPARC64) || defined(CONFIG_SUPERH) || \
42     defined(CONFIG_PPC) || defined(__mc68000__) || defined(__hppa__) || \
43     defined(__arm__)
44 
45 static int x86_sysrq_alt = 0;
46 #ifdef CONFIG_SPARC64
47 static int sparc_l1_a_state = 0;
48 extern void batten_down_hatches(void);
49 #endif
50 
51 static int jp_kbd_109 = 1;	/* Yes, .jp is the default. See 51142. */
52 
53 static unsigned short x86_keycodes[256] =
54 	{ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
55 	 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
56 	 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
57 	 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
58 	 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
59 	 80, 81, 82, 83, 43, 85, 86, 87, 88,115,119,120,121,375,123, 90,
60 	284,285,309,298,312, 91,327,328,329,331,333,335,336,337,338,339,
61 	367,294,293,286,350, 92,334,512,116,377,109,111,373,347,348,349,
62 	360, 93, 94, 95, 98,376,100,101,357,316,354,304,289,102,351,355,
63 	103,104,105,275,281,272,306,106,274,107,288,364,358,363,362,361,
64 	291,108,381,290,287,292,279,305,280, 99,112,257,258,113,270,114,
65 	118,117,125,374,379,259,260,261,262,263,264,265,266,267,268,269,
66 	271,273,276,277,278,282,283,295,296,297,299,300,301,302,303,307,
67 	308,310,313,314,315,317,318,319,320,321,322,323,324,325,326,330,
68 	332,340,341,342,343,344,345,346,356,359,365,368,369,370,371,372 };
69 
70 #ifdef CONFIG_MAC_EMUMOUSEBTN
71 extern int mac_hid_mouse_emulate_buttons(int, int, int);
72 #endif /* CONFIG_MAC_EMUMOUSEBTN */
73 #ifdef CONFIG_MAC_ADBKEYCODES
74 extern int mac_hid_keyboard_sends_linux_keycodes(void);
75 #else
76 #define mac_hid_keyboard_sends_linux_keycodes()	0
77 #endif /* CONFIG_MAC_ADBKEYCODES */
78 #if defined(CONFIG_MAC_ADBKEYCODES) || defined(CONFIG_ADB_KEYBOARD)
79 static unsigned char mac_keycodes[256] = {
80 	  0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48,
81 	 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54,128,  1,
82 	  2,  3,  5,  4, 38, 40, 37, 41, 39, 50, 56, 42,  6,  7,  8,  9,
83 	 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96,
84 	 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83,
85 	 84, 85, 82, 65, 42,  0, 10,103,111,  0,  0,  0,  0,  0,  0,  0,
86 	 76,125, 75,105,124,110,115, 62,116, 59, 60,119, 61,121,114,117,
87 	  0,  0,  0,  0,127, 81,  0,113,  0,  0,  0,  0, 95, 55, 55,  0,
88 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
89 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
90 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
91 	  0,  0,  0,  0,  0, 94,  0, 93,  0,  0,  0,  0,  0,  0,104,102 };
92 #endif	/* CONFIG_MAC_ADBKEYCODES || CONFIG_ADB_KEYBOARD */
93 
emulate_raw(unsigned int keycode,int down)94 static int emulate_raw(unsigned int keycode, int down)
95 {
96 #ifdef CONFIG_MAC_EMUMOUSEBTN
97 	if (mac_hid_mouse_emulate_buttons(1, keycode, down))
98 		return 0;
99 #endif /* CONFIG_MAC_EMUMOUSEBTN */
100 #if defined(CONFIG_MAC_ADBKEYCODES) || defined(CONFIG_ADB_KEYBOARD)
101 	if (!mac_hid_keyboard_sends_linux_keycodes()) {
102 		if (keycode > 255 || !mac_keycodes[keycode])
103 			return -1;
104 
105 		handle_scancode((mac_keycodes[keycode] & 0x7f), down);
106 		return 0;
107 	}
108 #endif	/* CONFIG_MAC_ADBKEYCODES || CONFIG_ADB_KEYBOARD */
109 
110 	if (keycode > 255 || !x86_keycodes[keycode])
111 		return -1;
112 
113 	if (keycode == KEY_PAUSE) {
114 		handle_scancode(0xe1, 1);
115 		handle_scancode(0x1d, down);
116 		handle_scancode(0x45, down);
117 		return 0;
118 	}
119 
120 	if (keycode == KEY_SYSRQ && x86_sysrq_alt) {
121 		handle_scancode(0x54, down);
122 
123 		return 0;
124 	}
125 
126 #ifdef CONFIG_SPARC64
127 	if (keycode == KEY_A && sparc_l1_a_state) {
128 		sparc_l1_a_state = 0;
129 		batten_down_hatches();
130 	}
131 #endif
132 
133 	if (x86_keycodes[keycode] & 0x100)
134 		handle_scancode(0xe0, 1);
135 
136 	handle_scancode(x86_keycodes[keycode] & 0x7f, down);
137 
138 	if (keycode == KEY_SYSRQ) {
139 		handle_scancode(0xe0, 1);
140 		handle_scancode(0x37, down);
141 	}
142 
143 	if (keycode == KEY_LEFTALT || keycode == KEY_RIGHTALT)
144 		x86_sysrq_alt = down;
145 #ifdef CONFIG_SPARC64
146 	if (keycode == KEY_STOP)
147 		sparc_l1_a_state = down;
148 #endif
149 
150 	return 0;
151 }
152 
153 #endif /* CONFIG_X86 || CONFIG_IA64 || __alpha__ || __mips__ || CONFIG_PPC */
154 
155 static struct input_handler keybdev_handler;
156 
157 static unsigned int ledstate = 0xff;
158 
keybdev_ledfunc(unsigned int led)159 void keybdev_ledfunc(unsigned int led)
160 {
161 	struct input_handle *handle;
162 
163 	ledstate = led;
164 
165 	for (handle = keybdev_handler.handle; handle; handle = handle->hnext) {
166 		input_event(handle->dev, EV_LED, LED_SCROLLL, !!(led & 0x01));
167 		input_event(handle->dev, EV_LED, LED_NUML,    !!(led & 0x02));
168 		input_event(handle->dev, EV_LED, LED_CAPSL,   !!(led & 0x04));
169 	}
170 }
171 
keybdev_event(struct input_handle * handle,unsigned int type,unsigned int code,int down)172 void keybdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int down)
173 {
174 	if (type != EV_KEY) return;
175 
176 	if (emulate_raw(code, down))
177 		if(code < BTN_MISC)
178 			printk(KERN_WARNING "keybdev.c: can't emulate rawmode for keycode %d\n", code);
179 
180 	tasklet_schedule(&keyboard_tasklet);
181 }
182 
keybdev_connect(struct input_handler * handler,struct input_dev * dev)183 static struct input_handle *keybdev_connect(struct input_handler *handler, struct input_dev *dev)
184 {
185 	struct input_handle *handle;
186 	int i;
187 
188 	if (!test_bit(EV_KEY, dev->evbit))
189 		return NULL;
190 
191 	for (i = KEY_RESERVED; i < BTN_MISC; i++)
192 		if (test_bit(i, dev->keybit)) break;
193 
194 	if (i == BTN_MISC)
195  		return NULL;
196 
197 	if (!(handle = kmalloc(sizeof(struct input_handle), GFP_KERNEL)))
198 		return NULL;
199 	memset(handle, 0, sizeof(struct input_handle));
200 
201 	handle->dev = dev;
202 	handle->handler = handler;
203 
204 	input_open_device(handle);
205 
206 //	printk(KERN_INFO "keybdev.c: Adding keyboard: input%d\n", dev->number);
207 
208 	if (ledstate != 0xff) {
209 		input_event(dev, EV_LED, LED_SCROLLL, !!(ledstate & 0x01));
210 		input_event(dev, EV_LED, LED_NUML,    !!(ledstate & 0x02));
211 		input_event(dev, EV_LED, LED_CAPSL,   !!(ledstate & 0x04));
212 	}
213 
214 	return handle;
215 }
216 
keybdev_disconnect(struct input_handle * handle)217 static void keybdev_disconnect(struct input_handle *handle)
218 {
219 //	printk(KERN_INFO "keybdev.c: Removing keyboard: input%d\n", handle->dev->number);
220 	input_close_device(handle);
221 	kfree(handle);
222 }
223 
224 static struct input_handler keybdev_handler = {
225 	event:		keybdev_event,
226 	connect:	keybdev_connect,
227 	disconnect:	keybdev_disconnect,
228 };
229 
keybdev_init(void)230 static int __init keybdev_init(void)
231 {
232 	input_register_handler(&keybdev_handler);
233 	kbd_ledfunc = keybdev_ledfunc;
234 	kbd_refresh_leds();
235 
236 	if (jp_kbd_109) {
237 		x86_keycodes[0xb5] = 0x73;	/* backslash, underscore */
238 		x86_keycodes[0xb6] = 0x70;
239 		x86_keycodes[0xb7] = 0x7d;	/* Yen, pipe */
240 		x86_keycodes[0xb8] = 0x79;
241 		x86_keycodes[0xb9] = 0x7b;
242 	}
243 
244 	return 0;
245 }
246 
keybdev_exit(void)247 static void __exit keybdev_exit(void)
248 {
249 	kbd_ledfunc = NULL;
250 	input_unregister_handler(&keybdev_handler);
251 }
252 
253 module_init(keybdev_init);
254 module_exit(keybdev_exit);
255 
256 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
257 MODULE_DESCRIPTION("Input driver to keyboard driver binding");
258 MODULE_PARM(jp_kbd_109, "i");
259 MODULE_LICENSE("GPL");
260