]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/macintosh/adbhid.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[mirror_ubuntu-bionic-kernel.git] / drivers / macintosh / adbhid.c
1 /*
2 * drivers/macintosh/adbhid.c
3 *
4 * ADB HID driver for Power Macintosh computers.
5 *
6 * Adapted from drivers/macintosh/mac_keyb.c by Franz Sirl.
7 * drivers/macintosh/mac_keyb.c was Copyright (C) 1996 Paul Mackerras
8 * with considerable contributions from Ben Herrenschmidt and others.
9 *
10 * Copyright (C) 2000 Franz Sirl.
11 *
12 * Adapted to ADB changes and support for more devices by
13 * Benjamin Herrenschmidt. Adapted from code in MkLinux
14 * and reworked.
15 *
16 * Supported devices:
17 *
18 * - Standard 1 button mouse
19 * - All standard Apple Extended protocol (handler ID 4)
20 * - mouseman and trackman mice & trackballs
21 * - PowerBook Trackpad (default setup: enable tapping)
22 * - MicroSpeed mouse & trackball (needs testing)
23 * - CH Products Trackball Pro (needs testing)
24 * - Contour Design (Contour Mouse)
25 * - Hunter digital (NoHandsMouse)
26 * - Kensignton TurboMouse 5 (needs testing)
27 * - Mouse Systems A3 mice and trackballs <aidan@kublai.com>
28 * - MacAlly 2-buttons mouse (needs testing) <pochini@denise.shiny.it>
29 *
30 * To do:
31 *
32 * Improve Kensington support.
33 * Split mouse/kbd
34 * Move to syfs
35 */
36
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/init.h>
40 #include <linux/notifier.h>
41 #include <linux/input.h>
42
43 #include <linux/adb.h>
44 #include <linux/cuda.h>
45 #include <linux/pmu.h>
46
47 #include <asm/machdep.h>
48 #ifdef CONFIG_PPC_PMAC
49 #include <asm/backlight.h>
50 #include <asm/pmac_feature.h>
51 #endif
52
53 MODULE_AUTHOR("Franz Sirl <Franz.Sirl-kernel@lauterbach.com>");
54
55 #define KEYB_KEYREG 0 /* register # for key up/down data */
56 #define KEYB_LEDREG 2 /* register # for leds on ADB keyboard */
57 #define MOUSE_DATAREG 0 /* reg# for movement/button codes from mouse */
58
59 static int adb_message_handler(struct notifier_block *, unsigned long, void *);
60 static struct notifier_block adbhid_adb_notifier = {
61 .notifier_call = adb_message_handler,
62 };
63
64 /* Some special keys */
65 #define ADB_KEY_DEL 0x33
66 #define ADB_KEY_CMD 0x37
67 #define ADB_KEY_CAPSLOCK 0x39
68 #define ADB_KEY_FN 0x3f
69 #define ADB_KEY_FWDEL 0x75
70 #define ADB_KEY_POWER_OLD 0x7e
71 #define ADB_KEY_POWER 0x7f
72
73 u16 adb_to_linux_keycodes[128] = {
74 /* 0x00 */ KEY_A, /* 30 */
75 /* 0x01 */ KEY_S, /* 31 */
76 /* 0x02 */ KEY_D, /* 32 */
77 /* 0x03 */ KEY_F, /* 33 */
78 /* 0x04 */ KEY_H, /* 35 */
79 /* 0x05 */ KEY_G, /* 34 */
80 /* 0x06 */ KEY_Z, /* 44 */
81 /* 0x07 */ KEY_X, /* 45 */
82 /* 0x08 */ KEY_C, /* 46 */
83 /* 0x09 */ KEY_V, /* 47 */
84 /* 0x0a */ KEY_102ND, /* 86 */
85 /* 0x0b */ KEY_B, /* 48 */
86 /* 0x0c */ KEY_Q, /* 16 */
87 /* 0x0d */ KEY_W, /* 17 */
88 /* 0x0e */ KEY_E, /* 18 */
89 /* 0x0f */ KEY_R, /* 19 */
90 /* 0x10 */ KEY_Y, /* 21 */
91 /* 0x11 */ KEY_T, /* 20 */
92 /* 0x12 */ KEY_1, /* 2 */
93 /* 0x13 */ KEY_2, /* 3 */
94 /* 0x14 */ KEY_3, /* 4 */
95 /* 0x15 */ KEY_4, /* 5 */
96 /* 0x16 */ KEY_6, /* 7 */
97 /* 0x17 */ KEY_5, /* 6 */
98 /* 0x18 */ KEY_EQUAL, /* 13 */
99 /* 0x19 */ KEY_9, /* 10 */
100 /* 0x1a */ KEY_7, /* 8 */
101 /* 0x1b */ KEY_MINUS, /* 12 */
102 /* 0x1c */ KEY_8, /* 9 */
103 /* 0x1d */ KEY_0, /* 11 */
104 /* 0x1e */ KEY_RIGHTBRACE, /* 27 */
105 /* 0x1f */ KEY_O, /* 24 */
106 /* 0x20 */ KEY_U, /* 22 */
107 /* 0x21 */ KEY_LEFTBRACE, /* 26 */
108 /* 0x22 */ KEY_I, /* 23 */
109 /* 0x23 */ KEY_P, /* 25 */
110 /* 0x24 */ KEY_ENTER, /* 28 */
111 /* 0x25 */ KEY_L, /* 38 */
112 /* 0x26 */ KEY_J, /* 36 */
113 /* 0x27 */ KEY_APOSTROPHE, /* 40 */
114 /* 0x28 */ KEY_K, /* 37 */
115 /* 0x29 */ KEY_SEMICOLON, /* 39 */
116 /* 0x2a */ KEY_BACKSLASH, /* 43 */
117 /* 0x2b */ KEY_COMMA, /* 51 */
118 /* 0x2c */ KEY_SLASH, /* 53 */
119 /* 0x2d */ KEY_N, /* 49 */
120 /* 0x2e */ KEY_M, /* 50 */
121 /* 0x2f */ KEY_DOT, /* 52 */
122 /* 0x30 */ KEY_TAB, /* 15 */
123 /* 0x31 */ KEY_SPACE, /* 57 */
124 /* 0x32 */ KEY_GRAVE, /* 41 */
125 /* 0x33 */ KEY_BACKSPACE, /* 14 */
126 /* 0x34 */ KEY_KPENTER, /* 96 */
127 /* 0x35 */ KEY_ESC, /* 1 */
128 /* 0x36 */ KEY_LEFTCTRL, /* 29 */
129 /* 0x37 */ KEY_LEFTMETA, /* 125 */
130 /* 0x38 */ KEY_LEFTSHIFT, /* 42 */
131 /* 0x39 */ KEY_CAPSLOCK, /* 58 */
132 /* 0x3a */ KEY_LEFTALT, /* 56 */
133 /* 0x3b */ KEY_LEFT, /* 105 */
134 /* 0x3c */ KEY_RIGHT, /* 106 */
135 /* 0x3d */ KEY_DOWN, /* 108 */
136 /* 0x3e */ KEY_UP, /* 103 */
137 /* 0x3f */ KEY_FN, /* 0x1d0 */
138 /* 0x40 */ 0,
139 /* 0x41 */ KEY_KPDOT, /* 83 */
140 /* 0x42 */ 0,
141 /* 0x43 */ KEY_KPASTERISK, /* 55 */
142 /* 0x44 */ 0,
143 /* 0x45 */ KEY_KPPLUS, /* 78 */
144 /* 0x46 */ 0,
145 /* 0x47 */ KEY_NUMLOCK, /* 69 */
146 /* 0x48 */ 0,
147 /* 0x49 */ 0,
148 /* 0x4a */ 0,
149 /* 0x4b */ KEY_KPSLASH, /* 98 */
150 /* 0x4c */ KEY_KPENTER, /* 96 */
151 /* 0x4d */ 0,
152 /* 0x4e */ KEY_KPMINUS, /* 74 */
153 /* 0x4f */ 0,
154 /* 0x50 */ 0,
155 /* 0x51 */ KEY_KPEQUAL, /* 117 */
156 /* 0x52 */ KEY_KP0, /* 82 */
157 /* 0x53 */ KEY_KP1, /* 79 */
158 /* 0x54 */ KEY_KP2, /* 80 */
159 /* 0x55 */ KEY_KP3, /* 81 */
160 /* 0x56 */ KEY_KP4, /* 75 */
161 /* 0x57 */ KEY_KP5, /* 76 */
162 /* 0x58 */ KEY_KP6, /* 77 */
163 /* 0x59 */ KEY_KP7, /* 71 */
164 /* 0x5a */ 0,
165 /* 0x5b */ KEY_KP8, /* 72 */
166 /* 0x5c */ KEY_KP9, /* 73 */
167 /* 0x5d */ KEY_YEN, /* 124 */
168 /* 0x5e */ KEY_RO, /* 89 */
169 /* 0x5f */ KEY_KPCOMMA, /* 121 */
170 /* 0x60 */ KEY_F5, /* 63 */
171 /* 0x61 */ KEY_F6, /* 64 */
172 /* 0x62 */ KEY_F7, /* 65 */
173 /* 0x63 */ KEY_F3, /* 61 */
174 /* 0x64 */ KEY_F8, /* 66 */
175 /* 0x65 */ KEY_F9, /* 67 */
176 /* 0x66 */ KEY_HANJA, /* 123 */
177 /* 0x67 */ KEY_F11, /* 87 */
178 /* 0x68 */ KEY_HANGEUL, /* 122 */
179 /* 0x69 */ KEY_SYSRQ, /* 99 */
180 /* 0x6a */ 0,
181 /* 0x6b */ KEY_SCROLLLOCK, /* 70 */
182 /* 0x6c */ 0,
183 /* 0x6d */ KEY_F10, /* 68 */
184 /* 0x6e */ KEY_COMPOSE, /* 127 */
185 /* 0x6f */ KEY_F12, /* 88 */
186 /* 0x70 */ 0,
187 /* 0x71 */ KEY_PAUSE, /* 119 */
188 /* 0x72 */ KEY_INSERT, /* 110 */
189 /* 0x73 */ KEY_HOME, /* 102 */
190 /* 0x74 */ KEY_PAGEUP, /* 104 */
191 /* 0x75 */ KEY_DELETE, /* 111 */
192 /* 0x76 */ KEY_F4, /* 62 */
193 /* 0x77 */ KEY_END, /* 107 */
194 /* 0x78 */ KEY_F2, /* 60 */
195 /* 0x79 */ KEY_PAGEDOWN, /* 109 */
196 /* 0x7a */ KEY_F1, /* 59 */
197 /* 0x7b */ KEY_RIGHTSHIFT, /* 54 */
198 /* 0x7c */ KEY_RIGHTALT, /* 100 */
199 /* 0x7d */ KEY_RIGHTCTRL, /* 97 */
200 /* 0x7e */ KEY_RIGHTMETA, /* 126 */
201 /* 0x7f */ KEY_POWER, /* 116 */
202 };
203
204 struct adbhid {
205 struct input_dev *input;
206 int id;
207 int default_id;
208 int original_handler_id;
209 int current_handler_id;
210 int mouse_kind;
211 u16 *keycode;
212 char name[64];
213 char phys[32];
214 int flags;
215 };
216
217 #define FLAG_FN_KEY_PRESSED 0x00000001
218 #define FLAG_POWER_FROM_FN 0x00000002
219 #define FLAG_EMU_FWDEL_DOWN 0x00000004
220
221 static struct adbhid *adbhid[16];
222
223 static void adbhid_probe(void);
224
225 static void adbhid_input_keycode(int, int, int);
226
227 static void init_trackpad(int id);
228 static void init_trackball(int id);
229 static void init_turbomouse(int id);
230 static void init_microspeed(int id);
231 static void init_ms_a3(int id);
232
233 static struct adb_ids keyboard_ids;
234 static struct adb_ids mouse_ids;
235 static struct adb_ids buttons_ids;
236
237 /* Kind of keyboard, see Apple technote 1152 */
238 #define ADB_KEYBOARD_UNKNOWN 0
239 #define ADB_KEYBOARD_ANSI 0x0100
240 #define ADB_KEYBOARD_ISO 0x0200
241 #define ADB_KEYBOARD_JIS 0x0300
242
243 /* Kind of mouse */
244 #define ADBMOUSE_STANDARD_100 0 /* Standard 100cpi mouse (handler 1) */
245 #define ADBMOUSE_STANDARD_200 1 /* Standard 200cpi mouse (handler 2) */
246 #define ADBMOUSE_EXTENDED 2 /* Apple Extended mouse (handler 4) */
247 #define ADBMOUSE_TRACKBALL 3 /* TrackBall (handler 4) */
248 #define ADBMOUSE_TRACKPAD 4 /* Apple's PowerBook trackpad (handler 4) */
249 #define ADBMOUSE_TURBOMOUSE5 5 /* Turbomouse 5 (previously req. mousehack) */
250 #define ADBMOUSE_MICROSPEED 6 /* Microspeed mouse (&trackball ?), MacPoint */
251 #define ADBMOUSE_TRACKBALLPRO 7 /* Trackball Pro (special buttons) */
252 #define ADBMOUSE_MS_A3 8 /* Mouse systems A3 trackball (handler 3) */
253 #define ADBMOUSE_MACALLY2 9 /* MacAlly 2-button mouse */
254
255 static void
256 adbhid_keyboard_input(unsigned char *data, int nb, int apoll)
257 {
258 int id = (data[0] >> 4) & 0x0f;
259
260 if (!adbhid[id]) {
261 printk(KERN_ERR "ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
262 id, data[0], data[1], data[2], data[3]);
263 return;
264 }
265
266 /* first check this is from register 0 */
267 if (nb != 3 || (data[0] & 3) != KEYB_KEYREG)
268 return; /* ignore it */
269 adbhid_input_keycode(id, data[1], 0);
270 if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
271 adbhid_input_keycode(id, data[2], 0);
272 }
273
274 static void
275 adbhid_input_keycode(int id, int keycode, int repeat)
276 {
277 struct adbhid *ahid = adbhid[id];
278 int up_flag, key;
279
280 up_flag = (keycode & 0x80);
281 keycode &= 0x7f;
282
283 switch (keycode) {
284 case ADB_KEY_CAPSLOCK: /* Generate down/up events for CapsLock everytime. */
285 input_report_key(ahid->input, KEY_CAPSLOCK, 1);
286 input_report_key(ahid->input, KEY_CAPSLOCK, 0);
287 input_sync(ahid->input);
288 return;
289 #ifdef CONFIG_PPC_PMAC
290 case ADB_KEY_POWER_OLD: /* Power key on PBook 3400 needs remapping */
291 switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO,
292 NULL, PMAC_MB_INFO_MODEL, 0)) {
293 case PMAC_TYPE_COMET:
294 case PMAC_TYPE_HOOPER:
295 case PMAC_TYPE_KANGA:
296 keycode = ADB_KEY_POWER;
297 }
298 break;
299 case ADB_KEY_POWER:
300 /* Fn + Command will produce a bogus "power" keycode */
301 if (ahid->flags & FLAG_FN_KEY_PRESSED) {
302 keycode = ADB_KEY_CMD;
303 if (up_flag)
304 ahid->flags &= ~FLAG_POWER_FROM_FN;
305 else
306 ahid->flags |= FLAG_POWER_FROM_FN;
307 } else if (ahid->flags & FLAG_POWER_FROM_FN) {
308 keycode = ADB_KEY_CMD;
309 ahid->flags &= ~FLAG_POWER_FROM_FN;
310 }
311 break;
312 case ADB_KEY_FN:
313 /* Keep track of the Fn key state */
314 if (up_flag) {
315 ahid->flags &= ~FLAG_FN_KEY_PRESSED;
316 /* Emulate Fn+delete = forward delete */
317 if (ahid->flags & FLAG_EMU_FWDEL_DOWN) {
318 ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
319 keycode = ADB_KEY_FWDEL;
320 break;
321 }
322 } else
323 ahid->flags |= FLAG_FN_KEY_PRESSED;
324 break;
325 case ADB_KEY_DEL:
326 /* Emulate Fn+delete = forward delete */
327 if (ahid->flags & FLAG_FN_KEY_PRESSED) {
328 keycode = ADB_KEY_FWDEL;
329 if (up_flag)
330 ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
331 else
332 ahid->flags |= FLAG_EMU_FWDEL_DOWN;
333 }
334 break;
335 #endif /* CONFIG_PPC_PMAC */
336 }
337
338 key = adbhid[id]->keycode[keycode];
339 if (key) {
340 input_report_key(adbhid[id]->input, key, !up_flag);
341 input_sync(adbhid[id]->input);
342 } else
343 printk(KERN_INFO "Unhandled ADB key (scancode %#02x) %s.\n", keycode,
344 up_flag ? "released" : "pressed");
345
346 }
347
348 static void
349 adbhid_mouse_input(unsigned char *data, int nb, int autopoll)
350 {
351 int id = (data[0] >> 4) & 0x0f;
352
353 if (!adbhid[id]) {
354 printk(KERN_ERR "ADB HID on ID %d not yet registered\n", id);
355 return;
356 }
357
358 /*
359 Handler 1 -- 100cpi original Apple mouse protocol.
360 Handler 2 -- 200cpi original Apple mouse protocol.
361
362 For Apple's standard one-button mouse protocol the data array will
363 contain the following values:
364
365 BITS COMMENTS
366 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
367 data[1] = bxxx xxxx First button and x-axis motion.
368 data[2] = byyy yyyy Second button and y-axis motion.
369
370 Handler 4 -- Apple Extended mouse protocol.
371
372 For Apple's 3-button mouse protocol the data array will contain the
373 following values:
374
375 BITS COMMENTS
376 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
377 data[1] = bxxx xxxx Left button and x-axis motion.
378 data[2] = byyy yyyy Second button and y-axis motion.
379 data[3] = byyy bxxx Third button and fourth button. Y is additional
380 high bits of y-axis motion. XY is additional
381 high bits of x-axis motion.
382
383 MacAlly 2-button mouse protocol.
384
385 For MacAlly 2-button mouse protocol the data array will contain the
386 following values:
387
388 BITS COMMENTS
389 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
390 data[1] = bxxx xxxx Left button and x-axis motion.
391 data[2] = byyy yyyy Right button and y-axis motion.
392 data[3] = ???? ???? unknown
393 data[4] = ???? ???? unknown
394
395 */
396
397 /* If it's a trackpad, we alias the second button to the first.
398 NOTE: Apple sends an ADB flush command to the trackpad when
399 the first (the real) button is released. We could do
400 this here using async flush requests.
401 */
402 switch (adbhid[id]->mouse_kind)
403 {
404 case ADBMOUSE_TRACKPAD:
405 data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
406 data[2] = data[2] | 0x80;
407 break;
408 case ADBMOUSE_MICROSPEED:
409 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
410 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
411 data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
412 | (data[3] & 0x08);
413 break;
414 case ADBMOUSE_TRACKBALLPRO:
415 data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
416 & ((data[3] & 0x08) << 4));
417 data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
418 data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
419 break;
420 case ADBMOUSE_MS_A3:
421 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
422 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
423 data[3] = ((data[3] & 0x04) << 5);
424 break;
425 case ADBMOUSE_MACALLY2:
426 data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
427 data[2] |= 0x80; /* Right button is mapped as button 3 */
428 nb=4;
429 break;
430 }
431
432 input_report_key(adbhid[id]->input, BTN_LEFT, !((data[1] >> 7) & 1));
433 input_report_key(adbhid[id]->input, BTN_MIDDLE, !((data[2] >> 7) & 1));
434
435 if (nb >= 4 && adbhid[id]->mouse_kind != ADBMOUSE_TRACKPAD)
436 input_report_key(adbhid[id]->input, BTN_RIGHT, !((data[3] >> 7) & 1));
437
438 input_report_rel(adbhid[id]->input, REL_X,
439 ((data[2]&0x7f) < 64 ? (data[2]&0x7f) : (data[2]&0x7f)-128 ));
440 input_report_rel(adbhid[id]->input, REL_Y,
441 ((data[1]&0x7f) < 64 ? (data[1]&0x7f) : (data[1]&0x7f)-128 ));
442
443 input_sync(adbhid[id]->input);
444 }
445
446 static void
447 adbhid_buttons_input(unsigned char *data, int nb, int autopoll)
448 {
449 int id = (data[0] >> 4) & 0x0f;
450
451 if (!adbhid[id]) {
452 printk(KERN_ERR "ADB HID on ID %d not yet registered\n", id);
453 return;
454 }
455
456 switch (adbhid[id]->original_handler_id) {
457 default:
458 case 0x02: /* Adjustable keyboard button device */
459 {
460 int down = (data[1] == (data[1] & 0xf));
461
462 switch (data[1] & 0x0f) {
463 case 0x0: /* microphone */
464 input_report_key(adbhid[id]->input, KEY_SOUND, down);
465 break;
466
467 case 0x1: /* mute */
468 input_report_key(adbhid[id]->input, KEY_MUTE, down);
469 break;
470
471 case 0x2: /* volume decrease */
472 input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
473 break;
474
475 case 0x3: /* volume increase */
476 input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
477 break;
478
479 default:
480 printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
481 data[0], data[1], data[2], data[3]);
482 break;
483 }
484 }
485 break;
486
487 case 0x1f: /* Powerbook button device */
488 {
489 int down = (data[1] == (data[1] & 0xf));
490
491 /*
492 * XXX: Where is the contrast control for the passive?
493 * -- Cort
494 */
495
496 switch (data[1] & 0x0f) {
497 case 0x8: /* mute */
498 input_report_key(adbhid[id]->input, KEY_MUTE, down);
499 break;
500
501 case 0x7: /* volume decrease */
502 input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
503 break;
504
505 case 0x6: /* volume increase */
506 input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
507 break;
508
509 case 0xb: /* eject */
510 input_report_key(adbhid[id]->input, KEY_EJECTCD, down);
511 break;
512
513 case 0xa: /* brightness decrease */
514 #ifdef CONFIG_PMAC_BACKLIGHT
515 if (down)
516 pmac_backlight_key_down();
517 #endif
518 input_report_key(adbhid[id]->input, KEY_BRIGHTNESSDOWN, down);
519 break;
520
521 case 0x9: /* brightness increase */
522 #ifdef CONFIG_PMAC_BACKLIGHT
523 if (down)
524 pmac_backlight_key_up();
525 #endif
526 input_report_key(adbhid[id]->input, KEY_BRIGHTNESSUP, down);
527 break;
528
529 case 0xc: /* videomode switch */
530 input_report_key(adbhid[id]->input, KEY_SWITCHVIDEOMODE, down);
531 break;
532
533 case 0xd: /* keyboard illumination toggle */
534 input_report_key(adbhid[id]->input, KEY_KBDILLUMTOGGLE, down);
535 break;
536
537 case 0xe: /* keyboard illumination decrease */
538 input_report_key(adbhid[id]->input, KEY_KBDILLUMDOWN, down);
539 break;
540
541 case 0xf:
542 switch (data[1]) {
543 case 0x8f:
544 case 0x0f:
545 /* keyboard illumination increase */
546 input_report_key(adbhid[id]->input, KEY_KBDILLUMUP, down);
547 break;
548
549 case 0x7f:
550 case 0xff:
551 /* keypad overlay toogle */
552 break;
553
554 default:
555 printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
556 data[0], data[1], data[2], data[3]);
557 break;
558 }
559 break;
560 default:
561 printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
562 data[0], data[1], data[2], data[3]);
563 break;
564 }
565 }
566 break;
567 }
568
569 input_sync(adbhid[id]->input);
570 }
571
572 static struct adb_request led_request;
573 static int leds_pending[16];
574 static int leds_req_pending;
575 static int pending_devs[16];
576 static int pending_led_start;
577 static int pending_led_end;
578 static DEFINE_SPINLOCK(leds_lock);
579
580 static void leds_done(struct adb_request *req)
581 {
582 int leds = 0, device = 0, pending = 0;
583 unsigned long flags;
584
585 spin_lock_irqsave(&leds_lock, flags);
586
587 if (pending_led_start != pending_led_end) {
588 device = pending_devs[pending_led_start];
589 leds = leds_pending[device] & 0xff;
590 leds_pending[device] = 0;
591 pending_led_start++;
592 pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
593 pending = leds_req_pending;
594 } else
595 leds_req_pending = 0;
596 spin_unlock_irqrestore(&leds_lock, flags);
597 if (pending)
598 adb_request(&led_request, leds_done, 0, 3,
599 ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
600 }
601
602 static void real_leds(unsigned char leds, int device)
603 {
604 unsigned long flags;
605
606 spin_lock_irqsave(&leds_lock, flags);
607 if (!leds_req_pending) {
608 leds_req_pending = 1;
609 spin_unlock_irqrestore(&leds_lock, flags);
610 adb_request(&led_request, leds_done, 0, 3,
611 ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
612 return;
613 } else {
614 if (!(leds_pending[device] & 0x100)) {
615 pending_devs[pending_led_end] = device;
616 pending_led_end++;
617 pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
618 }
619 leds_pending[device] = leds | 0x100;
620 }
621 spin_unlock_irqrestore(&leds_lock, flags);
622 }
623
624 /*
625 * Event callback from the input module. Events that change the state of
626 * the hardware are processed here.
627 */
628 static int adbhid_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
629 {
630 struct adbhid *adbhid = input_get_drvdata(dev);
631 unsigned char leds;
632
633 switch (type) {
634 case EV_LED:
635 leds = (test_bit(LED_SCROLLL, dev->led) ? 4 : 0) |
636 (test_bit(LED_NUML, dev->led) ? 1 : 0) |
637 (test_bit(LED_CAPSL, dev->led) ? 2 : 0);
638 real_leds(leds, adbhid->id);
639 return 0;
640 }
641
642 return -1;
643 }
644
645 static int
646 adb_message_handler(struct notifier_block *this, unsigned long code, void *x)
647 {
648 switch (code) {
649 case ADB_MSG_PRE_RESET:
650 case ADB_MSG_POWERDOWN:
651 /* Stop the repeat timer. Autopoll is already off at this point */
652 {
653 int i;
654 for (i = 1; i < 16; i++) {
655 if (adbhid[i])
656 del_timer_sync(&adbhid[i]->input->timer);
657 }
658 }
659
660 /* Stop pending led requests */
661 while(leds_req_pending)
662 adb_poll();
663 break;
664
665 case ADB_MSG_POST_RESET:
666 adbhid_probe();
667 break;
668 }
669 return NOTIFY_DONE;
670 }
671
672 static int
673 adbhid_input_register(int id, int default_id, int original_handler_id,
674 int current_handler_id, int mouse_kind)
675 {
676 struct adbhid *hid;
677 struct input_dev *input_dev;
678 int err;
679 int i;
680
681 if (adbhid[id]) {
682 printk(KERN_ERR "Trying to reregister ADB HID on ID %d\n", id);
683 return -EEXIST;
684 }
685
686 adbhid[id] = hid = kzalloc(sizeof(struct adbhid), GFP_KERNEL);
687 input_dev = input_allocate_device();
688 if (!hid || !input_dev) {
689 err = -ENOMEM;
690 goto fail;
691 }
692
693 sprintf(hid->phys, "adb%d:%d.%02x/input", id, default_id, original_handler_id);
694
695 hid->input = input_dev;
696 hid->id = default_id;
697 hid->original_handler_id = original_handler_id;
698 hid->current_handler_id = current_handler_id;
699 hid->mouse_kind = mouse_kind;
700 hid->flags = 0;
701 input_set_drvdata(input_dev, hid);
702 input_dev->name = hid->name;
703 input_dev->phys = hid->phys;
704 input_dev->id.bustype = BUS_ADB;
705 input_dev->id.vendor = 0x0001;
706 input_dev->id.product = (id << 12) | (default_id << 8) | original_handler_id;
707 input_dev->id.version = 0x0100;
708
709 switch (default_id) {
710 case ADB_KEYBOARD:
711 hid->keycode = kmalloc(sizeof(adb_to_linux_keycodes), GFP_KERNEL);
712 if (!hid->keycode) {
713 err = -ENOMEM;
714 goto fail;
715 }
716
717 sprintf(hid->name, "ADB keyboard");
718
719 memcpy(hid->keycode, adb_to_linux_keycodes, sizeof(adb_to_linux_keycodes));
720
721 printk(KERN_INFO "Detected ADB keyboard, type ");
722 switch (original_handler_id) {
723 default:
724 printk("<unknown>.\n");
725 input_dev->id.version = ADB_KEYBOARD_UNKNOWN;
726 break;
727
728 case 0x01: case 0x02: case 0x03: case 0x06: case 0x08:
729 case 0x0C: case 0x10: case 0x18: case 0x1B: case 0x1C:
730 case 0xC0: case 0xC3: case 0xC6:
731 printk("ANSI.\n");
732 input_dev->id.version = ADB_KEYBOARD_ANSI;
733 break;
734
735 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
736 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
737 case 0xC4: case 0xC7:
738 printk("ISO, swapping keys.\n");
739 input_dev->id.version = ADB_KEYBOARD_ISO;
740 i = hid->keycode[10];
741 hid->keycode[10] = hid->keycode[50];
742 hid->keycode[50] = i;
743 break;
744
745 case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
746 case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
747 printk("JIS.\n");
748 input_dev->id.version = ADB_KEYBOARD_JIS;
749 break;
750 }
751
752 for (i = 0; i < 128; i++)
753 if (hid->keycode[i])
754 set_bit(hid->keycode[i], input_dev->keybit);
755
756 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);
757 input_dev->ledbit[0] = BIT(LED_SCROLLL) | BIT(LED_CAPSL) | BIT(LED_NUML);
758 input_dev->event = adbhid_kbd_event;
759 input_dev->keycodemax = KEY_FN;
760 input_dev->keycodesize = sizeof(hid->keycode[0]);
761 break;
762
763 case ADB_MOUSE:
764 sprintf(hid->name, "ADB mouse");
765
766 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
767 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
768 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
769 break;
770
771 case ADB_MISC:
772 switch (original_handler_id) {
773 case 0x02: /* Adjustable keyboard button device */
774 sprintf(hid->name, "ADB adjustable keyboard buttons");
775 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
776 set_bit(KEY_SOUND, input_dev->keybit);
777 set_bit(KEY_MUTE, input_dev->keybit);
778 set_bit(KEY_VOLUMEUP, input_dev->keybit);
779 set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
780 break;
781 case 0x1f: /* Powerbook button device */
782 sprintf(hid->name, "ADB Powerbook buttons");
783 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
784 set_bit(KEY_MUTE, input_dev->keybit);
785 set_bit(KEY_VOLUMEUP, input_dev->keybit);
786 set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
787 set_bit(KEY_BRIGHTNESSUP, input_dev->keybit);
788 set_bit(KEY_BRIGHTNESSDOWN, input_dev->keybit);
789 set_bit(KEY_EJECTCD, input_dev->keybit);
790 set_bit(KEY_SWITCHVIDEOMODE, input_dev->keybit);
791 set_bit(KEY_KBDILLUMTOGGLE, input_dev->keybit);
792 set_bit(KEY_KBDILLUMDOWN, input_dev->keybit);
793 set_bit(KEY_KBDILLUMUP, input_dev->keybit);
794 break;
795 }
796 if (hid->name[0])
797 break;
798 /* else fall through */
799
800 default:
801 printk(KERN_INFO "Trying to register unknown ADB device to input layer.\n");
802 err = -ENODEV;
803 goto fail;
804 }
805
806 input_dev->keycode = hid->keycode;
807
808 err = input_register_device(input_dev);
809 if (err)
810 goto fail;
811
812 if (default_id == ADB_KEYBOARD) {
813 /* HACK WARNING!! This should go away as soon there is an utility
814 * to control that for event devices.
815 */
816 input_dev->rep[REP_DELAY] = 500; /* input layer default: 250 */
817 input_dev->rep[REP_PERIOD] = 66; /* input layer default: 33 */
818 }
819
820 return 0;
821
822 fail: input_free_device(input_dev);
823 if (hid) {
824 kfree(hid->keycode);
825 kfree(hid);
826 }
827 adbhid[id] = NULL;
828 return err;
829 }
830
831 static void adbhid_input_unregister(int id)
832 {
833 input_unregister_device(adbhid[id]->input);
834 kfree(adbhid[id]->keycode);
835 kfree(adbhid[id]);
836 adbhid[id] = NULL;
837 }
838
839
840 static u16
841 adbhid_input_reregister(int id, int default_id, int org_handler_id,
842 int cur_handler_id, int mk)
843 {
844 if (adbhid[id]) {
845 if (adbhid[id]->input->id.product !=
846 ((id << 12)|(default_id << 8)|org_handler_id)) {
847 adbhid_input_unregister(id);
848 adbhid_input_register(id, default_id, org_handler_id,
849 cur_handler_id, mk);
850 }
851 } else
852 adbhid_input_register(id, default_id, org_handler_id,
853 cur_handler_id, mk);
854 return 1<<id;
855 }
856
857 static void
858 adbhid_input_devcleanup(u16 exist)
859 {
860 int i;
861 for(i=1; i<16; i++)
862 if (adbhid[i] && !(exist&(1<<i)))
863 adbhid_input_unregister(i);
864 }
865
866 static void
867 adbhid_probe(void)
868 {
869 struct adb_request req;
870 int i, default_id, org_handler_id, cur_handler_id;
871 u16 reg = 0;
872
873 adb_register(ADB_MOUSE, 0, &mouse_ids, adbhid_mouse_input);
874 adb_register(ADB_KEYBOARD, 0, &keyboard_ids, adbhid_keyboard_input);
875 adb_register(ADB_MISC, 0, &buttons_ids, adbhid_buttons_input);
876
877 for (i = 0; i < keyboard_ids.nids; i++) {
878 int id = keyboard_ids.id[i];
879
880 adb_get_infos(id, &default_id, &org_handler_id);
881
882 /* turn off all leds */
883 adb_request(&req, NULL, ADBREQ_SYNC, 3,
884 ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
885
886 /* Enable full feature set of the keyboard
887 ->get it to send separate codes for left and right shift,
888 control, option keys */
889 #if 0 /* handler 5 doesn't send separate codes for R modifiers */
890 if (adb_try_handler_change(id, 5))
891 printk("ADB keyboard at %d, handler set to 5\n", id);
892 else
893 #endif
894 if (adb_try_handler_change(id, 3))
895 printk("ADB keyboard at %d, handler set to 3\n", id);
896 else
897 printk("ADB keyboard at %d, handler 1\n", id);
898
899 adb_get_infos(id, &default_id, &cur_handler_id);
900 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
901 cur_handler_id, 0);
902 }
903
904 for (i = 0; i < buttons_ids.nids; i++) {
905 int id = buttons_ids.id[i];
906
907 adb_get_infos(id, &default_id, &org_handler_id);
908 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
909 org_handler_id, 0);
910 }
911
912 /* Try to switch all mice to handler 4, or 2 for three-button
913 mode and full resolution. */
914 for (i = 0; i < mouse_ids.nids; i++) {
915 int id = mouse_ids.id[i];
916 int mouse_kind;
917
918 adb_get_infos(id, &default_id, &org_handler_id);
919
920 if (adb_try_handler_change(id, 4)) {
921 printk("ADB mouse at %d, handler set to 4", id);
922 mouse_kind = ADBMOUSE_EXTENDED;
923 }
924 else if (adb_try_handler_change(id, 0x2F)) {
925 printk("ADB mouse at %d, handler set to 0x2F", id);
926 mouse_kind = ADBMOUSE_MICROSPEED;
927 }
928 else if (adb_try_handler_change(id, 0x42)) {
929 printk("ADB mouse at %d, handler set to 0x42", id);
930 mouse_kind = ADBMOUSE_TRACKBALLPRO;
931 }
932 else if (adb_try_handler_change(id, 0x66)) {
933 printk("ADB mouse at %d, handler set to 0x66", id);
934 mouse_kind = ADBMOUSE_MICROSPEED;
935 }
936 else if (adb_try_handler_change(id, 0x5F)) {
937 printk("ADB mouse at %d, handler set to 0x5F", id);
938 mouse_kind = ADBMOUSE_MICROSPEED;
939 }
940 else if (adb_try_handler_change(id, 3)) {
941 printk("ADB mouse at %d, handler set to 3", id);
942 mouse_kind = ADBMOUSE_MS_A3;
943 }
944 else if (adb_try_handler_change(id, 2)) {
945 printk("ADB mouse at %d, handler set to 2", id);
946 mouse_kind = ADBMOUSE_STANDARD_200;
947 }
948 else {
949 printk("ADB mouse at %d, handler 1", id);
950 mouse_kind = ADBMOUSE_STANDARD_100;
951 }
952
953 if ((mouse_kind == ADBMOUSE_TRACKBALLPRO)
954 || (mouse_kind == ADBMOUSE_MICROSPEED)) {
955 init_microspeed(id);
956 } else if (mouse_kind == ADBMOUSE_MS_A3) {
957 init_ms_a3(id);
958 } else if (mouse_kind == ADBMOUSE_EXTENDED) {
959 /*
960 * Register 1 is usually used for device
961 * identification. Here, we try to identify
962 * a known device and call the appropriate
963 * init function.
964 */
965 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
966 ADB_READREG(id, 1));
967
968 if ((req.reply_len) &&
969 (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
970 || (req.reply[2] == 0x20))) {
971 mouse_kind = ADBMOUSE_TRACKBALL;
972 init_trackball(id);
973 }
974 else if ((req.reply_len >= 4) &&
975 (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
976 (req.reply[3] == 0x61) && (req.reply[4] == 0x64)) {
977 mouse_kind = ADBMOUSE_TRACKPAD;
978 init_trackpad(id);
979 }
980 else if ((req.reply_len >= 4) &&
981 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
982 (req.reply[3] == 0x4c) && (req.reply[4] == 0x31)) {
983 mouse_kind = ADBMOUSE_TURBOMOUSE5;
984 init_turbomouse(id);
985 }
986 else if ((req.reply_len == 9) &&
987 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
988 (req.reply[3] == 0x49) && (req.reply[4] == 0x54)) {
989 if (adb_try_handler_change(id, 0x42)) {
990 printk("\nADB MacAlly 2-button mouse at %d, handler set to 0x42", id);
991 mouse_kind = ADBMOUSE_MACALLY2;
992 }
993 }
994 }
995 printk("\n");
996
997 adb_get_infos(id, &default_id, &cur_handler_id);
998 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
999 cur_handler_id, mouse_kind);
1000 }
1001 adbhid_input_devcleanup(reg);
1002 }
1003
1004 static void
1005 init_trackpad(int id)
1006 {
1007 struct adb_request req;
1008 unsigned char r1_buffer[8];
1009
1010 printk(" (trackpad)");
1011
1012 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
1013 ADB_READREG(id,1));
1014 if (req.reply_len < 8)
1015 printk("bad length for reg. 1\n");
1016 else
1017 {
1018 memcpy(r1_buffer, &req.reply[1], 8);
1019
1020 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1021 ADB_WRITEREG(id,1),
1022 r1_buffer[0],
1023 r1_buffer[1],
1024 r1_buffer[2],
1025 r1_buffer[3],
1026 r1_buffer[4],
1027 r1_buffer[5],
1028 0x0d,
1029 r1_buffer[7]);
1030
1031 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1032 ADB_WRITEREG(id,2),
1033 0x99,
1034 0x94,
1035 0x19,
1036 0xff,
1037 0xb2,
1038 0x8a,
1039 0x1b,
1040 0x50);
1041
1042 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1043 ADB_WRITEREG(id,1),
1044 r1_buffer[0],
1045 r1_buffer[1],
1046 r1_buffer[2],
1047 r1_buffer[3],
1048 r1_buffer[4],
1049 r1_buffer[5],
1050 0x03, /*r1_buffer[6],*/
1051 r1_buffer[7]);
1052
1053 /* Without this flush, the trackpad may be locked up */
1054 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1055 }
1056 }
1057
1058 static void
1059 init_trackball(int id)
1060 {
1061 struct adb_request req;
1062
1063 printk(" (trackman/mouseman)");
1064
1065 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1066 ADB_WRITEREG(id,1), 00,0x81);
1067
1068 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1069 ADB_WRITEREG(id,1), 01,0x81);
1070
1071 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1072 ADB_WRITEREG(id,1), 02,0x81);
1073
1074 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1075 ADB_WRITEREG(id,1), 03,0x38);
1076
1077 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1078 ADB_WRITEREG(id,1), 00,0x81);
1079
1080 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1081 ADB_WRITEREG(id,1), 01,0x81);
1082
1083 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1084 ADB_WRITEREG(id,1), 02,0x81);
1085
1086 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1087 ADB_WRITEREG(id,1), 03,0x38);
1088 }
1089
1090 static void
1091 init_turbomouse(int id)
1092 {
1093 struct adb_request req;
1094
1095 printk(" (TurboMouse 5)");
1096
1097 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1098
1099 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
1100
1101 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1102 ADB_WRITEREG(3,2),
1103 0xe7,
1104 0x8c,
1105 0,
1106 0,
1107 0,
1108 0xff,
1109 0xff,
1110 0x94);
1111
1112 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
1113
1114 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1115 ADB_WRITEREG(3,2),
1116 0xa5,
1117 0x14,
1118 0,
1119 0,
1120 0x69,
1121 0xff,
1122 0xff,
1123 0x27);
1124 }
1125
1126 static void
1127 init_microspeed(int id)
1128 {
1129 struct adb_request req;
1130
1131 printk(" (Microspeed/MacPoint or compatible)");
1132
1133 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1134
1135 /* This will initialize mice using the Microspeed, MacPoint and
1136 other compatible firmware. Bit 12 enables extended protocol.
1137
1138 Register 1 Listen (4 Bytes)
1139 0 - 3 Button is mouse (set also for double clicking!!!)
1140 4 - 7 Button is locking (affects change speed also)
1141 8 - 11 Button changes speed
1142 12 1 = Extended mouse mode, 0 = normal mouse mode
1143 13 - 15 unused 0
1144 16 - 23 normal speed
1145 24 - 31 changed speed
1146
1147 Register 1 talk holds version and product identification information.
1148 Register 1 Talk (4 Bytes):
1149 0 - 7 Product code
1150 8 - 23 undefined, reserved
1151 24 - 31 Version number
1152
1153 Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
1154 */
1155 adb_request(&req, NULL, ADBREQ_SYNC, 5,
1156 ADB_WRITEREG(id,1),
1157 0x20, /* alt speed = 0x20 (rather slow) */
1158 0x00, /* norm speed = 0x00 (fastest) */
1159 0x10, /* extended protocol, no speed change */
1160 0x07); /* all buttons enabled as mouse buttons, no locking */
1161
1162
1163 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1164 }
1165
1166 static void
1167 init_ms_a3(int id)
1168 {
1169 struct adb_request req;
1170
1171 printk(" (Mouse Systems A3 Mouse, or compatible)");
1172 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1173 ADB_WRITEREG(id, 0x2),
1174 0x00,
1175 0x07);
1176
1177 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1178 }
1179
1180 static int __init adbhid_init(void)
1181 {
1182 #ifndef CONFIG_MAC
1183 if (!machine_is(chrp) && !machine_is(powermac))
1184 return 0;
1185 #endif
1186
1187 led_request.complete = 1;
1188
1189 adbhid_probe();
1190
1191 blocking_notifier_chain_register(&adb_client_list,
1192 &adbhid_adb_notifier);
1193
1194 return 0;
1195 }
1196
1197 static void __exit adbhid_exit(void)
1198 {
1199 }
1200
1201 module_init(adbhid_init);
1202 module_exit(adbhid_exit);