2 * dvb_frontend.c: DVB frontend tuning interface/thread
5 * Copyright (C) 1999-2001 Ralph Metzler
8 * for convergence integrated media GmbH
10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/module.h>
35 #include <linux/list.h>
36 #include <linux/freezer.h>
37 #include <linux/jiffies.h>
38 #include <linux/kthread.h>
39 #include <asm/processor.h>
41 #include "dvb_frontend.h"
43 #include <linux/dvb/version.h>
45 static int dvb_frontend_debug;
46 static int dvb_shutdown_timeout;
47 static int dvb_force_auto_inversion;
48 static int dvb_override_tune_delay;
49 static int dvb_powerdown_on_sleep = 1;
51 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
52 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
53 module_param(dvb_shutdown_timeout, int, 0644);
54 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
55 module_param(dvb_force_auto_inversion, int, 0644);
56 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
57 module_param(dvb_override_tune_delay, int, 0644);
58 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
59 module_param(dvb_powerdown_on_sleep, int, 0644);
60 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
62 #define dprintk if (dvb_frontend_debug) printk
64 #define FESTATE_IDLE 1
65 #define FESTATE_RETUNE 2
66 #define FESTATE_TUNING_FAST 4
67 #define FESTATE_TUNING_SLOW 8
68 #define FESTATE_TUNED 16
69 #define FESTATE_ZIGZAG_FAST 32
70 #define FESTATE_ZIGZAG_SLOW 64
71 #define FESTATE_DISEQC 128
72 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
73 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
74 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
75 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
79 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
80 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
81 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
82 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
83 * FESTATE_TUNED. The frontend has successfully locked on.
84 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
85 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
86 * FESTATE_DISEQC. A DISEQC command has just been issued.
87 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
88 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
89 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
90 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
93 static DEFINE_MUTEX(frontend_mutex);
95 struct dvb_frontend_private {
97 /* thread/frontend values */
98 struct dvb_device *dvbdev;
99 struct dvb_frontend_parameters parameters;
100 struct dvb_fe_events events;
101 struct semaphore sem;
102 struct list_head list_head;
103 wait_queue_head_t wait_queue;
104 struct task_struct *thread;
105 unsigned long release_jiffies;
109 unsigned long tune_mode_flags;
111 unsigned int reinitialise;
115 /* swzigzag values */
117 unsigned int bending;
119 unsigned int inversion;
120 unsigned int auto_step;
121 unsigned int auto_sub_step;
122 unsigned int started_auto_step;
123 unsigned int min_delay;
124 unsigned int max_drift;
125 unsigned int step_size;
127 unsigned int check_wrapped;
130 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
132 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
134 struct dvb_frontend_private *fepriv = fe->frontend_priv;
135 struct dvb_fe_events *events = &fepriv->events;
136 struct dvb_frontend_event *e;
139 dprintk ("%s\n", __func__);
141 if (mutex_lock_interruptible (&events->mtx))
144 wp = (events->eventw + 1) % MAX_EVENT;
146 if (wp == events->eventr) {
147 events->overflow = 1;
148 events->eventr = (events->eventr + 1) % MAX_EVENT;
151 e = &events->events[events->eventw];
153 memcpy (&e->parameters, &fepriv->parameters,
154 sizeof (struct dvb_frontend_parameters));
156 if (status & FE_HAS_LOCK)
157 if (fe->ops.get_frontend)
158 fe->ops.get_frontend(fe, &e->parameters);
162 mutex_unlock(&events->mtx);
166 wake_up_interruptible (&events->wait_queue);
169 static int dvb_frontend_get_event(struct dvb_frontend *fe,
170 struct dvb_frontend_event *event, int flags)
172 struct dvb_frontend_private *fepriv = fe->frontend_priv;
173 struct dvb_fe_events *events = &fepriv->events;
175 dprintk ("%s\n", __func__);
177 if (events->overflow) {
178 events->overflow = 0;
182 if (events->eventw == events->eventr) {
185 if (flags & O_NONBLOCK)
190 ret = wait_event_interruptible (events->wait_queue,
191 events->eventw != events->eventr);
193 if (down_interruptible (&fepriv->sem))
200 if (mutex_lock_interruptible (&events->mtx))
203 memcpy (event, &events->events[events->eventr],
204 sizeof(struct dvb_frontend_event));
206 events->eventr = (events->eventr + 1) % MAX_EVENT;
208 mutex_unlock(&events->mtx);
213 static void dvb_frontend_init(struct dvb_frontend *fe)
215 dprintk ("DVB: initialising frontend %i (%s)...\n",
221 if (fe->ops.tuner_ops.init) {
222 fe->ops.tuner_ops.init(fe);
223 if (fe->ops.i2c_gate_ctrl)
224 fe->ops.i2c_gate_ctrl(fe, 0);
228 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
230 struct dvb_frontend_private *fepriv = fe->frontend_priv;
232 fepriv->reinitialise = 1;
233 dvb_frontend_wakeup(fe);
235 EXPORT_SYMBOL(dvb_frontend_reinitialise);
237 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
241 dprintk ("%s\n", __func__);
244 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
246 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
248 q2 = fepriv->quality - 128;
251 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
255 * Performs automatic twiddling of frontend parameters.
257 * @param fe The frontend concerned.
258 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
259 * @returns Number of complete iterations that have been performed.
261 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
265 struct dvb_frontend_private *fepriv = fe->frontend_priv;
266 int original_inversion = fepriv->parameters.inversion;
267 u32 original_frequency = fepriv->parameters.frequency;
269 /* are we using autoinversion? */
270 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
271 (fepriv->parameters.inversion == INVERSION_AUTO));
273 /* setup parameters correctly */
275 /* calculate the lnb_drift */
276 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
278 /* wrap the auto_step if we've exceeded the maximum drift */
279 if (fepriv->lnb_drift > fepriv->max_drift) {
280 fepriv->auto_step = 0;
281 fepriv->auto_sub_step = 0;
282 fepriv->lnb_drift = 0;
285 /* perform inversion and +/- zigzag */
286 switch(fepriv->auto_sub_step) {
288 /* try with the current inversion and current drift setting */
293 if (!autoinversion) break;
295 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
300 if (fepriv->lnb_drift == 0) break;
302 fepriv->lnb_drift = -fepriv->lnb_drift;
307 if (fepriv->lnb_drift == 0) break;
308 if (!autoinversion) break;
310 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
311 fepriv->lnb_drift = -fepriv->lnb_drift;
317 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
321 if (!ready) fepriv->auto_sub_step++;
324 /* if this attempt would hit where we started, indicate a complete
325 * iteration has occurred */
326 if ((fepriv->auto_step == fepriv->started_auto_step) &&
327 (fepriv->auto_sub_step == 0) && check_wrapped) {
331 dprintk("%s: drift:%i inversion:%i auto_step:%i "
332 "auto_sub_step:%i started_auto_step:%i\n",
333 __func__, fepriv->lnb_drift, fepriv->inversion,
334 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
336 /* set the frontend itself */
337 fepriv->parameters.frequency += fepriv->lnb_drift;
339 fepriv->parameters.inversion = fepriv->inversion;
340 if (fe->ops.set_frontend)
341 fe->ops.set_frontend(fe, &fepriv->parameters);
343 fepriv->parameters.frequency = original_frequency;
344 fepriv->parameters.inversion = original_inversion;
346 fepriv->auto_sub_step++;
350 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
353 struct dvb_frontend_private *fepriv = fe->frontend_priv;
355 /* if we've got no parameters, just keep idling */
356 if (fepriv->state & FESTATE_IDLE) {
357 fepriv->delay = 3*HZ;
362 /* in SCAN mode, we just set the frontend when asked and leave it alone */
363 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
364 if (fepriv->state & FESTATE_RETUNE) {
365 if (fe->ops.set_frontend)
366 fe->ops.set_frontend(fe, &fepriv->parameters);
367 fepriv->state = FESTATE_TUNED;
369 fepriv->delay = 3*HZ;
374 /* get the frontend status */
375 if (fepriv->state & FESTATE_RETUNE) {
378 if (fe->ops.read_status)
379 fe->ops.read_status(fe, &s);
380 if (s != fepriv->status) {
381 dvb_frontend_add_event(fe, s);
386 /* if we're not tuned, and we have a lock, move to the TUNED state */
387 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
388 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
389 fepriv->state = FESTATE_TUNED;
391 /* if we're tuned, then we have determined the correct inversion */
392 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
393 (fepriv->parameters.inversion == INVERSION_AUTO)) {
394 fepriv->parameters.inversion = fepriv->inversion;
399 /* if we are tuned already, check we're still locked */
400 if (fepriv->state & FESTATE_TUNED) {
401 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
403 /* we're tuned, and the lock is still good... */
404 if (s & FE_HAS_LOCK) {
406 } else { /* if we _WERE_ tuned, but now don't have a lock */
407 fepriv->state = FESTATE_ZIGZAG_FAST;
408 fepriv->started_auto_step = fepriv->auto_step;
409 fepriv->check_wrapped = 0;
413 /* don't actually do anything if we're in the LOSTLOCK state,
414 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
415 if ((fepriv->state & FESTATE_LOSTLOCK) &&
416 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
417 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
421 /* don't do anything if we're in the DISEQC state, since this
422 * might be someone with a motorized dish controlled by DISEQC.
423 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
424 if (fepriv->state & FESTATE_DISEQC) {
425 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
429 /* if we're in the RETUNE state, set everything up for a brand
430 * new scan, keeping the current inversion setting, as the next
431 * tune is _very_ likely to require the same */
432 if (fepriv->state & FESTATE_RETUNE) {
433 fepriv->lnb_drift = 0;
434 fepriv->auto_step = 0;
435 fepriv->auto_sub_step = 0;
436 fepriv->started_auto_step = 0;
437 fepriv->check_wrapped = 0;
441 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
442 fepriv->delay = fepriv->min_delay;
445 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
446 /* OK, if we've run out of trials at the fast speed.
447 * Drop back to slow for the _next_ attempt */
448 fepriv->state = FESTATE_SEARCHING_SLOW;
449 fepriv->started_auto_step = fepriv->auto_step;
452 fepriv->check_wrapped = 1;
454 /* if we've just retuned, enter the ZIGZAG_FAST state.
455 * This ensures we cannot return from an
456 * FE_SET_FRONTEND ioctl before the first frontend tune
458 if (fepriv->state & FESTATE_RETUNE) {
459 fepriv->state = FESTATE_TUNING_FAST;
464 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
465 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
467 /* Note: don't bother checking for wrapping; we stay in this
468 * state until we get a lock */
469 dvb_frontend_swzigzag_autotune(fe, 0);
473 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
475 struct dvb_frontend_private *fepriv = fe->frontend_priv;
480 if (fepriv->dvbdev->writers == 1)
481 if (time_after(jiffies, fepriv->release_jiffies +
482 dvb_shutdown_timeout * HZ))
488 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
490 struct dvb_frontend_private *fepriv = fe->frontend_priv;
492 if (fepriv->wakeup) {
496 return dvb_frontend_is_exiting(fe);
499 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
501 struct dvb_frontend_private *fepriv = fe->frontend_priv;
504 wake_up_interruptible(&fepriv->wait_queue);
507 static int dvb_frontend_thread(void *data)
509 struct dvb_frontend *fe = data;
510 struct dvb_frontend_private *fepriv = fe->frontend_priv;
511 unsigned long timeout;
513 struct dvb_frontend_parameters *params;
515 dprintk("%s\n", __func__);
517 fepriv->check_wrapped = 0;
519 fepriv->delay = 3*HZ;
522 fepriv->reinitialise = 0;
524 dvb_frontend_init(fe);
528 up(&fepriv->sem); /* is locked when we enter the thread... */
530 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
531 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
532 || freezing(current),
535 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
536 /* got signal or quitting */
543 if (down_interruptible(&fepriv->sem))
546 if (fepriv->reinitialise) {
547 dvb_frontend_init(fe);
548 if (fepriv->tone != -1) {
549 fe->ops.set_tone(fe, fepriv->tone);
551 if (fepriv->voltage != -1) {
552 fe->ops.set_voltage(fe, fepriv->voltage);
554 fepriv->reinitialise = 0;
557 /* do an iteration of the tuning loop */
558 if (fe->ops.get_frontend_algo) {
559 if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
560 /* have we been asked to retune? */
562 if (fepriv->state & FESTATE_RETUNE) {
563 params = &fepriv->parameters;
564 fepriv->state = FESTATE_TUNED;
567 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
568 if (s != fepriv->status) {
569 dvb_frontend_add_event(fe, s);
573 dvb_frontend_swzigzag(fe);
575 dvb_frontend_swzigzag(fe);
578 if (dvb_powerdown_on_sleep) {
579 if (fe->ops.set_voltage)
580 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
581 if (fe->ops.tuner_ops.sleep) {
582 fe->ops.tuner_ops.sleep(fe);
583 if (fe->ops.i2c_gate_ctrl)
584 fe->ops.i2c_gate_ctrl(fe, 0);
590 fepriv->thread = NULL;
593 dvb_frontend_wakeup(fe);
597 static void dvb_frontend_stop(struct dvb_frontend *fe)
599 struct dvb_frontend_private *fepriv = fe->frontend_priv;
601 dprintk ("%s\n", __func__);
609 kthread_stop(fepriv->thread);
611 init_MUTEX (&fepriv->sem);
612 fepriv->state = FESTATE_IDLE;
614 /* paranoia check in case a signal arrived */
616 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
620 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
622 return ((curtime.tv_usec < lasttime.tv_usec) ?
623 1000000 - lasttime.tv_usec + curtime.tv_usec :
624 curtime.tv_usec - lasttime.tv_usec);
626 EXPORT_SYMBOL(timeval_usec_diff);
628 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
630 curtime->tv_usec += add_usec;
631 if (curtime->tv_usec >= 1000000) {
632 curtime->tv_usec -= 1000000;
638 * Sleep until gettimeofday() > waketime + add_usec
639 * This needs to be as precise as possible, but as the delay is
640 * usually between 2ms and 32ms, it is done using a scheduled msleep
641 * followed by usleep (normally a busy-wait loop) for the remainder
643 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
645 struct timeval lasttime;
648 timeval_usec_add(waketime, add_usec);
650 do_gettimeofday(&lasttime);
651 delta = timeval_usec_diff(lasttime, *waketime);
653 msleep((delta - 1500) / 1000);
654 do_gettimeofday(&lasttime);
655 newdelta = timeval_usec_diff(lasttime, *waketime);
656 delta = (newdelta > delta) ? 0 : newdelta;
661 EXPORT_SYMBOL(dvb_frontend_sleep_until);
663 static int dvb_frontend_start(struct dvb_frontend *fe)
666 struct dvb_frontend_private *fepriv = fe->frontend_priv;
667 struct task_struct *fe_thread;
669 dprintk ("%s\n", __func__);
671 if (fepriv->thread) {
675 dvb_frontend_stop (fe);
678 if (signal_pending(current))
680 if (down_interruptible (&fepriv->sem))
683 fepriv->state = FESTATE_IDLE;
685 fepriv->thread = NULL;
688 fe_thread = kthread_run(dvb_frontend_thread, fe,
689 "kdvb-fe-%i", fe->dvb->num);
690 if (IS_ERR(fe_thread)) {
691 ret = PTR_ERR(fe_thread);
692 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
696 fepriv->thread = fe_thread;
700 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
701 u32 *freq_min, u32 *freq_max)
703 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
705 if (fe->ops.info.frequency_max == 0)
706 *freq_max = fe->ops.tuner_ops.info.frequency_max;
707 else if (fe->ops.tuner_ops.info.frequency_max == 0)
708 *freq_max = fe->ops.info.frequency_max;
710 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
712 if (*freq_min == 0 || *freq_max == 0)
713 printk(KERN_WARNING "DVB: frontend %u frequency limits undefined - fix the driver\n",
717 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
718 struct dvb_frontend_parameters *parms)
723 /* range check: frequency */
724 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
725 if ((freq_min && parms->frequency < freq_min) ||
726 (freq_max && parms->frequency > freq_max)) {
727 printk(KERN_WARNING "DVB: frontend %u frequency %u out of range (%u..%u)\n",
728 fe->dvb->num, parms->frequency, freq_min, freq_max);
732 /* range check: symbol rate */
733 if (fe->ops.info.type == FE_QPSK) {
734 if ((fe->ops.info.symbol_rate_min &&
735 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
736 (fe->ops.info.symbol_rate_max &&
737 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
738 printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
739 fe->dvb->num, parms->u.qpsk.symbol_rate,
740 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
744 } else if (fe->ops.info.type == FE_QAM) {
745 if ((fe->ops.info.symbol_rate_min &&
746 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
747 (fe->ops.info.symbol_rate_max &&
748 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
749 printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
750 fe->dvb->num, parms->u.qam.symbol_rate,
751 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
759 struct dtv_cmds_h dtv_cmds[] = {
773 .name = "DTV_FREQUENCY",
774 .cmd = DTV_FREQUENCY,
777 [DTV_BANDWIDTH_HZ] = {
778 .name = "DTV_BANDWIDTH_HZ",
779 .cmd = DTV_BANDWIDTH_HZ,
783 .name = "DTV_MODULATION",
784 .cmd = DTV_MODULATION,
788 .name = "DTV_INVERSION",
789 .cmd = DTV_INVERSION,
792 [DTV_DISEQC_MASTER] = {
793 .name = "DTV_DISEQC_MASTER",
794 .cmd = DTV_DISEQC_MASTER,
798 [DTV_SYMBOL_RATE] = {
799 .name = "DTV_SYMBOL_RATE",
800 .cmd = DTV_SYMBOL_RATE,
804 .name = "DTV_INNER_FEC",
805 .cmd = DTV_INNER_FEC,
809 .name = "DTV_VOLTAGE",
824 .name = "DTV_ROLLOFF",
828 [DTV_DELIVERY_SYSTEM] = {
829 .name = "DTV_DELIVERY_SYSTEM",
830 .cmd = DTV_DELIVERY_SYSTEM,
833 [DTV_CODE_RATE_HP] = {
834 .name = "DTV_CODE_RATE_HP",
835 .cmd = DTV_CODE_RATE_HP,
838 [DTV_CODE_RATE_LP] = {
839 .name = "DTV_CODE_RATE_LP",
840 .cmd = DTV_CODE_RATE_LP,
844 [DTV_DISEQC_SLAVE_REPLY] = {
845 .name = "DTV_DISEQC_SLAVE_REPLY",
846 .cmd = DTV_DISEQC_SLAVE_REPLY,
850 [DTV_API_VERSION] = {
851 .name = "DTV_API_VERSION",
852 .cmd = DTV_API_VERSION,
855 [DTV_CODE_RATE_HP] = {
856 .name = "DTV_CODE_RATE_HP",
857 .cmd = DTV_CODE_RATE_HP,
860 [DTV_CODE_RATE_LP] = {
861 .name = "DTV_CODE_RATE_LP",
862 .cmd = DTV_CODE_RATE_LP,
867 void dtv_property_dump(struct dtv_property *tvp)
871 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
872 printk("%s: tvp.cmd = 0x%08x (undefined/unknown/invalid)\n",
877 printk("%s() tvp.cmd = 0x%08x (%s)\n"
880 ,dtv_cmds[ tvp->cmd ].name);
882 if(dtv_cmds[ tvp->cmd ].buffer) {
884 printk("%s() tvp.u.buffer.len = 0x%02x\n"
888 for(i = 0; i < tvp->u.buffer.len; i++)
889 printk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
892 ,tvp->u.buffer.data[i]);
895 printk("%s() tvp.u.data = 0x%08x\n", __FUNCTION__, tvp->u.data);
898 int is_legacy_delivery_system(fe_delivery_system_t s)
900 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
901 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
907 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
908 * drivers can use a single set_frontend tuning function, regardless of whether
909 * it's being used for the legacy or new API, reducing code and complexity.
911 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
913 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
915 printk("%s()\n", __FUNCTION__);
917 c->frequency = p->frequency;
918 c->inversion = p->inversion;
920 switch (fe->ops.info.type) {
922 c->modulation = QPSK; /* implied for DVB-S in legacy API */
923 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
924 c->symbol_rate = p->u.qpsk.symbol_rate;
925 c->fec_inner = p->u.qpsk.fec_inner;
926 c->delivery_system = SYS_DVBS;
929 c->symbol_rate = p->u.qam.symbol_rate;
930 c->fec_inner = p->u.qam.fec_inner;
931 c->modulation = p->u.qam.modulation;
932 c->delivery_system = SYS_DVBC_ANNEX_AC;
935 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
936 c->bandwidth_hz = 6000000;
937 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
938 c->bandwidth_hz = 7000000;
939 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
940 c->bandwidth_hz = 8000000;
942 /* Including BANDWIDTH_AUTO */
944 c->code_rate_HP = p->u.ofdm.code_rate_HP;
945 c->code_rate_LP = p->u.ofdm.code_rate_LP;
946 c->modulation = p->u.ofdm.constellation;
947 c->transmission_mode = p->u.ofdm.transmission_mode;
948 c->guard_interval = p->u.ofdm.guard_interval;
949 c->hierarchy = p->u.ofdm.hierarchy_information;
950 c->delivery_system = SYS_DVBT;
953 c->modulation = p->u.vsb.modulation;
954 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
955 c->delivery_system = SYS_ATSC;
957 c->delivery_system = SYS_DVBC_ANNEX_B;
962 /* Ensure the cached values are set correctly in the frontend
963 * legacy tuning structures, for the advanced tuning API.
965 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
967 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
968 struct dvb_frontend_private *fepriv = fe->frontend_priv;
969 struct dvb_frontend_parameters *p = &fepriv->parameters;
971 printk("%s()\n", __FUNCTION__);
973 p->frequency = c->frequency;
974 p->inversion = c->inversion;
976 switch (fe->ops.info.type) {
978 printk("%s() Preparing QPSK req\n", __FUNCTION__);
979 p->u.qpsk.symbol_rate = c->symbol_rate;
980 p->u.qpsk.fec_inner = c->fec_inner;
981 c->delivery_system = SYS_DVBS;
984 printk("%s() Preparing QAM req\n", __FUNCTION__);
985 p->u.qam.symbol_rate = c->symbol_rate;
986 p->u.qam.fec_inner = c->fec_inner;
987 p->u.qam.modulation = c->modulation;
988 c->delivery_system = SYS_DVBC_ANNEX_AC;
991 printk("%s() Preparing OFDM req\n", __FUNCTION__);
992 if (c->bandwidth_hz == 6000000)
993 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
994 else if (c->bandwidth_hz == 7000000)
995 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
996 else if (c->bandwidth_hz == 8000000)
997 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
999 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1000 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1001 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1002 p->u.ofdm.constellation = c->modulation;
1003 p->u.ofdm.transmission_mode = c->transmission_mode;
1004 p->u.ofdm.guard_interval = c->guard_interval;
1005 p->u.ofdm.hierarchy_information = c->hierarchy;
1006 c->delivery_system = SYS_DVBT;
1009 printk("%s() Preparing VSB req\n", __FUNCTION__);
1010 p->u.vsb.modulation = c->modulation;
1011 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1012 c->delivery_system = SYS_ATSC;
1014 c->delivery_system = SYS_DVBC_ANNEX_B;
1019 /* Ensure the cached values are set correctly in the frontend
1020 * legacy tuning structures, for the legacy tuning API.
1022 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1024 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1025 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1026 struct dvb_frontend_parameters *p = &fepriv->parameters;
1028 printk("%s()\n", __FUNCTION__);
1030 p->frequency = c->frequency;
1031 p->inversion = c->inversion;
1033 switch(c->modulation) {
1037 p->u.qpsk.symbol_rate = c->symbol_rate;
1038 p->u.qpsk.fec_inner = c->fec_inner;
1044 if(c->delivery_system == SYS_ISDBT) {
1045 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1046 p->frequency = c->frequency;
1047 p->inversion = INVERSION_AUTO;
1048 p->u.ofdm.constellation = QAM_AUTO;
1049 p->u.ofdm.code_rate_HP = FEC_AUTO;
1050 p->u.ofdm.code_rate_LP = FEC_AUTO;
1051 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1052 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1053 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1054 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1058 void dtv_property_cache_submit(struct dvb_frontend *fe)
1060 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1062 printk("%s()\n", __FUNCTION__);
1064 /* For legacy delivery systems we don't need the delivery_system to
1065 * be specified, but we populate the older structures from the cache
1066 * so we can call set_frontend on older drivers.
1068 if(is_legacy_delivery_system(c->delivery_system)) {
1070 printk("%s() legacy, modulation = %d\n", __FUNCTION__, c->modulation);
1071 dtv_property_legacy_params_sync(fe);
1074 printk("%s() adv, modulation = %d\n", __FUNCTION__, c->modulation);
1076 /* For advanced delivery systems / modulation types ...
1077 * we seed the lecacy dvb_frontend_parameters structure
1078 * so that the sanity checking code later in the IOCTL processing
1079 * can validate our basic frequency ranges, symbolrates, modulation
1082 dtv_property_adv_params_sync(fe);
1086 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1087 unsigned int cmd, void *parg);
1088 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1089 unsigned int cmd, void *parg);
1091 int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
1092 struct inode *inode, struct file *file)
1096 printk("%s()\n", __FUNCTION__);
1098 dtv_property_dump(tvp);
1100 /* Allow the frontend to validate incoming properties */
1101 if (fe->ops.get_property)
1102 r = fe->ops.get_property(fe, tvp);
1109 tvp->u.data = fe->dtv_property_cache.frequency;
1111 case DTV_MODULATION:
1112 tvp->u.data = fe->dtv_property_cache.modulation;
1114 case DTV_BANDWIDTH_HZ:
1115 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1118 tvp->u.data = fe->dtv_property_cache.inversion;
1120 case DTV_SYMBOL_RATE:
1121 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1124 tvp->u.data = fe->dtv_property_cache.fec_inner;
1127 tvp->u.data = fe->dtv_property_cache.pilot;
1130 tvp->u.data = fe->dtv_property_cache.rolloff;
1132 case DTV_DELIVERY_SYSTEM:
1133 tvp->u.data = fe->dtv_property_cache.delivery_system;
1136 tvp->u.data = fe->dtv_property_cache.voltage;
1139 tvp->u.data = fe->dtv_property_cache.sectone;
1141 case DTV_API_VERSION:
1142 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1144 case DTV_CODE_RATE_HP:
1145 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1147 case DTV_CODE_RATE_LP:
1148 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1157 int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
1158 struct inode *inode, struct file *file)
1161 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1162 printk("%s()\n", __FUNCTION__);
1163 dtv_property_dump(tvp);
1165 /* Allow the frontend to validate incoming properties */
1166 if (fe->ops.set_property)
1167 r = fe->ops.set_property(fe, tvp);
1174 /* Reset a cache of data specific to the frontend here. This does
1175 * not effect hardware.
1177 printk("%s() Flushing property cache\n", __FUNCTION__);
1178 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1179 fe->dtv_property_cache.state = tvp->cmd;
1180 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1183 /* interpret the cache of data, build either a traditional frontend
1184 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1187 fe->dtv_property_cache.state = tvp->cmd;
1188 printk("%s() Finalised property cache\n", __FUNCTION__);
1189 dtv_property_cache_submit(fe);
1191 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1192 &fepriv->parameters);
1195 fe->dtv_property_cache.frequency = tvp->u.data;
1197 case DTV_MODULATION:
1198 fe->dtv_property_cache.modulation = tvp->u.data;
1200 case DTV_BANDWIDTH_HZ:
1201 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1204 fe->dtv_property_cache.inversion = tvp->u.data;
1206 case DTV_SYMBOL_RATE:
1207 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1210 fe->dtv_property_cache.fec_inner = tvp->u.data;
1213 fe->dtv_property_cache.pilot = tvp->u.data;
1216 fe->dtv_property_cache.rolloff = tvp->u.data;
1218 case DTV_DELIVERY_SYSTEM:
1219 fe->dtv_property_cache.delivery_system = tvp->u.data;
1222 fe->dtv_property_cache.voltage = tvp->u.data;
1223 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1224 (void *)fe->dtv_property_cache.voltage);
1227 fe->dtv_property_cache.sectone = tvp->u.data;
1228 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1229 (void *)fe->dtv_property_cache.sectone);
1231 case DTV_CODE_RATE_HP:
1232 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1234 case DTV_CODE_RATE_LP:
1235 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1244 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1245 unsigned int cmd, void *parg)
1247 struct dvb_device *dvbdev = file->private_data;
1248 struct dvb_frontend *fe = dvbdev->priv;
1249 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1250 int err = -EOPNOTSUPP;
1252 dprintk ("%s\n", __func__);
1257 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1258 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1259 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1262 if (down_interruptible (&fepriv->sem))
1263 return -ERESTARTSYS;
1265 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1266 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1268 fe->dtv_property_cache.state = DTV_UNDEFINED;
1269 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1276 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1277 unsigned int cmd, void *parg)
1279 struct dvb_device *dvbdev = file->private_data;
1280 struct dvb_frontend *fe = dvbdev->priv;
1283 struct dtv_properties *tvps = NULL;
1284 struct dtv_property *tvp = NULL;
1287 dprintk("%s\n", __func__);
1289 if(cmd == FE_SET_PROPERTY) {
1290 printk("%s() FE_SET_PROPERTY\n", __FUNCTION__);
1292 tvps = (struct dtv_properties __user *)parg;
1294 printk("%s() properties.num = %d\n", __FUNCTION__, tvps->num);
1295 printk("%s() properties.props = %p\n", __FUNCTION__, tvps->props);
1297 /* Put an arbitrary limit on the number of messages that can
1298 * be sent at once */
1299 if (tvps->num > DTV_IOCTL_MAX_MSGS)
1302 tvp = (struct dtv_property *) kmalloc(tvps->num *
1303 sizeof(struct dtv_property), GFP_KERNEL);
1309 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1314 for (i = 0; i < tvps->num; i++) {
1315 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1316 err |= (tvp + i)->result;
1319 if(fe->dtv_property_cache.state == DTV_TUNE) {
1320 printk("%s() Property cache is full, tuning\n", __FUNCTION__);
1324 if(cmd == FE_GET_PROPERTY) {
1325 printk("%s() FE_GET_PROPERTY\n", __FUNCTION__);
1327 tvps = (struct dtv_properties __user *)parg;
1329 printk("%s() properties.num = %d\n", __FUNCTION__, tvps->num);
1330 printk("%s() properties.props = %p\n", __FUNCTION__, tvps->props);
1332 /* Put an arbitrary limit on the number of messages that can
1333 * be sent at once */
1334 if (tvps->num > DTV_IOCTL_MAX_MSGS)
1337 tvp = (struct dtv_property *) kmalloc(tvps->num *
1338 sizeof(struct dtv_property), GFP_KERNEL);
1344 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1349 for (i = 0; i < tvps->num; i++) {
1350 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1351 err |= (tvp + i)->result;
1354 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1367 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1368 unsigned int cmd, void *parg)
1370 struct dvb_device *dvbdev = file->private_data;
1371 struct dvb_frontend *fe = dvbdev->priv;
1372 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1373 int err = -EOPNOTSUPP;
1377 struct dvb_frontend_info* info = parg;
1378 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1379 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1381 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1382 * do it, it is done for it. */
1383 info->caps |= FE_CAN_INVERSION_AUTO;
1388 case FE_READ_STATUS: {
1389 fe_status_t* status = parg;
1391 /* if retune was requested but hasn't occured yet, prevent
1392 * that user get signal state from previous tuning */
1393 if(fepriv->state == FESTATE_RETUNE) {
1399 if (fe->ops.read_status)
1400 err = fe->ops.read_status(fe, status);
1404 if (fe->ops.read_ber)
1405 err = fe->ops.read_ber(fe, (__u32*) parg);
1408 case FE_READ_SIGNAL_STRENGTH:
1409 if (fe->ops.read_signal_strength)
1410 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1414 if (fe->ops.read_snr)
1415 err = fe->ops.read_snr(fe, (__u16*) parg);
1418 case FE_READ_UNCORRECTED_BLOCKS:
1419 if (fe->ops.read_ucblocks)
1420 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1424 case FE_DISEQC_RESET_OVERLOAD:
1425 if (fe->ops.diseqc_reset_overload) {
1426 err = fe->ops.diseqc_reset_overload(fe);
1427 fepriv->state = FESTATE_DISEQC;
1432 case FE_DISEQC_SEND_MASTER_CMD:
1433 if (fe->ops.diseqc_send_master_cmd) {
1434 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1435 fepriv->state = FESTATE_DISEQC;
1440 case FE_DISEQC_SEND_BURST:
1441 if (fe->ops.diseqc_send_burst) {
1442 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1443 fepriv->state = FESTATE_DISEQC;
1449 if (fe->ops.set_tone) {
1450 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1451 fepriv->tone = (fe_sec_tone_mode_t) parg;
1452 fepriv->state = FESTATE_DISEQC;
1457 case FE_SET_VOLTAGE:
1458 if (fe->ops.set_voltage) {
1459 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1460 fepriv->voltage = (fe_sec_voltage_t) parg;
1461 fepriv->state = FESTATE_DISEQC;
1466 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1467 if (fe->ops.dishnetwork_send_legacy_command) {
1468 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1469 fepriv->state = FESTATE_DISEQC;
1471 } else if (fe->ops.set_voltage) {
1473 * NOTE: This is a fallback condition. Some frontends
1474 * (stv0299 for instance) take longer than 8msec to
1475 * respond to a set_voltage command. Those switches
1476 * need custom routines to switch properly. For all
1477 * other frontends, the following shoule work ok.
1478 * Dish network legacy switches (as used by Dish500)
1479 * are controlled by sending 9-bit command words
1480 * spaced 8msec apart.
1481 * the actual command word is switch/port dependant
1482 * so it is up to the userspace application to send
1483 * the right command.
1484 * The command must always start with a '0' after
1485 * initialization, so parg is 8 bits and does not
1486 * include the initialization or start bit
1488 unsigned long swcmd = ((unsigned long) parg) << 1;
1489 struct timeval nexttime;
1490 struct timeval tv[10];
1493 if (dvb_frontend_debug)
1494 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1495 do_gettimeofday(&nexttime);
1496 if (dvb_frontend_debug)
1497 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1498 /* before sending a command, initialize by sending
1499 * a 32ms 18V to the switch
1501 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1502 dvb_frontend_sleep_until(&nexttime, 32000);
1504 for (i = 0; i < 9; i++) {
1505 if (dvb_frontend_debug)
1506 do_gettimeofday(&tv[i + 1]);
1507 if ((swcmd & 0x01) != last) {
1508 /* set voltage to (last ? 13V : 18V) */
1509 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1510 last = (last) ? 0 : 1;
1514 dvb_frontend_sleep_until(&nexttime, 8000);
1516 if (dvb_frontend_debug) {
1517 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1518 __func__, fe->dvb->num);
1519 for (i = 1; i < 10; i++)
1520 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1523 fepriv->state = FESTATE_DISEQC;
1528 case FE_DISEQC_RECV_SLAVE_REPLY:
1529 if (fe->ops.diseqc_recv_slave_reply)
1530 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1533 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1534 if (fe->ops.enable_high_lnb_voltage)
1535 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1538 case FE_SET_FRONTEND: {
1539 struct dvb_frontend_tune_settings fetunesettings;
1541 if(fe->dtv_property_cache.state == DTV_TUNE) {
1542 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1547 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1552 memcpy (&fepriv->parameters, parg,
1553 sizeof (struct dvb_frontend_parameters));
1554 dtv_property_cache_sync(fe, &fepriv->parameters);
1557 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1558 memcpy(&fetunesettings.parameters, parg,
1559 sizeof (struct dvb_frontend_parameters));
1561 /* force auto frequency inversion if requested */
1562 if (dvb_force_auto_inversion) {
1563 fepriv->parameters.inversion = INVERSION_AUTO;
1564 fetunesettings.parameters.inversion = INVERSION_AUTO;
1566 if (fe->ops.info.type == FE_OFDM) {
1567 /* without hierarchical coding code_rate_LP is irrelevant,
1568 * so we tolerate the otherwise invalid FEC_NONE setting */
1569 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1570 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1571 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1574 /* get frontend-specific tuning settings */
1575 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1576 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1577 fepriv->max_drift = fetunesettings.max_drift;
1578 fepriv->step_size = fetunesettings.step_size;
1580 /* default values */
1581 switch(fe->ops.info.type) {
1583 fepriv->min_delay = HZ/20;
1584 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1585 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1589 fepriv->min_delay = HZ/20;
1590 fepriv->step_size = 0; /* no zigzag */
1591 fepriv->max_drift = 0;
1595 fepriv->min_delay = HZ/20;
1596 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1597 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1600 fepriv->min_delay = HZ/20;
1601 fepriv->step_size = 0;
1602 fepriv->max_drift = 0;
1606 if (dvb_override_tune_delay > 0)
1607 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1609 fepriv->state = FESTATE_RETUNE;
1610 dvb_frontend_wakeup(fe);
1611 dvb_frontend_add_event(fe, 0);
1618 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1621 case FE_GET_FRONTEND:
1622 if (fe->ops.get_frontend) {
1623 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1624 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1628 case FE_SET_FRONTEND_TUNE_MODE:
1629 fepriv->tune_mode_flags = (unsigned long) parg;
1638 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1640 struct dvb_device *dvbdev = file->private_data;
1641 struct dvb_frontend *fe = dvbdev->priv;
1642 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1644 dprintk ("%s\n", __func__);
1646 poll_wait (file, &fepriv->events.wait_queue, wait);
1648 if (fepriv->events.eventw != fepriv->events.eventr)
1649 return (POLLIN | POLLRDNORM | POLLPRI);
1654 static int dvb_frontend_open(struct inode *inode, struct file *file)
1656 struct dvb_device *dvbdev = file->private_data;
1657 struct dvb_frontend *fe = dvbdev->priv;
1658 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1661 dprintk ("%s\n", __func__);
1663 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1664 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1668 if ((ret = dvb_generic_open (inode, file)) < 0)
1671 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1672 /* normal tune mode when opened R/W */
1673 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1675 fepriv->voltage = -1;
1677 ret = dvb_frontend_start (fe);
1681 /* empty event queue */
1682 fepriv->events.eventr = fepriv->events.eventw = 0;
1688 dvb_generic_release(inode, file);
1690 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1691 fe->ops.ts_bus_ctrl(fe, 0);
1695 static int dvb_frontend_release(struct inode *inode, struct file *file)
1697 struct dvb_device *dvbdev = file->private_data;
1698 struct dvb_frontend *fe = dvbdev->priv;
1699 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1702 dprintk ("%s\n", __func__);
1704 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1705 fepriv->release_jiffies = jiffies;
1707 ret = dvb_generic_release (inode, file);
1709 if (dvbdev->users == -1) {
1710 if (fepriv->exit == 1) {
1711 fops_put(file->f_op);
1713 wake_up(&dvbdev->wait_queue);
1715 if (fe->ops.ts_bus_ctrl)
1716 fe->ops.ts_bus_ctrl(fe, 0);
1722 static struct file_operations dvb_frontend_fops = {
1723 .owner = THIS_MODULE,
1724 .ioctl = dvb_generic_ioctl,
1725 .poll = dvb_frontend_poll,
1726 .open = dvb_frontend_open,
1727 .release = dvb_frontend_release
1730 int dvb_register_frontend(struct dvb_adapter* dvb,
1731 struct dvb_frontend* fe)
1733 struct dvb_frontend_private *fepriv;
1734 static const struct dvb_device dvbdev_template = {
1738 .fops = &dvb_frontend_fops,
1739 .kernel_ioctl = dvb_frontend_ioctl
1742 dprintk ("%s\n", __func__);
1744 if (mutex_lock_interruptible(&frontend_mutex))
1745 return -ERESTARTSYS;
1747 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1748 if (fe->frontend_priv == NULL) {
1749 mutex_unlock(&frontend_mutex);
1752 fepriv = fe->frontend_priv;
1754 init_MUTEX (&fepriv->sem);
1755 init_waitqueue_head (&fepriv->wait_queue);
1756 init_waitqueue_head (&fepriv->events.wait_queue);
1757 mutex_init(&fepriv->events.mtx);
1759 fepriv->inversion = INVERSION_OFF;
1761 printk ("DVB: registering frontend %i (%s)...\n",
1765 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1766 fe, DVB_DEVICE_FRONTEND);
1768 mutex_unlock(&frontend_mutex);
1771 EXPORT_SYMBOL(dvb_register_frontend);
1773 int dvb_unregister_frontend(struct dvb_frontend* fe)
1775 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1776 dprintk ("%s\n", __func__);
1778 mutex_lock(&frontend_mutex);
1779 dvb_frontend_stop (fe);
1780 mutex_unlock(&frontend_mutex);
1782 if (fepriv->dvbdev->users < -1)
1783 wait_event(fepriv->dvbdev->wait_queue,
1784 fepriv->dvbdev->users==-1);
1786 mutex_lock(&frontend_mutex);
1787 dvb_unregister_device (fepriv->dvbdev);
1789 /* fe is invalid now */
1791 mutex_unlock(&frontend_mutex);
1794 EXPORT_SYMBOL(dvb_unregister_frontend);
1796 #ifdef CONFIG_MEDIA_ATTACH
1797 void dvb_frontend_detach(struct dvb_frontend* fe)
1801 if (fe->ops.release_sec) {
1802 fe->ops.release_sec(fe);
1803 symbol_put_addr(fe->ops.release_sec);
1805 if (fe->ops.tuner_ops.release) {
1806 fe->ops.tuner_ops.release(fe);
1807 symbol_put_addr(fe->ops.tuner_ops.release);
1809 if (fe->ops.analog_ops.release) {
1810 fe->ops.analog_ops.release(fe);
1811 symbol_put_addr(fe->ops.analog_ops.release);
1813 ptr = (void*)fe->ops.release;
1815 fe->ops.release(fe);
1816 symbol_put_addr(ptr);
1820 void dvb_frontend_detach(struct dvb_frontend* fe)
1822 if (fe->ops.release_sec)
1823 fe->ops.release_sec(fe);
1824 if (fe->ops.tuner_ops.release)
1825 fe->ops.tuner_ops.release(fe);
1826 if (fe->ops.analog_ops.release)
1827 fe->ops.analog_ops.release(fe);
1828 if (fe->ops.release)
1829 fe->ops.release(fe);
1832 EXPORT_SYMBOL(dvb_frontend_detach);