📄 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@brturbo.com.br> 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/usb.h>#include <linux/i2c.h>#include <linux/version.h>#include <linux/video_decoder.h>#include <linux/mutex.h>#include "em28xx.h"#include <media/tuner.h>#include <media/v4l2-common.h>#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ "Markus Rechberger <mrechberger@gmail.com>, " \ "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \ "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 };module_param_array(card, int, NULL, 0444);MODULE_PARM_DESC(card,"card type");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]");/* 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, }};static const unsigned char saa7114_i2c_init[] = { 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0, 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a, 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42, 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff, 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff, 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff, 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00, 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00, 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0, 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06, 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67, 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd, 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00, 0xbe,0x00,0xbf,0x00};#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, }};/* FIXME: These are specific to saa711x - should be moved to its code */static struct v4l2_queryctrl saa711x_qctrl[] = { { .id = V4L2_CID_BRIGHTNESS, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Brightness", .minimum = -128, .maximum = 127, .step = 1, .default_value = 0, .flags = 0, },{ .id = V4L2_CID_CONTRAST, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Contrast", .minimum = 0x0, .maximum = 0x1f, .step = 0x1, .default_value = 0x10, .flags = 0, },{ .id = V4L2_CID_SATURATION, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Saturation", .minimum = 0x0, .maximum = 0x1f, .step = 0x1, .default_value = 0x10, .flags = 0, },{ .id = V4L2_CID_RED_BALANCE, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Red chroma balance", .minimum = -128, .maximum = 127, .step = 1, .default_value = 0, .flags = 0, },{ .id = V4L2_CID_BLUE_BALANCE, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Blue chroma balance", .minimum = -128, .maximum = 127, .step = 1, .default_value = 0, .flags = 0, },{ .id = V4L2_CID_GAMMA, .type = V4L2_CTRL_TYPE_INTEGER, .name = "Gamma", .minimum = 0x0, .maximum = 0x3f, .step = 0x1, .default_value = 0x20, .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 */ em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1); /* enable vbi capturing */ 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 video_decoder_init em28xx_vdi = {.data = NULL }; /* configure decoder */ if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){ em28xx_vdi.data=saa7114_i2c_init; em28xx_vdi.len=sizeof(saa7114_i2c_init); } em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi); em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);/* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); *//* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); *//* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); *//* em28xx_i2c_call_clients(dev,DECODER_DUMP, 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 input, ainput; input = INPUT(index)->vmux; dev->ctl_input = index; dev->ctl_ainput = INPUT(index)->amux; em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input); em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,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); em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput); 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; struct list_head *list; list_for_each(list,&em28xx_devlist) { h = list_entry(list, struct em28xx, devlist); if (h->vdev->minor == minor) { dev = h; } } filp->private_data=dev; em28xx_videodbg("users=%d\n", 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; }/* if(dev->vbi_dev->minor == minor){ dev->type=V4L2_BUF_TYPE_VBI_CAPTURE; }*/ if (dev->vdev->minor == minor) { dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; } init_MUTEX(&dev->fileop_lock); /* to 1 == available */ spin_lock_init(&dev->queue_lock); init_waitqueue_head(&dev->wait_frame); init_waitqueue_head(&dev->wait_stream); down(&dev->lock); 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; video_mux(dev, 0); err: up(&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); em28xx_info("V4L2 device /dev/video%d deregistered\n", dev->vdev->minor); 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);}/* * 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); down(&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); up(&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); up(&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 (down_interruptible(&dev->fileop_lock)) return -ERESTARTSYS; if (dev->state & DEV_DISCONNECTED) { em28xx_videodbg("device not present\n"); up(&dev->fileop_lock); return -ENODEV; } if (dev->state & DEV_MISCONFIGURED) { em28xx_videodbg("device misconfigured; close and open it again\n"); up(&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"); up(&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"); up(&dev->fileop_lock); return -ENOMEM; } dev->io = IO_READ; dev->stream = STREAM_ON; em28xx_queue_unusedframes(dev); } if (!count) { up(&dev->fileop_lock); return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -