uvc_status.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 (laurent.pinchart@ideasonboard.com)
  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 bool uvc_input_has_button(struct uvc_device *dev)
  20. {
  21. struct uvc_streaming *stream;
  22. /*
  23. * The device has button events if both bTriggerSupport and
  24. * bTriggerUsage are one. Otherwise the camera button does not
  25. * exist or is handled automatically by the camera without host
  26. * driver or client application intervention.
  27. */
  28. list_for_each_entry(stream, &dev->streams, list) {
  29. if (stream->header.bTriggerSupport == 1 &&
  30. stream->header.bTriggerUsage == 1)
  31. return true;
  32. }
  33. return false;
  34. }
  35. static int uvc_input_init(struct uvc_device *dev)
  36. {
  37. struct input_dev *input;
  38. int ret;
  39. if (!uvc_input_has_button(dev))
  40. return 0;
  41. input = input_allocate_device();
  42. if (input == NULL)
  43. return -ENOMEM;
  44. usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
  45. strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
  46. input->name = dev->name;
  47. input->phys = dev->input_phys;
  48. usb_to_input_id(dev->udev, &input->id);
  49. input->dev.parent = &dev->intf->dev;
  50. __set_bit(EV_KEY, input->evbit);
  51. __set_bit(KEY_CAMERA, input->keybit);
  52. ret = input_register_device(input);
  53. if (ret < 0)
  54. goto error;
  55. dev->input = input;
  56. return 0;
  57. error:
  58. input_free_device(input);
  59. return ret;
  60. }
  61. static void uvc_input_unregister(struct uvc_device *dev)
  62. {
  63. if (dev->input)
  64. input_unregister_device(dev->input);
  65. }
  66. static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
  67. int value)
  68. {
  69. if (dev->input) {
  70. input_report_key(dev->input, code, value);
  71. input_sync(dev->input);
  72. }
  73. }
  74. #else
  75. #define uvc_input_init(dev)
  76. #define uvc_input_unregister(dev)
  77. #define uvc_input_report_key(dev, code, value)
  78. #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
  79. /* --------------------------------------------------------------------------
  80. * Status interrupt endpoint
  81. */
  82. static void uvc_event_streaming(struct uvc_device *dev,
  83. struct uvc_status *status, int len)
  84. {
  85. if (len <= offsetof(struct uvc_status, bEvent)) {
  86. uvc_dbg(dev, STATUS,
  87. "Invalid streaming status event received\n");
  88. return;
  89. }
  90. if (status->bEvent == 0) {
  91. if (len <= offsetof(struct uvc_status, streaming))
  92. return;
  93. uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
  94. status->bOriginator,
  95. status->streaming.button ? "pressed" : "released", len);
  96. uvc_input_report_key(dev, KEY_CAMERA, status->streaming.button);
  97. } else {
  98. uvc_dbg(dev, STATUS, "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_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->control.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_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->control.bAttribute >= ARRAY_SIZE(attrs)) {
  144. uvc_dbg(dev, STATUS, "Invalid control status event received\n");
  145. return false;
  146. }
  147. uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
  148. status->bOriginator, status->control.bSelector,
  149. attrs[status->control.bAttribute], len);
  150. /* Find the control. */
  151. ctrl = uvc_event_find_ctrl(dev, status, &chain);
  152. if (!ctrl)
  153. return false;
  154. switch (status->control.bAttribute) {
  155. case UVC_CTRL_VALUE_CHANGE:
  156. return uvc_ctrl_status_event_async(urb, chain, ctrl,
  157. status->control.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 host controllers). */
  177. return;
  178. default:
  179. dev_warn(&dev->intf->dev,
  180. "Non-zero status (%d) in status completion handler.\n",
  181. urb->status);
  182. return;
  183. }
  184. len = urb->actual_length;
  185. if (len > 0) {
  186. switch (dev->status->bStatusType & 0x0f) {
  187. case UVC_STATUS_TYPE_CONTROL: {
  188. if (uvc_event_control(urb, dev->status, len))
  189. /* The URB will be resubmitted in work context. */
  190. return;
  191. break;
  192. }
  193. case UVC_STATUS_TYPE_STREAMING: {
  194. uvc_event_streaming(dev, dev->status, len);
  195. break;
  196. }
  197. default:
  198. uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
  199. dev->status->bStatusType);
  200. break;
  201. }
  202. }
  203. /* Resubmit the URB. */
  204. urb->interval = dev->int_ep->desc.bInterval;
  205. ret = usb_submit_urb(urb, GFP_ATOMIC);
  206. if (ret < 0)
  207. dev_err(&dev->intf->dev,
  208. "Failed to resubmit status URB (%d).\n", ret);
  209. }
  210. int uvc_status_init(struct uvc_device *dev)
  211. {
  212. struct usb_host_endpoint *ep = dev->int_ep;
  213. unsigned int pipe;
  214. int interval;
  215. mutex_init(&dev->status_lock);
  216. if (ep == NULL)
  217. return 0;
  218. dev->status = kzalloc_obj(*dev->status);
  219. if (!dev->status)
  220. return -ENOMEM;
  221. dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  222. if (!dev->int_urb) {
  223. kfree(dev->status);
  224. dev->status = NULL;
  225. return -ENOMEM;
  226. }
  227. pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
  228. /*
  229. * For high-speed interrupt endpoints, the bInterval value is used as
  230. * an exponent of two. Some developers forgot about it.
  231. */
  232. interval = ep->desc.bInterval;
  233. if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
  234. (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
  235. interval = fls(interval) - 1;
  236. usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
  237. dev->status, sizeof(*dev->status), uvc_status_complete,
  238. dev, interval);
  239. uvc_input_init(dev);
  240. return 0;
  241. }
  242. void uvc_status_unregister(struct uvc_device *dev)
  243. {
  244. if (!dev->status)
  245. return;
  246. uvc_status_suspend(dev);
  247. uvc_input_unregister(dev);
  248. }
  249. void uvc_status_cleanup(struct uvc_device *dev)
  250. {
  251. usb_free_urb(dev->int_urb);
  252. kfree(dev->status);
  253. }
  254. static int uvc_status_start(struct uvc_device *dev, gfp_t flags)
  255. {
  256. lockdep_assert_held(&dev->status_lock);
  257. if (!dev->int_urb)
  258. return 0;
  259. return usb_submit_urb(dev->int_urb, flags);
  260. }
  261. static void uvc_status_stop(struct uvc_device *dev)
  262. {
  263. struct uvc_ctrl_work *w = &dev->async_ctrl;
  264. lockdep_assert_held(&dev->status_lock);
  265. if (!dev->int_urb)
  266. return;
  267. /*
  268. * Prevent the asynchronous control handler from requeing the URB. The
  269. * barrier is needed so the flush_status change is visible to other
  270. * CPUs running the asynchronous handler before usb_kill_urb() is
  271. * called below.
  272. */
  273. smp_store_release(&dev->flush_status, true);
  274. /*
  275. * Cancel any pending asynchronous work. If any status event was queued,
  276. * process it synchronously.
  277. */
  278. if (cancel_work_sync(&w->work))
  279. uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
  280. /* Kill the urb. */
  281. usb_kill_urb(dev->int_urb);
  282. /*
  283. * The URB completion handler may have queued asynchronous work. This
  284. * won't resubmit the URB as flush_status is set, but it needs to be
  285. * cancelled before returning or it could then race with a future
  286. * uvc_status_start() call.
  287. */
  288. if (cancel_work_sync(&w->work))
  289. uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
  290. /*
  291. * From this point, there are no events on the queue and the status URB
  292. * is dead. No events will be queued until uvc_status_start() is called.
  293. * The barrier is needed to make sure that flush_status is visible to
  294. * uvc_ctrl_status_event_work() when uvc_status_start() will be called
  295. * again.
  296. */
  297. smp_store_release(&dev->flush_status, false);
  298. }
  299. int uvc_status_resume(struct uvc_device *dev)
  300. {
  301. guard(mutex)(&dev->status_lock);
  302. if (dev->status_users)
  303. return uvc_status_start(dev, GFP_NOIO);
  304. return 0;
  305. }
  306. void uvc_status_suspend(struct uvc_device *dev)
  307. {
  308. guard(mutex)(&dev->status_lock);
  309. if (dev->status_users)
  310. uvc_status_stop(dev);
  311. }
  312. int uvc_status_get(struct uvc_device *dev)
  313. {
  314. int ret;
  315. guard(mutex)(&dev->status_lock);
  316. if (!dev->status_users) {
  317. ret = uvc_status_start(dev, GFP_KERNEL);
  318. if (ret)
  319. return ret;
  320. }
  321. dev->status_users++;
  322. return 0;
  323. }
  324. void uvc_status_put(struct uvc_device *dev)
  325. {
  326. guard(mutex)(&dev->status_lock);
  327. if (dev->status_users == 1)
  328. uvc_status_stop(dev);
  329. WARN_ON(!dev->status_users);
  330. if (dev->status_users)
  331. dev->status_users--;
  332. }