📄 tm6000.c
字号:
/* * Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com> *//*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License version 2 as * * published by the Free Software Foundation. * * * * 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., 51 Franklin Street, Fifth Floor, Boston, * * MA 02110-1301 USA * ***************************************************************************//* * Contains code from the ZL10353 module for testing purposes: * Copyright (C) 2006 Christopher Pascoe <c.pascoe@itee.uq.edu.au> */#include <linux/module.h> /* Needed by all modules */#include <linux/kernel.h> /* Needed for KERN_ALERT */#include <linux/init.h>#include <linux/mutex.h>#include <linux/usb.h>#include <linux/sched.h> /* For current */#include <linux/version.h> /* For LINUX_VERSION_CODE */#include <linux/dvb/frontend.h>#include <linux/videodev2.h>#include <linux/firmware.h>#include <linux/delay.h>#include <linux/workqueue.h>#include <linux/i2c.h>#include "dmxdev.h"#include "dvb_demux.h"#include "dvb_frontend.h"#include "zl10353.h"#define DRIVER_AUTHOR "Michel Ludwig <michel.ludwig@gmail.com>"#define DRIVER_DESC "Trident TVMaster TM6000 USB driver"#define USB_SND_CONTROL_MSG(dev, request, value, index) tm6000_snd_control_msg(dev, request, value, index, NULL, 0)#define USB_SND_CONTROL_MSG_DATA(dev, request, value, index, local_data) tm6000_snd_local_control_msg(dev, request, value, index, local_data, sizeof(local_data) - 1)MODULE_LICENSE("GPL");MODULE_AUTHOR(DRIVER_AUTHOR);MODULE_DESCRIPTION(DRIVER_DESC);struct tm6000_driver { struct usb_driver driver; struct list_head devices; struct mutex open_close_mutex;} tm6000_driver;__u16 tm6000_init_data1[][4] ={{0x07, 0x00df, 0x001f, 0x0000},{0x07, 0x00ff, 0x0008, 0x0000},{0x07, 0x00ff, 0x0000, 0x0000},{0x07, 0x00d5, 0x004f, 0x0000},{0x07, 0x00da, 0x0023, 0x0000},{0x07, 0x00db, 0x0008, 0x0000},{0x07, 0x00e2, 0x0000, 0x0000},{0x07, 0x00e3, 0x0010, 0x0000},{0x07, 0x00e5, 0x0000, 0x0000},{0x07, 0x00e8, 0x0000, 0x0000},{0x07, 0x00eb, 0x0064, 0x0000},{0x07, 0x00ee, 0x00c2, 0x0000},{0x07, 0x003f, 0x0001, 0x0000},{0x07, 0x0000, 0x0000, 0x0000},{0x07, 0x0001, 0x0007, 0x0000},{0x07, 0x0002, 0x005f, 0x0000},{0x07, 0x0003, 0x0000, 0x0000},{0x07, 0x0005, 0x0064, 0x0000},{0x07, 0x0007, 0x0001, 0x0000},{0x07, 0x0008, 0x0082, 0x0000},{0x07, 0x0009, 0x0036, 0x0000},{0x07, 0x000a, 0x0050, 0x0000},{0x07, 0x000c, 0x006a, 0x0000},{0x07, 0x0011, 0x00c9, 0x0000},{0x07, 0x0012, 0x0007, 0x0000},{0x07, 0x0013, 0x003b, 0x0000},{0x07, 0x0014, 0x0047, 0x0000},{0x07, 0x0015, 0x006f, 0x0000},{0x07, 0x0017, 0x00cd, 0x0000},{0x07, 0x0018, 0x001e, 0x0000},{0x07, 0x0019, 0x008b, 0x0000},{0x07, 0x001a, 0x00a2, 0x0000},{0x07, 0x001b, 0x00e9, 0x0000},{0x07, 0x001c, 0x001c, 0x0000},{0x07, 0x001d, 0x00cc, 0x0000},{0x07, 0x001e, 0x00cc, 0x0000},{0x07, 0x001f, 0x00cd, 0x0000},{0x07, 0x0020, 0x003c, 0x0000},{0x07, 0x0021, 0x003c, 0x0000},{0x07, 0x002d, 0x0048, 0x0000},{0x07, 0x002e, 0x0088, 0x0000},{0x07, 0x0030, 0x0022, 0x0000},{0x07, 0x0031, 0x0061, 0x0000},{0x07, 0x0032, 0x0074, 0x0000},{0x07, 0x0033, 0x001c, 0x0000},{0x07, 0x0034, 0x0074, 0x0000},{0x07, 0x0035, 0x001c, 0x0000},{0x07, 0x0036, 0x007a, 0x0000},{0x07, 0x0037, 0x0026, 0x0000},{0x07, 0x0038, 0x0040, 0x0000},{0x07, 0x0039, 0x000a, 0x0000},{0x07, 0x0042, 0x0055, 0x0000},{0x07, 0x0051, 0x0011, 0x0000},{0x07, 0x0055, 0x0001, 0x0000},{0x07, 0x0057, 0x0002, 0x0000},{0x07, 0x0058, 0x0035, 0x0000},{0x07, 0x0059, 0x00a0, 0x0000},{0x07, 0x0080, 0x0015, 0x0000},{0x07, 0x0082, 0x0042, 0x0000},{0x07, 0x00c1, 0x00d0, 0x0000},{0x07, 0x00c3, 0x0088, 0x0000},{0x07, 0x003f, 0x0000, 0x0000},{0x05, 0x0018, 0x0000, 0x0000},{0x03, 0x0300, 0x0001, 0x0000},{0x03, 0x0300, 0x0000, 0x0000},{0x03, 0x0300, 0x0001, 0x0000}};struct tm6000_device { struct mutex mutex; struct mutex streaming_mutex; struct usb_device* usb_device; struct usb_interface* interface; struct dvb_adapter dvb_adapter; struct dvb_frontend* dvb_frontend; struct dvb_demux dvb_demux; struct dmxdev dmxdev; unsigned int streaming; struct i2c_adapter zl10353_i2c_adapter; struct i2c_adapter xc3028_i2c_adapter; struct work_struct remote_work_struct; u8 *transfer_buffer; /* transfer buffer */ unsigned int buffer_length; struct urb *urb; /* urb for transfers */ u8 i2c_response;};static struct usb_device_id tm6000_ids[] = { /* Freecom DVB-T & Analog USB Stick */ { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT, .idVendor = 0x14aa, .idProduct = 0x0620, .bInterfaceClass = USB_CLASS_VENDOR_SPEC, .bInterfaceSubClass = 0, .bInterfaceProtocol = 255 }, {}};MODULE_DEVICE_TABLE(usb, tm6000_ids);inline int tm6000_snd_control_msg(struct tm6000_device *dev, __u8 request, __u16 value, __u16 index, void *data, __u16 size){ int ret; mutex_lock(&tm6000_driver.open_close_mutex); ret = usb_control_msg(dev->usb_device, usb_sndctrlpipe(dev->usb_device, 0), request, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value, index, data, size, 5*HZ); mutex_unlock(&tm6000_driver.open_close_mutex); if(ret < 0) { err("tm6000: error %i has occurred whilst sending a USB control message with request %u, value %u and index %u\n", ret, request, value, index); } else { msleep(10); } return ret;}inline int tm6000_snd_local_control_msg(struct tm6000_device *dev, u8 request, u16 value, u16 index, void *data, u16 size){ int ret; u8 *snd_data = kzalloc(size*sizeof(u8), GFP_KERNEL); if(!snd_data) { err("tm6000: couldn't allocate memory for sending a USB control message (%s)", __FUNCTION__); return -ENOMEM; } memcpy(snd_data, data, size); ret = tm6000_snd_control_msg(dev, request, value, index, snd_data, size); kfree(data); return ret;}inline int tm6000_rcv_control_msg(struct tm6000_device *dev, __u8 request, __u16 value, __u16 index, void *data, __u16 size){ int ret; mutex_lock(&tm6000_driver.open_close_mutex); ret = usb_control_msg(dev->usb_device, usb_rcvctrlpipe(dev->usb_device, 0), request, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value, index, data, size, 5*HZ); mutex_unlock(&tm6000_driver.open_close_mutex); if(ret < 0) { err("tm6000: error %i has occurred whilst sending a receiving USB control message with request %u, value %u and index %u\n", ret, request, value, index); } else { msleep(10); } return ret;}// forward declarationsstatic int probe(struct usb_interface *intf, const struct usb_device_id *id);void disconnect(struct usb_interface *intf);struct tm6000_driver tm6000_driver = { .driver = {#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) .owner = THIS_MODULE,#endif .name = "tm6000", .probe = &probe, .disconnect = &disconnect, .id_table = tm6000_ids, }};#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)static void tm6000_urb_received(struct urb *urb, struct pt_regs *ptregs) {#elsestatic void tm6000_urb_received(struct urb *urb) {#endif int ret; struct tm6000_device* tm6000_dev = urb->context; if(urb->status != 0){ printk(KERN_ERR "tm6000: status != 0\n"); } else if(urb->actual_length>0){ dvb_dmx_swfilter(&tm6000_dev->dvb_demux,urb->transfer_buffer, urb->actual_length); } if(tm6000_dev->streaming > 0) {// usb_fill_bulk_urb(tm6000_dev->urb, tm6000_dev->usb_device, usb_rcvbulkpipe(tm6000_dev->usb_device,0x82), tm6000_dev->transfer_buffer, 0x1000, tm6000_urb_received, tm6000_dev); ret = usb_submit_urb(urb, GFP_ATOMIC); if(ret < 0) { printk(KERN_ERR "tm6000: error %s\n", __FUNCTION__); kfree(urb->transfer_buffer); usb_free_urb(urb); } }}static int tm6000_set_led_status(struct tm6000_device *tm6000_dev, int on_off){ int ret; if(on_off) { ret = tm6000_snd_control_msg(tm6000_dev, 0x03, 0x0102, 0x01, 0x0, 0x0); } else { ret = tm6000_snd_control_msg(tm6000_dev, 0x03, 0x0102, 0x00, 0x0, 0x0); } return ret;}static void tm6000_poll_remote(void* arg){ int ret; struct tm6000_device* tm6000_dev = arg; u8 *data; ret = tm6000_snd_control_msg(tm6000_dev, 0x04, 0x02, 0x0, 0x0, 0x0); ret = tm6000_snd_control_msg(tm6000_dev, 0x04, 0x02, 0x01, 0x0, 0x0); data = kzalloc(1*sizeof(u8), GFP_KERNEL); ret = tm6000_rcv_control_msg(tm6000_dev, 0x02, 0x0, 0x0, data, 0x1); if(ret < 0) { printk(KERN_ERR "error %i while polling value of remote control\n", ret); } if(data[0] != 0xff) { printk(KERN_ALERT "received value %u\n", data[0]); } kfree(data); schedule_delayed_work(&tm6000_dev->remote_work_struct, msecs_to_jiffies(200));}static int tm6000_start_stream(struct tm6000_device *tm6000_dev) { int i, errCode, ret; printk(KERN_INFO "tm6000: got start stream request %s\n",__FUNCTION__); /* switch the led on */ ret = tm6000_set_led_status(tm6000_dev, 0x1); if(ret < 0) { return -1; } ret = usb_clear_halt(tm6000_dev->usb_device, usb_rcvbulkpipe(tm6000_dev->usb_device, 0x82)); if(ret < 0) { printk(KERN_ERR "tm6000: error %i in %s\n",ret,__FUNCTION__); } else { printk(KERN_INFO "tm6000: pipe resetted\n"); } tm6000_dev->transfer_buffer = kzalloc(0x1000, GFP_KERNEL); if(tm6000_dev->transfer_buffer == NULL) { printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n"); return -1; } tm6000_dev->buffer_length = 0x1000;// INIT_WORK(&tm6000_dev->work_struct, tm6000_complete_work, tm6000_dev);// ret = schedule_delayed_work(&tm6000_dev->work_struct, msecs_to_jiffies(5));// if(ret < 0) {// printk(KERN_ERR "tm6000: error %i schedule_delayed_work\n", ret);// } tm6000_dev->urb = usb_alloc_urb(0, GFP_KERNEL); if(tm6000_dev->urb == NULL) { printk(KERN_ERR "tm6000: couldn't allocate urb\n"); kfree(tm6000_dev->urb->transfer_buffer); }// usb_fill_bulk_urb(tm6000_dev->urb, tm6000_dev->usb_device, usb_rcvbulkpipe(tm6000_dev->usb_device,0x82), tm6000_dev->transfer_buffer, 0x1000, tm6000_urb_received, tm6000_dev); usb_fill_int_urb(tm6000_dev->urb, tm6000_dev->usb_device, usb_rcvintpipe(tm6000_dev->usb_device,0x82), tm6000_dev->transfer_buffer, 0x1000, tm6000_urb_received, tm6000_dev, 1);// mutex_lock(&tm6000_driver.open_close_mutex); ret = usb_submit_urb(tm6000_dev->urb, GFP_KERNEL);// printk("urb submitted with transfer_buffer at %i\n", tm6000_dev->transfer_buffer);// mutex_unlock(&tm6000_driver.open_close_mutex); if (ret) { printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",ret); kfree(tm6000_dev->urb->transfer_buffer); usb_free_urb(tm6000_dev->urb); } ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00ff, 0x08, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00ff, 0x00, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x003f, 0x01, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00df, 0x08, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00e2, 0x0c, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00e8, 0xff, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00eb, 0xd8, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00c0, 0x40, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00c1, 0xd0, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00c3, 0x09, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00da, 0x37, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00d1, 0xd8, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00d2, 0xc0, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x07, 0x00d6, 0x60, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x04, 0x0020, 0x00, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x04, 0x0020, 0x01, 0x0, 0x00); ret = tm6000_snd_control_msg(tm6000_dev, 0x03, 0x0101, 0x01, 0x0, 0x0); ret = tm6000_snd_control_msg(tm6000_dev, 0x03, 0x0101, 0x00, 0x0, 0x0); return 0;}static void tm6000_stop_stream(struct tm6000_device *tm6000_dev) { /* switch the led off */ tm6000_set_led_status(tm6000_dev, 0x0); usb_kill_urb(tm6000_dev->urb); kfree(tm6000_dev->urb->transfer_buffer); usb_free_urb(tm6000_dev->urb); tm6000_dev->urb = NULL; tm6000_dev->transfer_buffer = NULL;}int tm6000_start_feed(struct dvb_demux_feed *feed) { struct dvb_demux *demux = feed->demux; struct tm6000_device *tm6000_dev = demux->priv; printk(KERN_INFO "tm6000: got start feed request %s\n",__FUNCTION__); mutex_lock(&tm6000_dev->mutex); if(tm6000_dev->streaming==0) { tm6000_dev->streaming = 1; mutex_init(&tm6000_dev->streaming_mutex); tm6000_start_stream(tm6000_dev); } else { ++(tm6000_dev->streaming); } mutex_unlock(&tm6000_dev->mutex); return 0;}int tm6000_stop_feed(struct dvb_demux_feed *feed) { struct dvb_demux *demux = feed->demux; struct tm6000_device *tm6000_dev = demux->priv; printk(KERN_INFO "tm6000: got stop feed request %s\n",__FUNCTION__); mutex_lock(&tm6000_dev->mutex); --tm6000_dev->streaming; mutex_unlock(&tm6000_dev->mutex); mutex_lock(&tm6000_dev->mutex); if(0 == tm6000_dev->streaming) { tm6000_stop_stream(tm6000_dev);// mutex_destroy(&tm6000_dev->streaming_mutex); } mutex_unlock(&tm6000_dev->mutex);// mutex_destroy(&tm6000_dev->streaming_mutex); return 0;}static int tm6000_zl10353_i2c_xfer(struct i2c_adapter *i2c_adapter, struct i2c_msg msgs[], int num){ int i, j, ret; unsigned int success = 0, k; u8* data; u16 req; struct tm6000_device *tm6000_dev = i2c_adapter->algo_data; for(i = 0; i < num; ++i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -