touchright.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Touchright 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. #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 "Touchright serial touchscreen driver"
  18. MODULE_AUTHOR("Rick Koch <[email protected]>");
  19. MODULE_DESCRIPTION(DRIVER_DESC);
  20. MODULE_LICENSE("GPL");
  21. /*
  22. * Definitions & global arrays.
  23. */
  24. #define TR_FORMAT_TOUCH_BIT 0x01
  25. #define TR_FORMAT_STATUS_BYTE 0x40
  26. #define TR_FORMAT_STATUS_MASK ~TR_FORMAT_TOUCH_BIT
  27. #define TR_LENGTH 5
  28. #define TR_MIN_XC 0
  29. #define TR_MAX_XC 0x1ff
  30. #define TR_MIN_YC 0
  31. #define TR_MAX_YC 0x1ff
  32. /*
  33. * Per-touchscreen data.
  34. */
  35. struct tr {
  36. struct input_dev *dev;
  37. struct serio *serio;
  38. int idx;
  39. unsigned char data[TR_LENGTH];
  40. char phys[32];
  41. };
  42. static irqreturn_t tr_interrupt(struct serio *serio,
  43. unsigned char data, unsigned int flags)
  44. {
  45. struct tr *tr = serio_get_drvdata(serio);
  46. struct input_dev *dev = tr->dev;
  47. tr->data[tr->idx] = data;
  48. if ((tr->data[0] & TR_FORMAT_STATUS_MASK) == TR_FORMAT_STATUS_BYTE) {
  49. if (++tr->idx == TR_LENGTH) {
  50. input_report_abs(dev, ABS_X,
  51. (tr->data[1] << 5) | (tr->data[2] >> 1));
  52. input_report_abs(dev, ABS_Y,
  53. (tr->data[3] << 5) | (tr->data[4] >> 1));
  54. input_report_key(dev, BTN_TOUCH,
  55. tr->data[0] & TR_FORMAT_TOUCH_BIT);
  56. input_sync(dev);
  57. tr->idx = 0;
  58. }
  59. }
  60. return IRQ_HANDLED;
  61. }
  62. /*
  63. * tr_disconnect() is the opposite of tr_connect()
  64. */
  65. static void tr_disconnect(struct serio *serio)
  66. {
  67. struct tr *tr = serio_get_drvdata(serio);
  68. input_get_device(tr->dev);
  69. input_unregister_device(tr->dev);
  70. serio_close(serio);
  71. serio_set_drvdata(serio, NULL);
  72. input_put_device(tr->dev);
  73. kfree(tr);
  74. }
  75. /*
  76. * tr_connect() is the routine that is called when someone adds a
  77. * new serio device that supports the Touchright protocol and registers it as
  78. * an input device.
  79. */
  80. static int tr_connect(struct serio *serio, struct serio_driver *drv)
  81. {
  82. struct tr *tr;
  83. struct input_dev *input_dev;
  84. int err;
  85. tr = kzalloc(sizeof(struct tr), GFP_KERNEL);
  86. input_dev = input_allocate_device();
  87. if (!tr || !input_dev) {
  88. err = -ENOMEM;
  89. goto fail1;
  90. }
  91. tr->serio = serio;
  92. tr->dev = input_dev;
  93. snprintf(tr->phys, sizeof(tr->phys), "%s/input0", serio->phys);
  94. input_dev->name = "Touchright Serial TouchScreen";
  95. input_dev->phys = tr->phys;
  96. input_dev->id.bustype = BUS_RS232;
  97. input_dev->id.vendor = SERIO_TOUCHRIGHT;
  98. input_dev->id.product = 0;
  99. input_dev->id.version = 0x0100;
  100. input_dev->dev.parent = &serio->dev;
  101. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  102. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  103. input_set_abs_params(tr->dev, ABS_X, TR_MIN_XC, TR_MAX_XC, 0, 0);
  104. input_set_abs_params(tr->dev, ABS_Y, TR_MIN_YC, TR_MAX_YC, 0, 0);
  105. serio_set_drvdata(serio, tr);
  106. err = serio_open(serio, drv);
  107. if (err)
  108. goto fail2;
  109. err = input_register_device(tr->dev);
  110. if (err)
  111. goto fail3;
  112. return 0;
  113. fail3: serio_close(serio);
  114. fail2: serio_set_drvdata(serio, NULL);
  115. fail1: input_free_device(input_dev);
  116. kfree(tr);
  117. return err;
  118. }
  119. /*
  120. * The serio driver structure.
  121. */
  122. static const struct serio_device_id tr_serio_ids[] = {
  123. {
  124. .type = SERIO_RS232,
  125. .proto = SERIO_TOUCHRIGHT,
  126. .id = SERIO_ANY,
  127. .extra = SERIO_ANY,
  128. },
  129. { 0 }
  130. };
  131. MODULE_DEVICE_TABLE(serio, tr_serio_ids);
  132. static struct serio_driver tr_drv = {
  133. .driver = {
  134. .name = "touchright",
  135. },
  136. .description = DRIVER_DESC,
  137. .id_table = tr_serio_ids,
  138. .interrupt = tr_interrupt,
  139. .connect = tr_connect,
  140. .disconnect = tr_disconnect,
  141. };
  142. module_serio_driver(tr_drv);