uvc_status.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_unregister(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_unregister(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. struct uvc_streaming_status {
  68. u8 bStatusType;
  69. u8 bOriginator;
  70. u8 bEvent;
  71. u8 bValue[];
  72. } __packed;
  73. struct uvc_control_status {
  74. u8 bStatusType;
  75. u8 bOriginator;
  76. u8 bEvent;
  77. u8 bSelector;
  78. u8 bAttribute;
  79. u8 bValue[];
  80. } __packed;
  81. static void uvc_event_streaming(struct uvc_device *dev,
  82. struct uvc_streaming_status *status, int len)
  83. {
  84. if (len < 3) {
  85. uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
  86. "received.\n");
  87. return;
  88. }
  89. if (status->bEvent == 0) {
  90. if (len < 4)
  91. return;
  92. uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
  93. status->bOriginator,
  94. status->bValue[0] ? "pressed" : "released", len);
  95. uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
  96. } else {
  97. uvc_trace(UVC_TRACE_STATUS,
  98. "Stream %u error event %02x len %d.\n",
  99. status->bOriginator, status->bEvent, len);
  100. }
  101. }
  102. #define UVC_CTRL_VALUE_CHANGE 0
  103. #define UVC_CTRL_INFO_CHANGE 1
  104. #define UVC_CTRL_FAILURE_CHANGE 2
  105. #define UVC_CTRL_MIN_CHANGE 3
  106. #define UVC_CTRL_MAX_CHANGE 4
  107. static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
  108. u8 selector)
  109. {
  110. struct uvc_control *ctrl;
  111. unsigned int i;
  112. for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
  113. if (ctrl->info.selector == selector)
  114. return ctrl;
  115. return NULL;
  116. }
  117. static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
  118. const struct uvc_control_status *status,
  119. struct uvc_video_chain **chain)
  120. {
  121. list_for_each_entry((*chain), &dev->chains, list) {
  122. struct uvc_entity *entity;
  123. struct uvc_control *ctrl;
  124. list_for_each_entry(entity, &(*chain)->entities, chain) {
  125. if (entity->id != status->bOriginator)
  126. continue;
  127. ctrl = uvc_event_entity_find_ctrl(entity,
  128. status->bSelector);
  129. if (ctrl)
  130. return ctrl;
  131. }
  132. }
  133. return NULL;
  134. }
  135. static bool uvc_event_control(struct urb *urb,
  136. const struct uvc_control_status *status, int len)
  137. {
  138. static const char *attrs[] = { "value", "info", "failure", "min", "max" };
  139. struct uvc_device *dev = urb->context;
  140. struct uvc_video_chain *chain;
  141. struct uvc_control *ctrl;
  142. if (len < 6 || status->bEvent != 0 ||
  143. status->bAttribute >= ARRAY_SIZE(attrs)) {
  144. uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
  145. "received.\n");
  146. return false;
  147. }
  148. uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
  149. status->bOriginator, status->bSelector,
  150. attrs[status->bAttribute], len);
  151. /* Find the control. */
  152. ctrl = uvc_event_find_ctrl(dev, status, &chain);
  153. if (!ctrl)
  154. return false;
  155. switch (status->bAttribute) {
  156. case UVC_CTRL_VALUE_CHANGE:
  157. return uvc_ctrl_status_event(urb, chain, ctrl, status->bValue);
  158. case UVC_CTRL_INFO_CHANGE:
  159. case UVC_CTRL_FAILURE_CHANGE:
  160. case UVC_CTRL_MIN_CHANGE:
  161. case UVC_CTRL_MAX_CHANGE:
  162. break;
  163. }
  164. return false;
  165. }
  166. static void uvc_status_complete(struct urb *urb)
  167. {
  168. struct uvc_device *dev = urb->context;
  169. int len, ret;
  170. switch (urb->status) {
  171. case 0:
  172. break;
  173. case -ENOENT: /* usb_kill_urb() called. */
  174. case -ECONNRESET: /* usb_unlink_urb() called. */
  175. case -ESHUTDOWN: /* The endpoint is being disabled. */
  176. case -EPROTO: /* Device is disconnected (reported by some
  177. * host controller). */
  178. return;
  179. default:
  180. uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
  181. "completion handler.\n", urb->status);
  182. return;
  183. }
  184. len = urb->actual_length;
  185. if (len > 0) {
  186. switch (dev->status[0] & 0x0f) {
  187. case UVC_STATUS_TYPE_CONTROL: {
  188. struct uvc_control_status *status =
  189. (struct uvc_control_status *)dev->status;
  190. if (uvc_event_control(urb, status, len))
  191. /* The URB will be resubmitted in work context. */
  192. return;
  193. break;
  194. }
  195. case UVC_STATUS_TYPE_STREAMING: {
  196. struct uvc_streaming_status *status =
  197. (struct uvc_streaming_status *)dev->status;
  198. uvc_event_streaming(dev, status, len);
  199. break;
  200. }
  201. default:
  202. uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
  203. "type %u.\n", dev->status[0]);
  204. break;
  205. }
  206. }
  207. /* Resubmit the URB. */
  208. urb->interval = dev->int_ep->desc.bInterval;
  209. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  210. uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
  211. ret);
  212. }
  213. }
  214. int uvc_status_init(struct uvc_device *dev)
  215. {
  216. struct usb_host_endpoint *ep = dev->int_ep;
  217. unsigned int pipe;
  218. int interval;
  219. if (ep == NULL)
  220. return 0;
  221. uvc_input_init(dev);
  222. dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
  223. if (dev->status == NULL)
  224. return -ENOMEM;
  225. dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  226. if (dev->int_urb == NULL) {
  227. kfree(dev->status);
  228. return -ENOMEM;
  229. }
  230. pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
  231. /* For high-speed interrupt endpoints, the bInterval value is used as
  232. * an exponent of two. Some developers forgot about it.
  233. */
  234. interval = ep->desc.bInterval;
  235. if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
  236. (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
  237. interval = fls(interval) - 1;
  238. usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
  239. dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
  240. dev, interval);
  241. return 0;
  242. }
  243. void uvc_status_unregister(struct uvc_device *dev)
  244. {
  245. usb_kill_urb(dev->int_urb);
  246. uvc_input_unregister(dev);
  247. }
  248. void uvc_status_cleanup(struct uvc_device *dev)
  249. {
  250. usb_free_urb(dev->int_urb);
  251. kfree(dev->status);
  252. }
  253. int uvc_status_start(struct uvc_device *dev, gfp_t flags)
  254. {
  255. if (dev->int_urb == NULL)
  256. return 0;
  257. return usb_submit_urb(dev->int_urb, flags);
  258. }
  259. void uvc_status_stop(struct uvc_device *dev)
  260. {
  261. usb_kill_urb(dev->int_urb);
  262. }