]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/usb/gadget/function/f_uvc.c
ASoC: sirf: Added blank line after declarations
[thirdparty/linux.git] / drivers / usb / gadget / function / f_uvc.c
1 /*
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.
11 */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/fs.h>
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/string.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/video.h>
24 #include <linux/vmalloc.h>
25 #include <linux/wait.h>
26
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-event.h>
29
30 #include "u_uvc.h"
31 #include "uvc.h"
32 #include "uvc_configfs.h"
33 #include "uvc_v4l2.h"
34 #include "uvc_video.h"
35
36 unsigned int uvc_gadget_trace_param;
37
38 /* --------------------------------------------------------------------------
39 * Function descriptors
40 */
41
42 /* string IDs are assigned dynamically */
43
44 #define UVC_STRING_CONTROL_IDX 0
45 #define UVC_STRING_STREAMING_IDX 1
46
47 static struct usb_string uvc_en_us_strings[] = {
48 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
49 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
50 { }
51 };
52
53 static struct usb_gadget_strings uvc_stringtab = {
54 .language = 0x0409, /* en-us */
55 .strings = uvc_en_us_strings,
56 };
57
58 static 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
66 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
67
68 static struct usb_interface_assoc_descriptor uvc_iad = {
69 .bLength = sizeof(uvc_iad),
70 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
71 .bFirstInterface = 0,
72 .bInterfaceCount = 2,
73 .bFunctionClass = USB_CLASS_VIDEO,
74 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
75 .bFunctionProtocol = 0x00,
76 .iFunction = 0,
77 };
78
79 static struct usb_interface_descriptor uvc_control_intf = {
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,
86 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
87 .bInterfaceProtocol = 0x00,
88 .iInterface = 0,
89 };
90
91 static struct usb_endpoint_descriptor uvc_control_ep = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_INT,
96 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
97 .bInterval = 8,
98 };
99
100 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
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
109 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
110 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
111 .bDescriptorType = USB_DT_CS_ENDPOINT,
112 .bDescriptorSubType = UVC_EP_INTERRUPT,
113 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
114 };
115
116 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
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,
123 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
124 .bInterfaceProtocol = 0x00,
125 .iInterface = 0,
126 };
127
128 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
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,
135 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
136 .bInterfaceProtocol = 0x00,
137 .iInterface = 0,
138 };
139
140 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = USB_DIR_IN,
144 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
145 | USB_ENDPOINT_XFER_ISOC,
146 /* The wMaxPacketSize and bInterval values will be initialized from
147 * module parameters.
148 */
149 };
150
151 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
155 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
156 | USB_ENDPOINT_XFER_ISOC,
157 /* The wMaxPacketSize and bInterval values will be initialized from
158 * module parameters.
159 */
160 };
161
162 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
165
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
168 | USB_ENDPOINT_XFER_ISOC,
169 /* The wMaxPacketSize and bInterval values will be initialized from
170 * module parameters.
171 */
172 };
173
174 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
175 .bLength = sizeof(uvc_ss_streaming_comp),
176 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
177 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
178 * initialized from module parameters.
179 */
180 };
181
182 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
183 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
184 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
185 NULL,
186 };
187
188 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
189 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
190 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
191 NULL,
192 };
193
194 static 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,
198 NULL,
199 };
200
201 void uvc_set_trace_param(unsigned int trace)
202 {
203 uvc_gadget_trace_param = trace;
204 }
205 EXPORT_SYMBOL(uvc_set_trace_param);
206
207 /* --------------------------------------------------------------------------
208 * Control requests
209 */
210
211 static void
212 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
213 {
214 struct uvc_device *uvc = req->context;
215 struct v4l2_event v4l2_event;
216 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
217
218 if (uvc->event_setup_out) {
219 uvc->event_setup_out = 0;
220
221 memset(&v4l2_event, 0, sizeof(v4l2_event));
222 v4l2_event.type = UVC_EVENT_DATA;
223 uvc_event->data.length = req->actual;
224 memcpy(&uvc_event->data.data, req->buf, req->actual);
225 v4l2_event_queue(&uvc->vdev, &v4l2_event);
226 }
227 }
228
229 static int
230 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
231 {
232 struct uvc_device *uvc = to_uvc(f);
233 struct v4l2_event v4l2_event;
234 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
235
236 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
237 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
238 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
239 */
240
241 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
242 INFO(f->config->cdev, "invalid request type\n");
243 return -EINVAL;
244 }
245
246 /* Stall too big requests. */
247 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
248 return -EINVAL;
249
250 /* Tell the complete callback to generate an event for the next request
251 * that will be enqueued by UVCIOC_SEND_RESPONSE.
252 */
253 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
254 uvc->event_length = le16_to_cpu(ctrl->wLength);
255
256 memset(&v4l2_event, 0, sizeof(v4l2_event));
257 v4l2_event.type = UVC_EVENT_SETUP;
258 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
259 v4l2_event_queue(&uvc->vdev, &v4l2_event);
260
261 /* Pass additional setup data to userspace */
262 if (uvc->event_setup_out && uvc->event_length) {
263 uvc->control_req->length = uvc->event_length;
264 return usb_ep_queue(uvc->func.config->cdev->gadget->ep0,
265 uvc->control_req, GFP_ATOMIC);
266 }
267
268 return 0;
269 }
270
271 void uvc_function_setup_continue(struct uvc_device *uvc)
272 {
273 struct usb_composite_dev *cdev = uvc->func.config->cdev;
274
275 usb_composite_setup_continue(cdev);
276 }
277
278 static int
279 uvc_function_get_alt(struct usb_function *f, unsigned interface)
280 {
281 struct uvc_device *uvc = to_uvc(f);
282
283 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
284
285 if (interface == uvc->control_intf)
286 return 0;
287 else if (interface != uvc->streaming_intf)
288 return -EINVAL;
289 else
290 return uvc->video.ep->enabled ? 1 : 0;
291 }
292
293 static int
294 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
295 {
296 struct uvc_device *uvc = to_uvc(f);
297 struct usb_composite_dev *cdev = f->config->cdev;
298 struct v4l2_event v4l2_event;
299 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
300 int ret;
301
302 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
303
304 if (interface == uvc->control_intf) {
305 if (alt)
306 return -EINVAL;
307
308 INFO(cdev, "reset UVC Control\n");
309 usb_ep_disable(uvc->control_ep);
310
311 if (!uvc->control_ep->desc)
312 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
313 return -EINVAL;
314
315 usb_ep_enable(uvc->control_ep);
316
317 if (uvc->state == UVC_STATE_DISCONNECTED) {
318 memset(&v4l2_event, 0, sizeof(v4l2_event));
319 v4l2_event.type = UVC_EVENT_CONNECT;
320 uvc_event->speed = cdev->gadget->speed;
321 v4l2_event_queue(&uvc->vdev, &v4l2_event);
322
323 uvc->state = UVC_STATE_CONNECTED;
324 }
325
326 return 0;
327 }
328
329 if (interface != uvc->streaming_intf)
330 return -EINVAL;
331
332 /* TODO
333 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
334 return alt ? -EINVAL : 0;
335 */
336
337 switch (alt) {
338 case 0:
339 if (uvc->state != UVC_STATE_STREAMING)
340 return 0;
341
342 if (uvc->video.ep)
343 usb_ep_disable(uvc->video.ep);
344
345 memset(&v4l2_event, 0, sizeof(v4l2_event));
346 v4l2_event.type = UVC_EVENT_STREAMOFF;
347 v4l2_event_queue(&uvc->vdev, &v4l2_event);
348
349 uvc->state = UVC_STATE_CONNECTED;
350 return 0;
351
352 case 1:
353 if (uvc->state != UVC_STATE_CONNECTED)
354 return 0;
355
356 if (!uvc->video.ep)
357 return -EINVAL;
358
359 INFO(cdev, "reset UVC\n");
360 usb_ep_disable(uvc->video.ep);
361
362 ret = config_ep_by_speed(f->config->cdev->gadget,
363 &(uvc->func), uvc->video.ep);
364 if (ret)
365 return ret;
366 usb_ep_enable(uvc->video.ep);
367
368 memset(&v4l2_event, 0, sizeof(v4l2_event));
369 v4l2_event.type = UVC_EVENT_STREAMON;
370 v4l2_event_queue(&uvc->vdev, &v4l2_event);
371 return USB_GADGET_DELAYED_STATUS;
372
373 default:
374 return -EINVAL;
375 }
376 }
377
378 static void
379 uvc_function_disable(struct usb_function *f)
380 {
381 struct uvc_device *uvc = to_uvc(f);
382 struct v4l2_event v4l2_event;
383
384 INFO(f->config->cdev, "uvc_function_disable\n");
385
386 memset(&v4l2_event, 0, sizeof(v4l2_event));
387 v4l2_event.type = UVC_EVENT_DISCONNECT;
388 v4l2_event_queue(&uvc->vdev, &v4l2_event);
389
390 uvc->state = UVC_STATE_DISCONNECTED;
391
392 usb_ep_disable(uvc->video.ep);
393 usb_ep_disable(uvc->control_ep);
394 }
395
396 /* --------------------------------------------------------------------------
397 * Connection / disconnection
398 */
399
400 void
401 uvc_function_connect(struct uvc_device *uvc)
402 {
403 struct usb_composite_dev *cdev = uvc->func.config->cdev;
404 int ret;
405
406 if ((ret = usb_function_activate(&uvc->func)) < 0)
407 INFO(cdev, "UVC connect failed with %d\n", ret);
408 }
409
410 void
411 uvc_function_disconnect(struct uvc_device *uvc)
412 {
413 struct usb_composite_dev *cdev = uvc->func.config->cdev;
414 int ret;
415
416 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
417 INFO(cdev, "UVC disconnect failed with %d\n", ret);
418 }
419
420 /* --------------------------------------------------------------------------
421 * USB probe and disconnect
422 */
423
424 static int
425 uvc_register_video(struct uvc_device *uvc)
426 {
427 struct usb_composite_dev *cdev = uvc->func.config->cdev;
428
429 /* TODO reference counting. */
430 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
431 uvc->vdev.fops = &uvc_v4l2_fops;
432 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
433 uvc->vdev.release = video_device_release_empty;
434 uvc->vdev.vfl_dir = VFL_DIR_TX;
435 uvc->vdev.lock = &uvc->video.mutex;
436 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
437
438 video_set_drvdata(&uvc->vdev, uvc);
439
440 return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
441 }
442
443 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
444 do { \
445 memcpy(mem, desc, (desc)->bLength); \
446 *(dst)++ = mem; \
447 mem += (desc)->bLength; \
448 } while (0);
449
450 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
451 do { \
452 const struct usb_descriptor_header * const *__src; \
453 for (__src = src; *__src; ++__src) { \
454 memcpy(mem, *__src, (*__src)->bLength); \
455 *dst++ = mem; \
456 mem += (*__src)->bLength; \
457 } \
458 } while (0)
459
460 static struct usb_descriptor_header **
461 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
462 {
463 struct uvc_input_header_descriptor *uvc_streaming_header;
464 struct uvc_header_descriptor *uvc_control_header;
465 const struct uvc_descriptor_header * const *uvc_control_desc;
466 const struct uvc_descriptor_header * const *uvc_streaming_cls;
467 const struct usb_descriptor_header * const *uvc_streaming_std;
468 const struct usb_descriptor_header * const *src;
469 struct usb_descriptor_header **dst;
470 struct usb_descriptor_header **hdr;
471 unsigned int control_size;
472 unsigned int streaming_size;
473 unsigned int n_desc;
474 unsigned int bytes;
475 void *mem;
476
477 switch (speed) {
478 case USB_SPEED_SUPER:
479 uvc_control_desc = uvc->desc.ss_control;
480 uvc_streaming_cls = uvc->desc.ss_streaming;
481 uvc_streaming_std = uvc_ss_streaming;
482 break;
483
484 case USB_SPEED_HIGH:
485 uvc_control_desc = uvc->desc.fs_control;
486 uvc_streaming_cls = uvc->desc.hs_streaming;
487 uvc_streaming_std = uvc_hs_streaming;
488 break;
489
490 case USB_SPEED_FULL:
491 default:
492 uvc_control_desc = uvc->desc.fs_control;
493 uvc_streaming_cls = uvc->desc.fs_streaming;
494 uvc_streaming_std = uvc_fs_streaming;
495 break;
496 }
497
498 if (!uvc_control_desc || !uvc_streaming_cls)
499 return ERR_PTR(-ENODEV);
500
501 /* Descriptors layout
502 *
503 * uvc_iad
504 * uvc_control_intf
505 * Class-specific UVC control descriptors
506 * uvc_control_ep
507 * uvc_control_cs_ep
508 * uvc_ss_control_comp (for SS only)
509 * uvc_streaming_intf_alt0
510 * Class-specific UVC streaming descriptors
511 * uvc_{fs|hs}_streaming
512 */
513
514 /* Count descriptors and compute their size. */
515 control_size = 0;
516 streaming_size = 0;
517 bytes = uvc_iad.bLength + uvc_control_intf.bLength
518 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
519 + uvc_streaming_intf_alt0.bLength;
520
521 if (speed == USB_SPEED_SUPER) {
522 bytes += uvc_ss_control_comp.bLength;
523 n_desc = 6;
524 } else {
525 n_desc = 5;
526 }
527
528 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
529 *src; ++src) {
530 control_size += (*src)->bLength;
531 bytes += (*src)->bLength;
532 n_desc++;
533 }
534 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
535 *src; ++src) {
536 streaming_size += (*src)->bLength;
537 bytes += (*src)->bLength;
538 n_desc++;
539 }
540 for (src = uvc_streaming_std; *src; ++src) {
541 bytes += (*src)->bLength;
542 n_desc++;
543 }
544
545 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
546 if (mem == NULL)
547 return NULL;
548
549 hdr = mem;
550 dst = mem;
551 mem += (n_desc + 1) * sizeof(*src);
552
553 /* Copy the descriptors. */
554 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
555 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
556
557 uvc_control_header = mem;
558 UVC_COPY_DESCRIPTORS(mem, dst,
559 (const struct usb_descriptor_header **)uvc_control_desc);
560 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
561 uvc_control_header->bInCollection = 1;
562 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
563
564 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
565 if (speed == USB_SPEED_SUPER)
566 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
567
568 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
569 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
570
571 uvc_streaming_header = mem;
572 UVC_COPY_DESCRIPTORS(mem, dst,
573 (const struct usb_descriptor_header**)uvc_streaming_cls);
574 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
575 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
576
577 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
578
579 *dst = NULL;
580 return hdr;
581 }
582
583 static int
584 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
585 {
586 struct usb_composite_dev *cdev = c->cdev;
587 struct uvc_device *uvc = to_uvc(f);
588 struct usb_string *us;
589 unsigned int max_packet_mult;
590 unsigned int max_packet_size;
591 struct usb_ep *ep;
592 struct f_uvc_opts *opts;
593 int ret = -EINVAL;
594
595 INFO(cdev, "uvc_function_bind\n");
596
597 opts = fi_to_f_uvc_opts(f->fi);
598 /* Sanity check the streaming endpoint module parameters.
599 */
600 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
601 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
602 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
603
604 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
605 * module parameters.
606 *
607 * NOTE: We assume that the user knows what they are doing and won't
608 * give parameters that their UDC doesn't support.
609 */
610 if (opts->streaming_maxpacket <= 1024) {
611 max_packet_mult = 1;
612 max_packet_size = opts->streaming_maxpacket;
613 } else if (opts->streaming_maxpacket <= 2048) {
614 max_packet_mult = 2;
615 max_packet_size = opts->streaming_maxpacket / 2;
616 } else {
617 max_packet_mult = 3;
618 max_packet_size = opts->streaming_maxpacket / 3;
619 }
620
621 uvc_fs_streaming_ep.wMaxPacketSize =
622 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
623 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
624
625 uvc_hs_streaming_ep.wMaxPacketSize =
626 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
627 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
628
629 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
630 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
631 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
632 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
633 uvc_ss_streaming_comp.wBytesPerInterval =
634 cpu_to_le16(max_packet_size * max_packet_mult *
635 opts->streaming_maxburst);
636
637 /* Allocate endpoints. */
638 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
639 if (!ep) {
640 INFO(cdev, "Unable to allocate control EP\n");
641 goto error;
642 }
643 uvc->control_ep = ep;
644
645 if (gadget_is_superspeed(c->cdev->gadget))
646 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
647 &uvc_ss_streaming_comp);
648 else if (gadget_is_dualspeed(cdev->gadget))
649 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
650 else
651 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
652
653 if (!ep) {
654 INFO(cdev, "Unable to allocate streaming EP\n");
655 goto error;
656 }
657 uvc->video.ep = ep;
658
659 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
661 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
662
663 us = usb_gstrings_attach(cdev, uvc_function_strings,
664 ARRAY_SIZE(uvc_en_us_strings));
665 if (IS_ERR(us)) {
666 ret = PTR_ERR(us);
667 goto error;
668 }
669 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
670 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
671 ret = us[UVC_STRING_STREAMING_IDX].id;
672 uvc_streaming_intf_alt0.iInterface = ret;
673 uvc_streaming_intf_alt1.iInterface = ret;
674
675 /* Allocate interface IDs. */
676 if ((ret = usb_interface_id(c, f)) < 0)
677 goto error;
678 uvc_iad.bFirstInterface = ret;
679 uvc_control_intf.bInterfaceNumber = ret;
680 uvc->control_intf = ret;
681
682 if ((ret = usb_interface_id(c, f)) < 0)
683 goto error;
684 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
685 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
686 uvc->streaming_intf = ret;
687
688 /* Copy descriptors */
689 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
690 if (IS_ERR(f->fs_descriptors)) {
691 ret = PTR_ERR(f->fs_descriptors);
692 f->fs_descriptors = NULL;
693 goto error;
694 }
695 if (gadget_is_dualspeed(cdev->gadget)) {
696 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
697 if (IS_ERR(f->hs_descriptors)) {
698 ret = PTR_ERR(f->hs_descriptors);
699 f->hs_descriptors = NULL;
700 goto error;
701 }
702 }
703 if (gadget_is_superspeed(c->cdev->gadget)) {
704 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
705 if (IS_ERR(f->ss_descriptors)) {
706 ret = PTR_ERR(f->ss_descriptors);
707 f->ss_descriptors = NULL;
708 goto error;
709 }
710 }
711
712 /* Preallocate control endpoint request. */
713 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
714 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
715 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
716 ret = -ENOMEM;
717 goto error;
718 }
719
720 uvc->control_req->buf = uvc->control_buf;
721 uvc->control_req->complete = uvc_function_ep0_complete;
722 uvc->control_req->context = uvc;
723
724 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
725 printk(KERN_INFO "v4l2_device_register failed\n");
726 goto error;
727 }
728
729 /* Initialise video. */
730 ret = uvcg_video_init(&uvc->video);
731 if (ret < 0)
732 goto error;
733
734 /* Register a V4L2 device. */
735 ret = uvc_register_video(uvc);
736 if (ret < 0) {
737 printk(KERN_INFO "Unable to register video device\n");
738 goto error;
739 }
740
741 return 0;
742
743 error:
744 v4l2_device_unregister(&uvc->v4l2_dev);
745
746 if (uvc->control_req)
747 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
748 kfree(uvc->control_buf);
749
750 usb_free_all_descriptors(f);
751 return ret;
752 }
753
754 /* --------------------------------------------------------------------------
755 * USB gadget function
756 */
757
758 static void uvc_free_inst(struct usb_function_instance *f)
759 {
760 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
761
762 mutex_destroy(&opts->lock);
763 kfree(opts);
764 }
765
766 static struct usb_function_instance *uvc_alloc_inst(void)
767 {
768 struct f_uvc_opts *opts;
769 struct uvc_camera_terminal_descriptor *cd;
770 struct uvc_processing_unit_descriptor *pd;
771 struct uvc_output_terminal_descriptor *od;
772 struct uvc_color_matching_descriptor *md;
773 struct uvc_descriptor_header **ctl_cls;
774
775 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
776 if (!opts)
777 return ERR_PTR(-ENOMEM);
778 opts->func_inst.free_func_inst = uvc_free_inst;
779 mutex_init(&opts->lock);
780
781 cd = &opts->uvc_camera_terminal;
782 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
783 cd->bDescriptorType = USB_DT_CS_INTERFACE;
784 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
785 cd->bTerminalID = 1;
786 cd->wTerminalType = cpu_to_le16(0x0201);
787 cd->bAssocTerminal = 0;
788 cd->iTerminal = 0;
789 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
790 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
791 cd->wOcularFocalLength = cpu_to_le16(0);
792 cd->bControlSize = 3;
793 cd->bmControls[0] = 2;
794 cd->bmControls[1] = 0;
795 cd->bmControls[2] = 0;
796
797 pd = &opts->uvc_processing;
798 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
799 pd->bDescriptorType = USB_DT_CS_INTERFACE;
800 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
801 pd->bUnitID = 2;
802 pd->bSourceID = 1;
803 pd->wMaxMultiplier = cpu_to_le16(16*1024);
804 pd->bControlSize = 2;
805 pd->bmControls[0] = 1;
806 pd->bmControls[1] = 0;
807 pd->iProcessing = 0;
808
809 od = &opts->uvc_output_terminal;
810 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
811 od->bDescriptorType = USB_DT_CS_INTERFACE;
812 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
813 od->bTerminalID = 3;
814 od->wTerminalType = cpu_to_le16(0x0101);
815 od->bAssocTerminal = 0;
816 od->bSourceID = 2;
817 od->iTerminal = 0;
818
819 md = &opts->uvc_color_matching;
820 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
821 md->bDescriptorType = USB_DT_CS_INTERFACE;
822 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
823 md->bColorPrimaries = 1;
824 md->bTransferCharacteristics = 1;
825 md->bMatrixCoefficients = 4;
826
827 /* Prepare fs control class descriptors for configfs-based gadgets */
828 ctl_cls = opts->uvc_fs_control_cls;
829 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
830 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
831 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
832 ctl_cls[3] = (struct uvc_descriptor_header *)od;
833 ctl_cls[4] = NULL; /* NULL-terminate */
834 opts->fs_control =
835 (const struct uvc_descriptor_header * const *)ctl_cls;
836
837 /* Prepare hs control class descriptors for configfs-based gadgets */
838 ctl_cls = opts->uvc_ss_control_cls;
839 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
840 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
841 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
842 ctl_cls[3] = (struct uvc_descriptor_header *)od;
843 ctl_cls[4] = NULL; /* NULL-terminate */
844 opts->ss_control =
845 (const struct uvc_descriptor_header * const *)ctl_cls;
846
847 opts->streaming_interval = 1;
848 opts->streaming_maxpacket = 1024;
849
850 uvcg_attach_configfs(opts);
851 return &opts->func_inst;
852 }
853
854 static void uvc_free(struct usb_function *f)
855 {
856 struct uvc_device *uvc = to_uvc(f);
857 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
858 func_inst);
859 --opts->refcnt;
860 kfree(uvc);
861 }
862
863 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
864 {
865 struct usb_composite_dev *cdev = c->cdev;
866 struct uvc_device *uvc = to_uvc(f);
867
868 INFO(cdev, "%s\n", __func__);
869
870 video_unregister_device(&uvc->vdev);
871 v4l2_device_unregister(&uvc->v4l2_dev);
872
873 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
874 kfree(uvc->control_buf);
875
876 usb_free_all_descriptors(f);
877 }
878
879 static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
880 {
881 struct uvc_device *uvc;
882 struct f_uvc_opts *opts;
883 struct uvc_descriptor_header **strm_cls;
884
885 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
886 if (uvc == NULL)
887 return ERR_PTR(-ENOMEM);
888
889 mutex_init(&uvc->video.mutex);
890 uvc->state = UVC_STATE_DISCONNECTED;
891 opts = fi_to_f_uvc_opts(fi);
892
893 mutex_lock(&opts->lock);
894 if (opts->uvc_fs_streaming_cls) {
895 strm_cls = opts->uvc_fs_streaming_cls;
896 opts->fs_streaming =
897 (const struct uvc_descriptor_header * const *)strm_cls;
898 }
899 if (opts->uvc_hs_streaming_cls) {
900 strm_cls = opts->uvc_hs_streaming_cls;
901 opts->hs_streaming =
902 (const struct uvc_descriptor_header * const *)strm_cls;
903 }
904 if (opts->uvc_ss_streaming_cls) {
905 strm_cls = opts->uvc_ss_streaming_cls;
906 opts->ss_streaming =
907 (const struct uvc_descriptor_header * const *)strm_cls;
908 }
909
910 uvc->desc.fs_control = opts->fs_control;
911 uvc->desc.ss_control = opts->ss_control;
912 uvc->desc.fs_streaming = opts->fs_streaming;
913 uvc->desc.hs_streaming = opts->hs_streaming;
914 uvc->desc.ss_streaming = opts->ss_streaming;
915 ++opts->refcnt;
916 mutex_unlock(&opts->lock);
917
918 /* Register the function. */
919 uvc->func.name = "uvc";
920 uvc->func.bind = uvc_function_bind;
921 uvc->func.unbind = uvc_unbind;
922 uvc->func.get_alt = uvc_function_get_alt;
923 uvc->func.set_alt = uvc_function_set_alt;
924 uvc->func.disable = uvc_function_disable;
925 uvc->func.setup = uvc_function_setup;
926 uvc->func.free_func = uvc_free;
927 uvc->func.bind_deactivated = true;
928
929 return &uvc->func;
930 }
931
932 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
933 MODULE_LICENSE("GPL");
934 MODULE_AUTHOR("Laurent Pinchart");