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