hampshire.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Hampshire serial touchscreen driver
  4. *
  5. * Copyright (c) 2010 Adam Bennett
  6. * Based on the dynapro driver (c) Tias Guns
  7. */
  8. /*
  9. * 2010/04/08 Adam Bennett <[email protected]>
  10. * Copied dynapro.c and edited for Hampshire 4-byte protocol
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/input.h>
  17. #include <linux/serio.h>
  18. #define DRIVER_DESC "Hampshire serial touchscreen driver"
  19. MODULE_AUTHOR("Adam Bennett <[email protected]>");
  20. MODULE_DESCRIPTION(DRIVER_DESC);
  21. MODULE_LICENSE("GPL");
  22. /*
  23. * Definitions & global arrays.
  24. */
  25. #define HAMPSHIRE_FORMAT_TOUCH_BIT 0x40
  26. #define HAMPSHIRE_FORMAT_LENGTH 4
  27. #define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x80
  28. #define HAMPSHIRE_MIN_XC 0
  29. #define HAMPSHIRE_MAX_XC 0x1000
  30. #define HAMPSHIRE_MIN_YC 0
  31. #define HAMPSHIRE_MAX_YC 0x1000
  32. #define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))
  33. #define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))
  34. #define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])
  35. /*
  36. * Per-touchscreen data.
  37. */
  38. struct hampshire {
  39. struct input_dev *dev;
  40. struct serio *serio;
  41. int idx;
  42. unsigned char data[HAMPSHIRE_FORMAT_LENGTH];
  43. char phys[32];
  44. };
  45. static void hampshire_process_data(struct hampshire *phampshire)
  46. {
  47. struct input_dev *dev = phampshire->dev;
  48. if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {
  49. input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));
  50. input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));
  51. input_report_key(dev, BTN_TOUCH,
  52. HAMPSHIRE_GET_TOUCHED(phampshire->data));
  53. input_sync(dev);
  54. phampshire->idx = 0;
  55. }
  56. }
  57. static irqreturn_t hampshire_interrupt(struct serio *serio,
  58. unsigned char data, unsigned int flags)
  59. {
  60. struct hampshire *phampshire = serio_get_drvdata(serio);
  61. phampshire->data[phampshire->idx] = data;
  62. if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])
  63. hampshire_process_data(phampshire);
  64. else
  65. dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
  66. phampshire->data[0]);
  67. return IRQ_HANDLED;
  68. }
  69. static void hampshire_disconnect(struct serio *serio)
  70. {
  71. struct hampshire *phampshire = serio_get_drvdata(serio);
  72. input_get_device(phampshire->dev);
  73. input_unregister_device(phampshire->dev);
  74. serio_close(serio);
  75. serio_set_drvdata(serio, NULL);
  76. input_put_device(phampshire->dev);
  77. kfree(phampshire);
  78. }
  79. /*
  80. * hampshire_connect() is the routine that is called when someone adds a
  81. * new serio device that supports hampshire protocol and registers it as
  82. * an input device. This is usually accomplished using inputattach.
  83. */
  84. static int hampshire_connect(struct serio *serio, struct serio_driver *drv)
  85. {
  86. struct hampshire *phampshire;
  87. struct input_dev *input_dev;
  88. int err;
  89. phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);
  90. input_dev = input_allocate_device();
  91. if (!phampshire || !input_dev) {
  92. err = -ENOMEM;
  93. goto fail1;
  94. }
  95. phampshire->serio = serio;
  96. phampshire->dev = input_dev;
  97. snprintf(phampshire->phys, sizeof(phampshire->phys),
  98. "%s/input0", serio->phys);
  99. input_dev->name = "Hampshire Serial TouchScreen";
  100. input_dev->phys = phampshire->phys;
  101. input_dev->id.bustype = BUS_RS232;
  102. input_dev->id.vendor = SERIO_HAMPSHIRE;
  103. input_dev->id.product = 0;
  104. input_dev->id.version = 0x0001;
  105. input_dev->dev.parent = &serio->dev;
  106. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  107. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  108. input_set_abs_params(phampshire->dev, ABS_X,
  109. HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);
  110. input_set_abs_params(phampshire->dev, ABS_Y,
  111. HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);
  112. serio_set_drvdata(serio, phampshire);
  113. err = serio_open(serio, drv);
  114. if (err)
  115. goto fail2;
  116. err = input_register_device(phampshire->dev);
  117. if (err)
  118. goto fail3;
  119. return 0;
  120. fail3: serio_close(serio);
  121. fail2: serio_set_drvdata(serio, NULL);
  122. fail1: input_free_device(input_dev);
  123. kfree(phampshire);
  124. return err;
  125. }
  126. /*
  127. * The serio driver structure.
  128. */
  129. static const struct serio_device_id hampshire_serio_ids[] = {
  130. {
  131. .type = SERIO_RS232,
  132. .proto = SERIO_HAMPSHIRE,
  133. .id = SERIO_ANY,
  134. .extra = SERIO_ANY,
  135. },
  136. { 0 }
  137. };
  138. MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);
  139. static struct serio_driver hampshire_drv = {
  140. .driver = {
  141. .name = "hampshire",
  142. },
  143. .description = DRIVER_DESC,
  144. .id_table = hampshire_serio_ids,
  145. .interrupt = hampshire_interrupt,
  146. .connect = hampshire_connect,
  147. .disconnect = hampshire_disconnect,
  148. };
  149. module_serio_driver(hampshire_drv);