adbhid.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 <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. #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 <Franz.Sirl-kernel@lauterbach.com>");
  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. printk(KERN_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. printk(KERN_INFO "Spurious caps lock event "
  300. "(scancode 0xff).\n");
  301. }
  302. }
  303. }
  304. switch (keycode) {
  305. case ADB_KEY_CAPSLOCK:
  306. if (!restore_capslock_events) {
  307. /* Generate down/up events for CapsLock every time. */
  308. input_report_key(ahid->input, KEY_CAPSLOCK, 1);
  309. input_sync(ahid->input);
  310. input_report_key(ahid->input, KEY_CAPSLOCK, 0);
  311. input_sync(ahid->input);
  312. return;
  313. }
  314. break;
  315. #ifdef CONFIG_PPC_PMAC
  316. case ADB_KEY_POWER_OLD: /* Power key on PBook 3400 needs remapping */
  317. switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO,
  318. NULL, PMAC_MB_INFO_MODEL, 0)) {
  319. case PMAC_TYPE_COMET:
  320. case PMAC_TYPE_HOOPER:
  321. case PMAC_TYPE_KANGA:
  322. keycode = ADB_KEY_POWER;
  323. }
  324. break;
  325. case ADB_KEY_POWER:
  326. /* Keep track of the power key state */
  327. if (up_flag)
  328. ahid->flags &= ~FLAG_POWER_KEY_PRESSED;
  329. else
  330. ahid->flags |= FLAG_POWER_KEY_PRESSED;
  331. /* Fn + Command will produce a bogus "power" keycode */
  332. if (ahid->flags & FLAG_FN_KEY_PRESSED) {
  333. keycode = ADB_KEY_CMD;
  334. if (up_flag)
  335. ahid->flags &= ~FLAG_POWER_FROM_FN;
  336. else
  337. ahid->flags |= FLAG_POWER_FROM_FN;
  338. } else if (ahid->flags & FLAG_POWER_FROM_FN) {
  339. keycode = ADB_KEY_CMD;
  340. ahid->flags &= ~FLAG_POWER_FROM_FN;
  341. }
  342. break;
  343. case ADB_KEY_FN:
  344. /* Keep track of the Fn key state */
  345. if (up_flag) {
  346. ahid->flags &= ~FLAG_FN_KEY_PRESSED;
  347. /* Emulate Fn+delete = forward delete */
  348. if (ahid->flags & FLAG_EMU_FWDEL_DOWN) {
  349. ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
  350. keycode = ADB_KEY_FWDEL;
  351. break;
  352. }
  353. } else
  354. ahid->flags |= FLAG_FN_KEY_PRESSED;
  355. break;
  356. case ADB_KEY_DEL:
  357. /* Emulate Fn+delete = forward delete */
  358. if (ahid->flags & FLAG_FN_KEY_PRESSED) {
  359. keycode = ADB_KEY_FWDEL;
  360. if (up_flag)
  361. ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
  362. else
  363. ahid->flags |= FLAG_EMU_FWDEL_DOWN;
  364. }
  365. break;
  366. #endif /* CONFIG_PPC_PMAC */
  367. }
  368. key = adbhid[id]->keycode[keycode];
  369. if (key) {
  370. input_report_key(adbhid[id]->input, key, !up_flag);
  371. input_sync(adbhid[id]->input);
  372. } else
  373. printk(KERN_INFO "Unhandled ADB key (scancode %#02x) %s.\n", keycode,
  374. up_flag ? "released" : "pressed");
  375. }
  376. static void
  377. adbhid_mouse_input(unsigned char *data, int nb, int autopoll)
  378. {
  379. int id = (data[0] >> 4) & 0x0f;
  380. if (!adbhid[id]) {
  381. printk(KERN_ERR "ADB HID on ID %d not yet registered\n", id);
  382. return;
  383. }
  384. /*
  385. Handler 1 -- 100cpi original Apple mouse protocol.
  386. Handler 2 -- 200cpi original Apple mouse protocol.
  387. For Apple's standard one-button mouse protocol the data array will
  388. contain the following values:
  389. BITS COMMENTS
  390. data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
  391. data[1] = bxxx xxxx First button and x-axis motion.
  392. data[2] = byyy yyyy Second button and y-axis motion.
  393. Handler 4 -- Apple Extended mouse protocol.
  394. For Apple's 3-button mouse protocol the data array will contain the
  395. following values:
  396. BITS COMMENTS
  397. data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
  398. data[1] = bxxx xxxx Left button and x-axis motion.
  399. data[2] = byyy yyyy Second button and y-axis motion.
  400. data[3] = byyy bxxx Third button and fourth button. Y is additional
  401. high bits of y-axis motion. XY is additional
  402. high bits of x-axis motion.
  403. MacAlly 2-button mouse protocol.
  404. For MacAlly 2-button mouse protocol the data array will contain the
  405. following values:
  406. BITS COMMENTS
  407. data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
  408. data[1] = bxxx xxxx Left button and x-axis motion.
  409. data[2] = byyy yyyy Right button and y-axis motion.
  410. data[3] = ???? ???? unknown
  411. data[4] = ???? ???? unknown
  412. */
  413. /* If it's a trackpad, we alias the second button to the first.
  414. NOTE: Apple sends an ADB flush command to the trackpad when
  415. the first (the real) button is released. We could do
  416. this here using async flush requests.
  417. */
  418. switch (adbhid[id]->mouse_kind)
  419. {
  420. case ADBMOUSE_TRACKPAD:
  421. data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
  422. data[2] = data[2] | 0x80;
  423. break;
  424. case ADBMOUSE_MICROSPEED:
  425. data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
  426. data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
  427. data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
  428. | (data[3] & 0x08);
  429. break;
  430. case ADBMOUSE_TRACKBALLPRO:
  431. data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
  432. & ((data[3] & 0x08) << 4));
  433. data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
  434. data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
  435. break;
  436. case ADBMOUSE_MS_A3:
  437. data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
  438. data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
  439. data[3] = ((data[3] & 0x04) << 5);
  440. break;
  441. case ADBMOUSE_MACALLY2:
  442. data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
  443. data[2] |= 0x80; /* Right button is mapped as button 3 */
  444. nb=4;
  445. break;
  446. }
  447. input_report_key(adbhid[id]->input, BTN_LEFT, !((data[1] >> 7) & 1));
  448. input_report_key(adbhid[id]->input, BTN_MIDDLE, !((data[2] >> 7) & 1));
  449. if (nb >= 4 && adbhid[id]->mouse_kind != ADBMOUSE_TRACKPAD)
  450. input_report_key(adbhid[id]->input, BTN_RIGHT, !((data[3] >> 7) & 1));
  451. input_report_rel(adbhid[id]->input, REL_X,
  452. ((data[2]&0x7f) < 64 ? (data[2]&0x7f) : (data[2]&0x7f)-128 ));
  453. input_report_rel(adbhid[id]->input, REL_Y,
  454. ((data[1]&0x7f) < 64 ? (data[1]&0x7f) : (data[1]&0x7f)-128 ));
  455. input_sync(adbhid[id]->input);
  456. }
  457. static void
  458. adbhid_buttons_input(unsigned char *data, int nb, int autopoll)
  459. {
  460. int id = (data[0] >> 4) & 0x0f;
  461. if (!adbhid[id]) {
  462. printk(KERN_ERR "ADB HID on ID %d not yet registered\n", id);
  463. return;
  464. }
  465. switch (adbhid[id]->original_handler_id) {
  466. default:
  467. case 0x02: /* Adjustable keyboard button device */
  468. {
  469. int down = (data[1] == (data[1] & 0xf));
  470. switch (data[1] & 0x0f) {
  471. case 0x0: /* microphone */
  472. input_report_key(adbhid[id]->input, KEY_SOUND, down);
  473. break;
  474. case 0x1: /* mute */
  475. input_report_key(adbhid[id]->input, KEY_MUTE, down);
  476. break;
  477. case 0x2: /* volume decrease */
  478. input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
  479. break;
  480. case 0x3: /* volume increase */
  481. input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
  482. break;
  483. default:
  484. printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
  485. data[0], data[1], data[2], data[3]);
  486. break;
  487. }
  488. }
  489. break;
  490. case 0x1f: /* Powerbook button device */
  491. {
  492. int down = (data[1] == (data[1] & 0xf));
  493. /*
  494. * XXX: Where is the contrast control for the passive?
  495. * -- Cort
  496. */
  497. switch (data[1] & 0x0f) {
  498. case 0x8: /* mute */
  499. input_report_key(adbhid[id]->input, KEY_MUTE, down);
  500. break;
  501. case 0x7: /* volume decrease */
  502. input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
  503. break;
  504. case 0x6: /* volume increase */
  505. input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
  506. break;
  507. case 0xb: /* eject */
  508. input_report_key(adbhid[id]->input, KEY_EJECTCD, down);
  509. break;
  510. case 0xa: /* brightness decrease */
  511. #ifdef CONFIG_PMAC_BACKLIGHT
  512. if (down)
  513. pmac_backlight_key_down();
  514. #endif
  515. input_report_key(adbhid[id]->input, KEY_BRIGHTNESSDOWN, down);
  516. break;
  517. case 0x9: /* brightness increase */
  518. #ifdef CONFIG_PMAC_BACKLIGHT
  519. if (down)
  520. pmac_backlight_key_up();
  521. #endif
  522. input_report_key(adbhid[id]->input, KEY_BRIGHTNESSUP, down);
  523. break;
  524. case 0xc: /* videomode switch */
  525. input_report_key(adbhid[id]->input, KEY_SWITCHVIDEOMODE, down);
  526. break;
  527. case 0xd: /* keyboard illumination toggle */
  528. input_report_key(adbhid[id]->input, KEY_KBDILLUMTOGGLE, down);
  529. break;
  530. case 0xe: /* keyboard illumination decrease */
  531. input_report_key(adbhid[id]->input, KEY_KBDILLUMDOWN, down);
  532. break;
  533. case 0xf:
  534. switch (data[1]) {
  535. case 0x8f:
  536. case 0x0f:
  537. /* keyboard illumination increase */
  538. input_report_key(adbhid[id]->input, KEY_KBDILLUMUP, down);
  539. break;
  540. case 0x7f:
  541. case 0xff:
  542. /* keypad overlay toogle */
  543. break;
  544. default:
  545. printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
  546. data[0], data[1], data[2], data[3]);
  547. break;
  548. }
  549. break;
  550. default:
  551. printk(KERN_INFO "Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
  552. data[0], data[1], data[2], data[3]);
  553. break;
  554. }
  555. }
  556. break;
  557. }
  558. input_sync(adbhid[id]->input);
  559. }
  560. static struct adb_request led_request;
  561. static int leds_pending[16];
  562. static int leds_req_pending;
  563. static int pending_devs[16];
  564. static int pending_led_start;
  565. static int pending_led_end;
  566. static DEFINE_SPINLOCK(leds_lock);
  567. static void leds_done(struct adb_request *req)
  568. {
  569. int leds = 0, device = 0, pending = 0;
  570. unsigned long flags;
  571. spin_lock_irqsave(&leds_lock, flags);
  572. if (pending_led_start != pending_led_end) {
  573. device = pending_devs[pending_led_start];
  574. leds = leds_pending[device] & 0xff;
  575. leds_pending[device] = 0;
  576. pending_led_start++;
  577. pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
  578. pending = leds_req_pending;
  579. } else
  580. leds_req_pending = 0;
  581. spin_unlock_irqrestore(&leds_lock, flags);
  582. if (pending)
  583. adb_request(&led_request, leds_done, 0, 3,
  584. ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
  585. }
  586. static void real_leds(unsigned char leds, int device)
  587. {
  588. unsigned long flags;
  589. spin_lock_irqsave(&leds_lock, flags);
  590. if (!leds_req_pending) {
  591. leds_req_pending = 1;
  592. spin_unlock_irqrestore(&leds_lock, flags);
  593. adb_request(&led_request, leds_done, 0, 3,
  594. ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
  595. return;
  596. } else {
  597. if (!(leds_pending[device] & 0x100)) {
  598. pending_devs[pending_led_end] = device;
  599. pending_led_end++;
  600. pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
  601. }
  602. leds_pending[device] = leds | 0x100;
  603. }
  604. spin_unlock_irqrestore(&leds_lock, flags);
  605. }
  606. /*
  607. * Event callback from the input module. Events that change the state of
  608. * the hardware are processed here.
  609. */
  610. static int adbhid_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  611. {
  612. struct adbhid *adbhid = input_get_drvdata(dev);
  613. unsigned char leds;
  614. switch (type) {
  615. case EV_LED:
  616. leds = (test_bit(LED_SCROLLL, dev->led) ? 4 : 0) |
  617. (test_bit(LED_NUML, dev->led) ? 1 : 0) |
  618. (test_bit(LED_CAPSL, dev->led) ? 2 : 0);
  619. real_leds(leds, adbhid->id);
  620. return 0;
  621. }
  622. return -1;
  623. }
  624. static void
  625. adbhid_kbd_capslock_remember(void)
  626. {
  627. struct adbhid *ahid;
  628. int i;
  629. for (i = 1; i < 16; i++) {
  630. ahid = adbhid[i];
  631. if (ahid && ahid->id == ADB_KEYBOARD)
  632. if (ahid->flags & FLAG_CAPSLOCK_TRANSLATE)
  633. ahid->flags |= FLAG_CAPSLOCK_IGNORE_NEXT;
  634. }
  635. }
  636. static int
  637. adb_message_handler(struct notifier_block *this, unsigned long code, void *x)
  638. {
  639. switch (code) {
  640. case ADB_MSG_PRE_RESET:
  641. case ADB_MSG_POWERDOWN:
  642. /* Stop the repeat timer. Autopoll is already off at this point */
  643. {
  644. int i;
  645. for (i = 1; i < 16; i++) {
  646. if (adbhid[i])
  647. del_timer_sync(&adbhid[i]->input->timer);
  648. }
  649. }
  650. /* Stop pending led requests */
  651. while (leds_req_pending)
  652. adb_poll();
  653. /* After resume, and if the capslock LED is on, the PMU will
  654. * send a "capslock down" key event. This confuses the
  655. * restore_capslock_events logic. Remember if the capslock
  656. * LED was on before suspend so the unwanted key event can
  657. * be ignored after resume. */
  658. if (restore_capslock_events)
  659. adbhid_kbd_capslock_remember();
  660. break;
  661. case ADB_MSG_POST_RESET:
  662. adbhid_probe();
  663. break;
  664. }
  665. return NOTIFY_DONE;
  666. }
  667. static int
  668. adbhid_input_register(int id, int default_id, int original_handler_id,
  669. int current_handler_id, int mouse_kind)
  670. {
  671. struct adbhid *hid;
  672. struct input_dev *input_dev;
  673. int err;
  674. int i;
  675. if (adbhid[id]) {
  676. printk(KERN_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 = kmalloc(sizeof(adb_to_linux_keycodes), GFP_KERNEL);
  702. if (!hid->keycode) {
  703. err = -ENOMEM;
  704. goto fail;
  705. }
  706. sprintf(hid->name, "ADB keyboard");
  707. memcpy(hid->keycode, adb_to_linux_keycodes, sizeof(adb_to_linux_keycodes));
  708. printk(KERN_INFO "Detected ADB keyboard, type ");
  709. switch (original_handler_id) {
  710. default:
  711. printk("<unknown>.\n");
  712. input_dev->id.version = ADB_KEYBOARD_UNKNOWN;
  713. break;
  714. case 0x01: case 0x02: case 0x03: case 0x06: case 0x08:
  715. case 0x0C: case 0x10: case 0x18: case 0x1B: case 0x1C:
  716. case 0xC0: case 0xC3: case 0xC6:
  717. printk("ANSI.\n");
  718. input_dev->id.version = ADB_KEYBOARD_ANSI;
  719. break;
  720. case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
  721. case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
  722. case 0xC4: case 0xC7:
  723. printk("ISO, swapping keys.\n");
  724. input_dev->id.version = ADB_KEYBOARD_ISO;
  725. i = hid->keycode[10];
  726. hid->keycode[10] = hid->keycode[50];
  727. hid->keycode[50] = i;
  728. break;
  729. case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
  730. case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
  731. printk("JIS.\n");
  732. input_dev->id.version = ADB_KEYBOARD_JIS;
  733. break;
  734. }
  735. for (i = 0; i < 128; i++)
  736. if (hid->keycode[i])
  737. set_bit(hid->keycode[i], input_dev->keybit);
  738. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
  739. BIT_MASK(EV_REP);
  740. input_dev->ledbit[0] = BIT_MASK(LED_SCROLLL) |
  741. BIT_MASK(LED_CAPSL) | BIT_MASK(LED_NUML);
  742. input_dev->event = adbhid_kbd_event;
  743. input_dev->keycodemax = KEY_FN;
  744. input_dev->keycodesize = sizeof(hid->keycode[0]);
  745. break;
  746. case ADB_MOUSE:
  747. sprintf(hid->name, "ADB mouse");
  748. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  749. input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  750. BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  751. input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  752. break;
  753. case ADB_MISC:
  754. switch (original_handler_id) {
  755. case 0x02: /* Adjustable keyboard button device */
  756. sprintf(hid->name, "ADB adjustable keyboard buttons");
  757. input_dev->evbit[0] = BIT_MASK(EV_KEY) |
  758. BIT_MASK(EV_REP);
  759. set_bit(KEY_SOUND, input_dev->keybit);
  760. set_bit(KEY_MUTE, input_dev->keybit);
  761. set_bit(KEY_VOLUMEUP, input_dev->keybit);
  762. set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
  763. break;
  764. case 0x1f: /* Powerbook button device */
  765. sprintf(hid->name, "ADB Powerbook buttons");
  766. input_dev->evbit[0] = BIT_MASK(EV_KEY) |
  767. BIT_MASK(EV_REP);
  768. set_bit(KEY_MUTE, input_dev->keybit);
  769. set_bit(KEY_VOLUMEUP, input_dev->keybit);
  770. set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
  771. set_bit(KEY_BRIGHTNESSUP, input_dev->keybit);
  772. set_bit(KEY_BRIGHTNESSDOWN, input_dev->keybit);
  773. set_bit(KEY_EJECTCD, input_dev->keybit);
  774. set_bit(KEY_SWITCHVIDEOMODE, input_dev->keybit);
  775. set_bit(KEY_KBDILLUMTOGGLE, input_dev->keybit);
  776. set_bit(KEY_KBDILLUMDOWN, input_dev->keybit);
  777. set_bit(KEY_KBDILLUMUP, input_dev->keybit);
  778. break;
  779. }
  780. if (hid->name[0])
  781. break;
  782. /* else fall through */
  783. default:
  784. printk(KERN_INFO "Trying to register unknown ADB device to input layer.\n");
  785. err = -ENODEV;
  786. goto fail;
  787. }
  788. input_dev->keycode = hid->keycode;
  789. err = input_register_device(input_dev);
  790. if (err)
  791. goto fail;
  792. if (default_id == ADB_KEYBOARD) {
  793. /* HACK WARNING!! This should go away as soon there is an utility
  794. * to control that for event devices.
  795. */
  796. input_dev->rep[REP_DELAY] = 500; /* input layer default: 250 */
  797. input_dev->rep[REP_PERIOD] = 66; /* input layer default: 33 */
  798. }
  799. return 0;
  800. fail: input_free_device(input_dev);
  801. if (hid) {
  802. kfree(hid->keycode);
  803. kfree(hid);
  804. }
  805. adbhid[id] = NULL;
  806. return err;
  807. }
  808. static void adbhid_input_unregister(int id)
  809. {
  810. input_unregister_device(adbhid[id]->input);
  811. kfree(adbhid[id]->keycode);
  812. kfree(adbhid[id]);
  813. adbhid[id] = NULL;
  814. }
  815. static u16
  816. adbhid_input_reregister(int id, int default_id, int org_handler_id,
  817. int cur_handler_id, int mk)
  818. {
  819. if (adbhid[id]) {
  820. if (adbhid[id]->input->id.product !=
  821. ((id << 12)|(default_id << 8)|org_handler_id)) {
  822. adbhid_input_unregister(id);
  823. adbhid_input_register(id, default_id, org_handler_id,
  824. cur_handler_id, mk);
  825. }
  826. } else
  827. adbhid_input_register(id, default_id, org_handler_id,
  828. cur_handler_id, mk);
  829. return 1<<id;
  830. }
  831. static void
  832. adbhid_input_devcleanup(u16 exist)
  833. {
  834. int i;
  835. for(i=1; i<16; i++)
  836. if (adbhid[i] && !(exist&(1<<i)))
  837. adbhid_input_unregister(i);
  838. }
  839. static void
  840. adbhid_probe(void)
  841. {
  842. struct adb_request req;
  843. int i, default_id, org_handler_id, cur_handler_id;
  844. u16 reg = 0;
  845. adb_register(ADB_MOUSE, 0, &mouse_ids, adbhid_mouse_input);
  846. adb_register(ADB_KEYBOARD, 0, &keyboard_ids, adbhid_keyboard_input);
  847. adb_register(ADB_MISC, 0, &buttons_ids, adbhid_buttons_input);
  848. for (i = 0; i < keyboard_ids.nids; i++) {
  849. int id = keyboard_ids.id[i];
  850. adb_get_infos(id, &default_id, &org_handler_id);
  851. /* turn off all leds */
  852. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  853. ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
  854. /* Enable full feature set of the keyboard
  855. ->get it to send separate codes for left and right shift,
  856. control, option keys */
  857. #if 0 /* handler 5 doesn't send separate codes for R modifiers */
  858. if (adb_try_handler_change(id, 5))
  859. printk("ADB keyboard at %d, handler set to 5\n", id);
  860. else
  861. #endif
  862. if (adb_try_handler_change(id, 3))
  863. printk("ADB keyboard at %d, handler set to 3\n", id);
  864. else
  865. printk("ADB keyboard at %d, handler 1\n", id);
  866. adb_get_infos(id, &default_id, &cur_handler_id);
  867. reg |= adbhid_input_reregister(id, default_id, org_handler_id,
  868. cur_handler_id, 0);
  869. }
  870. for (i = 0; i < buttons_ids.nids; i++) {
  871. int id = buttons_ids.id[i];
  872. adb_get_infos(id, &default_id, &org_handler_id);
  873. reg |= adbhid_input_reregister(id, default_id, org_handler_id,
  874. org_handler_id, 0);
  875. }
  876. /* Try to switch all mice to handler 4, or 2 for three-button
  877. mode and full resolution. */
  878. for (i = 0; i < mouse_ids.nids; i++) {
  879. int id = mouse_ids.id[i];
  880. int mouse_kind;
  881. adb_get_infos(id, &default_id, &org_handler_id);
  882. if (adb_try_handler_change(id, 4)) {
  883. printk("ADB mouse at %d, handler set to 4", id);
  884. mouse_kind = ADBMOUSE_EXTENDED;
  885. }
  886. else if (adb_try_handler_change(id, 0x2F)) {
  887. printk("ADB mouse at %d, handler set to 0x2F", id);
  888. mouse_kind = ADBMOUSE_MICROSPEED;
  889. }
  890. else if (adb_try_handler_change(id, 0x42)) {
  891. printk("ADB mouse at %d, handler set to 0x42", id);
  892. mouse_kind = ADBMOUSE_TRACKBALLPRO;
  893. }
  894. else if (adb_try_handler_change(id, 0x66)) {
  895. printk("ADB mouse at %d, handler set to 0x66", id);
  896. mouse_kind = ADBMOUSE_MICROSPEED;
  897. }
  898. else if (adb_try_handler_change(id, 0x5F)) {
  899. printk("ADB mouse at %d, handler set to 0x5F", id);
  900. mouse_kind = ADBMOUSE_MICROSPEED;
  901. }
  902. else if (adb_try_handler_change(id, 3)) {
  903. printk("ADB mouse at %d, handler set to 3", id);
  904. mouse_kind = ADBMOUSE_MS_A3;
  905. }
  906. else if (adb_try_handler_change(id, 2)) {
  907. printk("ADB mouse at %d, handler set to 2", id);
  908. mouse_kind = ADBMOUSE_STANDARD_200;
  909. }
  910. else {
  911. printk("ADB mouse at %d, handler 1", id);
  912. mouse_kind = ADBMOUSE_STANDARD_100;
  913. }
  914. if ((mouse_kind == ADBMOUSE_TRACKBALLPRO)
  915. || (mouse_kind == ADBMOUSE_MICROSPEED)) {
  916. init_microspeed(id);
  917. } else if (mouse_kind == ADBMOUSE_MS_A3) {
  918. init_ms_a3(id);
  919. } else if (mouse_kind == ADBMOUSE_EXTENDED) {
  920. /*
  921. * Register 1 is usually used for device
  922. * identification. Here, we try to identify
  923. * a known device and call the appropriate
  924. * init function.
  925. */
  926. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  927. ADB_READREG(id, 1));
  928. if ((req.reply_len) &&
  929. (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
  930. || (req.reply[2] == 0x20))) {
  931. mouse_kind = ADBMOUSE_TRACKBALL;
  932. init_trackball(id);
  933. }
  934. else if ((req.reply_len >= 4) &&
  935. (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
  936. (req.reply[3] == 0x61) && (req.reply[4] == 0x64)) {
  937. mouse_kind = ADBMOUSE_TRACKPAD;
  938. init_trackpad(id);
  939. }
  940. else if ((req.reply_len >= 4) &&
  941. (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
  942. (req.reply[3] == 0x4c) && (req.reply[4] == 0x31)) {
  943. mouse_kind = ADBMOUSE_TURBOMOUSE5;
  944. init_turbomouse(id);
  945. }
  946. else if ((req.reply_len == 9) &&
  947. (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
  948. (req.reply[3] == 0x49) && (req.reply[4] == 0x54)) {
  949. if (adb_try_handler_change(id, 0x42)) {
  950. printk("\nADB MacAlly 2-button mouse at %d, handler set to 0x42", id);
  951. mouse_kind = ADBMOUSE_MACALLY2;
  952. }
  953. }
  954. }
  955. printk("\n");
  956. adb_get_infos(id, &default_id, &cur_handler_id);
  957. reg |= adbhid_input_reregister(id, default_id, org_handler_id,
  958. cur_handler_id, mouse_kind);
  959. }
  960. adbhid_input_devcleanup(reg);
  961. }
  962. static void
  963. init_trackpad(int id)
  964. {
  965. struct adb_request req;
  966. unsigned char r1_buffer[8];
  967. printk(" (trackpad)");
  968. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  969. ADB_READREG(id,1));
  970. if (req.reply_len < 8)
  971. printk("bad length for reg. 1\n");
  972. else
  973. {
  974. memcpy(r1_buffer, &req.reply[1], 8);
  975. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  976. ADB_WRITEREG(id,1),
  977. r1_buffer[0],
  978. r1_buffer[1],
  979. r1_buffer[2],
  980. r1_buffer[3],
  981. r1_buffer[4],
  982. r1_buffer[5],
  983. 0x0d,
  984. r1_buffer[7]);
  985. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  986. ADB_WRITEREG(id,2),
  987. 0x99,
  988. 0x94,
  989. 0x19,
  990. 0xff,
  991. 0xb2,
  992. 0x8a,
  993. 0x1b,
  994. 0x50);
  995. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  996. ADB_WRITEREG(id,1),
  997. r1_buffer[0],
  998. r1_buffer[1],
  999. r1_buffer[2],
  1000. r1_buffer[3],
  1001. r1_buffer[4],
  1002. r1_buffer[5],
  1003. 0x03, /*r1_buffer[6],*/
  1004. r1_buffer[7]);
  1005. /* Without this flush, the trackpad may be locked up */
  1006. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1007. }
  1008. }
  1009. static void
  1010. init_trackball(int id)
  1011. {
  1012. struct adb_request req;
  1013. printk(" (trackman/mouseman)");
  1014. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1015. ADB_WRITEREG(id,1), 00,0x81);
  1016. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1017. ADB_WRITEREG(id,1), 01,0x81);
  1018. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1019. ADB_WRITEREG(id,1), 02,0x81);
  1020. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1021. ADB_WRITEREG(id,1), 03,0x38);
  1022. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1023. ADB_WRITEREG(id,1), 00,0x81);
  1024. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1025. ADB_WRITEREG(id,1), 01,0x81);
  1026. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1027. ADB_WRITEREG(id,1), 02,0x81);
  1028. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1029. ADB_WRITEREG(id,1), 03,0x38);
  1030. }
  1031. static void
  1032. init_turbomouse(int id)
  1033. {
  1034. struct adb_request req;
  1035. printk(" (TurboMouse 5)");
  1036. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1037. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
  1038. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  1039. ADB_WRITEREG(3,2),
  1040. 0xe7,
  1041. 0x8c,
  1042. 0,
  1043. 0,
  1044. 0,
  1045. 0xff,
  1046. 0xff,
  1047. 0x94);
  1048. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
  1049. adb_request(&req, NULL, ADBREQ_SYNC, 9,
  1050. ADB_WRITEREG(3,2),
  1051. 0xa5,
  1052. 0x14,
  1053. 0,
  1054. 0,
  1055. 0x69,
  1056. 0xff,
  1057. 0xff,
  1058. 0x27);
  1059. }
  1060. static void
  1061. init_microspeed(int id)
  1062. {
  1063. struct adb_request req;
  1064. printk(" (Microspeed/MacPoint or compatible)");
  1065. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1066. /* This will initialize mice using the Microspeed, MacPoint and
  1067. other compatible firmware. Bit 12 enables extended protocol.
  1068. Register 1 Listen (4 Bytes)
  1069. 0 - 3 Button is mouse (set also for double clicking!!!)
  1070. 4 - 7 Button is locking (affects change speed also)
  1071. 8 - 11 Button changes speed
  1072. 12 1 = Extended mouse mode, 0 = normal mouse mode
  1073. 13 - 15 unused 0
  1074. 16 - 23 normal speed
  1075. 24 - 31 changed speed
  1076. Register 1 talk holds version and product identification information.
  1077. Register 1 Talk (4 Bytes):
  1078. 0 - 7 Product code
  1079. 8 - 23 undefined, reserved
  1080. 24 - 31 Version number
  1081. Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
  1082. */
  1083. adb_request(&req, NULL, ADBREQ_SYNC, 5,
  1084. ADB_WRITEREG(id,1),
  1085. 0x20, /* alt speed = 0x20 (rather slow) */
  1086. 0x00, /* norm speed = 0x00 (fastest) */
  1087. 0x10, /* extended protocol, no speed change */
  1088. 0x07); /* all buttons enabled as mouse buttons, no locking */
  1089. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1090. }
  1091. static void
  1092. init_ms_a3(int id)
  1093. {
  1094. struct adb_request req;
  1095. printk(" (Mouse Systems A3 Mouse, or compatible)");
  1096. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  1097. ADB_WRITEREG(id, 0x2),
  1098. 0x00,
  1099. 0x07);
  1100. adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
  1101. }
  1102. static int __init adbhid_init(void)
  1103. {
  1104. #ifndef CONFIG_MAC
  1105. if (!machine_is(chrp) && !machine_is(powermac))
  1106. return 0;
  1107. #endif
  1108. led_request.complete = 1;
  1109. adbhid_probe();
  1110. blocking_notifier_chain_register(&adb_client_list,
  1111. &adbhid_adb_notifier);
  1112. return 0;
  1113. }
  1114. static void __exit adbhid_exit(void)
  1115. {
  1116. }
  1117. module_init(adbhid_init);
  1118. module_exit(adbhid_exit);