1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *	uvc_gadget.c  --  USB Video Class Gadget driver
4  *
5  *	Copyright (C) 2009-2010
6  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/string.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/g_uvc.h>
20 #include <linux/usb/video.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-event.h>
26 
27 #include "uvc.h"
28 #include "uvc_configfs.h"
29 #include "uvc_v4l2.h"
30 #include "uvc_video.h"
31 
32 unsigned int uvc_gadget_trace_param;
33 module_param_named(trace, uvc_gadget_trace_param, uint, 0644);
34 MODULE_PARM_DESC(trace, "Trace level bitmask");
35 
36 /* --------------------------------------------------------------------------
37  * Function descriptors
38  */
39 
40 /* string IDs are assigned dynamically */
41 
42 #define UVC_STRING_CONTROL_IDX			0
43 #define UVC_STRING_STREAMING_IDX		1
44 
45 static struct usb_string uvc_en_us_strings[] = {
46 	/* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */
47 	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
48 	{  }
49 };
50 
51 static struct usb_gadget_strings uvc_stringtab = {
52 	.language = 0x0409,	/* en-us */
53 	.strings = uvc_en_us_strings,
54 };
55 
56 static struct usb_gadget_strings *uvc_function_strings[] = {
57 	&uvc_stringtab,
58 	NULL,
59 };
60 
61 #define UVC_INTF_VIDEO_CONTROL			0
62 #define UVC_INTF_VIDEO_STREAMING		1
63 
64 #define UVC_STATUS_MAX_PACKET_SIZE		16	/* 16 bytes status */
65 
66 static struct usb_interface_assoc_descriptor uvc_iad = {
67 	.bLength		= sizeof(uvc_iad),
68 	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
69 	.bFirstInterface	= 0,
70 	.bInterfaceCount	= 2,
71 	.bFunctionClass		= USB_CLASS_VIDEO,
72 	.bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION,
73 	.bFunctionProtocol	= 0x00,
74 	.iFunction		= 0,
75 };
76 
77 static struct usb_interface_descriptor uvc_control_intf = {
78 	.bLength		= USB_DT_INTERFACE_SIZE,
79 	.bDescriptorType	= USB_DT_INTERFACE,
80 	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
81 	.bAlternateSetting	= 0,
82 	.bNumEndpoints		= 1,
83 	.bInterfaceClass	= USB_CLASS_VIDEO,
84 	.bInterfaceSubClass	= UVC_SC_VIDEOCONTROL,
85 	.bInterfaceProtocol	= 0x00,
86 	.iInterface		= 0,
87 };
88 
89 static struct usb_endpoint_descriptor uvc_control_ep = {
90 	.bLength		= USB_DT_ENDPOINT_SIZE,
91 	.bDescriptorType	= USB_DT_ENDPOINT,
92 	.bEndpointAddress	= USB_DIR_IN,
93 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
94 	.wMaxPacketSize		= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
95 	.bInterval		= 8,
96 };
97 
98 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
99 	.bLength		= sizeof(uvc_ss_control_comp),
100 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
101 	/* The following 3 values can be tweaked if necessary. */
102 	.bMaxBurst		= 0,
103 	.bmAttributes		= 0,
104 	.wBytesPerInterval	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
105 };
106 
107 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
108 	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
109 	.bDescriptorType	= USB_DT_CS_ENDPOINT,
110 	.bDescriptorSubType	= UVC_EP_INTERRUPT,
111 	.wMaxTransferSize	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
112 };
113 
114 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
115 	.bLength		= USB_DT_INTERFACE_SIZE,
116 	.bDescriptorType	= USB_DT_INTERFACE,
117 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
118 	.bAlternateSetting	= 0,
119 	.bNumEndpoints		= 0,
120 	.bInterfaceClass	= USB_CLASS_VIDEO,
121 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
122 	.bInterfaceProtocol	= 0x00,
123 	.iInterface		= 0,
124 };
125 
126 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
127 	.bLength		= USB_DT_INTERFACE_SIZE,
128 	.bDescriptorType	= USB_DT_INTERFACE,
129 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
130 	.bAlternateSetting	= 1,
131 	.bNumEndpoints		= 1,
132 	.bInterfaceClass	= USB_CLASS_VIDEO,
133 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
134 	.bInterfaceProtocol	= 0x00,
135 	.iInterface		= 0,
136 };
137 
138 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
139 	.bLength		= USB_DT_ENDPOINT_SIZE,
140 	.bDescriptorType	= USB_DT_ENDPOINT,
141 	.bEndpointAddress	= USB_DIR_IN,
142 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
143 				| USB_ENDPOINT_XFER_ISOC,
144 	/* The wMaxPacketSize and bInterval values will be initialized from
145 	 * module parameters.
146 	 */
147 };
148 
149 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
150 	.bLength		= USB_DT_ENDPOINT_SIZE,
151 	.bDescriptorType	= USB_DT_ENDPOINT,
152 	.bEndpointAddress	= USB_DIR_IN,
153 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
154 				| USB_ENDPOINT_XFER_ISOC,
155 	/* The wMaxPacketSize and bInterval values will be initialized from
156 	 * module parameters.
157 	 */
158 };
159 
160 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
161 	.bLength		= USB_DT_ENDPOINT_SIZE,
162 	.bDescriptorType	= USB_DT_ENDPOINT,
163 
164 	.bEndpointAddress	= USB_DIR_IN,
165 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
166 				| USB_ENDPOINT_XFER_ISOC,
167 	/* The wMaxPacketSize and bInterval values will be initialized from
168 	 * module parameters.
169 	 */
170 };
171 
172 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
173 	.bLength		= sizeof(uvc_ss_streaming_comp),
174 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
175 	/* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
176 	 * initialized from module parameters.
177 	 */
178 };
179 
180 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
181 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
182 	(struct usb_descriptor_header *) &uvc_fs_streaming_ep,
183 	NULL,
184 };
185 
186 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
187 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
188 	(struct usb_descriptor_header *) &uvc_hs_streaming_ep,
189 	NULL,
190 };
191 
192 static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
193 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
194 	(struct usb_descriptor_header *) &uvc_ss_streaming_ep,
195 	(struct usb_descriptor_header *) &uvc_ss_streaming_comp,
196 	NULL,
197 };
198 
199 /* --------------------------------------------------------------------------
200  * Control requests
201  */
202 
203 static void
204 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
205 {
206 	struct uvc_device *uvc = req->context;
207 	struct v4l2_event v4l2_event;
208 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
209 
210 	if (uvc->event_setup_out) {
211 		uvc->event_setup_out = 0;
212 
213 		memset(&v4l2_event, 0, sizeof(v4l2_event));
214 		v4l2_event.type = UVC_EVENT_DATA;
215 		uvc_event->data.length = req->actual;
216 		memcpy(&uvc_event->data.data, req->buf, req->actual);
217 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
218 	}
219 }
220 
221 static int
222 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
223 {
224 	struct uvc_device *uvc = to_uvc(f);
225 	struct v4l2_event v4l2_event;
226 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
227 
228 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
229 		uvcg_info(f, "invalid request type\n");
230 		return -EINVAL;
231 	}
232 
233 	/* Stall too big requests. */
234 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
235 		return -EINVAL;
236 
237 	/* Tell the complete callback to generate an event for the next request
238 	 * that will be enqueued by UVCIOC_SEND_RESPONSE.
239 	 */
240 	uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
241 	uvc->event_length = le16_to_cpu(ctrl->wLength);
242 
243 	memset(&v4l2_event, 0, sizeof(v4l2_event));
244 	v4l2_event.type = UVC_EVENT_SETUP;
245 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
246 	v4l2_event_queue(&uvc->vdev, &v4l2_event);
247 
248 	return 0;
249 }
250 
251 void uvc_function_setup_continue(struct uvc_device *uvc)
252 {
253 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
254 
255 	usb_composite_setup_continue(cdev);
256 }
257 
258 static int
259 uvc_function_get_alt(struct usb_function *f, unsigned interface)
260 {
261 	struct uvc_device *uvc = to_uvc(f);
262 
263 	uvcg_info(f, "%s(%u)\n", __func__, interface);
264 
265 	if (interface == uvc->control_intf)
266 		return 0;
267 	else if (interface != uvc->streaming_intf)
268 		return -EINVAL;
269 	else
270 		return uvc->video.ep->enabled ? 1 : 0;
271 }
272 
273 static int
274 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
275 {
276 	struct uvc_device *uvc = to_uvc(f);
277 	struct usb_composite_dev *cdev = f->config->cdev;
278 	struct v4l2_event v4l2_event;
279 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
280 	int ret;
281 
282 	uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
283 
284 	if (interface == uvc->control_intf) {
285 		if (alt)
286 			return -EINVAL;
287 
288 		uvcg_info(f, "reset UVC Control\n");
289 		usb_ep_disable(uvc->control_ep);
290 
291 		if (!uvc->control_ep->desc)
292 			if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
293 				return -EINVAL;
294 
295 		usb_ep_enable(uvc->control_ep);
296 
297 		if (uvc->state == UVC_STATE_DISCONNECTED) {
298 			memset(&v4l2_event, 0, sizeof(v4l2_event));
299 			v4l2_event.type = UVC_EVENT_CONNECT;
300 			uvc_event->speed = cdev->gadget->speed;
301 			v4l2_event_queue(&uvc->vdev, &v4l2_event);
302 
303 			uvc->state = UVC_STATE_CONNECTED;
304 		}
305 
306 		return 0;
307 	}
308 
309 	if (interface != uvc->streaming_intf)
310 		return -EINVAL;
311 
312 	/* TODO
313 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
314 		return alt ? -EINVAL : 0;
315 	*/
316 
317 	switch (alt) {
318 	case 0:
319 		if (uvc->state != UVC_STATE_STREAMING)
320 			return 0;
321 
322 		if (uvc->video.ep)
323 			usb_ep_disable(uvc->video.ep);
324 
325 		memset(&v4l2_event, 0, sizeof(v4l2_event));
326 		v4l2_event.type = UVC_EVENT_STREAMOFF;
327 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
328 
329 		uvc->state = UVC_STATE_CONNECTED;
330 		return 0;
331 
332 	case 1:
333 		if (uvc->state != UVC_STATE_CONNECTED)
334 			return 0;
335 
336 		if (!uvc->video.ep)
337 			return -EINVAL;
338 
339 		uvcg_info(f, "reset UVC\n");
340 		usb_ep_disable(uvc->video.ep);
341 
342 		ret = config_ep_by_speed(f->config->cdev->gadget,
343 				&(uvc->func), uvc->video.ep);
344 		if (ret)
345 			return ret;
346 		usb_ep_enable(uvc->video.ep);
347 
348 		memset(&v4l2_event, 0, sizeof(v4l2_event));
349 		v4l2_event.type = UVC_EVENT_STREAMON;
350 		v4l2_event_queue(&uvc->vdev, &v4l2_event);
351 		return USB_GADGET_DELAYED_STATUS;
352 
353 	default:
354 		return -EINVAL;
355 	}
356 }
357 
358 static void
359 uvc_function_disable(struct usb_function *f)
360 {
361 	struct uvc_device *uvc = to_uvc(f);
362 	struct v4l2_event v4l2_event;
363 
364 	uvcg_info(f, "%s()\n", __func__);
365 
366 	memset(&v4l2_event, 0, sizeof(v4l2_event));
367 	v4l2_event.type = UVC_EVENT_DISCONNECT;
368 	v4l2_event_queue(&uvc->vdev, &v4l2_event);
369 
370 	uvc->state = UVC_STATE_DISCONNECTED;
371 
372 	usb_ep_disable(uvc->video.ep);
373 	usb_ep_disable(uvc->control_ep);
374 }
375 
376 /* --------------------------------------------------------------------------
377  * Connection / disconnection
378  */
379 
380 void
381 uvc_function_connect(struct uvc_device *uvc)
382 {
383 	int ret;
384 
385 	if ((ret = usb_function_activate(&uvc->func)) < 0)
386 		uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
387 }
388 
389 void
390 uvc_function_disconnect(struct uvc_device *uvc)
391 {
392 	int ret;
393 
394 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
395 		uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
396 }
397 
398 /* --------------------------------------------------------------------------
399  * USB probe and disconnect
400  */
401 
402 static ssize_t function_name_show(struct device *dev,
403 				  struct device_attribute *attr, char *buf)
404 {
405 	struct uvc_device *uvc = dev_get_drvdata(dev);
406 
407 	return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
408 }
409 
410 static DEVICE_ATTR_RO(function_name);
411 
412 static int
413 uvc_register_video(struct uvc_device *uvc)
414 {
415 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
416 	int ret;
417 
418 	/* TODO reference counting. */
419 	memset(&uvc->vdev, 0, sizeof(uvc->video));
420 	uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
421 	uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev;
422 	uvc->vdev.fops = &uvc_v4l2_fops;
423 	uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
424 	uvc->vdev.release = video_device_release_empty;
425 	uvc->vdev.vfl_dir = VFL_DIR_TX;
426 	uvc->vdev.lock = &uvc->video.mutex;
427 	uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
428 	strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
429 
430 	video_set_drvdata(&uvc->vdev, uvc);
431 
432 	ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1);
433 	if (ret < 0)
434 		return ret;
435 
436 	ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
437 	if (ret < 0) {
438 		video_unregister_device(&uvc->vdev);
439 		return ret;
440 	}
441 
442 	return 0;
443 }
444 
445 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
446 	do { \
447 		memcpy(mem, desc, (desc)->bLength); \
448 		*(dst)++ = mem; \
449 		mem += (desc)->bLength; \
450 	} while (0);
451 
452 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
453 	do { \
454 		const struct usb_descriptor_header * const *__src; \
455 		for (__src = src; *__src; ++__src) { \
456 			memcpy(mem, *__src, (*__src)->bLength); \
457 			*dst++ = mem; \
458 			mem += (*__src)->bLength; \
459 		} \
460 	} while (0)
461 
462 static struct usb_descriptor_header **
463 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
464 {
465 	struct uvc_input_header_descriptor *uvc_streaming_header;
466 	struct uvc_header_descriptor *uvc_control_header;
467 	const struct uvc_descriptor_header * const *uvc_control_desc;
468 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
469 	const struct usb_descriptor_header * const *uvc_streaming_std;
470 	const struct usb_descriptor_header * const *src;
471 	struct usb_descriptor_header **dst;
472 	struct usb_descriptor_header **hdr;
473 	unsigned int control_size;
474 	unsigned int streaming_size;
475 	unsigned int n_desc;
476 	unsigned int bytes;
477 	void *mem;
478 
479 	switch (speed) {
480 	case USB_SPEED_SUPER:
481 		uvc_control_desc = uvc->desc.ss_control;
482 		uvc_streaming_cls = uvc->desc.ss_streaming;
483 		uvc_streaming_std = uvc_ss_streaming;
484 		break;
485 
486 	case USB_SPEED_HIGH:
487 		uvc_control_desc = uvc->desc.fs_control;
488 		uvc_streaming_cls = uvc->desc.hs_streaming;
489 		uvc_streaming_std = uvc_hs_streaming;
490 		break;
491 
492 	case USB_SPEED_FULL:
493 	default:
494 		uvc_control_desc = uvc->desc.fs_control;
495 		uvc_streaming_cls = uvc->desc.fs_streaming;
496 		uvc_streaming_std = uvc_fs_streaming;
497 		break;
498 	}
499 
500 	if (!uvc_control_desc || !uvc_streaming_cls)
501 		return ERR_PTR(-ENODEV);
502 
503 	/* Descriptors layout
504 	 *
505 	 * uvc_iad
506 	 * uvc_control_intf
507 	 * Class-specific UVC control descriptors
508 	 * uvc_control_ep
509 	 * uvc_control_cs_ep
510 	 * uvc_ss_control_comp (for SS only)
511 	 * uvc_streaming_intf_alt0
512 	 * Class-specific UVC streaming descriptors
513 	 * uvc_{fs|hs}_streaming
514 	 */
515 
516 	/* Count descriptors and compute their size. */
517 	control_size = 0;
518 	streaming_size = 0;
519 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
520 	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
521 	      + uvc_streaming_intf_alt0.bLength;
522 
523 	if (speed == USB_SPEED_SUPER) {
524 		bytes += uvc_ss_control_comp.bLength;
525 		n_desc = 6;
526 	} else {
527 		n_desc = 5;
528 	}
529 
530 	for (src = (const struct usb_descriptor_header **)uvc_control_desc;
531 	     *src; ++src) {
532 		control_size += (*src)->bLength;
533 		bytes += (*src)->bLength;
534 		n_desc++;
535 	}
536 	for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
537 	     *src; ++src) {
538 		streaming_size += (*src)->bLength;
539 		bytes += (*src)->bLength;
540 		n_desc++;
541 	}
542 	for (src = uvc_streaming_std; *src; ++src) {
543 		bytes += (*src)->bLength;
544 		n_desc++;
545 	}
546 
547 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
548 	if (mem == NULL)
549 		return NULL;
550 
551 	hdr = mem;
552 	dst = mem;
553 	mem += (n_desc + 1) * sizeof(*src);
554 
555 	/* Copy the descriptors. */
556 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
557 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
558 
559 	uvc_control_header = mem;
560 	UVC_COPY_DESCRIPTORS(mem, dst,
561 		(const struct usb_descriptor_header **)uvc_control_desc);
562 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
563 	uvc_control_header->bInCollection = 1;
564 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
565 
566 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
567 	if (speed == USB_SPEED_SUPER)
568 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
569 
570 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
571 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
572 
573 	uvc_streaming_header = mem;
574 	UVC_COPY_DESCRIPTORS(mem, dst,
575 		(const struct usb_descriptor_header**)uvc_streaming_cls);
576 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
577 	uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
578 
579 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
580 
581 	*dst = NULL;
582 	return hdr;
583 }
584 
585 static int
586 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
587 {
588 	struct usb_composite_dev *cdev = c->cdev;
589 	struct uvc_device *uvc = to_uvc(f);
590 	struct usb_string *us;
591 	unsigned int max_packet_mult;
592 	unsigned int max_packet_size;
593 	struct usb_ep *ep;
594 	struct f_uvc_opts *opts;
595 	int ret = -EINVAL;
596 
597 	uvcg_info(f, "%s()\n", __func__);
598 
599 	opts = fi_to_f_uvc_opts(f->fi);
600 	/* Sanity check the streaming endpoint module parameters.
601 	 */
602 	opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
603 	opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
604 	opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
605 
606 	/* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
607 	if (opts->streaming_maxburst &&
608 	    (opts->streaming_maxpacket % 1024) != 0) {
609 		opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
610 		uvcg_info(f, "overriding streaming_maxpacket to %d\n",
611 			  opts->streaming_maxpacket);
612 	}
613 
614 	/* Fill in the FS/HS/SS Video Streaming specific descriptors from the
615 	 * module parameters.
616 	 *
617 	 * NOTE: We assume that the user knows what they are doing and won't
618 	 * give parameters that their UDC doesn't support.
619 	 */
620 	if (opts->streaming_maxpacket <= 1024) {
621 		max_packet_mult = 1;
622 		max_packet_size = opts->streaming_maxpacket;
623 	} else if (opts->streaming_maxpacket <= 2048) {
624 		max_packet_mult = 2;
625 		max_packet_size = opts->streaming_maxpacket / 2;
626 	} else {
627 		max_packet_mult = 3;
628 		max_packet_size = opts->streaming_maxpacket / 3;
629 	}
630 
631 	uvc_fs_streaming_ep.wMaxPacketSize =
632 		cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
633 	uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
634 
635 	uvc_hs_streaming_ep.wMaxPacketSize =
636 		cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
637 
638 	/* A high-bandwidth endpoint must specify a bInterval value of 1 */
639 	if (max_packet_mult > 1)
640 		uvc_hs_streaming_ep.bInterval = 1;
641 	else
642 		uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
643 
644 	uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
645 	uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
646 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
647 	uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
648 	uvc_ss_streaming_comp.wBytesPerInterval =
649 		cpu_to_le16(max_packet_size * max_packet_mult *
650 			    (opts->streaming_maxburst + 1));
651 
652 	/* Allocate endpoints. */
653 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
654 	if (!ep) {
655 		uvcg_info(f, "Unable to allocate control EP\n");
656 		goto error;
657 	}
658 	uvc->control_ep = ep;
659 
660 	if (gadget_is_superspeed(c->cdev->gadget))
661 		ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
662 					  &uvc_ss_streaming_comp);
663 	else if (gadget_is_dualspeed(cdev->gadget))
664 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
665 	else
666 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
667 
668 	if (!ep) {
669 		uvcg_info(f, "Unable to allocate streaming EP\n");
670 		goto error;
671 	}
672 	uvc->video.ep = ep;
673 
674 	uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
675 	uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
676 	uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
677 
678 	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name;
679 	us = usb_gstrings_attach(cdev, uvc_function_strings,
680 				 ARRAY_SIZE(uvc_en_us_strings));
681 	if (IS_ERR(us)) {
682 		ret = PTR_ERR(us);
683 		goto error;
684 	}
685 	uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
686 	uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
687 	ret = us[UVC_STRING_STREAMING_IDX].id;
688 	uvc_streaming_intf_alt0.iInterface = ret;
689 	uvc_streaming_intf_alt1.iInterface = ret;
690 
691 	/* Allocate interface IDs. */
692 	if ((ret = usb_interface_id(c, f)) < 0)
693 		goto error;
694 	uvc_iad.bFirstInterface = ret;
695 	uvc_control_intf.bInterfaceNumber = ret;
696 	uvc->control_intf = ret;
697 	opts->control_interface = ret;
698 
699 	if ((ret = usb_interface_id(c, f)) < 0)
700 		goto error;
701 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
702 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
703 	uvc->streaming_intf = ret;
704 	opts->streaming_interface = ret;
705 
706 	/* Copy descriptors */
707 	f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
708 	if (IS_ERR(f->fs_descriptors)) {
709 		ret = PTR_ERR(f->fs_descriptors);
710 		f->fs_descriptors = NULL;
711 		goto error;
712 	}
713 	if (gadget_is_dualspeed(cdev->gadget)) {
714 		f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
715 		if (IS_ERR(f->hs_descriptors)) {
716 			ret = PTR_ERR(f->hs_descriptors);
717 			f->hs_descriptors = NULL;
718 			goto error;
719 		}
720 	}
721 	if (gadget_is_superspeed(c->cdev->gadget)) {
722 		f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
723 		if (IS_ERR(f->ss_descriptors)) {
724 			ret = PTR_ERR(f->ss_descriptors);
725 			f->ss_descriptors = NULL;
726 			goto error;
727 		}
728 	}
729 
730 	/* Preallocate control endpoint request. */
731 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
732 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
733 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
734 		ret = -ENOMEM;
735 		goto error;
736 	}
737 
738 	uvc->control_req->buf = uvc->control_buf;
739 	uvc->control_req->complete = uvc_function_ep0_complete;
740 	uvc->control_req->context = uvc;
741 
742 	if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
743 		uvcg_err(f, "failed to register V4L2 device\n");
744 		goto error;
745 	}
746 
747 	/* Initialise video. */
748 	ret = uvcg_video_init(&uvc->video, uvc);
749 	if (ret < 0)
750 		goto v4l2_error;
751 
752 	/* Register a V4L2 device. */
753 	ret = uvc_register_video(uvc);
754 	if (ret < 0) {
755 		uvcg_err(f, "failed to register video device\n");
756 		goto v4l2_error;
757 	}
758 
759 	return 0;
760 
761 v4l2_error:
762 	v4l2_device_unregister(&uvc->v4l2_dev);
763 error:
764 	if (uvc->control_req)
765 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
766 	kfree(uvc->control_buf);
767 
768 	usb_free_all_descriptors(f);
769 	return ret;
770 }
771 
772 /* --------------------------------------------------------------------------
773  * USB gadget function
774  */
775 
776 static void uvc_free_inst(struct usb_function_instance *f)
777 {
778 	struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
779 
780 	mutex_destroy(&opts->lock);
781 	kfree(opts);
782 }
783 
784 static struct usb_function_instance *uvc_alloc_inst(void)
785 {
786 	struct f_uvc_opts *opts;
787 	struct uvc_camera_terminal_descriptor *cd;
788 	struct uvc_processing_unit_descriptor *pd;
789 	struct uvc_output_terminal_descriptor *od;
790 	struct uvc_color_matching_descriptor *md;
791 	struct uvc_descriptor_header **ctl_cls;
792 	int ret;
793 
794 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
795 	if (!opts)
796 		return ERR_PTR(-ENOMEM);
797 	opts->func_inst.free_func_inst = uvc_free_inst;
798 	mutex_init(&opts->lock);
799 
800 	cd = &opts->uvc_camera_terminal;
801 	cd->bLength			= UVC_DT_CAMERA_TERMINAL_SIZE(3);
802 	cd->bDescriptorType		= USB_DT_CS_INTERFACE;
803 	cd->bDescriptorSubType		= UVC_VC_INPUT_TERMINAL;
804 	cd->bTerminalID			= 1;
805 	cd->wTerminalType		= cpu_to_le16(0x0201);
806 	cd->bAssocTerminal		= 0;
807 	cd->iTerminal			= 0;
808 	cd->wObjectiveFocalLengthMin	= cpu_to_le16(0);
809 	cd->wObjectiveFocalLengthMax	= cpu_to_le16(0);
810 	cd->wOcularFocalLength		= cpu_to_le16(0);
811 	cd->bControlSize		= 3;
812 	cd->bmControls[0]		= 2;
813 	cd->bmControls[1]		= 0;
814 	cd->bmControls[2]		= 0;
815 
816 	pd = &opts->uvc_processing;
817 	pd->bLength			= UVC_DT_PROCESSING_UNIT_SIZE(2);
818 	pd->bDescriptorType		= USB_DT_CS_INTERFACE;
819 	pd->bDescriptorSubType		= UVC_VC_PROCESSING_UNIT;
820 	pd->bUnitID			= 2;
821 	pd->bSourceID			= 1;
822 	pd->wMaxMultiplier		= cpu_to_le16(16*1024);
823 	pd->bControlSize		= 2;
824 	pd->bmControls[0]		= 1;
825 	pd->bmControls[1]		= 0;
826 	pd->iProcessing			= 0;
827 	pd->bmVideoStandards		= 0;
828 
829 	od = &opts->uvc_output_terminal;
830 	od->bLength			= UVC_DT_OUTPUT_TERMINAL_SIZE;
831 	od->bDescriptorType		= USB_DT_CS_INTERFACE;
832 	od->bDescriptorSubType		= UVC_VC_OUTPUT_TERMINAL;
833 	od->bTerminalID			= 3;
834 	od->wTerminalType		= cpu_to_le16(0x0101);
835 	od->bAssocTerminal		= 0;
836 	od->bSourceID			= 2;
837 	od->iTerminal			= 0;
838 
839 	md = &opts->uvc_color_matching;
840 	md->bLength			= UVC_DT_COLOR_MATCHING_SIZE;
841 	md->bDescriptorType		= USB_DT_CS_INTERFACE;
842 	md->bDescriptorSubType		= UVC_VS_COLORFORMAT;
843 	md->bColorPrimaries		= 1;
844 	md->bTransferCharacteristics	= 1;
845 	md->bMatrixCoefficients		= 4;
846 
847 	/* Prepare fs control class descriptors for configfs-based gadgets */
848 	ctl_cls = opts->uvc_fs_control_cls;
849 	ctl_cls[0] = NULL;	/* assigned elsewhere by configfs */
850 	ctl_cls[1] = (struct uvc_descriptor_header *)cd;
851 	ctl_cls[2] = (struct uvc_descriptor_header *)pd;
852 	ctl_cls[3] = (struct uvc_descriptor_header *)od;
853 	ctl_cls[4] = NULL;	/* NULL-terminate */
854 	opts->fs_control =
855 		(const struct uvc_descriptor_header * const *)ctl_cls;
856 
857 	/* Prepare hs control class descriptors for configfs-based gadgets */
858 	ctl_cls = opts->uvc_ss_control_cls;
859 	ctl_cls[0] = NULL;	/* assigned elsewhere by configfs */
860 	ctl_cls[1] = (struct uvc_descriptor_header *)cd;
861 	ctl_cls[2] = (struct uvc_descriptor_header *)pd;
862 	ctl_cls[3] = (struct uvc_descriptor_header *)od;
863 	ctl_cls[4] = NULL;	/* NULL-terminate */
864 	opts->ss_control =
865 		(const struct uvc_descriptor_header * const *)ctl_cls;
866 
867 	opts->streaming_interval = 1;
868 	opts->streaming_maxpacket = 1024;
869 	snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera");
870 
871 	ret = uvcg_attach_configfs(opts);
872 	if (ret < 0) {
873 		kfree(opts);
874 		return ERR_PTR(ret);
875 	}
876 
877 	return &opts->func_inst;
878 }
879 
880 static void uvc_free(struct usb_function *f)
881 {
882 	struct uvc_device *uvc = to_uvc(f);
883 	struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
884 					       func_inst);
885 	--opts->refcnt;
886 	kfree(uvc);
887 }
888 
889 static void uvc_function_unbind(struct usb_configuration *c,
890 				struct usb_function *f)
891 {
892 	struct usb_composite_dev *cdev = c->cdev;
893 	struct uvc_device *uvc = to_uvc(f);
894 	long wait_ret = 1;
895 
896 	uvcg_info(f, "%s()\n", __func__);
897 
898 	/* If we know we're connected via v4l2, then there should be a cleanup
899 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
900 	 * though the video device removal uevent. Allow some time for the
901 	 * application to close out before things get deleted.
902 	 */
903 	if (uvc->func_connected) {
904 		uvcg_dbg(f, "waiting for clean disconnect\n");
905 		wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
906 				uvc->func_connected == false, msecs_to_jiffies(500));
907 		uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret);
908 	}
909 
910 	device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
911 	video_unregister_device(&uvc->vdev);
912 	v4l2_device_unregister(&uvc->v4l2_dev);
913 
914 	if (uvc->func_connected) {
915 		/* Wait for the release to occur to ensure there are no longer any
916 		 * pending operations that may cause panics when resources are cleaned
917 		 * up.
918 		 */
919 		uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__);
920 		wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
921 				uvc->func_connected == false, msecs_to_jiffies(1000));
922 		uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret);
923 	}
924 
925 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
926 	kfree(uvc->control_buf);
927 
928 	usb_free_all_descriptors(f);
929 }
930 
931 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
932 {
933 	struct uvc_device *uvc;
934 	struct f_uvc_opts *opts;
935 	struct uvc_descriptor_header **strm_cls;
936 
937 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
938 	if (uvc == NULL)
939 		return ERR_PTR(-ENOMEM);
940 
941 	mutex_init(&uvc->video.mutex);
942 	uvc->state = UVC_STATE_DISCONNECTED;
943 	init_waitqueue_head(&uvc->func_connected_queue);
944 	opts = fi_to_f_uvc_opts(fi);
945 
946 	mutex_lock(&opts->lock);
947 	if (opts->uvc_fs_streaming_cls) {
948 		strm_cls = opts->uvc_fs_streaming_cls;
949 		opts->fs_streaming =
950 			(const struct uvc_descriptor_header * const *)strm_cls;
951 	}
952 	if (opts->uvc_hs_streaming_cls) {
953 		strm_cls = opts->uvc_hs_streaming_cls;
954 		opts->hs_streaming =
955 			(const struct uvc_descriptor_header * const *)strm_cls;
956 	}
957 	if (opts->uvc_ss_streaming_cls) {
958 		strm_cls = opts->uvc_ss_streaming_cls;
959 		opts->ss_streaming =
960 			(const struct uvc_descriptor_header * const *)strm_cls;
961 	}
962 
963 	uvc->desc.fs_control = opts->fs_control;
964 	uvc->desc.ss_control = opts->ss_control;
965 	uvc->desc.fs_streaming = opts->fs_streaming;
966 	uvc->desc.hs_streaming = opts->hs_streaming;
967 	uvc->desc.ss_streaming = opts->ss_streaming;
968 	++opts->refcnt;
969 	mutex_unlock(&opts->lock);
970 
971 	/* Register the function. */
972 	uvc->func.name = "uvc";
973 	uvc->func.bind = uvc_function_bind;
974 	uvc->func.unbind = uvc_function_unbind;
975 	uvc->func.get_alt = uvc_function_get_alt;
976 	uvc->func.set_alt = uvc_function_set_alt;
977 	uvc->func.disable = uvc_function_disable;
978 	uvc->func.setup = uvc_function_setup;
979 	uvc->func.free_func = uvc_free;
980 	uvc->func.bind_deactivated = true;
981 
982 	return &uvc->func;
983 }
984 
985 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
986 MODULE_LICENSE("GPL");
987 MODULE_AUTHOR("Laurent Pinchart");
988