gunze.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2000-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * Gunze AHL-51S touchscreen driver for Linux
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/input.h>
  13. #include <linux/serio.h>
  14. #define DRIVER_DESC "Gunze AHL-51S touchscreen driver"
  15. MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
  16. MODULE_DESCRIPTION(DRIVER_DESC);
  17. MODULE_LICENSE("GPL");
  18. /*
  19. * Definitions & global arrays.
  20. */
  21. #define GUNZE_MAX_LENGTH 10
  22. /*
  23. * Per-touchscreen data.
  24. */
  25. struct gunze {
  26. struct input_dev *dev;
  27. struct serio *serio;
  28. int idx;
  29. unsigned char data[GUNZE_MAX_LENGTH];
  30. char phys[32];
  31. };
  32. static void gunze_process_packet(struct gunze *gunze)
  33. {
  34. struct input_dev *dev = gunze->dev;
  35. if (gunze->idx != GUNZE_MAX_LENGTH || gunze->data[5] != ',' ||
  36. (gunze->data[0] != 'T' && gunze->data[0] != 'R')) {
  37. printk(KERN_WARNING "gunze.c: bad packet: >%.*s<\n", GUNZE_MAX_LENGTH, gunze->data);
  38. return;
  39. }
  40. input_report_abs(dev, ABS_X, simple_strtoul(gunze->data + 1, NULL, 10));
  41. input_report_abs(dev, ABS_Y, 1024 - simple_strtoul(gunze->data + 6, NULL, 10));
  42. input_report_key(dev, BTN_TOUCH, gunze->data[0] == 'T');
  43. input_sync(dev);
  44. }
  45. static irqreturn_t gunze_interrupt(struct serio *serio,
  46. unsigned char data, unsigned int flags)
  47. {
  48. struct gunze *gunze = serio_get_drvdata(serio);
  49. if (data == '\r') {
  50. gunze_process_packet(gunze);
  51. gunze->idx = 0;
  52. } else {
  53. if (gunze->idx < GUNZE_MAX_LENGTH)
  54. gunze->data[gunze->idx++] = data;
  55. }
  56. return IRQ_HANDLED;
  57. }
  58. /*
  59. * gunze_disconnect() is the opposite of gunze_connect()
  60. */
  61. static void gunze_disconnect(struct serio *serio)
  62. {
  63. struct gunze *gunze = serio_get_drvdata(serio);
  64. input_get_device(gunze->dev);
  65. input_unregister_device(gunze->dev);
  66. serio_close(serio);
  67. serio_set_drvdata(serio, NULL);
  68. input_put_device(gunze->dev);
  69. kfree(gunze);
  70. }
  71. /*
  72. * gunze_connect() is the routine that is called when someone adds a
  73. * new serio device that supports Gunze protocol and registers it as
  74. * an input device.
  75. */
  76. static int gunze_connect(struct serio *serio, struct serio_driver *drv)
  77. {
  78. struct gunze *gunze;
  79. struct input_dev *input_dev;
  80. int err;
  81. gunze = kzalloc(sizeof(struct gunze), GFP_KERNEL);
  82. input_dev = input_allocate_device();
  83. if (!gunze || !input_dev) {
  84. err = -ENOMEM;
  85. goto fail1;
  86. }
  87. gunze->serio = serio;
  88. gunze->dev = input_dev;
  89. snprintf(gunze->phys, sizeof(serio->phys), "%s/input0", serio->phys);
  90. input_dev->name = "Gunze AHL-51S TouchScreen";
  91. input_dev->phys = gunze->phys;
  92. input_dev->id.bustype = BUS_RS232;
  93. input_dev->id.vendor = SERIO_GUNZE;
  94. input_dev->id.product = 0x0051;
  95. input_dev->id.version = 0x0100;
  96. input_dev->dev.parent = &serio->dev;
  97. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  98. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  99. input_set_abs_params(input_dev, ABS_X, 24, 1000, 0, 0);
  100. input_set_abs_params(input_dev, ABS_Y, 24, 1000, 0, 0);
  101. serio_set_drvdata(serio, gunze);
  102. err = serio_open(serio, drv);
  103. if (err)
  104. goto fail2;
  105. err = input_register_device(gunze->dev);
  106. if (err)
  107. goto fail3;
  108. return 0;
  109. fail3: serio_close(serio);
  110. fail2: serio_set_drvdata(serio, NULL);
  111. fail1: input_free_device(input_dev);
  112. kfree(gunze);
  113. return err;
  114. }
  115. /*
  116. * The serio driver structure.
  117. */
  118. static const struct serio_device_id gunze_serio_ids[] = {
  119. {
  120. .type = SERIO_RS232,
  121. .proto = SERIO_GUNZE,
  122. .id = SERIO_ANY,
  123. .extra = SERIO_ANY,
  124. },
  125. { 0 }
  126. };
  127. MODULE_DEVICE_TABLE(serio, gunze_serio_ids);
  128. static struct serio_driver gunze_drv = {
  129. .driver = {
  130. .name = "gunze",
  131. },
  132. .description = DRIVER_DESC,
  133. .id_table = gunze_serio_ids,
  134. .interrupt = gunze_interrupt,
  135. .connect = gunze_connect,
  136. .disconnect = gunze_disconnect,
  137. };
  138. module_serio_driver(gunze_drv);