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> | |
14 | #include <linux/device.h> | |
15 | #include <linux/errno.h> | |
16 | #include <linux/fs.h> | |
17 | #include <linux/list.h> | |
18 | #include <linux/mutex.h> | |
4d2079c1 | 19 | #include <linux/string.h> |
cdda479f LP |
20 | #include <linux/usb/ch9.h> |
21 | #include <linux/usb/gadget.h> | |
22 | #include <linux/usb/video.h> | |
23 | #include <linux/vmalloc.h> | |
24 | #include <linux/wait.h> | |
25 | ||
26 | #include <media/v4l2-dev.h> | |
27 | #include <media/v4l2-event.h> | |
28 | ||
29 | #include "uvc.h" | |
30 | ||
5d9955f8 | 31 | unsigned int uvc_gadget_trace_param; |
cdda479f | 32 | |
fbcaba0e BS |
33 | /*-------------------------------------------------------------------------*/ |
34 | ||
35 | /* module parameters specific to the Video streaming endpoint */ | |
36 | static unsigned streaming_interval = 1; | |
37 | module_param(streaming_interval, uint, S_IRUGO|S_IWUSR); | |
38 | MODULE_PARM_DESC(streaming_interval, "1 - 16"); | |
39 | ||
40 | static unsigned streaming_maxpacket = 1024; | |
41 | module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR); | |
42 | MODULE_PARM_DESC(streaming_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)"); | |
43 | ||
44 | static unsigned streaming_mult; | |
45 | module_param(streaming_mult, uint, S_IRUGO|S_IWUSR); | |
46 | MODULE_PARM_DESC(streaming_mult, "0 - 2 (hs/ss only)"); | |
47 | ||
48 | static unsigned streaming_maxburst; | |
49 | module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR); | |
50 | MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)"); | |
51 | ||
cdda479f LP |
52 | /* -------------------------------------------------------------------------- |
53 | * Function descriptors | |
54 | */ | |
55 | ||
56 | /* string IDs are assigned dynamically */ | |
57 | ||
58 | #define UVC_STRING_ASSOCIATION_IDX 0 | |
59 | #define UVC_STRING_CONTROL_IDX 1 | |
60 | #define UVC_STRING_STREAMING_IDX 2 | |
61 | ||
62 | static struct usb_string uvc_en_us_strings[] = { | |
63 | [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera", | |
64 | [UVC_STRING_CONTROL_IDX].s = "Video Control", | |
65 | [UVC_STRING_STREAMING_IDX].s = "Video Streaming", | |
66 | { } | |
67 | }; | |
68 | ||
69 | static struct usb_gadget_strings uvc_stringtab = { | |
70 | .language = 0x0409, /* en-us */ | |
71 | .strings = uvc_en_us_strings, | |
72 | }; | |
73 | ||
74 | static struct usb_gadget_strings *uvc_function_strings[] = { | |
75 | &uvc_stringtab, | |
76 | NULL, | |
77 | }; | |
78 | ||
79 | #define UVC_INTF_VIDEO_CONTROL 0 | |
80 | #define UVC_INTF_VIDEO_STREAMING 1 | |
81 | ||
57976636 BS |
82 | #define STATUS_BYTECOUNT 16 /* 16 bytes status */ |
83 | ||
cdda479f | 84 | static struct usb_interface_assoc_descriptor uvc_iad __initdata = { |
bbafc0cb | 85 | .bLength = sizeof(uvc_iad), |
cdda479f LP |
86 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
87 | .bFirstInterface = 0, | |
88 | .bInterfaceCount = 2, | |
89 | .bFunctionClass = USB_CLASS_VIDEO, | |
bbafc0cb | 90 | .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, |
cdda479f LP |
91 | .bFunctionProtocol = 0x00, |
92 | .iFunction = 0, | |
93 | }; | |
94 | ||
95 | static struct usb_interface_descriptor uvc_control_intf __initdata = { | |
96 | .bLength = USB_DT_INTERFACE_SIZE, | |
97 | .bDescriptorType = USB_DT_INTERFACE, | |
98 | .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, | |
99 | .bAlternateSetting = 0, | |
100 | .bNumEndpoints = 1, | |
101 | .bInterfaceClass = USB_CLASS_VIDEO, | |
bbafc0cb | 102 | .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, |
cdda479f LP |
103 | .bInterfaceProtocol = 0x00, |
104 | .iInterface = 0, | |
105 | }; | |
106 | ||
fbcaba0e | 107 | static struct usb_endpoint_descriptor uvc_fs_control_ep __initdata = { |
cdda479f LP |
108 | .bLength = USB_DT_ENDPOINT_SIZE, |
109 | .bDescriptorType = USB_DT_ENDPOINT, | |
110 | .bEndpointAddress = USB_DIR_IN, | |
111 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
57976636 | 112 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
cdda479f LP |
113 | .bInterval = 8, |
114 | }; | |
115 | ||
116 | static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = { | |
117 | .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, | |
118 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
119 | .bDescriptorSubType = UVC_EP_INTERRUPT, | |
57976636 | 120 | .wMaxTransferSize = cpu_to_le16(STATUS_BYTECOUNT), |
cdda479f LP |
121 | }; |
122 | ||
123 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = { | |
124 | .bLength = USB_DT_INTERFACE_SIZE, | |
125 | .bDescriptorType = USB_DT_INTERFACE, | |
126 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, | |
127 | .bAlternateSetting = 0, | |
128 | .bNumEndpoints = 0, | |
129 | .bInterfaceClass = USB_CLASS_VIDEO, | |
bbafc0cb | 130 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
cdda479f LP |
131 | .bInterfaceProtocol = 0x00, |
132 | .iInterface = 0, | |
133 | }; | |
134 | ||
135 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = { | |
136 | .bLength = USB_DT_INTERFACE_SIZE, | |
137 | .bDescriptorType = USB_DT_INTERFACE, | |
138 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, | |
139 | .bAlternateSetting = 1, | |
140 | .bNumEndpoints = 1, | |
141 | .bInterfaceClass = USB_CLASS_VIDEO, | |
bbafc0cb | 142 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
cdda479f LP |
143 | .bInterfaceProtocol = 0x00, |
144 | .iInterface = 0, | |
145 | }; | |
146 | ||
fbcaba0e | 147 | static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { |
cdda479f LP |
148 | .bLength = USB_DT_ENDPOINT_SIZE, |
149 | .bDescriptorType = USB_DT_ENDPOINT, | |
150 | .bEndpointAddress = USB_DIR_IN, | |
151 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
152 | .wMaxPacketSize = cpu_to_le16(512), | |
153 | .bInterval = 1, | |
154 | }; | |
155 | ||
fbcaba0e BS |
156 | static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { |
157 | .bLength = USB_DT_ENDPOINT_SIZE, | |
158 | .bDescriptorType = USB_DT_ENDPOINT, | |
159 | .bEndpointAddress = USB_DIR_IN, | |
160 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
161 | .wMaxPacketSize = cpu_to_le16(1024), | |
162 | .bInterval = 1, | |
163 | }; | |
164 | ||
165 | /* super speed support */ | |
166 | static struct usb_endpoint_descriptor uvc_ss_control_ep __initdata = { | |
167 | .bLength = USB_DT_ENDPOINT_SIZE, | |
168 | .bDescriptorType = USB_DT_ENDPOINT, | |
169 | ||
170 | .bEndpointAddress = USB_DIR_IN, | |
171 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
172 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), | |
173 | .bInterval = 8, | |
174 | }; | |
175 | ||
176 | static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = { | |
177 | .bLength = sizeof uvc_ss_control_comp, | |
178 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
179 | ||
180 | /* the following 3 values can be tweaked if necessary */ | |
181 | /* .bMaxBurst = 0, */ | |
182 | /* .bmAttributes = 0, */ | |
183 | .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT), | |
184 | }; | |
185 | ||
186 | static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = { | |
187 | .bLength = USB_DT_ENDPOINT_SIZE, | |
188 | .bDescriptorType = USB_DT_ENDPOINT, | |
189 | ||
190 | .bEndpointAddress = USB_DIR_IN, | |
191 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
192 | .wMaxPacketSize = cpu_to_le16(1024), | |
193 | .bInterval = 4, | |
194 | }; | |
195 | ||
196 | static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { | |
197 | .bLength = sizeof uvc_ss_streaming_comp, | |
198 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
199 | ||
200 | /* the following 3 values can be tweaked if necessary */ | |
201 | .bMaxBurst = 0, | |
202 | .bmAttributes = 0, | |
203 | .wBytesPerInterval = cpu_to_le16(1024), | |
204 | }; | |
205 | ||
cdda479f LP |
206 | static const struct usb_descriptor_header * const uvc_fs_streaming[] = { |
207 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
fbcaba0e | 208 | (struct usb_descriptor_header *) &uvc_fs_streaming_ep, |
cdda479f LP |
209 | NULL, |
210 | }; | |
211 | ||
212 | static const struct usb_descriptor_header * const uvc_hs_streaming[] = { | |
213 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
fbcaba0e BS |
214 | (struct usb_descriptor_header *) &uvc_hs_streaming_ep, |
215 | NULL, | |
216 | }; | |
217 | ||
218 | static const struct usb_descriptor_header * const uvc_ss_streaming[] = { | |
219 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
220 | (struct usb_descriptor_header *) &uvc_ss_streaming_ep, | |
221 | (struct usb_descriptor_header *) &uvc_ss_streaming_comp, | |
cdda479f LP |
222 | NULL, |
223 | }; | |
224 | ||
225 | /* -------------------------------------------------------------------------- | |
226 | * Control requests | |
227 | */ | |
228 | ||
229 | static void | |
230 | uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) | |
231 | { | |
232 | struct uvc_device *uvc = req->context; | |
233 | struct v4l2_event v4l2_event; | |
234 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
235 | ||
236 | if (uvc->event_setup_out) { | |
237 | uvc->event_setup_out = 0; | |
238 | ||
239 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
240 | v4l2_event.type = UVC_EVENT_DATA; | |
241 | uvc_event->data.length = req->actual; | |
242 | memcpy(&uvc_event->data.data, req->buf, req->actual); | |
243 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
244 | } | |
245 | } | |
246 | ||
247 | static int | |
248 | uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | |
249 | { | |
250 | struct uvc_device *uvc = to_uvc(f); | |
251 | struct v4l2_event v4l2_event; | |
252 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
253 | ||
254 | /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", | |
255 | * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), | |
256 | * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); | |
257 | */ | |
258 | ||
259 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { | |
260 | INFO(f->config->cdev, "invalid request type\n"); | |
261 | return -EINVAL; | |
262 | } | |
263 | ||
264 | /* Stall too big requests. */ | |
265 | if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) | |
266 | return -EINVAL; | |
267 | ||
268 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
269 | v4l2_event.type = UVC_EVENT_SETUP; | |
270 | memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); | |
271 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | static int | |
277 | uvc_function_get_alt(struct usb_function *f, unsigned interface) | |
278 | { | |
279 | struct uvc_device *uvc = to_uvc(f); | |
280 | ||
281 | INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); | |
282 | ||
283 | if (interface == uvc->control_intf) | |
284 | return 0; | |
285 | else if (interface != uvc->streaming_intf) | |
286 | return -EINVAL; | |
287 | else | |
288 | return uvc->state == UVC_STATE_STREAMING ? 1 : 0; | |
289 | } | |
290 | ||
291 | static int | |
292 | uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) | |
293 | { | |
294 | struct uvc_device *uvc = to_uvc(f); | |
295 | struct v4l2_event v4l2_event; | |
296 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
fbcaba0e | 297 | int ret; |
cdda479f LP |
298 | |
299 | INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); | |
300 | ||
301 | if (interface == uvc->control_intf) { | |
302 | if (alt) | |
303 | return -EINVAL; | |
304 | ||
305 | if (uvc->state == UVC_STATE_DISCONNECTED) { | |
306 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
307 | v4l2_event.type = UVC_EVENT_CONNECT; | |
308 | uvc_event->speed = f->config->cdev->gadget->speed; | |
309 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
310 | ||
311 | uvc->state = UVC_STATE_CONNECTED; | |
312 | } | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | if (interface != uvc->streaming_intf) | |
318 | return -EINVAL; | |
319 | ||
320 | /* TODO | |
321 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) | |
322 | return alt ? -EINVAL : 0; | |
323 | */ | |
324 | ||
325 | switch (alt) { | |
326 | case 0: | |
327 | if (uvc->state != UVC_STATE_STREAMING) | |
328 | return 0; | |
329 | ||
330 | if (uvc->video.ep) | |
331 | usb_ep_disable(uvc->video.ep); | |
332 | ||
333 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
334 | v4l2_event.type = UVC_EVENT_STREAMOFF; | |
335 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
336 | ||
337 | uvc->state = UVC_STATE_CONNECTED; | |
338 | break; | |
339 | ||
340 | case 1: | |
341 | if (uvc->state != UVC_STATE_CONNECTED) | |
342 | return 0; | |
343 | ||
72c973dd | 344 | if (uvc->video.ep) { |
fbcaba0e BS |
345 | ret = config_ep_by_speed(f->config->cdev->gadget, |
346 | &(uvc->func), uvc->video.ep); | |
347 | if (ret) | |
348 | return ret; | |
72c973dd TB |
349 | usb_ep_enable(uvc->video.ep); |
350 | } | |
cdda479f LP |
351 | |
352 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
353 | v4l2_event.type = UVC_EVENT_STREAMON; | |
354 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
355 | ||
356 | uvc->state = UVC_STATE_STREAMING; | |
357 | break; | |
358 | ||
359 | default: | |
360 | return -EINVAL; | |
361 | } | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
366 | static void | |
367 | uvc_function_disable(struct usb_function *f) | |
368 | { | |
369 | struct uvc_device *uvc = to_uvc(f); | |
370 | struct v4l2_event v4l2_event; | |
371 | ||
372 | INFO(f->config->cdev, "uvc_function_disable\n"); | |
373 | ||
374 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
375 | v4l2_event.type = UVC_EVENT_DISCONNECT; | |
376 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
377 | ||
378 | uvc->state = UVC_STATE_DISCONNECTED; | |
379 | } | |
380 | ||
381 | /* -------------------------------------------------------------------------- | |
382 | * Connection / disconnection | |
383 | */ | |
384 | ||
385 | void | |
386 | uvc_function_connect(struct uvc_device *uvc) | |
387 | { | |
388 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
389 | int ret; | |
390 | ||
391 | if ((ret = usb_function_activate(&uvc->func)) < 0) | |
392 | INFO(cdev, "UVC connect failed with %d\n", ret); | |
393 | } | |
394 | ||
395 | void | |
396 | uvc_function_disconnect(struct uvc_device *uvc) | |
397 | { | |
398 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
399 | int ret; | |
400 | ||
401 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) | |
402 | INFO(cdev, "UVC disconnect failed with %d\n", ret); | |
403 | } | |
404 | ||
405 | /* -------------------------------------------------------------------------- | |
406 | * USB probe and disconnect | |
407 | */ | |
408 | ||
409 | static int | |
410 | uvc_register_video(struct uvc_device *uvc) | |
411 | { | |
412 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
413 | struct video_device *video; | |
414 | ||
415 | /* TODO reference counting. */ | |
416 | video = video_device_alloc(); | |
417 | if (video == NULL) | |
418 | return -ENOMEM; | |
419 | ||
420 | video->parent = &cdev->gadget->dev; | |
cdda479f LP |
421 | video->fops = &uvc_v4l2_fops; |
422 | video->release = video_device_release; | |
4d2079c1 | 423 | strlcpy(video->name, cdev->gadget->name, sizeof(video->name)); |
cdda479f LP |
424 | |
425 | uvc->vdev = video; | |
426 | video_set_drvdata(video, uvc); | |
427 | ||
428 | return video_register_device(video, VFL_TYPE_GRABBER, -1); | |
429 | } | |
430 | ||
431 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ | |
432 | do { \ | |
433 | memcpy(mem, desc, (desc)->bLength); \ | |
434 | *(dst)++ = mem; \ | |
435 | mem += (desc)->bLength; \ | |
436 | } while (0); | |
437 | ||
438 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ | |
439 | do { \ | |
440 | const struct usb_descriptor_header * const *__src; \ | |
441 | for (__src = src; *__src; ++__src) { \ | |
442 | memcpy(mem, *__src, (*__src)->bLength); \ | |
443 | *dst++ = mem; \ | |
444 | mem += (*__src)->bLength; \ | |
445 | } \ | |
446 | } while (0) | |
447 | ||
448 | static struct usb_descriptor_header ** __init | |
449 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) | |
450 | { | |
451 | struct uvc_input_header_descriptor *uvc_streaming_header; | |
452 | struct uvc_header_descriptor *uvc_control_header; | |
fbcaba0e | 453 | const struct uvc_descriptor_header * const *uvc_control_desc; |
cdda479f LP |
454 | const struct uvc_descriptor_header * const *uvc_streaming_cls; |
455 | const struct usb_descriptor_header * const *uvc_streaming_std; | |
456 | const struct usb_descriptor_header * const *src; | |
fbcaba0e | 457 | static struct usb_endpoint_descriptor *uvc_control_ep; |
cdda479f LP |
458 | struct usb_descriptor_header **dst; |
459 | struct usb_descriptor_header **hdr; | |
460 | unsigned int control_size; | |
461 | unsigned int streaming_size; | |
462 | unsigned int n_desc; | |
463 | unsigned int bytes; | |
464 | void *mem; | |
465 | ||
fbcaba0e BS |
466 | switch (speed) { |
467 | case USB_SPEED_SUPER: | |
468 | uvc_control_desc = uvc->desc.ss_control; | |
469 | uvc_streaming_cls = uvc->desc.ss_streaming; | |
470 | uvc_streaming_std = uvc_ss_streaming; | |
471 | uvc_control_ep = &uvc_ss_control_ep; | |
472 | break; | |
473 | ||
474 | case USB_SPEED_HIGH: | |
475 | uvc_control_desc = uvc->desc.fs_control; | |
476 | uvc_streaming_cls = uvc->desc.hs_streaming; | |
477 | uvc_streaming_std = uvc_hs_streaming; | |
478 | uvc_control_ep = &uvc_fs_control_ep; | |
479 | break; | |
480 | ||
481 | case USB_SPEED_FULL: | |
482 | default: | |
483 | uvc_control_desc = uvc->desc.fs_control; | |
484 | uvc_streaming_cls = uvc->desc.fs_streaming; | |
485 | uvc_streaming_std = uvc_fs_streaming; | |
486 | uvc_control_ep = &uvc_fs_control_ep; | |
487 | break; | |
488 | } | |
cdda479f LP |
489 | |
490 | /* Descriptors layout | |
491 | * | |
492 | * uvc_iad | |
493 | * uvc_control_intf | |
494 | * Class-specific UVC control descriptors | |
495 | * uvc_control_ep | |
496 | * uvc_control_cs_ep | |
497 | * uvc_streaming_intf_alt0 | |
498 | * Class-specific UVC streaming descriptors | |
499 | * uvc_{fs|hs}_streaming | |
500 | */ | |
501 | ||
502 | /* Count descriptors and compute their size. */ | |
503 | control_size = 0; | |
504 | streaming_size = 0; | |
505 | bytes = uvc_iad.bLength + uvc_control_intf.bLength | |
fbcaba0e | 506 | + uvc_control_ep->bLength + uvc_control_cs_ep.bLength |
cdda479f | 507 | + uvc_streaming_intf_alt0.bLength; |
cdda479f | 508 | |
fbcaba0e BS |
509 | if (speed == USB_SPEED_SUPER) { |
510 | bytes += uvc_ss_control_comp.bLength; | |
511 | n_desc = 6; | |
512 | } else { | |
513 | n_desc = 5; | |
514 | } | |
515 | ||
516 | for (src = (const struct usb_descriptor_header **)uvc_control_desc; | |
517 | *src; ++src) { | |
cdda479f LP |
518 | control_size += (*src)->bLength; |
519 | bytes += (*src)->bLength; | |
520 | n_desc++; | |
521 | } | |
fbcaba0e BS |
522 | for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; |
523 | *src; ++src) { | |
cdda479f LP |
524 | streaming_size += (*src)->bLength; |
525 | bytes += (*src)->bLength; | |
526 | n_desc++; | |
527 | } | |
528 | for (src = uvc_streaming_std; *src; ++src) { | |
529 | bytes += (*src)->bLength; | |
530 | n_desc++; | |
531 | } | |
532 | ||
533 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); | |
534 | if (mem == NULL) | |
535 | return NULL; | |
536 | ||
537 | hdr = mem; | |
538 | dst = mem; | |
539 | mem += (n_desc + 1) * sizeof(*src); | |
540 | ||
541 | /* Copy the descriptors. */ | |
542 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); | |
543 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); | |
544 | ||
545 | uvc_control_header = mem; | |
546 | UVC_COPY_DESCRIPTORS(mem, dst, | |
fbcaba0e | 547 | (const struct usb_descriptor_header **)uvc_control_desc); |
cdda479f LP |
548 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); |
549 | uvc_control_header->bInCollection = 1; | |
550 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; | |
551 | ||
fbcaba0e BS |
552 | UVC_COPY_DESCRIPTOR(mem, dst, uvc_control_ep); |
553 | if (speed == USB_SPEED_SUPER) | |
554 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); | |
555 | ||
cdda479f LP |
556 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); |
557 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); | |
558 | ||
559 | uvc_streaming_header = mem; | |
560 | UVC_COPY_DESCRIPTORS(mem, dst, | |
561 | (const struct usb_descriptor_header**)uvc_streaming_cls); | |
562 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); | |
fbcaba0e BS |
563 | uvc_streaming_header->bEndpointAddress = |
564 | uvc_fs_streaming_ep.bEndpointAddress; | |
cdda479f LP |
565 | |
566 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); | |
567 | ||
568 | *dst = NULL; | |
569 | return hdr; | |
570 | } | |
571 | ||
572 | static void | |
573 | uvc_function_unbind(struct usb_configuration *c, struct usb_function *f) | |
574 | { | |
575 | struct usb_composite_dev *cdev = c->cdev; | |
576 | struct uvc_device *uvc = to_uvc(f); | |
577 | ||
578 | INFO(cdev, "uvc_function_unbind\n"); | |
579 | ||
0f9df939 SAS |
580 | video_unregister_device(uvc->vdev); |
581 | uvc->control_ep->driver_data = NULL; | |
582 | uvc->video.ep->driver_data = NULL; | |
cdda479f | 583 | |
1616e99d | 584 | uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0; |
0f9df939 SAS |
585 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); |
586 | kfree(uvc->control_buf); | |
cdda479f | 587 | |
10287bae | 588 | usb_free_all_descriptors(f); |
cdda479f LP |
589 | |
590 | kfree(uvc); | |
591 | } | |
592 | ||
593 | static int __init | |
594 | uvc_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); | |
598 | struct usb_ep *ep; | |
599 | int ret = -EINVAL; | |
600 | ||
601 | INFO(cdev, "uvc_function_bind\n"); | |
602 | ||
fbcaba0e BS |
603 | /* sanity check the streaming endpoint module parameters */ |
604 | if (streaming_interval < 1) | |
605 | streaming_interval = 1; | |
606 | if (streaming_interval > 16) | |
607 | streaming_interval = 16; | |
608 | if (streaming_mult > 2) | |
609 | streaming_mult = 2; | |
610 | if (streaming_maxburst > 15) | |
611 | streaming_maxburst = 15; | |
612 | ||
613 | /* | |
614 | * fill in the FS video streaming specific descriptors from the | |
615 | * module parameters | |
616 | */ | |
617 | uvc_fs_streaming_ep.wMaxPacketSize = streaming_maxpacket > 1023 ? | |
618 | 1023 : streaming_maxpacket; | |
619 | uvc_fs_streaming_ep.bInterval = streaming_interval; | |
620 | ||
cdda479f | 621 | /* Allocate endpoints. */ |
fbcaba0e | 622 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_control_ep); |
cdda479f LP |
623 | if (!ep) { |
624 | INFO(cdev, "Unable to allocate control EP\n"); | |
625 | goto error; | |
626 | } | |
627 | uvc->control_ep = ep; | |
628 | ep->driver_data = uvc; | |
629 | ||
fbcaba0e | 630 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); |
cdda479f LP |
631 | if (!ep) { |
632 | INFO(cdev, "Unable to allocate streaming EP\n"); | |
633 | goto error; | |
634 | } | |
635 | uvc->video.ep = ep; | |
636 | ep->driver_data = uvc; | |
637 | ||
638 | /* Allocate interface IDs. */ | |
639 | if ((ret = usb_interface_id(c, f)) < 0) | |
640 | goto error; | |
641 | uvc_iad.bFirstInterface = ret; | |
642 | uvc_control_intf.bInterfaceNumber = ret; | |
643 | uvc->control_intf = ret; | |
644 | ||
645 | if ((ret = usb_interface_id(c, f)) < 0) | |
646 | goto error; | |
647 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; | |
648 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; | |
649 | uvc->streaming_intf = ret; | |
650 | ||
fbcaba0e BS |
651 | /* sanity check the streaming endpoint module parameters */ |
652 | if (streaming_maxpacket > 1024) | |
653 | streaming_maxpacket = 1024; | |
10287bae SAS |
654 | /* |
655 | * Fill in the HS descriptors from the module parameters for the Video | |
656 | * Streaming endpoint. | |
657 | * NOTE: We assume that the user knows what they are doing and won't | |
658 | * give parameters that their UDC doesn't support. | |
659 | */ | |
660 | uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket; | |
661 | uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11; | |
662 | uvc_hs_streaming_ep.bInterval = streaming_interval; | |
663 | uvc_hs_streaming_ep.bEndpointAddress = | |
664 | uvc_fs_streaming_ep.bEndpointAddress; | |
fbcaba0e | 665 | |
10287bae SAS |
666 | /* |
667 | * Fill in the SS descriptors from the module parameters for the Video | |
668 | * Streaming endpoint. | |
669 | * NOTE: We assume that the user knows what they are doing and won't | |
670 | * give parameters that their UDC doesn't support. | |
671 | */ | |
672 | uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket; | |
673 | uvc_ss_streaming_ep.bInterval = streaming_interval; | |
674 | uvc_ss_streaming_comp.bmAttributes = streaming_mult; | |
675 | uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst; | |
676 | uvc_ss_streaming_comp.wBytesPerInterval = | |
677 | streaming_maxpacket * (streaming_mult + 1) * | |
678 | (streaming_maxburst + 1); | |
679 | uvc_ss_streaming_ep.bEndpointAddress = | |
680 | uvc_fs_streaming_ep.bEndpointAddress; | |
fbcaba0e | 681 | |
10287bae SAS |
682 | /* Copy descriptors */ |
683 | f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); | |
684 | if (gadget_is_dualspeed(cdev->gadget)) | |
685 | f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); | |
686 | if (gadget_is_superspeed(c->cdev->gadget)) | |
fbcaba0e | 687 | f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); |
cdda479f LP |
688 | |
689 | /* Preallocate control endpoint request. */ | |
690 | uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); | |
691 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); | |
692 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { | |
693 | ret = -ENOMEM; | |
694 | goto error; | |
695 | } | |
696 | ||
697 | uvc->control_req->buf = uvc->control_buf; | |
698 | uvc->control_req->complete = uvc_function_ep0_complete; | |
699 | uvc->control_req->context = uvc; | |
700 | ||
701 | /* Avoid letting this gadget enumerate until the userspace server is | |
702 | * active. | |
703 | */ | |
704 | if ((ret = usb_function_deactivate(f)) < 0) | |
705 | goto error; | |
706 | ||
707 | /* Initialise video. */ | |
708 | ret = uvc_video_init(&uvc->video); | |
709 | if (ret < 0) | |
710 | goto error; | |
711 | ||
712 | /* Register a V4L2 device. */ | |
713 | ret = uvc_register_video(uvc); | |
714 | if (ret < 0) { | |
715 | printk(KERN_INFO "Unable to register video device\n"); | |
716 | goto error; | |
717 | } | |
718 | ||
719 | return 0; | |
720 | ||
721 | error: | |
0f9df939 SAS |
722 | if (uvc->vdev) |
723 | video_device_release(uvc->vdev); | |
724 | ||
725 | if (uvc->control_ep) | |
726 | uvc->control_ep->driver_data = NULL; | |
727 | if (uvc->video.ep) | |
728 | uvc->video.ep->driver_data = NULL; | |
729 | ||
730 | if (uvc->control_req) { | |
731 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); | |
732 | kfree(uvc->control_buf); | |
733 | } | |
734 | ||
10287bae | 735 | usb_free_all_descriptors(f); |
cdda479f LP |
736 | return ret; |
737 | } | |
738 | ||
739 | /* -------------------------------------------------------------------------- | |
740 | * USB gadget function | |
741 | */ | |
742 | ||
743 | /** | |
744 | * uvc_bind_config - add a UVC function to a configuration | |
745 | * @c: the configuration to support the UVC instance | |
746 | * Context: single threaded during gadget setup | |
747 | * | |
748 | * Returns zero on success, else negative errno. | |
749 | * | |
750 | * Caller must have called @uvc_setup(). Caller is also responsible for | |
751 | * calling @uvc_cleanup() before module unload. | |
752 | */ | |
753 | int __init | |
754 | uvc_bind_config(struct usb_configuration *c, | |
fbcaba0e BS |
755 | const struct uvc_descriptor_header * const *fs_control, |
756 | const struct uvc_descriptor_header * const *ss_control, | |
cdda479f | 757 | const struct uvc_descriptor_header * const *fs_streaming, |
fbcaba0e BS |
758 | const struct uvc_descriptor_header * const *hs_streaming, |
759 | const struct uvc_descriptor_header * const *ss_streaming) | |
cdda479f LP |
760 | { |
761 | struct uvc_device *uvc; | |
762 | int ret = 0; | |
763 | ||
764 | /* TODO Check if the USB device controller supports the required | |
765 | * features. | |
766 | */ | |
767 | if (!gadget_is_dualspeed(c->cdev->gadget)) | |
768 | return -EINVAL; | |
769 | ||
770 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); | |
771 | if (uvc == NULL) | |
772 | return -ENOMEM; | |
773 | ||
774 | uvc->state = UVC_STATE_DISCONNECTED; | |
775 | ||
776 | /* Validate the descriptors. */ | |
fbcaba0e BS |
777 | if (fs_control == NULL || fs_control[0] == NULL || |
778 | fs_control[0]->bDescriptorSubType != UVC_VC_HEADER) | |
779 | goto error; | |
780 | ||
781 | if (ss_control == NULL || ss_control[0] == NULL || | |
782 | ss_control[0]->bDescriptorSubType != UVC_VC_HEADER) | |
cdda479f LP |
783 | goto error; |
784 | ||
785 | if (fs_streaming == NULL || fs_streaming[0] == NULL || | |
fbcaba0e | 786 | fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
cdda479f LP |
787 | goto error; |
788 | ||
789 | if (hs_streaming == NULL || hs_streaming[0] == NULL || | |
fbcaba0e BS |
790 | hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
791 | goto error; | |
792 | ||
793 | if (ss_streaming == NULL || ss_streaming[0] == NULL || | |
794 | ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) | |
cdda479f LP |
795 | goto error; |
796 | ||
fbcaba0e BS |
797 | uvc->desc.fs_control = fs_control; |
798 | uvc->desc.ss_control = ss_control; | |
cdda479f LP |
799 | uvc->desc.fs_streaming = fs_streaming; |
800 | uvc->desc.hs_streaming = hs_streaming; | |
fbcaba0e | 801 | uvc->desc.ss_streaming = ss_streaming; |
cdda479f | 802 | |
1616e99d | 803 | /* Allocate string descriptor numbers. */ |
3de6e63f | 804 | if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) { |
1616e99d SAS |
805 | ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings); |
806 | if (ret) | |
3de6e63f | 807 | goto error; |
1616e99d SAS |
808 | uvc_iad.iFunction = |
809 | uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id; | |
810 | uvc_control_intf.iInterface = | |
811 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id; | |
812 | ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id; | |
3de6e63f BS |
813 | uvc_streaming_intf_alt0.iInterface = ret; |
814 | uvc_streaming_intf_alt1.iInterface = ret; | |
815 | } | |
cdda479f LP |
816 | |
817 | /* Register the function. */ | |
818 | uvc->func.name = "uvc"; | |
819 | uvc->func.strings = uvc_function_strings; | |
820 | uvc->func.bind = uvc_function_bind; | |
821 | uvc->func.unbind = uvc_function_unbind; | |
822 | uvc->func.get_alt = uvc_function_get_alt; | |
823 | uvc->func.set_alt = uvc_function_set_alt; | |
824 | uvc->func.disable = uvc_function_disable; | |
825 | uvc->func.setup = uvc_function_setup; | |
826 | ||
827 | ret = usb_add_function(c, &uvc->func); | |
828 | if (ret) | |
829 | kfree(uvc); | |
830 | ||
28f75f4d | 831 | return ret; |
cdda479f LP |
832 | |
833 | error: | |
834 | kfree(uvc); | |
835 | return ret; | |
836 | } | |
837 | ||
5d9955f8 | 838 | module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR); |
cdda479f LP |
839 | MODULE_PARM_DESC(trace, "Trace level bitmask"); |
840 |