• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/device.h>
15 #include <linux/errno.h>
16 #include <linux/fs.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/video.h>
22 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
24 
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-event.h>
27 
28 #include "uvc.h"
29 
30 unsigned int uvc_gadget_trace_param;
31 
32 /* --------------------------------------------------------------------------
33  * Function descriptors
34  */
35 
36 /* string IDs are assigned dynamically */
37 
38 #define UVC_STRING_ASSOCIATION_IDX		0
39 #define UVC_STRING_CONTROL_IDX			1
40 #define UVC_STRING_STREAMING_IDX		2
41 
42 static struct usb_string uvc_en_us_strings[] = {
43 	[UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
44 	[UVC_STRING_CONTROL_IDX].s = "Video Control",
45 	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
46 	{  }
47 };
48 
49 static struct usb_gadget_strings uvc_stringtab = {
50 	.language = 0x0409,	/* en-us */
51 	.strings = uvc_en_us_strings,
52 };
53 
54 static struct usb_gadget_strings *uvc_function_strings[] = {
55 	&uvc_stringtab,
56 	NULL,
57 };
58 
59 #define UVC_INTF_VIDEO_CONTROL			0
60 #define UVC_INTF_VIDEO_STREAMING		1
61 
62 static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
63 	.bLength		= sizeof(uvc_iad),
64 	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
65 	.bFirstInterface	= 0,
66 	.bInterfaceCount	= 2,
67 	.bFunctionClass		= USB_CLASS_VIDEO,
68 	.bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION,
69 	.bFunctionProtocol	= 0x00,
70 	.iFunction		= 0,
71 };
72 
73 static struct usb_interface_descriptor uvc_control_intf __initdata = {
74 	.bLength		= USB_DT_INTERFACE_SIZE,
75 	.bDescriptorType	= USB_DT_INTERFACE,
76 	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
77 	.bAlternateSetting	= 0,
78 	.bNumEndpoints		= 1,
79 	.bInterfaceClass	= USB_CLASS_VIDEO,
80 	.bInterfaceSubClass	= UVC_SC_VIDEOCONTROL,
81 	.bInterfaceProtocol	= 0x00,
82 	.iInterface		= 0,
83 };
84 
85 static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
86 	.bLength		= USB_DT_ENDPOINT_SIZE,
87 	.bDescriptorType	= USB_DT_ENDPOINT,
88 	.bEndpointAddress	= USB_DIR_IN,
89 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
90 	.wMaxPacketSize		= cpu_to_le16(16),
91 	.bInterval		= 8,
92 };
93 
94 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
95 	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
96 	.bDescriptorType	= USB_DT_CS_ENDPOINT,
97 	.bDescriptorSubType	= UVC_EP_INTERRUPT,
98 	.wMaxTransferSize	= cpu_to_le16(16),
99 };
100 
101 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
102 	.bLength		= USB_DT_INTERFACE_SIZE,
103 	.bDescriptorType	= USB_DT_INTERFACE,
104 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
105 	.bAlternateSetting	= 0,
106 	.bNumEndpoints		= 0,
107 	.bInterfaceClass	= USB_CLASS_VIDEO,
108 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
109 	.bInterfaceProtocol	= 0x00,
110 	.iInterface		= 0,
111 };
112 
113 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
114 	.bLength		= USB_DT_INTERFACE_SIZE,
115 	.bDescriptorType	= USB_DT_INTERFACE,
116 	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
117 	.bAlternateSetting	= 1,
118 	.bNumEndpoints		= 1,
119 	.bInterfaceClass	= USB_CLASS_VIDEO,
120 	.bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING,
121 	.bInterfaceProtocol	= 0x00,
122 	.iInterface		= 0,
123 };
124 
125 static struct usb_endpoint_descriptor uvc_streaming_ep = {
126 	.bLength		= USB_DT_ENDPOINT_SIZE,
127 	.bDescriptorType	= USB_DT_ENDPOINT,
128 	.bEndpointAddress	= USB_DIR_IN,
129 	.bmAttributes		= USB_ENDPOINT_XFER_ISOC,
130 	.wMaxPacketSize		= cpu_to_le16(512),
131 	.bInterval		= 1,
132 };
133 
134 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
135 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
136 	(struct usb_descriptor_header *) &uvc_streaming_ep,
137 	NULL,
138 };
139 
140 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
141 	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
142 	(struct usb_descriptor_header *) &uvc_streaming_ep,
143 	NULL,
144 };
145 
146 /* --------------------------------------------------------------------------
147  * Control requests
148  */
149 
150 static void
uvc_function_ep0_complete(struct usb_ep * ep,struct usb_request * req)151 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
152 {
153 	struct uvc_device *uvc = req->context;
154 	struct v4l2_event v4l2_event;
155 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
156 
157 	if (uvc->event_setup_out) {
158 		uvc->event_setup_out = 0;
159 
160 		memset(&v4l2_event, 0, sizeof(v4l2_event));
161 		v4l2_event.type = UVC_EVENT_DATA;
162 		uvc_event->data.length = req->actual;
163 		memcpy(&uvc_event->data.data, req->buf, req->actual);
164 		v4l2_event_queue(uvc->vdev, &v4l2_event);
165 	}
166 }
167 
168 static int
uvc_function_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)169 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
170 {
171 	struct uvc_device *uvc = to_uvc(f);
172 	struct v4l2_event v4l2_event;
173 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
174 
175 	/* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
176 	 *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
177 	 *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
178 	 */
179 
180 	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
181 		INFO(f->config->cdev, "invalid request type\n");
182 		return -EINVAL;
183 	}
184 
185 	/* Stall too big requests. */
186 	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
187 		return -EINVAL;
188 
189 	memset(&v4l2_event, 0, sizeof(v4l2_event));
190 	v4l2_event.type = UVC_EVENT_SETUP;
191 	memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
192 	v4l2_event_queue(uvc->vdev, &v4l2_event);
193 
194 	return 0;
195 }
196 
197 static int
uvc_function_get_alt(struct usb_function * f,unsigned interface)198 uvc_function_get_alt(struct usb_function *f, unsigned interface)
199 {
200 	struct uvc_device *uvc = to_uvc(f);
201 
202 	INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
203 
204 	if (interface == uvc->control_intf)
205 		return 0;
206 	else if (interface != uvc->streaming_intf)
207 		return -EINVAL;
208 	else
209 		return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
210 }
211 
212 static int
uvc_function_set_alt(struct usb_function * f,unsigned interface,unsigned alt)213 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
214 {
215 	struct uvc_device *uvc = to_uvc(f);
216 	struct v4l2_event v4l2_event;
217 	struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
218 
219 	INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
220 
221 	if (interface == uvc->control_intf) {
222 		if (alt)
223 			return -EINVAL;
224 
225 		if (uvc->state == UVC_STATE_DISCONNECTED) {
226 			memset(&v4l2_event, 0, sizeof(v4l2_event));
227 			v4l2_event.type = UVC_EVENT_CONNECT;
228 			uvc_event->speed = f->config->cdev->gadget->speed;
229 			v4l2_event_queue(uvc->vdev, &v4l2_event);
230 
231 			uvc->state = UVC_STATE_CONNECTED;
232 		}
233 
234 		return 0;
235 	}
236 
237 	if (interface != uvc->streaming_intf)
238 		return -EINVAL;
239 
240 	/* TODO
241 	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
242 		return alt ? -EINVAL : 0;
243 	*/
244 
245 	switch (alt) {
246 	case 0:
247 		if (uvc->state != UVC_STATE_STREAMING)
248 			return 0;
249 
250 		if (uvc->video.ep)
251 			usb_ep_disable(uvc->video.ep);
252 
253 		memset(&v4l2_event, 0, sizeof(v4l2_event));
254 		v4l2_event.type = UVC_EVENT_STREAMOFF;
255 		v4l2_event_queue(uvc->vdev, &v4l2_event);
256 
257 		uvc->state = UVC_STATE_CONNECTED;
258 		break;
259 
260 	case 1:
261 		if (uvc->state != UVC_STATE_CONNECTED)
262 			return 0;
263 
264 		if (uvc->video.ep) {
265 			uvc->video.ep->desc = &uvc_streaming_ep;
266 			usb_ep_enable(uvc->video.ep);
267 		}
268 
269 		memset(&v4l2_event, 0, sizeof(v4l2_event));
270 		v4l2_event.type = UVC_EVENT_STREAMON;
271 		v4l2_event_queue(uvc->vdev, &v4l2_event);
272 
273 		uvc->state = UVC_STATE_STREAMING;
274 		break;
275 
276 	default:
277 		return -EINVAL;
278 	}
279 
280 	return 0;
281 }
282 
283 static void
uvc_function_disable(struct usb_function * f)284 uvc_function_disable(struct usb_function *f)
285 {
286 	struct uvc_device *uvc = to_uvc(f);
287 	struct v4l2_event v4l2_event;
288 
289 	INFO(f->config->cdev, "uvc_function_disable\n");
290 
291 	memset(&v4l2_event, 0, sizeof(v4l2_event));
292 	v4l2_event.type = UVC_EVENT_DISCONNECT;
293 	v4l2_event_queue(uvc->vdev, &v4l2_event);
294 
295 	uvc->state = UVC_STATE_DISCONNECTED;
296 }
297 
298 /* --------------------------------------------------------------------------
299  * Connection / disconnection
300  */
301 
302 void
uvc_function_connect(struct uvc_device * uvc)303 uvc_function_connect(struct uvc_device *uvc)
304 {
305 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
306 	int ret;
307 
308 	if ((ret = usb_function_activate(&uvc->func)) < 0)
309 		INFO(cdev, "UVC connect failed with %d\n", ret);
310 }
311 
312 void
uvc_function_disconnect(struct uvc_device * uvc)313 uvc_function_disconnect(struct uvc_device *uvc)
314 {
315 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
316 	int ret;
317 
318 	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
319 		INFO(cdev, "UVC disconnect failed with %d\n", ret);
320 }
321 
322 /* --------------------------------------------------------------------------
323  * USB probe and disconnect
324  */
325 
326 static int
uvc_register_video(struct uvc_device * uvc)327 uvc_register_video(struct uvc_device *uvc)
328 {
329 	struct usb_composite_dev *cdev = uvc->func.config->cdev;
330 	struct video_device *video;
331 
332 	/* TODO reference counting. */
333 	video = video_device_alloc();
334 	if (video == NULL)
335 		return -ENOMEM;
336 
337 	video->parent = &cdev->gadget->dev;
338 	video->fops = &uvc_v4l2_fops;
339 	video->release = video_device_release;
340 	strncpy(video->name, cdev->gadget->name, sizeof(video->name));
341 
342 	uvc->vdev = video;
343 	video_set_drvdata(video, uvc);
344 
345 	return video_register_device(video, VFL_TYPE_GRABBER, -1);
346 }
347 
348 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
349 	do { \
350 		memcpy(mem, desc, (desc)->bLength); \
351 		*(dst)++ = mem; \
352 		mem += (desc)->bLength; \
353 	} while (0);
354 
355 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
356 	do { \
357 		const struct usb_descriptor_header * const *__src; \
358 		for (__src = src; *__src; ++__src) { \
359 			memcpy(mem, *__src, (*__src)->bLength); \
360 			*dst++ = mem; \
361 			mem += (*__src)->bLength; \
362 		} \
363 	} while (0)
364 
365 static struct usb_descriptor_header ** __init
uvc_copy_descriptors(struct uvc_device * uvc,enum usb_device_speed speed)366 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
367 {
368 	struct uvc_input_header_descriptor *uvc_streaming_header;
369 	struct uvc_header_descriptor *uvc_control_header;
370 	const struct uvc_descriptor_header * const *uvc_streaming_cls;
371 	const struct usb_descriptor_header * const *uvc_streaming_std;
372 	const struct usb_descriptor_header * const *src;
373 	struct usb_descriptor_header **dst;
374 	struct usb_descriptor_header **hdr;
375 	unsigned int control_size;
376 	unsigned int streaming_size;
377 	unsigned int n_desc;
378 	unsigned int bytes;
379 	void *mem;
380 
381 	uvc_streaming_cls = (speed == USB_SPEED_FULL)
382 			  ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
383 	uvc_streaming_std = (speed == USB_SPEED_FULL)
384 			  ? uvc_fs_streaming : uvc_hs_streaming;
385 
386 	/* Descriptors layout
387 	 *
388 	 * uvc_iad
389 	 * uvc_control_intf
390 	 * Class-specific UVC control descriptors
391 	 * uvc_control_ep
392 	 * uvc_control_cs_ep
393 	 * uvc_streaming_intf_alt0
394 	 * Class-specific UVC streaming descriptors
395 	 * uvc_{fs|hs}_streaming
396 	 */
397 
398 	/* Count descriptors and compute their size. */
399 	control_size = 0;
400 	streaming_size = 0;
401 	bytes = uvc_iad.bLength + uvc_control_intf.bLength
402 	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
403 	      + uvc_streaming_intf_alt0.bLength;
404 	n_desc = 5;
405 
406 	for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
407 		control_size += (*src)->bLength;
408 		bytes += (*src)->bLength;
409 		n_desc++;
410 	}
411 	for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
412 		streaming_size += (*src)->bLength;
413 		bytes += (*src)->bLength;
414 		n_desc++;
415 	}
416 	for (src = uvc_streaming_std; *src; ++src) {
417 		bytes += (*src)->bLength;
418 		n_desc++;
419 	}
420 
421 	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
422 	if (mem == NULL)
423 		return NULL;
424 
425 	hdr = mem;
426 	dst = mem;
427 	mem += (n_desc + 1) * sizeof(*src);
428 
429 	/* Copy the descriptors. */
430 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
431 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
432 
433 	uvc_control_header = mem;
434 	UVC_COPY_DESCRIPTORS(mem, dst,
435 		(const struct usb_descriptor_header**)uvc->desc.control);
436 	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
437 	uvc_control_header->bInCollection = 1;
438 	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
439 
440 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
441 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
442 	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
443 
444 	uvc_streaming_header = mem;
445 	UVC_COPY_DESCRIPTORS(mem, dst,
446 		(const struct usb_descriptor_header**)uvc_streaming_cls);
447 	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
448 	uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
449 
450 	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
451 
452 	*dst = NULL;
453 	return hdr;
454 }
455 
456 static void
uvc_function_unbind(struct usb_configuration * c,struct usb_function * f)457 uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
458 {
459 	struct usb_composite_dev *cdev = c->cdev;
460 	struct uvc_device *uvc = to_uvc(f);
461 
462 	INFO(cdev, "uvc_function_unbind\n");
463 
464 	video_unregister_device(uvc->vdev);
465 	uvc->control_ep->driver_data = NULL;
466 	uvc->video.ep->driver_data = NULL;
467 
468 	usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
469 	kfree(uvc->control_buf);
470 
471 	kfree(f->descriptors);
472 	kfree(f->hs_descriptors);
473 
474 	kfree(uvc);
475 }
476 
477 static int __init
uvc_function_bind(struct usb_configuration * c,struct usb_function * f)478 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
479 {
480 	struct usb_composite_dev *cdev = c->cdev;
481 	struct uvc_device *uvc = to_uvc(f);
482 	struct usb_ep *ep;
483 	int ret = -EINVAL;
484 
485 	INFO(cdev, "uvc_function_bind\n");
486 
487 	/* Allocate endpoints. */
488 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
489 	if (!ep) {
490 		INFO(cdev, "Unable to allocate control EP\n");
491 		goto error;
492 	}
493 	uvc->control_ep = ep;
494 	ep->driver_data = uvc;
495 
496 	ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
497 	if (!ep) {
498 		INFO(cdev, "Unable to allocate streaming EP\n");
499 		goto error;
500 	}
501 	uvc->video.ep = ep;
502 	ep->driver_data = uvc;
503 
504 	/* Allocate interface IDs. */
505 	if ((ret = usb_interface_id(c, f)) < 0)
506 		goto error;
507 	uvc_iad.bFirstInterface = ret;
508 	uvc_control_intf.bInterfaceNumber = ret;
509 	uvc->control_intf = ret;
510 
511 	if ((ret = usb_interface_id(c, f)) < 0)
512 		goto error;
513 	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
514 	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
515 	uvc->streaming_intf = ret;
516 
517 	/* Copy descriptors. */
518 	f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
519 	f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
520 
521 	/* Preallocate control endpoint request. */
522 	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
523 	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
524 	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
525 		ret = -ENOMEM;
526 		goto error;
527 	}
528 
529 	uvc->control_req->buf = uvc->control_buf;
530 	uvc->control_req->complete = uvc_function_ep0_complete;
531 	uvc->control_req->context = uvc;
532 
533 	/* Avoid letting this gadget enumerate until the userspace server is
534 	 * active.
535 	 */
536 	if ((ret = usb_function_deactivate(f)) < 0)
537 		goto error;
538 
539 	/* Initialise video. */
540 	ret = uvc_video_init(&uvc->video);
541 	if (ret < 0)
542 		goto error;
543 
544 	/* Register a V4L2 device. */
545 	ret = uvc_register_video(uvc);
546 	if (ret < 0) {
547 		printk(KERN_INFO "Unable to register video device\n");
548 		goto error;
549 	}
550 
551 	return 0;
552 
553 error:
554 	if (uvc->vdev)
555 		video_device_release(uvc->vdev);
556 
557 	if (uvc->control_ep)
558 		uvc->control_ep->driver_data = NULL;
559 	if (uvc->video.ep)
560 		uvc->video.ep->driver_data = NULL;
561 
562 	if (uvc->control_req) {
563 		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
564 		kfree(uvc->control_buf);
565 	}
566 
567 	kfree(f->descriptors);
568 	kfree(f->hs_descriptors);
569 	kfree(f->ss_descriptors);
570 	return ret;
571 }
572 
573 /* --------------------------------------------------------------------------
574  * USB gadget function
575  */
576 
577 /**
578  * uvc_bind_config - add a UVC function to a configuration
579  * @c: the configuration to support the UVC instance
580  * Context: single threaded during gadget setup
581  *
582  * Returns zero on success, else negative errno.
583  *
584  * Caller must have called @uvc_setup(). Caller is also responsible for
585  * calling @uvc_cleanup() before module unload.
586  */
587 int __init
uvc_bind_config(struct usb_configuration * c,const struct uvc_descriptor_header * const * control,const struct uvc_descriptor_header * const * fs_streaming,const struct uvc_descriptor_header * const * hs_streaming)588 uvc_bind_config(struct usb_configuration *c,
589 		const struct uvc_descriptor_header * const *control,
590 		const struct uvc_descriptor_header * const *fs_streaming,
591 		const struct uvc_descriptor_header * const *hs_streaming)
592 {
593 	struct uvc_device *uvc;
594 	int ret = 0;
595 
596 	/* TODO Check if the USB device controller supports the required
597 	 * features.
598 	 */
599 	if (!gadget_is_dualspeed(c->cdev->gadget))
600 		return -EINVAL;
601 
602 	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
603 	if (uvc == NULL)
604 		return -ENOMEM;
605 
606 	uvc->state = UVC_STATE_DISCONNECTED;
607 
608 	/* Validate the descriptors. */
609 	if (control == NULL || control[0] == NULL ||
610 	    control[0]->bDescriptorSubType != UVC_VC_HEADER)
611 		goto error;
612 
613 	if (fs_streaming == NULL || fs_streaming[0] == NULL ||
614 	    fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
615 		goto error;
616 
617 	if (hs_streaming == NULL || hs_streaming[0] == NULL ||
618 	    hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
619 		goto error;
620 
621 	uvc->desc.control = control;
622 	uvc->desc.fs_streaming = fs_streaming;
623 	uvc->desc.hs_streaming = hs_streaming;
624 
625 	/* Allocate string descriptor numbers. */
626 	if ((ret = usb_string_id(c->cdev)) < 0)
627 		goto error;
628 	uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
629 	uvc_iad.iFunction = ret;
630 
631 	if ((ret = usb_string_id(c->cdev)) < 0)
632 		goto error;
633 	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
634 	uvc_control_intf.iInterface = ret;
635 
636 	if ((ret = usb_string_id(c->cdev)) < 0)
637 		goto error;
638 	uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
639 	uvc_streaming_intf_alt0.iInterface = ret;
640 	uvc_streaming_intf_alt1.iInterface = ret;
641 
642 	/* Register the function. */
643 	uvc->func.name = "uvc";
644 	uvc->func.strings = uvc_function_strings;
645 	uvc->func.bind = uvc_function_bind;
646 	uvc->func.unbind = uvc_function_unbind;
647 	uvc->func.get_alt = uvc_function_get_alt;
648 	uvc->func.set_alt = uvc_function_set_alt;
649 	uvc->func.disable = uvc_function_disable;
650 	uvc->func.setup = uvc_function_setup;
651 
652 	ret = usb_add_function(c, &uvc->func);
653 	if (ret)
654 		kfree(uvc);
655 
656 	return ret;
657 
658 error:
659 	kfree(uvc);
660 	return ret;
661 }
662 
663 module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
664 MODULE_PARM_DESC(trace, "Trace level bitmask");
665 
666