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) ||
944 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
945 * drivers can use a single set_frontend tuning function, regardless of whether
946 * it's being used for the legacy or new API, reducing code and complexity.
948 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
950 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
952 c->frequency = p->frequency;
953 c->inversion = p->inversion;
955 switch (fe->ops.info.type) {
957 c->modulation = QPSK; /* implied for DVB-S in legacy API */
958 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
959 c->symbol_rate = p->u.qpsk.symbol_rate;
960 c->fec_inner = p->u.qpsk.fec_inner;
961 c->delivery_system = SYS_DVBS;
964 c->symbol_rate = p->u.qam.symbol_rate;
965 c->fec_inner = p->u.qam.fec_inner;
966 c->modulation = p->u.qam.modulation;
967 c->delivery_system = SYS_DVBC_ANNEX_AC;
970 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
971 c->bandwidth_hz = 6000000;
972 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
973 c->bandwidth_hz = 7000000;
974 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
975 c->bandwidth_hz = 8000000;
977 /* Including BANDWIDTH_AUTO */
979 c->code_rate_HP = p->u.ofdm.code_rate_HP;
980 c->code_rate_LP = p->u.ofdm.code_rate_LP;
981 c->modulation = p->u.ofdm.constellation;
982 c->transmission_mode = p->u.ofdm.transmission_mode;
983 c->guard_interval = p->u.ofdm.guard_interval;
984 c->hierarchy = p->u.ofdm.hierarchy_information;
985 c->delivery_system = SYS_DVBT;
988 c->modulation = p->u.vsb.modulation;
989 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
990 c->delivery_system = SYS_ATSC;
992 c->delivery_system = SYS_DVBC_ANNEX_B;
997 /* Ensure the cached values are set correctly in the frontend
998 * legacy tuning structures, for the advanced tuning API.
1000 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1002 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1003 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1004 struct dvb_frontend_parameters *p = &fepriv->parameters;
1006 p->frequency = c->frequency;
1007 p->inversion = c->inversion;
1009 switch (fe->ops.info.type) {
1011 dprintk("%s() Preparing QPSK req\n", __func__);
1012 p->u.qpsk.symbol_rate = c->symbol_rate;
1013 p->u.qpsk.fec_inner = c->fec_inner;
1014 c->delivery_system = SYS_DVBS;
1017 dprintk("%s() Preparing QAM req\n", __func__);
1018 p->u.qam.symbol_rate = c->symbol_rate;
1019 p->u.qam.fec_inner = c->fec_inner;
1020 p->u.qam.modulation = c->modulation;
1021 c->delivery_system = SYS_DVBC_ANNEX_AC;
1024 dprintk("%s() Preparing OFDM req\n", __func__);
1025 if (c->bandwidth_hz == 6000000)
1026 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1027 else if (c->bandwidth_hz == 7000000)
1028 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1029 else if (c->bandwidth_hz == 8000000)
1030 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1032 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1033 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1034 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1035 p->u.ofdm.constellation = c->modulation;
1036 p->u.ofdm.transmission_mode = c->transmission_mode;
1037 p->u.ofdm.guard_interval = c->guard_interval;
1038 p->u.ofdm.hierarchy_information = c->hierarchy;
1039 c->delivery_system = SYS_DVBT;
1042 dprintk("%s() Preparing VSB req\n", __func__);
1043 p->u.vsb.modulation = c->modulation;
1044 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1045 c->delivery_system = SYS_ATSC;
1047 c->delivery_system = SYS_DVBC_ANNEX_B;
1052 /* Ensure the cached values are set correctly in the frontend
1053 * legacy tuning structures, for the legacy tuning API.
1055 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1057 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1058 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1059 struct dvb_frontend_parameters *p = &fepriv->parameters;
1061 p->frequency = c->frequency;
1062 p->inversion = c->inversion;
1064 switch(c->modulation) {
1069 p->u.qpsk.symbol_rate = c->symbol_rate;
1070 p->u.qpsk.fec_inner = c->fec_inner;
1076 if(c->delivery_system == SYS_ISDBT) {
1077 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1078 p->frequency = c->frequency;
1079 p->inversion = INVERSION_AUTO;
1080 p->u.ofdm.constellation = QAM_AUTO;
1081 p->u.ofdm.code_rate_HP = FEC_AUTO;
1082 p->u.ofdm.code_rate_LP = FEC_AUTO;
1083 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1084 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1085 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1086 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1090 void dtv_property_cache_submit(struct dvb_frontend *fe)
1092 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1094 /* For legacy delivery systems we don't need the delivery_system to
1095 * be specified, but we populate the older structures from the cache
1096 * so we can call set_frontend on older drivers.
1098 if(is_legacy_delivery_system(c->delivery_system)) {
1100 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1101 dtv_property_legacy_params_sync(fe);
1104 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1106 /* For advanced delivery systems / modulation types ...
1107 * we seed the lecacy dvb_frontend_parameters structure
1108 * so that the sanity checking code later in the IOCTL processing
1109 * can validate our basic frequency ranges, symbolrates, modulation
1112 dtv_property_adv_params_sync(fe);
1116 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1117 unsigned int cmd, void *parg);
1118 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1119 unsigned int cmd, void *parg);
1121 int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
1122 struct inode *inode, struct file *file)
1126 dtv_property_dump(tvp);
1128 /* Allow the frontend to validate incoming properties */
1129 if (fe->ops.get_property)
1130 r = fe->ops.get_property(fe, tvp);
1137 tvp->u.data = fe->dtv_property_cache.frequency;
1139 case DTV_MODULATION:
1140 tvp->u.data = fe->dtv_property_cache.modulation;
1142 case DTV_BANDWIDTH_HZ:
1143 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1146 tvp->u.data = fe->dtv_property_cache.inversion;
1148 case DTV_SYMBOL_RATE:
1149 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1152 tvp->u.data = fe->dtv_property_cache.fec_inner;
1155 tvp->u.data = fe->dtv_property_cache.pilot;
1158 tvp->u.data = fe->dtv_property_cache.rolloff;
1160 case DTV_DELIVERY_SYSTEM:
1161 tvp->u.data = fe->dtv_property_cache.delivery_system;
1164 tvp->u.data = fe->dtv_property_cache.voltage;
1167 tvp->u.data = fe->dtv_property_cache.sectone;
1169 case DTV_API_VERSION:
1170 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1172 case DTV_CODE_RATE_HP:
1173 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1175 case DTV_CODE_RATE_LP:
1176 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1178 case DTV_GUARD_INTERVAL:
1179 tvp->u.data = fe->dtv_property_cache.guard_interval;
1181 case DTV_TRANSMISSION_MODE:
1182 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1185 tvp->u.data = fe->dtv_property_cache.hierarchy;
1194 int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
1195 struct inode *inode, struct file *file)
1198 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1199 dtv_property_dump(tvp);
1201 /* Allow the frontend to validate incoming properties */
1202 if (fe->ops.set_property)
1203 r = fe->ops.set_property(fe, tvp);
1210 /* Reset a cache of data specific to the frontend here. This does
1211 * not effect hardware.
1213 dprintk("%s() Flushing property cache\n", __func__);
1214 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1215 fe->dtv_property_cache.state = tvp->cmd;
1216 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1219 /* interpret the cache of data, build either a traditional frontend
1220 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1223 fe->dtv_property_cache.state = tvp->cmd;
1224 dprintk("%s() Finalised property cache\n", __func__);
1225 dtv_property_cache_submit(fe);
1227 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1228 &fepriv->parameters);
1231 fe->dtv_property_cache.frequency = tvp->u.data;
1233 case DTV_MODULATION:
1234 fe->dtv_property_cache.modulation = tvp->u.data;
1236 case DTV_BANDWIDTH_HZ:
1237 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1240 fe->dtv_property_cache.inversion = tvp->u.data;
1242 case DTV_SYMBOL_RATE:
1243 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1246 fe->dtv_property_cache.fec_inner = tvp->u.data;
1249 fe->dtv_property_cache.pilot = tvp->u.data;
1252 fe->dtv_property_cache.rolloff = tvp->u.data;
1254 case DTV_DELIVERY_SYSTEM:
1255 fe->dtv_property_cache.delivery_system = tvp->u.data;
1258 fe->dtv_property_cache.voltage = tvp->u.data;
1259 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1260 (void *)fe->dtv_property_cache.voltage);
1263 fe->dtv_property_cache.sectone = tvp->u.data;
1264 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1265 (void *)fe->dtv_property_cache.sectone);
1267 case DTV_CODE_RATE_HP:
1268 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1270 case DTV_CODE_RATE_LP:
1271 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1273 case DTV_GUARD_INTERVAL:
1274 fe->dtv_property_cache.guard_interval = tvp->u.data;
1276 case DTV_TRANSMISSION_MODE:
1277 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1280 fe->dtv_property_cache.hierarchy = tvp->u.data;
1289 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1290 unsigned int cmd, void *parg)
1292 struct dvb_device *dvbdev = file->private_data;
1293 struct dvb_frontend *fe = dvbdev->priv;
1294 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1295 int err = -EOPNOTSUPP;
1297 dprintk ("%s\n", __func__);
1302 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1303 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1304 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1307 if (down_interruptible (&fepriv->sem))
1308 return -ERESTARTSYS;
1310 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1311 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1313 fe->dtv_property_cache.state = DTV_UNDEFINED;
1314 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1321 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1322 unsigned int cmd, void *parg)
1324 struct dvb_device *dvbdev = file->private_data;
1325 struct dvb_frontend *fe = dvbdev->priv;
1328 struct dtv_properties *tvps = NULL;
1329 struct dtv_property *tvp = NULL;
1332 dprintk("%s\n", __func__);
1334 if(cmd == FE_SET_PROPERTY) {
1335 tvps = (struct dtv_properties __user *)parg;
1337 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1338 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1340 /* Put an arbitrary limit on the number of messages that can
1341 * be sent at once */
1342 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1345 tvp = (struct dtv_property *) kmalloc(tvps->num *
1346 sizeof(struct dtv_property), GFP_KERNEL);
1352 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1357 for (i = 0; i < tvps->num; i++) {
1358 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1359 err |= (tvp + i)->result;
1362 if(fe->dtv_property_cache.state == DTV_TUNE)
1363 dprintk("%s() Property cache is full, tuning\n", __func__);
1366 if(cmd == FE_GET_PROPERTY) {
1368 tvps = (struct dtv_properties __user *)parg;
1370 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1371 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1373 /* Put an arbitrary limit on the number of messages that can
1374 * be sent at once */
1375 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1378 tvp = (struct dtv_property *) kmalloc(tvps->num *
1379 sizeof(struct dtv_property), GFP_KERNEL);
1385 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1390 for (i = 0; i < tvps->num; i++) {
1391 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1392 err |= (tvp + i)->result;
1395 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1408 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1409 unsigned int cmd, void *parg)
1411 struct dvb_device *dvbdev = file->private_data;
1412 struct dvb_frontend *fe = dvbdev->priv;
1413 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1414 int err = -EOPNOTSUPP;
1418 struct dvb_frontend_info* info = parg;
1419 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1420 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1422 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1423 * do it, it is done for it. */
1424 info->caps |= FE_CAN_INVERSION_AUTO;
1429 case FE_READ_STATUS: {
1430 fe_status_t* status = parg;
1432 /* if retune was requested but hasn't occured yet, prevent
1433 * that user get signal state from previous tuning */
1434 if(fepriv->state == FESTATE_RETUNE) {
1440 if (fe->ops.read_status)
1441 err = fe->ops.read_status(fe, status);
1445 if (fe->ops.read_ber)
1446 err = fe->ops.read_ber(fe, (__u32*) parg);
1449 case FE_READ_SIGNAL_STRENGTH:
1450 if (fe->ops.read_signal_strength)
1451 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1455 if (fe->ops.read_snr)
1456 err = fe->ops.read_snr(fe, (__u16*) parg);
1459 case FE_READ_UNCORRECTED_BLOCKS:
1460 if (fe->ops.read_ucblocks)
1461 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1465 case FE_DISEQC_RESET_OVERLOAD:
1466 if (fe->ops.diseqc_reset_overload) {
1467 err = fe->ops.diseqc_reset_overload(fe);
1468 fepriv->state = FESTATE_DISEQC;
1473 case FE_DISEQC_SEND_MASTER_CMD:
1474 if (fe->ops.diseqc_send_master_cmd) {
1475 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1476 fepriv->state = FESTATE_DISEQC;
1481 case FE_DISEQC_SEND_BURST:
1482 if (fe->ops.diseqc_send_burst) {
1483 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1484 fepriv->state = FESTATE_DISEQC;
1490 if (fe->ops.set_tone) {
1491 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1492 fepriv->tone = (fe_sec_tone_mode_t) parg;
1493 fepriv->state = FESTATE_DISEQC;
1498 case FE_SET_VOLTAGE:
1499 if (fe->ops.set_voltage) {
1500 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1501 fepriv->voltage = (fe_sec_voltage_t) parg;
1502 fepriv->state = FESTATE_DISEQC;
1507 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1508 if (fe->ops.dishnetwork_send_legacy_command) {
1509 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1510 fepriv->state = FESTATE_DISEQC;
1512 } else if (fe->ops.set_voltage) {
1514 * NOTE: This is a fallback condition. Some frontends
1515 * (stv0299 for instance) take longer than 8msec to
1516 * respond to a set_voltage command. Those switches
1517 * need custom routines to switch properly. For all
1518 * other frontends, the following shoule work ok.
1519 * Dish network legacy switches (as used by Dish500)
1520 * are controlled by sending 9-bit command words
1521 * spaced 8msec apart.
1522 * the actual command word is switch/port dependant
1523 * so it is up to the userspace application to send
1524 * the right command.
1525 * The command must always start with a '0' after
1526 * initialization, so parg is 8 bits and does not
1527 * include the initialization or start bit
1529 unsigned long swcmd = ((unsigned long) parg) << 1;
1530 struct timeval nexttime;
1531 struct timeval tv[10];
1534 if (dvb_frontend_debug)
1535 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1536 do_gettimeofday(&nexttime);
1537 if (dvb_frontend_debug)
1538 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1539 /* before sending a command, initialize by sending
1540 * a 32ms 18V to the switch
1542 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1543 dvb_frontend_sleep_until(&nexttime, 32000);
1545 for (i = 0; i < 9; i++) {
1546 if (dvb_frontend_debug)
1547 do_gettimeofday(&tv[i + 1]);
1548 if ((swcmd & 0x01) != last) {
1549 /* set voltage to (last ? 13V : 18V) */
1550 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1551 last = (last) ? 0 : 1;
1555 dvb_frontend_sleep_until(&nexttime, 8000);
1557 if (dvb_frontend_debug) {
1558 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1559 __func__, fe->dvb->num);
1560 for (i = 1; i < 10; i++)
1561 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1564 fepriv->state = FESTATE_DISEQC;
1569 case FE_DISEQC_RECV_SLAVE_REPLY:
1570 if (fe->ops.diseqc_recv_slave_reply)
1571 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1574 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1575 if (fe->ops.enable_high_lnb_voltage)
1576 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1579 case FE_SET_FRONTEND: {
1580 struct dvb_frontend_tune_settings fetunesettings;
1582 if(fe->dtv_property_cache.state == DTV_TUNE) {
1583 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1588 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1593 memcpy (&fepriv->parameters, parg,
1594 sizeof (struct dvb_frontend_parameters));
1595 dtv_property_cache_sync(fe, &fepriv->parameters);
1598 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1599 memcpy(&fetunesettings.parameters, parg,
1600 sizeof (struct dvb_frontend_parameters));
1602 /* force auto frequency inversion if requested */
1603 if (dvb_force_auto_inversion) {
1604 fepriv->parameters.inversion = INVERSION_AUTO;
1605 fetunesettings.parameters.inversion = INVERSION_AUTO;
1607 if (fe->ops.info.type == FE_OFDM) {
1608 /* without hierarchical coding code_rate_LP is irrelevant,
1609 * so we tolerate the otherwise invalid FEC_NONE setting */
1610 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1611 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1612 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1615 /* get frontend-specific tuning settings */
1616 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1617 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1618 fepriv->max_drift = fetunesettings.max_drift;
1619 fepriv->step_size = fetunesettings.step_size;
1621 /* default values */
1622 switch(fe->ops.info.type) {
1624 fepriv->min_delay = HZ/20;
1625 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1626 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1630 fepriv->min_delay = HZ/20;
1631 fepriv->step_size = 0; /* no zigzag */
1632 fepriv->max_drift = 0;
1636 fepriv->min_delay = HZ/20;
1637 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1638 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1641 fepriv->min_delay = HZ/20;
1642 fepriv->step_size = 0;
1643 fepriv->max_drift = 0;
1647 if (dvb_override_tune_delay > 0)
1648 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1650 fepriv->state = FESTATE_RETUNE;
1651 dvb_frontend_wakeup(fe);
1652 dvb_frontend_add_event(fe, 0);
1659 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1662 case FE_GET_FRONTEND:
1663 if (fe->ops.get_frontend) {
1664 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1665 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1669 case FE_SET_FRONTEND_TUNE_MODE:
1670 fepriv->tune_mode_flags = (unsigned long) parg;
1679 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1681 struct dvb_device *dvbdev = file->private_data;
1682 struct dvb_frontend *fe = dvbdev->priv;
1683 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1685 dprintk ("%s\n", __func__);
1687 poll_wait (file, &fepriv->events.wait_queue, wait);
1689 if (fepriv->events.eventw != fepriv->events.eventr)
1690 return (POLLIN | POLLRDNORM | POLLPRI);
1695 static int dvb_frontend_open(struct inode *inode, struct file *file)
1697 struct dvb_device *dvbdev = file->private_data;
1698 struct dvb_frontend *fe = dvbdev->priv;
1699 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1700 struct dvb_adapter *adapter = fe->dvb;
1703 dprintk ("%s\n", __func__);
1705 if (adapter->mfe_shared) {
1706 mutex_lock (&adapter->mfe_lock);
1708 if (adapter->mfe_dvbdev == NULL)
1709 adapter->mfe_dvbdev = dvbdev;
1711 else if (adapter->mfe_dvbdev != dvbdev) {
1713 *mfedev = adapter->mfe_dvbdev;
1715 *mfe = mfedev->priv;
1716 struct dvb_frontend_private
1717 *mfepriv = mfe->frontend_priv;
1718 int mferetry = (dvb_mfe_wait_time << 1);
1720 mutex_unlock (&adapter->mfe_lock);
1721 while (mferetry-- && (mfedev->users != -1 ||
1722 mfepriv->thread != NULL)) {
1723 if(msleep_interruptible(500)) {
1724 if(signal_pending(current))
1729 mutex_lock (&adapter->mfe_lock);
1730 if(adapter->mfe_dvbdev != dvbdev) {
1731 mfedev = adapter->mfe_dvbdev;
1733 mfepriv = mfe->frontend_priv;
1734 if (mfedev->users != -1 ||
1735 mfepriv->thread != NULL) {
1736 mutex_unlock (&adapter->mfe_lock);
1739 adapter->mfe_dvbdev = dvbdev;
1744 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1745 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1749 if ((ret = dvb_generic_open (inode, file)) < 0)
1752 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1753 /* normal tune mode when opened R/W */
1754 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1756 fepriv->voltage = -1;
1758 ret = dvb_frontend_start (fe);
1762 /* empty event queue */
1763 fepriv->events.eventr = fepriv->events.eventw = 0;
1766 if (adapter->mfe_shared)
1767 mutex_unlock (&adapter->mfe_lock);
1771 dvb_generic_release(inode, file);
1773 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1774 fe->ops.ts_bus_ctrl(fe, 0);
1776 if (adapter->mfe_shared)
1777 mutex_unlock (&adapter->mfe_lock);
1781 static int dvb_frontend_release(struct inode *inode, struct file *file)
1783 struct dvb_device *dvbdev = file->private_data;
1784 struct dvb_frontend *fe = dvbdev->priv;
1785 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1788 dprintk ("%s\n", __func__);
1790 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1791 fepriv->release_jiffies = jiffies;
1793 ret = dvb_generic_release (inode, file);
1795 if (dvbdev->users == -1) {
1796 if (fepriv->exit == 1) {
1797 fops_put(file->f_op);
1799 wake_up(&dvbdev->wait_queue);
1801 if (fe->ops.ts_bus_ctrl)
1802 fe->ops.ts_bus_ctrl(fe, 0);
1808 static struct file_operations dvb_frontend_fops = {
1809 .owner = THIS_MODULE,
1810 .ioctl = dvb_generic_ioctl,
1811 .poll = dvb_frontend_poll,
1812 .open = dvb_frontend_open,
1813 .release = dvb_frontend_release
1816 int dvb_register_frontend(struct dvb_adapter* dvb,
1817 struct dvb_frontend* fe)
1819 struct dvb_frontend_private *fepriv;
1820 static const struct dvb_device dvbdev_template = {
1824 .fops = &dvb_frontend_fops,
1825 .kernel_ioctl = dvb_frontend_ioctl
1828 dprintk ("%s\n", __func__);
1830 if (mutex_lock_interruptible(&frontend_mutex))
1831 return -ERESTARTSYS;
1833 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1834 if (fe->frontend_priv == NULL) {
1835 mutex_unlock(&frontend_mutex);
1838 fepriv = fe->frontend_priv;
1840 init_MUTEX (&fepriv->sem);
1841 init_waitqueue_head (&fepriv->wait_queue);
1842 init_waitqueue_head (&fepriv->events.wait_queue);
1843 mutex_init(&fepriv->events.mtx);
1845 fepriv->inversion = INVERSION_OFF;
1847 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1852 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1853 fe, DVB_DEVICE_FRONTEND);
1855 mutex_unlock(&frontend_mutex);
1858 EXPORT_SYMBOL(dvb_register_frontend);
1860 int dvb_unregister_frontend(struct dvb_frontend* fe)
1862 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1863 dprintk ("%s\n", __func__);
1865 mutex_lock(&frontend_mutex);
1866 dvb_frontend_stop (fe);
1867 mutex_unlock(&frontend_mutex);
1869 if (fepriv->dvbdev->users < -1)
1870 wait_event(fepriv->dvbdev->wait_queue,
1871 fepriv->dvbdev->users==-1);
1873 mutex_lock(&frontend_mutex);
1874 dvb_unregister_device (fepriv->dvbdev);
1876 /* fe is invalid now */
1878 mutex_unlock(&frontend_mutex);
1881 EXPORT_SYMBOL(dvb_unregister_frontend);
1883 #ifdef CONFIG_MEDIA_ATTACH
1884 void dvb_frontend_detach(struct dvb_frontend* fe)
1888 if (fe->ops.release_sec) {
1889 fe->ops.release_sec(fe);
1890 symbol_put_addr(fe->ops.release_sec);
1892 if (fe->ops.tuner_ops.release) {
1893 fe->ops.tuner_ops.release(fe);
1894 symbol_put_addr(fe->ops.tuner_ops.release);
1896 if (fe->ops.analog_ops.release) {
1897 fe->ops.analog_ops.release(fe);
1898 symbol_put_addr(fe->ops.analog_ops.release);
1900 ptr = (void*)fe->ops.release;
1902 fe->ops.release(fe);
1903 symbol_put_addr(ptr);
1907 void dvb_frontend_detach(struct dvb_frontend* fe)
1909 if (fe->ops.release_sec)
1910 fe->ops.release_sec(fe);
1911 if (fe->ops.tuner_ops.release)
1912 fe->ops.tuner_ops.release(fe);
1913 if (fe->ops.analog_ops.release)
1914 fe->ops.analog_ops.release(fe);
1915 if (fe->ops.release)
1916 fe->ops.release(fe);
1919 EXPORT_SYMBOL(dvb_frontend_detach);