wacom_w8001.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Wacom W8001 penabled serial touchscreen driver
  3. *
  4. * Copyright (c) 2008 Jaya Kumar
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/input.h>
  17. #include <linux/serio.h>
  18. #include <linux/init.h>
  19. #include <linux/ctype.h>
  20. #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
  21. MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
  22. MODULE_DESCRIPTION(DRIVER_DESC);
  23. MODULE_LICENSE("GPL");
  24. /*
  25. * Definitions & global arrays.
  26. */
  27. #define W8001_MAX_LENGTH 11
  28. #define W8001_PACKET_LEN 11
  29. #define W8001_LEAD_MASK 0x80
  30. #define W8001_LEAD_BYTE 0x80
  31. #define W8001_TAB_MASK 0x40
  32. #define W8001_TAB_BYTE 0x40
  33. #define W8001_QUERY_PACKET 0x20
  34. struct w8001_coord {
  35. u8 rdy;
  36. u8 tsw;
  37. u8 f1;
  38. u8 f2;
  39. u16 x;
  40. u16 y;
  41. u16 pen_pressure;
  42. u8 tilt_x;
  43. u8 tilt_y;
  44. };
  45. /*
  46. * Per-touchscreen data.
  47. */
  48. struct w8001 {
  49. struct input_dev *dev;
  50. struct serio *serio;
  51. struct mutex cmd_mutex;
  52. struct completion cmd_done;
  53. int id;
  54. int idx;
  55. unsigned char expected_packet;
  56. unsigned char data[W8001_MAX_LENGTH];
  57. unsigned char response[W8001_PACKET_LEN];
  58. char phys[32];
  59. };
  60. static int parse_data(u8 *data, struct w8001_coord *coord)
  61. {
  62. coord->rdy = data[0] & 0x20;
  63. coord->tsw = data[0] & 0x01;
  64. coord->f1 = data[0] & 0x02;
  65. coord->f2 = data[0] & 0x04;
  66. coord->x = (data[1] & 0x7F) << 9;
  67. coord->x |= (data[2] & 0x7F) << 2;
  68. coord->x |= (data[6] & 0x60) >> 5;
  69. coord->y = (data[3] & 0x7F) << 9;
  70. coord->y |= (data[4] & 0x7F) << 2;
  71. coord->y |= (data[6] & 0x18) >> 3;
  72. coord->pen_pressure = data[5] & 0x7F;
  73. coord->pen_pressure |= (data[6] & 0x07) << 7 ;
  74. coord->tilt_x = data[7] & 0x7F;
  75. coord->tilt_y = data[8] & 0x7F;
  76. return 0;
  77. }
  78. static void w8001_process_data(struct w8001 *w8001, unsigned char data)
  79. {
  80. struct input_dev *dev = w8001->dev;
  81. u8 tmp;
  82. struct w8001_coord coord;
  83. w8001->data[w8001->idx] = data;
  84. switch (w8001->idx++) {
  85. case 0:
  86. if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
  87. pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
  88. w8001->idx = 0;
  89. }
  90. break;
  91. case 8:
  92. tmp = w8001->data[0] & W8001_TAB_MASK;
  93. if (unlikely(tmp == W8001_TAB_BYTE))
  94. break;
  95. w8001->idx = 0;
  96. memset(&coord, 0, sizeof(coord));
  97. parse_data(w8001->data, &coord);
  98. input_report_abs(dev, ABS_X, coord.x);
  99. input_report_abs(dev, ABS_Y, coord.y);
  100. input_report_abs(dev, ABS_PRESSURE, coord.pen_pressure);
  101. input_report_key(dev, BTN_TOUCH, coord.tsw);
  102. input_sync(dev);
  103. break;
  104. case 10:
  105. w8001->idx = 0;
  106. memcpy(w8001->response, &w8001->data, W8001_PACKET_LEN);
  107. w8001->expected_packet = W8001_QUERY_PACKET;
  108. complete(&w8001->cmd_done);
  109. break;
  110. }
  111. }
  112. static irqreturn_t w8001_interrupt(struct serio *serio,
  113. unsigned char data, unsigned int flags)
  114. {
  115. struct w8001 *w8001 = serio_get_drvdata(serio);
  116. w8001_process_data(w8001, data);
  117. return IRQ_HANDLED;
  118. }
  119. static int w8001_async_command(struct w8001 *w8001, unsigned char *packet,
  120. int len)
  121. {
  122. int rc = -1;
  123. int i;
  124. mutex_lock(&w8001->cmd_mutex);
  125. for (i = 0; i < len; i++) {
  126. if (serio_write(w8001->serio, packet[i]))
  127. goto out;
  128. }
  129. rc = 0;
  130. out:
  131. mutex_unlock(&w8001->cmd_mutex);
  132. return rc;
  133. }
  134. static int w8001_command(struct w8001 *w8001, unsigned char *packet, int len)
  135. {
  136. int rc = -1;
  137. int i;
  138. mutex_lock(&w8001->cmd_mutex);
  139. serio_pause_rx(w8001->serio);
  140. init_completion(&w8001->cmd_done);
  141. serio_continue_rx(w8001->serio);
  142. for (i = 0; i < len; i++) {
  143. if (serio_write(w8001->serio, packet[i]))
  144. goto out;
  145. }
  146. wait_for_completion_timeout(&w8001->cmd_done, HZ);
  147. if (w8001->expected_packet == W8001_QUERY_PACKET) {
  148. /* We are back in reporting mode, the query was ACKed */
  149. memcpy(packet, w8001->response, W8001_PACKET_LEN);
  150. rc = 0;
  151. }
  152. out:
  153. mutex_unlock(&w8001->cmd_mutex);
  154. return rc;
  155. }
  156. static int w8001_setup(struct w8001 *w8001)
  157. {
  158. struct w8001_coord coord;
  159. struct input_dev *dev = w8001->dev;
  160. unsigned char start[1] = { '1' };
  161. unsigned char query[11] = { '*' };
  162. if (w8001_command(w8001, query, 1))
  163. return -1;
  164. memset(&coord, 0, sizeof(coord));
  165. parse_data(query, &coord);
  166. input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
  167. input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
  168. input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
  169. input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
  170. input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
  171. if (w8001_async_command(w8001, start, 1))
  172. return -1;
  173. return 0;
  174. }
  175. /*
  176. * w8001_disconnect() is the opposite of w8001_connect()
  177. */
  178. static void w8001_disconnect(struct serio *serio)
  179. {
  180. struct w8001 *w8001 = serio_get_drvdata(serio);
  181. input_get_device(w8001->dev);
  182. input_unregister_device(w8001->dev);
  183. serio_close(serio);
  184. serio_set_drvdata(serio, NULL);
  185. input_put_device(w8001->dev);
  186. kfree(w8001);
  187. }
  188. /*
  189. * w8001_connect() is the routine that is called when someone adds a
  190. * new serio device that supports the w8001 protocol and registers it as
  191. * an input device.
  192. */
  193. static int w8001_connect(struct serio *serio, struct serio_driver *drv)
  194. {
  195. struct w8001 *w8001;
  196. struct input_dev *input_dev;
  197. int err;
  198. w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
  199. input_dev = input_allocate_device();
  200. if (!w8001 || !input_dev) {
  201. err = -ENOMEM;
  202. goto fail1;
  203. }
  204. w8001->serio = serio;
  205. w8001->id = serio->id.id;
  206. w8001->dev = input_dev;
  207. mutex_init(&w8001->cmd_mutex);
  208. init_completion(&w8001->cmd_done);
  209. snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
  210. input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
  211. input_dev->phys = w8001->phys;
  212. input_dev->id.bustype = BUS_RS232;
  213. input_dev->id.vendor = SERIO_W8001;
  214. input_dev->id.product = w8001->id;
  215. input_dev->id.version = 0x0100;
  216. input_dev->dev.parent = &serio->dev;
  217. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  218. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  219. serio_set_drvdata(serio, w8001);
  220. err = serio_open(serio, drv);
  221. if (err)
  222. goto fail2;
  223. if (w8001_setup(w8001))
  224. goto fail3;
  225. err = input_register_device(w8001->dev);
  226. if (err)
  227. goto fail3;
  228. return 0;
  229. fail3:
  230. serio_close(serio);
  231. fail2:
  232. serio_set_drvdata(serio, NULL);
  233. fail1:
  234. input_free_device(input_dev);
  235. kfree(w8001);
  236. return err;
  237. }
  238. static struct serio_device_id w8001_serio_ids[] = {
  239. {
  240. .type = SERIO_RS232,
  241. .proto = SERIO_W8001,
  242. .id = SERIO_ANY,
  243. .extra = SERIO_ANY,
  244. },
  245. { 0 }
  246. };
  247. MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
  248. static struct serio_driver w8001_drv = {
  249. .driver = {
  250. .name = "w8001",
  251. },
  252. .description = DRIVER_DESC,
  253. .id_table = w8001_serio_ids,
  254. .interrupt = w8001_interrupt,
  255. .connect = w8001_connect,
  256. .disconnect = w8001_disconnect,
  257. };
  258. static int __init w8001_init(void)
  259. {
  260. return serio_register_driver(&w8001_drv);
  261. }
  262. static void __exit w8001_exit(void)
  263. {
  264. serio_unregister_driver(&w8001_drv);
  265. }
  266. module_init(w8001_init);
  267. module_exit(w8001_exit);