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