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