1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * uvc_video.c -- USB Video Class driver - Video handling
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2005-2010
6*4882a593Smuzhiyun * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/list.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/usb.h>
14*4882a593Smuzhiyun #include <linux/videodev2.h>
15*4882a593Smuzhiyun #include <linux/vmalloc.h>
16*4882a593Smuzhiyun #include <linux/wait.h>
17*4882a593Smuzhiyun #include <linux/atomic.h>
18*4882a593Smuzhiyun #include <asm/unaligned.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <media/v4l2-common.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "uvcvideo.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* ------------------------------------------------------------------------
25*4882a593Smuzhiyun * UVC Controls
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
__uvc_query_ctrl(struct uvc_device * dev,u8 query,u8 unit,u8 intfnum,u8 cs,void * data,u16 size,int timeout)28*4882a593Smuzhiyun static int __uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
29*4882a593Smuzhiyun u8 intfnum, u8 cs, void *data, u16 size,
30*4882a593Smuzhiyun int timeout)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
33*4882a593Smuzhiyun unsigned int pipe;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
36*4882a593Smuzhiyun : usb_sndctrlpipe(dev->udev, 0);
37*4882a593Smuzhiyun type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
40*4882a593Smuzhiyun unit << 8 | intfnum, data, size, timeout);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
uvc_query_name(u8 query)43*4882a593Smuzhiyun static const char *uvc_query_name(u8 query)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun switch (query) {
46*4882a593Smuzhiyun case UVC_SET_CUR:
47*4882a593Smuzhiyun return "SET_CUR";
48*4882a593Smuzhiyun case UVC_GET_CUR:
49*4882a593Smuzhiyun return "GET_CUR";
50*4882a593Smuzhiyun case UVC_GET_MIN:
51*4882a593Smuzhiyun return "GET_MIN";
52*4882a593Smuzhiyun case UVC_GET_MAX:
53*4882a593Smuzhiyun return "GET_MAX";
54*4882a593Smuzhiyun case UVC_GET_RES:
55*4882a593Smuzhiyun return "GET_RES";
56*4882a593Smuzhiyun case UVC_GET_LEN:
57*4882a593Smuzhiyun return "GET_LEN";
58*4882a593Smuzhiyun case UVC_GET_INFO:
59*4882a593Smuzhiyun return "GET_INFO";
60*4882a593Smuzhiyun case UVC_GET_DEF:
61*4882a593Smuzhiyun return "GET_DEF";
62*4882a593Smuzhiyun default:
63*4882a593Smuzhiyun return "<invalid>";
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
uvc_query_ctrl(struct uvc_device * dev,u8 query,u8 unit,u8 intfnum,u8 cs,void * data,u16 size)67*4882a593Smuzhiyun int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
68*4882a593Smuzhiyun u8 intfnum, u8 cs, void *data, u16 size)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun int ret;
71*4882a593Smuzhiyun u8 error;
72*4882a593Smuzhiyun u8 tmp;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
75*4882a593Smuzhiyun UVC_CTRL_CONTROL_TIMEOUT);
76*4882a593Smuzhiyun if (likely(ret == size))
77*4882a593Smuzhiyun return 0;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun uvc_printk(KERN_ERR,
80*4882a593Smuzhiyun "Failed to query (%s) UVC control %u on unit %u: %d (exp. %u).\n",
81*4882a593Smuzhiyun uvc_query_name(query), cs, unit, ret, size);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun if (ret != -EPIPE)
84*4882a593Smuzhiyun return ret;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun tmp = *(u8 *)data;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun ret = __uvc_query_ctrl(dev, UVC_GET_CUR, 0, intfnum,
89*4882a593Smuzhiyun UVC_VC_REQUEST_ERROR_CODE_CONTROL, data, 1,
90*4882a593Smuzhiyun UVC_CTRL_CONTROL_TIMEOUT);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun error = *(u8 *)data;
93*4882a593Smuzhiyun *(u8 *)data = tmp;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (ret != 1)
96*4882a593Smuzhiyun return ret < 0 ? ret : -EPIPE;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun uvc_trace(UVC_TRACE_CONTROL, "Control error %u\n", error);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun switch (error) {
101*4882a593Smuzhiyun case 0:
102*4882a593Smuzhiyun /* Cannot happen - we received a STALL */
103*4882a593Smuzhiyun return -EPIPE;
104*4882a593Smuzhiyun case 1: /* Not ready */
105*4882a593Smuzhiyun return -EBUSY;
106*4882a593Smuzhiyun case 2: /* Wrong state */
107*4882a593Smuzhiyun return -EILSEQ;
108*4882a593Smuzhiyun case 3: /* Power */
109*4882a593Smuzhiyun return -EREMOTE;
110*4882a593Smuzhiyun case 4: /* Out of range */
111*4882a593Smuzhiyun return -ERANGE;
112*4882a593Smuzhiyun case 5: /* Invalid unit */
113*4882a593Smuzhiyun case 6: /* Invalid control */
114*4882a593Smuzhiyun case 7: /* Invalid Request */
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun * The firmware has not properly implemented
117*4882a593Smuzhiyun * the control or there has been a HW error.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun return -EIO;
120*4882a593Smuzhiyun case 8: /* Invalid value within range */
121*4882a593Smuzhiyun return -EINVAL;
122*4882a593Smuzhiyun default: /* reserved or unknown */
123*4882a593Smuzhiyun break;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun return -EPIPE;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
uvc_fixup_video_ctrl(struct uvc_streaming * stream,struct uvc_streaming_control * ctrl)129*4882a593Smuzhiyun static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
130*4882a593Smuzhiyun struct uvc_streaming_control *ctrl)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun static const struct usb_device_id elgato_cam_link_4k = {
133*4882a593Smuzhiyun USB_DEVICE(0x0fd9, 0x0066)
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun struct uvc_format *format = NULL;
136*4882a593Smuzhiyun struct uvc_frame *frame = NULL;
137*4882a593Smuzhiyun unsigned int i;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * The response of the Elgato Cam Link 4K is incorrect: The second byte
141*4882a593Smuzhiyun * contains bFormatIndex (instead of being the second byte of bmHint).
142*4882a593Smuzhiyun * The first byte is always zero. The third byte is always 1.
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * The UVC 1.5 class specification defines the first five bits in the
145*4882a593Smuzhiyun * bmHint bitfield. The remaining bits are reserved and should be zero.
146*4882a593Smuzhiyun * Therefore a valid bmHint will be less than 32.
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * Latest Elgato Cam Link 4K firmware as of 2021-03-23 needs this fix.
149*4882a593Smuzhiyun * MCU: 20.02.19, FPGA: 67
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun if (usb_match_one_id(stream->dev->intf, &elgato_cam_link_4k) &&
152*4882a593Smuzhiyun ctrl->bmHint > 255) {
153*4882a593Smuzhiyun u8 corrected_format_index = ctrl->bmHint >> 8;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* uvc_dbg(stream->dev, VIDEO,
156*4882a593Smuzhiyun "Correct USB video probe response from {bmHint: 0x%04x, bFormatIndex: %u} to {bmHint: 0x%04x, bFormatIndex: %u}\n",
157*4882a593Smuzhiyun ctrl->bmHint, ctrl->bFormatIndex,
158*4882a593Smuzhiyun 1, corrected_format_index); */
159*4882a593Smuzhiyun ctrl->bmHint = 1;
160*4882a593Smuzhiyun ctrl->bFormatIndex = corrected_format_index;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun for (i = 0; i < stream->nformats; ++i) {
164*4882a593Smuzhiyun if (stream->format[i].index == ctrl->bFormatIndex) {
165*4882a593Smuzhiyun format = &stream->format[i];
166*4882a593Smuzhiyun break;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (format == NULL)
171*4882a593Smuzhiyun return;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun for (i = 0; i < format->nframes; ++i) {
174*4882a593Smuzhiyun if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
175*4882a593Smuzhiyun frame = &format->frame[i];
176*4882a593Smuzhiyun break;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun if (frame == NULL)
181*4882a593Smuzhiyun return;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
184*4882a593Smuzhiyun (ctrl->dwMaxVideoFrameSize == 0 &&
185*4882a593Smuzhiyun stream->dev->uvc_version < 0x0110))
186*4882a593Smuzhiyun ctrl->dwMaxVideoFrameSize =
187*4882a593Smuzhiyun frame->dwMaxVideoFrameBufferSize;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /* The "TOSHIBA Web Camera - 5M" Chicony device (04f2:b50b) seems to
190*4882a593Smuzhiyun * compute the bandwidth on 16 bits and erroneously sign-extend it to
191*4882a593Smuzhiyun * 32 bits, resulting in a huge bandwidth value. Detect and fix that
192*4882a593Smuzhiyun * condition by setting the 16 MSBs to 0 when they're all equal to 1.
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun if ((ctrl->dwMaxPayloadTransferSize & 0xffff0000) == 0xffff0000)
195*4882a593Smuzhiyun ctrl->dwMaxPayloadTransferSize &= ~0xffff0000;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
198*4882a593Smuzhiyun stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
199*4882a593Smuzhiyun stream->intf->num_altsetting > 1) {
200*4882a593Smuzhiyun u32 interval;
201*4882a593Smuzhiyun u32 bandwidth;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun interval = (ctrl->dwFrameInterval > 100000)
204*4882a593Smuzhiyun ? ctrl->dwFrameInterval
205*4882a593Smuzhiyun : frame->dwFrameInterval[0];
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* Compute a bandwidth estimation by multiplying the frame
208*4882a593Smuzhiyun * size by the number of video frames per second, divide the
209*4882a593Smuzhiyun * result by the number of USB frames (or micro-frames for
210*4882a593Smuzhiyun * high-speed devices) per second and add the UVC header size
211*4882a593Smuzhiyun * (assumed to be 12 bytes long).
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
214*4882a593Smuzhiyun bandwidth *= 10000000 / interval + 1;
215*4882a593Smuzhiyun bandwidth /= 1000;
216*4882a593Smuzhiyun if (stream->dev->udev->speed == USB_SPEED_HIGH)
217*4882a593Smuzhiyun bandwidth /= 8;
218*4882a593Smuzhiyun bandwidth += 12;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* The bandwidth estimate is too low for many cameras. Don't use
221*4882a593Smuzhiyun * maximum packet sizes lower than 1024 bytes to try and work
222*4882a593Smuzhiyun * around the problem. According to measurements done on two
223*4882a593Smuzhiyun * different camera models, the value is high enough to get most
224*4882a593Smuzhiyun * resolutions working while not preventing two simultaneous
225*4882a593Smuzhiyun * VGA streams at 15 fps.
226*4882a593Smuzhiyun */
227*4882a593Smuzhiyun bandwidth = max_t(u32, bandwidth, 1024);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun ctrl->dwMaxPayloadTransferSize = bandwidth;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
uvc_video_ctrl_size(struct uvc_streaming * stream)233*4882a593Smuzhiyun static size_t uvc_video_ctrl_size(struct uvc_streaming *stream)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun * Return the size of the video probe and commit controls, which depends
237*4882a593Smuzhiyun * on the protocol version.
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun if (stream->dev->uvc_version < 0x0110)
240*4882a593Smuzhiyun return 26;
241*4882a593Smuzhiyun else if (stream->dev->uvc_version < 0x0150)
242*4882a593Smuzhiyun return 34;
243*4882a593Smuzhiyun else
244*4882a593Smuzhiyun return 48;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
uvc_get_video_ctrl(struct uvc_streaming * stream,struct uvc_streaming_control * ctrl,int probe,u8 query)247*4882a593Smuzhiyun static int uvc_get_video_ctrl(struct uvc_streaming *stream,
248*4882a593Smuzhiyun struct uvc_streaming_control *ctrl, int probe, u8 query)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun u16 size = uvc_video_ctrl_size(stream);
251*4882a593Smuzhiyun u8 *data;
252*4882a593Smuzhiyun int ret;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
255*4882a593Smuzhiyun query == UVC_GET_DEF)
256*4882a593Smuzhiyun return -EIO;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun data = kmalloc(size, GFP_KERNEL);
259*4882a593Smuzhiyun if (data == NULL)
260*4882a593Smuzhiyun return -ENOMEM;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
263*4882a593Smuzhiyun probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
264*4882a593Smuzhiyun size, uvc_timeout_param);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
267*4882a593Smuzhiyun /* Some cameras, mostly based on Bison Electronics chipsets,
268*4882a593Smuzhiyun * answer a GET_MIN or GET_MAX request with the wCompQuality
269*4882a593Smuzhiyun * field only.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
272*4882a593Smuzhiyun "compliance - GET_MIN/MAX(PROBE) incorrectly "
273*4882a593Smuzhiyun "supported. Enabling workaround.\n");
274*4882a593Smuzhiyun memset(ctrl, 0, sizeof(*ctrl));
275*4882a593Smuzhiyun ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
276*4882a593Smuzhiyun ret = 0;
277*4882a593Smuzhiyun goto out;
278*4882a593Smuzhiyun } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
279*4882a593Smuzhiyun /* Many cameras don't support the GET_DEF request on their
280*4882a593Smuzhiyun * video probe control. Warn once and return, the caller will
281*4882a593Smuzhiyun * fall back to GET_CUR.
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
284*4882a593Smuzhiyun "compliance - GET_DEF(PROBE) not supported. "
285*4882a593Smuzhiyun "Enabling workaround.\n");
286*4882a593Smuzhiyun ret = -EIO;
287*4882a593Smuzhiyun goto out;
288*4882a593Smuzhiyun } else if (ret != size) {
289*4882a593Smuzhiyun uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
290*4882a593Smuzhiyun "%d (exp. %u).\n", query, probe ? "probe" : "commit",
291*4882a593Smuzhiyun ret, size);
292*4882a593Smuzhiyun ret = -EIO;
293*4882a593Smuzhiyun goto out;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
297*4882a593Smuzhiyun ctrl->bFormatIndex = data[2];
298*4882a593Smuzhiyun ctrl->bFrameIndex = data[3];
299*4882a593Smuzhiyun ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
300*4882a593Smuzhiyun ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
301*4882a593Smuzhiyun ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
302*4882a593Smuzhiyun ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
303*4882a593Smuzhiyun ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
304*4882a593Smuzhiyun ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
305*4882a593Smuzhiyun ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
306*4882a593Smuzhiyun ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun if (size >= 34) {
309*4882a593Smuzhiyun ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
310*4882a593Smuzhiyun ctrl->bmFramingInfo = data[30];
311*4882a593Smuzhiyun ctrl->bPreferedVersion = data[31];
312*4882a593Smuzhiyun ctrl->bMinVersion = data[32];
313*4882a593Smuzhiyun ctrl->bMaxVersion = data[33];
314*4882a593Smuzhiyun } else {
315*4882a593Smuzhiyun ctrl->dwClockFrequency = stream->dev->clock_frequency;
316*4882a593Smuzhiyun ctrl->bmFramingInfo = 0;
317*4882a593Smuzhiyun ctrl->bPreferedVersion = 0;
318*4882a593Smuzhiyun ctrl->bMinVersion = 0;
319*4882a593Smuzhiyun ctrl->bMaxVersion = 0;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /* Some broken devices return null or wrong dwMaxVideoFrameSize and
323*4882a593Smuzhiyun * dwMaxPayloadTransferSize fields. Try to get the value from the
324*4882a593Smuzhiyun * format and frame descriptors.
325*4882a593Smuzhiyun */
326*4882a593Smuzhiyun uvc_fixup_video_ctrl(stream, ctrl);
327*4882a593Smuzhiyun ret = 0;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun out:
330*4882a593Smuzhiyun kfree(data);
331*4882a593Smuzhiyun return ret;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
uvc_set_video_ctrl(struct uvc_streaming * stream,struct uvc_streaming_control * ctrl,int probe)334*4882a593Smuzhiyun static int uvc_set_video_ctrl(struct uvc_streaming *stream,
335*4882a593Smuzhiyun struct uvc_streaming_control *ctrl, int probe)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun u16 size = uvc_video_ctrl_size(stream);
338*4882a593Smuzhiyun u8 *data;
339*4882a593Smuzhiyun int ret;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun data = kzalloc(size, GFP_KERNEL);
342*4882a593Smuzhiyun if (data == NULL)
343*4882a593Smuzhiyun return -ENOMEM;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
346*4882a593Smuzhiyun data[2] = ctrl->bFormatIndex;
347*4882a593Smuzhiyun data[3] = ctrl->bFrameIndex;
348*4882a593Smuzhiyun *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
349*4882a593Smuzhiyun *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
350*4882a593Smuzhiyun *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
351*4882a593Smuzhiyun *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
352*4882a593Smuzhiyun *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
353*4882a593Smuzhiyun *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
354*4882a593Smuzhiyun put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
355*4882a593Smuzhiyun put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun if (size >= 34) {
358*4882a593Smuzhiyun put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
359*4882a593Smuzhiyun data[30] = ctrl->bmFramingInfo;
360*4882a593Smuzhiyun data[31] = ctrl->bPreferedVersion;
361*4882a593Smuzhiyun data[32] = ctrl->bMinVersion;
362*4882a593Smuzhiyun data[33] = ctrl->bMaxVersion;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
366*4882a593Smuzhiyun probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
367*4882a593Smuzhiyun size, uvc_timeout_param);
368*4882a593Smuzhiyun if (ret != size) {
369*4882a593Smuzhiyun uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
370*4882a593Smuzhiyun "%d (exp. %u).\n", probe ? "probe" : "commit",
371*4882a593Smuzhiyun ret, size);
372*4882a593Smuzhiyun ret = -EIO;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun kfree(data);
376*4882a593Smuzhiyun return ret;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
uvc_probe_video(struct uvc_streaming * stream,struct uvc_streaming_control * probe)379*4882a593Smuzhiyun int uvc_probe_video(struct uvc_streaming *stream,
380*4882a593Smuzhiyun struct uvc_streaming_control *probe)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun struct uvc_streaming_control probe_min, probe_max;
383*4882a593Smuzhiyun u16 bandwidth;
384*4882a593Smuzhiyun unsigned int i;
385*4882a593Smuzhiyun int ret;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /* Perform probing. The device should adjust the requested values
388*4882a593Smuzhiyun * according to its capabilities. However, some devices, namely the
389*4882a593Smuzhiyun * first generation UVC Logitech webcams, don't implement the Video
390*4882a593Smuzhiyun * Probe control properly, and just return the needed bandwidth. For
391*4882a593Smuzhiyun * that reason, if the needed bandwidth exceeds the maximum available
392*4882a593Smuzhiyun * bandwidth, try to lower the quality.
393*4882a593Smuzhiyun */
394*4882a593Smuzhiyun ret = uvc_set_video_ctrl(stream, probe, 1);
395*4882a593Smuzhiyun if (ret < 0)
396*4882a593Smuzhiyun goto done;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun /* Get the minimum and maximum values for compression settings. */
399*4882a593Smuzhiyun if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
400*4882a593Smuzhiyun ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
401*4882a593Smuzhiyun if (ret < 0)
402*4882a593Smuzhiyun goto done;
403*4882a593Smuzhiyun ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
404*4882a593Smuzhiyun if (ret < 0)
405*4882a593Smuzhiyun goto done;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun probe->wCompQuality = probe_max.wCompQuality;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun for (i = 0; i < 2; ++i) {
411*4882a593Smuzhiyun ret = uvc_set_video_ctrl(stream, probe, 1);
412*4882a593Smuzhiyun if (ret < 0)
413*4882a593Smuzhiyun goto done;
414*4882a593Smuzhiyun ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
415*4882a593Smuzhiyun if (ret < 0)
416*4882a593Smuzhiyun goto done;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (stream->intf->num_altsetting == 1)
419*4882a593Smuzhiyun break;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun bandwidth = probe->dwMaxPayloadTransferSize;
422*4882a593Smuzhiyun if (bandwidth <= stream->maxpsize)
423*4882a593Smuzhiyun break;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
426*4882a593Smuzhiyun ret = -ENOSPC;
427*4882a593Smuzhiyun goto done;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /* TODO: negotiate compression parameters */
431*4882a593Smuzhiyun probe->wKeyFrameRate = probe_min.wKeyFrameRate;
432*4882a593Smuzhiyun probe->wPFrameRate = probe_min.wPFrameRate;
433*4882a593Smuzhiyun probe->wCompQuality = probe_max.wCompQuality;
434*4882a593Smuzhiyun probe->wCompWindowSize = probe_min.wCompWindowSize;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun done:
438*4882a593Smuzhiyun return ret;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
uvc_commit_video(struct uvc_streaming * stream,struct uvc_streaming_control * probe)441*4882a593Smuzhiyun static int uvc_commit_video(struct uvc_streaming *stream,
442*4882a593Smuzhiyun struct uvc_streaming_control *probe)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun return uvc_set_video_ctrl(stream, probe, 0);
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
448*4882a593Smuzhiyun * Clocks and timestamps
449*4882a593Smuzhiyun */
450*4882a593Smuzhiyun
uvc_video_get_time(void)451*4882a593Smuzhiyun static inline ktime_t uvc_video_get_time(void)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun if (uvc_clock_param == CLOCK_MONOTONIC)
454*4882a593Smuzhiyun return ktime_get();
455*4882a593Smuzhiyun else
456*4882a593Smuzhiyun return ktime_get_real();
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun static void
uvc_video_clock_decode(struct uvc_streaming * stream,struct uvc_buffer * buf,const u8 * data,int len)460*4882a593Smuzhiyun uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf,
461*4882a593Smuzhiyun const u8 *data, int len)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun struct uvc_clock_sample *sample;
464*4882a593Smuzhiyun unsigned int header_size;
465*4882a593Smuzhiyun bool has_pts = false;
466*4882a593Smuzhiyun bool has_scr = false;
467*4882a593Smuzhiyun unsigned long flags;
468*4882a593Smuzhiyun ktime_t time;
469*4882a593Smuzhiyun u16 host_sof;
470*4882a593Smuzhiyun u16 dev_sof;
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
473*4882a593Smuzhiyun case UVC_STREAM_PTS | UVC_STREAM_SCR:
474*4882a593Smuzhiyun header_size = 12;
475*4882a593Smuzhiyun has_pts = true;
476*4882a593Smuzhiyun has_scr = true;
477*4882a593Smuzhiyun break;
478*4882a593Smuzhiyun case UVC_STREAM_PTS:
479*4882a593Smuzhiyun header_size = 6;
480*4882a593Smuzhiyun has_pts = true;
481*4882a593Smuzhiyun break;
482*4882a593Smuzhiyun case UVC_STREAM_SCR:
483*4882a593Smuzhiyun header_size = 8;
484*4882a593Smuzhiyun has_scr = true;
485*4882a593Smuzhiyun break;
486*4882a593Smuzhiyun default:
487*4882a593Smuzhiyun header_size = 2;
488*4882a593Smuzhiyun break;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* Check for invalid headers. */
492*4882a593Smuzhiyun if (len < header_size)
493*4882a593Smuzhiyun return;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /* Extract the timestamps:
496*4882a593Smuzhiyun *
497*4882a593Smuzhiyun * - store the frame PTS in the buffer structure
498*4882a593Smuzhiyun * - if the SCR field is present, retrieve the host SOF counter and
499*4882a593Smuzhiyun * kernel timestamps and store them with the SCR STC and SOF fields
500*4882a593Smuzhiyun * in the ring buffer
501*4882a593Smuzhiyun */
502*4882a593Smuzhiyun if (has_pts && buf != NULL)
503*4882a593Smuzhiyun buf->pts = get_unaligned_le32(&data[2]);
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun if (!has_scr)
506*4882a593Smuzhiyun return;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun /* To limit the amount of data, drop SCRs with an SOF identical to the
509*4882a593Smuzhiyun * previous one.
510*4882a593Smuzhiyun */
511*4882a593Smuzhiyun dev_sof = get_unaligned_le16(&data[header_size - 2]);
512*4882a593Smuzhiyun if (dev_sof == stream->clock.last_sof)
513*4882a593Smuzhiyun return;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun stream->clock.last_sof = dev_sof;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun host_sof = usb_get_current_frame_number(stream->dev->udev);
518*4882a593Smuzhiyun time = uvc_video_get_time();
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun /* The UVC specification allows device implementations that can't obtain
521*4882a593Smuzhiyun * the USB frame number to keep their own frame counters as long as they
522*4882a593Smuzhiyun * match the size and frequency of the frame number associated with USB
523*4882a593Smuzhiyun * SOF tokens. The SOF values sent by such devices differ from the USB
524*4882a593Smuzhiyun * SOF tokens by a fixed offset that needs to be estimated and accounted
525*4882a593Smuzhiyun * for to make timestamp recovery as accurate as possible.
526*4882a593Smuzhiyun *
527*4882a593Smuzhiyun * The offset is estimated the first time a device SOF value is received
528*4882a593Smuzhiyun * as the difference between the host and device SOF values. As the two
529*4882a593Smuzhiyun * SOF values can differ slightly due to transmission delays, consider
530*4882a593Smuzhiyun * that the offset is null if the difference is not higher than 10 ms
531*4882a593Smuzhiyun * (negative differences can not happen and are thus considered as an
532*4882a593Smuzhiyun * offset). The video commit control wDelay field should be used to
533*4882a593Smuzhiyun * compute a dynamic threshold instead of using a fixed 10 ms value, but
534*4882a593Smuzhiyun * devices don't report reliable wDelay values.
535*4882a593Smuzhiyun *
536*4882a593Smuzhiyun * See uvc_video_clock_host_sof() for an explanation regarding why only
537*4882a593Smuzhiyun * the 8 LSBs of the delta are kept.
538*4882a593Smuzhiyun */
539*4882a593Smuzhiyun if (stream->clock.sof_offset == (u16)-1) {
540*4882a593Smuzhiyun u16 delta_sof = (host_sof - dev_sof) & 255;
541*4882a593Smuzhiyun if (delta_sof >= 10)
542*4882a593Smuzhiyun stream->clock.sof_offset = delta_sof;
543*4882a593Smuzhiyun else
544*4882a593Smuzhiyun stream->clock.sof_offset = 0;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun dev_sof = (dev_sof + stream->clock.sof_offset) & 2047;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun spin_lock_irqsave(&stream->clock.lock, flags);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun sample = &stream->clock.samples[stream->clock.head];
552*4882a593Smuzhiyun sample->dev_stc = get_unaligned_le32(&data[header_size - 6]);
553*4882a593Smuzhiyun sample->dev_sof = dev_sof;
554*4882a593Smuzhiyun sample->host_sof = host_sof;
555*4882a593Smuzhiyun sample->host_time = time;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /* Update the sliding window head and count. */
558*4882a593Smuzhiyun stream->clock.head = (stream->clock.head + 1) % stream->clock.size;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun if (stream->clock.count < stream->clock.size)
561*4882a593Smuzhiyun stream->clock.count++;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun spin_unlock_irqrestore(&stream->clock.lock, flags);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
uvc_video_clock_reset(struct uvc_streaming * stream)566*4882a593Smuzhiyun static void uvc_video_clock_reset(struct uvc_streaming *stream)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun struct uvc_clock *clock = &stream->clock;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun clock->head = 0;
571*4882a593Smuzhiyun clock->count = 0;
572*4882a593Smuzhiyun clock->last_sof = -1;
573*4882a593Smuzhiyun clock->sof_offset = -1;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
uvc_video_clock_init(struct uvc_streaming * stream)576*4882a593Smuzhiyun static int uvc_video_clock_init(struct uvc_streaming *stream)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun struct uvc_clock *clock = &stream->clock;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun spin_lock_init(&clock->lock);
581*4882a593Smuzhiyun clock->size = 32;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun clock->samples = kmalloc_array(clock->size, sizeof(*clock->samples),
584*4882a593Smuzhiyun GFP_KERNEL);
585*4882a593Smuzhiyun if (clock->samples == NULL)
586*4882a593Smuzhiyun return -ENOMEM;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun uvc_video_clock_reset(stream);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun return 0;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
uvc_video_clock_cleanup(struct uvc_streaming * stream)593*4882a593Smuzhiyun static void uvc_video_clock_cleanup(struct uvc_streaming *stream)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun kfree(stream->clock.samples);
596*4882a593Smuzhiyun stream->clock.samples = NULL;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /*
600*4882a593Smuzhiyun * uvc_video_clock_host_sof - Return the host SOF value for a clock sample
601*4882a593Smuzhiyun *
602*4882a593Smuzhiyun * Host SOF counters reported by usb_get_current_frame_number() usually don't
603*4882a593Smuzhiyun * cover the whole 11-bits SOF range (0-2047) but are limited to the HCI frame
604*4882a593Smuzhiyun * schedule window. They can be limited to 8, 9 or 10 bits depending on the host
605*4882a593Smuzhiyun * controller and its configuration.
606*4882a593Smuzhiyun *
607*4882a593Smuzhiyun * We thus need to recover the SOF value corresponding to the host frame number.
608*4882a593Smuzhiyun * As the device and host frame numbers are sampled in a short interval, the
609*4882a593Smuzhiyun * difference between their values should be equal to a small delta plus an
610*4882a593Smuzhiyun * integer multiple of 256 caused by the host frame number limited precision.
611*4882a593Smuzhiyun *
612*4882a593Smuzhiyun * To obtain the recovered host SOF value, compute the small delta by masking
613*4882a593Smuzhiyun * the high bits of the host frame counter and device SOF difference and add it
614*4882a593Smuzhiyun * to the device SOF value.
615*4882a593Smuzhiyun */
uvc_video_clock_host_sof(const struct uvc_clock_sample * sample)616*4882a593Smuzhiyun static u16 uvc_video_clock_host_sof(const struct uvc_clock_sample *sample)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun /* The delta value can be negative. */
619*4882a593Smuzhiyun s8 delta_sof;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun delta_sof = (sample->host_sof - sample->dev_sof) & 255;
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun return (sample->dev_sof + delta_sof) & 2047;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /*
627*4882a593Smuzhiyun * uvc_video_clock_update - Update the buffer timestamp
628*4882a593Smuzhiyun *
629*4882a593Smuzhiyun * This function converts the buffer PTS timestamp to the host clock domain by
630*4882a593Smuzhiyun * going through the USB SOF clock domain and stores the result in the V4L2
631*4882a593Smuzhiyun * buffer timestamp field.
632*4882a593Smuzhiyun *
633*4882a593Smuzhiyun * The relationship between the device clock and the host clock isn't known.
634*4882a593Smuzhiyun * However, the device and the host share the common USB SOF clock which can be
635*4882a593Smuzhiyun * used to recover that relationship.
636*4882a593Smuzhiyun *
637*4882a593Smuzhiyun * The relationship between the device clock and the USB SOF clock is considered
638*4882a593Smuzhiyun * to be linear over the clock samples sliding window and is given by
639*4882a593Smuzhiyun *
640*4882a593Smuzhiyun * SOF = m * PTS + p
641*4882a593Smuzhiyun *
642*4882a593Smuzhiyun * Several methods to compute the slope (m) and intercept (p) can be used. As
643*4882a593Smuzhiyun * the clock drift should be small compared to the sliding window size, we
644*4882a593Smuzhiyun * assume that the line that goes through the points at both ends of the window
645*4882a593Smuzhiyun * is a good approximation. Naming those points P1 and P2, we get
646*4882a593Smuzhiyun *
647*4882a593Smuzhiyun * SOF = (SOF2 - SOF1) / (STC2 - STC1) * PTS
648*4882a593Smuzhiyun * + (SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1)
649*4882a593Smuzhiyun *
650*4882a593Smuzhiyun * or
651*4882a593Smuzhiyun *
652*4882a593Smuzhiyun * SOF = ((SOF2 - SOF1) * PTS + SOF1 * STC2 - SOF2 * STC1) / (STC2 - STC1) (1)
653*4882a593Smuzhiyun *
654*4882a593Smuzhiyun * to avoid losing precision in the division. Similarly, the host timestamp is
655*4882a593Smuzhiyun * computed with
656*4882a593Smuzhiyun *
657*4882a593Smuzhiyun * TS = ((TS2 - TS1) * SOF + TS1 * SOF2 - TS2 * SOF1) / (SOF2 - SOF1) (2)
658*4882a593Smuzhiyun *
659*4882a593Smuzhiyun * SOF values are coded on 11 bits by USB. We extend their precision with 16
660*4882a593Smuzhiyun * decimal bits, leading to a 11.16 coding.
661*4882a593Smuzhiyun *
662*4882a593Smuzhiyun * TODO: To avoid surprises with device clock values, PTS/STC timestamps should
663*4882a593Smuzhiyun * be normalized using the nominal device clock frequency reported through the
664*4882a593Smuzhiyun * UVC descriptors.
665*4882a593Smuzhiyun *
666*4882a593Smuzhiyun * Both the PTS/STC and SOF counters roll over, after a fixed but device
667*4882a593Smuzhiyun * specific amount of time for PTS/STC and after 2048ms for SOF. As long as the
668*4882a593Smuzhiyun * sliding window size is smaller than the rollover period, differences computed
669*4882a593Smuzhiyun * on unsigned integers will produce the correct result. However, the p term in
670*4882a593Smuzhiyun * the linear relations will be miscomputed.
671*4882a593Smuzhiyun *
672*4882a593Smuzhiyun * To fix the issue, we subtract a constant from the PTS and STC values to bring
673*4882a593Smuzhiyun * PTS to half the 32 bit STC range. The sliding window STC values then fit into
674*4882a593Smuzhiyun * the 32 bit range without any rollover.
675*4882a593Smuzhiyun *
676*4882a593Smuzhiyun * Similarly, we add 2048 to the device SOF values to make sure that the SOF
677*4882a593Smuzhiyun * computed by (1) will never be smaller than 0. This offset is then compensated
678*4882a593Smuzhiyun * by adding 2048 to the SOF values used in (2). However, this doesn't prevent
679*4882a593Smuzhiyun * rollovers between (1) and (2): the SOF value computed by (1) can be slightly
680*4882a593Smuzhiyun * lower than 4096, and the host SOF counters can have rolled over to 2048. This
681*4882a593Smuzhiyun * case is handled by subtracting 2048 from the SOF value if it exceeds the host
682*4882a593Smuzhiyun * SOF value at the end of the sliding window.
683*4882a593Smuzhiyun *
684*4882a593Smuzhiyun * Finally we subtract a constant from the host timestamps to bring the first
685*4882a593Smuzhiyun * timestamp of the sliding window to 1s.
686*4882a593Smuzhiyun */
uvc_video_clock_update(struct uvc_streaming * stream,struct vb2_v4l2_buffer * vbuf,struct uvc_buffer * buf)687*4882a593Smuzhiyun void uvc_video_clock_update(struct uvc_streaming *stream,
688*4882a593Smuzhiyun struct vb2_v4l2_buffer *vbuf,
689*4882a593Smuzhiyun struct uvc_buffer *buf)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun struct uvc_clock *clock = &stream->clock;
692*4882a593Smuzhiyun struct uvc_clock_sample *first;
693*4882a593Smuzhiyun struct uvc_clock_sample *last;
694*4882a593Smuzhiyun unsigned long flags;
695*4882a593Smuzhiyun u64 timestamp;
696*4882a593Smuzhiyun u32 delta_stc;
697*4882a593Smuzhiyun u32 y1, y2;
698*4882a593Smuzhiyun u32 x1, x2;
699*4882a593Smuzhiyun u32 mean;
700*4882a593Smuzhiyun u32 sof;
701*4882a593Smuzhiyun u64 y;
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun if (!uvc_hw_timestamps_param)
704*4882a593Smuzhiyun return;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun /*
707*4882a593Smuzhiyun * We will get called from __vb2_queue_cancel() if there are buffers
708*4882a593Smuzhiyun * done but not dequeued by the user, but the sample array has already
709*4882a593Smuzhiyun * been released at that time. Just bail out in that case.
710*4882a593Smuzhiyun */
711*4882a593Smuzhiyun if (!clock->samples)
712*4882a593Smuzhiyun return;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun spin_lock_irqsave(&clock->lock, flags);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun if (clock->count < clock->size)
717*4882a593Smuzhiyun goto done;
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun first = &clock->samples[clock->head];
720*4882a593Smuzhiyun last = &clock->samples[(clock->head - 1) % clock->size];
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /* First step, PTS to SOF conversion. */
723*4882a593Smuzhiyun delta_stc = buf->pts - (1UL << 31);
724*4882a593Smuzhiyun x1 = first->dev_stc - delta_stc;
725*4882a593Smuzhiyun x2 = last->dev_stc - delta_stc;
726*4882a593Smuzhiyun if (x1 == x2)
727*4882a593Smuzhiyun goto done;
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun y1 = (first->dev_sof + 2048) << 16;
730*4882a593Smuzhiyun y2 = (last->dev_sof + 2048) << 16;
731*4882a593Smuzhiyun if (y2 < y1)
732*4882a593Smuzhiyun y2 += 2048 << 16;
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2
735*4882a593Smuzhiyun - (u64)y2 * (u64)x1;
736*4882a593Smuzhiyun y = div_u64(y, x2 - x1);
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun sof = y;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun uvc_trace(UVC_TRACE_CLOCK, "%s: PTS %u y %llu.%06llu SOF %u.%06llu "
741*4882a593Smuzhiyun "(x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n",
742*4882a593Smuzhiyun stream->dev->name, buf->pts,
743*4882a593Smuzhiyun y >> 16, div_u64((y & 0xffff) * 1000000, 65536),
744*4882a593Smuzhiyun sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
745*4882a593Smuzhiyun x1, x2, y1, y2, clock->sof_offset);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun /* Second step, SOF to host clock conversion. */
748*4882a593Smuzhiyun x1 = (uvc_video_clock_host_sof(first) + 2048) << 16;
749*4882a593Smuzhiyun x2 = (uvc_video_clock_host_sof(last) + 2048) << 16;
750*4882a593Smuzhiyun if (x2 < x1)
751*4882a593Smuzhiyun x2 += 2048 << 16;
752*4882a593Smuzhiyun if (x1 == x2)
753*4882a593Smuzhiyun goto done;
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun y1 = NSEC_PER_SEC;
756*4882a593Smuzhiyun y2 = (u32)ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1;
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun /* Interpolated and host SOF timestamps can wrap around at slightly
759*4882a593Smuzhiyun * different times. Handle this by adding or removing 2048 to or from
760*4882a593Smuzhiyun * the computed SOF value to keep it close to the SOF samples mean
761*4882a593Smuzhiyun * value.
762*4882a593Smuzhiyun */
763*4882a593Smuzhiyun mean = (x1 + x2) / 2;
764*4882a593Smuzhiyun if (mean - (1024 << 16) > sof)
765*4882a593Smuzhiyun sof += 2048 << 16;
766*4882a593Smuzhiyun else if (sof > mean + (1024 << 16))
767*4882a593Smuzhiyun sof -= 2048 << 16;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun y = (u64)(y2 - y1) * (u64)sof + (u64)y1 * (u64)x2
770*4882a593Smuzhiyun - (u64)y2 * (u64)x1;
771*4882a593Smuzhiyun y = div_u64(y, x2 - x1);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun timestamp = ktime_to_ns(first->host_time) + y - y1;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun uvc_trace(UVC_TRACE_CLOCK, "%s: SOF %u.%06llu y %llu ts %llu "
776*4882a593Smuzhiyun "buf ts %llu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n",
777*4882a593Smuzhiyun stream->dev->name,
778*4882a593Smuzhiyun sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536),
779*4882a593Smuzhiyun y, timestamp, vbuf->vb2_buf.timestamp,
780*4882a593Smuzhiyun x1, first->host_sof, first->dev_sof,
781*4882a593Smuzhiyun x2, last->host_sof, last->dev_sof, y1, y2);
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun /* Update the V4L2 buffer. */
784*4882a593Smuzhiyun vbuf->vb2_buf.timestamp = timestamp;
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun done:
787*4882a593Smuzhiyun spin_unlock_irqrestore(&clock->lock, flags);
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /* ------------------------------------------------------------------------
791*4882a593Smuzhiyun * Stream statistics
792*4882a593Smuzhiyun */
793*4882a593Smuzhiyun
uvc_video_stats_decode(struct uvc_streaming * stream,const u8 * data,int len)794*4882a593Smuzhiyun static void uvc_video_stats_decode(struct uvc_streaming *stream,
795*4882a593Smuzhiyun const u8 *data, int len)
796*4882a593Smuzhiyun {
797*4882a593Smuzhiyun unsigned int header_size;
798*4882a593Smuzhiyun bool has_pts = false;
799*4882a593Smuzhiyun bool has_scr = false;
800*4882a593Smuzhiyun u16 scr_sof;
801*4882a593Smuzhiyun u32 scr_stc;
802*4882a593Smuzhiyun u32 pts;
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun if (stream->stats.stream.nb_frames == 0 &&
805*4882a593Smuzhiyun stream->stats.frame.nb_packets == 0)
806*4882a593Smuzhiyun stream->stats.stream.start_ts = ktime_get();
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) {
809*4882a593Smuzhiyun case UVC_STREAM_PTS | UVC_STREAM_SCR:
810*4882a593Smuzhiyun header_size = 12;
811*4882a593Smuzhiyun has_pts = true;
812*4882a593Smuzhiyun has_scr = true;
813*4882a593Smuzhiyun break;
814*4882a593Smuzhiyun case UVC_STREAM_PTS:
815*4882a593Smuzhiyun header_size = 6;
816*4882a593Smuzhiyun has_pts = true;
817*4882a593Smuzhiyun break;
818*4882a593Smuzhiyun case UVC_STREAM_SCR:
819*4882a593Smuzhiyun header_size = 8;
820*4882a593Smuzhiyun has_scr = true;
821*4882a593Smuzhiyun break;
822*4882a593Smuzhiyun default:
823*4882a593Smuzhiyun header_size = 2;
824*4882a593Smuzhiyun break;
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun /* Check for invalid headers. */
828*4882a593Smuzhiyun if (len < header_size || data[0] < header_size) {
829*4882a593Smuzhiyun stream->stats.frame.nb_invalid++;
830*4882a593Smuzhiyun return;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /* Extract the timestamps. */
834*4882a593Smuzhiyun if (has_pts)
835*4882a593Smuzhiyun pts = get_unaligned_le32(&data[2]);
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun if (has_scr) {
838*4882a593Smuzhiyun scr_stc = get_unaligned_le32(&data[header_size - 6]);
839*4882a593Smuzhiyun scr_sof = get_unaligned_le16(&data[header_size - 2]);
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun /* Is PTS constant through the whole frame ? */
843*4882a593Smuzhiyun if (has_pts && stream->stats.frame.nb_pts) {
844*4882a593Smuzhiyun if (stream->stats.frame.pts != pts) {
845*4882a593Smuzhiyun stream->stats.frame.nb_pts_diffs++;
846*4882a593Smuzhiyun stream->stats.frame.last_pts_diff =
847*4882a593Smuzhiyun stream->stats.frame.nb_packets;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun if (has_pts) {
852*4882a593Smuzhiyun stream->stats.frame.nb_pts++;
853*4882a593Smuzhiyun stream->stats.frame.pts = pts;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun /* Do all frames have a PTS in their first non-empty packet, or before
857*4882a593Smuzhiyun * their first empty packet ?
858*4882a593Smuzhiyun */
859*4882a593Smuzhiyun if (stream->stats.frame.size == 0) {
860*4882a593Smuzhiyun if (len > header_size)
861*4882a593Smuzhiyun stream->stats.frame.has_initial_pts = has_pts;
862*4882a593Smuzhiyun if (len == header_size && has_pts)
863*4882a593Smuzhiyun stream->stats.frame.has_early_pts = true;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /* Do the SCR.STC and SCR.SOF fields vary through the frame ? */
867*4882a593Smuzhiyun if (has_scr && stream->stats.frame.nb_scr) {
868*4882a593Smuzhiyun if (stream->stats.frame.scr_stc != scr_stc)
869*4882a593Smuzhiyun stream->stats.frame.nb_scr_diffs++;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun if (has_scr) {
873*4882a593Smuzhiyun /* Expand the SOF counter to 32 bits and store its value. */
874*4882a593Smuzhiyun if (stream->stats.stream.nb_frames > 0 ||
875*4882a593Smuzhiyun stream->stats.frame.nb_scr > 0)
876*4882a593Smuzhiyun stream->stats.stream.scr_sof_count +=
877*4882a593Smuzhiyun (scr_sof - stream->stats.stream.scr_sof) % 2048;
878*4882a593Smuzhiyun stream->stats.stream.scr_sof = scr_sof;
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun stream->stats.frame.nb_scr++;
881*4882a593Smuzhiyun stream->stats.frame.scr_stc = scr_stc;
882*4882a593Smuzhiyun stream->stats.frame.scr_sof = scr_sof;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun if (scr_sof < stream->stats.stream.min_sof)
885*4882a593Smuzhiyun stream->stats.stream.min_sof = scr_sof;
886*4882a593Smuzhiyun if (scr_sof > stream->stats.stream.max_sof)
887*4882a593Smuzhiyun stream->stats.stream.max_sof = scr_sof;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun /* Record the first non-empty packet number. */
891*4882a593Smuzhiyun if (stream->stats.frame.size == 0 && len > header_size)
892*4882a593Smuzhiyun stream->stats.frame.first_data = stream->stats.frame.nb_packets;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /* Update the frame size. */
895*4882a593Smuzhiyun stream->stats.frame.size += len - header_size;
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun /* Update the packets counters. */
898*4882a593Smuzhiyun stream->stats.frame.nb_packets++;
899*4882a593Smuzhiyun if (len <= header_size)
900*4882a593Smuzhiyun stream->stats.frame.nb_empty++;
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun if (data[1] & UVC_STREAM_ERR)
903*4882a593Smuzhiyun stream->stats.frame.nb_errors++;
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun
uvc_video_stats_update(struct uvc_streaming * stream)906*4882a593Smuzhiyun static void uvc_video_stats_update(struct uvc_streaming *stream)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun struct uvc_stats_frame *frame = &stream->stats.frame;
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun uvc_trace(UVC_TRACE_STATS, "frame %u stats: %u/%u/%u packets, "
911*4882a593Smuzhiyun "%u/%u/%u pts (%searly %sinitial), %u/%u scr, "
912*4882a593Smuzhiyun "last pts/stc/sof %u/%u/%u\n",
913*4882a593Smuzhiyun stream->sequence, frame->first_data,
914*4882a593Smuzhiyun frame->nb_packets - frame->nb_empty, frame->nb_packets,
915*4882a593Smuzhiyun frame->nb_pts_diffs, frame->last_pts_diff, frame->nb_pts,
916*4882a593Smuzhiyun frame->has_early_pts ? "" : "!",
917*4882a593Smuzhiyun frame->has_initial_pts ? "" : "!",
918*4882a593Smuzhiyun frame->nb_scr_diffs, frame->nb_scr,
919*4882a593Smuzhiyun frame->pts, frame->scr_stc, frame->scr_sof);
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun stream->stats.stream.nb_frames++;
922*4882a593Smuzhiyun stream->stats.stream.nb_packets += stream->stats.frame.nb_packets;
923*4882a593Smuzhiyun stream->stats.stream.nb_empty += stream->stats.frame.nb_empty;
924*4882a593Smuzhiyun stream->stats.stream.nb_errors += stream->stats.frame.nb_errors;
925*4882a593Smuzhiyun stream->stats.stream.nb_invalid += stream->stats.frame.nb_invalid;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun if (frame->has_early_pts)
928*4882a593Smuzhiyun stream->stats.stream.nb_pts_early++;
929*4882a593Smuzhiyun if (frame->has_initial_pts)
930*4882a593Smuzhiyun stream->stats.stream.nb_pts_initial++;
931*4882a593Smuzhiyun if (frame->last_pts_diff <= frame->first_data)
932*4882a593Smuzhiyun stream->stats.stream.nb_pts_constant++;
933*4882a593Smuzhiyun if (frame->nb_scr >= frame->nb_packets - frame->nb_empty)
934*4882a593Smuzhiyun stream->stats.stream.nb_scr_count_ok++;
935*4882a593Smuzhiyun if (frame->nb_scr_diffs + 1 == frame->nb_scr)
936*4882a593Smuzhiyun stream->stats.stream.nb_scr_diffs_ok++;
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun memset(&stream->stats.frame, 0, sizeof(stream->stats.frame));
939*4882a593Smuzhiyun }
940*4882a593Smuzhiyun
uvc_video_stats_dump(struct uvc_streaming * stream,char * buf,size_t size)941*4882a593Smuzhiyun size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
942*4882a593Smuzhiyun size_t size)
943*4882a593Smuzhiyun {
944*4882a593Smuzhiyun unsigned int scr_sof_freq;
945*4882a593Smuzhiyun unsigned int duration;
946*4882a593Smuzhiyun size_t count = 0;
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun /* Compute the SCR.SOF frequency estimate. At the nominal 1kHz SOF
949*4882a593Smuzhiyun * frequency this will not overflow before more than 1h.
950*4882a593Smuzhiyun */
951*4882a593Smuzhiyun duration = ktime_ms_delta(stream->stats.stream.stop_ts,
952*4882a593Smuzhiyun stream->stats.stream.start_ts);
953*4882a593Smuzhiyun if (duration != 0)
954*4882a593Smuzhiyun scr_sof_freq = stream->stats.stream.scr_sof_count * 1000
955*4882a593Smuzhiyun / duration;
956*4882a593Smuzhiyun else
957*4882a593Smuzhiyun scr_sof_freq = 0;
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun count += scnprintf(buf + count, size - count,
960*4882a593Smuzhiyun "frames: %u\npackets: %u\nempty: %u\n"
961*4882a593Smuzhiyun "errors: %u\ninvalid: %u\n",
962*4882a593Smuzhiyun stream->stats.stream.nb_frames,
963*4882a593Smuzhiyun stream->stats.stream.nb_packets,
964*4882a593Smuzhiyun stream->stats.stream.nb_empty,
965*4882a593Smuzhiyun stream->stats.stream.nb_errors,
966*4882a593Smuzhiyun stream->stats.stream.nb_invalid);
967*4882a593Smuzhiyun count += scnprintf(buf + count, size - count,
968*4882a593Smuzhiyun "pts: %u early, %u initial, %u ok\n",
969*4882a593Smuzhiyun stream->stats.stream.nb_pts_early,
970*4882a593Smuzhiyun stream->stats.stream.nb_pts_initial,
971*4882a593Smuzhiyun stream->stats.stream.nb_pts_constant);
972*4882a593Smuzhiyun count += scnprintf(buf + count, size - count,
973*4882a593Smuzhiyun "scr: %u count ok, %u diff ok\n",
974*4882a593Smuzhiyun stream->stats.stream.nb_scr_count_ok,
975*4882a593Smuzhiyun stream->stats.stream.nb_scr_diffs_ok);
976*4882a593Smuzhiyun count += scnprintf(buf + count, size - count,
977*4882a593Smuzhiyun "sof: %u <= sof <= %u, freq %u.%03u kHz\n",
978*4882a593Smuzhiyun stream->stats.stream.min_sof,
979*4882a593Smuzhiyun stream->stats.stream.max_sof,
980*4882a593Smuzhiyun scr_sof_freq / 1000, scr_sof_freq % 1000);
981*4882a593Smuzhiyun
982*4882a593Smuzhiyun return count;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun
uvc_video_stats_start(struct uvc_streaming * stream)985*4882a593Smuzhiyun static void uvc_video_stats_start(struct uvc_streaming *stream)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun memset(&stream->stats, 0, sizeof(stream->stats));
988*4882a593Smuzhiyun stream->stats.stream.min_sof = 2048;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun
uvc_video_stats_stop(struct uvc_streaming * stream)991*4882a593Smuzhiyun static void uvc_video_stats_stop(struct uvc_streaming *stream)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun stream->stats.stream.stop_ts = ktime_get();
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun /* ------------------------------------------------------------------------
997*4882a593Smuzhiyun * Video codecs
998*4882a593Smuzhiyun */
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun /* Video payload decoding is handled by uvc_video_decode_start(),
1001*4882a593Smuzhiyun * uvc_video_decode_data() and uvc_video_decode_end().
1002*4882a593Smuzhiyun *
1003*4882a593Smuzhiyun * uvc_video_decode_start is called with URB data at the start of a bulk or
1004*4882a593Smuzhiyun * isochronous payload. It processes header data and returns the header size
1005*4882a593Smuzhiyun * in bytes if successful. If an error occurs, it returns a negative error
1006*4882a593Smuzhiyun * code. The following error codes have special meanings.
1007*4882a593Smuzhiyun *
1008*4882a593Smuzhiyun * - EAGAIN informs the caller that the current video buffer should be marked
1009*4882a593Smuzhiyun * as done, and that the function should be called again with the same data
1010*4882a593Smuzhiyun * and a new video buffer. This is used when end of frame conditions can be
1011*4882a593Smuzhiyun * reliably detected at the beginning of the next frame only.
1012*4882a593Smuzhiyun *
1013*4882a593Smuzhiyun * If an error other than -EAGAIN is returned, the caller will drop the current
1014*4882a593Smuzhiyun * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
1015*4882a593Smuzhiyun * made until the next payload. -ENODATA can be used to drop the current
1016*4882a593Smuzhiyun * payload if no other error code is appropriate.
1017*4882a593Smuzhiyun *
1018*4882a593Smuzhiyun * uvc_video_decode_data is called for every URB with URB data. It copies the
1019*4882a593Smuzhiyun * data to the video buffer.
1020*4882a593Smuzhiyun *
1021*4882a593Smuzhiyun * uvc_video_decode_end is called with header data at the end of a bulk or
1022*4882a593Smuzhiyun * isochronous payload. It performs any additional header data processing and
1023*4882a593Smuzhiyun * returns 0 or a negative error code if an error occurred. As header data have
1024*4882a593Smuzhiyun * already been processed by uvc_video_decode_start, this functions isn't
1025*4882a593Smuzhiyun * required to perform sanity checks a second time.
1026*4882a593Smuzhiyun *
1027*4882a593Smuzhiyun * For isochronous transfers where a payload is always transferred in a single
1028*4882a593Smuzhiyun * URB, the three functions will be called in a row.
1029*4882a593Smuzhiyun *
1030*4882a593Smuzhiyun * To let the decoder process header data and update its internal state even
1031*4882a593Smuzhiyun * when no video buffer is available, uvc_video_decode_start must be prepared
1032*4882a593Smuzhiyun * to be called with a NULL buf parameter. uvc_video_decode_data and
1033*4882a593Smuzhiyun * uvc_video_decode_end will never be called with a NULL buffer.
1034*4882a593Smuzhiyun */
uvc_video_decode_start(struct uvc_streaming * stream,struct uvc_buffer * buf,const u8 * data,int len)1035*4882a593Smuzhiyun static int uvc_video_decode_start(struct uvc_streaming *stream,
1036*4882a593Smuzhiyun struct uvc_buffer *buf, const u8 *data, int len)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun u8 fid;
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun /* Sanity checks:
1041*4882a593Smuzhiyun * - packet must be at least 2 bytes long
1042*4882a593Smuzhiyun * - bHeaderLength value must be at least 2 bytes (see above)
1043*4882a593Smuzhiyun * - bHeaderLength value can't be larger than the packet size.
1044*4882a593Smuzhiyun */
1045*4882a593Smuzhiyun if (len < 2 || data[0] < 2 || data[0] > len) {
1046*4882a593Smuzhiyun stream->stats.frame.nb_invalid++;
1047*4882a593Smuzhiyun return -EINVAL;
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun fid = data[1] & UVC_STREAM_FID;
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun /* Increase the sequence number regardless of any buffer states, so
1053*4882a593Smuzhiyun * that discontinuous sequence numbers always indicate lost frames.
1054*4882a593Smuzhiyun */
1055*4882a593Smuzhiyun if (stream->last_fid != fid) {
1056*4882a593Smuzhiyun stream->sequence++;
1057*4882a593Smuzhiyun if (stream->sequence)
1058*4882a593Smuzhiyun uvc_video_stats_update(stream);
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun uvc_video_clock_decode(stream, buf, data, len);
1062*4882a593Smuzhiyun uvc_video_stats_decode(stream, data, len);
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun /* Store the payload FID bit and return immediately when the buffer is
1065*4882a593Smuzhiyun * NULL.
1066*4882a593Smuzhiyun */
1067*4882a593Smuzhiyun if (buf == NULL) {
1068*4882a593Smuzhiyun stream->last_fid = fid;
1069*4882a593Smuzhiyun return -ENODATA;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun /* Mark the buffer as bad if the error bit is set. */
1073*4882a593Smuzhiyun if (data[1] & UVC_STREAM_ERR) {
1074*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "Marking buffer as bad (error bit "
1075*4882a593Smuzhiyun "set).\n");
1076*4882a593Smuzhiyun buf->error = 1;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /* Synchronize to the input stream by waiting for the FID bit to be
1080*4882a593Smuzhiyun * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
1081*4882a593Smuzhiyun * stream->last_fid is initialized to -1, so the first isochronous
1082*4882a593Smuzhiyun * frame will always be in sync.
1083*4882a593Smuzhiyun *
1084*4882a593Smuzhiyun * If the device doesn't toggle the FID bit, invert stream->last_fid
1085*4882a593Smuzhiyun * when the EOF bit is set to force synchronisation on the next packet.
1086*4882a593Smuzhiyun */
1087*4882a593Smuzhiyun if (buf->state != UVC_BUF_STATE_ACTIVE) {
1088*4882a593Smuzhiyun if (fid == stream->last_fid) {
1089*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
1090*4882a593Smuzhiyun "sync).\n");
1091*4882a593Smuzhiyun if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
1092*4882a593Smuzhiyun (data[1] & UVC_STREAM_EOF))
1093*4882a593Smuzhiyun stream->last_fid ^= UVC_STREAM_FID;
1094*4882a593Smuzhiyun return -ENODATA;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun buf->buf.field = V4L2_FIELD_NONE;
1098*4882a593Smuzhiyun buf->buf.sequence = stream->sequence;
1099*4882a593Smuzhiyun buf->buf.vb2_buf.timestamp = ktime_to_ns(uvc_video_get_time());
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun /* TODO: Handle PTS and SCR. */
1102*4882a593Smuzhiyun buf->state = UVC_BUF_STATE_ACTIVE;
1103*4882a593Smuzhiyun }
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun /* Mark the buffer as done if we're at the beginning of a new frame.
1106*4882a593Smuzhiyun * End of frame detection is better implemented by checking the EOF
1107*4882a593Smuzhiyun * bit (FID bit toggling is delayed by one frame compared to the EOF
1108*4882a593Smuzhiyun * bit), but some devices don't set the bit at end of frame (and the
1109*4882a593Smuzhiyun * last payload can be lost anyway). We thus must check if the FID has
1110*4882a593Smuzhiyun * been toggled.
1111*4882a593Smuzhiyun *
1112*4882a593Smuzhiyun * stream->last_fid is initialized to -1, so the first isochronous
1113*4882a593Smuzhiyun * frame will never trigger an end of frame detection.
1114*4882a593Smuzhiyun *
1115*4882a593Smuzhiyun * Empty buffers (bytesused == 0) don't trigger end of frame detection
1116*4882a593Smuzhiyun * as it doesn't make sense to return an empty buffer. This also
1117*4882a593Smuzhiyun * avoids detecting end of frame conditions at FID toggling if the
1118*4882a593Smuzhiyun * previous payload had the EOF bit set.
1119*4882a593Smuzhiyun */
1120*4882a593Smuzhiyun if (fid != stream->last_fid && buf->bytesused != 0) {
1121*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
1122*4882a593Smuzhiyun "toggled).\n");
1123*4882a593Smuzhiyun buf->state = UVC_BUF_STATE_READY;
1124*4882a593Smuzhiyun return -EAGAIN;
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun stream->last_fid = fid;
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun return data[0];
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun /*
1133*4882a593Smuzhiyun * uvc_video_decode_data_work: Asynchronous memcpy processing
1134*4882a593Smuzhiyun *
1135*4882a593Smuzhiyun * Copy URB data to video buffers in process context, releasing buffer
1136*4882a593Smuzhiyun * references and requeuing the URB when done.
1137*4882a593Smuzhiyun */
uvc_video_copy_data_work(struct work_struct * work)1138*4882a593Smuzhiyun static void uvc_video_copy_data_work(struct work_struct *work)
1139*4882a593Smuzhiyun {
1140*4882a593Smuzhiyun struct uvc_urb *uvc_urb = container_of(work, struct uvc_urb, work);
1141*4882a593Smuzhiyun unsigned int i;
1142*4882a593Smuzhiyun int ret;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun for (i = 0; i < uvc_urb->async_operations; i++) {
1145*4882a593Smuzhiyun struct uvc_copy_op *op = &uvc_urb->copy_operations[i];
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun memcpy(op->dst, op->src, op->len);
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun /* Release reference taken on this buffer. */
1150*4882a593Smuzhiyun uvc_queue_buffer_release(op->buf);
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun ret = usb_submit_urb(uvc_urb->urb, GFP_KERNEL);
1154*4882a593Smuzhiyun if (ret < 0)
1155*4882a593Smuzhiyun uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
1156*4882a593Smuzhiyun ret);
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun
uvc_video_decode_data(struct uvc_urb * uvc_urb,struct uvc_buffer * buf,const u8 * data,int len)1159*4882a593Smuzhiyun static void uvc_video_decode_data(struct uvc_urb *uvc_urb,
1160*4882a593Smuzhiyun struct uvc_buffer *buf, const u8 *data, int len)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun unsigned int active_op = uvc_urb->async_operations;
1163*4882a593Smuzhiyun struct uvc_copy_op *op = &uvc_urb->copy_operations[active_op];
1164*4882a593Smuzhiyun unsigned int maxlen;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun if (len <= 0)
1167*4882a593Smuzhiyun return;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun maxlen = buf->length - buf->bytesused;
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun /* Take a buffer reference for async work. */
1172*4882a593Smuzhiyun kref_get(&buf->ref);
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun op->buf = buf;
1175*4882a593Smuzhiyun op->src = data;
1176*4882a593Smuzhiyun op->dst = buf->mem + buf->bytesused;
1177*4882a593Smuzhiyun op->len = min_t(unsigned int, len, maxlen);
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun buf->bytesused += op->len;
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun /* Complete the current frame if the buffer size was exceeded. */
1182*4882a593Smuzhiyun if (len > maxlen) {
1183*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
1184*4882a593Smuzhiyun buf->error = 1;
1185*4882a593Smuzhiyun buf->state = UVC_BUF_STATE_READY;
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun uvc_urb->async_operations++;
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun
uvc_video_decode_end(struct uvc_streaming * stream,struct uvc_buffer * buf,const u8 * data,int len)1191*4882a593Smuzhiyun static void uvc_video_decode_end(struct uvc_streaming *stream,
1192*4882a593Smuzhiyun struct uvc_buffer *buf, const u8 *data, int len)
1193*4882a593Smuzhiyun {
1194*4882a593Smuzhiyun /* Mark the buffer as done if the EOF marker is set. */
1195*4882a593Smuzhiyun if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) {
1196*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
1197*4882a593Smuzhiyun if (data[0] == len)
1198*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
1199*4882a593Smuzhiyun buf->state = UVC_BUF_STATE_READY;
1200*4882a593Smuzhiyun if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
1201*4882a593Smuzhiyun stream->last_fid ^= UVC_STREAM_FID;
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun /* Video payload encoding is handled by uvc_video_encode_header() and
1206*4882a593Smuzhiyun * uvc_video_encode_data(). Only bulk transfers are currently supported.
1207*4882a593Smuzhiyun *
1208*4882a593Smuzhiyun * uvc_video_encode_header is called at the start of a payload. It adds header
1209*4882a593Smuzhiyun * data to the transfer buffer and returns the header size. As the only known
1210*4882a593Smuzhiyun * UVC output device transfers a whole frame in a single payload, the EOF bit
1211*4882a593Smuzhiyun * is always set in the header.
1212*4882a593Smuzhiyun *
1213*4882a593Smuzhiyun * uvc_video_encode_data is called for every URB and copies the data from the
1214*4882a593Smuzhiyun * video buffer to the transfer buffer.
1215*4882a593Smuzhiyun */
uvc_video_encode_header(struct uvc_streaming * stream,struct uvc_buffer * buf,u8 * data,int len)1216*4882a593Smuzhiyun static int uvc_video_encode_header(struct uvc_streaming *stream,
1217*4882a593Smuzhiyun struct uvc_buffer *buf, u8 *data, int len)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun data[0] = 2; /* Header length */
1220*4882a593Smuzhiyun data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
1221*4882a593Smuzhiyun | (stream->last_fid & UVC_STREAM_FID);
1222*4882a593Smuzhiyun return 2;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun
uvc_video_encode_data(struct uvc_streaming * stream,struct uvc_buffer * buf,u8 * data,int len)1225*4882a593Smuzhiyun static int uvc_video_encode_data(struct uvc_streaming *stream,
1226*4882a593Smuzhiyun struct uvc_buffer *buf, u8 *data, int len)
1227*4882a593Smuzhiyun {
1228*4882a593Smuzhiyun struct uvc_video_queue *queue = &stream->queue;
1229*4882a593Smuzhiyun unsigned int nbytes;
1230*4882a593Smuzhiyun void *mem;
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun /* Copy video data to the URB buffer. */
1233*4882a593Smuzhiyun mem = buf->mem + queue->buf_used;
1234*4882a593Smuzhiyun nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
1235*4882a593Smuzhiyun nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
1236*4882a593Smuzhiyun nbytes);
1237*4882a593Smuzhiyun memcpy(data, mem, nbytes);
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun queue->buf_used += nbytes;
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun return nbytes;
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun /* ------------------------------------------------------------------------
1245*4882a593Smuzhiyun * Metadata
1246*4882a593Smuzhiyun */
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun /*
1249*4882a593Smuzhiyun * Additionally to the payload headers we also want to provide the user with USB
1250*4882a593Smuzhiyun * Frame Numbers and system time values. The resulting buffer is thus composed
1251*4882a593Smuzhiyun * of blocks, containing a 64-bit timestamp in nanoseconds, a 16-bit USB Frame
1252*4882a593Smuzhiyun * Number, and a copy of the payload header.
1253*4882a593Smuzhiyun *
1254*4882a593Smuzhiyun * Ideally we want to capture all payload headers for each frame. However, their
1255*4882a593Smuzhiyun * number is unknown and unbound. We thus drop headers that contain no vendor
1256*4882a593Smuzhiyun * data and that either contain no SCR value or an SCR value identical to the
1257*4882a593Smuzhiyun * previous header.
1258*4882a593Smuzhiyun */
uvc_video_decode_meta(struct uvc_streaming * stream,struct uvc_buffer * meta_buf,const u8 * mem,unsigned int length)1259*4882a593Smuzhiyun static void uvc_video_decode_meta(struct uvc_streaming *stream,
1260*4882a593Smuzhiyun struct uvc_buffer *meta_buf,
1261*4882a593Smuzhiyun const u8 *mem, unsigned int length)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun struct uvc_meta_buf *meta;
1264*4882a593Smuzhiyun size_t len_std = 2;
1265*4882a593Smuzhiyun bool has_pts, has_scr;
1266*4882a593Smuzhiyun unsigned long flags;
1267*4882a593Smuzhiyun unsigned int sof;
1268*4882a593Smuzhiyun ktime_t time;
1269*4882a593Smuzhiyun const u8 *scr;
1270*4882a593Smuzhiyun
1271*4882a593Smuzhiyun if (!meta_buf || length == 2)
1272*4882a593Smuzhiyun return;
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun if (meta_buf->length - meta_buf->bytesused <
1275*4882a593Smuzhiyun length + sizeof(meta->ns) + sizeof(meta->sof)) {
1276*4882a593Smuzhiyun meta_buf->error = 1;
1277*4882a593Smuzhiyun return;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun has_pts = mem[1] & UVC_STREAM_PTS;
1281*4882a593Smuzhiyun has_scr = mem[1] & UVC_STREAM_SCR;
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun if (has_pts) {
1284*4882a593Smuzhiyun len_std += 4;
1285*4882a593Smuzhiyun scr = mem + 6;
1286*4882a593Smuzhiyun } else {
1287*4882a593Smuzhiyun scr = mem + 2;
1288*4882a593Smuzhiyun }
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun if (has_scr)
1291*4882a593Smuzhiyun len_std += 6;
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun if (stream->meta.format == V4L2_META_FMT_UVC)
1294*4882a593Smuzhiyun length = len_std;
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun if (length == len_std && (!has_scr ||
1297*4882a593Smuzhiyun !memcmp(scr, stream->clock.last_scr, 6)))
1298*4882a593Smuzhiyun return;
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun meta = (struct uvc_meta_buf *)((u8 *)meta_buf->mem + meta_buf->bytesused);
1301*4882a593Smuzhiyun local_irq_save(flags);
1302*4882a593Smuzhiyun time = uvc_video_get_time();
1303*4882a593Smuzhiyun sof = usb_get_current_frame_number(stream->dev->udev);
1304*4882a593Smuzhiyun local_irq_restore(flags);
1305*4882a593Smuzhiyun put_unaligned(ktime_to_ns(time), &meta->ns);
1306*4882a593Smuzhiyun put_unaligned(sof, &meta->sof);
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun if (has_scr)
1309*4882a593Smuzhiyun memcpy(stream->clock.last_scr, scr, 6);
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun memcpy(&meta->length, mem, length);
1312*4882a593Smuzhiyun meta_buf->bytesused += length + sizeof(meta->ns) + sizeof(meta->sof);
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME,
1315*4882a593Smuzhiyun "%s(): t-sys %lluns, SOF %u, len %u, flags 0x%x, PTS %u, STC %u frame SOF %u\n",
1316*4882a593Smuzhiyun __func__, ktime_to_ns(time), meta->sof, meta->length,
1317*4882a593Smuzhiyun meta->flags,
1318*4882a593Smuzhiyun has_pts ? *(u32 *)meta->buf : 0,
1319*4882a593Smuzhiyun has_scr ? *(u32 *)scr : 0,
1320*4882a593Smuzhiyun has_scr ? *(u32 *)(scr + 4) & 0x7ff : 0);
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun /* ------------------------------------------------------------------------
1324*4882a593Smuzhiyun * URB handling
1325*4882a593Smuzhiyun */
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun /*
1328*4882a593Smuzhiyun * Set error flag for incomplete buffer.
1329*4882a593Smuzhiyun */
uvc_video_validate_buffer(const struct uvc_streaming * stream,struct uvc_buffer * buf)1330*4882a593Smuzhiyun static void uvc_video_validate_buffer(const struct uvc_streaming *stream,
1331*4882a593Smuzhiyun struct uvc_buffer *buf)
1332*4882a593Smuzhiyun {
1333*4882a593Smuzhiyun if (stream->ctrl.dwMaxVideoFrameSize != buf->bytesused &&
1334*4882a593Smuzhiyun !(stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED))
1335*4882a593Smuzhiyun buf->error = 1;
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun /*
1339*4882a593Smuzhiyun * Completion handler for video URBs.
1340*4882a593Smuzhiyun */
1341*4882a593Smuzhiyun
uvc_video_next_buffers(struct uvc_streaming * stream,struct uvc_buffer ** video_buf,struct uvc_buffer ** meta_buf)1342*4882a593Smuzhiyun static void uvc_video_next_buffers(struct uvc_streaming *stream,
1343*4882a593Smuzhiyun struct uvc_buffer **video_buf, struct uvc_buffer **meta_buf)
1344*4882a593Smuzhiyun {
1345*4882a593Smuzhiyun uvc_video_validate_buffer(stream, *video_buf);
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun if (*meta_buf) {
1348*4882a593Smuzhiyun struct vb2_v4l2_buffer *vb2_meta = &(*meta_buf)->buf;
1349*4882a593Smuzhiyun const struct vb2_v4l2_buffer *vb2_video = &(*video_buf)->buf;
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun vb2_meta->sequence = vb2_video->sequence;
1352*4882a593Smuzhiyun vb2_meta->field = vb2_video->field;
1353*4882a593Smuzhiyun vb2_meta->vb2_buf.timestamp = vb2_video->vb2_buf.timestamp;
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun (*meta_buf)->state = UVC_BUF_STATE_READY;
1356*4882a593Smuzhiyun if (!(*meta_buf)->error)
1357*4882a593Smuzhiyun (*meta_buf)->error = (*video_buf)->error;
1358*4882a593Smuzhiyun *meta_buf = uvc_queue_next_buffer(&stream->meta.queue,
1359*4882a593Smuzhiyun *meta_buf);
1360*4882a593Smuzhiyun }
1361*4882a593Smuzhiyun *video_buf = uvc_queue_next_buffer(&stream->queue, *video_buf);
1362*4882a593Smuzhiyun }
1363*4882a593Smuzhiyun
uvc_video_decode_isoc(struct uvc_urb * uvc_urb,struct uvc_buffer * buf,struct uvc_buffer * meta_buf)1364*4882a593Smuzhiyun static void uvc_video_decode_isoc(struct uvc_urb *uvc_urb,
1365*4882a593Smuzhiyun struct uvc_buffer *buf, struct uvc_buffer *meta_buf)
1366*4882a593Smuzhiyun {
1367*4882a593Smuzhiyun struct urb *urb = uvc_urb->urb;
1368*4882a593Smuzhiyun struct uvc_streaming *stream = uvc_urb->stream;
1369*4882a593Smuzhiyun u8 *mem;
1370*4882a593Smuzhiyun int ret, i;
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun for (i = 0; i < urb->number_of_packets; ++i) {
1373*4882a593Smuzhiyun if (urb->iso_frame_desc[i].status < 0) {
1374*4882a593Smuzhiyun uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
1375*4882a593Smuzhiyun "lost (%d).\n", urb->iso_frame_desc[i].status);
1376*4882a593Smuzhiyun /* Mark the buffer as faulty. */
1377*4882a593Smuzhiyun if (buf != NULL)
1378*4882a593Smuzhiyun buf->error = 1;
1379*4882a593Smuzhiyun continue;
1380*4882a593Smuzhiyun }
1381*4882a593Smuzhiyun
1382*4882a593Smuzhiyun /* Decode the payload header. */
1383*4882a593Smuzhiyun mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1384*4882a593Smuzhiyun do {
1385*4882a593Smuzhiyun ret = uvc_video_decode_start(stream, buf, mem,
1386*4882a593Smuzhiyun urb->iso_frame_desc[i].actual_length);
1387*4882a593Smuzhiyun if (ret == -EAGAIN)
1388*4882a593Smuzhiyun uvc_video_next_buffers(stream, &buf, &meta_buf);
1389*4882a593Smuzhiyun } while (ret == -EAGAIN);
1390*4882a593Smuzhiyun
1391*4882a593Smuzhiyun if (ret < 0)
1392*4882a593Smuzhiyun continue;
1393*4882a593Smuzhiyun
1394*4882a593Smuzhiyun uvc_video_decode_meta(stream, meta_buf, mem, ret);
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun /* Decode the payload data. */
1397*4882a593Smuzhiyun uvc_video_decode_data(uvc_urb, buf, mem + ret,
1398*4882a593Smuzhiyun urb->iso_frame_desc[i].actual_length - ret);
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun /* Process the header again. */
1401*4882a593Smuzhiyun uvc_video_decode_end(stream, buf, mem,
1402*4882a593Smuzhiyun urb->iso_frame_desc[i].actual_length);
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun if (buf->state == UVC_BUF_STATE_READY)
1405*4882a593Smuzhiyun uvc_video_next_buffers(stream, &buf, &meta_buf);
1406*4882a593Smuzhiyun }
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun
uvc_video_decode_bulk(struct uvc_urb * uvc_urb,struct uvc_buffer * buf,struct uvc_buffer * meta_buf)1409*4882a593Smuzhiyun static void uvc_video_decode_bulk(struct uvc_urb *uvc_urb,
1410*4882a593Smuzhiyun struct uvc_buffer *buf, struct uvc_buffer *meta_buf)
1411*4882a593Smuzhiyun {
1412*4882a593Smuzhiyun struct urb *urb = uvc_urb->urb;
1413*4882a593Smuzhiyun struct uvc_streaming *stream = uvc_urb->stream;
1414*4882a593Smuzhiyun u8 *mem;
1415*4882a593Smuzhiyun int len, ret;
1416*4882a593Smuzhiyun
1417*4882a593Smuzhiyun /*
1418*4882a593Smuzhiyun * Ignore ZLPs if they're not part of a frame, otherwise process them
1419*4882a593Smuzhiyun * to trigger the end of payload detection.
1420*4882a593Smuzhiyun */
1421*4882a593Smuzhiyun if (urb->actual_length == 0 && stream->bulk.header_size == 0)
1422*4882a593Smuzhiyun return;
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun mem = urb->transfer_buffer;
1425*4882a593Smuzhiyun len = urb->actual_length;
1426*4882a593Smuzhiyun stream->bulk.payload_size += len;
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun /* If the URB is the first of its payload, decode and save the
1429*4882a593Smuzhiyun * header.
1430*4882a593Smuzhiyun */
1431*4882a593Smuzhiyun if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
1432*4882a593Smuzhiyun do {
1433*4882a593Smuzhiyun ret = uvc_video_decode_start(stream, buf, mem, len);
1434*4882a593Smuzhiyun if (ret == -EAGAIN)
1435*4882a593Smuzhiyun uvc_video_next_buffers(stream, &buf, &meta_buf);
1436*4882a593Smuzhiyun } while (ret == -EAGAIN);
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun /* If an error occurred skip the rest of the payload. */
1439*4882a593Smuzhiyun if (ret < 0 || buf == NULL) {
1440*4882a593Smuzhiyun stream->bulk.skip_payload = 1;
1441*4882a593Smuzhiyun } else {
1442*4882a593Smuzhiyun memcpy(stream->bulk.header, mem, ret);
1443*4882a593Smuzhiyun stream->bulk.header_size = ret;
1444*4882a593Smuzhiyun
1445*4882a593Smuzhiyun uvc_video_decode_meta(stream, meta_buf, mem, ret);
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun mem += ret;
1448*4882a593Smuzhiyun len -= ret;
1449*4882a593Smuzhiyun }
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun
1452*4882a593Smuzhiyun /* The buffer queue might have been cancelled while a bulk transfer
1453*4882a593Smuzhiyun * was in progress, so we can reach here with buf equal to NULL. Make
1454*4882a593Smuzhiyun * sure buf is never dereferenced if NULL.
1455*4882a593Smuzhiyun */
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun /* Prepare video data for processing. */
1458*4882a593Smuzhiyun if (!stream->bulk.skip_payload && buf != NULL)
1459*4882a593Smuzhiyun uvc_video_decode_data(uvc_urb, buf, mem, len);
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun /* Detect the payload end by a URB smaller than the maximum size (or
1462*4882a593Smuzhiyun * a payload size equal to the maximum) and process the header again.
1463*4882a593Smuzhiyun */
1464*4882a593Smuzhiyun if (urb->actual_length < urb->transfer_buffer_length ||
1465*4882a593Smuzhiyun stream->bulk.payload_size >= stream->bulk.max_payload_size) {
1466*4882a593Smuzhiyun if (!stream->bulk.skip_payload && buf != NULL) {
1467*4882a593Smuzhiyun uvc_video_decode_end(stream, buf, stream->bulk.header,
1468*4882a593Smuzhiyun stream->bulk.payload_size);
1469*4882a593Smuzhiyun if (buf->state == UVC_BUF_STATE_READY)
1470*4882a593Smuzhiyun uvc_video_next_buffers(stream, &buf, &meta_buf);
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun stream->bulk.header_size = 0;
1474*4882a593Smuzhiyun stream->bulk.skip_payload = 0;
1475*4882a593Smuzhiyun stream->bulk.payload_size = 0;
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun
uvc_video_encode_bulk(struct uvc_urb * uvc_urb,struct uvc_buffer * buf,struct uvc_buffer * meta_buf)1479*4882a593Smuzhiyun static void uvc_video_encode_bulk(struct uvc_urb *uvc_urb,
1480*4882a593Smuzhiyun struct uvc_buffer *buf, struct uvc_buffer *meta_buf)
1481*4882a593Smuzhiyun {
1482*4882a593Smuzhiyun struct urb *urb = uvc_urb->urb;
1483*4882a593Smuzhiyun struct uvc_streaming *stream = uvc_urb->stream;
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun u8 *mem = urb->transfer_buffer;
1486*4882a593Smuzhiyun int len = stream->urb_size, ret;
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun if (buf == NULL) {
1489*4882a593Smuzhiyun urb->transfer_buffer_length = 0;
1490*4882a593Smuzhiyun return;
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun /* If the URB is the first of its payload, add the header. */
1494*4882a593Smuzhiyun if (stream->bulk.header_size == 0) {
1495*4882a593Smuzhiyun ret = uvc_video_encode_header(stream, buf, mem, len);
1496*4882a593Smuzhiyun stream->bulk.header_size = ret;
1497*4882a593Smuzhiyun stream->bulk.payload_size += ret;
1498*4882a593Smuzhiyun mem += ret;
1499*4882a593Smuzhiyun len -= ret;
1500*4882a593Smuzhiyun }
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun /* Process video data. */
1503*4882a593Smuzhiyun ret = uvc_video_encode_data(stream, buf, mem, len);
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun stream->bulk.payload_size += ret;
1506*4882a593Smuzhiyun len -= ret;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun if (buf->bytesused == stream->queue.buf_used ||
1509*4882a593Smuzhiyun stream->bulk.payload_size == stream->bulk.max_payload_size) {
1510*4882a593Smuzhiyun if (buf->bytesused == stream->queue.buf_used) {
1511*4882a593Smuzhiyun stream->queue.buf_used = 0;
1512*4882a593Smuzhiyun buf->state = UVC_BUF_STATE_READY;
1513*4882a593Smuzhiyun buf->buf.sequence = ++stream->sequence;
1514*4882a593Smuzhiyun uvc_queue_next_buffer(&stream->queue, buf);
1515*4882a593Smuzhiyun stream->last_fid ^= UVC_STREAM_FID;
1516*4882a593Smuzhiyun }
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun stream->bulk.header_size = 0;
1519*4882a593Smuzhiyun stream->bulk.payload_size = 0;
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun urb->transfer_buffer_length = stream->urb_size - len;
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun
uvc_video_complete(struct urb * urb)1525*4882a593Smuzhiyun static void uvc_video_complete(struct urb *urb)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun struct uvc_urb *uvc_urb = urb->context;
1528*4882a593Smuzhiyun struct uvc_streaming *stream = uvc_urb->stream;
1529*4882a593Smuzhiyun struct uvc_video_queue *queue = &stream->queue;
1530*4882a593Smuzhiyun struct uvc_video_queue *qmeta = &stream->meta.queue;
1531*4882a593Smuzhiyun struct vb2_queue *vb2_qmeta = stream->meta.vdev.queue;
1532*4882a593Smuzhiyun struct uvc_buffer *buf = NULL;
1533*4882a593Smuzhiyun struct uvc_buffer *buf_meta = NULL;
1534*4882a593Smuzhiyun unsigned long flags;
1535*4882a593Smuzhiyun int ret;
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun switch (urb->status) {
1538*4882a593Smuzhiyun case 0:
1539*4882a593Smuzhiyun break;
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun default:
1542*4882a593Smuzhiyun uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
1543*4882a593Smuzhiyun "completion handler.\n", urb->status);
1544*4882a593Smuzhiyun fallthrough;
1545*4882a593Smuzhiyun case -ENOENT: /* usb_poison_urb() called. */
1546*4882a593Smuzhiyun if (stream->frozen)
1547*4882a593Smuzhiyun return;
1548*4882a593Smuzhiyun fallthrough;
1549*4882a593Smuzhiyun case -ECONNRESET: /* usb_unlink_urb() called. */
1550*4882a593Smuzhiyun case -ESHUTDOWN: /* The endpoint is being disabled. */
1551*4882a593Smuzhiyun uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
1552*4882a593Smuzhiyun if (vb2_qmeta)
1553*4882a593Smuzhiyun uvc_queue_cancel(qmeta, urb->status == -ESHUTDOWN);
1554*4882a593Smuzhiyun return;
1555*4882a593Smuzhiyun }
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun buf = uvc_queue_get_current_buffer(queue);
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun if (vb2_qmeta) {
1560*4882a593Smuzhiyun spin_lock_irqsave(&qmeta->irqlock, flags);
1561*4882a593Smuzhiyun if (!list_empty(&qmeta->irqqueue))
1562*4882a593Smuzhiyun buf_meta = list_first_entry(&qmeta->irqqueue,
1563*4882a593Smuzhiyun struct uvc_buffer, queue);
1564*4882a593Smuzhiyun spin_unlock_irqrestore(&qmeta->irqlock, flags);
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun /* Re-initialise the URB async work. */
1568*4882a593Smuzhiyun uvc_urb->async_operations = 0;
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun /*
1571*4882a593Smuzhiyun * Process the URB headers, and optionally queue expensive memcpy tasks
1572*4882a593Smuzhiyun * to be deferred to a work queue.
1573*4882a593Smuzhiyun */
1574*4882a593Smuzhiyun stream->decode(uvc_urb, buf, buf_meta);
1575*4882a593Smuzhiyun
1576*4882a593Smuzhiyun /* If no async work is needed, resubmit the URB immediately. */
1577*4882a593Smuzhiyun if (!uvc_urb->async_operations) {
1578*4882a593Smuzhiyun ret = usb_submit_urb(uvc_urb->urb, GFP_ATOMIC);
1579*4882a593Smuzhiyun if (ret < 0)
1580*4882a593Smuzhiyun uvc_printk(KERN_ERR,
1581*4882a593Smuzhiyun "Failed to resubmit video URB (%d).\n",
1582*4882a593Smuzhiyun ret);
1583*4882a593Smuzhiyun return;
1584*4882a593Smuzhiyun }
1585*4882a593Smuzhiyun
1586*4882a593Smuzhiyun queue_work(stream->async_wq, &uvc_urb->work);
1587*4882a593Smuzhiyun }
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun /*
1590*4882a593Smuzhiyun * Free transfer buffers.
1591*4882a593Smuzhiyun */
uvc_free_urb_buffers(struct uvc_streaming * stream)1592*4882a593Smuzhiyun static void uvc_free_urb_buffers(struct uvc_streaming *stream)
1593*4882a593Smuzhiyun {
1594*4882a593Smuzhiyun struct uvc_urb *uvc_urb;
1595*4882a593Smuzhiyun
1596*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream) {
1597*4882a593Smuzhiyun if (!uvc_urb->buffer)
1598*4882a593Smuzhiyun continue;
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun #ifndef CONFIG_DMA_NONCOHERENT
1601*4882a593Smuzhiyun usb_free_coherent(stream->dev->udev, stream->urb_size,
1602*4882a593Smuzhiyun uvc_urb->buffer, uvc_urb->dma);
1603*4882a593Smuzhiyun #else
1604*4882a593Smuzhiyun kfree(uvc_urb->buffer);
1605*4882a593Smuzhiyun #endif
1606*4882a593Smuzhiyun uvc_urb->buffer = NULL;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun stream->urb_size = 0;
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun /*
1613*4882a593Smuzhiyun * Allocate transfer buffers. This function can be called with buffers
1614*4882a593Smuzhiyun * already allocated when resuming from suspend, in which case it will
1615*4882a593Smuzhiyun * return without touching the buffers.
1616*4882a593Smuzhiyun *
1617*4882a593Smuzhiyun * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
1618*4882a593Smuzhiyun * system is too low on memory try successively smaller numbers of packets
1619*4882a593Smuzhiyun * until allocation succeeds.
1620*4882a593Smuzhiyun *
1621*4882a593Smuzhiyun * Return the number of allocated packets on success or 0 when out of memory.
1622*4882a593Smuzhiyun */
uvc_alloc_urb_buffers(struct uvc_streaming * stream,unsigned int size,unsigned int psize,gfp_t gfp_flags)1623*4882a593Smuzhiyun static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
1624*4882a593Smuzhiyun unsigned int size, unsigned int psize, gfp_t gfp_flags)
1625*4882a593Smuzhiyun {
1626*4882a593Smuzhiyun unsigned int npackets;
1627*4882a593Smuzhiyun unsigned int i;
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun /* Buffers are already allocated, bail out. */
1630*4882a593Smuzhiyun if (stream->urb_size)
1631*4882a593Smuzhiyun return stream->urb_size / psize;
1632*4882a593Smuzhiyun
1633*4882a593Smuzhiyun /* Compute the number of packets. Bulk endpoints might transfer UVC
1634*4882a593Smuzhiyun * payloads across multiple URBs.
1635*4882a593Smuzhiyun */
1636*4882a593Smuzhiyun npackets = DIV_ROUND_UP(size, psize);
1637*4882a593Smuzhiyun if (npackets > UVC_MAX_PACKETS)
1638*4882a593Smuzhiyun npackets = UVC_MAX_PACKETS;
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun /* Retry allocations until one succeed. */
1641*4882a593Smuzhiyun for (; npackets > 1; npackets /= 2) {
1642*4882a593Smuzhiyun for (i = 0; i < UVC_URBS; ++i) {
1643*4882a593Smuzhiyun struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun stream->urb_size = psize * npackets;
1646*4882a593Smuzhiyun #ifndef CONFIG_DMA_NONCOHERENT
1647*4882a593Smuzhiyun uvc_urb->buffer = usb_alloc_coherent(
1648*4882a593Smuzhiyun stream->dev->udev, stream->urb_size,
1649*4882a593Smuzhiyun gfp_flags | __GFP_NOWARN, &uvc_urb->dma);
1650*4882a593Smuzhiyun #else
1651*4882a593Smuzhiyun uvc_urb->buffer =
1652*4882a593Smuzhiyun kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
1653*4882a593Smuzhiyun #endif
1654*4882a593Smuzhiyun if (!uvc_urb->buffer) {
1655*4882a593Smuzhiyun uvc_free_urb_buffers(stream);
1656*4882a593Smuzhiyun break;
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun uvc_urb->stream = stream;
1660*4882a593Smuzhiyun }
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun if (i == UVC_URBS) {
1663*4882a593Smuzhiyun uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
1664*4882a593Smuzhiyun "of %ux%u bytes each.\n", UVC_URBS, npackets,
1665*4882a593Smuzhiyun psize);
1666*4882a593Smuzhiyun return npackets;
1667*4882a593Smuzhiyun }
1668*4882a593Smuzhiyun }
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
1671*4882a593Smuzhiyun "per packet).\n", psize);
1672*4882a593Smuzhiyun return 0;
1673*4882a593Smuzhiyun }
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun /*
1676*4882a593Smuzhiyun * Uninitialize isochronous/bulk URBs and free transfer buffers.
1677*4882a593Smuzhiyun */
uvc_video_stop_transfer(struct uvc_streaming * stream,int free_buffers)1678*4882a593Smuzhiyun static void uvc_video_stop_transfer(struct uvc_streaming *stream,
1679*4882a593Smuzhiyun int free_buffers)
1680*4882a593Smuzhiyun {
1681*4882a593Smuzhiyun struct uvc_urb *uvc_urb;
1682*4882a593Smuzhiyun
1683*4882a593Smuzhiyun uvc_video_stats_stop(stream);
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun /*
1686*4882a593Smuzhiyun * We must poison the URBs rather than kill them to ensure that even
1687*4882a593Smuzhiyun * after the completion handler returns, any asynchronous workqueues
1688*4882a593Smuzhiyun * will be prevented from resubmitting the URBs.
1689*4882a593Smuzhiyun */
1690*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream)
1691*4882a593Smuzhiyun usb_poison_urb(uvc_urb->urb);
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun flush_workqueue(stream->async_wq);
1694*4882a593Smuzhiyun
1695*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream) {
1696*4882a593Smuzhiyun usb_free_urb(uvc_urb->urb);
1697*4882a593Smuzhiyun uvc_urb->urb = NULL;
1698*4882a593Smuzhiyun }
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun if (free_buffers)
1701*4882a593Smuzhiyun uvc_free_urb_buffers(stream);
1702*4882a593Smuzhiyun }
1703*4882a593Smuzhiyun
1704*4882a593Smuzhiyun /*
1705*4882a593Smuzhiyun * Compute the maximum number of bytes per interval for an endpoint.
1706*4882a593Smuzhiyun */
uvc_endpoint_max_bpi(struct usb_device * dev,struct usb_host_endpoint * ep)1707*4882a593Smuzhiyun static unsigned int uvc_endpoint_max_bpi(struct usb_device *dev,
1708*4882a593Smuzhiyun struct usb_host_endpoint *ep)
1709*4882a593Smuzhiyun {
1710*4882a593Smuzhiyun u16 psize;
1711*4882a593Smuzhiyun u16 mult;
1712*4882a593Smuzhiyun
1713*4882a593Smuzhiyun switch (dev->speed) {
1714*4882a593Smuzhiyun case USB_SPEED_SUPER:
1715*4882a593Smuzhiyun case USB_SPEED_SUPER_PLUS:
1716*4882a593Smuzhiyun return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
1717*4882a593Smuzhiyun case USB_SPEED_HIGH:
1718*4882a593Smuzhiyun psize = usb_endpoint_maxp(&ep->desc);
1719*4882a593Smuzhiyun mult = usb_endpoint_maxp_mult(&ep->desc);
1720*4882a593Smuzhiyun return psize * mult;
1721*4882a593Smuzhiyun case USB_SPEED_WIRELESS:
1722*4882a593Smuzhiyun psize = usb_endpoint_maxp(&ep->desc);
1723*4882a593Smuzhiyun return psize;
1724*4882a593Smuzhiyun default:
1725*4882a593Smuzhiyun psize = usb_endpoint_maxp(&ep->desc);
1726*4882a593Smuzhiyun return psize;
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun }
1729*4882a593Smuzhiyun
1730*4882a593Smuzhiyun /*
1731*4882a593Smuzhiyun * Initialize isochronous URBs and allocate transfer buffers. The packet size
1732*4882a593Smuzhiyun * is given by the endpoint.
1733*4882a593Smuzhiyun */
uvc_init_video_isoc(struct uvc_streaming * stream,struct usb_host_endpoint * ep,gfp_t gfp_flags)1734*4882a593Smuzhiyun static int uvc_init_video_isoc(struct uvc_streaming *stream,
1735*4882a593Smuzhiyun struct usb_host_endpoint *ep, gfp_t gfp_flags)
1736*4882a593Smuzhiyun {
1737*4882a593Smuzhiyun struct urb *urb;
1738*4882a593Smuzhiyun struct uvc_urb *uvc_urb;
1739*4882a593Smuzhiyun unsigned int npackets, i;
1740*4882a593Smuzhiyun u16 psize;
1741*4882a593Smuzhiyun u32 size;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
1744*4882a593Smuzhiyun size = stream->ctrl.dwMaxVideoFrameSize;
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1747*4882a593Smuzhiyun if (npackets == 0)
1748*4882a593Smuzhiyun return -ENOMEM;
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun size = npackets * psize;
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream) {
1753*4882a593Smuzhiyun urb = usb_alloc_urb(npackets, gfp_flags);
1754*4882a593Smuzhiyun if (urb == NULL) {
1755*4882a593Smuzhiyun uvc_video_stop_transfer(stream, 1);
1756*4882a593Smuzhiyun return -ENOMEM;
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun
1759*4882a593Smuzhiyun urb->dev = stream->dev->udev;
1760*4882a593Smuzhiyun urb->context = uvc_urb;
1761*4882a593Smuzhiyun urb->pipe = usb_rcvisocpipe(stream->dev->udev,
1762*4882a593Smuzhiyun ep->desc.bEndpointAddress);
1763*4882a593Smuzhiyun #ifndef CONFIG_DMA_NONCOHERENT
1764*4882a593Smuzhiyun urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1765*4882a593Smuzhiyun urb->transfer_dma = uvc_urb->dma;
1766*4882a593Smuzhiyun #else
1767*4882a593Smuzhiyun urb->transfer_flags = URB_ISO_ASAP;
1768*4882a593Smuzhiyun #endif
1769*4882a593Smuzhiyun urb->interval = ep->desc.bInterval;
1770*4882a593Smuzhiyun urb->transfer_buffer = uvc_urb->buffer;
1771*4882a593Smuzhiyun urb->complete = uvc_video_complete;
1772*4882a593Smuzhiyun urb->number_of_packets = npackets;
1773*4882a593Smuzhiyun urb->transfer_buffer_length = size;
1774*4882a593Smuzhiyun
1775*4882a593Smuzhiyun for (i = 0; i < npackets; ++i) {
1776*4882a593Smuzhiyun urb->iso_frame_desc[i].offset = i * psize;
1777*4882a593Smuzhiyun urb->iso_frame_desc[i].length = psize;
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun uvc_urb->urb = urb;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun return 0;
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun /*
1787*4882a593Smuzhiyun * Initialize bulk URBs and allocate transfer buffers. The packet size is
1788*4882a593Smuzhiyun * given by the endpoint.
1789*4882a593Smuzhiyun */
uvc_init_video_bulk(struct uvc_streaming * stream,struct usb_host_endpoint * ep,gfp_t gfp_flags)1790*4882a593Smuzhiyun static int uvc_init_video_bulk(struct uvc_streaming *stream,
1791*4882a593Smuzhiyun struct usb_host_endpoint *ep, gfp_t gfp_flags)
1792*4882a593Smuzhiyun {
1793*4882a593Smuzhiyun struct urb *urb;
1794*4882a593Smuzhiyun struct uvc_urb *uvc_urb;
1795*4882a593Smuzhiyun unsigned int npackets, pipe;
1796*4882a593Smuzhiyun u16 psize;
1797*4882a593Smuzhiyun u32 size;
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun psize = usb_endpoint_maxp(&ep->desc);
1800*4882a593Smuzhiyun size = stream->ctrl.dwMaxPayloadTransferSize;
1801*4882a593Smuzhiyun stream->bulk.max_payload_size = size;
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
1804*4882a593Smuzhiyun if (npackets == 0)
1805*4882a593Smuzhiyun return -ENOMEM;
1806*4882a593Smuzhiyun
1807*4882a593Smuzhiyun size = npackets * psize;
1808*4882a593Smuzhiyun
1809*4882a593Smuzhiyun if (usb_endpoint_dir_in(&ep->desc))
1810*4882a593Smuzhiyun pipe = usb_rcvbulkpipe(stream->dev->udev,
1811*4882a593Smuzhiyun ep->desc.bEndpointAddress);
1812*4882a593Smuzhiyun else
1813*4882a593Smuzhiyun pipe = usb_sndbulkpipe(stream->dev->udev,
1814*4882a593Smuzhiyun ep->desc.bEndpointAddress);
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1817*4882a593Smuzhiyun size = 0;
1818*4882a593Smuzhiyun
1819*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream) {
1820*4882a593Smuzhiyun urb = usb_alloc_urb(0, gfp_flags);
1821*4882a593Smuzhiyun if (urb == NULL) {
1822*4882a593Smuzhiyun uvc_video_stop_transfer(stream, 1);
1823*4882a593Smuzhiyun return -ENOMEM;
1824*4882a593Smuzhiyun }
1825*4882a593Smuzhiyun
1826*4882a593Smuzhiyun usb_fill_bulk_urb(urb, stream->dev->udev, pipe, uvc_urb->buffer,
1827*4882a593Smuzhiyun size, uvc_video_complete, uvc_urb);
1828*4882a593Smuzhiyun #ifndef CONFIG_DMA_NONCOHERENT
1829*4882a593Smuzhiyun urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1830*4882a593Smuzhiyun urb->transfer_dma = uvc_urb->dma;
1831*4882a593Smuzhiyun #endif
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun uvc_urb->urb = urb;
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun return 0;
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun
1839*4882a593Smuzhiyun /*
1840*4882a593Smuzhiyun * Initialize isochronous/bulk URBs and allocate transfer buffers.
1841*4882a593Smuzhiyun */
uvc_video_start_transfer(struct uvc_streaming * stream,gfp_t gfp_flags)1842*4882a593Smuzhiyun static int uvc_video_start_transfer(struct uvc_streaming *stream,
1843*4882a593Smuzhiyun gfp_t gfp_flags)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun struct usb_interface *intf = stream->intf;
1846*4882a593Smuzhiyun struct usb_host_endpoint *ep;
1847*4882a593Smuzhiyun struct uvc_urb *uvc_urb;
1848*4882a593Smuzhiyun unsigned int i;
1849*4882a593Smuzhiyun int ret;
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun stream->sequence = -1;
1852*4882a593Smuzhiyun stream->last_fid = -1;
1853*4882a593Smuzhiyun stream->bulk.header_size = 0;
1854*4882a593Smuzhiyun stream->bulk.skip_payload = 0;
1855*4882a593Smuzhiyun stream->bulk.payload_size = 0;
1856*4882a593Smuzhiyun
1857*4882a593Smuzhiyun uvc_video_stats_start(stream);
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun if (intf->num_altsetting > 1) {
1860*4882a593Smuzhiyun struct usb_host_endpoint *best_ep = NULL;
1861*4882a593Smuzhiyun unsigned int best_psize = UINT_MAX;
1862*4882a593Smuzhiyun unsigned int bandwidth;
1863*4882a593Smuzhiyun unsigned int altsetting;
1864*4882a593Smuzhiyun int intfnum = stream->intfnum;
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun /* Isochronous endpoint, select the alternate setting. */
1867*4882a593Smuzhiyun bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun if (bandwidth == 0) {
1870*4882a593Smuzhiyun uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
1871*4882a593Smuzhiyun "bandwidth, defaulting to lowest.\n");
1872*4882a593Smuzhiyun bandwidth = 1;
1873*4882a593Smuzhiyun } else {
1874*4882a593Smuzhiyun uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
1875*4882a593Smuzhiyun "B/frame bandwidth.\n", bandwidth);
1876*4882a593Smuzhiyun }
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun for (i = 0; i < intf->num_altsetting; ++i) {
1879*4882a593Smuzhiyun struct usb_host_interface *alts;
1880*4882a593Smuzhiyun unsigned int psize;
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun alts = &intf->altsetting[i];
1883*4882a593Smuzhiyun ep = uvc_find_endpoint(alts,
1884*4882a593Smuzhiyun stream->header.bEndpointAddress);
1885*4882a593Smuzhiyun if (ep == NULL)
1886*4882a593Smuzhiyun continue;
1887*4882a593Smuzhiyun
1888*4882a593Smuzhiyun /* Check if the bandwidth is high enough. */
1889*4882a593Smuzhiyun psize = uvc_endpoint_max_bpi(stream->dev->udev, ep);
1890*4882a593Smuzhiyun if (psize >= bandwidth && psize <= best_psize) {
1891*4882a593Smuzhiyun altsetting = alts->desc.bAlternateSetting;
1892*4882a593Smuzhiyun best_psize = psize;
1893*4882a593Smuzhiyun best_ep = ep;
1894*4882a593Smuzhiyun }
1895*4882a593Smuzhiyun }
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun if (best_ep == NULL) {
1898*4882a593Smuzhiyun uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
1899*4882a593Smuzhiyun "for requested bandwidth.\n");
1900*4882a593Smuzhiyun return -EIO;
1901*4882a593Smuzhiyun }
1902*4882a593Smuzhiyun
1903*4882a593Smuzhiyun uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
1904*4882a593Smuzhiyun "(%u B/frame bandwidth).\n", altsetting, best_psize);
1905*4882a593Smuzhiyun
1906*4882a593Smuzhiyun ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
1907*4882a593Smuzhiyun if (ret < 0)
1908*4882a593Smuzhiyun return ret;
1909*4882a593Smuzhiyun
1910*4882a593Smuzhiyun ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
1911*4882a593Smuzhiyun } else {
1912*4882a593Smuzhiyun /* Bulk endpoint, proceed to URB initialization. */
1913*4882a593Smuzhiyun ep = uvc_find_endpoint(&intf->altsetting[0],
1914*4882a593Smuzhiyun stream->header.bEndpointAddress);
1915*4882a593Smuzhiyun if (ep == NULL)
1916*4882a593Smuzhiyun return -EIO;
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun /* Reject broken descriptors. */
1919*4882a593Smuzhiyun if (usb_endpoint_maxp(&ep->desc) == 0)
1920*4882a593Smuzhiyun return -EIO;
1921*4882a593Smuzhiyun
1922*4882a593Smuzhiyun ret = uvc_init_video_bulk(stream, ep, gfp_flags);
1923*4882a593Smuzhiyun }
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun if (ret < 0)
1926*4882a593Smuzhiyun return ret;
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun /* Submit the URBs. */
1929*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream) {
1930*4882a593Smuzhiyun ret = usb_submit_urb(uvc_urb->urb, gfp_flags);
1931*4882a593Smuzhiyun if (ret < 0) {
1932*4882a593Smuzhiyun uvc_printk(KERN_ERR, "Failed to submit URB %u (%d).\n",
1933*4882a593Smuzhiyun uvc_urb_index(uvc_urb), ret);
1934*4882a593Smuzhiyun uvc_video_stop_transfer(stream, 1);
1935*4882a593Smuzhiyun return ret;
1936*4882a593Smuzhiyun }
1937*4882a593Smuzhiyun }
1938*4882a593Smuzhiyun
1939*4882a593Smuzhiyun /* The Logitech C920 temporarily forgets that it should not be adjusting
1940*4882a593Smuzhiyun * Exposure Absolute during init so restore controls to stored values.
1941*4882a593Smuzhiyun */
1942*4882a593Smuzhiyun if (stream->dev->quirks & UVC_QUIRK_RESTORE_CTRLS_ON_INIT)
1943*4882a593Smuzhiyun uvc_ctrl_restore_values(stream->dev);
1944*4882a593Smuzhiyun
1945*4882a593Smuzhiyun return 0;
1946*4882a593Smuzhiyun }
1947*4882a593Smuzhiyun
1948*4882a593Smuzhiyun /* --------------------------------------------------------------------------
1949*4882a593Smuzhiyun * Suspend/resume
1950*4882a593Smuzhiyun */
1951*4882a593Smuzhiyun
1952*4882a593Smuzhiyun /*
1953*4882a593Smuzhiyun * Stop streaming without disabling the video queue.
1954*4882a593Smuzhiyun *
1955*4882a593Smuzhiyun * To let userspace applications resume without trouble, we must not touch the
1956*4882a593Smuzhiyun * video buffers in any way. We mark the device as frozen to make sure the URB
1957*4882a593Smuzhiyun * completion handler won't try to cancel the queue when we kill the URBs.
1958*4882a593Smuzhiyun */
uvc_video_suspend(struct uvc_streaming * stream)1959*4882a593Smuzhiyun int uvc_video_suspend(struct uvc_streaming *stream)
1960*4882a593Smuzhiyun {
1961*4882a593Smuzhiyun if (!uvc_queue_streaming(&stream->queue))
1962*4882a593Smuzhiyun return 0;
1963*4882a593Smuzhiyun
1964*4882a593Smuzhiyun stream->frozen = 1;
1965*4882a593Smuzhiyun uvc_video_stop_transfer(stream, 0);
1966*4882a593Smuzhiyun usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1967*4882a593Smuzhiyun return 0;
1968*4882a593Smuzhiyun }
1969*4882a593Smuzhiyun
1970*4882a593Smuzhiyun /*
1971*4882a593Smuzhiyun * Reconfigure the video interface and restart streaming if it was enabled
1972*4882a593Smuzhiyun * before suspend.
1973*4882a593Smuzhiyun *
1974*4882a593Smuzhiyun * If an error occurs, disable the video queue. This will wake all pending
1975*4882a593Smuzhiyun * buffers, making sure userspace applications are notified of the problem
1976*4882a593Smuzhiyun * instead of waiting forever.
1977*4882a593Smuzhiyun */
uvc_video_resume(struct uvc_streaming * stream,int reset)1978*4882a593Smuzhiyun int uvc_video_resume(struct uvc_streaming *stream, int reset)
1979*4882a593Smuzhiyun {
1980*4882a593Smuzhiyun int ret;
1981*4882a593Smuzhiyun
1982*4882a593Smuzhiyun /* If the bus has been reset on resume, set the alternate setting to 0.
1983*4882a593Smuzhiyun * This should be the default value, but some devices crash or otherwise
1984*4882a593Smuzhiyun * misbehave if they don't receive a SET_INTERFACE request before any
1985*4882a593Smuzhiyun * other video control request.
1986*4882a593Smuzhiyun */
1987*4882a593Smuzhiyun if (reset)
1988*4882a593Smuzhiyun usb_set_interface(stream->dev->udev, stream->intfnum, 0);
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun stream->frozen = 0;
1991*4882a593Smuzhiyun
1992*4882a593Smuzhiyun uvc_video_clock_reset(stream);
1993*4882a593Smuzhiyun
1994*4882a593Smuzhiyun if (!uvc_queue_streaming(&stream->queue))
1995*4882a593Smuzhiyun return 0;
1996*4882a593Smuzhiyun
1997*4882a593Smuzhiyun ret = uvc_commit_video(stream, &stream->ctrl);
1998*4882a593Smuzhiyun if (ret < 0)
1999*4882a593Smuzhiyun return ret;
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun return uvc_video_start_transfer(stream, GFP_NOIO);
2002*4882a593Smuzhiyun }
2003*4882a593Smuzhiyun
2004*4882a593Smuzhiyun /* ------------------------------------------------------------------------
2005*4882a593Smuzhiyun * Video device
2006*4882a593Smuzhiyun */
2007*4882a593Smuzhiyun
2008*4882a593Smuzhiyun /*
2009*4882a593Smuzhiyun * Initialize the UVC video device by switching to alternate setting 0 and
2010*4882a593Smuzhiyun * retrieve the default format.
2011*4882a593Smuzhiyun *
2012*4882a593Smuzhiyun * Some cameras (namely the Fuji Finepix) set the format and frame
2013*4882a593Smuzhiyun * indexes to zero. The UVC standard doesn't clearly make this a spec
2014*4882a593Smuzhiyun * violation, so try to silently fix the values if possible.
2015*4882a593Smuzhiyun *
2016*4882a593Smuzhiyun * This function is called before registering the device with V4L.
2017*4882a593Smuzhiyun */
uvc_video_init(struct uvc_streaming * stream)2018*4882a593Smuzhiyun int uvc_video_init(struct uvc_streaming *stream)
2019*4882a593Smuzhiyun {
2020*4882a593Smuzhiyun struct uvc_streaming_control *probe = &stream->ctrl;
2021*4882a593Smuzhiyun struct uvc_format *format = NULL;
2022*4882a593Smuzhiyun struct uvc_frame *frame = NULL;
2023*4882a593Smuzhiyun struct uvc_urb *uvc_urb;
2024*4882a593Smuzhiyun unsigned int i;
2025*4882a593Smuzhiyun int ret;
2026*4882a593Smuzhiyun
2027*4882a593Smuzhiyun if (stream->nformats == 0) {
2028*4882a593Smuzhiyun uvc_printk(KERN_INFO, "No supported video formats found.\n");
2029*4882a593Smuzhiyun return -EINVAL;
2030*4882a593Smuzhiyun }
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun atomic_set(&stream->active, 0);
2033*4882a593Smuzhiyun
2034*4882a593Smuzhiyun /* Alternate setting 0 should be the default, yet the XBox Live Vision
2035*4882a593Smuzhiyun * Cam (and possibly other devices) crash or otherwise misbehave if
2036*4882a593Smuzhiyun * they don't receive a SET_INTERFACE request before any other video
2037*4882a593Smuzhiyun * control request.
2038*4882a593Smuzhiyun */
2039*4882a593Smuzhiyun usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun /* Set the streaming probe control with default streaming parameters
2042*4882a593Smuzhiyun * retrieved from the device. Webcams that don't support GET_DEF
2043*4882a593Smuzhiyun * requests on the probe control will just keep their current streaming
2044*4882a593Smuzhiyun * parameters.
2045*4882a593Smuzhiyun */
2046*4882a593Smuzhiyun if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
2047*4882a593Smuzhiyun uvc_set_video_ctrl(stream, probe, 1);
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun /* Initialize the streaming parameters with the probe control current
2050*4882a593Smuzhiyun * value. This makes sure SET_CUR requests on the streaming commit
2051*4882a593Smuzhiyun * control will always use values retrieved from a successful GET_CUR
2052*4882a593Smuzhiyun * request on the probe control, as required by the UVC specification.
2053*4882a593Smuzhiyun */
2054*4882a593Smuzhiyun ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
2055*4882a593Smuzhiyun if (ret < 0)
2056*4882a593Smuzhiyun return ret;
2057*4882a593Smuzhiyun
2058*4882a593Smuzhiyun /* Check if the default format descriptor exists. Use the first
2059*4882a593Smuzhiyun * available format otherwise.
2060*4882a593Smuzhiyun */
2061*4882a593Smuzhiyun for (i = stream->nformats; i > 0; --i) {
2062*4882a593Smuzhiyun format = &stream->format[i-1];
2063*4882a593Smuzhiyun if (format->index == probe->bFormatIndex)
2064*4882a593Smuzhiyun break;
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun if (format->nframes == 0) {
2068*4882a593Smuzhiyun uvc_printk(KERN_INFO, "No frame descriptor found for the "
2069*4882a593Smuzhiyun "default format.\n");
2070*4882a593Smuzhiyun return -EINVAL;
2071*4882a593Smuzhiyun }
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun /* Zero bFrameIndex might be correct. Stream-based formats (including
2074*4882a593Smuzhiyun * MPEG-2 TS and DV) do not support frames but have a dummy frame
2075*4882a593Smuzhiyun * descriptor with bFrameIndex set to zero. If the default frame
2076*4882a593Smuzhiyun * descriptor is not found, use the first available frame.
2077*4882a593Smuzhiyun */
2078*4882a593Smuzhiyun for (i = format->nframes; i > 0; --i) {
2079*4882a593Smuzhiyun frame = &format->frame[i-1];
2080*4882a593Smuzhiyun if (frame->bFrameIndex == probe->bFrameIndex)
2081*4882a593Smuzhiyun break;
2082*4882a593Smuzhiyun }
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun probe->bFormatIndex = format->index;
2085*4882a593Smuzhiyun probe->bFrameIndex = frame->bFrameIndex;
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun stream->def_format = format;
2088*4882a593Smuzhiyun stream->cur_format = format;
2089*4882a593Smuzhiyun stream->cur_frame = frame;
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun /* Select the video decoding function */
2092*4882a593Smuzhiyun if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2093*4882a593Smuzhiyun if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
2094*4882a593Smuzhiyun stream->decode = uvc_video_decode_isight;
2095*4882a593Smuzhiyun else if (stream->intf->num_altsetting > 1)
2096*4882a593Smuzhiyun stream->decode = uvc_video_decode_isoc;
2097*4882a593Smuzhiyun else
2098*4882a593Smuzhiyun stream->decode = uvc_video_decode_bulk;
2099*4882a593Smuzhiyun } else {
2100*4882a593Smuzhiyun if (stream->intf->num_altsetting == 1)
2101*4882a593Smuzhiyun stream->decode = uvc_video_encode_bulk;
2102*4882a593Smuzhiyun else {
2103*4882a593Smuzhiyun uvc_printk(KERN_INFO, "Isochronous endpoints are not "
2104*4882a593Smuzhiyun "supported for video output devices.\n");
2105*4882a593Smuzhiyun return -EINVAL;
2106*4882a593Smuzhiyun }
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun
2109*4882a593Smuzhiyun /* Prepare asynchronous work items. */
2110*4882a593Smuzhiyun for_each_uvc_urb(uvc_urb, stream)
2111*4882a593Smuzhiyun INIT_WORK(&uvc_urb->work, uvc_video_copy_data_work);
2112*4882a593Smuzhiyun
2113*4882a593Smuzhiyun return 0;
2114*4882a593Smuzhiyun }
2115*4882a593Smuzhiyun
uvc_video_start_streaming(struct uvc_streaming * stream)2116*4882a593Smuzhiyun int uvc_video_start_streaming(struct uvc_streaming *stream)
2117*4882a593Smuzhiyun {
2118*4882a593Smuzhiyun int ret;
2119*4882a593Smuzhiyun
2120*4882a593Smuzhiyun ret = uvc_video_clock_init(stream);
2121*4882a593Smuzhiyun if (ret < 0)
2122*4882a593Smuzhiyun return ret;
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun /* Commit the streaming parameters. */
2125*4882a593Smuzhiyun ret = uvc_commit_video(stream, &stream->ctrl);
2126*4882a593Smuzhiyun if (ret < 0)
2127*4882a593Smuzhiyun goto error_commit;
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun ret = uvc_video_start_transfer(stream, GFP_KERNEL);
2130*4882a593Smuzhiyun if (ret < 0)
2131*4882a593Smuzhiyun goto error_video;
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun return 0;
2134*4882a593Smuzhiyun
2135*4882a593Smuzhiyun error_video:
2136*4882a593Smuzhiyun usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2137*4882a593Smuzhiyun error_commit:
2138*4882a593Smuzhiyun uvc_video_clock_cleanup(stream);
2139*4882a593Smuzhiyun
2140*4882a593Smuzhiyun return ret;
2141*4882a593Smuzhiyun }
2142*4882a593Smuzhiyun
uvc_video_stop_streaming(struct uvc_streaming * stream)2143*4882a593Smuzhiyun void uvc_video_stop_streaming(struct uvc_streaming *stream)
2144*4882a593Smuzhiyun {
2145*4882a593Smuzhiyun uvc_video_stop_transfer(stream, 1);
2146*4882a593Smuzhiyun
2147*4882a593Smuzhiyun if (stream->intf->num_altsetting > 1) {
2148*4882a593Smuzhiyun usb_set_interface(stream->dev->udev, stream->intfnum, 0);
2149*4882a593Smuzhiyun } else {
2150*4882a593Smuzhiyun /* UVC doesn't specify how to inform a bulk-based device
2151*4882a593Smuzhiyun * when the video stream is stopped. Windows sends a
2152*4882a593Smuzhiyun * CLEAR_FEATURE(HALT) request to the video streaming
2153*4882a593Smuzhiyun * bulk endpoint, mimic the same behaviour.
2154*4882a593Smuzhiyun */
2155*4882a593Smuzhiyun unsigned int epnum = stream->header.bEndpointAddress
2156*4882a593Smuzhiyun & USB_ENDPOINT_NUMBER_MASK;
2157*4882a593Smuzhiyun unsigned int dir = stream->header.bEndpointAddress
2158*4882a593Smuzhiyun & USB_ENDPOINT_DIR_MASK;
2159*4882a593Smuzhiyun unsigned int pipe;
2160*4882a593Smuzhiyun
2161*4882a593Smuzhiyun pipe = usb_sndbulkpipe(stream->dev->udev, epnum) | dir;
2162*4882a593Smuzhiyun usb_clear_halt(stream->dev->udev, pipe);
2163*4882a593Smuzhiyun }
2164*4882a593Smuzhiyun
2165*4882a593Smuzhiyun uvc_video_clock_cleanup(stream);
2166*4882a593Smuzhiyun }
2167