itmtouch.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. *****************************************************************************/
  41. #include <linux/config.h>
  42. #ifdef CONFIG_USB_DEBUG
  43. #define DEBUG
  44. #else
  45. #undef DEBUG
  46. #endif
  47. #include <linux/kernel.h>
  48. #include <linux/slab.h>
  49. #include <linux/input.h>
  50. #include <linux/module.h>
  51. #include <linux/init.h>
  52. #include <linux/usb.h>
  53. /* only an 8 byte buffer necessary for a single packet */
  54. #define ITM_BUFSIZE 8
  55. #define PATH_SIZE 64
  56. #define USB_VENDOR_ID_ITMINC 0x0403
  57. #define USB_PRODUCT_ID_TOUCHPANEL 0xf9e9
  58. #define DRIVER_AUTHOR "Hans-Christian Egtvedt <hc@mivu.no>"
  59. #define DRIVER_VERSION "v1.2.1"
  60. #define DRIVER_DESC "USB ITM Inc Touch Panel Driver"
  61. #define DRIVER_LICENSE "GPL"
  62. MODULE_AUTHOR( DRIVER_AUTHOR );
  63. MODULE_DESCRIPTION( DRIVER_DESC );
  64. MODULE_LICENSE( DRIVER_LICENSE );
  65. struct itmtouch_dev {
  66. struct usb_device *usbdev; /* usb device */
  67. struct input_dev inputdev; /* input device */
  68. struct urb *readurb; /* urb */
  69. char rbuf[ITM_BUFSIZE]; /* data */
  70. int users;
  71. char name[128];
  72. char phys[64];
  73. };
  74. static struct usb_device_id itmtouch_ids [] = {
  75. { USB_DEVICE(USB_VENDOR_ID_ITMINC, USB_PRODUCT_ID_TOUCHPANEL) },
  76. { }
  77. };
  78. static void itmtouch_irq(struct urb *urb, struct pt_regs *regs)
  79. {
  80. struct itmtouch_dev * itmtouch = urb->context;
  81. unsigned char *data = urb->transfer_buffer;
  82. struct input_dev *dev = &itmtouch->inputdev;
  83. int retval;
  84. switch (urb->status) {
  85. case 0:
  86. /* success */
  87. break;
  88. case -ETIMEDOUT:
  89. /* this urb is timing out */
  90. dbg("%s - urb timed out - was the device unplugged?",
  91. __FUNCTION__);
  92. return;
  93. case -ECONNRESET:
  94. case -ENOENT:
  95. case -ESHUTDOWN:
  96. /* this urb is terminated, clean up */
  97. dbg("%s - urb shutting down with status: %d",
  98. __FUNCTION__, urb->status);
  99. return;
  100. default:
  101. dbg("%s - nonzero urb status received: %d",
  102. __FUNCTION__, urb->status);
  103. goto exit;
  104. }
  105. input_regs(dev, regs);
  106. /* if pressure has been released, then don't report X/Y */
  107. if (data[7] & 0x20) {
  108. input_report_abs(dev, ABS_X, (data[0] & 0x1F) << 7 | (data[3] & 0x7F));
  109. input_report_abs(dev, ABS_Y, (data[1] & 0x1F) << 7 | (data[4] & 0x7F));
  110. }
  111. input_report_abs(dev, ABS_PRESSURE, (data[2] & 1) << 7 | (data[5] & 0x7F));
  112. input_report_key(dev, BTN_TOUCH, ~data[7] & 0x20);
  113. input_sync(dev);
  114. exit:
  115. retval = usb_submit_urb (urb, GFP_ATOMIC);
  116. if (retval)
  117. printk(KERN_ERR "%s - usb_submit_urb failed with result: %d",
  118. __FUNCTION__, retval);
  119. }
  120. static int itmtouch_open(struct input_dev *input)
  121. {
  122. struct itmtouch_dev *itmtouch = input->private;
  123. itmtouch->readurb->dev = itmtouch->usbdev;
  124. if (usb_submit_urb(itmtouch->readurb, GFP_KERNEL))
  125. return -EIO;
  126. return 0;
  127. }
  128. static void itmtouch_close(struct input_dev *input)
  129. {
  130. struct itmtouch_dev *itmtouch = input->private;
  131. usb_kill_urb(itmtouch->readurb);
  132. }
  133. static int itmtouch_probe(struct usb_interface *intf, const struct usb_device_id *id)
  134. {
  135. struct itmtouch_dev *itmtouch;
  136. struct usb_host_interface *interface;
  137. struct usb_endpoint_descriptor *endpoint;
  138. struct usb_device *udev = interface_to_usbdev(intf);
  139. unsigned int pipe;
  140. unsigned int maxp;
  141. char path[PATH_SIZE];
  142. interface = intf->cur_altsetting;
  143. endpoint = &interface->endpoint[0].desc;
  144. if (!(itmtouch = kcalloc(1, sizeof(struct itmtouch_dev), GFP_KERNEL))) {
  145. err("%s - Out of memory.", __FUNCTION__);
  146. return -ENOMEM;
  147. }
  148. itmtouch->usbdev = udev;
  149. itmtouch->inputdev.private = itmtouch;
  150. itmtouch->inputdev.open = itmtouch_open;
  151. itmtouch->inputdev.close = itmtouch_close;
  152. usb_make_path(udev, path, PATH_SIZE);
  153. itmtouch->inputdev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  154. itmtouch->inputdev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
  155. itmtouch->inputdev.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  156. itmtouch->inputdev.name = itmtouch->name;
  157. itmtouch->inputdev.phys = itmtouch->phys;
  158. itmtouch->inputdev.id.bustype = BUS_USB;
  159. itmtouch->inputdev.id.vendor = udev->descriptor.idVendor;
  160. itmtouch->inputdev.id.product = udev->descriptor.idProduct;
  161. itmtouch->inputdev.id.version = udev->descriptor.bcdDevice;
  162. itmtouch->inputdev.dev = &intf->dev;
  163. if (!strlen(itmtouch->name))
  164. sprintf(itmtouch->name, "USB ITM touchscreen");
  165. /* device limits */
  166. /* as specified by the ITM datasheet, X and Y are 12bit,
  167. * Z (pressure) is 8 bit. However, the fields are defined up
  168. * to 14 bits for future possible expansion.
  169. */
  170. input_set_abs_params(&itmtouch->inputdev, ABS_X, 0, 0x0FFF, 2, 0);
  171. input_set_abs_params(&itmtouch->inputdev, ABS_Y, 0, 0x0FFF, 2, 0);
  172. input_set_abs_params(&itmtouch->inputdev, ABS_PRESSURE, 0, 0xFF, 2, 0);
  173. /* initialise the URB so we can read from the transport stream */
  174. pipe = usb_rcvintpipe(itmtouch->usbdev, endpoint->bEndpointAddress);
  175. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  176. if (maxp > ITM_BUFSIZE)
  177. maxp = ITM_BUFSIZE;
  178. itmtouch->readurb = usb_alloc_urb(0, GFP_KERNEL);
  179. if (!itmtouch->readurb) {
  180. dbg("%s - usb_alloc_urb failed: itmtouch->readurb", __FUNCTION__);
  181. kfree(itmtouch);
  182. return -ENOMEM;
  183. }
  184. usb_fill_int_urb(itmtouch->readurb, itmtouch->usbdev, pipe, itmtouch->rbuf,
  185. maxp, itmtouch_irq, itmtouch, endpoint->bInterval);
  186. input_register_device(&itmtouch->inputdev);
  187. printk(KERN_INFO "itmtouch: %s registered on %s\n", itmtouch->name, path);
  188. usb_set_intfdata(intf, itmtouch);
  189. return 0;
  190. }
  191. static void itmtouch_disconnect(struct usb_interface *intf)
  192. {
  193. struct itmtouch_dev *itmtouch = usb_get_intfdata(intf);
  194. usb_set_intfdata(intf, NULL);
  195. if (itmtouch) {
  196. input_unregister_device(&itmtouch->inputdev);
  197. usb_kill_urb(itmtouch->readurb);
  198. usb_free_urb(itmtouch->readurb);
  199. kfree(itmtouch);
  200. }
  201. }
  202. MODULE_DEVICE_TABLE(usb, itmtouch_ids);
  203. static struct usb_driver itmtouch_driver = {
  204. .owner = THIS_MODULE,
  205. .name = "itmtouch",
  206. .probe = itmtouch_probe,
  207. .disconnect = itmtouch_disconnect,
  208. .id_table = itmtouch_ids,
  209. };
  210. static int __init itmtouch_init(void)
  211. {
  212. info(DRIVER_DESC " " DRIVER_VERSION);
  213. info(DRIVER_AUTHOR);
  214. return usb_register(&itmtouch_driver);
  215. }
  216. static void __exit itmtouch_exit(void)
  217. {
  218. usb_deregister(&itmtouch_driver);
  219. }
  220. module_init(itmtouch_init);
  221. module_exit(itmtouch_exit);