inexio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * iNexio serial touchscreen driver
  4. *
  5. * Copyright (c) 2008 Richard Lemon
  6. * Based on the mtouch driver (c) Vojtech Pavlik and Dan Streetman
  7. */
  8. /*
  9. * 2008/06/19 Richard Lemon <[email protected]>
  10. * Copied mtouch.c and edited for iNexio 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 "iNexio serial touchscreen driver"
  19. MODULE_AUTHOR("Richard Lemon <[email protected]>");
  20. MODULE_DESCRIPTION(DRIVER_DESC);
  21. MODULE_LICENSE("GPL");
  22. /*
  23. * Definitions & global arrays.
  24. */
  25. #define INEXIO_FORMAT_TOUCH_BIT 0x01
  26. #define INEXIO_FORMAT_LENGTH 5
  27. #define INEXIO_RESPONSE_BEGIN_BYTE 0x80
  28. /* todo: check specs for max length of all responses */
  29. #define INEXIO_MAX_LENGTH 16
  30. #define INEXIO_MIN_XC 0
  31. #define INEXIO_MAX_XC 0x3fff
  32. #define INEXIO_MIN_YC 0
  33. #define INEXIO_MAX_YC 0x3fff
  34. #define INEXIO_GET_XC(data) (((data[1])<<7) | data[2])
  35. #define INEXIO_GET_YC(data) (((data[3])<<7) | data[4])
  36. #define INEXIO_GET_TOUCHED(data) (INEXIO_FORMAT_TOUCH_BIT & data[0])
  37. /*
  38. * Per-touchscreen data.
  39. */
  40. struct inexio {
  41. struct input_dev *dev;
  42. struct serio *serio;
  43. int idx;
  44. unsigned char data[INEXIO_MAX_LENGTH];
  45. char phys[32];
  46. };
  47. static void inexio_process_data(struct inexio *pinexio)
  48. {
  49. struct input_dev *dev = pinexio->dev;
  50. if (INEXIO_FORMAT_LENGTH == ++pinexio->idx) {
  51. input_report_abs(dev, ABS_X, INEXIO_GET_XC(pinexio->data));
  52. input_report_abs(dev, ABS_Y, INEXIO_GET_YC(pinexio->data));
  53. input_report_key(dev, BTN_TOUCH, INEXIO_GET_TOUCHED(pinexio->data));
  54. input_sync(dev);
  55. pinexio->idx = 0;
  56. }
  57. }
  58. static irqreturn_t inexio_interrupt(struct serio *serio,
  59. unsigned char data, unsigned int flags)
  60. {
  61. struct inexio *pinexio = serio_get_drvdata(serio);
  62. pinexio->data[pinexio->idx] = data;
  63. if (INEXIO_RESPONSE_BEGIN_BYTE&pinexio->data[0])
  64. inexio_process_data(pinexio);
  65. else
  66. printk(KERN_DEBUG "inexio.c: unknown/unsynchronized data from device, byte %x\n",pinexio->data[0]);
  67. return IRQ_HANDLED;
  68. }
  69. /*
  70. * inexio_disconnect() is the opposite of inexio_connect()
  71. */
  72. static void inexio_disconnect(struct serio *serio)
  73. {
  74. struct inexio *pinexio = serio_get_drvdata(serio);
  75. input_get_device(pinexio->dev);
  76. input_unregister_device(pinexio->dev);
  77. serio_close(serio);
  78. serio_set_drvdata(serio, NULL);
  79. input_put_device(pinexio->dev);
  80. kfree(pinexio);
  81. }
  82. /*
  83. * inexio_connect() is the routine that is called when someone adds a
  84. * new serio device that supports iNexio protocol and registers it as
  85. * an input device. This is usually accomplished using inputattach.
  86. */
  87. static int inexio_connect(struct serio *serio, struct serio_driver *drv)
  88. {
  89. struct inexio *pinexio;
  90. struct input_dev *input_dev;
  91. int err;
  92. pinexio = kzalloc(sizeof(struct inexio), GFP_KERNEL);
  93. input_dev = input_allocate_device();
  94. if (!pinexio || !input_dev) {
  95. err = -ENOMEM;
  96. goto fail1;
  97. }
  98. pinexio->serio = serio;
  99. pinexio->dev = input_dev;
  100. snprintf(pinexio->phys, sizeof(pinexio->phys), "%s/input0", serio->phys);
  101. input_dev->name = "iNexio Serial TouchScreen";
  102. input_dev->phys = pinexio->phys;
  103. input_dev->id.bustype = BUS_RS232;
  104. input_dev->id.vendor = SERIO_INEXIO;
  105. input_dev->id.product = 0;
  106. input_dev->id.version = 0x0001;
  107. input_dev->dev.parent = &serio->dev;
  108. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  109. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  110. input_set_abs_params(pinexio->dev, ABS_X, INEXIO_MIN_XC, INEXIO_MAX_XC, 0, 0);
  111. input_set_abs_params(pinexio->dev, ABS_Y, INEXIO_MIN_YC, INEXIO_MAX_YC, 0, 0);
  112. serio_set_drvdata(serio, pinexio);
  113. err = serio_open(serio, drv);
  114. if (err)
  115. goto fail2;
  116. err = input_register_device(pinexio->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(pinexio);
  124. return err;
  125. }
  126. /*
  127. * The serio driver structure.
  128. */
  129. static const struct serio_device_id inexio_serio_ids[] = {
  130. {
  131. .type = SERIO_RS232,
  132. .proto = SERIO_INEXIO,
  133. .id = SERIO_ANY,
  134. .extra = SERIO_ANY,
  135. },
  136. { 0 }
  137. };
  138. MODULE_DEVICE_TABLE(serio, inexio_serio_ids);
  139. static struct serio_driver inexio_drv = {
  140. .driver = {
  141. .name = "inexio",
  142. },
  143. .description = DRIVER_DESC,
  144. .id_table = inexio_serio_ids,
  145. .interrupt = inexio_interrupt,
  146. .connect = inexio_connect,
  147. .disconnect = inexio_disconnect,
  148. };
  149. module_serio_driver(inexio_drv);