1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * uvc_gadget.c -- USB Video Class Gadget driver 4 * 5 * Copyright (C) 2009-2010 6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/device.h> 17 #include <linux/errno.h> 18 #include <linux/fs.h> 19 #include <linux/list.h> 20 #include <linux/mutex.h> 21 #include <linux/string.h> 22 #include <linux/usb/ch9.h> 23 #include <linux/usb/gadget.h> 24 #include <linux/usb/video.h> 25 #include <linux/vmalloc.h> 26 #include <linux/wait.h> 27 28 #include <media/v4l2-dev.h> 29 #include <media/v4l2-event.h> 30 31 #include "u_uvc.h" 32 #include "uvc.h" 33 #include "uvc_configfs.h" 34 #include "uvc_v4l2.h" 35 #include "uvc_video.h" 36 37 unsigned int uvc_gadget_trace_param; 38 39 /* -------------------------------------------------------------------------- 40 * Function descriptors 41 */ 42 43 /* string IDs are assigned dynamically */ 44 45 #define UVC_STRING_CONTROL_IDX 0 46 #define UVC_STRING_STREAMING_IDX 1 47 48 static struct usb_string uvc_en_us_strings[] = { 49 [UVC_STRING_CONTROL_IDX].s = "UVC Camera", 50 [UVC_STRING_STREAMING_IDX].s = "Video Streaming", 51 { } 52 }; 53 54 static struct usb_gadget_strings uvc_stringtab = { 55 .language = 0x0409, /* en-us */ 56 .strings = uvc_en_us_strings, 57 }; 58 59 static struct usb_gadget_strings *uvc_function_strings[] = { 60 &uvc_stringtab, 61 NULL, 62 }; 63 64 #define UVC_INTF_VIDEO_CONTROL 0 65 #define UVC_INTF_VIDEO_STREAMING 1 66 67 #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ 68 69 static struct usb_interface_assoc_descriptor uvc_iad = { 70 .bLength = sizeof(uvc_iad), 71 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 72 .bFirstInterface = 0, 73 .bInterfaceCount = 2, 74 .bFunctionClass = USB_CLASS_VIDEO, 75 .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, 76 .bFunctionProtocol = 0x00, 77 .iFunction = 0, 78 }; 79 80 static struct usb_interface_descriptor uvc_control_intf = { 81 .bLength = USB_DT_INTERFACE_SIZE, 82 .bDescriptorType = USB_DT_INTERFACE, 83 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, 84 .bAlternateSetting = 0, 85 .bNumEndpoints = 1, 86 .bInterfaceClass = USB_CLASS_VIDEO, 87 .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, 88 .bInterfaceProtocol = 0x00, 89 .iInterface = 0, 90 }; 91 92 static struct usb_endpoint_descriptor uvc_control_ep = { 93 .bLength = USB_DT_ENDPOINT_SIZE, 94 .bDescriptorType = USB_DT_ENDPOINT, 95 .bEndpointAddress = USB_DIR_IN, 96 .bmAttributes = USB_ENDPOINT_XFER_INT, 97 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 98 .bInterval = 8, 99 }; 100 101 static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = { 102 .bLength = sizeof(uvc_ss_control_comp), 103 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 104 /* The following 3 values can be tweaked if necessary. */ 105 .bMaxBurst = 0, 106 .bmAttributes = 0, 107 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 108 }; 109 110 static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = { 111 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, 112 .bDescriptorType = USB_DT_CS_ENDPOINT, 113 .bDescriptorSubType = UVC_EP_INTERRUPT, 114 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), 115 }; 116 117 static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { 118 .bLength = USB_DT_INTERFACE_SIZE, 119 .bDescriptorType = USB_DT_INTERFACE, 120 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, 121 .bAlternateSetting = 0, 122 .bNumEndpoints = 0, 123 .bInterfaceClass = USB_CLASS_VIDEO, 124 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 125 .bInterfaceProtocol = 0x00, 126 .iInterface = 0, 127 }; 128 129 static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { 130 .bLength = USB_DT_INTERFACE_SIZE, 131 .bDescriptorType = USB_DT_INTERFACE, 132 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, 133 .bAlternateSetting = 1, 134 .bNumEndpoints = 1, 135 .bInterfaceClass = USB_CLASS_VIDEO, 136 .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, 137 .bInterfaceProtocol = 0x00, 138 .iInterface = 0, 139 }; 140 141 static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { 142 .bLength = USB_DT_ENDPOINT_SIZE, 143 .bDescriptorType = USB_DT_ENDPOINT, 144 .bEndpointAddress = USB_DIR_IN, 145 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 146 | USB_ENDPOINT_XFER_ISOC, 147 /* The wMaxPacketSize and bInterval values will be initialized from 148 * module parameters. 149 */ 150 }; 151 152 static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { 153 .bLength = USB_DT_ENDPOINT_SIZE, 154 .bDescriptorType = USB_DT_ENDPOINT, 155 .bEndpointAddress = USB_DIR_IN, 156 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 157 | USB_ENDPOINT_XFER_ISOC, 158 /* The wMaxPacketSize and bInterval values will be initialized from 159 * module parameters. 160 */ 161 }; 162 163 static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { 164 .bLength = USB_DT_ENDPOINT_SIZE, 165 .bDescriptorType = USB_DT_ENDPOINT, 166 167 .bEndpointAddress = USB_DIR_IN, 168 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 169 | USB_ENDPOINT_XFER_ISOC, 170 /* The wMaxPacketSize and bInterval values will be initialized from 171 * module parameters. 172 */ 173 }; 174 175 static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { 176 .bLength = sizeof(uvc_ss_streaming_comp), 177 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 178 /* The bMaxBurst, bmAttributes and wBytesPerInterval values will be 179 * initialized from module parameters. 180 */ 181 }; 182 183 static const struct usb_descriptor_header * const uvc_fs_streaming[] = { 184 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 185 (struct usb_descriptor_header *) &uvc_fs_streaming_ep, 186 NULL, 187 }; 188 189 static const struct usb_descriptor_header * const uvc_hs_streaming[] = { 190 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 191 (struct usb_descriptor_header *) &uvc_hs_streaming_ep, 192 NULL, 193 }; 194 195 static const struct usb_descriptor_header * const uvc_ss_streaming[] = { 196 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, 197 (struct usb_descriptor_header *) &uvc_ss_streaming_ep, 198 (struct usb_descriptor_header *) &uvc_ss_streaming_comp, 199 NULL, 200 }; 201 202 void uvc_set_trace_param(unsigned int trace) 203 { 204 uvc_gadget_trace_param = trace; 205 } 206 EXPORT_SYMBOL(uvc_set_trace_param); 207 208 /* -------------------------------------------------------------------------- 209 * Control requests 210 */ 211 212 static void 213 uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) 214 { 215 struct uvc_device *uvc = req->context; 216 struct v4l2_event v4l2_event; 217 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 218 219 if (uvc->event_setup_out) { 220 uvc->event_setup_out = 0; 221 222 memset(&v4l2_event, 0, sizeof(v4l2_event)); 223 v4l2_event.type = UVC_EVENT_DATA; 224 uvc_event->data.length = req->actual; 225 memcpy(&uvc_event->data.data, req->buf, req->actual); 226 v4l2_event_queue(&uvc->vdev, &v4l2_event); 227 } 228 } 229 230 static int 231 uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 232 { 233 struct uvc_device *uvc = to_uvc(f); 234 struct v4l2_event v4l2_event; 235 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 236 237 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", 238 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), 239 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); 240 */ 241 242 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { 243 INFO(f->config->cdev, "invalid request type\n"); 244 return -EINVAL; 245 } 246 247 /* Stall too big requests. */ 248 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) 249 return -EINVAL; 250 251 /* Tell the complete callback to generate an event for the next request 252 * that will be enqueued by UVCIOC_SEND_RESPONSE. 253 */ 254 uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); 255 uvc->event_length = le16_to_cpu(ctrl->wLength); 256 257 memset(&v4l2_event, 0, sizeof(v4l2_event)); 258 v4l2_event.type = UVC_EVENT_SETUP; 259 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); 260 v4l2_event_queue(&uvc->vdev, &v4l2_event); 261 262 return 0; 263 } 264 265 void uvc_function_setup_continue(struct uvc_device *uvc) 266 { 267 struct usb_composite_dev *cdev = uvc->func.config->cdev; 268 269 usb_composite_setup_continue(cdev); 270 } 271 272 static int 273 uvc_function_get_alt(struct usb_function *f, unsigned interface) 274 { 275 struct uvc_device *uvc = to_uvc(f); 276 277 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); 278 279 if (interface == uvc->control_intf) 280 return 0; 281 else if (interface != uvc->streaming_intf) 282 return -EINVAL; 283 else 284 return uvc->video.ep->enabled ? 1 : 0; 285 } 286 287 static int 288 uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) 289 { 290 struct uvc_device *uvc = to_uvc(f); 291 struct usb_composite_dev *cdev = f->config->cdev; 292 struct v4l2_event v4l2_event; 293 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; 294 int ret; 295 296 INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); 297 298 if (interface == uvc->control_intf) { 299 if (alt) 300 return -EINVAL; 301 302 INFO(cdev, "reset UVC Control\n"); 303 usb_ep_disable(uvc->control_ep); 304 305 if (!uvc->control_ep->desc) 306 if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep)) 307 return -EINVAL; 308 309 usb_ep_enable(uvc->control_ep); 310 311 if (uvc->state == UVC_STATE_DISCONNECTED) { 312 memset(&v4l2_event, 0, sizeof(v4l2_event)); 313 v4l2_event.type = UVC_EVENT_CONNECT; 314 uvc_event->speed = cdev->gadget->speed; 315 v4l2_event_queue(&uvc->vdev, &v4l2_event); 316 317 uvc->state = UVC_STATE_CONNECTED; 318 } 319 320 return 0; 321 } 322 323 if (interface != uvc->streaming_intf) 324 return -EINVAL; 325 326 /* TODO 327 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) 328 return alt ? -EINVAL : 0; 329 */ 330 331 switch (alt) { 332 case 0: 333 if (uvc->state != UVC_STATE_STREAMING) 334 return 0; 335 336 if (uvc->video.ep) 337 usb_ep_disable(uvc->video.ep); 338 339 memset(&v4l2_event, 0, sizeof(v4l2_event)); 340 v4l2_event.type = UVC_EVENT_STREAMOFF; 341 v4l2_event_queue(&uvc->vdev, &v4l2_event); 342 343 uvc->state = UVC_STATE_CONNECTED; 344 return 0; 345 346 case 1: 347 if (uvc->state != UVC_STATE_CONNECTED) 348 return 0; 349 350 if (!uvc->video.ep) 351 return -EINVAL; 352 353 INFO(cdev, "reset UVC\n"); 354 usb_ep_disable(uvc->video.ep); 355 356 ret = config_ep_by_speed(f->config->cdev->gadget, 357 &(uvc->func), uvc->video.ep); 358 if (ret) 359 return ret; 360 usb_ep_enable(uvc->video.ep); 361 362 memset(&v4l2_event, 0, sizeof(v4l2_event)); 363 v4l2_event.type = UVC_EVENT_STREAMON; 364 v4l2_event_queue(&uvc->vdev, &v4l2_event); 365 return USB_GADGET_DELAYED_STATUS; 366 367 default: 368 return -EINVAL; 369 } 370 } 371 372 static void 373 uvc_function_disable(struct usb_function *f) 374 { 375 struct uvc_device *uvc = to_uvc(f); 376 struct v4l2_event v4l2_event; 377 378 INFO(f->config->cdev, "uvc_function_disable\n"); 379 380 memset(&v4l2_event, 0, sizeof(v4l2_event)); 381 v4l2_event.type = UVC_EVENT_DISCONNECT; 382 v4l2_event_queue(&uvc->vdev, &v4l2_event); 383 384 uvc->state = UVC_STATE_DISCONNECTED; 385 386 usb_ep_disable(uvc->video.ep); 387 usb_ep_disable(uvc->control_ep); 388 } 389 390 /* -------------------------------------------------------------------------- 391 * Connection / disconnection 392 */ 393 394 void 395 uvc_function_connect(struct uvc_device *uvc) 396 { 397 struct usb_composite_dev *cdev = uvc->func.config->cdev; 398 int ret; 399 400 if ((ret = usb_function_activate(&uvc->func)) < 0) 401 INFO(cdev, "UVC connect failed with %d\n", ret); 402 } 403 404 void 405 uvc_function_disconnect(struct uvc_device *uvc) 406 { 407 struct usb_composite_dev *cdev = uvc->func.config->cdev; 408 int ret; 409 410 if ((ret = usb_function_deactivate(&uvc->func)) < 0) 411 INFO(cdev, "UVC disconnect failed with %d\n", ret); 412 } 413 414 /* -------------------------------------------------------------------------- 415 * USB probe and disconnect 416 */ 417 418 static int 419 uvc_register_video(struct uvc_device *uvc) 420 { 421 struct usb_composite_dev *cdev = uvc->func.config->cdev; 422 423 /* TODO reference counting. */ 424 uvc->vdev.v4l2_dev = &uvc->v4l2_dev; 425 uvc->vdev.fops = &uvc_v4l2_fops; 426 uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops; 427 uvc->vdev.release = video_device_release_empty; 428 uvc->vdev.vfl_dir = VFL_DIR_TX; 429 uvc->vdev.lock = &uvc->video.mutex; 430 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name)); 431 432 video_set_drvdata(&uvc->vdev, uvc); 433 434 return video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1); 435 } 436 437 #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ 438 do { \ 439 memcpy(mem, desc, (desc)->bLength); \ 440 *(dst)++ = mem; \ 441 mem += (desc)->bLength; \ 442 } while (0); 443 444 #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ 445 do { \ 446 const struct usb_descriptor_header * const *__src; \ 447 for (__src = src; *__src; ++__src) { \ 448 memcpy(mem, *__src, (*__src)->bLength); \ 449 *dst++ = mem; \ 450 mem += (*__src)->bLength; \ 451 } \ 452 } while (0) 453 454 static struct usb_descriptor_header ** 455 uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) 456 { 457 struct uvc_input_header_descriptor *uvc_streaming_header; 458 struct uvc_header_descriptor *uvc_control_header; 459 const struct uvc_descriptor_header * const *uvc_control_desc; 460 const struct uvc_descriptor_header * const *uvc_streaming_cls; 461 const struct usb_descriptor_header * const *uvc_streaming_std; 462 const struct usb_descriptor_header * const *src; 463 struct usb_descriptor_header **dst; 464 struct usb_descriptor_header **hdr; 465 unsigned int control_size; 466 unsigned int streaming_size; 467 unsigned int n_desc; 468 unsigned int bytes; 469 void *mem; 470 471 switch (speed) { 472 case USB_SPEED_SUPER: 473 uvc_control_desc = uvc->desc.ss_control; 474 uvc_streaming_cls = uvc->desc.ss_streaming; 475 uvc_streaming_std = uvc_ss_streaming; 476 break; 477 478 case USB_SPEED_HIGH: 479 uvc_control_desc = uvc->desc.fs_control; 480 uvc_streaming_cls = uvc->desc.hs_streaming; 481 uvc_streaming_std = uvc_hs_streaming; 482 break; 483 484 case USB_SPEED_FULL: 485 default: 486 uvc_control_desc = uvc->desc.fs_control; 487 uvc_streaming_cls = uvc->desc.fs_streaming; 488 uvc_streaming_std = uvc_fs_streaming; 489 break; 490 } 491 492 if (!uvc_control_desc || !uvc_streaming_cls) 493 return ERR_PTR(-ENODEV); 494 495 /* Descriptors layout 496 * 497 * uvc_iad 498 * uvc_control_intf 499 * Class-specific UVC control descriptors 500 * uvc_control_ep 501 * uvc_control_cs_ep 502 * uvc_ss_control_comp (for SS only) 503 * uvc_streaming_intf_alt0 504 * Class-specific UVC streaming descriptors 505 * uvc_{fs|hs}_streaming 506 */ 507 508 /* Count descriptors and compute their size. */ 509 control_size = 0; 510 streaming_size = 0; 511 bytes = uvc_iad.bLength + uvc_control_intf.bLength 512 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength 513 + uvc_streaming_intf_alt0.bLength; 514 515 if (speed == USB_SPEED_SUPER) { 516 bytes += uvc_ss_control_comp.bLength; 517 n_desc = 6; 518 } else { 519 n_desc = 5; 520 } 521 522 for (src = (const struct usb_descriptor_header **)uvc_control_desc; 523 *src; ++src) { 524 control_size += (*src)->bLength; 525 bytes += (*src)->bLength; 526 n_desc++; 527 } 528 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; 529 *src; ++src) { 530 streaming_size += (*src)->bLength; 531 bytes += (*src)->bLength; 532 n_desc++; 533 } 534 for (src = uvc_streaming_std; *src; ++src) { 535 bytes += (*src)->bLength; 536 n_desc++; 537 } 538 539 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); 540 if (mem == NULL) 541 return NULL; 542 543 hdr = mem; 544 dst = mem; 545 mem += (n_desc + 1) * sizeof(*src); 546 547 /* Copy the descriptors. */ 548 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); 549 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); 550 551 uvc_control_header = mem; 552 UVC_COPY_DESCRIPTORS(mem, dst, 553 (const struct usb_descriptor_header **)uvc_control_desc); 554 uvc_control_header->wTotalLength = cpu_to_le16(control_size); 555 uvc_control_header->bInCollection = 1; 556 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; 557 558 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep); 559 if (speed == USB_SPEED_SUPER) 560 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); 561 562 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep); 563 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); 564 565 uvc_streaming_header = mem; 566 UVC_COPY_DESCRIPTORS(mem, dst, 567 (const struct usb_descriptor_header**)uvc_streaming_cls); 568 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); 569 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; 570 571 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); 572 573 *dst = NULL; 574 return hdr; 575 } 576 577 static int 578 uvc_function_bind(struct usb_configuration *c, struct usb_function *f) 579 { 580 struct usb_composite_dev *cdev = c->cdev; 581 struct uvc_device *uvc = to_uvc(f); 582 struct usb_string *us; 583 unsigned int max_packet_mult; 584 unsigned int max_packet_size; 585 struct usb_ep *ep; 586 struct f_uvc_opts *opts; 587 int ret = -EINVAL; 588 589 INFO(cdev, "uvc_function_bind\n"); 590 591 opts = fi_to_f_uvc_opts(f->fi); 592 /* Sanity check the streaming endpoint module parameters. 593 */ 594 opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); 595 opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); 596 opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); 597 598 /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */ 599 if (opts->streaming_maxburst && 600 (opts->streaming_maxpacket % 1024) != 0) { 601 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024); 602 INFO(cdev, "overriding streaming_maxpacket to %d\n", 603 opts->streaming_maxpacket); 604 } 605 606 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the 607 * module parameters. 608 * 609 * NOTE: We assume that the user knows what they are doing and won't 610 * give parameters that their UDC doesn't support. 611 */ 612 if (opts->streaming_maxpacket <= 1024) { 613 max_packet_mult = 1; 614 max_packet_size = opts->streaming_maxpacket; 615 } else if (opts->streaming_maxpacket <= 2048) { 616 max_packet_mult = 2; 617 max_packet_size = opts->streaming_maxpacket / 2; 618 } else { 619 max_packet_mult = 3; 620 max_packet_size = opts->streaming_maxpacket / 3; 621 } 622 623 uvc_fs_streaming_ep.wMaxPacketSize = 624 cpu_to_le16(min(opts->streaming_maxpacket, 1023U)); 625 uvc_fs_streaming_ep.bInterval = opts->streaming_interval; 626 627 uvc_hs_streaming_ep.wMaxPacketSize = 628 cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11)); 629 uvc_hs_streaming_ep.bInterval = opts->streaming_interval; 630 631 uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size); 632 uvc_ss_streaming_ep.bInterval = opts->streaming_interval; 633 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; 634 uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; 635 uvc_ss_streaming_comp.wBytesPerInterval = 636 cpu_to_le16(max_packet_size * max_packet_mult * 637 (opts->streaming_maxburst + 1)); 638 639 /* Allocate endpoints. */ 640 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); 641 if (!ep) { 642 INFO(cdev, "Unable to allocate control EP\n"); 643 goto error; 644 } 645 uvc->control_ep = ep; 646 647 if (gadget_is_superspeed(c->cdev->gadget)) 648 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, 649 &uvc_ss_streaming_comp); 650 else if (gadget_is_dualspeed(cdev->gadget)) 651 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); 652 else 653 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); 654 655 if (!ep) { 656 INFO(cdev, "Unable to allocate streaming EP\n"); 657 goto error; 658 } 659 uvc->video.ep = ep; 660 661 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 662 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; 663 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; 664 665 us = usb_gstrings_attach(cdev, uvc_function_strings, 666 ARRAY_SIZE(uvc_en_us_strings)); 667 if (IS_ERR(us)) { 668 ret = PTR_ERR(us); 669 goto error; 670 } 671 uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id; 672 uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id; 673 ret = us[UVC_STRING_STREAMING_IDX].id; 674 uvc_streaming_intf_alt0.iInterface = ret; 675 uvc_streaming_intf_alt1.iInterface = ret; 676 677 /* Allocate interface IDs. */ 678 if ((ret = usb_interface_id(c, f)) < 0) 679 goto error; 680 uvc_iad.bFirstInterface = ret; 681 uvc_control_intf.bInterfaceNumber = ret; 682 uvc->control_intf = ret; 683 684 if ((ret = usb_interface_id(c, f)) < 0) 685 goto error; 686 uvc_streaming_intf_alt0.bInterfaceNumber = ret; 687 uvc_streaming_intf_alt1.bInterfaceNumber = ret; 688 uvc->streaming_intf = ret; 689 690 /* Copy descriptors */ 691 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); 692 if (IS_ERR(f->fs_descriptors)) { 693 ret = PTR_ERR(f->fs_descriptors); 694 f->fs_descriptors = NULL; 695 goto error; 696 } 697 if (gadget_is_dualspeed(cdev->gadget)) { 698 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH); 699 if (IS_ERR(f->hs_descriptors)) { 700 ret = PTR_ERR(f->hs_descriptors); 701 f->hs_descriptors = NULL; 702 goto error; 703 } 704 } 705 if (gadget_is_superspeed(c->cdev->gadget)) { 706 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); 707 if (IS_ERR(f->ss_descriptors)) { 708 ret = PTR_ERR(f->ss_descriptors); 709 f->ss_descriptors = NULL; 710 goto error; 711 } 712 } 713 714 /* Preallocate control endpoint request. */ 715 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); 716 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); 717 if (uvc->control_req == NULL || uvc->control_buf == NULL) { 718 ret = -ENOMEM; 719 goto error; 720 } 721 722 uvc->control_req->buf = uvc->control_buf; 723 uvc->control_req->complete = uvc_function_ep0_complete; 724 uvc->control_req->context = uvc; 725 726 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { 727 printk(KERN_INFO "v4l2_device_register failed\n"); 728 goto error; 729 } 730 731 /* Initialise video. */ 732 ret = uvcg_video_init(&uvc->video); 733 if (ret < 0) 734 goto error; 735 736 /* Register a V4L2 device. */ 737 ret = uvc_register_video(uvc); 738 if (ret < 0) { 739 printk(KERN_INFO "Unable to register video device\n"); 740 goto error; 741 } 742 743 return 0; 744 745 error: 746 v4l2_device_unregister(&uvc->v4l2_dev); 747 748 if (uvc->control_req) 749 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 750 kfree(uvc->control_buf); 751 752 usb_free_all_descriptors(f); 753 return ret; 754 } 755 756 /* -------------------------------------------------------------------------- 757 * USB gadget function 758 */ 759 760 static void uvc_free_inst(struct usb_function_instance *f) 761 { 762 struct f_uvc_opts *opts = fi_to_f_uvc_opts(f); 763 764 mutex_destroy(&opts->lock); 765 kfree(opts); 766 } 767 768 static struct usb_function_instance *uvc_alloc_inst(void) 769 { 770 struct f_uvc_opts *opts; 771 struct uvc_camera_terminal_descriptor *cd; 772 struct uvc_processing_unit_descriptor *pd; 773 struct uvc_output_terminal_descriptor *od; 774 struct uvc_color_matching_descriptor *md; 775 struct uvc_descriptor_header **ctl_cls; 776 777 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 778 if (!opts) 779 return ERR_PTR(-ENOMEM); 780 opts->func_inst.free_func_inst = uvc_free_inst; 781 mutex_init(&opts->lock); 782 783 cd = &opts->uvc_camera_terminal; 784 cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3); 785 cd->bDescriptorType = USB_DT_CS_INTERFACE; 786 cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL; 787 cd->bTerminalID = 1; 788 cd->wTerminalType = cpu_to_le16(0x0201); 789 cd->bAssocTerminal = 0; 790 cd->iTerminal = 0; 791 cd->wObjectiveFocalLengthMin = cpu_to_le16(0); 792 cd->wObjectiveFocalLengthMax = cpu_to_le16(0); 793 cd->wOcularFocalLength = cpu_to_le16(0); 794 cd->bControlSize = 3; 795 cd->bmControls[0] = 2; 796 cd->bmControls[1] = 0; 797 cd->bmControls[2] = 0; 798 799 pd = &opts->uvc_processing; 800 pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2); 801 pd->bDescriptorType = USB_DT_CS_INTERFACE; 802 pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT; 803 pd->bUnitID = 2; 804 pd->bSourceID = 1; 805 pd->wMaxMultiplier = cpu_to_le16(16*1024); 806 pd->bControlSize = 2; 807 pd->bmControls[0] = 1; 808 pd->bmControls[1] = 0; 809 pd->iProcessing = 0; 810 811 od = &opts->uvc_output_terminal; 812 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE; 813 od->bDescriptorType = USB_DT_CS_INTERFACE; 814 od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL; 815 od->bTerminalID = 3; 816 od->wTerminalType = cpu_to_le16(0x0101); 817 od->bAssocTerminal = 0; 818 od->bSourceID = 2; 819 od->iTerminal = 0; 820 821 md = &opts->uvc_color_matching; 822 md->bLength = UVC_DT_COLOR_MATCHING_SIZE; 823 md->bDescriptorType = USB_DT_CS_INTERFACE; 824 md->bDescriptorSubType = UVC_VS_COLORFORMAT; 825 md->bColorPrimaries = 1; 826 md->bTransferCharacteristics = 1; 827 md->bMatrixCoefficients = 4; 828 829 /* Prepare fs control class descriptors for configfs-based gadgets */ 830 ctl_cls = opts->uvc_fs_control_cls; 831 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ 832 ctl_cls[1] = (struct uvc_descriptor_header *)cd; 833 ctl_cls[2] = (struct uvc_descriptor_header *)pd; 834 ctl_cls[3] = (struct uvc_descriptor_header *)od; 835 ctl_cls[4] = NULL; /* NULL-terminate */ 836 opts->fs_control = 837 (const struct uvc_descriptor_header * const *)ctl_cls; 838 839 /* Prepare hs control class descriptors for configfs-based gadgets */ 840 ctl_cls = opts->uvc_ss_control_cls; 841 ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ 842 ctl_cls[1] = (struct uvc_descriptor_header *)cd; 843 ctl_cls[2] = (struct uvc_descriptor_header *)pd; 844 ctl_cls[3] = (struct uvc_descriptor_header *)od; 845 ctl_cls[4] = NULL; /* NULL-terminate */ 846 opts->ss_control = 847 (const struct uvc_descriptor_header * const *)ctl_cls; 848 849 opts->streaming_interval = 1; 850 opts->streaming_maxpacket = 1024; 851 852 uvcg_attach_configfs(opts); 853 return &opts->func_inst; 854 } 855 856 static void uvc_free(struct usb_function *f) 857 { 858 struct uvc_device *uvc = to_uvc(f); 859 struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts, 860 func_inst); 861 --opts->refcnt; 862 kfree(uvc); 863 } 864 865 static void uvc_unbind(struct usb_configuration *c, struct usb_function *f) 866 { 867 struct usb_composite_dev *cdev = c->cdev; 868 struct uvc_device *uvc = to_uvc(f); 869 870 INFO(cdev, "%s\n", __func__); 871 872 video_unregister_device(&uvc->vdev); 873 v4l2_device_unregister(&uvc->v4l2_dev); 874 875 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 876 kfree(uvc->control_buf); 877 878 usb_free_all_descriptors(f); 879 } 880 881 static struct usb_function *uvc_alloc(struct usb_function_instance *fi) 882 { 883 struct uvc_device *uvc; 884 struct f_uvc_opts *opts; 885 struct uvc_descriptor_header **strm_cls; 886 887 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); 888 if (uvc == NULL) 889 return ERR_PTR(-ENOMEM); 890 891 mutex_init(&uvc->video.mutex); 892 uvc->state = UVC_STATE_DISCONNECTED; 893 opts = fi_to_f_uvc_opts(fi); 894 895 mutex_lock(&opts->lock); 896 if (opts->uvc_fs_streaming_cls) { 897 strm_cls = opts->uvc_fs_streaming_cls; 898 opts->fs_streaming = 899 (const struct uvc_descriptor_header * const *)strm_cls; 900 } 901 if (opts->uvc_hs_streaming_cls) { 902 strm_cls = opts->uvc_hs_streaming_cls; 903 opts->hs_streaming = 904 (const struct uvc_descriptor_header * const *)strm_cls; 905 } 906 if (opts->uvc_ss_streaming_cls) { 907 strm_cls = opts->uvc_ss_streaming_cls; 908 opts->ss_streaming = 909 (const struct uvc_descriptor_header * const *)strm_cls; 910 } 911 912 uvc->desc.fs_control = opts->fs_control; 913 uvc->desc.ss_control = opts->ss_control; 914 uvc->desc.fs_streaming = opts->fs_streaming; 915 uvc->desc.hs_streaming = opts->hs_streaming; 916 uvc->desc.ss_streaming = opts->ss_streaming; 917 ++opts->refcnt; 918 mutex_unlock(&opts->lock); 919 920 /* Register the function. */ 921 uvc->func.name = "uvc"; 922 uvc->func.bind = uvc_function_bind; 923 uvc->func.unbind = uvc_unbind; 924 uvc->func.get_alt = uvc_function_get_alt; 925 uvc->func.set_alt = uvc_function_set_alt; 926 uvc->func.disable = uvc_function_disable; 927 uvc->func.setup = uvc_function_setup; 928 uvc->func.free_func = uvc_free; 929 uvc->func.bind_deactivated = true; 930 931 return &uvc->func; 932 } 933 934 DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); 935 MODULE_LICENSE("GPL"); 936 MODULE_AUTHOR("Laurent Pinchart"); 937