| 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 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mutex.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/usb/ch9.h> |
| 18 | #include <linux/usb/gadget.h> |
| 19 | #include <linux/usb/g_uvc.h> |
| 20 | #include <linux/usb/video.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/wait.h> |
| 23 | |
| 24 | #include <media/v4l2-dev.h> |
| 25 | #include <media/v4l2-event.h> |
| 26 | |
| 27 | #include "uvc.h" |
| 28 | #include "uvc_configfs.h" |
| 29 | #include "uvc_v4l2.h" |
| 30 | #include "uvc_video.h" |
| 31 | |
| 32 | unsigned int uvc_gadget_trace_param; |
| 33 | module_param_named(trace, uvc_gadget_trace_param, uint, 0644); |
| 34 | MODULE_PARM_DESC(trace, "Trace level bitmask" ); |
| 35 | |
| 36 | /* -------------------------------------------------------------------------- |
| 37 | * Function descriptors |
| 38 | */ |
| 39 | |
| 40 | /* string IDs are assigned dynamically */ |
| 41 | |
| 42 | static struct usb_string uvc_en_us_strings[] = { |
| 43 | /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */ |
| 44 | [UVC_STRING_STREAMING_IDX].s = "Video Streaming" , |
| 45 | { } |
| 46 | }; |
| 47 | |
| 48 | static struct usb_gadget_strings uvc_stringtab = { |
| 49 | .language = 0x0409, /* en-us */ |
| 50 | .strings = uvc_en_us_strings, |
| 51 | }; |
| 52 | |
| 53 | static struct usb_gadget_strings *uvc_function_strings[] = { |
| 54 | &uvc_stringtab, |
| 55 | NULL, |
| 56 | }; |
| 57 | |
| 58 | #define UVC_INTF_VIDEO_CONTROL 0 |
| 59 | #define UVC_INTF_VIDEO_STREAMING 1 |
| 60 | |
| 61 | #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */ |
| 62 | |
| 63 | static struct usb_interface_assoc_descriptor uvc_iad = { |
| 64 | .bLength = sizeof(uvc_iad), |
| 65 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 66 | .bFirstInterface = 0, |
| 67 | .bInterfaceCount = 2, |
| 68 | .bFunctionClass = USB_CLASS_VIDEO, |
| 69 | .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION, |
| 70 | .bFunctionProtocol = 0x00, |
| 71 | .iFunction = 0, |
| 72 | }; |
| 73 | |
| 74 | static struct usb_interface_descriptor uvc_control_intf = { |
| 75 | .bLength = USB_DT_INTERFACE_SIZE, |
| 76 | .bDescriptorType = USB_DT_INTERFACE, |
| 77 | .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL, |
| 78 | .bAlternateSetting = 0, |
| 79 | .bNumEndpoints = 0, |
| 80 | .bInterfaceClass = USB_CLASS_VIDEO, |
| 81 | .bInterfaceSubClass = UVC_SC_VIDEOCONTROL, |
| 82 | .bInterfaceProtocol = 0x00, |
| 83 | .iInterface = 0, |
| 84 | }; |
| 85 | |
| 86 | static struct usb_endpoint_descriptor uvc_interrupt_ep = { |
| 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(UVC_STATUS_MAX_PACKET_SIZE), |
| 92 | .bInterval = 8, |
| 93 | }; |
| 94 | |
| 95 | static struct usb_ss_ep_comp_descriptor uvc_ss_interrupt_comp = { |
| 96 | .bLength = sizeof(uvc_ss_interrupt_comp), |
| 97 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 98 | /* The following 3 values can be tweaked if necessary. */ |
| 99 | .bMaxBurst = 0, |
| 100 | .bmAttributes = 0, |
| 101 | .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
| 102 | }; |
| 103 | |
| 104 | static struct uvc_control_endpoint_descriptor uvc_interrupt_cs_ep = { |
| 105 | .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, |
| 106 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 107 | .bDescriptorSubType = UVC_EP_INTERRUPT, |
| 108 | .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE), |
| 109 | }; |
| 110 | |
| 111 | static struct usb_interface_descriptor uvc_streaming_intf_alt0 = { |
| 112 | .bLength = USB_DT_INTERFACE_SIZE, |
| 113 | .bDescriptorType = USB_DT_INTERFACE, |
| 114 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, |
| 115 | .bAlternateSetting = 0, |
| 116 | .bNumEndpoints = 0, |
| 117 | .bInterfaceClass = USB_CLASS_VIDEO, |
| 118 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
| 119 | .bInterfaceProtocol = 0x00, |
| 120 | .iInterface = 0, |
| 121 | }; |
| 122 | |
| 123 | static struct usb_interface_descriptor uvc_streaming_intf_alt1 = { |
| 124 | .bLength = USB_DT_INTERFACE_SIZE, |
| 125 | .bDescriptorType = USB_DT_INTERFACE, |
| 126 | .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING, |
| 127 | .bAlternateSetting = 1, |
| 128 | .bNumEndpoints = 1, |
| 129 | .bInterfaceClass = USB_CLASS_VIDEO, |
| 130 | .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING, |
| 131 | .bInterfaceProtocol = 0x00, |
| 132 | .iInterface = 0, |
| 133 | }; |
| 134 | |
| 135 | static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { |
| 136 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 137 | .bDescriptorType = USB_DT_ENDPOINT, |
| 138 | .bEndpointAddress = USB_DIR_IN, |
| 139 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
| 140 | | USB_ENDPOINT_XFER_ISOC, |
| 141 | /* |
| 142 | * The wMaxPacketSize and bInterval values will be initialized from |
| 143 | * module parameters. |
| 144 | */ |
| 145 | }; |
| 146 | |
| 147 | static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { |
| 148 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 149 | .bDescriptorType = USB_DT_ENDPOINT, |
| 150 | .bEndpointAddress = USB_DIR_IN, |
| 151 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
| 152 | | USB_ENDPOINT_XFER_ISOC, |
| 153 | /* |
| 154 | * The wMaxPacketSize and bInterval values will be initialized from |
| 155 | * module parameters. |
| 156 | */ |
| 157 | }; |
| 158 | |
| 159 | static struct usb_endpoint_descriptor uvc_ss_streaming_ep = { |
| 160 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 161 | .bDescriptorType = USB_DT_ENDPOINT, |
| 162 | |
| 163 | .bEndpointAddress = USB_DIR_IN, |
| 164 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
| 165 | | USB_ENDPOINT_XFER_ISOC, |
| 166 | /* |
| 167 | * The wMaxPacketSize and bInterval values will be initialized from |
| 168 | * module parameters. |
| 169 | */ |
| 170 | }; |
| 171 | |
| 172 | static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { |
| 173 | .bLength = sizeof(uvc_ss_streaming_comp), |
| 174 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 175 | /* |
| 176 | * The bMaxBurst, bmAttributes and wBytesPerInterval values will be |
| 177 | * initialized from module parameters. |
| 178 | */ |
| 179 | }; |
| 180 | |
| 181 | static const struct usb_descriptor_header * const uvc_fs_streaming[] = { |
| 182 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, |
| 183 | (struct usb_descriptor_header *) &uvc_fs_streaming_ep, |
| 184 | NULL, |
| 185 | }; |
| 186 | |
| 187 | static const struct usb_descriptor_header * const uvc_hs_streaming[] = { |
| 188 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, |
| 189 | (struct usb_descriptor_header *) &uvc_hs_streaming_ep, |
| 190 | NULL, |
| 191 | }; |
| 192 | |
| 193 | static const struct usb_descriptor_header * const uvc_ss_streaming[] = { |
| 194 | (struct usb_descriptor_header *) &uvc_streaming_intf_alt1, |
| 195 | (struct usb_descriptor_header *) &uvc_ss_streaming_ep, |
| 196 | (struct usb_descriptor_header *) &uvc_ss_streaming_comp, |
| 197 | NULL, |
| 198 | }; |
| 199 | |
| 200 | /* -------------------------------------------------------------------------- |
| 201 | * Control requests |
| 202 | */ |
| 203 | |
| 204 | static void |
| 205 | uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req) |
| 206 | { |
| 207 | struct uvc_device *uvc = req->context; |
| 208 | struct v4l2_event v4l2_event; |
| 209 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 210 | |
| 211 | if (uvc->event_setup_out) { |
| 212 | uvc->event_setup_out = 0; |
| 213 | |
| 214 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 215 | v4l2_event.type = UVC_EVENT_DATA; |
| 216 | uvc_event->data.length = min_t(unsigned int, req->actual, |
| 217 | sizeof(uvc_event->data.data)); |
| 218 | memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length); |
| 219 | v4l2_event_queue(vdev: &uvc->vdev, ev: &v4l2_event); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static int |
| 224 | uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 225 | { |
| 226 | struct uvc_device *uvc = to_uvc(f); |
| 227 | struct v4l2_event v4l2_event; |
| 228 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 229 | unsigned int interface = le16_to_cpu(ctrl->wIndex) & 0xff; |
| 230 | struct usb_ctrlrequest *mctrl; |
| 231 | |
| 232 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { |
| 233 | uvcg_info(f, "invalid request type\n" ); |
| 234 | return -EINVAL; |
| 235 | } |
| 236 | |
| 237 | /* Stall too big requests. */ |
| 238 | if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | /* |
| 242 | * Tell the complete callback to generate an event for the next request |
| 243 | * that will be enqueued by UVCIOC_SEND_RESPONSE. |
| 244 | */ |
| 245 | uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN); |
| 246 | uvc->event_length = le16_to_cpu(ctrl->wLength); |
| 247 | |
| 248 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 249 | v4l2_event.type = UVC_EVENT_SETUP; |
| 250 | memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req)); |
| 251 | |
| 252 | /* check for the interface number, fixup the interface number in |
| 253 | * the ctrl request so the userspace doesn't have to bother with |
| 254 | * offset and configfs parsing |
| 255 | */ |
| 256 | mctrl = &uvc_event->req; |
| 257 | mctrl->wIndex &= ~cpu_to_le16(0xff); |
| 258 | if (interface == uvc->streaming_intf) |
| 259 | mctrl->wIndex = cpu_to_le16(UVC_STRING_STREAMING_IDX); |
| 260 | |
| 261 | v4l2_event_queue(vdev: &uvc->vdev, ev: &v4l2_event); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep) |
| 267 | { |
| 268 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 269 | |
| 270 | if (disable_ep && uvc->video.ep) |
| 271 | usb_ep_disable(ep: uvc->video.ep); |
| 272 | |
| 273 | usb_composite_setup_continue(cdev); |
| 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 | uvcg_info(f, "%s(%u)\n" , __func__, 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->video.ep->enabled ? 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 usb_composite_dev *cdev = f->config->cdev; |
| 296 | struct v4l2_event v4l2_event; |
| 297 | struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; |
| 298 | int ret; |
| 299 | |
| 300 | uvcg_info(f, "%s(%u, %u)\n" , __func__, interface, alt); |
| 301 | |
| 302 | if (interface == uvc->control_intf) { |
| 303 | if (alt) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | if (uvc->enable_interrupt_ep) { |
| 307 | uvcg_info(f, "reset UVC interrupt endpoint\n" ); |
| 308 | usb_ep_disable(ep: uvc->interrupt_ep); |
| 309 | |
| 310 | if (!uvc->interrupt_ep->desc) |
| 311 | if (config_ep_by_speed(g: cdev->gadget, f, |
| 312 | ep: uvc->interrupt_ep)) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | usb_ep_enable(ep: uvc->interrupt_ep); |
| 316 | } |
| 317 | |
| 318 | if (uvc->state == UVC_STATE_DISCONNECTED) { |
| 319 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 320 | v4l2_event.type = UVC_EVENT_CONNECT; |
| 321 | uvc_event->speed = cdev->gadget->speed; |
| 322 | v4l2_event_queue(vdev: &uvc->vdev, ev: &v4l2_event); |
| 323 | |
| 324 | uvc->state = UVC_STATE_CONNECTED; |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | if (interface != uvc->streaming_intf) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | /* TODO |
| 334 | if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep)) |
| 335 | return alt ? -EINVAL : 0; |
| 336 | */ |
| 337 | |
| 338 | switch (alt) { |
| 339 | case 0: |
| 340 | if (uvc->state != UVC_STATE_STREAMING) |
| 341 | return 0; |
| 342 | |
| 343 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 344 | v4l2_event.type = UVC_EVENT_STREAMOFF; |
| 345 | v4l2_event_queue(vdev: &uvc->vdev, ev: &v4l2_event); |
| 346 | |
| 347 | return USB_GADGET_DELAYED_STATUS; |
| 348 | |
| 349 | case 1: |
| 350 | if (uvc->state != UVC_STATE_CONNECTED) |
| 351 | return 0; |
| 352 | |
| 353 | if (!uvc->video.ep) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | uvcg_info(f, "reset UVC\n" ); |
| 357 | usb_ep_disable(ep: uvc->video.ep); |
| 358 | |
| 359 | ret = config_ep_by_speed(g: f->config->cdev->gadget, |
| 360 | f: &(uvc->func), ep: uvc->video.ep); |
| 361 | if (ret) |
| 362 | return ret; |
| 363 | usb_ep_enable(ep: uvc->video.ep); |
| 364 | |
| 365 | uvc->video.max_req_size = uvc->video.ep->maxpacket |
| 366 | * max_t(unsigned int, uvc->video.ep->maxburst, 1) |
| 367 | * (uvc->video.ep->mult); |
| 368 | |
| 369 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 370 | v4l2_event.type = UVC_EVENT_STREAMON; |
| 371 | v4l2_event_queue(vdev: &uvc->vdev, ev: &v4l2_event); |
| 372 | return USB_GADGET_DELAYED_STATUS; |
| 373 | |
| 374 | default: |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | static void |
| 380 | uvc_function_disable(struct usb_function *f) |
| 381 | { |
| 382 | struct uvc_device *uvc = to_uvc(f); |
| 383 | struct v4l2_event v4l2_event; |
| 384 | |
| 385 | uvcg_info(f, "%s()\n" , __func__); |
| 386 | |
| 387 | memset(&v4l2_event, 0, sizeof(v4l2_event)); |
| 388 | v4l2_event.type = UVC_EVENT_DISCONNECT; |
| 389 | v4l2_event_queue(vdev: &uvc->vdev, ev: &v4l2_event); |
| 390 | |
| 391 | uvc->state = UVC_STATE_DISCONNECTED; |
| 392 | |
| 393 | usb_ep_disable(ep: uvc->video.ep); |
| 394 | if (uvc->enable_interrupt_ep) |
| 395 | usb_ep_disable(ep: uvc->interrupt_ep); |
| 396 | } |
| 397 | |
| 398 | /* -------------------------------------------------------------------------- |
| 399 | * Connection / disconnection |
| 400 | */ |
| 401 | |
| 402 | void |
| 403 | uvc_function_connect(struct uvc_device *uvc) |
| 404 | { |
| 405 | int ret; |
| 406 | |
| 407 | if ((ret = usb_function_activate(&uvc->func)) < 0) |
| 408 | uvcg_info(&uvc->func, "UVC connect failed with %d\n" , ret); |
| 409 | } |
| 410 | |
| 411 | void |
| 412 | uvc_function_disconnect(struct uvc_device *uvc) |
| 413 | { |
| 414 | int ret; |
| 415 | |
| 416 | if ((ret = usb_function_deactivate(&uvc->func)) < 0) |
| 417 | uvcg_info(&uvc->func, "UVC disconnect failed with %d\n" , ret); |
| 418 | } |
| 419 | |
| 420 | /* -------------------------------------------------------------------------- |
| 421 | * USB probe and disconnect |
| 422 | */ |
| 423 | |
| 424 | static ssize_t function_name_show(struct device *dev, |
| 425 | struct device_attribute *attr, char *buf) |
| 426 | { |
| 427 | struct uvc_device *uvc = dev_get_drvdata(dev); |
| 428 | |
| 429 | return sprintf(buf, fmt: "%s\n" , uvc->func.fi->group.cg_item.ci_name); |
| 430 | } |
| 431 | |
| 432 | static DEVICE_ATTR_RO(function_name); |
| 433 | |
| 434 | static int |
| 435 | uvc_register_video(struct uvc_device *uvc) |
| 436 | { |
| 437 | struct usb_composite_dev *cdev = uvc->func.config->cdev; |
| 438 | int ret; |
| 439 | |
| 440 | /* TODO reference counting. */ |
| 441 | memset(&uvc->vdev, 0, sizeof(uvc->vdev)); |
| 442 | uvc->vdev.v4l2_dev = &uvc->v4l2_dev; |
| 443 | uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev; |
| 444 | uvc->vdev.fops = &uvc_v4l2_fops; |
| 445 | uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops; |
| 446 | uvc->vdev.release = video_device_release_empty; |
| 447 | uvc->vdev.vfl_dir = VFL_DIR_TX; |
| 448 | uvc->vdev.lock = &uvc->video.mutex; |
| 449 | uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; |
| 450 | strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name)); |
| 451 | |
| 452 | video_set_drvdata(vdev: &uvc->vdev, data: uvc); |
| 453 | |
| 454 | ret = video_register_device(vdev: &uvc->vdev, type: VFL_TYPE_VIDEO, nr: -1); |
| 455 | if (ret < 0) |
| 456 | return ret; |
| 457 | |
| 458 | ret = device_create_file(device: &uvc->vdev.dev, entry: &dev_attr_function_name); |
| 459 | if (ret < 0) { |
| 460 | video_unregister_device(vdev: &uvc->vdev); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \ |
| 468 | do { \ |
| 469 | memcpy(mem, desc, (desc)->bLength); \ |
| 470 | *(dst)++ = mem; \ |
| 471 | mem += (desc)->bLength; \ |
| 472 | } while (0) |
| 473 | |
| 474 | #define UVC_COPY_DESCRIPTORS(mem, dst, src) \ |
| 475 | do { \ |
| 476 | const struct usb_descriptor_header * const *__src; \ |
| 477 | for (__src = src; *__src; ++__src) { \ |
| 478 | memcpy(mem, *__src, (*__src)->bLength); \ |
| 479 | *dst++ = mem; \ |
| 480 | mem += (*__src)->bLength; \ |
| 481 | } \ |
| 482 | } while (0) |
| 483 | |
| 484 | #define UVC_COPY_XU_DESCRIPTOR(mem, dst, desc) \ |
| 485 | do { \ |
| 486 | *(dst)++ = mem; \ |
| 487 | memcpy(mem, desc, 22); /* bLength to bNrInPins */ \ |
| 488 | mem += 22; \ |
| 489 | \ |
| 490 | memcpy(mem, (desc)->baSourceID, (desc)->bNrInPins); \ |
| 491 | mem += (desc)->bNrInPins; \ |
| 492 | \ |
| 493 | memcpy(mem, &(desc)->bControlSize, 1); \ |
| 494 | mem++; \ |
| 495 | \ |
| 496 | memcpy(mem, (desc)->bmControls, (desc)->bControlSize); \ |
| 497 | mem += (desc)->bControlSize; \ |
| 498 | \ |
| 499 | memcpy(mem, &(desc)->iExtension, 1); \ |
| 500 | mem++; \ |
| 501 | } while (0) |
| 502 | |
| 503 | static struct usb_descriptor_header ** |
| 504 | uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed) |
| 505 | { |
| 506 | struct uvc_input_header_descriptor *; |
| 507 | struct uvc_header_descriptor *; |
| 508 | const struct uvc_descriptor_header * const *uvc_control_desc; |
| 509 | const struct uvc_descriptor_header * const *uvc_streaming_cls; |
| 510 | const struct usb_descriptor_header * const *uvc_streaming_std; |
| 511 | const struct usb_descriptor_header * const *src; |
| 512 | struct usb_descriptor_header **dst; |
| 513 | struct usb_descriptor_header **hdr; |
| 514 | struct uvcg_extension *xu; |
| 515 | unsigned int control_size; |
| 516 | unsigned int streaming_size; |
| 517 | unsigned int n_desc; |
| 518 | unsigned int bytes; |
| 519 | void *mem; |
| 520 | |
| 521 | switch (speed) { |
| 522 | case USB_SPEED_SUPER_PLUS: |
| 523 | case USB_SPEED_SUPER: |
| 524 | uvc_control_desc = uvc->desc.ss_control; |
| 525 | uvc_streaming_cls = uvc->desc.ss_streaming; |
| 526 | uvc_streaming_std = uvc_ss_streaming; |
| 527 | break; |
| 528 | |
| 529 | case USB_SPEED_HIGH: |
| 530 | uvc_control_desc = uvc->desc.fs_control; |
| 531 | uvc_streaming_cls = uvc->desc.hs_streaming; |
| 532 | uvc_streaming_std = uvc_hs_streaming; |
| 533 | break; |
| 534 | |
| 535 | case USB_SPEED_FULL: |
| 536 | default: |
| 537 | uvc_control_desc = uvc->desc.fs_control; |
| 538 | uvc_streaming_cls = uvc->desc.fs_streaming; |
| 539 | uvc_streaming_std = uvc_fs_streaming; |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | if (!uvc_control_desc || !uvc_streaming_cls) |
| 544 | return ERR_PTR(error: -ENODEV); |
| 545 | |
| 546 | /* |
| 547 | * Descriptors layout |
| 548 | * |
| 549 | * uvc_iad |
| 550 | * uvc_control_intf |
| 551 | * Class-specific UVC control descriptors |
| 552 | * uvc_interrupt_ep |
| 553 | * uvc_interrupt_cs_ep |
| 554 | * uvc_ss_interrupt_comp (for SS only) |
| 555 | * uvc_streaming_intf_alt0 |
| 556 | * Class-specific UVC streaming descriptors |
| 557 | * uvc_{fs|hs}_streaming |
| 558 | */ |
| 559 | |
| 560 | /* Count descriptors and compute their size. */ |
| 561 | control_size = 0; |
| 562 | streaming_size = 0; |
| 563 | bytes = uvc_iad.bLength + uvc_control_intf.bLength |
| 564 | + uvc_streaming_intf_alt0.bLength; |
| 565 | |
| 566 | n_desc = 3; |
| 567 | if (uvc->enable_interrupt_ep) { |
| 568 | bytes += uvc_interrupt_ep.bLength + uvc_interrupt_cs_ep.bLength; |
| 569 | n_desc += 2; |
| 570 | |
| 571 | if (speed == USB_SPEED_SUPER || |
| 572 | speed == USB_SPEED_SUPER_PLUS) { |
| 573 | bytes += uvc_ss_interrupt_comp.bLength; |
| 574 | n_desc += 1; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | for (src = (const struct usb_descriptor_header **)uvc_control_desc; |
| 579 | *src; ++src) { |
| 580 | control_size += (*src)->bLength; |
| 581 | bytes += (*src)->bLength; |
| 582 | n_desc++; |
| 583 | } |
| 584 | |
| 585 | list_for_each_entry(xu, uvc->desc.extension_units, list) { |
| 586 | control_size += xu->desc.bLength; |
| 587 | bytes += xu->desc.bLength; |
| 588 | n_desc++; |
| 589 | } |
| 590 | |
| 591 | for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; |
| 592 | *src; ++src) { |
| 593 | streaming_size += (*src)->bLength; |
| 594 | bytes += (*src)->bLength; |
| 595 | n_desc++; |
| 596 | } |
| 597 | for (src = uvc_streaming_std; *src; ++src) { |
| 598 | bytes += (*src)->bLength; |
| 599 | n_desc++; |
| 600 | } |
| 601 | |
| 602 | mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL); |
| 603 | if (mem == NULL) |
| 604 | return NULL; |
| 605 | |
| 606 | hdr = mem; |
| 607 | dst = mem; |
| 608 | mem += (n_desc + 1) * sizeof(*src); |
| 609 | |
| 610 | /* Copy the descriptors. */ |
| 611 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad); |
| 612 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf); |
| 613 | |
| 614 | uvc_control_header = mem; |
| 615 | UVC_COPY_DESCRIPTORS(mem, dst, |
| 616 | (const struct usb_descriptor_header **)uvc_control_desc); |
| 617 | |
| 618 | list_for_each_entry(xu, uvc->desc.extension_units, list) |
| 619 | UVC_COPY_XU_DESCRIPTOR(mem, dst, &xu->desc); |
| 620 | |
| 621 | uvc_control_header->wTotalLength = cpu_to_le16(control_size); |
| 622 | uvc_control_header->bInCollection = 1; |
| 623 | uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; |
| 624 | |
| 625 | if (uvc->enable_interrupt_ep) { |
| 626 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_interrupt_ep); |
| 627 | if (speed == USB_SPEED_SUPER || |
| 628 | speed == USB_SPEED_SUPER_PLUS) |
| 629 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_interrupt_comp); |
| 630 | |
| 631 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_interrupt_cs_ep); |
| 632 | } |
| 633 | |
| 634 | UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0); |
| 635 | |
| 636 | uvc_streaming_header = mem; |
| 637 | UVC_COPY_DESCRIPTORS(mem, dst, |
| 638 | (const struct usb_descriptor_header**)uvc_streaming_cls); |
| 639 | uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); |
| 640 | uvc_streaming_header->bEndpointAddress = uvc->video.ep->address; |
| 641 | |
| 642 | UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); |
| 643 | |
| 644 | *dst = NULL; |
| 645 | return hdr; |
| 646 | } |
| 647 | |
| 648 | static int |
| 649 | uvc_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 650 | { |
| 651 | struct usb_composite_dev *cdev = c->cdev; |
| 652 | struct uvc_device *uvc = to_uvc(f); |
| 653 | struct uvcg_extension *xu; |
| 654 | struct usb_string *us; |
| 655 | unsigned int max_packet_mult; |
| 656 | unsigned int max_packet_size; |
| 657 | struct usb_ep *ep; |
| 658 | struct f_uvc_opts *opts; |
| 659 | int ret = -EINVAL; |
| 660 | |
| 661 | uvcg_info(f, "%s()\n" , __func__); |
| 662 | |
| 663 | opts = fi_to_f_uvc_opts(f->fi); |
| 664 | /* Sanity check the streaming endpoint module parameters. */ |
| 665 | opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U); |
| 666 | opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U); |
| 667 | opts->streaming_maxburst = min(opts->streaming_maxburst, 15U); |
| 668 | |
| 669 | /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */ |
| 670 | if (opts->streaming_maxburst && |
| 671 | (opts->streaming_maxpacket % 1024) != 0) { |
| 672 | opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024); |
| 673 | uvcg_info(f, "overriding streaming_maxpacket to %d\n" , |
| 674 | opts->streaming_maxpacket); |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Fill in the FS/HS/SS Video Streaming specific descriptors from the |
| 679 | * module parameters. |
| 680 | * |
| 681 | * NOTE: We assume that the user knows what they are doing and won't |
| 682 | * give parameters that their UDC doesn't support. |
| 683 | */ |
| 684 | if (opts->streaming_maxpacket <= 1024) { |
| 685 | max_packet_mult = 1; |
| 686 | max_packet_size = opts->streaming_maxpacket; |
| 687 | } else if (opts->streaming_maxpacket <= 2048) { |
| 688 | max_packet_mult = 2; |
| 689 | max_packet_size = opts->streaming_maxpacket / 2; |
| 690 | } else { |
| 691 | max_packet_mult = 3; |
| 692 | max_packet_size = opts->streaming_maxpacket / 3; |
| 693 | } |
| 694 | |
| 695 | uvc_fs_streaming_ep.wMaxPacketSize = |
| 696 | cpu_to_le16(min(opts->streaming_maxpacket, 1023U)); |
| 697 | uvc_fs_streaming_ep.bInterval = opts->streaming_interval; |
| 698 | |
| 699 | uvc_hs_streaming_ep.wMaxPacketSize = |
| 700 | cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11)); |
| 701 | |
| 702 | /* A high-bandwidth endpoint must specify a bInterval value of 1 */ |
| 703 | if (max_packet_mult > 1) |
| 704 | uvc_hs_streaming_ep.bInterval = 1; |
| 705 | else |
| 706 | uvc_hs_streaming_ep.bInterval = opts->streaming_interval; |
| 707 | |
| 708 | uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size); |
| 709 | uvc_ss_streaming_ep.bInterval = opts->streaming_interval; |
| 710 | uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1; |
| 711 | uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst; |
| 712 | uvc_ss_streaming_comp.wBytesPerInterval = |
| 713 | cpu_to_le16(max_packet_size * max_packet_mult * |
| 714 | (opts->streaming_maxburst + 1)); |
| 715 | |
| 716 | /* Allocate endpoints. */ |
| 717 | if (opts->enable_interrupt_ep) { |
| 718 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_interrupt_ep); |
| 719 | if (!ep) { |
| 720 | uvcg_info(f, "Unable to allocate interrupt EP\n" ); |
| 721 | goto error; |
| 722 | } |
| 723 | uvc->interrupt_ep = ep; |
| 724 | uvc_control_intf.bNumEndpoints = 1; |
| 725 | } |
| 726 | uvc->enable_interrupt_ep = opts->enable_interrupt_ep; |
| 727 | |
| 728 | /* |
| 729 | * gadget_is_{super|dual}speed() API check UDC controller capitblity. It should pass down |
| 730 | * highest speed endpoint descriptor to UDC controller. So UDC controller driver can reserve |
| 731 | * enough resource at check_config(), especially mult and maxburst. So UDC driver (such as |
| 732 | * cdns3) can know need at least (mult + 1) * (maxburst + 1) * wMaxPacketSize internal |
| 733 | * memory for this uvc functions. This is the only straightforward method to resolve the UDC |
| 734 | * resource allocation issue in the current gadget framework. |
| 735 | */ |
| 736 | if (gadget_is_superspeed(g: c->cdev->gadget)) |
| 737 | ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep, |
| 738 | &uvc_ss_streaming_comp); |
| 739 | else if (gadget_is_dualspeed(g: cdev->gadget)) |
| 740 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep); |
| 741 | else |
| 742 | ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); |
| 743 | |
| 744 | if (!ep) { |
| 745 | uvcg_info(f, "Unable to allocate streaming EP\n" ); |
| 746 | goto error; |
| 747 | } |
| 748 | uvc->video.ep = ep; |
| 749 | |
| 750 | uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 751 | uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 752 | uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address; |
| 753 | |
| 754 | /* |
| 755 | * XUs can have an arbitrary string descriptor describing them. If they |
| 756 | * have one pick up the ID. |
| 757 | */ |
| 758 | list_for_each_entry(xu, &opts->extension_units, list) |
| 759 | if (xu->string_descriptor_index) |
| 760 | xu->desc.iExtension = cdev->usb_strings[xu->string_descriptor_index].id; |
| 761 | |
| 762 | /* |
| 763 | * We attach the hard-coded defaults incase the user does not provide |
| 764 | * any more appropriate strings through configfs. |
| 765 | */ |
| 766 | uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name; |
| 767 | us = usb_gstrings_attach(cdev, sp: uvc_function_strings, |
| 768 | ARRAY_SIZE(uvc_en_us_strings)); |
| 769 | if (IS_ERR(ptr: us)) { |
| 770 | ret = PTR_ERR(ptr: us); |
| 771 | goto error; |
| 772 | } |
| 773 | |
| 774 | uvc_iad.iFunction = opts->iad_index ? cdev->usb_strings[opts->iad_index].id : |
| 775 | us[UVC_STRING_CONTROL_IDX].id; |
| 776 | uvc_streaming_intf_alt0.iInterface = opts->vs0_index ? |
| 777 | cdev->usb_strings[opts->vs0_index].id : |
| 778 | us[UVC_STRING_STREAMING_IDX].id; |
| 779 | uvc_streaming_intf_alt1.iInterface = opts->vs1_index ? |
| 780 | cdev->usb_strings[opts->vs1_index].id : |
| 781 | us[UVC_STRING_STREAMING_IDX].id; |
| 782 | |
| 783 | /* Allocate interface IDs. */ |
| 784 | if ((ret = usb_interface_id(c, f)) < 0) |
| 785 | goto error; |
| 786 | uvc_iad.bFirstInterface = ret; |
| 787 | uvc_control_intf.bInterfaceNumber = ret; |
| 788 | uvc->control_intf = ret; |
| 789 | opts->control_interface = ret; |
| 790 | |
| 791 | if ((ret = usb_interface_id(c, f)) < 0) |
| 792 | goto error; |
| 793 | uvc_streaming_intf_alt0.bInterfaceNumber = ret; |
| 794 | uvc_streaming_intf_alt1.bInterfaceNumber = ret; |
| 795 | uvc->streaming_intf = ret; |
| 796 | opts->streaming_interface = ret; |
| 797 | |
| 798 | /* Copy descriptors */ |
| 799 | f->fs_descriptors = uvc_copy_descriptors(uvc, speed: USB_SPEED_FULL); |
| 800 | if (IS_ERR(ptr: f->fs_descriptors)) { |
| 801 | ret = PTR_ERR(ptr: f->fs_descriptors); |
| 802 | f->fs_descriptors = NULL; |
| 803 | goto error; |
| 804 | } |
| 805 | |
| 806 | f->hs_descriptors = uvc_copy_descriptors(uvc, speed: USB_SPEED_HIGH); |
| 807 | if (IS_ERR(ptr: f->hs_descriptors)) { |
| 808 | ret = PTR_ERR(ptr: f->hs_descriptors); |
| 809 | f->hs_descriptors = NULL; |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | f->ss_descriptors = uvc_copy_descriptors(uvc, speed: USB_SPEED_SUPER); |
| 814 | if (IS_ERR(ptr: f->ss_descriptors)) { |
| 815 | ret = PTR_ERR(ptr: f->ss_descriptors); |
| 816 | f->ss_descriptors = NULL; |
| 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | f->ssp_descriptors = uvc_copy_descriptors(uvc, speed: USB_SPEED_SUPER_PLUS); |
| 821 | if (IS_ERR(ptr: f->ssp_descriptors)) { |
| 822 | ret = PTR_ERR(ptr: f->ssp_descriptors); |
| 823 | f->ssp_descriptors = NULL; |
| 824 | goto error; |
| 825 | } |
| 826 | |
| 827 | /* Preallocate control endpoint request. */ |
| 828 | uvc->control_req = usb_ep_alloc_request(ep: cdev->gadget->ep0, GFP_KERNEL); |
| 829 | uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL); |
| 830 | if (uvc->control_req == NULL || uvc->control_buf == NULL) { |
| 831 | ret = -ENOMEM; |
| 832 | goto error; |
| 833 | } |
| 834 | |
| 835 | uvc->control_req->buf = uvc->control_buf; |
| 836 | uvc->control_req->complete = uvc_function_ep0_complete; |
| 837 | uvc->control_req->context = uvc; |
| 838 | |
| 839 | if (v4l2_device_register(dev: &cdev->gadget->dev, v4l2_dev: &uvc->v4l2_dev)) { |
| 840 | uvcg_err(f, "failed to register V4L2 device\n" ); |
| 841 | goto error; |
| 842 | } |
| 843 | |
| 844 | /* Initialise video. */ |
| 845 | ret = uvcg_video_init(video: &uvc->video, uvc); |
| 846 | if (ret < 0) |
| 847 | goto v4l2_error; |
| 848 | |
| 849 | /* Register a V4L2 device. */ |
| 850 | ret = uvc_register_video(uvc); |
| 851 | if (ret < 0) { |
| 852 | uvcg_err(f, "failed to register video device\n" ); |
| 853 | goto v4l2_error; |
| 854 | } |
| 855 | |
| 856 | return 0; |
| 857 | |
| 858 | v4l2_error: |
| 859 | v4l2_device_unregister(v4l2_dev: &uvc->v4l2_dev); |
| 860 | error: |
| 861 | if (uvc->control_req) |
| 862 | usb_ep_free_request(ep: cdev->gadget->ep0, req: uvc->control_req); |
| 863 | kfree(objp: uvc->control_buf); |
| 864 | |
| 865 | usb_free_all_descriptors(f); |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | /* -------------------------------------------------------------------------- |
| 870 | * USB gadget function |
| 871 | */ |
| 872 | |
| 873 | static void uvc_free_inst(struct usb_function_instance *f) |
| 874 | { |
| 875 | struct f_uvc_opts *opts = fi_to_f_uvc_opts(f); |
| 876 | |
| 877 | mutex_destroy(lock: &opts->lock); |
| 878 | kfree(objp: opts); |
| 879 | } |
| 880 | |
| 881 | static struct usb_function_instance *uvc_alloc_inst(void) |
| 882 | { |
| 883 | struct f_uvc_opts *opts; |
| 884 | struct uvc_camera_terminal_descriptor *cd; |
| 885 | struct uvc_processing_unit_descriptor *pd; |
| 886 | struct uvc_output_terminal_descriptor *od; |
| 887 | struct uvc_descriptor_header **ctl_cls; |
| 888 | int ret; |
| 889 | |
| 890 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 891 | if (!opts) |
| 892 | return ERR_PTR(error: -ENOMEM); |
| 893 | opts->func_inst.free_func_inst = uvc_free_inst; |
| 894 | mutex_init(&opts->lock); |
| 895 | |
| 896 | cd = &opts->uvc_camera_terminal; |
| 897 | cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3); |
| 898 | cd->bDescriptorType = USB_DT_CS_INTERFACE; |
| 899 | cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL; |
| 900 | cd->bTerminalID = 1; |
| 901 | cd->wTerminalType = cpu_to_le16(0x0201); |
| 902 | cd->bAssocTerminal = 0; |
| 903 | cd->iTerminal = 0; |
| 904 | cd->wObjectiveFocalLengthMin = cpu_to_le16(0); |
| 905 | cd->wObjectiveFocalLengthMax = cpu_to_le16(0); |
| 906 | cd->wOcularFocalLength = cpu_to_le16(0); |
| 907 | cd->bControlSize = 3; |
| 908 | cd->bmControls[0] = 2; |
| 909 | cd->bmControls[1] = 0; |
| 910 | cd->bmControls[2] = 0; |
| 911 | |
| 912 | pd = &opts->uvc_processing; |
| 913 | pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2); |
| 914 | pd->bDescriptorType = USB_DT_CS_INTERFACE; |
| 915 | pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT; |
| 916 | pd->bUnitID = 2; |
| 917 | pd->bSourceID = 1; |
| 918 | pd->wMaxMultiplier = cpu_to_le16(16*1024); |
| 919 | pd->bControlSize = 2; |
| 920 | pd->bmControls[0] = 1; |
| 921 | pd->bmControls[1] = 0; |
| 922 | pd->iProcessing = 0; |
| 923 | pd->bmVideoStandards = 0; |
| 924 | |
| 925 | od = &opts->uvc_output_terminal; |
| 926 | od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE; |
| 927 | od->bDescriptorType = USB_DT_CS_INTERFACE; |
| 928 | od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL; |
| 929 | od->bTerminalID = 3; |
| 930 | od->wTerminalType = cpu_to_le16(0x0101); |
| 931 | od->bAssocTerminal = 0; |
| 932 | od->bSourceID = 2; |
| 933 | od->iTerminal = 0; |
| 934 | |
| 935 | /* |
| 936 | * With the ability to add XUs to the UVC function graph, we need to be |
| 937 | * able to allocate unique unit IDs to them. The IDs are 1-based, with |
| 938 | * the CT, PU and OT above consuming the first 3. |
| 939 | */ |
| 940 | opts->last_unit_id = 3; |
| 941 | |
| 942 | /* Prepare fs control class descriptors for configfs-based gadgets */ |
| 943 | ctl_cls = opts->uvc_fs_control_cls; |
| 944 | ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ |
| 945 | ctl_cls[1] = (struct uvc_descriptor_header *)cd; |
| 946 | ctl_cls[2] = (struct uvc_descriptor_header *)pd; |
| 947 | ctl_cls[3] = (struct uvc_descriptor_header *)od; |
| 948 | ctl_cls[4] = NULL; /* NULL-terminate */ |
| 949 | opts->fs_control = |
| 950 | (const struct uvc_descriptor_header * const *)ctl_cls; |
| 951 | |
| 952 | /* Prepare hs control class descriptors for configfs-based gadgets */ |
| 953 | ctl_cls = opts->uvc_ss_control_cls; |
| 954 | ctl_cls[0] = NULL; /* assigned elsewhere by configfs */ |
| 955 | ctl_cls[1] = (struct uvc_descriptor_header *)cd; |
| 956 | ctl_cls[2] = (struct uvc_descriptor_header *)pd; |
| 957 | ctl_cls[3] = (struct uvc_descriptor_header *)od; |
| 958 | ctl_cls[4] = NULL; /* NULL-terminate */ |
| 959 | opts->ss_control = |
| 960 | (const struct uvc_descriptor_header * const *)ctl_cls; |
| 961 | |
| 962 | INIT_LIST_HEAD(list: &opts->extension_units); |
| 963 | |
| 964 | opts->streaming_interval = 1; |
| 965 | opts->streaming_maxpacket = 1024; |
| 966 | snprintf(buf: opts->function_name, size: sizeof(opts->function_name), fmt: "UVC Camera" ); |
| 967 | |
| 968 | ret = uvcg_attach_configfs(opts); |
| 969 | if (ret < 0) { |
| 970 | kfree(objp: opts); |
| 971 | return ERR_PTR(error: ret); |
| 972 | } |
| 973 | |
| 974 | return &opts->func_inst; |
| 975 | } |
| 976 | |
| 977 | static void uvc_free(struct usb_function *f) |
| 978 | { |
| 979 | struct uvc_device *uvc = to_uvc(f); |
| 980 | struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts, |
| 981 | func_inst); |
| 982 | if (!opts->header) |
| 983 | config_item_put(&uvc->header->item); |
| 984 | --opts->refcnt; |
| 985 | kfree(objp: uvc); |
| 986 | } |
| 987 | |
| 988 | static void uvc_function_unbind(struct usb_configuration *c, |
| 989 | struct usb_function *f) |
| 990 | { |
| 991 | struct usb_composite_dev *cdev = c->cdev; |
| 992 | struct uvc_device *uvc = to_uvc(f); |
| 993 | struct uvc_video *video = &uvc->video; |
| 994 | long wait_ret = 1; |
| 995 | |
| 996 | uvcg_info(f, "%s()\n" , __func__); |
| 997 | |
| 998 | kthread_cancel_work_sync(work: &video->hw_submit); |
| 999 | |
| 1000 | if (video->async_wq) |
| 1001 | destroy_workqueue(wq: video->async_wq); |
| 1002 | |
| 1003 | /* |
| 1004 | * If we know we're connected via v4l2, then there should be a cleanup |
| 1005 | * of the device from userspace either via UVC_EVENT_DISCONNECT or |
| 1006 | * though the video device removal uevent. Allow some time for the |
| 1007 | * application to close out before things get deleted. |
| 1008 | */ |
| 1009 | if (uvc->func_connected) { |
| 1010 | uvcg_dbg(f, "waiting for clean disconnect\n" ); |
| 1011 | wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue, |
| 1012 | uvc->func_connected == false, msecs_to_jiffies(500)); |
| 1013 | uvcg_dbg(f, "done waiting with ret: %ld\n" , wait_ret); |
| 1014 | } |
| 1015 | |
| 1016 | device_remove_file(dev: &uvc->vdev.dev, attr: &dev_attr_function_name); |
| 1017 | video_unregister_device(vdev: &uvc->vdev); |
| 1018 | v4l2_device_unregister(v4l2_dev: &uvc->v4l2_dev); |
| 1019 | |
| 1020 | if (uvc->func_connected) { |
| 1021 | /* |
| 1022 | * Wait for the release to occur to ensure there are no longer any |
| 1023 | * pending operations that may cause panics when resources are cleaned |
| 1024 | * up. |
| 1025 | */ |
| 1026 | uvcg_warn(f, "%s no clean disconnect, wait for release\n" , __func__); |
| 1027 | wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue, |
| 1028 | uvc->func_connected == false, msecs_to_jiffies(1000)); |
| 1029 | uvcg_dbg(f, "done waiting for release with ret: %ld\n" , wait_ret); |
| 1030 | } |
| 1031 | |
| 1032 | usb_ep_free_request(ep: cdev->gadget->ep0, req: uvc->control_req); |
| 1033 | kfree(objp: uvc->control_buf); |
| 1034 | |
| 1035 | usb_free_all_descriptors(f); |
| 1036 | } |
| 1037 | |
| 1038 | static struct usb_function *uvc_alloc(struct usb_function_instance *fi) |
| 1039 | { |
| 1040 | struct uvc_device *uvc; |
| 1041 | struct f_uvc_opts *opts; |
| 1042 | struct uvc_descriptor_header **strm_cls; |
| 1043 | struct config_item *streaming, *, *h; |
| 1044 | |
| 1045 | uvc = kzalloc(sizeof(*uvc), GFP_KERNEL); |
| 1046 | if (uvc == NULL) |
| 1047 | return ERR_PTR(error: -ENOMEM); |
| 1048 | |
| 1049 | mutex_init(&uvc->video.mutex); |
| 1050 | uvc->state = UVC_STATE_DISCONNECTED; |
| 1051 | init_waitqueue_head(&uvc->func_connected_queue); |
| 1052 | opts = fi_to_f_uvc_opts(fi); |
| 1053 | |
| 1054 | mutex_lock(&opts->lock); |
| 1055 | if (opts->uvc_fs_streaming_cls) { |
| 1056 | strm_cls = opts->uvc_fs_streaming_cls; |
| 1057 | opts->fs_streaming = |
| 1058 | (const struct uvc_descriptor_header * const *)strm_cls; |
| 1059 | } |
| 1060 | if (opts->uvc_hs_streaming_cls) { |
| 1061 | strm_cls = opts->uvc_hs_streaming_cls; |
| 1062 | opts->hs_streaming = |
| 1063 | (const struct uvc_descriptor_header * const *)strm_cls; |
| 1064 | } |
| 1065 | if (opts->uvc_ss_streaming_cls) { |
| 1066 | strm_cls = opts->uvc_ss_streaming_cls; |
| 1067 | opts->ss_streaming = |
| 1068 | (const struct uvc_descriptor_header * const *)strm_cls; |
| 1069 | } |
| 1070 | |
| 1071 | uvc->desc.fs_control = opts->fs_control; |
| 1072 | uvc->desc.ss_control = opts->ss_control; |
| 1073 | uvc->desc.fs_streaming = opts->fs_streaming; |
| 1074 | uvc->desc.hs_streaming = opts->hs_streaming; |
| 1075 | uvc->desc.ss_streaming = opts->ss_streaming; |
| 1076 | |
| 1077 | if (opts->header) { |
| 1078 | uvc->header = opts->header; |
| 1079 | } else { |
| 1080 | streaming = config_group_find_item(&opts->func_inst.group, "streaming" ); |
| 1081 | if (!streaming) |
| 1082 | goto err_config; |
| 1083 | |
| 1084 | header = config_group_find_item(to_config_group(item: streaming), "header" ); |
| 1085 | config_item_put(streaming); |
| 1086 | if (!header) |
| 1087 | goto err_config; |
| 1088 | |
| 1089 | h = config_group_find_item(to_config_group(item: header), "h" ); |
| 1090 | config_item_put(header); |
| 1091 | if (!h) |
| 1092 | goto err_config; |
| 1093 | |
| 1094 | uvc->header = to_uvcg_streaming_header(item: h); |
| 1095 | if (!uvc->header->linked) { |
| 1096 | mutex_unlock(lock: &opts->lock); |
| 1097 | kfree(objp: uvc); |
| 1098 | return ERR_PTR(error: -EBUSY); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | uvc->desc.extension_units = &opts->extension_units; |
| 1103 | |
| 1104 | ++opts->refcnt; |
| 1105 | mutex_unlock(lock: &opts->lock); |
| 1106 | |
| 1107 | /* Register the function. */ |
| 1108 | uvc->func.name = "uvc" ; |
| 1109 | uvc->func.bind = uvc_function_bind; |
| 1110 | uvc->func.unbind = uvc_function_unbind; |
| 1111 | uvc->func.get_alt = uvc_function_get_alt; |
| 1112 | uvc->func.set_alt = uvc_function_set_alt; |
| 1113 | uvc->func.disable = uvc_function_disable; |
| 1114 | uvc->func.setup = uvc_function_setup; |
| 1115 | uvc->func.free_func = uvc_free; |
| 1116 | uvc->func.bind_deactivated = true; |
| 1117 | |
| 1118 | return &uvc->func; |
| 1119 | |
| 1120 | err_config: |
| 1121 | mutex_unlock(lock: &opts->lock); |
| 1122 | kfree(objp: uvc); |
| 1123 | return ERR_PTR(error: -ENOENT); |
| 1124 | } |
| 1125 | |
| 1126 | DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc); |
| 1127 | MODULE_DESCRIPTION("USB Video Class Gadget driver" ); |
| 1128 | MODULE_LICENSE("GPL" ); |
| 1129 | MODULE_AUTHOR("Laurent Pinchart" ); |
| 1130 | |