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 /* start the transfer */
371 errCode = em28xx_init_isoc(dev);
376 filp->private_data = dev;
378 dev->stream = STREAM_OFF;
382 em28xx_empty_framequeues(dev);
384 dev->state |= DEV_INITIALIZED;
390 up_read(&em28xx_disconnect);
395 * em28xx_realease_resources()
396 * unregisters the v4l2,i2c and usb devices
397 * called when the device gets disconected or at module unload
399 static void em28xx_release_resources(struct em28xx *dev)
401 mutex_lock(&em28xx_sysfs_lock);
403 em28xx_info("V4L2 device /dev/video%d deregistered\n",
405 list_del(&dev->devlist);
406 video_unregister_device(dev->vdev);
407 /* video_unregister_device(dev->vbi_dev); */
408 em28xx_i2c_unregister(dev);
409 usb_put_dev(dev->udev);
410 mutex_unlock(&em28xx_sysfs_lock);
414 * em28xx_v4l2_close()
415 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
417 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
420 struct em28xx *dev=filp->private_data;
422 em28xx_videodbg("users=%d\n", dev->users);
426 em28xx_uninit_isoc(dev);
428 em28xx_release_buffers(dev);
430 /* the device is already disconnect, free the remaining resources */
431 if (dev->state & DEV_DISCONNECTED) {
432 em28xx_release_resources(dev);
438 /* set alternate 0 */
440 em28xx_videodbg("setting alternate 0\n");
441 errCode = usb_set_interface(dev->udev, 0, 0);
443 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
448 wake_up_interruptible_nr(&dev->open, 1);
455 * will allocate buffers when called for the first time
458 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
461 struct em28xx_frame_t *f, *i;
462 unsigned long lock_flags;
464 struct em28xx *dev = filp->private_data;
466 if (down_interruptible(&dev->fileop_lock))
469 if (dev->state & DEV_DISCONNECTED) {
470 em28xx_videodbg("device not present\n");
471 up(&dev->fileop_lock);
475 if (dev->state & DEV_MISCONFIGURED) {
476 em28xx_videodbg("device misconfigured; close and open it again\n");
477 up(&dev->fileop_lock);
481 if (dev->io == IO_MMAP) {
482 em28xx_videodbg ("IO method is set to mmap; close and open"
483 " the device again to choose the read method\n");
484 up(&dev->fileop_lock);
488 if (dev->io == IO_NONE) {
489 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
490 em28xx_errdev("read failed, not enough memory\n");
491 up(&dev->fileop_lock);
495 dev->stream = STREAM_ON;
496 em28xx_queue_unusedframes(dev);
500 up(&dev->fileop_lock);
504 if (list_empty(&dev->outqueue)) {
505 if (filp->f_flags & O_NONBLOCK) {
506 up(&dev->fileop_lock);
509 ret = wait_event_interruptible
511 (!list_empty(&dev->outqueue)) ||
512 (dev->state & DEV_DISCONNECTED));
514 up(&dev->fileop_lock);
517 if (dev->state & DEV_DISCONNECTED) {
518 up(&dev->fileop_lock);
523 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
525 spin_lock_irqsave(&dev->queue_lock, lock_flags);
526 list_for_each_entry(i, &dev->outqueue, frame)
528 INIT_LIST_HEAD(&dev->outqueue);
529 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
531 em28xx_queue_unusedframes(dev);
533 if (count > f->buf.length)
534 count = f->buf.length;
536 if (copy_to_user(buf, f->bufmem, count)) {
537 up(&dev->fileop_lock);
542 up(&dev->fileop_lock);
549 * will allocate buffers when called for the first time
551 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
553 unsigned int mask = 0;
554 struct em28xx *dev = filp->private_data;
556 if (down_interruptible(&dev->fileop_lock))
559 if (dev->state & DEV_DISCONNECTED) {
560 em28xx_videodbg("device not present\n");
561 } else if (dev->state & DEV_MISCONFIGURED) {
562 em28xx_videodbg("device is misconfigured; close and open it again\n");
564 if (dev->io == IO_NONE) {
565 if (!em28xx_request_buffers
566 (dev, EM28XX_NUM_READ_FRAMES)) {
568 ("poll() failed, not enough memory\n");
571 dev->stream = STREAM_ON;
575 if (dev->io == IO_READ) {
576 em28xx_queue_unusedframes(dev);
577 poll_wait(filp, &dev->wait_frame, wait);
579 if (!list_empty(&dev->outqueue))
580 mask |= POLLIN | POLLRDNORM;
582 up(&dev->fileop_lock);
588 up(&dev->fileop_lock);
595 static void em28xx_vm_open(struct vm_area_struct *vma)
597 struct em28xx_frame_t *f = vma->vm_private_data;
604 static void em28xx_vm_close(struct vm_area_struct *vma)
606 /* NOTE: buffers are not freed here */
607 struct em28xx_frame_t *f = vma->vm_private_data;
611 static struct vm_operations_struct em28xx_vm_ops = {
612 .open = em28xx_vm_open,
613 .close = em28xx_vm_close,
619 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
621 unsigned long size = vma->vm_end - vma->vm_start,
622 start = vma->vm_start;
626 struct em28xx *dev = filp->private_data;
628 if (down_interruptible(&dev->fileop_lock))
631 if (dev->state & DEV_DISCONNECTED) {
632 em28xx_videodbg("mmap: device not present\n");
633 up(&dev->fileop_lock);
637 if (dev->state & DEV_MISCONFIGURED) {
638 em28xx_videodbg ("mmap: Device is misconfigured; close and "
640 up(&dev->fileop_lock);
644 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
645 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
646 up(&dev->fileop_lock);
650 for (i = 0; i < dev->num_frames; i++) {
651 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
654 if (i == dev->num_frames) {
655 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
656 up(&dev->fileop_lock);
660 /* VM_IO is eventually going to replace PageReserved altogether */
661 vma->vm_flags |= VM_IO;
662 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
664 pos = dev->frame[i].bufmem;
665 while (size > 0) { /* size is page-aligned */
666 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
667 em28xx_videodbg("mmap: vm_insert_page failed\n");
668 up(&dev->fileop_lock);
676 vma->vm_ops = &em28xx_vm_ops;
677 vma->vm_private_data = &dev->frame[i];
680 up(&dev->fileop_lock);
686 * return the current saturation, brightness or contrast, mute state
688 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
691 case V4L2_CID_AUDIO_MUTE:
692 ctrl->value = dev->mute;
694 case V4L2_CID_AUDIO_VOLUME:
695 ctrl->value = dev->volume;
702 /*FIXME: should be moved to saa711x */
703 static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
707 case V4L2_CID_BRIGHTNESS:
708 if ((tmp = em28xx_brightness_get(dev)) < 0)
710 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
712 case V4L2_CID_CONTRAST:
713 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
716 case V4L2_CID_SATURATION:
717 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
720 case V4L2_CID_RED_BALANCE:
721 if ((tmp = em28xx_v_balance_get(dev)) < 0)
723 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
725 case V4L2_CID_BLUE_BALANCE:
726 if ((tmp = em28xx_u_balance_get(dev)) < 0)
728 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
731 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
741 * mute or set new saturation, brightness or contrast
743 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
746 case V4L2_CID_AUDIO_MUTE:
747 if (ctrl->value != dev->mute) {
748 dev->mute = ctrl->value;
749 em28xx_audio_usb_mute(dev, ctrl->value);
750 return em28xx_audio_analog_set(dev);
753 case V4L2_CID_AUDIO_VOLUME:
754 dev->volume = ctrl->value;
755 return em28xx_audio_analog_set(dev);
761 /*FIXME: should be moved to saa711x */
762 static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
765 case V4L2_CID_BRIGHTNESS:
766 return em28xx_brightness_set(dev, ctrl->value);
767 case V4L2_CID_CONTRAST:
768 return em28xx_contrast_set(dev, ctrl->value);
769 case V4L2_CID_SATURATION:
770 return em28xx_saturation_set(dev, ctrl->value);
771 case V4L2_CID_RED_BALANCE:
772 return em28xx_v_balance_set(dev, ctrl->value);
773 case V4L2_CID_BLUE_BALANCE:
774 return em28xx_u_balance_set(dev, ctrl->value);
776 return em28xx_gamma_set(dev, ctrl->value);
783 * em28xx_stream_interrupt()
786 static int em28xx_stream_interrupt(struct em28xx *dev)
790 /* stop reading from the device */
792 dev->stream = STREAM_INTERRUPT;
793 ret = wait_event_timeout(dev->wait_stream,
794 (dev->stream == STREAM_OFF) ||
795 (dev->state & DEV_DISCONNECTED),
797 if (dev->state & DEV_DISCONNECTED)
800 dev->state |= DEV_MISCONFIGURED;
801 em28xx_videodbg("device is misconfigured; close and "
802 "open /dev/video%d again\n", dev->vdev->minor);
809 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
811 unsigned int hscale, vscale;
812 unsigned int maxh, maxw;
814 maxw = norm_maxw(dev);
815 maxh = norm_maxh(dev);
817 /* width must even because of the YUYV format */
818 /* height must be even because of interlacing */
831 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
833 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
835 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
837 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
839 /* set new image size */
841 dev->height = height;
842 dev->frame_size = dev->width * dev->height * 2;
843 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
844 dev->bytesperline = dev->width * 2;
845 dev->hscale = hscale;
846 dev->vscale = vscale;
848 em28xx_resolution_set(dev);
854 * em28xx_v4l2_do_ioctl()
855 * This function is _not_ called directly, but from
856 * em28xx_v4l2_ioctl. Userspace
857 * copying is done already, arg is a kernel pointer.
859 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
860 struct em28xx *dev, unsigned int cmd, void *arg,
861 v4l2_kioctl driver_ioctl)
866 /* ---------- tv norms ---------- */
869 struct v4l2_standard *e = arg;
875 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
876 tvnorms[e->index].name);
884 v4l2_std_id *id = arg;
886 *id = dev->tvnorm->id;
891 v4l2_std_id *id = arg;
894 for (i = 0; i < TVNORMS; i++)
895 if (*id == tvnorms[i].id)
898 for (i = 0; i < TVNORMS; i++)
899 if (*id & tvnorms[i].id)
905 dev->tvnorm = &tvnorms[i];
907 em28xx_set_norm(dev, dev->width, dev->height);
910 dev->width=norm_maxw(dev);
911 dev->height=norm_maxh(dev);
912 dev->frame_size=dev->width*dev->height*2;
913 dev->field_size=dev->frame_size>>1;
914 dev->bytesperline=dev->width*2;
918 em28xx_resolution_set(dev);
921 em28xx_uninit_isoc(dev);
922 em28xx_set_alternate(dev);
923 em28xx_capture_start(dev, 1);
924 em28xx_resolution_set(dev);
925 em28xx_init_isoc(dev);
927 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
929 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
937 /* ------ input switching ---------- */
938 case VIDIOC_ENUMINPUT:
940 struct v4l2_input *i = arg;
942 static const char *iname[] = {
943 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
944 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
945 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
946 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
947 [EM28XX_VMUX_SVIDEO] = "S-Video",
948 [EM28XX_VMUX_TELEVISION] = "Television",
949 [EM28XX_VMUX_CABLE] = "Cable TV",
950 [EM28XX_VMUX_DVB] = "DVB",
951 [EM28XX_VMUX_DEBUG] = "for debug only",
955 if (n >= MAX_EM28XX_INPUT)
957 if (0 == INPUT(n)->type)
959 memset(i, 0, sizeof(*i));
961 i->type = V4L2_INPUT_TYPE_CAMERA;
962 strcpy(i->name, iname[INPUT(n)->type]);
963 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
964 (EM28XX_VMUX_CABLE == INPUT(n)->type))
965 i->type = V4L2_INPUT_TYPE_TUNER;
966 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
967 i->std |= tvnorms[n].id;
983 if (*index >= MAX_EM28XX_INPUT)
985 if (0 == INPUT(*index)->type)
989 video_mux(dev, *index);
997 struct v4l2_audio *a = arg;
998 unsigned int index = a->index;
1002 memset(a, 0, sizeof(*a));
1003 index = dev->ctl_ainput;
1006 strcpy(a->name, "Television");
1008 strcpy(a->name, "Line In");
1010 a->capability = V4L2_AUDCAP_STEREO;
1015 case VIDIOC_S_AUDIO:
1017 struct v4l2_audio *a = arg;
1018 if (a->index != dev->ctl_ainput)
1024 /* --- controls ---------------------------------------------- */
1025 case VIDIOC_QUERYCTRL:
1027 struct v4l2_queryctrl *qc = arg;
1030 memset(qc,0,sizeof(*qc));
1033 if (!dev->has_msp34xx) {
1034 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1035 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1036 memcpy(qc, &(em28xx_qctrl[i]),
1042 if (dev->decoder == EM28XX_TVP5150) {
1043 em28xx_i2c_call_clients(dev,cmd,qc);
1049 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1050 if (qc->id && qc->id == saa711x_qctrl[i].id) {
1051 memcpy(qc, &(saa711x_qctrl[i]),
1062 struct v4l2_control *ctrl = arg;
1065 if (!dev->has_msp34xx)
1066 retval=em28xx_get_ctrl(dev, ctrl);
1067 if (retval==-EINVAL) {
1068 if (dev->decoder == EM28XX_TVP5150) {
1069 em28xx_i2c_call_clients(dev,cmd,arg);
1073 return saa711x_get_ctrl(dev, ctrl);
1074 } else return retval;
1079 struct v4l2_control *ctrl = arg;
1082 if (!dev->has_msp34xx){
1083 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1084 if (ctrl->id == em28xx_qctrl[i].id) {
1086 em28xx_qctrl[i].minimum
1088 em28xx_qctrl[i].maximum)
1090 return em28xx_set_ctrl(dev, ctrl);
1095 if (dev->decoder == EM28XX_TVP5150) {
1096 em28xx_i2c_call_clients(dev,cmd,arg);
1098 } else if (!dev->has_msp34xx) {
1099 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1100 if (ctrl->id == em28xx_qctrl[i].id) {
1102 em28xx_qctrl[i].minimum
1104 em28xx_qctrl[i].maximum)
1106 return em28xx_set_ctrl(dev, ctrl);
1109 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1110 if (ctrl->id == saa711x_qctrl[i].id) {
1112 saa711x_qctrl[i].minimum
1114 saa711x_qctrl[i].maximum)
1116 return saa711x_set_ctrl(dev, ctrl);
1124 /* --- tuner ioctls ------------------------------------------ */
1125 case VIDIOC_G_TUNER:
1127 struct v4l2_tuner *t = arg;
1133 memset(t, 0, sizeof(*t));
1134 strcpy(t->name, "Tuner");
1135 t->type = V4L2_TUNER_ANALOG_TV;
1136 t->capability = V4L2_TUNER_CAP_NORM;
1137 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1138 /* t->signal = 0xffff;*/
1139 /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1140 /* No way to get signal strength? */
1142 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1146 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1148 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1152 case VIDIOC_S_TUNER:
1154 struct v4l2_tuner *t = arg;
1159 memset(t, 0, sizeof(*t));
1160 strcpy(t->name, "Tuner");
1161 t->type = V4L2_TUNER_ANALOG_TV;
1162 t->capability = V4L2_TUNER_CAP_NORM;
1163 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1164 /* t->signal = 0xffff; */
1165 /* No way to get signal strength? */
1167 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1171 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1173 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1177 case VIDIOC_G_FREQUENCY:
1179 struct v4l2_frequency *f = arg;
1181 memset(f, 0, sizeof(*f));
1182 f->type = V4L2_TUNER_ANALOG_TV;
1183 f->frequency = dev->ctl_freq;
1187 case VIDIOC_S_FREQUENCY:
1189 struct v4l2_frequency *f = arg;
1194 if (V4L2_TUNER_ANALOG_TV != f->type)
1198 dev->ctl_freq = f->frequency;
1199 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1204 case VIDIOC_CROPCAP:
1206 struct v4l2_cropcap *cc = arg;
1208 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1210 cc->bounds.left = 0;
1212 cc->bounds.width = dev->width;
1213 cc->bounds.height = dev->height;
1214 cc->defrect = cc->bounds;
1215 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1216 cc->pixelaspect.denominator = 59;
1219 case VIDIOC_STREAMON:
1223 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1224 || dev->io != IO_MMAP)
1227 if (list_empty(&dev->inqueue))
1230 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1232 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1236 case VIDIOC_STREAMOFF:
1241 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1242 || dev->io != IO_MMAP)
1245 if (dev->stream == STREAM_ON) {
1246 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1247 if ((ret = em28xx_stream_interrupt(dev)))
1250 em28xx_empty_framequeues(dev);
1255 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1262 * em28xx_v4l2_do_ioctl()
1263 * This function is _not_ called directly, but from
1264 * em28xx_v4l2_ioctl. Userspace
1265 * copying is done already, arg is a kernel pointer.
1267 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1268 unsigned int cmd, void *arg)
1270 struct em28xx *dev = filp->private_data;
1275 if (video_debug > 1)
1276 v4l_print_ioctl(dev->name,cmd);
1280 /* --- capabilities ------------------------------------------ */
1281 case VIDIOC_QUERYCAP:
1283 struct v4l2_capability *cap = arg;
1285 memset(cap, 0, sizeof(*cap));
1286 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1287 strlcpy(cap->card, em28xx_boards[dev->model].name,
1289 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1290 sizeof(cap->bus_info));
1291 cap->version = EM28XX_VERSION_CODE;
1293 V4L2_CAP_VIDEO_CAPTURE |
1295 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1297 cap->capabilities |= V4L2_CAP_TUNER;
1301 /* --- capture ioctls ---------------------------------------- */
1302 case VIDIOC_ENUM_FMT:
1304 struct v4l2_fmtdesc *fmtd = arg;
1306 if (fmtd->index != 0)
1308 memset(fmtd, 0, sizeof(*fmtd));
1309 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1310 strcpy(fmtd->description, "Packed YUY2");
1311 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1312 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1318 struct v4l2_format *format = arg;
1320 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
1322 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1323 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1324 V4L2_BUF_TYPE_VBI_CAPTURE ?
1325 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1328 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1331 format->fmt.pix.width = dev->width;
1332 format->fmt.pix.height = dev->height;
1333 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1334 format->fmt.pix.bytesperline = dev->bytesperline;
1335 format->fmt.pix.sizeimage = dev->frame_size;
1336 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1337 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1339 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
1344 case VIDIOC_TRY_FMT:
1347 struct v4l2_format *format = arg;
1350 int width = format->fmt.pix.width;
1351 int height = format->fmt.pix.height;
1352 unsigned int hscale, vscale;
1353 unsigned int maxh, maxw;
1355 maxw = norm_maxw(dev);
1356 maxh = norm_maxh(dev);
1358 /* int both_fields; */
1360 em28xx_videodbg("%s: type=%s\n",
1362 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1365 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1366 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1367 V4L2_BUF_TYPE_VBI_CAPTURE ?
1368 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1371 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1374 em28xx_videodbg("%s: requested %dx%d\n",
1376 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1377 "VIDIOC_S_FMT", format->fmt.pix.width,
1378 format->fmt.pix.height);
1380 /* FIXME: Move some code away from here */
1381 /* width must even because of the YUYV format */
1382 /* height must be even because of interlacing */
1396 /* the em2800 can only scale down to 50% */
1397 if(height % (maxh / 2))
1399 if(width % (maxw / 2))
1401 /* according to empiatech support */
1402 /* the MaxPacketSize is to small to support */
1403 /* framesizes larger than 640x480 @ 30 fps */
1404 /* or 640x576 @ 25 fps. As this would cut */
1405 /* of a part of the image we prefer */
1406 /* 360x576 or 360x480 for now */
1407 if(width == maxw && height == maxh)
1412 (((unsigned long)maxw) << 12) / width - 4096L) >=
1416 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1419 (((unsigned long)maxh) << 12) / height - 4096L) >=
1423 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1425 format->fmt.pix.width = width;
1426 format->fmt.pix.height = height;
1427 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1428 format->fmt.pix.bytesperline = width * 2;
1429 format->fmt.pix.sizeimage = width * 2 * height;
1430 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1431 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1433 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
1435 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1436 "VIDIOC_S_FMT", format->fmt.pix.width,
1437 format->fmt.pix.height, hscale, vscale);
1439 if (cmd == VIDIOC_TRY_FMT)
1442 for (i = 0; i < dev->num_frames; i++)
1443 if (dev->frame[i].vma_use_count) {
1444 em28xx_videodbg("VIDIOC_S_FMT failed. "
1445 "Unmap the buffers first.\n");
1449 /* stop io in case it is already in progress */
1450 if (dev->stream == STREAM_ON) {
1451 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
1452 if ((ret = em28xx_stream_interrupt(dev)))
1456 em28xx_release_buffers(dev);
1459 /* set new image size */
1461 dev->height = height;
1462 dev->frame_size = dev->width * dev->height * 2;
1463 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1464 dev->bytesperline = dev->width * 2;
1465 dev->hscale = hscale;
1466 dev->vscale = vscale;
1467 /* dev->both_fileds = both_fileds; */
1468 em28xx_uninit_isoc(dev);
1469 em28xx_set_alternate(dev);
1470 em28xx_capture_start(dev, 1);
1471 em28xx_resolution_set(dev);
1472 em28xx_init_isoc(dev);
1477 /* --- streaming capture ------------------------------------- */
1478 case VIDIOC_REQBUFS:
1480 struct v4l2_requestbuffers *rb = arg;
1484 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1485 rb->memory != V4L2_MEMORY_MMAP)
1488 if (dev->io == IO_READ) {
1489 em28xx_videodbg ("method is set to read;"
1490 " close and open the device again to"
1491 " choose the mmap I/O method\n");
1495 for (i = 0; i < dev->num_frames; i++)
1496 if (dev->frame[i].vma_use_count) {
1497 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1501 if (dev->stream == STREAM_ON) {
1502 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1503 if ((ret = em28xx_stream_interrupt(dev)))
1507 em28xx_empty_framequeues(dev);
1509 em28xx_release_buffers(dev);
1512 em28xx_request_buffers(dev, rb->count);
1514 dev->frame_current = NULL;
1516 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1518 dev->io = rb->count ? IO_MMAP : IO_NONE;
1522 case VIDIOC_QUERYBUF:
1524 struct v4l2_buffer *b = arg;
1526 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1527 b->index >= dev->num_frames || dev->io != IO_MMAP)
1530 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1532 if (dev->frame[b->index].vma_use_count) {
1533 b->flags |= V4L2_BUF_FLAG_MAPPED;
1535 if (dev->frame[b->index].state == F_DONE)
1536 b->flags |= V4L2_BUF_FLAG_DONE;
1537 else if (dev->frame[b->index].state != F_UNUSED)
1538 b->flags |= V4L2_BUF_FLAG_QUEUED;
1543 struct v4l2_buffer *b = arg;
1544 unsigned long lock_flags;
1546 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1547 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1551 if (dev->frame[b->index].state != F_UNUSED) {
1554 dev->frame[b->index].state = F_QUEUED;
1556 /* add frame to fifo */
1557 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1558 list_add_tail(&dev->frame[b->index].frame,
1560 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1566 struct v4l2_buffer *b = arg;
1567 struct em28xx_frame_t *f;
1568 unsigned long lock_flags;
1571 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1572 || dev->io != IO_MMAP)
1575 if (list_empty(&dev->outqueue)) {
1576 if (dev->stream == STREAM_OFF)
1578 if (filp->f_flags & O_NONBLOCK)
1580 ret = wait_event_interruptible
1582 (!list_empty(&dev->outqueue)) ||
1583 (dev->state & DEV_DISCONNECTED));
1586 if (dev->state & DEV_DISCONNECTED)
1590 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1591 f = list_entry(dev->outqueue.next,
1592 struct em28xx_frame_t, frame);
1593 list_del(dev->outqueue.next);
1594 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1596 f->state = F_UNUSED;
1597 memcpy(b, &f->buf, sizeof(*b));
1599 if (f->vma_use_count)
1600 b->flags |= V4L2_BUF_FLAG_MAPPED;
1605 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1606 em28xx_video_do_ioctl);
1612 * em28xx_v4l2_ioctl()
1613 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1615 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1616 unsigned int cmd, unsigned long arg)
1619 struct em28xx *dev = filp->private_data;
1621 if (down_interruptible(&dev->fileop_lock))
1622 return -ERESTARTSYS;
1624 if (dev->state & DEV_DISCONNECTED) {
1625 em28xx_errdev("v4l2 ioctl: device not present\n");
1626 up(&dev->fileop_lock);
1630 if (dev->state & DEV_MISCONFIGURED) {
1632 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1633 up(&dev->fileop_lock);
1637 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1639 up(&dev->fileop_lock);
1644 static struct file_operations em28xx_v4l_fops = {
1645 .owner = THIS_MODULE,
1646 .open = em28xx_v4l2_open,
1647 .release = em28xx_v4l2_close,
1648 .ioctl = em28xx_v4l2_ioctl,
1649 .read = em28xx_v4l2_read,
1650 .poll = em28xx_v4l2_poll,
1651 .mmap = em28xx_v4l2_mmap,
1652 .llseek = no_llseek,
1653 .compat_ioctl = v4l_compat_ioctl32,
1657 /******************************** usb interface *****************************************/
1661 * allocates and inits the device structs, registers i2c bus and v4l device
1663 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1664 int minor, int model)
1666 struct em28xx *dev = *devhandle;
1667 int retval = -ENOMEM;
1669 unsigned int maxh, maxw;
1673 init_MUTEX(&dev->lock);
1674 init_waitqueue_head(&dev->open);
1676 dev->em28xx_write_regs = em28xx_write_regs;
1677 dev->em28xx_read_reg = em28xx_read_reg;
1678 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1679 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1680 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1681 dev->is_em2800 = em28xx_boards[model].is_em2800;
1682 dev->has_tuner = em28xx_boards[model].has_tuner;
1683 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1684 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1685 dev->decoder = em28xx_boards[model].decoder;
1688 dev->tuner_type = tuner;
1690 dev->tuner_type = em28xx_boards[model].tuner_type;
1692 dev->video_inputs = em28xx_boards[model].vchannels;
1694 for (i = 0; i < TVNORMS; i++)
1695 if (em28xx_boards[model].norm == tvnorms[i].mode)
1700 dev->tvnorm = &tvnorms[i]; /* set default norm */
1702 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1704 maxw = norm_maxw(dev);
1705 maxh = norm_maxh(dev);
1707 /* set default image size */
1710 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1711 dev->field_size = dev->width * dev->height;
1713 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1714 dev->bytesperline = dev->width * 2;
1719 /* setup video picture settings for saa7113h */
1720 memset(&dev->vpic, 0, sizeof(dev->vpic));
1721 dev->vpic.colour = 128 << 8;
1722 dev->vpic.hue = 128 << 8;
1723 dev->vpic.brightness = 128 << 8;
1724 dev->vpic.contrast = 192 << 8;
1725 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1726 dev->vpic.depth = 16;
1727 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1729 #ifdef CONFIG_MODULES
1730 /* request some modules */
1731 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1732 request_module("saa711x");
1733 if (dev->decoder == EM28XX_TVP5150)
1734 request_module("tvp5150");
1736 request_module("tuner");
1737 if (dev->tda9887_conf)
1738 request_module("tda9887");
1740 errCode = em28xx_config(dev);
1742 em28xx_errdev("error configuring device\n");
1748 /* register i2c bus */
1749 em28xx_i2c_register(dev);
1751 /* Do board specific init and eeprom reading */
1752 em28xx_card_setup(dev);
1754 /* configure the device */
1755 em28xx_config_i2c(dev);
1759 errCode = em28xx_config(dev);
1761 #ifdef CONFIG_MODULES
1762 if (dev->has_msp34xx)
1763 request_module("msp3400");
1765 /* allocate and fill v4l2 device struct */
1766 dev->vdev = video_device_alloc();
1767 if (NULL == dev->vdev) {
1768 em28xx_errdev("cannot allocate video_device.\n");
1773 dev->vdev->type = VID_TYPE_CAPTURE;
1775 dev->vdev->type |= VID_TYPE_TUNER;
1776 dev->vdev->hardware = 0;
1777 dev->vdev->fops = &em28xx_v4l_fops;
1778 dev->vdev->minor = -1;
1779 dev->vdev->dev = &dev->udev->dev;
1780 dev->vdev->release = video_device_release;
1781 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1783 list_add_tail(&dev->devlist,&em28xx_devlist);
1785 /* register v4l2 device */
1787 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1788 em28xx_errdev("unable to register video device (error=%i).\n",
1791 list_del(&dev->devlist);
1792 video_device_release(dev->vdev);
1796 if (dev->has_msp34xx) {
1797 /* Send a reset to other chips via gpio */
1798 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1800 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1808 em28xx_info("V4L2 device registered as /dev/video%d\n",
1815 * em28xx_usb_probe()
1816 * checks for supported devices
1818 static int em28xx_usb_probe(struct usb_interface *interface,
1819 const struct usb_device_id *id)
1821 const struct usb_endpoint_descriptor *endpoint;
1822 struct usb_device *udev;
1823 struct usb_interface *uif;
1824 struct em28xx *dev = NULL;
1825 int retval = -ENODEV;
1826 int model,i,nr,ifnum;
1828 udev = usb_get_dev(interface_to_usbdev(interface));
1829 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1832 /* Don't register audio interfaces */
1833 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1834 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1835 udev->descriptor.idVendor,udev->descriptor.idProduct,
1837 interface->altsetting[0].desc.bInterfaceClass);
1841 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1842 udev->descriptor.idVendor,udev->descriptor.idProduct,
1844 interface->altsetting[0].desc.bInterfaceClass);
1846 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1848 /* check if the the device has the iso in endpoint at the correct place */
1849 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1850 USB_ENDPOINT_XFER_ISOC) {
1851 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1854 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1855 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1859 model=id->driver_info;
1860 nr=interface->minor;
1862 if (nr>EM28XX_MAXBOARDS) {
1863 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1867 /* allocate memory for our device state and initialize it */
1868 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1870 em28xx_err(DRIVER_NAME ": out of memory!\n");
1874 /* compute alternate max packet sizes */
1875 uif = udev->actconfig->interface[0];
1877 dev->num_alt=uif->num_altsetting;
1878 printk(DRIVER_NAME ": Alternate settings: %i\n",dev->num_alt);
1879 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1880 dev->alt_max_pkt_size = kmalloc(32*
1881 dev->num_alt,GFP_KERNEL);
1882 if (dev->alt_max_pkt_size == NULL) {
1883 em28xx_err(DRIVER_NAME ": out of memory!\n");
1887 for (i = 0; i < dev->num_alt ; i++) {
1888 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1890 dev->alt_max_pkt_size[i] =
1891 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1892 printk(DRIVER_NAME ": Alternate setting %i, max size= %i\n",i,
1893 dev->alt_max_pkt_size[i]);
1896 snprintf(dev->name, 29, "em28xx #%d", nr);
1898 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1901 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1902 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1903 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1904 "%s: workaround that. Redirect complaints to the vendor of\n"
1905 "%s: the TV card. Best regards,\n"
1907 dev->name,dev->name,dev->name,dev->name,dev->name);
1908 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1910 for (i = 0; i < em28xx_bcount; i++) {
1911 printk("%s: card=%d -> %s\n",
1912 dev->name, i, em28xx_boards[i].name);
1916 /* allocate device struct */
1917 retval = em28xx_init_dev(&dev, udev, nr, model);
1921 em28xx_info("Found %s\n", em28xx_boards[model].name);
1923 /* save our data pointer in this interface device */
1924 usb_set_intfdata(interface, dev);
1929 * em28xx_usb_disconnect()
1930 * called when the device gets diconencted
1931 * video device will be unregistered on v4l2_close in case it is still open
1933 static void em28xx_usb_disconnect(struct usb_interface *interface)
1935 struct em28xx *dev = usb_get_intfdata(interface);
1936 usb_set_intfdata(interface, NULL);
1938 /*FIXME: IR should be disconnected */
1944 down_write(&em28xx_disconnect);
1948 em28xx_info("disconnecting %s\n", dev->vdev->name);
1950 wake_up_interruptible_all(&dev->open);
1954 ("device /dev/video%d is open! Deregistration and memory "
1955 "deallocation are deferred on close.\n", dev->vdev->minor);
1956 dev->state |= DEV_MISCONFIGURED;
1957 em28xx_uninit_isoc(dev);
1958 dev->state |= DEV_DISCONNECTED;
1959 wake_up_interruptible(&dev->wait_frame);
1960 wake_up_interruptible(&dev->wait_stream);
1962 dev->state |= DEV_DISCONNECTED;
1963 em28xx_release_resources(dev);
1969 kfree(dev->alt_max_pkt_size);
1973 up_write(&em28xx_disconnect);
1976 static struct usb_driver em28xx_usb_driver = {
1978 .probe = em28xx_usb_probe,
1979 .disconnect = em28xx_usb_disconnect,
1980 .id_table = em28xx_id_table,
1983 static int __init em28xx_module_init(void)
1987 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1988 (EM28XX_VERSION_CODE >> 16) & 0xff,
1989 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1991 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1992 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1995 /* register this driver with the USB subsystem */
1996 result = usb_register(&em28xx_usb_driver);
1998 em28xx_err(DRIVER_NAME
1999 " usb_register failed. Error number %d.\n", result);
2004 static void __exit em28xx_module_exit(void)
2006 /* deregister this driver with the USB subsystem */
2007 usb_deregister(&em28xx_usb_driver);
2010 module_init(em28xx_module_init);
2011 module_exit(em28xx_module_exit);