Commit | Line | Data |
---|---|---|
cdda479f LP |
1 | /* |
2 | * uvc_gadget.c -- USB Video Class Gadget driver | |
3 | * | |
4 | * Copyright (C) 2009-2010 | |
5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
cdda479f LP |
11 | */ |
12 | ||
13 | #include <linux/kernel.h> | |
6d11ed76 | 14 | #include <linux/module.h> |
cdda479f LP |
15 | #include <linux/device.h> |
16 | #include <linux/errno.h> | |
17 | #include <linux/fs.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/mutex.h> | |
4d2079c1 | 20 | #include <linux/string.h> |
cdda479f LP |
21 | #include <linux/usb/ch9.h> |
22 | #include <linux/usb/gadget.h> | |
23 | #include <linux/usb/video.h> | |
24 | #include <linux/vmalloc.h> | |
25 | #include <linux/wait.h> | |
26 | ||
27 | #include <media/v4l2-dev.h> | |
28 | #include <media/v4l2-event.h> | |
29 | ||
30 | #include "uvc.h" | |
3a83c16e AP |
31 | #include "uvc_v4l2.h" |
32 | #include "uvc_video.h" | |
6d11ed76 | 33 | #include "u_uvc.h" |
cdda479f | 34 | |
5d9955f8 | 35 | unsigned int uvc_gadget_trace_param; |
fbcaba0e | 36 | |
cdda479f LP |
37 | /* -------------------------------------------------------------------------- |
38 | * Function descriptors | |
39 | */ | |
40 | ||
41 | /* string IDs are assigned dynamically */ | |
42 | ||
43ff05e2 BS |
43 | #define UVC_STRING_CONTROL_IDX 0 |
44 | #define UVC_STRING_STREAMING_IDX 1 | |
cdda479f LP |
45 | |
46 | static struct usb_string uvc_en_us_strings[] = { | |
43ff05e2 | 47 | [UVC_STRING_CONTROL_IDX].s = "UVC Camera", |
cdda479f LP |
48 | [UVC_STRING_STREAMING_IDX].s = "Video Streaming", |
49 | { } | |
50 | }; | |
51 | ||
52 | static struct usb_gadget_strings uvc_stringtab = { | |
53 | .language = 0x0409, /* en-us */ | |
54 | .strings = uvc_en_us_strings, | |
55 | }; | |
56 | ||
57 | static 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 | ||
912ca429 | 65 | #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ |
57976636 | 66 | |
6d11ed76 | 67 | static struct usb_interface_assoc_descriptor uvc_iad = { |
bbafc0cb | 68 | .bLength = sizeof(uvc_iad), |
cdda479f LP |
69 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
70 | .bFirstInterface = 0, | |
71 | .bInterfaceCount = 2, | |
72 | .bFunctionClass = USB_CLASS_VIDEO, | |
bbafc0cb | 73 | .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, |
cdda479f LP |
74 | .bFunctionProtocol = 0x00, |
75 | .iFunction = 0, | |
76 | }; | |
77 | ||
6d11ed76 | 78 | static struct usb_interface_descriptor uvc_control_intf = { |
cdda479f LP |
79 | .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, | |
bbafc0cb | 85 | .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, |
cdda479f LP |
86 | .bInterfaceProtocol = 0x00, |
87 | .iInterface = 0, | |
88 | }; | |
89 | ||
6d11ed76 | 90 | static struct usb_endpoint_descriptor uvc_control_ep = { |
cdda479f LP |
91 | .bLength = USB_DT_ENDPOINT_SIZE, |
92 | .bDescriptorType = USB_DT_ENDPOINT, | |
93 | .bEndpointAddress = USB_DIR_IN, | |
94 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
912ca429 | 95 | .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
cdda479f LP |
96 | .bInterval = 8, |
97 | }; | |
98 | ||
6d11ed76 | 99 | static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = { |
48eee0b4 LP |
100 | .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 | ||
6d11ed76 | 108 | static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = { |
cdda479f LP |
109 | .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, |
110 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
111 | .bDescriptorSubType = UVC_EP_INTERRUPT, | |
912ca429 | 112 | .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
cdda479f LP |
113 | }; |
114 | ||
6d11ed76 | 115 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { |
cdda479f LP |
116 | .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, | |
bbafc0cb | 122 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
cdda479f LP |
123 | .bInterfaceProtocol = 0x00, |
124 | .iInterface = 0, | |
125 | }; | |
126 | ||
6d11ed76 | 127 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { |
cdda479f LP |
128 | .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, | |
bbafc0cb | 134 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
cdda479f LP |
135 | .bInterfaceProtocol = 0x00, |
136 | .iInterface = 0, | |
137 | }; | |
138 | ||
6d11ed76 | 139 | static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { |
cdda479f LP |
140 | .bLength = USB_DT_ENDPOINT_SIZE, |
141 | .bDescriptorType = USB_DT_ENDPOINT, | |
142 | .bEndpointAddress = USB_DIR_IN, | |
609a0532 BS |
143 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
144 | | USB_ENDPOINT_XFER_ISOC, | |
20777dde LP |
145 | /* The wMaxPacketSize and bInterval values will be initialized from |
146 | * module parameters. | |
147 | */ | |
cdda479f LP |
148 | }; |
149 | ||
6d11ed76 | 150 | static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { |
fbcaba0e BS |
151 | .bLength = USB_DT_ENDPOINT_SIZE, |
152 | .bDescriptorType = USB_DT_ENDPOINT, | |
153 | .bEndpointAddress = USB_DIR_IN, | |
609a0532 BS |
154 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
155 | | USB_ENDPOINT_XFER_ISOC, | |
20777dde LP |
156 | /* The wMaxPacketSize and bInterval values will be initialized from |
157 | * module parameters. | |
158 | */ | |
fbcaba0e BS |
159 | }; |
160 | ||
6d11ed76 | 161 | static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { |
ee6a4d87 LP |
162 | .bLength = USB_DT_ENDPOINT_SIZE, |
163 | .bDescriptorType = USB_DT_ENDPOINT, | |
fbcaba0e | 164 | |
ee6a4d87 | 165 | .bEndpointAddress = USB_DIR_IN, |
609a0532 BS |
166 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
167 | | USB_ENDPOINT_XFER_ISOC, | |
20777dde LP |
168 | /* The wMaxPacketSize and bInterval values will be initialized from |
169 | * module parameters. | |
170 | */ | |
fbcaba0e BS |
171 | }; |
172 | ||
6d11ed76 | 173 | static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { |
ee6a4d87 LP |
174 | .bLength = sizeof(uvc_ss_streaming_comp), |
175 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
dbbafe66 LP |
176 | /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be |
177 | * initialized from module parameters. | |
178 | */ | |
fbcaba0e BS |
179 | }; |
180 | ||
cdda479f LP |
181 | static const struct usb_descriptor_header * const uvc_fs_streaming[] = { |
182 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
fbcaba0e | 183 | (struct usb_descriptor_header *) &uvc_fs_streaming_ep, |
cdda479f LP |
184 | NULL, |
185 | }; | |
186 | ||
187 | static const struct usb_descriptor_header * const uvc_hs_streaming[] = { | |
188 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
fbcaba0e BS |
189 | (struct usb_descriptor_header *) &uvc_hs_streaming_ep, |
190 | NULL, | |
191 | }; | |
192 | ||
193 | static 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, | |
cdda479f LP |
197 | NULL, |
198 | }; | |
199 | ||
6d11ed76 AP |
200 | void uvc_set_trace_param(unsigned int trace) |
201 | { | |
202 | uvc_gadget_trace_param = trace; | |
203 | } | |
204 | EXPORT_SYMBOL(uvc_set_trace_param); | |
205 | ||
cdda479f LP |
206 | /* -------------------------------------------------------------------------- |
207 | * Control requests | |
208 | */ | |
209 | ||
210 | static void | |
211 | uvc_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 | ||
228 | static int | |
229 | uvc_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 | ||
26a029f2 LP |
249 | /* 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 | ||
cdda479f LP |
255 | 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 | ||
41837c35 BS |
263 | void 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 | ||
cdda479f LP |
270 | static int |
271 | uvc_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 | |
282 | return uvc->state == UVC_STATE_STREAMING ? 1 : 0; | |
283 | } | |
284 | ||
285 | static int | |
286 | uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) | |
287 | { | |
288 | struct uvc_device *uvc = to_uvc(f); | |
289 | struct v4l2_event v4l2_event; | |
290 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
fbcaba0e | 291 | int ret; |
cdda479f LP |
292 | |
293 | INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); | |
294 | ||
295 | if (interface == uvc->control_intf) { | |
296 | if (alt) | |
297 | return -EINVAL; | |
298 | ||
299 | if (uvc->state == UVC_STATE_DISCONNECTED) { | |
300 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
301 | v4l2_event.type = UVC_EVENT_CONNECT; | |
302 | uvc_event->speed = f->config->cdev->gadget->speed; | |
303 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
304 | ||
305 | uvc->state = UVC_STATE_CONNECTED; | |
306 | } | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | if (interface != uvc->streaming_intf) | |
312 | return -EINVAL; | |
313 | ||
314 | /* TODO | |
315 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) | |
316 | return alt ? -EINVAL : 0; | |
317 | */ | |
318 | ||
319 | switch (alt) { | |
320 | case 0: | |
321 | if (uvc->state != UVC_STATE_STREAMING) | |
322 | return 0; | |
323 | ||
324 | if (uvc->video.ep) | |
325 | usb_ep_disable(uvc->video.ep); | |
326 | ||
327 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
328 | v4l2_event.type = UVC_EVENT_STREAMOFF; | |
329 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
330 | ||
331 | uvc->state = UVC_STATE_CONNECTED; | |
41837c35 | 332 | return 0; |
cdda479f LP |
333 | |
334 | case 1: | |
335 | if (uvc->state != UVC_STATE_CONNECTED) | |
336 | return 0; | |
337 | ||
72c973dd | 338 | if (uvc->video.ep) { |
fbcaba0e BS |
339 | ret = config_ep_by_speed(f->config->cdev->gadget, |
340 | &(uvc->func), uvc->video.ep); | |
341 | if (ret) | |
342 | return ret; | |
72c973dd TB |
343 | usb_ep_enable(uvc->video.ep); |
344 | } | |
cdda479f LP |
345 | |
346 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
347 | v4l2_event.type = UVC_EVENT_STREAMON; | |
348 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
41837c35 | 349 | return USB_GADGET_DELAYED_STATUS; |
cdda479f LP |
350 | |
351 | default: | |
352 | return -EINVAL; | |
353 | } | |
cdda479f LP |
354 | } |
355 | ||
356 | static void | |
357 | uvc_function_disable(struct usb_function *f) | |
358 | { | |
359 | struct uvc_device *uvc = to_uvc(f); | |
360 | struct v4l2_event v4l2_event; | |
361 | ||
362 | INFO(f->config->cdev, "uvc_function_disable\n"); | |
363 | ||
364 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
365 | v4l2_event.type = UVC_EVENT_DISCONNECT; | |
366 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
367 | ||
368 | uvc->state = UVC_STATE_DISCONNECTED; | |
369 | } | |
370 | ||
371 | /* -------------------------------------------------------------------------- | |
372 | * Connection / disconnection | |
373 | */ | |
374 | ||
375 | void | |
376 | uvc_function_connect(struct uvc_device *uvc) | |
377 | { | |
378 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
379 | int ret; | |
380 | ||
381 | if ((ret = usb_function_activate(&uvc->func)) < 0) | |
382 | INFO(cdev, "UVC connect failed with %d\n", ret); | |
383 | } | |
384 | ||
385 | void | |
386 | uvc_function_disconnect(struct uvc_device *uvc) | |
387 | { | |
388 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
389 | int ret; | |
390 | ||
391 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) | |
392 | INFO(cdev, "UVC disconnect failed with %d\n", ret); | |
393 | } | |
394 | ||
395 | /* -------------------------------------------------------------------------- | |
396 | * USB probe and disconnect | |
397 | */ | |
398 | ||
399 | static int | |
400 | uvc_register_video(struct uvc_device *uvc) | |
401 | { | |
402 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
403 | struct video_device *video; | |
404 | ||
405 | /* TODO reference counting. */ | |
406 | video = video_device_alloc(); | |
407 | if (video == NULL) | |
408 | return -ENOMEM; | |
409 | ||
b60f9aa1 | 410 | video->v4l2_dev = &uvc->v4l2_dev; |
cdda479f | 411 | video->fops = &uvc_v4l2_fops; |
a1d27a4b | 412 | video->ioctl_ops = &uvc_v4l2_ioctl_ops; |
cdda479f | 413 | video->release = video_device_release; |
a1d27a4b | 414 | video->vfl_dir = VFL_DIR_TX; |
4d2079c1 | 415 | strlcpy(video->name, cdev->gadget->name, sizeof(video->name)); |
cdda479f LP |
416 | |
417 | uvc->vdev = video; | |
418 | video_set_drvdata(video, uvc); | |
419 | ||
420 | return video_register_device(video, VFL_TYPE_GRABBER, -1); | |
421 | } | |
422 | ||
423 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ | |
424 | do { \ | |
425 | memcpy(mem, desc, (desc)->bLength); \ | |
426 | *(dst)++ = mem; \ | |
427 | mem += (desc)->bLength; \ | |
428 | } while (0); | |
429 | ||
430 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ | |
431 | do { \ | |
432 | const struct usb_descriptor_header * const *__src; \ | |
433 | for (__src = src; *__src; ++__src) { \ | |
434 | memcpy(mem, *__src, (*__src)->bLength); \ | |
435 | *dst++ = mem; \ | |
436 | mem += (*__src)->bLength; \ | |
437 | } \ | |
438 | } while (0) | |
439 | ||
6d11ed76 | 440 | static struct usb_descriptor_header ** |
cdda479f LP |
441 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) |
442 | { | |
443 | struct uvc_input_header_descriptor *uvc_streaming_header; | |
444 | struct uvc_header_descriptor *uvc_control_header; | |
fbcaba0e | 445 | const struct uvc_descriptor_header * const *uvc_control_desc; |
cdda479f LP |
446 | const struct uvc_descriptor_header * const *uvc_streaming_cls; |
447 | const struct usb_descriptor_header * const *uvc_streaming_std; | |
448 | const struct usb_descriptor_header * const *src; | |
449 | struct usb_descriptor_header **dst; | |
450 | struct usb_descriptor_header **hdr; | |
451 | unsigned int control_size; | |
452 | unsigned int streaming_size; | |
453 | unsigned int n_desc; | |
454 | unsigned int bytes; | |
455 | void *mem; | |
456 | ||
fbcaba0e BS |
457 | switch (speed) { |
458 | case USB_SPEED_SUPER: | |
459 | uvc_control_desc = uvc->desc.ss_control; | |
460 | uvc_streaming_cls = uvc->desc.ss_streaming; | |
461 | uvc_streaming_std = uvc_ss_streaming; | |
fbcaba0e BS |
462 | break; |
463 | ||
464 | case USB_SPEED_HIGH: | |
465 | uvc_control_desc = uvc->desc.fs_control; | |
466 | uvc_streaming_cls = uvc->desc.hs_streaming; | |
467 | uvc_streaming_std = uvc_hs_streaming; | |
fbcaba0e BS |
468 | break; |
469 | ||
470 | case USB_SPEED_FULL: | |
471 | default: | |
472 | uvc_control_desc = uvc->desc.fs_control; | |
473 | uvc_streaming_cls = uvc->desc.fs_streaming; | |
474 | uvc_streaming_std = uvc_fs_streaming; | |
fbcaba0e BS |
475 | break; |
476 | } | |
cdda479f LP |
477 | |
478 | /* Descriptors layout | |
479 | * | |
480 | * uvc_iad | |
481 | * uvc_control_intf | |
482 | * Class-specific UVC control descriptors | |
483 | * uvc_control_ep | |
484 | * uvc_control_cs_ep | |
48eee0b4 | 485 | * uvc_ss_control_comp (for SS only) |
cdda479f LP |
486 | * uvc_streaming_intf_alt0 |
487 | * Class-specific UVC streaming descriptors | |
488 | * uvc_{fs|hs}_streaming | |
489 | */ | |
490 | ||
491 | /* Count descriptors and compute their size. */ | |
492 | control_size = 0; | |
493 | streaming_size = 0; | |
494 | bytes = uvc_iad.bLength + uvc_control_intf.bLength | |
48eee0b4 | 495 | + uvc_control_ep.bLength + uvc_control_cs_ep.bLength |
cdda479f | 496 | + uvc_streaming_intf_alt0.bLength; |
cdda479f | 497 | |
fbcaba0e BS |
498 | if (speed == USB_SPEED_SUPER) { |
499 | bytes += uvc_ss_control_comp.bLength; | |
500 | n_desc = 6; | |
501 | } else { | |
502 | n_desc = 5; | |
503 | } | |
504 | ||
505 | for (src = (const struct usb_descriptor_header **)uvc_control_desc; | |
ee6a4d87 | 506 | *src; ++src) { |
cdda479f LP |
507 | control_size += (*src)->bLength; |
508 | bytes += (*src)->bLength; | |
509 | n_desc++; | |
510 | } | |
fbcaba0e | 511 | for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; |
ee6a4d87 | 512 | *src; ++src) { |
cdda479f LP |
513 | streaming_size += (*src)->bLength; |
514 | bytes += (*src)->bLength; | |
515 | n_desc++; | |
516 | } | |
517 | for (src = uvc_streaming_std; *src; ++src) { | |
518 | bytes += (*src)->bLength; | |
519 | n_desc++; | |
520 | } | |
521 | ||
522 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); | |
523 | if (mem == NULL) | |
524 | return NULL; | |
525 | ||
526 | hdr = mem; | |
527 | dst = mem; | |
528 | mem += (n_desc + 1) * sizeof(*src); | |
529 | ||
530 | /* Copy the descriptors. */ | |
531 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); | |
532 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); | |
533 | ||
534 | uvc_control_header = mem; | |
535 | UVC_COPY_DESCRIPTORS(mem, dst, | |
fbcaba0e | 536 | (const struct usb_descriptor_header **)uvc_control_desc); |
cdda479f LP |
537 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); |
538 | uvc_control_header->bInCollection = 1; | |
539 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; | |
540 | ||
48eee0b4 | 541 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); |
fbcaba0e BS |
542 | if (speed == USB_SPEED_SUPER) |
543 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); | |
544 | ||
cdda479f LP |
545 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); |
546 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); | |
547 | ||
548 | uvc_streaming_header = mem; | |
549 | UVC_COPY_DESCRIPTORS(mem, dst, | |
550 | (const struct usb_descriptor_header**)uvc_streaming_cls); | |
551 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); | |
0485ec0d | 552 | uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; |
cdda479f LP |
553 | |
554 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); | |
555 | ||
556 | *dst = NULL; | |
557 | return hdr; | |
558 | } | |
559 | ||
6d11ed76 | 560 | static int |
cdda479f LP |
561 | uvc_function_bind(struct usb_configuration *c, struct usb_function *f) |
562 | { | |
563 | struct usb_composite_dev *cdev = c->cdev; | |
564 | struct uvc_device *uvc = to_uvc(f); | |
13443799 | 565 | struct usb_string *us; |
20777dde LP |
566 | unsigned int max_packet_mult; |
567 | unsigned int max_packet_size; | |
cdda479f | 568 | struct usb_ep *ep; |
6d11ed76 | 569 | struct f_uvc_opts *opts; |
cdda479f LP |
570 | int ret = -EINVAL; |
571 | ||
572 | INFO(cdev, "uvc_function_bind\n"); | |
573 | ||
6d11ed76 AP |
574 | opts = to_f_uvc_opts(f->fi); |
575 | /* Sanity check the streaming endpoint module parameters. | |
576 | */ | |
577 | opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); | |
578 | opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); | |
579 | opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); | |
580 | ||
581 | /* Fill in the FS/HS/SS Video Streaming specific descriptors from the | |
582 | * module parameters. | |
583 | * | |
584 | * NOTE: We assume that the user knows what they are doing and won't | |
585 | * give parameters that their UDC doesn't support. | |
586 | */ | |
587 | if (opts->streaming_maxpacket <= 1024) { | |
588 | max_packet_mult = 1; | |
589 | max_packet_size = opts->streaming_maxpacket; | |
590 | } else if (opts->streaming_maxpacket <= 2048) { | |
591 | max_packet_mult = 2; | |
592 | max_packet_size = opts->streaming_maxpacket / 2; | |
593 | } else { | |
594 | max_packet_mult = 3; | |
595 | max_packet_size = opts->streaming_maxpacket / 3; | |
596 | } | |
597 | ||
598 | uvc_fs_streaming_ep.wMaxPacketSize = | |
599 | min(opts->streaming_maxpacket, 1023U); | |
600 | uvc_fs_streaming_ep.bInterval = opts->streaming_interval; | |
601 | ||
602 | uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size; | |
603 | uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11); | |
604 | uvc_hs_streaming_ep.bInterval = opts->streaming_interval; | |
605 | ||
606 | uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size; | |
607 | uvc_ss_streaming_ep.bInterval = opts->streaming_interval; | |
608 | uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; | |
609 | uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; | |
610 | uvc_ss_streaming_comp.wBytesPerInterval = | |
611 | max_packet_size * max_packet_mult * opts->streaming_maxburst; | |
20777dde | 612 | |
cdda479f | 613 | /* Allocate endpoints. */ |
48eee0b4 | 614 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); |
cdda479f LP |
615 | if (!ep) { |
616 | INFO(cdev, "Unable to allocate control EP\n"); | |
617 | goto error; | |
618 | } | |
619 | uvc->control_ep = ep; | |
620 | ep->driver_data = uvc; | |
621 | ||
0485ec0d LP |
622 | if (gadget_is_superspeed(c->cdev->gadget)) |
623 | ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, | |
624 | &uvc_ss_streaming_comp); | |
625 | else if (gadget_is_dualspeed(cdev->gadget)) | |
626 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); | |
627 | else | |
628 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); | |
629 | ||
cdda479f LP |
630 | if (!ep) { |
631 | INFO(cdev, "Unable to allocate streaming EP\n"); | |
632 | goto error; | |
633 | } | |
634 | uvc->video.ep = ep; | |
635 | ep->driver_data = uvc; | |
636 | ||
0485ec0d LP |
637 | uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
638 | uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; | |
639 | uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; | |
20777dde | 640 | |
13443799 AP |
641 | us = usb_gstrings_attach(cdev, uvc_function_strings, |
642 | ARRAY_SIZE(uvc_en_us_strings)); | |
643 | if (IS_ERR(us)) { | |
644 | ret = PTR_ERR(us); | |
645 | goto error; | |
6d11ed76 | 646 | } |
13443799 AP |
647 | uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id; |
648 | uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id; | |
649 | ret = us[UVC_STRING_STREAMING_IDX].id; | |
650 | uvc_streaming_intf_alt0.iInterface = ret; | |
651 | uvc_streaming_intf_alt1.iInterface = ret; | |
6d11ed76 | 652 | |
cdda479f LP |
653 | /* Allocate interface IDs. */ |
654 | if ((ret = usb_interface_id(c, f)) < 0) | |
655 | goto error; | |
656 | uvc_iad.bFirstInterface = ret; | |
657 | uvc_control_intf.bInterfaceNumber = ret; | |
658 | uvc->control_intf = ret; | |
659 | ||
660 | if ((ret = usb_interface_id(c, f)) < 0) | |
661 | goto error; | |
662 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; | |
663 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; | |
664 | uvc->streaming_intf = ret; | |
665 | ||
10287bae SAS |
666 | /* Copy descriptors */ |
667 | f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); | |
668 | if (gadget_is_dualspeed(cdev->gadget)) | |
669 | f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); | |
670 | if (gadget_is_superspeed(c->cdev->gadget)) | |
fbcaba0e | 671 | f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); |
cdda479f LP |
672 | |
673 | /* Preallocate control endpoint request. */ | |
674 | uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); | |
675 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); | |
676 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { | |
677 | ret = -ENOMEM; | |
678 | goto error; | |
679 | } | |
680 | ||
681 | uvc->control_req->buf = uvc->control_buf; | |
682 | uvc->control_req->complete = uvc_function_ep0_complete; | |
683 | uvc->control_req->context = uvc; | |
684 | ||
685 | /* Avoid letting this gadget enumerate until the userspace server is | |
686 | * active. | |
687 | */ | |
688 | if ((ret = usb_function_deactivate(f)) < 0) | |
689 | goto error; | |
690 | ||
b60f9aa1 HV |
691 | if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { |
692 | printk(KERN_INFO "v4l2_device_register failed\n"); | |
693 | goto error; | |
694 | } | |
695 | ||
cdda479f | 696 | /* Initialise video. */ |
7ea95b11 | 697 | ret = uvcg_video_init(&uvc->video); |
cdda479f LP |
698 | if (ret < 0) |
699 | goto error; | |
700 | ||
701 | /* Register a V4L2 device. */ | |
702 | ret = uvc_register_video(uvc); | |
703 | if (ret < 0) { | |
704 | printk(KERN_INFO "Unable to register video device\n"); | |
705 | goto error; | |
706 | } | |
707 | ||
708 | return 0; | |
709 | ||
710 | error: | |
b60f9aa1 | 711 | v4l2_device_unregister(&uvc->v4l2_dev); |
0f9df939 SAS |
712 | if (uvc->vdev) |
713 | video_device_release(uvc->vdev); | |
714 | ||
715 | if (uvc->control_ep) | |
716 | uvc->control_ep->driver_data = NULL; | |
717 | if (uvc->video.ep) | |
718 | uvc->video.ep->driver_data = NULL; | |
719 | ||
e7379857 | 720 | if (uvc->control_req) |
0f9df939 | 721 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
e7379857 | 722 | kfree(uvc->control_buf); |
0f9df939 | 723 | |
10287bae | 724 | usb_free_all_descriptors(f); |
cdda479f LP |
725 | return ret; |
726 | } | |
727 | ||
728 | /* -------------------------------------------------------------------------- | |
729 | * USB gadget function | |
730 | */ | |
731 | ||
6d11ed76 AP |
732 | static void uvc_free_inst(struct usb_function_instance *f) |
733 | { | |
734 | struct f_uvc_opts *opts = to_f_uvc_opts(f); | |
735 | ||
736 | kfree(opts); | |
737 | } | |
738 | ||
739 | static struct usb_function_instance *uvc_alloc_inst(void) | |
740 | { | |
741 | struct f_uvc_opts *opts; | |
742 | ||
743 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | |
744 | if (!opts) | |
745 | return ERR_PTR(-ENOMEM); | |
746 | opts->func_inst.free_func_inst = uvc_free_inst; | |
747 | ||
748 | return &opts->func_inst; | |
749 | } | |
750 | ||
751 | static void uvc_free(struct usb_function *f) | |
752 | { | |
753 | struct uvc_device *uvc = to_uvc(f); | |
754 | ||
755 | kfree(uvc); | |
756 | } | |
757 | ||
758 | static void uvc_unbind(struct usb_configuration *c, struct usb_function *f) | |
759 | { | |
760 | struct usb_composite_dev *cdev = c->cdev; | |
761 | struct uvc_device *uvc = to_uvc(f); | |
762 | ||
763 | INFO(cdev, "%s\n", __func__); | |
764 | ||
765 | video_unregister_device(uvc->vdev); | |
766 | v4l2_device_unregister(&uvc->v4l2_dev); | |
767 | uvc->control_ep->driver_data = NULL; | |
768 | uvc->video.ep->driver_data = NULL; | |
769 | ||
6d11ed76 AP |
770 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
771 | kfree(uvc->control_buf); | |
772 | ||
773 | usb_free_all_descriptors(f); | |
774 | } | |
775 | ||
776 | struct usb_function *uvc_alloc(struct usb_function_instance *fi) | |
777 | { | |
778 | struct uvc_device *uvc; | |
779 | struct f_uvc_opts *opts; | |
780 | ||
781 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); | |
782 | if (uvc == NULL) | |
783 | return ERR_PTR(-ENOMEM); | |
784 | ||
785 | uvc->state = UVC_STATE_DISCONNECTED; | |
786 | opts = to_f_uvc_opts(fi); | |
787 | ||
788 | uvc->desc.fs_control = opts->fs_control; | |
789 | uvc->desc.ss_control = opts->ss_control; | |
790 | uvc->desc.fs_streaming = opts->fs_streaming; | |
791 | uvc->desc.hs_streaming = opts->hs_streaming; | |
792 | uvc->desc.ss_streaming = opts->ss_streaming; | |
793 | ||
794 | /* Register the function. */ | |
795 | uvc->func.name = "uvc"; | |
6d11ed76 AP |
796 | uvc->func.bind = uvc_function_bind; |
797 | uvc->func.unbind = uvc_unbind; | |
798 | uvc->func.get_alt = uvc_function_get_alt; | |
799 | uvc->func.set_alt = uvc_function_set_alt; | |
800 | uvc->func.disable = uvc_function_disable; | |
801 | uvc->func.setup = uvc_function_setup; | |
802 | uvc->func.free_func = uvc_free; | |
803 | ||
804 | return &uvc->func; | |
805 | } | |
806 | ||
807 | DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); | |
808 | MODULE_LICENSE("GPL"); | |
809 | MODULE_AUTHOR("Laurent Pinchart"); |