sermouse.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/config.h>
  34. #include <linux/serio.h>
  35. #include <linux/init.h>
  36. #define DRIVER_DESC "Serial mouse driver"
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. static char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse",
  41. "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse",
  42. "Logitech MZ++ Mouse"};
  43. struct sermouse {
  44. struct input_dev *dev;
  45. signed char buf[8];
  46. unsigned char count;
  47. unsigned char type;
  48. unsigned long last;
  49. char phys[32];
  50. };
  51. /*
  52. * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and
  53. * applies some prediction to the data, resulting in 96 updates per
  54. * second, which is as good as a PS/2 or USB mouse.
  55. */
  56. static void sermouse_process_msc(struct sermouse *sermouse, signed char data, struct pt_regs *regs)
  57. {
  58. struct input_dev *dev = sermouse->dev;
  59. signed char *buf = sermouse->buf;
  60. input_regs(dev, regs);
  61. switch (sermouse->count) {
  62. case 0:
  63. if ((data & 0xf8) != 0x80) return;
  64. input_report_key(dev, BTN_LEFT, !(data & 4));
  65. input_report_key(dev, BTN_RIGHT, !(data & 1));
  66. input_report_key(dev, BTN_MIDDLE, !(data & 2));
  67. break;
  68. case 1:
  69. case 3:
  70. input_report_rel(dev, REL_X, data / 2);
  71. input_report_rel(dev, REL_Y, -buf[1]);
  72. buf[0] = data - data / 2;
  73. break;
  74. case 2:
  75. case 4:
  76. input_report_rel(dev, REL_X, buf[0]);
  77. input_report_rel(dev, REL_Y, buf[1] - data);
  78. buf[1] = data / 2;
  79. break;
  80. }
  81. input_sync(dev);
  82. if (++sermouse->count == 5)
  83. sermouse->count = 0;
  84. }
  85. /*
  86. * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and
  87. * generates events. With prediction it gets 80 updates/sec, assuming
  88. * standard 3-byte packets and 1200 bps.
  89. */
  90. static void sermouse_process_ms(struct sermouse *sermouse, signed char data, struct pt_regs *regs)
  91. {
  92. struct input_dev *dev = sermouse->dev;
  93. signed char *buf = sermouse->buf;
  94. if (data & 0x40) sermouse->count = 0;
  95. input_regs(dev, regs);
  96. switch (sermouse->count) {
  97. case 0:
  98. buf[1] = data;
  99. input_report_key(dev, BTN_LEFT, (data >> 5) & 1);
  100. input_report_key(dev, BTN_RIGHT, (data >> 4) & 1);
  101. break;
  102. case 1:
  103. buf[2] = data;
  104. data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
  105. input_report_rel(dev, REL_X, data / 2);
  106. input_report_rel(dev, REL_Y, buf[4]);
  107. buf[3] = data - data / 2;
  108. break;
  109. case 2:
  110. /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
  111. if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
  112. input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
  113. buf[0] = buf[1];
  114. data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
  115. input_report_rel(dev, REL_X, buf[3]);
  116. input_report_rel(dev, REL_Y, data - buf[4]);
  117. buf[4] = data / 2;
  118. break;
  119. case 3:
  120. switch (sermouse->type) {
  121. case SERIO_MS:
  122. sermouse->type = SERIO_MP;
  123. case SERIO_MP:
  124. if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */
  125. input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
  126. input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
  127. break;
  128. case SERIO_MZP:
  129. case SERIO_MZPP:
  130. input_report_key(dev, BTN_SIDE, (data >> 5) & 1);
  131. case SERIO_MZ:
  132. input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
  133. input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7));
  134. break;
  135. }
  136. break;
  137. case 4:
  138. case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
  139. buf[1] = (data >> 2) & 0x0f;
  140. break;
  141. case 5:
  142. case 7: /* Ignore anything besides MZ++ */
  143. if (sermouse->type != SERIO_MZPP) 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, struct pt_regs *regs)
  166. {
  167. struct sermouse *sermouse = serio_get_drvdata(serio);
  168. if (time_after(jiffies, sermouse->last + HZ/10)) sermouse->count = 0;
  169. sermouse->last = jiffies;
  170. if (sermouse->type > SERIO_SUN)
  171. sermouse_process_ms(sermouse, data, regs);
  172. else
  173. sermouse_process_msc(sermouse, data, regs);
  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 fail;
  202. sermouse->dev = input_dev;
  203. sprintf(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->cdev.dev = &serio->dev;
  212. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  213. input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
  214. input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  215. input_dev->private = sermouse;
  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 fail;
  225. input_register_device(sermouse->dev);
  226. return 0;
  227. fail: serio_set_drvdata(serio, NULL);
  228. input_free_device(input_dev);
  229. kfree(sermouse);
  230. return err;
  231. }
  232. static struct serio_device_id sermouse_serio_ids[] = {
  233. {
  234. .type = SERIO_RS232,
  235. .proto = SERIO_MSC,
  236. .id = SERIO_ANY,
  237. .extra = SERIO_ANY,
  238. },
  239. {
  240. .type = SERIO_RS232,
  241. .proto = SERIO_SUN,
  242. .id = SERIO_ANY,
  243. .extra = SERIO_ANY,
  244. },
  245. {
  246. .type = SERIO_RS232,
  247. .proto = SERIO_MS,
  248. .id = SERIO_ANY,
  249. .extra = SERIO_ANY,
  250. },
  251. {
  252. .type = SERIO_RS232,
  253. .proto = SERIO_MP,
  254. .id = SERIO_ANY,
  255. .extra = SERIO_ANY,
  256. },
  257. {
  258. .type = SERIO_RS232,
  259. .proto = SERIO_MZ,
  260. .id = SERIO_ANY,
  261. .extra = SERIO_ANY,
  262. },
  263. {
  264. .type = SERIO_RS232,
  265. .proto = SERIO_MZP,
  266. .id = SERIO_ANY,
  267. .extra = SERIO_ANY,
  268. },
  269. {
  270. .type = SERIO_RS232,
  271. .proto = SERIO_MZPP,
  272. .id = SERIO_ANY,
  273. .extra = SERIO_ANY,
  274. },
  275. { 0 }
  276. };
  277. MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
  278. static struct serio_driver sermouse_drv = {
  279. .driver = {
  280. .name = "sermouse",
  281. },
  282. .description = DRIVER_DESC,
  283. .id_table = sermouse_serio_ids,
  284. .interrupt = sermouse_interrupt,
  285. .connect = sermouse_connect,
  286. .disconnect = sermouse_disconnect,
  287. };
  288. static int __init sermouse_init(void)
  289. {
  290. serio_register_driver(&sermouse_drv);
  291. return 0;
  292. }
  293. static void __exit sermouse_exit(void)
  294. {
  295. serio_unregister_driver(&sermouse_drv);
  296. }
  297. module_init(sermouse_init);
  298. module_exit(sermouse_exit);