touchwin.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Touchwindow serial touchscreen driver
  4. *
  5. * Copyright (c) 2006 Rick Koch <[email protected]>
  6. *
  7. * Based on MicroTouch driver (drivers/input/touchscreen/mtouch.c)
  8. * Copyright (c) 2004 Vojtech Pavlik
  9. * and Dan Streetman <[email protected]>
  10. */
  11. /*
  12. * 2005/02/19 Rick Koch:
  13. * The Touchwindow I used is made by Edmark Corp. and
  14. * constantly outputs a stream of 0's unless it is touched.
  15. * It then outputs 3 bytes: X, Y, and a copy of Y.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/input.h>
  22. #include <linux/serio.h>
  23. #define DRIVER_DESC "Touchwindow serial touchscreen driver"
  24. MODULE_AUTHOR("Rick Koch <[email protected]>");
  25. MODULE_DESCRIPTION(DRIVER_DESC);
  26. MODULE_LICENSE("GPL");
  27. /*
  28. * Definitions & global arrays.
  29. */
  30. #define TW_LENGTH 3
  31. #define TW_MIN_XC 0
  32. #define TW_MAX_XC 0xff
  33. #define TW_MIN_YC 0
  34. #define TW_MAX_YC 0xff
  35. /*
  36. * Per-touchscreen data.
  37. */
  38. struct tw {
  39. struct input_dev *dev;
  40. struct serio *serio;
  41. int idx;
  42. int touched;
  43. unsigned char data[TW_LENGTH];
  44. char phys[32];
  45. };
  46. static irqreturn_t tw_interrupt(struct serio *serio,
  47. unsigned char data, unsigned int flags)
  48. {
  49. struct tw *tw = serio_get_drvdata(serio);
  50. struct input_dev *dev = tw->dev;
  51. if (data) { /* touch */
  52. tw->touched = 1;
  53. tw->data[tw->idx++] = data;
  54. /* verify length and that the two Y's are the same */
  55. if (tw->idx == TW_LENGTH && tw->data[1] == tw->data[2]) {
  56. input_report_abs(dev, ABS_X, tw->data[0]);
  57. input_report_abs(dev, ABS_Y, tw->data[1]);
  58. input_report_key(dev, BTN_TOUCH, 1);
  59. input_sync(dev);
  60. tw->idx = 0;
  61. }
  62. } else if (tw->touched) { /* untouch */
  63. input_report_key(dev, BTN_TOUCH, 0);
  64. input_sync(dev);
  65. tw->idx = 0;
  66. tw->touched = 0;
  67. }
  68. return IRQ_HANDLED;
  69. }
  70. /*
  71. * tw_disconnect() is the opposite of tw_connect()
  72. */
  73. static void tw_disconnect(struct serio *serio)
  74. {
  75. struct tw *tw = serio_get_drvdata(serio);
  76. input_get_device(tw->dev);
  77. input_unregister_device(tw->dev);
  78. serio_close(serio);
  79. serio_set_drvdata(serio, NULL);
  80. input_put_device(tw->dev);
  81. kfree(tw);
  82. }
  83. /*
  84. * tw_connect() is the routine that is called when someone adds a
  85. * new serio device that supports the Touchwin protocol and registers it as
  86. * an input device.
  87. */
  88. static int tw_connect(struct serio *serio, struct serio_driver *drv)
  89. {
  90. struct tw *tw;
  91. struct input_dev *input_dev;
  92. int err;
  93. tw = kzalloc(sizeof(struct tw), GFP_KERNEL);
  94. input_dev = input_allocate_device();
  95. if (!tw || !input_dev) {
  96. err = -ENOMEM;
  97. goto fail1;
  98. }
  99. tw->serio = serio;
  100. tw->dev = input_dev;
  101. snprintf(tw->phys, sizeof(tw->phys), "%s/input0", serio->phys);
  102. input_dev->name = "Touchwindow Serial TouchScreen";
  103. input_dev->phys = tw->phys;
  104. input_dev->id.bustype = BUS_RS232;
  105. input_dev->id.vendor = SERIO_TOUCHWIN;
  106. input_dev->id.product = 0;
  107. input_dev->id.version = 0x0100;
  108. input_dev->dev.parent = &serio->dev;
  109. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  110. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  111. input_set_abs_params(tw->dev, ABS_X, TW_MIN_XC, TW_MAX_XC, 0, 0);
  112. input_set_abs_params(tw->dev, ABS_Y, TW_MIN_YC, TW_MAX_YC, 0, 0);
  113. serio_set_drvdata(serio, tw);
  114. err = serio_open(serio, drv);
  115. if (err)
  116. goto fail2;
  117. err = input_register_device(tw->dev);
  118. if (err)
  119. goto fail3;
  120. return 0;
  121. fail3: serio_close(serio);
  122. fail2: serio_set_drvdata(serio, NULL);
  123. fail1: input_free_device(input_dev);
  124. kfree(tw);
  125. return err;
  126. }
  127. /*
  128. * The serio driver structure.
  129. */
  130. static const struct serio_device_id tw_serio_ids[] = {
  131. {
  132. .type = SERIO_RS232,
  133. .proto = SERIO_TOUCHWIN,
  134. .id = SERIO_ANY,
  135. .extra = SERIO_ANY,
  136. },
  137. { 0 }
  138. };
  139. MODULE_DEVICE_TABLE(serio, tw_serio_ids);
  140. static struct serio_driver tw_drv = {
  141. .driver = {
  142. .name = "touchwin",
  143. },
  144. .description = DRIVER_DESC,
  145. .id_table = tw_serio_ids,
  146. .interrupt = tw_interrupt,
  147. .connect = tw_connect,
  148. .disconnect = tw_disconnect,
  149. };
  150. module_serio_driver(tw_drv);