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