]>
Commit | Line | Data |
---|---|---|
f3d27f34 | 1 | /* |
c53a846c | 2 | * Copyright (c) 2013,2016 Lubomir Rintel |
f3d27f34 | 3 | * All rights reserved. |
f3d27f34 LR |
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"). | |
5ae1e2b2 LR |
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 | |
f3d27f34 LR |
43 | */ |
44 | ||
f3d27f34 | 45 | #include <media/v4l2-ioctl.h> |
c139990e | 46 | #include <media/videobuf2-v4l2.h> |
a3550ea6 FS |
47 | |
48 | #include "usbtv.h" | |
0e0fe395 GK |
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, | |
60 | } | |
61 | }; | |
62 | ||
0e0fe395 GK |
63 | static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm) |
64 | { | |
65 | int i, ret = 0; | |
66 | struct usbtv_norm_params *params = NULL; | |
67 | ||
68 | for (i = 0; i < ARRAY_SIZE(norm_params); i++) { | |
69 | if (norm_params[i].norm & norm) { | |
70 | params = &norm_params[i]; | |
71 | break; | |
72 | } | |
73 | } | |
74 | ||
75 | if (params) { | |
76 | usbtv->width = params->cap_width; | |
77 | usbtv->height = params->cap_height; | |
78 | usbtv->n_chunks = usbtv->width * usbtv->height | |
79 | / 4 / USBTV_CHUNK; | |
80 | usbtv->norm = params->norm; | |
81 | } else | |
82 | ret = -EINVAL; | |
83 | ||
84 | return ret; | |
85 | } | |
86 | ||
2206112b LR |
87 | static int usbtv_select_input(struct usbtv *usbtv, int input) |
88 | { | |
89 | int ret; | |
90 | ||
91 | static const u16 composite[][2] = { | |
92 | { USBTV_BASE + 0x0105, 0x0060 }, | |
93 | { USBTV_BASE + 0x011f, 0x00f2 }, | |
94 | { USBTV_BASE + 0x0127, 0x0060 }, | |
95 | { USBTV_BASE + 0x00ae, 0x0010 }, | |
2206112b LR |
96 | { USBTV_BASE + 0x0239, 0x0060 }, |
97 | }; | |
98 | ||
99 | static const u16 svideo[][2] = { | |
100 | { USBTV_BASE + 0x0105, 0x0010 }, | |
101 | { USBTV_BASE + 0x011f, 0x00ff }, | |
102 | { USBTV_BASE + 0x0127, 0x0060 }, | |
103 | { USBTV_BASE + 0x00ae, 0x0030 }, | |
2206112b LR |
104 | { USBTV_BASE + 0x0239, 0x0060 }, |
105 | }; | |
106 | ||
107 | switch (input) { | |
108 | case USBTV_COMPOSITE_INPUT: | |
109 | ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite)); | |
110 | break; | |
111 | case USBTV_SVIDEO_INPUT: | |
112 | ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo)); | |
113 | break; | |
114 | default: | |
115 | ret = -EINVAL; | |
116 | } | |
117 | ||
118 | if (!ret) | |
119 | usbtv->input = input; | |
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
0e0fe395 GK |
124 | static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm) |
125 | { | |
126 | int ret; | |
127 | static const u16 pal[][2] = { | |
128 | { USBTV_BASE + 0x001a, 0x0068 }, | |
129 | { USBTV_BASE + 0x010e, 0x0072 }, | |
130 | { USBTV_BASE + 0x010f, 0x00a2 }, | |
131 | { USBTV_BASE + 0x0112, 0x00b0 }, | |
132 | { USBTV_BASE + 0x0117, 0x0001 }, | |
133 | { USBTV_BASE + 0x0118, 0x002c }, | |
134 | { USBTV_BASE + 0x012d, 0x0010 }, | |
135 | { USBTV_BASE + 0x012f, 0x0020 }, | |
136 | { USBTV_BASE + 0x024f, 0x0002 }, | |
137 | { USBTV_BASE + 0x0254, 0x0059 }, | |
138 | { USBTV_BASE + 0x025a, 0x0016 }, | |
139 | { USBTV_BASE + 0x025b, 0x0035 }, | |
140 | { USBTV_BASE + 0x0263, 0x0017 }, | |
141 | { USBTV_BASE + 0x0266, 0x0016 }, | |
142 | { USBTV_BASE + 0x0267, 0x0036 } | |
143 | }; | |
144 | ||
145 | static const u16 ntsc[][2] = { | |
146 | { USBTV_BASE + 0x001a, 0x0079 }, | |
147 | { USBTV_BASE + 0x010e, 0x0068 }, | |
148 | { USBTV_BASE + 0x010f, 0x009c }, | |
149 | { USBTV_BASE + 0x0112, 0x00f0 }, | |
150 | { USBTV_BASE + 0x0117, 0x0000 }, | |
151 | { USBTV_BASE + 0x0118, 0x00fc }, | |
152 | { USBTV_BASE + 0x012d, 0x0004 }, | |
153 | { USBTV_BASE + 0x012f, 0x0008 }, | |
154 | { USBTV_BASE + 0x024f, 0x0001 }, | |
155 | { USBTV_BASE + 0x0254, 0x005f }, | |
156 | { USBTV_BASE + 0x025a, 0x0012 }, | |
157 | { USBTV_BASE + 0x025b, 0x0001 }, | |
158 | { USBTV_BASE + 0x0263, 0x001c }, | |
159 | { USBTV_BASE + 0x0266, 0x0011 }, | |
160 | { USBTV_BASE + 0x0267, 0x0005 } | |
161 | }; | |
162 | ||
163 | ret = usbtv_configure_for_norm(usbtv, norm); | |
164 | ||
165 | if (!ret) { | |
166 | if (norm & V4L2_STD_525_60) | |
167 | ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc)); | |
168 | else if (norm & V4L2_STD_PAL) | |
169 | ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal)); | |
170 | } | |
171 | ||
172 | return ret; | |
173 | } | |
174 | ||
2206112b LR |
175 | static int usbtv_setup_capture(struct usbtv *usbtv) |
176 | { | |
177 | int ret; | |
178 | static const u16 setup[][2] = { | |
f3d27f34 LR |
179 | /* These seem to enable the device. */ |
180 | { USBTV_BASE + 0x0008, 0x0001 }, | |
181 | { USBTV_BASE + 0x01d0, 0x00ff }, | |
182 | { USBTV_BASE + 0x01d9, 0x0002 }, | |
183 | ||
184 | /* These seem to influence color parameters, such as | |
185 | * brightness, etc. */ | |
186 | { USBTV_BASE + 0x0239, 0x0040 }, | |
187 | { USBTV_BASE + 0x0240, 0x0000 }, | |
188 | { USBTV_BASE + 0x0241, 0x0000 }, | |
189 | { USBTV_BASE + 0x0242, 0x0002 }, | |
190 | { USBTV_BASE + 0x0243, 0x0080 }, | |
191 | { USBTV_BASE + 0x0244, 0x0012 }, | |
192 | { USBTV_BASE + 0x0245, 0x0090 }, | |
193 | { USBTV_BASE + 0x0246, 0x0000 }, | |
194 | ||
195 | { USBTV_BASE + 0x0278, 0x002d }, | |
196 | { USBTV_BASE + 0x0279, 0x000a }, | |
197 | { USBTV_BASE + 0x027a, 0x0032 }, | |
198 | { 0xf890, 0x000c }, | |
199 | { 0xf894, 0x0086 }, | |
200 | ||
201 | { USBTV_BASE + 0x00ac, 0x00c0 }, | |
202 | { USBTV_BASE + 0x00ad, 0x0000 }, | |
203 | { USBTV_BASE + 0x00a2, 0x0012 }, | |
204 | { USBTV_BASE + 0x00a3, 0x00e0 }, | |
205 | { USBTV_BASE + 0x00a4, 0x0028 }, | |
206 | { USBTV_BASE + 0x00a5, 0x0082 }, | |
207 | { USBTV_BASE + 0x00a7, 0x0080 }, | |
208 | { USBTV_BASE + 0x0000, 0x0014 }, | |
209 | { USBTV_BASE + 0x0006, 0x0003 }, | |
210 | { USBTV_BASE + 0x0090, 0x0099 }, | |
211 | { USBTV_BASE + 0x0091, 0x0090 }, | |
212 | { USBTV_BASE + 0x0094, 0x0068 }, | |
213 | { USBTV_BASE + 0x0095, 0x0070 }, | |
214 | { USBTV_BASE + 0x009c, 0x0030 }, | |
215 | { USBTV_BASE + 0x009d, 0x00c0 }, | |
216 | { USBTV_BASE + 0x009e, 0x00e0 }, | |
217 | { USBTV_BASE + 0x0019, 0x0006 }, | |
218 | { USBTV_BASE + 0x008c, 0x00ba }, | |
219 | { USBTV_BASE + 0x0101, 0x00ff }, | |
220 | { USBTV_BASE + 0x010c, 0x00b3 }, | |
221 | { USBTV_BASE + 0x01b2, 0x0080 }, | |
222 | { USBTV_BASE + 0x01b4, 0x00a0 }, | |
223 | { USBTV_BASE + 0x014c, 0x00ff }, | |
224 | { USBTV_BASE + 0x014d, 0x00ca }, | |
225 | { USBTV_BASE + 0x0113, 0x0053 }, | |
226 | { USBTV_BASE + 0x0119, 0x008a }, | |
227 | { USBTV_BASE + 0x013c, 0x0003 }, | |
228 | { USBTV_BASE + 0x0150, 0x009c }, | |
229 | { USBTV_BASE + 0x0151, 0x0071 }, | |
230 | { USBTV_BASE + 0x0152, 0x00c6 }, | |
231 | { USBTV_BASE + 0x0153, 0x0084 }, | |
232 | { USBTV_BASE + 0x0154, 0x00bc }, | |
233 | { USBTV_BASE + 0x0155, 0x00a0 }, | |
234 | { USBTV_BASE + 0x0156, 0x00a0 }, | |
235 | { USBTV_BASE + 0x0157, 0x009c }, | |
236 | { USBTV_BASE + 0x0158, 0x001f }, | |
237 | { USBTV_BASE + 0x0159, 0x0006 }, | |
238 | { USBTV_BASE + 0x015d, 0x0000 }, | |
239 | ||
f3d27f34 | 240 | { USBTV_BASE + 0x0003, 0x0004 }, |
f3d27f34 | 241 | { USBTV_BASE + 0x0100, 0x00d3 }, |
f3d27f34 | 242 | { USBTV_BASE + 0x0115, 0x0015 }, |
f3d27f34 LR |
243 | { USBTV_BASE + 0x0220, 0x002e }, |
244 | { USBTV_BASE + 0x0225, 0x0008 }, | |
245 | { USBTV_BASE + 0x024e, 0x0002 }, | |
f3d27f34 LR |
246 | { USBTV_BASE + 0x024e, 0x0002 }, |
247 | { USBTV_BASE + 0x024f, 0x0002 }, | |
248 | }; | |
249 | ||
2206112b LR |
250 | ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup)); |
251 | if (ret) | |
252 | return ret; | |
f3d27f34 | 253 | |
0e0fe395 GK |
254 | ret = usbtv_select_norm(usbtv, usbtv->norm); |
255 | if (ret) | |
256 | return ret; | |
257 | ||
2206112b LR |
258 | ret = usbtv_select_input(usbtv, usbtv->input); |
259 | if (ret) | |
260 | return ret; | |
f3d27f34 | 261 | |
c53a846c LR |
262 | ret = v4l2_ctrl_handler_setup(&usbtv->ctrl); |
263 | if (ret) | |
264 | return ret; | |
265 | ||
f3d27f34 LR |
266 | return 0; |
267 | } | |
268 | ||
30800724 LR |
269 | /* Copy data from chunk into a frame buffer, deinterlacing the data |
270 | * into every second line. Unfortunately, they don't align nicely into | |
271 | * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels. | |
8ed50eb4 LR |
272 | * Therefore, we break down the chunk into two halves before copying, |
273 | * so that we can interleave a line if needed. | |
274 | * | |
275 | * Each "chunk" is 240 words; a word in this context equals 4 bytes. | |
276 | * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two | |
277 | * pixels, the Cr and Cb shared between the two pixels, but each having | |
278 | * separate Y values. Thus, the 240 words equal 480 pixels. It therefore, | |
279 | * takes 1.5 chunks to make a 720 pixel-wide line for the frame. | |
280 | * The image is interlaced, so there is a "scan" of odd lines, followed | |
281 | * by "scan" of even numbered lines. | |
282 | * | |
283 | * Following code is writing the chunks in correct sequence, skipping | |
284 | * the rows based on "odd" value. | |
285 | * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479] | |
286 | * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959] | |
287 | * ...etc. | |
288 | */ | |
6fbf4d04 | 289 | static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd) |
30800724 LR |
290 | { |
291 | int half; | |
292 | ||
293 | for (half = 0; half < 2; half++) { | |
294 | int part_no = chunk_no * 2 + half; | |
295 | int line = part_no / 3; | |
296 | int part_index = (line * 2 + !odd) * 3 + (part_no % 3); | |
297 | ||
298 | u32 *dst = &frame[part_index * USBTV_CHUNK/2]; | |
146af9cb | 299 | |
30800724 LR |
300 | memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src)); |
301 | src += USBTV_CHUNK/2; | |
302 | } | |
303 | } | |
304 | ||
f3d27f34 LR |
305 | /* Called for each 256-byte image chunk. |
306 | * First word identifies the chunk, followed by 240 words of image | |
307 | * data and padding. */ | |
6fbf4d04 | 308 | static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk) |
f3d27f34 LR |
309 | { |
310 | int frame_id, odd, chunk_no; | |
311 | u32 *frame; | |
312 | struct usbtv_buf *buf; | |
313 | unsigned long flags; | |
314 | ||
315 | /* Ignore corrupted lines. */ | |
316 | if (!USBTV_MAGIC_OK(chunk)) | |
317 | return; | |
318 | frame_id = USBTV_FRAME_ID(chunk); | |
319 | odd = USBTV_ODD(chunk); | |
320 | chunk_no = USBTV_CHUNK_NO(chunk); | |
0e0fe395 | 321 | if (chunk_no >= usbtv->n_chunks) |
f3d27f34 LR |
322 | return; |
323 | ||
324 | /* Beginning of a frame. */ | |
6ee0faa5 | 325 | if (chunk_no == 0) { |
f3d27f34 | 326 | usbtv->frame_id = frame_id; |
6ee0faa5 LR |
327 | usbtv->chunks_done = 0; |
328 | } | |
329 | ||
330 | if (usbtv->frame_id != frame_id) | |
331 | return; | |
f3d27f34 LR |
332 | |
333 | spin_lock_irqsave(&usbtv->buflock, flags); | |
334 | if (list_empty(&usbtv->bufs)) { | |
335 | /* No free buffers. Userspace likely too slow. */ | |
336 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
337 | return; | |
338 | } | |
339 | ||
340 | /* First available buffer. */ | |
341 | buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list); | |
2d700715 | 342 | frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); |
f3d27f34 | 343 | |
30800724 LR |
344 | /* Copy the chunk data. */ |
345 | usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd); | |
6ee0faa5 | 346 | usbtv->chunks_done++; |
f3d27f34 | 347 | |
80fa4f07 NF |
348 | /* Last chunk in a field */ |
349 | if (chunk_no == usbtv->n_chunks-1) { | |
350 | /* Last chunk in a frame, signalling an end */ | |
351 | if (odd && !usbtv->last_odd) { | |
352 | int size = vb2_plane_size(&buf->vb.vb2_buf, 0); | |
353 | enum vb2_buffer_state state = usbtv->chunks_done == | |
354 | usbtv->n_chunks ? | |
355 | VB2_BUF_STATE_DONE : | |
356 | VB2_BUF_STATE_ERROR; | |
357 | ||
358 | buf->vb.field = V4L2_FIELD_INTERLACED; | |
359 | buf->vb.sequence = usbtv->sequence++; | |
360 | buf->vb.vb2_buf.timestamp = ktime_get_ns(); | |
361 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); | |
362 | vb2_buffer_done(&buf->vb.vb2_buf, state); | |
363 | list_del(&buf->list); | |
364 | } | |
365 | usbtv->last_odd = odd; | |
f3d27f34 LR |
366 | } |
367 | ||
368 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
369 | } | |
370 | ||
371 | /* Got image data. Each packet contains a number of 256-word chunks we | |
372 | * compose the image from. */ | |
373 | static void usbtv_iso_cb(struct urb *ip) | |
374 | { | |
375 | int ret; | |
376 | int i; | |
377 | struct usbtv *usbtv = (struct usbtv *)ip->context; | |
378 | ||
379 | switch (ip->status) { | |
380 | /* All fine. */ | |
381 | case 0: | |
382 | break; | |
383 | /* Device disconnected or capture stopped? */ | |
384 | case -ENODEV: | |
385 | case -ENOENT: | |
386 | case -ECONNRESET: | |
387 | case -ESHUTDOWN: | |
388 | return; | |
389 | /* Unknown error. Retry. */ | |
390 | default: | |
391 | dev_warn(usbtv->dev, "Bad response for ISO request.\n"); | |
392 | goto resubmit; | |
393 | } | |
394 | ||
395 | for (i = 0; i < ip->number_of_packets; i++) { | |
396 | int size = ip->iso_frame_desc[i].actual_length; | |
397 | unsigned char *data = ip->transfer_buffer + | |
398 | ip->iso_frame_desc[i].offset; | |
399 | int offset; | |
400 | ||
401 | for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++) | |
402 | usbtv_image_chunk(usbtv, | |
6fbf4d04 | 403 | (__be32 *)&data[USBTV_CHUNK_SIZE * offset]); |
f3d27f34 LR |
404 | } |
405 | ||
406 | resubmit: | |
407 | ret = usb_submit_urb(ip, GFP_ATOMIC); | |
408 | if (ret < 0) | |
409 | dev_warn(usbtv->dev, "Could not resubmit ISO URB\n"); | |
410 | } | |
411 | ||
412 | static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv) | |
413 | { | |
414 | struct urb *ip; | |
415 | int size = usbtv->iso_size; | |
416 | int i; | |
417 | ||
418 | ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL); | |
419 | if (ip == NULL) | |
420 | return NULL; | |
421 | ||
422 | ip->dev = usbtv->udev; | |
423 | ip->context = usbtv; | |
424 | ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP); | |
425 | ip->interval = 1; | |
426 | ip->transfer_flags = URB_ISO_ASAP; | |
427 | ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS, | |
428 | GFP_KERNEL); | |
e22a3b34 IY |
429 | if (!ip->transfer_buffer) { |
430 | usb_free_urb(ip); | |
431 | return NULL; | |
432 | } | |
f3d27f34 LR |
433 | ip->complete = usbtv_iso_cb; |
434 | ip->number_of_packets = USBTV_ISOC_PACKETS; | |
435 | ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS; | |
436 | for (i = 0; i < USBTV_ISOC_PACKETS; i++) { | |
437 | ip->iso_frame_desc[i].offset = size * i; | |
438 | ip->iso_frame_desc[i].length = size; | |
439 | } | |
440 | ||
441 | return ip; | |
442 | } | |
443 | ||
444 | static void usbtv_stop(struct usbtv *usbtv) | |
445 | { | |
446 | int i; | |
447 | unsigned long flags; | |
448 | ||
449 | /* Cancel running transfers. */ | |
450 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { | |
451 | struct urb *ip = usbtv->isoc_urbs[i]; | |
146af9cb | 452 | |
f3d27f34 LR |
453 | if (ip == NULL) |
454 | continue; | |
455 | usb_kill_urb(ip); | |
456 | kfree(ip->transfer_buffer); | |
457 | usb_free_urb(ip); | |
458 | usbtv->isoc_urbs[i] = NULL; | |
459 | } | |
460 | ||
461 | /* Return buffers to userspace. */ | |
462 | spin_lock_irqsave(&usbtv->buflock, flags); | |
463 | while (!list_empty(&usbtv->bufs)) { | |
464 | struct usbtv_buf *buf = list_first_entry(&usbtv->bufs, | |
465 | struct usbtv_buf, list); | |
2d700715 | 466 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
f3d27f34 LR |
467 | list_del(&buf->list); |
468 | } | |
469 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
470 | } | |
471 | ||
472 | static int usbtv_start(struct usbtv *usbtv) | |
473 | { | |
474 | int i; | |
475 | int ret; | |
476 | ||
63ddf68d FS |
477 | usbtv_audio_suspend(usbtv); |
478 | ||
f3d27f34 LR |
479 | ret = usb_set_interface(usbtv->udev, 0, 0); |
480 | if (ret < 0) | |
481 | return ret; | |
482 | ||
483 | ret = usbtv_setup_capture(usbtv); | |
484 | if (ret < 0) | |
485 | return ret; | |
486 | ||
487 | ret = usb_set_interface(usbtv->udev, 0, 1); | |
488 | if (ret < 0) | |
489 | return ret; | |
490 | ||
63ddf68d FS |
491 | usbtv_audio_resume(usbtv); |
492 | ||
f3d27f34 LR |
493 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { |
494 | struct urb *ip; | |
495 | ||
496 | ip = usbtv_setup_iso_transfer(usbtv); | |
497 | if (ip == NULL) { | |
498 | ret = -ENOMEM; | |
499 | goto start_fail; | |
500 | } | |
501 | usbtv->isoc_urbs[i] = ip; | |
502 | ||
503 | ret = usb_submit_urb(ip, GFP_KERNEL); | |
504 | if (ret < 0) | |
505 | goto start_fail; | |
506 | } | |
507 | ||
508 | return 0; | |
509 | ||
510 | start_fail: | |
511 | usbtv_stop(usbtv); | |
512 | return ret; | |
513 | } | |
514 | ||
f3d27f34 LR |
515 | static int usbtv_querycap(struct file *file, void *priv, |
516 | struct v4l2_capability *cap) | |
517 | { | |
518 | struct usbtv *dev = video_drvdata(file); | |
519 | ||
520 | strlcpy(cap->driver, "usbtv", sizeof(cap->driver)); | |
521 | strlcpy(cap->card, "usbtv", sizeof(cap->card)); | |
522 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); | |
523 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE; | |
524 | cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
525 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
526 | return 0; | |
527 | } | |
528 | ||
529 | static int usbtv_enum_input(struct file *file, void *priv, | |
530 | struct v4l2_input *i) | |
531 | { | |
0e0fe395 GK |
532 | struct usbtv *dev = video_drvdata(file); |
533 | ||
2206112b LR |
534 | switch (i->index) { |
535 | case USBTV_COMPOSITE_INPUT: | |
536 | strlcpy(i->name, "Composite", sizeof(i->name)); | |
537 | break; | |
538 | case USBTV_SVIDEO_INPUT: | |
539 | strlcpy(i->name, "S-Video", sizeof(i->name)); | |
540 | break; | |
541 | default: | |
f3d27f34 | 542 | return -EINVAL; |
2206112b | 543 | } |
f3d27f34 | 544 | |
f3d27f34 | 545 | i->type = V4L2_INPUT_TYPE_CAMERA; |
0e0fe395 | 546 | i->std = dev->vdev.tvnorms; |
f3d27f34 LR |
547 | return 0; |
548 | } | |
549 | ||
550 | static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, | |
551 | struct v4l2_fmtdesc *f) | |
552 | { | |
553 | if (f->index > 0) | |
554 | return -EINVAL; | |
555 | ||
556 | strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed", | |
557 | sizeof(f->description)); | |
558 | f->pixelformat = V4L2_PIX_FMT_YUYV; | |
559 | return 0; | |
560 | } | |
561 | ||
562 | static int usbtv_fmt_vid_cap(struct file *file, void *priv, | |
563 | struct v4l2_format *f) | |
564 | { | |
0e0fe395 GK |
565 | struct usbtv *usbtv = video_drvdata(file); |
566 | ||
567 | f->fmt.pix.width = usbtv->width; | |
568 | f->fmt.pix.height = usbtv->height; | |
f3d27f34 LR |
569 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
570 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
0e0fe395 | 571 | f->fmt.pix.bytesperline = usbtv->width * 2; |
f3d27f34 LR |
572 | f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height); |
573 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
0e0fe395 | 574 | |
f3d27f34 LR |
575 | return 0; |
576 | } | |
577 | ||
578 | static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm) | |
579 | { | |
0e0fe395 GK |
580 | struct usbtv *usbtv = video_drvdata(file); |
581 | *norm = usbtv->norm; | |
f3d27f34 LR |
582 | return 0; |
583 | } | |
584 | ||
0e0fe395 GK |
585 | static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm) |
586 | { | |
587 | int ret = -EINVAL; | |
588 | struct usbtv *usbtv = video_drvdata(file); | |
589 | ||
590 | if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL)) | |
591 | ret = usbtv_select_norm(usbtv, norm); | |
592 | ||
593 | return ret; | |
594 | } | |
595 | ||
f3d27f34 LR |
596 | static int usbtv_g_input(struct file *file, void *priv, unsigned int *i) |
597 | { | |
2206112b LR |
598 | struct usbtv *usbtv = video_drvdata(file); |
599 | *i = usbtv->input; | |
f3d27f34 LR |
600 | return 0; |
601 | } | |
602 | ||
603 | static int usbtv_s_input(struct file *file, void *priv, unsigned int i) | |
604 | { | |
2206112b | 605 | struct usbtv *usbtv = video_drvdata(file); |
146af9cb | 606 | |
2206112b | 607 | return usbtv_select_input(usbtv, i); |
f3d27f34 LR |
608 | } |
609 | ||
9b058373 | 610 | static struct v4l2_ioctl_ops usbtv_ioctl_ops = { |
f3d27f34 LR |
611 | .vidioc_querycap = usbtv_querycap, |
612 | .vidioc_enum_input = usbtv_enum_input, | |
613 | .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap, | |
614 | .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap, | |
615 | .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap, | |
616 | .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap, | |
617 | .vidioc_g_std = usbtv_g_std, | |
618 | .vidioc_s_std = usbtv_s_std, | |
619 | .vidioc_g_input = usbtv_g_input, | |
620 | .vidioc_s_input = usbtv_s_input, | |
621 | ||
622 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
623 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
624 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
625 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
626 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
627 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
628 | .vidioc_streamon = vb2_ioctl_streamon, | |
629 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
630 | }; | |
631 | ||
9b058373 | 632 | static struct v4l2_file_operations usbtv_fops = { |
f3d27f34 LR |
633 | .owner = THIS_MODULE, |
634 | .unlocked_ioctl = video_ioctl2, | |
635 | .mmap = vb2_fop_mmap, | |
636 | .open = v4l2_fh_open, | |
637 | .release = vb2_fop_release, | |
638 | .read = vb2_fop_read, | |
639 | .poll = vb2_fop_poll, | |
640 | }; | |
641 | ||
642 | static int usbtv_queue_setup(struct vb2_queue *vq, | |
df9ecb0c | 643 | unsigned int *nbuffers, |
36c0f8b3 | 644 | unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) |
f3d27f34 | 645 | { |
0e0fe395 | 646 | struct usbtv *usbtv = vb2_get_drv_priv(vq); |
a988410a | 647 | unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32); |
0e0fe395 | 648 | |
a988410a HV |
649 | if (vq->num_buffers + *nbuffers < 2) |
650 | *nbuffers = 2 - vq->num_buffers; | |
df9ecb0c HV |
651 | if (*nplanes) |
652 | return sizes[0] < size ? -EINVAL : 0; | |
f3d27f34 | 653 | *nplanes = 1; |
df9ecb0c | 654 | sizes[0] = size; |
f3d27f34 LR |
655 | |
656 | return 0; | |
657 | } | |
658 | ||
659 | static void usbtv_buf_queue(struct vb2_buffer *vb) | |
660 | { | |
2d700715 | 661 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
f3d27f34 | 662 | struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue); |
2d700715 | 663 | struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb); |
f3d27f34 LR |
664 | unsigned long flags; |
665 | ||
666 | if (usbtv->udev == NULL) { | |
667 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
668 | return; | |
669 | } | |
670 | ||
671 | spin_lock_irqsave(&usbtv->buflock, flags); | |
672 | list_add_tail(&buf->list, &usbtv->bufs); | |
673 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
674 | } | |
675 | ||
676 | static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count) | |
677 | { | |
678 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
679 | ||
680 | if (usbtv->udev == NULL) | |
681 | return -ENODEV; | |
682 | ||
80fa4f07 | 683 | usbtv->last_odd = 1; |
a988410a | 684 | usbtv->sequence = 0; |
f3d27f34 LR |
685 | return usbtv_start(usbtv); |
686 | } | |
687 | ||
e37559b2 | 688 | static void usbtv_stop_streaming(struct vb2_queue *vq) |
f3d27f34 LR |
689 | { |
690 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
691 | ||
e37559b2 HV |
692 | if (usbtv->udev) |
693 | usbtv_stop(usbtv); | |
f3d27f34 LR |
694 | } |
695 | ||
1bc17717 | 696 | static const struct vb2_ops usbtv_vb2_ops = { |
f3d27f34 LR |
697 | .queue_setup = usbtv_queue_setup, |
698 | .buf_queue = usbtv_buf_queue, | |
699 | .start_streaming = usbtv_start_streaming, | |
700 | .stop_streaming = usbtv_stop_streaming, | |
701 | }; | |
702 | ||
c53a846c LR |
703 | static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl) |
704 | { | |
705 | struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv, | |
706 | ctrl); | |
707 | u8 data[3]; | |
708 | u16 index, size; | |
709 | int ret; | |
710 | ||
711 | /* | |
712 | * Read in the current brightness/contrast registers. We need them | |
713 | * both, because the values are for some reason interleaved. | |
714 | */ | |
715 | if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) { | |
716 | ret = usb_control_msg(usbtv->udev, | |
717 | usb_sndctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG, | |
718 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
719 | 0, USBTV_BASE + 0x0244, (void *)data, 3, 0); | |
720 | } | |
721 | ||
722 | switch (ctrl->id) { | |
723 | case V4L2_CID_BRIGHTNESS: | |
724 | index = USBTV_BASE + 0x0244; | |
725 | size = 3; | |
726 | data[0] &= 0xf0; | |
727 | data[0] |= (ctrl->val >> 8) & 0xf; | |
728 | data[2] = ctrl->val & 0xff; | |
729 | break; | |
730 | case V4L2_CID_CONTRAST: | |
731 | index = USBTV_BASE + 0x0244; | |
732 | size = 3; | |
733 | data[0] &= 0x0f; | |
734 | data[0] |= (ctrl->val >> 4) & 0xf0; | |
735 | data[1] = ctrl->val & 0xff; | |
736 | break; | |
737 | case V4L2_CID_SATURATION: | |
738 | index = USBTV_BASE + 0x0242; | |
739 | data[0] = ctrl->val >> 8; | |
740 | data[1] = ctrl->val & 0xff; | |
741 | size = 2; | |
742 | break; | |
743 | case V4L2_CID_HUE: | |
744 | index = USBTV_BASE + 0x0240; | |
745 | size = 2; | |
746 | if (ctrl->val > 0) { | |
747 | data[0] = 0x92 + (ctrl->val >> 8); | |
748 | data[1] = ctrl->val & 0xff; | |
749 | } else { | |
750 | data[0] = 0x82 + (-ctrl->val >> 8); | |
751 | data[1] = -ctrl->val & 0xff; | |
752 | } | |
753 | break; | |
754 | default: | |
755 | return -EINVAL; | |
756 | } | |
757 | ||
758 | ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0), | |
759 | USBTV_CONTROL_REG, | |
760 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
761 | 0, index, (void *)data, size, 0); | |
762 | if (ret < 0) { | |
763 | dev_warn(usbtv->dev, "Failed to submit a control request.\n"); | |
764 | return ret; | |
765 | } | |
766 | ||
767 | return 0; | |
768 | } | |
769 | ||
770 | static const struct v4l2_ctrl_ops usbtv_ctrl_ops = { | |
771 | .s_ctrl = usbtv_s_ctrl, | |
772 | }; | |
773 | ||
f3d27f34 LR |
774 | static void usbtv_release(struct v4l2_device *v4l2_dev) |
775 | { | |
776 | struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev); | |
777 | ||
778 | v4l2_device_unregister(&usbtv->v4l2_dev); | |
c53a846c | 779 | v4l2_ctrl_handler_free(&usbtv->ctrl); |
f3d27f34 LR |
780 | vb2_queue_release(&usbtv->vb2q); |
781 | kfree(usbtv); | |
782 | } | |
783 | ||
a3550ea6 | 784 | int usbtv_video_init(struct usbtv *usbtv) |
f3d27f34 LR |
785 | { |
786 | int ret; | |
0e0fe395 GK |
787 | |
788 | (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60); | |
789 | ||
f3d27f34 LR |
790 | spin_lock_init(&usbtv->buflock); |
791 | mutex_init(&usbtv->v4l2_lock); | |
792 | mutex_init(&usbtv->vb2q_lock); | |
793 | INIT_LIST_HEAD(&usbtv->bufs); | |
794 | ||
795 | /* videobuf2 structure */ | |
796 | usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
797 | usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; | |
798 | usbtv->vb2q.drv_priv = usbtv; | |
799 | usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf); | |
800 | usbtv->vb2q.ops = &usbtv_vb2_ops; | |
801 | usbtv->vb2q.mem_ops = &vb2_vmalloc_memops; | |
ade48681 | 802 | usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
f3d27f34 LR |
803 | usbtv->vb2q.lock = &usbtv->vb2q_lock; |
804 | ret = vb2_queue_init(&usbtv->vb2q); | |
805 | if (ret < 0) { | |
a3550ea6 FS |
806 | dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n"); |
807 | return ret; | |
f3d27f34 LR |
808 | } |
809 | ||
c53a846c LR |
810 | /* controls */ |
811 | v4l2_ctrl_handler_init(&usbtv->ctrl, 4); | |
812 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, | |
813 | V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0); | |
814 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, | |
815 | V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0); | |
816 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, | |
817 | V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200); | |
818 | v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops, | |
819 | V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000); | |
820 | ret = usbtv->ctrl.error; | |
821 | if (ret < 0) { | |
822 | dev_warn(usbtv->dev, "Could not initialize controls\n"); | |
823 | goto ctrl_fail; | |
824 | } | |
825 | ||
f3d27f34 | 826 | /* v4l2 structure */ |
c53a846c | 827 | usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl; |
f3d27f34 | 828 | usbtv->v4l2_dev.release = usbtv_release; |
a3550ea6 | 829 | ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev); |
f3d27f34 | 830 | if (ret < 0) { |
a3550ea6 | 831 | dev_warn(usbtv->dev, "Could not register v4l2 device\n"); |
f3d27f34 LR |
832 | goto v4l2_fail; |
833 | } | |
834 | ||
f3d27f34 LR |
835 | /* Video structure */ |
836 | strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name)); | |
837 | usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev; | |
838 | usbtv->vdev.release = video_device_release_empty; | |
839 | usbtv->vdev.fops = &usbtv_fops; | |
840 | usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops; | |
0e0fe395 | 841 | usbtv->vdev.tvnorms = USBTV_TV_STD; |
f3d27f34 LR |
842 | usbtv->vdev.queue = &usbtv->vb2q; |
843 | usbtv->vdev.lock = &usbtv->v4l2_lock; | |
f3d27f34 LR |
844 | video_set_drvdata(&usbtv->vdev, usbtv); |
845 | ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1); | |
846 | if (ret < 0) { | |
a3550ea6 | 847 | dev_warn(usbtv->dev, "Could not register video device\n"); |
f3d27f34 LR |
848 | goto vdev_fail; |
849 | } | |
850 | ||
f3d27f34 LR |
851 | return 0; |
852 | ||
853 | vdev_fail: | |
854 | v4l2_device_unregister(&usbtv->v4l2_dev); | |
855 | v4l2_fail: | |
c53a846c LR |
856 | ctrl_fail: |
857 | v4l2_ctrl_handler_free(&usbtv->ctrl); | |
f3d27f34 | 858 | vb2_queue_release(&usbtv->vb2q); |
f3d27f34 LR |
859 | |
860 | return ret; | |
861 | } | |
862 | ||
a3550ea6 | 863 | void usbtv_video_free(struct usbtv *usbtv) |
f3d27f34 | 864 | { |
f3d27f34 LR |
865 | mutex_lock(&usbtv->vb2q_lock); |
866 | mutex_lock(&usbtv->v4l2_lock); | |
867 | ||
868 | usbtv_stop(usbtv); | |
f3d27f34 LR |
869 | video_unregister_device(&usbtv->vdev); |
870 | v4l2_device_disconnect(&usbtv->v4l2_dev); | |
f3d27f34 LR |
871 | |
872 | mutex_unlock(&usbtv->v4l2_lock); | |
873 | mutex_unlock(&usbtv->vb2q_lock); | |
874 | ||
875 | v4l2_device_put(&usbtv->v4l2_dev); | |
876 | } |