blob: 87b5306d12558e0e4fa9b915e1adbe319612d17f [file] [log] [blame]
Laurent Pinchartcdda4792010-05-02 20:57:41 +02001/*
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 Pinchartcdda4792010-05-02 20:57:41 +020011 */
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 Gang4d2079c2013-02-02 15:48:54 +080019#include <linux/string.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020020#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 Pinchart5d9955f2010-07-10 16:13:05 -030031unsigned int uvc_gadget_trace_param;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020032
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053033/*-------------------------------------------------------------------------*/
34
35/* module parameters specific to the Video streaming endpoint */
Laurent Pinchart20777dd2013-03-01 20:46:26 +010036static unsigned int streaming_interval = 1;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053037module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
38MODULE_PARM_DESC(streaming_interval, "1 - 16");
39
Laurent Pinchart20777dd2013-03-01 20:46:26 +010040static unsigned int streaming_maxpacket = 1024;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053041module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
Laurent Pinchart20777dd2013-03-01 20:46:26 +010042MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053043
Laurent Pinchart20777dd2013-03-01 20:46:26 +010044static unsigned int streaming_maxburst;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053045module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
47
Laurent Pinchartcdda4792010-05-02 20:57:41 +020048/* --------------------------------------------------------------------------
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
58static 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
65static struct usb_gadget_strings uvc_stringtab = {
66 .language = 0x0409, /* en-us */
67 .strings = uvc_en_us_strings,
68};
69
70static 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 Pinchart912ca422013-03-01 20:46:23 +010078#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053079
Laurent Pinchartcdda4792010-05-02 20:57:41 +020080static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030081 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020082 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
83 .bFirstInterface = 0,
84 .bInterfaceCount = 2,
85 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030086 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020087 .bFunctionProtocol = 0x00,
88 .iFunction = 0,
89};
90
91static 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 Pinchartbbafc0c2010-07-10 15:03:20 -030098 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020099 .bInterfaceProtocol = 0x00,
100 .iInterface = 0,
101};
102
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100103static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = USB_DIR_IN,
107 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100108 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200109 .bInterval = 8,
110};
111
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100112static 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 Pinchartcdda4792010-05-02 20:57:41 +0200121static 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 Pinchart912ca422013-03-01 20:46:23 +0100125 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200126};
127
128static 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 Pinchartbbafc0c2010-07-10 15:03:20 -0300135 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200136 .bInterfaceProtocol = 0x00,
137 .iInterface = 0,
138};
139
140static 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 Pinchartbbafc0c2010-07-10 15:03:20 -0300147 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200148 .bInterfaceProtocol = 0x00,
149 .iInterface = 0,
150};
151
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100152static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200153 .bLength = USB_DT_ENDPOINT_SIZE,
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = USB_DIR_IN,
156 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100157 /* The wMaxPacketSize and bInterval values will be initialized from
158 * module parameters.
159 */
160 .wMaxPacketSize = 0,
161 .bInterval = 0,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200162};
163
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100164static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530165 .bLength = USB_DT_ENDPOINT_SIZE,
166 .bDescriptorType = USB_DT_ENDPOINT,
167 .bEndpointAddress = USB_DIR_IN,
168 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100169 /* The wMaxPacketSize and bInterval values will be initialized from
170 * module parameters.
171 */
172 .wMaxPacketSize = 0,
173 .bInterval = 0,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530174};
175
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530176static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100177 .bLength = USB_DT_ENDPOINT_SIZE,
178 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530179
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100180 .bEndpointAddress = USB_DIR_IN,
181 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100182 /* The wMaxPacketSize and bInterval values will be initialized from
183 * module parameters.
184 */
185 .wMaxPacketSize = 0,
186 .bInterval = 0,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530187};
188
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100189static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100190 .bLength = sizeof(uvc_ss_streaming_comp),
191 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100192 /* The following 3 values can be tweaked if necessary. */
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100193 .bMaxBurst = 0,
194 .bmAttributes = 0,
195 .wBytesPerInterval = cpu_to_le16(1024),
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530196};
197
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200198static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
199 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530200 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200201 NULL,
202};
203
204static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
205 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530206 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
207 NULL,
208};
209
210static 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 Pinchartcdda4792010-05-02 20:57:41 +0200214 NULL,
215};
216
217/* --------------------------------------------------------------------------
218 * Control requests
219 */
220
221static void
222uvc_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
239static int
240uvc_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
268static int
269uvc_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
283static int
284uvc_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 Sharmafbcaba02012-06-01 15:08:56 +0530289 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200290
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 Brokhman72c973d2011-06-28 16:33:48 +0300336 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530337 ret = config_ep_by_speed(f->config->cdev->gadget,
338 &(uvc->func), uvc->video.ep);
339 if (ret)
340 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300341 usb_ep_enable(uvc->video.ep);
342 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200343
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
358static void
359uvc_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
377void
378uvc_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
387void
388uvc_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
401static int
402uvc_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 Pinchartcdda4792010-05-02 20:57:41 +0200413 video->fops = &uvc_v4l2_fops;
414 video->release = video_device_release;
Chen Gang4d2079c2013-02-02 15:48:54 +0800415 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200416
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
440static struct usb_descriptor_header ** __init
441uvc_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 Sharmafbcaba02012-06-01 15:08:56 +0530445 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200446 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 Sharmafbcaba02012-06-01 15:08:56 +0530457 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 Sharmafbcaba02012-06-01 15:08:56 +0530462 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 Sharmafbcaba02012-06-01 15:08:56 +0530468 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 Sharmafbcaba02012-06-01 15:08:56 +0530475 break;
476 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200477
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 Pinchart48eee0b2013-03-01 20:46:25 +0100485 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200486 * 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 Pinchart48eee0b2013-03-01 20:46:25 +0100495 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200496 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200497
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530498 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 Pinchartee6a4d82013-03-01 20:46:24 +0100506 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200507 control_size += (*src)->bLength;
508 bytes += (*src)->bLength;
509 n_desc++;
510 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530511 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100512 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200513 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 Sharmafbcaba02012-06-01 15:08:56 +0530536 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200537 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 Pinchart48eee0b2013-03-01 20:46:25 +0100541 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530542 if (speed == USB_SPEED_SUPER)
543 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
544
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200545 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 Pinchart0485ec02013-03-01 20:46:27 +0100552 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200553
554 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
555
556 *dst = NULL;
557 return hdr;
558}
559
560static void
561uvc_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 Siewior0f9df932012-10-22 22:15:05 +0200568 video_unregister_device(uvc->vdev);
569 uvc->control_ep->driver_data = NULL;
570 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200571
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200572 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200573 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
574 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200575
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200576 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200577
578 kfree(uvc);
579}
580
581static int __init
582uvc_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 Pinchart20777dd2013-03-01 20:46:26 +0100586 unsigned int max_packet_mult;
587 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200588 struct usb_ep *ep;
589 int ret = -EINVAL;
590
591 INFO(cdev, "uvc_function_bind\n");
592
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100593 /* Sanity check the streaming endpoint module parameters.
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530594 */
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100595 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 Sharmafbcaba02012-06-01 15:08:56 +0530617 uvc_fs_streaming_ep.bInterval = streaming_interval;
618
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100619 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 Pinchartcdda4792010-05-02 20:57:41 +0200630 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100631 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200632 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 Pinchart0485ec02013-03-01 20:46:27 +0100639 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 Pinchartcdda4792010-05-02 20:57:41 +0200647 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 Pinchart0485ec02013-03-01 20:46:27 +0100654 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 Pinchart20777dd2013-03-01 20:46:26 +0100657
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200658 /* 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 Siewior10287ba2012-10-22 22:15:06 +0200671 /* Copy descriptors */
672 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
673 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530674 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200675 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530676 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200677
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
710error:
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200711 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 Siewior10287ba2012-10-22 22:15:06 +0200724 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200725 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 */
742int __init
743uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530744 const struct uvc_descriptor_header * const *fs_control,
745 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200746 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530747 const struct uvc_descriptor_header * const *hs_streaming,
748 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200749{
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 Sharmafbcaba02012-06-01 15:08:56 +0530766 if (fs_control == NULL || fs_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100767 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530768 goto error;
769
770 if (ss_control == NULL || ss_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100771 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200772 goto error;
773
774 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100775 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200776 goto error;
777
778 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100779 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200780 goto error;
781
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530782 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100783 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530784 goto error;
785
786 uvc->desc.fs_control = fs_control;
787 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200788 uvc->desc.fs_streaming = fs_streaming;
789 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530790 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200791
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100792 /* 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 Sharma3de6e632012-06-01 15:08:54 +0530796 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200797 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
798 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530799 goto error;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200800 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 Sharma3de6e632012-06-01 15:08:54 +0530805 uvc_streaming_intf_alt0.iInterface = ret;
806 uvc_streaming_intf_alt1.iInterface = ret;
807 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200808
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 Brar28f75f42011-06-25 00:17:26 +0530823 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200824
825error:
826 kfree(uvc);
827 return ret;
828}
829
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300830module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200831MODULE_PARM_DESC(trace, "Trace level bitmask");
832