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 module_param_array(card
, int, NULL
, 0444);
66 module_param_array(video_nr
, int, NULL
, 0444);
67 module_param_array(vbi_nr
, int, NULL
, 0444);
68 MODULE_PARM_DESC(card
,"card type");
69 MODULE_PARM_DESC(video_nr
,"video device numbers");
70 MODULE_PARM_DESC(vbi_nr
,"vbi device numbers");
72 static unsigned int video_debug
= 0;
73 module_param(video_debug
,int,0644);
74 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
76 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
77 static unsigned long em28xx_devused
;
79 /* supported controls */
80 /* Common to all boards */
81 static struct v4l2_queryctrl em28xx_qctrl
[] = {
83 .id
= V4L2_CID_AUDIO_VOLUME
,
84 .type
= V4L2_CTRL_TYPE_INTEGER
,
89 .default_value
= 0x1f,
92 .id
= V4L2_CID_AUDIO_MUTE
,
93 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
103 static struct usb_driver em28xx_usb_driver
;
106 /********************* v4l2 interface ******************************************/
110 * inits registers with sane defaults
112 static int em28xx_config(struct em28xx
*dev
)
115 /* Sets I2C speed to 100 KHz */
117 em28xx_write_regs_req(dev
, 0x00, 0x06, "\x40", 1);
119 /* enable vbi capturing */
121 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
122 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
123 em28xx_write_regs_req(dev
,0x00,0x11,"\x51",1);
125 em28xx_audio_usb_mute(dev
, 1);
126 dev
->mute
= 1; /* maybe not the right place... */
128 em28xx_audio_analog_set(dev
);
129 em28xx_audio_analog_setup(dev
);
130 em28xx_outfmt_set_yuv422(dev
);
131 em28xx_colorlevels_set_default(dev
);
132 em28xx_compression_disable(dev
);
138 * em28xx_config_i2c()
139 * configure i2c attached devices
141 static void em28xx_config_i2c(struct em28xx
*dev
)
143 struct v4l2_routing route
;
145 route
.input
= INPUT(dev
->ctl_input
)->vmux
;
147 em28xx_i2c_call_clients(dev
, VIDIOC_INT_RESET
, NULL
);
148 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
149 em28xx_i2c_call_clients(dev
, VIDIOC_STREAMON
, NULL
);
153 * em28xx_empty_framequeues()
154 * prepare queues for incoming and outgoing frames
156 static void em28xx_empty_framequeues(struct em28xx
*dev
)
160 INIT_LIST_HEAD(&dev
->inqueue
);
161 INIT_LIST_HEAD(&dev
->outqueue
);
163 for (i
= 0; i
< EM28XX_NUM_FRAMES
; i
++) {
164 dev
->frame
[i
].state
= F_UNUSED
;
165 dev
->frame
[i
].buf
.bytesused
= 0;
169 static void video_mux(struct em28xx
*dev
, int index
)
172 struct v4l2_routing route
;
174 route
.input
= INPUT(index
)->vmux
;
176 dev
->ctl_input
= index
;
177 dev
->ctl_ainput
= INPUT(index
)->amux
;
179 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
181 if (dev
->has_msp34xx
) {
183 em28xx_i2c_call_clients(dev
, VIDIOC_INT_I2S_CLOCK_FREQ
, &dev
->i2s_speed
);
184 route
.input
= dev
->ctl_ainput
;
185 route
.output
= MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
);
186 /* Note: this is msp3400 specific */
187 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_AUDIO_ROUTING
, &route
);
188 ainput
= EM28XX_AUDIO_SRC_TUNER
;
189 em28xx_audio_source(dev
, ainput
);
191 switch (dev
->ctl_ainput
) {
193 ainput
= EM28XX_AUDIO_SRC_TUNER
;
196 ainput
= EM28XX_AUDIO_SRC_LINE
;
198 em28xx_audio_source(dev
, ainput
);
202 /* Usage lock check functions */
203 static int res_get(struct em28xx_fh
*fh
)
205 struct em28xx
*dev
= fh
->dev
;
208 /* This instance already has stream_on */
212 mutex_lock(&dev
->lock
);
221 mutex_unlock(&dev
->lock
);
225 static int res_check(struct em28xx_fh
*fh
)
227 return (fh
->stream_on
);
230 static void res_free(struct em28xx_fh
*fh
)
232 struct em28xx
*dev
= fh
->dev
;
234 mutex_lock(&dev
->lock
);
237 mutex_unlock(&dev
->lock
);
243 static void em28xx_vm_open(struct vm_area_struct
*vma
)
245 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
252 static void em28xx_vm_close(struct vm_area_struct
*vma
)
254 /* NOTE: buffers are not freed here */
255 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
257 if (f
->vma_use_count
)
261 static struct vm_operations_struct em28xx_vm_ops
= {
262 .open
= em28xx_vm_open
,
263 .close
= em28xx_vm_close
,
269 * return the current saturation, brightness or contrast, mute state
271 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
274 case V4L2_CID_AUDIO_MUTE
:
275 ctrl
->value
= dev
->mute
;
277 case V4L2_CID_AUDIO_VOLUME
:
278 ctrl
->value
= dev
->volume
;
287 * mute or set new saturation, brightness or contrast
289 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
292 case V4L2_CID_AUDIO_MUTE
:
293 if (ctrl
->value
!= dev
->mute
) {
294 dev
->mute
= ctrl
->value
;
295 em28xx_audio_usb_mute(dev
, ctrl
->value
);
296 return em28xx_audio_analog_set(dev
);
299 case V4L2_CID_AUDIO_VOLUME
:
300 dev
->volume
= ctrl
->value
;
301 return em28xx_audio_analog_set(dev
);
308 * em28xx_stream_interrupt()
311 static int em28xx_stream_interrupt(struct em28xx
*dev
)
315 /* stop reading from the device */
317 dev
->stream
= STREAM_INTERRUPT
;
318 rc
= wait_event_timeout(dev
->wait_stream
,
319 (dev
->stream
== STREAM_OFF
) ||
320 (dev
->state
& DEV_DISCONNECTED
),
324 dev
->state
|= DEV_MISCONFIGURED
;
325 em28xx_videodbg("device is misconfigured; close and "
326 "open /dev/video%d again\n",
327 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
335 static int check_dev(struct em28xx
*dev
)
337 if (dev
->state
& DEV_DISCONNECTED
) {
338 em28xx_errdev("v4l2 ioctl: device not present\n");
342 if (dev
->state
& DEV_MISCONFIGURED
) {
343 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
344 "close and open it again\n");
350 static void get_scale(struct em28xx
*dev
,
351 unsigned int width
, unsigned int height
,
352 unsigned int *hscale
, unsigned int *vscale
)
354 unsigned int maxw
= norm_maxw(dev
);
355 unsigned int maxh
= norm_maxh(dev
);
357 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
358 if (*hscale
>= 0x4000)
361 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
362 if (*vscale
>= 0x4000)
366 /* ------------------------------------------------------------------
367 IOCTL vidioc handling
368 ------------------------------------------------------------------*/
370 static int vidioc_g_fmt_cap(struct file
*file
, void *priv
,
371 struct v4l2_format
*f
)
373 struct em28xx_fh
*fh
= priv
;
374 struct em28xx
*dev
= fh
->dev
;
376 mutex_lock(&dev
->lock
);
378 f
->fmt
.pix
.width
= dev
->width
;
379 f
->fmt
.pix
.height
= dev
->height
;
380 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
381 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
382 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
383 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
385 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
386 f
->fmt
.pix
.field
= dev
->interlaced
?
387 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
389 mutex_unlock(&dev
->lock
);
393 static int vidioc_try_fmt_cap(struct file
*file
, void *priv
,
394 struct v4l2_format
*f
)
396 struct em28xx_fh
*fh
= priv
;
397 struct em28xx
*dev
= fh
->dev
;
398 int width
= f
->fmt
.pix
.width
;
399 int height
= f
->fmt
.pix
.height
;
400 unsigned int maxw
= norm_maxw(dev
);
401 unsigned int maxh
= norm_maxh(dev
);
402 unsigned int hscale
, vscale
;
404 /* width must even because of the YUYV format
405 height must be even because of interlacing */
418 mutex_lock(&dev
->lock
);
420 if (dev
->is_em2800
) {
421 /* the em2800 can only scale down to 50% */
422 if (height
% (maxh
/ 2))
424 if (width
% (maxw
/ 2))
426 /* according to empiatech support */
427 /* the MaxPacketSize is to small to support */
428 /* framesizes larger than 640x480 @ 30 fps */
429 /* or 640x576 @ 25 fps. As this would cut */
430 /* of a part of the image we prefer */
431 /* 360x576 or 360x480 for now */
432 if (width
== maxw
&& height
== maxh
)
436 get_scale(dev
, width
, height
, &hscale
, &vscale
);
438 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
439 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
441 f
->fmt
.pix
.width
= width
;
442 f
->fmt
.pix
.height
= height
;
443 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
444 f
->fmt
.pix
.bytesperline
= width
* 2;
445 f
->fmt
.pix
.sizeimage
= width
* 2 * height
;
446 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
447 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
449 mutex_unlock(&dev
->lock
);
453 static int vidioc_s_fmt_cap(struct file
*file
, void *priv
,
454 struct v4l2_format
*f
)
456 struct em28xx_fh
*fh
= priv
;
457 struct em28xx
*dev
= fh
->dev
;
464 vidioc_try_fmt_cap(file
, priv
, f
);
466 mutex_lock(&dev
->lock
);
468 for (i
= 0; i
< dev
->num_frames
; i
++)
469 if (dev
->frame
[i
].vma_use_count
) {
470 em28xx_videodbg("VIDIOC_S_FMT failed. "
471 "Unmap the buffers first.\n");
476 /* stop io in case it is already in progress */
477 if (dev
->stream
== STREAM_ON
) {
478 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
479 rc
= em28xx_stream_interrupt(dev
);
484 em28xx_release_buffers(dev
);
487 /* set new image size */
488 dev
->width
= f
->fmt
.pix
.width
;
489 dev
->height
= f
->fmt
.pix
.height
;
490 dev
->frame_size
= dev
->width
* dev
->height
* 2;
491 dev
->field_size
= dev
->frame_size
>> 1;
492 dev
->bytesperline
= dev
->width
* 2;
493 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
495 /* FIXME: This is really weird! Why capture is starting with
498 em28xx_uninit_isoc(dev
);
499 em28xx_set_alternate(dev
);
500 em28xx_capture_start(dev
, 1);
501 em28xx_resolution_set(dev
);
502 em28xx_init_isoc(dev
);
506 mutex_unlock(&dev
->lock
);
510 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
512 struct em28xx_fh
*fh
= priv
;
513 struct em28xx
*dev
= fh
->dev
;
514 struct v4l2_format f
;
521 mutex_lock(&dev
->lock
);
523 mutex_unlock(&dev
->lock
);
525 /* Adjusts width/height, if needed */
526 f
.fmt
.pix
.width
= dev
->width
;
527 f
.fmt
.pix
.height
= dev
->height
;
528 vidioc_try_fmt_cap(file
, priv
, &f
);
530 mutex_lock(&dev
->lock
);
532 /* set new image size */
533 dev
->width
= f
.fmt
.pix
.width
;
534 dev
->height
= f
.fmt
.pix
.height
;
535 dev
->frame_size
= dev
->width
* dev
->height
* 2;
536 dev
->field_size
= dev
->frame_size
>> 1;
537 dev
->bytesperline
= dev
->width
* 2;
538 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
540 em28xx_resolution_set(dev
);
541 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->norm
);
543 mutex_unlock(&dev
->lock
);
547 static const char *iname
[] = {
548 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
549 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
550 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
551 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
552 [EM28XX_VMUX_SVIDEO
] = "S-Video",
553 [EM28XX_VMUX_TELEVISION
] = "Television",
554 [EM28XX_VMUX_CABLE
] = "Cable TV",
555 [EM28XX_VMUX_DVB
] = "DVB",
556 [EM28XX_VMUX_DEBUG
] = "for debug only",
559 static int vidioc_enum_input(struct file
*file
, void *priv
,
560 struct v4l2_input
*i
)
562 struct em28xx_fh
*fh
= priv
;
563 struct em28xx
*dev
= fh
->dev
;
567 if (n
>= MAX_EM28XX_INPUT
)
569 if (0 == INPUT(n
)->type
)
573 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
575 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
577 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
578 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
579 i
->type
= V4L2_INPUT_TYPE_TUNER
;
581 i
->std
= dev
->vdev
->tvnorms
;
586 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
588 struct em28xx_fh
*fh
= priv
;
589 struct em28xx
*dev
= fh
->dev
;
596 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
598 struct em28xx_fh
*fh
= priv
;
599 struct em28xx
*dev
= fh
->dev
;
606 if (i
>= MAX_EM28XX_INPUT
)
608 if (0 == INPUT(i
)->type
)
611 mutex_lock(&dev
->lock
);
615 mutex_unlock(&dev
->lock
);
619 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
621 struct em28xx_fh
*fh
= priv
;
622 struct em28xx
*dev
= fh
->dev
;
623 unsigned int index
= a
->index
;
628 index
= dev
->ctl_ainput
;
631 strcpy(a
->name
, "Television");
633 strcpy(a
->name
, "Line In");
635 a
->capability
= V4L2_AUDCAP_STEREO
;
641 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
643 struct em28xx_fh
*fh
= priv
;
644 struct em28xx
*dev
= fh
->dev
;
646 if (a
->index
!= dev
->ctl_ainput
)
652 static int vidioc_queryctrl(struct file
*file
, void *priv
,
653 struct v4l2_queryctrl
*qc
)
655 struct em28xx_fh
*fh
= priv
;
656 struct em28xx
*dev
= fh
->dev
;
665 memset(qc
, 0, sizeof(*qc
));
669 if (!dev
->has_msp34xx
) {
670 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
671 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
672 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
677 mutex_lock(&dev
->lock
);
678 em28xx_i2c_call_clients(dev
, VIDIOC_QUERYCTRL
, qc
);
679 mutex_unlock(&dev
->lock
);
687 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
688 struct v4l2_control
*ctrl
)
690 struct em28xx_fh
*fh
= priv
;
691 struct em28xx
*dev
= fh
->dev
;
697 mutex_lock(&dev
->lock
);
699 if (!dev
->has_msp34xx
)
700 rc
= em28xx_get_ctrl(dev
, ctrl
);
705 em28xx_i2c_call_clients(dev
, VIDIOC_G_CTRL
, ctrl
);
709 mutex_unlock(&dev
->lock
);
713 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
714 struct v4l2_control
*ctrl
)
716 struct em28xx_fh
*fh
= priv
;
717 struct em28xx
*dev
= fh
->dev
;
725 mutex_lock(&dev
->lock
);
727 if (dev
->has_msp34xx
)
728 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
731 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
732 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
733 if (ctrl
->value
< em28xx_qctrl
[i
].minimum
||
734 ctrl
->value
> em28xx_qctrl
[i
].maximum
) {
739 rc
= em28xx_set_ctrl(dev
, ctrl
);
745 /* Control not found - try to send it to the attached devices */
747 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
751 mutex_unlock(&dev
->lock
);
755 static int vidioc_g_tuner(struct file
*file
, void *priv
,
756 struct v4l2_tuner
*t
)
758 struct em28xx_fh
*fh
= priv
;
759 struct em28xx
*dev
= fh
->dev
;
769 strcpy(t
->name
, "Tuner");
771 mutex_lock(&dev
->lock
);
773 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
775 mutex_unlock(&dev
->lock
);
779 static int vidioc_s_tuner(struct file
*file
, void *priv
,
780 struct v4l2_tuner
*t
)
782 struct em28xx_fh
*fh
= priv
;
783 struct em28xx
*dev
= fh
->dev
;
793 mutex_lock(&dev
->lock
);
795 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
797 mutex_unlock(&dev
->lock
);
801 static int vidioc_g_frequency(struct file
*file
, void *priv
,
802 struct v4l2_frequency
*f
)
804 struct em28xx_fh
*fh
= priv
;
805 struct em28xx
*dev
= fh
->dev
;
807 f
->type
= V4L2_TUNER_ANALOG_TV
;
808 f
->frequency
= dev
->ctl_freq
;
813 static int vidioc_s_frequency(struct file
*file
, void *priv
,
814 struct v4l2_frequency
*f
)
816 struct em28xx_fh
*fh
= priv
;
817 struct em28xx
*dev
= fh
->dev
;
827 if (V4L2_TUNER_ANALOG_TV
!= f
->type
)
830 mutex_lock(&dev
->lock
);
832 dev
->ctl_freq
= f
->frequency
;
833 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
835 mutex_unlock(&dev
->lock
);
839 static int vidioc_cropcap(struct file
*file
, void *priv
,
840 struct v4l2_cropcap
*cc
)
842 struct em28xx_fh
*fh
= priv
;
843 struct em28xx
*dev
= fh
->dev
;
845 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
850 cc
->bounds
.width
= dev
->width
;
851 cc
->bounds
.height
= dev
->height
;
852 cc
->defrect
= cc
->bounds
;
853 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
854 cc
->pixelaspect
.denominator
= 59;
859 static int vidioc_streamon(struct file
*file
, void *priv
,
860 enum v4l2_buf_type type
)
862 struct em28xx_fh
*fh
= priv
;
863 struct em28xx
*dev
= fh
->dev
;
870 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
873 if (list_empty(&dev
->inqueue
))
876 mutex_lock(&dev
->lock
);
878 if (unlikely(res_get(fh
) < 0)) {
879 mutex_unlock(&dev
->lock
);
883 dev
->stream
= STREAM_ON
; /* FIXME: Start video capture here? */
885 mutex_unlock(&dev
->lock
);
889 static int vidioc_streamoff(struct file
*file
, void *priv
,
890 enum v4l2_buf_type type
)
892 struct em28xx_fh
*fh
= priv
;
893 struct em28xx
*dev
= fh
->dev
;
900 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
903 mutex_lock(&dev
->lock
);
905 if (dev
->stream
== STREAM_ON
) {
906 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
907 rc
= em28xx_stream_interrupt(dev
);
909 mutex_unlock(&dev
->lock
);
914 em28xx_empty_framequeues(dev
);
916 mutex_unlock(&dev
->lock
);
920 static int vidioc_querycap(struct file
*file
, void *priv
,
921 struct v4l2_capability
*cap
)
923 struct em28xx_fh
*fh
= priv
;
924 struct em28xx
*dev
= fh
->dev
;
926 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
927 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
928 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
, sizeof(cap
->bus_info
));
930 cap
->version
= EM28XX_VERSION_CODE
;
933 V4L2_CAP_SLICED_VBI_CAPTURE
|
934 V4L2_CAP_VIDEO_CAPTURE
|
936 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
939 cap
->capabilities
|= V4L2_CAP_TUNER
;
944 static int vidioc_enum_fmt_cap(struct file
*file
, void *priv
,
945 struct v4l2_fmtdesc
*fmtd
)
947 if (fmtd
->index
!= 0)
950 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
951 strcpy(fmtd
->description
, "Packed YUY2");
952 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
953 memset(fmtd
->reserved
, 0, sizeof(fmtd
->reserved
));
958 /* Sliced VBI ioctls */
959 static int vidioc_g_fmt_vbi_capture(struct file
*file
, void *priv
,
960 struct v4l2_format
*f
)
962 struct em28xx_fh
*fh
= priv
;
963 struct em28xx
*dev
= fh
->dev
;
970 mutex_lock(&dev
->lock
);
972 f
->fmt
.sliced
.service_set
= 0;
974 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
976 if (f
->fmt
.sliced
.service_set
== 0)
979 mutex_unlock(&dev
->lock
);
983 static int vidioc_try_set_vbi_capture(struct file
*file
, void *priv
,
984 struct v4l2_format
*f
)
986 struct em28xx_fh
*fh
= priv
;
987 struct em28xx
*dev
= fh
->dev
;
994 mutex_lock(&dev
->lock
);
995 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
996 mutex_unlock(&dev
->lock
);
998 if (f
->fmt
.sliced
.service_set
== 0)
1005 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1006 struct v4l2_requestbuffers
*rb
)
1008 struct em28xx_fh
*fh
= priv
;
1009 struct em28xx
*dev
= fh
->dev
;
1013 rc
= check_dev(dev
);
1017 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1018 rb
->memory
!= V4L2_MEMORY_MMAP
)
1021 if (dev
->io
== IO_READ
) {
1022 em28xx_videodbg("method is set to read;"
1023 " close and open the device again to"
1024 " choose the mmap I/O method\n");
1028 for (i
= 0; i
< dev
->num_frames
; i
++)
1029 if (dev
->frame
[i
].vma_use_count
) {
1030 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1031 "previous buffers are still mapped\n");
1035 mutex_lock(&dev
->lock
);
1037 if (dev
->stream
== STREAM_ON
) {
1038 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1039 rc
= em28xx_stream_interrupt(dev
);
1041 mutex_unlock(&dev
->lock
);
1046 em28xx_empty_framequeues(dev
);
1048 em28xx_release_buffers(dev
);
1050 rb
->count
= em28xx_request_buffers(dev
, rb
->count
);
1052 dev
->frame_current
= NULL
;
1053 dev
->io
= rb
->count
? IO_MMAP
: IO_NONE
;
1055 mutex_unlock(&dev
->lock
);
1059 static int vidioc_querybuf(struct file
*file
, void *priv
,
1060 struct v4l2_buffer
*b
)
1062 struct em28xx_fh
*fh
= priv
;
1063 struct em28xx
*dev
= fh
->dev
;
1066 rc
= check_dev(dev
);
1070 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1071 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
)
1074 mutex_lock(&dev
->lock
);
1076 memcpy(b
, &dev
->frame
[b
->index
].buf
, sizeof(*b
));
1078 if (dev
->frame
[b
->index
].vma_use_count
)
1079 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1081 if (dev
->frame
[b
->index
].state
== F_DONE
)
1082 b
->flags
|= V4L2_BUF_FLAG_DONE
;
1083 else if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1084 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1086 mutex_unlock(&dev
->lock
);
1090 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1092 struct em28xx_fh
*fh
= priv
;
1093 struct em28xx
*dev
= fh
->dev
;
1094 unsigned long lock_flags
;
1097 rc
= check_dev(dev
);
1101 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
||
1102 b
->index
>= dev
->num_frames
)
1105 if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1108 dev
->frame
[b
->index
].state
= F_QUEUED
;
1110 /* add frame to fifo */
1111 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1112 list_add_tail(&dev
->frame
[b
->index
].frame
, &dev
->inqueue
);
1113 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1118 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1120 struct em28xx_fh
*fh
= priv
;
1121 struct em28xx
*dev
= fh
->dev
;
1123 struct em28xx_frame_t
*f
;
1124 unsigned long lock_flags
;
1126 rc
= check_dev(dev
);
1130 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| dev
->io
!= IO_MMAP
)
1133 if (list_empty(&dev
->outqueue
)) {
1134 if (dev
->stream
== STREAM_OFF
)
1137 if (file
->f_flags
& O_NONBLOCK
)
1140 rc
= wait_event_interruptible(dev
->wait_frame
,
1141 (!list_empty(&dev
->outqueue
)) ||
1142 (dev
->state
& DEV_DISCONNECTED
));
1146 if (dev
->state
& DEV_DISCONNECTED
)
1150 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1151 f
= list_entry(dev
->outqueue
.next
, struct em28xx_frame_t
, frame
);
1152 list_del(dev
->outqueue
.next
);
1153 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1155 f
->state
= F_UNUSED
;
1156 memcpy(b
, &f
->buf
, sizeof(*b
));
1158 if (f
->vma_use_count
)
1159 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1165 * em28xx_v4l2_open()
1166 * inits the device and starts isoc transfer
1168 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
1170 int minor
= iminor(inode
);
1172 struct em28xx
*h
,*dev
= NULL
;
1173 struct em28xx_fh
*fh
;
1175 list_for_each_entry(h
, &em28xx_devlist
, devlist
) {
1176 if (h
->vdev
->minor
== minor
) {
1178 dev
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1180 if (h
->vbi_dev
->minor
== minor
) {
1182 dev
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1188 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1189 minor
,v4l2_type_names
[dev
->type
],dev
->users
);
1191 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1194 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1197 mutex_lock(&dev
->lock
);
1199 filp
->private_data
= fh
;
1201 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1202 em28xx_set_alternate(dev
);
1204 dev
->width
= norm_maxw(dev
);
1205 dev
->height
= norm_maxh(dev
);
1206 dev
->frame_size
= dev
->width
* dev
->height
* 2;
1207 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1208 dev
->bytesperline
= dev
->width
* 2;
1212 em28xx_capture_start(dev
, 1);
1213 em28xx_resolution_set(dev
);
1216 /* start the transfer */
1217 errCode
= em28xx_init_isoc(dev
);
1221 em28xx_empty_framequeues(dev
);
1227 mutex_unlock(&dev
->lock
);
1232 * em28xx_realease_resources()
1233 * unregisters the v4l2,i2c and usb devices
1234 * called when the device gets disconected or at module unload
1236 static void em28xx_release_resources(struct em28xx
*dev
)
1239 /*FIXME: I2C IR should be disconnected */
1241 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1242 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1243 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1244 list_del(&dev
->devlist
);
1245 video_unregister_device(dev
->vdev
);
1246 video_unregister_device(dev
->vbi_dev
);
1247 em28xx_i2c_unregister(dev
);
1248 usb_put_dev(dev
->udev
);
1251 /* Mark device as unused */
1252 em28xx_devused
&=~(1<<dev
->devno
);
1256 * em28xx_v4l2_close()
1257 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1259 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
1261 struct em28xx_fh
*fh
= filp
->private_data
;
1262 struct em28xx
*dev
= fh
->dev
;
1265 em28xx_videodbg("users=%d\n", dev
->users
);
1271 mutex_lock(&dev
->lock
);
1273 if (dev
->users
== 1) {
1274 em28xx_uninit_isoc(dev
);
1275 em28xx_release_buffers(dev
);
1278 /* the device is already disconnect,
1279 free the remaining resources */
1280 if (dev
->state
& DEV_DISCONNECTED
) {
1281 em28xx_release_resources(dev
);
1282 mutex_unlock(&dev
->lock
);
1287 /* set alternate 0 */
1289 em28xx_videodbg("setting alternate 0\n");
1290 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1292 em28xx_errdev("cannot change alternate number to "
1293 "0 (error=%i)\n", errCode
);
1298 wake_up_interruptible_nr(&dev
->open
, 1);
1299 mutex_unlock(&dev
->lock
);
1304 * em28xx_v4l2_read()
1305 * will allocate buffers when called for the first time
1308 em28xx_v4l2_read(struct file
*filp
, char __user
* buf
, size_t count
,
1311 struct em28xx_frame_t
*f
, *i
;
1312 unsigned long lock_flags
;
1314 struct em28xx_fh
*fh
= filp
->private_data
;
1315 struct em28xx
*dev
= fh
->dev
;
1317 /* FIXME: read() is not prepared to allow changing the video
1318 resolution while streaming. Seems a bug at em28xx_set_fmt
1321 if (unlikely(res_get(fh
) < 0))
1324 mutex_lock(&dev
->lock
);
1326 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1327 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1329 if (dev
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1330 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1331 em28xx_videodbg("not supported yet! ...\n");
1332 if (copy_to_user(buf
, "", 1)) {
1333 mutex_unlock(&dev
->lock
);
1336 mutex_unlock(&dev
->lock
);
1339 if (dev
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
) {
1340 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1341 em28xx_videodbg("not supported yet! ...\n");
1342 if (copy_to_user(buf
, "", 1)) {
1343 mutex_unlock(&dev
->lock
);
1346 mutex_unlock(&dev
->lock
);
1350 if (dev
->state
& DEV_DISCONNECTED
) {
1351 em28xx_videodbg("device not present\n");
1352 mutex_unlock(&dev
->lock
);
1356 if (dev
->state
& DEV_MISCONFIGURED
) {
1357 em28xx_videodbg("device misconfigured; close and open it again\n");
1358 mutex_unlock(&dev
->lock
);
1362 if (dev
->io
== IO_MMAP
) {
1363 em28xx_videodbg ("IO method is set to mmap; close and open"
1364 " the device again to choose the read method\n");
1365 mutex_unlock(&dev
->lock
);
1369 if (dev
->io
== IO_NONE
) {
1370 if (!em28xx_request_buffers(dev
, EM28XX_NUM_READ_FRAMES
)) {
1371 em28xx_errdev("read failed, not enough memory\n");
1372 mutex_unlock(&dev
->lock
);
1376 dev
->stream
= STREAM_ON
;
1377 em28xx_queue_unusedframes(dev
);
1381 mutex_unlock(&dev
->lock
);
1385 if (list_empty(&dev
->outqueue
)) {
1386 if (filp
->f_flags
& O_NONBLOCK
) {
1387 mutex_unlock(&dev
->lock
);
1390 ret
= wait_event_interruptible
1392 (!list_empty(&dev
->outqueue
)) ||
1393 (dev
->state
& DEV_DISCONNECTED
));
1395 mutex_unlock(&dev
->lock
);
1398 if (dev
->state
& DEV_DISCONNECTED
) {
1399 mutex_unlock(&dev
->lock
);
1402 dev
->video_bytesread
= 0;
1405 f
= list_entry(dev
->outqueue
.prev
, struct em28xx_frame_t
, frame
);
1407 em28xx_queue_unusedframes(dev
);
1409 if (count
> f
->buf
.length
)
1410 count
= f
->buf
.length
;
1412 if ((dev
->video_bytesread
+ count
) > dev
->frame_size
)
1413 count
= dev
->frame_size
- dev
->video_bytesread
;
1415 if (copy_to_user(buf
, f
->bufmem
+dev
->video_bytesread
, count
)) {
1416 em28xx_err("Error while copying to user\n");
1419 dev
->video_bytesread
+= count
;
1421 if (dev
->video_bytesread
== dev
->frame_size
) {
1422 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1423 list_for_each_entry(i
, &dev
->outqueue
, frame
)
1424 i
->state
= F_UNUSED
;
1425 INIT_LIST_HEAD(&dev
->outqueue
);
1426 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1428 em28xx_queue_unusedframes(dev
);
1429 dev
->video_bytesread
= 0;
1434 mutex_unlock(&dev
->lock
);
1440 * em28xx_v4l2_poll()
1441 * will allocate buffers when called for the first time
1443 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
1445 unsigned int mask
= 0;
1446 struct em28xx_fh
*fh
= filp
->private_data
;
1447 struct em28xx
*dev
= fh
->dev
;
1449 if (unlikely(res_get(fh
) < 0))
1452 mutex_lock(&dev
->lock
);
1454 if (dev
->state
& DEV_DISCONNECTED
) {
1455 em28xx_videodbg("device not present\n");
1456 } else if (dev
->state
& DEV_MISCONFIGURED
) {
1457 em28xx_videodbg("device is misconfigured; close and open it again\n");
1459 if (dev
->io
== IO_NONE
) {
1460 if (!em28xx_request_buffers
1461 (dev
, EM28XX_NUM_READ_FRAMES
)) {
1463 ("poll() failed, not enough memory\n");
1466 dev
->stream
= STREAM_ON
;
1470 if (dev
->io
== IO_READ
) {
1471 em28xx_queue_unusedframes(dev
);
1472 poll_wait(filp
, &dev
->wait_frame
, wait
);
1474 if (!list_empty(&dev
->outqueue
))
1475 mask
|= POLLIN
| POLLRDNORM
;
1477 mutex_unlock(&dev
->lock
);
1483 mutex_unlock(&dev
->lock
);
1488 * em28xx_v4l2_mmap()
1490 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1492 struct em28xx_fh
*fh
= filp
->private_data
;
1493 struct em28xx
*dev
= fh
->dev
;
1494 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1495 unsigned long start
= vma
->vm_start
;
1499 if (unlikely(res_get(fh
) < 0))
1502 mutex_lock(&dev
->lock
);
1504 if (dev
->state
& DEV_DISCONNECTED
) {
1505 em28xx_videodbg("mmap: device not present\n");
1506 mutex_unlock(&dev
->lock
);
1510 if (dev
->state
& DEV_MISCONFIGURED
) {
1511 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1513 mutex_unlock(&dev
->lock
);
1517 if (dev
->io
!= IO_MMAP
|| !(vma
->vm_flags
& VM_WRITE
)) {
1518 mutex_unlock(&dev
->lock
);
1522 if (size
> PAGE_ALIGN(dev
->frame
[0].buf
.length
))
1523 size
= PAGE_ALIGN(dev
->frame
[0].buf
.length
);
1525 for (i
= 0; i
< dev
->num_frames
; i
++) {
1526 if ((dev
->frame
[i
].buf
.m
.offset
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
1529 if (i
== dev
->num_frames
) {
1530 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1531 mutex_unlock(&dev
->lock
);
1535 /* VM_IO is eventually going to replace PageReserved altogether */
1536 vma
->vm_flags
|= VM_IO
;
1537 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1539 pos
= dev
->frame
[i
].bufmem
;
1540 while (size
> 0) { /* size is page-aligned */
1541 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1542 em28xx_videodbg("mmap: vm_insert_page failed\n");
1543 mutex_unlock(&dev
->lock
);
1551 vma
->vm_ops
= &em28xx_vm_ops
;
1552 vma
->vm_private_data
= &dev
->frame
[i
];
1554 em28xx_vm_open(vma
);
1555 mutex_unlock(&dev
->lock
);
1559 static const struct file_operations em28xx_v4l_fops
= {
1560 .owner
= THIS_MODULE
,
1561 .open
= em28xx_v4l2_open
,
1562 .release
= em28xx_v4l2_close
,
1563 .read
= em28xx_v4l2_read
,
1564 .poll
= em28xx_v4l2_poll
,
1565 .mmap
= em28xx_v4l2_mmap
,
1566 .ioctl
= video_ioctl2
,
1567 .llseek
= no_llseek
,
1568 .compat_ioctl
= v4l_compat_ioctl32
,
1571 static const struct video_device em28xx_video_template
= {
1572 .fops
= &em28xx_v4l_fops
,
1573 .release
= video_device_release
,
1576 .vidioc_querycap
= vidioc_querycap
,
1577 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
1578 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
1579 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
1580 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
1581 .vidioc_g_audio
= vidioc_g_audio
,
1582 .vidioc_s_audio
= vidioc_s_audio
,
1583 .vidioc_cropcap
= vidioc_cropcap
,
1585 .vidioc_g_fmt_vbi_capture
= vidioc_g_fmt_vbi_capture
,
1586 .vidioc_try_fmt_vbi_capture
= vidioc_try_set_vbi_capture
,
1587 .vidioc_s_fmt_vbi_capture
= vidioc_try_set_vbi_capture
,
1589 .vidioc_reqbufs
= vidioc_reqbufs
,
1590 .vidioc_querybuf
= vidioc_querybuf
,
1591 .vidioc_qbuf
= vidioc_qbuf
,
1592 .vidioc_dqbuf
= vidioc_dqbuf
,
1593 .vidioc_s_std
= vidioc_s_std
,
1594 .vidioc_enum_input
= vidioc_enum_input
,
1595 .vidioc_g_input
= vidioc_g_input
,
1596 .vidioc_s_input
= vidioc_s_input
,
1597 .vidioc_queryctrl
= vidioc_queryctrl
,
1598 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1599 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1600 .vidioc_streamon
= vidioc_streamon
,
1601 .vidioc_streamoff
= vidioc_streamoff
,
1602 .vidioc_g_tuner
= vidioc_g_tuner
,
1603 .vidioc_s_tuner
= vidioc_s_tuner
,
1604 .vidioc_g_frequency
= vidioc_g_frequency
,
1605 .vidioc_s_frequency
= vidioc_s_frequency
,
1607 .tvnorms
= V4L2_STD_ALL
,
1608 .current_norm
= V4L2_STD_PAL
,
1612 /******************************** usb interface *****************************************/
1616 * allocates and inits the device structs, registers i2c bus and v4l device
1618 static int em28xx_init_dev(struct em28xx
**devhandle
, struct usb_device
*udev
,
1621 struct em28xx
*dev
= *devhandle
;
1622 int retval
= -ENOMEM
;
1624 unsigned int maxh
, maxw
;
1627 mutex_init(&dev
->lock
);
1628 spin_lock_init(&dev
->queue_lock
);
1629 init_waitqueue_head(&dev
->open
);
1630 init_waitqueue_head(&dev
->wait_frame
);
1631 init_waitqueue_head(&dev
->wait_stream
);
1633 dev
->em28xx_write_regs
= em28xx_write_regs
;
1634 dev
->em28xx_read_reg
= em28xx_read_reg
;
1635 dev
->em28xx_read_reg_req_len
= em28xx_read_reg_req_len
;
1636 dev
->em28xx_write_regs_req
= em28xx_write_regs_req
;
1637 dev
->em28xx_read_reg_req
= em28xx_read_reg_req
;
1638 dev
->is_em2800
= em28xx_boards
[dev
->model
].is_em2800
;
1640 em28xx_pre_card_setup(dev
);
1642 errCode
= em28xx_config(dev
);
1644 em28xx_errdev("error configuring device\n");
1645 em28xx_devused
&= ~(1<<dev
->devno
);
1650 /* register i2c bus */
1651 em28xx_i2c_register(dev
);
1653 /* Do board specific init and eeprom reading */
1654 em28xx_card_setup(dev
);
1656 /* configure the device */
1657 em28xx_config_i2c(dev
);
1659 /* set default norm */
1660 dev
->norm
= em28xx_video_template
.current_norm
;
1662 maxw
= norm_maxw(dev
);
1663 maxh
= norm_maxh(dev
);
1665 /* set default image size */
1668 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1669 dev
->field_size
= dev
->width
* dev
->height
;
1671 dev
->interlaced
? dev
->field_size
<< 1 : dev
->field_size
;
1672 dev
->bytesperline
= dev
->width
* 2;
1677 errCode
= em28xx_config(dev
);
1679 /* allocate and fill video video_device struct */
1680 dev
->vdev
= video_device_alloc();
1681 if (NULL
== dev
->vdev
) {
1682 em28xx_errdev("cannot allocate video_device.\n");
1683 em28xx_devused
&=~(1<<dev
->devno
);
1687 memcpy(dev
->vdev
, &em28xx_video_template
,
1688 sizeof(em28xx_video_template
));
1689 dev
->vdev
->type
= VID_TYPE_CAPTURE
;
1691 dev
->vdev
->type
|= VID_TYPE_TUNER
;
1692 dev
->vdev
->dev
= &dev
->udev
->dev
;
1693 snprintf(dev
->vdev
->name
, sizeof(dev
->vbi_dev
->name
),
1694 "%s#%d %s", "em28xx", dev
->devno
, "video");
1696 /* Allocate and fill vbi video_device struct */
1697 dev
->vbi_dev
= video_device_alloc();
1698 if (NULL
== dev
->vbi_dev
) {
1699 em28xx_errdev("cannot allocate video_device.\n");
1701 em28xx_devused
&=~(1<<dev
->devno
);
1705 memcpy(dev
->vbi_dev
, &em28xx_video_template
,
1706 sizeof(em28xx_video_template
));
1707 dev
->vbi_dev
->type
= VFL_TYPE_VBI
;
1708 dev
->vbi_dev
->dev
= &dev
->udev
->dev
;
1709 snprintf(dev
->vbi_dev
->name
, sizeof(dev
->vbi_dev
->name
),
1710 "%s#%d %s", "em28xx", dev
->devno
, "vbi");
1712 list_add_tail(&dev
->devlist
,&em28xx_devlist
);
1714 if (dev
->has_msp34xx
) {
1715 /* Send a reset to other chips via gpio */
1716 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xf7", 1);
1718 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xff", 1);
1724 /* register v4l2 video video_device */
1725 if ((retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
1726 video_nr
[dev
->devno
]))) {
1727 em28xx_errdev("unable to register video device (error=%i).\n",
1729 mutex_unlock(&dev
->lock
);
1730 list_del(&dev
->devlist
);
1731 video_device_release(dev
->vdev
);
1732 em28xx_devused
&=~(1<<dev
->devno
);
1737 /* register v4l2 vbi video_device */
1738 if (video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1739 vbi_nr
[dev
->devno
]) < 0) {
1740 printk("unable to register vbi device\n");
1741 mutex_unlock(&dev
->lock
);
1742 list_del(&dev
->devlist
);
1743 video_device_release(dev
->vbi_dev
);
1744 video_device_release(dev
->vdev
);
1745 em28xx_devused
&=~(1<<dev
->devno
);
1750 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1751 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1752 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1758 * em28xx_usb_probe()
1759 * checks for supported devices
1761 static int em28xx_usb_probe(struct usb_interface
*interface
,
1762 const struct usb_device_id
*id
)
1764 const struct usb_endpoint_descriptor
*endpoint
;
1765 struct usb_device
*udev
;
1766 struct usb_interface
*uif
;
1767 struct em28xx
*dev
= NULL
;
1768 int retval
= -ENODEV
;
1771 udev
= usb_get_dev(interface_to_usbdev(interface
));
1772 ifnum
= interface
->altsetting
[0].desc
.bInterfaceNumber
;
1774 /* Check to see next free device and mark as used */
1775 nr
=find_first_zero_bit(&em28xx_devused
,EM28XX_MAXBOARDS
);
1776 em28xx_devused
|=1<<nr
;
1778 /* Don't register audio interfaces */
1779 if (interface
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
1780 em28xx_err(DRIVER_NAME
" audio device (%04x:%04x): interface %i, class %i\n",
1781 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1783 interface
->altsetting
[0].desc
.bInterfaceClass
);
1785 em28xx_devused
&=~(1<<nr
);
1789 em28xx_err(DRIVER_NAME
" new video device (%04x:%04x): interface %i, class %i\n",
1790 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1792 interface
->altsetting
[0].desc
.bInterfaceClass
);
1794 endpoint
= &interface
->cur_altsetting
->endpoint
[1].desc
;
1796 /* check if the device has the iso in endpoint at the correct place */
1797 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
1798 USB_ENDPOINT_XFER_ISOC
) {
1799 em28xx_err(DRIVER_NAME
" probing error: endpoint is non-ISO endpoint!\n");
1800 em28xx_devused
&=~(1<<nr
);
1803 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1804 em28xx_err(DRIVER_NAME
" probing error: endpoint is ISO OUT endpoint!\n");
1805 em28xx_devused
&=~(1<<nr
);
1809 if (nr
>= EM28XX_MAXBOARDS
) {
1810 printk (DRIVER_NAME
": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS
);
1811 em28xx_devused
&=~(1<<nr
);
1815 /* allocate memory for our device state and initialize it */
1816 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1818 em28xx_err(DRIVER_NAME
": out of memory!\n");
1819 em28xx_devused
&=~(1<<nr
);
1823 snprintf(dev
->name
, 29, "em28xx #%d", nr
);
1825 dev
->model
= id
->driver_info
;
1827 /* compute alternate max packet sizes */
1828 uif
= udev
->actconfig
->interface
[0];
1830 dev
->num_alt
=uif
->num_altsetting
;
1831 em28xx_info("Alternate settings: %i\n",dev
->num_alt
);
1832 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1833 dev
->alt_max_pkt_size
= kmalloc(32*
1834 dev
->num_alt
,GFP_KERNEL
);
1835 if (dev
->alt_max_pkt_size
== NULL
) {
1836 em28xx_errdev("out of memory!\n");
1837 em28xx_devused
&=~(1<<nr
);
1842 for (i
= 0; i
< dev
->num_alt
; i
++) {
1843 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1845 dev
->alt_max_pkt_size
[i
] =
1846 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1847 em28xx_info("Alternate setting %i, max size= %i\n",i
,
1848 dev
->alt_max_pkt_size
[i
]);
1851 if ((card
[nr
]>=0)&&(card
[nr
]<em28xx_bcount
))
1852 dev
->model
= card
[nr
];
1854 /* allocate device struct */
1855 retval
= em28xx_init_dev(&dev
, udev
, nr
);
1859 em28xx_info("Found %s\n", em28xx_boards
[dev
->model
].name
);
1861 /* save our data pointer in this interface device */
1862 usb_set_intfdata(interface
, dev
);
1867 * em28xx_usb_disconnect()
1868 * called when the device gets diconencted
1869 * video device will be unregistered on v4l2_close in case it is still open
1871 static void em28xx_usb_disconnect(struct usb_interface
*interface
)
1875 dev
= usb_get_intfdata(interface
);
1876 usb_set_intfdata(interface
, NULL
);
1881 em28xx_info("disconnecting %s\n", dev
->vdev
->name
);
1883 /* wait until all current v4l2 io is finished then deallocate resources */
1884 mutex_lock(&dev
->lock
);
1886 wake_up_interruptible_all(&dev
->open
);
1890 ("device /dev/video%d is open! Deregistration and memory "
1891 "deallocation are deferred on close.\n",
1892 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
1894 dev
->state
|= DEV_MISCONFIGURED
;
1895 em28xx_uninit_isoc(dev
);
1896 dev
->state
|= DEV_DISCONNECTED
;
1897 wake_up_interruptible(&dev
->wait_frame
);
1898 wake_up_interruptible(&dev
->wait_stream
);
1900 dev
->state
|= DEV_DISCONNECTED
;
1901 em28xx_release_resources(dev
);
1905 mutex_unlock(&dev
->lock
);
1908 kfree(dev
->alt_max_pkt_size
);
1914 static struct usb_driver em28xx_usb_driver
= {
1916 .probe
= em28xx_usb_probe
,
1917 .disconnect
= em28xx_usb_disconnect
,
1918 .id_table
= em28xx_id_table
,
1921 static int __init
em28xx_module_init(void)
1925 printk(KERN_INFO DRIVER_NAME
" v4l2 driver version %d.%d.%d loaded\n",
1926 (EM28XX_VERSION_CODE
>> 16) & 0xff,
1927 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
1929 printk(KERN_INFO DRIVER_NAME
" snapshot date %04d-%02d-%02d\n",
1930 SNAPSHOT
/ 10000, (SNAPSHOT
/ 100) % 100, SNAPSHOT
% 100);
1933 /* register this driver with the USB subsystem */
1934 result
= usb_register(&em28xx_usb_driver
);
1936 em28xx_err(DRIVER_NAME
1937 " usb_register failed. Error number %d.\n", result
);
1942 static void __exit
em28xx_module_exit(void)
1944 /* deregister this driver with the USB subsystem */
1945 usb_deregister(&em28xx_usb_driver
);
1948 module_init(em28xx_module_init
);
1949 module_exit(em28xx_module_exit
);