forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/media/usb/uvc/uvc_video.c
....@@ -1,14 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * uvc_video.c -- USB Video Class driver - Video handling
34 *
45 * Copyright (C) 2005-2010
56 * 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
- *
127 */
138
149 #include <linux/kernel.h>
....@@ -659,7 +654,7 @@
659654 * to avoid losing precision in the division. Similarly, the host timestamp is
660655 * computed with
661656 *
662
- * TS = ((TS2 - TS1) * PTS + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
657
+ * TS = ((TS2 - TS1) * SOF + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
663658 *
664659 * SOF values are coded on 11 bits by USB. We extend their precision with 16
665660 * decimal bits, leading to a 11.16 coding.
....@@ -802,9 +797,9 @@
802797 unsigned int header_size;
803798 bool has_pts = false;
804799 bool has_scr = false;
805
- u16 uninitialized_var(scr_sof);
806
- u32 uninitialized_var(scr_stc);
807
- u32 uninitialized_var(pts);
800
+ u16 scr_sof;
801
+ u32 scr_stc;
802
+ u32 pts;
808803
809804 if (stream->stats.stream.nb_frames == 0 &&
810805 stream->stats.frame.nb_packets == 0)
....@@ -1546,11 +1541,11 @@
15461541 default:
15471542 uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
15481543 "completion handler.\n", urb->status);
1549
- /* fall through */
1544
+ fallthrough;
15501545 case -ENOENT: /* usb_poison_urb() called. */
15511546 if (stream->frozen)
15521547 return;
1553
- /* fall through */
1548
+ fallthrough;
15541549 case -ECONNRESET: /* usb_unlink_urb() called. */
15551550 case -ESHUTDOWN: /* The endpoint is being disabled. */
15561551 uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
....@@ -1596,20 +1591,19 @@
15961591 */
15971592 static void uvc_free_urb_buffers(struct uvc_streaming *stream)
15981593 {
1599
- unsigned int i;
1594
+ struct uvc_urb *uvc_urb;
16001595
1601
- for (i = 0; i < UVC_URBS; ++i) {
1602
- struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
1596
+ for_each_uvc_urb(uvc_urb, stream) {
1597
+ if (!uvc_urb->buffer)
1598
+ continue;
16031599
1604
- if (uvc_urb->buffer) {
16051600 #ifndef CONFIG_DMA_NONCOHERENT
1606
- usb_free_coherent(stream->dev->udev, stream->urb_size,
1607
- uvc_urb->buffer, uvc_urb->dma);
1601
+ usb_free_coherent(stream->dev->udev, stream->urb_size,
1602
+ uvc_urb->buffer, uvc_urb->dma);
16081603 #else
1609
- kfree(uvc_urb->buffer);
1604
+ kfree(uvc_urb->buffer);
16101605 #endif
1611
- uvc_urb->buffer = NULL;
1612
- }
1606
+ uvc_urb->buffer = NULL;
16131607 }
16141608
16151609 stream->urb_size = 0;
....@@ -1681,7 +1675,8 @@
16811675 /*
16821676 * Uninitialize isochronous/bulk URBs and free transfer buffers.
16831677 */
1684
-static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
1678
+static void uvc_video_stop_transfer(struct uvc_streaming *stream,
1679
+ int free_buffers)
16851680 {
16861681 struct uvc_urb *uvc_urb;
16871682
....@@ -1740,7 +1735,8 @@
17401735 struct usb_host_endpoint *ep, gfp_t gfp_flags)
17411736 {
17421737 struct urb *urb;
1743
- unsigned int npackets, i, j;
1738
+ struct uvc_urb *uvc_urb;
1739
+ unsigned int npackets, i;
17441740 u16 psize;
17451741 u32 size;
17461742
....@@ -1753,12 +1749,10 @@
17531749
17541750 size = npackets * psize;
17551751
1756
- for (i = 0; i < UVC_URBS; ++i) {
1757
- struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
1758
-
1752
+ for_each_uvc_urb(uvc_urb, stream) {
17591753 urb = usb_alloc_urb(npackets, gfp_flags);
17601754 if (urb == NULL) {
1761
- uvc_uninit_video(stream, 1);
1755
+ uvc_video_stop_transfer(stream, 1);
17621756 return -ENOMEM;
17631757 }
17641758
....@@ -1778,9 +1772,9 @@
17781772 urb->number_of_packets = npackets;
17791773 urb->transfer_buffer_length = size;
17801774
1781
- for (j = 0; j < npackets; ++j) {
1782
- urb->iso_frame_desc[j].offset = j * psize;
1783
- urb->iso_frame_desc[j].length = psize;
1775
+ for (i = 0; i < npackets; ++i) {
1776
+ urb->iso_frame_desc[i].offset = i * psize;
1777
+ urb->iso_frame_desc[i].length = psize;
17841778 }
17851779
17861780 uvc_urb->urb = urb;
....@@ -1797,7 +1791,8 @@
17971791 struct usb_host_endpoint *ep, gfp_t gfp_flags)
17981792 {
17991793 struct urb *urb;
1800
- unsigned int npackets, pipe, i;
1794
+ struct uvc_urb *uvc_urb;
1795
+ unsigned int npackets, pipe;
18011796 u16 psize;
18021797 u32 size;
18031798
....@@ -1821,12 +1816,10 @@
18211816 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
18221817 size = 0;
18231818
1824
- for (i = 0; i < UVC_URBS; ++i) {
1825
- struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
1826
-
1819
+ for_each_uvc_urb(uvc_urb, stream) {
18271820 urb = usb_alloc_urb(0, gfp_flags);
18281821 if (urb == NULL) {
1829
- uvc_uninit_video(stream, 1);
1822
+ uvc_video_stop_transfer(stream, 1);
18301823 return -ENOMEM;
18311824 }
18321825
....@@ -1846,10 +1839,12 @@
18461839 /*
18471840 * Initialize isochronous/bulk URBs and allocate transfer buffers.
18481841 */
1849
-static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
1842
+static int uvc_video_start_transfer(struct uvc_streaming *stream,
1843
+ gfp_t gfp_flags)
18501844 {
18511845 struct usb_interface *intf = stream->intf;
18521846 struct usb_host_endpoint *ep;
1847
+ struct uvc_urb *uvc_urb;
18531848 unsigned int i;
18541849 int ret;
18551850
....@@ -1865,7 +1860,7 @@
18651860 struct usb_host_endpoint *best_ep = NULL;
18661861 unsigned int best_psize = UINT_MAX;
18671862 unsigned int bandwidth;
1868
- unsigned int uninitialized_var(altsetting);
1863
+ unsigned int altsetting;
18691864 int intfnum = stream->intfnum;
18701865
18711866 /* Isochronous endpoint, select the alternate setting. */
....@@ -1931,14 +1926,12 @@
19311926 return ret;
19321927
19331928 /* Submit the URBs. */
1934
- for (i = 0; i < UVC_URBS; ++i) {
1935
- struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
1936
-
1929
+ for_each_uvc_urb(uvc_urb, stream) {
19371930 ret = usb_submit_urb(uvc_urb->urb, gfp_flags);
19381931 if (ret < 0) {
1939
- uvc_printk(KERN_ERR, "Failed to submit URB %u "
1940
- "(%d).\n", i, ret);
1941
- uvc_uninit_video(stream, 1);
1932
+ uvc_printk(KERN_ERR, "Failed to submit URB %u (%d).\n",
1933
+ uvc_urb_index(uvc_urb), ret);
1934
+ uvc_video_stop_transfer(stream, 1);
19421935 return ret;
19431936 }
19441937 }
....@@ -1969,7 +1962,7 @@
19691962 return 0;
19701963
19711964 stream->frozen = 1;
1972
- uvc_uninit_video(stream, 0);
1965
+ uvc_video_stop_transfer(stream, 0);
19731966 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
19741967 return 0;
19751968 }
....@@ -2005,7 +1998,7 @@
20051998 if (ret < 0)
20061999 return ret;
20072000
2008
- return uvc_init_video(stream, GFP_NOIO);
2001
+ return uvc_video_start_transfer(stream, GFP_NOIO);
20092002 }
20102003
20112004 /* ------------------------------------------------------------------------
....@@ -2046,7 +2039,7 @@
20462039 usb_set_interface(stream->dev->udev, stream->intfnum, 0);
20472040
20482041 /* Set the streaming probe control with default streaming parameters
2049
- * retrieved from the device. Webcams that don't suport GET_DEF
2042
+ * retrieved from the device. Webcams that don't support GET_DEF
20502043 * requests on the probe control will just keep their current streaming
20512044 * parameters.
20522045 */
....@@ -2120,37 +2113,9 @@
21202113 return 0;
21212114 }
21222115
2123
-/*
2124
- * Enable or disable the video stream.
2125
- */
2126
-int uvc_video_enable(struct uvc_streaming *stream, int enable)
2116
+int uvc_video_start_streaming(struct uvc_streaming *stream)
21272117 {
21282118 int ret;
2129
-
2130
- if (!enable) {
2131
- uvc_uninit_video(stream, 1);
2132
- if (stream->intf->num_altsetting > 1) {
2133
- usb_set_interface(stream->dev->udev,
2134
- stream->intfnum, 0);
2135
- } else {
2136
- /* UVC doesn't specify how to inform a bulk-based device
2137
- * when the video stream is stopped. Windows sends a
2138
- * CLEAR_FEATURE(HALT) request to the video streaming
2139
- * bulk endpoint, mimic the same behaviour.
2140
- */
2141
- unsigned int epnum = stream->header.bEndpointAddress
2142
- & USB_ENDPOINT_NUMBER_MASK;
2143
- unsigned int dir = stream->header.bEndpointAddress
2144
- & USB_ENDPOINT_DIR_MASK;
2145
- unsigned int pipe;
2146
-
2147
- pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
2148
- usb_clear_halt(stream->dev->udev, pipe);
2149
- }
2150
-
2151
- uvc_video_clock_cleanup(stream);
2152
- return 0;
2153
- }
21542119
21552120 ret = uvc_video_clock_init(stream);
21562121 if (ret < 0)
....@@ -2161,7 +2126,7 @@
21612126 if (ret < 0)
21622127 goto error_commit;
21632128
2164
- ret = uvc_init_video(stream, GFP_KERNEL);
2129
+ ret = uvc_video_start_transfer(stream, GFP_KERNEL);
21652130 if (ret < 0)
21662131 goto error_video;
21672132
....@@ -2174,3 +2139,28 @@
21742139
21752140 return ret;
21762141 }
2142
+
2143
+void uvc_video_stop_streaming(struct uvc_streaming *stream)
2144
+{
2145
+ uvc_video_stop_transfer(stream, 1);
2146
+
2147
+ if (stream->intf->num_altsetting > 1) {
2148
+ usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2149
+ } else {
2150
+ /* UVC doesn't specify how to inform a bulk-based device
2151
+ * when the video stream is stopped. Windows sends a
2152
+ * CLEAR_FEATURE(HALT) request to the video streaming
2153
+ * bulk endpoint, mimic the same behaviour.
2154
+ */
2155
+ unsigned int epnum = stream->header.bEndpointAddress
2156
+ & USB_ENDPOINT_NUMBER_MASK;
2157
+ unsigned int dir = stream->header.bEndpointAddress
2158
+ & USB_ENDPOINT_DIR_MASK;
2159
+ unsigned int pipe;
2160
+
2161
+ pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
2162
+ usb_clear_halt(stream->dev->udev, pipe);
2163
+ }
2164
+
2165
+ uvc_video_clock_cleanup(stream);
2166
+}