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
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)
42 static int uvc_input_init(struct uvc_device
*dev
)
44 struct input_dev
*input
;
47 if (!uvc_input_has_button(dev
))
50 input
= input_allocate_device();
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)
72 input_free_device(input
);
76 static void uvc_input_unregister(struct uvc_device
*dev
)
79 input_unregister_device(dev
->input
);
82 static void uvc_input_report_key(struct uvc_device
*dev
, unsigned int code
,
86 input_report_key(dev
->input
, code
, value
);
87 input_sync(dev
->input
);
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
)) {
105 "Invalid streaming status event received\n");
109 if (status
->bEvent
== 0) {
110 if (len
<= offsetof(struct uvc_status
, streaming
))
113 uvc_dbg(dev
, STATUS
, "Button (intf %u) %s len %d\n",
115 status
->streaming
.button
? "pressed" : "released", len
);
116 uvc_input_report_key(dev
, KEY_CAMERA
, status
->streaming
.button
);
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
,
132 struct uvc_control
*ctrl
;
135 for (i
= 0, ctrl
= entity
->controls
; i
< entity
->ncontrols
; i
++, ctrl
++)
136 if (ctrl
->info
.selector
== selector
)
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
)
154 ctrl
= uvc_event_entity_find_ctrl(entity
,
155 status
->control
.bSelector
);
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");
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
);
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
:
202 static void uvc_status_complete(struct urb
*urb
)
204 struct uvc_device
*dev
= urb
->context
;
207 switch (urb
->status
) {
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). */
218 dev_warn(&dev
->udev
->dev
,
219 "Non-zero status (%d) in status completion handler.\n",
224 len
= urb
->actual_length
;
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. */
234 case UVC_STATUS_TYPE_STREAMING
: {
235 uvc_event_streaming(dev
, dev
->status
, len
);
240 uvc_dbg(dev
, STATUS
, "Unknown status event type %u\n",
241 dev
->status
->bStatusType
);
246 /* Resubmit the URB. */
247 urb
->interval
= dev
->int_ep
->desc
.bInterval
;
248 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
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
;
260 mutex_init(&dev
->status_lock
);
267 dev
->status
= kzalloc(sizeof(*dev
->status
), GFP_KERNEL
);
271 dev
->int_urb
= usb_alloc_urb(0, GFP_KERNEL
);
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
,
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
);
307 static int uvc_status_start(struct uvc_device
*dev
, gfp_t flags
)
309 lockdep_assert_held(&dev
->status_lock
);
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
);
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
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
);
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
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
);
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
)
385 guard(mutex
)(&dev
->status_lock
);
387 if (!dev
->status_users
) {
388 ret
= uvc_status_start(dev
, GFP_KERNEL
);
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
)