touchwin.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #define DRIVER_DESC "Touchwindow serial touchscreen driver"
  28. MODULE_AUTHOR("Rick Koch <n1gp@hotmail.com>");
  29. MODULE_DESCRIPTION(DRIVER_DESC);
  30. MODULE_LICENSE("GPL");
  31. /*
  32. * Definitions & global arrays.
  33. */
  34. #define TW_LENGTH 3
  35. #define TW_MIN_XC 0
  36. #define TW_MAX_XC 0xff
  37. #define TW_MIN_YC 0
  38. #define TW_MAX_YC 0xff
  39. /*
  40. * Per-touchscreen data.
  41. */
  42. struct tw {
  43. struct input_dev *dev;
  44. struct serio *serio;
  45. int idx;
  46. int touched;
  47. unsigned char data[TW_LENGTH];
  48. char phys[32];
  49. };
  50. static irqreturn_t tw_interrupt(struct serio *serio,
  51. unsigned char data, unsigned int flags)
  52. {
  53. struct tw *tw = serio_get_drvdata(serio);
  54. struct input_dev *dev = tw->dev;
  55. if (data) { /* touch */
  56. tw->touched = 1;
  57. tw->data[tw->idx++] = data;
  58. /* verify length and that the two Y's are the same */
  59. if (tw->idx == TW_LENGTH && tw->data[1] == tw->data[2]) {
  60. input_report_abs(dev, ABS_X, tw->data[0]);
  61. input_report_abs(dev, ABS_Y, tw->data[1]);
  62. input_report_key(dev, BTN_TOUCH, 1);
  63. input_sync(dev);
  64. tw->idx = 0;
  65. }
  66. } else if (tw->touched) { /* untouch */
  67. input_report_key(dev, BTN_TOUCH, 0);
  68. input_sync(dev);
  69. tw->idx = 0;
  70. tw->touched = 0;
  71. }
  72. return IRQ_HANDLED;
  73. }
  74. /*
  75. * tw_disconnect() is the opposite of tw_connect()
  76. */
  77. static void tw_disconnect(struct serio *serio)
  78. {
  79. struct tw *tw = serio_get_drvdata(serio);
  80. input_get_device(tw->dev);
  81. input_unregister_device(tw->dev);
  82. serio_close(serio);
  83. serio_set_drvdata(serio, NULL);
  84. input_put_device(tw->dev);
  85. kfree(tw);
  86. }
  87. /*
  88. * tw_connect() is the routine that is called when someone adds a
  89. * new serio device that supports the Touchwin protocol and registers it as
  90. * an input device.
  91. */
  92. static int tw_connect(struct serio *serio, struct serio_driver *drv)
  93. {
  94. struct tw *tw;
  95. struct input_dev *input_dev;
  96. int err;
  97. tw = kzalloc(sizeof(struct tw), GFP_KERNEL);
  98. input_dev = input_allocate_device();
  99. if (!tw || !input_dev) {
  100. err = -ENOMEM;
  101. goto fail1;
  102. }
  103. tw->serio = serio;
  104. tw->dev = input_dev;
  105. snprintf(tw->phys, sizeof(tw->phys), "%s/input0", serio->phys);
  106. input_dev->name = "Touchwindow Serial TouchScreen";
  107. input_dev->phys = tw->phys;
  108. input_dev->id.bustype = BUS_RS232;
  109. input_dev->id.vendor = SERIO_TOUCHWIN;
  110. input_dev->id.product = 0;
  111. input_dev->id.version = 0x0100;
  112. input_dev->dev.parent = &serio->dev;
  113. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  114. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  115. input_set_abs_params(tw->dev, ABS_X, TW_MIN_XC, TW_MAX_XC, 0, 0);
  116. input_set_abs_params(tw->dev, ABS_Y, TW_MIN_YC, TW_MAX_YC, 0, 0);
  117. serio_set_drvdata(serio, tw);
  118. err = serio_open(serio, drv);
  119. if (err)
  120. goto fail2;
  121. err = input_register_device(tw->dev);
  122. if (err)
  123. goto fail3;
  124. return 0;
  125. fail3: serio_close(serio);
  126. fail2: serio_set_drvdata(serio, NULL);
  127. fail1: input_free_device(input_dev);
  128. kfree(tw);
  129. return err;
  130. }
  131. /*
  132. * The serio driver structure.
  133. */
  134. static struct serio_device_id tw_serio_ids[] = {
  135. {
  136. .type = SERIO_RS232,
  137. .proto = SERIO_TOUCHWIN,
  138. .id = SERIO_ANY,
  139. .extra = SERIO_ANY,
  140. },
  141. { 0 }
  142. };
  143. MODULE_DEVICE_TABLE(serio, tw_serio_ids);
  144. static struct serio_driver tw_drv = {
  145. .driver = {
  146. .name = "touchwin",
  147. },
  148. .description = DRIVER_DESC,
  149. .id_table = tw_serio_ids,
  150. .interrupt = tw_interrupt,
  151. .connect = tw_connect,
  152. .disconnect = tw_disconnect,
  153. };
  154. module_serio_driver(tw_drv);