2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de>
9 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/bitmap.h>
32 #include <linux/usb.h>
33 #include <linux/i2c.h>
34 #include <linux/version.h>
36 #include <linux/mutex.h>
39 #include <media/v4l2-common.h>
40 #include <media/msp3400.h>
42 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
43 "Markus Rechberger <mrechberger@gmail.com>, " \
44 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
45 "Sascha Sommer <saschasommer@freenet.de>"
47 #define DRIVER_NAME "em28xx"
48 #define DRIVER_DESC "Empia em28xx based USB video device driver"
49 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
51 #define em28xx_videodbg(fmt, arg...) do {\
53 printk(KERN_INFO "%s %s :"fmt, \
54 dev->name, __FUNCTION__ , ##arg); } while (0)
56 MODULE_AUTHOR(DRIVER_AUTHOR
);
57 MODULE_DESCRIPTION(DRIVER_DESC
);
58 MODULE_LICENSE("GPL");
60 static LIST_HEAD(em28xx_devlist
);
62 static unsigned int card
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
63 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
64 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
65 static unsigned int radio_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
67 module_param_array(card
, int, NULL
, 0444);
68 module_param_array(video_nr
, int, NULL
, 0444);
69 module_param_array(vbi_nr
, int, NULL
, 0444);
70 module_param_array(radio_nr
, int, NULL
, 0444);
71 MODULE_PARM_DESC(card
, "card type");
72 MODULE_PARM_DESC(video_nr
, "video device numbers");
73 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
74 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
76 static unsigned int video_debug
= 0;
77 module_param(video_debug
,int,0644);
78 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
80 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
81 static unsigned long em28xx_devused
;
83 /* supported controls */
84 /* Common to all boards */
85 static struct v4l2_queryctrl em28xx_qctrl
[] = {
87 .id
= V4L2_CID_AUDIO_VOLUME
,
88 .type
= V4L2_CTRL_TYPE_INTEGER
,
93 .default_value
= 0x1f,
96 .id
= V4L2_CID_AUDIO_MUTE
,
97 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
107 static struct usb_driver em28xx_usb_driver
;
110 /********************* v4l2 interface ******************************************/
114 * inits registers with sane defaults
116 static int em28xx_config(struct em28xx
*dev
)
119 /* Sets I2C speed to 100 KHz */
121 em28xx_write_regs_req(dev
, 0x00, 0x06, "\x40", 1);
123 /* enable vbi capturing */
125 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
126 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
127 em28xx_write_regs_req(dev
,0x00,0x11,"\x51",1);
129 dev
->mute
= 1; /* maybe not the right place... */
132 em28xx_outfmt_set_yuv422(dev
);
133 em28xx_colorlevels_set_default(dev
);
134 em28xx_compression_disable(dev
);
140 * em28xx_config_i2c()
141 * configure i2c attached devices
143 static void em28xx_config_i2c(struct em28xx
*dev
)
145 struct v4l2_routing route
;
147 route
.input
= INPUT(dev
->ctl_input
)->vmux
;
149 em28xx_i2c_call_clients(dev
, VIDIOC_INT_RESET
, NULL
);
150 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
151 em28xx_i2c_call_clients(dev
, VIDIOC_STREAMON
, NULL
);
155 * em28xx_empty_framequeues()
156 * prepare queues for incoming and outgoing frames
158 static void em28xx_empty_framequeues(struct em28xx
*dev
)
162 INIT_LIST_HEAD(&dev
->inqueue
);
163 INIT_LIST_HEAD(&dev
->outqueue
);
165 for (i
= 0; i
< EM28XX_NUM_FRAMES
; i
++) {
166 dev
->frame
[i
].state
= F_UNUSED
;
167 dev
->frame
[i
].buf
.bytesused
= 0;
171 static void video_mux(struct em28xx
*dev
, int index
)
173 struct v4l2_routing route
;
175 route
.input
= INPUT(index
)->vmux
;
177 dev
->ctl_input
= index
;
178 dev
->ctl_ainput
= INPUT(index
)->amux
;
180 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
182 if (dev
->has_msp34xx
) {
184 em28xx_i2c_call_clients(dev
, VIDIOC_INT_I2S_CLOCK_FREQ
, &dev
->i2s_speed
);
185 route
.input
= dev
->ctl_ainput
;
186 route
.output
= MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
);
187 /* Note: this is msp3400 specific */
188 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_AUDIO_ROUTING
, &route
);
191 em28xx_set_audio_source(dev
);
194 /* Usage lock check functions */
195 static int res_get(struct em28xx_fh
*fh
)
197 struct em28xx
*dev
= fh
->dev
;
200 /* This instance already has stream_on */
204 mutex_lock(&dev
->lock
);
213 mutex_unlock(&dev
->lock
);
217 static int res_check(struct em28xx_fh
*fh
)
219 return (fh
->stream_on
);
222 static void res_free(struct em28xx_fh
*fh
)
224 struct em28xx
*dev
= fh
->dev
;
226 mutex_lock(&dev
->lock
);
229 mutex_unlock(&dev
->lock
);
235 static void em28xx_vm_open(struct vm_area_struct
*vma
)
237 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
244 static void em28xx_vm_close(struct vm_area_struct
*vma
)
246 /* NOTE: buffers are not freed here */
247 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
249 if (f
->vma_use_count
)
253 static struct vm_operations_struct em28xx_vm_ops
= {
254 .open
= em28xx_vm_open
,
255 .close
= em28xx_vm_close
,
261 * return the current saturation, brightness or contrast, mute state
263 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
266 case V4L2_CID_AUDIO_MUTE
:
267 ctrl
->value
= dev
->mute
;
269 case V4L2_CID_AUDIO_VOLUME
:
270 ctrl
->value
= dev
->volume
;
279 * mute or set new saturation, brightness or contrast
281 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
284 case V4L2_CID_AUDIO_MUTE
:
285 if (ctrl
->value
!= dev
->mute
) {
286 dev
->mute
= ctrl
->value
;
287 return em28xx_audio_analog_set(dev
);
290 case V4L2_CID_AUDIO_VOLUME
:
291 dev
->volume
= ctrl
->value
;
292 return em28xx_audio_analog_set(dev
);
299 * em28xx_stream_interrupt()
302 static int em28xx_stream_interrupt(struct em28xx
*dev
)
306 /* stop reading from the device */
308 dev
->stream
= STREAM_INTERRUPT
;
309 rc
= wait_event_timeout(dev
->wait_stream
,
310 (dev
->stream
== STREAM_OFF
) ||
311 (dev
->state
& DEV_DISCONNECTED
),
315 dev
->state
|= DEV_MISCONFIGURED
;
316 em28xx_videodbg("device is misconfigured; close and "
317 "open /dev/video%d again\n",
318 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
326 static int check_dev(struct em28xx
*dev
)
328 if (dev
->state
& DEV_DISCONNECTED
) {
329 em28xx_errdev("v4l2 ioctl: device not present\n");
333 if (dev
->state
& DEV_MISCONFIGURED
) {
334 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
335 "close and open it again\n");
341 static void get_scale(struct em28xx
*dev
,
342 unsigned int width
, unsigned int height
,
343 unsigned int *hscale
, unsigned int *vscale
)
345 unsigned int maxw
= norm_maxw(dev
);
346 unsigned int maxh
= norm_maxh(dev
);
348 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
349 if (*hscale
>= 0x4000)
352 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
353 if (*vscale
>= 0x4000)
357 /* ------------------------------------------------------------------
358 IOCTL vidioc handling
359 ------------------------------------------------------------------*/
361 static int vidioc_g_fmt_cap(struct file
*file
, void *priv
,
362 struct v4l2_format
*f
)
364 struct em28xx_fh
*fh
= priv
;
365 struct em28xx
*dev
= fh
->dev
;
367 mutex_lock(&dev
->lock
);
369 f
->fmt
.pix
.width
= dev
->width
;
370 f
->fmt
.pix
.height
= dev
->height
;
371 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
372 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
373 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
374 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
376 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
377 f
->fmt
.pix
.field
= dev
->interlaced
?
378 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
380 mutex_unlock(&dev
->lock
);
384 static int vidioc_try_fmt_cap(struct file
*file
, void *priv
,
385 struct v4l2_format
*f
)
387 struct em28xx_fh
*fh
= priv
;
388 struct em28xx
*dev
= fh
->dev
;
389 int width
= f
->fmt
.pix
.width
;
390 int height
= f
->fmt
.pix
.height
;
391 unsigned int maxw
= norm_maxw(dev
);
392 unsigned int maxh
= norm_maxh(dev
);
393 unsigned int hscale
, vscale
;
395 /* width must even because of the YUYV format
396 height must be even because of interlacing */
409 mutex_lock(&dev
->lock
);
411 if (dev
->is_em2800
) {
412 /* the em2800 can only scale down to 50% */
413 if (height
% (maxh
/ 2))
415 if (width
% (maxw
/ 2))
417 /* according to empiatech support */
418 /* the MaxPacketSize is to small to support */
419 /* framesizes larger than 640x480 @ 30 fps */
420 /* or 640x576 @ 25 fps. As this would cut */
421 /* of a part of the image we prefer */
422 /* 360x576 or 360x480 for now */
423 if (width
== maxw
&& height
== maxh
)
427 get_scale(dev
, width
, height
, &hscale
, &vscale
);
429 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
430 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
432 f
->fmt
.pix
.width
= width
;
433 f
->fmt
.pix
.height
= height
;
434 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
435 f
->fmt
.pix
.bytesperline
= width
* 2;
436 f
->fmt
.pix
.sizeimage
= width
* 2 * height
;
437 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
438 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
440 mutex_unlock(&dev
->lock
);
444 static int vidioc_s_fmt_cap(struct file
*file
, void *priv
,
445 struct v4l2_format
*f
)
447 struct em28xx_fh
*fh
= priv
;
448 struct em28xx
*dev
= fh
->dev
;
455 vidioc_try_fmt_cap(file
, priv
, f
);
457 mutex_lock(&dev
->lock
);
459 for (i
= 0; i
< dev
->num_frames
; i
++)
460 if (dev
->frame
[i
].vma_use_count
) {
461 em28xx_videodbg("VIDIOC_S_FMT failed. "
462 "Unmap the buffers first.\n");
467 /* stop io in case it is already in progress */
468 if (dev
->stream
== STREAM_ON
) {
469 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
470 rc
= em28xx_stream_interrupt(dev
);
475 em28xx_release_buffers(dev
);
478 /* set new image size */
479 dev
->width
= f
->fmt
.pix
.width
;
480 dev
->height
= f
->fmt
.pix
.height
;
481 dev
->frame_size
= dev
->width
* dev
->height
* 2;
482 dev
->field_size
= dev
->frame_size
>> 1;
483 dev
->bytesperline
= dev
->width
* 2;
484 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
486 /* FIXME: This is really weird! Why capture is starting with
489 em28xx_uninit_isoc(dev
);
490 em28xx_set_alternate(dev
);
491 em28xx_capture_start(dev
, 1);
492 em28xx_resolution_set(dev
);
493 em28xx_init_isoc(dev
);
497 mutex_unlock(&dev
->lock
);
501 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
503 struct em28xx_fh
*fh
= priv
;
504 struct em28xx
*dev
= fh
->dev
;
505 struct v4l2_format f
;
512 mutex_lock(&dev
->lock
);
514 mutex_unlock(&dev
->lock
);
516 /* Adjusts width/height, if needed */
517 f
.fmt
.pix
.width
= dev
->width
;
518 f
.fmt
.pix
.height
= dev
->height
;
519 vidioc_try_fmt_cap(file
, priv
, &f
);
521 mutex_lock(&dev
->lock
);
523 /* set new image size */
524 dev
->width
= f
.fmt
.pix
.width
;
525 dev
->height
= f
.fmt
.pix
.height
;
526 dev
->frame_size
= dev
->width
* dev
->height
* 2;
527 dev
->field_size
= dev
->frame_size
>> 1;
528 dev
->bytesperline
= dev
->width
* 2;
529 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
531 em28xx_resolution_set(dev
);
532 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->norm
);
534 mutex_unlock(&dev
->lock
);
538 static const char *iname
[] = {
539 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
540 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
541 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
542 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
543 [EM28XX_VMUX_SVIDEO
] = "S-Video",
544 [EM28XX_VMUX_TELEVISION
] = "Television",
545 [EM28XX_VMUX_CABLE
] = "Cable TV",
546 [EM28XX_VMUX_DVB
] = "DVB",
547 [EM28XX_VMUX_DEBUG
] = "for debug only",
550 static int vidioc_enum_input(struct file
*file
, void *priv
,
551 struct v4l2_input
*i
)
553 struct em28xx_fh
*fh
= priv
;
554 struct em28xx
*dev
= fh
->dev
;
558 if (n
>= MAX_EM28XX_INPUT
)
560 if (0 == INPUT(n
)->type
)
564 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
566 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
568 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
569 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
570 i
->type
= V4L2_INPUT_TYPE_TUNER
;
572 i
->std
= dev
->vdev
->tvnorms
;
577 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
579 struct em28xx_fh
*fh
= priv
;
580 struct em28xx
*dev
= fh
->dev
;
587 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
589 struct em28xx_fh
*fh
= priv
;
590 struct em28xx
*dev
= fh
->dev
;
597 if (i
>= MAX_EM28XX_INPUT
)
599 if (0 == INPUT(i
)->type
)
602 mutex_lock(&dev
->lock
);
606 mutex_unlock(&dev
->lock
);
610 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
612 struct em28xx_fh
*fh
= priv
;
613 struct em28xx
*dev
= fh
->dev
;
614 unsigned int index
= a
->index
;
619 index
= dev
->ctl_ainput
;
622 strcpy(a
->name
, "Television");
624 strcpy(a
->name
, "Line In");
626 a
->capability
= V4L2_AUDCAP_STEREO
;
632 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
634 struct em28xx_fh
*fh
= priv
;
635 struct em28xx
*dev
= fh
->dev
;
637 if (a
->index
!= dev
->ctl_ainput
)
643 static int vidioc_queryctrl(struct file
*file
, void *priv
,
644 struct v4l2_queryctrl
*qc
)
646 struct em28xx_fh
*fh
= priv
;
647 struct em28xx
*dev
= fh
->dev
;
656 memset(qc
, 0, sizeof(*qc
));
660 if (!dev
->has_msp34xx
) {
661 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
662 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
663 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
668 mutex_lock(&dev
->lock
);
669 em28xx_i2c_call_clients(dev
, VIDIOC_QUERYCTRL
, qc
);
670 mutex_unlock(&dev
->lock
);
678 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
679 struct v4l2_control
*ctrl
)
681 struct em28xx_fh
*fh
= priv
;
682 struct em28xx
*dev
= fh
->dev
;
688 mutex_lock(&dev
->lock
);
690 if (!dev
->has_msp34xx
)
691 rc
= em28xx_get_ctrl(dev
, ctrl
);
696 em28xx_i2c_call_clients(dev
, VIDIOC_G_CTRL
, ctrl
);
700 mutex_unlock(&dev
->lock
);
704 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
705 struct v4l2_control
*ctrl
)
707 struct em28xx_fh
*fh
= priv
;
708 struct em28xx
*dev
= fh
->dev
;
716 mutex_lock(&dev
->lock
);
718 if (dev
->has_msp34xx
)
719 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
722 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
723 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
724 if (ctrl
->value
< em28xx_qctrl
[i
].minimum
||
725 ctrl
->value
> em28xx_qctrl
[i
].maximum
) {
730 rc
= em28xx_set_ctrl(dev
, ctrl
);
736 /* Control not found - try to send it to the attached devices */
738 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
742 mutex_unlock(&dev
->lock
);
746 static int vidioc_g_tuner(struct file
*file
, void *priv
,
747 struct v4l2_tuner
*t
)
749 struct em28xx_fh
*fh
= priv
;
750 struct em28xx
*dev
= fh
->dev
;
760 strcpy(t
->name
, "Tuner");
762 mutex_lock(&dev
->lock
);
764 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
766 mutex_unlock(&dev
->lock
);
770 static int vidioc_s_tuner(struct file
*file
, void *priv
,
771 struct v4l2_tuner
*t
)
773 struct em28xx_fh
*fh
= priv
;
774 struct em28xx
*dev
= fh
->dev
;
784 mutex_lock(&dev
->lock
);
786 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
788 mutex_unlock(&dev
->lock
);
792 static int vidioc_g_frequency(struct file
*file
, void *priv
,
793 struct v4l2_frequency
*f
)
795 struct em28xx_fh
*fh
= priv
;
796 struct em28xx
*dev
= fh
->dev
;
798 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
799 f
->frequency
= dev
->ctl_freq
;
804 static int vidioc_s_frequency(struct file
*file
, void *priv
,
805 struct v4l2_frequency
*f
)
807 struct em28xx_fh
*fh
= priv
;
808 struct em28xx
*dev
= fh
->dev
;
818 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
820 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
823 mutex_lock(&dev
->lock
);
825 dev
->ctl_freq
= f
->frequency
;
826 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
828 mutex_unlock(&dev
->lock
);
832 static int vidioc_cropcap(struct file
*file
, void *priv
,
833 struct v4l2_cropcap
*cc
)
835 struct em28xx_fh
*fh
= priv
;
836 struct em28xx
*dev
= fh
->dev
;
838 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
843 cc
->bounds
.width
= dev
->width
;
844 cc
->bounds
.height
= dev
->height
;
845 cc
->defrect
= cc
->bounds
;
846 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
847 cc
->pixelaspect
.denominator
= 59;
852 static int vidioc_streamon(struct file
*file
, void *priv
,
853 enum v4l2_buf_type type
)
855 struct em28xx_fh
*fh
= priv
;
856 struct em28xx
*dev
= fh
->dev
;
863 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
866 if (list_empty(&dev
->inqueue
))
869 mutex_lock(&dev
->lock
);
871 if (unlikely(res_get(fh
) < 0)) {
872 mutex_unlock(&dev
->lock
);
876 dev
->stream
= STREAM_ON
; /* FIXME: Start video capture here? */
878 mutex_unlock(&dev
->lock
);
882 static int vidioc_streamoff(struct file
*file
, void *priv
,
883 enum v4l2_buf_type type
)
885 struct em28xx_fh
*fh
= priv
;
886 struct em28xx
*dev
= fh
->dev
;
893 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
896 mutex_lock(&dev
->lock
);
898 if (dev
->stream
== STREAM_ON
) {
899 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
900 rc
= em28xx_stream_interrupt(dev
);
902 mutex_unlock(&dev
->lock
);
907 em28xx_empty_framequeues(dev
);
909 mutex_unlock(&dev
->lock
);
913 static int vidioc_querycap(struct file
*file
, void *priv
,
914 struct v4l2_capability
*cap
)
916 struct em28xx_fh
*fh
= priv
;
917 struct em28xx
*dev
= fh
->dev
;
919 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
920 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
921 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
, sizeof(cap
->bus_info
));
923 cap
->version
= EM28XX_VERSION_CODE
;
926 V4L2_CAP_SLICED_VBI_CAPTURE
|
927 V4L2_CAP_VIDEO_CAPTURE
|
929 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
932 cap
->capabilities
|= V4L2_CAP_TUNER
;
937 static int vidioc_enum_fmt_cap(struct file
*file
, void *priv
,
938 struct v4l2_fmtdesc
*fmtd
)
940 if (fmtd
->index
!= 0)
943 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
944 strcpy(fmtd
->description
, "Packed YUY2");
945 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
946 memset(fmtd
->reserved
, 0, sizeof(fmtd
->reserved
));
951 /* Sliced VBI ioctls */
952 static int vidioc_g_fmt_vbi_capture(struct file
*file
, void *priv
,
953 struct v4l2_format
*f
)
955 struct em28xx_fh
*fh
= priv
;
956 struct em28xx
*dev
= fh
->dev
;
963 mutex_lock(&dev
->lock
);
965 f
->fmt
.sliced
.service_set
= 0;
967 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
969 if (f
->fmt
.sliced
.service_set
== 0)
972 mutex_unlock(&dev
->lock
);
976 static int vidioc_try_set_vbi_capture(struct file
*file
, void *priv
,
977 struct v4l2_format
*f
)
979 struct em28xx_fh
*fh
= priv
;
980 struct em28xx
*dev
= fh
->dev
;
987 mutex_lock(&dev
->lock
);
988 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
989 mutex_unlock(&dev
->lock
);
991 if (f
->fmt
.sliced
.service_set
== 0)
998 static int vidioc_reqbufs(struct file
*file
, void *priv
,
999 struct v4l2_requestbuffers
*rb
)
1001 struct em28xx_fh
*fh
= priv
;
1002 struct em28xx
*dev
= fh
->dev
;
1006 rc
= check_dev(dev
);
1010 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1011 rb
->memory
!= V4L2_MEMORY_MMAP
)
1014 if (dev
->io
== IO_READ
) {
1015 em28xx_videodbg("method is set to read;"
1016 " close and open the device again to"
1017 " choose the mmap I/O method\n");
1021 for (i
= 0; i
< dev
->num_frames
; i
++)
1022 if (dev
->frame
[i
].vma_use_count
) {
1023 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1024 "previous buffers are still mapped\n");
1028 mutex_lock(&dev
->lock
);
1030 if (dev
->stream
== STREAM_ON
) {
1031 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1032 rc
= em28xx_stream_interrupt(dev
);
1034 mutex_unlock(&dev
->lock
);
1039 em28xx_empty_framequeues(dev
);
1041 em28xx_release_buffers(dev
);
1043 rb
->count
= em28xx_request_buffers(dev
, rb
->count
);
1045 dev
->frame_current
= NULL
;
1046 dev
->io
= rb
->count
? IO_MMAP
: IO_NONE
;
1048 mutex_unlock(&dev
->lock
);
1052 static int vidioc_querybuf(struct file
*file
, void *priv
,
1053 struct v4l2_buffer
*b
)
1055 struct em28xx_fh
*fh
= priv
;
1056 struct em28xx
*dev
= fh
->dev
;
1059 rc
= check_dev(dev
);
1063 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1064 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
)
1067 mutex_lock(&dev
->lock
);
1069 memcpy(b
, &dev
->frame
[b
->index
].buf
, sizeof(*b
));
1071 if (dev
->frame
[b
->index
].vma_use_count
)
1072 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1074 if (dev
->frame
[b
->index
].state
== F_DONE
)
1075 b
->flags
|= V4L2_BUF_FLAG_DONE
;
1076 else if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1077 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1079 mutex_unlock(&dev
->lock
);
1083 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1085 struct em28xx_fh
*fh
= priv
;
1086 struct em28xx
*dev
= fh
->dev
;
1087 unsigned long lock_flags
;
1090 rc
= check_dev(dev
);
1094 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
||
1095 b
->index
>= dev
->num_frames
)
1098 if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1101 dev
->frame
[b
->index
].state
= F_QUEUED
;
1103 /* add frame to fifo */
1104 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1105 list_add_tail(&dev
->frame
[b
->index
].frame
, &dev
->inqueue
);
1106 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1111 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1113 struct em28xx_fh
*fh
= priv
;
1114 struct em28xx
*dev
= fh
->dev
;
1116 struct em28xx_frame_t
*f
;
1117 unsigned long lock_flags
;
1119 rc
= check_dev(dev
);
1123 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
1126 if (list_empty(&dev
->outqueue
)) {
1127 if (dev
->stream
== STREAM_OFF
)
1130 if (file
->f_flags
& O_NONBLOCK
)
1133 rc
= wait_event_interruptible(dev
->wait_frame
,
1134 (!list_empty(&dev
->outqueue
)) ||
1135 (dev
->state
& DEV_DISCONNECTED
));
1139 if (dev
->state
& DEV_DISCONNECTED
)
1143 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1144 f
= list_entry(dev
->outqueue
.next
, struct em28xx_frame_t
, frame
);
1145 list_del(dev
->outqueue
.next
);
1146 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1148 f
->state
= F_UNUSED
;
1149 memcpy(b
, &f
->buf
, sizeof(*b
));
1151 if (f
->vma_use_count
)
1152 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1157 /* ----------------------------------------------------------- */
1158 /* RADIO ESPECIFIC IOCTLS */
1159 /* ----------------------------------------------------------- */
1161 static int radio_querycap(struct file
*file
, void *priv
,
1162 struct v4l2_capability
*cap
)
1164 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1166 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1167 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1168 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
, sizeof(cap
->bus_info
));
1170 cap
->version
= EM28XX_VERSION_CODE
;
1171 cap
->capabilities
= V4L2_CAP_TUNER
;
1175 static int radio_g_tuner(struct file
*file
, void *priv
,
1176 struct v4l2_tuner
*t
)
1178 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1180 if (unlikely(t
->index
> 0))
1183 strcpy(t
->name
, "Radio");
1184 t
->type
= V4L2_TUNER_RADIO
;
1186 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
1190 static int radio_enum_input(struct file
*file
, void *priv
,
1191 struct v4l2_input
*i
)
1195 strcpy(i
->name
, "Radio");
1196 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1201 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1203 if (unlikely(a
->index
))
1206 strcpy(a
->name
, "Radio");
1210 static int radio_s_tuner(struct file
*file
, void *priv
,
1211 struct v4l2_tuner
*t
)
1213 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1218 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
1223 static int radio_s_audio(struct file
*file
, void *fh
,
1224 struct v4l2_audio
*a
)
1229 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
1234 static int radio_queryctrl(struct file
*file
, void *priv
,
1235 struct v4l2_queryctrl
*qc
)
1239 if (qc
->id
< V4L2_CID_BASE
||
1240 qc
->id
>= V4L2_CID_LASTP1
)
1243 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1244 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1245 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
1254 * em28xx_v4l2_open()
1255 * inits the device and starts isoc transfer
1257 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
1259 int minor
= iminor(inode
);
1260 int errCode
= 0, radio
= 0;
1261 struct em28xx
*h
,*dev
= NULL
;
1262 struct em28xx_fh
*fh
;
1264 list_for_each_entry(h
, &em28xx_devlist
, devlist
) {
1265 if (h
->vdev
->minor
== minor
) {
1267 dev
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1269 if (h
->vbi_dev
->minor
== minor
) {
1271 dev
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1274 h
->radio_dev
->minor
== minor
) {
1282 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1283 minor
,v4l2_type_names
[dev
->type
],dev
->users
);
1285 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1288 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1291 mutex_lock(&dev
->lock
);
1294 filp
->private_data
= fh
;
1296 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1297 em28xx_set_alternate(dev
);
1299 dev
->width
= norm_maxw(dev
);
1300 dev
->height
= norm_maxh(dev
);
1301 dev
->frame_size
= dev
->width
* dev
->height
* 2;
1302 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1303 dev
->bytesperline
= dev
->width
* 2;
1307 em28xx_capture_start(dev
, 1);
1308 em28xx_resolution_set(dev
);
1311 /* start the transfer */
1312 errCode
= em28xx_init_isoc(dev
);
1316 em28xx_empty_framequeues(dev
);
1319 em28xx_videodbg("video_open: setting radio device\n");
1320 em28xx_i2c_call_clients(dev
, AUDC_SET_RADIO
, NULL
);
1326 mutex_unlock(&dev
->lock
);
1331 * em28xx_realease_resources()
1332 * unregisters the v4l2,i2c and usb devices
1333 * called when the device gets disconected or at module unload
1335 static void em28xx_release_resources(struct em28xx
*dev
)
1338 /*FIXME: I2C IR should be disconnected */
1340 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1341 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1342 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1343 list_del(&dev
->devlist
);
1344 if (dev
->radio_dev
) {
1345 if (-1 != dev
->radio_dev
->minor
)
1346 video_unregister_device(dev
->radio_dev
);
1348 video_device_release(dev
->radio_dev
);
1349 dev
->radio_dev
= NULL
;
1352 if (-1 != dev
->vbi_dev
->minor
)
1353 video_unregister_device(dev
->vbi_dev
);
1355 video_device_release(dev
->vbi_dev
);
1356 dev
->vbi_dev
= NULL
;
1359 if (-1 != dev
->vdev
->minor
)
1360 video_unregister_device(dev
->vdev
);
1362 video_device_release(dev
->vdev
);
1365 em28xx_i2c_unregister(dev
);
1366 usb_put_dev(dev
->udev
);
1368 /* Mark device as unused */
1369 em28xx_devused
&=~(1<<dev
->devno
);
1373 * em28xx_v4l2_close()
1374 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1376 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
1378 struct em28xx_fh
*fh
= filp
->private_data
;
1379 struct em28xx
*dev
= fh
->dev
;
1382 em28xx_videodbg("users=%d\n", dev
->users
);
1388 mutex_lock(&dev
->lock
);
1390 if (dev
->users
== 1) {
1391 em28xx_uninit_isoc(dev
);
1392 em28xx_release_buffers(dev
);
1395 /* the device is already disconnect,
1396 free the remaining resources */
1397 if (dev
->state
& DEV_DISCONNECTED
) {
1398 em28xx_release_resources(dev
);
1399 mutex_unlock(&dev
->lock
);
1404 /* set alternate 0 */
1406 em28xx_videodbg("setting alternate 0\n");
1407 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1409 em28xx_errdev("cannot change alternate number to "
1410 "0 (error=%i)\n", errCode
);
1415 wake_up_interruptible_nr(&dev
->open
, 1);
1416 mutex_unlock(&dev
->lock
);
1421 * em28xx_v4l2_read()
1422 * will allocate buffers when called for the first time
1425 em28xx_v4l2_read(struct file
*filp
, char __user
* buf
, size_t count
,
1428 struct em28xx_frame_t
*f
, *i
;
1429 unsigned long lock_flags
;
1431 struct em28xx_fh
*fh
= filp
->private_data
;
1432 struct em28xx
*dev
= fh
->dev
;
1434 /* FIXME: read() is not prepared to allow changing the video
1435 resolution while streaming. Seems a bug at em28xx_set_fmt
1438 if (unlikely(res_get(fh
) < 0))
1441 mutex_lock(&dev
->lock
);
1443 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1444 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1446 if (dev
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1447 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1448 em28xx_videodbg("not supported yet! ...\n");
1449 if (copy_to_user(buf
, "", 1)) {
1450 mutex_unlock(&dev
->lock
);
1453 mutex_unlock(&dev
->lock
);
1456 if (dev
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
) {
1457 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1458 em28xx_videodbg("not supported yet! ...\n");
1459 if (copy_to_user(buf
, "", 1)) {
1460 mutex_unlock(&dev
->lock
);
1463 mutex_unlock(&dev
->lock
);
1467 if (dev
->state
& DEV_DISCONNECTED
) {
1468 em28xx_videodbg("device not present\n");
1469 mutex_unlock(&dev
->lock
);
1473 if (dev
->state
& DEV_MISCONFIGURED
) {
1474 em28xx_videodbg("device misconfigured; close and open it again\n");
1475 mutex_unlock(&dev
->lock
);
1479 if (dev
->io
== IO_MMAP
) {
1480 em28xx_videodbg ("IO method is set to mmap; close and open"
1481 " the device again to choose the read method\n");
1482 mutex_unlock(&dev
->lock
);
1486 if (dev
->io
== IO_NONE
) {
1487 if (!em28xx_request_buffers(dev
, EM28XX_NUM_READ_FRAMES
)) {
1488 em28xx_errdev("read failed, not enough memory\n");
1489 mutex_unlock(&dev
->lock
);
1493 dev
->stream
= STREAM_ON
;
1494 em28xx_queue_unusedframes(dev
);
1498 mutex_unlock(&dev
->lock
);
1502 if (list_empty(&dev
->outqueue
)) {
1503 if (filp
->f_flags
& O_NONBLOCK
) {
1504 mutex_unlock(&dev
->lock
);
1507 ret
= wait_event_interruptible
1509 (!list_empty(&dev
->outqueue
)) ||
1510 (dev
->state
& DEV_DISCONNECTED
));
1512 mutex_unlock(&dev
->lock
);
1515 if (dev
->state
& DEV_DISCONNECTED
) {
1516 mutex_unlock(&dev
->lock
);
1519 dev
->video_bytesread
= 0;
1522 f
= list_entry(dev
->outqueue
.prev
, struct em28xx_frame_t
, frame
);
1524 em28xx_queue_unusedframes(dev
);
1526 if (count
> f
->buf
.length
)
1527 count
= f
->buf
.length
;
1529 if ((dev
->video_bytesread
+ count
) > dev
->frame_size
)
1530 count
= dev
->frame_size
- dev
->video_bytesread
;
1532 if (copy_to_user(buf
, f
->bufmem
+dev
->video_bytesread
, count
)) {
1533 em28xx_err("Error while copying to user\n");
1536 dev
->video_bytesread
+= count
;
1538 if (dev
->video_bytesread
== dev
->frame_size
) {
1539 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1540 list_for_each_entry(i
, &dev
->outqueue
, frame
)
1541 i
->state
= F_UNUSED
;
1542 INIT_LIST_HEAD(&dev
->outqueue
);
1543 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1545 em28xx_queue_unusedframes(dev
);
1546 dev
->video_bytesread
= 0;
1551 mutex_unlock(&dev
->lock
);
1557 * em28xx_v4l2_poll()
1558 * will allocate buffers when called for the first time
1560 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
1562 unsigned int mask
= 0;
1563 struct em28xx_fh
*fh
= filp
->private_data
;
1564 struct em28xx
*dev
= fh
->dev
;
1566 if (unlikely(res_get(fh
) < 0))
1569 mutex_lock(&dev
->lock
);
1571 if (dev
->state
& DEV_DISCONNECTED
) {
1572 em28xx_videodbg("device not present\n");
1573 } else if (dev
->state
& DEV_MISCONFIGURED
) {
1574 em28xx_videodbg("device is misconfigured; close and open it again\n");
1576 if (dev
->io
== IO_NONE
) {
1577 if (!em28xx_request_buffers
1578 (dev
, EM28XX_NUM_READ_FRAMES
)) {
1580 ("poll() failed, not enough memory\n");
1583 dev
->stream
= STREAM_ON
;
1587 if (dev
->io
== IO_READ
) {
1588 em28xx_queue_unusedframes(dev
);
1589 poll_wait(filp
, &dev
->wait_frame
, wait
);
1591 if (!list_empty(&dev
->outqueue
))
1592 mask
|= POLLIN
| POLLRDNORM
;
1594 mutex_unlock(&dev
->lock
);
1600 mutex_unlock(&dev
->lock
);
1605 * em28xx_v4l2_mmap()
1607 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1609 struct em28xx_fh
*fh
= filp
->private_data
;
1610 struct em28xx
*dev
= fh
->dev
;
1611 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1612 unsigned long start
= vma
->vm_start
;
1616 if (unlikely(res_get(fh
) < 0))
1619 mutex_lock(&dev
->lock
);
1621 if (dev
->state
& DEV_DISCONNECTED
) {
1622 em28xx_videodbg("mmap: device not present\n");
1623 mutex_unlock(&dev
->lock
);
1627 if (dev
->state
& DEV_MISCONFIGURED
) {
1628 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1630 mutex_unlock(&dev
->lock
);
1634 if (dev
->io
!= IO_MMAP
|| !(vma
->vm_flags
& VM_WRITE
)) {
1635 mutex_unlock(&dev
->lock
);
1639 if (size
> PAGE_ALIGN(dev
->frame
[0].buf
.length
))
1640 size
= PAGE_ALIGN(dev
->frame
[0].buf
.length
);
1642 for (i
= 0; i
< dev
->num_frames
; i
++) {
1643 if ((dev
->frame
[i
].buf
.m
.offset
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
1646 if (i
== dev
->num_frames
) {
1647 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1648 mutex_unlock(&dev
->lock
);
1652 /* VM_IO is eventually going to replace PageReserved altogether */
1653 vma
->vm_flags
|= VM_IO
;
1654 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1656 pos
= dev
->frame
[i
].bufmem
;
1657 while (size
> 0) { /* size is page-aligned */
1658 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1659 em28xx_videodbg("mmap: vm_insert_page failed\n");
1660 mutex_unlock(&dev
->lock
);
1668 vma
->vm_ops
= &em28xx_vm_ops
;
1669 vma
->vm_private_data
= &dev
->frame
[i
];
1671 em28xx_vm_open(vma
);
1672 mutex_unlock(&dev
->lock
);
1676 static const struct file_operations em28xx_v4l_fops
= {
1677 .owner
= THIS_MODULE
,
1678 .open
= em28xx_v4l2_open
,
1679 .release
= em28xx_v4l2_close
,
1680 .read
= em28xx_v4l2_read
,
1681 .poll
= em28xx_v4l2_poll
,
1682 .mmap
= em28xx_v4l2_mmap
,
1683 .ioctl
= video_ioctl2
,
1684 .llseek
= no_llseek
,
1685 .compat_ioctl
= v4l_compat_ioctl32
,
1688 static const struct file_operations radio_fops
= {
1689 .owner
= THIS_MODULE
,
1690 .open
= em28xx_v4l2_open
,
1691 .release
= em28xx_v4l2_close
,
1692 .ioctl
= video_ioctl2
,
1693 .compat_ioctl
= v4l_compat_ioctl32
,
1694 .llseek
= no_llseek
,
1697 static const struct video_device em28xx_video_template
= {
1698 .fops
= &em28xx_v4l_fops
,
1699 .release
= video_device_release
,
1702 .vidioc_querycap
= vidioc_querycap
,
1703 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
1704 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
1705 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
1706 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
1707 .vidioc_g_audio
= vidioc_g_audio
,
1708 .vidioc_s_audio
= vidioc_s_audio
,
1709 .vidioc_cropcap
= vidioc_cropcap
,
1711 .vidioc_g_fmt_vbi_capture
= vidioc_g_fmt_vbi_capture
,
1712 .vidioc_try_fmt_vbi_capture
= vidioc_try_set_vbi_capture
,
1713 .vidioc_s_fmt_vbi_capture
= vidioc_try_set_vbi_capture
,
1715 .vidioc_reqbufs
= vidioc_reqbufs
,
1716 .vidioc_querybuf
= vidioc_querybuf
,
1717 .vidioc_qbuf
= vidioc_qbuf
,
1718 .vidioc_dqbuf
= vidioc_dqbuf
,
1719 .vidioc_s_std
= vidioc_s_std
,
1720 .vidioc_enum_input
= vidioc_enum_input
,
1721 .vidioc_g_input
= vidioc_g_input
,
1722 .vidioc_s_input
= vidioc_s_input
,
1723 .vidioc_queryctrl
= vidioc_queryctrl
,
1724 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1725 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1726 .vidioc_streamon
= vidioc_streamon
,
1727 .vidioc_streamoff
= vidioc_streamoff
,
1728 .vidioc_g_tuner
= vidioc_g_tuner
,
1729 .vidioc_s_tuner
= vidioc_s_tuner
,
1730 .vidioc_g_frequency
= vidioc_g_frequency
,
1731 .vidioc_s_frequency
= vidioc_s_frequency
,
1733 .tvnorms
= V4L2_STD_ALL
,
1734 .current_norm
= V4L2_STD_PAL
,
1737 static struct video_device em28xx_radio_template
= {
1738 .name
= "em28xx-radio",
1739 .type
= VID_TYPE_TUNER
,
1740 .fops
= &radio_fops
,
1742 .vidioc_querycap
= radio_querycap
,
1743 .vidioc_g_tuner
= radio_g_tuner
,
1744 .vidioc_enum_input
= radio_enum_input
,
1745 .vidioc_g_audio
= radio_g_audio
,
1746 .vidioc_s_tuner
= radio_s_tuner
,
1747 .vidioc_s_audio
= radio_s_audio
,
1748 .vidioc_s_input
= radio_s_input
,
1749 .vidioc_queryctrl
= radio_queryctrl
,
1750 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1751 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1752 .vidioc_g_frequency
= vidioc_g_frequency
,
1753 .vidioc_s_frequency
= vidioc_s_frequency
,
1756 /******************************** usb interface *****************************************/
1759 static LIST_HEAD(em28xx_extension_devlist
);
1760 static DEFINE_MUTEX(em28xx_extension_devlist_lock
);
1762 int em28xx_register_extension(struct em28xx_ops
*ops
)
1764 struct em28xx
*h
, *dev
= NULL
;
1766 list_for_each_entry(h
, &em28xx_devlist
, devlist
)
1769 mutex_lock(&em28xx_extension_devlist_lock
);
1770 list_add_tail(&ops
->next
, &em28xx_extension_devlist
);
1774 printk(KERN_INFO
"Em28xx: Initialized (%s) extension\n", ops
->name
);
1775 mutex_unlock(&em28xx_extension_devlist_lock
);
1779 EXPORT_SYMBOL(em28xx_register_extension
);
1781 void em28xx_unregister_extension(struct em28xx_ops
*ops
)
1783 struct em28xx
*h
, *dev
= NULL
;
1785 list_for_each_entry(h
, &em28xx_devlist
, devlist
)
1791 mutex_lock(&em28xx_extension_devlist_lock
);
1792 printk(KERN_INFO
"Em28xx: Removed (%s) extension\n", ops
->name
);
1793 list_del(&ops
->next
);
1794 mutex_unlock(&em28xx_extension_devlist_lock
);
1796 EXPORT_SYMBOL(em28xx_unregister_extension
);
1798 struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
1799 const struct video_device
*template,
1801 const char *type_name
)
1803 struct video_device
*vfd
;
1805 vfd
= video_device_alloc();
1810 vfd
->dev
= &dev
->udev
->dev
;
1811 vfd
->release
= video_device_release
;
1814 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
1815 dev
->name
, type_name
);
1823 * allocates and inits the device structs, registers i2c bus and v4l device
1825 static int em28xx_init_dev(struct em28xx
**devhandle
, struct usb_device
*udev
,
1828 struct em28xx_ops
*ops
= NULL
;
1829 struct em28xx
*dev
= *devhandle
;
1830 int retval
= -ENOMEM
;
1832 unsigned int maxh
, maxw
;
1835 mutex_init(&dev
->lock
);
1836 spin_lock_init(&dev
->queue_lock
);
1837 init_waitqueue_head(&dev
->open
);
1838 init_waitqueue_head(&dev
->wait_frame
);
1839 init_waitqueue_head(&dev
->wait_stream
);
1841 dev
->em28xx_write_regs
= em28xx_write_regs
;
1842 dev
->em28xx_read_reg
= em28xx_read_reg
;
1843 dev
->em28xx_read_reg_req_len
= em28xx_read_reg_req_len
;
1844 dev
->em28xx_write_regs_req
= em28xx_write_regs_req
;
1845 dev
->em28xx_read_reg_req
= em28xx_read_reg_req
;
1846 dev
->is_em2800
= em28xx_boards
[dev
->model
].is_em2800
;
1848 errCode
= em28xx_read_reg(dev
, CHIPID_REG
);
1850 em28xx_info("em28xx chip ID = %d\n", errCode
);
1852 em28xx_pre_card_setup(dev
);
1854 errCode
= em28xx_config(dev
);
1856 em28xx_errdev("error configuring device\n");
1857 em28xx_devused
&= ~(1<<dev
->devno
);
1862 /* register i2c bus */
1863 em28xx_i2c_register(dev
);
1865 /* Do board specific init and eeprom reading */
1866 em28xx_card_setup(dev
);
1868 /* Configure audio */
1869 em28xx_audio_analog_set(dev
);
1871 /* configure the device */
1872 em28xx_config_i2c(dev
);
1874 /* set default norm */
1875 dev
->norm
= em28xx_video_template
.current_norm
;
1877 maxw
= norm_maxw(dev
);
1878 maxh
= norm_maxh(dev
);
1880 /* set default image size */
1883 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1884 dev
->field_size
= dev
->width
* dev
->height
;
1886 dev
->interlaced
? dev
->field_size
<< 1 : dev
->field_size
;
1887 dev
->bytesperline
= dev
->width
* 2;
1892 errCode
= em28xx_config(dev
);
1894 list_add_tail(&dev
->devlist
, &em28xx_devlist
);
1896 /* allocate and fill video video_device struct */
1897 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
,
1898 VID_TYPE_CAPTURE
, "video");
1899 if (NULL
== dev
->vdev
) {
1900 em28xx_errdev("cannot allocate video_device.\n");
1904 dev
->vdev
->type
|= VID_TYPE_TUNER
;
1906 /* register v4l2 video video_device */
1907 retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
1908 video_nr
[dev
->devno
]);
1910 em28xx_errdev("unable to register video device (error=%i).\n",
1915 /* Allocate and fill vbi video_device struct */
1916 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
,
1917 VFL_TYPE_VBI
, "vbi");
1918 /* register v4l2 vbi video_device */
1919 if (video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1920 vbi_nr
[dev
->devno
]) < 0) {
1921 em28xx_errdev("unable to register vbi device\n");
1926 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
1927 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
,
1928 VFL_TYPE_RADIO
, "radio");
1929 if (NULL
== dev
->radio_dev
) {
1930 em28xx_errdev("cannot allocate video_device.\n");
1933 retval
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
1934 radio_nr
[dev
->devno
]);
1936 em28xx_errdev("can't register radio device\n");
1939 em28xx_info("Registered radio device as /dev/radio%d\n",
1940 dev
->radio_dev
->minor
& 0x1f);
1944 if (dev
->has_msp34xx
) {
1945 /* Send a reset to other chips via gpio */
1946 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xf7", 1);
1948 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xff", 1);
1954 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1955 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1956 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1958 mutex_lock(&em28xx_extension_devlist_lock
);
1959 if (!list_empty(&em28xx_extension_devlist
)) {
1960 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1965 mutex_unlock(&em28xx_extension_devlist_lock
);
1970 em28xx_release_resources(dev
);
1971 mutex_unlock(&dev
->lock
);
1976 #if defined(CONFIG_MODULES) && defined(MODULE)
1977 static void request_module_async(struct work_struct
*work
)
1979 struct em28xx
*dev
= container_of(work
,
1980 struct em28xx
, request_module_wk
);
1982 if (!dev
->has_audio_class
)
1983 request_module("em28xx-alsa");
1986 static void request_modules(struct em28xx
*dev
)
1988 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
1989 schedule_work(&dev
->request_module_wk
);
1992 #define request_modules(dev)
1993 #endif /* CONFIG_MODULES */
1996 * em28xx_usb_probe()
1997 * checks for supported devices
1999 static int em28xx_usb_probe(struct usb_interface
*interface
,
2000 const struct usb_device_id
*id
)
2002 const struct usb_endpoint_descriptor
*endpoint
;
2003 struct usb_device
*udev
;
2004 struct usb_interface
*uif
;
2005 struct em28xx
*dev
= NULL
;
2006 int retval
= -ENODEV
;
2009 udev
= usb_get_dev(interface_to_usbdev(interface
));
2010 ifnum
= interface
->altsetting
[0].desc
.bInterfaceNumber
;
2012 /* Check to see next free device and mark as used */
2013 nr
=find_first_zero_bit(&em28xx_devused
,EM28XX_MAXBOARDS
);
2014 em28xx_devused
|=1<<nr
;
2016 /* Don't register audio interfaces */
2017 if (interface
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
2018 em28xx_err(DRIVER_NAME
" audio device (%04x:%04x): interface %i, class %i\n",
2019 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
2021 interface
->altsetting
[0].desc
.bInterfaceClass
);
2023 em28xx_devused
&=~(1<<nr
);
2027 em28xx_err(DRIVER_NAME
" new video device (%04x:%04x): interface %i, class %i\n",
2028 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
2030 interface
->altsetting
[0].desc
.bInterfaceClass
);
2032 endpoint
= &interface
->cur_altsetting
->endpoint
[1].desc
;
2034 /* check if the device has the iso in endpoint at the correct place */
2035 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
2036 USB_ENDPOINT_XFER_ISOC
) {
2037 em28xx_err(DRIVER_NAME
" probing error: endpoint is non-ISO endpoint!\n");
2038 em28xx_devused
&=~(1<<nr
);
2041 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
2042 em28xx_err(DRIVER_NAME
" probing error: endpoint is ISO OUT endpoint!\n");
2043 em28xx_devused
&=~(1<<nr
);
2047 if (nr
>= EM28XX_MAXBOARDS
) {
2048 printk (DRIVER_NAME
": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS
);
2049 em28xx_devused
&=~(1<<nr
);
2053 /* allocate memory for our device state and initialize it */
2054 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2056 em28xx_err(DRIVER_NAME
": out of memory!\n");
2057 em28xx_devused
&=~(1<<nr
);
2061 snprintf(dev
->name
, 29, "em28xx #%d", nr
);
2063 dev
->model
= id
->driver_info
;
2065 /* Checks if audio is provided by some interface */
2066 for (i
= 0; i
< udev
->config
->desc
.bNumInterfaces
; i
++) {
2067 uif
= udev
->config
->interface
[i
];
2068 if (uif
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
2069 dev
->has_audio_class
= 1;
2074 printk(KERN_INFO DRIVER_NAME
" %s usb audio class\n",
2075 dev
->has_audio_class
? "Has" : "Doesn't have");
2077 /* compute alternate max packet sizes */
2078 uif
= udev
->actconfig
->interface
[0];
2080 dev
->num_alt
=uif
->num_altsetting
;
2081 em28xx_info("Alternate settings: %i\n",dev
->num_alt
);
2082 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2083 dev
->alt_max_pkt_size
= kmalloc(32*
2084 dev
->num_alt
,GFP_KERNEL
);
2085 if (dev
->alt_max_pkt_size
== NULL
) {
2086 em28xx_errdev("out of memory!\n");
2087 em28xx_devused
&=~(1<<nr
);
2092 for (i
= 0; i
< dev
->num_alt
; i
++) {
2093 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
2095 dev
->alt_max_pkt_size
[i
] =
2096 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
2097 em28xx_info("Alternate setting %i, max size= %i\n",i
,
2098 dev
->alt_max_pkt_size
[i
]);
2101 if ((card
[nr
]>=0)&&(card
[nr
]<em28xx_bcount
))
2102 dev
->model
= card
[nr
];
2104 /* allocate device struct */
2105 retval
= em28xx_init_dev(&dev
, udev
, nr
);
2109 em28xx_info("Found %s\n", em28xx_boards
[dev
->model
].name
);
2111 /* save our data pointer in this interface device */
2112 usb_set_intfdata(interface
, dev
);
2114 request_modules(dev
);
2120 * em28xx_usb_disconnect()
2121 * called when the device gets diconencted
2122 * video device will be unregistered on v4l2_close in case it is still open
2124 static void em28xx_usb_disconnect(struct usb_interface
*interface
)
2127 struct em28xx_ops
*ops
= NULL
;
2129 dev
= usb_get_intfdata(interface
);
2130 usb_set_intfdata(interface
, NULL
);
2135 em28xx_info("disconnecting %s\n", dev
->vdev
->name
);
2137 /* wait until all current v4l2 io is finished then deallocate resources */
2138 mutex_lock(&dev
->lock
);
2140 wake_up_interruptible_all(&dev
->open
);
2144 ("device /dev/video%d is open! Deregistration and memory "
2145 "deallocation are deferred on close.\n",
2146 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
2148 dev
->state
|= DEV_MISCONFIGURED
;
2149 em28xx_uninit_isoc(dev
);
2150 dev
->state
|= DEV_DISCONNECTED
;
2151 wake_up_interruptible(&dev
->wait_frame
);
2152 wake_up_interruptible(&dev
->wait_stream
);
2154 dev
->state
|= DEV_DISCONNECTED
;
2155 em28xx_release_resources(dev
);
2157 mutex_unlock(&dev
->lock
);
2159 mutex_lock(&em28xx_extension_devlist_lock
);
2160 if (!list_empty(&em28xx_extension_devlist
)) {
2161 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
2165 mutex_unlock(&em28xx_extension_devlist_lock
);
2168 kfree(dev
->alt_max_pkt_size
);
2173 static struct usb_driver em28xx_usb_driver
= {
2175 .probe
= em28xx_usb_probe
,
2176 .disconnect
= em28xx_usb_disconnect
,
2177 .id_table
= em28xx_id_table
,
2180 static int __init
em28xx_module_init(void)
2184 printk(KERN_INFO DRIVER_NAME
" v4l2 driver version %d.%d.%d loaded\n",
2185 (EM28XX_VERSION_CODE
>> 16) & 0xff,
2186 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
2188 printk(KERN_INFO DRIVER_NAME
" snapshot date %04d-%02d-%02d\n",
2189 SNAPSHOT
/ 10000, (SNAPSHOT
/ 100) % 100, SNAPSHOT
% 100);
2192 /* register this driver with the USB subsystem */
2193 result
= usb_register(&em28xx_usb_driver
);
2195 em28xx_err(DRIVER_NAME
2196 " usb_register failed. Error number %d.\n", result
);
2201 static void __exit
em28xx_module_exit(void)
2203 /* deregister this driver with the USB subsystem */
2204 usb_deregister(&em28xx_usb_driver
);
2207 module_init(em28xx_module_init
);
2208 module_exit(em28xx_module_exit
);