1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_status.c -- USB Video Class driver - Status endpoint
5 * Copyright (C) 2005-2009
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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>
18 /* --------------------------------------------------------------------------
21 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
22 static int uvc_input_init(struct uvc_device *dev)
24 struct input_dev *input;
27 input = input_allocate_device();
31 usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
32 strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
34 input->name = dev->name;
35 input->phys = dev->input_phys;
36 usb_to_input_id(dev->udev, &input->id);
37 input->dev.parent = &dev->intf->dev;
39 __set_bit(EV_KEY, input->evbit);
40 __set_bit(KEY_CAMERA, input->keybit);
42 if ((ret = input_register_device(input)) < 0)
49 input_free_device(input);
53 static void uvc_input_unregister(struct uvc_device *dev)
56 input_unregister_device(dev->input);
59 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
63 input_report_key(dev->input, code, value);
64 input_sync(dev->input);
69 #define uvc_input_init(dev)
70 #define uvc_input_unregister(dev)
71 #define uvc_input_report_key(dev, code, value)
72 #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
74 /* --------------------------------------------------------------------------
75 * Status interrupt endpoint
77 struct uvc_streaming_status {
84 struct uvc_control_status {
93 static void uvc_event_streaming(struct uvc_device *dev,
94 struct uvc_streaming_status *status, int len)
97 uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
102 if (status->bEvent == 0) {
105 uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
107 status->bValue[0] ? "pressed" : "released", len);
108 uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
110 uvc_trace(UVC_TRACE_STATUS,
111 "Stream %u error event %02x len %d.\n",
112 status->bOriginator, status->bEvent, len);
116 #define UVC_CTRL_VALUE_CHANGE 0
117 #define UVC_CTRL_INFO_CHANGE 1
118 #define UVC_CTRL_FAILURE_CHANGE 2
119 #define UVC_CTRL_MIN_CHANGE 3
120 #define UVC_CTRL_MAX_CHANGE 4
122 static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
125 struct uvc_control *ctrl;
128 for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
129 if (ctrl->info.selector == selector)
135 static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
136 const struct uvc_control_status *status,
137 struct uvc_video_chain **chain)
139 list_for_each_entry((*chain), &dev->chains, list) {
140 struct uvc_entity *entity;
141 struct uvc_control *ctrl;
143 list_for_each_entry(entity, &(*chain)->entities, chain) {
144 if (entity->id != status->bOriginator)
147 ctrl = uvc_event_entity_find_ctrl(entity,
157 static bool uvc_event_control(struct urb *urb,
158 const struct uvc_control_status *status, int len)
160 static const char *attrs[] = { "value", "info", "failure", "min", "max" };
161 struct uvc_device *dev = urb->context;
162 struct uvc_video_chain *chain;
163 struct uvc_control *ctrl;
165 if (len < 6 || status->bEvent != 0 ||
166 status->bAttribute >= ARRAY_SIZE(attrs)) {
167 uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
172 uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
173 status->bOriginator, status->bSelector,
174 attrs[status->bAttribute], len);
176 /* Find the control. */
177 ctrl = uvc_event_find_ctrl(dev, status, &chain);
181 switch (status->bAttribute) {
182 case UVC_CTRL_VALUE_CHANGE:
183 return uvc_ctrl_status_event_async(urb, chain, ctrl,
186 case UVC_CTRL_INFO_CHANGE:
187 case UVC_CTRL_FAILURE_CHANGE:
188 case UVC_CTRL_MIN_CHANGE:
189 case UVC_CTRL_MAX_CHANGE:
196 static void uvc_status_complete(struct urb *urb)
198 struct uvc_device *dev = urb->context;
201 switch (urb->status) {
205 case -ENOENT: /* usb_kill_urb() called. */
206 case -ECONNRESET: /* usb_unlink_urb() called. */
207 case -ESHUTDOWN: /* The endpoint is being disabled. */
208 case -EPROTO: /* Device is disconnected (reported by some
209 * host controller). */
213 uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
214 "completion handler.\n", urb->status);
218 len = urb->actual_length;
220 switch (dev->status[0] & 0x0f) {
221 case UVC_STATUS_TYPE_CONTROL: {
222 struct uvc_control_status *status =
223 (struct uvc_control_status *)dev->status;
225 if (uvc_event_control(urb, status, len))
226 /* The URB will be resubmitted in work context. */
231 case UVC_STATUS_TYPE_STREAMING: {
232 struct uvc_streaming_status *status =
233 (struct uvc_streaming_status *)dev->status;
235 uvc_event_streaming(dev, status, len);
240 uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
241 "type %u.\n", dev->status[0]);
246 /* Resubmit the URB. */
247 urb->interval = dev->int_ep->desc.bInterval;
248 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
249 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
254 int uvc_status_init(struct uvc_device *dev)
256 struct usb_host_endpoint *ep = dev->int_ep;
265 dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
266 if (dev->status == NULL)
269 dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
270 if (dev->int_urb == NULL) {
275 pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
277 /* For high-speed interrupt endpoints, the bInterval value is used as
278 * an exponent of two. Some developers forgot about it.
280 interval = ep->desc.bInterval;
281 if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
282 (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
283 interval = fls(interval) - 1;
285 usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
286 dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
292 void uvc_status_unregister(struct uvc_device *dev)
294 usb_kill_urb(dev->int_urb);
295 uvc_input_unregister(dev);
298 void uvc_status_cleanup(struct uvc_device *dev)
300 usb_free_urb(dev->int_urb);
304 int uvc_status_start(struct uvc_device *dev, gfp_t flags)
306 if (dev->int_urb == NULL)
309 return usb_submit_urb(dev->int_urb, flags);
312 void uvc_status_stop(struct uvc_device *dev)
314 struct uvc_ctrl_work *w = &dev->async_ctrl;
317 * Prevent the asynchronous control handler from requeing the URB. The
318 * barrier is needed so the flush_status change is visible to other
319 * CPUs running the asynchronous handler before usb_kill_urb() is
322 smp_store_release(&dev->flush_status, true);
325 * Cancel any pending asynchronous work. If any status event was queued,
326 * process it synchronously.
328 if (cancel_work_sync(&w->work))
329 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
332 usb_kill_urb(dev->int_urb);
335 * The URB completion handler may have queued asynchronous work. This
336 * won't resubmit the URB as flush_status is set, but it needs to be
337 * cancelled before returning or it could then race with a future
338 * uvc_status_start() call.
340 if (cancel_work_sync(&w->work))
341 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
344 * From this point, there are no events on the queue and the status URB
345 * is dead. No events will be queued until uvc_status_start() is called.
346 * The barrier is needed to make sure that flush_status is visible to
347 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
350 smp_store_release(&dev->flush_status, false);