gunze.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2000-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * Gunze AHL-51S touchscreen driver for Linux
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/input.h>
  27. #include <linux/serio.h>
  28. #define DRIVER_DESC "Gunze AHL-51S touchscreen driver"
  29. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  30. MODULE_DESCRIPTION(DRIVER_DESC);
  31. MODULE_LICENSE("GPL");
  32. /*
  33. * Definitions & global arrays.
  34. */
  35. #define GUNZE_MAX_LENGTH 10
  36. /*
  37. * Per-touchscreen data.
  38. */
  39. struct gunze {
  40. struct input_dev *dev;
  41. struct serio *serio;
  42. int idx;
  43. unsigned char data[GUNZE_MAX_LENGTH];
  44. char phys[32];
  45. };
  46. static void gunze_process_packet(struct gunze* gunze)
  47. {
  48. struct input_dev *dev = gunze->dev;
  49. if (gunze->idx != GUNZE_MAX_LENGTH || gunze->data[5] != ',' ||
  50. (gunze->data[0] != 'T' && gunze->data[0] != 'R')) {
  51. printk(KERN_WARNING "gunze.c: bad packet: >%.*s<\n", GUNZE_MAX_LENGTH, gunze->data);
  52. return;
  53. }
  54. input_report_abs(dev, ABS_X, simple_strtoul(gunze->data + 1, NULL, 10));
  55. input_report_abs(dev, ABS_Y, 1024 - simple_strtoul(gunze->data + 6, NULL, 10));
  56. input_report_key(dev, BTN_TOUCH, gunze->data[0] == 'T');
  57. input_sync(dev);
  58. }
  59. static irqreturn_t gunze_interrupt(struct serio *serio,
  60. unsigned char data, unsigned int flags)
  61. {
  62. struct gunze* gunze = serio_get_drvdata(serio);
  63. if (data == '\r') {
  64. gunze_process_packet(gunze);
  65. gunze->idx = 0;
  66. } else {
  67. if (gunze->idx < GUNZE_MAX_LENGTH)
  68. gunze->data[gunze->idx++] = data;
  69. }
  70. return IRQ_HANDLED;
  71. }
  72. /*
  73. * gunze_disconnect() is the opposite of gunze_connect()
  74. */
  75. static void gunze_disconnect(struct serio *serio)
  76. {
  77. struct gunze *gunze = serio_get_drvdata(serio);
  78. input_get_device(gunze->dev);
  79. input_unregister_device(gunze->dev);
  80. serio_close(serio);
  81. serio_set_drvdata(serio, NULL);
  82. input_put_device(gunze->dev);
  83. kfree(gunze);
  84. }
  85. /*
  86. * gunze_connect() is the routine that is called when someone adds a
  87. * new serio device that supports Gunze protocol and registers it as
  88. * an input device.
  89. */
  90. static int gunze_connect(struct serio *serio, struct serio_driver *drv)
  91. {
  92. struct gunze *gunze;
  93. struct input_dev *input_dev;
  94. int err;
  95. gunze = kzalloc(sizeof(struct gunze), GFP_KERNEL);
  96. input_dev = input_allocate_device();
  97. if (!gunze || !input_dev) {
  98. err = -ENOMEM;
  99. goto fail1;
  100. }
  101. gunze->serio = serio;
  102. gunze->dev = input_dev;
  103. snprintf(gunze->phys, sizeof(serio->phys), "%s/input0", serio->phys);
  104. input_dev->name = "Gunze AHL-51S TouchScreen";
  105. input_dev->phys = gunze->phys;
  106. input_dev->id.bustype = BUS_RS232;
  107. input_dev->id.vendor = SERIO_GUNZE;
  108. input_dev->id.product = 0x0051;
  109. input_dev->id.version = 0x0100;
  110. input_dev->dev.parent = &serio->dev;
  111. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  112. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  113. input_set_abs_params(input_dev, ABS_X, 24, 1000, 0, 0);
  114. input_set_abs_params(input_dev, ABS_Y, 24, 1000, 0, 0);
  115. serio_set_drvdata(serio, gunze);
  116. err = serio_open(serio, drv);
  117. if (err)
  118. goto fail2;
  119. err = input_register_device(gunze->dev);
  120. if (err)
  121. goto fail3;
  122. return 0;
  123. fail3: serio_close(serio);
  124. fail2: serio_set_drvdata(serio, NULL);
  125. fail1: input_free_device(input_dev);
  126. kfree(gunze);
  127. return err;
  128. }
  129. /*
  130. * The serio driver structure.
  131. */
  132. static const struct serio_device_id gunze_serio_ids[] = {
  133. {
  134. .type = SERIO_RS232,
  135. .proto = SERIO_GUNZE,
  136. .id = SERIO_ANY,
  137. .extra = SERIO_ANY,
  138. },
  139. { 0 }
  140. };
  141. MODULE_DEVICE_TABLE(serio, gunze_serio_ids);
  142. static struct serio_driver gunze_drv = {
  143. .driver = {
  144. .name = "gunze",
  145. },
  146. .description = DRIVER_DESC,
  147. .id_table = gunze_serio_ids,
  148. .interrupt = gunze_interrupt,
  149. .connect = gunze_connect,
  150. .disconnect = gunze_disconnect,
  151. };
  152. module_serio_driver(gunze_drv);