adbhid.c 34 KB

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