blob: 49939e44ed748a2751bd5478d94271797a4ac3fb [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
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010054#define UVC_STRING_CONTROL_IDX 0
55#define UVC_STRING_STREAMING_IDX 1
Laurent Pinchartcdda4792010-05-02 20:57:41 +020056
57static struct usb_string uvc_en_us_strings[] = {
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010058 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
Laurent Pinchartcdda4792010-05-02 20:57:41 +020059 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
60 { }
61};
62
63static struct usb_gadget_strings uvc_stringtab = {
64 .language = 0x0409, /* en-us */
65 .strings = uvc_en_us_strings,
66};
67
68static struct usb_gadget_strings *uvc_function_strings[] = {
69 &uvc_stringtab,
70 NULL,
71};
72
73#define UVC_INTF_VIDEO_CONTROL 0
74#define UVC_INTF_VIDEO_STREAMING 1
75
Laurent Pinchart912ca422013-03-01 20:46:23 +010076#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053077
Laurent Pinchartcdda4792010-05-02 20:57:41 +020078static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030079 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020080 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
81 .bFirstInterface = 0,
82 .bInterfaceCount = 2,
83 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030084 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020085 .bFunctionProtocol = 0x00,
86 .iFunction = 0,
87};
88
89static struct usb_interface_descriptor uvc_control_intf __initdata = {
90 .bLength = USB_DT_INTERFACE_SIZE,
91 .bDescriptorType = USB_DT_INTERFACE,
92 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
93 .bAlternateSetting = 0,
94 .bNumEndpoints = 1,
95 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030096 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020097 .bInterfaceProtocol = 0x00,
98 .iInterface = 0,
99};
100
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100101static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200102 .bLength = USB_DT_ENDPOINT_SIZE,
103 .bDescriptorType = USB_DT_ENDPOINT,
104 .bEndpointAddress = USB_DIR_IN,
105 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100106 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200107 .bInterval = 8,
108};
109
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100110static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
111 .bLength = sizeof(uvc_ss_control_comp),
112 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
113 /* The following 3 values can be tweaked if necessary. */
114 .bMaxBurst = 0,
115 .bmAttributes = 0,
116 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
117};
118
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200119static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
120 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
121 .bDescriptorType = USB_DT_CS_ENDPOINT,
122 .bDescriptorSubType = UVC_EP_INTERRUPT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100123 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200124};
125
126static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
127 .bLength = USB_DT_INTERFACE_SIZE,
128 .bDescriptorType = USB_DT_INTERFACE,
129 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
130 .bAlternateSetting = 0,
131 .bNumEndpoints = 0,
132 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300133 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200134 .bInterfaceProtocol = 0x00,
135 .iInterface = 0,
136};
137
138static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
139 .bLength = USB_DT_INTERFACE_SIZE,
140 .bDescriptorType = USB_DT_INTERFACE,
141 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
142 .bAlternateSetting = 1,
143 .bNumEndpoints = 1,
144 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300145 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200146 .bInterfaceProtocol = 0x00,
147 .iInterface = 0,
148};
149
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100150static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100154 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
155 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100156 /* The wMaxPacketSize and bInterval values will be initialized from
157 * module parameters.
158 */
159 .wMaxPacketSize = 0,
160 .bInterval = 0,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200161};
162
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100163static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100167 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
168 | 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,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100181 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
182 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100183 /* The wMaxPacketSize and bInterval values will be initialized from
184 * module parameters.
185 */
186 .wMaxPacketSize = 0,
187 .bInterval = 0,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530188};
189
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100190static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100191 .bLength = sizeof(uvc_ss_streaming_comp),
192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100193 /* The following 3 values can be tweaked if necessary. */
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100194 .bMaxBurst = 0,
195 .bmAttributes = 0,
196 .wBytesPerInterval = cpu_to_le16(1024),
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530197};
198
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200199static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
200 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530201 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200202 NULL,
203};
204
205static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
206 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530207 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
208 NULL,
209};
210
211static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
212 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
213 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
214 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200215 NULL,
216};
217
218/* --------------------------------------------------------------------------
219 * Control requests
220 */
221
222static void
223uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
224{
225 struct uvc_device *uvc = req->context;
226 struct v4l2_event v4l2_event;
227 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
228
229 if (uvc->event_setup_out) {
230 uvc->event_setup_out = 0;
231
232 memset(&v4l2_event, 0, sizeof(v4l2_event));
233 v4l2_event.type = UVC_EVENT_DATA;
234 uvc_event->data.length = req->actual;
235 memcpy(&uvc_event->data.data, req->buf, req->actual);
236 v4l2_event_queue(uvc->vdev, &v4l2_event);
237 }
238}
239
240static int
241uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
242{
243 struct uvc_device *uvc = to_uvc(f);
244 struct v4l2_event v4l2_event;
245 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
246
247 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
248 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
249 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
250 */
251
252 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
253 INFO(f->config->cdev, "invalid request type\n");
254 return -EINVAL;
255 }
256
257 /* Stall too big requests. */
258 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
259 return -EINVAL;
260
261 memset(&v4l2_event, 0, sizeof(v4l2_event));
262 v4l2_event.type = UVC_EVENT_SETUP;
263 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
264 v4l2_event_queue(uvc->vdev, &v4l2_event);
265
266 return 0;
267}
268
269static int
270uvc_function_get_alt(struct usb_function *f, unsigned interface)
271{
272 struct uvc_device *uvc = to_uvc(f);
273
274 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
275
276 if (interface == uvc->control_intf)
277 return 0;
278 else if (interface != uvc->streaming_intf)
279 return -EINVAL;
280 else
281 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
282}
283
284static int
285uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
286{
287 struct uvc_device *uvc = to_uvc(f);
288 struct v4l2_event v4l2_event;
289 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530290 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200291
292 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
293
294 if (interface == uvc->control_intf) {
295 if (alt)
296 return -EINVAL;
297
298 if (uvc->state == UVC_STATE_DISCONNECTED) {
299 memset(&v4l2_event, 0, sizeof(v4l2_event));
300 v4l2_event.type = UVC_EVENT_CONNECT;
301 uvc_event->speed = f->config->cdev->gadget->speed;
302 v4l2_event_queue(uvc->vdev, &v4l2_event);
303
304 uvc->state = UVC_STATE_CONNECTED;
305 }
306
307 return 0;
308 }
309
310 if (interface != uvc->streaming_intf)
311 return -EINVAL;
312
313 /* TODO
314 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
315 return alt ? -EINVAL : 0;
316 */
317
318 switch (alt) {
319 case 0:
320 if (uvc->state != UVC_STATE_STREAMING)
321 return 0;
322
323 if (uvc->video.ep)
324 usb_ep_disable(uvc->video.ep);
325
326 memset(&v4l2_event, 0, sizeof(v4l2_event));
327 v4l2_event.type = UVC_EVENT_STREAMOFF;
328 v4l2_event_queue(uvc->vdev, &v4l2_event);
329
330 uvc->state = UVC_STATE_CONNECTED;
331 break;
332
333 case 1:
334 if (uvc->state != UVC_STATE_CONNECTED)
335 return 0;
336
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300337 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530338 ret = config_ep_by_speed(f->config->cdev->gadget,
339 &(uvc->func), uvc->video.ep);
340 if (ret)
341 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300342 usb_ep_enable(uvc->video.ep);
343 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200344
345 memset(&v4l2_event, 0, sizeof(v4l2_event));
346 v4l2_event.type = UVC_EVENT_STREAMON;
347 v4l2_event_queue(uvc->vdev, &v4l2_event);
348
349 uvc->state = UVC_STATE_STREAMING;
350 break;
351
352 default:
353 return -EINVAL;
354 }
355
356 return 0;
357}
358
359static void
360uvc_function_disable(struct usb_function *f)
361{
362 struct uvc_device *uvc = to_uvc(f);
363 struct v4l2_event v4l2_event;
364
365 INFO(f->config->cdev, "uvc_function_disable\n");
366
367 memset(&v4l2_event, 0, sizeof(v4l2_event));
368 v4l2_event.type = UVC_EVENT_DISCONNECT;
369 v4l2_event_queue(uvc->vdev, &v4l2_event);
370
371 uvc->state = UVC_STATE_DISCONNECTED;
372}
373
374/* --------------------------------------------------------------------------
375 * Connection / disconnection
376 */
377
378void
379uvc_function_connect(struct uvc_device *uvc)
380{
381 struct usb_composite_dev *cdev = uvc->func.config->cdev;
382 int ret;
383
384 if ((ret = usb_function_activate(&uvc->func)) < 0)
385 INFO(cdev, "UVC connect failed with %d\n", ret);
386}
387
388void
389uvc_function_disconnect(struct uvc_device *uvc)
390{
391 struct usb_composite_dev *cdev = uvc->func.config->cdev;
392 int ret;
393
394 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
395 INFO(cdev, "UVC disconnect failed with %d\n", ret);
396}
397
398/* --------------------------------------------------------------------------
399 * USB probe and disconnect
400 */
401
402static int
403uvc_register_video(struct uvc_device *uvc)
404{
405 struct usb_composite_dev *cdev = uvc->func.config->cdev;
406 struct video_device *video;
407
408 /* TODO reference counting. */
409 video = video_device_alloc();
410 if (video == NULL)
411 return -ENOMEM;
412
413 video->parent = &cdev->gadget->dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200414 video->fops = &uvc_v4l2_fops;
415 video->release = video_device_release;
Chen Gang4d2079c2013-02-02 15:48:54 +0800416 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200417
418 uvc->vdev = video;
419 video_set_drvdata(video, uvc);
420
421 return video_register_device(video, VFL_TYPE_GRABBER, -1);
422}
423
424#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
425 do { \
426 memcpy(mem, desc, (desc)->bLength); \
427 *(dst)++ = mem; \
428 mem += (desc)->bLength; \
429 } while (0);
430
431#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
432 do { \
433 const struct usb_descriptor_header * const *__src; \
434 for (__src = src; *__src; ++__src) { \
435 memcpy(mem, *__src, (*__src)->bLength); \
436 *dst++ = mem; \
437 mem += (*__src)->bLength; \
438 } \
439 } while (0)
440
441static struct usb_descriptor_header ** __init
442uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
443{
444 struct uvc_input_header_descriptor *uvc_streaming_header;
445 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530446 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200447 const struct uvc_descriptor_header * const *uvc_streaming_cls;
448 const struct usb_descriptor_header * const *uvc_streaming_std;
449 const struct usb_descriptor_header * const *src;
450 struct usb_descriptor_header **dst;
451 struct usb_descriptor_header **hdr;
452 unsigned int control_size;
453 unsigned int streaming_size;
454 unsigned int n_desc;
455 unsigned int bytes;
456 void *mem;
457
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530458 switch (speed) {
459 case USB_SPEED_SUPER:
460 uvc_control_desc = uvc->desc.ss_control;
461 uvc_streaming_cls = uvc->desc.ss_streaming;
462 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530463 break;
464
465 case USB_SPEED_HIGH:
466 uvc_control_desc = uvc->desc.fs_control;
467 uvc_streaming_cls = uvc->desc.hs_streaming;
468 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530469 break;
470
471 case USB_SPEED_FULL:
472 default:
473 uvc_control_desc = uvc->desc.fs_control;
474 uvc_streaming_cls = uvc->desc.fs_streaming;
475 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530476 break;
477 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200478
479 /* Descriptors layout
480 *
481 * uvc_iad
482 * uvc_control_intf
483 * Class-specific UVC control descriptors
484 * uvc_control_ep
485 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100486 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200487 * uvc_streaming_intf_alt0
488 * Class-specific UVC streaming descriptors
489 * uvc_{fs|hs}_streaming
490 */
491
492 /* Count descriptors and compute their size. */
493 control_size = 0;
494 streaming_size = 0;
495 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100496 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200497 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200498
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530499 if (speed == USB_SPEED_SUPER) {
500 bytes += uvc_ss_control_comp.bLength;
501 n_desc = 6;
502 } else {
503 n_desc = 5;
504 }
505
506 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100507 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200508 control_size += (*src)->bLength;
509 bytes += (*src)->bLength;
510 n_desc++;
511 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530512 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100513 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200514 streaming_size += (*src)->bLength;
515 bytes += (*src)->bLength;
516 n_desc++;
517 }
518 for (src = uvc_streaming_std; *src; ++src) {
519 bytes += (*src)->bLength;
520 n_desc++;
521 }
522
523 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
524 if (mem == NULL)
525 return NULL;
526
527 hdr = mem;
528 dst = mem;
529 mem += (n_desc + 1) * sizeof(*src);
530
531 /* Copy the descriptors. */
532 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
533 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
534
535 uvc_control_header = mem;
536 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530537 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200538 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
539 uvc_control_header->bInCollection = 1;
540 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
541
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100542 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530543 if (speed == USB_SPEED_SUPER)
544 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
545
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200546 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
547 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
548
549 uvc_streaming_header = mem;
550 UVC_COPY_DESCRIPTORS(mem, dst,
551 (const struct usb_descriptor_header**)uvc_streaming_cls);
552 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100553 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200554
555 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
556
557 *dst = NULL;
558 return hdr;
559}
560
561static void
562uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
563{
564 struct usb_composite_dev *cdev = c->cdev;
565 struct uvc_device *uvc = to_uvc(f);
566
567 INFO(cdev, "uvc_function_unbind\n");
568
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200569 video_unregister_device(uvc->vdev);
570 uvc->control_ep->driver_data = NULL;
571 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200572
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100573 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200574 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
575 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200576
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200577 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200578
579 kfree(uvc);
580}
581
582static int __init
583uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
584{
585 struct usb_composite_dev *cdev = c->cdev;
586 struct uvc_device *uvc = to_uvc(f);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100587 unsigned int max_packet_mult;
588 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200589 struct usb_ep *ep;
590 int ret = -EINVAL;
591
592 INFO(cdev, "uvc_function_bind\n");
593
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100594 /* Sanity check the streaming endpoint module parameters.
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530595 */
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100596 streaming_interval = clamp(streaming_interval, 1U, 16U);
597 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
598 streaming_maxburst = min(streaming_maxburst, 15U);
599
600 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
601 * module parameters.
602 *
603 * NOTE: We assume that the user knows what they are doing and won't
604 * give parameters that their UDC doesn't support.
605 */
606 if (streaming_maxpacket <= 1024) {
607 max_packet_mult = 1;
608 max_packet_size = streaming_maxpacket;
609 } else if (streaming_maxpacket <= 2048) {
610 max_packet_mult = 2;
611 max_packet_size = streaming_maxpacket / 2;
612 } else {
613 max_packet_mult = 3;
614 max_packet_size = streaming_maxpacket / 3;
615 }
616
617 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530618 uvc_fs_streaming_ep.bInterval = streaming_interval;
619
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100620 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
621 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
622 uvc_hs_streaming_ep.bInterval = streaming_interval;
623
624 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
625 uvc_ss_streaming_ep.bInterval = streaming_interval;
626 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
627 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
628 uvc_ss_streaming_comp.wBytesPerInterval =
629 max_packet_size * max_packet_mult * streaming_maxburst;
630
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200631 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100632 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200633 if (!ep) {
634 INFO(cdev, "Unable to allocate control EP\n");
635 goto error;
636 }
637 uvc->control_ep = ep;
638 ep->driver_data = uvc;
639
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100640 if (gadget_is_superspeed(c->cdev->gadget))
641 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
642 &uvc_ss_streaming_comp);
643 else if (gadget_is_dualspeed(cdev->gadget))
644 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
645 else
646 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
647
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200648 if (!ep) {
649 INFO(cdev, "Unable to allocate streaming EP\n");
650 goto error;
651 }
652 uvc->video.ep = ep;
653 ep->driver_data = uvc;
654
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100655 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
656 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
657 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100658
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200659 /* Allocate interface IDs. */
660 if ((ret = usb_interface_id(c, f)) < 0)
661 goto error;
662 uvc_iad.bFirstInterface = ret;
663 uvc_control_intf.bInterfaceNumber = ret;
664 uvc->control_intf = ret;
665
666 if ((ret = usb_interface_id(c, f)) < 0)
667 goto error;
668 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
669 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
670 uvc->streaming_intf = ret;
671
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200672 /* Copy descriptors */
673 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
674 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530675 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200676 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530677 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200678
679 /* Preallocate control endpoint request. */
680 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
681 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
682 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
683 ret = -ENOMEM;
684 goto error;
685 }
686
687 uvc->control_req->buf = uvc->control_buf;
688 uvc->control_req->complete = uvc_function_ep0_complete;
689 uvc->control_req->context = uvc;
690
691 /* Avoid letting this gadget enumerate until the userspace server is
692 * active.
693 */
694 if ((ret = usb_function_deactivate(f)) < 0)
695 goto error;
696
697 /* Initialise video. */
698 ret = uvc_video_init(&uvc->video);
699 if (ret < 0)
700 goto error;
701
702 /* Register a V4L2 device. */
703 ret = uvc_register_video(uvc);
704 if (ret < 0) {
705 printk(KERN_INFO "Unable to register video device\n");
706 goto error;
707 }
708
709 return 0;
710
711error:
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200712 if (uvc->vdev)
713 video_device_release(uvc->vdev);
714
715 if (uvc->control_ep)
716 uvc->control_ep->driver_data = NULL;
717 if (uvc->video.ep)
718 uvc->video.ep->driver_data = NULL;
719
720 if (uvc->control_req) {
721 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
722 kfree(uvc->control_buf);
723 }
724
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200725 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200726 return ret;
727}
728
729/* --------------------------------------------------------------------------
730 * USB gadget function
731 */
732
733/**
734 * uvc_bind_config - add a UVC function to a configuration
735 * @c: the configuration to support the UVC instance
736 * Context: single threaded during gadget setup
737 *
738 * Returns zero on success, else negative errno.
739 *
740 * Caller must have called @uvc_setup(). Caller is also responsible for
741 * calling @uvc_cleanup() before module unload.
742 */
743int __init
744uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530745 const struct uvc_descriptor_header * const *fs_control,
746 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200747 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530748 const struct uvc_descriptor_header * const *hs_streaming,
749 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200750{
751 struct uvc_device *uvc;
752 int ret = 0;
753
754 /* TODO Check if the USB device controller supports the required
755 * features.
756 */
757 if (!gadget_is_dualspeed(c->cdev->gadget))
758 return -EINVAL;
759
760 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
761 if (uvc == NULL)
762 return -ENOMEM;
763
764 uvc->state = UVC_STATE_DISCONNECTED;
765
766 /* Validate the descriptors. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530767 if (fs_control == NULL || fs_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100768 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530769 goto error;
770
771 if (ss_control == NULL || ss_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100772 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200773 goto error;
774
775 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100776 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200777 goto error;
778
779 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100780 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200781 goto error;
782
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530783 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100784 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530785 goto error;
786
787 uvc->desc.fs_control = fs_control;
788 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200789 uvc->desc.fs_streaming = fs_streaming;
790 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530791 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200792
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100793 /* String descriptors are global, we only need to allocate string IDs
794 * for the first UVC function. UVC functions beyond the first (if any)
795 * will reuse the same IDs.
796 */
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100797 if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200798 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
799 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530800 goto error;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200801 uvc_iad.iFunction =
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100802 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200803 uvc_control_intf.iInterface =
804 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
805 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530806 uvc_streaming_intf_alt0.iInterface = ret;
807 uvc_streaming_intf_alt1.iInterface = ret;
808 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200809
810 /* Register the function. */
811 uvc->func.name = "uvc";
812 uvc->func.strings = uvc_function_strings;
813 uvc->func.bind = uvc_function_bind;
814 uvc->func.unbind = uvc_function_unbind;
815 uvc->func.get_alt = uvc_function_get_alt;
816 uvc->func.set_alt = uvc_function_set_alt;
817 uvc->func.disable = uvc_function_disable;
818 uvc->func.setup = uvc_function_setup;
819
820 ret = usb_add_function(c, &uvc->func);
821 if (ret)
822 kfree(uvc);
823
Jassi Brar28f75f42011-06-25 00:17:26 +0530824 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200825
826error:
827 kfree(uvc);
828 return ret;
829}
830
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300831module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200832MODULE_PARM_DESC(trace, "Trace level bitmask");
833