Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 2 | /* |
| 3 | * uvc_gadget.c -- USB Video Class Gadget driver |
| 4 | * |
| 5 | * Copyright (C) 2009-2010 |
| 6 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/fs.h> |
Laurent Pinchart | 284eb166 | 2018-05-21 11:28:52 +0300 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 13 | #include <linux/list.h> |
Laurent Pinchart | 284eb166 | 2018-05-21 11:28:52 +0300 | [diff] [blame] | 14 | #include <linux/module.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Chen Gang | 4d2079c | 2013-02-02 15:48:54 +0800 | [diff] [blame] | 16 | #include <linux/string.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 17 | #include <linux/usb/ch9.h> |
| 18 | #include <linux/usb/gadget.h> |
Laurent Pinchart | 284eb166 | 2018-05-21 11:28:52 +0300 | [diff] [blame] | 19 | #include <linux/usb/g_uvc.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 20 | #include <linux/usb/video.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/wait.h> |
| 23 | |
| 24 | #include <media/v4l2-dev.h> |
| 25 | #include <media/v4l2-event.h> |
| 26 | |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 27 | #include "u_uvc.h" |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 28 | #include "uvc.h" |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 29 | #include "uvc_configfs.h" |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 30 | #include "uvc_v4l2.h" |
| 31 | #include "uvc_video.h" |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 32 | |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 33 | unsigned int uvc_gadget_trace_param; |
Laurent Pinchart | 20970d8 | 2018-05-21 11:28:53 +0300 | [diff] [blame] | 34 | module_param_named(trace, uvc_gadget_trace_param, uint, 0644); |
| 35 | MODULE_PARM_DESC(trace, "Trace level bitmask"); |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 36 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 37 | /* -------------------------------------------------------------------------- |
| 38 | * Function descriptors |
| 39 | */ |
| 40 | |
| 41 | /* string IDs are assigned dynamically */ |
| 42 | |
Bhupesh Sharma | 43ff05e | 2013-03-01 20:46:29 +0100 | [diff] [blame] | 43 | #define UVC_STRING_CONTROL_IDX 0 |
| 44 | #define UVC_STRING_STREAMING_IDX 1 |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 45 | |
| 46 | static struct usb_string uvc_en_us_strings[] = { |
Bhupesh Sharma | 43ff05e | 2013-03-01 20:46:29 +0100 | [diff] [blame] | 47 | [UVC_STRING_CONTROL_IDX].s = "UVC Camera", |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 48 | [UVC_STRING_STREAMING_IDX].s = "Video Streaming", |
| 49 | { } |
| 50 | }; |
| 51 | |
| 52 | static struct usb_gadget_strings uvc_stringtab = { |
| 53 | .language = 0x0409, /* en-us */ |
| 54 | .strings = uvc_en_us_strings, |
| 55 | }; |
| 56 | |
| 57 | static struct usb_gadget_strings *uvc_function_strings[] = { |
| 58 | &uvc_stringtab, |
| 59 | NULL, |
| 60 | }; |
| 61 | |
| 62 | #define UVC_INTF_VIDEO_CONTROL 0 |
| 63 | #define UVC_INTF_VIDEO_STREAMING 1 |
| 64 | |
Laurent Pinchart | 912ca42 | 2013-03-01 20:46:23 +0100 | [diff] [blame] | 65 | #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ |
Bhupesh Sharma | 5797663 | 2012-06-01 15:08:55 +0530 | [diff] [blame] | 66 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 67 | static struct usb_interface_assoc_descriptor uvc_iad = { |
Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 68 | .bLength = sizeof(uvc_iad), |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 69 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 70 | .bFirstInterface = 0, |
| 71 | .bInterfaceCount = 2, |
| 72 | .bFunctionClass = USB_CLASS_VIDEO, |
Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 73 | .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 74 | .bFunctionProtocol = 0x00, |
| 75 | .iFunction = 0, |
| 76 | }; |
| 77 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 78 | static struct usb_interface_descriptor uvc_control_intf = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 79 | .bLength = USB_DT_INTERFACE_SIZE, |
| 80 | .bDescriptorType = USB_DT_INTERFACE, |
| 81 | .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, |
| 82 | .bAlternateSetting = 0, |
| 83 | .bNumEndpoints = 1, |
| 84 | .bInterfaceClass = USB_CLASS_VIDEO, |
Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 85 | .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 86 | .bInterfaceProtocol = 0x00, |
| 87 | .iInterface = 0, |
| 88 | }; |
| 89 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 90 | static struct usb_endpoint_descriptor uvc_control_ep = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 91 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 92 | .bDescriptorType = USB_DT_ENDPOINT, |
| 93 | .bEndpointAddress = USB_DIR_IN, |
| 94 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Laurent Pinchart | 912ca42 | 2013-03-01 20:46:23 +0100 | [diff] [blame] | 95 | .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 96 | .bInterval = 8, |
| 97 | }; |
| 98 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 99 | static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = { |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 100 | .bLength = sizeof(uvc_ss_control_comp), |
| 101 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 102 | /* The following 3 values can be tweaked if necessary. */ |
| 103 | .bMaxBurst = 0, |
| 104 | .bmAttributes = 0, |
| 105 | .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
| 106 | }; |
| 107 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 108 | static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 109 | .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, |
| 110 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 111 | .bDescriptorSubType = UVC_EP_INTERRUPT, |
Laurent Pinchart | 912ca42 | 2013-03-01 20:46:23 +0100 | [diff] [blame] | 112 | .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 115 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 116 | .bLength = USB_DT_INTERFACE_SIZE, |
| 117 | .bDescriptorType = USB_DT_INTERFACE, |
| 118 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, |
| 119 | .bAlternateSetting = 0, |
| 120 | .bNumEndpoints = 0, |
| 121 | .bInterfaceClass = USB_CLASS_VIDEO, |
Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 122 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 123 | .bInterfaceProtocol = 0x00, |
| 124 | .iInterface = 0, |
| 125 | }; |
| 126 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 127 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 128 | .bLength = USB_DT_INTERFACE_SIZE, |
| 129 | .bDescriptorType = USB_DT_INTERFACE, |
| 130 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, |
| 131 | .bAlternateSetting = 1, |
| 132 | .bNumEndpoints = 1, |
| 133 | .bInterfaceClass = USB_CLASS_VIDEO, |
Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 134 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 135 | .bInterfaceProtocol = 0x00, |
| 136 | .iInterface = 0, |
| 137 | }; |
| 138 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 139 | static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 140 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 141 | .bDescriptorType = USB_DT_ENDPOINT, |
| 142 | .bEndpointAddress = USB_DIR_IN, |
Bhupesh Sharma | 609a053 | 2013-03-01 20:46:28 +0100 | [diff] [blame] | 143 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
| 144 | | USB_ENDPOINT_XFER_ISOC, |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 145 | /* The wMaxPacketSize and bInterval values will be initialized from |
| 146 | * module parameters. |
| 147 | */ |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 150 | static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 151 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 152 | .bDescriptorType = USB_DT_ENDPOINT, |
| 153 | .bEndpointAddress = USB_DIR_IN, |
Bhupesh Sharma | 609a053 | 2013-03-01 20:46:28 +0100 | [diff] [blame] | 154 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
| 155 | | USB_ENDPOINT_XFER_ISOC, |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 156 | /* The wMaxPacketSize and bInterval values will be initialized from |
| 157 | * module parameters. |
| 158 | */ |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 159 | }; |
| 160 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 161 | static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 162 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 163 | .bDescriptorType = USB_DT_ENDPOINT, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 164 | |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 165 | .bEndpointAddress = USB_DIR_IN, |
Bhupesh Sharma | 609a053 | 2013-03-01 20:46:28 +0100 | [diff] [blame] | 166 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
| 167 | | USB_ENDPOINT_XFER_ISOC, |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 168 | /* The wMaxPacketSize and bInterval values will be initialized from |
| 169 | * module parameters. |
| 170 | */ |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 171 | }; |
| 172 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 173 | static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 174 | .bLength = sizeof(uvc_ss_streaming_comp), |
| 175 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
Laurent Pinchart | dbbafe6 | 2013-04-29 12:16:30 +0200 | [diff] [blame] | 176 | /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be |
| 177 | * initialized from module parameters. |
| 178 | */ |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 179 | }; |
| 180 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 181 | static const struct usb_descriptor_header * const uvc_fs_streaming[] = { |
| 182 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 183 | (struct usb_descriptor_header *) &uvc_fs_streaming_ep, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 184 | NULL, |
| 185 | }; |
| 186 | |
| 187 | static const struct usb_descriptor_header * const uvc_hs_streaming[] = { |
| 188 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 189 | (struct usb_descriptor_header *) &uvc_hs_streaming_ep, |
| 190 | NULL, |
| 191 | }; |
| 192 | |
| 193 | static const struct usb_descriptor_header * const uvc_ss_streaming[] = { |
| 194 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, |
| 195 | (struct usb_descriptor_header *) &uvc_ss_streaming_ep, |
| 196 | (struct usb_descriptor_header *) &uvc_ss_streaming_comp, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 197 | NULL, |
| 198 | }; |
| 199 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 200 | void uvc_set_trace_param(unsigned int trace) |
| 201 | { |
| 202 | uvc_gadget_trace_param = trace; |
| 203 | } |
| 204 | EXPORT_SYMBOL(uvc_set_trace_param); |
| 205 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 206 | /* -------------------------------------------------------------------------- |
| 207 | * Control requests |
| 208 | */ |
| 209 | |
| 210 | static void |
| 211 | uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) |
| 212 | { |
| 213 | struct uvc_device *uvc = req->context; |
| 214 | struct v4l2_event v4l2_event; |
| 215 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 216 | |
| 217 | if (uvc->event_setup_out) { |
| 218 | uvc->event_setup_out = 0; |
| 219 | |
| 220 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 221 | v4l2_event.type = UVC_EVENT_DATA; |
| 222 | uvc_event->data.length = req->actual; |
| 223 | memcpy(&uvc_event->data.data, req->buf, req->actual); |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 224 | v4l2_event_queue(&uvc->vdev, &v4l2_event); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 230 | { |
| 231 | struct uvc_device *uvc = to_uvc(f); |
| 232 | struct v4l2_event v4l2_event; |
| 233 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 234 | |
| 235 | /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", |
| 236 | * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), |
| 237 | * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); |
| 238 | */ |
| 239 | |
| 240 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { |
| 241 | INFO(f->config->cdev, "invalid request type\n"); |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | /* Stall too big requests. */ |
| 246 | if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) |
| 247 | return -EINVAL; |
| 248 | |
Laurent Pinchart | 26a029f | 2014-09-08 11:18:14 +0300 | [diff] [blame] | 249 | /* Tell the complete callback to generate an event for the next request |
| 250 | * that will be enqueued by UVCIOC_SEND_RESPONSE. |
| 251 | */ |
| 252 | uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); |
| 253 | uvc->event_length = le16_to_cpu(ctrl->wLength); |
| 254 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 255 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 256 | v4l2_event.type = UVC_EVENT_SETUP; |
| 257 | memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 258 | v4l2_event_queue(&uvc->vdev, &v4l2_event); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Bhupesh Sharma | 41837c3 | 2013-03-01 20:46:30 +0100 | [diff] [blame] | 263 | void uvc_function_setup_continue(struct uvc_device *uvc) |
| 264 | { |
| 265 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 266 | |
| 267 | usb_composite_setup_continue(cdev); |
| 268 | } |
| 269 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 270 | static int |
| 271 | uvc_function_get_alt(struct usb_function *f, unsigned interface) |
| 272 | { |
| 273 | struct uvc_device *uvc = to_uvc(f); |
| 274 | |
| 275 | INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); |
| 276 | |
| 277 | if (interface == uvc->control_intf) |
| 278 | return 0; |
| 279 | else if (interface != uvc->streaming_intf) |
| 280 | return -EINVAL; |
| 281 | else |
Robert Baldyga | d62bf8c | 2015-09-16 12:11:00 +0200 | [diff] [blame] | 282 | return uvc->video.ep->enabled ? 1 : 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static int |
| 286 | uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) |
| 287 | { |
| 288 | struct uvc_device *uvc = to_uvc(f); |
Felipe Balbi | c92bae7 | 2014-09-29 09:20:35 -0500 | [diff] [blame] | 289 | struct usb_composite_dev *cdev = f->config->cdev; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 290 | struct v4l2_event v4l2_event; |
| 291 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 292 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 293 | |
Felipe Balbi | c92bae7 | 2014-09-29 09:20:35 -0500 | [diff] [blame] | 294 | INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 295 | |
| 296 | if (interface == uvc->control_intf) { |
| 297 | if (alt) |
| 298 | return -EINVAL; |
| 299 | |
Robert Baldyga | d62bf8c | 2015-09-16 12:11:00 +0200 | [diff] [blame] | 300 | INFO(cdev, "reset UVC Control\n"); |
| 301 | usb_ep_disable(uvc->control_ep); |
Felipe Balbi | 62e3707 | 2014-09-29 13:41:04 -0500 | [diff] [blame] | 302 | |
| 303 | if (!uvc->control_ep->desc) |
| 304 | if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep)) |
| 305 | return -EINVAL; |
| 306 | |
| 307 | usb_ep_enable(uvc->control_ep); |
Felipe Balbi | 62e3707 | 2014-09-29 13:41:04 -0500 | [diff] [blame] | 308 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 309 | if (uvc->state == UVC_STATE_DISCONNECTED) { |
| 310 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 311 | v4l2_event.type = UVC_EVENT_CONNECT; |
Felipe Balbi | c92bae7 | 2014-09-29 09:20:35 -0500 | [diff] [blame] | 312 | uvc_event->speed = cdev->gadget->speed; |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 313 | v4l2_event_queue(&uvc->vdev, &v4l2_event); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 314 | |
| 315 | uvc->state = UVC_STATE_CONNECTED; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | if (interface != uvc->streaming_intf) |
| 322 | return -EINVAL; |
| 323 | |
| 324 | /* TODO |
| 325 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) |
| 326 | return alt ? -EINVAL : 0; |
| 327 | */ |
| 328 | |
| 329 | switch (alt) { |
| 330 | case 0: |
| 331 | if (uvc->state != UVC_STATE_STREAMING) |
| 332 | return 0; |
| 333 | |
Robert Baldyga | d62bf8c | 2015-09-16 12:11:00 +0200 | [diff] [blame] | 334 | if (uvc->video.ep) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 335 | usb_ep_disable(uvc->video.ep); |
| 336 | |
| 337 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 338 | v4l2_event.type = UVC_EVENT_STREAMOFF; |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 339 | v4l2_event_queue(&uvc->vdev, &v4l2_event); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 340 | |
| 341 | uvc->state = UVC_STATE_CONNECTED; |
Bhupesh Sharma | 41837c3 | 2013-03-01 20:46:30 +0100 | [diff] [blame] | 342 | return 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 343 | |
| 344 | case 1: |
| 345 | if (uvc->state != UVC_STATE_CONNECTED) |
| 346 | return 0; |
| 347 | |
Felipe Balbi | c92bae7 | 2014-09-29 09:20:35 -0500 | [diff] [blame] | 348 | if (!uvc->video.ep) |
| 349 | return -EINVAL; |
| 350 | |
Robert Baldyga | d62bf8c | 2015-09-16 12:11:00 +0200 | [diff] [blame] | 351 | INFO(cdev, "reset UVC\n"); |
| 352 | usb_ep_disable(uvc->video.ep); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 353 | |
Felipe Balbi | c92bae7 | 2014-09-29 09:20:35 -0500 | [diff] [blame] | 354 | ret = config_ep_by_speed(f->config->cdev->gadget, |
| 355 | &(uvc->func), uvc->video.ep); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | usb_ep_enable(uvc->video.ep); |
Felipe Balbi | c92bae7 | 2014-09-29 09:20:35 -0500 | [diff] [blame] | 359 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 360 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 361 | v4l2_event.type = UVC_EVENT_STREAMON; |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 362 | v4l2_event_queue(&uvc->vdev, &v4l2_event); |
Bhupesh Sharma | 41837c3 | 2013-03-01 20:46:30 +0100 | [diff] [blame] | 363 | return USB_GADGET_DELAYED_STATUS; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 364 | |
| 365 | default: |
| 366 | return -EINVAL; |
| 367 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void |
| 371 | uvc_function_disable(struct usb_function *f) |
| 372 | { |
| 373 | struct uvc_device *uvc = to_uvc(f); |
| 374 | struct v4l2_event v4l2_event; |
| 375 | |
| 376 | INFO(f->config->cdev, "uvc_function_disable\n"); |
| 377 | |
| 378 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 379 | v4l2_event.type = UVC_EVENT_DISCONNECT; |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 380 | v4l2_event_queue(&uvc->vdev, &v4l2_event); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 381 | |
| 382 | uvc->state = UVC_STATE_DISCONNECTED; |
Felipe Balbi | e3122f5 | 2014-09-29 13:43:20 -0500 | [diff] [blame] | 383 | |
Robert Baldyga | d62bf8c | 2015-09-16 12:11:00 +0200 | [diff] [blame] | 384 | usb_ep_disable(uvc->video.ep); |
| 385 | usb_ep_disable(uvc->control_ep); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* -------------------------------------------------------------------------- |
| 389 | * Connection / disconnection |
| 390 | */ |
| 391 | |
| 392 | void |
| 393 | uvc_function_connect(struct uvc_device *uvc) |
| 394 | { |
| 395 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 396 | int ret; |
| 397 | |
| 398 | if ((ret = usb_function_activate(&uvc->func)) < 0) |
| 399 | INFO(cdev, "UVC connect failed with %d\n", ret); |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | uvc_function_disconnect(struct uvc_device *uvc) |
| 404 | { |
| 405 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 406 | int ret; |
| 407 | |
| 408 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) |
| 409 | INFO(cdev, "UVC disconnect failed with %d\n", ret); |
| 410 | } |
| 411 | |
| 412 | /* -------------------------------------------------------------------------- |
| 413 | * USB probe and disconnect |
| 414 | */ |
| 415 | |
Kieran Bingham | d7af78b | 2018-05-24 17:16:12 +0100 | [diff] [blame] | 416 | static ssize_t function_name_show(struct device *dev, |
| 417 | struct device_attribute *attr, char *buf) |
| 418 | { |
| 419 | struct uvc_device *uvc = dev_get_drvdata(dev); |
| 420 | |
| 421 | return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name); |
| 422 | } |
| 423 | |
| 424 | static DEVICE_ATTR_RO(function_name); |
| 425 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 426 | static int |
| 427 | uvc_register_video(struct uvc_device *uvc) |
| 428 | { |
| 429 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
Kieran Bingham | d7af78b | 2018-05-24 17:16:12 +0100 | [diff] [blame] | 430 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 431 | |
| 432 | /* TODO reference counting. */ |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 433 | uvc->vdev.v4l2_dev = &uvc->v4l2_dev; |
| 434 | uvc->vdev.fops = &uvc_v4l2_fops; |
| 435 | uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops; |
| 436 | uvc->vdev.release = video_device_release_empty; |
| 437 | uvc->vdev.vfl_dir = VFL_DIR_TX; |
| 438 | uvc->vdev.lock = &uvc->video.mutex; |
| 439 | strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name)); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 440 | |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 441 | video_set_drvdata(&uvc->vdev, uvc); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 442 | |
Kieran Bingham | d7af78b | 2018-05-24 17:16:12 +0100 | [diff] [blame] | 443 | ret = video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1); |
| 444 | if (ret < 0) |
| 445 | return ret; |
| 446 | |
| 447 | ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name); |
| 448 | if (ret < 0) { |
| 449 | video_unregister_device(&uvc->vdev); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | return 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ |
| 457 | do { \ |
| 458 | memcpy(mem, desc, (desc)->bLength); \ |
| 459 | *(dst)++ = mem; \ |
| 460 | mem += (desc)->bLength; \ |
| 461 | } while (0); |
| 462 | |
| 463 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ |
| 464 | do { \ |
| 465 | const struct usb_descriptor_header * const *__src; \ |
| 466 | for (__src = src; *__src; ++__src) { \ |
| 467 | memcpy(mem, *__src, (*__src)->bLength); \ |
| 468 | *dst++ = mem; \ |
| 469 | mem += (*__src)->bLength; \ |
| 470 | } \ |
| 471 | } while (0) |
| 472 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 473 | static struct usb_descriptor_header ** |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 474 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) |
| 475 | { |
| 476 | struct uvc_input_header_descriptor *uvc_streaming_header; |
| 477 | struct uvc_header_descriptor *uvc_control_header; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 478 | const struct uvc_descriptor_header * const *uvc_control_desc; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 479 | const struct uvc_descriptor_header * const *uvc_streaming_cls; |
| 480 | const struct usb_descriptor_header * const *uvc_streaming_std; |
| 481 | const struct usb_descriptor_header * const *src; |
| 482 | struct usb_descriptor_header **dst; |
| 483 | struct usb_descriptor_header **hdr; |
| 484 | unsigned int control_size; |
| 485 | unsigned int streaming_size; |
| 486 | unsigned int n_desc; |
| 487 | unsigned int bytes; |
| 488 | void *mem; |
| 489 | |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 490 | switch (speed) { |
| 491 | case USB_SPEED_SUPER: |
| 492 | uvc_control_desc = uvc->desc.ss_control; |
| 493 | uvc_streaming_cls = uvc->desc.ss_streaming; |
| 494 | uvc_streaming_std = uvc_ss_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 495 | break; |
| 496 | |
| 497 | case USB_SPEED_HIGH: |
| 498 | uvc_control_desc = uvc->desc.fs_control; |
| 499 | uvc_streaming_cls = uvc->desc.hs_streaming; |
| 500 | uvc_streaming_std = uvc_hs_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case USB_SPEED_FULL: |
| 504 | default: |
| 505 | uvc_control_desc = uvc->desc.fs_control; |
| 506 | uvc_streaming_cls = uvc->desc.fs_streaming; |
| 507 | uvc_streaming_std = uvc_fs_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 508 | break; |
| 509 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 510 | |
Andrzej Pietrasiewicz | 6c25955 | 2014-12-10 12:34:01 +0100 | [diff] [blame] | 511 | if (!uvc_control_desc || !uvc_streaming_cls) |
| 512 | return ERR_PTR(-ENODEV); |
| 513 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 514 | /* Descriptors layout |
| 515 | * |
| 516 | * uvc_iad |
| 517 | * uvc_control_intf |
| 518 | * Class-specific UVC control descriptors |
| 519 | * uvc_control_ep |
| 520 | * uvc_control_cs_ep |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 521 | * uvc_ss_control_comp (for SS only) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 522 | * uvc_streaming_intf_alt0 |
| 523 | * Class-specific UVC streaming descriptors |
| 524 | * uvc_{fs|hs}_streaming |
| 525 | */ |
| 526 | |
| 527 | /* Count descriptors and compute their size. */ |
| 528 | control_size = 0; |
| 529 | streaming_size = 0; |
| 530 | bytes = uvc_iad.bLength + uvc_control_intf.bLength |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 531 | + uvc_control_ep.bLength + uvc_control_cs_ep.bLength |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 532 | + uvc_streaming_intf_alt0.bLength; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 533 | |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 534 | if (speed == USB_SPEED_SUPER) { |
| 535 | bytes += uvc_ss_control_comp.bLength; |
| 536 | n_desc = 6; |
| 537 | } else { |
| 538 | n_desc = 5; |
| 539 | } |
| 540 | |
| 541 | for (src = (const struct usb_descriptor_header **)uvc_control_desc; |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 542 | *src; ++src) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 543 | control_size += (*src)->bLength; |
| 544 | bytes += (*src)->bLength; |
| 545 | n_desc++; |
| 546 | } |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 547 | for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 548 | *src; ++src) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 549 | streaming_size += (*src)->bLength; |
| 550 | bytes += (*src)->bLength; |
| 551 | n_desc++; |
| 552 | } |
| 553 | for (src = uvc_streaming_std; *src; ++src) { |
| 554 | bytes += (*src)->bLength; |
| 555 | n_desc++; |
| 556 | } |
| 557 | |
| 558 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); |
| 559 | if (mem == NULL) |
| 560 | return NULL; |
| 561 | |
| 562 | hdr = mem; |
| 563 | dst = mem; |
| 564 | mem += (n_desc + 1) * sizeof(*src); |
| 565 | |
| 566 | /* Copy the descriptors. */ |
| 567 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); |
| 568 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); |
| 569 | |
| 570 | uvc_control_header = mem; |
| 571 | UVC_COPY_DESCRIPTORS(mem, dst, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 572 | (const struct usb_descriptor_header **)uvc_control_desc); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 573 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); |
| 574 | uvc_control_header->bInCollection = 1; |
| 575 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; |
| 576 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 577 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 578 | if (speed == USB_SPEED_SUPER) |
| 579 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); |
| 580 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 581 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); |
| 582 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); |
| 583 | |
| 584 | uvc_streaming_header = mem; |
| 585 | UVC_COPY_DESCRIPTORS(mem, dst, |
| 586 | (const struct usb_descriptor_header**)uvc_streaming_cls); |
| 587 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); |
Laurent Pinchart | 0485ec0 | 2013-03-01 20:46:27 +0100 | [diff] [blame] | 588 | uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 589 | |
| 590 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); |
| 591 | |
| 592 | *dst = NULL; |
| 593 | return hdr; |
| 594 | } |
| 595 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 596 | static int |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 597 | uvc_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 598 | { |
| 599 | struct usb_composite_dev *cdev = c->cdev; |
| 600 | struct uvc_device *uvc = to_uvc(f); |
Andrzej Pietrasiewicz | 1344379 | 2014-09-09 02:02:13 +0300 | [diff] [blame] | 601 | struct usb_string *us; |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 602 | unsigned int max_packet_mult; |
| 603 | unsigned int max_packet_size; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 604 | struct usb_ep *ep; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 605 | struct f_uvc_opts *opts; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 606 | int ret = -EINVAL; |
| 607 | |
| 608 | INFO(cdev, "uvc_function_bind\n"); |
| 609 | |
Andrzej Pietrasiewicz | bbea6de1 | 2014-12-10 12:34:00 +0100 | [diff] [blame] | 610 | opts = fi_to_f_uvc_opts(f->fi); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 611 | /* Sanity check the streaming endpoint module parameters. |
| 612 | */ |
| 613 | opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); |
| 614 | opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); |
| 615 | opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); |
| 616 | |
Roger Quadros | 16bb05d | 2017-03-08 16:05:44 +0200 | [diff] [blame] | 617 | /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */ |
| 618 | if (opts->streaming_maxburst && |
| 619 | (opts->streaming_maxpacket % 1024) != 0) { |
| 620 | opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024); |
| 621 | INFO(cdev, "overriding streaming_maxpacket to %d\n", |
| 622 | opts->streaming_maxpacket); |
| 623 | } |
| 624 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 625 | /* Fill in the FS/HS/SS Video Streaming specific descriptors from the |
| 626 | * module parameters. |
| 627 | * |
| 628 | * NOTE: We assume that the user knows what they are doing and won't |
| 629 | * give parameters that their UDC doesn't support. |
| 630 | */ |
| 631 | if (opts->streaming_maxpacket <= 1024) { |
| 632 | max_packet_mult = 1; |
| 633 | max_packet_size = opts->streaming_maxpacket; |
| 634 | } else if (opts->streaming_maxpacket <= 2048) { |
| 635 | max_packet_mult = 2; |
| 636 | max_packet_size = opts->streaming_maxpacket / 2; |
| 637 | } else { |
| 638 | max_packet_mult = 3; |
| 639 | max_packet_size = opts->streaming_maxpacket / 3; |
| 640 | } |
| 641 | |
| 642 | uvc_fs_streaming_ep.wMaxPacketSize = |
Laurent Pinchart | e102609 | 2014-09-16 17:26:47 +0300 | [diff] [blame] | 643 | cpu_to_le16(min(opts->streaming_maxpacket, 1023U)); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 644 | uvc_fs_streaming_ep.bInterval = opts->streaming_interval; |
| 645 | |
Laurent Pinchart | e102609 | 2014-09-16 17:26:47 +0300 | [diff] [blame] | 646 | uvc_hs_streaming_ep.wMaxPacketSize = |
| 647 | cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11)); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 648 | uvc_hs_streaming_ep.bInterval = opts->streaming_interval; |
| 649 | |
Laurent Pinchart | e102609 | 2014-09-16 17:26:47 +0300 | [diff] [blame] | 650 | uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 651 | uvc_ss_streaming_ep.bInterval = opts->streaming_interval; |
| 652 | uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; |
| 653 | uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; |
| 654 | uvc_ss_streaming_comp.wBytesPerInterval = |
Laurent Pinchart | e102609 | 2014-09-16 17:26:47 +0300 | [diff] [blame] | 655 | cpu_to_le16(max_packet_size * max_packet_mult * |
Roger Quadros | 09424c5 | 2017-03-08 16:05:43 +0200 | [diff] [blame] | 656 | (opts->streaming_maxburst + 1)); |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 657 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 658 | /* Allocate endpoints. */ |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 659 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 660 | if (!ep) { |
| 661 | INFO(cdev, "Unable to allocate control EP\n"); |
| 662 | goto error; |
| 663 | } |
| 664 | uvc->control_ep = ep; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 665 | |
Laurent Pinchart | 0485ec0 | 2013-03-01 20:46:27 +0100 | [diff] [blame] | 666 | if (gadget_is_superspeed(c->cdev->gadget)) |
| 667 | ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, |
| 668 | &uvc_ss_streaming_comp); |
| 669 | else if (gadget_is_dualspeed(cdev->gadget)) |
| 670 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); |
| 671 | else |
| 672 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); |
| 673 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 674 | if (!ep) { |
| 675 | INFO(cdev, "Unable to allocate streaming EP\n"); |
| 676 | goto error; |
| 677 | } |
| 678 | uvc->video.ep = ep; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 679 | |
Laurent Pinchart | 0485ec0 | 2013-03-01 20:46:27 +0100 | [diff] [blame] | 680 | uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 681 | uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 682 | uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 683 | |
Andrzej Pietrasiewicz | 1344379 | 2014-09-09 02:02:13 +0300 | [diff] [blame] | 684 | us = usb_gstrings_attach(cdev, uvc_function_strings, |
| 685 | ARRAY_SIZE(uvc_en_us_strings)); |
| 686 | if (IS_ERR(us)) { |
| 687 | ret = PTR_ERR(us); |
| 688 | goto error; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 689 | } |
Andrzej Pietrasiewicz | 1344379 | 2014-09-09 02:02:13 +0300 | [diff] [blame] | 690 | uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id; |
| 691 | uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id; |
| 692 | ret = us[UVC_STRING_STREAMING_IDX].id; |
| 693 | uvc_streaming_intf_alt0.iInterface = ret; |
| 694 | uvc_streaming_intf_alt1.iInterface = ret; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 695 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 696 | /* Allocate interface IDs. */ |
| 697 | if ((ret = usb_interface_id(c, f)) < 0) |
| 698 | goto error; |
| 699 | uvc_iad.bFirstInterface = ret; |
| 700 | uvc_control_intf.bInterfaceNumber = ret; |
| 701 | uvc->control_intf = ret; |
Laurent Pinchart | bf71544 | 2018-05-23 18:47:56 +0300 | [diff] [blame^] | 702 | opts->control_interface = ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 703 | |
| 704 | if ((ret = usb_interface_id(c, f)) < 0) |
| 705 | goto error; |
| 706 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; |
| 707 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; |
| 708 | uvc->streaming_intf = ret; |
Laurent Pinchart | bf71544 | 2018-05-23 18:47:56 +0300 | [diff] [blame^] | 709 | opts->streaming_interface = ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 710 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 711 | /* Copy descriptors */ |
| 712 | f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); |
Andrzej Pietrasiewicz | 6c25955 | 2014-12-10 12:34:01 +0100 | [diff] [blame] | 713 | if (IS_ERR(f->fs_descriptors)) { |
| 714 | ret = PTR_ERR(f->fs_descriptors); |
| 715 | f->fs_descriptors = NULL; |
| 716 | goto error; |
| 717 | } |
| 718 | if (gadget_is_dualspeed(cdev->gadget)) { |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 719 | f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); |
Andrzej Pietrasiewicz | 6c25955 | 2014-12-10 12:34:01 +0100 | [diff] [blame] | 720 | if (IS_ERR(f->hs_descriptors)) { |
| 721 | ret = PTR_ERR(f->hs_descriptors); |
| 722 | f->hs_descriptors = NULL; |
| 723 | goto error; |
| 724 | } |
| 725 | } |
| 726 | if (gadget_is_superspeed(c->cdev->gadget)) { |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 727 | f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); |
Andrzej Pietrasiewicz | 6c25955 | 2014-12-10 12:34:01 +0100 | [diff] [blame] | 728 | if (IS_ERR(f->ss_descriptors)) { |
| 729 | ret = PTR_ERR(f->ss_descriptors); |
| 730 | f->ss_descriptors = NULL; |
| 731 | goto error; |
| 732 | } |
| 733 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 734 | |
| 735 | /* Preallocate control endpoint request. */ |
| 736 | uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); |
| 737 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); |
| 738 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { |
| 739 | ret = -ENOMEM; |
| 740 | goto error; |
| 741 | } |
| 742 | |
| 743 | uvc->control_req->buf = uvc->control_buf; |
| 744 | uvc->control_req->complete = uvc_function_ep0_complete; |
| 745 | uvc->control_req->context = uvc; |
| 746 | |
Hans Verkuil | b60f9aa | 2013-06-12 06:02:38 -0300 | [diff] [blame] | 747 | if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { |
| 748 | printk(KERN_INFO "v4l2_device_register failed\n"); |
| 749 | goto error; |
| 750 | } |
| 751 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 752 | /* Initialise video. */ |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame] | 753 | ret = uvcg_video_init(&uvc->video); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 754 | if (ret < 0) |
| 755 | goto error; |
| 756 | |
| 757 | /* Register a V4L2 device. */ |
| 758 | ret = uvc_register_video(uvc); |
| 759 | if (ret < 0) { |
| 760 | printk(KERN_INFO "Unable to register video device\n"); |
| 761 | goto error; |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | |
| 766 | error: |
Hans Verkuil | b60f9aa | 2013-06-12 06:02:38 -0300 | [diff] [blame] | 767 | v4l2_device_unregister(&uvc->v4l2_dev); |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 768 | |
Andrzej Pietrasiewicz | e737985 | 2014-08-21 16:54:45 +0200 | [diff] [blame] | 769 | if (uvc->control_req) |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 770 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
Andrzej Pietrasiewicz | e737985 | 2014-08-21 16:54:45 +0200 | [diff] [blame] | 771 | kfree(uvc->control_buf); |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 772 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 773 | usb_free_all_descriptors(f); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 774 | return ret; |
| 775 | } |
| 776 | |
| 777 | /* -------------------------------------------------------------------------- |
| 778 | * USB gadget function |
| 779 | */ |
| 780 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 781 | static void uvc_free_inst(struct usb_function_instance *f) |
| 782 | { |
Andrzej Pietrasiewicz | bbea6de1 | 2014-12-10 12:34:00 +0100 | [diff] [blame] | 783 | struct f_uvc_opts *opts = fi_to_f_uvc_opts(f); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 784 | |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 785 | mutex_destroy(&opts->lock); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 786 | kfree(opts); |
| 787 | } |
| 788 | |
| 789 | static struct usb_function_instance *uvc_alloc_inst(void) |
| 790 | { |
| 791 | struct f_uvc_opts *opts; |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 792 | struct uvc_camera_terminal_descriptor *cd; |
| 793 | struct uvc_processing_unit_descriptor *pd; |
| 794 | struct uvc_output_terminal_descriptor *od; |
| 795 | struct uvc_color_matching_descriptor *md; |
| 796 | struct uvc_descriptor_header **ctl_cls; |
Laurent Pinchart | efbf0af | 2018-05-24 17:49:34 +0300 | [diff] [blame] | 797 | int ret; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 798 | |
| 799 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 800 | if (!opts) |
| 801 | return ERR_PTR(-ENOMEM); |
| 802 | opts->func_inst.free_func_inst = uvc_free_inst; |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 803 | mutex_init(&opts->lock); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 804 | |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 805 | cd = &opts->uvc_camera_terminal; |
| 806 | cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3); |
| 807 | cd->bDescriptorType = USB_DT_CS_INTERFACE; |
| 808 | cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL; |
| 809 | cd->bTerminalID = 1; |
| 810 | cd->wTerminalType = cpu_to_le16(0x0201); |
| 811 | cd->bAssocTerminal = 0; |
| 812 | cd->iTerminal = 0; |
| 813 | cd->wObjectiveFocalLengthMin = cpu_to_le16(0); |
| 814 | cd->wObjectiveFocalLengthMax = cpu_to_le16(0); |
| 815 | cd->wOcularFocalLength = cpu_to_le16(0); |
| 816 | cd->bControlSize = 3; |
| 817 | cd->bmControls[0] = 2; |
| 818 | cd->bmControls[1] = 0; |
| 819 | cd->bmControls[2] = 0; |
| 820 | |
| 821 | pd = &opts->uvc_processing; |
| 822 | pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2); |
| 823 | pd->bDescriptorType = USB_DT_CS_INTERFACE; |
| 824 | pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT; |
| 825 | pd->bUnitID = 2; |
| 826 | pd->bSourceID = 1; |
| 827 | pd->wMaxMultiplier = cpu_to_le16(16*1024); |
| 828 | pd->bControlSize = 2; |
| 829 | pd->bmControls[0] = 1; |
| 830 | pd->bmControls[1] = 0; |
| 831 | pd->iProcessing = 0; |
| 832 | |
| 833 | od = &opts->uvc_output_terminal; |
| 834 | od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE; |
| 835 | od->bDescriptorType = USB_DT_CS_INTERFACE; |
| 836 | od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL; |
| 837 | od->bTerminalID = 3; |
| 838 | od->wTerminalType = cpu_to_le16(0x0101); |
| 839 | od->bAssocTerminal = 0; |
| 840 | od->bSourceID = 2; |
| 841 | od->iTerminal = 0; |
| 842 | |
| 843 | md = &opts->uvc_color_matching; |
| 844 | md->bLength = UVC_DT_COLOR_MATCHING_SIZE; |
| 845 | md->bDescriptorType = USB_DT_CS_INTERFACE; |
| 846 | md->bDescriptorSubType = UVC_VS_COLORFORMAT; |
| 847 | md->bColorPrimaries = 1; |
| 848 | md->bTransferCharacteristics = 1; |
| 849 | md->bMatrixCoefficients = 4; |
| 850 | |
| 851 | /* Prepare fs control class descriptors for configfs-based gadgets */ |
| 852 | ctl_cls = opts->uvc_fs_control_cls; |
| 853 | ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ |
| 854 | ctl_cls[1] = (struct uvc_descriptor_header *)cd; |
| 855 | ctl_cls[2] = (struct uvc_descriptor_header *)pd; |
| 856 | ctl_cls[3] = (struct uvc_descriptor_header *)od; |
| 857 | ctl_cls[4] = NULL; /* NULL-terminate */ |
| 858 | opts->fs_control = |
| 859 | (const struct uvc_descriptor_header * const *)ctl_cls; |
| 860 | |
| 861 | /* Prepare hs control class descriptors for configfs-based gadgets */ |
| 862 | ctl_cls = opts->uvc_ss_control_cls; |
| 863 | ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ |
| 864 | ctl_cls[1] = (struct uvc_descriptor_header *)cd; |
| 865 | ctl_cls[2] = (struct uvc_descriptor_header *)pd; |
| 866 | ctl_cls[3] = (struct uvc_descriptor_header *)od; |
| 867 | ctl_cls[4] = NULL; /* NULL-terminate */ |
| 868 | opts->ss_control = |
| 869 | (const struct uvc_descriptor_header * const *)ctl_cls; |
| 870 | |
| 871 | opts->streaming_interval = 1; |
| 872 | opts->streaming_maxpacket = 1024; |
| 873 | |
Laurent Pinchart | efbf0af | 2018-05-24 17:49:34 +0300 | [diff] [blame] | 874 | ret = uvcg_attach_configfs(opts); |
| 875 | if (ret < 0) { |
| 876 | kfree(opts); |
| 877 | return ERR_PTR(ret); |
| 878 | } |
| 879 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 880 | return &opts->func_inst; |
| 881 | } |
| 882 | |
| 883 | static void uvc_free(struct usb_function *f) |
| 884 | { |
| 885 | struct uvc_device *uvc = to_uvc(f); |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 886 | struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts, |
| 887 | func_inst); |
| 888 | --opts->refcnt; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 889 | kfree(uvc); |
| 890 | } |
| 891 | |
| 892 | static void uvc_unbind(struct usb_configuration *c, struct usb_function *f) |
| 893 | { |
| 894 | struct usb_composite_dev *cdev = c->cdev; |
| 895 | struct uvc_device *uvc = to_uvc(f); |
| 896 | |
| 897 | INFO(cdev, "%s\n", __func__); |
| 898 | |
Kieran Bingham | d7af78b | 2018-05-24 17:16:12 +0100 | [diff] [blame] | 899 | device_remove_file(&uvc->vdev.dev, &dev_attr_function_name); |
Hans Verkuil | dbe98b3 | 2015-03-09 13:34:08 -0300 | [diff] [blame] | 900 | video_unregister_device(&uvc->vdev); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 901 | v4l2_device_unregister(&uvc->v4l2_dev); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 902 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 903 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
| 904 | kfree(uvc->control_buf); |
| 905 | |
| 906 | usb_free_all_descriptors(f); |
| 907 | } |
| 908 | |
Fengguang Wu | 4a6698b | 2014-09-16 17:26:46 +0300 | [diff] [blame] | 909 | static struct usb_function *uvc_alloc(struct usb_function_instance *fi) |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 910 | { |
| 911 | struct uvc_device *uvc; |
| 912 | struct f_uvc_opts *opts; |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 913 | struct uvc_descriptor_header **strm_cls; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 914 | |
| 915 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); |
| 916 | if (uvc == NULL) |
| 917 | return ERR_PTR(-ENOMEM); |
| 918 | |
Hans Verkuil | d8e96c4 | 2015-02-17 05:44:06 -0300 | [diff] [blame] | 919 | mutex_init(&uvc->video.mutex); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 920 | uvc->state = UVC_STATE_DISCONNECTED; |
Andrzej Pietrasiewicz | bbea6de1 | 2014-12-10 12:34:00 +0100 | [diff] [blame] | 921 | opts = fi_to_f_uvc_opts(fi); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 922 | |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 923 | mutex_lock(&opts->lock); |
| 924 | if (opts->uvc_fs_streaming_cls) { |
| 925 | strm_cls = opts->uvc_fs_streaming_cls; |
| 926 | opts->fs_streaming = |
| 927 | (const struct uvc_descriptor_header * const *)strm_cls; |
| 928 | } |
| 929 | if (opts->uvc_hs_streaming_cls) { |
| 930 | strm_cls = opts->uvc_hs_streaming_cls; |
| 931 | opts->hs_streaming = |
| 932 | (const struct uvc_descriptor_header * const *)strm_cls; |
| 933 | } |
| 934 | if (opts->uvc_ss_streaming_cls) { |
| 935 | strm_cls = opts->uvc_ss_streaming_cls; |
| 936 | opts->ss_streaming = |
| 937 | (const struct uvc_descriptor_header * const *)strm_cls; |
| 938 | } |
| 939 | |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 940 | uvc->desc.fs_control = opts->fs_control; |
| 941 | uvc->desc.ss_control = opts->ss_control; |
| 942 | uvc->desc.fs_streaming = opts->fs_streaming; |
| 943 | uvc->desc.hs_streaming = opts->hs_streaming; |
| 944 | uvc->desc.ss_streaming = opts->ss_streaming; |
Andrzej Pietrasiewicz | 46919a2 | 2014-12-10 12:34:02 +0100 | [diff] [blame] | 945 | ++opts->refcnt; |
| 946 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 947 | |
| 948 | /* Register the function. */ |
| 949 | uvc->func.name = "uvc"; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 950 | uvc->func.bind = uvc_function_bind; |
| 951 | uvc->func.unbind = uvc_unbind; |
| 952 | uvc->func.get_alt = uvc_function_get_alt; |
| 953 | uvc->func.set_alt = uvc_function_set_alt; |
| 954 | uvc->func.disable = uvc_function_disable; |
| 955 | uvc->func.setup = uvc_function_setup; |
| 956 | uvc->func.free_func = uvc_free; |
Robert Baldyga | f277bf2 | 2015-05-04 14:55:14 +0200 | [diff] [blame] | 957 | uvc->func.bind_deactivated = true; |
Andrzej Pietrasiewicz | 6d11ed7 | 2014-09-09 02:02:10 +0300 | [diff] [blame] | 958 | |
| 959 | return &uvc->func; |
| 960 | } |
| 961 | |
| 962 | DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); |
| 963 | MODULE_LICENSE("GPL"); |
| 964 | MODULE_AUTHOR("Laurent Pinchart"); |