2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7 Sascha Sommer <saschasommer@freenet.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/usb.h>
29 #include <linux/i2c.h>
30 #include <linux/video_decoder.h>
31 #include <linux/utsname.h>
34 #include <media/tuner.h>
36 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
37 "Markus Rechberger <mrechberger@gmail.com>, " \
38 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
39 "Sascha Sommer <saschasommer@freenet.de>"
41 #define DRIVER_NAME "em28xx"
42 #define DRIVER_DESC "Empia em28xx based USB video device driver"
43 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
45 #define em28xx_videodbg(fmt, arg...) do {\
47 printk(KERN_INFO "%s %s :"fmt, \
48 dev->name, __FUNCTION__ , ##arg); } while (0)
50 MODULE_AUTHOR(DRIVER_AUTHOR);
51 MODULE_DESCRIPTION(DRIVER_DESC);
52 MODULE_LICENSE("GPL");
54 static LIST_HEAD(em28xx_devlist);
56 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
58 module_param_array(card, int, NULL, 0444);
59 MODULE_PARM_DESC(card,"card type");
61 static int tuner = -1;
62 module_param(tuner, int, 0444);
63 MODULE_PARM_DESC(tuner, "tuner type");
65 static unsigned int video_debug = 0;
66 module_param(video_debug,int,0644);
67 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
69 /* supported tv norms */
70 static struct em28xx_tvnorm tvnorms[] = {
74 .mode = VIDEO_MODE_PAL,
78 .mode = VIDEO_MODE_NTSC,
82 .mode = VIDEO_MODE_SECAM,
86 .mode = VIDEO_MODE_PAL,
90 static const unsigned char saa7114_i2c_init[] = {
91 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
92 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
93 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
94 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
95 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
96 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
97 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
98 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
99 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
100 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
101 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
102 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
103 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
107 #define TVNORMS ARRAY_SIZE(tvnorms)
109 /* supported controls */
110 static struct v4l2_queryctrl em28xx_qctrl[] = {
112 .id = V4L2_CID_BRIGHTNESS,
113 .type = V4L2_CTRL_TYPE_INTEGER,
114 .name = "Brightness",
121 .id = V4L2_CID_CONTRAST,
122 .type = V4L2_CTRL_TYPE_INTEGER,
127 .default_value = 0x10,
130 .id = V4L2_CID_SATURATION,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Saturation",
136 .default_value = 0x10,
139 .id = V4L2_CID_AUDIO_VOLUME,
140 .type = V4L2_CTRL_TYPE_INTEGER,
145 .default_value = 0x1f,
148 .id = V4L2_CID_AUDIO_MUTE,
149 .type = V4L2_CTRL_TYPE_BOOLEAN,
157 .id = V4L2_CID_RED_BALANCE,
158 .type = V4L2_CTRL_TYPE_INTEGER,
159 .name = "Red chroma balance",
166 .id = V4L2_CID_BLUE_BALANCE,
167 .type = V4L2_CTRL_TYPE_INTEGER,
168 .name = "Blue chroma balance",
175 .id = V4L2_CID_GAMMA,
176 .type = V4L2_CTRL_TYPE_INTEGER,
181 .default_value = 0x20,
186 static struct usb_driver em28xx_usb_driver;
188 static DECLARE_MUTEX(em28xx_sysfs_lock);
189 static DECLARE_RWSEM(em28xx_disconnect);
191 /********************* v4l2 interface ******************************************/
193 static inline unsigned long kvirt_to_pa(unsigned long adr)
195 unsigned long kva, ret;
197 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
198 kva |= adr & (PAGE_SIZE - 1);
205 * inits registers with sane defaults
207 static int em28xx_config(struct em28xx *dev)
210 /* Sets I2C speed to 100 KHz */
211 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
213 /* enable vbi capturing */
214 em28xx_audio_usb_mute(dev, 1);
215 dev->mute = 1; /* maybe not the right place... */
217 em28xx_audio_analog_set(dev);
218 em28xx_audio_analog_setup(dev);
219 em28xx_outfmt_set_yuv422(dev);
220 em28xx_colorlevels_set_default(dev);
221 em28xx_compression_disable(dev);
227 * em28xx_config_i2c()
228 * configure i2c attached devices
230 void em28xx_config_i2c(struct em28xx *dev)
232 struct v4l2_frequency f;
233 struct video_decoder_init em28xx_vdi = {.data = NULL };
236 /* configure decoder */
237 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
238 em28xx_vdi.data=saa7114_i2c_init;
239 em28xx_vdi.len=sizeof(saa7114_i2c_init);
243 em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
244 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
245 /* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
246 /* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
247 /* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
248 /* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
250 /* configure tuner */
252 f.type = V4L2_TUNER_ANALOG_TV;
253 f.frequency = 9076; /* FIXME:remove magic number */
254 dev->ctl_freq = f.frequency;
255 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
257 /* configure tda9887 */
260 /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
264 * em28xx_empty_framequeues()
265 * prepare queues for incoming and outgoing frames
267 static void em28xx_empty_framequeues(struct em28xx *dev)
271 INIT_LIST_HEAD(&dev->inqueue);
272 INIT_LIST_HEAD(&dev->outqueue);
274 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
275 dev->frame[i].state = F_UNUSED;
276 dev->frame[i].buf.bytesused = 0;
282 * inits the device and starts isoc transfer
284 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
286 int minor = iminor(inode);
288 struct em28xx *h,*dev = NULL;
289 struct list_head *list;
291 list_for_each(list,&em28xx_devlist) {
292 h = list_entry(list, struct em28xx, devlist);
293 if (h->vdev->minor == minor) {
298 filp->private_data=dev;
301 em28xx_videodbg("users=%d", dev->users);
303 if (!down_read_trylock(&em28xx_disconnect))
307 em28xx_warn("this driver can be opened only once\n");
308 up_read(&em28xx_disconnect);
312 /* if(dev->vbi_dev->minor == minor){
313 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
315 if (dev->vdev->minor == minor) {
316 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
319 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
320 spin_lock_init(&dev->queue_lock);
321 init_waitqueue_head(&dev->wait_frame);
322 init_waitqueue_head(&dev->wait_stream);
326 em28xx_set_alternate(dev);
328 dev->width = norm_maxw(dev);
329 dev->height = norm_maxh(dev);
330 dev->frame_size = dev->width * dev->height * 2;
331 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
332 dev->bytesperline = dev->width * 2;
336 em28xx_capture_start(dev, 1);
337 em28xx_resolution_set(dev);
339 /* start the transfer */
340 errCode = em28xx_init_isoc(dev);
345 filp->private_data = dev;
347 dev->stream = STREAM_OFF;
351 em28xx_empty_framequeues(dev);
353 dev->state |= DEV_INITIALIZED;
357 up_read(&em28xx_disconnect);
362 * em28xx_realease_resources()
363 * unregisters the v4l2,i2c and usb devices
364 * called when the device gets disconected or at module unload
366 static void em28xx_release_resources(struct em28xx *dev)
368 down(&em28xx_sysfs_lock);
370 em28xx_info("V4L2 device /dev/video%d deregistered\n",
372 list_del(&dev->devlist);
373 video_unregister_device(dev->vdev);
374 /* video_unregister_device(dev->vbi_dev); */
375 em28xx_i2c_unregister(dev);
376 usb_put_dev(dev->udev);
377 up(&em28xx_sysfs_lock);
381 * em28xx_v4l2_close()
382 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
384 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
387 struct em28xx *dev=filp->private_data;
389 em28xx_videodbg("users=%d", dev->users);
393 em28xx_uninit_isoc(dev);
395 em28xx_release_buffers(dev);
397 /* the device is already disconnect, free the remaining resources */
398 if (dev->state & DEV_DISCONNECTED) {
399 em28xx_release_resources(dev);
405 /* set alternate 0 */
407 em28xx_videodbg("setting alternate 0");
408 errCode = usb_set_interface(dev->udev, 0, 0);
410 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
415 wake_up_interruptible_nr(&dev->open, 1);
422 * will allocate buffers when called for the first time
425 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
428 struct em28xx_frame_t *f, *i;
429 unsigned long lock_flags;
431 struct em28xx *dev = filp->private_data;
433 if (down_interruptible(&dev->fileop_lock))
436 if (dev->state & DEV_DISCONNECTED) {
437 em28xx_videodbg("device not present");
438 up(&dev->fileop_lock);
442 if (dev->state & DEV_MISCONFIGURED) {
443 em28xx_videodbg("device misconfigured; close and open it again");
444 up(&dev->fileop_lock);
448 if (dev->io == IO_MMAP) {
449 em28xx_videodbg ("IO method is set to mmap; close and open"
450 " the device again to choose the read method");
451 up(&dev->fileop_lock);
455 if (dev->io == IO_NONE) {
456 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
457 em28xx_errdev("read failed, not enough memory\n");
458 up(&dev->fileop_lock);
462 dev->stream = STREAM_ON;
463 em28xx_queue_unusedframes(dev);
467 up(&dev->fileop_lock);
471 if (list_empty(&dev->outqueue)) {
472 if (filp->f_flags & O_NONBLOCK) {
473 up(&dev->fileop_lock);
476 ret = wait_event_interruptible
478 (!list_empty(&dev->outqueue)) ||
479 (dev->state & DEV_DISCONNECTED));
481 up(&dev->fileop_lock);
484 if (dev->state & DEV_DISCONNECTED) {
485 up(&dev->fileop_lock);
490 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
492 spin_lock_irqsave(&dev->queue_lock, lock_flags);
493 list_for_each_entry(i, &dev->outqueue, frame)
495 INIT_LIST_HEAD(&dev->outqueue);
496 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
498 em28xx_queue_unusedframes(dev);
500 if (count > f->buf.length)
501 count = f->buf.length;
503 if (copy_to_user(buf, f->bufmem, count)) {
504 up(&dev->fileop_lock);
509 up(&dev->fileop_lock);
516 * will allocate buffers when called for the first time
518 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
520 unsigned int mask = 0;
521 struct em28xx *dev = filp->private_data;
523 if (down_interruptible(&dev->fileop_lock))
526 if (dev->state & DEV_DISCONNECTED) {
527 em28xx_videodbg("device not present");
528 } else if (dev->state & DEV_MISCONFIGURED) {
529 em28xx_videodbg("device is misconfigured; close and open it again");
531 if (dev->io == IO_NONE) {
532 if (!em28xx_request_buffers
533 (dev, EM28XX_NUM_READ_FRAMES)) {
535 ("poll() failed, not enough memory\n");
538 dev->stream = STREAM_ON;
542 if (dev->io == IO_READ) {
543 em28xx_queue_unusedframes(dev);
544 poll_wait(filp, &dev->wait_frame, wait);
546 if (!list_empty(&dev->outqueue))
547 mask |= POLLIN | POLLRDNORM;
549 up(&dev->fileop_lock);
555 up(&dev->fileop_lock);
562 static void em28xx_vm_open(struct vm_area_struct *vma)
564 struct em28xx_frame_t *f = vma->vm_private_data;
571 static void em28xx_vm_close(struct vm_area_struct *vma)
573 /* NOTE: buffers are not freed here */
574 struct em28xx_frame_t *f = vma->vm_private_data;
578 static struct vm_operations_struct em28xx_vm_ops = {
579 .open = em28xx_vm_open,
580 .close = em28xx_vm_close,
586 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
588 unsigned long size = vma->vm_end - vma->vm_start,
589 start = vma->vm_start, pos, page;
592 struct em28xx *dev = filp->private_data;
594 if (down_interruptible(&dev->fileop_lock))
597 if (dev->state & DEV_DISCONNECTED) {
598 em28xx_videodbg("mmap: device not present");
599 up(&dev->fileop_lock);
603 if (dev->state & DEV_MISCONFIGURED) {
604 em28xx_videodbg ("mmap: Device is misconfigured; close and "
606 up(&dev->fileop_lock);
610 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
611 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
612 up(&dev->fileop_lock);
616 for (i = 0; i < dev->num_frames; i++) {
617 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
620 if (i == dev->num_frames) {
621 em28xx_videodbg("mmap: user supplied mapping address is out of range");
622 up(&dev->fileop_lock);
626 /* VM_IO is eventually going to replace PageReserved altogether */
627 vma->vm_flags |= VM_IO;
628 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
630 pos = (unsigned long)dev->frame[i].bufmem;
631 while (size > 0) { /* size is page-aligned */
632 page = vmalloc_to_pfn((void *)pos);
633 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
634 vma->vm_page_prot)) {
635 em28xx_videodbg("mmap: rename page map failed");
636 up(&dev->fileop_lock);
644 vma->vm_ops = &em28xx_vm_ops;
645 vma->vm_private_data = &dev->frame[i];
648 up(&dev->fileop_lock);
654 * return the current saturation, brightness or contrast, mute state
656 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
660 case V4L2_CID_AUDIO_MUTE:
661 ctrl->value = dev->mute;
663 case V4L2_CID_AUDIO_VOLUME:
664 ctrl->value = dev->volume;
666 case V4L2_CID_BRIGHTNESS:
667 if ((tmp = em28xx_brightness_get(dev)) < 0)
669 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
671 case V4L2_CID_CONTRAST:
672 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
675 case V4L2_CID_SATURATION:
676 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
679 case V4L2_CID_RED_BALANCE:
680 if ((tmp = em28xx_v_balance_get(dev)) < 0)
682 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
684 case V4L2_CID_BLUE_BALANCE:
685 if ((tmp = em28xx_u_balance_get(dev)) < 0)
687 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
690 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
700 * mute or set new saturation, brightness or contrast
702 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
705 case V4L2_CID_AUDIO_MUTE:
706 if (ctrl->value != dev->mute) {
707 dev->mute = ctrl->value;
708 em28xx_audio_usb_mute(dev, ctrl->value);
709 return em28xx_audio_analog_set(dev);
712 case V4L2_CID_AUDIO_VOLUME:
713 dev->volume = ctrl->value;
714 return em28xx_audio_analog_set(dev);
715 case V4L2_CID_BRIGHTNESS:
716 return em28xx_brightness_set(dev, ctrl->value);
717 case V4L2_CID_CONTRAST:
718 return em28xx_contrast_set(dev, ctrl->value);
719 case V4L2_CID_SATURATION:
720 return em28xx_saturation_set(dev, ctrl->value);
721 case V4L2_CID_RED_BALANCE:
722 return em28xx_v_balance_set(dev, ctrl->value);
723 case V4L2_CID_BLUE_BALANCE:
724 return em28xx_u_balance_set(dev, ctrl->value);
726 return em28xx_gamma_set(dev, ctrl->value);
733 * em28xx_stream_interrupt()
736 static int em28xx_stream_interrupt(struct em28xx *dev)
740 /* stop reading from the device */
742 dev->stream = STREAM_INTERRUPT;
743 ret = wait_event_timeout(dev->wait_stream,
744 (dev->stream == STREAM_OFF) ||
745 (dev->state & DEV_DISCONNECTED),
747 if (dev->state & DEV_DISCONNECTED)
750 dev->state |= DEV_MISCONFIGURED;
751 em28xx_videodbg("device is misconfigured; close and "
752 "open /dev/video%d again", dev->vdev->minor);
759 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
761 unsigned int hscale, vscale;
762 unsigned int maxh, maxw;
764 maxw = norm_maxw(dev);
765 maxh = norm_maxh(dev);
767 /* width must even because of the YUYV format */
768 /* height must be even because of interlacing */
781 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
783 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
785 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
787 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
789 /* set new image size */
791 dev->height = height;
792 dev->frame_size = dev->width * dev->height * 2;
793 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
794 dev->bytesperline = dev->width * 2;
795 dev->hscale = hscale;
796 dev->vscale = vscale;
798 em28xx_resolution_set(dev);
803 static void video_mux(struct em28xx *dev, int index)
807 input = INPUT(index)->vmux;
808 dev->ctl_input = index;
810 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
812 dev->ctl_ainput = INPUT(index)->amux;
814 switch (dev->ctl_ainput) {
816 ainput = EM28XX_AUDIO_SRC_TUNER;
819 ainput = EM28XX_AUDIO_SRC_LINE;
822 em28xx_audio_source(dev, ainput);
826 * em28xx_v4l2_do_ioctl()
827 * This function is _not_ called directly, but from
828 * em28xx_v4l2_ioctl. Userspace
829 * copying is done already, arg is a kernel pointer.
831 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
832 struct em28xx *dev, unsigned int cmd, void *arg,
833 v4l2_kioctl driver_ioctl)
838 /* ---------- tv norms ---------- */
841 struct v4l2_standard *e = arg;
847 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
848 tvnorms[e->index].name);
856 v4l2_std_id *id = arg;
858 *id = dev->tvnorm->id;
863 v4l2_std_id *id = arg;
866 for (i = 0; i < TVNORMS; i++)
867 if (*id == tvnorms[i].id)
870 for (i = 0; i < TVNORMS; i++)
871 if (*id & tvnorms[i].id)
877 dev->tvnorm = &tvnorms[i];
879 em28xx_set_norm(dev, dev->width, dev->height);
882 dev->width=norm_maxw(dev);
883 dev->height=norm_maxh(dev);
884 dev->frame_size=dev->width*dev->height*2;
885 dev->field_size=dev->frame_size>>1;
886 dev->bytesperline=dev->width*2;
890 em28xx_resolution_set(dev);
893 em28xx_uninit_isoc(dev);
894 em28xx_set_alternate(dev);
895 em28xx_capture_start(dev, 1);
896 em28xx_resolution_set(dev);
897 em28xx_init_isoc(dev);
899 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
901 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
909 /* ------ input switching ---------- */
910 case VIDIOC_ENUMINPUT:
912 struct v4l2_input *i = arg;
914 static const char *iname[] = {
915 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
916 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
917 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
918 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
919 [EM28XX_VMUX_SVIDEO] = "S-Video",
920 [EM28XX_VMUX_TELEVISION] = "Television",
921 [EM28XX_VMUX_CABLE] = "Cable TV",
922 [EM28XX_VMUX_DVB] = "DVB",
923 [EM28XX_VMUX_DEBUG] = "for debug only",
927 if (n >= MAX_EM28XX_INPUT)
929 if (0 == INPUT(n)->type)
931 memset(i, 0, sizeof(*i));
933 i->type = V4L2_INPUT_TYPE_CAMERA;
934 strcpy(i->name, iname[INPUT(n)->type]);
935 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
936 (EM28XX_VMUX_CABLE == INPUT(n)->type))
937 i->type = V4L2_INPUT_TYPE_TUNER;
938 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
939 i->std |= tvnorms[n].id;
955 if (*index >= MAX_EM28XX_INPUT)
957 if (0 == INPUT(*index)->type)
961 video_mux(dev, *index);
969 struct v4l2_audio *a = arg;
970 unsigned int index = a->index;
974 memset(a, 0, sizeof(*a));
975 index = dev->ctl_ainput;
978 strcpy(a->name, "Television");
980 strcpy(a->name, "Line In");
982 a->capability = V4L2_AUDCAP_STEREO;
989 struct v4l2_audio *a = arg;
990 if (a->index != dev->ctl_ainput)
996 /* --- controls ---------------------------------------------- */
997 case VIDIOC_QUERYCTRL:
999 struct v4l2_queryctrl *qc = arg;
1001 n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]);
1002 for (i = 0; i < n; i++)
1003 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1004 memcpy(qc, &(em28xx_qctrl[i]),
1014 struct v4l2_control *ctrl = arg;
1017 return em28xx_get_ctrl(dev, ctrl);
1020 case VIDIOC_S_CTRL_OLD: /* ??? */
1023 struct v4l2_control *ctrl = arg;
1027 n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]);
1028 for (i = 0; i < n; i++)
1029 if (ctrl->id == em28xx_qctrl[i].id) {
1031 em28xx_qctrl[i].minimum
1033 em28xx_qctrl[i].maximum)
1036 return em28xx_set_ctrl(dev, ctrl);
1041 /* --- tuner ioctls ------------------------------------------ */
1042 case VIDIOC_G_TUNER:
1044 struct v4l2_tuner *t = arg;
1050 memset(t, 0, sizeof(*t));
1051 strcpy(t->name, "Tuner");
1052 t->type = V4L2_TUNER_ANALOG_TV;
1053 t->capability = V4L2_TUNER_CAP_NORM;
1054 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1055 /* t->signal = 0xffff;*/
1056 /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1057 /* No way to get signal strength? */
1059 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1063 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1065 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1069 case VIDIOC_S_TUNER:
1071 struct v4l2_tuner *t = arg;
1076 memset(t, 0, sizeof(*t));
1077 strcpy(t->name, "Tuner");
1078 t->type = V4L2_TUNER_ANALOG_TV;
1079 t->capability = V4L2_TUNER_CAP_NORM;
1080 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1081 /* t->signal = 0xffff; */
1082 /* No way to get signal strength? */
1084 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1088 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1090 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1094 case VIDIOC_G_FREQUENCY:
1096 struct v4l2_frequency *f = arg;
1098 memset(f, 0, sizeof(*f));
1099 f->type = V4L2_TUNER_ANALOG_TV;
1100 f->frequency = dev->ctl_freq;
1104 case VIDIOC_S_FREQUENCY:
1106 struct v4l2_frequency *f = arg;
1111 if (V4L2_TUNER_ANALOG_TV != f->type)
1115 dev->ctl_freq = f->frequency;
1116 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1121 case VIDIOC_CROPCAP:
1123 struct v4l2_cropcap *cc = arg;
1125 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1127 cc->bounds.left = 0;
1129 cc->bounds.width = dev->width;
1130 cc->bounds.height = dev->height;
1131 cc->defrect = cc->bounds;
1132 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1133 cc->pixelaspect.denominator = 59;
1136 case VIDIOC_STREAMON:
1140 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1141 || dev->io != IO_MMAP)
1144 if (list_empty(&dev->inqueue))
1147 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1149 em28xx_videodbg("VIDIOC_STREAMON: starting stream");
1153 case VIDIOC_STREAMOFF:
1158 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1159 || dev->io != IO_MMAP)
1162 if (dev->stream == STREAM_ON) {
1163 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1164 if ((ret = em28xx_stream_interrupt(dev)))
1167 em28xx_empty_framequeues(dev);
1172 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1179 * em28xx_v4l2_do_ioctl()
1180 * This function is _not_ called directly, but from
1181 * em28xx_v4l2_ioctl. Userspace
1182 * copying is done already, arg is a kernel pointer.
1184 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1185 unsigned int cmd, void *arg)
1187 struct em28xx *dev = filp->private_data;
1192 if (video_debug > 1)
1193 em28xx_print_ioctl(dev->name,cmd);
1197 /* --- capabilities ------------------------------------------ */
1198 case VIDIOC_QUERYCAP:
1200 struct v4l2_capability *cap = arg;
1202 memset(cap, 0, sizeof(*cap));
1203 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1204 strlcpy(cap->card, em28xx_boards[dev->model].name,
1206 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1207 sizeof(cap->bus_info));
1208 cap->version = EM28XX_VERSION_CODE;
1210 V4L2_CAP_VIDEO_CAPTURE |
1212 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1214 cap->capabilities |= V4L2_CAP_TUNER;
1218 /* --- capture ioctls ---------------------------------------- */
1219 case VIDIOC_ENUM_FMT:
1221 struct v4l2_fmtdesc *fmtd = arg;
1223 if (fmtd->index != 0)
1225 memset(fmtd, 0, sizeof(*fmtd));
1226 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1227 strcpy(fmtd->description, "Packed YUY2");
1228 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1229 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1235 struct v4l2_format *format = arg;
1237 em28xx_videodbg("VIDIOC_G_FMT: type=%s",
1239 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1240 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1241 V4L2_BUF_TYPE_VBI_CAPTURE ?
1242 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1245 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1248 format->fmt.pix.width = dev->width;
1249 format->fmt.pix.height = dev->height;
1250 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1251 format->fmt.pix.bytesperline = dev->bytesperline;
1252 format->fmt.pix.sizeimage = dev->frame_size;
1253 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1254 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1256 em28xx_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1261 case VIDIOC_TRY_FMT:
1264 struct v4l2_format *format = arg;
1267 int width = format->fmt.pix.width;
1268 int height = format->fmt.pix.height;
1269 unsigned int hscale, vscale;
1270 unsigned int maxh, maxw;
1272 maxw = norm_maxw(dev);
1273 maxh = norm_maxh(dev);
1275 /* int both_fields; */
1277 em28xx_videodbg("%s: type=%s",
1279 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1282 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1283 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1284 V4L2_BUF_TYPE_VBI_CAPTURE ?
1285 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1288 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1291 em28xx_videodbg("%s: requested %dx%d",
1293 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1294 "VIDIOC_S_FMT", format->fmt.pix.width,
1295 format->fmt.pix.height);
1297 /* FIXME: Move some code away from here */
1298 /* width must even because of the YUYV format */
1299 /* height must be even because of interlacing */
1313 /* the em2800 can only scale down to 50% */
1314 if(height % (maxh / 2))
1316 if(width % (maxw / 2))
1318 /* according to empiatech support */
1319 /* the MaxPacketSize is to small to support */
1320 /* framesizes larger than 640x480 @ 30 fps */
1321 /* or 640x576 @ 25 fps. As this would cut */
1322 /* of a part of the image we prefer */
1323 /* 360x576 or 360x480 for now */
1324 if(width == maxw && height == maxh)
1329 (((unsigned long)maxw) << 12) / width - 4096L) >=
1333 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1336 (((unsigned long)maxh) << 12) / height - 4096L) >=
1340 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1342 format->fmt.pix.width = width;
1343 format->fmt.pix.height = height;
1344 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1345 format->fmt.pix.bytesperline = width * 2;
1346 format->fmt.pix.sizeimage = width * 2 * height;
1347 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1348 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1350 em28xx_videodbg("%s: returned %dx%d (%d, %d)",
1352 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1353 "VIDIOC_S_FMT", format->fmt.pix.width,
1354 format->fmt.pix.height, hscale, vscale);
1356 if (cmd == VIDIOC_TRY_FMT)
1359 for (i = 0; i < dev->num_frames; i++)
1360 if (dev->frame[i].vma_use_count) {
1361 em28xx_videodbg("VIDIOC_S_FMT failed. "
1362 "Unmap the buffers first.");
1366 /* stop io in case it is already in progress */
1367 if (dev->stream == STREAM_ON) {
1368 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream");
1369 if ((ret = em28xx_stream_interrupt(dev)))
1373 em28xx_release_buffers(dev);
1376 /* set new image size */
1378 dev->height = height;
1379 dev->frame_size = dev->width * dev->height * 2;
1380 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1381 dev->bytesperline = dev->width * 2;
1382 dev->hscale = hscale;
1383 dev->vscale = vscale;
1384 /* dev->both_fileds = both_fileds; */
1385 em28xx_uninit_isoc(dev);
1386 em28xx_set_alternate(dev);
1387 em28xx_capture_start(dev, 1);
1388 em28xx_resolution_set(dev);
1389 em28xx_init_isoc(dev);
1394 /* --- streaming capture ------------------------------------- */
1395 case VIDIOC_REQBUFS:
1397 struct v4l2_requestbuffers *rb = arg;
1401 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1402 rb->memory != V4L2_MEMORY_MMAP)
1405 if (dev->io == IO_READ) {
1406 em28xx_videodbg ("method is set to read;"
1407 " close and open the device again to"
1408 " choose the mmap I/O method");
1412 for (i = 0; i < dev->num_frames; i++)
1413 if (dev->frame[i].vma_use_count) {
1414 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1418 if (dev->stream == STREAM_ON) {
1419 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream");
1420 if ((ret = em28xx_stream_interrupt(dev)))
1424 em28xx_empty_framequeues(dev);
1426 em28xx_release_buffers(dev);
1429 em28xx_request_buffers(dev, rb->count);
1431 dev->frame_current = NULL;
1433 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1435 dev->io = rb->count ? IO_MMAP : IO_NONE;
1439 case VIDIOC_QUERYBUF:
1441 struct v4l2_buffer *b = arg;
1443 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1444 b->index >= dev->num_frames || dev->io != IO_MMAP)
1447 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1449 if (dev->frame[b->index].vma_use_count) {
1450 b->flags |= V4L2_BUF_FLAG_MAPPED;
1452 if (dev->frame[b->index].state == F_DONE)
1453 b->flags |= V4L2_BUF_FLAG_DONE;
1454 else if (dev->frame[b->index].state != F_UNUSED)
1455 b->flags |= V4L2_BUF_FLAG_QUEUED;
1460 struct v4l2_buffer *b = arg;
1461 unsigned long lock_flags;
1463 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1464 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1468 if (dev->frame[b->index].state != F_UNUSED) {
1471 dev->frame[b->index].state = F_QUEUED;
1473 /* add frame to fifo */
1474 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1475 list_add_tail(&dev->frame[b->index].frame,
1477 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1483 struct v4l2_buffer *b = arg;
1484 struct em28xx_frame_t *f;
1485 unsigned long lock_flags;
1488 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1489 || dev->io != IO_MMAP)
1492 if (list_empty(&dev->outqueue)) {
1493 if (dev->stream == STREAM_OFF)
1495 if (filp->f_flags & O_NONBLOCK)
1497 ret = wait_event_interruptible
1499 (!list_empty(&dev->outqueue)) ||
1500 (dev->state & DEV_DISCONNECTED));
1503 if (dev->state & DEV_DISCONNECTED)
1507 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1508 f = list_entry(dev->outqueue.next,
1509 struct em28xx_frame_t, frame);
1510 list_del(dev->outqueue.next);
1511 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1513 f->state = F_UNUSED;
1514 memcpy(b, &f->buf, sizeof(*b));
1516 if (f->vma_use_count)
1517 b->flags |= V4L2_BUF_FLAG_MAPPED;
1522 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1523 em28xx_video_do_ioctl);
1529 * em28xx_v4l2_ioctl()
1530 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1532 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1533 unsigned int cmd, unsigned long arg)
1536 struct em28xx *dev = filp->private_data;
1538 if (down_interruptible(&dev->fileop_lock))
1539 return -ERESTARTSYS;
1541 if (dev->state & DEV_DISCONNECTED) {
1542 em28xx_errdev("v4l2 ioctl: device not present\n");
1543 up(&dev->fileop_lock);
1547 if (dev->state & DEV_MISCONFIGURED) {
1549 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1550 up(&dev->fileop_lock);
1554 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1556 up(&dev->fileop_lock);
1561 static struct file_operations em28xx_v4l_fops = {
1562 .owner = THIS_MODULE,
1563 .open = em28xx_v4l2_open,
1564 .release = em28xx_v4l2_close,
1565 .ioctl = em28xx_v4l2_ioctl,
1566 .read = em28xx_v4l2_read,
1567 .poll = em28xx_v4l2_poll,
1568 .mmap = em28xx_v4l2_mmap,
1569 .llseek = no_llseek,
1572 /******************************** usb interface *****************************************/
1576 * allocates and inits the device structs, registers i2c bus and v4l device
1578 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1579 int minor, int model)
1581 struct em28xx *dev = *devhandle;
1582 int retval = -ENOMEM;
1584 unsigned int maxh, maxw;
1585 struct usb_interface *uif;
1589 init_MUTEX(&dev->lock);
1590 init_waitqueue_head(&dev->open);
1592 dev->em28xx_write_regs = em28xx_write_regs;
1593 dev->em28xx_read_reg = em28xx_read_reg;
1594 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1595 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1596 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1597 dev->is_em2800 = em28xx_boards[model].is_em2800;
1598 dev->has_tuner = em28xx_boards[model].has_tuner;
1599 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1600 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1601 dev->decoder = em28xx_boards[model].decoder;
1604 dev->tuner_type = tuner;
1606 dev->tuner_type = em28xx_boards[model].tuner_type;
1608 dev->video_inputs = em28xx_boards[model].vchannels;
1610 for (i = 0; i < TVNORMS; i++)
1611 if (em28xx_boards[model].norm == tvnorms[i].mode)
1616 dev->tvnorm = &tvnorms[i]; /* set default norm */
1618 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1620 maxw = norm_maxw(dev);
1621 maxh = norm_maxh(dev);
1623 /* set default image size */
1626 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1627 dev->field_size = dev->width * dev->height;
1629 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1630 dev->bytesperline = dev->width * 2;
1635 /* setup video picture settings for saa7113h */
1636 memset(&dev->vpic, 0, sizeof(dev->vpic));
1637 dev->vpic.colour = 128 << 8;
1638 dev->vpic.hue = 128 << 8;
1639 dev->vpic.brightness = 128 << 8;
1640 dev->vpic.contrast = 192 << 8;
1641 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1642 dev->vpic.depth = 16;
1643 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1645 /* compute alternate max packet sizes */
1646 uif = dev->udev->actconfig->interface[0];
1647 dev->alt_max_pkt_size[0] = 0;
1648 for (i = 1; i <= EM28XX_MAX_ALT && i < uif->num_altsetting ; i++) {
1650 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1652 dev->alt_max_pkt_size[i] =
1653 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1656 #ifdef CONFIG_MODULES
1657 /* request some modules */
1658 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1659 request_module("saa711x");
1660 if (dev->decoder == EM28XX_TVP5150)
1661 request_module("tvp5150");
1663 request_module("tuner");
1664 if (dev->tda9887_conf)
1665 request_module("tda9887");
1667 errCode = em28xx_config(dev);
1669 em28xx_errdev("error configuring device\n");
1675 /* register i2c bus */
1676 em28xx_i2c_register(dev);
1678 /* Do board specific init and eeprom reading */
1679 em28xx_card_setup(dev);
1681 /* configure the device */
1682 em28xx_config_i2c(dev);
1686 errCode = em28xx_config(dev);
1688 #ifdef CONFIG_MODULES
1689 if (dev->has_msp34xx)
1690 request_module("msp3400");
1692 /* allocate and fill v4l2 device struct */
1693 dev->vdev = video_device_alloc();
1694 if (NULL == dev->vdev) {
1695 em28xx_errdev("cannot allocate video_device.\n");
1700 dev->vdev->type = VID_TYPE_CAPTURE;
1702 dev->vdev->type |= VID_TYPE_TUNER;
1703 dev->vdev->hardware = 0;
1704 dev->vdev->fops = &em28xx_v4l_fops;
1705 dev->vdev->minor = -1;
1706 dev->vdev->dev = &dev->udev->dev;
1707 dev->vdev->release = video_device_release;
1708 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1710 list_add_tail(&dev->devlist,&em28xx_devlist);
1712 /* register v4l2 device */
1714 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1715 em28xx_errdev("unable to register video device (error=%i).\n",
1718 list_del(&dev->devlist);
1719 video_device_release(dev->vdev);
1723 if (dev->has_msp34xx) {
1724 /* Send a reset to other chips via gpio */
1725 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1727 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1735 em28xx_info("V4L2 device registered as /dev/video%d\n",
1742 * em28xx_usb_probe()
1743 * checks for supported devices
1745 static int em28xx_usb_probe(struct usb_interface *interface,
1746 const struct usb_device_id *id)
1748 const struct usb_endpoint_descriptor *endpoint;
1749 struct usb_device *udev;
1750 struct em28xx *dev = NULL;
1751 int retval = -ENODEV;
1752 int model,i,nr,ifnum;
1754 udev = usb_get_dev(interface_to_usbdev(interface));
1755 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1758 /* Don't register audio interfaces */
1759 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1760 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1761 udev->descriptor.idVendor,udev->descriptor.idProduct,
1763 interface->altsetting[0].desc.bInterfaceClass);
1767 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1768 udev->descriptor.idVendor,udev->descriptor.idProduct,
1770 interface->altsetting[0].desc.bInterfaceClass);
1772 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1774 /* check if the the device has the iso in endpoint at the correct place */
1775 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1776 USB_ENDPOINT_XFER_ISOC) {
1777 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1780 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1781 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1785 model=id->driver_info;
1786 nr=interface->minor;
1788 if (nr>EM28XX_MAXBOARDS) {
1789 printk ("em28xx: Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1793 /* allocate memory for our device state and initialize it */
1794 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1796 em28xx_err(DRIVER_NAME ": out of memory!\n");
1799 memset(dev, 0, sizeof(*dev));
1801 snprintf(dev->name, 29, "em28xx #%d", nr);
1803 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1806 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1807 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1808 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1809 "%s: workaround that. Redirect complaints to the vendor of\n"
1810 "%s: the TV card. Best regards,\n"
1812 dev->name,dev->name,dev->name,dev->name,dev->name);
1813 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1815 for (i = 0; i < em28xx_bcount; i++) {
1816 printk("%s: card=%d -> %s\n",
1817 dev->name, i, em28xx_boards[i].name);
1821 /* allocate device struct */
1822 retval = em28xx_init_dev(&dev, udev, nr, model);
1826 em28xx_info("Found %s\n", em28xx_boards[model].name);
1828 /* save our data pointer in this interface device */
1829 usb_set_intfdata(interface, dev);
1834 * em28xx_usb_disconnect()
1835 * called when the device gets diconencted
1836 * video device will be unregistered on v4l2_close in case it is still open
1838 static void em28xx_usb_disconnect(struct usb_interface *interface)
1840 struct em28xx *dev = usb_get_intfdata(interface);
1841 usb_set_intfdata(interface, NULL);
1846 down_write(&em28xx_disconnect);
1850 em28xx_info("disconnecting %s\n", dev->vdev->name);
1852 wake_up_interruptible_all(&dev->open);
1856 ("device /dev/video%d is open! Deregistration and memory "
1857 "deallocation are deferred on close.\n", dev->vdev->minor);
1858 dev->state |= DEV_MISCONFIGURED;
1859 em28xx_uninit_isoc(dev);
1860 dev->state |= DEV_DISCONNECTED;
1861 wake_up_interruptible(&dev->wait_frame);
1862 wake_up_interruptible(&dev->wait_stream);
1864 dev->state |= DEV_DISCONNECTED;
1865 em28xx_release_resources(dev);
1873 up_write(&em28xx_disconnect);
1877 static struct usb_driver em28xx_usb_driver = {
1878 .owner = THIS_MODULE,
1880 .probe = em28xx_usb_probe,
1881 .disconnect = em28xx_usb_disconnect,
1882 .id_table = em28xx_id_table,
1885 static int __init em28xx_module_init(void)
1889 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1890 (EM28XX_VERSION_CODE >> 16) & 0xff,
1891 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1893 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1894 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1897 /* register this driver with the USB subsystem */
1898 result = usb_register(&em28xx_usb_driver);
1900 em28xx_err(DRIVER_NAME
1901 " usb_register failed. Error number %d.\n", result);
1906 static void __exit em28xx_module_exit(void)
1908 /* deregister this driver with the USB subsystem */
1909 usb_deregister(&em28xx_usb_driver);
1912 module_init(em28xx_module_init);
1913 module_exit(em28xx_module_exit);