mtouch.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, struct pt_regs *regs)
  54. {
  55. struct input_dev *dev = mtouch->dev;
  56. if (MTOUCH_FORMAT_TABLET_LENGTH == ++mtouch->idx) {
  57. input_regs(dev, regs);
  58. input_report_abs(dev, ABS_X, MTOUCH_GET_XC(mtouch->data));
  59. input_report_abs(dev, ABS_Y, MTOUCH_MAX_YC - MTOUCH_GET_YC(mtouch->data));
  60. input_report_key(dev, BTN_TOUCH, MTOUCH_GET_TOUCHED(mtouch->data));
  61. input_sync(dev);
  62. mtouch->idx = 0;
  63. }
  64. }
  65. static void mtouch_process_response(struct mtouch *mtouch, struct pt_regs *regs)
  66. {
  67. if (MTOUCH_RESPONSE_END_BYTE == mtouch->data[mtouch->idx++]) {
  68. /* FIXME - process response */
  69. mtouch->idx = 0;
  70. } else if (MTOUCH_MAX_LENGTH == mtouch->idx) {
  71. printk(KERN_ERR "mtouch.c: too many response bytes\n");
  72. mtouch->idx = 0;
  73. }
  74. }
  75. static irqreturn_t mtouch_interrupt(struct serio *serio,
  76. unsigned char data, unsigned int flags, struct pt_regs *regs)
  77. {
  78. struct mtouch* mtouch = serio_get_drvdata(serio);
  79. mtouch->data[mtouch->idx] = data;
  80. if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])
  81. mtouch_process_format_tablet(mtouch, regs);
  82. else if (MTOUCH_RESPONSE_BEGIN_BYTE == mtouch->data[0])
  83. mtouch_process_response(mtouch, regs);
  84. else
  85. printk(KERN_DEBUG "mtouch.c: unknown/unsynchronized data from device, byte %x\n",mtouch->data[0]);
  86. return IRQ_HANDLED;
  87. }
  88. /*
  89. * mtouch_disconnect() is the opposite of mtouch_connect()
  90. */
  91. static void mtouch_disconnect(struct serio *serio)
  92. {
  93. struct mtouch* mtouch = serio_get_drvdata(serio);
  94. input_get_device(mtouch->dev);
  95. input_unregister_device(mtouch->dev);
  96. serio_close(serio);
  97. serio_set_drvdata(serio, NULL);
  98. input_put_device(mtouch->dev);
  99. kfree(mtouch);
  100. }
  101. /*
  102. * mtouch_connect() is the routine that is called when someone adds a
  103. * new serio device that supports MicroTouch (Format Tablet) protocol and registers it as
  104. * an input device.
  105. */
  106. static int mtouch_connect(struct serio *serio, struct serio_driver *drv)
  107. {
  108. struct mtouch *mtouch;
  109. struct input_dev *input_dev;
  110. int err;
  111. mtouch = kzalloc(sizeof(struct mtouch), GFP_KERNEL);
  112. input_dev = input_allocate_device();
  113. if (!mtouch || !input_dev) {
  114. err = -ENOMEM;
  115. goto fail;
  116. }
  117. mtouch->serio = serio;
  118. mtouch->dev = input_dev;
  119. sprintf(mtouch->phys, "%s/input0", serio->phys);
  120. input_dev->private = mtouch;
  121. input_dev->name = "MicroTouch Serial TouchScreen";
  122. input_dev->phys = mtouch->phys;
  123. input_dev->id.bustype = BUS_RS232;
  124. input_dev->id.vendor = SERIO_MICROTOUCH;
  125. input_dev->id.product = 0;
  126. input_dev->id.version = 0x0100;
  127. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  128. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  129. input_set_abs_params(mtouch->dev, ABS_X, MTOUCH_MIN_XC, MTOUCH_MAX_XC, 0, 0);
  130. input_set_abs_params(mtouch->dev, ABS_Y, MTOUCH_MIN_YC, MTOUCH_MAX_YC, 0, 0);
  131. serio_set_drvdata(serio, mtouch);
  132. err = serio_open(serio, drv);
  133. if (err)
  134. goto fail;
  135. input_register_device(mtouch->dev);
  136. return 0;
  137. fail: serio_set_drvdata(serio, NULL);
  138. input_free_device(input_dev);
  139. kfree(mtouch);
  140. return err;
  141. }
  142. /*
  143. * The serio driver structure.
  144. */
  145. static struct serio_device_id mtouch_serio_ids[] = {
  146. {
  147. .type = SERIO_RS232,
  148. .proto = SERIO_MICROTOUCH,
  149. .id = SERIO_ANY,
  150. .extra = SERIO_ANY,
  151. },
  152. { 0 }
  153. };
  154. MODULE_DEVICE_TABLE(serio, mtouch_serio_ids);
  155. static struct serio_driver mtouch_drv = {
  156. .driver = {
  157. .name = "mtouch",
  158. },
  159. .description = DRIVER_DESC,
  160. .id_table = mtouch_serio_ids,
  161. .interrupt = mtouch_interrupt,
  162. .connect = mtouch_connect,
  163. .disconnect = mtouch_disconnect,
  164. };
  165. /*
  166. * The functions for inserting/removing us as a module.
  167. */
  168. static int __init mtouch_init(void)
  169. {
  170. serio_register_driver(&mtouch_drv);
  171. return 0;
  172. }
  173. static void __exit mtouch_exit(void)
  174. {
  175. serio_unregister_driver(&mtouch_drv);
  176. }
  177. module_init(mtouch_init);
  178. module_exit(mtouch_exit);