mtouchusb.c 10 KB

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