📄 dvb_frontend.c
字号:
/* * dvb-core.c: DVB core driver * * Copyright (C) 1999-2001 Ralph Metzler * Marcus Metzler * Holger Waechtler * for convergence integrated media GmbH * * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup) * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html */#include <linux/string.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/wait.h>#include <linux/slab.h>#include <linux/poll.h>#include <linux/module.h>#include <linux/list.h>#include <asm/processor.h>#include <asm/semaphore.h>#include "dvb_frontend.h"#include "dvbdev.h"#include "dvb_functions.h"#define FESTATE_IDLE 1#define FESTATE_RETUNE 2#define FESTATE_TUNING_FAST 4#define FESTATE_TUNING_SLOW 8#define FESTATE_TUNED 16#define FESTATE_ZIGZAG_FAST 32#define FESTATE_ZIGZAG_SLOW 64#define FESTATE_CLEAN_SETUP 128#define FESTATE_SEARCHING (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)#define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)#define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)#define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)/* * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling. * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune. * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress. * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower. * FESTATE_TUNED. The frontend has successfully locked on. * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it. * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower. * FESTATE_CLEAN_SETUP. Used for certain dodgy tuners which need special massaging to lock. * FESTATE_SEARCHING. When we're searching for a signal using a zigzag scan of any sort. * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan. * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan. * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again. */static int dvb_frontend_debug = 0;static int dvb_shutdown_timeout = 5;static int dvb_frequency_bending = 1;#define dprintk if (dvb_frontend_debug) printk#define MAX_EVENT 8struct dvb_fe_events { struct dvb_frontend_event events[MAX_EVENT]; int eventw; int eventr; int overflow; wait_queue_head_t wait_queue; struct semaphore sem;};struct dvb_frontend_data { struct dvb_frontend_info *info; struct dvb_frontend frontend; struct dvb_device *dvbdev; struct dvb_frontend_parameters parameters; struct dvb_fe_events events; struct semaphore sem; struct list_head list_head; wait_queue_head_t wait_queue; pid_t thread_pid; unsigned long release_jiffies; int state; int bending; int lnb_drift; int inversion; int auto_count; int started_auto_count; int min_delay; int exit; fe_status_t status;};struct dvb_frontend_ioctl_data { struct list_head list_head; struct dvb_adapter *adapter; int (*before_ioctl) (struct dvb_frontend *frontend, unsigned int cmd, void *arg); int (*after_ioctl) (struct dvb_frontend *frontend, unsigned int cmd, void *arg); void *before_after_data;};struct dvb_frontend_notifier_data { struct list_head list_head; struct dvb_adapter *adapter; void (*callback) (fe_status_t s, void *data); void *data;};static LIST_HEAD(frontend_list);static LIST_HEAD(frontend_ioctl_list);static LIST_HEAD(frontend_notifier_list);static DECLARE_MUTEX(frontend_mutex);static int dvb_frontend_internal_ioctl (struct dvb_frontend *frontend, unsigned int cmd, void *arg){ int err = -EOPNOTSUPP; dprintk ("%s\n", __FUNCTION__); if (frontend->before_ioctl) err = frontend->before_ioctl (frontend, cmd, arg); if (err == -EOPNOTSUPP) { err = frontend->ioctl (frontend, cmd, arg); if ((err == -EOPNOTSUPP) && frontend->after_ioctl) err = frontend->after_ioctl (frontend, cmd, arg); } return err;}/** * if 2 tuners are located side by side you can get interferences when * they try to tune to the same frequency, so both lose sync. * We will slightly mistune in this case. The AFC of the demodulator * should make it still possible to receive the requested transponder * on both tuners... */static void dvb_bend_frequency (struct dvb_frontend_data *this_fe, int recursive){ struct list_head *entry; int stepsize = this_fe->info->frequency_stepsize; int this_fe_adap_num = this_fe->frontend.i2c->adapter->num; int frequency; if (!stepsize || recursive > 10) { printk ("%s: too deep recursion, check frequency_stepsize " "in your frontend code!\n", __FUNCTION__); return; } dprintk ("%s\n", __FUNCTION__); if (!recursive) { if (down_interruptible (&frontend_mutex)) return; this_fe->bending = 0; } list_for_each (entry, &frontend_list) { struct dvb_frontend_data *fe; int f; fe = list_entry (entry, struct dvb_frontend_data, list_head); if (fe->frontend.i2c->adapter->num != this_fe_adap_num) continue; f = fe->parameters.frequency; f += fe->lnb_drift; f += fe->bending; frequency = this_fe->parameters.frequency; frequency += this_fe->lnb_drift; frequency += this_fe->bending; if (this_fe != fe && (fe->state != FESTATE_IDLE) && frequency > f - stepsize && frequency < f + stepsize) { if (recursive % 2) this_fe->bending += stepsize; else this_fe->bending = -this_fe->bending; dvb_bend_frequency (this_fe, recursive + 1); goto done; } }done: if (!recursive) up (&frontend_mutex);}static void dvb_call_frontend_notifiers (struct dvb_frontend_data *fe, fe_status_t s){ dprintk ("%s\n", __FUNCTION__); if (((s ^ fe->status) & FE_HAS_LOCK) && (s & FE_HAS_LOCK)) dvb_delay (fe->info->notifier_delay); fe->status = s; if (!(s & FE_HAS_LOCK) && (fe->info->caps & FE_CAN_MUTE_TS)) return; /** * now tell the Demux about the TS status changes... */ if (fe->frontend.notifier_callback) fe->frontend.notifier_callback(fe->status, fe->frontend.notifier_data);}static void dvb_frontend_add_event (struct dvb_frontend_data *fe, fe_status_t status){ struct dvb_fe_events *events = &fe->events; struct dvb_frontend_event *e; int wp; dprintk ("%s\n", __FUNCTION__); if (down_interruptible (&events->sem)) return; wp = (events->eventw + 1) % MAX_EVENT; if (wp == events->eventr) { events->overflow = 1; events->eventr = (events->eventr + 1) % MAX_EVENT; } e = &events->events[events->eventw]; memcpy (&e->parameters, &fe->parameters, sizeof (struct dvb_frontend_parameters)); if (status & FE_HAS_LOCK) dvb_frontend_internal_ioctl (&fe->frontend, FE_GET_FRONTEND, &e->parameters); events->eventw = wp; up (&events->sem); e->status = status; dvb_call_frontend_notifiers (fe, status); wake_up_interruptible (&events->wait_queue);}static int dvb_frontend_get_event (struct dvb_frontend_data *fe, struct dvb_frontend_event *event, int flags){ struct dvb_fe_events *events = &fe->events; dprintk ("%s\n", __FUNCTION__); if (events->overflow) { events->overflow = 0; return -EOVERFLOW; } if (events->eventw == events->eventr) { int ret; if (flags & O_NONBLOCK) return -EWOULDBLOCK; up(&fe->sem); ret = wait_event_interruptible (events->wait_queue, events->eventw != events->eventr); if (down_interruptible (&fe->sem)) return -ERESTARTSYS; if (ret < 0) return ret; } if (down_interruptible (&events->sem)) return -ERESTARTSYS; memcpy (event, &events->events[events->eventr], sizeof(struct dvb_frontend_event)); events->eventr = (events->eventr + 1) % MAX_EVENT; up (&events->sem); return 0;}static void dvb_frontend_init (struct dvb_frontend_data *fe){ struct dvb_frontend *frontend = &fe->frontend; dprintk ("DVB: initialising frontend %i:%i (%s)...\n", frontend->i2c->adapter->num, frontend->i2c->id, fe->info->name); dvb_frontend_internal_ioctl (frontend, FE_INIT, NULL);}static void update_delay (int *quality, int *delay, int min_delay, int locked){ int q2; dprintk ("%s\n", __FUNCTION__); if (locked) (*quality) = (*quality * 220 + 36*256) / 256; else (*quality) = (*quality * 220 + 0) / 256; q2 = *quality - 128; q2 *= q2; *delay = min_delay + q2 * HZ / (128*128);}/** * Performs automatic twiddling of frontend parameters. * * @param fe The frontend concerned. * @returns Number of complete iterations that have been performed. */static int dvb_frontend_autotune(struct dvb_frontend_data *fe){ int stepsize; int maxdrift; int autoinversion; int ready = 0; int wrapped = 0; int original_inversion = fe->parameters.inversion; u32 original_frequency = fe->parameters.frequency; dprintk ("%s\n", __FUNCTION__); // choose step size for zigzag scan switch(fe->info->type) { case FE_QPSK: if (fe->parameters.u.qpsk.symbol_rate < 10000000) { stepsize = fe->parameters.u.qpsk.symbol_rate / 32000; maxdrift = 5000; } else { stepsize = fe->parameters.u.qpsk.symbol_rate / 16000; maxdrift = fe->parameters.u.qpsk.symbol_rate / 2000; } break; case FE_QAM: stepsize = 1; maxdrift = 0; // don't want any zigzagging under DVB-C frontends break; case FE_OFDM: stepsize = fe->info->frequency_stepsize * 2; maxdrift = (fe->info->frequency_stepsize * 2) + 1; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -