Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 1 | /* |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 2 | * Copyright (c) 2013,2016 Lubomir Rintel |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 3 | * All rights reserved. |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions, and the following disclaimer, |
| 10 | * without modification. |
| 11 | * 2. The name of the author may not be used to endorse or promote products |
| 12 | * derived from this software without specific prior written permission. |
| 13 | * |
| 14 | * Alternatively, this software may be distributed under the terms of the |
| 15 | * GNU General Public License ("GPL"). |
Lubomir Rintel | 5ae1e2b | 2016-06-01 10:03:44 -0300 | [diff] [blame] | 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | /* |
| 30 | * Fushicai USBTV007 Audio-Video Grabber Driver |
| 31 | * |
| 32 | * Product web site: |
| 33 | * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html |
| 34 | * |
| 35 | * Following LWN articles were very useful in construction of this driver: |
| 36 | * Video4Linux2 API series: http://lwn.net/Articles/203924/ |
| 37 | * videobuf2 API explanation: http://lwn.net/Articles/447435/ |
| 38 | * Thanks go to Jonathan Corbet for providing this quality documentation. |
| 39 | * He is awesome. |
| 40 | * |
| 41 | * No physical hardware was harmed running Windows during the |
| 42 | * reverse-engineering activity |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 43 | */ |
| 44 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 45 | #include <media/v4l2-ioctl.h> |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 46 | #include <media/videobuf2-v4l2.h> |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 47 | |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 48 | #include "usbtv.h" |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 49 | |
| 50 | static struct usbtv_norm_params norm_params[] = { |
| 51 | { |
| 52 | .norm = V4L2_STD_525_60, |
| 53 | .cap_width = 720, |
| 54 | .cap_height = 480, |
| 55 | }, |
| 56 | { |
| 57 | .norm = V4L2_STD_PAL, |
| 58 | .cap_width = 720, |
| 59 | .cap_height = 576, |
Hugo Grostabussiat | c41e20d | 2018-04-08 17:11:57 -0400 | [diff] [blame^] | 60 | }, |
| 61 | { |
| 62 | .norm = V4L2_STD_SECAM, |
| 63 | .cap_width = 720, |
| 64 | .cap_height = 576, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 65 | } |
| 66 | }; |
| 67 | |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 68 | static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm) |
| 69 | { |
| 70 | int i, ret = 0; |
| 71 | struct usbtv_norm_params *params = NULL; |
| 72 | |
| 73 | for (i = 0; i < ARRAY_SIZE(norm_params); i++) { |
| 74 | if (norm_params[i].norm & norm) { |
| 75 | params = &norm_params[i]; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (params) { |
| 81 | usbtv->width = params->cap_width; |
| 82 | usbtv->height = params->cap_height; |
| 83 | usbtv->n_chunks = usbtv->width * usbtv->height |
| 84 | / 4 / USBTV_CHUNK; |
| 85 | usbtv->norm = params->norm; |
| 86 | } else |
| 87 | ret = -EINVAL; |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 92 | static int usbtv_select_input(struct usbtv *usbtv, int input) |
| 93 | { |
| 94 | int ret; |
| 95 | |
| 96 | static const u16 composite[][2] = { |
| 97 | { USBTV_BASE + 0x0105, 0x0060 }, |
| 98 | { USBTV_BASE + 0x011f, 0x00f2 }, |
| 99 | { USBTV_BASE + 0x0127, 0x0060 }, |
| 100 | { USBTV_BASE + 0x00ae, 0x0010 }, |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 101 | { USBTV_BASE + 0x0239, 0x0060 }, |
| 102 | }; |
| 103 | |
| 104 | static const u16 svideo[][2] = { |
| 105 | { USBTV_BASE + 0x0105, 0x0010 }, |
| 106 | { USBTV_BASE + 0x011f, 0x00ff }, |
| 107 | { USBTV_BASE + 0x0127, 0x0060 }, |
| 108 | { USBTV_BASE + 0x00ae, 0x0030 }, |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 109 | { USBTV_BASE + 0x0239, 0x0060 }, |
| 110 | }; |
| 111 | |
| 112 | switch (input) { |
| 113 | case USBTV_COMPOSITE_INPUT: |
| 114 | ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite)); |
| 115 | break; |
| 116 | case USBTV_SVIDEO_INPUT: |
| 117 | ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo)); |
| 118 | break; |
| 119 | default: |
| 120 | ret = -EINVAL; |
| 121 | } |
| 122 | |
| 123 | if (!ret) |
| 124 | usbtv->input = input; |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 129 | static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm) |
| 130 | { |
| 131 | int ret; |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 132 | /* These are the series of register values used to configure the |
| 133 | * decoder for a specific standard. |
| 134 | * The first 21 register writes are copied from the |
| 135 | * Settings\DecoderDefaults registry keys present in the Windows driver |
| 136 | * .INF file, and control various image tuning parameters (color |
| 137 | * correction, sharpness, ...). |
| 138 | */ |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 139 | static const u16 pal[][2] = { |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 140 | /* "AVPAL" tuning sequence from .INF file */ |
| 141 | { USBTV_BASE + 0x0003, 0x0004 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 142 | { USBTV_BASE + 0x001a, 0x0068 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 143 | { USBTV_BASE + 0x0100, 0x00d3 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 144 | { USBTV_BASE + 0x010e, 0x0072 }, |
| 145 | { USBTV_BASE + 0x010f, 0x00a2 }, |
| 146 | { USBTV_BASE + 0x0112, 0x00b0 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 147 | { USBTV_BASE + 0x0115, 0x0015 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 148 | { USBTV_BASE + 0x0117, 0x0001 }, |
| 149 | { USBTV_BASE + 0x0118, 0x002c }, |
| 150 | { USBTV_BASE + 0x012d, 0x0010 }, |
| 151 | { USBTV_BASE + 0x012f, 0x0020 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 152 | { USBTV_BASE + 0x0220, 0x002e }, |
| 153 | { USBTV_BASE + 0x0225, 0x0008 }, |
| 154 | { USBTV_BASE + 0x024e, 0x0002 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 155 | { USBTV_BASE + 0x024f, 0x0002 }, |
| 156 | { USBTV_BASE + 0x0254, 0x0059 }, |
| 157 | { USBTV_BASE + 0x025a, 0x0016 }, |
| 158 | { USBTV_BASE + 0x025b, 0x0035 }, |
| 159 | { USBTV_BASE + 0x0263, 0x0017 }, |
| 160 | { USBTV_BASE + 0x0266, 0x0016 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 161 | { USBTV_BASE + 0x0267, 0x0036 }, |
| 162 | /* Epilog */ |
| 163 | { USBTV_BASE + 0x024e, 0x0002 }, |
| 164 | { USBTV_BASE + 0x024f, 0x0002 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | static const u16 ntsc[][2] = { |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 168 | /* "AVNTSC" tuning sequence from .INF file */ |
| 169 | { USBTV_BASE + 0x0003, 0x0004 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 170 | { USBTV_BASE + 0x001a, 0x0079 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 171 | { USBTV_BASE + 0x0100, 0x00d3 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 172 | { USBTV_BASE + 0x010e, 0x0068 }, |
| 173 | { USBTV_BASE + 0x010f, 0x009c }, |
| 174 | { USBTV_BASE + 0x0112, 0x00f0 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 175 | { USBTV_BASE + 0x0115, 0x0015 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 176 | { USBTV_BASE + 0x0117, 0x0000 }, |
| 177 | { USBTV_BASE + 0x0118, 0x00fc }, |
| 178 | { USBTV_BASE + 0x012d, 0x0004 }, |
| 179 | { USBTV_BASE + 0x012f, 0x0008 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 180 | { USBTV_BASE + 0x0220, 0x002e }, |
| 181 | { USBTV_BASE + 0x0225, 0x0008 }, |
| 182 | { USBTV_BASE + 0x024e, 0x0002 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 183 | { USBTV_BASE + 0x024f, 0x0001 }, |
| 184 | { USBTV_BASE + 0x0254, 0x005f }, |
| 185 | { USBTV_BASE + 0x025a, 0x0012 }, |
| 186 | { USBTV_BASE + 0x025b, 0x0001 }, |
| 187 | { USBTV_BASE + 0x0263, 0x001c }, |
| 188 | { USBTV_BASE + 0x0266, 0x0011 }, |
Hugo Grostabussiat | 5c909be | 2018-04-08 17:11:56 -0400 | [diff] [blame] | 189 | { USBTV_BASE + 0x0267, 0x0005 }, |
| 190 | /* Epilog */ |
| 191 | { USBTV_BASE + 0x024e, 0x0002 }, |
| 192 | { USBTV_BASE + 0x024f, 0x0002 }, |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 193 | }; |
| 194 | |
Hugo Grostabussiat | c41e20d | 2018-04-08 17:11:57 -0400 | [diff] [blame^] | 195 | static const u16 secam[][2] = { |
| 196 | /* "AVSECAM" tuning sequence from .INF file */ |
| 197 | { USBTV_BASE + 0x0003, 0x0004 }, |
| 198 | { USBTV_BASE + 0x001a, 0x0073 }, |
| 199 | { USBTV_BASE + 0x0100, 0x00dc }, |
| 200 | { USBTV_BASE + 0x010e, 0x0072 }, |
| 201 | { USBTV_BASE + 0x010f, 0x00a2 }, |
| 202 | { USBTV_BASE + 0x0112, 0x0090 }, |
| 203 | { USBTV_BASE + 0x0115, 0x0035 }, |
| 204 | { USBTV_BASE + 0x0117, 0x0001 }, |
| 205 | { USBTV_BASE + 0x0118, 0x0030 }, |
| 206 | { USBTV_BASE + 0x012d, 0x0004 }, |
| 207 | { USBTV_BASE + 0x012f, 0x0008 }, |
| 208 | { USBTV_BASE + 0x0220, 0x002d }, |
| 209 | { USBTV_BASE + 0x0225, 0x0028 }, |
| 210 | { USBTV_BASE + 0x024e, 0x0008 }, |
| 211 | { USBTV_BASE + 0x024f, 0x0002 }, |
| 212 | { USBTV_BASE + 0x0254, 0x0069 }, |
| 213 | { USBTV_BASE + 0x025a, 0x0016 }, |
| 214 | { USBTV_BASE + 0x025b, 0x0035 }, |
| 215 | { USBTV_BASE + 0x0263, 0x0021 }, |
| 216 | { USBTV_BASE + 0x0266, 0x0016 }, |
| 217 | { USBTV_BASE + 0x0267, 0x0036 }, |
| 218 | /* Epilog */ |
| 219 | { USBTV_BASE + 0x024e, 0x0002 }, |
| 220 | { USBTV_BASE + 0x024f, 0x0002 }, |
| 221 | }; |
| 222 | |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 223 | ret = usbtv_configure_for_norm(usbtv, norm); |
| 224 | |
| 225 | if (!ret) { |
| 226 | if (norm & V4L2_STD_525_60) |
| 227 | ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc)); |
| 228 | else if (norm & V4L2_STD_PAL) |
| 229 | ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal)); |
Hugo Grostabussiat | c41e20d | 2018-04-08 17:11:57 -0400 | [diff] [blame^] | 230 | else if (norm & V4L2_STD_SECAM) |
| 231 | ret = usbtv_set_regs(usbtv, secam, ARRAY_SIZE(secam)); |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 237 | static int usbtv_setup_capture(struct usbtv *usbtv) |
| 238 | { |
| 239 | int ret; |
| 240 | static const u16 setup[][2] = { |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 241 | /* These seem to enable the device. */ |
| 242 | { USBTV_BASE + 0x0008, 0x0001 }, |
| 243 | { USBTV_BASE + 0x01d0, 0x00ff }, |
| 244 | { USBTV_BASE + 0x01d9, 0x0002 }, |
| 245 | |
| 246 | /* These seem to influence color parameters, such as |
| 247 | * brightness, etc. */ |
| 248 | { USBTV_BASE + 0x0239, 0x0040 }, |
| 249 | { USBTV_BASE + 0x0240, 0x0000 }, |
| 250 | { USBTV_BASE + 0x0241, 0x0000 }, |
| 251 | { USBTV_BASE + 0x0242, 0x0002 }, |
| 252 | { USBTV_BASE + 0x0243, 0x0080 }, |
| 253 | { USBTV_BASE + 0x0244, 0x0012 }, |
| 254 | { USBTV_BASE + 0x0245, 0x0090 }, |
| 255 | { USBTV_BASE + 0x0246, 0x0000 }, |
| 256 | |
| 257 | { USBTV_BASE + 0x0278, 0x002d }, |
| 258 | { USBTV_BASE + 0x0279, 0x000a }, |
| 259 | { USBTV_BASE + 0x027a, 0x0032 }, |
| 260 | { 0xf890, 0x000c }, |
| 261 | { 0xf894, 0x0086 }, |
| 262 | |
| 263 | { USBTV_BASE + 0x00ac, 0x00c0 }, |
| 264 | { USBTV_BASE + 0x00ad, 0x0000 }, |
| 265 | { USBTV_BASE + 0x00a2, 0x0012 }, |
| 266 | { USBTV_BASE + 0x00a3, 0x00e0 }, |
| 267 | { USBTV_BASE + 0x00a4, 0x0028 }, |
| 268 | { USBTV_BASE + 0x00a5, 0x0082 }, |
| 269 | { USBTV_BASE + 0x00a7, 0x0080 }, |
| 270 | { USBTV_BASE + 0x0000, 0x0014 }, |
| 271 | { USBTV_BASE + 0x0006, 0x0003 }, |
| 272 | { USBTV_BASE + 0x0090, 0x0099 }, |
| 273 | { USBTV_BASE + 0x0091, 0x0090 }, |
| 274 | { USBTV_BASE + 0x0094, 0x0068 }, |
| 275 | { USBTV_BASE + 0x0095, 0x0070 }, |
| 276 | { USBTV_BASE + 0x009c, 0x0030 }, |
| 277 | { USBTV_BASE + 0x009d, 0x00c0 }, |
| 278 | { USBTV_BASE + 0x009e, 0x00e0 }, |
| 279 | { USBTV_BASE + 0x0019, 0x0006 }, |
| 280 | { USBTV_BASE + 0x008c, 0x00ba }, |
| 281 | { USBTV_BASE + 0x0101, 0x00ff }, |
| 282 | { USBTV_BASE + 0x010c, 0x00b3 }, |
| 283 | { USBTV_BASE + 0x01b2, 0x0080 }, |
| 284 | { USBTV_BASE + 0x01b4, 0x00a0 }, |
| 285 | { USBTV_BASE + 0x014c, 0x00ff }, |
| 286 | { USBTV_BASE + 0x014d, 0x00ca }, |
| 287 | { USBTV_BASE + 0x0113, 0x0053 }, |
| 288 | { USBTV_BASE + 0x0119, 0x008a }, |
| 289 | { USBTV_BASE + 0x013c, 0x0003 }, |
| 290 | { USBTV_BASE + 0x0150, 0x009c }, |
| 291 | { USBTV_BASE + 0x0151, 0x0071 }, |
| 292 | { USBTV_BASE + 0x0152, 0x00c6 }, |
| 293 | { USBTV_BASE + 0x0153, 0x0084 }, |
| 294 | { USBTV_BASE + 0x0154, 0x00bc }, |
| 295 | { USBTV_BASE + 0x0155, 0x00a0 }, |
| 296 | { USBTV_BASE + 0x0156, 0x00a0 }, |
| 297 | { USBTV_BASE + 0x0157, 0x009c }, |
| 298 | { USBTV_BASE + 0x0158, 0x001f }, |
| 299 | { USBTV_BASE + 0x0159, 0x0006 }, |
| 300 | { USBTV_BASE + 0x015d, 0x0000 }, |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 301 | }; |
| 302 | |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 303 | ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup)); |
| 304 | if (ret) |
| 305 | return ret; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 306 | |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 307 | ret = usbtv_select_norm(usbtv, usbtv->norm); |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 311 | ret = usbtv_select_input(usbtv, usbtv->input); |
| 312 | if (ret) |
| 313 | return ret; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 314 | |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 315 | ret = v4l2_ctrl_handler_setup(&usbtv->ctrl); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
Lubomir Rintel | 3080072 | 2013-07-02 07:56:38 -0300 | [diff] [blame] | 322 | /* Copy data from chunk into a frame buffer, deinterlacing the data |
| 323 | * into every second line. Unfortunately, they don't align nicely into |
| 324 | * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels. |
Lubomir Rintel | 8ed50eb | 2016-06-01 10:04:07 -0300 | [diff] [blame] | 325 | * Therefore, we break down the chunk into two halves before copying, |
| 326 | * so that we can interleave a line if needed. |
| 327 | * |
| 328 | * Each "chunk" is 240 words; a word in this context equals 4 bytes. |
| 329 | * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two |
| 330 | * pixels, the Cr and Cb shared between the two pixels, but each having |
| 331 | * separate Y values. Thus, the 240 words equal 480 pixels. It therefore, |
| 332 | * takes 1.5 chunks to make a 720 pixel-wide line for the frame. |
| 333 | * The image is interlaced, so there is a "scan" of odd lines, followed |
| 334 | * by "scan" of even numbered lines. |
| 335 | * |
| 336 | * Following code is writing the chunks in correct sequence, skipping |
| 337 | * the rows based on "odd" value. |
| 338 | * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479] |
| 339 | * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959] |
| 340 | * ...etc. |
| 341 | */ |
Hans Verkuil | 6fbf4d0 | 2014-08-20 18:03:53 -0300 | [diff] [blame] | 342 | static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd) |
Lubomir Rintel | 3080072 | 2013-07-02 07:56:38 -0300 | [diff] [blame] | 343 | { |
| 344 | int half; |
| 345 | |
| 346 | for (half = 0; half < 2; half++) { |
| 347 | int part_no = chunk_no * 2 + half; |
| 348 | int line = part_no / 3; |
| 349 | int part_index = (line * 2 + !odd) * 3 + (part_no % 3); |
| 350 | |
| 351 | u32 *dst = &frame[part_index * USBTV_CHUNK/2]; |
Amber Thrall | 146af9c | 2014-09-20 01:03:15 -0300 | [diff] [blame] | 352 | |
Lubomir Rintel | 3080072 | 2013-07-02 07:56:38 -0300 | [diff] [blame] | 353 | memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src)); |
| 354 | src += USBTV_CHUNK/2; |
| 355 | } |
| 356 | } |
| 357 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 358 | /* Called for each 256-byte image chunk. |
| 359 | * First word identifies the chunk, followed by 240 words of image |
| 360 | * data and padding. */ |
Hans Verkuil | 6fbf4d0 | 2014-08-20 18:03:53 -0300 | [diff] [blame] | 361 | static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk) |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 362 | { |
| 363 | int frame_id, odd, chunk_no; |
| 364 | u32 *frame; |
| 365 | struct usbtv_buf *buf; |
| 366 | unsigned long flags; |
| 367 | |
| 368 | /* Ignore corrupted lines. */ |
| 369 | if (!USBTV_MAGIC_OK(chunk)) |
| 370 | return; |
| 371 | frame_id = USBTV_FRAME_ID(chunk); |
| 372 | odd = USBTV_ODD(chunk); |
| 373 | chunk_no = USBTV_CHUNK_NO(chunk); |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 374 | if (chunk_no >= usbtv->n_chunks) |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 375 | return; |
| 376 | |
| 377 | /* Beginning of a frame. */ |
Lubomir Rintel | 6ee0faa | 2013-07-02 07:56:39 -0300 | [diff] [blame] | 378 | if (chunk_no == 0) { |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 379 | usbtv->frame_id = frame_id; |
Lubomir Rintel | 6ee0faa | 2013-07-02 07:56:39 -0300 | [diff] [blame] | 380 | usbtv->chunks_done = 0; |
| 381 | } |
| 382 | |
| 383 | if (usbtv->frame_id != frame_id) |
| 384 | return; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 385 | |
| 386 | spin_lock_irqsave(&usbtv->buflock, flags); |
| 387 | if (list_empty(&usbtv->bufs)) { |
| 388 | /* No free buffers. Userspace likely too slow. */ |
| 389 | spin_unlock_irqrestore(&usbtv->buflock, flags); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | /* First available buffer. */ |
| 394 | buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 395 | frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 396 | |
Lubomir Rintel | 3080072 | 2013-07-02 07:56:38 -0300 | [diff] [blame] | 397 | /* Copy the chunk data. */ |
| 398 | usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd); |
Lubomir Rintel | 6ee0faa | 2013-07-02 07:56:39 -0300 | [diff] [blame] | 399 | usbtv->chunks_done++; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 400 | |
Nikola Forró | 80fa4f0 | 2015-12-20 09:57:17 -0200 | [diff] [blame] | 401 | /* Last chunk in a field */ |
| 402 | if (chunk_no == usbtv->n_chunks-1) { |
| 403 | /* Last chunk in a frame, signalling an end */ |
| 404 | if (odd && !usbtv->last_odd) { |
| 405 | int size = vb2_plane_size(&buf->vb.vb2_buf, 0); |
| 406 | enum vb2_buffer_state state = usbtv->chunks_done == |
| 407 | usbtv->n_chunks ? |
| 408 | VB2_BUF_STATE_DONE : |
| 409 | VB2_BUF_STATE_ERROR; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 410 | |
Nikola Forró | 80fa4f0 | 2015-12-20 09:57:17 -0200 | [diff] [blame] | 411 | buf->vb.field = V4L2_FIELD_INTERLACED; |
| 412 | buf->vb.sequence = usbtv->sequence++; |
| 413 | buf->vb.vb2_buf.timestamp = ktime_get_ns(); |
| 414 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); |
| 415 | vb2_buffer_done(&buf->vb.vb2_buf, state); |
| 416 | list_del(&buf->list); |
| 417 | } |
| 418 | usbtv->last_odd = odd; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | spin_unlock_irqrestore(&usbtv->buflock, flags); |
| 422 | } |
| 423 | |
| 424 | /* Got image data. Each packet contains a number of 256-word chunks we |
| 425 | * compose the image from. */ |
| 426 | static void usbtv_iso_cb(struct urb *ip) |
| 427 | { |
| 428 | int ret; |
| 429 | int i; |
| 430 | struct usbtv *usbtv = (struct usbtv *)ip->context; |
| 431 | |
| 432 | switch (ip->status) { |
| 433 | /* All fine. */ |
| 434 | case 0: |
| 435 | break; |
| 436 | /* Device disconnected or capture stopped? */ |
| 437 | case -ENODEV: |
| 438 | case -ENOENT: |
| 439 | case -ECONNRESET: |
| 440 | case -ESHUTDOWN: |
| 441 | return; |
| 442 | /* Unknown error. Retry. */ |
| 443 | default: |
| 444 | dev_warn(usbtv->dev, "Bad response for ISO request.\n"); |
| 445 | goto resubmit; |
| 446 | } |
| 447 | |
| 448 | for (i = 0; i < ip->number_of_packets; i++) { |
| 449 | int size = ip->iso_frame_desc[i].actual_length; |
| 450 | unsigned char *data = ip->transfer_buffer + |
| 451 | ip->iso_frame_desc[i].offset; |
| 452 | int offset; |
| 453 | |
| 454 | for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++) |
| 455 | usbtv_image_chunk(usbtv, |
Hans Verkuil | 6fbf4d0 | 2014-08-20 18:03:53 -0300 | [diff] [blame] | 456 | (__be32 *)&data[USBTV_CHUNK_SIZE * offset]); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | resubmit: |
| 460 | ret = usb_submit_urb(ip, GFP_ATOMIC); |
| 461 | if (ret < 0) |
| 462 | dev_warn(usbtv->dev, "Could not resubmit ISO URB\n"); |
| 463 | } |
| 464 | |
| 465 | static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv) |
| 466 | { |
| 467 | struct urb *ip; |
| 468 | int size = usbtv->iso_size; |
| 469 | int i; |
| 470 | |
| 471 | ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL); |
| 472 | if (ip == NULL) |
| 473 | return NULL; |
| 474 | |
| 475 | ip->dev = usbtv->udev; |
| 476 | ip->context = usbtv; |
| 477 | ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP); |
| 478 | ip->interval = 1; |
| 479 | ip->transfer_flags = URB_ISO_ASAP; |
| 480 | ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS, |
| 481 | GFP_KERNEL); |
Insu Yun | e22a3b3 | 2015-12-29 19:48:29 -0200 | [diff] [blame] | 482 | if (!ip->transfer_buffer) { |
| 483 | usb_free_urb(ip); |
| 484 | return NULL; |
| 485 | } |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 486 | ip->complete = usbtv_iso_cb; |
| 487 | ip->number_of_packets = USBTV_ISOC_PACKETS; |
| 488 | ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS; |
| 489 | for (i = 0; i < USBTV_ISOC_PACKETS; i++) { |
| 490 | ip->iso_frame_desc[i].offset = size * i; |
| 491 | ip->iso_frame_desc[i].length = size; |
| 492 | } |
| 493 | |
| 494 | return ip; |
| 495 | } |
| 496 | |
| 497 | static void usbtv_stop(struct usbtv *usbtv) |
| 498 | { |
| 499 | int i; |
| 500 | unsigned long flags; |
| 501 | |
| 502 | /* Cancel running transfers. */ |
| 503 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { |
| 504 | struct urb *ip = usbtv->isoc_urbs[i]; |
Amber Thrall | 146af9c | 2014-09-20 01:03:15 -0300 | [diff] [blame] | 505 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 506 | if (ip == NULL) |
| 507 | continue; |
| 508 | usb_kill_urb(ip); |
| 509 | kfree(ip->transfer_buffer); |
| 510 | usb_free_urb(ip); |
| 511 | usbtv->isoc_urbs[i] = NULL; |
| 512 | } |
| 513 | |
| 514 | /* Return buffers to userspace. */ |
| 515 | spin_lock_irqsave(&usbtv->buflock, flags); |
| 516 | while (!list_empty(&usbtv->bufs)) { |
| 517 | struct usbtv_buf *buf = list_first_entry(&usbtv->bufs, |
| 518 | struct usbtv_buf, list); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 519 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 520 | list_del(&buf->list); |
| 521 | } |
| 522 | spin_unlock_irqrestore(&usbtv->buflock, flags); |
| 523 | } |
| 524 | |
| 525 | static int usbtv_start(struct usbtv *usbtv) |
| 526 | { |
| 527 | int i; |
| 528 | int ret; |
| 529 | |
Federico Simoncelli | 63ddf68 | 2014-08-11 18:42:22 -0300 | [diff] [blame] | 530 | usbtv_audio_suspend(usbtv); |
| 531 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 532 | ret = usb_set_interface(usbtv->udev, 0, 0); |
| 533 | if (ret < 0) |
| 534 | return ret; |
| 535 | |
| 536 | ret = usbtv_setup_capture(usbtv); |
| 537 | if (ret < 0) |
| 538 | return ret; |
| 539 | |
| 540 | ret = usb_set_interface(usbtv->udev, 0, 1); |
| 541 | if (ret < 0) |
| 542 | return ret; |
| 543 | |
Federico Simoncelli | 63ddf68 | 2014-08-11 18:42:22 -0300 | [diff] [blame] | 544 | usbtv_audio_resume(usbtv); |
| 545 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 546 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { |
| 547 | struct urb *ip; |
| 548 | |
| 549 | ip = usbtv_setup_iso_transfer(usbtv); |
| 550 | if (ip == NULL) { |
| 551 | ret = -ENOMEM; |
| 552 | goto start_fail; |
| 553 | } |
| 554 | usbtv->isoc_urbs[i] = ip; |
| 555 | |
| 556 | ret = usb_submit_urb(ip, GFP_KERNEL); |
| 557 | if (ret < 0) |
| 558 | goto start_fail; |
| 559 | } |
| 560 | |
| 561 | return 0; |
| 562 | |
| 563 | start_fail: |
| 564 | usbtv_stop(usbtv); |
| 565 | return ret; |
| 566 | } |
| 567 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 568 | static int usbtv_querycap(struct file *file, void *priv, |
| 569 | struct v4l2_capability *cap) |
| 570 | { |
| 571 | struct usbtv *dev = video_drvdata(file); |
| 572 | |
| 573 | strlcpy(cap->driver, "usbtv", sizeof(cap->driver)); |
| 574 | strlcpy(cap->card, "usbtv", sizeof(cap->card)); |
| 575 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
| 576 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE; |
| 577 | cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 578 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static int usbtv_enum_input(struct file *file, void *priv, |
| 583 | struct v4l2_input *i) |
| 584 | { |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 585 | struct usbtv *dev = video_drvdata(file); |
| 586 | |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 587 | switch (i->index) { |
| 588 | case USBTV_COMPOSITE_INPUT: |
| 589 | strlcpy(i->name, "Composite", sizeof(i->name)); |
| 590 | break; |
| 591 | case USBTV_SVIDEO_INPUT: |
| 592 | strlcpy(i->name, "S-Video", sizeof(i->name)); |
| 593 | break; |
| 594 | default: |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 595 | return -EINVAL; |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 596 | } |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 597 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 598 | i->type = V4L2_INPUT_TYPE_CAMERA; |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 599 | i->std = dev->vdev.tvnorms; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, |
| 604 | struct v4l2_fmtdesc *f) |
| 605 | { |
| 606 | if (f->index > 0) |
| 607 | return -EINVAL; |
| 608 | |
| 609 | strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed", |
| 610 | sizeof(f->description)); |
| 611 | f->pixelformat = V4L2_PIX_FMT_YUYV; |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int usbtv_fmt_vid_cap(struct file *file, void *priv, |
| 616 | struct v4l2_format *f) |
| 617 | { |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 618 | struct usbtv *usbtv = video_drvdata(file); |
| 619 | |
| 620 | f->fmt.pix.width = usbtv->width; |
| 621 | f->fmt.pix.height = usbtv->height; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 622 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 623 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 624 | f->fmt.pix.bytesperline = usbtv->width * 2; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 625 | f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height); |
| 626 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 627 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm) |
| 632 | { |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 633 | struct usbtv *usbtv = video_drvdata(file); |
| 634 | *norm = usbtv->norm; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 638 | static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm) |
| 639 | { |
| 640 | int ret = -EINVAL; |
| 641 | struct usbtv *usbtv = video_drvdata(file); |
| 642 | |
Hugo Grostabussiat | c41e20d | 2018-04-08 17:11:57 -0400 | [diff] [blame^] | 643 | if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL) || |
| 644 | (norm & V4L2_STD_SECAM)) |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 645 | ret = usbtv_select_norm(usbtv, norm); |
| 646 | |
| 647 | return ret; |
| 648 | } |
| 649 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 650 | static int usbtv_g_input(struct file *file, void *priv, unsigned int *i) |
| 651 | { |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 652 | struct usbtv *usbtv = video_drvdata(file); |
| 653 | *i = usbtv->input; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int usbtv_s_input(struct file *file, void *priv, unsigned int i) |
| 658 | { |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 659 | struct usbtv *usbtv = video_drvdata(file); |
Amber Thrall | 146af9c | 2014-09-20 01:03:15 -0300 | [diff] [blame] | 660 | |
Lubomir Rintel | 2206112 | 2013-07-12 05:03:03 -0300 | [diff] [blame] | 661 | return usbtv_select_input(usbtv, i); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 662 | } |
| 663 | |
Fengguang Wu | 9b05837 | 2014-02-04 06:02:02 -0300 | [diff] [blame] | 664 | static struct v4l2_ioctl_ops usbtv_ioctl_ops = { |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 665 | .vidioc_querycap = usbtv_querycap, |
| 666 | .vidioc_enum_input = usbtv_enum_input, |
| 667 | .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap, |
| 668 | .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap, |
| 669 | .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap, |
| 670 | .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap, |
| 671 | .vidioc_g_std = usbtv_g_std, |
| 672 | .vidioc_s_std = usbtv_s_std, |
| 673 | .vidioc_g_input = usbtv_g_input, |
| 674 | .vidioc_s_input = usbtv_s_input, |
| 675 | |
| 676 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
| 677 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, |
| 678 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 679 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
| 680 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 681 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
| 682 | .vidioc_streamon = vb2_ioctl_streamon, |
| 683 | .vidioc_streamoff = vb2_ioctl_streamoff, |
| 684 | }; |
| 685 | |
Bhumika Goyal | fe9619a | 2017-08-26 02:13:46 -0400 | [diff] [blame] | 686 | static const struct v4l2_file_operations usbtv_fops = { |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 687 | .owner = THIS_MODULE, |
| 688 | .unlocked_ioctl = video_ioctl2, |
| 689 | .mmap = vb2_fop_mmap, |
| 690 | .open = v4l2_fh_open, |
| 691 | .release = vb2_fop_release, |
| 692 | .read = vb2_fop_read, |
| 693 | .poll = vb2_fop_poll, |
| 694 | }; |
| 695 | |
| 696 | static int usbtv_queue_setup(struct vb2_queue *vq, |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 697 | unsigned int *nbuffers, |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 698 | unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 699 | { |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 700 | struct usbtv *usbtv = vb2_get_drv_priv(vq); |
Hans Verkuil | a988410 | 2015-04-24 04:26:01 -0300 | [diff] [blame] | 701 | unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32); |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 702 | |
Hans Verkuil | a988410 | 2015-04-24 04:26:01 -0300 | [diff] [blame] | 703 | if (vq->num_buffers + *nbuffers < 2) |
| 704 | *nbuffers = 2 - vq->num_buffers; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 705 | if (*nplanes) |
| 706 | return sizes[0] < size ? -EINVAL : 0; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 707 | *nplanes = 1; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 708 | sizes[0] = size; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | static void usbtv_buf_queue(struct vb2_buffer *vb) |
| 714 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 715 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 716 | struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 717 | struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 718 | unsigned long flags; |
| 719 | |
| 720 | if (usbtv->udev == NULL) { |
| 721 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | spin_lock_irqsave(&usbtv->buflock, flags); |
| 726 | list_add_tail(&buf->list, &usbtv->bufs); |
| 727 | spin_unlock_irqrestore(&usbtv->buflock, flags); |
| 728 | } |
| 729 | |
| 730 | static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count) |
| 731 | { |
| 732 | struct usbtv *usbtv = vb2_get_drv_priv(vq); |
| 733 | |
| 734 | if (usbtv->udev == NULL) |
| 735 | return -ENODEV; |
| 736 | |
Nikola Forró | 80fa4f0 | 2015-12-20 09:57:17 -0200 | [diff] [blame] | 737 | usbtv->last_odd = 1; |
Hans Verkuil | a988410 | 2015-04-24 04:26:01 -0300 | [diff] [blame] | 738 | usbtv->sequence = 0; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 739 | return usbtv_start(usbtv); |
| 740 | } |
| 741 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 742 | static void usbtv_stop_streaming(struct vb2_queue *vq) |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 743 | { |
| 744 | struct usbtv *usbtv = vb2_get_drv_priv(vq); |
| 745 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 746 | if (usbtv->udev) |
| 747 | usbtv_stop(usbtv); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 748 | } |
| 749 | |
Julia Lawall | 1bc1771 | 2016-09-08 20:59:01 -0300 | [diff] [blame] | 750 | static const struct vb2_ops usbtv_vb2_ops = { |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 751 | .queue_setup = usbtv_queue_setup, |
| 752 | .buf_queue = usbtv_buf_queue, |
| 753 | .start_streaming = usbtv_start_streaming, |
| 754 | .stop_streaming = usbtv_stop_streaming, |
| 755 | }; |
| 756 | |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 757 | static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl) |
| 758 | { |
| 759 | struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv, |
| 760 | ctrl); |
Mauro Carvalho Chehab | 62de7d99 | 2016-11-16 13:13:02 -0200 | [diff] [blame] | 761 | u8 *data; |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 762 | u16 index, size; |
| 763 | int ret; |
| 764 | |
Mauro Carvalho Chehab | 62de7d99 | 2016-11-16 13:13:02 -0200 | [diff] [blame] | 765 | data = kmalloc(3, GFP_KERNEL); |
| 766 | if (!data) |
| 767 | return -ENOMEM; |
| 768 | |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 769 | /* |
| 770 | * Read in the current brightness/contrast registers. We need them |
| 771 | * both, because the values are for some reason interleaved. |
| 772 | */ |
| 773 | if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) { |
| 774 | ret = usb_control_msg(usbtv->udev, |
Adam Sampson | b3168c8 | 2017-10-24 16:14:46 -0400 | [diff] [blame] | 775 | usb_rcvctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG, |
| 776 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 777 | 0, USBTV_BASE + 0x0244, (void *)data, 3, 0); |
Mauro Carvalho Chehab | 62de7d99 | 2016-11-16 13:13:02 -0200 | [diff] [blame] | 778 | if (ret < 0) |
| 779 | goto error; |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | switch (ctrl->id) { |
| 783 | case V4L2_CID_BRIGHTNESS: |
| 784 | index = USBTV_BASE + 0x0244; |
| 785 | size = 3; |
| 786 | data[0] &= 0xf0; |
| 787 | data[0] |= (ctrl->val >> 8) & 0xf; |
| 788 | data[2] = ctrl->val & 0xff; |
| 789 | break; |
| 790 | case V4L2_CID_CONTRAST: |
| 791 | index = USBTV_BASE + 0x0244; |
| 792 | size = 3; |
| 793 | data[0] &= 0x0f; |
| 794 | data[0] |= (ctrl->val >> 4) & 0xf0; |
| 795 | data[1] = ctrl->val & 0xff; |
| 796 | break; |
| 797 | case V4L2_CID_SATURATION: |
| 798 | index = USBTV_BASE + 0x0242; |
| 799 | data[0] = ctrl->val >> 8; |
| 800 | data[1] = ctrl->val & 0xff; |
| 801 | size = 2; |
| 802 | break; |
| 803 | case V4L2_CID_HUE: |
| 804 | index = USBTV_BASE + 0x0240; |
| 805 | size = 2; |
| 806 | if (ctrl->val > 0) { |
| 807 | data[0] = 0x92 + (ctrl->val >> 8); |
| 808 | data[1] = ctrl->val & 0xff; |
| 809 | } else { |
| 810 | data[0] = 0x82 + (-ctrl->val >> 8); |
| 811 | data[1] = -ctrl->val & 0xff; |
| 812 | } |
| 813 | break; |
Lubomir Rintel | 9165ba1 | 2017-02-06 04:47:36 -0200 | [diff] [blame] | 814 | case V4L2_CID_SHARPNESS: |
| 815 | index = USBTV_BASE + 0x0239; |
| 816 | data[0] = 0; |
| 817 | data[1] = ctrl->val; |
| 818 | size = 2; |
| 819 | break; |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 820 | default: |
Mauro Carvalho Chehab | 62de7d99 | 2016-11-16 13:13:02 -0200 | [diff] [blame] | 821 | kfree(data); |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 822 | return -EINVAL; |
| 823 | } |
| 824 | |
| 825 | ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0), |
| 826 | USBTV_CONTROL_REG, |
| 827 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 828 | 0, index, (void *)data, size, 0); |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 829 | |
Mauro Carvalho Chehab | 62de7d99 | 2016-11-16 13:13:02 -0200 | [diff] [blame] | 830 | error: |
| 831 | if (ret < 0) |
| 832 | dev_warn(usbtv->dev, "Failed to submit a control request.\n"); |
| 833 | |
| 834 | kfree(data); |
| 835 | return ret; |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static const struct v4l2_ctrl_ops usbtv_ctrl_ops = { |
| 839 | .s_ctrl = usbtv_s_ctrl, |
| 840 | }; |
| 841 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 842 | static void usbtv_release(struct v4l2_device *v4l2_dev) |
| 843 | { |
| 844 | struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev); |
| 845 | |
| 846 | v4l2_device_unregister(&usbtv->v4l2_dev); |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 847 | v4l2_ctrl_handler_free(&usbtv->ctrl); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 848 | vb2_queue_release(&usbtv->vb2q); |
| 849 | kfree(usbtv); |
| 850 | } |
| 851 | |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 852 | int usbtv_video_init(struct usbtv *usbtv) |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 853 | { |
| 854 | int ret; |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 855 | |
| 856 | (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60); |
| 857 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 858 | spin_lock_init(&usbtv->buflock); |
| 859 | mutex_init(&usbtv->v4l2_lock); |
| 860 | mutex_init(&usbtv->vb2q_lock); |
| 861 | INIT_LIST_HEAD(&usbtv->bufs); |
| 862 | |
| 863 | /* videobuf2 structure */ |
| 864 | usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 865 | usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; |
| 866 | usbtv->vb2q.drv_priv = usbtv; |
| 867 | usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf); |
| 868 | usbtv->vb2q.ops = &usbtv_vb2_ops; |
| 869 | usbtv->vb2q.mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 870 | usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 871 | usbtv->vb2q.lock = &usbtv->vb2q_lock; |
| 872 | ret = vb2_queue_init(&usbtv->vb2q); |
| 873 | if (ret < 0) { |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 874 | dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n"); |
| 875 | return ret; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 876 | } |
| 877 | |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 878 | /* controls */ |
| 879 | v4l2_ctrl_handler_init(&usbtv->ctrl, 4); |
| 880 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, |
| 881 | V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0); |
| 882 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, |
| 883 | V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0); |
| 884 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, |
| 885 | V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200); |
| 886 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, |
| 887 | V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000); |
Lubomir Rintel | 9165ba1 | 2017-02-06 04:47:36 -0200 | [diff] [blame] | 888 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, |
| 889 | V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60); |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 890 | ret = usbtv->ctrl.error; |
| 891 | if (ret < 0) { |
| 892 | dev_warn(usbtv->dev, "Could not initialize controls\n"); |
| 893 | goto ctrl_fail; |
| 894 | } |
| 895 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 896 | /* v4l2 structure */ |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 897 | usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 898 | usbtv->v4l2_dev.release = usbtv_release; |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 899 | ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 900 | if (ret < 0) { |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 901 | dev_warn(usbtv->dev, "Could not register v4l2 device\n"); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 902 | goto v4l2_fail; |
| 903 | } |
| 904 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 905 | /* Video structure */ |
| 906 | strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name)); |
| 907 | usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev; |
| 908 | usbtv->vdev.release = video_device_release_empty; |
| 909 | usbtv->vdev.fops = &usbtv_fops; |
| 910 | usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops; |
Georg Kaindl | 0e0fe39 | 2013-10-21 12:01:36 -0300 | [diff] [blame] | 911 | usbtv->vdev.tvnorms = USBTV_TV_STD; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 912 | usbtv->vdev.queue = &usbtv->vb2q; |
| 913 | usbtv->vdev.lock = &usbtv->v4l2_lock; |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 914 | video_set_drvdata(&usbtv->vdev, usbtv); |
| 915 | ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1); |
| 916 | if (ret < 0) { |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 917 | dev_warn(usbtv->dev, "Could not register video device\n"); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 918 | goto vdev_fail; |
| 919 | } |
| 920 | |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 921 | return 0; |
| 922 | |
| 923 | vdev_fail: |
| 924 | v4l2_device_unregister(&usbtv->v4l2_dev); |
| 925 | v4l2_fail: |
Lubomir Rintel | c53a846 | 2016-10-16 07:38:22 -0200 | [diff] [blame] | 926 | ctrl_fail: |
| 927 | v4l2_ctrl_handler_free(&usbtv->ctrl); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 928 | vb2_queue_release(&usbtv->vb2q); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
Federico Simoncelli | a3550ea | 2014-01-07 19:13:21 -0300 | [diff] [blame] | 933 | void usbtv_video_free(struct usbtv *usbtv) |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 934 | { |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 935 | mutex_lock(&usbtv->vb2q_lock); |
| 936 | mutex_lock(&usbtv->v4l2_lock); |
| 937 | |
| 938 | usbtv_stop(usbtv); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 939 | video_unregister_device(&usbtv->vdev); |
| 940 | v4l2_device_disconnect(&usbtv->v4l2_dev); |
Lubomir Rintel | f3d27f3 | 2013-06-17 10:29:06 -0300 | [diff] [blame] | 941 | |
| 942 | mutex_unlock(&usbtv->v4l2_lock); |
| 943 | mutex_unlock(&usbtv->vb2q_lock); |
| 944 | |
| 945 | v4l2_device_put(&usbtv->v4l2_dev); |
| 946 | } |