hampshire.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Hampshire serial touchscreen driver
  3. *
  4. * Copyright (c) 2010 Adam Bennett
  5. * Based on the dynapro driver (c) Tias Guns
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. */
  13. /*
  14. * 2010/04/08 Adam Bennett <abennett72@gmail.com>
  15. * Copied dynapro.c and edited for Hampshire 4-byte protocol
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/input.h>
  22. #include <linux/serio.h>
  23. #include <linux/init.h>
  24. #define DRIVER_DESC "Hampshire serial touchscreen driver"
  25. MODULE_AUTHOR("Adam Bennett <abennett72@gmail.com>");
  26. MODULE_DESCRIPTION(DRIVER_DESC);
  27. MODULE_LICENSE("GPL");
  28. /*
  29. * Definitions & global arrays.
  30. */
  31. #define HAMPSHIRE_FORMAT_TOUCH_BIT 0x40
  32. #define HAMPSHIRE_FORMAT_LENGTH 4
  33. #define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x80
  34. #define HAMPSHIRE_MIN_XC 0
  35. #define HAMPSHIRE_MAX_XC 0x1000
  36. #define HAMPSHIRE_MIN_YC 0
  37. #define HAMPSHIRE_MAX_YC 0x1000
  38. #define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))
  39. #define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))
  40. #define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])
  41. /*
  42. * Per-touchscreen data.
  43. */
  44. struct hampshire {
  45. struct input_dev *dev;
  46. struct serio *serio;
  47. int idx;
  48. unsigned char data[HAMPSHIRE_FORMAT_LENGTH];
  49. char phys[32];
  50. };
  51. static void hampshire_process_data(struct hampshire *phampshire)
  52. {
  53. struct input_dev *dev = phampshire->dev;
  54. if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {
  55. input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));
  56. input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));
  57. input_report_key(dev, BTN_TOUCH,
  58. HAMPSHIRE_GET_TOUCHED(phampshire->data));
  59. input_sync(dev);
  60. phampshire->idx = 0;
  61. }
  62. }
  63. static irqreturn_t hampshire_interrupt(struct serio *serio,
  64. unsigned char data, unsigned int flags)
  65. {
  66. struct hampshire *phampshire = serio_get_drvdata(serio);
  67. phampshire->data[phampshire->idx] = data;
  68. if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])
  69. hampshire_process_data(phampshire);
  70. else
  71. dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
  72. phampshire->data[0]);
  73. return IRQ_HANDLED;
  74. }
  75. static void hampshire_disconnect(struct serio *serio)
  76. {
  77. struct hampshire *phampshire = serio_get_drvdata(serio);
  78. input_get_device(phampshire->dev);
  79. input_unregister_device(phampshire->dev);
  80. serio_close(serio);
  81. serio_set_drvdata(serio, NULL);
  82. input_put_device(phampshire->dev);
  83. kfree(phampshire);
  84. }
  85. /*
  86. * hampshire_connect() is the routine that is called when someone adds a
  87. * new serio device that supports hampshire protocol and registers it as
  88. * an input device. This is usually accomplished using inputattach.
  89. */
  90. static int hampshire_connect(struct serio *serio, struct serio_driver *drv)
  91. {
  92. struct hampshire *phampshire;
  93. struct input_dev *input_dev;
  94. int err;
  95. phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);
  96. input_dev = input_allocate_device();
  97. if (!phampshire || !input_dev) {
  98. err = -ENOMEM;
  99. goto fail1;
  100. }
  101. phampshire->serio = serio;
  102. phampshire->dev = input_dev;
  103. snprintf(phampshire->phys, sizeof(phampshire->phys),
  104. "%s/input0", serio->phys);
  105. input_dev->name = "Hampshire Serial TouchScreen";
  106. input_dev->phys = phampshire->phys;
  107. input_dev->id.bustype = BUS_RS232;
  108. input_dev->id.vendor = SERIO_HAMPSHIRE;
  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(phampshire->dev, ABS_X,
  115. HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);
  116. input_set_abs_params(phampshire->dev, ABS_Y,
  117. HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);
  118. serio_set_drvdata(serio, phampshire);
  119. err = serio_open(serio, drv);
  120. if (err)
  121. goto fail2;
  122. err = input_register_device(phampshire->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(phampshire);
  130. return err;
  131. }
  132. /*
  133. * The serio driver structure.
  134. */
  135. static struct serio_device_id hampshire_serio_ids[] = {
  136. {
  137. .type = SERIO_RS232,
  138. .proto = SERIO_HAMPSHIRE,
  139. .id = SERIO_ANY,
  140. .extra = SERIO_ANY,
  141. },
  142. { 0 }
  143. };
  144. MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);
  145. static struct serio_driver hampshire_drv = {
  146. .driver = {
  147. .name = "hampshire",
  148. },
  149. .description = DRIVER_DESC,
  150. .id_table = hampshire_serio_ids,
  151. .interrupt = hampshire_interrupt,
  152. .connect = hampshire_connect,
  153. .disconnect = hampshire_disconnect,
  154. };
  155. /*
  156. * The functions for inserting/removing us as a module.
  157. */
  158. static int __init hampshire_init(void)
  159. {
  160. return serio_register_driver(&hampshire_drv);
  161. }
  162. static void __exit hampshire_exit(void)
  163. {
  164. serio_unregister_driver(&hampshire_drv);
  165. }
  166. module_init(hampshire_init);
  167. module_exit(hampshire_exit);