touchkitusb.c 8.2 KB

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