wacom_w8001.c 14 KB

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