blob: 8e4827c3afb5bd9e619a2e1bddf43072a820987c [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 */
36static unsigned streaming_interval = 1;
37module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
38MODULE_PARM_DESC(streaming_interval, "1 - 16");
39
40static unsigned streaming_maxpacket = 1024;
41module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
42MODULE_PARM_DESC(streaming_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
43
44static unsigned streaming_mult;
45module_param(streaming_mult, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(streaming_mult, "0 - 2 (hs/ss only)");
47
48static unsigned streaming_maxburst;
49module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
50MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
51
Laurent Pinchartcdda4792010-05-02 20:57:41 +020052/* --------------------------------------------------------------------------
53 * Function descriptors
54 */
55
56/* string IDs are assigned dynamically */
57
58#define UVC_STRING_ASSOCIATION_IDX 0
59#define UVC_STRING_CONTROL_IDX 1
60#define UVC_STRING_STREAMING_IDX 2
61
62static struct usb_string uvc_en_us_strings[] = {
63 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
64 [UVC_STRING_CONTROL_IDX].s = "Video Control",
65 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
66 { }
67};
68
69static struct usb_gadget_strings uvc_stringtab = {
70 .language = 0x0409, /* en-us */
71 .strings = uvc_en_us_strings,
72};
73
74static struct usb_gadget_strings *uvc_function_strings[] = {
75 &uvc_stringtab,
76 NULL,
77};
78
79#define UVC_INTF_VIDEO_CONTROL 0
80#define UVC_INTF_VIDEO_STREAMING 1
81
Laurent Pinchart912ca422013-03-01 20:46:23 +010082#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053083
Laurent Pinchartcdda4792010-05-02 20:57:41 +020084static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030085 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020086 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
87 .bFirstInterface = 0,
88 .bInterfaceCount = 2,
89 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030090 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020091 .bFunctionProtocol = 0x00,
92 .iFunction = 0,
93};
94
95static struct usb_interface_descriptor uvc_control_intf __initdata = {
96 .bLength = USB_DT_INTERFACE_SIZE,
97 .bDescriptorType = USB_DT_INTERFACE,
98 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
99 .bAlternateSetting = 0,
100 .bNumEndpoints = 1,
101 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300102 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200103 .bInterfaceProtocol = 0x00,
104 .iInterface = 0,
105};
106
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100107static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200108 .bLength = USB_DT_ENDPOINT_SIZE,
109 .bDescriptorType = USB_DT_ENDPOINT,
110 .bEndpointAddress = USB_DIR_IN,
111 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100112 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200113 .bInterval = 8,
114};
115
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100116static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
117 .bLength = sizeof(uvc_ss_control_comp),
118 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
119 /* The following 3 values can be tweaked if necessary. */
120 .bMaxBurst = 0,
121 .bmAttributes = 0,
122 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
123};
124
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200125static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
126 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
127 .bDescriptorType = USB_DT_CS_ENDPOINT,
128 .bDescriptorSubType = UVC_EP_INTERRUPT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100129 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200130};
131
132static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
133 .bLength = USB_DT_INTERFACE_SIZE,
134 .bDescriptorType = USB_DT_INTERFACE,
135 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
136 .bAlternateSetting = 0,
137 .bNumEndpoints = 0,
138 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300139 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200140 .bInterfaceProtocol = 0x00,
141 .iInterface = 0,
142};
143
144static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
145 .bLength = USB_DT_INTERFACE_SIZE,
146 .bDescriptorType = USB_DT_INTERFACE,
147 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
148 .bAlternateSetting = 1,
149 .bNumEndpoints = 1,
150 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300151 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200152 .bInterfaceProtocol = 0x00,
153 .iInterface = 0,
154};
155
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100156static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
159 .bEndpointAddress = USB_DIR_IN,
160 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
161 .wMaxPacketSize = cpu_to_le16(512),
162 .bInterval = 1,
163};
164
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100165static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530166 .bLength = USB_DT_ENDPOINT_SIZE,
167 .bDescriptorType = USB_DT_ENDPOINT,
168 .bEndpointAddress = USB_DIR_IN,
169 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
170 .wMaxPacketSize = cpu_to_le16(1024),
171 .bInterval = 1,
172};
173
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530174static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530177
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100178 .bEndpointAddress = USB_DIR_IN,
179 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
180 .wMaxPacketSize = cpu_to_le16(1024),
181 .bInterval = 4,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530182};
183
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100184static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100185 .bLength = sizeof(uvc_ss_streaming_comp),
186 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100187 /* The following 3 values can be tweaked if necessary. */
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100188 .bMaxBurst = 0,
189 .bmAttributes = 0,
190 .wBytesPerInterval = cpu_to_le16(1024),
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530191};
192
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200193static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
194 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530195 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200196 NULL,
197};
198
199static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
200 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530201 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
202 NULL,
203};
204
205static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
206 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
207 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
208 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200209 NULL,
210};
211
212/* --------------------------------------------------------------------------
213 * Control requests
214 */
215
216static void
217uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
218{
219 struct uvc_device *uvc = req->context;
220 struct v4l2_event v4l2_event;
221 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
222
223 if (uvc->event_setup_out) {
224 uvc->event_setup_out = 0;
225
226 memset(&v4l2_event, 0, sizeof(v4l2_event));
227 v4l2_event.type = UVC_EVENT_DATA;
228 uvc_event->data.length = req->actual;
229 memcpy(&uvc_event->data.data, req->buf, req->actual);
230 v4l2_event_queue(uvc->vdev, &v4l2_event);
231 }
232}
233
234static int
235uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
236{
237 struct uvc_device *uvc = to_uvc(f);
238 struct v4l2_event v4l2_event;
239 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
240
241 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
242 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
243 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
244 */
245
246 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
247 INFO(f->config->cdev, "invalid request type\n");
248 return -EINVAL;
249 }
250
251 /* Stall too big requests. */
252 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
253 return -EINVAL;
254
255 memset(&v4l2_event, 0, sizeof(v4l2_event));
256 v4l2_event.type = UVC_EVENT_SETUP;
257 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
258 v4l2_event_queue(uvc->vdev, &v4l2_event);
259
260 return 0;
261}
262
263static int
264uvc_function_get_alt(struct usb_function *f, unsigned interface)
265{
266 struct uvc_device *uvc = to_uvc(f);
267
268 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
269
270 if (interface == uvc->control_intf)
271 return 0;
272 else if (interface != uvc->streaming_intf)
273 return -EINVAL;
274 else
275 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
276}
277
278static int
279uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
280{
281 struct uvc_device *uvc = to_uvc(f);
282 struct v4l2_event v4l2_event;
283 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530284 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200285
286 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
287
288 if (interface == uvc->control_intf) {
289 if (alt)
290 return -EINVAL;
291
292 if (uvc->state == UVC_STATE_DISCONNECTED) {
293 memset(&v4l2_event, 0, sizeof(v4l2_event));
294 v4l2_event.type = UVC_EVENT_CONNECT;
295 uvc_event->speed = f->config->cdev->gadget->speed;
296 v4l2_event_queue(uvc->vdev, &v4l2_event);
297
298 uvc->state = UVC_STATE_CONNECTED;
299 }
300
301 return 0;
302 }
303
304 if (interface != uvc->streaming_intf)
305 return -EINVAL;
306
307 /* TODO
308 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
309 return alt ? -EINVAL : 0;
310 */
311
312 switch (alt) {
313 case 0:
314 if (uvc->state != UVC_STATE_STREAMING)
315 return 0;
316
317 if (uvc->video.ep)
318 usb_ep_disable(uvc->video.ep);
319
320 memset(&v4l2_event, 0, sizeof(v4l2_event));
321 v4l2_event.type = UVC_EVENT_STREAMOFF;
322 v4l2_event_queue(uvc->vdev, &v4l2_event);
323
324 uvc->state = UVC_STATE_CONNECTED;
325 break;
326
327 case 1:
328 if (uvc->state != UVC_STATE_CONNECTED)
329 return 0;
330
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300331 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530332 ret = config_ep_by_speed(f->config->cdev->gadget,
333 &(uvc->func), uvc->video.ep);
334 if (ret)
335 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300336 usb_ep_enable(uvc->video.ep);
337 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200338
339 memset(&v4l2_event, 0, sizeof(v4l2_event));
340 v4l2_event.type = UVC_EVENT_STREAMON;
341 v4l2_event_queue(uvc->vdev, &v4l2_event);
342
343 uvc->state = UVC_STATE_STREAMING;
344 break;
345
346 default:
347 return -EINVAL;
348 }
349
350 return 0;
351}
352
353static void
354uvc_function_disable(struct usb_function *f)
355{
356 struct uvc_device *uvc = to_uvc(f);
357 struct v4l2_event v4l2_event;
358
359 INFO(f->config->cdev, "uvc_function_disable\n");
360
361 memset(&v4l2_event, 0, sizeof(v4l2_event));
362 v4l2_event.type = UVC_EVENT_DISCONNECT;
363 v4l2_event_queue(uvc->vdev, &v4l2_event);
364
365 uvc->state = UVC_STATE_DISCONNECTED;
366}
367
368/* --------------------------------------------------------------------------
369 * Connection / disconnection
370 */
371
372void
373uvc_function_connect(struct uvc_device *uvc)
374{
375 struct usb_composite_dev *cdev = uvc->func.config->cdev;
376 int ret;
377
378 if ((ret = usb_function_activate(&uvc->func)) < 0)
379 INFO(cdev, "UVC connect failed with %d\n", ret);
380}
381
382void
383uvc_function_disconnect(struct uvc_device *uvc)
384{
385 struct usb_composite_dev *cdev = uvc->func.config->cdev;
386 int ret;
387
388 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
389 INFO(cdev, "UVC disconnect failed with %d\n", ret);
390}
391
392/* --------------------------------------------------------------------------
393 * USB probe and disconnect
394 */
395
396static int
397uvc_register_video(struct uvc_device *uvc)
398{
399 struct usb_composite_dev *cdev = uvc->func.config->cdev;
400 struct video_device *video;
401
402 /* TODO reference counting. */
403 video = video_device_alloc();
404 if (video == NULL)
405 return -ENOMEM;
406
407 video->parent = &cdev->gadget->dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200408 video->fops = &uvc_v4l2_fops;
409 video->release = video_device_release;
Chen Gang4d2079c2013-02-02 15:48:54 +0800410 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200411
412 uvc->vdev = video;
413 video_set_drvdata(video, uvc);
414
415 return video_register_device(video, VFL_TYPE_GRABBER, -1);
416}
417
418#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
419 do { \
420 memcpy(mem, desc, (desc)->bLength); \
421 *(dst)++ = mem; \
422 mem += (desc)->bLength; \
423 } while (0);
424
425#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
426 do { \
427 const struct usb_descriptor_header * const *__src; \
428 for (__src = src; *__src; ++__src) { \
429 memcpy(mem, *__src, (*__src)->bLength); \
430 *dst++ = mem; \
431 mem += (*__src)->bLength; \
432 } \
433 } while (0)
434
435static struct usb_descriptor_header ** __init
436uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
437{
438 struct uvc_input_header_descriptor *uvc_streaming_header;
439 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530440 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200441 const struct uvc_descriptor_header * const *uvc_streaming_cls;
442 const struct usb_descriptor_header * const *uvc_streaming_std;
443 const struct usb_descriptor_header * const *src;
444 struct usb_descriptor_header **dst;
445 struct usb_descriptor_header **hdr;
446 unsigned int control_size;
447 unsigned int streaming_size;
448 unsigned int n_desc;
449 unsigned int bytes;
450 void *mem;
451
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530452 switch (speed) {
453 case USB_SPEED_SUPER:
454 uvc_control_desc = uvc->desc.ss_control;
455 uvc_streaming_cls = uvc->desc.ss_streaming;
456 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530457 break;
458
459 case USB_SPEED_HIGH:
460 uvc_control_desc = uvc->desc.fs_control;
461 uvc_streaming_cls = uvc->desc.hs_streaming;
462 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530463 break;
464
465 case USB_SPEED_FULL:
466 default:
467 uvc_control_desc = uvc->desc.fs_control;
468 uvc_streaming_cls = uvc->desc.fs_streaming;
469 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530470 break;
471 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200472
473 /* Descriptors layout
474 *
475 * uvc_iad
476 * uvc_control_intf
477 * Class-specific UVC control descriptors
478 * uvc_control_ep
479 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100480 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200481 * uvc_streaming_intf_alt0
482 * Class-specific UVC streaming descriptors
483 * uvc_{fs|hs}_streaming
484 */
485
486 /* Count descriptors and compute their size. */
487 control_size = 0;
488 streaming_size = 0;
489 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100490 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200491 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200492
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530493 if (speed == USB_SPEED_SUPER) {
494 bytes += uvc_ss_control_comp.bLength;
495 n_desc = 6;
496 } else {
497 n_desc = 5;
498 }
499
500 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100501 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200502 control_size += (*src)->bLength;
503 bytes += (*src)->bLength;
504 n_desc++;
505 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530506 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100507 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200508 streaming_size += (*src)->bLength;
509 bytes += (*src)->bLength;
510 n_desc++;
511 }
512 for (src = uvc_streaming_std; *src; ++src) {
513 bytes += (*src)->bLength;
514 n_desc++;
515 }
516
517 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
518 if (mem == NULL)
519 return NULL;
520
521 hdr = mem;
522 dst = mem;
523 mem += (n_desc + 1) * sizeof(*src);
524
525 /* Copy the descriptors. */
526 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
527 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
528
529 uvc_control_header = mem;
530 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530531 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200532 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
533 uvc_control_header->bInCollection = 1;
534 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
535
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100536 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530537 if (speed == USB_SPEED_SUPER)
538 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
539
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200540 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
541 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
542
543 uvc_streaming_header = mem;
544 UVC_COPY_DESCRIPTORS(mem, dst,
545 (const struct usb_descriptor_header**)uvc_streaming_cls);
546 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530547 uvc_streaming_header->bEndpointAddress =
548 uvc_fs_streaming_ep.bEndpointAddress;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200549
550 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
551
552 *dst = NULL;
553 return hdr;
554}
555
556static void
557uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
558{
559 struct usb_composite_dev *cdev = c->cdev;
560 struct uvc_device *uvc = to_uvc(f);
561
562 INFO(cdev, "uvc_function_unbind\n");
563
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200564 video_unregister_device(uvc->vdev);
565 uvc->control_ep->driver_data = NULL;
566 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200567
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200568 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200569 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
570 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200571
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200572 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200573
574 kfree(uvc);
575}
576
577static int __init
578uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
579{
580 struct usb_composite_dev *cdev = c->cdev;
581 struct uvc_device *uvc = to_uvc(f);
582 struct usb_ep *ep;
583 int ret = -EINVAL;
584
585 INFO(cdev, "uvc_function_bind\n");
586
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530587 /* sanity check the streaming endpoint module parameters */
588 if (streaming_interval < 1)
589 streaming_interval = 1;
590 if (streaming_interval > 16)
591 streaming_interval = 16;
592 if (streaming_mult > 2)
593 streaming_mult = 2;
594 if (streaming_maxburst > 15)
595 streaming_maxburst = 15;
596
597 /*
598 * fill in the FS video streaming specific descriptors from the
599 * module parameters
600 */
601 uvc_fs_streaming_ep.wMaxPacketSize = streaming_maxpacket > 1023 ?
602 1023 : streaming_maxpacket;
603 uvc_fs_streaming_ep.bInterval = streaming_interval;
604
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200605 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100606 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200607 if (!ep) {
608 INFO(cdev, "Unable to allocate control EP\n");
609 goto error;
610 }
611 uvc->control_ep = ep;
612 ep->driver_data = uvc;
613
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530614 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200615 if (!ep) {
616 INFO(cdev, "Unable to allocate streaming EP\n");
617 goto error;
618 }
619 uvc->video.ep = ep;
620 ep->driver_data = uvc;
621
622 /* Allocate interface IDs. */
623 if ((ret = usb_interface_id(c, f)) < 0)
624 goto error;
625 uvc_iad.bFirstInterface = ret;
626 uvc_control_intf.bInterfaceNumber = ret;
627 uvc->control_intf = ret;
628
629 if ((ret = usb_interface_id(c, f)) < 0)
630 goto error;
631 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
632 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
633 uvc->streaming_intf = ret;
634
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530635 /* sanity check the streaming endpoint module parameters */
636 if (streaming_maxpacket > 1024)
637 streaming_maxpacket = 1024;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200638 /*
639 * Fill in the HS descriptors from the module parameters for the Video
640 * Streaming endpoint.
641 * NOTE: We assume that the user knows what they are doing and won't
642 * give parameters that their UDC doesn't support.
643 */
644 uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket;
645 uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11;
646 uvc_hs_streaming_ep.bInterval = streaming_interval;
647 uvc_hs_streaming_ep.bEndpointAddress =
648 uvc_fs_streaming_ep.bEndpointAddress;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530649
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200650 /*
651 * Fill in the SS descriptors from the module parameters for the Video
652 * Streaming endpoint.
653 * NOTE: We assume that the user knows what they are doing and won't
654 * give parameters that their UDC doesn't support.
655 */
656 uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
657 uvc_ss_streaming_ep.bInterval = streaming_interval;
658 uvc_ss_streaming_comp.bmAttributes = streaming_mult;
659 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
660 uvc_ss_streaming_comp.wBytesPerInterval =
661 streaming_maxpacket * (streaming_mult + 1) *
662 (streaming_maxburst + 1);
663 uvc_ss_streaming_ep.bEndpointAddress =
664 uvc_fs_streaming_ep.bEndpointAddress;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530665
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200666 /* Copy descriptors */
667 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
668 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530669 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200670 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530671 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200672
673 /* Preallocate control endpoint request. */
674 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
675 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
676 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
677 ret = -ENOMEM;
678 goto error;
679 }
680
681 uvc->control_req->buf = uvc->control_buf;
682 uvc->control_req->complete = uvc_function_ep0_complete;
683 uvc->control_req->context = uvc;
684
685 /* Avoid letting this gadget enumerate until the userspace server is
686 * active.
687 */
688 if ((ret = usb_function_deactivate(f)) < 0)
689 goto error;
690
691 /* Initialise video. */
692 ret = uvc_video_init(&uvc->video);
693 if (ret < 0)
694 goto error;
695
696 /* Register a V4L2 device. */
697 ret = uvc_register_video(uvc);
698 if (ret < 0) {
699 printk(KERN_INFO "Unable to register video device\n");
700 goto error;
701 }
702
703 return 0;
704
705error:
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200706 if (uvc->vdev)
707 video_device_release(uvc->vdev);
708
709 if (uvc->control_ep)
710 uvc->control_ep->driver_data = NULL;
711 if (uvc->video.ep)
712 uvc->video.ep->driver_data = NULL;
713
714 if (uvc->control_req) {
715 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
716 kfree(uvc->control_buf);
717 }
718
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200719 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200720 return ret;
721}
722
723/* --------------------------------------------------------------------------
724 * USB gadget function
725 */
726
727/**
728 * uvc_bind_config - add a UVC function to a configuration
729 * @c: the configuration to support the UVC instance
730 * Context: single threaded during gadget setup
731 *
732 * Returns zero on success, else negative errno.
733 *
734 * Caller must have called @uvc_setup(). Caller is also responsible for
735 * calling @uvc_cleanup() before module unload.
736 */
737int __init
738uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530739 const struct uvc_descriptor_header * const *fs_control,
740 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200741 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530742 const struct uvc_descriptor_header * const *hs_streaming,
743 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200744{
745 struct uvc_device *uvc;
746 int ret = 0;
747
748 /* TODO Check if the USB device controller supports the required
749 * features.
750 */
751 if (!gadget_is_dualspeed(c->cdev->gadget))
752 return -EINVAL;
753
754 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
755 if (uvc == NULL)
756 return -ENOMEM;
757
758 uvc->state = UVC_STATE_DISCONNECTED;
759
760 /* Validate the descriptors. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530761 if (fs_control == NULL || fs_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100762 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530763 goto error;
764
765 if (ss_control == NULL || ss_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100766 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200767 goto error;
768
769 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100770 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200771 goto error;
772
773 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100774 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200775 goto error;
776
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530777 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100778 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530779 goto error;
780
781 uvc->desc.fs_control = fs_control;
782 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200783 uvc->desc.fs_streaming = fs_streaming;
784 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530785 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200786
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100787 /* String descriptors are global, we only need to allocate string IDs
788 * for the first UVC function. UVC functions beyond the first (if any)
789 * will reuse the same IDs.
790 */
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530791 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200792 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
793 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530794 goto error;
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200795 uvc_iad.iFunction =
796 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id;
797 uvc_control_intf.iInterface =
798 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
799 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530800 uvc_streaming_intf_alt0.iInterface = ret;
801 uvc_streaming_intf_alt1.iInterface = ret;
802 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200803
804 /* Register the function. */
805 uvc->func.name = "uvc";
806 uvc->func.strings = uvc_function_strings;
807 uvc->func.bind = uvc_function_bind;
808 uvc->func.unbind = uvc_function_unbind;
809 uvc->func.get_alt = uvc_function_get_alt;
810 uvc->func.set_alt = uvc_function_set_alt;
811 uvc->func.disable = uvc_function_disable;
812 uvc->func.setup = uvc_function_setup;
813
814 ret = usb_add_function(c, &uvc->func);
815 if (ret)
816 kfree(uvc);
817
Jassi Brar28f75f42011-06-25 00:17:26 +0530818 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200819
820error:
821 kfree(uvc);
822 return ret;
823}
824
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300825module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200826MODULE_PARM_DESC(trace, "Trace level bitmask");
827