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