15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 200a2430fSAndrzej Pietrasiewicz /* 300a2430fSAndrzej Pietrasiewicz * uvc_gadget.c -- USB Video Class Gadget driver 400a2430fSAndrzej Pietrasiewicz * 500a2430fSAndrzej Pietrasiewicz * Copyright (C) 2009-2010 600a2430fSAndrzej Pietrasiewicz * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 700a2430fSAndrzej Pietrasiewicz */ 800a2430fSAndrzej Pietrasiewicz 900a2430fSAndrzej Pietrasiewicz #include <linux/device.h> 1000a2430fSAndrzej Pietrasiewicz #include <linux/errno.h> 1100a2430fSAndrzej Pietrasiewicz #include <linux/fs.h> 12284eb166SLaurent Pinchart #include <linux/kernel.h> 1300a2430fSAndrzej Pietrasiewicz #include <linux/list.h> 14284eb166SLaurent Pinchart #include <linux/module.h> 1500a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h> 1600a2430fSAndrzej Pietrasiewicz #include <linux/string.h> 1700a2430fSAndrzej Pietrasiewicz #include <linux/usb/ch9.h> 1800a2430fSAndrzej Pietrasiewicz #include <linux/usb/gadget.h> 19284eb166SLaurent Pinchart #include <linux/usb/g_uvc.h> 2000a2430fSAndrzej Pietrasiewicz #include <linux/usb/video.h> 2100a2430fSAndrzej Pietrasiewicz #include <linux/vmalloc.h> 2200a2430fSAndrzej Pietrasiewicz #include <linux/wait.h> 2300a2430fSAndrzej Pietrasiewicz 2400a2430fSAndrzej Pietrasiewicz #include <media/v4l2-dev.h> 2500a2430fSAndrzej Pietrasiewicz #include <media/v4l2-event.h> 2600a2430fSAndrzej Pietrasiewicz 2700a2430fSAndrzej Pietrasiewicz #include "uvc.h" 2846919a23SAndrzej Pietrasiewicz #include "uvc_configfs.h" 293a83c16eSAndrzej Pietrasiewicz #include "uvc_v4l2.h" 303a83c16eSAndrzej Pietrasiewicz #include "uvc_video.h" 3100a2430fSAndrzej Pietrasiewicz 3200a2430fSAndrzej Pietrasiewicz unsigned int uvc_gadget_trace_param; 3320970d82SLaurent Pinchart module_param_named(trace, uvc_gadget_trace_param, uint, 0644); 3420970d82SLaurent Pinchart MODULE_PARM_DESC(trace, "Trace level bitmask"); 3500a2430fSAndrzej Pietrasiewicz 3600a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 3700a2430fSAndrzej Pietrasiewicz * Function descriptors 3800a2430fSAndrzej Pietrasiewicz */ 3900a2430fSAndrzej Pietrasiewicz 4000a2430fSAndrzej Pietrasiewicz /* string IDs are assigned dynamically */ 4100a2430fSAndrzej Pietrasiewicz 4200a2430fSAndrzej Pietrasiewicz #define UVC_STRING_CONTROL_IDX 0 4300a2430fSAndrzej Pietrasiewicz #define UVC_STRING_STREAMING_IDX 1 4400a2430fSAndrzej Pietrasiewicz 4500a2430fSAndrzej Pietrasiewicz static struct usb_string uvc_en_us_strings[] = { 46324e4f85SDan Vacura /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */ 4700a2430fSAndrzej Pietrasiewicz [UVC_STRING_STREAMING_IDX].s = "Video Streaming", 4800a2430fSAndrzej Pietrasiewicz { } 4900a2430fSAndrzej Pietrasiewicz }; 5000a2430fSAndrzej Pietrasiewicz 5100a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings uvc_stringtab = { 5200a2430fSAndrzej Pietrasiewicz .language = 0x0409, /* en-us */ 5300a2430fSAndrzej Pietrasiewicz .strings = uvc_en_us_strings, 5400a2430fSAndrzej Pietrasiewicz }; 5500a2430fSAndrzej Pietrasiewicz 5600a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings *uvc_function_strings[] = { 5700a2430fSAndrzej Pietrasiewicz &uvc_stringtab, 5800a2430fSAndrzej Pietrasiewicz NULL, 5900a2430fSAndrzej Pietrasiewicz }; 6000a2430fSAndrzej Pietrasiewicz 6100a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_CONTROL 0 6200a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_STREAMING 1 6300a2430fSAndrzej Pietrasiewicz 6400a2430fSAndrzej Pietrasiewicz #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ 6500a2430fSAndrzej Pietrasiewicz 666d11ed76SAndrzej Pietrasiewicz static struct usb_interface_assoc_descriptor uvc_iad = { 6700a2430fSAndrzej Pietrasiewicz .bLength = sizeof(uvc_iad), 6800a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 6900a2430fSAndrzej Pietrasiewicz .bFirstInterface = 0, 7000a2430fSAndrzej Pietrasiewicz .bInterfaceCount = 2, 7100a2430fSAndrzej Pietrasiewicz .bFunctionClass = USB_CLASS_VIDEO, 7200a2430fSAndrzej Pietrasiewicz .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, 7300a2430fSAndrzej Pietrasiewicz .bFunctionProtocol = 0x00, 7400a2430fSAndrzej Pietrasiewicz .iFunction = 0, 7500a2430fSAndrzej Pietrasiewicz }; 7600a2430fSAndrzej Pietrasiewicz 776d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_control_intf = { 7800a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_INTERFACE_SIZE, 7900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 8000a2430fSAndrzej Pietrasiewicz .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, 8100a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8200a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 1, 8300a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_VIDEO, 8400a2430fSAndrzej Pietrasiewicz .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, 8500a2430fSAndrzej Pietrasiewicz .bInterfaceProtocol = 0x00, 8600a2430fSAndrzej Pietrasiewicz .iInterface = 0, 8700a2430fSAndrzej Pietrasiewicz }; 8800a2430fSAndrzej Pietrasiewicz 896d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_control_ep = { 9000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 9100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 9200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 9300a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 9400a2430fSAndrzej Pietrasiewicz .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 9500a2430fSAndrzej Pietrasiewicz .bInterval = 8, 9600a2430fSAndrzej Pietrasiewicz }; 9700a2430fSAndrzej Pietrasiewicz 986d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = { 9900a2430fSAndrzej Pietrasiewicz .bLength = sizeof(uvc_ss_control_comp), 10000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 10100a2430fSAndrzej Pietrasiewicz /* The following 3 values can be tweaked if necessary. */ 10200a2430fSAndrzej Pietrasiewicz .bMaxBurst = 0, 10300a2430fSAndrzej Pietrasiewicz .bmAttributes = 0, 10400a2430fSAndrzej Pietrasiewicz .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 10500a2430fSAndrzej Pietrasiewicz }; 10600a2430fSAndrzej Pietrasiewicz 1076d11ed76SAndrzej Pietrasiewicz static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = { 10800a2430fSAndrzej Pietrasiewicz .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, 10900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_CS_ENDPOINT, 11000a2430fSAndrzej Pietrasiewicz .bDescriptorSubType = UVC_EP_INTERRUPT, 11100a2430fSAndrzej Pietrasiewicz .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 11200a2430fSAndrzej Pietrasiewicz }; 11300a2430fSAndrzej Pietrasiewicz 1146d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { 11500a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_INTERFACE_SIZE, 11600a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 11700a2430fSAndrzej Pietrasiewicz .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, 11800a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 11900a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 0, 12000a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_VIDEO, 12100a2430fSAndrzej Pietrasiewicz .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 12200a2430fSAndrzej Pietrasiewicz .bInterfaceProtocol = 0x00, 12300a2430fSAndrzej Pietrasiewicz .iInterface = 0, 12400a2430fSAndrzej Pietrasiewicz }; 12500a2430fSAndrzej Pietrasiewicz 1266d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { 12700a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_INTERFACE_SIZE, 12800a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 12900a2430fSAndrzej Pietrasiewicz .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, 13000a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 1, 13100a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 1, 13200a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_VIDEO, 13300a2430fSAndrzej Pietrasiewicz .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 13400a2430fSAndrzej Pietrasiewicz .bInterfaceProtocol = 0x00, 13500a2430fSAndrzej Pietrasiewicz .iInterface = 0, 13600a2430fSAndrzej Pietrasiewicz }; 13700a2430fSAndrzej Pietrasiewicz 1386d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { 13900a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 14000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 14100a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 14200a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 14300a2430fSAndrzej Pietrasiewicz | USB_ENDPOINT_XFER_ISOC, 144c5d337a3SLaurent Pinchart /* 145c5d337a3SLaurent Pinchart * The wMaxPacketSize and bInterval values will be initialized from 14600a2430fSAndrzej Pietrasiewicz * module parameters. 14700a2430fSAndrzej Pietrasiewicz */ 14800a2430fSAndrzej Pietrasiewicz }; 14900a2430fSAndrzej Pietrasiewicz 1506d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { 15100a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 15200a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 15300a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 15400a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 15500a2430fSAndrzej Pietrasiewicz | USB_ENDPOINT_XFER_ISOC, 156c5d337a3SLaurent Pinchart /* 157c5d337a3SLaurent Pinchart * The wMaxPacketSize and bInterval values will be initialized from 15800a2430fSAndrzej Pietrasiewicz * module parameters. 15900a2430fSAndrzej Pietrasiewicz */ 16000a2430fSAndrzej Pietrasiewicz }; 16100a2430fSAndrzej Pietrasiewicz 1626d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { 16300a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 16400a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 16500a2430fSAndrzej Pietrasiewicz 16600a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 16700a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 16800a2430fSAndrzej Pietrasiewicz | USB_ENDPOINT_XFER_ISOC, 169c5d337a3SLaurent Pinchart /* 170c5d337a3SLaurent Pinchart * The wMaxPacketSize and bInterval values will be initialized from 17100a2430fSAndrzej Pietrasiewicz * module parameters. 17200a2430fSAndrzej Pietrasiewicz */ 17300a2430fSAndrzej Pietrasiewicz }; 17400a2430fSAndrzej Pietrasiewicz 1756d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { 17600a2430fSAndrzej Pietrasiewicz .bLength = sizeof(uvc_ss_streaming_comp), 17700a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 178c5d337a3SLaurent Pinchart /* 179c5d337a3SLaurent Pinchart * The bMaxBurst, bmAttributes and wBytesPerInterval values will be 18000a2430fSAndrzej Pietrasiewicz * initialized from module parameters. 18100a2430fSAndrzej Pietrasiewicz */ 18200a2430fSAndrzej Pietrasiewicz }; 18300a2430fSAndrzej Pietrasiewicz 18400a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_fs_streaming[] = { 18500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 18600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_fs_streaming_ep, 18700a2430fSAndrzej Pietrasiewicz NULL, 18800a2430fSAndrzej Pietrasiewicz }; 18900a2430fSAndrzej Pietrasiewicz 19000a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_hs_streaming[] = { 19100a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 19200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_hs_streaming_ep, 19300a2430fSAndrzej Pietrasiewicz NULL, 19400a2430fSAndrzej Pietrasiewicz }; 19500a2430fSAndrzej Pietrasiewicz 19600a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_ss_streaming[] = { 19700a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 19800a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_ss_streaming_ep, 19900a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_ss_streaming_comp, 20000a2430fSAndrzej Pietrasiewicz NULL, 20100a2430fSAndrzej Pietrasiewicz }; 20200a2430fSAndrzej Pietrasiewicz 20300a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 20400a2430fSAndrzej Pietrasiewicz * Control requests 20500a2430fSAndrzej Pietrasiewicz */ 20600a2430fSAndrzej Pietrasiewicz 20700a2430fSAndrzej Pietrasiewicz static void 20800a2430fSAndrzej Pietrasiewicz uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) 20900a2430fSAndrzej Pietrasiewicz { 21000a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = req->context; 21100a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 21200a2430fSAndrzej Pietrasiewicz struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 21300a2430fSAndrzej Pietrasiewicz 21400a2430fSAndrzej Pietrasiewicz if (uvc->event_setup_out) { 21500a2430fSAndrzej Pietrasiewicz uvc->event_setup_out = 0; 21600a2430fSAndrzej Pietrasiewicz 21700a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 21800a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_DATA; 21900a2430fSAndrzej Pietrasiewicz uvc_event->data.length = req->actual; 22000a2430fSAndrzej Pietrasiewicz memcpy(&uvc_event->data.data, req->buf, req->actual); 221dbe98b30SHans Verkuil v4l2_event_queue(&uvc->vdev, &v4l2_event); 22200a2430fSAndrzej Pietrasiewicz } 22300a2430fSAndrzej Pietrasiewicz } 22400a2430fSAndrzej Pietrasiewicz 22500a2430fSAndrzej Pietrasiewicz static int 22600a2430fSAndrzej Pietrasiewicz uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 22700a2430fSAndrzej Pietrasiewicz { 22800a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 22900a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 23000a2430fSAndrzej Pietrasiewicz struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 23100a2430fSAndrzej Pietrasiewicz 23200a2430fSAndrzej Pietrasiewicz if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { 233dc0f755bSLaurent Pinchart uvcg_info(f, "invalid request type\n"); 23400a2430fSAndrzej Pietrasiewicz return -EINVAL; 23500a2430fSAndrzej Pietrasiewicz } 23600a2430fSAndrzej Pietrasiewicz 23700a2430fSAndrzej Pietrasiewicz /* Stall too big requests. */ 23800a2430fSAndrzej Pietrasiewicz if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) 23900a2430fSAndrzej Pietrasiewicz return -EINVAL; 24000a2430fSAndrzej Pietrasiewicz 241c5d337a3SLaurent Pinchart /* 242c5d337a3SLaurent Pinchart * Tell the complete callback to generate an event for the next request 24326a029f2SLaurent Pinchart * that will be enqueued by UVCIOC_SEND_RESPONSE. 24426a029f2SLaurent Pinchart */ 24526a029f2SLaurent Pinchart uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); 24626a029f2SLaurent Pinchart uvc->event_length = le16_to_cpu(ctrl->wLength); 24726a029f2SLaurent Pinchart 24800a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 24900a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_SETUP; 25000a2430fSAndrzej Pietrasiewicz memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); 251dbe98b30SHans Verkuil v4l2_event_queue(&uvc->vdev, &v4l2_event); 25200a2430fSAndrzej Pietrasiewicz 25300a2430fSAndrzej Pietrasiewicz return 0; 25400a2430fSAndrzej Pietrasiewicz } 25500a2430fSAndrzej Pietrasiewicz 25600a2430fSAndrzej Pietrasiewicz void uvc_function_setup_continue(struct uvc_device *uvc) 25700a2430fSAndrzej Pietrasiewicz { 25800a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = uvc->func.config->cdev; 25900a2430fSAndrzej Pietrasiewicz 26000a2430fSAndrzej Pietrasiewicz usb_composite_setup_continue(cdev); 26100a2430fSAndrzej Pietrasiewicz } 26200a2430fSAndrzej Pietrasiewicz 26300a2430fSAndrzej Pietrasiewicz static int 26400a2430fSAndrzej Pietrasiewicz uvc_function_get_alt(struct usb_function *f, unsigned interface) 26500a2430fSAndrzej Pietrasiewicz { 26600a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 26700a2430fSAndrzej Pietrasiewicz 268dc0f755bSLaurent Pinchart uvcg_info(f, "%s(%u)\n", __func__, interface); 26900a2430fSAndrzej Pietrasiewicz 27000a2430fSAndrzej Pietrasiewicz if (interface == uvc->control_intf) 27100a2430fSAndrzej Pietrasiewicz return 0; 27200a2430fSAndrzej Pietrasiewicz else if (interface != uvc->streaming_intf) 27300a2430fSAndrzej Pietrasiewicz return -EINVAL; 27400a2430fSAndrzej Pietrasiewicz else 275d62bf8c1SRobert Baldyga return uvc->video.ep->enabled ? 1 : 0; 27600a2430fSAndrzej Pietrasiewicz } 27700a2430fSAndrzej Pietrasiewicz 27800a2430fSAndrzej Pietrasiewicz static int 27900a2430fSAndrzej Pietrasiewicz uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) 28000a2430fSAndrzej Pietrasiewicz { 28100a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 282c92bae75SFelipe Balbi struct usb_composite_dev *cdev = f->config->cdev; 28300a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 28400a2430fSAndrzej Pietrasiewicz struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 28500a2430fSAndrzej Pietrasiewicz int ret; 28600a2430fSAndrzej Pietrasiewicz 287dc0f755bSLaurent Pinchart uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt); 28800a2430fSAndrzej Pietrasiewicz 28900a2430fSAndrzej Pietrasiewicz if (interface == uvc->control_intf) { 29000a2430fSAndrzej Pietrasiewicz if (alt) 29100a2430fSAndrzej Pietrasiewicz return -EINVAL; 29200a2430fSAndrzej Pietrasiewicz 293dc0f755bSLaurent Pinchart uvcg_info(f, "reset UVC Control\n"); 29462e37078SFelipe Balbi usb_ep_disable(uvc->control_ep); 29562e37078SFelipe Balbi 29662e37078SFelipe Balbi if (!uvc->control_ep->desc) 29762e37078SFelipe Balbi if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep)) 29862e37078SFelipe Balbi return -EINVAL; 29962e37078SFelipe Balbi 30062e37078SFelipe Balbi usb_ep_enable(uvc->control_ep); 30162e37078SFelipe Balbi 30200a2430fSAndrzej Pietrasiewicz if (uvc->state == UVC_STATE_DISCONNECTED) { 30300a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 30400a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_CONNECT; 305c92bae75SFelipe Balbi uvc_event->speed = cdev->gadget->speed; 306dbe98b30SHans Verkuil v4l2_event_queue(&uvc->vdev, &v4l2_event); 30700a2430fSAndrzej Pietrasiewicz 30800a2430fSAndrzej Pietrasiewicz uvc->state = UVC_STATE_CONNECTED; 30900a2430fSAndrzej Pietrasiewicz } 31000a2430fSAndrzej Pietrasiewicz 31100a2430fSAndrzej Pietrasiewicz return 0; 31200a2430fSAndrzej Pietrasiewicz } 31300a2430fSAndrzej Pietrasiewicz 31400a2430fSAndrzej Pietrasiewicz if (interface != uvc->streaming_intf) 31500a2430fSAndrzej Pietrasiewicz return -EINVAL; 31600a2430fSAndrzej Pietrasiewicz 31700a2430fSAndrzej Pietrasiewicz /* TODO 31800a2430fSAndrzej Pietrasiewicz if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) 31900a2430fSAndrzej Pietrasiewicz return alt ? -EINVAL : 0; 32000a2430fSAndrzej Pietrasiewicz */ 32100a2430fSAndrzej Pietrasiewicz 32200a2430fSAndrzej Pietrasiewicz switch (alt) { 32300a2430fSAndrzej Pietrasiewicz case 0: 32400a2430fSAndrzej Pietrasiewicz if (uvc->state != UVC_STATE_STREAMING) 32500a2430fSAndrzej Pietrasiewicz return 0; 32600a2430fSAndrzej Pietrasiewicz 327d62bf8c1SRobert Baldyga if (uvc->video.ep) 32800a2430fSAndrzej Pietrasiewicz usb_ep_disable(uvc->video.ep); 32900a2430fSAndrzej Pietrasiewicz 33000a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 33100a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_STREAMOFF; 332dbe98b30SHans Verkuil v4l2_event_queue(&uvc->vdev, &v4l2_event); 33300a2430fSAndrzej Pietrasiewicz 33400a2430fSAndrzej Pietrasiewicz uvc->state = UVC_STATE_CONNECTED; 33500a2430fSAndrzej Pietrasiewicz return 0; 33600a2430fSAndrzej Pietrasiewicz 33700a2430fSAndrzej Pietrasiewicz case 1: 33800a2430fSAndrzej Pietrasiewicz if (uvc->state != UVC_STATE_CONNECTED) 33900a2430fSAndrzej Pietrasiewicz return 0; 34000a2430fSAndrzej Pietrasiewicz 341c92bae75SFelipe Balbi if (!uvc->video.ep) 342c92bae75SFelipe Balbi return -EINVAL; 343c92bae75SFelipe Balbi 344dc0f755bSLaurent Pinchart uvcg_info(f, "reset UVC\n"); 345c92bae75SFelipe Balbi usb_ep_disable(uvc->video.ep); 346c92bae75SFelipe Balbi 34700a2430fSAndrzej Pietrasiewicz ret = config_ep_by_speed(f->config->cdev->gadget, 34800a2430fSAndrzej Pietrasiewicz &(uvc->func), uvc->video.ep); 34900a2430fSAndrzej Pietrasiewicz if (ret) 35000a2430fSAndrzej Pietrasiewicz return ret; 35100a2430fSAndrzej Pietrasiewicz usb_ep_enable(uvc->video.ep); 35200a2430fSAndrzej Pietrasiewicz 35300a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 35400a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_STREAMON; 355dbe98b30SHans Verkuil v4l2_event_queue(&uvc->vdev, &v4l2_event); 35600a2430fSAndrzej Pietrasiewicz return USB_GADGET_DELAYED_STATUS; 35700a2430fSAndrzej Pietrasiewicz 35800a2430fSAndrzej Pietrasiewicz default: 35900a2430fSAndrzej Pietrasiewicz return -EINVAL; 36000a2430fSAndrzej Pietrasiewicz } 36100a2430fSAndrzej Pietrasiewicz } 36200a2430fSAndrzej Pietrasiewicz 36300a2430fSAndrzej Pietrasiewicz static void 36400a2430fSAndrzej Pietrasiewicz uvc_function_disable(struct usb_function *f) 36500a2430fSAndrzej Pietrasiewicz { 36600a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 36700a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 36800a2430fSAndrzej Pietrasiewicz 369dc0f755bSLaurent Pinchart uvcg_info(f, "%s()\n", __func__); 37000a2430fSAndrzej Pietrasiewicz 37100a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 37200a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_DISCONNECT; 373dbe98b30SHans Verkuil v4l2_event_queue(&uvc->vdev, &v4l2_event); 37400a2430fSAndrzej Pietrasiewicz 37500a2430fSAndrzej Pietrasiewicz uvc->state = UVC_STATE_DISCONNECTED; 376e3122f5fSFelipe Balbi 377e3122f5fSFelipe Balbi usb_ep_disable(uvc->video.ep); 378e3122f5fSFelipe Balbi usb_ep_disable(uvc->control_ep); 37900a2430fSAndrzej Pietrasiewicz } 38000a2430fSAndrzej Pietrasiewicz 38100a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 38200a2430fSAndrzej Pietrasiewicz * Connection / disconnection 38300a2430fSAndrzej Pietrasiewicz */ 38400a2430fSAndrzej Pietrasiewicz 38500a2430fSAndrzej Pietrasiewicz void 38600a2430fSAndrzej Pietrasiewicz uvc_function_connect(struct uvc_device *uvc) 38700a2430fSAndrzej Pietrasiewicz { 38800a2430fSAndrzej Pietrasiewicz int ret; 38900a2430fSAndrzej Pietrasiewicz 39000a2430fSAndrzej Pietrasiewicz if ((ret = usb_function_activate(&uvc->func)) < 0) 391dc0f755bSLaurent Pinchart uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret); 39200a2430fSAndrzej Pietrasiewicz } 39300a2430fSAndrzej Pietrasiewicz 39400a2430fSAndrzej Pietrasiewicz void 39500a2430fSAndrzej Pietrasiewicz uvc_function_disconnect(struct uvc_device *uvc) 39600a2430fSAndrzej Pietrasiewicz { 39700a2430fSAndrzej Pietrasiewicz int ret; 39800a2430fSAndrzej Pietrasiewicz 39900a2430fSAndrzej Pietrasiewicz if ((ret = usb_function_deactivate(&uvc->func)) < 0) 400dc0f755bSLaurent Pinchart uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret); 40100a2430fSAndrzej Pietrasiewicz } 40200a2430fSAndrzej Pietrasiewicz 40300a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 40400a2430fSAndrzej Pietrasiewicz * USB probe and disconnect 40500a2430fSAndrzej Pietrasiewicz */ 40600a2430fSAndrzej Pietrasiewicz 407d7af78b9SKieran Bingham static ssize_t function_name_show(struct device *dev, 408d7af78b9SKieran Bingham struct device_attribute *attr, char *buf) 409d7af78b9SKieran Bingham { 410d7af78b9SKieran Bingham struct uvc_device *uvc = dev_get_drvdata(dev); 411d7af78b9SKieran Bingham 412d7af78b9SKieran Bingham return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name); 413d7af78b9SKieran Bingham } 414d7af78b9SKieran Bingham 415d7af78b9SKieran Bingham static DEVICE_ATTR_RO(function_name); 416d7af78b9SKieran Bingham 41700a2430fSAndrzej Pietrasiewicz static int 41800a2430fSAndrzej Pietrasiewicz uvc_register_video(struct uvc_device *uvc) 41900a2430fSAndrzej Pietrasiewicz { 42000a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = uvc->func.config->cdev; 421d7af78b9SKieran Bingham int ret; 42200a2430fSAndrzej Pietrasiewicz 42300a2430fSAndrzej Pietrasiewicz /* TODO reference counting. */ 424e4ce9ed8SMichael Grzeschik memset(&uvc->vdev, 0, sizeof(uvc->video)); 425dbe98b30SHans Verkuil uvc->vdev.v4l2_dev = &uvc->v4l2_dev; 426b9b82d3dSMichael Grzeschik uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev; 427dbe98b30SHans Verkuil uvc->vdev.fops = &uvc_v4l2_fops; 428dbe98b30SHans Verkuil uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops; 429dbe98b30SHans Verkuil uvc->vdev.release = video_device_release_empty; 430dbe98b30SHans Verkuil uvc->vdev.vfl_dir = VFL_DIR_TX; 431dbe98b30SHans Verkuil uvc->vdev.lock = &uvc->video.mutex; 4321397e3ecSHans Verkuil uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; 433b7db5733SWolfram Sang strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name)); 43400a2430fSAndrzej Pietrasiewicz 435dbe98b30SHans Verkuil video_set_drvdata(&uvc->vdev, uvc); 43600a2430fSAndrzej Pietrasiewicz 4370ceba550SHans Verkuil ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1); 438d7af78b9SKieran Bingham if (ret < 0) 439d7af78b9SKieran Bingham return ret; 440d7af78b9SKieran Bingham 441d7af78b9SKieran Bingham ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name); 442d7af78b9SKieran Bingham if (ret < 0) { 443d7af78b9SKieran Bingham video_unregister_device(&uvc->vdev); 444d7af78b9SKieran Bingham return ret; 445d7af78b9SKieran Bingham } 446d7af78b9SKieran Bingham 447d7af78b9SKieran Bingham return 0; 44800a2430fSAndrzej Pietrasiewicz } 44900a2430fSAndrzej Pietrasiewicz 45000a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ 45100a2430fSAndrzej Pietrasiewicz do { \ 45200a2430fSAndrzej Pietrasiewicz memcpy(mem, desc, (desc)->bLength); \ 45300a2430fSAndrzej Pietrasiewicz *(dst)++ = mem; \ 45400a2430fSAndrzej Pietrasiewicz mem += (desc)->bLength; \ 45500a2430fSAndrzej Pietrasiewicz } while (0); 45600a2430fSAndrzej Pietrasiewicz 45700a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ 45800a2430fSAndrzej Pietrasiewicz do { \ 45900a2430fSAndrzej Pietrasiewicz const struct usb_descriptor_header * const *__src; \ 46000a2430fSAndrzej Pietrasiewicz for (__src = src; *__src; ++__src) { \ 46100a2430fSAndrzej Pietrasiewicz memcpy(mem, *__src, (*__src)->bLength); \ 46200a2430fSAndrzej Pietrasiewicz *dst++ = mem; \ 46300a2430fSAndrzej Pietrasiewicz mem += (*__src)->bLength; \ 46400a2430fSAndrzej Pietrasiewicz } \ 46500a2430fSAndrzej Pietrasiewicz } while (0) 46600a2430fSAndrzej Pietrasiewicz 4676d11ed76SAndrzej Pietrasiewicz static struct usb_descriptor_header ** 46800a2430fSAndrzej Pietrasiewicz uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) 46900a2430fSAndrzej Pietrasiewicz { 47000a2430fSAndrzej Pietrasiewicz struct uvc_input_header_descriptor *uvc_streaming_header; 47100a2430fSAndrzej Pietrasiewicz struct uvc_header_descriptor *uvc_control_header; 47200a2430fSAndrzej Pietrasiewicz const struct uvc_descriptor_header * const *uvc_control_desc; 47300a2430fSAndrzej Pietrasiewicz const struct uvc_descriptor_header * const *uvc_streaming_cls; 47400a2430fSAndrzej Pietrasiewicz const struct usb_descriptor_header * const *uvc_streaming_std; 47500a2430fSAndrzej Pietrasiewicz const struct usb_descriptor_header * const *src; 47600a2430fSAndrzej Pietrasiewicz struct usb_descriptor_header **dst; 47700a2430fSAndrzej Pietrasiewicz struct usb_descriptor_header **hdr; 47800a2430fSAndrzej Pietrasiewicz unsigned int control_size; 47900a2430fSAndrzej Pietrasiewicz unsigned int streaming_size; 48000a2430fSAndrzej Pietrasiewicz unsigned int n_desc; 48100a2430fSAndrzej Pietrasiewicz unsigned int bytes; 48200a2430fSAndrzej Pietrasiewicz void *mem; 48300a2430fSAndrzej Pietrasiewicz 48400a2430fSAndrzej Pietrasiewicz switch (speed) { 48500a2430fSAndrzej Pietrasiewicz case USB_SPEED_SUPER: 48600a2430fSAndrzej Pietrasiewicz uvc_control_desc = uvc->desc.ss_control; 48700a2430fSAndrzej Pietrasiewicz uvc_streaming_cls = uvc->desc.ss_streaming; 48800a2430fSAndrzej Pietrasiewicz uvc_streaming_std = uvc_ss_streaming; 48900a2430fSAndrzej Pietrasiewicz break; 49000a2430fSAndrzej Pietrasiewicz 49100a2430fSAndrzej Pietrasiewicz case USB_SPEED_HIGH: 49200a2430fSAndrzej Pietrasiewicz uvc_control_desc = uvc->desc.fs_control; 49300a2430fSAndrzej Pietrasiewicz uvc_streaming_cls = uvc->desc.hs_streaming; 49400a2430fSAndrzej Pietrasiewicz uvc_streaming_std = uvc_hs_streaming; 49500a2430fSAndrzej Pietrasiewicz break; 49600a2430fSAndrzej Pietrasiewicz 49700a2430fSAndrzej Pietrasiewicz case USB_SPEED_FULL: 49800a2430fSAndrzej Pietrasiewicz default: 49900a2430fSAndrzej Pietrasiewicz uvc_control_desc = uvc->desc.fs_control; 50000a2430fSAndrzej Pietrasiewicz uvc_streaming_cls = uvc->desc.fs_streaming; 50100a2430fSAndrzej Pietrasiewicz uvc_streaming_std = uvc_fs_streaming; 50200a2430fSAndrzej Pietrasiewicz break; 50300a2430fSAndrzej Pietrasiewicz } 50400a2430fSAndrzej Pietrasiewicz 5056c25955eSAndrzej Pietrasiewicz if (!uvc_control_desc || !uvc_streaming_cls) 5066c25955eSAndrzej Pietrasiewicz return ERR_PTR(-ENODEV); 5076c25955eSAndrzej Pietrasiewicz 508c5d337a3SLaurent Pinchart /* 509c5d337a3SLaurent Pinchart * Descriptors layout 51000a2430fSAndrzej Pietrasiewicz * 51100a2430fSAndrzej Pietrasiewicz * uvc_iad 51200a2430fSAndrzej Pietrasiewicz * uvc_control_intf 51300a2430fSAndrzej Pietrasiewicz * Class-specific UVC control descriptors 51400a2430fSAndrzej Pietrasiewicz * uvc_control_ep 51500a2430fSAndrzej Pietrasiewicz * uvc_control_cs_ep 51600a2430fSAndrzej Pietrasiewicz * uvc_ss_control_comp (for SS only) 51700a2430fSAndrzej Pietrasiewicz * uvc_streaming_intf_alt0 51800a2430fSAndrzej Pietrasiewicz * Class-specific UVC streaming descriptors 51900a2430fSAndrzej Pietrasiewicz * uvc_{fs|hs}_streaming 52000a2430fSAndrzej Pietrasiewicz */ 52100a2430fSAndrzej Pietrasiewicz 52200a2430fSAndrzej Pietrasiewicz /* Count descriptors and compute their size. */ 52300a2430fSAndrzej Pietrasiewicz control_size = 0; 52400a2430fSAndrzej Pietrasiewicz streaming_size = 0; 52500a2430fSAndrzej Pietrasiewicz bytes = uvc_iad.bLength + uvc_control_intf.bLength 52600a2430fSAndrzej Pietrasiewicz + uvc_control_ep.bLength + uvc_control_cs_ep.bLength 52700a2430fSAndrzej Pietrasiewicz + uvc_streaming_intf_alt0.bLength; 52800a2430fSAndrzej Pietrasiewicz 52900a2430fSAndrzej Pietrasiewicz if (speed == USB_SPEED_SUPER) { 53000a2430fSAndrzej Pietrasiewicz bytes += uvc_ss_control_comp.bLength; 53100a2430fSAndrzej Pietrasiewicz n_desc = 6; 53200a2430fSAndrzej Pietrasiewicz } else { 53300a2430fSAndrzej Pietrasiewicz n_desc = 5; 53400a2430fSAndrzej Pietrasiewicz } 53500a2430fSAndrzej Pietrasiewicz 53600a2430fSAndrzej Pietrasiewicz for (src = (const struct usb_descriptor_header **)uvc_control_desc; 53700a2430fSAndrzej Pietrasiewicz *src; ++src) { 53800a2430fSAndrzej Pietrasiewicz control_size += (*src)->bLength; 53900a2430fSAndrzej Pietrasiewicz bytes += (*src)->bLength; 54000a2430fSAndrzej Pietrasiewicz n_desc++; 54100a2430fSAndrzej Pietrasiewicz } 54200a2430fSAndrzej Pietrasiewicz for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; 54300a2430fSAndrzej Pietrasiewicz *src; ++src) { 54400a2430fSAndrzej Pietrasiewicz streaming_size += (*src)->bLength; 54500a2430fSAndrzej Pietrasiewicz bytes += (*src)->bLength; 54600a2430fSAndrzej Pietrasiewicz n_desc++; 54700a2430fSAndrzej Pietrasiewicz } 54800a2430fSAndrzej Pietrasiewicz for (src = uvc_streaming_std; *src; ++src) { 54900a2430fSAndrzej Pietrasiewicz bytes += (*src)->bLength; 55000a2430fSAndrzej Pietrasiewicz n_desc++; 55100a2430fSAndrzej Pietrasiewicz } 55200a2430fSAndrzej Pietrasiewicz 55300a2430fSAndrzej Pietrasiewicz mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); 55400a2430fSAndrzej Pietrasiewicz if (mem == NULL) 55500a2430fSAndrzej Pietrasiewicz return NULL; 55600a2430fSAndrzej Pietrasiewicz 55700a2430fSAndrzej Pietrasiewicz hdr = mem; 55800a2430fSAndrzej Pietrasiewicz dst = mem; 55900a2430fSAndrzej Pietrasiewicz mem += (n_desc + 1) * sizeof(*src); 56000a2430fSAndrzej Pietrasiewicz 56100a2430fSAndrzej Pietrasiewicz /* Copy the descriptors. */ 56200a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); 56300a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); 56400a2430fSAndrzej Pietrasiewicz 56500a2430fSAndrzej Pietrasiewicz uvc_control_header = mem; 56600a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTORS(mem, dst, 56700a2430fSAndrzej Pietrasiewicz (const struct usb_descriptor_header **)uvc_control_desc); 56800a2430fSAndrzej Pietrasiewicz uvc_control_header->wTotalLength = cpu_to_le16(control_size); 56900a2430fSAndrzej Pietrasiewicz uvc_control_header->bInCollection = 1; 57000a2430fSAndrzej Pietrasiewicz uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; 57100a2430fSAndrzej Pietrasiewicz 57200a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); 57300a2430fSAndrzej Pietrasiewicz if (speed == USB_SPEED_SUPER) 57400a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); 57500a2430fSAndrzej Pietrasiewicz 57600a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); 57700a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); 57800a2430fSAndrzej Pietrasiewicz 57900a2430fSAndrzej Pietrasiewicz uvc_streaming_header = mem; 58000a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTORS(mem, dst, 58100a2430fSAndrzej Pietrasiewicz (const struct usb_descriptor_header**)uvc_streaming_cls); 58200a2430fSAndrzej Pietrasiewicz uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); 58300a2430fSAndrzej Pietrasiewicz uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; 58400a2430fSAndrzej Pietrasiewicz 58500a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); 58600a2430fSAndrzej Pietrasiewicz 58700a2430fSAndrzej Pietrasiewicz *dst = NULL; 58800a2430fSAndrzej Pietrasiewicz return hdr; 58900a2430fSAndrzej Pietrasiewicz } 59000a2430fSAndrzej Pietrasiewicz 5916d11ed76SAndrzej Pietrasiewicz static int 59200a2430fSAndrzej Pietrasiewicz uvc_function_bind(struct usb_configuration *c, struct usb_function *f) 59300a2430fSAndrzej Pietrasiewicz { 59400a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = c->cdev; 59500a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 59613443799SAndrzej Pietrasiewicz struct usb_string *us; 59700a2430fSAndrzej Pietrasiewicz unsigned int max_packet_mult; 59800a2430fSAndrzej Pietrasiewicz unsigned int max_packet_size; 59900a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 6006d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts; 60100a2430fSAndrzej Pietrasiewicz int ret = -EINVAL; 60200a2430fSAndrzej Pietrasiewicz 603dc0f755bSLaurent Pinchart uvcg_info(f, "%s()\n", __func__); 60400a2430fSAndrzej Pietrasiewicz 605bbea6de1SAndrzej Pietrasiewicz opts = fi_to_f_uvc_opts(f->fi); 606c5d337a3SLaurent Pinchart /* Sanity check the streaming endpoint module parameters. */ 6076d11ed76SAndrzej Pietrasiewicz opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); 6086d11ed76SAndrzej Pietrasiewicz opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); 6096d11ed76SAndrzej Pietrasiewicz opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); 6106d11ed76SAndrzej Pietrasiewicz 61116bb05d9SRoger Quadros /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */ 61216bb05d9SRoger Quadros if (opts->streaming_maxburst && 61316bb05d9SRoger Quadros (opts->streaming_maxpacket % 1024) != 0) { 61416bb05d9SRoger Quadros opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024); 615dc0f755bSLaurent Pinchart uvcg_info(f, "overriding streaming_maxpacket to %d\n", 61616bb05d9SRoger Quadros opts->streaming_maxpacket); 61716bb05d9SRoger Quadros } 61816bb05d9SRoger Quadros 619c5d337a3SLaurent Pinchart /* 620c5d337a3SLaurent Pinchart * Fill in the FS/HS/SS Video Streaming specific descriptors from the 6216d11ed76SAndrzej Pietrasiewicz * module parameters. 6226d11ed76SAndrzej Pietrasiewicz * 6236d11ed76SAndrzej Pietrasiewicz * NOTE: We assume that the user knows what they are doing and won't 6246d11ed76SAndrzej Pietrasiewicz * give parameters that their UDC doesn't support. 6256d11ed76SAndrzej Pietrasiewicz */ 6266d11ed76SAndrzej Pietrasiewicz if (opts->streaming_maxpacket <= 1024) { 6276d11ed76SAndrzej Pietrasiewicz max_packet_mult = 1; 6286d11ed76SAndrzej Pietrasiewicz max_packet_size = opts->streaming_maxpacket; 6296d11ed76SAndrzej Pietrasiewicz } else if (opts->streaming_maxpacket <= 2048) { 6306d11ed76SAndrzej Pietrasiewicz max_packet_mult = 2; 6316d11ed76SAndrzej Pietrasiewicz max_packet_size = opts->streaming_maxpacket / 2; 6326d11ed76SAndrzej Pietrasiewicz } else { 6336d11ed76SAndrzej Pietrasiewicz max_packet_mult = 3; 6346d11ed76SAndrzej Pietrasiewicz max_packet_size = opts->streaming_maxpacket / 3; 6356d11ed76SAndrzej Pietrasiewicz } 6366d11ed76SAndrzej Pietrasiewicz 6376d11ed76SAndrzej Pietrasiewicz uvc_fs_streaming_ep.wMaxPacketSize = 638e102609fSLaurent Pinchart cpu_to_le16(min(opts->streaming_maxpacket, 1023U)); 6396d11ed76SAndrzej Pietrasiewicz uvc_fs_streaming_ep.bInterval = opts->streaming_interval; 6406d11ed76SAndrzej Pietrasiewicz 641e102609fSLaurent Pinchart uvc_hs_streaming_ep.wMaxPacketSize = 642e102609fSLaurent Pinchart cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11)); 64326adde04SPawel Laszczak 64426adde04SPawel Laszczak /* A high-bandwidth endpoint must specify a bInterval value of 1 */ 64526adde04SPawel Laszczak if (max_packet_mult > 1) 64626adde04SPawel Laszczak uvc_hs_streaming_ep.bInterval = 1; 64726adde04SPawel Laszczak else 6486d11ed76SAndrzej Pietrasiewicz uvc_hs_streaming_ep.bInterval = opts->streaming_interval; 6496d11ed76SAndrzej Pietrasiewicz 650e102609fSLaurent Pinchart uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size); 6516d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_ep.bInterval = opts->streaming_interval; 6526d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; 6536d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; 6546d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_comp.wBytesPerInterval = 655e102609fSLaurent Pinchart cpu_to_le16(max_packet_size * max_packet_mult * 65609424c50SRoger Quadros (opts->streaming_maxburst + 1)); 65700a2430fSAndrzej Pietrasiewicz 65800a2430fSAndrzej Pietrasiewicz /* Allocate endpoints. */ 65900a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); 66000a2430fSAndrzej Pietrasiewicz if (!ep) { 661dc0f755bSLaurent Pinchart uvcg_info(f, "Unable to allocate control EP\n"); 66200a2430fSAndrzej Pietrasiewicz goto error; 66300a2430fSAndrzej Pietrasiewicz } 66400a2430fSAndrzej Pietrasiewicz uvc->control_ep = ep; 66500a2430fSAndrzej Pietrasiewicz 66600a2430fSAndrzej Pietrasiewicz if (gadget_is_superspeed(c->cdev->gadget)) 66700a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, 66800a2430fSAndrzej Pietrasiewicz &uvc_ss_streaming_comp); 66900a2430fSAndrzej Pietrasiewicz else if (gadget_is_dualspeed(cdev->gadget)) 67000a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); 67100a2430fSAndrzej Pietrasiewicz else 67200a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); 67300a2430fSAndrzej Pietrasiewicz 67400a2430fSAndrzej Pietrasiewicz if (!ep) { 675dc0f755bSLaurent Pinchart uvcg_info(f, "Unable to allocate streaming EP\n"); 67600a2430fSAndrzej Pietrasiewicz goto error; 67700a2430fSAndrzej Pietrasiewicz } 67800a2430fSAndrzej Pietrasiewicz uvc->video.ep = ep; 67900a2430fSAndrzej Pietrasiewicz 68000a2430fSAndrzej Pietrasiewicz uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 68100a2430fSAndrzej Pietrasiewicz uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 68200a2430fSAndrzej Pietrasiewicz uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; 68300a2430fSAndrzej Pietrasiewicz 684324e4f85SDan Vacura uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name; 68513443799SAndrzej Pietrasiewicz us = usb_gstrings_attach(cdev, uvc_function_strings, 68613443799SAndrzej Pietrasiewicz ARRAY_SIZE(uvc_en_us_strings)); 68713443799SAndrzej Pietrasiewicz if (IS_ERR(us)) { 68813443799SAndrzej Pietrasiewicz ret = PTR_ERR(us); 6896d11ed76SAndrzej Pietrasiewicz goto error; 69013443799SAndrzej Pietrasiewicz } 69113443799SAndrzej Pietrasiewicz uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id; 69213443799SAndrzej Pietrasiewicz uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id; 69313443799SAndrzej Pietrasiewicz ret = us[UVC_STRING_STREAMING_IDX].id; 6946d11ed76SAndrzej Pietrasiewicz uvc_streaming_intf_alt0.iInterface = ret; 6956d11ed76SAndrzej Pietrasiewicz uvc_streaming_intf_alt1.iInterface = ret; 6966d11ed76SAndrzej Pietrasiewicz 69700a2430fSAndrzej Pietrasiewicz /* Allocate interface IDs. */ 69800a2430fSAndrzej Pietrasiewicz if ((ret = usb_interface_id(c, f)) < 0) 69900a2430fSAndrzej Pietrasiewicz goto error; 70000a2430fSAndrzej Pietrasiewicz uvc_iad.bFirstInterface = ret; 70100a2430fSAndrzej Pietrasiewicz uvc_control_intf.bInterfaceNumber = ret; 70200a2430fSAndrzej Pietrasiewicz uvc->control_intf = ret; 703bf715448SLaurent Pinchart opts->control_interface = ret; 70400a2430fSAndrzej Pietrasiewicz 70500a2430fSAndrzej Pietrasiewicz if ((ret = usb_interface_id(c, f)) < 0) 70600a2430fSAndrzej Pietrasiewicz goto error; 70700a2430fSAndrzej Pietrasiewicz uvc_streaming_intf_alt0.bInterfaceNumber = ret; 70800a2430fSAndrzej Pietrasiewicz uvc_streaming_intf_alt1.bInterfaceNumber = ret; 70900a2430fSAndrzej Pietrasiewicz uvc->streaming_intf = ret; 710bf715448SLaurent Pinchart opts->streaming_interface = ret; 71100a2430fSAndrzej Pietrasiewicz 71200a2430fSAndrzej Pietrasiewicz /* Copy descriptors */ 71300a2430fSAndrzej Pietrasiewicz f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); 7146c25955eSAndrzej Pietrasiewicz if (IS_ERR(f->fs_descriptors)) { 7156c25955eSAndrzej Pietrasiewicz ret = PTR_ERR(f->fs_descriptors); 7166c25955eSAndrzej Pietrasiewicz f->fs_descriptors = NULL; 7176c25955eSAndrzej Pietrasiewicz goto error; 7186c25955eSAndrzej Pietrasiewicz } 7196c25955eSAndrzej Pietrasiewicz if (gadget_is_dualspeed(cdev->gadget)) { 72000a2430fSAndrzej Pietrasiewicz f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); 7216c25955eSAndrzej Pietrasiewicz if (IS_ERR(f->hs_descriptors)) { 7226c25955eSAndrzej Pietrasiewicz ret = PTR_ERR(f->hs_descriptors); 7236c25955eSAndrzej Pietrasiewicz f->hs_descriptors = NULL; 7246c25955eSAndrzej Pietrasiewicz goto error; 7256c25955eSAndrzej Pietrasiewicz } 7266c25955eSAndrzej Pietrasiewicz } 7276c25955eSAndrzej Pietrasiewicz if (gadget_is_superspeed(c->cdev->gadget)) { 72800a2430fSAndrzej Pietrasiewicz f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); 7296c25955eSAndrzej Pietrasiewicz if (IS_ERR(f->ss_descriptors)) { 7306c25955eSAndrzej Pietrasiewicz ret = PTR_ERR(f->ss_descriptors); 7316c25955eSAndrzej Pietrasiewicz f->ss_descriptors = NULL; 7326c25955eSAndrzej Pietrasiewicz goto error; 7336c25955eSAndrzej Pietrasiewicz } 7346c25955eSAndrzej Pietrasiewicz } 73500a2430fSAndrzej Pietrasiewicz 73600a2430fSAndrzej Pietrasiewicz /* Preallocate control endpoint request. */ 73700a2430fSAndrzej Pietrasiewicz uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); 73800a2430fSAndrzej Pietrasiewicz uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); 73900a2430fSAndrzej Pietrasiewicz if (uvc->control_req == NULL || uvc->control_buf == NULL) { 74000a2430fSAndrzej Pietrasiewicz ret = -ENOMEM; 74100a2430fSAndrzej Pietrasiewicz goto error; 74200a2430fSAndrzej Pietrasiewicz } 74300a2430fSAndrzej Pietrasiewicz 74400a2430fSAndrzej Pietrasiewicz uvc->control_req->buf = uvc->control_buf; 74500a2430fSAndrzej Pietrasiewicz uvc->control_req->complete = uvc_function_ep0_complete; 74600a2430fSAndrzej Pietrasiewicz uvc->control_req->context = uvc; 74700a2430fSAndrzej Pietrasiewicz 74800a2430fSAndrzej Pietrasiewicz if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { 749dc0f755bSLaurent Pinchart uvcg_err(f, "failed to register V4L2 device\n"); 75000a2430fSAndrzej Pietrasiewicz goto error; 75100a2430fSAndrzej Pietrasiewicz } 75200a2430fSAndrzej Pietrasiewicz 75300a2430fSAndrzej Pietrasiewicz /* Initialise video. */ 754dc0f755bSLaurent Pinchart ret = uvcg_video_init(&uvc->video, uvc); 75500a2430fSAndrzej Pietrasiewicz if (ret < 0) 756f0c48566SZqiang goto v4l2_error; 75700a2430fSAndrzej Pietrasiewicz 75800a2430fSAndrzej Pietrasiewicz /* Register a V4L2 device. */ 75900a2430fSAndrzej Pietrasiewicz ret = uvc_register_video(uvc); 76000a2430fSAndrzej Pietrasiewicz if (ret < 0) { 761dc0f755bSLaurent Pinchart uvcg_err(f, "failed to register video device\n"); 762f0c48566SZqiang goto v4l2_error; 76300a2430fSAndrzej Pietrasiewicz } 76400a2430fSAndrzej Pietrasiewicz 76500a2430fSAndrzej Pietrasiewicz return 0; 76600a2430fSAndrzej Pietrasiewicz 767f0c48566SZqiang v4l2_error: 76800a2430fSAndrzej Pietrasiewicz v4l2_device_unregister(&uvc->v4l2_dev); 769f0c48566SZqiang error: 770e7379857SAndrzej Pietrasiewicz if (uvc->control_req) 77100a2430fSAndrzej Pietrasiewicz usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 77200a2430fSAndrzej Pietrasiewicz kfree(uvc->control_buf); 77300a2430fSAndrzej Pietrasiewicz 77400a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 77500a2430fSAndrzej Pietrasiewicz return ret; 77600a2430fSAndrzej Pietrasiewicz } 77700a2430fSAndrzej Pietrasiewicz 77800a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 77900a2430fSAndrzej Pietrasiewicz * USB gadget function 78000a2430fSAndrzej Pietrasiewicz */ 78100a2430fSAndrzej Pietrasiewicz 7826d11ed76SAndrzej Pietrasiewicz static void uvc_free_inst(struct usb_function_instance *f) 7836d11ed76SAndrzej Pietrasiewicz { 784bbea6de1SAndrzej Pietrasiewicz struct f_uvc_opts *opts = fi_to_f_uvc_opts(f); 7856d11ed76SAndrzej Pietrasiewicz 78646919a23SAndrzej Pietrasiewicz mutex_destroy(&opts->lock); 7876d11ed76SAndrzej Pietrasiewicz kfree(opts); 7886d11ed76SAndrzej Pietrasiewicz } 7896d11ed76SAndrzej Pietrasiewicz 7906d11ed76SAndrzej Pietrasiewicz static struct usb_function_instance *uvc_alloc_inst(void) 7916d11ed76SAndrzej Pietrasiewicz { 7926d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts; 79346919a23SAndrzej Pietrasiewicz struct uvc_camera_terminal_descriptor *cd; 79446919a23SAndrzej Pietrasiewicz struct uvc_processing_unit_descriptor *pd; 79546919a23SAndrzej Pietrasiewicz struct uvc_output_terminal_descriptor *od; 79646919a23SAndrzej Pietrasiewicz struct uvc_color_matching_descriptor *md; 79746919a23SAndrzej Pietrasiewicz struct uvc_descriptor_header **ctl_cls; 798efbf0af7SLaurent Pinchart int ret; 7996d11ed76SAndrzej Pietrasiewicz 8006d11ed76SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 8016d11ed76SAndrzej Pietrasiewicz if (!opts) 8026d11ed76SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 8036d11ed76SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = uvc_free_inst; 80446919a23SAndrzej Pietrasiewicz mutex_init(&opts->lock); 8056d11ed76SAndrzej Pietrasiewicz 80646919a23SAndrzej Pietrasiewicz cd = &opts->uvc_camera_terminal; 80746919a23SAndrzej Pietrasiewicz cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3); 80846919a23SAndrzej Pietrasiewicz cd->bDescriptorType = USB_DT_CS_INTERFACE; 80946919a23SAndrzej Pietrasiewicz cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL; 81046919a23SAndrzej Pietrasiewicz cd->bTerminalID = 1; 81146919a23SAndrzej Pietrasiewicz cd->wTerminalType = cpu_to_le16(0x0201); 81246919a23SAndrzej Pietrasiewicz cd->bAssocTerminal = 0; 81346919a23SAndrzej Pietrasiewicz cd->iTerminal = 0; 81446919a23SAndrzej Pietrasiewicz cd->wObjectiveFocalLengthMin = cpu_to_le16(0); 81546919a23SAndrzej Pietrasiewicz cd->wObjectiveFocalLengthMax = cpu_to_le16(0); 81646919a23SAndrzej Pietrasiewicz cd->wOcularFocalLength = cpu_to_le16(0); 81746919a23SAndrzej Pietrasiewicz cd->bControlSize = 3; 81846919a23SAndrzej Pietrasiewicz cd->bmControls[0] = 2; 81946919a23SAndrzej Pietrasiewicz cd->bmControls[1] = 0; 82046919a23SAndrzej Pietrasiewicz cd->bmControls[2] = 0; 82146919a23SAndrzej Pietrasiewicz 82246919a23SAndrzej Pietrasiewicz pd = &opts->uvc_processing; 82346919a23SAndrzej Pietrasiewicz pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2); 82446919a23SAndrzej Pietrasiewicz pd->bDescriptorType = USB_DT_CS_INTERFACE; 82546919a23SAndrzej Pietrasiewicz pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT; 82646919a23SAndrzej Pietrasiewicz pd->bUnitID = 2; 82746919a23SAndrzej Pietrasiewicz pd->bSourceID = 1; 82846919a23SAndrzej Pietrasiewicz pd->wMaxMultiplier = cpu_to_le16(16*1024); 82946919a23SAndrzej Pietrasiewicz pd->bControlSize = 2; 83046919a23SAndrzej Pietrasiewicz pd->bmControls[0] = 1; 83146919a23SAndrzej Pietrasiewicz pd->bmControls[1] = 0; 83246919a23SAndrzej Pietrasiewicz pd->iProcessing = 0; 8336a154ec9SPawel Laszczak pd->bmVideoStandards = 0; 83446919a23SAndrzej Pietrasiewicz 83546919a23SAndrzej Pietrasiewicz od = &opts->uvc_output_terminal; 83646919a23SAndrzej Pietrasiewicz od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE; 83746919a23SAndrzej Pietrasiewicz od->bDescriptorType = USB_DT_CS_INTERFACE; 83846919a23SAndrzej Pietrasiewicz od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL; 83946919a23SAndrzej Pietrasiewicz od->bTerminalID = 3; 84046919a23SAndrzej Pietrasiewicz od->wTerminalType = cpu_to_le16(0x0101); 84146919a23SAndrzej Pietrasiewicz od->bAssocTerminal = 0; 84246919a23SAndrzej Pietrasiewicz od->bSourceID = 2; 84346919a23SAndrzej Pietrasiewicz od->iTerminal = 0; 84446919a23SAndrzej Pietrasiewicz 84546919a23SAndrzej Pietrasiewicz md = &opts->uvc_color_matching; 84646919a23SAndrzej Pietrasiewicz md->bLength = UVC_DT_COLOR_MATCHING_SIZE; 84746919a23SAndrzej Pietrasiewicz md->bDescriptorType = USB_DT_CS_INTERFACE; 84846919a23SAndrzej Pietrasiewicz md->bDescriptorSubType = UVC_VS_COLORFORMAT; 84946919a23SAndrzej Pietrasiewicz md->bColorPrimaries = 1; 85046919a23SAndrzej Pietrasiewicz md->bTransferCharacteristics = 1; 85146919a23SAndrzej Pietrasiewicz md->bMatrixCoefficients = 4; 85246919a23SAndrzej Pietrasiewicz 85346919a23SAndrzej Pietrasiewicz /* Prepare fs control class descriptors for configfs-based gadgets */ 85446919a23SAndrzej Pietrasiewicz ctl_cls = opts->uvc_fs_control_cls; 85546919a23SAndrzej Pietrasiewicz ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ 85646919a23SAndrzej Pietrasiewicz ctl_cls[1] = (struct uvc_descriptor_header *)cd; 85746919a23SAndrzej Pietrasiewicz ctl_cls[2] = (struct uvc_descriptor_header *)pd; 85846919a23SAndrzej Pietrasiewicz ctl_cls[3] = (struct uvc_descriptor_header *)od; 85946919a23SAndrzej Pietrasiewicz ctl_cls[4] = NULL; /* NULL-terminate */ 86046919a23SAndrzej Pietrasiewicz opts->fs_control = 86146919a23SAndrzej Pietrasiewicz (const struct uvc_descriptor_header * const *)ctl_cls; 86246919a23SAndrzej Pietrasiewicz 86346919a23SAndrzej Pietrasiewicz /* Prepare hs control class descriptors for configfs-based gadgets */ 86446919a23SAndrzej Pietrasiewicz ctl_cls = opts->uvc_ss_control_cls; 86546919a23SAndrzej Pietrasiewicz ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ 86646919a23SAndrzej Pietrasiewicz ctl_cls[1] = (struct uvc_descriptor_header *)cd; 86746919a23SAndrzej Pietrasiewicz ctl_cls[2] = (struct uvc_descriptor_header *)pd; 86846919a23SAndrzej Pietrasiewicz ctl_cls[3] = (struct uvc_descriptor_header *)od; 86946919a23SAndrzej Pietrasiewicz ctl_cls[4] = NULL; /* NULL-terminate */ 87046919a23SAndrzej Pietrasiewicz opts->ss_control = 87146919a23SAndrzej Pietrasiewicz (const struct uvc_descriptor_header * const *)ctl_cls; 87246919a23SAndrzej Pietrasiewicz 87346919a23SAndrzej Pietrasiewicz opts->streaming_interval = 1; 87446919a23SAndrzej Pietrasiewicz opts->streaming_maxpacket = 1024; 875324e4f85SDan Vacura snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera"); 87646919a23SAndrzej Pietrasiewicz 877efbf0af7SLaurent Pinchart ret = uvcg_attach_configfs(opts); 878efbf0af7SLaurent Pinchart if (ret < 0) { 879efbf0af7SLaurent Pinchart kfree(opts); 880efbf0af7SLaurent Pinchart return ERR_PTR(ret); 881efbf0af7SLaurent Pinchart } 882efbf0af7SLaurent Pinchart 8836d11ed76SAndrzej Pietrasiewicz return &opts->func_inst; 8846d11ed76SAndrzej Pietrasiewicz } 8856d11ed76SAndrzej Pietrasiewicz 8866d11ed76SAndrzej Pietrasiewicz static void uvc_free(struct usb_function *f) 8876d11ed76SAndrzej Pietrasiewicz { 8886d11ed76SAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 88946919a23SAndrzej Pietrasiewicz struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts, 89046919a23SAndrzej Pietrasiewicz func_inst); 891*588b9e85SMichael Grzeschik config_item_put(&uvc->header->item); 89246919a23SAndrzej Pietrasiewicz --opts->refcnt; 8936d11ed76SAndrzej Pietrasiewicz kfree(uvc); 8946d11ed76SAndrzej Pietrasiewicz } 8956d11ed76SAndrzej Pietrasiewicz 896e6bab2b6SMichael Tretter static void uvc_function_unbind(struct usb_configuration *c, 897e6bab2b6SMichael Tretter struct usb_function *f) 8986d11ed76SAndrzej Pietrasiewicz { 8996d11ed76SAndrzej Pietrasiewicz struct usb_composite_dev *cdev = c->cdev; 9006d11ed76SAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 9019b91a652SMichael Grzeschik struct uvc_video *video = &uvc->video; 902b81ac439SDan Vacura long wait_ret = 1; 9036d11ed76SAndrzej Pietrasiewicz 904e6bab2b6SMichael Tretter uvcg_info(f, "%s()\n", __func__); 9056d11ed76SAndrzej Pietrasiewicz 9069b91a652SMichael Grzeschik if (video->async_wq) 9079b91a652SMichael Grzeschik destroy_workqueue(video->async_wq); 9089b91a652SMichael Grzeschik 909c5d337a3SLaurent Pinchart /* 910c5d337a3SLaurent Pinchart * If we know we're connected via v4l2, then there should be a cleanup 911b81ac439SDan Vacura * of the device from userspace either via UVC_EVENT_DISCONNECT or 912b81ac439SDan Vacura * though the video device removal uevent. Allow some time for the 913b81ac439SDan Vacura * application to close out before things get deleted. 914b81ac439SDan Vacura */ 915b81ac439SDan Vacura if (uvc->func_connected) { 916b81ac439SDan Vacura uvcg_dbg(f, "waiting for clean disconnect\n"); 917b81ac439SDan Vacura wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue, 918b81ac439SDan Vacura uvc->func_connected == false, msecs_to_jiffies(500)); 919b81ac439SDan Vacura uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret); 920b81ac439SDan Vacura } 921b81ac439SDan Vacura 922d7af78b9SKieran Bingham device_remove_file(&uvc->vdev.dev, &dev_attr_function_name); 923dbe98b30SHans Verkuil video_unregister_device(&uvc->vdev); 9246d11ed76SAndrzej Pietrasiewicz v4l2_device_unregister(&uvc->v4l2_dev); 9256d11ed76SAndrzej Pietrasiewicz 926b81ac439SDan Vacura if (uvc->func_connected) { 927c5d337a3SLaurent Pinchart /* 928c5d337a3SLaurent Pinchart * Wait for the release to occur to ensure there are no longer any 929b81ac439SDan Vacura * pending operations that may cause panics when resources are cleaned 930b81ac439SDan Vacura * up. 931b81ac439SDan Vacura */ 932b81ac439SDan Vacura uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__); 933b81ac439SDan Vacura wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue, 934b81ac439SDan Vacura uvc->func_connected == false, msecs_to_jiffies(1000)); 935b81ac439SDan Vacura uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret); 936b81ac439SDan Vacura } 937b81ac439SDan Vacura 9386d11ed76SAndrzej Pietrasiewicz usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 9396d11ed76SAndrzej Pietrasiewicz kfree(uvc->control_buf); 9406d11ed76SAndrzej Pietrasiewicz 9416d11ed76SAndrzej Pietrasiewicz usb_free_all_descriptors(f); 9426d11ed76SAndrzej Pietrasiewicz } 9436d11ed76SAndrzej Pietrasiewicz 9444a6698b8SFengguang Wu static struct usb_function *uvc_alloc(struct usb_function_instance *fi) 9456d11ed76SAndrzej Pietrasiewicz { 9466d11ed76SAndrzej Pietrasiewicz struct uvc_device *uvc; 9476d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts; 94846919a23SAndrzej Pietrasiewicz struct uvc_descriptor_header **strm_cls; 949*588b9e85SMichael Grzeschik struct config_item *streaming, *header, *h; 9506d11ed76SAndrzej Pietrasiewicz 9516d11ed76SAndrzej Pietrasiewicz uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); 9526d11ed76SAndrzej Pietrasiewicz if (uvc == NULL) 9536d11ed76SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 9546d11ed76SAndrzej Pietrasiewicz 955d8e96c4bSHans Verkuil mutex_init(&uvc->video.mutex); 9566d11ed76SAndrzej Pietrasiewicz uvc->state = UVC_STATE_DISCONNECTED; 957b81ac439SDan Vacura init_waitqueue_head(&uvc->func_connected_queue); 958bbea6de1SAndrzej Pietrasiewicz opts = fi_to_f_uvc_opts(fi); 9596d11ed76SAndrzej Pietrasiewicz 96046919a23SAndrzej Pietrasiewicz mutex_lock(&opts->lock); 96146919a23SAndrzej Pietrasiewicz if (opts->uvc_fs_streaming_cls) { 96246919a23SAndrzej Pietrasiewicz strm_cls = opts->uvc_fs_streaming_cls; 96346919a23SAndrzej Pietrasiewicz opts->fs_streaming = 96446919a23SAndrzej Pietrasiewicz (const struct uvc_descriptor_header * const *)strm_cls; 96546919a23SAndrzej Pietrasiewicz } 96646919a23SAndrzej Pietrasiewicz if (opts->uvc_hs_streaming_cls) { 96746919a23SAndrzej Pietrasiewicz strm_cls = opts->uvc_hs_streaming_cls; 96846919a23SAndrzej Pietrasiewicz opts->hs_streaming = 96946919a23SAndrzej Pietrasiewicz (const struct uvc_descriptor_header * const *)strm_cls; 97046919a23SAndrzej Pietrasiewicz } 97146919a23SAndrzej Pietrasiewicz if (opts->uvc_ss_streaming_cls) { 97246919a23SAndrzej Pietrasiewicz strm_cls = opts->uvc_ss_streaming_cls; 97346919a23SAndrzej Pietrasiewicz opts->ss_streaming = 97446919a23SAndrzej Pietrasiewicz (const struct uvc_descriptor_header * const *)strm_cls; 97546919a23SAndrzej Pietrasiewicz } 97646919a23SAndrzej Pietrasiewicz 9776d11ed76SAndrzej Pietrasiewicz uvc->desc.fs_control = opts->fs_control; 9786d11ed76SAndrzej Pietrasiewicz uvc->desc.ss_control = opts->ss_control; 9796d11ed76SAndrzej Pietrasiewicz uvc->desc.fs_streaming = opts->fs_streaming; 9806d11ed76SAndrzej Pietrasiewicz uvc->desc.hs_streaming = opts->hs_streaming; 9816d11ed76SAndrzej Pietrasiewicz uvc->desc.ss_streaming = opts->ss_streaming; 982*588b9e85SMichael Grzeschik 983*588b9e85SMichael Grzeschik streaming = config_group_find_item(&opts->func_inst.group, "streaming"); 984*588b9e85SMichael Grzeschik if (!streaming) 985*588b9e85SMichael Grzeschik goto err_config; 986*588b9e85SMichael Grzeschik 987*588b9e85SMichael Grzeschik header = config_group_find_item(to_config_group(streaming), "header"); 988*588b9e85SMichael Grzeschik config_item_put(streaming); 989*588b9e85SMichael Grzeschik if (!header) 990*588b9e85SMichael Grzeschik goto err_config; 991*588b9e85SMichael Grzeschik 992*588b9e85SMichael Grzeschik h = config_group_find_item(to_config_group(header), "h"); 993*588b9e85SMichael Grzeschik config_item_put(header); 994*588b9e85SMichael Grzeschik if (!h) 995*588b9e85SMichael Grzeschik goto err_config; 996*588b9e85SMichael Grzeschik 997*588b9e85SMichael Grzeschik uvc->header = to_uvcg_streaming_header(h); 998*588b9e85SMichael Grzeschik config_item_put(h); 999*588b9e85SMichael Grzeschik if (!uvc->header->linked) { 1000*588b9e85SMichael Grzeschik mutex_unlock(&opts->lock); 1001*588b9e85SMichael Grzeschik kfree(uvc); 1002*588b9e85SMichael Grzeschik return ERR_PTR(-EBUSY); 1003*588b9e85SMichael Grzeschik } 1004*588b9e85SMichael Grzeschik 100546919a23SAndrzej Pietrasiewicz ++opts->refcnt; 100646919a23SAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 10076d11ed76SAndrzej Pietrasiewicz 10086d11ed76SAndrzej Pietrasiewicz /* Register the function. */ 10096d11ed76SAndrzej Pietrasiewicz uvc->func.name = "uvc"; 10106d11ed76SAndrzej Pietrasiewicz uvc->func.bind = uvc_function_bind; 1011e6bab2b6SMichael Tretter uvc->func.unbind = uvc_function_unbind; 10126d11ed76SAndrzej Pietrasiewicz uvc->func.get_alt = uvc_function_get_alt; 10136d11ed76SAndrzej Pietrasiewicz uvc->func.set_alt = uvc_function_set_alt; 10146d11ed76SAndrzej Pietrasiewicz uvc->func.disable = uvc_function_disable; 10156d11ed76SAndrzej Pietrasiewicz uvc->func.setup = uvc_function_setup; 10166d11ed76SAndrzej Pietrasiewicz uvc->func.free_func = uvc_free; 1017f277bf27SRobert Baldyga uvc->func.bind_deactivated = true; 10186d11ed76SAndrzej Pietrasiewicz 10196d11ed76SAndrzej Pietrasiewicz return &uvc->func; 1020*588b9e85SMichael Grzeschik 1021*588b9e85SMichael Grzeschik err_config: 1022*588b9e85SMichael Grzeschik mutex_unlock(&opts->lock); 1023*588b9e85SMichael Grzeschik kfree(uvc); 1024*588b9e85SMichael Grzeschik return ERR_PTR(-ENOENT); 10256d11ed76SAndrzej Pietrasiewicz } 10266d11ed76SAndrzej Pietrasiewicz 10276d11ed76SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); 10286d11ed76SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 10296d11ed76SAndrzej Pietrasiewicz MODULE_AUTHOR("Laurent Pinchart"); 1030