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 #include <linux/kernel.h> 14 #include <linux/module.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/string.h> 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 "u_uvc.h" 31 #include "uvc.h" 32 #include "uvc_configfs.h" 33 #include "uvc_v4l2.h" 34 #include "uvc_video.h" 35 36 unsigned int uvc_gadget_trace_param; 37 38 /* -------------------------------------------------------------------------- 39 * Function descriptors 40 */ 41 42 /* string IDs are assigned dynamically */ 43 44 #define UVC_STRING_CONTROL_IDX 0 45 #define UVC_STRING_STREAMING_IDX 1 46 47 static struct usb_string uvc_en_us_strings[] = { 48 [UVC_STRING_CONTROL_IDX].s = "UVC Camera", 49 [UVC_STRING_STREAMING_IDX].s = "Video Streaming", 50 { } 51 }; 52 53 static struct usb_gadget_strings uvc_stringtab = { 54 .language = 0x0409, /* en-us */ 55 .strings = uvc_en_us_strings, 56 }; 57 58 static 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 66 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ 67 68 static struct usb_interface_assoc_descriptor uvc_iad = { 69 .bLength = sizeof(uvc_iad), 70 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 71 .bFirstInterface = 0, 72 .bInterfaceCount = 2, 73 .bFunctionClass = USB_CLASS_VIDEO, 74 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, 75 .bFunctionProtocol = 0x00, 76 .iFunction = 0, 77 }; 78 79 static struct usb_interface_descriptor uvc_control_intf = { 80 .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, 86 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, 87 .bInterfaceProtocol = 0x00, 88 .iInterface = 0, 89 }; 90 91 static struct usb_endpoint_descriptor uvc_control_ep = { 92 .bLength = USB_DT_ENDPOINT_SIZE, 93 .bDescriptorType = USB_DT_ENDPOINT, 94 .bEndpointAddress = USB_DIR_IN, 95 .bmAttributes = USB_ENDPOINT_XFER_INT, 96 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 97 .bInterval = 8, 98 }; 99 100 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = { 101 .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 109 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = { 110 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, 111 .bDescriptorType = USB_DT_CS_ENDPOINT, 112 .bDescriptorSubType = UVC_EP_INTERRUPT, 113 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 114 }; 115 116 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { 117 .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, 123 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 124 .bInterfaceProtocol = 0x00, 125 .iInterface = 0, 126 }; 127 128 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { 129 .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, 135 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 136 .bInterfaceProtocol = 0x00, 137 .iInterface = 0, 138 }; 139 140 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { 141 .bLength = USB_DT_ENDPOINT_SIZE, 142 .bDescriptorType = USB_DT_ENDPOINT, 143 .bEndpointAddress = USB_DIR_IN, 144 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 145 | USB_ENDPOINT_XFER_ISOC, 146 /* The wMaxPacketSize and bInterval values will be initialized from 147 * module parameters. 148 */ 149 }; 150 151 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { 152 .bLength = USB_DT_ENDPOINT_SIZE, 153 .bDescriptorType = USB_DT_ENDPOINT, 154 .bEndpointAddress = USB_DIR_IN, 155 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 156 | USB_ENDPOINT_XFER_ISOC, 157 /* The wMaxPacketSize and bInterval values will be initialized from 158 * module parameters. 159 */ 160 }; 161 162 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { 163 .bLength = USB_DT_ENDPOINT_SIZE, 164 .bDescriptorType = USB_DT_ENDPOINT, 165 166 .bEndpointAddress = USB_DIR_IN, 167 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 168 | USB_ENDPOINT_XFER_ISOC, 169 /* The wMaxPacketSize and bInterval values will be initialized from 170 * module parameters. 171 */ 172 }; 173 174 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { 175 .bLength = sizeof(uvc_ss_streaming_comp), 176 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 177 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be 178 * initialized from module parameters. 179 */ 180 }; 181 182 static const struct usb_descriptor_header * const uvc_fs_streaming[] = { 183 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 184 (struct usb_descriptor_header *) &uvc_fs_streaming_ep, 185 NULL, 186 }; 187 188 static const struct usb_descriptor_header * const uvc_hs_streaming[] = { 189 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 190 (struct usb_descriptor_header *) &uvc_hs_streaming_ep, 191 NULL, 192 }; 193 194 static 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, 198 NULL, 199 }; 200 201 void uvc_set_trace_param(unsigned int trace) 202 { 203 uvc_gadget_trace_param = trace; 204 } 205 EXPORT_SYMBOL(uvc_set_trace_param); 206 207 /* -------------------------------------------------------------------------- 208 * Control requests 209 */ 210 211 static void 212 uvc_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); 225 v4l2_event_queue(uvc->vdev, &v4l2_event); 226 } 227 } 228 229 static int 230 uvc_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 250 /* 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 256 memset(&v4l2_event, 0, sizeof(v4l2_event)); 257 v4l2_event.type = UVC_EVENT_SETUP; 258 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); 259 v4l2_event_queue(uvc->vdev, &v4l2_event); 260 261 return 0; 262 } 263 264 void 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 271 static int 272 uvc_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 283 return uvc->video.ep->driver_data ? 1 : 0; 284 } 285 286 static int 287 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) 288 { 289 struct uvc_device *uvc = to_uvc(f); 290 struct usb_composite_dev *cdev = f->config->cdev; 291 struct v4l2_event v4l2_event; 292 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 293 int ret; 294 295 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); 296 297 if (interface == uvc->control_intf) { 298 if (alt) 299 return -EINVAL; 300 301 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 314 if (uvc->state == UVC_STATE_DISCONNECTED) { 315 memset(&v4l2_event, 0, sizeof(v4l2_event)); 316 v4l2_event.type = UVC_EVENT_CONNECT; 317 uvc_event->speed = cdev->gadget->speed; 318 v4l2_event_queue(uvc->vdev, &v4l2_event); 319 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 339 if (uvc->video.ep) { 340 usb_ep_disable(uvc->video.ep); 341 uvc->video.ep->driver_data = NULL; 342 } 343 344 memset(&v4l2_event, 0, sizeof(v4l2_event)); 345 v4l2_event.type = UVC_EVENT_STREAMOFF; 346 v4l2_event_queue(uvc->vdev, &v4l2_event); 347 348 uvc->state = UVC_STATE_CONNECTED; 349 return 0; 350 351 case 1: 352 if (uvc->state != UVC_STATE_CONNECTED) 353 return 0; 354 355 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; 362 } 363 364 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 371 memset(&v4l2_event, 0, sizeof(v4l2_event)); 372 v4l2_event.type = UVC_EVENT_STREAMON; 373 v4l2_event_queue(uvc->vdev, &v4l2_event); 374 return USB_GADGET_DELAYED_STATUS; 375 376 default: 377 return -EINVAL; 378 } 379 } 380 381 static void 382 uvc_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; 391 v4l2_event_queue(uvc->vdev, &v4l2_event); 392 393 uvc->state = UVC_STATE_DISCONNECTED; 394 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 } 404 } 405 406 /* -------------------------------------------------------------------------- 407 * Connection / disconnection 408 */ 409 410 void 411 uvc_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 420 void 421 uvc_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 434 static int 435 uvc_register_video(struct uvc_device *uvc) 436 { 437 struct usb_composite_dev *cdev = uvc->func.config->cdev; 438 struct video_device *video; 439 440 /* TODO reference counting. */ 441 video = video_device_alloc(); 442 if (video == NULL) 443 return -ENOMEM; 444 445 video->v4l2_dev = &uvc->v4l2_dev; 446 video->fops = &uvc_v4l2_fops; 447 video->ioctl_ops = &uvc_v4l2_ioctl_ops; 448 video->release = video_device_release; 449 video->vfl_dir = VFL_DIR_TX; 450 strlcpy(video->name, cdev->gadget->name, sizeof(video->name)); 451 452 uvc->vdev = video; 453 video_set_drvdata(video, uvc); 454 455 return video_register_device(video, VFL_TYPE_GRABBER, -1); 456 } 457 458 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ 459 do { \ 460 memcpy(mem, desc, (desc)->bLength); \ 461 *(dst)++ = mem; \ 462 mem += (desc)->bLength; \ 463 } while (0); 464 465 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ 466 do { \ 467 const struct usb_descriptor_header * const *__src; \ 468 for (__src = src; *__src; ++__src) { \ 469 memcpy(mem, *__src, (*__src)->bLength); \ 470 *dst++ = mem; \ 471 mem += (*__src)->bLength; \ 472 } \ 473 } while (0) 474 475 static struct usb_descriptor_header ** 476 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) 477 { 478 struct uvc_input_header_descriptor *uvc_streaming_header; 479 struct uvc_header_descriptor *uvc_control_header; 480 const struct uvc_descriptor_header * const *uvc_control_desc; 481 const struct uvc_descriptor_header * const *uvc_streaming_cls; 482 const struct usb_descriptor_header * const *uvc_streaming_std; 483 const struct usb_descriptor_header * const *src; 484 struct usb_descriptor_header **dst; 485 struct usb_descriptor_header **hdr; 486 unsigned int control_size; 487 unsigned int streaming_size; 488 unsigned int n_desc; 489 unsigned int bytes; 490 void *mem; 491 492 switch (speed) { 493 case USB_SPEED_SUPER: 494 uvc_control_desc = uvc->desc.ss_control; 495 uvc_streaming_cls = uvc->desc.ss_streaming; 496 uvc_streaming_std = uvc_ss_streaming; 497 break; 498 499 case USB_SPEED_HIGH: 500 uvc_control_desc = uvc->desc.fs_control; 501 uvc_streaming_cls = uvc->desc.hs_streaming; 502 uvc_streaming_std = uvc_hs_streaming; 503 break; 504 505 case USB_SPEED_FULL: 506 default: 507 uvc_control_desc = uvc->desc.fs_control; 508 uvc_streaming_cls = uvc->desc.fs_streaming; 509 uvc_streaming_std = uvc_fs_streaming; 510 break; 511 } 512 513 if (!uvc_control_desc || !uvc_streaming_cls) 514 return ERR_PTR(-ENODEV); 515 516 /* Descriptors layout 517 * 518 * uvc_iad 519 * uvc_control_intf 520 * Class-specific UVC control descriptors 521 * uvc_control_ep 522 * uvc_control_cs_ep 523 * uvc_ss_control_comp (for SS only) 524 * uvc_streaming_intf_alt0 525 * Class-specific UVC streaming descriptors 526 * uvc_{fs|hs}_streaming 527 */ 528 529 /* Count descriptors and compute their size. */ 530 control_size = 0; 531 streaming_size = 0; 532 bytes = uvc_iad.bLength + uvc_control_intf.bLength 533 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength 534 + uvc_streaming_intf_alt0.bLength; 535 536 if (speed == USB_SPEED_SUPER) { 537 bytes += uvc_ss_control_comp.bLength; 538 n_desc = 6; 539 } else { 540 n_desc = 5; 541 } 542 543 for (src = (const struct usb_descriptor_header **)uvc_control_desc; 544 *src; ++src) { 545 control_size += (*src)->bLength; 546 bytes += (*src)->bLength; 547 n_desc++; 548 } 549 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; 550 *src; ++src) { 551 streaming_size += (*src)->bLength; 552 bytes += (*src)->bLength; 553 n_desc++; 554 } 555 for (src = uvc_streaming_std; *src; ++src) { 556 bytes += (*src)->bLength; 557 n_desc++; 558 } 559 560 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); 561 if (mem == NULL) 562 return NULL; 563 564 hdr = mem; 565 dst = mem; 566 mem += (n_desc + 1) * sizeof(*src); 567 568 /* Copy the descriptors. */ 569 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); 570 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); 571 572 uvc_control_header = mem; 573 UVC_COPY_DESCRIPTORS(mem, dst, 574 (const struct usb_descriptor_header **)uvc_control_desc); 575 uvc_control_header->wTotalLength = cpu_to_le16(control_size); 576 uvc_control_header->bInCollection = 1; 577 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; 578 579 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); 580 if (speed == USB_SPEED_SUPER) 581 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); 582 583 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); 584 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); 585 586 uvc_streaming_header = mem; 587 UVC_COPY_DESCRIPTORS(mem, dst, 588 (const struct usb_descriptor_header**)uvc_streaming_cls); 589 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); 590 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; 591 592 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); 593 594 *dst = NULL; 595 return hdr; 596 } 597 598 static int 599 uvc_function_bind(struct usb_configuration *c, struct usb_function *f) 600 { 601 struct usb_composite_dev *cdev = c->cdev; 602 struct uvc_device *uvc = to_uvc(f); 603 struct usb_string *us; 604 unsigned int max_packet_mult; 605 unsigned int max_packet_size; 606 struct usb_ep *ep; 607 struct f_uvc_opts *opts; 608 int ret = -EINVAL; 609 610 INFO(cdev, "uvc_function_bind\n"); 611 612 opts = fi_to_f_uvc_opts(f->fi); 613 /* Sanity check the streaming endpoint module parameters. 614 */ 615 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); 616 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); 617 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); 618 619 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the 620 * module parameters. 621 * 622 * NOTE: We assume that the user knows what they are doing and won't 623 * give parameters that their UDC doesn't support. 624 */ 625 if (opts->streaming_maxpacket <= 1024) { 626 max_packet_mult = 1; 627 max_packet_size = opts->streaming_maxpacket; 628 } else if (opts->streaming_maxpacket <= 2048) { 629 max_packet_mult = 2; 630 max_packet_size = opts->streaming_maxpacket / 2; 631 } else { 632 max_packet_mult = 3; 633 max_packet_size = opts->streaming_maxpacket / 3; 634 } 635 636 uvc_fs_streaming_ep.wMaxPacketSize = 637 cpu_to_le16(min(opts->streaming_maxpacket, 1023U)); 638 uvc_fs_streaming_ep.bInterval = opts->streaming_interval; 639 640 uvc_hs_streaming_ep.wMaxPacketSize = 641 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11)); 642 uvc_hs_streaming_ep.bInterval = opts->streaming_interval; 643 644 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size); 645 uvc_ss_streaming_ep.bInterval = opts->streaming_interval; 646 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; 647 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; 648 uvc_ss_streaming_comp.wBytesPerInterval = 649 cpu_to_le16(max_packet_size * max_packet_mult * 650 opts->streaming_maxburst); 651 652 /* Allocate endpoints. */ 653 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); 654 if (!ep) { 655 INFO(cdev, "Unable to allocate control EP\n"); 656 goto error; 657 } 658 uvc->control_ep = ep; 659 ep->driver_data = uvc; 660 661 if (gadget_is_superspeed(c->cdev->gadget)) 662 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, 663 &uvc_ss_streaming_comp); 664 else if (gadget_is_dualspeed(cdev->gadget)) 665 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); 666 else 667 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); 668 669 if (!ep) { 670 INFO(cdev, "Unable to allocate streaming EP\n"); 671 goto error; 672 } 673 uvc->video.ep = ep; 674 ep->driver_data = uvc; 675 676 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 677 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 678 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; 679 680 us = usb_gstrings_attach(cdev, uvc_function_strings, 681 ARRAY_SIZE(uvc_en_us_strings)); 682 if (IS_ERR(us)) { 683 ret = PTR_ERR(us); 684 goto error; 685 } 686 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id; 687 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id; 688 ret = us[UVC_STRING_STREAMING_IDX].id; 689 uvc_streaming_intf_alt0.iInterface = ret; 690 uvc_streaming_intf_alt1.iInterface = ret; 691 692 /* Allocate interface IDs. */ 693 if ((ret = usb_interface_id(c, f)) < 0) 694 goto error; 695 uvc_iad.bFirstInterface = ret; 696 uvc_control_intf.bInterfaceNumber = ret; 697 uvc->control_intf = ret; 698 699 if ((ret = usb_interface_id(c, f)) < 0) 700 goto error; 701 uvc_streaming_intf_alt0.bInterfaceNumber = ret; 702 uvc_streaming_intf_alt1.bInterfaceNumber = ret; 703 uvc->streaming_intf = ret; 704 705 /* Copy descriptors */ 706 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); 707 if (IS_ERR(f->fs_descriptors)) { 708 ret = PTR_ERR(f->fs_descriptors); 709 f->fs_descriptors = NULL; 710 goto error; 711 } 712 if (gadget_is_dualspeed(cdev->gadget)) { 713 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); 714 if (IS_ERR(f->hs_descriptors)) { 715 ret = PTR_ERR(f->hs_descriptors); 716 f->hs_descriptors = NULL; 717 goto error; 718 } 719 } 720 if (gadget_is_superspeed(c->cdev->gadget)) { 721 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); 722 if (IS_ERR(f->ss_descriptors)) { 723 ret = PTR_ERR(f->ss_descriptors); 724 f->ss_descriptors = NULL; 725 goto error; 726 } 727 } 728 729 /* Preallocate control endpoint request. */ 730 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); 731 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); 732 if (uvc->control_req == NULL || uvc->control_buf == NULL) { 733 ret = -ENOMEM; 734 goto error; 735 } 736 737 uvc->control_req->buf = uvc->control_buf; 738 uvc->control_req->complete = uvc_function_ep0_complete; 739 uvc->control_req->context = uvc; 740 741 /* Avoid letting this gadget enumerate until the userspace server is 742 * active. 743 */ 744 if ((ret = usb_function_deactivate(f)) < 0) 745 goto error; 746 747 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { 748 printk(KERN_INFO "v4l2_device_register failed\n"); 749 goto error; 750 } 751 752 /* Initialise video. */ 753 ret = uvcg_video_init(&uvc->video); 754 if (ret < 0) 755 goto error; 756 757 /* Register a V4L2 device. */ 758 ret = uvc_register_video(uvc); 759 if (ret < 0) { 760 printk(KERN_INFO "Unable to register video device\n"); 761 goto error; 762 } 763 764 return 0; 765 766 error: 767 v4l2_device_unregister(&uvc->v4l2_dev); 768 if (uvc->vdev) 769 video_device_release(uvc->vdev); 770 771 if (uvc->control_ep) 772 uvc->control_ep->driver_data = NULL; 773 if (uvc->video.ep) 774 uvc->video.ep->driver_data = NULL; 775 776 if (uvc->control_req) 777 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 778 kfree(uvc->control_buf); 779 780 usb_free_all_descriptors(f); 781 return ret; 782 } 783 784 /* -------------------------------------------------------------------------- 785 * USB gadget function 786 */ 787 788 static void uvc_free_inst(struct usb_function_instance *f) 789 { 790 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f); 791 792 mutex_destroy(&opts->lock); 793 kfree(opts); 794 } 795 796 static struct usb_function_instance *uvc_alloc_inst(void) 797 { 798 struct f_uvc_opts *opts; 799 struct uvc_camera_terminal_descriptor *cd; 800 struct uvc_processing_unit_descriptor *pd; 801 struct uvc_output_terminal_descriptor *od; 802 struct uvc_color_matching_descriptor *md; 803 struct uvc_descriptor_header **ctl_cls; 804 805 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 806 if (!opts) 807 return ERR_PTR(-ENOMEM); 808 opts->func_inst.free_func_inst = uvc_free_inst; 809 mutex_init(&opts->lock); 810 811 cd = &opts->uvc_camera_terminal; 812 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3); 813 cd->bDescriptorType = USB_DT_CS_INTERFACE; 814 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL; 815 cd->bTerminalID = 1; 816 cd->wTerminalType = cpu_to_le16(0x0201); 817 cd->bAssocTerminal = 0; 818 cd->iTerminal = 0; 819 cd->wObjectiveFocalLengthMin = cpu_to_le16(0); 820 cd->wObjectiveFocalLengthMax = cpu_to_le16(0); 821 cd->wOcularFocalLength = cpu_to_le16(0); 822 cd->bControlSize = 3; 823 cd->bmControls[0] = 2; 824 cd->bmControls[1] = 0; 825 cd->bmControls[2] = 0; 826 827 pd = &opts->uvc_processing; 828 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2); 829 pd->bDescriptorType = USB_DT_CS_INTERFACE; 830 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT; 831 pd->bUnitID = 2; 832 pd->bSourceID = 1; 833 pd->wMaxMultiplier = cpu_to_le16(16*1024); 834 pd->bControlSize = 2; 835 pd->bmControls[0] = 1; 836 pd->bmControls[1] = 0; 837 pd->iProcessing = 0; 838 839 od = &opts->uvc_output_terminal; 840 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE; 841 od->bDescriptorType = USB_DT_CS_INTERFACE; 842 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL; 843 od->bTerminalID = 3; 844 od->wTerminalType = cpu_to_le16(0x0101); 845 od->bAssocTerminal = 0; 846 od->bSourceID = 2; 847 od->iTerminal = 0; 848 849 md = &opts->uvc_color_matching; 850 md->bLength = UVC_DT_COLOR_MATCHING_SIZE; 851 md->bDescriptorType = USB_DT_CS_INTERFACE; 852 md->bDescriptorSubType = UVC_VS_COLORFORMAT; 853 md->bColorPrimaries = 1; 854 md->bTransferCharacteristics = 1; 855 md->bMatrixCoefficients = 4; 856 857 /* Prepare fs control class descriptors for configfs-based gadgets */ 858 ctl_cls = opts->uvc_fs_control_cls; 859 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ 860 ctl_cls[1] = (struct uvc_descriptor_header *)cd; 861 ctl_cls[2] = (struct uvc_descriptor_header *)pd; 862 ctl_cls[3] = (struct uvc_descriptor_header *)od; 863 ctl_cls[4] = NULL; /* NULL-terminate */ 864 opts->fs_control = 865 (const struct uvc_descriptor_header * const *)ctl_cls; 866 867 /* Prepare hs control class descriptors for configfs-based gadgets */ 868 ctl_cls = opts->uvc_ss_control_cls; 869 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ 870 ctl_cls[1] = (struct uvc_descriptor_header *)cd; 871 ctl_cls[2] = (struct uvc_descriptor_header *)pd; 872 ctl_cls[3] = (struct uvc_descriptor_header *)od; 873 ctl_cls[4] = NULL; /* NULL-terminate */ 874 opts->ss_control = 875 (const struct uvc_descriptor_header * const *)ctl_cls; 876 877 opts->streaming_interval = 1; 878 opts->streaming_maxpacket = 1024; 879 880 uvcg_attach_configfs(opts); 881 return &opts->func_inst; 882 } 883 884 static void uvc_free(struct usb_function *f) 885 { 886 struct uvc_device *uvc = to_uvc(f); 887 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts, 888 func_inst); 889 --opts->refcnt; 890 kfree(uvc); 891 } 892 893 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f) 894 { 895 struct usb_composite_dev *cdev = c->cdev; 896 struct uvc_device *uvc = to_uvc(f); 897 898 INFO(cdev, "%s\n", __func__); 899 900 video_unregister_device(uvc->vdev); 901 v4l2_device_unregister(&uvc->v4l2_dev); 902 uvc->control_ep->driver_data = NULL; 903 uvc->video.ep->driver_data = NULL; 904 905 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 906 kfree(uvc->control_buf); 907 908 usb_free_all_descriptors(f); 909 } 910 911 static struct usb_function *uvc_alloc(struct usb_function_instance *fi) 912 { 913 struct uvc_device *uvc; 914 struct f_uvc_opts *opts; 915 struct uvc_descriptor_header **strm_cls; 916 917 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); 918 if (uvc == NULL) 919 return ERR_PTR(-ENOMEM); 920 921 uvc->state = UVC_STATE_DISCONNECTED; 922 opts = fi_to_f_uvc_opts(fi); 923 924 mutex_lock(&opts->lock); 925 if (opts->uvc_fs_streaming_cls) { 926 strm_cls = opts->uvc_fs_streaming_cls; 927 opts->fs_streaming = 928 (const struct uvc_descriptor_header * const *)strm_cls; 929 } 930 if (opts->uvc_hs_streaming_cls) { 931 strm_cls = opts->uvc_hs_streaming_cls; 932 opts->hs_streaming = 933 (const struct uvc_descriptor_header * const *)strm_cls; 934 } 935 if (opts->uvc_ss_streaming_cls) { 936 strm_cls = opts->uvc_ss_streaming_cls; 937 opts->ss_streaming = 938 (const struct uvc_descriptor_header * const *)strm_cls; 939 } 940 941 uvc->desc.fs_control = opts->fs_control; 942 uvc->desc.ss_control = opts->ss_control; 943 uvc->desc.fs_streaming = opts->fs_streaming; 944 uvc->desc.hs_streaming = opts->hs_streaming; 945 uvc->desc.ss_streaming = opts->ss_streaming; 946 ++opts->refcnt; 947 mutex_unlock(&opts->lock); 948 949 /* Register the function. */ 950 uvc->func.name = "uvc"; 951 uvc->func.bind = uvc_function_bind; 952 uvc->func.unbind = uvc_unbind; 953 uvc->func.get_alt = uvc_function_get_alt; 954 uvc->func.set_alt = uvc_function_set_alt; 955 uvc->func.disable = uvc_function_disable; 956 uvc->func.setup = uvc_function_setup; 957 uvc->func.free_func = uvc_free; 958 959 return &uvc->func; 960 } 961 962 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); 963 MODULE_LICENSE("GPL"); 964 MODULE_AUTHOR("Laurent Pinchart"); 965