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