uvc_status.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * uvc_status.c -- USB Video Class driver - Status endpoint
  3. *
  4. * Copyright (C) 2005-2009
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/input.h>
  15. #include <linux/slab.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/input.h>
  18. #include "uvcvideo.h"
  19. /* --------------------------------------------------------------------------
  20. * Input device
  21. */
  22. #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
  23. static int uvc_input_init(struct uvc_device *dev)
  24. {
  25. struct input_dev *input;
  26. int ret;
  27. input = input_allocate_device();
  28. if (input == NULL)
  29. return -ENOMEM;
  30. usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
  31. strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
  32. input->name = dev->name;
  33. input->phys = dev->input_phys;
  34. usb_to_input_id(dev->udev, &input->id);
  35. input->dev.parent = &dev->intf->dev;
  36. __set_bit(EV_KEY, input->evbit);
  37. __set_bit(KEY_CAMERA, input->keybit);
  38. if ((ret = input_register_device(input)) < 0)
  39. goto error;
  40. dev->input = input;
  41. return 0;
  42. error:
  43. input_free_device(input);
  44. return ret;
  45. }
  46. static void uvc_input_cleanup(struct uvc_device *dev)
  47. {
  48. if (dev->input)
  49. input_unregister_device(dev->input);
  50. }
  51. static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
  52. int value)
  53. {
  54. if (dev->input) {
  55. input_report_key(dev->input, code, value);
  56. input_sync(dev->input);
  57. }
  58. }
  59. #else
  60. #define uvc_input_init(dev)
  61. #define uvc_input_cleanup(dev)
  62. #define uvc_input_report_key(dev, code, value)
  63. #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
  64. /* --------------------------------------------------------------------------
  65. * Status interrupt endpoint
  66. */
  67. static void uvc_event_streaming(struct uvc_device *dev, __u8 *data, int len)
  68. {
  69. if (len < 3) {
  70. uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
  71. "received.\n");
  72. return;
  73. }
  74. if (data[2] == 0) {
  75. if (len < 4)
  76. return;
  77. uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
  78. data[1], data[3] ? "pressed" : "released", len);
  79. uvc_input_report_key(dev, KEY_CAMERA, data[3]);
  80. } else {
  81. uvc_trace(UVC_TRACE_STATUS, "Stream %u error event %02x %02x "
  82. "len %d.\n", data[1], data[2], data[3], len);
  83. }
  84. }
  85. static void uvc_event_control(struct uvc_device *dev, __u8 *data, int len)
  86. {
  87. char *attrs[3] = { "value", "info", "failure" };
  88. if (len < 6 || data[2] != 0 || data[4] > 2) {
  89. uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
  90. "received.\n");
  91. return;
  92. }
  93. uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
  94. data[1], data[3], attrs[data[4]], len);
  95. }
  96. static void uvc_status_complete(struct urb *urb)
  97. {
  98. struct uvc_device *dev = urb->context;
  99. int len, ret;
  100. switch (urb->status) {
  101. case 0:
  102. break;
  103. case -ENOENT: /* usb_kill_urb() called. */
  104. case -ECONNRESET: /* usb_unlink_urb() called. */
  105. case -ESHUTDOWN: /* The endpoint is being disabled. */
  106. case -EPROTO: /* Device is disconnected (reported by some
  107. * host controller). */
  108. return;
  109. default:
  110. uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
  111. "completion handler.\n", urb->status);
  112. return;
  113. }
  114. len = urb->actual_length;
  115. if (len > 0) {
  116. switch (dev->status[0] & 0x0f) {
  117. case UVC_STATUS_TYPE_CONTROL:
  118. uvc_event_control(dev, dev->status, len);
  119. break;
  120. case UVC_STATUS_TYPE_STREAMING:
  121. uvc_event_streaming(dev, dev->status, len);
  122. break;
  123. default:
  124. uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
  125. "type %u.\n", dev->status[0]);
  126. break;
  127. }
  128. }
  129. /* Resubmit the URB. */
  130. urb->interval = dev->int_ep->desc.bInterval;
  131. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  132. uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
  133. ret);
  134. }
  135. }
  136. int uvc_status_init(struct uvc_device *dev)
  137. {
  138. struct usb_host_endpoint *ep = dev->int_ep;
  139. unsigned int pipe;
  140. int interval;
  141. if (ep == NULL)
  142. return 0;
  143. uvc_input_init(dev);
  144. dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
  145. if (dev->status == NULL)
  146. return -ENOMEM;
  147. dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  148. if (dev->int_urb == NULL) {
  149. kfree(dev->status);
  150. return -ENOMEM;
  151. }
  152. pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
  153. /* For high-speed interrupt endpoints, the bInterval value is used as
  154. * an exponent of two. Some developers forgot about it.
  155. */
  156. interval = ep->desc.bInterval;
  157. if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
  158. (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
  159. interval = fls(interval) - 1;
  160. usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
  161. dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
  162. dev, interval);
  163. return 0;
  164. }
  165. void uvc_status_cleanup(struct uvc_device *dev)
  166. {
  167. usb_kill_urb(dev->int_urb);
  168. usb_free_urb(dev->int_urb);
  169. kfree(dev->status);
  170. uvc_input_cleanup(dev);
  171. }
  172. int uvc_status_start(struct uvc_device *dev, gfp_t flags)
  173. {
  174. if (dev->int_urb == NULL)
  175. return 0;
  176. return usb_submit_urb(dev->int_urb, flags);
  177. }
  178. void uvc_status_stop(struct uvc_device *dev)
  179. {
  180. usb_kill_urb(dev->int_urb);
  181. }