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