// SPDX-License-Identifier: GPL-2.0+/* * uvc_gadget.c -- USB Video Class Gadget driver * * Copyright (C) 2009-2010 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) */ #include <linux/device.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/list.h>#include <linux/module.h>#include <linux/mutex.h>#include <linux/string.h>#include <linux/usb/ch9.h>#include <linux/usb/gadget.h>#include <linux/usb/g_uvc.h>#include <linux/usb/video.h>#include <linux/vmalloc.h>#include <linux/wait.h> #include <media/v4l2-dev.h>#include <media/v4l2-event.h> #include "uvc.h"#include "uvc_configfs.h"#include "uvc_v4l2.h"#include "uvc_video.h" unsigned int uvc_gadget_trace_param;module_param_named(trace, uvc_gadget_trace_param, uint, 0644);MODULE_PARM_DESC(trace, "Trace level bitmask"); /* -------------------------------------------------------------------------- * Function descriptors */ /* string IDs are assigned dynamically */ static struct usb_string uvc_en_us_strings[] = { /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */ [UVC_STRING_STREAMING_IDX].s = "Video Streaming", { }}; static struct usb_gadget_strings uvc_stringtab = { .language = 0x0409, /* en-us */ .strings = uvc_en_us_strings,}; static struct usb_gadget_strings *uvc_function_strings[] = { &uvc_stringtab, NULL,}; #define UVC_INTF_VIDEO_CONTROL 0#define UVC_INTF_VIDEO_STREAMING 1 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ static struct usb_interface_assoc_descriptor uvc_iad = { .bLength = sizeof(uvc_iad), .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, .bFirstInterface = 0, .bInterfaceCount = 2, .bFunctionClass = USB_CLASS_VIDEO, .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, .bFunctionProtocol = 0x00, .iFunction = 0,}; static struct usb_interface_descriptor uvc_control_intf = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, .bInterfaceProtocol = 0x00, .iInterface = 0,}; static struct usb_endpoint_descriptor uvc_interrupt_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_XFER_INT, .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), .bInterval = 8,}; static struct usb_ss_ep_comp_descriptor uvc_ss_interrupt_comp = { .bLength = sizeof(uvc_ss_interrupt_comp), .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, /* The following 3 values can be tweaked if necessary. */ .bMaxBurst = 0, .bmAttributes = 0, .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),}; static struct uvc_control_endpoint_descriptor uvc_interrupt_cs_ep = { .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, .bDescriptorType = USB_DT_CS_ENDPOINT, .bDescriptorSubType = UVC_EP_INTERRUPT, .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),}; static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, .bInterfaceProtocol = 0x00, .iInterface = 0,}; static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, .bAlternateSetting = 1, .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, .bInterfaceProtocol = 0x00, .iInterface = 0,}; static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_SYNC_ASYNC | USB_ENDPOINT_XFER_ISOC, /* * The wMaxPacketSize and bInterval values will be initialized from * module parameters. */}; static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_SYNC_ASYNC | USB_ENDPOINT_XFER_ISOC, /* * The wMaxPacketSize and bInterval values will be initialized from * module parameters. */}; static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_SYNC_ASYNC | USB_ENDPOINT_XFER_ISOC, /* * The wMaxPacketSize and bInterval values will be initialized from * module parameters. */}; static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { .bLength = sizeof(uvc_ss_streaming_comp), .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, /* * The bMaxBurst, bmAttributes and wBytesPerInterval values will be * initialized from module parameters. */}; static const struct usb_descriptor_header * const uvc_fs_streaming[] = { (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, (struct usb_descriptor_header *) &uvc_fs_streaming_ep, NULL,}; static const struct usb_descriptor_header * const uvc_hs_streaming[] = { (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, (struct usb_descriptor_header *) &uvc_hs_streaming_ep, NULL,}; static const struct usb_descriptor_header * const uvc_ss_streaming[] = { (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, (struct usb_descriptor_header *) &uvc_ss_streaming_ep, (struct usb_descriptor_header *) &uvc_ss_streaming_comp, NULL,}; /* -------------------------------------------------------------------------- * Control requests */ static voiduvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req){ struct uvc_device *uvc = req->context; struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; if (uvc->event_setup_out) { uvc->event_setup_out = 0; memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_DATA; uvc_event->data.length = min_t(unsigned int, req->actual, sizeof(uvc_event->data.data)); memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length); v4l2_event_queue(&uvc->vdev, &v4l2_event); }} static intuvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl){ struct uvc_device *uvc = to_uvc(f); struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; unsigned int interface = le16_to_cpu(ctrl->wIndex) & 0xff; struct usb_ctrlrequest *mctrl; if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { uvcg_info(f, "invalid request type\n"); return -EINVAL; } /* Stall too big requests. */ if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) return -EINVAL; /* * Tell the complete callback to generate an event for the next request * that will be enqueued by UVCIOC_SEND_RESPONSE. */ uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); uvc->event_length = le16_to_cpu(ctrl->wLength); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_SETUP; memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); /* check for the interface number, fixup the interface number in * the ctrl request so the userspace doesn't have to bother with * offset and configfs parsing */ mctrl = &uvc_event->req; mctrl->wIndex &= ~cpu_to_le16(0xff); if (interface == uvc->streaming_intf) mctrl->wIndex = cpu_to_le16(UVC_STRING_STREAMING_IDX); v4l2_event_queue(&uvc->vdev, &v4l2_event); return 0;} void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep){ struct usb_composite_dev *cdev = uvc->func.config->cdev; if (disable_ep && uvc->video.ep) usb_ep_disable(uvc->video.ep); usb_composite_setup_continue(cdev);} static intuvc_function_get_alt(struct usb_function *f, unsigned interface){ struct uvc_device *uvc = to_uvc(f); uvcg_info(f, "%s(%u)\n", __func__, interface); if (interface == uvc->control_intf) return 0; else if (interface != uvc->streaming_intf) return -EINVAL; else return uvc->video.ep->enabled ? 1 : 0;} static intuvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt){ struct uvc_device *uvc = to_uvc(f); struct usb_composite_dev *cdev = f->config->cdev; struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; int ret; uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt); if (interface == uvc->control_intf) { if (alt) return -EINVAL; if (uvc->enable_interrupt_ep) { uvcg_info(f, "reset UVC interrupt endpoint\n"); usb_ep_disable(uvc->interrupt_ep); if (!uvc->interrupt_ep->desc) if (config_ep_by_speed(cdev->gadget, f, uvc->interrupt_ep)) return -EINVAL; usb_ep_enable(uvc->interrupt_ep); } if (uvc->state == UVC_STATE_DISCONNECTED) { memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_CONNECT; uvc_event->speed = cdev->gadget->speed; v4l2_event_queue(&uvc->vdev, &v4l2_event); uvc->state = UVC_STATE_CONNECTED; } return 0; } if (interface != uvc->streaming_intf) return -EINVAL; /* TODO if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) return alt ? -EINVAL : 0; */ switch (alt) { case 0: if (uvc->state != UVC_STATE_STREAMING) return 0; memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_STREAMOFF; v4l2_event_queue(&uvc->vdev, &v4l2_event); return USB_GADGET_DELAYED_STATUS; case 1: if (uvc->state != UVC_STATE_CONNECTED) return 0; if (!uvc->video.ep) return -EINVAL; uvcg_info(f, "reset UVC\n"); usb_ep_disable(uvc->video.ep); ret = config_ep_by_speed(f->config->cdev->gadget, &(uvc->func), uvc->video.ep); if (ret) return ret; usb_ep_enable(uvc->video.ep); uvc->video.max_req_size = uvc->video.ep->maxpacket * max_t(unsigned int, uvc->video.ep->maxburst, 1) * (uvc->video.ep->mult); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_STREAMON; v4l2_event_queue(&uvc->vdev, &v4l2_event); return USB_GADGET_DELAYED_STATUS; default: return -EINVAL; }} static voiduvc_function_disable(struct usb_function *f){ struct uvc_device *uvc = to_uvc(f); struct v4l2_event v4l2_event; uvcg_info(f, "%s()\n", __func__); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_DISCONNECT; v4l2_event_queue(&uvc->vdev, &v4l2_event); uvc->state = UVC_STATE_DISCONNECTED; usb_ep_disable(uvc->video.ep); if (uvc->enable_interrupt_ep) usb_ep_disable(uvc->interrupt_ep);} /* -------------------------------------------------------------------------- * Connection / disconnection */ voiduvc_function_connect(struct uvc_device *uvc){ int ret; if ((ret = usb_function_activate(&uvc->func)) < 0) uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);} voiduvc_function_disconnect(struct uvc_device *uvc){ int ret; guard(mutex)(&uvc->lock); if (uvc->func_unbound) { dev_dbg(&uvc->vdev.dev, "skipping function deactivate (unbound)\n"); return; } if ((ret = usb_function_deactivate(&uvc->func)) < 0) uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);} /* -------------------------------------------------------------------------- * USB probe and disconnect */ static ssize_t function_name_show(struct device *dev, struct device_attribute *attr, char *buf){ struct uvc_device *uvc = dev_get_drvdata(dev); return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);} static DEVICE_ATTR_RO(function_name); static void uvc_vdev_release(struct video_device *vdev){ struct uvc_device *uvc = video_get_drvdata(vdev); /* Signal uvc_function_unbind() that the video device has been released */ if (uvc->vdev_release_done) complete(uvc->vdev_release_done);} static intuvc_register_video(struct uvc_device *uvc){ struct usb_composite_dev *cdev = uvc->func.config->cdev; int ret; /* TODO reference counting. */ memset(&uvc->vdev, 0, sizeof(uvc->vdev)); uvc->vdev.v4l2_dev = &uvc->v4l2_dev; uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev; uvc->vdev.fops = &uvc_v4l2_fops; uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops; uvc->vdev.release = uvc_vdev_release; uvc->vdev.vfl_dir = VFL_DIR_TX; uvc->vdev.lock = &uvc->video.mutex; uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name)); video_set_drvdata(&uvc->vdev, uvc); ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1); if (ret < 0) return ret; ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name); if (ret < 0) { video_unregister_device(&uvc->vdev); return ret; } return 0;} #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ do { \ memcpy(mem, desc, (desc)->bLength); \ *(dst)++ = mem; \ mem += (desc)->bLength; \ } while (0) #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ do { \ const struct usb_descriptor_header * const *__src; \ for (__src = src; *__src; ++__src) { \ memcpy(mem, *__src, (*__src)->bLength); \ *dst++ = mem; \ mem += (*__src)->bLength; \ } \ } while (0) #define UVC_COPY_XU_DESCRIPTOR(mem, dst, desc) \ do { \Lines 1–500 of 1181. Load more
// SPDX-License-Identifier: GPL-2.0+ /* * uvc_gadget.c -- USB Video Class Gadget driver * * Copyright (C) 2009-2010 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) */ #include <linux/device.h> #include <linux/errno.h> #include <linux/fs.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/string.h> #include <linux/usb/ch9.h> #include <linux/usb/gadget.h> #include <linux/usb/g_uvc.h> #include <linux/usb/video.h> #include <linux/vmalloc.h> #include <linux/wait.h> #include <media/v4l2-dev.h> #include <media/v4l2-event.h> #include "uvc.h" #include "uvc_configfs.h" #include "uvc_v4l2.h" #include "uvc_video.h" unsigned int uvc_gadget_trace_param; module_param_named(trace, uvc_gadget_trace_param, uint, 0644); MODULE_PARM_DESC(trace, "Trace level bitmask"); /* -------------------------------------------------------------------------- * Function descriptors */ /* string IDs are assigned dynamically */ static struct usb_string uvc_en_us_strings[] = { /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */ [UVC_STRING_STREAMING_IDX].s = "Video Streaming", { } }; static struct usb_gadget_strings uvc_stringtab = { .language = 0x0409, /* en-us */ .strings = uvc_en_us_strings, }; static struct usb_gadget_strings *uvc_function_strings[] = { &uvc_stringtab, NULL, }; #define UVC_INTF_VIDEO_CONTROL 0 #define UVC_INTF_VIDEO_STREAMING 1 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ static struct usb_interface_assoc_descriptor uvc_iad = { .bLength = sizeof(uvc_iad), .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, .bFirstInterface = 0, .bInterfaceCount = 2, .bFunctionClass = USB_CLASS_VIDEO, .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, .bFunctionProtocol = 0x00, .iFunction = 0, }; static struct usb_interface_descriptor uvc_control_intf = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, .bInterfaceProtocol = 0x00, .iInterface = 0, }; static struct usb_endpoint_descriptor uvc_interrupt_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_XFER_INT, .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), .bInterval = 8, }; static struct usb_ss_ep_comp_descriptor uvc_ss_interrupt_comp = { .bLength = sizeof(uvc_ss_interrupt_comp), .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, /* The following 3 values can be tweaked if necessary. */ .bMaxBurst = 0, .bmAttributes = 0, .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), }; static struct uvc_control_endpoint_descriptor uvc_interrupt_cs_ep = { .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, .bDescriptorType = USB_DT_CS_ENDPOINT, .bDescriptorSubType = UVC_EP_INTERRUPT, .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), }; static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, .bInterfaceProtocol = 0x00, .iInterface = 0, }; static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, .bAlternateSetting = 1, .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, .bInterfaceProtocol = 0x00, .iInterface = 0, }; static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_SYNC_ASYNC | USB_ENDPOINT_XFER_ISOC, /* * The wMaxPacketSize and bInterval values will be initialized from * module parameters. */ }; static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_SYNC_ASYNC | USB_ENDPOINT_XFER_ISOC, /* * The wMaxPacketSize and bInterval values will be initialized from * module parameters. */ }; static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_SYNC_ASYNC | USB_ENDPOINT_XFER_ISOC, /* * The wMaxPacketSize and bInterval values will be initialized from * module parameters. */ }; static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { .bLength = sizeof(uvc_ss_streaming_comp), .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, /* * The bMaxBurst, bmAttributes and wBytesPerInterval values will be * initialized from module parameters. */ }; static const struct usb_descriptor_header * const uvc_fs_streaming[] = { (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, (struct usb_descriptor_header *) &uvc_fs_streaming_ep, NULL, }; static const struct usb_descriptor_header * const uvc_hs_streaming[] = { (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, (struct usb_descriptor_header *) &uvc_hs_streaming_ep, NULL, }; static const struct usb_descriptor_header * const uvc_ss_streaming[] = { (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, (struct usb_descriptor_header *) &uvc_ss_streaming_ep, (struct usb_descriptor_header *) &uvc_ss_streaming_comp, NULL, }; /* -------------------------------------------------------------------------- * Control requests */ static void uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) { struct uvc_device *uvc = req->context; struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; if (uvc->event_setup_out) { uvc->event_setup_out = 0; memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_DATA; uvc_event->data.length = min_t(unsigned int, req->actual, sizeof(uvc_event->data.data)); memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length); v4l2_event_queue(&uvc->vdev, &v4l2_event); } } static int uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) { struct uvc_device *uvc = to_uvc(f); struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; unsigned int interface = le16_to_cpu(ctrl->wIndex) & 0xff; struct usb_ctrlrequest *mctrl; if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { uvcg_info(f, "invalid request type\n"); return -EINVAL; } /* Stall too big requests. */ if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) return -EINVAL; /* * Tell the complete callback to generate an event for the next request * that will be enqueued by UVCIOC_SEND_RESPONSE. */ uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); uvc->event_length = le16_to_cpu(ctrl->wLength); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_SETUP; memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); /* check for the interface number, fixup the interface number in * the ctrl request so the userspace doesn't have to bother with * offset and configfs parsing */ mctrl = &uvc_event->req; mctrl->wIndex &= ~cpu_to_le16(0xff); if (interface == uvc->streaming_intf) mctrl->wIndex = cpu_to_le16(UVC_STRING_STREAMING_IDX); v4l2_event_queue(&uvc->vdev, &v4l2_event); return 0; } void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep) { struct usb_composite_dev *cdev = uvc->func.config->cdev; if (disable_ep && uvc->video.ep) usb_ep_disable(uvc->video.ep); usb_composite_setup_continue(cdev); } static int uvc_function_get_alt(struct usb_function *f, unsigned interface) { struct uvc_device *uvc = to_uvc(f); uvcg_info(f, "%s(%u)\n", __func__, interface); if (interface == uvc->control_intf) return 0; else if (interface != uvc->streaming_intf) return -EINVAL; else return uvc->video.ep->enabled ? 1 : 0; } static int uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) { struct uvc_device *uvc = to_uvc(f); struct usb_composite_dev *cdev = f->config->cdev; struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; int ret; uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt); if (interface == uvc->control_intf) { if (alt) return -EINVAL; if (uvc->enable_interrupt_ep) { uvcg_info(f, "reset UVC interrupt endpoint\n"); usb_ep_disable(uvc->interrupt_ep); if (!uvc->interrupt_ep->desc) if (config_ep_by_speed(cdev->gadget, f, uvc->interrupt_ep)) return -EINVAL; usb_ep_enable(uvc->interrupt_ep); } if (uvc->state == UVC_STATE_DISCONNECTED) { memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_CONNECT; uvc_event->speed = cdev->gadget->speed; v4l2_event_queue(&uvc->vdev, &v4l2_event); uvc->state = UVC_STATE_CONNECTED; } return 0; } if (interface != uvc->streaming_intf) return -EINVAL; /* TODO if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) return alt ? -EINVAL : 0; */ switch (alt) { case 0: if (uvc->state != UVC_STATE_STREAMING) return 0; memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_STREAMOFF; v4l2_event_queue(&uvc->vdev, &v4l2_event); return USB_GADGET_DELAYED_STATUS; case 1: if (uvc->state != UVC_STATE_CONNECTED) return 0; if (!uvc->video.ep) return -EINVAL; uvcg_info(f, "reset UVC\n"); usb_ep_disable(uvc->video.ep); ret = config_ep_by_speed(f->config->cdev->gadget, &(uvc->func), uvc->video.ep); if (ret) return ret; usb_ep_enable(uvc->video.ep); uvc->video.max_req_size = uvc->video.ep->maxpacket * max_t(unsigned int, uvc->video.ep->maxburst, 1) * (uvc->video.ep->mult); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_STREAMON; v4l2_event_queue(&uvc->vdev, &v4l2_event); return USB_GADGET_DELAYED_STATUS; default: return -EINVAL; } } static void uvc_function_disable(struct usb_function *f) { struct uvc_device *uvc = to_uvc(f); struct v4l2_event v4l2_event; uvcg_info(f, "%s()\n", __func__); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_DISCONNECT; v4l2_event_queue(&uvc->vdev, &v4l2_event); uvc->state = UVC_STATE_DISCONNECTED; usb_ep_disable(uvc->video.ep); if (uvc->enable_interrupt_ep) usb_ep_disable(uvc->interrupt_ep); } /* -------------------------------------------------------------------------- * Connection / disconnection */ void uvc_function_connect(struct uvc_device *uvc) { int ret; if ((ret = usb_function_activate(&uvc->func)) < 0) uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret); } void uvc_function_disconnect(struct uvc_device *uvc) { int ret; guard(mutex)(&uvc->lock); if (uvc->func_unbound) { dev_dbg(&uvc->vdev.dev, "skipping function deactivate (unbound)\n"); return; } if ((ret = usb_function_deactivate(&uvc->func)) < 0) uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret); } /* -------------------------------------------------------------------------- * USB probe and disconnect */ static ssize_t function_name_show(struct device *dev, struct device_attribute *attr, char *buf) { struct uvc_device *uvc = dev_get_drvdata(dev); return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name); } static DEVICE_ATTR_RO(function_name); static void uvc_vdev_release(struct video_device *vdev) { struct uvc_device *uvc = video_get_drvdata(vdev); /* Signal uvc_function_unbind() that the video device has been released */ if (uvc->vdev_release_done) complete(uvc->vdev_release_done); } static int uvc_register_video(struct uvc_device *uvc) { struct usb_composite_dev *cdev = uvc->func.config->cdev; int ret; /* TODO reference counting. */ memset(&uvc->vdev, 0, sizeof(uvc->vdev)); uvc->vdev.v4l2_dev = &uvc->v4l2_dev; uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev; uvc->vdev.fops = &uvc_v4l2_fops; uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops; uvc->vdev.release = uvc_vdev_release; uvc->vdev.vfl_dir = VFL_DIR_TX; uvc->vdev.lock = &uvc->video.mutex; uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name)); video_set_drvdata(&uvc->vdev, uvc); ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1); if (ret < 0) return ret; ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name); if (ret < 0) { video_unregister_device(&uvc->vdev); return ret; } return 0; } #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ do { \ memcpy(mem, desc, (desc)->bLength); \ *(dst)++ = mem; \ mem += (desc)->bLength; \ } while (0) #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ do { \ const struct usb_descriptor_header * const *__src; \ for (__src = src; *__src; ++__src) { \ memcpy(mem, *__src, (*__src)->bLength); \ *dst++ = mem; \ mem += (*__src)->bLength; \ } \ } while (0) #define UVC_COPY_XU_DESCRIPTOR(mem, dst, desc) \ do { \