itmtouch.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /******************************************************************************
  2. * itmtouch.c -- Driver for ITM touchscreen panel
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Based upon original work by Chris Collins <xfire-itmtouch@xware.cx>.
  19. *
  20. * Kudos to ITM for providing me with the datasheet for the panel,
  21. * even though it was a day later than I had finished writing this
  22. * driver.
  23. *
  24. * It has meant that I've been able to correct my interpretation of the
  25. * protocol packets however.
  26. *
  27. * CC -- 2003/9/29
  28. *
  29. * History
  30. * 1.0 & 1.1 2003 (CC) vojtech@suse.cz
  31. * Original version for 2.4.x kernels
  32. *
  33. * 1.2 02/03/2005 (HCE) hc@mivu.no
  34. * Complete rewrite to support Linux 2.6.10, thanks to mtouchusb.c for hints.
  35. * Unfortunately no calibration support at this time.
  36. *
  37. * 1.2.1 09/03/2005 (HCE) hc@mivu.no
  38. * Code cleanup and adjusting syntax to start matching kernel standards
  39. *
  40. * 1.2.2 10/05/2006 (MJA) massad@gmail.com
  41. * Flag for detecting if the screen was being touch was incorrectly
  42. * inverted, so no touch events were being detected.
  43. *
  44. *****************************************************************************/
  45. #include <linux/kernel.h>
  46. #include <linux/slab.h>
  47. #include <linux/module.h>
  48. #include <linux/init.h>
  49. #include <linux/usb/input.h>
  50. /* only an 8 byte buffer necessary for a single packet */
  51. #define ITM_BUFSIZE 8
  52. #define PATH_SIZE 64
  53. #define USB_VENDOR_ID_ITMINC 0x0403
  54. #define USB_PRODUCT_ID_TOUCHPANEL 0xf9e9
  55. #define DRIVER_AUTHOR "Hans-Christian Egtvedt <hc@mivu.no>"
  56. #define DRIVER_VERSION "v1.2.2"
  57. #define DRIVER_DESC "USB ITM Inc Touch Panel Driver"
  58. #define DRIVER_LICENSE "GPL"
  59. MODULE_AUTHOR( DRIVER_AUTHOR );
  60. MODULE_DESCRIPTION( DRIVER_DESC );
  61. MODULE_LICENSE( DRIVER_LICENSE );
  62. struct itmtouch_dev {
  63. struct usb_device *usbdev; /* usb device */
  64. struct input_dev *inputdev; /* input device */
  65. struct urb *readurb; /* urb */
  66. char rbuf[ITM_BUFSIZE]; /* data */
  67. int users;
  68. char name[128];
  69. char phys[64];
  70. };
  71. static struct usb_device_id itmtouch_ids [] = {
  72. { USB_DEVICE(USB_VENDOR_ID_ITMINC, USB_PRODUCT_ID_TOUCHPANEL) },
  73. { }
  74. };
  75. static void itmtouch_irq(struct urb *urb, struct pt_regs *regs)
  76. {
  77. struct itmtouch_dev *itmtouch = urb->context;
  78. unsigned char *data = urb->transfer_buffer;
  79. struct input_dev *dev = itmtouch->inputdev;
  80. int retval;
  81. switch (urb->status) {
  82. case 0:
  83. /* success */
  84. break;
  85. case -ETIME:
  86. /* this urb is timing out */
  87. dbg("%s - urb timed out - was the device unplugged?",
  88. __FUNCTION__);
  89. return;
  90. case -ECONNRESET:
  91. case -ENOENT:
  92. case -ESHUTDOWN:
  93. /* this urb is terminated, clean up */
  94. dbg("%s - urb shutting down with status: %d",
  95. __FUNCTION__, urb->status);
  96. return;
  97. default:
  98. dbg("%s - nonzero urb status received: %d",
  99. __FUNCTION__, urb->status);
  100. goto exit;
  101. }
  102. input_regs(dev, regs);
  103. /* if pressure has been released, then don't report X/Y */
  104. if (!(data[7] & 0x20)) {
  105. input_report_abs(dev, ABS_X, (data[0] & 0x1F) << 7 | (data[3] & 0x7F));
  106. input_report_abs(dev, ABS_Y, (data[1] & 0x1F) << 7 | (data[4] & 0x7F));
  107. }
  108. input_report_abs(dev, ABS_PRESSURE, (data[2] & 1) << 7 | (data[5] & 0x7F));
  109. input_report_key(dev, BTN_TOUCH, ~data[7] & 0x20);
  110. input_sync(dev);
  111. exit:
  112. retval = usb_submit_urb (urb, GFP_ATOMIC);
  113. if (retval)
  114. printk(KERN_ERR "%s - usb_submit_urb failed with result: %d",
  115. __FUNCTION__, retval);
  116. }
  117. static int itmtouch_open(struct input_dev *input)
  118. {
  119. struct itmtouch_dev *itmtouch = input->private;
  120. itmtouch->readurb->dev = itmtouch->usbdev;
  121. if (usb_submit_urb(itmtouch->readurb, GFP_KERNEL))
  122. return -EIO;
  123. return 0;
  124. }
  125. static void itmtouch_close(struct input_dev *input)
  126. {
  127. struct itmtouch_dev *itmtouch = input->private;
  128. usb_kill_urb(itmtouch->readurb);
  129. }
  130. static int itmtouch_probe(struct usb_interface *intf, const struct usb_device_id *id)
  131. {
  132. struct itmtouch_dev *itmtouch;
  133. struct input_dev *input_dev;
  134. struct usb_host_interface *interface;
  135. struct usb_endpoint_descriptor *endpoint;
  136. struct usb_device *udev = interface_to_usbdev(intf);
  137. unsigned int pipe;
  138. unsigned int maxp;
  139. interface = intf->cur_altsetting;
  140. endpoint = &interface->endpoint[0].desc;
  141. itmtouch = kzalloc(sizeof(struct itmtouch_dev), GFP_KERNEL);
  142. input_dev = input_allocate_device();
  143. if (!itmtouch || !input_dev) {
  144. err("%s - Out of memory.", __FUNCTION__);
  145. goto fail;
  146. }
  147. itmtouch->usbdev = udev;
  148. itmtouch->inputdev = input_dev;
  149. if (udev->manufacturer)
  150. strlcpy(itmtouch->name, udev->manufacturer, sizeof(itmtouch->name));
  151. if (udev->product) {
  152. if (udev->manufacturer)
  153. strlcat(itmtouch->name, " ", sizeof(itmtouch->name));
  154. strlcat(itmtouch->name, udev->product, sizeof(itmtouch->name));
  155. }
  156. if (!strlen(itmtouch->name))
  157. sprintf(itmtouch->name, "USB ITM touchscreen");
  158. usb_make_path(udev, itmtouch->phys, sizeof(itmtouch->phys));
  159. strlcpy(itmtouch->phys, "/input0", sizeof(itmtouch->phys));
  160. input_dev->name = itmtouch->name;
  161. input_dev->phys = itmtouch->phys;
  162. usb_to_input_id(udev, &input_dev->id);
  163. input_dev->cdev.dev = &intf->dev;
  164. input_dev->private = itmtouch;
  165. input_dev->open = itmtouch_open;
  166. input_dev->close = itmtouch_close;
  167. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  168. input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
  169. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  170. /* device limits */
  171. /* as specified by the ITM datasheet, X and Y are 12bit,
  172. * Z (pressure) is 8 bit. However, the fields are defined up
  173. * to 14 bits for future possible expansion.
  174. */
  175. input_set_abs_params(input_dev, ABS_X, 0, 0x0FFF, 2, 0);
  176. input_set_abs_params(input_dev, ABS_Y, 0, 0x0FFF, 2, 0);
  177. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xFF, 2, 0);
  178. /* initialise the URB so we can read from the transport stream */
  179. pipe = usb_rcvintpipe(itmtouch->usbdev, endpoint->bEndpointAddress);
  180. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  181. if (maxp > ITM_BUFSIZE)
  182. maxp = ITM_BUFSIZE;
  183. itmtouch->readurb = usb_alloc_urb(0, GFP_KERNEL);
  184. if (!itmtouch->readurb) {
  185. dbg("%s - usb_alloc_urb failed: itmtouch->readurb", __FUNCTION__);
  186. goto fail;
  187. }
  188. usb_fill_int_urb(itmtouch->readurb, itmtouch->usbdev, pipe, itmtouch->rbuf,
  189. maxp, itmtouch_irq, itmtouch, endpoint->bInterval);
  190. input_register_device(itmtouch->inputdev);
  191. usb_set_intfdata(intf, itmtouch);
  192. return 0;
  193. fail: input_free_device(input_dev);
  194. kfree(itmtouch);
  195. return -ENOMEM;
  196. }
  197. static void itmtouch_disconnect(struct usb_interface *intf)
  198. {
  199. struct itmtouch_dev *itmtouch = usb_get_intfdata(intf);
  200. usb_set_intfdata(intf, NULL);
  201. if (itmtouch) {
  202. input_unregister_device(itmtouch->inputdev);
  203. usb_kill_urb(itmtouch->readurb);
  204. usb_free_urb(itmtouch->readurb);
  205. kfree(itmtouch);
  206. }
  207. }
  208. MODULE_DEVICE_TABLE(usb, itmtouch_ids);
  209. static struct usb_driver itmtouch_driver = {
  210. .name = "itmtouch",
  211. .probe = itmtouch_probe,
  212. .disconnect = itmtouch_disconnect,
  213. .id_table = itmtouch_ids,
  214. };
  215. static int __init itmtouch_init(void)
  216. {
  217. info(DRIVER_DESC " " DRIVER_VERSION);
  218. info(DRIVER_AUTHOR);
  219. return usb_register(&itmtouch_driver);
  220. }
  221. static void __exit itmtouch_exit(void)
  222. {
  223. usb_deregister(&itmtouch_driver);
  224. }
  225. module_init(itmtouch_init);
  226. module_exit(itmtouch_exit);