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