sermouse.c 8.6 KB

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