mtouchusb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /******************************************************************************
  2. * mtouchusb.c -- Driver for Microtouch (Now 3M) USB Touchscreens
  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 Radoslaw Garbacz (usb-support@ite.pl)
  19. * (http://freshmeat.net/projects/3mtouchscreendriver)
  20. *
  21. * History
  22. *
  23. * 0.3 & 0.4 2002 (TEJ) tejohnson@yahoo.com
  24. * Updated to 2.4.18, then 2.4.19
  25. * Old version still relied on stealing a minor
  26. *
  27. * 0.5 02/26/2004 (TEJ) tejohnson@yahoo.com
  28. * Complete rewrite using Linux Input in 2.6.3
  29. * Unfortunately no calibration support at this time
  30. *
  31. * 1.4 04/25/2004 (TEJ) tejohnson@yahoo.com
  32. * Changed reset from standard USB dev reset to vendor reset
  33. * Changed data sent to host from compensated to raw coordinates
  34. * Eliminated vendor/product module params
  35. * Performed multiple successful tests with an EXII-5010UC
  36. *
  37. * 1.5 02/27/2005 ddstreet@ieee.org
  38. * Added module parameter to select raw or hw-calibrated coordinate reporting
  39. *
  40. *****************************************************************************/
  41. #include <linux/config.h>
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <linux/usb/input.h>
  47. #define MTOUCHUSB_MIN_XC 0x0
  48. #define MTOUCHUSB_MAX_RAW_XC 0x4000
  49. #define MTOUCHUSB_MAX_CALIB_XC 0xffff
  50. #define MTOUCHUSB_XC_FUZZ 0x0
  51. #define MTOUCHUSB_XC_FLAT 0x0
  52. #define MTOUCHUSB_MIN_YC 0x0
  53. #define MTOUCHUSB_MAX_RAW_YC 0x4000
  54. #define MTOUCHUSB_MAX_CALIB_YC 0xffff
  55. #define MTOUCHUSB_YC_FUZZ 0x0
  56. #define MTOUCHUSB_YC_FLAT 0x0
  57. #define MTOUCHUSB_ASYNC_REPORT 1
  58. #define MTOUCHUSB_RESET 7
  59. #define MTOUCHUSB_REPORT_DATA_SIZE 11
  60. #define MTOUCHUSB_REQ_CTRLLR_ID 10
  61. #define MTOUCHUSB_GET_RAW_XC(data) (data[8]<<8 | data[7])
  62. #define MTOUCHUSB_GET_CALIB_XC(data) (data[4]<<8 | data[3])
  63. #define MTOUCHUSB_GET_RAW_YC(data) (data[10]<<8 | data[9])
  64. #define MTOUCHUSB_GET_CALIB_YC(data) (data[6]<<8 | data[5])
  65. #define MTOUCHUSB_GET_XC(data) (raw_coordinates ? \
  66. MTOUCHUSB_GET_RAW_XC(data) : \
  67. MTOUCHUSB_GET_CALIB_XC(data))
  68. #define MTOUCHUSB_GET_YC(data) (raw_coordinates ? \
  69. MTOUCHUSB_GET_RAW_YC(data) : \
  70. MTOUCHUSB_GET_CALIB_YC(data))
  71. #define MTOUCHUSB_GET_TOUCHED(data) ((data[2] & 0x40) ? 1:0)
  72. #define DRIVER_VERSION "v1.5"
  73. #define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
  74. #define DRIVER_DESC "3M USB Touchscreen Driver"
  75. #define DRIVER_LICENSE "GPL"
  76. static int raw_coordinates = 1;
  77. module_param(raw_coordinates, bool, S_IRUGO | S_IWUSR);
  78. MODULE_PARM_DESC(raw_coordinates, "report raw coordinate values (y, default) or hardware-calibrated coordinate values (n)");
  79. struct mtouch_usb {
  80. unsigned char *data;
  81. dma_addr_t data_dma;
  82. struct urb *irq;
  83. struct usb_device *udev;
  84. struct input_dev *input;
  85. char name[128];
  86. char phys[64];
  87. };
  88. static struct usb_device_id mtouchusb_devices[] = {
  89. { USB_DEVICE(0x0596, 0x0001) },
  90. { }
  91. };
  92. static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
  93. {
  94. struct mtouch_usb *mtouch = urb->context;
  95. int retval;
  96. switch (urb->status) {
  97. case 0:
  98. /* success */
  99. break;
  100. case -ETIMEDOUT:
  101. /* this urb is timing out */
  102. dbg("%s - urb timed out - was the device unplugged?",
  103. __FUNCTION__);
  104. return;
  105. case -ECONNRESET:
  106. case -ENOENT:
  107. case -ESHUTDOWN:
  108. /* this urb is terminated, clean up */
  109. dbg("%s - urb shutting down with status: %d",
  110. __FUNCTION__, urb->status);
  111. return;
  112. default:
  113. dbg("%s - nonzero urb status received: %d",
  114. __FUNCTION__, urb->status);
  115. goto exit;
  116. }
  117. input_regs(mtouch->input, regs);
  118. input_report_key(mtouch->input, BTN_TOUCH,
  119. MTOUCHUSB_GET_TOUCHED(mtouch->data));
  120. input_report_abs(mtouch->input, ABS_X, MTOUCHUSB_GET_XC(mtouch->data));
  121. input_report_abs(mtouch->input, ABS_Y,
  122. (raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC)
  123. - MTOUCHUSB_GET_YC(mtouch->data));
  124. input_sync(mtouch->input);
  125. exit:
  126. retval = usb_submit_urb(urb, GFP_ATOMIC);
  127. if (retval)
  128. err("%s - usb_submit_urb failed with result: %d",
  129. __FUNCTION__, retval);
  130. }
  131. static int mtouchusb_open(struct input_dev *input)
  132. {
  133. struct mtouch_usb *mtouch = input->private;
  134. mtouch->irq->dev = mtouch->udev;
  135. if (usb_submit_urb(mtouch->irq, GFP_ATOMIC))
  136. return -EIO;
  137. return 0;
  138. }
  139. static void mtouchusb_close(struct input_dev *input)
  140. {
  141. struct mtouch_usb *mtouch = input->private;
  142. usb_kill_urb(mtouch->irq);
  143. }
  144. static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
  145. {
  146. dbg("%s - called", __FUNCTION__);
  147. mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
  148. SLAB_ATOMIC, &mtouch->data_dma);
  149. if (!mtouch->data)
  150. return -1;
  151. return 0;
  152. }
  153. static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
  154. {
  155. dbg("%s - called", __FUNCTION__);
  156. if (mtouch->data)
  157. usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
  158. mtouch->data, mtouch->data_dma);
  159. }
  160. static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  161. {
  162. struct mtouch_usb *mtouch;
  163. struct input_dev *input_dev;
  164. struct usb_host_interface *interface;
  165. struct usb_endpoint_descriptor *endpoint;
  166. struct usb_device *udev = interface_to_usbdev(intf);
  167. int nRet;
  168. dbg("%s - called", __FUNCTION__);
  169. dbg("%s - setting interface", __FUNCTION__);
  170. interface = intf->cur_altsetting;
  171. dbg("%s - setting endpoint", __FUNCTION__);
  172. endpoint = &interface->endpoint[0].desc;
  173. mtouch = kzalloc(sizeof(struct mtouch_usb), GFP_KERNEL);
  174. input_dev = input_allocate_device();
  175. if (!mtouch || !input_dev) {
  176. err("%s - Out of memory.", __FUNCTION__);
  177. goto fail1;
  178. }
  179. dbg("%s - allocating buffers", __FUNCTION__);
  180. if (mtouchusb_alloc_buffers(udev, mtouch))
  181. goto fail2;
  182. mtouch->udev = udev;
  183. mtouch->input = input_dev;
  184. if (udev->manufacturer)
  185. strlcpy(mtouch->name, udev->manufacturer, sizeof(mtouch->name));
  186. if (udev->product) {
  187. if (udev->manufacturer)
  188. strlcat(mtouch->name, " ", sizeof(mtouch->name));
  189. strlcat(mtouch->name, udev->product, sizeof(mtouch->name));
  190. }
  191. if (!strlen(mtouch->name))
  192. snprintf(mtouch->name, sizeof(mtouch->name),
  193. "USB Touchscreen %04x:%04x",
  194. le16_to_cpu(udev->descriptor.idVendor),
  195. le16_to_cpu(udev->descriptor.idProduct));
  196. usb_make_path(udev, mtouch->phys, sizeof(mtouch->phys));
  197. strlcpy(mtouch->phys, "/input0", sizeof(mtouch->phys));
  198. input_dev->name = mtouch->name;
  199. input_dev->phys = mtouch->phys;
  200. usb_to_input_id(udev, &input_dev->id);
  201. input_dev->cdev.dev = &intf->dev;
  202. input_dev->private = mtouch;
  203. input_dev->open = mtouchusb_open;
  204. input_dev->close = mtouchusb_close;
  205. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  206. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  207. input_set_abs_params(input_dev, ABS_X, MTOUCHUSB_MIN_XC,
  208. raw_coordinates ? MTOUCHUSB_MAX_RAW_XC : MTOUCHUSB_MAX_CALIB_XC,
  209. MTOUCHUSB_XC_FUZZ, MTOUCHUSB_XC_FLAT);
  210. input_set_abs_params(input_dev, ABS_Y, MTOUCHUSB_MIN_YC,
  211. raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC,
  212. MTOUCHUSB_YC_FUZZ, MTOUCHUSB_YC_FLAT);
  213. nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
  214. MTOUCHUSB_RESET,
  215. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  216. 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  217. dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
  218. __FUNCTION__, nRet);
  219. dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
  220. mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  221. if (!mtouch->irq) {
  222. dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
  223. goto fail2;
  224. }
  225. dbg("%s - usb_fill_int_urb", __FUNCTION__);
  226. usb_fill_int_urb(mtouch->irq, mtouch->udev,
  227. usb_rcvintpipe(mtouch->udev, 0x81),
  228. mtouch->data, MTOUCHUSB_REPORT_DATA_SIZE,
  229. mtouchusb_irq, mtouch, endpoint->bInterval);
  230. dbg("%s - input_register_device", __FUNCTION__);
  231. input_register_device(mtouch->input);
  232. nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
  233. MTOUCHUSB_ASYNC_REPORT,
  234. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  235. 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
  236. dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
  237. __FUNCTION__, nRet);
  238. usb_set_intfdata(intf, mtouch);
  239. return 0;
  240. fail2: mtouchusb_free_buffers(udev, mtouch);
  241. fail1: input_free_device(input_dev);
  242. kfree(mtouch);
  243. return -ENOMEM;
  244. }
  245. static void mtouchusb_disconnect(struct usb_interface *intf)
  246. {
  247. struct mtouch_usb *mtouch = usb_get_intfdata(intf);
  248. dbg("%s - called", __FUNCTION__);
  249. usb_set_intfdata(intf, NULL);
  250. if (mtouch) {
  251. dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
  252. usb_kill_urb(mtouch->irq);
  253. input_unregister_device(mtouch->input);
  254. usb_free_urb(mtouch->irq);
  255. mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
  256. kfree(mtouch);
  257. }
  258. }
  259. MODULE_DEVICE_TABLE(usb, mtouchusb_devices);
  260. static struct usb_driver mtouchusb_driver = {
  261. .name = "mtouchusb",
  262. .probe = mtouchusb_probe,
  263. .disconnect = mtouchusb_disconnect,
  264. .id_table = mtouchusb_devices,
  265. };
  266. static int __init mtouchusb_init(void)
  267. {
  268. dbg("%s - called", __FUNCTION__);
  269. return usb_register(&mtouchusb_driver);
  270. }
  271. static void __exit mtouchusb_cleanup(void)
  272. {
  273. dbg("%s - called", __FUNCTION__);
  274. usb_deregister(&mtouchusb_driver);
  275. }
  276. module_init(mtouchusb_init);
  277. module_exit(mtouchusb_cleanup);
  278. MODULE_AUTHOR(DRIVER_AUTHOR);
  279. MODULE_DESCRIPTION(DRIVER_DESC);
  280. MODULE_LICENSE("GPL");