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