blob: b2fe7a3dc47134a358c11e5a25e9f01fedc85d9c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02003 * drivers/macintosh/adbhid.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * ADB HID driver for Power Macintosh computers.
6 *
7 * Adapted from drivers/macintosh/mac_keyb.c by Franz Sirl.
8 * drivers/macintosh/mac_keyb.c was Copyright (C) 1996 Paul Mackerras
9 * with considerable contributions from Ben Herrenschmidt and others.
10 *
11 * Copyright (C) 2000 Franz Sirl.
12 *
13 * Adapted to ADB changes and support for more devices by
14 * Benjamin Herrenschmidt. Adapted from code in MkLinux
15 * and reworked.
16 *
17 * Supported devices:
18 *
19 * - Standard 1 button mouse
20 * - All standard Apple Extended protocol (handler ID 4)
21 * - mouseman and trackman mice & trackballs
22 * - PowerBook Trackpad (default setup: enable tapping)
23 * - MicroSpeed mouse & trackball (needs testing)
24 * - CH Products Trackball Pro (needs testing)
25 * - Contour Design (Contour Mouse)
26 * - Hunter digital (NoHandsMouse)
27 * - Kensignton TurboMouse 5 (needs testing)
28 * - Mouse Systems A3 mice and trackballs <aidan@kublai.com>
29 * - MacAlly 2-buttons mouse (needs testing) <pochini@denise.shiny.it>
30 *
31 * To do:
32 *
33 * Improve Kensington support.
34 * Split mouse/kbd
35 * Move to syfs
36 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/slab.h>
40#include <linux/init.h>
41#include <linux/notifier.h>
42#include <linux/input.h>
43
44#include <linux/adb.h>
45#include <linux/cuda.h>
46#include <linux/pmu.h>
47
48#include <asm/machdep.h>
49#ifdef CONFIG_PPC_PMAC
Al Viroa07562e2006-09-23 01:25:18 +010050#include <asm/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/pmac_feature.h>
52#endif
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_AUTHOR("Franz Sirl <Franz.Sirl-kernel@lauterbach.com>");
55
Andrew McNabb9a402b62007-10-11 00:49:43 -040056static int restore_capslock_events;
57module_param(restore_capslock_events, int, 0644);
58MODULE_PARM_DESC(restore_capslock_events,
59 "Produce keypress events for capslock on both keyup and keydown.");
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define KEYB_KEYREG 0 /* register # for key up/down data */
62#define KEYB_LEDREG 2 /* register # for leds on ADB keyboard */
63#define MOUSE_DATAREG 0 /* reg# for movement/button codes from mouse */
64
65static int adb_message_handler(struct notifier_block *, unsigned long, void *);
66static struct notifier_block adbhid_adb_notifier = {
67 .notifier_call = adb_message_handler,
68};
69
70/* Some special keys */
71#define ADB_KEY_DEL 0x33
72#define ADB_KEY_CMD 0x37
73#define ADB_KEY_CAPSLOCK 0x39
74#define ADB_KEY_FN 0x3f
75#define ADB_KEY_FWDEL 0x75
76#define ADB_KEY_POWER_OLD 0x7e
77#define ADB_KEY_POWER 0x7f
78
Adrian Bunk9b4a8dd2008-06-24 03:46:57 +100079static const u16 adb_to_linux_keycodes[128] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* 0x00 */ KEY_A, /* 30 */
81 /* 0x01 */ KEY_S, /* 31 */
82 /* 0x02 */ KEY_D, /* 32 */
83 /* 0x03 */ KEY_F, /* 33 */
84 /* 0x04 */ KEY_H, /* 35 */
85 /* 0x05 */ KEY_G, /* 34 */
86 /* 0x06 */ KEY_Z, /* 44 */
87 /* 0x07 */ KEY_X, /* 45 */
88 /* 0x08 */ KEY_C, /* 46 */
89 /* 0x09 */ KEY_V, /* 47 */
90 /* 0x0a */ KEY_102ND, /* 86 */
91 /* 0x0b */ KEY_B, /* 48 */
92 /* 0x0c */ KEY_Q, /* 16 */
93 /* 0x0d */ KEY_W, /* 17 */
94 /* 0x0e */ KEY_E, /* 18 */
95 /* 0x0f */ KEY_R, /* 19 */
96 /* 0x10 */ KEY_Y, /* 21 */
97 /* 0x11 */ KEY_T, /* 20 */
98 /* 0x12 */ KEY_1, /* 2 */
99 /* 0x13 */ KEY_2, /* 3 */
100 /* 0x14 */ KEY_3, /* 4 */
101 /* 0x15 */ KEY_4, /* 5 */
102 /* 0x16 */ KEY_6, /* 7 */
103 /* 0x17 */ KEY_5, /* 6 */
104 /* 0x18 */ KEY_EQUAL, /* 13 */
105 /* 0x19 */ KEY_9, /* 10 */
106 /* 0x1a */ KEY_7, /* 8 */
107 /* 0x1b */ KEY_MINUS, /* 12 */
108 /* 0x1c */ KEY_8, /* 9 */
109 /* 0x1d */ KEY_0, /* 11 */
110 /* 0x1e */ KEY_RIGHTBRACE, /* 27 */
111 /* 0x1f */ KEY_O, /* 24 */
112 /* 0x20 */ KEY_U, /* 22 */
113 /* 0x21 */ KEY_LEFTBRACE, /* 26 */
114 /* 0x22 */ KEY_I, /* 23 */
115 /* 0x23 */ KEY_P, /* 25 */
116 /* 0x24 */ KEY_ENTER, /* 28 */
117 /* 0x25 */ KEY_L, /* 38 */
118 /* 0x26 */ KEY_J, /* 36 */
119 /* 0x27 */ KEY_APOSTROPHE, /* 40 */
120 /* 0x28 */ KEY_K, /* 37 */
121 /* 0x29 */ KEY_SEMICOLON, /* 39 */
122 /* 0x2a */ KEY_BACKSLASH, /* 43 */
123 /* 0x2b */ KEY_COMMA, /* 51 */
124 /* 0x2c */ KEY_SLASH, /* 53 */
125 /* 0x2d */ KEY_N, /* 49 */
126 /* 0x2e */ KEY_M, /* 50 */
127 /* 0x2f */ KEY_DOT, /* 52 */
128 /* 0x30 */ KEY_TAB, /* 15 */
129 /* 0x31 */ KEY_SPACE, /* 57 */
130 /* 0x32 */ KEY_GRAVE, /* 41 */
131 /* 0x33 */ KEY_BACKSPACE, /* 14 */
132 /* 0x34 */ KEY_KPENTER, /* 96 */
133 /* 0x35 */ KEY_ESC, /* 1 */
134 /* 0x36 */ KEY_LEFTCTRL, /* 29 */
135 /* 0x37 */ KEY_LEFTMETA, /* 125 */
136 /* 0x38 */ KEY_LEFTSHIFT, /* 42 */
137 /* 0x39 */ KEY_CAPSLOCK, /* 58 */
138 /* 0x3a */ KEY_LEFTALT, /* 56 */
139 /* 0x3b */ KEY_LEFT, /* 105 */
140 /* 0x3c */ KEY_RIGHT, /* 106 */
141 /* 0x3d */ KEY_DOWN, /* 108 */
142 /* 0x3e */ KEY_UP, /* 103 */
Aristeu Rozanski555ddbb2007-07-17 06:53:09 +1000143 /* 0x3f */ KEY_FN, /* 0x1d0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* 0x40 */ 0,
145 /* 0x41 */ KEY_KPDOT, /* 83 */
146 /* 0x42 */ 0,
147 /* 0x43 */ KEY_KPASTERISK, /* 55 */
148 /* 0x44 */ 0,
149 /* 0x45 */ KEY_KPPLUS, /* 78 */
150 /* 0x46 */ 0,
151 /* 0x47 */ KEY_NUMLOCK, /* 69 */
152 /* 0x48 */ 0,
153 /* 0x49 */ 0,
154 /* 0x4a */ 0,
155 /* 0x4b */ KEY_KPSLASH, /* 98 */
156 /* 0x4c */ KEY_KPENTER, /* 96 */
157 /* 0x4d */ 0,
158 /* 0x4e */ KEY_KPMINUS, /* 74 */
159 /* 0x4f */ 0,
160 /* 0x50 */ 0,
161 /* 0x51 */ KEY_KPEQUAL, /* 117 */
162 /* 0x52 */ KEY_KP0, /* 82 */
163 /* 0x53 */ KEY_KP1, /* 79 */
164 /* 0x54 */ KEY_KP2, /* 80 */
165 /* 0x55 */ KEY_KP3, /* 81 */
166 /* 0x56 */ KEY_KP4, /* 75 */
167 /* 0x57 */ KEY_KP5, /* 76 */
168 /* 0x58 */ KEY_KP6, /* 77 */
169 /* 0x59 */ KEY_KP7, /* 71 */
170 /* 0x5a */ 0,
171 /* 0x5b */ KEY_KP8, /* 72 */
172 /* 0x5c */ KEY_KP9, /* 73 */
173 /* 0x5d */ KEY_YEN, /* 124 */
174 /* 0x5e */ KEY_RO, /* 89 */
175 /* 0x5f */ KEY_KPCOMMA, /* 121 */
176 /* 0x60 */ KEY_F5, /* 63 */
177 /* 0x61 */ KEY_F6, /* 64 */
178 /* 0x62 */ KEY_F7, /* 65 */
179 /* 0x63 */ KEY_F3, /* 61 */
180 /* 0x64 */ KEY_F8, /* 66 */
181 /* 0x65 */ KEY_F9, /* 67 */
182 /* 0x66 */ KEY_HANJA, /* 123 */
183 /* 0x67 */ KEY_F11, /* 87 */
Jerome Pinotb9ab58d2006-06-26 01:51:23 -0400184 /* 0x68 */ KEY_HANGEUL, /* 122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* 0x69 */ KEY_SYSRQ, /* 99 */
186 /* 0x6a */ 0,
187 /* 0x6b */ KEY_SCROLLLOCK, /* 70 */
188 /* 0x6c */ 0,
189 /* 0x6d */ KEY_F10, /* 68 */
190 /* 0x6e */ KEY_COMPOSE, /* 127 */
191 /* 0x6f */ KEY_F12, /* 88 */
192 /* 0x70 */ 0,
193 /* 0x71 */ KEY_PAUSE, /* 119 */
194 /* 0x72 */ KEY_INSERT, /* 110 */
195 /* 0x73 */ KEY_HOME, /* 102 */
196 /* 0x74 */ KEY_PAGEUP, /* 104 */
197 /* 0x75 */ KEY_DELETE, /* 111 */
198 /* 0x76 */ KEY_F4, /* 62 */
199 /* 0x77 */ KEY_END, /* 107 */
200 /* 0x78 */ KEY_F2, /* 60 */
201 /* 0x79 */ KEY_PAGEDOWN, /* 109 */
202 /* 0x7a */ KEY_F1, /* 59 */
203 /* 0x7b */ KEY_RIGHTSHIFT, /* 54 */
204 /* 0x7c */ KEY_RIGHTALT, /* 100 */
205 /* 0x7d */ KEY_RIGHTCTRL, /* 97 */
206 /* 0x7e */ KEY_RIGHTMETA, /* 126 */
207 /* 0x7f */ KEY_POWER, /* 116 */
208};
209
210struct adbhid {
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500211 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int id;
213 int default_id;
214 int original_handler_id;
215 int current_handler_id;
216 int mouse_kind;
Aristeu Rozanski555ddbb2007-07-17 06:53:09 +1000217 u16 *keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 char name[64];
219 char phys[32];
220 int flags;
221};
222
Rodney Lorrimar2e75f042008-05-05 11:59:00 -0400223#define FLAG_FN_KEY_PRESSED 0x00000001
224#define FLAG_POWER_FROM_FN 0x00000002
225#define FLAG_EMU_FWDEL_DOWN 0x00000004
226#define FLAG_CAPSLOCK_TRANSLATE 0x00000008
227#define FLAG_CAPSLOCK_DOWN 0x00000010
228#define FLAG_CAPSLOCK_IGNORE_NEXT 0x00000020
Rodney Lorrimar5b7c4072008-05-05 11:59:24 -0400229#define FLAG_POWER_KEY_PRESSED 0x00000040
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231static struct adbhid *adbhid[16];
232
233static void adbhid_probe(void);
234
David Howells7d12e782006-10-05 14:55:46 +0100235static void adbhid_input_keycode(int, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237static void init_trackpad(int id);
238static void init_trackball(int id);
239static void init_turbomouse(int id);
240static void init_microspeed(int id);
241static void init_ms_a3(int id);
242
243static struct adb_ids keyboard_ids;
244static struct adb_ids mouse_ids;
245static struct adb_ids buttons_ids;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* Kind of keyboard, see Apple technote 1152 */
248#define ADB_KEYBOARD_UNKNOWN 0
249#define ADB_KEYBOARD_ANSI 0x0100
250#define ADB_KEYBOARD_ISO 0x0200
251#define ADB_KEYBOARD_JIS 0x0300
252
253/* Kind of mouse */
254#define ADBMOUSE_STANDARD_100 0 /* Standard 100cpi mouse (handler 1) */
255#define ADBMOUSE_STANDARD_200 1 /* Standard 200cpi mouse (handler 2) */
256#define ADBMOUSE_EXTENDED 2 /* Apple Extended mouse (handler 4) */
257#define ADBMOUSE_TRACKBALL 3 /* TrackBall (handler 4) */
258#define ADBMOUSE_TRACKPAD 4 /* Apple's PowerBook trackpad (handler 4) */
259#define ADBMOUSE_TURBOMOUSE5 5 /* Turbomouse 5 (previously req. mousehack) */
260#define ADBMOUSE_MICROSPEED 6 /* Microspeed mouse (&trackball ?), MacPoint */
261#define ADBMOUSE_TRACKBALLPRO 7 /* Trackball Pro (special buttons) */
262#define ADBMOUSE_MS_A3 8 /* Mouse systems A3 trackball (handler 3) */
263#define ADBMOUSE_MACALLY2 9 /* MacAlly 2-button mouse */
264
265static void
David Howells7d12e782006-10-05 14:55:46 +0100266adbhid_keyboard_input(unsigned char *data, int nb, int apoll)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 int id = (data[0] >> 4) & 0x0f;
269
270 if (!adbhid[id]) {
Andreas Schwabf2be6292016-11-28 21:29:07 +0100271 pr_err("ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 id, data[0], data[1], data[2], data[3]);
273 return;
274 }
275
276 /* first check this is from register 0 */
277 if (nb != 3 || (data[0] & 3) != KEYB_KEYREG)
278 return; /* ignore it */
David Howells7d12e782006-10-05 14:55:46 +0100279 adbhid_input_keycode(id, data[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
David Howells7d12e782006-10-05 14:55:46 +0100281 adbhid_input_keycode(id, data[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static void
Andrew McNabb9a402b62007-10-11 00:49:43 -0400285adbhid_input_keycode(int id, int scancode, int repeat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct adbhid *ahid = adbhid[id];
Al Viro68f3e662007-10-17 01:02:46 +0100288 int keycode, up_flag, key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Andrew McNabb9a402b62007-10-11 00:49:43 -0400290 keycode = scancode & 0x7f;
291 up_flag = scancode & 0x80;
292
293 if (restore_capslock_events) {
294 if (keycode == ADB_KEY_CAPSLOCK && !up_flag) {
295 /* Key pressed, turning on the CapsLock LED.
296 * The next 0xff will be interpreted as a release. */
Rodney Lorrimar2e75f042008-05-05 11:59:00 -0400297 if (ahid->flags & FLAG_CAPSLOCK_IGNORE_NEXT) {
298 /* Throw away this key event if it happens
299 * just after resume. */
300 ahid->flags &= ~FLAG_CAPSLOCK_IGNORE_NEXT;
301 return;
302 } else {
303 ahid->flags |= FLAG_CAPSLOCK_TRANSLATE
Andrew McNabb9a402b62007-10-11 00:49:43 -0400304 | FLAG_CAPSLOCK_DOWN;
Rodney Lorrimar2e75f042008-05-05 11:59:00 -0400305 }
Rodney Lorrimar5b7c4072008-05-05 11:59:24 -0400306 } else if (scancode == 0xff &&
307 !(ahid->flags & FLAG_POWER_KEY_PRESSED)) {
Andrew McNabb9a402b62007-10-11 00:49:43 -0400308 /* Scancode 0xff usually signifies that the capslock
Rodney Lorrimar5b7c4072008-05-05 11:59:24 -0400309 * key was either pressed or released, or that the
310 * power button was released. */
Andrew McNabb9a402b62007-10-11 00:49:43 -0400311 if (ahid->flags & FLAG_CAPSLOCK_TRANSLATE) {
312 keycode = ADB_KEY_CAPSLOCK;
313 if (ahid->flags & FLAG_CAPSLOCK_DOWN) {
314 /* Key released */
315 up_flag = 1;
316 ahid->flags &= ~FLAG_CAPSLOCK_DOWN;
317 } else {
318 /* Key pressed */
319 up_flag = 0;
320 ahid->flags &= ~FLAG_CAPSLOCK_TRANSLATE;
321 }
322 } else {
Andreas Schwabf2be6292016-11-28 21:29:07 +0100323 pr_info("Spurious caps lock event (scancode 0xff).\n");
Andrew McNabb9a402b62007-10-11 00:49:43 -0400324 }
325 }
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 switch (keycode) {
Andrew McNabb9a402b62007-10-11 00:49:43 -0400329 case ADB_KEY_CAPSLOCK:
330 if (!restore_capslock_events) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300331 /* Generate down/up events for CapsLock every time. */
Andrew McNabb9a402b62007-10-11 00:49:43 -0400332 input_report_key(ahid->input, KEY_CAPSLOCK, 1);
333 input_sync(ahid->input);
334 input_report_key(ahid->input, KEY_CAPSLOCK, 0);
335 input_sync(ahid->input);
Andy Wingo9f31c052008-01-14 00:55:15 -0800336 return;
Andrew McNabb9a402b62007-10-11 00:49:43 -0400337 }
Andy Wingo9f31c052008-01-14 00:55:15 -0800338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#ifdef CONFIG_PPC_PMAC
340 case ADB_KEY_POWER_OLD: /* Power key on PBook 3400 needs remapping */
341 switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO,
342 NULL, PMAC_MB_INFO_MODEL, 0)) {
343 case PMAC_TYPE_COMET:
344 case PMAC_TYPE_HOOPER:
345 case PMAC_TYPE_KANGA:
346 keycode = ADB_KEY_POWER;
347 }
348 break;
Dmitry Torokhovb981d8b32007-10-12 21:27:47 -0400349 case ADB_KEY_POWER:
Rodney Lorrimar5b7c4072008-05-05 11:59:24 -0400350 /* Keep track of the power key state */
351 if (up_flag)
352 ahid->flags &= ~FLAG_POWER_KEY_PRESSED;
353 else
354 ahid->flags |= FLAG_POWER_KEY_PRESSED;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* Fn + Command will produce a bogus "power" keycode */
357 if (ahid->flags & FLAG_FN_KEY_PRESSED) {
358 keycode = ADB_KEY_CMD;
359 if (up_flag)
360 ahid->flags &= ~FLAG_POWER_FROM_FN;
361 else
362 ahid->flags |= FLAG_POWER_FROM_FN;
363 } else if (ahid->flags & FLAG_POWER_FROM_FN) {
364 keycode = ADB_KEY_CMD;
365 ahid->flags &= ~FLAG_POWER_FROM_FN;
366 }
367 break;
368 case ADB_KEY_FN:
369 /* Keep track of the Fn key state */
370 if (up_flag) {
371 ahid->flags &= ~FLAG_FN_KEY_PRESSED;
372 /* Emulate Fn+delete = forward delete */
373 if (ahid->flags & FLAG_EMU_FWDEL_DOWN) {
374 ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
375 keycode = ADB_KEY_FWDEL;
376 break;
377 }
378 } else
379 ahid->flags |= FLAG_FN_KEY_PRESSED;
Aristeu Rozanski555ddbb2007-07-17 06:53:09 +1000380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case ADB_KEY_DEL:
382 /* Emulate Fn+delete = forward delete */
383 if (ahid->flags & FLAG_FN_KEY_PRESSED) {
384 keycode = ADB_KEY_FWDEL;
385 if (up_flag)
386 ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
387 else
388 ahid->flags |= FLAG_EMU_FWDEL_DOWN;
389 }
390 break;
391#endif /* CONFIG_PPC_PMAC */
392 }
393
Aristeu Rozanski555ddbb2007-07-17 06:53:09 +1000394 key = adbhid[id]->keycode[keycode];
395 if (key) {
396 input_report_key(adbhid[id]->input, key, !up_flag);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500397 input_sync(adbhid[id]->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else
Andreas Schwabf2be6292016-11-28 21:29:07 +0100399 pr_info("Unhandled ADB key (scancode %#02x) %s.\n", keycode,
400 up_flag ? "released" : "pressed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402}
403
404static void
David Howells7d12e782006-10-05 14:55:46 +0100405adbhid_mouse_input(unsigned char *data, int nb, int autopoll)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int id = (data[0] >> 4) & 0x0f;
408
409 if (!adbhid[id]) {
Andreas Schwabf2be6292016-11-28 21:29:07 +0100410 pr_err("ADB HID on ID %d not yet registered\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return;
412 }
413
414 /*
415 Handler 1 -- 100cpi original Apple mouse protocol.
416 Handler 2 -- 200cpi original Apple mouse protocol.
417
418 For Apple's standard one-button mouse protocol the data array will
419 contain the following values:
420
421 BITS COMMENTS
422 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
423 data[1] = bxxx xxxx First button and x-axis motion.
424 data[2] = byyy yyyy Second button and y-axis motion.
425
426 Handler 4 -- Apple Extended mouse protocol.
427
428 For Apple's 3-button mouse protocol the data array will contain the
429 following values:
430
431 BITS COMMENTS
432 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
433 data[1] = bxxx xxxx Left button and x-axis motion.
434 data[2] = byyy yyyy Second button and y-axis motion.
435 data[3] = byyy bxxx Third button and fourth button. Y is additional
436 high bits of y-axis motion. XY is additional
437 high bits of x-axis motion.
438
439 MacAlly 2-button mouse protocol.
440
441 For MacAlly 2-button mouse protocol the data array will contain the
442 following values:
443
444 BITS COMMENTS
445 data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
446 data[1] = bxxx xxxx Left button and x-axis motion.
447 data[2] = byyy yyyy Right button and y-axis motion.
448 data[3] = ???? ???? unknown
449 data[4] = ???? ???? unknown
450
451 */
452
453 /* If it's a trackpad, we alias the second button to the first.
454 NOTE: Apple sends an ADB flush command to the trackpad when
455 the first (the real) button is released. We could do
456 this here using async flush requests.
457 */
458 switch (adbhid[id]->mouse_kind)
459 {
460 case ADBMOUSE_TRACKPAD:
461 data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
462 data[2] = data[2] | 0x80;
463 break;
464 case ADBMOUSE_MICROSPEED:
465 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
466 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
467 data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
468 | (data[3] & 0x08);
469 break;
470 case ADBMOUSE_TRACKBALLPRO:
471 data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
472 & ((data[3] & 0x08) << 4));
473 data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
474 data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
475 break;
476 case ADBMOUSE_MS_A3:
477 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
478 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
479 data[3] = ((data[3] & 0x04) << 5);
480 break;
481 case ADBMOUSE_MACALLY2:
482 data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
483 data[2] |= 0x80; /* Right button is mapped as button 3 */
484 nb=4;
485 break;
486 }
487
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500488 input_report_key(adbhid[id]->input, BTN_LEFT, !((data[1] >> 7) & 1));
489 input_report_key(adbhid[id]->input, BTN_MIDDLE, !((data[2] >> 7) & 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (nb >= 4 && adbhid[id]->mouse_kind != ADBMOUSE_TRACKPAD)
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500492 input_report_key(adbhid[id]->input, BTN_RIGHT, !((data[3] >> 7) & 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500494 input_report_rel(adbhid[id]->input, REL_X,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 ((data[2]&0x7f) < 64 ? (data[2]&0x7f) : (data[2]&0x7f)-128 ));
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500496 input_report_rel(adbhid[id]->input, REL_Y,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ((data[1]&0x7f) < 64 ? (data[1]&0x7f) : (data[1]&0x7f)-128 ));
498
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500499 input_sync(adbhid[id]->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502static void
David Howells7d12e782006-10-05 14:55:46 +0100503adbhid_buttons_input(unsigned char *data, int nb, int autopoll)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int id = (data[0] >> 4) & 0x0f;
506
507 if (!adbhid[id]) {
Andreas Schwabf2be6292016-11-28 21:29:07 +0100508 pr_err("ADB HID on ID %d not yet registered\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return;
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 switch (adbhid[id]->original_handler_id) {
513 default:
514 case 0x02: /* Adjustable keyboard button device */
515 {
516 int down = (data[1] == (data[1] & 0xf));
517
518 switch (data[1] & 0x0f) {
519 case 0x0: /* microphone */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500520 input_report_key(adbhid[id]->input, KEY_SOUND, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522
523 case 0x1: /* mute */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500524 input_report_key(adbhid[id]->input, KEY_MUTE, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526
527 case 0x2: /* volume decrease */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500528 input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530
531 case 0x3: /* volume increase */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500532 input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 default:
Andreas Schwabf2be6292016-11-28 21:29:07 +0100536 pr_info("Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
537 data[0], data[1], data[2], data[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
539 }
540 }
541 break;
542
543 case 0x1f: /* Powerbook button device */
544 {
545 int down = (data[1] == (data[1] & 0xf));
Michael Hanselmann5474c122006-06-25 05:47:08 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /*
548 * XXX: Where is the contrast control for the passive?
549 * -- Cort
550 */
551
552 switch (data[1] & 0x0f) {
553 case 0x8: /* mute */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500554 input_report_key(adbhid[id]->input, KEY_MUTE, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556
557 case 0x7: /* volume decrease */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500558 input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560
561 case 0x6: /* volume increase */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500562 input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564
565 case 0xb: /* eject */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500566 input_report_key(adbhid[id]->input, KEY_EJECTCD, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568
569 case 0xa: /* brightness decrease */
570#ifdef CONFIG_PMAC_BACKLIGHT
Michael Hanselmann4b755992006-07-30 03:04:19 -0700571 if (down)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700572 pmac_backlight_key_down();
573#endif
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500574 input_report_key(adbhid[id]->input, KEY_BRIGHTNESSDOWN, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576
577 case 0x9: /* brightness increase */
578#ifdef CONFIG_PMAC_BACKLIGHT
Michael Hanselmann4b755992006-07-30 03:04:19 -0700579 if (down)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700580 pmac_backlight_key_up();
581#endif
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500582 input_report_key(adbhid[id]->input, KEY_BRIGHTNESSUP, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700584
585 case 0xc: /* videomode switch */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500586 input_report_key(adbhid[id]->input, KEY_SWITCHVIDEOMODE, down);
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700587 break;
588
589 case 0xd: /* keyboard illumination toggle */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500590 input_report_key(adbhid[id]->input, KEY_KBDILLUMTOGGLE, down);
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700591 break;
592
593 case 0xe: /* keyboard illumination decrease */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500594 input_report_key(adbhid[id]->input, KEY_KBDILLUMDOWN, down);
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700595 break;
596
597 case 0xf:
598 switch (data[1]) {
599 case 0x8f:
600 case 0x0f:
601 /* keyboard illumination increase */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500602 input_report_key(adbhid[id]->input, KEY_KBDILLUMUP, down);
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700603 break;
604
605 case 0x7f:
606 case 0xff:
607 /* keypad overlay toogle */
608 break;
609
610 default:
Andreas Schwabf2be6292016-11-28 21:29:07 +0100611 pr_info("Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
612 data[0], data[1], data[2], data[3]);
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700613 break;
614 }
615 break;
616 default:
Andreas Schwabf2be6292016-11-28 21:29:07 +0100617 pr_info("Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
618 data[0], data[1], data[2], data[3]);
Andreas Jaggi146a4b32005-05-01 08:58:41 -0700619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 }
622 break;
623 }
624
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500625 input_sync(adbhid[id]->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static struct adb_request led_request;
629static int leds_pending[16];
630static int leds_req_pending;
631static int pending_devs[16];
Olaf Hering87275852007-02-10 21:35:12 +0100632static int pending_led_start;
633static int pending_led_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static DEFINE_SPINLOCK(leds_lock);
635
636static void leds_done(struct adb_request *req)
637{
638 int leds = 0, device = 0, pending = 0;
639 unsigned long flags;
640
641 spin_lock_irqsave(&leds_lock, flags);
642
643 if (pending_led_start != pending_led_end) {
644 device = pending_devs[pending_led_start];
645 leds = leds_pending[device] & 0xff;
646 leds_pending[device] = 0;
647 pending_led_start++;
648 pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
649 pending = leds_req_pending;
650 } else
651 leds_req_pending = 0;
652 spin_unlock_irqrestore(&leds_lock, flags);
653 if (pending)
654 adb_request(&led_request, leds_done, 0, 3,
655 ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
656}
657
658static void real_leds(unsigned char leds, int device)
659{
660 unsigned long flags;
661
662 spin_lock_irqsave(&leds_lock, flags);
663 if (!leds_req_pending) {
664 leds_req_pending = 1;
665 spin_unlock_irqrestore(&leds_lock, flags);
666 adb_request(&led_request, leds_done, 0, 3,
667 ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
668 return;
669 } else {
670 if (!(leds_pending[device] & 0x100)) {
671 pending_devs[pending_led_end] = device;
672 pending_led_end++;
673 pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
674 }
675 leds_pending[device] = leds | 0x100;
676 }
677 spin_unlock_irqrestore(&leds_lock, flags);
678}
679
680/*
681 * Event callback from the input module. Events that change the state of
682 * the hardware are processed here.
683 */
684static int adbhid_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
685{
Dmitry Torokhov7d7b33d2007-05-08 23:34:30 -0400686 struct adbhid *adbhid = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned char leds;
688
689 switch (type) {
690 case EV_LED:
Dmitry Torokhov7d7b33d2007-05-08 23:34:30 -0400691 leds = (test_bit(LED_SCROLLL, dev->led) ? 4 : 0) |
692 (test_bit(LED_NUML, dev->led) ? 1 : 0) |
693 (test_bit(LED_CAPSL, dev->led) ? 2 : 0);
694 real_leds(leds, adbhid->id);
695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
698 return -1;
699}
700
Rodney Lorrimar2e75f042008-05-05 11:59:00 -0400701static void
702adbhid_kbd_capslock_remember(void)
703{
704 struct adbhid *ahid;
705 int i;
706
707 for (i = 1; i < 16; i++) {
708 ahid = adbhid[i];
709
710 if (ahid && ahid->id == ADB_KEYBOARD)
711 if (ahid->flags & FLAG_CAPSLOCK_TRANSLATE)
712 ahid->flags |= FLAG_CAPSLOCK_IGNORE_NEXT;
713 }
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int
717adb_message_handler(struct notifier_block *this, unsigned long code, void *x)
718{
719 switch (code) {
720 case ADB_MSG_PRE_RESET:
721 case ADB_MSG_POWERDOWN:
Dmitry Torokhov7d7b33d2007-05-08 23:34:30 -0400722 /* Stop the repeat timer. Autopoll is already off at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 {
724 int i;
725 for (i = 1; i < 16; i++) {
726 if (adbhid[i])
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500727 del_timer_sync(&adbhid[i]->input->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 }
730
731 /* Stop pending led requests */
Rodney Lorrimar2e75f042008-05-05 11:59:00 -0400732 while (leds_req_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 adb_poll();
Rodney Lorrimar2e75f042008-05-05 11:59:00 -0400734
735 /* After resume, and if the capslock LED is on, the PMU will
736 * send a "capslock down" key event. This confuses the
737 * restore_capslock_events logic. Remember if the capslock
738 * LED was on before suspend so the unwanted key event can
739 * be ignored after resume. */
740 if (restore_capslock_events)
741 adbhid_kbd_capslock_remember();
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744
745 case ADB_MSG_POST_RESET:
746 adbhid_probe();
747 break;
748 }
749 return NOTIFY_DONE;
750}
751
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500752static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753adbhid_input_register(int id, int default_id, int original_handler_id,
754 int current_handler_id, int mouse_kind)
755{
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500756 struct adbhid *hid;
757 struct input_dev *input_dev;
758 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int i;
Finn Thain23416292018-09-11 20:18:44 -0400760 char *keyboard_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (adbhid[id]) {
Andreas Schwabf2be6292016-11-28 21:29:07 +0100763 pr_err("Trying to reregister ADB HID on ID %d\n", id);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500764 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500767 adbhid[id] = hid = kzalloc(sizeof(struct adbhid), GFP_KERNEL);
768 input_dev = input_allocate_device();
769 if (!hid || !input_dev) {
770 err = -ENOMEM;
771 goto fail;
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500774 sprintf(hid->phys, "adb%d:%d.%02x/input", id, default_id, original_handler_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Paul Mackerraseb162922005-10-31 01:30:32 -0500776 hid->input = input_dev;
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500777 hid->id = default_id;
778 hid->original_handler_id = original_handler_id;
779 hid->current_handler_id = current_handler_id;
780 hid->mouse_kind = mouse_kind;
781 hid->flags = 0;
Dmitry Torokhov7d7b33d2007-05-08 23:34:30 -0400782 input_set_drvdata(input_dev, hid);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500783 input_dev->name = hid->name;
784 input_dev->phys = hid->phys;
785 input_dev->id.bustype = BUS_ADB;
786 input_dev->id.vendor = 0x0001;
787 input_dev->id.product = (id << 12) | (default_id << 8) | original_handler_id;
788 input_dev->id.version = 0x0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 switch (default_id) {
791 case ADB_KEYBOARD:
Fuqian Huang7641c1b2019-07-04 00:28:21 +0800792 hid->keycode = kmemdup(adb_to_linux_keycodes,
793 sizeof(adb_to_linux_keycodes), GFP_KERNEL);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500794 if (!hid->keycode) {
795 err = -ENOMEM;
796 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500799 sprintf(hid->name, "ADB keyboard");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 switch (original_handler_id) {
802 default:
Finn Thain23416292018-09-11 20:18:44 -0400803 keyboard_type = "<unknown>";
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500804 input_dev->id.version = ADB_KEYBOARD_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806
807 case 0x01: case 0x02: case 0x03: case 0x06: case 0x08:
808 case 0x0C: case 0x10: case 0x18: case 0x1B: case 0x1C:
809 case 0xC0: case 0xC3: case 0xC6:
Finn Thain23416292018-09-11 20:18:44 -0400810 keyboard_type = "ANSI";
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500811 input_dev->id.version = ADB_KEYBOARD_ANSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813
814 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
815 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
816 case 0xC4: case 0xC7:
Finn Thain23416292018-09-11 20:18:44 -0400817 keyboard_type = "ISO, swapping keys";
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500818 input_dev->id.version = ADB_KEYBOARD_ISO;
Yang Guang6130ed72021-11-04 09:14:56 +0800819 swap(hid->keycode[10], hid->keycode[50]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821
822 case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
823 case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
Finn Thain23416292018-09-11 20:18:44 -0400824 keyboard_type = "JIS";
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500825 input_dev->id.version = ADB_KEYBOARD_JIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827 }
Finn Thain23416292018-09-11 20:18:44 -0400828 pr_info("Detected ADB keyboard, type %s.\n", keyboard_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 for (i = 0; i < 128; i++)
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500831 if (hid->keycode[i])
832 set_bit(hid->keycode[i], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700834 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
835 BIT_MASK(EV_REP);
836 input_dev->ledbit[0] = BIT_MASK(LED_SCROLLL) |
837 BIT_MASK(LED_CAPSL) | BIT_MASK(LED_NUML);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500838 input_dev->event = adbhid_kbd_event;
Aristeu Rozanski555ddbb2007-07-17 06:53:09 +1000839 input_dev->keycodemax = KEY_FN;
840 input_dev->keycodesize = sizeof(hid->keycode[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
842
843 case ADB_MOUSE:
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500844 sprintf(hid->name, "ADB mouse");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700846 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
847 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
848 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
849 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851
852 case ADB_MISC:
853 switch (original_handler_id) {
854 case 0x02: /* Adjustable keyboard button device */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500855 sprintf(hid->name, "ADB adjustable keyboard buttons");
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700856 input_dev->evbit[0] = BIT_MASK(EV_KEY) |
857 BIT_MASK(EV_REP);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500858 set_bit(KEY_SOUND, input_dev->keybit);
859 set_bit(KEY_MUTE, input_dev->keybit);
860 set_bit(KEY_VOLUMEUP, input_dev->keybit);
861 set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 case 0x1f: /* Powerbook button device */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500864 sprintf(hid->name, "ADB Powerbook buttons");
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700865 input_dev->evbit[0] = BIT_MASK(EV_KEY) |
866 BIT_MASK(EV_REP);
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500867 set_bit(KEY_MUTE, input_dev->keybit);
868 set_bit(KEY_VOLUMEUP, input_dev->keybit);
869 set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
870 set_bit(KEY_BRIGHTNESSUP, input_dev->keybit);
871 set_bit(KEY_BRIGHTNESSDOWN, input_dev->keybit);
872 set_bit(KEY_EJECTCD, input_dev->keybit);
873 set_bit(KEY_SWITCHVIDEOMODE, input_dev->keybit);
874 set_bit(KEY_KBDILLUMTOGGLE, input_dev->keybit);
875 set_bit(KEY_KBDILLUMDOWN, input_dev->keybit);
876 set_bit(KEY_KBDILLUMUP, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 }
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500879 if (hid->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500881 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 default:
Andreas Schwabf2be6292016-11-28 21:29:07 +0100884 pr_info("Trying to register unknown ADB device to input layer.\n");
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500885 err = -ENODEV;
886 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500889 input_dev->keycode = hid->keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Dmitry Torokhov4bdbd282006-11-24 00:43:22 -0500891 err = input_register_device(input_dev);
892 if (err)
893 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (default_id == ADB_KEYBOARD) {
896 /* HACK WARNING!! This should go away as soon there is an utility
897 * to control that for event devices.
898 */
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500899 input_dev->rep[REP_DELAY] = 500; /* input layer default: 250 */
900 input_dev->rep[REP_PERIOD] = 66; /* input layer default: 33 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500902
903 return 0;
904
905 fail: input_free_device(input_dev);
Dmitry Torokhov4bdbd282006-11-24 00:43:22 -0500906 if (hid) {
907 kfree(hid->keycode);
908 kfree(hid);
909 }
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500910 adbhid[id] = NULL;
911 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914static void adbhid_input_unregister(int id)
915{
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500916 input_unregister_device(adbhid[id]->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800917 kfree(adbhid[id]->keycode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 kfree(adbhid[id]);
919 adbhid[id] = NULL;
920}
921
922
923static u16
924adbhid_input_reregister(int id, int default_id, int org_handler_id,
925 int cur_handler_id, int mk)
926{
927 if (adbhid[id]) {
Dmitry Torokhovc7f7a562005-09-15 02:01:41 -0500928 if (adbhid[id]->input->id.product !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ((id << 12)|(default_id << 8)|org_handler_id)) {
930 adbhid_input_unregister(id);
931 adbhid_input_register(id, default_id, org_handler_id,
932 cur_handler_id, mk);
933 }
934 } else
935 adbhid_input_register(id, default_id, org_handler_id,
936 cur_handler_id, mk);
937 return 1<<id;
938}
939
940static void
941adbhid_input_devcleanup(u16 exist)
942{
943 int i;
944 for(i=1; i<16; i++)
945 if (adbhid[i] && !(exist&(1<<i)))
946 adbhid_input_unregister(i);
947}
948
949static void
950adbhid_probe(void)
951{
952 struct adb_request req;
953 int i, default_id, org_handler_id, cur_handler_id;
954 u16 reg = 0;
955
956 adb_register(ADB_MOUSE, 0, &mouse_ids, adbhid_mouse_input);
957 adb_register(ADB_KEYBOARD, 0, &keyboard_ids, adbhid_keyboard_input);
958 adb_register(ADB_MISC, 0, &buttons_ids, adbhid_buttons_input);
959
960 for (i = 0; i < keyboard_ids.nids; i++) {
961 int id = keyboard_ids.id[i];
962
963 adb_get_infos(id, &default_id, &org_handler_id);
964
965 /* turn off all leds */
966 adb_request(&req, NULL, ADBREQ_SYNC, 3,
967 ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
968
969 /* Enable full feature set of the keyboard
970 ->get it to send separate codes for left and right shift,
971 control, option keys */
972#if 0 /* handler 5 doesn't send separate codes for R modifiers */
Finn Thain23416292018-09-11 20:18:44 -0400973 if (!adb_try_handler_change(id, 5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#endif
Finn Thain23416292018-09-11 20:18:44 -0400975 adb_try_handler_change(id, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 adb_get_infos(id, &default_id, &cur_handler_id);
Finn Thain23416292018-09-11 20:18:44 -0400978 printk(KERN_DEBUG "ADB keyboard at %d has handler 0x%X\n",
979 id, cur_handler_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
981 cur_handler_id, 0);
982 }
983
984 for (i = 0; i < buttons_ids.nids; i++) {
985 int id = buttons_ids.id[i];
986
987 adb_get_infos(id, &default_id, &org_handler_id);
988 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
989 org_handler_id, 0);
990 }
991
992 /* Try to switch all mice to handler 4, or 2 for three-button
993 mode and full resolution. */
994 for (i = 0; i < mouse_ids.nids; i++) {
995 int id = mouse_ids.id[i];
996 int mouse_kind;
Finn Thain23416292018-09-11 20:18:44 -0400997 char *desc = "standard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 adb_get_infos(id, &default_id, &org_handler_id);
1000
1001 if (adb_try_handler_change(id, 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 mouse_kind = ADBMOUSE_EXTENDED;
1003 }
1004 else if (adb_try_handler_change(id, 0x2F)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 mouse_kind = ADBMOUSE_MICROSPEED;
1006 }
1007 else if (adb_try_handler_change(id, 0x42)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 mouse_kind = ADBMOUSE_TRACKBALLPRO;
1009 }
1010 else if (adb_try_handler_change(id, 0x66)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 mouse_kind = ADBMOUSE_MICROSPEED;
1012 }
1013 else if (adb_try_handler_change(id, 0x5F)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 mouse_kind = ADBMOUSE_MICROSPEED;
1015 }
1016 else if (adb_try_handler_change(id, 3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 mouse_kind = ADBMOUSE_MS_A3;
1018 }
1019 else if (adb_try_handler_change(id, 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 mouse_kind = ADBMOUSE_STANDARD_200;
1021 }
1022 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 mouse_kind = ADBMOUSE_STANDARD_100;
1024 }
1025
1026 if ((mouse_kind == ADBMOUSE_TRACKBALLPRO)
1027 || (mouse_kind == ADBMOUSE_MICROSPEED)) {
Finn Thain23416292018-09-11 20:18:44 -04001028 desc = "Microspeed/MacPoint or compatible";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 init_microspeed(id);
1030 } else if (mouse_kind == ADBMOUSE_MS_A3) {
Finn Thain23416292018-09-11 20:18:44 -04001031 desc = "Mouse Systems A3 Mouse or compatible";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 init_ms_a3(id);
1033 } else if (mouse_kind == ADBMOUSE_EXTENDED) {
Finn Thain23416292018-09-11 20:18:44 -04001034 desc = "extended";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /*
1036 * Register 1 is usually used for device
1037 * identification. Here, we try to identify
1038 * a known device and call the appropriate
1039 * init function.
1040 */
1041 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
1042 ADB_READREG(id, 1));
1043
1044 if ((req.reply_len) &&
1045 (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
1046 || (req.reply[2] == 0x20))) {
1047 mouse_kind = ADBMOUSE_TRACKBALL;
Finn Thain23416292018-09-11 20:18:44 -04001048 desc = "trackman/mouseman";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 init_trackball(id);
1050 }
1051 else if ((req.reply_len >= 4) &&
1052 (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
1053 (req.reply[3] == 0x61) && (req.reply[4] == 0x64)) {
1054 mouse_kind = ADBMOUSE_TRACKPAD;
Finn Thain23416292018-09-11 20:18:44 -04001055 desc = "trackpad";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 init_trackpad(id);
1057 }
1058 else if ((req.reply_len >= 4) &&
1059 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
1060 (req.reply[3] == 0x4c) && (req.reply[4] == 0x31)) {
1061 mouse_kind = ADBMOUSE_TURBOMOUSE5;
Finn Thain23416292018-09-11 20:18:44 -04001062 desc = "TurboMouse 5";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 init_turbomouse(id);
1064 }
1065 else if ((req.reply_len == 9) &&
1066 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
1067 (req.reply[3] == 0x49) && (req.reply[4] == 0x54)) {
1068 if (adb_try_handler_change(id, 0x42)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mouse_kind = ADBMOUSE_MACALLY2;
Finn Thain23416292018-09-11 20:18:44 -04001070 desc = "MacAlly 2-button";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 }
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 adb_get_infos(id, &default_id, &cur_handler_id);
Finn Thain23416292018-09-11 20:18:44 -04001076 printk(KERN_DEBUG "ADB mouse (%s) at %d has handler 0x%X\n",
1077 desc, id, cur_handler_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 reg |= adbhid_input_reregister(id, default_id, org_handler_id,
1079 cur_handler_id, mouse_kind);
1080 }
1081 adbhid_input_devcleanup(reg);
1082}
1083
1084static void
1085init_trackpad(int id)
1086{
1087 struct adb_request req;
1088 unsigned char r1_buffer[8];
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
1091 ADB_READREG(id,1));
1092 if (req.reply_len < 8)
Finn Thain23416292018-09-11 20:18:44 -04001093 pr_err("%s: bad length for reg. 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 else
1095 {
1096 memcpy(r1_buffer, &req.reply[1], 8);
1097
1098 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1099 ADB_WRITEREG(id,1),
1100 r1_buffer[0],
1101 r1_buffer[1],
1102 r1_buffer[2],
1103 r1_buffer[3],
1104 r1_buffer[4],
1105 r1_buffer[5],
1106 0x0d,
1107 r1_buffer[7]);
1108
1109 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1110 ADB_WRITEREG(id,2),
1111 0x99,
1112 0x94,
1113 0x19,
1114 0xff,
1115 0xb2,
1116 0x8a,
1117 0x1b,
1118 0x50);
1119
1120 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1121 ADB_WRITEREG(id,1),
1122 r1_buffer[0],
1123 r1_buffer[1],
1124 r1_buffer[2],
1125 r1_buffer[3],
1126 r1_buffer[4],
1127 r1_buffer[5],
1128 0x03, /*r1_buffer[6],*/
1129 r1_buffer[7]);
1130
1131 /* Without this flush, the trackpad may be locked up */
1132 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1133 }
1134}
1135
1136static void
1137init_trackball(int id)
1138{
1139 struct adb_request req;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1142 ADB_WRITEREG(id,1), 00,0x81);
1143
1144 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1145 ADB_WRITEREG(id,1), 01,0x81);
1146
1147 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1148 ADB_WRITEREG(id,1), 02,0x81);
1149
1150 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1151 ADB_WRITEREG(id,1), 03,0x38);
1152
1153 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1154 ADB_WRITEREG(id,1), 00,0x81);
1155
1156 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1157 ADB_WRITEREG(id,1), 01,0x81);
1158
1159 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1160 ADB_WRITEREG(id,1), 02,0x81);
1161
1162 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1163 ADB_WRITEREG(id,1), 03,0x38);
1164}
1165
1166static void
1167init_turbomouse(int id)
1168{
1169 struct adb_request req;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1172
1173 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
1174
1175 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1176 ADB_WRITEREG(3,2),
1177 0xe7,
1178 0x8c,
1179 0,
1180 0,
1181 0,
1182 0xff,
1183 0xff,
1184 0x94);
1185
1186 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
1187
1188 adb_request(&req, NULL, ADBREQ_SYNC, 9,
1189 ADB_WRITEREG(3,2),
1190 0xa5,
1191 0x14,
1192 0,
1193 0,
1194 0x69,
1195 0xff,
1196 0xff,
1197 0x27);
1198}
1199
1200static void
1201init_microspeed(int id)
1202{
1203 struct adb_request req;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1206
1207 /* This will initialize mice using the Microspeed, MacPoint and
1208 other compatible firmware. Bit 12 enables extended protocol.
1209
1210 Register 1 Listen (4 Bytes)
1211 0 - 3 Button is mouse (set also for double clicking!!!)
1212 4 - 7 Button is locking (affects change speed also)
1213 8 - 11 Button changes speed
1214 12 1 = Extended mouse mode, 0 = normal mouse mode
1215 13 - 15 unused 0
1216 16 - 23 normal speed
1217 24 - 31 changed speed
1218
1219 Register 1 talk holds version and product identification information.
1220 Register 1 Talk (4 Bytes):
1221 0 - 7 Product code
1222 8 - 23 undefined, reserved
1223 24 - 31 Version number
1224
1225 Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
1226 */
1227 adb_request(&req, NULL, ADBREQ_SYNC, 5,
1228 ADB_WRITEREG(id,1),
1229 0x20, /* alt speed = 0x20 (rather slow) */
1230 0x00, /* norm speed = 0x00 (fastest) */
1231 0x10, /* extended protocol, no speed change */
1232 0x07); /* all buttons enabled as mouse buttons, no locking */
1233
1234
1235 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1236}
1237
1238static void
1239init_ms_a3(int id)
1240{
1241 struct adb_request req;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 adb_request(&req, NULL, ADBREQ_SYNC, 3,
1244 ADB_WRITEREG(id, 0x2),
1245 0x00,
1246 0x07);
1247
1248 adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1249}
1250
1251static int __init adbhid_init(void)
1252{
1253#ifndef CONFIG_MAC
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001254 if (!machine_is(chrp) && !machine_is(powermac))
1255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#endif
1257
1258 led_request.complete = 1;
1259
1260 adbhid_probe();
1261
Alan Sterne041c682006-03-27 01:16:30 -08001262 blocking_notifier_chain_register(&adb_client_list,
1263 &adbhid_adb_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 return 0;
1266}
1267
1268static void __exit adbhid_exit(void)
1269{
1270}
1271
1272module_init(adbhid_init);
1273module_exit(adbhid_exit);