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 <linux/kernel.h>
10 #include <linux/input.h>
11 #include <linux/slab.h>
12 #include <linux/usb.h>
13 #include <linux/usb/input.h>
17 /* --------------------------------------------------------------------------
20 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
21 static int uvc_input_init(struct uvc_device
*dev
)
23 struct input_dev
*input
;
26 input
= input_allocate_device();
30 usb_make_path(dev
->udev
, dev
->input_phys
, sizeof(dev
->input_phys
));
31 strlcat(dev
->input_phys
, "/button", sizeof(dev
->input_phys
));
33 input
->name
= dev
->name
;
34 input
->phys
= dev
->input_phys
;
35 usb_to_input_id(dev
->udev
, &input
->id
);
36 input
->dev
.parent
= &dev
->intf
->dev
;
38 __set_bit(EV_KEY
, input
->evbit
);
39 __set_bit(KEY_CAMERA
, input
->keybit
);
41 if ((ret
= input_register_device(input
)) < 0)
48 input_free_device(input
);
52 static void uvc_input_unregister(struct uvc_device
*dev
)
55 input_unregister_device(dev
->input
);
58 static void uvc_input_report_key(struct uvc_device
*dev
, unsigned int code
,
62 input_report_key(dev
->input
, code
, value
);
63 input_sync(dev
->input
);
68 #define uvc_input_init(dev)
69 #define uvc_input_unregister(dev)
70 #define uvc_input_report_key(dev, code, value)
71 #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
73 /* --------------------------------------------------------------------------
74 * Status interrupt endpoint
76 struct uvc_streaming_status
{
83 struct uvc_control_status
{
92 static void uvc_event_streaming(struct uvc_device
*dev
,
93 struct uvc_streaming_status
*status
, int len
)
96 uvc_trace(UVC_TRACE_STATUS
, "Invalid streaming status event "
101 if (status
->bEvent
== 0) {
104 uvc_trace(UVC_TRACE_STATUS
, "Button (intf %u) %s len %d\n",
106 status
->bValue
[0] ? "pressed" : "released", len
);
107 uvc_input_report_key(dev
, KEY_CAMERA
, status
->bValue
[0]);
109 uvc_trace(UVC_TRACE_STATUS
,
110 "Stream %u error event %02x len %d.\n",
111 status
->bOriginator
, status
->bEvent
, len
);
115 #define UVC_CTRL_VALUE_CHANGE 0
116 #define UVC_CTRL_INFO_CHANGE 1
117 #define UVC_CTRL_FAILURE_CHANGE 2
118 #define UVC_CTRL_MIN_CHANGE 3
119 #define UVC_CTRL_MAX_CHANGE 4
121 static struct uvc_control
*uvc_event_entity_find_ctrl(struct uvc_entity
*entity
,
124 struct uvc_control
*ctrl
;
127 for (i
= 0, ctrl
= entity
->controls
; i
< entity
->ncontrols
; i
++, ctrl
++)
128 if (ctrl
->info
.selector
== selector
)
134 static struct uvc_control
*uvc_event_find_ctrl(struct uvc_device
*dev
,
135 const struct uvc_control_status
*status
,
136 struct uvc_video_chain
**chain
)
138 list_for_each_entry((*chain
), &dev
->chains
, list
) {
139 struct uvc_entity
*entity
;
140 struct uvc_control
*ctrl
;
142 list_for_each_entry(entity
, &(*chain
)->entities
, chain
) {
143 if (entity
->id
!= status
->bOriginator
)
146 ctrl
= uvc_event_entity_find_ctrl(entity
,
156 static bool uvc_event_control(struct urb
*urb
,
157 const struct uvc_control_status
*status
, int len
)
159 static const char *attrs
[] = { "value", "info", "failure", "min", "max" };
160 struct uvc_device
*dev
= urb
->context
;
161 struct uvc_video_chain
*chain
;
162 struct uvc_control
*ctrl
;
164 if (len
< 6 || status
->bEvent
!= 0 ||
165 status
->bAttribute
>= ARRAY_SIZE(attrs
)) {
166 uvc_trace(UVC_TRACE_STATUS
, "Invalid control status event "
171 uvc_trace(UVC_TRACE_STATUS
, "Control %u/%u %s change len %d.\n",
172 status
->bOriginator
, status
->bSelector
,
173 attrs
[status
->bAttribute
], len
);
175 /* Find the control. */
176 ctrl
= uvc_event_find_ctrl(dev
, status
, &chain
);
180 switch (status
->bAttribute
) {
181 case UVC_CTRL_VALUE_CHANGE
:
182 return uvc_ctrl_status_event(urb
, chain
, ctrl
, status
->bValue
);
184 case UVC_CTRL_INFO_CHANGE
:
185 case UVC_CTRL_FAILURE_CHANGE
:
186 case UVC_CTRL_MIN_CHANGE
:
187 case UVC_CTRL_MAX_CHANGE
:
194 static void uvc_status_complete(struct urb
*urb
)
196 struct uvc_device
*dev
= urb
->context
;
199 switch (urb
->status
) {
203 case -ENOENT
: /* usb_kill_urb() called. */
204 case -ECONNRESET
: /* usb_unlink_urb() called. */
205 case -ESHUTDOWN
: /* The endpoint is being disabled. */
206 case -EPROTO
: /* Device is disconnected (reported by some
207 * host controller). */
211 uvc_printk(KERN_WARNING
, "Non-zero status (%d) in status "
212 "completion handler.\n", urb
->status
);
216 len
= urb
->actual_length
;
218 switch (dev
->status
[0] & 0x0f) {
219 case UVC_STATUS_TYPE_CONTROL
: {
220 struct uvc_control_status
*status
=
221 (struct uvc_control_status
*)dev
->status
;
223 if (uvc_event_control(urb
, status
, len
))
224 /* The URB will be resubmitted in work context. */
229 case UVC_STATUS_TYPE_STREAMING
: {
230 struct uvc_streaming_status
*status
=
231 (struct uvc_streaming_status
*)dev
->status
;
233 uvc_event_streaming(dev
, status
, len
);
238 uvc_trace(UVC_TRACE_STATUS
, "Unknown status event "
239 "type %u.\n", dev
->status
[0]);
244 /* Resubmit the URB. */
245 urb
->interval
= dev
->int_ep
->desc
.bInterval
;
246 if ((ret
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
247 uvc_printk(KERN_ERR
, "Failed to resubmit status URB (%d).\n",
252 int uvc_status_init(struct uvc_device
*dev
)
254 struct usb_host_endpoint
*ep
= dev
->int_ep
;
263 dev
->status
= kzalloc(UVC_MAX_STATUS_SIZE
, GFP_KERNEL
);
264 if (dev
->status
== NULL
)
267 dev
->int_urb
= usb_alloc_urb(0, GFP_KERNEL
);
268 if (dev
->int_urb
== NULL
) {
273 pipe
= usb_rcvintpipe(dev
->udev
, ep
->desc
.bEndpointAddress
);
275 /* For high-speed interrupt endpoints, the bInterval value is used as
276 * an exponent of two. Some developers forgot about it.
278 interval
= ep
->desc
.bInterval
;
279 if (interval
> 16 && dev
->udev
->speed
== USB_SPEED_HIGH
&&
280 (dev
->quirks
& UVC_QUIRK_STATUS_INTERVAL
))
281 interval
= fls(interval
) - 1;
283 usb_fill_int_urb(dev
->int_urb
, dev
->udev
, pipe
,
284 dev
->status
, UVC_MAX_STATUS_SIZE
, uvc_status_complete
,
290 void uvc_status_unregister(struct uvc_device
*dev
)
292 usb_kill_urb(dev
->int_urb
);
293 uvc_input_unregister(dev
);
296 void uvc_status_cleanup(struct uvc_device
*dev
)
298 usb_free_urb(dev
->int_urb
);
302 int uvc_status_start(struct uvc_device
*dev
, gfp_t flags
)
304 if (dev
->int_urb
== NULL
)
307 return usb_submit_urb(dev
->int_urb
, flags
);
310 void uvc_status_stop(struct uvc_device
*dev
)
312 usb_kill_urb(dev
->int_urb
);