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