adbhid.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /*
  2. * drivers/macintosh/adbhid.c
  3. *
  4. * ADB HID driver for Power Macintosh computers.
  5. *
  6. * Adapted from drivers/macintosh/mac_keyb.c by Franz Sirl.
  7. * drivers/macintosh/mac_keyb.c was Copyright (C) 1996 Paul Mackerras
  8. * with considerable contributions from Ben Herrenschmidt and others.
  9. *
  10. * Copyright (C) 2000 Franz Sirl.
  11. *
  12. * Adapted to ADB changes and support for more devices by
  13. * Benjamin Herrenschmidt. Adapted from code in MkLinux
  14. * and reworked.
  15. *
  16. * Supported devices:
  17. *
  18. * - Standard 1 button mouse
  19. * - All standard Apple Extended protocol (handler ID 4)
  20. * - mouseman and trackman mice & trackballs
  21. * - PowerBook Trackpad (default setup: enable tapping)
  22. * - MicroSpeed mouse & trackball (needs testing)
  23. * - CH Products Trackball Pro (needs testing)
  24. * - Contour Design (Contour Mouse)
  25. * - Hunter digital (NoHandsMouse)
  26. * - Kensignton TurboMouse 5 (needs testing)
  27. * - Mouse Systems A3 mice and trackballs <aidan@kublai.com>
  28. * - MacAlly 2-buttons mouse (needs testing) <pochini@denise.shiny.it>
  29. *
  30. * To do:
  31. *
  32. * Improve Kensington support.
  33. * Split mouse/kbd
  34. * Move to syfs
  35. */
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <linux/notifier.h>
  40. #include <linux/input.h>
  41. #include <linux/adb.h>
  42. #include <linux/cuda.h>
  43. #include <linux/pmu.h>
  44. #include <asm/machdep.h>
  45. #ifdef CONFIG_PPC_PMAC
  46. #include <asm/backlight.h>
  47. #include <asm/pmac_feature.h>
  48. #endif
  49. MODULE_AUTHOR("Franz Sirl <Franz.Sirl-kernel@lauterbach.com>");
  50. static int restore_capslock_events;
  51. module_param(restore_capslock_events, int, 0644);
  52. MODULE_PARM_DESC(restore_capslock_events,
  53. "Produce keypress events for capslock on both keyup and keydown.");
  54. #define KEYB_KEYREG 0 /* register # for key up/down data */
  55. #define KEYB_LEDREG 2 /* register # for leds on ADB keyboard */
  56. #define MOUSE_DATAREG 0 /* reg# for movement/button codes from mouse */
  57. static int adb_message_handler(struct notifier_block *, unsigned long, void *);
  58. static struct notifier_block adbhid_adb_notifier = {
  59. .notifier_call = adb_message_handler,
  60. };
  61. /* Some special keys */
  62. #define ADB_KEY_DEL 0x33
  63. #define ADB_KEY_CMD 0x37
  64. #define ADB_KEY_CAPSLOCK 0x39
  65. #define ADB_KEY_FN 0x3f
  66. #define ADB_KEY_FWDEL 0x75
  67. #define ADB_KEY_POWER_OLD 0x7e
  68. #define ADB_KEY_POWER 0x7f
  69. static const u16 adb_to_linux_keycodes[128] = {
  70. /* 0x00 */ KEY_A, /* 30 */
  71. /* 0x01 */ KEY_S, /* 31 */
  72. /* 0x02 */ KEY_D, /* 32 */
  73. /* 0x03 */ KEY_F, /* 33 */
  74. /* 0x04 */ KEY_H, /* 35 */
  75. /* 0x05 */ KEY_G, /* 34 */
  76. /* 0x06 */ KEY_Z, /* 44 */
  77. /* 0x07 */ KEY_X, /* 45 */
  78. /* 0x08 */ KEY_C, /* 46 */
  79. /* 0x09 */ KEY_V, /* 47 */
  80. /* 0x0a */ KEY_102ND, /* 86 */
  81. /* 0x0b */ KEY_B, /* 48 */
  82. /* 0x0c */ KEY_Q, /* 16 */
  83. /* 0x0d */ KEY_W, /* 17 */
  84. /* 0x0e */ KEY_E, /* 18 */
  85. /* 0x0f */ KEY_R, /* 19 */
  86. /* 0x10 */ KEY_Y, /* 21 */
  87. /* 0x11 */ KEY_T, /* 20 */
  88. /* 0x12 */ KEY_1, /* 2 */
  89. /* 0x13 */ KEY_2, /* 3 */
  90. /* 0x14 */ KEY_3, /* 4 */
  91. /* 0x15 */ KEY_4, /* 5 */
  92. /* 0x16 */ KEY_6, /* 7 */
  93. /* 0x17 */ KEY_5, /* 6 */
  94. /* 0x18 */ KEY_EQUAL, /* 13 */
  95. /* 0x19 */ KEY_9, /* 10 */
  96. /* 0x1a */ KEY_7, /* 8 */
  97. /* 0x1b */ KEY_MINUS, /* 12 */
  98. /* 0x1c */ KEY_8, /* 9 */
  99. /* 0x1d */ KEY_0, /* 11 */
  100. /* 0x1e */ KEY_RIGHTBRACE, /* 27 */
  101. /* 0x1f */ KEY_O, /* 24 */
  102. /* 0x20 */ KEY_U, /* 22 */
  103. /* 0x21 */ KEY_LEFTBRACE, /* 26 */
  104. /* 0x22 */ KEY_I, /* 23 */
  105. /* 0x23 */ KEY_P, /* 25 */
  106. /* 0x24 */ KEY_ENTER, /* 28 */
  107. /* 0x25 */ KEY_L, /* 38 */
  108. /* 0x26 */ KEY_J, /* 36 */
  109. /* 0x27 */ KEY_APOSTROPHE, /* 40 */
  110. /* 0x28 */ KEY_K, /* 37 */
  111. /* 0x29 */ KEY_SEMICOLON, /* 39 */
  112. /* 0x2a */ KEY_BACKSLASH, /* 43 */
  113. /* 0x2b */ KEY_COMMA, /* 51 */
  114. /* 0x2c */ KEY_SLASH, /* 53 */
  115. /* 0x2d */ KEY_N, /* 49 */
  116. /* 0x2e */ KEY_M, /* 50 */
  117. /* 0x2f */ KEY_DOT, /* 52 */
  118. /* 0x30 */ KEY_TAB, /* 15 */
  119. /* 0x31 */ KEY_SPACE, /* 57 */
  120. /* 0x32 */ KEY_GRAVE, /* 41 */
  121. /* 0x33 */ KEY_BACKSPACE, /* 14 */
  122. /* 0x34 */ KEY_KPENTER, /* 96 */
  123. /* 0x35 */ KEY_ESC, /* 1 */
  124. /* 0x36 */ KEY_LEFTCTRL, /* 29 */
  125. /* 0x37 */ KEY_LEFTMETA, /* 125 */
  126. /* 0x38 */ KEY_LEFTSHIFT, /* 42 */
  127. /* 0x39 */ KEY_CAPSLOCK, /* 58 */
  128. /* 0x3a */ KEY_LEFTALT, /* 56 */
  129. /* 0x3b */ KEY_LEFT, /* 105 */
  130. /* 0x3c */ KEY_RIGHT, /* 106 */
  131. /* 0x3d */ KEY_DOWN, /* 108 */
  132. /* 0x3e */ KEY_UP, /* 103 */
  133. /* 0x3f */ KEY_FN, /* 0x1d0 */
  134. /* 0x40 */ 0,
  135. /* 0x41 */ KEY_KPDOT, /* 83 */
  136. /* 0x42 */ 0,
  137. /* 0x43 */ KEY_KPASTERISK, /* 55 */
  138. /* 0x44 */ 0,
  139. /* 0x45 */ KEY_KPPLUS, /* 78 */
  140. /* 0x46 */ 0,
  141. /* 0x47 */ KEY_NUMLOCK, /* 69 */
  142. /* 0x48 */ 0,
  143. /* 0x49 */ 0,
  144. /* 0x4a */ 0,
  145. /* 0x4b */ KEY_KPSLASH, /* 98 */
  146. /* 0x4c */ KEY_KPENTER, /* 96 */
  147. /* 0x4d */ 0,
  148. /* 0x4e */ KEY_KPMINUS, /* 74 */
  149. /* 0x4f */ 0,
  150. /* 0x50 */ 0,
  151. /* 0x51 */ KEY_KPEQUAL, /* 117 */
  152. /* 0x52 */ KEY_KP0, /* 82 */
  153. /* 0x53 */ KEY_KP1, /* 79 */
  154. /* 0x54 */ KEY_KP2, /* 80 */
  155. /* 0x55 */ KEY_KP3, /* 81 */
  156. /* 0x56 */ KEY_KP4, /* 75 */
  157. /* 0x57 */ KEY_KP5, /* 76 */
  158. /* 0x58 */ KEY_KP6, /* 77 */
  159. /* 0x59 */ KEY_KP7, /* 71 */
  160. /* 0x5a */ 0,
  161. /* 0x5b */ KEY_KP8, /* 72 */
  162. /* 0x5c */ KEY_KP9, /* 73 */
  163. /* 0x5d */ KEY_YEN, /* 124 */
  164. /* 0x5e */ KEY_RO, /* 89 */
  165. /* 0x5f */ KEY_KPCOMMA, /* 121 */
  166. /* 0x60 */ KEY_F5, /* 63 */
  167. /* 0x61 */ KEY_F6, /* 64 */
  168. /* 0x62 */ KEY_F7, /* 65 */
  169. /* 0x63 */ KEY_F3, /* 61 */
  170. /* 0x64 */ KEY_F8, /* 66 */
  171. /* 0x65 */ KEY_F9, /* 67 */
  172. /* 0x66 */ KEY_HANJA, /* 123 */
  173. /* 0x67 */ KEY_F11, /* 87 */
  174. /* 0x68 */ KEY_HANGEUL, /* 122 */
  175. /* 0x69 */ KEY_SYSRQ, /* 99 */
  176. /* 0x6a */ 0,
  177. /* 0x6b */ KEY_SCROLLLOCK, /* 70 */
  178. /* 0x6c */ 0,
  179. /* 0x6d */ KEY_F10, /* 68 */
  180. /* 0x6e */ KEY_COMPOSE, /* 127 */
  181. /* 0x6f */ KEY_F12, /* 88 */
  182. /* 0x70 */ 0,
  183. /* 0x71 */ KEY_PAUSE, /* 119 */
  184. /* 0x72 */ KEY_INSERT, /* 110 */
  185. /* 0x73 */ KEY_HOME, /* 102 */
  186. /* 0x74 */ KEY_PAGEUP, /* 104 */
  187. /* 0x75 */ KEY_DELETE, /* 111 */
  188. /* 0x76 */ KEY_F4, /* 62 */
  189. /* 0x77 */ KEY_END, /* 107 */
  190. /* 0x78 */ KEY_F2, /* 60 */
  191. /* 0x79 */ KEY_PAGEDOWN, /* 109 */
  192. /* 0x7a */ KEY_F1, /* 59 */
  193. /* 0x7b */ KEY_RIGHTSHIFT, /* 54 */
  194. /* 0x7c */ KEY_RIGHTALT, /* 100 */
  195. /* 0x7d */ KEY_RIGHTCTRL, /* 97 */
  196. /* 0x7e */ KEY_RIGHTMETA, /* 126 */
  197. /* 0x7f */ KEY_POWER, /* 116 */
  198. };
  199. struct adbhid {
  200. struct input_dev *input;
  201. int id;
  202. int default_id;
  203. int original_handler_id;
  204. int current_handler_id;
  205. int mouse_kind;
  206. u16 *keycode;
  207. char name[64];
  208. char phys[32];
  209. int flags;
  210. };
  211. #define FLAG_FN_KEY_PRESSED 0x00000001
  212. #define FLAG_POWER_FROM_FN 0x00000002
  213. #define FLAG_EMU_FWDEL_DOWN 0x00000004
  214. #define FLAG_CAPSLOCK_TRANSLATE 0x00000008
  215. #define FLAG_CAPSLOCK_DOWN 0x00000010
  216. #define FLAG_CAPSLOCK_IGNORE_NEXT 0x00000020
  217. #define FLAG_POWER_KEY_PRESSED 0x00000040
  218. static struct adbhid *adbhid[16];
  219. static void adbhid_probe(void);
  220. static void adbhid_input_keycode(int, int, int);
  221. static void init_trackpad(int id);
  222. static void init_trackball(int id);
  223. static void init_turbomouse(int id);
  224. static void init_microspeed(int id);
  225. static void init_ms_a3(int id);
  226. static struct adb_ids keyboard_ids;
  227. static struct adb_ids mouse_ids;
  228. static struct adb_ids buttons_ids;
  229. /* Kind of keyboard, see Apple technote 1152 */
  230. #define ADB_KEYBOARD_UNKNOWN 0
  231. #define ADB_KEYBOARD_ANSI 0x0100
  232. #define ADB_KEYBOARD_ISO 0x0200
  233. #define ADB_KEYBOARD_JIS 0x0300
  234. /* Kind of mouse */
  235. #define ADBMOUSE_STANDARD_100 0 /* Standard 100cpi mouse (handler 1) */
  236. #define ADBMOUSE_STANDARD_200 1 /* Standard 200cpi mouse (handler 2) */
  237. #define ADBMOUSE_EXTENDED 2 /* Apple Extended mouse (handler 4) */
  238. #define ADBMOUSE_TRACKBALL 3 /* TrackBall (handler 4) */
  239. #define ADBMOUSE_TRACKPAD 4 /* Apple's PowerBook trackpad (handler 4) */
  240. #define ADBMOUSE_TURBOMOUSE5 5 /* Turbomouse 5 (previously req. mousehack) */
  241. #define ADBMOUSE_MICROSPEED 6 /* Microspeed mouse (&trackball ?), MacPoint */
  242. #define ADBMOUSE_TRACKBALLPRO 7 /* Trackball Pro (special buttons) */
  243. #define ADBMOUSE_MS_A3 8 /* Mouse systems A3 trackball (handler 3) */
  244. #define ADBMOUSE_MACALLY2 9 /* MacAlly 2-button mouse */
  245. static void
  246. adbhid_keyboard_input(unsigned char *data, int nb, int apoll)
  247. {
  248. int id = (data[0] >> 4) & 0x0f;
  249. if (!adbhid[id]) {
  250. printk(KERN_ERR "ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
  251. id, data[0], data[1], data[2], data[3]);
  252. return;
  253. }
  254. /* first check this is from register 0 */
  255. if (nb != 3 || (data[0] & 3) != KEYB_KEYREG)
  256. return; /* ignore it */
  257. adbhid_input_keycode(id, data[1], 0);
  258. if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
  259. adbhid_input_keycode(id, data[2], 0);
  260. }
  261. static void
  262. adbhid_input_keycode(int id, int scancode, int repeat)
  263. {
  264. struct adbhid *ahid = adbhid[id];
  265. int keycode, up_flag, key;
  266. keycode = scancode & 0x7f;
  267. up_flag = scancode & 0x80;
  268. if (restore_capslock_events) {
  269. if (keycode == ADB_KEY_CAPSLOCK && !up_flag) {
  270. /* Key pressed, turning on the CapsLock LED.
  271. * The next 0xff will be interpreted as a release. */
  272. if (ahid->flags & FLAG_CAPSLOCK_IGNORE_NEXT) {
  273. /* Throw away this key event if it happens
  274. * just after resume. */
  275. ahid->flags &= ~FLAG_CAPSLOCK_IGNORE_NEXT;
  276. return;
  277. } else {
  278. ahid->flags |= FLAG_CAPSLOCK_TRANSLATE
  279. | FLAG_CAPSLOCK_DOWN;
  280. }
  281. } else if (scancode == 0xff &&
  282. !(ahid->flags & FLAG_POWER_KEY_PRESSED)) {
  283. /* Scancode 0xff usually signifies that the capslock
  284. * key was either pressed or released, or that the
  285. * power button was released. */
  286. if (ahid->flags & FLAG_CAPSLOCK_TRANSLATE) {
  287. keycode = ADB_KEY_CAPSLOCK;
  288. if (ahid->flags & FLAG_CAPSLOCK_DOWN) {
  289. /* Key released */
  290. up_flag = 1;
  291. ahid->flags &= ~FLAG_CAPSLOCK_DOWN;
  292. } else {
  293. /* Key pressed */
  294. up_flag = 0;
  295. ahid->flags &= ~FLAG_CAPSLOCK_TRANSLATE;
  296. }
  297. } else {
  298. printk(KERN_INFO "Spurious caps lock event "
  299. "(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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_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. if (adbhid[id]) {
  675. printk(KERN_ERR "Trying to reregister ADB HID on ID %d\n", id);
  676. return -EEXIST;
  677. }
  678. adbhid[id] = hid = kzalloc(sizeof(struct adbhid), GFP_KERNEL);
  679. input_dev = input_allocate_device();
  680. if (!hid || !input_dev) {
  681. err = -ENOMEM;
  682. goto fail;
  683. }
  684. sprintf(hid->phys, "adb%d:%d.%02x/input", id, default_id, original_handler_id);
  685. hid->input = input_dev;
  686. hid->id = default_id;
  687. hid->original_handler_id = original_handler_id;
  688. hid->current_handler_id = current_handler_id;
  689. hid->mouse_kind = mouse_kind;
  690. hid->flags = 0;
  691. input_set_drvdata(input_dev, hid);
  692. input_dev->name = hid->name;
  693. input_dev->phys = hid->phys;
  694. input_dev->id.bustype = BUS_ADB;
  695. input_dev->id.vendor = 0x0001;
  696. input_dev->id.product = (id << 12) | (default_id << 8) | original_handler_id;
  697. input_dev->id.version = 0x0100;
  698. switch (default_id) {
  699. case ADB_KEYBOARD:
  700. hid->keycode = kmalloc(sizeof(adb_to_linux_keycodes), GFP_KERNEL);
  701. if (!hid->keycode) {
  702. err = -ENOMEM;
  703. goto fail;
  704. }
  705. sprintf(hid->name, "ADB keyboard");
  706. memcpy(hid->keycode, adb_to_linux_keycodes, sizeof(adb_to_linux_keycodes));
  707. printk(KERN_INFO "Detected ADB keyboard, type ");
  708. switch (original_handler_id) {
  709. default:
  710. printk("<unknown>.\n");
  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. printk("ANSI.\n");
  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. printk("ISO, swapping keys.\n");
  723. input_dev->id.version = ADB_KEYBOARD_ISO;
  724. i = hid->keycode[10];
  725. hid->keycode[10] = hid->keycode[50];
  726. hid->keycode[50] = i;
  727. break;
  728. case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
  729. case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
  730. printk("JIS.\n");
  731. input_dev->id.version = ADB_KEYBOARD_JIS;
  732. break;
  733. }
  734. for (i = 0; i < 128; i++)
  735. if (hid->keycode[i])
  736. set_bit(hid->keycode[i], input_dev->keybit);
  737. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
  738. BIT_MASK(EV_REP);
  739. input_dev->ledbit[0] = BIT_MASK(LED_SCROLLL) |
  740. BIT_MASK(LED_CAPSL) | BIT_MASK(LED_NUML);
  741. input_dev->event = adbhid_kbd_event;
  742. input_dev->keycodemax = KEY_FN;
  743. input_dev->keycodesize = sizeof(hid->keycode[0]);
  744. break;
  745. case ADB_MOUSE:
  746. sprintf(hid->name, "ADB mouse");
  747. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  748. input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  749. BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  750. input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  751. break;
  752. case ADB_MISC:
  753. switch (original_handler_id) {
  754. case 0x02: /* Adjustable keyboard button device */
  755. sprintf(hid->name, "ADB adjustable keyboard buttons");
  756. input_dev->evbit[0] = BIT_MASK(EV_KEY) |
  757. BIT_MASK(EV_REP);
  758. set_bit(KEY_SOUND, input_dev->keybit);
  759. set_bit(KEY_MUTE, input_dev->keybit);
  760. set_bit(KEY_VOLUMEUP, input_dev->keybit);
  761. set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
  762. break;
  763. case 0x1f: /* Powerbook button device */
  764. sprintf(hid->name, "ADB Powerbook buttons");
  765. input_dev->evbit[0] = BIT_MASK(EV_KEY) |
  766. BIT_MASK(EV_REP);
  767. set_bit(KEY_MUTE, input_dev->keybit);
  768. set_bit(KEY_VOLUMEUP, input_dev->keybit);
  769. set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
  770. set_bit(KEY_BRIGHTNESSUP, input_dev->keybit);
  771. set_bit(KEY_BRIGHTNESSDOWN, input_dev->keybit);
  772. set_bit(KEY_EJECTCD, input_dev->keybit);
  773. set_bit(KEY_SWITCHVIDEOMODE, input_dev->keybit);
  774. set_bit(KEY_KBDILLUMTOGGLE, input_dev->keybit);
  775. set_bit(KEY_KBDILLUMDOWN, input_dev->keybit);
  776. set_bit(KEY_KBDILLUMUP, input_dev->keybit);
  777. break;
  778. }
  779. if (hid->name[0])
  780. break;
  781. /* else fall through */
  782. default:
  783. printk(KERN_INFO "Trying to register unknown ADB device to input layer.\n");
  784. err = -ENODEV;
  785. goto fail;
  786. }
  787. input_dev->keycode = hid->keycode;
  788. err = input_register_device(input_dev);
  789. if (err)
  790. goto fail;
  791. if (default_id == ADB_KEYBOARD) {
  792. /* HACK WARNING!! This should go away as soon there is an utility
  793. * to control that for event devices.
  794. */
  795. input_dev->rep[REP_DELAY] = 500; /* input layer default: 250 */
  796. input_dev->rep[REP_PERIOD] = 66; /* input layer default: 33 */
  797. }
  798. return 0;
  799. fail: input_free_device(input_dev);
  800. if (hid) {
  801. kfree(hid->keycode);
  802. kfree(hid);
  803. }
  804. adbhid[id] = NULL;
  805. return err;
  806. }
  807. static void adbhid_input_unregister(int id)
  808. {
  809. input_unregister_device(adbhid[id]->input);
  810. kfree(adbhid[id]->keycode);
  811. kfree(adbhid[id]);
  812. adbhid[id] = NULL;
  813. }
  814. static u16
  815. adbhid_input_reregister(int id, int default_id, int org_handler_id,
  816. int cur_handler_id, int mk)
  817. {
  818. if (adbhid[id]) {
  819. if (adbhid[id]->input->id.product !=
  820. ((id << 12)|(default_id << 8)|org_handler_id)) {
  821. adbhid_input_unregister(id);
  822. adbhid_input_register(id, default_id, org_handler_id,
  823. cur_handler_id, mk);
  824. }
  825. } else
  826. adbhid_input_register(id, default_id, org_handler_id,
  827. cur_handler_id, mk);
  828. return 1<<id;
  829. }
  830. static void
  831. adbhid_input_devcleanup(u16 exist)
  832. {
  833. int i;
  834. for(i=1; i<16; i++)
  835. if (adbhid[i] && !(exist&(1<<i)))
  836. adbhid_input_unregister(i);
  837. }
  838. static void
  839. adbhid_probe(void)
  840. {
  841. struct adb_request req;
  842. int i, default_id, org_handler_id, cur_handler_id;
  843. u16 reg = 0;
  844. adb_register(ADB_MOUSE, 0, &mouse_ids, adbhid_mouse_input);
  845. adb_register(ADB_KEYBOARD, 0, &keyboard_ids, adbhid_keyboard_input);
  846. adb_register(ADB_MISC, 0, &buttons_ids, adbhid_buttons_input);
  847. for (i = 0; i < keyboard_ids.nids; i++) {
  848. int id = keyboard_ids.id[i];
  849. adb_get_infos(id, &default_id, &org_handler_id);
  850. /* turn off all leds */
  851. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  852. ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
  853. /* Enable full feature set of the keyboard
  854. ->get it to send separate codes for left and right shift,
  855. control, option keys */
  856. #if 0 /* handler 5 doesn't send separate codes for R modifiers */
  857. if (adb_try_handler_change(id, 5))
  858. printk("ADB keyboard at %d, handler set to 5\n", id);
  859. else
  860. #endif
  861. if (adb_try_handler_change(id, 3))
  862. printk("ADB keyboard at %d, handler set to 3\n", id);
  863. else
  864. printk("ADB keyboard at %d, handler 1\n", id);
  865. adb_get_infos(id, &default_id, &cur_handler_id);
  866. reg |= adbhid_input_reregister(id, default_id, org_handler_id,
  867. cur_handler_id, 0);
  868. }
  869. for (i = 0; i < buttons_ids.nids; i++) {
  870. int id = buttons_ids.id[i];
  871. adb_get_infos(id, &default_id, &org_handler_id);
  872. reg |= adbhid_input_reregister(id, default_id, org_handler_id,
  873. org_handler_id, 0);
  874. }
  875. /* Try to switch all mice to handler 4, or 2 for three-button
  876. mode and full resolution. */
  877. for (i = 0; i < mouse_ids.nids; i++) {
  878. int id = mouse_ids.id[i];
  879. int mouse_kind;
  880. adb_get_infos(id, &default_id, &org_handler_id);
  881. if (adb_try_handler_change(id, 4)) {
  882. printk("ADB mouse at %d, handler set to 4", id);
  883. mouse_kind = ADBMOUSE_EXTENDED;
  884. }
  885. else if (adb_try_handler_change(id, 0x2F)) {
  886. printk("ADB mouse at %d, handler set to 0x2F", id);
  887. mouse_kind = ADBMOUSE_MICROSPEED;
  888. }
  889. else if (adb_try_handler_change(id, 0x42)) {
  890. printk("ADB mouse at %d, handler set to 0x42", id);
  891. mouse_kind = ADBMOUSE_TRACKBALLPRO;
  892. }
  893. else if (adb_try_handler_change(id, 0x66)) {
  894. printk("ADB mouse at %d, handler set to 0x66", id);
  895. mouse_kind = ADBMOUSE_MICROSPEED;
  896. }
  897. else if (adb_try_handler_change(id, 0x5F)) {
  898. printk("ADB mouse at %d, handler set to 0x5F", id);
  899. mouse_kind = ADBMOUSE_MICROSPEED;
  900. }
  901. else if (adb_try_handler_change(id, 3)) {
  902. printk("ADB mouse at %d, handler set to 3", id);
  903. mouse_kind = ADBMOUSE_MS_A3;
  904. }
  905. else if (adb_try_handler_change(id, 2)) {
  906. printk("ADB mouse at %d, handler set to 2", id);
  907. mouse_kind = ADBMOUSE_STANDARD_200;
  908. }
  909. else {
  910. printk("ADB mouse at %d, handler 1", id);
  911. mouse_kind = ADBMOUSE_STANDARD_100;
  912. }
  913. if ((mouse_kind == ADBMOUSE_TRACKBALLPRO)
  914. || (mouse_kind == ADBMOUSE_MICROSPEED)) {
  915. init_microspeed(id);
  916. } else if (mouse_kind == ADBMOUSE_MS_A3) {
  917. init_ms_a3(id);
  918. } else if (mouse_kind == ADBMOUSE_EXTENDED) {
  919. /*
  920. * Register 1 is usually used for device
  921. * identification. Here, we try to identify
  922. * a known device and call the appropriate
  923. * init function.
  924. */
  925. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  926. ADB_READREG(id, 1));
  927. if ((req.reply_len) &&
  928. (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
  929. || (req.reply[2] == 0x20))) {
  930. mouse_kind = ADBMOUSE_TRACKBALL;
  931. init_trackball(id);
  932. }
  933. else if ((req.reply_len >= 4) &&
  934. (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
  935. (req.reply[3] == 0x61) && (req.reply[4] == 0x64)) {
  936. mouse_kind = ADBMOUSE_TRACKPAD;
  937. init_trackpad(id);
  938. }
  939. else if ((req.reply_len >= 4) &&
  940. (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
  941. (req.reply[3] == 0x4c) && (req.reply[4] == 0x31)) {
  942. mouse_kind = ADBMOUSE_TURBOMOUSE5;
  943. init_turbomouse(id);
  944. }
  945. else if ((req.reply_len == 9) &&
  946. (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
  947. (req.reply[3] == 0x49) && (req.reply[4] == 0x54)) {
  948. if (adb_try_handler_change(id, 0x42)) {
  949. printk("\nADB MacAlly 2-button mouse at %d, handler set to 0x42", id);
  950. mouse_kind = ADBMOUSE_MACALLY2;
  951. }
  952. }
  953. }
  954. printk("\n");
  955. adb_get_infos(id, &default_id, &cur_handler_id);
  956. reg |= adbhid_input_reregister(id, default_id, org_handler_id,
  957. cur_handler_id, mouse_kind);
  958. }
  959. adbhid_input_devcleanup(reg);
  960. }
  961. static void
  962. init_trackpad(int id)
  963. {
  964. struct adb_request req;
  965. unsigned char r1_buffer[8];
  966. printk(" (trackpad)");
  967. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  968. ADB_READREG(id,1));
  969. if (req.reply_len < 8)
  970. printk("bad length for reg. 1\n");
  971. else
  972. {
  973. memcpy(r1_buffer, &req.reply[1], 8);
  974. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  975. ADB_WRITEREG(id,1),
  976. r1_buffer[0],
  977. r1_buffer[1],
  978. r1_buffer[2],
  979. r1_buffer[3],
  980. r1_buffer[4],
  981. r1_buffer[5],
  982. 0x0d,
  983. r1_buffer[7]);
  984. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  985. ADB_WRITEREG(id,2),
  986. 0x99,
  987. 0x94,
  988. 0x19,
  989. 0xff,
  990. 0xb2,
  991. 0x8a,
  992. 0x1b,
  993. 0x50);
  994. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  995. ADB_WRITEREG(id,1),
  996. r1_buffer[0],
  997. r1_buffer[1],
  998. r1_buffer[2],
  999. r1_buffer[3],
  1000. r1_buffer[4],
  1001. r1_buffer[5],
  1002. 0x03, /*r1_buffer[6],*/
  1003. r1_buffer[7]);
  1004. /* Without this flush, the trackpad may be locked up */
  1005. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1006. }
  1007. }
  1008. static void
  1009. init_trackball(int id)
  1010. {
  1011. struct adb_request req;
  1012. printk(" (trackman/mouseman)");
  1013. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1014. ADB_WRITEREG(id,1), 00,0x81);
  1015. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1016. ADB_WRITEREG(id,1), 01,0x81);
  1017. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1018. ADB_WRITEREG(id,1), 02,0x81);
  1019. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1020. ADB_WRITEREG(id,1), 03,0x38);
  1021. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1022. ADB_WRITEREG(id,1), 00,0x81);
  1023. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1024. ADB_WRITEREG(id,1), 01,0x81);
  1025. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1026. ADB_WRITEREG(id,1), 02,0x81);
  1027. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1028. ADB_WRITEREG(id,1), 03,0x38);
  1029. }
  1030. static void
  1031. init_turbomouse(int id)
  1032. {
  1033. struct adb_request req;
  1034. printk(" (TurboMouse 5)");
  1035. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1036. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
  1037. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  1038. ADB_WRITEREG(3,2),
  1039. 0xe7,
  1040. 0x8c,
  1041. 0,
  1042. 0,
  1043. 0,
  1044. 0xff,
  1045. 0xff,
  1046. 0x94);
  1047. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
  1048. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  1049. ADB_WRITEREG(3,2),
  1050. 0xa5,
  1051. 0x14,
  1052. 0,
  1053. 0,
  1054. 0x69,
  1055. 0xff,
  1056. 0xff,
  1057. 0x27);
  1058. }
  1059. static void
  1060. init_microspeed(int id)
  1061. {
  1062. struct adb_request req;
  1063. printk(" (Microspeed/MacPoint or compatible)");
  1064. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1065. /* This will initialize mice using the Microspeed, MacPoint and
  1066. other compatible firmware. Bit 12 enables extended protocol.
  1067. Register 1 Listen (4 Bytes)
  1068. 0 - 3 Button is mouse (set also for double clicking!!!)
  1069. 4 - 7 Button is locking (affects change speed also)
  1070. 8 - 11 Button changes speed
  1071. 12 1 = Extended mouse mode, 0 = normal mouse mode
  1072. 13 - 15 unused 0
  1073. 16 - 23 normal speed
  1074. 24 - 31 changed speed
  1075. Register 1 talk holds version and product identification information.
  1076. Register 1 Talk (4 Bytes):
  1077. 0 - 7 Product code
  1078. 8 - 23 undefined, reserved
  1079. 24 - 31 Version number
  1080. Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
  1081. */
  1082. adb_request(&req, NULL, ADBREQ_SYNC, 5,
  1083. ADB_WRITEREG(id,1),
  1084. 0x20, /* alt speed = 0x20 (rather slow) */
  1085. 0x00, /* norm speed = 0x00 (fastest) */
  1086. 0x10, /* extended protocol, no speed change */
  1087. 0x07); /* all buttons enabled as mouse buttons, no locking */
  1088. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1089. }
  1090. static void
  1091. init_ms_a3(int id)
  1092. {
  1093. struct adb_request req;
  1094. printk(" (Mouse Systems A3 Mouse, or compatible)");
  1095. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1096. ADB_WRITEREG(id, 0x2),
  1097. 0x00,
  1098. 0x07);
  1099. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1100. }
  1101. static int __init adbhid_init(void)
  1102. {
  1103. #ifndef CONFIG_MAC
  1104. if (!machine_is(chrp) && !machine_is(powermac))
  1105. return 0;
  1106. #endif
  1107. led_request.complete = 1;
  1108. adbhid_probe();
  1109. blocking_notifier_chain_register(&adb_client_list,
  1110. &adbhid_adb_notifier);
  1111. return 0;
  1112. }
  1113. static void __exit adbhid_exit(void)
  1114. {
  1115. }
  1116. module_init(adbhid_init);
  1117. module_exit(adbhid_exit);