touchkitusb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /******************************************************************************
  2. * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens
  3. *
  4. * Copyright (C) 2004-2005 by Daniel Ritz <daniel.ritz@gmx.ch>
  5. * Copyright (C) by Todd E. Johnson (mtouchusb.c)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * Based upon mtouchusb.c
  22. *
  23. *****************************************************************************/
  24. //#define DEBUG
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/usb/input.h>
  30. #define TOUCHKIT_MIN_XC 0x0
  31. #define TOUCHKIT_MAX_XC 0x07ff
  32. #define TOUCHKIT_XC_FUZZ 0x0
  33. #define TOUCHKIT_XC_FLAT 0x0
  34. #define TOUCHKIT_MIN_YC 0x0
  35. #define TOUCHKIT_MAX_YC 0x07ff
  36. #define TOUCHKIT_YC_FUZZ 0x0
  37. #define TOUCHKIT_YC_FLAT 0x0
  38. #define TOUCHKIT_REPORT_DATA_SIZE 16
  39. #define TOUCHKIT_DOWN 0x01
  40. #define TOUCHKIT_PKT_TYPE_MASK 0xFE
  41. #define TOUCHKIT_PKT_TYPE_REPT 0x80
  42. #define TOUCHKIT_PKT_TYPE_DIAG 0x0A
  43. #define DRIVER_VERSION "v0.1"
  44. #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
  45. #define DRIVER_DESC "eGalax TouchKit USB HID Touchscreen Driver"
  46. static int swap_xy;
  47. module_param(swap_xy, bool, 0644);
  48. MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
  49. struct touchkit_usb {
  50. unsigned char *data;
  51. dma_addr_t data_dma;
  52. char buffer[TOUCHKIT_REPORT_DATA_SIZE];
  53. int buf_len;
  54. struct urb *irq;
  55. struct usb_device *udev;
  56. struct input_dev *input;
  57. char name[128];
  58. char phys[64];
  59. };
  60. static struct usb_device_id touchkit_devices[] = {
  61. {USB_DEVICE(0x3823, 0x0001)},
  62. {USB_DEVICE(0x0123, 0x0001)},
  63. {USB_DEVICE(0x0eef, 0x0001)},
  64. {USB_DEVICE(0x0eef, 0x0002)},
  65. {}
  66. };
  67. /* helpers to read the data */
  68. static inline int touchkit_get_touched(char *data)
  69. {
  70. return (data[0] & TOUCHKIT_DOWN) ? 1 : 0;
  71. }
  72. static inline int touchkit_get_x(char *data)
  73. {
  74. return ((data[3] & 0x0F) << 7) | (data[4] & 0x7F);
  75. }
  76. static inline int touchkit_get_y(char *data)
  77. {
  78. return ((data[1] & 0x0F) << 7) | (data[2] & 0x7F);
  79. }
  80. /* processes one input packet. */
  81. static void touchkit_process_pkt(struct touchkit_usb *touchkit, char *pkt)
  82. {
  83. int x, y;
  84. /* only process report packets */
  85. if ((pkt[0] & TOUCHKIT_PKT_TYPE_MASK) != TOUCHKIT_PKT_TYPE_REPT)
  86. return;
  87. if (swap_xy) {
  88. y = touchkit_get_x(pkt);
  89. x = touchkit_get_y(pkt);
  90. } else {
  91. x = touchkit_get_x(pkt);
  92. y = touchkit_get_y(pkt);
  93. }
  94. input_report_key(touchkit->input, BTN_TOUCH, touchkit_get_touched(pkt));
  95. input_report_abs(touchkit->input, ABS_X, x);
  96. input_report_abs(touchkit->input, ABS_Y, y);
  97. input_sync(touchkit->input);
  98. }
  99. static int touchkit_get_pkt_len(char *buf)
  100. {
  101. switch (buf[0] & TOUCHKIT_PKT_TYPE_MASK) {
  102. case TOUCHKIT_PKT_TYPE_REPT:
  103. return 5;
  104. case TOUCHKIT_PKT_TYPE_DIAG:
  105. return buf[1] + 2;
  106. }
  107. return 0;
  108. }
  109. static void touchkit_process(struct touchkit_usb *touchkit, int len)
  110. {
  111. char *buffer;
  112. int pkt_len, buf_len, pos;
  113. /* if the buffer contains data, append */
  114. if (unlikely(touchkit->buf_len)) {
  115. int tmp;
  116. /* if only 1 byte in buffer, add another one to get length */
  117. if (touchkit->buf_len == 1)
  118. touchkit->buffer[1] = touchkit->data[0];
  119. pkt_len = touchkit_get_pkt_len(touchkit->buffer);
  120. /* unknown packet: drop everything */
  121. if (!pkt_len)
  122. return;
  123. /* append, process */
  124. tmp = pkt_len - touchkit->buf_len;
  125. memcpy(touchkit->buffer + touchkit->buf_len, touchkit->data, tmp);
  126. touchkit_process_pkt(touchkit, touchkit->buffer);
  127. buffer = touchkit->data + tmp;
  128. buf_len = len - tmp;
  129. } else {
  130. buffer = touchkit->data;
  131. buf_len = len;
  132. }
  133. /* only one byte left in buffer */
  134. if (unlikely(buf_len == 1)) {
  135. touchkit->buffer[0] = buffer[0];
  136. touchkit->buf_len = 1;
  137. return;
  138. }
  139. /* loop over the buffer */
  140. pos = 0;
  141. while (pos < buf_len) {
  142. /* get packet len */
  143. pkt_len = touchkit_get_pkt_len(buffer + pos);
  144. /* unknown packet: drop everything */
  145. if (unlikely(!pkt_len))
  146. return;
  147. /* full packet: process */
  148. if (likely(pkt_len <= buf_len)) {
  149. touchkit_process_pkt(touchkit, buffer + pos);
  150. } else {
  151. /* incomplete packet: save in buffer */
  152. memcpy(touchkit->buffer, buffer + pos, buf_len - pos);
  153. touchkit->buf_len = buf_len - pos;
  154. }
  155. pos += pkt_len;
  156. }
  157. }
  158. static void touchkit_irq(struct urb *urb)
  159. {
  160. struct touchkit_usb *touchkit = urb->context;
  161. int retval;
  162. switch (urb->status) {
  163. case 0:
  164. /* success */
  165. break;
  166. case -ETIME:
  167. /* this urb is timing out */
  168. dbg("%s - urb timed out - was the device unplugged?",
  169. __FUNCTION__);
  170. return;
  171. case -ECONNRESET:
  172. case -ENOENT:
  173. case -ESHUTDOWN:
  174. /* this urb is terminated, clean up */
  175. dbg("%s - urb shutting down with status: %d",
  176. __FUNCTION__, urb->status);
  177. return;
  178. default:
  179. dbg("%s - nonzero urb status received: %d",
  180. __FUNCTION__, urb->status);
  181. goto exit;
  182. }
  183. touchkit_process(touchkit, urb->actual_length);
  184. exit:
  185. retval = usb_submit_urb(urb, GFP_ATOMIC);
  186. if (retval)
  187. err("%s - usb_submit_urb failed with result: %d",
  188. __FUNCTION__, retval);
  189. }
  190. static int touchkit_open(struct input_dev *input)
  191. {
  192. struct touchkit_usb *touchkit = input->private;
  193. touchkit->irq->dev = touchkit->udev;
  194. if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
  195. return -EIO;
  196. return 0;
  197. }
  198. static void touchkit_close(struct input_dev *input)
  199. {
  200. struct touchkit_usb *touchkit = input->private;
  201. usb_kill_urb(touchkit->irq);
  202. }
  203. static int touchkit_alloc_buffers(struct usb_device *udev,
  204. struct touchkit_usb *touchkit)
  205. {
  206. touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
  207. SLAB_ATOMIC, &touchkit->data_dma);
  208. if (!touchkit->data)
  209. return -1;
  210. return 0;
  211. }
  212. static void touchkit_free_buffers(struct usb_device *udev,
  213. struct touchkit_usb *touchkit)
  214. {
  215. if (touchkit->data)
  216. usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
  217. touchkit->data, touchkit->data_dma);
  218. }
  219. static int touchkit_probe(struct usb_interface *intf,
  220. const struct usb_device_id *id)
  221. {
  222. struct touchkit_usb *touchkit;
  223. struct input_dev *input_dev;
  224. struct usb_host_interface *interface;
  225. struct usb_endpoint_descriptor *endpoint;
  226. struct usb_device *udev = interface_to_usbdev(intf);
  227. interface = intf->cur_altsetting;
  228. endpoint = &interface->endpoint[0].desc;
  229. touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
  230. input_dev = input_allocate_device();
  231. if (!touchkit || !input_dev)
  232. goto out_free;
  233. if (touchkit_alloc_buffers(udev, touchkit))
  234. goto out_free;
  235. touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
  236. if (!touchkit->irq) {
  237. dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
  238. goto out_free_buffers;
  239. }
  240. touchkit->udev = udev;
  241. touchkit->input = input_dev;
  242. if (udev->manufacturer)
  243. strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
  244. if (udev->product) {
  245. if (udev->manufacturer)
  246. strlcat(touchkit->name, " ", sizeof(touchkit->name));
  247. strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
  248. }
  249. if (!strlen(touchkit->name))
  250. snprintf(touchkit->name, sizeof(touchkit->name),
  251. "USB Touchscreen %04x:%04x",
  252. le16_to_cpu(udev->descriptor.idVendor),
  253. le16_to_cpu(udev->descriptor.idProduct));
  254. usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
  255. strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
  256. input_dev->name = touchkit->name;
  257. input_dev->phys = touchkit->phys;
  258. usb_to_input_id(udev, &input_dev->id);
  259. input_dev->cdev.dev = &intf->dev;
  260. input_dev->private = touchkit;
  261. input_dev->open = touchkit_open;
  262. input_dev->close = touchkit_close;
  263. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  264. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  265. input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
  266. TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
  267. input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
  268. TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
  269. usb_fill_int_urb(touchkit->irq, touchkit->udev,
  270. usb_rcvintpipe(touchkit->udev, 0x81),
  271. touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
  272. touchkit_irq, touchkit, endpoint->bInterval);
  273. touchkit->irq->transfer_dma = touchkit->data_dma;
  274. touchkit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  275. input_register_device(touchkit->input);
  276. usb_set_intfdata(intf, touchkit);
  277. return 0;
  278. out_free_buffers:
  279. touchkit_free_buffers(udev, touchkit);
  280. out_free:
  281. input_free_device(input_dev);
  282. kfree(touchkit);
  283. return -ENOMEM;
  284. }
  285. static void touchkit_disconnect(struct usb_interface *intf)
  286. {
  287. struct touchkit_usb *touchkit = usb_get_intfdata(intf);
  288. dbg("%s - called", __FUNCTION__);
  289. if (!touchkit)
  290. return;
  291. dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
  292. usb_set_intfdata(intf, NULL);
  293. usb_kill_urb(touchkit->irq);
  294. input_unregister_device(touchkit->input);
  295. usb_free_urb(touchkit->irq);
  296. touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
  297. kfree(touchkit);
  298. }
  299. MODULE_DEVICE_TABLE(usb, touchkit_devices);
  300. static struct usb_driver touchkit_driver = {
  301. .name = "touchkitusb",
  302. .probe = touchkit_probe,
  303. .disconnect = touchkit_disconnect,
  304. .id_table = touchkit_devices,
  305. };
  306. static int __init touchkit_init(void)
  307. {
  308. return usb_register(&touchkit_driver);
  309. }
  310. static void __exit touchkit_cleanup(void)
  311. {
  312. usb_deregister(&touchkit_driver);
  313. }
  314. module_init(touchkit_init);
  315. module_exit(touchkit_cleanup);
  316. MODULE_AUTHOR(DRIVER_AUTHOR);
  317. MODULE_DESCRIPTION(DRIVER_DESC);
  318. MODULE_LICENSE("GPL");