sermouse.c 8.5 KB

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