100a2430fSAndrzej Pietrasiewicz /* 200a2430fSAndrzej Pietrasiewicz * uvc_gadget.c -- USB Video Class Gadget driver 300a2430fSAndrzej Pietrasiewicz * 400a2430fSAndrzej Pietrasiewicz * Copyright (C) 2009-2010 500a2430fSAndrzej Pietrasiewicz * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 600a2430fSAndrzej Pietrasiewicz * 700a2430fSAndrzej Pietrasiewicz * This program is free software; you can redistribute it and/or modify 800a2430fSAndrzej Pietrasiewicz * it under the terms of the GNU General Public License as published by 900a2430fSAndrzej Pietrasiewicz * the Free Software Foundation; either version 2 of the License, or 1000a2430fSAndrzej Pietrasiewicz * (at your option) any later version. 1100a2430fSAndrzej Pietrasiewicz */ 1200a2430fSAndrzej Pietrasiewicz 1300a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h> 146d11ed76SAndrzej Pietrasiewicz #include <linux/module.h> 1500a2430fSAndrzej Pietrasiewicz #include <linux/device.h> 1600a2430fSAndrzej Pietrasiewicz #include <linux/errno.h> 1700a2430fSAndrzej Pietrasiewicz #include <linux/fs.h> 1800a2430fSAndrzej Pietrasiewicz #include <linux/list.h> 1900a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h> 2000a2430fSAndrzej Pietrasiewicz #include <linux/string.h> 2100a2430fSAndrzej Pietrasiewicz #include <linux/usb/ch9.h> 2200a2430fSAndrzej Pietrasiewicz #include <linux/usb/gadget.h> 2300a2430fSAndrzej Pietrasiewicz #include <linux/usb/video.h> 2400a2430fSAndrzej Pietrasiewicz #include <linux/vmalloc.h> 2500a2430fSAndrzej Pietrasiewicz #include <linux/wait.h> 2600a2430fSAndrzej Pietrasiewicz 2700a2430fSAndrzej Pietrasiewicz #include <media/v4l2-dev.h> 2800a2430fSAndrzej Pietrasiewicz #include <media/v4l2-event.h> 2900a2430fSAndrzej Pietrasiewicz 3000a2430fSAndrzej Pietrasiewicz #include "uvc.h" 313a83c16eSAndrzej Pietrasiewicz #include "uvc_v4l2.h" 323a83c16eSAndrzej Pietrasiewicz #include "uvc_video.h" 336d11ed76SAndrzej Pietrasiewicz #include "u_uvc.h" 3400a2430fSAndrzej Pietrasiewicz 3500a2430fSAndrzej Pietrasiewicz unsigned int uvc_gadget_trace_param; 3600a2430fSAndrzej Pietrasiewicz 3700a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 3800a2430fSAndrzej Pietrasiewicz * Function descriptors 3900a2430fSAndrzej Pietrasiewicz */ 4000a2430fSAndrzej Pietrasiewicz 4100a2430fSAndrzej Pietrasiewicz /* string IDs are assigned dynamically */ 4200a2430fSAndrzej Pietrasiewicz 4300a2430fSAndrzej Pietrasiewicz #define UVC_STRING_CONTROL_IDX 0 4400a2430fSAndrzej Pietrasiewicz #define UVC_STRING_STREAMING_IDX 1 4500a2430fSAndrzej Pietrasiewicz 4600a2430fSAndrzej Pietrasiewicz static struct usb_string uvc_en_us_strings[] = { 4700a2430fSAndrzej Pietrasiewicz [UVC_STRING_CONTROL_IDX].s = "UVC Camera", 4800a2430fSAndrzej Pietrasiewicz [UVC_STRING_STREAMING_IDX].s = "Video Streaming", 4900a2430fSAndrzej Pietrasiewicz { } 5000a2430fSAndrzej Pietrasiewicz }; 5100a2430fSAndrzej Pietrasiewicz 5200a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings uvc_stringtab = { 5300a2430fSAndrzej Pietrasiewicz .language = 0x0409, /* en-us */ 5400a2430fSAndrzej Pietrasiewicz .strings = uvc_en_us_strings, 5500a2430fSAndrzej Pietrasiewicz }; 5600a2430fSAndrzej Pietrasiewicz 5700a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings *uvc_function_strings[] = { 5800a2430fSAndrzej Pietrasiewicz &uvc_stringtab, 5900a2430fSAndrzej Pietrasiewicz NULL, 6000a2430fSAndrzej Pietrasiewicz }; 6100a2430fSAndrzej Pietrasiewicz 6200a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_CONTROL 0 6300a2430fSAndrzej Pietrasiewicz #define UVC_INTF_VIDEO_STREAMING 1 6400a2430fSAndrzej Pietrasiewicz 6500a2430fSAndrzej Pietrasiewicz #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ 6600a2430fSAndrzej Pietrasiewicz 676d11ed76SAndrzej Pietrasiewicz static struct usb_interface_assoc_descriptor uvc_iad = { 6800a2430fSAndrzej Pietrasiewicz .bLength = sizeof(uvc_iad), 6900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 7000a2430fSAndrzej Pietrasiewicz .bFirstInterface = 0, 7100a2430fSAndrzej Pietrasiewicz .bInterfaceCount = 2, 7200a2430fSAndrzej Pietrasiewicz .bFunctionClass = USB_CLASS_VIDEO, 7300a2430fSAndrzej Pietrasiewicz .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, 7400a2430fSAndrzej Pietrasiewicz .bFunctionProtocol = 0x00, 7500a2430fSAndrzej Pietrasiewicz .iFunction = 0, 7600a2430fSAndrzej Pietrasiewicz }; 7700a2430fSAndrzej Pietrasiewicz 786d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_control_intf = { 7900a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_INTERFACE_SIZE, 8000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 8100a2430fSAndrzej Pietrasiewicz .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, 8200a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8300a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 1, 8400a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_VIDEO, 8500a2430fSAndrzej Pietrasiewicz .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, 8600a2430fSAndrzej Pietrasiewicz .bInterfaceProtocol = 0x00, 8700a2430fSAndrzej Pietrasiewicz .iInterface = 0, 8800a2430fSAndrzej Pietrasiewicz }; 8900a2430fSAndrzej Pietrasiewicz 906d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_control_ep = { 9100a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 9200a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 9300a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 9400a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 9500a2430fSAndrzej Pietrasiewicz .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 9600a2430fSAndrzej Pietrasiewicz .bInterval = 8, 9700a2430fSAndrzej Pietrasiewicz }; 9800a2430fSAndrzej Pietrasiewicz 996d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = { 10000a2430fSAndrzej Pietrasiewicz .bLength = sizeof(uvc_ss_control_comp), 10100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 10200a2430fSAndrzej Pietrasiewicz /* The following 3 values can be tweaked if necessary. */ 10300a2430fSAndrzej Pietrasiewicz .bMaxBurst = 0, 10400a2430fSAndrzej Pietrasiewicz .bmAttributes = 0, 10500a2430fSAndrzej Pietrasiewicz .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 10600a2430fSAndrzej Pietrasiewicz }; 10700a2430fSAndrzej Pietrasiewicz 1086d11ed76SAndrzej Pietrasiewicz static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = { 10900a2430fSAndrzej Pietrasiewicz .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, 11000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_CS_ENDPOINT, 11100a2430fSAndrzej Pietrasiewicz .bDescriptorSubType = UVC_EP_INTERRUPT, 11200a2430fSAndrzej Pietrasiewicz .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 11300a2430fSAndrzej Pietrasiewicz }; 11400a2430fSAndrzej Pietrasiewicz 1156d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { 11600a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_INTERFACE_SIZE, 11700a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 11800a2430fSAndrzej Pietrasiewicz .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, 11900a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 12000a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 0, 12100a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_VIDEO, 12200a2430fSAndrzej Pietrasiewicz .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 12300a2430fSAndrzej Pietrasiewicz .bInterfaceProtocol = 0x00, 12400a2430fSAndrzej Pietrasiewicz .iInterface = 0, 12500a2430fSAndrzej Pietrasiewicz }; 12600a2430fSAndrzej Pietrasiewicz 1276d11ed76SAndrzej Pietrasiewicz static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { 12800a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_INTERFACE_SIZE, 12900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 13000a2430fSAndrzej Pietrasiewicz .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, 13100a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 1, 13200a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 1, 13300a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_VIDEO, 13400a2430fSAndrzej Pietrasiewicz .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 13500a2430fSAndrzej Pietrasiewicz .bInterfaceProtocol = 0x00, 13600a2430fSAndrzej Pietrasiewicz .iInterface = 0, 13700a2430fSAndrzej Pietrasiewicz }; 13800a2430fSAndrzej Pietrasiewicz 1396d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { 14000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 14100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 14200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 14300a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 14400a2430fSAndrzej Pietrasiewicz | USB_ENDPOINT_XFER_ISOC, 14500a2430fSAndrzej Pietrasiewicz /* 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, 15600a2430fSAndrzej Pietrasiewicz /* The wMaxPacketSize and bInterval values will be initialized from 15700a2430fSAndrzej Pietrasiewicz * module parameters. 15800a2430fSAndrzej Pietrasiewicz */ 15900a2430fSAndrzej Pietrasiewicz }; 16000a2430fSAndrzej Pietrasiewicz 1616d11ed76SAndrzej Pietrasiewicz static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { 16200a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 16300a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 16400a2430fSAndrzej Pietrasiewicz 16500a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 16600a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 16700a2430fSAndrzej Pietrasiewicz | USB_ENDPOINT_XFER_ISOC, 16800a2430fSAndrzej Pietrasiewicz /* The wMaxPacketSize and bInterval values will be initialized from 16900a2430fSAndrzej Pietrasiewicz * module parameters. 17000a2430fSAndrzej Pietrasiewicz */ 17100a2430fSAndrzej Pietrasiewicz }; 17200a2430fSAndrzej Pietrasiewicz 1736d11ed76SAndrzej Pietrasiewicz static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { 17400a2430fSAndrzej Pietrasiewicz .bLength = sizeof(uvc_ss_streaming_comp), 17500a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 17600a2430fSAndrzej Pietrasiewicz /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be 17700a2430fSAndrzej Pietrasiewicz * initialized from module parameters. 17800a2430fSAndrzej Pietrasiewicz */ 17900a2430fSAndrzej Pietrasiewicz }; 18000a2430fSAndrzej Pietrasiewicz 18100a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_fs_streaming[] = { 18200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 18300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_fs_streaming_ep, 18400a2430fSAndrzej Pietrasiewicz NULL, 18500a2430fSAndrzej Pietrasiewicz }; 18600a2430fSAndrzej Pietrasiewicz 18700a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_hs_streaming[] = { 18800a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 18900a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_hs_streaming_ep, 19000a2430fSAndrzej Pietrasiewicz NULL, 19100a2430fSAndrzej Pietrasiewicz }; 19200a2430fSAndrzej Pietrasiewicz 19300a2430fSAndrzej Pietrasiewicz static const struct usb_descriptor_header * const uvc_ss_streaming[] = { 19400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 19500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_ss_streaming_ep, 19600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *) &uvc_ss_streaming_comp, 19700a2430fSAndrzej Pietrasiewicz NULL, 19800a2430fSAndrzej Pietrasiewicz }; 19900a2430fSAndrzej Pietrasiewicz 2006d11ed76SAndrzej Pietrasiewicz void uvc_set_trace_param(unsigned int trace) 2016d11ed76SAndrzej Pietrasiewicz { 2026d11ed76SAndrzej Pietrasiewicz uvc_gadget_trace_param = trace; 2036d11ed76SAndrzej Pietrasiewicz } 2046d11ed76SAndrzej Pietrasiewicz EXPORT_SYMBOL(uvc_set_trace_param); 2056d11ed76SAndrzej Pietrasiewicz 20600a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 20700a2430fSAndrzej Pietrasiewicz * Control requests 20800a2430fSAndrzej Pietrasiewicz */ 20900a2430fSAndrzej Pietrasiewicz 21000a2430fSAndrzej Pietrasiewicz static void 21100a2430fSAndrzej Pietrasiewicz uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) 21200a2430fSAndrzej Pietrasiewicz { 21300a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = req->context; 21400a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 21500a2430fSAndrzej Pietrasiewicz struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 21600a2430fSAndrzej Pietrasiewicz 21700a2430fSAndrzej Pietrasiewicz if (uvc->event_setup_out) { 21800a2430fSAndrzej Pietrasiewicz uvc->event_setup_out = 0; 21900a2430fSAndrzej Pietrasiewicz 22000a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 22100a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_DATA; 22200a2430fSAndrzej Pietrasiewicz uvc_event->data.length = req->actual; 22300a2430fSAndrzej Pietrasiewicz memcpy(&uvc_event->data.data, req->buf, req->actual); 22400a2430fSAndrzej Pietrasiewicz v4l2_event_queue(uvc->vdev, &v4l2_event); 22500a2430fSAndrzej Pietrasiewicz } 22600a2430fSAndrzej Pietrasiewicz } 22700a2430fSAndrzej Pietrasiewicz 22800a2430fSAndrzej Pietrasiewicz static int 22900a2430fSAndrzej Pietrasiewicz uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 23000a2430fSAndrzej Pietrasiewicz { 23100a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 23200a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 23300a2430fSAndrzej Pietrasiewicz struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 23400a2430fSAndrzej Pietrasiewicz 23500a2430fSAndrzej Pietrasiewicz /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", 23600a2430fSAndrzej Pietrasiewicz * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), 23700a2430fSAndrzej Pietrasiewicz * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); 23800a2430fSAndrzej Pietrasiewicz */ 23900a2430fSAndrzej Pietrasiewicz 24000a2430fSAndrzej Pietrasiewicz if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { 24100a2430fSAndrzej Pietrasiewicz INFO(f->config->cdev, "invalid request type\n"); 24200a2430fSAndrzej Pietrasiewicz return -EINVAL; 24300a2430fSAndrzej Pietrasiewicz } 24400a2430fSAndrzej Pietrasiewicz 24500a2430fSAndrzej Pietrasiewicz /* Stall too big requests. */ 24600a2430fSAndrzej Pietrasiewicz if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) 24700a2430fSAndrzej Pietrasiewicz return -EINVAL; 24800a2430fSAndrzej Pietrasiewicz 24926a029f2SLaurent Pinchart /* Tell the complete callback to generate an event for the next request 25026a029f2SLaurent Pinchart * that will be enqueued by UVCIOC_SEND_RESPONSE. 25126a029f2SLaurent Pinchart */ 25226a029f2SLaurent Pinchart uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); 25326a029f2SLaurent Pinchart uvc->event_length = le16_to_cpu(ctrl->wLength); 25426a029f2SLaurent Pinchart 25500a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 25600a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_SETUP; 25700a2430fSAndrzej Pietrasiewicz memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); 25800a2430fSAndrzej Pietrasiewicz v4l2_event_queue(uvc->vdev, &v4l2_event); 25900a2430fSAndrzej Pietrasiewicz 26000a2430fSAndrzej Pietrasiewicz return 0; 26100a2430fSAndrzej Pietrasiewicz } 26200a2430fSAndrzej Pietrasiewicz 26300a2430fSAndrzej Pietrasiewicz void uvc_function_setup_continue(struct uvc_device *uvc) 26400a2430fSAndrzej Pietrasiewicz { 26500a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = uvc->func.config->cdev; 26600a2430fSAndrzej Pietrasiewicz 26700a2430fSAndrzej Pietrasiewicz usb_composite_setup_continue(cdev); 26800a2430fSAndrzej Pietrasiewicz } 26900a2430fSAndrzej Pietrasiewicz 27000a2430fSAndrzej Pietrasiewicz static int 27100a2430fSAndrzej Pietrasiewicz uvc_function_get_alt(struct usb_function *f, unsigned interface) 27200a2430fSAndrzej Pietrasiewicz { 27300a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 27400a2430fSAndrzej Pietrasiewicz 27500a2430fSAndrzej Pietrasiewicz INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); 27600a2430fSAndrzej Pietrasiewicz 27700a2430fSAndrzej Pietrasiewicz if (interface == uvc->control_intf) 27800a2430fSAndrzej Pietrasiewicz return 0; 27900a2430fSAndrzej Pietrasiewicz else if (interface != uvc->streaming_intf) 28000a2430fSAndrzej Pietrasiewicz return -EINVAL; 28100a2430fSAndrzej Pietrasiewicz else 282e975be28SFelipe Balbi return uvc->video.ep->driver_data ? 1 : 0; 28300a2430fSAndrzej Pietrasiewicz } 28400a2430fSAndrzej Pietrasiewicz 28500a2430fSAndrzej Pietrasiewicz static int 28600a2430fSAndrzej Pietrasiewicz uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) 28700a2430fSAndrzej Pietrasiewicz { 28800a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 289c92bae75SFelipe Balbi struct usb_composite_dev *cdev = f->config->cdev; 29000a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 29100a2430fSAndrzej Pietrasiewicz struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 29200a2430fSAndrzej Pietrasiewicz int ret; 29300a2430fSAndrzej Pietrasiewicz 294c92bae75SFelipe Balbi INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); 29500a2430fSAndrzej Pietrasiewicz 29600a2430fSAndrzej Pietrasiewicz if (interface == uvc->control_intf) { 29700a2430fSAndrzej Pietrasiewicz if (alt) 29800a2430fSAndrzej Pietrasiewicz return -EINVAL; 29900a2430fSAndrzej Pietrasiewicz 30062e37078SFelipe Balbi if (uvc->control_ep->driver_data) { 30162e37078SFelipe Balbi INFO(cdev, "reset UVC Control\n"); 30262e37078SFelipe Balbi usb_ep_disable(uvc->control_ep); 30362e37078SFelipe Balbi uvc->control_ep->driver_data = NULL; 30462e37078SFelipe Balbi } 30562e37078SFelipe Balbi 30662e37078SFelipe Balbi if (!uvc->control_ep->desc) 30762e37078SFelipe Balbi if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep)) 30862e37078SFelipe Balbi return -EINVAL; 30962e37078SFelipe Balbi 31062e37078SFelipe Balbi usb_ep_enable(uvc->control_ep); 31162e37078SFelipe Balbi uvc->control_ep->driver_data = uvc; 31262e37078SFelipe Balbi 31300a2430fSAndrzej Pietrasiewicz if (uvc->state == UVC_STATE_DISCONNECTED) { 31400a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 31500a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_CONNECT; 316c92bae75SFelipe Balbi uvc_event->speed = cdev->gadget->speed; 31700a2430fSAndrzej Pietrasiewicz v4l2_event_queue(uvc->vdev, &v4l2_event); 31800a2430fSAndrzej Pietrasiewicz 31900a2430fSAndrzej Pietrasiewicz uvc->state = UVC_STATE_CONNECTED; 32000a2430fSAndrzej Pietrasiewicz } 32100a2430fSAndrzej Pietrasiewicz 32200a2430fSAndrzej Pietrasiewicz return 0; 32300a2430fSAndrzej Pietrasiewicz } 32400a2430fSAndrzej Pietrasiewicz 32500a2430fSAndrzej Pietrasiewicz if (interface != uvc->streaming_intf) 32600a2430fSAndrzej Pietrasiewicz return -EINVAL; 32700a2430fSAndrzej Pietrasiewicz 32800a2430fSAndrzej Pietrasiewicz /* TODO 32900a2430fSAndrzej Pietrasiewicz if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) 33000a2430fSAndrzej Pietrasiewicz return alt ? -EINVAL : 0; 33100a2430fSAndrzej Pietrasiewicz */ 33200a2430fSAndrzej Pietrasiewicz 33300a2430fSAndrzej Pietrasiewicz switch (alt) { 33400a2430fSAndrzej Pietrasiewicz case 0: 33500a2430fSAndrzej Pietrasiewicz if (uvc->state != UVC_STATE_STREAMING) 33600a2430fSAndrzej Pietrasiewicz return 0; 33700a2430fSAndrzej Pietrasiewicz 338c92bae75SFelipe Balbi if (uvc->video.ep) { 33900a2430fSAndrzej Pietrasiewicz usb_ep_disable(uvc->video.ep); 340c92bae75SFelipe Balbi uvc->video.ep->driver_data = NULL; 341c92bae75SFelipe Balbi } 34200a2430fSAndrzej Pietrasiewicz 34300a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 34400a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_STREAMOFF; 34500a2430fSAndrzej Pietrasiewicz v4l2_event_queue(uvc->vdev, &v4l2_event); 34600a2430fSAndrzej Pietrasiewicz 34700a2430fSAndrzej Pietrasiewicz uvc->state = UVC_STATE_CONNECTED; 34800a2430fSAndrzej Pietrasiewicz return 0; 34900a2430fSAndrzej Pietrasiewicz 35000a2430fSAndrzej Pietrasiewicz case 1: 35100a2430fSAndrzej Pietrasiewicz if (uvc->state != UVC_STATE_CONNECTED) 35200a2430fSAndrzej Pietrasiewicz return 0; 35300a2430fSAndrzej Pietrasiewicz 354c92bae75SFelipe Balbi if (!uvc->video.ep) 355c92bae75SFelipe Balbi return -EINVAL; 356c92bae75SFelipe Balbi 357c92bae75SFelipe Balbi if (uvc->video.ep->driver_data) { 358c92bae75SFelipe Balbi INFO(cdev, "reset UVC\n"); 359c92bae75SFelipe Balbi usb_ep_disable(uvc->video.ep); 360c92bae75SFelipe Balbi uvc->video.ep->driver_data = NULL; 361c92bae75SFelipe Balbi } 362c92bae75SFelipe Balbi 36300a2430fSAndrzej Pietrasiewicz ret = config_ep_by_speed(f->config->cdev->gadget, 36400a2430fSAndrzej Pietrasiewicz &(uvc->func), uvc->video.ep); 36500a2430fSAndrzej Pietrasiewicz if (ret) 36600a2430fSAndrzej Pietrasiewicz return ret; 36700a2430fSAndrzej Pietrasiewicz usb_ep_enable(uvc->video.ep); 368c92bae75SFelipe Balbi uvc->video.ep->driver_data = uvc; 36900a2430fSAndrzej Pietrasiewicz 37000a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 37100a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_STREAMON; 37200a2430fSAndrzej Pietrasiewicz v4l2_event_queue(uvc->vdev, &v4l2_event); 37300a2430fSAndrzej Pietrasiewicz return USB_GADGET_DELAYED_STATUS; 37400a2430fSAndrzej Pietrasiewicz 37500a2430fSAndrzej Pietrasiewicz default: 37600a2430fSAndrzej Pietrasiewicz return -EINVAL; 37700a2430fSAndrzej Pietrasiewicz } 37800a2430fSAndrzej Pietrasiewicz } 37900a2430fSAndrzej Pietrasiewicz 38000a2430fSAndrzej Pietrasiewicz static void 38100a2430fSAndrzej Pietrasiewicz uvc_function_disable(struct usb_function *f) 38200a2430fSAndrzej Pietrasiewicz { 38300a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 38400a2430fSAndrzej Pietrasiewicz struct v4l2_event v4l2_event; 38500a2430fSAndrzej Pietrasiewicz 38600a2430fSAndrzej Pietrasiewicz INFO(f->config->cdev, "uvc_function_disable\n"); 38700a2430fSAndrzej Pietrasiewicz 38800a2430fSAndrzej Pietrasiewicz memset(&v4l2_event, 0, sizeof(v4l2_event)); 38900a2430fSAndrzej Pietrasiewicz v4l2_event.type = UVC_EVENT_DISCONNECT; 39000a2430fSAndrzej Pietrasiewicz v4l2_event_queue(uvc->vdev, &v4l2_event); 39100a2430fSAndrzej Pietrasiewicz 39200a2430fSAndrzej Pietrasiewicz uvc->state = UVC_STATE_DISCONNECTED; 393*e3122f5fSFelipe Balbi 394*e3122f5fSFelipe Balbi if (uvc->video.ep->driver_data) { 395*e3122f5fSFelipe Balbi usb_ep_disable(uvc->video.ep); 396*e3122f5fSFelipe Balbi uvc->video.ep->driver_data = NULL; 397*e3122f5fSFelipe Balbi } 398*e3122f5fSFelipe Balbi 399*e3122f5fSFelipe Balbi if (uvc->control_ep->driver_data) { 400*e3122f5fSFelipe Balbi usb_ep_disable(uvc->control_ep); 401*e3122f5fSFelipe Balbi uvc->control_ep->driver_data = NULL; 402*e3122f5fSFelipe Balbi } 40300a2430fSAndrzej Pietrasiewicz } 40400a2430fSAndrzej Pietrasiewicz 40500a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 40600a2430fSAndrzej Pietrasiewicz * Connection / disconnection 40700a2430fSAndrzej Pietrasiewicz */ 40800a2430fSAndrzej Pietrasiewicz 40900a2430fSAndrzej Pietrasiewicz void 41000a2430fSAndrzej Pietrasiewicz uvc_function_connect(struct uvc_device *uvc) 41100a2430fSAndrzej Pietrasiewicz { 41200a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = uvc->func.config->cdev; 41300a2430fSAndrzej Pietrasiewicz int ret; 41400a2430fSAndrzej Pietrasiewicz 41500a2430fSAndrzej Pietrasiewicz if ((ret = usb_function_activate(&uvc->func)) < 0) 41600a2430fSAndrzej Pietrasiewicz INFO(cdev, "UVC connect failed with %d\n", ret); 41700a2430fSAndrzej Pietrasiewicz } 41800a2430fSAndrzej Pietrasiewicz 41900a2430fSAndrzej Pietrasiewicz void 42000a2430fSAndrzej Pietrasiewicz uvc_function_disconnect(struct uvc_device *uvc) 42100a2430fSAndrzej Pietrasiewicz { 42200a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = uvc->func.config->cdev; 42300a2430fSAndrzej Pietrasiewicz int ret; 42400a2430fSAndrzej Pietrasiewicz 42500a2430fSAndrzej Pietrasiewicz if ((ret = usb_function_deactivate(&uvc->func)) < 0) 42600a2430fSAndrzej Pietrasiewicz INFO(cdev, "UVC disconnect failed with %d\n", ret); 42700a2430fSAndrzej Pietrasiewicz } 42800a2430fSAndrzej Pietrasiewicz 42900a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 43000a2430fSAndrzej Pietrasiewicz * USB probe and disconnect 43100a2430fSAndrzej Pietrasiewicz */ 43200a2430fSAndrzej Pietrasiewicz 43300a2430fSAndrzej Pietrasiewicz static int 43400a2430fSAndrzej Pietrasiewicz uvc_register_video(struct uvc_device *uvc) 43500a2430fSAndrzej Pietrasiewicz { 43600a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = uvc->func.config->cdev; 43700a2430fSAndrzej Pietrasiewicz struct video_device *video; 43800a2430fSAndrzej Pietrasiewicz 43900a2430fSAndrzej Pietrasiewicz /* TODO reference counting. */ 44000a2430fSAndrzej Pietrasiewicz video = video_device_alloc(); 44100a2430fSAndrzej Pietrasiewicz if (video == NULL) 44200a2430fSAndrzej Pietrasiewicz return -ENOMEM; 44300a2430fSAndrzej Pietrasiewicz 44400a2430fSAndrzej Pietrasiewicz video->v4l2_dev = &uvc->v4l2_dev; 44500a2430fSAndrzej Pietrasiewicz video->fops = &uvc_v4l2_fops; 446a1d27a4bSLaurent Pinchart video->ioctl_ops = &uvc_v4l2_ioctl_ops; 44700a2430fSAndrzej Pietrasiewicz video->release = video_device_release; 448a1d27a4bSLaurent Pinchart video->vfl_dir = VFL_DIR_TX; 44900a2430fSAndrzej Pietrasiewicz strlcpy(video->name, cdev->gadget->name, sizeof(video->name)); 45000a2430fSAndrzej Pietrasiewicz 45100a2430fSAndrzej Pietrasiewicz uvc->vdev = video; 45200a2430fSAndrzej Pietrasiewicz video_set_drvdata(video, uvc); 45300a2430fSAndrzej Pietrasiewicz 45400a2430fSAndrzej Pietrasiewicz return video_register_device(video, VFL_TYPE_GRABBER, -1); 45500a2430fSAndrzej Pietrasiewicz } 45600a2430fSAndrzej Pietrasiewicz 45700a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ 45800a2430fSAndrzej Pietrasiewicz do { \ 45900a2430fSAndrzej Pietrasiewicz memcpy(mem, desc, (desc)->bLength); \ 46000a2430fSAndrzej Pietrasiewicz *(dst)++ = mem; \ 46100a2430fSAndrzej Pietrasiewicz mem += (desc)->bLength; \ 46200a2430fSAndrzej Pietrasiewicz } while (0); 46300a2430fSAndrzej Pietrasiewicz 46400a2430fSAndrzej Pietrasiewicz #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ 46500a2430fSAndrzej Pietrasiewicz do { \ 46600a2430fSAndrzej Pietrasiewicz const struct usb_descriptor_header * const *__src; \ 46700a2430fSAndrzej Pietrasiewicz for (__src = src; *__src; ++__src) { \ 46800a2430fSAndrzej Pietrasiewicz memcpy(mem, *__src, (*__src)->bLength); \ 46900a2430fSAndrzej Pietrasiewicz *dst++ = mem; \ 47000a2430fSAndrzej Pietrasiewicz mem += (*__src)->bLength; \ 47100a2430fSAndrzej Pietrasiewicz } \ 47200a2430fSAndrzej Pietrasiewicz } while (0) 47300a2430fSAndrzej Pietrasiewicz 4746d11ed76SAndrzej Pietrasiewicz static struct usb_descriptor_header ** 47500a2430fSAndrzej Pietrasiewicz uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) 47600a2430fSAndrzej Pietrasiewicz { 47700a2430fSAndrzej Pietrasiewicz struct uvc_input_header_descriptor *uvc_streaming_header; 47800a2430fSAndrzej Pietrasiewicz struct uvc_header_descriptor *uvc_control_header; 47900a2430fSAndrzej Pietrasiewicz const struct uvc_descriptor_header * const *uvc_control_desc; 48000a2430fSAndrzej Pietrasiewicz const struct uvc_descriptor_header * const *uvc_streaming_cls; 48100a2430fSAndrzej Pietrasiewicz const struct usb_descriptor_header * const *uvc_streaming_std; 48200a2430fSAndrzej Pietrasiewicz const struct usb_descriptor_header * const *src; 48300a2430fSAndrzej Pietrasiewicz struct usb_descriptor_header **dst; 48400a2430fSAndrzej Pietrasiewicz struct usb_descriptor_header **hdr; 48500a2430fSAndrzej Pietrasiewicz unsigned int control_size; 48600a2430fSAndrzej Pietrasiewicz unsigned int streaming_size; 48700a2430fSAndrzej Pietrasiewicz unsigned int n_desc; 48800a2430fSAndrzej Pietrasiewicz unsigned int bytes; 48900a2430fSAndrzej Pietrasiewicz void *mem; 49000a2430fSAndrzej Pietrasiewicz 49100a2430fSAndrzej Pietrasiewicz switch (speed) { 49200a2430fSAndrzej Pietrasiewicz case USB_SPEED_SUPER: 49300a2430fSAndrzej Pietrasiewicz uvc_control_desc = uvc->desc.ss_control; 49400a2430fSAndrzej Pietrasiewicz uvc_streaming_cls = uvc->desc.ss_streaming; 49500a2430fSAndrzej Pietrasiewicz uvc_streaming_std = uvc_ss_streaming; 49600a2430fSAndrzej Pietrasiewicz break; 49700a2430fSAndrzej Pietrasiewicz 49800a2430fSAndrzej Pietrasiewicz case USB_SPEED_HIGH: 49900a2430fSAndrzej Pietrasiewicz uvc_control_desc = uvc->desc.fs_control; 50000a2430fSAndrzej Pietrasiewicz uvc_streaming_cls = uvc->desc.hs_streaming; 50100a2430fSAndrzej Pietrasiewicz uvc_streaming_std = uvc_hs_streaming; 50200a2430fSAndrzej Pietrasiewicz break; 50300a2430fSAndrzej Pietrasiewicz 50400a2430fSAndrzej Pietrasiewicz case USB_SPEED_FULL: 50500a2430fSAndrzej Pietrasiewicz default: 50600a2430fSAndrzej Pietrasiewicz uvc_control_desc = uvc->desc.fs_control; 50700a2430fSAndrzej Pietrasiewicz uvc_streaming_cls = uvc->desc.fs_streaming; 50800a2430fSAndrzej Pietrasiewicz uvc_streaming_std = uvc_fs_streaming; 50900a2430fSAndrzej Pietrasiewicz break; 51000a2430fSAndrzej Pietrasiewicz } 51100a2430fSAndrzej Pietrasiewicz 51200a2430fSAndrzej Pietrasiewicz /* Descriptors layout 51300a2430fSAndrzej Pietrasiewicz * 51400a2430fSAndrzej Pietrasiewicz * uvc_iad 51500a2430fSAndrzej Pietrasiewicz * uvc_control_intf 51600a2430fSAndrzej Pietrasiewicz * Class-specific UVC control descriptors 51700a2430fSAndrzej Pietrasiewicz * uvc_control_ep 51800a2430fSAndrzej Pietrasiewicz * uvc_control_cs_ep 51900a2430fSAndrzej Pietrasiewicz * uvc_ss_control_comp (for SS only) 52000a2430fSAndrzej Pietrasiewicz * uvc_streaming_intf_alt0 52100a2430fSAndrzej Pietrasiewicz * Class-specific UVC streaming descriptors 52200a2430fSAndrzej Pietrasiewicz * uvc_{fs|hs}_streaming 52300a2430fSAndrzej Pietrasiewicz */ 52400a2430fSAndrzej Pietrasiewicz 52500a2430fSAndrzej Pietrasiewicz /* Count descriptors and compute their size. */ 52600a2430fSAndrzej Pietrasiewicz control_size = 0; 52700a2430fSAndrzej Pietrasiewicz streaming_size = 0; 52800a2430fSAndrzej Pietrasiewicz bytes = uvc_iad.bLength + uvc_control_intf.bLength 52900a2430fSAndrzej Pietrasiewicz + uvc_control_ep.bLength + uvc_control_cs_ep.bLength 53000a2430fSAndrzej Pietrasiewicz + uvc_streaming_intf_alt0.bLength; 53100a2430fSAndrzej Pietrasiewicz 53200a2430fSAndrzej Pietrasiewicz if (speed == USB_SPEED_SUPER) { 53300a2430fSAndrzej Pietrasiewicz bytes += uvc_ss_control_comp.bLength; 53400a2430fSAndrzej Pietrasiewicz n_desc = 6; 53500a2430fSAndrzej Pietrasiewicz } else { 53600a2430fSAndrzej Pietrasiewicz n_desc = 5; 53700a2430fSAndrzej Pietrasiewicz } 53800a2430fSAndrzej Pietrasiewicz 53900a2430fSAndrzej Pietrasiewicz for (src = (const struct usb_descriptor_header **)uvc_control_desc; 54000a2430fSAndrzej Pietrasiewicz *src; ++src) { 54100a2430fSAndrzej Pietrasiewicz control_size += (*src)->bLength; 54200a2430fSAndrzej Pietrasiewicz bytes += (*src)->bLength; 54300a2430fSAndrzej Pietrasiewicz n_desc++; 54400a2430fSAndrzej Pietrasiewicz } 54500a2430fSAndrzej Pietrasiewicz for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; 54600a2430fSAndrzej Pietrasiewicz *src; ++src) { 54700a2430fSAndrzej Pietrasiewicz streaming_size += (*src)->bLength; 54800a2430fSAndrzej Pietrasiewicz bytes += (*src)->bLength; 54900a2430fSAndrzej Pietrasiewicz n_desc++; 55000a2430fSAndrzej Pietrasiewicz } 55100a2430fSAndrzej Pietrasiewicz for (src = uvc_streaming_std; *src; ++src) { 55200a2430fSAndrzej Pietrasiewicz bytes += (*src)->bLength; 55300a2430fSAndrzej Pietrasiewicz n_desc++; 55400a2430fSAndrzej Pietrasiewicz } 55500a2430fSAndrzej Pietrasiewicz 55600a2430fSAndrzej Pietrasiewicz mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); 55700a2430fSAndrzej Pietrasiewicz if (mem == NULL) 55800a2430fSAndrzej Pietrasiewicz return NULL; 55900a2430fSAndrzej Pietrasiewicz 56000a2430fSAndrzej Pietrasiewicz hdr = mem; 56100a2430fSAndrzej Pietrasiewicz dst = mem; 56200a2430fSAndrzej Pietrasiewicz mem += (n_desc + 1) * sizeof(*src); 56300a2430fSAndrzej Pietrasiewicz 56400a2430fSAndrzej Pietrasiewicz /* Copy the descriptors. */ 56500a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); 56600a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); 56700a2430fSAndrzej Pietrasiewicz 56800a2430fSAndrzej Pietrasiewicz uvc_control_header = mem; 56900a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTORS(mem, dst, 57000a2430fSAndrzej Pietrasiewicz (const struct usb_descriptor_header **)uvc_control_desc); 57100a2430fSAndrzej Pietrasiewicz uvc_control_header->wTotalLength = cpu_to_le16(control_size); 57200a2430fSAndrzej Pietrasiewicz uvc_control_header->bInCollection = 1; 57300a2430fSAndrzej Pietrasiewicz uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; 57400a2430fSAndrzej Pietrasiewicz 57500a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); 57600a2430fSAndrzej Pietrasiewicz if (speed == USB_SPEED_SUPER) 57700a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); 57800a2430fSAndrzej Pietrasiewicz 57900a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); 58000a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); 58100a2430fSAndrzej Pietrasiewicz 58200a2430fSAndrzej Pietrasiewicz uvc_streaming_header = mem; 58300a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTORS(mem, dst, 58400a2430fSAndrzej Pietrasiewicz (const struct usb_descriptor_header**)uvc_streaming_cls); 58500a2430fSAndrzej Pietrasiewicz uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); 58600a2430fSAndrzej Pietrasiewicz uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; 58700a2430fSAndrzej Pietrasiewicz 58800a2430fSAndrzej Pietrasiewicz UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); 58900a2430fSAndrzej Pietrasiewicz 59000a2430fSAndrzej Pietrasiewicz *dst = NULL; 59100a2430fSAndrzej Pietrasiewicz return hdr; 59200a2430fSAndrzej Pietrasiewicz } 59300a2430fSAndrzej Pietrasiewicz 5946d11ed76SAndrzej Pietrasiewicz static int 59500a2430fSAndrzej Pietrasiewicz uvc_function_bind(struct usb_configuration *c, struct usb_function *f) 59600a2430fSAndrzej Pietrasiewicz { 59700a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = c->cdev; 59800a2430fSAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 59913443799SAndrzej Pietrasiewicz struct usb_string *us; 60000a2430fSAndrzej Pietrasiewicz unsigned int max_packet_mult; 60100a2430fSAndrzej Pietrasiewicz unsigned int max_packet_size; 60200a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 6036d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts; 60400a2430fSAndrzej Pietrasiewicz int ret = -EINVAL; 60500a2430fSAndrzej Pietrasiewicz 60600a2430fSAndrzej Pietrasiewicz INFO(cdev, "uvc_function_bind\n"); 60700a2430fSAndrzej Pietrasiewicz 6086d11ed76SAndrzej Pietrasiewicz opts = to_f_uvc_opts(f->fi); 6096d11ed76SAndrzej Pietrasiewicz /* Sanity check the streaming endpoint module parameters. 6106d11ed76SAndrzej Pietrasiewicz */ 6116d11ed76SAndrzej Pietrasiewicz opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); 6126d11ed76SAndrzej Pietrasiewicz opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); 6136d11ed76SAndrzej Pietrasiewicz opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); 6146d11ed76SAndrzej Pietrasiewicz 6156d11ed76SAndrzej Pietrasiewicz /* Fill in the FS/HS/SS Video Streaming specific descriptors from the 6166d11ed76SAndrzej Pietrasiewicz * module parameters. 6176d11ed76SAndrzej Pietrasiewicz * 6186d11ed76SAndrzej Pietrasiewicz * NOTE: We assume that the user knows what they are doing and won't 6196d11ed76SAndrzej Pietrasiewicz * give parameters that their UDC doesn't support. 6206d11ed76SAndrzej Pietrasiewicz */ 6216d11ed76SAndrzej Pietrasiewicz if (opts->streaming_maxpacket <= 1024) { 6226d11ed76SAndrzej Pietrasiewicz max_packet_mult = 1; 6236d11ed76SAndrzej Pietrasiewicz max_packet_size = opts->streaming_maxpacket; 6246d11ed76SAndrzej Pietrasiewicz } else if (opts->streaming_maxpacket <= 2048) { 6256d11ed76SAndrzej Pietrasiewicz max_packet_mult = 2; 6266d11ed76SAndrzej Pietrasiewicz max_packet_size = opts->streaming_maxpacket / 2; 6276d11ed76SAndrzej Pietrasiewicz } else { 6286d11ed76SAndrzej Pietrasiewicz max_packet_mult = 3; 6296d11ed76SAndrzej Pietrasiewicz max_packet_size = opts->streaming_maxpacket / 3; 6306d11ed76SAndrzej Pietrasiewicz } 6316d11ed76SAndrzej Pietrasiewicz 6326d11ed76SAndrzej Pietrasiewicz uvc_fs_streaming_ep.wMaxPacketSize = 633e102609fSLaurent Pinchart cpu_to_le16(min(opts->streaming_maxpacket, 1023U)); 6346d11ed76SAndrzej Pietrasiewicz uvc_fs_streaming_ep.bInterval = opts->streaming_interval; 6356d11ed76SAndrzej Pietrasiewicz 636e102609fSLaurent Pinchart uvc_hs_streaming_ep.wMaxPacketSize = 637e102609fSLaurent Pinchart cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11)); 6386d11ed76SAndrzej Pietrasiewicz uvc_hs_streaming_ep.bInterval = opts->streaming_interval; 6396d11ed76SAndrzej Pietrasiewicz 640e102609fSLaurent Pinchart uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size); 6416d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_ep.bInterval = opts->streaming_interval; 6426d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; 6436d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; 6446d11ed76SAndrzej Pietrasiewicz uvc_ss_streaming_comp.wBytesPerInterval = 645e102609fSLaurent Pinchart cpu_to_le16(max_packet_size * max_packet_mult * 646e102609fSLaurent Pinchart opts->streaming_maxburst); 64700a2430fSAndrzej Pietrasiewicz 64800a2430fSAndrzej Pietrasiewicz /* Allocate endpoints. */ 64900a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); 65000a2430fSAndrzej Pietrasiewicz if (!ep) { 65100a2430fSAndrzej Pietrasiewicz INFO(cdev, "Unable to allocate control EP\n"); 65200a2430fSAndrzej Pietrasiewicz goto error; 65300a2430fSAndrzej Pietrasiewicz } 65400a2430fSAndrzej Pietrasiewicz uvc->control_ep = ep; 65500a2430fSAndrzej Pietrasiewicz ep->driver_data = uvc; 65600a2430fSAndrzej Pietrasiewicz 65700a2430fSAndrzej Pietrasiewicz if (gadget_is_superspeed(c->cdev->gadget)) 65800a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, 65900a2430fSAndrzej Pietrasiewicz &uvc_ss_streaming_comp); 66000a2430fSAndrzej Pietrasiewicz else if (gadget_is_dualspeed(cdev->gadget)) 66100a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); 66200a2430fSAndrzej Pietrasiewicz else 66300a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); 66400a2430fSAndrzej Pietrasiewicz 66500a2430fSAndrzej Pietrasiewicz if (!ep) { 66600a2430fSAndrzej Pietrasiewicz INFO(cdev, "Unable to allocate streaming EP\n"); 66700a2430fSAndrzej Pietrasiewicz goto error; 66800a2430fSAndrzej Pietrasiewicz } 66900a2430fSAndrzej Pietrasiewicz uvc->video.ep = ep; 67000a2430fSAndrzej Pietrasiewicz ep->driver_data = uvc; 67100a2430fSAndrzej Pietrasiewicz 67200a2430fSAndrzej Pietrasiewicz uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 67300a2430fSAndrzej Pietrasiewicz uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 67400a2430fSAndrzej Pietrasiewicz uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; 67500a2430fSAndrzej Pietrasiewicz 67613443799SAndrzej Pietrasiewicz us = usb_gstrings_attach(cdev, uvc_function_strings, 67713443799SAndrzej Pietrasiewicz ARRAY_SIZE(uvc_en_us_strings)); 67813443799SAndrzej Pietrasiewicz if (IS_ERR(us)) { 67913443799SAndrzej Pietrasiewicz ret = PTR_ERR(us); 6806d11ed76SAndrzej Pietrasiewicz goto error; 68113443799SAndrzej Pietrasiewicz } 68213443799SAndrzej Pietrasiewicz uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id; 68313443799SAndrzej Pietrasiewicz uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id; 68413443799SAndrzej Pietrasiewicz ret = us[UVC_STRING_STREAMING_IDX].id; 6856d11ed76SAndrzej Pietrasiewicz uvc_streaming_intf_alt0.iInterface = ret; 6866d11ed76SAndrzej Pietrasiewicz uvc_streaming_intf_alt1.iInterface = ret; 6876d11ed76SAndrzej Pietrasiewicz 68800a2430fSAndrzej Pietrasiewicz /* Allocate interface IDs. */ 68900a2430fSAndrzej Pietrasiewicz if ((ret = usb_interface_id(c, f)) < 0) 69000a2430fSAndrzej Pietrasiewicz goto error; 69100a2430fSAndrzej Pietrasiewicz uvc_iad.bFirstInterface = ret; 69200a2430fSAndrzej Pietrasiewicz uvc_control_intf.bInterfaceNumber = ret; 69300a2430fSAndrzej Pietrasiewicz uvc->control_intf = ret; 69400a2430fSAndrzej Pietrasiewicz 69500a2430fSAndrzej Pietrasiewicz if ((ret = usb_interface_id(c, f)) < 0) 69600a2430fSAndrzej Pietrasiewicz goto error; 69700a2430fSAndrzej Pietrasiewicz uvc_streaming_intf_alt0.bInterfaceNumber = ret; 69800a2430fSAndrzej Pietrasiewicz uvc_streaming_intf_alt1.bInterfaceNumber = ret; 69900a2430fSAndrzej Pietrasiewicz uvc->streaming_intf = ret; 70000a2430fSAndrzej Pietrasiewicz 70100a2430fSAndrzej Pietrasiewicz /* Copy descriptors */ 70200a2430fSAndrzej Pietrasiewicz f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); 70300a2430fSAndrzej Pietrasiewicz if (gadget_is_dualspeed(cdev->gadget)) 70400a2430fSAndrzej Pietrasiewicz f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); 70500a2430fSAndrzej Pietrasiewicz if (gadget_is_superspeed(c->cdev->gadget)) 70600a2430fSAndrzej Pietrasiewicz f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); 70700a2430fSAndrzej Pietrasiewicz 70800a2430fSAndrzej Pietrasiewicz /* Preallocate control endpoint request. */ 70900a2430fSAndrzej Pietrasiewicz uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); 71000a2430fSAndrzej Pietrasiewicz uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); 71100a2430fSAndrzej Pietrasiewicz if (uvc->control_req == NULL || uvc->control_buf == NULL) { 71200a2430fSAndrzej Pietrasiewicz ret = -ENOMEM; 71300a2430fSAndrzej Pietrasiewicz goto error; 71400a2430fSAndrzej Pietrasiewicz } 71500a2430fSAndrzej Pietrasiewicz 71600a2430fSAndrzej Pietrasiewicz uvc->control_req->buf = uvc->control_buf; 71700a2430fSAndrzej Pietrasiewicz uvc->control_req->complete = uvc_function_ep0_complete; 71800a2430fSAndrzej Pietrasiewicz uvc->control_req->context = uvc; 71900a2430fSAndrzej Pietrasiewicz 72000a2430fSAndrzej Pietrasiewicz /* Avoid letting this gadget enumerate until the userspace server is 72100a2430fSAndrzej Pietrasiewicz * active. 72200a2430fSAndrzej Pietrasiewicz */ 72300a2430fSAndrzej Pietrasiewicz if ((ret = usb_function_deactivate(f)) < 0) 72400a2430fSAndrzej Pietrasiewicz goto error; 72500a2430fSAndrzej Pietrasiewicz 72600a2430fSAndrzej Pietrasiewicz if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { 72700a2430fSAndrzej Pietrasiewicz printk(KERN_INFO "v4l2_device_register failed\n"); 72800a2430fSAndrzej Pietrasiewicz goto error; 72900a2430fSAndrzej Pietrasiewicz } 73000a2430fSAndrzej Pietrasiewicz 73100a2430fSAndrzej Pietrasiewicz /* Initialise video. */ 7327ea95b11SAndrzej Pietrasiewicz ret = uvcg_video_init(&uvc->video); 73300a2430fSAndrzej Pietrasiewicz if (ret < 0) 73400a2430fSAndrzej Pietrasiewicz goto error; 73500a2430fSAndrzej Pietrasiewicz 73600a2430fSAndrzej Pietrasiewicz /* Register a V4L2 device. */ 73700a2430fSAndrzej Pietrasiewicz ret = uvc_register_video(uvc); 73800a2430fSAndrzej Pietrasiewicz if (ret < 0) { 73900a2430fSAndrzej Pietrasiewicz printk(KERN_INFO "Unable to register video device\n"); 74000a2430fSAndrzej Pietrasiewicz goto error; 74100a2430fSAndrzej Pietrasiewicz } 74200a2430fSAndrzej Pietrasiewicz 74300a2430fSAndrzej Pietrasiewicz return 0; 74400a2430fSAndrzej Pietrasiewicz 74500a2430fSAndrzej Pietrasiewicz error: 74600a2430fSAndrzej Pietrasiewicz v4l2_device_unregister(&uvc->v4l2_dev); 74700a2430fSAndrzej Pietrasiewicz if (uvc->vdev) 74800a2430fSAndrzej Pietrasiewicz video_device_release(uvc->vdev); 74900a2430fSAndrzej Pietrasiewicz 75000a2430fSAndrzej Pietrasiewicz if (uvc->control_ep) 75100a2430fSAndrzej Pietrasiewicz uvc->control_ep->driver_data = NULL; 75200a2430fSAndrzej Pietrasiewicz if (uvc->video.ep) 75300a2430fSAndrzej Pietrasiewicz uvc->video.ep->driver_data = NULL; 75400a2430fSAndrzej Pietrasiewicz 755e7379857SAndrzej Pietrasiewicz if (uvc->control_req) 75600a2430fSAndrzej Pietrasiewicz usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 75700a2430fSAndrzej Pietrasiewicz kfree(uvc->control_buf); 75800a2430fSAndrzej Pietrasiewicz 75900a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 76000a2430fSAndrzej Pietrasiewicz return ret; 76100a2430fSAndrzej Pietrasiewicz } 76200a2430fSAndrzej Pietrasiewicz 76300a2430fSAndrzej Pietrasiewicz /* -------------------------------------------------------------------------- 76400a2430fSAndrzej Pietrasiewicz * USB gadget function 76500a2430fSAndrzej Pietrasiewicz */ 76600a2430fSAndrzej Pietrasiewicz 7676d11ed76SAndrzej Pietrasiewicz static void uvc_free_inst(struct usb_function_instance *f) 7686d11ed76SAndrzej Pietrasiewicz { 7696d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts = to_f_uvc_opts(f); 7706d11ed76SAndrzej Pietrasiewicz 7716d11ed76SAndrzej Pietrasiewicz kfree(opts); 7726d11ed76SAndrzej Pietrasiewicz } 7736d11ed76SAndrzej Pietrasiewicz 7746d11ed76SAndrzej Pietrasiewicz static struct usb_function_instance *uvc_alloc_inst(void) 7756d11ed76SAndrzej Pietrasiewicz { 7766d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts; 7776d11ed76SAndrzej Pietrasiewicz 7786d11ed76SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 7796d11ed76SAndrzej Pietrasiewicz if (!opts) 7806d11ed76SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 7816d11ed76SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = uvc_free_inst; 7826d11ed76SAndrzej Pietrasiewicz 7836d11ed76SAndrzej Pietrasiewicz return &opts->func_inst; 7846d11ed76SAndrzej Pietrasiewicz } 7856d11ed76SAndrzej Pietrasiewicz 7866d11ed76SAndrzej Pietrasiewicz static void uvc_free(struct usb_function *f) 7876d11ed76SAndrzej Pietrasiewicz { 7886d11ed76SAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 7896d11ed76SAndrzej Pietrasiewicz 7906d11ed76SAndrzej Pietrasiewicz kfree(uvc); 7916d11ed76SAndrzej Pietrasiewicz } 7926d11ed76SAndrzej Pietrasiewicz 7936d11ed76SAndrzej Pietrasiewicz static void uvc_unbind(struct usb_configuration *c, struct usb_function *f) 7946d11ed76SAndrzej Pietrasiewicz { 7956d11ed76SAndrzej Pietrasiewicz struct usb_composite_dev *cdev = c->cdev; 7966d11ed76SAndrzej Pietrasiewicz struct uvc_device *uvc = to_uvc(f); 7976d11ed76SAndrzej Pietrasiewicz 7986d11ed76SAndrzej Pietrasiewicz INFO(cdev, "%s\n", __func__); 7996d11ed76SAndrzej Pietrasiewicz 8006d11ed76SAndrzej Pietrasiewicz video_unregister_device(uvc->vdev); 8016d11ed76SAndrzej Pietrasiewicz v4l2_device_unregister(&uvc->v4l2_dev); 8026d11ed76SAndrzej Pietrasiewicz uvc->control_ep->driver_data = NULL; 8036d11ed76SAndrzej Pietrasiewicz uvc->video.ep->driver_data = NULL; 8046d11ed76SAndrzej Pietrasiewicz 8056d11ed76SAndrzej Pietrasiewicz usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 8066d11ed76SAndrzej Pietrasiewicz kfree(uvc->control_buf); 8076d11ed76SAndrzej Pietrasiewicz 8086d11ed76SAndrzej Pietrasiewicz usb_free_all_descriptors(f); 8096d11ed76SAndrzej Pietrasiewicz } 8106d11ed76SAndrzej Pietrasiewicz 8114a6698b8SFengguang Wu static struct usb_function *uvc_alloc(struct usb_function_instance *fi) 8126d11ed76SAndrzej Pietrasiewicz { 8136d11ed76SAndrzej Pietrasiewicz struct uvc_device *uvc; 8146d11ed76SAndrzej Pietrasiewicz struct f_uvc_opts *opts; 8156d11ed76SAndrzej Pietrasiewicz 8166d11ed76SAndrzej Pietrasiewicz uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); 8176d11ed76SAndrzej Pietrasiewicz if (uvc == NULL) 8186d11ed76SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 8196d11ed76SAndrzej Pietrasiewicz 8206d11ed76SAndrzej Pietrasiewicz uvc->state = UVC_STATE_DISCONNECTED; 8216d11ed76SAndrzej Pietrasiewicz opts = to_f_uvc_opts(fi); 8226d11ed76SAndrzej Pietrasiewicz 8236d11ed76SAndrzej Pietrasiewicz uvc->desc.fs_control = opts->fs_control; 8246d11ed76SAndrzej Pietrasiewicz uvc->desc.ss_control = opts->ss_control; 8256d11ed76SAndrzej Pietrasiewicz uvc->desc.fs_streaming = opts->fs_streaming; 8266d11ed76SAndrzej Pietrasiewicz uvc->desc.hs_streaming = opts->hs_streaming; 8276d11ed76SAndrzej Pietrasiewicz uvc->desc.ss_streaming = opts->ss_streaming; 8286d11ed76SAndrzej Pietrasiewicz 8296d11ed76SAndrzej Pietrasiewicz /* Register the function. */ 8306d11ed76SAndrzej Pietrasiewicz uvc->func.name = "uvc"; 8316d11ed76SAndrzej Pietrasiewicz uvc->func.bind = uvc_function_bind; 8326d11ed76SAndrzej Pietrasiewicz uvc->func.unbind = uvc_unbind; 8336d11ed76SAndrzej Pietrasiewicz uvc->func.get_alt = uvc_function_get_alt; 8346d11ed76SAndrzej Pietrasiewicz uvc->func.set_alt = uvc_function_set_alt; 8356d11ed76SAndrzej Pietrasiewicz uvc->func.disable = uvc_function_disable; 8366d11ed76SAndrzej Pietrasiewicz uvc->func.setup = uvc_function_setup; 8376d11ed76SAndrzej Pietrasiewicz uvc->func.free_func = uvc_free; 8386d11ed76SAndrzej Pietrasiewicz 8396d11ed76SAndrzej Pietrasiewicz return &uvc->func; 8406d11ed76SAndrzej Pietrasiewicz } 8416d11ed76SAndrzej Pietrasiewicz 8426d11ed76SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); 8436d11ed76SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 8446d11ed76SAndrzej Pietrasiewicz MODULE_AUTHOR("Laurent Pinchart"); 845