mtouchusb.c 13 KB

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