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