vsxxxaa.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Driver for DEC VSXXX-AA mouse (hockey-puck mouse, ball or two rollers)
  3. * DEC VSXXX-GA mouse (rectangular mouse, with ball)
  4. * DEC VSXXX-AB tablet (digitizer with hair cross or stylus)
  5. *
  6. * Copyright (C) 2003-2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
  7. *
  8. * The packet format was initially taken from a patch to GPM which is (C) 2001
  9. * by Karsten Merker <merker@linuxtag.org>
  10. * and Maciej W. Rozycki <macro@ds2.pg.gda.pl>
  11. * Later on, I had access to the device's documentation (referenced below).
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. /*
  29. * Building an adaptor to DE9 / DB25 RS232
  30. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. *
  32. * DISCLAIMER: Use this description AT YOUR OWN RISK! I'll not pay for
  33. * anything if you break your mouse, your computer or whatever!
  34. *
  35. * In theory, this mouse is a simple RS232 device. In practice, it has got
  36. * a quite uncommon plug and the requirement to additionally get a power
  37. * supply at +5V and -12V.
  38. *
  39. * If you look at the socket/jack (_not_ at the plug), we use this pin
  40. * numbering:
  41. * _______
  42. * / 7 6 5 \
  43. * | 4 --- 3 |
  44. * \ 2 1 /
  45. * -------
  46. *
  47. * DEC socket DE9 DB25 Note
  48. * 1 (GND) 5 7 -
  49. * 2 (RxD) 2 3 -
  50. * 3 (TxD) 3 2 -
  51. * 4 (-12V) - - Somewhere from the PSU. At ATX, it's
  52. * the thin blue wire at pin 12 of the
  53. * ATX power connector. Only required for
  54. * VSXXX-AA/-GA mice.
  55. * 5 (+5V) - - PSU (red wires of ATX power connector
  56. * on pin 4, 6, 19 or 20) or HDD power
  57. * connector (also red wire).
  58. * 6 (+12V) - - HDD power connector, yellow wire. Only
  59. * required for VSXXX-AB digitizer.
  60. * 7 (dev. avail.) - - The mouse shorts this one to pin 1.
  61. * This way, the host computer can detect
  62. * the mouse. To use it with the adaptor,
  63. * simply don't connect this pin.
  64. *
  65. * So to get a working adaptor, you need to connect the mouse with three
  66. * wires to a RS232 port and two or three additional wires for +5V, +12V and
  67. * -12V to the PSU.
  68. *
  69. * Flow specification for the link is 4800, 8o1.
  70. *
  71. * The mice and tablet are described in "VCB02 Video Subsystem - Technical
  72. * Manual", DEC EK-104AA-TM-001. You'll find it at MANX, a search engine
  73. * specific for DEC documentation. Try
  74. * http://www.vt100.net/manx/details?pn=EK-104AA-TM-001;id=21;cp=1
  75. */
  76. #include <linux/delay.h>
  77. #include <linux/module.h>
  78. #include <linux/slab.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/input.h>
  81. #include <linux/config.h>
  82. #include <linux/serio.h>
  83. #include <linux/init.h>
  84. #define DRIVER_DESC "Driver for DEC VSXXX-AA and -GA mice and VSXXX-AB tablet"
  85. MODULE_AUTHOR ("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  86. MODULE_DESCRIPTION (DRIVER_DESC);
  87. MODULE_LICENSE ("GPL");
  88. #undef VSXXXAA_DEBUG
  89. #ifdef VSXXXAA_DEBUG
  90. #define DBG(x...) printk (x)
  91. #else
  92. #define DBG(x...) do {} while (0)
  93. #endif
  94. #define VSXXXAA_INTRO_MASK 0x80
  95. #define VSXXXAA_INTRO_HEAD 0x80
  96. #define IS_HDR_BYTE(x) (((x) & VSXXXAA_INTRO_MASK) \
  97. == VSXXXAA_INTRO_HEAD)
  98. #define VSXXXAA_PACKET_MASK 0xe0
  99. #define VSXXXAA_PACKET_REL 0x80
  100. #define VSXXXAA_PACKET_ABS 0xc0
  101. #define VSXXXAA_PACKET_POR 0xa0
  102. #define MATCH_PACKET_TYPE(data, type) (((data) & VSXXXAA_PACKET_MASK) == (type))
  103. struct vsxxxaa {
  104. struct input_dev *dev;
  105. struct serio *serio;
  106. #define BUFLEN 15 /* At least 5 is needed for a full tablet packet */
  107. unsigned char buf[BUFLEN];
  108. unsigned char count;
  109. unsigned char version;
  110. unsigned char country;
  111. unsigned char type;
  112. char name[64];
  113. char phys[32];
  114. };
  115. static void
  116. vsxxxaa_drop_bytes (struct vsxxxaa *mouse, int num)
  117. {
  118. if (num >= mouse->count)
  119. mouse->count = 0;
  120. else {
  121. memmove (mouse->buf, mouse->buf + num - 1, BUFLEN - num);
  122. mouse->count -= num;
  123. }
  124. }
  125. static void
  126. vsxxxaa_queue_byte (struct vsxxxaa *mouse, unsigned char byte)
  127. {
  128. if (mouse->count == BUFLEN) {
  129. printk (KERN_ERR "%s on %s: Dropping a byte of full buffer.\n",
  130. mouse->name, mouse->phys);
  131. vsxxxaa_drop_bytes (mouse, 1);
  132. }
  133. DBG (KERN_INFO "Queueing byte 0x%02x\n", byte);
  134. mouse->buf[mouse->count++] = byte;
  135. }
  136. static void
  137. vsxxxaa_detection_done (struct vsxxxaa *mouse)
  138. {
  139. switch (mouse->type) {
  140. case 0x02:
  141. strlcpy (mouse->name, "DEC VSXXX-AA/-GA mouse",
  142. sizeof (mouse->name));
  143. break;
  144. case 0x04:
  145. strlcpy (mouse->name, "DEC VSXXX-AB digitizer",
  146. sizeof (mouse->name));
  147. break;
  148. default:
  149. snprintf (mouse->name, sizeof (mouse->name),
  150. "unknown DEC pointer device (type = 0x%02x)",
  151. mouse->type);
  152. break;
  153. }
  154. printk (KERN_INFO
  155. "Found %s version 0x%02x from country 0x%02x on port %s\n",
  156. mouse->name, mouse->version, mouse->country, mouse->phys);
  157. }
  158. /*
  159. * Returns number of bytes to be dropped, 0 if packet is okay.
  160. */
  161. static int
  162. vsxxxaa_check_packet (struct vsxxxaa *mouse, int packet_len)
  163. {
  164. int i;
  165. /* First byte must be a header byte */
  166. if (!IS_HDR_BYTE (mouse->buf[0])) {
  167. DBG ("vsck: len=%d, 1st=0x%02x\n", packet_len, mouse->buf[0]);
  168. return 1;
  169. }
  170. /* Check all following bytes */
  171. if (packet_len > 1) {
  172. for (i = 1; i < packet_len; i++) {
  173. if (IS_HDR_BYTE (mouse->buf[i])) {
  174. printk (KERN_ERR "Need to drop %d bytes "
  175. "of a broken packet.\n",
  176. i - 1);
  177. DBG (KERN_INFO "check: len=%d, b[%d]=0x%02x\n",
  178. packet_len, i, mouse->buf[i]);
  179. return i - 1;
  180. }
  181. }
  182. }
  183. return 0;
  184. }
  185. static __inline__ int
  186. vsxxxaa_smells_like_packet (struct vsxxxaa *mouse, unsigned char type, size_t len)
  187. {
  188. return (mouse->count >= len) && MATCH_PACKET_TYPE (mouse->buf[0], type);
  189. }
  190. static void
  191. vsxxxaa_handle_REL_packet (struct vsxxxaa *mouse, struct pt_regs *regs)
  192. {
  193. struct input_dev *dev = mouse->dev;
  194. unsigned char *buf = mouse->buf;
  195. int left, middle, right;
  196. int dx, dy;
  197. /*
  198. * Check for normal stream packets. This is three bytes,
  199. * with the first byte's 3 MSB set to 100.
  200. *
  201. * [0]: 1 0 0 SignX SignY Left Middle Right
  202. * [1]: 0 dx dx dx dx dx dx dx
  203. * [2]: 0 dy dy dy dy dy dy dy
  204. */
  205. /*
  206. * Low 7 bit of byte 1 are abs(dx), bit 7 is
  207. * 0, bit 4 of byte 0 is direction.
  208. */
  209. dx = buf[1] & 0x7f;
  210. dx *= ((buf[0] >> 4) & 0x01)? 1: -1;
  211. /*
  212. * Low 7 bit of byte 2 are abs(dy), bit 7 is
  213. * 0, bit 3 of byte 0 is direction.
  214. */
  215. dy = buf[2] & 0x7f;
  216. dy *= ((buf[0] >> 3) & 0x01)? -1: 1;
  217. /*
  218. * Get button state. It's the low three bits
  219. * (for three buttons) of byte 0.
  220. */
  221. left = (buf[0] & 0x04)? 1: 0;
  222. middle = (buf[0] & 0x02)? 1: 0;
  223. right = (buf[0] & 0x01)? 1: 0;
  224. vsxxxaa_drop_bytes (mouse, 3);
  225. DBG (KERN_INFO "%s on %s: dx=%d, dy=%d, buttons=%s%s%s\n",
  226. mouse->name, mouse->phys, dx, dy,
  227. left? "L": "l", middle? "M": "m", right? "R": "r");
  228. /*
  229. * Report what we've found so far...
  230. */
  231. input_regs (dev, regs);
  232. input_report_key (dev, BTN_LEFT, left);
  233. input_report_key (dev, BTN_MIDDLE, middle);
  234. input_report_key (dev, BTN_RIGHT, right);
  235. input_report_key (dev, BTN_TOUCH, 0);
  236. input_report_rel (dev, REL_X, dx);
  237. input_report_rel (dev, REL_Y, dy);
  238. input_sync (dev);
  239. }
  240. static void
  241. vsxxxaa_handle_ABS_packet (struct vsxxxaa *mouse, struct pt_regs *regs)
  242. {
  243. struct input_dev *dev = mouse->dev;
  244. unsigned char *buf = mouse->buf;
  245. int left, middle, right, touch;
  246. int x, y;
  247. /*
  248. * Tablet position / button packet
  249. *
  250. * [0]: 1 1 0 B4 B3 B2 B1 Pr
  251. * [1]: 0 0 X5 X4 X3 X2 X1 X0
  252. * [2]: 0 0 X11 X10 X9 X8 X7 X6
  253. * [3]: 0 0 Y5 Y4 Y3 Y2 Y1 Y0
  254. * [4]: 0 0 Y11 Y10 Y9 Y8 Y7 Y6
  255. */
  256. /*
  257. * Get X/Y position. Y axis needs to be inverted since VSXXX-AB
  258. * counts down->top while monitor counts top->bottom.
  259. */
  260. x = ((buf[2] & 0x3f) << 6) | (buf[1] & 0x3f);
  261. y = ((buf[4] & 0x3f) << 6) | (buf[3] & 0x3f);
  262. y = 1023 - y;
  263. /*
  264. * Get button state. It's bits <4..1> of byte 0.
  265. */
  266. left = (buf[0] & 0x02)? 1: 0;
  267. middle = (buf[0] & 0x04)? 1: 0;
  268. right = (buf[0] & 0x08)? 1: 0;
  269. touch = (buf[0] & 0x10)? 1: 0;
  270. vsxxxaa_drop_bytes (mouse, 5);
  271. DBG (KERN_INFO "%s on %s: x=%d, y=%d, buttons=%s%s%s%s\n",
  272. mouse->name, mouse->phys, x, y,
  273. left? "L": "l", middle? "M": "m",
  274. right? "R": "r", touch? "T": "t");
  275. /*
  276. * Report what we've found so far...
  277. */
  278. input_regs (dev, regs);
  279. input_report_key (dev, BTN_LEFT, left);
  280. input_report_key (dev, BTN_MIDDLE, middle);
  281. input_report_key (dev, BTN_RIGHT, right);
  282. input_report_key (dev, BTN_TOUCH, touch);
  283. input_report_abs (dev, ABS_X, x);
  284. input_report_abs (dev, ABS_Y, y);
  285. input_sync (dev);
  286. }
  287. static void
  288. vsxxxaa_handle_POR_packet (struct vsxxxaa *mouse, struct pt_regs *regs)
  289. {
  290. struct input_dev *dev = mouse->dev;
  291. unsigned char *buf = mouse->buf;
  292. int left, middle, right;
  293. unsigned char error;
  294. /*
  295. * Check for Power-On-Reset packets. These are sent out
  296. * after plugging the mouse in, or when explicitely
  297. * requested by sending 'T'.
  298. *
  299. * [0]: 1 0 1 0 R3 R2 R1 R0
  300. * [1]: 0 M2 M1 M0 D3 D2 D1 D0
  301. * [2]: 0 E6 E5 E4 E3 E2 E1 E0
  302. * [3]: 0 0 0 0 0 Left Middle Right
  303. *
  304. * M: manufacturer location code
  305. * R: revision code
  306. * E: Error code. If it's in the range of 0x00..0x1f, only some
  307. * minor problem occured. Errors >= 0x20 are considered bad
  308. * and the device may not work properly...
  309. * D: <0010> == mouse, <0100> == tablet
  310. */
  311. mouse->version = buf[0] & 0x0f;
  312. mouse->country = (buf[1] >> 4) & 0x07;
  313. mouse->type = buf[1] & 0x0f;
  314. error = buf[2] & 0x7f;
  315. /*
  316. * Get button state. It's the low three bits
  317. * (for three buttons) of byte 0. Maybe even the bit <3>
  318. * has some meaning if a tablet is attached.
  319. */
  320. left = (buf[0] & 0x04)? 1: 0;
  321. middle = (buf[0] & 0x02)? 1: 0;
  322. right = (buf[0] & 0x01)? 1: 0;
  323. vsxxxaa_drop_bytes (mouse, 4);
  324. vsxxxaa_detection_done (mouse);
  325. if (error <= 0x1f) {
  326. /* No (serious) error. Report buttons */
  327. input_regs (dev, regs);
  328. input_report_key (dev, BTN_LEFT, left);
  329. input_report_key (dev, BTN_MIDDLE, middle);
  330. input_report_key (dev, BTN_RIGHT, right);
  331. input_report_key (dev, BTN_TOUCH, 0);
  332. input_sync (dev);
  333. if (error != 0)
  334. printk (KERN_INFO "Your %s on %s reports error=0x%02x\n",
  335. mouse->name, mouse->phys, error);
  336. }
  337. /*
  338. * If the mouse was hot-plugged, we need to force differential mode
  339. * now... However, give it a second to recover from it's reset.
  340. */
  341. printk (KERN_NOTICE "%s on %s: Forceing standard packet format, "
  342. "incremental streaming mode and 72 samples/sec\n",
  343. mouse->name, mouse->phys);
  344. mouse->serio->write (mouse->serio, 'S'); /* Standard format */
  345. mdelay (50);
  346. mouse->serio->write (mouse->serio, 'R'); /* Incremental */
  347. mdelay (50);
  348. mouse->serio->write (mouse->serio, 'L'); /* 72 samples/sec */
  349. }
  350. static void
  351. vsxxxaa_parse_buffer (struct vsxxxaa *mouse, struct pt_regs *regs)
  352. {
  353. unsigned char *buf = mouse->buf;
  354. int stray_bytes;
  355. /*
  356. * Parse buffer to death...
  357. */
  358. do {
  359. /*
  360. * Out of sync? Throw away what we don't understand. Each
  361. * packet starts with a byte whose bit 7 is set. Unhandled
  362. * packets (ie. which we don't know about or simply b0rk3d
  363. * data...) will get shifted out of the buffer after some
  364. * activity on the mouse.
  365. */
  366. while (mouse->count > 0 && !IS_HDR_BYTE(buf[0])) {
  367. printk (KERN_ERR "%s on %s: Dropping a byte to regain "
  368. "sync with mouse data stream...\n",
  369. mouse->name, mouse->phys);
  370. vsxxxaa_drop_bytes (mouse, 1);
  371. }
  372. /*
  373. * Check for packets we know about.
  374. */
  375. if (vsxxxaa_smells_like_packet (mouse, VSXXXAA_PACKET_REL, 3)) {
  376. /* Check for broken packet */
  377. stray_bytes = vsxxxaa_check_packet (mouse, 3);
  378. if (stray_bytes > 0) {
  379. printk (KERN_ERR "Dropping %d bytes now...\n",
  380. stray_bytes);
  381. vsxxxaa_drop_bytes (mouse, stray_bytes);
  382. continue;
  383. }
  384. vsxxxaa_handle_REL_packet (mouse, regs);
  385. continue; /* More to parse? */
  386. }
  387. if (vsxxxaa_smells_like_packet (mouse, VSXXXAA_PACKET_ABS, 5)) {
  388. /* Check for broken packet */
  389. stray_bytes = vsxxxaa_check_packet (mouse, 5);
  390. if (stray_bytes > 0) {
  391. printk (KERN_ERR "Dropping %d bytes now...\n",
  392. stray_bytes);
  393. vsxxxaa_drop_bytes (mouse, stray_bytes);
  394. continue;
  395. }
  396. vsxxxaa_handle_ABS_packet (mouse, regs);
  397. continue; /* More to parse? */
  398. }
  399. if (vsxxxaa_smells_like_packet (mouse, VSXXXAA_PACKET_POR, 4)) {
  400. /* Check for broken packet */
  401. stray_bytes = vsxxxaa_check_packet (mouse, 4);
  402. if (stray_bytes > 0) {
  403. printk (KERN_ERR "Dropping %d bytes now...\n",
  404. stray_bytes);
  405. vsxxxaa_drop_bytes (mouse, stray_bytes);
  406. continue;
  407. }
  408. vsxxxaa_handle_POR_packet (mouse, regs);
  409. continue; /* More to parse? */
  410. }
  411. break; /* No REL, ABS or POR packet found */
  412. } while (1);
  413. }
  414. static irqreturn_t
  415. vsxxxaa_interrupt (struct serio *serio, unsigned char data, unsigned int flags,
  416. struct pt_regs *regs)
  417. {
  418. struct vsxxxaa *mouse = serio_get_drvdata (serio);
  419. vsxxxaa_queue_byte (mouse, data);
  420. vsxxxaa_parse_buffer (mouse, regs);
  421. return IRQ_HANDLED;
  422. }
  423. static void
  424. vsxxxaa_disconnect (struct serio *serio)
  425. {
  426. struct vsxxxaa *mouse = serio_get_drvdata (serio);
  427. serio_close (serio);
  428. serio_set_drvdata (serio, NULL);
  429. input_unregister_device (mouse->dev);
  430. kfree (mouse);
  431. }
  432. static int
  433. vsxxxaa_connect (struct serio *serio, struct serio_driver *drv)
  434. {
  435. struct vsxxxaa *mouse;
  436. struct input_dev *input_dev;
  437. int err = -ENOMEM;
  438. mouse = kzalloc (sizeof (struct vsxxxaa), GFP_KERNEL);
  439. input_dev = input_allocate_device ();
  440. if (!mouse || !input_dev)
  441. goto fail;
  442. mouse->dev = input_dev;
  443. mouse->serio = serio;
  444. strlcat (mouse->name, "DEC VSXXX-AA/-GA mouse or VSXXX-AB digitizer",
  445. sizeof (mouse->name));
  446. snprintf (mouse->phys, sizeof (mouse->phys), "%s/input0", serio->phys);
  447. input_dev->name = mouse->name;
  448. input_dev->phys = mouse->phys;
  449. input_dev->id.bustype = BUS_RS232;
  450. input_dev->cdev.dev = &serio->dev;
  451. input_dev->private = mouse;
  452. set_bit (EV_KEY, input_dev->evbit); /* We have buttons */
  453. set_bit (EV_REL, input_dev->evbit);
  454. set_bit (EV_ABS, input_dev->evbit);
  455. set_bit (BTN_LEFT, input_dev->keybit); /* We have 3 buttons */
  456. set_bit (BTN_MIDDLE, input_dev->keybit);
  457. set_bit (BTN_RIGHT, input_dev->keybit);
  458. set_bit (BTN_TOUCH, input_dev->keybit); /* ...and Tablet */
  459. set_bit (REL_X, input_dev->relbit);
  460. set_bit (REL_Y, input_dev->relbit);
  461. input_set_abs_params (input_dev, ABS_X, 0, 1023, 0, 0);
  462. input_set_abs_params (input_dev, ABS_Y, 0, 1023, 0, 0);
  463. serio_set_drvdata (serio, mouse);
  464. err = serio_open (serio, drv);
  465. if (err)
  466. goto fail;
  467. /*
  468. * Request selftest. Standard packet format and differential
  469. * mode will be requested after the device ID'ed successfully.
  470. */
  471. serio->write (serio, 'T'); /* Test */
  472. input_register_device (input_dev);
  473. return 0;
  474. fail: serio_set_drvdata (serio, NULL);
  475. input_free_device (input_dev);
  476. kfree (mouse);
  477. return err;
  478. }
  479. static struct serio_device_id vsxxaa_serio_ids[] = {
  480. {
  481. .type = SERIO_RS232,
  482. .proto = SERIO_VSXXXAA,
  483. .id = SERIO_ANY,
  484. .extra = SERIO_ANY,
  485. },
  486. { 0 }
  487. };
  488. MODULE_DEVICE_TABLE(serio, vsxxaa_serio_ids);
  489. static struct serio_driver vsxxxaa_drv = {
  490. .driver = {
  491. .name = "vsxxxaa",
  492. },
  493. .description = DRIVER_DESC,
  494. .id_table = vsxxaa_serio_ids,
  495. .connect = vsxxxaa_connect,
  496. .interrupt = vsxxxaa_interrupt,
  497. .disconnect = vsxxxaa_disconnect,
  498. };
  499. static int __init
  500. vsxxxaa_init (void)
  501. {
  502. serio_register_driver(&vsxxxaa_drv);
  503. return 0;
  504. }
  505. static void __exit
  506. vsxxxaa_exit (void)
  507. {
  508. serio_unregister_driver(&vsxxxaa_drv);
  509. }
  510. module_init (vsxxxaa_init);
  511. module_exit (vsxxxaa_exit);