wacom_w8001.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #define W8001_MAX_LENGTH 11
  25. #define W8001_LEAD_MASK 0x80
  26. #define W8001_LEAD_BYTE 0x80
  27. #define W8001_TAB_MASK 0x40
  28. #define W8001_TAB_BYTE 0x40
  29. #define W8001_QUERY_PACKET 0x20
  30. #define W8001_CMD_START '1'
  31. #define W8001_CMD_QUERY '*'
  32. struct w8001_coord {
  33. u8 rdy;
  34. u8 tsw;
  35. u8 f1;
  36. u8 f2;
  37. u16 x;
  38. u16 y;
  39. u16 pen_pressure;
  40. u8 tilt_x;
  41. u8 tilt_y;
  42. };
  43. /*
  44. * Per-touchscreen data.
  45. */
  46. struct w8001 {
  47. struct input_dev *dev;
  48. struct serio *serio;
  49. struct completion cmd_done;
  50. int id;
  51. int idx;
  52. unsigned char response_type;
  53. unsigned char response[W8001_MAX_LENGTH];
  54. unsigned char data[W8001_MAX_LENGTH];
  55. char phys[32];
  56. };
  57. static void parse_data(u8 *data, struct w8001_coord *coord)
  58. {
  59. memset(coord, 0, sizeof(*coord));
  60. coord->rdy = data[0] & 0x20;
  61. coord->tsw = data[0] & 0x01;
  62. coord->f1 = data[0] & 0x02;
  63. coord->f2 = data[0] & 0x04;
  64. coord->x = (data[1] & 0x7F) << 9;
  65. coord->x |= (data[2] & 0x7F) << 2;
  66. coord->x |= (data[6] & 0x60) >> 5;
  67. coord->y = (data[3] & 0x7F) << 9;
  68. coord->y |= (data[4] & 0x7F) << 2;
  69. coord->y |= (data[6] & 0x18) >> 3;
  70. coord->pen_pressure = data[5] & 0x7F;
  71. coord->pen_pressure |= (data[6] & 0x07) << 7 ;
  72. coord->tilt_x = data[7] & 0x7F;
  73. coord->tilt_y = data[8] & 0x7F;
  74. }
  75. static irqreturn_t w8001_interrupt(struct serio *serio,
  76. unsigned char data, unsigned int flags)
  77. {
  78. struct w8001 *w8001 = serio_get_drvdata(serio);
  79. struct input_dev *dev = w8001->dev;
  80. struct w8001_coord coord;
  81. unsigned char tmp;
  82. w8001->data[w8001->idx] = data;
  83. switch (w8001->idx++) {
  84. case 0:
  85. if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
  86. pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
  87. w8001->idx = 0;
  88. }
  89. break;
  90. case 8:
  91. tmp = w8001->data[0] & W8001_TAB_MASK;
  92. if (unlikely(tmp == W8001_TAB_BYTE))
  93. break;
  94. w8001->idx = 0;
  95. parse_data(w8001->data, &coord);
  96. input_report_abs(dev, ABS_X, coord.x);
  97. input_report_abs(dev, ABS_Y, coord.y);
  98. input_report_abs(dev, ABS_PRESSURE, coord.pen_pressure);
  99. input_report_key(dev, BTN_TOUCH, coord.tsw);
  100. input_sync(dev);
  101. break;
  102. case 10:
  103. w8001->idx = 0;
  104. memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
  105. w8001->response_type = W8001_QUERY_PACKET;
  106. complete(&w8001->cmd_done);
  107. break;
  108. }
  109. return IRQ_HANDLED;
  110. }
  111. static int w8001_command(struct w8001 *w8001, unsigned char command,
  112. bool wait_response)
  113. {
  114. int rc;
  115. w8001->response_type = 0;
  116. init_completion(&w8001->cmd_done);
  117. rc = serio_write(w8001->serio, command);
  118. if (rc == 0 && wait_response) {
  119. wait_for_completion_timeout(&w8001->cmd_done, HZ);
  120. if (w8001->response_type != W8001_QUERY_PACKET)
  121. rc = -EIO;
  122. }
  123. return rc;
  124. }
  125. static int w8001_setup(struct w8001 *w8001)
  126. {
  127. struct input_dev *dev = w8001->dev;
  128. struct w8001_coord coord;
  129. int error;
  130. error = w8001_command(w8001, W8001_CMD_QUERY, true);
  131. if (error)
  132. return error;
  133. parse_data(w8001->response, &coord);
  134. input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
  135. input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
  136. input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
  137. input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
  138. input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
  139. return w8001_command(w8001, W8001_CMD_START, false);
  140. }
  141. /*
  142. * w8001_disconnect() is the opposite of w8001_connect()
  143. */
  144. static void w8001_disconnect(struct serio *serio)
  145. {
  146. struct w8001 *w8001 = serio_get_drvdata(serio);
  147. input_get_device(w8001->dev);
  148. input_unregister_device(w8001->dev);
  149. serio_close(serio);
  150. serio_set_drvdata(serio, NULL);
  151. input_put_device(w8001->dev);
  152. kfree(w8001);
  153. }
  154. /*
  155. * w8001_connect() is the routine that is called when someone adds a
  156. * new serio device that supports the w8001 protocol and registers it as
  157. * an input device.
  158. */
  159. static int w8001_connect(struct serio *serio, struct serio_driver *drv)
  160. {
  161. struct w8001 *w8001;
  162. struct input_dev *input_dev;
  163. int err;
  164. w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
  165. input_dev = input_allocate_device();
  166. if (!w8001 || !input_dev) {
  167. err = -ENOMEM;
  168. goto fail1;
  169. }
  170. w8001->serio = serio;
  171. w8001->id = serio->id.id;
  172. w8001->dev = input_dev;
  173. init_completion(&w8001->cmd_done);
  174. snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
  175. input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
  176. input_dev->phys = w8001->phys;
  177. input_dev->id.bustype = BUS_RS232;
  178. input_dev->id.vendor = SERIO_W8001;
  179. input_dev->id.product = w8001->id;
  180. input_dev->id.version = 0x0100;
  181. input_dev->dev.parent = &serio->dev;
  182. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  183. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  184. serio_set_drvdata(serio, w8001);
  185. err = serio_open(serio, drv);
  186. if (err)
  187. goto fail2;
  188. err = w8001_setup(w8001);
  189. if (err)
  190. goto fail3;
  191. err = input_register_device(w8001->dev);
  192. if (err)
  193. goto fail3;
  194. return 0;
  195. fail3:
  196. serio_close(serio);
  197. fail2:
  198. serio_set_drvdata(serio, NULL);
  199. fail1:
  200. input_free_device(input_dev);
  201. kfree(w8001);
  202. return err;
  203. }
  204. static struct serio_device_id w8001_serio_ids[] = {
  205. {
  206. .type = SERIO_RS232,
  207. .proto = SERIO_W8001,
  208. .id = SERIO_ANY,
  209. .extra = SERIO_ANY,
  210. },
  211. { 0 }
  212. };
  213. MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
  214. static struct serio_driver w8001_drv = {
  215. .driver = {
  216. .name = "w8001",
  217. },
  218. .description = DRIVER_DESC,
  219. .id_table = w8001_serio_ids,
  220. .interrupt = w8001_interrupt,
  221. .connect = w8001_connect,
  222. .disconnect = w8001_disconnect,
  223. };
  224. static int __init w8001_init(void)
  225. {
  226. return serio_register_driver(&w8001_drv);
  227. }
  228. static void __exit w8001_exit(void)
  229. {
  230. serio_unregister_driver(&w8001_drv);
  231. }
  232. module_init(w8001_init);
  233. module_exit(w8001_exit);