sermouse.c 8.9 KB

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