touchit213.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sahara TouchIT-213 serial touchscreen driver
  4. *
  5. * Copyright (c) 2007-2008 Claudio Nieder <[email protected]>
  6. *
  7. * Based on Touchright driver (drivers/input/touchscreen/touchright.c)
  8. * Copyright (c) 2006 Rick Koch <[email protected]>
  9. * Copyright (c) 2004 Vojtech Pavlik
  10. * and Dan Streetman <[email protected]>
  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. #define DRIVER_DESC "Sahara TouchIT-213 serial touchscreen driver"
  19. MODULE_AUTHOR("Claudio Nieder <[email protected]>");
  20. MODULE_DESCRIPTION(DRIVER_DESC);
  21. MODULE_LICENSE("GPL");
  22. /*
  23. * Definitions & global arrays.
  24. */
  25. /*
  26. * Data is received through COM1 at 9600bit/s,8bit,no parity in packets
  27. * of 5 byte each.
  28. *
  29. * +--------+ +--------+ +--------+ +--------+ +--------+
  30. * |1000000p| |0xxxxxxx| |0xxxxxxx| |0yyyyyyy| |0yyyyyyy|
  31. * +--------+ +--------+ +--------+ +--------+ +--------+
  32. * MSB LSB MSB LSB
  33. *
  34. * The value of p is 1 as long as the screen is touched and 0 when
  35. * reporting the location where touching stopped, e.g. where the pen was
  36. * lifted from the screen.
  37. *
  38. * When holding the screen in landscape mode as the BIOS text output is
  39. * presented, x is the horizontal axis with values growing from left to
  40. * right and y is the vertical axis with values growing from top to
  41. * bottom.
  42. *
  43. * When holding the screen in portrait mode with the Sahara logo in its
  44. * correct position, x ist the vertical axis with values growing from
  45. * top to bottom and y is the horizontal axis with values growing from
  46. * right to left.
  47. */
  48. #define T213_FORMAT_TOUCH_BIT 0x01
  49. #define T213_FORMAT_STATUS_BYTE 0x80
  50. #define T213_FORMAT_STATUS_MASK ~T213_FORMAT_TOUCH_BIT
  51. /*
  52. * On my Sahara Touch-IT 213 I have observed x values from 0 to 0x7f0
  53. * and y values from 0x1d to 0x7e9, so the actual measurement is
  54. * probably done with an 11 bit precision.
  55. */
  56. #define T213_MIN_XC 0
  57. #define T213_MAX_XC 0x07ff
  58. #define T213_MIN_YC 0
  59. #define T213_MAX_YC 0x07ff
  60. /*
  61. * Per-touchscreen data.
  62. */
  63. struct touchit213 {
  64. struct input_dev *dev;
  65. struct serio *serio;
  66. int idx;
  67. unsigned char csum;
  68. unsigned char data[5];
  69. char phys[32];
  70. };
  71. static irqreturn_t touchit213_interrupt(struct serio *serio,
  72. unsigned char data, unsigned int flags)
  73. {
  74. struct touchit213 *touchit213 = serio_get_drvdata(serio);
  75. struct input_dev *dev = touchit213->dev;
  76. touchit213->data[touchit213->idx] = data;
  77. switch (touchit213->idx++) {
  78. case 0:
  79. if ((touchit213->data[0] & T213_FORMAT_STATUS_MASK) !=
  80. T213_FORMAT_STATUS_BYTE) {
  81. pr_debug("unsynchronized data: 0x%02x\n", data);
  82. touchit213->idx = 0;
  83. }
  84. break;
  85. case 4:
  86. touchit213->idx = 0;
  87. input_report_abs(dev, ABS_X,
  88. (touchit213->data[1] << 7) | touchit213->data[2]);
  89. input_report_abs(dev, ABS_Y,
  90. (touchit213->data[3] << 7) | touchit213->data[4]);
  91. input_report_key(dev, BTN_TOUCH,
  92. touchit213->data[0] & T213_FORMAT_TOUCH_BIT);
  93. input_sync(dev);
  94. break;
  95. }
  96. return IRQ_HANDLED;
  97. }
  98. /*
  99. * touchit213_disconnect() is the opposite of touchit213_connect()
  100. */
  101. static void touchit213_disconnect(struct serio *serio)
  102. {
  103. struct touchit213 *touchit213 = serio_get_drvdata(serio);
  104. input_get_device(touchit213->dev);
  105. input_unregister_device(touchit213->dev);
  106. serio_close(serio);
  107. serio_set_drvdata(serio, NULL);
  108. input_put_device(touchit213->dev);
  109. kfree(touchit213);
  110. }
  111. /*
  112. * touchit213_connect() is the routine that is called when someone adds a
  113. * new serio device that supports the Touchright protocol and registers it as
  114. * an input device.
  115. */
  116. static int touchit213_connect(struct serio *serio, struct serio_driver *drv)
  117. {
  118. struct touchit213 *touchit213;
  119. struct input_dev *input_dev;
  120. int err;
  121. touchit213 = kzalloc(sizeof(struct touchit213), GFP_KERNEL);
  122. input_dev = input_allocate_device();
  123. if (!touchit213 || !input_dev) {
  124. err = -ENOMEM;
  125. goto fail1;
  126. }
  127. touchit213->serio = serio;
  128. touchit213->dev = input_dev;
  129. snprintf(touchit213->phys, sizeof(touchit213->phys),
  130. "%s/input0", serio->phys);
  131. input_dev->name = "Sahara Touch-iT213 Serial TouchScreen";
  132. input_dev->phys = touchit213->phys;
  133. input_dev->id.bustype = BUS_RS232;
  134. input_dev->id.vendor = SERIO_TOUCHIT213;
  135. input_dev->id.product = 0;
  136. input_dev->id.version = 0x0100;
  137. input_dev->dev.parent = &serio->dev;
  138. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  139. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  140. input_set_abs_params(touchit213->dev, ABS_X,
  141. T213_MIN_XC, T213_MAX_XC, 0, 0);
  142. input_set_abs_params(touchit213->dev, ABS_Y,
  143. T213_MIN_YC, T213_MAX_YC, 0, 0);
  144. serio_set_drvdata(serio, touchit213);
  145. err = serio_open(serio, drv);
  146. if (err)
  147. goto fail2;
  148. err = input_register_device(touchit213->dev);
  149. if (err)
  150. goto fail3;
  151. return 0;
  152. fail3: serio_close(serio);
  153. fail2: serio_set_drvdata(serio, NULL);
  154. fail1: input_free_device(input_dev);
  155. kfree(touchit213);
  156. return err;
  157. }
  158. /*
  159. * The serio driver structure.
  160. */
  161. static const struct serio_device_id touchit213_serio_ids[] = {
  162. {
  163. .type = SERIO_RS232,
  164. .proto = SERIO_TOUCHIT213,
  165. .id = SERIO_ANY,
  166. .extra = SERIO_ANY,
  167. },
  168. { 0 }
  169. };
  170. MODULE_DEVICE_TABLE(serio, touchit213_serio_ids);
  171. static struct serio_driver touchit213_drv = {
  172. .driver = {
  173. .name = "touchit213",
  174. },
  175. .description = DRIVER_DESC,
  176. .id_table = touchit213_serio_ids,
  177. .interrupt = touchit213_interrupt,
  178. .connect = touchit213_connect,
  179. .disconnect = touchit213_disconnect,
  180. };
  181. module_serio_driver(touchit213_drv);