• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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>
15 
16 #include "uvcvideo.h"
17 
18 /* --------------------------------------------------------------------------
19  * Input device
20  */
21 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
uvc_input_init(struct uvc_device * dev)22 static int uvc_input_init(struct uvc_device *dev)
23 {
24 	struct input_dev *input;
25 	int ret;
26 
27 	input = input_allocate_device();
28 	if (input == NULL)
29 		return -ENOMEM;
30 
31 	usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
32 	strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
33 
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;
38 
39 	__set_bit(EV_KEY, input->evbit);
40 	__set_bit(KEY_CAMERA, input->keybit);
41 
42 	if ((ret = input_register_device(input)) < 0)
43 		goto error;
44 
45 	dev->input = input;
46 	return 0;
47 
48 error:
49 	input_free_device(input);
50 	return ret;
51 }
52 
uvc_input_unregister(struct uvc_device * dev)53 static void uvc_input_unregister(struct uvc_device *dev)
54 {
55 	if (dev->input)
56 		input_unregister_device(dev->input);
57 }
58 
uvc_input_report_key(struct uvc_device * dev,unsigned int code,int value)59 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
60 	int value)
61 {
62 	if (dev->input) {
63 		input_report_key(dev->input, code, value);
64 		input_sync(dev->input);
65 	}
66 }
67 
68 #else
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 */
73 
74 /* --------------------------------------------------------------------------
75  * Status interrupt endpoint
76  */
77 struct uvc_streaming_status {
78 	u8	bStatusType;
79 	u8	bOriginator;
80 	u8	bEvent;
81 	u8	bValue[];
82 } __packed;
83 
84 struct uvc_control_status {
85 	u8	bStatusType;
86 	u8	bOriginator;
87 	u8	bEvent;
88 	u8	bSelector;
89 	u8	bAttribute;
90 	u8	bValue[];
91 } __packed;
92 
uvc_event_streaming(struct uvc_device * dev,struct uvc_streaming_status * status,int len)93 static void uvc_event_streaming(struct uvc_device *dev,
94 				struct uvc_streaming_status *status, int len)
95 {
96 	if (len < 3) {
97 		uvc_dbg(dev, STATUS,
98 			"Invalid streaming status event received\n");
99 		return;
100 	}
101 
102 	if (status->bEvent == 0) {
103 		if (len < 4)
104 			return;
105 		uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
106 			status->bOriginator,
107 			status->bValue[0] ? "pressed" : "released", len);
108 		uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
109 	} else {
110 		uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
111 			status->bOriginator, status->bEvent, len);
112 	}
113 }
114 
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
120 
uvc_event_entity_find_ctrl(struct uvc_entity * entity,u8 selector)121 static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
122 						      u8 selector)
123 {
124 	struct uvc_control *ctrl;
125 	unsigned int i;
126 
127 	for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
128 		if (ctrl->info.selector == selector)
129 			return ctrl;
130 
131 	return NULL;
132 }
133 
uvc_event_find_ctrl(struct uvc_device * dev,const struct uvc_control_status * status,struct uvc_video_chain ** chain)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)
137 {
138 	list_for_each_entry((*chain), &dev->chains, list) {
139 		struct uvc_entity *entity;
140 		struct uvc_control *ctrl;
141 
142 		list_for_each_entry(entity, &(*chain)->entities, chain) {
143 			if (entity->id != status->bOriginator)
144 				continue;
145 
146 			ctrl = uvc_event_entity_find_ctrl(entity,
147 							  status->bSelector);
148 			if (ctrl)
149 				return ctrl;
150 		}
151 	}
152 
153 	return NULL;
154 }
155 
uvc_event_control(struct urb * urb,const struct uvc_control_status * status,int len)156 static bool uvc_event_control(struct urb *urb,
157 			      const struct uvc_control_status *status, int len)
158 {
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;
163 
164 	if (len < 6 || status->bEvent != 0 ||
165 	    status->bAttribute >= ARRAY_SIZE(attrs)) {
166 		uvc_dbg(dev, STATUS, "Invalid control status event received\n");
167 		return false;
168 	}
169 
170 	uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
171 		status->bOriginator, status->bSelector,
172 		attrs[status->bAttribute], len);
173 
174 	/* Find the control. */
175 	ctrl = uvc_event_find_ctrl(dev, status, &chain);
176 	if (!ctrl)
177 		return false;
178 
179 	switch (status->bAttribute) {
180 	case UVC_CTRL_VALUE_CHANGE:
181 		return uvc_ctrl_status_event_async(urb, chain, ctrl,
182 						   status->bValue);
183 
184 	case UVC_CTRL_INFO_CHANGE:
185 	case UVC_CTRL_FAILURE_CHANGE:
186 	case UVC_CTRL_MIN_CHANGE:
187 	case UVC_CTRL_MAX_CHANGE:
188 		break;
189 	}
190 
191 	return false;
192 }
193 
uvc_status_complete(struct urb * urb)194 static void uvc_status_complete(struct urb *urb)
195 {
196 	struct uvc_device *dev = urb->context;
197 	int len, ret;
198 
199 	switch (urb->status) {
200 	case 0:
201 		break;
202 
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). */
208 		return;
209 
210 	default:
211 		dev_warn(&dev->udev->dev,
212 			 "Non-zero status (%d) in status completion handler.\n",
213 			 urb->status);
214 		return;
215 	}
216 
217 	len = urb->actual_length;
218 	if (len > 0) {
219 		switch (dev->status[0] & 0x0f) {
220 		case UVC_STATUS_TYPE_CONTROL: {
221 			struct uvc_control_status *status =
222 				(struct uvc_control_status *)dev->status;
223 
224 			if (uvc_event_control(urb, status, len))
225 				/* The URB will be resubmitted in work context. */
226 				return;
227 			break;
228 		}
229 
230 		case UVC_STATUS_TYPE_STREAMING: {
231 			struct uvc_streaming_status *status =
232 				(struct uvc_streaming_status *)dev->status;
233 
234 			uvc_event_streaming(dev, status, len);
235 			break;
236 		}
237 
238 		default:
239 			uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
240 				dev->status[0]);
241 			break;
242 		}
243 	}
244 
245 	/* Resubmit the URB. */
246 	urb->interval = dev->int_ep->desc.bInterval;
247 	ret = usb_submit_urb(urb, GFP_ATOMIC);
248 	if (ret < 0)
249 		dev_err(&dev->udev->dev,
250 			"Failed to resubmit status URB (%d).\n", ret);
251 }
252 
uvc_status_init(struct uvc_device * dev)253 int uvc_status_init(struct uvc_device *dev)
254 {
255 	struct usb_host_endpoint *ep = dev->int_ep;
256 	unsigned int pipe;
257 	int interval;
258 
259 	if (ep == NULL)
260 		return 0;
261 
262 	uvc_input_init(dev);
263 
264 	dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
265 	if (dev->status == NULL)
266 		return -ENOMEM;
267 
268 	dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
269 	if (dev->int_urb == NULL) {
270 		kfree(dev->status);
271 		return -ENOMEM;
272 	}
273 
274 	pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
275 
276 	/* For high-speed interrupt endpoints, the bInterval value is used as
277 	 * an exponent of two. Some developers forgot about it.
278 	 */
279 	interval = ep->desc.bInterval;
280 	if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
281 	    (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
282 		interval = fls(interval) - 1;
283 
284 	usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
285 		dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
286 		dev, interval);
287 
288 	return 0;
289 }
290 
uvc_status_unregister(struct uvc_device * dev)291 void uvc_status_unregister(struct uvc_device *dev)
292 {
293 	usb_kill_urb(dev->int_urb);
294 	uvc_input_unregister(dev);
295 }
296 
uvc_status_cleanup(struct uvc_device * dev)297 void uvc_status_cleanup(struct uvc_device *dev)
298 {
299 	usb_free_urb(dev->int_urb);
300 	kfree(dev->status);
301 }
302 
uvc_status_start(struct uvc_device * dev,gfp_t flags)303 int uvc_status_start(struct uvc_device *dev, gfp_t flags)
304 {
305 	if (dev->int_urb == NULL)
306 		return 0;
307 
308 	return usb_submit_urb(dev->int_urb, flags);
309 }
310 
uvc_status_stop(struct uvc_device * dev)311 void uvc_status_stop(struct uvc_device *dev)
312 {
313 	struct uvc_ctrl_work *w = &dev->async_ctrl;
314 
315 	/*
316 	 * Prevent the asynchronous control handler from requeing the URB. The
317 	 * barrier is needed so the flush_status change is visible to other
318 	 * CPUs running the asynchronous handler before usb_kill_urb() is
319 	 * called below.
320 	 */
321 	smp_store_release(&dev->flush_status, true);
322 
323 	/*
324 	 * Cancel any pending asynchronous work. If any status event was queued,
325 	 * process it synchronously.
326 	 */
327 	if (cancel_work_sync(&w->work))
328 		uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
329 
330 	/* Kill the urb. */
331 	usb_kill_urb(dev->int_urb);
332 
333 	/*
334 	 * The URB completion handler may have queued asynchronous work. This
335 	 * won't resubmit the URB as flush_status is set, but it needs to be
336 	 * cancelled before returning or it could then race with a future
337 	 * uvc_status_start() call.
338 	 */
339 	if (cancel_work_sync(&w->work))
340 		uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
341 
342 	/*
343 	 * From this point, there are no events on the queue and the status URB
344 	 * is dead. No events will be queued until uvc_status_start() is called.
345 	 * The barrier is needed to make sure that flush_status is visible to
346 	 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
347 	 * again.
348 	 */
349 	smp_store_release(&dev->flush_status, false);
350 }
351