2 * drivers/macintosh/adbhid.c
4 * ADB HID driver for Power Macintosh computers.
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.
10 * Copyright (C) 2000 Franz Sirl.
12 * Adapted to ADB changes and support for more devices by
13 * Benjamin Herrenschmidt. Adapted from code in MkLinux
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>
32 * Improve Kensington support.
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>
43 #include <linux/adb.h>
44 #include <linux/cuda.h>
45 #include <linux/pmu.h>
47 #include <asm/machdep.h>
48 #ifdef CONFIG_PPC_PMAC
49 #include <asm/backlight.h>
50 #include <asm/pmac_feature.h>
53 MODULE_AUTHOR("Franz Sirl <Franz.Sirl-kernel@lauterbach.com>");
55 static int restore_capslock_events
;
56 module_param(restore_capslock_events
, int, 0644);
57 MODULE_PARM_DESC(restore_capslock_events
,
58 "Produce keypress events for capslock on both keyup and keydown.");
60 #define KEYB_KEYREG 0 /* register # for key up/down data */
61 #define KEYB_LEDREG 2 /* register # for leds on ADB keyboard */
62 #define MOUSE_DATAREG 0 /* reg# for movement/button codes from mouse */
64 static int adb_message_handler(struct notifier_block
*, unsigned long, void *);
65 static struct notifier_block adbhid_adb_notifier
= {
66 .notifier_call
= adb_message_handler
,
69 /* Some special keys */
70 #define ADB_KEY_DEL 0x33
71 #define ADB_KEY_CMD 0x37
72 #define ADB_KEY_CAPSLOCK 0x39
73 #define ADB_KEY_FN 0x3f
74 #define ADB_KEY_FWDEL 0x75
75 #define ADB_KEY_POWER_OLD 0x7e
76 #define ADB_KEY_POWER 0x7f
78 u16 adb_to_linux_keycodes
[128] = {
79 /* 0x00 */ KEY_A
, /* 30 */
80 /* 0x01 */ KEY_S
, /* 31 */
81 /* 0x02 */ KEY_D
, /* 32 */
82 /* 0x03 */ KEY_F
, /* 33 */
83 /* 0x04 */ KEY_H
, /* 35 */
84 /* 0x05 */ KEY_G
, /* 34 */
85 /* 0x06 */ KEY_Z
, /* 44 */
86 /* 0x07 */ KEY_X
, /* 45 */
87 /* 0x08 */ KEY_C
, /* 46 */
88 /* 0x09 */ KEY_V
, /* 47 */
89 /* 0x0a */ KEY_102ND
, /* 86 */
90 /* 0x0b */ KEY_B
, /* 48 */
91 /* 0x0c */ KEY_Q
, /* 16 */
92 /* 0x0d */ KEY_W
, /* 17 */
93 /* 0x0e */ KEY_E
, /* 18 */
94 /* 0x0f */ KEY_R
, /* 19 */
95 /* 0x10 */ KEY_Y
, /* 21 */
96 /* 0x11 */ KEY_T
, /* 20 */
97 /* 0x12 */ KEY_1
, /* 2 */
98 /* 0x13 */ KEY_2
, /* 3 */
99 /* 0x14 */ KEY_3
, /* 4 */
100 /* 0x15 */ KEY_4
, /* 5 */
101 /* 0x16 */ KEY_6
, /* 7 */
102 /* 0x17 */ KEY_5
, /* 6 */
103 /* 0x18 */ KEY_EQUAL
, /* 13 */
104 /* 0x19 */ KEY_9
, /* 10 */
105 /* 0x1a */ KEY_7
, /* 8 */
106 /* 0x1b */ KEY_MINUS
, /* 12 */
107 /* 0x1c */ KEY_8
, /* 9 */
108 /* 0x1d */ KEY_0
, /* 11 */
109 /* 0x1e */ KEY_RIGHTBRACE
, /* 27 */
110 /* 0x1f */ KEY_O
, /* 24 */
111 /* 0x20 */ KEY_U
, /* 22 */
112 /* 0x21 */ KEY_LEFTBRACE
, /* 26 */
113 /* 0x22 */ KEY_I
, /* 23 */
114 /* 0x23 */ KEY_P
, /* 25 */
115 /* 0x24 */ KEY_ENTER
, /* 28 */
116 /* 0x25 */ KEY_L
, /* 38 */
117 /* 0x26 */ KEY_J
, /* 36 */
118 /* 0x27 */ KEY_APOSTROPHE
, /* 40 */
119 /* 0x28 */ KEY_K
, /* 37 */
120 /* 0x29 */ KEY_SEMICOLON
, /* 39 */
121 /* 0x2a */ KEY_BACKSLASH
, /* 43 */
122 /* 0x2b */ KEY_COMMA
, /* 51 */
123 /* 0x2c */ KEY_SLASH
, /* 53 */
124 /* 0x2d */ KEY_N
, /* 49 */
125 /* 0x2e */ KEY_M
, /* 50 */
126 /* 0x2f */ KEY_DOT
, /* 52 */
127 /* 0x30 */ KEY_TAB
, /* 15 */
128 /* 0x31 */ KEY_SPACE
, /* 57 */
129 /* 0x32 */ KEY_GRAVE
, /* 41 */
130 /* 0x33 */ KEY_BACKSPACE
, /* 14 */
131 /* 0x34 */ KEY_KPENTER
, /* 96 */
132 /* 0x35 */ KEY_ESC
, /* 1 */
133 /* 0x36 */ KEY_LEFTCTRL
, /* 29 */
134 /* 0x37 */ KEY_LEFTMETA
, /* 125 */
135 /* 0x38 */ KEY_LEFTSHIFT
, /* 42 */
136 /* 0x39 */ KEY_CAPSLOCK
, /* 58 */
137 /* 0x3a */ KEY_LEFTALT
, /* 56 */
138 /* 0x3b */ KEY_LEFT
, /* 105 */
139 /* 0x3c */ KEY_RIGHT
, /* 106 */
140 /* 0x3d */ KEY_DOWN
, /* 108 */
141 /* 0x3e */ KEY_UP
, /* 103 */
142 /* 0x3f */ KEY_FN
, /* 0x1d0 */
144 /* 0x41 */ KEY_KPDOT
, /* 83 */
146 /* 0x43 */ KEY_KPASTERISK
, /* 55 */
148 /* 0x45 */ KEY_KPPLUS
, /* 78 */
150 /* 0x47 */ KEY_NUMLOCK
, /* 69 */
154 /* 0x4b */ KEY_KPSLASH
, /* 98 */
155 /* 0x4c */ KEY_KPENTER
, /* 96 */
157 /* 0x4e */ KEY_KPMINUS
, /* 74 */
160 /* 0x51 */ KEY_KPEQUAL
, /* 117 */
161 /* 0x52 */ KEY_KP0
, /* 82 */
162 /* 0x53 */ KEY_KP1
, /* 79 */
163 /* 0x54 */ KEY_KP2
, /* 80 */
164 /* 0x55 */ KEY_KP3
, /* 81 */
165 /* 0x56 */ KEY_KP4
, /* 75 */
166 /* 0x57 */ KEY_KP5
, /* 76 */
167 /* 0x58 */ KEY_KP6
, /* 77 */
168 /* 0x59 */ KEY_KP7
, /* 71 */
170 /* 0x5b */ KEY_KP8
, /* 72 */
171 /* 0x5c */ KEY_KP9
, /* 73 */
172 /* 0x5d */ KEY_YEN
, /* 124 */
173 /* 0x5e */ KEY_RO
, /* 89 */
174 /* 0x5f */ KEY_KPCOMMA
, /* 121 */
175 /* 0x60 */ KEY_F5
, /* 63 */
176 /* 0x61 */ KEY_F6
, /* 64 */
177 /* 0x62 */ KEY_F7
, /* 65 */
178 /* 0x63 */ KEY_F3
, /* 61 */
179 /* 0x64 */ KEY_F8
, /* 66 */
180 /* 0x65 */ KEY_F9
, /* 67 */
181 /* 0x66 */ KEY_HANJA
, /* 123 */
182 /* 0x67 */ KEY_F11
, /* 87 */
183 /* 0x68 */ KEY_HANGEUL
, /* 122 */
184 /* 0x69 */ KEY_SYSRQ
, /* 99 */
186 /* 0x6b */ KEY_SCROLLLOCK
, /* 70 */
188 /* 0x6d */ KEY_F10
, /* 68 */
189 /* 0x6e */ KEY_COMPOSE
, /* 127 */
190 /* 0x6f */ KEY_F12
, /* 88 */
192 /* 0x71 */ KEY_PAUSE
, /* 119 */
193 /* 0x72 */ KEY_INSERT
, /* 110 */
194 /* 0x73 */ KEY_HOME
, /* 102 */
195 /* 0x74 */ KEY_PAGEUP
, /* 104 */
196 /* 0x75 */ KEY_DELETE
, /* 111 */
197 /* 0x76 */ KEY_F4
, /* 62 */
198 /* 0x77 */ KEY_END
, /* 107 */
199 /* 0x78 */ KEY_F2
, /* 60 */
200 /* 0x79 */ KEY_PAGEDOWN
, /* 109 */
201 /* 0x7a */ KEY_F1
, /* 59 */
202 /* 0x7b */ KEY_RIGHTSHIFT
, /* 54 */
203 /* 0x7c */ KEY_RIGHTALT
, /* 100 */
204 /* 0x7d */ KEY_RIGHTCTRL
, /* 97 */
205 /* 0x7e */ KEY_RIGHTMETA
, /* 126 */
206 /* 0x7f */ KEY_POWER
, /* 116 */
210 struct input_dev
*input
;
213 int original_handler_id
;
214 int current_handler_id
;
222 #define FLAG_FN_KEY_PRESSED 0x00000001
223 #define FLAG_POWER_FROM_FN 0x00000002
224 #define FLAG_EMU_FWDEL_DOWN 0x00000004
225 #define FLAG_CAPSLOCK_TRANSLATE 0x00000008
226 #define FLAG_CAPSLOCK_DOWN 0x00000010
227 #define FLAG_CAPSLOCK_IGNORE_NEXT 0x00000020
229 static struct adbhid
*adbhid
[16];
231 static void adbhid_probe(void);
233 static void adbhid_input_keycode(int, int, int);
235 static void init_trackpad(int id
);
236 static void init_trackball(int id
);
237 static void init_turbomouse(int id
);
238 static void init_microspeed(int id
);
239 static void init_ms_a3(int id
);
241 static struct adb_ids keyboard_ids
;
242 static struct adb_ids mouse_ids
;
243 static struct adb_ids buttons_ids
;
245 /* Kind of keyboard, see Apple technote 1152 */
246 #define ADB_KEYBOARD_UNKNOWN 0
247 #define ADB_KEYBOARD_ANSI 0x0100
248 #define ADB_KEYBOARD_ISO 0x0200
249 #define ADB_KEYBOARD_JIS 0x0300
252 #define ADBMOUSE_STANDARD_100 0 /* Standard 100cpi mouse (handler 1) */
253 #define ADBMOUSE_STANDARD_200 1 /* Standard 200cpi mouse (handler 2) */
254 #define ADBMOUSE_EXTENDED 2 /* Apple Extended mouse (handler 4) */
255 #define ADBMOUSE_TRACKBALL 3 /* TrackBall (handler 4) */
256 #define ADBMOUSE_TRACKPAD 4 /* Apple's PowerBook trackpad (handler 4) */
257 #define ADBMOUSE_TURBOMOUSE5 5 /* Turbomouse 5 (previously req. mousehack) */
258 #define ADBMOUSE_MICROSPEED 6 /* Microspeed mouse (&trackball ?), MacPoint */
259 #define ADBMOUSE_TRACKBALLPRO 7 /* Trackball Pro (special buttons) */
260 #define ADBMOUSE_MS_A3 8 /* Mouse systems A3 trackball (handler 3) */
261 #define ADBMOUSE_MACALLY2 9 /* MacAlly 2-button mouse */
264 adbhid_keyboard_input(unsigned char *data
, int nb
, int apoll
)
266 int id
= (data
[0] >> 4) & 0x0f;
269 printk(KERN_ERR
"ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
270 id
, data
[0], data
[1], data
[2], data
[3]);
274 /* first check this is from register 0 */
275 if (nb
!= 3 || (data
[0] & 3) != KEYB_KEYREG
)
276 return; /* ignore it */
277 adbhid_input_keycode(id
, data
[1], 0);
278 if (!(data
[2] == 0xff || (data
[2] == 0x7f && data
[1] == 0x7f)))
279 adbhid_input_keycode(id
, data
[2], 0);
283 adbhid_input_keycode(int id
, int scancode
, int repeat
)
285 struct adbhid
*ahid
= adbhid
[id
];
286 int keycode
, up_flag
, key
;
288 keycode
= scancode
& 0x7f;
289 up_flag
= scancode
& 0x80;
291 if (restore_capslock_events
) {
292 if (keycode
== ADB_KEY_CAPSLOCK
&& !up_flag
) {
293 /* Key pressed, turning on the CapsLock LED.
294 * The next 0xff will be interpreted as a release. */
295 if (ahid
->flags
& FLAG_CAPSLOCK_IGNORE_NEXT
) {
296 /* Throw away this key event if it happens
297 * just after resume. */
298 ahid
->flags
&= ~FLAG_CAPSLOCK_IGNORE_NEXT
;
301 ahid
->flags
|= FLAG_CAPSLOCK_TRANSLATE
302 | FLAG_CAPSLOCK_DOWN
;
304 } else if (scancode
== 0xff) {
305 /* Scancode 0xff usually signifies that the capslock
306 * key was either pressed or released. */
307 if (ahid
->flags
& FLAG_CAPSLOCK_TRANSLATE
) {
308 keycode
= ADB_KEY_CAPSLOCK
;
309 if (ahid
->flags
& FLAG_CAPSLOCK_DOWN
) {
312 ahid
->flags
&= ~FLAG_CAPSLOCK_DOWN
;
316 ahid
->flags
&= ~FLAG_CAPSLOCK_TRANSLATE
;
319 printk(KERN_INFO
"Spurious caps lock event "
326 case ADB_KEY_CAPSLOCK
:
327 if (!restore_capslock_events
) {
328 /* Generate down/up events for CapsLock everytime. */
329 input_report_key(ahid
->input
, KEY_CAPSLOCK
, 1);
330 input_sync(ahid
->input
);
331 input_report_key(ahid
->input
, KEY_CAPSLOCK
, 0);
332 input_sync(ahid
->input
);
336 #ifdef CONFIG_PPC_PMAC
337 case ADB_KEY_POWER_OLD
: /* Power key on PBook 3400 needs remapping */
338 switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
339 NULL
, PMAC_MB_INFO_MODEL
, 0)) {
340 case PMAC_TYPE_COMET
:
341 case PMAC_TYPE_HOOPER
:
342 case PMAC_TYPE_KANGA
:
343 keycode
= ADB_KEY_POWER
;
347 /* Fn + Command will produce a bogus "power" keycode */
348 if (ahid
->flags
& FLAG_FN_KEY_PRESSED
) {
349 keycode
= ADB_KEY_CMD
;
351 ahid
->flags
&= ~FLAG_POWER_FROM_FN
;
353 ahid
->flags
|= FLAG_POWER_FROM_FN
;
354 } else if (ahid
->flags
& FLAG_POWER_FROM_FN
) {
355 keycode
= ADB_KEY_CMD
;
356 ahid
->flags
&= ~FLAG_POWER_FROM_FN
;
360 /* Keep track of the Fn key state */
362 ahid
->flags
&= ~FLAG_FN_KEY_PRESSED
;
363 /* Emulate Fn+delete = forward delete */
364 if (ahid
->flags
& FLAG_EMU_FWDEL_DOWN
) {
365 ahid
->flags
&= ~FLAG_EMU_FWDEL_DOWN
;
366 keycode
= ADB_KEY_FWDEL
;
370 ahid
->flags
|= FLAG_FN_KEY_PRESSED
;
373 /* Emulate Fn+delete = forward delete */
374 if (ahid
->flags
& FLAG_FN_KEY_PRESSED
) {
375 keycode
= ADB_KEY_FWDEL
;
377 ahid
->flags
&= ~FLAG_EMU_FWDEL_DOWN
;
379 ahid
->flags
|= FLAG_EMU_FWDEL_DOWN
;
382 #endif /* CONFIG_PPC_PMAC */
385 key
= adbhid
[id
]->keycode
[keycode
];
387 input_report_key(adbhid
[id
]->input
, key
, !up_flag
);
388 input_sync(adbhid
[id
]->input
);
390 printk(KERN_INFO
"Unhandled ADB key (scancode %#02x) %s.\n", keycode
,
391 up_flag
? "released" : "pressed");
396 adbhid_mouse_input(unsigned char *data
, int nb
, int autopoll
)
398 int id
= (data
[0] >> 4) & 0x0f;
401 printk(KERN_ERR
"ADB HID on ID %d not yet registered\n", id
);
406 Handler 1 -- 100cpi original Apple mouse protocol.
407 Handler 2 -- 200cpi original Apple mouse protocol.
409 For Apple's standard one-button mouse protocol the data array will
410 contain the following values:
413 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
414 data[1] = bxxx xxxx First button and x-axis motion.
415 data[2] = byyy yyyy Second button and y-axis motion.
417 Handler 4 -- Apple Extended mouse protocol.
419 For Apple's 3-button mouse protocol the data array will contain the
423 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
424 data[1] = bxxx xxxx Left button and x-axis motion.
425 data[2] = byyy yyyy Second button and y-axis motion.
426 data[3] = byyy bxxx Third button and fourth button. Y is additional
427 high bits of y-axis motion. XY is additional
428 high bits of x-axis motion.
430 MacAlly 2-button mouse protocol.
432 For MacAlly 2-button mouse protocol the data array will contain the
436 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
437 data[1] = bxxx xxxx Left button and x-axis motion.
438 data[2] = byyy yyyy Right button and y-axis motion.
439 data[3] = ???? ???? unknown
440 data[4] = ???? ???? unknown
444 /* If it's a trackpad, we alias the second button to the first.
445 NOTE: Apple sends an ADB flush command to the trackpad when
446 the first (the real) button is released. We could do
447 this here using async flush requests.
449 switch (adbhid
[id
]->mouse_kind
)
451 case ADBMOUSE_TRACKPAD
:
452 data
[1] = (data
[1] & 0x7f) | ((data
[1] & data
[2]) & 0x80);
453 data
[2] = data
[2] | 0x80;
455 case ADBMOUSE_MICROSPEED
:
456 data
[1] = (data
[1] & 0x7f) | ((data
[3] & 0x01) << 7);
457 data
[2] = (data
[2] & 0x7f) | ((data
[3] & 0x02) << 6);
458 data
[3] = (data
[3] & 0x77) | ((data
[3] & 0x04) << 5)
461 case ADBMOUSE_TRACKBALLPRO
:
462 data
[1] = (data
[1] & 0x7f) | (((data
[3] & 0x04) << 5)
463 & ((data
[3] & 0x08) << 4));
464 data
[2] = (data
[2] & 0x7f) | ((data
[3] & 0x01) << 7);
465 data
[3] = (data
[3] & 0x77) | ((data
[3] & 0x02) << 6);
468 data
[1] = (data
[1] & 0x7f) | ((data
[3] & 0x01) << 7);
469 data
[2] = (data
[2] & 0x7f) | ((data
[3] & 0x02) << 6);
470 data
[3] = ((data
[3] & 0x04) << 5);
472 case ADBMOUSE_MACALLY2
:
473 data
[3] = (data
[2] & 0x80) ? 0x80 : 0x00;
474 data
[2] |= 0x80; /* Right button is mapped as button 3 */
479 input_report_key(adbhid
[id
]->input
, BTN_LEFT
, !((data
[1] >> 7) & 1));
480 input_report_key(adbhid
[id
]->input
, BTN_MIDDLE
, !((data
[2] >> 7) & 1));
482 if (nb
>= 4 && adbhid
[id
]->mouse_kind
!= ADBMOUSE_TRACKPAD
)
483 input_report_key(adbhid
[id
]->input
, BTN_RIGHT
, !((data
[3] >> 7) & 1));
485 input_report_rel(adbhid
[id
]->input
, REL_X
,
486 ((data
[2]&0x7f) < 64 ? (data
[2]&0x7f) : (data
[2]&0x7f)-128 ));
487 input_report_rel(adbhid
[id
]->input
, REL_Y
,
488 ((data
[1]&0x7f) < 64 ? (data
[1]&0x7f) : (data
[1]&0x7f)-128 ));
490 input_sync(adbhid
[id
]->input
);
494 adbhid_buttons_input(unsigned char *data
, int nb
, int autopoll
)
496 int id
= (data
[0] >> 4) & 0x0f;
499 printk(KERN_ERR
"ADB HID on ID %d not yet registered\n", id
);
503 switch (adbhid
[id
]->original_handler_id
) {
505 case 0x02: /* Adjustable keyboard button device */
507 int down
= (data
[1] == (data
[1] & 0xf));
509 switch (data
[1] & 0x0f) {
510 case 0x0: /* microphone */
511 input_report_key(adbhid
[id
]->input
, KEY_SOUND
, down
);
515 input_report_key(adbhid
[id
]->input
, KEY_MUTE
, down
);
518 case 0x2: /* volume decrease */
519 input_report_key(adbhid
[id
]->input
, KEY_VOLUMEDOWN
, down
);
522 case 0x3: /* volume increase */
523 input_report_key(adbhid
[id
]->input
, KEY_VOLUMEUP
, down
);
527 printk(KERN_INFO
"Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
528 data
[0], data
[1], data
[2], data
[3]);
534 case 0x1f: /* Powerbook button device */
536 int down
= (data
[1] == (data
[1] & 0xf));
539 * XXX: Where is the contrast control for the passive?
543 switch (data
[1] & 0x0f) {
545 input_report_key(adbhid
[id
]->input
, KEY_MUTE
, down
);
548 case 0x7: /* volume decrease */
549 input_report_key(adbhid
[id
]->input
, KEY_VOLUMEDOWN
, down
);
552 case 0x6: /* volume increase */
553 input_report_key(adbhid
[id
]->input
, KEY_VOLUMEUP
, down
);
556 case 0xb: /* eject */
557 input_report_key(adbhid
[id
]->input
, KEY_EJECTCD
, down
);
560 case 0xa: /* brightness decrease */
561 #ifdef CONFIG_PMAC_BACKLIGHT
563 pmac_backlight_key_down();
565 input_report_key(adbhid
[id
]->input
, KEY_BRIGHTNESSDOWN
, down
);
568 case 0x9: /* brightness increase */
569 #ifdef CONFIG_PMAC_BACKLIGHT
571 pmac_backlight_key_up();
573 input_report_key(adbhid
[id
]->input
, KEY_BRIGHTNESSUP
, down
);
576 case 0xc: /* videomode switch */
577 input_report_key(adbhid
[id
]->input
, KEY_SWITCHVIDEOMODE
, down
);
580 case 0xd: /* keyboard illumination toggle */
581 input_report_key(adbhid
[id
]->input
, KEY_KBDILLUMTOGGLE
, down
);
584 case 0xe: /* keyboard illumination decrease */
585 input_report_key(adbhid
[id
]->input
, KEY_KBDILLUMDOWN
, down
);
592 /* keyboard illumination increase */
593 input_report_key(adbhid
[id
]->input
, KEY_KBDILLUMUP
, down
);
598 /* keypad overlay toogle */
602 printk(KERN_INFO
"Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
603 data
[0], data
[1], data
[2], data
[3]);
608 printk(KERN_INFO
"Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
609 data
[0], data
[1], data
[2], data
[3]);
616 input_sync(adbhid
[id
]->input
);
619 static struct adb_request led_request
;
620 static int leds_pending
[16];
621 static int leds_req_pending
;
622 static int pending_devs
[16];
623 static int pending_led_start
;
624 static int pending_led_end
;
625 static DEFINE_SPINLOCK(leds_lock
);
627 static void leds_done(struct adb_request
*req
)
629 int leds
= 0, device
= 0, pending
= 0;
632 spin_lock_irqsave(&leds_lock
, flags
);
634 if (pending_led_start
!= pending_led_end
) {
635 device
= pending_devs
[pending_led_start
];
636 leds
= leds_pending
[device
] & 0xff;
637 leds_pending
[device
] = 0;
639 pending_led_start
= (pending_led_start
< 16) ? pending_led_start
: 0;
640 pending
= leds_req_pending
;
642 leds_req_pending
= 0;
643 spin_unlock_irqrestore(&leds_lock
, flags
);
645 adb_request(&led_request
, leds_done
, 0, 3,
646 ADB_WRITEREG(device
, KEYB_LEDREG
), 0xff, ~leds
);
649 static void real_leds(unsigned char leds
, int device
)
653 spin_lock_irqsave(&leds_lock
, flags
);
654 if (!leds_req_pending
) {
655 leds_req_pending
= 1;
656 spin_unlock_irqrestore(&leds_lock
, flags
);
657 adb_request(&led_request
, leds_done
, 0, 3,
658 ADB_WRITEREG(device
, KEYB_LEDREG
), 0xff, ~leds
);
661 if (!(leds_pending
[device
] & 0x100)) {
662 pending_devs
[pending_led_end
] = device
;
664 pending_led_end
= (pending_led_end
< 16) ? pending_led_end
: 0;
666 leds_pending
[device
] = leds
| 0x100;
668 spin_unlock_irqrestore(&leds_lock
, flags
);
672 * Event callback from the input module. Events that change the state of
673 * the hardware are processed here.
675 static int adbhid_kbd_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
677 struct adbhid
*adbhid
= input_get_drvdata(dev
);
682 leds
= (test_bit(LED_SCROLLL
, dev
->led
) ? 4 : 0) |
683 (test_bit(LED_NUML
, dev
->led
) ? 1 : 0) |
684 (test_bit(LED_CAPSL
, dev
->led
) ? 2 : 0);
685 real_leds(leds
, adbhid
->id
);
693 adbhid_kbd_capslock_remember(void)
698 for (i
= 1; i
< 16; i
++) {
701 if (ahid
&& ahid
->id
== ADB_KEYBOARD
)
702 if (ahid
->flags
& FLAG_CAPSLOCK_TRANSLATE
)
703 ahid
->flags
|= FLAG_CAPSLOCK_IGNORE_NEXT
;
708 adb_message_handler(struct notifier_block
*this, unsigned long code
, void *x
)
711 case ADB_MSG_PRE_RESET
:
712 case ADB_MSG_POWERDOWN
:
713 /* Stop the repeat timer. Autopoll is already off at this point */
716 for (i
= 1; i
< 16; i
++) {
718 del_timer_sync(&adbhid
[i
]->input
->timer
);
722 /* Stop pending led requests */
723 while (leds_req_pending
)
726 /* After resume, and if the capslock LED is on, the PMU will
727 * send a "capslock down" key event. This confuses the
728 * restore_capslock_events logic. Remember if the capslock
729 * LED was on before suspend so the unwanted key event can
730 * be ignored after resume. */
731 if (restore_capslock_events
)
732 adbhid_kbd_capslock_remember();
736 case ADB_MSG_POST_RESET
:
744 adbhid_input_register(int id
, int default_id
, int original_handler_id
,
745 int current_handler_id
, int mouse_kind
)
748 struct input_dev
*input_dev
;
753 printk(KERN_ERR
"Trying to reregister ADB HID on ID %d\n", id
);
757 adbhid
[id
] = hid
= kzalloc(sizeof(struct adbhid
), GFP_KERNEL
);
758 input_dev
= input_allocate_device();
759 if (!hid
|| !input_dev
) {
764 sprintf(hid
->phys
, "adb%d:%d.%02x/input", id
, default_id
, original_handler_id
);
766 hid
->input
= input_dev
;
767 hid
->id
= default_id
;
768 hid
->original_handler_id
= original_handler_id
;
769 hid
->current_handler_id
= current_handler_id
;
770 hid
->mouse_kind
= mouse_kind
;
772 input_set_drvdata(input_dev
, hid
);
773 input_dev
->name
= hid
->name
;
774 input_dev
->phys
= hid
->phys
;
775 input_dev
->id
.bustype
= BUS_ADB
;
776 input_dev
->id
.vendor
= 0x0001;
777 input_dev
->id
.product
= (id
<< 12) | (default_id
<< 8) | original_handler_id
;
778 input_dev
->id
.version
= 0x0100;
780 switch (default_id
) {
782 hid
->keycode
= kmalloc(sizeof(adb_to_linux_keycodes
), GFP_KERNEL
);
788 sprintf(hid
->name
, "ADB keyboard");
790 memcpy(hid
->keycode
, adb_to_linux_keycodes
, sizeof(adb_to_linux_keycodes
));
792 printk(KERN_INFO
"Detected ADB keyboard, type ");
793 switch (original_handler_id
) {
795 printk("<unknown>.\n");
796 input_dev
->id
.version
= ADB_KEYBOARD_UNKNOWN
;
799 case 0x01: case 0x02: case 0x03: case 0x06: case 0x08:
800 case 0x0C: case 0x10: case 0x18: case 0x1B: case 0x1C:
801 case 0xC0: case 0xC3: case 0xC6:
803 input_dev
->id
.version
= ADB_KEYBOARD_ANSI
;
806 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
807 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
808 case 0xC4: case 0xC7:
809 printk("ISO, swapping keys.\n");
810 input_dev
->id
.version
= ADB_KEYBOARD_ISO
;
811 i
= hid
->keycode
[10];
812 hid
->keycode
[10] = hid
->keycode
[50];
813 hid
->keycode
[50] = i
;
816 case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
817 case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
819 input_dev
->id
.version
= ADB_KEYBOARD_JIS
;
823 for (i
= 0; i
< 128; i
++)
825 set_bit(hid
->keycode
[i
], input_dev
->keybit
);
827 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_LED
) |
829 input_dev
->ledbit
[0] = BIT_MASK(LED_SCROLLL
) |
830 BIT_MASK(LED_CAPSL
) | BIT_MASK(LED_NUML
);
831 input_dev
->event
= adbhid_kbd_event
;
832 input_dev
->keycodemax
= KEY_FN
;
833 input_dev
->keycodesize
= sizeof(hid
->keycode
[0]);
837 sprintf(hid
->name
, "ADB mouse");
839 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
840 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
841 BIT_MASK(BTN_MIDDLE
) | BIT_MASK(BTN_RIGHT
);
842 input_dev
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
846 switch (original_handler_id
) {
847 case 0x02: /* Adjustable keyboard button device */
848 sprintf(hid
->name
, "ADB adjustable keyboard buttons");
849 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) |
851 set_bit(KEY_SOUND
, input_dev
->keybit
);
852 set_bit(KEY_MUTE
, input_dev
->keybit
);
853 set_bit(KEY_VOLUMEUP
, input_dev
->keybit
);
854 set_bit(KEY_VOLUMEDOWN
, input_dev
->keybit
);
856 case 0x1f: /* Powerbook button device */
857 sprintf(hid
->name
, "ADB Powerbook buttons");
858 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) |
860 set_bit(KEY_MUTE
, input_dev
->keybit
);
861 set_bit(KEY_VOLUMEUP
, input_dev
->keybit
);
862 set_bit(KEY_VOLUMEDOWN
, input_dev
->keybit
);
863 set_bit(KEY_BRIGHTNESSUP
, input_dev
->keybit
);
864 set_bit(KEY_BRIGHTNESSDOWN
, input_dev
->keybit
);
865 set_bit(KEY_EJECTCD
, input_dev
->keybit
);
866 set_bit(KEY_SWITCHVIDEOMODE
, input_dev
->keybit
);
867 set_bit(KEY_KBDILLUMTOGGLE
, input_dev
->keybit
);
868 set_bit(KEY_KBDILLUMDOWN
, input_dev
->keybit
);
869 set_bit(KEY_KBDILLUMUP
, input_dev
->keybit
);
874 /* else fall through */
877 printk(KERN_INFO
"Trying to register unknown ADB device to input layer.\n");
882 input_dev
->keycode
= hid
->keycode
;
884 err
= input_register_device(input_dev
);
888 if (default_id
== ADB_KEYBOARD
) {
889 /* HACK WARNING!! This should go away as soon there is an utility
890 * to control that for event devices.
892 input_dev
->rep
[REP_DELAY
] = 500; /* input layer default: 250 */
893 input_dev
->rep
[REP_PERIOD
] = 66; /* input layer default: 33 */
898 fail
: input_free_device(input_dev
);
907 static void adbhid_input_unregister(int id
)
909 input_unregister_device(adbhid
[id
]->input
);
910 kfree(adbhid
[id
]->keycode
);
917 adbhid_input_reregister(int id
, int default_id
, int org_handler_id
,
918 int cur_handler_id
, int mk
)
921 if (adbhid
[id
]->input
->id
.product
!=
922 ((id
<< 12)|(default_id
<< 8)|org_handler_id
)) {
923 adbhid_input_unregister(id
);
924 adbhid_input_register(id
, default_id
, org_handler_id
,
928 adbhid_input_register(id
, default_id
, org_handler_id
,
934 adbhid_input_devcleanup(u16 exist
)
938 if (adbhid
[i
] && !(exist
&(1<<i
)))
939 adbhid_input_unregister(i
);
945 struct adb_request req
;
946 int i
, default_id
, org_handler_id
, cur_handler_id
;
949 adb_register(ADB_MOUSE
, 0, &mouse_ids
, adbhid_mouse_input
);
950 adb_register(ADB_KEYBOARD
, 0, &keyboard_ids
, adbhid_keyboard_input
);
951 adb_register(ADB_MISC
, 0, &buttons_ids
, adbhid_buttons_input
);
953 for (i
= 0; i
< keyboard_ids
.nids
; i
++) {
954 int id
= keyboard_ids
.id
[i
];
956 adb_get_infos(id
, &default_id
, &org_handler_id
);
958 /* turn off all leds */
959 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
960 ADB_WRITEREG(id
, KEYB_LEDREG
), 0xff, 0xff);
962 /* Enable full feature set of the keyboard
963 ->get it to send separate codes for left and right shift,
964 control, option keys */
965 #if 0 /* handler 5 doesn't send separate codes for R modifiers */
966 if (adb_try_handler_change(id
, 5))
967 printk("ADB keyboard at %d, handler set to 5\n", id
);
970 if (adb_try_handler_change(id
, 3))
971 printk("ADB keyboard at %d, handler set to 3\n", id
);
973 printk("ADB keyboard at %d, handler 1\n", id
);
975 adb_get_infos(id
, &default_id
, &cur_handler_id
);
976 reg
|= adbhid_input_reregister(id
, default_id
, org_handler_id
,
980 for (i
= 0; i
< buttons_ids
.nids
; i
++) {
981 int id
= buttons_ids
.id
[i
];
983 adb_get_infos(id
, &default_id
, &org_handler_id
);
984 reg
|= adbhid_input_reregister(id
, default_id
, org_handler_id
,
988 /* Try to switch all mice to handler 4, or 2 for three-button
989 mode and full resolution. */
990 for (i
= 0; i
< mouse_ids
.nids
; i
++) {
991 int id
= mouse_ids
.id
[i
];
994 adb_get_infos(id
, &default_id
, &org_handler_id
);
996 if (adb_try_handler_change(id
, 4)) {
997 printk("ADB mouse at %d, handler set to 4", id
);
998 mouse_kind
= ADBMOUSE_EXTENDED
;
1000 else if (adb_try_handler_change(id
, 0x2F)) {
1001 printk("ADB mouse at %d, handler set to 0x2F", id
);
1002 mouse_kind
= ADBMOUSE_MICROSPEED
;
1004 else if (adb_try_handler_change(id
, 0x42)) {
1005 printk("ADB mouse at %d, handler set to 0x42", id
);
1006 mouse_kind
= ADBMOUSE_TRACKBALLPRO
;
1008 else if (adb_try_handler_change(id
, 0x66)) {
1009 printk("ADB mouse at %d, handler set to 0x66", id
);
1010 mouse_kind
= ADBMOUSE_MICROSPEED
;
1012 else if (adb_try_handler_change(id
, 0x5F)) {
1013 printk("ADB mouse at %d, handler set to 0x5F", id
);
1014 mouse_kind
= ADBMOUSE_MICROSPEED
;
1016 else if (adb_try_handler_change(id
, 3)) {
1017 printk("ADB mouse at %d, handler set to 3", id
);
1018 mouse_kind
= ADBMOUSE_MS_A3
;
1020 else if (adb_try_handler_change(id
, 2)) {
1021 printk("ADB mouse at %d, handler set to 2", id
);
1022 mouse_kind
= ADBMOUSE_STANDARD_200
;
1025 printk("ADB mouse at %d, handler 1", id
);
1026 mouse_kind
= ADBMOUSE_STANDARD_100
;
1029 if ((mouse_kind
== ADBMOUSE_TRACKBALLPRO
)
1030 || (mouse_kind
== ADBMOUSE_MICROSPEED
)) {
1031 init_microspeed(id
);
1032 } else if (mouse_kind
== ADBMOUSE_MS_A3
) {
1034 } else if (mouse_kind
== ADBMOUSE_EXTENDED
) {
1036 * Register 1 is usually used for device
1037 * identification. Here, we try to identify
1038 * a known device and call the appropriate
1041 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
1042 ADB_READREG(id
, 1));
1044 if ((req
.reply_len
) &&
1045 (req
.reply
[1] == 0x9a) && ((req
.reply
[2] == 0x21)
1046 || (req
.reply
[2] == 0x20))) {
1047 mouse_kind
= ADBMOUSE_TRACKBALL
;
1050 else if ((req
.reply_len
>= 4) &&
1051 (req
.reply
[1] == 0x74) && (req
.reply
[2] == 0x70) &&
1052 (req
.reply
[3] == 0x61) && (req
.reply
[4] == 0x64)) {
1053 mouse_kind
= ADBMOUSE_TRACKPAD
;
1056 else if ((req
.reply_len
>= 4) &&
1057 (req
.reply
[1] == 0x4b) && (req
.reply
[2] == 0x4d) &&
1058 (req
.reply
[3] == 0x4c) && (req
.reply
[4] == 0x31)) {
1059 mouse_kind
= ADBMOUSE_TURBOMOUSE5
;
1060 init_turbomouse(id
);
1062 else if ((req
.reply_len
== 9) &&
1063 (req
.reply
[1] == 0x4b) && (req
.reply
[2] == 0x4f) &&
1064 (req
.reply
[3] == 0x49) && (req
.reply
[4] == 0x54)) {
1065 if (adb_try_handler_change(id
, 0x42)) {
1066 printk("\nADB MacAlly 2-button mouse at %d, handler set to 0x42", id
);
1067 mouse_kind
= ADBMOUSE_MACALLY2
;
1073 adb_get_infos(id
, &default_id
, &cur_handler_id
);
1074 reg
|= adbhid_input_reregister(id
, default_id
, org_handler_id
,
1075 cur_handler_id
, mouse_kind
);
1077 adbhid_input_devcleanup(reg
);
1081 init_trackpad(int id
)
1083 struct adb_request req
;
1084 unsigned char r1_buffer
[8];
1086 printk(" (trackpad)");
1088 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
1090 if (req
.reply_len
< 8)
1091 printk("bad length for reg. 1\n");
1094 memcpy(r1_buffer
, &req
.reply
[1], 8);
1096 adb_request(&req
, NULL
, ADBREQ_SYNC
, 9,
1107 adb_request(&req
, NULL
, ADBREQ_SYNC
, 9,
1118 adb_request(&req
, NULL
, ADBREQ_SYNC
, 9,
1126 0x03, /*r1_buffer[6],*/
1129 /* Without this flush, the trackpad may be locked up */
1130 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(id
));
1135 init_trackball(int id
)
1137 struct adb_request req
;
1139 printk(" (trackman/mouseman)");
1141 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1142 ADB_WRITEREG(id
,1), 00,0x81);
1144 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1145 ADB_WRITEREG(id
,1), 01,0x81);
1147 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1148 ADB_WRITEREG(id
,1), 02,0x81);
1150 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1151 ADB_WRITEREG(id
,1), 03,0x38);
1153 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1154 ADB_WRITEREG(id
,1), 00,0x81);
1156 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1157 ADB_WRITEREG(id
,1), 01,0x81);
1159 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1160 ADB_WRITEREG(id
,1), 02,0x81);
1162 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1163 ADB_WRITEREG(id
,1), 03,0x38);
1167 init_turbomouse(int id
)
1169 struct adb_request req
;
1171 printk(" (TurboMouse 5)");
1173 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(id
));
1175 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(3));
1177 adb_request(&req
, NULL
, ADBREQ_SYNC
, 9,
1188 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(3));
1190 adb_request(&req
, NULL
, ADBREQ_SYNC
, 9,
1203 init_microspeed(int id
)
1205 struct adb_request req
;
1207 printk(" (Microspeed/MacPoint or compatible)");
1209 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(id
));
1211 /* This will initialize mice using the Microspeed, MacPoint and
1212 other compatible firmware. Bit 12 enables extended protocol.
1214 Register 1 Listen (4 Bytes)
1215 0 - 3 Button is mouse (set also for double clicking!!!)
1216 4 - 7 Button is locking (affects change speed also)
1217 8 - 11 Button changes speed
1218 12 1 = Extended mouse mode, 0 = normal mouse mode
1220 16 - 23 normal speed
1221 24 - 31 changed speed
1223 Register 1 talk holds version and product identification information.
1224 Register 1 Talk (4 Bytes):
1226 8 - 23 undefined, reserved
1227 24 - 31 Version number
1229 Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
1231 adb_request(&req
, NULL
, ADBREQ_SYNC
, 5,
1233 0x20, /* alt speed = 0x20 (rather slow) */
1234 0x00, /* norm speed = 0x00 (fastest) */
1235 0x10, /* extended protocol, no speed change */
1236 0x07); /* all buttons enabled as mouse buttons, no locking */
1239 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(id
));
1245 struct adb_request req
;
1247 printk(" (Mouse Systems A3 Mouse, or compatible)");
1248 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
1249 ADB_WRITEREG(id
, 0x2),
1253 adb_request(&req
, NULL
, ADBREQ_SYNC
, 1, ADB_FLUSH(id
));
1256 static int __init
adbhid_init(void)
1259 if (!machine_is(chrp
) && !machine_is(powermac
))
1263 led_request
.complete
= 1;
1267 blocking_notifier_chain_register(&adb_client_list
,
1268 &adbhid_adb_notifier
);
1273 static void __exit
adbhid_exit(void)
1277 module_init(adbhid_init
);
1278 module_exit(adbhid_exit
);