uvc_status.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * uvc_status.c -- USB Video Class driver - Status endpoint
  4. *
  5. * Copyright (C) 2005-2009
  6. * Laurent Pinchart ([email protected])
  7. */
  8. #include <asm/barrier.h>
  9. #include <linux/kernel.h>
  10. #include <linux/input.h>
  11. #include <linux/slab.h>
  12. #include <linux/usb.h>
  13. #include <linux/usb/input.h>
  14. #include "uvcvideo.h"
  15. /* --------------------------------------------------------------------------
  16. * Input device
  17. */
  18. #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
  19. static int uvc_input_init(struct uvc_device *dev)
  20. {
  21. struct input_dev *input;
  22. int ret;
  23. input = input_allocate_device();
  24. if (input == NULL)
  25. return -ENOMEM;
  26. usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
  27. strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
  28. input->name = dev->name;
  29. input->phys = dev->input_phys;
  30. usb_to_input_id(dev->udev, &input->id);
  31. input->dev.parent = &dev->intf->dev;
  32. __set_bit(EV_KEY, input->evbit);
  33. __set_bit(KEY_CAMERA, input->keybit);
  34. if ((ret = input_register_device(input)) < 0)
  35. goto error;
  36. dev->input = input;
  37. return 0;
  38. error:
  39. input_free_device(input);
  40. return ret;
  41. }
  42. static void uvc_input_unregister(struct uvc_device *dev)
  43. {
  44. if (dev->input)
  45. input_unregister_device(dev->input);
  46. }
  47. static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
  48. int value)
  49. {
  50. if (dev->input) {
  51. input_report_key(dev->input, code, value);
  52. input_sync(dev->input);
  53. }
  54. }
  55. #else
  56. #define uvc_input_init(dev)
  57. #define uvc_input_unregister(dev)
  58. #define uvc_input_report_key(dev, code, value)
  59. #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
  60. /* --------------------------------------------------------------------------
  61. * Status interrupt endpoint
  62. */
  63. struct uvc_streaming_status {
  64. u8 bStatusType;
  65. u8 bOriginator;
  66. u8 bEvent;
  67. u8 bValue[];
  68. } __packed;
  69. struct uvc_control_status {
  70. u8 bStatusType;
  71. u8 bOriginator;
  72. u8 bEvent;
  73. u8 bSelector;
  74. u8 bAttribute;
  75. u8 bValue[];
  76. } __packed;
  77. static void uvc_event_streaming(struct uvc_device *dev,
  78. struct uvc_streaming_status *status, int len)
  79. {
  80. if (len < 3) {
  81. uvc_dbg(dev, STATUS,
  82. "Invalid streaming status event received\n");
  83. return;
  84. }
  85. if (status->bEvent == 0) {
  86. if (len < 4)
  87. return;
  88. uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
  89. status->bOriginator,
  90. status->bValue[0] ? "pressed" : "released", len);
  91. uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
  92. } else {
  93. uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
  94. status->bOriginator, status->bEvent, len);
  95. }
  96. }
  97. #define UVC_CTRL_VALUE_CHANGE 0
  98. #define UVC_CTRL_INFO_CHANGE 1
  99. #define UVC_CTRL_FAILURE_CHANGE 2
  100. #define UVC_CTRL_MIN_CHANGE 3
  101. #define UVC_CTRL_MAX_CHANGE 4
  102. static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
  103. u8 selector)
  104. {
  105. struct uvc_control *ctrl;
  106. unsigned int i;
  107. for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
  108. if (ctrl->info.selector == selector)
  109. return ctrl;
  110. return NULL;
  111. }
  112. static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
  113. const struct uvc_control_status *status,
  114. struct uvc_video_chain **chain)
  115. {
  116. list_for_each_entry((*chain), &dev->chains, list) {
  117. struct uvc_entity *entity;
  118. struct uvc_control *ctrl;
  119. list_for_each_entry(entity, &(*chain)->entities, chain) {
  120. if (entity->id != status->bOriginator)
  121. continue;
  122. ctrl = uvc_event_entity_find_ctrl(entity,
  123. status->bSelector);
  124. if (ctrl)
  125. return ctrl;
  126. }
  127. }
  128. return NULL;
  129. }
  130. static bool uvc_event_control(struct urb *urb,
  131. const struct uvc_control_status *status, int len)
  132. {
  133. static const char *attrs[] = { "value", "info", "failure", "min", "max" };
  134. struct uvc_device *dev = urb->context;
  135. struct uvc_video_chain *chain;
  136. struct uvc_control *ctrl;
  137. if (len < 6 || status->bEvent != 0 ||
  138. status->bAttribute >= ARRAY_SIZE(attrs)) {
  139. uvc_dbg(dev, STATUS, "Invalid control status event received\n");
  140. return false;
  141. }
  142. uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
  143. status->bOriginator, status->bSelector,
  144. attrs[status->bAttribute], len);
  145. /* Find the control. */
  146. ctrl = uvc_event_find_ctrl(dev, status, &chain);
  147. if (!ctrl)
  148. return false;
  149. switch (status->bAttribute) {
  150. case UVC_CTRL_VALUE_CHANGE:
  151. return uvc_ctrl_status_event_async(urb, chain, ctrl,
  152. status->bValue);
  153. case UVC_CTRL_INFO_CHANGE:
  154. case UVC_CTRL_FAILURE_CHANGE:
  155. case UVC_CTRL_MIN_CHANGE:
  156. case UVC_CTRL_MAX_CHANGE:
  157. break;
  158. }
  159. return false;
  160. }
  161. static void uvc_status_complete(struct urb *urb)
  162. {
  163. struct uvc_device *dev = urb->context;
  164. int len, ret;
  165. switch (urb->status) {
  166. case 0:
  167. break;
  168. case -ENOENT: /* usb_kill_urb() called. */
  169. case -ECONNRESET: /* usb_unlink_urb() called. */
  170. case -ESHUTDOWN: /* The endpoint is being disabled. */
  171. case -EPROTO: /* Device is disconnected (reported by some host controllers). */
  172. return;
  173. default:
  174. dev_warn(&dev->udev->dev,
  175. "Non-zero status (%d) in status completion handler.\n",
  176. urb->status);
  177. return;
  178. }
  179. len = urb->actual_length;
  180. if (len > 0) {
  181. switch (dev->status[0] & 0x0f) {
  182. case UVC_STATUS_TYPE_CONTROL: {
  183. struct uvc_control_status *status =
  184. (struct uvc_control_status *)dev->status;
  185. if (uvc_event_control(urb, status, len))
  186. /* The URB will be resubmitted in work context. */
  187. return;
  188. break;
  189. }
  190. case UVC_STATUS_TYPE_STREAMING: {
  191. struct uvc_streaming_status *status =
  192. (struct uvc_streaming_status *)dev->status;
  193. uvc_event_streaming(dev, status, len);
  194. break;
  195. }
  196. default:
  197. uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
  198. dev->status[0]);
  199. break;
  200. }
  201. }
  202. /* Resubmit the URB. */
  203. urb->interval = dev->int_ep->desc.bInterval;
  204. ret = usb_submit_urb(urb, GFP_ATOMIC);
  205. if (ret < 0)
  206. dev_err(&dev->udev->dev,
  207. "Failed to resubmit status URB (%d).\n", ret);
  208. }
  209. int uvc_status_init(struct uvc_device *dev)
  210. {
  211. struct usb_host_endpoint *ep = dev->int_ep;
  212. unsigned int pipe;
  213. int interval;
  214. if (ep == NULL)
  215. return 0;
  216. uvc_input_init(dev);
  217. dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
  218. if (dev->status == NULL)
  219. return -ENOMEM;
  220. dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  221. if (dev->int_urb == NULL) {
  222. kfree(dev->status);
  223. return -ENOMEM;
  224. }
  225. pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
  226. /*
  227. * For high-speed interrupt endpoints, the bInterval value is used as
  228. * an exponent of two. Some developers forgot about it.
  229. */
  230. interval = ep->desc.bInterval;
  231. if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
  232. (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
  233. interval = fls(interval) - 1;
  234. usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
  235. dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
  236. dev, interval);
  237. return 0;
  238. }
  239. void uvc_status_unregister(struct uvc_device *dev)
  240. {
  241. usb_kill_urb(dev->int_urb);
  242. uvc_input_unregister(dev);
  243. }
  244. void uvc_status_cleanup(struct uvc_device *dev)
  245. {
  246. usb_free_urb(dev->int_urb);
  247. kfree(dev->status);
  248. }
  249. int uvc_status_start(struct uvc_device *dev, gfp_t flags)
  250. {
  251. if (dev->int_urb == NULL)
  252. return 0;
  253. return usb_submit_urb(dev->int_urb, flags);
  254. }
  255. void uvc_status_stop(struct uvc_device *dev)
  256. {
  257. struct uvc_ctrl_work *w = &dev->async_ctrl;
  258. /*
  259. * Prevent the asynchronous control handler from requeing the URB. The
  260. * barrier is needed so the flush_status change is visible to other
  261. * CPUs running the asynchronous handler before usb_kill_urb() is
  262. * called below.
  263. */
  264. smp_store_release(&dev->flush_status, true);
  265. /*
  266. * Cancel any pending asynchronous work. If any status event was queued,
  267. * process it synchronously.
  268. */
  269. if (cancel_work_sync(&w->work))
  270. uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
  271. /* Kill the urb. */
  272. usb_kill_urb(dev->int_urb);
  273. /*
  274. * The URB completion handler may have queued asynchronous work. This
  275. * won't resubmit the URB as flush_status is set, but it needs to be
  276. * cancelled before returning or it could then race with a future
  277. * uvc_status_start() call.
  278. */
  279. if (cancel_work_sync(&w->work))
  280. uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
  281. /*
  282. * From this point, there are no events on the queue and the status URB
  283. * is dead. No events will be queued until uvc_status_start() is called.
  284. * The barrier is needed to make sure that flush_status is visible to
  285. * uvc_ctrl_status_event_work() when uvc_status_start() will be called
  286. * again.
  287. */
  288. smp_store_release(&dev->flush_status, false);
  289. }