blob: e9d625b3575142ca4735cd6ce7b6c76373ccdf2c [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 */
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200159};
160
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100161static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530162 .bLength = USB_DT_ENDPOINT_SIZE,
163 .bDescriptorType = USB_DT_ENDPOINT,
164 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100165 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
166 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100167 /* The wMaxPacketSize and bInterval values will be initialized from
168 * module parameters.
169 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530170};
171
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530172static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100173 .bLength = USB_DT_ENDPOINT_SIZE,
174 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530175
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100176 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100177 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
178 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100179 /* The wMaxPacketSize and bInterval values will be initialized from
180 * module parameters.
181 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530182};
183
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100184static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100185 .bLength = sizeof(uvc_ss_streaming_comp),
186 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchartdbbafe62013-04-29 12:16:30 +0200187 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
188 * initialized from module parameters.
189 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530190};
191
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200192static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
193 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530194 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200195 NULL,
196};
197
198static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
199 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530200 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
201 NULL,
202};
203
204static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
205 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
206 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
207 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200208 NULL,
209};
210
211/* --------------------------------------------------------------------------
212 * Control requests
213 */
214
215static void
216uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
217{
218 struct uvc_device *uvc = req->context;
219 struct v4l2_event v4l2_event;
220 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
221
222 if (uvc->event_setup_out) {
223 uvc->event_setup_out = 0;
224
225 memset(&v4l2_event, 0, sizeof(v4l2_event));
226 v4l2_event.type = UVC_EVENT_DATA;
227 uvc_event->data.length = req->actual;
228 memcpy(&uvc_event->data.data, req->buf, req->actual);
229 v4l2_event_queue(uvc->vdev, &v4l2_event);
230 }
231}
232
233static int
234uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
235{
236 struct uvc_device *uvc = to_uvc(f);
237 struct v4l2_event v4l2_event;
238 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
239
240 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
241 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
242 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
243 */
244
245 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
246 INFO(f->config->cdev, "invalid request type\n");
247 return -EINVAL;
248 }
249
250 /* Stall too big requests. */
251 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
252 return -EINVAL;
253
Laurent Pinchart26a029f2014-09-08 11:18:14 +0300254 /* Tell the complete callback to generate an event for the next request
255 * that will be enqueued by UVCIOC_SEND_RESPONSE.
256 */
257 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
258 uvc->event_length = le16_to_cpu(ctrl->wLength);
259
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200260 memset(&v4l2_event, 0, sizeof(v4l2_event));
261 v4l2_event.type = UVC_EVENT_SETUP;
262 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
263 v4l2_event_queue(uvc->vdev, &v4l2_event);
264
265 return 0;
266}
267
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100268void uvc_function_setup_continue(struct uvc_device *uvc)
269{
270 struct usb_composite_dev *cdev = uvc->func.config->cdev;
271
272 usb_composite_setup_continue(cdev);
273}
274
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200275static int
276uvc_function_get_alt(struct usb_function *f, unsigned interface)
277{
278 struct uvc_device *uvc = to_uvc(f);
279
280 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
281
282 if (interface == uvc->control_intf)
283 return 0;
284 else if (interface != uvc->streaming_intf)
285 return -EINVAL;
286 else
287 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
288}
289
290static int
291uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
292{
293 struct uvc_device *uvc = to_uvc(f);
294 struct v4l2_event v4l2_event;
295 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530296 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200297
298 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
299
300 if (interface == uvc->control_intf) {
301 if (alt)
302 return -EINVAL;
303
304 if (uvc->state == UVC_STATE_DISCONNECTED) {
305 memset(&v4l2_event, 0, sizeof(v4l2_event));
306 v4l2_event.type = UVC_EVENT_CONNECT;
307 uvc_event->speed = f->config->cdev->gadget->speed;
308 v4l2_event_queue(uvc->vdev, &v4l2_event);
309
310 uvc->state = UVC_STATE_CONNECTED;
311 }
312
313 return 0;
314 }
315
316 if (interface != uvc->streaming_intf)
317 return -EINVAL;
318
319 /* TODO
320 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
321 return alt ? -EINVAL : 0;
322 */
323
324 switch (alt) {
325 case 0:
326 if (uvc->state != UVC_STATE_STREAMING)
327 return 0;
328
329 if (uvc->video.ep)
330 usb_ep_disable(uvc->video.ep);
331
332 memset(&v4l2_event, 0, sizeof(v4l2_event));
333 v4l2_event.type = UVC_EVENT_STREAMOFF;
334 v4l2_event_queue(uvc->vdev, &v4l2_event);
335
336 uvc->state = UVC_STATE_CONNECTED;
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100337 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200338
339 case 1:
340 if (uvc->state != UVC_STATE_CONNECTED)
341 return 0;
342
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300343 if (uvc->video.ep) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530344 ret = config_ep_by_speed(f->config->cdev->gadget,
345 &(uvc->func), uvc->video.ep);
346 if (ret)
347 return ret;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300348 usb_ep_enable(uvc->video.ep);
349 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200350
351 memset(&v4l2_event, 0, sizeof(v4l2_event));
352 v4l2_event.type = UVC_EVENT_STREAMON;
353 v4l2_event_queue(uvc->vdev, &v4l2_event);
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100354 return USB_GADGET_DELAYED_STATUS;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200355
356 default:
357 return -EINVAL;
358 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200359}
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
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300415 video->v4l2_dev = &uvc->v4l2_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);
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300572 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200573 uvc->control_ep->driver_data = NULL;
574 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200575
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100576 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200577 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
578 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200579
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200580 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200581
582 kfree(uvc);
583}
584
585static int __init
586uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
587{
588 struct usb_composite_dev *cdev = c->cdev;
589 struct uvc_device *uvc = to_uvc(f);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100590 unsigned int max_packet_mult;
591 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200592 struct usb_ep *ep;
593 int ret = -EINVAL;
594
595 INFO(cdev, "uvc_function_bind\n");
596
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100597 /* Sanity check the streaming endpoint module parameters.
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530598 */
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100599 streaming_interval = clamp(streaming_interval, 1U, 16U);
600 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
601 streaming_maxburst = min(streaming_maxburst, 15U);
602
603 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
604 * module parameters.
605 *
606 * NOTE: We assume that the user knows what they are doing and won't
607 * give parameters that their UDC doesn't support.
608 */
609 if (streaming_maxpacket <= 1024) {
610 max_packet_mult = 1;
611 max_packet_size = streaming_maxpacket;
612 } else if (streaming_maxpacket <= 2048) {
613 max_packet_mult = 2;
614 max_packet_size = streaming_maxpacket / 2;
615 } else {
616 max_packet_mult = 3;
617 max_packet_size = streaming_maxpacket / 3;
618 }
619
620 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530621 uvc_fs_streaming_ep.bInterval = streaming_interval;
622
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100623 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
624 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
625 uvc_hs_streaming_ep.bInterval = streaming_interval;
626
627 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
628 uvc_ss_streaming_ep.bInterval = streaming_interval;
629 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
630 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
631 uvc_ss_streaming_comp.wBytesPerInterval =
632 max_packet_size * max_packet_mult * streaming_maxburst;
633
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200634 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100635 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200636 if (!ep) {
637 INFO(cdev, "Unable to allocate control EP\n");
638 goto error;
639 }
640 uvc->control_ep = ep;
641 ep->driver_data = uvc;
642
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100643 if (gadget_is_superspeed(c->cdev->gadget))
644 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
645 &uvc_ss_streaming_comp);
646 else if (gadget_is_dualspeed(cdev->gadget))
647 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
648 else
649 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
650
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200651 if (!ep) {
652 INFO(cdev, "Unable to allocate streaming EP\n");
653 goto error;
654 }
655 uvc->video.ep = ep;
656 ep->driver_data = uvc;
657
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100658 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100661
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200662 /* Allocate interface IDs. */
663 if ((ret = usb_interface_id(c, f)) < 0)
664 goto error;
665 uvc_iad.bFirstInterface = ret;
666 uvc_control_intf.bInterfaceNumber = ret;
667 uvc->control_intf = ret;
668
669 if ((ret = usb_interface_id(c, f)) < 0)
670 goto error;
671 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
672 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
673 uvc->streaming_intf = ret;
674
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200675 /* Copy descriptors */
676 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
677 if (gadget_is_dualspeed(cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530678 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200679 if (gadget_is_superspeed(c->cdev->gadget))
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530680 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200681
682 /* Preallocate control endpoint request. */
683 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
684 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
685 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
686 ret = -ENOMEM;
687 goto error;
688 }
689
690 uvc->control_req->buf = uvc->control_buf;
691 uvc->control_req->complete = uvc_function_ep0_complete;
692 uvc->control_req->context = uvc;
693
694 /* Avoid letting this gadget enumerate until the userspace server is
695 * active.
696 */
697 if ((ret = usb_function_deactivate(f)) < 0)
698 goto error;
699
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300700 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
701 printk(KERN_INFO "v4l2_device_register failed\n");
702 goto error;
703 }
704
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200705 /* Initialise video. */
706 ret = uvc_video_init(&uvc->video);
707 if (ret < 0)
708 goto error;
709
710 /* Register a V4L2 device. */
711 ret = uvc_register_video(uvc);
712 if (ret < 0) {
713 printk(KERN_INFO "Unable to register video device\n");
714 goto error;
715 }
716
717 return 0;
718
719error:
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300720 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200721 if (uvc->vdev)
722 video_device_release(uvc->vdev);
723
724 if (uvc->control_ep)
725 uvc->control_ep->driver_data = NULL;
726 if (uvc->video.ep)
727 uvc->video.ep->driver_data = NULL;
728
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200729 if (uvc->control_req)
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200730 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200731 kfree(uvc->control_buf);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200732
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200733 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200734 return ret;
735}
736
737/* --------------------------------------------------------------------------
738 * USB gadget function
739 */
740
741/**
742 * uvc_bind_config - add a UVC function to a configuration
743 * @c: the configuration to support the UVC instance
744 * Context: single threaded during gadget setup
745 *
746 * Returns zero on success, else negative errno.
747 *
748 * Caller must have called @uvc_setup(). Caller is also responsible for
749 * calling @uvc_cleanup() before module unload.
750 */
751int __init
752uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530753 const struct uvc_descriptor_header * const *fs_control,
754 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200755 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530756 const struct uvc_descriptor_header * const *hs_streaming,
757 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200758{
759 struct uvc_device *uvc;
760 int ret = 0;
761
762 /* TODO Check if the USB device controller supports the required
763 * features.
764 */
765 if (!gadget_is_dualspeed(c->cdev->gadget))
766 return -EINVAL;
767
768 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
769 if (uvc == NULL)
770 return -ENOMEM;
771
772 uvc->state = UVC_STATE_DISCONNECTED;
773
774 /* Validate the descriptors. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530775 if (fs_control == NULL || fs_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100776 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530777 goto error;
778
779 if (ss_control == NULL || ss_control[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100780 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200781 goto error;
782
783 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100784 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200785 goto error;
786
787 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100788 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200789 goto error;
790
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530791 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100792 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530793 goto error;
794
795 uvc->desc.fs_control = fs_control;
796 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200797 uvc->desc.fs_streaming = fs_streaming;
798 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530799 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200800
Laurent Pinchartf9e61202013-03-01 20:46:22 +0100801 /* String descriptors are global, we only need to allocate string IDs
802 * for the first UVC function. UVC functions beyond the first (if any)
803 * will reuse the same IDs.
804 */
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100805 if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200806 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
807 if (ret)
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530808 goto error;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200809 uvc_iad.iFunction =
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +0100810 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200811 uvc_control_intf.iInterface =
812 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
813 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530814 uvc_streaming_intf_alt0.iInterface = ret;
815 uvc_streaming_intf_alt1.iInterface = ret;
816 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200817
818 /* Register the function. */
819 uvc->func.name = "uvc";
820 uvc->func.strings = uvc_function_strings;
821 uvc->func.bind = uvc_function_bind;
822 uvc->func.unbind = uvc_function_unbind;
823 uvc->func.get_alt = uvc_function_get_alt;
824 uvc->func.set_alt = uvc_function_set_alt;
825 uvc->func.disable = uvc_function_disable;
826 uvc->func.setup = uvc_function_setup;
827
828 ret = usb_add_function(c, &uvc->func);
829 if (ret)
830 kfree(uvc);
831
Jassi Brar28f75f42011-06-25 00:17:26 +0530832 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200833
834error:
835 kfree(uvc);
836 return ret;
837}
838
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300839module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200840MODULE_PARM_DESC(trace, "Trace level bitmask");
841