wacom_w8001.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Wacom W8001 penabled serial touchscreen driver
  3. *
  4. * Copyright (c) 2008 Jaya Kumar
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. *
  12. * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/input/mt.h>
  19. #include <linux/serio.h>
  20. #include <linux/init.h>
  21. #include <linux/ctype.h>
  22. #include <linux/delay.h>
  23. #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
  24. MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
  25. MODULE_DESCRIPTION(DRIVER_DESC);
  26. MODULE_LICENSE("GPL");
  27. #define W8001_MAX_LENGTH 11
  28. #define W8001_LEAD_MASK 0x80
  29. #define W8001_LEAD_BYTE 0x80
  30. #define W8001_TAB_MASK 0x40
  31. #define W8001_TAB_BYTE 0x40
  32. /* set in first byte of touch data packets */
  33. #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
  34. #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
  35. #define W8001_QUERY_PACKET 0x20
  36. #define W8001_CMD_STOP '0'
  37. #define W8001_CMD_START '1'
  38. #define W8001_CMD_QUERY '*'
  39. #define W8001_CMD_TOUCHQUERY '%'
  40. /* length of data packets in bytes, depends on device. */
  41. #define W8001_PKTLEN_TOUCH93 5
  42. #define W8001_PKTLEN_TOUCH9A 7
  43. #define W8001_PKTLEN_TPCPEN 9
  44. #define W8001_PKTLEN_TPCCTL 11 /* control packet */
  45. #define W8001_PKTLEN_TOUCH2FG 13
  46. /* resolution in points/mm */
  47. #define W8001_PEN_RESOLUTION 100
  48. #define W8001_TOUCH_RESOLUTION 10
  49. struct w8001_coord {
  50. u8 rdy;
  51. u8 tsw;
  52. u8 f1;
  53. u8 f2;
  54. u16 x;
  55. u16 y;
  56. u16 pen_pressure;
  57. u8 tilt_x;
  58. u8 tilt_y;
  59. };
  60. /* touch query reply packet */
  61. struct w8001_touch_query {
  62. u16 x;
  63. u16 y;
  64. u8 panel_res;
  65. u8 capacity_res;
  66. u8 sensor_id;
  67. };
  68. /*
  69. * Per-touchscreen data.
  70. */
  71. struct w8001 {
  72. struct input_dev *dev;
  73. struct serio *serio;
  74. struct completion cmd_done;
  75. int id;
  76. int idx;
  77. unsigned char response_type;
  78. unsigned char response[W8001_MAX_LENGTH];
  79. unsigned char data[W8001_MAX_LENGTH];
  80. char phys[32];
  81. int type;
  82. unsigned int pktlen;
  83. u16 max_touch_x;
  84. u16 max_touch_y;
  85. u16 max_pen_x;
  86. u16 max_pen_y;
  87. char name[64];
  88. };
  89. static void parse_pen_data(u8 *data, struct w8001_coord *coord)
  90. {
  91. memset(coord, 0, sizeof(*coord));
  92. coord->rdy = data[0] & 0x20;
  93. coord->tsw = data[0] & 0x01;
  94. coord->f1 = data[0] & 0x02;
  95. coord->f2 = data[0] & 0x04;
  96. coord->x = (data[1] & 0x7F) << 9;
  97. coord->x |= (data[2] & 0x7F) << 2;
  98. coord->x |= (data[6] & 0x60) >> 5;
  99. coord->y = (data[3] & 0x7F) << 9;
  100. coord->y |= (data[4] & 0x7F) << 2;
  101. coord->y |= (data[6] & 0x18) >> 3;
  102. coord->pen_pressure = data[5] & 0x7F;
  103. coord->pen_pressure |= (data[6] & 0x07) << 7 ;
  104. coord->tilt_x = data[7] & 0x7F;
  105. coord->tilt_y = data[8] & 0x7F;
  106. }
  107. static void parse_single_touch(u8 *data, struct w8001_coord *coord)
  108. {
  109. coord->x = (data[1] << 7) | data[2];
  110. coord->y = (data[3] << 7) | data[4];
  111. coord->tsw = data[0] & 0x01;
  112. }
  113. static void scale_touch_coordinates(struct w8001 *w8001,
  114. unsigned int *x, unsigned int *y)
  115. {
  116. if (w8001->max_pen_x && w8001->max_touch_x)
  117. *x = *x * w8001->max_pen_x / w8001->max_touch_x;
  118. if (w8001->max_pen_y && w8001->max_touch_y)
  119. *y = *y * w8001->max_pen_y / w8001->max_touch_y;
  120. }
  121. static void parse_multi_touch(struct w8001 *w8001)
  122. {
  123. struct input_dev *dev = w8001->dev;
  124. unsigned char *data = w8001->data;
  125. unsigned int x, y;
  126. int i;
  127. int count = 0;
  128. for (i = 0; i < 2; i++) {
  129. bool touch = data[0] & (1 << i);
  130. input_mt_slot(dev, i);
  131. input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
  132. if (touch) {
  133. x = (data[6 * i + 1] << 7) | data[6 * i + 2];
  134. y = (data[6 * i + 3] << 7) | data[6 * i + 4];
  135. /* data[5,6] and [11,12] is finger capacity */
  136. /* scale to pen maximum */
  137. scale_touch_coordinates(w8001, &x, &y);
  138. input_report_abs(dev, ABS_MT_POSITION_X, x);
  139. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  140. count++;
  141. }
  142. }
  143. /* emulate single touch events when stylus is out of proximity.
  144. * This is to make single touch backward support consistent
  145. * across all Wacom single touch devices.
  146. */
  147. if (w8001->type != BTN_TOOL_PEN &&
  148. w8001->type != BTN_TOOL_RUBBER) {
  149. w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED;
  150. input_mt_report_pointer_emulation(dev, true);
  151. }
  152. input_sync(dev);
  153. }
  154. static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
  155. {
  156. memset(query, 0, sizeof(*query));
  157. query->panel_res = data[1];
  158. query->sensor_id = data[2] & 0x7;
  159. query->capacity_res = data[7];
  160. query->x = data[3] << 9;
  161. query->x |= data[4] << 2;
  162. query->x |= (data[2] >> 5) & 0x3;
  163. query->y = data[5] << 9;
  164. query->y |= data[6] << 2;
  165. query->y |= (data[2] >> 3) & 0x3;
  166. /* Early days' single-finger touch models need the following defaults */
  167. if (!query->x && !query->y) {
  168. query->x = 1024;
  169. query->y = 1024;
  170. if (query->panel_res)
  171. query->x = query->y = (1 << query->panel_res);
  172. query->panel_res = W8001_TOUCH_RESOLUTION;
  173. }
  174. }
  175. static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
  176. {
  177. struct input_dev *dev = w8001->dev;
  178. /*
  179. * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
  180. * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
  181. * or eraser. Assume:
  182. * - if dev is already in proximity and f2 is toggled → pen + side2
  183. * - if dev comes into proximity with f2 set → eraser
  184. * If f2 disappears after assuming eraser, fake proximity out for
  185. * eraser and in for pen.
  186. */
  187. switch (w8001->type) {
  188. case BTN_TOOL_RUBBER:
  189. if (!coord->f2) {
  190. input_report_abs(dev, ABS_PRESSURE, 0);
  191. input_report_key(dev, BTN_TOUCH, 0);
  192. input_report_key(dev, BTN_STYLUS, 0);
  193. input_report_key(dev, BTN_STYLUS2, 0);
  194. input_report_key(dev, BTN_TOOL_RUBBER, 0);
  195. input_sync(dev);
  196. w8001->type = BTN_TOOL_PEN;
  197. }
  198. break;
  199. case BTN_TOOL_FINGER:
  200. input_report_key(dev, BTN_TOUCH, 0);
  201. input_report_key(dev, BTN_TOOL_FINGER, 0);
  202. input_sync(dev);
  203. /* fall through */
  204. case KEY_RESERVED:
  205. w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  206. break;
  207. default:
  208. input_report_key(dev, BTN_STYLUS2, coord->f2);
  209. break;
  210. }
  211. input_report_abs(dev, ABS_X, coord->x);
  212. input_report_abs(dev, ABS_Y, coord->y);
  213. input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
  214. input_report_key(dev, BTN_TOUCH, coord->tsw);
  215. input_report_key(dev, BTN_STYLUS, coord->f1);
  216. input_report_key(dev, w8001->type, coord->rdy);
  217. input_sync(dev);
  218. if (!coord->rdy)
  219. w8001->type = KEY_RESERVED;
  220. }
  221. static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
  222. {
  223. struct input_dev *dev = w8001->dev;
  224. unsigned int x = coord->x;
  225. unsigned int y = coord->y;
  226. /* scale to pen maximum */
  227. scale_touch_coordinates(w8001, &x, &y);
  228. input_report_abs(dev, ABS_X, x);
  229. input_report_abs(dev, ABS_Y, y);
  230. input_report_key(dev, BTN_TOUCH, coord->tsw);
  231. input_report_key(dev, BTN_TOOL_FINGER, coord->tsw);
  232. input_sync(dev);
  233. w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
  234. }
  235. static irqreturn_t w8001_interrupt(struct serio *serio,
  236. unsigned char data, unsigned int flags)
  237. {
  238. struct w8001 *w8001 = serio_get_drvdata(serio);
  239. struct w8001_coord coord;
  240. unsigned char tmp;
  241. w8001->data[w8001->idx] = data;
  242. switch (w8001->idx++) {
  243. case 0:
  244. if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
  245. pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
  246. w8001->idx = 0;
  247. }
  248. break;
  249. case W8001_PKTLEN_TOUCH93 - 1:
  250. case W8001_PKTLEN_TOUCH9A - 1:
  251. tmp = w8001->data[0] & W8001_TOUCH_BYTE;
  252. if (tmp != W8001_TOUCH_BYTE)
  253. break;
  254. if (w8001->pktlen == w8001->idx) {
  255. w8001->idx = 0;
  256. if (w8001->type != BTN_TOOL_PEN &&
  257. w8001->type != BTN_TOOL_RUBBER) {
  258. parse_single_touch(w8001->data, &coord);
  259. report_single_touch(w8001, &coord);
  260. }
  261. }
  262. break;
  263. /* Pen coordinates packet */
  264. case W8001_PKTLEN_TPCPEN - 1:
  265. tmp = w8001->data[0] & W8001_TAB_MASK;
  266. if (unlikely(tmp == W8001_TAB_BYTE))
  267. break;
  268. tmp = w8001->data[0] & W8001_TOUCH_BYTE;
  269. if (tmp == W8001_TOUCH_BYTE)
  270. break;
  271. w8001->idx = 0;
  272. parse_pen_data(w8001->data, &coord);
  273. report_pen_events(w8001, &coord);
  274. break;
  275. /* control packet */
  276. case W8001_PKTLEN_TPCCTL - 1:
  277. tmp = w8001->data[0] & W8001_TOUCH_MASK;
  278. if (tmp == W8001_TOUCH_BYTE)
  279. break;
  280. w8001->idx = 0;
  281. memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
  282. w8001->response_type = W8001_QUERY_PACKET;
  283. complete(&w8001->cmd_done);
  284. break;
  285. /* 2 finger touch packet */
  286. case W8001_PKTLEN_TOUCH2FG - 1:
  287. w8001->idx = 0;
  288. parse_multi_touch(w8001);
  289. break;
  290. }
  291. return IRQ_HANDLED;
  292. }
  293. static int w8001_command(struct w8001 *w8001, unsigned char command,
  294. bool wait_response)
  295. {
  296. int rc;
  297. w8001->response_type = 0;
  298. init_completion(&w8001->cmd_done);
  299. rc = serio_write(w8001->serio, command);
  300. if (rc == 0 && wait_response) {
  301. wait_for_completion_timeout(&w8001->cmd_done, HZ);
  302. if (w8001->response_type != W8001_QUERY_PACKET)
  303. rc = -EIO;
  304. }
  305. return rc;
  306. }
  307. static int w8001_setup(struct w8001 *w8001)
  308. {
  309. struct input_dev *dev = w8001->dev;
  310. struct w8001_coord coord;
  311. struct w8001_touch_query touch;
  312. int error;
  313. error = w8001_command(w8001, W8001_CMD_STOP, false);
  314. if (error)
  315. return error;
  316. msleep(250); /* wait 250ms before querying the device */
  317. dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  318. strlcat(w8001->name, "Wacom Serial", sizeof(w8001->name));
  319. /* penabled? */
  320. error = w8001_command(w8001, W8001_CMD_QUERY, true);
  321. if (!error) {
  322. __set_bit(BTN_TOUCH, dev->keybit);
  323. __set_bit(BTN_TOOL_PEN, dev->keybit);
  324. __set_bit(BTN_TOOL_RUBBER, dev->keybit);
  325. __set_bit(BTN_STYLUS, dev->keybit);
  326. __set_bit(BTN_STYLUS2, dev->keybit);
  327. parse_pen_data(w8001->response, &coord);
  328. w8001->max_pen_x = coord.x;
  329. w8001->max_pen_y = coord.y;
  330. input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
  331. input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
  332. input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
  333. input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
  334. input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
  335. if (coord.tilt_x && coord.tilt_y) {
  336. input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
  337. input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
  338. }
  339. w8001->id = 0x90;
  340. strlcat(w8001->name, " Penabled", sizeof(w8001->name));
  341. }
  342. /* Touch enabled? */
  343. error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
  344. /*
  345. * Some non-touch devices may reply to the touch query. But their
  346. * second byte is empty, which indicates touch is not supported.
  347. */
  348. if (!error && w8001->response[1]) {
  349. __set_bit(BTN_TOUCH, dev->keybit);
  350. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  351. parse_touchquery(w8001->response, &touch);
  352. w8001->max_touch_x = touch.x;
  353. w8001->max_touch_y = touch.y;
  354. if (w8001->max_pen_x && w8001->max_pen_y) {
  355. /* if pen is supported scale to pen maximum */
  356. touch.x = w8001->max_pen_x;
  357. touch.y = w8001->max_pen_y;
  358. touch.panel_res = W8001_PEN_RESOLUTION;
  359. }
  360. input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
  361. input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
  362. input_abs_set_res(dev, ABS_X, touch.panel_res);
  363. input_abs_set_res(dev, ABS_Y, touch.panel_res);
  364. switch (touch.sensor_id) {
  365. case 0:
  366. case 2:
  367. w8001->pktlen = W8001_PKTLEN_TOUCH93;
  368. w8001->id = 0x93;
  369. strlcat(w8001->name, " 1FG", sizeof(w8001->name));
  370. break;
  371. case 1:
  372. case 3:
  373. case 4:
  374. w8001->pktlen = W8001_PKTLEN_TOUCH9A;
  375. strlcat(w8001->name, " 1FG", sizeof(w8001->name));
  376. w8001->id = 0x9a;
  377. break;
  378. case 5:
  379. w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
  380. input_mt_init_slots(dev, 2);
  381. input_set_abs_params(dev, ABS_MT_POSITION_X,
  382. 0, touch.x, 0, 0);
  383. input_set_abs_params(dev, ABS_MT_POSITION_Y,
  384. 0, touch.y, 0, 0);
  385. input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
  386. 0, MT_TOOL_MAX, 0, 0);
  387. strlcat(w8001->name, " 2FG", sizeof(w8001->name));
  388. if (w8001->max_pen_x && w8001->max_pen_y)
  389. w8001->id = 0xE3;
  390. else
  391. w8001->id = 0xE2;
  392. break;
  393. }
  394. }
  395. strlcat(w8001->name, " Touchscreen", sizeof(w8001->name));
  396. return w8001_command(w8001, W8001_CMD_START, false);
  397. }
  398. /*
  399. * w8001_disconnect() is the opposite of w8001_connect()
  400. */
  401. static void w8001_disconnect(struct serio *serio)
  402. {
  403. struct w8001 *w8001 = serio_get_drvdata(serio);
  404. input_get_device(w8001->dev);
  405. input_unregister_device(w8001->dev);
  406. serio_close(serio);
  407. serio_set_drvdata(serio, NULL);
  408. input_put_device(w8001->dev);
  409. kfree(w8001);
  410. }
  411. /*
  412. * w8001_connect() is the routine that is called when someone adds a
  413. * new serio device that supports the w8001 protocol and registers it as
  414. * an input device.
  415. */
  416. static int w8001_connect(struct serio *serio, struct serio_driver *drv)
  417. {
  418. struct w8001 *w8001;
  419. struct input_dev *input_dev;
  420. int err;
  421. w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
  422. input_dev = input_allocate_device();
  423. if (!w8001 || !input_dev) {
  424. err = -ENOMEM;
  425. goto fail1;
  426. }
  427. w8001->serio = serio;
  428. w8001->dev = input_dev;
  429. init_completion(&w8001->cmd_done);
  430. snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
  431. serio_set_drvdata(serio, w8001);
  432. err = serio_open(serio, drv);
  433. if (err)
  434. goto fail2;
  435. err = w8001_setup(w8001);
  436. if (err)
  437. goto fail3;
  438. input_dev->name = w8001->name;
  439. input_dev->phys = w8001->phys;
  440. input_dev->id.product = w8001->id;
  441. input_dev->id.bustype = BUS_RS232;
  442. input_dev->id.vendor = 0x056a;
  443. input_dev->id.version = 0x0100;
  444. input_dev->dev.parent = &serio->dev;
  445. err = input_register_device(w8001->dev);
  446. if (err)
  447. goto fail3;
  448. return 0;
  449. fail3:
  450. serio_close(serio);
  451. fail2:
  452. serio_set_drvdata(serio, NULL);
  453. fail1:
  454. input_free_device(input_dev);
  455. kfree(w8001);
  456. return err;
  457. }
  458. static struct serio_device_id w8001_serio_ids[] = {
  459. {
  460. .type = SERIO_RS232,
  461. .proto = SERIO_W8001,
  462. .id = SERIO_ANY,
  463. .extra = SERIO_ANY,
  464. },
  465. { 0 }
  466. };
  467. MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
  468. static struct serio_driver w8001_drv = {
  469. .driver = {
  470. .name = "w8001",
  471. },
  472. .description = DRIVER_DESC,
  473. .id_table = w8001_serio_ids,
  474. .interrupt = w8001_interrupt,
  475. .connect = w8001_connect,
  476. .disconnect = w8001_disconnect,
  477. };
  478. static int __init w8001_init(void)
  479. {
  480. return serio_register_driver(&w8001_drv);
  481. }
  482. static void __exit w8001_exit(void)
  483. {
  484. serio_unregister_driver(&w8001_drv);
  485. }
  486. module_init(w8001_init);
  487. module_exit(w8001_exit);