hampshire.c 4.7 KB

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