mtouch.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MicroTouch (3M) serial touchscreen driver
  4. *
  5. * Copyright (c) 2004 Vojtech Pavlik
  6. */
  7. /*
  8. * 2005/02/19 Dan Streetman <[email protected]>
  9. * Copied elo.c and edited for MicroTouch protocol
  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 "MicroTouch serial touchscreen driver"
  18. MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
  19. MODULE_DESCRIPTION(DRIVER_DESC);
  20. MODULE_LICENSE("GPL");
  21. /*
  22. * Definitions & global arrays.
  23. */
  24. #define MTOUCH_FORMAT_TABLET_STATUS_BIT 0x80
  25. #define MTOUCH_FORMAT_TABLET_TOUCH_BIT 0x40
  26. #define MTOUCH_FORMAT_TABLET_LENGTH 5
  27. #define MTOUCH_RESPONSE_BEGIN_BYTE 0x01
  28. #define MTOUCH_RESPONSE_END_BYTE 0x0d
  29. /* todo: check specs for max length of all responses */
  30. #define MTOUCH_MAX_LENGTH 16
  31. #define MTOUCH_MIN_XC 0
  32. #define MTOUCH_MAX_XC 0x3fff
  33. #define MTOUCH_MIN_YC 0
  34. #define MTOUCH_MAX_YC 0x3fff
  35. #define MTOUCH_GET_XC(data) (((data[2])<<7) | data[1])
  36. #define MTOUCH_GET_YC(data) (((data[4])<<7) | data[3])
  37. #define MTOUCH_GET_TOUCHED(data) (MTOUCH_FORMAT_TABLET_TOUCH_BIT & data[0])
  38. /*
  39. * Per-touchscreen data.
  40. */
  41. struct mtouch {
  42. struct input_dev *dev;
  43. struct serio *serio;
  44. int idx;
  45. unsigned char data[MTOUCH_MAX_LENGTH];
  46. char phys[32];
  47. };
  48. static void mtouch_process_format_tablet(struct mtouch *mtouch)
  49. {
  50. struct input_dev *dev = mtouch->dev;
  51. if (MTOUCH_FORMAT_TABLET_LENGTH == ++mtouch->idx) {
  52. input_report_abs(dev, ABS_X, MTOUCH_GET_XC(mtouch->data));
  53. input_report_abs(dev, ABS_Y, MTOUCH_MAX_YC - MTOUCH_GET_YC(mtouch->data));
  54. input_report_key(dev, BTN_TOUCH, MTOUCH_GET_TOUCHED(mtouch->data));
  55. input_sync(dev);
  56. mtouch->idx = 0;
  57. }
  58. }
  59. static void mtouch_process_response(struct mtouch *mtouch)
  60. {
  61. if (MTOUCH_RESPONSE_END_BYTE == mtouch->data[mtouch->idx++]) {
  62. /* FIXME - process response */
  63. mtouch->idx = 0;
  64. } else if (MTOUCH_MAX_LENGTH == mtouch->idx) {
  65. printk(KERN_ERR "mtouch.c: too many response bytes\n");
  66. mtouch->idx = 0;
  67. }
  68. }
  69. static irqreturn_t mtouch_interrupt(struct serio *serio,
  70. unsigned char data, unsigned int flags)
  71. {
  72. struct mtouch *mtouch = serio_get_drvdata(serio);
  73. mtouch->data[mtouch->idx] = data;
  74. if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])
  75. mtouch_process_format_tablet(mtouch);
  76. else if (MTOUCH_RESPONSE_BEGIN_BYTE == mtouch->data[0])
  77. mtouch_process_response(mtouch);
  78. else
  79. printk(KERN_DEBUG "mtouch.c: unknown/unsynchronized data from device, byte %x\n",mtouch->data[0]);
  80. return IRQ_HANDLED;
  81. }
  82. /*
  83. * mtouch_disconnect() is the opposite of mtouch_connect()
  84. */
  85. static void mtouch_disconnect(struct serio *serio)
  86. {
  87. struct mtouch *mtouch = serio_get_drvdata(serio);
  88. input_get_device(mtouch->dev);
  89. input_unregister_device(mtouch->dev);
  90. serio_close(serio);
  91. serio_set_drvdata(serio, NULL);
  92. input_put_device(mtouch->dev);
  93. kfree(mtouch);
  94. }
  95. /*
  96. * mtouch_connect() is the routine that is called when someone adds a
  97. * new serio device that supports MicroTouch (Format Tablet) protocol and registers it as
  98. * an input device.
  99. */
  100. static int mtouch_connect(struct serio *serio, struct serio_driver *drv)
  101. {
  102. struct mtouch *mtouch;
  103. struct input_dev *input_dev;
  104. int err;
  105. mtouch = kzalloc(sizeof(struct mtouch), GFP_KERNEL);
  106. input_dev = input_allocate_device();
  107. if (!mtouch || !input_dev) {
  108. err = -ENOMEM;
  109. goto fail1;
  110. }
  111. mtouch->serio = serio;
  112. mtouch->dev = input_dev;
  113. snprintf(mtouch->phys, sizeof(mtouch->phys), "%s/input0", serio->phys);
  114. input_dev->name = "MicroTouch Serial TouchScreen";
  115. input_dev->phys = mtouch->phys;
  116. input_dev->id.bustype = BUS_RS232;
  117. input_dev->id.vendor = SERIO_MICROTOUCH;
  118. input_dev->id.product = 0;
  119. input_dev->id.version = 0x0100;
  120. input_dev->dev.parent = &serio->dev;
  121. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  122. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  123. input_set_abs_params(mtouch->dev, ABS_X, MTOUCH_MIN_XC, MTOUCH_MAX_XC, 0, 0);
  124. input_set_abs_params(mtouch->dev, ABS_Y, MTOUCH_MIN_YC, MTOUCH_MAX_YC, 0, 0);
  125. serio_set_drvdata(serio, mtouch);
  126. err = serio_open(serio, drv);
  127. if (err)
  128. goto fail2;
  129. err = input_register_device(mtouch->dev);
  130. if (err)
  131. goto fail3;
  132. return 0;
  133. fail3: serio_close(serio);
  134. fail2: serio_set_drvdata(serio, NULL);
  135. fail1: input_free_device(input_dev);
  136. kfree(mtouch);
  137. return err;
  138. }
  139. /*
  140. * The serio driver structure.
  141. */
  142. static const struct serio_device_id mtouch_serio_ids[] = {
  143. {
  144. .type = SERIO_RS232,
  145. .proto = SERIO_MICROTOUCH,
  146. .id = SERIO_ANY,
  147. .extra = SERIO_ANY,
  148. },
  149. { 0 }
  150. };
  151. MODULE_DEVICE_TABLE(serio, mtouch_serio_ids);
  152. static struct serio_driver mtouch_drv = {
  153. .driver = {
  154. .name = "mtouch",
  155. },
  156. .description = DRIVER_DESC,
  157. .id_table = mtouch_serio_ids,
  158. .interrupt = mtouch_interrupt,
  159. .connect = mtouch_connect,
  160. .disconnect = mtouch_disconnect,
  161. };
  162. module_serio_driver(mtouch_drv);