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 #define TVNORMS ARRAY_SIZE(tvnorms)
106 /* supported controls */
107 /* Common to all boards */
108 static struct v4l2_queryctrl em28xx_qctrl[] = {
110 .id = V4L2_CID_AUDIO_VOLUME,
111 .type = V4L2_CTRL_TYPE_INTEGER,
116 .default_value = 0x1f,
119 .id = V4L2_CID_AUDIO_MUTE,
120 .type = V4L2_CTRL_TYPE_BOOLEAN,
130 static struct usb_driver em28xx_usb_driver;
132 static DEFINE_MUTEX(em28xx_sysfs_lock);
133 static DECLARE_RWSEM(em28xx_disconnect);
135 /********************* v4l2 interface ******************************************/
139 * inits registers with sane defaults
141 static int em28xx_config(struct em28xx *dev)
144 /* Sets I2C speed to 100 KHz */
145 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
147 /* enable vbi capturing */
149 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
150 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
151 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
153 em28xx_audio_usb_mute(dev, 1);
154 dev->mute = 1; /* maybe not the right place... */
156 em28xx_audio_analog_set(dev);
157 em28xx_audio_analog_setup(dev);
158 em28xx_outfmt_set_yuv422(dev);
159 em28xx_colorlevels_set_default(dev);
160 em28xx_compression_disable(dev);
166 * em28xx_config_i2c()
167 * configure i2c attached devices
169 static void em28xx_config_i2c(struct em28xx *dev)
171 struct v4l2_frequency f;
172 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
173 em28xx_i2c_call_clients(dev, VIDIOC_S_INPUT, &dev->ctl_input);
174 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
176 /* configure tuner */
178 f.type = V4L2_TUNER_ANALOG_TV;
179 f.frequency = 9076; /* FIXME:remove magic number */
180 dev->ctl_freq = f.frequency;
181 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
183 /* configure tda9887 */
186 /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
190 * em28xx_empty_framequeues()
191 * prepare queues for incoming and outgoing frames
193 static void em28xx_empty_framequeues(struct em28xx *dev)
197 INIT_LIST_HEAD(&dev->inqueue);
198 INIT_LIST_HEAD(&dev->outqueue);
200 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
201 dev->frame[i].state = F_UNUSED;
202 dev->frame[i].buf.bytesused = 0;
206 static void video_mux(struct em28xx *dev, int index)
210 input = INPUT(index)->vmux;
211 dev->ctl_input = index;
212 dev->ctl_ainput = INPUT(index)->amux;
214 em28xx_i2c_call_clients(dev, VIDIOC_S_INPUT, &input);
216 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
218 if (dev->has_msp34xx) {
220 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
221 em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
222 ainput = EM28XX_AUDIO_SRC_TUNER;
223 em28xx_audio_source(dev, ainput);
225 switch (dev->ctl_ainput) {
227 ainput = EM28XX_AUDIO_SRC_TUNER;
230 ainput = EM28XX_AUDIO_SRC_LINE;
232 em28xx_audio_source(dev, ainput);
238 * inits the device and starts isoc transfer
240 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
242 int minor = iminor(inode);
244 struct em28xx *h,*dev = NULL;
245 struct list_head *list;
247 list_for_each(list,&em28xx_devlist) {
248 h = list_entry(list, struct em28xx, devlist);
249 if (h->vdev->minor == minor) {
251 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
253 if (h->vbi_dev->minor == minor) {
255 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
261 filp->private_data=dev;
263 em28xx_videodbg("open minor=%d type=%s users=%d\n",
264 minor,v4l2_type_names[dev->type],dev->users);
266 if (!down_read_trylock(&em28xx_disconnect))
270 em28xx_warn("this driver can be opened only once\n");
271 up_read(&em28xx_disconnect);
275 mutex_init(&dev->fileop_lock); /* to 1 == available */
276 spin_lock_init(&dev->queue_lock);
277 init_waitqueue_head(&dev->wait_frame);
278 init_waitqueue_head(&dev->wait_stream);
280 mutex_lock(&dev->lock);
282 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
283 em28xx_set_alternate(dev);
285 dev->width = norm_maxw(dev);
286 dev->height = norm_maxh(dev);
287 dev->frame_size = dev->width * dev->height * 2;
288 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
289 dev->bytesperline = dev->width * 2;
293 em28xx_capture_start(dev, 1);
294 em28xx_resolution_set(dev);
296 /* device needs to be initialized before isoc transfer */
299 /* start the transfer */
300 errCode = em28xx_init_isoc(dev);
307 filp->private_data = dev;
309 dev->stream = STREAM_OFF;
313 em28xx_empty_framequeues(dev);
315 dev->state |= DEV_INITIALIZED;
318 mutex_unlock(&dev->lock);
319 up_read(&em28xx_disconnect);
324 * em28xx_realease_resources()
325 * unregisters the v4l2,i2c and usb devices
326 * called when the device gets disconected or at module unload
328 static void em28xx_release_resources(struct em28xx *dev)
330 mutex_lock(&em28xx_sysfs_lock);
332 /*FIXME: I2C IR should be disconnected */
334 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
335 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
336 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
337 list_del(&dev->devlist);
338 video_unregister_device(dev->vdev);
339 video_unregister_device(dev->vbi_dev);
340 em28xx_i2c_unregister(dev);
341 usb_put_dev(dev->udev);
342 mutex_unlock(&em28xx_sysfs_lock);
345 /* Mark device as unused */
346 em28xx_devused&=~(1<<dev->devno);
350 * em28xx_v4l2_close()
351 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
353 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
356 struct em28xx *dev=filp->private_data;
358 em28xx_videodbg("users=%d\n", dev->users);
360 mutex_lock(&dev->lock);
362 em28xx_uninit_isoc(dev);
364 em28xx_release_buffers(dev);
366 /* the device is already disconnect, free the remaining resources */
367 if (dev->state & DEV_DISCONNECTED) {
368 em28xx_release_resources(dev);
369 mutex_unlock(&dev->lock);
374 /* set alternate 0 */
376 em28xx_videodbg("setting alternate 0\n");
377 errCode = usb_set_interface(dev->udev, 0, 0);
379 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
384 wake_up_interruptible_nr(&dev->open, 1);
385 mutex_unlock(&dev->lock);
391 * will allocate buffers when called for the first time
394 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
397 struct em28xx_frame_t *f, *i;
398 unsigned long lock_flags;
400 struct em28xx *dev = filp->private_data;
402 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
403 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
405 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
406 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
407 em28xx_videodbg("not supported yet! ...\n");
408 if (copy_to_user(buf, "", 1)) {
409 mutex_unlock(&dev->fileop_lock);
414 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
415 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
416 em28xx_videodbg("not supported yet! ...\n");
417 if (copy_to_user(buf, "", 1)) {
418 mutex_unlock(&dev->fileop_lock);
424 if (mutex_lock_interruptible(&dev->fileop_lock))
427 if (dev->state & DEV_DISCONNECTED) {
428 em28xx_videodbg("device not present\n");
429 mutex_unlock(&dev->fileop_lock);
433 if (dev->state & DEV_MISCONFIGURED) {
434 em28xx_videodbg("device misconfigured; close and open it again\n");
435 mutex_unlock(&dev->fileop_lock);
439 if (dev->io == IO_MMAP) {
440 em28xx_videodbg ("IO method is set to mmap; close and open"
441 " the device again to choose the read method\n");
442 mutex_unlock(&dev->fileop_lock);
446 if (dev->io == IO_NONE) {
447 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
448 em28xx_errdev("read failed, not enough memory\n");
449 mutex_unlock(&dev->fileop_lock);
453 dev->stream = STREAM_ON;
454 em28xx_queue_unusedframes(dev);
458 mutex_unlock(&dev->fileop_lock);
462 if (list_empty(&dev->outqueue)) {
463 if (filp->f_flags & O_NONBLOCK) {
464 mutex_unlock(&dev->fileop_lock);
467 ret = wait_event_interruptible
469 (!list_empty(&dev->outqueue)) ||
470 (dev->state & DEV_DISCONNECTED));
472 mutex_unlock(&dev->fileop_lock);
475 if (dev->state & DEV_DISCONNECTED) {
476 mutex_unlock(&dev->fileop_lock);
481 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
483 spin_lock_irqsave(&dev->queue_lock, lock_flags);
484 list_for_each_entry(i, &dev->outqueue, frame)
486 INIT_LIST_HEAD(&dev->outqueue);
487 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
489 em28xx_queue_unusedframes(dev);
491 if (count > f->buf.length)
492 count = f->buf.length;
494 if (copy_to_user(buf, f->bufmem, count)) {
495 mutex_unlock(&dev->fileop_lock);
500 mutex_unlock(&dev->fileop_lock);
507 * will allocate buffers when called for the first time
509 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
511 unsigned int mask = 0;
512 struct em28xx *dev = filp->private_data;
514 if (mutex_lock_interruptible(&dev->fileop_lock))
517 if (dev->state & DEV_DISCONNECTED) {
518 em28xx_videodbg("device not present\n");
519 } else if (dev->state & DEV_MISCONFIGURED) {
520 em28xx_videodbg("device is misconfigured; close and open it again\n");
522 if (dev->io == IO_NONE) {
523 if (!em28xx_request_buffers
524 (dev, EM28XX_NUM_READ_FRAMES)) {
526 ("poll() failed, not enough memory\n");
529 dev->stream = STREAM_ON;
533 if (dev->io == IO_READ) {
534 em28xx_queue_unusedframes(dev);
535 poll_wait(filp, &dev->wait_frame, wait);
537 if (!list_empty(&dev->outqueue))
538 mask |= POLLIN | POLLRDNORM;
540 mutex_unlock(&dev->fileop_lock);
546 mutex_unlock(&dev->fileop_lock);
553 static void em28xx_vm_open(struct vm_area_struct *vma)
555 struct em28xx_frame_t *f = vma->vm_private_data;
562 static void em28xx_vm_close(struct vm_area_struct *vma)
564 /* NOTE: buffers are not freed here */
565 struct em28xx_frame_t *f = vma->vm_private_data;
569 static struct vm_operations_struct em28xx_vm_ops = {
570 .open = em28xx_vm_open,
571 .close = em28xx_vm_close,
577 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
579 unsigned long size = vma->vm_end - vma->vm_start,
580 start = vma->vm_start;
584 struct em28xx *dev = filp->private_data;
586 if (mutex_lock_interruptible(&dev->fileop_lock))
589 if (dev->state & DEV_DISCONNECTED) {
590 em28xx_videodbg("mmap: device not present\n");
591 mutex_unlock(&dev->fileop_lock);
595 if (dev->state & DEV_MISCONFIGURED) {
596 em28xx_videodbg ("mmap: Device is misconfigured; close and "
598 mutex_unlock(&dev->fileop_lock);
602 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
603 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
604 mutex_unlock(&dev->fileop_lock);
608 for (i = 0; i < dev->num_frames; i++) {
609 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
612 if (i == dev->num_frames) {
613 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
614 mutex_unlock(&dev->fileop_lock);
618 /* VM_IO is eventually going to replace PageReserved altogether */
619 vma->vm_flags |= VM_IO;
620 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
622 pos = dev->frame[i].bufmem;
623 while (size > 0) { /* size is page-aligned */
624 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
625 em28xx_videodbg("mmap: vm_insert_page failed\n");
626 mutex_unlock(&dev->fileop_lock);
634 vma->vm_ops = &em28xx_vm_ops;
635 vma->vm_private_data = &dev->frame[i];
638 mutex_unlock(&dev->fileop_lock);
644 * return the current saturation, brightness or contrast, mute state
646 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
649 case V4L2_CID_AUDIO_MUTE:
650 ctrl->value = dev->mute;
652 case V4L2_CID_AUDIO_VOLUME:
653 ctrl->value = dev->volume;
662 * mute or set new saturation, brightness or contrast
664 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
667 case V4L2_CID_AUDIO_MUTE:
668 if (ctrl->value != dev->mute) {
669 dev->mute = ctrl->value;
670 em28xx_audio_usb_mute(dev, ctrl->value);
671 return em28xx_audio_analog_set(dev);
674 case V4L2_CID_AUDIO_VOLUME:
675 dev->volume = ctrl->value;
676 return em28xx_audio_analog_set(dev);
683 * em28xx_stream_interrupt()
686 static int em28xx_stream_interrupt(struct em28xx *dev)
690 /* stop reading from the device */
692 dev->stream = STREAM_INTERRUPT;
693 ret = wait_event_timeout(dev->wait_stream,
694 (dev->stream == STREAM_OFF) ||
695 (dev->state & DEV_DISCONNECTED),
697 if (dev->state & DEV_DISCONNECTED)
700 dev->state |= DEV_MISCONFIGURED;
701 em28xx_videodbg("device is misconfigured; close and "
702 "open /dev/video%d again\n",
703 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
710 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
712 unsigned int hscale, vscale;
713 unsigned int maxh, maxw;
715 maxw = norm_maxw(dev);
716 maxh = norm_maxh(dev);
718 /* width must even because of the YUYV format */
719 /* height must be even because of interlacing */
732 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
734 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
736 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
738 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
740 /* set new image size */
742 dev->height = height;
743 dev->frame_size = dev->width * dev->height * 2;
744 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
745 dev->bytesperline = dev->width * 2;
746 dev->hscale = hscale;
747 dev->vscale = vscale;
749 em28xx_resolution_set(dev);
754 static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
756 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
757 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
758 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
759 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
760 "V4L2_BUF_TYPE_VBI_CAPTURE" :
761 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
762 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
765 switch (format->type) {
766 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
768 format->fmt.pix.width = dev->width;
769 format->fmt.pix.height = dev->height;
770 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
771 format->fmt.pix.bytesperline = dev->bytesperline;
772 format->fmt.pix.sizeimage = dev->frame_size;
773 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
774 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
776 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
781 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
783 format->fmt.sliced.service_set=0;
785 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
787 if (format->fmt.sliced.service_set==0)
799 static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
803 int width = format->fmt.pix.width;
804 int height = format->fmt.pix.height;
805 unsigned int hscale, vscale;
806 unsigned int maxh, maxw;
808 maxw = norm_maxw(dev);
809 maxh = norm_maxh(dev);
811 em28xx_videodbg("%s: type=%s\n",
812 cmd == VIDIOC_TRY_FMT ?
813 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
814 format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
815 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
816 format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
817 "V4L2_BUF_TYPE_VBI_CAPTURE " :
820 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
821 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
823 if (format->fmt.sliced.service_set==0)
830 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
833 em28xx_videodbg("%s: requested %dx%d\n",
834 cmd == VIDIOC_TRY_FMT ?
835 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
836 format->fmt.pix.width, format->fmt.pix.height);
838 /* FIXME: Move some code away from here */
839 /* width must even because of the YUYV format */
840 /* height must be even because of interlacing */
854 /* the em2800 can only scale down to 50% */
855 if(height % (maxh / 2))
857 if(width % (maxw / 2))
859 /* according to empiatech support */
860 /* the MaxPacketSize is to small to support */
861 /* framesizes larger than 640x480 @ 30 fps */
862 /* or 640x576 @ 25 fps. As this would cut */
863 /* of a part of the image we prefer */
864 /* 360x576 or 360x480 for now */
865 if(width == maxw && height == maxh)
869 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
872 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
874 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
877 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
879 format->fmt.pix.width = width;
880 format->fmt.pix.height = height;
881 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
882 format->fmt.pix.bytesperline = width * 2;
883 format->fmt.pix.sizeimage = width * 2 * height;
884 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
885 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
887 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
888 cmd == VIDIOC_TRY_FMT ?
889 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
890 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
892 if (cmd == VIDIOC_TRY_FMT)
895 for (i = 0; i < dev->num_frames; i++)
896 if (dev->frame[i].vma_use_count) {
897 em28xx_videodbg("VIDIOC_S_FMT failed. "
898 "Unmap the buffers first.\n");
902 /* stop io in case it is already in progress */
903 if (dev->stream == STREAM_ON) {
904 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
905 if ((ret = em28xx_stream_interrupt(dev)))
909 em28xx_release_buffers(dev);
912 /* set new image size */
914 dev->height = height;
915 dev->frame_size = dev->width * dev->height * 2;
916 dev->field_size = dev->frame_size >> 1;
917 dev->bytesperline = dev->width * 2;
918 dev->hscale = hscale;
919 dev->vscale = vscale;
920 em28xx_uninit_isoc(dev);
921 em28xx_set_alternate(dev);
922 em28xx_capture_start(dev, 1);
923 em28xx_resolution_set(dev);
924 em28xx_init_isoc(dev);
930 * em28xx_v4l2_do_ioctl()
931 * This function is _not_ called directly, but from
932 * em28xx_v4l2_ioctl. Userspace
933 * copying is done already, arg is a kernel pointer.
935 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
936 struct em28xx *dev, unsigned int cmd, void *arg,
937 v4l2_kioctl driver_ioctl)
942 /* ---------- tv norms ---------- */
945 struct v4l2_standard *e = arg;
951 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
952 tvnorms[e->index].name);
960 v4l2_std_id *id = arg;
962 *id = dev->tvnorm->id;
967 v4l2_std_id *id = arg;
970 for (i = 0; i < TVNORMS; i++)
971 if (*id == tvnorms[i].id)
974 for (i = 0; i < TVNORMS; i++)
975 if (*id & tvnorms[i].id)
980 mutex_lock(&dev->lock);
981 dev->tvnorm = &tvnorms[i];
983 em28xx_set_norm(dev, dev->width, dev->height);
985 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
988 mutex_unlock(&dev->lock);
993 /* ------ input switching ---------- */
994 case VIDIOC_ENUMINPUT:
996 struct v4l2_input *i = arg;
998 static const char *iname[] = {
999 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1000 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1001 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1002 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1003 [EM28XX_VMUX_SVIDEO] = "S-Video",
1004 [EM28XX_VMUX_TELEVISION] = "Television",
1005 [EM28XX_VMUX_CABLE] = "Cable TV",
1006 [EM28XX_VMUX_DVB] = "DVB",
1007 [EM28XX_VMUX_DEBUG] = "for debug only",
1011 if (n >= MAX_EM28XX_INPUT)
1013 if (0 == INPUT(n)->type)
1015 memset(i, 0, sizeof(*i));
1017 i->type = V4L2_INPUT_TYPE_CAMERA;
1018 strcpy(i->name, iname[INPUT(n)->type]);
1019 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1020 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1021 i->type = V4L2_INPUT_TYPE_TUNER;
1022 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1023 i->std |= tvnorms[n].id;
1026 case VIDIOC_G_INPUT:
1029 *i = dev->ctl_input;
1033 case VIDIOC_S_INPUT:
1037 if (*index >= MAX_EM28XX_INPUT)
1039 if (0 == INPUT(*index)->type)
1042 mutex_lock(&dev->lock);
1043 video_mux(dev, *index);
1044 mutex_unlock(&dev->lock);
1048 case VIDIOC_G_AUDIO:
1050 struct v4l2_audio *a = arg;
1051 unsigned int index = a->index;
1055 memset(a, 0, sizeof(*a));
1056 index = dev->ctl_ainput;
1059 strcpy(a->name, "Television");
1061 strcpy(a->name, "Line In");
1063 a->capability = V4L2_AUDCAP_STEREO;
1067 case VIDIOC_S_AUDIO:
1069 struct v4l2_audio *a = arg;
1071 if (a->index != dev->ctl_ainput)
1077 /* --- controls ---------------------------------------------- */
1078 case VIDIOC_QUERYCTRL:
1080 struct v4l2_queryctrl *qc = arg;
1083 memset(qc,0,sizeof(*qc));
1086 if (!dev->has_msp34xx) {
1087 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1088 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1089 memcpy(qc, &(em28xx_qctrl[i]),
1095 em28xx_i2c_call_clients(dev,cmd,qc);
1103 struct v4l2_control *ctrl = arg;
1106 if (!dev->has_msp34xx)
1107 retval=em28xx_get_ctrl(dev, ctrl);
1108 if (retval==-EINVAL) {
1109 em28xx_i2c_call_clients(dev,cmd,arg);
1111 } else return retval;
1115 struct v4l2_control *ctrl = arg;
1118 if (!dev->has_msp34xx){
1119 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1120 if (ctrl->id == em28xx_qctrl[i].id) {
1122 em28xx_qctrl[i].minimum
1124 em28xx_qctrl[i].maximum)
1126 return em28xx_set_ctrl(dev, ctrl);
1131 em28xx_i2c_call_clients(dev,cmd,arg);
1134 /* --- tuner ioctls ------------------------------------------ */
1135 case VIDIOC_G_TUNER:
1137 struct v4l2_tuner *t = arg;
1143 memset(t, 0, sizeof(*t));
1144 strcpy(t->name, "Tuner");
1145 t->type = V4L2_TUNER_ANALOG_TV;
1146 t->capability = V4L2_TUNER_CAP_NORM;
1147 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1148 /* t->signal = 0xffff;*/
1149 /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1150 /* No way to get signal strength? */
1151 mutex_lock(&dev->lock);
1152 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1154 mutex_unlock(&dev->lock);
1156 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1158 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1162 case VIDIOC_S_TUNER:
1164 struct v4l2_tuner *t = arg;
1169 memset(t, 0, sizeof(*t));
1170 strcpy(t->name, "Tuner");
1171 t->type = V4L2_TUNER_ANALOG_TV;
1172 t->capability = V4L2_TUNER_CAP_NORM;
1173 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1174 /* t->signal = 0xffff; */
1175 /* No way to get signal strength? */
1176 mutex_lock(&dev->lock);
1177 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1179 mutex_unlock(&dev->lock);
1181 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1183 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1187 case VIDIOC_G_FREQUENCY:
1189 struct v4l2_frequency *f = arg;
1191 memset(f, 0, sizeof(*f));
1192 f->type = V4L2_TUNER_ANALOG_TV;
1193 f->frequency = dev->ctl_freq;
1197 case VIDIOC_S_FREQUENCY:
1199 struct v4l2_frequency *f = arg;
1204 if (V4L2_TUNER_ANALOG_TV != f->type)
1207 mutex_lock(&dev->lock);
1208 dev->ctl_freq = f->frequency;
1209 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1210 mutex_unlock(&dev->lock);
1213 case VIDIOC_CROPCAP:
1215 struct v4l2_cropcap *cc = arg;
1217 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1219 cc->bounds.left = 0;
1221 cc->bounds.width = dev->width;
1222 cc->bounds.height = dev->height;
1223 cc->defrect = cc->bounds;
1224 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1225 cc->pixelaspect.denominator = 59;
1228 case VIDIOC_STREAMON:
1232 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1233 || dev->io != IO_MMAP)
1236 if (list_empty(&dev->inqueue))
1239 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1241 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1245 case VIDIOC_STREAMOFF:
1250 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1251 || dev->io != IO_MMAP)
1254 if (dev->stream == STREAM_ON) {
1255 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1256 if ((ret = em28xx_stream_interrupt(dev)))
1259 em28xx_empty_framequeues(dev);
1264 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1271 * em28xx_v4l2_do_ioctl()
1272 * This function is _not_ called directly, but from
1273 * em28xx_v4l2_ioctl. Userspace
1274 * copying is done already, arg is a kernel pointer.
1276 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1277 unsigned int cmd, void *arg)
1279 struct em28xx *dev = filp->private_data;
1284 if (video_debug > 1)
1285 v4l_print_ioctl(dev->name,cmd);
1289 /* --- capabilities ------------------------------------------ */
1290 case VIDIOC_QUERYCAP:
1292 struct v4l2_capability *cap = arg;
1294 memset(cap, 0, sizeof(*cap));
1295 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1296 strlcpy(cap->card, em28xx_boards[dev->model].name,
1298 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1299 sizeof(cap->bus_info));
1300 cap->version = EM28XX_VERSION_CODE;
1302 V4L2_CAP_SLICED_VBI_CAPTURE |
1303 V4L2_CAP_VIDEO_CAPTURE |
1305 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1307 cap->capabilities |= V4L2_CAP_TUNER;
1310 /* --- capture ioctls ---------------------------------------- */
1311 case VIDIOC_ENUM_FMT:
1313 struct v4l2_fmtdesc *fmtd = arg;
1315 if (fmtd->index != 0)
1317 memset(fmtd, 0, sizeof(*fmtd));
1318 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1319 strcpy(fmtd->description, "Packed YUY2");
1320 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1321 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1325 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
1327 case VIDIOC_TRY_FMT:
1329 return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
1331 case VIDIOC_REQBUFS:
1333 struct v4l2_requestbuffers *rb = arg;
1337 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1338 rb->memory != V4L2_MEMORY_MMAP)
1341 if (dev->io == IO_READ) {
1342 em28xx_videodbg ("method is set to read;"
1343 " close and open the device again to"
1344 " choose the mmap I/O method\n");
1348 for (i = 0; i < dev->num_frames; i++)
1349 if (dev->frame[i].vma_use_count) {
1350 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1354 if (dev->stream == STREAM_ON) {
1355 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1356 if ((ret = em28xx_stream_interrupt(dev)))
1360 em28xx_empty_framequeues(dev);
1362 em28xx_release_buffers(dev);
1365 em28xx_request_buffers(dev, rb->count);
1367 dev->frame_current = NULL;
1369 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1371 dev->io = rb->count ? IO_MMAP : IO_NONE;
1374 case VIDIOC_QUERYBUF:
1376 struct v4l2_buffer *b = arg;
1378 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1379 b->index >= dev->num_frames || dev->io != IO_MMAP)
1382 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1384 if (dev->frame[b->index].vma_use_count) {
1385 b->flags |= V4L2_BUF_FLAG_MAPPED;
1387 if (dev->frame[b->index].state == F_DONE)
1388 b->flags |= V4L2_BUF_FLAG_DONE;
1389 else if (dev->frame[b->index].state != F_UNUSED)
1390 b->flags |= V4L2_BUF_FLAG_QUEUED;
1395 struct v4l2_buffer *b = arg;
1396 unsigned long lock_flags;
1398 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1399 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1403 if (dev->frame[b->index].state != F_UNUSED) {
1406 dev->frame[b->index].state = F_QUEUED;
1408 /* add frame to fifo */
1409 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1410 list_add_tail(&dev->frame[b->index].frame,
1412 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1418 struct v4l2_buffer *b = arg;
1419 struct em28xx_frame_t *f;
1420 unsigned long lock_flags;
1423 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1424 || dev->io != IO_MMAP)
1427 if (list_empty(&dev->outqueue)) {
1428 if (dev->stream == STREAM_OFF)
1430 if (filp->f_flags & O_NONBLOCK)
1432 ret = wait_event_interruptible
1434 (!list_empty(&dev->outqueue)) ||
1435 (dev->state & DEV_DISCONNECTED));
1438 if (dev->state & DEV_DISCONNECTED)
1442 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1443 f = list_entry(dev->outqueue.next,
1444 struct em28xx_frame_t, frame);
1445 list_del(dev->outqueue.next);
1446 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1448 f->state = F_UNUSED;
1449 memcpy(b, &f->buf, sizeof(*b));
1451 if (f->vma_use_count)
1452 b->flags |= V4L2_BUF_FLAG_MAPPED;
1457 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1458 em28xx_video_do_ioctl);
1464 * em28xx_v4l2_ioctl()
1465 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1467 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1468 unsigned int cmd, unsigned long arg)
1471 struct em28xx *dev = filp->private_data;
1473 if (mutex_lock_interruptible(&dev->fileop_lock))
1474 return -ERESTARTSYS;
1476 if (dev->state & DEV_DISCONNECTED) {
1477 em28xx_errdev("v4l2 ioctl: device not present\n");
1478 mutex_unlock(&dev->fileop_lock);
1482 if (dev->state & DEV_MISCONFIGURED) {
1484 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1485 mutex_unlock(&dev->fileop_lock);
1489 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1491 mutex_unlock(&dev->fileop_lock);
1496 static struct file_operations em28xx_v4l_fops = {
1497 .owner = THIS_MODULE,
1498 .open = em28xx_v4l2_open,
1499 .release = em28xx_v4l2_close,
1500 .ioctl = em28xx_v4l2_ioctl,
1501 .read = em28xx_v4l2_read,
1502 .poll = em28xx_v4l2_poll,
1503 .mmap = em28xx_v4l2_mmap,
1504 .llseek = no_llseek,
1505 .compat_ioctl = v4l_compat_ioctl32,
1509 /******************************** usb interface *****************************************/
1513 * allocates and inits the device structs, registers i2c bus and v4l device
1515 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1516 int minor, int model)
1518 struct em28xx *dev = *devhandle;
1519 int retval = -ENOMEM;
1521 unsigned int maxh, maxw;
1525 mutex_init(&dev->lock);
1526 init_waitqueue_head(&dev->open);
1528 dev->em28xx_write_regs = em28xx_write_regs;
1529 dev->em28xx_read_reg = em28xx_read_reg;
1530 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1531 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1532 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1533 dev->is_em2800 = em28xx_boards[model].is_em2800;
1534 dev->has_tuner = em28xx_boards[model].has_tuner;
1535 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1536 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1537 dev->decoder = em28xx_boards[model].decoder;
1540 dev->tuner_type = tuner;
1542 dev->tuner_type = em28xx_boards[model].tuner_type;
1544 dev->video_inputs = em28xx_boards[model].vchannels;
1546 for (i = 0; i < TVNORMS; i++)
1547 if (em28xx_boards[model].norm == tvnorms[i].mode)
1552 dev->tvnorm = &tvnorms[i]; /* set default norm */
1554 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1556 maxw = norm_maxw(dev);
1557 maxh = norm_maxh(dev);
1559 /* set default image size */
1562 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1563 dev->field_size = dev->width * dev->height;
1565 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1566 dev->bytesperline = dev->width * 2;
1571 /* setup video picture settings for saa7113h */
1572 memset(&dev->vpic, 0, sizeof(dev->vpic));
1573 dev->vpic.colour = 128 << 8;
1574 dev->vpic.hue = 128 << 8;
1575 dev->vpic.brightness = 128 << 8;
1576 dev->vpic.contrast = 192 << 8;
1577 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1578 dev->vpic.depth = 16;
1579 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1581 em28xx_pre_card_setup(dev);
1582 #ifdef CONFIG_MODULES
1583 /* request some modules */
1584 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1585 request_module("saa7115");
1586 if (dev->decoder == EM28XX_TVP5150)
1587 request_module("tvp5150");
1589 request_module("tuner");
1590 if (dev->tda9887_conf)
1591 request_module("tda9887");
1593 errCode = em28xx_config(dev);
1595 em28xx_errdev("error configuring device\n");
1597 em28xx_devused&=~(1<<dev->devno);
1601 mutex_lock(&dev->lock);
1602 /* register i2c bus */
1603 em28xx_i2c_register(dev);
1605 /* Do board specific init and eeprom reading */
1606 em28xx_card_setup(dev);
1608 /* configure the device */
1609 em28xx_config_i2c(dev);
1611 mutex_unlock(&dev->lock);
1613 errCode = em28xx_config(dev);
1615 #ifdef CONFIG_MODULES
1616 if (dev->has_msp34xx)
1617 request_module("msp3400");
1619 /* allocate and fill v4l2 device struct */
1620 dev->vdev = video_device_alloc();
1621 if (NULL == dev->vdev) {
1622 em28xx_errdev("cannot allocate video_device.\n");
1624 em28xx_devused&=~(1<<dev->devno);
1628 dev->vbi_dev = video_device_alloc();
1629 if (NULL == dev->vbi_dev) {
1630 em28xx_errdev("cannot allocate video_device.\n");
1633 em28xx_devused&=~(1<<dev->devno);
1637 /* Fills VBI device info */
1638 dev->vbi_dev->type = VFL_TYPE_VBI;
1639 dev->vbi_dev->hardware = 0;
1640 dev->vbi_dev->fops = &em28xx_v4l_fops;
1641 dev->vbi_dev->minor = -1;
1642 dev->vbi_dev->dev = &dev->udev->dev;
1643 dev->vbi_dev->release = video_device_release;
1644 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1645 "em28xx",dev->devno,"vbi");
1647 /* Fills CAPTURE device info */
1648 dev->vdev->type = VID_TYPE_CAPTURE;
1650 dev->vdev->type |= VID_TYPE_TUNER;
1651 dev->vdev->hardware = 0;
1652 dev->vdev->fops = &em28xx_v4l_fops;
1653 dev->vdev->minor = -1;
1654 dev->vdev->dev = &dev->udev->dev;
1655 dev->vdev->release = video_device_release;
1656 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1657 "em28xx",dev->devno,"video");
1659 list_add_tail(&dev->devlist,&em28xx_devlist);
1661 /* register v4l2 device */
1662 mutex_lock(&dev->lock);
1663 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1664 video_nr[dev->devno]))) {
1665 em28xx_errdev("unable to register video device (error=%i).\n",
1667 mutex_unlock(&dev->lock);
1668 list_del(&dev->devlist);
1669 video_device_release(dev->vdev);
1671 em28xx_devused&=~(1<<dev->devno);
1675 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1676 vbi_nr[dev->devno]) < 0) {
1677 printk("unable to register vbi device\n");
1678 mutex_unlock(&dev->lock);
1679 list_del(&dev->devlist);
1680 video_device_release(dev->vbi_dev);
1681 video_device_release(dev->vdev);
1683 em28xx_devused&=~(1<<dev->devno);
1686 printk("registered VBI\n");
1689 if (dev->has_msp34xx) {
1690 /* Send a reset to other chips via gpio */
1691 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1693 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1699 mutex_unlock(&dev->lock);
1701 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1702 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1703 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1709 * em28xx_usb_probe()
1710 * checks for supported devices
1712 static int em28xx_usb_probe(struct usb_interface *interface,
1713 const struct usb_device_id *id)
1715 const struct usb_endpoint_descriptor *endpoint;
1716 struct usb_device *udev;
1717 struct usb_interface *uif;
1718 struct em28xx *dev = NULL;
1719 int retval = -ENODEV;
1720 int model,i,nr,ifnum;
1722 udev = usb_get_dev(interface_to_usbdev(interface));
1723 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1725 /* Check to see next free device and mark as used */
1726 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1727 em28xx_devused|=1<<nr;
1729 /* Don't register audio interfaces */
1730 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1731 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1732 udev->descriptor.idVendor,udev->descriptor.idProduct,
1734 interface->altsetting[0].desc.bInterfaceClass);
1736 em28xx_devused&=~(1<<nr);
1740 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1741 udev->descriptor.idVendor,udev->descriptor.idProduct,
1743 interface->altsetting[0].desc.bInterfaceClass);
1745 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1747 /* check if the the device has the iso in endpoint at the correct place */
1748 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1749 USB_ENDPOINT_XFER_ISOC) {
1750 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1751 em28xx_devused&=~(1<<nr);
1754 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1755 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1756 em28xx_devused&=~(1<<nr);
1760 model=id->driver_info;
1762 if (nr >= EM28XX_MAXBOARDS) {
1763 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1764 em28xx_devused&=~(1<<nr);
1768 /* allocate memory for our device state and initialize it */
1769 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1771 em28xx_err(DRIVER_NAME ": out of memory!\n");
1772 em28xx_devused&=~(1<<nr);
1776 snprintf(dev->name, 29, "em28xx #%d", nr);
1779 /* compute alternate max packet sizes */
1780 uif = udev->actconfig->interface[0];
1782 dev->num_alt=uif->num_altsetting;
1783 em28xx_info("Alternate settings: %i\n",dev->num_alt);
1784 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1785 dev->alt_max_pkt_size = kmalloc(32*
1786 dev->num_alt,GFP_KERNEL);
1787 if (dev->alt_max_pkt_size == NULL) {
1788 em28xx_errdev("out of memory!\n");
1789 em28xx_devused&=~(1<<nr);
1793 for (i = 0; i < dev->num_alt ; i++) {
1794 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1796 dev->alt_max_pkt_size[i] =
1797 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1798 em28xx_info("Alternate setting %i, max size= %i\n",i,
1799 dev->alt_max_pkt_size[i]);
1802 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1805 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1806 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
1807 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1808 "%s: workaround that. Redirect complaints to the vendor of\n"
1809 "%s: the TV card. Generic type will be used."
1810 "%s: Best regards,\n"
1812 dev->name,dev->name,dev->name,dev->name,dev->name);
1813 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1815 for (i = 0; i < em28xx_bcount; i++) {
1816 em28xx_errdev(" card=%d -> %s\n", i,
1817 em28xx_boards[i].name);
1821 /* allocate device struct */
1822 retval = em28xx_init_dev(&dev, udev, nr, model);
1826 em28xx_info("Found %s\n", em28xx_boards[model].name);
1828 /* save our data pointer in this interface device */
1829 usb_set_intfdata(interface, dev);
1834 * em28xx_usb_disconnect()
1835 * called when the device gets diconencted
1836 * video device will be unregistered on v4l2_close in case it is still open
1838 static void em28xx_usb_disconnect(struct usb_interface *interface)
1840 struct em28xx *dev = usb_get_intfdata(interface);
1841 usb_set_intfdata(interface, NULL);
1846 down_write(&em28xx_disconnect);
1848 mutex_lock(&dev->lock);
1850 em28xx_info("disconnecting %s\n", dev->vdev->name);
1852 wake_up_interruptible_all(&dev->open);
1856 ("device /dev/video%d is open! Deregistration and memory "
1857 "deallocation are deferred on close.\n",
1858 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1860 dev->state |= DEV_MISCONFIGURED;
1861 em28xx_uninit_isoc(dev);
1862 dev->state |= DEV_DISCONNECTED;
1863 wake_up_interruptible(&dev->wait_frame);
1864 wake_up_interruptible(&dev->wait_stream);
1866 dev->state |= DEV_DISCONNECTED;
1867 em28xx_release_resources(dev);
1870 mutex_unlock(&dev->lock);
1873 kfree(dev->alt_max_pkt_size);
1877 up_write(&em28xx_disconnect);
1880 static struct usb_driver em28xx_usb_driver = {
1882 .probe = em28xx_usb_probe,
1883 .disconnect = em28xx_usb_disconnect,
1884 .id_table = em28xx_id_table,
1887 static int __init em28xx_module_init(void)
1891 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1892 (EM28XX_VERSION_CODE >> 16) & 0xff,
1893 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1895 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1896 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1899 /* register this driver with the USB subsystem */
1900 result = usb_register(&em28xx_usb_driver);
1902 em28xx_err(DRIVER_NAME
1903 " usb_register failed. Error number %d.\n", result);
1908 static void __exit em28xx_module_exit(void)
1910 /* deregister this driver with the USB subsystem */
1911 usb_deregister(&em28xx_usb_driver);
1914 module_init(em28xx_module_init);
1915 module_exit(em28xx_module_exit);