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