touchkitusb.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /******************************************************************************
  2. * touchkitusb.c -- Driver for eGalax TouchKit USB Touchscreens
  3. *
  4. * Copyright (C) 2004 by Daniel Ritz
  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. #if !defined(DEBUG) && defined(CONFIG_USB_DEBUG)
  32. #define DEBUG
  33. #endif
  34. #include <linux/usb.h>
  35. #include <linux/usb_input.h>
  36. #define TOUCHKIT_MIN_XC 0x0
  37. #define TOUCHKIT_MAX_XC 0x07ff
  38. #define TOUCHKIT_XC_FUZZ 0x0
  39. #define TOUCHKIT_XC_FLAT 0x0
  40. #define TOUCHKIT_MIN_YC 0x0
  41. #define TOUCHKIT_MAX_YC 0x07ff
  42. #define TOUCHKIT_YC_FUZZ 0x0
  43. #define TOUCHKIT_YC_FLAT 0x0
  44. #define TOUCHKIT_REPORT_DATA_SIZE 8
  45. #define TOUCHKIT_DOWN 0x01
  46. #define TOUCHKIT_POINT_TOUCH 0x81
  47. #define TOUCHKIT_POINT_NOTOUCH 0x80
  48. #define TOUCHKIT_GET_TOUCHED(dat) ((((dat)[0]) & TOUCHKIT_DOWN) ? 1 : 0)
  49. #define TOUCHKIT_GET_X(dat) (((dat)[3] << 7) | (dat)[4])
  50. #define TOUCHKIT_GET_Y(dat) (((dat)[1] << 7) | (dat)[2])
  51. #define DRIVER_VERSION "v0.1"
  52. #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
  53. #define DRIVER_DESC "eGalax TouchKit USB HID Touchscreen Driver"
  54. static int swap_xy;
  55. module_param(swap_xy, bool, 0644);
  56. MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
  57. struct touchkit_usb {
  58. unsigned char *data;
  59. dma_addr_t data_dma;
  60. struct urb *irq;
  61. struct usb_device *udev;
  62. struct input_dev *input;
  63. char name[128];
  64. char phys[64];
  65. };
  66. static struct usb_device_id touchkit_devices[] = {
  67. {USB_DEVICE(0x3823, 0x0001)},
  68. {USB_DEVICE(0x0123, 0x0001)},
  69. {USB_DEVICE(0x0eef, 0x0001)},
  70. {USB_DEVICE(0x0eef, 0x0002)},
  71. {}
  72. };
  73. static void touchkit_irq(struct urb *urb, struct pt_regs *regs)
  74. {
  75. struct touchkit_usb *touchkit = urb->context;
  76. int retval;
  77. int x, y;
  78. switch (urb->status) {
  79. case 0:
  80. /* success */
  81. break;
  82. case -ETIMEDOUT:
  83. /* this urb is timing out */
  84. dbg("%s - urb timed out - was the device unplugged?",
  85. __FUNCTION__);
  86. return;
  87. case -ECONNRESET:
  88. case -ENOENT:
  89. case -ESHUTDOWN:
  90. /* this urb is terminated, clean up */
  91. dbg("%s - urb shutting down with status: %d",
  92. __FUNCTION__, urb->status);
  93. return;
  94. default:
  95. dbg("%s - nonzero urb status received: %d",
  96. __FUNCTION__, urb->status);
  97. goto exit;
  98. }
  99. if (swap_xy) {
  100. y = TOUCHKIT_GET_X(touchkit->data);
  101. x = TOUCHKIT_GET_Y(touchkit->data);
  102. } else {
  103. x = TOUCHKIT_GET_X(touchkit->data);
  104. y = TOUCHKIT_GET_Y(touchkit->data);
  105. }
  106. input_regs(touchkit->input, regs);
  107. input_report_key(touchkit->input, BTN_TOUCH,
  108. TOUCHKIT_GET_TOUCHED(touchkit->data));
  109. input_report_abs(touchkit->input, ABS_X, x);
  110. input_report_abs(touchkit->input, ABS_Y, y);
  111. input_sync(touchkit->input);
  112. exit:
  113. retval = usb_submit_urb(urb, GFP_ATOMIC);
  114. if (retval)
  115. err("%s - usb_submit_urb failed with result: %d",
  116. __FUNCTION__, retval);
  117. }
  118. static int touchkit_open(struct input_dev *input)
  119. {
  120. struct touchkit_usb *touchkit = input->private;
  121. touchkit->irq->dev = touchkit->udev;
  122. if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
  123. return -EIO;
  124. return 0;
  125. }
  126. static void touchkit_close(struct input_dev *input)
  127. {
  128. struct touchkit_usb *touchkit = input->private;
  129. usb_kill_urb(touchkit->irq);
  130. }
  131. static int touchkit_alloc_buffers(struct usb_device *udev,
  132. struct touchkit_usb *touchkit)
  133. {
  134. touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
  135. SLAB_ATOMIC, &touchkit->data_dma);
  136. if (!touchkit->data)
  137. return -1;
  138. return 0;
  139. }
  140. static void touchkit_free_buffers(struct usb_device *udev,
  141. struct touchkit_usb *touchkit)
  142. {
  143. if (touchkit->data)
  144. usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
  145. touchkit->data, touchkit->data_dma);
  146. }
  147. static int touchkit_probe(struct usb_interface *intf,
  148. const struct usb_device_id *id)
  149. {
  150. struct touchkit_usb *touchkit;
  151. struct input_dev *input_dev;
  152. struct usb_host_interface *interface;
  153. struct usb_endpoint_descriptor *endpoint;
  154. struct usb_device *udev = interface_to_usbdev(intf);
  155. interface = intf->cur_altsetting;
  156. endpoint = &interface->endpoint[0].desc;
  157. touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
  158. input_dev = input_allocate_device();
  159. if (!touchkit || !input_dev)
  160. goto out_free;
  161. if (touchkit_alloc_buffers(udev, touchkit))
  162. goto out_free;
  163. touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
  164. if (!touchkit->irq) {
  165. dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
  166. goto out_free_buffers;
  167. }
  168. touchkit->udev = udev;
  169. touchkit->input = input_dev;
  170. if (udev->manufacturer)
  171. strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
  172. if (udev->product) {
  173. if (udev->manufacturer)
  174. strlcat(touchkit->name, " ", sizeof(touchkit->name));
  175. strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
  176. }
  177. if (!strlen(touchkit->name))
  178. snprintf(touchkit->name, sizeof(touchkit->name),
  179. "USB Touchscreen %04x:%04x",
  180. le16_to_cpu(udev->descriptor.idVendor),
  181. le16_to_cpu(udev->descriptor.idProduct));
  182. usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
  183. strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
  184. input_dev->name = touchkit->name;
  185. input_dev->phys = touchkit->phys;
  186. usb_to_input_id(udev, &input_dev->id);
  187. input_dev->cdev.dev = &intf->dev;
  188. input_dev->private = touchkit;
  189. input_dev->open = touchkit_open;
  190. input_dev->close = touchkit_close;
  191. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  192. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  193. input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
  194. TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
  195. input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
  196. TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
  197. usb_fill_int_urb(touchkit->irq, touchkit->udev,
  198. usb_rcvintpipe(touchkit->udev, 0x81),
  199. touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
  200. touchkit_irq, touchkit, endpoint->bInterval);
  201. input_register_device(touchkit->input);
  202. usb_set_intfdata(intf, touchkit);
  203. return 0;
  204. out_free_buffers:
  205. touchkit_free_buffers(udev, touchkit);
  206. out_free:
  207. input_free_device(input_dev);
  208. kfree(touchkit);
  209. return -ENOMEM;
  210. }
  211. static void touchkit_disconnect(struct usb_interface *intf)
  212. {
  213. struct touchkit_usb *touchkit = usb_get_intfdata(intf);
  214. dbg("%s - called", __FUNCTION__);
  215. if (!touchkit)
  216. return;
  217. dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
  218. usb_set_intfdata(intf, NULL);
  219. usb_kill_urb(touchkit->irq);
  220. input_unregister_device(touchkit->input);
  221. usb_free_urb(touchkit->irq);
  222. touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
  223. kfree(touchkit);
  224. }
  225. MODULE_DEVICE_TABLE(usb, touchkit_devices);
  226. static struct usb_driver touchkit_driver = {
  227. .owner = THIS_MODULE,
  228. .name = "touchkitusb",
  229. .probe = touchkit_probe,
  230. .disconnect = touchkit_disconnect,
  231. .id_table = touchkit_devices,
  232. };
  233. static int __init touchkit_init(void)
  234. {
  235. return usb_register(&touchkit_driver);
  236. }
  237. static void __exit touchkit_cleanup(void)
  238. {
  239. usb_deregister(&touchkit_driver);
  240. }
  241. module_init(touchkit_init);
  242. module_exit(touchkit_cleanup);
  243. MODULE_AUTHOR(DRIVER_AUTHOR);
  244. MODULE_DESCRIPTION(DRIVER_DESC);
  245. MODULE_LICENSE("GPL");