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@brturbo.com.br>
7 Sascha Sommer <saschasommer@freenet.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/usb.h>
29 #include <linux/i2c.h>
30 #include <linux/version.h>
31 #include <linux/video_decoder.h>
34 #include <media/tuner.h>
36 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
37 "Markus Rechberger <mrechberger@gmail.com>, " \
38 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
39 "Sascha Sommer <saschasommer@freenet.de>"
41 #define DRIVER_NAME "em28xx"
42 #define DRIVER_DESC "Empia em28xx based USB video device driver"
43 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
45 #define em28xx_videodbg(fmt, arg...) do {\
47 printk(KERN_INFO "%s %s :"fmt, \
48 dev->name, __FUNCTION__ , ##arg); } while (0)
50 MODULE_AUTHOR(DRIVER_AUTHOR
);
51 MODULE_DESCRIPTION(DRIVER_DESC
);
52 MODULE_LICENSE("GPL");
54 static LIST_HEAD(em28xx_devlist
);
56 static unsigned int card
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
57 module_param_array(card
, int, NULL
, 0444);
58 MODULE_PARM_DESC(card
,"card type");
60 static int tuner
= -1;
61 module_param(tuner
, int, 0444);
62 MODULE_PARM_DESC(tuner
, "tuner type");
64 static unsigned int video_debug
= 0;
65 module_param(video_debug
,int,0644);
66 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
68 /* supported tv norms */
69 static struct em28xx_tvnorm tvnorms
[] = {
73 .mode
= VIDEO_MODE_PAL
,
77 .mode
= VIDEO_MODE_NTSC
,
81 .mode
= VIDEO_MODE_SECAM
,
85 .mode
= VIDEO_MODE_PAL
,
89 static const unsigned char saa7114_i2c_init
[] = {
90 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
91 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
92 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
93 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
94 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
95 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
96 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
97 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
98 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
99 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
100 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
101 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
102 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
106 #define TVNORMS ARRAY_SIZE(tvnorms)
108 /* supported controls */
109 static struct v4l2_queryctrl em28xx_qctrl
[] = {
111 .id
= V4L2_CID_BRIGHTNESS
,
112 .type
= V4L2_CTRL_TYPE_INTEGER
,
113 .name
= "Brightness",
120 .id
= V4L2_CID_CONTRAST
,
121 .type
= V4L2_CTRL_TYPE_INTEGER
,
126 .default_value
= 0x10,
129 .id
= V4L2_CID_SATURATION
,
130 .type
= V4L2_CTRL_TYPE_INTEGER
,
131 .name
= "Saturation",
135 .default_value
= 0x10,
138 .id
= V4L2_CID_AUDIO_VOLUME
,
139 .type
= V4L2_CTRL_TYPE_INTEGER
,
144 .default_value
= 0x1f,
147 .id
= V4L2_CID_AUDIO_MUTE
,
148 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
156 .id
= V4L2_CID_RED_BALANCE
,
157 .type
= V4L2_CTRL_TYPE_INTEGER
,
158 .name
= "Red chroma balance",
165 .id
= V4L2_CID_BLUE_BALANCE
,
166 .type
= V4L2_CTRL_TYPE_INTEGER
,
167 .name
= "Blue chroma balance",
174 .id
= V4L2_CID_GAMMA
,
175 .type
= V4L2_CTRL_TYPE_INTEGER
,
180 .default_value
= 0x20,
185 static struct usb_driver em28xx_usb_driver
;
187 static DECLARE_MUTEX(em28xx_sysfs_lock
);
188 static DECLARE_RWSEM(em28xx_disconnect
);
190 /********************* v4l2 interface ******************************************/
194 * inits registers with sane defaults
196 static int em28xx_config(struct em28xx
*dev
)
199 /* Sets I2C speed to 100 KHz */
200 em28xx_write_regs_req(dev
, 0x00, 0x06, "\x40", 1);
202 /* enable vbi capturing */
203 em28xx_audio_usb_mute(dev
, 1);
204 dev
->mute
= 1; /* maybe not the right place... */
206 em28xx_audio_analog_set(dev
);
207 em28xx_audio_analog_setup(dev
);
208 em28xx_outfmt_set_yuv422(dev
);
209 em28xx_colorlevels_set_default(dev
);
210 em28xx_compression_disable(dev
);
216 * em28xx_config_i2c()
217 * configure i2c attached devices
219 static void em28xx_config_i2c(struct em28xx
*dev
)
221 struct v4l2_frequency f
;
222 struct video_decoder_init em28xx_vdi
= {.data
= NULL
};
225 /* configure decoder */
226 if(dev
->model
== EM2820_BOARD_MSI_VOX_USB_2
){
227 em28xx_vdi
.data
=saa7114_i2c_init
;
228 em28xx_vdi
.len
=sizeof(saa7114_i2c_init
);
232 em28xx_i2c_call_clients(dev
, DECODER_INIT
, &em28xx_vdi
);
233 em28xx_i2c_call_clients(dev
, DECODER_SET_INPUT
, &dev
->ctl_input
);
234 /* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
235 /* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
236 /* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
237 /* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
239 /* configure tuner */
241 f
.type
= V4L2_TUNER_ANALOG_TV
;
242 f
.frequency
= 9076; /* FIXME:remove magic number */
243 dev
->ctl_freq
= f
.frequency
;
244 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, &f
);
246 /* configure tda9887 */
249 /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
253 * em28xx_empty_framequeues()
254 * prepare queues for incoming and outgoing frames
256 static void em28xx_empty_framequeues(struct em28xx
*dev
)
260 INIT_LIST_HEAD(&dev
->inqueue
);
261 INIT_LIST_HEAD(&dev
->outqueue
);
263 for (i
= 0; i
< EM28XX_NUM_FRAMES
; i
++) {
264 dev
->frame
[i
].state
= F_UNUSED
;
265 dev
->frame
[i
].buf
.bytesused
= 0;
269 static void video_mux(struct em28xx
*dev
, int index
)
273 input
= INPUT(index
)->vmux
;
274 dev
->ctl_input
= index
;
275 dev
->ctl_ainput
= INPUT(index
)->amux
;
277 em28xx_i2c_call_clients(dev
, DECODER_SET_INPUT
, &input
);
280 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index
,input
,dev
->ctl_ainput
);
282 if (dev
->has_msp34xx
) {
283 em28xx_i2c_call_clients(dev
, VIDIOC_S_AUDIO
, &dev
->ctl_ainput
);
284 ainput
= EM28XX_AUDIO_SRC_TUNER
;
285 em28xx_audio_source(dev
, ainput
);
287 switch (dev
->ctl_ainput
) {
289 ainput
= EM28XX_AUDIO_SRC_TUNER
;
292 ainput
= EM28XX_AUDIO_SRC_LINE
;
294 em28xx_audio_source(dev
, ainput
);
300 * inits the device and starts isoc transfer
302 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
304 int minor
= iminor(inode
);
306 struct em28xx
*h
,*dev
= NULL
;
307 struct list_head
*list
;
309 list_for_each(list
,&em28xx_devlist
) {
310 h
= list_entry(list
, struct em28xx
, devlist
);
311 if (h
->vdev
->minor
== minor
) {
316 filp
->private_data
=dev
;
319 em28xx_videodbg("users=%d\n", dev
->users
);
321 if (!down_read_trylock(&em28xx_disconnect
))
325 em28xx_warn("this driver can be opened only once\n");
326 up_read(&em28xx_disconnect
);
330 /* if(dev->vbi_dev->minor == minor){
331 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
333 if (dev
->vdev
->minor
== minor
) {
334 dev
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
337 init_MUTEX(&dev
->fileop_lock
); /* to 1 == available */
338 spin_lock_init(&dev
->queue_lock
);
339 init_waitqueue_head(&dev
->wait_frame
);
340 init_waitqueue_head(&dev
->wait_stream
);
344 em28xx_set_alternate(dev
);
346 dev
->width
= norm_maxw(dev
);
347 dev
->height
= norm_maxh(dev
);
348 dev
->frame_size
= dev
->width
* dev
->height
* 2;
349 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
350 dev
->bytesperline
= dev
->width
* 2;
354 em28xx_capture_start(dev
, 1);
355 em28xx_resolution_set(dev
);
357 /* start the transfer */
358 errCode
= em28xx_init_isoc(dev
);
363 filp
->private_data
= dev
;
365 dev
->stream
= STREAM_OFF
;
369 em28xx_empty_framequeues(dev
);
371 dev
->state
|= DEV_INITIALIZED
;
377 up_read(&em28xx_disconnect
);
382 * em28xx_realease_resources()
383 * unregisters the v4l2,i2c and usb devices
384 * called when the device gets disconected or at module unload
386 static void em28xx_release_resources(struct em28xx
*dev
)
388 down(&em28xx_sysfs_lock
);
390 em28xx_info("V4L2 device /dev/video%d deregistered\n",
392 list_del(&dev
->devlist
);
393 video_unregister_device(dev
->vdev
);
394 /* video_unregister_device(dev->vbi_dev); */
395 em28xx_i2c_unregister(dev
);
396 usb_put_dev(dev
->udev
);
397 up(&em28xx_sysfs_lock
);
401 * em28xx_v4l2_close()
402 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
404 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
407 struct em28xx
*dev
=filp
->private_data
;
409 em28xx_videodbg("users=%d\n", dev
->users
);
413 em28xx_uninit_isoc(dev
);
415 em28xx_release_buffers(dev
);
417 /* the device is already disconnect, free the remaining resources */
418 if (dev
->state
& DEV_DISCONNECTED
) {
419 em28xx_release_resources(dev
);
425 /* set alternate 0 */
427 em28xx_videodbg("setting alternate 0\n");
428 errCode
= usb_set_interface(dev
->udev
, 0, 0);
430 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
435 wake_up_interruptible_nr(&dev
->open
, 1);
442 * will allocate buffers when called for the first time
445 em28xx_v4l2_read(struct file
*filp
, char __user
* buf
, size_t count
,
448 struct em28xx_frame_t
*f
, *i
;
449 unsigned long lock_flags
;
451 struct em28xx
*dev
= filp
->private_data
;
453 if (down_interruptible(&dev
->fileop_lock
))
456 if (dev
->state
& DEV_DISCONNECTED
) {
457 em28xx_videodbg("device not present\n");
458 up(&dev
->fileop_lock
);
462 if (dev
->state
& DEV_MISCONFIGURED
) {
463 em28xx_videodbg("device misconfigured; close and open it again\n");
464 up(&dev
->fileop_lock
);
468 if (dev
->io
== IO_MMAP
) {
469 em28xx_videodbg ("IO method is set to mmap; close and open"
470 " the device again to choose the read method\n");
471 up(&dev
->fileop_lock
);
475 if (dev
->io
== IO_NONE
) {
476 if (!em28xx_request_buffers(dev
, EM28XX_NUM_READ_FRAMES
)) {
477 em28xx_errdev("read failed, not enough memory\n");
478 up(&dev
->fileop_lock
);
482 dev
->stream
= STREAM_ON
;
483 em28xx_queue_unusedframes(dev
);
487 up(&dev
->fileop_lock
);
491 if (list_empty(&dev
->outqueue
)) {
492 if (filp
->f_flags
& O_NONBLOCK
) {
493 up(&dev
->fileop_lock
);
496 ret
= wait_event_interruptible
498 (!list_empty(&dev
->outqueue
)) ||
499 (dev
->state
& DEV_DISCONNECTED
));
501 up(&dev
->fileop_lock
);
504 if (dev
->state
& DEV_DISCONNECTED
) {
505 up(&dev
->fileop_lock
);
510 f
= list_entry(dev
->outqueue
.prev
, struct em28xx_frame_t
, frame
);
512 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
513 list_for_each_entry(i
, &dev
->outqueue
, frame
)
515 INIT_LIST_HEAD(&dev
->outqueue
);
516 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
518 em28xx_queue_unusedframes(dev
);
520 if (count
> f
->buf
.length
)
521 count
= f
->buf
.length
;
523 if (copy_to_user(buf
, f
->bufmem
, count
)) {
524 up(&dev
->fileop_lock
);
529 up(&dev
->fileop_lock
);
536 * will allocate buffers when called for the first time
538 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
540 unsigned int mask
= 0;
541 struct em28xx
*dev
= filp
->private_data
;
543 if (down_interruptible(&dev
->fileop_lock
))
546 if (dev
->state
& DEV_DISCONNECTED
) {
547 em28xx_videodbg("device not present\n");
548 } else if (dev
->state
& DEV_MISCONFIGURED
) {
549 em28xx_videodbg("device is misconfigured; close and open it again\n");
551 if (dev
->io
== IO_NONE
) {
552 if (!em28xx_request_buffers
553 (dev
, EM28XX_NUM_READ_FRAMES
)) {
555 ("poll() failed, not enough memory\n");
558 dev
->stream
= STREAM_ON
;
562 if (dev
->io
== IO_READ
) {
563 em28xx_queue_unusedframes(dev
);
564 poll_wait(filp
, &dev
->wait_frame
, wait
);
566 if (!list_empty(&dev
->outqueue
))
567 mask
|= POLLIN
| POLLRDNORM
;
569 up(&dev
->fileop_lock
);
575 up(&dev
->fileop_lock
);
582 static void em28xx_vm_open(struct vm_area_struct
*vma
)
584 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
591 static void em28xx_vm_close(struct vm_area_struct
*vma
)
593 /* NOTE: buffers are not freed here */
594 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
598 static struct vm_operations_struct em28xx_vm_ops
= {
599 .open
= em28xx_vm_open
,
600 .close
= em28xx_vm_close
,
606 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
608 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
609 start
= vma
->vm_start
;
613 struct em28xx
*dev
= filp
->private_data
;
615 if (down_interruptible(&dev
->fileop_lock
))
618 if (dev
->state
& DEV_DISCONNECTED
) {
619 em28xx_videodbg("mmap: device not present\n");
620 up(&dev
->fileop_lock
);
624 if (dev
->state
& DEV_MISCONFIGURED
) {
625 em28xx_videodbg ("mmap: Device is misconfigured; close and "
627 up(&dev
->fileop_lock
);
631 if (dev
->io
!= IO_MMAP
|| !(vma
->vm_flags
& VM_WRITE
) ||
632 size
!= PAGE_ALIGN(dev
->frame
[0].buf
.length
)) {
633 up(&dev
->fileop_lock
);
637 for (i
= 0; i
< dev
->num_frames
; i
++) {
638 if ((dev
->frame
[i
].buf
.m
.offset
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
641 if (i
== dev
->num_frames
) {
642 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
643 up(&dev
->fileop_lock
);
647 /* VM_IO is eventually going to replace PageReserved altogether */
648 vma
->vm_flags
|= VM_IO
;
649 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
651 pos
= dev
->frame
[i
].bufmem
;
652 while (size
> 0) { /* size is page-aligned */
653 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
654 em28xx_videodbg("mmap: vm_insert_page failed\n");
655 up(&dev
->fileop_lock
);
663 vma
->vm_ops
= &em28xx_vm_ops
;
664 vma
->vm_private_data
= &dev
->frame
[i
];
667 up(&dev
->fileop_lock
);
673 * return the current saturation, brightness or contrast, mute state
675 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
679 case V4L2_CID_AUDIO_MUTE
:
680 ctrl
->value
= dev
->mute
;
682 case V4L2_CID_AUDIO_VOLUME
:
683 ctrl
->value
= dev
->volume
;
685 case V4L2_CID_BRIGHTNESS
:
686 if ((tmp
= em28xx_brightness_get(dev
)) < 0)
688 ctrl
->value
= (s32
) ((s8
) tmp
); /* FIXME: clenaer way to extend sign? */
690 case V4L2_CID_CONTRAST
:
691 if ((ctrl
->value
= em28xx_contrast_get(dev
)) < 0)
694 case V4L2_CID_SATURATION
:
695 if ((ctrl
->value
= em28xx_saturation_get(dev
)) < 0)
698 case V4L2_CID_RED_BALANCE
:
699 if ((tmp
= em28xx_v_balance_get(dev
)) < 0)
701 ctrl
->value
= (s32
) ((s8
) tmp
); /* FIXME: clenaer way to extend sign? */
703 case V4L2_CID_BLUE_BALANCE
:
704 if ((tmp
= em28xx_u_balance_get(dev
)) < 0)
706 ctrl
->value
= (s32
) ((s8
) tmp
); /* FIXME: clenaer way to extend sign? */
709 if ((ctrl
->value
= em28xx_gamma_get(dev
)) < 0)
719 * mute or set new saturation, brightness or contrast
721 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
724 case V4L2_CID_AUDIO_MUTE
:
725 if (ctrl
->value
!= dev
->mute
) {
726 dev
->mute
= ctrl
->value
;
727 em28xx_audio_usb_mute(dev
, ctrl
->value
);
728 return em28xx_audio_analog_set(dev
);
731 case V4L2_CID_AUDIO_VOLUME
:
732 dev
->volume
= ctrl
->value
;
733 return em28xx_audio_analog_set(dev
);
734 case V4L2_CID_BRIGHTNESS
:
735 return em28xx_brightness_set(dev
, ctrl
->value
);
736 case V4L2_CID_CONTRAST
:
737 return em28xx_contrast_set(dev
, ctrl
->value
);
738 case V4L2_CID_SATURATION
:
739 return em28xx_saturation_set(dev
, ctrl
->value
);
740 case V4L2_CID_RED_BALANCE
:
741 return em28xx_v_balance_set(dev
, ctrl
->value
);
742 case V4L2_CID_BLUE_BALANCE
:
743 return em28xx_u_balance_set(dev
, ctrl
->value
);
745 return em28xx_gamma_set(dev
, ctrl
->value
);
752 * em28xx_stream_interrupt()
755 static int em28xx_stream_interrupt(struct em28xx
*dev
)
759 /* stop reading from the device */
761 dev
->stream
= STREAM_INTERRUPT
;
762 ret
= wait_event_timeout(dev
->wait_stream
,
763 (dev
->stream
== STREAM_OFF
) ||
764 (dev
->state
& DEV_DISCONNECTED
),
766 if (dev
->state
& DEV_DISCONNECTED
)
769 dev
->state
|= DEV_MISCONFIGURED
;
770 em28xx_videodbg("device is misconfigured; close and "
771 "open /dev/video%d again\n", dev
->vdev
->minor
);
778 static int em28xx_set_norm(struct em28xx
*dev
, int width
, int height
)
780 unsigned int hscale
, vscale
;
781 unsigned int maxh
, maxw
;
783 maxw
= norm_maxw(dev
);
784 maxh
= norm_maxh(dev
);
786 /* width must even because of the YUYV format */
787 /* height must be even because of interlacing */
800 if ((hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L) >= 0x4000)
802 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
804 if ((vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L) >= 0x4000)
806 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
808 /* set new image size */
810 dev
->height
= height
;
811 dev
->frame_size
= dev
->width
* dev
->height
* 2;
812 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
813 dev
->bytesperline
= dev
->width
* 2;
814 dev
->hscale
= hscale
;
815 dev
->vscale
= vscale
;
817 em28xx_resolution_set(dev
);
823 * em28xx_v4l2_do_ioctl()
824 * This function is _not_ called directly, but from
825 * em28xx_v4l2_ioctl. Userspace
826 * copying is done already, arg is a kernel pointer.
828 static int em28xx_do_ioctl(struct inode
*inode
, struct file
*filp
,
829 struct em28xx
*dev
, unsigned int cmd
, void *arg
,
830 v4l2_kioctl driver_ioctl
)
835 /* ---------- tv norms ---------- */
838 struct v4l2_standard
*e
= arg
;
844 ret
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
845 tvnorms
[e
->index
].name
);
853 v4l2_std_id
*id
= arg
;
855 *id
= dev
->tvnorm
->id
;
860 v4l2_std_id
*id
= arg
;
863 for (i
= 0; i
< TVNORMS
; i
++)
864 if (*id
== tvnorms
[i
].id
)
867 for (i
= 0; i
< TVNORMS
; i
++)
868 if (*id
& tvnorms
[i
].id
)
874 dev
->tvnorm
= &tvnorms
[i
];
876 em28xx_set_norm(dev
, dev
->width
, dev
->height
);
879 dev->width=norm_maxw(dev);
880 dev->height=norm_maxh(dev);
881 dev->frame_size=dev->width*dev->height*2;
882 dev->field_size=dev->frame_size>>1;
883 dev->bytesperline=dev->width*2;
887 em28xx_resolution_set(dev);
890 em28xx_uninit_isoc(dev);
891 em28xx_set_alternate(dev);
892 em28xx_capture_start(dev, 1);
893 em28xx_resolution_set(dev);
894 em28xx_init_isoc(dev);
896 em28xx_i2c_call_clients(dev
, DECODER_SET_NORM
,
898 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
,
906 /* ------ input switching ---------- */
907 case VIDIOC_ENUMINPUT
:
909 struct v4l2_input
*i
= arg
;
911 static const char *iname
[] = {
912 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
913 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
914 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
915 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
916 [EM28XX_VMUX_SVIDEO
] = "S-Video",
917 [EM28XX_VMUX_TELEVISION
] = "Television",
918 [EM28XX_VMUX_CABLE
] = "Cable TV",
919 [EM28XX_VMUX_DVB
] = "DVB",
920 [EM28XX_VMUX_DEBUG
] = "for debug only",
924 if (n
>= MAX_EM28XX_INPUT
)
926 if (0 == INPUT(n
)->type
)
928 memset(i
, 0, sizeof(*i
));
930 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
931 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
932 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
933 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
934 i
->type
= V4L2_INPUT_TYPE_TUNER
;
935 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
936 i
->std
|= tvnorms
[n
].id
;
952 if (*index
>= MAX_EM28XX_INPUT
)
954 if (0 == INPUT(*index
)->type
)
958 video_mux(dev
, *index
);
966 struct v4l2_audio
*a
= arg
;
967 unsigned int index
= a
->index
;
971 memset(a
, 0, sizeof(*a
));
972 index
= dev
->ctl_ainput
;
975 strcpy(a
->name
, "Television");
977 strcpy(a
->name
, "Line In");
979 a
->capability
= V4L2_AUDCAP_STEREO
;
986 struct v4l2_audio
*a
= arg
;
987 if (a
->index
!= dev
->ctl_ainput
)
993 /* --- controls ---------------------------------------------- */
994 case VIDIOC_QUERYCTRL
:
996 struct v4l2_queryctrl
*qc
= arg
;
998 n
= sizeof(em28xx_qctrl
) / sizeof(em28xx_qctrl
[0]);
999 for (i
= 0; i
< n
; i
++)
1000 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1001 memcpy(qc
, &(em28xx_qctrl
[i
]),
1011 struct v4l2_control
*ctrl
= arg
;
1014 return em28xx_get_ctrl(dev
, ctrl
);
1017 case VIDIOC_S_CTRL_OLD
: /* ??? */
1020 struct v4l2_control
*ctrl
= arg
;
1024 n
= sizeof(em28xx_qctrl
) / sizeof(em28xx_qctrl
[0]);
1025 for (i
= 0; i
< n
; i
++)
1026 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
1028 em28xx_qctrl
[i
].minimum
1030 em28xx_qctrl
[i
].maximum
)
1033 return em28xx_set_ctrl(dev
, ctrl
);
1038 /* --- tuner ioctls ------------------------------------------ */
1039 case VIDIOC_G_TUNER
:
1041 struct v4l2_tuner
*t
= arg
;
1047 memset(t
, 0, sizeof(*t
));
1048 strcpy(t
->name
, "Tuner");
1049 t
->type
= V4L2_TUNER_ANALOG_TV
;
1050 t
->capability
= V4L2_TUNER_CAP_NORM
;
1051 t
->rangehigh
= 0xffffffffUL
; /* FIXME: set correct range */
1052 /* t->signal = 0xffff;*/
1053 /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1054 /* No way to get signal strength? */
1056 em28xx_i2c_call_clients(dev
, DECODER_GET_STATUS
,
1060 (status
& DECODER_STATUS_GOOD
) != 0 ? 0xffff : 0;
1062 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t
->signal
,
1066 case VIDIOC_S_TUNER
:
1068 struct v4l2_tuner
*t
= arg
;
1073 memset(t
, 0, sizeof(*t
));
1074 strcpy(t
->name
, "Tuner");
1075 t
->type
= V4L2_TUNER_ANALOG_TV
;
1076 t
->capability
= V4L2_TUNER_CAP_NORM
;
1077 t
->rangehigh
= 0xffffffffUL
; /* FIXME: set correct range */
1078 /* t->signal = 0xffff; */
1079 /* No way to get signal strength? */
1081 em28xx_i2c_call_clients(dev
, DECODER_GET_STATUS
,
1085 (status
& DECODER_STATUS_GOOD
) != 0 ? 0xffff : 0;
1087 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1091 case VIDIOC_G_FREQUENCY
:
1093 struct v4l2_frequency
*f
= arg
;
1095 memset(f
, 0, sizeof(*f
));
1096 f
->type
= V4L2_TUNER_ANALOG_TV
;
1097 f
->frequency
= dev
->ctl_freq
;
1101 case VIDIOC_S_FREQUENCY
:
1103 struct v4l2_frequency
*f
= arg
;
1108 if (V4L2_TUNER_ANALOG_TV
!= f
->type
)
1112 dev
->ctl_freq
= f
->frequency
;
1113 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
1118 case VIDIOC_CROPCAP
:
1120 struct v4l2_cropcap
*cc
= arg
;
1122 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1124 cc
->bounds
.left
= 0;
1126 cc
->bounds
.width
= dev
->width
;
1127 cc
->bounds
.height
= dev
->height
;
1128 cc
->defrect
= cc
->bounds
;
1129 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1130 cc
->pixelaspect
.denominator
= 59;
1133 case VIDIOC_STREAMON
:
1137 if (*type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1138 || dev
->io
!= IO_MMAP
)
1141 if (list_empty(&dev
->inqueue
))
1144 dev
->stream
= STREAM_ON
; /* FIXME: Start video capture here? */
1146 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1150 case VIDIOC_STREAMOFF
:
1155 if (*type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1156 || dev
->io
!= IO_MMAP
)
1159 if (dev
->stream
== STREAM_ON
) {
1160 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1161 if ((ret
= em28xx_stream_interrupt(dev
)))
1164 em28xx_empty_framequeues(dev
);
1169 return v4l_compat_translate_ioctl(inode
, filp
, cmd
, arg
,
1176 * em28xx_v4l2_do_ioctl()
1177 * This function is _not_ called directly, but from
1178 * em28xx_v4l2_ioctl. Userspace
1179 * copying is done already, arg is a kernel pointer.
1181 static int em28xx_video_do_ioctl(struct inode
*inode
, struct file
*filp
,
1182 unsigned int cmd
, void *arg
)
1184 struct em28xx
*dev
= filp
->private_data
;
1189 if (video_debug
> 1)
1190 em28xx_print_ioctl(dev
->name
,cmd
);
1194 /* --- capabilities ------------------------------------------ */
1195 case VIDIOC_QUERYCAP
:
1197 struct v4l2_capability
*cap
= arg
;
1199 memset(cap
, 0, sizeof(*cap
));
1200 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1201 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
,
1203 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
,
1204 sizeof(cap
->bus_info
));
1205 cap
->version
= EM28XX_VERSION_CODE
;
1207 V4L2_CAP_VIDEO_CAPTURE
|
1209 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1211 cap
->capabilities
|= V4L2_CAP_TUNER
;
1215 /* --- capture ioctls ---------------------------------------- */
1216 case VIDIOC_ENUM_FMT
:
1218 struct v4l2_fmtdesc
*fmtd
= arg
;
1220 if (fmtd
->index
!= 0)
1222 memset(fmtd
, 0, sizeof(*fmtd
));
1223 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1224 strcpy(fmtd
->description
, "Packed YUY2");
1225 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
1226 memset(fmtd
->reserved
, 0, sizeof(fmtd
->reserved
));
1232 struct v4l2_format
*format
= arg
;
1234 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
1236 V4L2_BUF_TYPE_VIDEO_CAPTURE
?
1237 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format
->type
==
1238 V4L2_BUF_TYPE_VBI_CAPTURE
?
1239 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1242 if (format
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1245 format
->fmt
.pix
.width
= dev
->width
;
1246 format
->fmt
.pix
.height
= dev
->height
;
1247 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
1248 format
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1249 format
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1250 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1251 format
->fmt
.pix
.field
= dev
->interlaced
? V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1253 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev
->width
,
1258 case VIDIOC_TRY_FMT
:
1261 struct v4l2_format
*format
= arg
;
1264 int width
= format
->fmt
.pix
.width
;
1265 int height
= format
->fmt
.pix
.height
;
1266 unsigned int hscale
, vscale
;
1267 unsigned int maxh
, maxw
;
1269 maxw
= norm_maxw(dev
);
1270 maxh
= norm_maxh(dev
);
1272 /* int both_fields; */
1274 em28xx_videodbg("%s: type=%s\n",
1276 VIDIOC_TRY_FMT
? "VIDIOC_TRY_FMT" :
1279 V4L2_BUF_TYPE_VIDEO_CAPTURE
?
1280 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format
->type
==
1281 V4L2_BUF_TYPE_VBI_CAPTURE
?
1282 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1285 if (format
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1288 em28xx_videodbg("%s: requested %dx%d\n",
1290 VIDIOC_TRY_FMT
? "VIDIOC_TRY_FMT" :
1291 "VIDIOC_S_FMT", format
->fmt
.pix
.width
,
1292 format
->fmt
.pix
.height
);
1294 /* FIXME: Move some code away from here */
1295 /* width must even because of the YUYV format */
1296 /* height must be even because of interlacing */
1310 /* the em2800 can only scale down to 50% */
1311 if(height
% (maxh
/ 2))
1313 if(width
% (maxw
/ 2))
1315 /* according to empiatech support */
1316 /* the MaxPacketSize is to small to support */
1317 /* framesizes larger than 640x480 @ 30 fps */
1318 /* or 640x576 @ 25 fps. As this would cut */
1319 /* of a part of the image we prefer */
1320 /* 360x576 or 360x480 for now */
1321 if(width
== maxw
&& height
== maxh
)
1326 (((unsigned long)maxw
) << 12) / width
- 4096L) >=
1330 (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
1333 (((unsigned long)maxh
) << 12) / height
- 4096L) >=
1337 (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
1339 format
->fmt
.pix
.width
= width
;
1340 format
->fmt
.pix
.height
= height
;
1341 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
1342 format
->fmt
.pix
.bytesperline
= width
* 2;
1343 format
->fmt
.pix
.sizeimage
= width
* 2 * height
;
1344 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1345 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1347 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
1349 VIDIOC_TRY_FMT
? "VIDIOC_TRY_FMT" :
1350 "VIDIOC_S_FMT", format
->fmt
.pix
.width
,
1351 format
->fmt
.pix
.height
, hscale
, vscale
);
1353 if (cmd
== VIDIOC_TRY_FMT
)
1356 for (i
= 0; i
< dev
->num_frames
; i
++)
1357 if (dev
->frame
[i
].vma_use_count
) {
1358 em28xx_videodbg("VIDIOC_S_FMT failed. "
1359 "Unmap the buffers first.\n");
1363 /* stop io in case it is already in progress */
1364 if (dev
->stream
== STREAM_ON
) {
1365 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
1366 if ((ret
= em28xx_stream_interrupt(dev
)))
1370 em28xx_release_buffers(dev
);
1373 /* set new image size */
1375 dev
->height
= height
;
1376 dev
->frame_size
= dev
->width
* dev
->height
* 2;
1377 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1378 dev
->bytesperline
= dev
->width
* 2;
1379 dev
->hscale
= hscale
;
1380 dev
->vscale
= vscale
;
1381 /* dev->both_fileds = both_fileds; */
1382 em28xx_uninit_isoc(dev
);
1383 em28xx_set_alternate(dev
);
1384 em28xx_capture_start(dev
, 1);
1385 em28xx_resolution_set(dev
);
1386 em28xx_init_isoc(dev
);
1391 /* --- streaming capture ------------------------------------- */
1392 case VIDIOC_REQBUFS
:
1394 struct v4l2_requestbuffers
*rb
= arg
;
1398 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1399 rb
->memory
!= V4L2_MEMORY_MMAP
)
1402 if (dev
->io
== IO_READ
) {
1403 em28xx_videodbg ("method is set to read;"
1404 " close and open the device again to"
1405 " choose the mmap I/O method\n");
1409 for (i
= 0; i
< dev
->num_frames
; i
++)
1410 if (dev
->frame
[i
].vma_use_count
) {
1411 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1415 if (dev
->stream
== STREAM_ON
) {
1416 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1417 if ((ret
= em28xx_stream_interrupt(dev
)))
1421 em28xx_empty_framequeues(dev
);
1423 em28xx_release_buffers(dev
);
1426 em28xx_request_buffers(dev
, rb
->count
);
1428 dev
->frame_current
= NULL
;
1430 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1432 dev
->io
= rb
->count
? IO_MMAP
: IO_NONE
;
1436 case VIDIOC_QUERYBUF
:
1438 struct v4l2_buffer
*b
= arg
;
1440 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1441 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
)
1444 memcpy(b
, &dev
->frame
[b
->index
].buf
, sizeof(*b
));
1446 if (dev
->frame
[b
->index
].vma_use_count
) {
1447 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1449 if (dev
->frame
[b
->index
].state
== F_DONE
)
1450 b
->flags
|= V4L2_BUF_FLAG_DONE
;
1451 else if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1452 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1457 struct v4l2_buffer
*b
= arg
;
1458 unsigned long lock_flags
;
1460 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1461 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
) {
1465 if (dev
->frame
[b
->index
].state
!= F_UNUSED
) {
1468 dev
->frame
[b
->index
].state
= F_QUEUED
;
1470 /* add frame to fifo */
1471 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1472 list_add_tail(&dev
->frame
[b
->index
].frame
,
1474 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1480 struct v4l2_buffer
*b
= arg
;
1481 struct em28xx_frame_t
*f
;
1482 unsigned long lock_flags
;
1485 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1486 || dev
->io
!= IO_MMAP
)
1489 if (list_empty(&dev
->outqueue
)) {
1490 if (dev
->stream
== STREAM_OFF
)
1492 if (filp
->f_flags
& O_NONBLOCK
)
1494 ret
= wait_event_interruptible
1496 (!list_empty(&dev
->outqueue
)) ||
1497 (dev
->state
& DEV_DISCONNECTED
));
1500 if (dev
->state
& DEV_DISCONNECTED
)
1504 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1505 f
= list_entry(dev
->outqueue
.next
,
1506 struct em28xx_frame_t
, frame
);
1507 list_del(dev
->outqueue
.next
);
1508 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1510 f
->state
= F_UNUSED
;
1511 memcpy(b
, &f
->buf
, sizeof(*b
));
1513 if (f
->vma_use_count
)
1514 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1519 return em28xx_do_ioctl(inode
, filp
, dev
, cmd
, arg
,
1520 em28xx_video_do_ioctl
);
1526 * em28xx_v4l2_ioctl()
1527 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1529 static int em28xx_v4l2_ioctl(struct inode
*inode
, struct file
*filp
,
1530 unsigned int cmd
, unsigned long arg
)
1533 struct em28xx
*dev
= filp
->private_data
;
1535 if (down_interruptible(&dev
->fileop_lock
))
1536 return -ERESTARTSYS
;
1538 if (dev
->state
& DEV_DISCONNECTED
) {
1539 em28xx_errdev("v4l2 ioctl: device not present\n");
1540 up(&dev
->fileop_lock
);
1544 if (dev
->state
& DEV_MISCONFIGURED
) {
1546 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1547 up(&dev
->fileop_lock
);
1551 ret
= video_usercopy(inode
, filp
, cmd
, arg
, em28xx_video_do_ioctl
);
1553 up(&dev
->fileop_lock
);
1558 static struct file_operations em28xx_v4l_fops
= {
1559 .owner
= THIS_MODULE
,
1560 .open
= em28xx_v4l2_open
,
1561 .release
= em28xx_v4l2_close
,
1562 .ioctl
= em28xx_v4l2_ioctl
,
1563 .read
= em28xx_v4l2_read
,
1564 .poll
= em28xx_v4l2_poll
,
1565 .mmap
= em28xx_v4l2_mmap
,
1566 .llseek
= no_llseek
,
1569 /******************************** usb interface *****************************************/
1573 * allocates and inits the device structs, registers i2c bus and v4l device
1575 static int em28xx_init_dev(struct em28xx
**devhandle
, struct usb_device
*udev
,
1576 int minor
, int model
)
1578 struct em28xx
*dev
= *devhandle
;
1579 int retval
= -ENOMEM
;
1581 unsigned int maxh
, maxw
;
1585 init_MUTEX(&dev
->lock
);
1586 init_waitqueue_head(&dev
->open
);
1588 dev
->em28xx_write_regs
= em28xx_write_regs
;
1589 dev
->em28xx_read_reg
= em28xx_read_reg
;
1590 dev
->em28xx_read_reg_req_len
= em28xx_read_reg_req_len
;
1591 dev
->em28xx_write_regs_req
= em28xx_write_regs_req
;
1592 dev
->em28xx_read_reg_req
= em28xx_read_reg_req
;
1593 dev
->is_em2800
= em28xx_boards
[model
].is_em2800
;
1594 dev
->has_tuner
= em28xx_boards
[model
].has_tuner
;
1595 dev
->has_msp34xx
= em28xx_boards
[model
].has_msp34xx
;
1596 dev
->tda9887_conf
= em28xx_boards
[model
].tda9887_conf
;
1597 dev
->decoder
= em28xx_boards
[model
].decoder
;
1600 dev
->tuner_type
= tuner
;
1602 dev
->tuner_type
= em28xx_boards
[model
].tuner_type
;
1604 dev
->video_inputs
= em28xx_boards
[model
].vchannels
;
1606 for (i
= 0; i
< TVNORMS
; i
++)
1607 if (em28xx_boards
[model
].norm
== tvnorms
[i
].mode
)
1612 dev
->tvnorm
= &tvnorms
[i
]; /* set default norm */
1614 em28xx_videodbg("tvnorm=%s\n", dev
->tvnorm
->name
);
1616 maxw
= norm_maxw(dev
);
1617 maxh
= norm_maxh(dev
);
1619 /* set default image size */
1622 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1623 dev
->field_size
= dev
->width
* dev
->height
;
1625 dev
->interlaced
? dev
->field_size
<< 1 : dev
->field_size
;
1626 dev
->bytesperline
= dev
->width
* 2;
1631 /* setup video picture settings for saa7113h */
1632 memset(&dev
->vpic
, 0, sizeof(dev
->vpic
));
1633 dev
->vpic
.colour
= 128 << 8;
1634 dev
->vpic
.hue
= 128 << 8;
1635 dev
->vpic
.brightness
= 128 << 8;
1636 dev
->vpic
.contrast
= 192 << 8;
1637 dev
->vpic
.whiteness
= 128 << 8; /* This one isn't used */
1638 dev
->vpic
.depth
= 16;
1639 dev
->vpic
.palette
= VIDEO_PALETTE_YUV422
;
1641 #ifdef CONFIG_MODULES
1642 /* request some modules */
1643 if (dev
->decoder
== EM28XX_SAA7113
|| dev
->decoder
== EM28XX_SAA7114
)
1644 request_module("saa711x");
1645 if (dev
->decoder
== EM28XX_TVP5150
)
1646 request_module("tvp5150");
1648 request_module("tuner");
1649 if (dev
->tda9887_conf
)
1650 request_module("tda9887");
1652 errCode
= em28xx_config(dev
);
1654 em28xx_errdev("error configuring device\n");
1660 /* register i2c bus */
1661 em28xx_i2c_register(dev
);
1663 /* Do board specific init and eeprom reading */
1664 em28xx_card_setup(dev
);
1666 /* configure the device */
1667 em28xx_config_i2c(dev
);
1671 errCode
= em28xx_config(dev
);
1673 #ifdef CONFIG_MODULES
1674 if (dev
->has_msp34xx
)
1675 request_module("msp3400");
1677 /* allocate and fill v4l2 device struct */
1678 dev
->vdev
= video_device_alloc();
1679 if (NULL
== dev
->vdev
) {
1680 em28xx_errdev("cannot allocate video_device.\n");
1685 dev
->vdev
->type
= VID_TYPE_CAPTURE
;
1687 dev
->vdev
->type
|= VID_TYPE_TUNER
;
1688 dev
->vdev
->hardware
= 0;
1689 dev
->vdev
->fops
= &em28xx_v4l_fops
;
1690 dev
->vdev
->minor
= -1;
1691 dev
->vdev
->dev
= &dev
->udev
->dev
;
1692 dev
->vdev
->release
= video_device_release
;
1693 snprintf(dev
->vdev
->name
, sizeof(dev
->vdev
->name
), "%s",
1695 list_add_tail(&dev
->devlist
,&em28xx_devlist
);
1697 /* register v4l2 device */
1699 if ((retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1))) {
1700 em28xx_errdev("unable to register video device (error=%i).\n",
1703 list_del(&dev
->devlist
);
1704 video_device_release(dev
->vdev
);
1708 if (dev
->has_msp34xx
) {
1709 /* Send a reset to other chips via gpio */
1710 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xf7", 1);
1712 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xff", 1);
1720 em28xx_info("V4L2 device registered as /dev/video%d\n",
1727 * em28xx_usb_probe()
1728 * checks for supported devices
1730 static int em28xx_usb_probe(struct usb_interface
*interface
,
1731 const struct usb_device_id
*id
)
1733 const struct usb_endpoint_descriptor
*endpoint
;
1734 struct usb_device
*udev
;
1735 struct usb_interface
*uif
;
1736 struct em28xx
*dev
= NULL
;
1737 int retval
= -ENODEV
;
1738 int model
,i
,nr
,ifnum
;
1740 udev
= usb_get_dev(interface_to_usbdev(interface
));
1741 ifnum
= interface
->altsetting
[0].desc
.bInterfaceNumber
;
1744 /* Don't register audio interfaces */
1745 if (interface
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
1746 em28xx_err(DRIVER_NAME
" audio device (%04x:%04x): interface %i, class %i\n",
1747 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1749 interface
->altsetting
[0].desc
.bInterfaceClass
);
1753 em28xx_err(DRIVER_NAME
" new video device (%04x:%04x): interface %i, class %i\n",
1754 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1756 interface
->altsetting
[0].desc
.bInterfaceClass
);
1758 endpoint
= &interface
->cur_altsetting
->endpoint
[1].desc
;
1760 /* check if the the device has the iso in endpoint at the correct place */
1761 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
1762 USB_ENDPOINT_XFER_ISOC
) {
1763 em28xx_err(DRIVER_NAME
" probing error: endpoint is non-ISO endpoint!\n");
1766 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1767 em28xx_err(DRIVER_NAME
" probing error: endpoint is ISO OUT endpoint!\n");
1771 model
=id
->driver_info
;
1772 nr
=interface
->minor
;
1774 if (nr
>EM28XX_MAXBOARDS
) {
1775 printk (DRIVER_NAME
": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS
);
1779 /* allocate memory for our device state and initialize it */
1780 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
1782 em28xx_err(DRIVER_NAME
": out of memory!\n");
1785 memset(dev
, 0, sizeof(*dev
));
1787 /* compute alternate max packet sizes */
1788 uif
= udev
->actconfig
->interface
[0];
1790 dev
->num_alt
=uif
->num_altsetting
;
1791 printk(DRIVER_NAME
": Alternate settings: %i\n",dev
->num_alt
);
1792 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1793 dev
->alt_max_pkt_size
= kmalloc(32*
1794 dev
->num_alt
,GFP_KERNEL
);
1795 if (dev
->alt_max_pkt_size
== NULL
) {
1796 em28xx_err(DRIVER_NAME
": out of memory!\n");
1800 for (i
= 0; i
< dev
->num_alt
; i
++) {
1801 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1803 dev
->alt_max_pkt_size
[i
] =
1804 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1805 printk(DRIVER_NAME
": Alternate setting %i, max size= %i\n",i
,
1806 dev
->alt_max_pkt_size
[i
]);
1809 snprintf(dev
->name
, 29, "em28xx #%d", nr
);
1811 if ((card
[nr
]>=0)&&(card
[nr
]<em28xx_bcount
))
1814 if ((model
==EM2800_BOARD_UNKNOWN
)||(model
==EM2820_BOARD_UNKNOWN
)) {
1815 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1816 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1817 "%s: workaround that. Redirect complaints to the vendor of\n"
1818 "%s: the TV card. Best regards,\n"
1820 dev
->name
,dev
->name
,dev
->name
,dev
->name
,dev
->name
);
1821 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1823 for (i
= 0; i
< em28xx_bcount
; i
++) {
1824 printk("%s: card=%d -> %s\n",
1825 dev
->name
, i
, em28xx_boards
[i
].name
);
1829 /* allocate device struct */
1830 retval
= em28xx_init_dev(&dev
, udev
, nr
, model
);
1834 em28xx_info("Found %s\n", em28xx_boards
[model
].name
);
1836 /* save our data pointer in this interface device */
1837 usb_set_intfdata(interface
, dev
);
1842 * em28xx_usb_disconnect()
1843 * called when the device gets diconencted
1844 * video device will be unregistered on v4l2_close in case it is still open
1846 static void em28xx_usb_disconnect(struct usb_interface
*interface
)
1848 struct em28xx
*dev
= usb_get_intfdata(interface
);
1849 usb_set_intfdata(interface
, NULL
);
1854 down_write(&em28xx_disconnect
);
1858 em28xx_info("disconnecting %s\n", dev
->vdev
->name
);
1860 wake_up_interruptible_all(&dev
->open
);
1864 ("device /dev/video%d is open! Deregistration and memory "
1865 "deallocation are deferred on close.\n", dev
->vdev
->minor
);
1866 dev
->state
|= DEV_MISCONFIGURED
;
1867 em28xx_uninit_isoc(dev
);
1868 dev
->state
|= DEV_DISCONNECTED
;
1869 wake_up_interruptible(&dev
->wait_frame
);
1870 wake_up_interruptible(&dev
->wait_stream
);
1872 dev
->state
|= DEV_DISCONNECTED
;
1873 em28xx_release_resources(dev
);
1879 kfree(dev
->alt_max_pkt_size
);
1883 up_write(&em28xx_disconnect
);
1886 static struct usb_driver em28xx_usb_driver
= {
1887 .owner
= THIS_MODULE
,
1889 .probe
= em28xx_usb_probe
,
1890 .disconnect
= em28xx_usb_disconnect
,
1891 .id_table
= em28xx_id_table
,
1894 static int __init
em28xx_module_init(void)
1898 printk(KERN_INFO DRIVER_NAME
" v4l2 driver version %d.%d.%d loaded\n",
1899 (EM28XX_VERSION_CODE
>> 16) & 0xff,
1900 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
1902 printk(KERN_INFO DRIVER_NAME
" snapshot date %04d-%02d-%02d\n",
1903 SNAPSHOT
/ 10000, (SNAPSHOT
/ 100) % 100, SNAPSHOT
% 100);
1906 /* register this driver with the USB subsystem */
1907 result
= usb_register(&em28xx_usb_driver
);
1909 em28xx_err(DRIVER_NAME
1910 " usb_register failed. Error number %d.\n", result
);
1915 static void __exit
em28xx_module_exit(void)
1917 /* deregister this driver with the USB subsystem */
1918 usb_deregister(&em28xx_usb_driver
);
1921 module_init(em28xx_module_init
);
1922 module_exit(em28xx_module_exit
);