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 fe->ops.tuner_ops.init(fe);
227 if (fe->ops.i2c_gate_ctrl)
228 fe->ops.i2c_gate_ctrl(fe, 0);
232 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
234 struct dvb_frontend_private *fepriv = fe->frontend_priv;
236 fepriv->reinitialise = 1;
237 dvb_frontend_wakeup(fe);
239 EXPORT_SYMBOL(dvb_frontend_reinitialise);
241 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
245 dprintk ("%s\n", __func__);
248 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
250 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
252 q2 = fepriv->quality - 128;
255 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
259 * Performs automatic twiddling of frontend parameters.
261 * @param fe The frontend concerned.
262 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
263 * @returns Number of complete iterations that have been performed.
265 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
269 struct dvb_frontend_private *fepriv = fe->frontend_priv;
270 int original_inversion = fepriv->parameters.inversion;
271 u32 original_frequency = fepriv->parameters.frequency;
273 /* are we using autoinversion? */
274 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
275 (fepriv->parameters.inversion == INVERSION_AUTO));
277 /* setup parameters correctly */
279 /* calculate the lnb_drift */
280 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
282 /* wrap the auto_step if we've exceeded the maximum drift */
283 if (fepriv->lnb_drift > fepriv->max_drift) {
284 fepriv->auto_step = 0;
285 fepriv->auto_sub_step = 0;
286 fepriv->lnb_drift = 0;
289 /* perform inversion and +/- zigzag */
290 switch(fepriv->auto_sub_step) {
292 /* try with the current inversion and current drift setting */
297 if (!autoinversion) break;
299 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
304 if (fepriv->lnb_drift == 0) break;
306 fepriv->lnb_drift = -fepriv->lnb_drift;
311 if (fepriv->lnb_drift == 0) break;
312 if (!autoinversion) break;
314 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
315 fepriv->lnb_drift = -fepriv->lnb_drift;
321 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
325 if (!ready) fepriv->auto_sub_step++;
328 /* if this attempt would hit where we started, indicate a complete
329 * iteration has occurred */
330 if ((fepriv->auto_step == fepriv->started_auto_step) &&
331 (fepriv->auto_sub_step == 0) && check_wrapped) {
335 dprintk("%s: drift:%i inversion:%i auto_step:%i "
336 "auto_sub_step:%i started_auto_step:%i\n",
337 __func__, fepriv->lnb_drift, fepriv->inversion,
338 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
340 /* set the frontend itself */
341 fepriv->parameters.frequency += fepriv->lnb_drift;
343 fepriv->parameters.inversion = fepriv->inversion;
344 if (fe->ops.set_frontend)
345 fe->ops.set_frontend(fe, &fepriv->parameters);
347 fepriv->parameters.frequency = original_frequency;
348 fepriv->parameters.inversion = original_inversion;
350 fepriv->auto_sub_step++;
354 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
357 struct dvb_frontend_private *fepriv = fe->frontend_priv;
359 /* if we've got no parameters, just keep idling */
360 if (fepriv->state & FESTATE_IDLE) {
361 fepriv->delay = 3*HZ;
366 /* in SCAN mode, we just set the frontend when asked and leave it alone */
367 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
368 if (fepriv->state & FESTATE_RETUNE) {
369 if (fe->ops.set_frontend)
370 fe->ops.set_frontend(fe, &fepriv->parameters);
371 fepriv->state = FESTATE_TUNED;
373 fepriv->delay = 3*HZ;
378 /* get the frontend status */
379 if (fepriv->state & FESTATE_RETUNE) {
382 if (fe->ops.read_status)
383 fe->ops.read_status(fe, &s);
384 if (s != fepriv->status) {
385 dvb_frontend_add_event(fe, s);
390 /* if we're not tuned, and we have a lock, move to the TUNED state */
391 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
392 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
393 fepriv->state = FESTATE_TUNED;
395 /* if we're tuned, then we have determined the correct inversion */
396 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
397 (fepriv->parameters.inversion == INVERSION_AUTO)) {
398 fepriv->parameters.inversion = fepriv->inversion;
403 /* if we are tuned already, check we're still locked */
404 if (fepriv->state & FESTATE_TUNED) {
405 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
407 /* we're tuned, and the lock is still good... */
408 if (s & FE_HAS_LOCK) {
410 } else { /* if we _WERE_ tuned, but now don't have a lock */
411 fepriv->state = FESTATE_ZIGZAG_FAST;
412 fepriv->started_auto_step = fepriv->auto_step;
413 fepriv->check_wrapped = 0;
417 /* don't actually do anything if we're in the LOSTLOCK state,
418 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
419 if ((fepriv->state & FESTATE_LOSTLOCK) &&
420 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
421 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
425 /* don't do anything if we're in the DISEQC state, since this
426 * might be someone with a motorized dish controlled by DISEQC.
427 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
428 if (fepriv->state & FESTATE_DISEQC) {
429 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
433 /* if we're in the RETUNE state, set everything up for a brand
434 * new scan, keeping the current inversion setting, as the next
435 * tune is _very_ likely to require the same */
436 if (fepriv->state & FESTATE_RETUNE) {
437 fepriv->lnb_drift = 0;
438 fepriv->auto_step = 0;
439 fepriv->auto_sub_step = 0;
440 fepriv->started_auto_step = 0;
441 fepriv->check_wrapped = 0;
445 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
446 fepriv->delay = fepriv->min_delay;
449 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
450 /* OK, if we've run out of trials at the fast speed.
451 * Drop back to slow for the _next_ attempt */
452 fepriv->state = FESTATE_SEARCHING_SLOW;
453 fepriv->started_auto_step = fepriv->auto_step;
456 fepriv->check_wrapped = 1;
458 /* if we've just retuned, enter the ZIGZAG_FAST state.
459 * This ensures we cannot return from an
460 * FE_SET_FRONTEND ioctl before the first frontend tune
462 if (fepriv->state & FESTATE_RETUNE) {
463 fepriv->state = FESTATE_TUNING_FAST;
468 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
469 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
471 /* Note: don't bother checking for wrapping; we stay in this
472 * state until we get a lock */
473 dvb_frontend_swzigzag_autotune(fe, 0);
477 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
479 struct dvb_frontend_private *fepriv = fe->frontend_priv;
484 if (fepriv->dvbdev->writers == 1)
485 if (time_after(jiffies, fepriv->release_jiffies +
486 dvb_shutdown_timeout * HZ))
492 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
494 struct dvb_frontend_private *fepriv = fe->frontend_priv;
496 if (fepriv->wakeup) {
500 return dvb_frontend_is_exiting(fe);
503 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
505 struct dvb_frontend_private *fepriv = fe->frontend_priv;
508 wake_up_interruptible(&fepriv->wait_queue);
511 static int dvb_frontend_thread(void *data)
513 struct dvb_frontend *fe = data;
514 struct dvb_frontend_private *fepriv = fe->frontend_priv;
515 unsigned long timeout;
517 struct dvb_frontend_parameters *params;
519 dprintk("%s\n", __func__);
521 fepriv->check_wrapped = 0;
523 fepriv->delay = 3*HZ;
526 fepriv->reinitialise = 0;
528 dvb_frontend_init(fe);
532 up(&fepriv->sem); /* is locked when we enter the thread... */
534 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
535 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
536 || freezing(current),
539 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
540 /* got signal or quitting */
547 if (down_interruptible(&fepriv->sem))
550 if (fepriv->reinitialise) {
551 dvb_frontend_init(fe);
552 if (fepriv->tone != -1) {
553 fe->ops.set_tone(fe, fepriv->tone);
555 if (fepriv->voltage != -1) {
556 fe->ops.set_voltage(fe, fepriv->voltage);
558 fepriv->reinitialise = 0;
561 /* do an iteration of the tuning loop */
562 if (fe->ops.get_frontend_algo) {
563 if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
564 /* have we been asked to retune? */
566 if (fepriv->state & FESTATE_RETUNE) {
567 params = &fepriv->parameters;
568 fepriv->state = FESTATE_TUNED;
571 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
572 if (s != fepriv->status) {
573 dvb_frontend_add_event(fe, s);
577 dvb_frontend_swzigzag(fe);
579 dvb_frontend_swzigzag(fe);
582 if (dvb_powerdown_on_sleep) {
583 if (fe->ops.set_voltage)
584 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
585 if (fe->ops.tuner_ops.sleep) {
586 fe->ops.tuner_ops.sleep(fe);
587 if (fe->ops.i2c_gate_ctrl)
588 fe->ops.i2c_gate_ctrl(fe, 0);
594 fepriv->thread = NULL;
597 dvb_frontend_wakeup(fe);
601 static void dvb_frontend_stop(struct dvb_frontend *fe)
603 struct dvb_frontend_private *fepriv = fe->frontend_priv;
605 dprintk ("%s\n", __func__);
613 kthread_stop(fepriv->thread);
615 init_MUTEX (&fepriv->sem);
616 fepriv->state = FESTATE_IDLE;
618 /* paranoia check in case a signal arrived */
620 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
624 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
626 return ((curtime.tv_usec < lasttime.tv_usec) ?
627 1000000 - lasttime.tv_usec + curtime.tv_usec :
628 curtime.tv_usec - lasttime.tv_usec);
630 EXPORT_SYMBOL(timeval_usec_diff);
632 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
634 curtime->tv_usec += add_usec;
635 if (curtime->tv_usec >= 1000000) {
636 curtime->tv_usec -= 1000000;
642 * Sleep until gettimeofday() > waketime + add_usec
643 * This needs to be as precise as possible, but as the delay is
644 * usually between 2ms and 32ms, it is done using a scheduled msleep
645 * followed by usleep (normally a busy-wait loop) for the remainder
647 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
649 struct timeval lasttime;
652 timeval_usec_add(waketime, add_usec);
654 do_gettimeofday(&lasttime);
655 delta = timeval_usec_diff(lasttime, *waketime);
657 msleep((delta - 1500) / 1000);
658 do_gettimeofday(&lasttime);
659 newdelta = timeval_usec_diff(lasttime, *waketime);
660 delta = (newdelta > delta) ? 0 : newdelta;
665 EXPORT_SYMBOL(dvb_frontend_sleep_until);
667 static int dvb_frontend_start(struct dvb_frontend *fe)
670 struct dvb_frontend_private *fepriv = fe->frontend_priv;
671 struct task_struct *fe_thread;
673 dprintk ("%s\n", __func__);
675 if (fepriv->thread) {
679 dvb_frontend_stop (fe);
682 if (signal_pending(current))
684 if (down_interruptible (&fepriv->sem))
687 fepriv->state = FESTATE_IDLE;
689 fepriv->thread = NULL;
692 fe_thread = kthread_run(dvb_frontend_thread, fe,
693 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
694 if (IS_ERR(fe_thread)) {
695 ret = PTR_ERR(fe_thread);
696 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
700 fepriv->thread = fe_thread;
704 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
705 u32 *freq_min, u32 *freq_max)
707 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
709 if (fe->ops.info.frequency_max == 0)
710 *freq_max = fe->ops.tuner_ops.info.frequency_max;
711 else if (fe->ops.tuner_ops.info.frequency_max == 0)
712 *freq_max = fe->ops.info.frequency_max;
714 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
716 if (*freq_min == 0 || *freq_max == 0)
717 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
718 fe->dvb->num,fe->id);
721 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
722 struct dvb_frontend_parameters *parms)
727 /* range check: frequency */
728 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
729 if ((freq_min && parms->frequency < freq_min) ||
730 (freq_max && parms->frequency > freq_max)) {
731 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
732 fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
736 /* range check: symbol rate */
737 if (fe->ops.info.type == FE_QPSK) {
738 if ((fe->ops.info.symbol_rate_min &&
739 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
740 (fe->ops.info.symbol_rate_max &&
741 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
742 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
743 fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
744 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
748 } else if (fe->ops.info.type == FE_QAM) {
749 if ((fe->ops.info.symbol_rate_min &&
750 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
751 (fe->ops.info.symbol_rate_max &&
752 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
753 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
754 fe->dvb->num, fe->id, parms->u.qam.symbol_rate,
755 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
763 struct dtv_cmds_h dtv_cmds[] = {
777 .name = "DTV_FREQUENCY",
778 .cmd = DTV_FREQUENCY,
781 [DTV_BANDWIDTH_HZ] = {
782 .name = "DTV_BANDWIDTH_HZ",
783 .cmd = DTV_BANDWIDTH_HZ,
787 .name = "DTV_MODULATION",
788 .cmd = DTV_MODULATION,
792 .name = "DTV_INVERSION",
793 .cmd = DTV_INVERSION,
796 [DTV_DISEQC_MASTER] = {
797 .name = "DTV_DISEQC_MASTER",
798 .cmd = DTV_DISEQC_MASTER,
802 [DTV_SYMBOL_RATE] = {
803 .name = "DTV_SYMBOL_RATE",
804 .cmd = DTV_SYMBOL_RATE,
808 .name = "DTV_INNER_FEC",
809 .cmd = DTV_INNER_FEC,
813 .name = "DTV_VOLTAGE",
828 .name = "DTV_ROLLOFF",
832 [DTV_DELIVERY_SYSTEM] = {
833 .name = "DTV_DELIVERY_SYSTEM",
834 .cmd = DTV_DELIVERY_SYSTEM,
838 .name = "DTV_HIERARCHY",
839 .cmd = DTV_HIERARCHY,
842 [DTV_CODE_RATE_HP] = {
843 .name = "DTV_CODE_RATE_HP",
844 .cmd = DTV_CODE_RATE_HP,
847 [DTV_CODE_RATE_LP] = {
848 .name = "DTV_CODE_RATE_LP",
849 .cmd = DTV_CODE_RATE_LP,
852 [DTV_GUARD_INTERVAL] = {
853 .name = "DTV_GUARD_INTERVAL",
854 .cmd = DTV_GUARD_INTERVAL,
857 [DTV_TRANSMISSION_MODE] = {
858 .name = "DTV_TRANSMISSION_MODE",
859 .cmd = DTV_TRANSMISSION_MODE,
863 [DTV_DISEQC_SLAVE_REPLY] = {
864 .name = "DTV_DISEQC_SLAVE_REPLY",
865 .cmd = DTV_DISEQC_SLAVE_REPLY,
869 [DTV_API_VERSION] = {
870 .name = "DTV_API_VERSION",
871 .cmd = DTV_API_VERSION,
874 [DTV_CODE_RATE_HP] = {
875 .name = "DTV_CODE_RATE_HP",
876 .cmd = DTV_CODE_RATE_HP,
879 [DTV_CODE_RATE_LP] = {
880 .name = "DTV_CODE_RATE_LP",
881 .cmd = DTV_CODE_RATE_LP,
884 [DTV_GUARD_INTERVAL] = {
885 .name = "DTV_GUARD_INTERVAL",
886 .cmd = DTV_GUARD_INTERVAL,
889 [DTV_TRANSMISSION_MODE] = {
890 .name = "DTV_TRANSMISSION_MODE",
891 .cmd = DTV_TRANSMISSION_MODE,
895 .name = "DTV_HIERARCHY",
896 .cmd = DTV_HIERARCHY,
901 void dtv_property_dump(struct dtv_property *tvp)
905 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
906 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
911 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
914 ,dtv_cmds[ tvp->cmd ].name);
916 if(dtv_cmds[ tvp->cmd ].buffer) {
918 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
922 for(i = 0; i < tvp->u.buffer.len; i++)
923 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
926 ,tvp->u.buffer.data[i]);
929 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
932 int is_legacy_delivery_system(fe_delivery_system_t s)
934 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
935 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
941 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
942 * drivers can use a single set_frontend tuning function, regardless of whether
943 * it's being used for the legacy or new API, reducing code and complexity.
945 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
947 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
949 c->frequency = p->frequency;
950 c->inversion = p->inversion;
952 switch (fe->ops.info.type) {
954 c->modulation = QPSK; /* implied for DVB-S in legacy API */
955 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
956 c->symbol_rate = p->u.qpsk.symbol_rate;
957 c->fec_inner = p->u.qpsk.fec_inner;
958 c->delivery_system = SYS_DVBS;
961 c->symbol_rate = p->u.qam.symbol_rate;
962 c->fec_inner = p->u.qam.fec_inner;
963 c->modulation = p->u.qam.modulation;
964 c->delivery_system = SYS_DVBC_ANNEX_AC;
967 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
968 c->bandwidth_hz = 6000000;
969 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
970 c->bandwidth_hz = 7000000;
971 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
972 c->bandwidth_hz = 8000000;
974 /* Including BANDWIDTH_AUTO */
976 c->code_rate_HP = p->u.ofdm.code_rate_HP;
977 c->code_rate_LP = p->u.ofdm.code_rate_LP;
978 c->modulation = p->u.ofdm.constellation;
979 c->transmission_mode = p->u.ofdm.transmission_mode;
980 c->guard_interval = p->u.ofdm.guard_interval;
981 c->hierarchy = p->u.ofdm.hierarchy_information;
982 c->delivery_system = SYS_DVBT;
985 c->modulation = p->u.vsb.modulation;
986 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
987 c->delivery_system = SYS_ATSC;
989 c->delivery_system = SYS_DVBC_ANNEX_B;
994 /* Ensure the cached values are set correctly in the frontend
995 * legacy tuning structures, for the advanced tuning API.
997 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
999 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1000 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1001 struct dvb_frontend_parameters *p = &fepriv->parameters;
1003 p->frequency = c->frequency;
1004 p->inversion = c->inversion;
1006 switch (fe->ops.info.type) {
1008 dprintk("%s() Preparing QPSK req\n", __func__);
1009 p->u.qpsk.symbol_rate = c->symbol_rate;
1010 p->u.qpsk.fec_inner = c->fec_inner;
1011 c->delivery_system = SYS_DVBS;
1014 dprintk("%s() Preparing QAM req\n", __func__);
1015 p->u.qam.symbol_rate = c->symbol_rate;
1016 p->u.qam.fec_inner = c->fec_inner;
1017 p->u.qam.modulation = c->modulation;
1018 c->delivery_system = SYS_DVBC_ANNEX_AC;
1021 dprintk("%s() Preparing OFDM req\n", __func__);
1022 if (c->bandwidth_hz == 6000000)
1023 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1024 else if (c->bandwidth_hz == 7000000)
1025 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1026 else if (c->bandwidth_hz == 8000000)
1027 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1029 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1030 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1031 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1032 p->u.ofdm.constellation = c->modulation;
1033 p->u.ofdm.transmission_mode = c->transmission_mode;
1034 p->u.ofdm.guard_interval = c->guard_interval;
1035 p->u.ofdm.hierarchy_information = c->hierarchy;
1036 c->delivery_system = SYS_DVBT;
1039 dprintk("%s() Preparing VSB req\n", __func__);
1040 p->u.vsb.modulation = c->modulation;
1041 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1042 c->delivery_system = SYS_ATSC;
1044 c->delivery_system = SYS_DVBC_ANNEX_B;
1049 /* Ensure the cached values are set correctly in the frontend
1050 * legacy tuning structures, for the legacy tuning API.
1052 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1054 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1055 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1056 struct dvb_frontend_parameters *p = &fepriv->parameters;
1058 p->frequency = c->frequency;
1059 p->inversion = c->inversion;
1061 switch(c->modulation) {
1066 p->u.qpsk.symbol_rate = c->symbol_rate;
1067 p->u.qpsk.fec_inner = c->fec_inner;
1073 if(c->delivery_system == SYS_ISDBT) {
1074 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1075 p->frequency = c->frequency;
1076 p->inversion = INVERSION_AUTO;
1077 p->u.ofdm.constellation = QAM_AUTO;
1078 p->u.ofdm.code_rate_HP = FEC_AUTO;
1079 p->u.ofdm.code_rate_LP = FEC_AUTO;
1080 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1081 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1082 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1083 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1087 void dtv_property_cache_submit(struct dvb_frontend *fe)
1089 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1091 /* For legacy delivery systems we don't need the delivery_system to
1092 * be specified, but we populate the older structures from the cache
1093 * so we can call set_frontend on older drivers.
1095 if(is_legacy_delivery_system(c->delivery_system)) {
1097 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1098 dtv_property_legacy_params_sync(fe);
1101 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1103 /* For advanced delivery systems / modulation types ...
1104 * we seed the lecacy dvb_frontend_parameters structure
1105 * so that the sanity checking code later in the IOCTL processing
1106 * can validate our basic frequency ranges, symbolrates, modulation
1109 dtv_property_adv_params_sync(fe);
1113 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1114 unsigned int cmd, void *parg);
1115 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1116 unsigned int cmd, void *parg);
1118 int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
1119 struct inode *inode, struct file *file)
1123 dtv_property_dump(tvp);
1125 /* Allow the frontend to validate incoming properties */
1126 if (fe->ops.get_property)
1127 r = fe->ops.get_property(fe, tvp);
1134 tvp->u.data = fe->dtv_property_cache.frequency;
1136 case DTV_MODULATION:
1137 tvp->u.data = fe->dtv_property_cache.modulation;
1139 case DTV_BANDWIDTH_HZ:
1140 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1143 tvp->u.data = fe->dtv_property_cache.inversion;
1145 case DTV_SYMBOL_RATE:
1146 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1149 tvp->u.data = fe->dtv_property_cache.fec_inner;
1152 tvp->u.data = fe->dtv_property_cache.pilot;
1155 tvp->u.data = fe->dtv_property_cache.rolloff;
1157 case DTV_DELIVERY_SYSTEM:
1158 tvp->u.data = fe->dtv_property_cache.delivery_system;
1161 tvp->u.data = fe->dtv_property_cache.voltage;
1164 tvp->u.data = fe->dtv_property_cache.sectone;
1166 case DTV_API_VERSION:
1167 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1169 case DTV_CODE_RATE_HP:
1170 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1172 case DTV_CODE_RATE_LP:
1173 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1175 case DTV_GUARD_INTERVAL:
1176 tvp->u.data = fe->dtv_property_cache.guard_interval;
1178 case DTV_TRANSMISSION_MODE:
1179 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1182 tvp->u.data = fe->dtv_property_cache.hierarchy;
1191 int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
1192 struct inode *inode, struct file *file)
1195 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1196 dtv_property_dump(tvp);
1198 /* Allow the frontend to validate incoming properties */
1199 if (fe->ops.set_property)
1200 r = fe->ops.set_property(fe, tvp);
1207 /* Reset a cache of data specific to the frontend here. This does
1208 * not effect hardware.
1210 dprintk("%s() Flushing property cache\n", __func__);
1211 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1212 fe->dtv_property_cache.state = tvp->cmd;
1213 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1216 /* interpret the cache of data, build either a traditional frontend
1217 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1220 fe->dtv_property_cache.state = tvp->cmd;
1221 dprintk("%s() Finalised property cache\n", __func__);
1222 dtv_property_cache_submit(fe);
1224 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1225 &fepriv->parameters);
1228 fe->dtv_property_cache.frequency = tvp->u.data;
1230 case DTV_MODULATION:
1231 fe->dtv_property_cache.modulation = tvp->u.data;
1233 case DTV_BANDWIDTH_HZ:
1234 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1237 fe->dtv_property_cache.inversion = tvp->u.data;
1239 case DTV_SYMBOL_RATE:
1240 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1243 fe->dtv_property_cache.fec_inner = tvp->u.data;
1246 fe->dtv_property_cache.pilot = tvp->u.data;
1249 fe->dtv_property_cache.rolloff = tvp->u.data;
1251 case DTV_DELIVERY_SYSTEM:
1252 fe->dtv_property_cache.delivery_system = tvp->u.data;
1255 fe->dtv_property_cache.voltage = tvp->u.data;
1256 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1257 (void *)fe->dtv_property_cache.voltage);
1260 fe->dtv_property_cache.sectone = tvp->u.data;
1261 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1262 (void *)fe->dtv_property_cache.sectone);
1264 case DTV_CODE_RATE_HP:
1265 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1267 case DTV_CODE_RATE_LP:
1268 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1270 case DTV_GUARD_INTERVAL:
1271 fe->dtv_property_cache.guard_interval = tvp->u.data;
1273 case DTV_TRANSMISSION_MODE:
1274 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1277 fe->dtv_property_cache.hierarchy = tvp->u.data;
1286 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1287 unsigned int cmd, void *parg)
1289 struct dvb_device *dvbdev = file->private_data;
1290 struct dvb_frontend *fe = dvbdev->priv;
1291 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1292 int err = -EOPNOTSUPP;
1294 dprintk ("%s\n", __func__);
1299 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1300 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1301 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1304 if (down_interruptible (&fepriv->sem))
1305 return -ERESTARTSYS;
1307 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1308 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1310 fe->dtv_property_cache.state = DTV_UNDEFINED;
1311 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1318 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1319 unsigned int cmd, void *parg)
1321 struct dvb_device *dvbdev = file->private_data;
1322 struct dvb_frontend *fe = dvbdev->priv;
1325 struct dtv_properties *tvps = NULL;
1326 struct dtv_property *tvp = NULL;
1329 dprintk("%s\n", __func__);
1331 if(cmd == FE_SET_PROPERTY) {
1332 tvps = (struct dtv_properties __user *)parg;
1334 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1335 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1337 /* Put an arbitrary limit on the number of messages that can
1338 * be sent at once */
1339 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1342 tvp = (struct dtv_property *) kmalloc(tvps->num *
1343 sizeof(struct dtv_property), GFP_KERNEL);
1349 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1354 for (i = 0; i < tvps->num; i++) {
1355 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1356 err |= (tvp + i)->result;
1359 if(fe->dtv_property_cache.state == DTV_TUNE)
1360 dprintk("%s() Property cache is full, tuning\n", __func__);
1363 if(cmd == FE_GET_PROPERTY) {
1365 tvps = (struct dtv_properties __user *)parg;
1367 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1368 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1370 /* Put an arbitrary limit on the number of messages that can
1371 * be sent at once */
1372 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1375 tvp = (struct dtv_property *) kmalloc(tvps->num *
1376 sizeof(struct dtv_property), GFP_KERNEL);
1382 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1387 for (i = 0; i < tvps->num; i++) {
1388 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1389 err |= (tvp + i)->result;
1392 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1405 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1406 unsigned int cmd, void *parg)
1408 struct dvb_device *dvbdev = file->private_data;
1409 struct dvb_frontend *fe = dvbdev->priv;
1410 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1411 int err = -EOPNOTSUPP;
1415 struct dvb_frontend_info* info = parg;
1416 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1417 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1419 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1420 * do it, it is done for it. */
1421 info->caps |= FE_CAN_INVERSION_AUTO;
1426 case FE_READ_STATUS: {
1427 fe_status_t* status = parg;
1429 /* if retune was requested but hasn't occured yet, prevent
1430 * that user get signal state from previous tuning */
1431 if(fepriv->state == FESTATE_RETUNE) {
1437 if (fe->ops.read_status)
1438 err = fe->ops.read_status(fe, status);
1442 if (fe->ops.read_ber)
1443 err = fe->ops.read_ber(fe, (__u32*) parg);
1446 case FE_READ_SIGNAL_STRENGTH:
1447 if (fe->ops.read_signal_strength)
1448 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1452 if (fe->ops.read_snr)
1453 err = fe->ops.read_snr(fe, (__u16*) parg);
1456 case FE_READ_UNCORRECTED_BLOCKS:
1457 if (fe->ops.read_ucblocks)
1458 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1462 case FE_DISEQC_RESET_OVERLOAD:
1463 if (fe->ops.diseqc_reset_overload) {
1464 err = fe->ops.diseqc_reset_overload(fe);
1465 fepriv->state = FESTATE_DISEQC;
1470 case FE_DISEQC_SEND_MASTER_CMD:
1471 if (fe->ops.diseqc_send_master_cmd) {
1472 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1473 fepriv->state = FESTATE_DISEQC;
1478 case FE_DISEQC_SEND_BURST:
1479 if (fe->ops.diseqc_send_burst) {
1480 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1481 fepriv->state = FESTATE_DISEQC;
1487 if (fe->ops.set_tone) {
1488 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1489 fepriv->tone = (fe_sec_tone_mode_t) parg;
1490 fepriv->state = FESTATE_DISEQC;
1495 case FE_SET_VOLTAGE:
1496 if (fe->ops.set_voltage) {
1497 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1498 fepriv->voltage = (fe_sec_voltage_t) parg;
1499 fepriv->state = FESTATE_DISEQC;
1504 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1505 if (fe->ops.dishnetwork_send_legacy_command) {
1506 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1507 fepriv->state = FESTATE_DISEQC;
1509 } else if (fe->ops.set_voltage) {
1511 * NOTE: This is a fallback condition. Some frontends
1512 * (stv0299 for instance) take longer than 8msec to
1513 * respond to a set_voltage command. Those switches
1514 * need custom routines to switch properly. For all
1515 * other frontends, the following shoule work ok.
1516 * Dish network legacy switches (as used by Dish500)
1517 * are controlled by sending 9-bit command words
1518 * spaced 8msec apart.
1519 * the actual command word is switch/port dependant
1520 * so it is up to the userspace application to send
1521 * the right command.
1522 * The command must always start with a '0' after
1523 * initialization, so parg is 8 bits and does not
1524 * include the initialization or start bit
1526 unsigned long swcmd = ((unsigned long) parg) << 1;
1527 struct timeval nexttime;
1528 struct timeval tv[10];
1531 if (dvb_frontend_debug)
1532 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1533 do_gettimeofday(&nexttime);
1534 if (dvb_frontend_debug)
1535 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1536 /* before sending a command, initialize by sending
1537 * a 32ms 18V to the switch
1539 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1540 dvb_frontend_sleep_until(&nexttime, 32000);
1542 for (i = 0; i < 9; i++) {
1543 if (dvb_frontend_debug)
1544 do_gettimeofday(&tv[i + 1]);
1545 if ((swcmd & 0x01) != last) {
1546 /* set voltage to (last ? 13V : 18V) */
1547 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1548 last = (last) ? 0 : 1;
1552 dvb_frontend_sleep_until(&nexttime, 8000);
1554 if (dvb_frontend_debug) {
1555 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1556 __func__, fe->dvb->num);
1557 for (i = 1; i < 10; i++)
1558 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1561 fepriv->state = FESTATE_DISEQC;
1566 case FE_DISEQC_RECV_SLAVE_REPLY:
1567 if (fe->ops.diseqc_recv_slave_reply)
1568 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1571 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1572 if (fe->ops.enable_high_lnb_voltage)
1573 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1576 case FE_SET_FRONTEND: {
1577 struct dvb_frontend_tune_settings fetunesettings;
1579 if(fe->dtv_property_cache.state == DTV_TUNE) {
1580 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1585 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1590 memcpy (&fepriv->parameters, parg,
1591 sizeof (struct dvb_frontend_parameters));
1592 dtv_property_cache_sync(fe, &fepriv->parameters);
1595 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1596 memcpy(&fetunesettings.parameters, parg,
1597 sizeof (struct dvb_frontend_parameters));
1599 /* force auto frequency inversion if requested */
1600 if (dvb_force_auto_inversion) {
1601 fepriv->parameters.inversion = INVERSION_AUTO;
1602 fetunesettings.parameters.inversion = INVERSION_AUTO;
1604 if (fe->ops.info.type == FE_OFDM) {
1605 /* without hierarchical coding code_rate_LP is irrelevant,
1606 * so we tolerate the otherwise invalid FEC_NONE setting */
1607 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1608 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1609 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1612 /* get frontend-specific tuning settings */
1613 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1614 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1615 fepriv->max_drift = fetunesettings.max_drift;
1616 fepriv->step_size = fetunesettings.step_size;
1618 /* default values */
1619 switch(fe->ops.info.type) {
1621 fepriv->min_delay = HZ/20;
1622 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1623 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1627 fepriv->min_delay = HZ/20;
1628 fepriv->step_size = 0; /* no zigzag */
1629 fepriv->max_drift = 0;
1633 fepriv->min_delay = HZ/20;
1634 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1635 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1638 fepriv->min_delay = HZ/20;
1639 fepriv->step_size = 0;
1640 fepriv->max_drift = 0;
1644 if (dvb_override_tune_delay > 0)
1645 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1647 fepriv->state = FESTATE_RETUNE;
1648 dvb_frontend_wakeup(fe);
1649 dvb_frontend_add_event(fe, 0);
1656 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1659 case FE_GET_FRONTEND:
1660 if (fe->ops.get_frontend) {
1661 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1662 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1666 case FE_SET_FRONTEND_TUNE_MODE:
1667 fepriv->tune_mode_flags = (unsigned long) parg;
1676 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1678 struct dvb_device *dvbdev = file->private_data;
1679 struct dvb_frontend *fe = dvbdev->priv;
1680 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1682 dprintk ("%s\n", __func__);
1684 poll_wait (file, &fepriv->events.wait_queue, wait);
1686 if (fepriv->events.eventw != fepriv->events.eventr)
1687 return (POLLIN | POLLRDNORM | POLLPRI);
1692 static int dvb_frontend_open(struct inode *inode, struct file *file)
1694 struct dvb_device *dvbdev = file->private_data;
1695 struct dvb_frontend *fe = dvbdev->priv;
1696 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1697 struct dvb_adapter *adapter = fe->dvb;
1700 dprintk ("%s\n", __func__);
1702 if (adapter->mfe_shared) {
1703 mutex_lock (&adapter->mfe_lock);
1705 if (adapter->mfe_dvbdev == NULL)
1706 adapter->mfe_dvbdev = dvbdev;
1708 else if (adapter->mfe_dvbdev != dvbdev) {
1710 *mfedev = adapter->mfe_dvbdev;
1712 *mfe = mfedev->priv;
1713 struct dvb_frontend_private
1714 *mfepriv = mfe->frontend_priv;
1715 int mferetry = (dvb_mfe_wait_time << 1);
1717 mutex_unlock (&adapter->mfe_lock);
1718 while (mferetry-- && (mfedev->users != -1 ||
1719 mfepriv->thread != NULL)) {
1720 if(msleep_interruptible(500)) {
1721 if(signal_pending(current))
1726 mutex_lock (&adapter->mfe_lock);
1727 if(adapter->mfe_dvbdev != dvbdev) {
1728 mfedev = adapter->mfe_dvbdev;
1730 mfepriv = mfe->frontend_priv;
1731 if (mfedev->users != -1 ||
1732 mfepriv->thread != NULL) {
1733 mutex_unlock (&adapter->mfe_lock);
1736 adapter->mfe_dvbdev = dvbdev;
1741 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1742 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1746 if ((ret = dvb_generic_open (inode, file)) < 0)
1749 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1750 /* normal tune mode when opened R/W */
1751 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1753 fepriv->voltage = -1;
1755 ret = dvb_frontend_start (fe);
1759 /* empty event queue */
1760 fepriv->events.eventr = fepriv->events.eventw = 0;
1763 if (adapter->mfe_shared)
1764 mutex_unlock (&adapter->mfe_lock);
1768 dvb_generic_release(inode, file);
1770 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1771 fe->ops.ts_bus_ctrl(fe, 0);
1773 if (adapter->mfe_shared)
1774 mutex_unlock (&adapter->mfe_lock);
1778 static int dvb_frontend_release(struct inode *inode, struct file *file)
1780 struct dvb_device *dvbdev = file->private_data;
1781 struct dvb_frontend *fe = dvbdev->priv;
1782 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1785 dprintk ("%s\n", __func__);
1787 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1788 fepriv->release_jiffies = jiffies;
1790 ret = dvb_generic_release (inode, file);
1792 if (dvbdev->users == -1) {
1793 if (fepriv->exit == 1) {
1794 fops_put(file->f_op);
1796 wake_up(&dvbdev->wait_queue);
1798 if (fe->ops.ts_bus_ctrl)
1799 fe->ops.ts_bus_ctrl(fe, 0);
1805 static struct file_operations dvb_frontend_fops = {
1806 .owner = THIS_MODULE,
1807 .ioctl = dvb_generic_ioctl,
1808 .poll = dvb_frontend_poll,
1809 .open = dvb_frontend_open,
1810 .release = dvb_frontend_release
1813 int dvb_register_frontend(struct dvb_adapter* dvb,
1814 struct dvb_frontend* fe)
1816 struct dvb_frontend_private *fepriv;
1817 static const struct dvb_device dvbdev_template = {
1821 .fops = &dvb_frontend_fops,
1822 .kernel_ioctl = dvb_frontend_ioctl
1825 dprintk ("%s\n", __func__);
1827 if (mutex_lock_interruptible(&frontend_mutex))
1828 return -ERESTARTSYS;
1830 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1831 if (fe->frontend_priv == NULL) {
1832 mutex_unlock(&frontend_mutex);
1835 fepriv = fe->frontend_priv;
1837 init_MUTEX (&fepriv->sem);
1838 init_waitqueue_head (&fepriv->wait_queue);
1839 init_waitqueue_head (&fepriv->events.wait_queue);
1840 mutex_init(&fepriv->events.mtx);
1842 fepriv->inversion = INVERSION_OFF;
1844 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1849 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1850 fe, DVB_DEVICE_FRONTEND);
1852 mutex_unlock(&frontend_mutex);
1855 EXPORT_SYMBOL(dvb_register_frontend);
1857 int dvb_unregister_frontend(struct dvb_frontend* fe)
1859 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1860 dprintk ("%s\n", __func__);
1862 mutex_lock(&frontend_mutex);
1863 dvb_frontend_stop (fe);
1864 mutex_unlock(&frontend_mutex);
1866 if (fepriv->dvbdev->users < -1)
1867 wait_event(fepriv->dvbdev->wait_queue,
1868 fepriv->dvbdev->users==-1);
1870 mutex_lock(&frontend_mutex);
1871 dvb_unregister_device (fepriv->dvbdev);
1873 /* fe is invalid now */
1875 mutex_unlock(&frontend_mutex);
1878 EXPORT_SYMBOL(dvb_unregister_frontend);
1880 #ifdef CONFIG_MEDIA_ATTACH
1881 void dvb_frontend_detach(struct dvb_frontend* fe)
1885 if (fe->ops.release_sec) {
1886 fe->ops.release_sec(fe);
1887 symbol_put_addr(fe->ops.release_sec);
1889 if (fe->ops.tuner_ops.release) {
1890 fe->ops.tuner_ops.release(fe);
1891 symbol_put_addr(fe->ops.tuner_ops.release);
1893 if (fe->ops.analog_ops.release) {
1894 fe->ops.analog_ops.release(fe);
1895 symbol_put_addr(fe->ops.analog_ops.release);
1897 ptr = (void*)fe->ops.release;
1899 fe->ops.release(fe);
1900 symbol_put_addr(ptr);
1904 void dvb_frontend_detach(struct dvb_frontend* fe)
1906 if (fe->ops.release_sec)
1907 fe->ops.release_sec(fe);
1908 if (fe->ops.tuner_ops.release)
1909 fe->ops.tuner_ops.release(fe);
1910 if (fe->ops.analog_ops.release)
1911 fe->ops.analog_ops.release(fe);
1912 if (fe->ops.release)
1913 fe->ops.release(fe);
1916 EXPORT_SYMBOL(dvb_frontend_detach);