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/video_decoder.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/msp3400.h>
43 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
45 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
46 "Sascha Sommer <saschasommer@freenet.de>"
48 #define DRIVER_NAME "em28xx"
49 #define DRIVER_DESC "Empia em28xx based USB video device driver"
50 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
52 #define em28xx_videodbg(fmt, arg...) do {\
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __FUNCTION__ , ##arg); } while (0)
57 MODULE_AUTHOR(DRIVER_AUTHOR
);
58 MODULE_DESCRIPTION(DRIVER_DESC
);
59 MODULE_LICENSE("GPL");
61 static LIST_HEAD(em28xx_devlist
);
63 static unsigned int card
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
64 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
65 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
66 module_param_array(card
, int, NULL
, 0444);
67 module_param_array(video_nr
, int, NULL
, 0444);
68 module_param_array(vbi_nr
, int, NULL
, 0444);
69 MODULE_PARM_DESC(card
,"card type");
70 MODULE_PARM_DESC(video_nr
,"video device numbers");
71 MODULE_PARM_DESC(vbi_nr
,"vbi device numbers");
73 static unsigned int video_debug
= 0;
74 module_param(video_debug
,int,0644);
75 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
77 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
78 static unsigned long em28xx_devused
;
80 /* supported tv norms */
81 static struct em28xx_tvnorm tvnorms
[] = {
85 .mode
= VIDEO_MODE_PAL
,
89 .mode
= VIDEO_MODE_NTSC
,
93 .mode
= VIDEO_MODE_SECAM
,
97 .mode
= VIDEO_MODE_PAL
,
101 #define TVNORMS ARRAY_SIZE(tvnorms)
103 /* supported controls */
104 /* Common to all boards */
105 static struct v4l2_queryctrl em28xx_qctrl
[] = {
107 .id
= V4L2_CID_AUDIO_VOLUME
,
108 .type
= V4L2_CTRL_TYPE_INTEGER
,
113 .default_value
= 0x1f,
116 .id
= V4L2_CID_AUDIO_MUTE
,
117 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
127 static struct usb_driver em28xx_usb_driver
;
130 /********************* v4l2 interface ******************************************/
134 * inits registers with sane defaults
136 static int em28xx_config(struct em28xx
*dev
)
139 /* Sets I2C speed to 100 KHz */
141 em28xx_write_regs_req(dev
, 0x00, 0x06, "\x40", 1);
143 /* enable vbi capturing */
145 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
146 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
147 em28xx_write_regs_req(dev
,0x00,0x11,"\x51",1);
149 em28xx_audio_usb_mute(dev
, 1);
150 dev
->mute
= 1; /* maybe not the right place... */
152 em28xx_audio_analog_set(dev
);
153 em28xx_audio_analog_setup(dev
);
154 em28xx_outfmt_set_yuv422(dev
);
155 em28xx_colorlevels_set_default(dev
);
156 em28xx_compression_disable(dev
);
162 * em28xx_config_i2c()
163 * configure i2c attached devices
165 static void em28xx_config_i2c(struct em28xx
*dev
)
167 struct v4l2_routing route
;
169 route
.input
= INPUT(dev
->ctl_input
)->vmux
;
171 em28xx_i2c_call_clients(dev
, VIDIOC_INT_RESET
, NULL
);
172 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
173 em28xx_i2c_call_clients(dev
, VIDIOC_STREAMON
, NULL
);
177 * em28xx_empty_framequeues()
178 * prepare queues for incoming and outgoing frames
180 static void em28xx_empty_framequeues(struct em28xx
*dev
)
184 INIT_LIST_HEAD(&dev
->inqueue
);
185 INIT_LIST_HEAD(&dev
->outqueue
);
187 for (i
= 0; i
< EM28XX_NUM_FRAMES
; i
++) {
188 dev
->frame
[i
].state
= F_UNUSED
;
189 dev
->frame
[i
].buf
.bytesused
= 0;
193 static void video_mux(struct em28xx
*dev
, int index
)
196 struct v4l2_routing route
;
198 route
.input
= INPUT(index
)->vmux
;
200 dev
->ctl_input
= index
;
201 dev
->ctl_ainput
= INPUT(index
)->amux
;
203 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
205 if (dev
->has_msp34xx
) {
207 em28xx_i2c_call_clients(dev
, VIDIOC_INT_I2S_CLOCK_FREQ
, &dev
->i2s_speed
);
208 route
.input
= dev
->ctl_ainput
;
209 route
.output
= MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
);
210 /* Note: this is msp3400 specific */
211 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_AUDIO_ROUTING
, &route
);
212 ainput
= EM28XX_AUDIO_SRC_TUNER
;
213 em28xx_audio_source(dev
, ainput
);
215 switch (dev
->ctl_ainput
) {
217 ainput
= EM28XX_AUDIO_SRC_TUNER
;
220 ainput
= EM28XX_AUDIO_SRC_LINE
;
222 em28xx_audio_source(dev
, ainput
);
226 /* Usage lock check functions */
227 static int res_get(struct em28xx_fh
*fh
)
229 struct em28xx
*dev
= fh
->dev
;
232 /* This instance already has stream_on */
236 mutex_lock(&dev
->lock
);
245 mutex_unlock(&dev
->lock
);
249 static int res_check(struct em28xx_fh
*fh
)
251 return (fh
->stream_on
);
254 static void res_free(struct em28xx_fh
*fh
)
256 struct em28xx
*dev
= fh
->dev
;
258 mutex_lock(&dev
->lock
);
261 mutex_unlock(&dev
->lock
);
267 static void em28xx_vm_open(struct vm_area_struct
*vma
)
269 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
276 static void em28xx_vm_close(struct vm_area_struct
*vma
)
278 /* NOTE: buffers are not freed here */
279 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
281 if (f
->vma_use_count
)
285 static struct vm_operations_struct em28xx_vm_ops
= {
286 .open
= em28xx_vm_open
,
287 .close
= em28xx_vm_close
,
293 * return the current saturation, brightness or contrast, mute state
295 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
298 case V4L2_CID_AUDIO_MUTE
:
299 ctrl
->value
= dev
->mute
;
301 case V4L2_CID_AUDIO_VOLUME
:
302 ctrl
->value
= dev
->volume
;
311 * mute or set new saturation, brightness or contrast
313 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
316 case V4L2_CID_AUDIO_MUTE
:
317 if (ctrl
->value
!= dev
->mute
) {
318 dev
->mute
= ctrl
->value
;
319 em28xx_audio_usb_mute(dev
, ctrl
->value
);
320 return em28xx_audio_analog_set(dev
);
323 case V4L2_CID_AUDIO_VOLUME
:
324 dev
->volume
= ctrl
->value
;
325 return em28xx_audio_analog_set(dev
);
332 * em28xx_stream_interrupt()
335 static int em28xx_stream_interrupt(struct em28xx
*dev
)
339 /* stop reading from the device */
341 dev
->stream
= STREAM_INTERRUPT
;
342 rc
= wait_event_timeout(dev
->wait_stream
,
343 (dev
->stream
== STREAM_OFF
) ||
344 (dev
->state
& DEV_DISCONNECTED
),
348 dev
->state
|= DEV_MISCONFIGURED
;
349 em28xx_videodbg("device is misconfigured; close and "
350 "open /dev/video%d again\n",
351 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
359 static int check_dev(struct em28xx
*dev
)
361 if (dev
->state
& DEV_DISCONNECTED
) {
362 em28xx_errdev("v4l2 ioctl: device not present\n");
366 if (dev
->state
& DEV_MISCONFIGURED
) {
367 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
368 "close and open it again\n");
374 static void get_scale(struct em28xx
*dev
,
375 unsigned int width
, unsigned int height
,
376 unsigned int *hscale
, unsigned int *vscale
)
378 unsigned int maxw
= norm_maxw(dev
);
379 unsigned int maxh
= norm_maxh(dev
);
381 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
382 if (*hscale
>= 0x4000)
385 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
386 if (*vscale
>= 0x4000)
390 /* ------------------------------------------------------------------
391 IOCTL vidioc handling
392 ------------------------------------------------------------------*/
394 static int vidioc_g_fmt_cap(struct file
*file
, void *priv
,
395 struct v4l2_format
*f
)
397 struct em28xx_fh
*fh
= priv
;
398 struct em28xx
*dev
= fh
->dev
;
400 mutex_lock(&dev
->lock
);
402 f
->fmt
.pix
.width
= dev
->width
;
403 f
->fmt
.pix
.height
= dev
->height
;
404 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
405 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
406 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
407 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
409 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
410 f
->fmt
.pix
.field
= dev
->interlaced
?
411 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
413 mutex_unlock(&dev
->lock
);
417 static int vidioc_try_fmt_cap(struct file
*file
, void *priv
,
418 struct v4l2_format
*f
)
420 struct em28xx_fh
*fh
= priv
;
421 struct em28xx
*dev
= fh
->dev
;
422 int width
= f
->fmt
.pix
.width
;
423 int height
= f
->fmt
.pix
.height
;
424 unsigned int maxw
= norm_maxw(dev
);
425 unsigned int maxh
= norm_maxh(dev
);
426 unsigned int hscale
, vscale
;
428 /* width must even because of the YUYV format
429 height must be even because of interlacing */
442 mutex_lock(&dev
->lock
);
444 if (dev
->is_em2800
) {
445 /* the em2800 can only scale down to 50% */
446 if (height
% (maxh
/ 2))
448 if (width
% (maxw
/ 2))
450 /* according to empiatech support */
451 /* the MaxPacketSize is to small to support */
452 /* framesizes larger than 640x480 @ 30 fps */
453 /* or 640x576 @ 25 fps. As this would cut */
454 /* of a part of the image we prefer */
455 /* 360x576 or 360x480 for now */
456 if (width
== maxw
&& height
== maxh
)
460 get_scale(dev
, width
, height
, &hscale
, &vscale
);
462 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
463 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
465 f
->fmt
.pix
.width
= width
;
466 f
->fmt
.pix
.height
= height
;
467 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
468 f
->fmt
.pix
.bytesperline
= width
* 2;
469 f
->fmt
.pix
.sizeimage
= width
* 2 * height
;
470 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
471 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
473 mutex_unlock(&dev
->lock
);
477 static int vidioc_s_fmt_cap(struct file
*file
, void *priv
,
478 struct v4l2_format
*f
)
480 struct em28xx_fh
*fh
= priv
;
481 struct em28xx
*dev
= fh
->dev
;
488 vidioc_try_fmt_cap(file
, priv
, f
);
490 mutex_lock(&dev
->lock
);
492 for (i
= 0; i
< dev
->num_frames
; i
++)
493 if (dev
->frame
[i
].vma_use_count
) {
494 em28xx_videodbg("VIDIOC_S_FMT failed. "
495 "Unmap the buffers first.\n");
500 /* stop io in case it is already in progress */
501 if (dev
->stream
== STREAM_ON
) {
502 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
503 rc
= em28xx_stream_interrupt(dev
);
508 em28xx_release_buffers(dev
);
511 /* set new image size */
512 dev
->width
= f
->fmt
.pix
.width
;
513 dev
->height
= f
->fmt
.pix
.height
;
514 dev
->frame_size
= dev
->width
* dev
->height
* 2;
515 dev
->field_size
= dev
->frame_size
>> 1;
516 dev
->bytesperline
= dev
->width
* 2;
517 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
519 /* FIXME: This is really weird! Why capture is starting with
522 em28xx_uninit_isoc(dev
);
523 em28xx_set_alternate(dev
);
524 em28xx_capture_start(dev
, 1);
525 em28xx_resolution_set(dev
);
526 em28xx_init_isoc(dev
);
530 mutex_unlock(&dev
->lock
);
534 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
536 struct em28xx_fh
*fh
= priv
;
537 struct em28xx
*dev
= fh
->dev
;
538 struct v4l2_format f
;
546 for (i
= 0; i
< TVNORMS
; i
++)
547 if (*norm
== tvnorms
[i
].id
)
550 for (i
= 0; i
< TVNORMS
; i
++)
551 if (*norm
& tvnorms
[i
].id
)
556 *norm
= tvnorms
[i
].id
;
558 mutex_lock(&dev
->lock
);
559 dev
->tvnorm
= &tvnorms
[i
];
560 mutex_unlock(&dev
->lock
);
562 /* Adjusts width/height, if needed */
563 f
.fmt
.pix
.width
= dev
->width
;
564 f
.fmt
.pix
.height
= dev
->height
;
565 vidioc_try_fmt_cap(file
, priv
, &f
);
567 mutex_lock(&dev
->lock
);
569 /* set new image size */
570 dev
->width
= f
.fmt
.pix
.width
;
571 dev
->height
= f
.fmt
.pix
.height
;
572 dev
->frame_size
= dev
->width
* dev
->height
* 2;
573 dev
->field_size
= dev
->frame_size
>> 1;
574 dev
->bytesperline
= dev
->width
* 2;
575 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
577 em28xx_resolution_set(dev
);
578 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->tvnorm
->id
);
580 mutex_unlock(&dev
->lock
);
584 static const char *iname
[] = {
585 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
586 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
587 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
588 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
589 [EM28XX_VMUX_SVIDEO
] = "S-Video",
590 [EM28XX_VMUX_TELEVISION
] = "Television",
591 [EM28XX_VMUX_CABLE
] = "Cable TV",
592 [EM28XX_VMUX_DVB
] = "DVB",
593 [EM28XX_VMUX_DEBUG
] = "for debug only",
596 static int vidioc_enum_input(struct file
*file
, void *priv
,
597 struct v4l2_input
*i
)
599 struct em28xx_fh
*fh
= priv
;
600 struct em28xx
*dev
= fh
->dev
;
604 if (n
>= MAX_EM28XX_INPUT
)
606 if (0 == INPUT(n
)->type
)
610 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
612 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
614 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
615 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
616 i
->type
= V4L2_INPUT_TYPE_TUNER
;
618 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
619 i
->std
|= tvnorms
[n
].id
;
624 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
626 struct em28xx_fh
*fh
= priv
;
627 struct em28xx
*dev
= fh
->dev
;
634 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
636 struct em28xx_fh
*fh
= priv
;
637 struct em28xx
*dev
= fh
->dev
;
644 if (i
>= MAX_EM28XX_INPUT
)
646 if (0 == INPUT(i
)->type
)
649 mutex_lock(&dev
->lock
);
653 mutex_unlock(&dev
->lock
);
657 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
659 struct em28xx_fh
*fh
= priv
;
660 struct em28xx
*dev
= fh
->dev
;
661 unsigned int index
= a
->index
;
666 index
= dev
->ctl_ainput
;
669 strcpy(a
->name
, "Television");
671 strcpy(a
->name
, "Line In");
673 a
->capability
= V4L2_AUDCAP_STEREO
;
679 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
681 struct em28xx_fh
*fh
= priv
;
682 struct em28xx
*dev
= fh
->dev
;
684 if (a
->index
!= dev
->ctl_ainput
)
690 static int vidioc_queryctrl(struct file
*file
, void *priv
,
691 struct v4l2_queryctrl
*qc
)
693 struct em28xx_fh
*fh
= priv
;
694 struct em28xx
*dev
= fh
->dev
;
703 memset(qc
, 0, sizeof(*qc
));
707 if (!dev
->has_msp34xx
) {
708 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
709 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
710 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
715 mutex_lock(&dev
->lock
);
716 em28xx_i2c_call_clients(dev
, VIDIOC_QUERYCTRL
, qc
);
717 mutex_unlock(&dev
->lock
);
725 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
726 struct v4l2_control
*ctrl
)
728 struct em28xx_fh
*fh
= priv
;
729 struct em28xx
*dev
= fh
->dev
;
735 mutex_lock(&dev
->lock
);
737 if (!dev
->has_msp34xx
)
738 rc
= em28xx_get_ctrl(dev
, ctrl
);
743 em28xx_i2c_call_clients(dev
, VIDIOC_G_CTRL
, ctrl
);
747 mutex_unlock(&dev
->lock
);
751 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
752 struct v4l2_control
*ctrl
)
754 struct em28xx_fh
*fh
= priv
;
755 struct em28xx
*dev
= fh
->dev
;
763 mutex_lock(&dev
->lock
);
765 if (dev
->has_msp34xx
)
766 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
769 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
770 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
771 if (ctrl
->value
< em28xx_qctrl
[i
].minimum
||
772 ctrl
->value
> em28xx_qctrl
[i
].maximum
) {
777 rc
= em28xx_set_ctrl(dev
, ctrl
);
783 /* Control not found - try to send it to the attached devices */
785 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
789 mutex_unlock(&dev
->lock
);
793 static int vidioc_g_tuner(struct file
*file
, void *priv
,
794 struct v4l2_tuner
*t
)
796 struct em28xx_fh
*fh
= priv
;
797 struct em28xx
*dev
= fh
->dev
;
807 strcpy(t
->name
, "Tuner");
809 mutex_lock(&dev
->lock
);
811 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
813 mutex_unlock(&dev
->lock
);
817 static int vidioc_s_tuner(struct file
*file
, void *priv
,
818 struct v4l2_tuner
*t
)
820 struct em28xx_fh
*fh
= priv
;
821 struct em28xx
*dev
= fh
->dev
;
831 mutex_lock(&dev
->lock
);
833 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
835 mutex_unlock(&dev
->lock
);
839 static int vidioc_g_frequency(struct file
*file
, void *priv
,
840 struct v4l2_frequency
*f
)
842 struct em28xx_fh
*fh
= priv
;
843 struct em28xx
*dev
= fh
->dev
;
845 f
->type
= V4L2_TUNER_ANALOG_TV
;
846 f
->frequency
= dev
->ctl_freq
;
851 static int vidioc_s_frequency(struct file
*file
, void *priv
,
852 struct v4l2_frequency
*f
)
854 struct em28xx_fh
*fh
= priv
;
855 struct em28xx
*dev
= fh
->dev
;
865 if (V4L2_TUNER_ANALOG_TV
!= f
->type
)
868 mutex_lock(&dev
->lock
);
870 dev
->ctl_freq
= f
->frequency
;
871 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
873 mutex_unlock(&dev
->lock
);
877 static int vidioc_cropcap(struct file
*file
, void *priv
,
878 struct v4l2_cropcap
*cc
)
880 struct em28xx_fh
*fh
= priv
;
881 struct em28xx
*dev
= fh
->dev
;
883 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
888 cc
->bounds
.width
= dev
->width
;
889 cc
->bounds
.height
= dev
->height
;
890 cc
->defrect
= cc
->bounds
;
891 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
892 cc
->pixelaspect
.denominator
= 59;
897 static int vidioc_streamon(struct file
*file
, void *priv
,
898 enum v4l2_buf_type type
)
900 struct em28xx_fh
*fh
= priv
;
901 struct em28xx
*dev
= fh
->dev
;
908 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
911 if (list_empty(&dev
->inqueue
))
914 mutex_lock(&dev
->lock
);
916 if (unlikely(res_get(fh
) < 0)) {
917 mutex_unlock(&dev
->lock
);
921 dev
->stream
= STREAM_ON
; /* FIXME: Start video capture here? */
923 mutex_unlock(&dev
->lock
);
927 static int vidioc_streamoff(struct file
*file
, void *priv
,
928 enum v4l2_buf_type type
)
930 struct em28xx_fh
*fh
= priv
;
931 struct em28xx
*dev
= fh
->dev
;
938 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
941 mutex_lock(&dev
->lock
);
943 if (dev
->stream
== STREAM_ON
) {
944 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
945 rc
= em28xx_stream_interrupt(dev
);
947 mutex_unlock(&dev
->lock
);
952 em28xx_empty_framequeues(dev
);
954 mutex_unlock(&dev
->lock
);
958 static int vidioc_querycap(struct file
*file
, void *priv
,
959 struct v4l2_capability
*cap
)
961 struct em28xx_fh
*fh
= priv
;
962 struct em28xx
*dev
= fh
->dev
;
964 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
965 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
966 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
, sizeof(cap
->bus_info
));
968 cap
->version
= EM28XX_VERSION_CODE
;
971 V4L2_CAP_SLICED_VBI_CAPTURE
|
972 V4L2_CAP_VIDEO_CAPTURE
|
974 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
977 cap
->capabilities
|= V4L2_CAP_TUNER
;
982 static int vidioc_enum_fmt_cap(struct file
*file
, void *priv
,
983 struct v4l2_fmtdesc
*fmtd
)
985 if (fmtd
->index
!= 0)
988 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
989 strcpy(fmtd
->description
, "Packed YUY2");
990 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
991 memset(fmtd
->reserved
, 0, sizeof(fmtd
->reserved
));
996 /* Sliced VBI ioctls */
997 static int vidioc_g_fmt_vbi_capture(struct file
*file
, void *priv
,
998 struct v4l2_format
*f
)
1000 struct em28xx_fh
*fh
= priv
;
1001 struct em28xx
*dev
= fh
->dev
;
1004 rc
= check_dev(dev
);
1008 mutex_lock(&dev
->lock
);
1010 f
->fmt
.sliced
.service_set
= 0;
1012 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
1014 if (f
->fmt
.sliced
.service_set
== 0)
1017 mutex_unlock(&dev
->lock
);
1021 static int vidioc_try_set_vbi_capture(struct file
*file
, void *priv
,
1022 struct v4l2_format
*f
)
1024 struct em28xx_fh
*fh
= priv
;
1025 struct em28xx
*dev
= fh
->dev
;
1028 rc
= check_dev(dev
);
1032 mutex_lock(&dev
->lock
);
1033 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
1034 mutex_unlock(&dev
->lock
);
1036 if (f
->fmt
.sliced
.service_set
== 0)
1043 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1044 struct v4l2_requestbuffers
*rb
)
1046 struct em28xx_fh
*fh
= priv
;
1047 struct em28xx
*dev
= fh
->dev
;
1051 rc
= check_dev(dev
);
1055 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1056 rb
->memory
!= V4L2_MEMORY_MMAP
)
1059 if (dev
->io
== IO_READ
) {
1060 em28xx_videodbg("method is set to read;"
1061 " close and open the device again to"
1062 " choose the mmap I/O method\n");
1066 for (i
= 0; i
< dev
->num_frames
; i
++)
1067 if (dev
->frame
[i
].vma_use_count
) {
1068 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1069 "previous buffers are still mapped\n");
1073 mutex_lock(&dev
->lock
);
1075 if (dev
->stream
== STREAM_ON
) {
1076 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1077 rc
= em28xx_stream_interrupt(dev
);
1079 mutex_unlock(&dev
->lock
);
1084 em28xx_empty_framequeues(dev
);
1086 em28xx_release_buffers(dev
);
1088 rb
->count
= em28xx_request_buffers(dev
, rb
->count
);
1090 dev
->frame_current
= NULL
;
1091 dev
->io
= rb
->count
? IO_MMAP
: IO_NONE
;
1093 mutex_unlock(&dev
->lock
);
1097 static int vidioc_querybuf(struct file
*file
, void *priv
,
1098 struct v4l2_buffer
*b
)
1100 struct em28xx_fh
*fh
= priv
;
1101 struct em28xx
*dev
= fh
->dev
;
1104 rc
= check_dev(dev
);
1108 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1109 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
)
1112 mutex_lock(&dev
->lock
);
1114 memcpy(b
, &dev
->frame
[b
->index
].buf
, sizeof(*b
));
1116 if (dev
->frame
[b
->index
].vma_use_count
)
1117 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1119 if (dev
->frame
[b
->index
].state
== F_DONE
)
1120 b
->flags
|= V4L2_BUF_FLAG_DONE
;
1121 else if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1122 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1124 mutex_unlock(&dev
->lock
);
1128 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1130 struct em28xx_fh
*fh
= priv
;
1131 struct em28xx
*dev
= fh
->dev
;
1132 unsigned long lock_flags
;
1135 rc
= check_dev(dev
);
1139 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
||
1140 b
->index
>= dev
->num_frames
)
1143 if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1146 dev
->frame
[b
->index
].state
= F_QUEUED
;
1148 /* add frame to fifo */
1149 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1150 list_add_tail(&dev
->frame
[b
->index
].frame
, &dev
->inqueue
);
1151 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1156 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1158 struct em28xx_fh
*fh
= priv
;
1159 struct em28xx
*dev
= fh
->dev
;
1161 struct em28xx_frame_t
*f
;
1162 unsigned long lock_flags
;
1164 rc
= check_dev(dev
);
1168 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
1171 if (list_empty(&dev
->outqueue
)) {
1172 if (dev
->stream
== STREAM_OFF
)
1175 if (file
->f_flags
& O_NONBLOCK
)
1178 rc
= wait_event_interruptible(dev
->wait_frame
,
1179 (!list_empty(&dev
->outqueue
)) ||
1180 (dev
->state
& DEV_DISCONNECTED
));
1184 if (dev
->state
& DEV_DISCONNECTED
)
1188 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1189 f
= list_entry(dev
->outqueue
.next
, struct em28xx_frame_t
, frame
);
1190 list_del(dev
->outqueue
.next
);
1191 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1193 f
->state
= F_UNUSED
;
1194 memcpy(b
, &f
->buf
, sizeof(*b
));
1196 if (f
->vma_use_count
)
1197 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1203 * em28xx_v4l2_open()
1204 * inits the device and starts isoc transfer
1206 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
1208 int minor
= iminor(inode
);
1210 struct em28xx
*h
,*dev
= NULL
;
1211 struct em28xx_fh
*fh
;
1213 list_for_each_entry(h
, &em28xx_devlist
, devlist
) {
1214 if (h
->vdev
->minor
== minor
) {
1216 dev
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1218 if (h
->vbi_dev
->minor
== minor
) {
1220 dev
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1226 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1227 minor
,v4l2_type_names
[dev
->type
],dev
->users
);
1229 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1232 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1235 mutex_lock(&dev
->lock
);
1237 filp
->private_data
= fh
;
1239 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1240 em28xx_set_alternate(dev
);
1242 dev
->width
= norm_maxw(dev
);
1243 dev
->height
= norm_maxh(dev
);
1244 dev
->frame_size
= dev
->width
* dev
->height
* 2;
1245 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1246 dev
->bytesperline
= dev
->width
* 2;
1250 em28xx_capture_start(dev
, 1);
1251 em28xx_resolution_set(dev
);
1254 /* start the transfer */
1255 errCode
= em28xx_init_isoc(dev
);
1259 em28xx_empty_framequeues(dev
);
1265 mutex_unlock(&dev
->lock
);
1270 * em28xx_realease_resources()
1271 * unregisters the v4l2,i2c and usb devices
1272 * called when the device gets disconected or at module unload
1274 static void em28xx_release_resources(struct em28xx
*dev
)
1277 /*FIXME: I2C IR should be disconnected */
1279 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1280 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1281 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1282 list_del(&dev
->devlist
);
1283 video_unregister_device(dev
->vdev
);
1284 video_unregister_device(dev
->vbi_dev
);
1285 em28xx_i2c_unregister(dev
);
1286 usb_put_dev(dev
->udev
);
1289 /* Mark device as unused */
1290 em28xx_devused
&=~(1<<dev
->devno
);
1294 * em28xx_v4l2_close()
1295 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1297 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
1299 struct em28xx_fh
*fh
= filp
->private_data
;
1300 struct em28xx
*dev
= fh
->dev
;
1303 em28xx_videodbg("users=%d\n", dev
->users
);
1309 mutex_lock(&dev
->lock
);
1311 if (dev
->users
== 1) {
1312 em28xx_uninit_isoc(dev
);
1313 em28xx_release_buffers(dev
);
1316 /* the device is already disconnect,
1317 free the remaining resources */
1318 if (dev
->state
& DEV_DISCONNECTED
) {
1319 em28xx_release_resources(dev
);
1320 mutex_unlock(&dev
->lock
);
1325 /* set alternate 0 */
1327 em28xx_videodbg("setting alternate 0\n");
1328 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1330 em28xx_errdev("cannot change alternate number to "
1331 "0 (error=%i)\n", errCode
);
1336 wake_up_interruptible_nr(&dev
->open
, 1);
1337 mutex_unlock(&dev
->lock
);
1342 * em28xx_v4l2_read()
1343 * will allocate buffers when called for the first time
1346 em28xx_v4l2_read(struct file
*filp
, char __user
* buf
, size_t count
,
1349 struct em28xx_frame_t
*f
, *i
;
1350 unsigned long lock_flags
;
1352 struct em28xx_fh
*fh
= filp
->private_data
;
1353 struct em28xx
*dev
= fh
->dev
;
1355 /* FIXME: read() is not prepared to allow changing the video
1356 resolution while streaming. Seems a bug at em28xx_set_fmt
1359 if (unlikely(res_get(fh
) < 0))
1362 mutex_lock(&dev
->lock
);
1364 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1365 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1367 if (dev
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1368 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1369 em28xx_videodbg("not supported yet! ...\n");
1370 if (copy_to_user(buf
, "", 1)) {
1371 mutex_unlock(&dev
->lock
);
1374 mutex_unlock(&dev
->lock
);
1377 if (dev
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
) {
1378 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1379 em28xx_videodbg("not supported yet! ...\n");
1380 if (copy_to_user(buf
, "", 1)) {
1381 mutex_unlock(&dev
->lock
);
1384 mutex_unlock(&dev
->lock
);
1388 if (dev
->state
& DEV_DISCONNECTED
) {
1389 em28xx_videodbg("device not present\n");
1390 mutex_unlock(&dev
->lock
);
1394 if (dev
->state
& DEV_MISCONFIGURED
) {
1395 em28xx_videodbg("device misconfigured; close and open it again\n");
1396 mutex_unlock(&dev
->lock
);
1400 if (dev
->io
== IO_MMAP
) {
1401 em28xx_videodbg ("IO method is set to mmap; close and open"
1402 " the device again to choose the read method\n");
1403 mutex_unlock(&dev
->lock
);
1407 if (dev
->io
== IO_NONE
) {
1408 if (!em28xx_request_buffers(dev
, EM28XX_NUM_READ_FRAMES
)) {
1409 em28xx_errdev("read failed, not enough memory\n");
1410 mutex_unlock(&dev
->lock
);
1414 dev
->stream
= STREAM_ON
;
1415 em28xx_queue_unusedframes(dev
);
1419 mutex_unlock(&dev
->lock
);
1423 if (list_empty(&dev
->outqueue
)) {
1424 if (filp
->f_flags
& O_NONBLOCK
) {
1425 mutex_unlock(&dev
->lock
);
1428 ret
= wait_event_interruptible
1430 (!list_empty(&dev
->outqueue
)) ||
1431 (dev
->state
& DEV_DISCONNECTED
));
1433 mutex_unlock(&dev
->lock
);
1436 if (dev
->state
& DEV_DISCONNECTED
) {
1437 mutex_unlock(&dev
->lock
);
1440 dev
->video_bytesread
= 0;
1443 f
= list_entry(dev
->outqueue
.prev
, struct em28xx_frame_t
, frame
);
1445 em28xx_queue_unusedframes(dev
);
1447 if (count
> f
->buf
.length
)
1448 count
= f
->buf
.length
;
1450 if ((dev
->video_bytesread
+ count
) > dev
->frame_size
)
1451 count
= dev
->frame_size
- dev
->video_bytesread
;
1453 if (copy_to_user(buf
, f
->bufmem
+dev
->video_bytesread
, count
)) {
1454 em28xx_err("Error while copying to user\n");
1457 dev
->video_bytesread
+= count
;
1459 if (dev
->video_bytesread
== dev
->frame_size
) {
1460 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1461 list_for_each_entry(i
, &dev
->outqueue
, frame
)
1462 i
->state
= F_UNUSED
;
1463 INIT_LIST_HEAD(&dev
->outqueue
);
1464 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1466 em28xx_queue_unusedframes(dev
);
1467 dev
->video_bytesread
= 0;
1472 mutex_unlock(&dev
->lock
);
1478 * em28xx_v4l2_poll()
1479 * will allocate buffers when called for the first time
1481 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
1483 unsigned int mask
= 0;
1484 struct em28xx_fh
*fh
= filp
->private_data
;
1485 struct em28xx
*dev
= fh
->dev
;
1487 if (unlikely(res_get(fh
) < 0))
1490 mutex_lock(&dev
->lock
);
1492 if (dev
->state
& DEV_DISCONNECTED
) {
1493 em28xx_videodbg("device not present\n");
1494 } else if (dev
->state
& DEV_MISCONFIGURED
) {
1495 em28xx_videodbg("device is misconfigured; close and open it again\n");
1497 if (dev
->io
== IO_NONE
) {
1498 if (!em28xx_request_buffers
1499 (dev
, EM28XX_NUM_READ_FRAMES
)) {
1501 ("poll() failed, not enough memory\n");
1504 dev
->stream
= STREAM_ON
;
1508 if (dev
->io
== IO_READ
) {
1509 em28xx_queue_unusedframes(dev
);
1510 poll_wait(filp
, &dev
->wait_frame
, wait
);
1512 if (!list_empty(&dev
->outqueue
))
1513 mask
|= POLLIN
| POLLRDNORM
;
1515 mutex_unlock(&dev
->lock
);
1521 mutex_unlock(&dev
->lock
);
1526 * em28xx_v4l2_mmap()
1528 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1530 struct em28xx_fh
*fh
= filp
->private_data
;
1531 struct em28xx
*dev
= fh
->dev
;
1532 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1533 unsigned long start
= vma
->vm_start
;
1537 if (unlikely(res_get(fh
) < 0))
1540 mutex_lock(&dev
->lock
);
1542 if (dev
->state
& DEV_DISCONNECTED
) {
1543 em28xx_videodbg("mmap: device not present\n");
1544 mutex_unlock(&dev
->lock
);
1548 if (dev
->state
& DEV_MISCONFIGURED
) {
1549 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1551 mutex_unlock(&dev
->lock
);
1555 if (dev
->io
!= IO_MMAP
|| !(vma
->vm_flags
& VM_WRITE
)) {
1556 mutex_unlock(&dev
->lock
);
1560 if (size
> PAGE_ALIGN(dev
->frame
[0].buf
.length
))
1561 size
= PAGE_ALIGN(dev
->frame
[0].buf
.length
);
1563 for (i
= 0; i
< dev
->num_frames
; i
++) {
1564 if ((dev
->frame
[i
].buf
.m
.offset
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
1567 if (i
== dev
->num_frames
) {
1568 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1569 mutex_unlock(&dev
->lock
);
1573 /* VM_IO is eventually going to replace PageReserved altogether */
1574 vma
->vm_flags
|= VM_IO
;
1575 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1577 pos
= dev
->frame
[i
].bufmem
;
1578 while (size
> 0) { /* size is page-aligned */
1579 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1580 em28xx_videodbg("mmap: vm_insert_page failed\n");
1581 mutex_unlock(&dev
->lock
);
1589 vma
->vm_ops
= &em28xx_vm_ops
;
1590 vma
->vm_private_data
= &dev
->frame
[i
];
1592 em28xx_vm_open(vma
);
1593 mutex_unlock(&dev
->lock
);
1597 static const struct file_operations em28xx_v4l_fops
= {
1598 .owner
= THIS_MODULE
,
1599 .open
= em28xx_v4l2_open
,
1600 .release
= em28xx_v4l2_close
,
1601 .read
= em28xx_v4l2_read
,
1602 .poll
= em28xx_v4l2_poll
,
1603 .mmap
= em28xx_v4l2_mmap
,
1604 .ioctl
= video_ioctl2
,
1605 .llseek
= no_llseek
,
1606 .compat_ioctl
= v4l_compat_ioctl32
,
1609 static const struct video_device em28xx_video_template
= {
1610 .fops
= &em28xx_v4l_fops
,
1611 .release
= video_device_release
,
1614 .vidioc_querycap
= vidioc_querycap
,
1615 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
1616 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
1617 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
1618 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
1619 .vidioc_g_audio
= vidioc_g_audio
,
1620 .vidioc_s_audio
= vidioc_s_audio
,
1621 .vidioc_cropcap
= vidioc_cropcap
,
1623 .vidioc_g_fmt_vbi_capture
= vidioc_g_fmt_vbi_capture
,
1624 .vidioc_try_fmt_vbi_capture
= vidioc_try_set_vbi_capture
,
1625 .vidioc_s_fmt_vbi_capture
= vidioc_try_set_vbi_capture
,
1627 .vidioc_reqbufs
= vidioc_reqbufs
,
1628 .vidioc_querybuf
= vidioc_querybuf
,
1629 .vidioc_qbuf
= vidioc_qbuf
,
1630 .vidioc_dqbuf
= vidioc_dqbuf
,
1631 .vidioc_s_std
= vidioc_s_std
,
1632 .vidioc_enum_input
= vidioc_enum_input
,
1633 .vidioc_g_input
= vidioc_g_input
,
1634 .vidioc_s_input
= vidioc_s_input
,
1635 .vidioc_queryctrl
= vidioc_queryctrl
,
1636 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1637 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1638 .vidioc_streamon
= vidioc_streamon
,
1639 .vidioc_streamoff
= vidioc_streamoff
,
1640 .vidioc_g_tuner
= vidioc_g_tuner
,
1641 .vidioc_s_tuner
= vidioc_s_tuner
,
1642 .vidioc_g_frequency
= vidioc_g_frequency
,
1643 .vidioc_s_frequency
= vidioc_s_frequency
,
1645 .tvnorms
= V4L2_STD_ALL
,
1646 .current_norm
= V4L2_STD_NTSC_M
,
1650 /******************************** usb interface *****************************************/
1654 * allocates and inits the device structs, registers i2c bus and v4l device
1656 static int em28xx_init_dev(struct em28xx
**devhandle
, struct usb_device
*udev
,
1659 struct em28xx
*dev
= *devhandle
;
1660 int retval
= -ENOMEM
;
1662 unsigned int maxh
, maxw
;
1665 mutex_init(&dev
->lock
);
1666 spin_lock_init(&dev
->queue_lock
);
1667 init_waitqueue_head(&dev
->open
);
1668 init_waitqueue_head(&dev
->wait_frame
);
1669 init_waitqueue_head(&dev
->wait_stream
);
1671 dev
->em28xx_write_regs
= em28xx_write_regs
;
1672 dev
->em28xx_read_reg
= em28xx_read_reg
;
1673 dev
->em28xx_read_reg_req_len
= em28xx_read_reg_req_len
;
1674 dev
->em28xx_write_regs_req
= em28xx_write_regs_req
;
1675 dev
->em28xx_read_reg_req
= em28xx_read_reg_req
;
1676 dev
->is_em2800
= em28xx_boards
[dev
->model
].is_em2800
;
1678 em28xx_pre_card_setup(dev
);
1680 errCode
= em28xx_config(dev
);
1682 em28xx_errdev("error configuring device\n");
1683 em28xx_devused
&= ~(1<<dev
->devno
);
1688 /* register i2c bus */
1689 em28xx_i2c_register(dev
);
1691 /* Do board specific init and eeprom reading */
1692 em28xx_card_setup(dev
);
1694 /* configure the device */
1695 em28xx_config_i2c(dev
);
1697 for (i
= 0; i
< TVNORMS
; i
++)
1698 if (em28xx_boards
[dev
->model
].norm
== tvnorms
[i
].mode
)
1703 dev
->tvnorm
= &tvnorms
[i
]; /* set default norm */
1705 em28xx_videodbg("tvnorm=%s\n", dev
->tvnorm
->name
);
1707 maxw
= norm_maxw(dev
);
1708 maxh
= norm_maxh(dev
);
1710 /* set default image size */
1713 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1714 dev
->field_size
= dev
->width
* dev
->height
;
1716 dev
->interlaced
? dev
->field_size
<< 1 : dev
->field_size
;
1717 dev
->bytesperline
= dev
->width
* 2;
1722 errCode
= em28xx_config(dev
);
1724 /* allocate and fill video video_device struct */
1725 dev
->vdev
= video_device_alloc();
1726 if (NULL
== dev
->vdev
) {
1727 em28xx_errdev("cannot allocate video_device.\n");
1728 em28xx_devused
&=~(1<<dev
->devno
);
1732 memcpy(dev
->vdev
, &em28xx_video_template
,
1733 sizeof(em28xx_video_template
));
1734 dev
->vdev
->type
= VID_TYPE_CAPTURE
;
1736 dev
->vdev
->type
|= VID_TYPE_TUNER
;
1737 dev
->vdev
->dev
= &dev
->udev
->dev
;
1738 snprintf(dev
->vdev
->name
, sizeof(dev
->vbi_dev
->name
),
1739 "%s#%d %s", "em28xx", dev
->devno
, "video");
1740 dev
->vdev
->current_norm
= dev
->tvnorm
->id
;
1742 /* Allocate and fill vbi video_device struct */
1743 dev
->vbi_dev
= video_device_alloc();
1744 if (NULL
== dev
->vbi_dev
) {
1745 em28xx_errdev("cannot allocate video_device.\n");
1747 em28xx_devused
&=~(1<<dev
->devno
);
1751 memcpy(dev
->vbi_dev
, &em28xx_video_template
,
1752 sizeof(em28xx_video_template
));
1753 dev
->vbi_dev
->type
= VFL_TYPE_VBI
;
1754 dev
->vbi_dev
->dev
= &dev
->udev
->dev
;
1755 snprintf(dev
->vbi_dev
->name
, sizeof(dev
->vbi_dev
->name
),
1756 "%s#%d %s", "em28xx", dev
->devno
, "vbi");
1757 dev
->vbi_dev
->current_norm
= dev
->tvnorm
->id
;
1759 list_add_tail(&dev
->devlist
,&em28xx_devlist
);
1761 if (dev
->has_msp34xx
) {
1762 /* Send a reset to other chips via gpio */
1763 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xf7", 1);
1765 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xff", 1);
1771 /* register v4l2 video video_device */
1772 if ((retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
1773 video_nr
[dev
->devno
]))) {
1774 em28xx_errdev("unable to register video device (error=%i).\n",
1776 mutex_unlock(&dev
->lock
);
1777 list_del(&dev
->devlist
);
1778 video_device_release(dev
->vdev
);
1779 em28xx_devused
&=~(1<<dev
->devno
);
1784 /* register v4l2 vbi video_device */
1785 if (video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1786 vbi_nr
[dev
->devno
]) < 0) {
1787 printk("unable to register vbi device\n");
1788 mutex_unlock(&dev
->lock
);
1789 list_del(&dev
->devlist
);
1790 video_device_release(dev
->vbi_dev
);
1791 video_device_release(dev
->vdev
);
1792 em28xx_devused
&=~(1<<dev
->devno
);
1797 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1798 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1799 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1805 * em28xx_usb_probe()
1806 * checks for supported devices
1808 static int em28xx_usb_probe(struct usb_interface
*interface
,
1809 const struct usb_device_id
*id
)
1811 const struct usb_endpoint_descriptor
*endpoint
;
1812 struct usb_device
*udev
;
1813 struct usb_interface
*uif
;
1814 struct em28xx
*dev
= NULL
;
1815 int retval
= -ENODEV
;
1818 udev
= usb_get_dev(interface_to_usbdev(interface
));
1819 ifnum
= interface
->altsetting
[0].desc
.bInterfaceNumber
;
1821 /* Check to see next free device and mark as used */
1822 nr
=find_first_zero_bit(&em28xx_devused
,EM28XX_MAXBOARDS
);
1823 em28xx_devused
|=1<<nr
;
1825 /* Don't register audio interfaces */
1826 if (interface
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
1827 em28xx_err(DRIVER_NAME
" audio device (%04x:%04x): interface %i, class %i\n",
1828 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1830 interface
->altsetting
[0].desc
.bInterfaceClass
);
1832 em28xx_devused
&=~(1<<nr
);
1836 em28xx_err(DRIVER_NAME
" new video device (%04x:%04x): interface %i, class %i\n",
1837 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1839 interface
->altsetting
[0].desc
.bInterfaceClass
);
1841 endpoint
= &interface
->cur_altsetting
->endpoint
[1].desc
;
1843 /* check if the device has the iso in endpoint at the correct place */
1844 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
1845 USB_ENDPOINT_XFER_ISOC
) {
1846 em28xx_err(DRIVER_NAME
" probing error: endpoint is non-ISO endpoint!\n");
1847 em28xx_devused
&=~(1<<nr
);
1850 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1851 em28xx_err(DRIVER_NAME
" probing error: endpoint is ISO OUT endpoint!\n");
1852 em28xx_devused
&=~(1<<nr
);
1856 if (nr
>= EM28XX_MAXBOARDS
) {
1857 printk (DRIVER_NAME
": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS
);
1858 em28xx_devused
&=~(1<<nr
);
1862 /* allocate memory for our device state and initialize it */
1863 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1865 em28xx_err(DRIVER_NAME
": out of memory!\n");
1866 em28xx_devused
&=~(1<<nr
);
1870 snprintf(dev
->name
, 29, "em28xx #%d", nr
);
1872 dev
->model
= id
->driver_info
;
1874 /* compute alternate max packet sizes */
1875 uif
= udev
->actconfig
->interface
[0];
1877 dev
->num_alt
=uif
->num_altsetting
;
1878 em28xx_info("Alternate settings: %i\n",dev
->num_alt
);
1879 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1880 dev
->alt_max_pkt_size
= kmalloc(32*
1881 dev
->num_alt
,GFP_KERNEL
);
1882 if (dev
->alt_max_pkt_size
== NULL
) {
1883 em28xx_errdev("out of memory!\n");
1884 em28xx_devused
&=~(1<<nr
);
1889 for (i
= 0; i
< dev
->num_alt
; i
++) {
1890 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1892 dev
->alt_max_pkt_size
[i
] =
1893 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1894 em28xx_info("Alternate setting %i, max size= %i\n",i
,
1895 dev
->alt_max_pkt_size
[i
]);
1898 if ((card
[nr
]>=0)&&(card
[nr
]<em28xx_bcount
))
1899 dev
->model
= card
[nr
];
1901 /* allocate device struct */
1902 retval
= em28xx_init_dev(&dev
, udev
, nr
);
1906 em28xx_info("Found %s\n", em28xx_boards
[dev
->model
].name
);
1908 /* save our data pointer in this interface device */
1909 usb_set_intfdata(interface
, dev
);
1914 * em28xx_usb_disconnect()
1915 * called when the device gets diconencted
1916 * video device will be unregistered on v4l2_close in case it is still open
1918 static void em28xx_usb_disconnect(struct usb_interface
*interface
)
1922 dev
= usb_get_intfdata(interface
);
1923 usb_set_intfdata(interface
, NULL
);
1928 em28xx_info("disconnecting %s\n", dev
->vdev
->name
);
1930 /* wait until all current v4l2 io is finished then deallocate resources */
1931 mutex_lock(&dev
->lock
);
1933 wake_up_interruptible_all(&dev
->open
);
1937 ("device /dev/video%d is open! Deregistration and memory "
1938 "deallocation are deferred on close.\n",
1939 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
1941 dev
->state
|= DEV_MISCONFIGURED
;
1942 em28xx_uninit_isoc(dev
);
1943 dev
->state
|= DEV_DISCONNECTED
;
1944 wake_up_interruptible(&dev
->wait_frame
);
1945 wake_up_interruptible(&dev
->wait_stream
);
1947 dev
->state
|= DEV_DISCONNECTED
;
1948 em28xx_release_resources(dev
);
1952 mutex_unlock(&dev
->lock
);
1955 kfree(dev
->alt_max_pkt_size
);
1961 static struct usb_driver em28xx_usb_driver
= {
1963 .probe
= em28xx_usb_probe
,
1964 .disconnect
= em28xx_usb_disconnect
,
1965 .id_table
= em28xx_id_table
,
1968 static int __init
em28xx_module_init(void)
1972 printk(KERN_INFO DRIVER_NAME
" v4l2 driver version %d.%d.%d loaded\n",
1973 (EM28XX_VERSION_CODE
>> 16) & 0xff,
1974 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
1976 printk(KERN_INFO DRIVER_NAME
" snapshot date %04d-%02d-%02d\n",
1977 SNAPSHOT
/ 10000, (SNAPSHOT
/ 100) % 100, SNAPSHOT
% 100);
1980 /* register this driver with the USB subsystem */
1981 result
= usb_register(&em28xx_usb_driver
);
1983 em28xx_err(DRIVER_NAME
1984 " usb_register failed. Error number %d.\n", result
);
1989 static void __exit
em28xx_module_exit(void)
1991 /* deregister this driver with the USB subsystem */
1992 usb_deregister(&em28xx_usb_driver
);
1995 module_init(em28xx_module_init
);
1996 module_exit(em28xx_module_exit
);