1 /*
2  *	uvc_gadget.c  --  USB Video Class Gadget driver
3  *
4  *	Copyright (C) 2009-2010
5  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License as published by
9  *	the Free Software Foundation; either version 2 of the License, or
10  *	(at your option) any later version.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/fs.h>
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/string.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/video.h>
24 #include <linux/vmalloc.h>
25 #include <linux/wait.h>
26 
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-event.h>
29 
30 #include "uvc.h"
31 #include "uvc_v4l2.h"
32 #include "uvc_video.h"
33 #include "u_uvc.h"
34 
35 unsigned int uvc_gadget_trace_param;
36 
37 /* --------------------------------------------------------------------------
38  * Function descriptors
39  */
40 
41 /* string IDs are assigned dynamically */
42 
43 #define UVC_STRING_CONTROL_IDX			0
44 #define UVC_STRING_STREAMING_IDX		1
45 
46 static struct usb_string uvc_en_us_strings[] = {
47 	[UVC_STRING_CONTROL_IDX].s = "UVC Camera",
48 	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
49 	{  }
50 };
51 
52 static struct usb_gadget_strings uvc_stringtab = {
53 	.language = 0x0409,	/* en-us */
54 	.strings = uvc_en_us_strings,
55 };
56 
57 static struct usb_gadget_strings *uvc_function_strings[] = {
58 	&uvc_stringtab,
59 	NULL,
60 };
61 
62 #define UVC_INTF_VIDEO_CONTROL			0
63 #define UVC_INTF_VIDEO_STREAMING		1
64 
65 #define UVC_STATUS_MAX_PACKET_SIZE		16	/* 16 bytes status */
66 
67 static struct usb_interface_assoc_descriptor uvc_iad = {
68 	.bLength		= sizeof(uvc_iad),
69 	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
70 	.bFirstInterface	= 0,
71 	.bInterfaceCount	= 2,
72 	.bFunctionClass		= USB_CLASS_VIDEO,
73 	.bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION,
74 	.bFunctionProtocol	= 0x00,
75 	.iFunction		= 0,
76 };
77 
78 static struct usb_interface_descriptor uvc_control_intf = {
79 	.bLength		= USB_DT_INTERFACE_SIZE,
80 	.bDescriptorType	= USB_DT_INTERFACE,
81 	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
82 	.bAlternateSetting	= 0,
83 	.bNumEndpoints		= 1,
84 	.bInterfaceClass	= USB_CLASS_VIDEO,
85 	.bInterfaceSubClass	= UVC_SC_VIDEOCONTROL,
86 	.bInterfaceProtocol	= 0x00,
87 	.iInterface		= 0,
88 };
89 
90 static struct usb_endpoint_descriptor uvc_control_ep = {
91 	.bLength		= USB_DT_ENDPOINT_SIZE,
92 	.bDescriptorType	= USB_DT_ENDPOINT,
93 	.bEndpointAddress	= USB_DIR_IN,
94 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
95 	.wMaxPacketSize		= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
96 	.bInterval		= 8,
97 };
98 
99 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
100 	.bLength		= sizeof(uvc_ss_control_comp),
101 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
102 	/* The following 3 values can be tweaked if necessary. */
103 	.bMaxBurst		= 0,
104 	.bmAttributes		= 0,
105 	.wBytesPerInterval	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
106 };
107 
108 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
109 	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
110 	.bDescriptorType	= USB_DT_CS_ENDPOINT,
111 	.bDescriptorSubType	= UVC_EP_INTERRUPT,
112 	.wMaxTransferSize	= cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
113 };
114 
115 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
116 	.bLength		= USB_DT_INTERFACE_SIZE,
117 	.bDescriptorType	= USB_DT_INTERFACE,
118 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
119 	.bAlternateSetting	= 0,
120 	.bNumEndpoints		= 0,
121 	.bInterfaceClass	= USB_CLASS_VIDEO,
122 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
123 	.bInterfaceProtocol	= 0x00,
124 	.iInterface		= 0,
125 };
126 
127 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
128 	.bLength		= USB_DT_INTERFACE_SIZE,
129 	.bDescriptorType	= USB_DT_INTERFACE,
130 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
131 	.bAlternateSetting	= 1,
132 	.bNumEndpoints		= 1,
133 	.bInterfaceClass	= USB_CLASS_VIDEO,
134 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
135 	.bInterfaceProtocol	= 0x00,
136 	.iInterface		= 0,
137 };
138 
139 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
140 	.bLength		= USB_DT_ENDPOINT_SIZE,
141 	.bDescriptorType	= USB_DT_ENDPOINT,
142 	.bEndpointAddress	= USB_DIR_IN,
143 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
144 				| USB_ENDPOINT_XFER_ISOC,
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 	/* The wMaxPacketSize and bInterval values will be initialized from
157 	 * module parameters.
158 	 */
159 };
160 
161 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
162 	.bLength		= USB_DT_ENDPOINT_SIZE,
163 	.bDescriptorType	= USB_DT_ENDPOINT,
164 
165 	.bEndpointAddress	= USB_DIR_IN,
166 	.bmAttributes		= USB_ENDPOINT_SYNC_ASYNC
167 				| USB_ENDPOINT_XFER_ISOC,
168 	/* The wMaxPacketSize and bInterval values will be initialized from
169 	 * module parameters.
170 	 */
171 };
172 
173 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
174 	.bLength		= sizeof(uvc_ss_streaming_comp),
175 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
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 void uvc_set_trace_param(unsigned int trace)
201 {
202 	uvc_gadget_trace_param = trace;
203 }
204 EXPORT_SYMBOL(uvc_set_trace_param);
205 
206 /* --------------------------------------------------------------------------
207  * Control requests
208  */
209 
210 static void
211 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
212 {
213 	struct uvc_device *uvc = req->context;
214 	struct v4l2_event v4l2_event;
215 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
216 
217 	if (uvc->event_setup_out) {
218 		uvc->event_setup_out = 0;
219 
220 		memset(&v4l2_event, 0, sizeof(v4l2_event));
221 		v4l2_event.type = UVC_EVENT_DATA;
222 		uvc_event->data.length = req->actual;
223 		memcpy(&uvc_event->data.data, req->buf, req->actual);
224 		v4l2_event_queue(uvc->vdev, &v4l2_event);
225 	}
226 }
227 
228 static int
229 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
230 {
231 	struct uvc_device *uvc = to_uvc(f);
232 	struct v4l2_event v4l2_event;
233 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
234 
235 	/* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
236 	 *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
237 	 *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
238 	 */
239 
240 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
241 		INFO(f->config->cdev, "invalid request type\n");
242 		return -EINVAL;
243 	}
244 
245 	/* Stall too big requests. */
246 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
247 		return -EINVAL;
248 
249 	/* Tell the complete callback to generate an event for the next request
250 	 * that will be enqueued by UVCIOC_SEND_RESPONSE.
251 	 */
252 	uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
253 	uvc->event_length = le16_to_cpu(ctrl->wLength);
254 
255 	memset(&v4l2_event, 0, sizeof(v4l2_event));
256 	v4l2_event.type = UVC_EVENT_SETUP;
257 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
258 	v4l2_event_queue(uvc->vdev, &v4l2_event);
259 
260 	return 0;
261 }
262 
263 void uvc_function_setup_continue(struct uvc_device *uvc)
264 {
265 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
266 
267 	usb_composite_setup_continue(cdev);
268 }
269 
270 static int
271 uvc_function_get_alt(struct usb_function *f, unsigned interface)
272 {
273 	struct uvc_device *uvc = to_uvc(f);
274 
275 	INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
276 
277 	if (interface == uvc->control_intf)
278 		return 0;
279 	else if (interface != uvc->streaming_intf)
280 		return -EINVAL;
281 	else
282 		return uvc->video.ep->driver_data ? 1 : 0;
283 }
284 
285 static int
286 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
287 {
288 	struct uvc_device *uvc = to_uvc(f);
289 	struct usb_composite_dev *cdev = f->config->cdev;
290 	struct v4l2_event v4l2_event;
291 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
292 	int ret;
293 
294 	INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
295 
296 	if (interface == uvc->control_intf) {
297 		if (alt)
298 			return -EINVAL;
299 
300 		if (uvc->control_ep->driver_data) {
301 			INFO(cdev, "reset UVC Control\n");
302 			usb_ep_disable(uvc->control_ep);
303 			uvc->control_ep->driver_data = NULL;
304 		}
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 		uvc->control_ep->driver_data = uvc;
312 
313 		if (uvc->state == UVC_STATE_DISCONNECTED) {
314 			memset(&v4l2_event, 0, sizeof(v4l2_event));
315 			v4l2_event.type = UVC_EVENT_CONNECT;
316 			uvc_event->speed = cdev->gadget->speed;
317 			v4l2_event_queue(uvc->vdev, &v4l2_event);
318 
319 			uvc->state = UVC_STATE_CONNECTED;
320 		}
321 
322 		return 0;
323 	}
324 
325 	if (interface != uvc->streaming_intf)
326 		return -EINVAL;
327 
328 	/* TODO
329 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
330 		return alt ? -EINVAL : 0;
331 	*/
332 
333 	switch (alt) {
334 	case 0:
335 		if (uvc->state != UVC_STATE_STREAMING)
336 			return 0;
337 
338 		if (uvc->video.ep) {
339 			usb_ep_disable(uvc->video.ep);
340 			uvc->video.ep->driver_data = NULL;
341 		}
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 		if (uvc->video.ep->driver_data) {
358 			INFO(cdev, "reset UVC\n");
359 			usb_ep_disable(uvc->video.ep);
360 			uvc->video.ep->driver_data = NULL;
361 		}
362 
363 		ret = config_ep_by_speed(f->config->cdev->gadget,
364 				&(uvc->func), uvc->video.ep);
365 		if (ret)
366 			return ret;
367 		usb_ep_enable(uvc->video.ep);
368 		uvc->video.ep->driver_data = uvc;
369 
370 		memset(&v4l2_event, 0, sizeof(v4l2_event));
371 		v4l2_event.type = UVC_EVENT_STREAMON;
372 		v4l2_event_queue(uvc->vdev, &v4l2_event);
373 		return USB_GADGET_DELAYED_STATUS;
374 
375 	default:
376 		return -EINVAL;
377 	}
378 }
379 
380 static void
381 uvc_function_disable(struct usb_function *f)
382 {
383 	struct uvc_device *uvc = to_uvc(f);
384 	struct v4l2_event v4l2_event;
385 
386 	INFO(f->config->cdev, "uvc_function_disable\n");
387 
388 	memset(&v4l2_event, 0, sizeof(v4l2_event));
389 	v4l2_event.type = UVC_EVENT_DISCONNECT;
390 	v4l2_event_queue(uvc->vdev, &v4l2_event);
391 
392 	uvc->state = UVC_STATE_DISCONNECTED;
393 
394 	if (uvc->video.ep->driver_data) {
395 		usb_ep_disable(uvc->video.ep);
396 		uvc->video.ep->driver_data = NULL;
397 	}
398 
399 	if (uvc->control_ep->driver_data) {
400 		usb_ep_disable(uvc->control_ep);
401 		uvc->control_ep->driver_data = NULL;
402 	}
403 }
404 
405 /* --------------------------------------------------------------------------
406  * Connection / disconnection
407  */
408 
409 void
410 uvc_function_connect(struct uvc_device *uvc)
411 {
412 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
413 	int ret;
414 
415 	if ((ret = usb_function_activate(&uvc->func)) < 0)
416 		INFO(cdev, "UVC connect failed with %d\n", ret);
417 }
418 
419 void
420 uvc_function_disconnect(struct uvc_device *uvc)
421 {
422 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
423 	int ret;
424 
425 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
426 		INFO(cdev, "UVC disconnect failed with %d\n", ret);
427 }
428 
429 /* --------------------------------------------------------------------------
430  * USB probe and disconnect
431  */
432 
433 static int
434 uvc_register_video(struct uvc_device *uvc)
435 {
436 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
437 	struct video_device *video;
438 
439 	/* TODO reference counting. */
440 	video = video_device_alloc();
441 	if (video == NULL)
442 		return -ENOMEM;
443 
444 	video->v4l2_dev = &uvc->v4l2_dev;
445 	video->fops = &uvc_v4l2_fops;
446 	video->ioctl_ops = &uvc_v4l2_ioctl_ops;
447 	video->release = video_device_release;
448 	video->vfl_dir = VFL_DIR_TX;
449 	strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
450 
451 	uvc->vdev = video;
452 	video_set_drvdata(video, uvc);
453 
454 	return video_register_device(video, VFL_TYPE_GRABBER, -1);
455 }
456 
457 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
458 	do { \
459 		memcpy(mem, desc, (desc)->bLength); \
460 		*(dst)++ = mem; \
461 		mem += (desc)->bLength; \
462 	} while (0);
463 
464 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
465 	do { \
466 		const struct usb_descriptor_header * const *__src; \
467 		for (__src = src; *__src; ++__src) { \
468 			memcpy(mem, *__src, (*__src)->bLength); \
469 			*dst++ = mem; \
470 			mem += (*__src)->bLength; \
471 		} \
472 	} while (0)
473 
474 static struct usb_descriptor_header **
475 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
476 {
477 	struct uvc_input_header_descriptor *uvc_streaming_header;
478 	struct uvc_header_descriptor *uvc_control_header;
479 	const struct uvc_descriptor_header * const *uvc_control_desc;
480 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
481 	const struct usb_descriptor_header * const *uvc_streaming_std;
482 	const struct usb_descriptor_header * const *src;
483 	struct usb_descriptor_header **dst;
484 	struct usb_descriptor_header **hdr;
485 	unsigned int control_size;
486 	unsigned int streaming_size;
487 	unsigned int n_desc;
488 	unsigned int bytes;
489 	void *mem;
490 
491 	switch (speed) {
492 	case USB_SPEED_SUPER:
493 		uvc_control_desc = uvc->desc.ss_control;
494 		uvc_streaming_cls = uvc->desc.ss_streaming;
495 		uvc_streaming_std = uvc_ss_streaming;
496 		break;
497 
498 	case USB_SPEED_HIGH:
499 		uvc_control_desc = uvc->desc.fs_control;
500 		uvc_streaming_cls = uvc->desc.hs_streaming;
501 		uvc_streaming_std = uvc_hs_streaming;
502 		break;
503 
504 	case USB_SPEED_FULL:
505 	default:
506 		uvc_control_desc = uvc->desc.fs_control;
507 		uvc_streaming_cls = uvc->desc.fs_streaming;
508 		uvc_streaming_std = uvc_fs_streaming;
509 		break;
510 	}
511 
512 	/* Descriptors layout
513 	 *
514 	 * uvc_iad
515 	 * uvc_control_intf
516 	 * Class-specific UVC control descriptors
517 	 * uvc_control_ep
518 	 * uvc_control_cs_ep
519 	 * uvc_ss_control_comp (for SS only)
520 	 * uvc_streaming_intf_alt0
521 	 * Class-specific UVC streaming descriptors
522 	 * uvc_{fs|hs}_streaming
523 	 */
524 
525 	/* Count descriptors and compute their size. */
526 	control_size = 0;
527 	streaming_size = 0;
528 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
529 	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
530 	      + uvc_streaming_intf_alt0.bLength;
531 
532 	if (speed == USB_SPEED_SUPER) {
533 		bytes += uvc_ss_control_comp.bLength;
534 		n_desc = 6;
535 	} else {
536 		n_desc = 5;
537 	}
538 
539 	for (src = (const struct usb_descriptor_header **)uvc_control_desc;
540 	     *src; ++src) {
541 		control_size += (*src)->bLength;
542 		bytes += (*src)->bLength;
543 		n_desc++;
544 	}
545 	for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
546 	     *src; ++src) {
547 		streaming_size += (*src)->bLength;
548 		bytes += (*src)->bLength;
549 		n_desc++;
550 	}
551 	for (src = uvc_streaming_std; *src; ++src) {
552 		bytes += (*src)->bLength;
553 		n_desc++;
554 	}
555 
556 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
557 	if (mem == NULL)
558 		return NULL;
559 
560 	hdr = mem;
561 	dst = mem;
562 	mem += (n_desc + 1) * sizeof(*src);
563 
564 	/* Copy the descriptors. */
565 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
566 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
567 
568 	uvc_control_header = mem;
569 	UVC_COPY_DESCRIPTORS(mem, dst,
570 		(const struct usb_descriptor_header **)uvc_control_desc);
571 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
572 	uvc_control_header->bInCollection = 1;
573 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
574 
575 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
576 	if (speed == USB_SPEED_SUPER)
577 		UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
578 
579 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
580 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
581 
582 	uvc_streaming_header = mem;
583 	UVC_COPY_DESCRIPTORS(mem, dst,
584 		(const struct usb_descriptor_header**)uvc_streaming_cls);
585 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
586 	uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
587 
588 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
589 
590 	*dst = NULL;
591 	return hdr;
592 }
593 
594 static int
595 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
596 {
597 	struct usb_composite_dev *cdev = c->cdev;
598 	struct uvc_device *uvc = to_uvc(f);
599 	struct usb_string *us;
600 	unsigned int max_packet_mult;
601 	unsigned int max_packet_size;
602 	struct usb_ep *ep;
603 	struct f_uvc_opts *opts;
604 	int ret = -EINVAL;
605 
606 	INFO(cdev, "uvc_function_bind\n");
607 
608 	opts = to_f_uvc_opts(f->fi);
609 	/* Sanity check the streaming endpoint module parameters.
610 	 */
611 	opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
612 	opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
613 	opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
614 
615 	/* Fill in the FS/HS/SS Video Streaming specific descriptors from the
616 	 * module parameters.
617 	 *
618 	 * NOTE: We assume that the user knows what they are doing and won't
619 	 * give parameters that their UDC doesn't support.
620 	 */
621 	if (opts->streaming_maxpacket <= 1024) {
622 		max_packet_mult = 1;
623 		max_packet_size = opts->streaming_maxpacket;
624 	} else if (opts->streaming_maxpacket <= 2048) {
625 		max_packet_mult = 2;
626 		max_packet_size = opts->streaming_maxpacket / 2;
627 	} else {
628 		max_packet_mult = 3;
629 		max_packet_size = opts->streaming_maxpacket / 3;
630 	}
631 
632 	uvc_fs_streaming_ep.wMaxPacketSize =
633 		cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
634 	uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
635 
636 	uvc_hs_streaming_ep.wMaxPacketSize =
637 		cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
638 	uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
639 
640 	uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
641 	uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
642 	uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
643 	uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
644 	uvc_ss_streaming_comp.wBytesPerInterval =
645 		cpu_to_le16(max_packet_size * max_packet_mult *
646 			    opts->streaming_maxburst);
647 
648 	/* Allocate endpoints. */
649 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
650 	if (!ep) {
651 		INFO(cdev, "Unable to allocate control EP\n");
652 		goto error;
653 	}
654 	uvc->control_ep = ep;
655 	ep->driver_data = uvc;
656 
657 	if (gadget_is_superspeed(c->cdev->gadget))
658 		ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
659 					  &uvc_ss_streaming_comp);
660 	else if (gadget_is_dualspeed(cdev->gadget))
661 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
662 	else
663 		ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
664 
665 	if (!ep) {
666 		INFO(cdev, "Unable to allocate streaming EP\n");
667 		goto error;
668 	}
669 	uvc->video.ep = ep;
670 	ep->driver_data = uvc;
671 
672 	uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
673 	uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
674 	uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
675 
676 	us = usb_gstrings_attach(cdev, uvc_function_strings,
677 				 ARRAY_SIZE(uvc_en_us_strings));
678 	if (IS_ERR(us)) {
679 		ret = PTR_ERR(us);
680 		goto error;
681 	}
682 	uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
683 	uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
684 	ret = us[UVC_STRING_STREAMING_IDX].id;
685 	uvc_streaming_intf_alt0.iInterface = ret;
686 	uvc_streaming_intf_alt1.iInterface = ret;
687 
688 	/* Allocate interface IDs. */
689 	if ((ret = usb_interface_id(c, f)) < 0)
690 		goto error;
691 	uvc_iad.bFirstInterface = ret;
692 	uvc_control_intf.bInterfaceNumber = ret;
693 	uvc->control_intf = ret;
694 
695 	if ((ret = usb_interface_id(c, f)) < 0)
696 		goto error;
697 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
698 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
699 	uvc->streaming_intf = ret;
700 
701 	/* Copy descriptors */
702 	f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
703 	if (gadget_is_dualspeed(cdev->gadget))
704 		f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
705 	if (gadget_is_superspeed(c->cdev->gadget))
706 		f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
707 
708 	/* Preallocate control endpoint request. */
709 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
710 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
711 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
712 		ret = -ENOMEM;
713 		goto error;
714 	}
715 
716 	uvc->control_req->buf = uvc->control_buf;
717 	uvc->control_req->complete = uvc_function_ep0_complete;
718 	uvc->control_req->context = uvc;
719 
720 	/* Avoid letting this gadget enumerate until the userspace server is
721 	 * active.
722 	 */
723 	if ((ret = usb_function_deactivate(f)) < 0)
724 		goto error;
725 
726 	if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
727 		printk(KERN_INFO "v4l2_device_register failed\n");
728 		goto error;
729 	}
730 
731 	/* Initialise video. */
732 	ret = uvcg_video_init(&uvc->video);
733 	if (ret < 0)
734 		goto error;
735 
736 	/* Register a V4L2 device. */
737 	ret = uvc_register_video(uvc);
738 	if (ret < 0) {
739 		printk(KERN_INFO "Unable to register video device\n");
740 		goto error;
741 	}
742 
743 	return 0;
744 
745 error:
746 	v4l2_device_unregister(&uvc->v4l2_dev);
747 	if (uvc->vdev)
748 		video_device_release(uvc->vdev);
749 
750 	if (uvc->control_ep)
751 		uvc->control_ep->driver_data = NULL;
752 	if (uvc->video.ep)
753 		uvc->video.ep->driver_data = NULL;
754 
755 	if (uvc->control_req)
756 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
757 	kfree(uvc->control_buf);
758 
759 	usb_free_all_descriptors(f);
760 	return ret;
761 }
762 
763 /* --------------------------------------------------------------------------
764  * USB gadget function
765  */
766 
767 static void uvc_free_inst(struct usb_function_instance *f)
768 {
769 	struct f_uvc_opts *opts = to_f_uvc_opts(f);
770 
771 	kfree(opts);
772 }
773 
774 static struct usb_function_instance *uvc_alloc_inst(void)
775 {
776 	struct f_uvc_opts *opts;
777 
778 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
779 	if (!opts)
780 		return ERR_PTR(-ENOMEM);
781 	opts->func_inst.free_func_inst = uvc_free_inst;
782 
783 	return &opts->func_inst;
784 }
785 
786 static void uvc_free(struct usb_function *f)
787 {
788 	struct uvc_device *uvc = to_uvc(f);
789 
790 	kfree(uvc);
791 }
792 
793 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
794 {
795 	struct usb_composite_dev *cdev = c->cdev;
796 	struct uvc_device *uvc = to_uvc(f);
797 
798 	INFO(cdev, "%s\n", __func__);
799 
800 	video_unregister_device(uvc->vdev);
801 	v4l2_device_unregister(&uvc->v4l2_dev);
802 	uvc->control_ep->driver_data = NULL;
803 	uvc->video.ep->driver_data = NULL;
804 
805 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
806 	kfree(uvc->control_buf);
807 
808 	usb_free_all_descriptors(f);
809 }
810 
811 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
812 {
813 	struct uvc_device *uvc;
814 	struct f_uvc_opts *opts;
815 
816 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
817 	if (uvc == NULL)
818 		return ERR_PTR(-ENOMEM);
819 
820 	uvc->state = UVC_STATE_DISCONNECTED;
821 	opts = to_f_uvc_opts(fi);
822 
823 	uvc->desc.fs_control = opts->fs_control;
824 	uvc->desc.ss_control = opts->ss_control;
825 	uvc->desc.fs_streaming = opts->fs_streaming;
826 	uvc->desc.hs_streaming = opts->hs_streaming;
827 	uvc->desc.ss_streaming = opts->ss_streaming;
828 
829 	/* Register the function. */
830 	uvc->func.name = "uvc";
831 	uvc->func.bind = uvc_function_bind;
832 	uvc->func.unbind = uvc_unbind;
833 	uvc->func.get_alt = uvc_function_get_alt;
834 	uvc->func.set_alt = uvc_function_set_alt;
835 	uvc->func.disable = uvc_function_disable;
836 	uvc->func.setup = uvc_function_setup;
837 	uvc->func.free_func = uvc_free;
838 
839 	return &uvc->func;
840 }
841 
842 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
843 MODULE_LICENSE("GPL");
844 MODULE_AUTHOR("Laurent Pinchart");
845