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