mtouch.c 4.9 KB

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