File: /usr/src/linux/drivers/macintosh/mac_keyb.c
1 /*
2 * drivers/char/mac_keyb.c
3 *
4 * Keyboard driver for Power Macintosh computers.
5 *
6 * Adapted from drivers/char/keyboard.c by Paul Mackerras
7 * (see that file for its authors and contributors).
8 *
9 * Copyright (C) 1996 Paul Mackerras.
10 *
11 * Adapted to ADB changes and support for more devices by
12 * Benjamin Herrenschmidt. Adapted from code in MkLinux
13 * and reworked.
14 *
15 * Supported devices:
16 *
17 * - Standard 1 button mouse
18 * - All standard Apple Extended protocol (handler ID 4)
19 * - mouseman and trackman mice & trackballs
20 * - PowerBook Trackpad (default setup: enable tapping)
21 * - MicroSpeed mouse & trackball (needs testing)
22 * - CH Products Trackball Pro (needs testing)
23 * - Contour Design (Contour Mouse)
24 * - Hunter digital (NoHandsMouse)
25 * - Kensignton TurboMouse 5 (needs testing)
26 * - Mouse Systems A3 mice and trackballs <aidan@kublai.com>
27 * - MacAlly 2-buttons mouse (needs testing) <pochini@denise.shiny.it>
28 *
29 * To do:
30 *
31 * Improve Kensignton support, add MacX support as a dynamic
32 * option (not a compile-time option).
33 */
34
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/tty.h>
38 #include <linux/mm.h>
39 #include <linux/signal.h>
40 #include <linux/ioport.h>
41 #include <linux/init.h>
42 #include <linux/tty_flip.h>
43 #include <linux/config.h>
44 #include <linux/notifier.h>
45
46 #include <asm/bitops.h>
47
48 #include <linux/adb.h>
49 #include <linux/cuda.h>
50 #include <linux/pmu.h>
51 #include <linux/kbd_kern.h>
52 #include <linux/kbd_ll.h>
53
54 #ifdef CONFIG_PMAC_BACKLIGHT
55 #include <asm/backlight.h>
56 #endif
57
58 #define KEYB_KEYREG 0 /* register # for key up/down data */
59 #define KEYB_LEDREG 2 /* register # for leds on ADB keyboard */
60 #define MOUSE_DATAREG 0 /* reg# for movement/button codes from mouse */
61
62 static int adb_message_handler(struct notifier_block *, unsigned long, void *);
63 static struct notifier_block mackeyb_adb_notifier = {
64 adb_message_handler,
65 NULL,
66 0
67 };
68
69 /* this map indicates which keys shouldn't autorepeat. */
70 static unsigned char dont_repeat[128] = {
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* esc...option */
75 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* fn, num lock */
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, /* scroll lock */
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, /* R modifiers */
79 };
80
81 /* Simple translation table for the SysRq keys */
82
83 #ifdef CONFIG_MAGIC_SYSRQ
84 unsigned char mackbd_sysrq_xlate[128] =
85 "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
86 "yt123465=97-80o]" /* 0x10 - 0x1f */
87 "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
88 "\t `\177\000\033\000\000\000\000\000\000\000\000\000\000"
89 /* 0x30 - 0x3f */
90 "\000\000\000*\000+\000\000\000\000\000/\r\000-\000"
91 /* 0x40 - 0x4f */
92 "\000\0000123456789\000\000\000" /* 0x50 - 0x5f */
93 "\205\206\207\203\210\211\000\213\000\215\000\000\000\000\000\212\000\214";
94 /* 0x60 - 0x6f */
95 #endif
96
97 static u_short macplain_map[NR_KEYS] __initdata = {
98 0xfb61, 0xfb73, 0xfb64, 0xfb66, 0xfb68, 0xfb67, 0xfb7a, 0xfb78,
99 0xfb63, 0xfb76, 0xf200, 0xfb62, 0xfb71, 0xfb77, 0xfb65, 0xfb72,
100 0xfb79, 0xfb74, 0xf031, 0xf032, 0xf033, 0xf034, 0xf036, 0xf035,
101 0xf03d, 0xf039, 0xf037, 0xf02d, 0xf038, 0xf030, 0xf05d, 0xfb6f,
102 0xfb75, 0xf05b, 0xfb69, 0xfb70, 0xf201, 0xfb6c, 0xfb6a, 0xf027,
103 0xfb6b, 0xf03b, 0xf05c, 0xf02c, 0xf02f, 0xfb6e, 0xfb6d, 0xf02e,
104 0xf009, 0xf020, 0xf060, 0xf07f, 0xf200, 0xf01b, 0xf702, 0xf703,
105 0xf700, 0xf207, 0xf701, 0xf601, 0xf602, 0xf600, 0xf603, 0xf200,
106 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
107 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
108 0xf200, 0xf200, 0xf300, 0xf301, 0xf302, 0xf303, 0xf304, 0xf305,
109 0xf306, 0xf307, 0xf200, 0xf308, 0xf309, 0xf200, 0xf200, 0xf200,
110 0xf104, 0xf105, 0xf106, 0xf102, 0xf107, 0xf108, 0xf200, 0xf10a,
111 0xf200, 0xf10c, 0xf200, 0xf209, 0xf200, 0xf109, 0xf200, 0xf10b,
112 0xf200, 0xf11d, 0xf115, 0xf114, 0xf118, 0xf116, 0xf103, 0xf117,
113 0xf101, 0xf119, 0xf100, 0xf700, 0xf701, 0xf702, 0xf200, 0xf200,
114 };
115
116 static u_short macshift_map[NR_KEYS] __initdata = {
117 0xfb41, 0xfb53, 0xfb44, 0xfb46, 0xfb48, 0xfb47, 0xfb5a, 0xfb58,
118 0xfb43, 0xfb56, 0xf200, 0xfb42, 0xfb51, 0xfb57, 0xfb45, 0xfb52,
119 0xfb59, 0xfb54, 0xf021, 0xf040, 0xf023, 0xf024, 0xf05e, 0xf025,
120 0xf02b, 0xf028, 0xf026, 0xf05f, 0xf02a, 0xf029, 0xf07d, 0xfb4f,
121 0xfb55, 0xf07b, 0xfb49, 0xfb50, 0xf201, 0xfb4c, 0xfb4a, 0xf022,
122 0xfb4b, 0xf03a, 0xf07c, 0xf03c, 0xf03f, 0xfb4e, 0xfb4d, 0xf03e,
123 0xf009, 0xf020, 0xf07e, 0xf07f, 0xf200, 0xf01b, 0xf702, 0xf703,
124 0xf700, 0xf207, 0xf701, 0xf601, 0xf602, 0xf600, 0xf603, 0xf200,
125 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
126 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
127 0xf200, 0xf200, 0xf300, 0xf301, 0xf302, 0xf303, 0xf304, 0xf305,
128 0xf306, 0xf307, 0xf200, 0xf308, 0xf309, 0xf200, 0xf200, 0xf200,
129 0xf10e, 0xf10f, 0xf110, 0xf10c, 0xf111, 0xf112, 0xf200, 0xf10a,
130 0xf200, 0xf10c, 0xf200, 0xf203, 0xf200, 0xf113, 0xf200, 0xf10b,
131 0xf200, 0xf11d, 0xf115, 0xf114, 0xf20b, 0xf116, 0xf10d, 0xf117,
132 0xf10b, 0xf20a, 0xf10a, 0xf700, 0xf701, 0xf702, 0xf200, 0xf200,
133 };
134
135 static u_short macaltgr_map[NR_KEYS] __initdata = {
136 0xf914, 0xfb73, 0xf917, 0xf919, 0xfb68, 0xfb67, 0xfb7a, 0xfb78,
137 0xf916, 0xfb76, 0xf200, 0xf915, 0xfb71, 0xfb77, 0xf918, 0xfb72,
138 0xfb79, 0xfb74, 0xf200, 0xf040, 0xf200, 0xf024, 0xf200, 0xf200,
139 0xf200, 0xf05d, 0xf07b, 0xf05c, 0xf05b, 0xf07d, 0xf07e, 0xfb6f,
140 0xfb75, 0xf200, 0xfb69, 0xfb70, 0xf201, 0xfb6c, 0xfb6a, 0xf200,
141 0xfb6b, 0xf200, 0xf200, 0xf200, 0xf200, 0xfb6e, 0xfb6d, 0xf200,
142 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf702, 0xf703,
143 0xf700, 0xf207, 0xf701, 0xf601, 0xf602, 0xf600, 0xf603, 0xf200,
144 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
145 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
146 0xf200, 0xf200, 0xf90a, 0xf90b, 0xf90c, 0xf90d, 0xf90e, 0xf90f,
147 0xf910, 0xf911, 0xf200, 0xf912, 0xf913, 0xf200, 0xf200, 0xf200,
148 0xf510, 0xf511, 0xf512, 0xf50e, 0xf513, 0xf514, 0xf200, 0xf516,
149 0xf200, 0xf10c, 0xf200, 0xf202, 0xf200, 0xf515, 0xf200, 0xf517,
150 0xf200, 0xf11d, 0xf115, 0xf114, 0xf118, 0xf116, 0xf50f, 0xf117,
151 0xf50d, 0xf119, 0xf50c, 0xf700, 0xf701, 0xf702, 0xf200, 0xf200,
152 };
153
154 static u_short macctrl_map[NR_KEYS] __initdata = {
155 0xf001, 0xf013, 0xf004, 0xf006, 0xf008, 0xf007, 0xf01a, 0xf018,
156 0xf003, 0xf016, 0xf200, 0xf002, 0xf011, 0xf017, 0xf005, 0xf012,
157 0xf019, 0xf014, 0xf200, 0xf000, 0xf01b, 0xf01c, 0xf01e, 0xf01d,
158 0xf200, 0xf200, 0xf01f, 0xf01f, 0xf07f, 0xf200, 0xf01d, 0xf00f,
159 0xf015, 0xf01b, 0xf009, 0xf010, 0xf201, 0xf00c, 0xf00a, 0xf007,
160 0xf00b, 0xf200, 0xf01c, 0xf200, 0xf07f, 0xf00e, 0xf00d, 0xf20e,
161 0xf200, 0xf000, 0xf000, 0xf008, 0xf200, 0xf200, 0xf702, 0xf703,
162 0xf700, 0xf207, 0xf701, 0xf601, 0xf602, 0xf600, 0xf603, 0xf200,
163 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
164 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
165 0xf200, 0xf200, 0xf300, 0xf301, 0xf302, 0xf303, 0xf304, 0xf305,
166 0xf306, 0xf307, 0xf200, 0xf308, 0xf309, 0xf200, 0xf200, 0xf200,
167 0xf104, 0xf105, 0xf106, 0xf102, 0xf107, 0xf108, 0xf200, 0xf10a,
168 0xf200, 0xf10c, 0xf200, 0xf204, 0xf200, 0xf109, 0xf200, 0xf10b,
169 0xf200, 0xf11d, 0xf115, 0xf114, 0xf118, 0xf116, 0xf103, 0xf117,
170 0xf101, 0xf119, 0xf100, 0xf700, 0xf701, 0xf702, 0xf200, 0xf200,
171 };
172
173 static u_short macshift_ctrl_map[NR_KEYS] __initdata = {
174 0xf001, 0xf013, 0xf004, 0xf006, 0xf008, 0xf007, 0xf01a, 0xf018,
175 0xf003, 0xf016, 0xf200, 0xf002, 0xf011, 0xf017, 0xf005, 0xf012,
176 0xf019, 0xf014, 0xf200, 0xf000, 0xf200, 0xf200, 0xf200, 0xf200,
177 0xf200, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf200, 0xf200, 0xf00f,
178 0xf015, 0xf200, 0xf009, 0xf010, 0xf201, 0xf00c, 0xf00a, 0xf200,
179 0xf00b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf00e, 0xf00d, 0xf200,
180 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf702, 0xf703,
181 0xf700, 0xf207, 0xf701, 0xf601, 0xf602, 0xf600, 0xf603, 0xf200,
182 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
183 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
184 0xf200, 0xf200, 0xf300, 0xf301, 0xf302, 0xf303, 0xf304, 0xf305,
185 0xf306, 0xf307, 0xf200, 0xf308, 0xf309, 0xf200, 0xf200, 0xf200,
186 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
187 0xf200, 0xf10c, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
188 0xf200, 0xf11d, 0xf115, 0xf114, 0xf118, 0xf116, 0xf200, 0xf117,
189 0xf200, 0xf119, 0xf200, 0xf700, 0xf701, 0xf702, 0xf200, 0xf20c,
190 };
191
192 static u_short macalt_map[NR_KEYS] __initdata = {
193 0xf861, 0xf873, 0xf864, 0xf866, 0xf868, 0xf867, 0xf87a, 0xf878,
194 0xf863, 0xf876, 0xf200, 0xf862, 0xf871, 0xf877, 0xf865, 0xf872,
195 0xf879, 0xf874, 0xf831, 0xf832, 0xf833, 0xf834, 0xf836, 0xf835,
196 0xf83d, 0xf839, 0xf837, 0xf82d, 0xf838, 0xf830, 0xf85d, 0xf86f,
197 0xf875, 0xf85b, 0xf869, 0xf870, 0xf80d, 0xf86c, 0xf86a, 0xf827,
198 0xf86b, 0xf83b, 0xf85c, 0xf82c, 0xf82f, 0xf86e, 0xf86d, 0xf82e,
199 0xf809, 0xf820, 0xf860, 0xf87f, 0xf200, 0xf81b, 0xf702, 0xf703,
200 0xf700, 0xf207, 0xf701, 0xf210, 0xf211, 0xf600, 0xf603, 0xf200,
201 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
202 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
203 0xf200, 0xf200, 0xf900, 0xf901, 0xf902, 0xf903, 0xf904, 0xf905,
204 0xf906, 0xf907, 0xf200, 0xf908, 0xf909, 0xf200, 0xf200, 0xf200,
205 0xf504, 0xf505, 0xf506, 0xf502, 0xf507, 0xf508, 0xf200, 0xf50a,
206 0xf200, 0xf10c, 0xf200, 0xf209, 0xf200, 0xf509, 0xf200, 0xf50b,
207 0xf200, 0xf11d, 0xf115, 0xf114, 0xf118, 0xf116, 0xf503, 0xf117,
208 0xf501, 0xf119, 0xf500, 0xf700, 0xf701, 0xf702, 0xf200, 0xf200,
209 };
210
211 static u_short macctrl_alt_map[NR_KEYS] __initdata = {
212 0xf801, 0xf813, 0xf804, 0xf806, 0xf808, 0xf807, 0xf81a, 0xf818,
213 0xf803, 0xf816, 0xf200, 0xf802, 0xf811, 0xf817, 0xf805, 0xf812,
214 0xf819, 0xf814, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
215 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf80f,
216 0xf815, 0xf200, 0xf809, 0xf810, 0xf201, 0xf80c, 0xf80a, 0xf200,
217 0xf80b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf80e, 0xf80d, 0xf200,
218 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf702, 0xf703,
219 0xf700, 0xf207, 0xf701, 0xf601, 0xf602, 0xf600, 0xf603, 0xf200,
220 0xf200, 0xf310, 0xf200, 0xf30c, 0xf200, 0xf30a, 0xf200, 0xf208,
221 0xf200, 0xf200, 0xf200, 0xf30d, 0xf30e, 0xf200, 0xf30b, 0xf200,
222 0xf200, 0xf200, 0xf300, 0xf301, 0xf302, 0xf303, 0xf304, 0xf305,
223 0xf306, 0xf307, 0xf200, 0xf308, 0xf309, 0xf200, 0xf200, 0xf200,
224 0xf504, 0xf505, 0xf506, 0xf502, 0xf507, 0xf508, 0xf200, 0xf50a,
225 0xf200, 0xf10c, 0xf200, 0xf200, 0xf200, 0xf509, 0xf200, 0xf50b,
226 0xf200, 0xf11d, 0xf115, 0xf114, 0xf118, 0xf116, 0xf503, 0xf117,
227 0xf501, 0xf119, 0xf500, 0xf700, 0xf701, 0xf702, 0xf200, 0xf200,
228 };
229
230
231 static void kbd_repeat(unsigned long);
232 static struct timer_list repeat_timer = { function: kbd_repeat };
233 static int last_keycode;
234
235 static void mackeyb_probe(void);
236
237 static void keyboard_input(unsigned char *, int, struct pt_regs *, int);
238 static void input_keycode(int, int);
239 static void leds_done(struct adb_request *);
240 static void mac_put_queue(int);
241
242 static void buttons_input(unsigned char *, int, struct pt_regs *, int);
243
244 static void init_trackpad(int id);
245 static void init_trackball(int id);
246 static void init_turbomouse(int id);
247 static void init_microspeed(int id);
248 static void init_ms_a3(int id);
249
250 #ifdef CONFIG_ADBMOUSE
251 /* XXX: Hook for mouse driver */
252 void (*adb_mouse_interrupt_hook)(unsigned char *, int);
253 int adb_emulate_buttons = 0;
254 int adb_button2_keycode = 0x7d; /* right control key */
255 int adb_button3_keycode = 0x7c; /* right option key */
256 #endif
257
258 extern struct kbd_struct kbd_table[];
259 extern wait_queue_head_t keypress_wait;
260
261 extern void handle_scancode(unsigned char, int);
262
263 static struct adb_ids keyboard_ids;
264 static struct adb_ids mouse_ids;
265 static struct adb_ids buttons_ids;
266
267 /* Kind of mouse */
268 #define ADBMOUSE_STANDARD_100 0 /* Standard 100cpi mouse (handler 1) */
269 #define ADBMOUSE_STANDARD_200 1 /* Standard 200cpi mouse (handler 2) */
270 #define ADBMOUSE_EXTENDED 2 /* Apple Extended mouse (handler 4) */
271 #define ADBMOUSE_TRACKBALL 3 /* TrackBall (handler 4) */
272 #define ADBMOUSE_TRACKPAD 4 /* Apple's PowerBook trackpad (handler 4) */
273 #define ADBMOUSE_TURBOMOUSE5 5 /* Turbomouse 5 (previously req. mousehack) */
274 #define ADBMOUSE_MICROSPEED 6 /* Microspeed mouse (&trackball ?), MacPoint */
275 #define ADBMOUSE_TRACKBALLPRO 7 /* Trackball Pro (special buttons) */
276 #define ADBMOUSE_MS_A3 8 /* Mouse systems A3 trackball (handler 3) */
277 #define ADBMOUSE_MACALLY2 9 /* MacAlly 2-button mouse */
278
279 static int adb_mouse_kinds[16];
280
281 int mackbd_setkeycode(unsigned int scancode, unsigned int keycode)
282 {
283 return -EINVAL;
284 }
285
286 int mackbd_getkeycode(unsigned int scancode)
287 {
288 return -EINVAL;
289 }
290
291 int mackbd_translate(unsigned char keycode, unsigned char *keycodep,
292 char raw_mode)
293 {
294 if (!raw_mode) {
295 /*
296 * Convert R-shift/control/option to L version.
297 */
298 switch (keycode) {
299 case 0x7b: keycode = 0x38; break; /* R-shift */
300 case 0x7c: keycode = 0x3a; break; /* R-option */
301 case 0x7d: keycode = 0x36; break; /* R-control */
302 }
303 }
304 *keycodep = keycode;
305 return 1;
306 }
307
308 char mackbd_unexpected_up(unsigned char keycode)
309 {
310 return 0x80;
311 }
312
313 static void
314 keyboard_input(unsigned char *data, int nb, struct pt_regs *regs, int apoll)
315 {
316 /* first check this is from register 0 */
317 if (nb != 3 || (data[0] & 3) != KEYB_KEYREG)
318 return; /* ignore it */
319 kbd_pt_regs = regs;
320 input_keycode(data[1], 0);
321 if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
322 input_keycode(data[2], 0);
323 }
324
325 static void
326 input_keycode(int keycode, int repeat)
327 {
328 struct kbd_struct *kbd;
329 int up_flag;
330
331 kbd = kbd_table + fg_console;
332 up_flag = (keycode & 0x80);
333 keycode &= 0x7f;
334
335 /* on the powerbook 3400, the power key gives code 0x7e */
336 if (keycode == 0x7e)
337 keycode = 0x7f;
338 /* remap the "Fn" key of the PowerBook G3 Series to 0x48
339 to avoid conflict with button emulation */
340 if (keycode == 0x3f)
341 keycode = 0x48;
342
343 if (!repeat)
344 del_timer(&repeat_timer);
345
346 #ifdef CONFIG_ADBMOUSE
347 /*
348 * XXX: Add mouse button 2+3 fake codes here if mouse open.
349 * Keep track of 'button' states here as we only send
350 * single up/down events!
351 * Really messy; might need to check if keyboard is in
352 * VC_RAW mode.
353 * Might also want to know how many buttons need to be emulated.
354 * -> hide this as function in arch/m68k/mac ?
355 */
356 if (adb_emulate_buttons
357 && (keycode == adb_button2_keycode
358 || keycode == adb_button3_keycode)
359 && (adb_mouse_interrupt_hook || console_loglevel == 10)) {
360 int button;
361 /* faked ADB packet */
362 static unsigned char data[4] = { 0, 0x80, 0x80, 0x80 };
363
364 button = keycode == adb_button2_keycode? 2: 3;
365 if (data[button] != up_flag) {
366 /* send a fake mouse packet */
367 data[button] = up_flag;
368 if (console_loglevel >= 8)
369 printk("fake mouse event: %x %x %x\n",
370 data[1], data[2], data[3]);
371 if (adb_mouse_interrupt_hook)
372 adb_mouse_interrupt_hook(data, 4);
373 }
374 return;
375 }
376 #endif /* CONFIG_ADBMOUSE */
377
378 if (kbd->kbdmode != VC_RAW) {
379 if (!up_flag && !dont_repeat[keycode]) {
380 last_keycode = keycode;
381 repeat_timer.expires = jiffies + (repeat? HZ/15: HZ/2);
382 add_timer(&repeat_timer);
383 }
384
385 /*
386 * adb kludge!! Imitate pc caps lock behaviour by
387 * generating an up/down event for each time caps
388 * is pressed/released. Also, makes sure that the
389 * LED are handled. atong@uiuc.edu
390 */
391 switch (keycode) {
392 /*case 0xb9:*/
393 case 0x39:
394 handle_scancode(0x39, 1);
395 handle_scancode(0x39, 0);
396 tasklet_schedule(&keyboard_tasklet);
397 return;
398 case 0x47:
399 /*case 0xc7:*/
400 tasklet_schedule(&keyboard_tasklet);
401 break;
402 }
403 }
404
405 handle_scancode(keycode, !up_flag);
406 tasklet_schedule(&keyboard_tasklet);
407 }
408
409 static void
410 kbd_repeat(unsigned long xxx)
411 {
412 unsigned long flags;
413
414 save_flags(flags);
415 cli();
416 input_keycode(last_keycode, 1);
417 restore_flags(flags);
418 }
419
420 static void mac_put_queue(int ch)
421 {
422 extern struct tty_driver console_driver;
423 struct tty_struct *tty;
424
425 tty = console_driver.table? console_driver.table[fg_console]: NULL;
426 wake_up(&keypress_wait);
427 if (tty) {
428 tty_insert_flip_char(tty, ch, 0);
429 con_schedule_flip(tty);
430 }
431 }
432
433 #ifdef CONFIG_ADBMOUSE
434 static void
435 mouse_input(unsigned char *data, int nb, struct pt_regs *regs, int autopoll)
436 {
437 /* [ACA:23-Mar-97] Three button mouse support. This is designed to
438 function with MkLinux DR-2.1 style X servers. It only works with
439 three-button mice that conform to Apple's multi-button mouse
440 protocol. */
441
442 /*
443 The X server for MkLinux DR2.1 uses the following unused keycodes to
444 read the mouse:
445
446 0x7e This indicates that the next two keycodes should be interpreted
447 as mouse information. The first following byte's high bit
448 represents the state of the left button. The lower seven bits
449 represent the x-axis acceleration. The lower seven bits of the
450 second byte represent y-axis acceleration.
451
452 0x3f The x server interprets this keycode as a middle button
453 release.
454
455 0xbf The x server interprets this keycode as a middle button
456 depress.
457
458 0x40 The x server interprets this keycode as a right button
459 release.
460
461 0xc0 The x server interprets this keycode as a right button
462 depress.
463
464 NOTES: There should be a better way of handling mice in the X server.
465 The MOUSE_ESCAPE code (0x7e) should be followed by three bytes instead
466 of two. The three mouse buttons should then, in the X server, be read
467 as the high-bits of all three bytes. The x and y motions can still be
468 in the first two bytes. Maybe I'll do this...
469 */
470
471 /*
472 Handler 1 -- 100cpi original Apple mouse protocol.
473 Handler 2 -- 200cpi original Apple mouse protocol.
474
475 For Apple's standard one-button mouse protocol the data array will
476 contain the following values:
477
478 BITS COMMENTS
479 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
480 data[1] = bxxx xxxx First button and x-axis motion.
481 data[2] = byyy yyyy Second button and y-axis motion.
482
483 Handler 4 -- Apple Extended mouse protocol.
484
485 For Apple's 3-button mouse protocol the data array will contain the
486 following values:
487
488 BITS COMMENTS
489 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
490 data[1] = bxxx xxxx Left button and x-axis motion.
491 data[2] = byyy yyyy Second button and y-axis motion.
492 data[3] = byyy bxxx Third button and fourth button. Y is additional
493 high bits of y-axis motion. XY is additional
494 high bits of x-axis motion.
495
496 MacAlly 2-button mouse protocol.
497
498 For MacAlly 2-button mouse protocol the data array will contain the
499 following values:
500
501 BITS COMMENTS
502 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
503 data[1] = bxxx xxxx Left button and x-axis motion.
504 data[2] = byyy yyyy Right button and y-axis motion.
505 data[3] = ???? ???? unknown
506 data[4] = ???? ???? unknown
507
508 */
509 struct kbd_struct *kbd;
510
511 /* If it's a trackpad, we alias the second button to the first.
512 NOTE: Apple sends an ADB flush command to the trackpad when
513 the first (the real) button is released. We could do
514 this here using async flush requests.
515 */
516 switch (adb_mouse_kinds[(data[0]>>4) & 0xf])
517 {
518 case ADBMOUSE_TRACKPAD:
519 data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
520 data[2] = data[2] | 0x80;
521 break;
522 case ADBMOUSE_MICROSPEED:
523 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
524 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
525 data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
526 | (data[3] & 0x08);
527 break;
528 case ADBMOUSE_TRACKBALLPRO:
529 data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
530 & ((data[3] & 0x08) << 4));
531 data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
532 data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
533 break;
534 case ADBMOUSE_MS_A3:
535 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
536 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
537 data[3] = ((data[3] & 0x04) << 5);
538 break;
539 case ADBMOUSE_MACALLY2:
540 data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
541 data[2] |= 0x80; /* Right button is mapped as button 3 */
542 nb=4;
543 break;
544 }
545
546 if (adb_mouse_interrupt_hook)
547 adb_mouse_interrupt_hook(data, nb);
548
549 kbd = kbd_table + fg_console;
550
551 /* Only send mouse codes when keyboard is in raw mode. */
552 if (kbd->kbdmode == VC_RAW) {
553 static unsigned char uch_ButtonStateSecond = 0x80;
554 unsigned char uchButtonSecond;
555
556 /* Send first button, second button and movement. */
557 mac_put_queue(0x7e);
558 mac_put_queue(data[1]);
559 mac_put_queue(data[2]);
560
561 /* [ACA: Are there any two-button ADB mice that use handler 1 or 2?] */
562
563 /* Store the button state. */
564 uchButtonSecond = (data[2] & 0x80);
565
566 /* Send second button. */
567 if (uchButtonSecond != uch_ButtonStateSecond) {
568 mac_put_queue(0x3f | uchButtonSecond);
569 uch_ButtonStateSecond = uchButtonSecond;
570 }
571
572 /* Macintosh 3-button mouse (handler 4). */
573 if (nb >= 4) {
574 static unsigned char uch_ButtonStateThird = 0x80;
575 unsigned char uchButtonThird;
576
577 /* Store the button state for speed. */
578 uchButtonThird = (data[3] & 0x80);
579
580 /* Send third button. */
581 if (uchButtonThird != uch_ButtonStateThird) {
582 mac_put_queue(0x40 | uchButtonThird);
583 uch_ButtonStateThird = uchButtonThird;
584 }
585 }
586 }
587 }
588 #endif /* CONFIG_ADBMOUSE */
589
590 static void
591 buttons_input(unsigned char *data, int nb, struct pt_regs *regs, int autopoll)
592 {
593 #ifdef CONFIG_PMAC_BACKLIGHT
594 int backlight = get_backlight_level();
595
596 /*
597 * XXX: Where is the contrast control for the passive?
598 * -- Cort
599 */
600
601 /* Ignore data from register other than 0 */
602 if ((data[0] & 0x3) || (nb < 2))
603 return;
604
605 switch (data[1]) {
606 case 0x8: /* mute */
607 break;
608
609 case 0x7: /* contrast decrease */
610 break;
611
612 case 0x6: /* contrast increase */
613 break;
614
615 case 0xa: /* brightness decrease */
616 if (backlight < 0)
617 break;
618 if (backlight > BACKLIGHT_OFF)
619 set_backlight_level(backlight-1);
620 else
621 set_backlight_level(BACKLIGHT_OFF);
622 break;
623
624 case 0x9: /* brightness increase */
625 if (backlight < 0)
626 break;
627 if (backlight < BACKLIGHT_MAX)
628 set_backlight_level(backlight+1);
629 else
630 set_backlight_level(BACKLIGHT_MAX);
631 break;
632 }
633 #endif /* CONFIG_PMAC_BACKLIGHT */
634 }
635
636 /* Map led flags as defined in kbd_kern.h to bits for Apple keyboard. */
637 static unsigned char mac_ledmap[8] = {
638 0, /* none */
639 4, /* scroll lock */
640 1, /* num lock */
641 5, /* scroll + num lock */
642 2, /* caps lock */
643 6, /* caps + scroll lock */
644 3, /* caps + num lock */
645 7, /* caps + num + scroll lock */
646 };
647
648 static struct adb_request led_request;
649 static int leds_pending[16];
650 static int pending_devs[16];
651 static int pending_led_start=0;
652 static int pending_led_end=0;
653
654 static void real_mackbd_leds(unsigned char leds, int device)
655 {
656 if (led_request.complete) {
657 adb_request(&led_request, leds_done, 0, 3,
658 ADB_WRITEREG(device, KEYB_LEDREG), 0xff,
659 ~mac_ledmap[leds]);
660 } else {
661 if (!(leds_pending[device] & 0x100)) {
662 pending_devs[pending_led_end] = device;
663 pending_led_end++;
664 pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
665 }
666 leds_pending[device] = leds | 0x100;
667 }
668 }
669
670 void mackbd_leds(unsigned char leds)
671 {
672 int i;
673
674 for(i = 0; i < keyboard_ids.nids; i++)
675 real_mackbd_leds(leds,keyboard_ids.id[i]);
676 }
677
678 static void leds_done(struct adb_request *req)
679 {
680 int leds,device;
681
682 if (pending_led_start != pending_led_end) {
683 device = pending_devs[pending_led_start];
684 leds = leds_pending[device] & 0xff;
685 leds_pending[device] = 0;
686 pending_led_start++;
687 pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
688 real_mackbd_leds(leds,device);
689 }
690
691 }
692
693 void __init mackbd_init_hw(void)
694 {
695 #ifdef CONFIG_PPC
696 if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
697 return;
698 #endif
699 #ifdef CONFIG_MAC
700 if (!MACH_IS_MAC)
701 return;
702 #endif
703
704 /* setup key map */
705 memcpy(key_maps[0], macplain_map, sizeof(plain_map));
706 memcpy(key_maps[1], macshift_map, sizeof(plain_map));
707 memcpy(key_maps[2], macaltgr_map, sizeof(plain_map));
708 memcpy(key_maps[4], macctrl_map, sizeof(plain_map));
709 memcpy(key_maps[5], macshift_ctrl_map, sizeof(plain_map));
710 memcpy(key_maps[8], macalt_map, sizeof(plain_map));
711 memcpy(key_maps[12], macctrl_alt_map, sizeof(plain_map));
712
713 #ifdef CONFIG_ADBMOUSE
714 /* initialize mouse interrupt hook */
715 adb_mouse_interrupt_hook = NULL;
716 #endif
717
718 led_request.complete = 1;
719
720 mackeyb_probe();
721
722 notifier_chain_register(&adb_client_list, &mackeyb_adb_notifier);
723 }
724
725 static int
726 adb_message_handler(struct notifier_block *this, unsigned long code, void *x)
727 {
728 unsigned long flags;
729
730 switch (code) {
731 case ADB_MSG_PRE_RESET:
732 case ADB_MSG_POWERDOWN:
733 /* Stop the repeat timer. Autopoll is already off at this point */
734 save_flags(flags);
735 cli();
736 del_timer(&repeat_timer);
737 restore_flags(flags);
738
739 /* Stop pending led requests */
740 while(!led_request.complete)
741 adb_poll();
742 break;
743
744 case ADB_MSG_POST_RESET:
745 mackeyb_probe();
746 break;
747 }
748 return NOTIFY_DONE;
749 }
750
751 static void
752 mackeyb_probe(void)
753 {
754 struct adb_request req;
755 int i;
756
757 #ifdef CONFIG_ADBMOUSE
758 adb_register(ADB_MOUSE, 0, &mouse_ids, mouse_input);
759 #endif /* CONFIG_ADBMOUSE */
760
761 adb_register(ADB_KEYBOARD, 0, &keyboard_ids, keyboard_input);
762 adb_register(0x07, 0x1F, &buttons_ids, buttons_input);
763
764 for (i = 0; i < keyboard_ids.nids; i++) {
765 int id = keyboard_ids.id[i];
766
767 /* turn off all leds */
768 adb_request(&req, NULL, ADBREQ_SYNC, 3,
769 ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
770
771 /* Enable full feature set of the keyboard
772 ->get it to send separate codes for left and right shift,
773 control, option keys */
774 #if 0 /* handler 5 doesn't send separate codes for R modifiers */
775 if (adb_try_handler_change(id, 5))
776 printk("ADB keyboard at %d, handler set to 5\n", id);
777 else
778 #endif
779 if (adb_try_handler_change(id, 3))
780 printk("ADB keyboard at %d, handler set to 3\n", id);
781 else
782 printk("ADB keyboard at %d, handler 1\n", id);
783 }
784
785 /* Try to switch all mice to handler 4, or 2 for three-button
786 mode and full resolution. */
787 for (i = 0; i < mouse_ids.nids; i++) {
788 int id = mouse_ids.id[i];
789 if (adb_try_handler_change(id, 4)) {
790 printk("ADB mouse at %d, handler set to 4", id);
791 adb_mouse_kinds[id] = ADBMOUSE_EXTENDED;
792 }
793 else if (adb_try_handler_change(id, 0x2F)) {
794 printk("ADB mouse at %d, handler set to 0x2F", id);
795 adb_mouse_kinds[id] = ADBMOUSE_MICROSPEED;
796 }
797 else if (adb_try_handler_change(id, 0x42)) {
798 printk("ADB mouse at %d, handler set to 0x42", id);
799 adb_mouse_kinds[id] = ADBMOUSE_TRACKBALLPRO;
800 }
801 else if (adb_try_handler_change(id, 0x66)) {
802 printk("ADB mouse at %d, handler set to 0x66", id);
803 adb_mouse_kinds[id] = ADBMOUSE_MICROSPEED;
804 }
805 else if (adb_try_handler_change(id, 0x5F)) {
806 printk("ADB mouse at %d, handler set to 0x5F", id);
807 adb_mouse_kinds[id] = ADBMOUSE_MICROSPEED;
808 }
809 else if (adb_try_handler_change(id, 3)) {
810 printk("ADB mouse at %d, handler set to 3", id);
811 adb_mouse_kinds[id] = ADBMOUSE_MS_A3;
812 }
813 else if (adb_try_handler_change(id, 2)) {
814 printk("ADB mouse at %d, handler set to 2", id);
815 adb_mouse_kinds[id] = ADBMOUSE_STANDARD_200;
816 }
817 else {
818 printk("ADB mouse at %d, handler 1", id);
819 adb_mouse_kinds[id] = ADBMOUSE_STANDARD_100;
820 }
821
822 if ((adb_mouse_kinds[id] == ADBMOUSE_TRACKBALLPRO)
823 || (adb_mouse_kinds[id] == ADBMOUSE_MICROSPEED)) {
824 init_microspeed(id);
825 } else if (adb_mouse_kinds[id] == ADBMOUSE_MS_A3) {
826 init_ms_a3(id);
827 } else if (adb_mouse_kinds[id] == ADBMOUSE_EXTENDED) {
828 /*
829 * Register 1 is usually used for device
830 * identification. Here, we try to identify
831 * a known device and call the appropriate
832 * init function.
833 */
834 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
835 ADB_READREG(id, 1));
836
837 if ((req.reply_len) &&
838 (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
839 || (req.reply[2] == 0x20)))
840 init_trackball(id);
841 else if ((req.reply_len >= 4) &&
842 (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
843 (req.reply[3] == 0x61) && (req.reply[4] == 0x64))
844 init_trackpad(id);
845 else if ((req.reply_len >= 4) &&
846 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
847 (req.reply[3] == 0x4c) && (req.reply[4] == 0x31))
848 init_turbomouse(id);
849 else if ((req.reply_len == 9) &&
850 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
851 (req.reply[3] == 0x49) && (req.reply[4] == 0x54)){
852 if (adb_try_handler_change(id, 0x42)) {
853 printk("\nADB MacAlly 2-button mouse at %d, handler set to 0x42", id);
854 adb_mouse_kinds[id] = ADBMOUSE_MACALLY2;
855 }
856 }
857 }
858 printk("\n");
859 }
860 }
861
862 static void
863 init_trackpad(int id)
864 {
865 struct adb_request req;
866 unsigned char r1_buffer[8];
867
868 printk(" (trackpad)");
869
870 adb_mouse_kinds[id] = ADBMOUSE_TRACKPAD;
871
872 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
873 ADB_READREG(id,1));
874 if (req.reply_len < 8)
875 printk("bad length for reg. 1\n");
876 else
877 {
878 memcpy(r1_buffer, &req.reply[1], 8);
879 adb_request(&req, NULL, ADBREQ_SYNC, 9,
880 ADB_WRITEREG(id,1),
881 r1_buffer[0],
882 r1_buffer[1],
883 r1_buffer[2],
884 r1_buffer[3],
885 r1_buffer[4],
886 r1_buffer[5],
887 0x0d, /*r1_buffer[6],*/
888 r1_buffer[7]);
889
890 adb_request(&req, NULL, ADBREQ_SYNC, 9,
891 ADB_WRITEREG(id,2),
892 0x99,
893 0x94,
894 0x19,
895 0xff,
896 0xb2,
897 0x8a,
898 0x1b,
899 0x50);
900
901 adb_request(&req, NULL, ADBREQ_SYNC, 9,
902 ADB_WRITEREG(id,1),
903 r1_buffer[0],
904 r1_buffer[1],
905 r1_buffer[2],
906 r1_buffer[3],
907 r1_buffer[4],
908 r1_buffer[5],
909 0x03, /*r1_buffer[6],*/
910 r1_buffer[7]);
911
912 /* Without this flush, the trackpad may be locked up */
913 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
914 }
915 }
916
917 static void
918 init_trackball(int id)
919 {
920 struct adb_request req;
921
922 printk(" (trackman/mouseman)");
923
924 adb_mouse_kinds[id] = ADBMOUSE_TRACKBALL;
925
926 adb_request(&req, NULL, ADBREQ_SYNC, 3,
927 ADB_WRITEREG(id,1), 00,0x81);
928
929 adb_request(&req, NULL, ADBREQ_SYNC, 3,
930 ADB_WRITEREG(id,1), 01,0x81);
931
932 adb_request(&req, NULL, ADBREQ_SYNC, 3,
933 ADB_WRITEREG(id,1), 02,0x81);
934
935 adb_request(&req, NULL, ADBREQ_SYNC, 3,
936 ADB_WRITEREG(id,1), 03,0x38);
937
938 adb_request(&req, NULL, ADBREQ_SYNC, 3,
939 ADB_WRITEREG(id,1), 00,0x81);
940
941 adb_request(&req, NULL, ADBREQ_SYNC, 3,
942 ADB_WRITEREG(id,1), 01,0x81);
943
944 adb_request(&req, NULL, ADBREQ_SYNC, 3,
945 ADB_WRITEREG(id,1), 02,0x81);
946
947 adb_request(&req, NULL, ADBREQ_SYNC, 3,
948 ADB_WRITEREG(id,1), 03,0x38);
949 }
950
951 static void
952 init_turbomouse(int id)
953 {
954 struct adb_request req;
955
956 printk(" (TurboMouse 5)");
957
958 adb_mouse_kinds[id] = ADBMOUSE_TURBOMOUSE5;
959
960 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
961
962 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
963
964 adb_request(&req, NULL, ADBREQ_SYNC, 9,
965 ADB_WRITEREG(3,2),
966 0xe7,
967 0x8c,
968 0,
969 0,
970 0,
971 0xff,
972 0xff,
973 0x94);
974
975 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
976
977 adb_request(&req, NULL, ADBREQ_SYNC, 9,
978 ADB_WRITEREG(3,2),
979 0xa5,
980 0x14,
981 0,
982 0,
983 0x69,
984 0xff,
985 0xff,
986 0x27);
987 }
988
989 static void
990 init_microspeed(int id)
991 {
992 struct adb_request req;
993
994 printk(" (Microspeed/MacPoint or compatible)");
995
996 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
997
998 /* This will initialize mice using the Microspeed, MacPoint and
999 other compatible firmware. Bit 12 enables extended protocol.
1000
1001 Register 1 Listen (4 Bytes)
1002 0 - 3 Button is mouse (set also for double clicking!!!)
1003 4 - 7 Button is locking (affects change speed also)
1004 8 - 11 Button changes speed
1005 12 1 = Extended mouse mode, 0 = normal mouse mode
1006 13 - 15 unused 0
1007 16 - 23 normal speed
1008 24 - 31 changed speed
1009
1010 Register 1 talk holds version and product identification information.
1011 Register 1 Talk (4 Bytes):
1012 0 - 7 Product code
1013 8 - 23 undefined, reserved
1014 24 - 31 Version number
1015
1016 Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
1017 */
1018 adb_request(&req, NULL, ADBREQ_SYNC, 5,
1019 ADB_WRITEREG(id,1),
1020 0x20, /* alt speed = 0x20 (rather slow) */
1021 0x00, /* norm speed = 0x00 (fastest) */
1022 0x10, /* extended protocol, no speed change */
1023 0x07); /* all buttons enabled as mouse buttons, no locking */
1024
1025
1026 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1027 }
1028
1029 static void
1030 init_ms_a3(int id)
1031 {
1032 struct adb_request req;
1033
1034 printk(" (Mouse Systems A3 Mouse, or compatible)");
1035 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1036 ADB_WRITEREG(id, 0x2),
1037 0x00,
1038 0x07);
1039
1040 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1041 }
1042
1043