1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/version.h>
49#include <linux/kernel.h>
50#include <linux/list.h>
51#include <linux/timer.h>
52#include <linux/slab.h>
53#include <linux/mm.h>
54#include <linux/utsname.h>
55#include <linux/highmem.h>
56#include <linux/videodev.h>
57#include <linux/vmalloc.h>
58#include <linux/module.h>
59#include <linux/init.h>
60#include <linux/spinlock.h>
61#include <asm/io.h>
62#include <linux/videodev2.h>
63#include <linux/video_decoder.h>
64#include <linux/i2c.h>
65
66#include <media/saa7115.h>
67#include <media/v4l2-common.h>
68#include <media/tuner.h>
69#include <media/audiochip.h>
70
71#include <linux/workqueue.h>
72
73#ifdef CONFIG_KMOD
74#include <linux/kmod.h>
75#endif
76
77#include "usbvision.h"
78#include "usbvision-cards.h"
79
80#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
81 Dwaine Garden <DwaineGarden@rogers.com>"
82#define DRIVER_NAME "usbvision"
83#define DRIVER_ALIAS "USBVision"
84#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85#define DRIVER_LICENSE "GPL"
86#define USBVISION_DRIVER_VERSION_MAJOR 0
87#define USBVISION_DRIVER_VERSION_MINOR 9
88#define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
89#define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
90USBVISION_DRIVER_VERSION_MINOR,\
91USBVISION_DRIVER_VERSION_PATCHLEVEL)
92#define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
93 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
94 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
95
96#define ENABLE_HEXDUMP 0
97
98
99#ifdef USBVISION_DEBUG
100 #define PDEBUG(level, fmt, args...) { \
101 if (video_debug & (level)) \
102 info("[%s:%d] " fmt, __func__, __LINE__ , ## args); \
103 }
104#else
105 #define PDEBUG(level, fmt, args...) do {} while(0)
106#endif
107
108#define DBG_IO 1<<1
109#define DBG_PROBE 1<<2
110#define DBG_MMAP 1<<3
111
112
113#define rmspace(str) while(*str==' ') str++;
114#define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
115
116
117
118static int usbvision_nr;
119
120static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
121 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
122 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
123 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
124 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
125 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
126 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
127 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" },
128 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
129};
130
131
132static void usbvision_release(struct usb_usbvision *usbvision);
133
134
135
136static int isocMode = ISOC_MODE_COMPRESS;
137
138static int video_debug;
139
140static int PowerOnAtOpen = 1;
141
142static int video_nr = -1;
143
144static int radio_nr = -1;
145
146static int vbi_nr = -1;
147
148
149
150
151module_param(isocMode, int, 0444);
152module_param(video_debug, int, 0444);
153module_param(PowerOnAtOpen, int, 0444);
154module_param(video_nr, int, 0444);
155module_param(radio_nr, int, 0444);
156module_param(vbi_nr, int, 0444);
157
158MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
159MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
160MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
161MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
162MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
163MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
164
165
166
167MODULE_AUTHOR(DRIVER_AUTHOR);
168MODULE_DESCRIPTION(DRIVER_DESC);
169MODULE_LICENSE(DRIVER_LICENSE);
170MODULE_VERSION(USBVISION_VERSION_STRING);
171MODULE_ALIAS(DRIVER_ALIAS);
172
173
174
175
176
177
178
179
180
181
182
183#define YES_NO(x) ((x) ? "Yes" : "No")
184
185static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
186{
187 struct video_device *vdev =
188 container_of(cd, struct video_device, class_dev);
189 return video_get_drvdata(vdev);
190}
191
192static ssize_t show_version(struct device *cd,
193 struct device_attribute *attr, char *buf)
194{
195 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
196}
197static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
198
199static ssize_t show_model(struct device *cd,
200 struct device_attribute *attr, char *buf)
201{
202 struct video_device *vdev =
203 container_of(cd, struct video_device, class_dev);
204 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
205 return sprintf(buf, "%s\n",
206 usbvision_device_data[usbvision->DevModel].ModelString);
207}
208static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
209
210static ssize_t show_hue(struct device *cd,
211 struct device_attribute *attr, char *buf)
212{
213 struct video_device *vdev =
214 container_of(cd, struct video_device, class_dev);
215 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
216 struct v4l2_control ctrl;
217 ctrl.id = V4L2_CID_HUE;
218 ctrl.value = 0;
219 if(usbvision->user)
220 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
221 return sprintf(buf, "%d\n", ctrl.value);
222}
223static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
224
225static ssize_t show_contrast(struct device *cd,
226 struct device_attribute *attr, char *buf)
227{
228 struct video_device *vdev =
229 container_of(cd, struct video_device, class_dev);
230 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
231 struct v4l2_control ctrl;
232 ctrl.id = V4L2_CID_CONTRAST;
233 ctrl.value = 0;
234 if(usbvision->user)
235 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
236 return sprintf(buf, "%d\n", ctrl.value);
237}
238static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
239
240static ssize_t show_brightness(struct device *cd,
241 struct device_attribute *attr, char *buf)
242{
243 struct video_device *vdev =
244 container_of(cd, struct video_device, class_dev);
245 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
246 struct v4l2_control ctrl;
247 ctrl.id = V4L2_CID_BRIGHTNESS;
248 ctrl.value = 0;
249 if(usbvision->user)
250 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
251 return sprintf(buf, "%d\n", ctrl.value);
252}
253static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
254
255static ssize_t show_saturation(struct device *cd,
256 struct device_attribute *attr, char *buf)
257{
258 struct video_device *vdev =
259 container_of(cd, struct video_device, class_dev);
260 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
261 struct v4l2_control ctrl;
262 ctrl.id = V4L2_CID_SATURATION;
263 ctrl.value = 0;
264 if(usbvision->user)
265 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
266 return sprintf(buf, "%d\n", ctrl.value);
267}
268static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
269
270static ssize_t show_streaming(struct device *cd,
271 struct device_attribute *attr, char *buf)
272{
273 struct video_device *vdev =
274 container_of(cd, struct video_device, class_dev);
275 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
276 return sprintf(buf, "%s\n",
277 YES_NO(usbvision->streaming==Stream_On?1:0));
278}
279static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
280
281static ssize_t show_compression(struct device *cd,
282 struct device_attribute *attr, char *buf)
283{
284 struct video_device *vdev =
285 container_of(cd, struct video_device, class_dev);
286 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
287 return sprintf(buf, "%s\n",
288 YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
289}
290static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
291
292static ssize_t show_device_bridge(struct device *cd,
293 struct device_attribute *attr, char *buf)
294{
295 struct video_device *vdev =
296 container_of(cd, struct video_device, class_dev);
297 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
298 return sprintf(buf, "%d\n", usbvision->bridgeType);
299}
300static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
301
302static void usbvision_create_sysfs(struct video_device *vdev)
303{
304 int res;
305 if (!vdev)
306 return;
307 do {
308 res = device_create_file(&vdev->class_dev,
309 &dev_attr_version);
310 if (res<0)
311 break;
312 res = device_create_file(&vdev->class_dev,
313 &dev_attr_model);
314 if (res<0)
315 break;
316 res = device_create_file(&vdev->class_dev,
317 &dev_attr_hue);
318 if (res<0)
319 break;
320 res = device_create_file(&vdev->class_dev,
321 &dev_attr_contrast);
322 if (res<0)
323 break;
324 res = device_create_file(&vdev->class_dev,
325 &dev_attr_brightness);
326 if (res<0)
327 break;
328 res = device_create_file(&vdev->class_dev,
329 &dev_attr_saturation);
330 if (res<0)
331 break;
332 res = device_create_file(&vdev->class_dev,
333 &dev_attr_streaming);
334 if (res<0)
335 break;
336 res = device_create_file(&vdev->class_dev,
337 &dev_attr_compression);
338 if (res<0)
339 break;
340 res = device_create_file(&vdev->class_dev,
341 &dev_attr_bridge);
342 if (res>=0)
343 return;
344 } while (0);
345
346 err("%s error: %d\n", __func__, res);
347}
348
349static void usbvision_remove_sysfs(struct video_device *vdev)
350{
351 if (vdev) {
352 device_remove_file(&vdev->class_dev,
353 &dev_attr_version);
354 device_remove_file(&vdev->class_dev,
355 &dev_attr_model);
356 device_remove_file(&vdev->class_dev,
357 &dev_attr_hue);
358 device_remove_file(&vdev->class_dev,
359 &dev_attr_contrast);
360 device_remove_file(&vdev->class_dev,
361 &dev_attr_brightness);
362 device_remove_file(&vdev->class_dev,
363 &dev_attr_saturation);
364 device_remove_file(&vdev->class_dev,
365 &dev_attr_streaming);
366 device_remove_file(&vdev->class_dev,
367 &dev_attr_compression);
368 device_remove_file(&vdev->class_dev,
369 &dev_attr_bridge);
370 }
371}
372
373
374
375
376
377
378
379
380
381
382static int usbvision_v4l2_open(struct inode *inode, struct file *file)
383{
384 struct video_device *dev = video_devdata(file);
385 struct usb_usbvision *usbvision =
386 (struct usb_usbvision *) video_get_drvdata(dev);
387 int errCode = 0;
388
389 PDEBUG(DBG_IO, "open");
390
391
392 usbvision_reset_powerOffTimer(usbvision);
393
394 if (usbvision->user)
395 errCode = -EBUSY;
396 else {
397
398 errCode = usbvision_scratch_alloc(usbvision);
399 if (isocMode==ISOC_MODE_COMPRESS) {
400
401
402 errCode = usbvision_decompress_alloc(usbvision);
403 }
404 if (errCode) {
405
406 usbvision_scratch_free(usbvision);
407 usbvision_decompress_free(usbvision);
408 }
409 }
410
411
412 if (!errCode) {
413 mutex_lock(&usbvision->lock);
414 if (usbvision->power == 0) {
415 usbvision_power_on(usbvision);
416 usbvision_i2c_register(usbvision);
417 }
418
419
420 if (!usbvision->initialized) {
421 int setup_ok = 0;
422 setup_ok = usbvision_setup(usbvision,isocMode);
423 if (setup_ok)
424 usbvision->initialized = 1;
425 else
426 errCode = -EBUSY;
427 }
428
429 if (!errCode) {
430 usbvision_begin_streaming(usbvision);
431 errCode = usbvision_init_isoc(usbvision);
432
433 usbvision_muxsel(usbvision,0);
434 usbvision->user++;
435 } else {
436 if (PowerOnAtOpen) {
437 usbvision_i2c_unregister(usbvision);
438 usbvision_power_off(usbvision);
439 usbvision->initialized = 0;
440 }
441 }
442 mutex_unlock(&usbvision->lock);
443 }
444
445 if (errCode) {
446 }
447
448
449 usbvision_empty_framequeues(usbvision);
450
451 PDEBUG(DBG_IO, "success");
452 return errCode;
453}
454
455
456
457
458
459
460
461
462
463static int usbvision_v4l2_close(struct inode *inode, struct file *file)
464{
465 struct video_device *dev = video_devdata(file);
466 struct usb_usbvision *usbvision =
467 (struct usb_usbvision *) video_get_drvdata(dev);
468
469 PDEBUG(DBG_IO, "close");
470 mutex_lock(&usbvision->lock);
471
472 usbvision_audio_off(usbvision);
473 usbvision_restart_isoc(usbvision);
474 usbvision_stop_isoc(usbvision);
475
476 usbvision_decompress_free(usbvision);
477 usbvision_frames_free(usbvision);
478 usbvision_empty_framequeues(usbvision);
479 usbvision_scratch_free(usbvision);
480
481 usbvision->user--;
482
483 if (PowerOnAtOpen) {
484
485
486 usbvision_set_powerOffTimer(usbvision);
487 usbvision->initialized = 0;
488 }
489
490 mutex_unlock(&usbvision->lock);
491
492 if (usbvision->remove_pending) {
493 printk(KERN_INFO "%s: Final disconnect\n", __func__);
494 usbvision_release(usbvision);
495 }
496
497 PDEBUG(DBG_IO, "success");
498
499
500 return 0;
501}
502
503
504
505
506
507
508
509
510#ifdef CONFIG_VIDEO_ADV_DEBUG
511static int vidioc_g_register (struct file *file, void *priv,
512 struct v4l2_register *reg)
513{
514 struct video_device *dev = video_devdata(file);
515 struct usb_usbvision *usbvision =
516 (struct usb_usbvision *) video_get_drvdata(dev);
517 int errCode;
518
519 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
520 return -EINVAL;
521
522 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
523 if (errCode < 0) {
524 err("%s: VIDIOC_DBG_G_REGISTER failed: error %d",
525 __func__, errCode);
526 return errCode;
527 }
528 reg->val = errCode;
529 return 0;
530}
531
532static int vidioc_s_register (struct file *file, void *priv,
533 struct v4l2_register *reg)
534{
535 struct video_device *dev = video_devdata(file);
536 struct usb_usbvision *usbvision =
537 (struct usb_usbvision *) video_get_drvdata(dev);
538 int errCode;
539
540 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
541 return -EINVAL;
542
543 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
544 if (errCode < 0) {
545 err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
546 __func__, errCode);
547 return errCode;
548 }
549 return 0;
550}
551#endif
552
553static int vidioc_querycap (struct file *file, void *priv,
554 struct v4l2_capability *vc)
555{
556 struct video_device *dev = video_devdata(file);
557 struct usb_usbvision *usbvision =
558 (struct usb_usbvision *) video_get_drvdata(dev);
559
560 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
561 strlcpy(vc->card,
562 usbvision_device_data[usbvision->DevModel].ModelString,
563 sizeof(vc->card));
564 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
565 sizeof(vc->bus_info));
566 vc->version = USBVISION_DRIVER_VERSION;
567 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
568 V4L2_CAP_AUDIO |
569 V4L2_CAP_READWRITE |
570 V4L2_CAP_STREAMING |
571 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
572 return 0;
573}
574
575static int vidioc_enum_input (struct file *file, void *priv,
576 struct v4l2_input *vi)
577{
578 struct video_device *dev = video_devdata(file);
579 struct usb_usbvision *usbvision =
580 (struct usb_usbvision *) video_get_drvdata(dev);
581 int chan;
582
583 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
584 return -EINVAL;
585 if (usbvision->have_tuner) {
586 chan = vi->index;
587 } else {
588 chan = vi->index + 1;
589 }
590
591
592 switch(chan) {
593 case 0:
594 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
595 strcpy(vi->name, "White Video Input");
596 } else {
597 strcpy(vi->name, "Television");
598 vi->type = V4L2_INPUT_TYPE_TUNER;
599 vi->audioset = 1;
600 vi->tuner = chan;
601 vi->std = USBVISION_NORMS;
602 }
603 break;
604 case 1:
605 vi->type = V4L2_INPUT_TYPE_CAMERA;
606 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
607 strcpy(vi->name, "Green Video Input");
608 } else {
609 strcpy(vi->name, "Composite Video Input");
610 }
611 vi->std = V4L2_STD_PAL;
612 break;
613 case 2:
614 vi->type = V4L2_INPUT_TYPE_CAMERA;
615 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
616 strcpy(vi->name, "Yellow Video Input");
617 } else {
618 strcpy(vi->name, "S-Video Input");
619 }
620 vi->std = V4L2_STD_PAL;
621 break;
622 case 3:
623 vi->type = V4L2_INPUT_TYPE_CAMERA;
624 strcpy(vi->name, "Red Video Input");
625 vi->std = V4L2_STD_PAL;
626 break;
627 }
628 return 0;
629}
630
631static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
632{
633 struct video_device *dev = video_devdata(file);
634 struct usb_usbvision *usbvision =
635 (struct usb_usbvision *) video_get_drvdata(dev);
636
637 *input = usbvision->ctl_input;
638 return 0;
639}
640
641static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
642{
643 struct video_device *dev = video_devdata(file);
644 struct usb_usbvision *usbvision =
645 (struct usb_usbvision *) video_get_drvdata(dev);
646
647 if ((input >= usbvision->video_inputs) || (input < 0) )
648 return -EINVAL;
649
650 mutex_lock(&usbvision->lock);
651 usbvision_muxsel(usbvision, input);
652 usbvision_set_input(usbvision);
653 usbvision_set_output(usbvision,
654 usbvision->curwidth,
655 usbvision->curheight);
656 mutex_unlock(&usbvision->lock);
657 return 0;
658}
659
660static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
661{
662 struct video_device *dev = video_devdata(file);
663 struct usb_usbvision *usbvision =
664 (struct usb_usbvision *) video_get_drvdata(dev);
665 usbvision->tvnormId=*id;
666
667 mutex_lock(&usbvision->lock);
668 call_i2c_clients(usbvision, VIDIOC_S_STD,
669 &usbvision->tvnormId);
670 mutex_unlock(&usbvision->lock);
671
672 usbvision_muxsel(usbvision, usbvision->ctl_input);
673
674 return 0;
675}
676
677static int vidioc_g_tuner (struct file *file, void *priv,
678 struct v4l2_tuner *vt)
679{
680 struct video_device *dev = video_devdata(file);
681 struct usb_usbvision *usbvision =
682 (struct usb_usbvision *) video_get_drvdata(dev);
683
684 if (!usbvision->have_tuner || vt->index)
685 return -EINVAL;
686 if(usbvision->radio) {
687 strcpy(vt->name, "Radio");
688 vt->type = V4L2_TUNER_RADIO;
689 } else {
690 strcpy(vt->name, "Television");
691 }
692
693 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
694
695 return 0;
696}
697
698static int vidioc_s_tuner (struct file *file, void *priv,
699 struct v4l2_tuner *vt)
700{
701 struct video_device *dev = video_devdata(file);
702 struct usb_usbvision *usbvision =
703 (struct usb_usbvision *) video_get_drvdata(dev);
704
705
706 if (!usbvision->have_tuner || vt->index)
707 return -EINVAL;
708
709 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
710
711 return 0;
712}
713
714static int vidioc_g_frequency (struct file *file, void *priv,
715 struct v4l2_frequency *freq)
716{
717 struct video_device *dev = video_devdata(file);
718 struct usb_usbvision *usbvision =
719 (struct usb_usbvision *) video_get_drvdata(dev);
720
721 freq->tuner = 0;
722 if(usbvision->radio) {
723 freq->type = V4L2_TUNER_RADIO;
724 } else {
725 freq->type = V4L2_TUNER_ANALOG_TV;
726 }
727 freq->frequency = usbvision->freq;
728
729 return 0;
730}
731
732static int vidioc_s_frequency (struct file *file, void *priv,
733 struct v4l2_frequency *freq)
734{
735 struct video_device *dev = video_devdata(file);
736 struct usb_usbvision *usbvision =
737 (struct usb_usbvision *) video_get_drvdata(dev);
738
739
740 if (!usbvision->have_tuner || freq->tuner)
741 return -EINVAL;
742
743 usbvision->freq = freq->frequency;
744 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
745
746 return 0;
747}
748
749static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
750{
751 struct video_device *dev = video_devdata(file);
752 struct usb_usbvision *usbvision =
753 (struct usb_usbvision *) video_get_drvdata(dev);
754
755 memset(a,0,sizeof(*a));
756 if(usbvision->radio) {
757 strcpy(a->name,"Radio");
758 } else {
759 strcpy(a->name, "TV");
760 }
761
762 return 0;
763}
764
765static int vidioc_s_audio (struct file *file, void *fh,
766 struct v4l2_audio *a)
767{
768 if(a->index) {
769 return -EINVAL;
770 }
771
772 return 0;
773}
774
775static int vidioc_queryctrl (struct file *file, void *priv,
776 struct v4l2_queryctrl *ctrl)
777{
778 struct video_device *dev = video_devdata(file);
779 struct usb_usbvision *usbvision =
780 (struct usb_usbvision *) video_get_drvdata(dev);
781 int id=ctrl->id;
782
783 memset(ctrl,0,sizeof(*ctrl));
784 ctrl->id=id;
785
786 call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
787
788 if (!ctrl->type)
789 return -EINVAL;
790
791 return 0;
792}
793
794static int vidioc_g_ctrl (struct file *file, void *priv,
795 struct v4l2_control *ctrl)
796{
797 struct video_device *dev = video_devdata(file);
798 struct usb_usbvision *usbvision =
799 (struct usb_usbvision *) video_get_drvdata(dev);
800 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
801
802 return 0;
803}
804
805static int vidioc_s_ctrl (struct file *file, void *priv,
806 struct v4l2_control *ctrl)
807{
808 struct video_device *dev = video_devdata(file);
809 struct usb_usbvision *usbvision =
810 (struct usb_usbvision *) video_get_drvdata(dev);
811 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
812
813 return 0;
814}
815
816static int vidioc_reqbufs (struct file *file,
817 void *priv, struct v4l2_requestbuffers *vr)
818{
819 struct video_device *dev = video_devdata(file);
820 struct usb_usbvision *usbvision =
821 (struct usb_usbvision *) video_get_drvdata(dev);
822 int ret;
823
824 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
825
826
827
828 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
829 (vr->memory != V4L2_MEMORY_MMAP))
830 return -EINVAL;
831
832 if(usbvision->streaming == Stream_On) {
833 if ((ret = usbvision_stream_interrupt(usbvision)))
834 return ret;
835 }
836
837 usbvision_frames_free(usbvision);
838 usbvision_empty_framequeues(usbvision);
839 vr->count = usbvision_frames_alloc(usbvision,vr->count);
840
841 usbvision->curFrame = NULL;
842
843 return 0;
844}
845
846static int vidioc_querybuf (struct file *file,
847 void *priv, struct v4l2_buffer *vb)
848{
849 struct video_device *dev = video_devdata(file);
850 struct usb_usbvision *usbvision =
851 (struct usb_usbvision *) video_get_drvdata(dev);
852 struct usbvision_frame *frame;
853
854
855
856 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
857 return -EINVAL;
858 }
859 if(vb->index>=usbvision->num_frames) {
860 return -EINVAL;
861 }
862
863 vb->flags = 0;
864 frame = &usbvision->frame[vb->index];
865 if(frame->grabstate >= FrameState_Ready)
866 vb->flags |= V4L2_BUF_FLAG_QUEUED;
867 if(frame->grabstate >= FrameState_Done)
868 vb->flags |= V4L2_BUF_FLAG_DONE;
869 if(frame->grabstate == FrameState_Unused)
870 vb->flags |= V4L2_BUF_FLAG_MAPPED;
871 vb->memory = V4L2_MEMORY_MMAP;
872
873 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
874
875 vb->memory = V4L2_MEMORY_MMAP;
876 vb->field = V4L2_FIELD_NONE;
877 vb->length = usbvision->curwidth*
878 usbvision->curheight*
879 usbvision->palette.bytes_per_pixel;
880 vb->timestamp = usbvision->frame[vb->index].timestamp;
881 vb->sequence = usbvision->frame[vb->index].sequence;
882 return 0;
883}
884
885static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
886{
887 struct video_device *dev = video_devdata(file);
888 struct usb_usbvision *usbvision =
889 (struct usb_usbvision *) video_get_drvdata(dev);
890 struct usbvision_frame *frame;
891 unsigned long lock_flags;
892
893
894 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
895 return -EINVAL;
896 }
897 if(vb->index>=usbvision->num_frames) {
898 return -EINVAL;
899 }
900
901 frame = &usbvision->frame[vb->index];
902
903 if (frame->grabstate != FrameState_Unused) {
904 return -EAGAIN;
905 }
906
907
908 frame->grabstate = FrameState_Ready;
909 frame->scanstate = ScanState_Scanning;
910 frame->scanlength = 0;
911
912 vb->flags &= ~V4L2_BUF_FLAG_DONE;
913
914
915 frame->v4l2_format = usbvision->palette;
916
917 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
918 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
919 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
920
921 return 0;
922}
923
924static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
925{
926 struct video_device *dev = video_devdata(file);
927 struct usb_usbvision *usbvision =
928 (struct usb_usbvision *) video_get_drvdata(dev);
929 int ret;
930 struct usbvision_frame *f;
931 unsigned long lock_flags;
932
933 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
934 return -EINVAL;
935
936 if (list_empty(&(usbvision->outqueue))) {
937 if (usbvision->streaming == Stream_Idle)
938 return -EINVAL;
939 ret = wait_event_interruptible
940 (usbvision->wait_frame,
941 !list_empty(&(usbvision->outqueue)));
942 if (ret)
943 return ret;
944 }
945
946 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
947 f = list_entry(usbvision->outqueue.next,
948 struct usbvision_frame, frame);
949 list_del(usbvision->outqueue.next);
950 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
951
952 f->grabstate = FrameState_Unused;
953
954 vb->memory = V4L2_MEMORY_MMAP;
955 vb->flags = V4L2_BUF_FLAG_MAPPED |
956 V4L2_BUF_FLAG_QUEUED |
957 V4L2_BUF_FLAG_DONE;
958 vb->index = f->index;
959 vb->sequence = f->sequence;
960 vb->timestamp = f->timestamp;
961 vb->field = V4L2_FIELD_NONE;
962 vb->bytesused = f->scanlength;
963
964 return 0;
965}
966
967static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
968{
969 struct video_device *dev = video_devdata(file);
970 struct usb_usbvision *usbvision =
971 (struct usb_usbvision *) video_get_drvdata(dev);
972 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
973
974 usbvision->streaming = Stream_On;
975 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
976
977 return 0;
978}
979
980static int vidioc_streamoff(struct file *file,
981 void *priv, enum v4l2_buf_type type)
982{
983 struct video_device *dev = video_devdata(file);
984 struct usb_usbvision *usbvision =
985 (struct usb_usbvision *) video_get_drvdata(dev);
986 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
987
988 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
989 return -EINVAL;
990
991 if(usbvision->streaming == Stream_On) {
992 usbvision_stream_interrupt(usbvision);
993
994 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
995 }
996 usbvision_empty_framequeues(usbvision);
997
998 return 0;
999}
1000
1001static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1002 struct v4l2_fmtdesc *vfd)
1003{
1004 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
1005 return -EINVAL;
1006 }
1007 vfd->flags = 0;
1008 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
1010 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
1011 memset(vfd->reserved, 0, sizeof(vfd->reserved));
1012 return 0;
1013}
1014
1015static int vidioc_g_fmt_cap (struct file *file, void *priv,
1016 struct v4l2_format *vf)
1017{
1018 struct video_device *dev = video_devdata(file);
1019 struct usb_usbvision *usbvision =
1020 (struct usb_usbvision *) video_get_drvdata(dev);
1021 vf->fmt.pix.width = usbvision->curwidth;
1022 vf->fmt.pix.height = usbvision->curheight;
1023 vf->fmt.pix.pixelformat = usbvision->palette.format;
1024 vf->fmt.pix.bytesperline =
1025 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
1026 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
1027 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1028 vf->fmt.pix.field = V4L2_FIELD_NONE;
1029
1030 return 0;
1031}
1032
1033static int vidioc_try_fmt_cap (struct file *file, void *priv,
1034 struct v4l2_format *vf)
1035{
1036 struct video_device *dev = video_devdata(file);
1037 struct usb_usbvision *usbvision =
1038 (struct usb_usbvision *) video_get_drvdata(dev);
1039 int formatIdx;
1040
1041
1042 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1043 if(vf->fmt.pix.pixelformat ==
1044 usbvision_v4l2_format[formatIdx].format) {
1045 usbvision->palette = usbvision_v4l2_format[formatIdx];
1046 break;
1047 }
1048 }
1049
1050 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1051 return -EINVAL;
1052 }
1053 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1054 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1055
1056 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
1057 usbvision->palette.bytes_per_pixel;
1058 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1059
1060 return 0;
1061}
1062
1063static int vidioc_s_fmt_cap(struct file *file, void *priv,
1064 struct v4l2_format *vf)
1065{
1066 struct video_device *dev = video_devdata(file);
1067 struct usb_usbvision *usbvision =
1068 (struct usb_usbvision *) video_get_drvdata(dev);
1069 int ret;
1070
1071 if( 0 != (ret=vidioc_try_fmt_cap (file, priv, vf)) ) {
1072 return ret;
1073 }
1074
1075
1076 if(usbvision->streaming == Stream_On) {
1077 if ((ret = usbvision_stream_interrupt(usbvision)))
1078 return ret;
1079 }
1080 usbvision_frames_free(usbvision);
1081 usbvision_empty_framequeues(usbvision);
1082
1083 usbvision->curFrame = NULL;
1084
1085
1086 mutex_lock(&usbvision->lock);
1087 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1088 mutex_unlock(&usbvision->lock);
1089
1090 return 0;
1091}
1092
1093static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1094 size_t count, loff_t *ppos)
1095{
1096 struct video_device *dev = video_devdata(file);
1097 struct usb_usbvision *usbvision =
1098 (struct usb_usbvision *) video_get_drvdata(dev);
1099 int noblock = file->f_flags & O_NONBLOCK;
1100 unsigned long lock_flags;
1101
1102 int ret,i;
1103 struct usbvision_frame *frame;
1104
1105 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
1106 (unsigned long)count, noblock);
1107
1108 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1109 return -EFAULT;
1110
1111
1112
1113
1114 if(!usbvision->num_frames) {
1115
1116
1117 usbvision_frames_free(usbvision);
1118 usbvision_empty_framequeues(usbvision);
1119 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1120 }
1121
1122 if(usbvision->streaming != Stream_On) {
1123
1124 usbvision->streaming = Stream_On;
1125 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1126 }
1127
1128
1129
1130 for(i=0;i<usbvision->num_frames;i++) {
1131 frame = &usbvision->frame[i];
1132 if(frame->grabstate == FrameState_Unused) {
1133
1134 frame->grabstate = FrameState_Ready;
1135 frame->scanstate = ScanState_Scanning;
1136
1137 frame->scanlength = 0;
1138
1139
1140 frame->v4l2_format = usbvision->palette;
1141
1142 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1143 list_add_tail(&frame->frame, &usbvision->inqueue);
1144 spin_unlock_irqrestore(&usbvision->queue_lock,
1145 lock_flags);
1146 }
1147 }
1148
1149
1150 if (list_empty(&(usbvision->outqueue))) {
1151 if(noblock)
1152 return -EAGAIN;
1153
1154 ret = wait_event_interruptible
1155 (usbvision->wait_frame,
1156 !list_empty(&(usbvision->outqueue)));
1157 if (ret)
1158 return ret;
1159 }
1160
1161 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1162 frame = list_entry(usbvision->outqueue.next,
1163 struct usbvision_frame, frame);
1164 list_del(usbvision->outqueue.next);
1165 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1166
1167
1168 if (frame->grabstate == FrameState_Error) {
1169 frame->bytes_read = 0;
1170 return 0;
1171 }
1172
1173 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1174 __func__,
1175 frame->index, frame->bytes_read, frame->scanlength);
1176
1177
1178 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1179 count = frame->scanlength - frame->bytes_read;
1180
1181 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1182 return -EFAULT;
1183 }
1184
1185 frame->bytes_read += count;
1186 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1187 __func__,
1188 (unsigned long)count, frame->bytes_read);
1189
1190
1191
1192 frame->bytes_read = 0;
1193
1194
1195 frame->grabstate = FrameState_Unused;
1196
1197
1198 return count;
1199}
1200
1201static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1202{
1203 unsigned long size = vma->vm_end - vma->vm_start,
1204 start = vma->vm_start;
1205 void *pos;
1206 u32 i;
1207
1208 struct video_device *dev = video_devdata(file);
1209 struct usb_usbvision *usbvision =
1210 (struct usb_usbvision *) video_get_drvdata(dev);
1211
1212 PDEBUG(DBG_MMAP, "mmap");
1213
1214 mutex_lock(&usbvision->lock);
1215
1216 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1217 mutex_unlock(&usbvision->lock);
1218 return -EFAULT;
1219 }
1220
1221 if (!(vma->vm_flags & VM_WRITE) ||
1222 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1223 mutex_unlock(&usbvision->lock);
1224 return -EINVAL;
1225 }
1226
1227 for (i = 0; i < usbvision->num_frames; i++) {
1228 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1229 vma->vm_pgoff)
1230 break;
1231 }
1232 if (i == usbvision->num_frames) {
1233 PDEBUG(DBG_MMAP,
1234 "mmap: user supplied mapping address is out of range");
1235 mutex_unlock(&usbvision->lock);
1236 return -EINVAL;
1237 }
1238
1239
1240 vma->vm_flags |= VM_IO;
1241 vma->vm_flags |= VM_RESERVED;
1242
1243 pos = usbvision->frame[i].data;
1244 while (size > 0) {
1245
1246 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1247 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1248 mutex_unlock(&usbvision->lock);
1249 return -EAGAIN;
1250 }
1251 start += PAGE_SIZE;
1252 pos += PAGE_SIZE;
1253 size -= PAGE_SIZE;
1254 }
1255
1256 mutex_unlock(&usbvision->lock);
1257 return 0;
1258}
1259
1260
1261
1262
1263
1264
1265static int usbvision_radio_open(struct inode *inode, struct file *file)
1266{
1267 struct video_device *dev = video_devdata(file);
1268 struct usb_usbvision *usbvision =
1269 (struct usb_usbvision *) video_get_drvdata(dev);
1270 int errCode = 0;
1271
1272 PDEBUG(DBG_IO, "%s:", __func__);
1273
1274 mutex_lock(&usbvision->lock);
1275
1276 if (usbvision->user) {
1277 err("%s: Someone tried to open an already opened USBVision Radio!", __func__);
1278 errCode = -EBUSY;
1279 }
1280 else {
1281 if(PowerOnAtOpen) {
1282 usbvision_reset_powerOffTimer(usbvision);
1283 if (usbvision->power == 0) {
1284 usbvision_power_on(usbvision);
1285 usbvision_i2c_register(usbvision);
1286 }
1287 }
1288
1289
1290 errCode = usbvision_set_alternate(usbvision);
1291 if (errCode < 0) {
1292 usbvision->last_error = errCode;
1293 errCode = -EBUSY;
1294 goto out;
1295 }
1296
1297
1298 usbvision->radio = 1;
1299 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1300 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1301 usbvision->user++;
1302 }
1303
1304 if (errCode) {
1305 if (PowerOnAtOpen) {
1306 usbvision_i2c_unregister(usbvision);
1307 usbvision_power_off(usbvision);
1308 usbvision->initialized = 0;
1309 }
1310 }
1311out:
1312 mutex_unlock(&usbvision->lock);
1313 return errCode;
1314}
1315
1316
1317static int usbvision_radio_close(struct inode *inode, struct file *file)
1318{
1319 struct video_device *dev = video_devdata(file);
1320 struct usb_usbvision *usbvision =
1321 (struct usb_usbvision *) video_get_drvdata(dev);
1322 int errCode = 0;
1323
1324 PDEBUG(DBG_IO, "");
1325
1326 mutex_lock(&usbvision->lock);
1327
1328
1329 usbvision->ifaceAlt=0;
1330 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1331 usbvision->ifaceAlt);
1332
1333 usbvision_audio_off(usbvision);
1334 usbvision->radio=0;
1335 usbvision->user--;
1336
1337 if (PowerOnAtOpen) {
1338 usbvision_set_powerOffTimer(usbvision);
1339 usbvision->initialized = 0;
1340 }
1341
1342 mutex_unlock(&usbvision->lock);
1343
1344 if (usbvision->remove_pending) {
1345 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1346 usbvision_release(usbvision);
1347 }
1348
1349
1350 PDEBUG(DBG_IO, "success");
1351
1352 return errCode;
1353}
1354
1355
1356
1357
1358
1359static int usbvision_vbi_open(struct inode *inode, struct file *file)
1360{
1361
1362 return -ENODEV;
1363
1364}
1365
1366static int usbvision_vbi_close(struct inode *inode, struct file *file)
1367{
1368
1369 return -ENODEV;
1370}
1371
1372static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1373 unsigned int cmd, void *arg)
1374{
1375
1376 return -ENOIOCTLCMD;
1377}
1378
1379static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1380 unsigned int cmd, unsigned long arg)
1381{
1382 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1383}
1384
1385
1386
1387
1388
1389
1390
1391static const struct file_operations usbvision_fops = {
1392 .owner = THIS_MODULE,
1393 .open = usbvision_v4l2_open,
1394 .release = usbvision_v4l2_close,
1395 .read = usbvision_v4l2_read,
1396 .mmap = usbvision_v4l2_mmap,
1397 .ioctl = video_ioctl2,
1398 .llseek = no_llseek,
1399
1400 .compat_ioctl = v4l_compat_ioctl32,
1401};
1402static struct video_device usbvision_video_template = {
1403 .owner = THIS_MODULE,
1404 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1405 .fops = &usbvision_fops,
1406 .name = "usbvision-video",
1407 .release = video_device_release,
1408 .minor = -1,
1409 .vidioc_querycap = vidioc_querycap,
1410 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1411 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1412 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1413 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1414 .vidioc_reqbufs = vidioc_reqbufs,
1415 .vidioc_querybuf = vidioc_querybuf,
1416 .vidioc_qbuf = vidioc_qbuf,
1417 .vidioc_dqbuf = vidioc_dqbuf,
1418 .vidioc_s_std = vidioc_s_std,
1419 .vidioc_enum_input = vidioc_enum_input,
1420 .vidioc_g_input = vidioc_g_input,
1421 .vidioc_s_input = vidioc_s_input,
1422 .vidioc_queryctrl = vidioc_queryctrl,
1423 .vidioc_g_audio = vidioc_g_audio,
1424 .vidioc_s_audio = vidioc_s_audio,
1425 .vidioc_g_ctrl = vidioc_g_ctrl,
1426 .vidioc_s_ctrl = vidioc_s_ctrl,
1427 .vidioc_streamon = vidioc_streamon,
1428 .vidioc_streamoff = vidioc_streamoff,
1429#ifdef CONFIG_VIDEO_V4L1_COMPAT
1430
1431#endif
1432 .vidioc_g_tuner = vidioc_g_tuner,
1433 .vidioc_s_tuner = vidioc_s_tuner,
1434 .vidioc_g_frequency = vidioc_g_frequency,
1435 .vidioc_s_frequency = vidioc_s_frequency,
1436#ifdef CONFIG_VIDEO_ADV_DEBUG
1437 .vidioc_g_register = vidioc_g_register,
1438 .vidioc_s_register = vidioc_s_register,
1439#endif
1440 .tvnorms = USBVISION_NORMS,
1441 .current_norm = V4L2_STD_PAL
1442};
1443
1444
1445
1446static const struct file_operations usbvision_radio_fops = {
1447 .owner = THIS_MODULE,
1448 .open = usbvision_radio_open,
1449 .release = usbvision_radio_close,
1450 .ioctl = video_ioctl2,
1451 .llseek = no_llseek,
1452 .compat_ioctl = v4l_compat_ioctl32,
1453};
1454
1455static struct video_device usbvision_radio_template=
1456{
1457 .owner = THIS_MODULE,
1458 .type = VID_TYPE_TUNER,
1459 .fops = &usbvision_radio_fops,
1460 .name = "usbvision-radio",
1461 .release = video_device_release,
1462 .minor = -1,
1463 .vidioc_querycap = vidioc_querycap,
1464 .vidioc_enum_input = vidioc_enum_input,
1465 .vidioc_g_input = vidioc_g_input,
1466 .vidioc_s_input = vidioc_s_input,
1467 .vidioc_queryctrl = vidioc_queryctrl,
1468 .vidioc_g_audio = vidioc_g_audio,
1469 .vidioc_s_audio = vidioc_s_audio,
1470 .vidioc_g_ctrl = vidioc_g_ctrl,
1471 .vidioc_s_ctrl = vidioc_s_ctrl,
1472 .vidioc_g_tuner = vidioc_g_tuner,
1473 .vidioc_s_tuner = vidioc_s_tuner,
1474 .vidioc_g_frequency = vidioc_g_frequency,
1475 .vidioc_s_frequency = vidioc_s_frequency,
1476
1477 .tvnorms = USBVISION_NORMS,
1478 .current_norm = V4L2_STD_PAL
1479};
1480
1481
1482static const struct file_operations usbvision_vbi_fops = {
1483 .owner = THIS_MODULE,
1484 .open = usbvision_vbi_open,
1485 .release = usbvision_vbi_close,
1486 .ioctl = usbvision_vbi_ioctl,
1487 .llseek = no_llseek,
1488 .compat_ioctl = v4l_compat_ioctl32,
1489};
1490
1491static struct video_device usbvision_vbi_template=
1492{
1493 .owner = THIS_MODULE,
1494 .type = VID_TYPE_TUNER,
1495 .fops = &usbvision_vbi_fops,
1496 .release = video_device_release,
1497 .name = "usbvision-vbi",
1498 .minor = -1,
1499};
1500
1501
1502static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1503 struct video_device *vdev_template,
1504 char *name)
1505{
1506 struct usb_device *usb_dev = usbvision->dev;
1507 struct video_device *vdev;
1508
1509 if (usb_dev == NULL) {
1510 err("%s: usbvision->dev is not set", __func__);
1511 return NULL;
1512 }
1513
1514 vdev = video_device_alloc();
1515 if (NULL == vdev) {
1516 return NULL;
1517 }
1518 *vdev = *vdev_template;
1519
1520 vdev->dev = &usb_dev->dev;
1521 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1522 video_set_drvdata(vdev, usbvision);
1523 return vdev;
1524}
1525
1526
1527static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1528{
1529
1530 if (usbvision->vbi) {
1531 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
1532 usbvision->vbi->minor & 0x1f);
1533 if (usbvision->vbi->minor != -1) {
1534 video_unregister_device(usbvision->vbi);
1535 } else {
1536 video_device_release(usbvision->vbi);
1537 }
1538 usbvision->vbi = NULL;
1539 }
1540
1541
1542 if (usbvision->rdev) {
1543 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
1544 usbvision->rdev->minor & 0x1f);
1545 if (usbvision->rdev->minor != -1) {
1546 video_unregister_device(usbvision->rdev);
1547 } else {
1548 video_device_release(usbvision->rdev);
1549 }
1550 usbvision->rdev = NULL;
1551 }
1552
1553
1554 if (usbvision->vdev) {
1555 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
1556 usbvision->vdev->minor & 0x1f);
1557 if (usbvision->vdev->minor != -1) {
1558 video_unregister_device(usbvision->vdev);
1559 } else {
1560 video_device_release(usbvision->vdev);
1561 }
1562 usbvision->vdev = NULL;
1563 }
1564}
1565
1566
1567static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1568{
1569
1570 usbvision->vdev = usbvision_vdev_init(usbvision,
1571 &usbvision_video_template,
1572 "USBVision Video");
1573 if (usbvision->vdev == NULL) {
1574 goto err_exit;
1575 }
1576 if (video_register_device(usbvision->vdev,
1577 VFL_TYPE_GRABBER,
1578 video_nr)<0) {
1579 goto err_exit;
1580 }
1581 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
1582 usbvision->nr,usbvision->vdev->minor & 0x1f);
1583
1584
1585 if (usbvision_device_data[usbvision->DevModel].Radio) {
1586
1587 usbvision->rdev = usbvision_vdev_init(usbvision,
1588 &usbvision_radio_template,
1589 "USBVision Radio");
1590 if (usbvision->rdev == NULL) {
1591 goto err_exit;
1592 }
1593 if (video_register_device(usbvision->rdev,
1594 VFL_TYPE_RADIO,
1595 radio_nr)<0) {
1596 goto err_exit;
1597 }
1598 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
1599 usbvision->nr, usbvision->rdev->minor & 0x1f);
1600 }
1601
1602 if (usbvision_device_data[usbvision->DevModel].vbi) {
1603 usbvision->vbi = usbvision_vdev_init(usbvision,
1604 &usbvision_vbi_template,
1605 "USBVision VBI");
1606 if (usbvision->vdev == NULL) {
1607 goto err_exit;
1608 }
1609 if (video_register_device(usbvision->vbi,
1610 VFL_TYPE_VBI,
1611 vbi_nr)<0) {
1612 goto err_exit;
1613 }
1614 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
1615 usbvision->nr,usbvision->vbi->minor & 0x1f);
1616 }
1617
1618 return 0;
1619
1620 err_exit:
1621 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1622 usbvision_unregister_video(usbvision);
1623 return -1;
1624}
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1636{
1637 struct usb_usbvision *usbvision;
1638
1639 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
1640 NULL) {
1641 goto err_exit;
1642 }
1643
1644 usbvision->dev = dev;
1645
1646 mutex_init(&usbvision->lock);
1647
1648
1649 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1650 if (usbvision->ctrlUrb == NULL) {
1651 goto err_exit;
1652 }
1653 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1654
1655 usbvision_init_powerOffTimer(usbvision);
1656
1657 return usbvision;
1658
1659err_exit:
1660 if (usbvision && usbvision->ctrlUrb) {
1661 usb_free_urb(usbvision->ctrlUrb);
1662 }
1663 if (usbvision) {
1664 kfree(usbvision);
1665 }
1666 return NULL;
1667}
1668
1669
1670
1671
1672
1673
1674
1675
1676static void usbvision_release(struct usb_usbvision *usbvision)
1677{
1678 PDEBUG(DBG_PROBE, "");
1679
1680 mutex_lock(&usbvision->lock);
1681
1682 usbvision_reset_powerOffTimer(usbvision);
1683
1684 usbvision->initialized = 0;
1685
1686 mutex_unlock(&usbvision->lock);
1687
1688 usbvision_remove_sysfs(usbvision->vdev);
1689 usbvision_unregister_video(usbvision);
1690
1691 if (usbvision->ctrlUrb) {
1692 usb_free_urb(usbvision->ctrlUrb);
1693 }
1694
1695 kfree(usbvision);
1696
1697 PDEBUG(DBG_PROBE, "success");
1698}
1699
1700
1701
1702
1703static void usbvision_configure_video(struct usb_usbvision *usbvision)
1704{
1705 int model;
1706
1707 if (usbvision == NULL)
1708 return;
1709
1710 model = usbvision->DevModel;
1711 usbvision->palette = usbvision_v4l2_format[2];
1712
1713 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
1714 usbvision->Vin_Reg2_Preset =
1715 usbvision_device_data[usbvision->DevModel].Vin_Reg2;
1716 } else {
1717 usbvision->Vin_Reg2_Preset = 0;
1718 }
1719
1720 usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
1721
1722 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1723 usbvision->ctl_input = 0;
1724
1725
1726
1727 usbvision_audio_off(usbvision);
1728 if (!PowerOnAtOpen) {
1729
1730 usbvision_power_on(usbvision);
1731 usbvision_i2c_register(usbvision);
1732 }
1733}
1734
1735
1736
1737
1738
1739
1740
1741
1742static int __devinit usbvision_probe(struct usb_interface *intf,
1743 const struct usb_device_id *devid)
1744{
1745 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1746 struct usb_interface *uif;
1747 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1748 const struct usb_host_interface *interface;
1749 struct usb_usbvision *usbvision = NULL;
1750 const struct usb_endpoint_descriptor *endpoint;
1751 int model,i;
1752
1753 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1754 dev->descriptor.idVendor,
1755 dev->descriptor.idProduct, ifnum);
1756
1757 model = devid->driver_info;
1758 if ( (model<0) || (model>=usbvision_device_data_size) ) {
1759 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
1760 return -ENODEV;
1761 }
1762 printk(KERN_INFO "%s: %s found\n", __func__,
1763 usbvision_device_data[model].ModelString);
1764
1765 if (usbvision_device_data[model].Interface >= 0) {
1766 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1767 } else {
1768 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1769 }
1770 endpoint = &interface->endpoint[1].desc;
1771 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1772 USB_ENDPOINT_XFER_ISOC) {
1773 err("%s: interface %d. has non-ISO endpoint!",
1774 __func__, ifnum);
1775 err("%s: Endpoint attributes %d",
1776 __func__, endpoint->bmAttributes);
1777 return -ENODEV;
1778 }
1779 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1780 USB_DIR_OUT) {
1781 err("%s: interface %d. has ISO OUT endpoint!",
1782 __func__, ifnum);
1783 return -ENODEV;
1784 }
1785
1786 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1787 err("%s: couldn't allocate USBVision struct", __func__);
1788 return -ENOMEM;
1789 }
1790
1791 if (dev->descriptor.bNumConfigurations > 1) {
1792 usbvision->bridgeType = BRIDGE_NT1004;
1793 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
1794 usbvision->bridgeType = BRIDGE_NT1005;
1795 } else {
1796 usbvision->bridgeType = BRIDGE_NT1003;
1797 }
1798 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1799
1800 mutex_lock(&usbvision->lock);
1801
1802
1803 uif = dev->actconfig->interface[0];
1804
1805 usbvision->num_alt=uif->num_altsetting;
1806 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1807 usbvision->alt_max_pkt_size = kmalloc(32*
1808 usbvision->num_alt,GFP_KERNEL);
1809 if (usbvision->alt_max_pkt_size == NULL) {
1810 err("usbvision: out of memory!\n");
1811 mutex_unlock(&usbvision->lock);
1812 return -ENOMEM;
1813 }
1814
1815 for (i = 0; i < usbvision->num_alt ; i++) {
1816 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1817 wMaxPacketSize);
1818 usbvision->alt_max_pkt_size[i] =
1819 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1820 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1821 usbvision->alt_max_pkt_size[i]);
1822 }
1823
1824
1825 usbvision->nr = usbvision_nr++;
1826
1827 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1828 if (usbvision->have_tuner) {
1829 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1830 }
1831
1832 usbvision->tuner_addr = ADDR_UNSET;
1833
1834 usbvision->DevModel = model;
1835 usbvision->remove_pending = 0;
1836 usbvision->iface = ifnum;
1837 usbvision->ifaceAlt = 0;
1838 usbvision->video_endp = endpoint->bEndpointAddress;
1839 usbvision->isocPacketSize = 0;
1840 usbvision->usb_bandwidth = 0;
1841 usbvision->user = 0;
1842 usbvision->streaming = Stream_Off;
1843 usbvision_register_video(usbvision);
1844 usbvision_configure_video(usbvision);
1845 mutex_unlock(&usbvision->lock);
1846
1847
1848 usb_set_intfdata (intf, usbvision);
1849 usbvision_create_sysfs(usbvision->vdev);
1850
1851 PDEBUG(DBG_PROBE, "success");
1852 return 0;
1853}
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864static void __devexit usbvision_disconnect(struct usb_interface *intf)
1865{
1866 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1867
1868 PDEBUG(DBG_PROBE, "");
1869
1870 if (usbvision == NULL) {
1871 err("%s: usb_get_intfdata() failed", __func__);
1872 return;
1873 }
1874 usb_set_intfdata (intf, NULL);
1875
1876 mutex_lock(&usbvision->lock);
1877
1878
1879 usbvision_stop_isoc(usbvision);
1880
1881 if (usbvision->power) {
1882 usbvision_i2c_unregister(usbvision);
1883 usbvision_power_off(usbvision);
1884 }
1885 usbvision->remove_pending = 1;
1886
1887 usb_put_dev(usbvision->dev);
1888 usbvision->dev = NULL;
1889
1890 mutex_unlock(&usbvision->lock);
1891
1892 if (usbvision->user) {
1893 printk(KERN_INFO "%s: In use, disconnect pending\n",
1894 __func__);
1895 wake_up_interruptible(&usbvision->wait_frame);
1896 wake_up_interruptible(&usbvision->wait_stream);
1897 } else {
1898 usbvision_release(usbvision);
1899 }
1900
1901 PDEBUG(DBG_PROBE, "success");
1902
1903}
1904
1905static struct usb_driver usbvision_driver = {
1906 .name = "usbvision",
1907 .id_table = usbvision_table,
1908 .probe = usbvision_probe,
1909 .disconnect = usbvision_disconnect
1910};
1911
1912
1913
1914
1915
1916
1917
1918static int __init usbvision_init(void)
1919{
1920 int errCode;
1921
1922 PDEBUG(DBG_PROBE, "");
1923
1924 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1925 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1926 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1927
1928
1929 if (isocMode != ISOC_MODE_COMPRESS ) {
1930
1931 usbvision_v4l2_format[6].supported = 0;
1932 usbvision_v4l2_format[7].supported = 0;
1933 }
1934
1935 errCode = usb_register(&usbvision_driver);
1936
1937 if (errCode == 0) {
1938 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1939 PDEBUG(DBG_PROBE, "success");
1940 }
1941 return errCode;
1942}
1943
1944static void __exit usbvision_exit(void)
1945{
1946 PDEBUG(DBG_PROBE, "");
1947
1948 usb_deregister(&usbvision_driver);
1949 PDEBUG(DBG_PROBE, "success");
1950}
1951
1952module_init(usbvision_init);
1953module_exit(usbvision_exit);
1954
1955
1956
1957
1958
1959
1960
1961
1962