blob: 825080d271a794a66220b260542fe3897175471b [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"
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030030#include "uvc_v4l2.h"
31#include "uvc_video.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020032
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030033unsigned int uvc_gadget_trace_param;
Andrzej Pietrasiewiczefb540c2014-09-08 11:18:16 +030034static unsigned int streaming_interval;
35static unsigned int streaming_maxpacket;
Laurent Pinchart20777dd2013-03-01 20:46:26 +010036static unsigned int streaming_maxburst;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053037
Laurent Pinchartcdda4792010-05-02 20:57:41 +020038/* --------------------------------------------------------------------------
39 * Function descriptors
40 */
41
42/* string IDs are assigned dynamically */
43
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010044#define UVC_STRING_CONTROL_IDX 0
45#define UVC_STRING_STREAMING_IDX 1
Laurent Pinchartcdda4792010-05-02 20:57:41 +020046
47static struct usb_string uvc_en_us_strings[] = {
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010048 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
Laurent Pinchartcdda4792010-05-02 20:57:41 +020049 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
50 { }
51};
52
53static struct usb_gadget_strings uvc_stringtab = {
54 .language = 0x0409, /* en-us */
55 .strings = uvc_en_us_strings,
56};
57
58static struct usb_gadget_strings *uvc_function_strings[] = {
59 &uvc_stringtab,
60 NULL,
61};
62
63#define UVC_INTF_VIDEO_CONTROL 0
64#define UVC_INTF_VIDEO_STREAMING 1
65
Laurent Pinchart912ca422013-03-01 20:46:23 +010066#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053067
Laurent Pinchartcdda4792010-05-02 20:57:41 +020068static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030069 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020070 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
71 .bFirstInterface = 0,
72 .bInterfaceCount = 2,
73 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030074 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020075 .bFunctionProtocol = 0x00,
76 .iFunction = 0,
77};
78
79static struct usb_interface_descriptor uvc_control_intf __initdata = {
80 .bLength = USB_DT_INTERFACE_SIZE,
81 .bDescriptorType = USB_DT_INTERFACE,
82 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
83 .bAlternateSetting = 0,
84 .bNumEndpoints = 1,
85 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030086 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020087 .bInterfaceProtocol = 0x00,
88 .iInterface = 0,
89};
90
Laurent Pinchart48eee0b2013-03-01 20:46:25 +010091static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020092 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +010096 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020097 .bInterval = 8,
98};
99
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100100static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
101 .bLength = sizeof(uvc_ss_control_comp),
102 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
103 /* The following 3 values can be tweaked if necessary. */
104 .bMaxBurst = 0,
105 .bmAttributes = 0,
106 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
107};
108
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200109static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
110 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
111 .bDescriptorType = USB_DT_CS_ENDPOINT,
112 .bDescriptorSubType = UVC_EP_INTERRUPT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100113 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200114};
115
116static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
117 .bLength = USB_DT_INTERFACE_SIZE,
118 .bDescriptorType = USB_DT_INTERFACE,
119 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
120 .bAlternateSetting = 0,
121 .bNumEndpoints = 0,
122 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300123 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200124 .bInterfaceProtocol = 0x00,
125 .iInterface = 0,
126};
127
128static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
129 .bLength = USB_DT_INTERFACE_SIZE,
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
132 .bAlternateSetting = 1,
133 .bNumEndpoints = 1,
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
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100140static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100144 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
145 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100146 /* The wMaxPacketSize and bInterval values will be initialized from
147 * module parameters.
148 */
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200149};
150
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100151static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100155 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
156 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100157 /* The wMaxPacketSize and bInterval values will be initialized from
158 * module parameters.
159 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530160};
161
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530162static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530165
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100166 .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 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530172};
173
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100174static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100175 .bLength = sizeof(uvc_ss_streaming_comp),
176 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchartdbbafe62013-04-29 12:16:30 +0200177 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
178 * initialized from module parameters.
179 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530180};
181
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200182static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
183 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530184 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200185 NULL,
186};
187
188static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
189 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530190 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
191 NULL,
192};
193
194static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
195 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
196 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
197 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200198 NULL,
199};
200
201/* --------------------------------------------------------------------------
202 * Control requests
203 */
204
205static void
206uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
207{
208 struct uvc_device *uvc = req->context;
209 struct v4l2_event v4l2_event;
210 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
211
212 if (uvc->event_setup_out) {
213 uvc->event_setup_out = 0;
214
215 memset(&v4l2_event, 0, sizeof(v4l2_event));
216 v4l2_event.type = UVC_EVENT_DATA;
217 uvc_event->data.length = req->actual;
218 memcpy(&uvc_event->data.data, req->buf, req->actual);
219 v4l2_event_queue(uvc->vdev, &v4l2_event);
220 }
221}
222
223static int
224uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
225{
226 struct uvc_device *uvc = to_uvc(f);
227 struct v4l2_event v4l2_event;
228 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
229
230 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
231 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
232 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
233 */
234
235 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
236 INFO(f->config->cdev, "invalid request type\n");
237 return -EINVAL;
238 }
239
240 /* Stall too big requests. */
241 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
242 return -EINVAL;
243
Laurent Pinchart26a029f2014-09-08 11:18:14 +0300244 /* Tell the complete callback to generate an event for the next request
245 * that will be enqueued by UVCIOC_SEND_RESPONSE.
246 */
247 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
248 uvc->event_length = le16_to_cpu(ctrl->wLength);
249
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200250 memset(&v4l2_event, 0, sizeof(v4l2_event));
251 v4l2_event.type = UVC_EVENT_SETUP;
252 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
253 v4l2_event_queue(uvc->vdev, &v4l2_event);
254
255 return 0;
256}
257
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100258void uvc_function_setup_continue(struct uvc_device *uvc)
259{
260 struct usb_composite_dev *cdev = uvc->func.config->cdev;
261
262 usb_composite_setup_continue(cdev);
263}
264
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200265static int
266uvc_function_get_alt(struct usb_function *f, unsigned interface)
267{
268 struct uvc_device *uvc = to_uvc(f);
269
270 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
271
272 if (interface == uvc->control_intf)
273 return 0;
274 else if (interface != uvc->streaming_intf)
275 return -EINVAL;
276 else
277 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
278}
279
280static int
281uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
282{
283 struct uvc_device *uvc = to_uvc(f);
284 struct v4l2_event v4l2_event;
285 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530286 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200287
288 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
289
290 if (interface == uvc->control_intf) {
291 if (alt)
292 return -EINVAL;
293
294 if (uvc->state == UVC_STATE_DISCONNECTED) {
295 memset(&v4l2_event, 0, sizeof(v4l2_event));
296 v4l2_event.type = UVC_EVENT_CONNECT;
297 uvc_event->speed = f->config->cdev->gadget->speed;
298 v4l2_event_queue(uvc->vdev, &v4l2_event);
299
300 uvc->state = UVC_STATE_CONNECTED;
301 }
302
303 return 0;
304 }
305
306 if (interface != uvc->streaming_intf)
307 return -EINVAL;
308
309 /* TODO
310 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
311 return alt ? -EINVAL : 0;
312 */
313
314 switch (alt) {
315 case 0:
316 if (uvc->state != UVC_STATE_STREAMING)
317 return 0;
318
319 if (uvc->video.ep)
320 usb_ep_disable(uvc->video.ep);
321
322 memset(&v4l2_event, 0, sizeof(v4l2_event));
323 v4l2_event.type = UVC_EVENT_STREAMOFF;
324 v4l2_event_queue(uvc->vdev, &v4l2_event);
325
326 uvc->state = UVC_STATE_CONNECTED;
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100327 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200328
329 case 1:
330 if (uvc->state != UVC_STATE_CONNECTED)
331 return 0;
332
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300333 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530334 ret = config_ep_by_speed(f->config->cdev->gadget,
335 &(uvc->func), uvc->video.ep);
336 if (ret)
337 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300338 usb_ep_enable(uvc->video.ep);
339 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200340
341 memset(&v4l2_event, 0, sizeof(v4l2_event));
342 v4l2_event.type = UVC_EVENT_STREAMON;
343 v4l2_event_queue(uvc->vdev, &v4l2_event);
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100344 return USB_GADGET_DELAYED_STATUS;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200345
346 default:
347 return -EINVAL;
348 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200349}
350
351static void
352uvc_function_disable(struct usb_function *f)
353{
354 struct uvc_device *uvc = to_uvc(f);
355 struct v4l2_event v4l2_event;
356
357 INFO(f->config->cdev, "uvc_function_disable\n");
358
359 memset(&v4l2_event, 0, sizeof(v4l2_event));
360 v4l2_event.type = UVC_EVENT_DISCONNECT;
361 v4l2_event_queue(uvc->vdev, &v4l2_event);
362
363 uvc->state = UVC_STATE_DISCONNECTED;
364}
365
366/* --------------------------------------------------------------------------
367 * Connection / disconnection
368 */
369
370void
371uvc_function_connect(struct uvc_device *uvc)
372{
373 struct usb_composite_dev *cdev = uvc->func.config->cdev;
374 int ret;
375
376 if ((ret = usb_function_activate(&uvc->func)) < 0)
377 INFO(cdev, "UVC connect failed with %d\n", ret);
378}
379
380void
381uvc_function_disconnect(struct uvc_device *uvc)
382{
383 struct usb_composite_dev *cdev = uvc->func.config->cdev;
384 int ret;
385
386 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
387 INFO(cdev, "UVC disconnect failed with %d\n", ret);
388}
389
390/* --------------------------------------------------------------------------
391 * USB probe and disconnect
392 */
393
394static int
395uvc_register_video(struct uvc_device *uvc)
396{
397 struct usb_composite_dev *cdev = uvc->func.config->cdev;
398 struct video_device *video;
399
400 /* TODO reference counting. */
401 video = video_device_alloc();
402 if (video == NULL)
403 return -ENOMEM;
404
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300405 video->v4l2_dev = &uvc->v4l2_dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200406 video->fops = &uvc_v4l2_fops;
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300407 video->ioctl_ops = &uvc_v4l2_ioctl_ops;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200408 video->release = video_device_release;
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300409 video->vfl_dir = VFL_DIR_TX;
Chen Gang4d2079c2013-02-02 15:48:54 +0800410 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200411
412 uvc->vdev = video;
413 video_set_drvdata(video, uvc);
414
415 return video_register_device(video, VFL_TYPE_GRABBER, -1);
416}
417
418#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
419 do { \
420 memcpy(mem, desc, (desc)->bLength); \
421 *(dst)++ = mem; \
422 mem += (desc)->bLength; \
423 } while (0);
424
425#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
426 do { \
427 const struct usb_descriptor_header * const *__src; \
428 for (__src = src; *__src; ++__src) { \
429 memcpy(mem, *__src, (*__src)->bLength); \
430 *dst++ = mem; \
431 mem += (*__src)->bLength; \
432 } \
433 } while (0)
434
435static struct usb_descriptor_header ** __init
436uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
437{
438 struct uvc_input_header_descriptor *uvc_streaming_header;
439 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530440 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200441 const struct uvc_descriptor_header * const *uvc_streaming_cls;
442 const struct usb_descriptor_header * const *uvc_streaming_std;
443 const struct usb_descriptor_header * const *src;
444 struct usb_descriptor_header **dst;
445 struct usb_descriptor_header **hdr;
446 unsigned int control_size;
447 unsigned int streaming_size;
448 unsigned int n_desc;
449 unsigned int bytes;
450 void *mem;
451
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530452 switch (speed) {
453 case USB_SPEED_SUPER:
454 uvc_control_desc = uvc->desc.ss_control;
455 uvc_streaming_cls = uvc->desc.ss_streaming;
456 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530457 break;
458
459 case USB_SPEED_HIGH:
460 uvc_control_desc = uvc->desc.fs_control;
461 uvc_streaming_cls = uvc->desc.hs_streaming;
462 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530463 break;
464
465 case USB_SPEED_FULL:
466 default:
467 uvc_control_desc = uvc->desc.fs_control;
468 uvc_streaming_cls = uvc->desc.fs_streaming;
469 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530470 break;
471 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200472
473 /* Descriptors layout
474 *
475 * uvc_iad
476 * uvc_control_intf
477 * Class-specific UVC control descriptors
478 * uvc_control_ep
479 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100480 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200481 * uvc_streaming_intf_alt0
482 * Class-specific UVC streaming descriptors
483 * uvc_{fs|hs}_streaming
484 */
485
486 /* Count descriptors and compute their size. */
487 control_size = 0;
488 streaming_size = 0;
489 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100490 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200491 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200492
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530493 if (speed == USB_SPEED_SUPER) {
494 bytes += uvc_ss_control_comp.bLength;
495 n_desc = 6;
496 } else {
497 n_desc = 5;
498 }
499
500 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100501 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200502 control_size += (*src)->bLength;
503 bytes += (*src)->bLength;
504 n_desc++;
505 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530506 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100507 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200508 streaming_size += (*src)->bLength;
509 bytes += (*src)->bLength;
510 n_desc++;
511 }
512 for (src = uvc_streaming_std; *src; ++src) {
513 bytes += (*src)->bLength;
514 n_desc++;
515 }
516
517 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
518 if (mem == NULL)
519 return NULL;
520
521 hdr = mem;
522 dst = mem;
523 mem += (n_desc + 1) * sizeof(*src);
524
525 /* Copy the descriptors. */
526 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
527 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
528
529 uvc_control_header = mem;
530 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530531 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200532 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
533 uvc_control_header->bInCollection = 1;
534 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
535
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100536 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530537 if (speed == USB_SPEED_SUPER)
538 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
539
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200540 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
541 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
542
543 uvc_streaming_header = mem;
544 UVC_COPY_DESCRIPTORS(mem, dst,
545 (const struct usb_descriptor_header**)uvc_streaming_cls);
546 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100547 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200548
549 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
550
551 *dst = NULL;
552 return hdr;
553}
554
555static void
556uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
557{
558 struct usb_composite_dev *cdev = c->cdev;
559 struct uvc_device *uvc = to_uvc(f);
560
561 INFO(cdev, "uvc_function_unbind\n");
562
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200563 video_unregister_device(uvc->vdev);
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300564 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200565 uvc->control_ep->driver_data = NULL;
566 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200567
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100568 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200569 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
570 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200571
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200572 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200573
574 kfree(uvc);
575}
576
577static int __init
578uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
579{
580 struct usb_composite_dev *cdev = c->cdev;
581 struct uvc_device *uvc = to_uvc(f);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100582 unsigned int max_packet_mult;
583 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200584 struct usb_ep *ep;
585 int ret = -EINVAL;
586
587 INFO(cdev, "uvc_function_bind\n");
588
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100589 /* Sanity check the streaming endpoint module parameters.
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530590 */
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100591 streaming_interval = clamp(streaming_interval, 1U, 16U);
592 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
593 streaming_maxburst = min(streaming_maxburst, 15U);
594
595 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
596 * module parameters.
597 *
598 * NOTE: We assume that the user knows what they are doing and won't
599 * give parameters that their UDC doesn't support.
600 */
601 if (streaming_maxpacket <= 1024) {
602 max_packet_mult = 1;
603 max_packet_size = streaming_maxpacket;
604 } else if (streaming_maxpacket <= 2048) {
605 max_packet_mult = 2;
606 max_packet_size = streaming_maxpacket / 2;
607 } else {
608 max_packet_mult = 3;
609 max_packet_size = streaming_maxpacket / 3;
610 }
611
612 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530613 uvc_fs_streaming_ep.bInterval = streaming_interval;
614
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100615 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
616 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
617 uvc_hs_streaming_ep.bInterval = streaming_interval;
618
619 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
620 uvc_ss_streaming_ep.bInterval = streaming_interval;
621 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
622 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
623 uvc_ss_streaming_comp.wBytesPerInterval =
624 max_packet_size * max_packet_mult * streaming_maxburst;
625
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200626 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100627 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200628 if (!ep) {
629 INFO(cdev, "Unable to allocate control EP\n");
630 goto error;
631 }
632 uvc->control_ep = ep;
633 ep->driver_data = uvc;
634
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100635 if (gadget_is_superspeed(c->cdev->gadget))
636 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
637 &uvc_ss_streaming_comp);
638 else if (gadget_is_dualspeed(cdev->gadget))
639 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
640 else
641 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
642
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200643 if (!ep) {
644 INFO(cdev, "Unable to allocate streaming EP\n");
645 goto error;
646 }
647 uvc->video.ep = ep;
648 ep->driver_data = uvc;
649
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100650 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
651 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
652 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100653
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200654 /* Allocate interface IDs. */
655 if ((ret = usb_interface_id(c, f)) < 0)
656 goto error;
657 uvc_iad.bFirstInterface = ret;
658 uvc_control_intf.bInterfaceNumber = ret;
659 uvc->control_intf = ret;
660
661 if ((ret = usb_interface_id(c, f)) < 0)
662 goto error;
663 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
664 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
665 uvc->streaming_intf = ret;
666
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200667 /* Copy descriptors */
668 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
669 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530670 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200671 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530672 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200673
674 /* Preallocate control endpoint request. */
675 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
676 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
677 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
678 ret = -ENOMEM;
679 goto error;
680 }
681
682 uvc->control_req->buf = uvc->control_buf;
683 uvc->control_req->complete = uvc_function_ep0_complete;
684 uvc->control_req->context = uvc;
685
686 /* Avoid letting this gadget enumerate until the userspace server is
687 * active.
688 */
689 if ((ret = usb_function_deactivate(f)) < 0)
690 goto error;
691
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300692 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
693 printk(KERN_INFO "v4l2_device_register failed\n");
694 goto error;
695 }
696
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200697 /* Initialise video. */
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300698 ret = uvcg_video_init(&uvc->video);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200699 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:
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300712 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200713 if (uvc->vdev)
714 video_device_release(uvc->vdev);
715
716 if (uvc->control_ep)
717 uvc->control_ep->driver_data = NULL;
718 if (uvc->video.ep)
719 uvc->video.ep->driver_data = NULL;
720
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200721 if (uvc->control_req)
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200722 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200723 kfree(uvc->control_buf);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200724
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,
Andrzej Pietrasiewiczefb540c2014-09-08 11:18:16 +0300749 const struct uvc_descriptor_header * const *ss_streaming,
750 unsigned int stream_interv, unsigned int stream_maxpkt,
751 unsigned int stream_maxburst, unsigned int trace)
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
Andrzej Pietrasiewiczefb540c2014-09-08 11:18:16 +0300789 streaming_interval = stream_interv;
790 streaming_maxpacket = stream_maxpkt;
791 streaming_maxburst = stream_maxburst;
792 uvc_gadget_trace_param = trace;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530793 uvc->desc.fs_control = fs_control;
794 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200795 uvc->desc.fs_streaming = fs_streaming;
796 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530797 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200798
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100799 /* String descriptors are global, we only need to allocate string IDs
800 * for the first UVC function. UVC functions beyond the first (if any)
801 * will reuse the same IDs.
802 */
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100803 if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200804 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
805 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530806 goto error;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200807 uvc_iad.iFunction =
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100808 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200809 uvc_control_intf.iInterface =
810 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
811 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530812 uvc_streaming_intf_alt0.iInterface = ret;
813 uvc_streaming_intf_alt1.iInterface = ret;
814 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200815
816 /* Register the function. */
817 uvc->func.name = "uvc";
818 uvc->func.strings = uvc_function_strings;
819 uvc->func.bind = uvc_function_bind;
820 uvc->func.unbind = uvc_function_unbind;
821 uvc->func.get_alt = uvc_function_get_alt;
822 uvc->func.set_alt = uvc_function_set_alt;
823 uvc->func.disable = uvc_function_disable;
824 uvc->func.setup = uvc_function_setup;
825
826 ret = usb_add_function(c, &uvc->func);
827 if (ret)
828 kfree(uvc);
829
Jassi Brar28f75f42011-06-25 00:17:26 +0530830 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200831
832error:
833 kfree(uvc);
834 return ret;
835}
836
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200837