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