uvc_status.c 5.4 KB

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