2 * uvc_gadget.c -- USB Video Class Gadget driver
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/errno.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>
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-event.h>
32 #include "uvc_video.h"
35 unsigned int uvc_gadget_trace_param;
37 /* --------------------------------------------------------------------------
38 * Function descriptors
41 /* string IDs are assigned dynamically */
43 #define UVC_STRING_CONTROL_IDX 0
44 #define UVC_STRING_STREAMING_IDX 1
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",
52 static struct usb_gadget_strings uvc_stringtab = {
53 .language = 0x0409, /* en-us */
54 .strings = uvc_en_us_strings,
57 static struct usb_gadget_strings *uvc_function_strings[] = {
62 #define UVC_INTF_VIDEO_CONTROL 0
63 #define UVC_INTF_VIDEO_STREAMING 1
65 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
67 static struct usb_interface_assoc_descriptor uvc_iad = {
68 .bLength = sizeof(uvc_iad),
69 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
72 .bFunctionClass = USB_CLASS_VIDEO,
73 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
74 .bFunctionProtocol = 0x00,
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,
84 .bInterfaceClass = USB_CLASS_VIDEO,
85 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
86 .bInterfaceProtocol = 0x00,
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),
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. */
105 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
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),
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,
121 .bInterfaceClass = USB_CLASS_VIDEO,
122 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
123 .bInterfaceProtocol = 0x00,
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,
133 .bInterfaceClass = USB_CLASS_VIDEO,
134 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
135 .bInterfaceProtocol = 0x00,
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
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
161 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
162 .bLength = USB_DT_ENDPOINT_SIZE,
163 .bDescriptorType = USB_DT_ENDPOINT,
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
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.
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,
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,
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,
200 void uvc_set_trace_param(unsigned int trace)
202 uvc_gadget_trace_param = trace;
204 EXPORT_SYMBOL(uvc_set_trace_param);
206 /* --------------------------------------------------------------------------
211 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
213 struct uvc_device *uvc = req->context;
214 struct v4l2_event v4l2_event;
215 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
217 if (uvc->event_setup_out) {
218 uvc->event_setup_out = 0;
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);
229 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
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;
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));
240 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
241 INFO(f->config->cdev, "invalid request type\n");
245 /* Stall too big requests. */
246 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
249 /* Tell the complete callback to generate an event for the next request
250 * that will be enqueued by UVCIOC_SEND_RESPONSE.
252 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
253 uvc->event_length = le16_to_cpu(ctrl->wLength);
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);
263 void uvc_function_setup_continue(struct uvc_device *uvc)
265 struct usb_composite_dev *cdev = uvc->func.config->cdev;
267 usb_composite_setup_continue(cdev);
271 uvc_function_get_alt(struct usb_function *f, unsigned interface)
273 struct uvc_device *uvc = to_uvc(f);
275 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
277 if (interface == uvc->control_intf)
279 else if (interface != uvc->streaming_intf)
282 return uvc->video.ep->driver_data ? 1 : 0;
286 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
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;
294 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
296 if (interface == uvc->control_intf) {
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;
306 if (!uvc->control_ep->desc)
307 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
310 usb_ep_enable(uvc->control_ep);
311 uvc->control_ep->driver_data = uvc;
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);
319 uvc->state = UVC_STATE_CONNECTED;
325 if (interface != uvc->streaming_intf)
329 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
330 return alt ? -EINVAL : 0;
335 if (uvc->state != UVC_STATE_STREAMING)
339 usb_ep_disable(uvc->video.ep);
340 uvc->video.ep->driver_data = NULL;
343 memset(&v4l2_event, 0, sizeof(v4l2_event));
344 v4l2_event.type = UVC_EVENT_STREAMOFF;
345 v4l2_event_queue(uvc->vdev, &v4l2_event);
347 uvc->state = UVC_STATE_CONNECTED;
351 if (uvc->state != UVC_STATE_CONNECTED)
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;
363 ret = config_ep_by_speed(f->config->cdev->gadget,
364 &(uvc->func), uvc->video.ep);
367 usb_ep_enable(uvc->video.ep);
368 uvc->video.ep->driver_data = uvc;
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;
381 uvc_function_disable(struct usb_function *f)
383 struct uvc_device *uvc = to_uvc(f);
384 struct v4l2_event v4l2_event;
386 INFO(f->config->cdev, "uvc_function_disable\n");
388 memset(&v4l2_event, 0, sizeof(v4l2_event));
389 v4l2_event.type = UVC_EVENT_DISCONNECT;
390 v4l2_event_queue(uvc->vdev, &v4l2_event);
392 uvc->state = UVC_STATE_DISCONNECTED;
394 if (uvc->video.ep->driver_data) {
395 usb_ep_disable(uvc->video.ep);
396 uvc->video.ep->driver_data = NULL;
399 if (uvc->control_ep->driver_data) {
400 usb_ep_disable(uvc->control_ep);
401 uvc->control_ep->driver_data = NULL;
405 /* --------------------------------------------------------------------------
406 * Connection / disconnection
410 uvc_function_connect(struct uvc_device *uvc)
412 struct usb_composite_dev *cdev = uvc->func.config->cdev;
415 if ((ret = usb_function_activate(&uvc->func)) < 0)
416 INFO(cdev, "UVC connect failed with %d\n", ret);
420 uvc_function_disconnect(struct uvc_device *uvc)
422 struct usb_composite_dev *cdev = uvc->func.config->cdev;
425 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
426 INFO(cdev, "UVC disconnect failed with %d\n", ret);
429 /* --------------------------------------------------------------------------
430 * USB probe and disconnect
434 uvc_register_video(struct uvc_device *uvc)
436 struct usb_composite_dev *cdev = uvc->func.config->cdev;
437 struct video_device *video;
439 /* TODO reference counting. */
440 video = video_device_alloc();
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));
452 video_set_drvdata(video, uvc);
454 return video_register_device(video, VFL_TYPE_GRABBER, -1);
457 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
459 memcpy(mem, desc, (desc)->bLength); \
461 mem += (desc)->bLength; \
464 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
466 const struct usb_descriptor_header * const *__src; \
467 for (__src = src; *__src; ++__src) { \
468 memcpy(mem, *__src, (*__src)->bLength); \
470 mem += (*__src)->bLength; \
474 static struct usb_descriptor_header **
475 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
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;
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;
499 uvc_control_desc = uvc->desc.fs_control;
500 uvc_streaming_cls = uvc->desc.hs_streaming;
501 uvc_streaming_std = uvc_hs_streaming;
506 uvc_control_desc = uvc->desc.fs_control;
507 uvc_streaming_cls = uvc->desc.fs_streaming;
508 uvc_streaming_std = uvc_fs_streaming;
512 /* Descriptors layout
516 * Class-specific UVC control descriptors
519 * uvc_ss_control_comp (for SS only)
520 * uvc_streaming_intf_alt0
521 * Class-specific UVC streaming descriptors
522 * uvc_{fs|hs}_streaming
525 /* Count descriptors and compute their size. */
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;
532 if (speed == USB_SPEED_SUPER) {
533 bytes += uvc_ss_control_comp.bLength;
539 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
541 control_size += (*src)->bLength;
542 bytes += (*src)->bLength;
545 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
547 streaming_size += (*src)->bLength;
548 bytes += (*src)->bLength;
551 for (src = uvc_streaming_std; *src; ++src) {
552 bytes += (*src)->bLength;
556 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
562 mem += (n_desc + 1) * sizeof(*src);
564 /* Copy the descriptors. */
565 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
566 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
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;
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);
579 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
580 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
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;
588 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
595 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
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;
603 struct f_uvc_opts *opts;
606 INFO(cdev, "uvc_function_bind\n");
608 opts = to_f_uvc_opts(f->fi);
609 /* Sanity check the streaming endpoint module parameters.
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);
615 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
618 * NOTE: We assume that the user knows what they are doing and won't
619 * give parameters that their UDC doesn't support.
621 if (opts->streaming_maxpacket <= 1024) {
623 max_packet_size = opts->streaming_maxpacket;
624 } else if (opts->streaming_maxpacket <= 2048) {
626 max_packet_size = opts->streaming_maxpacket / 2;
629 max_packet_size = opts->streaming_maxpacket / 3;
632 uvc_fs_streaming_ep.wMaxPacketSize =
633 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
634 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
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;
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);
648 /* Allocate endpoints. */
649 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
651 INFO(cdev, "Unable to allocate control EP\n");
654 uvc->control_ep = ep;
655 ep->driver_data = uvc;
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);
663 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
666 INFO(cdev, "Unable to allocate streaming EP\n");
670 ep->driver_data = uvc;
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;
676 us = usb_gstrings_attach(cdev, uvc_function_strings,
677 ARRAY_SIZE(uvc_en_us_strings));
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;
688 /* Allocate interface IDs. */
689 if ((ret = usb_interface_id(c, f)) < 0)
691 uvc_iad.bFirstInterface = ret;
692 uvc_control_intf.bInterfaceNumber = ret;
693 uvc->control_intf = ret;
695 if ((ret = usb_interface_id(c, f)) < 0)
697 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
698 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
699 uvc->streaming_intf = ret;
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);
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) {
716 uvc->control_req->buf = uvc->control_buf;
717 uvc->control_req->complete = uvc_function_ep0_complete;
718 uvc->control_req->context = uvc;
720 /* Avoid letting this gadget enumerate until the userspace server is
723 if ((ret = usb_function_deactivate(f)) < 0)
726 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
727 printk(KERN_INFO "v4l2_device_register failed\n");
731 /* Initialise video. */
732 ret = uvcg_video_init(&uvc->video);
736 /* Register a V4L2 device. */
737 ret = uvc_register_video(uvc);
739 printk(KERN_INFO "Unable to register video device\n");
746 v4l2_device_unregister(&uvc->v4l2_dev);
748 video_device_release(uvc->vdev);
751 uvc->control_ep->driver_data = NULL;
753 uvc->video.ep->driver_data = NULL;
755 if (uvc->control_req)
756 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
757 kfree(uvc->control_buf);
759 usb_free_all_descriptors(f);
763 /* --------------------------------------------------------------------------
764 * USB gadget function
767 static void uvc_free_inst(struct usb_function_instance *f)
769 struct f_uvc_opts *opts = to_f_uvc_opts(f);
774 static struct usb_function_instance *uvc_alloc_inst(void)
776 struct f_uvc_opts *opts;
778 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
780 return ERR_PTR(-ENOMEM);
781 opts->func_inst.free_func_inst = uvc_free_inst;
783 return &opts->func_inst;
786 static void uvc_free(struct usb_function *f)
788 struct uvc_device *uvc = to_uvc(f);
793 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
795 struct usb_composite_dev *cdev = c->cdev;
796 struct uvc_device *uvc = to_uvc(f);
798 INFO(cdev, "%s\n", __func__);
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;
805 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
806 kfree(uvc->control_buf);
808 usb_free_all_descriptors(f);
811 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
813 struct uvc_device *uvc;
814 struct f_uvc_opts *opts;
816 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
818 return ERR_PTR(-ENOMEM);
820 uvc->state = UVC_STATE_DISCONNECTED;
821 opts = to_f_uvc_opts(fi);
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;
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;
842 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
843 MODULE_LICENSE("GPL");
844 MODULE_AUTHOR("Laurent Pinchart");