sermouse.c 8.8 KB

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