touchright.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Touchright 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. #include <linux/errno.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/input.h>
  20. #include <linux/serio.h>
  21. #define DRIVER_DESC "Touchright serial touchscreen driver"
  22. MODULE_AUTHOR("Rick Koch <n1gp@hotmail.com>");
  23. MODULE_DESCRIPTION(DRIVER_DESC);
  24. MODULE_LICENSE("GPL");
  25. /*
  26. * Definitions & global arrays.
  27. */
  28. #define TR_FORMAT_TOUCH_BIT 0x01
  29. #define TR_FORMAT_STATUS_BYTE 0x40
  30. #define TR_FORMAT_STATUS_MASK ~TR_FORMAT_TOUCH_BIT
  31. #define TR_LENGTH 5
  32. #define TR_MIN_XC 0
  33. #define TR_MAX_XC 0x1ff
  34. #define TR_MIN_YC 0
  35. #define TR_MAX_YC 0x1ff
  36. /*
  37. * Per-touchscreen data.
  38. */
  39. struct tr {
  40. struct input_dev *dev;
  41. struct serio *serio;
  42. int idx;
  43. unsigned char data[TR_LENGTH];
  44. char phys[32];
  45. };
  46. static irqreturn_t tr_interrupt(struct serio *serio,
  47. unsigned char data, unsigned int flags)
  48. {
  49. struct tr *tr = serio_get_drvdata(serio);
  50. struct input_dev *dev = tr->dev;
  51. tr->data[tr->idx] = data;
  52. if ((tr->data[0] & TR_FORMAT_STATUS_MASK) == TR_FORMAT_STATUS_BYTE) {
  53. if (++tr->idx == TR_LENGTH) {
  54. input_report_abs(dev, ABS_X,
  55. (tr->data[1] << 5) | (tr->data[2] >> 1));
  56. input_report_abs(dev, ABS_Y,
  57. (tr->data[3] << 5) | (tr->data[4] >> 1));
  58. input_report_key(dev, BTN_TOUCH,
  59. tr->data[0] & TR_FORMAT_TOUCH_BIT);
  60. input_sync(dev);
  61. tr->idx = 0;
  62. }
  63. }
  64. return IRQ_HANDLED;
  65. }
  66. /*
  67. * tr_disconnect() is the opposite of tr_connect()
  68. */
  69. static void tr_disconnect(struct serio *serio)
  70. {
  71. struct tr *tr = serio_get_drvdata(serio);
  72. input_get_device(tr->dev);
  73. input_unregister_device(tr->dev);
  74. serio_close(serio);
  75. serio_set_drvdata(serio, NULL);
  76. input_put_device(tr->dev);
  77. kfree(tr);
  78. }
  79. /*
  80. * tr_connect() is the routine that is called when someone adds a
  81. * new serio device that supports the Touchright protocol and registers it as
  82. * an input device.
  83. */
  84. static int tr_connect(struct serio *serio, struct serio_driver *drv)
  85. {
  86. struct tr *tr;
  87. struct input_dev *input_dev;
  88. int err;
  89. tr = kzalloc(sizeof(struct tr), GFP_KERNEL);
  90. input_dev = input_allocate_device();
  91. if (!tr || !input_dev) {
  92. err = -ENOMEM;
  93. goto fail1;
  94. }
  95. tr->serio = serio;
  96. tr->dev = input_dev;
  97. snprintf(tr->phys, sizeof(tr->phys), "%s/input0", serio->phys);
  98. input_dev->name = "Touchright Serial TouchScreen";
  99. input_dev->phys = tr->phys;
  100. input_dev->id.bustype = BUS_RS232;
  101. input_dev->id.vendor = SERIO_TOUCHRIGHT;
  102. input_dev->id.product = 0;
  103. input_dev->id.version = 0x0100;
  104. input_dev->dev.parent = &serio->dev;
  105. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  106. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  107. input_set_abs_params(tr->dev, ABS_X, TR_MIN_XC, TR_MAX_XC, 0, 0);
  108. input_set_abs_params(tr->dev, ABS_Y, TR_MIN_YC, TR_MAX_YC, 0, 0);
  109. serio_set_drvdata(serio, tr);
  110. err = serio_open(serio, drv);
  111. if (err)
  112. goto fail2;
  113. err = input_register_device(tr->dev);
  114. if (err)
  115. goto fail3;
  116. return 0;
  117. fail3: serio_close(serio);
  118. fail2: serio_set_drvdata(serio, NULL);
  119. fail1: input_free_device(input_dev);
  120. kfree(tr);
  121. return err;
  122. }
  123. /*
  124. * The serio driver structure.
  125. */
  126. static struct serio_device_id tr_serio_ids[] = {
  127. {
  128. .type = SERIO_RS232,
  129. .proto = SERIO_TOUCHRIGHT,
  130. .id = SERIO_ANY,
  131. .extra = SERIO_ANY,
  132. },
  133. { 0 }
  134. };
  135. MODULE_DEVICE_TABLE(serio, tr_serio_ids);
  136. static struct serio_driver tr_drv = {
  137. .driver = {
  138. .name = "touchright",
  139. },
  140. .description = DRIVER_DESC,
  141. .id_table = tr_serio_ids,
  142. .interrupt = tr_interrupt,
  143. .connect = tr_connect,
  144. .disconnect = tr_disconnect,
  145. };
  146. module_serio_driver(tr_drv);