touchkitusb.c 10.0 KB

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