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