wacom_w8001.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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_open(struct input_dev *dev)
  308. {
  309. struct w8001 *w8001 = input_get_drvdata(dev);
  310. return w8001_command(w8001, W8001_CMD_START, false);
  311. }
  312. static void w8001_close(struct input_dev *dev)
  313. {
  314. struct w8001 *w8001 = input_get_drvdata(dev);
  315. w8001_command(w8001, W8001_CMD_STOP, false);
  316. }
  317. static int w8001_setup(struct w8001 *w8001)
  318. {
  319. struct input_dev *dev = w8001->dev;
  320. struct w8001_coord coord;
  321. struct w8001_touch_query touch;
  322. int error;
  323. error = w8001_command(w8001, W8001_CMD_STOP, false);
  324. if (error)
  325. return error;
  326. msleep(250); /* wait 250ms before querying the device */
  327. dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  328. strlcat(w8001->name, "Wacom Serial", sizeof(w8001->name));
  329. __set_bit(INPUT_PROP_DIRECT, dev->propbit);
  330. /* penabled? */
  331. error = w8001_command(w8001, W8001_CMD_QUERY, true);
  332. if (!error) {
  333. __set_bit(BTN_TOUCH, dev->keybit);
  334. __set_bit(BTN_TOOL_PEN, dev->keybit);
  335. __set_bit(BTN_TOOL_RUBBER, dev->keybit);
  336. __set_bit(BTN_STYLUS, dev->keybit);
  337. __set_bit(BTN_STYLUS2, dev->keybit);
  338. parse_pen_data(w8001->response, &coord);
  339. w8001->max_pen_x = coord.x;
  340. w8001->max_pen_y = coord.y;
  341. input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
  342. input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
  343. input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
  344. input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
  345. input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
  346. if (coord.tilt_x && coord.tilt_y) {
  347. input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
  348. input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
  349. }
  350. w8001->id = 0x90;
  351. strlcat(w8001->name, " Penabled", sizeof(w8001->name));
  352. }
  353. /* Touch enabled? */
  354. error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
  355. /*
  356. * Some non-touch devices may reply to the touch query. But their
  357. * second byte is empty, which indicates touch is not supported.
  358. */
  359. if (!error && w8001->response[1]) {
  360. __set_bit(BTN_TOUCH, dev->keybit);
  361. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  362. parse_touchquery(w8001->response, &touch);
  363. w8001->max_touch_x = touch.x;
  364. w8001->max_touch_y = touch.y;
  365. if (w8001->max_pen_x && w8001->max_pen_y) {
  366. /* if pen is supported scale to pen maximum */
  367. touch.x = w8001->max_pen_x;
  368. touch.y = w8001->max_pen_y;
  369. touch.panel_res = W8001_PEN_RESOLUTION;
  370. }
  371. input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
  372. input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
  373. input_abs_set_res(dev, ABS_X, touch.panel_res);
  374. input_abs_set_res(dev, ABS_Y, touch.panel_res);
  375. switch (touch.sensor_id) {
  376. case 0:
  377. case 2:
  378. w8001->pktlen = W8001_PKTLEN_TOUCH93;
  379. w8001->id = 0x93;
  380. strlcat(w8001->name, " 1FG", sizeof(w8001->name));
  381. break;
  382. case 1:
  383. case 3:
  384. case 4:
  385. w8001->pktlen = W8001_PKTLEN_TOUCH9A;
  386. strlcat(w8001->name, " 1FG", sizeof(w8001->name));
  387. w8001->id = 0x9a;
  388. break;
  389. case 5:
  390. w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
  391. input_mt_init_slots(dev, 2);
  392. input_set_abs_params(dev, ABS_MT_POSITION_X,
  393. 0, touch.x, 0, 0);
  394. input_set_abs_params(dev, ABS_MT_POSITION_Y,
  395. 0, touch.y, 0, 0);
  396. input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
  397. 0, MT_TOOL_MAX, 0, 0);
  398. strlcat(w8001->name, " 2FG", sizeof(w8001->name));
  399. if (w8001->max_pen_x && w8001->max_pen_y)
  400. w8001->id = 0xE3;
  401. else
  402. w8001->id = 0xE2;
  403. break;
  404. }
  405. }
  406. strlcat(w8001->name, " Touchscreen", sizeof(w8001->name));
  407. return 0;
  408. }
  409. /*
  410. * w8001_disconnect() is the opposite of w8001_connect()
  411. */
  412. static void w8001_disconnect(struct serio *serio)
  413. {
  414. struct w8001 *w8001 = serio_get_drvdata(serio);
  415. serio_close(serio);
  416. input_unregister_device(w8001->dev);
  417. kfree(w8001);
  418. serio_set_drvdata(serio, NULL);
  419. }
  420. /*
  421. * w8001_connect() is the routine that is called when someone adds a
  422. * new serio device that supports the w8001 protocol and registers it as
  423. * an input device.
  424. */
  425. static int w8001_connect(struct serio *serio, struct serio_driver *drv)
  426. {
  427. struct w8001 *w8001;
  428. struct input_dev *input_dev;
  429. int err;
  430. w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
  431. input_dev = input_allocate_device();
  432. if (!w8001 || !input_dev) {
  433. err = -ENOMEM;
  434. goto fail1;
  435. }
  436. w8001->serio = serio;
  437. w8001->dev = input_dev;
  438. init_completion(&w8001->cmd_done);
  439. snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
  440. serio_set_drvdata(serio, w8001);
  441. err = serio_open(serio, drv);
  442. if (err)
  443. goto fail2;
  444. err = w8001_setup(w8001);
  445. if (err)
  446. goto fail3;
  447. input_dev->name = w8001->name;
  448. input_dev->phys = w8001->phys;
  449. input_dev->id.product = w8001->id;
  450. input_dev->id.bustype = BUS_RS232;
  451. input_dev->id.vendor = 0x056a;
  452. input_dev->id.version = 0x0100;
  453. input_dev->dev.parent = &serio->dev;
  454. input_dev->open = w8001_open;
  455. input_dev->close = w8001_close;
  456. input_set_drvdata(input_dev, w8001);
  457. err = input_register_device(w8001->dev);
  458. if (err)
  459. goto fail3;
  460. return 0;
  461. fail3:
  462. serio_close(serio);
  463. fail2:
  464. serio_set_drvdata(serio, NULL);
  465. fail1:
  466. input_free_device(input_dev);
  467. kfree(w8001);
  468. return err;
  469. }
  470. static struct serio_device_id w8001_serio_ids[] = {
  471. {
  472. .type = SERIO_RS232,
  473. .proto = SERIO_W8001,
  474. .id = SERIO_ANY,
  475. .extra = SERIO_ANY,
  476. },
  477. { 0 }
  478. };
  479. MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
  480. static struct serio_driver w8001_drv = {
  481. .driver = {
  482. .name = "w8001",
  483. },
  484. .description = DRIVER_DESC,
  485. .id_table = w8001_serio_ids,
  486. .interrupt = w8001_interrupt,
  487. .connect = w8001_connect,
  488. .disconnect = w8001_disconnect,
  489. };
  490. static int __init w8001_init(void)
  491. {
  492. return serio_register_driver(&w8001_drv);
  493. }
  494. static void __exit w8001_exit(void)
  495. {
  496. serio_unregister_driver(&w8001_drv);
  497. }
  498. module_init(w8001_init);
  499. module_exit(w8001_exit);