📄 em28xx-video.c
字号:
/* em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> Markus Rechberger <mrechberger@gmail.com> Mauro Carvalho Chehab <mchehab@infradead.org> Sascha Sommer <saschasommer@freenet.de> Some parts based on SN9C10x PC Camera Controllers GPL driver made by Luca Risolia <luca.risolia@studio.unibo.it> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/init.h>#include <linux/list.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/bitmap.h>#include <linux/usb.h>#include <linux/i2c.h>#include <linux/version.h>#include <linux/mm.h>#include <linux/video_decoder.h>#include <linux/mutex.h>#include "em28xx.h"#include <media/tuner.h>#include <media/v4l2-common.h>#include <media/msp3400.h>#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ "Markus Rechberger <mrechberger@gmail.com>, " \ "Mauro Carvalho Chehab <mchehab@infradead.org>, " \ "Sascha Sommer <saschasommer@freenet.de>"#define DRIVER_NAME "em28xx"#define DRIVER_DESC "Empia em28xx based USB video device driver"#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)#define em28xx_videodbg(fmt, arg...) do {\ if (video_debug) \ printk(KERN_INFO "%s %s :"fmt, \ dev->name, __FUNCTION__ , ##arg); } while (0)MODULE_AUTHOR(DRIVER_AUTHOR);MODULE_DESCRIPTION(DRIVER_DESC);MODULE_LICENSE("GPL");static LIST_HEAD(em28xx_devlist);static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };module_param_array(card, int, NULL, 0444);module_param_array(video_nr, int, NULL, 0444);module_param_array(vbi_nr, int, NULL, 0444);MODULE_PARM_DESC(card,"card type");MODULE_PARM_DESC(video_nr,"video device numbers");MODULE_PARM_DESC(vbi_nr,"vbi device numbers");static int tuner = -1;module_param(tuner, int, 0444);MODULE_PARM_DESC(tuner, "tuner type");static unsigned int video_debug = 0;module_param(video_debug,int,0644);MODULE_PARM_DESC(video_debug,"enable debug messages [video]");/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */static unsigned long em28xx_devused;/* supported tv norms */static struct em28xx_tvnorm tvnorms[] = { { .name = "PAL", .id = V4L2_STD_PAL, .mode = VIDEO_MODE_PAL, }, { .name = "NTSC", .id = V4L2_STD_NTSC, .mode = VIDEO_MODE_NTSC, }, { .name = "SECAM", .id = V4L2_STD_SECAM, .mode = VIDEO_MODE_SECAM, }, { .name = "PAL-M", .id = V4L2_STD_PAL_M, .mode = VIDEO_MODE_PAL, }};#define TVNORMS ARRAY_SIZE(tvnorms)/* supported controls *//* Common to all boards */static struct v4l2_queryctrl em28xx_qctrl[] = { { .id = V4L2_CID_AUDIO_VOLUME, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Volume", .minimum = 0x0, .maximum = 0x1f, .step = 0x1, .default_value = 0x1f, .flags = 0, },{ .id = V4L2_CID_AUDIO_MUTE, .type = V4L2_CTRL_TYPE_BOOLEAN, .name = "Mute", .minimum = 0, .maximum = 1, .step = 1, .default_value = 1, .flags = 0, }};static struct usb_driver em28xx_usb_driver;static DEFINE_MUTEX(em28xx_sysfs_lock);static DECLARE_RWSEM(em28xx_disconnect);/********************* v4l2 interface ******************************************//* * em28xx_config() * inits registers with sane defaults */static int em28xx_config(struct em28xx *dev){ /* Sets I2C speed to 100 KHz */ if (!dev->is_em2800) em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1); /* enable vbi capturing *//* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register *//* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */ em28xx_write_regs_req(dev,0x00,0x11,"\x51",1); em28xx_audio_usb_mute(dev, 1); dev->mute = 1; /* maybe not the right place... */ dev->volume = 0x1f; em28xx_audio_analog_set(dev); em28xx_audio_analog_setup(dev); em28xx_outfmt_set_yuv422(dev); em28xx_colorlevels_set_default(dev); em28xx_compression_disable(dev); return 0;}/* * em28xx_config_i2c() * configure i2c attached devices */static void em28xx_config_i2c(struct em28xx *dev){ struct v4l2_frequency f; struct v4l2_routing route; route.input = INPUT(dev->ctl_input)->vmux; route.output = 0; em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL); em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); /* configure tuner */ f.tuner = 0; f.type = V4L2_TUNER_ANALOG_TV; f.frequency = 9076; /* FIXME:remove magic number */ dev->ctl_freq = f.frequency; em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f); /* configure tda9887 *//* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */}/* * em28xx_empty_framequeues() * prepare queues for incoming and outgoing frames */static void em28xx_empty_framequeues(struct em28xx *dev){ u32 i; INIT_LIST_HEAD(&dev->inqueue); INIT_LIST_HEAD(&dev->outqueue); for (i = 0; i < EM28XX_NUM_FRAMES; i++) { dev->frame[i].state = F_UNUSED; dev->frame[i].buf.bytesused = 0; }}static void video_mux(struct em28xx *dev, int index){ int ainput; struct v4l2_routing route; route.input = INPUT(index)->vmux; route.output = 0; dev->ctl_input = index; dev->ctl_ainput = INPUT(index)->amux; em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput); if (dev->has_msp34xx) { if (dev->i2s_speed) em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed); route.input = dev->ctl_ainput; route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); /* Note: this is msp3400 specific */ em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route); ainput = EM28XX_AUDIO_SRC_TUNER; em28xx_audio_source(dev, ainput); } else { switch (dev->ctl_ainput) { case 0: ainput = EM28XX_AUDIO_SRC_TUNER; break; default: ainput = EM28XX_AUDIO_SRC_LINE; } em28xx_audio_source(dev, ainput); }}/* * em28xx_v4l2_open() * inits the device and starts isoc transfer */static int em28xx_v4l2_open(struct inode *inode, struct file *filp){ int minor = iminor(inode); int errCode = 0; struct em28xx *h,*dev = NULL; list_for_each_entry(h, &em28xx_devlist, devlist) { if (h->vdev->minor == minor) { dev = h; dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; } if (h->vbi_dev->minor == minor) { dev = h; dev->type = V4L2_BUF_TYPE_VBI_CAPTURE; } } if (NULL == dev) return -ENODEV; em28xx_videodbg("open minor=%d type=%s users=%d\n", minor,v4l2_type_names[dev->type],dev->users); if (!down_read_trylock(&em28xx_disconnect)) return -ERESTARTSYS; if (dev->users) { em28xx_warn("this driver can be opened only once\n"); up_read(&em28xx_disconnect); return -EBUSY; } mutex_init(&dev->fileop_lock); /* to 1 == available */ spin_lock_init(&dev->queue_lock); init_waitqueue_head(&dev->wait_frame); init_waitqueue_head(&dev->wait_stream); mutex_lock(&dev->lock); if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { em28xx_set_alternate(dev); dev->width = norm_maxw(dev); dev->height = norm_maxh(dev); dev->frame_size = dev->width * dev->height * 2; dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ dev->bytesperline = dev->width * 2; dev->hscale = 0; dev->vscale = 0; em28xx_capture_start(dev, 1); em28xx_resolution_set(dev); /* device needs to be initialized before isoc transfer */ video_mux(dev, 0); /* start the transfer */ errCode = em28xx_init_isoc(dev); if (errCode) goto err; } dev->users++; filp->private_data = dev; dev->io = IO_NONE; dev->stream = STREAM_OFF; dev->num_frames = 0; /* prepare queues */ em28xx_empty_framequeues(dev); dev->state |= DEV_INITIALIZED;err: mutex_unlock(&dev->lock); up_read(&em28xx_disconnect); return errCode;}/* * em28xx_realease_resources() * unregisters the v4l2,i2c and usb devices * called when the device gets disconected or at module unload*/static void em28xx_release_resources(struct em28xx *dev){ mutex_lock(&em28xx_sysfs_lock); /*FIXME: I2C IR should be disconnected */ em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n", dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); list_del(&dev->devlist); video_unregister_device(dev->vdev); video_unregister_device(dev->vbi_dev); em28xx_i2c_unregister(dev); usb_put_dev(dev->udev); mutex_unlock(&em28xx_sysfs_lock); /* Mark device as unused */ em28xx_devused&=~(1<<dev->devno);}/* * em28xx_v4l2_close() * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls */static int em28xx_v4l2_close(struct inode *inode, struct file *filp){ int errCode; struct em28xx *dev=filp->private_data; em28xx_videodbg("users=%d\n", dev->users); mutex_lock(&dev->lock); em28xx_uninit_isoc(dev); em28xx_release_buffers(dev); /* the device is already disconnect, free the remaining resources */ if (dev->state & DEV_DISCONNECTED) { em28xx_release_resources(dev); mutex_unlock(&dev->lock); kfree(dev); return 0; } /* set alternate 0 */ dev->alt = 0; em28xx_videodbg("setting alternate 0\n"); errCode = usb_set_interface(dev->udev, 0, 0); if (errCode < 0) { em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n", errCode); } dev->users--; wake_up_interruptible_nr(&dev->open, 1); mutex_unlock(&dev->lock); return 0;}/* * em28xx_v4l2_read() * will allocate buffers when called for the first time */static ssize_tem28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, loff_t * f_pos){ struct em28xx_frame_t *f, *i; unsigned long lock_flags; int ret = 0; struct em28xx *dev = filp->private_data; if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { em28xx_videodbg("V4l2_Buf_type_videocapture is set\n"); } if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) { em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n"); em28xx_videodbg("not supported yet! ...\n"); if (copy_to_user(buf, "", 1)) { mutex_unlock(&dev->fileop_lock); return -EFAULT; } return (1); } if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) { em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n"); em28xx_videodbg("not supported yet! ...\n"); if (copy_to_user(buf, "", 1)) { mutex_unlock(&dev->fileop_lock); return -EFAULT; } return (1); } if (mutex_lock_interruptible(&dev->fileop_lock)) return -ERESTARTSYS; if (dev->state & DEV_DISCONNECTED) { em28xx_videodbg("device not present\n"); mutex_unlock(&dev->fileop_lock); return -ENODEV; } if (dev->state & DEV_MISCONFIGURED) { em28xx_videodbg("device misconfigured; close and open it again\n"); mutex_unlock(&dev->fileop_lock); return -EIO; } if (dev->io == IO_MMAP) { em28xx_videodbg ("IO method is set to mmap; close and open" " the device again to choose the read method\n"); mutex_unlock(&dev->fileop_lock); return -EINVAL; } if (dev->io == IO_NONE) { if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) { em28xx_errdev("read failed, not enough memory\n"); mutex_unlock(&dev->fileop_lock); return -ENOMEM; } dev->io = IO_READ; dev->stream = STREAM_ON; em28xx_queue_unusedframes(dev); } if (!count) { mutex_unlock(&dev->fileop_lock); return 0; } if (list_empty(&dev->outqueue)) { if (filp->f_flags & O_NONBLOCK) { mutex_unlock(&dev->fileop_lock); return -EAGAIN; } ret = wait_event_interruptible
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -