gunze.c 4.8 KB

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