egalax_ts_serial.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * EETI Egalax serial touchscreen driver
  4. *
  5. * Copyright (c) 2015 Zoltán Böszörményi <zboszor@pr.hu>
  6. *
  7. * based on the
  8. *
  9. * Hampshire serial touchscreen driver (Copyright (c) 2010 Adam Bennett)
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/input.h>
  16. #include <linux/serio.h>
  17. #define DRIVER_DESC "EETI Egalax serial touchscreen driver"
  18. /*
  19. * Definitions & global arrays.
  20. */
  21. #define EGALAX_FORMAT_MAX_LENGTH 6
  22. #define EGALAX_FORMAT_START_BIT BIT(7)
  23. #define EGALAX_FORMAT_PRESSURE_BIT BIT(6)
  24. #define EGALAX_FORMAT_TOUCH_BIT BIT(0)
  25. #define EGALAX_FORMAT_RESOLUTION_MASK 0x06
  26. #define EGALAX_MIN_XC 0
  27. #define EGALAX_MAX_XC 0x4000
  28. #define EGALAX_MIN_YC 0
  29. #define EGALAX_MAX_YC 0x4000
  30. /*
  31. * Per-touchscreen data.
  32. */
  33. struct egalax {
  34. struct input_dev *input;
  35. struct serio *serio;
  36. int idx;
  37. u8 data[EGALAX_FORMAT_MAX_LENGTH];
  38. char phys[32];
  39. };
  40. static void egalax_process_data(struct egalax *egalax)
  41. {
  42. struct input_dev *dev = egalax->input;
  43. u8 *data = egalax->data;
  44. u16 x, y;
  45. u8 shift;
  46. u8 mask;
  47. shift = 3 - ((data[0] & EGALAX_FORMAT_RESOLUTION_MASK) >> 1);
  48. mask = 0xff >> (shift + 1);
  49. x = (((u16)(data[1] & mask) << 7) | (data[2] & 0x7f)) << shift;
  50. y = (((u16)(data[3] & mask) << 7) | (data[4] & 0x7f)) << shift;
  51. input_report_key(dev, BTN_TOUCH, data[0] & EGALAX_FORMAT_TOUCH_BIT);
  52. input_report_abs(dev, ABS_X, x);
  53. input_report_abs(dev, ABS_Y, y);
  54. input_sync(dev);
  55. }
  56. static irqreturn_t egalax_interrupt(struct serio *serio,
  57. unsigned char data, unsigned int flags)
  58. {
  59. struct egalax *egalax = serio_get_drvdata(serio);
  60. int pkt_len;
  61. egalax->data[egalax->idx++] = data;
  62. if (likely(egalax->data[0] & EGALAX_FORMAT_START_BIT)) {
  63. pkt_len = egalax->data[0] & EGALAX_FORMAT_PRESSURE_BIT ? 6 : 5;
  64. if (pkt_len == egalax->idx) {
  65. egalax_process_data(egalax);
  66. egalax->idx = 0;
  67. }
  68. } else {
  69. dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
  70. egalax->data[0]);
  71. egalax->idx = 0;
  72. }
  73. return IRQ_HANDLED;
  74. }
  75. /*
  76. * egalax_connect() is the routine that is called when someone adds a
  77. * new serio device that supports egalax protocol and registers it as
  78. * an input device. This is usually accomplished using inputattach.
  79. */
  80. static int egalax_connect(struct serio *serio, struct serio_driver *drv)
  81. {
  82. struct egalax *egalax;
  83. struct input_dev *input_dev;
  84. int error;
  85. egalax = kzalloc_obj(*egalax);
  86. input_dev = input_allocate_device();
  87. if (!egalax || !input_dev) {
  88. error = -ENOMEM;
  89. goto err_free_mem;
  90. }
  91. egalax->serio = serio;
  92. egalax->input = input_dev;
  93. scnprintf(egalax->phys, sizeof(egalax->phys), "%s/input0", serio->phys);
  94. input_dev->name = "EETI eGalaxTouch Serial TouchScreen";
  95. input_dev->phys = egalax->phys;
  96. input_dev->id.bustype = BUS_RS232;
  97. input_dev->id.vendor = SERIO_EGALAX;
  98. input_dev->id.product = 0;
  99. input_dev->id.version = 0x0001;
  100. input_dev->dev.parent = &serio->dev;
  101. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  102. input_set_abs_params(input_dev, ABS_X,
  103. EGALAX_MIN_XC, EGALAX_MAX_XC, 0, 0);
  104. input_set_abs_params(input_dev, ABS_Y,
  105. EGALAX_MIN_YC, EGALAX_MAX_YC, 0, 0);
  106. serio_set_drvdata(serio, egalax);
  107. error = serio_open(serio, drv);
  108. if (error)
  109. goto err_reset_drvdata;
  110. error = input_register_device(input_dev);
  111. if (error)
  112. goto err_close_serio;
  113. return 0;
  114. err_close_serio:
  115. serio_close(serio);
  116. err_reset_drvdata:
  117. serio_set_drvdata(serio, NULL);
  118. err_free_mem:
  119. input_free_device(input_dev);
  120. kfree(egalax);
  121. return error;
  122. }
  123. static void egalax_disconnect(struct serio *serio)
  124. {
  125. struct egalax *egalax = serio_get_drvdata(serio);
  126. serio_close(serio);
  127. serio_set_drvdata(serio, NULL);
  128. input_unregister_device(egalax->input);
  129. kfree(egalax);
  130. }
  131. /*
  132. * The serio driver structure.
  133. */
  134. static const struct serio_device_id egalax_serio_ids[] = {
  135. {
  136. .type = SERIO_RS232,
  137. .proto = SERIO_EGALAX,
  138. .id = SERIO_ANY,
  139. .extra = SERIO_ANY,
  140. },
  141. { 0 }
  142. };
  143. MODULE_DEVICE_TABLE(serio, egalax_serio_ids);
  144. static struct serio_driver egalax_drv = {
  145. .driver = {
  146. .name = "egalax",
  147. },
  148. .description = DRIVER_DESC,
  149. .id_table = egalax_serio_ids,
  150. .interrupt = egalax_interrupt,
  151. .connect = egalax_connect,
  152. .disconnect = egalax_disconnect,
  153. };
  154. module_serio_driver(egalax_drv);
  155. MODULE_AUTHOR("Zoltán Böszörményi <zboszor@pr.hu>");
  156. MODULE_DESCRIPTION(DRIVER_DESC);
  157. MODULE_LICENSE("GPL v2");