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