| 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> | 
|  | 19 | #include <linux/usb/ch9.h> | 
|  | 20 | #include <linux/usb/gadget.h> | 
|  | 21 | #include <linux/usb/video.h> | 
|  | 22 | #include <linux/vmalloc.h> | 
|  | 23 | #include <linux/wait.h> | 
|  | 24 |  | 
|  | 25 | #include <media/v4l2-dev.h> | 
|  | 26 | #include <media/v4l2-event.h> | 
|  | 27 |  | 
|  | 28 | #include "uvc.h" | 
|  | 29 |  | 
| Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 30 | unsigned int uvc_gadget_trace_param; | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 31 |  | 
|  | 32 | /* -------------------------------------------------------------------------- | 
|  | 33 | * Function descriptors | 
|  | 34 | */ | 
|  | 35 |  | 
|  | 36 | /* string IDs are assigned dynamically */ | 
|  | 37 |  | 
|  | 38 | #define UVC_STRING_ASSOCIATION_IDX		0 | 
|  | 39 | #define UVC_STRING_CONTROL_IDX			1 | 
|  | 40 | #define UVC_STRING_STREAMING_IDX		2 | 
|  | 41 |  | 
|  | 42 | static struct usb_string uvc_en_us_strings[] = { | 
|  | 43 | [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera", | 
|  | 44 | [UVC_STRING_CONTROL_IDX].s = "Video Control", | 
|  | 45 | [UVC_STRING_STREAMING_IDX].s = "Video Streaming", | 
|  | 46 | {  } | 
|  | 47 | }; | 
|  | 48 |  | 
|  | 49 | static struct usb_gadget_strings uvc_stringtab = { | 
|  | 50 | .language = 0x0409,	/* en-us */ | 
|  | 51 | .strings = uvc_en_us_strings, | 
|  | 52 | }; | 
|  | 53 |  | 
|  | 54 | static struct usb_gadget_strings *uvc_function_strings[] = { | 
|  | 55 | &uvc_stringtab, | 
|  | 56 | NULL, | 
|  | 57 | }; | 
|  | 58 |  | 
|  | 59 | #define UVC_INTF_VIDEO_CONTROL			0 | 
|  | 60 | #define UVC_INTF_VIDEO_STREAMING		1 | 
|  | 61 |  | 
|  | 62 | static struct usb_interface_assoc_descriptor uvc_iad __initdata = { | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 63 | .bLength		= sizeof(uvc_iad), | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 64 | .bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION, | 
|  | 65 | .bFirstInterface	= 0, | 
|  | 66 | .bInterfaceCount	= 2, | 
|  | 67 | .bFunctionClass		= USB_CLASS_VIDEO, | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 68 | .bFunctionSubClass	= UVC_SC_VIDEO_INTERFACE_COLLECTION, | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 69 | .bFunctionProtocol	= 0x00, | 
|  | 70 | .iFunction		= 0, | 
|  | 71 | }; | 
|  | 72 |  | 
|  | 73 | static struct usb_interface_descriptor uvc_control_intf __initdata = { | 
|  | 74 | .bLength		= USB_DT_INTERFACE_SIZE, | 
|  | 75 | .bDescriptorType	= USB_DT_INTERFACE, | 
|  | 76 | .bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL, | 
|  | 77 | .bAlternateSetting	= 0, | 
|  | 78 | .bNumEndpoints		= 1, | 
|  | 79 | .bInterfaceClass	= USB_CLASS_VIDEO, | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 80 | .bInterfaceSubClass	= UVC_SC_VIDEOCONTROL, | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 81 | .bInterfaceProtocol	= 0x00, | 
|  | 82 | .iInterface		= 0, | 
|  | 83 | }; | 
|  | 84 |  | 
|  | 85 | static struct usb_endpoint_descriptor uvc_control_ep __initdata = { | 
|  | 86 | .bLength		= USB_DT_ENDPOINT_SIZE, | 
|  | 87 | .bDescriptorType	= USB_DT_ENDPOINT, | 
|  | 88 | .bEndpointAddress	= USB_DIR_IN, | 
|  | 89 | .bmAttributes		= USB_ENDPOINT_XFER_INT, | 
|  | 90 | .wMaxPacketSize		= cpu_to_le16(16), | 
|  | 91 | .bInterval		= 8, | 
|  | 92 | }; | 
|  | 93 |  | 
|  | 94 | static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = { | 
|  | 95 | .bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE, | 
|  | 96 | .bDescriptorType	= USB_DT_CS_ENDPOINT, | 
|  | 97 | .bDescriptorSubType	= UVC_EP_INTERRUPT, | 
|  | 98 | .wMaxTransferSize	= cpu_to_le16(16), | 
|  | 99 | }; | 
|  | 100 |  | 
|  | 101 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = { | 
|  | 102 | .bLength		= USB_DT_INTERFACE_SIZE, | 
|  | 103 | .bDescriptorType	= USB_DT_INTERFACE, | 
|  | 104 | .bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING, | 
|  | 105 | .bAlternateSetting	= 0, | 
|  | 106 | .bNumEndpoints		= 0, | 
|  | 107 | .bInterfaceClass	= USB_CLASS_VIDEO, | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 108 | .bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING, | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 109 | .bInterfaceProtocol	= 0x00, | 
|  | 110 | .iInterface		= 0, | 
|  | 111 | }; | 
|  | 112 |  | 
|  | 113 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = { | 
|  | 114 | .bLength		= USB_DT_INTERFACE_SIZE, | 
|  | 115 | .bDescriptorType	= USB_DT_INTERFACE, | 
|  | 116 | .bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING, | 
|  | 117 | .bAlternateSetting	= 1, | 
|  | 118 | .bNumEndpoints		= 1, | 
|  | 119 | .bInterfaceClass	= USB_CLASS_VIDEO, | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 120 | .bInterfaceSubClass	= UVC_SC_VIDEOSTREAMING, | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 121 | .bInterfaceProtocol	= 0x00, | 
|  | 122 | .iInterface		= 0, | 
|  | 123 | }; | 
|  | 124 |  | 
|  | 125 | static struct usb_endpoint_descriptor uvc_streaming_ep = { | 
|  | 126 | .bLength		= USB_DT_ENDPOINT_SIZE, | 
|  | 127 | .bDescriptorType	= USB_DT_ENDPOINT, | 
|  | 128 | .bEndpointAddress	= USB_DIR_IN, | 
|  | 129 | .bmAttributes		= USB_ENDPOINT_XFER_ISOC, | 
|  | 130 | .wMaxPacketSize		= cpu_to_le16(512), | 
|  | 131 | .bInterval		= 1, | 
|  | 132 | }; | 
|  | 133 |  | 
|  | 134 | static const struct usb_descriptor_header * const uvc_fs_streaming[] = { | 
|  | 135 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | 
|  | 136 | (struct usb_descriptor_header *) &uvc_streaming_ep, | 
|  | 137 | NULL, | 
|  | 138 | }; | 
|  | 139 |  | 
|  | 140 | static const struct usb_descriptor_header * const uvc_hs_streaming[] = { | 
|  | 141 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | 
|  | 142 | (struct usb_descriptor_header *) &uvc_streaming_ep, | 
|  | 143 | NULL, | 
|  | 144 | }; | 
|  | 145 |  | 
|  | 146 | /* -------------------------------------------------------------------------- | 
|  | 147 | * Control requests | 
|  | 148 | */ | 
|  | 149 |  | 
|  | 150 | static void | 
|  | 151 | uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) | 
|  | 152 | { | 
|  | 153 | struct uvc_device *uvc = req->context; | 
|  | 154 | struct v4l2_event v4l2_event; | 
|  | 155 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | 
|  | 156 |  | 
|  | 157 | if (uvc->event_setup_out) { | 
|  | 158 | uvc->event_setup_out = 0; | 
|  | 159 |  | 
|  | 160 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | 
|  | 161 | v4l2_event.type = UVC_EVENT_DATA; | 
|  | 162 | uvc_event->data.length = req->actual; | 
|  | 163 | memcpy(&uvc_event->data.data, req->buf, req->actual); | 
|  | 164 | v4l2_event_queue(uvc->vdev, &v4l2_event); | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | static int | 
|  | 169 | uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | 
|  | 170 | { | 
|  | 171 | struct uvc_device *uvc = to_uvc(f); | 
|  | 172 | struct v4l2_event v4l2_event; | 
|  | 173 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | 
|  | 174 |  | 
|  | 175 | /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", | 
|  | 176 | *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), | 
|  | 177 | *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); | 
|  | 178 | */ | 
|  | 179 |  | 
|  | 180 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { | 
|  | 181 | INFO(f->config->cdev, "invalid request type\n"); | 
|  | 182 | return -EINVAL; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | /* Stall too big requests. */ | 
|  | 186 | if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) | 
|  | 187 | return -EINVAL; | 
|  | 188 |  | 
|  | 189 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | 
|  | 190 | v4l2_event.type = UVC_EVENT_SETUP; | 
|  | 191 | memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); | 
|  | 192 | v4l2_event_queue(uvc->vdev, &v4l2_event); | 
|  | 193 |  | 
|  | 194 | return 0; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | static int | 
|  | 198 | uvc_function_get_alt(struct usb_function *f, unsigned interface) | 
|  | 199 | { | 
|  | 200 | struct uvc_device *uvc = to_uvc(f); | 
|  | 201 |  | 
|  | 202 | INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); | 
|  | 203 |  | 
|  | 204 | if (interface == uvc->control_intf) | 
|  | 205 | return 0; | 
|  | 206 | else if (interface != uvc->streaming_intf) | 
|  | 207 | return -EINVAL; | 
|  | 208 | else | 
|  | 209 | return uvc->state == UVC_STATE_STREAMING ? 1 : 0; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | static int | 
|  | 213 | uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) | 
|  | 214 | { | 
|  | 215 | struct uvc_device *uvc = to_uvc(f); | 
|  | 216 | struct v4l2_event v4l2_event; | 
|  | 217 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | 
|  | 218 |  | 
|  | 219 | INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); | 
|  | 220 |  | 
|  | 221 | if (interface == uvc->control_intf) { | 
|  | 222 | if (alt) | 
|  | 223 | return -EINVAL; | 
|  | 224 |  | 
|  | 225 | if (uvc->state == UVC_STATE_DISCONNECTED) { | 
|  | 226 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | 
|  | 227 | v4l2_event.type = UVC_EVENT_CONNECT; | 
|  | 228 | uvc_event->speed = f->config->cdev->gadget->speed; | 
|  | 229 | v4l2_event_queue(uvc->vdev, &v4l2_event); | 
|  | 230 |  | 
|  | 231 | uvc->state = UVC_STATE_CONNECTED; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | return 0; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | if (interface != uvc->streaming_intf) | 
|  | 238 | return -EINVAL; | 
|  | 239 |  | 
|  | 240 | /* TODO | 
|  | 241 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) | 
|  | 242 | return alt ? -EINVAL : 0; | 
|  | 243 | */ | 
|  | 244 |  | 
|  | 245 | switch (alt) { | 
|  | 246 | case 0: | 
|  | 247 | if (uvc->state != UVC_STATE_STREAMING) | 
|  | 248 | return 0; | 
|  | 249 |  | 
|  | 250 | if (uvc->video.ep) | 
|  | 251 | usb_ep_disable(uvc->video.ep); | 
|  | 252 |  | 
|  | 253 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | 
|  | 254 | v4l2_event.type = UVC_EVENT_STREAMOFF; | 
|  | 255 | v4l2_event_queue(uvc->vdev, &v4l2_event); | 
|  | 256 |  | 
|  | 257 | uvc->state = UVC_STATE_CONNECTED; | 
|  | 258 | break; | 
|  | 259 |  | 
|  | 260 | case 1: | 
|  | 261 | if (uvc->state != UVC_STATE_CONNECTED) | 
|  | 262 | return 0; | 
|  | 263 |  | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 264 | if (uvc->video.ep) { | 
|  | 265 | uvc->video.ep->desc = &uvc_streaming_ep; | 
|  | 266 | usb_ep_enable(uvc->video.ep); | 
|  | 267 | } | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 268 |  | 
|  | 269 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | 
|  | 270 | v4l2_event.type = UVC_EVENT_STREAMON; | 
|  | 271 | v4l2_event_queue(uvc->vdev, &v4l2_event); | 
|  | 272 |  | 
|  | 273 | uvc->state = UVC_STATE_STREAMING; | 
|  | 274 | break; | 
|  | 275 |  | 
|  | 276 | default: | 
|  | 277 | return -EINVAL; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | return 0; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | static void | 
|  | 284 | uvc_function_disable(struct usb_function *f) | 
|  | 285 | { | 
|  | 286 | struct uvc_device *uvc = to_uvc(f); | 
|  | 287 | struct v4l2_event v4l2_event; | 
|  | 288 |  | 
|  | 289 | INFO(f->config->cdev, "uvc_function_disable\n"); | 
|  | 290 |  | 
|  | 291 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | 
|  | 292 | v4l2_event.type = UVC_EVENT_DISCONNECT; | 
|  | 293 | v4l2_event_queue(uvc->vdev, &v4l2_event); | 
|  | 294 |  | 
|  | 295 | uvc->state = UVC_STATE_DISCONNECTED; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | /* -------------------------------------------------------------------------- | 
|  | 299 | * Connection / disconnection | 
|  | 300 | */ | 
|  | 301 |  | 
|  | 302 | void | 
|  | 303 | uvc_function_connect(struct uvc_device *uvc) | 
|  | 304 | { | 
|  | 305 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | 
|  | 306 | int ret; | 
|  | 307 |  | 
|  | 308 | if ((ret = usb_function_activate(&uvc->func)) < 0) | 
|  | 309 | INFO(cdev, "UVC connect failed with %d\n", ret); | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | void | 
|  | 313 | uvc_function_disconnect(struct uvc_device *uvc) | 
|  | 314 | { | 
|  | 315 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | 
|  | 316 | int ret; | 
|  | 317 |  | 
|  | 318 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) | 
|  | 319 | INFO(cdev, "UVC disconnect failed with %d\n", ret); | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | /* -------------------------------------------------------------------------- | 
|  | 323 | * USB probe and disconnect | 
|  | 324 | */ | 
|  | 325 |  | 
|  | 326 | static int | 
|  | 327 | uvc_register_video(struct uvc_device *uvc) | 
|  | 328 | { | 
|  | 329 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | 
|  | 330 | struct video_device *video; | 
|  | 331 |  | 
|  | 332 | /* TODO reference counting. */ | 
|  | 333 | video = video_device_alloc(); | 
|  | 334 | if (video == NULL) | 
|  | 335 | return -ENOMEM; | 
|  | 336 |  | 
|  | 337 | video->parent = &cdev->gadget->dev; | 
|  | 338 | video->minor = -1; | 
|  | 339 | video->fops = &uvc_v4l2_fops; | 
|  | 340 | video->release = video_device_release; | 
|  | 341 | strncpy(video->name, cdev->gadget->name, sizeof(video->name)); | 
|  | 342 |  | 
|  | 343 | uvc->vdev = video; | 
|  | 344 | video_set_drvdata(video, uvc); | 
|  | 345 |  | 
|  | 346 | return video_register_device(video, VFL_TYPE_GRABBER, -1); | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ | 
|  | 350 | do { \ | 
|  | 351 | memcpy(mem, desc, (desc)->bLength); \ | 
|  | 352 | *(dst)++ = mem; \ | 
|  | 353 | mem += (desc)->bLength; \ | 
|  | 354 | } while (0); | 
|  | 355 |  | 
|  | 356 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ | 
|  | 357 | do { \ | 
|  | 358 | const struct usb_descriptor_header * const *__src; \ | 
|  | 359 | for (__src = src; *__src; ++__src) { \ | 
|  | 360 | memcpy(mem, *__src, (*__src)->bLength); \ | 
|  | 361 | *dst++ = mem; \ | 
|  | 362 | mem += (*__src)->bLength; \ | 
|  | 363 | } \ | 
|  | 364 | } while (0) | 
|  | 365 |  | 
|  | 366 | static struct usb_descriptor_header ** __init | 
|  | 367 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) | 
|  | 368 | { | 
|  | 369 | struct uvc_input_header_descriptor *uvc_streaming_header; | 
|  | 370 | struct uvc_header_descriptor *uvc_control_header; | 
|  | 371 | const struct uvc_descriptor_header * const *uvc_streaming_cls; | 
|  | 372 | const struct usb_descriptor_header * const *uvc_streaming_std; | 
|  | 373 | const struct usb_descriptor_header * const *src; | 
|  | 374 | struct usb_descriptor_header **dst; | 
|  | 375 | struct usb_descriptor_header **hdr; | 
|  | 376 | unsigned int control_size; | 
|  | 377 | unsigned int streaming_size; | 
|  | 378 | unsigned int n_desc; | 
|  | 379 | unsigned int bytes; | 
|  | 380 | void *mem; | 
|  | 381 |  | 
|  | 382 | uvc_streaming_cls = (speed == USB_SPEED_FULL) | 
|  | 383 | ? uvc->desc.fs_streaming : uvc->desc.hs_streaming; | 
|  | 384 | uvc_streaming_std = (speed == USB_SPEED_FULL) | 
|  | 385 | ? uvc_fs_streaming : uvc_hs_streaming; | 
|  | 386 |  | 
|  | 387 | /* Descriptors layout | 
|  | 388 | * | 
|  | 389 | * uvc_iad | 
|  | 390 | * uvc_control_intf | 
|  | 391 | * Class-specific UVC control descriptors | 
|  | 392 | * uvc_control_ep | 
|  | 393 | * uvc_control_cs_ep | 
|  | 394 | * uvc_streaming_intf_alt0 | 
|  | 395 | * Class-specific UVC streaming descriptors | 
|  | 396 | * uvc_{fs|hs}_streaming | 
|  | 397 | */ | 
|  | 398 |  | 
|  | 399 | /* Count descriptors and compute their size. */ | 
|  | 400 | control_size = 0; | 
|  | 401 | streaming_size = 0; | 
|  | 402 | bytes = uvc_iad.bLength + uvc_control_intf.bLength | 
|  | 403 | + uvc_control_ep.bLength + uvc_control_cs_ep.bLength | 
|  | 404 | + uvc_streaming_intf_alt0.bLength; | 
|  | 405 | n_desc = 5; | 
|  | 406 |  | 
|  | 407 | for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) { | 
|  | 408 | control_size += (*src)->bLength; | 
|  | 409 | bytes += (*src)->bLength; | 
|  | 410 | n_desc++; | 
|  | 411 | } | 
|  | 412 | for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) { | 
|  | 413 | streaming_size += (*src)->bLength; | 
|  | 414 | bytes += (*src)->bLength; | 
|  | 415 | n_desc++; | 
|  | 416 | } | 
|  | 417 | for (src = uvc_streaming_std; *src; ++src) { | 
|  | 418 | bytes += (*src)->bLength; | 
|  | 419 | n_desc++; | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); | 
|  | 423 | if (mem == NULL) | 
|  | 424 | return NULL; | 
|  | 425 |  | 
|  | 426 | hdr = mem; | 
|  | 427 | dst = mem; | 
|  | 428 | mem += (n_desc + 1) * sizeof(*src); | 
|  | 429 |  | 
|  | 430 | /* Copy the descriptors. */ | 
|  | 431 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); | 
|  | 432 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); | 
|  | 433 |  | 
|  | 434 | uvc_control_header = mem; | 
|  | 435 | UVC_COPY_DESCRIPTORS(mem, dst, | 
|  | 436 | (const struct usb_descriptor_header**)uvc->desc.control); | 
|  | 437 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); | 
|  | 438 | uvc_control_header->bInCollection = 1; | 
|  | 439 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; | 
|  | 440 |  | 
|  | 441 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); | 
|  | 442 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); | 
|  | 443 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); | 
|  | 444 |  | 
|  | 445 | uvc_streaming_header = mem; | 
|  | 446 | UVC_COPY_DESCRIPTORS(mem, dst, | 
|  | 447 | (const struct usb_descriptor_header**)uvc_streaming_cls); | 
|  | 448 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); | 
|  | 449 | uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress; | 
|  | 450 |  | 
|  | 451 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); | 
|  | 452 |  | 
|  | 453 | *dst = NULL; | 
|  | 454 | return hdr; | 
|  | 455 | } | 
|  | 456 |  | 
|  | 457 | static void | 
|  | 458 | uvc_function_unbind(struct usb_configuration *c, struct usb_function *f) | 
|  | 459 | { | 
|  | 460 | struct usb_composite_dev *cdev = c->cdev; | 
|  | 461 | struct uvc_device *uvc = to_uvc(f); | 
|  | 462 |  | 
|  | 463 | INFO(cdev, "uvc_function_unbind\n"); | 
|  | 464 |  | 
|  | 465 | if (uvc->vdev) { | 
|  | 466 | if (uvc->vdev->minor == -1) | 
|  | 467 | video_device_release(uvc->vdev); | 
|  | 468 | else | 
|  | 469 | video_unregister_device(uvc->vdev); | 
|  | 470 | uvc->vdev = NULL; | 
|  | 471 | } | 
|  | 472 |  | 
|  | 473 | if (uvc->control_ep) | 
|  | 474 | uvc->control_ep->driver_data = NULL; | 
|  | 475 | if (uvc->video.ep) | 
|  | 476 | uvc->video.ep->driver_data = NULL; | 
|  | 477 |  | 
|  | 478 | if (uvc->control_req) { | 
|  | 479 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); | 
|  | 480 | kfree(uvc->control_buf); | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | kfree(f->descriptors); | 
|  | 484 | kfree(f->hs_descriptors); | 
|  | 485 |  | 
|  | 486 | kfree(uvc); | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | static int __init | 
|  | 490 | uvc_function_bind(struct usb_configuration *c, struct usb_function *f) | 
|  | 491 | { | 
|  | 492 | struct usb_composite_dev *cdev = c->cdev; | 
|  | 493 | struct uvc_device *uvc = to_uvc(f); | 
|  | 494 | struct usb_ep *ep; | 
|  | 495 | int ret = -EINVAL; | 
|  | 496 |  | 
|  | 497 | INFO(cdev, "uvc_function_bind\n"); | 
|  | 498 |  | 
|  | 499 | /* Allocate endpoints. */ | 
|  | 500 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); | 
|  | 501 | if (!ep) { | 
|  | 502 | INFO(cdev, "Unable to allocate control EP\n"); | 
|  | 503 | goto error; | 
|  | 504 | } | 
|  | 505 | uvc->control_ep = ep; | 
|  | 506 | ep->driver_data = uvc; | 
|  | 507 |  | 
|  | 508 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep); | 
|  | 509 | if (!ep) { | 
|  | 510 | INFO(cdev, "Unable to allocate streaming EP\n"); | 
|  | 511 | goto error; | 
|  | 512 | } | 
|  | 513 | uvc->video.ep = ep; | 
|  | 514 | ep->driver_data = uvc; | 
|  | 515 |  | 
|  | 516 | /* Allocate interface IDs. */ | 
|  | 517 | if ((ret = usb_interface_id(c, f)) < 0) | 
|  | 518 | goto error; | 
|  | 519 | uvc_iad.bFirstInterface = ret; | 
|  | 520 | uvc_control_intf.bInterfaceNumber = ret; | 
|  | 521 | uvc->control_intf = ret; | 
|  | 522 |  | 
|  | 523 | if ((ret = usb_interface_id(c, f)) < 0) | 
|  | 524 | goto error; | 
|  | 525 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; | 
|  | 526 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; | 
|  | 527 | uvc->streaming_intf = ret; | 
|  | 528 |  | 
|  | 529 | /* Copy descriptors. */ | 
|  | 530 | f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); | 
|  | 531 | f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); | 
|  | 532 |  | 
|  | 533 | /* Preallocate control endpoint request. */ | 
|  | 534 | uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); | 
|  | 535 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); | 
|  | 536 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { | 
|  | 537 | ret = -ENOMEM; | 
|  | 538 | goto error; | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | uvc->control_req->buf = uvc->control_buf; | 
|  | 542 | uvc->control_req->complete = uvc_function_ep0_complete; | 
|  | 543 | uvc->control_req->context = uvc; | 
|  | 544 |  | 
|  | 545 | /* Avoid letting this gadget enumerate until the userspace server is | 
|  | 546 | * active. | 
|  | 547 | */ | 
|  | 548 | if ((ret = usb_function_deactivate(f)) < 0) | 
|  | 549 | goto error; | 
|  | 550 |  | 
|  | 551 | /* Initialise video. */ | 
|  | 552 | ret = uvc_video_init(&uvc->video); | 
|  | 553 | if (ret < 0) | 
|  | 554 | goto error; | 
|  | 555 |  | 
|  | 556 | /* Register a V4L2 device. */ | 
|  | 557 | ret = uvc_register_video(uvc); | 
|  | 558 | if (ret < 0) { | 
|  | 559 | printk(KERN_INFO "Unable to register video device\n"); | 
|  | 560 | goto error; | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 | return 0; | 
|  | 564 |  | 
|  | 565 | error: | 
|  | 566 | uvc_function_unbind(c, f); | 
|  | 567 | return ret; | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 | /* -------------------------------------------------------------------------- | 
|  | 571 | * USB gadget function | 
|  | 572 | */ | 
|  | 573 |  | 
|  | 574 | /** | 
|  | 575 | * uvc_bind_config - add a UVC function to a configuration | 
|  | 576 | * @c: the configuration to support the UVC instance | 
|  | 577 | * Context: single threaded during gadget setup | 
|  | 578 | * | 
|  | 579 | * Returns zero on success, else negative errno. | 
|  | 580 | * | 
|  | 581 | * Caller must have called @uvc_setup(). Caller is also responsible for | 
|  | 582 | * calling @uvc_cleanup() before module unload. | 
|  | 583 | */ | 
|  | 584 | int __init | 
|  | 585 | uvc_bind_config(struct usb_configuration *c, | 
|  | 586 | const struct uvc_descriptor_header * const *control, | 
|  | 587 | const struct uvc_descriptor_header * const *fs_streaming, | 
|  | 588 | const struct uvc_descriptor_header * const *hs_streaming) | 
|  | 589 | { | 
|  | 590 | struct uvc_device *uvc; | 
|  | 591 | int ret = 0; | 
|  | 592 |  | 
|  | 593 | /* TODO Check if the USB device controller supports the required | 
|  | 594 | * features. | 
|  | 595 | */ | 
|  | 596 | if (!gadget_is_dualspeed(c->cdev->gadget)) | 
|  | 597 | return -EINVAL; | 
|  | 598 |  | 
|  | 599 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); | 
|  | 600 | if (uvc == NULL) | 
|  | 601 | return -ENOMEM; | 
|  | 602 |  | 
|  | 603 | uvc->state = UVC_STATE_DISCONNECTED; | 
|  | 604 |  | 
|  | 605 | /* Validate the descriptors. */ | 
|  | 606 | if (control == NULL || control[0] == NULL || | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 607 | control[0]->bDescriptorSubType != UVC_VC_HEADER) | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 608 | goto error; | 
|  | 609 |  | 
|  | 610 | if (fs_streaming == NULL || fs_streaming[0] == NULL || | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 611 | fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 612 | goto error; | 
|  | 613 |  | 
|  | 614 | if (hs_streaming == NULL || hs_streaming[0] == NULL || | 
| Laurent Pinchart | bbafc0c | 2010-07-10 15:03:20 -0300 | [diff] [blame] | 615 | hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 616 | goto error; | 
|  | 617 |  | 
|  | 618 | uvc->desc.control = control; | 
|  | 619 | uvc->desc.fs_streaming = fs_streaming; | 
|  | 620 | uvc->desc.hs_streaming = hs_streaming; | 
|  | 621 |  | 
|  | 622 | /* Allocate string descriptor numbers. */ | 
|  | 623 | if ((ret = usb_string_id(c->cdev)) < 0) | 
|  | 624 | goto error; | 
|  | 625 | uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret; | 
|  | 626 | uvc_iad.iFunction = ret; | 
|  | 627 |  | 
|  | 628 | if ((ret = usb_string_id(c->cdev)) < 0) | 
|  | 629 | goto error; | 
|  | 630 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret; | 
|  | 631 | uvc_control_intf.iInterface = ret; | 
|  | 632 |  | 
|  | 633 | if ((ret = usb_string_id(c->cdev)) < 0) | 
|  | 634 | goto error; | 
|  | 635 | uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret; | 
|  | 636 | uvc_streaming_intf_alt0.iInterface = ret; | 
|  | 637 | uvc_streaming_intf_alt1.iInterface = ret; | 
|  | 638 |  | 
|  | 639 | /* Register the function. */ | 
|  | 640 | uvc->func.name = "uvc"; | 
|  | 641 | uvc->func.strings = uvc_function_strings; | 
|  | 642 | uvc->func.bind = uvc_function_bind; | 
|  | 643 | uvc->func.unbind = uvc_function_unbind; | 
|  | 644 | uvc->func.get_alt = uvc_function_get_alt; | 
|  | 645 | uvc->func.set_alt = uvc_function_set_alt; | 
|  | 646 | uvc->func.disable = uvc_function_disable; | 
|  | 647 | uvc->func.setup = uvc_function_setup; | 
|  | 648 |  | 
|  | 649 | ret = usb_add_function(c, &uvc->func); | 
|  | 650 | if (ret) | 
|  | 651 | kfree(uvc); | 
|  | 652 |  | 
| Jassi Brar | 28f75f4 | 2011-06-25 00:17:26 +0530 | [diff] [blame] | 653 | return ret; | 
| Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 654 |  | 
|  | 655 | error: | 
|  | 656 | kfree(uvc); | 
|  | 657 | return ret; | 
|  | 658 | } | 
|  | 659 |  | 
| Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 660 | 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] | 661 | MODULE_PARM_DESC(trace, "Trace level bitmask"); | 
|  | 662 |  |