blob: 62ca0c5c7f6e14742380630c9d23a08ddbef5830 [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>
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030014#include <linux/module.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020015#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/fs.h>
18#include <linux/list.h>
19#include <linux/mutex.h>
Chen Gang4d2079c2013-02-02 15:48:54 +080020#include <linux/string.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020021#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 "uvc.h"
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030031#include "uvc_v4l2.h"
32#include "uvc_video.h"
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030033#include "u_uvc.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020034
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030035unsigned int uvc_gadget_trace_param;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053036
Laurent Pinchartcdda4792010-05-02 20:57:41 +020037/* --------------------------------------------------------------------------
38 * Function descriptors
39 */
40
41/* string IDs are assigned dynamically */
42
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010043#define UVC_STRING_CONTROL_IDX 0
44#define UVC_STRING_STREAMING_IDX 1
Laurent Pinchartcdda4792010-05-02 20:57:41 +020045
46static struct usb_string uvc_en_us_strings[] = {
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010047 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
Laurent Pinchartcdda4792010-05-02 20:57:41 +020048 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
49 { }
50};
51
52static struct usb_gadget_strings uvc_stringtab = {
53 .language = 0x0409, /* en-us */
54 .strings = uvc_en_us_strings,
55};
56
57static struct usb_gadget_strings *uvc_function_strings[] = {
58 &uvc_stringtab,
59 NULL,
60};
61
62#define UVC_INTF_VIDEO_CONTROL 0
63#define UVC_INTF_VIDEO_STREAMING 1
64
Laurent Pinchart912ca422013-03-01 20:46:23 +010065#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053066
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030067static struct usb_interface_assoc_descriptor uvc_iad = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030068 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020069 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
70 .bFirstInterface = 0,
71 .bInterfaceCount = 2,
72 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030073 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020074 .bFunctionProtocol = 0x00,
75 .iFunction = 0,
76};
77
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030078static struct usb_interface_descriptor uvc_control_intf = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020079 .bLength = USB_DT_INTERFACE_SIZE,
80 .bDescriptorType = USB_DT_INTERFACE,
81 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
82 .bAlternateSetting = 0,
83 .bNumEndpoints = 1,
84 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030085 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020086 .bInterfaceProtocol = 0x00,
87 .iInterface = 0,
88};
89
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030090static struct usb_endpoint_descriptor uvc_control_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020091 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93 .bEndpointAddress = USB_DIR_IN,
94 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +010095 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020096 .bInterval = 8,
97};
98
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030099static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100100 .bLength = sizeof(uvc_ss_control_comp),
101 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
102 /* The following 3 values can be tweaked if necessary. */
103 .bMaxBurst = 0,
104 .bmAttributes = 0,
105 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
106};
107
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300108static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200109 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_CS_ENDPOINT,
111 .bDescriptorSubType = UVC_EP_INTERRUPT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100112 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200113};
114
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300115static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200116 .bLength = USB_DT_INTERFACE_SIZE,
117 .bDescriptorType = USB_DT_INTERFACE,
118 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
119 .bAlternateSetting = 0,
120 .bNumEndpoints = 0,
121 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300122 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200123 .bInterfaceProtocol = 0x00,
124 .iInterface = 0,
125};
126
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300127static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200128 .bLength = USB_DT_INTERFACE_SIZE,
129 .bDescriptorType = USB_DT_INTERFACE,
130 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
131 .bAlternateSetting = 1,
132 .bNumEndpoints = 1,
133 .bInterfaceClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300134 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200135 .bInterfaceProtocol = 0x00,
136 .iInterface = 0,
137};
138
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300139static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200140 .bLength = USB_DT_ENDPOINT_SIZE,
141 .bDescriptorType = USB_DT_ENDPOINT,
142 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100143 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
144 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100145 /* The wMaxPacketSize and bInterval values will be initialized from
146 * module parameters.
147 */
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200148};
149
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300150static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530151 .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 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530159};
160
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300161static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100162 .bLength = USB_DT_ENDPOINT_SIZE,
163 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530164
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100165 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100166 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
167 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100168 /* The wMaxPacketSize and bInterval values will be initialized from
169 * module parameters.
170 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530171};
172
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300173static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100174 .bLength = sizeof(uvc_ss_streaming_comp),
175 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchartdbbafe62013-04-29 12:16:30 +0200176 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
177 * initialized from module parameters.
178 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530179};
180
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200181static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
182 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530183 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200184 NULL,
185};
186
187static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
188 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530189 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
190 NULL,
191};
192
193static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
194 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
195 (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
196 (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200197 NULL,
198};
199
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300200void uvc_set_trace_param(unsigned int trace)
201{
202 uvc_gadget_trace_param = trace;
203}
204EXPORT_SYMBOL(uvc_set_trace_param);
205
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200206/* --------------------------------------------------------------------------
207 * Control requests
208 */
209
210static void
211uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
212{
213 struct uvc_device *uvc = req->context;
214 struct v4l2_event v4l2_event;
215 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
216
217 if (uvc->event_setup_out) {
218 uvc->event_setup_out = 0;
219
220 memset(&v4l2_event, 0, sizeof(v4l2_event));
221 v4l2_event.type = UVC_EVENT_DATA;
222 uvc_event->data.length = req->actual;
223 memcpy(&uvc_event->data.data, req->buf, req->actual);
224 v4l2_event_queue(uvc->vdev, &v4l2_event);
225 }
226}
227
228static int
229uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
230{
231 struct uvc_device *uvc = to_uvc(f);
232 struct v4l2_event v4l2_event;
233 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
234
235 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
236 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
237 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
238 */
239
240 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
241 INFO(f->config->cdev, "invalid request type\n");
242 return -EINVAL;
243 }
244
245 /* Stall too big requests. */
246 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
247 return -EINVAL;
248
Laurent Pinchart26a029f2014-09-08 11:18:14 +0300249 /* Tell the complete callback to generate an event for the next request
250 * that will be enqueued by UVCIOC_SEND_RESPONSE.
251 */
252 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
253 uvc->event_length = le16_to_cpu(ctrl->wLength);
254
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200255 memset(&v4l2_event, 0, sizeof(v4l2_event));
256 v4l2_event.type = UVC_EVENT_SETUP;
257 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
258 v4l2_event_queue(uvc->vdev, &v4l2_event);
259
260 return 0;
261}
262
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100263void uvc_function_setup_continue(struct uvc_device *uvc)
264{
265 struct usb_composite_dev *cdev = uvc->func.config->cdev;
266
267 usb_composite_setup_continue(cdev);
268}
269
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200270static int
271uvc_function_get_alt(struct usb_function *f, unsigned interface)
272{
273 struct uvc_device *uvc = to_uvc(f);
274
275 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
276
277 if (interface == uvc->control_intf)
278 return 0;
279 else if (interface != uvc->streaming_intf)
280 return -EINVAL;
281 else
Felipe Balbie975be22014-09-29 11:13:26 -0500282 return uvc->video.ep->driver_data ? 1 : 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200283}
284
285static int
286uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
287{
288 struct uvc_device *uvc = to_uvc(f);
Felipe Balbic92bae72014-09-29 09:20:35 -0500289 struct usb_composite_dev *cdev = f->config->cdev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200290 struct v4l2_event v4l2_event;
291 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530292 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200293
Felipe Balbic92bae72014-09-29 09:20:35 -0500294 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200295
296 if (interface == uvc->control_intf) {
297 if (alt)
298 return -EINVAL;
299
Felipe Balbi62e37072014-09-29 13:41:04 -0500300 if (uvc->control_ep->driver_data) {
301 INFO(cdev, "reset UVC Control\n");
302 usb_ep_disable(uvc->control_ep);
303 uvc->control_ep->driver_data = NULL;
304 }
305
306 if (!uvc->control_ep->desc)
307 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
308 return -EINVAL;
309
310 usb_ep_enable(uvc->control_ep);
311 uvc->control_ep->driver_data = uvc;
312
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200313 if (uvc->state == UVC_STATE_DISCONNECTED) {
314 memset(&v4l2_event, 0, sizeof(v4l2_event));
315 v4l2_event.type = UVC_EVENT_CONNECT;
Felipe Balbic92bae72014-09-29 09:20:35 -0500316 uvc_event->speed = cdev->gadget->speed;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200317 v4l2_event_queue(uvc->vdev, &v4l2_event);
318
319 uvc->state = UVC_STATE_CONNECTED;
320 }
321
322 return 0;
323 }
324
325 if (interface != uvc->streaming_intf)
326 return -EINVAL;
327
328 /* TODO
329 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
330 return alt ? -EINVAL : 0;
331 */
332
333 switch (alt) {
334 case 0:
335 if (uvc->state != UVC_STATE_STREAMING)
336 return 0;
337
Felipe Balbic92bae72014-09-29 09:20:35 -0500338 if (uvc->video.ep) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200339 usb_ep_disable(uvc->video.ep);
Felipe Balbic92bae72014-09-29 09:20:35 -0500340 uvc->video.ep->driver_data = NULL;
341 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200342
343 memset(&v4l2_event, 0, sizeof(v4l2_event));
344 v4l2_event.type = UVC_EVENT_STREAMOFF;
345 v4l2_event_queue(uvc->vdev, &v4l2_event);
346
347 uvc->state = UVC_STATE_CONNECTED;
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100348 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200349
350 case 1:
351 if (uvc->state != UVC_STATE_CONNECTED)
352 return 0;
353
Felipe Balbic92bae72014-09-29 09:20:35 -0500354 if (!uvc->video.ep)
355 return -EINVAL;
356
357 if (uvc->video.ep->driver_data) {
358 INFO(cdev, "reset UVC\n");
359 usb_ep_disable(uvc->video.ep);
360 uvc->video.ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300361 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200362
Felipe Balbic92bae72014-09-29 09:20:35 -0500363 ret = config_ep_by_speed(f->config->cdev->gadget,
364 &(uvc->func), uvc->video.ep);
365 if (ret)
366 return ret;
367 usb_ep_enable(uvc->video.ep);
368 uvc->video.ep->driver_data = uvc;
369
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200370 memset(&v4l2_event, 0, sizeof(v4l2_event));
371 v4l2_event.type = UVC_EVENT_STREAMON;
372 v4l2_event_queue(uvc->vdev, &v4l2_event);
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100373 return USB_GADGET_DELAYED_STATUS;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200374
375 default:
376 return -EINVAL;
377 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200378}
379
380static void
381uvc_function_disable(struct usb_function *f)
382{
383 struct uvc_device *uvc = to_uvc(f);
384 struct v4l2_event v4l2_event;
385
386 INFO(f->config->cdev, "uvc_function_disable\n");
387
388 memset(&v4l2_event, 0, sizeof(v4l2_event));
389 v4l2_event.type = UVC_EVENT_DISCONNECT;
390 v4l2_event_queue(uvc->vdev, &v4l2_event);
391
392 uvc->state = UVC_STATE_DISCONNECTED;
Felipe Balbie3122f52014-09-29 13:43:20 -0500393
394 if (uvc->video.ep->driver_data) {
395 usb_ep_disable(uvc->video.ep);
396 uvc->video.ep->driver_data = NULL;
397 }
398
399 if (uvc->control_ep->driver_data) {
400 usb_ep_disable(uvc->control_ep);
401 uvc->control_ep->driver_data = NULL;
402 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200403}
404
405/* --------------------------------------------------------------------------
406 * Connection / disconnection
407 */
408
409void
410uvc_function_connect(struct uvc_device *uvc)
411{
412 struct usb_composite_dev *cdev = uvc->func.config->cdev;
413 int ret;
414
415 if ((ret = usb_function_activate(&uvc->func)) < 0)
416 INFO(cdev, "UVC connect failed with %d\n", ret);
417}
418
419void
420uvc_function_disconnect(struct uvc_device *uvc)
421{
422 struct usb_composite_dev *cdev = uvc->func.config->cdev;
423 int ret;
424
425 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
426 INFO(cdev, "UVC disconnect failed with %d\n", ret);
427}
428
429/* --------------------------------------------------------------------------
430 * USB probe and disconnect
431 */
432
433static int
434uvc_register_video(struct uvc_device *uvc)
435{
436 struct usb_composite_dev *cdev = uvc->func.config->cdev;
437 struct video_device *video;
438
439 /* TODO reference counting. */
440 video = video_device_alloc();
441 if (video == NULL)
442 return -ENOMEM;
443
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300444 video->v4l2_dev = &uvc->v4l2_dev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200445 video->fops = &uvc_v4l2_fops;
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300446 video->ioctl_ops = &uvc_v4l2_ioctl_ops;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200447 video->release = video_device_release;
Laurent Pincharta1d27a42014-09-08 11:18:15 +0300448 video->vfl_dir = VFL_DIR_TX;
Chen Gang4d2079c2013-02-02 15:48:54 +0800449 strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200450
451 uvc->vdev = video;
452 video_set_drvdata(video, uvc);
453
454 return video_register_device(video, VFL_TYPE_GRABBER, -1);
455}
456
457#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
458 do { \
459 memcpy(mem, desc, (desc)->bLength); \
460 *(dst)++ = mem; \
461 mem += (desc)->bLength; \
462 } while (0);
463
464#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
465 do { \
466 const struct usb_descriptor_header * const *__src; \
467 for (__src = src; *__src; ++__src) { \
468 memcpy(mem, *__src, (*__src)->bLength); \
469 *dst++ = mem; \
470 mem += (*__src)->bLength; \
471 } \
472 } while (0)
473
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300474static struct usb_descriptor_header **
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200475uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
476{
477 struct uvc_input_header_descriptor *uvc_streaming_header;
478 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530479 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200480 const struct uvc_descriptor_header * const *uvc_streaming_cls;
481 const struct usb_descriptor_header * const *uvc_streaming_std;
482 const struct usb_descriptor_header * const *src;
483 struct usb_descriptor_header **dst;
484 struct usb_descriptor_header **hdr;
485 unsigned int control_size;
486 unsigned int streaming_size;
487 unsigned int n_desc;
488 unsigned int bytes;
489 void *mem;
490
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530491 switch (speed) {
492 case USB_SPEED_SUPER:
493 uvc_control_desc = uvc->desc.ss_control;
494 uvc_streaming_cls = uvc->desc.ss_streaming;
495 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530496 break;
497
498 case USB_SPEED_HIGH:
499 uvc_control_desc = uvc->desc.fs_control;
500 uvc_streaming_cls = uvc->desc.hs_streaming;
501 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530502 break;
503
504 case USB_SPEED_FULL:
505 default:
506 uvc_control_desc = uvc->desc.fs_control;
507 uvc_streaming_cls = uvc->desc.fs_streaming;
508 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530509 break;
510 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200511
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100512 if (!uvc_control_desc || !uvc_streaming_cls)
513 return ERR_PTR(-ENODEV);
514
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200515 /* Descriptors layout
516 *
517 * uvc_iad
518 * uvc_control_intf
519 * Class-specific UVC control descriptors
520 * uvc_control_ep
521 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100522 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200523 * uvc_streaming_intf_alt0
524 * Class-specific UVC streaming descriptors
525 * uvc_{fs|hs}_streaming
526 */
527
528 /* Count descriptors and compute their size. */
529 control_size = 0;
530 streaming_size = 0;
531 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100532 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200533 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200534
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530535 if (speed == USB_SPEED_SUPER) {
536 bytes += uvc_ss_control_comp.bLength;
537 n_desc = 6;
538 } else {
539 n_desc = 5;
540 }
541
542 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100543 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200544 control_size += (*src)->bLength;
545 bytes += (*src)->bLength;
546 n_desc++;
547 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530548 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100549 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200550 streaming_size += (*src)->bLength;
551 bytes += (*src)->bLength;
552 n_desc++;
553 }
554 for (src = uvc_streaming_std; *src; ++src) {
555 bytes += (*src)->bLength;
556 n_desc++;
557 }
558
559 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
560 if (mem == NULL)
561 return NULL;
562
563 hdr = mem;
564 dst = mem;
565 mem += (n_desc + 1) * sizeof(*src);
566
567 /* Copy the descriptors. */
568 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
569 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
570
571 uvc_control_header = mem;
572 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530573 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200574 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
575 uvc_control_header->bInCollection = 1;
576 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
577
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100578 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530579 if (speed == USB_SPEED_SUPER)
580 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
581
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200582 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
583 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
584
585 uvc_streaming_header = mem;
586 UVC_COPY_DESCRIPTORS(mem, dst,
587 (const struct usb_descriptor_header**)uvc_streaming_cls);
588 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100589 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200590
591 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
592
593 *dst = NULL;
594 return hdr;
595}
596
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300597static int
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200598uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
599{
600 struct usb_composite_dev *cdev = c->cdev;
601 struct uvc_device *uvc = to_uvc(f);
Andrzej Pietrasiewicz13443792014-09-09 02:02:13 +0300602 struct usb_string *us;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100603 unsigned int max_packet_mult;
604 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200605 struct usb_ep *ep;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300606 struct f_uvc_opts *opts;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200607 int ret = -EINVAL;
608
609 INFO(cdev, "uvc_function_bind\n");
610
Andrzej Pietrasiewiczbbea6de12014-12-10 12:34:00 +0100611 opts = fi_to_f_uvc_opts(f->fi);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300612 /* Sanity check the streaming endpoint module parameters.
613 */
614 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
615 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
616 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
617
618 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
619 * module parameters.
620 *
621 * NOTE: We assume that the user knows what they are doing and won't
622 * give parameters that their UDC doesn't support.
623 */
624 if (opts->streaming_maxpacket <= 1024) {
625 max_packet_mult = 1;
626 max_packet_size = opts->streaming_maxpacket;
627 } else if (opts->streaming_maxpacket <= 2048) {
628 max_packet_mult = 2;
629 max_packet_size = opts->streaming_maxpacket / 2;
630 } else {
631 max_packet_mult = 3;
632 max_packet_size = opts->streaming_maxpacket / 3;
633 }
634
635 uvc_fs_streaming_ep.wMaxPacketSize =
Laurent Pincharte1026092014-09-16 17:26:47 +0300636 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300637 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
638
Laurent Pincharte1026092014-09-16 17:26:47 +0300639 uvc_hs_streaming_ep.wMaxPacketSize =
640 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300641 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
642
Laurent Pincharte1026092014-09-16 17:26:47 +0300643 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300644 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
645 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
646 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
647 uvc_ss_streaming_comp.wBytesPerInterval =
Laurent Pincharte1026092014-09-16 17:26:47 +0300648 cpu_to_le16(max_packet_size * max_packet_mult *
649 opts->streaming_maxburst);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100650
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200651 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100652 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200653 if (!ep) {
654 INFO(cdev, "Unable to allocate control EP\n");
655 goto error;
656 }
657 uvc->control_ep = ep;
658 ep->driver_data = uvc;
659
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100660 if (gadget_is_superspeed(c->cdev->gadget))
661 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
662 &uvc_ss_streaming_comp);
663 else if (gadget_is_dualspeed(cdev->gadget))
664 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
665 else
666 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
667
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200668 if (!ep) {
669 INFO(cdev, "Unable to allocate streaming EP\n");
670 goto error;
671 }
672 uvc->video.ep = ep;
673 ep->driver_data = uvc;
674
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100675 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
676 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
677 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100678
Andrzej Pietrasiewicz13443792014-09-09 02:02:13 +0300679 us = usb_gstrings_attach(cdev, uvc_function_strings,
680 ARRAY_SIZE(uvc_en_us_strings));
681 if (IS_ERR(us)) {
682 ret = PTR_ERR(us);
683 goto error;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300684 }
Andrzej Pietrasiewicz13443792014-09-09 02:02:13 +0300685 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
686 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
687 ret = us[UVC_STRING_STREAMING_IDX].id;
688 uvc_streaming_intf_alt0.iInterface = ret;
689 uvc_streaming_intf_alt1.iInterface = ret;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300690
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200691 /* Allocate interface IDs. */
692 if ((ret = usb_interface_id(c, f)) < 0)
693 goto error;
694 uvc_iad.bFirstInterface = ret;
695 uvc_control_intf.bInterfaceNumber = ret;
696 uvc->control_intf = ret;
697
698 if ((ret = usb_interface_id(c, f)) < 0)
699 goto error;
700 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
701 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
702 uvc->streaming_intf = ret;
703
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200704 /* Copy descriptors */
705 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100706 if (IS_ERR(f->fs_descriptors)) {
707 ret = PTR_ERR(f->fs_descriptors);
708 f->fs_descriptors = NULL;
709 goto error;
710 }
711 if (gadget_is_dualspeed(cdev->gadget)) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530712 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100713 if (IS_ERR(f->hs_descriptors)) {
714 ret = PTR_ERR(f->hs_descriptors);
715 f->hs_descriptors = NULL;
716 goto error;
717 }
718 }
719 if (gadget_is_superspeed(c->cdev->gadget)) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530720 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100721 if (IS_ERR(f->ss_descriptors)) {
722 ret = PTR_ERR(f->ss_descriptors);
723 f->ss_descriptors = NULL;
724 goto error;
725 }
726 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200727
728 /* Preallocate control endpoint request. */
729 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
730 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
731 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
732 ret = -ENOMEM;
733 goto error;
734 }
735
736 uvc->control_req->buf = uvc->control_buf;
737 uvc->control_req->complete = uvc_function_ep0_complete;
738 uvc->control_req->context = uvc;
739
740 /* Avoid letting this gadget enumerate until the userspace server is
741 * active.
742 */
743 if ((ret = usb_function_deactivate(f)) < 0)
744 goto error;
745
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300746 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
747 printk(KERN_INFO "v4l2_device_register failed\n");
748 goto error;
749 }
750
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200751 /* Initialise video. */
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300752 ret = uvcg_video_init(&uvc->video);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200753 if (ret < 0)
754 goto error;
755
756 /* Register a V4L2 device. */
757 ret = uvc_register_video(uvc);
758 if (ret < 0) {
759 printk(KERN_INFO "Unable to register video device\n");
760 goto error;
761 }
762
763 return 0;
764
765error:
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300766 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200767 if (uvc->vdev)
768 video_device_release(uvc->vdev);
769
770 if (uvc->control_ep)
771 uvc->control_ep->driver_data = NULL;
772 if (uvc->video.ep)
773 uvc->video.ep->driver_data = NULL;
774
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200775 if (uvc->control_req)
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200776 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200777 kfree(uvc->control_buf);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200778
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200779 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200780 return ret;
781}
782
783/* --------------------------------------------------------------------------
784 * USB gadget function
785 */
786
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300787static void uvc_free_inst(struct usb_function_instance *f)
788{
Andrzej Pietrasiewiczbbea6de12014-12-10 12:34:00 +0100789 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300790
791 kfree(opts);
792}
793
794static struct usb_function_instance *uvc_alloc_inst(void)
795{
796 struct f_uvc_opts *opts;
797
798 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
799 if (!opts)
800 return ERR_PTR(-ENOMEM);
801 opts->func_inst.free_func_inst = uvc_free_inst;
802
803 return &opts->func_inst;
804}
805
806static void uvc_free(struct usb_function *f)
807{
808 struct uvc_device *uvc = to_uvc(f);
809
810 kfree(uvc);
811}
812
813static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
814{
815 struct usb_composite_dev *cdev = c->cdev;
816 struct uvc_device *uvc = to_uvc(f);
817
818 INFO(cdev, "%s\n", __func__);
819
820 video_unregister_device(uvc->vdev);
821 v4l2_device_unregister(&uvc->v4l2_dev);
822 uvc->control_ep->driver_data = NULL;
823 uvc->video.ep->driver_data = NULL;
824
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300825 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
826 kfree(uvc->control_buf);
827
828 usb_free_all_descriptors(f);
829}
830
Fengguang Wu4a6698b2014-09-16 17:26:46 +0300831static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300832{
833 struct uvc_device *uvc;
834 struct f_uvc_opts *opts;
835
836 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
837 if (uvc == NULL)
838 return ERR_PTR(-ENOMEM);
839
840 uvc->state = UVC_STATE_DISCONNECTED;
Andrzej Pietrasiewiczbbea6de12014-12-10 12:34:00 +0100841 opts = fi_to_f_uvc_opts(fi);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300842
843 uvc->desc.fs_control = opts->fs_control;
844 uvc->desc.ss_control = opts->ss_control;
845 uvc->desc.fs_streaming = opts->fs_streaming;
846 uvc->desc.hs_streaming = opts->hs_streaming;
847 uvc->desc.ss_streaming = opts->ss_streaming;
848
849 /* Register the function. */
850 uvc->func.name = "uvc";
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300851 uvc->func.bind = uvc_function_bind;
852 uvc->func.unbind = uvc_unbind;
853 uvc->func.get_alt = uvc_function_get_alt;
854 uvc->func.set_alt = uvc_function_set_alt;
855 uvc->func.disable = uvc_function_disable;
856 uvc->func.setup = uvc_function_setup;
857 uvc->func.free_func = uvc_free;
858
859 return &uvc->func;
860}
861
862DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
863MODULE_LICENSE("GPL");
864MODULE_AUTHOR("Laurent Pinchart");