2 em2820-video.c - driver for Empia EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/usb.h>
30 #include <linux/i2c.h>
31 #include <media/tuner.h>
32 #include <linux/video_decoder.h>
36 #define DRIVER_AUTHOR "Markus Rechberger <mrechberger@gmail.com>, " \
37 "Ludovico Cavedon <cavedon@sssup.it>, " \
38 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>"
40 #define DRIVER_NAME "em2820"
41 #define DRIVER_DESC "Empia em2820 based USB video device driver"
42 #define EM2820_VERSION_CODE KERNEL_VERSION(0, 0, 1)
44 #define em2820_videodbg(fmt, arg...) do {\
46 printk(KERN_INFO "%s %s :"fmt, \
47 dev->name, __FUNCTION__ , ##arg); } while (0)
49 MODULE_AUTHOR(DRIVER_AUTHOR);
50 MODULE_DESCRIPTION(DRIVER_DESC);
51 MODULE_LICENSE("GPL");
53 static int tuner = -1;
54 module_param(tuner, int, 0444);
55 MODULE_PARM_DESC(tuner, "tuner type");
57 static unsigned int video_debug = 0;
58 module_param(video_debug,int,0644);
59 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
61 /* supported tv norms */
62 static struct em2820_tvnorm tvnorms[] = {
66 .mode = VIDEO_MODE_PAL,
70 .mode = VIDEO_MODE_NTSC,
74 .mode = VIDEO_MODE_SECAM,
78 .mode = VIDEO_MODE_PAL,
82 #define TVNORMS ARRAY_SIZE(tvnorms)
84 /* supported controls */
85 static struct v4l2_queryctrl em2820_qctrl[] = {
87 .id = V4L2_CID_BRIGHTNESS,
88 .type = V4L2_CTRL_TYPE_INTEGER,
96 .id = V4L2_CID_CONTRAST,
97 .type = V4L2_CTRL_TYPE_INTEGER,
102 .default_value = 0x10,
105 .id = V4L2_CID_SATURATION,
106 .type = V4L2_CTRL_TYPE_INTEGER,
107 .name = "Saturation",
111 .default_value = 0x10,
114 .id = V4L2_CID_AUDIO_VOLUME,
115 .type = V4L2_CTRL_TYPE_INTEGER,
120 .default_value = 0x1f,
123 .id = V4L2_CID_AUDIO_MUTE,
124 .type = V4L2_CTRL_TYPE_BOOLEAN,
132 .id = V4L2_CID_RED_BALANCE,
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 .name = "Red chroma balance",
141 .id = V4L2_CID_BLUE_BALANCE,
142 .type = V4L2_CTRL_TYPE_INTEGER,
143 .name = "Blue chroma balance",
150 .id = V4L2_CID_GAMMA,
151 .type = V4L2_CTRL_TYPE_INTEGER,
156 .default_value = 0x20,
161 static struct usb_driver em2820_usb_driver;
163 static DECLARE_MUTEX(em2820_sysfs_lock);
164 static DECLARE_RWSEM(em2820_disconnect);
166 /********************* v4l2 interface ******************************************/
168 static inline unsigned long kvirt_to_pa(unsigned long adr)
170 unsigned long kva, ret;
172 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
173 kva |= adr & (PAGE_SIZE - 1);
180 * inits registers with sane defaults
182 static int em2820_config(struct em2820 *dev)
185 /* Sets I2C speed to 100 KHz */
186 em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
188 /* enable vbi capturing */
189 em2820_audio_usb_mute(dev, 1);
190 dev->mute = 1; /* maybe not the right place... */
192 em2820_audio_analog_set(dev);
193 em2820_audio_analog_setup(dev);
194 em2820_outfmt_set_yuv422(dev);
195 em2820_colorlevels_set_default(dev);
196 em2820_compression_disable(dev);
202 * em2820_config_i2c()
203 * configure i2c attached devices
205 void em2820_config_i2c(struct em2820 *dev)
207 struct v4l2_frequency f;
208 struct video_decoder_init em2820_vdi = {.data = NULL };
211 /* configure decoder */
212 em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
213 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
214 /* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
215 /* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
216 /* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
217 /* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
219 /* configure tuner */
221 f.type = V4L2_TUNER_ANALOG_TV;
222 f.frequency = 9076; /* FIXME:remove magic number */
223 dev->ctl_freq = f.frequency;
224 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
226 /* configure tda9887 */
228 em2820_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf);
230 /* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
234 * em2820_empty_framequeues()
235 * prepare queues for incoming and outgoing frames
237 static void em2820_empty_framequeues(struct em2820 *dev)
241 INIT_LIST_HEAD(&dev->inqueue);
242 INIT_LIST_HEAD(&dev->outqueue);
244 for (i = 0; i < EM2820_NUM_FRAMES; i++) {
245 dev->frame[i].state = F_UNUSED;
246 dev->frame[i].buf.bytesused = 0;
252 * inits the device and starts isoc transfer
254 static int em2820_v4l2_open(struct inode *inode, struct file *filp)
256 struct video_device *vdev = video_devdata(filp);
257 int minor = iminor(inode);
258 struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
261 em2820_videodbg("users=%d", dev->users);
263 if (!down_read_trylock(&em2820_disconnect))
267 em2820_warn("this driver can be opened only once\n");
268 up_read(&em2820_disconnect);
272 /* if(dev->vbi_dev->minor == minor){
273 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
275 if (dev->vdev->minor == minor) {
276 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
279 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
280 spin_lock_init(&dev->queue_lock);
281 init_waitqueue_head(&dev->wait_frame);
282 init_waitqueue_head(&dev->wait_stream);
286 em2820_set_alternate(dev);
288 dev->width = norm_maxw(dev);
289 dev->height = norm_maxh(dev);
290 dev->frame_size = dev->width * dev->height * 2;
291 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
292 dev->bytesperline = dev->width * 2;
296 em2820_capture_start(dev, 1);
297 em2820_resolution_set(dev);
299 /* start the transfer */
300 errCode = em2820_init_isoc(dev);
305 filp->private_data = dev;
307 dev->stream = STREAM_OFF;
311 em2820_empty_framequeues(dev);
313 dev->state |= DEV_INITIALIZED;
317 up_read(&em2820_disconnect);
322 * em2820_realease_resources()
323 * unregisters the v4l2,i2c and usb devices
324 * called when the device gets disconected or at module unload
326 static void em2820_release_resources(struct em2820 *dev)
328 down(&em2820_sysfs_lock);
330 em2820_info("V4L2 device /dev/video%d deregistered\n",
332 video_set_drvdata(dev->vdev, NULL);
333 video_unregister_device(dev->vdev);
334 /* video_unregister_device(dev->vbi_dev); */
335 em2820_i2c_unregister(dev);
336 usb_put_dev(dev->udev);
337 up(&em2820_sysfs_lock);
341 * em2820_v4l2_close()
342 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
344 static int em2820_v4l2_close(struct inode *inode, struct file *file)
346 struct video_device *vdev = video_devdata(file);
347 struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
350 em2820_videodbg("users=%d", dev->users);
354 em2820_uninit_isoc(dev);
356 em2820_release_buffers(dev);
358 /* the device is already disconnect, free the remaining resources */
359 if (dev->state & DEV_DISCONNECTED) {
360 em2820_release_resources(dev);
366 /* set alternate 0 */
368 em2820_videodbg("setting alternate 0");
369 errCode = usb_set_interface(dev->udev, 0, 0);
371 em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
376 wake_up_interruptible_nr(&dev->open, 1);
383 * will allocate buffers when called for the first time
386 em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
389 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
390 struct em2820_frame_t *f, *i;
391 unsigned long lock_flags;
394 if (down_interruptible(&dev->fileop_lock))
397 if (dev->state & DEV_DISCONNECTED) {
398 em2820_videodbg("device not present");
399 up(&dev->fileop_lock);
403 if (dev->state & DEV_MISCONFIGURED) {
404 em2820_videodbg("device misconfigured; close and open it again");
405 up(&dev->fileop_lock);
409 if (dev->io == IO_MMAP) {
410 em2820_videodbg ("IO method is set to mmap; close and open"
411 " the device again to choose the read method");
412 up(&dev->fileop_lock);
416 if (dev->io == IO_NONE) {
417 if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
418 em2820_errdev("read failed, not enough memory\n");
419 up(&dev->fileop_lock);
423 dev->stream = STREAM_ON;
424 em2820_queue_unusedframes(dev);
428 up(&dev->fileop_lock);
432 if (list_empty(&dev->outqueue)) {
433 if (filp->f_flags & O_NONBLOCK) {
434 up(&dev->fileop_lock);
437 ret = wait_event_interruptible
439 (!list_empty(&dev->outqueue)) ||
440 (dev->state & DEV_DISCONNECTED));
442 up(&dev->fileop_lock);
445 if (dev->state & DEV_DISCONNECTED) {
446 up(&dev->fileop_lock);
451 f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
453 spin_lock_irqsave(&dev->queue_lock, lock_flags);
454 list_for_each_entry(i, &dev->outqueue, frame)
456 INIT_LIST_HEAD(&dev->outqueue);
457 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
459 em2820_queue_unusedframes(dev);
461 if (count > f->buf.length)
462 count = f->buf.length;
464 if (copy_to_user(buf, f->bufmem, count)) {
465 up(&dev->fileop_lock);
470 up(&dev->fileop_lock);
477 * will allocate buffers when called for the first time
479 static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
481 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
482 unsigned int mask = 0;
484 if (down_interruptible(&dev->fileop_lock))
487 if (dev->state & DEV_DISCONNECTED) {
488 em2820_videodbg("device not present");
489 } else if (dev->state & DEV_MISCONFIGURED) {
490 em2820_videodbg("device is misconfigured; close and open it again");
492 if (dev->io == IO_NONE) {
493 if (!em2820_request_buffers
494 (dev, EM2820_NUM_READ_FRAMES)) {
496 ("poll() failed, not enough memory\n");
499 dev->stream = STREAM_ON;
503 if (dev->io == IO_READ) {
504 em2820_queue_unusedframes(dev);
505 poll_wait(filp, &dev->wait_frame, wait);
507 if (!list_empty(&dev->outqueue))
508 mask |= POLLIN | POLLRDNORM;
510 up(&dev->fileop_lock);
516 up(&dev->fileop_lock);
523 static void em2820_vm_open(struct vm_area_struct *vma)
525 struct em2820_frame_t *f = vma->vm_private_data;
532 static void em2820_vm_close(struct vm_area_struct *vma)
534 /* NOTE: buffers are not freed here */
535 struct em2820_frame_t *f = vma->vm_private_data;
539 static struct vm_operations_struct em2820_vm_ops = {
540 .open = em2820_vm_open,
541 .close = em2820_vm_close,
547 static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
549 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
550 unsigned long size = vma->vm_end - vma->vm_start,
551 start = vma->vm_start, pos, page;
553 if (down_interruptible(&dev->fileop_lock))
556 if (dev->state & DEV_DISCONNECTED) {
557 em2820_videodbg("mmap: device not present");
558 up(&dev->fileop_lock);
562 if (dev->state & DEV_MISCONFIGURED) {
563 em2820_videodbg ("mmap: Device is misconfigured; close and "
565 up(&dev->fileop_lock);
569 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
570 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
571 up(&dev->fileop_lock);
575 for (i = 0; i < dev->num_frames; i++) {
576 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
579 if (i == dev->num_frames) {
580 em2820_videodbg("mmap: user supplied mapping address is out of range");
581 up(&dev->fileop_lock);
585 /* VM_IO is eventually going to replace PageReserved altogether */
586 vma->vm_flags |= VM_IO;
587 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
589 pos = (unsigned long)dev->frame[i].bufmem;
590 while (size > 0) { /* size is page-aligned */
591 page = vmalloc_to_pfn((void *)pos);
592 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
593 vma->vm_page_prot)) {
594 em2820_videodbg("mmap: rename page map failed");
595 up(&dev->fileop_lock);
603 vma->vm_ops = &em2820_vm_ops;
604 vma->vm_private_data = &dev->frame[i];
607 up(&dev->fileop_lock);
613 * return the current saturation, brightness or contrast, mute state
615 static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
619 case V4L2_CID_AUDIO_MUTE:
620 ctrl->value = dev->mute;
622 case V4L2_CID_AUDIO_VOLUME:
623 ctrl->value = dev->volume;
625 case V4L2_CID_BRIGHTNESS:
626 if ((tmp = em2820_brightness_get(dev)) < 0)
628 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
630 case V4L2_CID_CONTRAST:
631 if ((ctrl->value = em2820_contrast_get(dev)) < 0)
634 case V4L2_CID_SATURATION:
635 if ((ctrl->value = em2820_saturation_get(dev)) < 0)
638 case V4L2_CID_RED_BALANCE:
639 if ((tmp = em2820_v_balance_get(dev)) < 0)
641 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
643 case V4L2_CID_BLUE_BALANCE:
644 if ((tmp = em2820_u_balance_get(dev)) < 0)
646 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
649 if ((ctrl->value = em2820_gamma_get(dev)) < 0)
659 * mute or set new saturation, brightness or contrast
661 static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
664 case V4L2_CID_AUDIO_MUTE:
665 if (ctrl->value != dev->mute) {
666 dev->mute = ctrl->value;
667 em2820_audio_usb_mute(dev, ctrl->value);
668 return em2820_audio_analog_set(dev);
671 case V4L2_CID_AUDIO_VOLUME:
672 dev->volume = ctrl->value;
673 return em2820_audio_analog_set(dev);
674 case V4L2_CID_BRIGHTNESS:
675 return em2820_brightness_set(dev, ctrl->value);
676 case V4L2_CID_CONTRAST:
677 return em2820_contrast_set(dev, ctrl->value);
678 case V4L2_CID_SATURATION:
679 return em2820_saturation_set(dev, ctrl->value);
680 case V4L2_CID_RED_BALANCE:
681 return em2820_v_balance_set(dev, ctrl->value);
682 case V4L2_CID_BLUE_BALANCE:
683 return em2820_u_balance_set(dev, ctrl->value);
685 return em2820_gamma_set(dev, ctrl->value);
692 * em2820_stream_interrupt()
695 static int em2820_stream_interrupt(struct em2820 *dev)
699 /* stop reading from the device */
701 dev->stream = STREAM_INTERRUPT;
702 ret = wait_event_timeout(dev->wait_stream,
703 (dev->stream == STREAM_OFF) ||
704 (dev->state & DEV_DISCONNECTED),
706 if (dev->state & DEV_DISCONNECTED)
709 dev->state |= DEV_MISCONFIGURED;
710 em2820_videodbg("device is misconfigured; close and "
711 "open /dev/video%d again", dev->vdev->minor);
718 static int em2820_set_norm(struct em2820 *dev, int width, int height)
720 unsigned int hscale, vscale;
721 unsigned int maxh, maxw;
723 maxw = norm_maxw(dev);
724 maxh = norm_maxh(dev);
726 /* width must even because of the YUYV format */
727 /* height must be even because of interlacing */
740 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
742 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
744 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
746 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
748 /* set new image size */
750 dev->height = height;
751 dev->frame_size = dev->width * dev->height * 2;
752 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
753 dev->bytesperline = dev->width * 2;
754 dev->hscale = hscale;
755 dev->vscale = vscale;
757 em2820_resolution_set(dev);
762 static void video_mux(struct em2820 *dev, int index)
766 input = INPUT(index)->vmux;
767 dev->ctl_input = index;
769 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
771 dev->ctl_ainput = INPUT(index)->amux;
773 switch (dev->ctl_ainput) {
775 ainput = EM2820_AUDIO_SRC_TUNER;
778 ainput = EM2820_AUDIO_SRC_LINE;
781 em2820_audio_source(dev, ainput);
785 * em2820_v4l2_do_ioctl()
786 * This function is _not_ called directly, but from
787 * em2820_v4l2_ioctl. Userspace
788 * copying is done already, arg is a kernel pointer.
790 static int em2820_do_ioctl(struct inode *inode, struct file *filp,
791 struct em2820 *dev, unsigned int cmd, void *arg,
792 v4l2_kioctl driver_ioctl)
797 /* ---------- tv norms ---------- */
800 struct v4l2_standard *e = arg;
806 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
807 tvnorms[e->index].name);
815 v4l2_std_id *id = arg;
817 *id = dev->tvnorm->id;
822 v4l2_std_id *id = arg;
825 for (i = 0; i < TVNORMS; i++)
826 if (*id == tvnorms[i].id)
829 for (i = 0; i < TVNORMS; i++)
830 if (*id & tvnorms[i].id)
836 dev->tvnorm = &tvnorms[i];
838 em2820_set_norm(dev, dev->width, dev->height);
841 dev->width=norm_maxw(dev);
842 dev->height=norm_maxh(dev);
843 dev->frame_size=dev->width*dev->height*2;
844 dev->field_size=dev->frame_size>>1;
845 dev->bytesperline=dev->width*2;
849 em2820_resolution_set(dev);
852 em2820_uninit_isoc(dev);
853 em2820_set_alternate(dev);
854 em2820_capture_start(dev, 1);
855 em2820_resolution_set(dev);
856 em2820_init_isoc(dev);
858 em2820_i2c_call_clients(dev, DECODER_SET_NORM,
860 em2820_i2c_call_clients(dev, VIDIOC_S_STD,
868 /* ------ input switching ---------- */
869 case VIDIOC_ENUMINPUT:
871 struct v4l2_input *i = arg;
873 static const char *iname[] = {
874 [EM2820_VMUX_COMPOSITE1] = "Composite1",
875 [EM2820_VMUX_COMPOSITE2] = "Composite2",
876 [EM2820_VMUX_COMPOSITE3] = "Composite3",
877 [EM2820_VMUX_COMPOSITE4] = "Composite4",
878 [EM2820_VMUX_SVIDEO] = "S-Video",
879 [EM2820_VMUX_TELEVISION] = "Television",
880 [EM2820_VMUX_CABLE] = "Cable TV",
881 [EM2820_VMUX_DVB] = "DVB",
882 [EM2820_VMUX_DEBUG] = "for debug only",
886 if (n >= MAX_EM2820_INPUT)
888 if (0 == INPUT(n)->type)
890 memset(i, 0, sizeof(*i));
892 i->type = V4L2_INPUT_TYPE_CAMERA;
893 strcpy(i->name, iname[INPUT(n)->type]);
894 if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
895 (EM2820_VMUX_CABLE == INPUT(n)->type))
896 i->type = V4L2_INPUT_TYPE_TUNER;
897 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
898 i->std |= tvnorms[n].id;
914 if (*index >= MAX_EM2820_INPUT)
916 if (0 == INPUT(*index)->type)
920 video_mux(dev, *index);
928 struct v4l2_audio *a = arg;
929 unsigned int index = a->index;
933 memset(a, 0, sizeof(*a));
934 index = dev->ctl_ainput;
937 strcpy(a->name, "Television");
939 strcpy(a->name, "Line In");
941 a->capability = V4L2_AUDCAP_STEREO;
948 struct v4l2_audio *a = arg;
949 if (a->index != dev->ctl_ainput)
955 /* --- controls ---------------------------------------------- */
956 case VIDIOC_QUERYCTRL:
958 struct v4l2_queryctrl *qc = arg;
960 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
961 for (i = 0; i < n; i++)
962 if (qc->id && qc->id == em2820_qctrl[i].id) {
963 memcpy(qc, &(em2820_qctrl[i]),
973 struct v4l2_control *ctrl = arg;
976 return em2820_get_ctrl(dev, ctrl);
979 case VIDIOC_S_CTRL_OLD: /* ??? */
982 struct v4l2_control *ctrl = arg;
986 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
987 for (i = 0; i < n; i++)
988 if (ctrl->id == em2820_qctrl[i].id) {
990 em2820_qctrl[i].minimum
992 em2820_qctrl[i].maximum)
995 return em2820_set_ctrl(dev, ctrl);
1000 /* --- tuner ioctls ------------------------------------------ */
1001 case VIDIOC_G_TUNER:
1003 struct v4l2_tuner *t = arg;
1009 memset(t, 0, sizeof(*t));
1010 strcpy(t->name, "Tuner");
1011 t->type = V4L2_TUNER_ANALOG_TV;
1012 t->capability = V4L2_TUNER_CAP_NORM;
1013 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1014 /* t->signal = 0xffff;*/
1015 /* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1016 /* No way to get signal strength? */
1018 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1022 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1024 em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1028 case VIDIOC_S_TUNER:
1030 struct v4l2_tuner *t = arg;
1035 memset(t, 0, sizeof(*t));
1036 strcpy(t->name, "Tuner");
1037 t->type = V4L2_TUNER_ANALOG_TV;
1038 t->capability = V4L2_TUNER_CAP_NORM;
1039 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1040 /* t->signal = 0xffff; */
1041 /* No way to get signal strength? */
1043 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1047 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1049 em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1053 case VIDIOC_G_FREQUENCY:
1055 struct v4l2_frequency *f = arg;
1057 memset(f, 0, sizeof(*f));
1058 f->type = V4L2_TUNER_ANALOG_TV;
1059 f->frequency = dev->ctl_freq;
1063 case VIDIOC_S_FREQUENCY:
1065 struct v4l2_frequency *f = arg;
1070 if (V4L2_TUNER_ANALOG_TV != f->type)
1074 dev->ctl_freq = f->frequency;
1075 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1080 case VIDIOC_CROPCAP:
1082 struct v4l2_cropcap *cc = arg;
1084 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1086 cc->bounds.left = 0;
1088 cc->bounds.width = dev->width;
1089 cc->bounds.height = dev->height;
1090 cc->defrect = cc->bounds;
1091 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1092 cc->pixelaspect.denominator = 59;
1095 case VIDIOC_STREAMON:
1099 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1100 || dev->io != IO_MMAP)
1103 if (list_empty(&dev->inqueue))
1106 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1108 em2820_videodbg("VIDIOC_STREAMON: starting stream");
1112 case VIDIOC_STREAMOFF:
1117 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1118 || dev->io != IO_MMAP)
1121 if (dev->stream == STREAM_ON) {
1122 em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1123 if ((ret = em2820_stream_interrupt(dev)))
1126 em2820_empty_framequeues(dev);
1131 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1138 * em2820_v4l2_do_ioctl()
1139 * This function is _not_ called directly, but from
1140 * em2820_v4l2_ioctl. Userspace
1141 * copying is done already, arg is a kernel pointer.
1143 static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
1144 unsigned int cmd, void *arg)
1146 struct em2820 *dev = filp->private_data;
1151 if (video_debug > 1)
1152 em2820_print_ioctl(dev->name,cmd);
1156 /* --- capabilities ------------------------------------------ */
1157 case VIDIOC_QUERYCAP:
1159 struct v4l2_capability *cap = arg;
1161 memset(cap, 0, sizeof(*cap));
1162 strlcpy(cap->driver, "em2820", sizeof(cap->driver));
1163 strlcpy(cap->card, em2820_boards[dev->model].name,
1165 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1166 sizeof(cap->bus_info));
1167 cap->version = EM2820_VERSION_CODE;
1169 V4L2_CAP_VIDEO_CAPTURE |
1171 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1173 cap->capabilities |= V4L2_CAP_TUNER;
1177 /* --- capture ioctls ---------------------------------------- */
1178 case VIDIOC_ENUM_FMT:
1180 struct v4l2_fmtdesc *fmtd = arg;
1182 if (fmtd->index != 0)
1184 memset(fmtd, 0, sizeof(*fmtd));
1185 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1186 strcpy(fmtd->description, "Packed YUY2");
1187 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1188 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1194 struct v4l2_format *format = arg;
1196 em2820_videodbg("VIDIOC_G_FMT: type=%s",
1198 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1199 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1200 V4L2_BUF_TYPE_VBI_CAPTURE ?
1201 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1204 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1207 format->fmt.pix.width = dev->width;
1208 format->fmt.pix.height = dev->height;
1209 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1210 format->fmt.pix.bytesperline = dev->bytesperline;
1211 format->fmt.pix.sizeimage = dev->frame_size;
1212 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1213 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1215 em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1220 case VIDIOC_TRY_FMT:
1223 struct v4l2_format *format = arg;
1226 int width = format->fmt.pix.width;
1227 int height = format->fmt.pix.height;
1228 unsigned int hscale, vscale;
1229 unsigned int maxh, maxw;
1231 maxw = norm_maxw(dev);
1232 maxh = norm_maxh(dev);
1234 /* int both_fields; */
1236 em2820_videodbg("%s: type=%s",
1238 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1241 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1242 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1243 V4L2_BUF_TYPE_VBI_CAPTURE ?
1244 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1247 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1250 em2820_videodbg("%s: requested %dx%d",
1252 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1253 "VIDIOC_S_FMT", format->fmt.pix.width,
1254 format->fmt.pix.height);
1256 /* FIXME: Move some code away from here */
1257 /* width must even because of the YUYV format */
1258 /* height must be even because of interlacing */
1272 (((unsigned long)maxw) << 12) / width - 4096L) >=
1276 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1279 (((unsigned long)maxh) << 12) / height - 4096L) >=
1283 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1285 format->fmt.pix.width = width;
1286 format->fmt.pix.height = height;
1287 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1288 format->fmt.pix.bytesperline = width * 2;
1289 format->fmt.pix.sizeimage = width * 2 * height;
1290 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1291 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1293 em2820_videodbg("%s: returned %dx%d (%d, %d)",
1295 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1296 "VIDIOC_S_FMT", format->fmt.pix.width,
1297 format->fmt.pix.height, hscale, vscale);
1299 if (cmd == VIDIOC_TRY_FMT)
1302 for (i = 0; i < dev->num_frames; i++)
1303 if (dev->frame[i].vma_use_count) {
1304 em2820_videodbg("VIDIOC_S_FMT failed. "
1305 "Unmap the buffers first.");
1309 /* stop io in case it is already in progress */
1310 if (dev->stream == STREAM_ON) {
1311 em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
1312 if ((ret = em2820_stream_interrupt(dev)))
1316 em2820_release_buffers(dev);
1319 /* set new image size */
1321 dev->height = height;
1322 dev->frame_size = dev->width * dev->height * 2;
1323 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1324 dev->bytesperline = dev->width * 2;
1325 dev->hscale = hscale;
1326 dev->vscale = vscale;
1327 /* dev->both_fileds = both_fileds; */
1328 em2820_uninit_isoc(dev);
1329 em2820_set_alternate(dev);
1330 em2820_capture_start(dev, 1);
1331 em2820_resolution_set(dev);
1332 em2820_init_isoc(dev);
1337 /* --- streaming capture ------------------------------------- */
1338 case VIDIOC_REQBUFS:
1340 struct v4l2_requestbuffers *rb = arg;
1344 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1345 rb->memory != V4L2_MEMORY_MMAP)
1348 if (dev->io == IO_READ) {
1349 em2820_videodbg ("method is set to read;"
1350 " close and open the device again to"
1351 " choose the mmap I/O method");
1355 for (i = 0; i < dev->num_frames; i++)
1356 if (dev->frame[i].vma_use_count) {
1357 em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1361 if (dev->stream == STREAM_ON) {
1362 em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
1363 if ((ret = em2820_stream_interrupt(dev)))
1367 em2820_empty_framequeues(dev);
1369 em2820_release_buffers(dev);
1372 em2820_request_buffers(dev, rb->count);
1374 dev->frame_current = NULL;
1376 em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1378 dev->io = rb->count ? IO_MMAP : IO_NONE;
1382 case VIDIOC_QUERYBUF:
1384 struct v4l2_buffer *b = arg;
1386 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1387 b->index >= dev->num_frames || dev->io != IO_MMAP)
1390 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1392 if (dev->frame[b->index].vma_use_count) {
1393 b->flags |= V4L2_BUF_FLAG_MAPPED;
1395 if (dev->frame[b->index].state == F_DONE)
1396 b->flags |= V4L2_BUF_FLAG_DONE;
1397 else if (dev->frame[b->index].state != F_UNUSED)
1398 b->flags |= V4L2_BUF_FLAG_QUEUED;
1403 struct v4l2_buffer *b = arg;
1404 unsigned long lock_flags;
1406 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1407 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1411 if (dev->frame[b->index].state != F_UNUSED) {
1414 dev->frame[b->index].state = F_QUEUED;
1416 /* add frame to fifo */
1417 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1418 list_add_tail(&dev->frame[b->index].frame,
1420 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1426 struct v4l2_buffer *b = arg;
1427 struct em2820_frame_t *f;
1428 unsigned long lock_flags;
1431 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1432 || dev->io != IO_MMAP)
1435 if (list_empty(&dev->outqueue)) {
1436 if (dev->stream == STREAM_OFF)
1438 if (filp->f_flags & O_NONBLOCK)
1440 ret = wait_event_interruptible
1442 (!list_empty(&dev->outqueue)) ||
1443 (dev->state & DEV_DISCONNECTED));
1446 if (dev->state & DEV_DISCONNECTED)
1450 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1451 f = list_entry(dev->outqueue.next,
1452 struct em2820_frame_t, frame);
1453 list_del(dev->outqueue.next);
1454 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1456 f->state = F_UNUSED;
1457 memcpy(b, &f->buf, sizeof(*b));
1459 if (f->vma_use_count)
1460 b->flags |= V4L2_BUF_FLAG_MAPPED;
1465 return em2820_do_ioctl(inode, filp, dev, cmd, arg,
1466 em2820_video_do_ioctl);
1472 * em2820_v4l2_ioctl()
1473 * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
1475 static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
1476 unsigned int cmd, unsigned long arg)
1478 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
1481 if (down_interruptible(&dev->fileop_lock))
1482 return -ERESTARTSYS;
1484 if (dev->state & DEV_DISCONNECTED) {
1485 em2820_errdev("v4l2 ioctl: device not present\n");
1486 up(&dev->fileop_lock);
1490 if (dev->state & DEV_MISCONFIGURED) {
1492 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1493 up(&dev->fileop_lock);
1497 ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
1499 up(&dev->fileop_lock);
1504 static struct file_operations em2820_v4l_fops = {
1505 .owner = THIS_MODULE,
1506 .open = em2820_v4l2_open,
1507 .release = em2820_v4l2_close,
1508 .ioctl = em2820_v4l2_ioctl,
1509 .read = em2820_v4l2_read,
1510 .poll = em2820_v4l2_poll,
1511 .mmap = em2820_v4l2_mmap,
1512 .llseek = no_llseek,
1515 /******************************** usb interface *****************************************/
1519 * allocates and inits the device structs, registers i2c bus and v4l device
1521 static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
1522 int minor, int model)
1525 int retval = -ENOMEM;
1527 unsigned int maxh, maxw;
1528 struct usb_interface *uif;
1530 /* allocate memory for our device state and initialize it */
1531 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1533 em2820_err(DRIVER_NAME ": out of memory!\n");
1536 memset(dev, 0x00, sizeof(*dev));
1538 snprintf(dev->name, 29, "em2820 #%d", minor);
1541 init_MUTEX(&dev->lock);
1542 init_waitqueue_head(&dev->open);
1544 dev->em2820_write_regs = em2820_write_regs;
1545 dev->em2820_read_reg = em2820_read_reg;
1546 dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
1547 dev->em2820_write_regs_req = em2820_write_regs_req;
1548 dev->em2820_read_reg_req = em2820_read_reg_req;
1549 dev->has_tuner = em2820_boards[model].has_tuner;
1550 dev->has_msp34xx = em2820_boards[model].has_msp34xx;
1551 dev->tda9887_conf = em2820_boards[model].tda9887_conf;
1552 dev->decoder = em2820_boards[model].decoder;
1555 dev->tuner_type = tuner;
1557 dev->tuner_type = em2820_boards[model].tuner_type;
1559 dev->video_inputs = em2820_boards[model].vchannels;
1561 for (i = 0; i < TVNORMS; i++)
1562 if (em2820_boards[model].norm == tvnorms[i].mode)
1567 dev->tvnorm = &tvnorms[i]; /* set default norm */
1569 em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1571 maxw = norm_maxw(dev);
1572 maxh = norm_maxh(dev);
1574 /* set default image size */
1577 dev->interlaced = EM2820_INTERLACED_DEFAULT;
1578 dev->field_size = dev->width * dev->height;
1580 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1581 dev->bytesperline = dev->width * 2;
1586 /* setup video picture settings for saa7113h */
1587 memset(&dev->vpic, 0, sizeof(dev->vpic));
1588 dev->vpic.colour = 128 << 8;
1589 dev->vpic.hue = 128 << 8;
1590 dev->vpic.brightness = 128 << 8;
1591 dev->vpic.contrast = 192 << 8;
1592 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1593 dev->vpic.depth = 16;
1594 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1596 /* compute alternate max packet sizes */
1597 uif = dev->udev->actconfig->interface[0];
1598 dev->alt_max_pkt_size[0] = 0;
1599 for (i = 1; i <= EM2820_MAX_ALT; i++) {
1601 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1603 dev->alt_max_pkt_size[i] =
1604 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1607 #ifdef CONFIG_MODULES
1608 /* request some modules */
1609 if (dev->decoder == EM2820_SAA7113)
1610 request_module("saa7113");
1611 if (dev->decoder == EM2820_SAA7114)
1612 request_module("saa7114");
1613 if (dev->decoder == EM2820_TVP5150)
1614 request_module("tvp5150");
1616 request_module("tuner");
1617 if (dev->tda9887_conf)
1618 request_module("tda9887");
1620 errCode = em2820_config(dev);
1622 em2820_errdev("error configuring device\n");
1628 /* register i2c bus */
1629 em2820_i2c_register(dev);
1631 /* Do board specific init and eeprom reading */
1632 em2820_card_setup(dev);
1634 /* configure the device */
1635 em2820_config_i2c(dev);
1639 errCode = em2820_config(dev);
1641 #ifdef CONFIG_MODULES
1642 if (dev->has_msp34xx)
1643 request_module("msp3400");
1645 /* allocate and fill v4l2 device struct */
1646 dev->vdev = video_device_alloc();
1647 if (NULL == dev->vdev) {
1648 em2820_errdev("cannot allocate video_device.\n");
1653 dev->vdev->owner = THIS_MODULE;
1654 dev->vdev->type = VID_TYPE_CAPTURE;
1656 dev->vdev->type |= VID_TYPE_TUNER;
1657 dev->vdev->hardware = 0;
1658 dev->vdev->fops = &em2820_v4l_fops;
1659 dev->vdev->minor = -1;
1660 dev->vdev->dev = &dev->udev->dev;
1661 dev->vdev->release = video_device_release;
1662 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1664 video_set_drvdata(dev->vdev, dev);
1666 /* register v4l2 device */
1668 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1669 em2820_errdev("unable to register video device (error=%i).\n",
1672 video_set_drvdata(dev->vdev, NULL);
1673 video_device_release(dev->vdev);
1677 if (dev->has_msp34xx) {
1678 /* Send a reset to other chips via gpio */
1679 em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1681 em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1689 em2820_info("V4L2 device registered as /dev/video%d\n",
1697 * em2820_usb_probe()
1698 * checks for supported devices
1700 static int em2820_usb_probe(struct usb_interface *interface,
1701 const struct usb_device_id *id)
1703 const struct usb_endpoint_descriptor *endpoint;
1704 struct usb_device *udev;
1705 struct em2820 *dev = NULL;
1706 int retval = -ENODEV;
1708 udev = usb_get_dev(interface_to_usbdev(interface));
1709 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1711 /* check if the the device has the iso in endpoint at the correct place */
1712 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1713 USB_ENDPOINT_XFER_ISOC) {
1714 /* em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); */
1717 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1718 /* em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); */
1722 /* allocate device struct */
1723 retval = em2820_init_dev(&dev, udev, interface->minor, id->driver_info);
1727 em2820_info("Found %s\n", em2820_boards[id->driver_info].name);
1729 /* save our data pointer in this interface device */
1730 usb_set_intfdata(interface, dev);
1735 * em2820_usb_disconnect()
1736 * called when the device gets diconencted
1737 * video device will be unregistered on v4l2_close in case it is still open
1739 static void em2820_usb_disconnect(struct usb_interface *interface)
1741 struct em2820 *dev = usb_get_intfdata(interface);
1742 usb_set_intfdata(interface, NULL);
1747 down_write(&em2820_disconnect);
1751 em2820_info("disconnecting %s\n", dev->vdev->name);
1753 wake_up_interruptible_all(&dev->open);
1757 ("device /dev/video%d is open! Deregistration and memory "
1758 "deallocation are deferred on close.\n", dev->vdev->minor);
1759 dev->state |= DEV_MISCONFIGURED;
1760 em2820_uninit_isoc(dev);
1761 dev->state |= DEV_DISCONNECTED;
1762 wake_up_interruptible(&dev->wait_frame);
1763 wake_up_interruptible(&dev->wait_stream);
1765 dev->state |= DEV_DISCONNECTED;
1766 em2820_release_resources(dev);
1774 up_write(&em2820_disconnect);
1778 static struct usb_driver em2820_usb_driver = {
1779 .owner = THIS_MODULE,
1781 .probe = em2820_usb_probe,
1782 .disconnect = em2820_usb_disconnect,
1783 .id_table = em2820_id_table,
1786 static int __init em2820_module_init(void)
1790 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1791 (EM2820_VERSION_CODE >> 16) & 0xff,
1792 (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
1794 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1795 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1798 /* register this driver with the USB subsystem */
1799 result = usb_register(&em2820_usb_driver);
1801 em2820_err(DRIVER_NAME
1802 " usb_register failed. Error number %d.\n", result);
1807 static void __exit em2820_module_exit(void)
1809 /* deregister this driver with the USB subsystem */
1810 usb_deregister(&em2820_usb_driver);
1813 module_init(em2820_module_init);
1814 module_exit(em2820_module_exit);