mtouch.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * MicroTouch (3M) serial touchscreen driver
  3. *
  4. * Copyright (c) 2004 Vojtech Pavlik
  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 by
  9. * the Free Software Foundation.
  10. */
  11. /*
  12. * 2005/02/19 Dan Streetman <ddstreet@ieee.org>
  13. * Copied elo.c and edited for MicroTouch protocol
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/input.h>
  20. #include <linux/serio.h>
  21. #include <linux/init.h>
  22. #define DRIVER_DESC "MicroTouch serial touchscreen driver"
  23. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  24. MODULE_DESCRIPTION(DRIVER_DESC);
  25. MODULE_LICENSE("GPL");
  26. /*
  27. * Definitions & global arrays.
  28. */
  29. #define MTOUCH_FORMAT_TABLET_STATUS_BIT 0x80
  30. #define MTOUCH_FORMAT_TABLET_TOUCH_BIT 0x40
  31. #define MTOUCH_FORMAT_TABLET_LENGTH 5
  32. #define MTOUCH_RESPONSE_BEGIN_BYTE 0x01
  33. #define MTOUCH_RESPONSE_END_BYTE 0x0d
  34. /* todo: check specs for max length of all responses */
  35. #define MTOUCH_MAX_LENGTH 16
  36. #define MTOUCH_MIN_XC 0
  37. #define MTOUCH_MAX_XC 0x3fff
  38. #define MTOUCH_MIN_YC 0
  39. #define MTOUCH_MAX_YC 0x3fff
  40. #define MTOUCH_GET_XC(data) (((data[2])<<7) | data[1])
  41. #define MTOUCH_GET_YC(data) (((data[4])<<7) | data[3])
  42. #define MTOUCH_GET_TOUCHED(data) (MTOUCH_FORMAT_TABLET_TOUCH_BIT & data[0])
  43. /*
  44. * Per-touchscreen data.
  45. */
  46. struct mtouch {
  47. struct input_dev *dev;
  48. struct serio *serio;
  49. int idx;
  50. unsigned char data[MTOUCH_MAX_LENGTH];
  51. char phys[32];
  52. };
  53. static void mtouch_process_format_tablet(struct mtouch *mtouch)
  54. {
  55. struct input_dev *dev = mtouch->dev;
  56. if (MTOUCH_FORMAT_TABLET_LENGTH == ++mtouch->idx) {
  57. input_report_abs(dev, ABS_X, MTOUCH_GET_XC(mtouch->data));
  58. input_report_abs(dev, ABS_Y, MTOUCH_MAX_YC - MTOUCH_GET_YC(mtouch->data));
  59. input_report_key(dev, BTN_TOUCH, MTOUCH_GET_TOUCHED(mtouch->data));
  60. input_sync(dev);
  61. mtouch->idx = 0;
  62. }
  63. }
  64. static void mtouch_process_response(struct mtouch *mtouch)
  65. {
  66. if (MTOUCH_RESPONSE_END_BYTE == mtouch->data[mtouch->idx++]) {
  67. /* FIXME - process response */
  68. mtouch->idx = 0;
  69. } else if (MTOUCH_MAX_LENGTH == mtouch->idx) {
  70. printk(KERN_ERR "mtouch.c: too many response bytes\n");
  71. mtouch->idx = 0;
  72. }
  73. }
  74. static irqreturn_t mtouch_interrupt(struct serio *serio,
  75. unsigned char data, unsigned int flags)
  76. {
  77. struct mtouch* mtouch = serio_get_drvdata(serio);
  78. mtouch->data[mtouch->idx] = data;
  79. if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])
  80. mtouch_process_format_tablet(mtouch);
  81. else if (MTOUCH_RESPONSE_BEGIN_BYTE == mtouch->data[0])
  82. mtouch_process_response(mtouch);
  83. else
  84. printk(KERN_DEBUG "mtouch.c: unknown/unsynchronized data from device, byte %x\n",mtouch->data[0]);
  85. return IRQ_HANDLED;
  86. }
  87. /*
  88. * mtouch_disconnect() is the opposite of mtouch_connect()
  89. */
  90. static void mtouch_disconnect(struct serio *serio)
  91. {
  92. struct mtouch* mtouch = serio_get_drvdata(serio);
  93. input_get_device(mtouch->dev);
  94. input_unregister_device(mtouch->dev);
  95. serio_close(serio);
  96. serio_set_drvdata(serio, NULL);
  97. input_put_device(mtouch->dev);
  98. kfree(mtouch);
  99. }
  100. /*
  101. * mtouch_connect() is the routine that is called when someone adds a
  102. * new serio device that supports MicroTouch (Format Tablet) protocol and registers it as
  103. * an input device.
  104. */
  105. static int mtouch_connect(struct serio *serio, struct serio_driver *drv)
  106. {
  107. struct mtouch *mtouch;
  108. struct input_dev *input_dev;
  109. int err;
  110. mtouch = kzalloc(sizeof(struct mtouch), GFP_KERNEL);
  111. input_dev = input_allocate_device();
  112. if (!mtouch || !input_dev) {
  113. err = -ENOMEM;
  114. goto fail1;
  115. }
  116. mtouch->serio = serio;
  117. mtouch->dev = input_dev;
  118. snprintf(mtouch->phys, sizeof(mtouch->phys), "%s/input0", serio->phys);
  119. input_dev->name = "MicroTouch Serial TouchScreen";
  120. input_dev->phys = mtouch->phys;
  121. input_dev->id.bustype = BUS_RS232;
  122. input_dev->id.vendor = SERIO_MICROTOUCH;
  123. input_dev->id.product = 0;
  124. input_dev->id.version = 0x0100;
  125. input_dev->dev.parent = &serio->dev;
  126. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  127. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  128. input_set_abs_params(mtouch->dev, ABS_X, MTOUCH_MIN_XC, MTOUCH_MAX_XC, 0, 0);
  129. input_set_abs_params(mtouch->dev, ABS_Y, MTOUCH_MIN_YC, MTOUCH_MAX_YC, 0, 0);
  130. serio_set_drvdata(serio, mtouch);
  131. err = serio_open(serio, drv);
  132. if (err)
  133. goto fail2;
  134. err = input_register_device(mtouch->dev);
  135. if (err)
  136. goto fail3;
  137. return 0;
  138. fail3: serio_close(serio);
  139. fail2: serio_set_drvdata(serio, NULL);
  140. fail1: input_free_device(input_dev);
  141. kfree(mtouch);
  142. return err;
  143. }
  144. /*
  145. * The serio driver structure.
  146. */
  147. static struct serio_device_id mtouch_serio_ids[] = {
  148. {
  149. .type = SERIO_RS232,
  150. .proto = SERIO_MICROTOUCH,
  151. .id = SERIO_ANY,
  152. .extra = SERIO_ANY,
  153. },
  154. { 0 }
  155. };
  156. MODULE_DEVICE_TABLE(serio, mtouch_serio_ids);
  157. static struct serio_driver mtouch_drv = {
  158. .driver = {
  159. .name = "mtouch",
  160. },
  161. .description = DRIVER_DESC,
  162. .id_table = mtouch_serio_ids,
  163. .interrupt = mtouch_interrupt,
  164. .connect = mtouch_connect,
  165. .disconnect = mtouch_disconnect,
  166. };
  167. /*
  168. * The functions for inserting/removing us as a module.
  169. */
  170. static int __init mtouch_init(void)
  171. {
  172. return serio_register_driver(&mtouch_drv);
  173. }
  174. static void __exit mtouch_exit(void)
  175. {
  176. serio_unregister_driver(&mtouch_drv);
  177. }
  178. module_init(mtouch_init);
  179. module_exit(mtouch_exit);