blob: 76ec10fa5f2b35e6230a9f513b89622c50203453 [file] [log] [blame]
Laurent Pinchartcdda4792010-05-02 20:57:41 +02001/*
2 * uvc_gadget.c -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020011 */
12
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/fs.h>
17#include <linux/list.h>
18#include <linux/mutex.h>
Chen Gang4d2079c2013-02-02 15:48:54 +080019#include <linux/string.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020020#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include <linux/usb/video.h>
23#include <linux/vmalloc.h>
24#include <linux/wait.h>
25
26#include <media/v4l2-dev.h>
27#include <media/v4l2-event.h>
28
29#include "uvc.h"
30
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030031unsigned int uvc_gadget_trace_param;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020032
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053033/*-------------------------------------------------------------------------*/
34
35/* module parameters specific to the Video streaming endpoint */
Laurent Pinchart20777dd2013-03-01 20:46:26 +010036static unsigned int streaming_interval = 1;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053037module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
38MODULE_PARM_DESC(streaming_interval, "1 - 16");
39
Laurent Pinchart20777dd2013-03-01 20:46:26 +010040static unsigned int streaming_maxpacket = 1024;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053041module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
Laurent Pinchart20777dd2013-03-01 20:46:26 +010042MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053043
Laurent Pinchart20777dd2013-03-01 20:46:26 +010044static unsigned int streaming_maxburst;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053045module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
47
Laurent Pinchartcdda4792010-05-02 20:57:41 +020048/* --------------------------------------------------------------------------
49 * Function descriptors
50 */
51
52/* string IDs are assigned dynamically */
53
54#define UVC_STRING_ASSOCIATION_IDX 0
55#define UVC_STRING_CONTROL_IDX 1
56#define UVC_STRING_STREAMING_IDX 2
57
58static struct usb_string uvc_en_us_strings[] = {
59 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
60 [UVC_STRING_CONTROL_IDX].s = "Video Control",
61 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
62 { }
63};
64
65static struct usb_gadget_strings uvc_stringtab = {
66 .language = 0x0409, /* en-us */
67 .strings = uvc_en_us_strings,
68};
69
70static struct usb_gadget_strings *uvc_function_strings[] = {
71 &uvc_stringtab,
72 NULL,
73};
74
75#define UVC_INTF_VIDEO_CONTROL 0
76#define UVC_INTF_VIDEO_STREAMING 1
77
Laurent Pinchart912ca422013-03-01 20:46:23 +010078#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053079
Laurent Pinchartcdda4792010-05-02 20:57:41 +020080static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030081 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020082 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
83 .bFirstInterface = 0,
84 .bInterfaceCount = 2,
85 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030086 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020087 .bFunctionProtocol = 0x00,
88 .iFunction = 0,
89};
90
91static struct usb_interface_descriptor uvc_control_intf __initdata = {
92 .bLength = USB_DT_INTERFACE_SIZE,
93 .bDescriptorType = USB_DT_INTERFACE,
94 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
95 .bAlternateSetting = 0,
96 .bNumEndpoints = 1,
97 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030098 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020099 .bInterfaceProtocol = 0x00,
100 .iInterface = 0,
101};
102
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100103static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = USB_DIR_IN,
107 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100108 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200109 .bInterval = 8,
110};
111
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100112static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
113 .bLength = sizeof(uvc_ss_control_comp),
114 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
115 /* The following 3 values can be tweaked if necessary. */
116 .bMaxBurst = 0,
117 .bmAttributes = 0,
118 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
119};
120
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200121static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
122 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
123 .bDescriptorType = USB_DT_CS_ENDPOINT,
124 .bDescriptorSubType = UVC_EP_INTERRUPT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100125 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200126};
127
128static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
129 .bLength = USB_DT_INTERFACE_SIZE,
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
132 .bAlternateSetting = 0,
133 .bNumEndpoints = 0,
134 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300135 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200136 .bInterfaceProtocol = 0x00,
137 .iInterface = 0,
138};
139
140static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
141 .bLength = USB_DT_INTERFACE_SIZE,
142 .bDescriptorType = USB_DT_INTERFACE,
143 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
144 .bAlternateSetting = 1,
145 .bNumEndpoints = 1,
146 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300147 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200148 .bInterfaceProtocol = 0x00,
149 .iInterface = 0,
150};
151
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100152static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200153 .bLength = USB_DT_ENDPOINT_SIZE,
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100156 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
157 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100158 /* The wMaxPacketSize and bInterval values will be initialized from
159 * module parameters.
160 */
161 .wMaxPacketSize = 0,
162 .bInterval = 0,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200163};
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,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100169 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
170 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100171 /* The wMaxPacketSize and bInterval values will be initialized from
172 * module parameters.
173 */
174 .wMaxPacketSize = 0,
175 .bInterval = 0,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530176};
177
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530178static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530181
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100182 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100183 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
184 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100185 /* The wMaxPacketSize and bInterval values will be initialized from
186 * module parameters.
187 */
188 .wMaxPacketSize = 0,
189 .bInterval = 0,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530190};
191
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100192static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100193 .bLength = sizeof(uvc_ss_streaming_comp),
194 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100195 /* The following 3 values can be tweaked if necessary. */
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100196 .bMaxBurst = 0,
197 .bmAttributes = 0,
198 .wBytesPerInterval = cpu_to_le16(1024),
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530199};
200
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200201static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
202 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530203 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200204 NULL,
205};
206
207static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
208 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530209 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
210 NULL,
211};
212
213static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
214 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
215 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
216 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200217 NULL,
218};
219
220/* --------------------------------------------------------------------------
221 * Control requests
222 */
223
224static void
225uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
226{
227 struct uvc_device *uvc = req->context;
228 struct v4l2_event v4l2_event;
229 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
230
231 if (uvc->event_setup_out) {
232 uvc->event_setup_out = 0;
233
234 memset(&v4l2_event, 0, sizeof(v4l2_event));
235 v4l2_event.type = UVC_EVENT_DATA;
236 uvc_event->data.length = req->actual;
237 memcpy(&uvc_event->data.data, req->buf, req->actual);
238 v4l2_event_queue(uvc->vdev, &v4l2_event);
239 }
240}
241
242static int
243uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
244{
245 struct uvc_device *uvc = to_uvc(f);
246 struct v4l2_event v4l2_event;
247 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
248
249 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
250 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
251 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
252 */
253
254 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
255 INFO(f->config->cdev, "invalid request type\n");
256 return -EINVAL;
257 }
258
259 /* Stall too big requests. */
260 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
261 return -EINVAL;
262
263 memset(&v4l2_event, 0, sizeof(v4l2_event));
264 v4l2_event.type = UVC_EVENT_SETUP;
265 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
266 v4l2_event_queue(uvc->vdev, &v4l2_event);
267
268 return 0;
269}
270
271static int
272uvc_function_get_alt(struct usb_function *f, unsigned interface)
273{
274 struct uvc_device *uvc = to_uvc(f);
275
276 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
277
278 if (interface == uvc->control_intf)
279 return 0;
280 else if (interface != uvc->streaming_intf)
281 return -EINVAL;
282 else
283 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
284}
285
286static int
287uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
288{
289 struct uvc_device *uvc = to_uvc(f);
290 struct v4l2_event v4l2_event;
291 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530292 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200293
294 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
295
296 if (interface == uvc->control_intf) {
297 if (alt)
298 return -EINVAL;
299
300 if (uvc->state == UVC_STATE_DISCONNECTED) {
301 memset(&v4l2_event, 0, sizeof(v4l2_event));
302 v4l2_event.type = UVC_EVENT_CONNECT;
303 uvc_event->speed = f->config->cdev->gadget->speed;
304 v4l2_event_queue(uvc->vdev, &v4l2_event);
305
306 uvc->state = UVC_STATE_CONNECTED;
307 }
308
309 return 0;
310 }
311
312 if (interface != uvc->streaming_intf)
313 return -EINVAL;
314
315 /* TODO
316 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
317 return alt ? -EINVAL : 0;
318 */
319
320 switch (alt) {
321 case 0:
322 if (uvc->state != UVC_STATE_STREAMING)
323 return 0;
324
325 if (uvc->video.ep)
326 usb_ep_disable(uvc->video.ep);
327
328 memset(&v4l2_event, 0, sizeof(v4l2_event));
329 v4l2_event.type = UVC_EVENT_STREAMOFF;
330 v4l2_event_queue(uvc->vdev, &v4l2_event);
331
332 uvc->state = UVC_STATE_CONNECTED;
333 break;
334
335 case 1:
336 if (uvc->state != UVC_STATE_CONNECTED)
337 return 0;
338
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300339 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530340 ret = config_ep_by_speed(f->config->cdev->gadget,
341 &(uvc->func), uvc->video.ep);
342 if (ret)
343 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300344 usb_ep_enable(uvc->video.ep);
345 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200346
347 memset(&v4l2_event, 0, sizeof(v4l2_event));
348 v4l2_event.type = UVC_EVENT_STREAMON;
349 v4l2_event_queue(uvc->vdev, &v4l2_event);
350
351 uvc->state = UVC_STATE_STREAMING;
352 break;
353
354 default:
355 return -EINVAL;
356 }
357
358 return 0;
359}
360
361static void
362uvc_function_disable(struct usb_function *f)
363{
364 struct uvc_device *uvc = to_uvc(f);
365 struct v4l2_event v4l2_event;
366
367 INFO(f->config->cdev, "uvc_function_disable\n");
368
369 memset(&v4l2_event, 0, sizeof(v4l2_event));
370 v4l2_event.type = UVC_EVENT_DISCONNECT;
371 v4l2_event_queue(uvc->vdev, &v4l2_event);
372
373 uvc->state = UVC_STATE_DISCONNECTED;
374}
375
376/* --------------------------------------------------------------------------
377 * Connection / disconnection
378 */
379
380void
381uvc_function_connect(struct uvc_device *uvc)
382{
383 struct usb_composite_dev *cdev = uvc->func.config->cdev;
384 int ret;
385
386 if ((ret = usb_function_activate(&uvc->func)) < 0)
387 INFO(cdev, "UVC connect failed with %d\n", ret);
388}
389
390void
391uvc_function_disconnect(struct uvc_device *uvc)
392{
393 struct usb_composite_dev *cdev = uvc->func.config->cdev;
394 int ret;
395
396 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
397 INFO(cdev, "UVC disconnect failed with %d\n", ret);
398}
399
400/* --------------------------------------------------------------------------
401 * USB probe and disconnect
402 */
403
404static int
405uvc_register_video(struct uvc_device *uvc)
406{
407 struct usb_composite_dev *cdev = uvc->func.config->cdev;
408 struct video_device *video;
409
410 /* TODO reference counting. */
411 video = video_device_alloc();
412 if (video == NULL)
413 return -ENOMEM;
414
415 video->parent = &cdev->gadget->dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200416 video->fops = &uvc_v4l2_fops;
417 video->release = video_device_release;
Chen Gang4d2079c2013-02-02 15:48:54 +0800418 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200419
420 uvc->vdev = video;
421 video_set_drvdata(video, uvc);
422
423 return video_register_device(video, VFL_TYPE_GRABBER, -1);
424}
425
426#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
427 do { \
428 memcpy(mem, desc, (desc)->bLength); \
429 *(dst)++ = mem; \
430 mem += (desc)->bLength; \
431 } while (0);
432
433#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
434 do { \
435 const struct usb_descriptor_header * const *__src; \
436 for (__src = src; *__src; ++__src) { \
437 memcpy(mem, *__src, (*__src)->bLength); \
438 *dst++ = mem; \
439 mem += (*__src)->bLength; \
440 } \
441 } while (0)
442
443static struct usb_descriptor_header ** __init
444uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
445{
446 struct uvc_input_header_descriptor *uvc_streaming_header;
447 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530448 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200449 const struct uvc_descriptor_header * const *uvc_streaming_cls;
450 const struct usb_descriptor_header * const *uvc_streaming_std;
451 const struct usb_descriptor_header * const *src;
452 struct usb_descriptor_header **dst;
453 struct usb_descriptor_header **hdr;
454 unsigned int control_size;
455 unsigned int streaming_size;
456 unsigned int n_desc;
457 unsigned int bytes;
458 void *mem;
459
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530460 switch (speed) {
461 case USB_SPEED_SUPER:
462 uvc_control_desc = uvc->desc.ss_control;
463 uvc_streaming_cls = uvc->desc.ss_streaming;
464 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530465 break;
466
467 case USB_SPEED_HIGH:
468 uvc_control_desc = uvc->desc.fs_control;
469 uvc_streaming_cls = uvc->desc.hs_streaming;
470 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530471 break;
472
473 case USB_SPEED_FULL:
474 default:
475 uvc_control_desc = uvc->desc.fs_control;
476 uvc_streaming_cls = uvc->desc.fs_streaming;
477 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530478 break;
479 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200480
481 /* Descriptors layout
482 *
483 * uvc_iad
484 * uvc_control_intf
485 * Class-specific UVC control descriptors
486 * uvc_control_ep
487 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100488 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200489 * uvc_streaming_intf_alt0
490 * Class-specific UVC streaming descriptors
491 * uvc_{fs|hs}_streaming
492 */
493
494 /* Count descriptors and compute their size. */
495 control_size = 0;
496 streaming_size = 0;
497 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100498 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200499 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200500
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530501 if (speed == USB_SPEED_SUPER) {
502 bytes += uvc_ss_control_comp.bLength;
503 n_desc = 6;
504 } else {
505 n_desc = 5;
506 }
507
508 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100509 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200510 control_size += (*src)->bLength;
511 bytes += (*src)->bLength;
512 n_desc++;
513 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530514 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100515 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200516 streaming_size += (*src)->bLength;
517 bytes += (*src)->bLength;
518 n_desc++;
519 }
520 for (src = uvc_streaming_std; *src; ++src) {
521 bytes += (*src)->bLength;
522 n_desc++;
523 }
524
525 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
526 if (mem == NULL)
527 return NULL;
528
529 hdr = mem;
530 dst = mem;
531 mem += (n_desc + 1) * sizeof(*src);
532
533 /* Copy the descriptors. */
534 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
535 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
536
537 uvc_control_header = mem;
538 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530539 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200540 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
541 uvc_control_header->bInCollection = 1;
542 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
543
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100544 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530545 if (speed == USB_SPEED_SUPER)
546 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
547
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200548 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
549 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
550
551 uvc_streaming_header = mem;
552 UVC_COPY_DESCRIPTORS(mem, dst,
553 (const struct usb_descriptor_header**)uvc_streaming_cls);
554 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100555 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200556
557 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
558
559 *dst = NULL;
560 return hdr;
561}
562
563static void
564uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
565{
566 struct usb_composite_dev *cdev = c->cdev;
567 struct uvc_device *uvc = to_uvc(f);
568
569 INFO(cdev, "uvc_function_unbind\n");
570
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200571 video_unregister_device(uvc->vdev);
572 uvc->control_ep->driver_data = NULL;
573 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200574
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200575 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200576 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
577 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200578
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200579 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200580
581 kfree(uvc);
582}
583
584static int __init
585uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
586{
587 struct usb_composite_dev *cdev = c->cdev;
588 struct uvc_device *uvc = to_uvc(f);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100589 unsigned int max_packet_mult;
590 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200591 struct usb_ep *ep;
592 int ret = -EINVAL;
593
594 INFO(cdev, "uvc_function_bind\n");
595
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100596 /* Sanity check the streaming endpoint module parameters.
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530597 */
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100598 streaming_interval = clamp(streaming_interval, 1U, 16U);
599 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
600 streaming_maxburst = min(streaming_maxburst, 15U);
601
602 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
603 * module parameters.
604 *
605 * NOTE: We assume that the user knows what they are doing and won't
606 * give parameters that their UDC doesn't support.
607 */
608 if (streaming_maxpacket <= 1024) {
609 max_packet_mult = 1;
610 max_packet_size = streaming_maxpacket;
611 } else if (streaming_maxpacket <= 2048) {
612 max_packet_mult = 2;
613 max_packet_size = streaming_maxpacket / 2;
614 } else {
615 max_packet_mult = 3;
616 max_packet_size = streaming_maxpacket / 3;
617 }
618
619 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530620 uvc_fs_streaming_ep.bInterval = streaming_interval;
621
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100622 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
623 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
624 uvc_hs_streaming_ep.bInterval = streaming_interval;
625
626 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
627 uvc_ss_streaming_ep.bInterval = streaming_interval;
628 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
629 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
630 uvc_ss_streaming_comp.wBytesPerInterval =
631 max_packet_size * max_packet_mult * streaming_maxburst;
632
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200633 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100634 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200635 if (!ep) {
636 INFO(cdev, "Unable to allocate control EP\n");
637 goto error;
638 }
639 uvc->control_ep = ep;
640 ep->driver_data = uvc;
641
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100642 if (gadget_is_superspeed(c->cdev->gadget))
643 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
644 &uvc_ss_streaming_comp);
645 else if (gadget_is_dualspeed(cdev->gadget))
646 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
647 else
648 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
649
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200650 if (!ep) {
651 INFO(cdev, "Unable to allocate streaming EP\n");
652 goto error;
653 }
654 uvc->video.ep = ep;
655 ep->driver_data = uvc;
656
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100657 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
658 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100660
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200661 /* Allocate interface IDs. */
662 if ((ret = usb_interface_id(c, f)) < 0)
663 goto error;
664 uvc_iad.bFirstInterface = ret;
665 uvc_control_intf.bInterfaceNumber = ret;
666 uvc->control_intf = ret;
667
668 if ((ret = usb_interface_id(c, f)) < 0)
669 goto error;
670 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
671 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
672 uvc->streaming_intf = ret;
673
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200674 /* Copy descriptors */
675 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
676 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530677 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200678 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530679 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200680
681 /* Preallocate control endpoint request. */
682 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
683 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
684 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
685 ret = -ENOMEM;
686 goto error;
687 }
688
689 uvc->control_req->buf = uvc->control_buf;
690 uvc->control_req->complete = uvc_function_ep0_complete;
691 uvc->control_req->context = uvc;
692
693 /* Avoid letting this gadget enumerate until the userspace server is
694 * active.
695 */
696 if ((ret = usb_function_deactivate(f)) < 0)
697 goto error;
698
699 /* Initialise video. */
700 ret = uvc_video_init(&uvc->video);
701 if (ret < 0)
702 goto error;
703
704 /* Register a V4L2 device. */
705 ret = uvc_register_video(uvc);
706 if (ret < 0) {
707 printk(KERN_INFO "Unable to register video device\n");
708 goto error;
709 }
710
711 return 0;
712
713error:
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200714 if (uvc->vdev)
715 video_device_release(uvc->vdev);
716
717 if (uvc->control_ep)
718 uvc->control_ep->driver_data = NULL;
719 if (uvc->video.ep)
720 uvc->video.ep->driver_data = NULL;
721
722 if (uvc->control_req) {
723 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
724 kfree(uvc->control_buf);
725 }
726
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200727 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200728 return ret;
729}
730
731/* --------------------------------------------------------------------------
732 * USB gadget function
733 */
734
735/**
736 * uvc_bind_config - add a UVC function to a configuration
737 * @c: the configuration to support the UVC instance
738 * Context: single threaded during gadget setup
739 *
740 * Returns zero on success, else negative errno.
741 *
742 * Caller must have called @uvc_setup(). Caller is also responsible for
743 * calling @uvc_cleanup() before module unload.
744 */
745int __init
746uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530747 const struct uvc_descriptor_header * const *fs_control,
748 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200749 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530750 const struct uvc_descriptor_header * const *hs_streaming,
751 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200752{
753 struct uvc_device *uvc;
754 int ret = 0;
755
756 /* TODO Check if the USB device controller supports the required
757 * features.
758 */
759 if (!gadget_is_dualspeed(c->cdev->gadget))
760 return -EINVAL;
761
762 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
763 if (uvc == NULL)
764 return -ENOMEM;
765
766 uvc->state = UVC_STATE_DISCONNECTED;
767
768 /* Validate the descriptors. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530769 if (fs_control == NULL || fs_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100770 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530771 goto error;
772
773 if (ss_control == NULL || ss_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100774 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200775 goto error;
776
777 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100778 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200779 goto error;
780
781 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100782 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200783 goto error;
784
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530785 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100786 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530787 goto error;
788
789 uvc->desc.fs_control = fs_control;
790 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200791 uvc->desc.fs_streaming = fs_streaming;
792 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530793 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200794
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100795 /* String descriptors are global, we only need to allocate string IDs
796 * for the first UVC function. UVC functions beyond the first (if any)
797 * will reuse the same IDs.
798 */
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530799 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200800 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
801 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530802 goto error;
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200803 uvc_iad.iFunction =
804 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id;
805 uvc_control_intf.iInterface =
806 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
807 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530808 uvc_streaming_intf_alt0.iInterface = ret;
809 uvc_streaming_intf_alt1.iInterface = ret;
810 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200811
812 /* Register the function. */
813 uvc->func.name = "uvc";
814 uvc->func.strings = uvc_function_strings;
815 uvc->func.bind = uvc_function_bind;
816 uvc->func.unbind = uvc_function_unbind;
817 uvc->func.get_alt = uvc_function_get_alt;
818 uvc->func.set_alt = uvc_function_set_alt;
819 uvc->func.disable = uvc_function_disable;
820 uvc->func.setup = uvc_function_setup;
821
822 ret = usb_add_function(c, &uvc->func);
823 if (ret)
824 kfree(uvc);
825
Jassi Brar28f75f42011-06-25 00:17:26 +0530826 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200827
828error:
829 kfree(uvc);
830 return ret;
831}
832
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300833module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200834MODULE_PARM_DESC(trace, "Trace level bitmask");
835