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