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