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