touchwin.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Touchwindow serial touchscreen driver
  3. *
  4. * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
  5. *
  6. * Based on MicroTouch driver (drivers/input/touchscreen/mtouch.c)
  7. * Copyright (c) 2004 Vojtech Pavlik
  8. * and Dan Streetman <ddstreet@ieee.org>
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published
  13. * by the Free Software Foundation.
  14. */
  15. /*
  16. * 2005/02/19 Rick Koch:
  17. * The Touchwindow I used is made by Edmark Corp. and
  18. * constantly outputs a stream of 0's unless it is touched.
  19. * It then outputs 3 bytes: X, Y, and a copy of Y.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/input.h>
  26. #include <linux/serio.h>
  27. #include <linux/init.h>
  28. #define DRIVER_DESC "Touchwindow serial touchscreen driver"
  29. MODULE_AUTHOR("Rick Koch <n1gp@hotmail.com>");
  30. MODULE_DESCRIPTION(DRIVER_DESC);
  31. MODULE_LICENSE("GPL");
  32. /*
  33. * Definitions & global arrays.
  34. */
  35. #define TW_LENGTH 3
  36. #define TW_MIN_XC 0
  37. #define TW_MAX_XC 0xff
  38. #define TW_MIN_YC 0
  39. #define TW_MAX_YC 0xff
  40. /*
  41. * Per-touchscreen data.
  42. */
  43. struct tw {
  44. struct input_dev *dev;
  45. struct serio *serio;
  46. int idx;
  47. int touched;
  48. unsigned char data[TW_LENGTH];
  49. char phys[32];
  50. };
  51. static irqreturn_t tw_interrupt(struct serio *serio,
  52. unsigned char data, unsigned int flags)
  53. {
  54. struct tw *tw = serio_get_drvdata(serio);
  55. struct input_dev *dev = tw->dev;
  56. if (data) { /* touch */
  57. tw->touched = 1;
  58. tw->data[tw->idx++] = data;
  59. /* verify length and that the two Y's are the same */
  60. if (tw->idx == TW_LENGTH && tw->data[1] == tw->data[2]) {
  61. input_report_abs(dev, ABS_X, tw->data[0]);
  62. input_report_abs(dev, ABS_Y, tw->data[1]);
  63. input_report_key(dev, BTN_TOUCH, 1);
  64. input_sync(dev);
  65. tw->idx = 0;
  66. }
  67. } else if (tw->touched) { /* untouch */
  68. input_report_key(dev, BTN_TOUCH, 0);
  69. input_sync(dev);
  70. tw->idx = 0;
  71. tw->touched = 0;
  72. }
  73. return IRQ_HANDLED;
  74. }
  75. /*
  76. * tw_disconnect() is the opposite of tw_connect()
  77. */
  78. static void tw_disconnect(struct serio *serio)
  79. {
  80. struct tw *tw = serio_get_drvdata(serio);
  81. input_get_device(tw->dev);
  82. input_unregister_device(tw->dev);
  83. serio_close(serio);
  84. serio_set_drvdata(serio, NULL);
  85. input_put_device(tw->dev);
  86. kfree(tw);
  87. }
  88. /*
  89. * tw_connect() is the routine that is called when someone adds a
  90. * new serio device that supports the Touchwin protocol and registers it as
  91. * an input device.
  92. */
  93. static int tw_connect(struct serio *serio, struct serio_driver *drv)
  94. {
  95. struct tw *tw;
  96. struct input_dev *input_dev;
  97. int err;
  98. tw = kzalloc(sizeof(struct tw), GFP_KERNEL);
  99. input_dev = input_allocate_device();
  100. if (!tw || !input_dev) {
  101. err = -ENOMEM;
  102. goto fail1;
  103. }
  104. tw->serio = serio;
  105. tw->dev = input_dev;
  106. snprintf(tw->phys, sizeof(tw->phys), "%s/input0", serio->phys);
  107. input_dev->private = tw;
  108. input_dev->name = "Touchwindow Serial TouchScreen";
  109. input_dev->phys = tw->phys;
  110. input_dev->id.bustype = BUS_RS232;
  111. input_dev->id.vendor = SERIO_TOUCHWIN;
  112. input_dev->id.product = 0;
  113. input_dev->id.version = 0x0100;
  114. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  115. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  116. input_set_abs_params(tw->dev, ABS_X, TW_MIN_XC, TW_MAX_XC, 0, 0);
  117. input_set_abs_params(tw->dev, ABS_Y, TW_MIN_YC, TW_MAX_YC, 0, 0);
  118. serio_set_drvdata(serio, tw);
  119. err = serio_open(serio, drv);
  120. if (err)
  121. goto fail2;
  122. err = input_register_device(tw->dev);
  123. if (err)
  124. goto fail3;
  125. return 0;
  126. fail3: serio_close(serio);
  127. fail2: serio_set_drvdata(serio, NULL);
  128. fail1: input_free_device(input_dev);
  129. kfree(tw);
  130. return err;
  131. }
  132. /*
  133. * The serio driver structure.
  134. */
  135. static struct serio_device_id tw_serio_ids[] = {
  136. {
  137. .type = SERIO_RS232,
  138. .proto = SERIO_TOUCHWIN,
  139. .id = SERIO_ANY,
  140. .extra = SERIO_ANY,
  141. },
  142. { 0 }
  143. };
  144. MODULE_DEVICE_TABLE(serio, tw_serio_ids);
  145. static struct serio_driver tw_drv = {
  146. .driver = {
  147. .name = "touchwin",
  148. },
  149. .description = DRIVER_DESC,
  150. .id_table = tw_serio_ids,
  151. .interrupt = tw_interrupt,
  152. .connect = tw_connect,
  153. .disconnect = tw_disconnect,
  154. };
  155. /*
  156. * The functions for inserting/removing us as a module.
  157. */
  158. static int __init tw_init(void)
  159. {
  160. return serio_register_driver(&tw_drv);
  161. }
  162. static void __exit tw_exit(void)
  163. {
  164. serio_unregister_driver(&tw_drv);
  165. }
  166. module_init(tw_init);
  167. module_exit(tw_exit);