logips2pp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Logitech PS/2++ mouse driver
  3. *
  4. * Copyright (c) 1999-2003 Vojtech Pavlik <vojtech@suse.cz>
  5. * Copyright (c) 2003 Eric Wong <eric@yhbt.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/input.h>
  12. #include <linux/serio.h>
  13. #include <linux/libps2.h>
  14. #include "psmouse.h"
  15. #include "logips2pp.h"
  16. /* Logitech mouse types */
  17. #define PS2PP_KIND_WHEEL 1
  18. #define PS2PP_KIND_MX 2
  19. #define PS2PP_KIND_TP3 3
  20. #define PS2PP_KIND_TRACKMAN 4
  21. /* Logitech mouse features */
  22. #define PS2PP_WHEEL 0x01
  23. #define PS2PP_HWHEEL 0x02
  24. #define PS2PP_SIDE_BTN 0x04
  25. #define PS2PP_EXTRA_BTN 0x08
  26. #define PS2PP_TASK_BTN 0x10
  27. #define PS2PP_NAV_BTN 0x20
  28. struct ps2pp_info {
  29. u8 model;
  30. u8 kind;
  31. u16 features;
  32. };
  33. /*
  34. * Process a PS2++ or PS2T++ packet.
  35. */
  36. static psmouse_ret_t ps2pp_process_byte(struct psmouse *psmouse)
  37. {
  38. struct input_dev *dev = psmouse->dev;
  39. unsigned char *packet = psmouse->packet;
  40. if (psmouse->pktcnt < 3)
  41. return PSMOUSE_GOOD_DATA;
  42. /*
  43. * Full packet accumulated, process it
  44. */
  45. if ((packet[0] & 0x48) == 0x48 && (packet[1] & 0x02) == 0x02) {
  46. /* Logitech extended packet */
  47. switch ((packet[1] >> 4) | (packet[0] & 0x30)) {
  48. case 0x0d: /* Mouse extra info */
  49. input_report_rel(dev, packet[2] & 0x80 ? REL_HWHEEL : REL_WHEEL,
  50. (int) (packet[2] & 8) - (int) (packet[2] & 7));
  51. input_report_key(dev, BTN_SIDE, (packet[2] >> 4) & 1);
  52. input_report_key(dev, BTN_EXTRA, (packet[2] >> 5) & 1);
  53. break;
  54. case 0x0e: /* buttons 4, 5, 6, 7, 8, 9, 10 info */
  55. input_report_key(dev, BTN_SIDE, (packet[2]) & 1);
  56. input_report_key(dev, BTN_EXTRA, (packet[2] >> 1) & 1);
  57. input_report_key(dev, BTN_BACK, (packet[2] >> 3) & 1);
  58. input_report_key(dev, BTN_FORWARD, (packet[2] >> 4) & 1);
  59. input_report_key(dev, BTN_TASK, (packet[2] >> 2) & 1);
  60. break;
  61. case 0x0f: /* TouchPad extra info */
  62. input_report_rel(dev, packet[2] & 0x08 ? REL_HWHEEL : REL_WHEEL,
  63. (int) ((packet[2] >> 4) & 8) - (int) ((packet[2] >> 4) & 7));
  64. packet[0] = packet[2] | 0x08;
  65. break;
  66. default:
  67. psmouse_dbg(psmouse,
  68. "Received PS2++ packet #%x, but don't know how to handle.\n",
  69. (packet[1] >> 4) | (packet[0] & 0x30));
  70. break;
  71. }
  72. } else {
  73. /* Standard PS/2 motion data */
  74. input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
  75. input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
  76. }
  77. input_report_key(dev, BTN_LEFT, packet[0] & 1);
  78. input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
  79. input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
  80. input_sync(dev);
  81. return PSMOUSE_FULL_PACKET;
  82. }
  83. /*
  84. * ps2pp_cmd() sends a PS2++ command, sliced into two bit
  85. * pieces through the SETRES command. This is needed to send extended
  86. * commands to mice on notebooks that try to understand the PS/2 protocol
  87. * Ugly.
  88. */
  89. static int ps2pp_cmd(struct psmouse *psmouse, unsigned char *param, unsigned char command)
  90. {
  91. if (psmouse_sliced_command(psmouse, command))
  92. return -1;
  93. if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_POLL | 0x0300))
  94. return -1;
  95. return 0;
  96. }
  97. /*
  98. * SmartScroll / CruiseControl for some newer Logitech mice Defaults to
  99. * enabled if we do nothing to it. Of course I put this in because I want it
  100. * disabled :P
  101. * 1 - enabled (if previously disabled, also default)
  102. * 0 - disabled
  103. */
  104. static void ps2pp_set_smartscroll(struct psmouse *psmouse, bool smartscroll)
  105. {
  106. struct ps2dev *ps2dev = &psmouse->ps2dev;
  107. unsigned char param[4];
  108. ps2pp_cmd(psmouse, param, 0x32);
  109. param[0] = 0;
  110. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  111. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  112. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  113. param[0] = smartscroll;
  114. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  115. }
  116. static ssize_t ps2pp_attr_show_smartscroll(struct psmouse *psmouse,
  117. void *data, char *buf)
  118. {
  119. return sprintf(buf, "%d\n", psmouse->smartscroll);
  120. }
  121. static ssize_t ps2pp_attr_set_smartscroll(struct psmouse *psmouse, void *data,
  122. const char *buf, size_t count)
  123. {
  124. unsigned int value;
  125. int err;
  126. err = kstrtouint(buf, 10, &value);
  127. if (err)
  128. return err;
  129. if (value > 1)
  130. return -EINVAL;
  131. ps2pp_set_smartscroll(psmouse, value);
  132. psmouse->smartscroll = value;
  133. return count;
  134. }
  135. PSMOUSE_DEFINE_ATTR(smartscroll, S_IWUSR | S_IRUGO, NULL,
  136. ps2pp_attr_show_smartscroll, ps2pp_attr_set_smartscroll);
  137. /*
  138. * Support 800 dpi resolution _only_ if the user wants it (there are good
  139. * reasons to not use it even if the mouse supports it, and of course there are
  140. * also good reasons to use it, let the user decide).
  141. */
  142. static void ps2pp_set_resolution(struct psmouse *psmouse, unsigned int resolution)
  143. {
  144. if (resolution > 400) {
  145. struct ps2dev *ps2dev = &psmouse->ps2dev;
  146. unsigned char param = 3;
  147. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  148. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  149. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  150. ps2_command(ps2dev, &param, PSMOUSE_CMD_SETRES);
  151. psmouse->resolution = 800;
  152. } else
  153. psmouse_set_resolution(psmouse, resolution);
  154. }
  155. static void ps2pp_disconnect(struct psmouse *psmouse)
  156. {
  157. device_remove_file(&psmouse->ps2dev.serio->dev, &psmouse_attr_smartscroll.dattr);
  158. }
  159. static const struct ps2pp_info *get_model_info(unsigned char model)
  160. {
  161. static const struct ps2pp_info ps2pp_list[] = {
  162. { 1, 0, 0 }, /* Simple 2-button mouse */
  163. { 12, 0, PS2PP_SIDE_BTN},
  164. { 13, 0, 0 },
  165. { 15, PS2PP_KIND_MX, /* MX1000 */
  166. PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
  167. PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
  168. { 40, 0, PS2PP_SIDE_BTN },
  169. { 41, 0, PS2PP_SIDE_BTN },
  170. { 42, 0, PS2PP_SIDE_BTN },
  171. { 43, 0, PS2PP_SIDE_BTN },
  172. { 50, 0, 0 },
  173. { 51, 0, 0 },
  174. { 52, PS2PP_KIND_WHEEL, PS2PP_SIDE_BTN | PS2PP_WHEEL },
  175. { 53, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  176. { 56, PS2PP_KIND_WHEEL, PS2PP_SIDE_BTN | PS2PP_WHEEL }, /* Cordless MouseMan Wheel */
  177. { 61, PS2PP_KIND_MX, /* MX700 */
  178. PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
  179. PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
  180. { 66, PS2PP_KIND_MX, /* MX3100 receiver */
  181. PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
  182. PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
  183. { 72, PS2PP_KIND_TRACKMAN, 0 }, /* T-CH11: TrackMan Marble */
  184. { 73, PS2PP_KIND_TRACKMAN, PS2PP_SIDE_BTN }, /* TrackMan FX */
  185. { 75, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  186. { 76, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  187. { 79, PS2PP_KIND_TRACKMAN, PS2PP_WHEEL }, /* TrackMan with wheel */
  188. { 80, PS2PP_KIND_WHEEL, PS2PP_SIDE_BTN | PS2PP_WHEEL },
  189. { 81, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  190. { 83, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  191. { 85, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  192. { 86, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  193. { 87, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  194. { 88, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  195. { 96, 0, 0 },
  196. { 97, PS2PP_KIND_TP3, PS2PP_WHEEL | PS2PP_HWHEEL },
  197. { 99, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
  198. { 100, PS2PP_KIND_MX, /* MX510 */
  199. PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
  200. PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
  201. { 111, PS2PP_KIND_MX, PS2PP_WHEEL | PS2PP_SIDE_BTN }, /* MX300 reports task button as side */
  202. { 112, PS2PP_KIND_MX, /* MX500 */
  203. PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
  204. PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
  205. { 114, PS2PP_KIND_MX, /* MX310 */
  206. PS2PP_WHEEL | PS2PP_SIDE_BTN |
  207. PS2PP_TASK_BTN | PS2PP_EXTRA_BTN }
  208. };
  209. int i;
  210. for (i = 0; i < ARRAY_SIZE(ps2pp_list); i++)
  211. if (model == ps2pp_list[i].model)
  212. return &ps2pp_list[i];
  213. return NULL;
  214. }
  215. /*
  216. * Set up input device's properties based on the detected mouse model.
  217. */
  218. static void ps2pp_set_model_properties(struct psmouse *psmouse,
  219. const struct ps2pp_info *model_info,
  220. bool using_ps2pp)
  221. {
  222. struct input_dev *input_dev = psmouse->dev;
  223. if (model_info->features & PS2PP_SIDE_BTN)
  224. __set_bit(BTN_SIDE, input_dev->keybit);
  225. if (model_info->features & PS2PP_EXTRA_BTN)
  226. __set_bit(BTN_EXTRA, input_dev->keybit);
  227. if (model_info->features & PS2PP_TASK_BTN)
  228. __set_bit(BTN_TASK, input_dev->keybit);
  229. if (model_info->features & PS2PP_NAV_BTN) {
  230. __set_bit(BTN_FORWARD, input_dev->keybit);
  231. __set_bit(BTN_BACK, input_dev->keybit);
  232. }
  233. if (model_info->features & PS2PP_WHEEL)
  234. __set_bit(REL_WHEEL, input_dev->relbit);
  235. if (model_info->features & PS2PP_HWHEEL)
  236. __set_bit(REL_HWHEEL, input_dev->relbit);
  237. switch (model_info->kind) {
  238. case PS2PP_KIND_WHEEL:
  239. psmouse->name = "Wheel Mouse";
  240. break;
  241. case PS2PP_KIND_MX:
  242. psmouse->name = "MX Mouse";
  243. break;
  244. case PS2PP_KIND_TP3:
  245. psmouse->name = "TouchPad 3";
  246. break;
  247. case PS2PP_KIND_TRACKMAN:
  248. psmouse->name = "TrackMan";
  249. break;
  250. default:
  251. /*
  252. * Set name to "Mouse" only when using PS2++,
  253. * otherwise let other protocols define suitable
  254. * name
  255. */
  256. if (using_ps2pp)
  257. psmouse->name = "Mouse";
  258. break;
  259. }
  260. }
  261. /*
  262. * Logitech magic init. Detect whether the mouse is a Logitech one
  263. * and its exact model and try turning on extended protocol for ones
  264. * that support it.
  265. */
  266. int ps2pp_init(struct psmouse *psmouse, bool set_properties)
  267. {
  268. struct ps2dev *ps2dev = &psmouse->ps2dev;
  269. unsigned char param[4];
  270. unsigned char model, buttons;
  271. const struct ps2pp_info *model_info;
  272. bool use_ps2pp = false;
  273. int error;
  274. param[0] = 0;
  275. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  276. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  277. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  278. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  279. param[1] = 0;
  280. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
  281. model = ((param[0] >> 4) & 0x07) | ((param[0] << 3) & 0x78);
  282. buttons = param[1];
  283. if (!model || !buttons)
  284. return -1;
  285. model_info = get_model_info(model);
  286. if (model_info) {
  287. /*
  288. * Do Logitech PS2++ / PS2T++ magic init.
  289. */
  290. if (model_info->kind == PS2PP_KIND_TP3) { /* Touch Pad 3 */
  291. /* Unprotect RAM */
  292. param[0] = 0x11; param[1] = 0x04; param[2] = 0x68;
  293. ps2_command(ps2dev, param, 0x30d1);
  294. /* Enable features */
  295. param[0] = 0x11; param[1] = 0x05; param[2] = 0x0b;
  296. ps2_command(ps2dev, param, 0x30d1);
  297. /* Enable PS2++ */
  298. param[0] = 0x11; param[1] = 0x09; param[2] = 0xc3;
  299. ps2_command(ps2dev, param, 0x30d1);
  300. param[0] = 0;
  301. if (!ps2_command(ps2dev, param, 0x13d1) &&
  302. param[0] == 0x06 && param[1] == 0x00 && param[2] == 0x14) {
  303. use_ps2pp = true;
  304. }
  305. } else {
  306. param[0] = param[1] = param[2] = 0;
  307. ps2pp_cmd(psmouse, param, 0x39); /* Magic knock */
  308. ps2pp_cmd(psmouse, param, 0xDB);
  309. if ((param[0] & 0x78) == 0x48 &&
  310. (param[1] & 0xf3) == 0xc2 &&
  311. (param[2] & 0x03) == ((param[1] >> 2) & 3)) {
  312. ps2pp_set_smartscroll(psmouse, false);
  313. use_ps2pp = true;
  314. }
  315. }
  316. } else {
  317. psmouse_warn(psmouse, "Detected unknown Logitech mouse model %d\n", model);
  318. }
  319. if (set_properties) {
  320. psmouse->vendor = "Logitech";
  321. psmouse->model = model;
  322. if (use_ps2pp) {
  323. psmouse->protocol_handler = ps2pp_process_byte;
  324. psmouse->pktsize = 3;
  325. if (model_info->kind != PS2PP_KIND_TP3) {
  326. psmouse->set_resolution = ps2pp_set_resolution;
  327. psmouse->disconnect = ps2pp_disconnect;
  328. error = device_create_file(&psmouse->ps2dev.serio->dev,
  329. &psmouse_attr_smartscroll.dattr);
  330. if (error) {
  331. psmouse_err(psmouse,
  332. "failed to create smartscroll sysfs attribute, error: %d\n",
  333. error);
  334. return -1;
  335. }
  336. }
  337. }
  338. if (buttons >= 3)
  339. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  340. if (model_info)
  341. ps2pp_set_model_properties(psmouse, model_info, use_ps2pp);
  342. }
  343. return use_ps2pp ? 0 : -1;
  344. }