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 #define TVNORMS ARRAY_SIZE(tvnorms)
91 /* supported controls */
92 static struct v4l2_queryctrl em2820_qctrl[] = {
94 .id = V4L2_CID_BRIGHTNESS,
95 .type = V4L2_CTRL_TYPE_INTEGER,
103 .id = V4L2_CID_CONTRAST,
104 .type = V4L2_CTRL_TYPE_INTEGER,
109 .default_value = 0x10,
112 .id = V4L2_CID_SATURATION,
113 .type = V4L2_CTRL_TYPE_INTEGER,
114 .name = "Saturation",
118 .default_value = 0x10,
121 .id = V4L2_CID_AUDIO_VOLUME,
122 .type = V4L2_CTRL_TYPE_INTEGER,
127 .default_value = 0x1f,
130 .id = V4L2_CID_AUDIO_MUTE,
131 .type = V4L2_CTRL_TYPE_BOOLEAN,
139 .id = V4L2_CID_RED_BALANCE,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "Red chroma balance",
148 .id = V4L2_CID_BLUE_BALANCE,
149 .type = V4L2_CTRL_TYPE_INTEGER,
150 .name = "Blue chroma balance",
157 .id = V4L2_CID_GAMMA,
158 .type = V4L2_CTRL_TYPE_INTEGER,
163 .default_value = 0x20,
168 static struct usb_driver em2820_usb_driver;
170 static DECLARE_MUTEX(em2820_sysfs_lock);
171 static DECLARE_RWSEM(em2820_disconnect);
173 /********************* v4l2 interface ******************************************/
175 static inline unsigned long kvirt_to_pa(unsigned long adr)
177 unsigned long kva, ret;
179 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
180 kva |= adr & (PAGE_SIZE - 1);
187 * inits registers with sane defaults
189 static int em2820_config(struct em2820 *dev)
192 /* Sets I2C speed to 100 KHz */
193 em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
195 /* enable vbi capturing */
196 em2820_audio_usb_mute(dev, 1);
197 dev->mute = 1; /* maybe not the right place... */
199 em2820_audio_analog_set(dev);
200 em2820_audio_analog_setup(dev);
201 em2820_outfmt_set_yuv422(dev);
202 em2820_colorlevels_set_default(dev);
203 em2820_compression_disable(dev);
209 * em2820_config_i2c()
210 * configure i2c attached devices
212 void em2820_config_i2c(struct em2820 *dev)
214 struct v4l2_frequency f;
215 struct video_decoder_init em2820_vdi = {.data = NULL };
218 /* configure decoder */
219 em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
220 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
221 /* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
222 /* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
223 /* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
224 /* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
226 /* configure tuner */
228 f.type = V4L2_TUNER_ANALOG_TV;
229 f.frequency = 9076; /* FIXME:remove magic number */
230 dev->ctl_freq = f.frequency;
231 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
233 /* configure tda9887 */
236 /* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
240 * em2820_empty_framequeues()
241 * prepare queues for incoming and outgoing frames
243 static void em2820_empty_framequeues(struct em2820 *dev)
247 INIT_LIST_HEAD(&dev->inqueue);
248 INIT_LIST_HEAD(&dev->outqueue);
250 for (i = 0; i < EM2820_NUM_FRAMES; i++) {
251 dev->frame[i].state = F_UNUSED;
252 dev->frame[i].buf.bytesused = 0;
258 * inits the device and starts isoc transfer
260 static int em2820_v4l2_open(struct inode *inode, struct file *filp)
262 int minor = iminor(inode);
264 struct em2820 *h,*dev = NULL;
265 struct list_head *list;
267 list_for_each(list,&em2820_devlist) {
268 h = list_entry(list, struct em2820, devlist);
269 if (h->vdev->minor == minor) {
274 filp->private_data=dev;
277 em2820_videodbg("users=%d", dev->users);
279 if (!down_read_trylock(&em2820_disconnect))
283 em2820_warn("this driver can be opened only once\n");
284 up_read(&em2820_disconnect);
288 /* if(dev->vbi_dev->minor == minor){
289 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
291 if (dev->vdev->minor == minor) {
292 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
295 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
296 spin_lock_init(&dev->queue_lock);
297 init_waitqueue_head(&dev->wait_frame);
298 init_waitqueue_head(&dev->wait_stream);
302 em2820_set_alternate(dev);
304 dev->width = norm_maxw(dev);
305 dev->height = norm_maxh(dev);
306 dev->frame_size = dev->width * dev->height * 2;
307 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
308 dev->bytesperline = dev->width * 2;
312 em2820_capture_start(dev, 1);
313 em2820_resolution_set(dev);
315 /* start the transfer */
316 errCode = em2820_init_isoc(dev);
321 filp->private_data = dev;
323 dev->stream = STREAM_OFF;
327 em2820_empty_framequeues(dev);
329 dev->state |= DEV_INITIALIZED;
333 up_read(&em2820_disconnect);
338 * em2820_realease_resources()
339 * unregisters the v4l2,i2c and usb devices
340 * called when the device gets disconected or at module unload
342 static void em2820_release_resources(struct em2820 *dev)
344 down(&em2820_sysfs_lock);
346 em2820_info("V4L2 device /dev/video%d deregistered\n",
348 list_del(&dev->devlist);
349 video_unregister_device(dev->vdev);
350 /* video_unregister_device(dev->vbi_dev); */
351 em2820_i2c_unregister(dev);
352 usb_put_dev(dev->udev);
353 up(&em2820_sysfs_lock);
357 * em2820_v4l2_close()
358 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
360 static int em2820_v4l2_close(struct inode *inode, struct file *file)
363 int minor = iminor(inode);
364 struct em2820 *h,*dev = NULL;
365 struct list_head *list;
367 list_for_each(list,&em2820_devlist) {
368 h = list_entry(list, struct em2820, devlist);
369 if (h->vdev->minor == minor) {
375 em2820_videodbg("users=%d", dev->users);
379 em2820_uninit_isoc(dev);
381 em2820_release_buffers(dev);
383 /* the device is already disconnect, free the remaining resources */
384 if (dev->state & DEV_DISCONNECTED) {
385 em2820_release_resources(dev);
391 /* set alternate 0 */
393 em2820_videodbg("setting alternate 0");
394 errCode = usb_set_interface(dev->udev, 0, 0);
396 em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
401 wake_up_interruptible_nr(&dev->open, 1);
408 * will allocate buffers when called for the first time
411 em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
414 struct em2820_frame_t *f, *i;
415 unsigned long lock_flags;
417 struct em2820 *dev = filp->private_data;
419 if (down_interruptible(&dev->fileop_lock))
422 if (dev->state & DEV_DISCONNECTED) {
423 em2820_videodbg("device not present");
424 up(&dev->fileop_lock);
428 if (dev->state & DEV_MISCONFIGURED) {
429 em2820_videodbg("device misconfigured; close and open it again");
430 up(&dev->fileop_lock);
434 if (dev->io == IO_MMAP) {
435 em2820_videodbg ("IO method is set to mmap; close and open"
436 " the device again to choose the read method");
437 up(&dev->fileop_lock);
441 if (dev->io == IO_NONE) {
442 if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
443 em2820_errdev("read failed, not enough memory\n");
444 up(&dev->fileop_lock);
448 dev->stream = STREAM_ON;
449 em2820_queue_unusedframes(dev);
453 up(&dev->fileop_lock);
457 if (list_empty(&dev->outqueue)) {
458 if (filp->f_flags & O_NONBLOCK) {
459 up(&dev->fileop_lock);
462 ret = wait_event_interruptible
464 (!list_empty(&dev->outqueue)) ||
465 (dev->state & DEV_DISCONNECTED));
467 up(&dev->fileop_lock);
470 if (dev->state & DEV_DISCONNECTED) {
471 up(&dev->fileop_lock);
476 f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
478 spin_lock_irqsave(&dev->queue_lock, lock_flags);
479 list_for_each_entry(i, &dev->outqueue, frame)
481 INIT_LIST_HEAD(&dev->outqueue);
482 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
484 em2820_queue_unusedframes(dev);
486 if (count > f->buf.length)
487 count = f->buf.length;
489 if (copy_to_user(buf, f->bufmem, count)) {
490 up(&dev->fileop_lock);
495 up(&dev->fileop_lock);
502 * will allocate buffers when called for the first time
504 static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
506 unsigned int mask = 0;
507 struct em2820 *dev = filp->private_data;
509 if (down_interruptible(&dev->fileop_lock))
512 if (dev->state & DEV_DISCONNECTED) {
513 em2820_videodbg("device not present");
514 } else if (dev->state & DEV_MISCONFIGURED) {
515 em2820_videodbg("device is misconfigured; close and open it again");
517 if (dev->io == IO_NONE) {
518 if (!em2820_request_buffers
519 (dev, EM2820_NUM_READ_FRAMES)) {
521 ("poll() failed, not enough memory\n");
524 dev->stream = STREAM_ON;
528 if (dev->io == IO_READ) {
529 em2820_queue_unusedframes(dev);
530 poll_wait(filp, &dev->wait_frame, wait);
532 if (!list_empty(&dev->outqueue))
533 mask |= POLLIN | POLLRDNORM;
535 up(&dev->fileop_lock);
541 up(&dev->fileop_lock);
548 static void em2820_vm_open(struct vm_area_struct *vma)
550 struct em2820_frame_t *f = vma->vm_private_data;
557 static void em2820_vm_close(struct vm_area_struct *vma)
559 /* NOTE: buffers are not freed here */
560 struct em2820_frame_t *f = vma->vm_private_data;
564 static struct vm_operations_struct em2820_vm_ops = {
565 .open = em2820_vm_open,
566 .close = em2820_vm_close,
572 static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
574 unsigned long size = vma->vm_end - vma->vm_start,
575 start = vma->vm_start, pos, page;
578 struct em2820 *dev = filp->private_data;
580 if (down_interruptible(&dev->fileop_lock))
583 if (dev->state & DEV_DISCONNECTED) {
584 em2820_videodbg("mmap: device not present");
585 up(&dev->fileop_lock);
589 if (dev->state & DEV_MISCONFIGURED) {
590 em2820_videodbg ("mmap: Device is misconfigured; close and "
592 up(&dev->fileop_lock);
596 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
597 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
598 up(&dev->fileop_lock);
602 for (i = 0; i < dev->num_frames; i++) {
603 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
606 if (i == dev->num_frames) {
607 em2820_videodbg("mmap: user supplied mapping address is out of range");
608 up(&dev->fileop_lock);
612 /* VM_IO is eventually going to replace PageReserved altogether */
613 vma->vm_flags |= VM_IO;
614 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
616 pos = (unsigned long)dev->frame[i].bufmem;
617 while (size > 0) { /* size is page-aligned */
618 page = vmalloc_to_pfn((void *)pos);
619 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
620 vma->vm_page_prot)) {
621 em2820_videodbg("mmap: rename page map failed");
622 up(&dev->fileop_lock);
630 vma->vm_ops = &em2820_vm_ops;
631 vma->vm_private_data = &dev->frame[i];
634 up(&dev->fileop_lock);
640 * return the current saturation, brightness or contrast, mute state
642 static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
646 case V4L2_CID_AUDIO_MUTE:
647 ctrl->value = dev->mute;
649 case V4L2_CID_AUDIO_VOLUME:
650 ctrl->value = dev->volume;
652 case V4L2_CID_BRIGHTNESS:
653 if ((tmp = em2820_brightness_get(dev)) < 0)
655 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
657 case V4L2_CID_CONTRAST:
658 if ((ctrl->value = em2820_contrast_get(dev)) < 0)
661 case V4L2_CID_SATURATION:
662 if ((ctrl->value = em2820_saturation_get(dev)) < 0)
665 case V4L2_CID_RED_BALANCE:
666 if ((tmp = em2820_v_balance_get(dev)) < 0)
668 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
670 case V4L2_CID_BLUE_BALANCE:
671 if ((tmp = em2820_u_balance_get(dev)) < 0)
673 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
676 if ((ctrl->value = em2820_gamma_get(dev)) < 0)
686 * mute or set new saturation, brightness or contrast
688 static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
691 case V4L2_CID_AUDIO_MUTE:
692 if (ctrl->value != dev->mute) {
693 dev->mute = ctrl->value;
694 em2820_audio_usb_mute(dev, ctrl->value);
695 return em2820_audio_analog_set(dev);
698 case V4L2_CID_AUDIO_VOLUME:
699 dev->volume = ctrl->value;
700 return em2820_audio_analog_set(dev);
701 case V4L2_CID_BRIGHTNESS:
702 return em2820_brightness_set(dev, ctrl->value);
703 case V4L2_CID_CONTRAST:
704 return em2820_contrast_set(dev, ctrl->value);
705 case V4L2_CID_SATURATION:
706 return em2820_saturation_set(dev, ctrl->value);
707 case V4L2_CID_RED_BALANCE:
708 return em2820_v_balance_set(dev, ctrl->value);
709 case V4L2_CID_BLUE_BALANCE:
710 return em2820_u_balance_set(dev, ctrl->value);
712 return em2820_gamma_set(dev, ctrl->value);
719 * em2820_stream_interrupt()
722 static int em2820_stream_interrupt(struct em2820 *dev)
726 /* stop reading from the device */
728 dev->stream = STREAM_INTERRUPT;
729 ret = wait_event_timeout(dev->wait_stream,
730 (dev->stream == STREAM_OFF) ||
731 (dev->state & DEV_DISCONNECTED),
733 if (dev->state & DEV_DISCONNECTED)
736 dev->state |= DEV_MISCONFIGURED;
737 em2820_videodbg("device is misconfigured; close and "
738 "open /dev/video%d again", dev->vdev->minor);
745 static int em2820_set_norm(struct em2820 *dev, int width, int height)
747 unsigned int hscale, vscale;
748 unsigned int maxh, maxw;
750 maxw = norm_maxw(dev);
751 maxh = norm_maxh(dev);
753 /* width must even because of the YUYV format */
754 /* height must be even because of interlacing */
767 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
769 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
771 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
773 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
775 /* set new image size */
777 dev->height = height;
778 dev->frame_size = dev->width * dev->height * 2;
779 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
780 dev->bytesperline = dev->width * 2;
781 dev->hscale = hscale;
782 dev->vscale = vscale;
784 em2820_resolution_set(dev);
789 static void video_mux(struct em2820 *dev, int index)
793 input = INPUT(index)->vmux;
794 dev->ctl_input = index;
796 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
798 dev->ctl_ainput = INPUT(index)->amux;
800 switch (dev->ctl_ainput) {
802 ainput = EM2820_AUDIO_SRC_TUNER;
805 ainput = EM2820_AUDIO_SRC_LINE;
808 em2820_audio_source(dev, ainput);
812 * em2820_v4l2_do_ioctl()
813 * This function is _not_ called directly, but from
814 * em2820_v4l2_ioctl. Userspace
815 * copying is done already, arg is a kernel pointer.
817 static int em2820_do_ioctl(struct inode *inode, struct file *filp,
818 struct em2820 *dev, unsigned int cmd, void *arg,
819 v4l2_kioctl driver_ioctl)
824 /* ---------- tv norms ---------- */
827 struct v4l2_standard *e = arg;
833 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
834 tvnorms[e->index].name);
842 v4l2_std_id *id = arg;
844 *id = dev->tvnorm->id;
849 v4l2_std_id *id = arg;
852 for (i = 0; i < TVNORMS; i++)
853 if (*id == tvnorms[i].id)
856 for (i = 0; i < TVNORMS; i++)
857 if (*id & tvnorms[i].id)
863 dev->tvnorm = &tvnorms[i];
865 em2820_set_norm(dev, dev->width, dev->height);
868 dev->width=norm_maxw(dev);
869 dev->height=norm_maxh(dev);
870 dev->frame_size=dev->width*dev->height*2;
871 dev->field_size=dev->frame_size>>1;
872 dev->bytesperline=dev->width*2;
876 em2820_resolution_set(dev);
879 em2820_uninit_isoc(dev);
880 em2820_set_alternate(dev);
881 em2820_capture_start(dev, 1);
882 em2820_resolution_set(dev);
883 em2820_init_isoc(dev);
885 em2820_i2c_call_clients(dev, DECODER_SET_NORM,
887 em2820_i2c_call_clients(dev, VIDIOC_S_STD,
895 /* ------ input switching ---------- */
896 case VIDIOC_ENUMINPUT:
898 struct v4l2_input *i = arg;
900 static const char *iname[] = {
901 [EM2820_VMUX_COMPOSITE1] = "Composite1",
902 [EM2820_VMUX_COMPOSITE2] = "Composite2",
903 [EM2820_VMUX_COMPOSITE3] = "Composite3",
904 [EM2820_VMUX_COMPOSITE4] = "Composite4",
905 [EM2820_VMUX_SVIDEO] = "S-Video",
906 [EM2820_VMUX_TELEVISION] = "Television",
907 [EM2820_VMUX_CABLE] = "Cable TV",
908 [EM2820_VMUX_DVB] = "DVB",
909 [EM2820_VMUX_DEBUG] = "for debug only",
913 if (n >= MAX_EM2820_INPUT)
915 if (0 == INPUT(n)->type)
917 memset(i, 0, sizeof(*i));
919 i->type = V4L2_INPUT_TYPE_CAMERA;
920 strcpy(i->name, iname[INPUT(n)->type]);
921 if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
922 (EM2820_VMUX_CABLE == INPUT(n)->type))
923 i->type = V4L2_INPUT_TYPE_TUNER;
924 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
925 i->std |= tvnorms[n].id;
941 if (*index >= MAX_EM2820_INPUT)
943 if (0 == INPUT(*index)->type)
947 video_mux(dev, *index);
955 struct v4l2_audio *a = arg;
956 unsigned int index = a->index;
960 memset(a, 0, sizeof(*a));
961 index = dev->ctl_ainput;
964 strcpy(a->name, "Television");
966 strcpy(a->name, "Line In");
968 a->capability = V4L2_AUDCAP_STEREO;
975 struct v4l2_audio *a = arg;
976 if (a->index != dev->ctl_ainput)
982 /* --- controls ---------------------------------------------- */
983 case VIDIOC_QUERYCTRL:
985 struct v4l2_queryctrl *qc = arg;
987 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
988 for (i = 0; i < n; i++)
989 if (qc->id && qc->id == em2820_qctrl[i].id) {
990 memcpy(qc, &(em2820_qctrl[i]),
1000 struct v4l2_control *ctrl = arg;
1003 return em2820_get_ctrl(dev, ctrl);
1006 case VIDIOC_S_CTRL_OLD: /* ??? */
1009 struct v4l2_control *ctrl = arg;
1013 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
1014 for (i = 0; i < n; i++)
1015 if (ctrl->id == em2820_qctrl[i].id) {
1017 em2820_qctrl[i].minimum
1019 em2820_qctrl[i].maximum)
1022 return em2820_set_ctrl(dev, ctrl);
1027 /* --- tuner ioctls ------------------------------------------ */
1028 case VIDIOC_G_TUNER:
1030 struct v4l2_tuner *t = arg;
1036 memset(t, 0, sizeof(*t));
1037 strcpy(t->name, "Tuner");
1038 t->type = V4L2_TUNER_ANALOG_TV;
1039 t->capability = V4L2_TUNER_CAP_NORM;
1040 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1041 /* t->signal = 0xffff;*/
1042 /* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1043 /* No way to get signal strength? */
1045 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1049 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1051 em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1055 case VIDIOC_S_TUNER:
1057 struct v4l2_tuner *t = arg;
1062 memset(t, 0, sizeof(*t));
1063 strcpy(t->name, "Tuner");
1064 t->type = V4L2_TUNER_ANALOG_TV;
1065 t->capability = V4L2_TUNER_CAP_NORM;
1066 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1067 /* t->signal = 0xffff; */
1068 /* No way to get signal strength? */
1070 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1074 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1076 em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1080 case VIDIOC_G_FREQUENCY:
1082 struct v4l2_frequency *f = arg;
1084 memset(f, 0, sizeof(*f));
1085 f->type = V4L2_TUNER_ANALOG_TV;
1086 f->frequency = dev->ctl_freq;
1090 case VIDIOC_S_FREQUENCY:
1092 struct v4l2_frequency *f = arg;
1097 if (V4L2_TUNER_ANALOG_TV != f->type)
1101 dev->ctl_freq = f->frequency;
1102 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1107 case VIDIOC_CROPCAP:
1109 struct v4l2_cropcap *cc = arg;
1111 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1113 cc->bounds.left = 0;
1115 cc->bounds.width = dev->width;
1116 cc->bounds.height = dev->height;
1117 cc->defrect = cc->bounds;
1118 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1119 cc->pixelaspect.denominator = 59;
1122 case VIDIOC_STREAMON:
1126 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1127 || dev->io != IO_MMAP)
1130 if (list_empty(&dev->inqueue))
1133 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1135 em2820_videodbg("VIDIOC_STREAMON: starting stream");
1139 case VIDIOC_STREAMOFF:
1144 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1145 || dev->io != IO_MMAP)
1148 if (dev->stream == STREAM_ON) {
1149 em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1150 if ((ret = em2820_stream_interrupt(dev)))
1153 em2820_empty_framequeues(dev);
1158 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1165 * em2820_v4l2_do_ioctl()
1166 * This function is _not_ called directly, but from
1167 * em2820_v4l2_ioctl. Userspace
1168 * copying is done already, arg is a kernel pointer.
1170 static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
1171 unsigned int cmd, void *arg)
1173 struct em2820 *dev = filp->private_data;
1178 if (video_debug > 1)
1179 em2820_print_ioctl(dev->name,cmd);
1183 /* --- capabilities ------------------------------------------ */
1184 case VIDIOC_QUERYCAP:
1186 struct v4l2_capability *cap = arg;
1188 memset(cap, 0, sizeof(*cap));
1189 strlcpy(cap->driver, "em2820", sizeof(cap->driver));
1190 strlcpy(cap->card, em2820_boards[dev->model].name,
1192 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1193 sizeof(cap->bus_info));
1194 cap->version = EM2820_VERSION_CODE;
1196 V4L2_CAP_VIDEO_CAPTURE |
1198 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1200 cap->capabilities |= V4L2_CAP_TUNER;
1204 /* --- capture ioctls ---------------------------------------- */
1205 case VIDIOC_ENUM_FMT:
1207 struct v4l2_fmtdesc *fmtd = arg;
1209 if (fmtd->index != 0)
1211 memset(fmtd, 0, sizeof(*fmtd));
1212 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1213 strcpy(fmtd->description, "Packed YUY2");
1214 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1215 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1221 struct v4l2_format *format = arg;
1223 em2820_videodbg("VIDIOC_G_FMT: type=%s",
1225 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1226 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1227 V4L2_BUF_TYPE_VBI_CAPTURE ?
1228 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1231 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1234 format->fmt.pix.width = dev->width;
1235 format->fmt.pix.height = dev->height;
1236 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1237 format->fmt.pix.bytesperline = dev->bytesperline;
1238 format->fmt.pix.sizeimage = dev->frame_size;
1239 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1240 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1242 em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1247 case VIDIOC_TRY_FMT:
1250 struct v4l2_format *format = arg;
1253 int width = format->fmt.pix.width;
1254 int height = format->fmt.pix.height;
1255 unsigned int hscale, vscale;
1256 unsigned int maxh, maxw;
1258 maxw = norm_maxw(dev);
1259 maxh = norm_maxh(dev);
1261 /* int both_fields; */
1263 em2820_videodbg("%s: type=%s",
1265 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1268 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1269 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1270 V4L2_BUF_TYPE_VBI_CAPTURE ?
1271 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1274 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1277 em2820_videodbg("%s: requested %dx%d",
1279 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1280 "VIDIOC_S_FMT", format->fmt.pix.width,
1281 format->fmt.pix.height);
1283 /* FIXME: Move some code away from here */
1284 /* width must even because of the YUYV format */
1285 /* height must be even because of interlacing */
1300 /* we only know how to scale to 50% */
1301 if(height % (maxh / 2))
1303 if(width % (maxw / 2))
1305 /* larger resoltion don't seem to work either */
1306 if(width == maxw && height == maxh)
1311 (((unsigned long)maxw) << 12) / width - 4096L) >=
1315 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1318 (((unsigned long)maxh) << 12) / height - 4096L) >=
1322 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1324 format->fmt.pix.width = width;
1325 format->fmt.pix.height = height;
1326 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1327 format->fmt.pix.bytesperline = width * 2;
1328 format->fmt.pix.sizeimage = width * 2 * height;
1329 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1330 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1332 em2820_videodbg("%s: returned %dx%d (%d, %d)",
1334 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1335 "VIDIOC_S_FMT", format->fmt.pix.width,
1336 format->fmt.pix.height, hscale, vscale);
1338 if (cmd == VIDIOC_TRY_FMT)
1341 for (i = 0; i < dev->num_frames; i++)
1342 if (dev->frame[i].vma_use_count) {
1343 em2820_videodbg("VIDIOC_S_FMT failed. "
1344 "Unmap the buffers first.");
1348 /* stop io in case it is already in progress */
1349 if (dev->stream == STREAM_ON) {
1350 em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
1351 if ((ret = em2820_stream_interrupt(dev)))
1355 em2820_release_buffers(dev);
1358 /* set new image size */
1360 dev->height = height;
1361 dev->frame_size = dev->width * dev->height * 2;
1362 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1363 dev->bytesperline = dev->width * 2;
1364 dev->hscale = hscale;
1365 dev->vscale = vscale;
1366 /* dev->both_fileds = both_fileds; */
1367 em2820_uninit_isoc(dev);
1368 em2820_set_alternate(dev);
1369 em2820_capture_start(dev, 1);
1370 em2820_resolution_set(dev);
1371 em2820_init_isoc(dev);
1376 /* --- streaming capture ------------------------------------- */
1377 case VIDIOC_REQBUFS:
1379 struct v4l2_requestbuffers *rb = arg;
1383 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1384 rb->memory != V4L2_MEMORY_MMAP)
1387 if (dev->io == IO_READ) {
1388 em2820_videodbg ("method is set to read;"
1389 " close and open the device again to"
1390 " choose the mmap I/O method");
1394 for (i = 0; i < dev->num_frames; i++)
1395 if (dev->frame[i].vma_use_count) {
1396 em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1400 if (dev->stream == STREAM_ON) {
1401 em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
1402 if ((ret = em2820_stream_interrupt(dev)))
1406 em2820_empty_framequeues(dev);
1408 em2820_release_buffers(dev);
1411 em2820_request_buffers(dev, rb->count);
1413 dev->frame_current = NULL;
1415 em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1417 dev->io = rb->count ? IO_MMAP : IO_NONE;
1421 case VIDIOC_QUERYBUF:
1423 struct v4l2_buffer *b = arg;
1425 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1426 b->index >= dev->num_frames || dev->io != IO_MMAP)
1429 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1431 if (dev->frame[b->index].vma_use_count) {
1432 b->flags |= V4L2_BUF_FLAG_MAPPED;
1434 if (dev->frame[b->index].state == F_DONE)
1435 b->flags |= V4L2_BUF_FLAG_DONE;
1436 else if (dev->frame[b->index].state != F_UNUSED)
1437 b->flags |= V4L2_BUF_FLAG_QUEUED;
1442 struct v4l2_buffer *b = arg;
1443 unsigned long lock_flags;
1445 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1446 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1450 if (dev->frame[b->index].state != F_UNUSED) {
1453 dev->frame[b->index].state = F_QUEUED;
1455 /* add frame to fifo */
1456 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1457 list_add_tail(&dev->frame[b->index].frame,
1459 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1465 struct v4l2_buffer *b = arg;
1466 struct em2820_frame_t *f;
1467 unsigned long lock_flags;
1470 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1471 || dev->io != IO_MMAP)
1474 if (list_empty(&dev->outqueue)) {
1475 if (dev->stream == STREAM_OFF)
1477 if (filp->f_flags & O_NONBLOCK)
1479 ret = wait_event_interruptible
1481 (!list_empty(&dev->outqueue)) ||
1482 (dev->state & DEV_DISCONNECTED));
1485 if (dev->state & DEV_DISCONNECTED)
1489 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1490 f = list_entry(dev->outqueue.next,
1491 struct em2820_frame_t, frame);
1492 list_del(dev->outqueue.next);
1493 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1495 f->state = F_UNUSED;
1496 memcpy(b, &f->buf, sizeof(*b));
1498 if (f->vma_use_count)
1499 b->flags |= V4L2_BUF_FLAG_MAPPED;
1504 return em2820_do_ioctl(inode, filp, dev, cmd, arg,
1505 em2820_video_do_ioctl);
1511 * em2820_v4l2_ioctl()
1512 * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
1514 static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
1515 unsigned int cmd, unsigned long arg)
1518 struct em2820 *dev = filp->private_data;
1520 if (down_interruptible(&dev->fileop_lock))
1521 return -ERESTARTSYS;
1523 if (dev->state & DEV_DISCONNECTED) {
1524 em2820_errdev("v4l2 ioctl: device not present\n");
1525 up(&dev->fileop_lock);
1529 if (dev->state & DEV_MISCONFIGURED) {
1531 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1532 up(&dev->fileop_lock);
1536 ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
1538 up(&dev->fileop_lock);
1543 static struct file_operations em2820_v4l_fops = {
1544 .owner = THIS_MODULE,
1545 .open = em2820_v4l2_open,
1546 .release = em2820_v4l2_close,
1547 .ioctl = em2820_v4l2_ioctl,
1548 .read = em2820_v4l2_read,
1549 .poll = em2820_v4l2_poll,
1550 .mmap = em2820_v4l2_mmap,
1551 .llseek = no_llseek,
1554 /******************************** usb interface *****************************************/
1558 * allocates and inits the device structs, registers i2c bus and v4l device
1560 static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
1561 int minor, int model)
1563 struct em2820 *dev = *devhandle;
1564 int retval = -ENOMEM;
1566 unsigned int maxh, maxw;
1567 struct usb_interface *uif;
1571 init_MUTEX(&dev->lock);
1572 init_waitqueue_head(&dev->open);
1574 dev->em2820_write_regs = em2820_write_regs;
1575 dev->em2820_read_reg = em2820_read_reg;
1576 dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
1577 dev->em2820_write_regs_req = em2820_write_regs_req;
1578 dev->em2820_read_reg_req = em2820_read_reg_req;
1579 dev->is_em2800 = em2820_boards[model].is_em2800;
1580 dev->has_tuner = em2820_boards[model].has_tuner;
1581 dev->has_msp34xx = em2820_boards[model].has_msp34xx;
1582 dev->tda9887_conf = em2820_boards[model].tda9887_conf;
1583 dev->decoder = em2820_boards[model].decoder;
1586 dev->tuner_type = tuner;
1588 dev->tuner_type = em2820_boards[model].tuner_type;
1590 dev->video_inputs = em2820_boards[model].vchannels;
1592 for (i = 0; i < TVNORMS; i++)
1593 if (em2820_boards[model].norm == tvnorms[i].mode)
1598 dev->tvnorm = &tvnorms[i]; /* set default norm */
1600 em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1602 maxw = norm_maxw(dev);
1603 maxh = norm_maxh(dev);
1605 /* set default image size */
1608 dev->interlaced = EM2820_INTERLACED_DEFAULT;
1609 dev->field_size = dev->width * dev->height;
1611 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1612 dev->bytesperline = dev->width * 2;
1617 /* setup video picture settings for saa7113h */
1618 memset(&dev->vpic, 0, sizeof(dev->vpic));
1619 dev->vpic.colour = 128 << 8;
1620 dev->vpic.hue = 128 << 8;
1621 dev->vpic.brightness = 128 << 8;
1622 dev->vpic.contrast = 192 << 8;
1623 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1624 dev->vpic.depth = 16;
1625 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1627 /* compute alternate max packet sizes */
1628 uif = dev->udev->actconfig->interface[0];
1629 dev->alt_max_pkt_size[0] = 0;
1630 for (i = 1; i <= EM2820_MAX_ALT && i < uif->num_altsetting ; i++) {
1632 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1634 dev->alt_max_pkt_size[i] =
1635 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1638 #ifdef CONFIG_MODULES
1639 /* request some modules */
1640 if (dev->decoder == EM2820_SAA7113)
1641 request_module("saa7113");
1642 if (dev->decoder == EM2820_SAA7114)
1643 request_module("saa7114");
1644 if (dev->decoder == EM2820_TVP5150)
1645 request_module("tvp5150");
1647 request_module("tuner");
1648 if (dev->tda9887_conf)
1649 request_module("tda9887");
1651 errCode = em2820_config(dev);
1653 em2820_errdev("error configuring device\n");
1659 /* register i2c bus */
1660 em2820_i2c_register(dev);
1662 /* Do board specific init and eeprom reading */
1663 em2820_card_setup(dev);
1665 /* configure the device */
1666 em2820_config_i2c(dev);
1670 errCode = em2820_config(dev);
1672 #ifdef CONFIG_MODULES
1673 if (dev->has_msp34xx)
1674 request_module("msp3400");
1676 /* allocate and fill v4l2 device struct */
1677 dev->vdev = video_device_alloc();
1678 if (NULL == dev->vdev) {
1679 em2820_errdev("cannot allocate video_device.\n");
1684 dev->vdev->owner = THIS_MODULE;
1685 dev->vdev->type = VID_TYPE_CAPTURE;
1687 dev->vdev->type |= VID_TYPE_TUNER;
1688 dev->vdev->hardware = 0;
1689 dev->vdev->fops = &em2820_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,&em2820_devlist);
1697 /* register v4l2 device */
1699 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1700 em2820_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 em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1712 em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1720 em2820_info("V4L2 device registered as /dev/video%d\n",
1727 * em2820_usb_probe()
1728 * checks for supported devices
1730 static int em2820_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 em2820 *dev = NULL;
1736 int retval = -ENODEV;
1737 int model,i,nr,ifnum;
1739 udev = usb_get_dev(interface_to_usbdev(interface));
1740 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1742 em2820_err(DRIVER_NAME " new device (%04x:%04x): interface %i, class %i\n",
1743 udev->descriptor.idVendor,udev->descriptor.idProduct,
1745 interface->altsetting[0].desc.bInterfaceClass);
1747 /* Don't register audio interfaces */
1748 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
1751 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1753 /* check if the the device has the iso in endpoint at the correct place */
1754 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1755 USB_ENDPOINT_XFER_ISOC) {
1756 em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1759 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1760 em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1764 model=id->driver_info;
1765 nr=interface->minor;
1767 if (nr>EM2820_MAXBOARDS) {
1768 printk ("em2820: Supports only %i em28xx boards.\n",EM2820_MAXBOARDS);
1772 /* allocate memory for our device state and initialize it */
1773 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1775 em2820_err(DRIVER_NAME ": out of memory!\n");
1778 memset(dev, 0, sizeof(*dev));
1780 snprintf(dev->name, 29, "em2820 #%d", nr);
1782 if ((card[nr]>=0)&&(card[nr]<em2820_bcount))
1785 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1786 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1787 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1788 "%s: workaround that. Redirect complaints to the vendor of\n"
1789 "%s: the TV card. Best regards,\n"
1791 dev->name,dev->name,dev->name,dev->name,dev->name);
1792 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1794 for (i = 0; i < em2820_bcount; i++) {
1795 printk("%s: card=%d -> %s\n",
1796 dev->name, i, em2820_boards[i].name);
1800 /* allocate device struct */
1801 retval = em2820_init_dev(&dev, udev, nr, model);
1805 em2820_info("Found %s\n", em2820_boards[model].name);
1807 /* save our data pointer in this interface device */
1808 usb_set_intfdata(interface, dev);
1813 * em2820_usb_disconnect()
1814 * called when the device gets diconencted
1815 * video device will be unregistered on v4l2_close in case it is still open
1817 static void em2820_usb_disconnect(struct usb_interface *interface)
1819 struct em2820 *dev = usb_get_intfdata(interface);
1820 usb_set_intfdata(interface, NULL);
1825 down_write(&em2820_disconnect);
1829 em2820_info("disconnecting %s\n", dev->vdev->name);
1831 wake_up_interruptible_all(&dev->open);
1835 ("device /dev/video%d is open! Deregistration and memory "
1836 "deallocation are deferred on close.\n", dev->vdev->minor);
1837 dev->state |= DEV_MISCONFIGURED;
1838 em2820_uninit_isoc(dev);
1839 dev->state |= DEV_DISCONNECTED;
1840 wake_up_interruptible(&dev->wait_frame);
1841 wake_up_interruptible(&dev->wait_stream);
1843 dev->state |= DEV_DISCONNECTED;
1844 em2820_release_resources(dev);
1852 up_write(&em2820_disconnect);
1856 static struct usb_driver em2820_usb_driver = {
1857 .owner = THIS_MODULE,
1859 .probe = em2820_usb_probe,
1860 .disconnect = em2820_usb_disconnect,
1861 .id_table = em2820_id_table,
1864 static int __init em2820_module_init(void)
1868 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1869 (EM2820_VERSION_CODE >> 16) & 0xff,
1870 (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
1872 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1873 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1876 /* register this driver with the USB subsystem */
1877 result = usb_register(&em2820_usb_driver);
1879 em2820_err(DRIVER_NAME
1880 " usb_register failed. Error number %d.\n", result);
1885 static void __exit em2820_module_exit(void)
1887 /* deregister this driver with the USB subsystem */
1888 usb_deregister(&em2820_usb_driver);
1891 module_init(em2820_module_init);
1892 module_exit(em2820_module_exit);