sermouse.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1999-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * Serial mouse driver for Linux
  7. */
  8. /*
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/input.h>
  15. #include <linux/serio.h>
  16. #define DRIVER_DESC "Serial mouse driver"
  17. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  18. MODULE_DESCRIPTION(DRIVER_DESC);
  19. MODULE_LICENSE("GPL");
  20. static const char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
  21. "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
  22. "Logitech MZ++ Mouse"};
  23. struct sermouse {
  24. struct input_dev *dev;
  25. signed char buf[8];
  26. unsigned char count;
  27. unsigned char type;
  28. unsigned long last;
  29. char phys[32];
  30. };
  31. /*
  32. * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
  33. * applies some prediction to the data, resulting in 96 updates per
  34. * second, which is as good as a PS/2 or USB mouse.
  35. */
  36. static void sermouse_process_msc(struct sermouse *sermouse, signed char data)
  37. {
  38. struct input_dev *dev = sermouse->dev;
  39. signed char *buf = sermouse->buf;
  40. switch (sermouse->count) {
  41. case 0:
  42. if ((data & 0xf8) != 0x80)
  43. return;
  44. input_report_key(dev, BTN_LEFT, !(data & 4));
  45. input_report_key(dev, BTN_RIGHT, !(data & 1));
  46. input_report_key(dev, BTN_MIDDLE, !(data & 2));
  47. break;
  48. case 1:
  49. case 3:
  50. input_report_rel(dev, REL_X, data / 2);
  51. input_report_rel(dev, REL_Y, -buf[1]);
  52. buf[0] = data - data / 2;
  53. break;
  54. case 2:
  55. case 4:
  56. input_report_rel(dev, REL_X, buf[0]);
  57. input_report_rel(dev, REL_Y, buf[1] - data);
  58. buf[1] = data / 2;
  59. break;
  60. }
  61. input_sync(dev);
  62. if (++sermouse->count == 5)
  63. sermouse->count = 0;
  64. }
  65. /*
  66. * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
  67. * generates events. With prediction it gets 80 updates/sec, assuming
  68. * standard 3-byte packets and 1200 bps.
  69. */
  70. static void sermouse_process_ms(struct sermouse *sermouse, signed char data)
  71. {
  72. struct input_dev *dev = sermouse->dev;
  73. signed char *buf = sermouse->buf;
  74. if (data & 0x40)
  75. sermouse->count = 0;
  76. else if (sermouse->count == 0)
  77. return;
  78. switch (sermouse->count) {
  79. case 0:
  80. buf[1] = data;
  81. input_report_key(dev, BTN_LEFT, (data >> 5) & 1);
  82. input_report_key(dev, BTN_RIGHT, (data >> 4) & 1);
  83. break;
  84. case 1:
  85. buf[2] = data;
  86. data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
  87. input_report_rel(dev, REL_X, data / 2);
  88. input_report_rel(dev, REL_Y, buf[4]);
  89. buf[3] = data - data / 2;
  90. break;
  91. case 2:
  92. /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
  93. if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
  94. input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
  95. buf[0] = buf[1];
  96. data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
  97. input_report_rel(dev, REL_X, buf[3]);
  98. input_report_rel(dev, REL_Y, data - buf[4]);
  99. buf[4] = data / 2;
  100. break;
  101. case 3:
  102. switch (sermouse->type) {
  103. case SERIO_MS:
  104. sermouse->type = SERIO_MP;
  105. fallthrough;
  106. case SERIO_MP:
  107. if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */
  108. input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
  109. input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
  110. break;
  111. case SERIO_MZP:
  112. case SERIO_MZPP:
  113. input_report_key(dev, BTN_SIDE, (data >> 5) & 1);
  114. fallthrough;
  115. case SERIO_MZ:
  116. input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
  117. input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7));
  118. break;
  119. }
  120. break;
  121. case 4:
  122. case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
  123. buf[1] = (data >> 2) & 0x0f;
  124. break;
  125. case 5:
  126. case 7: /* Ignore anything besides MZ++ */
  127. if (sermouse->type != SERIO_MZPP)
  128. break;
  129. switch (buf[1]) {
  130. case 1: /* Extra mouse info */
  131. input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
  132. input_report_key(dev, BTN_EXTRA, (data >> 5) & 1);
  133. input_report_rel(dev, data & 0x80 ? REL_HWHEEL : REL_WHEEL, (data & 7) - (data & 8));
  134. break;
  135. default: /* We don't decode anything else yet. */
  136. printk(KERN_WARNING
  137. "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf[1]);
  138. break;
  139. }
  140. break;
  141. }
  142. input_sync(dev);
  143. sermouse->count++;
  144. }
  145. /*
  146. * sermouse_interrupt() handles incoming characters, either gathering them into
  147. * packets or passing them to the command routine as command output.
  148. */
  149. static irqreturn_t sermouse_interrupt(struct serio *serio,
  150. unsigned char data, unsigned int flags)
  151. {
  152. struct sermouse *sermouse = serio_get_drvdata(serio);
  153. if (time_after(jiffies, sermouse->last + HZ/10))
  154. sermouse->count = 0;
  155. sermouse->last = jiffies;
  156. if (sermouse->type > SERIO_SUN)
  157. sermouse_process_ms(sermouse, data);
  158. else
  159. sermouse_process_msc(sermouse, data);
  160. return IRQ_HANDLED;
  161. }
  162. /*
  163. * sermouse_disconnect() cleans up after we don't want talk
  164. * to the mouse anymore.
  165. */
  166. static void sermouse_disconnect(struct serio *serio)
  167. {
  168. struct sermouse *sermouse = serio_get_drvdata(serio);
  169. serio_close(serio);
  170. serio_set_drvdata(serio, NULL);
  171. input_unregister_device(sermouse->dev);
  172. kfree(sermouse);
  173. }
  174. /*
  175. * sermouse_connect() is a callback form the serio module when
  176. * an unhandled serio port is found.
  177. */
  178. static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
  179. {
  180. struct sermouse *sermouse;
  181. struct input_dev *input_dev;
  182. unsigned char c = serio->id.extra;
  183. int err = -ENOMEM;
  184. sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
  185. input_dev = input_allocate_device();
  186. if (!sermouse || !input_dev)
  187. goto fail1;
  188. sermouse->dev = input_dev;
  189. snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
  190. sermouse->type = serio->id.proto;
  191. input_dev->name = sermouse_protocols[sermouse->type];
  192. input_dev->phys = sermouse->phys;
  193. input_dev->id.bustype = BUS_RS232;
  194. input_dev->id.vendor = sermouse->type;
  195. input_dev->id.product = c;
  196. input_dev->id.version = 0x0100;
  197. input_dev->dev.parent = &serio->dev;
  198. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  199. input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  200. BIT_MASK(BTN_RIGHT);
  201. input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  202. if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit);
  203. if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit);
  204. if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit);
  205. if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit);
  206. if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit);
  207. serio_set_drvdata(serio, sermouse);
  208. err = serio_open(serio, drv);
  209. if (err)
  210. goto fail2;
  211. err = input_register_device(sermouse->dev);
  212. if (err)
  213. goto fail3;
  214. return 0;
  215. fail3: serio_close(serio);
  216. fail2: serio_set_drvdata(serio, NULL);
  217. fail1: input_free_device(input_dev);
  218. kfree(sermouse);
  219. return err;
  220. }
  221. static struct serio_device_id sermouse_serio_ids[] = {
  222. {
  223. .type = SERIO_RS232,
  224. .proto = SERIO_MSC,
  225. .id = SERIO_ANY,
  226. .extra = SERIO_ANY,
  227. },
  228. {
  229. .type = SERIO_RS232,
  230. .proto = SERIO_SUN,
  231. .id = SERIO_ANY,
  232. .extra = SERIO_ANY,
  233. },
  234. {
  235. .type = SERIO_RS232,
  236. .proto = SERIO_MS,
  237. .id = SERIO_ANY,
  238. .extra = SERIO_ANY,
  239. },
  240. {
  241. .type = SERIO_RS232,
  242. .proto = SERIO_MP,
  243. .id = SERIO_ANY,
  244. .extra = SERIO_ANY,
  245. },
  246. {
  247. .type = SERIO_RS232,
  248. .proto = SERIO_MZ,
  249. .id = SERIO_ANY,
  250. .extra = SERIO_ANY,
  251. },
  252. {
  253. .type = SERIO_RS232,
  254. .proto = SERIO_MZP,
  255. .id = SERIO_ANY,
  256. .extra = SERIO_ANY,
  257. },
  258. {
  259. .type = SERIO_RS232,
  260. .proto = SERIO_MZPP,
  261. .id = SERIO_ANY,
  262. .extra = SERIO_ANY,
  263. },
  264. { 0 }
  265. };
  266. MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
  267. static struct serio_driver sermouse_drv = {
  268. .driver = {
  269. .name = "sermouse",
  270. },
  271. .description = DRIVER_DESC,
  272. .id_table = sermouse_serio_ids,
  273. .interrupt = sermouse_interrupt,
  274. .connect = sermouse_connect,
  275. .disconnect = sermouse_disconnect,
  276. };
  277. module_serio_driver(sermouse_drv);