xref: /openbmc/linux/drivers/media/usb/uvc/uvc_status.c (revision 7033b937)
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 
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 
15 #include "uvcvideo.h"
16 
17 /* --------------------------------------------------------------------------
18  * Input device
19  */
20 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
21 static int uvc_input_init(struct uvc_device *dev)
22 {
23 	struct input_dev *input;
24 	int ret;
25 
26 	input = input_allocate_device();
27 	if (input == NULL)
28 		return -ENOMEM;
29 
30 	usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
31 	strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
32 
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;
37 
38 	__set_bit(EV_KEY, input->evbit);
39 	__set_bit(KEY_CAMERA, input->keybit);
40 
41 	if ((ret = input_register_device(input)) < 0)
42 		goto error;
43 
44 	dev->input = input;
45 	return 0;
46 
47 error:
48 	input_free_device(input);
49 	return ret;
50 }
51 
52 static void uvc_input_unregister(struct uvc_device *dev)
53 {
54 	if (dev->input)
55 		input_unregister_device(dev->input);
56 }
57 
58 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
59 	int value)
60 {
61 	if (dev->input) {
62 		input_report_key(dev->input, code, value);
63 		input_sync(dev->input);
64 	}
65 }
66 
67 #else
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 */
72 
73 /* --------------------------------------------------------------------------
74  * Status interrupt endpoint
75  */
76 struct uvc_streaming_status {
77 	u8	bStatusType;
78 	u8	bOriginator;
79 	u8	bEvent;
80 	u8	bValue[];
81 } __packed;
82 
83 struct uvc_control_status {
84 	u8	bStatusType;
85 	u8	bOriginator;
86 	u8	bEvent;
87 	u8	bSelector;
88 	u8	bAttribute;
89 	u8	bValue[];
90 } __packed;
91 
92 static void uvc_event_streaming(struct uvc_device *dev,
93 				struct uvc_streaming_status *status, int len)
94 {
95 	if (len < 3) {
96 		uvc_dbg(dev, STATUS,
97 			"Invalid streaming status event received\n");
98 		return;
99 	}
100 
101 	if (status->bEvent == 0) {
102 		if (len < 4)
103 			return;
104 		uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
105 			status->bOriginator,
106 			status->bValue[0] ? "pressed" : "released", len);
107 		uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
108 	} else {
109 		uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
110 			status->bOriginator, status->bEvent, len);
111 	}
112 }
113 
114 #define UVC_CTRL_VALUE_CHANGE	0
115 #define UVC_CTRL_INFO_CHANGE	1
116 #define UVC_CTRL_FAILURE_CHANGE	2
117 #define UVC_CTRL_MIN_CHANGE	3
118 #define UVC_CTRL_MAX_CHANGE	4
119 
120 static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
121 						      u8 selector)
122 {
123 	struct uvc_control *ctrl;
124 	unsigned int i;
125 
126 	for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
127 		if (ctrl->info.selector == selector)
128 			return ctrl;
129 
130 	return NULL;
131 }
132 
133 static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
134 					const struct uvc_control_status *status,
135 					struct uvc_video_chain **chain)
136 {
137 	list_for_each_entry((*chain), &dev->chains, list) {
138 		struct uvc_entity *entity;
139 		struct uvc_control *ctrl;
140 
141 		list_for_each_entry(entity, &(*chain)->entities, chain) {
142 			if (entity->id != status->bOriginator)
143 				continue;
144 
145 			ctrl = uvc_event_entity_find_ctrl(entity,
146 							  status->bSelector);
147 			if (ctrl)
148 				return ctrl;
149 		}
150 	}
151 
152 	return NULL;
153 }
154 
155 static bool uvc_event_control(struct urb *urb,
156 			      const struct uvc_control_status *status, int len)
157 {
158 	static const char *attrs[] = { "value", "info", "failure", "min", "max" };
159 	struct uvc_device *dev = urb->context;
160 	struct uvc_video_chain *chain;
161 	struct uvc_control *ctrl;
162 
163 	if (len < 6 || status->bEvent != 0 ||
164 	    status->bAttribute >= ARRAY_SIZE(attrs)) {
165 		uvc_dbg(dev, STATUS, "Invalid control status event received\n");
166 		return false;
167 	}
168 
169 	uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
170 		status->bOriginator, status->bSelector,
171 		attrs[status->bAttribute], len);
172 
173 	/* Find the control. */
174 	ctrl = uvc_event_find_ctrl(dev, status, &chain);
175 	if (!ctrl)
176 		return false;
177 
178 	switch (status->bAttribute) {
179 	case UVC_CTRL_VALUE_CHANGE:
180 		return uvc_ctrl_status_event_async(urb, chain, ctrl,
181 						   status->bValue);
182 
183 	case UVC_CTRL_INFO_CHANGE:
184 	case UVC_CTRL_FAILURE_CHANGE:
185 	case UVC_CTRL_MIN_CHANGE:
186 	case UVC_CTRL_MAX_CHANGE:
187 		break;
188 	}
189 
190 	return false;
191 }
192 
193 static void uvc_status_complete(struct urb *urb)
194 {
195 	struct uvc_device *dev = urb->context;
196 	int len, ret;
197 
198 	switch (urb->status) {
199 	case 0:
200 		break;
201 
202 	case -ENOENT:		/* usb_kill_urb() called. */
203 	case -ECONNRESET:	/* usb_unlink_urb() called. */
204 	case -ESHUTDOWN:	/* The endpoint is being disabled. */
205 	case -EPROTO:		/* Device is disconnected (reported by some host controllers). */
206 		return;
207 
208 	default:
209 		dev_warn(&dev->udev->dev,
210 			 "Non-zero status (%d) in status completion handler.\n",
211 			 urb->status);
212 		return;
213 	}
214 
215 	len = urb->actual_length;
216 	if (len > 0) {
217 		switch (dev->status[0] & 0x0f) {
218 		case UVC_STATUS_TYPE_CONTROL: {
219 			struct uvc_control_status *status =
220 				(struct uvc_control_status *)dev->status;
221 
222 			if (uvc_event_control(urb, status, len))
223 				/* The URB will be resubmitted in work context. */
224 				return;
225 			break;
226 		}
227 
228 		case UVC_STATUS_TYPE_STREAMING: {
229 			struct uvc_streaming_status *status =
230 				(struct uvc_streaming_status *)dev->status;
231 
232 			uvc_event_streaming(dev, status, len);
233 			break;
234 		}
235 
236 		default:
237 			uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
238 				dev->status[0]);
239 			break;
240 		}
241 	}
242 
243 	/* Resubmit the URB. */
244 	urb->interval = dev->int_ep->desc.bInterval;
245 	ret = usb_submit_urb(urb, GFP_ATOMIC);
246 	if (ret < 0)
247 		dev_err(&dev->udev->dev,
248 			"Failed to resubmit status URB (%d).\n", ret);
249 }
250 
251 int uvc_status_init(struct uvc_device *dev)
252 {
253 	struct usb_host_endpoint *ep = dev->int_ep;
254 	unsigned int pipe;
255 	int interval;
256 
257 	if (ep == NULL)
258 		return 0;
259 
260 	uvc_input_init(dev);
261 
262 	dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
263 	if (dev->status == NULL)
264 		return -ENOMEM;
265 
266 	dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
267 	if (dev->int_urb == NULL) {
268 		kfree(dev->status);
269 		return -ENOMEM;
270 	}
271 
272 	pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
273 
274 	/*
275 	 * For high-speed interrupt endpoints, the bInterval value is used as
276 	 * an exponent of two. Some developers forgot about it.
277 	 */
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;
282 
283 	usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
284 		dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
285 		dev, interval);
286 
287 	return 0;
288 }
289 
290 void uvc_status_unregister(struct uvc_device *dev)
291 {
292 	usb_kill_urb(dev->int_urb);
293 	uvc_input_unregister(dev);
294 }
295 
296 void uvc_status_cleanup(struct uvc_device *dev)
297 {
298 	usb_free_urb(dev->int_urb);
299 	kfree(dev->status);
300 }
301 
302 int uvc_status_start(struct uvc_device *dev, gfp_t flags)
303 {
304 	if (dev->int_urb == NULL)
305 		return 0;
306 
307 	return usb_submit_urb(dev->int_urb, flags);
308 }
309 
310 void uvc_status_stop(struct uvc_device *dev)
311 {
312 	usb_kill_urb(dev->int_urb);
313 }
314