blob: 10f13c1213c71f2a6b338c128d56571a6989f6bc [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>
19#include <linux/usb/ch9.h>
20#include <linux/usb/gadget.h>
21#include <linux/usb/video.h>
22#include <linux/vmalloc.h>
23#include <linux/wait.h>
24
25#include <media/v4l2-dev.h>
26#include <media/v4l2-event.h>
27
28#include "uvc.h"
29
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030030unsigned int uvc_gadget_trace_param;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020031
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053032/*-------------------------------------------------------------------------*/
33
34/* module parameters specific to the Video streaming endpoint */
35static unsigned streaming_interval = 1;
36module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
37MODULE_PARM_DESC(streaming_interval, "1 - 16");
38
39static unsigned streaming_maxpacket = 1024;
40module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
41MODULE_PARM_DESC(streaming_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
42
43static unsigned streaming_mult;
44module_param(streaming_mult, uint, S_IRUGO|S_IWUSR);
45MODULE_PARM_DESC(streaming_mult, "0 - 2 (hs/ss only)");
46
47static unsigned streaming_maxburst;
48module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
49MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
50
Laurent Pinchartcdda4792010-05-02 20:57:41 +020051/* --------------------------------------------------------------------------
52 * Function descriptors
53 */
54
55/* string IDs are assigned dynamically */
56
57#define UVC_STRING_ASSOCIATION_IDX 0
58#define UVC_STRING_CONTROL_IDX 1
59#define UVC_STRING_STREAMING_IDX 2
60
61static struct usb_string uvc_en_us_strings[] = {
62 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
63 [UVC_STRING_CONTROL_IDX].s = "Video Control",
64 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
65 { }
66};
67
68static struct usb_gadget_strings uvc_stringtab = {
69 .language = 0x0409, /* en-us */
70 .strings = uvc_en_us_strings,
71};
72
73static struct usb_gadget_strings *uvc_function_strings[] = {
74 &uvc_stringtab,
75 NULL,
76};
77
78#define UVC_INTF_VIDEO_CONTROL 0
79#define UVC_INTF_VIDEO_STREAMING 1
80
Bhupesh Sharma57976632012-06-01 15:08:55 +053081#define STATUS_BYTECOUNT 16 /* 16 bytes status */
82
Laurent Pinchartcdda4792010-05-02 20:57:41 +020083static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030084 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020085 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
86 .bFirstInterface = 0,
87 .bInterfaceCount = 2,
88 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030089 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020090 .bFunctionProtocol = 0x00,
91 .iFunction = 0,
92};
93
94static struct usb_interface_descriptor uvc_control_intf __initdata = {
95 .bLength = USB_DT_INTERFACE_SIZE,
96 .bDescriptorType = USB_DT_INTERFACE,
97 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
98 .bAlternateSetting = 0,
99 .bNumEndpoints = 1,
100 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300101 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200102 .bInterfaceProtocol = 0x00,
103 .iInterface = 0,
104};
105
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530106static struct usb_endpoint_descriptor uvc_fs_control_ep __initdata = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bEndpointAddress = USB_DIR_IN,
110 .bmAttributes = USB_ENDPOINT_XFER_INT,
Bhupesh Sharma57976632012-06-01 15:08:55 +0530111 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200112 .bInterval = 8,
113};
114
115static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
116 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
117 .bDescriptorType = USB_DT_CS_ENDPOINT,
118 .bDescriptorSubType = UVC_EP_INTERRUPT,
Bhupesh Sharma57976632012-06-01 15:08:55 +0530119 .wMaxTransferSize = cpu_to_le16(STATUS_BYTECOUNT),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200120};
121
122static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
123 .bLength = USB_DT_INTERFACE_SIZE,
124 .bDescriptorType = USB_DT_INTERFACE,
125 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
126 .bAlternateSetting = 0,
127 .bNumEndpoints = 0,
128 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300129 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200130 .bInterfaceProtocol = 0x00,
131 .iInterface = 0,
132};
133
134static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
135 .bLength = USB_DT_INTERFACE_SIZE,
136 .bDescriptorType = USB_DT_INTERFACE,
137 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
138 .bAlternateSetting = 1,
139 .bNumEndpoints = 1,
140 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300141 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200142 .bInterfaceProtocol = 0x00,
143 .iInterface = 0,
144};
145
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530146static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200147 .bLength = USB_DT_ENDPOINT_SIZE,
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = USB_DIR_IN,
150 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
151 .wMaxPacketSize = cpu_to_le16(512),
152 .bInterval = 1,
153};
154
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530155static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
156 .bLength = USB_DT_ENDPOINT_SIZE,
157 .bDescriptorType = USB_DT_ENDPOINT,
158 .bEndpointAddress = USB_DIR_IN,
159 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
160 .wMaxPacketSize = cpu_to_le16(1024),
161 .bInterval = 1,
162};
163
164/* super speed support */
165static struct usb_endpoint_descriptor uvc_ss_control_ep __initdata = {
166 .bLength = USB_DT_ENDPOINT_SIZE,
167 .bDescriptorType = USB_DT_ENDPOINT,
168
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_INT,
171 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
172 .bInterval = 8,
173};
174
175static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
176 .bLength = sizeof uvc_ss_control_comp,
177 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
178
179 /* the following 3 values can be tweaked if necessary */
180 /* .bMaxBurst = 0, */
181 /* .bmAttributes = 0, */
182 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
183};
184
185static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
188
189 .bEndpointAddress = USB_DIR_IN,
190 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
191 .wMaxPacketSize = cpu_to_le16(1024),
192 .bInterval = 4,
193};
194
195static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
196 .bLength = sizeof uvc_ss_streaming_comp,
197 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
198
199 /* the following 3 values can be tweaked if necessary */
200 .bMaxBurst = 0,
201 .bmAttributes = 0,
202 .wBytesPerInterval = cpu_to_le16(1024),
203};
204
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200205static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
206 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530207 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200208 NULL,
209};
210
211static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
212 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530213 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
214 NULL,
215};
216
217static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
218 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
219 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
220 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200221 NULL,
222};
223
224/* --------------------------------------------------------------------------
225 * Control requests
226 */
227
228static void
229uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
230{
231 struct uvc_device *uvc = req->context;
232 struct v4l2_event v4l2_event;
233 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
234
235 if (uvc->event_setup_out) {
236 uvc->event_setup_out = 0;
237
238 memset(&v4l2_event, 0, sizeof(v4l2_event));
239 v4l2_event.type = UVC_EVENT_DATA;
240 uvc_event->data.length = req->actual;
241 memcpy(&uvc_event->data.data, req->buf, req->actual);
242 v4l2_event_queue(uvc->vdev, &v4l2_event);
243 }
244}
245
246static int
247uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
248{
249 struct uvc_device *uvc = to_uvc(f);
250 struct v4l2_event v4l2_event;
251 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
252
253 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
254 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
255 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
256 */
257
258 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
259 INFO(f->config->cdev, "invalid request type\n");
260 return -EINVAL;
261 }
262
263 /* Stall too big requests. */
264 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
265 return -EINVAL;
266
267 memset(&v4l2_event, 0, sizeof(v4l2_event));
268 v4l2_event.type = UVC_EVENT_SETUP;
269 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
270 v4l2_event_queue(uvc->vdev, &v4l2_event);
271
272 return 0;
273}
274
275static 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;
337 break;
338
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);
354
355 uvc->state = UVC_STATE_STREAMING;
356 break;
357
358 default:
359 return -EINVAL;
360 }
361
362 return 0;
363}
364
365static void
366uvc_function_disable(struct usb_function *f)
367{
368 struct uvc_device *uvc = to_uvc(f);
369 struct v4l2_event v4l2_event;
370
371 INFO(f->config->cdev, "uvc_function_disable\n");
372
373 memset(&v4l2_event, 0, sizeof(v4l2_event));
374 v4l2_event.type = UVC_EVENT_DISCONNECT;
375 v4l2_event_queue(uvc->vdev, &v4l2_event);
376
377 uvc->state = UVC_STATE_DISCONNECTED;
378}
379
380/* --------------------------------------------------------------------------
381 * Connection / disconnection
382 */
383
384void
385uvc_function_connect(struct uvc_device *uvc)
386{
387 struct usb_composite_dev *cdev = uvc->func.config->cdev;
388 int ret;
389
390 if ((ret = usb_function_activate(&uvc->func)) < 0)
391 INFO(cdev, "UVC connect failed with %d\n", ret);
392}
393
394void
395uvc_function_disconnect(struct uvc_device *uvc)
396{
397 struct usb_composite_dev *cdev = uvc->func.config->cdev;
398 int ret;
399
400 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
401 INFO(cdev, "UVC disconnect failed with %d\n", ret);
402}
403
404/* --------------------------------------------------------------------------
405 * USB probe and disconnect
406 */
407
408static int
409uvc_register_video(struct uvc_device *uvc)
410{
411 struct usb_composite_dev *cdev = uvc->func.config->cdev;
412 struct video_device *video;
413
414 /* TODO reference counting. */
415 video = video_device_alloc();
416 if (video == NULL)
417 return -ENOMEM;
418
419 video->parent = &cdev->gadget->dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200420 video->fops = &uvc_v4l2_fops;
421 video->release = video_device_release;
422 strncpy(video->name, cdev->gadget->name, sizeof(video->name));
423
424 uvc->vdev = video;
425 video_set_drvdata(video, uvc);
426
427 return video_register_device(video, VFL_TYPE_GRABBER, -1);
428}
429
430#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
431 do { \
432 memcpy(mem, desc, (desc)->bLength); \
433 *(dst)++ = mem; \
434 mem += (desc)->bLength; \
435 } while (0);
436
437#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
438 do { \
439 const struct usb_descriptor_header * const *__src; \
440 for (__src = src; *__src; ++__src) { \
441 memcpy(mem, *__src, (*__src)->bLength); \
442 *dst++ = mem; \
443 mem += (*__src)->bLength; \
444 } \
445 } while (0)
446
447static struct usb_descriptor_header ** __init
448uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
449{
450 struct uvc_input_header_descriptor *uvc_streaming_header;
451 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530452 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200453 const struct uvc_descriptor_header * const *uvc_streaming_cls;
454 const struct usb_descriptor_header * const *uvc_streaming_std;
455 const struct usb_descriptor_header * const *src;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530456 static struct usb_endpoint_descriptor *uvc_control_ep;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200457 struct usb_descriptor_header **dst;
458 struct usb_descriptor_header **hdr;
459 unsigned int control_size;
460 unsigned int streaming_size;
461 unsigned int n_desc;
462 unsigned int bytes;
463 void *mem;
464
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530465 switch (speed) {
466 case USB_SPEED_SUPER:
467 uvc_control_desc = uvc->desc.ss_control;
468 uvc_streaming_cls = uvc->desc.ss_streaming;
469 uvc_streaming_std = uvc_ss_streaming;
470 uvc_control_ep = &uvc_ss_control_ep;
471 break;
472
473 case USB_SPEED_HIGH:
474 uvc_control_desc = uvc->desc.fs_control;
475 uvc_streaming_cls = uvc->desc.hs_streaming;
476 uvc_streaming_std = uvc_hs_streaming;
477 uvc_control_ep = &uvc_fs_control_ep;
478 break;
479
480 case USB_SPEED_FULL:
481 default:
482 uvc_control_desc = uvc->desc.fs_control;
483 uvc_streaming_cls = uvc->desc.fs_streaming;
484 uvc_streaming_std = uvc_fs_streaming;
485 uvc_control_ep = &uvc_fs_control_ep;
486 break;
487 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200488
489 /* Descriptors layout
490 *
491 * uvc_iad
492 * uvc_control_intf
493 * Class-specific UVC control descriptors
494 * uvc_control_ep
495 * uvc_control_cs_ep
496 * uvc_streaming_intf_alt0
497 * Class-specific UVC streaming descriptors
498 * uvc_{fs|hs}_streaming
499 */
500
501 /* Count descriptors and compute their size. */
502 control_size = 0;
503 streaming_size = 0;
504 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530505 + uvc_control_ep->bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200506 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200507
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530508 if (speed == USB_SPEED_SUPER) {
509 bytes += uvc_ss_control_comp.bLength;
510 n_desc = 6;
511 } else {
512 n_desc = 5;
513 }
514
515 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
516 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200517 control_size += (*src)->bLength;
518 bytes += (*src)->bLength;
519 n_desc++;
520 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530521 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
522 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200523 streaming_size += (*src)->bLength;
524 bytes += (*src)->bLength;
525 n_desc++;
526 }
527 for (src = uvc_streaming_std; *src; ++src) {
528 bytes += (*src)->bLength;
529 n_desc++;
530 }
531
532 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
533 if (mem == NULL)
534 return NULL;
535
536 hdr = mem;
537 dst = mem;
538 mem += (n_desc + 1) * sizeof(*src);
539
540 /* Copy the descriptors. */
541 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
542 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
543
544 uvc_control_header = mem;
545 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530546 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200547 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
548 uvc_control_header->bInCollection = 1;
549 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
550
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530551 UVC_COPY_DESCRIPTOR(mem, dst, uvc_control_ep);
552 if (speed == USB_SPEED_SUPER)
553 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
554
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200555 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
556 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
557
558 uvc_streaming_header = mem;
559 UVC_COPY_DESCRIPTORS(mem, dst,
560 (const struct usb_descriptor_header**)uvc_streaming_cls);
561 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530562 uvc_streaming_header->bEndpointAddress =
563 uvc_fs_streaming_ep.bEndpointAddress;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200564
565 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
566
567 *dst = NULL;
568 return hdr;
569}
570
571static void
572uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
573{
574 struct usb_composite_dev *cdev = c->cdev;
575 struct uvc_device *uvc = to_uvc(f);
576
577 INFO(cdev, "uvc_function_unbind\n");
578
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200579 video_unregister_device(uvc->vdev);
580 uvc->control_ep->driver_data = NULL;
581 uvc->video.ep->driver_data = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200582
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200583 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
584 kfree(uvc->control_buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200585
586 kfree(f->descriptors);
587 kfree(f->hs_descriptors);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530588 kfree(f->ss_descriptors);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200589
590 kfree(uvc);
591}
592
593static int __init
594uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
595{
596 struct usb_composite_dev *cdev = c->cdev;
597 struct uvc_device *uvc = to_uvc(f);
598 struct usb_ep *ep;
599 int ret = -EINVAL;
600
601 INFO(cdev, "uvc_function_bind\n");
602
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530603 /* sanity check the streaming endpoint module parameters */
604 if (streaming_interval < 1)
605 streaming_interval = 1;
606 if (streaming_interval > 16)
607 streaming_interval = 16;
608 if (streaming_mult > 2)
609 streaming_mult = 2;
610 if (streaming_maxburst > 15)
611 streaming_maxburst = 15;
612
613 /*
614 * fill in the FS video streaming specific descriptors from the
615 * module parameters
616 */
617 uvc_fs_streaming_ep.wMaxPacketSize = streaming_maxpacket > 1023 ?
618 1023 : streaming_maxpacket;
619 uvc_fs_streaming_ep.bInterval = streaming_interval;
620
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200621 /* Allocate endpoints. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530622 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200623 if (!ep) {
624 INFO(cdev, "Unable to allocate control EP\n");
625 goto error;
626 }
627 uvc->control_ep = ep;
628 ep->driver_data = uvc;
629
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530630 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200631 if (!ep) {
632 INFO(cdev, "Unable to allocate streaming EP\n");
633 goto error;
634 }
635 uvc->video.ep = ep;
636 ep->driver_data = uvc;
637
638 /* Allocate interface IDs. */
639 if ((ret = usb_interface_id(c, f)) < 0)
640 goto error;
641 uvc_iad.bFirstInterface = ret;
642 uvc_control_intf.bInterfaceNumber = ret;
643 uvc->control_intf = ret;
644
645 if ((ret = usb_interface_id(c, f)) < 0)
646 goto error;
647 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
648 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
649 uvc->streaming_intf = ret;
650
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530651 /* sanity check the streaming endpoint module parameters */
652 if (streaming_maxpacket > 1024)
653 streaming_maxpacket = 1024;
654
655 /* Copy descriptors for FS. */
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200656 f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530657
658 /* support high speed hardware */
659 if (gadget_is_dualspeed(cdev->gadget)) {
660 /*
661 * Fill in the HS descriptors from the module parameters for the
662 * Video Streaming endpoint.
663 * NOTE: We assume that the user knows what they are doing and
664 * won't give parameters that their UDC doesn't support.
665 */
666 uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket;
667 uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11;
668 uvc_hs_streaming_ep.bInterval = streaming_interval;
669 uvc_hs_streaming_ep.bEndpointAddress =
670 uvc_fs_streaming_ep.bEndpointAddress;
671
672 /* Copy descriptors. */
673 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
674 }
675
676 /* support super speed hardware */
677 if (gadget_is_superspeed(c->cdev->gadget)) {
678 /*
679 * Fill in the SS descriptors from the module parameters for the
680 * Video Streaming endpoint.
681 * NOTE: We assume that the user knows what they are doing and
682 * won't give parameters that their UDC doesn't support.
683 */
684 uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
685 uvc_ss_streaming_ep.bInterval = streaming_interval;
686 uvc_ss_streaming_comp.bmAttributes = streaming_mult;
687 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
688 uvc_ss_streaming_comp.wBytesPerInterval =
689 streaming_maxpacket * (streaming_mult + 1) *
690 (streaming_maxburst + 1);
691 uvc_ss_streaming_ep.bEndpointAddress =
692 uvc_fs_streaming_ep.bEndpointAddress;
693
694 /* Copy descriptors. */
695 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
696 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200697
698 /* Preallocate control endpoint request. */
699 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
700 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
701 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
702 ret = -ENOMEM;
703 goto error;
704 }
705
706 uvc->control_req->buf = uvc->control_buf;
707 uvc->control_req->complete = uvc_function_ep0_complete;
708 uvc->control_req->context = uvc;
709
710 /* Avoid letting this gadget enumerate until the userspace server is
711 * active.
712 */
713 if ((ret = usb_function_deactivate(f)) < 0)
714 goto error;
715
716 /* Initialise video. */
717 ret = uvc_video_init(&uvc->video);
718 if (ret < 0)
719 goto error;
720
721 /* Register a V4L2 device. */
722 ret = uvc_register_video(uvc);
723 if (ret < 0) {
724 printk(KERN_INFO "Unable to register video device\n");
725 goto error;
726 }
727
728 return 0;
729
730error:
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200731 if (uvc->vdev)
732 video_device_release(uvc->vdev);
733
734 if (uvc->control_ep)
735 uvc->control_ep->driver_data = NULL;
736 if (uvc->video.ep)
737 uvc->video.ep->driver_data = NULL;
738
739 if (uvc->control_req) {
740 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
741 kfree(uvc->control_buf);
742 }
743
744 kfree(f->descriptors);
745 kfree(f->hs_descriptors);
746 kfree(f->ss_descriptors);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200747 return ret;
748}
749
750/* --------------------------------------------------------------------------
751 * USB gadget function
752 */
753
754/**
755 * uvc_bind_config - add a UVC function to a configuration
756 * @c: the configuration to support the UVC instance
757 * Context: single threaded during gadget setup
758 *
759 * Returns zero on success, else negative errno.
760 *
761 * Caller must have called @uvc_setup(). Caller is also responsible for
762 * calling @uvc_cleanup() before module unload.
763 */
764int __init
765uvc_bind_config(struct usb_configuration *c,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530766 const struct uvc_descriptor_header * const *fs_control,
767 const struct uvc_descriptor_header * const *ss_control,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200768 const struct uvc_descriptor_header * const *fs_streaming,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530769 const struct uvc_descriptor_header * const *hs_streaming,
770 const struct uvc_descriptor_header * const *ss_streaming)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200771{
772 struct uvc_device *uvc;
773 int ret = 0;
774
775 /* TODO Check if the USB device controller supports the required
776 * features.
777 */
778 if (!gadget_is_dualspeed(c->cdev->gadget))
779 return -EINVAL;
780
781 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
782 if (uvc == NULL)
783 return -ENOMEM;
784
785 uvc->state = UVC_STATE_DISCONNECTED;
786
787 /* Validate the descriptors. */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530788 if (fs_control == NULL || fs_control[0] == NULL ||
789 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
790 goto error;
791
792 if (ss_control == NULL || ss_control[0] == NULL ||
793 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200794 goto error;
795
796 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530797 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200798 goto error;
799
800 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530801 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200802 goto error;
803
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530804 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
805 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
806 goto error;
807
808 uvc->desc.fs_control = fs_control;
809 uvc->desc.ss_control = ss_control;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200810 uvc->desc.fs_streaming = fs_streaming;
811 uvc->desc.hs_streaming = hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530812 uvc->desc.ss_streaming = ss_streaming;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200813
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530814 /* maybe allocate device-global string IDs, and patch descriptors */
815 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
816 /* Allocate string descriptor numbers. */
817 ret = usb_string_id(c->cdev);
818 if (ret < 0)
819 goto error;
820 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
821 uvc_iad.iFunction = ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200822
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530823 ret = usb_string_id(c->cdev);
824 if (ret < 0)
825 goto error;
826 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
827 uvc_control_intf.iInterface = ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200828
Bhupesh Sharma3de6e632012-06-01 15:08:54 +0530829 ret = usb_string_id(c->cdev);
830 if (ret < 0)
831 goto error;
832 uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
833 uvc_streaming_intf_alt0.iInterface = ret;
834 uvc_streaming_intf_alt1.iInterface = ret;
835 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200836
837 /* Register the function. */
838 uvc->func.name = "uvc";
839 uvc->func.strings = uvc_function_strings;
840 uvc->func.bind = uvc_function_bind;
841 uvc->func.unbind = uvc_function_unbind;
842 uvc->func.get_alt = uvc_function_get_alt;
843 uvc->func.set_alt = uvc_function_set_alt;
844 uvc->func.disable = uvc_function_disable;
845 uvc->func.setup = uvc_function_setup;
846
847 ret = usb_add_function(c, &uvc->func);
848 if (ret)
849 kfree(uvc);
850
Jassi Brar28f75f42011-06-25 00:17:26 +0530851 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200852
853error:
854 kfree(uvc);
855 return ret;
856}
857
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300858module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200859MODULE_PARM_DESC(trace, "Trace level bitmask");
860