vsxxxaa.c 14 KB

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