fujitsu_ts.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Fujitsu serial touchscreen driver
  3. *
  4. * Copyright (c) Dmitry Torokhov <dtor@mail.ru>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/input.h>
  16. #include <linux/serio.h>
  17. #define DRIVER_DESC "Fujitsu serial touchscreen driver"
  18. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  19. MODULE_DESCRIPTION(DRIVER_DESC);
  20. MODULE_LICENSE("GPL");
  21. #define FUJITSU_LENGTH 5
  22. /*
  23. * Per-touchscreen data.
  24. */
  25. struct fujitsu {
  26. struct input_dev *dev;
  27. struct serio *serio;
  28. int idx;
  29. unsigned char data[FUJITSU_LENGTH];
  30. char phys[32];
  31. };
  32. /*
  33. * Decode serial data (5 bytes per packet)
  34. * First byte
  35. * 1 C 0 0 R S S S
  36. * Where C is 1 while in calibration mode (which we don't use)
  37. * R is 1 when no coordinate corection was done.
  38. * S are button state
  39. */
  40. static irqreturn_t fujitsu_interrupt(struct serio *serio,
  41. unsigned char data, unsigned int flags)
  42. {
  43. struct fujitsu *fujitsu = serio_get_drvdata(serio);
  44. struct input_dev *dev = fujitsu->dev;
  45. if (fujitsu->idx == 0) {
  46. /* resync skip until start of frame */
  47. if ((data & 0xf0) != 0x80)
  48. return IRQ_HANDLED;
  49. } else {
  50. /* resync skip garbage */
  51. if (data & 0x80) {
  52. fujitsu->idx = 0;
  53. return IRQ_HANDLED;
  54. }
  55. }
  56. fujitsu->data[fujitsu->idx++] = data;
  57. if (fujitsu->idx == FUJITSU_LENGTH) {
  58. input_report_abs(dev, ABS_X,
  59. (fujitsu->data[2] << 7) | fujitsu->data[1]);
  60. input_report_abs(dev, ABS_Y,
  61. (fujitsu->data[4] << 7) | fujitsu->data[3]);
  62. input_report_key(dev, BTN_TOUCH,
  63. (fujitsu->data[0] & 0x03) != 2);
  64. input_sync(dev);
  65. fujitsu->idx = 0;
  66. }
  67. return IRQ_HANDLED;
  68. }
  69. /*
  70. * fujitsu_disconnect() is the opposite of fujitsu_connect()
  71. */
  72. static void fujitsu_disconnect(struct serio *serio)
  73. {
  74. struct fujitsu *fujitsu = serio_get_drvdata(serio);
  75. input_get_device(fujitsu->dev);
  76. input_unregister_device(fujitsu->dev);
  77. serio_close(serio);
  78. serio_set_drvdata(serio, NULL);
  79. input_put_device(fujitsu->dev);
  80. kfree(fujitsu);
  81. }
  82. /*
  83. * fujitsu_connect() is the routine that is called when someone adds a
  84. * new serio device that supports the Fujitsu protocol and registers it
  85. * as input device.
  86. */
  87. static int fujitsu_connect(struct serio *serio, struct serio_driver *drv)
  88. {
  89. struct fujitsu *fujitsu;
  90. struct input_dev *input_dev;
  91. int err;
  92. fujitsu = kzalloc(sizeof(struct fujitsu), GFP_KERNEL);
  93. input_dev = input_allocate_device();
  94. if (!fujitsu || !input_dev) {
  95. err = -ENOMEM;
  96. goto fail1;
  97. }
  98. fujitsu->serio = serio;
  99. fujitsu->dev = input_dev;
  100. snprintf(fujitsu->phys, sizeof(fujitsu->phys),
  101. "%s/input0", serio->phys);
  102. input_dev->name = "Fujitsu Serial Touchscreen";
  103. input_dev->phys = fujitsu->phys;
  104. input_dev->id.bustype = BUS_RS232;
  105. input_dev->id.vendor = SERIO_FUJITSU;
  106. input_dev->id.product = 0;
  107. input_dev->id.version = 0x0100;
  108. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  109. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  110. input_set_abs_params(input_dev, ABS_X, 0, 4096, 0, 0);
  111. input_set_abs_params(input_dev, ABS_Y, 0, 4096, 0, 0);
  112. serio_set_drvdata(serio, fujitsu);
  113. err = serio_open(serio, drv);
  114. if (err)
  115. goto fail2;
  116. err = input_register_device(fujitsu->dev);
  117. if (err)
  118. goto fail3;
  119. return 0;
  120. fail3:
  121. serio_close(serio);
  122. fail2:
  123. serio_set_drvdata(serio, NULL);
  124. fail1:
  125. input_free_device(input_dev);
  126. kfree(fujitsu);
  127. return err;
  128. }
  129. /*
  130. * The serio driver structure.
  131. */
  132. static struct serio_device_id fujitsu_serio_ids[] = {
  133. {
  134. .type = SERIO_RS232,
  135. .proto = SERIO_FUJITSU,
  136. .id = SERIO_ANY,
  137. .extra = SERIO_ANY,
  138. },
  139. { 0 }
  140. };
  141. MODULE_DEVICE_TABLE(serio, fujitsu_serio_ids);
  142. static struct serio_driver fujitsu_drv = {
  143. .driver = {
  144. .name = "fujitsu_ts",
  145. },
  146. .description = DRIVER_DESC,
  147. .id_table = fujitsu_serio_ids,
  148. .interrupt = fujitsu_interrupt,
  149. .connect = fujitsu_connect,
  150. .disconnect = fujitsu_disconnect,
  151. };
  152. module_serio_driver(fujitsu_drv);