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