dynapro.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #define DRIVER_DESC "Dynapro serial touchscreen driver"
  25. MODULE_AUTHOR("Tias Guns <tias@ulyssis.org>");
  26. MODULE_DESCRIPTION(DRIVER_DESC);
  27. MODULE_LICENSE("GPL");
  28. /*
  29. * Definitions & global arrays.
  30. */
  31. #define DYNAPRO_FORMAT_TOUCH_BIT 0x40
  32. #define DYNAPRO_FORMAT_LENGTH 3
  33. #define DYNAPRO_RESPONSE_BEGIN_BYTE 0x80
  34. #define DYNAPRO_MIN_XC 0
  35. #define DYNAPRO_MAX_XC 0x3ff
  36. #define DYNAPRO_MIN_YC 0
  37. #define DYNAPRO_MAX_YC 0x3ff
  38. #define DYNAPRO_GET_XC(data) (data[1] | ((data[0] & 0x38) << 4))
  39. #define DYNAPRO_GET_YC(data) (data[2] | ((data[0] & 0x07) << 7))
  40. #define DYNAPRO_GET_TOUCHED(data) (DYNAPRO_FORMAT_TOUCH_BIT & data[0])
  41. /*
  42. * Per-touchscreen data.
  43. */
  44. struct dynapro {
  45. struct input_dev *dev;
  46. struct serio *serio;
  47. int idx;
  48. unsigned char data[DYNAPRO_FORMAT_LENGTH];
  49. char phys[32];
  50. };
  51. static void dynapro_process_data(struct dynapro *pdynapro)
  52. {
  53. struct input_dev *dev = pdynapro->dev;
  54. if (DYNAPRO_FORMAT_LENGTH == ++pdynapro->idx) {
  55. input_report_abs(dev, ABS_X, DYNAPRO_GET_XC(pdynapro->data));
  56. input_report_abs(dev, ABS_Y, DYNAPRO_GET_YC(pdynapro->data));
  57. input_report_key(dev, BTN_TOUCH,
  58. DYNAPRO_GET_TOUCHED(pdynapro->data));
  59. input_sync(dev);
  60. pdynapro->idx = 0;
  61. }
  62. }
  63. static irqreturn_t dynapro_interrupt(struct serio *serio,
  64. unsigned char data, unsigned int flags)
  65. {
  66. struct dynapro *pdynapro = serio_get_drvdata(serio);
  67. pdynapro->data[pdynapro->idx] = data;
  68. if (DYNAPRO_RESPONSE_BEGIN_BYTE & pdynapro->data[0])
  69. dynapro_process_data(pdynapro);
  70. else
  71. dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
  72. pdynapro->data[0]);
  73. return IRQ_HANDLED;
  74. }
  75. static void dynapro_disconnect(struct serio *serio)
  76. {
  77. struct dynapro *pdynapro = serio_get_drvdata(serio);
  78. input_get_device(pdynapro->dev);
  79. input_unregister_device(pdynapro->dev);
  80. serio_close(serio);
  81. serio_set_drvdata(serio, NULL);
  82. input_put_device(pdynapro->dev);
  83. kfree(pdynapro);
  84. }
  85. /*
  86. * dynapro_connect() is the routine that is called when someone adds a
  87. * new serio device that supports dynapro protocol and registers it as
  88. * an input device. This is usually accomplished using inputattach.
  89. */
  90. static int dynapro_connect(struct serio *serio, struct serio_driver *drv)
  91. {
  92. struct dynapro *pdynapro;
  93. struct input_dev *input_dev;
  94. int err;
  95. pdynapro = kzalloc(sizeof(struct dynapro), GFP_KERNEL);
  96. input_dev = input_allocate_device();
  97. if (!pdynapro || !input_dev) {
  98. err = -ENOMEM;
  99. goto fail1;
  100. }
  101. pdynapro->serio = serio;
  102. pdynapro->dev = input_dev;
  103. snprintf(pdynapro->phys, sizeof(pdynapro->phys),
  104. "%s/input0", serio->phys);
  105. input_dev->name = "Dynapro Serial TouchScreen";
  106. input_dev->phys = pdynapro->phys;
  107. input_dev->id.bustype = BUS_RS232;
  108. input_dev->id.vendor = SERIO_DYNAPRO;
  109. input_dev->id.product = 0;
  110. input_dev->id.version = 0x0001;
  111. input_dev->dev.parent = &serio->dev;
  112. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  113. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  114. input_set_abs_params(pdynapro->dev, ABS_X,
  115. DYNAPRO_MIN_XC, DYNAPRO_MAX_XC, 0, 0);
  116. input_set_abs_params(pdynapro->dev, ABS_Y,
  117. DYNAPRO_MIN_YC, DYNAPRO_MAX_YC, 0, 0);
  118. serio_set_drvdata(serio, pdynapro);
  119. err = serio_open(serio, drv);
  120. if (err)
  121. goto fail2;
  122. err = input_register_device(pdynapro->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(pdynapro);
  130. return err;
  131. }
  132. /*
  133. * The serio driver structure.
  134. */
  135. static struct serio_device_id dynapro_serio_ids[] = {
  136. {
  137. .type = SERIO_RS232,
  138. .proto = SERIO_DYNAPRO,
  139. .id = SERIO_ANY,
  140. .extra = SERIO_ANY,
  141. },
  142. { 0 }
  143. };
  144. MODULE_DEVICE_TABLE(serio, dynapro_serio_ids);
  145. static struct serio_driver dynapro_drv = {
  146. .driver = {
  147. .name = "dynapro",
  148. },
  149. .description = DRIVER_DESC,
  150. .id_table = dynapro_serio_ids,
  151. .interrupt = dynapro_interrupt,
  152. .connect = dynapro_connect,
  153. .disconnect = dynapro_disconnect,
  154. };
  155. module_serio_driver(dynapro_drv);