blob: 1d06567ff41e6892cc668fbb58c8b4f8d83ca9f4 [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
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100269void uvc_function_setup_continue(struct uvc_device *uvc)
270{
271 struct usb_composite_dev *cdev = uvc->func.config->cdev;
272
273 usb_composite_setup_continue(cdev);
274}
275
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200276static int
277uvc_function_get_alt(struct usb_function *f, unsigned interface)
278{
279 struct uvc_device *uvc = to_uvc(f);
280
281 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
282
283 if (interface == uvc->control_intf)
284 return 0;
285 else if (interface != uvc->streaming_intf)
286 return -EINVAL;
287 else
288 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
289}
290
291static int
292uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
293{
294 struct uvc_device *uvc = to_uvc(f);
295 struct v4l2_event v4l2_event;
296 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530297 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200298
299 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
300
301 if (interface == uvc->control_intf) {
302 if (alt)
303 return -EINVAL;
304
305 if (uvc->state == UVC_STATE_DISCONNECTED) {
306 memset(&v4l2_event, 0, sizeof(v4l2_event));
307 v4l2_event.type = UVC_EVENT_CONNECT;
308 uvc_event->speed = f->config->cdev->gadget->speed;
309 v4l2_event_queue(uvc->vdev, &v4l2_event);
310
311 uvc->state = UVC_STATE_CONNECTED;
312 }
313
314 return 0;
315 }
316
317 if (interface != uvc->streaming_intf)
318 return -EINVAL;
319
320 /* TODO
321 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
322 return alt ? -EINVAL : 0;
323 */
324
325 switch (alt) {
326 case 0:
327 if (uvc->state != UVC_STATE_STREAMING)
328 return 0;
329
330 if (uvc->video.ep)
331 usb_ep_disable(uvc->video.ep);
332
333 memset(&v4l2_event, 0, sizeof(v4l2_event));
334 v4l2_event.type = UVC_EVENT_STREAMOFF;
335 v4l2_event_queue(uvc->vdev, &v4l2_event);
336
337 uvc->state = UVC_STATE_CONNECTED;
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100338 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200339
340 case 1:
341 if (uvc->state != UVC_STATE_CONNECTED)
342 return 0;
343
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300344 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530345 ret = config_ep_by_speed(f->config->cdev->gadget,
346 &(uvc->func), uvc->video.ep);
347 if (ret)
348 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300349 usb_ep_enable(uvc->video.ep);
350 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200351
352 memset(&v4l2_event, 0, sizeof(v4l2_event));
353 v4l2_event.type = UVC_EVENT_STREAMON;
354 v4l2_event_queue(uvc->vdev, &v4l2_event);
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100355 return USB_GADGET_DELAYED_STATUS;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200356
357 default:
358 return -EINVAL;
359 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200360}
361
362static void
363uvc_function_disable(struct usb_function *f)
364{
365 struct uvc_device *uvc = to_uvc(f);
366 struct v4l2_event v4l2_event;
367
368 INFO(f->config->cdev, "uvc_function_disable\n");
369
370 memset(&v4l2_event, 0, sizeof(v4l2_event));
371 v4l2_event.type = UVC_EVENT_DISCONNECT;
372 v4l2_event_queue(uvc->vdev, &v4l2_event);
373
374 uvc->state = UVC_STATE_DISCONNECTED;
375}
376
377/* --------------------------------------------------------------------------
378 * Connection / disconnection
379 */
380
381void
382uvc_function_connect(struct uvc_device *uvc)
383{
384 struct usb_composite_dev *cdev = uvc->func.config->cdev;
385 int ret;
386
387 if ((ret = usb_function_activate(&uvc->func)) < 0)
388 INFO(cdev, "UVC connect failed with %d\n", ret);
389}
390
391void
392uvc_function_disconnect(struct uvc_device *uvc)
393{
394 struct usb_composite_dev *cdev = uvc->func.config->cdev;
395 int ret;
396
397 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
398 INFO(cdev, "UVC disconnect failed with %d\n", ret);
399}
400
401/* --------------------------------------------------------------------------
402 * USB probe and disconnect
403 */
404
405static int
406uvc_register_video(struct uvc_device *uvc)
407{
408 struct usb_composite_dev *cdev = uvc->func.config->cdev;
409 struct video_device *video;
410
411 /* TODO reference counting. */
412 video = video_device_alloc();
413 if (video == NULL)
414 return -ENOMEM;
415
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300416 video->v4l2_dev = &uvc->v4l2_dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200417 video->fops = &uvc_v4l2_fops;
418 video->release = video_device_release;
Chen Gang4d2079c2013-02-02 15:48:54 +0800419 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200420
421 uvc->vdev = video;
422 video_set_drvdata(video, uvc);
423
424 return video_register_device(video, VFL_TYPE_GRABBER, -1);
425}
426
427#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
428 do { \
429 memcpy(mem, desc, (desc)->bLength); \
430 *(dst)++ = mem; \
431 mem += (desc)->bLength; \
432 } while (0);
433
434#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
435 do { \
436 const struct usb_descriptor_header * const *__src; \
437 for (__src = src; *__src; ++__src) { \
438 memcpy(mem, *__src, (*__src)->bLength); \
439 *dst++ = mem; \
440 mem += (*__src)->bLength; \
441 } \
442 } while (0)
443
444static struct usb_descriptor_header ** __init
445uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
446{
447 struct uvc_input_header_descriptor *uvc_streaming_header;
448 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530449 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200450 const struct uvc_descriptor_header * const *uvc_streaming_cls;
451 const struct usb_descriptor_header * const *uvc_streaming_std;
452 const struct usb_descriptor_header * const *src;
453 struct usb_descriptor_header **dst;
454 struct usb_descriptor_header **hdr;
455 unsigned int control_size;
456 unsigned int streaming_size;
457 unsigned int n_desc;
458 unsigned int bytes;
459 void *mem;
460
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530461 switch (speed) {
462 case USB_SPEED_SUPER:
463 uvc_control_desc = uvc->desc.ss_control;
464 uvc_streaming_cls = uvc->desc.ss_streaming;
465 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530466 break;
467
468 case USB_SPEED_HIGH:
469 uvc_control_desc = uvc->desc.fs_control;
470 uvc_streaming_cls = uvc->desc.hs_streaming;
471 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530472 break;
473
474 case USB_SPEED_FULL:
475 default:
476 uvc_control_desc = uvc->desc.fs_control;
477 uvc_streaming_cls = uvc->desc.fs_streaming;
478 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530479 break;
480 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200481
482 /* Descriptors layout
483 *
484 * uvc_iad
485 * uvc_control_intf
486 * Class-specific UVC control descriptors
487 * uvc_control_ep
488 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100489 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200490 * uvc_streaming_intf_alt0
491 * Class-specific UVC streaming descriptors
492 * uvc_{fs|hs}_streaming
493 */
494
495 /* Count descriptors and compute their size. */
496 control_size = 0;
497 streaming_size = 0;
498 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100499 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200500 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200501
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530502 if (speed == USB_SPEED_SUPER) {
503 bytes += uvc_ss_control_comp.bLength;
504 n_desc = 6;
505 } else {
506 n_desc = 5;
507 }
508
509 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100510 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200511 control_size += (*src)->bLength;
512 bytes += (*src)->bLength;
513 n_desc++;
514 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530515 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100516 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200517 streaming_size += (*src)->bLength;
518 bytes += (*src)->bLength;
519 n_desc++;
520 }
521 for (src = uvc_streaming_std; *src; ++src) {
522 bytes += (*src)->bLength;
523 n_desc++;
524 }
525
526 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
527 if (mem == NULL)
528 return NULL;
529
530 hdr = mem;
531 dst = mem;
532 mem += (n_desc + 1) * sizeof(*src);
533
534 /* Copy the descriptors. */
535 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
536 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
537
538 uvc_control_header = mem;
539 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530540 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200541 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
542 uvc_control_header->bInCollection = 1;
543 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
544
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100545 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530546 if (speed == USB_SPEED_SUPER)
547 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
548
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200549 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
550 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
551
552 uvc_streaming_header = mem;
553 UVC_COPY_DESCRIPTORS(mem, dst,
554 (const struct usb_descriptor_header**)uvc_streaming_cls);
555 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100556 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200557
558 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
559
560 *dst = NULL;
561 return hdr;
562}
563
564static void
565uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
566{
567 struct usb_composite_dev *cdev = c->cdev;
568 struct uvc_device *uvc = to_uvc(f);
569
570 INFO(cdev, "uvc_function_unbind\n");
571
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200572 video_unregister_device(uvc->vdev);
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300573 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200574 uvc->control_ep->driver_data = NULL;
575 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200576
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100577 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200578 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
579 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200580
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200581 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200582
583 kfree(uvc);
584}
585
586static int __init
587uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
588{
589 struct usb_composite_dev *cdev = c->cdev;
590 struct uvc_device *uvc = to_uvc(f);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100591 unsigned int max_packet_mult;
592 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200593 struct usb_ep *ep;
594 int ret = -EINVAL;
595
596 INFO(cdev, "uvc_function_bind\n");
597
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100598 /* Sanity check the streaming endpoint module parameters.
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530599 */
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100600 streaming_interval = clamp(streaming_interval, 1U, 16U);
601 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
602 streaming_maxburst = min(streaming_maxburst, 15U);
603
604 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
605 * module parameters.
606 *
607 * NOTE: We assume that the user knows what they are doing and won't
608 * give parameters that their UDC doesn't support.
609 */
610 if (streaming_maxpacket <= 1024) {
611 max_packet_mult = 1;
612 max_packet_size = streaming_maxpacket;
613 } else if (streaming_maxpacket <= 2048) {
614 max_packet_mult = 2;
615 max_packet_size = streaming_maxpacket / 2;
616 } else {
617 max_packet_mult = 3;
618 max_packet_size = streaming_maxpacket / 3;
619 }
620
621 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530622 uvc_fs_streaming_ep.bInterval = streaming_interval;
623
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100624 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
625 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
626 uvc_hs_streaming_ep.bInterval = streaming_interval;
627
628 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
629 uvc_ss_streaming_ep.bInterval = streaming_interval;
630 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
631 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
632 uvc_ss_streaming_comp.wBytesPerInterval =
633 max_packet_size * max_packet_mult * streaming_maxburst;
634
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200635 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100636 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200637 if (!ep) {
638 INFO(cdev, "Unable to allocate control EP\n");
639 goto error;
640 }
641 uvc->control_ep = ep;
642 ep->driver_data = uvc;
643
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100644 if (gadget_is_superspeed(c->cdev->gadget))
645 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
646 &uvc_ss_streaming_comp);
647 else if (gadget_is_dualspeed(cdev->gadget))
648 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
649 else
650 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
651
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200652 if (!ep) {
653 INFO(cdev, "Unable to allocate streaming EP\n");
654 goto error;
655 }
656 uvc->video.ep = ep;
657 ep->driver_data = uvc;
658
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100659 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
661 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100662
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200663 /* Allocate interface IDs. */
664 if ((ret = usb_interface_id(c, f)) < 0)
665 goto error;
666 uvc_iad.bFirstInterface = ret;
667 uvc_control_intf.bInterfaceNumber = ret;
668 uvc->control_intf = ret;
669
670 if ((ret = usb_interface_id(c, f)) < 0)
671 goto error;
672 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
673 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
674 uvc->streaming_intf = ret;
675
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200676 /* Copy descriptors */
677 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
678 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530679 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200680 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530681 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200682
683 /* Preallocate control endpoint request. */
684 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
685 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
686 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
687 ret = -ENOMEM;
688 goto error;
689 }
690
691 uvc->control_req->buf = uvc->control_buf;
692 uvc->control_req->complete = uvc_function_ep0_complete;
693 uvc->control_req->context = uvc;
694
695 /* Avoid letting this gadget enumerate until the userspace server is
696 * active.
697 */
698 if ((ret = usb_function_deactivate(f)) < 0)
699 goto error;
700
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300701 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
702 printk(KERN_INFO "v4l2_device_register failed\n");
703 goto error;
704 }
705
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200706 /* Initialise video. */
707 ret = uvc_video_init(&uvc->video);
708 if (ret < 0)
709 goto error;
710
711 /* Register a V4L2 device. */
712 ret = uvc_register_video(uvc);
713 if (ret < 0) {
714 printk(KERN_INFO "Unable to register video device\n");
715 goto error;
716 }
717
718 return 0;
719
720error:
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300721 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200722 if (uvc->vdev)
723 video_device_release(uvc->vdev);
724
725 if (uvc->control_ep)
726 uvc->control_ep->driver_data = NULL;
727 if (uvc->video.ep)
728 uvc->video.ep->driver_data = NULL;
729
730 if (uvc->control_req) {
731 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
732 kfree(uvc->control_buf);
733 }
734
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200735 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200736 return ret;
737}
738
739/* --------------------------------------------------------------------------
740 * USB gadget function
741 */
742
743/**
744 * uvc_bind_config - add a UVC function to a configuration
745 * @c: the configuration to support the UVC instance
746 * Context: single threaded during gadget setup
747 *
748 * Returns zero on success, else negative errno.
749 *
750 * Caller must have called @uvc_setup(). Caller is also responsible for
751 * calling @uvc_cleanup() before module unload.
752 */
753int __init
754uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530755 const struct uvc_descriptor_header * const *fs_control,
756 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200757 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530758 const struct uvc_descriptor_header * const *hs_streaming,
759 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200760{
761 struct uvc_device *uvc;
762 int ret = 0;
763
764 /* TODO Check if the USB device controller supports the required
765 * features.
766 */
767 if (!gadget_is_dualspeed(c->cdev->gadget))
768 return -EINVAL;
769
770 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
771 if (uvc == NULL)
772 return -ENOMEM;
773
774 uvc->state = UVC_STATE_DISCONNECTED;
775
776 /* Validate the descriptors. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530777 if (fs_control == NULL || fs_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100778 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530779 goto error;
780
781 if (ss_control == NULL || ss_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100782 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200783 goto error;
784
785 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100786 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200787 goto error;
788
789 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100790 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200791 goto error;
792
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530793 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100794 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530795 goto error;
796
797 uvc->desc.fs_control = fs_control;
798 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200799 uvc->desc.fs_streaming = fs_streaming;
800 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530801 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200802
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100803 /* String descriptors are global, we only need to allocate string IDs
804 * for the first UVC function. UVC functions beyond the first (if any)
805 * will reuse the same IDs.
806 */
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100807 if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200808 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
809 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530810 goto error;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200811 uvc_iad.iFunction =
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100812 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200813 uvc_control_intf.iInterface =
814 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
815 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530816 uvc_streaming_intf_alt0.iInterface = ret;
817 uvc_streaming_intf_alt1.iInterface = ret;
818 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200819
820 /* Register the function. */
821 uvc->func.name = "uvc";
822 uvc->func.strings = uvc_function_strings;
823 uvc->func.bind = uvc_function_bind;
824 uvc->func.unbind = uvc_function_unbind;
825 uvc->func.get_alt = uvc_function_get_alt;
826 uvc->func.set_alt = uvc_function_set_alt;
827 uvc->func.disable = uvc_function_disable;
828 uvc->func.setup = uvc_function_setup;
829
830 ret = usb_add_function(c, &uvc->func);
831 if (ret)
832 kfree(uvc);
833
Jassi Brar28f75f42011-06-25 00:17:26 +0530834 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200835
836error:
837 kfree(uvc);
838 return ret;
839}
840
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300841module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200842MODULE_PARM_DESC(trace, "Trace level bitmask");
843