egalax_ts_serial.c 4.5 KB

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