2 em2820-video.c - driver for Empia EM2800/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 <linux/video_decoder.h>
34 #include <media/tuner.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 LIST_HEAD(em2820_devlist);
55 static unsigned int card[] = {[0 ... (EM2820_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 em2820_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 em2820_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 em2820_usb_driver;
187 static DECLARE_MUTEX(em2820_sysfs_lock);
188 static DECLARE_RWSEM(em2820_disconnect);
190 /********************* v4l2 interface ******************************************/
192 static inline unsigned long kvirt_to_pa(unsigned long adr)
194 unsigned long kva, ret;
196 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
197 kva |= adr & (PAGE_SIZE - 1);
204 * inits registers with sane defaults
206 static int em2820_config(struct em2820 *dev)
209 /* Sets I2C speed to 100 KHz */
210 em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
212 /* enable vbi capturing */
213 em2820_audio_usb_mute(dev, 1);
214 dev->mute = 1; /* maybe not the right place... */
216 em2820_audio_analog_set(dev);
217 em2820_audio_analog_setup(dev);
218 em2820_outfmt_set_yuv422(dev);
219 em2820_colorlevels_set_default(dev);
220 em2820_compression_disable(dev);
226 * em2820_config_i2c()
227 * configure i2c attached devices
229 void em2820_config_i2c(struct em2820 *dev)
231 struct v4l2_frequency f;
232 struct video_decoder_init em2820_vdi = {.data = NULL };
235 /* configure decoder */
236 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
237 em2820_vdi.data=saa7114_i2c_init;
238 em2820_vdi.len=sizeof(saa7114_i2c_init);
242 em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
243 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
244 /* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
245 /* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
246 /* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
247 /* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
249 /* configure tuner */
251 f.type = V4L2_TUNER_ANALOG_TV;
252 f.frequency = 9076; /* FIXME:remove magic number */
253 dev->ctl_freq = f.frequency;
254 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
256 /* configure tda9887 */
259 /* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
263 * em2820_empty_framequeues()
264 * prepare queues for incoming and outgoing frames
266 static void em2820_empty_framequeues(struct em2820 *dev)
270 INIT_LIST_HEAD(&dev->inqueue);
271 INIT_LIST_HEAD(&dev->outqueue);
273 for (i = 0; i < EM2820_NUM_FRAMES; i++) {
274 dev->frame[i].state = F_UNUSED;
275 dev->frame[i].buf.bytesused = 0;
281 * inits the device and starts isoc transfer
283 static int em2820_v4l2_open(struct inode *inode, struct file *filp)
285 int minor = iminor(inode);
287 struct em2820 *h,*dev = NULL;
288 struct list_head *list;
290 list_for_each(list,&em2820_devlist) {
291 h = list_entry(list, struct em2820, devlist);
292 if (h->vdev->minor == minor) {
297 filp->private_data=dev;
300 em2820_videodbg("users=%d", dev->users);
302 if (!down_read_trylock(&em2820_disconnect))
306 em2820_warn("this driver can be opened only once\n");
307 up_read(&em2820_disconnect);
311 /* if(dev->vbi_dev->minor == minor){
312 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
314 if (dev->vdev->minor == minor) {
315 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
318 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
319 spin_lock_init(&dev->queue_lock);
320 init_waitqueue_head(&dev->wait_frame);
321 init_waitqueue_head(&dev->wait_stream);
325 em2820_set_alternate(dev);
327 dev->width = norm_maxw(dev);
328 dev->height = norm_maxh(dev);
329 dev->frame_size = dev->width * dev->height * 2;
330 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
331 dev->bytesperline = dev->width * 2;
335 em2820_capture_start(dev, 1);
336 em2820_resolution_set(dev);
338 /* start the transfer */
339 errCode = em2820_init_isoc(dev);
344 filp->private_data = dev;
346 dev->stream = STREAM_OFF;
350 em2820_empty_framequeues(dev);
352 dev->state |= DEV_INITIALIZED;
356 up_read(&em2820_disconnect);
361 * em2820_realease_resources()
362 * unregisters the v4l2,i2c and usb devices
363 * called when the device gets disconected or at module unload
365 static void em2820_release_resources(struct em2820 *dev)
367 down(&em2820_sysfs_lock);
369 em2820_info("V4L2 device /dev/video%d deregistered\n",
371 list_del(&dev->devlist);
372 video_unregister_device(dev->vdev);
373 /* video_unregister_device(dev->vbi_dev); */
374 em2820_i2c_unregister(dev);
375 usb_put_dev(dev->udev);
376 up(&em2820_sysfs_lock);
380 * em2820_v4l2_close()
381 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
383 static int em2820_v4l2_close(struct inode *inode, struct file *filp)
386 struct em2820 *dev=filp->private_data;
388 em2820_videodbg("users=%d", dev->users);
392 em2820_uninit_isoc(dev);
394 em2820_release_buffers(dev);
396 /* the device is already disconnect, free the remaining resources */
397 if (dev->state & DEV_DISCONNECTED) {
398 em2820_release_resources(dev);
404 /* set alternate 0 */
406 em2820_videodbg("setting alternate 0");
407 errCode = usb_set_interface(dev->udev, 0, 0);
409 em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
414 wake_up_interruptible_nr(&dev->open, 1);
421 * will allocate buffers when called for the first time
424 em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
427 struct em2820_frame_t *f, *i;
428 unsigned long lock_flags;
430 struct em2820 *dev = filp->private_data;
432 if (down_interruptible(&dev->fileop_lock))
435 if (dev->state & DEV_DISCONNECTED) {
436 em2820_videodbg("device not present");
437 up(&dev->fileop_lock);
441 if (dev->state & DEV_MISCONFIGURED) {
442 em2820_videodbg("device misconfigured; close and open it again");
443 up(&dev->fileop_lock);
447 if (dev->io == IO_MMAP) {
448 em2820_videodbg ("IO method is set to mmap; close and open"
449 " the device again to choose the read method");
450 up(&dev->fileop_lock);
454 if (dev->io == IO_NONE) {
455 if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
456 em2820_errdev("read failed, not enough memory\n");
457 up(&dev->fileop_lock);
461 dev->stream = STREAM_ON;
462 em2820_queue_unusedframes(dev);
466 up(&dev->fileop_lock);
470 if (list_empty(&dev->outqueue)) {
471 if (filp->f_flags & O_NONBLOCK) {
472 up(&dev->fileop_lock);
475 ret = wait_event_interruptible
477 (!list_empty(&dev->outqueue)) ||
478 (dev->state & DEV_DISCONNECTED));
480 up(&dev->fileop_lock);
483 if (dev->state & DEV_DISCONNECTED) {
484 up(&dev->fileop_lock);
489 f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
491 spin_lock_irqsave(&dev->queue_lock, lock_flags);
492 list_for_each_entry(i, &dev->outqueue, frame)
494 INIT_LIST_HEAD(&dev->outqueue);
495 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
497 em2820_queue_unusedframes(dev);
499 if (count > f->buf.length)
500 count = f->buf.length;
502 if (copy_to_user(buf, f->bufmem, count)) {
503 up(&dev->fileop_lock);
508 up(&dev->fileop_lock);
515 * will allocate buffers when called for the first time
517 static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
519 unsigned int mask = 0;
520 struct em2820 *dev = filp->private_data;
522 if (down_interruptible(&dev->fileop_lock))
525 if (dev->state & DEV_DISCONNECTED) {
526 em2820_videodbg("device not present");
527 } else if (dev->state & DEV_MISCONFIGURED) {
528 em2820_videodbg("device is misconfigured; close and open it again");
530 if (dev->io == IO_NONE) {
531 if (!em2820_request_buffers
532 (dev, EM2820_NUM_READ_FRAMES)) {
534 ("poll() failed, not enough memory\n");
537 dev->stream = STREAM_ON;
541 if (dev->io == IO_READ) {
542 em2820_queue_unusedframes(dev);
543 poll_wait(filp, &dev->wait_frame, wait);
545 if (!list_empty(&dev->outqueue))
546 mask |= POLLIN | POLLRDNORM;
548 up(&dev->fileop_lock);
554 up(&dev->fileop_lock);
561 static void em2820_vm_open(struct vm_area_struct *vma)
563 struct em2820_frame_t *f = vma->vm_private_data;
570 static void em2820_vm_close(struct vm_area_struct *vma)
572 /* NOTE: buffers are not freed here */
573 struct em2820_frame_t *f = vma->vm_private_data;
577 static struct vm_operations_struct em2820_vm_ops = {
578 .open = em2820_vm_open,
579 .close = em2820_vm_close,
585 static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
587 unsigned long size = vma->vm_end - vma->vm_start,
588 start = vma->vm_start, pos, page;
591 struct em2820 *dev = filp->private_data;
593 if (down_interruptible(&dev->fileop_lock))
596 if (dev->state & DEV_DISCONNECTED) {
597 em2820_videodbg("mmap: device not present");
598 up(&dev->fileop_lock);
602 if (dev->state & DEV_MISCONFIGURED) {
603 em2820_videodbg ("mmap: Device is misconfigured; close and "
605 up(&dev->fileop_lock);
609 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
610 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
611 up(&dev->fileop_lock);
615 for (i = 0; i < dev->num_frames; i++) {
616 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
619 if (i == dev->num_frames) {
620 em2820_videodbg("mmap: user supplied mapping address is out of range");
621 up(&dev->fileop_lock);
625 /* VM_IO is eventually going to replace PageReserved altogether */
626 vma->vm_flags |= VM_IO;
627 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
629 pos = (unsigned long)dev->frame[i].bufmem;
630 while (size > 0) { /* size is page-aligned */
631 page = vmalloc_to_pfn((void *)pos);
632 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
633 vma->vm_page_prot)) {
634 em2820_videodbg("mmap: rename page map failed");
635 up(&dev->fileop_lock);
643 vma->vm_ops = &em2820_vm_ops;
644 vma->vm_private_data = &dev->frame[i];
647 up(&dev->fileop_lock);
653 * return the current saturation, brightness or contrast, mute state
655 static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
659 case V4L2_CID_AUDIO_MUTE:
660 ctrl->value = dev->mute;
662 case V4L2_CID_AUDIO_VOLUME:
663 ctrl->value = dev->volume;
665 case V4L2_CID_BRIGHTNESS:
666 if ((tmp = em2820_brightness_get(dev)) < 0)
668 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
670 case V4L2_CID_CONTRAST:
671 if ((ctrl->value = em2820_contrast_get(dev)) < 0)
674 case V4L2_CID_SATURATION:
675 if ((ctrl->value = em2820_saturation_get(dev)) < 0)
678 case V4L2_CID_RED_BALANCE:
679 if ((tmp = em2820_v_balance_get(dev)) < 0)
681 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
683 case V4L2_CID_BLUE_BALANCE:
684 if ((tmp = em2820_u_balance_get(dev)) < 0)
686 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
689 if ((ctrl->value = em2820_gamma_get(dev)) < 0)
699 * mute or set new saturation, brightness or contrast
701 static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
704 case V4L2_CID_AUDIO_MUTE:
705 if (ctrl->value != dev->mute) {
706 dev->mute = ctrl->value;
707 em2820_audio_usb_mute(dev, ctrl->value);
708 return em2820_audio_analog_set(dev);
711 case V4L2_CID_AUDIO_VOLUME:
712 dev->volume = ctrl->value;
713 return em2820_audio_analog_set(dev);
714 case V4L2_CID_BRIGHTNESS:
715 return em2820_brightness_set(dev, ctrl->value);
716 case V4L2_CID_CONTRAST:
717 return em2820_contrast_set(dev, ctrl->value);
718 case V4L2_CID_SATURATION:
719 return em2820_saturation_set(dev, ctrl->value);
720 case V4L2_CID_RED_BALANCE:
721 return em2820_v_balance_set(dev, ctrl->value);
722 case V4L2_CID_BLUE_BALANCE:
723 return em2820_u_balance_set(dev, ctrl->value);
725 return em2820_gamma_set(dev, ctrl->value);
732 * em2820_stream_interrupt()
735 static int em2820_stream_interrupt(struct em2820 *dev)
739 /* stop reading from the device */
741 dev->stream = STREAM_INTERRUPT;
742 ret = wait_event_timeout(dev->wait_stream,
743 (dev->stream == STREAM_OFF) ||
744 (dev->state & DEV_DISCONNECTED),
746 if (dev->state & DEV_DISCONNECTED)
749 dev->state |= DEV_MISCONFIGURED;
750 em2820_videodbg("device is misconfigured; close and "
751 "open /dev/video%d again", dev->vdev->minor);
758 static int em2820_set_norm(struct em2820 *dev, int width, int height)
760 unsigned int hscale, vscale;
761 unsigned int maxh, maxw;
763 maxw = norm_maxw(dev);
764 maxh = norm_maxh(dev);
766 /* width must even because of the YUYV format */
767 /* height must be even because of interlacing */
780 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
782 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
784 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
786 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
788 /* set new image size */
790 dev->height = height;
791 dev->frame_size = dev->width * dev->height * 2;
792 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
793 dev->bytesperline = dev->width * 2;
794 dev->hscale = hscale;
795 dev->vscale = vscale;
797 em2820_resolution_set(dev);
802 static void video_mux(struct em2820 *dev, int index)
806 input = INPUT(index)->vmux;
807 dev->ctl_input = index;
809 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
811 dev->ctl_ainput = INPUT(index)->amux;
813 switch (dev->ctl_ainput) {
815 ainput = EM2820_AUDIO_SRC_TUNER;
818 ainput = EM2820_AUDIO_SRC_LINE;
821 em2820_audio_source(dev, ainput);
825 * em2820_v4l2_do_ioctl()
826 * This function is _not_ called directly, but from
827 * em2820_v4l2_ioctl. Userspace
828 * copying is done already, arg is a kernel pointer.
830 static int em2820_do_ioctl(struct inode *inode, struct file *filp,
831 struct em2820 *dev, unsigned int cmd, void *arg,
832 v4l2_kioctl driver_ioctl)
837 /* ---------- tv norms ---------- */
840 struct v4l2_standard *e = arg;
846 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
847 tvnorms[e->index].name);
855 v4l2_std_id *id = arg;
857 *id = dev->tvnorm->id;
862 v4l2_std_id *id = arg;
865 for (i = 0; i < TVNORMS; i++)
866 if (*id == tvnorms[i].id)
869 for (i = 0; i < TVNORMS; i++)
870 if (*id & tvnorms[i].id)
876 dev->tvnorm = &tvnorms[i];
878 em2820_set_norm(dev, dev->width, dev->height);
881 dev->width=norm_maxw(dev);
882 dev->height=norm_maxh(dev);
883 dev->frame_size=dev->width*dev->height*2;
884 dev->field_size=dev->frame_size>>1;
885 dev->bytesperline=dev->width*2;
889 em2820_resolution_set(dev);
892 em2820_uninit_isoc(dev);
893 em2820_set_alternate(dev);
894 em2820_capture_start(dev, 1);
895 em2820_resolution_set(dev);
896 em2820_init_isoc(dev);
898 em2820_i2c_call_clients(dev, DECODER_SET_NORM,
900 em2820_i2c_call_clients(dev, VIDIOC_S_STD,
908 /* ------ input switching ---------- */
909 case VIDIOC_ENUMINPUT:
911 struct v4l2_input *i = arg;
913 static const char *iname[] = {
914 [EM2820_VMUX_COMPOSITE1] = "Composite1",
915 [EM2820_VMUX_COMPOSITE2] = "Composite2",
916 [EM2820_VMUX_COMPOSITE3] = "Composite3",
917 [EM2820_VMUX_COMPOSITE4] = "Composite4",
918 [EM2820_VMUX_SVIDEO] = "S-Video",
919 [EM2820_VMUX_TELEVISION] = "Television",
920 [EM2820_VMUX_CABLE] = "Cable TV",
921 [EM2820_VMUX_DVB] = "DVB",
922 [EM2820_VMUX_DEBUG] = "for debug only",
926 if (n >= MAX_EM2820_INPUT)
928 if (0 == INPUT(n)->type)
930 memset(i, 0, sizeof(*i));
932 i->type = V4L2_INPUT_TYPE_CAMERA;
933 strcpy(i->name, iname[INPUT(n)->type]);
934 if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
935 (EM2820_VMUX_CABLE == INPUT(n)->type))
936 i->type = V4L2_INPUT_TYPE_TUNER;
937 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
938 i->std |= tvnorms[n].id;
954 if (*index >= MAX_EM2820_INPUT)
956 if (0 == INPUT(*index)->type)
960 video_mux(dev, *index);
968 struct v4l2_audio *a = arg;
969 unsigned int index = a->index;
973 memset(a, 0, sizeof(*a));
974 index = dev->ctl_ainput;
977 strcpy(a->name, "Television");
979 strcpy(a->name, "Line In");
981 a->capability = V4L2_AUDCAP_STEREO;
988 struct v4l2_audio *a = arg;
989 if (a->index != dev->ctl_ainput)
995 /* --- controls ---------------------------------------------- */
996 case VIDIOC_QUERYCTRL:
998 struct v4l2_queryctrl *qc = arg;
1000 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
1001 for (i = 0; i < n; i++)
1002 if (qc->id && qc->id == em2820_qctrl[i].id) {
1003 memcpy(qc, &(em2820_qctrl[i]),
1013 struct v4l2_control *ctrl = arg;
1016 return em2820_get_ctrl(dev, ctrl);
1019 case VIDIOC_S_CTRL_OLD: /* ??? */
1022 struct v4l2_control *ctrl = arg;
1026 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
1027 for (i = 0; i < n; i++)
1028 if (ctrl->id == em2820_qctrl[i].id) {
1030 em2820_qctrl[i].minimum
1032 em2820_qctrl[i].maximum)
1035 return em2820_set_ctrl(dev, ctrl);
1040 /* --- tuner ioctls ------------------------------------------ */
1041 case VIDIOC_G_TUNER:
1043 struct v4l2_tuner *t = arg;
1049 memset(t, 0, sizeof(*t));
1050 strcpy(t->name, "Tuner");
1051 t->type = V4L2_TUNER_ANALOG_TV;
1052 t->capability = V4L2_TUNER_CAP_NORM;
1053 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1054 /* t->signal = 0xffff;*/
1055 /* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1056 /* No way to get signal strength? */
1058 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1062 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1064 em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1068 case VIDIOC_S_TUNER:
1070 struct v4l2_tuner *t = arg;
1075 memset(t, 0, sizeof(*t));
1076 strcpy(t->name, "Tuner");
1077 t->type = V4L2_TUNER_ANALOG_TV;
1078 t->capability = V4L2_TUNER_CAP_NORM;
1079 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1080 /* t->signal = 0xffff; */
1081 /* No way to get signal strength? */
1083 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1087 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1089 em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1093 case VIDIOC_G_FREQUENCY:
1095 struct v4l2_frequency *f = arg;
1097 memset(f, 0, sizeof(*f));
1098 f->type = V4L2_TUNER_ANALOG_TV;
1099 f->frequency = dev->ctl_freq;
1103 case VIDIOC_S_FREQUENCY:
1105 struct v4l2_frequency *f = arg;
1110 if (V4L2_TUNER_ANALOG_TV != f->type)
1114 dev->ctl_freq = f->frequency;
1115 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1120 case VIDIOC_CROPCAP:
1122 struct v4l2_cropcap *cc = arg;
1124 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1126 cc->bounds.left = 0;
1128 cc->bounds.width = dev->width;
1129 cc->bounds.height = dev->height;
1130 cc->defrect = cc->bounds;
1131 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1132 cc->pixelaspect.denominator = 59;
1135 case VIDIOC_STREAMON:
1139 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1140 || dev->io != IO_MMAP)
1143 if (list_empty(&dev->inqueue))
1146 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1148 em2820_videodbg("VIDIOC_STREAMON: starting stream");
1152 case VIDIOC_STREAMOFF:
1157 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1158 || dev->io != IO_MMAP)
1161 if (dev->stream == STREAM_ON) {
1162 em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1163 if ((ret = em2820_stream_interrupt(dev)))
1166 em2820_empty_framequeues(dev);
1171 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1178 * em2820_v4l2_do_ioctl()
1179 * This function is _not_ called directly, but from
1180 * em2820_v4l2_ioctl. Userspace
1181 * copying is done already, arg is a kernel pointer.
1183 static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
1184 unsigned int cmd, void *arg)
1186 struct em2820 *dev = filp->private_data;
1191 if (video_debug > 1)
1192 em2820_print_ioctl(dev->name,cmd);
1196 /* --- capabilities ------------------------------------------ */
1197 case VIDIOC_QUERYCAP:
1199 struct v4l2_capability *cap = arg;
1201 memset(cap, 0, sizeof(*cap));
1202 strlcpy(cap->driver, "em2820", sizeof(cap->driver));
1203 strlcpy(cap->card, em2820_boards[dev->model].name,
1205 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1206 sizeof(cap->bus_info));
1207 cap->version = EM2820_VERSION_CODE;
1209 V4L2_CAP_VIDEO_CAPTURE |
1211 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1213 cap->capabilities |= V4L2_CAP_TUNER;
1217 /* --- capture ioctls ---------------------------------------- */
1218 case VIDIOC_ENUM_FMT:
1220 struct v4l2_fmtdesc *fmtd = arg;
1222 if (fmtd->index != 0)
1224 memset(fmtd, 0, sizeof(*fmtd));
1225 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1226 strcpy(fmtd->description, "Packed YUY2");
1227 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1228 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1234 struct v4l2_format *format = arg;
1236 em2820_videodbg("VIDIOC_G_FMT: type=%s",
1238 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1239 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1240 V4L2_BUF_TYPE_VBI_CAPTURE ?
1241 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1244 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1247 format->fmt.pix.width = dev->width;
1248 format->fmt.pix.height = dev->height;
1249 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1250 format->fmt.pix.bytesperline = dev->bytesperline;
1251 format->fmt.pix.sizeimage = dev->frame_size;
1252 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1253 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1255 em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1260 case VIDIOC_TRY_FMT:
1263 struct v4l2_format *format = arg;
1266 int width = format->fmt.pix.width;
1267 int height = format->fmt.pix.height;
1268 unsigned int hscale, vscale;
1269 unsigned int maxh, maxw;
1271 maxw = norm_maxw(dev);
1272 maxh = norm_maxh(dev);
1274 /* int both_fields; */
1276 em2820_videodbg("%s: type=%s",
1278 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1281 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1282 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1283 V4L2_BUF_TYPE_VBI_CAPTURE ?
1284 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1287 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1290 em2820_videodbg("%s: requested %dx%d",
1292 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1293 "VIDIOC_S_FMT", format->fmt.pix.width,
1294 format->fmt.pix.height);
1296 /* FIXME: Move some code away from here */
1297 /* width must even because of the YUYV format */
1298 /* height must be even because of interlacing */
1313 /* we only know how to scale to 50% */
1314 if(height % (maxh / 2))
1316 if(width % (maxw / 2))
1318 /* larger resoltion don't seem to work either */
1319 if(width == maxw && height == maxh)
1324 (((unsigned long)maxw) << 12) / width - 4096L) >=
1328 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1331 (((unsigned long)maxh) << 12) / height - 4096L) >=
1335 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1337 format->fmt.pix.width = width;
1338 format->fmt.pix.height = height;
1339 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1340 format->fmt.pix.bytesperline = width * 2;
1341 format->fmt.pix.sizeimage = width * 2 * height;
1342 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1343 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1345 em2820_videodbg("%s: returned %dx%d (%d, %d)",
1347 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1348 "VIDIOC_S_FMT", format->fmt.pix.width,
1349 format->fmt.pix.height, hscale, vscale);
1351 if (cmd == VIDIOC_TRY_FMT)
1354 for (i = 0; i < dev->num_frames; i++)
1355 if (dev->frame[i].vma_use_count) {
1356 em2820_videodbg("VIDIOC_S_FMT failed. "
1357 "Unmap the buffers first.");
1361 /* stop io in case it is already in progress */
1362 if (dev->stream == STREAM_ON) {
1363 em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
1364 if ((ret = em2820_stream_interrupt(dev)))
1368 em2820_release_buffers(dev);
1371 /* set new image size */
1373 dev->height = height;
1374 dev->frame_size = dev->width * dev->height * 2;
1375 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1376 dev->bytesperline = dev->width * 2;
1377 dev->hscale = hscale;
1378 dev->vscale = vscale;
1379 /* dev->both_fileds = both_fileds; */
1380 em2820_uninit_isoc(dev);
1381 em2820_set_alternate(dev);
1382 em2820_capture_start(dev, 1);
1383 em2820_resolution_set(dev);
1384 em2820_init_isoc(dev);
1389 /* --- streaming capture ------------------------------------- */
1390 case VIDIOC_REQBUFS:
1392 struct v4l2_requestbuffers *rb = arg;
1396 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1397 rb->memory != V4L2_MEMORY_MMAP)
1400 if (dev->io == IO_READ) {
1401 em2820_videodbg ("method is set to read;"
1402 " close and open the device again to"
1403 " choose the mmap I/O method");
1407 for (i = 0; i < dev->num_frames; i++)
1408 if (dev->frame[i].vma_use_count) {
1409 em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1413 if (dev->stream == STREAM_ON) {
1414 em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
1415 if ((ret = em2820_stream_interrupt(dev)))
1419 em2820_empty_framequeues(dev);
1421 em2820_release_buffers(dev);
1424 em2820_request_buffers(dev, rb->count);
1426 dev->frame_current = NULL;
1428 em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1430 dev->io = rb->count ? IO_MMAP : IO_NONE;
1434 case VIDIOC_QUERYBUF:
1436 struct v4l2_buffer *b = arg;
1438 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1439 b->index >= dev->num_frames || dev->io != IO_MMAP)
1442 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1444 if (dev->frame[b->index].vma_use_count) {
1445 b->flags |= V4L2_BUF_FLAG_MAPPED;
1447 if (dev->frame[b->index].state == F_DONE)
1448 b->flags |= V4L2_BUF_FLAG_DONE;
1449 else if (dev->frame[b->index].state != F_UNUSED)
1450 b->flags |= V4L2_BUF_FLAG_QUEUED;
1455 struct v4l2_buffer *b = arg;
1456 unsigned long lock_flags;
1458 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1459 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1463 if (dev->frame[b->index].state != F_UNUSED) {
1466 dev->frame[b->index].state = F_QUEUED;
1468 /* add frame to fifo */
1469 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1470 list_add_tail(&dev->frame[b->index].frame,
1472 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1478 struct v4l2_buffer *b = arg;
1479 struct em2820_frame_t *f;
1480 unsigned long lock_flags;
1483 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1484 || dev->io != IO_MMAP)
1487 if (list_empty(&dev->outqueue)) {
1488 if (dev->stream == STREAM_OFF)
1490 if (filp->f_flags & O_NONBLOCK)
1492 ret = wait_event_interruptible
1494 (!list_empty(&dev->outqueue)) ||
1495 (dev->state & DEV_DISCONNECTED));
1498 if (dev->state & DEV_DISCONNECTED)
1502 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1503 f = list_entry(dev->outqueue.next,
1504 struct em2820_frame_t, frame);
1505 list_del(dev->outqueue.next);
1506 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1508 f->state = F_UNUSED;
1509 memcpy(b, &f->buf, sizeof(*b));
1511 if (f->vma_use_count)
1512 b->flags |= V4L2_BUF_FLAG_MAPPED;
1517 return em2820_do_ioctl(inode, filp, dev, cmd, arg,
1518 em2820_video_do_ioctl);
1524 * em2820_v4l2_ioctl()
1525 * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
1527 static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
1528 unsigned int cmd, unsigned long arg)
1531 struct em2820 *dev = filp->private_data;
1533 if (down_interruptible(&dev->fileop_lock))
1534 return -ERESTARTSYS;
1536 if (dev->state & DEV_DISCONNECTED) {
1537 em2820_errdev("v4l2 ioctl: device not present\n");
1538 up(&dev->fileop_lock);
1542 if (dev->state & DEV_MISCONFIGURED) {
1544 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1545 up(&dev->fileop_lock);
1549 ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
1551 up(&dev->fileop_lock);
1556 static struct file_operations em2820_v4l_fops = {
1557 .owner = THIS_MODULE,
1558 .open = em2820_v4l2_open,
1559 .release = em2820_v4l2_close,
1560 .ioctl = em2820_v4l2_ioctl,
1561 .read = em2820_v4l2_read,
1562 .poll = em2820_v4l2_poll,
1563 .mmap = em2820_v4l2_mmap,
1564 .llseek = no_llseek,
1567 /******************************** usb interface *****************************************/
1571 * allocates and inits the device structs, registers i2c bus and v4l device
1573 static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
1574 int minor, int model)
1576 struct em2820 *dev = *devhandle;
1577 int retval = -ENOMEM;
1579 unsigned int maxh, maxw;
1580 struct usb_interface *uif;
1584 init_MUTEX(&dev->lock);
1585 init_waitqueue_head(&dev->open);
1587 dev->em2820_write_regs = em2820_write_regs;
1588 dev->em2820_read_reg = em2820_read_reg;
1589 dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
1590 dev->em2820_write_regs_req = em2820_write_regs_req;
1591 dev->em2820_read_reg_req = em2820_read_reg_req;
1592 dev->is_em2800 = em2820_boards[model].is_em2800;
1593 dev->has_tuner = em2820_boards[model].has_tuner;
1594 dev->has_msp34xx = em2820_boards[model].has_msp34xx;
1595 dev->tda9887_conf = em2820_boards[model].tda9887_conf;
1596 dev->decoder = em2820_boards[model].decoder;
1599 dev->tuner_type = tuner;
1601 dev->tuner_type = em2820_boards[model].tuner_type;
1603 dev->video_inputs = em2820_boards[model].vchannels;
1605 for (i = 0; i < TVNORMS; i++)
1606 if (em2820_boards[model].norm == tvnorms[i].mode)
1611 dev->tvnorm = &tvnorms[i]; /* set default norm */
1613 em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1615 maxw = norm_maxw(dev);
1616 maxh = norm_maxh(dev);
1618 /* set default image size */
1621 dev->interlaced = EM2820_INTERLACED_DEFAULT;
1622 dev->field_size = dev->width * dev->height;
1624 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1625 dev->bytesperline = dev->width * 2;
1630 /* setup video picture settings for saa7113h */
1631 memset(&dev->vpic, 0, sizeof(dev->vpic));
1632 dev->vpic.colour = 128 << 8;
1633 dev->vpic.hue = 128 << 8;
1634 dev->vpic.brightness = 128 << 8;
1635 dev->vpic.contrast = 192 << 8;
1636 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1637 dev->vpic.depth = 16;
1638 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1640 /* compute alternate max packet sizes */
1641 uif = dev->udev->actconfig->interface[0];
1642 dev->alt_max_pkt_size[0] = 0;
1643 for (i = 1; i <= EM2820_MAX_ALT && i < uif->num_altsetting ; i++) {
1645 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1647 dev->alt_max_pkt_size[i] =
1648 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1651 #ifdef CONFIG_MODULES
1652 /* request some modules */
1653 if (dev->decoder == EM2820_SAA7113 || dev->decoder == EM2820_SAA7114)
1654 request_module("saa7113");
1655 if (dev->decoder == EM2820_TVP5150)
1656 request_module("tvp5150");
1658 request_module("tuner");
1659 if (dev->tda9887_conf)
1660 request_module("tda9887");
1662 errCode = em2820_config(dev);
1664 em2820_errdev("error configuring device\n");
1670 /* register i2c bus */
1671 em2820_i2c_register(dev);
1673 /* Do board specific init and eeprom reading */
1674 em2820_card_setup(dev);
1676 /* configure the device */
1677 em2820_config_i2c(dev);
1681 errCode = em2820_config(dev);
1683 #ifdef CONFIG_MODULES
1684 if (dev->has_msp34xx)
1685 request_module("msp3400");
1687 /* allocate and fill v4l2 device struct */
1688 dev->vdev = video_device_alloc();
1689 if (NULL == dev->vdev) {
1690 em2820_errdev("cannot allocate video_device.\n");
1695 dev->vdev->type = VID_TYPE_CAPTURE;
1697 dev->vdev->type |= VID_TYPE_TUNER;
1698 dev->vdev->hardware = 0;
1699 dev->vdev->fops = &em2820_v4l_fops;
1700 dev->vdev->minor = -1;
1701 dev->vdev->dev = &dev->udev->dev;
1702 dev->vdev->release = video_device_release;
1703 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1705 list_add_tail(&dev->devlist,&em2820_devlist);
1707 /* register v4l2 device */
1709 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1710 em2820_errdev("unable to register video device (error=%i).\n",
1713 list_del(&dev->devlist);
1714 video_device_release(dev->vdev);
1718 if (dev->has_msp34xx) {
1719 /* Send a reset to other chips via gpio */
1720 em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1722 em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1730 em2820_info("V4L2 device registered as /dev/video%d\n",
1737 * em2820_usb_probe()
1738 * checks for supported devices
1740 static int em2820_usb_probe(struct usb_interface *interface,
1741 const struct usb_device_id *id)
1743 const struct usb_endpoint_descriptor *endpoint;
1744 struct usb_device *udev;
1745 struct em2820 *dev = NULL;
1746 int retval = -ENODEV;
1747 int model,i,nr,ifnum;
1749 udev = usb_get_dev(interface_to_usbdev(interface));
1750 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1752 em2820_err(DRIVER_NAME " new device (%04x:%04x): interface %i, class %i\n",
1753 udev->descriptor.idVendor,udev->descriptor.idProduct,
1755 interface->altsetting[0].desc.bInterfaceClass);
1757 /* Don't register audio interfaces */
1758 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
1761 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1763 /* check if the the device has the iso in endpoint at the correct place */
1764 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1765 USB_ENDPOINT_XFER_ISOC) {
1766 em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1769 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1770 em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1774 model=id->driver_info;
1775 nr=interface->minor;
1777 if (nr>EM2820_MAXBOARDS) {
1778 printk ("em2820: Supports only %i em28xx boards.\n",EM2820_MAXBOARDS);
1782 /* allocate memory for our device state and initialize it */
1783 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1785 em2820_err(DRIVER_NAME ": out of memory!\n");
1788 memset(dev, 0, sizeof(*dev));
1790 snprintf(dev->name, 29, "em2820 #%d", nr);
1792 if ((card[nr]>=0)&&(card[nr]<em2820_bcount))
1795 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1796 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1797 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1798 "%s: workaround that. Redirect complaints to the vendor of\n"
1799 "%s: the TV card. Best regards,\n"
1801 dev->name,dev->name,dev->name,dev->name,dev->name);
1802 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1804 for (i = 0; i < em2820_bcount; i++) {
1805 printk("%s: card=%d -> %s\n",
1806 dev->name, i, em2820_boards[i].name);
1810 /* allocate device struct */
1811 retval = em2820_init_dev(&dev, udev, nr, model);
1815 em2820_info("Found %s\n", em2820_boards[model].name);
1817 /* save our data pointer in this interface device */
1818 usb_set_intfdata(interface, dev);
1823 * em2820_usb_disconnect()
1824 * called when the device gets diconencted
1825 * video device will be unregistered on v4l2_close in case it is still open
1827 static void em2820_usb_disconnect(struct usb_interface *interface)
1829 struct em2820 *dev = usb_get_intfdata(interface);
1830 usb_set_intfdata(interface, NULL);
1835 down_write(&em2820_disconnect);
1839 em2820_info("disconnecting %s\n", dev->vdev->name);
1841 wake_up_interruptible_all(&dev->open);
1845 ("device /dev/video%d is open! Deregistration and memory "
1846 "deallocation are deferred on close.\n", dev->vdev->minor);
1847 dev->state |= DEV_MISCONFIGURED;
1848 em2820_uninit_isoc(dev);
1849 dev->state |= DEV_DISCONNECTED;
1850 wake_up_interruptible(&dev->wait_frame);
1851 wake_up_interruptible(&dev->wait_stream);
1853 dev->state |= DEV_DISCONNECTED;
1854 em2820_release_resources(dev);
1862 up_write(&em2820_disconnect);
1866 static struct usb_driver em2820_usb_driver = {
1867 .owner = THIS_MODULE,
1869 .probe = em2820_usb_probe,
1870 .disconnect = em2820_usb_disconnect,
1871 .id_table = em2820_id_table,
1874 static int __init em2820_module_init(void)
1878 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1879 (EM2820_VERSION_CODE >> 16) & 0xff,
1880 (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
1882 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1883 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1886 /* register this driver with the USB subsystem */
1887 result = usb_register(&em2820_usb_driver);
1889 em2820_err(DRIVER_NAME
1890 " usb_register failed. Error number %d.\n", result);
1895 static void __exit em2820_module_exit(void)
1897 /* deregister this driver with the USB subsystem */
1898 usb_deregister(&em2820_usb_driver);
1901 module_init(em2820_module_init);
1902 module_exit(em2820_module_exit);