gunze.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * $Id: gunze.c,v 1.12 2001/09/25 10:12:07 vojtech Exp $
  3. *
  4. * Copyright (c) 2000-2001 Vojtech Pavlik
  5. */
  6. /*
  7. * Gunze AHL-51S touchscreen driver for Linux
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Should you need to contact me, the author, you can do so either by
  25. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  26. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  27. */
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/input.h>
  33. #include <linux/serio.h>
  34. #include <linux/init.h>
  35. #define DRIVER_DESC "Gunze AHL-51S touchscreen driver"
  36. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  37. MODULE_DESCRIPTION(DRIVER_DESC);
  38. MODULE_LICENSE("GPL");
  39. /*
  40. * Definitions & global arrays.
  41. */
  42. #define GUNZE_MAX_LENGTH 10
  43. /*
  44. * Per-touchscreen data.
  45. */
  46. struct gunze {
  47. struct input_dev *dev;
  48. struct serio *serio;
  49. int idx;
  50. unsigned char data[GUNZE_MAX_LENGTH];
  51. char phys[32];
  52. };
  53. static void gunze_process_packet(struct gunze* gunze, struct pt_regs *regs)
  54. {
  55. struct input_dev *dev = gunze->dev;
  56. if (gunze->idx != GUNZE_MAX_LENGTH || gunze->data[5] != ',' ||
  57. (gunze->data[0] != 'T' && gunze->data[0] != 'R')) {
  58. printk(KERN_WARNING "gunze.c: bad packet: >%.*s<\n", GUNZE_MAX_LENGTH, gunze->data);
  59. return;
  60. }
  61. input_regs(dev, regs);
  62. input_report_abs(dev, ABS_X, simple_strtoul(gunze->data + 1, NULL, 10));
  63. input_report_abs(dev, ABS_Y, 1024 - simple_strtoul(gunze->data + 6, NULL, 10));
  64. input_report_key(dev, BTN_TOUCH, gunze->data[0] == 'T');
  65. input_sync(dev);
  66. }
  67. static irqreturn_t gunze_interrupt(struct serio *serio,
  68. unsigned char data, unsigned int flags, struct pt_regs *regs)
  69. {
  70. struct gunze* gunze = serio_get_drvdata(serio);
  71. if (data == '\r') {
  72. gunze_process_packet(gunze, regs);
  73. gunze->idx = 0;
  74. } else {
  75. if (gunze->idx < GUNZE_MAX_LENGTH)
  76. gunze->data[gunze->idx++] = data;
  77. }
  78. return IRQ_HANDLED;
  79. }
  80. /*
  81. * gunze_disconnect() is the opposite of gunze_connect()
  82. */
  83. static void gunze_disconnect(struct serio *serio)
  84. {
  85. struct gunze *gunze = serio_get_drvdata(serio);
  86. input_get_device(gunze->dev);
  87. input_unregister_device(gunze->dev);
  88. serio_close(serio);
  89. serio_set_drvdata(serio, NULL);
  90. input_put_device(gunze->dev);
  91. kfree(gunze);
  92. }
  93. /*
  94. * gunze_connect() is the routine that is called when someone adds a
  95. * new serio device that supports Gunze protocol and registers it as
  96. * an input device.
  97. */
  98. static int gunze_connect(struct serio *serio, struct serio_driver *drv)
  99. {
  100. struct gunze *gunze;
  101. struct input_dev *input_dev;
  102. int err;
  103. gunze = kzalloc(sizeof(struct gunze), GFP_KERNEL);
  104. input_dev = input_allocate_device();
  105. if (!gunze || !input_dev) {
  106. err = -ENOMEM;
  107. goto fail;
  108. }
  109. gunze->serio = serio;
  110. gunze->dev = input_dev;
  111. sprintf(gunze->phys, "%s/input0", serio->phys);
  112. input_dev->private = gunze;
  113. input_dev->name = "Gunze AHL-51S TouchScreen";
  114. input_dev->phys = gunze->phys;
  115. input_dev->id.bustype = BUS_RS232;
  116. input_dev->id.vendor = SERIO_GUNZE;
  117. input_dev->id.product = 0x0051;
  118. input_dev->id.version = 0x0100;
  119. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  120. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  121. input_set_abs_params(input_dev, ABS_X, 24, 1000, 0, 0);
  122. input_set_abs_params(input_dev, ABS_Y, 24, 1000, 0, 0);
  123. serio_set_drvdata(serio, gunze);
  124. err = serio_open(serio, drv);
  125. if (err)
  126. goto fail;
  127. input_register_device(gunze->dev);
  128. return 0;
  129. fail: serio_set_drvdata(serio, NULL);
  130. 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. serio_register_driver(&gunze_drv);
  163. return 0;
  164. }
  165. static void __exit gunze_exit(void)
  166. {
  167. serio_unregister_driver(&gunze_drv);
  168. }
  169. module_init(gunze_init);
  170. module_exit(gunze_exit);