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