egalax_ts_serial.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <[email protected]>
  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(sizeof(struct egalax), GFP_KERNEL);
  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. snprintf(egalax->phys, sizeof(egalax->phys),
  94. "%s/input0", serio->phys);
  95. input_dev->name = "EETI eGalaxTouch Serial TouchScreen";
  96. input_dev->phys = egalax->phys;
  97. input_dev->id.bustype = BUS_RS232;
  98. input_dev->id.vendor = SERIO_EGALAX;
  99. input_dev->id.product = 0;
  100. input_dev->id.version = 0x0001;
  101. input_dev->dev.parent = &serio->dev;
  102. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  103. input_set_abs_params(input_dev, ABS_X,
  104. EGALAX_MIN_XC, EGALAX_MAX_XC, 0, 0);
  105. input_set_abs_params(input_dev, ABS_Y,
  106. EGALAX_MIN_YC, EGALAX_MAX_YC, 0, 0);
  107. serio_set_drvdata(serio, egalax);
  108. error = serio_open(serio, drv);
  109. if (error)
  110. goto err_reset_drvdata;
  111. error = input_register_device(input_dev);
  112. if (error)
  113. goto err_close_serio;
  114. return 0;
  115. err_close_serio:
  116. serio_close(serio);
  117. err_reset_drvdata:
  118. serio_set_drvdata(serio, NULL);
  119. err_free_mem:
  120. input_free_device(input_dev);
  121. kfree(egalax);
  122. return error;
  123. }
  124. static void egalax_disconnect(struct serio *serio)
  125. {
  126. struct egalax *egalax = serio_get_drvdata(serio);
  127. serio_close(serio);
  128. serio_set_drvdata(serio, NULL);
  129. input_unregister_device(egalax->input);
  130. kfree(egalax);
  131. }
  132. /*
  133. * The serio driver structure.
  134. */
  135. static const struct serio_device_id egalax_serio_ids[] = {
  136. {
  137. .type = SERIO_RS232,
  138. .proto = SERIO_EGALAX,
  139. .id = SERIO_ANY,
  140. .extra = SERIO_ANY,
  141. },
  142. { 0 }
  143. };
  144. MODULE_DEVICE_TABLE(serio, egalax_serio_ids);
  145. static struct serio_driver egalax_drv = {
  146. .driver = {
  147. .name = "egalax",
  148. },
  149. .description = DRIVER_DESC,
  150. .id_table = egalax_serio_ids,
  151. .interrupt = egalax_interrupt,
  152. .connect = egalax_connect,
  153. .disconnect = egalax_disconnect,
  154. };
  155. module_serio_driver(egalax_drv);
  156. MODULE_AUTHOR("Zoltán Böszörményi <[email protected]>");
  157. MODULE_DESCRIPTION(DRIVER_DESC);
  158. MODULE_LICENSE("GPL v2");