wacom_w8001.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Wacom W8001 penabled serial touchscreen driver
  3. *
  4. * Copyright (c) 2008 Jaya Kumar
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. *
  11. * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/input.h>
  18. #include <linux/serio.h>
  19. #include <linux/init.h>
  20. #include <linux/ctype.h>
  21. #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
  22. MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
  23. MODULE_DESCRIPTION(DRIVER_DESC);
  24. MODULE_LICENSE("GPL");
  25. #define W8001_MAX_LENGTH 11
  26. #define W8001_LEAD_MASK 0x80
  27. #define W8001_LEAD_BYTE 0x80
  28. #define W8001_TAB_MASK 0x40
  29. #define W8001_TAB_BYTE 0x40
  30. /* set in first byte of touch data packets */
  31. #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
  32. #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
  33. #define W8001_QUERY_PACKET 0x20
  34. #define W8001_CMD_START '1'
  35. #define W8001_CMD_QUERY '*'
  36. #define W8001_CMD_TOUCHQUERY '%'
  37. /* length of data packets in bytes, depends on device. */
  38. #define W8001_PKTLEN_TOUCH93 5
  39. #define W8001_PKTLEN_TOUCH9A 7
  40. #define W8001_PKTLEN_TPCPEN 9
  41. #define W8001_PKTLEN_TPCCTL 11 /* control packet */
  42. #define W8001_PKTLEN_TOUCH2FG 13
  43. struct w8001_coord {
  44. u8 rdy;
  45. u8 tsw;
  46. u8 f1;
  47. u8 f2;
  48. u16 x;
  49. u16 y;
  50. u16 pen_pressure;
  51. u8 tilt_x;
  52. u8 tilt_y;
  53. };
  54. /* touch query reply packet */
  55. struct w8001_touch_query {
  56. u8 panel_res;
  57. u8 capacity_res;
  58. u8 sensor_id;
  59. u16 x;
  60. u16 y;
  61. };
  62. /*
  63. * Per-touchscreen data.
  64. */
  65. struct w8001 {
  66. struct input_dev *dev;
  67. struct serio *serio;
  68. struct completion cmd_done;
  69. int id;
  70. int idx;
  71. unsigned char response_type;
  72. unsigned char response[W8001_MAX_LENGTH];
  73. unsigned char data[W8001_MAX_LENGTH];
  74. char phys[32];
  75. int type;
  76. unsigned int pktlen;
  77. };
  78. static void parse_data(u8 *data, struct w8001_coord *coord)
  79. {
  80. memset(coord, 0, sizeof(*coord));
  81. coord->rdy = data[0] & 0x20;
  82. coord->tsw = data[0] & 0x01;
  83. coord->f1 = data[0] & 0x02;
  84. coord->f2 = data[0] & 0x04;
  85. coord->x = (data[1] & 0x7F) << 9;
  86. coord->x |= (data[2] & 0x7F) << 2;
  87. coord->x |= (data[6] & 0x60) >> 5;
  88. coord->y = (data[3] & 0x7F) << 9;
  89. coord->y |= (data[4] & 0x7F) << 2;
  90. coord->y |= (data[6] & 0x18) >> 3;
  91. coord->pen_pressure = data[5] & 0x7F;
  92. coord->pen_pressure |= (data[6] & 0x07) << 7 ;
  93. coord->tilt_x = data[7] & 0x7F;
  94. coord->tilt_y = data[8] & 0x7F;
  95. }
  96. static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
  97. {
  98. memset(query, 0, sizeof(*query));
  99. query->panel_res = data[1];
  100. query->sensor_id = data[2] & 0x7;
  101. query->capacity_res = data[7];
  102. query->x = data[3] << 9;
  103. query->x |= data[4] << 2;
  104. query->x |= (data[2] >> 5) & 0x3;
  105. query->y = data[5] << 9;
  106. query->y |= data[6] << 2;
  107. query->y |= (data[2] >> 3) & 0x3;
  108. }
  109. static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
  110. {
  111. struct input_dev *dev = w8001->dev;
  112. /*
  113. * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
  114. * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
  115. * or eraser. assume
  116. * - if dev is already in proximity and f2 is toggled → pen + side2
  117. * - if dev comes into proximity with f2 set → eraser
  118. * If f2 disappears after assuming eraser, fake proximity out for
  119. * eraser and in for pen.
  120. */
  121. if (!w8001->type) {
  122. w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  123. } else if (w8001->type == BTN_TOOL_RUBBER) {
  124. if (!coord->f2) {
  125. input_report_abs(dev, ABS_PRESSURE, 0);
  126. input_report_key(dev, BTN_TOUCH, 0);
  127. input_report_key(dev, BTN_STYLUS, 0);
  128. input_report_key(dev, BTN_STYLUS2, 0);
  129. input_report_key(dev, BTN_TOOL_RUBBER, 0);
  130. input_sync(dev);
  131. w8001->type = BTN_TOOL_PEN;
  132. }
  133. } else {
  134. input_report_key(dev, BTN_STYLUS2, coord->f2);
  135. }
  136. input_report_abs(dev, ABS_X, coord->x);
  137. input_report_abs(dev, ABS_Y, coord->y);
  138. input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
  139. input_report_key(dev, BTN_TOUCH, coord->tsw);
  140. input_report_key(dev, BTN_STYLUS, coord->f1);
  141. input_report_key(dev, w8001->type, coord->rdy);
  142. input_sync(dev);
  143. if (!coord->rdy)
  144. w8001->type = 0;
  145. }
  146. static irqreturn_t w8001_interrupt(struct serio *serio,
  147. unsigned char data, unsigned int flags)
  148. {
  149. struct w8001 *w8001 = serio_get_drvdata(serio);
  150. struct w8001_coord coord;
  151. unsigned char tmp;
  152. w8001->data[w8001->idx] = data;
  153. switch (w8001->idx++) {
  154. case 0:
  155. if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
  156. pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
  157. w8001->idx = 0;
  158. }
  159. break;
  160. case W8001_PKTLEN_TOUCH93 - 1:
  161. case W8001_PKTLEN_TOUCH9A - 1:
  162. /* ignore one-finger touch packet. */
  163. if (w8001->pktlen == w8001->idx)
  164. w8001->idx = 0;
  165. break;
  166. /* Pen coordinates packet */
  167. case W8001_PKTLEN_TPCPEN - 1:
  168. tmp = w8001->data[0] & W8001_TAB_MASK;
  169. if (unlikely(tmp == W8001_TAB_BYTE))
  170. break;
  171. tmp = (w8001->data[0] & W8001_TOUCH_BYTE);
  172. if (tmp == W8001_TOUCH_BYTE)
  173. break;
  174. w8001->idx = 0;
  175. parse_data(w8001->data, &coord);
  176. report_pen_events(w8001, &coord);
  177. break;
  178. /* control packet */
  179. case W8001_PKTLEN_TPCCTL - 1:
  180. tmp = (w8001->data[0] & W8001_TOUCH_MASK);
  181. if (tmp == W8001_TOUCH_BYTE)
  182. break;
  183. w8001->idx = 0;
  184. memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
  185. w8001->response_type = W8001_QUERY_PACKET;
  186. complete(&w8001->cmd_done);
  187. break;
  188. case W8001_PKTLEN_TOUCH2FG - 1:
  189. /* ignore two-finger touch packet. */
  190. if (w8001->pktlen == w8001->idx)
  191. w8001->idx = 0;
  192. break;
  193. }
  194. return IRQ_HANDLED;
  195. }
  196. static int w8001_command(struct w8001 *w8001, unsigned char command,
  197. bool wait_response)
  198. {
  199. int rc;
  200. w8001->response_type = 0;
  201. init_completion(&w8001->cmd_done);
  202. rc = serio_write(w8001->serio, command);
  203. if (rc == 0 && wait_response) {
  204. wait_for_completion_timeout(&w8001->cmd_done, HZ);
  205. if (w8001->response_type != W8001_QUERY_PACKET)
  206. rc = -EIO;
  207. }
  208. return rc;
  209. }
  210. static int w8001_setup(struct w8001 *w8001)
  211. {
  212. struct input_dev *dev = w8001->dev;
  213. struct w8001_coord coord;
  214. int error;
  215. error = w8001_command(w8001, W8001_CMD_QUERY, true);
  216. if (error)
  217. return error;
  218. parse_data(w8001->response, &coord);
  219. input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
  220. input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
  221. input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
  222. input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
  223. input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
  224. error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
  225. if (!error) {
  226. struct w8001_touch_query touch;
  227. parse_touchquery(w8001->response, &touch);
  228. switch (touch.sensor_id) {
  229. case 0:
  230. case 2:
  231. w8001->pktlen = W8001_PKTLEN_TOUCH93;
  232. break;
  233. case 1:
  234. case 3:
  235. case 4:
  236. w8001->pktlen = W8001_PKTLEN_TOUCH9A;
  237. break;
  238. case 5:
  239. w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
  240. break;
  241. }
  242. }
  243. return w8001_command(w8001, W8001_CMD_START, false);
  244. }
  245. /*
  246. * w8001_disconnect() is the opposite of w8001_connect()
  247. */
  248. static void w8001_disconnect(struct serio *serio)
  249. {
  250. struct w8001 *w8001 = serio_get_drvdata(serio);
  251. input_get_device(w8001->dev);
  252. input_unregister_device(w8001->dev);
  253. serio_close(serio);
  254. serio_set_drvdata(serio, NULL);
  255. input_put_device(w8001->dev);
  256. kfree(w8001);
  257. }
  258. /*
  259. * w8001_connect() is the routine that is called when someone adds a
  260. * new serio device that supports the w8001 protocol and registers it as
  261. * an input device.
  262. */
  263. static int w8001_connect(struct serio *serio, struct serio_driver *drv)
  264. {
  265. struct w8001 *w8001;
  266. struct input_dev *input_dev;
  267. int err;
  268. w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
  269. input_dev = input_allocate_device();
  270. if (!w8001 || !input_dev) {
  271. err = -ENOMEM;
  272. goto fail1;
  273. }
  274. w8001->serio = serio;
  275. w8001->id = serio->id.id;
  276. w8001->dev = input_dev;
  277. init_completion(&w8001->cmd_done);
  278. snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
  279. input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
  280. input_dev->phys = w8001->phys;
  281. input_dev->id.bustype = BUS_RS232;
  282. input_dev->id.vendor = SERIO_W8001;
  283. input_dev->id.product = w8001->id;
  284. input_dev->id.version = 0x0100;
  285. input_dev->dev.parent = &serio->dev;
  286. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  287. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  288. input_dev->keybit[BIT_WORD(BTN_TOOL_PEN)] |= BIT_MASK(BTN_TOOL_PEN);
  289. input_dev->keybit[BIT_WORD(BTN_TOOL_RUBBER)] |= BIT_MASK(BTN_TOOL_RUBBER);
  290. input_dev->keybit[BIT_WORD(BTN_STYLUS)] |= BIT_MASK(BTN_STYLUS);
  291. input_dev->keybit[BIT_WORD(BTN_STYLUS2)] |= BIT_MASK(BTN_STYLUS2);
  292. serio_set_drvdata(serio, w8001);
  293. err = serio_open(serio, drv);
  294. if (err)
  295. goto fail2;
  296. err = w8001_setup(w8001);
  297. if (err)
  298. goto fail3;
  299. err = input_register_device(w8001->dev);
  300. if (err)
  301. goto fail3;
  302. return 0;
  303. fail3:
  304. serio_close(serio);
  305. fail2:
  306. serio_set_drvdata(serio, NULL);
  307. fail1:
  308. input_free_device(input_dev);
  309. kfree(w8001);
  310. return err;
  311. }
  312. static struct serio_device_id w8001_serio_ids[] = {
  313. {
  314. .type = SERIO_RS232,
  315. .proto = SERIO_W8001,
  316. .id = SERIO_ANY,
  317. .extra = SERIO_ANY,
  318. },
  319. { 0 }
  320. };
  321. MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
  322. static struct serio_driver w8001_drv = {
  323. .driver = {
  324. .name = "w8001",
  325. },
  326. .description = DRIVER_DESC,
  327. .id_table = w8001_serio_ids,
  328. .interrupt = w8001_interrupt,
  329. .connect = w8001_connect,
  330. .disconnect = w8001_disconnect,
  331. };
  332. static int __init w8001_init(void)
  333. {
  334. return serio_register_driver(&w8001_drv);
  335. }
  336. static void __exit w8001_exit(void)
  337. {
  338. serio_unregister_driver(&w8001_drv);
  339. }
  340. module_init(w8001_init);
  341. module_exit(w8001_exit);