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