touchkitusb.c 10 KB

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