logips2pp.c 11 KB

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