vsxxxaa.c 14 KB

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