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 | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 4 | * Copyright (C) 2005-2009 |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 5 | * Laurent Pinchart (laurent.pinchart@skynet.be) |
| 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> |
| 17 | #include <linux/usb.h> |
| 18 | #include <linux/videodev2.h> |
| 19 | #include <linux/vmalloc.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <asm/atomic.h> |
| 22 | #include <asm/unaligned.h> |
| 23 | |
| 24 | #include <media/v4l2-common.h> |
| 25 | |
| 26 | #include "uvcvideo.h" |
| 27 | |
| 28 | /* ------------------------------------------------------------------------ |
| 29 | * UVC Controls |
| 30 | */ |
| 31 | |
| 32 | static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit, |
| 33 | __u8 intfnum, __u8 cs, void *data, __u16 size, |
| 34 | int timeout) |
| 35 | { |
| 36 | __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 37 | unsigned int pipe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 38 | |
| 39 | pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0) |
| 40 | : usb_sndctrlpipe(dev->udev, 0); |
| 41 | type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT; |
| 42 | |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 43 | return usb_control_msg(dev->udev, pipe, query, type, cs << 8, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 44 | unit << 8 | intfnum, data, size, timeout); |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 45 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 46 | |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 47 | int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit, |
| 48 | __u8 intfnum, __u8 cs, void *data, __u16 size) |
| 49 | { |
| 50 | int ret; |
| 51 | |
| 52 | ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size, |
| 53 | UVC_CTRL_CONTROL_TIMEOUT); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 54 | if (ret != size) { |
| 55 | uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u " |
| 56 | "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret, |
| 57 | size); |
| 58 | return -EIO; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Laurent Pinchart | 50144ae | 2009-02-16 17:41:52 -0300 | [diff] [blame] | 64 | static void uvc_fixup_video_ctrl(struct uvc_video_device *video, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 65 | struct uvc_streaming_control *ctrl) |
| 66 | { |
| 67 | struct uvc_format *format; |
Laurent Pinchart | 078f894 | 2009-05-06 12:30:30 -0300 | [diff] [blame^] | 68 | struct uvc_frame *frame = NULL; |
| 69 | unsigned int i; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 70 | |
| 71 | if (ctrl->bFormatIndex <= 0 || |
| 72 | ctrl->bFormatIndex > video->streaming->nformats) |
| 73 | return; |
| 74 | |
| 75 | format = &video->streaming->format[ctrl->bFormatIndex - 1]; |
| 76 | |
Laurent Pinchart | 078f894 | 2009-05-06 12:30:30 -0300 | [diff] [blame^] | 77 | for (i = 0; i < format->nframes; ++i) { |
| 78 | if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) { |
| 79 | frame = &format->frame[i]; |
| 80 | break; |
| 81 | } |
| 82 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 83 | |
Laurent Pinchart | 078f894 | 2009-05-06 12:30:30 -0300 | [diff] [blame^] | 84 | if (frame == NULL) |
| 85 | return; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 86 | |
| 87 | if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) || |
| 88 | (ctrl->dwMaxVideoFrameSize == 0 && |
| 89 | video->dev->uvc_version < 0x0110)) |
| 90 | ctrl->dwMaxVideoFrameSize = |
| 91 | frame->dwMaxVideoFrameBufferSize; |
Laurent Pinchart | 50144ae | 2009-02-16 17:41:52 -0300 | [diff] [blame] | 92 | |
| 93 | if (video->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH && |
| 94 | video->streaming->intf->num_altsetting > 1) { |
| 95 | u32 interval; |
| 96 | u32 bandwidth; |
| 97 | |
| 98 | interval = (ctrl->dwFrameInterval > 100000) |
| 99 | ? ctrl->dwFrameInterval |
| 100 | : frame->dwFrameInterval[0]; |
| 101 | |
| 102 | /* Compute a bandwidth estimation by multiplying the frame |
| 103 | * size by the number of video frames per second, divide the |
| 104 | * result by the number of USB frames (or micro-frames for |
| 105 | * high-speed devices) per second and add the UVC header size |
| 106 | * (assumed to be 12 bytes long). |
| 107 | */ |
| 108 | bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp; |
| 109 | bandwidth *= 10000000 / interval + 1; |
| 110 | bandwidth /= 1000; |
| 111 | if (video->dev->udev->speed == USB_SPEED_HIGH) |
| 112 | bandwidth /= 8; |
| 113 | bandwidth += 12; |
| 114 | |
| 115 | ctrl->dwMaxPayloadTransferSize = bandwidth; |
| 116 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int uvc_get_video_ctrl(struct uvc_video_device *video, |
| 120 | struct uvc_streaming_control *ctrl, int probe, __u8 query) |
| 121 | { |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 122 | __u8 *data; |
| 123 | __u16 size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 124 | int ret; |
| 125 | |
| 126 | size = video->dev->uvc_version >= 0x0110 ? 34 : 26; |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 127 | data = kmalloc(size, GFP_KERNEL); |
| 128 | if (data == NULL) |
| 129 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 130 | |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 131 | ret = __uvc_query_ctrl(video->dev, query, 0, video->streaming->intfnum, |
| 132 | probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size, |
| 133 | UVC_CTRL_STREAMING_TIMEOUT); |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 134 | |
| 135 | if ((query == GET_MIN || query == GET_MAX) && ret == 2) { |
| 136 | /* Some cameras, mostly based on Bison Electronics chipsets, |
| 137 | * answer a GET_MIN or GET_MAX request with the wCompQuality |
| 138 | * field only. |
| 139 | */ |
| 140 | uvc_warn_once(video->dev, UVC_WARN_MINMAX, "UVC non " |
| 141 | "compliance - GET_MIN/MAX(PROBE) incorrectly " |
| 142 | "supported. Enabling workaround.\n"); |
| 143 | memset(ctrl, 0, sizeof ctrl); |
| 144 | ctrl->wCompQuality = le16_to_cpup((__le16 *)data); |
| 145 | ret = 0; |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 146 | goto out; |
Laurent Pinchart | 51cac8a | 2009-01-03 20:08:26 -0300 | [diff] [blame] | 147 | } else if (query == GET_DEF && probe == 1 && ret != size) { |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 148 | /* Many cameras don't support the GET_DEF request on their |
| 149 | * video probe control. Warn once and return, the caller will |
| 150 | * fall back to GET_CUR. |
| 151 | */ |
| 152 | uvc_warn_once(video->dev, UVC_WARN_PROBE_DEF, "UVC non " |
| 153 | "compliance - GET_DEF(PROBE) not supported. " |
| 154 | "Enabling workaround.\n"); |
| 155 | ret = -EIO; |
| 156 | goto out; |
| 157 | } else if (ret != size) { |
| 158 | uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : " |
| 159 | "%d (exp. %u).\n", query, probe ? "probe" : "commit", |
| 160 | ret, size); |
| 161 | ret = -EIO; |
| 162 | goto out; |
| 163 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 164 | |
| 165 | ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]); |
| 166 | ctrl->bFormatIndex = data[2]; |
| 167 | ctrl->bFrameIndex = data[3]; |
| 168 | ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]); |
| 169 | ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]); |
| 170 | ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]); |
| 171 | ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]); |
| 172 | ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]); |
| 173 | ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]); |
Laurent Pinchart | 4d3939f | 2008-11-11 14:04:30 -0300 | [diff] [blame] | 174 | ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]); |
| 175 | ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 176 | |
| 177 | if (size == 34) { |
Laurent Pinchart | 4d3939f | 2008-11-11 14:04:30 -0300 | [diff] [blame] | 178 | ctrl->dwClockFrequency = get_unaligned_le32(&data[26]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 179 | ctrl->bmFramingInfo = data[30]; |
| 180 | ctrl->bPreferedVersion = data[31]; |
| 181 | ctrl->bMinVersion = data[32]; |
| 182 | ctrl->bMaxVersion = data[33]; |
| 183 | } else { |
| 184 | ctrl->dwClockFrequency = video->dev->clock_frequency; |
| 185 | ctrl->bmFramingInfo = 0; |
| 186 | ctrl->bPreferedVersion = 0; |
| 187 | ctrl->bMinVersion = 0; |
| 188 | ctrl->bMaxVersion = 0; |
| 189 | } |
| 190 | |
Laurent Pinchart | 50144ae | 2009-02-16 17:41:52 -0300 | [diff] [blame] | 191 | /* Some broken devices return null or wrong dwMaxVideoFrameSize and |
| 192 | * dwMaxPayloadTransferSize fields. Try to get the value from the |
| 193 | * format and frame descriptors. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 194 | */ |
Laurent Pinchart | 50144ae | 2009-02-16 17:41:52 -0300 | [diff] [blame] | 195 | uvc_fixup_video_ctrl(video, ctrl); |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 196 | ret = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 197 | |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 198 | out: |
| 199 | kfree(data); |
| 200 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 201 | } |
| 202 | |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 203 | static int uvc_set_video_ctrl(struct uvc_video_device *video, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 204 | struct uvc_streaming_control *ctrl, int probe) |
| 205 | { |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 206 | __u8 *data; |
| 207 | __u16 size; |
| 208 | int ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 209 | |
| 210 | size = video->dev->uvc_version >= 0x0110 ? 34 : 26; |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 211 | data = kzalloc(size, GFP_KERNEL); |
| 212 | if (data == NULL) |
| 213 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 214 | |
| 215 | *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint); |
| 216 | data[2] = ctrl->bFormatIndex; |
| 217 | data[3] = ctrl->bFrameIndex; |
| 218 | *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval); |
| 219 | *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate); |
| 220 | *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate); |
| 221 | *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality); |
| 222 | *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize); |
| 223 | *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay); |
Laurent Pinchart | 4d3939f | 2008-11-11 14:04:30 -0300 | [diff] [blame] | 224 | put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]); |
| 225 | put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 226 | |
| 227 | if (size == 34) { |
Laurent Pinchart | 4d3939f | 2008-11-11 14:04:30 -0300 | [diff] [blame] | 228 | put_unaligned_le32(ctrl->dwClockFrequency, &data[26]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 229 | data[30] = ctrl->bmFramingInfo; |
| 230 | data[31] = ctrl->bPreferedVersion; |
| 231 | data[32] = ctrl->bMinVersion; |
| 232 | data[33] = ctrl->bMaxVersion; |
| 233 | } |
| 234 | |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 235 | ret = __uvc_query_ctrl(video->dev, SET_CUR, 0, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 236 | video->streaming->intfnum, |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 237 | probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 238 | UVC_CTRL_STREAMING_TIMEOUT); |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 239 | if (ret != size) { |
| 240 | uvc_printk(KERN_ERR, "Failed to set UVC %s control : " |
| 241 | "%d (exp. %u).\n", probe ? "probe" : "commit", |
| 242 | ret, size); |
| 243 | ret = -EIO; |
| 244 | } |
Laurent Pinchart | 04793dd | 2008-07-31 17:11:12 -0300 | [diff] [blame] | 245 | |
| 246 | kfree(data); |
| 247 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | int uvc_probe_video(struct uvc_video_device *video, |
| 251 | struct uvc_streaming_control *probe) |
| 252 | { |
| 253 | struct uvc_streaming_control probe_min, probe_max; |
| 254 | __u16 bandwidth; |
| 255 | unsigned int i; |
| 256 | int ret; |
| 257 | |
| 258 | mutex_lock(&video->streaming->mutex); |
| 259 | |
| 260 | /* Perform probing. The device should adjust the requested values |
| 261 | * according to its capabilities. However, some devices, namely the |
| 262 | * first generation UVC Logitech webcams, don't implement the Video |
| 263 | * Probe control properly, and just return the needed bandwidth. For |
| 264 | * that reason, if the needed bandwidth exceeds the maximum available |
| 265 | * bandwidth, try to lower the quality. |
| 266 | */ |
| 267 | if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0) |
| 268 | goto done; |
| 269 | |
| 270 | /* Get the minimum and maximum values for compression settings. */ |
| 271 | if (!(video->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) { |
| 272 | ret = uvc_get_video_ctrl(video, &probe_min, 1, GET_MIN); |
| 273 | if (ret < 0) |
| 274 | goto done; |
| 275 | ret = uvc_get_video_ctrl(video, &probe_max, 1, GET_MAX); |
| 276 | if (ret < 0) |
| 277 | goto done; |
| 278 | |
| 279 | probe->wCompQuality = probe_max.wCompQuality; |
| 280 | } |
| 281 | |
| 282 | for (i = 0; i < 2; ++i) { |
| 283 | if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0 || |
| 284 | (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0) |
| 285 | goto done; |
| 286 | |
| 287 | if (video->streaming->intf->num_altsetting == 1) |
| 288 | break; |
| 289 | |
| 290 | bandwidth = probe->dwMaxPayloadTransferSize; |
| 291 | if (bandwidth <= video->streaming->maxpsize) |
| 292 | break; |
| 293 | |
| 294 | if (video->dev->quirks & UVC_QUIRK_PROBE_MINMAX) { |
| 295 | ret = -ENOSPC; |
| 296 | goto done; |
| 297 | } |
| 298 | |
| 299 | /* TODO: negotiate compression parameters */ |
| 300 | probe->wKeyFrameRate = probe_min.wKeyFrameRate; |
| 301 | probe->wPFrameRate = probe_min.wPFrameRate; |
| 302 | probe->wCompQuality = probe_max.wCompQuality; |
| 303 | probe->wCompWindowSize = probe_min.wCompWindowSize; |
| 304 | } |
| 305 | |
| 306 | done: |
| 307 | mutex_unlock(&video->streaming->mutex); |
| 308 | return ret; |
| 309 | } |
| 310 | |
Laurent Pinchart | 44f0079 | 2008-11-08 19:14:50 -0300 | [diff] [blame] | 311 | int uvc_commit_video(struct uvc_video_device *video, |
| 312 | struct uvc_streaming_control *probe) |
| 313 | { |
| 314 | return uvc_set_video_ctrl(video, probe, 0); |
| 315 | } |
| 316 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 317 | /* ------------------------------------------------------------------------ |
| 318 | * Video codecs |
| 319 | */ |
| 320 | |
| 321 | /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */ |
| 322 | #define UVC_STREAM_EOH (1 << 7) |
| 323 | #define UVC_STREAM_ERR (1 << 6) |
| 324 | #define UVC_STREAM_STI (1 << 5) |
| 325 | #define UVC_STREAM_RES (1 << 4) |
| 326 | #define UVC_STREAM_SCR (1 << 3) |
| 327 | #define UVC_STREAM_PTS (1 << 2) |
| 328 | #define UVC_STREAM_EOF (1 << 1) |
| 329 | #define UVC_STREAM_FID (1 << 0) |
| 330 | |
| 331 | /* Video payload decoding is handled by uvc_video_decode_start(), |
| 332 | * uvc_video_decode_data() and uvc_video_decode_end(). |
| 333 | * |
| 334 | * uvc_video_decode_start is called with URB data at the start of a bulk or |
| 335 | * isochronous payload. It processes header data and returns the header size |
| 336 | * in bytes if successful. If an error occurs, it returns a negative error |
| 337 | * code. The following error codes have special meanings. |
| 338 | * |
| 339 | * - EAGAIN informs the caller that the current video buffer should be marked |
| 340 | * as done, and that the function should be called again with the same data |
| 341 | * and a new video buffer. This is used when end of frame conditions can be |
| 342 | * reliably detected at the beginning of the next frame only. |
| 343 | * |
| 344 | * If an error other than -EAGAIN is returned, the caller will drop the current |
| 345 | * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be |
| 346 | * made until the next payload. -ENODATA can be used to drop the current |
| 347 | * payload if no other error code is appropriate. |
| 348 | * |
| 349 | * uvc_video_decode_data is called for every URB with URB data. It copies the |
| 350 | * data to the video buffer. |
| 351 | * |
| 352 | * uvc_video_decode_end is called with header data at the end of a bulk or |
| 353 | * isochronous payload. It performs any additional header data processing and |
| 354 | * returns 0 or a negative error code if an error occured. As header data have |
| 355 | * already been processed by uvc_video_decode_start, this functions isn't |
| 356 | * required to perform sanity checks a second time. |
| 357 | * |
| 358 | * For isochronous transfers where a payload is always transfered in a single |
| 359 | * URB, the three functions will be called in a row. |
| 360 | * |
| 361 | * To let the decoder process header data and update its internal state even |
| 362 | * when no video buffer is available, uvc_video_decode_start must be prepared |
| 363 | * to be called with a NULL buf parameter. uvc_video_decode_data and |
| 364 | * uvc_video_decode_end will never be called with a NULL buffer. |
| 365 | */ |
| 366 | static int uvc_video_decode_start(struct uvc_video_device *video, |
| 367 | struct uvc_buffer *buf, const __u8 *data, int len) |
| 368 | { |
| 369 | __u8 fid; |
| 370 | |
| 371 | /* Sanity checks: |
| 372 | * - packet must be at least 2 bytes long |
| 373 | * - bHeaderLength value must be at least 2 bytes (see above) |
| 374 | * - bHeaderLength value can't be larger than the packet size. |
| 375 | */ |
| 376 | if (len < 2 || data[0] < 2 || data[0] > len) |
| 377 | return -EINVAL; |
| 378 | |
| 379 | /* Skip payloads marked with the error bit ("error frames"). */ |
| 380 | if (data[1] & UVC_STREAM_ERR) { |
| 381 | uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit " |
| 382 | "set).\n"); |
| 383 | return -ENODATA; |
| 384 | } |
| 385 | |
| 386 | fid = data[1] & UVC_STREAM_FID; |
| 387 | |
| 388 | /* Store the payload FID bit and return immediately when the buffer is |
| 389 | * NULL. |
| 390 | */ |
| 391 | if (buf == NULL) { |
| 392 | video->last_fid = fid; |
| 393 | return -ENODATA; |
| 394 | } |
| 395 | |
| 396 | /* Synchronize to the input stream by waiting for the FID bit to be |
| 397 | * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE. |
Laurent Pinchart | f8dd4af | 2008-12-16 10:41:57 -0300 | [diff] [blame] | 398 | * video->last_fid is initialized to -1, so the first isochronous |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 399 | * frame will always be in sync. |
| 400 | * |
| 401 | * If the device doesn't toggle the FID bit, invert video->last_fid |
| 402 | * when the EOF bit is set to force synchronisation on the next packet. |
| 403 | */ |
| 404 | if (buf->state != UVC_BUF_STATE_ACTIVE) { |
| 405 | if (fid == video->last_fid) { |
| 406 | uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of " |
| 407 | "sync).\n"); |
| 408 | if ((video->dev->quirks & UVC_QUIRK_STREAM_NO_FID) && |
| 409 | (data[1] & UVC_STREAM_EOF)) |
| 410 | video->last_fid ^= UVC_STREAM_FID; |
| 411 | return -ENODATA; |
| 412 | } |
| 413 | |
| 414 | /* TODO: Handle PTS and SCR. */ |
| 415 | buf->state = UVC_BUF_STATE_ACTIVE; |
| 416 | } |
| 417 | |
| 418 | /* Mark the buffer as done if we're at the beginning of a new frame. |
| 419 | * End of frame detection is better implemented by checking the EOF |
| 420 | * bit (FID bit toggling is delayed by one frame compared to the EOF |
| 421 | * bit), but some devices don't set the bit at end of frame (and the |
| 422 | * last payload can be lost anyway). We thus must check if the FID has |
| 423 | * been toggled. |
| 424 | * |
Laurent Pinchart | f8dd4af | 2008-12-16 10:41:57 -0300 | [diff] [blame] | 425 | * video->last_fid is initialized to -1, so the first isochronous |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 426 | * frame will never trigger an end of frame detection. |
| 427 | * |
| 428 | * Empty buffers (bytesused == 0) don't trigger end of frame detection |
| 429 | * 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] | 430 | * avoids detecting end of frame conditions at FID toggling if the |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 431 | * previous payload had the EOF bit set. |
| 432 | */ |
| 433 | if (fid != video->last_fid && buf->buf.bytesused != 0) { |
| 434 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit " |
| 435 | "toggled).\n"); |
| 436 | buf->state = UVC_BUF_STATE_DONE; |
| 437 | return -EAGAIN; |
| 438 | } |
| 439 | |
| 440 | video->last_fid = fid; |
| 441 | |
| 442 | return data[0]; |
| 443 | } |
| 444 | |
| 445 | static void uvc_video_decode_data(struct uvc_video_device *video, |
| 446 | struct uvc_buffer *buf, const __u8 *data, int len) |
| 447 | { |
| 448 | struct uvc_video_queue *queue = &video->queue; |
| 449 | unsigned int maxlen, nbytes; |
| 450 | void *mem; |
| 451 | |
| 452 | if (len <= 0) |
| 453 | return; |
| 454 | |
| 455 | /* Copy the video data to the buffer. */ |
| 456 | maxlen = buf->buf.length - buf->buf.bytesused; |
| 457 | mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused; |
| 458 | nbytes = min((unsigned int)len, maxlen); |
| 459 | memcpy(mem, data, nbytes); |
| 460 | buf->buf.bytesused += nbytes; |
| 461 | |
| 462 | /* Complete the current frame if the buffer size was exceeded. */ |
| 463 | if (len > maxlen) { |
| 464 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n"); |
| 465 | buf->state = UVC_BUF_STATE_DONE; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static void uvc_video_decode_end(struct uvc_video_device *video, |
| 470 | struct uvc_buffer *buf, const __u8 *data, int len) |
| 471 | { |
| 472 | /* Mark the buffer as done if the EOF marker is set. */ |
| 473 | if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) { |
| 474 | uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n"); |
| 475 | if (data[0] == len) |
| 476 | uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n"); |
| 477 | buf->state = UVC_BUF_STATE_DONE; |
| 478 | if (video->dev->quirks & UVC_QUIRK_STREAM_NO_FID) |
| 479 | video->last_fid ^= UVC_STREAM_FID; |
| 480 | } |
| 481 | } |
| 482 | |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 483 | /* Video payload encoding is handled by uvc_video_encode_header() and |
| 484 | * uvc_video_encode_data(). Only bulk transfers are currently supported. |
| 485 | * |
| 486 | * uvc_video_encode_header is called at the start of a payload. It adds header |
| 487 | * data to the transfer buffer and returns the header size. As the only known |
| 488 | * UVC output device transfers a whole frame in a single payload, the EOF bit |
| 489 | * is always set in the header. |
| 490 | * |
| 491 | * uvc_video_encode_data is called for every URB and copies the data from the |
| 492 | * video buffer to the transfer buffer. |
| 493 | */ |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 494 | static int uvc_video_encode_header(struct uvc_video_device *video, |
| 495 | struct uvc_buffer *buf, __u8 *data, int len) |
| 496 | { |
| 497 | data[0] = 2; /* Header length */ |
| 498 | data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF |
| 499 | | (video->last_fid & UVC_STREAM_FID); |
| 500 | return 2; |
| 501 | } |
| 502 | |
| 503 | static int uvc_video_encode_data(struct uvc_video_device *video, |
| 504 | struct uvc_buffer *buf, __u8 *data, int len) |
| 505 | { |
| 506 | struct uvc_video_queue *queue = &video->queue; |
| 507 | unsigned int nbytes; |
| 508 | void *mem; |
| 509 | |
| 510 | /* Copy video data to the URB buffer. */ |
| 511 | mem = queue->mem + buf->buf.m.offset + queue->buf_used; |
| 512 | nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used); |
| 513 | nbytes = min(video->bulk.max_payload_size - video->bulk.payload_size, |
| 514 | nbytes); |
| 515 | memcpy(data, mem, nbytes); |
| 516 | |
| 517 | queue->buf_used += nbytes; |
| 518 | |
| 519 | return nbytes; |
| 520 | } |
| 521 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 522 | /* ------------------------------------------------------------------------ |
| 523 | * URB handling |
| 524 | */ |
| 525 | |
| 526 | /* |
| 527 | * Completion handler for video URBs. |
| 528 | */ |
| 529 | static void uvc_video_decode_isoc(struct urb *urb, |
| 530 | struct uvc_video_device *video, struct uvc_buffer *buf) |
| 531 | { |
| 532 | u8 *mem; |
| 533 | int ret, i; |
| 534 | |
| 535 | for (i = 0; i < urb->number_of_packets; ++i) { |
| 536 | if (urb->iso_frame_desc[i].status < 0) { |
| 537 | uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame " |
| 538 | "lost (%d).\n", urb->iso_frame_desc[i].status); |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | /* Decode the payload header. */ |
| 543 | mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
| 544 | do { |
| 545 | ret = uvc_video_decode_start(video, buf, mem, |
| 546 | urb->iso_frame_desc[i].actual_length); |
| 547 | if (ret == -EAGAIN) |
| 548 | buf = uvc_queue_next_buffer(&video->queue, buf); |
| 549 | } while (ret == -EAGAIN); |
| 550 | |
| 551 | if (ret < 0) |
| 552 | continue; |
| 553 | |
| 554 | /* Decode the payload data. */ |
| 555 | uvc_video_decode_data(video, buf, mem + ret, |
| 556 | urb->iso_frame_desc[i].actual_length - ret); |
| 557 | |
| 558 | /* Process the header again. */ |
Laurent Pinchart | 08ebd00 | 2008-08-29 17:37:44 -0300 | [diff] [blame] | 559 | uvc_video_decode_end(video, buf, mem, |
| 560 | urb->iso_frame_desc[i].actual_length); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 561 | |
| 562 | if (buf->state == UVC_BUF_STATE_DONE || |
| 563 | buf->state == UVC_BUF_STATE_ERROR) |
| 564 | buf = uvc_queue_next_buffer(&video->queue, buf); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | static void uvc_video_decode_bulk(struct urb *urb, |
| 569 | struct uvc_video_device *video, struct uvc_buffer *buf) |
| 570 | { |
| 571 | u8 *mem; |
| 572 | int len, ret; |
| 573 | |
Laurent Pinchart | c90e777 | 2009-02-14 19:39:08 -0300 | [diff] [blame] | 574 | if (urb->actual_length == 0) |
| 575 | return; |
| 576 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 577 | mem = urb->transfer_buffer; |
| 578 | len = urb->actual_length; |
| 579 | video->bulk.payload_size += len; |
| 580 | |
| 581 | /* If the URB is the first of its payload, decode and save the |
| 582 | * header. |
| 583 | */ |
Laurent Pinchart | f8dd4af | 2008-12-16 10:41:57 -0300 | [diff] [blame] | 584 | if (video->bulk.header_size == 0 && !video->bulk.skip_payload) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 585 | do { |
| 586 | ret = uvc_video_decode_start(video, buf, mem, len); |
| 587 | if (ret == -EAGAIN) |
| 588 | buf = uvc_queue_next_buffer(&video->queue, buf); |
| 589 | } while (ret == -EAGAIN); |
| 590 | |
| 591 | /* If an error occured skip the rest of the payload. */ |
| 592 | if (ret < 0 || buf == NULL) { |
| 593 | video->bulk.skip_payload = 1; |
Laurent Pinchart | f8dd4af | 2008-12-16 10:41:57 -0300 | [diff] [blame] | 594 | } else { |
| 595 | memcpy(video->bulk.header, mem, ret); |
| 596 | video->bulk.header_size = ret; |
| 597 | |
| 598 | mem += ret; |
| 599 | len -= ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 600 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* The buffer queue might have been cancelled while a bulk transfer |
| 604 | * was in progress, so we can reach here with buf equal to NULL. Make |
| 605 | * sure buf is never dereferenced if NULL. |
| 606 | */ |
| 607 | |
| 608 | /* Process video data. */ |
| 609 | if (!video->bulk.skip_payload && buf != NULL) |
| 610 | uvc_video_decode_data(video, buf, mem, len); |
| 611 | |
| 612 | /* Detect the payload end by a URB smaller than the maximum size (or |
| 613 | * a payload size equal to the maximum) and process the header again. |
| 614 | */ |
| 615 | if (urb->actual_length < urb->transfer_buffer_length || |
| 616 | video->bulk.payload_size >= video->bulk.max_payload_size) { |
| 617 | if (!video->bulk.skip_payload && buf != NULL) { |
| 618 | uvc_video_decode_end(video, buf, video->bulk.header, |
Laurent Pinchart | 08ebd00 | 2008-08-29 17:37:44 -0300 | [diff] [blame] | 619 | video->bulk.payload_size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 620 | if (buf->state == UVC_BUF_STATE_DONE || |
| 621 | buf->state == UVC_BUF_STATE_ERROR) |
| 622 | buf = uvc_queue_next_buffer(&video->queue, buf); |
| 623 | } |
| 624 | |
| 625 | video->bulk.header_size = 0; |
| 626 | video->bulk.skip_payload = 0; |
| 627 | video->bulk.payload_size = 0; |
| 628 | } |
| 629 | } |
| 630 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 631 | static void uvc_video_encode_bulk(struct urb *urb, |
| 632 | struct uvc_video_device *video, struct uvc_buffer *buf) |
| 633 | { |
| 634 | u8 *mem = urb->transfer_buffer; |
| 635 | int len = video->urb_size, ret; |
| 636 | |
| 637 | if (buf == NULL) { |
| 638 | urb->transfer_buffer_length = 0; |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | /* If the URB is the first of its payload, add the header. */ |
| 643 | if (video->bulk.header_size == 0) { |
| 644 | ret = uvc_video_encode_header(video, buf, mem, len); |
| 645 | video->bulk.header_size = ret; |
| 646 | video->bulk.payload_size += ret; |
| 647 | mem += ret; |
| 648 | len -= ret; |
| 649 | } |
| 650 | |
| 651 | /* Process video data. */ |
| 652 | ret = uvc_video_encode_data(video, buf, mem, len); |
| 653 | |
| 654 | video->bulk.payload_size += ret; |
| 655 | len -= ret; |
| 656 | |
| 657 | if (buf->buf.bytesused == video->queue.buf_used || |
| 658 | video->bulk.payload_size == video->bulk.max_payload_size) { |
| 659 | if (buf->buf.bytesused == video->queue.buf_used) { |
| 660 | video->queue.buf_used = 0; |
| 661 | buf->state = UVC_BUF_STATE_DONE; |
| 662 | uvc_queue_next_buffer(&video->queue, buf); |
| 663 | video->last_fid ^= UVC_STREAM_FID; |
| 664 | } |
| 665 | |
| 666 | video->bulk.header_size = 0; |
| 667 | video->bulk.payload_size = 0; |
| 668 | } |
| 669 | |
| 670 | urb->transfer_buffer_length = video->urb_size - len; |
| 671 | } |
| 672 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 673 | static void uvc_video_complete(struct urb *urb) |
| 674 | { |
| 675 | struct uvc_video_device *video = urb->context; |
| 676 | struct uvc_video_queue *queue = &video->queue; |
| 677 | struct uvc_buffer *buf = NULL; |
| 678 | unsigned long flags; |
| 679 | int ret; |
| 680 | |
| 681 | switch (urb->status) { |
| 682 | case 0: |
| 683 | break; |
| 684 | |
| 685 | default: |
| 686 | uvc_printk(KERN_WARNING, "Non-zero status (%d) in video " |
| 687 | "completion handler.\n", urb->status); |
| 688 | |
| 689 | case -ENOENT: /* usb_kill_urb() called. */ |
| 690 | if (video->frozen) |
| 691 | return; |
| 692 | |
| 693 | case -ECONNRESET: /* usb_unlink_urb() called. */ |
| 694 | case -ESHUTDOWN: /* The endpoint is being disabled. */ |
| 695 | uvc_queue_cancel(queue, urb->status == -ESHUTDOWN); |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | spin_lock_irqsave(&queue->irqlock, flags); |
| 700 | if (!list_empty(&queue->irqqueue)) |
| 701 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 702 | queue); |
| 703 | spin_unlock_irqrestore(&queue->irqlock, flags); |
| 704 | |
| 705 | video->decode(urb, video, buf); |
| 706 | |
| 707 | if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 708 | uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n", |
| 709 | ret); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 714 | * Free transfer buffers. |
| 715 | */ |
| 716 | static void uvc_free_urb_buffers(struct uvc_video_device *video) |
| 717 | { |
| 718 | unsigned int i; |
| 719 | |
| 720 | for (i = 0; i < UVC_URBS; ++i) { |
| 721 | if (video->urb_buffer[i]) { |
| 722 | usb_buffer_free(video->dev->udev, video->urb_size, |
| 723 | video->urb_buffer[i], video->urb_dma[i]); |
| 724 | video->urb_buffer[i] = NULL; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | video->urb_size = 0; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Allocate transfer buffers. This function can be called with buffers |
| 733 | * already allocated when resuming from suspend, in which case it will |
| 734 | * return without touching the buffers. |
| 735 | * |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 736 | * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the |
| 737 | * system is too low on memory try successively smaller numbers of packets |
| 738 | * until allocation succeeds. |
| 739 | * |
| 740 | * 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] | 741 | */ |
| 742 | static int uvc_alloc_urb_buffers(struct uvc_video_device *video, |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 743 | unsigned int size, unsigned int psize, gfp_t gfp_flags) |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 744 | { |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 745 | unsigned int npackets; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 746 | unsigned int i; |
| 747 | |
| 748 | /* Buffers are already allocated, bail out. */ |
| 749 | if (video->urb_size) |
Ming Lei | cb1287a | 2009-04-17 20:50:30 -0300 | [diff] [blame] | 750 | return video->urb_size / psize; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 751 | |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 752 | /* Compute the number of packets. Bulk endpoints might transfer UVC |
| 753 | * payloads accross multiple URBs. |
| 754 | */ |
| 755 | npackets = DIV_ROUND_UP(size, psize); |
| 756 | if (npackets > UVC_MAX_PACKETS) |
| 757 | npackets = UVC_MAX_PACKETS; |
| 758 | |
| 759 | /* Retry allocations until one succeed. */ |
| 760 | for (; npackets > 1; npackets /= 2) { |
| 761 | for (i = 0; i < UVC_URBS; ++i) { |
| 762 | video->urb_buffer[i] = usb_buffer_alloc( |
| 763 | video->dev->udev, psize * npackets, |
| 764 | gfp_flags | __GFP_NOWARN, &video->urb_dma[i]); |
| 765 | if (!video->urb_buffer[i]) { |
| 766 | uvc_free_urb_buffers(video); |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (i == UVC_URBS) { |
| 772 | video->urb_size = psize * npackets; |
| 773 | return npackets; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 781 | * Uninitialize isochronous/bulk URBs and free transfer buffers. |
| 782 | */ |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 783 | static void uvc_uninit_video(struct uvc_video_device *video, int free_buffers) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 784 | { |
| 785 | struct urb *urb; |
| 786 | unsigned int i; |
| 787 | |
| 788 | for (i = 0; i < UVC_URBS; ++i) { |
| 789 | if ((urb = video->urb[i]) == NULL) |
| 790 | continue; |
| 791 | |
| 792 | usb_kill_urb(urb); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 793 | usb_free_urb(urb); |
| 794 | video->urb[i] = NULL; |
| 795 | } |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 796 | |
| 797 | if (free_buffers) |
| 798 | uvc_free_urb_buffers(video); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Initialize isochronous URBs and allocate transfer buffers. The packet size |
| 803 | * is given by the endpoint. |
| 804 | */ |
| 805 | static int uvc_init_video_isoc(struct uvc_video_device *video, |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 806 | struct usb_host_endpoint *ep, gfp_t gfp_flags) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 807 | { |
| 808 | struct urb *urb; |
| 809 | unsigned int npackets, i, j; |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 810 | u16 psize; |
| 811 | u32 size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 812 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 813 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 814 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 815 | size = video->streaming->ctrl.dwMaxVideoFrameSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 816 | |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 817 | npackets = uvc_alloc_urb_buffers(video, size, psize, gfp_flags); |
| 818 | if (npackets == 0) |
| 819 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 820 | |
| 821 | size = npackets * psize; |
| 822 | |
| 823 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 824 | urb = usb_alloc_urb(npackets, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 825 | if (urb == NULL) { |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 826 | uvc_uninit_video(video, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 827 | return -ENOMEM; |
| 828 | } |
| 829 | |
| 830 | urb->dev = video->dev->udev; |
| 831 | urb->context = video; |
| 832 | urb->pipe = usb_rcvisocpipe(video->dev->udev, |
| 833 | ep->desc.bEndpointAddress); |
| 834 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
| 835 | urb->interval = ep->desc.bInterval; |
| 836 | urb->transfer_buffer = video->urb_buffer[i]; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 837 | urb->transfer_dma = video->urb_dma[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 838 | urb->complete = uvc_video_complete; |
| 839 | urb->number_of_packets = npackets; |
| 840 | urb->transfer_buffer_length = size; |
| 841 | |
| 842 | for (j = 0; j < npackets; ++j) { |
| 843 | urb->iso_frame_desc[j].offset = j * psize; |
| 844 | urb->iso_frame_desc[j].length = psize; |
| 845 | } |
| 846 | |
| 847 | video->urb[i] = urb; |
| 848 | } |
| 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Initialize bulk URBs and allocate transfer buffers. The packet size is |
| 855 | * given by the endpoint. |
| 856 | */ |
| 857 | static int uvc_init_video_bulk(struct uvc_video_device *video, |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 858 | struct usb_host_endpoint *ep, gfp_t gfp_flags) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 859 | { |
| 860 | struct urb *urb; |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 861 | unsigned int npackets, pipe, i; |
| 862 | u16 psize; |
| 863 | u32 size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 864 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 865 | psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff; |
| 866 | size = video->streaming->ctrl.dwMaxPayloadTransferSize; |
| 867 | video->bulk.max_payload_size = size; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 868 | |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 869 | npackets = uvc_alloc_urb_buffers(video, size, psize, gfp_flags); |
| 870 | if (npackets == 0) |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 871 | return -ENOMEM; |
| 872 | |
Laurent Pinchart | efdc8a9 | 2009-01-18 17:46:30 -0300 | [diff] [blame] | 873 | size = npackets * psize; |
| 874 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 875 | if (usb_endpoint_dir_in(&ep->desc)) |
| 876 | pipe = usb_rcvbulkpipe(video->dev->udev, |
| 877 | ep->desc.bEndpointAddress); |
| 878 | else |
| 879 | pipe = usb_sndbulkpipe(video->dev->udev, |
| 880 | ep->desc.bEndpointAddress); |
| 881 | |
| 882 | if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 883 | size = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 884 | |
| 885 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 886 | urb = usb_alloc_urb(0, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 887 | if (urb == NULL) { |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 888 | uvc_uninit_video(video, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 889 | return -ENOMEM; |
| 890 | } |
| 891 | |
| 892 | usb_fill_bulk_urb(urb, video->dev->udev, pipe, |
| 893 | video->urb_buffer[i], size, uvc_video_complete, |
| 894 | video); |
| 895 | urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 896 | urb->transfer_dma = video->urb_dma[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 897 | |
| 898 | video->urb[i] = urb; |
| 899 | } |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Initialize isochronous/bulk URBs and allocate transfer buffers. |
| 906 | */ |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 907 | static int uvc_init_video(struct uvc_video_device *video, gfp_t gfp_flags) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 908 | { |
| 909 | struct usb_interface *intf = video->streaming->intf; |
| 910 | struct usb_host_interface *alts; |
| 911 | struct usb_host_endpoint *ep = NULL; |
| 912 | int intfnum = video->streaming->intfnum; |
| 913 | unsigned int bandwidth, psize, i; |
| 914 | int ret; |
| 915 | |
| 916 | video->last_fid = -1; |
| 917 | video->bulk.header_size = 0; |
| 918 | video->bulk.skip_payload = 0; |
| 919 | video->bulk.payload_size = 0; |
| 920 | |
| 921 | if (intf->num_altsetting > 1) { |
| 922 | /* Isochronous endpoint, select the alternate setting. */ |
| 923 | bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize; |
| 924 | |
| 925 | if (bandwidth == 0) { |
| 926 | uvc_printk(KERN_WARNING, "device %s requested null " |
| 927 | "bandwidth, defaulting to lowest.\n", |
| 928 | video->vdev->name); |
| 929 | bandwidth = 1; |
| 930 | } |
| 931 | |
| 932 | for (i = 0; i < intf->num_altsetting; ++i) { |
| 933 | alts = &intf->altsetting[i]; |
| 934 | ep = uvc_find_endpoint(alts, |
| 935 | video->streaming->header.bEndpointAddress); |
| 936 | if (ep == NULL) |
| 937 | continue; |
| 938 | |
| 939 | /* Check if the bandwidth is high enough. */ |
| 940 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 941 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
| 942 | if (psize >= bandwidth) |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | if (i >= intf->num_altsetting) |
| 947 | return -EIO; |
| 948 | |
| 949 | if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0) |
| 950 | return ret; |
| 951 | |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 952 | ret = uvc_init_video_isoc(video, ep, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 953 | } else { |
| 954 | /* Bulk endpoint, proceed to URB initialization. */ |
| 955 | ep = uvc_find_endpoint(&intf->altsetting[0], |
| 956 | video->streaming->header.bEndpointAddress); |
| 957 | if (ep == NULL) |
| 958 | return -EIO; |
| 959 | |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 960 | ret = uvc_init_video_bulk(video, ep, gfp_flags); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | if (ret < 0) |
| 964 | return ret; |
| 965 | |
| 966 | /* Submit the URBs. */ |
| 967 | for (i = 0; i < UVC_URBS; ++i) { |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 968 | if ((ret = usb_submit_urb(video->urb[i], gfp_flags)) < 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 969 | uvc_printk(KERN_ERR, "Failed to submit URB %u " |
| 970 | "(%d).\n", i, ret); |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 971 | uvc_uninit_video(video, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 972 | return ret; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | /* -------------------------------------------------------------------------- |
| 980 | * Suspend/resume |
| 981 | */ |
| 982 | |
| 983 | /* |
| 984 | * Stop streaming without disabling the video queue. |
| 985 | * |
| 986 | * To let userspace applications resume without trouble, we must not touch the |
| 987 | * video buffers in any way. We mark the device as frozen to make sure the URB |
| 988 | * completion handler won't try to cancel the queue when we kill the URBs. |
| 989 | */ |
| 990 | int uvc_video_suspend(struct uvc_video_device *video) |
| 991 | { |
| 992 | if (!uvc_queue_streaming(&video->queue)) |
| 993 | return 0; |
| 994 | |
| 995 | video->frozen = 1; |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 996 | uvc_uninit_video(video, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 997 | usb_set_interface(video->dev->udev, video->streaming->intfnum, 0); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | /* |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 1002 | * Reconfigure the video interface and restart streaming if it was enabled |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1003 | * before suspend. |
| 1004 | * |
| 1005 | * If an error occurs, disable the video queue. This will wake all pending |
| 1006 | * buffers, making sure userspace applications are notified of the problem |
| 1007 | * instead of waiting forever. |
| 1008 | */ |
| 1009 | int uvc_video_resume(struct uvc_video_device *video) |
| 1010 | { |
| 1011 | int ret; |
| 1012 | |
| 1013 | video->frozen = 0; |
| 1014 | |
Laurent Pinchart | 23867b2 | 2008-11-12 11:46:43 -0300 | [diff] [blame] | 1015 | if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1016 | uvc_queue_enable(&video->queue, 0); |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | if (!uvc_queue_streaming(&video->queue)) |
| 1021 | return 0; |
| 1022 | |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 1023 | if ((ret = uvc_init_video(video, GFP_NOIO)) < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1024 | uvc_queue_enable(&video->queue, 0); |
| 1025 | |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
| 1029 | /* ------------------------------------------------------------------------ |
| 1030 | * Video device |
| 1031 | */ |
| 1032 | |
| 1033 | /* |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 1034 | * Initialize the UVC video device by switching to alternate setting 0 and |
| 1035 | * retrieve the default format. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1036 | * |
| 1037 | * Some cameras (namely the Fuji Finepix) set the format and frame |
| 1038 | * indexes to zero. The UVC standard doesn't clearly make this a spec |
| 1039 | * violation, so try to silently fix the values if possible. |
| 1040 | * |
| 1041 | * This function is called before registering the device with V4L. |
| 1042 | */ |
| 1043 | int uvc_video_init(struct uvc_video_device *video) |
| 1044 | { |
| 1045 | struct uvc_streaming_control *probe = &video->streaming->ctrl; |
| 1046 | struct uvc_format *format = NULL; |
| 1047 | struct uvc_frame *frame = NULL; |
| 1048 | unsigned int i; |
| 1049 | int ret; |
| 1050 | |
| 1051 | if (video->streaming->nformats == 0) { |
| 1052 | uvc_printk(KERN_INFO, "No supported video formats found.\n"); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
| 1056 | /* Alternate setting 0 should be the default, yet the XBox Live Vision |
| 1057 | * Cam (and possibly other devices) crash or otherwise misbehave if |
| 1058 | * they don't receive a SET_INTERFACE request before any other video |
| 1059 | * control request. |
| 1060 | */ |
| 1061 | usb_set_interface(video->dev->udev, video->streaming->intfnum, 0); |
| 1062 | |
Laurent Pinchart | 7236242 | 2009-02-14 19:26:56 -0300 | [diff] [blame] | 1063 | /* Set the streaming probe control with default streaming parameters |
| 1064 | * retrieved from the device. Webcams that don't suport GET_DEF |
| 1065 | * requests on the probe control will just keep their current streaming |
| 1066 | * parameters. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1067 | */ |
Laurent Pinchart | 7236242 | 2009-02-14 19:26:56 -0300 | [diff] [blame] | 1068 | if (uvc_get_video_ctrl(video, probe, 1, GET_DEF) == 0) |
| 1069 | uvc_set_video_ctrl(video, probe, 1); |
| 1070 | |
| 1071 | /* Initialize the streaming parameters with the probe control current |
| 1072 | * value. This makes sure SET_CUR requests on the streaming commit |
| 1073 | * control will always use values retrieved from a successful GET_CUR |
| 1074 | * request on the probe control, as required by the UVC specification. |
| 1075 | */ |
| 1076 | if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1077 | return ret; |
| 1078 | |
| 1079 | /* Check if the default format descriptor exists. Use the first |
| 1080 | * available format otherwise. |
| 1081 | */ |
| 1082 | for (i = video->streaming->nformats; i > 0; --i) { |
| 1083 | format = &video->streaming->format[i-1]; |
| 1084 | if (format->index == probe->bFormatIndex) |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | if (format->nframes == 0) { |
| 1089 | uvc_printk(KERN_INFO, "No frame descriptor found for the " |
| 1090 | "default format.\n"); |
| 1091 | return -EINVAL; |
| 1092 | } |
| 1093 | |
| 1094 | /* Zero bFrameIndex might be correct. Stream-based formats (including |
| 1095 | * MPEG-2 TS and DV) do not support frames but have a dummy frame |
| 1096 | * descriptor with bFrameIndex set to zero. If the default frame |
Laurent Pinchart | 078f894 | 2009-05-06 12:30:30 -0300 | [diff] [blame^] | 1097 | * descriptor is not found, use the first available frame. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1098 | */ |
| 1099 | for (i = format->nframes; i > 0; --i) { |
| 1100 | frame = &format->frame[i-1]; |
| 1101 | if (frame->bFrameIndex == probe->bFrameIndex) |
| 1102 | break; |
| 1103 | } |
| 1104 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1105 | probe->bFormatIndex = format->index; |
| 1106 | probe->bFrameIndex = frame->bFrameIndex; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1107 | |
| 1108 | video->streaming->cur_format = format; |
| 1109 | video->streaming->cur_frame = frame; |
| 1110 | atomic_set(&video->active, 0); |
| 1111 | |
| 1112 | /* Select the video decoding function */ |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 1113 | if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1114 | if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT) |
| 1115 | video->decode = uvc_video_decode_isight; |
| 1116 | else if (video->streaming->intf->num_altsetting > 1) |
| 1117 | video->decode = uvc_video_decode_isoc; |
| 1118 | else |
| 1119 | video->decode = uvc_video_decode_bulk; |
| 1120 | } else { |
| 1121 | if (video->streaming->intf->num_altsetting == 1) |
| 1122 | video->decode = uvc_video_encode_bulk; |
| 1123 | else { |
| 1124 | uvc_printk(KERN_INFO, "Isochronous endpoints are not " |
| 1125 | "supported for video output devices.\n"); |
| 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Enable or disable the video stream. |
| 1135 | */ |
| 1136 | int uvc_video_enable(struct uvc_video_device *video, int enable) |
| 1137 | { |
| 1138 | int ret; |
| 1139 | |
| 1140 | if (!enable) { |
Laurent Pinchart | e01117c | 2008-07-04 00:36:21 -0300 | [diff] [blame] | 1141 | uvc_uninit_video(video, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1142 | usb_set_interface(video->dev->udev, |
| 1143 | video->streaming->intfnum, 0); |
| 1144 | uvc_queue_enable(&video->queue, 0); |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Laurent Pinchart | 0fbd8ee | 2008-12-06 16:25:14 -0300 | [diff] [blame] | 1148 | if ((video->streaming->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) || |
| 1149 | uvc_no_drop_param) |
Laurent Pinchart | d63beb9 | 2008-09-15 22:24:29 -0300 | [diff] [blame] | 1150 | video->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE; |
| 1151 | else |
| 1152 | video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE; |
| 1153 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1154 | if ((ret = uvc_queue_enable(&video->queue, 1)) < 0) |
| 1155 | return ret; |
| 1156 | |
Laurent Pinchart | 23867b2 | 2008-11-12 11:46:43 -0300 | [diff] [blame] | 1157 | /* Commit the streaming parameters. */ |
| 1158 | if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0) |
| 1159 | return ret; |
| 1160 | |
Laurent Pinchart | 2913587 | 2008-07-04 00:35:26 -0300 | [diff] [blame] | 1161 | return uvc_init_video(video, GFP_KERNEL); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1162 | } |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 1163 | |