added mtd driver
[linux-2.4.git] / drivers / macintosh / adbhid.c
1 /*
2  * drivers/input/adbhid.c
3  *
4  * ADB HID driver for Power Macintosh computers.
5  *
6  * Adapted from drivers/macintosh/mac_keyb.c by Franz Sirl
7  * (see that file for its authors and contributors).
8  *
9  * Copyright (C) 2000 Franz Sirl.
10  *
11  * Adapted to ADB changes and support for more devices by
12  * Benjamin Herrenschmidt. Adapted from code in MkLinux
13  * and reworked.
14  * 
15  * Supported devices:
16  *
17  * - Standard 1 button mouse
18  * - All standard Apple Extended protocol (handler ID 4)
19  * - mouseman and trackman mice & trackballs 
20  * - PowerBook Trackpad (default setup: enable tapping)
21  * - MicroSpeed mouse & trackball (needs testing)
22  * - CH Products Trackball Pro (needs testing)
23  * - Contour Design (Contour Mouse)
24  * - Hunter digital (NoHandsMouse)
25  * - Kensignton TurboMouse 5 (needs testing)
26  * - Mouse Systems A3 mice and trackballs <aidan@kublai.com>
27  * - MacAlly 2-buttons mouse (needs testing) <pochini@denise.shiny.it>
28  *
29  * To do:
30  *
31  * Improve Kensington support.
32  */
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/init.h>
38 #include <linux/notifier.h>
39 #include <linux/input.h>
40 #include <linux/kbd_ll.h>
41
42 #include <linux/adb.h>
43 #include <linux/cuda.h>
44 #include <linux/pmu.h>
45
46 #include <asm/machdep.h>
47 #ifdef CONFIG_ALL_PPC
48 #include <asm/pmac_feature.h>
49 #endif
50
51 #ifdef CONFIG_PMAC_BACKLIGHT
52 #include <asm/backlight.h>
53 #endif
54
55 MODULE_AUTHOR("Franz Sirl <Franz.Sirl-kernel@lauterbach.com>");
56
57 #define KEYB_KEYREG     0       /* register # for key up/down data */
58 #define KEYB_LEDREG     2       /* register # for leds on ADB keyboard */
59 #define MOUSE_DATAREG   0       /* reg# for movement/button codes from mouse */
60
61 static int adb_message_handler(struct notifier_block *, unsigned long, void *);
62 static struct notifier_block adbhid_adb_notifier = {
63         notifier_call:  adb_message_handler,
64 };
65
66 unsigned char adb_to_linux_keycodes[128] = {
67          30, 31, 32, 33, 35, 34, 44, 45, 46, 47, 86, 48, 16, 17, 18, 19,
68          21, 20,  2,  3,  4,  5,  7,  6, 13, 10,  8, 12,  9, 11, 27, 24,
69          22, 26, 23, 25, 28, 38, 36, 40, 37, 39, 43, 51, 53, 49, 50, 52,
70          15, 57, 41, 14, 96,  1, 29,125, 42, 58, 56,105,106,108,103,  0,
71           0, 83,  0, 55,  0, 78,  0, 69,  0,  0,  0, 98, 96,  0, 74,  0,
72           0,117, 82, 79, 80, 81, 75, 76, 77, 71,  0, 72, 73,183,181,124,
73          63, 64, 65, 61, 66, 67,191, 87,190, 99,  0, 70,  0, 68,101, 88,
74           0,119,110,102,104,111, 62,107, 60,109, 59, 54,100, 97,126,116
75 };
76
77 struct adbhid {
78         struct input_dev input;
79         int id;
80         int default_id;
81         int original_handler_id;
82         int current_handler_id;
83         int mouse_kind;
84         unsigned char *keycode;
85         char name[64];
86 };
87
88 static struct adbhid *adbhid[16] = { 0 };
89
90 static void adbhid_probe(void);
91
92 static void adbhid_input_keycode(int, int, int);
93 static void leds_done(struct adb_request *);
94
95 static void init_trackpad(int id);
96 static void init_trackball(int id);
97 static void init_turbomouse(int id);
98 static void init_microspeed(int id);
99 static void init_ms_a3(int id);
100
101 static struct adb_ids keyboard_ids;
102 static struct adb_ids mouse_ids;
103 static struct adb_ids buttons_ids;
104
105 #ifdef CONFIG_PMAC_BACKLIGHT
106 /* Exported to via-pmu.c */
107 int disable_kernel_backlight = 0;
108 #endif /* CONFIG_PMAC_BACKLIGHT */
109
110 /* Kind of keyboard, see Apple technote 1152  */
111 #define ADB_KEYBOARD_UNKNOWN    0
112 #define ADB_KEYBOARD_ANSI       0x0100
113 #define ADB_KEYBOARD_ISO        0x0200
114 #define ADB_KEYBOARD_JIS        0x0300
115
116 /* Kind of mouse  */
117 #define ADBMOUSE_STANDARD_100   0       /* Standard 100cpi mouse (handler 1) */
118 #define ADBMOUSE_STANDARD_200   1       /* Standard 200cpi mouse (handler 2) */
119 #define ADBMOUSE_EXTENDED       2       /* Apple Extended mouse (handler 4) */
120 #define ADBMOUSE_TRACKBALL      3       /* TrackBall (handler 4) */
121 #define ADBMOUSE_TRACKPAD       4       /* Apple's PowerBook trackpad (handler 4) */
122 #define ADBMOUSE_TURBOMOUSE5    5       /* Turbomouse 5 (previously req. mousehack) */
123 #define ADBMOUSE_MICROSPEED     6       /* Microspeed mouse (&trackball ?), MacPoint */
124 #define ADBMOUSE_TRACKBALLPRO   7       /* Trackball Pro (special buttons) */
125 #define ADBMOUSE_MS_A3          8       /* Mouse systems A3 trackball (handler 3) */
126 #define ADBMOUSE_MACALLY2       9       /* MacAlly 2-button mouse */
127
128 static void
129 adbhid_keyboard_input(unsigned char *data, int nb, struct pt_regs *regs, int apoll)
130 {
131         int id = (data[0] >> 4) & 0x0f;
132
133         if (!adbhid[id]) {
134                 printk(KERN_ERR "ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
135                        id, data[0], data[1], data[2], data[3]);
136                 return;
137         }
138
139         /* first check this is from register 0 */
140         if (nb != 3 || (data[0] & 3) != KEYB_KEYREG)
141                 return;         /* ignore it */
142         kbd_pt_regs = regs;
143         adbhid_input_keycode(id, data[1], 0);
144         if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
145                 adbhid_input_keycode(id, data[2], 0);
146 }
147
148 static void
149 adbhid_input_keycode(int id, int keycode, int repeat)
150 {
151         int up_flag;
152
153         up_flag = (keycode & 0x80);
154         keycode &= 0x7f;
155
156         switch (keycode) {
157         case 0x39: /* Generate down/up events for CapsLock everytime. */
158                 input_report_key(&adbhid[id]->input, KEY_CAPSLOCK, 1);
159                 input_report_key(&adbhid[id]->input, KEY_CAPSLOCK, 0);
160                 return;
161         case 0x3f: /* ignore Powerbook Fn key */
162                 return;
163 #ifdef CONFIG_ALL_PPC
164         case 0x7e: /* Power key on PBook 3400 needs remapping */
165                 switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO,
166                         NULL, PMAC_MB_INFO_MODEL, 0)) {
167                 case PMAC_TYPE_COMET:
168                 case PMAC_TYPE_HOOPER:
169                 case PMAC_TYPE_KANGA:
170                         keycode = 0x7f;
171                 }
172                 break;
173 #endif /* CONFIG_ALL_PPC */
174         }
175
176         if (adbhid[id]->keycode[keycode])
177                 input_report_key(&adbhid[id]->input,
178                                  adbhid[id]->keycode[keycode], !up_flag);
179         else
180                 printk(KERN_INFO "Unhandled ADB key (scancode %#02x) %s.\n", keycode,
181                        up_flag ? "released" : "pressed");
182 }
183
184 static void
185 adbhid_mouse_input(unsigned char *data, int nb, struct pt_regs *regs, int autopoll)
186 {
187         int id = (data[0] >> 4) & 0x0f;
188
189         if (!adbhid[id]) {
190                 printk(KERN_ERR "ADB HID on ID %d not yet registered\n", id);
191                 return;
192         }
193
194   /*
195     Handler 1 -- 100cpi original Apple mouse protocol.
196     Handler 2 -- 200cpi original Apple mouse protocol.
197
198     For Apple's standard one-button mouse protocol the data array will
199     contain the following values:
200
201                 BITS    COMMENTS
202     data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
203     data[1] = bxxx xxxx First button and x-axis motion.
204     data[2] = byyy yyyy Second button and y-axis motion.
205
206     Handler 4 -- Apple Extended mouse protocol.
207
208     For Apple's 3-button mouse protocol the data array will contain the
209     following values:
210
211                 BITS    COMMENTS
212     data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
213     data[1] = bxxx xxxx Left button and x-axis motion.
214     data[2] = byyy yyyy Second button and y-axis motion.
215     data[3] = byyy bxxx Third button and fourth button.  Y is additional
216               high bits of y-axis motion.  XY is additional
217               high bits of x-axis motion.
218
219     MacAlly 2-button mouse protocol.
220
221     For MacAlly 2-button mouse protocol the data array will contain the
222     following values:
223
224                 BITS    COMMENTS
225     data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
226     data[1] = bxxx xxxx Left button and x-axis motion.
227     data[2] = byyy yyyy Right button and y-axis motion.
228     data[3] = ???? ???? unknown
229     data[4] = ???? ???? unknown
230
231   */
232
233         /* If it's a trackpad, we alias the second button to the first.
234            NOTE: Apple sends an ADB flush command to the trackpad when
235                  the first (the real) button is released. We could do
236                  this here using async flush requests.
237         */
238         switch (adbhid[id]->mouse_kind)
239         {
240             case ADBMOUSE_TRACKPAD:
241                 data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
242                 data[2] = data[2] | 0x80;
243                 break;
244             case ADBMOUSE_MICROSPEED:
245                 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
246                 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
247                 data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
248                         | (data[3] & 0x08);
249                 break;
250             case ADBMOUSE_TRACKBALLPRO:
251                 data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
252                         & ((data[3] & 0x08) << 4));
253                 data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
254                 data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
255                 break;
256             case ADBMOUSE_MS_A3:
257                 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
258                 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
259                 data[3] = ((data[3] & 0x04) << 5);
260                 break;
261             case ADBMOUSE_MACALLY2:
262                 data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
263                 data[2] |= 0x80;  /* Right button is mapped as button 3 */
264                 nb=4;
265                 break;
266         }
267
268         input_report_key(&adbhid[id]->input, BTN_LEFT,   !((data[1] >> 7) & 1));
269         input_report_key(&adbhid[id]->input, BTN_MIDDLE, !((data[2] >> 7) & 1));
270
271         if (nb >= 4)
272                 input_report_key(&adbhid[id]->input, BTN_RIGHT,  !((data[3] >> 7) & 1));
273
274         input_report_rel(&adbhid[id]->input, REL_X,
275                          ((data[2]&0x7f) < 64 ? (data[2]&0x7f) : (data[2]&0x7f)-128 ));
276         input_report_rel(&adbhid[id]->input, REL_Y,
277                          ((data[1]&0x7f) < 64 ? (data[1]&0x7f) : (data[1]&0x7f)-128 ));
278 }
279
280 static void
281 adbhid_buttons_input(unsigned char *data, int nb, struct pt_regs *regs, int autopoll)
282 {
283         int id = (data[0] >> 4) & 0x0f;
284
285         if (!adbhid[id]) {
286                 printk(KERN_ERR "ADB HID on ID %d not yet registered\n", id);
287                 return;
288         }
289
290         switch (adbhid[id]->original_handler_id) {
291         default:
292         case 0x02: /* Adjustable keyboard button device */
293                 printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
294                        data[0], data[1], data[2], data[3]);
295                 break;
296         case 0x1f: /* Powerbook button device */
297           {
298                 int down = (data[1] == (data[1] & 0xf));
299 #ifdef CONFIG_PMAC_BACKLIGHT
300                 int backlight = get_backlight_level();
301 #endif
302                 /*
303                  * XXX: Where is the contrast control for the passive?
304                  *  -- Cort
305                  */
306
307                 switch (data[1] & 0x0f) {
308                 case 0x8:       /* mute */
309                         input_report_key(&adbhid[id]->input, KEY_MUTE, down);
310                         break;
311
312                 case 0x7:       /* volume decrease */
313                         input_report_key(&adbhid[id]->input, KEY_VOLUMEDOWN, down);
314                         break;
315
316                 case 0x6:       /* volume increase */
317                         input_report_key(&adbhid[id]->input, KEY_VOLUMEUP, down);
318                         break;
319
320                 case 0xb:       /* eject */
321                         input_report_key(&adbhid[id]->input, KEY_EJECTCD, down);
322                         break;
323                 case 0xa:       /* brightness decrease */
324 #ifdef CONFIG_PMAC_BACKLIGHT
325                         if (!disable_kernel_backlight) {
326                                 if (down && backlight >= 0) {
327                                         if (backlight > BACKLIGHT_OFF)
328                                                 set_backlight_level(backlight-1);
329                                         else
330                                                 set_backlight_level(BACKLIGHT_OFF);
331                                 }
332                         }
333 #endif /* CONFIG_PMAC_BACKLIGHT */
334                         input_report_key(&adbhid[id]->input, KEY_BRIGHTNESSDOWN, down);
335                         break;
336                 case 0x9:       /* brightness increase */
337 #ifdef CONFIG_PMAC_BACKLIGHT
338                         if (!disable_kernel_backlight) {
339                                 if (down && backlight >= 0) {
340                                         if (backlight < BACKLIGHT_MAX)
341                                                 set_backlight_level(backlight+1);
342                                         else 
343                                                 set_backlight_level(BACKLIGHT_MAX);
344                                 }
345                         }
346 #endif /* CONFIG_PMAC_BACKLIGHT */
347                         input_report_key(&adbhid[id]->input, KEY_BRIGHTNESSUP, down);
348                         break;
349                 }
350           }
351           break;
352         }
353 }
354
355 static struct adb_request led_request;
356 static int leds_pending[16];
357 static int pending_devs[16];
358 static int pending_led_start=0;
359 static int pending_led_end=0;
360
361 static void real_leds(unsigned char leds, int device)
362 {
363     if (led_request.complete) {
364         adb_request(&led_request, leds_done, 0, 3,
365                     ADB_WRITEREG(device, KEYB_LEDREG), 0xff,
366                     ~leds);
367     } else {
368         if (!(leds_pending[device] & 0x100)) {
369             pending_devs[pending_led_end] = device;
370             pending_led_end++;
371             pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
372         }
373         leds_pending[device] = leds | 0x100;
374     }
375 }
376
377 /*
378  * Event callback from the input module. Events that change the state of
379  * the hardware are processed here.
380  */
381 static int adbhid_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
382 {
383         struct adbhid *adbhid = dev->private;
384         unsigned char leds;
385
386         switch (type) {
387         case EV_LED:
388           leds = (test_bit(LED_SCROLLL, dev->led) ? 4 : 0)
389                | (test_bit(LED_NUML,    dev->led) ? 1 : 0)
390                | (test_bit(LED_CAPSL,   dev->led) ? 2 : 0);
391           real_leds(leds, adbhid->id);
392           return 0;
393         }
394
395         return -1;
396 }
397
398 static void leds_done(struct adb_request *req)
399 {
400     int leds,device;
401
402     if (pending_led_start != pending_led_end) {
403         device = pending_devs[pending_led_start];
404         leds = leds_pending[device] & 0xff;
405         leds_pending[device] = 0;
406         pending_led_start++;
407         pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
408         real_leds(leds,device);
409     }
410
411 }
412
413 static int
414 adb_message_handler(struct notifier_block *this, unsigned long code, void *x)
415 {
416         unsigned long flags;
417
418         switch (code) {
419         case ADB_MSG_PRE_RESET:
420         case ADB_MSG_POWERDOWN:
421                 /* Stop the repeat timer. Autopoll is already off at this point */
422                 save_flags(flags);
423                 cli();
424                 {
425                         int i;
426                         for (i = 1; i < 16; i++) {
427                                 if (adbhid[i])
428                                         del_timer(&adbhid[i]->input.timer);
429                         }
430                 }
431                 restore_flags(flags);
432
433                 /* Stop pending led requests */
434                 while(!led_request.complete)
435                         adb_poll();
436                 break;
437
438         case ADB_MSG_POST_RESET:
439                 adbhid_probe();
440                 break;
441         }
442         return NOTIFY_DONE;
443 }
444
445 static void
446 adbhid_input_register(int id, int default_id, int original_handler_id,
447                       int current_handler_id, int mouse_kind)
448 {
449         int i;
450
451         if (adbhid[id]) {
452                 printk(KERN_ERR "Trying to reregister ADB HID on ID %d\n", id);
453                 return;
454         }
455
456         if (!(adbhid[id] = kmalloc(sizeof(struct adbhid), GFP_KERNEL)))
457                 return;
458
459         memset(adbhid[id], 0, sizeof(struct adbhid));
460
461         adbhid[id]->id = default_id;
462         adbhid[id]->original_handler_id = original_handler_id;
463         adbhid[id]->current_handler_id = current_handler_id;
464         adbhid[id]->mouse_kind = mouse_kind;
465         adbhid[id]->input.private = adbhid[id];
466         adbhid[id]->input.name = adbhid[id]->name;
467         adbhid[id]->input.idbus = BUS_ADB;
468         adbhid[id]->input.idvendor = 0x0001;
469         adbhid[id]->input.idproduct = (id << 12) | (default_id << 8) | original_handler_id;
470         adbhid[id]->input.idversion = 0x0100;
471
472         switch (default_id) {
473         case ADB_KEYBOARD:
474                 if (!(adbhid[id]->keycode = kmalloc(sizeof(adb_to_linux_keycodes), GFP_KERNEL))) {
475                         kfree(adbhid[id]);
476                         return;
477                 }
478
479                 sprintf(adbhid[id]->name, "ADB keyboard on ID %d:%d.%02x",
480                         id, default_id, original_handler_id);
481
482                 memcpy(adbhid[id]->keycode, adb_to_linux_keycodes, sizeof(adb_to_linux_keycodes));
483
484                 printk(KERN_INFO "Detected ADB keyboard, type ");
485                 switch (original_handler_id) {
486                 default:
487                         printk("<unknown>.\n");
488                         adbhid[id]->input.idversion = ADB_KEYBOARD_UNKNOWN;
489                         break;
490
491                 case 0x01: case 0x02: case 0x03: case 0x06: case 0x08:
492                 case 0x0C: case 0x10: case 0x18: case 0x1B: case 0x1C:
493                 case 0xC0: case 0xC3: case 0xC6:
494                         printk("ANSI.\n");
495                         adbhid[id]->input.idversion = ADB_KEYBOARD_ANSI;
496                         break;
497
498                 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
499                 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
500                 case 0xC4: case 0xC7:
501                         printk("ISO, swapping keys.\n");
502                         adbhid[id]->input.idversion = ADB_KEYBOARD_ISO;
503                         i = adbhid[id]->keycode[10];
504                         adbhid[id]->keycode[10] = adbhid[id]->keycode[50];
505                         adbhid[id]->keycode[50] = i;
506                         break;
507
508                 case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
509                 case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
510                         printk("JIS.\n");
511                         adbhid[id]->input.idversion = ADB_KEYBOARD_JIS;
512                         break;
513                 }
514
515                 for (i = 0; i < 128; i++)
516                         if (adbhid[id]->keycode[i])
517                                 set_bit(adbhid[id]->keycode[i], adbhid[id]->input.keybit);
518
519                 adbhid[id]->input.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);
520                 adbhid[id]->input.ledbit[0] = BIT(LED_SCROLLL) | BIT(LED_CAPSL) | BIT(LED_NUML);
521                 adbhid[id]->input.event = adbhid_kbd_event;
522                 adbhid[id]->input.keycodemax = 127;
523                 adbhid[id]->input.keycodesize = 1;
524                 break;
525
526         case ADB_MOUSE:
527                 sprintf(adbhid[id]->name, "ADB mouse on ID %d:%d.%02x",
528                         id, default_id, original_handler_id);
529
530                 adbhid[id]->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
531                 adbhid[id]->input.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
532                 adbhid[id]->input.relbit[0] = BIT(REL_X) | BIT(REL_Y);
533                 break;
534
535         case ADB_MISC:
536                 switch (original_handler_id) {
537                 case 0x02: /* Adjustable keyboard button device */
538                         sprintf(adbhid[id]->name, "ADB adjustable keyboard buttons on ID %d:%d.%02x",
539                                 id, default_id, original_handler_id);
540                         break;
541                 case 0x1f: /* Powerbook button device */
542                         sprintf(adbhid[id]->name, "ADB Powerbook buttons on ID %d:%d.%02x",
543                                 id, default_id, original_handler_id);
544                         adbhid[id]->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
545                         set_bit(KEY_MUTE, adbhid[id]->input.keybit);
546                         set_bit(KEY_VOLUMEUP, adbhid[id]->input.keybit);
547                         set_bit(KEY_VOLUMEDOWN, adbhid[id]->input.keybit);
548                         set_bit(KEY_BRIGHTNESSUP, adbhid[id]->input.keybit);
549                         set_bit(KEY_BRIGHTNESSDOWN, adbhid[id]->input.keybit);
550                         set_bit(KEY_EJECTCD, adbhid[id]->input.keybit);
551                         break;
552                 }
553                 if (adbhid[id]->name[0])
554                         break;
555                 /* else fall through */
556
557         default:
558                 printk(KERN_INFO "Trying to register unknown ADB device to input layer.\n");
559                 kfree(adbhid[id]);
560                 return;
561         }
562
563         adbhid[id]->input.keycode = adbhid[id]->keycode;
564
565         input_register_device(&adbhid[id]->input);
566
567         printk(KERN_INFO "input%d: ADB HID on ID %d:%d.%02x\n",
568                adbhid[id]->input.number, id, default_id, original_handler_id);
569
570         if (default_id == ADB_KEYBOARD) {
571                 /* HACK WARNING!! This should go away as soon there is an utility
572                  * to control that for event devices.
573                  */
574                 adbhid[id]->input.rep[REP_DELAY] = HZ/2;   /* input layer default: HZ/4 */
575                 adbhid[id]->input.rep[REP_PERIOD] = HZ/15; /* input layer default: HZ/33 */
576         }
577 }
578
579 static void adbhid_input_unregister(int id)
580 {
581         input_unregister_device(&adbhid[id]->input);
582         if (adbhid[id]->keycode)
583                 kfree(adbhid[id]->keycode);
584         kfree(adbhid[id]);
585         adbhid[id] = 0;
586 }
587
588
589 static u16
590 adbhid_input_reregister(int id, int default_id, int org_handler_id,
591                         int cur_handler_id, int mk)
592 {
593         if (adbhid[id]) {
594                 if (adbhid[id]->input.idproduct !=
595                     ((id << 12)|(default_id << 8)|org_handler_id)) {
596                         adbhid_input_unregister(id);
597                         adbhid_input_register(id, default_id, org_handler_id,
598                                 cur_handler_id, mk);
599                 }
600         } else
601                 adbhid_input_register(id, default_id, org_handler_id,
602                         cur_handler_id, mk);
603         return 1<<id;
604 }
605
606 static void
607 adbhid_input_devcleanup(u16 exist)
608 {
609         int i;
610         for(i=1; i<16; i++)
611             if (adbhid[i] && !(exist&(1<<i)))
612                 adbhid_input_unregister(i);
613 }
614  
615 static void
616 adbhid_probe(void)
617 {
618         struct adb_request req;
619         int i, default_id, org_handler_id, cur_handler_id;
620         u16 reg = 0;
621
622         adb_register(ADB_MOUSE, 0, &mouse_ids, adbhid_mouse_input);
623         adb_register(ADB_KEYBOARD, 0, &keyboard_ids, adbhid_keyboard_input);
624         adb_register(ADB_MISC, 0, &buttons_ids, adbhid_buttons_input);
625
626         for (i = 0; i < keyboard_ids.nids; i++) {
627                 int id = keyboard_ids.id[i];
628
629                 adb_get_infos(id, &default_id, &org_handler_id);
630
631                 /* turn off all leds */
632                 adb_request(&req, NULL, ADBREQ_SYNC, 3,
633                             ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
634
635                 /* Enable full feature set of the keyboard
636                    ->get it to send separate codes for left and right shift,
637                    control, option keys */
638 #if 0           /* handler 5 doesn't send separate codes for R modifiers */
639                 if (adb_try_handler_change(id, 5))
640                         printk("ADB keyboard at %d, handler set to 5\n", id);
641                 else
642 #endif
643                 if (adb_try_handler_change(id, 3))
644                         printk("ADB keyboard at %d, handler set to 3\n", id);
645                 else
646                         printk("ADB keyboard at %d, handler 1\n", id);
647
648                 adb_get_infos(id, &default_id, &cur_handler_id);
649                 reg |= adbhid_input_reregister(id, default_id, org_handler_id, cur_handler_id, 0);
650         }
651
652         for (i = 0; i < buttons_ids.nids; i++) {
653                 int id = buttons_ids.id[i];
654
655                 adb_get_infos(id, &default_id, &org_handler_id);
656                 reg |= adbhid_input_reregister(id, default_id, org_handler_id, org_handler_id, 0);
657         }
658
659         /* Try to switch all mice to handler 4, or 2 for three-button
660            mode and full resolution. */
661         for (i = 0; i < mouse_ids.nids; i++) {
662                 int id = mouse_ids.id[i];
663                 int mouse_kind;
664
665                 adb_get_infos(id, &default_id, &org_handler_id);
666
667                 if (adb_try_handler_change(id, 4)) {
668                         printk("ADB mouse at %d, handler set to 4", id);
669                         mouse_kind = ADBMOUSE_EXTENDED;
670                 }
671                 else if (adb_try_handler_change(id, 0x2F)) {
672                         printk("ADB mouse at %d, handler set to 0x2F", id);
673                         mouse_kind = ADBMOUSE_MICROSPEED;
674                 }
675                 else if (adb_try_handler_change(id, 0x42)) {
676                         printk("ADB mouse at %d, handler set to 0x42", id);
677                         mouse_kind = ADBMOUSE_TRACKBALLPRO;
678                 }
679                 else if (adb_try_handler_change(id, 0x66)) {
680                         printk("ADB mouse at %d, handler set to 0x66", id);
681                         mouse_kind = ADBMOUSE_MICROSPEED;
682                 }
683                 else if (adb_try_handler_change(id, 0x5F)) {
684                         printk("ADB mouse at %d, handler set to 0x5F", id);
685                         mouse_kind = ADBMOUSE_MICROSPEED;
686                 }
687                 else if (adb_try_handler_change(id, 3)) {
688                         printk("ADB mouse at %d, handler set to 3", id);
689                         mouse_kind = ADBMOUSE_MS_A3;
690                 }
691                 else if (adb_try_handler_change(id, 2)) {
692                         printk("ADB mouse at %d, handler set to 2", id);
693                         mouse_kind = ADBMOUSE_STANDARD_200;
694                 }
695                 else {
696                         printk("ADB mouse at %d, handler 1", id);
697                         mouse_kind = ADBMOUSE_STANDARD_100;
698                 }
699
700                 if ((mouse_kind == ADBMOUSE_TRACKBALLPRO)
701                     || (mouse_kind == ADBMOUSE_MICROSPEED)) {
702                         init_microspeed(id);
703                 } else if (mouse_kind == ADBMOUSE_MS_A3) {
704                         init_ms_a3(id);
705                 } else if (mouse_kind ==  ADBMOUSE_EXTENDED) {
706                         /*
707                          * Register 1 is usually used for device
708                          * identification.  Here, we try to identify
709                          * a known device and call the appropriate
710                          * init function.
711                          */
712                         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
713                                     ADB_READREG(id, 1));
714
715                         if ((req.reply_len) &&
716                             (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
717                                 || (req.reply[2] == 0x20))) {
718                                 mouse_kind = ADBMOUSE_TRACKBALL;
719                                 init_trackball(id);
720                         }
721                         else if ((req.reply_len >= 4) &&
722                             (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
723                             (req.reply[3] == 0x61) && (req.reply[4] == 0x64)) {
724                                 mouse_kind = ADBMOUSE_TRACKPAD;
725                                 init_trackpad(id);
726                         }
727                         else if ((req.reply_len >= 4) &&
728                             (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
729                             (req.reply[3] == 0x4c) && (req.reply[4] == 0x31)) {
730                                 mouse_kind = ADBMOUSE_TURBOMOUSE5;
731                                 init_turbomouse(id);
732                         }
733                         else if ((req.reply_len == 9) &&
734                             (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
735                             (req.reply[3] == 0x49) && (req.reply[4] == 0x54)) {
736                                 if (adb_try_handler_change(id, 0x42)) {
737                                         printk("\nADB MacAlly 2-button mouse at %d, handler set to 0x42", id);
738                                         mouse_kind = ADBMOUSE_MACALLY2;
739                                 }
740                         }
741                 }
742                 printk("\n");
743
744                 adb_get_infos(id, &default_id, &cur_handler_id);
745                 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
746                                       cur_handler_id, mouse_kind);
747         }
748         adbhid_input_devcleanup(reg);
749 }
750
751 static void 
752 init_trackpad(int id)
753 {
754         struct adb_request req;
755         unsigned char r1_buffer[8];
756
757         printk(" (trackpad)");
758
759         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
760                 ADB_READREG(id,1));
761         if (req.reply_len < 8)
762             printk("bad length for reg. 1\n");
763         else
764         {
765             memcpy(r1_buffer, &req.reply[1], 8);
766
767             adb_request(&req, NULL, ADBREQ_SYNC, 9,
768                 ADB_WRITEREG(id,1),
769                     r1_buffer[0],
770                     r1_buffer[1],
771                     r1_buffer[2],
772                     r1_buffer[3],
773                     r1_buffer[4],
774                     r1_buffer[5],
775                     0x0d,
776                     r1_buffer[7]);
777
778             adb_request(&req, NULL, ADBREQ_SYNC, 9,
779                 ADB_WRITEREG(id,2),
780                     0x99,
781                     0x94,
782                     0x19,
783                     0xff,
784                     0xb2,
785                     0x8a,
786                     0x1b,
787                     0x50);
788
789             adb_request(&req, NULL, ADBREQ_SYNC, 9,
790                 ADB_WRITEREG(id,1),
791                     r1_buffer[0],
792                     r1_buffer[1],
793                     r1_buffer[2],
794                     r1_buffer[3],
795                     r1_buffer[4],
796                     r1_buffer[5],
797                     0x03, /*r1_buffer[6],*/
798                     r1_buffer[7]);
799
800             /* Without this flush, the trackpad may be locked up */         
801             adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
802         }
803 }
804
805 static void 
806 init_trackball(int id)
807 {
808         struct adb_request req;
809
810         printk(" (trackman/mouseman)");
811
812         adb_request(&req, NULL, ADBREQ_SYNC, 3,
813         ADB_WRITEREG(id,1), 00,0x81);
814
815         adb_request(&req, NULL, ADBREQ_SYNC, 3,
816         ADB_WRITEREG(id,1), 01,0x81);
817
818         adb_request(&req, NULL, ADBREQ_SYNC, 3,
819         ADB_WRITEREG(id,1), 02,0x81);
820
821         adb_request(&req, NULL, ADBREQ_SYNC, 3,
822         ADB_WRITEREG(id,1), 03,0x38);
823
824         adb_request(&req, NULL, ADBREQ_SYNC, 3,
825         ADB_WRITEREG(id,1), 00,0x81);
826
827         adb_request(&req, NULL, ADBREQ_SYNC, 3,
828         ADB_WRITEREG(id,1), 01,0x81);
829
830         adb_request(&req, NULL, ADBREQ_SYNC, 3,
831         ADB_WRITEREG(id,1), 02,0x81);
832
833         adb_request(&req, NULL, ADBREQ_SYNC, 3,
834         ADB_WRITEREG(id,1), 03,0x38);
835 }
836
837 static void
838 init_turbomouse(int id)
839 {
840         struct adb_request req;
841
842         printk(" (TurboMouse 5)");
843
844         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
845
846         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
847
848         adb_request(&req, NULL, ADBREQ_SYNC, 9,
849         ADB_WRITEREG(3,2),
850             0xe7,
851             0x8c,
852             0,
853             0,
854             0,
855             0xff,
856             0xff,
857             0x94);
858
859         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
860
861         adb_request(&req, NULL, ADBREQ_SYNC, 9,
862         ADB_WRITEREG(3,2),
863             0xa5,
864             0x14,
865             0,
866             0,
867             0x69,
868             0xff,
869             0xff,
870             0x27);
871 }
872
873 static void
874 init_microspeed(int id)
875 {
876         struct adb_request req;
877
878         printk(" (Microspeed/MacPoint or compatible)");
879
880         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
881
882         /* This will initialize mice using the Microspeed, MacPoint and
883            other compatible firmware. Bit 12 enables extended protocol.
884            
885            Register 1 Listen (4 Bytes)
886             0 -  3     Button is mouse (set also for double clicking!!!)
887             4 -  7     Button is locking (affects change speed also)
888             8 - 11     Button changes speed
889            12          1 = Extended mouse mode, 0 = normal mouse mode
890            13 - 15     unused 0
891            16 - 23     normal speed
892            24 - 31     changed speed
893
894        Register 1 talk holds version and product identification information.
895        Register 1 Talk (4 Bytes):
896             0 -  7     Product code
897             8 - 23     undefined, reserved
898            24 - 31     Version number
899         
900        Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
901  */
902         adb_request(&req, NULL, ADBREQ_SYNC, 5,
903         ADB_WRITEREG(id,1),
904             0x20,       /* alt speed = 0x20 (rather slow) */
905             0x00,       /* norm speed = 0x00 (fastest) */
906             0x10,       /* extended protocol, no speed change */
907             0x07);      /* all buttons enabled as mouse buttons, no locking */
908
909
910         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
911 }
912
913 static void
914 init_ms_a3(int id)
915 {
916         struct adb_request req;
917
918         printk(" (Mouse Systems A3 Mouse, or compatible)");
919         adb_request(&req, NULL, ADBREQ_SYNC, 3,
920         ADB_WRITEREG(id, 0x2),
921             0x00,
922             0x07);
923  
924         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
925 }
926
927 static int __init adbhid_init(void)
928 {
929 #ifndef CONFIG_MAC
930         if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
931             return 0;
932 #endif
933
934         led_request.complete = 1;
935
936         adbhid_probe();
937
938         notifier_chain_register(&adb_client_list, &adbhid_adb_notifier);
939
940         return 0;
941 }
942
943 static void __exit adbhid_exit(void)
944 {
945 }
946  
947 module_init(adbhid_init);
948 module_exit(adbhid_exit);