hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/usb/gadget/function/f_uvc.c
....@@ -44,7 +44,7 @@
4444 #define UVC_STRING_STREAMING_IDX 1
4545
4646 static struct usb_string uvc_en_us_strings[] = {
47
- [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
47
+ /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */
4848 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
4949 { }
5050 };
....@@ -263,11 +263,6 @@
263263 (struct usb_descriptor_header *)&uvc_ss_bulk_streaming_comp,
264264 NULL,
265265 };
266
-void uvc_set_trace_param(unsigned int trace)
267
-{
268
- uvc_gadget_trace_param = trace;
269
-}
270
-EXPORT_SYMBOL(uvc_set_trace_param);
271266
272267 /* --------------------------------------------------------------------------
273268 * Control requests
....@@ -289,8 +284,9 @@
289284
290285 memset(&v4l2_event, 0, sizeof(v4l2_event));
291286 v4l2_event.type = UVC_EVENT_DATA;
292
- uvc_event->data.length = req->actual;
293
- memcpy(&uvc_event->data.data, req->buf, req->actual);
287
+ uvc_event->data.length = min_t(unsigned int, req->actual,
288
+ sizeof(uvc_event->data.data));
289
+ memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length);
294290 v4l2_event_queue(&uvc->vdev, &v4l2_event);
295291 }
296292 }
....@@ -302,18 +298,13 @@
302298 struct v4l2_event v4l2_event;
303299 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
304300
305
- /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
306
- * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
307
- * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
308
- */
309
-
310301 uvc_trace(UVC_TRACE_CONTROL,
311302 "setup request %02x %02x value %04x index %04x %04x\n",
312303 ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
313304 le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
314305
315306 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
316
- INFO(f->config->cdev, "invalid request type\n");
307
+ uvcg_info(f, "invalid request type\n");
317308 return -EINVAL;
318309 }
319310
....@@ -348,7 +339,7 @@
348339 struct uvc_device *uvc = to_uvc(f);
349340 struct f_uvc_opts *opts;
350341
351
- INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
342
+ uvcg_info(f, "%s(%u)\n", __func__, interface);
352343
353344 opts = fi_to_f_uvc_opts(f->fi);
354345
....@@ -379,7 +370,7 @@
379370 struct f_uvc_opts *opts;
380371 int ret;
381372
382
- INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
373
+ uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
383374
384375 opts = fi_to_f_uvc_opts(f->fi);
385376
....@@ -387,7 +378,7 @@
387378 if (alt)
388379 return -EINVAL;
389380
390
- INFO(cdev, "reset UVC Control\n");
381
+ uvcg_info(f, "reset UVC Control\n");
391382 usb_ep_disable(uvc->control_ep);
392383
393384 if (!uvc->control_ep->desc)
....@@ -516,14 +507,13 @@
516507 struct uvc_device *uvc = to_uvc(f);
517508 struct v4l2_event v4l2_event;
518509
519
- INFO(f->config->cdev, "uvc_function_disable\n");
510
+ uvcg_info(f, "%s()\n", __func__);
520511
521512 memset(&v4l2_event, 0, sizeof(v4l2_event));
522513 v4l2_event.type = UVC_EVENT_DISCONNECT;
523514 v4l2_event_queue(&uvc->vdev, &v4l2_event);
524515
525516 uvc->state = UVC_STATE_DISCONNECTED;
526
- f->config->cdev->gadget->uvc_enabled = false;
527517
528518 usb_ep_disable(uvc->video.ep);
529519 usb_ep_disable(uvc->control_ep);
....@@ -560,23 +550,19 @@
560550 void
561551 uvc_function_connect(struct uvc_device *uvc)
562552 {
563
- struct usb_composite_dev *cdev = uvc->func.config->cdev;
564553 int ret;
565554
566
- cdev->gadget->uvc_enabled = true;
567
-
568555 if ((ret = usb_function_activate(&uvc->func)) < 0)
569
- INFO(cdev, "UVC connect failed with %d\n", ret);
556
+ uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
570557 }
571558
572559 void
573560 uvc_function_disconnect(struct uvc_device *uvc)
574561 {
575
- struct usb_composite_dev *cdev = uvc->func.config->cdev;
576562 int ret;
577563
578564 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
579
- INFO(cdev, "UVC disconnect failed with %d\n", ret);
565
+ uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
580566 }
581567
582568 /* --------------------------------------------------------------------------
....@@ -606,11 +592,12 @@
606592 uvc->vdev.release = video_device_release_empty;
607593 uvc->vdev.vfl_dir = VFL_DIR_TX;
608594 uvc->vdev.lock = &uvc->video.mutex;
595
+ uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
609596 strlcpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
610597
611598 video_set_drvdata(&uvc->vdev, uvc);
612599
613
- ret = video_register_device(&uvc->vdev, VFL_TYPE_GRABBER, -1);
600
+ ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1);
614601 if (ret < 0)
615602 return ret;
616603
....@@ -794,7 +781,7 @@
794781 int ret = -EINVAL;
795782 u8 address;
796783
797
- INFO(cdev, "uvc_function_bind\n");
784
+ uvcg_info(f, "%s()\n", __func__);
798785
799786 opts = fi_to_f_uvc_opts(f->fi);
800787 /* Sanity check the streaming endpoint module parameters.
....@@ -815,8 +802,8 @@
815802 if (opts->streaming_maxburst &&
816803 (opts->streaming_maxpacket % 1024) != 0) {
817804 opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
818
- INFO(cdev, "overriding streaming_maxpacket to %d\n",
819
- opts->streaming_maxpacket);
805
+ uvcg_info(f, "overriding streaming_maxpacket to %d\n",
806
+ opts->streaming_maxpacket);
820807 }
821808
822809 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
....@@ -880,7 +867,7 @@
880867 /* Allocate endpoints. */
881868 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
882869 if (!ep) {
883
- INFO(cdev, "Unable to allocate control EP\n");
870
+ uvcg_info(f, "Unable to allocate control EP\n");
884871 goto error;
885872 }
886873 uvc->control_ep = ep;
....@@ -921,7 +908,7 @@
921908 }
922909
923910 if (!ep) {
924
- INFO(cdev, "Unable to allocate streaming EP\n");
911
+ uvcg_info(f, "Unable to allocate streaming EP\n");
925912 goto error;
926913 }
927914 uvc->video.ep = ep;
....@@ -937,9 +924,12 @@
937924 uvc_ss_bulk_streaming_ep.bEndpointAddress = address;
938925 }
939926
927
+#if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
940928 if (opts->device_name)
941929 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->device_name;
930
+#endif
942931
932
+ uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name;
943933 us = usb_gstrings_attach(cdev, uvc_function_strings,
944934 ARRAY_SIZE(uvc_en_us_strings));
945935 if (IS_ERR(us)) {
....@@ -1014,35 +1004,29 @@
10141004 uvc->control_req->context = uvc;
10151005
10161006 if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
1017
- printk(KERN_INFO "v4l2_device_register failed\n");
1007
+ uvcg_err(f, "failed to register V4L2 device\n");
10181008 goto error;
10191009 }
10201010
1021
- uvc->video.async_wq = alloc_workqueue("uvcgvideo",
1022
- WQ_UNBOUND | WQ_HIGHPRI,
1023
- 0);
1024
- if (!uvc->video.async_wq)
1025
- goto error;
1026
-
10271011 /* Initialise video. */
1028
- ret = uvcg_video_init(&uvc->video);
1012
+ ret = uvcg_video_init(&uvc->video, uvc);
10291013 if (ret < 0)
1030
- goto error;
1014
+ goto v4l2_error;
10311015
10321016 if (opts->streaming_bulk)
10331017 uvc->video.max_payload_size = uvc->video.imagesize;
10341018 /* Register a V4L2 device. */
10351019 ret = uvc_register_video(uvc);
10361020 if (ret < 0) {
1037
- printk(KERN_INFO "Unable to register video device\n");
1038
- goto error;
1021
+ uvcg_err(f, "failed to register video device\n");
1022
+ goto v4l2_error;
10391023 }
10401024
10411025 return 0;
10421026
1043
-error:
1027
+v4l2_error:
10441028 v4l2_device_unregister(&uvc->v4l2_dev);
1045
-
1029
+error:
10461030 if (uvc->control_req)
10471031 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
10481032 kfree(uvc->control_buf);
....@@ -1061,11 +1045,13 @@
10611045
10621046 mutex_destroy(&opts->lock);
10631047
1048
+#if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
10641049 if (opts->device_name_allocated) {
10651050 opts->device_name_allocated = false;
10661051 kfree(opts->device_name);
10671052 opts->device_name = NULL;
10681053 }
1054
+#endif
10691055
10701056 kfree(opts);
10711057 }
....@@ -1114,6 +1100,7 @@
11141100 pd->bmControls[0] = 1;
11151101 pd->bmControls[1] = 0;
11161102 pd->iProcessing = 0;
1103
+ pd->bmVideoStandards = 0;
11171104
11181105 od = &opts->uvc_output_terminal;
11191106 od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
....@@ -1185,8 +1172,8 @@
11851172
11861173 opts->streaming_interval = 1;
11871174 opts->streaming_maxpacket = 1024;
1188
- opts->uvc_num_request = UVC_NUM_REQUESTS;
11891175 opts->pm_qos_latency = 0;
1176
+ snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera");
11901177
11911178 ret = uvcg_attach_configfs(opts);
11921179 if (ret < 0) {
....@@ -1206,22 +1193,44 @@
12061193 kfree(uvc);
12071194 }
12081195
1209
-static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
1196
+static void uvc_function_unbind(struct usb_configuration *c,
1197
+ struct usb_function *f)
12101198 {
12111199 struct usb_composite_dev *cdev = c->cdev;
12121200 struct uvc_device *uvc = to_uvc(f);
1201
+ long wait_ret = 1;
12131202
1214
- INFO(cdev, "%s\n", __func__);
1203
+ uvcg_info(f, "%s()\n", __func__);
1204
+
1205
+ /* If we know we're connected via v4l2, then there should be a cleanup
1206
+ * of the device from userspace either via UVC_EVENT_DISCONNECT or
1207
+ * though the video device removal uevent. Allow some time for the
1208
+ * application to close out before things get deleted.
1209
+ */
1210
+ if (uvc->func_connected) {
1211
+ uvcg_dbg(f, "waiting for clean disconnect\n");
1212
+ wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1213
+ uvc->func_connected == false, msecs_to_jiffies(500));
1214
+ uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret);
1215
+ }
12151216
12161217 device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
12171218 video_unregister_device(&uvc->vdev);
12181219 v4l2_device_unregister(&uvc->v4l2_dev);
12191220
1221
+ if (uvc->func_connected) {
1222
+ /* Wait for the release to occur to ensure there are no longer any
1223
+ * pending operations that may cause panics when resources are cleaned
1224
+ * up.
1225
+ */
1226
+ uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__);
1227
+ wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
1228
+ uvc->func_connected == false, msecs_to_jiffies(1000));
1229
+ uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret);
1230
+ }
1231
+
12201232 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
12211233 kfree(uvc->control_buf);
1222
-
1223
- if (uvc->video.async_wq)
1224
- destroy_workqueue(uvc->video.async_wq);
12251234
12261235 usb_free_all_descriptors(f);
12271236 }
....@@ -1238,6 +1247,7 @@
12381247
12391248 mutex_init(&uvc->video.mutex);
12401249 uvc->state = UVC_STATE_DISCONNECTED;
1250
+ init_waitqueue_head(&uvc->func_connected_queue);
12411251 opts = fi_to_f_uvc_opts(fi);
12421252
12431253 mutex_lock(&opts->lock);
....@@ -1268,7 +1278,7 @@
12681278 /* Register the function. */
12691279 uvc->func.name = "uvc";
12701280 uvc->func.bind = uvc_function_bind;
1271
- uvc->func.unbind = uvc_unbind;
1281
+ uvc->func.unbind = uvc_function_unbind;
12721282 uvc->func.get_alt = uvc_function_get_alt;
12731283 uvc->func.set_alt = uvc_function_set_alt;
12741284 uvc->func.disable = uvc_function_disable;