aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/usbvision
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/usbvision')
-rw-r--r--drivers/media/video/usbvision/Kconfig12
-rw-r--r--drivers/media/video/usbvision/Makefile6
-rw-r--r--drivers/media/video/usbvision/usbvision-cards.c1132
-rw-r--r--drivers/media/video/usbvision/usbvision-cards.h69
-rw-r--r--drivers/media/video/usbvision/usbvision-core.c2520
-rw-r--r--drivers/media/video/usbvision/usbvision-i2c.c478
-rw-r--r--drivers/media/video/usbvision/usbvision-video.c1686
-rw-r--r--drivers/media/video/usbvision/usbvision.h535
8 files changed, 6438 insertions, 0 deletions
diff --git a/drivers/media/video/usbvision/Kconfig b/drivers/media/video/usbvision/Kconfig
new file mode 100644
index 00000000000..fc24ef05b3f
--- /dev/null
+++ b/drivers/media/video/usbvision/Kconfig
@@ -0,0 +1,12 @@
1config VIDEO_USBVISION
2 tristate "USB video devices based on Nogatech NT1003/1004/1005"
3 depends on I2C && VIDEO_V4L2
4 select VIDEO_TUNER
5 select VIDEO_SAA711X if VIDEO_HELPER_CHIPS_AUTO
6 ---help---
7 There are more than 50 different USB video devices based on
8 NT1003/1004/1005 USB Bridges. This driver enables using those
9 devices.
10
11 To compile this driver as a module, choose M here: the
12 module will be called usbvision.
diff --git a/drivers/media/video/usbvision/Makefile b/drivers/media/video/usbvision/Makefile
new file mode 100644
index 00000000000..33871875094
--- /dev/null
+++ b/drivers/media/video/usbvision/Makefile
@@ -0,0 +1,6 @@
1usbvision-objs := usbvision-core.o usbvision-video.o usbvision-i2c.o usbvision-cards.o
2
3obj-$(CONFIG_VIDEO_USBVISION) += usbvision.o
4
5EXTRA_CFLAGS += -Idrivers/media/video
6EXTRA_CFLAGS += -Idrivers/media/common/tuners
diff --git a/drivers/media/video/usbvision/usbvision-cards.c b/drivers/media/video/usbvision/usbvision-cards.c
new file mode 100644
index 00000000000..8f5266157f1
--- /dev/null
+++ b/drivers/media/video/usbvision/usbvision-cards.c
@@ -0,0 +1,1132 @@
1/*
2 * usbvision-cards.c
3 * usbvision cards definition file
4 *
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 *
7 * This module is part of usbvision driver project.
8 * Updates to driver completed by Dwaine P. Garden
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
26#include <linux/list.h>
27#include <media/v4l2-dev.h>
28#include <media/tuner.h>
29#include "usbvision.h"
30#include "usbvision-cards.h"
31
32/* Supported Devices: A table for usbvision.c*/
33struct usbvision_device_data_st usbvision_device_data[] = {
34 [XANBOO] = {
35 .interface = -1,
36 .codec = CODEC_SAA7113,
37 .video_channels = 4,
38 .video_norm = V4L2_STD_NTSC,
39 .audio_channels = 1,
40 .radio = 0,
41 .vbi = 1,
42 .tuner = 0,
43 .tuner_type = 0,
44 .x_offset = -1,
45 .y_offset = -1,
46 .model_string = "Xanboo",
47 },
48 [BELKIN_VIDEOBUS_II] = {
49 .interface = -1,
50 .codec = CODEC_SAA7113,
51 .video_channels = 2,
52 .video_norm = V4L2_STD_PAL,
53 .audio_channels = 1,
54 .radio = 0,
55 .vbi = 1,
56 .tuner = 0,
57 .tuner_type = 0,
58 .x_offset = 0,
59 .y_offset = 3,
60 .dvi_yuv_override = 1,
61 .dvi_yuv = 7,
62 .model_string = "Belkin USB VideoBus II Adapter",
63 },
64 [BELKIN_VIDEOBUS] = {
65 .interface = -1,
66 .codec = CODEC_SAA7111,
67 .video_channels = 2,
68 .video_norm = V4L2_STD_NTSC,
69 .audio_channels = 1,
70 .radio = 0,
71 .vbi = 1,
72 .tuner = 0,
73 .tuner_type = 0,
74 .x_offset = -1,
75 .y_offset = -1,
76 .model_string = "Belkin Components USB VideoBus",
77 },
78 [BELKIN_USB_VIDEOBUS_II] = {
79 .interface = -1,
80 .codec = CODEC_SAA7113,
81 .video_channels = 2,
82 .video_norm = V4L2_STD_NTSC,
83 .audio_channels = 1,
84 .radio = 0,
85 .vbi = 1,
86 .tuner = 0,
87 .tuner_type = 0,
88 .x_offset = 0,
89 .y_offset = 3,
90 .dvi_yuv_override = 1,
91 .dvi_yuv = 7,
92 .model_string = "Belkin USB VideoBus II",
93 },
94 [ECHOFX_INTERVIEW_LITE] = {
95 .interface = 0,
96 .codec = CODEC_SAA7111,
97 .video_channels = 2,
98 .video_norm = V4L2_STD_PAL,
99 .audio_channels = 0,
100 .radio = 0,
101 .vbi = 1,
102 .tuner = 0,
103 .tuner_type = 0,
104 .x_offset = -1,
105 .y_offset = -1,
106 .dvi_yuv_override = 1,
107 .dvi_yuv = 7,
108 .model_string = "echoFX InterView Lite",
109 },
110 [USBGEAR_USBG_V1] = {
111 .interface = -1,
112 .codec = CODEC_SAA7111,
113 .video_channels = 2,
114 .video_norm = V4L2_STD_NTSC,
115 .audio_channels = 1,
116 .radio = 0,
117 .vbi = 1,
118 .tuner = 0,
119 .tuner_type = 0,
120 .x_offset = -1,
121 .y_offset = -1,
122 .model_string = "USBGear USBG-V1 resp. HAMA USB",
123 },
124 [D_LINK_V100] = {
125 .interface = -1,
126 .codec = CODEC_SAA7113,
127 .video_channels = 4,
128 .video_norm = V4L2_STD_NTSC,
129 .audio_channels = 0,
130 .radio = 0,
131 .vbi = 1,
132 .tuner = 0,
133 .tuner_type = 0,
134 .x_offset = 0,
135 .y_offset = 3,
136 .dvi_yuv_override = 1,
137 .dvi_yuv = 7,
138 .model_string = "D-Link V100",
139 },
140 [X10_USB_CAMERA] = {
141 .interface = -1,
142 .codec = CODEC_SAA7111,
143 .video_channels = 2,
144 .video_norm = V4L2_STD_NTSC,
145 .audio_channels = 1,
146 .radio = 0,
147 .vbi = 1,
148 .tuner = 0,
149 .tuner_type = 0,
150 .x_offset = -1,
151 .y_offset = -1,
152 .model_string = "X10 USB Camera",
153 },
154 [HPG_WINTV_LIVE_PAL_BG] = {
155 .interface = -1,
156 .codec = CODEC_SAA7111,
157 .video_channels = 2,
158 .video_norm = V4L2_STD_PAL,
159 .audio_channels = 1,
160 .radio = 0,
161 .vbi = 1,
162 .tuner = 0,
163 .tuner_type = 0,
164 .x_offset = -1,
165 .y_offset = 3,
166 .dvi_yuv_override = 1,
167 .dvi_yuv = 7,
168 .model_string = "Hauppauge WinTV USB Live (PAL B/G)",
169 },
170 [HPG_WINTV_LIVE_PRO_NTSC_MN] = {
171 .interface = -1,
172 .codec = CODEC_SAA7113,
173 .video_channels = 2,
174 .video_norm = V4L2_STD_NTSC,
175 .audio_channels = 0,
176 .radio = 0,
177 .vbi = 1,
178 .tuner = 0,
179 .tuner_type = 0,
180 .x_offset = 0,
181 .y_offset = 3,
182 .dvi_yuv_override = 1,
183 .dvi_yuv = 7,
184 .model_string = "Hauppauge WinTV USB Live Pro (NTSC M/N)",
185 },
186 [ZORAN_PMD_NOGATECH] = {
187 .interface = -1,
188 .codec = CODEC_SAA7113,
189 .video_channels = 2,
190 .video_norm = V4L2_STD_PAL,
191 .audio_channels = 2,
192 .radio = 0,
193 .vbi = 1,
194 .tuner = 0,
195 .tuner_type = 0,
196 .x_offset = 0,
197 .y_offset = 3,
198 .dvi_yuv_override = 1,
199 .dvi_yuv = 7,
200 .model_string = "Zoran Co. PMD (Nogatech) AV-grabber Manhattan",
201 },
202 [NOGATECH_USB_TV_NTSC_FM] = {
203 .interface = -1,
204 .codec = CODEC_SAA7111,
205 .video_channels = 3,
206 .video_norm = V4L2_STD_NTSC,
207 .audio_channels = 1,
208 .radio = 1,
209 .vbi = 1,
210 .tuner = 1,
211 .tuner_type = TUNER_PHILIPS_NTSC_M,
212 .x_offset = -1,
213 .y_offset = 20,
214 .model_string = "Nogatech USB-TV (NTSC) FM",
215 },
216 [PNY_USB_TV_NTSC_FM] = {
217 .interface = -1,
218 .codec = CODEC_SAA7111,
219 .video_channels = 3,
220 .video_norm = V4L2_STD_NTSC,
221 .audio_channels = 1,
222 .radio = 1,
223 .vbi = 1,
224 .tuner = 1,
225 .tuner_type = TUNER_PHILIPS_NTSC_M,
226 .x_offset = -1,
227 .y_offset = 20,
228 .model_string = "PNY USB-TV (NTSC) FM",
229 },
230 [PV_PLAYTV_USB_PRO_PAL_FM] = {
231 .interface = 0,
232 .codec = CODEC_SAA7113,
233 .video_channels = 3,
234 .video_norm = V4L2_STD_PAL,
235 .audio_channels = 1,
236 .radio = 1,
237 .vbi = 1,
238 .tuner = 1,
239 .tuner_type = TUNER_PHILIPS_PAL,
240 .x_offset = 0,
241 .y_offset = 3,
242 .dvi_yuv_override = 1,
243 .dvi_yuv = 7,
244 .model_string = "PixelView PlayTv-USB PRO (PAL) FM",
245 },
246 [ZT_721] = {
247 .interface = 0,
248 .codec = CODEC_SAA7113,
249 .video_channels = 3,
250 .video_norm = V4L2_STD_PAL,
251 .audio_channels = 1,
252 .radio = 1,
253 .vbi = 1,
254 .tuner = 1,
255 .tuner_type = TUNER_PHILIPS_PAL,
256 .x_offset = 0,
257 .y_offset = 3,
258 .dvi_yuv_override = 1,
259 .dvi_yuv = 7,
260 .model_string = "ZTV ZT-721 2.4GHz USB A/V Receiver",
261 },
262 [HPG_WINTV_NTSC_MN] = {
263 .interface = -1,
264 .codec = CODEC_SAA7111,
265 .video_channels = 3,
266 .video_norm = V4L2_STD_NTSC,
267 .audio_channels = 1,
268 .radio = 0,
269 .vbi = 1,
270 .tuner = 1,
271 .tuner_type = TUNER_PHILIPS_NTSC_M,
272 .x_offset = -1,
273 .y_offset = 20,
274 .model_string = "Hauppauge WinTV USB (NTSC M/N)",
275 },
276 [HPG_WINTV_PAL_BG] = {
277 .interface = -1,
278 .codec = CODEC_SAA7111,
279 .video_channels = 3,
280 .video_norm = V4L2_STD_PAL,
281 .audio_channels = 1,
282 .radio = 0,
283 .vbi = 1,
284 .tuner = 1,
285 .tuner_type = TUNER_PHILIPS_PAL,
286 .x_offset = -1,
287 .y_offset = -1,
288 .model_string = "Hauppauge WinTV USB (PAL B/G)",
289 },
290 [HPG_WINTV_PAL_I] = {
291 .interface = -1,
292 .codec = CODEC_SAA7111,
293 .video_channels = 3,
294 .video_norm = V4L2_STD_PAL,
295 .audio_channels = 1,
296 .radio = 0,
297 .vbi = 1,
298 .tuner = 1,
299 .tuner_type = TUNER_PHILIPS_PAL,
300 .x_offset = -1,
301 .y_offset = -1,
302 .model_string = "Hauppauge WinTV USB (PAL I)",
303 },
304 [HPG_WINTV_PAL_SECAM_L] = {
305 .interface = -1,
306 .codec = CODEC_SAA7111,
307 .video_channels = 3,
308 .video_norm = V4L2_STD_SECAM,
309 .audio_channels = 1,
310 .radio = 0,
311 .vbi = 1,
312 .tuner = 1,
313 .tuner_type = TUNER_PHILIPS_SECAM,
314 .x_offset = 0x80,
315 .y_offset = 0x16,
316 .model_string = "Hauppauge WinTV USB (PAL/SECAM L)",
317 },
318 [HPG_WINTV_PAL_D_K] = {
319 .interface = -1,
320 .codec = CODEC_SAA7111,
321 .video_channels = 3,
322 .video_norm = V4L2_STD_PAL,
323 .audio_channels = 1,
324 .radio = 0,
325 .vbi = 1,
326 .tuner = 1,
327 .tuner_type = TUNER_PHILIPS_PAL,
328 .x_offset = -1,
329 .y_offset = -1,
330 .model_string = "Hauppauge WinTV USB (PAL D/K)",
331 },
332 [HPG_WINTV_NTSC_FM] = {
333 .interface = -1,
334 .codec = CODEC_SAA7111,
335 .video_channels = 3,
336 .video_norm = V4L2_STD_NTSC,
337 .audio_channels = 1,
338 .radio = 1,
339 .vbi = 1,
340 .tuner = 1,
341 .tuner_type = TUNER_PHILIPS_NTSC_M,
342 .x_offset = -1,
343 .y_offset = -1,
344 .model_string = "Hauppauge WinTV USB (NTSC FM)",
345 },
346 [HPG_WINTV_PAL_BG_FM] = {
347 .interface = -1,
348 .codec = CODEC_SAA7111,
349 .video_channels = 3,
350 .video_norm = V4L2_STD_PAL,
351 .audio_channels = 1,
352 .radio = 1,
353 .vbi = 1,
354 .tuner = 1,
355 .tuner_type = TUNER_PHILIPS_PAL,
356 .x_offset = -1,
357 .y_offset = -1,
358 .model_string = "Hauppauge WinTV USB (PAL B/G FM)",
359 },
360 [HPG_WINTV_PAL_I_FM] = {
361 .interface = -1,
362 .codec = CODEC_SAA7111,
363 .video_channels = 3,
364 .video_norm = V4L2_STD_PAL,
365 .audio_channels = 1,
366 .radio = 1,
367 .vbi = 1,
368 .tuner = 1,
369 .tuner_type = TUNER_PHILIPS_PAL,
370 .x_offset = -1,
371 .y_offset = -1,
372 .model_string = "Hauppauge WinTV USB (PAL I FM)",
373 },
374 [HPG_WINTV_PAL_D_K_FM] = {
375 .interface = -1,
376 .codec = CODEC_SAA7111,
377 .video_channels = 3,
378 .video_norm = V4L2_STD_PAL,
379 .audio_channels = 1,
380 .radio = 1,
381 .vbi = 1,
382 .tuner = 1,
383 .tuner_type = TUNER_PHILIPS_PAL,
384 .x_offset = -1,
385 .y_offset = -1,
386 .model_string = "Hauppauge WinTV USB (PAL D/K FM)",
387 },
388 [HPG_WINTV_PRO_NTSC_MN] = {
389 .interface = 0,
390 .codec = CODEC_SAA7113,
391 .video_channels = 3,
392 .video_norm = V4L2_STD_NTSC,
393 .audio_channels = 1,
394 .radio = 1,
395 .vbi = 1,
396 .tuner = 1,
397 .tuner_type = TUNER_MICROTUNE_4049FM5,
398 .x_offset = 0,
399 .y_offset = 3,
400 .dvi_yuv_override = 1,
401 .dvi_yuv = 7,
402 .model_string = "Hauppauge WinTV USB Pro (NTSC M/N)",
403 },
404 [HPG_WINTV_PRO_NTSC_MN_V2] = {
405 .interface = 0,
406 .codec = CODEC_SAA7113,
407 .video_channels = 3,
408 .video_norm = V4L2_STD_NTSC,
409 .audio_channels = 1,
410 .radio = 1,
411 .vbi = 1,
412 .tuner = 1,
413 .tuner_type = TUNER_MICROTUNE_4049FM5,
414 .x_offset = 0,
415 .y_offset = 3,
416 .dvi_yuv_override = 1,
417 .dvi_yuv = 7,
418 .model_string = "Hauppauge WinTV USB Pro (NTSC M/N) V2",
419 },
420 [HPG_WINTV_PRO_PAL] = {
421 .interface = 0,
422 .codec = CODEC_SAA7113,
423 .video_channels = 3,
424 .video_norm = V4L2_STD_PAL,
425 .audio_channels = 1,
426 .radio = 0,
427 .vbi = 1,
428 .tuner = 1,
429 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
430 .x_offset = 0,
431 .y_offset = 3,
432 .dvi_yuv_override = 1,
433 .dvi_yuv = 7,
434 .model_string = "Hauppauge WinTV USB Pro (PAL/SECAM B/G/I/D/K/L)",
435 },
436 [HPG_WINTV_PRO_NTSC_MN_V3] = {
437 .interface = 0,
438 .codec = CODEC_SAA7113,
439 .video_channels = 3,
440 .video_norm = V4L2_STD_NTSC,
441 .audio_channels = 1,
442 .radio = 1,
443 .vbi = 1,
444 .tuner = 1,
445 .tuner_type = TUNER_PHILIPS_NTSC_M,
446 .x_offset = 0,
447 .y_offset = 3,
448 .dvi_yuv_override = 1,
449 .dvi_yuv = 7,
450 .model_string = "Hauppauge WinTV USB Pro (NTSC M/N) V3",
451 },
452 [HPG_WINTV_PRO_PAL_BG] = {
453 .interface = 0,
454 .codec = CODEC_SAA7113,
455 .video_channels = 3,
456 .video_norm = V4L2_STD_PAL,
457 .audio_channels = 1,
458 .radio = 0,
459 .vbi = 1,
460 .tuner = 1,
461 .tuner_type = TUNER_PHILIPS_PAL,
462 .x_offset = 0,
463 .y_offset = 3,
464 .dvi_yuv_override = 1,
465 .dvi_yuv = 7,
466 .model_string = "Hauppauge WinTV USB Pro (PAL B/G)",
467 },
468 [HPG_WINTV_PRO_PAL_I] = {
469 .interface = 0,
470 .codec = CODEC_SAA7113,
471 .video_channels = 3,
472 .video_norm = V4L2_STD_PAL,
473 .audio_channels = 1,
474 .radio = 0,
475 .vbi = 1,
476 .tuner = 1,
477 .tuner_type = TUNER_PHILIPS_PAL,
478 .x_offset = 0,
479 .y_offset = 3,
480 .dvi_yuv_override = 1,
481 .dvi_yuv = 7,
482 .model_string = "Hauppauge WinTV USB Pro (PAL I)",
483 },
484 [HPG_WINTV_PRO_PAL_SECAM_L] = {
485 .interface = -1,
486 .codec = CODEC_SAA7113,
487 .video_channels = 3,
488 .video_norm = V4L2_STD_SECAM,
489 .audio_channels = 1,
490 .radio = 0,
491 .vbi = 1,
492 .tuner = 1,
493 .tuner_type = TUNER_PHILIPS_SECAM,
494 .x_offset = 0,
495 .y_offset = 3,
496 .dvi_yuv_override = 1,
497 .dvi_yuv = 7,
498 .model_string = "Hauppauge WinTV USB Pro (PAL/SECAM L)",
499 },
500 [HPG_WINTV_PRO_PAL_D_K] = {
501 .interface = -1,
502 .codec = CODEC_SAA7113,
503 .video_channels = 3,
504 .video_norm = V4L2_STD_PAL,
505 .audio_channels = 1,
506 .radio = 0,
507 .vbi = 1,
508 .tuner = 1,
509 .tuner_type = TUNER_PHILIPS_PAL,
510 .x_offset = 0,
511 .y_offset = 3,
512 .dvi_yuv_override = 1,
513 .dvi_yuv = 7,
514 .model_string = "Hauppauge WinTV USB Pro (PAL D/K)",
515 },
516 [HPG_WINTV_PRO_PAL_SECAM] = {
517 .interface = -1,
518 .codec = CODEC_SAA7113,
519 .video_channels = 3,
520 .video_norm = V4L2_STD_SECAM,
521 .audio_channels = 1,
522 .radio = 0,
523 .vbi = 1,
524 .tuner = 1,
525 .tuner_type = TUNER_PHILIPS_SECAM,
526 .x_offset = 0,
527 .y_offset = 3,
528 .dvi_yuv_override = 1,
529 .dvi_yuv = 7,
530 .model_string = "Hauppauge WinTV USB Pro (PAL/SECAM BGDK/I/L)",
531 },
532 [HPG_WINTV_PRO_PAL_SECAM_V2] = {
533 .interface = -1,
534 .codec = CODEC_SAA7113,
535 .video_channels = 3,
536 .video_norm = V4L2_STD_SECAM,
537 .audio_channels = 1,
538 .radio = 0,
539 .vbi = 1,
540 .tuner = 1,
541 .tuner_type = TUNER_PHILIPS_SECAM,
542 .x_offset = 0,
543 .y_offset = 3,
544 .dvi_yuv_override = 1,
545 .dvi_yuv = 7,
546 .model_string = "Hauppauge WinTV USB Pro (PAL/SECAM BGDK/I/L) V2",
547 },
548 [HPG_WINTV_PRO_PAL_BG_V2] = {
549 .interface = -1,
550 .codec = CODEC_SAA7113,
551 .video_channels = 3,
552 .video_norm = V4L2_STD_PAL,
553 .audio_channels = 1,
554 .radio = 0,
555 .vbi = 1,
556 .tuner = 1,
557 .tuner_type = TUNER_ALPS_TSBE1_PAL,
558 .x_offset = 0,
559 .y_offset = 3,
560 .dvi_yuv_override = 1,
561 .dvi_yuv = 7,
562 .model_string = "Hauppauge WinTV USB Pro (PAL B/G) V2",
563 },
564 [HPG_WINTV_PRO_PAL_BG_D_K] = {
565 .interface = -1,
566 .codec = CODEC_SAA7113,
567 .video_channels = 3,
568 .video_norm = V4L2_STD_PAL,
569 .audio_channels = 1,
570 .radio = 0,
571 .vbi = 1,
572 .tuner = 1,
573 .tuner_type = TUNER_ALPS_TSBE1_PAL,
574 .x_offset = 0,
575 .y_offset = 3,
576 .dvi_yuv_override = 1,
577 .dvi_yuv = 7,
578 .model_string = "Hauppauge WinTV USB Pro (PAL B/G,D/K)",
579 },
580 [HPG_WINTV_PRO_PAL_I_D_K] = {
581 .interface = -1,
582 .codec = CODEC_SAA7113,
583 .video_channels = 3,
584 .video_norm = V4L2_STD_PAL,
585 .audio_channels = 1,
586 .radio = 0,
587 .vbi = 1,
588 .tuner = 1,
589 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
590 .x_offset = 0,
591 .y_offset = 3,
592 .dvi_yuv_override = 1,
593 .dvi_yuv = 7,
594 .model_string = "Hauppauge WinTV USB Pro (PAL I,D/K)",
595 },
596 [HPG_WINTV_PRO_NTSC_MN_FM] = {
597 .interface = -1,
598 .codec = CODEC_SAA7113,
599 .video_channels = 3,
600 .video_norm = V4L2_STD_NTSC,
601 .audio_channels = 1,
602 .radio = 1,
603 .vbi = 1,
604 .tuner = 1,
605 .tuner_type = TUNER_PHILIPS_NTSC_M,
606 .x_offset = 0,
607 .y_offset = 3,
608 .dvi_yuv_override = 1,
609 .dvi_yuv = 7,
610 .model_string = "Hauppauge WinTV USB Pro (NTSC M/N FM)",
611 },
612 [HPG_WINTV_PRO_PAL_BG_FM] = {
613 .interface = 0,
614 .codec = CODEC_SAA7113,
615 .video_channels = 3,
616 .video_norm = V4L2_STD_PAL,
617 .audio_channels = 1,
618 .radio = 1,
619 .vbi = 1,
620 .tuner = 1,
621 .tuner_type = TUNER_PHILIPS_PAL,
622 .x_offset = 0,
623 .y_offset = 3,
624 .dvi_yuv_override = 1,
625 .dvi_yuv = 7,
626 .model_string = "Hauppauge WinTV USB Pro (PAL B/G FM)",
627 },
628 [HPG_WINTV_PRO_PAL_I_FM] = {
629 .interface = 0,
630 .codec = CODEC_SAA7113,
631 .video_channels = 3,
632 .video_norm = V4L2_STD_PAL,
633 .audio_channels = 1,
634 .radio = 1,
635 .vbi = 1,
636 .tuner = 1,
637 .tuner_type = TUNER_PHILIPS_PAL,
638 .x_offset = 0,
639 .y_offset = 3,
640 .dvi_yuv_override = 1,
641 .dvi_yuv = 7,
642 .model_string = "Hauppauge WinTV USB Pro (PAL I FM)",
643 },
644 [HPG_WINTV_PRO_PAL_D_K_FM] = {
645 .interface = 0,
646 .codec = CODEC_SAA7113,
647 .video_channels = 3,
648 .video_norm = V4L2_STD_PAL,
649 .audio_channels = 1,
650 .radio = 1,
651 .vbi = 1,
652 .tuner = 1,
653 .tuner_type = TUNER_PHILIPS_PAL,
654 .x_offset = 0,
655 .y_offset = 3,
656 .dvi_yuv_override = 1,
657 .dvi_yuv = 7,
658 .model_string = "Hauppauge WinTV USB Pro (PAL D/K FM)",
659 },
660 [HPG_WINTV_PRO_TEMIC_PAL_FM] = {
661 .interface = 0,
662 .codec = CODEC_SAA7113,
663 .video_channels = 3,
664 .video_norm = V4L2_STD_PAL,
665 .audio_channels = 1,
666 .radio = 1,
667 .vbi = 1,
668 .tuner = 1,
669 .tuner_type = TUNER_MICROTUNE_4049FM5,
670 .x_offset = 0,
671 .y_offset = 3,
672 .dvi_yuv_override = 1,
673 .dvi_yuv = 7,
674 .model_string = "Hauppauge WinTV USB Pro (Temic PAL/SECAM B/G/I/D/K/L FM)",
675 },
676 [HPG_WINTV_PRO_TEMIC_PAL_BG_FM] = {
677 .interface = 0,
678 .codec = CODEC_SAA7113,
679 .video_channels = 3,
680 .video_norm = V4L2_STD_PAL,
681 .audio_channels = 1,
682 .radio = 1,
683 .vbi = 1,
684 .tuner = 1,
685 .tuner_type = TUNER_MICROTUNE_4049FM5,
686 .x_offset = 0,
687 .y_offset = 3,
688 .dvi_yuv_override = 1,
689 .dvi_yuv = 7,
690 .model_string = "Hauppauge WinTV USB Pro (Temic PAL B/G FM)",
691 },
692 [HPG_WINTV_PRO_PAL_FM] = {
693 .interface = 0,
694 .codec = CODEC_SAA7113,
695 .video_channels = 3,
696 .video_norm = V4L2_STD_PAL,
697 .audio_channels = 1,
698 .radio = 1,
699 .vbi = 1,
700 .tuner = 1,
701 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
702 .x_offset = 0,
703 .y_offset = 3,
704 .dvi_yuv_override = 1,
705 .dvi_yuv = 7,
706 .model_string = "Hauppauge WinTV USB Pro (PAL/SECAM B/G/I/D/K/L FM)",
707 },
708 [HPG_WINTV_PRO_NTSC_MN_FM_V2] = {
709 .interface = 0,
710 .codec = CODEC_SAA7113,
711 .video_channels = 3,
712 .video_norm = V4L2_STD_NTSC,
713 .audio_channels = 1,
714 .radio = 1,
715 .vbi = 1,
716 .tuner = 1,
717 .tuner_type = TUNER_PHILIPS_NTSC_M,
718 .x_offset = 0,
719 .y_offset = 3,
720 .dvi_yuv_override = 1,
721 .dvi_yuv = 7,
722 .model_string = "Hauppauge WinTV USB Pro (NTSC M/N FM) V2",
723 },
724 [CAMTEL_TVB330] = {
725 .interface = -1,
726 .codec = CODEC_SAA7113,
727 .video_channels = 3,
728 .video_norm = V4L2_STD_NTSC,
729 .audio_channels = 1,
730 .radio = 1,
731 .vbi = 1,
732 .tuner = 1,
733 .tuner_type = TUNER_PHILIPS_NTSC_M,
734 .x_offset = 5,
735 .y_offset = 5,
736 .model_string = "Camtel Technology USB TV Genie Pro FM Model TVB330",
737 },
738 [DIGITAL_VIDEO_CREATOR_I] = {
739 .interface = -1,
740 .codec = CODEC_SAA7113,
741 .video_channels = 2,
742 .video_norm = V4L2_STD_PAL,
743 .audio_channels = 0,
744 .radio = 0,
745 .vbi = 1,
746 .tuner = 0,
747 .tuner_type = 0,
748 .x_offset = 0,
749 .y_offset = 3,
750 .dvi_yuv_override = 1,
751 .dvi_yuv = 7,
752 .model_string = "Digital Video Creator I",
753 },
754 [GLOBAL_VILLAGE_GV_007_NTSC] = {
755 .interface = -1,
756 .codec = CODEC_SAA7111,
757 .video_channels = 2,
758 .video_norm = V4L2_STD_NTSC,
759 .audio_channels = 0,
760 .radio = 0,
761 .vbi = 1,
762 .tuner = 0,
763 .tuner_type = 0,
764 .x_offset = 82,
765 .y_offset = 20,
766 .dvi_yuv_override = 1,
767 .dvi_yuv = 7,
768 .model_string = "Global Village GV-007 (NTSC)",
769 },
770 [DAZZLE_DVC_50_REV_1_NTSC] = {
771 .interface = 0,
772 .codec = CODEC_SAA7113,
773 .video_channels = 2,
774 .video_norm = V4L2_STD_NTSC,
775 .audio_channels = 0,
776 .radio = 0,
777 .vbi = 1,
778 .tuner = 0,
779 .tuner_type = 0,
780 .x_offset = 0,
781 .y_offset = 3,
782 .dvi_yuv_override = 1,
783 .dvi_yuv = 7,
784 .model_string = "Dazzle Fusion Model DVC-50 Rev 1 (NTSC)",
785 },
786 [DAZZLE_DVC_80_REV_1_PAL] = {
787 .interface = 0,
788 .codec = CODEC_SAA7113,
789 .video_channels = 2,
790 .video_norm = V4L2_STD_PAL,
791 .audio_channels = 0,
792 .radio = 0,
793 .vbi = 1,
794 .tuner = 0,
795 .tuner_type = 0,
796 .x_offset = 0,
797 .y_offset = 3,
798 .dvi_yuv_override = 1,
799 .dvi_yuv = 7,
800 .model_string = "Dazzle Fusion Model DVC-80 Rev 1 (PAL)",
801 },
802 [DAZZLE_DVC_90_REV_1_SECAM] = {
803 .interface = 0,
804 .codec = CODEC_SAA7113,
805 .video_channels = 2,
806 .video_norm = V4L2_STD_SECAM,
807 .audio_channels = 0,
808 .radio = 0,
809 .vbi = 1,
810 .tuner = 0,
811 .tuner_type = 0,
812 .x_offset = 0,
813 .y_offset = 3,
814 .dvi_yuv_override = 1,
815 .dvi_yuv = 7,
816 .model_string = "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)",
817 },
818 [ESKAPE_LABS_MYTV2GO] = {
819 .interface = 0,
820 .codec = CODEC_SAA7113,
821 .video_channels = 2,
822 .video_norm = V4L2_STD_PAL,
823 .audio_channels = 1,
824 .radio = 1,
825 .vbi = 1,
826 .tuner = 1,
827 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
828 .x_offset = 0,
829 .y_offset = 3,
830 .dvi_yuv_override = 1,
831 .dvi_yuv = 7,
832 .model_string = "Eskape Labs MyTV2Go",
833 },
834 [PINNA_PCTV_USB_PAL] = {
835 .interface = -1,
836 .codec = CODEC_SAA7111,
837 .video_channels = 3,
838 .video_norm = V4L2_STD_PAL,
839 .audio_channels = 1,
840 .radio = 0,
841 .vbi = 0,
842 .tuner = 1,
843 .tuner_type = TUNER_TEMIC_4066FY5_PAL_I,
844 .x_offset = -1,
845 .y_offset = -1,
846 .model_string = "Pinnacle Studio PCTV USB (PAL)",
847 },
848 [PINNA_PCTV_USB_SECAM] = {
849 .interface = -1,
850 .codec = CODEC_SAA7111,
851 .video_channels = 3,
852 .video_norm = V4L2_STD_SECAM,
853 .audio_channels = 1,
854 .radio = 0,
855 .vbi = 1,
856 .tuner = 1,
857 .tuner_type = TUNER_PHILIPS_SECAM,
858 .x_offset = -1,
859 .y_offset = -1,
860 .model_string = "Pinnacle Studio PCTV USB (SECAM)",
861 },
862 [PINNA_PCTV_USB_PAL_FM] = {
863 .interface = -1,
864 .codec = CODEC_SAA7111,
865 .video_channels = 3,
866 .video_norm = V4L2_STD_PAL,
867 .audio_channels = 1,
868 .radio = 1,
869 .vbi = 1,
870 .tuner = 1,
871 .tuner_type = TUNER_PHILIPS_PAL,
872 .x_offset = 128,
873 .y_offset = 23,
874 .model_string = "Pinnacle Studio PCTV USB (PAL) FM",
875 },
876 [MIRO_PCTV_USB] = {
877 .interface = -1,
878 .codec = CODEC_SAA7111,
879 .video_channels = 3,
880 .video_norm = V4L2_STD_PAL,
881 .audio_channels = 1,
882 .radio = 0,
883 .vbi = 1,
884 .tuner = 1,
885 .tuner_type = TUNER_PHILIPS_PAL,
886 .x_offset = -1,
887 .y_offset = -1,
888 .model_string = "Miro PCTV USB",
889 },
890 [PINNA_PCTV_USB_NTSC_FM] = {
891 .interface = -1,
892 .codec = CODEC_SAA7111,
893 .video_channels = 3,
894 .video_norm = V4L2_STD_NTSC,
895 .audio_channels = 1,
896 .radio = 1,
897 .vbi = 1,
898 .tuner = 1,
899 .tuner_type = TUNER_PHILIPS_NTSC_M,
900 .x_offset = -1,
901 .y_offset = -1,
902 .model_string = "Pinnacle Studio PCTV USB (NTSC) FM",
903 },
904 [PINNA_PCTV_USB_NTSC_FM_V3] = {
905 .interface = -1,
906 .codec = CODEC_SAA7111,
907 .video_channels = 3,
908 .video_norm = V4L2_STD_NTSC,
909 .audio_channels = 1,
910 .radio = 1,
911 .vbi = 1,
912 .tuner = 1,
913 .tuner_type = TUNER_PHILIPS_NTSC_M,
914 .x_offset = -1,
915 .y_offset = -1,
916 .model_string = "Pinnacle Studio PCTV USB (NTSC) FM V3",
917 },
918 [PINNA_PCTV_USB_PAL_FM_V2] = {
919 .interface = -1,
920 .codec = CODEC_SAA7113,
921 .video_channels = 3,
922 .video_norm = V4L2_STD_PAL,
923 .audio_channels = 1,
924 .radio = 1,
925 .vbi = 1,
926 .tuner = 1,
927 .tuner_type = TUNER_TEMIC_4009FR5_PAL,
928 .x_offset = 0,
929 .y_offset = 3,
930 .dvi_yuv_override = 1,
931 .dvi_yuv = 7,
932 .model_string = "Pinnacle Studio PCTV USB (PAL) FM V2",
933 },
934 [PINNA_PCTV_USB_NTSC_FM_V2] = {
935 .interface = -1,
936 .codec = CODEC_SAA7111,
937 .video_channels = 3,
938 .video_norm = V4L2_STD_NTSC,
939 .audio_channels = 1,
940 .radio = 1,
941 .vbi = 1,
942 .tuner = 1,
943 .tuner_type = TUNER_TEMIC_4039FR5_NTSC,
944 .x_offset = 0,
945 .y_offset = 3,
946 .dvi_yuv_override = 1,
947 .dvi_yuv = 7,
948 .model_string = "Pinnacle Studio PCTV USB (NTSC) FM V2",
949 },
950 [PINNA_PCTV_USB_PAL_FM_V3] = {
951 .interface = -1,
952 .codec = CODEC_SAA7113,
953 .video_channels = 3,
954 .video_norm = V4L2_STD_PAL,
955 .audio_channels = 1,
956 .radio = 1,
957 .vbi = 1,
958 .tuner = 1,
959 .tuner_type = TUNER_TEMIC_4009FR5_PAL,
960 .x_offset = 0,
961 .y_offset = 3,
962 .dvi_yuv_override = 1,
963 .dvi_yuv = 7,
964 .model_string = "Pinnacle Studio PCTV USB (PAL) FM V3",
965 },
966 [PINNA_LINX_VD_IN_CAB_NTSC] = {
967 .interface = -1,
968 .codec = CODEC_SAA7113,
969 .video_channels = 2,
970 .video_norm = V4L2_STD_NTSC,
971 .audio_channels = 1,
972 .radio = 0,
973 .vbi = 1,
974 .tuner = 0,
975 .tuner_type = 0,
976 .x_offset = 0,
977 .y_offset = 3,
978 .dvi_yuv_override = 1,
979 .dvi_yuv = 7,
980 .model_string = "Pinnacle Studio Linx Video input cable (NTSC)",
981 },
982 [PINNA_LINX_VD_IN_CAB_PAL] = {
983 .interface = -1,
984 .codec = CODEC_SAA7113,
985 .video_channels = 2,
986 .video_norm = V4L2_STD_PAL,
987 .audio_channels = 1,
988 .radio = 0,
989 .vbi = 1,
990 .tuner = 0,
991 .tuner_type = 0,
992 .x_offset = 0,
993 .y_offset = 3,
994 .dvi_yuv_override = 1,
995 .dvi_yuv = 7,
996 .model_string = "Pinnacle Studio Linx Video input cable (PAL)",
997 },
998 [PINNA_PCTV_BUNGEE_PAL_FM] = {
999 .interface = -1,
1000 .codec = CODEC_SAA7113,
1001 .video_channels = 3,
1002 .video_norm = V4L2_STD_PAL,
1003 .audio_channels = 1,
1004 .radio = 1,
1005 .vbi = 1,
1006 .tuner = 1,
1007 .tuner_type = TUNER_TEMIC_4009FR5_PAL,
1008 .x_offset = 0,
1009 .y_offset = 3,
1010 .dvi_yuv_override = 1,
1011 .dvi_yuv = 7,
1012 .model_string = "Pinnacle PCTV Bungee USB (PAL) FM",
1013 },
1014 [HPG_WINTV] = {
1015 .interface = -1,
1016 .codec = CODEC_SAA7111,
1017 .video_channels = 3,
1018 .video_norm = V4L2_STD_NTSC,
1019 .audio_channels = 1,
1020 .radio = 0,
1021 .vbi = 1,
1022 .tuner = 1,
1023 .tuner_type = TUNER_PHILIPS_NTSC_M,
1024 .x_offset = -1,
1025 .y_offset = -1,
1026 .model_string = "Hauppauge WinTv-USB",
1027 },
1028 [MICROCAM_NTSC] = {
1029 .interface = -1,
1030 .codec = CODEC_WEBCAM,
1031 .video_channels = 1,
1032 .video_norm = V4L2_STD_NTSC,
1033 .audio_channels = 0,
1034 .radio = 0,
1035 .vbi = 0,
1036 .tuner = 0,
1037 .tuner_type = 0,
1038 .x_offset = 71,
1039 .y_offset = 15,
1040 .model_string = "Nogatech USB MicroCam NTSC (NV3000N)",
1041 },
1042 [MICROCAM_PAL] = {
1043 .interface = -1,
1044 .codec = CODEC_WEBCAM,
1045 .video_channels = 1,
1046 .video_norm = V4L2_STD_PAL,
1047 .audio_channels = 0,
1048 .radio = 0,
1049 .vbi = 0,
1050 .tuner = 0,
1051 .tuner_type = 0,
1052 .x_offset = 71,
1053 .y_offset = 18,
1054 .model_string = "Nogatech USB MicroCam PAL (NV3001P)",
1055 },
1056};
1057const int usbvision_device_data_size = ARRAY_SIZE(usbvision_device_data);
1058
1059/* Supported Devices */
1060
1061struct usb_device_id usbvision_table[] = {
1062 { USB_DEVICE(0x0a6f, 0x0400), .driver_info = XANBOO },
1063 { USB_DEVICE(0x050d, 0x0106), .driver_info = BELKIN_VIDEOBUS_II },
1064 { USB_DEVICE(0x050d, 0x0207), .driver_info = BELKIN_VIDEOBUS },
1065 { USB_DEVICE(0x050d, 0x0208), .driver_info = BELKIN_USB_VIDEOBUS_II },
1066 { USB_DEVICE(0x0571, 0x0002), .driver_info = ECHOFX_INTERVIEW_LITE },
1067 { USB_DEVICE(0x0573, 0x0003), .driver_info = USBGEAR_USBG_V1 },
1068 { USB_DEVICE(0x0573, 0x0400), .driver_info = D_LINK_V100 },
1069 { USB_DEVICE(0x0573, 0x2000), .driver_info = X10_USB_CAMERA },
1070 { USB_DEVICE(0x0573, 0x2d00), .driver_info = HPG_WINTV_LIVE_PAL_BG },
1071 { USB_DEVICE(0x0573, 0x2d01), .driver_info = HPG_WINTV_LIVE_PRO_NTSC_MN },
1072 { USB_DEVICE(0x0573, 0x2101), .driver_info = ZORAN_PMD_NOGATECH },
1073 { USB_DEVICE(0x0573, 0x3000), .driver_info = MICROCAM_NTSC },
1074 { USB_DEVICE(0x0573, 0x3001), .driver_info = MICROCAM_PAL },
1075 { USB_DEVICE(0x0573, 0x4100), .driver_info = NOGATECH_USB_TV_NTSC_FM },
1076 { USB_DEVICE(0x0573, 0x4110), .driver_info = PNY_USB_TV_NTSC_FM },
1077 { USB_DEVICE(0x0573, 0x4450), .driver_info = PV_PLAYTV_USB_PRO_PAL_FM },
1078 { USB_DEVICE(0x0573, 0x4550), .driver_info = ZT_721 },
1079 { USB_DEVICE(0x0573, 0x4d00), .driver_info = HPG_WINTV_NTSC_MN },
1080 { USB_DEVICE(0x0573, 0x4d01), .driver_info = HPG_WINTV_PAL_BG },
1081 { USB_DEVICE(0x0573, 0x4d02), .driver_info = HPG_WINTV_PAL_I },
1082 { USB_DEVICE(0x0573, 0x4d03), .driver_info = HPG_WINTV_PAL_SECAM_L },
1083 { USB_DEVICE(0x0573, 0x4d04), .driver_info = HPG_WINTV_PAL_D_K },
1084 { USB_DEVICE(0x0573, 0x4d10), .driver_info = HPG_WINTV_NTSC_FM },
1085 { USB_DEVICE(0x0573, 0x4d11), .driver_info = HPG_WINTV_PAL_BG_FM },
1086 { USB_DEVICE(0x0573, 0x4d12), .driver_info = HPG_WINTV_PAL_I_FM },
1087 { USB_DEVICE(0x0573, 0x4d14), .driver_info = HPG_WINTV_PAL_D_K_FM },
1088 { USB_DEVICE(0x0573, 0x4d2a), .driver_info = HPG_WINTV_PRO_NTSC_MN },
1089 { USB_DEVICE(0x0573, 0x4d2b), .driver_info = HPG_WINTV_PRO_NTSC_MN_V2 },
1090 { USB_DEVICE(0x0573, 0x4d2c), .driver_info = HPG_WINTV_PRO_PAL },
1091 { USB_DEVICE(0x0573, 0x4d20), .driver_info = HPG_WINTV_PRO_NTSC_MN_V3 },
1092 { USB_DEVICE(0x0573, 0x4d21), .driver_info = HPG_WINTV_PRO_PAL_BG },
1093 { USB_DEVICE(0x0573, 0x4d22), .driver_info = HPG_WINTV_PRO_PAL_I },
1094 { USB_DEVICE(0x0573, 0x4d23), .driver_info = HPG_WINTV_PRO_PAL_SECAM_L },
1095 { USB_DEVICE(0x0573, 0x4d24), .driver_info = HPG_WINTV_PRO_PAL_D_K },
1096 { USB_DEVICE(0x0573, 0x4d25), .driver_info = HPG_WINTV_PRO_PAL_SECAM },
1097 { USB_DEVICE(0x0573, 0x4d26), .driver_info = HPG_WINTV_PRO_PAL_SECAM_V2 },
1098 { USB_DEVICE(0x0573, 0x4d27), .driver_info = HPG_WINTV_PRO_PAL_BG_V2 },
1099 { USB_DEVICE(0x0573, 0x4d28), .driver_info = HPG_WINTV_PRO_PAL_BG_D_K },
1100 { USB_DEVICE(0x0573, 0x4d29), .driver_info = HPG_WINTV_PRO_PAL_I_D_K },
1101 { USB_DEVICE(0x0573, 0x4d30), .driver_info = HPG_WINTV_PRO_NTSC_MN_FM },
1102 { USB_DEVICE(0x0573, 0x4d31), .driver_info = HPG_WINTV_PRO_PAL_BG_FM },
1103 { USB_DEVICE(0x0573, 0x4d32), .driver_info = HPG_WINTV_PRO_PAL_I_FM },
1104 { USB_DEVICE(0x0573, 0x4d34), .driver_info = HPG_WINTV_PRO_PAL_D_K_FM },
1105 { USB_DEVICE(0x0573, 0x4d35), .driver_info = HPG_WINTV_PRO_TEMIC_PAL_FM },
1106 { USB_DEVICE(0x0573, 0x4d36), .driver_info = HPG_WINTV_PRO_TEMIC_PAL_BG_FM },
1107 { USB_DEVICE(0x0573, 0x4d37), .driver_info = HPG_WINTV_PRO_PAL_FM },
1108 { USB_DEVICE(0x0573, 0x4d38), .driver_info = HPG_WINTV_PRO_NTSC_MN_FM_V2 },
1109 { USB_DEVICE(0x0768, 0x0006), .driver_info = CAMTEL_TVB330 },
1110 { USB_DEVICE(0x07d0, 0x0001), .driver_info = DIGITAL_VIDEO_CREATOR_I },
1111 { USB_DEVICE(0x07d0, 0x0002), .driver_info = GLOBAL_VILLAGE_GV_007_NTSC },
1112 { USB_DEVICE(0x07d0, 0x0003), .driver_info = DAZZLE_DVC_50_REV_1_NTSC },
1113 { USB_DEVICE(0x07d0, 0x0004), .driver_info = DAZZLE_DVC_80_REV_1_PAL },
1114 { USB_DEVICE(0x07d0, 0x0005), .driver_info = DAZZLE_DVC_90_REV_1_SECAM },
1115 { USB_DEVICE(0x07f8, 0x9104), .driver_info = ESKAPE_LABS_MYTV2GO },
1116 { USB_DEVICE(0x2304, 0x010d), .driver_info = PINNA_PCTV_USB_PAL },
1117 { USB_DEVICE(0x2304, 0x0109), .driver_info = PINNA_PCTV_USB_SECAM },
1118 { USB_DEVICE(0x2304, 0x0110), .driver_info = PINNA_PCTV_USB_PAL_FM },
1119 { USB_DEVICE(0x2304, 0x0111), .driver_info = MIRO_PCTV_USB },
1120 { USB_DEVICE(0x2304, 0x0112), .driver_info = PINNA_PCTV_USB_NTSC_FM },
1121 { USB_DEVICE(0x2304, 0x0113), .driver_info = PINNA_PCTV_USB_NTSC_FM_V3 },
1122 { USB_DEVICE(0x2304, 0x0210), .driver_info = PINNA_PCTV_USB_PAL_FM_V2 },
1123 { USB_DEVICE(0x2304, 0x0212), .driver_info = PINNA_PCTV_USB_NTSC_FM_V2 },
1124 { USB_DEVICE(0x2304, 0x0214), .driver_info = PINNA_PCTV_USB_PAL_FM_V3 },
1125 { USB_DEVICE(0x2304, 0x0300), .driver_info = PINNA_LINX_VD_IN_CAB_NTSC },
1126 { USB_DEVICE(0x2304, 0x0301), .driver_info = PINNA_LINX_VD_IN_CAB_PAL },
1127 { USB_DEVICE(0x2304, 0x0419), .driver_info = PINNA_PCTV_BUNGEE_PAL_FM },
1128 { USB_DEVICE(0x2400, 0x4200), .driver_info = HPG_WINTV },
1129 { }, /* terminate list */
1130};
1131
1132MODULE_DEVICE_TABLE(usb, usbvision_table);
diff --git a/drivers/media/video/usbvision/usbvision-cards.h b/drivers/media/video/usbvision/usbvision-cards.h
new file mode 100644
index 00000000000..a51cc1185cc
--- /dev/null
+++ b/drivers/media/video/usbvision/usbvision-cards.h
@@ -0,0 +1,69 @@
1#define XANBOO 0
2#define BELKIN_VIDEOBUS_II 1
3#define BELKIN_VIDEOBUS 2
4#define BELKIN_USB_VIDEOBUS_II 3
5#define ECHOFX_INTERVIEW_LITE 4
6#define USBGEAR_USBG_V1 5
7#define D_LINK_V100 6
8#define X10_USB_CAMERA 7
9#define HPG_WINTV_LIVE_PAL_BG 8
10#define HPG_WINTV_LIVE_PRO_NTSC_MN 9
11#define ZORAN_PMD_NOGATECH 10
12#define NOGATECH_USB_TV_NTSC_FM 11
13#define PNY_USB_TV_NTSC_FM 12
14#define PV_PLAYTV_USB_PRO_PAL_FM 13
15#define ZT_721 14
16#define HPG_WINTV_NTSC_MN 15
17#define HPG_WINTV_PAL_BG 16
18#define HPG_WINTV_PAL_I 17
19#define HPG_WINTV_PAL_SECAM_L 18
20#define HPG_WINTV_PAL_D_K 19
21#define HPG_WINTV_NTSC_FM 20
22#define HPG_WINTV_PAL_BG_FM 21
23#define HPG_WINTV_PAL_I_FM 22
24#define HPG_WINTV_PAL_D_K_FM 23
25#define HPG_WINTV_PRO_NTSC_MN 24
26#define HPG_WINTV_PRO_NTSC_MN_V2 25
27#define HPG_WINTV_PRO_PAL 26
28#define HPG_WINTV_PRO_NTSC_MN_V3 27
29#define HPG_WINTV_PRO_PAL_BG 28
30#define HPG_WINTV_PRO_PAL_I 29
31#define HPG_WINTV_PRO_PAL_SECAM_L 30
32#define HPG_WINTV_PRO_PAL_D_K 31
33#define HPG_WINTV_PRO_PAL_SECAM 32
34#define HPG_WINTV_PRO_PAL_SECAM_V2 33
35#define HPG_WINTV_PRO_PAL_BG_V2 34
36#define HPG_WINTV_PRO_PAL_BG_D_K 35
37#define HPG_WINTV_PRO_PAL_I_D_K 36
38#define HPG_WINTV_PRO_NTSC_MN_FM 37
39#define HPG_WINTV_PRO_PAL_BG_FM 38
40#define HPG_WINTV_PRO_PAL_I_FM 39
41#define HPG_WINTV_PRO_PAL_D_K_FM 40
42#define HPG_WINTV_PRO_TEMIC_PAL_FM 41
43#define HPG_WINTV_PRO_TEMIC_PAL_BG_FM 42
44#define HPG_WINTV_PRO_PAL_FM 43
45#define HPG_WINTV_PRO_NTSC_MN_FM_V2 44
46#define CAMTEL_TVB330 45
47#define DIGITAL_VIDEO_CREATOR_I 46
48#define GLOBAL_VILLAGE_GV_007_NTSC 47
49#define DAZZLE_DVC_50_REV_1_NTSC 48
50#define DAZZLE_DVC_80_REV_1_PAL 49
51#define DAZZLE_DVC_90_REV_1_SECAM 50
52#define ESKAPE_LABS_MYTV2GO 51
53#define PINNA_PCTV_USB_PAL 52
54#define PINNA_PCTV_USB_SECAM 53
55#define PINNA_PCTV_USB_PAL_FM 54
56#define MIRO_PCTV_USB 55
57#define PINNA_PCTV_USB_NTSC_FM 56
58#define PINNA_PCTV_USB_PAL_FM_V2 57
59#define PINNA_PCTV_USB_NTSC_FM_V2 58
60#define PINNA_PCTV_USB_PAL_FM_V3 59
61#define PINNA_LINX_VD_IN_CAB_NTSC 60
62#define PINNA_LINX_VD_IN_CAB_PAL 61
63#define PINNA_PCTV_BUNGEE_PAL_FM 62
64#define HPG_WINTV 63
65#define PINNA_PCTV_USB_NTSC_FM_V3 64
66#define MICROCAM_NTSC 65
67#define MICROCAM_PAL 66
68
69extern const int usbvision_device_data_size;
diff --git a/drivers/media/video/usbvision/usbvision-core.c b/drivers/media/video/usbvision/usbvision-core.c
new file mode 100644
index 00000000000..f344411a457
--- /dev/null
+++ b/drivers/media/video/usbvision/usbvision-core.c
@@ -0,0 +1,2520 @@
1/*
2 * usbvision-core.c - driver for NT100x USB video capture devices
3 *
4 *
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
7 *
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/kernel.h>
27#include <linux/list.h>
28#include <linux/timer.h>
29#include <linux/gfp.h>
30#include <linux/mm.h>
31#include <linux/highmem.h>
32#include <linux/vmalloc.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/spinlock.h>
36#include <linux/io.h>
37#include <linux/videodev2.h>
38#include <linux/i2c.h>
39
40#include <media/saa7115.h>
41#include <media/v4l2-common.h>
42#include <media/tuner.h>
43
44#include <linux/workqueue.h>
45
46#include "usbvision.h"
47
48static unsigned int core_debug;
49module_param(core_debug, int, 0644);
50MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
51
52static int adjust_compression = 1; /* Set the compression to be adaptive */
53module_param(adjust_compression, int, 0444);
54MODULE_PARM_DESC(adjust_compression, " Set the ADPCM compression for the device. Default: 1 (On)");
55
56/* To help people with Black and White output with using s-video input.
57 * Some cables and input device are wired differently. */
58static int switch_svideo_input;
59module_param(switch_svideo_input, int, 0444);
60MODULE_PARM_DESC(switch_svideo_input, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
61
62static unsigned int adjust_x_offset = -1;
63module_param(adjust_x_offset, int, 0644);
64MODULE_PARM_DESC(adjust_x_offset, "adjust X offset display [core]");
65
66static unsigned int adjust_y_offset = -1;
67module_param(adjust_y_offset, int, 0644);
68MODULE_PARM_DESC(adjust_y_offset, "adjust Y offset display [core]");
69
70
71#define ENABLE_HEXDUMP 0 /* Enable if you need it */
72
73
74#ifdef USBVISION_DEBUG
75 #define PDEBUG(level, fmt, args...) { \
76 if (core_debug & (level)) \
77 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
78 __func__, __LINE__ , ## args); \
79 }
80#else
81 #define PDEBUG(level, fmt, args...) do {} while (0)
82#endif
83
84#define DBG_HEADER (1 << 0)
85#define DBG_IRQ (1 << 1)
86#define DBG_ISOC (1 << 2)
87#define DBG_PARSE (1 << 3)
88#define DBG_SCRATCH (1 << 4)
89#define DBG_FUNC (1 << 5)
90
91static const int max_imgwidth = MAX_FRAME_WIDTH;
92static const int max_imgheight = MAX_FRAME_HEIGHT;
93static const int min_imgwidth = MIN_FRAME_WIDTH;
94static const int min_imgheight = MIN_FRAME_HEIGHT;
95
96/* The value of 'scratch_buf_size' affects quality of the picture
97 * in many ways. Shorter buffers may cause loss of data when client
98 * is too slow. Larger buffers are memory-consuming and take longer
99 * to work with. This setting can be adjusted, but the default value
100 * should be OK for most desktop users.
101 */
102#define DEFAULT_SCRATCH_BUF_SIZE (0x20000) /* 128kB memory scratch buffer */
103static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
104
105/* Function prototypes */
106static int usbvision_request_intra(struct usb_usbvision *usbvision);
107static int usbvision_unrequest_intra(struct usb_usbvision *usbvision);
108static int usbvision_adjust_compression(struct usb_usbvision *usbvision);
109static int usbvision_measure_bandwidth(struct usb_usbvision *usbvision);
110
111/*******************************/
112/* Memory management functions */
113/*******************************/
114
115/*
116 * Here we want the physical address of the memory.
117 * This is used when initializing the contents of the area.
118 */
119
120static void *usbvision_rvmalloc(unsigned long size)
121{
122 void *mem;
123 unsigned long adr;
124
125 size = PAGE_ALIGN(size);
126 mem = vmalloc_32(size);
127 if (!mem)
128 return NULL;
129
130 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
131 adr = (unsigned long) mem;
132 while (size > 0) {
133 SetPageReserved(vmalloc_to_page((void *)adr));
134 adr += PAGE_SIZE;
135 size -= PAGE_SIZE;
136 }
137
138 return mem;
139}
140
141static void usbvision_rvfree(void *mem, unsigned long size)
142{
143 unsigned long adr;
144
145 if (!mem)
146 return;
147
148 size = PAGE_ALIGN(size);
149
150 adr = (unsigned long) mem;
151 while ((long) size > 0) {
152 ClearPageReserved(vmalloc_to_page((void *)adr));
153 adr += PAGE_SIZE;
154 size -= PAGE_SIZE;
155 }
156
157 vfree(mem);
158}
159
160
161#if ENABLE_HEXDUMP
162static void usbvision_hexdump(const unsigned char *data, int len)
163{
164 char tmp[80];
165 int i, k;
166
167 for (i = k = 0; len > 0; i++, len--) {
168 if (i > 0 && (i % 16 == 0)) {
169 printk("%s\n", tmp);
170 k = 0;
171 }
172 k += sprintf(&tmp[k], "%02x ", data[i]);
173 }
174 if (k > 0)
175 printk(KERN_CONT "%s\n", tmp);
176}
177#endif
178
179/********************************
180 * scratch ring buffer handling
181 ********************************/
182static int scratch_len(struct usb_usbvision *usbvision) /* This returns the amount of data actually in the buffer */
183{
184 int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
185
186 if (len < 0)
187 len += scratch_buf_size;
188 PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len);
189
190 return len;
191}
192
193
194/* This returns the free space left in the buffer */
195static int scratch_free(struct usb_usbvision *usbvision)
196{
197 int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr;
198 if (free <= 0)
199 free += scratch_buf_size;
200 if (free) {
201 free -= 1; /* at least one byte in the buffer must */
202 /* left blank, otherwise there is no chance to differ between full and empty */
203 }
204 PDEBUG(DBG_SCRATCH, "return %d\n", free);
205
206 return free;
207}
208
209
210/* This puts data into the buffer */
211static int scratch_put(struct usb_usbvision *usbvision, unsigned char *data,
212 int len)
213{
214 int len_part;
215
216 if (usbvision->scratch_write_ptr + len < scratch_buf_size) {
217 memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len);
218 usbvision->scratch_write_ptr += len;
219 } else {
220 len_part = scratch_buf_size - usbvision->scratch_write_ptr;
221 memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part);
222 if (len == len_part) {
223 usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */
224 } else {
225 memcpy(usbvision->scratch, data + len_part, len - len_part);
226 usbvision->scratch_write_ptr = len - len_part;
227 }
228 }
229
230 PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr);
231
232 return len;
233}
234
235/* This marks the write_ptr as position of new frame header */
236static void scratch_mark_header(struct usb_usbvision *usbvision)
237{
238 PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr);
239
240 usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] =
241 usbvision->scratch_write_ptr;
242 usbvision->scratch_headermarker_write_ptr += 1;
243 usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER;
244}
245
246/* This gets data from the buffer at the given "ptr" position */
247static int scratch_get_extra(struct usb_usbvision *usbvision,
248 unsigned char *data, int *ptr, int len)
249{
250 int len_part;
251
252 if (*ptr + len < scratch_buf_size) {
253 memcpy(data, usbvision->scratch + *ptr, len);
254 *ptr += len;
255 } else {
256 len_part = scratch_buf_size - *ptr;
257 memcpy(data, usbvision->scratch + *ptr, len_part);
258 if (len == len_part) {
259 *ptr = 0; /* just set the y_ptr to zero */
260 } else {
261 memcpy(data + len_part, usbvision->scratch, len - len_part);
262 *ptr = len - len_part;
263 }
264 }
265
266 PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr);
267
268 return len;
269}
270
271
272/* This sets the scratch extra read pointer */
273static void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr,
274 int len)
275{
276 *ptr = (usbvision->scratch_read_ptr + len) % scratch_buf_size;
277
278 PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
279}
280
281
282/* This increments the scratch extra read pointer */
283static void scratch_inc_extra_ptr(int *ptr, int len)
284{
285 *ptr = (*ptr + len) % scratch_buf_size;
286
287 PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
288}
289
290
291/* This gets data from the buffer */
292static int scratch_get(struct usb_usbvision *usbvision, unsigned char *data,
293 int len)
294{
295 int len_part;
296
297 if (usbvision->scratch_read_ptr + len < scratch_buf_size) {
298 memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len);
299 usbvision->scratch_read_ptr += len;
300 } else {
301 len_part = scratch_buf_size - usbvision->scratch_read_ptr;
302 memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part);
303 if (len == len_part) {
304 usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */
305 } else {
306 memcpy(data + len_part, usbvision->scratch, len - len_part);
307 usbvision->scratch_read_ptr = len - len_part;
308 }
309 }
310
311 PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr);
312
313 return len;
314}
315
316
317/* This sets read pointer to next header and returns it */
318static int scratch_get_header(struct usb_usbvision *usbvision,
319 struct usbvision_frame_header *header)
320{
321 int err_code = 0;
322
323 PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr);
324
325 while (usbvision->scratch_headermarker_write_ptr -
326 usbvision->scratch_headermarker_read_ptr != 0) {
327 usbvision->scratch_read_ptr =
328 usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr];
329 usbvision->scratch_headermarker_read_ptr += 1;
330 usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER;
331 scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH);
332 if ((header->magic_1 == USBVISION_MAGIC_1)
333 && (header->magic_2 == USBVISION_MAGIC_2)
334 && (header->header_length == USBVISION_HEADER_LENGTH)) {
335 err_code = USBVISION_HEADER_LENGTH;
336 header->frame_width = header->frame_width_lo + (header->frame_width_hi << 8);
337 header->frame_height = header->frame_height_lo + (header->frame_height_hi << 8);
338 break;
339 }
340 }
341
342 return err_code;
343}
344
345
346/* This removes len bytes of old data from the buffer */
347static void scratch_rm_old(struct usb_usbvision *usbvision, int len)
348{
349 usbvision->scratch_read_ptr += len;
350 usbvision->scratch_read_ptr %= scratch_buf_size;
351 PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr);
352}
353
354
355/* This resets the buffer - kills all data in it too */
356static void scratch_reset(struct usb_usbvision *usbvision)
357{
358 PDEBUG(DBG_SCRATCH, "\n");
359
360 usbvision->scratch_read_ptr = 0;
361 usbvision->scratch_write_ptr = 0;
362 usbvision->scratch_headermarker_read_ptr = 0;
363 usbvision->scratch_headermarker_write_ptr = 0;
364 usbvision->isocstate = isoc_state_no_frame;
365}
366
367int usbvision_scratch_alloc(struct usb_usbvision *usbvision)
368{
369 usbvision->scratch = vmalloc_32(scratch_buf_size);
370 scratch_reset(usbvision);
371 if (usbvision->scratch == NULL) {
372 dev_err(&usbvision->dev->dev,
373 "%s: unable to allocate %d bytes for scratch\n",
374 __func__, scratch_buf_size);
375 return -ENOMEM;
376 }
377 return 0;
378}
379
380void usbvision_scratch_free(struct usb_usbvision *usbvision)
381{
382 vfree(usbvision->scratch);
383 usbvision->scratch = NULL;
384}
385
386/*
387 * usbvision_decompress_alloc()
388 *
389 * allocates intermediate buffer for decompression
390 */
391int usbvision_decompress_alloc(struct usb_usbvision *usbvision)
392{
393 int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
394
395 usbvision->intra_frame_buffer = vmalloc_32(IFB_size);
396 if (usbvision->intra_frame_buffer == NULL) {
397 dev_err(&usbvision->dev->dev,
398 "%s: unable to allocate %d for compr. frame buffer\n",
399 __func__, IFB_size);
400 return -ENOMEM;
401 }
402 return 0;
403}
404
405/*
406 * usbvision_decompress_free()
407 *
408 * frees intermediate buffer for decompression
409 */
410void usbvision_decompress_free(struct usb_usbvision *usbvision)
411{
412 vfree(usbvision->intra_frame_buffer);
413 usbvision->intra_frame_buffer = NULL;
414
415}
416
417/************************************************************
418 * Here comes the data parsing stuff that is run as interrupt
419 ************************************************************/
420/*
421 * usbvision_find_header()
422 *
423 * Locate one of supported header markers in the scratch buffer.
424 */
425static enum parse_state usbvision_find_header(struct usb_usbvision *usbvision)
426{
427 struct usbvision_frame *frame;
428 int found_header = 0;
429
430 frame = usbvision->cur_frame;
431
432 while (scratch_get_header(usbvision, &frame->isoc_header) == USBVISION_HEADER_LENGTH) {
433 /* found header in scratch */
434 PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
435 frame->isoc_header.magic_2,
436 frame->isoc_header.magic_1,
437 frame->isoc_header.header_length,
438 frame->isoc_header.frame_num,
439 frame->isoc_header.frame_phase,
440 frame->isoc_header.frame_latency,
441 frame->isoc_header.data_format,
442 frame->isoc_header.format_param,
443 frame->isoc_header.frame_width,
444 frame->isoc_header.frame_height);
445
446 if (usbvision->request_intra) {
447 if (frame->isoc_header.format_param & 0x80) {
448 found_header = 1;
449 usbvision->last_isoc_frame_num = -1; /* do not check for lost frames this time */
450 usbvision_unrequest_intra(usbvision);
451 break;
452 }
453 } else {
454 found_header = 1;
455 break;
456 }
457 }
458
459 if (found_header) {
460 frame->frmwidth = frame->isoc_header.frame_width * usbvision->stretch_width;
461 frame->frmheight = frame->isoc_header.frame_height * usbvision->stretch_height;
462 frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth) >> 3;
463 } else { /* no header found */
464 PDEBUG(DBG_HEADER, "skipping scratch data, no header");
465 scratch_reset(usbvision);
466 return parse_state_end_parse;
467 }
468
469 /* found header */
470 if (frame->isoc_header.data_format == ISOC_MODE_COMPRESS) {
471 /* check isoc_header.frame_num for lost frames */
472 if (usbvision->last_isoc_frame_num >= 0) {
473 if (((usbvision->last_isoc_frame_num + 1) % 32) != frame->isoc_header.frame_num) {
474 /* unexpected frame drop: need to request new intra frame */
475 PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isoc_header.frame_num);
476 usbvision_request_intra(usbvision);
477 return parse_state_next_frame;
478 }
479 }
480 usbvision->last_isoc_frame_num = frame->isoc_header.frame_num;
481 }
482 usbvision->header_count++;
483 frame->scanstate = scan_state_lines;
484 frame->curline = 0;
485
486 return parse_state_continue;
487}
488
489static enum parse_state usbvision_parse_lines_422(struct usb_usbvision *usbvision,
490 long *pcopylen)
491{
492 volatile struct usbvision_frame *frame;
493 unsigned char *f;
494 int len;
495 int i;
496 unsigned char yuyv[4] = { 180, 128, 10, 128 }; /* YUV components */
497 unsigned char rv, gv, bv; /* RGB components */
498 int clipmask_index, bytes_per_pixel;
499 int stretch_bytes, clipmask_add;
500
501 frame = usbvision->cur_frame;
502 f = frame->data + (frame->v4l2_linesize * frame->curline);
503
504 /* Make sure there's enough data for the entire line */
505 len = (frame->isoc_header.frame_width * 2) + 5;
506 if (scratch_len(usbvision) < len) {
507 PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len);
508 return parse_state_out;
509 }
510
511 if ((frame->curline + 1) >= frame->frmheight)
512 return parse_state_next_frame;
513
514 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
515 stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
516 clipmask_index = frame->curline * MAX_FRAME_WIDTH;
517 clipmask_add = usbvision->stretch_width;
518
519 for (i = 0; i < frame->frmwidth; i += (2 * usbvision->stretch_width)) {
520 scratch_get(usbvision, &yuyv[0], 4);
521
522 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
523 *f++ = yuyv[0]; /* Y */
524 *f++ = yuyv[3]; /* U */
525 } else {
526 YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
527 switch (frame->v4l2_format.format) {
528 case V4L2_PIX_FMT_RGB565:
529 *f++ = (0x1F & rv) |
530 (0xE0 & (gv << 5));
531 *f++ = (0x07 & (gv >> 3)) |
532 (0xF8 & bv);
533 break;
534 case V4L2_PIX_FMT_RGB24:
535 *f++ = rv;
536 *f++ = gv;
537 *f++ = bv;
538 break;
539 case V4L2_PIX_FMT_RGB32:
540 *f++ = rv;
541 *f++ = gv;
542 *f++ = bv;
543 f++;
544 break;
545 case V4L2_PIX_FMT_RGB555:
546 *f++ = (0x1F & rv) |
547 (0xE0 & (gv << 5));
548 *f++ = (0x03 & (gv >> 3)) |
549 (0x7C & (bv << 2));
550 break;
551 }
552 }
553 clipmask_index += clipmask_add;
554 f += stretch_bytes;
555
556 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
557 *f++ = yuyv[2]; /* Y */
558 *f++ = yuyv[1]; /* V */
559 } else {
560 YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
561 switch (frame->v4l2_format.format) {
562 case V4L2_PIX_FMT_RGB565:
563 *f++ = (0x1F & rv) |
564 (0xE0 & (gv << 5));
565 *f++ = (0x07 & (gv >> 3)) |
566 (0xF8 & bv);
567 break;
568 case V4L2_PIX_FMT_RGB24:
569 *f++ = rv;
570 *f++ = gv;
571 *f++ = bv;
572 break;
573 case V4L2_PIX_FMT_RGB32:
574 *f++ = rv;
575 *f++ = gv;
576 *f++ = bv;
577 f++;
578 break;
579 case V4L2_PIX_FMT_RGB555:
580 *f++ = (0x1F & rv) |
581 (0xE0 & (gv << 5));
582 *f++ = (0x03 & (gv >> 3)) |
583 (0x7C & (bv << 2));
584 break;
585 }
586 }
587 clipmask_index += clipmask_add;
588 f += stretch_bytes;
589 }
590
591 frame->curline += usbvision->stretch_height;
592 *pcopylen += frame->v4l2_linesize * usbvision->stretch_height;
593
594 if (frame->curline >= frame->frmheight)
595 return parse_state_next_frame;
596 return parse_state_continue;
597}
598
599/* The decompression routine */
600static int usbvision_decompress(struct usb_usbvision *usbvision, unsigned char *compressed,
601 unsigned char *decompressed, int *start_pos,
602 int *block_typestart_pos, int len)
603{
604 int rest_pixel, idx, max_pos, pos, extra_pos, block_len, block_type_pos, block_type_len;
605 unsigned char block_byte, block_code, block_type, block_type_byte, integrator;
606
607 integrator = 0;
608 pos = *start_pos;
609 block_type_pos = *block_typestart_pos;
610 max_pos = 396; /* pos + len; */
611 extra_pos = pos;
612 block_len = 0;
613 block_byte = 0;
614 block_code = 0;
615 block_type = 0;
616 block_type_byte = 0;
617 block_type_len = 0;
618 rest_pixel = len;
619
620 for (idx = 0; idx < len; idx++) {
621 if (block_len == 0) {
622 if (block_type_len == 0) {
623 block_type_byte = compressed[block_type_pos];
624 block_type_pos++;
625 block_type_len = 4;
626 }
627 block_type = (block_type_byte & 0xC0) >> 6;
628
629 /* statistic: */
630 usbvision->compr_block_types[block_type]++;
631
632 pos = extra_pos;
633 if (block_type == 0) {
634 if (rest_pixel >= 24) {
635 idx += 23;
636 rest_pixel -= 24;
637 integrator = decompressed[idx];
638 } else {
639 idx += rest_pixel - 1;
640 rest_pixel = 0;
641 }
642 } else {
643 block_code = compressed[pos];
644 pos++;
645 if (rest_pixel >= 24)
646 block_len = 24;
647 else
648 block_len = rest_pixel;
649 rest_pixel -= block_len;
650 extra_pos = pos + (block_len / 4);
651 }
652 block_type_byte <<= 2;
653 block_type_len -= 1;
654 }
655 if (block_len > 0) {
656 if ((block_len % 4) == 0) {
657 block_byte = compressed[pos];
658 pos++;
659 }
660 if (block_type == 1) /* inter Block */
661 integrator = decompressed[idx];
662 switch (block_byte & 0xC0) {
663 case 0x03 << 6:
664 integrator += compressed[extra_pos];
665 extra_pos++;
666 break;
667 case 0x02 << 6:
668 integrator += block_code;
669 break;
670 case 0x00:
671 integrator -= block_code;
672 break;
673 }
674 decompressed[idx] = integrator;
675 block_byte <<= 2;
676 block_len -= 1;
677 }
678 }
679 *start_pos = extra_pos;
680 *block_typestart_pos = block_type_pos;
681 return idx;
682}
683
684
685/*
686 * usbvision_parse_compress()
687 *
688 * Parse compressed frame from the scratch buffer, put
689 * decoded RGB value into the current frame buffer and add the written
690 * number of bytes (RGB) to the *pcopylen.
691 *
692 */
693static enum parse_state usbvision_parse_compress(struct usb_usbvision *usbvision,
694 long *pcopylen)
695{
696#define USBVISION_STRIP_MAGIC 0x5A
697#define USBVISION_STRIP_LEN_MAX 400
698#define USBVISION_STRIP_HEADER_LEN 3
699
700 struct usbvision_frame *frame;
701 unsigned char *f, *u = NULL, *v = NULL;
702 unsigned char strip_data[USBVISION_STRIP_LEN_MAX];
703 unsigned char strip_header[USBVISION_STRIP_HEADER_LEN];
704 int idx, idx_end, strip_len, strip_ptr, startblock_pos, block_pos, block_type_pos;
705 int clipmask_index, bytes_per_pixel, rc;
706 int image_size;
707 unsigned char rv, gv, bv;
708 static unsigned char *Y, *U, *V;
709
710 frame = usbvision->cur_frame;
711 image_size = frame->frmwidth * frame->frmheight;
712 if ((frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
713 (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420)) { /* this is a planar format */
714 /* ... v4l2_linesize not used here. */
715 f = frame->data + (frame->width * frame->curline);
716 } else
717 f = frame->data + (frame->v4l2_linesize * frame->curline);
718
719 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { /* initialise u and v pointers */
720 /* get base of u and b planes add halfoffset */
721 u = frame->data
722 + image_size
723 + (frame->frmwidth >> 1) * frame->curline;
724 v = u + (image_size >> 1);
725 } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
726 v = frame->data + image_size + ((frame->curline * (frame->width)) >> 2);
727 u = v + (image_size >> 2);
728 }
729
730 if (frame->curline == 0)
731 usbvision_adjust_compression(usbvision);
732
733 if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN)
734 return parse_state_out;
735
736 /* get strip header without changing the scratch_read_ptr */
737 scratch_set_extra_ptr(usbvision, &strip_ptr, 0);
738 scratch_get_extra(usbvision, &strip_header[0], &strip_ptr,
739 USBVISION_STRIP_HEADER_LEN);
740
741 if (strip_header[0] != USBVISION_STRIP_MAGIC) {
742 /* wrong strip magic */
743 usbvision->strip_magic_errors++;
744 return parse_state_next_frame;
745 }
746
747 if (frame->curline != (int)strip_header[2]) {
748 /* line number mismatch error */
749 usbvision->strip_line_number_errors++;
750 }
751
752 strip_len = 2 * (unsigned int)strip_header[1];
753 if (strip_len > USBVISION_STRIP_LEN_MAX) {
754 /* strip overrun */
755 /* I think this never happens */
756 usbvision_request_intra(usbvision);
757 }
758
759 if (scratch_len(usbvision) < strip_len) {
760 /* there is not enough data for the strip */
761 return parse_state_out;
762 }
763
764 if (usbvision->intra_frame_buffer) {
765 Y = usbvision->intra_frame_buffer + frame->frmwidth * frame->curline;
766 U = usbvision->intra_frame_buffer + image_size + (frame->frmwidth / 2) * (frame->curline / 2);
767 V = usbvision->intra_frame_buffer + image_size / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2);
768 } else {
769 return parse_state_next_frame;
770 }
771
772 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
773 clipmask_index = frame->curline * MAX_FRAME_WIDTH;
774
775 scratch_get(usbvision, strip_data, strip_len);
776
777 idx_end = frame->frmwidth;
778 block_type_pos = USBVISION_STRIP_HEADER_LEN;
779 startblock_pos = block_type_pos + (idx_end - 1) / 96 + (idx_end / 2 - 1) / 96 + 2;
780 block_pos = startblock_pos;
781
782 usbvision->block_pos = block_pos;
783
784 rc = usbvision_decompress(usbvision, strip_data, Y, &block_pos, &block_type_pos, idx_end);
785 if (strip_len > usbvision->max_strip_len)
786 usbvision->max_strip_len = strip_len;
787
788 if (frame->curline % 2)
789 rc = usbvision_decompress(usbvision, strip_data, V, &block_pos, &block_type_pos, idx_end / 2);
790 else
791 rc = usbvision_decompress(usbvision, strip_data, U, &block_pos, &block_type_pos, idx_end / 2);
792
793 if (block_pos > usbvision->comprblock_pos)
794 usbvision->comprblock_pos = block_pos;
795 if (block_pos > strip_len)
796 usbvision->strip_len_errors++;
797
798 for (idx = 0; idx < idx_end; idx++) {
799 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
800 *f++ = Y[idx];
801 *f++ = idx & 0x01 ? U[idx / 2] : V[idx / 2];
802 } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) {
803 *f++ = Y[idx];
804 if (idx & 0x01)
805 *u++ = U[idx >> 1];
806 else
807 *v++ = V[idx >> 1];
808 } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
809 *f++ = Y[idx];
810 if (!((idx & 0x01) | (frame->curline & 0x01))) {
811 /* only need do this for 1 in 4 pixels */
812 /* intraframe buffer is YUV420 format */
813 *u++ = U[idx >> 1];
814 *v++ = V[idx >> 1];
815 }
816 } else {
817 YUV_TO_RGB_BY_THE_BOOK(Y[idx], U[idx / 2], V[idx / 2], rv, gv, bv);
818 switch (frame->v4l2_format.format) {
819 case V4L2_PIX_FMT_GREY:
820 *f++ = Y[idx];
821 break;
822 case V4L2_PIX_FMT_RGB555:
823 *f++ = (0x1F & rv) |
824 (0xE0 & (gv << 5));
825 *f++ = (0x03 & (gv >> 3)) |
826 (0x7C & (bv << 2));
827 break;
828 case V4L2_PIX_FMT_RGB565:
829 *f++ = (0x1F & rv) |
830 (0xE0 & (gv << 5));
831 *f++ = (0x07 & (gv >> 3)) |
832 (0xF8 & bv);
833 break;
834 case V4L2_PIX_FMT_RGB24:
835 *f++ = rv;
836 *f++ = gv;
837 *f++ = bv;
838 break;
839 case V4L2_PIX_FMT_RGB32:
840 *f++ = rv;
841 *f++ = gv;
842 *f++ = bv;
843 f++;
844 break;
845 }
846 }
847 clipmask_index++;
848 }
849 /* Deal with non-integer no. of bytes for YUV420P */
850 if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420)
851 *pcopylen += frame->v4l2_linesize;
852 else
853 *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
854
855 frame->curline += 1;
856
857 if (frame->curline >= frame->frmheight)
858 return parse_state_next_frame;
859 return parse_state_continue;
860
861}
862
863
864/*
865 * usbvision_parse_lines_420()
866 *
867 * Parse two lines from the scratch buffer, put
868 * decoded RGB value into the current frame buffer and add the written
869 * number of bytes (RGB) to the *pcopylen.
870 *
871 */
872static enum parse_state usbvision_parse_lines_420(struct usb_usbvision *usbvision,
873 long *pcopylen)
874{
875 struct usbvision_frame *frame;
876 unsigned char *f_even = NULL, *f_odd = NULL;
877 unsigned int pixel_per_line, block;
878 int pixel, block_split;
879 int y_ptr, u_ptr, v_ptr, y_odd_offset;
880 const int y_block_size = 128;
881 const int uv_block_size = 64;
882 const int sub_block_size = 32;
883 const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4;
884 const int uv_step[] = { 0, 0, 0, 4 }, uv_step_size = 4;
885 unsigned char y[2], u, v; /* YUV components */
886 int y_, u_, v_, vb, uvg, ur;
887 int r_, g_, b_; /* RGB components */
888 unsigned char g;
889 int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
890 int clipmask_add, stretch_bytes;
891
892 frame = usbvision->cur_frame;
893 f_even = frame->data + (frame->v4l2_linesize * frame->curline);
894 f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height;
895
896 /* Make sure there's enough data for the entire line */
897 /* In this mode usbvision transfer 3 bytes for every 2 pixels */
898 /* I need two lines to decode the color */
899 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
900 stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
901 clipmask_even_index = frame->curline * MAX_FRAME_WIDTH;
902 clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH;
903 clipmask_add = usbvision->stretch_width;
904 pixel_per_line = frame->isoc_header.frame_width;
905
906 if (scratch_len(usbvision) < (int)pixel_per_line * 3) {
907 /* printk(KERN_DEBUG "out of data, need %d\n", len); */
908 return parse_state_out;
909 }
910
911 if ((frame->curline + 1) >= frame->frmheight)
912 return parse_state_next_frame;
913
914 block_split = (pixel_per_line%y_block_size) ? 1 : 0; /* are some blocks splitted into different lines? */
915
916 y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size)
917 + block_split * uv_block_size;
918
919 scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset);
920 scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size);
921 scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset
922 + (4 - block_split) * sub_block_size);
923
924 for (block = 0; block < (pixel_per_line / sub_block_size); block++) {
925 for (pixel = 0; pixel < sub_block_size; pixel += 2) {
926 scratch_get(usbvision, &y[0], 2);
927 scratch_get_extra(usbvision, &u, &u_ptr, 1);
928 scratch_get_extra(usbvision, &v, &v_ptr, 1);
929
930 /* I don't use the YUV_TO_RGB macro for better performance */
931 v_ = v - 128;
932 u_ = u - 128;
933 vb = 132252 * v_;
934 uvg = -53281 * u_ - 25625 * v_;
935 ur = 104595 * u_;
936
937 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
938 *f_even++ = y[0];
939 *f_even++ = v;
940 } else {
941 y_ = 76284 * (y[0] - 16);
942
943 b_ = (y_ + vb) >> 16;
944 g_ = (y_ + uvg) >> 16;
945 r_ = (y_ + ur) >> 16;
946
947 switch (frame->v4l2_format.format) {
948 case V4L2_PIX_FMT_RGB565:
949 g = LIMIT_RGB(g_);
950 *f_even++ =
951 (0x1F & LIMIT_RGB(r_)) |
952 (0xE0 & (g << 5));
953 *f_even++ =
954 (0x07 & (g >> 3)) |
955 (0xF8 & LIMIT_RGB(b_));
956 break;
957 case V4L2_PIX_FMT_RGB24:
958 *f_even++ = LIMIT_RGB(r_);
959 *f_even++ = LIMIT_RGB(g_);
960 *f_even++ = LIMIT_RGB(b_);
961 break;
962 case V4L2_PIX_FMT_RGB32:
963 *f_even++ = LIMIT_RGB(r_);
964 *f_even++ = LIMIT_RGB(g_);
965 *f_even++ = LIMIT_RGB(b_);
966 f_even++;
967 break;
968 case V4L2_PIX_FMT_RGB555:
969 g = LIMIT_RGB(g_);
970 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
971 (0xE0 & (g << 5));
972 *f_even++ = (0x03 & (g >> 3)) |
973 (0x7C & (LIMIT_RGB(b_) << 2));
974 break;
975 }
976 }
977 clipmask_even_index += clipmask_add;
978 f_even += stretch_bytes;
979
980 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
981 *f_even++ = y[1];
982 *f_even++ = u;
983 } else {
984 y_ = 76284 * (y[1] - 16);
985
986 b_ = (y_ + vb) >> 16;
987 g_ = (y_ + uvg) >> 16;
988 r_ = (y_ + ur) >> 16;
989
990 switch (frame->v4l2_format.format) {
991 case V4L2_PIX_FMT_RGB565:
992 g = LIMIT_RGB(g_);
993 *f_even++ =
994 (0x1F & LIMIT_RGB(r_)) |
995 (0xE0 & (g << 5));
996 *f_even++ =
997 (0x07 & (g >> 3)) |
998 (0xF8 & LIMIT_RGB(b_));
999 break;
1000 case V4L2_PIX_FMT_RGB24:
1001 *f_even++ = LIMIT_RGB(r_);
1002 *f_even++ = LIMIT_RGB(g_);
1003 *f_even++ = LIMIT_RGB(b_);
1004 break;
1005 case V4L2_PIX_FMT_RGB32:
1006 *f_even++ = LIMIT_RGB(r_);
1007 *f_even++ = LIMIT_RGB(g_);
1008 *f_even++ = LIMIT_RGB(b_);
1009 f_even++;
1010 break;
1011 case V4L2_PIX_FMT_RGB555:
1012 g = LIMIT_RGB(g_);
1013 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1014 (0xE0 & (g << 5));
1015 *f_even++ = (0x03 & (g >> 3)) |
1016 (0x7C & (LIMIT_RGB(b_) << 2));
1017 break;
1018 }
1019 }
1020 clipmask_even_index += clipmask_add;
1021 f_even += stretch_bytes;
1022
1023 scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
1024
1025 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
1026 *f_odd++ = y[0];
1027 *f_odd++ = v;
1028 } else {
1029 y_ = 76284 * (y[0] - 16);
1030
1031 b_ = (y_ + vb) >> 16;
1032 g_ = (y_ + uvg) >> 16;
1033 r_ = (y_ + ur) >> 16;
1034
1035 switch (frame->v4l2_format.format) {
1036 case V4L2_PIX_FMT_RGB565:
1037 g = LIMIT_RGB(g_);
1038 *f_odd++ =
1039 (0x1F & LIMIT_RGB(r_)) |
1040 (0xE0 & (g << 5));
1041 *f_odd++ =
1042 (0x07 & (g >> 3)) |
1043 (0xF8 & LIMIT_RGB(b_));
1044 break;
1045 case V4L2_PIX_FMT_RGB24:
1046 *f_odd++ = LIMIT_RGB(r_);
1047 *f_odd++ = LIMIT_RGB(g_);
1048 *f_odd++ = LIMIT_RGB(b_);
1049 break;
1050 case V4L2_PIX_FMT_RGB32:
1051 *f_odd++ = LIMIT_RGB(r_);
1052 *f_odd++ = LIMIT_RGB(g_);
1053 *f_odd++ = LIMIT_RGB(b_);
1054 f_odd++;
1055 break;
1056 case V4L2_PIX_FMT_RGB555:
1057 g = LIMIT_RGB(g_);
1058 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1059 (0xE0 & (g << 5));
1060 *f_odd++ = (0x03 & (g >> 3)) |
1061 (0x7C & (LIMIT_RGB(b_) << 2));
1062 break;
1063 }
1064 }
1065 clipmask_odd_index += clipmask_add;
1066 f_odd += stretch_bytes;
1067
1068 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
1069 *f_odd++ = y[1];
1070 *f_odd++ = u;
1071 } else {
1072 y_ = 76284 * (y[1] - 16);
1073
1074 b_ = (y_ + vb) >> 16;
1075 g_ = (y_ + uvg) >> 16;
1076 r_ = (y_ + ur) >> 16;
1077
1078 switch (frame->v4l2_format.format) {
1079 case V4L2_PIX_FMT_RGB565:
1080 g = LIMIT_RGB(g_);
1081 *f_odd++ =
1082 (0x1F & LIMIT_RGB(r_)) |
1083 (0xE0 & (g << 5));
1084 *f_odd++ =
1085 (0x07 & (g >> 3)) |
1086 (0xF8 & LIMIT_RGB(b_));
1087 break;
1088 case V4L2_PIX_FMT_RGB24:
1089 *f_odd++ = LIMIT_RGB(r_);
1090 *f_odd++ = LIMIT_RGB(g_);
1091 *f_odd++ = LIMIT_RGB(b_);
1092 break;
1093 case V4L2_PIX_FMT_RGB32:
1094 *f_odd++ = LIMIT_RGB(r_);
1095 *f_odd++ = LIMIT_RGB(g_);
1096 *f_odd++ = LIMIT_RGB(b_);
1097 f_odd++;
1098 break;
1099 case V4L2_PIX_FMT_RGB555:
1100 g = LIMIT_RGB(g_);
1101 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1102 (0xE0 & (g << 5));
1103 *f_odd++ = (0x03 & (g >> 3)) |
1104 (0x7C & (LIMIT_RGB(b_) << 2));
1105 break;
1106 }
1107 }
1108 clipmask_odd_index += clipmask_add;
1109 f_odd += stretch_bytes;
1110 }
1111
1112 scratch_rm_old(usbvision, y_step[block % y_step_size] * sub_block_size);
1113 scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size]
1114 * sub_block_size);
1115 scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size]
1116 * sub_block_size);
1117 scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size]
1118 * sub_block_size);
1119 }
1120
1121 scratch_rm_old(usbvision, pixel_per_line * 3 / 2
1122 + block_split * sub_block_size);
1123
1124 frame->curline += 2 * usbvision->stretch_height;
1125 *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height;
1126
1127 if (frame->curline >= frame->frmheight)
1128 return parse_state_next_frame;
1129 return parse_state_continue;
1130}
1131
1132/*
1133 * usbvision_parse_data()
1134 *
1135 * Generic routine to parse the scratch buffer. It employs either
1136 * usbvision_find_header() or usbvision_parse_lines() to do most
1137 * of work.
1138 *
1139 */
1140static void usbvision_parse_data(struct usb_usbvision *usbvision)
1141{
1142 struct usbvision_frame *frame;
1143 enum parse_state newstate;
1144 long copylen = 0;
1145 unsigned long lock_flags;
1146
1147 frame = usbvision->cur_frame;
1148
1149 PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
1150
1151 while (1) {
1152 newstate = parse_state_out;
1153 if (scratch_len(usbvision)) {
1154 if (frame->scanstate == scan_state_scanning) {
1155 newstate = usbvision_find_header(usbvision);
1156 } else if (frame->scanstate == scan_state_lines) {
1157 if (usbvision->isoc_mode == ISOC_MODE_YUV420)
1158 newstate = usbvision_parse_lines_420(usbvision, &copylen);
1159 else if (usbvision->isoc_mode == ISOC_MODE_YUV422)
1160 newstate = usbvision_parse_lines_422(usbvision, &copylen);
1161 else if (usbvision->isoc_mode == ISOC_MODE_COMPRESS)
1162 newstate = usbvision_parse_compress(usbvision, &copylen);
1163 }
1164 }
1165 if (newstate == parse_state_continue)
1166 continue;
1167 if ((newstate == parse_state_next_frame) || (newstate == parse_state_out))
1168 break;
1169 return; /* parse_state_end_parse */
1170 }
1171
1172 if (newstate == parse_state_next_frame) {
1173 frame->grabstate = frame_state_done;
1174 do_gettimeofday(&(frame->timestamp));
1175 frame->sequence = usbvision->frame_num;
1176
1177 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1178 list_move_tail(&(frame->frame), &usbvision->outqueue);
1179 usbvision->cur_frame = NULL;
1180 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1181
1182 usbvision->frame_num++;
1183
1184 /* This will cause the process to request another frame. */
1185 if (waitqueue_active(&usbvision->wait_frame)) {
1186 PDEBUG(DBG_PARSE, "Wake up !");
1187 wake_up_interruptible(&usbvision->wait_frame);
1188 }
1189 } else {
1190 frame->grabstate = frame_state_grabbing;
1191 }
1192
1193 /* Update the frame's uncompressed length. */
1194 frame->scanlength += copylen;
1195}
1196
1197
1198/*
1199 * Make all of the blocks of data contiguous
1200 */
1201static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
1202 struct urb *urb)
1203{
1204 unsigned char *packet_data;
1205 int i, totlen = 0;
1206
1207 for (i = 0; i < urb->number_of_packets; i++) {
1208 int packet_len = urb->iso_frame_desc[i].actual_length;
1209 int packet_stat = urb->iso_frame_desc[i].status;
1210
1211 packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1212
1213 /* Detect and ignore errored packets */
1214 if (packet_stat) { /* packet_stat != 0 ????????????? */
1215 PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat);
1216 usbvision->isoc_err_count++;
1217 continue;
1218 }
1219
1220 /* Detect and ignore empty packets */
1221 if (packet_len < 0) {
1222 PDEBUG(DBG_ISOC, "error packet [%d]", i);
1223 usbvision->isoc_skip_count++;
1224 continue;
1225 } else if (packet_len == 0) { /* Frame end ????? */
1226 PDEBUG(DBG_ISOC, "null packet [%d]", i);
1227 usbvision->isocstate = isoc_state_no_frame;
1228 usbvision->isoc_skip_count++;
1229 continue;
1230 } else if (packet_len > usbvision->isoc_packet_size) {
1231 PDEBUG(DBG_ISOC, "packet[%d] > isoc_packet_size", i);
1232 usbvision->isoc_skip_count++;
1233 continue;
1234 }
1235
1236 PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len);
1237
1238 if (usbvision->isocstate == isoc_state_no_frame) { /* new frame begins */
1239 usbvision->isocstate = isoc_state_in_frame;
1240 scratch_mark_header(usbvision);
1241 usbvision_measure_bandwidth(usbvision);
1242 PDEBUG(DBG_ISOC, "packet with header");
1243 }
1244
1245 /*
1246 * If usbvision continues to feed us with data but there is no
1247 * consumption (if, for example, V4L client fell asleep) we
1248 * may overflow the buffer. We have to move old data over to
1249 * free room for new data. This is bad for old data. If we
1250 * just drop new data then it's bad for new data... choose
1251 * your favorite evil here.
1252 */
1253 if (scratch_free(usbvision) < packet_len) {
1254 usbvision->scratch_ovf_count++;
1255 PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d",
1256 scratch_len(usbvision), packet_len);
1257 scratch_rm_old(usbvision, packet_len - scratch_free(usbvision));
1258 }
1259
1260 /* Now we know that there is enough room in scratch buffer */
1261 scratch_put(usbvision, packet_data, packet_len);
1262 totlen += packet_len;
1263 usbvision->isoc_data_count += packet_len;
1264 usbvision->isoc_packet_count++;
1265 }
1266#if ENABLE_HEXDUMP
1267 if (totlen > 0) {
1268 static int foo;
1269
1270 if (foo < 1) {
1271 printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen);
1272 usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen);
1273 ++foo;
1274 }
1275 }
1276#endif
1277 return totlen;
1278}
1279
1280static void usbvision_isoc_irq(struct urb *urb)
1281{
1282 int err_code = 0;
1283 int len;
1284 struct usb_usbvision *usbvision = urb->context;
1285 int i;
1286 unsigned long start_time = jiffies;
1287 struct usbvision_frame **f;
1288
1289 /* We don't want to do anything if we are about to be removed! */
1290 if (!USBVISION_IS_OPERATIONAL(usbvision))
1291 return;
1292
1293 /* any urb with wrong status is ignored without acknowledgement */
1294 if (urb->status == -ENOENT)
1295 return;
1296
1297 f = &usbvision->cur_frame;
1298
1299 /* Manage streaming interruption */
1300 if (usbvision->streaming == stream_interrupt) {
1301 usbvision->streaming = stream_idle;
1302 if ((*f)) {
1303 (*f)->grabstate = frame_state_ready;
1304 (*f)->scanstate = scan_state_scanning;
1305 }
1306 PDEBUG(DBG_IRQ, "stream interrupted");
1307 wake_up_interruptible(&usbvision->wait_stream);
1308 }
1309
1310 /* Copy the data received into our scratch buffer */
1311 len = usbvision_compress_isochronous(usbvision, urb);
1312
1313 usbvision->isoc_urb_count++;
1314 usbvision->urb_length = len;
1315
1316 if (usbvision->streaming == stream_on) {
1317 /* If we collected enough data let's parse! */
1318 if (scratch_len(usbvision) > USBVISION_HEADER_LENGTH &&
1319 !list_empty(&(usbvision->inqueue))) {
1320 if (!(*f)) {
1321 (*f) = list_entry(usbvision->inqueue.next,
1322 struct usbvision_frame,
1323 frame);
1324 }
1325 usbvision_parse_data(usbvision);
1326 } else {
1327 /* If we don't have a frame
1328 we're current working on, complain */
1329 PDEBUG(DBG_IRQ,
1330 "received data, but no one needs it");
1331 scratch_reset(usbvision);
1332 }
1333 } else {
1334 PDEBUG(DBG_IRQ, "received data, but no one needs it");
1335 scratch_reset(usbvision);
1336 }
1337
1338 usbvision->time_in_irq += jiffies - start_time;
1339
1340 for (i = 0; i < USBVISION_URB_FRAMES; i++) {
1341 urb->iso_frame_desc[i].status = 0;
1342 urb->iso_frame_desc[i].actual_length = 0;
1343 }
1344
1345 urb->status = 0;
1346 urb->dev = usbvision->dev;
1347 err_code = usb_submit_urb(urb, GFP_ATOMIC);
1348
1349 if (err_code) {
1350 dev_err(&usbvision->dev->dev,
1351 "%s: usb_submit_urb failed: error %d\n",
1352 __func__, err_code);
1353 }
1354
1355 return;
1356}
1357
1358/*************************************/
1359/* Low level usbvision access functions */
1360/*************************************/
1361
1362/*
1363 * usbvision_read_reg()
1364 *
1365 * return < 0 -> Error
1366 * >= 0 -> Data
1367 */
1368
1369int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
1370{
1371 int err_code = 0;
1372 unsigned char buffer[1];
1373
1374 if (!USBVISION_IS_OPERATIONAL(usbvision))
1375 return -1;
1376
1377 err_code = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1),
1378 USBVISION_OP_CODE,
1379 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1380 0, (__u16) reg, buffer, 1, HZ);
1381
1382 if (err_code < 0) {
1383 dev_err(&usbvision->dev->dev,
1384 "%s: failed: error %d\n", __func__, err_code);
1385 return err_code;
1386 }
1387 return buffer[0];
1388}
1389
1390/*
1391 * usbvision_write_reg()
1392 *
1393 * return 1 -> Reg written
1394 * 0 -> usbvision is not yet ready
1395 * -1 -> Something went wrong
1396 */
1397
1398int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
1399 unsigned char value)
1400{
1401 int err_code = 0;
1402
1403 if (!USBVISION_IS_OPERATIONAL(usbvision))
1404 return 0;
1405
1406 err_code = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1407 USBVISION_OP_CODE,
1408 USB_DIR_OUT | USB_TYPE_VENDOR |
1409 USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ);
1410
1411 if (err_code < 0) {
1412 dev_err(&usbvision->dev->dev,
1413 "%s: failed: error %d\n", __func__, err_code);
1414 }
1415 return err_code;
1416}
1417
1418
1419static void usbvision_ctrl_urb_complete(struct urb *urb)
1420{
1421 struct usb_usbvision *usbvision = (struct usb_usbvision *)urb->context;
1422
1423 PDEBUG(DBG_IRQ, "");
1424 usbvision->ctrl_urb_busy = 0;
1425 if (waitqueue_active(&usbvision->ctrl_urb_wq))
1426 wake_up_interruptible(&usbvision->ctrl_urb_wq);
1427}
1428
1429
1430static int usbvision_write_reg_irq(struct usb_usbvision *usbvision, int address,
1431 unsigned char *data, int len)
1432{
1433 int err_code = 0;
1434
1435 PDEBUG(DBG_IRQ, "");
1436 if (len > 8)
1437 return -EFAULT;
1438 if (usbvision->ctrl_urb_busy)
1439 return -EBUSY;
1440 usbvision->ctrl_urb_busy = 1;
1441
1442 usbvision->ctrl_urb_setup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
1443 usbvision->ctrl_urb_setup.bRequest = USBVISION_OP_CODE;
1444 usbvision->ctrl_urb_setup.wValue = 0;
1445 usbvision->ctrl_urb_setup.wIndex = cpu_to_le16(address);
1446 usbvision->ctrl_urb_setup.wLength = cpu_to_le16(len);
1447 usb_fill_control_urb(usbvision->ctrl_urb, usbvision->dev,
1448 usb_sndctrlpipe(usbvision->dev, 1),
1449 (unsigned char *)&usbvision->ctrl_urb_setup,
1450 (void *)usbvision->ctrl_urb_buffer, len,
1451 usbvision_ctrl_urb_complete,
1452 (void *)usbvision);
1453
1454 memcpy(usbvision->ctrl_urb_buffer, data, len);
1455
1456 err_code = usb_submit_urb(usbvision->ctrl_urb, GFP_ATOMIC);
1457 if (err_code < 0) {
1458 /* error in usb_submit_urb() */
1459 usbvision->ctrl_urb_busy = 0;
1460 }
1461 PDEBUG(DBG_IRQ, "submit %d byte: error %d", len, err_code);
1462 return err_code;
1463}
1464
1465
1466static int usbvision_init_compression(struct usb_usbvision *usbvision)
1467{
1468 int err_code = 0;
1469
1470 usbvision->last_isoc_frame_num = -1;
1471 usbvision->isoc_data_count = 0;
1472 usbvision->isoc_packet_count = 0;
1473 usbvision->isoc_skip_count = 0;
1474 usbvision->compr_level = 50;
1475 usbvision->last_compr_level = -1;
1476 usbvision->isoc_urb_count = 0;
1477 usbvision->request_intra = 1;
1478 usbvision->isoc_measure_bandwidth_count = 0;
1479
1480 return err_code;
1481}
1482
1483/* this function measures the used bandwidth since last call
1484 * return: 0 : no error
1485 * sets used_bandwidth to 1-100 : 1-100% of full bandwidth resp. to isoc_packet_size
1486 */
1487static int usbvision_measure_bandwidth(struct usb_usbvision *usbvision)
1488{
1489 int err_code = 0;
1490
1491 if (usbvision->isoc_measure_bandwidth_count < 2) { /* this gives an average bandwidth of 3 frames */
1492 usbvision->isoc_measure_bandwidth_count++;
1493 return err_code;
1494 }
1495 if ((usbvision->isoc_packet_size > 0) && (usbvision->isoc_packet_count > 0)) {
1496 usbvision->used_bandwidth = usbvision->isoc_data_count /
1497 (usbvision->isoc_packet_count + usbvision->isoc_skip_count) *
1498 100 / usbvision->isoc_packet_size;
1499 }
1500 usbvision->isoc_measure_bandwidth_count = 0;
1501 usbvision->isoc_data_count = 0;
1502 usbvision->isoc_packet_count = 0;
1503 usbvision->isoc_skip_count = 0;
1504 return err_code;
1505}
1506
1507static int usbvision_adjust_compression(struct usb_usbvision *usbvision)
1508{
1509 int err_code = 0;
1510 unsigned char buffer[6];
1511
1512 PDEBUG(DBG_IRQ, "");
1513 if ((adjust_compression) && (usbvision->used_bandwidth > 0)) {
1514 usbvision->compr_level += (usbvision->used_bandwidth - 90) / 2;
1515 RESTRICT_TO_RANGE(usbvision->compr_level, 0, 100);
1516 if (usbvision->compr_level != usbvision->last_compr_level) {
1517 int distortion;
1518
1519 if (usbvision->bridge_type == BRIDGE_NT1004 || usbvision->bridge_type == BRIDGE_NT1005) {
1520 buffer[0] = (unsigned char)(4 + 16 * usbvision->compr_level / 100); /* PCM Threshold 1 */
1521 buffer[1] = (unsigned char)(4 + 8 * usbvision->compr_level / 100); /* PCM Threshold 2 */
1522 distortion = 7 + 248 * usbvision->compr_level / 100;
1523 buffer[2] = (unsigned char)(distortion & 0xFF); /* Average distortion Threshold (inter) */
1524 buffer[3] = (unsigned char)(distortion & 0xFF); /* Average distortion Threshold (intra) */
1525 distortion = 1 + 42 * usbvision->compr_level / 100;
1526 buffer[4] = (unsigned char)(distortion & 0xFF); /* Maximum distortion Threshold (inter) */
1527 buffer[5] = (unsigned char)(distortion & 0xFF); /* Maximum distortion Threshold (intra) */
1528 } else { /* BRIDGE_NT1003 */
1529 buffer[0] = (unsigned char)(4 + 16 * usbvision->compr_level / 100); /* PCM threshold 1 */
1530 buffer[1] = (unsigned char)(4 + 8 * usbvision->compr_level / 100); /* PCM threshold 2 */
1531 distortion = 2 + 253 * usbvision->compr_level / 100;
1532 buffer[2] = (unsigned char)(distortion & 0xFF); /* distortion threshold bit0-7 */
1533 buffer[3] = 0; /* (unsigned char)((distortion >> 8) & 0x0F); distortion threshold bit 8-11 */
1534 distortion = 0 + 43 * usbvision->compr_level / 100;
1535 buffer[4] = (unsigned char)(distortion & 0xFF); /* maximum distortion bit0-7 */
1536 buffer[5] = 0; /* (unsigned char)((distortion >> 8) & 0x01); maximum distortion bit 8 */
1537 }
1538 err_code = usbvision_write_reg_irq(usbvision, USBVISION_PCM_THR1, buffer, 6);
1539 if (err_code == 0) {
1540 PDEBUG(DBG_IRQ, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0],
1541 buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
1542 usbvision->last_compr_level = usbvision->compr_level;
1543 }
1544 }
1545 }
1546 return err_code;
1547}
1548
1549static int usbvision_request_intra(struct usb_usbvision *usbvision)
1550{
1551 int err_code = 0;
1552 unsigned char buffer[1];
1553
1554 PDEBUG(DBG_IRQ, "");
1555 usbvision->request_intra = 1;
1556 buffer[0] = 1;
1557 usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
1558 return err_code;
1559}
1560
1561static int usbvision_unrequest_intra(struct usb_usbvision *usbvision)
1562{
1563 int err_code = 0;
1564 unsigned char buffer[1];
1565
1566 PDEBUG(DBG_IRQ, "");
1567 usbvision->request_intra = 0;
1568 buffer[0] = 0;
1569 usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
1570 return err_code;
1571}
1572
1573/*******************************
1574 * usbvision utility functions
1575 *******************************/
1576
1577int usbvision_power_off(struct usb_usbvision *usbvision)
1578{
1579 int err_code = 0;
1580
1581 PDEBUG(DBG_FUNC, "");
1582
1583 err_code = usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
1584 if (err_code == 1)
1585 usbvision->power = 0;
1586 PDEBUG(DBG_FUNC, "%s: err_code %d", (err_code != 1) ? "ERROR" : "power is off", err_code);
1587 return err_code;
1588}
1589
1590/* configure webcam image sensor using the serial port */
1591static int usbvision_init_webcam(struct usb_usbvision *usbvision)
1592{
1593 int rc;
1594 int i;
1595 static char init_values[38][3] = {
1596 { 0x04, 0x12, 0x08 }, { 0x05, 0xff, 0xc8 }, { 0x06, 0x18, 0x07 }, { 0x07, 0x90, 0x00 },
1597 { 0x09, 0x00, 0x00 }, { 0x0a, 0x00, 0x00 }, { 0x0b, 0x08, 0x00 }, { 0x0d, 0xcc, 0xcc },
1598 { 0x0e, 0x13, 0x14 }, { 0x10, 0x9b, 0x83 }, { 0x11, 0x5a, 0x3f }, { 0x12, 0xe4, 0x73 },
1599 { 0x13, 0x88, 0x84 }, { 0x14, 0x89, 0x80 }, { 0x15, 0x00, 0x20 }, { 0x16, 0x00, 0x00 },
1600 { 0x17, 0xff, 0xa0 }, { 0x18, 0x6b, 0x20 }, { 0x19, 0x22, 0x40 }, { 0x1a, 0x10, 0x07 },
1601 { 0x1b, 0x00, 0x47 }, { 0x1c, 0x03, 0xe0 }, { 0x1d, 0x00, 0x00 }, { 0x1e, 0x00, 0x00 },
1602 { 0x1f, 0x00, 0x00 }, { 0x20, 0x00, 0x00 }, { 0x21, 0x00, 0x00 }, { 0x22, 0x00, 0x00 },
1603 { 0x23, 0x00, 0x00 }, { 0x24, 0x00, 0x00 }, { 0x25, 0x00, 0x00 }, { 0x26, 0x00, 0x00 },
1604 { 0x27, 0x00, 0x00 }, { 0x28, 0x00, 0x00 }, { 0x29, 0x00, 0x00 }, { 0x08, 0x80, 0x60 },
1605 { 0x0f, 0x2d, 0x24 }, { 0x0c, 0x80, 0x80 }
1606 };
1607 char value[3];
1608
1609 /* the only difference between PAL and NTSC init_values */
1610 if (usbvision_device_data[usbvision->dev_model].video_norm == V4L2_STD_NTSC)
1611 init_values[4][1] = 0x34;
1612
1613 for (i = 0; i < sizeof(init_values) / 3; i++) {
1614 usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_SER_MODE_SOFT);
1615 memcpy(value, init_values[i], 3);
1616 rc = usb_control_msg(usbvision->dev,
1617 usb_sndctrlpipe(usbvision->dev, 1),
1618 USBVISION_OP_CODE,
1619 USB_DIR_OUT | USB_TYPE_VENDOR |
1620 USB_RECIP_ENDPOINT, 0,
1621 (__u16) USBVISION_SER_DAT1, value,
1622 3, HZ);
1623 if (rc < 0)
1624 return rc;
1625 usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_SER_MODE_SIO);
1626 /* write 3 bytes to the serial port using SIO mode */
1627 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 3 | 0x10);
1628 usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, 0);
1629 usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_SER_MODE_SOFT);
1630 usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_IO_2);
1631 usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_SER_MODE_SOFT | USBVISION_CLK_OUT);
1632 usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_SER_MODE_SOFT | USBVISION_DAT_IO);
1633 usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_SER_MODE_SOFT | USBVISION_CLK_OUT | USBVISION_DAT_IO);
1634 }
1635
1636 return 0;
1637}
1638
1639/*
1640 * usbvision_set_video_format()
1641 *
1642 */
1643static int usbvision_set_video_format(struct usb_usbvision *usbvision, int format)
1644{
1645 static const char proc[] = "usbvision_set_video_format";
1646 int rc;
1647 unsigned char value[2];
1648
1649 if (!USBVISION_IS_OPERATIONAL(usbvision))
1650 return 0;
1651
1652 PDEBUG(DBG_FUNC, "isoc_mode %#02x", format);
1653
1654 if ((format != ISOC_MODE_YUV422)
1655 && (format != ISOC_MODE_YUV420)
1656 && (format != ISOC_MODE_COMPRESS)) {
1657 printk(KERN_ERR "usbvision: unknown video format %02x, using default YUV420",
1658 format);
1659 format = ISOC_MODE_YUV420;
1660 }
1661 value[0] = 0x0A; /* TODO: See the effect of the filter */
1662 value[1] = format; /* Sets the VO_MODE register which follows FILT_CONT */
1663 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1664 USBVISION_OP_CODE,
1665 USB_DIR_OUT | USB_TYPE_VENDOR |
1666 USB_RECIP_ENDPOINT, 0,
1667 (__u16) USBVISION_FILT_CONT, value, 2, HZ);
1668
1669 if (rc < 0) {
1670 printk(KERN_ERR "%s: ERROR=%d. USBVISION stopped - "
1671 "reconnect or reload driver.\n", proc, rc);
1672 }
1673 usbvision->isoc_mode = format;
1674 return rc;
1675}
1676
1677/*
1678 * usbvision_set_output()
1679 *
1680 */
1681
1682int usbvision_set_output(struct usb_usbvision *usbvision, int width,
1683 int height)
1684{
1685 int err_code = 0;
1686 int usb_width, usb_height;
1687 unsigned int frame_rate = 0, frame_drop = 0;
1688 unsigned char value[4];
1689
1690 if (!USBVISION_IS_OPERATIONAL(usbvision))
1691 return 0;
1692
1693 if (width > MAX_USB_WIDTH) {
1694 usb_width = width / 2;
1695 usbvision->stretch_width = 2;
1696 } else {
1697 usb_width = width;
1698 usbvision->stretch_width = 1;
1699 }
1700
1701 if (height > MAX_USB_HEIGHT) {
1702 usb_height = height / 2;
1703 usbvision->stretch_height = 2;
1704 } else {
1705 usb_height = height;
1706 usbvision->stretch_height = 1;
1707 }
1708
1709 RESTRICT_TO_RANGE(usb_width, MIN_FRAME_WIDTH, MAX_USB_WIDTH);
1710 usb_width &= ~(MIN_FRAME_WIDTH-1);
1711 RESTRICT_TO_RANGE(usb_height, MIN_FRAME_HEIGHT, MAX_USB_HEIGHT);
1712 usb_height &= ~(1);
1713
1714 PDEBUG(DBG_FUNC, "usb %dx%d; screen %dx%d; stretch %dx%d",
1715 usb_width, usb_height, width, height,
1716 usbvision->stretch_width, usbvision->stretch_height);
1717
1718 /* I'll not rewrite the same values */
1719 if ((usb_width != usbvision->curwidth) || (usb_height != usbvision->curheight)) {
1720 value[0] = usb_width & 0xff; /* LSB */
1721 value[1] = (usb_width >> 8) & 0x03; /* MSB */
1722 value[2] = usb_height & 0xff; /* LSB */
1723 value[3] = (usb_height >> 8) & 0x03; /* MSB */
1724
1725 err_code = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1726 USBVISION_OP_CODE,
1727 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1728 0, (__u16) USBVISION_LXSIZE_O, value, 4, HZ);
1729
1730 if (err_code < 0) {
1731 dev_err(&usbvision->dev->dev,
1732 "%s failed: error %d\n", __func__, err_code);
1733 return err_code;
1734 }
1735 usbvision->curwidth = usbvision->stretch_width * usb_width;
1736 usbvision->curheight = usbvision->stretch_height * usb_height;
1737 }
1738
1739 if (usbvision->isoc_mode == ISOC_MODE_YUV422)
1740 frame_rate = (usbvision->isoc_packet_size * 1000) / (usb_width * usb_height * 2);
1741 else if (usbvision->isoc_mode == ISOC_MODE_YUV420)
1742 frame_rate = (usbvision->isoc_packet_size * 1000) / ((usb_width * usb_height * 12) / 8);
1743 else
1744 frame_rate = FRAMERATE_MAX;
1745
1746 if (usbvision->tvnorm_id & V4L2_STD_625_50)
1747 frame_drop = frame_rate * 32 / 25 - 1;
1748 else if (usbvision->tvnorm_id & V4L2_STD_525_60)
1749 frame_drop = frame_rate * 32 / 30 - 1;
1750
1751 RESTRICT_TO_RANGE(frame_drop, FRAMERATE_MIN, FRAMERATE_MAX);
1752
1753 PDEBUG(DBG_FUNC, "frame_rate %d fps, frame_drop %d", frame_rate, frame_drop);
1754
1755 frame_drop = FRAMERATE_MAX; /* We can allow the maximum here, because dropping is controlled */
1756
1757 if (usbvision_device_data[usbvision->dev_model].codec == CODEC_WEBCAM) {
1758 if (usbvision_device_data[usbvision->dev_model].video_norm == V4L2_STD_PAL)
1759 frame_drop = 25;
1760 else
1761 frame_drop = 30;
1762 }
1763
1764 /* frame_drop = 7; => frame_phase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
1765 => frame_skip = 4;
1766 => frame_rate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
1767
1768 frame_drop = 9; => frame_phase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
1769 => frame_skip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
1770 => frame_rate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
1771 */
1772 err_code = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frame_drop);
1773 return err_code;
1774}
1775
1776
1777/*
1778 * usbvision_frames_alloc
1779 * allocate the required frames
1780 */
1781int usbvision_frames_alloc(struct usb_usbvision *usbvision, int number_of_frames)
1782{
1783 int i;
1784
1785 /* needs to be page aligned cause the buffers can be mapped individually! */
1786 usbvision->max_frame_size = PAGE_ALIGN(usbvision->curwidth *
1787 usbvision->curheight *
1788 usbvision->palette.bytes_per_pixel);
1789
1790 /* Try to do my best to allocate the frames the user want in the remaining memory */
1791 usbvision->num_frames = number_of_frames;
1792 while (usbvision->num_frames > 0) {
1793 usbvision->fbuf_size = usbvision->num_frames * usbvision->max_frame_size;
1794 usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size);
1795 if (usbvision->fbuf)
1796 break;
1797 usbvision->num_frames--;
1798 }
1799
1800 spin_lock_init(&usbvision->queue_lock);
1801 init_waitqueue_head(&usbvision->wait_frame);
1802 init_waitqueue_head(&usbvision->wait_stream);
1803
1804 /* Allocate all buffers */
1805 for (i = 0; i < usbvision->num_frames; i++) {
1806 usbvision->frame[i].index = i;
1807 usbvision->frame[i].grabstate = frame_state_unused;
1808 usbvision->frame[i].data = usbvision->fbuf +
1809 i * usbvision->max_frame_size;
1810 /*
1811 * Set default sizes for read operation.
1812 */
1813 usbvision->stretch_width = 1;
1814 usbvision->stretch_height = 1;
1815 usbvision->frame[i].width = usbvision->curwidth;
1816 usbvision->frame[i].height = usbvision->curheight;
1817 usbvision->frame[i].bytes_read = 0;
1818 }
1819 PDEBUG(DBG_FUNC, "allocated %d frames (%d bytes per frame)",
1820 usbvision->num_frames, usbvision->max_frame_size);
1821 return usbvision->num_frames;
1822}
1823
1824/*
1825 * usbvision_frames_free
1826 * frees memory allocated for the frames
1827 */
1828void usbvision_frames_free(struct usb_usbvision *usbvision)
1829{
1830 /* Have to free all that memory */
1831 PDEBUG(DBG_FUNC, "free %d frames", usbvision->num_frames);
1832
1833 if (usbvision->fbuf != NULL) {
1834 usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
1835 usbvision->fbuf = NULL;
1836
1837 usbvision->num_frames = 0;
1838 }
1839}
1840/*
1841 * usbvision_empty_framequeues()
1842 * prepare queues for incoming and outgoing frames
1843 */
1844void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
1845{
1846 u32 i;
1847
1848 INIT_LIST_HEAD(&(usbvision->inqueue));
1849 INIT_LIST_HEAD(&(usbvision->outqueue));
1850
1851 for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1852 usbvision->frame[i].grabstate = frame_state_unused;
1853 usbvision->frame[i].bytes_read = 0;
1854 }
1855}
1856
1857/*
1858 * usbvision_stream_interrupt()
1859 * stops streaming
1860 */
1861int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
1862{
1863 int ret = 0;
1864
1865 /* stop reading from the device */
1866
1867 usbvision->streaming = stream_interrupt;
1868 ret = wait_event_timeout(usbvision->wait_stream,
1869 (usbvision->streaming == stream_idle),
1870 msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES));
1871 return ret;
1872}
1873
1874/*
1875 * usbvision_set_compress_params()
1876 *
1877 */
1878
1879static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
1880{
1881 static const char proc[] = "usbvision_set_compresion_params: ";
1882 int rc;
1883 unsigned char value[6];
1884
1885 value[0] = 0x0F; /* Intra-Compression cycle */
1886 value[1] = 0x01; /* Reg.45 one line per strip */
1887 value[2] = 0x00; /* Reg.46 Force intra mode on all new frames */
1888 value[3] = 0x00; /* Reg.47 FORCE_UP <- 0 normal operation (not force) */
1889 value[4] = 0xA2; /* Reg.48 BUF_THR I'm not sure if this does something in not compressed mode. */
1890 value[5] = 0x00; /* Reg.49 DVI_YUV This has nothing to do with compression */
1891
1892 /* catched values for NT1004 */
1893 /* value[0] = 0xFF; Never apply intra mode automatically */
1894 /* value[1] = 0xF1; Use full frame height for virtual strip width; One line per strip */
1895 /* value[2] = 0x01; Force intra mode on all new frames */
1896 /* value[3] = 0x00; Strip size 400 Bytes; do not force up */
1897 /* value[4] = 0xA2; */
1898 if (!USBVISION_IS_OPERATIONAL(usbvision))
1899 return 0;
1900
1901 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1902 USBVISION_OP_CODE,
1903 USB_DIR_OUT | USB_TYPE_VENDOR |
1904 USB_RECIP_ENDPOINT, 0,
1905 (__u16) USBVISION_INTRA_CYC, value, 5, HZ);
1906
1907 if (rc < 0) {
1908 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
1909 "reconnect or reload driver.\n", proc, rc);
1910 return rc;
1911 }
1912
1913 if (usbvision->bridge_type == BRIDGE_NT1004) {
1914 value[0] = 20; /* PCM Threshold 1 */
1915 value[1] = 12; /* PCM Threshold 2 */
1916 value[2] = 255; /* Distortion Threshold inter */
1917 value[3] = 255; /* Distortion Threshold intra */
1918 value[4] = 43; /* Max Distortion inter */
1919 value[5] = 43; /* Max Distortion intra */
1920 } else {
1921 value[0] = 20; /* PCM Threshold 1 */
1922 value[1] = 12; /* PCM Threshold 2 */
1923 value[2] = 255; /* Distortion Threshold d7-d0 */
1924 value[3] = 0; /* Distortion Threshold d11-d8 */
1925 value[4] = 43; /* Max Distortion d7-d0 */
1926 value[5] = 0; /* Max Distortion d8 */
1927 }
1928
1929 if (!USBVISION_IS_OPERATIONAL(usbvision))
1930 return 0;
1931
1932 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1933 USBVISION_OP_CODE,
1934 USB_DIR_OUT | USB_TYPE_VENDOR |
1935 USB_RECIP_ENDPOINT, 0,
1936 (__u16) USBVISION_PCM_THR1, value, 6, HZ);
1937
1938 if (rc < 0) {
1939 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
1940 "reconnect or reload driver.\n", proc, rc);
1941 }
1942 return rc;
1943}
1944
1945
1946/*
1947 * usbvision_set_input()
1948 *
1949 * Set the input (saa711x, ...) size x y and other misc input params
1950 * I've no idea if this parameters are right
1951 *
1952 */
1953int usbvision_set_input(struct usb_usbvision *usbvision)
1954{
1955 static const char proc[] = "usbvision_set_input: ";
1956 int rc;
1957 unsigned char value[8];
1958 unsigned char dvi_yuv_value;
1959
1960 if (!USBVISION_IS_OPERATIONAL(usbvision))
1961 return 0;
1962
1963 /* Set input format expected from decoder*/
1964 if (usbvision_device_data[usbvision->dev_model].vin_reg1_override) {
1965 value[0] = usbvision_device_data[usbvision->dev_model].vin_reg1;
1966 } else if (usbvision_device_data[usbvision->dev_model].codec == CODEC_SAA7113) {
1967 /* SAA7113 uses 8 bit output */
1968 value[0] = USBVISION_8_422_SYNC;
1969 } else {
1970 /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
1971 * as that is how saa7111 is configured */
1972 value[0] = USBVISION_16_422_SYNC;
1973 /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
1974 }
1975
1976 rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]);
1977 if (rc < 0) {
1978 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
1979 "reconnect or reload driver.\n", proc, rc);
1980 return rc;
1981 }
1982
1983
1984 if (usbvision->tvnorm_id & V4L2_STD_PAL) {
1985 value[0] = 0xC0;
1986 value[1] = 0x02; /* 0x02C0 -> 704 Input video line length */
1987 value[2] = 0x20;
1988 value[3] = 0x01; /* 0x0120 -> 288 Input video n. of lines */
1989 value[4] = 0x60;
1990 value[5] = 0x00; /* 0x0060 -> 96 Input video h offset */
1991 value[6] = 0x16;
1992 value[7] = 0x00; /* 0x0016 -> 22 Input video v offset */
1993 } else if (usbvision->tvnorm_id & V4L2_STD_SECAM) {
1994 value[0] = 0xC0;
1995 value[1] = 0x02; /* 0x02C0 -> 704 Input video line length */
1996 value[2] = 0x20;
1997 value[3] = 0x01; /* 0x0120 -> 288 Input video n. of lines */
1998 value[4] = 0x01;
1999 value[5] = 0x00; /* 0x0001 -> 01 Input video h offset */
2000 value[6] = 0x01;
2001 value[7] = 0x00; /* 0x0001 -> 01 Input video v offset */
2002 } else { /* V4L2_STD_NTSC */
2003 value[0] = 0xD0;
2004 value[1] = 0x02; /* 0x02D0 -> 720 Input video line length */
2005 value[2] = 0xF0;
2006 value[3] = 0x00; /* 0x00F0 -> 240 Input video number of lines */
2007 value[4] = 0x50;
2008 value[5] = 0x00; /* 0x0050 -> 80 Input video h offset */
2009 value[6] = 0x10;
2010 value[7] = 0x00; /* 0x0010 -> 16 Input video v offset */
2011 }
2012
2013 /* webcam is only 480 pixels wide, both PAL and NTSC version */
2014 if (usbvision_device_data[usbvision->dev_model].codec == CODEC_WEBCAM) {
2015 value[0] = 0xe0;
2016 value[1] = 0x01; /* 0x01E0 -> 480 Input video line length */
2017 }
2018
2019 if (usbvision_device_data[usbvision->dev_model].x_offset >= 0) {
2020 value[4] = usbvision_device_data[usbvision->dev_model].x_offset & 0xff;
2021 value[5] = (usbvision_device_data[usbvision->dev_model].x_offset & 0x0300) >> 8;
2022 }
2023
2024 if (adjust_x_offset != -1) {
2025 value[4] = adjust_x_offset & 0xff;
2026 value[5] = (adjust_x_offset & 0x0300) >> 8;
2027 }
2028
2029 if (usbvision_device_data[usbvision->dev_model].y_offset >= 0) {
2030 value[6] = usbvision_device_data[usbvision->dev_model].y_offset & 0xff;
2031 value[7] = (usbvision_device_data[usbvision->dev_model].y_offset & 0x0300) >> 8;
2032 }
2033
2034 if (adjust_y_offset != -1) {
2035 value[6] = adjust_y_offset & 0xff;
2036 value[7] = (adjust_y_offset & 0x0300) >> 8;
2037 }
2038
2039 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2040 USBVISION_OP_CODE, /* USBVISION specific code */
2041 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0,
2042 (__u16) USBVISION_LXSIZE_I, value, 8, HZ);
2043 if (rc < 0) {
2044 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2045 "reconnect or reload driver.\n", proc, rc);
2046 return rc;
2047 }
2048
2049
2050 dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
2051
2052 if (usbvision_device_data[usbvision->dev_model].dvi_yuv_override) {
2053 dvi_yuv_value = usbvision_device_data[usbvision->dev_model].dvi_yuv;
2054 } else if (usbvision_device_data[usbvision->dev_model].codec == CODEC_SAA7113) {
2055 /* This changes as the fine sync control changes. Further investigation necessary */
2056 dvi_yuv_value = 0x06;
2057 }
2058
2059 return usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value);
2060}
2061
2062
2063/*
2064 * usbvision_set_dram_settings()
2065 *
2066 * Set the buffer address needed by the usbvision dram to operate
2067 * This values has been taken with usbsnoop.
2068 *
2069 */
2070
2071static int usbvision_set_dram_settings(struct usb_usbvision *usbvision)
2072{
2073 int rc;
2074 unsigned char value[8];
2075
2076 if (usbvision->isoc_mode == ISOC_MODE_COMPRESS) {
2077 value[0] = 0x42;
2078 value[1] = 0x71;
2079 value[2] = 0xff;
2080 value[3] = 0x00;
2081 value[4] = 0x98;
2082 value[5] = 0xe0;
2083 value[6] = 0x71;
2084 value[7] = 0xff;
2085 /* UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte) */
2086 /* FDL: 0x00000-0x0E099 = 57498 Words */
2087 /* VDW: 0x0E3FF-0x3FFFF */
2088 } else {
2089 value[0] = 0x42;
2090 value[1] = 0x00;
2091 value[2] = 0xff;
2092 value[3] = 0x00;
2093 value[4] = 0x00;
2094 value[5] = 0x00;
2095 value[6] = 0x00;
2096 value[7] = 0xff;
2097 }
2098 /* These are the values of the address of the video buffer,
2099 * they have to be loaded into the USBVISION_DRM_PRM1-8
2100 *
2101 * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
2102 * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
2103 * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
2104 * Only used in compressed mode
2105 * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
2106 * Only used in compressed mode
2107 * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
2108 * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
2109 */
2110
2111 if (!USBVISION_IS_OPERATIONAL(usbvision))
2112 return 0;
2113
2114 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2115 USBVISION_OP_CODE, /* USBVISION specific code */
2116 USB_DIR_OUT | USB_TYPE_VENDOR |
2117 USB_RECIP_ENDPOINT, 0,
2118 (__u16) USBVISION_DRM_PRM1, value, 8, HZ);
2119
2120 if (rc < 0) {
2121 dev_err(&usbvision->dev->dev, "%s: ERROR=%d\n", __func__, rc);
2122 return rc;
2123 }
2124
2125 /* Restart the video buffer logic */
2126 rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR |
2127 USBVISION_RES_FDL | USBVISION_RES_VDW);
2128 if (rc < 0)
2129 return rc;
2130 rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00);
2131
2132 return rc;
2133}
2134
2135/*
2136 * ()
2137 *
2138 * Power on the device, enables suspend-resume logic
2139 * & reset the isoc End-Point
2140 *
2141 */
2142
2143int usbvision_power_on(struct usb_usbvision *usbvision)
2144{
2145 int err_code = 0;
2146
2147 PDEBUG(DBG_FUNC, "");
2148
2149 usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
2150 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2151 USBVISION_SSPND_EN | USBVISION_RES2);
2152
2153 if (usbvision_device_data[usbvision->dev_model].codec == CODEC_WEBCAM) {
2154 usbvision_write_reg(usbvision, USBVISION_VIN_REG1,
2155 USBVISION_16_422_SYNC | USBVISION_HVALID_PO);
2156 usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
2157 USBVISION_NOHVALID | USBVISION_KEEP_BLANK);
2158 }
2159 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2160 USBVISION_SSPND_EN | USBVISION_PWR_VID);
2161 mdelay(10);
2162 err_code = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2163 USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2);
2164 if (err_code == 1)
2165 usbvision->power = 1;
2166 PDEBUG(DBG_FUNC, "%s: err_code %d", (err_code < 0) ? "ERROR" : "power is on", err_code);
2167 return err_code;
2168}
2169
2170
2171/*
2172 * usbvision timer stuff
2173 */
2174
2175/* to call usbvision_power_off from task queue */
2176static void call_usbvision_power_off(struct work_struct *work)
2177{
2178 struct usb_usbvision *usbvision = container_of(work, struct usb_usbvision, power_off_work);
2179
2180 PDEBUG(DBG_FUNC, "");
2181 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
2182 return;
2183
2184 if (usbvision->user == 0) {
2185 usbvision_i2c_unregister(usbvision);
2186
2187 usbvision_power_off(usbvision);
2188 usbvision->initialized = 0;
2189 }
2190 mutex_unlock(&usbvision->v4l2_lock);
2191}
2192
2193static void usbvision_power_off_timer(unsigned long data)
2194{
2195 struct usb_usbvision *usbvision = (void *)data;
2196
2197 PDEBUG(DBG_FUNC, "");
2198 del_timer(&usbvision->power_off_timer);
2199 INIT_WORK(&usbvision->power_off_work, call_usbvision_power_off);
2200 (void) schedule_work(&usbvision->power_off_work);
2201}
2202
2203void usbvision_init_power_off_timer(struct usb_usbvision *usbvision)
2204{
2205 init_timer(&usbvision->power_off_timer);
2206 usbvision->power_off_timer.data = (long)usbvision;
2207 usbvision->power_off_timer.function = usbvision_power_off_timer;
2208}
2209
2210void usbvision_set_power_off_timer(struct usb_usbvision *usbvision)
2211{
2212 mod_timer(&usbvision->power_off_timer, jiffies + USBVISION_POWEROFF_TIME);
2213}
2214
2215void usbvision_reset_power_off_timer(struct usb_usbvision *usbvision)
2216{
2217 if (timer_pending(&usbvision->power_off_timer))
2218 del_timer(&usbvision->power_off_timer);
2219}
2220
2221/*
2222 * usbvision_begin_streaming()
2223 * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
2224 * idea about the rest
2225 */
2226int usbvision_begin_streaming(struct usb_usbvision *usbvision)
2227{
2228 if (usbvision->isoc_mode == ISOC_MODE_COMPRESS)
2229 usbvision_init_compression(usbvision);
2230 return usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
2231 USBVISION_NOHVALID | usbvision->vin_reg2_preset);
2232}
2233
2234/*
2235 * usbvision_restart_isoc()
2236 * Not sure yet if touching here PWR_REG make loose the config
2237 */
2238
2239int usbvision_restart_isoc(struct usb_usbvision *usbvision)
2240{
2241 int ret;
2242
2243 ret = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2244 USBVISION_SSPND_EN | USBVISION_PWR_VID);
2245 if (ret < 0)
2246 return ret;
2247 ret = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2248 USBVISION_SSPND_EN | USBVISION_PWR_VID |
2249 USBVISION_RES2);
2250 if (ret < 0)
2251 return ret;
2252 ret = usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
2253 USBVISION_KEEP_BLANK | USBVISION_NOHVALID |
2254 usbvision->vin_reg2_preset);
2255 if (ret < 0)
2256 return ret;
2257
2258 /* TODO: schedule timeout */
2259 while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) & 0x01) != 1)
2260 ;
2261
2262 return 0;
2263}
2264
2265int usbvision_audio_off(struct usb_usbvision *usbvision)
2266{
2267 if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) {
2268 printk(KERN_ERR "usbvision_audio_off: can't write reg\n");
2269 return -1;
2270 }
2271 usbvision->audio_mute = 0;
2272 usbvision->audio_channel = USBVISION_AUDIO_MUTE;
2273 return 0;
2274}
2275
2276int usbvision_set_audio(struct usb_usbvision *usbvision, int audio_channel)
2277{
2278 if (!usbvision->audio_mute) {
2279 if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, audio_channel) < 0) {
2280 printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n");
2281 return -1;
2282 }
2283 }
2284 usbvision->audio_channel = audio_channel;
2285 return 0;
2286}
2287
2288int usbvision_setup(struct usb_usbvision *usbvision, int format)
2289{
2290 if (usbvision_device_data[usbvision->dev_model].codec == CODEC_WEBCAM)
2291 usbvision_init_webcam(usbvision);
2292 usbvision_set_video_format(usbvision, format);
2293 usbvision_set_dram_settings(usbvision);
2294 usbvision_set_compress_params(usbvision);
2295 usbvision_set_input(usbvision);
2296 usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT);
2297 usbvision_restart_isoc(usbvision);
2298
2299 /* cosas del PCM */
2300 return USBVISION_IS_OPERATIONAL(usbvision);
2301}
2302
2303int usbvision_set_alternate(struct usb_usbvision *dev)
2304{
2305 int err_code, prev_alt = dev->iface_alt;
2306 int i;
2307
2308 dev->iface_alt = 0;
2309 for (i = 0; i < dev->num_alt; i++)
2310 if (dev->alt_max_pkt_size[i] > dev->alt_max_pkt_size[dev->iface_alt])
2311 dev->iface_alt = i;
2312
2313 if (dev->iface_alt != prev_alt) {
2314 dev->isoc_packet_size = dev->alt_max_pkt_size[dev->iface_alt];
2315 PDEBUG(DBG_FUNC, "setting alternate %d with max_packet_size=%u",
2316 dev->iface_alt, dev->isoc_packet_size);
2317 err_code = usb_set_interface(dev->dev, dev->iface, dev->iface_alt);
2318 if (err_code < 0) {
2319 dev_err(&dev->dev->dev,
2320 "cannot change alternate number to %d (error=%i)\n",
2321 dev->iface_alt, err_code);
2322 return err_code;
2323 }
2324 }
2325
2326 PDEBUG(DBG_ISOC, "ISO Packet Length:%d", dev->isoc_packet_size);
2327
2328 return 0;
2329}
2330
2331/*
2332 * usbvision_init_isoc()
2333 *
2334 */
2335int usbvision_init_isoc(struct usb_usbvision *usbvision)
2336{
2337 struct usb_device *dev = usbvision->dev;
2338 int buf_idx, err_code, reg_value;
2339 int sb_size;
2340
2341 if (!USBVISION_IS_OPERATIONAL(usbvision))
2342 return -EFAULT;
2343
2344 usbvision->cur_frame = NULL;
2345 scratch_reset(usbvision);
2346
2347 /* Alternate interface 1 is is the biggest frame size */
2348 err_code = usbvision_set_alternate(usbvision);
2349 if (err_code < 0) {
2350 usbvision->last_error = err_code;
2351 return -EBUSY;
2352 }
2353 sb_size = USBVISION_URB_FRAMES * usbvision->isoc_packet_size;
2354
2355 reg_value = (16 - usbvision_read_reg(usbvision,
2356 USBVISION_ALTER_REG)) & 0x0F;
2357
2358 usbvision->usb_bandwidth = reg_value >> 1;
2359 PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec",
2360 usbvision->usb_bandwidth);
2361
2362
2363
2364 /* We double buffer the Iso lists */
2365
2366 for (buf_idx = 0; buf_idx < USBVISION_NUMSBUF; buf_idx++) {
2367 int j, k;
2368 struct urb *urb;
2369
2370 urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
2371 if (urb == NULL) {
2372 dev_err(&usbvision->dev->dev,
2373 "%s: usb_alloc_urb() failed\n", __func__);
2374 return -ENOMEM;
2375 }
2376 usbvision->sbuf[buf_idx].urb = urb;
2377 usbvision->sbuf[buf_idx].data =
2378 usb_alloc_coherent(usbvision->dev,
2379 sb_size,
2380 GFP_KERNEL,
2381 &urb->transfer_dma);
2382 urb->dev = dev;
2383 urb->context = usbvision;
2384 urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp);
2385 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
2386 urb->interval = 1;
2387 urb->transfer_buffer = usbvision->sbuf[buf_idx].data;
2388 urb->complete = usbvision_isoc_irq;
2389 urb->number_of_packets = USBVISION_URB_FRAMES;
2390 urb->transfer_buffer_length =
2391 usbvision->isoc_packet_size * USBVISION_URB_FRAMES;
2392 for (j = k = 0; j < USBVISION_URB_FRAMES; j++,
2393 k += usbvision->isoc_packet_size) {
2394 urb->iso_frame_desc[j].offset = k;
2395 urb->iso_frame_desc[j].length =
2396 usbvision->isoc_packet_size;
2397 }
2398 }
2399
2400 /* Submit all URBs */
2401 for (buf_idx = 0; buf_idx < USBVISION_NUMSBUF; buf_idx++) {
2402 err_code = usb_submit_urb(usbvision->sbuf[buf_idx].urb,
2403 GFP_KERNEL);
2404 if (err_code) {
2405 dev_err(&usbvision->dev->dev,
2406 "%s: usb_submit_urb(%d) failed: error %d\n",
2407 __func__, buf_idx, err_code);
2408 }
2409 }
2410
2411 usbvision->streaming = stream_idle;
2412 PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x",
2413 __func__,
2414 usbvision->video_endp);
2415 return 0;
2416}
2417
2418/*
2419 * usbvision_stop_isoc()
2420 *
2421 * This procedure stops streaming and deallocates URBs. Then it
2422 * activates zero-bandwidth alt. setting of the video interface.
2423 *
2424 */
2425void usbvision_stop_isoc(struct usb_usbvision *usbvision)
2426{
2427 int buf_idx, err_code, reg_value;
2428 int sb_size = USBVISION_URB_FRAMES * usbvision->isoc_packet_size;
2429
2430 if ((usbvision->streaming == stream_off) || (usbvision->dev == NULL))
2431 return;
2432
2433 /* Unschedule all of the iso td's */
2434 for (buf_idx = 0; buf_idx < USBVISION_NUMSBUF; buf_idx++) {
2435 usb_kill_urb(usbvision->sbuf[buf_idx].urb);
2436 if (usbvision->sbuf[buf_idx].data) {
2437 usb_free_coherent(usbvision->dev,
2438 sb_size,
2439 usbvision->sbuf[buf_idx].data,
2440 usbvision->sbuf[buf_idx].urb->transfer_dma);
2441 }
2442 usb_free_urb(usbvision->sbuf[buf_idx].urb);
2443 usbvision->sbuf[buf_idx].urb = NULL;
2444 }
2445
2446 PDEBUG(DBG_ISOC, "%s: streaming=stream_off\n", __func__);
2447 usbvision->streaming = stream_off;
2448
2449 if (!usbvision->remove_pending) {
2450 /* Set packet size to 0 */
2451 usbvision->iface_alt = 0;
2452 err_code = usb_set_interface(usbvision->dev, usbvision->iface,
2453 usbvision->iface_alt);
2454 if (err_code < 0) {
2455 dev_err(&usbvision->dev->dev,
2456 "%s: usb_set_interface() failed: error %d\n",
2457 __func__, err_code);
2458 usbvision->last_error = err_code;
2459 }
2460 reg_value = (16-usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
2461 usbvision->isoc_packet_size =
2462 (reg_value == 0) ? 0 : (reg_value * 64) - 1;
2463 PDEBUG(DBG_ISOC, "ISO Packet Length:%d",
2464 usbvision->isoc_packet_size);
2465
2466 usbvision->usb_bandwidth = reg_value >> 1;
2467 PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec",
2468 usbvision->usb_bandwidth);
2469 }
2470}
2471
2472int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
2473{
2474 /* inputs #0 and #3 are constant for every SAA711x. */
2475 /* inputs #1 and #2 are variable for SAA7111 and SAA7113 */
2476 int mode[4] = { SAA7115_COMPOSITE0, 0, 0, SAA7115_COMPOSITE3 };
2477 int audio[] = { 1, 0, 0, 0 };
2478 /* channel 0 is TV with audiochannel 1 (tuner mono) */
2479 /* channel 1 is Composite with audio channel 0 (line in) */
2480 /* channel 2 is S-Video with audio channel 0 (line in) */
2481 /* channel 3 is additional video inputs to the device with audio channel 0 (line in) */
2482
2483 RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs);
2484 usbvision->ctl_input = channel;
2485
2486 /* set the new channel */
2487 /* Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video */
2488 /* Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red */
2489
2490 switch (usbvision_device_data[usbvision->dev_model].codec) {
2491 case CODEC_SAA7113:
2492 mode[1] = SAA7115_COMPOSITE2;
2493 if (switch_svideo_input) {
2494 /* To handle problems with S-Video Input for
2495 * some devices. Use switch_svideo_input
2496 * parameter when loading the module.*/
2497 mode[2] = SAA7115_COMPOSITE1;
2498 } else {
2499 mode[2] = SAA7115_SVIDEO1;
2500 }
2501 break;
2502 case CODEC_SAA7111:
2503 default:
2504 /* modes for saa7111 */
2505 mode[1] = SAA7115_COMPOSITE1;
2506 mode[2] = SAA7115_SVIDEO1;
2507 break;
2508 }
2509 call_all(usbvision, video, s_routing, mode[channel], 0, 0);
2510 usbvision_set_audio(usbvision, audio[channel]);
2511 return 0;
2512}
2513
2514/*
2515 * Overrides for Emacs so that we follow Linus's tabbing style.
2516 * ---------------------------------------------------------------------------
2517 * Local variables:
2518 * c-basic-offset: 8
2519 * End:
2520 */
diff --git a/drivers/media/video/usbvision/usbvision-i2c.c b/drivers/media/video/usbvision/usbvision-i2c.c
new file mode 100644
index 00000000000..d7f97513b28
--- /dev/null
+++ b/drivers/media/video/usbvision/usbvision-i2c.c
@@ -0,0 +1,478 @@
1/*
2 * usbvision_i2c.c
3 * i2c algorithm for USB-I2C Bridges
4 *
5 * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
7 *
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/delay.h>
30#include <linux/init.h>
31#include <linux/uaccess.h>
32#include <linux/ioport.h>
33#include <linux/errno.h>
34#include <linux/usb.h>
35#include <linux/i2c.h>
36#include "usbvision.h"
37
38#define DBG_I2C (1 << 0)
39
40static int i2c_debug;
41
42module_param(i2c_debug, int, 0644); /* debug_i2c_usb mode of the device driver */
43MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
44
45#define PDEBUG(level, fmt, args...) { \
46 if (i2c_debug & (level)) \
47 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
48 __func__, __LINE__ , ## args); \
49 }
50
51static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
52 short len);
53static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
54 short len);
55
56static inline int try_write_address(struct i2c_adapter *i2c_adap,
57 unsigned char addr, int retries)
58{
59 struct usb_usbvision *usbvision;
60 int i, ret = -1;
61 char buf[4];
62
63 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
64 buf[0] = 0x00;
65 for (i = 0; i <= retries; i++) {
66 ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
67 if (ret == 1)
68 break; /* success! */
69 udelay(5);
70 if (i == retries) /* no success */
71 break;
72 udelay(10);
73 }
74 if (i) {
75 PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
76 PDEBUG(DBG_I2C, "Maybe there's no device at this address");
77 }
78 return ret;
79}
80
81static inline int try_read_address(struct i2c_adapter *i2c_adap,
82 unsigned char addr, int retries)
83{
84 struct usb_usbvision *usbvision;
85 int i, ret = -1;
86 char buf[4];
87
88 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
89 for (i = 0; i <= retries; i++) {
90 ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
91 if (ret == 1)
92 break; /* success! */
93 udelay(5);
94 if (i == retries) /* no success */
95 break;
96 udelay(10);
97 }
98 if (i) {
99 PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
100 PDEBUG(DBG_I2C, "Maybe there's no device at this address");
101 }
102 return ret;
103}
104
105static inline int usb_find_address(struct i2c_adapter *i2c_adap,
106 struct i2c_msg *msg, int retries,
107 unsigned char *add)
108{
109 unsigned short flags = msg->flags;
110
111 unsigned char addr;
112 int ret;
113 if ((flags & I2C_M_TEN)) {
114 /* a ten bit address */
115 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
116 /* try extended address code... */
117 ret = try_write_address(i2c_adap, addr, retries);
118 if (ret != 1) {
119 dev_err(&i2c_adap->dev,
120 "died at extended address code, while writing\n");
121 return -EREMOTEIO;
122 }
123 add[0] = addr;
124 if (flags & I2C_M_RD) {
125 /* okay, now switch into reading mode */
126 addr |= 0x01;
127 ret = try_read_address(i2c_adap, addr, retries);
128 if (ret != 1) {
129 dev_err(&i2c_adap->dev,
130 "died at extended address code, while reading\n");
131 return -EREMOTEIO;
132 }
133 }
134
135 } else { /* normal 7bit address */
136 addr = (msg->addr << 1);
137 if (flags & I2C_M_RD)
138 addr |= 1;
139
140 add[0] = addr;
141 if (flags & I2C_M_RD)
142 ret = try_read_address(i2c_adap, addr, retries);
143 else
144 ret = try_write_address(i2c_adap, addr, retries);
145
146 if (ret != 1)
147 return -EREMOTEIO;
148 }
149 return 0;
150}
151
152static int
153usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
154{
155 struct i2c_msg *pmsg;
156 struct usb_usbvision *usbvision;
157 int i, ret;
158 unsigned char addr = 0;
159
160 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
161
162 for (i = 0; i < num; i++) {
163 pmsg = &msgs[i];
164 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
165 if (ret != 0) {
166 PDEBUG(DBG_I2C, "got NAK from device, message #%d", i);
167 return (ret < 0) ? ret : -EREMOTEIO;
168 }
169
170 if (pmsg->flags & I2C_M_RD) {
171 /* read bytes into buffer */
172 ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
173 if (ret < pmsg->len)
174 return (ret < 0) ? ret : -EREMOTEIO;
175 } else {
176 /* write bytes from buffer */
177 ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
178 if (ret < pmsg->len)
179 return (ret < 0) ? ret : -EREMOTEIO;
180 }
181 }
182 return num;
183}
184
185static u32 functionality(struct i2c_adapter *adap)
186{
187 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
188}
189
190/* -----exported algorithm data: ------------------------------------- */
191
192static struct i2c_algorithm usbvision_algo = {
193 .master_xfer = usbvision_i2c_xfer,
194 .smbus_xfer = NULL,
195 .functionality = functionality,
196};
197
198
199/* ----------------------------------------------------------------------- */
200/* usbvision specific I2C functions */
201/* ----------------------------------------------------------------------- */
202static struct i2c_adapter i2c_adap_template;
203
204int usbvision_i2c_register(struct usb_usbvision *usbvision)
205{
206 static unsigned short saa711x_addrs[] = {
207 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
208 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
209 I2C_CLIENT_END };
210
211 if (usbvision->registered_i2c)
212 return 0;
213
214 memcpy(&usbvision->i2c_adap, &i2c_adap_template,
215 sizeof(struct i2c_adapter));
216
217 sprintf(usbvision->i2c_adap.name, "%s-%d-%s", i2c_adap_template.name,
218 usbvision->dev->bus->busnum, usbvision->dev->devpath);
219 PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name);
220 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
221
222 i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
223
224 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
225 printk(KERN_ERR "usbvision_i2c_register: can't write reg\n");
226 return -EBUSY;
227 }
228
229 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
230 PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
231
232 /* register new adapter to i2c module... */
233
234 usbvision->i2c_adap.algo = &usbvision_algo;
235
236 usbvision->i2c_adap.timeout = 100; /* default values, should */
237 usbvision->i2c_adap.retries = 3; /* be replaced by defines */
238
239 i2c_add_adapter(&usbvision->i2c_adap);
240
241 PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
242
243 /* Request the load of the i2c modules we need */
244 switch (usbvision_device_data[usbvision->dev_model].codec) {
245 case CODEC_SAA7113:
246 case CODEC_SAA7111:
247 /* Without this delay the detection of the saa711x is
248 hit-and-miss. */
249 mdelay(10);
250 v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
251 &usbvision->i2c_adap,
252 "saa7115_auto", 0, saa711x_addrs);
253 break;
254 }
255 if (usbvision_device_data[usbvision->dev_model].tuner == 1) {
256 struct v4l2_subdev *sd;
257 enum v4l2_i2c_tuner_type type;
258 struct tuner_setup tun_setup;
259
260 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
261 &usbvision->i2c_adap,
262 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
263 /* depending on whether we found a demod or not, select
264 the tuner type. */
265 type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
266
267 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
268 &usbvision->i2c_adap,
269 "tuner", 0, v4l2_i2c_tuner_addrs(type));
270
271 if (sd == NULL)
272 return -ENODEV;
273 if (usbvision->tuner_type != -1) {
274 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
275 tun_setup.type = usbvision->tuner_type;
276 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
277 call_all(usbvision, tuner, s_type_addr, &tun_setup);
278 }
279 }
280 usbvision->registered_i2c = 1;
281
282 return 0;
283}
284
285int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
286{
287 if (!usbvision->registered_i2c)
288 return 0;
289
290 i2c_del_adapter(&(usbvision->i2c_adap));
291 usbvision->registered_i2c = 0;
292
293 PDEBUG(DBG_I2C, "i2c bus for %s unregistered", usbvision->i2c_adap.name);
294
295 return 0;
296}
297
298static int
299usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
300 char *buf, short len)
301{
302 int rc, retries;
303
304 for (retries = 5;;) {
305 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
306 if (rc < 0)
307 return rc;
308
309 /* Initiate byte read cycle */
310 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
311 /* d3 0=Wr 1=Rd */
312 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
313 (len & 0x07) | 0x18);
314 if (rc < 0)
315 return rc;
316
317 /* Test for Busy and ACK */
318 do {
319 /* USBVISION_SER_CONT -> d4 == 0 busy */
320 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
321 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
322 if (rc < 0)
323 return rc;
324
325 /* USBVISION_SER_CONT -> d5 == 1 Not ack */
326 if ((rc & 0x20) == 0) /* Ack? */
327 break;
328
329 /* I2C abort */
330 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
331 if (rc < 0)
332 return rc;
333
334 if (--retries < 0)
335 return -1;
336 }
337
338 switch (len) {
339 case 4:
340 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
341 case 3:
342 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
343 case 2:
344 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
345 case 1:
346 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
347 break;
348 default:
349 printk(KERN_ERR
350 "usbvision_i2c_read_max4: buffer length > 4\n");
351 }
352
353 if (i2c_debug & DBG_I2C) {
354 int idx;
355
356 for (idx = 0; idx < len; idx++)
357 PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr);
358 }
359 return len;
360}
361
362
363static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
364 unsigned char addr, const char *buf,
365 short len)
366{
367 int rc, retries;
368 int i;
369 unsigned char value[6];
370 unsigned char ser_cont;
371
372 ser_cont = (len & 0x07) | 0x10;
373
374 value[0] = addr;
375 value[1] = ser_cont;
376 for (i = 0; i < len; i++)
377 value[i + 2] = buf[i];
378
379 for (retries = 5;;) {
380 rc = usb_control_msg(usbvision->dev,
381 usb_sndctrlpipe(usbvision->dev, 1),
382 USBVISION_OP_CODE,
383 USB_DIR_OUT | USB_TYPE_VENDOR |
384 USB_RECIP_ENDPOINT, 0,
385 (__u16) USBVISION_SER_ADRS, value,
386 len + 2, HZ);
387
388 if (rc < 0)
389 return rc;
390
391 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
392 (len & 0x07) | 0x10);
393 if (rc < 0)
394 return rc;
395
396 /* Test for Busy and ACK */
397 do {
398 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
399 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
400 if (rc < 0)
401 return rc;
402
403 if ((rc & 0x20) == 0) /* Ack? */
404 break;
405
406 /* I2C abort */
407 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
408
409 if (--retries < 0)
410 return -1;
411
412 }
413
414 if (i2c_debug & DBG_I2C) {
415 int idx;
416
417 for (idx = 0; idx < len; idx++)
418 PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr);
419 }
420 return len;
421}
422
423static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
424 short len)
425{
426 char *buf_ptr = buf;
427 int retval;
428 int wrcount = 0;
429 int count;
430 int max_len = 4;
431
432 while (len > 0) {
433 count = (len > max_len) ? max_len : len;
434 retval = usbvision_i2c_write_max4(usbvision, addr, buf_ptr, count);
435 if (retval > 0) {
436 len -= count;
437 buf_ptr += count;
438 wrcount += count;
439 } else
440 return (retval < 0) ? retval : -EFAULT;
441 }
442 return wrcount;
443}
444
445static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
446 short len)
447{
448 char temp[4];
449 int retval, i;
450 int rdcount = 0;
451 int count;
452
453 while (len > 0) {
454 count = (len > 3) ? 4 : len;
455 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
456 if (retval > 0) {
457 for (i = 0; i < len; i++)
458 buf[rdcount + i] = temp[i];
459 len -= count;
460 rdcount += count;
461 } else
462 return (retval < 0) ? retval : -EFAULT;
463 }
464 return rdcount;
465}
466
467static struct i2c_adapter i2c_adap_template = {
468 .owner = THIS_MODULE,
469 .name = "usbvision",
470};
471
472/*
473 * Overrides for Emacs so that we follow Linus's tabbing style.
474 * ---------------------------------------------------------------------------
475 * Local variables:
476 * c-basic-offset: 8
477 * End:
478 */
diff --git a/drivers/media/video/usbvision/usbvision-video.c b/drivers/media/video/usbvision/usbvision-video.c
new file mode 100644
index 00000000000..5a74f5e07d7
--- /dev/null
+++ b/drivers/media/video/usbvision/usbvision-video.c
@@ -0,0 +1,1686 @@
1/*
2 * USB USBVISION Video device driver 0.9.10
3 *
4 *
5 *
6 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
7 *
8 * This module is part of usbvision driver project.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * Let's call the version 0.... until compression decoding is completely
25 * implemented.
26 *
27 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
28 * It was based on USB CPiA driver written by Peter Pregler,
29 * Scott J. Bertin and Johannes Erdfelt
30 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
31 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
32 * Updates to driver completed by Dwaine P. Garden
33 *
34 *
35 * TODO:
36 * - use submit_urb for all setup packets
37 * - Fix memory settings for nt1004. It is 4 times as big as the
38 * nt1003 memory.
39 * - Add audio on endpoint 3 for nt1004 chip.
40 * Seems impossible, needs a codec interface. Which one?
41 * - Clean up the driver.
42 * - optimization for performance.
43 * - Add Videotext capability (VBI). Working on it.....
44 * - Check audio for other devices
45 *
46 */
47
48#include <linux/kernel.h>
49#include <linux/list.h>
50#include <linux/timer.h>
51#include <linux/slab.h>
52#include <linux/mm.h>
53#include <linux/highmem.h>
54#include <linux/vmalloc.h>
55#include <linux/module.h>
56#include <linux/init.h>
57#include <linux/spinlock.h>
58#include <linux/io.h>
59#include <linux/videodev2.h>
60#include <linux/i2c.h>
61
62#include <media/saa7115.h>
63#include <media/v4l2-common.h>
64#include <media/v4l2-ioctl.h>
65#include <media/tuner.h>
66
67#include <linux/workqueue.h>
68
69#include "usbvision.h"
70#include "usbvision-cards.h"
71
72#define DRIVER_AUTHOR \
73 "Joerg Heckenbach <joerg@heckenbach-aw.de>, " \
74 "Dwaine Garden <DwaineGarden@rogers.com>"
75#define DRIVER_NAME "usbvision"
76#define DRIVER_ALIAS "USBVision"
77#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
78#define DRIVER_LICENSE "GPL"
79#define USBVISION_VERSION_STRING "0.9.11"
80
81#define ENABLE_HEXDUMP 0 /* Enable if you need it */
82
83
84#ifdef USBVISION_DEBUG
85 #define PDEBUG(level, fmt, args...) { \
86 if (video_debug & (level)) \
87 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
88 __func__, __LINE__ , ## args); \
89 }
90#else
91 #define PDEBUG(level, fmt, args...) do {} while (0)
92#endif
93
94#define DBG_IO (1 << 1)
95#define DBG_PROBE (1 << 2)
96#define DBG_MMAP (1 << 3)
97
98/* String operations */
99#define rmspace(str) while (*str == ' ') str++;
100#define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
101
102
103/* sequential number of usbvision device */
104static int usbvision_nr;
105
106static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
107 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
108 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
109 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
110 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
111 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
112 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
113 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, /* 1.5 ! */
114 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
115};
116
117/* Function prototypes */
118static void usbvision_release(struct usb_usbvision *usbvision);
119
120/* Default initialization of device driver parameters */
121/* Set the default format for ISOC endpoint */
122static int isoc_mode = ISOC_MODE_COMPRESS;
123/* Set the default Debug Mode of the device driver */
124static int video_debug;
125/* Set the default device to power on at startup */
126static int power_on_at_open = 1;
127/* Sequential Number of Video Device */
128static int video_nr = -1;
129/* Sequential Number of Radio Device */
130static int radio_nr = -1;
131
132/* Grab parameters for the device driver */
133
134/* Showing parameters under SYSFS */
135module_param(isoc_mode, int, 0444);
136module_param(video_debug, int, 0444);
137module_param(power_on_at_open, int, 0444);
138module_param(video_nr, int, 0444);
139module_param(radio_nr, int, 0444);
140
141MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
142MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
143MODULE_PARM_DESC(power_on_at_open, " Set the default device to power on when device is opened. Default: 1 (On)");
144MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
145MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
146
147
148/* Misc stuff */
149MODULE_AUTHOR(DRIVER_AUTHOR);
150MODULE_DESCRIPTION(DRIVER_DESC);
151MODULE_LICENSE(DRIVER_LICENSE);
152MODULE_VERSION(USBVISION_VERSION_STRING);
153MODULE_ALIAS(DRIVER_ALIAS);
154
155
156/*****************************************************************************/
157/* SYSFS Code - Copied from the stv680.c usb module. */
158/* Device information is located at /sys/class/video4linux/video0 */
159/* Device parameters information is located at /sys/module/usbvision */
160/* Device USB Information is located at */
161/* /sys/bus/usb/drivers/USBVision Video Grabber */
162/*****************************************************************************/
163
164#define YES_NO(x) ((x) ? "Yes" : "No")
165
166static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
167{
168 struct video_device *vdev =
169 container_of(cd, struct video_device, dev);
170 return video_get_drvdata(vdev);
171}
172
173static ssize_t show_version(struct device *cd,
174 struct device_attribute *attr, char *buf)
175{
176 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
177}
178static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
179
180static ssize_t show_model(struct device *cd,
181 struct device_attribute *attr, char *buf)
182{
183 struct video_device *vdev =
184 container_of(cd, struct video_device, dev);
185 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
186 return sprintf(buf, "%s\n",
187 usbvision_device_data[usbvision->dev_model].model_string);
188}
189static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
190
191static ssize_t show_hue(struct device *cd,
192 struct device_attribute *attr, char *buf)
193{
194 struct video_device *vdev =
195 container_of(cd, struct video_device, dev);
196 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
197 struct v4l2_control ctrl;
198 ctrl.id = V4L2_CID_HUE;
199 ctrl.value = 0;
200 if (usbvision->user)
201 call_all(usbvision, core, g_ctrl, &ctrl);
202 return sprintf(buf, "%d\n", ctrl.value);
203}
204static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
205
206static ssize_t show_contrast(struct device *cd,
207 struct device_attribute *attr, char *buf)
208{
209 struct video_device *vdev =
210 container_of(cd, struct video_device, dev);
211 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
212 struct v4l2_control ctrl;
213 ctrl.id = V4L2_CID_CONTRAST;
214 ctrl.value = 0;
215 if (usbvision->user)
216 call_all(usbvision, core, g_ctrl, &ctrl);
217 return sprintf(buf, "%d\n", ctrl.value);
218}
219static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
220
221static ssize_t show_brightness(struct device *cd,
222 struct device_attribute *attr, char *buf)
223{
224 struct video_device *vdev =
225 container_of(cd, struct video_device, dev);
226 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
227 struct v4l2_control ctrl;
228 ctrl.id = V4L2_CID_BRIGHTNESS;
229 ctrl.value = 0;
230 if (usbvision->user)
231 call_all(usbvision, core, g_ctrl, &ctrl);
232 return sprintf(buf, "%d\n", ctrl.value);
233}
234static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
235
236static ssize_t show_saturation(struct device *cd,
237 struct device_attribute *attr, char *buf)
238{
239 struct video_device *vdev =
240 container_of(cd, struct video_device, dev);
241 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
242 struct v4l2_control ctrl;
243 ctrl.id = V4L2_CID_SATURATION;
244 ctrl.value = 0;
245 if (usbvision->user)
246 call_all(usbvision, core, g_ctrl, &ctrl);
247 return sprintf(buf, "%d\n", ctrl.value);
248}
249static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
250
251static ssize_t show_streaming(struct device *cd,
252 struct device_attribute *attr, char *buf)
253{
254 struct video_device *vdev =
255 container_of(cd, struct video_device, dev);
256 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
257 return sprintf(buf, "%s\n",
258 YES_NO(usbvision->streaming == stream_on ? 1 : 0));
259}
260static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
261
262static ssize_t show_compression(struct device *cd,
263 struct device_attribute *attr, char *buf)
264{
265 struct video_device *vdev =
266 container_of(cd, struct video_device, dev);
267 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
268 return sprintf(buf, "%s\n",
269 YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
270}
271static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
272
273static ssize_t show_device_bridge(struct device *cd,
274 struct device_attribute *attr, char *buf)
275{
276 struct video_device *vdev =
277 container_of(cd, struct video_device, dev);
278 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
279 return sprintf(buf, "%d\n", usbvision->bridge_type);
280}
281static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
282
283static void usbvision_create_sysfs(struct video_device *vdev)
284{
285 int res;
286
287 if (!vdev)
288 return;
289 do {
290 res = device_create_file(&vdev->dev, &dev_attr_version);
291 if (res < 0)
292 break;
293 res = device_create_file(&vdev->dev, &dev_attr_model);
294 if (res < 0)
295 break;
296 res = device_create_file(&vdev->dev, &dev_attr_hue);
297 if (res < 0)
298 break;
299 res = device_create_file(&vdev->dev, &dev_attr_contrast);
300 if (res < 0)
301 break;
302 res = device_create_file(&vdev->dev, &dev_attr_brightness);
303 if (res < 0)
304 break;
305 res = device_create_file(&vdev->dev, &dev_attr_saturation);
306 if (res < 0)
307 break;
308 res = device_create_file(&vdev->dev, &dev_attr_streaming);
309 if (res < 0)
310 break;
311 res = device_create_file(&vdev->dev, &dev_attr_compression);
312 if (res < 0)
313 break;
314 res = device_create_file(&vdev->dev, &dev_attr_bridge);
315 if (res >= 0)
316 return;
317 } while (0);
318
319 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
320}
321
322static void usbvision_remove_sysfs(struct video_device *vdev)
323{
324 if (vdev) {
325 device_remove_file(&vdev->dev, &dev_attr_version);
326 device_remove_file(&vdev->dev, &dev_attr_model);
327 device_remove_file(&vdev->dev, &dev_attr_hue);
328 device_remove_file(&vdev->dev, &dev_attr_contrast);
329 device_remove_file(&vdev->dev, &dev_attr_brightness);
330 device_remove_file(&vdev->dev, &dev_attr_saturation);
331 device_remove_file(&vdev->dev, &dev_attr_streaming);
332 device_remove_file(&vdev->dev, &dev_attr_compression);
333 device_remove_file(&vdev->dev, &dev_attr_bridge);
334 }
335}
336
337/*
338 * usbvision_open()
339 *
340 * This is part of Video 4 Linux API. The driver can be opened by one
341 * client only (checks internal counter 'usbvision->user'). The procedure
342 * then allocates buffers needed for video processing.
343 *
344 */
345static int usbvision_v4l2_open(struct file *file)
346{
347 struct usb_usbvision *usbvision = video_drvdata(file);
348 int err_code = 0;
349
350 PDEBUG(DBG_IO, "open");
351
352 usbvision_reset_power_off_timer(usbvision);
353
354 if (usbvision->user)
355 err_code = -EBUSY;
356 else {
357 /* Allocate memory for the scratch ring buffer */
358 err_code = usbvision_scratch_alloc(usbvision);
359 if (isoc_mode == ISOC_MODE_COMPRESS) {
360 /* Allocate intermediate decompression buffers
361 only if needed */
362 err_code = usbvision_decompress_alloc(usbvision);
363 }
364 if (err_code) {
365 /* Deallocate all buffers if trouble */
366 usbvision_scratch_free(usbvision);
367 usbvision_decompress_free(usbvision);
368 }
369 }
370
371 /* If so far no errors then we shall start the camera */
372 if (!err_code) {
373 if (usbvision->power == 0) {
374 usbvision_power_on(usbvision);
375 usbvision_i2c_register(usbvision);
376 }
377
378 /* Send init sequence only once, it's large! */
379 if (!usbvision->initialized) {
380 int setup_ok = 0;
381 setup_ok = usbvision_setup(usbvision, isoc_mode);
382 if (setup_ok)
383 usbvision->initialized = 1;
384 else
385 err_code = -EBUSY;
386 }
387
388 if (!err_code) {
389 usbvision_begin_streaming(usbvision);
390 err_code = usbvision_init_isoc(usbvision);
391 /* device must be initialized before isoc transfer */
392 usbvision_muxsel(usbvision, 0);
393 usbvision->user++;
394 } else {
395 if (power_on_at_open) {
396 usbvision_i2c_unregister(usbvision);
397 usbvision_power_off(usbvision);
398 usbvision->initialized = 0;
399 }
400 }
401 }
402
403 /* prepare queues */
404 usbvision_empty_framequeues(usbvision);
405
406 PDEBUG(DBG_IO, "success");
407 return err_code;
408}
409
410/*
411 * usbvision_v4l2_close()
412 *
413 * This is part of Video 4 Linux API. The procedure
414 * stops streaming and deallocates all buffers that were earlier
415 * allocated in usbvision_v4l2_open().
416 *
417 */
418static int usbvision_v4l2_close(struct file *file)
419{
420 struct usb_usbvision *usbvision = video_drvdata(file);
421
422 PDEBUG(DBG_IO, "close");
423
424 usbvision_audio_off(usbvision);
425 usbvision_restart_isoc(usbvision);
426 usbvision_stop_isoc(usbvision);
427
428 usbvision_decompress_free(usbvision);
429 usbvision_frames_free(usbvision);
430 usbvision_empty_framequeues(usbvision);
431 usbvision_scratch_free(usbvision);
432
433 usbvision->user--;
434
435 if (power_on_at_open) {
436 /* power off in a little while
437 to avoid off/on every close/open short sequences */
438 usbvision_set_power_off_timer(usbvision);
439 usbvision->initialized = 0;
440 }
441
442 if (usbvision->remove_pending) {
443 printk(KERN_INFO "%s: Final disconnect\n", __func__);
444 usbvision_release(usbvision);
445 }
446
447 PDEBUG(DBG_IO, "success");
448 return 0;
449}
450
451
452/*
453 * usbvision_ioctl()
454 *
455 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
456 *
457 */
458#ifdef CONFIG_VIDEO_ADV_DEBUG
459static int vidioc_g_register(struct file *file, void *priv,
460 struct v4l2_dbg_register *reg)
461{
462 struct usb_usbvision *usbvision = video_drvdata(file);
463 int err_code;
464
465 if (!v4l2_chip_match_host(&reg->match))
466 return -EINVAL;
467 /* NT100x has a 8-bit register space */
468 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
469 if (err_code < 0) {
470 dev_err(&usbvision->vdev->dev,
471 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
472 __func__, err_code);
473 return err_code;
474 }
475 reg->val = err_code;
476 reg->size = 1;
477 return 0;
478}
479
480static int vidioc_s_register(struct file *file, void *priv,
481 struct v4l2_dbg_register *reg)
482{
483 struct usb_usbvision *usbvision = video_drvdata(file);
484 int err_code;
485
486 if (!v4l2_chip_match_host(&reg->match))
487 return -EINVAL;
488 /* NT100x has a 8-bit register space */
489 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
490 if (err_code < 0) {
491 dev_err(&usbvision->vdev->dev,
492 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
493 __func__, err_code);
494 return err_code;
495 }
496 return 0;
497}
498#endif
499
500static int vidioc_querycap(struct file *file, void *priv,
501 struct v4l2_capability *vc)
502{
503 struct usb_usbvision *usbvision = video_drvdata(file);
504
505 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
506 strlcpy(vc->card,
507 usbvision_device_data[usbvision->dev_model].model_string,
508 sizeof(vc->card));
509 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
510 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_AUDIO |
512 V4L2_CAP_READWRITE |
513 V4L2_CAP_STREAMING |
514 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
515 return 0;
516}
517
518static int vidioc_enum_input(struct file *file, void *priv,
519 struct v4l2_input *vi)
520{
521 struct usb_usbvision *usbvision = video_drvdata(file);
522 int chan;
523
524 if (vi->index >= usbvision->video_inputs)
525 return -EINVAL;
526 if (usbvision->have_tuner)
527 chan = vi->index;
528 else
529 chan = vi->index + 1; /* skip Television string*/
530
531 /* Determine the requested input characteristics
532 specific for each usbvision card model */
533 switch (chan) {
534 case 0:
535 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
536 strcpy(vi->name, "White Video Input");
537 } else {
538 strcpy(vi->name, "Television");
539 vi->type = V4L2_INPUT_TYPE_TUNER;
540 vi->audioset = 1;
541 vi->tuner = chan;
542 vi->std = USBVISION_NORMS;
543 }
544 break;
545 case 1:
546 vi->type = V4L2_INPUT_TYPE_CAMERA;
547 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
548 strcpy(vi->name, "Green Video Input");
549 else
550 strcpy(vi->name, "Composite Video Input");
551 vi->std = V4L2_STD_PAL;
552 break;
553 case 2:
554 vi->type = V4L2_INPUT_TYPE_CAMERA;
555 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
556 strcpy(vi->name, "Yellow Video Input");
557 else
558 strcpy(vi->name, "S-Video Input");
559 vi->std = V4L2_STD_PAL;
560 break;
561 case 3:
562 vi->type = V4L2_INPUT_TYPE_CAMERA;
563 strcpy(vi->name, "Red Video Input");
564 vi->std = V4L2_STD_PAL;
565 break;
566 }
567 return 0;
568}
569
570static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
571{
572 struct usb_usbvision *usbvision = video_drvdata(file);
573
574 *input = usbvision->ctl_input;
575 return 0;
576}
577
578static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
579{
580 struct usb_usbvision *usbvision = video_drvdata(file);
581
582 if (input >= usbvision->video_inputs)
583 return -EINVAL;
584
585 usbvision_muxsel(usbvision, input);
586 usbvision_set_input(usbvision);
587 usbvision_set_output(usbvision,
588 usbvision->curwidth,
589 usbvision->curheight);
590 return 0;
591}
592
593static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
594{
595 struct usb_usbvision *usbvision = video_drvdata(file);
596
597 usbvision->tvnorm_id = *id;
598
599 call_all(usbvision, core, s_std, usbvision->tvnorm_id);
600 /* propagate the change to the decoder */
601 usbvision_muxsel(usbvision, usbvision->ctl_input);
602
603 return 0;
604}
605
606static int vidioc_g_tuner(struct file *file, void *priv,
607 struct v4l2_tuner *vt)
608{
609 struct usb_usbvision *usbvision = video_drvdata(file);
610
611 if (!usbvision->have_tuner || vt->index) /* Only tuner 0 */
612 return -EINVAL;
613 if (usbvision->radio) {
614 strcpy(vt->name, "Radio");
615 vt->type = V4L2_TUNER_RADIO;
616 } else {
617 strcpy(vt->name, "Television");
618 }
619 /* Let clients fill in the remainder of this struct */
620 call_all(usbvision, tuner, g_tuner, vt);
621
622 return 0;
623}
624
625static int vidioc_s_tuner(struct file *file, void *priv,
626 struct v4l2_tuner *vt)
627{
628 struct usb_usbvision *usbvision = video_drvdata(file);
629
630 /* Only no or one tuner for now */
631 if (!usbvision->have_tuner || vt->index)
632 return -EINVAL;
633 /* let clients handle this */
634 call_all(usbvision, tuner, s_tuner, vt);
635
636 return 0;
637}
638
639static int vidioc_g_frequency(struct file *file, void *priv,
640 struct v4l2_frequency *freq)
641{
642 struct usb_usbvision *usbvision = video_drvdata(file);
643
644 freq->tuner = 0; /* Only one tuner */
645 if (usbvision->radio)
646 freq->type = V4L2_TUNER_RADIO;
647 else
648 freq->type = V4L2_TUNER_ANALOG_TV;
649 freq->frequency = usbvision->freq;
650
651 return 0;
652}
653
654static int vidioc_s_frequency(struct file *file, void *priv,
655 struct v4l2_frequency *freq)
656{
657 struct usb_usbvision *usbvision = video_drvdata(file);
658
659 /* Only no or one tuner for now */
660 if (!usbvision->have_tuner || freq->tuner)
661 return -EINVAL;
662
663 usbvision->freq = freq->frequency;
664 call_all(usbvision, tuner, s_frequency, freq);
665
666 return 0;
667}
668
669static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
670{
671 struct usb_usbvision *usbvision = video_drvdata(file);
672
673 if (usbvision->radio)
674 strcpy(a->name, "Radio");
675 else
676 strcpy(a->name, "TV");
677
678 return 0;
679}
680
681static int vidioc_s_audio(struct file *file, void *fh,
682 struct v4l2_audio *a)
683{
684 if (a->index)
685 return -EINVAL;
686 return 0;
687}
688
689static int vidioc_queryctrl(struct file *file, void *priv,
690 struct v4l2_queryctrl *ctrl)
691{
692 struct usb_usbvision *usbvision = video_drvdata(file);
693
694 call_all(usbvision, core, queryctrl, ctrl);
695
696 if (!ctrl->type)
697 return -EINVAL;
698
699 return 0;
700}
701
702static int vidioc_g_ctrl(struct file *file, void *priv,
703 struct v4l2_control *ctrl)
704{
705 struct usb_usbvision *usbvision = video_drvdata(file);
706
707 call_all(usbvision, core, g_ctrl, ctrl);
708 return 0;
709}
710
711static int vidioc_s_ctrl(struct file *file, void *priv,
712 struct v4l2_control *ctrl)
713{
714 struct usb_usbvision *usbvision = video_drvdata(file);
715
716 call_all(usbvision, core, s_ctrl, ctrl);
717 return 0;
718}
719
720static int vidioc_reqbufs(struct file *file,
721 void *priv, struct v4l2_requestbuffers *vr)
722{
723 struct usb_usbvision *usbvision = video_drvdata(file);
724 int ret;
725
726 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
727
728 /* Check input validity:
729 the user must do a VIDEO CAPTURE and MMAP method. */
730 if (vr->memory != V4L2_MEMORY_MMAP)
731 return -EINVAL;
732
733 if (usbvision->streaming == stream_on) {
734 ret = usbvision_stream_interrupt(usbvision);
735 if (ret)
736 return ret;
737 }
738
739 usbvision_frames_free(usbvision);
740 usbvision_empty_framequeues(usbvision);
741 vr->count = usbvision_frames_alloc(usbvision, vr->count);
742
743 usbvision->cur_frame = NULL;
744
745 return 0;
746}
747
748static int vidioc_querybuf(struct file *file,
749 void *priv, struct v4l2_buffer *vb)
750{
751 struct usb_usbvision *usbvision = video_drvdata(file);
752 struct usbvision_frame *frame;
753
754 /* FIXME : must control
755 that buffers are mapped (VIDIOC_REQBUFS has been called) */
756 if (vb->index >= usbvision->num_frames)
757 return -EINVAL;
758 /* Updating the corresponding frame state */
759 vb->flags = 0;
760 frame = &usbvision->frame[vb->index];
761 if (frame->grabstate >= frame_state_ready)
762 vb->flags |= V4L2_BUF_FLAG_QUEUED;
763 if (frame->grabstate >= frame_state_done)
764 vb->flags |= V4L2_BUF_FLAG_DONE;
765 if (frame->grabstate == frame_state_unused)
766 vb->flags |= V4L2_BUF_FLAG_MAPPED;
767 vb->memory = V4L2_MEMORY_MMAP;
768
769 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
770
771 vb->memory = V4L2_MEMORY_MMAP;
772 vb->field = V4L2_FIELD_NONE;
773 vb->length = usbvision->curwidth *
774 usbvision->curheight *
775 usbvision->palette.bytes_per_pixel;
776 vb->timestamp = usbvision->frame[vb->index].timestamp;
777 vb->sequence = usbvision->frame[vb->index].sequence;
778 return 0;
779}
780
781static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
782{
783 struct usb_usbvision *usbvision = video_drvdata(file);
784 struct usbvision_frame *frame;
785 unsigned long lock_flags;
786
787 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
788 if (vb->index >= usbvision->num_frames)
789 return -EINVAL;
790
791 frame = &usbvision->frame[vb->index];
792
793 if (frame->grabstate != frame_state_unused)
794 return -EAGAIN;
795
796 /* Mark it as ready and enqueue frame */
797 frame->grabstate = frame_state_ready;
798 frame->scanstate = scan_state_scanning;
799 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
800
801 vb->flags &= ~V4L2_BUF_FLAG_DONE;
802
803 /* set v4l2_format index */
804 frame->v4l2_format = usbvision->palette;
805
806 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
807 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
808 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
809
810 return 0;
811}
812
813static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
814{
815 struct usb_usbvision *usbvision = video_drvdata(file);
816 int ret;
817 struct usbvision_frame *f;
818 unsigned long lock_flags;
819
820 if (list_empty(&(usbvision->outqueue))) {
821 if (usbvision->streaming == stream_idle)
822 return -EINVAL;
823 ret = wait_event_interruptible
824 (usbvision->wait_frame,
825 !list_empty(&(usbvision->outqueue)));
826 if (ret)
827 return ret;
828 }
829
830 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
831 f = list_entry(usbvision->outqueue.next,
832 struct usbvision_frame, frame);
833 list_del(usbvision->outqueue.next);
834 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
835
836 f->grabstate = frame_state_unused;
837
838 vb->memory = V4L2_MEMORY_MMAP;
839 vb->flags = V4L2_BUF_FLAG_MAPPED |
840 V4L2_BUF_FLAG_QUEUED |
841 V4L2_BUF_FLAG_DONE;
842 vb->index = f->index;
843 vb->sequence = f->sequence;
844 vb->timestamp = f->timestamp;
845 vb->field = V4L2_FIELD_NONE;
846 vb->bytesused = f->scanlength;
847
848 return 0;
849}
850
851static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
852{
853 struct usb_usbvision *usbvision = video_drvdata(file);
854
855 usbvision->streaming = stream_on;
856 call_all(usbvision, video, s_stream, 1);
857
858 return 0;
859}
860
861static int vidioc_streamoff(struct file *file,
862 void *priv, enum v4l2_buf_type type)
863{
864 struct usb_usbvision *usbvision = video_drvdata(file);
865
866 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
867 return -EINVAL;
868
869 if (usbvision->streaming == stream_on) {
870 usbvision_stream_interrupt(usbvision);
871 /* Stop all video streamings */
872 call_all(usbvision, video, s_stream, 0);
873 }
874 usbvision_empty_framequeues(usbvision);
875
876 return 0;
877}
878
879static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
880 struct v4l2_fmtdesc *vfd)
881{
882 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
883 return -EINVAL;
884 strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
885 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
886 return 0;
887}
888
889static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
890 struct v4l2_format *vf)
891{
892 struct usb_usbvision *usbvision = video_drvdata(file);
893 vf->fmt.pix.width = usbvision->curwidth;
894 vf->fmt.pix.height = usbvision->curheight;
895 vf->fmt.pix.pixelformat = usbvision->palette.format;
896 vf->fmt.pix.bytesperline =
897 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
898 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
899 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
900 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
901
902 return 0;
903}
904
905static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
906 struct v4l2_format *vf)
907{
908 struct usb_usbvision *usbvision = video_drvdata(file);
909 int format_idx;
910
911 /* Find requested format in available ones */
912 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
913 if (vf->fmt.pix.pixelformat ==
914 usbvision_v4l2_format[format_idx].format) {
915 usbvision->palette = usbvision_v4l2_format[format_idx];
916 break;
917 }
918 }
919 /* robustness */
920 if (format_idx == USBVISION_SUPPORTED_PALETTES)
921 return -EINVAL;
922 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
923 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
924
925 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
926 usbvision->palette.bytes_per_pixel;
927 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
928
929 return 0;
930}
931
932static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
933 struct v4l2_format *vf)
934{
935 struct usb_usbvision *usbvision = video_drvdata(file);
936 int ret;
937
938 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
939 if (ret)
940 return ret;
941
942 /* stop io in case it is already in progress */
943 if (usbvision->streaming == stream_on) {
944 ret = usbvision_stream_interrupt(usbvision);
945 if (ret)
946 return ret;
947 }
948 usbvision_frames_free(usbvision);
949 usbvision_empty_framequeues(usbvision);
950
951 usbvision->cur_frame = NULL;
952
953 /* by now we are committed to the new data... */
954 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
955
956 return 0;
957}
958
959static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
960 size_t count, loff_t *ppos)
961{
962 struct usb_usbvision *usbvision = video_drvdata(file);
963 int noblock = file->f_flags & O_NONBLOCK;
964 unsigned long lock_flags;
965 int ret, i;
966 struct usbvision_frame *frame;
967
968 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
969 (unsigned long)count, noblock);
970
971 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
972 return -EFAULT;
973
974 /* This entry point is compatible with the mmap routines
975 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
976 to get frames or call read on the device. */
977 if (!usbvision->num_frames) {
978 /* First, allocate some frames to work with
979 if this has not been done with VIDIOC_REQBUF */
980 usbvision_frames_free(usbvision);
981 usbvision_empty_framequeues(usbvision);
982 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
983 }
984
985 if (usbvision->streaming != stream_on) {
986 /* no stream is running, make it running ! */
987 usbvision->streaming = stream_on;
988 call_all(usbvision, video, s_stream, 1);
989 }
990
991 /* Then, enqueue as many frames as possible
992 (like a user of VIDIOC_QBUF would do) */
993 for (i = 0; i < usbvision->num_frames; i++) {
994 frame = &usbvision->frame[i];
995 if (frame->grabstate == frame_state_unused) {
996 /* Mark it as ready and enqueue frame */
997 frame->grabstate = frame_state_ready;
998 frame->scanstate = scan_state_scanning;
999 /* Accumulated in usbvision_parse_data() */
1000 frame->scanlength = 0;
1001
1002 /* set v4l2_format index */
1003 frame->v4l2_format = usbvision->palette;
1004
1005 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1006 list_add_tail(&frame->frame, &usbvision->inqueue);
1007 spin_unlock_irqrestore(&usbvision->queue_lock,
1008 lock_flags);
1009 }
1010 }
1011
1012 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1013 if (list_empty(&(usbvision->outqueue))) {
1014 if (noblock)
1015 return -EAGAIN;
1016
1017 ret = wait_event_interruptible
1018 (usbvision->wait_frame,
1019 !list_empty(&(usbvision->outqueue)));
1020 if (ret)
1021 return ret;
1022 }
1023
1024 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1025 frame = list_entry(usbvision->outqueue.next,
1026 struct usbvision_frame, frame);
1027 list_del(usbvision->outqueue.next);
1028 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1029
1030 /* An error returns an empty frame */
1031 if (frame->grabstate == frame_state_error) {
1032 frame->bytes_read = 0;
1033 return 0;
1034 }
1035
1036 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1037 __func__,
1038 frame->index, frame->bytes_read, frame->scanlength);
1039
1040 /* copy bytes to user space; we allow for partials reads */
1041 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1042 count = frame->scanlength - frame->bytes_read;
1043
1044 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
1045 return -EFAULT;
1046
1047 frame->bytes_read += count;
1048 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1049 __func__,
1050 (unsigned long)count, frame->bytes_read);
1051
1052 /* For now, forget the frame if it has not been read in one shot. */
1053/* if (frame->bytes_read >= frame->scanlength) {*/ /* All data has been read */
1054 frame->bytes_read = 0;
1055
1056 /* Mark it as available to be used again. */
1057 frame->grabstate = frame_state_unused;
1058/* } */
1059
1060 return count;
1061}
1062
1063static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1064{
1065 unsigned long size = vma->vm_end - vma->vm_start,
1066 start = vma->vm_start;
1067 void *pos;
1068 u32 i;
1069 struct usb_usbvision *usbvision = video_drvdata(file);
1070
1071 PDEBUG(DBG_MMAP, "mmap");
1072
1073 if (!USBVISION_IS_OPERATIONAL(usbvision))
1074 return -EFAULT;
1075
1076 if (!(vma->vm_flags & VM_WRITE) ||
1077 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1078 return -EINVAL;
1079 }
1080
1081 for (i = 0; i < usbvision->num_frames; i++) {
1082 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1083 vma->vm_pgoff)
1084 break;
1085 }
1086 if (i == usbvision->num_frames) {
1087 PDEBUG(DBG_MMAP,
1088 "mmap: user supplied mapping address is out of range");
1089 return -EINVAL;
1090 }
1091
1092 /* VM_IO is eventually going to replace PageReserved altogether */
1093 vma->vm_flags |= VM_IO;
1094 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1095
1096 pos = usbvision->frame[i].data;
1097 while (size > 0) {
1098 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1099 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1100 return -EAGAIN;
1101 }
1102 start += PAGE_SIZE;
1103 pos += PAGE_SIZE;
1104 size -= PAGE_SIZE;
1105 }
1106
1107 return 0;
1108}
1109
1110
1111/*
1112 * Here comes the stuff for radio on usbvision based devices
1113 *
1114 */
1115static int usbvision_radio_open(struct file *file)
1116{
1117 struct usb_usbvision *usbvision = video_drvdata(file);
1118 int err_code = 0;
1119
1120 PDEBUG(DBG_IO, "%s:", __func__);
1121
1122 if (usbvision->user) {
1123 dev_err(&usbvision->rdev->dev,
1124 "%s: Someone tried to open an already opened USBVision Radio!\n",
1125 __func__);
1126 err_code = -EBUSY;
1127 } else {
1128 if (power_on_at_open) {
1129 usbvision_reset_power_off_timer(usbvision);
1130 if (usbvision->power == 0) {
1131 usbvision_power_on(usbvision);
1132 usbvision_i2c_register(usbvision);
1133 }
1134 }
1135
1136 /* Alternate interface 1 is is the biggest frame size */
1137 err_code = usbvision_set_alternate(usbvision);
1138 if (err_code < 0) {
1139 usbvision->last_error = err_code;
1140 err_code = -EBUSY;
1141 goto out;
1142 }
1143
1144 /* If so far no errors then we shall start the radio */
1145 usbvision->radio = 1;
1146 call_all(usbvision, tuner, s_radio);
1147 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1148 usbvision->user++;
1149 }
1150
1151 if (err_code) {
1152 if (power_on_at_open) {
1153 usbvision_i2c_unregister(usbvision);
1154 usbvision_power_off(usbvision);
1155 usbvision->initialized = 0;
1156 }
1157 }
1158out:
1159 return err_code;
1160}
1161
1162
1163static int usbvision_radio_close(struct file *file)
1164{
1165 struct usb_usbvision *usbvision = video_drvdata(file);
1166 int err_code = 0;
1167
1168 PDEBUG(DBG_IO, "");
1169
1170 /* Set packet size to 0 */
1171 usbvision->iface_alt = 0;
1172 err_code = usb_set_interface(usbvision->dev, usbvision->iface,
1173 usbvision->iface_alt);
1174
1175 usbvision_audio_off(usbvision);
1176 usbvision->radio = 0;
1177 usbvision->user--;
1178
1179 if (power_on_at_open) {
1180 usbvision_set_power_off_timer(usbvision);
1181 usbvision->initialized = 0;
1182 }
1183
1184 if (usbvision->remove_pending) {
1185 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1186 usbvision_release(usbvision);
1187 }
1188
1189 PDEBUG(DBG_IO, "success");
1190 return err_code;
1191}
1192
1193/* Video registration stuff */
1194
1195/* Video template */
1196static const struct v4l2_file_operations usbvision_fops = {
1197 .owner = THIS_MODULE,
1198 .open = usbvision_v4l2_open,
1199 .release = usbvision_v4l2_close,
1200 .read = usbvision_v4l2_read,
1201 .mmap = usbvision_v4l2_mmap,
1202 .unlocked_ioctl = video_ioctl2,
1203/* .poll = video_poll, */
1204};
1205
1206static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1207 .vidioc_querycap = vidioc_querycap,
1208 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1209 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1210 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1211 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1212 .vidioc_reqbufs = vidioc_reqbufs,
1213 .vidioc_querybuf = vidioc_querybuf,
1214 .vidioc_qbuf = vidioc_qbuf,
1215 .vidioc_dqbuf = vidioc_dqbuf,
1216 .vidioc_s_std = vidioc_s_std,
1217 .vidioc_enum_input = vidioc_enum_input,
1218 .vidioc_g_input = vidioc_g_input,
1219 .vidioc_s_input = vidioc_s_input,
1220 .vidioc_queryctrl = vidioc_queryctrl,
1221 .vidioc_g_audio = vidioc_g_audio,
1222 .vidioc_s_audio = vidioc_s_audio,
1223 .vidioc_g_ctrl = vidioc_g_ctrl,
1224 .vidioc_s_ctrl = vidioc_s_ctrl,
1225 .vidioc_streamon = vidioc_streamon,
1226 .vidioc_streamoff = vidioc_streamoff,
1227 .vidioc_g_tuner = vidioc_g_tuner,
1228 .vidioc_s_tuner = vidioc_s_tuner,
1229 .vidioc_g_frequency = vidioc_g_frequency,
1230 .vidioc_s_frequency = vidioc_s_frequency,
1231#ifdef CONFIG_VIDEO_ADV_DEBUG
1232 .vidioc_g_register = vidioc_g_register,
1233 .vidioc_s_register = vidioc_s_register,
1234#endif
1235};
1236
1237static struct video_device usbvision_video_template = {
1238 .fops = &usbvision_fops,
1239 .ioctl_ops = &usbvision_ioctl_ops,
1240 .name = "usbvision-video",
1241 .release = video_device_release,
1242 .tvnorms = USBVISION_NORMS,
1243 .current_norm = V4L2_STD_PAL
1244};
1245
1246
1247/* Radio template */
1248static const struct v4l2_file_operations usbvision_radio_fops = {
1249 .owner = THIS_MODULE,
1250 .open = usbvision_radio_open,
1251 .release = usbvision_radio_close,
1252 .unlocked_ioctl = video_ioctl2,
1253};
1254
1255static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1256 .vidioc_querycap = vidioc_querycap,
1257 .vidioc_enum_input = vidioc_enum_input,
1258 .vidioc_g_input = vidioc_g_input,
1259 .vidioc_s_input = vidioc_s_input,
1260 .vidioc_queryctrl = vidioc_queryctrl,
1261 .vidioc_g_audio = vidioc_g_audio,
1262 .vidioc_s_audio = vidioc_s_audio,
1263 .vidioc_g_ctrl = vidioc_g_ctrl,
1264 .vidioc_s_ctrl = vidioc_s_ctrl,
1265 .vidioc_g_tuner = vidioc_g_tuner,
1266 .vidioc_s_tuner = vidioc_s_tuner,
1267 .vidioc_g_frequency = vidioc_g_frequency,
1268 .vidioc_s_frequency = vidioc_s_frequency,
1269};
1270
1271static struct video_device usbvision_radio_template = {
1272 .fops = &usbvision_radio_fops,
1273 .name = "usbvision-radio",
1274 .release = video_device_release,
1275 .ioctl_ops = &usbvision_radio_ioctl_ops,
1276
1277 .tvnorms = USBVISION_NORMS,
1278 .current_norm = V4L2_STD_PAL
1279};
1280
1281
1282static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1283 struct video_device *vdev_template,
1284 char *name)
1285{
1286 struct usb_device *usb_dev = usbvision->dev;
1287 struct video_device *vdev;
1288
1289 if (usb_dev == NULL) {
1290 dev_err(&usbvision->dev->dev,
1291 "%s: usbvision->dev is not set\n", __func__);
1292 return NULL;
1293 }
1294
1295 vdev = video_device_alloc();
1296 if (NULL == vdev)
1297 return NULL;
1298 *vdev = *vdev_template;
1299 vdev->lock = &usbvision->v4l2_lock;
1300 vdev->v4l2_dev = &usbvision->v4l2_dev;
1301 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1302 video_set_drvdata(vdev, usbvision);
1303 return vdev;
1304}
1305
1306/* unregister video4linux devices */
1307static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1308{
1309 /* Radio Device: */
1310 if (usbvision->rdev) {
1311 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1312 video_device_node_name(usbvision->rdev));
1313 if (video_is_registered(usbvision->rdev))
1314 video_unregister_device(usbvision->rdev);
1315 else
1316 video_device_release(usbvision->rdev);
1317 usbvision->rdev = NULL;
1318 }
1319
1320 /* Video Device: */
1321 if (usbvision->vdev) {
1322 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1323 video_device_node_name(usbvision->vdev));
1324 if (video_is_registered(usbvision->vdev))
1325 video_unregister_device(usbvision->vdev);
1326 else
1327 video_device_release(usbvision->vdev);
1328 usbvision->vdev = NULL;
1329 }
1330}
1331
1332/* register video4linux devices */
1333static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1334{
1335 /* Video Device: */
1336 usbvision->vdev = usbvision_vdev_init(usbvision,
1337 &usbvision_video_template,
1338 "USBVision Video");
1339 if (usbvision->vdev == NULL)
1340 goto err_exit;
1341 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1342 goto err_exit;
1343 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1344 usbvision->nr, video_device_node_name(usbvision->vdev));
1345
1346 /* Radio Device: */
1347 if (usbvision_device_data[usbvision->dev_model].radio) {
1348 /* usbvision has radio */
1349 usbvision->rdev = usbvision_vdev_init(usbvision,
1350 &usbvision_radio_template,
1351 "USBVision Radio");
1352 if (usbvision->rdev == NULL)
1353 goto err_exit;
1354 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1355 goto err_exit;
1356 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1357 usbvision->nr, video_device_node_name(usbvision->rdev));
1358 }
1359 /* all done */
1360 return 0;
1361
1362 err_exit:
1363 dev_err(&usbvision->dev->dev,
1364 "USBVision[%d]: video_register_device() failed\n",
1365 usbvision->nr);
1366 usbvision_unregister_video(usbvision);
1367 return -1;
1368}
1369
1370/*
1371 * usbvision_alloc()
1372 *
1373 * This code allocates the struct usb_usbvision.
1374 * It is filled with default values.
1375 *
1376 * Returns NULL on error, a pointer to usb_usbvision else.
1377 *
1378 */
1379static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1380 struct usb_interface *intf)
1381{
1382 struct usb_usbvision *usbvision;
1383
1384 usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
1385 if (usbvision == NULL)
1386 return NULL;
1387
1388 usbvision->dev = dev;
1389 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1390 goto err_free;
1391
1392 mutex_init(&usbvision->v4l2_lock);
1393
1394 /* prepare control urb for control messages during interrupts */
1395 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1396 if (usbvision->ctrl_urb == NULL)
1397 goto err_unreg;
1398 init_waitqueue_head(&usbvision->ctrl_urb_wq);
1399
1400 usbvision_init_power_off_timer(usbvision);
1401
1402 return usbvision;
1403
1404err_unreg:
1405 v4l2_device_unregister(&usbvision->v4l2_dev);
1406err_free:
1407 kfree(usbvision);
1408 return NULL;
1409}
1410
1411/*
1412 * usbvision_release()
1413 *
1414 * This code does final release of struct usb_usbvision. This happens
1415 * after the device is disconnected -and- all clients closed their files.
1416 *
1417 */
1418static void usbvision_release(struct usb_usbvision *usbvision)
1419{
1420 PDEBUG(DBG_PROBE, "");
1421
1422 usbvision_reset_power_off_timer(usbvision);
1423
1424 usbvision->initialized = 0;
1425
1426 usbvision_remove_sysfs(usbvision->vdev);
1427 usbvision_unregister_video(usbvision);
1428
1429 usb_free_urb(usbvision->ctrl_urb);
1430
1431 v4l2_device_unregister(&usbvision->v4l2_dev);
1432 kfree(usbvision);
1433
1434 PDEBUG(DBG_PROBE, "success");
1435}
1436
1437
1438/*********************** usb interface **********************************/
1439
1440static void usbvision_configure_video(struct usb_usbvision *usbvision)
1441{
1442 int model;
1443
1444 if (usbvision == NULL)
1445 return;
1446
1447 model = usbvision->dev_model;
1448 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1449
1450 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1451 usbvision->vin_reg2_preset =
1452 usbvision_device_data[usbvision->dev_model].vin_reg2;
1453 } else {
1454 usbvision->vin_reg2_preset = 0;
1455 }
1456
1457 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1458
1459 usbvision->video_inputs = usbvision_device_data[model].video_channels;
1460 usbvision->ctl_input = 0;
1461
1462 /* This should be here to make i2c clients to be able to register */
1463 /* first switch off audio */
1464 if (usbvision_device_data[model].audio_channels > 0)
1465 usbvision_audio_off(usbvision);
1466 if (!power_on_at_open) {
1467 /* and then power up the noisy tuner */
1468 usbvision_power_on(usbvision);
1469 usbvision_i2c_register(usbvision);
1470 }
1471}
1472
1473/*
1474 * usbvision_probe()
1475 *
1476 * This procedure queries device descriptor and accepts the interface
1477 * if it looks like USBVISION video device
1478 *
1479 */
1480static int __devinit usbvision_probe(struct usb_interface *intf,
1481 const struct usb_device_id *devid)
1482{
1483 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1484 struct usb_interface *uif;
1485 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1486 const struct usb_host_interface *interface;
1487 struct usb_usbvision *usbvision = NULL;
1488 const struct usb_endpoint_descriptor *endpoint;
1489 int model, i;
1490
1491 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1492 dev->descriptor.idVendor,
1493 dev->descriptor.idProduct, ifnum);
1494
1495 model = devid->driver_info;
1496 if (model < 0 || model >= usbvision_device_data_size) {
1497 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1498 return -ENODEV;
1499 }
1500 printk(KERN_INFO "%s: %s found\n", __func__,
1501 usbvision_device_data[model].model_string);
1502
1503 if (usbvision_device_data[model].interface >= 0)
1504 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1505 else
1506 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1507 endpoint = &interface->endpoint[1].desc;
1508 if (!usb_endpoint_xfer_isoc(endpoint)) {
1509 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1510 __func__, ifnum);
1511 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1512 __func__, endpoint->bmAttributes);
1513 return -ENODEV;
1514 }
1515 if (usb_endpoint_dir_out(endpoint)) {
1516 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1517 __func__, ifnum);
1518 return -ENODEV;
1519 }
1520
1521 usbvision = usbvision_alloc(dev, intf);
1522 if (usbvision == NULL) {
1523 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1524 return -ENOMEM;
1525 }
1526
1527 if (dev->descriptor.bNumConfigurations > 1)
1528 usbvision->bridge_type = BRIDGE_NT1004;
1529 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1530 usbvision->bridge_type = BRIDGE_NT1005;
1531 else
1532 usbvision->bridge_type = BRIDGE_NT1003;
1533 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1534
1535 /* compute alternate max packet sizes */
1536 uif = dev->actconfig->interface[0];
1537
1538 usbvision->num_alt = uif->num_altsetting;
1539 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1540 usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
1541 if (usbvision->alt_max_pkt_size == NULL) {
1542 dev_err(&intf->dev, "usbvision: out of memory!\n");
1543 return -ENOMEM;
1544 }
1545
1546 for (i = 0; i < usbvision->num_alt; i++) {
1547 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1548 wMaxPacketSize);
1549 usbvision->alt_max_pkt_size[i] =
1550 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1551 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1552 usbvision->alt_max_pkt_size[i]);
1553 }
1554
1555
1556 usbvision->nr = usbvision_nr++;
1557
1558 usbvision->have_tuner = usbvision_device_data[model].tuner;
1559 if (usbvision->have_tuner)
1560 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1561
1562 usbvision->dev_model = model;
1563 usbvision->remove_pending = 0;
1564 usbvision->iface = ifnum;
1565 usbvision->iface_alt = 0;
1566 usbvision->video_endp = endpoint->bEndpointAddress;
1567 usbvision->isoc_packet_size = 0;
1568 usbvision->usb_bandwidth = 0;
1569 usbvision->user = 0;
1570 usbvision->streaming = stream_off;
1571 usbvision_configure_video(usbvision);
1572 usbvision_register_video(usbvision);
1573
1574 usbvision_create_sysfs(usbvision->vdev);
1575
1576 PDEBUG(DBG_PROBE, "success");
1577 return 0;
1578}
1579
1580
1581/*
1582 * usbvision_disconnect()
1583 *
1584 * This procedure stops all driver activity, deallocates interface-private
1585 * structure (pointed by 'ptr') and after that driver should be removable
1586 * with no ill consequences.
1587 *
1588 */
1589static void __devexit usbvision_disconnect(struct usb_interface *intf)
1590{
1591 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1592
1593 PDEBUG(DBG_PROBE, "");
1594
1595 if (usbvision == NULL) {
1596 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1597 return;
1598 }
1599
1600 mutex_lock(&usbvision->v4l2_lock);
1601
1602 /* At this time we ask to cancel outstanding URBs */
1603 usbvision_stop_isoc(usbvision);
1604
1605 v4l2_device_disconnect(&usbvision->v4l2_dev);
1606
1607 if (usbvision->power) {
1608 usbvision_i2c_unregister(usbvision);
1609 usbvision_power_off(usbvision);
1610 }
1611 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
1612
1613 usb_put_dev(usbvision->dev);
1614 usbvision->dev = NULL; /* USB device is no more */
1615
1616 mutex_unlock(&usbvision->v4l2_lock);
1617
1618 if (usbvision->user) {
1619 printk(KERN_INFO "%s: In use, disconnect pending\n",
1620 __func__);
1621 wake_up_interruptible(&usbvision->wait_frame);
1622 wake_up_interruptible(&usbvision->wait_stream);
1623 } else {
1624 usbvision_release(usbvision);
1625 }
1626
1627 PDEBUG(DBG_PROBE, "success");
1628}
1629
1630static struct usb_driver usbvision_driver = {
1631 .name = "usbvision",
1632 .id_table = usbvision_table,
1633 .probe = usbvision_probe,
1634 .disconnect = __devexit_p(usbvision_disconnect),
1635};
1636
1637/*
1638 * usbvision_init()
1639 *
1640 * This code is run to initialize the driver.
1641 *
1642 */
1643static int __init usbvision_init(void)
1644{
1645 int err_code;
1646
1647 PDEBUG(DBG_PROBE, "");
1648
1649 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1650 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1651 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1652
1653 /* disable planar mode support unless compression enabled */
1654 if (isoc_mode != ISOC_MODE_COMPRESS) {
1655 /* FIXME : not the right way to set supported flag */
1656 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1657 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1658 }
1659
1660 err_code = usb_register(&usbvision_driver);
1661
1662 if (err_code == 0) {
1663 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1664 PDEBUG(DBG_PROBE, "success");
1665 }
1666 return err_code;
1667}
1668
1669static void __exit usbvision_exit(void)
1670{
1671 PDEBUG(DBG_PROBE, "");
1672
1673 usb_deregister(&usbvision_driver);
1674 PDEBUG(DBG_PROBE, "success");
1675}
1676
1677module_init(usbvision_init);
1678module_exit(usbvision_exit);
1679
1680/*
1681 * Overrides for Emacs so that we follow Linus's tabbing style.
1682 * ---------------------------------------------------------------------------
1683 * Local variables:
1684 * c-basic-offset: 8
1685 * End:
1686 */
diff --git a/drivers/media/video/usbvision/usbvision.h b/drivers/media/video/usbvision/usbvision.h
new file mode 100644
index 00000000000..43cf61fe494
--- /dev/null
+++ b/drivers/media/video/usbvision/usbvision.h
@@ -0,0 +1,535 @@
1/*
2 * USBVISION.H
3 * usbvision header file
4 *
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
7 *
8 *
9 * Report problems to v4l MailingList: linux-media@vger.kernel.org
10 *
11 * This module is part of usbvision driver project.
12 * Updates to driver completed by Dwaine P. Garden
13 * v4l2 conversion by Thierry Merle <thierry.merle@free.fr>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30
31#ifndef __LINUX_USBVISION_H
32#define __LINUX_USBVISION_H
33
34#include <linux/list.h>
35#include <linux/usb.h>
36#include <linux/i2c.h>
37#include <linux/mutex.h>
38#include <media/v4l2-device.h>
39#include <media/tuner.h>
40#include <linux/videodev2.h>
41
42#define USBVISION_DEBUG /* Turn on debug messages */
43
44#define USBVISION_PWR_REG 0x00
45 #define USBVISION_SSPND_EN (1 << 1)
46 #define USBVISION_RES2 (1 << 2)
47 #define USBVISION_PWR_VID (1 << 5)
48 #define USBVISION_E2_EN (1 << 7)
49#define USBVISION_CONFIG_REG 0x01
50#define USBVISION_ADRS_REG 0x02
51#define USBVISION_ALTER_REG 0x03
52#define USBVISION_FORCE_ALTER_REG 0x04
53#define USBVISION_STATUS_REG 0x05
54#define USBVISION_IOPIN_REG 0x06
55 #define USBVISION_IO_1 (1 << 0)
56 #define USBVISION_IO_2 (1 << 1)
57 #define USBVISION_AUDIO_IN 0
58 #define USBVISION_AUDIO_TV 1
59 #define USBVISION_AUDIO_RADIO 2
60 #define USBVISION_AUDIO_MUTE 3
61#define USBVISION_SER_MODE 0x07
62 #define USBVISION_CLK_OUT (1 << 0)
63 #define USBVISION_DAT_IO (1 << 1)
64 #define USBVISION_SENS_OUT (1 << 2)
65 #define USBVISION_SER_MODE_SOFT (0 << 4)
66 #define USBVISION_SER_MODE_SIO (1 << 4)
67#define USBVISION_SER_ADRS 0x08
68#define USBVISION_SER_CONT 0x09
69#define USBVISION_SER_DAT1 0x0A
70#define USBVISION_SER_DAT2 0x0B
71#define USBVISION_SER_DAT3 0x0C
72#define USBVISION_SER_DAT4 0x0D
73#define USBVISION_EE_DATA 0x0E
74#define USBVISION_EE_LSBAD 0x0F
75#define USBVISION_EE_CONT 0x10
76#define USBVISION_DRM_CONT 0x12
77 #define USBVISION_REF (1 << 0)
78 #define USBVISION_RES_UR (1 << 2)
79 #define USBVISION_RES_FDL (1 << 3)
80 #define USBVISION_RES_VDW (1 << 4)
81#define USBVISION_DRM_PRM1 0x13
82#define USBVISION_DRM_PRM2 0x14
83#define USBVISION_DRM_PRM3 0x15
84#define USBVISION_DRM_PRM4 0x16
85#define USBVISION_DRM_PRM5 0x17
86#define USBVISION_DRM_PRM6 0x18
87#define USBVISION_DRM_PRM7 0x19
88#define USBVISION_DRM_PRM8 0x1A
89#define USBVISION_VIN_REG1 0x1B
90 #define USBVISION_8_422_SYNC 0x01
91 #define USBVISION_16_422_SYNC 0x02
92 #define USBVISION_VSNC_POL (1 << 3)
93 #define USBVISION_HSNC_POL (1 << 4)
94 #define USBVISION_FID_POL (1 << 5)
95 #define USBVISION_HVALID_PO (1 << 6)
96 #define USBVISION_VCLK_POL (1 << 7)
97#define USBVISION_VIN_REG2 0x1C
98 #define USBVISION_AUTO_FID (1 << 0)
99 #define USBVISION_NONE_INTER (1 << 1)
100 #define USBVISION_NOHVALID (1 << 2)
101 #define USBVISION_UV_ID (1 << 3)
102 #define USBVISION_FIX_2C (1 << 4)
103 #define USBVISION_SEND_FID (1 << 5)
104 #define USBVISION_KEEP_BLANK (1 << 7)
105#define USBVISION_LXSIZE_I 0x1D
106#define USBVISION_MXSIZE_I 0x1E
107#define USBVISION_LYSIZE_I 0x1F
108#define USBVISION_MYSIZE_I 0x20
109#define USBVISION_LX_OFFST 0x21
110#define USBVISION_MX_OFFST 0x22
111#define USBVISION_LY_OFFST 0x23
112#define USBVISION_MY_OFFST 0x24
113#define USBVISION_FRM_RATE 0x25
114#define USBVISION_LXSIZE_O 0x26
115#define USBVISION_MXSIZE_O 0x27
116#define USBVISION_LYSIZE_O 0x28
117#define USBVISION_MYSIZE_O 0x29
118#define USBVISION_FILT_CONT 0x2A
119#define USBVISION_VO_MODE 0x2B
120#define USBVISION_INTRA_CYC 0x2C
121#define USBVISION_STRIP_SZ 0x2D
122#define USBVISION_FORCE_INTRA 0x2E
123#define USBVISION_FORCE_UP 0x2F
124#define USBVISION_BUF_THR 0x30
125#define USBVISION_DVI_YUV 0x31
126#define USBVISION_AUDIO_CONT 0x32
127#define USBVISION_AUD_PK_LEN 0x33
128#define USBVISION_BLK_PK_LEN 0x34
129#define USBVISION_PCM_THR1 0x38
130#define USBVISION_PCM_THR2 0x39
131#define USBVISION_DIST_THR_L 0x3A
132#define USBVISION_DIST_THR_H 0x3B
133#define USBVISION_MAX_DIST_L 0x3C
134#define USBVISION_MAX_DIST_H 0x3D
135#define USBVISION_OP_CODE 0x33
136
137#define MAX_BYTES_PER_PIXEL 4
138
139#define MIN_FRAME_WIDTH 64
140#define MAX_USB_WIDTH 320 /* 384 */
141#define MAX_FRAME_WIDTH 320 /* 384 */ /* streching sometimes causes crashes*/
142
143#define MIN_FRAME_HEIGHT 48
144#define MAX_USB_HEIGHT 240 /* 288 */
145#define MAX_FRAME_HEIGHT 240 /* 288 */ /* Streching sometimes causes crashes*/
146
147#define MAX_FRAME_SIZE (MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * MAX_BYTES_PER_PIXEL)
148#define USBVISION_CLIPMASK_SIZE (MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT / 8) /* bytesize of clipmask */
149
150#define USBVISION_URB_FRAMES 32
151
152#define USBVISION_NUM_HEADERMARKER 20
153#define USBVISION_NUMFRAMES 3 /* Maximum number of frames an application can get */
154#define USBVISION_NUMSBUF 2 /* Dimensioning the USB S buffering */
155
156#define USBVISION_POWEROFF_TIME (3 * HZ) /* 3 seconds */
157
158
159#define FRAMERATE_MIN 0
160#define FRAMERATE_MAX 31
161
162enum {
163 ISOC_MODE_YUV422 = 0x03,
164 ISOC_MODE_YUV420 = 0x14,
165 ISOC_MODE_COMPRESS = 0x60,
166};
167
168/* This macro restricts an int variable to an inclusive range */
169#define RESTRICT_TO_RANGE(v, mi, ma) \
170 { if ((v) < (mi)) (v) = (mi); else if ((v) > (ma)) (v) = (ma); }
171
172/*
173 * We use macros to do YUV -> RGB conversion because this is
174 * very important for speed and totally unimportant for size.
175 *
176 * YUV -> RGB Conversion
177 * ---------------------
178 *
179 * B = 1.164*(Y-16) + 2.018*(V-128)
180 * G = 1.164*(Y-16) - 0.813*(U-128) - 0.391*(V-128)
181 * R = 1.164*(Y-16) + 1.596*(U-128)
182 *
183 * If you fancy integer arithmetics (as you should), hear this:
184 *
185 * 65536*B = 76284*(Y-16) + 132252*(V-128)
186 * 65536*G = 76284*(Y-16) - 53281*(U-128) - 25625*(V-128)
187 * 65536*R = 76284*(Y-16) + 104595*(U-128)
188 *
189 * Make sure the output values are within [0..255] range.
190 */
191#define LIMIT_RGB(x) (((x) < 0) ? 0 : (((x) > 255) ? 255 : (x)))
192#define YUV_TO_RGB_BY_THE_BOOK(my, mu, mv, mr, mg, mb) { \
193 int mm_y, mm_yc, mm_u, mm_v, mm_r, mm_g, mm_b; \
194 mm_y = (my) - 16; \
195 mm_u = (mu) - 128; \
196 mm_v = (mv) - 128; \
197 mm_yc = mm_y * 76284; \
198 mm_b = (mm_yc + 132252 * mm_v) >> 16; \
199 mm_g = (mm_yc - 53281 * mm_u - 25625 * mm_v) >> 16; \
200 mm_r = (mm_yc + 104595 * mm_u) >> 16; \
201 mb = LIMIT_RGB(mm_b); \
202 mg = LIMIT_RGB(mm_g); \
203 mr = LIMIT_RGB(mm_r); \
204}
205
206/* Debugging aid */
207#define USBVISION_SAY_AND_WAIT(what) { \
208 wait_queue_head_t wq; \
209 init_waitqueue_head(&wq); \
210 printk(KERN_INFO "Say: %s\n", what); \
211 interruptible_sleep_on_timeout(&wq, HZ * 3); \
212}
213
214/*
215 * This macro checks if usbvision is still operational. The 'usbvision'
216 * pointer must be valid, usbvision->dev must be valid, we are not
217 * removing the device and the device has not erred on us.
218 */
219#define USBVISION_IS_OPERATIONAL(udevice) (\
220 (udevice != NULL) && \
221 ((udevice)->dev != NULL) && \
222 ((udevice)->last_error == 0) && \
223 (!(udevice)->remove_pending))
224
225#define I2C_USB_ADAP_MAX 16
226
227#define USBVISION_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_M)
228
229/* ----------------------------------------------------------------- */
230/* usbvision video structures */
231/* ----------------------------------------------------------------- */
232enum scan_state {
233 scan_state_scanning, /* Scanning for header */
234 scan_state_lines /* Parsing lines */
235};
236
237/* Completion states of the data parser */
238enum parse_state {
239 parse_state_continue, /* Just parse next item */
240 parse_state_next_frame, /* Frame done, send it to V4L */
241 parse_state_out, /* Not enough data for frame */
242 parse_state_end_parse /* End parsing */
243};
244
245enum frame_state {
246 frame_state_unused, /* Unused (no MCAPTURE) */
247 frame_state_ready, /* Ready to start grabbing */
248 frame_state_grabbing, /* In the process of being grabbed into */
249 frame_state_done, /* Finished grabbing, but not been synced yet */
250 frame_state_done_hold, /* Are syncing or reading */
251 frame_state_error, /* Something bad happened while processing */
252};
253
254/* stream states */
255enum stream_state {
256 stream_off, /* Driver streaming is completely OFF */
257 stream_idle, /* Driver streaming is ready to be put ON by the application */
258 stream_interrupt, /* Driver streaming must be interrupted */
259 stream_on, /* Driver streaming is put ON by the application */
260};
261
262enum isoc_state {
263 isoc_state_in_frame, /* Isoc packet is member of frame */
264 isoc_state_no_frame, /* Isoc packet is not member of any frame */
265};
266
267struct usb_device;
268
269struct usbvision_sbuf {
270 char *data;
271 struct urb *urb;
272};
273
274#define USBVISION_MAGIC_1 0x55
275#define USBVISION_MAGIC_2 0xAA
276#define USBVISION_HEADER_LENGTH 0x0c
277#define USBVISION_SAA7111_ADDR 0x48
278#define USBVISION_SAA7113_ADDR 0x4a
279#define USBVISION_IIC_LRACK 0x20
280#define USBVISION_IIC_LRNACK 0x30
281#define USBVISION_FRAME_FORMAT_PARAM_INTRA (1<<7)
282
283struct usbvision_v4l2_format_st {
284 int supported;
285 int bytes_per_pixel;
286 int depth;
287 int format;
288 char *desc;
289};
290#define USBVISION_SUPPORTED_PALETTES ARRAY_SIZE(usbvision_v4l2_format)
291
292struct usbvision_frame_header {
293 unsigned char magic_1; /* 0 magic */
294 unsigned char magic_2; /* 1 magic */
295 unsigned char header_length; /* 2 */
296 unsigned char frame_num; /* 3 */
297 unsigned char frame_phase; /* 4 */
298 unsigned char frame_latency; /* 5 */
299 unsigned char data_format; /* 6 */
300 unsigned char format_param; /* 7 */
301 unsigned char frame_width_lo; /* 8 */
302 unsigned char frame_width_hi; /* 9 */
303 unsigned char frame_height_lo; /* 10 */
304 unsigned char frame_height_hi; /* 11 */
305 __u16 frame_width; /* 8 - 9 after endian correction*/
306 __u16 frame_height; /* 10 - 11 after endian correction*/
307};
308
309struct usbvision_frame {
310 char *data; /* Frame buffer */
311 struct usbvision_frame_header isoc_header; /* Header from stream */
312
313 int width; /* Width application is expecting */
314 int height; /* Height */
315 int index; /* Frame index */
316 int frmwidth; /* Width the frame actually is */
317 int frmheight; /* Height */
318
319 volatile int grabstate; /* State of grabbing */
320 int scanstate; /* State of scanning */
321
322 struct list_head frame;
323
324 int curline; /* Line of frame we're working on */
325
326 long scanlength; /* uncompressed, raw data length of frame */
327 long bytes_read; /* amount of scanlength that has been read from data */
328 struct usbvision_v4l2_format_st v4l2_format; /* format the user needs*/
329 int v4l2_linesize; /* bytes for one videoline*/
330 struct timeval timestamp;
331 int sequence; /* How many video frames we send to user */
332};
333
334#define CODEC_SAA7113 7113
335#define CODEC_SAA7111 7111
336#define CODEC_WEBCAM 3000
337#define BRIDGE_NT1003 1003
338#define BRIDGE_NT1004 1004
339#define BRIDGE_NT1005 1005
340
341struct usbvision_device_data_st {
342 __u64 video_norm;
343 const char *model_string;
344 int interface; /* to handle special interface number like BELKIN and Hauppauge WinTV-USB II */
345 __u16 codec;
346 unsigned video_channels:3;
347 unsigned audio_channels:2;
348 unsigned radio:1;
349 unsigned vbi:1;
350 unsigned tuner:1;
351 unsigned vin_reg1_override:1; /* Override default value with */
352 unsigned vin_reg2_override:1; /* vin_reg1, vin_reg2, etc. */
353 unsigned dvi_yuv_override:1;
354 __u8 vin_reg1;
355 __u8 vin_reg2;
356 __u8 dvi_yuv;
357 __u8 tuner_type;
358 __s16 x_offset;
359 __s16 y_offset;
360};
361
362/* Declared on usbvision-cards.c */
363extern struct usbvision_device_data_st usbvision_device_data[];
364extern struct usb_device_id usbvision_table[];
365
366struct usb_usbvision {
367 struct v4l2_device v4l2_dev;
368 struct video_device *vdev; /* Video Device */
369 struct video_device *rdev; /* Radio Device */
370
371 /* i2c Declaration Section*/
372 struct i2c_adapter i2c_adap;
373 int registered_i2c;
374
375 struct urb *ctrl_urb;
376 unsigned char ctrl_urb_buffer[8];
377 int ctrl_urb_busy;
378 struct usb_ctrlrequest ctrl_urb_setup;
379 wait_queue_head_t ctrl_urb_wq; /* Processes waiting */
380
381 /* configuration part */
382 int have_tuner;
383 int tuner_type;
384 int bridge_type; /* NT1003, NT1004, NT1005 */
385 int radio;
386 int video_inputs; /* # of inputs */
387 unsigned long freq;
388 int audio_mute;
389 int audio_channel;
390 int isoc_mode; /* format of video data for the usb isoc-transfer */
391 unsigned int nr; /* Number of the device */
392
393 /* Device structure */
394 struct usb_device *dev;
395 /* usb transfer */
396 int num_alt; /* Number of alternative settings */
397 unsigned int *alt_max_pkt_size; /* array of max_packet_size */
398 unsigned char iface; /* Video interface number */
399 unsigned char iface_alt; /* Alt settings */
400 unsigned char vin_reg2_preset;
401 struct mutex v4l2_lock;
402 struct timer_list power_off_timer;
403 struct work_struct power_off_work;
404 int power; /* is the device powered on? */
405 int user; /* user count for exclusive use */
406 int initialized; /* Had we already sent init sequence? */
407 int dev_model; /* What type of USBVISION device we got? */
408 enum stream_state streaming; /* Are we streaming Isochronous? */
409 int last_error; /* What calamity struck us? */
410 int curwidth; /* width of the frame the device is currently set to*/
411 int curheight; /* height of the frame the device is currently set to*/
412 int stretch_width; /* stretch-factor for frame width (from usb to screen)*/
413 int stretch_height; /* stretch-factor for frame height (from usb to screen)*/
414 char *fbuf; /* Videodev buffer area for mmap*/
415 int max_frame_size; /* Bytes in one video frame */
416 int fbuf_size; /* Videodev buffer size */
417 spinlock_t queue_lock; /* spinlock for protecting mods on inqueue and outqueue */
418 struct list_head inqueue, outqueue; /* queued frame list and ready to dequeue frame list */
419 wait_queue_head_t wait_frame; /* Processes waiting */
420 wait_queue_head_t wait_stream; /* Processes waiting */
421 struct usbvision_frame *cur_frame; /* pointer to current frame, set by usbvision_find_header */
422 struct usbvision_frame frame[USBVISION_NUMFRAMES]; /* frame buffer */
423 int num_frames; /* number of frames allocated */
424 struct usbvision_sbuf sbuf[USBVISION_NUMSBUF]; /* S buffering */
425 volatile int remove_pending; /* If set then about to exit */
426
427 /* Scratch space from the Isochronous Pipe.*/
428 unsigned char *scratch;
429 int scratch_read_ptr;
430 int scratch_write_ptr;
431 int scratch_headermarker[USBVISION_NUM_HEADERMARKER];
432 int scratch_headermarker_read_ptr;
433 int scratch_headermarker_write_ptr;
434 enum isoc_state isocstate;
435 struct usbvision_v4l2_format_st palette;
436
437 struct v4l2_capability vcap; /* Video capabilities */
438 unsigned int ctl_input; /* selected input */
439 v4l2_std_id tvnorm_id; /* selected tv norm */
440 unsigned char video_endp; /* 0x82 for USBVISION devices based */
441
442 /* Decompression stuff: */
443 unsigned char *intra_frame_buffer; /* Buffer for reference frame */
444 int block_pos; /* for test only */
445 int request_intra; /* 0 = normal; 1 = intra frame is requested; */
446 int last_isoc_frame_num; /* check for lost isoc frames */
447 int isoc_packet_size; /* need to calculate used_bandwidth */
448 int used_bandwidth; /* used bandwidth 0-100%, need to set compr_level */
449 int compr_level; /* How strong (100) or weak (0) is compression */
450 int last_compr_level; /* How strong (100) or weak (0) was compression */
451 int usb_bandwidth; /* Mbit/s */
452
453 /* Statistics that can be overlayed on the screen */
454 unsigned long isoc_urb_count; /* How many URBs we received so far */
455 unsigned long urb_length; /* Length of last URB */
456 unsigned long isoc_data_count; /* How many bytes we received */
457 unsigned long header_count; /* How many frame headers we found */
458 unsigned long scratch_ovf_count; /* How many times we overflowed scratch */
459 unsigned long isoc_skip_count; /* How many empty ISO packets received */
460 unsigned long isoc_err_count; /* How many bad ISO packets received */
461 unsigned long isoc_packet_count; /* How many packets we totally got */
462 unsigned long time_in_irq; /* How long do we need for interrupt */
463 int isoc_measure_bandwidth_count;
464 int frame_num; /* How many video frames we send to user */
465 int max_strip_len; /* How big is the biggest strip */
466 int comprblock_pos;
467 int strip_len_errors; /* How many times was block_pos greater than strip_len */
468 int strip_magic_errors;
469 int strip_line_number_errors;
470 int compr_block_types[4];
471};
472
473static inline struct usb_usbvision *to_usbvision(struct v4l2_device *v4l2_dev)
474{
475 return container_of(v4l2_dev, struct usb_usbvision, v4l2_dev);
476}
477
478#define call_all(usbvision, o, f, args...) \
479 v4l2_device_call_all(&usbvision->v4l2_dev, 0, o, f, ##args)
480
481/* --------------------------------------------------------------- */
482/* defined in usbvision-i2c.c */
483/* i2c-algo-usb declaration */
484/* --------------------------------------------------------------- */
485
486/* ----------------------------------------------------------------------- */
487/* usbvision specific I2C functions */
488/* ----------------------------------------------------------------------- */
489int usbvision_i2c_register(struct usb_usbvision *usbvision);
490int usbvision_i2c_unregister(struct usb_usbvision *usbvision);
491
492/* defined in usbvision-core.c */
493int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg);
494int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
495 unsigned char value);
496
497int usbvision_frames_alloc(struct usb_usbvision *usbvision, int number_of_frames);
498void usbvision_frames_free(struct usb_usbvision *usbvision);
499int usbvision_scratch_alloc(struct usb_usbvision *usbvision);
500void usbvision_scratch_free(struct usb_usbvision *usbvision);
501int usbvision_decompress_alloc(struct usb_usbvision *usbvision);
502void usbvision_decompress_free(struct usb_usbvision *usbvision);
503
504int usbvision_setup(struct usb_usbvision *usbvision, int format);
505int usbvision_init_isoc(struct usb_usbvision *usbvision);
506int usbvision_restart_isoc(struct usb_usbvision *usbvision);
507void usbvision_stop_isoc(struct usb_usbvision *usbvision);
508int usbvision_set_alternate(struct usb_usbvision *dev);
509
510int usbvision_set_audio(struct usb_usbvision *usbvision, int audio_channel);
511int usbvision_audio_off(struct usb_usbvision *usbvision);
512
513int usbvision_begin_streaming(struct usb_usbvision *usbvision);
514void usbvision_empty_framequeues(struct usb_usbvision *dev);
515int usbvision_stream_interrupt(struct usb_usbvision *dev);
516
517int usbvision_muxsel(struct usb_usbvision *usbvision, int channel);
518int usbvision_set_input(struct usb_usbvision *usbvision);
519int usbvision_set_output(struct usb_usbvision *usbvision, int width, int height);
520
521void usbvision_init_power_off_timer(struct usb_usbvision *usbvision);
522void usbvision_set_power_off_timer(struct usb_usbvision *usbvision);
523void usbvision_reset_power_off_timer(struct usb_usbvision *usbvision);
524int usbvision_power_off(struct usb_usbvision *usbvision);
525int usbvision_power_on(struct usb_usbvision *usbvision);
526
527#endif /* __LINUX_USBVISION_H */
528
529/*
530 * Overrides for Emacs so that we follow Linus's tabbing style.
531 * ---------------------------------------------------------------------------
532 * Local variables:
533 * c-basic-offset: 8
534 * End:
535 */