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@infradead.org>
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>
36 #include <linux/video_decoder.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/msp3400.h>
43 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
45 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
46 "Sascha Sommer <saschasommer@freenet.de>"
48 #define DRIVER_NAME "em28xx"
49 #define DRIVER_DESC "Empia em28xx based USB video device driver"
50 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
52 #define em28xx_videodbg(fmt, arg...) do {\
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __FUNCTION__ , ##arg); } while (0)
57 MODULE_AUTHOR(DRIVER_AUTHOR);
58 MODULE_DESCRIPTION(DRIVER_DESC);
59 MODULE_LICENSE("GPL");
61 static LIST_HEAD(em28xx_devlist);
63 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
66 module_param_array(card, int, NULL, 0444);
67 module_param_array(video_nr, int, NULL, 0444);
68 module_param_array(vbi_nr, int, NULL, 0444);
69 MODULE_PARM_DESC(card,"card type");
70 MODULE_PARM_DESC(video_nr,"video device numbers");
71 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
73 static unsigned int video_debug = 0;
74 module_param(video_debug,int,0644);
75 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
77 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
78 static unsigned long em28xx_devused;
80 /* supported tv norms */
81 static struct em28xx_tvnorm tvnorms[] = {
85 .mode = VIDEO_MODE_PAL,
89 .mode = VIDEO_MODE_NTSC,
93 .mode = VIDEO_MODE_SECAM,
97 .mode = VIDEO_MODE_PAL,
101 #define TVNORMS ARRAY_SIZE(tvnorms)
103 /* supported controls */
104 /* Common to all boards */
105 static struct v4l2_queryctrl em28xx_qctrl[] = {
107 .id = V4L2_CID_AUDIO_VOLUME,
108 .type = V4L2_CTRL_TYPE_INTEGER,
113 .default_value = 0x1f,
116 .id = V4L2_CID_AUDIO_MUTE,
117 .type = V4L2_CTRL_TYPE_BOOLEAN,
127 static struct usb_driver em28xx_usb_driver;
130 /********************* v4l2 interface ******************************************/
134 * inits registers with sane defaults
136 static int em28xx_config(struct em28xx *dev)
139 /* Sets I2C speed to 100 KHz */
141 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
143 /* enable vbi capturing */
145 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
146 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
147 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
149 em28xx_audio_usb_mute(dev, 1);
150 dev->mute = 1; /* maybe not the right place... */
152 em28xx_audio_analog_set(dev);
153 em28xx_audio_analog_setup(dev);
154 em28xx_outfmt_set_yuv422(dev);
155 em28xx_colorlevels_set_default(dev);
156 em28xx_compression_disable(dev);
162 * em28xx_config_i2c()
163 * configure i2c attached devices
165 static void em28xx_config_i2c(struct em28xx *dev)
167 struct v4l2_routing route;
169 route.input = INPUT(dev->ctl_input)->vmux;
171 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
172 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
173 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
177 * em28xx_empty_framequeues()
178 * prepare queues for incoming and outgoing frames
180 static void em28xx_empty_framequeues(struct em28xx *dev)
184 INIT_LIST_HEAD(&dev->inqueue);
185 INIT_LIST_HEAD(&dev->outqueue);
187 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
188 dev->frame[i].state = F_UNUSED;
189 dev->frame[i].buf.bytesused = 0;
193 static void video_mux(struct em28xx *dev, int index)
196 struct v4l2_routing route;
198 route.input = INPUT(index)->vmux;
200 dev->ctl_input = index;
201 dev->ctl_ainput = INPUT(index)->amux;
203 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
205 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput);
207 if (dev->has_msp34xx) {
209 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
210 route.input = dev->ctl_ainput;
211 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
212 /* Note: this is msp3400 specific */
213 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
214 ainput = EM28XX_AUDIO_SRC_TUNER;
215 em28xx_audio_source(dev, ainput);
217 switch (dev->ctl_ainput) {
219 ainput = EM28XX_AUDIO_SRC_TUNER;
222 ainput = EM28XX_AUDIO_SRC_LINE;
224 em28xx_audio_source(dev, ainput);
230 * inits the device and starts isoc transfer
232 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
234 int minor = iminor(inode);
236 struct em28xx *h,*dev = NULL;
238 list_for_each_entry(h, &em28xx_devlist, devlist) {
239 if (h->vdev->minor == minor) {
241 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
243 if (h->vbi_dev->minor == minor) {
245 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
251 em28xx_videodbg("open minor=%d type=%s users=%d\n",
252 minor,v4l2_type_names[dev->type],dev->users);
254 mutex_lock(&dev->lock);
257 em28xx_warn("this driver can be opened only once\n");
258 mutex_unlock(&dev->lock);
262 spin_lock_init(&dev->queue_lock);
263 init_waitqueue_head(&dev->wait_frame);
264 init_waitqueue_head(&dev->wait_stream);
266 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
267 em28xx_set_alternate(dev);
269 dev->width = norm_maxw(dev);
270 dev->height = norm_maxh(dev);
271 dev->frame_size = dev->width * dev->height * 2;
272 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
273 dev->bytesperline = dev->width * 2;
277 em28xx_capture_start(dev, 1);
278 em28xx_resolution_set(dev);
280 /* device needs to be initialized before isoc transfer */
283 /* start the transfer */
284 errCode = em28xx_init_isoc(dev);
291 filp->private_data = dev;
293 dev->stream = STREAM_OFF;
297 em28xx_empty_framequeues(dev);
299 dev->state |= DEV_INITIALIZED;
302 mutex_unlock(&dev->lock);
307 * em28xx_realease_resources()
308 * unregisters the v4l2,i2c and usb devices
309 * called when the device gets disconected or at module unload
311 static void em28xx_release_resources(struct em28xx *dev)
314 /*FIXME: I2C IR should be disconnected */
316 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
317 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
318 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
319 list_del(&dev->devlist);
320 video_unregister_device(dev->vdev);
321 video_unregister_device(dev->vbi_dev);
322 em28xx_i2c_unregister(dev);
323 usb_put_dev(dev->udev);
326 /* Mark device as unused */
327 em28xx_devused&=~(1<<dev->devno);
331 * em28xx_v4l2_close()
332 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
334 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
337 struct em28xx *dev=filp->private_data;
339 em28xx_videodbg("users=%d\n", dev->users);
341 mutex_lock(&dev->lock);
343 em28xx_uninit_isoc(dev);
345 em28xx_release_buffers(dev);
347 /* the device is already disconnect, free the remaining resources */
348 if (dev->state & DEV_DISCONNECTED) {
349 em28xx_release_resources(dev);
350 mutex_unlock(&dev->lock);
355 /* set alternate 0 */
357 em28xx_videodbg("setting alternate 0\n");
358 errCode = usb_set_interface(dev->udev, 0, 0);
360 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
365 wake_up_interruptible_nr(&dev->open, 1);
366 mutex_unlock(&dev->lock);
372 * will allocate buffers when called for the first time
375 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
378 struct em28xx_frame_t *f, *i;
379 unsigned long lock_flags;
381 struct em28xx *dev = filp->private_data;
383 mutex_lock(&dev->lock);
385 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
386 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
388 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
389 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
390 em28xx_videodbg("not supported yet! ...\n");
391 if (copy_to_user(buf, "", 1)) {
392 mutex_unlock(&dev->lock);
395 mutex_unlock(&dev->lock);
398 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
399 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
400 em28xx_videodbg("not supported yet! ...\n");
401 if (copy_to_user(buf, "", 1)) {
402 mutex_unlock(&dev->lock);
405 mutex_unlock(&dev->lock);
409 if (dev->state & DEV_DISCONNECTED) {
410 em28xx_videodbg("device not present\n");
411 mutex_unlock(&dev->lock);
415 if (dev->state & DEV_MISCONFIGURED) {
416 em28xx_videodbg("device misconfigured; close and open it again\n");
417 mutex_unlock(&dev->lock);
421 if (dev->io == IO_MMAP) {
422 em28xx_videodbg ("IO method is set to mmap; close and open"
423 " the device again to choose the read method\n");
424 mutex_unlock(&dev->lock);
428 if (dev->io == IO_NONE) {
429 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
430 em28xx_errdev("read failed, not enough memory\n");
431 mutex_unlock(&dev->lock);
435 dev->stream = STREAM_ON;
436 em28xx_queue_unusedframes(dev);
440 mutex_unlock(&dev->lock);
444 if (list_empty(&dev->outqueue)) {
445 if (filp->f_flags & O_NONBLOCK) {
446 mutex_unlock(&dev->lock);
449 ret = wait_event_interruptible
451 (!list_empty(&dev->outqueue)) ||
452 (dev->state & DEV_DISCONNECTED));
454 mutex_unlock(&dev->lock);
457 if (dev->state & DEV_DISCONNECTED) {
458 mutex_unlock(&dev->lock);
463 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
465 spin_lock_irqsave(&dev->queue_lock, lock_flags);
466 list_for_each_entry(i, &dev->outqueue, frame)
468 INIT_LIST_HEAD(&dev->outqueue);
469 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
471 em28xx_queue_unusedframes(dev);
473 if (count > f->buf.length)
474 count = f->buf.length;
476 if (copy_to_user(buf, f->bufmem, count)) {
477 mutex_unlock(&dev->lock);
482 mutex_unlock(&dev->lock);
489 * will allocate buffers when called for the first time
491 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
493 unsigned int mask = 0;
494 struct em28xx *dev = filp->private_data;
496 mutex_lock(&dev->lock);
498 if (dev->state & DEV_DISCONNECTED) {
499 em28xx_videodbg("device not present\n");
500 } else if (dev->state & DEV_MISCONFIGURED) {
501 em28xx_videodbg("device is misconfigured; close and open it again\n");
503 if (dev->io == IO_NONE) {
504 if (!em28xx_request_buffers
505 (dev, EM28XX_NUM_READ_FRAMES)) {
507 ("poll() failed, not enough memory\n");
510 dev->stream = STREAM_ON;
514 if (dev->io == IO_READ) {
515 em28xx_queue_unusedframes(dev);
516 poll_wait(filp, &dev->wait_frame, wait);
518 if (!list_empty(&dev->outqueue))
519 mask |= POLLIN | POLLRDNORM;
521 mutex_unlock(&dev->lock);
527 mutex_unlock(&dev->lock);
534 static void em28xx_vm_open(struct vm_area_struct *vma)
536 struct em28xx_frame_t *f = vma->vm_private_data;
543 static void em28xx_vm_close(struct vm_area_struct *vma)
545 /* NOTE: buffers are not freed here */
546 struct em28xx_frame_t *f = vma->vm_private_data;
548 if (f->vma_use_count)
552 static struct vm_operations_struct em28xx_vm_ops = {
553 .open = em28xx_vm_open,
554 .close = em28xx_vm_close,
560 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
562 unsigned long size = vma->vm_end - vma->vm_start,
563 start = vma->vm_start;
567 struct em28xx *dev = filp->private_data;
569 mutex_lock(&dev->lock);
571 if (dev->state & DEV_DISCONNECTED) {
572 em28xx_videodbg("mmap: device not present\n");
573 mutex_unlock(&dev->lock);
577 if (dev->state & DEV_MISCONFIGURED) {
578 em28xx_videodbg ("mmap: Device is misconfigured; close and "
580 mutex_unlock(&dev->lock);
584 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
585 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
586 mutex_unlock(&dev->lock);
590 for (i = 0; i < dev->num_frames; i++) {
591 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
594 if (i == dev->num_frames) {
595 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
596 mutex_unlock(&dev->lock);
600 /* VM_IO is eventually going to replace PageReserved altogether */
601 vma->vm_flags |= VM_IO;
602 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
604 pos = dev->frame[i].bufmem;
605 while (size > 0) { /* size is page-aligned */
606 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
607 em28xx_videodbg("mmap: vm_insert_page failed\n");
608 mutex_unlock(&dev->lock);
616 vma->vm_ops = &em28xx_vm_ops;
617 vma->vm_private_data = &dev->frame[i];
620 mutex_unlock(&dev->lock);
626 * return the current saturation, brightness or contrast, mute state
628 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
631 case V4L2_CID_AUDIO_MUTE:
632 ctrl->value = dev->mute;
634 case V4L2_CID_AUDIO_VOLUME:
635 ctrl->value = dev->volume;
644 * mute or set new saturation, brightness or contrast
646 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
649 case V4L2_CID_AUDIO_MUTE:
650 if (ctrl->value != dev->mute) {
651 dev->mute = ctrl->value;
652 em28xx_audio_usb_mute(dev, ctrl->value);
653 return em28xx_audio_analog_set(dev);
656 case V4L2_CID_AUDIO_VOLUME:
657 dev->volume = ctrl->value;
658 return em28xx_audio_analog_set(dev);
665 * em28xx_stream_interrupt()
668 static int em28xx_stream_interrupt(struct em28xx *dev)
672 /* stop reading from the device */
674 dev->stream = STREAM_INTERRUPT;
675 ret = wait_event_timeout(dev->wait_stream,
676 (dev->stream == STREAM_OFF) ||
677 (dev->state & DEV_DISCONNECTED),
679 if (dev->state & DEV_DISCONNECTED)
682 dev->state |= DEV_MISCONFIGURED;
683 em28xx_videodbg("device is misconfigured; close and "
684 "open /dev/video%d again\n",
685 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
692 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
694 unsigned int hscale, vscale;
695 unsigned int maxh, maxw;
697 maxw = norm_maxw(dev);
698 maxh = norm_maxh(dev);
700 /* width must even because of the YUYV format */
701 /* height must be even because of interlacing */
714 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
716 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
718 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
720 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
722 /* set new image size */
724 dev->height = height;
725 dev->frame_size = dev->width * dev->height * 2;
726 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
727 dev->bytesperline = dev->width * 2;
728 dev->hscale = hscale;
729 dev->vscale = vscale;
731 em28xx_resolution_set(dev);
736 static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
738 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
739 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
740 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
741 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
742 "V4L2_BUF_TYPE_VBI_CAPTURE" :
743 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
744 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
747 switch (format->type) {
748 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
750 format->fmt.pix.width = dev->width;
751 format->fmt.pix.height = dev->height;
752 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
753 format->fmt.pix.bytesperline = dev->bytesperline;
754 format->fmt.pix.sizeimage = dev->frame_size;
755 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
756 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
758 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
763 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
765 format->fmt.sliced.service_set=0;
767 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
769 if (format->fmt.sliced.service_set==0)
781 static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
785 int width = format->fmt.pix.width;
786 int height = format->fmt.pix.height;
787 unsigned int hscale, vscale;
788 unsigned int maxh, maxw;
790 maxw = norm_maxw(dev);
791 maxh = norm_maxh(dev);
793 em28xx_videodbg("%s: type=%s\n",
794 cmd == VIDIOC_TRY_FMT ?
795 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
796 format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
797 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
798 format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
799 "V4L2_BUF_TYPE_VBI_CAPTURE " :
802 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
803 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
805 if (format->fmt.sliced.service_set==0)
812 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
815 em28xx_videodbg("%s: requested %dx%d\n",
816 cmd == VIDIOC_TRY_FMT ?
817 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
818 format->fmt.pix.width, format->fmt.pix.height);
820 /* FIXME: Move some code away from here */
821 /* width must even because of the YUYV format */
822 /* height must be even because of interlacing */
836 /* the em2800 can only scale down to 50% */
837 if(height % (maxh / 2))
839 if(width % (maxw / 2))
841 /* according to empiatech support */
842 /* the MaxPacketSize is to small to support */
843 /* framesizes larger than 640x480 @ 30 fps */
844 /* or 640x576 @ 25 fps. As this would cut */
845 /* of a part of the image we prefer */
846 /* 360x576 or 360x480 for now */
847 if(width == maxw && height == maxh)
851 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
854 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
856 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
859 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
861 format->fmt.pix.width = width;
862 format->fmt.pix.height = height;
863 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
864 format->fmt.pix.bytesperline = width * 2;
865 format->fmt.pix.sizeimage = width * 2 * height;
866 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
867 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
869 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
870 cmd == VIDIOC_TRY_FMT ?
871 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
872 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
874 if (cmd == VIDIOC_TRY_FMT)
877 for (i = 0; i < dev->num_frames; i++)
878 if (dev->frame[i].vma_use_count) {
879 em28xx_videodbg("VIDIOC_S_FMT failed. "
880 "Unmap the buffers first.\n");
884 /* stop io in case it is already in progress */
885 if (dev->stream == STREAM_ON) {
886 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
887 if ((ret = em28xx_stream_interrupt(dev)))
891 em28xx_release_buffers(dev);
894 /* set new image size */
896 dev->height = height;
897 dev->frame_size = dev->width * dev->height * 2;
898 dev->field_size = dev->frame_size >> 1;
899 dev->bytesperline = dev->width * 2;
900 dev->hscale = hscale;
901 dev->vscale = vscale;
902 em28xx_uninit_isoc(dev);
903 em28xx_set_alternate(dev);
904 em28xx_capture_start(dev, 1);
905 em28xx_resolution_set(dev);
906 em28xx_init_isoc(dev);
912 * em28xx_v4l2_do_ioctl()
913 * This function is _not_ called directly, but from
914 * em28xx_v4l2_ioctl. Userspace
915 * copying is done already, arg is a kernel pointer.
917 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
918 struct em28xx *dev, unsigned int cmd, void *arg,
919 v4l2_kioctl driver_ioctl)
924 /* ---------- tv norms ---------- */
927 struct v4l2_standard *e = arg;
933 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
934 tvnorms[e->index].name);
942 v4l2_std_id *id = arg;
944 *id = dev->tvnorm->id;
949 v4l2_std_id *id = arg;
952 for (i = 0; i < TVNORMS; i++)
953 if (*id == tvnorms[i].id)
956 for (i = 0; i < TVNORMS; i++)
957 if (*id & tvnorms[i].id)
962 mutex_lock(&dev->lock);
963 dev->tvnorm = &tvnorms[i];
965 em28xx_set_norm(dev, dev->width, dev->height);
967 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
970 mutex_unlock(&dev->lock);
975 /* ------ input switching ---------- */
976 case VIDIOC_ENUMINPUT:
978 struct v4l2_input *i = arg;
980 static const char *iname[] = {
981 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
982 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
983 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
984 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
985 [EM28XX_VMUX_SVIDEO] = "S-Video",
986 [EM28XX_VMUX_TELEVISION] = "Television",
987 [EM28XX_VMUX_CABLE] = "Cable TV",
988 [EM28XX_VMUX_DVB] = "DVB",
989 [EM28XX_VMUX_DEBUG] = "for debug only",
993 if (n >= MAX_EM28XX_INPUT)
995 if (0 == INPUT(n)->type)
997 memset(i, 0, sizeof(*i));
999 i->type = V4L2_INPUT_TYPE_CAMERA;
1000 strcpy(i->name, iname[INPUT(n)->type]);
1001 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1002 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1003 i->type = V4L2_INPUT_TYPE_TUNER;
1004 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1005 i->std |= tvnorms[n].id;
1008 case VIDIOC_G_INPUT:
1011 *i = dev->ctl_input;
1015 case VIDIOC_S_INPUT:
1019 if (*index >= MAX_EM28XX_INPUT)
1021 if (0 == INPUT(*index)->type)
1024 mutex_lock(&dev->lock);
1025 video_mux(dev, *index);
1026 mutex_unlock(&dev->lock);
1030 case VIDIOC_G_AUDIO:
1032 struct v4l2_audio *a = arg;
1033 unsigned int index = a->index;
1037 memset(a, 0, sizeof(*a));
1038 index = dev->ctl_ainput;
1041 strcpy(a->name, "Television");
1043 strcpy(a->name, "Line In");
1045 a->capability = V4L2_AUDCAP_STEREO;
1049 case VIDIOC_S_AUDIO:
1051 struct v4l2_audio *a = arg;
1053 if (a->index != dev->ctl_ainput)
1059 /* --- controls ---------------------------------------------- */
1060 case VIDIOC_QUERYCTRL:
1062 struct v4l2_queryctrl *qc = arg;
1065 memset(qc,0,sizeof(*qc));
1068 if (!dev->has_msp34xx) {
1069 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1070 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1071 memcpy(qc, &(em28xx_qctrl[i]),
1077 mutex_lock(&dev->lock);
1078 em28xx_i2c_call_clients(dev,cmd,qc);
1079 mutex_unlock(&dev->lock);
1087 struct v4l2_control *ctrl = arg;
1090 if (!dev->has_msp34xx)
1091 retval=em28xx_get_ctrl(dev, ctrl);
1092 if (retval==-EINVAL) {
1093 mutex_lock(&dev->lock);
1094 em28xx_i2c_call_clients(dev,cmd,arg);
1095 mutex_unlock(&dev->lock);
1097 } else return retval;
1101 struct v4l2_control *ctrl = arg;
1103 mutex_lock(&dev->lock);
1105 if (!dev->has_msp34xx){
1106 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1107 if (ctrl->id == em28xx_qctrl[i].id) {
1110 em28xx_qctrl[i].minimum
1112 em28xx_qctrl[i].maximum)
1114 retval = em28xx_set_ctrl(dev, ctrl);
1115 mutex_unlock(&dev->lock);
1121 em28xx_i2c_call_clients(dev,cmd,arg);
1122 mutex_unlock(&dev->lock);
1125 /* --- tuner ioctls ------------------------------------------ */
1126 case VIDIOC_G_TUNER:
1128 struct v4l2_tuner *t = arg;
1133 memset(t, 0, sizeof(*t));
1134 strcpy(t->name, "Tuner");
1135 mutex_lock(&dev->lock);
1136 /* let clients fill in the remainder of this struct */
1137 em28xx_i2c_call_clients(dev, cmd, t);
1138 mutex_unlock(&dev->lock);
1139 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1143 case VIDIOC_S_TUNER:
1145 struct v4l2_tuner *t = arg;
1149 mutex_lock(&dev->lock);
1150 /* let clients handle this */
1151 em28xx_i2c_call_clients(dev, cmd, t);
1152 mutex_unlock(&dev->lock);
1155 case VIDIOC_G_FREQUENCY:
1157 struct v4l2_frequency *f = arg;
1159 memset(f, 0, sizeof(*f));
1160 f->type = V4L2_TUNER_ANALOG_TV;
1161 f->frequency = dev->ctl_freq;
1165 case VIDIOC_S_FREQUENCY:
1167 struct v4l2_frequency *f = arg;
1172 if (V4L2_TUNER_ANALOG_TV != f->type)
1175 mutex_lock(&dev->lock);
1176 dev->ctl_freq = f->frequency;
1177 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1178 mutex_unlock(&dev->lock);
1181 case VIDIOC_CROPCAP:
1183 struct v4l2_cropcap *cc = arg;
1185 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1187 cc->bounds.left = 0;
1189 cc->bounds.width = dev->width;
1190 cc->bounds.height = dev->height;
1191 cc->defrect = cc->bounds;
1192 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1193 cc->pixelaspect.denominator = 59;
1196 case VIDIOC_STREAMON:
1200 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1201 || dev->io != IO_MMAP)
1204 if (list_empty(&dev->inqueue))
1207 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1209 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1213 case VIDIOC_STREAMOFF:
1218 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1219 || dev->io != IO_MMAP)
1222 mutex_lock(&dev->lock);
1223 if (dev->stream == STREAM_ON) {
1224 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1225 if ((ret = em28xx_stream_interrupt(dev))){
1226 mutex_unlock(&dev->lock);
1230 em28xx_empty_framequeues(dev);
1231 mutex_unlock(&dev->lock);
1236 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1243 * em28xx_v4l2_do_ioctl()
1244 * This function is _not_ called directly, but from
1245 * em28xx_v4l2_ioctl. Userspace
1246 * copying is done already, arg is a kernel pointer.
1248 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1249 unsigned int cmd, void *arg)
1251 struct em28xx *dev = filp->private_data;
1256 if (video_debug > 1)
1257 v4l_print_ioctl(dev->name,cmd);
1261 /* --- capabilities ------------------------------------------ */
1262 case VIDIOC_QUERYCAP:
1264 struct v4l2_capability *cap = arg;
1266 memset(cap, 0, sizeof(*cap));
1267 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1268 strlcpy(cap->card, em28xx_boards[dev->model].name,
1270 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1271 sizeof(cap->bus_info));
1272 cap->version = EM28XX_VERSION_CODE;
1274 V4L2_CAP_SLICED_VBI_CAPTURE |
1275 V4L2_CAP_VIDEO_CAPTURE |
1277 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1279 cap->capabilities |= V4L2_CAP_TUNER;
1282 /* --- capture ioctls ---------------------------------------- */
1283 case VIDIOC_ENUM_FMT:
1285 struct v4l2_fmtdesc *fmtd = arg;
1287 if (fmtd->index != 0)
1289 memset(fmtd, 0, sizeof(*fmtd));
1290 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1291 strcpy(fmtd->description, "Packed YUY2");
1292 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1293 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1299 mutex_lock(&dev->lock);
1300 retval = em28xx_get_fmt(dev, (struct v4l2_format *) arg);
1301 mutex_unlock(&dev->lock);
1305 case VIDIOC_TRY_FMT:
1309 mutex_lock(&dev->lock);
1310 retval = em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
1311 mutex_unlock(&dev->lock);
1315 case VIDIOC_REQBUFS:
1317 struct v4l2_requestbuffers *rb = arg;
1321 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1322 rb->memory != V4L2_MEMORY_MMAP)
1325 if (dev->io == IO_READ) {
1326 em28xx_videodbg ("method is set to read;"
1327 " close and open the device again to"
1328 " choose the mmap I/O method\n");
1332 for (i = 0; i < dev->num_frames; i++)
1333 if (dev->frame[i].vma_use_count) {
1334 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1338 mutex_lock(&dev->lock);
1339 if (dev->stream == STREAM_ON) {
1340 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1341 if ((ret = em28xx_stream_interrupt(dev))){
1342 mutex_unlock(&dev->lock);
1347 em28xx_empty_framequeues(dev);
1349 em28xx_release_buffers(dev);
1352 em28xx_request_buffers(dev, rb->count);
1354 dev->frame_current = NULL;
1356 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1358 dev->io = rb->count ? IO_MMAP : IO_NONE;
1359 mutex_unlock(&dev->lock);
1362 case VIDIOC_QUERYBUF:
1364 struct v4l2_buffer *b = arg;
1366 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1367 b->index >= dev->num_frames || dev->io != IO_MMAP)
1370 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1372 if (dev->frame[b->index].vma_use_count) {
1373 b->flags |= V4L2_BUF_FLAG_MAPPED;
1375 if (dev->frame[b->index].state == F_DONE)
1376 b->flags |= V4L2_BUF_FLAG_DONE;
1377 else if (dev->frame[b->index].state != F_UNUSED)
1378 b->flags |= V4L2_BUF_FLAG_QUEUED;
1383 struct v4l2_buffer *b = arg;
1384 unsigned long lock_flags;
1386 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1387 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1391 if (dev->frame[b->index].state != F_UNUSED) {
1394 dev->frame[b->index].state = F_QUEUED;
1396 /* add frame to fifo */
1397 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1398 list_add_tail(&dev->frame[b->index].frame,
1400 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1406 struct v4l2_buffer *b = arg;
1407 struct em28xx_frame_t *f;
1408 unsigned long lock_flags;
1411 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1412 || dev->io != IO_MMAP)
1415 if (list_empty(&dev->outqueue)) {
1416 if (dev->stream == STREAM_OFF)
1418 if (filp->f_flags & O_NONBLOCK)
1420 ret = wait_event_interruptible
1422 (!list_empty(&dev->outqueue)) ||
1423 (dev->state & DEV_DISCONNECTED));
1426 if (dev->state & DEV_DISCONNECTED)
1430 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1431 f = list_entry(dev->outqueue.next,
1432 struct em28xx_frame_t, frame);
1433 list_del(dev->outqueue.next);
1434 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1436 f->state = F_UNUSED;
1437 memcpy(b, &f->buf, sizeof(*b));
1439 if (f->vma_use_count)
1440 b->flags |= V4L2_BUF_FLAG_MAPPED;
1445 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1446 em28xx_video_do_ioctl);
1452 * em28xx_v4l2_ioctl()
1453 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1455 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1456 unsigned int cmd, unsigned long arg)
1459 struct em28xx *dev = filp->private_data;
1461 if (dev->state & DEV_DISCONNECTED) {
1462 em28xx_errdev("v4l2 ioctl: device not present\n");
1466 if (dev->state & DEV_MISCONFIGURED) {
1468 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1472 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1477 static const struct file_operations em28xx_v4l_fops = {
1478 .owner = THIS_MODULE,
1479 .open = em28xx_v4l2_open,
1480 .release = em28xx_v4l2_close,
1481 .ioctl = em28xx_v4l2_ioctl,
1482 .read = em28xx_v4l2_read,
1483 .poll = em28xx_v4l2_poll,
1484 .mmap = em28xx_v4l2_mmap,
1485 .llseek = no_llseek,
1486 .compat_ioctl = v4l_compat_ioctl32,
1490 /******************************** usb interface *****************************************/
1494 * allocates and inits the device structs, registers i2c bus and v4l device
1496 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1499 struct em28xx *dev = *devhandle;
1500 int retval = -ENOMEM;
1502 unsigned int maxh, maxw;
1505 mutex_init(&dev->lock);
1506 init_waitqueue_head(&dev->open);
1508 dev->em28xx_write_regs = em28xx_write_regs;
1509 dev->em28xx_read_reg = em28xx_read_reg;
1510 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1511 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1512 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1514 /* setup video picture settings for saa7113h */
1515 memset(&dev->vpic, 0, sizeof(dev->vpic));
1516 dev->vpic.colour = 128 << 8;
1517 dev->vpic.hue = 128 << 8;
1518 dev->vpic.brightness = 128 << 8;
1519 dev->vpic.contrast = 192 << 8;
1520 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1521 dev->vpic.depth = 16;
1522 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1524 em28xx_pre_card_setup(dev);
1526 errCode = em28xx_config(dev);
1528 em28xx_errdev("error configuring device\n");
1529 em28xx_devused &= ~(1<<dev->devno);
1534 /* register i2c bus */
1535 em28xx_i2c_register(dev);
1537 /* Do board specific init and eeprom reading */
1538 em28xx_card_setup(dev);
1540 /* configure the device */
1541 em28xx_config_i2c(dev);
1543 for (i = 0; i < TVNORMS; i++)
1544 if (em28xx_boards[dev->model].norm == tvnorms[i].mode)
1549 dev->tvnorm = &tvnorms[i]; /* set default norm */
1551 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1553 maxw = norm_maxw(dev);
1554 maxh = norm_maxh(dev);
1556 /* set default image size */
1559 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1560 dev->field_size = dev->width * dev->height;
1562 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1563 dev->bytesperline = dev->width * 2;
1568 errCode = em28xx_config(dev);
1570 /* allocate and fill v4l2 device struct */
1571 dev->vdev = video_device_alloc();
1572 if (NULL == dev->vdev) {
1573 em28xx_errdev("cannot allocate video_device.\n");
1574 em28xx_devused&=~(1<<dev->devno);
1579 dev->vbi_dev = video_device_alloc();
1580 if (NULL == dev->vbi_dev) {
1581 em28xx_errdev("cannot allocate video_device.\n");
1583 em28xx_devused&=~(1<<dev->devno);
1588 /* Fills VBI device info */
1589 dev->vbi_dev->type = VFL_TYPE_VBI;
1590 dev->vbi_dev->fops = &em28xx_v4l_fops;
1591 dev->vbi_dev->minor = -1;
1592 dev->vbi_dev->dev = &dev->udev->dev;
1593 dev->vbi_dev->release = video_device_release;
1594 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1595 "em28xx",dev->devno,"vbi");
1597 /* Fills CAPTURE device info */
1598 dev->vdev->type = VID_TYPE_CAPTURE;
1600 dev->vdev->type |= VID_TYPE_TUNER;
1601 dev->vdev->fops = &em28xx_v4l_fops;
1602 dev->vdev->minor = -1;
1603 dev->vdev->dev = &dev->udev->dev;
1604 dev->vdev->release = video_device_release;
1605 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1606 "em28xx",dev->devno,"video");
1608 list_add_tail(&dev->devlist,&em28xx_devlist);
1611 if (dev->has_msp34xx) {
1612 /* Send a reset to other chips via gpio */
1613 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1615 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1621 /* register v4l2 device */
1622 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1623 video_nr[dev->devno]))) {
1624 em28xx_errdev("unable to register video device (error=%i).\n",
1626 mutex_unlock(&dev->lock);
1627 list_del(&dev->devlist);
1628 video_device_release(dev->vdev);
1629 em28xx_devused&=~(1<<dev->devno);
1634 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1635 vbi_nr[dev->devno]) < 0) {
1636 printk("unable to register vbi device\n");
1637 mutex_unlock(&dev->lock);
1638 list_del(&dev->devlist);
1639 video_device_release(dev->vbi_dev);
1640 video_device_release(dev->vdev);
1641 em28xx_devused&=~(1<<dev->devno);
1645 printk("registered VBI\n");
1648 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1649 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1650 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1656 * em28xx_usb_probe()
1657 * checks for supported devices
1659 static int em28xx_usb_probe(struct usb_interface *interface,
1660 const struct usb_device_id *id)
1662 const struct usb_endpoint_descriptor *endpoint;
1663 struct usb_device *udev;
1664 struct usb_interface *uif;
1665 struct em28xx *dev = NULL;
1666 int retval = -ENODEV;
1669 udev = usb_get_dev(interface_to_usbdev(interface));
1670 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1672 /* Check to see next free device and mark as used */
1673 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1674 em28xx_devused|=1<<nr;
1676 /* Don't register audio interfaces */
1677 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1678 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1679 udev->descriptor.idVendor,udev->descriptor.idProduct,
1681 interface->altsetting[0].desc.bInterfaceClass);
1683 em28xx_devused&=~(1<<nr);
1687 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1688 udev->descriptor.idVendor,udev->descriptor.idProduct,
1690 interface->altsetting[0].desc.bInterfaceClass);
1692 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1694 /* check if the device has the iso in endpoint at the correct place */
1695 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1696 USB_ENDPOINT_XFER_ISOC) {
1697 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1698 em28xx_devused&=~(1<<nr);
1701 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1702 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1703 em28xx_devused&=~(1<<nr);
1707 if (nr >= EM28XX_MAXBOARDS) {
1708 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1709 em28xx_devused&=~(1<<nr);
1713 /* allocate memory for our device state and initialize it */
1714 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1716 em28xx_err(DRIVER_NAME ": out of memory!\n");
1717 em28xx_devused&=~(1<<nr);
1721 snprintf(dev->name, 29, "em28xx #%d", nr);
1723 dev->model = id->driver_info;
1725 /* compute alternate max packet sizes */
1726 uif = udev->actconfig->interface[0];
1728 dev->num_alt=uif->num_altsetting;
1729 em28xx_info("Alternate settings: %i\n",dev->num_alt);
1730 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1731 dev->alt_max_pkt_size = kmalloc(32*
1732 dev->num_alt,GFP_KERNEL);
1733 if (dev->alt_max_pkt_size == NULL) {
1734 em28xx_errdev("out of memory!\n");
1735 em28xx_devused&=~(1<<nr);
1740 for (i = 0; i < dev->num_alt ; i++) {
1741 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1743 dev->alt_max_pkt_size[i] =
1744 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1745 em28xx_info("Alternate setting %i, max size= %i\n",i,
1746 dev->alt_max_pkt_size[i]);
1749 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1750 dev->model = card[nr];
1752 /* allocate device struct */
1753 retval = em28xx_init_dev(&dev, udev, nr);
1757 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
1759 /* save our data pointer in this interface device */
1760 usb_set_intfdata(interface, dev);
1765 * em28xx_usb_disconnect()
1766 * called when the device gets diconencted
1767 * video device will be unregistered on v4l2_close in case it is still open
1769 static void em28xx_usb_disconnect(struct usb_interface *interface)
1773 dev = usb_get_intfdata(interface);
1774 usb_set_intfdata(interface, NULL);
1779 em28xx_info("disconnecting %s\n", dev->vdev->name);
1781 /* wait until all current v4l2 io is finished then deallocate resources */
1782 mutex_lock(&dev->lock);
1784 wake_up_interruptible_all(&dev->open);
1788 ("device /dev/video%d is open! Deregistration and memory "
1789 "deallocation are deferred on close.\n",
1790 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1792 dev->state |= DEV_MISCONFIGURED;
1793 em28xx_uninit_isoc(dev);
1794 dev->state |= DEV_DISCONNECTED;
1795 wake_up_interruptible(&dev->wait_frame);
1796 wake_up_interruptible(&dev->wait_stream);
1798 dev->state |= DEV_DISCONNECTED;
1799 em28xx_release_resources(dev);
1803 mutex_unlock(&dev->lock);
1806 kfree(dev->alt_max_pkt_size);
1812 static struct usb_driver em28xx_usb_driver = {
1814 .probe = em28xx_usb_probe,
1815 .disconnect = em28xx_usb_disconnect,
1816 .id_table = em28xx_id_table,
1819 static int __init em28xx_module_init(void)
1823 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1824 (EM28XX_VERSION_CODE >> 16) & 0xff,
1825 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1827 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1828 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1831 /* register this driver with the USB subsystem */
1832 result = usb_register(&em28xx_usb_driver);
1834 em28xx_err(DRIVER_NAME
1835 " usb_register failed. Error number %d.\n", result);
1840 static void __exit em28xx_module_exit(void)
1842 /* deregister this driver with the USB subsystem */
1843 usb_deregister(&em28xx_usb_driver);
1846 module_init(em28xx_module_init);
1847 module_exit(em28xx_module_exit);