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