touchkitusb.c 10.0 KB

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