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