dynapro.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Dynapro serial touchscreen driver
  3. *
  4. * Copyright (c) 2009 Tias Guns
  5. * Based on the inexio driver (c) Vojtech Pavlik and Dan Streetman and
  6. * Richard Lemon
  7. *
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. */
  14. /*
  15. * 2009/09/19 Tias Guns <tias@ulyssis.org>
  16. * Copied inexio.c and edited for Dynapro protocol (from retired Xorg module)
  17. */
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/input.h>
  23. #include <linux/serio.h>
  24. #include <linux/init.h>
  25. #define DRIVER_DESC "Dynapro serial touchscreen driver"
  26. MODULE_AUTHOR("Tias Guns <tias@ulyssis.org>");
  27. MODULE_DESCRIPTION(DRIVER_DESC);
  28. MODULE_LICENSE("GPL");
  29. /*
  30. * Definitions & global arrays.
  31. */
  32. #define DYNAPRO_FORMAT_TOUCH_BIT 0x40
  33. #define DYNAPRO_FORMAT_LENGTH 3
  34. #define DYNAPRO_RESPONSE_BEGIN_BYTE 0x80
  35. #define DYNAPRO_MIN_XC 0
  36. #define DYNAPRO_MAX_XC 0x3ff
  37. #define DYNAPRO_MIN_YC 0
  38. #define DYNAPRO_MAX_YC 0x3ff
  39. #define DYNAPRO_GET_XC(data) (data[1] | ((data[0] & 0x38) << 4))
  40. #define DYNAPRO_GET_YC(data) (data[2] | ((data[0] & 0x07) << 7))
  41. #define DYNAPRO_GET_TOUCHED(data) (DYNAPRO_FORMAT_TOUCH_BIT & data[0])
  42. /*
  43. * Per-touchscreen data.
  44. */
  45. struct dynapro {
  46. struct input_dev *dev;
  47. struct serio *serio;
  48. int idx;
  49. unsigned char data[DYNAPRO_FORMAT_LENGTH];
  50. char phys[32];
  51. };
  52. static void dynapro_process_data(struct dynapro *pdynapro)
  53. {
  54. struct input_dev *dev = pdynapro->dev;
  55. if (DYNAPRO_FORMAT_LENGTH == ++pdynapro->idx) {
  56. input_report_abs(dev, ABS_X, DYNAPRO_GET_XC(pdynapro->data));
  57. input_report_abs(dev, ABS_Y, DYNAPRO_GET_YC(pdynapro->data));
  58. input_report_key(dev, BTN_TOUCH,
  59. DYNAPRO_GET_TOUCHED(pdynapro->data));
  60. input_sync(dev);
  61. pdynapro->idx = 0;
  62. }
  63. }
  64. static irqreturn_t dynapro_interrupt(struct serio *serio,
  65. unsigned char data, unsigned int flags)
  66. {
  67. struct dynapro *pdynapro = serio_get_drvdata(serio);
  68. pdynapro->data[pdynapro->idx] = data;
  69. if (DYNAPRO_RESPONSE_BEGIN_BYTE & pdynapro->data[0])
  70. dynapro_process_data(pdynapro);
  71. else
  72. dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
  73. pdynapro->data[0]);
  74. return IRQ_HANDLED;
  75. }
  76. static void dynapro_disconnect(struct serio *serio)
  77. {
  78. struct dynapro *pdynapro = serio_get_drvdata(serio);
  79. input_get_device(pdynapro->dev);
  80. input_unregister_device(pdynapro->dev);
  81. serio_close(serio);
  82. serio_set_drvdata(serio, NULL);
  83. input_put_device(pdynapro->dev);
  84. kfree(pdynapro);
  85. }
  86. /*
  87. * dynapro_connect() is the routine that is called when someone adds a
  88. * new serio device that supports dynapro protocol and registers it as
  89. * an input device. This is usually accomplished using inputattach.
  90. */
  91. static int dynapro_connect(struct serio *serio, struct serio_driver *drv)
  92. {
  93. struct dynapro *pdynapro;
  94. struct input_dev *input_dev;
  95. int err;
  96. pdynapro = kzalloc(sizeof(struct dynapro), GFP_KERNEL);
  97. input_dev = input_allocate_device();
  98. if (!pdynapro || !input_dev) {
  99. err = -ENOMEM;
  100. goto fail1;
  101. }
  102. pdynapro->serio = serio;
  103. pdynapro->dev = input_dev;
  104. snprintf(pdynapro->phys, sizeof(pdynapro->phys),
  105. "%s/input0", serio->phys);
  106. input_dev->name = "Dynapro Serial TouchScreen";
  107. input_dev->phys = pdynapro->phys;
  108. input_dev->id.bustype = BUS_RS232;
  109. input_dev->id.vendor = SERIO_DYNAPRO;
  110. input_dev->id.product = 0;
  111. input_dev->id.version = 0x0001;
  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(pdynapro->dev, ABS_X,
  116. DYNAPRO_MIN_XC, DYNAPRO_MAX_XC, 0, 0);
  117. input_set_abs_params(pdynapro->dev, ABS_Y,
  118. DYNAPRO_MIN_YC, DYNAPRO_MAX_YC, 0, 0);
  119. serio_set_drvdata(serio, pdynapro);
  120. err = serio_open(serio, drv);
  121. if (err)
  122. goto fail2;
  123. err = input_register_device(pdynapro->dev);
  124. if (err)
  125. goto fail3;
  126. return 0;
  127. fail3: serio_close(serio);
  128. fail2: serio_set_drvdata(serio, NULL);
  129. fail1: input_free_device(input_dev);
  130. kfree(pdynapro);
  131. return err;
  132. }
  133. /*
  134. * The serio driver structure.
  135. */
  136. static struct serio_device_id dynapro_serio_ids[] = {
  137. {
  138. .type = SERIO_RS232,
  139. .proto = SERIO_DYNAPRO,
  140. .id = SERIO_ANY,
  141. .extra = SERIO_ANY,
  142. },
  143. { 0 }
  144. };
  145. MODULE_DEVICE_TABLE(serio, dynapro_serio_ids);
  146. static struct serio_driver dynapro_drv = {
  147. .driver = {
  148. .name = "dynapro",
  149. },
  150. .description = DRIVER_DESC,
  151. .id_table = dynapro_serio_ids,
  152. .interrupt = dynapro_interrupt,
  153. .connect = dynapro_connect,
  154. .disconnect = dynapro_disconnect,
  155. };
  156. /*
  157. * The functions for inserting/removing us as a module.
  158. */
  159. static int __init dynapro_init(void)
  160. {
  161. return serio_register_driver(&dynapro_drv);
  162. }
  163. static void __exit dynapro_exit(void)
  164. {
  165. serio_unregister_driver(&dynapro_drv);
  166. }
  167. module_init(dynapro_init);
  168. module_exit(dynapro_exit);