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/bitmap.h>
32 #include <linux/usb.h>
33 #include <linux/i2c.h>
34 #include <linux/version.h>
35 #include <linux/video_decoder.h>
36 #include <linux/mutex.h>
39 #include <media/tuner.h>
40 #include <media/v4l2-common.h>
42 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
43 "Markus Rechberger <mrechberger@gmail.com>, " \
44 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
45 "Sascha Sommer <saschasommer@freenet.de>"
47 #define DRIVER_NAME "em28xx"
48 #define DRIVER_DESC "Empia em28xx based USB video device driver"
49 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
51 #define em28xx_videodbg(fmt, arg...) do {\
53 printk(KERN_INFO "%s %s :"fmt, \
54 dev->name, __FUNCTION__ , ##arg); } while (0)
56 MODULE_AUTHOR(DRIVER_AUTHOR);
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_LICENSE("GPL");
60 static LIST_HEAD(em28xx_devlist);
62 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
63 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65 module_param_array(card, int, NULL, 0444);
66 module_param_array(video_nr, int, NULL, 0444);
67 module_param_array(vbi_nr, int, NULL, 0444);
68 MODULE_PARM_DESC(card,"card type");
69 MODULE_PARM_DESC(video_nr,"video device numbers");
70 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
72 static int tuner = -1;
73 module_param(tuner, int, 0444);
74 MODULE_PARM_DESC(tuner, "tuner type");
76 static unsigned int video_debug = 0;
77 module_param(video_debug,int,0644);
78 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
80 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
81 static unsigned long em28xx_devused;
83 /* supported tv norms */
84 static struct em28xx_tvnorm tvnorms[] = {
88 .mode = VIDEO_MODE_PAL,
92 .mode = VIDEO_MODE_NTSC,
96 .mode = VIDEO_MODE_SECAM,
100 .mode = VIDEO_MODE_PAL,
104 static const unsigned char saa7114_i2c_init[] = {
105 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
106 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
107 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
108 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
109 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
110 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
111 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
112 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
113 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
114 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
115 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
116 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
117 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
121 #define TVNORMS ARRAY_SIZE(tvnorms)
123 /* supported controls */
124 /* Common to all boards */
125 static struct v4l2_queryctrl em28xx_qctrl[] = {
127 .id = V4L2_CID_AUDIO_VOLUME,
128 .type = V4L2_CTRL_TYPE_INTEGER,
133 .default_value = 0x1f,
136 .id = V4L2_CID_AUDIO_MUTE,
137 .type = V4L2_CTRL_TYPE_BOOLEAN,
147 /* FIXME: These are specific to saa711x - should be moved to its code */
148 static struct v4l2_queryctrl saa711x_qctrl[] = {
150 .id = V4L2_CID_BRIGHTNESS,
151 .type = V4L2_CTRL_TYPE_INTEGER,
152 .name = "Brightness",
159 .id = V4L2_CID_CONTRAST,
160 .type = V4L2_CTRL_TYPE_INTEGER,
165 .default_value = 0x10,
168 .id = V4L2_CID_SATURATION,
169 .type = V4L2_CTRL_TYPE_INTEGER,
170 .name = "Saturation",
174 .default_value = 0x10,
177 .id = V4L2_CID_RED_BALANCE,
178 .type = V4L2_CTRL_TYPE_INTEGER,
179 .name = "Red chroma balance",
186 .id = V4L2_CID_BLUE_BALANCE,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 .name = "Blue chroma balance",
195 .id = V4L2_CID_GAMMA,
196 .type = V4L2_CTRL_TYPE_INTEGER,
201 .default_value = 0x20,
206 static struct usb_driver em28xx_usb_driver;
208 static DEFINE_MUTEX(em28xx_sysfs_lock);
209 static DECLARE_RWSEM(em28xx_disconnect);
211 /********************* v4l2 interface ******************************************/
215 * inits registers with sane defaults
217 static int em28xx_config(struct em28xx *dev)
220 /* Sets I2C speed to 100 KHz */
221 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
223 /* enable vbi capturing */
225 em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1);
226 em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1);
227 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
229 em28xx_audio_usb_mute(dev, 1);
230 dev->mute = 1; /* maybe not the right place... */
232 em28xx_audio_analog_set(dev);
233 em28xx_audio_analog_setup(dev);
234 em28xx_outfmt_set_yuv422(dev);
235 em28xx_colorlevels_set_default(dev);
236 em28xx_compression_disable(dev);
242 * em28xx_config_i2c()
243 * configure i2c attached devices
245 static void em28xx_config_i2c(struct em28xx *dev)
247 struct v4l2_frequency f;
248 struct video_decoder_init em28xx_vdi = {.data = NULL };
251 /* configure decoder */
252 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
253 em28xx_vdi.data=saa7114_i2c_init;
254 em28xx_vdi.len=sizeof(saa7114_i2c_init);
258 em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
259 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
260 /* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
261 /* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
262 /* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
263 /* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
265 /* configure tuner */
267 f.type = V4L2_TUNER_ANALOG_TV;
268 f.frequency = 9076; /* FIXME:remove magic number */
269 dev->ctl_freq = f.frequency;
270 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
272 /* configure tda9887 */
275 /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
279 * em28xx_empty_framequeues()
280 * prepare queues for incoming and outgoing frames
282 static void em28xx_empty_framequeues(struct em28xx *dev)
286 INIT_LIST_HEAD(&dev->inqueue);
287 INIT_LIST_HEAD(&dev->outqueue);
289 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
290 dev->frame[i].state = F_UNUSED;
291 dev->frame[i].buf.bytesused = 0;
295 static void video_mux(struct em28xx *dev, int index)
299 input = INPUT(index)->vmux;
300 dev->ctl_input = index;
301 dev->ctl_ainput = INPUT(index)->amux;
303 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
306 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
308 if (dev->has_msp34xx) {
310 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
311 em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
312 ainput = EM28XX_AUDIO_SRC_TUNER;
313 em28xx_audio_source(dev, ainput);
315 switch (dev->ctl_ainput) {
317 ainput = EM28XX_AUDIO_SRC_TUNER;
320 ainput = EM28XX_AUDIO_SRC_LINE;
322 em28xx_audio_source(dev, ainput);
328 * inits the device and starts isoc transfer
330 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
332 int minor = iminor(inode);
334 struct em28xx *h,*dev = NULL;
335 struct list_head *list;
337 list_for_each(list,&em28xx_devlist) {
338 h = list_entry(list, struct em28xx, devlist);
339 if (h->vdev->minor == minor) {
341 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343 if (h->vbi_dev->minor == minor) {
345 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
351 filp->private_data=dev;
353 em28xx_videodbg("open minor=%d type=%s users=%d\n",
354 minor,v4l2_type_names[dev->type],dev->users);
356 if (!down_read_trylock(&em28xx_disconnect))
360 em28xx_warn("this driver can be opened only once\n");
361 up_read(&em28xx_disconnect);
365 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
366 spin_lock_init(&dev->queue_lock);
367 init_waitqueue_head(&dev->wait_frame);
368 init_waitqueue_head(&dev->wait_stream);
372 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
373 em28xx_set_alternate(dev);
375 dev->width = norm_maxw(dev);
376 dev->height = norm_maxh(dev);
377 dev->frame_size = dev->width * dev->height * 2;
378 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
379 dev->bytesperline = dev->width * 2;
383 em28xx_capture_start(dev, 1);
384 em28xx_resolution_set(dev);
386 /* start the transfer */
387 errCode = em28xx_init_isoc(dev);
395 filp->private_data = dev;
397 dev->stream = STREAM_OFF;
401 em28xx_empty_framequeues(dev);
403 dev->state |= DEV_INITIALIZED;
407 up_read(&em28xx_disconnect);
412 * em28xx_realease_resources()
413 * unregisters the v4l2,i2c and usb devices
414 * called when the device gets disconected or at module unload
416 static void em28xx_release_resources(struct em28xx *dev)
418 mutex_lock(&em28xx_sysfs_lock);
420 /*FIXME: I2C IR should be disconnected */
422 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
423 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
424 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
425 list_del(&dev->devlist);
426 video_unregister_device(dev->vdev);
427 video_unregister_device(dev->vbi_dev);
428 em28xx_i2c_unregister(dev);
429 usb_put_dev(dev->udev);
430 mutex_unlock(&em28xx_sysfs_lock);
433 /* Mark device as unused */
434 em28xx_devused&=~(1<<dev->devno);
438 * em28xx_v4l2_close()
439 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
441 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
444 struct em28xx *dev=filp->private_data;
446 em28xx_videodbg("users=%d\n", dev->users);
450 em28xx_uninit_isoc(dev);
452 em28xx_release_buffers(dev);
454 /* the device is already disconnect, free the remaining resources */
455 if (dev->state & DEV_DISCONNECTED) {
456 em28xx_release_resources(dev);
462 /* set alternate 0 */
464 em28xx_videodbg("setting alternate 0\n");
465 errCode = usb_set_interface(dev->udev, 0, 0);
467 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
472 wake_up_interruptible_nr(&dev->open, 1);
479 * will allocate buffers when called for the first time
482 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
485 struct em28xx_frame_t *f, *i;
486 unsigned long lock_flags;
488 struct em28xx *dev = filp->private_data;
490 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
491 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
493 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
494 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
495 em28xx_videodbg("not supported yet! ...\n");
496 if (copy_to_user(buf, "", 1)) {
497 up(&dev->fileop_lock);
502 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
503 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
504 em28xx_videodbg("not supported yet! ...\n");
505 if (copy_to_user(buf, "", 1)) {
506 up(&dev->fileop_lock);
512 if (down_interruptible(&dev->fileop_lock))
515 if (dev->state & DEV_DISCONNECTED) {
516 em28xx_videodbg("device not present\n");
517 up(&dev->fileop_lock);
521 if (dev->state & DEV_MISCONFIGURED) {
522 em28xx_videodbg("device misconfigured; close and open it again\n");
523 up(&dev->fileop_lock);
527 if (dev->io == IO_MMAP) {
528 em28xx_videodbg ("IO method is set to mmap; close and open"
529 " the device again to choose the read method\n");
530 up(&dev->fileop_lock);
534 if (dev->io == IO_NONE) {
535 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
536 em28xx_errdev("read failed, not enough memory\n");
537 up(&dev->fileop_lock);
541 dev->stream = STREAM_ON;
542 em28xx_queue_unusedframes(dev);
546 up(&dev->fileop_lock);
550 if (list_empty(&dev->outqueue)) {
551 if (filp->f_flags & O_NONBLOCK) {
552 up(&dev->fileop_lock);
555 ret = wait_event_interruptible
557 (!list_empty(&dev->outqueue)) ||
558 (dev->state & DEV_DISCONNECTED));
560 up(&dev->fileop_lock);
563 if (dev->state & DEV_DISCONNECTED) {
564 up(&dev->fileop_lock);
569 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
571 spin_lock_irqsave(&dev->queue_lock, lock_flags);
572 list_for_each_entry(i, &dev->outqueue, frame)
574 INIT_LIST_HEAD(&dev->outqueue);
575 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
577 em28xx_queue_unusedframes(dev);
579 if (count > f->buf.length)
580 count = f->buf.length;
582 if (copy_to_user(buf, f->bufmem, count)) {
583 up(&dev->fileop_lock);
588 up(&dev->fileop_lock);
595 * will allocate buffers when called for the first time
597 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
599 unsigned int mask = 0;
600 struct em28xx *dev = filp->private_data;
602 if (down_interruptible(&dev->fileop_lock))
605 if (dev->state & DEV_DISCONNECTED) {
606 em28xx_videodbg("device not present\n");
607 } else if (dev->state & DEV_MISCONFIGURED) {
608 em28xx_videodbg("device is misconfigured; close and open it again\n");
610 if (dev->io == IO_NONE) {
611 if (!em28xx_request_buffers
612 (dev, EM28XX_NUM_READ_FRAMES)) {
614 ("poll() failed, not enough memory\n");
617 dev->stream = STREAM_ON;
621 if (dev->io == IO_READ) {
622 em28xx_queue_unusedframes(dev);
623 poll_wait(filp, &dev->wait_frame, wait);
625 if (!list_empty(&dev->outqueue))
626 mask |= POLLIN | POLLRDNORM;
628 up(&dev->fileop_lock);
634 up(&dev->fileop_lock);
641 static void em28xx_vm_open(struct vm_area_struct *vma)
643 struct em28xx_frame_t *f = vma->vm_private_data;
650 static void em28xx_vm_close(struct vm_area_struct *vma)
652 /* NOTE: buffers are not freed here */
653 struct em28xx_frame_t *f = vma->vm_private_data;
657 static struct vm_operations_struct em28xx_vm_ops = {
658 .open = em28xx_vm_open,
659 .close = em28xx_vm_close,
665 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
667 unsigned long size = vma->vm_end - vma->vm_start,
668 start = vma->vm_start;
672 struct em28xx *dev = filp->private_data;
674 if (down_interruptible(&dev->fileop_lock))
677 if (dev->state & DEV_DISCONNECTED) {
678 em28xx_videodbg("mmap: device not present\n");
679 up(&dev->fileop_lock);
683 if (dev->state & DEV_MISCONFIGURED) {
684 em28xx_videodbg ("mmap: Device is misconfigured; close and "
686 up(&dev->fileop_lock);
690 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
691 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
692 up(&dev->fileop_lock);
696 for (i = 0; i < dev->num_frames; i++) {
697 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
700 if (i == dev->num_frames) {
701 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
702 up(&dev->fileop_lock);
706 /* VM_IO is eventually going to replace PageReserved altogether */
707 vma->vm_flags |= VM_IO;
708 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
710 pos = dev->frame[i].bufmem;
711 while (size > 0) { /* size is page-aligned */
712 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
713 em28xx_videodbg("mmap: vm_insert_page failed\n");
714 up(&dev->fileop_lock);
722 vma->vm_ops = &em28xx_vm_ops;
723 vma->vm_private_data = &dev->frame[i];
726 up(&dev->fileop_lock);
732 * return the current saturation, brightness or contrast, mute state
734 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
737 case V4L2_CID_AUDIO_MUTE:
738 ctrl->value = dev->mute;
740 case V4L2_CID_AUDIO_VOLUME:
741 ctrl->value = dev->volume;
748 /*FIXME: should be moved to saa711x */
749 static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
753 case V4L2_CID_BRIGHTNESS:
754 if ((tmp = em28xx_brightness_get(dev)) < 0)
756 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
758 case V4L2_CID_CONTRAST:
759 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
762 case V4L2_CID_SATURATION:
763 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
766 case V4L2_CID_RED_BALANCE:
767 if ((tmp = em28xx_v_balance_get(dev)) < 0)
769 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
771 case V4L2_CID_BLUE_BALANCE:
772 if ((tmp = em28xx_u_balance_get(dev)) < 0)
774 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
777 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
787 * mute or set new saturation, brightness or contrast
789 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
792 case V4L2_CID_AUDIO_MUTE:
793 if (ctrl->value != dev->mute) {
794 dev->mute = ctrl->value;
795 em28xx_audio_usb_mute(dev, ctrl->value);
796 return em28xx_audio_analog_set(dev);
799 case V4L2_CID_AUDIO_VOLUME:
800 dev->volume = ctrl->value;
801 return em28xx_audio_analog_set(dev);
807 /*FIXME: should be moved to saa711x */
808 static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
811 case V4L2_CID_BRIGHTNESS:
812 return em28xx_brightness_set(dev, ctrl->value);
813 case V4L2_CID_CONTRAST:
814 return em28xx_contrast_set(dev, ctrl->value);
815 case V4L2_CID_SATURATION:
816 return em28xx_saturation_set(dev, ctrl->value);
817 case V4L2_CID_RED_BALANCE:
818 return em28xx_v_balance_set(dev, ctrl->value);
819 case V4L2_CID_BLUE_BALANCE:
820 return em28xx_u_balance_set(dev, ctrl->value);
822 return em28xx_gamma_set(dev, ctrl->value);
829 * em28xx_stream_interrupt()
832 static int em28xx_stream_interrupt(struct em28xx *dev)
836 /* stop reading from the device */
838 dev->stream = STREAM_INTERRUPT;
839 ret = wait_event_timeout(dev->wait_stream,
840 (dev->stream == STREAM_OFF) ||
841 (dev->state & DEV_DISCONNECTED),
843 if (dev->state & DEV_DISCONNECTED)
846 dev->state |= DEV_MISCONFIGURED;
847 em28xx_videodbg("device is misconfigured; close and "
848 "open /dev/video%d again\n",
849 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
856 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
858 unsigned int hscale, vscale;
859 unsigned int maxh, maxw;
861 maxw = norm_maxw(dev);
862 maxh = norm_maxh(dev);
864 /* width must even because of the YUYV format */
865 /* height must be even because of interlacing */
878 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
880 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
882 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
884 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
886 /* set new image size */
888 dev->height = height;
889 dev->frame_size = dev->width * dev->height * 2;
890 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
891 dev->bytesperline = dev->width * 2;
892 dev->hscale = hscale;
893 dev->vscale = vscale;
895 em28xx_resolution_set(dev);
901 static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
903 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
904 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
905 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
906 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
907 "V4L2_BUF_TYPE_VBI_CAPTURE" :
908 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
909 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
912 if (format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
913 format->fmt.pix.width = dev->width;
914 format->fmt.pix.height = dev->height;
915 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
916 format->fmt.pix.bytesperline = dev->bytesperline;
917 format->fmt.pix.sizeimage = dev->frame_size;
918 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
919 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
921 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
931 * em28xx_v4l2_do_ioctl()
932 * This function is _not_ called directly, but from
933 * em28xx_v4l2_ioctl. Userspace
934 * copying is done already, arg is a kernel pointer.
936 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
937 struct em28xx *dev, unsigned int cmd, void *arg,
938 v4l2_kioctl driver_ioctl)
943 /* ---------- tv norms ---------- */
946 struct v4l2_standard *e = arg;
952 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
953 tvnorms[e->index].name);
961 v4l2_std_id *id = arg;
963 *id = dev->tvnorm->id;
968 v4l2_std_id *id = arg;
971 for (i = 0; i < TVNORMS; i++)
972 if (*id == tvnorms[i].id)
975 for (i = 0; i < TVNORMS; i++)
976 if (*id & tvnorms[i].id)
982 dev->tvnorm = &tvnorms[i];
984 em28xx_set_norm(dev, dev->width, dev->height);
987 dev->width=norm_maxw(dev);
988 dev->height=norm_maxh(dev);
989 dev->frame_size=dev->width*dev->height*2;
990 dev->field_size=dev->frame_size>>1;
991 dev->bytesperline=dev->width*2;
995 em28xx_resolution_set(dev);
998 em28xx_uninit_isoc(dev);
999 em28xx_set_alternate(dev);
1000 em28xx_capture_start(dev, 1);
1001 em28xx_resolution_set(dev);
1002 em28xx_init_isoc(dev);
1004 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
1006 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
1014 /* ------ input switching ---------- */
1015 case VIDIOC_ENUMINPUT:
1017 struct v4l2_input *i = arg;
1019 static const char *iname[] = {
1020 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1021 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1022 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1023 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1024 [EM28XX_VMUX_SVIDEO] = "S-Video",
1025 [EM28XX_VMUX_TELEVISION] = "Television",
1026 [EM28XX_VMUX_CABLE] = "Cable TV",
1027 [EM28XX_VMUX_DVB] = "DVB",
1028 [EM28XX_VMUX_DEBUG] = "for debug only",
1032 if (n >= MAX_EM28XX_INPUT)
1034 if (0 == INPUT(n)->type)
1036 memset(i, 0, sizeof(*i));
1038 i->type = V4L2_INPUT_TYPE_CAMERA;
1039 strcpy(i->name, iname[INPUT(n)->type]);
1040 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1041 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1042 i->type = V4L2_INPUT_TYPE_TUNER;
1043 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1044 i->std |= tvnorms[n].id;
1048 case VIDIOC_G_INPUT:
1051 *i = dev->ctl_input;
1056 case VIDIOC_S_INPUT:
1060 if (*index >= MAX_EM28XX_INPUT)
1062 if (0 == INPUT(*index)->type)
1066 video_mux(dev, *index);
1072 case VIDIOC_G_AUDIO:
1074 struct v4l2_audio *a = arg;
1075 unsigned int index = a->index;
1079 memset(a, 0, sizeof(*a));
1080 index = dev->ctl_ainput;
1083 strcpy(a->name, "Television");
1085 strcpy(a->name, "Line In");
1087 a->capability = V4L2_AUDCAP_STEREO;
1092 case VIDIOC_S_AUDIO:
1094 struct v4l2_audio *a = arg;
1095 if (a->index != dev->ctl_ainput)
1101 /* --- controls ---------------------------------------------- */
1102 case VIDIOC_QUERYCTRL:
1104 struct v4l2_queryctrl *qc = arg;
1107 memset(qc,0,sizeof(*qc));
1110 if (!dev->has_msp34xx) {
1111 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1112 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1113 memcpy(qc, &(em28xx_qctrl[i]),
1119 if (dev->decoder == EM28XX_TVP5150) {
1120 em28xx_i2c_call_clients(dev,cmd,qc);
1126 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1127 if (qc->id && qc->id == saa711x_qctrl[i].id) {
1128 memcpy(qc, &(saa711x_qctrl[i]),
1139 struct v4l2_control *ctrl = arg;
1142 if (!dev->has_msp34xx)
1143 retval=em28xx_get_ctrl(dev, ctrl);
1144 if (retval==-EINVAL) {
1145 if (dev->decoder == EM28XX_TVP5150) {
1146 em28xx_i2c_call_clients(dev,cmd,arg);
1150 return saa711x_get_ctrl(dev, ctrl);
1151 } else return retval;
1156 struct v4l2_control *ctrl = arg;
1159 if (!dev->has_msp34xx){
1160 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1161 if (ctrl->id == em28xx_qctrl[i].id) {
1163 em28xx_qctrl[i].minimum
1165 em28xx_qctrl[i].maximum)
1167 return em28xx_set_ctrl(dev, ctrl);
1172 if (dev->decoder == EM28XX_TVP5150) {
1173 em28xx_i2c_call_clients(dev,cmd,arg);
1175 } else if (!dev->has_msp34xx) {
1176 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1177 if (ctrl->id == em28xx_qctrl[i].id) {
1179 em28xx_qctrl[i].minimum
1181 em28xx_qctrl[i].maximum)
1183 return em28xx_set_ctrl(dev, ctrl);
1186 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1187 if (ctrl->id == saa711x_qctrl[i].id) {
1189 saa711x_qctrl[i].minimum
1191 saa711x_qctrl[i].maximum)
1193 return saa711x_set_ctrl(dev, ctrl);
1201 /* --- tuner ioctls ------------------------------------------ */
1202 case VIDIOC_G_TUNER:
1204 struct v4l2_tuner *t = arg;
1210 memset(t, 0, sizeof(*t));
1211 strcpy(t->name, "Tuner");
1212 t->type = V4L2_TUNER_ANALOG_TV;
1213 t->capability = V4L2_TUNER_CAP_NORM;
1214 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1215 /* t->signal = 0xffff;*/
1216 /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1217 /* No way to get signal strength? */
1219 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1223 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1225 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1229 case VIDIOC_S_TUNER:
1231 struct v4l2_tuner *t = arg;
1236 memset(t, 0, sizeof(*t));
1237 strcpy(t->name, "Tuner");
1238 t->type = V4L2_TUNER_ANALOG_TV;
1239 t->capability = V4L2_TUNER_CAP_NORM;
1240 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1241 /* t->signal = 0xffff; */
1242 /* No way to get signal strength? */
1244 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1248 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1250 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1254 case VIDIOC_G_FREQUENCY:
1256 struct v4l2_frequency *f = arg;
1258 memset(f, 0, sizeof(*f));
1259 f->type = V4L2_TUNER_ANALOG_TV;
1260 f->frequency = dev->ctl_freq;
1264 case VIDIOC_S_FREQUENCY:
1266 struct v4l2_frequency *f = arg;
1271 if (V4L2_TUNER_ANALOG_TV != f->type)
1275 dev->ctl_freq = f->frequency;
1276 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1281 case VIDIOC_CROPCAP:
1283 struct v4l2_cropcap *cc = arg;
1285 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1287 cc->bounds.left = 0;
1289 cc->bounds.width = dev->width;
1290 cc->bounds.height = dev->height;
1291 cc->defrect = cc->bounds;
1292 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1293 cc->pixelaspect.denominator = 59;
1296 case VIDIOC_STREAMON:
1300 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1301 || dev->io != IO_MMAP)
1304 if (list_empty(&dev->inqueue))
1307 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1309 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1313 case VIDIOC_STREAMOFF:
1318 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1319 || dev->io != IO_MMAP)
1322 if (dev->stream == STREAM_ON) {
1323 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1324 if ((ret = em28xx_stream_interrupt(dev)))
1327 em28xx_empty_framequeues(dev);
1332 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1339 * em28xx_v4l2_do_ioctl()
1340 * This function is _not_ called directly, but from
1341 * em28xx_v4l2_ioctl. Userspace
1342 * copying is done already, arg is a kernel pointer.
1344 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1345 unsigned int cmd, void *arg)
1347 struct em28xx *dev = filp->private_data;
1352 if (video_debug > 1)
1353 v4l_print_ioctl(dev->name,cmd);
1357 /* --- capabilities ------------------------------------------ */
1358 case VIDIOC_QUERYCAP:
1360 struct v4l2_capability *cap = arg;
1362 memset(cap, 0, sizeof(*cap));
1363 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1364 strlcpy(cap->card, em28xx_boards[dev->model].name,
1366 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1367 sizeof(cap->bus_info));
1368 cap->version = EM28XX_VERSION_CODE;
1370 V4L2_CAP_SLICED_VBI_CAPTURE |
1371 V4L2_CAP_VIDEO_CAPTURE |
1373 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1375 cap->capabilities |= V4L2_CAP_TUNER;
1379 /* --- capture ioctls ---------------------------------------- */
1380 case VIDIOC_ENUM_FMT:
1382 struct v4l2_fmtdesc *fmtd = arg;
1384 if (fmtd->index != 0)
1386 memset(fmtd, 0, sizeof(*fmtd));
1387 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1388 strcpy(fmtd->description, "Packed YUY2");
1389 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1390 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1395 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
1397 case VIDIOC_TRY_FMT:
1400 struct v4l2_format *format = arg;
1403 int width = format->fmt.pix.width;
1404 int height = format->fmt.pix.height;
1405 unsigned int hscale, vscale;
1406 unsigned int maxh, maxw;
1408 maxw = norm_maxw(dev);
1409 maxh = norm_maxh(dev);
1411 /* int both_fields; */
1413 em28xx_videodbg("%s: type=%s\n",
1415 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1418 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1419 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1420 V4L2_BUF_TYPE_VBI_CAPTURE ?
1421 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1424 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1427 em28xx_videodbg("%s: requested %dx%d\n",
1429 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1430 "VIDIOC_S_FMT", format->fmt.pix.width,
1431 format->fmt.pix.height);
1433 /* FIXME: Move some code away from here */
1434 /* width must even because of the YUYV format */
1435 /* height must be even because of interlacing */
1449 /* the em2800 can only scale down to 50% */
1450 if(height % (maxh / 2))
1452 if(width % (maxw / 2))
1454 /* according to empiatech support */
1455 /* the MaxPacketSize is to small to support */
1456 /* framesizes larger than 640x480 @ 30 fps */
1457 /* or 640x576 @ 25 fps. As this would cut */
1458 /* of a part of the image we prefer */
1459 /* 360x576 or 360x480 for now */
1460 if(width == maxw && height == maxh)
1465 (((unsigned long)maxw) << 12) / width - 4096L) >=
1469 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1472 (((unsigned long)maxh) << 12) / height - 4096L) >=
1476 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1478 format->fmt.pix.width = width;
1479 format->fmt.pix.height = height;
1480 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1481 format->fmt.pix.bytesperline = width * 2;
1482 format->fmt.pix.sizeimage = width * 2 * height;
1483 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1484 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1486 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
1488 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1489 "VIDIOC_S_FMT", format->fmt.pix.width,
1490 format->fmt.pix.height, hscale, vscale);
1492 if (cmd == VIDIOC_TRY_FMT)
1495 for (i = 0; i < dev->num_frames; i++)
1496 if (dev->frame[i].vma_use_count) {
1497 em28xx_videodbg("VIDIOC_S_FMT failed. "
1498 "Unmap the buffers first.\n");
1502 /* stop io in case it is already in progress */
1503 if (dev->stream == STREAM_ON) {
1504 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
1505 if ((ret = em28xx_stream_interrupt(dev)))
1509 em28xx_release_buffers(dev);
1512 /* set new image size */
1514 dev->height = height;
1515 dev->frame_size = dev->width * dev->height * 2;
1516 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1517 dev->bytesperline = dev->width * 2;
1518 dev->hscale = hscale;
1519 dev->vscale = vscale;
1520 /* dev->both_fileds = both_fileds; */
1521 em28xx_uninit_isoc(dev);
1522 em28xx_set_alternate(dev);
1523 em28xx_capture_start(dev, 1);
1524 em28xx_resolution_set(dev);
1525 em28xx_init_isoc(dev);
1530 /* --- streaming capture ------------------------------------- */
1531 case VIDIOC_REQBUFS:
1533 struct v4l2_requestbuffers *rb = arg;
1537 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1538 rb->memory != V4L2_MEMORY_MMAP)
1541 if (dev->io == IO_READ) {
1542 em28xx_videodbg ("method is set to read;"
1543 " close and open the device again to"
1544 " choose the mmap I/O method\n");
1548 for (i = 0; i < dev->num_frames; i++)
1549 if (dev->frame[i].vma_use_count) {
1550 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1554 if (dev->stream == STREAM_ON) {
1555 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1556 if ((ret = em28xx_stream_interrupt(dev)))
1560 em28xx_empty_framequeues(dev);
1562 em28xx_release_buffers(dev);
1565 em28xx_request_buffers(dev, rb->count);
1567 dev->frame_current = NULL;
1569 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1571 dev->io = rb->count ? IO_MMAP : IO_NONE;
1575 case VIDIOC_QUERYBUF:
1577 struct v4l2_buffer *b = arg;
1579 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1580 b->index >= dev->num_frames || dev->io != IO_MMAP)
1583 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1585 if (dev->frame[b->index].vma_use_count) {
1586 b->flags |= V4L2_BUF_FLAG_MAPPED;
1588 if (dev->frame[b->index].state == F_DONE)
1589 b->flags |= V4L2_BUF_FLAG_DONE;
1590 else if (dev->frame[b->index].state != F_UNUSED)
1591 b->flags |= V4L2_BUF_FLAG_QUEUED;
1596 struct v4l2_buffer *b = arg;
1597 unsigned long lock_flags;
1599 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1600 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1604 if (dev->frame[b->index].state != F_UNUSED) {
1607 dev->frame[b->index].state = F_QUEUED;
1609 /* add frame to fifo */
1610 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1611 list_add_tail(&dev->frame[b->index].frame,
1613 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1619 struct v4l2_buffer *b = arg;
1620 struct em28xx_frame_t *f;
1621 unsigned long lock_flags;
1624 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1625 || dev->io != IO_MMAP)
1628 if (list_empty(&dev->outqueue)) {
1629 if (dev->stream == STREAM_OFF)
1631 if (filp->f_flags & O_NONBLOCK)
1633 ret = wait_event_interruptible
1635 (!list_empty(&dev->outqueue)) ||
1636 (dev->state & DEV_DISCONNECTED));
1639 if (dev->state & DEV_DISCONNECTED)
1643 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1644 f = list_entry(dev->outqueue.next,
1645 struct em28xx_frame_t, frame);
1646 list_del(dev->outqueue.next);
1647 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1649 f->state = F_UNUSED;
1650 memcpy(b, &f->buf, sizeof(*b));
1652 if (f->vma_use_count)
1653 b->flags |= V4L2_BUF_FLAG_MAPPED;
1658 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1659 em28xx_video_do_ioctl);
1665 * em28xx_v4l2_ioctl()
1666 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1668 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1669 unsigned int cmd, unsigned long arg)
1672 struct em28xx *dev = filp->private_data;
1674 if (down_interruptible(&dev->fileop_lock))
1675 return -ERESTARTSYS;
1677 if (dev->state & DEV_DISCONNECTED) {
1678 em28xx_errdev("v4l2 ioctl: device not present\n");
1679 up(&dev->fileop_lock);
1683 if (dev->state & DEV_MISCONFIGURED) {
1685 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1686 up(&dev->fileop_lock);
1690 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1692 up(&dev->fileop_lock);
1697 static struct file_operations em28xx_v4l_fops = {
1698 .owner = THIS_MODULE,
1699 .open = em28xx_v4l2_open,
1700 .release = em28xx_v4l2_close,
1701 .ioctl = em28xx_v4l2_ioctl,
1702 .read = em28xx_v4l2_read,
1703 .poll = em28xx_v4l2_poll,
1704 .mmap = em28xx_v4l2_mmap,
1705 .llseek = no_llseek,
1706 .compat_ioctl = v4l_compat_ioctl32,
1710 /******************************** usb interface *****************************************/
1714 * allocates and inits the device structs, registers i2c bus and v4l device
1716 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1717 int minor, int model)
1719 struct em28xx *dev = *devhandle;
1720 int retval = -ENOMEM;
1722 unsigned int maxh, maxw;
1726 init_MUTEX(&dev->lock);
1727 init_waitqueue_head(&dev->open);
1729 dev->em28xx_write_regs = em28xx_write_regs;
1730 dev->em28xx_read_reg = em28xx_read_reg;
1731 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1732 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1733 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1734 dev->is_em2800 = em28xx_boards[model].is_em2800;
1735 dev->has_tuner = em28xx_boards[model].has_tuner;
1736 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1737 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1738 dev->decoder = em28xx_boards[model].decoder;
1741 dev->tuner_type = tuner;
1743 dev->tuner_type = em28xx_boards[model].tuner_type;
1745 dev->video_inputs = em28xx_boards[model].vchannels;
1747 for (i = 0; i < TVNORMS; i++)
1748 if (em28xx_boards[model].norm == tvnorms[i].mode)
1753 dev->tvnorm = &tvnorms[i]; /* set default norm */
1755 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1757 maxw = norm_maxw(dev);
1758 maxh = norm_maxh(dev);
1760 /* set default image size */
1763 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1764 dev->field_size = dev->width * dev->height;
1766 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1767 dev->bytesperline = dev->width * 2;
1772 /* setup video picture settings for saa7113h */
1773 memset(&dev->vpic, 0, sizeof(dev->vpic));
1774 dev->vpic.colour = 128 << 8;
1775 dev->vpic.hue = 128 << 8;
1776 dev->vpic.brightness = 128 << 8;
1777 dev->vpic.contrast = 192 << 8;
1778 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1779 dev->vpic.depth = 16;
1780 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1782 #ifdef CONFIG_MODULES
1783 /* request some modules */
1784 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1785 request_module("saa711x");
1786 if (dev->decoder == EM28XX_TVP5150)
1787 request_module("tvp5150");
1789 request_module("tuner");
1790 if (dev->tda9887_conf)
1791 request_module("tda9887");
1793 errCode = em28xx_config(dev);
1795 em28xx_errdev("error configuring device\n");
1797 em28xx_devused&=~(1<<dev->devno);
1802 /* register i2c bus */
1803 em28xx_i2c_register(dev);
1805 /* Do board specific init and eeprom reading */
1806 em28xx_card_setup(dev);
1808 /* configure the device */
1809 em28xx_config_i2c(dev);
1813 errCode = em28xx_config(dev);
1815 #ifdef CONFIG_MODULES
1816 if (dev->has_msp34xx)
1817 request_module("msp3400");
1819 /* allocate and fill v4l2 device struct */
1820 dev->vdev = video_device_alloc();
1821 if (NULL == dev->vdev) {
1822 em28xx_errdev("cannot allocate video_device.\n");
1824 em28xx_devused&=~(1<<dev->devno);
1828 dev->vbi_dev = video_device_alloc();
1829 if (NULL == dev->vbi_dev) {
1830 em28xx_errdev("cannot allocate video_device.\n");
1833 em28xx_devused&=~(1<<dev->devno);
1837 /* Fills VBI device info */
1838 dev->vbi_dev->type = VFL_TYPE_VBI;
1839 dev->vbi_dev->hardware = 0;
1840 dev->vbi_dev->fops = &em28xx_v4l_fops;
1841 dev->vbi_dev->minor = -1;
1842 dev->vbi_dev->dev = &dev->udev->dev;
1843 dev->vbi_dev->release = video_device_release;
1844 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1845 "em28xx",dev->devno,"vbi");
1847 /* Fills CAPTURE device info */
1848 dev->vdev->type = VID_TYPE_CAPTURE;
1850 dev->vdev->type |= VID_TYPE_TUNER;
1851 dev->vdev->hardware = 0;
1852 dev->vdev->fops = &em28xx_v4l_fops;
1853 dev->vdev->minor = -1;
1854 dev->vdev->dev = &dev->udev->dev;
1855 dev->vdev->release = video_device_release;
1856 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1857 "em28xx",dev->devno,"video");
1859 list_add_tail(&dev->devlist,&em28xx_devlist);
1861 /* register v4l2 device */
1863 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1864 video_nr[dev->devno]))) {
1865 em28xx_errdev("unable to register video device (error=%i).\n",
1868 list_del(&dev->devlist);
1869 video_device_release(dev->vdev);
1871 em28xx_devused&=~(1<<dev->devno);
1875 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1876 vbi_nr[dev->devno]) < 0) {
1877 printk("unable to register vbi device\n");
1879 list_del(&dev->devlist);
1880 video_device_release(dev->vbi_dev);
1881 video_device_release(dev->vdev);
1883 em28xx_devused&=~(1<<dev->devno);
1886 printk("registered VBI\n");
1889 if (dev->has_msp34xx) {
1890 /* Send a reset to other chips via gpio */
1891 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1893 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1901 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1902 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1903 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1909 * em28xx_usb_probe()
1910 * checks for supported devices
1912 static int em28xx_usb_probe(struct usb_interface *interface,
1913 const struct usb_device_id *id)
1915 const struct usb_endpoint_descriptor *endpoint;
1916 struct usb_device *udev;
1917 struct usb_interface *uif;
1918 struct em28xx *dev = NULL;
1919 int retval = -ENODEV;
1920 int model,i,nr,ifnum;
1922 udev = usb_get_dev(interface_to_usbdev(interface));
1923 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1925 /* Check to see next free device and mark as used */
1926 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1927 em28xx_devused|=1<<nr;
1929 /* Don't register audio interfaces */
1930 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1931 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1932 udev->descriptor.idVendor,udev->descriptor.idProduct,
1934 interface->altsetting[0].desc.bInterfaceClass);
1936 em28xx_devused&=~(1<<nr);
1940 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1941 udev->descriptor.idVendor,udev->descriptor.idProduct,
1943 interface->altsetting[0].desc.bInterfaceClass);
1945 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1947 /* check if the the device has the iso in endpoint at the correct place */
1948 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1949 USB_ENDPOINT_XFER_ISOC) {
1950 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1951 em28xx_devused&=~(1<<nr);
1954 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1955 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1956 em28xx_devused&=~(1<<nr);
1960 model=id->driver_info;
1962 if (nr > EM28XX_MAXBOARDS) {
1963 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1964 em28xx_devused&=~(1<<nr);
1968 /* allocate memory for our device state and initialize it */
1969 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1971 em28xx_err(DRIVER_NAME ": out of memory!\n");
1972 em28xx_devused&=~(1<<nr);
1976 snprintf(dev->name, 29, "em28xx #%d", nr);
1979 /* compute alternate max packet sizes */
1980 uif = udev->actconfig->interface[0];
1982 dev->num_alt=uif->num_altsetting;
1983 em28xx_info("Alternate settings: %i\n",dev->num_alt);
1984 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1985 dev->alt_max_pkt_size = kmalloc(32*
1986 dev->num_alt,GFP_KERNEL);
1987 if (dev->alt_max_pkt_size == NULL) {
1988 em28xx_errdev("out of memory!\n");
1989 em28xx_devused&=~(1<<nr);
1993 for (i = 0; i < dev->num_alt ; i++) {
1994 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1996 dev->alt_max_pkt_size[i] =
1997 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1998 em28xx_info("Alternate setting %i, max size= %i\n",i,
1999 dev->alt_max_pkt_size[i]);
2002 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2005 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
2006 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
2007 "%s: be autodetected. Please pass card=<n> insmod option to\n"
2008 "%s: workaround that. Redirect complaints to the vendor of\n"
2009 "%s: the TV card. Generic type will be used."
2010 "%s: Best regards,\n"
2012 dev->name,dev->name,dev->name,dev->name,dev->name);
2013 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
2015 for (i = 0; i < em28xx_bcount; i++) {
2016 em28xx_errdev(" card=%d -> %s\n", i,
2017 em28xx_boards[i].name);
2021 /* allocate device struct */
2022 retval = em28xx_init_dev(&dev, udev, nr, model);
2026 em28xx_info("Found %s\n", em28xx_boards[model].name);
2028 /* save our data pointer in this interface device */
2029 usb_set_intfdata(interface, dev);
2034 * em28xx_usb_disconnect()
2035 * called when the device gets diconencted
2036 * video device will be unregistered on v4l2_close in case it is still open
2038 static void em28xx_usb_disconnect(struct usb_interface *interface)
2040 struct em28xx *dev = usb_get_intfdata(interface);
2041 usb_set_intfdata(interface, NULL);
2046 down_write(&em28xx_disconnect);
2050 em28xx_info("disconnecting %s\n", dev->vdev->name);
2052 wake_up_interruptible_all(&dev->open);
2056 ("device /dev/video%d is open! Deregistration and memory "
2057 "deallocation are deferred on close.\n",
2058 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2060 dev->state |= DEV_MISCONFIGURED;
2061 em28xx_uninit_isoc(dev);
2062 dev->state |= DEV_DISCONNECTED;
2063 wake_up_interruptible(&dev->wait_frame);
2064 wake_up_interruptible(&dev->wait_stream);
2066 dev->state |= DEV_DISCONNECTED;
2067 em28xx_release_resources(dev);
2073 kfree(dev->alt_max_pkt_size);
2077 up_write(&em28xx_disconnect);
2080 static struct usb_driver em28xx_usb_driver = {
2082 .probe = em28xx_usb_probe,
2083 .disconnect = em28xx_usb_disconnect,
2084 .id_table = em28xx_id_table,
2087 static int __init em28xx_module_init(void)
2091 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2092 (EM28XX_VERSION_CODE >> 16) & 0xff,
2093 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2095 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2096 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2099 /* register this driver with the USB subsystem */
2100 result = usb_register(&em28xx_usb_driver);
2102 em28xx_err(DRIVER_NAME
2103 " usb_register failed. Error number %d.\n", result);
2108 static void __exit em28xx_module_exit(void)
2110 /* deregister this driver with the USB subsystem */
2111 usb_deregister(&em28xx_usb_driver);
2114 module_init(em28xx_module_init);
2115 module_exit(em28xx_module_exit);