blob: 743be34605dcbff26eaf066c80578fb5112e41cf [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
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +010030#include "u_uvc.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020031#include "uvc.h"
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +010032#include "uvc_configfs.h"
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030033#include "uvc_v4l2.h"
34#include "uvc_video.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020035
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030036unsigned int uvc_gadget_trace_param;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +053037
Laurent Pinchartcdda4792010-05-02 20:57:41 +020038/* --------------------------------------------------------------------------
39 * Function descriptors
40 */
41
42/* string IDs are assigned dynamically */
43
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010044#define UVC_STRING_CONTROL_IDX 0
45#define UVC_STRING_STREAMING_IDX 1
Laurent Pinchartcdda4792010-05-02 20:57:41 +020046
47static struct usb_string uvc_en_us_strings[] = {
Bhupesh Sharma43ff05e2013-03-01 20:46:29 +010048 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
Laurent Pinchartcdda4792010-05-02 20:57:41 +020049 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
50 { }
51};
52
53static struct usb_gadget_strings uvc_stringtab = {
54 .language = 0x0409, /* en-us */
55 .strings = uvc_en_us_strings,
56};
57
58static 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
Laurent Pinchart912ca422013-03-01 20:46:23 +010066#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
Bhupesh Sharma57976632012-06-01 15:08:55 +053067
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030068static struct usb_interface_assoc_descriptor uvc_iad = {
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030069 .bLength = sizeof(uvc_iad),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020070 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
71 .bFirstInterface = 0,
72 .bInterfaceCount = 2,
73 .bFunctionClass = USB_CLASS_VIDEO,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030074 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020075 .bFunctionProtocol = 0x00,
76 .iFunction = 0,
77};
78
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030079static struct usb_interface_descriptor uvc_control_intf = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020080 .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,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030086 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
Laurent Pinchartcdda4792010-05-02 20:57:41 +020087 .bInterfaceProtocol = 0x00,
88 .iInterface = 0,
89};
90
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +030091static struct usb_endpoint_descriptor uvc_control_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020092 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_INT,
Laurent Pinchart912ca422013-03-01 20:46:23 +010096 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +020097 .bInterval = 8,
98};
99
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300100static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100101 .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
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300109static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200110 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
111 .bDescriptorType = USB_DT_CS_ENDPOINT,
112 .bDescriptorSubType = UVC_EP_INTERRUPT,
Laurent Pinchart912ca422013-03-01 20:46:23 +0100113 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200114};
115
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300116static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200117 .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,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300123 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200124 .bInterfaceProtocol = 0x00,
125 .iInterface = 0,
126};
127
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300128static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200129 .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,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300135 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200136 .bInterfaceProtocol = 0x00,
137 .iInterface = 0,
138};
139
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300140static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100144 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
145 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100146 /* The wMaxPacketSize and bInterval values will be initialized from
147 * module parameters.
148 */
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200149};
150
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300151static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100155 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
156 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100157 /* The wMaxPacketSize and bInterval values will be initialized from
158 * module parameters.
159 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530160};
161
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300162static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530165
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100166 .bEndpointAddress = USB_DIR_IN,
Bhupesh Sharma609a0532013-03-01 20:46:28 +0100167 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
168 | USB_ENDPOINT_XFER_ISOC,
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100169 /* The wMaxPacketSize and bInterval values will be initialized from
170 * module parameters.
171 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530172};
173
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300174static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100175 .bLength = sizeof(uvc_ss_streaming_comp),
176 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Laurent Pinchartdbbafe62013-04-29 12:16:30 +0200177 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be
178 * initialized from module parameters.
179 */
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530180};
181
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200182static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
183 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530184 (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200185 NULL,
186};
187
188static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
189 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530190 (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
191 NULL,
192};
193
194static 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,
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200198 NULL,
199};
200
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300201void uvc_set_trace_param(unsigned int trace)
202{
203 uvc_gadget_trace_param = trace;
204}
205EXPORT_SYMBOL(uvc_set_trace_param);
206
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200207/* --------------------------------------------------------------------------
208 * Control requests
209 */
210
211static void
212uvc_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);
Hans Verkuildbe98b32015-03-09 13:34:08 -0300225 v4l2_event_queue(&uvc->vdev, &v4l2_event);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200226 }
227}
228
229static int
230uvc_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
Laurent Pinchart26a029f2014-09-08 11:18:14 +0300250 /* 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
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200256 memset(&v4l2_event, 0, sizeof(v4l2_event));
257 v4l2_event.type = UVC_EVENT_SETUP;
258 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
Hans Verkuildbe98b32015-03-09 13:34:08 -0300259 v4l2_event_queue(&uvc->vdev, &v4l2_event);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200260
261 return 0;
262}
263
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100264void uvc_function_setup_continue(struct uvc_device *uvc)
265{
266 struct usb_composite_dev *cdev = uvc->func.config->cdev;
267
268 usb_composite_setup_continue(cdev);
269}
270
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200271static int
272uvc_function_get_alt(struct usb_function *f, unsigned interface)
273{
274 struct uvc_device *uvc = to_uvc(f);
275
276 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
277
278 if (interface == uvc->control_intf)
279 return 0;
280 else if (interface != uvc->streaming_intf)
281 return -EINVAL;
282 else
Felipe Balbie975be22014-09-29 11:13:26 -0500283 return uvc->video.ep->driver_data ? 1 : 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200284}
285
286static int
287uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
288{
289 struct uvc_device *uvc = to_uvc(f);
Felipe Balbic92bae72014-09-29 09:20:35 -0500290 struct usb_composite_dev *cdev = f->config->cdev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200291 struct v4l2_event v4l2_event;
292 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530293 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200294
Felipe Balbic92bae72014-09-29 09:20:35 -0500295 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200296
297 if (interface == uvc->control_intf) {
298 if (alt)
299 return -EINVAL;
300
Felipe Balbi62e37072014-09-29 13:41:04 -0500301 if (uvc->control_ep->driver_data) {
302 INFO(cdev, "reset UVC Control\n");
303 usb_ep_disable(uvc->control_ep);
304 uvc->control_ep->driver_data = NULL;
305 }
306
307 if (!uvc->control_ep->desc)
308 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
309 return -EINVAL;
310
311 usb_ep_enable(uvc->control_ep);
312 uvc->control_ep->driver_data = uvc;
313
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200314 if (uvc->state == UVC_STATE_DISCONNECTED) {
315 memset(&v4l2_event, 0, sizeof(v4l2_event));
316 v4l2_event.type = UVC_EVENT_CONNECT;
Felipe Balbic92bae72014-09-29 09:20:35 -0500317 uvc_event->speed = cdev->gadget->speed;
Hans Verkuildbe98b32015-03-09 13:34:08 -0300318 v4l2_event_queue(&uvc->vdev, &v4l2_event);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200319
320 uvc->state = UVC_STATE_CONNECTED;
321 }
322
323 return 0;
324 }
325
326 if (interface != uvc->streaming_intf)
327 return -EINVAL;
328
329 /* TODO
330 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
331 return alt ? -EINVAL : 0;
332 */
333
334 switch (alt) {
335 case 0:
336 if (uvc->state != UVC_STATE_STREAMING)
337 return 0;
338
Felipe Balbic92bae72014-09-29 09:20:35 -0500339 if (uvc->video.ep) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200340 usb_ep_disable(uvc->video.ep);
Felipe Balbic92bae72014-09-29 09:20:35 -0500341 uvc->video.ep->driver_data = NULL;
342 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200343
344 memset(&v4l2_event, 0, sizeof(v4l2_event));
345 v4l2_event.type = UVC_EVENT_STREAMOFF;
Hans Verkuildbe98b32015-03-09 13:34:08 -0300346 v4l2_event_queue(&uvc->vdev, &v4l2_event);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200347
348 uvc->state = UVC_STATE_CONNECTED;
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100349 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200350
351 case 1:
352 if (uvc->state != UVC_STATE_CONNECTED)
353 return 0;
354
Felipe Balbic92bae72014-09-29 09:20:35 -0500355 if (!uvc->video.ep)
356 return -EINVAL;
357
358 if (uvc->video.ep->driver_data) {
359 INFO(cdev, "reset UVC\n");
360 usb_ep_disable(uvc->video.ep);
361 uvc->video.ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300362 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200363
Felipe Balbic92bae72014-09-29 09:20:35 -0500364 ret = config_ep_by_speed(f->config->cdev->gadget,
365 &(uvc->func), uvc->video.ep);
366 if (ret)
367 return ret;
368 usb_ep_enable(uvc->video.ep);
369 uvc->video.ep->driver_data = uvc;
370
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200371 memset(&v4l2_event, 0, sizeof(v4l2_event));
372 v4l2_event.type = UVC_EVENT_STREAMON;
Hans Verkuildbe98b32015-03-09 13:34:08 -0300373 v4l2_event_queue(&uvc->vdev, &v4l2_event);
Bhupesh Sharma41837c32013-03-01 20:46:30 +0100374 return USB_GADGET_DELAYED_STATUS;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200375
376 default:
377 return -EINVAL;
378 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200379}
380
381static void
382uvc_function_disable(struct usb_function *f)
383{
384 struct uvc_device *uvc = to_uvc(f);
385 struct v4l2_event v4l2_event;
386
387 INFO(f->config->cdev, "uvc_function_disable\n");
388
389 memset(&v4l2_event, 0, sizeof(v4l2_event));
390 v4l2_event.type = UVC_EVENT_DISCONNECT;
Hans Verkuildbe98b32015-03-09 13:34:08 -0300391 v4l2_event_queue(&uvc->vdev, &v4l2_event);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200392
393 uvc->state = UVC_STATE_DISCONNECTED;
Felipe Balbie3122f52014-09-29 13:43:20 -0500394
395 if (uvc->video.ep->driver_data) {
396 usb_ep_disable(uvc->video.ep);
397 uvc->video.ep->driver_data = NULL;
398 }
399
400 if (uvc->control_ep->driver_data) {
401 usb_ep_disable(uvc->control_ep);
402 uvc->control_ep->driver_data = NULL;
403 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200404}
405
406/* --------------------------------------------------------------------------
407 * Connection / disconnection
408 */
409
410void
411uvc_function_connect(struct uvc_device *uvc)
412{
413 struct usb_composite_dev *cdev = uvc->func.config->cdev;
414 int ret;
415
416 if ((ret = usb_function_activate(&uvc->func)) < 0)
417 INFO(cdev, "UVC connect failed with %d\n", ret);
418}
419
420void
421uvc_function_disconnect(struct uvc_device *uvc)
422{
423 struct usb_composite_dev *cdev = uvc->func.config->cdev;
424 int ret;
425
426 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
427 INFO(cdev, "UVC disconnect failed with %d\n", ret);
428}
429
430/* --------------------------------------------------------------------------
431 * USB probe and disconnect
432 */
433
434static int
435uvc_register_video(struct uvc_device *uvc)
436{
437 struct usb_composite_dev *cdev = uvc->func.config->cdev;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200438
439 /* TODO reference counting. */
Hans Verkuildbe98b32015-03-09 13:34:08 -0300440 uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
441 uvc->vdev.fops = &uvc_v4l2_fops;
442 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
443 uvc->vdev.release = video_device_release_empty;
444 uvc->vdev.vfl_dir = VFL_DIR_TX;
445 uvc->vdev.lock = &uvc->video.mutex;
446 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200447
Hans Verkuildbe98b32015-03-09 13:34:08 -0300448 video_set_drvdata(&uvc->vdev, uvc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200449
Hans Verkuildbe98b32015-03-09 13:34:08 -0300450 return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200451}
452
453#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
454 do { \
455 memcpy(mem, desc, (desc)->bLength); \
456 *(dst)++ = mem; \
457 mem += (desc)->bLength; \
458 } while (0);
459
460#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
461 do { \
462 const struct usb_descriptor_header * const *__src; \
463 for (__src = src; *__src; ++__src) { \
464 memcpy(mem, *__src, (*__src)->bLength); \
465 *dst++ = mem; \
466 mem += (*__src)->bLength; \
467 } \
468 } while (0)
469
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300470static struct usb_descriptor_header **
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200471uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
472{
473 struct uvc_input_header_descriptor *uvc_streaming_header;
474 struct uvc_header_descriptor *uvc_control_header;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530475 const struct uvc_descriptor_header * const *uvc_control_desc;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200476 const struct uvc_descriptor_header * const *uvc_streaming_cls;
477 const struct usb_descriptor_header * const *uvc_streaming_std;
478 const struct usb_descriptor_header * const *src;
479 struct usb_descriptor_header **dst;
480 struct usb_descriptor_header **hdr;
481 unsigned int control_size;
482 unsigned int streaming_size;
483 unsigned int n_desc;
484 unsigned int bytes;
485 void *mem;
486
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530487 switch (speed) {
488 case USB_SPEED_SUPER:
489 uvc_control_desc = uvc->desc.ss_control;
490 uvc_streaming_cls = uvc->desc.ss_streaming;
491 uvc_streaming_std = uvc_ss_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530492 break;
493
494 case USB_SPEED_HIGH:
495 uvc_control_desc = uvc->desc.fs_control;
496 uvc_streaming_cls = uvc->desc.hs_streaming;
497 uvc_streaming_std = uvc_hs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530498 break;
499
500 case USB_SPEED_FULL:
501 default:
502 uvc_control_desc = uvc->desc.fs_control;
503 uvc_streaming_cls = uvc->desc.fs_streaming;
504 uvc_streaming_std = uvc_fs_streaming;
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530505 break;
506 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200507
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100508 if (!uvc_control_desc || !uvc_streaming_cls)
509 return ERR_PTR(-ENODEV);
510
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200511 /* Descriptors layout
512 *
513 * uvc_iad
514 * uvc_control_intf
515 * Class-specific UVC control descriptors
516 * uvc_control_ep
517 * uvc_control_cs_ep
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100518 * uvc_ss_control_comp (for SS only)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200519 * uvc_streaming_intf_alt0
520 * Class-specific UVC streaming descriptors
521 * uvc_{fs|hs}_streaming
522 */
523
524 /* Count descriptors and compute their size. */
525 control_size = 0;
526 streaming_size = 0;
527 bytes = uvc_iad.bLength + uvc_control_intf.bLength
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100528 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200529 + uvc_streaming_intf_alt0.bLength;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200530
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530531 if (speed == USB_SPEED_SUPER) {
532 bytes += uvc_ss_control_comp.bLength;
533 n_desc = 6;
534 } else {
535 n_desc = 5;
536 }
537
538 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100539 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200540 control_size += (*src)->bLength;
541 bytes += (*src)->bLength;
542 n_desc++;
543 }
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530544 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
Laurent Pinchartee6a4d82013-03-01 20:46:24 +0100545 *src; ++src) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200546 streaming_size += (*src)->bLength;
547 bytes += (*src)->bLength;
548 n_desc++;
549 }
550 for (src = uvc_streaming_std; *src; ++src) {
551 bytes += (*src)->bLength;
552 n_desc++;
553 }
554
555 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
556 if (mem == NULL)
557 return NULL;
558
559 hdr = mem;
560 dst = mem;
561 mem += (n_desc + 1) * sizeof(*src);
562
563 /* Copy the descriptors. */
564 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
565 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
566
567 uvc_control_header = mem;
568 UVC_COPY_DESCRIPTORS(mem, dst,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530569 (const struct usb_descriptor_header **)uvc_control_desc);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200570 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
571 uvc_control_header->bInCollection = 1;
572 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
573
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100574 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530575 if (speed == USB_SPEED_SUPER)
576 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
577
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200578 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
579 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
580
581 uvc_streaming_header = mem;
582 UVC_COPY_DESCRIPTORS(mem, dst,
583 (const struct usb_descriptor_header**)uvc_streaming_cls);
584 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100585 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200586
587 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
588
589 *dst = NULL;
590 return hdr;
591}
592
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300593static int
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200594uvc_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);
Andrzej Pietrasiewicz13443792014-09-09 02:02:13 +0300598 struct usb_string *us;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100599 unsigned int max_packet_mult;
600 unsigned int max_packet_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200601 struct usb_ep *ep;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300602 struct f_uvc_opts *opts;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200603 int ret = -EINVAL;
604
605 INFO(cdev, "uvc_function_bind\n");
606
Andrzej Pietrasiewiczbbea6de12014-12-10 12:34:00 +0100607 opts = fi_to_f_uvc_opts(f->fi);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300608 /* Sanity check the streaming endpoint module parameters.
609 */
610 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
611 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
612 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
613
614 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
615 * module parameters.
616 *
617 * NOTE: We assume that the user knows what they are doing and won't
618 * give parameters that their UDC doesn't support.
619 */
620 if (opts->streaming_maxpacket <= 1024) {
621 max_packet_mult = 1;
622 max_packet_size = opts->streaming_maxpacket;
623 } else if (opts->streaming_maxpacket <= 2048) {
624 max_packet_mult = 2;
625 max_packet_size = opts->streaming_maxpacket / 2;
626 } else {
627 max_packet_mult = 3;
628 max_packet_size = opts->streaming_maxpacket / 3;
629 }
630
631 uvc_fs_streaming_ep.wMaxPacketSize =
Laurent Pincharte1026092014-09-16 17:26:47 +0300632 cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300633 uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
634
Laurent Pincharte1026092014-09-16 17:26:47 +0300635 uvc_hs_streaming_ep.wMaxPacketSize =
636 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300637 uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
638
Laurent Pincharte1026092014-09-16 17:26:47 +0300639 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300640 uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
641 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
642 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
643 uvc_ss_streaming_comp.wBytesPerInterval =
Laurent Pincharte1026092014-09-16 17:26:47 +0300644 cpu_to_le16(max_packet_size * max_packet_mult *
645 opts->streaming_maxburst);
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100646
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200647 /* Allocate endpoints. */
Laurent Pinchart48eee0b2013-03-01 20:46:25 +0100648 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200649 if (!ep) {
650 INFO(cdev, "Unable to allocate control EP\n");
651 goto error;
652 }
653 uvc->control_ep = ep;
654 ep->driver_data = uvc;
655
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100656 if (gadget_is_superspeed(c->cdev->gadget))
657 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
658 &uvc_ss_streaming_comp);
659 else if (gadget_is_dualspeed(cdev->gadget))
660 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
661 else
662 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
663
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200664 if (!ep) {
665 INFO(cdev, "Unable to allocate streaming EP\n");
666 goto error;
667 }
668 uvc->video.ep = ep;
669 ep->driver_data = uvc;
670
Laurent Pinchart0485ec02013-03-01 20:46:27 +0100671 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
672 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
673 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
Laurent Pinchart20777dd2013-03-01 20:46:26 +0100674
Andrzej Pietrasiewicz13443792014-09-09 02:02:13 +0300675 us = usb_gstrings_attach(cdev, uvc_function_strings,
676 ARRAY_SIZE(uvc_en_us_strings));
677 if (IS_ERR(us)) {
678 ret = PTR_ERR(us);
679 goto error;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300680 }
Andrzej Pietrasiewicz13443792014-09-09 02:02:13 +0300681 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
682 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
683 ret = us[UVC_STRING_STREAMING_IDX].id;
684 uvc_streaming_intf_alt0.iInterface = ret;
685 uvc_streaming_intf_alt1.iInterface = ret;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300686
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200687 /* Allocate interface IDs. */
688 if ((ret = usb_interface_id(c, f)) < 0)
689 goto error;
690 uvc_iad.bFirstInterface = ret;
691 uvc_control_intf.bInterfaceNumber = ret;
692 uvc->control_intf = ret;
693
694 if ((ret = usb_interface_id(c, f)) < 0)
695 goto error;
696 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
697 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
698 uvc->streaming_intf = ret;
699
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200700 /* Copy descriptors */
701 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100702 if (IS_ERR(f->fs_descriptors)) {
703 ret = PTR_ERR(f->fs_descriptors);
704 f->fs_descriptors = NULL;
705 goto error;
706 }
707 if (gadget_is_dualspeed(cdev->gadget)) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530708 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100709 if (IS_ERR(f->hs_descriptors)) {
710 ret = PTR_ERR(f->hs_descriptors);
711 f->hs_descriptors = NULL;
712 goto error;
713 }
714 }
715 if (gadget_is_superspeed(c->cdev->gadget)) {
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530716 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
Andrzej Pietrasiewicz6c259552014-12-10 12:34:01 +0100717 if (IS_ERR(f->ss_descriptors)) {
718 ret = PTR_ERR(f->ss_descriptors);
719 f->ss_descriptors = NULL;
720 goto error;
721 }
722 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200723
724 /* Preallocate control endpoint request. */
725 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
726 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
727 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
728 ret = -ENOMEM;
729 goto error;
730 }
731
732 uvc->control_req->buf = uvc->control_buf;
733 uvc->control_req->complete = uvc_function_ep0_complete;
734 uvc->control_req->context = uvc;
735
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300736 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
737 printk(KERN_INFO "v4l2_device_register failed\n");
738 goto error;
739 }
740
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200741 /* Initialise video. */
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300742 ret = uvcg_video_init(&uvc->video);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200743 if (ret < 0)
744 goto error;
745
746 /* Register a V4L2 device. */
747 ret = uvc_register_video(uvc);
748 if (ret < 0) {
749 printk(KERN_INFO "Unable to register video device\n");
750 goto error;
751 }
752
753 return 0;
754
755error:
Hans Verkuilb60f9aa2013-06-12 06:02:38 -0300756 v4l2_device_unregister(&uvc->v4l2_dev);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200757
758 if (uvc->control_ep)
759 uvc->control_ep->driver_data = NULL;
760 if (uvc->video.ep)
761 uvc->video.ep->driver_data = NULL;
762
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200763 if (uvc->control_req)
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200764 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
Andrzej Pietrasiewicze7379852014-08-21 16:54:45 +0200765 kfree(uvc->control_buf);
Sebastian Andrzej Siewior0f9df932012-10-22 22:15:05 +0200766
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200767 usb_free_all_descriptors(f);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200768 return ret;
769}
770
771/* --------------------------------------------------------------------------
772 * USB gadget function
773 */
774
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300775static void uvc_free_inst(struct usb_function_instance *f)
776{
Andrzej Pietrasiewiczbbea6de12014-12-10 12:34:00 +0100777 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300778
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100779 mutex_destroy(&opts->lock);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300780 kfree(opts);
781}
782
783static struct usb_function_instance *uvc_alloc_inst(void)
784{
785 struct f_uvc_opts *opts;
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100786 struct uvc_camera_terminal_descriptor *cd;
787 struct uvc_processing_unit_descriptor *pd;
788 struct uvc_output_terminal_descriptor *od;
789 struct uvc_color_matching_descriptor *md;
790 struct uvc_descriptor_header **ctl_cls;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300791
792 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
793 if (!opts)
794 return ERR_PTR(-ENOMEM);
795 opts->func_inst.free_func_inst = uvc_free_inst;
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100796 mutex_init(&opts->lock);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300797
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100798 cd = &opts->uvc_camera_terminal;
799 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
800 cd->bDescriptorType = USB_DT_CS_INTERFACE;
801 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
802 cd->bTerminalID = 1;
803 cd->wTerminalType = cpu_to_le16(0x0201);
804 cd->bAssocTerminal = 0;
805 cd->iTerminal = 0;
806 cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
807 cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
808 cd->wOcularFocalLength = cpu_to_le16(0);
809 cd->bControlSize = 3;
810 cd->bmControls[0] = 2;
811 cd->bmControls[1] = 0;
812 cd->bmControls[2] = 0;
813
814 pd = &opts->uvc_processing;
815 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
816 pd->bDescriptorType = USB_DT_CS_INTERFACE;
817 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
818 pd->bUnitID = 2;
819 pd->bSourceID = 1;
820 pd->wMaxMultiplier = cpu_to_le16(16*1024);
821 pd->bControlSize = 2;
822 pd->bmControls[0] = 1;
823 pd->bmControls[1] = 0;
824 pd->iProcessing = 0;
825
826 od = &opts->uvc_output_terminal;
827 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
828 od->bDescriptorType = USB_DT_CS_INTERFACE;
829 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
830 od->bTerminalID = 3;
831 od->wTerminalType = cpu_to_le16(0x0101);
832 od->bAssocTerminal = 0;
833 od->bSourceID = 2;
834 od->iTerminal = 0;
835
836 md = &opts->uvc_color_matching;
837 md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
838 md->bDescriptorType = USB_DT_CS_INTERFACE;
839 md->bDescriptorSubType = UVC_VS_COLORFORMAT;
840 md->bColorPrimaries = 1;
841 md->bTransferCharacteristics = 1;
842 md->bMatrixCoefficients = 4;
843
844 /* Prepare fs control class descriptors for configfs-based gadgets */
845 ctl_cls = opts->uvc_fs_control_cls;
846 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
847 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
848 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
849 ctl_cls[3] = (struct uvc_descriptor_header *)od;
850 ctl_cls[4] = NULL; /* NULL-terminate */
851 opts->fs_control =
852 (const struct uvc_descriptor_header * const *)ctl_cls;
853
854 /* Prepare hs control class descriptors for configfs-based gadgets */
855 ctl_cls = opts->uvc_ss_control_cls;
856 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
857 ctl_cls[1] = (struct uvc_descriptor_header *)cd;
858 ctl_cls[2] = (struct uvc_descriptor_header *)pd;
859 ctl_cls[3] = (struct uvc_descriptor_header *)od;
860 ctl_cls[4] = NULL; /* NULL-terminate */
861 opts->ss_control =
862 (const struct uvc_descriptor_header * const *)ctl_cls;
863
864 opts->streaming_interval = 1;
865 opts->streaming_maxpacket = 1024;
866
867 uvcg_attach_configfs(opts);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300868 return &opts->func_inst;
869}
870
871static void uvc_free(struct usb_function *f)
872{
873 struct uvc_device *uvc = to_uvc(f);
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100874 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
875 func_inst);
876 --opts->refcnt;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300877 kfree(uvc);
878}
879
880static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
881{
882 struct usb_composite_dev *cdev = c->cdev;
883 struct uvc_device *uvc = to_uvc(f);
884
885 INFO(cdev, "%s\n", __func__);
886
Hans Verkuildbe98b32015-03-09 13:34:08 -0300887 video_unregister_device(&uvc->vdev);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300888 v4l2_device_unregister(&uvc->v4l2_dev);
889 uvc->control_ep->driver_data = NULL;
890 uvc->video.ep->driver_data = NULL;
891
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300892 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
893 kfree(uvc->control_buf);
894
895 usb_free_all_descriptors(f);
896}
897
Fengguang Wu4a6698b2014-09-16 17:26:46 +0300898static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300899{
900 struct uvc_device *uvc;
901 struct f_uvc_opts *opts;
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100902 struct uvc_descriptor_header **strm_cls;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300903
904 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
905 if (uvc == NULL)
906 return ERR_PTR(-ENOMEM);
907
Hans Verkuild8e96c42015-02-17 05:44:06 -0300908 mutex_init(&uvc->video.mutex);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300909 uvc->state = UVC_STATE_DISCONNECTED;
Andrzej Pietrasiewiczbbea6de12014-12-10 12:34:00 +0100910 opts = fi_to_f_uvc_opts(fi);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300911
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100912 mutex_lock(&opts->lock);
913 if (opts->uvc_fs_streaming_cls) {
914 strm_cls = opts->uvc_fs_streaming_cls;
915 opts->fs_streaming =
916 (const struct uvc_descriptor_header * const *)strm_cls;
917 }
918 if (opts->uvc_hs_streaming_cls) {
919 strm_cls = opts->uvc_hs_streaming_cls;
920 opts->hs_streaming =
921 (const struct uvc_descriptor_header * const *)strm_cls;
922 }
923 if (opts->uvc_ss_streaming_cls) {
924 strm_cls = opts->uvc_ss_streaming_cls;
925 opts->ss_streaming =
926 (const struct uvc_descriptor_header * const *)strm_cls;
927 }
928
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300929 uvc->desc.fs_control = opts->fs_control;
930 uvc->desc.ss_control = opts->ss_control;
931 uvc->desc.fs_streaming = opts->fs_streaming;
932 uvc->desc.hs_streaming = opts->hs_streaming;
933 uvc->desc.ss_streaming = opts->ss_streaming;
Andrzej Pietrasiewicz46919a22014-12-10 12:34:02 +0100934 ++opts->refcnt;
935 mutex_unlock(&opts->lock);
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300936
937 /* Register the function. */
938 uvc->func.name = "uvc";
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300939 uvc->func.bind = uvc_function_bind;
940 uvc->func.unbind = uvc_unbind;
941 uvc->func.get_alt = uvc_function_get_alt;
942 uvc->func.set_alt = uvc_function_set_alt;
943 uvc->func.disable = uvc_function_disable;
944 uvc->func.setup = uvc_function_setup;
945 uvc->func.free_func = uvc_free;
Robert Baldygaf277bf22015-05-04 14:55:14 +0200946 uvc->func.bind_deactivated = true;
Andrzej Pietrasiewicz6d11ed72014-09-09 02:02:10 +0300947
948 return &uvc->func;
949}
950
951DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
952MODULE_LICENSE("GPL");
953MODULE_AUTHOR("Laurent Pinchart");