Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / usb / gadget / f_uvc.c
blob2022fe492148b04a3fd8c1c4a6c9c1ab3a4ee942
1 /*
2 * uvc_gadget.c -- USB Video Class Gadget driver
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
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>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-event.h>
28 #include "uvc.h"
30 unsigned int uvc_gadget_trace_param;
32 /* --------------------------------------------------------------------------
33 * Function descriptors
36 /* string IDs are assigned dynamically */
38 #define UVC_STRING_ASSOCIATION_IDX 0
39 #define UVC_STRING_CONTROL_IDX 1
40 #define UVC_STRING_STREAMING_IDX 2
42 static struct usb_string uvc_en_us_strings[] = {
43 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
44 [UVC_STRING_CONTROL_IDX].s = "Video Control",
45 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
46 { }
49 static struct usb_gadget_strings uvc_stringtab = {
50 .language = 0x0409, /* en-us */
51 .strings = uvc_en_us_strings,
54 static struct usb_gadget_strings *uvc_function_strings[] = {
55 &uvc_stringtab,
56 NULL,
59 #define UVC_INTF_VIDEO_CONTROL 0
60 #define UVC_INTF_VIDEO_STREAMING 1
62 static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
63 .bLength = sizeof(uvc_iad),
64 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
65 .bFirstInterface = 0,
66 .bInterfaceCount = 2,
67 .bFunctionClass = USB_CLASS_VIDEO,
68 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
69 .bFunctionProtocol = 0x00,
70 .iFunction = 0,
73 static struct usb_interface_descriptor uvc_control_intf __initdata = {
74 .bLength = USB_DT_INTERFACE_SIZE,
75 .bDescriptorType = USB_DT_INTERFACE,
76 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
77 .bAlternateSetting = 0,
78 .bNumEndpoints = 1,
79 .bInterfaceClass = USB_CLASS_VIDEO,
80 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
81 .bInterfaceProtocol = 0x00,
82 .iInterface = 0,
85 static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
88 .bEndpointAddress = USB_DIR_IN,
89 .bmAttributes = USB_ENDPOINT_XFER_INT,
90 .wMaxPacketSize = cpu_to_le16(16),
91 .bInterval = 8,
94 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
95 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
96 .bDescriptorType = USB_DT_CS_ENDPOINT,
97 .bDescriptorSubType = UVC_EP_INTERRUPT,
98 .wMaxTransferSize = cpu_to_le16(16),
101 static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
102 .bLength = USB_DT_INTERFACE_SIZE,
103 .bDescriptorType = USB_DT_INTERFACE,
104 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
105 .bAlternateSetting = 0,
106 .bNumEndpoints = 0,
107 .bInterfaceClass = USB_CLASS_VIDEO,
108 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
109 .bInterfaceProtocol = 0x00,
110 .iInterface = 0,
113 static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
114 .bLength = USB_DT_INTERFACE_SIZE,
115 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
117 .bAlternateSetting = 1,
118 .bNumEndpoints = 1,
119 .bInterfaceClass = USB_CLASS_VIDEO,
120 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
121 .bInterfaceProtocol = 0x00,
122 .iInterface = 0,
125 static struct usb_endpoint_descriptor uvc_streaming_ep = {
126 .bLength = USB_DT_ENDPOINT_SIZE,
127 .bDescriptorType = USB_DT_ENDPOINT,
128 .bEndpointAddress = USB_DIR_IN,
129 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
130 .wMaxPacketSize = cpu_to_le16(512),
131 .bInterval = 1,
134 static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
135 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
136 (struct usb_descriptor_header *) &uvc_streaming_ep,
137 NULL,
140 static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
141 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
142 (struct usb_descriptor_header *) &uvc_streaming_ep,
143 NULL,
146 /* --------------------------------------------------------------------------
147 * Control requests
150 static void
151 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
153 struct uvc_device *uvc = req->context;
154 struct v4l2_event v4l2_event;
155 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
157 if (uvc->event_setup_out) {
158 uvc->event_setup_out = 0;
160 memset(&v4l2_event, 0, sizeof(v4l2_event));
161 v4l2_event.type = UVC_EVENT_DATA;
162 uvc_event->data.length = req->actual;
163 memcpy(&uvc_event->data.data, req->buf, req->actual);
164 v4l2_event_queue(uvc->vdev, &v4l2_event);
168 static int
169 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
171 struct uvc_device *uvc = to_uvc(f);
172 struct v4l2_event v4l2_event;
173 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
175 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
176 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
177 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
180 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
181 INFO(f->config->cdev, "invalid request type\n");
182 return -EINVAL;
185 /* Stall too big requests. */
186 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
187 return -EINVAL;
189 memset(&v4l2_event, 0, sizeof(v4l2_event));
190 v4l2_event.type = UVC_EVENT_SETUP;
191 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
192 v4l2_event_queue(uvc->vdev, &v4l2_event);
194 return 0;
197 static int
198 uvc_function_get_alt(struct usb_function *f, unsigned interface)
200 struct uvc_device *uvc = to_uvc(f);
202 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
204 if (interface == uvc->control_intf)
205 return 0;
206 else if (interface != uvc->streaming_intf)
207 return -EINVAL;
208 else
209 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
212 static int
213 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
215 struct uvc_device *uvc = to_uvc(f);
216 struct v4l2_event v4l2_event;
217 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
219 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
221 if (interface == uvc->control_intf) {
222 if (alt)
223 return -EINVAL;
225 if (uvc->state == UVC_STATE_DISCONNECTED) {
226 memset(&v4l2_event, 0, sizeof(v4l2_event));
227 v4l2_event.type = UVC_EVENT_CONNECT;
228 uvc_event->speed = f->config->cdev->gadget->speed;
229 v4l2_event_queue(uvc->vdev, &v4l2_event);
231 uvc->state = UVC_STATE_CONNECTED;
234 return 0;
237 if (interface != uvc->streaming_intf)
238 return -EINVAL;
240 /* TODO
241 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
242 return alt ? -EINVAL : 0;
245 switch (alt) {
246 case 0:
247 if (uvc->state != UVC_STATE_STREAMING)
248 return 0;
250 if (uvc->video.ep)
251 usb_ep_disable(uvc->video.ep);
253 memset(&v4l2_event, 0, sizeof(v4l2_event));
254 v4l2_event.type = UVC_EVENT_STREAMOFF;
255 v4l2_event_queue(uvc->vdev, &v4l2_event);
257 uvc->state = UVC_STATE_CONNECTED;
258 break;
260 case 1:
261 if (uvc->state != UVC_STATE_CONNECTED)
262 return 0;
264 if (uvc->video.ep) {
265 uvc->video.ep->desc = &uvc_streaming_ep;
266 usb_ep_enable(uvc->video.ep);
269 memset(&v4l2_event, 0, sizeof(v4l2_event));
270 v4l2_event.type = UVC_EVENT_STREAMON;
271 v4l2_event_queue(uvc->vdev, &v4l2_event);
273 uvc->state = UVC_STATE_STREAMING;
274 break;
276 default:
277 return -EINVAL;
280 return 0;
283 static void
284 uvc_function_disable(struct usb_function *f)
286 struct uvc_device *uvc = to_uvc(f);
287 struct v4l2_event v4l2_event;
289 INFO(f->config->cdev, "uvc_function_disable\n");
291 memset(&v4l2_event, 0, sizeof(v4l2_event));
292 v4l2_event.type = UVC_EVENT_DISCONNECT;
293 v4l2_event_queue(uvc->vdev, &v4l2_event);
295 uvc->state = UVC_STATE_DISCONNECTED;
298 /* --------------------------------------------------------------------------
299 * Connection / disconnection
302 void
303 uvc_function_connect(struct uvc_device *uvc)
305 struct usb_composite_dev *cdev = uvc->func.config->cdev;
306 int ret;
308 if ((ret = usb_function_activate(&uvc->func)) < 0)
309 INFO(cdev, "UVC connect failed with %d\n", ret);
312 void
313 uvc_function_disconnect(struct uvc_device *uvc)
315 struct usb_composite_dev *cdev = uvc->func.config->cdev;
316 int ret;
318 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
319 INFO(cdev, "UVC disconnect failed with %d\n", ret);
322 /* --------------------------------------------------------------------------
323 * USB probe and disconnect
326 static int
327 uvc_register_video(struct uvc_device *uvc)
329 struct usb_composite_dev *cdev = uvc->func.config->cdev;
330 struct video_device *video;
332 /* TODO reference counting. */
333 video = video_device_alloc();
334 if (video == NULL)
335 return -ENOMEM;
337 video->parent = &cdev->gadget->dev;
338 video->minor = -1;
339 video->fops = &uvc_v4l2_fops;
340 video->release = video_device_release;
341 strncpy(video->name, cdev->gadget->name, sizeof(video->name));
343 uvc->vdev = video;
344 video_set_drvdata(video, uvc);
346 return video_register_device(video, VFL_TYPE_GRABBER, -1);
349 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
350 do { \
351 memcpy(mem, desc, (desc)->bLength); \
352 *(dst)++ = mem; \
353 mem += (desc)->bLength; \
354 } while (0);
356 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
357 do { \
358 const struct usb_descriptor_header * const *__src; \
359 for (__src = src; *__src; ++__src) { \
360 memcpy(mem, *__src, (*__src)->bLength); \
361 *dst++ = mem; \
362 mem += (*__src)->bLength; \
364 } while (0)
366 static struct usb_descriptor_header ** __init
367 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
369 struct uvc_input_header_descriptor *uvc_streaming_header;
370 struct uvc_header_descriptor *uvc_control_header;
371 const struct uvc_descriptor_header * const *uvc_streaming_cls;
372 const struct usb_descriptor_header * const *uvc_streaming_std;
373 const struct usb_descriptor_header * const *src;
374 struct usb_descriptor_header **dst;
375 struct usb_descriptor_header **hdr;
376 unsigned int control_size;
377 unsigned int streaming_size;
378 unsigned int n_desc;
379 unsigned int bytes;
380 void *mem;
382 uvc_streaming_cls = (speed == USB_SPEED_FULL)
383 ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
384 uvc_streaming_std = (speed == USB_SPEED_FULL)
385 ? uvc_fs_streaming : uvc_hs_streaming;
387 /* Descriptors layout
389 * uvc_iad
390 * uvc_control_intf
391 * Class-specific UVC control descriptors
392 * uvc_control_ep
393 * uvc_control_cs_ep
394 * uvc_streaming_intf_alt0
395 * Class-specific UVC streaming descriptors
396 * uvc_{fs|hs}_streaming
399 /* Count descriptors and compute their size. */
400 control_size = 0;
401 streaming_size = 0;
402 bytes = uvc_iad.bLength + uvc_control_intf.bLength
403 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
404 + uvc_streaming_intf_alt0.bLength;
405 n_desc = 5;
407 for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
408 control_size += (*src)->bLength;
409 bytes += (*src)->bLength;
410 n_desc++;
412 for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
413 streaming_size += (*src)->bLength;
414 bytes += (*src)->bLength;
415 n_desc++;
417 for (src = uvc_streaming_std; *src; ++src) {
418 bytes += (*src)->bLength;
419 n_desc++;
422 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
423 if (mem == NULL)
424 return NULL;
426 hdr = mem;
427 dst = mem;
428 mem += (n_desc + 1) * sizeof(*src);
430 /* Copy the descriptors. */
431 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
432 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
434 uvc_control_header = mem;
435 UVC_COPY_DESCRIPTORS(mem, dst,
436 (const struct usb_descriptor_header**)uvc->desc.control);
437 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
438 uvc_control_header->bInCollection = 1;
439 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
441 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
442 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
443 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
445 uvc_streaming_header = mem;
446 UVC_COPY_DESCRIPTORS(mem, dst,
447 (const struct usb_descriptor_header**)uvc_streaming_cls);
448 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
449 uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
451 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
453 *dst = NULL;
454 return hdr;
457 static void
458 uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
460 struct usb_composite_dev *cdev = c->cdev;
461 struct uvc_device *uvc = to_uvc(f);
463 INFO(cdev, "uvc_function_unbind\n");
465 if (uvc->vdev) {
466 if (uvc->vdev->minor == -1)
467 video_device_release(uvc->vdev);
468 else
469 video_unregister_device(uvc->vdev);
470 uvc->vdev = NULL;
473 if (uvc->control_ep)
474 uvc->control_ep->driver_data = NULL;
475 if (uvc->video.ep)
476 uvc->video.ep->driver_data = NULL;
478 if (uvc->control_req) {
479 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
480 kfree(uvc->control_buf);
483 kfree(f->descriptors);
484 kfree(f->hs_descriptors);
486 kfree(uvc);
489 static int __init
490 uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
492 struct usb_composite_dev *cdev = c->cdev;
493 struct uvc_device *uvc = to_uvc(f);
494 struct usb_ep *ep;
495 int ret = -EINVAL;
497 INFO(cdev, "uvc_function_bind\n");
499 /* Allocate endpoints. */
500 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
501 if (!ep) {
502 INFO(cdev, "Unable to allocate control EP\n");
503 goto error;
505 uvc->control_ep = ep;
506 ep->driver_data = uvc;
508 ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
509 if (!ep) {
510 INFO(cdev, "Unable to allocate streaming EP\n");
511 goto error;
513 uvc->video.ep = ep;
514 ep->driver_data = uvc;
516 /* Allocate interface IDs. */
517 if ((ret = usb_interface_id(c, f)) < 0)
518 goto error;
519 uvc_iad.bFirstInterface = ret;
520 uvc_control_intf.bInterfaceNumber = ret;
521 uvc->control_intf = ret;
523 if ((ret = usb_interface_id(c, f)) < 0)
524 goto error;
525 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
526 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
527 uvc->streaming_intf = ret;
529 /* Copy descriptors. */
530 f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
531 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
533 /* Preallocate control endpoint request. */
534 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
535 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
536 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
537 ret = -ENOMEM;
538 goto error;
541 uvc->control_req->buf = uvc->control_buf;
542 uvc->control_req->complete = uvc_function_ep0_complete;
543 uvc->control_req->context = uvc;
545 /* Avoid letting this gadget enumerate until the userspace server is
546 * active.
548 if ((ret = usb_function_deactivate(f)) < 0)
549 goto error;
551 /* Initialise video. */
552 ret = uvc_video_init(&uvc->video);
553 if (ret < 0)
554 goto error;
556 /* Register a V4L2 device. */
557 ret = uvc_register_video(uvc);
558 if (ret < 0) {
559 printk(KERN_INFO "Unable to register video device\n");
560 goto error;
563 return 0;
565 error:
566 uvc_function_unbind(c, f);
567 return ret;
570 /* --------------------------------------------------------------------------
571 * USB gadget function
575 * uvc_bind_config - add a UVC function to a configuration
576 * @c: the configuration to support the UVC instance
577 * Context: single threaded during gadget setup
579 * Returns zero on success, else negative errno.
581 * Caller must have called @uvc_setup(). Caller is also responsible for
582 * calling @uvc_cleanup() before module unload.
584 int __init
585 uvc_bind_config(struct usb_configuration *c,
586 const struct uvc_descriptor_header * const *control,
587 const struct uvc_descriptor_header * const *fs_streaming,
588 const struct uvc_descriptor_header * const *hs_streaming)
590 struct uvc_device *uvc;
591 int ret = 0;
593 /* TODO Check if the USB device controller supports the required
594 * features.
596 if (!gadget_is_dualspeed(c->cdev->gadget))
597 return -EINVAL;
599 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
600 if (uvc == NULL)
601 return -ENOMEM;
603 uvc->state = UVC_STATE_DISCONNECTED;
605 /* Validate the descriptors. */
606 if (control == NULL || control[0] == NULL ||
607 control[0]->bDescriptorSubType != UVC_VC_HEADER)
608 goto error;
610 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
611 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
612 goto error;
614 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
615 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
616 goto error;
618 uvc->desc.control = control;
619 uvc->desc.fs_streaming = fs_streaming;
620 uvc->desc.hs_streaming = hs_streaming;
622 /* Allocate string descriptor numbers. */
623 if ((ret = usb_string_id(c->cdev)) < 0)
624 goto error;
625 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
626 uvc_iad.iFunction = ret;
628 if ((ret = usb_string_id(c->cdev)) < 0)
629 goto error;
630 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
631 uvc_control_intf.iInterface = ret;
633 if ((ret = usb_string_id(c->cdev)) < 0)
634 goto error;
635 uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
636 uvc_streaming_intf_alt0.iInterface = ret;
637 uvc_streaming_intf_alt1.iInterface = ret;
639 /* Register the function. */
640 uvc->func.name = "uvc";
641 uvc->func.strings = uvc_function_strings;
642 uvc->func.bind = uvc_function_bind;
643 uvc->func.unbind = uvc_function_unbind;
644 uvc->func.get_alt = uvc_function_get_alt;
645 uvc->func.set_alt = uvc_function_set_alt;
646 uvc->func.disable = uvc_function_disable;
647 uvc->func.setup = uvc_function_setup;
649 ret = usb_add_function(c, &uvc->func);
650 if (ret)
651 kfree(uvc);
653 return ret;
655 error:
656 kfree(uvc);
657 return ret;
660 module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
661 MODULE_PARM_DESC(trace, "Trace level bitmask");