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> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/mutex.h> |
Chen Gang | 4d2079c | 2013-02-02 15:48:54 +0800 | [diff] [blame] | 19 | #include <linux/string.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 20 | #include <linux/usb/ch9.h> |
| 21 | #include <linux/usb/gadget.h> |
| 22 | #include <linux/usb/video.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/wait.h> |
| 25 | |
| 26 | #include <media/v4l2-dev.h> |
| 27 | #include <media/v4l2-event.h> |
| 28 | |
| 29 | #include "uvc.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; |
Andrzej Pietrasiewicz | efb540c | 2014-09-08 11:18:16 +0300 | [diff] [blame] | 34 | static unsigned int streaming_interval; |
| 35 | static unsigned int streaming_maxpacket; |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 36 | static unsigned int streaming_maxburst; |
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 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 68 | static struct usb_interface_assoc_descriptor uvc_iad __initdata = { |
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 | |
| 79 | static struct usb_interface_descriptor uvc_control_intf __initdata = { |
| 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 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 91 | static struct usb_endpoint_descriptor uvc_control_ep __initdata = { |
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 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 100 | static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = { |
| 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 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 109 | static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = { |
| 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 | |
| 116 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = { |
| 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 | |
| 128 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = { |
| 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 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 140 | static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = { |
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 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 151 | static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = { |
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 | |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 162 | static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = { |
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 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 174 | static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = { |
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 | |
| 201 | /* -------------------------------------------------------------------------- |
| 202 | * Control requests |
| 203 | */ |
| 204 | |
| 205 | static void |
| 206 | uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) |
| 207 | { |
| 208 | struct uvc_device *uvc = req->context; |
| 209 | struct v4l2_event v4l2_event; |
| 210 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 211 | |
| 212 | if (uvc->event_setup_out) { |
| 213 | uvc->event_setup_out = 0; |
| 214 | |
| 215 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 216 | v4l2_event.type = UVC_EVENT_DATA; |
| 217 | uvc_event->data.length = req->actual; |
| 218 | memcpy(&uvc_event->data.data, req->buf, req->actual); |
| 219 | v4l2_event_queue(uvc->vdev, &v4l2_event); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static int |
| 224 | uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 225 | { |
| 226 | struct uvc_device *uvc = to_uvc(f); |
| 227 | struct v4l2_event v4l2_event; |
| 228 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 229 | |
| 230 | /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", |
| 231 | * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), |
| 232 | * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); |
| 233 | */ |
| 234 | |
| 235 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { |
| 236 | INFO(f->config->cdev, "invalid request type\n"); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | /* Stall too big requests. */ |
| 241 | if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) |
| 242 | return -EINVAL; |
| 243 | |
Laurent Pinchart | 26a029f | 2014-09-08 11:18:14 +0300 | [diff] [blame] | 244 | /* Tell the complete callback to generate an event for the next request |
| 245 | * that will be enqueued by UVCIOC_SEND_RESPONSE. |
| 246 | */ |
| 247 | uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); |
| 248 | uvc->event_length = le16_to_cpu(ctrl->wLength); |
| 249 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 250 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 251 | v4l2_event.type = UVC_EVENT_SETUP; |
| 252 | memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); |
| 253 | v4l2_event_queue(uvc->vdev, &v4l2_event); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Bhupesh Sharma | 41837c3 | 2013-03-01 20:46:30 +0100 | [diff] [blame] | 258 | void uvc_function_setup_continue(struct uvc_device *uvc) |
| 259 | { |
| 260 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 261 | |
| 262 | usb_composite_setup_continue(cdev); |
| 263 | } |
| 264 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 265 | static int |
| 266 | uvc_function_get_alt(struct usb_function *f, unsigned interface) |
| 267 | { |
| 268 | struct uvc_device *uvc = to_uvc(f); |
| 269 | |
| 270 | INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); |
| 271 | |
| 272 | if (interface == uvc->control_intf) |
| 273 | return 0; |
| 274 | else if (interface != uvc->streaming_intf) |
| 275 | return -EINVAL; |
| 276 | else |
| 277 | return uvc->state == UVC_STATE_STREAMING ? 1 : 0; |
| 278 | } |
| 279 | |
| 280 | static int |
| 281 | uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) |
| 282 | { |
| 283 | struct uvc_device *uvc = to_uvc(f); |
| 284 | struct v4l2_event v4l2_event; |
| 285 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 286 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 287 | |
| 288 | INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); |
| 289 | |
| 290 | if (interface == uvc->control_intf) { |
| 291 | if (alt) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | if (uvc->state == UVC_STATE_DISCONNECTED) { |
| 295 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 296 | v4l2_event.type = UVC_EVENT_CONNECT; |
| 297 | uvc_event->speed = f->config->cdev->gadget->speed; |
| 298 | v4l2_event_queue(uvc->vdev, &v4l2_event); |
| 299 | |
| 300 | uvc->state = UVC_STATE_CONNECTED; |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | if (interface != uvc->streaming_intf) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | /* TODO |
| 310 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) |
| 311 | return alt ? -EINVAL : 0; |
| 312 | */ |
| 313 | |
| 314 | switch (alt) { |
| 315 | case 0: |
| 316 | if (uvc->state != UVC_STATE_STREAMING) |
| 317 | return 0; |
| 318 | |
| 319 | if (uvc->video.ep) |
| 320 | usb_ep_disable(uvc->video.ep); |
| 321 | |
| 322 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 323 | v4l2_event.type = UVC_EVENT_STREAMOFF; |
| 324 | v4l2_event_queue(uvc->vdev, &v4l2_event); |
| 325 | |
| 326 | uvc->state = UVC_STATE_CONNECTED; |
Bhupesh Sharma | 41837c3 | 2013-03-01 20:46:30 +0100 | [diff] [blame] | 327 | return 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 328 | |
| 329 | case 1: |
| 330 | if (uvc->state != UVC_STATE_CONNECTED) |
| 331 | return 0; |
| 332 | |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 333 | if (uvc->video.ep) { |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 334 | ret = config_ep_by_speed(f->config->cdev->gadget, |
| 335 | &(uvc->func), uvc->video.ep); |
| 336 | if (ret) |
| 337 | return ret; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 338 | usb_ep_enable(uvc->video.ep); |
| 339 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 340 | |
| 341 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 342 | v4l2_event.type = UVC_EVENT_STREAMON; |
| 343 | v4l2_event_queue(uvc->vdev, &v4l2_event); |
Bhupesh Sharma | 41837c3 | 2013-03-01 20:46:30 +0100 | [diff] [blame] | 344 | return USB_GADGET_DELAYED_STATUS; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 345 | |
| 346 | default: |
| 347 | return -EINVAL; |
| 348 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static void |
| 352 | uvc_function_disable(struct usb_function *f) |
| 353 | { |
| 354 | struct uvc_device *uvc = to_uvc(f); |
| 355 | struct v4l2_event v4l2_event; |
| 356 | |
| 357 | INFO(f->config->cdev, "uvc_function_disable\n"); |
| 358 | |
| 359 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 360 | v4l2_event.type = UVC_EVENT_DISCONNECT; |
| 361 | v4l2_event_queue(uvc->vdev, &v4l2_event); |
| 362 | |
| 363 | uvc->state = UVC_STATE_DISCONNECTED; |
| 364 | } |
| 365 | |
| 366 | /* -------------------------------------------------------------------------- |
| 367 | * Connection / disconnection |
| 368 | */ |
| 369 | |
| 370 | void |
| 371 | uvc_function_connect(struct uvc_device *uvc) |
| 372 | { |
| 373 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 374 | int ret; |
| 375 | |
| 376 | if ((ret = usb_function_activate(&uvc->func)) < 0) |
| 377 | INFO(cdev, "UVC connect failed with %d\n", ret); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | uvc_function_disconnect(struct uvc_device *uvc) |
| 382 | { |
| 383 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 384 | int ret; |
| 385 | |
| 386 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) |
| 387 | INFO(cdev, "UVC disconnect failed with %d\n", ret); |
| 388 | } |
| 389 | |
| 390 | /* -------------------------------------------------------------------------- |
| 391 | * USB probe and disconnect |
| 392 | */ |
| 393 | |
| 394 | static int |
| 395 | uvc_register_video(struct uvc_device *uvc) |
| 396 | { |
| 397 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 398 | struct video_device *video; |
| 399 | |
| 400 | /* TODO reference counting. */ |
| 401 | video = video_device_alloc(); |
| 402 | if (video == NULL) |
| 403 | return -ENOMEM; |
| 404 | |
Hans Verkuil | b60f9aa | 2013-06-12 06:02:38 -0300 | [diff] [blame] | 405 | video->v4l2_dev = &uvc->v4l2_dev; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 406 | video->fops = &uvc_v4l2_fops; |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 407 | video->ioctl_ops = &uvc_v4l2_ioctl_ops; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 408 | video->release = video_device_release; |
Laurent Pinchart | a1d27a4 | 2014-09-08 11:18:15 +0300 | [diff] [blame] | 409 | video->vfl_dir = VFL_DIR_TX; |
Chen Gang | 4d2079c | 2013-02-02 15:48:54 +0800 | [diff] [blame] | 410 | strlcpy(video->name, cdev->gadget->name, sizeof(video->name)); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 411 | |
| 412 | uvc->vdev = video; |
| 413 | video_set_drvdata(video, uvc); |
| 414 | |
| 415 | return video_register_device(video, VFL_TYPE_GRABBER, -1); |
| 416 | } |
| 417 | |
| 418 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ |
| 419 | do { \ |
| 420 | memcpy(mem, desc, (desc)->bLength); \ |
| 421 | *(dst)++ = mem; \ |
| 422 | mem += (desc)->bLength; \ |
| 423 | } while (0); |
| 424 | |
| 425 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ |
| 426 | do { \ |
| 427 | const struct usb_descriptor_header * const *__src; \ |
| 428 | for (__src = src; *__src; ++__src) { \ |
| 429 | memcpy(mem, *__src, (*__src)->bLength); \ |
| 430 | *dst++ = mem; \ |
| 431 | mem += (*__src)->bLength; \ |
| 432 | } \ |
| 433 | } while (0) |
| 434 | |
| 435 | static struct usb_descriptor_header ** __init |
| 436 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) |
| 437 | { |
| 438 | struct uvc_input_header_descriptor *uvc_streaming_header; |
| 439 | struct uvc_header_descriptor *uvc_control_header; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 440 | const struct uvc_descriptor_header * const *uvc_control_desc; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 441 | const struct uvc_descriptor_header * const *uvc_streaming_cls; |
| 442 | const struct usb_descriptor_header * const *uvc_streaming_std; |
| 443 | const struct usb_descriptor_header * const *src; |
| 444 | struct usb_descriptor_header **dst; |
| 445 | struct usb_descriptor_header **hdr; |
| 446 | unsigned int control_size; |
| 447 | unsigned int streaming_size; |
| 448 | unsigned int n_desc; |
| 449 | unsigned int bytes; |
| 450 | void *mem; |
| 451 | |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 452 | switch (speed) { |
| 453 | case USB_SPEED_SUPER: |
| 454 | uvc_control_desc = uvc->desc.ss_control; |
| 455 | uvc_streaming_cls = uvc->desc.ss_streaming; |
| 456 | uvc_streaming_std = uvc_ss_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case USB_SPEED_HIGH: |
| 460 | uvc_control_desc = uvc->desc.fs_control; |
| 461 | uvc_streaming_cls = uvc->desc.hs_streaming; |
| 462 | uvc_streaming_std = uvc_hs_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case USB_SPEED_FULL: |
| 466 | default: |
| 467 | uvc_control_desc = uvc->desc.fs_control; |
| 468 | uvc_streaming_cls = uvc->desc.fs_streaming; |
| 469 | uvc_streaming_std = uvc_fs_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 470 | break; |
| 471 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 472 | |
| 473 | /* Descriptors layout |
| 474 | * |
| 475 | * uvc_iad |
| 476 | * uvc_control_intf |
| 477 | * Class-specific UVC control descriptors |
| 478 | * uvc_control_ep |
| 479 | * uvc_control_cs_ep |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 480 | * uvc_ss_control_comp (for SS only) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 481 | * uvc_streaming_intf_alt0 |
| 482 | * Class-specific UVC streaming descriptors |
| 483 | * uvc_{fs|hs}_streaming |
| 484 | */ |
| 485 | |
| 486 | /* Count descriptors and compute their size. */ |
| 487 | control_size = 0; |
| 488 | streaming_size = 0; |
| 489 | bytes = uvc_iad.bLength + uvc_control_intf.bLength |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 490 | + uvc_control_ep.bLength + uvc_control_cs_ep.bLength |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 491 | + uvc_streaming_intf_alt0.bLength; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 492 | |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 493 | if (speed == USB_SPEED_SUPER) { |
| 494 | bytes += uvc_ss_control_comp.bLength; |
| 495 | n_desc = 6; |
| 496 | } else { |
| 497 | n_desc = 5; |
| 498 | } |
| 499 | |
| 500 | for (src = (const struct usb_descriptor_header **)uvc_control_desc; |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 501 | *src; ++src) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 502 | control_size += (*src)->bLength; |
| 503 | bytes += (*src)->bLength; |
| 504 | n_desc++; |
| 505 | } |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 506 | for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 507 | *src; ++src) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 508 | streaming_size += (*src)->bLength; |
| 509 | bytes += (*src)->bLength; |
| 510 | n_desc++; |
| 511 | } |
| 512 | for (src = uvc_streaming_std; *src; ++src) { |
| 513 | bytes += (*src)->bLength; |
| 514 | n_desc++; |
| 515 | } |
| 516 | |
| 517 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); |
| 518 | if (mem == NULL) |
| 519 | return NULL; |
| 520 | |
| 521 | hdr = mem; |
| 522 | dst = mem; |
| 523 | mem += (n_desc + 1) * sizeof(*src); |
| 524 | |
| 525 | /* Copy the descriptors. */ |
| 526 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); |
| 527 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); |
| 528 | |
| 529 | uvc_control_header = mem; |
| 530 | UVC_COPY_DESCRIPTORS(mem, dst, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 531 | (const struct usb_descriptor_header **)uvc_control_desc); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 532 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); |
| 533 | uvc_control_header->bInCollection = 1; |
| 534 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; |
| 535 | |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 536 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 537 | if (speed == USB_SPEED_SUPER) |
| 538 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); |
| 539 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 540 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); |
| 541 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); |
| 542 | |
| 543 | uvc_streaming_header = mem; |
| 544 | UVC_COPY_DESCRIPTORS(mem, dst, |
| 545 | (const struct usb_descriptor_header**)uvc_streaming_cls); |
| 546 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); |
Laurent Pinchart | 0485ec0 | 2013-03-01 20:46:27 +0100 | [diff] [blame] | 547 | uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 548 | |
| 549 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); |
| 550 | |
| 551 | *dst = NULL; |
| 552 | return hdr; |
| 553 | } |
| 554 | |
| 555 | static void |
| 556 | uvc_function_unbind(struct usb_configuration *c, struct usb_function *f) |
| 557 | { |
| 558 | struct usb_composite_dev *cdev = c->cdev; |
| 559 | struct uvc_device *uvc = to_uvc(f); |
| 560 | |
| 561 | INFO(cdev, "uvc_function_unbind\n"); |
| 562 | |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 563 | video_unregister_device(uvc->vdev); |
Hans Verkuil | b60f9aa | 2013-06-12 06:02:38 -0300 | [diff] [blame] | 564 | v4l2_device_unregister(&uvc->v4l2_dev); |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 565 | uvc->control_ep->driver_data = NULL; |
| 566 | uvc->video.ep->driver_data = NULL; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 567 | |
Bhupesh Sharma | 43ff05e | 2013-03-01 20:46:29 +0100 | [diff] [blame] | 568 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0; |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 569 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
| 570 | kfree(uvc->control_buf); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 571 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 572 | usb_free_all_descriptors(f); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 573 | |
| 574 | kfree(uvc); |
| 575 | } |
| 576 | |
| 577 | static int __init |
| 578 | uvc_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 579 | { |
| 580 | struct usb_composite_dev *cdev = c->cdev; |
| 581 | struct uvc_device *uvc = to_uvc(f); |
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; |
| 585 | int ret = -EINVAL; |
| 586 | |
| 587 | INFO(cdev, "uvc_function_bind\n"); |
| 588 | |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 589 | /* Sanity check the streaming endpoint module parameters. |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 590 | */ |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 591 | streaming_interval = clamp(streaming_interval, 1U, 16U); |
| 592 | streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U); |
| 593 | streaming_maxburst = min(streaming_maxburst, 15U); |
| 594 | |
| 595 | /* Fill in the FS/HS/SS Video Streaming specific descriptors from the |
| 596 | * module parameters. |
| 597 | * |
| 598 | * NOTE: We assume that the user knows what they are doing and won't |
| 599 | * give parameters that their UDC doesn't support. |
| 600 | */ |
| 601 | if (streaming_maxpacket <= 1024) { |
| 602 | max_packet_mult = 1; |
| 603 | max_packet_size = streaming_maxpacket; |
| 604 | } else if (streaming_maxpacket <= 2048) { |
| 605 | max_packet_mult = 2; |
| 606 | max_packet_size = streaming_maxpacket / 2; |
| 607 | } else { |
| 608 | max_packet_mult = 3; |
| 609 | max_packet_size = streaming_maxpacket / 3; |
| 610 | } |
| 611 | |
| 612 | uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U); |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 613 | uvc_fs_streaming_ep.bInterval = streaming_interval; |
| 614 | |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 615 | uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size; |
| 616 | uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11); |
| 617 | uvc_hs_streaming_ep.bInterval = streaming_interval; |
| 618 | |
| 619 | uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size; |
| 620 | uvc_ss_streaming_ep.bInterval = streaming_interval; |
| 621 | uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; |
| 622 | uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst; |
| 623 | uvc_ss_streaming_comp.wBytesPerInterval = |
| 624 | max_packet_size * max_packet_mult * streaming_maxburst; |
| 625 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 626 | /* Allocate endpoints. */ |
Laurent Pinchart | 48eee0b | 2013-03-01 20:46:25 +0100 | [diff] [blame] | 627 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 628 | if (!ep) { |
| 629 | INFO(cdev, "Unable to allocate control EP\n"); |
| 630 | goto error; |
| 631 | } |
| 632 | uvc->control_ep = ep; |
| 633 | ep->driver_data = uvc; |
| 634 | |
Laurent Pinchart | 0485ec0 | 2013-03-01 20:46:27 +0100 | [diff] [blame] | 635 | if (gadget_is_superspeed(c->cdev->gadget)) |
| 636 | ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, |
| 637 | &uvc_ss_streaming_comp); |
| 638 | else if (gadget_is_dualspeed(cdev->gadget)) |
| 639 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); |
| 640 | else |
| 641 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); |
| 642 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 643 | if (!ep) { |
| 644 | INFO(cdev, "Unable to allocate streaming EP\n"); |
| 645 | goto error; |
| 646 | } |
| 647 | uvc->video.ep = ep; |
| 648 | ep->driver_data = uvc; |
| 649 | |
Laurent Pinchart | 0485ec0 | 2013-03-01 20:46:27 +0100 | [diff] [blame] | 650 | uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 651 | uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 652 | uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
Laurent Pinchart | 20777dd | 2013-03-01 20:46:26 +0100 | [diff] [blame] | 653 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 654 | /* Allocate interface IDs. */ |
| 655 | if ((ret = usb_interface_id(c, f)) < 0) |
| 656 | goto error; |
| 657 | uvc_iad.bFirstInterface = ret; |
| 658 | uvc_control_intf.bInterfaceNumber = ret; |
| 659 | uvc->control_intf = ret; |
| 660 | |
| 661 | if ((ret = usb_interface_id(c, f)) < 0) |
| 662 | goto error; |
| 663 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; |
| 664 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; |
| 665 | uvc->streaming_intf = ret; |
| 666 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 667 | /* Copy descriptors */ |
| 668 | f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); |
| 669 | if (gadget_is_dualspeed(cdev->gadget)) |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 670 | f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 671 | if (gadget_is_superspeed(c->cdev->gadget)) |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 672 | f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 673 | |
| 674 | /* Preallocate control endpoint request. */ |
| 675 | uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); |
| 676 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); |
| 677 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { |
| 678 | ret = -ENOMEM; |
| 679 | goto error; |
| 680 | } |
| 681 | |
| 682 | uvc->control_req->buf = uvc->control_buf; |
| 683 | uvc->control_req->complete = uvc_function_ep0_complete; |
| 684 | uvc->control_req->context = uvc; |
| 685 | |
| 686 | /* Avoid letting this gadget enumerate until the userspace server is |
| 687 | * active. |
| 688 | */ |
| 689 | if ((ret = usb_function_deactivate(f)) < 0) |
| 690 | goto error; |
| 691 | |
Hans Verkuil | b60f9aa | 2013-06-12 06:02:38 -0300 | [diff] [blame] | 692 | if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { |
| 693 | printk(KERN_INFO "v4l2_device_register failed\n"); |
| 694 | goto error; |
| 695 | } |
| 696 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 697 | /* Initialise video. */ |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame] | 698 | ret = uvcg_video_init(&uvc->video); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 699 | if (ret < 0) |
| 700 | goto error; |
| 701 | |
| 702 | /* Register a V4L2 device. */ |
| 703 | ret = uvc_register_video(uvc); |
| 704 | if (ret < 0) { |
| 705 | printk(KERN_INFO "Unable to register video device\n"); |
| 706 | goto error; |
| 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | |
| 711 | error: |
Hans Verkuil | b60f9aa | 2013-06-12 06:02:38 -0300 | [diff] [blame] | 712 | v4l2_device_unregister(&uvc->v4l2_dev); |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 713 | if (uvc->vdev) |
| 714 | video_device_release(uvc->vdev); |
| 715 | |
| 716 | if (uvc->control_ep) |
| 717 | uvc->control_ep->driver_data = NULL; |
| 718 | if (uvc->video.ep) |
| 719 | uvc->video.ep->driver_data = NULL; |
| 720 | |
Andrzej Pietrasiewicz | e737985 | 2014-08-21 16:54:45 +0200 | [diff] [blame] | 721 | if (uvc->control_req) |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 722 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
Andrzej Pietrasiewicz | e737985 | 2014-08-21 16:54:45 +0200 | [diff] [blame] | 723 | kfree(uvc->control_buf); |
Sebastian Andrzej Siewior | 0f9df93 | 2012-10-22 22:15:05 +0200 | [diff] [blame] | 724 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 725 | usb_free_all_descriptors(f); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | /* -------------------------------------------------------------------------- |
| 730 | * USB gadget function |
| 731 | */ |
| 732 | |
| 733 | /** |
| 734 | * uvc_bind_config - add a UVC function to a configuration |
| 735 | * @c: the configuration to support the UVC instance |
| 736 | * Context: single threaded during gadget setup |
| 737 | * |
| 738 | * Returns zero on success, else negative errno. |
| 739 | * |
| 740 | * Caller must have called @uvc_setup(). Caller is also responsible for |
| 741 | * calling @uvc_cleanup() before module unload. |
| 742 | */ |
| 743 | int __init |
| 744 | uvc_bind_config(struct usb_configuration *c, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 745 | const struct uvc_descriptor_header * const *fs_control, |
| 746 | const struct uvc_descriptor_header * const *ss_control, |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 747 | const struct uvc_descriptor_header * const *fs_streaming, |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 748 | const struct uvc_descriptor_header * const *hs_streaming, |
Andrzej Pietrasiewicz | efb540c | 2014-09-08 11:18:16 +0300 | [diff] [blame] | 749 | const struct uvc_descriptor_header * const *ss_streaming, |
| 750 | unsigned int stream_interv, unsigned int stream_maxpkt, |
| 751 | unsigned int stream_maxburst, unsigned int trace) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 752 | { |
| 753 | struct uvc_device *uvc; |
| 754 | int ret = 0; |
| 755 | |
| 756 | /* TODO Check if the USB device controller supports the required |
| 757 | * features. |
| 758 | */ |
| 759 | if (!gadget_is_dualspeed(c->cdev->gadget)) |
| 760 | return -EINVAL; |
| 761 | |
| 762 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); |
| 763 | if (uvc == NULL) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | uvc->state = UVC_STATE_DISCONNECTED; |
| 767 | |
| 768 | /* Validate the descriptors. */ |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 769 | if (fs_control == NULL || fs_control[0] == NULL || |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 770 | fs_control[0]->bDescriptorSubType != UVC_VC_HEADER) |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 771 | goto error; |
| 772 | |
| 773 | if (ss_control == NULL || ss_control[0] == NULL || |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 774 | ss_control[0]->bDescriptorSubType != UVC_VC_HEADER) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 775 | goto error; |
| 776 | |
| 777 | if (fs_streaming == NULL || fs_streaming[0] == NULL || |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 778 | fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 779 | goto error; |
| 780 | |
| 781 | if (hs_streaming == NULL || hs_streaming[0] == NULL || |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 782 | hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 783 | goto error; |
| 784 | |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 785 | if (ss_streaming == NULL || ss_streaming[0] == NULL || |
Laurent Pinchart | ee6a4d8 | 2013-03-01 20:46:24 +0100 | [diff] [blame] | 786 | ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 787 | goto error; |
| 788 | |
Andrzej Pietrasiewicz | efb540c | 2014-09-08 11:18:16 +0300 | [diff] [blame] | 789 | streaming_interval = stream_interv; |
| 790 | streaming_maxpacket = stream_maxpkt; |
| 791 | streaming_maxburst = stream_maxburst; |
| 792 | uvc_gadget_trace_param = trace; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 793 | uvc->desc.fs_control = fs_control; |
| 794 | uvc->desc.ss_control = ss_control; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 795 | uvc->desc.fs_streaming = fs_streaming; |
| 796 | uvc->desc.hs_streaming = hs_streaming; |
Bhupesh Sharma | fbcaba0 | 2012-06-01 15:08:56 +0530 | [diff] [blame] | 797 | uvc->desc.ss_streaming = ss_streaming; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 798 | |
Laurent Pinchart | f9e6120 | 2013-03-01 20:46:22 +0100 | [diff] [blame] | 799 | /* String descriptors are global, we only need to allocate string IDs |
| 800 | * for the first UVC function. UVC functions beyond the first (if any) |
| 801 | * will reuse the same IDs. |
| 802 | */ |
Bhupesh Sharma | 43ff05e | 2013-03-01 20:46:29 +0100 | [diff] [blame] | 803 | if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) { |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 804 | ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings); |
| 805 | if (ret) |
Bhupesh Sharma | 3de6e63 | 2012-06-01 15:08:54 +0530 | [diff] [blame] | 806 | goto error; |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 807 | uvc_iad.iFunction = |
Bhupesh Sharma | 43ff05e | 2013-03-01 20:46:29 +0100 | [diff] [blame] | 808 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id; |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 809 | uvc_control_intf.iInterface = |
| 810 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id; |
| 811 | ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id; |
Bhupesh Sharma | 3de6e63 | 2012-06-01 15:08:54 +0530 | [diff] [blame] | 812 | uvc_streaming_intf_alt0.iInterface = ret; |
| 813 | uvc_streaming_intf_alt1.iInterface = ret; |
| 814 | } |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 815 | |
| 816 | /* Register the function. */ |
| 817 | uvc->func.name = "uvc"; |
| 818 | uvc->func.strings = uvc_function_strings; |
| 819 | uvc->func.bind = uvc_function_bind; |
| 820 | uvc->func.unbind = uvc_function_unbind; |
| 821 | uvc->func.get_alt = uvc_function_get_alt; |
| 822 | uvc->func.set_alt = uvc_function_set_alt; |
| 823 | uvc->func.disable = uvc_function_disable; |
| 824 | uvc->func.setup = uvc_function_setup; |
| 825 | |
| 826 | ret = usb_add_function(c, &uvc->func); |
| 827 | if (ret) |
| 828 | kfree(uvc); |
| 829 | |
Jassi Brar | 28f75f4 | 2011-06-25 00:17:26 +0530 | [diff] [blame] | 830 | return ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 831 | |
| 832 | error: |
| 833 | kfree(uvc); |
| 834 | return ret; |
| 835 | } |
| 836 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 837 | |