gunze.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include <linux/errno.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/input.h>
  31. #include <linux/serio.h>
  32. #define DRIVER_DESC "Gunze AHL-51S touchscreen driver"
  33. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  34. MODULE_DESCRIPTION(DRIVER_DESC);
  35. MODULE_LICENSE("GPL");
  36. /*
  37. * Definitions & global arrays.
  38. */
  39. #define GUNZE_MAX_LENGTH 10
  40. /*
  41. * Per-touchscreen data.
  42. */
  43. struct gunze {
  44. struct input_dev *dev;
  45. struct serio *serio;
  46. int idx;
  47. unsigned char data[GUNZE_MAX_LENGTH];
  48. char phys[32];
  49. };
  50. static void gunze_process_packet(struct gunze* gunze)
  51. {
  52. struct input_dev *dev = gunze->dev;
  53. if (gunze->idx != GUNZE_MAX_LENGTH || gunze->data[5] != ',' ||
  54. (gunze->data[0] != 'T' && gunze->data[0] != 'R')) {
  55. printk(KERN_WARNING "gunze.c: bad packet: >%.*s<\n", GUNZE_MAX_LENGTH, gunze->data);
  56. return;
  57. }
  58. input_report_abs(dev, ABS_X, simple_strtoul(gunze->data + 1, NULL, 10));
  59. input_report_abs(dev, ABS_Y, 1024 - simple_strtoul(gunze->data + 6, NULL, 10));
  60. input_report_key(dev, BTN_TOUCH, gunze->data[0] == 'T');
  61. input_sync(dev);
  62. }
  63. static irqreturn_t gunze_interrupt(struct serio *serio,
  64. unsigned char data, unsigned int flags)
  65. {
  66. struct gunze* gunze = serio_get_drvdata(serio);
  67. if (data == '\r') {
  68. gunze_process_packet(gunze);
  69. gunze->idx = 0;
  70. } else {
  71. if (gunze->idx < GUNZE_MAX_LENGTH)
  72. gunze->data[gunze->idx++] = data;
  73. }
  74. return IRQ_HANDLED;
  75. }
  76. /*
  77. * gunze_disconnect() is the opposite of gunze_connect()
  78. */
  79. static void gunze_disconnect(struct serio *serio)
  80. {
  81. struct gunze *gunze = serio_get_drvdata(serio);
  82. input_get_device(gunze->dev);
  83. input_unregister_device(gunze->dev);
  84. serio_close(serio);
  85. serio_set_drvdata(serio, NULL);
  86. input_put_device(gunze->dev);
  87. kfree(gunze);
  88. }
  89. /*
  90. * gunze_connect() is the routine that is called when someone adds a
  91. * new serio device that supports Gunze protocol and registers it as
  92. * an input device.
  93. */
  94. static int gunze_connect(struct serio *serio, struct serio_driver *drv)
  95. {
  96. struct gunze *gunze;
  97. struct input_dev *input_dev;
  98. int err;
  99. gunze = kzalloc(sizeof(struct gunze), GFP_KERNEL);
  100. input_dev = input_allocate_device();
  101. if (!gunze || !input_dev) {
  102. err = -ENOMEM;
  103. goto fail1;
  104. }
  105. gunze->serio = serio;
  106. gunze->dev = input_dev;
  107. snprintf(gunze->phys, sizeof(serio->phys), "%s/input0", serio->phys);
  108. input_dev->name = "Gunze AHL-51S TouchScreen";
  109. input_dev->phys = gunze->phys;
  110. input_dev->id.bustype = BUS_RS232;
  111. input_dev->id.vendor = SERIO_GUNZE;
  112. input_dev->id.product = 0x0051;
  113. input_dev->id.version = 0x0100;
  114. input_dev->dev.parent = &serio->dev;
  115. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  116. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  117. input_set_abs_params(input_dev, ABS_X, 24, 1000, 0, 0);
  118. input_set_abs_params(input_dev, ABS_Y, 24, 1000, 0, 0);
  119. serio_set_drvdata(serio, gunze);
  120. err = serio_open(serio, drv);
  121. if (err)
  122. goto fail2;
  123. err = input_register_device(gunze->dev);
  124. if (err)
  125. goto fail3;
  126. return 0;
  127. fail3: serio_close(serio);
  128. fail2: serio_set_drvdata(serio, NULL);
  129. fail1: input_free_device(input_dev);
  130. kfree(gunze);
  131. return err;
  132. }
  133. /*
  134. * The serio driver structure.
  135. */
  136. static struct serio_device_id gunze_serio_ids[] = {
  137. {
  138. .type = SERIO_RS232,
  139. .proto = SERIO_GUNZE,
  140. .id = SERIO_ANY,
  141. .extra = SERIO_ANY,
  142. },
  143. { 0 }
  144. };
  145. MODULE_DEVICE_TABLE(serio, gunze_serio_ids);
  146. static struct serio_driver gunze_drv = {
  147. .driver = {
  148. .name = "gunze",
  149. },
  150. .description = DRIVER_DESC,
  151. .id_table = gunze_serio_ids,
  152. .interrupt = gunze_interrupt,
  153. .connect = gunze_connect,
  154. .disconnect = gunze_disconnect,
  155. };
  156. module_serio_driver(gunze_drv);