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->remove_pending) {
331 err_code = -ENODEV;
332 goto unlock;
333 }
334 if (usbvision->user) {
335 err_code = -EBUSY;
336 } else {
337 err_code = v4l2_fh_open(file);
338 if (err_code)
339 goto unlock;
340
341 /* Allocate memory for the scratch ring buffer */
342 err_code = usbvision_scratch_alloc(usbvision);
343 if (isoc_mode == ISOC_MODE_COMPRESS) {
344 /* Allocate intermediate decompression buffers
345 only if needed */
346 err_code = usbvision_decompress_alloc(usbvision);
347 }
348 if (err_code) {
349 /* Deallocate all buffers if trouble */
350 usbvision_scratch_free(usbvision);
351 usbvision_decompress_free(usbvision);
352 }
353 }
354
355 /* If so far no errors then we shall start the camera */
356 if (!err_code) {
357 /* Send init sequence only once, it's large! */
358 if (!usbvision->initialized) {
359 int setup_ok = 0;
360 setup_ok = usbvision_setup(usbvision, isoc_mode);
361 if (setup_ok)
362 usbvision->initialized = 1;
363 else
364 err_code = -EBUSY;
365 }
366
367 if (!err_code) {
368 usbvision_begin_streaming(usbvision);
369 err_code = usbvision_init_isoc(usbvision);
370 /* device must be initialized before isoc transfer */
371 usbvision_muxsel(usbvision, 0);
372
373 /* prepare queues */
374 usbvision_empty_framequeues(usbvision);
375 usbvision->user++;
376 }
377 }
378
379 unlock:
380 mutex_unlock(&usbvision->v4l2_lock);
381
382 PDEBUG(DBG_IO, "success");
383 return err_code;
384 }
385
386 /*
387 * usbvision_v4l2_close()
388 *
389 * This is part of Video 4 Linux API. The procedure
390 * stops streaming and deallocates all buffers that were earlier
391 * allocated in usbvision_v4l2_open().
392 *
393 */
usbvision_v4l2_close(struct file * file)394 static int usbvision_v4l2_close(struct file *file)
395 {
396 struct usb_usbvision *usbvision = video_drvdata(file);
397 int r;
398
399 PDEBUG(DBG_IO, "close");
400
401 mutex_lock(&usbvision->v4l2_lock);
402 usbvision_audio_off(usbvision);
403 usbvision_restart_isoc(usbvision);
404 usbvision_stop_isoc(usbvision);
405
406 usbvision_decompress_free(usbvision);
407 usbvision_frames_free(usbvision);
408 usbvision_empty_framequeues(usbvision);
409 usbvision_scratch_free(usbvision);
410
411 usbvision->user--;
412 r = usbvision->remove_pending;
413 mutex_unlock(&usbvision->v4l2_lock);
414
415 if (r) {
416 printk(KERN_INFO "%s: Final disconnect\n", __func__);
417 usbvision_release(usbvision);
418 return 0;
419 }
420
421 PDEBUG(DBG_IO, "success");
422 return v4l2_fh_release(file);
423 }
424
425
426 /*
427 * usbvision_ioctl()
428 *
429 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
430 *
431 */
432 #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_register(struct file * file,void * priv,struct v4l2_dbg_register * reg)433 static int vidioc_g_register(struct file *file, void *priv,
434 struct v4l2_dbg_register *reg)
435 {
436 struct usb_usbvision *usbvision = video_drvdata(file);
437 int err_code;
438
439 /* NT100x has a 8-bit register space */
440 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
441 if (err_code < 0) {
442 dev_err(&usbvision->vdev.dev,
443 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
444 __func__, err_code);
445 return err_code;
446 }
447 reg->val = err_code;
448 reg->size = 1;
449 return 0;
450 }
451
vidioc_s_register(struct file * file,void * priv,const struct v4l2_dbg_register * reg)452 static int vidioc_s_register(struct file *file, void *priv,
453 const struct v4l2_dbg_register *reg)
454 {
455 struct usb_usbvision *usbvision = video_drvdata(file);
456 int err_code;
457
458 /* NT100x has a 8-bit register space */
459 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
460 if (err_code < 0) {
461 dev_err(&usbvision->vdev.dev,
462 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
463 __func__, err_code);
464 return err_code;
465 }
466 return 0;
467 }
468 #endif
469
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * vc)470 static int vidioc_querycap(struct file *file, void *priv,
471 struct v4l2_capability *vc)
472 {
473 struct usb_usbvision *usbvision = video_drvdata(file);
474 struct video_device *vdev = video_devdata(file);
475
476 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
477 strlcpy(vc->card,
478 usbvision_device_data[usbvision->dev_model].model_string,
479 sizeof(vc->card));
480 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
481 vc->device_caps = usbvision->have_tuner ? V4L2_CAP_TUNER : 0;
482 if (vdev->vfl_type == VFL_TYPE_GRABBER)
483 vc->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
484 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
485 else
486 vc->device_caps |= V4L2_CAP_RADIO;
487
488 vc->capabilities = vc->device_caps | V4L2_CAP_VIDEO_CAPTURE |
489 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
490 if (usbvision_device_data[usbvision->dev_model].radio)
491 vc->capabilities |= V4L2_CAP_RADIO;
492 return 0;
493 }
494
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * vi)495 static int vidioc_enum_input(struct file *file, void *priv,
496 struct v4l2_input *vi)
497 {
498 struct usb_usbvision *usbvision = video_drvdata(file);
499 int chan;
500
501 if (vi->index >= usbvision->video_inputs)
502 return -EINVAL;
503 if (usbvision->have_tuner)
504 chan = vi->index;
505 else
506 chan = vi->index + 1; /* skip Television string*/
507
508 /* Determine the requested input characteristics
509 specific for each usbvision card model */
510 switch (chan) {
511 case 0:
512 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
513 strcpy(vi->name, "White Video Input");
514 } else {
515 strcpy(vi->name, "Television");
516 vi->type = V4L2_INPUT_TYPE_TUNER;
517 vi->tuner = chan;
518 vi->std = USBVISION_NORMS;
519 }
520 break;
521 case 1:
522 vi->type = V4L2_INPUT_TYPE_CAMERA;
523 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
524 strcpy(vi->name, "Green Video Input");
525 else
526 strcpy(vi->name, "Composite Video Input");
527 vi->std = USBVISION_NORMS;
528 break;
529 case 2:
530 vi->type = V4L2_INPUT_TYPE_CAMERA;
531 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
532 strcpy(vi->name, "Yellow Video Input");
533 else
534 strcpy(vi->name, "S-Video Input");
535 vi->std = USBVISION_NORMS;
536 break;
537 case 3:
538 vi->type = V4L2_INPUT_TYPE_CAMERA;
539 strcpy(vi->name, "Red Video Input");
540 vi->std = USBVISION_NORMS;
541 break;
542 }
543 return 0;
544 }
545
vidioc_g_input(struct file * file,void * priv,unsigned int * input)546 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
547 {
548 struct usb_usbvision *usbvision = video_drvdata(file);
549
550 *input = usbvision->ctl_input;
551 return 0;
552 }
553
vidioc_s_input(struct file * file,void * priv,unsigned int input)554 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
555 {
556 struct usb_usbvision *usbvision = video_drvdata(file);
557
558 if (input >= usbvision->video_inputs)
559 return -EINVAL;
560
561 usbvision_muxsel(usbvision, input);
562 usbvision_set_input(usbvision);
563 usbvision_set_output(usbvision,
564 usbvision->curwidth,
565 usbvision->curheight);
566 return 0;
567 }
568
vidioc_s_std(struct file * file,void * priv,v4l2_std_id id)569 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
570 {
571 struct usb_usbvision *usbvision = video_drvdata(file);
572
573 usbvision->tvnorm_id = id;
574
575 call_all(usbvision, video, s_std, usbvision->tvnorm_id);
576 /* propagate the change to the decoder */
577 usbvision_muxsel(usbvision, usbvision->ctl_input);
578
579 return 0;
580 }
581
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * id)582 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
583 {
584 struct usb_usbvision *usbvision = video_drvdata(file);
585
586 *id = usbvision->tvnorm_id;
587 return 0;
588 }
589
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * vt)590 static int vidioc_g_tuner(struct file *file, void *priv,
591 struct v4l2_tuner *vt)
592 {
593 struct usb_usbvision *usbvision = video_drvdata(file);
594
595 if (vt->index) /* Only tuner 0 */
596 return -EINVAL;
597 if (vt->type == V4L2_TUNER_RADIO)
598 strcpy(vt->name, "Radio");
599 else
600 strcpy(vt->name, "Television");
601
602 /* Let clients fill in the remainder of this struct */
603 call_all(usbvision, tuner, g_tuner, vt);
604
605 return 0;
606 }
607
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * vt)608 static int vidioc_s_tuner(struct file *file, void *priv,
609 const struct v4l2_tuner *vt)
610 {
611 struct usb_usbvision *usbvision = video_drvdata(file);
612
613 /* Only one tuner for now */
614 if (vt->index)
615 return -EINVAL;
616 /* let clients handle this */
617 call_all(usbvision, tuner, s_tuner, vt);
618
619 return 0;
620 }
621
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * freq)622 static int vidioc_g_frequency(struct file *file, void *priv,
623 struct v4l2_frequency *freq)
624 {
625 struct usb_usbvision *usbvision = video_drvdata(file);
626
627 /* Only one tuner */
628 if (freq->tuner)
629 return -EINVAL;
630 if (freq->type == V4L2_TUNER_RADIO)
631 freq->frequency = usbvision->radio_freq;
632 else
633 freq->frequency = usbvision->tv_freq;
634
635 return 0;
636 }
637
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * freq)638 static int vidioc_s_frequency(struct file *file, void *priv,
639 const struct v4l2_frequency *freq)
640 {
641 struct usb_usbvision *usbvision = video_drvdata(file);
642 struct v4l2_frequency new_freq = *freq;
643
644 /* Only one tuner for now */
645 if (freq->tuner)
646 return -EINVAL;
647
648 call_all(usbvision, tuner, s_frequency, freq);
649 call_all(usbvision, tuner, g_frequency, &new_freq);
650 if (freq->type == V4L2_TUNER_RADIO)
651 usbvision->radio_freq = new_freq.frequency;
652 else
653 usbvision->tv_freq = new_freq.frequency;
654
655 return 0;
656 }
657
vidioc_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * vr)658 static int vidioc_reqbufs(struct file *file,
659 void *priv, struct v4l2_requestbuffers *vr)
660 {
661 struct usb_usbvision *usbvision = video_drvdata(file);
662 int ret;
663
664 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
665
666 /* Check input validity:
667 the user must do a VIDEO CAPTURE and MMAP method. */
668 if (vr->memory != V4L2_MEMORY_MMAP)
669 return -EINVAL;
670
671 if (usbvision->streaming == stream_on) {
672 ret = usbvision_stream_interrupt(usbvision);
673 if (ret)
674 return ret;
675 }
676
677 usbvision_frames_free(usbvision);
678 usbvision_empty_framequeues(usbvision);
679 vr->count = usbvision_frames_alloc(usbvision, vr->count);
680
681 usbvision->cur_frame = NULL;
682
683 return 0;
684 }
685
vidioc_querybuf(struct file * file,void * priv,struct v4l2_buffer * vb)686 static int vidioc_querybuf(struct file *file,
687 void *priv, struct v4l2_buffer *vb)
688 {
689 struct usb_usbvision *usbvision = video_drvdata(file);
690 struct usbvision_frame *frame;
691
692 /* FIXME : must control
693 that buffers are mapped (VIDIOC_REQBUFS has been called) */
694 if (vb->index >= usbvision->num_frames)
695 return -EINVAL;
696 /* Updating the corresponding frame state */
697 vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
698 frame = &usbvision->frame[vb->index];
699 if (frame->grabstate >= frame_state_ready)
700 vb->flags |= V4L2_BUF_FLAG_QUEUED;
701 if (frame->grabstate >= frame_state_done)
702 vb->flags |= V4L2_BUF_FLAG_DONE;
703 if (frame->grabstate == frame_state_unused)
704 vb->flags |= V4L2_BUF_FLAG_MAPPED;
705 vb->memory = V4L2_MEMORY_MMAP;
706
707 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
708
709 vb->memory = V4L2_MEMORY_MMAP;
710 vb->field = V4L2_FIELD_NONE;
711 vb->length = usbvision->curwidth *
712 usbvision->curheight *
713 usbvision->palette.bytes_per_pixel;
714 vb->timestamp = usbvision->frame[vb->index].timestamp;
715 vb->sequence = usbvision->frame[vb->index].sequence;
716 return 0;
717 }
718
vidioc_qbuf(struct file * file,void * priv,struct v4l2_buffer * vb)719 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
720 {
721 struct usb_usbvision *usbvision = video_drvdata(file);
722 struct usbvision_frame *frame;
723 unsigned long lock_flags;
724
725 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
726 if (vb->index >= usbvision->num_frames)
727 return -EINVAL;
728
729 frame = &usbvision->frame[vb->index];
730
731 if (frame->grabstate != frame_state_unused)
732 return -EAGAIN;
733
734 /* Mark it as ready and enqueue frame */
735 frame->grabstate = frame_state_ready;
736 frame->scanstate = scan_state_scanning;
737 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
738
739 vb->flags &= ~V4L2_BUF_FLAG_DONE;
740
741 /* set v4l2_format index */
742 frame->v4l2_format = usbvision->palette;
743
744 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
745 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
746 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
747
748 return 0;
749 }
750
vidioc_dqbuf(struct file * file,void * priv,struct v4l2_buffer * vb)751 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
752 {
753 struct usb_usbvision *usbvision = video_drvdata(file);
754 int ret;
755 struct usbvision_frame *f;
756 unsigned long lock_flags;
757
758 if (list_empty(&(usbvision->outqueue))) {
759 if (usbvision->streaming == stream_idle)
760 return -EINVAL;
761 ret = wait_event_interruptible
762 (usbvision->wait_frame,
763 !list_empty(&(usbvision->outqueue)));
764 if (ret)
765 return ret;
766 }
767
768 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
769 f = list_entry(usbvision->outqueue.next,
770 struct usbvision_frame, frame);
771 list_del(usbvision->outqueue.next);
772 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
773
774 f->grabstate = frame_state_unused;
775
776 vb->memory = V4L2_MEMORY_MMAP;
777 vb->flags = V4L2_BUF_FLAG_MAPPED |
778 V4L2_BUF_FLAG_QUEUED |
779 V4L2_BUF_FLAG_DONE |
780 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
781 vb->index = f->index;
782 vb->sequence = f->sequence;
783 vb->timestamp = f->timestamp;
784 vb->field = V4L2_FIELD_NONE;
785 vb->bytesused = f->scanlength;
786
787 return 0;
788 }
789
vidioc_streamon(struct file * file,void * priv,enum v4l2_buf_type i)790 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
791 {
792 struct usb_usbvision *usbvision = video_drvdata(file);
793
794 usbvision->streaming = stream_on;
795 call_all(usbvision, video, s_stream, 1);
796
797 return 0;
798 }
799
vidioc_streamoff(struct file * file,void * priv,enum v4l2_buf_type type)800 static int vidioc_streamoff(struct file *file,
801 void *priv, enum v4l2_buf_type type)
802 {
803 struct usb_usbvision *usbvision = video_drvdata(file);
804
805 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
806 return -EINVAL;
807
808 if (usbvision->streaming == stream_on) {
809 usbvision_stream_interrupt(usbvision);
810 /* Stop all video streamings */
811 call_all(usbvision, video, s_stream, 0);
812 }
813 usbvision_empty_framequeues(usbvision);
814
815 return 0;
816 }
817
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * vfd)818 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
819 struct v4l2_fmtdesc *vfd)
820 {
821 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
822 return -EINVAL;
823 strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
824 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
825 return 0;
826 }
827
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * vf)828 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
829 struct v4l2_format *vf)
830 {
831 struct usb_usbvision *usbvision = video_drvdata(file);
832 vf->fmt.pix.width = usbvision->curwidth;
833 vf->fmt.pix.height = usbvision->curheight;
834 vf->fmt.pix.pixelformat = usbvision->palette.format;
835 vf->fmt.pix.bytesperline =
836 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
837 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
838 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
839 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
840
841 return 0;
842 }
843
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * vf)844 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
845 struct v4l2_format *vf)
846 {
847 struct usb_usbvision *usbvision = video_drvdata(file);
848 int format_idx;
849
850 /* Find requested format in available ones */
851 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
852 if (vf->fmt.pix.pixelformat ==
853 usbvision_v4l2_format[format_idx].format) {
854 usbvision->palette = usbvision_v4l2_format[format_idx];
855 break;
856 }
857 }
858 /* robustness */
859 if (format_idx == USBVISION_SUPPORTED_PALETTES)
860 return -EINVAL;
861 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
862 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
863
864 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
865 usbvision->palette.bytes_per_pixel;
866 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
867 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
868 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
869
870 return 0;
871 }
872
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * vf)873 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
874 struct v4l2_format *vf)
875 {
876 struct usb_usbvision *usbvision = video_drvdata(file);
877 int ret;
878
879 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
880 if (ret)
881 return ret;
882
883 /* stop io in case it is already in progress */
884 if (usbvision->streaming == stream_on) {
885 ret = usbvision_stream_interrupt(usbvision);
886 if (ret)
887 return ret;
888 }
889 usbvision_frames_free(usbvision);
890 usbvision_empty_framequeues(usbvision);
891
892 usbvision->cur_frame = NULL;
893
894 /* by now we are committed to the new data... */
895 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
896
897 return 0;
898 }
899
usbvision_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)900 static ssize_t usbvision_read(struct file *file, char __user *buf,
901 size_t count, loff_t *ppos)
902 {
903 struct usb_usbvision *usbvision = video_drvdata(file);
904 int noblock = file->f_flags & O_NONBLOCK;
905 unsigned long lock_flags;
906 int ret, i;
907 struct usbvision_frame *frame;
908
909 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
910 (unsigned long)count, noblock);
911
912 if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
913 return -EFAULT;
914
915 /* This entry point is compatible with the mmap routines
916 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
917 to get frames or call read on the device. */
918 if (!usbvision->num_frames) {
919 /* First, allocate some frames to work with
920 if this has not been done with VIDIOC_REQBUF */
921 usbvision_frames_free(usbvision);
922 usbvision_empty_framequeues(usbvision);
923 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
924 }
925
926 if (usbvision->streaming != stream_on) {
927 /* no stream is running, make it running ! */
928 usbvision->streaming = stream_on;
929 call_all(usbvision, video, s_stream, 1);
930 }
931
932 /* Then, enqueue as many frames as possible
933 (like a user of VIDIOC_QBUF would do) */
934 for (i = 0; i < usbvision->num_frames; i++) {
935 frame = &usbvision->frame[i];
936 if (frame->grabstate == frame_state_unused) {
937 /* Mark it as ready and enqueue frame */
938 frame->grabstate = frame_state_ready;
939 frame->scanstate = scan_state_scanning;
940 /* Accumulated in usbvision_parse_data() */
941 frame->scanlength = 0;
942
943 /* set v4l2_format index */
944 frame->v4l2_format = usbvision->palette;
945
946 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
947 list_add_tail(&frame->frame, &usbvision->inqueue);
948 spin_unlock_irqrestore(&usbvision->queue_lock,
949 lock_flags);
950 }
951 }
952
953 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
954 if (list_empty(&(usbvision->outqueue))) {
955 if (noblock)
956 return -EAGAIN;
957
958 ret = wait_event_interruptible
959 (usbvision->wait_frame,
960 !list_empty(&(usbvision->outqueue)));
961 if (ret)
962 return ret;
963 }
964
965 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
966 frame = list_entry(usbvision->outqueue.next,
967 struct usbvision_frame, frame);
968 list_del(usbvision->outqueue.next);
969 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
970
971 /* An error returns an empty frame */
972 if (frame->grabstate == frame_state_error) {
973 frame->bytes_read = 0;
974 return 0;
975 }
976
977 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
978 __func__,
979 frame->index, frame->bytes_read, frame->scanlength);
980
981 /* copy bytes to user space; we allow for partials reads */
982 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
983 count = frame->scanlength - frame->bytes_read;
984
985 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
986 return -EFAULT;
987
988 frame->bytes_read += count;
989 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
990 __func__,
991 (unsigned long)count, frame->bytes_read);
992
993 #if 1
994 /*
995 * FIXME:
996 * For now, forget the frame if it has not been read in one shot.
997 */
998 frame->bytes_read = 0;
999
1000 /* Mark it as available to be used again. */
1001 frame->grabstate = frame_state_unused;
1002 #else
1003 if (frame->bytes_read >= frame->scanlength) {
1004 /* All data has been read */
1005 frame->bytes_read = 0;
1006
1007 /* Mark it as available to be used again. */
1008 frame->grabstate = frame_state_unused;
1009 }
1010 #endif
1011
1012 return count;
1013 }
1014
usbvision_v4l2_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1015 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1016 size_t count, loff_t *ppos)
1017 {
1018 struct usb_usbvision *usbvision = video_drvdata(file);
1019 int res;
1020
1021 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1022 return -ERESTARTSYS;
1023 res = usbvision_read(file, buf, count, ppos);
1024 mutex_unlock(&usbvision->v4l2_lock);
1025 return res;
1026 }
1027
usbvision_mmap(struct file * file,struct vm_area_struct * vma)1028 static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1029 {
1030 unsigned long size = vma->vm_end - vma->vm_start,
1031 start = vma->vm_start;
1032 void *pos;
1033 u32 i;
1034 struct usb_usbvision *usbvision = video_drvdata(file);
1035
1036 PDEBUG(DBG_MMAP, "mmap");
1037
1038 if (!USBVISION_IS_OPERATIONAL(usbvision))
1039 return -EFAULT;
1040
1041 if (!(vma->vm_flags & VM_WRITE) ||
1042 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1043 return -EINVAL;
1044 }
1045
1046 for (i = 0; i < usbvision->num_frames; i++) {
1047 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1048 vma->vm_pgoff)
1049 break;
1050 }
1051 if (i == usbvision->num_frames) {
1052 PDEBUG(DBG_MMAP,
1053 "mmap: user supplied mapping address is out of range");
1054 return -EINVAL;
1055 }
1056
1057 /* VM_IO is eventually going to replace PageReserved altogether */
1058 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1059
1060 pos = usbvision->frame[i].data;
1061 while (size > 0) {
1062 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1063 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1064 return -EAGAIN;
1065 }
1066 start += PAGE_SIZE;
1067 pos += PAGE_SIZE;
1068 size -= PAGE_SIZE;
1069 }
1070
1071 return 0;
1072 }
1073
usbvision_v4l2_mmap(struct file * file,struct vm_area_struct * vma)1074 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1075 {
1076 struct usb_usbvision *usbvision = video_drvdata(file);
1077 int res;
1078
1079 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1080 return -ERESTARTSYS;
1081 res = usbvision_mmap(file, vma);
1082 mutex_unlock(&usbvision->v4l2_lock);
1083 return res;
1084 }
1085
1086 /*
1087 * Here comes the stuff for radio on usbvision based devices
1088 *
1089 */
usbvision_radio_open(struct file * file)1090 static int usbvision_radio_open(struct file *file)
1091 {
1092 struct usb_usbvision *usbvision = video_drvdata(file);
1093 int err_code = 0;
1094
1095 PDEBUG(DBG_IO, "%s:", __func__);
1096
1097 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1098 return -ERESTARTSYS;
1099
1100 if (usbvision->remove_pending) {
1101 err_code = -ENODEV;
1102 goto out;
1103 }
1104 err_code = v4l2_fh_open(file);
1105 if (err_code)
1106 goto out;
1107 if (usbvision->user) {
1108 dev_err(&usbvision->rdev.dev,
1109 "%s: Someone tried to open an already opened USBVision Radio!\n",
1110 __func__);
1111 err_code = -EBUSY;
1112 } else {
1113 /* Alternate interface 1 is is the biggest frame size */
1114 err_code = usbvision_set_alternate(usbvision);
1115 if (err_code < 0) {
1116 usbvision->last_error = err_code;
1117 err_code = -EBUSY;
1118 goto out;
1119 }
1120
1121 /* If so far no errors then we shall start the radio */
1122 usbvision->radio = 1;
1123 call_all(usbvision, tuner, s_radio);
1124 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1125 usbvision->user++;
1126 }
1127 out:
1128 mutex_unlock(&usbvision->v4l2_lock);
1129 return err_code;
1130 }
1131
1132
usbvision_radio_close(struct file * file)1133 static int usbvision_radio_close(struct file *file)
1134 {
1135 struct usb_usbvision *usbvision = video_drvdata(file);
1136 int r;
1137
1138 PDEBUG(DBG_IO, "");
1139
1140 mutex_lock(&usbvision->v4l2_lock);
1141 /* Set packet size to 0 */
1142 usbvision->iface_alt = 0;
1143 usb_set_interface(usbvision->dev, usbvision->iface,
1144 usbvision->iface_alt);
1145
1146 usbvision_audio_off(usbvision);
1147 usbvision->radio = 0;
1148 usbvision->user--;
1149 r = usbvision->remove_pending;
1150 mutex_unlock(&usbvision->v4l2_lock);
1151
1152 if (r) {
1153 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1154 v4l2_fh_release(file);
1155 usbvision_release(usbvision);
1156 return 0;
1157 }
1158
1159 PDEBUG(DBG_IO, "success");
1160 return v4l2_fh_release(file);
1161 }
1162
1163 /* Video registration stuff */
1164
1165 /* Video template */
1166 static const struct v4l2_file_operations usbvision_fops = {
1167 .owner = THIS_MODULE,
1168 .open = usbvision_v4l2_open,
1169 .release = usbvision_v4l2_close,
1170 .read = usbvision_v4l2_read,
1171 .mmap = usbvision_v4l2_mmap,
1172 .unlocked_ioctl = video_ioctl2,
1173 };
1174
1175 static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1176 .vidioc_querycap = vidioc_querycap,
1177 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1178 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1179 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1180 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1181 .vidioc_reqbufs = vidioc_reqbufs,
1182 .vidioc_querybuf = vidioc_querybuf,
1183 .vidioc_qbuf = vidioc_qbuf,
1184 .vidioc_dqbuf = vidioc_dqbuf,
1185 .vidioc_s_std = vidioc_s_std,
1186 .vidioc_g_std = vidioc_g_std,
1187 .vidioc_enum_input = vidioc_enum_input,
1188 .vidioc_g_input = vidioc_g_input,
1189 .vidioc_s_input = vidioc_s_input,
1190 .vidioc_streamon = vidioc_streamon,
1191 .vidioc_streamoff = vidioc_streamoff,
1192 .vidioc_g_tuner = vidioc_g_tuner,
1193 .vidioc_s_tuner = vidioc_s_tuner,
1194 .vidioc_g_frequency = vidioc_g_frequency,
1195 .vidioc_s_frequency = vidioc_s_frequency,
1196 .vidioc_log_status = v4l2_ctrl_log_status,
1197 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1198 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1199 #ifdef CONFIG_VIDEO_ADV_DEBUG
1200 .vidioc_g_register = vidioc_g_register,
1201 .vidioc_s_register = vidioc_s_register,
1202 #endif
1203 };
1204
1205 static struct video_device usbvision_video_template = {
1206 .fops = &usbvision_fops,
1207 .ioctl_ops = &usbvision_ioctl_ops,
1208 .name = "usbvision-video",
1209 .release = video_device_release_empty,
1210 .tvnorms = USBVISION_NORMS,
1211 };
1212
1213
1214 /* Radio template */
1215 static const struct v4l2_file_operations usbvision_radio_fops = {
1216 .owner = THIS_MODULE,
1217 .open = usbvision_radio_open,
1218 .release = usbvision_radio_close,
1219 .poll = v4l2_ctrl_poll,
1220 .unlocked_ioctl = video_ioctl2,
1221 };
1222
1223 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1224 .vidioc_querycap = vidioc_querycap,
1225 .vidioc_g_tuner = vidioc_g_tuner,
1226 .vidioc_s_tuner = vidioc_s_tuner,
1227 .vidioc_g_frequency = vidioc_g_frequency,
1228 .vidioc_s_frequency = vidioc_s_frequency,
1229 .vidioc_log_status = v4l2_ctrl_log_status,
1230 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1231 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1232 };
1233
1234 static struct video_device usbvision_radio_template = {
1235 .fops = &usbvision_radio_fops,
1236 .name = "usbvision-radio",
1237 .release = video_device_release_empty,
1238 .ioctl_ops = &usbvision_radio_ioctl_ops,
1239 };
1240
1241
usbvision_vdev_init(struct usb_usbvision * usbvision,struct video_device * vdev,const struct video_device * vdev_template,const char * name)1242 static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1243 struct video_device *vdev,
1244 const struct video_device *vdev_template,
1245 const char *name)
1246 {
1247 struct usb_device *usb_dev = usbvision->dev;
1248
1249 if (!usb_dev) {
1250 dev_err(&usbvision->dev->dev,
1251 "%s: usbvision->dev is not set\n", __func__);
1252 return;
1253 }
1254
1255 *vdev = *vdev_template;
1256 vdev->lock = &usbvision->v4l2_lock;
1257 vdev->v4l2_dev = &usbvision->v4l2_dev;
1258 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1259 video_set_drvdata(vdev, usbvision);
1260 }
1261
1262 /* unregister video4linux devices */
usbvision_unregister_video(struct usb_usbvision * usbvision)1263 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1264 {
1265 /* Radio Device: */
1266 if (video_is_registered(&usbvision->rdev)) {
1267 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1268 video_device_node_name(&usbvision->rdev));
1269 video_unregister_device(&usbvision->rdev);
1270 }
1271
1272 /* Video Device: */
1273 if (video_is_registered(&usbvision->vdev)) {
1274 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1275 video_device_node_name(&usbvision->vdev));
1276 video_unregister_device(&usbvision->vdev);
1277 }
1278 }
1279
1280 /* register video4linux devices */
usbvision_register_video(struct usb_usbvision * usbvision)1281 static int usbvision_register_video(struct usb_usbvision *usbvision)
1282 {
1283 int res = -ENOMEM;
1284
1285 /* Video Device: */
1286 usbvision_vdev_init(usbvision, &usbvision->vdev,
1287 &usbvision_video_template, "USBVision Video");
1288 if (!usbvision->have_tuner) {
1289 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1290 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1291 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1292 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1293 }
1294 if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1295 goto err_exit;
1296 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1297 usbvision->nr, video_device_node_name(&usbvision->vdev));
1298
1299 /* Radio Device: */
1300 if (usbvision_device_data[usbvision->dev_model].radio) {
1301 /* usbvision has radio */
1302 usbvision_vdev_init(usbvision, &usbvision->rdev,
1303 &usbvision_radio_template, "USBVision Radio");
1304 if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1305 goto err_exit;
1306 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1307 usbvision->nr, video_device_node_name(&usbvision->rdev));
1308 }
1309 /* all done */
1310 return 0;
1311
1312 err_exit:
1313 dev_err(&usbvision->dev->dev,
1314 "USBVision[%d]: video_register_device() failed\n",
1315 usbvision->nr);
1316 usbvision_unregister_video(usbvision);
1317 return res;
1318 }
1319
1320 /*
1321 * usbvision_alloc()
1322 *
1323 * This code allocates the struct usb_usbvision.
1324 * It is filled with default values.
1325 *
1326 * Returns NULL on error, a pointer to usb_usbvision else.
1327 *
1328 */
usbvision_alloc(struct usb_device * dev,struct usb_interface * intf)1329 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1330 struct usb_interface *intf)
1331 {
1332 struct usb_usbvision *usbvision;
1333
1334 usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
1335 if (!usbvision)
1336 return NULL;
1337
1338 usbvision->dev = dev;
1339 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1340 goto err_free;
1341
1342 if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1343 goto err_unreg;
1344 usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
1345 mutex_init(&usbvision->v4l2_lock);
1346
1347 /* prepare control urb for control messages during interrupts */
1348 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1349 if (!usbvision->ctrl_urb)
1350 goto err_unreg;
1351
1352 return usbvision;
1353
1354 err_unreg:
1355 v4l2_ctrl_handler_free(&usbvision->hdl);
1356 v4l2_device_unregister(&usbvision->v4l2_dev);
1357 err_free:
1358 kfree(usbvision);
1359 return NULL;
1360 }
1361
1362 /*
1363 * usbvision_release()
1364 *
1365 * This code does final release of struct usb_usbvision. This happens
1366 * after the device is disconnected -and- all clients closed their files.
1367 *
1368 */
usbvision_release(struct usb_usbvision * usbvision)1369 static void usbvision_release(struct usb_usbvision *usbvision)
1370 {
1371 PDEBUG(DBG_PROBE, "");
1372
1373 usbvision->initialized = 0;
1374
1375 usbvision_remove_sysfs(&usbvision->vdev);
1376 usbvision_unregister_video(usbvision);
1377 kfree(usbvision->alt_max_pkt_size);
1378
1379 usb_free_urb(usbvision->ctrl_urb);
1380
1381 v4l2_ctrl_handler_free(&usbvision->hdl);
1382 v4l2_device_unregister(&usbvision->v4l2_dev);
1383 kfree(usbvision);
1384
1385 PDEBUG(DBG_PROBE, "success");
1386 }
1387
1388
1389 /*********************** usb interface **********************************/
1390
usbvision_configure_video(struct usb_usbvision * usbvision)1391 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1392 {
1393 int model;
1394
1395 if (!usbvision)
1396 return;
1397
1398 model = usbvision->dev_model;
1399 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1400
1401 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1402 usbvision->vin_reg2_preset =
1403 usbvision_device_data[usbvision->dev_model].vin_reg2;
1404 } else {
1405 usbvision->vin_reg2_preset = 0;
1406 }
1407
1408 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1409 usbvision->video_inputs = usbvision_device_data[model].video_channels;
1410 usbvision->ctl_input = 0;
1411 usbvision->radio_freq = 87.5 * 16000;
1412 usbvision->tv_freq = 400 * 16;
1413
1414 /* This should be here to make i2c clients to be able to register */
1415 /* first switch off audio */
1416 if (usbvision_device_data[model].audio_channels > 0)
1417 usbvision_audio_off(usbvision);
1418 /* and then power up the tuner */
1419 usbvision_power_on(usbvision);
1420 usbvision_i2c_register(usbvision);
1421 }
1422
1423 /*
1424 * usbvision_probe()
1425 *
1426 * This procedure queries device descriptor and accepts the interface
1427 * if it looks like USBVISION video device
1428 *
1429 */
usbvision_probe(struct usb_interface * intf,const struct usb_device_id * devid)1430 static int usbvision_probe(struct usb_interface *intf,
1431 const struct usb_device_id *devid)
1432 {
1433 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1434 struct usb_interface *uif;
1435 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1436 const struct usb_host_interface *interface;
1437 struct usb_usbvision *usbvision = NULL;
1438 const struct usb_endpoint_descriptor *endpoint;
1439 int model, i, ret;
1440
1441 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1442 le16_to_cpu(dev->descriptor.idVendor),
1443 le16_to_cpu(dev->descriptor.idProduct), ifnum);
1444
1445 model = devid->driver_info;
1446 if (model < 0 || model >= usbvision_device_data_size) {
1447 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1448 ret = -ENODEV;
1449 goto err_usb;
1450 }
1451 printk(KERN_INFO "%s: %s found\n", __func__,
1452 usbvision_device_data[model].model_string);
1453
1454 if (usbvision_device_data[model].interface >= 0)
1455 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1456 else if (ifnum < dev->actconfig->desc.bNumInterfaces)
1457 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1458 else {
1459 dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1460 ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1461 ret = -ENODEV;
1462 goto err_usb;
1463 }
1464
1465 if (interface->desc.bNumEndpoints < 2) {
1466 dev_err(&intf->dev, "interface %d has %d endpoints, but must have minimum 2\n",
1467 ifnum, interface->desc.bNumEndpoints);
1468 ret = -ENODEV;
1469 goto err_usb;
1470 }
1471 endpoint = &interface->endpoint[1].desc;
1472
1473 if (!usb_endpoint_xfer_isoc(endpoint)) {
1474 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1475 __func__, ifnum);
1476 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1477 __func__, endpoint->bmAttributes);
1478 ret = -ENODEV;
1479 goto err_usb;
1480 }
1481 if (usb_endpoint_dir_out(endpoint)) {
1482 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1483 __func__, ifnum);
1484 ret = -ENODEV;
1485 goto err_usb;
1486 }
1487
1488 usbvision = usbvision_alloc(dev, intf);
1489 if (!usbvision) {
1490 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1491 ret = -ENOMEM;
1492 goto err_usb;
1493 }
1494
1495 if (dev->descriptor.bNumConfigurations > 1)
1496 usbvision->bridge_type = BRIDGE_NT1004;
1497 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1498 usbvision->bridge_type = BRIDGE_NT1005;
1499 else
1500 usbvision->bridge_type = BRIDGE_NT1003;
1501 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1502
1503 /* compute alternate max packet sizes */
1504 uif = dev->actconfig->interface[0];
1505
1506 usbvision->num_alt = uif->num_altsetting;
1507 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1508 usbvision->alt_max_pkt_size = kmalloc_array(32, usbvision->num_alt,
1509 GFP_KERNEL);
1510 if (!usbvision->alt_max_pkt_size) {
1511 ret = -ENOMEM;
1512 goto err_pkt;
1513 }
1514
1515 for (i = 0; i < usbvision->num_alt; i++) {
1516 u16 tmp;
1517
1518 if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1519 ret = -ENODEV;
1520 goto err_pkt;
1521 }
1522
1523 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1524 wMaxPacketSize);
1525 usbvision->alt_max_pkt_size[i] =
1526 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1527 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1528 usbvision->alt_max_pkt_size[i]);
1529 }
1530
1531
1532 usbvision->nr = usbvision_nr++;
1533
1534 spin_lock_init(&usbvision->queue_lock);
1535 init_waitqueue_head(&usbvision->wait_frame);
1536 init_waitqueue_head(&usbvision->wait_stream);
1537
1538 usbvision->have_tuner = usbvision_device_data[model].tuner;
1539 if (usbvision->have_tuner)
1540 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1541
1542 usbvision->dev_model = model;
1543 usbvision->remove_pending = 0;
1544 usbvision->iface = ifnum;
1545 usbvision->iface_alt = 0;
1546 usbvision->video_endp = endpoint->bEndpointAddress;
1547 usbvision->isoc_packet_size = 0;
1548 usbvision->usb_bandwidth = 0;
1549 usbvision->user = 0;
1550 usbvision->streaming = stream_off;
1551 usbvision_configure_video(usbvision);
1552 usbvision_register_video(usbvision);
1553
1554 usbvision_create_sysfs(&usbvision->vdev);
1555
1556 PDEBUG(DBG_PROBE, "success");
1557 return 0;
1558
1559 err_pkt:
1560 usbvision_release(usbvision);
1561 err_usb:
1562 usb_put_dev(dev);
1563 return ret;
1564 }
1565
1566
1567 /*
1568 * usbvision_disconnect()
1569 *
1570 * This procedure stops all driver activity, deallocates interface-private
1571 * structure (pointed by 'ptr') and after that driver should be removable
1572 * with no ill consequences.
1573 *
1574 */
usbvision_disconnect(struct usb_interface * intf)1575 static void usbvision_disconnect(struct usb_interface *intf)
1576 {
1577 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1578 int u;
1579
1580 PDEBUG(DBG_PROBE, "");
1581
1582 if (!usbvision) {
1583 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1584 return;
1585 }
1586
1587 mutex_lock(&usbvision->v4l2_lock);
1588
1589 /* At this time we ask to cancel outstanding URBs */
1590 usbvision_stop_isoc(usbvision);
1591
1592 v4l2_device_disconnect(&usbvision->v4l2_dev);
1593 usbvision_i2c_unregister(usbvision);
1594 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
1595 u = usbvision->user;
1596
1597 usb_put_dev(usbvision->dev);
1598 usbvision->dev = NULL; /* USB device is no more */
1599
1600 mutex_unlock(&usbvision->v4l2_lock);
1601
1602 if (u) {
1603 printk(KERN_INFO "%s: In use, disconnect pending\n",
1604 __func__);
1605 wake_up_interruptible(&usbvision->wait_frame);
1606 wake_up_interruptible(&usbvision->wait_stream);
1607 } else {
1608 usbvision_release(usbvision);
1609 }
1610
1611 PDEBUG(DBG_PROBE, "success");
1612 }
1613
1614 static struct usb_driver usbvision_driver = {
1615 .name = "usbvision",
1616 .id_table = usbvision_table,
1617 .probe = usbvision_probe,
1618 .disconnect = usbvision_disconnect,
1619 };
1620
1621 /*
1622 * usbvision_init()
1623 *
1624 * This code is run to initialize the driver.
1625 *
1626 */
usbvision_init(void)1627 static int __init usbvision_init(void)
1628 {
1629 int err_code;
1630
1631 PDEBUG(DBG_PROBE, "");
1632
1633 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1634 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1635 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1636
1637 /* disable planar mode support unless compression enabled */
1638 if (isoc_mode != ISOC_MODE_COMPRESS) {
1639 /* FIXME : not the right way to set supported flag */
1640 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1641 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1642 }
1643
1644 err_code = usb_register(&usbvision_driver);
1645
1646 if (err_code == 0) {
1647 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1648 PDEBUG(DBG_PROBE, "success");
1649 }
1650 return err_code;
1651 }
1652
usbvision_exit(void)1653 static void __exit usbvision_exit(void)
1654 {
1655 PDEBUG(DBG_PROBE, "");
1656
1657 usb_deregister(&usbvision_driver);
1658 PDEBUG(DBG_PROBE, "success");
1659 }
1660
1661 module_init(usbvision_init);
1662 module_exit(usbvision_exit);
1663