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