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. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <linux/kernel.h> | |
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> | |
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 LP |
32 | |
33 | /* -------------------------------------------------------------------------- | |
34 | * Function descriptors | |
35 | */ | |
36 | ||
37 | /* string IDs are assigned dynamically */ | |
38 | ||
39 | #define UVC_STRING_ASSOCIATION_IDX 0 | |
40 | #define UVC_STRING_CONTROL_IDX 1 | |
41 | #define UVC_STRING_STREAMING_IDX 2 | |
42 | ||
43 | static struct usb_string uvc_en_us_strings[] = { | |
44 | [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera", | |
45 | [UVC_STRING_CONTROL_IDX].s = "Video Control", | |
46 | [UVC_STRING_STREAMING_IDX].s = "Video Streaming", | |
47 | { } | |
48 | }; | |
49 | ||
50 | static struct usb_gadget_strings uvc_stringtab = { | |
51 | .language = 0x0409, /* en-us */ | |
52 | .strings = uvc_en_us_strings, | |
53 | }; | |
54 | ||
55 | static struct usb_gadget_strings *uvc_function_strings[] = { | |
56 | &uvc_stringtab, | |
57 | NULL, | |
58 | }; | |
59 | ||
60 | #define UVC_INTF_VIDEO_CONTROL 0 | |
61 | #define UVC_INTF_VIDEO_STREAMING 1 | |
62 | ||
63 | static struct usb_interface_assoc_descriptor uvc_iad __initdata = { | |
bbafc0cb | 64 | .bLength = sizeof(uvc_iad), |
cdda479f LP |
65 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
66 | .bFirstInterface = 0, | |
67 | .bInterfaceCount = 2, | |
68 | .bFunctionClass = USB_CLASS_VIDEO, | |
bbafc0cb | 69 | .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, |
cdda479f LP |
70 | .bFunctionProtocol = 0x00, |
71 | .iFunction = 0, | |
72 | }; | |
73 | ||
74 | static struct usb_interface_descriptor uvc_control_intf __initdata = { | |
75 | .bLength = USB_DT_INTERFACE_SIZE, | |
76 | .bDescriptorType = USB_DT_INTERFACE, | |
77 | .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, | |
78 | .bAlternateSetting = 0, | |
79 | .bNumEndpoints = 1, | |
80 | .bInterfaceClass = USB_CLASS_VIDEO, | |
bbafc0cb | 81 | .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, |
cdda479f LP |
82 | .bInterfaceProtocol = 0x00, |
83 | .iInterface = 0, | |
84 | }; | |
85 | ||
86 | static struct usb_endpoint_descriptor uvc_control_ep __initdata = { | |
87 | .bLength = USB_DT_ENDPOINT_SIZE, | |
88 | .bDescriptorType = USB_DT_ENDPOINT, | |
89 | .bEndpointAddress = USB_DIR_IN, | |
90 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
91 | .wMaxPacketSize = cpu_to_le16(16), | |
92 | .bInterval = 8, | |
93 | }; | |
94 | ||
95 | static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = { | |
96 | .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, | |
97 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
98 | .bDescriptorSubType = UVC_EP_INTERRUPT, | |
99 | .wMaxTransferSize = cpu_to_le16(16), | |
100 | }; | |
101 | ||
102 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = { | |
103 | .bLength = USB_DT_INTERFACE_SIZE, | |
104 | .bDescriptorType = USB_DT_INTERFACE, | |
105 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, | |
106 | .bAlternateSetting = 0, | |
107 | .bNumEndpoints = 0, | |
108 | .bInterfaceClass = USB_CLASS_VIDEO, | |
bbafc0cb | 109 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
cdda479f LP |
110 | .bInterfaceProtocol = 0x00, |
111 | .iInterface = 0, | |
112 | }; | |
113 | ||
114 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = { | |
115 | .bLength = USB_DT_INTERFACE_SIZE, | |
116 | .bDescriptorType = USB_DT_INTERFACE, | |
117 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, | |
118 | .bAlternateSetting = 1, | |
119 | .bNumEndpoints = 1, | |
120 | .bInterfaceClass = USB_CLASS_VIDEO, | |
bbafc0cb | 121 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
cdda479f LP |
122 | .bInterfaceProtocol = 0x00, |
123 | .iInterface = 0, | |
124 | }; | |
125 | ||
126 | static struct usb_endpoint_descriptor uvc_streaming_ep = { | |
127 | .bLength = USB_DT_ENDPOINT_SIZE, | |
128 | .bDescriptorType = USB_DT_ENDPOINT, | |
129 | .bEndpointAddress = USB_DIR_IN, | |
130 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
131 | .wMaxPacketSize = cpu_to_le16(512), | |
132 | .bInterval = 1, | |
133 | }; | |
134 | ||
135 | static const struct usb_descriptor_header * const uvc_fs_streaming[] = { | |
136 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
137 | (struct usb_descriptor_header *) &uvc_streaming_ep, | |
138 | NULL, | |
139 | }; | |
140 | ||
141 | static const struct usb_descriptor_header * const uvc_hs_streaming[] = { | |
142 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, | |
143 | (struct usb_descriptor_header *) &uvc_streaming_ep, | |
144 | NULL, | |
145 | }; | |
146 | ||
147 | /* -------------------------------------------------------------------------- | |
148 | * Control requests | |
149 | */ | |
150 | ||
151 | static void | |
152 | uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) | |
153 | { | |
154 | struct uvc_device *uvc = req->context; | |
155 | struct v4l2_event v4l2_event; | |
156 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
157 | ||
158 | if (uvc->event_setup_out) { | |
159 | uvc->event_setup_out = 0; | |
160 | ||
161 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
162 | v4l2_event.type = UVC_EVENT_DATA; | |
163 | uvc_event->data.length = req->actual; | |
164 | memcpy(&uvc_event->data.data, req->buf, req->actual); | |
165 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
166 | } | |
167 | } | |
168 | ||
169 | static int | |
170 | uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | |
171 | { | |
172 | struct uvc_device *uvc = to_uvc(f); | |
173 | struct v4l2_event v4l2_event; | |
174 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
175 | ||
176 | /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", | |
177 | * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), | |
178 | * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); | |
179 | */ | |
180 | ||
181 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { | |
182 | INFO(f->config->cdev, "invalid request type\n"); | |
183 | return -EINVAL; | |
184 | } | |
185 | ||
186 | /* Stall too big requests. */ | |
187 | if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) | |
188 | return -EINVAL; | |
189 | ||
190 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
191 | v4l2_event.type = UVC_EVENT_SETUP; | |
192 | memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); | |
193 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | static int | |
199 | uvc_function_get_alt(struct usb_function *f, unsigned interface) | |
200 | { | |
201 | struct uvc_device *uvc = to_uvc(f); | |
202 | ||
203 | INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); | |
204 | ||
205 | if (interface == uvc->control_intf) | |
206 | return 0; | |
207 | else if (interface != uvc->streaming_intf) | |
208 | return -EINVAL; | |
209 | else | |
210 | return uvc->state == UVC_STATE_STREAMING ? 1 : 0; | |
211 | } | |
212 | ||
213 | static int | |
214 | uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) | |
215 | { | |
216 | struct uvc_device *uvc = to_uvc(f); | |
217 | struct v4l2_event v4l2_event; | |
218 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; | |
219 | ||
220 | INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); | |
221 | ||
222 | if (interface == uvc->control_intf) { | |
223 | if (alt) | |
224 | return -EINVAL; | |
225 | ||
226 | if (uvc->state == UVC_STATE_DISCONNECTED) { | |
227 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
228 | v4l2_event.type = UVC_EVENT_CONNECT; | |
229 | uvc_event->speed = f->config->cdev->gadget->speed; | |
230 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
231 | ||
232 | uvc->state = UVC_STATE_CONNECTED; | |
233 | } | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | if (interface != uvc->streaming_intf) | |
239 | return -EINVAL; | |
240 | ||
241 | /* TODO | |
242 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) | |
243 | return alt ? -EINVAL : 0; | |
244 | */ | |
245 | ||
246 | switch (alt) { | |
247 | case 0: | |
248 | if (uvc->state != UVC_STATE_STREAMING) | |
249 | return 0; | |
250 | ||
251 | if (uvc->video.ep) | |
252 | usb_ep_disable(uvc->video.ep); | |
253 | ||
254 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
255 | v4l2_event.type = UVC_EVENT_STREAMOFF; | |
256 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
257 | ||
258 | uvc->state = UVC_STATE_CONNECTED; | |
259 | break; | |
260 | ||
261 | case 1: | |
262 | if (uvc->state != UVC_STATE_CONNECTED) | |
263 | return 0; | |
264 | ||
72c973dd TB |
265 | if (uvc->video.ep) { |
266 | uvc->video.ep->desc = &uvc_streaming_ep; | |
267 | usb_ep_enable(uvc->video.ep); | |
268 | } | |
cdda479f LP |
269 | |
270 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
271 | v4l2_event.type = UVC_EVENT_STREAMON; | |
272 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
273 | ||
274 | uvc->state = UVC_STATE_STREAMING; | |
275 | break; | |
276 | ||
277 | default: | |
278 | return -EINVAL; | |
279 | } | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
284 | static void | |
285 | uvc_function_disable(struct usb_function *f) | |
286 | { | |
287 | struct uvc_device *uvc = to_uvc(f); | |
288 | struct v4l2_event v4l2_event; | |
289 | ||
290 | INFO(f->config->cdev, "uvc_function_disable\n"); | |
291 | ||
292 | memset(&v4l2_event, 0, sizeof(v4l2_event)); | |
293 | v4l2_event.type = UVC_EVENT_DISCONNECT; | |
294 | v4l2_event_queue(uvc->vdev, &v4l2_event); | |
295 | ||
296 | uvc->state = UVC_STATE_DISCONNECTED; | |
297 | } | |
298 | ||
299 | /* -------------------------------------------------------------------------- | |
300 | * Connection / disconnection | |
301 | */ | |
302 | ||
303 | void | |
304 | uvc_function_connect(struct uvc_device *uvc) | |
305 | { | |
306 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
307 | int ret; | |
308 | ||
309 | if ((ret = usb_function_activate(&uvc->func)) < 0) | |
310 | INFO(cdev, "UVC connect failed with %d\n", ret); | |
311 | } | |
312 | ||
313 | void | |
314 | uvc_function_disconnect(struct uvc_device *uvc) | |
315 | { | |
316 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
317 | int ret; | |
318 | ||
319 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) | |
320 | INFO(cdev, "UVC disconnect failed with %d\n", ret); | |
321 | } | |
322 | ||
323 | /* -------------------------------------------------------------------------- | |
324 | * USB probe and disconnect | |
325 | */ | |
326 | ||
327 | static int | |
328 | uvc_register_video(struct uvc_device *uvc) | |
329 | { | |
330 | struct usb_composite_dev *cdev = uvc->func.config->cdev; | |
331 | struct video_device *video; | |
332 | ||
333 | /* TODO reference counting. */ | |
334 | video = video_device_alloc(); | |
335 | if (video == NULL) | |
336 | return -ENOMEM; | |
337 | ||
338 | video->parent = &cdev->gadget->dev; | |
339 | video->minor = -1; | |
340 | video->fops = &uvc_v4l2_fops; | |
341 | video->release = video_device_release; | |
342 | strncpy(video->name, cdev->gadget->name, sizeof(video->name)); | |
343 | ||
344 | uvc->vdev = video; | |
345 | video_set_drvdata(video, uvc); | |
346 | ||
347 | return video_register_device(video, VFL_TYPE_GRABBER, -1); | |
348 | } | |
349 | ||
350 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ | |
351 | do { \ | |
352 | memcpy(mem, desc, (desc)->bLength); \ | |
353 | *(dst)++ = mem; \ | |
354 | mem += (desc)->bLength; \ | |
355 | } while (0); | |
356 | ||
357 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ | |
358 | do { \ | |
359 | const struct usb_descriptor_header * const *__src; \ | |
360 | for (__src = src; *__src; ++__src) { \ | |
361 | memcpy(mem, *__src, (*__src)->bLength); \ | |
362 | *dst++ = mem; \ | |
363 | mem += (*__src)->bLength; \ | |
364 | } \ | |
365 | } while (0) | |
366 | ||
367 | static struct usb_descriptor_header ** __init | |
368 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) | |
369 | { | |
370 | struct uvc_input_header_descriptor *uvc_streaming_header; | |
371 | struct uvc_header_descriptor *uvc_control_header; | |
372 | const struct uvc_descriptor_header * const *uvc_streaming_cls; | |
373 | const struct usb_descriptor_header * const *uvc_streaming_std; | |
374 | const struct usb_descriptor_header * const *src; | |
375 | struct usb_descriptor_header **dst; | |
376 | struct usb_descriptor_header **hdr; | |
377 | unsigned int control_size; | |
378 | unsigned int streaming_size; | |
379 | unsigned int n_desc; | |
380 | unsigned int bytes; | |
381 | void *mem; | |
382 | ||
383 | uvc_streaming_cls = (speed == USB_SPEED_FULL) | |
384 | ? uvc->desc.fs_streaming : uvc->desc.hs_streaming; | |
385 | uvc_streaming_std = (speed == USB_SPEED_FULL) | |
386 | ? uvc_fs_streaming : uvc_hs_streaming; | |
387 | ||
388 | /* Descriptors layout | |
389 | * | |
390 | * uvc_iad | |
391 | * uvc_control_intf | |
392 | * Class-specific UVC control descriptors | |
393 | * uvc_control_ep | |
394 | * uvc_control_cs_ep | |
395 | * uvc_streaming_intf_alt0 | |
396 | * Class-specific UVC streaming descriptors | |
397 | * uvc_{fs|hs}_streaming | |
398 | */ | |
399 | ||
400 | /* Count descriptors and compute their size. */ | |
401 | control_size = 0; | |
402 | streaming_size = 0; | |
403 | bytes = uvc_iad.bLength + uvc_control_intf.bLength | |
404 | + uvc_control_ep.bLength + uvc_control_cs_ep.bLength | |
405 | + uvc_streaming_intf_alt0.bLength; | |
406 | n_desc = 5; | |
407 | ||
408 | for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) { | |
409 | control_size += (*src)->bLength; | |
410 | bytes += (*src)->bLength; | |
411 | n_desc++; | |
412 | } | |
413 | for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) { | |
414 | streaming_size += (*src)->bLength; | |
415 | bytes += (*src)->bLength; | |
416 | n_desc++; | |
417 | } | |
418 | for (src = uvc_streaming_std; *src; ++src) { | |
419 | bytes += (*src)->bLength; | |
420 | n_desc++; | |
421 | } | |
422 | ||
423 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); | |
424 | if (mem == NULL) | |
425 | return NULL; | |
426 | ||
427 | hdr = mem; | |
428 | dst = mem; | |
429 | mem += (n_desc + 1) * sizeof(*src); | |
430 | ||
431 | /* Copy the descriptors. */ | |
432 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); | |
433 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); | |
434 | ||
435 | uvc_control_header = mem; | |
436 | UVC_COPY_DESCRIPTORS(mem, dst, | |
437 | (const struct usb_descriptor_header**)uvc->desc.control); | |
438 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); | |
439 | uvc_control_header->bInCollection = 1; | |
440 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; | |
441 | ||
442 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); | |
443 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); | |
444 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); | |
445 | ||
446 | uvc_streaming_header = mem; | |
447 | UVC_COPY_DESCRIPTORS(mem, dst, | |
448 | (const struct usb_descriptor_header**)uvc_streaming_cls); | |
449 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); | |
450 | uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress; | |
451 | ||
452 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); | |
453 | ||
454 | *dst = NULL; | |
455 | return hdr; | |
456 | } | |
457 | ||
458 | static void | |
459 | uvc_function_unbind(struct usb_configuration *c, struct usb_function *f) | |
460 | { | |
461 | struct usb_composite_dev *cdev = c->cdev; | |
462 | struct uvc_device *uvc = to_uvc(f); | |
463 | ||
464 | INFO(cdev, "uvc_function_unbind\n"); | |
465 | ||
466 | if (uvc->vdev) { | |
467 | if (uvc->vdev->minor == -1) | |
468 | video_device_release(uvc->vdev); | |
469 | else | |
470 | video_unregister_device(uvc->vdev); | |
471 | uvc->vdev = NULL; | |
472 | } | |
473 | ||
474 | if (uvc->control_ep) | |
475 | uvc->control_ep->driver_data = NULL; | |
476 | if (uvc->video.ep) | |
477 | uvc->video.ep->driver_data = NULL; | |
478 | ||
479 | if (uvc->control_req) { | |
480 | usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); | |
481 | kfree(uvc->control_buf); | |
482 | } | |
483 | ||
484 | kfree(f->descriptors); | |
485 | kfree(f->hs_descriptors); | |
486 | ||
487 | kfree(uvc); | |
488 | } | |
489 | ||
490 | static int __init | |
491 | uvc_function_bind(struct usb_configuration *c, struct usb_function *f) | |
492 | { | |
493 | struct usb_composite_dev *cdev = c->cdev; | |
494 | struct uvc_device *uvc = to_uvc(f); | |
495 | struct usb_ep *ep; | |
496 | int ret = -EINVAL; | |
497 | ||
498 | INFO(cdev, "uvc_function_bind\n"); | |
499 | ||
500 | /* Allocate endpoints. */ | |
501 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); | |
502 | if (!ep) { | |
503 | INFO(cdev, "Unable to allocate control EP\n"); | |
504 | goto error; | |
505 | } | |
506 | uvc->control_ep = ep; | |
507 | ep->driver_data = uvc; | |
508 | ||
509 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep); | |
510 | if (!ep) { | |
511 | INFO(cdev, "Unable to allocate streaming EP\n"); | |
512 | goto error; | |
513 | } | |
514 | uvc->video.ep = ep; | |
515 | ep->driver_data = uvc; | |
516 | ||
517 | /* Allocate interface IDs. */ | |
518 | if ((ret = usb_interface_id(c, f)) < 0) | |
519 | goto error; | |
520 | uvc_iad.bFirstInterface = ret; | |
521 | uvc_control_intf.bInterfaceNumber = ret; | |
522 | uvc->control_intf = ret; | |
523 | ||
524 | if ((ret = usb_interface_id(c, f)) < 0) | |
525 | goto error; | |
526 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; | |
527 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; | |
528 | uvc->streaming_intf = ret; | |
529 | ||
530 | /* Copy descriptors. */ | |
531 | f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); | |
532 | f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); | |
533 | ||
534 | /* Preallocate control endpoint request. */ | |
535 | uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); | |
536 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); | |
537 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { | |
538 | ret = -ENOMEM; | |
539 | goto error; | |
540 | } | |
541 | ||
542 | uvc->control_req->buf = uvc->control_buf; | |
543 | uvc->control_req->complete = uvc_function_ep0_complete; | |
544 | uvc->control_req->context = uvc; | |
545 | ||
546 | /* Avoid letting this gadget enumerate until the userspace server is | |
547 | * active. | |
548 | */ | |
549 | if ((ret = usb_function_deactivate(f)) < 0) | |
550 | goto error; | |
551 | ||
552 | /* Initialise video. */ | |
553 | ret = uvc_video_init(&uvc->video); | |
554 | if (ret < 0) | |
555 | goto error; | |
556 | ||
557 | /* Register a V4L2 device. */ | |
558 | ret = uvc_register_video(uvc); | |
559 | if (ret < 0) { | |
560 | printk(KERN_INFO "Unable to register video device\n"); | |
561 | goto error; | |
562 | } | |
563 | ||
564 | return 0; | |
565 | ||
566 | error: | |
567 | uvc_function_unbind(c, f); | |
568 | return ret; | |
569 | } | |
570 | ||
571 | /* -------------------------------------------------------------------------- | |
572 | * USB gadget function | |
573 | */ | |
574 | ||
575 | /** | |
576 | * uvc_bind_config - add a UVC function to a configuration | |
577 | * @c: the configuration to support the UVC instance | |
578 | * Context: single threaded during gadget setup | |
579 | * | |
580 | * Returns zero on success, else negative errno. | |
581 | * | |
582 | * Caller must have called @uvc_setup(). Caller is also responsible for | |
583 | * calling @uvc_cleanup() before module unload. | |
584 | */ | |
585 | int __init | |
586 | uvc_bind_config(struct usb_configuration *c, | |
587 | const struct uvc_descriptor_header * const *control, | |
588 | const struct uvc_descriptor_header * const *fs_streaming, | |
589 | const struct uvc_descriptor_header * const *hs_streaming) | |
590 | { | |
591 | struct uvc_device *uvc; | |
592 | int ret = 0; | |
593 | ||
594 | /* TODO Check if the USB device controller supports the required | |
595 | * features. | |
596 | */ | |
597 | if (!gadget_is_dualspeed(c->cdev->gadget)) | |
598 | return -EINVAL; | |
599 | ||
600 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); | |
601 | if (uvc == NULL) | |
602 | return -ENOMEM; | |
603 | ||
604 | uvc->state = UVC_STATE_DISCONNECTED; | |
605 | ||
606 | /* Validate the descriptors. */ | |
607 | if (control == NULL || control[0] == NULL || | |
bbafc0cb | 608 | control[0]->bDescriptorSubType != UVC_VC_HEADER) |
cdda479f LP |
609 | goto error; |
610 | ||
611 | if (fs_streaming == NULL || fs_streaming[0] == NULL || | |
bbafc0cb | 612 | fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
cdda479f LP |
613 | goto error; |
614 | ||
615 | if (hs_streaming == NULL || hs_streaming[0] == NULL || | |
bbafc0cb | 616 | hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) |
cdda479f LP |
617 | goto error; |
618 | ||
619 | uvc->desc.control = control; | |
620 | uvc->desc.fs_streaming = fs_streaming; | |
621 | uvc->desc.hs_streaming = hs_streaming; | |
622 | ||
623 | /* Allocate string descriptor numbers. */ | |
624 | if ((ret = usb_string_id(c->cdev)) < 0) | |
625 | goto error; | |
626 | uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret; | |
627 | uvc_iad.iFunction = ret; | |
628 | ||
629 | if ((ret = usb_string_id(c->cdev)) < 0) | |
630 | goto error; | |
631 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret; | |
632 | uvc_control_intf.iInterface = ret; | |
633 | ||
634 | if ((ret = usb_string_id(c->cdev)) < 0) | |
635 | goto error; | |
636 | uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret; | |
637 | uvc_streaming_intf_alt0.iInterface = ret; | |
638 | uvc_streaming_intf_alt1.iInterface = ret; | |
639 | ||
640 | /* Register the function. */ | |
641 | uvc->func.name = "uvc"; | |
642 | uvc->func.strings = uvc_function_strings; | |
643 | uvc->func.bind = uvc_function_bind; | |
644 | uvc->func.unbind = uvc_function_unbind; | |
645 | uvc->func.get_alt = uvc_function_get_alt; | |
646 | uvc->func.set_alt = uvc_function_set_alt; | |
647 | uvc->func.disable = uvc_function_disable; | |
648 | uvc->func.setup = uvc_function_setup; | |
649 | ||
650 | ret = usb_add_function(c, &uvc->func); | |
651 | if (ret) | |
652 | kfree(uvc); | |
653 | ||
28f75f4d | 654 | return ret; |
cdda479f LP |
655 | |
656 | error: | |
657 | kfree(uvc); | |
658 | return ret; | |
659 | } | |
660 | ||
5d9955f8 | 661 | module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR); |
cdda479f LP |
662 | MODULE_PARM_DESC(trace, "Trace level bitmask"); |
663 |