drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / media / usb / uvc / uvc_status.c
blob06c867510c8fe6d051c7814586796700339c3316
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * uvc_status.c -- USB Video Class driver - Status endpoint
5 * Copyright (C) 2005-2009
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
9 #include <asm/barrier.h>
10 #include <linux/kernel.h>
11 #include <linux/input.h>
12 #include <linux/slab.h>
13 #include <linux/usb.h>
14 #include <linux/usb/input.h>
16 #include "uvcvideo.h"
18 /* --------------------------------------------------------------------------
19 * Input device
21 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
23 static bool uvc_input_has_button(struct uvc_device *dev)
25 struct uvc_streaming *stream;
28 * The device has button events if both bTriggerSupport and
29 * bTriggerUsage are one. Otherwise the camera button does not
30 * exist or is handled automatically by the camera without host
31 * driver or client application intervention.
33 list_for_each_entry(stream, &dev->streams, list) {
34 if (stream->header.bTriggerSupport == 1 &&
35 stream->header.bTriggerUsage == 1)
36 return true;
39 return false;
42 static int uvc_input_init(struct uvc_device *dev)
44 struct input_dev *input;
45 int ret;
47 if (!uvc_input_has_button(dev))
48 return 0;
50 input = input_allocate_device();
51 if (input == NULL)
52 return -ENOMEM;
54 usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
55 strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
57 input->name = dev->name;
58 input->phys = dev->input_phys;
59 usb_to_input_id(dev->udev, &input->id);
60 input->dev.parent = &dev->intf->dev;
62 __set_bit(EV_KEY, input->evbit);
63 __set_bit(KEY_CAMERA, input->keybit);
65 if ((ret = input_register_device(input)) < 0)
66 goto error;
68 dev->input = input;
69 return 0;
71 error:
72 input_free_device(input);
73 return ret;
76 static void uvc_input_unregister(struct uvc_device *dev)
78 if (dev->input)
79 input_unregister_device(dev->input);
82 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
83 int value)
85 if (dev->input) {
86 input_report_key(dev->input, code, value);
87 input_sync(dev->input);
91 #else
92 #define uvc_input_init(dev)
93 #define uvc_input_unregister(dev)
94 #define uvc_input_report_key(dev, code, value)
95 #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
97 /* --------------------------------------------------------------------------
98 * Status interrupt endpoint
100 static void uvc_event_streaming(struct uvc_device *dev,
101 struct uvc_status *status, int len)
103 if (len <= offsetof(struct uvc_status, bEvent)) {
104 uvc_dbg(dev, STATUS,
105 "Invalid streaming status event received\n");
106 return;
109 if (status->bEvent == 0) {
110 if (len <= offsetof(struct uvc_status, streaming))
111 return;
113 uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
114 status->bOriginator,
115 status->streaming.button ? "pressed" : "released", len);
116 uvc_input_report_key(dev, KEY_CAMERA, status->streaming.button);
117 } else {
118 uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
119 status->bOriginator, status->bEvent, len);
123 #define UVC_CTRL_VALUE_CHANGE 0
124 #define UVC_CTRL_INFO_CHANGE 1
125 #define UVC_CTRL_FAILURE_CHANGE 2
126 #define UVC_CTRL_MIN_CHANGE 3
127 #define UVC_CTRL_MAX_CHANGE 4
129 static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
130 u8 selector)
132 struct uvc_control *ctrl;
133 unsigned int i;
135 for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
136 if (ctrl->info.selector == selector)
137 return ctrl;
139 return NULL;
142 static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
143 const struct uvc_status *status,
144 struct uvc_video_chain **chain)
146 list_for_each_entry((*chain), &dev->chains, list) {
147 struct uvc_entity *entity;
148 struct uvc_control *ctrl;
150 list_for_each_entry(entity, &(*chain)->entities, chain) {
151 if (entity->id != status->bOriginator)
152 continue;
154 ctrl = uvc_event_entity_find_ctrl(entity,
155 status->control.bSelector);
156 if (ctrl)
157 return ctrl;
161 return NULL;
164 static bool uvc_event_control(struct urb *urb,
165 const struct uvc_status *status, int len)
167 static const char *attrs[] = { "value", "info", "failure", "min", "max" };
168 struct uvc_device *dev = urb->context;
169 struct uvc_video_chain *chain;
170 struct uvc_control *ctrl;
172 if (len < 6 || status->bEvent != 0 ||
173 status->control.bAttribute >= ARRAY_SIZE(attrs)) {
174 uvc_dbg(dev, STATUS, "Invalid control status event received\n");
175 return false;
178 uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
179 status->bOriginator, status->control.bSelector,
180 attrs[status->control.bAttribute], len);
182 /* Find the control. */
183 ctrl = uvc_event_find_ctrl(dev, status, &chain);
184 if (!ctrl)
185 return false;
187 switch (status->control.bAttribute) {
188 case UVC_CTRL_VALUE_CHANGE:
189 return uvc_ctrl_status_event_async(urb, chain, ctrl,
190 status->control.bValue);
192 case UVC_CTRL_INFO_CHANGE:
193 case UVC_CTRL_FAILURE_CHANGE:
194 case UVC_CTRL_MIN_CHANGE:
195 case UVC_CTRL_MAX_CHANGE:
196 break;
199 return false;
202 static void uvc_status_complete(struct urb *urb)
204 struct uvc_device *dev = urb->context;
205 int len, ret;
207 switch (urb->status) {
208 case 0:
209 break;
211 case -ENOENT: /* usb_kill_urb() called. */
212 case -ECONNRESET: /* usb_unlink_urb() called. */
213 case -ESHUTDOWN: /* The endpoint is being disabled. */
214 case -EPROTO: /* Device is disconnected (reported by some host controllers). */
215 return;
217 default:
218 dev_warn(&dev->udev->dev,
219 "Non-zero status (%d) in status completion handler.\n",
220 urb->status);
221 return;
224 len = urb->actual_length;
225 if (len > 0) {
226 switch (dev->status->bStatusType & 0x0f) {
227 case UVC_STATUS_TYPE_CONTROL: {
228 if (uvc_event_control(urb, dev->status, len))
229 /* The URB will be resubmitted in work context. */
230 return;
231 break;
234 case UVC_STATUS_TYPE_STREAMING: {
235 uvc_event_streaming(dev, dev->status, len);
236 break;
239 default:
240 uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
241 dev->status->bStatusType);
242 break;
246 /* Resubmit the URB. */
247 urb->interval = dev->int_ep->desc.bInterval;
248 ret = usb_submit_urb(urb, GFP_ATOMIC);
249 if (ret < 0)
250 dev_err(&dev->udev->dev,
251 "Failed to resubmit status URB (%d).\n", ret);
254 int uvc_status_init(struct uvc_device *dev)
256 struct usb_host_endpoint *ep = dev->int_ep;
257 unsigned int pipe;
258 int interval;
260 mutex_init(&dev->status_lock);
262 if (ep == NULL)
263 return 0;
265 uvc_input_init(dev);
267 dev->status = kzalloc(sizeof(*dev->status), GFP_KERNEL);
268 if (!dev->status)
269 return -ENOMEM;
271 dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
272 if (!dev->int_urb) {
273 kfree(dev->status);
274 return -ENOMEM;
277 pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
280 * For high-speed interrupt endpoints, the bInterval value is used as
281 * an exponent of two. Some developers forgot about it.
283 interval = ep->desc.bInterval;
284 if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
285 (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
286 interval = fls(interval) - 1;
288 usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
289 dev->status, sizeof(*dev->status), uvc_status_complete,
290 dev, interval);
292 return 0;
295 void uvc_status_unregister(struct uvc_device *dev)
297 uvc_status_suspend(dev);
298 uvc_input_unregister(dev);
301 void uvc_status_cleanup(struct uvc_device *dev)
303 usb_free_urb(dev->int_urb);
304 kfree(dev->status);
307 static int uvc_status_start(struct uvc_device *dev, gfp_t flags)
309 lockdep_assert_held(&dev->status_lock);
311 if (!dev->int_urb)
312 return 0;
314 return usb_submit_urb(dev->int_urb, flags);
317 static void uvc_status_stop(struct uvc_device *dev)
319 struct uvc_ctrl_work *w = &dev->async_ctrl;
321 lockdep_assert_held(&dev->status_lock);
323 if (!dev->int_urb)
324 return;
327 * Prevent the asynchronous control handler from requeing the URB. The
328 * barrier is needed so the flush_status change is visible to other
329 * CPUs running the asynchronous handler before usb_kill_urb() is
330 * called below.
332 smp_store_release(&dev->flush_status, true);
335 * Cancel any pending asynchronous work. If any status event was queued,
336 * process it synchronously.
338 if (cancel_work_sync(&w->work))
339 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
341 /* Kill the urb. */
342 usb_kill_urb(dev->int_urb);
345 * The URB completion handler may have queued asynchronous work. This
346 * won't resubmit the URB as flush_status is set, but it needs to be
347 * cancelled before returning or it could then race with a future
348 * uvc_status_start() call.
350 if (cancel_work_sync(&w->work))
351 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
354 * From this point, there are no events on the queue and the status URB
355 * is dead. No events will be queued until uvc_status_start() is called.
356 * The barrier is needed to make sure that flush_status is visible to
357 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
358 * again.
360 smp_store_release(&dev->flush_status, false);
363 int uvc_status_resume(struct uvc_device *dev)
365 guard(mutex)(&dev->status_lock);
367 if (dev->status_users)
368 return uvc_status_start(dev, GFP_NOIO);
370 return 0;
373 void uvc_status_suspend(struct uvc_device *dev)
375 guard(mutex)(&dev->status_lock);
377 if (dev->status_users)
378 uvc_status_stop(dev);
381 int uvc_status_get(struct uvc_device *dev)
383 int ret;
385 guard(mutex)(&dev->status_lock);
387 if (!dev->status_users) {
388 ret = uvc_status_start(dev, GFP_KERNEL);
389 if (ret)
390 return ret;
393 dev->status_users++;
395 return 0;
398 void uvc_status_put(struct uvc_device *dev)
400 guard(mutex)(&dev->status_lock);
402 if (dev->status_users == 1)
403 uvc_status_stop(dev);
404 WARN_ON(!dev->status_users);
405 if (dev->status_users)
406 dev->status_users--;