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