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