]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2013 Lubomir Rintel | |
3 | * All rights reserved. | |
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"). | |
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 | |
43 | */ | |
44 | ||
45 | #include <media/v4l2-ioctl.h> | |
46 | #include <media/videobuf2-v4l2.h> | |
47 | ||
48 | #include "usbtv.h" | |
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 | ||
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 | ||
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 }, | |
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 }, | |
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 | ||
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 | ||
175 | static int usbtv_setup_capture(struct usbtv *usbtv) | |
176 | { | |
177 | int ret; | |
178 | static const u16 setup[][2] = { | |
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 | ||
240 | { USBTV_BASE + 0x0003, 0x0004 }, | |
241 | { USBTV_BASE + 0x0100, 0x00d3 }, | |
242 | { USBTV_BASE + 0x0115, 0x0015 }, | |
243 | { USBTV_BASE + 0x0220, 0x002e }, | |
244 | { USBTV_BASE + 0x0225, 0x0008 }, | |
245 | { USBTV_BASE + 0x024e, 0x0002 }, | |
246 | { USBTV_BASE + 0x024e, 0x0002 }, | |
247 | { USBTV_BASE + 0x024f, 0x0002 }, | |
248 | }; | |
249 | ||
250 | ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup)); | |
251 | if (ret) | |
252 | return ret; | |
253 | ||
254 | ret = usbtv_select_norm(usbtv, usbtv->norm); | |
255 | if (ret) | |
256 | return ret; | |
257 | ||
258 | ret = usbtv_select_input(usbtv, usbtv->input); | |
259 | if (ret) | |
260 | return ret; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | /* Copy data from chunk into a frame buffer, deinterlacing the data | |
266 | * into every second line. Unfortunately, they don't align nicely into | |
267 | * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels. | |
268 | * Therefore, we break down the chunk into two halves before copyting, | |
269 | * so that we can interleave a line if needed. */ | |
270 | static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd) | |
271 | { | |
272 | int half; | |
273 | ||
274 | for (half = 0; half < 2; half++) { | |
275 | int part_no = chunk_no * 2 + half; | |
276 | int line = part_no / 3; | |
277 | int part_index = (line * 2 + !odd) * 3 + (part_no % 3); | |
278 | ||
279 | u32 *dst = &frame[part_index * USBTV_CHUNK/2]; | |
280 | ||
281 | memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src)); | |
282 | src += USBTV_CHUNK/2; | |
283 | } | |
284 | } | |
285 | ||
286 | /* Called for each 256-byte image chunk. | |
287 | * First word identifies the chunk, followed by 240 words of image | |
288 | * data and padding. */ | |
289 | static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk) | |
290 | { | |
291 | int frame_id, odd, chunk_no; | |
292 | u32 *frame; | |
293 | struct usbtv_buf *buf; | |
294 | unsigned long flags; | |
295 | ||
296 | /* Ignore corrupted lines. */ | |
297 | if (!USBTV_MAGIC_OK(chunk)) | |
298 | return; | |
299 | frame_id = USBTV_FRAME_ID(chunk); | |
300 | odd = USBTV_ODD(chunk); | |
301 | chunk_no = USBTV_CHUNK_NO(chunk); | |
302 | if (chunk_no >= usbtv->n_chunks) | |
303 | return; | |
304 | ||
305 | /* Beginning of a frame. */ | |
306 | if (chunk_no == 0) { | |
307 | usbtv->frame_id = frame_id; | |
308 | usbtv->chunks_done = 0; | |
309 | } | |
310 | ||
311 | if (usbtv->frame_id != frame_id) | |
312 | return; | |
313 | ||
314 | spin_lock_irqsave(&usbtv->buflock, flags); | |
315 | if (list_empty(&usbtv->bufs)) { | |
316 | /* No free buffers. Userspace likely too slow. */ | |
317 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
318 | return; | |
319 | } | |
320 | ||
321 | /* First available buffer. */ | |
322 | buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list); | |
323 | frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); | |
324 | ||
325 | /* Copy the chunk data. */ | |
326 | usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd); | |
327 | usbtv->chunks_done++; | |
328 | ||
329 | /* Last chunk in a field */ | |
330 | if (chunk_no == usbtv->n_chunks-1) { | |
331 | /* Last chunk in a frame, signalling an end */ | |
332 | if (odd && !usbtv->last_odd) { | |
333 | int size = vb2_plane_size(&buf->vb.vb2_buf, 0); | |
334 | enum vb2_buffer_state state = usbtv->chunks_done == | |
335 | usbtv->n_chunks ? | |
336 | VB2_BUF_STATE_DONE : | |
337 | VB2_BUF_STATE_ERROR; | |
338 | ||
339 | buf->vb.field = V4L2_FIELD_INTERLACED; | |
340 | buf->vb.sequence = usbtv->sequence++; | |
341 | buf->vb.vb2_buf.timestamp = ktime_get_ns(); | |
342 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); | |
343 | vb2_buffer_done(&buf->vb.vb2_buf, state); | |
344 | list_del(&buf->list); | |
345 | } | |
346 | usbtv->last_odd = odd; | |
347 | } | |
348 | ||
349 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
350 | } | |
351 | ||
352 | /* Got image data. Each packet contains a number of 256-word chunks we | |
353 | * compose the image from. */ | |
354 | static void usbtv_iso_cb(struct urb *ip) | |
355 | { | |
356 | int ret; | |
357 | int i; | |
358 | struct usbtv *usbtv = (struct usbtv *)ip->context; | |
359 | ||
360 | switch (ip->status) { | |
361 | /* All fine. */ | |
362 | case 0: | |
363 | break; | |
364 | /* Device disconnected or capture stopped? */ | |
365 | case -ENODEV: | |
366 | case -ENOENT: | |
367 | case -ECONNRESET: | |
368 | case -ESHUTDOWN: | |
369 | return; | |
370 | /* Unknown error. Retry. */ | |
371 | default: | |
372 | dev_warn(usbtv->dev, "Bad response for ISO request.\n"); | |
373 | goto resubmit; | |
374 | } | |
375 | ||
376 | for (i = 0; i < ip->number_of_packets; i++) { | |
377 | int size = ip->iso_frame_desc[i].actual_length; | |
378 | unsigned char *data = ip->transfer_buffer + | |
379 | ip->iso_frame_desc[i].offset; | |
380 | int offset; | |
381 | ||
382 | for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++) | |
383 | usbtv_image_chunk(usbtv, | |
384 | (__be32 *)&data[USBTV_CHUNK_SIZE * offset]); | |
385 | } | |
386 | ||
387 | resubmit: | |
388 | ret = usb_submit_urb(ip, GFP_ATOMIC); | |
389 | if (ret < 0) | |
390 | dev_warn(usbtv->dev, "Could not resubmit ISO URB\n"); | |
391 | } | |
392 | ||
393 | static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv) | |
394 | { | |
395 | struct urb *ip; | |
396 | int size = usbtv->iso_size; | |
397 | int i; | |
398 | ||
399 | ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL); | |
400 | if (ip == NULL) | |
401 | return NULL; | |
402 | ||
403 | ip->dev = usbtv->udev; | |
404 | ip->context = usbtv; | |
405 | ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP); | |
406 | ip->interval = 1; | |
407 | ip->transfer_flags = URB_ISO_ASAP; | |
408 | ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS, | |
409 | GFP_KERNEL); | |
410 | if (!ip->transfer_buffer) { | |
411 | usb_free_urb(ip); | |
412 | return NULL; | |
413 | } | |
414 | ip->complete = usbtv_iso_cb; | |
415 | ip->number_of_packets = USBTV_ISOC_PACKETS; | |
416 | ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS; | |
417 | for (i = 0; i < USBTV_ISOC_PACKETS; i++) { | |
418 | ip->iso_frame_desc[i].offset = size * i; | |
419 | ip->iso_frame_desc[i].length = size; | |
420 | } | |
421 | ||
422 | return ip; | |
423 | } | |
424 | ||
425 | static void usbtv_stop(struct usbtv *usbtv) | |
426 | { | |
427 | int i; | |
428 | unsigned long flags; | |
429 | ||
430 | /* Cancel running transfers. */ | |
431 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { | |
432 | struct urb *ip = usbtv->isoc_urbs[i]; | |
433 | ||
434 | if (ip == NULL) | |
435 | continue; | |
436 | usb_kill_urb(ip); | |
437 | kfree(ip->transfer_buffer); | |
438 | usb_free_urb(ip); | |
439 | usbtv->isoc_urbs[i] = NULL; | |
440 | } | |
441 | ||
442 | /* Return buffers to userspace. */ | |
443 | spin_lock_irqsave(&usbtv->buflock, flags); | |
444 | while (!list_empty(&usbtv->bufs)) { | |
445 | struct usbtv_buf *buf = list_first_entry(&usbtv->bufs, | |
446 | struct usbtv_buf, list); | |
447 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); | |
448 | list_del(&buf->list); | |
449 | } | |
450 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
451 | } | |
452 | ||
453 | static int usbtv_start(struct usbtv *usbtv) | |
454 | { | |
455 | int i; | |
456 | int ret; | |
457 | ||
458 | usbtv_audio_suspend(usbtv); | |
459 | ||
460 | ret = usb_set_interface(usbtv->udev, 0, 0); | |
461 | if (ret < 0) | |
462 | return ret; | |
463 | ||
464 | ret = usbtv_setup_capture(usbtv); | |
465 | if (ret < 0) | |
466 | return ret; | |
467 | ||
468 | ret = usb_set_interface(usbtv->udev, 0, 1); | |
469 | if (ret < 0) | |
470 | return ret; | |
471 | ||
472 | usbtv_audio_resume(usbtv); | |
473 | ||
474 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { | |
475 | struct urb *ip; | |
476 | ||
477 | ip = usbtv_setup_iso_transfer(usbtv); | |
478 | if (ip == NULL) { | |
479 | ret = -ENOMEM; | |
480 | goto start_fail; | |
481 | } | |
482 | usbtv->isoc_urbs[i] = ip; | |
483 | ||
484 | ret = usb_submit_urb(ip, GFP_KERNEL); | |
485 | if (ret < 0) | |
486 | goto start_fail; | |
487 | } | |
488 | ||
489 | return 0; | |
490 | ||
491 | start_fail: | |
492 | usbtv_stop(usbtv); | |
493 | return ret; | |
494 | } | |
495 | ||
496 | static int usbtv_querycap(struct file *file, void *priv, | |
497 | struct v4l2_capability *cap) | |
498 | { | |
499 | struct usbtv *dev = video_drvdata(file); | |
500 | ||
501 | strlcpy(cap->driver, "usbtv", sizeof(cap->driver)); | |
502 | strlcpy(cap->card, "usbtv", sizeof(cap->card)); | |
503 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); | |
504 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE; | |
505 | cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
506 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
507 | return 0; | |
508 | } | |
509 | ||
510 | static int usbtv_enum_input(struct file *file, void *priv, | |
511 | struct v4l2_input *i) | |
512 | { | |
513 | struct usbtv *dev = video_drvdata(file); | |
514 | ||
515 | switch (i->index) { | |
516 | case USBTV_COMPOSITE_INPUT: | |
517 | strlcpy(i->name, "Composite", sizeof(i->name)); | |
518 | break; | |
519 | case USBTV_SVIDEO_INPUT: | |
520 | strlcpy(i->name, "S-Video", sizeof(i->name)); | |
521 | break; | |
522 | default: | |
523 | return -EINVAL; | |
524 | } | |
525 | ||
526 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
527 | i->std = dev->vdev.tvnorms; | |
528 | return 0; | |
529 | } | |
530 | ||
531 | static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, | |
532 | struct v4l2_fmtdesc *f) | |
533 | { | |
534 | if (f->index > 0) | |
535 | return -EINVAL; | |
536 | ||
537 | strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed", | |
538 | sizeof(f->description)); | |
539 | f->pixelformat = V4L2_PIX_FMT_YUYV; | |
540 | return 0; | |
541 | } | |
542 | ||
543 | static int usbtv_fmt_vid_cap(struct file *file, void *priv, | |
544 | struct v4l2_format *f) | |
545 | { | |
546 | struct usbtv *usbtv = video_drvdata(file); | |
547 | ||
548 | f->fmt.pix.width = usbtv->width; | |
549 | f->fmt.pix.height = usbtv->height; | |
550 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; | |
551 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
552 | f->fmt.pix.bytesperline = usbtv->width * 2; | |
553 | f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height); | |
554 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm) | |
560 | { | |
561 | struct usbtv *usbtv = video_drvdata(file); | |
562 | *norm = usbtv->norm; | |
563 | return 0; | |
564 | } | |
565 | ||
566 | static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm) | |
567 | { | |
568 | int ret = -EINVAL; | |
569 | struct usbtv *usbtv = video_drvdata(file); | |
570 | ||
571 | if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL)) | |
572 | ret = usbtv_select_norm(usbtv, norm); | |
573 | ||
574 | return ret; | |
575 | } | |
576 | ||
577 | static int usbtv_g_input(struct file *file, void *priv, unsigned int *i) | |
578 | { | |
579 | struct usbtv *usbtv = video_drvdata(file); | |
580 | *i = usbtv->input; | |
581 | return 0; | |
582 | } | |
583 | ||
584 | static int usbtv_s_input(struct file *file, void *priv, unsigned int i) | |
585 | { | |
586 | struct usbtv *usbtv = video_drvdata(file); | |
587 | ||
588 | return usbtv_select_input(usbtv, i); | |
589 | } | |
590 | ||
591 | static struct v4l2_ioctl_ops usbtv_ioctl_ops = { | |
592 | .vidioc_querycap = usbtv_querycap, | |
593 | .vidioc_enum_input = usbtv_enum_input, | |
594 | .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap, | |
595 | .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap, | |
596 | .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap, | |
597 | .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap, | |
598 | .vidioc_g_std = usbtv_g_std, | |
599 | .vidioc_s_std = usbtv_s_std, | |
600 | .vidioc_g_input = usbtv_g_input, | |
601 | .vidioc_s_input = usbtv_s_input, | |
602 | ||
603 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
604 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
605 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
606 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
607 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
608 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
609 | .vidioc_streamon = vb2_ioctl_streamon, | |
610 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
611 | }; | |
612 | ||
613 | static struct v4l2_file_operations usbtv_fops = { | |
614 | .owner = THIS_MODULE, | |
615 | .unlocked_ioctl = video_ioctl2, | |
616 | .mmap = vb2_fop_mmap, | |
617 | .open = v4l2_fh_open, | |
618 | .release = vb2_fop_release, | |
619 | .read = vb2_fop_read, | |
620 | .poll = vb2_fop_poll, | |
621 | }; | |
622 | ||
623 | static int usbtv_queue_setup(struct vb2_queue *vq, | |
624 | unsigned int *nbuffers, | |
625 | unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) | |
626 | { | |
627 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
628 | unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32); | |
629 | ||
630 | if (vq->num_buffers + *nbuffers < 2) | |
631 | *nbuffers = 2 - vq->num_buffers; | |
632 | if (*nplanes) | |
633 | return sizes[0] < size ? -EINVAL : 0; | |
634 | *nplanes = 1; | |
635 | sizes[0] = size; | |
636 | ||
637 | return 0; | |
638 | } | |
639 | ||
640 | static void usbtv_buf_queue(struct vb2_buffer *vb) | |
641 | { | |
642 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); | |
643 | struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue); | |
644 | struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb); | |
645 | unsigned long flags; | |
646 | ||
647 | if (usbtv->udev == NULL) { | |
648 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
649 | return; | |
650 | } | |
651 | ||
652 | spin_lock_irqsave(&usbtv->buflock, flags); | |
653 | list_add_tail(&buf->list, &usbtv->bufs); | |
654 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
655 | } | |
656 | ||
657 | static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count) | |
658 | { | |
659 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
660 | ||
661 | if (usbtv->udev == NULL) | |
662 | return -ENODEV; | |
663 | ||
664 | usbtv->last_odd = 1; | |
665 | usbtv->sequence = 0; | |
666 | return usbtv_start(usbtv); | |
667 | } | |
668 | ||
669 | static void usbtv_stop_streaming(struct vb2_queue *vq) | |
670 | { | |
671 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
672 | ||
673 | if (usbtv->udev) | |
674 | usbtv_stop(usbtv); | |
675 | } | |
676 | ||
677 | static struct vb2_ops usbtv_vb2_ops = { | |
678 | .queue_setup = usbtv_queue_setup, | |
679 | .buf_queue = usbtv_buf_queue, | |
680 | .start_streaming = usbtv_start_streaming, | |
681 | .stop_streaming = usbtv_stop_streaming, | |
682 | }; | |
683 | ||
684 | static void usbtv_release(struct v4l2_device *v4l2_dev) | |
685 | { | |
686 | struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev); | |
687 | ||
688 | v4l2_device_unregister(&usbtv->v4l2_dev); | |
689 | vb2_queue_release(&usbtv->vb2q); | |
690 | kfree(usbtv); | |
691 | } | |
692 | ||
693 | int usbtv_video_init(struct usbtv *usbtv) | |
694 | { | |
695 | int ret; | |
696 | ||
697 | (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60); | |
698 | ||
699 | spin_lock_init(&usbtv->buflock); | |
700 | mutex_init(&usbtv->v4l2_lock); | |
701 | mutex_init(&usbtv->vb2q_lock); | |
702 | INIT_LIST_HEAD(&usbtv->bufs); | |
703 | ||
704 | /* videobuf2 structure */ | |
705 | usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
706 | usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; | |
707 | usbtv->vb2q.drv_priv = usbtv; | |
708 | usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf); | |
709 | usbtv->vb2q.ops = &usbtv_vb2_ops; | |
710 | usbtv->vb2q.mem_ops = &vb2_vmalloc_memops; | |
711 | usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
712 | usbtv->vb2q.lock = &usbtv->vb2q_lock; | |
713 | ret = vb2_queue_init(&usbtv->vb2q); | |
714 | if (ret < 0) { | |
715 | dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n"); | |
716 | return ret; | |
717 | } | |
718 | ||
719 | /* v4l2 structure */ | |
720 | usbtv->v4l2_dev.release = usbtv_release; | |
721 | ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev); | |
722 | if (ret < 0) { | |
723 | dev_warn(usbtv->dev, "Could not register v4l2 device\n"); | |
724 | goto v4l2_fail; | |
725 | } | |
726 | ||
727 | /* Video structure */ | |
728 | strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name)); | |
729 | usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev; | |
730 | usbtv->vdev.release = video_device_release_empty; | |
731 | usbtv->vdev.fops = &usbtv_fops; | |
732 | usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops; | |
733 | usbtv->vdev.tvnorms = USBTV_TV_STD; | |
734 | usbtv->vdev.queue = &usbtv->vb2q; | |
735 | usbtv->vdev.lock = &usbtv->v4l2_lock; | |
736 | video_set_drvdata(&usbtv->vdev, usbtv); | |
737 | ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1); | |
738 | if (ret < 0) { | |
739 | dev_warn(usbtv->dev, "Could not register video device\n"); | |
740 | goto vdev_fail; | |
741 | } | |
742 | ||
743 | return 0; | |
744 | ||
745 | vdev_fail: | |
746 | v4l2_device_unregister(&usbtv->v4l2_dev); | |
747 | v4l2_fail: | |
748 | vb2_queue_release(&usbtv->vb2q); | |
749 | ||
750 | return ret; | |
751 | } | |
752 | ||
753 | void usbtv_video_free(struct usbtv *usbtv) | |
754 | { | |
755 | mutex_lock(&usbtv->vb2q_lock); | |
756 | mutex_lock(&usbtv->v4l2_lock); | |
757 | ||
758 | usbtv_stop(usbtv); | |
759 | video_unregister_device(&usbtv->vdev); | |
760 | v4l2_device_disconnect(&usbtv->v4l2_dev); | |
761 | ||
762 | mutex_unlock(&usbtv->v4l2_lock); | |
763 | mutex_unlock(&usbtv->vb2q_lock); | |
764 | ||
765 | v4l2_device_put(&usbtv->v4l2_dev); | |
766 | } |