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 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/usb.h>
32 #include <linux/i2c.h>
33 #include <linux/version.h>
34 #include <linux/video_decoder.h>
35 #include <linux/mutex.h>
38 #include <media/tuner.h>
39 #include <media/v4l2-common.h>
41 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
42 "Markus Rechberger <mrechberger@gmail.com>, " \
43 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
44 "Sascha Sommer <saschasommer@freenet.de>"
46 #define DRIVER_NAME "em28xx"
47 #define DRIVER_DESC "Empia em28xx based USB video device driver"
48 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
50 #define em28xx_videodbg(fmt, arg...) do {\
52 printk(KERN_INFO "%s %s :"fmt, \
53 dev->name, __FUNCTION__ , ##arg); } while (0)
55 MODULE_AUTHOR(DRIVER_AUTHOR);
56 MODULE_DESCRIPTION(DRIVER_DESC);
57 MODULE_LICENSE("GPL");
59 static LIST_HEAD(em28xx_devlist);
61 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
62 module_param_array(card, int, NULL, 0444);
63 MODULE_PARM_DESC(card,"card type");
65 static int tuner = -1;
66 module_param(tuner, int, 0444);
67 MODULE_PARM_DESC(tuner, "tuner type");
69 static unsigned int video_debug = 0;
70 module_param(video_debug,int,0644);
71 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
73 /* supported tv norms */
74 static struct em28xx_tvnorm tvnorms[] = {
78 .mode = VIDEO_MODE_PAL,
82 .mode = VIDEO_MODE_NTSC,
86 .mode = VIDEO_MODE_SECAM,
90 .mode = VIDEO_MODE_PAL,
94 static const unsigned char saa7114_i2c_init[] = {
95 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
96 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
97 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
98 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
99 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
100 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
101 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
102 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
103 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
104 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
105 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
106 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
107 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
111 #define TVNORMS ARRAY_SIZE(tvnorms)
113 /* supported controls */
114 /* Common to all boards */
115 static struct v4l2_queryctrl em28xx_qctrl[] = {
117 .id = V4L2_CID_AUDIO_VOLUME,
118 .type = V4L2_CTRL_TYPE_INTEGER,
123 .default_value = 0x1f,
126 .id = V4L2_CID_AUDIO_MUTE,
127 .type = V4L2_CTRL_TYPE_BOOLEAN,
137 /* FIXME: These are specific to saa711x - should be moved to its code */
138 static struct v4l2_queryctrl saa711x_qctrl[] = {
140 .id = V4L2_CID_BRIGHTNESS,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Brightness",
149 .id = V4L2_CID_CONTRAST,
150 .type = V4L2_CTRL_TYPE_INTEGER,
155 .default_value = 0x10,
158 .id = V4L2_CID_SATURATION,
159 .type = V4L2_CTRL_TYPE_INTEGER,
160 .name = "Saturation",
164 .default_value = 0x10,
167 .id = V4L2_CID_RED_BALANCE,
168 .type = V4L2_CTRL_TYPE_INTEGER,
169 .name = "Red chroma balance",
176 .id = V4L2_CID_BLUE_BALANCE,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 .name = "Blue chroma balance",
185 .id = V4L2_CID_GAMMA,
186 .type = V4L2_CTRL_TYPE_INTEGER,
191 .default_value = 0x20,
196 static struct usb_driver em28xx_usb_driver;
198 static DEFINE_MUTEX(em28xx_sysfs_lock);
199 static DECLARE_RWSEM(em28xx_disconnect);
201 /********************* v4l2 interface ******************************************/
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 static 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;
280 static void video_mux(struct em28xx *dev, int index)
284 input = INPUT(index)->vmux;
285 dev->ctl_input = index;
286 dev->ctl_ainput = INPUT(index)->amux;
288 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
291 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
293 if (dev->has_msp34xx) {
295 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
296 em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
297 ainput = EM28XX_AUDIO_SRC_TUNER;
298 em28xx_audio_source(dev, ainput);
300 switch (dev->ctl_ainput) {
302 ainput = EM28XX_AUDIO_SRC_TUNER;
305 ainput = EM28XX_AUDIO_SRC_LINE;
307 em28xx_audio_source(dev, ainput);
313 * inits the device and starts isoc transfer
315 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
317 int minor = iminor(inode);
319 struct em28xx *h,*dev = NULL;
320 struct list_head *list;
322 list_for_each(list,&em28xx_devlist) {
323 h = list_entry(list, struct em28xx, devlist);
324 if (h->vdev->minor == minor) {
329 filp->private_data=dev;
332 em28xx_videodbg("users=%d\n", dev->users);
334 if (!down_read_trylock(&em28xx_disconnect))
338 em28xx_warn("this driver can be opened only once\n");
339 up_read(&em28xx_disconnect);
343 /* if(dev->vbi_dev->minor == minor){
344 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
346 if (dev->vdev->minor == minor) {
347 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
350 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
351 spin_lock_init(&dev->queue_lock);
352 init_waitqueue_head(&dev->wait_frame);
353 init_waitqueue_head(&dev->wait_stream);
357 em28xx_set_alternate(dev);
359 dev->width = norm_maxw(dev);
360 dev->height = norm_maxh(dev);
361 dev->frame_size = dev->width * dev->height * 2;
362 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
363 dev->bytesperline = dev->width * 2;
367 em28xx_capture_start(dev, 1);
368 em28xx_resolution_set(dev);
370 /* device needs to be initialized before isoc transfer */
373 /* start the transfer */
374 errCode = em28xx_init_isoc(dev);
379 filp->private_data = dev;
381 dev->stream = STREAM_OFF;
385 em28xx_empty_framequeues(dev);
387 dev->state |= DEV_INITIALIZED;
393 up_read(&em28xx_disconnect);
398 * em28xx_realease_resources()
399 * unregisters the v4l2,i2c and usb devices
400 * called when the device gets disconected or at module unload
402 static void em28xx_release_resources(struct em28xx *dev)
404 mutex_lock(&em28xx_sysfs_lock);
406 em28xx_info("V4L2 device /dev/video%d deregistered\n",
408 list_del(&dev->devlist);
409 video_unregister_device(dev->vdev);
410 /* video_unregister_device(dev->vbi_dev); */
411 em28xx_i2c_unregister(dev);
412 usb_put_dev(dev->udev);
413 mutex_unlock(&em28xx_sysfs_lock);
417 * em28xx_v4l2_close()
418 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
420 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
423 struct em28xx *dev=filp->private_data;
425 em28xx_videodbg("users=%d\n", dev->users);
429 em28xx_uninit_isoc(dev);
431 em28xx_release_buffers(dev);
433 /* the device is already disconnect, free the remaining resources */
434 if (dev->state & DEV_DISCONNECTED) {
435 em28xx_release_resources(dev);
441 /* set alternate 0 */
443 em28xx_videodbg("setting alternate 0\n");
444 errCode = usb_set_interface(dev->udev, 0, 0);
446 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
451 wake_up_interruptible_nr(&dev->open, 1);
458 * will allocate buffers when called for the first time
461 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
464 struct em28xx_frame_t *f, *i;
465 unsigned long lock_flags;
467 struct em28xx *dev = filp->private_data;
469 if (down_interruptible(&dev->fileop_lock))
472 if (dev->state & DEV_DISCONNECTED) {
473 em28xx_videodbg("device not present\n");
474 up(&dev->fileop_lock);
478 if (dev->state & DEV_MISCONFIGURED) {
479 em28xx_videodbg("device misconfigured; close and open it again\n");
480 up(&dev->fileop_lock);
484 if (dev->io == IO_MMAP) {
485 em28xx_videodbg ("IO method is set to mmap; close and open"
486 " the device again to choose the read method\n");
487 up(&dev->fileop_lock);
491 if (dev->io == IO_NONE) {
492 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
493 em28xx_errdev("read failed, not enough memory\n");
494 up(&dev->fileop_lock);
498 dev->stream = STREAM_ON;
499 em28xx_queue_unusedframes(dev);
503 up(&dev->fileop_lock);
507 if (list_empty(&dev->outqueue)) {
508 if (filp->f_flags & O_NONBLOCK) {
509 up(&dev->fileop_lock);
512 ret = wait_event_interruptible
514 (!list_empty(&dev->outqueue)) ||
515 (dev->state & DEV_DISCONNECTED));
517 up(&dev->fileop_lock);
520 if (dev->state & DEV_DISCONNECTED) {
521 up(&dev->fileop_lock);
526 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
528 spin_lock_irqsave(&dev->queue_lock, lock_flags);
529 list_for_each_entry(i, &dev->outqueue, frame)
531 INIT_LIST_HEAD(&dev->outqueue);
532 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
534 em28xx_queue_unusedframes(dev);
536 if (count > f->buf.length)
537 count = f->buf.length;
539 if (copy_to_user(buf, f->bufmem, count)) {
540 up(&dev->fileop_lock);
545 up(&dev->fileop_lock);
552 * will allocate buffers when called for the first time
554 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
556 unsigned int mask = 0;
557 struct em28xx *dev = filp->private_data;
559 if (down_interruptible(&dev->fileop_lock))
562 if (dev->state & DEV_DISCONNECTED) {
563 em28xx_videodbg("device not present\n");
564 } else if (dev->state & DEV_MISCONFIGURED) {
565 em28xx_videodbg("device is misconfigured; close and open it again\n");
567 if (dev->io == IO_NONE) {
568 if (!em28xx_request_buffers
569 (dev, EM28XX_NUM_READ_FRAMES)) {
571 ("poll() failed, not enough memory\n");
574 dev->stream = STREAM_ON;
578 if (dev->io == IO_READ) {
579 em28xx_queue_unusedframes(dev);
580 poll_wait(filp, &dev->wait_frame, wait);
582 if (!list_empty(&dev->outqueue))
583 mask |= POLLIN | POLLRDNORM;
585 up(&dev->fileop_lock);
591 up(&dev->fileop_lock);
598 static void em28xx_vm_open(struct vm_area_struct *vma)
600 struct em28xx_frame_t *f = vma->vm_private_data;
607 static void em28xx_vm_close(struct vm_area_struct *vma)
609 /* NOTE: buffers are not freed here */
610 struct em28xx_frame_t *f = vma->vm_private_data;
614 static struct vm_operations_struct em28xx_vm_ops = {
615 .open = em28xx_vm_open,
616 .close = em28xx_vm_close,
622 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
624 unsigned long size = vma->vm_end - vma->vm_start,
625 start = vma->vm_start;
629 struct em28xx *dev = filp->private_data;
631 if (down_interruptible(&dev->fileop_lock))
634 if (dev->state & DEV_DISCONNECTED) {
635 em28xx_videodbg("mmap: device not present\n");
636 up(&dev->fileop_lock);
640 if (dev->state & DEV_MISCONFIGURED) {
641 em28xx_videodbg ("mmap: Device is misconfigured; close and "
643 up(&dev->fileop_lock);
647 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
648 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
649 up(&dev->fileop_lock);
653 for (i = 0; i < dev->num_frames; i++) {
654 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
657 if (i == dev->num_frames) {
658 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
659 up(&dev->fileop_lock);
663 /* VM_IO is eventually going to replace PageReserved altogether */
664 vma->vm_flags |= VM_IO;
665 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
667 pos = dev->frame[i].bufmem;
668 while (size > 0) { /* size is page-aligned */
669 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
670 em28xx_videodbg("mmap: vm_insert_page failed\n");
671 up(&dev->fileop_lock);
679 vma->vm_ops = &em28xx_vm_ops;
680 vma->vm_private_data = &dev->frame[i];
683 up(&dev->fileop_lock);
689 * return the current saturation, brightness or contrast, mute state
691 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
694 case V4L2_CID_AUDIO_MUTE:
695 ctrl->value = dev->mute;
697 case V4L2_CID_AUDIO_VOLUME:
698 ctrl->value = dev->volume;
705 /*FIXME: should be moved to saa711x */
706 static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
710 case V4L2_CID_BRIGHTNESS:
711 if ((tmp = em28xx_brightness_get(dev)) < 0)
713 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
715 case V4L2_CID_CONTRAST:
716 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
719 case V4L2_CID_SATURATION:
720 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
723 case V4L2_CID_RED_BALANCE:
724 if ((tmp = em28xx_v_balance_get(dev)) < 0)
726 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
728 case V4L2_CID_BLUE_BALANCE:
729 if ((tmp = em28xx_u_balance_get(dev)) < 0)
731 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
734 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
744 * mute or set new saturation, brightness or contrast
746 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
749 case V4L2_CID_AUDIO_MUTE:
750 if (ctrl->value != dev->mute) {
751 dev->mute = ctrl->value;
752 em28xx_audio_usb_mute(dev, ctrl->value);
753 return em28xx_audio_analog_set(dev);
756 case V4L2_CID_AUDIO_VOLUME:
757 dev->volume = ctrl->value;
758 return em28xx_audio_analog_set(dev);
764 /*FIXME: should be moved to saa711x */
765 static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
768 case V4L2_CID_BRIGHTNESS:
769 return em28xx_brightness_set(dev, ctrl->value);
770 case V4L2_CID_CONTRAST:
771 return em28xx_contrast_set(dev, ctrl->value);
772 case V4L2_CID_SATURATION:
773 return em28xx_saturation_set(dev, ctrl->value);
774 case V4L2_CID_RED_BALANCE:
775 return em28xx_v_balance_set(dev, ctrl->value);
776 case V4L2_CID_BLUE_BALANCE:
777 return em28xx_u_balance_set(dev, ctrl->value);
779 return em28xx_gamma_set(dev, ctrl->value);
786 * em28xx_stream_interrupt()
789 static int em28xx_stream_interrupt(struct em28xx *dev)
793 /* stop reading from the device */
795 dev->stream = STREAM_INTERRUPT;
796 ret = wait_event_timeout(dev->wait_stream,
797 (dev->stream == STREAM_OFF) ||
798 (dev->state & DEV_DISCONNECTED),
800 if (dev->state & DEV_DISCONNECTED)
803 dev->state |= DEV_MISCONFIGURED;
804 em28xx_videodbg("device is misconfigured; close and "
805 "open /dev/video%d again\n", dev->vdev->minor);
812 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
814 unsigned int hscale, vscale;
815 unsigned int maxh, maxw;
817 maxw = norm_maxw(dev);
818 maxh = norm_maxh(dev);
820 /* width must even because of the YUYV format */
821 /* height must be even because of interlacing */
834 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
836 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
838 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
840 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
842 /* set new image size */
844 dev->height = height;
845 dev->frame_size = dev->width * dev->height * 2;
846 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
847 dev->bytesperline = dev->width * 2;
848 dev->hscale = hscale;
849 dev->vscale = vscale;
851 em28xx_resolution_set(dev);
857 * em28xx_v4l2_do_ioctl()
858 * This function is _not_ called directly, but from
859 * em28xx_v4l2_ioctl. Userspace
860 * copying is done already, arg is a kernel pointer.
862 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
863 struct em28xx *dev, unsigned int cmd, void *arg,
864 v4l2_kioctl driver_ioctl)
869 /* ---------- tv norms ---------- */
872 struct v4l2_standard *e = arg;
878 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
879 tvnorms[e->index].name);
887 v4l2_std_id *id = arg;
889 *id = dev->tvnorm->id;
894 v4l2_std_id *id = arg;
897 for (i = 0; i < TVNORMS; i++)
898 if (*id == tvnorms[i].id)
901 for (i = 0; i < TVNORMS; i++)
902 if (*id & tvnorms[i].id)
908 dev->tvnorm = &tvnorms[i];
910 em28xx_set_norm(dev, dev->width, dev->height);
913 dev->width=norm_maxw(dev);
914 dev->height=norm_maxh(dev);
915 dev->frame_size=dev->width*dev->height*2;
916 dev->field_size=dev->frame_size>>1;
917 dev->bytesperline=dev->width*2;
921 em28xx_resolution_set(dev);
924 em28xx_uninit_isoc(dev);
925 em28xx_set_alternate(dev);
926 em28xx_capture_start(dev, 1);
927 em28xx_resolution_set(dev);
928 em28xx_init_isoc(dev);
930 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
932 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
940 /* ------ input switching ---------- */
941 case VIDIOC_ENUMINPUT:
943 struct v4l2_input *i = arg;
945 static const char *iname[] = {
946 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
947 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
948 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
949 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
950 [EM28XX_VMUX_SVIDEO] = "S-Video",
951 [EM28XX_VMUX_TELEVISION] = "Television",
952 [EM28XX_VMUX_CABLE] = "Cable TV",
953 [EM28XX_VMUX_DVB] = "DVB",
954 [EM28XX_VMUX_DEBUG] = "for debug only",
958 if (n >= MAX_EM28XX_INPUT)
960 if (0 == INPUT(n)->type)
962 memset(i, 0, sizeof(*i));
964 i->type = V4L2_INPUT_TYPE_CAMERA;
965 strcpy(i->name, iname[INPUT(n)->type]);
966 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
967 (EM28XX_VMUX_CABLE == INPUT(n)->type))
968 i->type = V4L2_INPUT_TYPE_TUNER;
969 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
970 i->std |= tvnorms[n].id;
986 if (*index >= MAX_EM28XX_INPUT)
988 if (0 == INPUT(*index)->type)
992 video_mux(dev, *index);
1000 struct v4l2_audio *a = arg;
1001 unsigned int index = a->index;
1005 memset(a, 0, sizeof(*a));
1006 index = dev->ctl_ainput;
1009 strcpy(a->name, "Television");
1011 strcpy(a->name, "Line In");
1013 a->capability = V4L2_AUDCAP_STEREO;
1018 case VIDIOC_S_AUDIO:
1020 struct v4l2_audio *a = arg;
1021 if (a->index != dev->ctl_ainput)
1027 /* --- controls ---------------------------------------------- */
1028 case VIDIOC_QUERYCTRL:
1030 struct v4l2_queryctrl *qc = arg;
1033 memset(qc,0,sizeof(*qc));
1036 if (!dev->has_msp34xx) {
1037 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1038 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1039 memcpy(qc, &(em28xx_qctrl[i]),
1045 if (dev->decoder == EM28XX_TVP5150) {
1046 em28xx_i2c_call_clients(dev,cmd,qc);
1052 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1053 if (qc->id && qc->id == saa711x_qctrl[i].id) {
1054 memcpy(qc, &(saa711x_qctrl[i]),
1065 struct v4l2_control *ctrl = arg;
1068 if (!dev->has_msp34xx)
1069 retval=em28xx_get_ctrl(dev, ctrl);
1070 if (retval==-EINVAL) {
1071 if (dev->decoder == EM28XX_TVP5150) {
1072 em28xx_i2c_call_clients(dev,cmd,arg);
1076 return saa711x_get_ctrl(dev, ctrl);
1077 } else return retval;
1082 struct v4l2_control *ctrl = arg;
1085 if (!dev->has_msp34xx){
1086 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1087 if (ctrl->id == em28xx_qctrl[i].id) {
1089 em28xx_qctrl[i].minimum
1091 em28xx_qctrl[i].maximum)
1093 return em28xx_set_ctrl(dev, ctrl);
1098 if (dev->decoder == EM28XX_TVP5150) {
1099 em28xx_i2c_call_clients(dev,cmd,arg);
1101 } else if (!dev->has_msp34xx) {
1102 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1103 if (ctrl->id == em28xx_qctrl[i].id) {
1105 em28xx_qctrl[i].minimum
1107 em28xx_qctrl[i].maximum)
1109 return em28xx_set_ctrl(dev, ctrl);
1112 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1113 if (ctrl->id == saa711x_qctrl[i].id) {
1115 saa711x_qctrl[i].minimum
1117 saa711x_qctrl[i].maximum)
1119 return saa711x_set_ctrl(dev, ctrl);
1127 /* --- tuner ioctls ------------------------------------------ */
1128 case VIDIOC_G_TUNER:
1130 struct v4l2_tuner *t = arg;
1136 memset(t, 0, sizeof(*t));
1137 strcpy(t->name, "Tuner");
1138 t->type = V4L2_TUNER_ANALOG_TV;
1139 t->capability = V4L2_TUNER_CAP_NORM;
1140 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1141 /* t->signal = 0xffff;*/
1142 /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1143 /* No way to get signal strength? */
1145 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1149 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1151 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1155 case VIDIOC_S_TUNER:
1157 struct v4l2_tuner *t = arg;
1162 memset(t, 0, sizeof(*t));
1163 strcpy(t->name, "Tuner");
1164 t->type = V4L2_TUNER_ANALOG_TV;
1165 t->capability = V4L2_TUNER_CAP_NORM;
1166 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1167 /* t->signal = 0xffff; */
1168 /* No way to get signal strength? */
1170 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1174 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1176 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1180 case VIDIOC_G_FREQUENCY:
1182 struct v4l2_frequency *f = arg;
1184 memset(f, 0, sizeof(*f));
1185 f->type = V4L2_TUNER_ANALOG_TV;
1186 f->frequency = dev->ctl_freq;
1190 case VIDIOC_S_FREQUENCY:
1192 struct v4l2_frequency *f = arg;
1197 if (V4L2_TUNER_ANALOG_TV != f->type)
1201 dev->ctl_freq = f->frequency;
1202 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1207 case VIDIOC_CROPCAP:
1209 struct v4l2_cropcap *cc = arg;
1211 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1213 cc->bounds.left = 0;
1215 cc->bounds.width = dev->width;
1216 cc->bounds.height = dev->height;
1217 cc->defrect = cc->bounds;
1218 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1219 cc->pixelaspect.denominator = 59;
1222 case VIDIOC_STREAMON:
1226 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1227 || dev->io != IO_MMAP)
1230 if (list_empty(&dev->inqueue))
1233 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1235 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1239 case VIDIOC_STREAMOFF:
1244 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1245 || dev->io != IO_MMAP)
1248 if (dev->stream == STREAM_ON) {
1249 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1250 if ((ret = em28xx_stream_interrupt(dev)))
1253 em28xx_empty_framequeues(dev);
1258 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1265 * em28xx_v4l2_do_ioctl()
1266 * This function is _not_ called directly, but from
1267 * em28xx_v4l2_ioctl. Userspace
1268 * copying is done already, arg is a kernel pointer.
1270 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1271 unsigned int cmd, void *arg)
1273 struct em28xx *dev = filp->private_data;
1278 if (video_debug > 1)
1279 v4l_print_ioctl(dev->name,cmd);
1283 /* --- capabilities ------------------------------------------ */
1284 case VIDIOC_QUERYCAP:
1286 struct v4l2_capability *cap = arg;
1288 memset(cap, 0, sizeof(*cap));
1289 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1290 strlcpy(cap->card, em28xx_boards[dev->model].name,
1292 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1293 sizeof(cap->bus_info));
1294 cap->version = EM28XX_VERSION_CODE;
1296 V4L2_CAP_VIDEO_CAPTURE |
1298 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1300 cap->capabilities |= V4L2_CAP_TUNER;
1304 /* --- capture ioctls ---------------------------------------- */
1305 case VIDIOC_ENUM_FMT:
1307 struct v4l2_fmtdesc *fmtd = arg;
1309 if (fmtd->index != 0)
1311 memset(fmtd, 0, sizeof(*fmtd));
1312 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1313 strcpy(fmtd->description, "Packed YUY2");
1314 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1315 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1321 struct v4l2_format *format = arg;
1323 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
1325 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1326 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1327 V4L2_BUF_TYPE_VBI_CAPTURE ?
1328 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1331 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1334 format->fmt.pix.width = dev->width;
1335 format->fmt.pix.height = dev->height;
1336 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1337 format->fmt.pix.bytesperline = dev->bytesperline;
1338 format->fmt.pix.sizeimage = dev->frame_size;
1339 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1340 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1342 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
1347 case VIDIOC_TRY_FMT:
1350 struct v4l2_format *format = arg;
1353 int width = format->fmt.pix.width;
1354 int height = format->fmt.pix.height;
1355 unsigned int hscale, vscale;
1356 unsigned int maxh, maxw;
1358 maxw = norm_maxw(dev);
1359 maxh = norm_maxh(dev);
1361 /* int both_fields; */
1363 em28xx_videodbg("%s: type=%s\n",
1365 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1368 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1369 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1370 V4L2_BUF_TYPE_VBI_CAPTURE ?
1371 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1374 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1377 em28xx_videodbg("%s: requested %dx%d\n",
1379 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1380 "VIDIOC_S_FMT", format->fmt.pix.width,
1381 format->fmt.pix.height);
1383 /* FIXME: Move some code away from here */
1384 /* width must even because of the YUYV format */
1385 /* height must be even because of interlacing */
1399 /* the em2800 can only scale down to 50% */
1400 if(height % (maxh / 2))
1402 if(width % (maxw / 2))
1404 /* according to empiatech support */
1405 /* the MaxPacketSize is to small to support */
1406 /* framesizes larger than 640x480 @ 30 fps */
1407 /* or 640x576 @ 25 fps. As this would cut */
1408 /* of a part of the image we prefer */
1409 /* 360x576 or 360x480 for now */
1410 if(width == maxw && height == maxh)
1415 (((unsigned long)maxw) << 12) / width - 4096L) >=
1419 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1422 (((unsigned long)maxh) << 12) / height - 4096L) >=
1426 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1428 format->fmt.pix.width = width;
1429 format->fmt.pix.height = height;
1430 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1431 format->fmt.pix.bytesperline = width * 2;
1432 format->fmt.pix.sizeimage = width * 2 * height;
1433 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1434 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1436 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
1438 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1439 "VIDIOC_S_FMT", format->fmt.pix.width,
1440 format->fmt.pix.height, hscale, vscale);
1442 if (cmd == VIDIOC_TRY_FMT)
1445 for (i = 0; i < dev->num_frames; i++)
1446 if (dev->frame[i].vma_use_count) {
1447 em28xx_videodbg("VIDIOC_S_FMT failed. "
1448 "Unmap the buffers first.\n");
1452 /* stop io in case it is already in progress */
1453 if (dev->stream == STREAM_ON) {
1454 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
1455 if ((ret = em28xx_stream_interrupt(dev)))
1459 em28xx_release_buffers(dev);
1462 /* set new image size */
1464 dev->height = height;
1465 dev->frame_size = dev->width * dev->height * 2;
1466 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1467 dev->bytesperline = dev->width * 2;
1468 dev->hscale = hscale;
1469 dev->vscale = vscale;
1470 /* dev->both_fileds = both_fileds; */
1471 em28xx_uninit_isoc(dev);
1472 em28xx_set_alternate(dev);
1473 em28xx_capture_start(dev, 1);
1474 em28xx_resolution_set(dev);
1475 em28xx_init_isoc(dev);
1480 /* --- streaming capture ------------------------------------- */
1481 case VIDIOC_REQBUFS:
1483 struct v4l2_requestbuffers *rb = arg;
1487 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1488 rb->memory != V4L2_MEMORY_MMAP)
1491 if (dev->io == IO_READ) {
1492 em28xx_videodbg ("method is set to read;"
1493 " close and open the device again to"
1494 " choose the mmap I/O method\n");
1498 for (i = 0; i < dev->num_frames; i++)
1499 if (dev->frame[i].vma_use_count) {
1500 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1504 if (dev->stream == STREAM_ON) {
1505 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1506 if ((ret = em28xx_stream_interrupt(dev)))
1510 em28xx_empty_framequeues(dev);
1512 em28xx_release_buffers(dev);
1515 em28xx_request_buffers(dev, rb->count);
1517 dev->frame_current = NULL;
1519 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1521 dev->io = rb->count ? IO_MMAP : IO_NONE;
1525 case VIDIOC_QUERYBUF:
1527 struct v4l2_buffer *b = arg;
1529 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1530 b->index >= dev->num_frames || dev->io != IO_MMAP)
1533 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1535 if (dev->frame[b->index].vma_use_count) {
1536 b->flags |= V4L2_BUF_FLAG_MAPPED;
1538 if (dev->frame[b->index].state == F_DONE)
1539 b->flags |= V4L2_BUF_FLAG_DONE;
1540 else if (dev->frame[b->index].state != F_UNUSED)
1541 b->flags |= V4L2_BUF_FLAG_QUEUED;
1546 struct v4l2_buffer *b = arg;
1547 unsigned long lock_flags;
1549 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1550 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1554 if (dev->frame[b->index].state != F_UNUSED) {
1557 dev->frame[b->index].state = F_QUEUED;
1559 /* add frame to fifo */
1560 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1561 list_add_tail(&dev->frame[b->index].frame,
1563 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1569 struct v4l2_buffer *b = arg;
1570 struct em28xx_frame_t *f;
1571 unsigned long lock_flags;
1574 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1575 || dev->io != IO_MMAP)
1578 if (list_empty(&dev->outqueue)) {
1579 if (dev->stream == STREAM_OFF)
1581 if (filp->f_flags & O_NONBLOCK)
1583 ret = wait_event_interruptible
1585 (!list_empty(&dev->outqueue)) ||
1586 (dev->state & DEV_DISCONNECTED));
1589 if (dev->state & DEV_DISCONNECTED)
1593 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1594 f = list_entry(dev->outqueue.next,
1595 struct em28xx_frame_t, frame);
1596 list_del(dev->outqueue.next);
1597 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1599 f->state = F_UNUSED;
1600 memcpy(b, &f->buf, sizeof(*b));
1602 if (f->vma_use_count)
1603 b->flags |= V4L2_BUF_FLAG_MAPPED;
1608 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1609 em28xx_video_do_ioctl);
1615 * em28xx_v4l2_ioctl()
1616 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1618 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1619 unsigned int cmd, unsigned long arg)
1622 struct em28xx *dev = filp->private_data;
1624 if (down_interruptible(&dev->fileop_lock))
1625 return -ERESTARTSYS;
1627 if (dev->state & DEV_DISCONNECTED) {
1628 em28xx_errdev("v4l2 ioctl: device not present\n");
1629 up(&dev->fileop_lock);
1633 if (dev->state & DEV_MISCONFIGURED) {
1635 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1636 up(&dev->fileop_lock);
1640 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1642 up(&dev->fileop_lock);
1647 static struct file_operations em28xx_v4l_fops = {
1648 .owner = THIS_MODULE,
1649 .open = em28xx_v4l2_open,
1650 .release = em28xx_v4l2_close,
1651 .ioctl = em28xx_v4l2_ioctl,
1652 .read = em28xx_v4l2_read,
1653 .poll = em28xx_v4l2_poll,
1654 .mmap = em28xx_v4l2_mmap,
1655 .llseek = no_llseek,
1656 .compat_ioctl = v4l_compat_ioctl32,
1660 /******************************** usb interface *****************************************/
1664 * allocates and inits the device structs, registers i2c bus and v4l device
1666 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1667 int minor, int model)
1669 struct em28xx *dev = *devhandle;
1670 int retval = -ENOMEM;
1672 unsigned int maxh, maxw;
1676 init_MUTEX(&dev->lock);
1677 init_waitqueue_head(&dev->open);
1679 dev->em28xx_write_regs = em28xx_write_regs;
1680 dev->em28xx_read_reg = em28xx_read_reg;
1681 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1682 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1683 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1684 dev->is_em2800 = em28xx_boards[model].is_em2800;
1685 dev->has_tuner = em28xx_boards[model].has_tuner;
1686 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1687 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1688 dev->decoder = em28xx_boards[model].decoder;
1691 dev->tuner_type = tuner;
1693 dev->tuner_type = em28xx_boards[model].tuner_type;
1695 dev->video_inputs = em28xx_boards[model].vchannels;
1697 for (i = 0; i < TVNORMS; i++)
1698 if (em28xx_boards[model].norm == tvnorms[i].mode)
1703 dev->tvnorm = &tvnorms[i]; /* set default norm */
1705 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1707 maxw = norm_maxw(dev);
1708 maxh = norm_maxh(dev);
1710 /* set default image size */
1713 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1714 dev->field_size = dev->width * dev->height;
1716 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1717 dev->bytesperline = dev->width * 2;
1722 /* setup video picture settings for saa7113h */
1723 memset(&dev->vpic, 0, sizeof(dev->vpic));
1724 dev->vpic.colour = 128 << 8;
1725 dev->vpic.hue = 128 << 8;
1726 dev->vpic.brightness = 128 << 8;
1727 dev->vpic.contrast = 192 << 8;
1728 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1729 dev->vpic.depth = 16;
1730 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1732 #ifdef CONFIG_MODULES
1733 /* request some modules */
1734 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1735 request_module("saa711x");
1736 if (dev->decoder == EM28XX_TVP5150)
1737 request_module("tvp5150");
1739 request_module("tuner");
1740 if (dev->tda9887_conf)
1741 request_module("tda9887");
1743 errCode = em28xx_config(dev);
1745 em28xx_errdev("error configuring device\n");
1751 /* register i2c bus */
1752 em28xx_i2c_register(dev);
1754 /* Do board specific init and eeprom reading */
1755 em28xx_card_setup(dev);
1757 /* configure the device */
1758 em28xx_config_i2c(dev);
1762 errCode = em28xx_config(dev);
1764 #ifdef CONFIG_MODULES
1765 if (dev->has_msp34xx)
1766 request_module("msp3400");
1768 /* allocate and fill v4l2 device struct */
1769 dev->vdev = video_device_alloc();
1770 if (NULL == dev->vdev) {
1771 em28xx_errdev("cannot allocate video_device.\n");
1776 dev->vdev->type = VID_TYPE_CAPTURE;
1778 dev->vdev->type |= VID_TYPE_TUNER;
1779 dev->vdev->hardware = 0;
1780 dev->vdev->fops = &em28xx_v4l_fops;
1781 dev->vdev->minor = -1;
1782 dev->vdev->dev = &dev->udev->dev;
1783 dev->vdev->release = video_device_release;
1784 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1786 list_add_tail(&dev->devlist,&em28xx_devlist);
1788 /* register v4l2 device */
1790 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1791 em28xx_errdev("unable to register video device (error=%i).\n",
1794 list_del(&dev->devlist);
1795 video_device_release(dev->vdev);
1799 if (dev->has_msp34xx) {
1800 /* Send a reset to other chips via gpio */
1801 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1803 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1811 em28xx_info("V4L2 device registered as /dev/video%d\n",
1818 * em28xx_usb_probe()
1819 * checks for supported devices
1821 static int em28xx_usb_probe(struct usb_interface *interface,
1822 const struct usb_device_id *id)
1824 const struct usb_endpoint_descriptor *endpoint;
1825 struct usb_device *udev;
1826 struct usb_interface *uif;
1827 struct em28xx *dev = NULL;
1828 int retval = -ENODEV;
1829 int model,i,nr,ifnum;
1831 udev = usb_get_dev(interface_to_usbdev(interface));
1832 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1835 /* Don't register audio interfaces */
1836 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1837 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1838 udev->descriptor.idVendor,udev->descriptor.idProduct,
1840 interface->altsetting[0].desc.bInterfaceClass);
1844 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1845 udev->descriptor.idVendor,udev->descriptor.idProduct,
1847 interface->altsetting[0].desc.bInterfaceClass);
1849 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1851 /* check if the the device has the iso in endpoint at the correct place */
1852 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1853 USB_ENDPOINT_XFER_ISOC) {
1854 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1857 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1858 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1862 model=id->driver_info;
1863 nr=interface->minor;
1865 if (nr>EM28XX_MAXBOARDS) {
1866 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1870 /* allocate memory for our device state and initialize it */
1871 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1873 em28xx_err(DRIVER_NAME ": out of memory!\n");
1877 /* compute alternate max packet sizes */
1878 uif = udev->actconfig->interface[0];
1880 dev->num_alt=uif->num_altsetting;
1881 printk(DRIVER_NAME ": Alternate settings: %i\n",dev->num_alt);
1882 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1883 dev->alt_max_pkt_size = kmalloc(32*
1884 dev->num_alt,GFP_KERNEL);
1885 if (dev->alt_max_pkt_size == NULL) {
1886 em28xx_err(DRIVER_NAME ": out of memory!\n");
1890 for (i = 0; i < dev->num_alt ; i++) {
1891 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1893 dev->alt_max_pkt_size[i] =
1894 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1895 printk(DRIVER_NAME ": Alternate setting %i, max size= %i\n",i,
1896 dev->alt_max_pkt_size[i]);
1899 snprintf(dev->name, 29, "em28xx #%d", nr);
1901 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1904 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1905 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1906 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1907 "%s: workaround that. Redirect complaints to the vendor of\n"
1908 "%s: the TV card. Best regards,\n"
1910 dev->name,dev->name,dev->name,dev->name,dev->name);
1911 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1913 for (i = 0; i < em28xx_bcount; i++) {
1914 printk("%s: card=%d -> %s\n",
1915 dev->name, i, em28xx_boards[i].name);
1919 /* allocate device struct */
1920 retval = em28xx_init_dev(&dev, udev, nr, model);
1924 em28xx_info("Found %s\n", em28xx_boards[model].name);
1926 /* save our data pointer in this interface device */
1927 usb_set_intfdata(interface, dev);
1932 * em28xx_usb_disconnect()
1933 * called when the device gets diconencted
1934 * video device will be unregistered on v4l2_close in case it is still open
1936 static void em28xx_usb_disconnect(struct usb_interface *interface)
1938 struct em28xx *dev = usb_get_intfdata(interface);
1939 usb_set_intfdata(interface, NULL);
1941 /*FIXME: IR should be disconnected */
1947 down_write(&em28xx_disconnect);
1951 em28xx_info("disconnecting %s\n", dev->vdev->name);
1953 wake_up_interruptible_all(&dev->open);
1957 ("device /dev/video%d is open! Deregistration and memory "
1958 "deallocation are deferred on close.\n", dev->vdev->minor);
1959 dev->state |= DEV_MISCONFIGURED;
1960 em28xx_uninit_isoc(dev);
1961 dev->state |= DEV_DISCONNECTED;
1962 wake_up_interruptible(&dev->wait_frame);
1963 wake_up_interruptible(&dev->wait_stream);
1965 dev->state |= DEV_DISCONNECTED;
1966 em28xx_release_resources(dev);
1972 kfree(dev->alt_max_pkt_size);
1976 up_write(&em28xx_disconnect);
1979 static struct usb_driver em28xx_usb_driver = {
1981 .probe = em28xx_usb_probe,
1982 .disconnect = em28xx_usb_disconnect,
1983 .id_table = em28xx_id_table,
1986 static int __init em28xx_module_init(void)
1990 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1991 (EM28XX_VERSION_CODE >> 16) & 0xff,
1992 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1994 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1995 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1998 /* register this driver with the USB subsystem */
1999 result = usb_register(&em28xx_usb_driver);
2001 em28xx_err(DRIVER_NAME
2002 " usb_register failed. Error number %d.\n", result);
2007 static void __exit em28xx_module_exit(void)
2009 /* deregister this driver with the USB subsystem */
2010 usb_deregister(&em28xx_usb_driver);
2013 module_init(em28xx_module_init);
2014 module_exit(em28xx_module_exit);