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