sermouse.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 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, struct pt_regs *regs)
  56. {
  57. struct input_dev *dev = sermouse->dev;
  58. signed char *buf = sermouse->buf;
  59. input_regs(dev, regs);
  60. switch (sermouse->count) {
  61. case 0:
  62. if ((data & 0xf8) != 0x80) return;
  63. input_report_key(dev, BTN_LEFT, !(data & 4));
  64. input_report_key(dev, BTN_RIGHT, !(data & 1));
  65. input_report_key(dev, BTN_MIDDLE, !(data & 2));
  66. break;
  67. case 1:
  68. case 3:
  69. input_report_rel(dev, REL_X, data / 2);
  70. input_report_rel(dev, REL_Y, -buf[1]);
  71. buf[0] = data - data / 2;
  72. break;
  73. case 2:
  74. case 4:
  75. input_report_rel(dev, REL_X, buf[0]);
  76. input_report_rel(dev, REL_Y, buf[1] - data);
  77. buf[1] = data / 2;
  78. break;
  79. }
  80. input_sync(dev);
  81. if (++sermouse->count == 5)
  82. sermouse->count = 0;
  83. }
  84. /*
  85. * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
  86. * generates events. With prediction it gets 80 updates/sec, assuming
  87. * standard 3-byte packets and 1200 bps.
  88. */
  89. static void sermouse_process_ms(struct sermouse *sermouse, signed char data, struct pt_regs *regs)
  90. {
  91. struct input_dev *dev = sermouse->dev;
  92. signed char *buf = sermouse->buf;
  93. if (data & 0x40) sermouse->count = 0;
  94. input_regs(dev, regs);
  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) 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, struct pt_regs *regs)
  165. {
  166. struct sermouse *sermouse = serio_get_drvdata(serio);
  167. if (time_after(jiffies, sermouse->last + HZ/10)) sermouse->count = 0;
  168. sermouse->last = jiffies;
  169. if (sermouse->type > SERIO_SUN)
  170. sermouse_process_ms(sermouse, data, regs);
  171. else
  172. sermouse_process_msc(sermouse, data, regs);
  173. return IRQ_HANDLED;
  174. }
  175. /*
  176. * sermouse_disconnect() cleans up after we don't want talk
  177. * to the mouse anymore.
  178. */
  179. static void sermouse_disconnect(struct serio *serio)
  180. {
  181. struct sermouse *sermouse = serio_get_drvdata(serio);
  182. serio_close(serio);
  183. serio_set_drvdata(serio, NULL);
  184. input_unregister_device(sermouse->dev);
  185. kfree(sermouse);
  186. }
  187. /*
  188. * sermouse_connect() is a callback form the serio module when
  189. * an unhandled serio port is found.
  190. */
  191. static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
  192. {
  193. struct sermouse *sermouse;
  194. struct input_dev *input_dev;
  195. unsigned char c = serio->id.extra;
  196. int err = -ENOMEM;
  197. sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
  198. input_dev = input_allocate_device();
  199. if (!sermouse || !input_dev)
  200. goto fail;
  201. sermouse->dev = input_dev;
  202. snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
  203. sermouse->type = serio->id.proto;
  204. input_dev->name = sermouse_protocols[sermouse->type];
  205. input_dev->phys = sermouse->phys;
  206. input_dev->id.bustype = BUS_RS232;
  207. input_dev->id.vendor = sermouse->type;
  208. input_dev->id.product = c;
  209. input_dev->id.version = 0x0100;
  210. input_dev->cdev.dev = &serio->dev;
  211. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  212. input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
  213. input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  214. input_dev->private = sermouse;
  215. if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit);
  216. if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit);
  217. if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit);
  218. if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit);
  219. if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit);
  220. serio_set_drvdata(serio, sermouse);
  221. err = serio_open(serio, drv);
  222. if (err)
  223. goto fail;
  224. input_register_device(sermouse->dev);
  225. return 0;
  226. fail: serio_set_drvdata(serio, NULL);
  227. input_free_device(input_dev);
  228. kfree(sermouse);
  229. return err;
  230. }
  231. static struct serio_device_id sermouse_serio_ids[] = {
  232. {
  233. .type = SERIO_RS232,
  234. .proto = SERIO_MSC,
  235. .id = SERIO_ANY,
  236. .extra = SERIO_ANY,
  237. },
  238. {
  239. .type = SERIO_RS232,
  240. .proto = SERIO_SUN,
  241. .id = SERIO_ANY,
  242. .extra = SERIO_ANY,
  243. },
  244. {
  245. .type = SERIO_RS232,
  246. .proto = SERIO_MS,
  247. .id = SERIO_ANY,
  248. .extra = SERIO_ANY,
  249. },
  250. {
  251. .type = SERIO_RS232,
  252. .proto = SERIO_MP,
  253. .id = SERIO_ANY,
  254. .extra = SERIO_ANY,
  255. },
  256. {
  257. .type = SERIO_RS232,
  258. .proto = SERIO_MZ,
  259. .id = SERIO_ANY,
  260. .extra = SERIO_ANY,
  261. },
  262. {
  263. .type = SERIO_RS232,
  264. .proto = SERIO_MZP,
  265. .id = SERIO_ANY,
  266. .extra = SERIO_ANY,
  267. },
  268. {
  269. .type = SERIO_RS232,
  270. .proto = SERIO_MZPP,
  271. .id = SERIO_ANY,
  272. .extra = SERIO_ANY,
  273. },
  274. { 0 }
  275. };
  276. MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
  277. static struct serio_driver sermouse_drv = {
  278. .driver = {
  279. .name = "sermouse",
  280. },
  281. .description = DRIVER_DESC,
  282. .id_table = sermouse_serio_ids,
  283. .interrupt = sermouse_interrupt,
  284. .connect = sermouse_connect,
  285. .disconnect = sermouse_disconnect,
  286. };
  287. static int __init sermouse_init(void)
  288. {
  289. serio_register_driver(&sermouse_drv);
  290. return 0;
  291. }
  292. static void __exit sermouse_exit(void)
  293. {
  294. serio_unregister_driver(&sermouse_drv);
  295. }
  296. module_init(sermouse_init);
  297. module_exit(sermouse_exit);