sermouse.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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)
  62. 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)
  90. {
  91. struct input_dev *dev = sermouse->dev;
  92. signed char *buf = sermouse->buf;
  93. if (data & 0x40)
  94. sermouse->count = 0;
  95. else if (sermouse->count == 0)
  96. return;
  97. switch (sermouse->count) {
  98. case 0:
  99. buf[1] = data;
  100. input_report_key(dev, BTN_LEFT, (data >> 5) & 1);
  101. input_report_key(dev, BTN_RIGHT, (data >> 4) & 1);
  102. break;
  103. case 1:
  104. buf[2] = data;
  105. data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f));
  106. input_report_rel(dev, REL_X, data / 2);
  107. input_report_rel(dev, REL_Y, buf[4]);
  108. buf[3] = data - data / 2;
  109. break;
  110. case 2:
  111. /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */
  112. if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1]))
  113. input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key));
  114. buf[0] = buf[1];
  115. data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f));
  116. input_report_rel(dev, REL_X, buf[3]);
  117. input_report_rel(dev, REL_Y, data - buf[4]);
  118. buf[4] = data / 2;
  119. break;
  120. case 3:
  121. switch (sermouse->type) {
  122. case SERIO_MS:
  123. sermouse->type = SERIO_MP;
  124. case SERIO_MP:
  125. if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */
  126. input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1);
  127. input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
  128. break;
  129. case SERIO_MZP:
  130. case SERIO_MZPP:
  131. input_report_key(dev, BTN_SIDE, (data >> 5) & 1);
  132. case SERIO_MZ:
  133. input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1);
  134. input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7));
  135. break;
  136. }
  137. break;
  138. case 4:
  139. case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */
  140. buf[1] = (data >> 2) & 0x0f;
  141. break;
  142. case 5:
  143. case 7: /* Ignore anything besides MZ++ */
  144. if (sermouse->type != SERIO_MZPP)
  145. break;
  146. switch (buf[1]) {
  147. case 1: /* Extra mouse info */
  148. input_report_key(dev, BTN_SIDE, (data >> 4) & 1);
  149. input_report_key(dev, BTN_EXTRA, (data >> 5) & 1);
  150. input_report_rel(dev, data & 0x80 ? REL_HWHEEL : REL_WHEEL, (data & 7) - (data & 8));
  151. break;
  152. default: /* We don't decode anything else yet. */
  153. printk(KERN_WARNING
  154. "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf[1]);
  155. break;
  156. }
  157. break;
  158. }
  159. input_sync(dev);
  160. sermouse->count++;
  161. }
  162. /*
  163. * sermouse_interrupt() handles incoming characters, either gathering them into
  164. * packets or passing them to the command routine as command output.
  165. */
  166. static irqreturn_t sermouse_interrupt(struct serio *serio,
  167. unsigned char data, unsigned int flags)
  168. {
  169. struct sermouse *sermouse = serio_get_drvdata(serio);
  170. if (time_after(jiffies, sermouse->last + HZ/10))
  171. sermouse->count = 0;
  172. sermouse->last = jiffies;
  173. if (sermouse->type > SERIO_SUN)
  174. sermouse_process_ms(sermouse, data);
  175. else
  176. sermouse_process_msc(sermouse, data);
  177. return IRQ_HANDLED;
  178. }
  179. /*
  180. * sermouse_disconnect() cleans up after we don't want talk
  181. * to the mouse anymore.
  182. */
  183. static void sermouse_disconnect(struct serio *serio)
  184. {
  185. struct sermouse *sermouse = serio_get_drvdata(serio);
  186. serio_close(serio);
  187. serio_set_drvdata(serio, NULL);
  188. input_unregister_device(sermouse->dev);
  189. kfree(sermouse);
  190. }
  191. /*
  192. * sermouse_connect() is a callback form the serio module when
  193. * an unhandled serio port is found.
  194. */
  195. static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
  196. {
  197. struct sermouse *sermouse;
  198. struct input_dev *input_dev;
  199. unsigned char c = serio->id.extra;
  200. int err = -ENOMEM;
  201. sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
  202. input_dev = input_allocate_device();
  203. if (!sermouse || !input_dev)
  204. goto fail1;
  205. sermouse->dev = input_dev;
  206. snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
  207. sermouse->type = serio->id.proto;
  208. input_dev->name = sermouse_protocols[sermouse->type];
  209. input_dev->phys = sermouse->phys;
  210. input_dev->id.bustype = BUS_RS232;
  211. input_dev->id.vendor = sermouse->type;
  212. input_dev->id.product = c;
  213. input_dev->id.version = 0x0100;
  214. input_dev->dev.parent = &serio->dev;
  215. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  216. input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
  217. input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  218. if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit);
  219. if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit);
  220. if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit);
  221. if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit);
  222. if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit);
  223. serio_set_drvdata(serio, sermouse);
  224. err = serio_open(serio, drv);
  225. if (err)
  226. goto fail2;
  227. err = input_register_device(sermouse->dev);
  228. if (err)
  229. goto fail3;
  230. return 0;
  231. fail3: serio_close(serio);
  232. fail2: serio_set_drvdata(serio, NULL);
  233. fail1: input_free_device(input_dev);
  234. kfree(sermouse);
  235. return err;
  236. }
  237. static struct serio_device_id sermouse_serio_ids[] = {
  238. {
  239. .type = SERIO_RS232,
  240. .proto = SERIO_MSC,
  241. .id = SERIO_ANY,
  242. .extra = SERIO_ANY,
  243. },
  244. {
  245. .type = SERIO_RS232,
  246. .proto = SERIO_SUN,
  247. .id = SERIO_ANY,
  248. .extra = SERIO_ANY,
  249. },
  250. {
  251. .type = SERIO_RS232,
  252. .proto = SERIO_MS,
  253. .id = SERIO_ANY,
  254. .extra = SERIO_ANY,
  255. },
  256. {
  257. .type = SERIO_RS232,
  258. .proto = SERIO_MP,
  259. .id = SERIO_ANY,
  260. .extra = SERIO_ANY,
  261. },
  262. {
  263. .type = SERIO_RS232,
  264. .proto = SERIO_MZ,
  265. .id = SERIO_ANY,
  266. .extra = SERIO_ANY,
  267. },
  268. {
  269. .type = SERIO_RS232,
  270. .proto = SERIO_MZP,
  271. .id = SERIO_ANY,
  272. .extra = SERIO_ANY,
  273. },
  274. {
  275. .type = SERIO_RS232,
  276. .proto = SERIO_MZPP,
  277. .id = SERIO_ANY,
  278. .extra = SERIO_ANY,
  279. },
  280. { 0 }
  281. };
  282. MODULE_DEVICE_TABLE(serio, sermouse_serio_ids);
  283. static struct serio_driver sermouse_drv = {
  284. .driver = {
  285. .name = "sermouse",
  286. },
  287. .description = DRIVER_DESC,
  288. .id_table = sermouse_serio_ids,
  289. .interrupt = sermouse_interrupt,
  290. .connect = sermouse_connect,
  291. .disconnect = sermouse_disconnect,
  292. };
  293. static int __init sermouse_init(void)
  294. {
  295. return serio_register_driver(&sermouse_drv);
  296. }
  297. static void __exit sermouse_exit(void)
  298. {
  299. serio_unregister_driver(&sermouse_drv);
  300. }
  301. module_init(sermouse_init);
  302. module_exit(sermouse_exit);