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;
50 static int dvb_mfe_wait_time = 5;
52 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
53 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
54 module_param(dvb_shutdown_timeout, int, 0644);
55 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
56 module_param(dvb_force_auto_inversion, int, 0644);
57 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
58 module_param(dvb_override_tune_delay, int, 0644);
59 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
60 module_param(dvb_powerdown_on_sleep, int, 0644);
61 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
62 module_param(dvb_mfe_wait_time, int, 0644);
63 MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
65 #define dprintk if (dvb_frontend_debug) printk
67 #define FESTATE_IDLE 1
68 #define FESTATE_RETUNE 2
69 #define FESTATE_TUNING_FAST 4
70 #define FESTATE_TUNING_SLOW 8
71 #define FESTATE_TUNED 16
72 #define FESTATE_ZIGZAG_FAST 32
73 #define FESTATE_ZIGZAG_SLOW 64
74 #define FESTATE_DISEQC 128
75 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
76 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
77 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
78 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
82 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
83 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
84 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
85 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
86 * FESTATE_TUNED. The frontend has successfully locked on.
87 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
88 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
89 * FESTATE_DISEQC. A DISEQC command has just been issued.
90 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
91 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
92 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
93 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
96 static DEFINE_MUTEX(frontend_mutex);
98 struct dvb_frontend_private {
100 /* thread/frontend values */
101 struct dvb_device *dvbdev;
102 struct dvb_frontend_parameters parameters;
103 struct dvb_fe_events events;
104 struct semaphore sem;
105 struct list_head list_head;
106 wait_queue_head_t wait_queue;
107 struct task_struct *thread;
108 unsigned long release_jiffies;
112 unsigned long tune_mode_flags;
114 unsigned int reinitialise;
118 /* swzigzag values */
120 unsigned int bending;
122 unsigned int inversion;
123 unsigned int auto_step;
124 unsigned int auto_sub_step;
125 unsigned int started_auto_step;
126 unsigned int min_delay;
127 unsigned int max_drift;
128 unsigned int step_size;
130 unsigned int check_wrapped;
133 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
135 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
137 struct dvb_frontend_private *fepriv = fe->frontend_priv;
138 struct dvb_fe_events *events = &fepriv->events;
139 struct dvb_frontend_event *e;
142 dprintk ("%s\n", __func__);
144 if (mutex_lock_interruptible (&events->mtx))
147 wp = (events->eventw + 1) % MAX_EVENT;
149 if (wp == events->eventr) {
150 events->overflow = 1;
151 events->eventr = (events->eventr + 1) % MAX_EVENT;
154 e = &events->events[events->eventw];
156 memcpy (&e->parameters, &fepriv->parameters,
157 sizeof (struct dvb_frontend_parameters));
159 if (status & FE_HAS_LOCK)
160 if (fe->ops.get_frontend)
161 fe->ops.get_frontend(fe, &e->parameters);
165 mutex_unlock(&events->mtx);
169 wake_up_interruptible (&events->wait_queue);
172 static int dvb_frontend_get_event(struct dvb_frontend *fe,
173 struct dvb_frontend_event *event, int flags)
175 struct dvb_frontend_private *fepriv = fe->frontend_priv;
176 struct dvb_fe_events *events = &fepriv->events;
178 dprintk ("%s\n", __func__);
180 if (events->overflow) {
181 events->overflow = 0;
185 if (events->eventw == events->eventr) {
188 if (flags & O_NONBLOCK)
193 ret = wait_event_interruptible (events->wait_queue,
194 events->eventw != events->eventr);
196 if (down_interruptible (&fepriv->sem))
203 if (mutex_lock_interruptible (&events->mtx))
206 memcpy (event, &events->events[events->eventr],
207 sizeof(struct dvb_frontend_event));
209 events->eventr = (events->eventr + 1) % MAX_EVENT;
211 mutex_unlock(&events->mtx);
216 static void dvb_frontend_init(struct dvb_frontend *fe)
218 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
225 if (fe->ops.tuner_ops.init) {
226 if (fe->ops.i2c_gate_ctrl)
227 fe->ops.i2c_gate_ctrl(fe, 1);
228 fe->ops.tuner_ops.init(fe);
229 if (fe->ops.i2c_gate_ctrl)
230 fe->ops.i2c_gate_ctrl(fe, 0);
234 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
236 struct dvb_frontend_private *fepriv = fe->frontend_priv;
238 fepriv->reinitialise = 1;
239 dvb_frontend_wakeup(fe);
241 EXPORT_SYMBOL(dvb_frontend_reinitialise);
243 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
247 dprintk ("%s\n", __func__);
250 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
252 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
254 q2 = fepriv->quality - 128;
257 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
261 * Performs automatic twiddling of frontend parameters.
263 * @param fe The frontend concerned.
264 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
265 * @returns Number of complete iterations that have been performed.
267 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
271 struct dvb_frontend_private *fepriv = fe->frontend_priv;
272 int original_inversion = fepriv->parameters.inversion;
273 u32 original_frequency = fepriv->parameters.frequency;
275 /* are we using autoinversion? */
276 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
277 (fepriv->parameters.inversion == INVERSION_AUTO));
279 /* setup parameters correctly */
281 /* calculate the lnb_drift */
282 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
284 /* wrap the auto_step if we've exceeded the maximum drift */
285 if (fepriv->lnb_drift > fepriv->max_drift) {
286 fepriv->auto_step = 0;
287 fepriv->auto_sub_step = 0;
288 fepriv->lnb_drift = 0;
291 /* perform inversion and +/- zigzag */
292 switch(fepriv->auto_sub_step) {
294 /* try with the current inversion and current drift setting */
299 if (!autoinversion) break;
301 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
306 if (fepriv->lnb_drift == 0) break;
308 fepriv->lnb_drift = -fepriv->lnb_drift;
313 if (fepriv->lnb_drift == 0) break;
314 if (!autoinversion) break;
316 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
317 fepriv->lnb_drift = -fepriv->lnb_drift;
323 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
327 if (!ready) fepriv->auto_sub_step++;
330 /* if this attempt would hit where we started, indicate a complete
331 * iteration has occurred */
332 if ((fepriv->auto_step == fepriv->started_auto_step) &&
333 (fepriv->auto_sub_step == 0) && check_wrapped) {
337 dprintk("%s: drift:%i inversion:%i auto_step:%i "
338 "auto_sub_step:%i started_auto_step:%i\n",
339 __func__, fepriv->lnb_drift, fepriv->inversion,
340 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
342 /* set the frontend itself */
343 fepriv->parameters.frequency += fepriv->lnb_drift;
345 fepriv->parameters.inversion = fepriv->inversion;
346 if (fe->ops.set_frontend)
347 fe->ops.set_frontend(fe, &fepriv->parameters);
349 fepriv->parameters.frequency = original_frequency;
350 fepriv->parameters.inversion = original_inversion;
352 fepriv->auto_sub_step++;
356 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
359 struct dvb_frontend_private *fepriv = fe->frontend_priv;
361 /* if we've got no parameters, just keep idling */
362 if (fepriv->state & FESTATE_IDLE) {
363 fepriv->delay = 3*HZ;
368 /* in SCAN mode, we just set the frontend when asked and leave it alone */
369 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
370 if (fepriv->state & FESTATE_RETUNE) {
371 if (fe->ops.set_frontend)
372 fe->ops.set_frontend(fe, &fepriv->parameters);
373 fepriv->state = FESTATE_TUNED;
375 fepriv->delay = 3*HZ;
380 /* get the frontend status */
381 if (fepriv->state & FESTATE_RETUNE) {
384 if (fe->ops.read_status)
385 fe->ops.read_status(fe, &s);
386 if (s != fepriv->status) {
387 dvb_frontend_add_event(fe, s);
392 /* if we're not tuned, and we have a lock, move to the TUNED state */
393 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
394 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
395 fepriv->state = FESTATE_TUNED;
397 /* if we're tuned, then we have determined the correct inversion */
398 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
399 (fepriv->parameters.inversion == INVERSION_AUTO)) {
400 fepriv->parameters.inversion = fepriv->inversion;
405 /* if we are tuned already, check we're still locked */
406 if (fepriv->state & FESTATE_TUNED) {
407 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
409 /* we're tuned, and the lock is still good... */
410 if (s & FE_HAS_LOCK) {
412 } else { /* if we _WERE_ tuned, but now don't have a lock */
413 fepriv->state = FESTATE_ZIGZAG_FAST;
414 fepriv->started_auto_step = fepriv->auto_step;
415 fepriv->check_wrapped = 0;
419 /* don't actually do anything if we're in the LOSTLOCK state,
420 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
421 if ((fepriv->state & FESTATE_LOSTLOCK) &&
422 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
423 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
427 /* don't do anything if we're in the DISEQC state, since this
428 * might be someone with a motorized dish controlled by DISEQC.
429 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
430 if (fepriv->state & FESTATE_DISEQC) {
431 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
435 /* if we're in the RETUNE state, set everything up for a brand
436 * new scan, keeping the current inversion setting, as the next
437 * tune is _very_ likely to require the same */
438 if (fepriv->state & FESTATE_RETUNE) {
439 fepriv->lnb_drift = 0;
440 fepriv->auto_step = 0;
441 fepriv->auto_sub_step = 0;
442 fepriv->started_auto_step = 0;
443 fepriv->check_wrapped = 0;
447 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
448 fepriv->delay = fepriv->min_delay;
451 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
452 /* OK, if we've run out of trials at the fast speed.
453 * Drop back to slow for the _next_ attempt */
454 fepriv->state = FESTATE_SEARCHING_SLOW;
455 fepriv->started_auto_step = fepriv->auto_step;
458 fepriv->check_wrapped = 1;
460 /* if we've just retuned, enter the ZIGZAG_FAST state.
461 * This ensures we cannot return from an
462 * FE_SET_FRONTEND ioctl before the first frontend tune
464 if (fepriv->state & FESTATE_RETUNE) {
465 fepriv->state = FESTATE_TUNING_FAST;
470 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
471 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
473 /* Note: don't bother checking for wrapping; we stay in this
474 * state until we get a lock */
475 dvb_frontend_swzigzag_autotune(fe, 0);
479 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
481 struct dvb_frontend_private *fepriv = fe->frontend_priv;
486 if (fepriv->dvbdev->writers == 1)
487 if (time_after(jiffies, fepriv->release_jiffies +
488 dvb_shutdown_timeout * HZ))
494 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
496 struct dvb_frontend_private *fepriv = fe->frontend_priv;
498 if (fepriv->wakeup) {
502 return dvb_frontend_is_exiting(fe);
505 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
507 struct dvb_frontend_private *fepriv = fe->frontend_priv;
510 wake_up_interruptible(&fepriv->wait_queue);
513 static int dvb_frontend_thread(void *data)
515 struct dvb_frontend *fe = data;
516 struct dvb_frontend_private *fepriv = fe->frontend_priv;
517 unsigned long timeout;
519 struct dvb_frontend_parameters *params;
521 dprintk("%s\n", __func__);
523 fepriv->check_wrapped = 0;
525 fepriv->delay = 3*HZ;
528 fepriv->reinitialise = 0;
530 dvb_frontend_init(fe);
534 up(&fepriv->sem); /* is locked when we enter the thread... */
536 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
537 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
538 || freezing(current),
541 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
542 /* got signal or quitting */
549 if (down_interruptible(&fepriv->sem))
552 if (fepriv->reinitialise) {
553 dvb_frontend_init(fe);
554 if (fepriv->tone != -1) {
555 fe->ops.set_tone(fe, fepriv->tone);
557 if (fepriv->voltage != -1) {
558 fe->ops.set_voltage(fe, fepriv->voltage);
560 fepriv->reinitialise = 0;
563 /* do an iteration of the tuning loop */
564 if (fe->ops.get_frontend_algo) {
565 if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
566 /* have we been asked to retune? */
568 if (fepriv->state & FESTATE_RETUNE) {
569 params = &fepriv->parameters;
570 fepriv->state = FESTATE_TUNED;
573 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
574 if (s != fepriv->status) {
575 dvb_frontend_add_event(fe, s);
579 dvb_frontend_swzigzag(fe);
581 dvb_frontend_swzigzag(fe);
584 if (dvb_powerdown_on_sleep) {
585 if (fe->ops.set_voltage)
586 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
587 if (fe->ops.tuner_ops.sleep) {
588 fe->ops.tuner_ops.sleep(fe);
589 if (fe->ops.i2c_gate_ctrl)
590 fe->ops.i2c_gate_ctrl(fe, 0);
596 fepriv->thread = NULL;
599 dvb_frontend_wakeup(fe);
603 static void dvb_frontend_stop(struct dvb_frontend *fe)
605 struct dvb_frontend_private *fepriv = fe->frontend_priv;
607 dprintk ("%s\n", __func__);
615 kthread_stop(fepriv->thread);
617 init_MUTEX (&fepriv->sem);
618 fepriv->state = FESTATE_IDLE;
620 /* paranoia check in case a signal arrived */
622 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
626 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
628 return ((curtime.tv_usec < lasttime.tv_usec) ?
629 1000000 - lasttime.tv_usec + curtime.tv_usec :
630 curtime.tv_usec - lasttime.tv_usec);
632 EXPORT_SYMBOL(timeval_usec_diff);
634 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
636 curtime->tv_usec += add_usec;
637 if (curtime->tv_usec >= 1000000) {
638 curtime->tv_usec -= 1000000;
644 * Sleep until gettimeofday() > waketime + add_usec
645 * This needs to be as precise as possible, but as the delay is
646 * usually between 2ms and 32ms, it is done using a scheduled msleep
647 * followed by usleep (normally a busy-wait loop) for the remainder
649 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
651 struct timeval lasttime;
654 timeval_usec_add(waketime, add_usec);
656 do_gettimeofday(&lasttime);
657 delta = timeval_usec_diff(lasttime, *waketime);
659 msleep((delta - 1500) / 1000);
660 do_gettimeofday(&lasttime);
661 newdelta = timeval_usec_diff(lasttime, *waketime);
662 delta = (newdelta > delta) ? 0 : newdelta;
667 EXPORT_SYMBOL(dvb_frontend_sleep_until);
669 static int dvb_frontend_start(struct dvb_frontend *fe)
672 struct dvb_frontend_private *fepriv = fe->frontend_priv;
673 struct task_struct *fe_thread;
675 dprintk ("%s\n", __func__);
677 if (fepriv->thread) {
681 dvb_frontend_stop (fe);
684 if (signal_pending(current))
686 if (down_interruptible (&fepriv->sem))
689 fepriv->state = FESTATE_IDLE;
691 fepriv->thread = NULL;
694 fe_thread = kthread_run(dvb_frontend_thread, fe,
695 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
696 if (IS_ERR(fe_thread)) {
697 ret = PTR_ERR(fe_thread);
698 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
702 fepriv->thread = fe_thread;
706 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
707 u32 *freq_min, u32 *freq_max)
709 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
711 if (fe->ops.info.frequency_max == 0)
712 *freq_max = fe->ops.tuner_ops.info.frequency_max;
713 else if (fe->ops.tuner_ops.info.frequency_max == 0)
714 *freq_max = fe->ops.info.frequency_max;
716 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
718 if (*freq_min == 0 || *freq_max == 0)
719 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
720 fe->dvb->num,fe->id);
723 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
724 struct dvb_frontend_parameters *parms)
729 /* range check: frequency */
730 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
731 if ((freq_min && parms->frequency < freq_min) ||
732 (freq_max && parms->frequency > freq_max)) {
733 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
734 fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
738 /* range check: symbol rate */
739 if (fe->ops.info.type == FE_QPSK) {
740 if ((fe->ops.info.symbol_rate_min &&
741 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
742 (fe->ops.info.symbol_rate_max &&
743 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
744 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
745 fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
746 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
750 } else if (fe->ops.info.type == FE_QAM) {
751 if ((fe->ops.info.symbol_rate_min &&
752 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
753 (fe->ops.info.symbol_rate_max &&
754 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
755 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
756 fe->dvb->num, fe->id, parms->u.qam.symbol_rate,
757 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
765 struct dtv_cmds_h dtv_cmds[] = {
779 .name = "DTV_FREQUENCY",
780 .cmd = DTV_FREQUENCY,
783 [DTV_BANDWIDTH_HZ] = {
784 .name = "DTV_BANDWIDTH_HZ",
785 .cmd = DTV_BANDWIDTH_HZ,
789 .name = "DTV_MODULATION",
790 .cmd = DTV_MODULATION,
794 .name = "DTV_INVERSION",
795 .cmd = DTV_INVERSION,
798 [DTV_DISEQC_MASTER] = {
799 .name = "DTV_DISEQC_MASTER",
800 .cmd = DTV_DISEQC_MASTER,
804 [DTV_SYMBOL_RATE] = {
805 .name = "DTV_SYMBOL_RATE",
806 .cmd = DTV_SYMBOL_RATE,
810 .name = "DTV_INNER_FEC",
811 .cmd = DTV_INNER_FEC,
815 .name = "DTV_VOLTAGE",
830 .name = "DTV_ROLLOFF",
834 [DTV_DELIVERY_SYSTEM] = {
835 .name = "DTV_DELIVERY_SYSTEM",
836 .cmd = DTV_DELIVERY_SYSTEM,
840 .name = "DTV_HIERARCHY",
841 .cmd = DTV_HIERARCHY,
844 [DTV_CODE_RATE_HP] = {
845 .name = "DTV_CODE_RATE_HP",
846 .cmd = DTV_CODE_RATE_HP,
849 [DTV_CODE_RATE_LP] = {
850 .name = "DTV_CODE_RATE_LP",
851 .cmd = DTV_CODE_RATE_LP,
854 [DTV_GUARD_INTERVAL] = {
855 .name = "DTV_GUARD_INTERVAL",
856 .cmd = DTV_GUARD_INTERVAL,
859 [DTV_TRANSMISSION_MODE] = {
860 .name = "DTV_TRANSMISSION_MODE",
861 .cmd = DTV_TRANSMISSION_MODE,
865 [DTV_DISEQC_SLAVE_REPLY] = {
866 .name = "DTV_DISEQC_SLAVE_REPLY",
867 .cmd = DTV_DISEQC_SLAVE_REPLY,
871 [DTV_API_VERSION] = {
872 .name = "DTV_API_VERSION",
873 .cmd = DTV_API_VERSION,
876 [DTV_CODE_RATE_HP] = {
877 .name = "DTV_CODE_RATE_HP",
878 .cmd = DTV_CODE_RATE_HP,
881 [DTV_CODE_RATE_LP] = {
882 .name = "DTV_CODE_RATE_LP",
883 .cmd = DTV_CODE_RATE_LP,
886 [DTV_GUARD_INTERVAL] = {
887 .name = "DTV_GUARD_INTERVAL",
888 .cmd = DTV_GUARD_INTERVAL,
891 [DTV_TRANSMISSION_MODE] = {
892 .name = "DTV_TRANSMISSION_MODE",
893 .cmd = DTV_TRANSMISSION_MODE,
897 .name = "DTV_HIERARCHY",
898 .cmd = DTV_HIERARCHY,
903 void dtv_property_dump(struct dtv_property *tvp)
907 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
908 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
913 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
916 ,dtv_cmds[ tvp->cmd ].name);
918 if(dtv_cmds[ tvp->cmd ].buffer) {
920 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
924 for(i = 0; i < tvp->u.buffer.len; i++)
925 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
928 ,tvp->u.buffer.data[i]);
931 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
934 int is_legacy_delivery_system(fe_delivery_system_t s)
936 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
937 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
943 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
944 * drivers can use a single set_frontend tuning function, regardless of whether
945 * it's being used for the legacy or new API, reducing code and complexity.
947 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
949 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
951 c->frequency = p->frequency;
952 c->inversion = p->inversion;
954 switch (fe->ops.info.type) {
956 c->modulation = QPSK; /* implied for DVB-S in legacy API */
957 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
958 c->symbol_rate = p->u.qpsk.symbol_rate;
959 c->fec_inner = p->u.qpsk.fec_inner;
960 c->delivery_system = SYS_DVBS;
963 c->symbol_rate = p->u.qam.symbol_rate;
964 c->fec_inner = p->u.qam.fec_inner;
965 c->modulation = p->u.qam.modulation;
966 c->delivery_system = SYS_DVBC_ANNEX_AC;
969 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
970 c->bandwidth_hz = 6000000;
971 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
972 c->bandwidth_hz = 7000000;
973 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
974 c->bandwidth_hz = 8000000;
976 /* Including BANDWIDTH_AUTO */
978 c->code_rate_HP = p->u.ofdm.code_rate_HP;
979 c->code_rate_LP = p->u.ofdm.code_rate_LP;
980 c->modulation = p->u.ofdm.constellation;
981 c->transmission_mode = p->u.ofdm.transmission_mode;
982 c->guard_interval = p->u.ofdm.guard_interval;
983 c->hierarchy = p->u.ofdm.hierarchy_information;
984 c->delivery_system = SYS_DVBT;
987 c->modulation = p->u.vsb.modulation;
988 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
989 c->delivery_system = SYS_ATSC;
991 c->delivery_system = SYS_DVBC_ANNEX_B;
996 /* Ensure the cached values are set correctly in the frontend
997 * legacy tuning structures, for the advanced tuning API.
999 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1001 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1002 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1003 struct dvb_frontend_parameters *p = &fepriv->parameters;
1005 p->frequency = c->frequency;
1006 p->inversion = c->inversion;
1008 switch (fe->ops.info.type) {
1010 dprintk("%s() Preparing QPSK req\n", __func__);
1011 p->u.qpsk.symbol_rate = c->symbol_rate;
1012 p->u.qpsk.fec_inner = c->fec_inner;
1013 c->delivery_system = SYS_DVBS;
1016 dprintk("%s() Preparing QAM req\n", __func__);
1017 p->u.qam.symbol_rate = c->symbol_rate;
1018 p->u.qam.fec_inner = c->fec_inner;
1019 p->u.qam.modulation = c->modulation;
1020 c->delivery_system = SYS_DVBC_ANNEX_AC;
1023 dprintk("%s() Preparing OFDM req\n", __func__);
1024 if (c->bandwidth_hz == 6000000)
1025 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1026 else if (c->bandwidth_hz == 7000000)
1027 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1028 else if (c->bandwidth_hz == 8000000)
1029 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1031 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1032 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1033 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1034 p->u.ofdm.constellation = c->modulation;
1035 p->u.ofdm.transmission_mode = c->transmission_mode;
1036 p->u.ofdm.guard_interval = c->guard_interval;
1037 p->u.ofdm.hierarchy_information = c->hierarchy;
1038 c->delivery_system = SYS_DVBT;
1041 dprintk("%s() Preparing VSB req\n", __func__);
1042 p->u.vsb.modulation = c->modulation;
1043 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1044 c->delivery_system = SYS_ATSC;
1046 c->delivery_system = SYS_DVBC_ANNEX_B;
1051 /* Ensure the cached values are set correctly in the frontend
1052 * legacy tuning structures, for the legacy tuning API.
1054 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1056 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1057 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1058 struct dvb_frontend_parameters *p = &fepriv->parameters;
1060 p->frequency = c->frequency;
1061 p->inversion = c->inversion;
1063 switch(c->modulation) {
1068 p->u.qpsk.symbol_rate = c->symbol_rate;
1069 p->u.qpsk.fec_inner = c->fec_inner;
1075 if(c->delivery_system == SYS_ISDBT) {
1076 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1077 p->frequency = c->frequency;
1078 p->inversion = INVERSION_AUTO;
1079 p->u.ofdm.constellation = QAM_AUTO;
1080 p->u.ofdm.code_rate_HP = FEC_AUTO;
1081 p->u.ofdm.code_rate_LP = FEC_AUTO;
1082 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1083 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1084 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1085 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1089 void dtv_property_cache_submit(struct dvb_frontend *fe)
1091 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1093 /* For legacy delivery systems we don't need the delivery_system to
1094 * be specified, but we populate the older structures from the cache
1095 * so we can call set_frontend on older drivers.
1097 if(is_legacy_delivery_system(c->delivery_system)) {
1099 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1100 dtv_property_legacy_params_sync(fe);
1103 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1105 /* For advanced delivery systems / modulation types ...
1106 * we seed the lecacy dvb_frontend_parameters structure
1107 * so that the sanity checking code later in the IOCTL processing
1108 * can validate our basic frequency ranges, symbolrates, modulation
1111 dtv_property_adv_params_sync(fe);
1115 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1116 unsigned int cmd, void *parg);
1117 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1118 unsigned int cmd, void *parg);
1120 int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
1121 struct inode *inode, struct file *file)
1125 dtv_property_dump(tvp);
1127 /* Allow the frontend to validate incoming properties */
1128 if (fe->ops.get_property)
1129 r = fe->ops.get_property(fe, tvp);
1136 tvp->u.data = fe->dtv_property_cache.frequency;
1138 case DTV_MODULATION:
1139 tvp->u.data = fe->dtv_property_cache.modulation;
1141 case DTV_BANDWIDTH_HZ:
1142 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1145 tvp->u.data = fe->dtv_property_cache.inversion;
1147 case DTV_SYMBOL_RATE:
1148 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1151 tvp->u.data = fe->dtv_property_cache.fec_inner;
1154 tvp->u.data = fe->dtv_property_cache.pilot;
1157 tvp->u.data = fe->dtv_property_cache.rolloff;
1159 case DTV_DELIVERY_SYSTEM:
1160 tvp->u.data = fe->dtv_property_cache.delivery_system;
1163 tvp->u.data = fe->dtv_property_cache.voltage;
1166 tvp->u.data = fe->dtv_property_cache.sectone;
1168 case DTV_API_VERSION:
1169 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1171 case DTV_CODE_RATE_HP:
1172 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1174 case DTV_CODE_RATE_LP:
1175 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1177 case DTV_GUARD_INTERVAL:
1178 tvp->u.data = fe->dtv_property_cache.guard_interval;
1180 case DTV_TRANSMISSION_MODE:
1181 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1184 tvp->u.data = fe->dtv_property_cache.hierarchy;
1193 int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
1194 struct inode *inode, struct file *file)
1197 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1198 dtv_property_dump(tvp);
1200 /* Allow the frontend to validate incoming properties */
1201 if (fe->ops.set_property)
1202 r = fe->ops.set_property(fe, tvp);
1209 /* Reset a cache of data specific to the frontend here. This does
1210 * not effect hardware.
1212 dprintk("%s() Flushing property cache\n", __func__);
1213 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1214 fe->dtv_property_cache.state = tvp->cmd;
1215 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1218 /* interpret the cache of data, build either a traditional frontend
1219 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1222 fe->dtv_property_cache.state = tvp->cmd;
1223 dprintk("%s() Finalised property cache\n", __func__);
1224 dtv_property_cache_submit(fe);
1226 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1227 &fepriv->parameters);
1230 fe->dtv_property_cache.frequency = tvp->u.data;
1232 case DTV_MODULATION:
1233 fe->dtv_property_cache.modulation = tvp->u.data;
1235 case DTV_BANDWIDTH_HZ:
1236 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1239 fe->dtv_property_cache.inversion = tvp->u.data;
1241 case DTV_SYMBOL_RATE:
1242 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1245 fe->dtv_property_cache.fec_inner = tvp->u.data;
1248 fe->dtv_property_cache.pilot = tvp->u.data;
1251 fe->dtv_property_cache.rolloff = tvp->u.data;
1253 case DTV_DELIVERY_SYSTEM:
1254 fe->dtv_property_cache.delivery_system = tvp->u.data;
1257 fe->dtv_property_cache.voltage = tvp->u.data;
1258 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1259 (void *)fe->dtv_property_cache.voltage);
1262 fe->dtv_property_cache.sectone = tvp->u.data;
1263 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1264 (void *)fe->dtv_property_cache.sectone);
1266 case DTV_CODE_RATE_HP:
1267 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1269 case DTV_CODE_RATE_LP:
1270 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1272 case DTV_GUARD_INTERVAL:
1273 fe->dtv_property_cache.guard_interval = tvp->u.data;
1275 case DTV_TRANSMISSION_MODE:
1276 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1279 fe->dtv_property_cache.hierarchy = tvp->u.data;
1288 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1289 unsigned int cmd, void *parg)
1291 struct dvb_device *dvbdev = file->private_data;
1292 struct dvb_frontend *fe = dvbdev->priv;
1293 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1294 int err = -EOPNOTSUPP;
1296 dprintk ("%s\n", __func__);
1301 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1302 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1303 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1306 if (down_interruptible (&fepriv->sem))
1307 return -ERESTARTSYS;
1309 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1310 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1312 fe->dtv_property_cache.state = DTV_UNDEFINED;
1313 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1320 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1321 unsigned int cmd, void *parg)
1323 struct dvb_device *dvbdev = file->private_data;
1324 struct dvb_frontend *fe = dvbdev->priv;
1327 struct dtv_properties *tvps = NULL;
1328 struct dtv_property *tvp = NULL;
1331 dprintk("%s\n", __func__);
1333 if(cmd == FE_SET_PROPERTY) {
1334 tvps = (struct dtv_properties __user *)parg;
1336 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1337 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1339 /* Put an arbitrary limit on the number of messages that can
1340 * be sent at once */
1341 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1344 tvp = (struct dtv_property *) kmalloc(tvps->num *
1345 sizeof(struct dtv_property), GFP_KERNEL);
1351 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1356 for (i = 0; i < tvps->num; i++) {
1357 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1358 err |= (tvp + i)->result;
1361 if(fe->dtv_property_cache.state == DTV_TUNE)
1362 dprintk("%s() Property cache is full, tuning\n", __func__);
1365 if(cmd == FE_GET_PROPERTY) {
1367 tvps = (struct dtv_properties __user *)parg;
1369 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1370 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1372 /* Put an arbitrary limit on the number of messages that can
1373 * be sent at once */
1374 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1377 tvp = (struct dtv_property *) kmalloc(tvps->num *
1378 sizeof(struct dtv_property), GFP_KERNEL);
1384 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1389 for (i = 0; i < tvps->num; i++) {
1390 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1391 err |= (tvp + i)->result;
1394 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1407 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1408 unsigned int cmd, void *parg)
1410 struct dvb_device *dvbdev = file->private_data;
1411 struct dvb_frontend *fe = dvbdev->priv;
1412 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1413 int err = -EOPNOTSUPP;
1417 struct dvb_frontend_info* info = parg;
1418 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1419 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1421 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1422 * do it, it is done for it. */
1423 info->caps |= FE_CAN_INVERSION_AUTO;
1428 case FE_READ_STATUS: {
1429 fe_status_t* status = parg;
1431 /* if retune was requested but hasn't occured yet, prevent
1432 * that user get signal state from previous tuning */
1433 if(fepriv->state == FESTATE_RETUNE) {
1439 if (fe->ops.read_status)
1440 err = fe->ops.read_status(fe, status);
1444 if (fe->ops.read_ber)
1445 err = fe->ops.read_ber(fe, (__u32*) parg);
1448 case FE_READ_SIGNAL_STRENGTH:
1449 if (fe->ops.read_signal_strength)
1450 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1454 if (fe->ops.read_snr)
1455 err = fe->ops.read_snr(fe, (__u16*) parg);
1458 case FE_READ_UNCORRECTED_BLOCKS:
1459 if (fe->ops.read_ucblocks)
1460 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1464 case FE_DISEQC_RESET_OVERLOAD:
1465 if (fe->ops.diseqc_reset_overload) {
1466 err = fe->ops.diseqc_reset_overload(fe);
1467 fepriv->state = FESTATE_DISEQC;
1472 case FE_DISEQC_SEND_MASTER_CMD:
1473 if (fe->ops.diseqc_send_master_cmd) {
1474 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1475 fepriv->state = FESTATE_DISEQC;
1480 case FE_DISEQC_SEND_BURST:
1481 if (fe->ops.diseqc_send_burst) {
1482 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1483 fepriv->state = FESTATE_DISEQC;
1489 if (fe->ops.set_tone) {
1490 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1491 fepriv->tone = (fe_sec_tone_mode_t) parg;
1492 fepriv->state = FESTATE_DISEQC;
1497 case FE_SET_VOLTAGE:
1498 if (fe->ops.set_voltage) {
1499 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1500 fepriv->voltage = (fe_sec_voltage_t) parg;
1501 fepriv->state = FESTATE_DISEQC;
1506 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1507 if (fe->ops.dishnetwork_send_legacy_command) {
1508 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1509 fepriv->state = FESTATE_DISEQC;
1511 } else if (fe->ops.set_voltage) {
1513 * NOTE: This is a fallback condition. Some frontends
1514 * (stv0299 for instance) take longer than 8msec to
1515 * respond to a set_voltage command. Those switches
1516 * need custom routines to switch properly. For all
1517 * other frontends, the following shoule work ok.
1518 * Dish network legacy switches (as used by Dish500)
1519 * are controlled by sending 9-bit command words
1520 * spaced 8msec apart.
1521 * the actual command word is switch/port dependant
1522 * so it is up to the userspace application to send
1523 * the right command.
1524 * The command must always start with a '0' after
1525 * initialization, so parg is 8 bits and does not
1526 * include the initialization or start bit
1528 unsigned long swcmd = ((unsigned long) parg) << 1;
1529 struct timeval nexttime;
1530 struct timeval tv[10];
1533 if (dvb_frontend_debug)
1534 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1535 do_gettimeofday(&nexttime);
1536 if (dvb_frontend_debug)
1537 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1538 /* before sending a command, initialize by sending
1539 * a 32ms 18V to the switch
1541 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1542 dvb_frontend_sleep_until(&nexttime, 32000);
1544 for (i = 0; i < 9; i++) {
1545 if (dvb_frontend_debug)
1546 do_gettimeofday(&tv[i + 1]);
1547 if ((swcmd & 0x01) != last) {
1548 /* set voltage to (last ? 13V : 18V) */
1549 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1550 last = (last) ? 0 : 1;
1554 dvb_frontend_sleep_until(&nexttime, 8000);
1556 if (dvb_frontend_debug) {
1557 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1558 __func__, fe->dvb->num);
1559 for (i = 1; i < 10; i++)
1560 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1563 fepriv->state = FESTATE_DISEQC;
1568 case FE_DISEQC_RECV_SLAVE_REPLY:
1569 if (fe->ops.diseqc_recv_slave_reply)
1570 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1573 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1574 if (fe->ops.enable_high_lnb_voltage)
1575 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1578 case FE_SET_FRONTEND: {
1579 struct dvb_frontend_tune_settings fetunesettings;
1581 if(fe->dtv_property_cache.state == DTV_TUNE) {
1582 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1587 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1592 memcpy (&fepriv->parameters, parg,
1593 sizeof (struct dvb_frontend_parameters));
1594 dtv_property_cache_sync(fe, &fepriv->parameters);
1597 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1598 memcpy(&fetunesettings.parameters, parg,
1599 sizeof (struct dvb_frontend_parameters));
1601 /* force auto frequency inversion if requested */
1602 if (dvb_force_auto_inversion) {
1603 fepriv->parameters.inversion = INVERSION_AUTO;
1604 fetunesettings.parameters.inversion = INVERSION_AUTO;
1606 if (fe->ops.info.type == FE_OFDM) {
1607 /* without hierarchical coding code_rate_LP is irrelevant,
1608 * so we tolerate the otherwise invalid FEC_NONE setting */
1609 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1610 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1611 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1614 /* get frontend-specific tuning settings */
1615 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1616 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1617 fepriv->max_drift = fetunesettings.max_drift;
1618 fepriv->step_size = fetunesettings.step_size;
1620 /* default values */
1621 switch(fe->ops.info.type) {
1623 fepriv->min_delay = HZ/20;
1624 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1625 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1629 fepriv->min_delay = HZ/20;
1630 fepriv->step_size = 0; /* no zigzag */
1631 fepriv->max_drift = 0;
1635 fepriv->min_delay = HZ/20;
1636 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1637 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1640 fepriv->min_delay = HZ/20;
1641 fepriv->step_size = 0;
1642 fepriv->max_drift = 0;
1646 if (dvb_override_tune_delay > 0)
1647 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1649 fepriv->state = FESTATE_RETUNE;
1650 dvb_frontend_wakeup(fe);
1651 dvb_frontend_add_event(fe, 0);
1658 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1661 case FE_GET_FRONTEND:
1662 if (fe->ops.get_frontend) {
1663 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1664 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1668 case FE_SET_FRONTEND_TUNE_MODE:
1669 fepriv->tune_mode_flags = (unsigned long) parg;
1678 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1680 struct dvb_device *dvbdev = file->private_data;
1681 struct dvb_frontend *fe = dvbdev->priv;
1682 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1684 dprintk ("%s\n", __func__);
1686 poll_wait (file, &fepriv->events.wait_queue, wait);
1688 if (fepriv->events.eventw != fepriv->events.eventr)
1689 return (POLLIN | POLLRDNORM | POLLPRI);
1694 static int dvb_frontend_open(struct inode *inode, struct file *file)
1696 struct dvb_device *dvbdev = file->private_data;
1697 struct dvb_frontend *fe = dvbdev->priv;
1698 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1699 struct dvb_adapter *adapter = fe->dvb;
1702 dprintk ("%s\n", __func__);
1704 if (adapter->mfe_shared) {
1705 mutex_lock (&adapter->mfe_lock);
1707 if (adapter->mfe_dvbdev == NULL)
1708 adapter->mfe_dvbdev = dvbdev;
1710 else if (adapter->mfe_dvbdev != dvbdev) {
1712 *mfedev = adapter->mfe_dvbdev;
1714 *mfe = mfedev->priv;
1715 struct dvb_frontend_private
1716 *mfepriv = mfe->frontend_priv;
1717 int mferetry = (dvb_mfe_wait_time << 1);
1719 mutex_unlock (&adapter->mfe_lock);
1720 while (mferetry-- && (mfedev->users != -1 ||
1721 mfepriv->thread != NULL)) {
1722 if(msleep_interruptible(500)) {
1723 if(signal_pending(current))
1728 mutex_lock (&adapter->mfe_lock);
1729 if(adapter->mfe_dvbdev != dvbdev) {
1730 mfedev = adapter->mfe_dvbdev;
1732 mfepriv = mfe->frontend_priv;
1733 if (mfedev->users != -1 ||
1734 mfepriv->thread != NULL) {
1735 mutex_unlock (&adapter->mfe_lock);
1738 adapter->mfe_dvbdev = dvbdev;
1743 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1744 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1748 if ((ret = dvb_generic_open (inode, file)) < 0)
1751 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1752 /* normal tune mode when opened R/W */
1753 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1755 fepriv->voltage = -1;
1757 ret = dvb_frontend_start (fe);
1761 /* empty event queue */
1762 fepriv->events.eventr = fepriv->events.eventw = 0;
1765 if (adapter->mfe_shared)
1766 mutex_unlock (&adapter->mfe_lock);
1770 dvb_generic_release(inode, file);
1772 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1773 fe->ops.ts_bus_ctrl(fe, 0);
1775 if (adapter->mfe_shared)
1776 mutex_unlock (&adapter->mfe_lock);
1780 static int dvb_frontend_release(struct inode *inode, struct file *file)
1782 struct dvb_device *dvbdev = file->private_data;
1783 struct dvb_frontend *fe = dvbdev->priv;
1784 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1787 dprintk ("%s\n", __func__);
1789 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1790 fepriv->release_jiffies = jiffies;
1792 ret = dvb_generic_release (inode, file);
1794 if (dvbdev->users == -1) {
1795 if (fepriv->exit == 1) {
1796 fops_put(file->f_op);
1798 wake_up(&dvbdev->wait_queue);
1800 if (fe->ops.ts_bus_ctrl)
1801 fe->ops.ts_bus_ctrl(fe, 0);
1807 static struct file_operations dvb_frontend_fops = {
1808 .owner = THIS_MODULE,
1809 .ioctl = dvb_generic_ioctl,
1810 .poll = dvb_frontend_poll,
1811 .open = dvb_frontend_open,
1812 .release = dvb_frontend_release
1815 int dvb_register_frontend(struct dvb_adapter* dvb,
1816 struct dvb_frontend* fe)
1818 struct dvb_frontend_private *fepriv;
1819 static const struct dvb_device dvbdev_template = {
1823 .fops = &dvb_frontend_fops,
1824 .kernel_ioctl = dvb_frontend_ioctl
1827 dprintk ("%s\n", __func__);
1829 if (mutex_lock_interruptible(&frontend_mutex))
1830 return -ERESTARTSYS;
1832 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1833 if (fe->frontend_priv == NULL) {
1834 mutex_unlock(&frontend_mutex);
1837 fepriv = fe->frontend_priv;
1839 init_MUTEX (&fepriv->sem);
1840 init_waitqueue_head (&fepriv->wait_queue);
1841 init_waitqueue_head (&fepriv->events.wait_queue);
1842 mutex_init(&fepriv->events.mtx);
1844 fepriv->inversion = INVERSION_OFF;
1846 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1851 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1852 fe, DVB_DEVICE_FRONTEND);
1854 mutex_unlock(&frontend_mutex);
1857 EXPORT_SYMBOL(dvb_register_frontend);
1859 int dvb_unregister_frontend(struct dvb_frontend* fe)
1861 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1862 dprintk ("%s\n", __func__);
1864 mutex_lock(&frontend_mutex);
1865 dvb_frontend_stop (fe);
1866 mutex_unlock(&frontend_mutex);
1868 if (fepriv->dvbdev->users < -1)
1869 wait_event(fepriv->dvbdev->wait_queue,
1870 fepriv->dvbdev->users==-1);
1872 mutex_lock(&frontend_mutex);
1873 dvb_unregister_device (fepriv->dvbdev);
1875 /* fe is invalid now */
1877 mutex_unlock(&frontend_mutex);
1880 EXPORT_SYMBOL(dvb_unregister_frontend);
1882 #ifdef CONFIG_MEDIA_ATTACH
1883 void dvb_frontend_detach(struct dvb_frontend* fe)
1887 if (fe->ops.release_sec) {
1888 fe->ops.release_sec(fe);
1889 symbol_put_addr(fe->ops.release_sec);
1891 if (fe->ops.tuner_ops.release) {
1892 fe->ops.tuner_ops.release(fe);
1893 symbol_put_addr(fe->ops.tuner_ops.release);
1895 if (fe->ops.analog_ops.release) {
1896 fe->ops.analog_ops.release(fe);
1897 symbol_put_addr(fe->ops.analog_ops.release);
1899 ptr = (void*)fe->ops.release;
1901 fe->ops.release(fe);
1902 symbol_put_addr(ptr);
1906 void dvb_frontend_detach(struct dvb_frontend* fe)
1908 if (fe->ops.release_sec)
1909 fe->ops.release_sec(fe);
1910 if (fe->ops.tuner_ops.release)
1911 fe->ops.tuner_ops.release(fe);
1912 if (fe->ops.analog_ops.release)
1913 fe->ops.analog_ops.release(fe);
1914 if (fe->ops.release)
1915 fe->ops.release(fe);
1918 EXPORT_SYMBOL(dvb_frontend_detach);