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 if (fe->ops.i2c_gate_ctrl)
589 fe->ops.i2c_gate_ctrl(fe, 1);
590 fe->ops.tuner_ops.sleep(fe);
591 if (fe->ops.i2c_gate_ctrl)
592 fe->ops.i2c_gate_ctrl(fe, 0);
598 fepriv->thread = NULL;
601 dvb_frontend_wakeup(fe);
605 static void dvb_frontend_stop(struct dvb_frontend *fe)
607 struct dvb_frontend_private *fepriv = fe->frontend_priv;
609 dprintk ("%s\n", __func__);
617 kthread_stop(fepriv->thread);
619 init_MUTEX (&fepriv->sem);
620 fepriv->state = FESTATE_IDLE;
622 /* paranoia check in case a signal arrived */
624 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
628 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
630 return ((curtime.tv_usec < lasttime.tv_usec) ?
631 1000000 - lasttime.tv_usec + curtime.tv_usec :
632 curtime.tv_usec - lasttime.tv_usec);
634 EXPORT_SYMBOL(timeval_usec_diff);
636 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
638 curtime->tv_usec += add_usec;
639 if (curtime->tv_usec >= 1000000) {
640 curtime->tv_usec -= 1000000;
646 * Sleep until gettimeofday() > waketime + add_usec
647 * This needs to be as precise as possible, but as the delay is
648 * usually between 2ms and 32ms, it is done using a scheduled msleep
649 * followed by usleep (normally a busy-wait loop) for the remainder
651 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
653 struct timeval lasttime;
656 timeval_usec_add(waketime, add_usec);
658 do_gettimeofday(&lasttime);
659 delta = timeval_usec_diff(lasttime, *waketime);
661 msleep((delta - 1500) / 1000);
662 do_gettimeofday(&lasttime);
663 newdelta = timeval_usec_diff(lasttime, *waketime);
664 delta = (newdelta > delta) ? 0 : newdelta;
669 EXPORT_SYMBOL(dvb_frontend_sleep_until);
671 static int dvb_frontend_start(struct dvb_frontend *fe)
674 struct dvb_frontend_private *fepriv = fe->frontend_priv;
675 struct task_struct *fe_thread;
677 dprintk ("%s\n", __func__);
679 if (fepriv->thread) {
683 dvb_frontend_stop (fe);
686 if (signal_pending(current))
688 if (down_interruptible (&fepriv->sem))
691 fepriv->state = FESTATE_IDLE;
693 fepriv->thread = NULL;
696 fe_thread = kthread_run(dvb_frontend_thread, fe,
697 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
698 if (IS_ERR(fe_thread)) {
699 ret = PTR_ERR(fe_thread);
700 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
704 fepriv->thread = fe_thread;
708 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
709 u32 *freq_min, u32 *freq_max)
711 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
713 if (fe->ops.info.frequency_max == 0)
714 *freq_max = fe->ops.tuner_ops.info.frequency_max;
715 else if (fe->ops.tuner_ops.info.frequency_max == 0)
716 *freq_max = fe->ops.info.frequency_max;
718 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
720 if (*freq_min == 0 || *freq_max == 0)
721 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
722 fe->dvb->num,fe->id);
725 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
726 struct dvb_frontend_parameters *parms)
731 /* range check: frequency */
732 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
733 if ((freq_min && parms->frequency < freq_min) ||
734 (freq_max && parms->frequency > freq_max)) {
735 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
736 fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
740 /* range check: symbol rate */
741 if (fe->ops.info.type == FE_QPSK) {
742 if ((fe->ops.info.symbol_rate_min &&
743 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
744 (fe->ops.info.symbol_rate_max &&
745 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
746 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
747 fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
748 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
752 } else if (fe->ops.info.type == FE_QAM) {
753 if ((fe->ops.info.symbol_rate_min &&
754 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
755 (fe->ops.info.symbol_rate_max &&
756 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
757 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
758 fe->dvb->num, fe->id, parms->u.qam.symbol_rate,
759 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
767 struct dtv_cmds_h dtv_cmds[] = {
781 .name = "DTV_FREQUENCY",
782 .cmd = DTV_FREQUENCY,
785 [DTV_BANDWIDTH_HZ] = {
786 .name = "DTV_BANDWIDTH_HZ",
787 .cmd = DTV_BANDWIDTH_HZ,
791 .name = "DTV_MODULATION",
792 .cmd = DTV_MODULATION,
796 .name = "DTV_INVERSION",
797 .cmd = DTV_INVERSION,
800 [DTV_DISEQC_MASTER] = {
801 .name = "DTV_DISEQC_MASTER",
802 .cmd = DTV_DISEQC_MASTER,
806 [DTV_SYMBOL_RATE] = {
807 .name = "DTV_SYMBOL_RATE",
808 .cmd = DTV_SYMBOL_RATE,
812 .name = "DTV_INNER_FEC",
813 .cmd = DTV_INNER_FEC,
817 .name = "DTV_VOLTAGE",
832 .name = "DTV_ROLLOFF",
836 [DTV_DELIVERY_SYSTEM] = {
837 .name = "DTV_DELIVERY_SYSTEM",
838 .cmd = DTV_DELIVERY_SYSTEM,
842 .name = "DTV_HIERARCHY",
843 .cmd = DTV_HIERARCHY,
846 [DTV_CODE_RATE_HP] = {
847 .name = "DTV_CODE_RATE_HP",
848 .cmd = DTV_CODE_RATE_HP,
851 [DTV_CODE_RATE_LP] = {
852 .name = "DTV_CODE_RATE_LP",
853 .cmd = DTV_CODE_RATE_LP,
856 [DTV_GUARD_INTERVAL] = {
857 .name = "DTV_GUARD_INTERVAL",
858 .cmd = DTV_GUARD_INTERVAL,
861 [DTV_TRANSMISSION_MODE] = {
862 .name = "DTV_TRANSMISSION_MODE",
863 .cmd = DTV_TRANSMISSION_MODE,
867 [DTV_DISEQC_SLAVE_REPLY] = {
868 .name = "DTV_DISEQC_SLAVE_REPLY",
869 .cmd = DTV_DISEQC_SLAVE_REPLY,
873 [DTV_API_VERSION] = {
874 .name = "DTV_API_VERSION",
875 .cmd = DTV_API_VERSION,
878 [DTV_CODE_RATE_HP] = {
879 .name = "DTV_CODE_RATE_HP",
880 .cmd = DTV_CODE_RATE_HP,
883 [DTV_CODE_RATE_LP] = {
884 .name = "DTV_CODE_RATE_LP",
885 .cmd = DTV_CODE_RATE_LP,
888 [DTV_GUARD_INTERVAL] = {
889 .name = "DTV_GUARD_INTERVAL",
890 .cmd = DTV_GUARD_INTERVAL,
893 [DTV_TRANSMISSION_MODE] = {
894 .name = "DTV_TRANSMISSION_MODE",
895 .cmd = DTV_TRANSMISSION_MODE,
899 .name = "DTV_HIERARCHY",
900 .cmd = DTV_HIERARCHY,
905 void dtv_property_dump(struct dtv_property *tvp)
909 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
910 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
915 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
918 ,dtv_cmds[ tvp->cmd ].name);
920 if(dtv_cmds[ tvp->cmd ].buffer) {
922 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
926 for(i = 0; i < tvp->u.buffer.len; i++)
927 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
930 ,tvp->u.buffer.data[i]);
933 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
936 int is_legacy_delivery_system(fe_delivery_system_t s)
938 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
939 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS) ||
946 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
947 * drivers can use a single set_frontend tuning function, regardless of whether
948 * it's being used for the legacy or new API, reducing code and complexity.
950 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
952 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
954 c->frequency = p->frequency;
955 c->inversion = p->inversion;
957 switch (fe->ops.info.type) {
959 c->modulation = QPSK; /* implied for DVB-S in legacy API */
960 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
961 c->symbol_rate = p->u.qpsk.symbol_rate;
962 c->fec_inner = p->u.qpsk.fec_inner;
963 c->delivery_system = SYS_DVBS;
966 c->symbol_rate = p->u.qam.symbol_rate;
967 c->fec_inner = p->u.qam.fec_inner;
968 c->modulation = p->u.qam.modulation;
969 c->delivery_system = SYS_DVBC_ANNEX_AC;
972 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
973 c->bandwidth_hz = 6000000;
974 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
975 c->bandwidth_hz = 7000000;
976 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
977 c->bandwidth_hz = 8000000;
979 /* Including BANDWIDTH_AUTO */
981 c->code_rate_HP = p->u.ofdm.code_rate_HP;
982 c->code_rate_LP = p->u.ofdm.code_rate_LP;
983 c->modulation = p->u.ofdm.constellation;
984 c->transmission_mode = p->u.ofdm.transmission_mode;
985 c->guard_interval = p->u.ofdm.guard_interval;
986 c->hierarchy = p->u.ofdm.hierarchy_information;
987 c->delivery_system = SYS_DVBT;
990 c->modulation = p->u.vsb.modulation;
991 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
992 c->delivery_system = SYS_ATSC;
994 c->delivery_system = SYS_DVBC_ANNEX_B;
999 /* Ensure the cached values are set correctly in the frontend
1000 * legacy tuning structures, for the advanced tuning API.
1002 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1004 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1005 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1006 struct dvb_frontend_parameters *p = &fepriv->parameters;
1008 p->frequency = c->frequency;
1009 p->inversion = c->inversion;
1011 switch (fe->ops.info.type) {
1013 dprintk("%s() Preparing QPSK req\n", __func__);
1014 p->u.qpsk.symbol_rate = c->symbol_rate;
1015 p->u.qpsk.fec_inner = c->fec_inner;
1016 c->delivery_system = SYS_DVBS;
1019 dprintk("%s() Preparing QAM req\n", __func__);
1020 p->u.qam.symbol_rate = c->symbol_rate;
1021 p->u.qam.fec_inner = c->fec_inner;
1022 p->u.qam.modulation = c->modulation;
1023 c->delivery_system = SYS_DVBC_ANNEX_AC;
1026 dprintk("%s() Preparing OFDM req\n", __func__);
1027 if (c->bandwidth_hz == 6000000)
1028 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1029 else if (c->bandwidth_hz == 7000000)
1030 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1031 else if (c->bandwidth_hz == 8000000)
1032 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1034 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1035 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1036 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1037 p->u.ofdm.constellation = c->modulation;
1038 p->u.ofdm.transmission_mode = c->transmission_mode;
1039 p->u.ofdm.guard_interval = c->guard_interval;
1040 p->u.ofdm.hierarchy_information = c->hierarchy;
1041 c->delivery_system = SYS_DVBT;
1044 dprintk("%s() Preparing VSB req\n", __func__);
1045 p->u.vsb.modulation = c->modulation;
1046 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1047 c->delivery_system = SYS_ATSC;
1049 c->delivery_system = SYS_DVBC_ANNEX_B;
1054 /* Ensure the cached values are set correctly in the frontend
1055 * legacy tuning structures, for the legacy tuning API.
1057 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1059 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1060 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1061 struct dvb_frontend_parameters *p = &fepriv->parameters;
1063 p->frequency = c->frequency;
1064 p->inversion = c->inversion;
1066 switch(c->modulation) {
1071 p->u.qpsk.symbol_rate = c->symbol_rate;
1072 p->u.qpsk.fec_inner = c->fec_inner;
1078 if(c->delivery_system == SYS_ISDBT) {
1079 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1080 p->frequency = c->frequency;
1081 p->inversion = INVERSION_AUTO;
1082 p->u.ofdm.constellation = QAM_AUTO;
1083 p->u.ofdm.code_rate_HP = FEC_AUTO;
1084 p->u.ofdm.code_rate_LP = FEC_AUTO;
1085 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1086 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1087 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1088 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1092 void dtv_property_cache_submit(struct dvb_frontend *fe)
1094 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1096 /* For legacy delivery systems we don't need the delivery_system to
1097 * be specified, but we populate the older structures from the cache
1098 * so we can call set_frontend on older drivers.
1100 if(is_legacy_delivery_system(c->delivery_system)) {
1102 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1103 dtv_property_legacy_params_sync(fe);
1106 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1108 /* For advanced delivery systems / modulation types ...
1109 * we seed the lecacy dvb_frontend_parameters structure
1110 * so that the sanity checking code later in the IOCTL processing
1111 * can validate our basic frequency ranges, symbolrates, modulation
1114 dtv_property_adv_params_sync(fe);
1118 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1119 unsigned int cmd, void *parg);
1120 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1121 unsigned int cmd, void *parg);
1123 int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
1124 struct inode *inode, struct file *file)
1128 dtv_property_dump(tvp);
1130 /* Allow the frontend to validate incoming properties */
1131 if (fe->ops.get_property)
1132 r = fe->ops.get_property(fe, tvp);
1139 tvp->u.data = fe->dtv_property_cache.frequency;
1141 case DTV_MODULATION:
1142 tvp->u.data = fe->dtv_property_cache.modulation;
1144 case DTV_BANDWIDTH_HZ:
1145 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1148 tvp->u.data = fe->dtv_property_cache.inversion;
1150 case DTV_SYMBOL_RATE:
1151 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1154 tvp->u.data = fe->dtv_property_cache.fec_inner;
1157 tvp->u.data = fe->dtv_property_cache.pilot;
1160 tvp->u.data = fe->dtv_property_cache.rolloff;
1162 case DTV_DELIVERY_SYSTEM:
1163 tvp->u.data = fe->dtv_property_cache.delivery_system;
1166 tvp->u.data = fe->dtv_property_cache.voltage;
1169 tvp->u.data = fe->dtv_property_cache.sectone;
1171 case DTV_API_VERSION:
1172 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1174 case DTV_CODE_RATE_HP:
1175 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1177 case DTV_CODE_RATE_LP:
1178 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1180 case DTV_GUARD_INTERVAL:
1181 tvp->u.data = fe->dtv_property_cache.guard_interval;
1183 case DTV_TRANSMISSION_MODE:
1184 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1187 tvp->u.data = fe->dtv_property_cache.hierarchy;
1196 int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
1197 struct inode *inode, struct file *file)
1200 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1201 dtv_property_dump(tvp);
1203 /* Allow the frontend to validate incoming properties */
1204 if (fe->ops.set_property)
1205 r = fe->ops.set_property(fe, tvp);
1212 /* Reset a cache of data specific to the frontend here. This does
1213 * not effect hardware.
1215 dprintk("%s() Flushing property cache\n", __func__);
1216 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1217 fe->dtv_property_cache.state = tvp->cmd;
1218 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1221 /* interpret the cache of data, build either a traditional frontend
1222 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1225 fe->dtv_property_cache.state = tvp->cmd;
1226 dprintk("%s() Finalised property cache\n", __func__);
1227 dtv_property_cache_submit(fe);
1229 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1230 &fepriv->parameters);
1233 fe->dtv_property_cache.frequency = tvp->u.data;
1235 case DTV_MODULATION:
1236 fe->dtv_property_cache.modulation = tvp->u.data;
1238 case DTV_BANDWIDTH_HZ:
1239 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1242 fe->dtv_property_cache.inversion = tvp->u.data;
1244 case DTV_SYMBOL_RATE:
1245 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1248 fe->dtv_property_cache.fec_inner = tvp->u.data;
1251 fe->dtv_property_cache.pilot = tvp->u.data;
1254 fe->dtv_property_cache.rolloff = tvp->u.data;
1256 case DTV_DELIVERY_SYSTEM:
1257 fe->dtv_property_cache.delivery_system = tvp->u.data;
1260 fe->dtv_property_cache.voltage = tvp->u.data;
1261 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1262 (void *)fe->dtv_property_cache.voltage);
1265 fe->dtv_property_cache.sectone = tvp->u.data;
1266 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1267 (void *)fe->dtv_property_cache.sectone);
1269 case DTV_CODE_RATE_HP:
1270 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1272 case DTV_CODE_RATE_LP:
1273 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1275 case DTV_GUARD_INTERVAL:
1276 fe->dtv_property_cache.guard_interval = tvp->u.data;
1278 case DTV_TRANSMISSION_MODE:
1279 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1282 fe->dtv_property_cache.hierarchy = tvp->u.data;
1291 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1292 unsigned int cmd, void *parg)
1294 struct dvb_device *dvbdev = file->private_data;
1295 struct dvb_frontend *fe = dvbdev->priv;
1296 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1297 int err = -EOPNOTSUPP;
1299 dprintk ("%s\n", __func__);
1304 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1305 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1306 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1309 if (down_interruptible (&fepriv->sem))
1310 return -ERESTARTSYS;
1312 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1313 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1315 fe->dtv_property_cache.state = DTV_UNDEFINED;
1316 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1323 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1324 unsigned int cmd, void *parg)
1326 struct dvb_device *dvbdev = file->private_data;
1327 struct dvb_frontend *fe = dvbdev->priv;
1330 struct dtv_properties *tvps = NULL;
1331 struct dtv_property *tvp = NULL;
1334 dprintk("%s\n", __func__);
1336 if(cmd == FE_SET_PROPERTY) {
1337 tvps = (struct dtv_properties __user *)parg;
1339 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1340 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1342 /* Put an arbitrary limit on the number of messages that can
1343 * be sent at once */
1344 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1347 tvp = (struct dtv_property *) kmalloc(tvps->num *
1348 sizeof(struct dtv_property), GFP_KERNEL);
1354 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1359 for (i = 0; i < tvps->num; i++) {
1360 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1361 err |= (tvp + i)->result;
1364 if(fe->dtv_property_cache.state == DTV_TUNE)
1365 dprintk("%s() Property cache is full, tuning\n", __func__);
1368 if(cmd == FE_GET_PROPERTY) {
1370 tvps = (struct dtv_properties __user *)parg;
1372 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1373 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1375 /* Put an arbitrary limit on the number of messages that can
1376 * be sent at once */
1377 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1380 tvp = (struct dtv_property *) kmalloc(tvps->num *
1381 sizeof(struct dtv_property), GFP_KERNEL);
1387 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1392 for (i = 0; i < tvps->num; i++) {
1393 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1394 err |= (tvp + i)->result;
1397 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1410 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1411 unsigned int cmd, void *parg)
1413 struct dvb_device *dvbdev = file->private_data;
1414 struct dvb_frontend *fe = dvbdev->priv;
1415 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1416 int err = -EOPNOTSUPP;
1420 struct dvb_frontend_info* info = parg;
1421 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1422 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1424 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1425 * do it, it is done for it. */
1426 info->caps |= FE_CAN_INVERSION_AUTO;
1431 case FE_READ_STATUS: {
1432 fe_status_t* status = parg;
1434 /* if retune was requested but hasn't occured yet, prevent
1435 * that user get signal state from previous tuning */
1436 if(fepriv->state == FESTATE_RETUNE) {
1442 if (fe->ops.read_status)
1443 err = fe->ops.read_status(fe, status);
1447 if (fe->ops.read_ber)
1448 err = fe->ops.read_ber(fe, (__u32*) parg);
1451 case FE_READ_SIGNAL_STRENGTH:
1452 if (fe->ops.read_signal_strength)
1453 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1457 if (fe->ops.read_snr)
1458 err = fe->ops.read_snr(fe, (__u16*) parg);
1461 case FE_READ_UNCORRECTED_BLOCKS:
1462 if (fe->ops.read_ucblocks)
1463 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1467 case FE_DISEQC_RESET_OVERLOAD:
1468 if (fe->ops.diseqc_reset_overload) {
1469 err = fe->ops.diseqc_reset_overload(fe);
1470 fepriv->state = FESTATE_DISEQC;
1475 case FE_DISEQC_SEND_MASTER_CMD:
1476 if (fe->ops.diseqc_send_master_cmd) {
1477 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1478 fepriv->state = FESTATE_DISEQC;
1483 case FE_DISEQC_SEND_BURST:
1484 if (fe->ops.diseqc_send_burst) {
1485 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1486 fepriv->state = FESTATE_DISEQC;
1492 if (fe->ops.set_tone) {
1493 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1494 fepriv->tone = (fe_sec_tone_mode_t) parg;
1495 fepriv->state = FESTATE_DISEQC;
1500 case FE_SET_VOLTAGE:
1501 if (fe->ops.set_voltage) {
1502 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1503 fepriv->voltage = (fe_sec_voltage_t) parg;
1504 fepriv->state = FESTATE_DISEQC;
1509 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1510 if (fe->ops.dishnetwork_send_legacy_command) {
1511 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1512 fepriv->state = FESTATE_DISEQC;
1514 } else if (fe->ops.set_voltage) {
1516 * NOTE: This is a fallback condition. Some frontends
1517 * (stv0299 for instance) take longer than 8msec to
1518 * respond to a set_voltage command. Those switches
1519 * need custom routines to switch properly. For all
1520 * other frontends, the following shoule work ok.
1521 * Dish network legacy switches (as used by Dish500)
1522 * are controlled by sending 9-bit command words
1523 * spaced 8msec apart.
1524 * the actual command word is switch/port dependant
1525 * so it is up to the userspace application to send
1526 * the right command.
1527 * The command must always start with a '0' after
1528 * initialization, so parg is 8 bits and does not
1529 * include the initialization or start bit
1531 unsigned long swcmd = ((unsigned long) parg) << 1;
1532 struct timeval nexttime;
1533 struct timeval tv[10];
1536 if (dvb_frontend_debug)
1537 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1538 do_gettimeofday(&nexttime);
1539 if (dvb_frontend_debug)
1540 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1541 /* before sending a command, initialize by sending
1542 * a 32ms 18V to the switch
1544 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1545 dvb_frontend_sleep_until(&nexttime, 32000);
1547 for (i = 0; i < 9; i++) {
1548 if (dvb_frontend_debug)
1549 do_gettimeofday(&tv[i + 1]);
1550 if ((swcmd & 0x01) != last) {
1551 /* set voltage to (last ? 13V : 18V) */
1552 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1553 last = (last) ? 0 : 1;
1557 dvb_frontend_sleep_until(&nexttime, 8000);
1559 if (dvb_frontend_debug) {
1560 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1561 __func__, fe->dvb->num);
1562 for (i = 1; i < 10; i++)
1563 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1566 fepriv->state = FESTATE_DISEQC;
1571 case FE_DISEQC_RECV_SLAVE_REPLY:
1572 if (fe->ops.diseqc_recv_slave_reply)
1573 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1576 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1577 if (fe->ops.enable_high_lnb_voltage)
1578 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1581 case FE_SET_FRONTEND: {
1582 struct dvb_frontend_tune_settings fetunesettings;
1584 if(fe->dtv_property_cache.state == DTV_TUNE) {
1585 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1590 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1595 memcpy (&fepriv->parameters, parg,
1596 sizeof (struct dvb_frontend_parameters));
1597 dtv_property_cache_sync(fe, &fepriv->parameters);
1600 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1601 memcpy(&fetunesettings.parameters, parg,
1602 sizeof (struct dvb_frontend_parameters));
1604 /* force auto frequency inversion if requested */
1605 if (dvb_force_auto_inversion) {
1606 fepriv->parameters.inversion = INVERSION_AUTO;
1607 fetunesettings.parameters.inversion = INVERSION_AUTO;
1609 if (fe->ops.info.type == FE_OFDM) {
1610 /* without hierarchical coding code_rate_LP is irrelevant,
1611 * so we tolerate the otherwise invalid FEC_NONE setting */
1612 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1613 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1614 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1617 /* get frontend-specific tuning settings */
1618 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1619 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1620 fepriv->max_drift = fetunesettings.max_drift;
1621 fepriv->step_size = fetunesettings.step_size;
1623 /* default values */
1624 switch(fe->ops.info.type) {
1626 fepriv->min_delay = HZ/20;
1627 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1628 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1632 fepriv->min_delay = HZ/20;
1633 fepriv->step_size = 0; /* no zigzag */
1634 fepriv->max_drift = 0;
1638 fepriv->min_delay = HZ/20;
1639 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1640 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1643 fepriv->min_delay = HZ/20;
1644 fepriv->step_size = 0;
1645 fepriv->max_drift = 0;
1649 if (dvb_override_tune_delay > 0)
1650 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1652 fepriv->state = FESTATE_RETUNE;
1653 dvb_frontend_wakeup(fe);
1654 dvb_frontend_add_event(fe, 0);
1661 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1664 case FE_GET_FRONTEND:
1665 if (fe->ops.get_frontend) {
1666 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1667 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1671 case FE_SET_FRONTEND_TUNE_MODE:
1672 fepriv->tune_mode_flags = (unsigned long) parg;
1681 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1683 struct dvb_device *dvbdev = file->private_data;
1684 struct dvb_frontend *fe = dvbdev->priv;
1685 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1687 dprintk ("%s\n", __func__);
1689 poll_wait (file, &fepriv->events.wait_queue, wait);
1691 if (fepriv->events.eventw != fepriv->events.eventr)
1692 return (POLLIN | POLLRDNORM | POLLPRI);
1697 static int dvb_frontend_open(struct inode *inode, struct file *file)
1699 struct dvb_device *dvbdev = file->private_data;
1700 struct dvb_frontend *fe = dvbdev->priv;
1701 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1702 struct dvb_adapter *adapter = fe->dvb;
1705 dprintk ("%s\n", __func__);
1707 if (adapter->mfe_shared) {
1708 mutex_lock (&adapter->mfe_lock);
1710 if (adapter->mfe_dvbdev == NULL)
1711 adapter->mfe_dvbdev = dvbdev;
1713 else if (adapter->mfe_dvbdev != dvbdev) {
1715 *mfedev = adapter->mfe_dvbdev;
1717 *mfe = mfedev->priv;
1718 struct dvb_frontend_private
1719 *mfepriv = mfe->frontend_priv;
1720 int mferetry = (dvb_mfe_wait_time << 1);
1722 mutex_unlock (&adapter->mfe_lock);
1723 while (mferetry-- && (mfedev->users != -1 ||
1724 mfepriv->thread != NULL)) {
1725 if(msleep_interruptible(500)) {
1726 if(signal_pending(current))
1731 mutex_lock (&adapter->mfe_lock);
1732 if(adapter->mfe_dvbdev != dvbdev) {
1733 mfedev = adapter->mfe_dvbdev;
1735 mfepriv = mfe->frontend_priv;
1736 if (mfedev->users != -1 ||
1737 mfepriv->thread != NULL) {
1738 mutex_unlock (&adapter->mfe_lock);
1741 adapter->mfe_dvbdev = dvbdev;
1746 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1747 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1751 if ((ret = dvb_generic_open (inode, file)) < 0)
1754 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1755 /* normal tune mode when opened R/W */
1756 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1758 fepriv->voltage = -1;
1760 ret = dvb_frontend_start (fe);
1764 /* empty event queue */
1765 fepriv->events.eventr = fepriv->events.eventw = 0;
1768 if (adapter->mfe_shared)
1769 mutex_unlock (&adapter->mfe_lock);
1773 dvb_generic_release(inode, file);
1775 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1776 fe->ops.ts_bus_ctrl(fe, 0);
1778 if (adapter->mfe_shared)
1779 mutex_unlock (&adapter->mfe_lock);
1783 static int dvb_frontend_release(struct inode *inode, struct file *file)
1785 struct dvb_device *dvbdev = file->private_data;
1786 struct dvb_frontend *fe = dvbdev->priv;
1787 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1790 dprintk ("%s\n", __func__);
1792 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1793 fepriv->release_jiffies = jiffies;
1795 ret = dvb_generic_release (inode, file);
1797 if (dvbdev->users == -1) {
1798 if (fepriv->exit == 1) {
1799 fops_put(file->f_op);
1801 wake_up(&dvbdev->wait_queue);
1803 if (fe->ops.ts_bus_ctrl)
1804 fe->ops.ts_bus_ctrl(fe, 0);
1810 static struct file_operations dvb_frontend_fops = {
1811 .owner = THIS_MODULE,
1812 .ioctl = dvb_generic_ioctl,
1813 .poll = dvb_frontend_poll,
1814 .open = dvb_frontend_open,
1815 .release = dvb_frontend_release
1818 int dvb_register_frontend(struct dvb_adapter* dvb,
1819 struct dvb_frontend* fe)
1821 struct dvb_frontend_private *fepriv;
1822 static const struct dvb_device dvbdev_template = {
1826 .fops = &dvb_frontend_fops,
1827 .kernel_ioctl = dvb_frontend_ioctl
1830 dprintk ("%s\n", __func__);
1832 if (mutex_lock_interruptible(&frontend_mutex))
1833 return -ERESTARTSYS;
1835 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1836 if (fe->frontend_priv == NULL) {
1837 mutex_unlock(&frontend_mutex);
1840 fepriv = fe->frontend_priv;
1842 init_MUTEX (&fepriv->sem);
1843 init_waitqueue_head (&fepriv->wait_queue);
1844 init_waitqueue_head (&fepriv->events.wait_queue);
1845 mutex_init(&fepriv->events.mtx);
1847 fepriv->inversion = INVERSION_OFF;
1849 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1854 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1855 fe, DVB_DEVICE_FRONTEND);
1857 mutex_unlock(&frontend_mutex);
1860 EXPORT_SYMBOL(dvb_register_frontend);
1862 int dvb_unregister_frontend(struct dvb_frontend* fe)
1864 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1865 dprintk ("%s\n", __func__);
1867 mutex_lock(&frontend_mutex);
1868 dvb_frontend_stop (fe);
1869 mutex_unlock(&frontend_mutex);
1871 if (fepriv->dvbdev->users < -1)
1872 wait_event(fepriv->dvbdev->wait_queue,
1873 fepriv->dvbdev->users==-1);
1875 mutex_lock(&frontend_mutex);
1876 dvb_unregister_device (fepriv->dvbdev);
1878 /* fe is invalid now */
1880 mutex_unlock(&frontend_mutex);
1883 EXPORT_SYMBOL(dvb_unregister_frontend);
1885 #ifdef CONFIG_MEDIA_ATTACH
1886 void dvb_frontend_detach(struct dvb_frontend* fe)
1890 if (fe->ops.release_sec) {
1891 fe->ops.release_sec(fe);
1892 symbol_put_addr(fe->ops.release_sec);
1894 if (fe->ops.tuner_ops.release) {
1895 fe->ops.tuner_ops.release(fe);
1896 symbol_put_addr(fe->ops.tuner_ops.release);
1898 if (fe->ops.analog_ops.release) {
1899 fe->ops.analog_ops.release(fe);
1900 symbol_put_addr(fe->ops.analog_ops.release);
1902 ptr = (void*)fe->ops.release;
1904 fe->ops.release(fe);
1905 symbol_put_addr(ptr);
1909 void dvb_frontend_detach(struct dvb_frontend* fe)
1911 if (fe->ops.release_sec)
1912 fe->ops.release_sec(fe);
1913 if (fe->ops.tuner_ops.release)
1914 fe->ops.tuner_ops.release(fe);
1915 if (fe->ops.analog_ops.release)
1916 fe->ops.analog_ops.release(fe);
1917 if (fe->ops.release)
1918 fe->ops.release(fe);
1921 EXPORT_SYMBOL(dvb_frontend_detach);