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 | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 354 | int uvc_commit_video(struct uvc_streaming *stream, |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 355 | struct uvc_streaming_control *probe) |
| 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 | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 360 | /* ------------------------------------------------------------------------ |
| 361 | * Video codecs |
| 362 | */ |
| 363 | |
| 364 | /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */ |
| 365 | #define UVC_STREAM_EOH (1 << 7) |
| 366 | #define UVC_STREAM_ERR (1 << 6) |
| 367 | #define UVC_STREAM_STI (1 << 5) |
| 368 | #define UVC_STREAM_RES (1 << 4) |
| 369 | #define UVC_STREAM_SCR (1 << 3) |
| 370 | #define UVC_STREAM_PTS (1 << 2) |
| 371 | #define UVC_STREAM_EOF (1 << 1) |
| 372 | #define UVC_STREAM_FID (1 << 0) |
| 373 | |
| 374 | /* Video payload decoding is handled by uvc_video_decode_start(), |
| 375 | * uvc_video_decode_data() and uvc_video_decode_end(). |
| 376 | * |
| 377 | * uvc_video_decode_start is called with URB data at the start of a bulk or |
| 378 | * isochronous payload. It processes header data and returns the header size |
| 379 | * in bytes if successful. If an error occurs, it returns a negative error |
| 380 | * code. The following error codes have special meanings. |
| 381 | * |
| 382 | * - EAGAIN informs the caller that the current video buffer should be marked |
| 383 | * as done, and that the function should be called again with the same data |
| 384 | * and a new video buffer. This is used when end of frame conditions can be |
| 385 | * reliably detected at the beginning of the next frame only. |
| 386 | * |
| 387 | * If an error other than -EAGAIN is returned, the caller will drop the current |
| 388 | * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be |
| 389 | * made until the next payload. -ENODATA can be used to drop the current |
| 390 | * payload if no other error code is appropriate. |
| 391 | * |
| 392 | * uvc_video_decode_data is called for every URB with URB data. It copies the |
| 393 | * data to the video buffer. |
| 394 | * |
| 395 | * uvc_video_decode_end is called with header data at the end of a bulk or |
| 396 | * isochronous payload. It performs any additional header data processing and |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 397 | * 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] | 398 | * already been processed by uvc_video_decode_start, this functions isn't |
| 399 | * required to perform sanity checks a second time. |
| 400 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 401 | * For isochronous transfers where a payload is always transferred in a single |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 402 | * URB, the three functions will be called in a row. |
| 403 | * |
| 404 | * To let the decoder process header data and update its internal state even |
| 405 | * when no video buffer is available, uvc_video_decode_start must be prepared |
| 406 | * to be called with a NULL buf parameter. uvc_video_decode_data and |
| 407 | * uvc_video_decode_end will never be called with a NULL buffer. |
| 408 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 409 | static int uvc_video_decode_start(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 410 | struct uvc_buffer *buf, const __u8 *data, int len) |
| 411 | { |
| 412 | __u8 fid; |
| 413 | |
| 414 | /* Sanity checks: |
| 415 | * - packet must be at least 2 bytes long |
| 416 | * - bHeaderLength value must be at least 2 bytes (see above) |
| 417 | * - bHeaderLength value can't be larger than the packet size. |
| 418 | */ |
| 419 | if (len < 2 || data[0] < 2 || data[0] > len) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | /* Skip payloads marked with the error bit ("error frames"). */ |
| 423 | if (data[1] & UVC_STREAM_ERR) { |
| 424 | uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit " |
| 425 | "set).\n"); |
| 426 | return -ENODATA; |
| 427 | } |
| 428 | |
| 429 | fid = data[1] & UVC_STREAM_FID; |
| 430 | |
Laurent Pinchart | 650b95f | 2010-10-02 11:06:05 -0300 | [diff] [blame] | 431 | /* Increase the sequence number regardless of any buffer states, so |
| 432 | * that discontinuous sequence numbers always indicate lost frames. |
| 433 | */ |
| 434 | if (stream->last_fid != fid) |
| 435 | stream->sequence++; |
| 436 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 437 | /* Store the payload FID bit and return immediately when the buffer is |
| 438 | * NULL. |
| 439 | */ |
| 440 | if (buf == NULL) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 441 | stream->last_fid = fid; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 442 | return -ENODATA; |
| 443 | } |
| 444 | |
| 445 | /* Synchronize to the input stream by waiting for the FID bit to be |
| 446 | * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE. |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 447 | * stream->last_fid is initialized to -1, so the first isochronous |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 448 | * frame will always be in sync. |
| 449 | * |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 450 | * 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] | 451 | * when the EOF bit is set to force synchronisation on the next packet. |
| 452 | */ |
| 453 | if (buf->state != UVC_BUF_STATE_ACTIVE) { |
Laurent Pinchart | 310fe52 | 2009-12-09 22:57:48 -0300 | [diff] [blame] | 454 | struct timespec ts; |
| 455 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 456 | if (fid == stream->last_fid) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 457 | uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of " |
| 458 | "sync).\n"); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 459 | if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) && |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 460 | (data[1] & UVC_STREAM_EOF)) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 461 | stream->last_fid ^= UVC_STREAM_FID; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 462 | return -ENODATA; |
| 463 | } |
| 464 | |
Laurent Pinchart | 310fe52 | 2009-12-09 22:57:48 -0300 | [diff] [blame] | 465 | if (uvc_clock_param == CLOCK_MONOTONIC) |
| 466 | ktime_get_ts(&ts); |
| 467 | else |
| 468 | ktime_get_real_ts(&ts); |
| 469 | |
Laurent Pinchart | 6998b6f | 2011-10-24 11:53:59 -0300 | [diff] [blame] | 470 | buf->buf.v4l2_buf.sequence = stream->sequence; |
| 471 | buf->buf.v4l2_buf.timestamp.tv_sec = ts.tv_sec; |
| 472 | buf->buf.v4l2_buf.timestamp.tv_usec = |
| 473 | ts.tv_nsec / NSEC_PER_USEC; |
Laurent Pinchart | 310fe52 | 2009-12-09 22:57:48 -0300 | [diff] [blame] | 474 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 475 | /* TODO: Handle PTS and SCR. */ |
| 476 | buf->state = UVC_BUF_STATE_ACTIVE; |
| 477 | } |
| 478 | |
| 479 | /* Mark the buffer as done if we're at the beginning of a new frame. |
| 480 | * End of frame detection is better implemented by checking the EOF |
| 481 | * bit (FID bit toggling is delayed by one frame compared to the EOF |
| 482 | * bit), but some devices don't set the bit at end of frame (and the |
| 483 | * last payload can be lost anyway). We thus must check if the FID has |
| 484 | * been toggled. |
| 485 | * |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 486 | * stream->last_fid is initialized to -1, so the first isochronous |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 487 | * frame will never trigger an end of frame detection. |
| 488 | * |
| 489 | * Empty buffers (bytesused == 0) don't trigger end of frame detection |
| 490 | * 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] | 491 | * avoids detecting end of frame conditions at FID toggling if the |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 492 | * previous payload had the EOF bit set. |
| 493 | */ |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 494 | if (fid != stream->last_fid && buf->bytesused != 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 495 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit " |
| 496 | "toggled).\n"); |
Laurent Pinchart | d7c0d43 | 2009-12-16 21:20:45 -0300 | [diff] [blame] | 497 | buf->state = UVC_BUF_STATE_READY; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 498 | return -EAGAIN; |
| 499 | } |
| 500 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 501 | stream->last_fid = fid; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 502 | |
| 503 | return data[0]; |
| 504 | } |
| 505 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 506 | static void uvc_video_decode_data(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 507 | struct uvc_buffer *buf, const __u8 *data, int len) |
| 508 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 509 | unsigned int maxlen, nbytes; |
| 510 | void *mem; |
| 511 | |
| 512 | if (len <= 0) |
| 513 | return; |
| 514 | |
| 515 | /* Copy the video data to the buffer. */ |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 516 | maxlen = buf->length - buf->bytesused; |
| 517 | mem = buf->mem + buf->bytesused; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 518 | nbytes = min((unsigned int)len, maxlen); |
| 519 | memcpy(mem, data, nbytes); |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 520 | buf->bytesused += nbytes; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 521 | |
| 522 | /* Complete the current frame if the buffer size was exceeded. */ |
| 523 | if (len > maxlen) { |
| 524 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n"); |
Laurent Pinchart | d7c0d43 | 2009-12-16 21:20:45 -0300 | [diff] [blame] | 525 | buf->state = UVC_BUF_STATE_READY; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 529 | static void uvc_video_decode_end(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 530 | struct uvc_buffer *buf, const __u8 *data, int len) |
| 531 | { |
| 532 | /* Mark the buffer as done if the EOF marker is set. */ |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 533 | if (data[1] & UVC_STREAM_EOF && buf->bytesused != 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 534 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n"); |
| 535 | if (data[0] == len) |
| 536 | uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n"); |
Laurent Pinchart | d7c0d43 | 2009-12-16 21:20:45 -0300 | [diff] [blame] | 537 | buf->state = UVC_BUF_STATE_READY; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 538 | if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) |
| 539 | stream->last_fid ^= UVC_STREAM_FID; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 543 | /* Video payload encoding is handled by uvc_video_encode_header() and |
| 544 | * uvc_video_encode_data(). Only bulk transfers are currently supported. |
| 545 | * |
| 546 | * uvc_video_encode_header is called at the start of a payload. It adds header |
| 547 | * data to the transfer buffer and returns the header size. As the only known |
| 548 | * UVC output device transfers a whole frame in a single payload, the EOF bit |
| 549 | * is always set in the header. |
| 550 | * |
| 551 | * uvc_video_encode_data is called for every URB and copies the data from the |
| 552 | * video buffer to the transfer buffer. |
| 553 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 554 | static int uvc_video_encode_header(struct uvc_streaming *stream, |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 555 | struct uvc_buffer *buf, __u8 *data, int len) |
| 556 | { |
| 557 | data[0] = 2; /* Header length */ |
| 558 | data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 559 | | (stream->last_fid & UVC_STREAM_FID); |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 560 | return 2; |
| 561 | } |
| 562 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 563 | static int uvc_video_encode_data(struct uvc_streaming *stream, |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 564 | struct uvc_buffer *buf, __u8 *data, int len) |
| 565 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 566 | struct uvc_video_queue *queue = &stream->queue; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 567 | unsigned int nbytes; |
| 568 | void *mem; |
| 569 | |
| 570 | /* Copy video data to the URB buffer. */ |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 571 | mem = buf->mem + queue->buf_used; |
| 572 | nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 573 | nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size, |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 574 | nbytes); |
| 575 | memcpy(data, mem, nbytes); |
| 576 | |
| 577 | queue->buf_used += nbytes; |
| 578 | |
| 579 | return nbytes; |
| 580 | } |
| 581 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 582 | /* ------------------------------------------------------------------------ |
| 583 | * URB handling |
| 584 | */ |
| 585 | |
| 586 | /* |
| 587 | * Completion handler for video URBs. |
| 588 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 589 | static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream, |
| 590 | struct uvc_buffer *buf) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 591 | { |
| 592 | u8 *mem; |
| 593 | int ret, i; |
| 594 | |
| 595 | for (i = 0; i < urb->number_of_packets; ++i) { |
| 596 | if (urb->iso_frame_desc[i].status < 0) { |
| 597 | uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame " |
| 598 | "lost (%d).\n", urb->iso_frame_desc[i].status); |
Laurent Pinchart | 9bde9f2 | 2010-06-17 06:52:37 -0300 | [diff] [blame] | 599 | /* Mark the buffer as faulty. */ |
| 600 | if (buf != NULL) |
| 601 | buf->error = 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 602 | continue; |
| 603 | } |
| 604 | |
| 605 | /* Decode the payload header. */ |
| 606 | mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
| 607 | do { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 608 | ret = uvc_video_decode_start(stream, buf, mem, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 609 | urb->iso_frame_desc[i].actual_length); |
| 610 | if (ret == -EAGAIN) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 611 | buf = uvc_queue_next_buffer(&stream->queue, |
| 612 | buf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 613 | } while (ret == -EAGAIN); |
| 614 | |
| 615 | if (ret < 0) |
| 616 | continue; |
| 617 | |
| 618 | /* Decode the payload data. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 619 | uvc_video_decode_data(stream, buf, mem + ret, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 620 | urb->iso_frame_desc[i].actual_length - ret); |
| 621 | |
| 622 | /* Process the header again. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 623 | uvc_video_decode_end(stream, buf, mem, |
Laurent Pinchart | 08ebd00 | 2008-08-29 17:37:44 -0300 | [diff] [blame] | 624 | urb->iso_frame_desc[i].actual_length); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 625 | |
Laurent Pinchart | 9bde9f2 | 2010-06-17 06:52:37 -0300 | [diff] [blame] | 626 | if (buf->state == UVC_BUF_STATE_READY) { |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 627 | if (buf->length != buf->bytesused && |
Laurent Pinchart | 9bde9f2 | 2010-06-17 06:52:37 -0300 | [diff] [blame] | 628 | !(stream->cur_format->flags & |
| 629 | UVC_FMT_FLAG_COMPRESSED)) |
| 630 | buf->error = 1; |
| 631 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 632 | buf = uvc_queue_next_buffer(&stream->queue, buf); |
Laurent Pinchart | 9bde9f2 | 2010-06-17 06:52:37 -0300 | [diff] [blame] | 633 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 637 | static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream, |
| 638 | struct uvc_buffer *buf) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 639 | { |
| 640 | u8 *mem; |
| 641 | int len, ret; |
| 642 | |
Laurent Pinchart | c90e777 | 2009-02-14 19:39:08 -0300 | [diff] [blame] | 643 | if (urb->actual_length == 0) |
| 644 | return; |
| 645 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 646 | mem = urb->transfer_buffer; |
| 647 | len = urb->actual_length; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 648 | stream->bulk.payload_size += len; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 649 | |
| 650 | /* If the URB is the first of its payload, decode and save the |
| 651 | * header. |
| 652 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 653 | if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 654 | do { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 655 | ret = uvc_video_decode_start(stream, buf, mem, len); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 656 | if (ret == -EAGAIN) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 657 | buf = uvc_queue_next_buffer(&stream->queue, |
| 658 | buf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 659 | } while (ret == -EAGAIN); |
| 660 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 661 | /* If an error occurred skip the rest of the payload. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 662 | if (ret < 0 || buf == NULL) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 663 | stream->bulk.skip_payload = 1; |
Laurent Pinchart | f8dd4af | 2008-12-16 10:41:57 -0300 | [diff] [blame] | 664 | } else { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 665 | memcpy(stream->bulk.header, mem, ret); |
| 666 | stream->bulk.header_size = ret; |
Laurent Pinchart | f8dd4af | 2008-12-16 10:41:57 -0300 | [diff] [blame] | 667 | |
| 668 | mem += ret; |
| 669 | len -= ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 670 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* The buffer queue might have been cancelled while a bulk transfer |
| 674 | * was in progress, so we can reach here with buf equal to NULL. Make |
| 675 | * sure buf is never dereferenced if NULL. |
| 676 | */ |
| 677 | |
| 678 | /* Process video data. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 679 | if (!stream->bulk.skip_payload && buf != NULL) |
| 680 | uvc_video_decode_data(stream, buf, mem, len); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 681 | |
| 682 | /* Detect the payload end by a URB smaller than the maximum size (or |
| 683 | * a payload size equal to the maximum) and process the header again. |
| 684 | */ |
| 685 | if (urb->actual_length < urb->transfer_buffer_length || |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 686 | stream->bulk.payload_size >= stream->bulk.max_payload_size) { |
| 687 | if (!stream->bulk.skip_payload && buf != NULL) { |
| 688 | uvc_video_decode_end(stream, buf, stream->bulk.header, |
| 689 | stream->bulk.payload_size); |
Laurent Pinchart | d7c0d43 | 2009-12-16 21:20:45 -0300 | [diff] [blame] | 690 | if (buf->state == UVC_BUF_STATE_READY) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 691 | buf = uvc_queue_next_buffer(&stream->queue, |
| 692 | buf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 693 | } |
| 694 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 695 | stream->bulk.header_size = 0; |
| 696 | stream->bulk.skip_payload = 0; |
| 697 | stream->bulk.payload_size = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 701 | static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream, |
| 702 | struct uvc_buffer *buf) |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 703 | { |
| 704 | u8 *mem = urb->transfer_buffer; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 705 | int len = stream->urb_size, ret; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 706 | |
| 707 | if (buf == NULL) { |
| 708 | urb->transfer_buffer_length = 0; |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | /* If the URB is the first of its payload, add the header. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 713 | if (stream->bulk.header_size == 0) { |
| 714 | ret = uvc_video_encode_header(stream, buf, mem, len); |
| 715 | stream->bulk.header_size = ret; |
| 716 | stream->bulk.payload_size += ret; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 717 | mem += ret; |
| 718 | len -= ret; |
| 719 | } |
| 720 | |
| 721 | /* Process video data. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 722 | ret = uvc_video_encode_data(stream, buf, mem, len); |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 723 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 724 | stream->bulk.payload_size += ret; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 725 | len -= ret; |
| 726 | |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 727 | if (buf->bytesused == stream->queue.buf_used || |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 728 | stream->bulk.payload_size == stream->bulk.max_payload_size) { |
Laurent Pinchart | 3d95e93 | 2011-10-24 11:49:19 -0300 | [diff] [blame] | 729 | if (buf->bytesused == stream->queue.buf_used) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 730 | stream->queue.buf_used = 0; |
Laurent Pinchart | d7c0d43 | 2009-12-16 21:20:45 -0300 | [diff] [blame] | 731 | buf->state = UVC_BUF_STATE_READY; |
Laurent Pinchart | 6998b6f | 2011-10-24 11:53:59 -0300 | [diff] [blame] | 732 | buf->buf.v4l2_buf.sequence = ++stream->sequence; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 733 | uvc_queue_next_buffer(&stream->queue, buf); |
| 734 | stream->last_fid ^= UVC_STREAM_FID; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 735 | } |
| 736 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 737 | stream->bulk.header_size = 0; |
| 738 | stream->bulk.payload_size = 0; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 739 | } |
| 740 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 741 | urb->transfer_buffer_length = stream->urb_size - len; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 742 | } |
| 743 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 744 | static void uvc_video_complete(struct urb *urb) |
| 745 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 746 | struct uvc_streaming *stream = urb->context; |
| 747 | struct uvc_video_queue *queue = &stream->queue; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 748 | struct uvc_buffer *buf = NULL; |
| 749 | unsigned long flags; |
| 750 | int ret; |
| 751 | |
| 752 | switch (urb->status) { |
| 753 | case 0: |
| 754 | break; |
| 755 | |
| 756 | default: |
| 757 | uvc_printk(KERN_WARNING, "Non-zero status (%d) in video " |
| 758 | "completion handler.\n", urb->status); |
| 759 | |
| 760 | case -ENOENT: /* usb_kill_urb() called. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 761 | if (stream->frozen) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 762 | return; |
| 763 | |
| 764 | case -ECONNRESET: /* usb_unlink_urb() called. */ |
| 765 | case -ESHUTDOWN: /* The endpoint is being disabled. */ |
| 766 | uvc_queue_cancel(queue, urb->status == -ESHUTDOWN); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | spin_lock_irqsave(&queue->irqlock, flags); |
| 771 | if (!list_empty(&queue->irqqueue)) |
| 772 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 773 | queue); |
| 774 | spin_unlock_irqrestore(&queue->irqlock, flags); |
| 775 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 776 | stream->decode(urb, stream, buf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 777 | |
| 778 | if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 779 | uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n", |
| 780 | ret); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /* |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 785 | * Free transfer buffers. |
| 786 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 787 | static void uvc_free_urb_buffers(struct uvc_streaming *stream) |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 788 | { |
| 789 | unsigned int i; |
| 790 | |
| 791 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 792 | if (stream->urb_buffer[i]) { |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 793 | #ifndef CONFIG_DMA_NONCOHERENT |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 794 | usb_free_coherent(stream->dev->udev, stream->urb_size, |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 795 | stream->urb_buffer[i], stream->urb_dma[i]); |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 796 | #else |
| 797 | kfree(stream->urb_buffer[i]); |
| 798 | #endif |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 799 | stream->urb_buffer[i] = NULL; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 803 | stream->urb_size = 0; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Allocate transfer buffers. This function can be called with buffers |
| 808 | * already allocated when resuming from suspend, in which case it will |
| 809 | * return without touching the buffers. |
| 810 | * |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 811 | * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the |
| 812 | * system is too low on memory try successively smaller numbers of packets |
| 813 | * until allocation succeeds. |
| 814 | * |
| 815 | * 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] | 816 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 817 | static int uvc_alloc_urb_buffers(struct uvc_streaming *stream, |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 818 | unsigned int size, unsigned int psize, gfp_t gfp_flags) |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 819 | { |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 820 | unsigned int npackets; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 821 | unsigned int i; |
| 822 | |
| 823 | /* Buffers are already allocated, bail out. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 824 | if (stream->urb_size) |
| 825 | return stream->urb_size / psize; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 826 | |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 827 | /* Compute the number of packets. Bulk endpoints might transfer UVC |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 828 | * payloads across multiple URBs. |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 829 | */ |
| 830 | npackets = DIV_ROUND_UP(size, psize); |
| 831 | if (npackets > UVC_MAX_PACKETS) |
| 832 | npackets = UVC_MAX_PACKETS; |
| 833 | |
| 834 | /* Retry allocations until one succeed. */ |
| 835 | for (; npackets > 1; npackets /= 2) { |
| 836 | for (i = 0; i < UVC_URBS; ++i) { |
Ming Lei | fd1b6bb | 2009-09-27 05:30:34 -0300 | [diff] [blame] | 837 | stream->urb_size = psize * npackets; |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 838 | #ifndef CONFIG_DMA_NONCOHERENT |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 839 | stream->urb_buffer[i] = usb_alloc_coherent( |
Ming Lei | fd1b6bb | 2009-09-27 05:30:34 -0300 | [diff] [blame] | 840 | stream->dev->udev, stream->urb_size, |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 841 | gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]); |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 842 | #else |
| 843 | stream->urb_buffer[i] = |
| 844 | kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN); |
| 845 | #endif |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 846 | if (!stream->urb_buffer[i]) { |
| 847 | uvc_free_urb_buffers(stream); |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if (i == UVC_URBS) { |
Laurent Pinchart | 663a419 | 2009-08-06 11:41:17 -0300 | [diff] [blame] | 853 | uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers " |
| 854 | "of %ux%u bytes each.\n", UVC_URBS, npackets, |
| 855 | psize); |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 856 | return npackets; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Laurent Pinchart | 663a419 | 2009-08-06 11:41:17 -0300 | [diff] [blame] | 860 | uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes " |
| 861 | "per packet).\n", psize); |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | /* |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 866 | * Uninitialize isochronous/bulk URBs and free transfer buffers. |
| 867 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 868 | static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 869 | { |
| 870 | struct urb *urb; |
| 871 | unsigned int i; |
| 872 | |
| 873 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 874 | urb = stream->urb[i]; |
| 875 | if (urb == NULL) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 876 | continue; |
| 877 | |
| 878 | usb_kill_urb(urb); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 879 | usb_free_urb(urb); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 880 | stream->urb[i] = NULL; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 881 | } |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 882 | |
| 883 | if (free_buffers) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 884 | uvc_free_urb_buffers(stream); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Initialize isochronous URBs and allocate transfer buffers. The packet size |
| 889 | * is given by the endpoint. |
| 890 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 891 | static int uvc_init_video_isoc(struct uvc_streaming *stream, |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 892 | struct usb_host_endpoint *ep, gfp_t gfp_flags) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 893 | { |
| 894 | struct urb *urb; |
| 895 | unsigned int npackets, i, j; |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 896 | u16 psize; |
| 897 | u32 size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 898 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 899 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 900 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 901 | size = stream->ctrl.dwMaxVideoFrameSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 902 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 903 | npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags); |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 904 | if (npackets == 0) |
| 905 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 906 | |
| 907 | size = npackets * psize; |
| 908 | |
| 909 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 910 | urb = usb_alloc_urb(npackets, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 911 | if (urb == NULL) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 912 | uvc_uninit_video(stream, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 913 | return -ENOMEM; |
| 914 | } |
| 915 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 916 | urb->dev = stream->dev->udev; |
| 917 | urb->context = stream; |
| 918 | urb->pipe = usb_rcvisocpipe(stream->dev->udev, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 919 | ep->desc.bEndpointAddress); |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 920 | #ifndef CONFIG_DMA_NONCOHERENT |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 921 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 922 | urb->transfer_dma = stream->urb_dma[i]; |
| 923 | #else |
| 924 | urb->transfer_flags = URB_ISO_ASAP; |
| 925 | #endif |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 926 | urb->interval = ep->desc.bInterval; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 927 | urb->transfer_buffer = stream->urb_buffer[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 928 | urb->complete = uvc_video_complete; |
| 929 | urb->number_of_packets = npackets; |
| 930 | urb->transfer_buffer_length = size; |
| 931 | |
| 932 | for (j = 0; j < npackets; ++j) { |
| 933 | urb->iso_frame_desc[j].offset = j * psize; |
| 934 | urb->iso_frame_desc[j].length = psize; |
| 935 | } |
| 936 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 937 | stream->urb[i] = urb; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Initialize bulk URBs and allocate transfer buffers. The packet size is |
| 945 | * given by the endpoint. |
| 946 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 947 | static int uvc_init_video_bulk(struct uvc_streaming *stream, |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 948 | struct usb_host_endpoint *ep, gfp_t gfp_flags) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 949 | { |
| 950 | struct urb *urb; |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 951 | unsigned int npackets, pipe, i; |
| 952 | u16 psize; |
| 953 | u32 size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 954 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 955 | psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 956 | size = stream->ctrl.dwMaxPayloadTransferSize; |
| 957 | stream->bulk.max_payload_size = size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 958 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 959 | npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags); |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 960 | if (npackets == 0) |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 961 | return -ENOMEM; |
| 962 | |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 963 | size = npackets * psize; |
| 964 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 965 | if (usb_endpoint_dir_in(&ep->desc)) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 966 | pipe = usb_rcvbulkpipe(stream->dev->udev, |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 967 | ep->desc.bEndpointAddress); |
| 968 | else |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 969 | pipe = usb_sndbulkpipe(stream->dev->udev, |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 970 | ep->desc.bEndpointAddress); |
| 971 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 972 | if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 973 | size = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 974 | |
| 975 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 976 | urb = usb_alloc_urb(0, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 977 | if (urb == NULL) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 978 | uvc_uninit_video(stream, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 979 | return -ENOMEM; |
| 980 | } |
| 981 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 982 | usb_fill_bulk_urb(urb, stream->dev->udev, pipe, |
| 983 | stream->urb_buffer[i], size, uvc_video_complete, |
| 984 | stream); |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 985 | #ifndef CONFIG_DMA_NONCOHERENT |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 986 | urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 987 | urb->transfer_dma = stream->urb_dma[i]; |
Al Cooper | 3e0ac67 | 2011-08-18 10:28:29 -0300 | [diff] [blame] | 988 | #endif |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 989 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 990 | stream->urb[i] = urb; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Initialize isochronous/bulk URBs and allocate transfer buffers. |
| 998 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 999 | 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] | 1000 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1001 | struct usb_interface *intf = stream->intf; |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1002 | struct usb_host_endpoint *ep; |
| 1003 | unsigned int i; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1004 | int ret; |
| 1005 | |
Laurent Pinchart | 650b95f | 2010-10-02 11:06:05 -0300 | [diff] [blame] | 1006 | stream->sequence = -1; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1007 | stream->last_fid = -1; |
| 1008 | stream->bulk.header_size = 0; |
| 1009 | stream->bulk.skip_payload = 0; |
| 1010 | stream->bulk.payload_size = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1011 | |
| 1012 | if (intf->num_altsetting > 1) { |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1013 | struct usb_host_endpoint *best_ep = NULL; |
| 1014 | unsigned int best_psize = 3 * 1024; |
| 1015 | unsigned int bandwidth; |
| 1016 | unsigned int uninitialized_var(altsetting); |
| 1017 | int intfnum = stream->intfnum; |
| 1018 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1019 | /* Isochronous endpoint, select the alternate setting. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1020 | bandwidth = stream->ctrl.dwMaxPayloadTransferSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1021 | |
| 1022 | if (bandwidth == 0) { |
Laurent Pinchart | 663a419 | 2009-08-06 11:41:17 -0300 | [diff] [blame] | 1023 | uvc_trace(UVC_TRACE_VIDEO, "Device requested null " |
| 1024 | "bandwidth, defaulting to lowest.\n"); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1025 | bandwidth = 1; |
Laurent Pinchart | 663a419 | 2009-08-06 11:41:17 -0300 | [diff] [blame] | 1026 | } else { |
| 1027 | uvc_trace(UVC_TRACE_VIDEO, "Device requested %u " |
| 1028 | "B/frame bandwidth.\n", bandwidth); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | for (i = 0; i < intf->num_altsetting; ++i) { |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1032 | struct usb_host_interface *alts; |
| 1033 | unsigned int psize; |
| 1034 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1035 | alts = &intf->altsetting[i]; |
| 1036 | ep = uvc_find_endpoint(alts, |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1037 | stream->header.bEndpointAddress); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1038 | if (ep == NULL) |
| 1039 | continue; |
| 1040 | |
| 1041 | /* Check if the bandwidth is high enough. */ |
| 1042 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 1043 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1044 | if (psize >= bandwidth && psize <= best_psize) { |
| 1045 | altsetting = i; |
| 1046 | best_psize = psize; |
| 1047 | best_ep = ep; |
| 1048 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1049 | } |
| 1050 | |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1051 | if (best_ep == NULL) { |
Laurent Pinchart | 663a419 | 2009-08-06 11:41:17 -0300 | [diff] [blame] | 1052 | uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting " |
| 1053 | "for requested bandwidth.\n"); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1054 | return -EIO; |
Laurent Pinchart | 663a419 | 2009-08-06 11:41:17 -0300 | [diff] [blame] | 1055 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1056 | |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1057 | uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u " |
| 1058 | "(%u B/frame bandwidth).\n", altsetting, best_psize); |
| 1059 | |
| 1060 | ret = usb_set_interface(stream->dev->udev, intfnum, altsetting); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1061 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1062 | return ret; |
| 1063 | |
Laurent Pinchart | 2c4d9de | 2009-12-10 21:19:31 -0300 | [diff] [blame] | 1064 | ret = uvc_init_video_isoc(stream, best_ep, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1065 | } else { |
| 1066 | /* Bulk endpoint, proceed to URB initialization. */ |
| 1067 | ep = uvc_find_endpoint(&intf->altsetting[0], |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1068 | stream->header.bEndpointAddress); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1069 | if (ep == NULL) |
| 1070 | return -EIO; |
| 1071 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1072 | ret = uvc_init_video_bulk(stream, ep, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | if (ret < 0) |
| 1076 | return ret; |
| 1077 | |
| 1078 | /* Submit the URBs. */ |
| 1079 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1080 | ret = usb_submit_urb(stream->urb[i], gfp_flags); |
| 1081 | if (ret < 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1082 | uvc_printk(KERN_ERR, "Failed to submit URB %u " |
| 1083 | "(%d).\n", i, ret); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1084 | uvc_uninit_video(stream, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1085 | return ret; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | /* -------------------------------------------------------------------------- |
| 1093 | * Suspend/resume |
| 1094 | */ |
| 1095 | |
| 1096 | /* |
| 1097 | * Stop streaming without disabling the video queue. |
| 1098 | * |
| 1099 | * To let userspace applications resume without trouble, we must not touch the |
| 1100 | * video buffers in any way. We mark the device as frozen to make sure the URB |
| 1101 | * completion handler won't try to cancel the queue when we kill the URBs. |
| 1102 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1103 | int uvc_video_suspend(struct uvc_streaming *stream) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1104 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1105 | if (!uvc_queue_streaming(&stream->queue)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1106 | return 0; |
| 1107 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1108 | stream->frozen = 1; |
| 1109 | uvc_uninit_video(stream, 0); |
| 1110 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 1115 | * Reconfigure the video interface and restart streaming if it was enabled |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1116 | * before suspend. |
| 1117 | * |
| 1118 | * If an error occurs, disable the video queue. This will wake all pending |
| 1119 | * buffers, making sure userspace applications are notified of the problem |
| 1120 | * instead of waiting forever. |
| 1121 | */ |
Ming Lei | d59a7b1 | 2011-07-16 00:51:00 -0300 | [diff] [blame] | 1122 | int uvc_video_resume(struct uvc_streaming *stream, int reset) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1123 | { |
| 1124 | int ret; |
| 1125 | |
Ming Lei | d59a7b1 | 2011-07-16 00:51:00 -0300 | [diff] [blame] | 1126 | /* If the bus has been reset on resume, set the alternate setting to 0. |
| 1127 | * This should be the default value, but some devices crash or otherwise |
| 1128 | * misbehave if they don't receive a SET_INTERFACE request before any |
| 1129 | * other video control request. |
| 1130 | */ |
| 1131 | if (reset) |
| 1132 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); |
| 1133 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1134 | stream->frozen = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1135 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1136 | ret = uvc_commit_video(stream, &stream->ctrl); |
| 1137 | if (ret < 0) { |
| 1138 | uvc_queue_enable(&stream->queue, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1139 | return ret; |
| 1140 | } |
| 1141 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1142 | if (!uvc_queue_streaming(&stream->queue)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1143 | return 0; |
| 1144 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1145 | ret = uvc_init_video(stream, GFP_NOIO); |
| 1146 | if (ret < 0) |
| 1147 | uvc_queue_enable(&stream->queue, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1148 | |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
| 1152 | /* ------------------------------------------------------------------------ |
| 1153 | * Video device |
| 1154 | */ |
| 1155 | |
| 1156 | /* |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 1157 | * Initialize the UVC video device by switching to alternate setting 0 and |
| 1158 | * retrieve the default format. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1159 | * |
| 1160 | * Some cameras (namely the Fuji Finepix) set the format and frame |
| 1161 | * indexes to zero. The UVC standard doesn't clearly make this a spec |
| 1162 | * violation, so try to silently fix the values if possible. |
| 1163 | * |
| 1164 | * This function is called before registering the device with V4L. |
| 1165 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1166 | int uvc_video_init(struct uvc_streaming *stream) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1167 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1168 | struct uvc_streaming_control *probe = &stream->ctrl; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1169 | struct uvc_format *format = NULL; |
| 1170 | struct uvc_frame *frame = NULL; |
| 1171 | unsigned int i; |
| 1172 | int ret; |
| 1173 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1174 | if (stream->nformats == 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1175 | uvc_printk(KERN_INFO, "No supported video formats found.\n"); |
| 1176 | return -EINVAL; |
| 1177 | } |
| 1178 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1179 | atomic_set(&stream->active, 0); |
| 1180 | |
| 1181 | /* Initialize the video buffers queue. */ |
Laurent Pinchart | 9bde9f2 | 2010-06-17 06:52:37 -0300 | [diff] [blame] | 1182 | uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1183 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1184 | /* Alternate setting 0 should be the default, yet the XBox Live Vision |
| 1185 | * Cam (and possibly other devices) crash or otherwise misbehave if |
| 1186 | * they don't receive a SET_INTERFACE request before any other video |
| 1187 | * control request. |
| 1188 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1189 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1190 | |
Laurent Pinchart | 7236242 | 2009-02-14 19:26:56 -0300 | [diff] [blame] | 1191 | /* Set the streaming probe control with default streaming parameters |
| 1192 | * retrieved from the device. Webcams that don't suport GET_DEF |
| 1193 | * requests on the probe control will just keep their current streaming |
| 1194 | * parameters. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1195 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1196 | if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0) |
| 1197 | uvc_set_video_ctrl(stream, probe, 1); |
Laurent Pinchart | 7236242 | 2009-02-14 19:26:56 -0300 | [diff] [blame] | 1198 | |
| 1199 | /* Initialize the streaming parameters with the probe control current |
| 1200 | * value. This makes sure SET_CUR requests on the streaming commit |
| 1201 | * control will always use values retrieved from a successful GET_CUR |
| 1202 | * request on the probe control, as required by the UVC specification. |
| 1203 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1204 | ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR); |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1205 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1206 | return ret; |
| 1207 | |
| 1208 | /* Check if the default format descriptor exists. Use the first |
| 1209 | * available format otherwise. |
| 1210 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1211 | for (i = stream->nformats; i > 0; --i) { |
| 1212 | format = &stream->format[i-1]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1213 | if (format->index == probe->bFormatIndex) |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | if (format->nframes == 0) { |
| 1218 | uvc_printk(KERN_INFO, "No frame descriptor found for the " |
| 1219 | "default format.\n"); |
| 1220 | return -EINVAL; |
| 1221 | } |
| 1222 | |
| 1223 | /* Zero bFrameIndex might be correct. Stream-based formats (including |
| 1224 | * MPEG-2 TS and DV) do not support frames but have a dummy frame |
| 1225 | * descriptor with bFrameIndex set to zero. If the default frame |
Laurent Pinchart | 078f894 | 2009-05-06 12:30:30 -0300 | [diff] [blame] | 1226 | * descriptor is not found, use the first available frame. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1227 | */ |
| 1228 | for (i = format->nframes; i > 0; --i) { |
| 1229 | frame = &format->frame[i-1]; |
| 1230 | if (frame->bFrameIndex == probe->bFrameIndex) |
| 1231 | break; |
| 1232 | } |
| 1233 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1234 | probe->bFormatIndex = format->index; |
| 1235 | probe->bFrameIndex = frame->bFrameIndex; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1236 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1237 | stream->cur_format = format; |
| 1238 | stream->cur_frame = frame; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1239 | |
| 1240 | /* Select the video decoding function */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1241 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1242 | if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT) |
| 1243 | stream->decode = uvc_video_decode_isight; |
| 1244 | else if (stream->intf->num_altsetting > 1) |
| 1245 | stream->decode = uvc_video_decode_isoc; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 1246 | else |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1247 | stream->decode = uvc_video_decode_bulk; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 1248 | } else { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1249 | if (stream->intf->num_altsetting == 1) |
| 1250 | stream->decode = uvc_video_encode_bulk; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 1251 | else { |
| 1252 | uvc_printk(KERN_INFO, "Isochronous endpoints are not " |
| 1253 | "supported for video output devices.\n"); |
| 1254 | return -EINVAL; |
| 1255 | } |
| 1256 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Enable or disable the video stream. |
| 1263 | */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1264 | int uvc_video_enable(struct uvc_streaming *stream, int enable) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1265 | { |
| 1266 | int ret; |
| 1267 | |
| 1268 | if (!enable) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1269 | uvc_uninit_video(stream, 1); |
| 1270 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); |
| 1271 | uvc_queue_enable(&stream->queue, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1272 | return 0; |
| 1273 | } |
| 1274 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1275 | ret = uvc_queue_enable(&stream->queue, 1); |
| 1276 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1277 | return ret; |
| 1278 | |
Laurent Pinchart | 23867b2 | 2008-11-12 11:46:43 -0300 | [diff] [blame] | 1279 | /* Commit the streaming parameters. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1280 | ret = uvc_commit_video(stream, &stream->ctrl); |
Sjoerd Simons | aa122d4 | 2011-05-30 15:02:00 -0300 | [diff] [blame] | 1281 | if (ret < 0) { |
| 1282 | uvc_queue_enable(&stream->queue, 0); |
Laurent Pinchart | 23867b2 | 2008-11-12 11:46:43 -0300 | [diff] [blame] | 1283 | return ret; |
Sjoerd Simons | aa122d4 | 2011-05-30 15:02:00 -0300 | [diff] [blame] | 1284 | } |
Laurent Pinchart | 23867b2 | 2008-11-12 11:46:43 -0300 | [diff] [blame] | 1285 | |
Laurent Pinchart | 24c3aae | 2011-10-25 09:03:42 -0300 | [diff] [blame^] | 1286 | ret = uvc_init_video(stream, GFP_KERNEL); |
| 1287 | if (ret < 0) { |
| 1288 | usb_set_interface(stream->dev->udev, stream->intfnum, 0); |
| 1289 | uvc_queue_enable(&stream->queue, 0); |
| 1290 | } |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 1291 | |
Laurent Pinchart | 24c3aae | 2011-10-25 09:03:42 -0300 | [diff] [blame^] | 1292 | return ret; |
| 1293 | } |