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"
44 static int dvb_frontend_debug;
45 static int dvb_shutdown_timeout;
46 static int dvb_force_auto_inversion;
47 static int dvb_override_tune_delay;
48 static int dvb_powerdown_on_sleep = 1;
50 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
51 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
52 module_param(dvb_shutdown_timeout, int, 0644);
53 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
54 module_param(dvb_force_auto_inversion, int, 0644);
55 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
56 module_param(dvb_override_tune_delay, int, 0644);
57 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
58 module_param(dvb_powerdown_on_sleep, int, 0644);
59 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
61 #define dprintk if (dvb_frontend_debug) printk
63 #define FESTATE_IDLE 1
64 #define FESTATE_RETUNE 2
65 #define FESTATE_TUNING_FAST 4
66 #define FESTATE_TUNING_SLOW 8
67 #define FESTATE_TUNED 16
68 #define FESTATE_ZIGZAG_FAST 32
69 #define FESTATE_ZIGZAG_SLOW 64
70 #define FESTATE_DISEQC 128
71 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
72 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
73 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
74 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
78 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
79 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
80 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
81 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
82 * FESTATE_TUNED. The frontend has successfully locked on.
83 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
84 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
85 * FESTATE_DISEQC. A DISEQC command has just been issued.
86 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
87 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
88 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
89 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
92 static DEFINE_MUTEX(frontend_mutex);
94 struct dvb_frontend_private {
96 /* thread/frontend values */
97 struct dvb_device *dvbdev;
98 struct dvb_frontend_parameters parameters;
99 struct dvb_fe_events events;
100 struct semaphore sem;
101 struct list_head list_head;
102 wait_queue_head_t wait_queue;
103 struct task_struct *thread;
104 unsigned long release_jiffies;
108 unsigned long tune_mode_flags;
110 unsigned int reinitialise;
114 /* swzigzag values */
116 unsigned int bending;
118 unsigned int inversion;
119 unsigned int auto_step;
120 unsigned int auto_sub_step;
121 unsigned int started_auto_step;
122 unsigned int min_delay;
123 unsigned int max_drift;
124 unsigned int step_size;
126 unsigned int check_wrapped;
129 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
131 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
133 struct dvb_frontend_private *fepriv = fe->frontend_priv;
134 struct dvb_fe_events *events = &fepriv->events;
135 struct dvb_frontend_event *e;
138 dprintk ("%s\n", __func__);
140 if (mutex_lock_interruptible (&events->mtx))
143 wp = (events->eventw + 1) % MAX_EVENT;
145 if (wp == events->eventr) {
146 events->overflow = 1;
147 events->eventr = (events->eventr + 1) % MAX_EVENT;
150 e = &events->events[events->eventw];
152 memcpy (&e->parameters, &fepriv->parameters,
153 sizeof (struct dvb_frontend_parameters));
155 if (status & FE_HAS_LOCK)
156 if (fe->ops.get_frontend)
157 fe->ops.get_frontend(fe, &e->parameters);
161 mutex_unlock(&events->mtx);
165 wake_up_interruptible (&events->wait_queue);
168 static int dvb_frontend_get_event(struct dvb_frontend *fe,
169 struct dvb_frontend_event *event, int flags)
171 struct dvb_frontend_private *fepriv = fe->frontend_priv;
172 struct dvb_fe_events *events = &fepriv->events;
174 dprintk ("%s\n", __func__);
176 if (events->overflow) {
177 events->overflow = 0;
181 if (events->eventw == events->eventr) {
184 if (flags & O_NONBLOCK)
189 ret = wait_event_interruptible (events->wait_queue,
190 events->eventw != events->eventr);
192 if (down_interruptible (&fepriv->sem))
199 if (mutex_lock_interruptible (&events->mtx))
202 memcpy (event, &events->events[events->eventr],
203 sizeof(struct dvb_frontend_event));
205 events->eventr = (events->eventr + 1) % MAX_EVENT;
207 mutex_unlock(&events->mtx);
212 static void dvb_frontend_init(struct dvb_frontend *fe)
214 dprintk ("DVB: initialising frontend %i (%s)...\n",
220 if (fe->ops.tuner_ops.init) {
221 fe->ops.tuner_ops.init(fe);
222 if (fe->ops.i2c_gate_ctrl)
223 fe->ops.i2c_gate_ctrl(fe, 0);
227 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
229 struct dvb_frontend_private *fepriv = fe->frontend_priv;
231 fepriv->reinitialise = 1;
232 dvb_frontend_wakeup(fe);
234 EXPORT_SYMBOL(dvb_frontend_reinitialise);
236 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
240 dprintk ("%s\n", __func__);
243 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
245 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
247 q2 = fepriv->quality - 128;
250 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
254 * Performs automatic twiddling of frontend parameters.
256 * @param fe The frontend concerned.
257 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
258 * @returns Number of complete iterations that have been performed.
260 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
264 struct dvb_frontend_private *fepriv = fe->frontend_priv;
265 int original_inversion = fepriv->parameters.inversion;
266 u32 original_frequency = fepriv->parameters.frequency;
268 /* are we using autoinversion? */
269 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
270 (fepriv->parameters.inversion == INVERSION_AUTO));
272 /* setup parameters correctly */
274 /* calculate the lnb_drift */
275 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
277 /* wrap the auto_step if we've exceeded the maximum drift */
278 if (fepriv->lnb_drift > fepriv->max_drift) {
279 fepriv->auto_step = 0;
280 fepriv->auto_sub_step = 0;
281 fepriv->lnb_drift = 0;
284 /* perform inversion and +/- zigzag */
285 switch(fepriv->auto_sub_step) {
287 /* try with the current inversion and current drift setting */
292 if (!autoinversion) break;
294 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
299 if (fepriv->lnb_drift == 0) break;
301 fepriv->lnb_drift = -fepriv->lnb_drift;
306 if (fepriv->lnb_drift == 0) break;
307 if (!autoinversion) break;
309 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
310 fepriv->lnb_drift = -fepriv->lnb_drift;
316 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
320 if (!ready) fepriv->auto_sub_step++;
323 /* if this attempt would hit where we started, indicate a complete
324 * iteration has occurred */
325 if ((fepriv->auto_step == fepriv->started_auto_step) &&
326 (fepriv->auto_sub_step == 0) && check_wrapped) {
330 dprintk("%s: drift:%i inversion:%i auto_step:%i "
331 "auto_sub_step:%i started_auto_step:%i\n",
332 __func__, fepriv->lnb_drift, fepriv->inversion,
333 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
335 /* set the frontend itself */
336 fepriv->parameters.frequency += fepriv->lnb_drift;
338 fepriv->parameters.inversion = fepriv->inversion;
339 if (fe->ops.set_frontend)
340 fe->ops.set_frontend(fe, &fepriv->parameters);
342 fepriv->parameters.frequency = original_frequency;
343 fepriv->parameters.inversion = original_inversion;
345 fepriv->auto_sub_step++;
349 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
352 struct dvb_frontend_private *fepriv = fe->frontend_priv;
354 /* if we've got no parameters, just keep idling */
355 if (fepriv->state & FESTATE_IDLE) {
356 fepriv->delay = 3*HZ;
361 /* in SCAN mode, we just set the frontend when asked and leave it alone */
362 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
363 if (fepriv->state & FESTATE_RETUNE) {
364 if (fe->ops.set_frontend)
365 fe->ops.set_frontend(fe, &fepriv->parameters);
366 fepriv->state = FESTATE_TUNED;
368 fepriv->delay = 3*HZ;
373 /* get the frontend status */
374 if (fepriv->state & FESTATE_RETUNE) {
377 if (fe->ops.read_status)
378 fe->ops.read_status(fe, &s);
379 if (s != fepriv->status) {
380 dvb_frontend_add_event(fe, s);
385 /* if we're not tuned, and we have a lock, move to the TUNED state */
386 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
387 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
388 fepriv->state = FESTATE_TUNED;
390 /* if we're tuned, then we have determined the correct inversion */
391 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
392 (fepriv->parameters.inversion == INVERSION_AUTO)) {
393 fepriv->parameters.inversion = fepriv->inversion;
398 /* if we are tuned already, check we're still locked */
399 if (fepriv->state & FESTATE_TUNED) {
400 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
402 /* we're tuned, and the lock is still good... */
403 if (s & FE_HAS_LOCK) {
405 } else { /* if we _WERE_ tuned, but now don't have a lock */
406 fepriv->state = FESTATE_ZIGZAG_FAST;
407 fepriv->started_auto_step = fepriv->auto_step;
408 fepriv->check_wrapped = 0;
412 /* don't actually do anything if we're in the LOSTLOCK state,
413 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
414 if ((fepriv->state & FESTATE_LOSTLOCK) &&
415 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
416 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
420 /* don't do anything if we're in the DISEQC state, since this
421 * might be someone with a motorized dish controlled by DISEQC.
422 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
423 if (fepriv->state & FESTATE_DISEQC) {
424 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
428 /* if we're in the RETUNE state, set everything up for a brand
429 * new scan, keeping the current inversion setting, as the next
430 * tune is _very_ likely to require the same */
431 if (fepriv->state & FESTATE_RETUNE) {
432 fepriv->lnb_drift = 0;
433 fepriv->auto_step = 0;
434 fepriv->auto_sub_step = 0;
435 fepriv->started_auto_step = 0;
436 fepriv->check_wrapped = 0;
440 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
441 fepriv->delay = fepriv->min_delay;
444 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
445 /* OK, if we've run out of trials at the fast speed.
446 * Drop back to slow for the _next_ attempt */
447 fepriv->state = FESTATE_SEARCHING_SLOW;
448 fepriv->started_auto_step = fepriv->auto_step;
451 fepriv->check_wrapped = 1;
453 /* if we've just retuned, enter the ZIGZAG_FAST state.
454 * This ensures we cannot return from an
455 * FE_SET_FRONTEND ioctl before the first frontend tune
457 if (fepriv->state & FESTATE_RETUNE) {
458 fepriv->state = FESTATE_TUNING_FAST;
463 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
464 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
466 /* Note: don't bother checking for wrapping; we stay in this
467 * state until we get a lock */
468 dvb_frontend_swzigzag_autotune(fe, 0);
472 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
474 struct dvb_frontend_private *fepriv = fe->frontend_priv;
479 if (fepriv->dvbdev->writers == 1)
480 if (time_after(jiffies, fepriv->release_jiffies +
481 dvb_shutdown_timeout * HZ))
487 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
489 struct dvb_frontend_private *fepriv = fe->frontend_priv;
491 if (fepriv->wakeup) {
495 return dvb_frontend_is_exiting(fe);
498 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
500 struct dvb_frontend_private *fepriv = fe->frontend_priv;
503 wake_up_interruptible(&fepriv->wait_queue);
506 static int dvb_frontend_thread(void *data)
508 struct dvb_frontend *fe = data;
509 struct dvb_frontend_private *fepriv = fe->frontend_priv;
510 unsigned long timeout;
512 struct dvb_frontend_parameters *params;
514 dprintk("%s\n", __func__);
516 fepriv->check_wrapped = 0;
518 fepriv->delay = 3*HZ;
521 fepriv->reinitialise = 0;
523 dvb_frontend_init(fe);
527 up(&fepriv->sem); /* is locked when we enter the thread... */
529 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
530 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
531 || freezing(current),
534 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
535 /* got signal or quitting */
542 if (down_interruptible(&fepriv->sem))
545 if (fepriv->reinitialise) {
546 dvb_frontend_init(fe);
547 if (fepriv->tone != -1) {
548 fe->ops.set_tone(fe, fepriv->tone);
550 if (fepriv->voltage != -1) {
551 fe->ops.set_voltage(fe, fepriv->voltage);
553 fepriv->reinitialise = 0;
556 /* do an iteration of the tuning loop */
557 if (fe->ops.get_frontend_algo) {
558 if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
559 /* have we been asked to retune? */
561 if (fepriv->state & FESTATE_RETUNE) {
562 params = &fepriv->parameters;
563 fepriv->state = FESTATE_TUNED;
566 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
567 if (s != fepriv->status) {
568 dvb_frontend_add_event(fe, s);
572 dvb_frontend_swzigzag(fe);
574 dvb_frontend_swzigzag(fe);
577 if (dvb_powerdown_on_sleep) {
578 if (fe->ops.set_voltage)
579 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
580 if (fe->ops.tuner_ops.sleep) {
581 fe->ops.tuner_ops.sleep(fe);
582 if (fe->ops.i2c_gate_ctrl)
583 fe->ops.i2c_gate_ctrl(fe, 0);
589 fepriv->thread = NULL;
592 dvb_frontend_wakeup(fe);
596 static void dvb_frontend_stop(struct dvb_frontend *fe)
598 struct dvb_frontend_private *fepriv = fe->frontend_priv;
600 dprintk ("%s\n", __func__);
608 kthread_stop(fepriv->thread);
610 init_MUTEX (&fepriv->sem);
611 fepriv->state = FESTATE_IDLE;
613 /* paranoia check in case a signal arrived */
615 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
619 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
621 return ((curtime.tv_usec < lasttime.tv_usec) ?
622 1000000 - lasttime.tv_usec + curtime.tv_usec :
623 curtime.tv_usec - lasttime.tv_usec);
625 EXPORT_SYMBOL(timeval_usec_diff);
627 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
629 curtime->tv_usec += add_usec;
630 if (curtime->tv_usec >= 1000000) {
631 curtime->tv_usec -= 1000000;
637 * Sleep until gettimeofday() > waketime + add_usec
638 * This needs to be as precise as possible, but as the delay is
639 * usually between 2ms and 32ms, it is done using a scheduled msleep
640 * followed by usleep (normally a busy-wait loop) for the remainder
642 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
644 struct timeval lasttime;
647 timeval_usec_add(waketime, add_usec);
649 do_gettimeofday(&lasttime);
650 delta = timeval_usec_diff(lasttime, *waketime);
652 msleep((delta - 1500) / 1000);
653 do_gettimeofday(&lasttime);
654 newdelta = timeval_usec_diff(lasttime, *waketime);
655 delta = (newdelta > delta) ? 0 : newdelta;
660 EXPORT_SYMBOL(dvb_frontend_sleep_until);
662 static int dvb_frontend_start(struct dvb_frontend *fe)
665 struct dvb_frontend_private *fepriv = fe->frontend_priv;
666 struct task_struct *fe_thread;
668 dprintk ("%s\n", __func__);
670 if (fepriv->thread) {
674 dvb_frontend_stop (fe);
677 if (signal_pending(current))
679 if (down_interruptible (&fepriv->sem))
682 fepriv->state = FESTATE_IDLE;
684 fepriv->thread = NULL;
687 fe_thread = kthread_run(dvb_frontend_thread, fe,
688 "kdvb-fe-%i", fe->dvb->num);
689 if (IS_ERR(fe_thread)) {
690 ret = PTR_ERR(fe_thread);
691 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
695 fepriv->thread = fe_thread;
699 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
700 u32 *freq_min, u32 *freq_max)
702 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
704 if (fe->ops.info.frequency_max == 0)
705 *freq_max = fe->ops.tuner_ops.info.frequency_max;
706 else if (fe->ops.tuner_ops.info.frequency_max == 0)
707 *freq_max = fe->ops.info.frequency_max;
709 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
711 if (*freq_min == 0 || *freq_max == 0)
712 printk(KERN_WARNING "DVB: frontend %u frequency limits undefined - fix the driver\n",
716 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
717 struct dvb_frontend_parameters *parms)
722 /* range check: frequency */
723 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
724 if ((freq_min && parms->frequency < freq_min) ||
725 (freq_max && parms->frequency > freq_max)) {
726 printk(KERN_WARNING "DVB: frontend %u frequency %u out of range (%u..%u)\n",
727 fe->dvb->num, parms->frequency, freq_min, freq_max);
731 /* range check: symbol rate */
732 if (fe->ops.info.type == FE_QPSK) {
733 if ((fe->ops.info.symbol_rate_min &&
734 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
735 (fe->ops.info.symbol_rate_max &&
736 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
737 printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
738 fe->dvb->num, parms->u.qpsk.symbol_rate,
739 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
743 } else if (fe->ops.info.type == FE_QAM) {
744 if ((fe->ops.info.symbol_rate_min &&
745 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
746 (fe->ops.info.symbol_rate_max &&
747 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
748 printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
749 fe->dvb->num, parms->u.qam.symbol_rate,
750 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
758 struct dtv_cmds_h dtv_cmds[] = {
771 [DTV_SET_FREQUENCY] = {
772 .name = "DTV_SET_FREQUENCY",
773 .cmd = DTV_SET_FREQUENCY,
776 [DTV_SET_BANDWIDTH] = {
777 .name = "DTV_SET_BANDWIDTH",
778 .cmd = DTV_SET_BANDWIDTH,
781 [DTV_SET_MODULATION] = {
782 .name = "DTV_SET_MODULATION",
783 .cmd = DTV_SET_MODULATION,
786 [DTV_SET_INVERSION] = {
787 .name = "DTV_SET_INVERSION",
788 .cmd = DTV_SET_INVERSION,
791 [DTV_SET_DISEQC_MASTER] = {
792 .name = "DTV_SET_DISEQC_MASTER",
793 .cmd = DTV_SET_DISEQC_MASTER,
797 [DTV_SET_SYMBOL_RATE] = {
798 .name = "DTV_SET_SYMBOL_RATE",
799 .cmd = DTV_SET_SYMBOL_RATE,
802 [DTV_SET_INNER_FEC] = {
803 .name = "DTV_SET_INNER_FEC",
804 .cmd = DTV_SET_INNER_FEC,
807 [DTV_SET_VOLTAGE] = {
808 .name = "DTV_SET_VOLTAGE",
809 .cmd = DTV_SET_VOLTAGE,
813 .name = "DTV_SET_TONE",
818 .name = "DTV_SET_PILOT",
819 .cmd = DTV_SET_PILOT,
822 [DTV_SET_ROLLOFF] = {
823 .name = "DTV_SET_ROLLOFF",
824 .cmd = DTV_SET_ROLLOFF,
827 [DTV_SET_DELIVERY_SYSTEM] = {
828 .name = "DTV_SET_DELIVERY_SYSTEM",
829 .cmd = DTV_SET_DELIVERY_SYSTEM,
832 [DTV_SET_ISDB_SEGMENT_NUM] = {
833 .name = "DTV_SET_ISDB_SEGMENT_NUM",
834 .cmd = DTV_SET_ISDB_SEGMENT_NUM,
837 [DTV_SET_ISDB_SEGMENT_WIDTH] = {
838 .name = "DTV_SET_ISDB_SEGMENT_WIDTH",
839 .cmd = DTV_SET_ISDB_SEGMENT_WIDTH,
844 [DTV_GET_FREQUENCY] = {
845 .name = "DTV_GET_FREQUENCY",
846 .cmd = DTV_GET_FREQUENCY,
849 [DTV_GET_BANDWIDTH] = {
850 .name = "DTV_GET_BANDWIDTH",
851 .cmd = DTV_GET_BANDWIDTH,
854 [DTV_GET_MODULATION] = {
855 .name = "DTV_GET_MODULATION",
856 .cmd = DTV_GET_MODULATION,
859 [DTV_GET_INVERSION] = {
860 .name = "DTV_GET_INVERSION",
861 .cmd = DTV_GET_INVERSION,
864 [DTV_GET_DISEQC_SLAVE_REPLY] = {
865 .name = "DTV_GET_DISEQC_SLAVE_REPLY",
866 .cmd = DTV_GET_DISEQC_SLAVE_REPLY,
870 [DTV_GET_SYMBOL_RATE] = {
871 .name = "DTV_GET_SYMBOL_RATE",
872 .cmd = DTV_GET_SYMBOL_RATE,
875 [DTV_GET_INNER_FEC] = {
876 .name = "DTV_GET_INNER_FEC",
877 .cmd = DTV_GET_INNER_FEC,
880 [DTV_GET_VOLTAGE] = {
881 .name = "DTV_GET_VOLTAGE",
882 .cmd = DTV_GET_VOLTAGE,
886 .name = "DTV_GET_TONE",
891 .name = "DTV_GET_PILOT",
892 .cmd = DTV_GET_PILOT,
895 [DTV_GET_ROLLOFF] = {
896 .name = "DTV_GET_ROLLOFF",
897 .cmd = DTV_GET_ROLLOFF,
900 [DTV_GET_DELIVERY_SYSTEM] = {
901 .name = "DTV_GET_DELIVERY_SYSTEM",
902 .cmd = DTV_GET_DELIVERY_SYSTEM,
905 [DTV_GET_ISDB_SEGMENT_NUM] = {
906 .name = "DTV_GET_ISDB_SEGMENT_NUM",
907 .cmd = DTV_GET_ISDB_SEGMENT_NUM,
910 [DTV_GET_ISDB_SEGMENT_WIDTH] = {
911 .name = "DTV_GET_ISDB_SEGMENT_WIDTH",
912 .cmd = DTV_GET_ISDB_SEGMENT_WIDTH,
915 [DTV_GET_ISDB_LAYERA_FEC] = {
916 .name = "DTV_GET_ISDB_LAYERA_FEC",
917 .cmd = DTV_GET_ISDB_LAYERA_FEC,
920 [DTV_GET_ISDB_LAYERA_MODULATION] = {
921 .name = "DTV_GET_ISDB_LAYERA_MODULATION",
922 .cmd = DTV_GET_ISDB_LAYERA_MODULATION,
925 [DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH] = {
926 .name = "DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH",
927 .cmd = DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH,
930 [DTV_GET_ISDB_LAYERB_FEC] = {
931 .name = "DTV_GET_ISDB_LAYERB_FEC",
932 .cmd = DTV_GET_ISDB_LAYERB_FEC,
935 [DTV_GET_ISDB_LAYERB_MODULATION] = {
936 .name = "DTV_GET_ISDB_LAYERB_MODULATION",
937 .cmd = DTV_GET_ISDB_LAYERB_MODULATION,
940 [DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH] = {
941 .name = "DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH",
942 .cmd = DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH,
945 [DTV_GET_ISDB_LAYERC_FEC] = {
946 .name = "DTV_GET_ISDB_LAYERC_FEC",
947 .cmd = DTV_GET_ISDB_LAYERC_FEC,
950 [DTV_GET_ISDB_LAYERC_MODULATION] = {
951 .name = "DTV_GET_ISDB_LAYERC_MODULATION",
952 .cmd = DTV_GET_ISDB_LAYERC_MODULATION,
955 [DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH] = {
956 .name = "DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH",
957 .cmd = DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH,
962 void dtv_property_dump(struct dtv_property *tvp)
966 printk("%s() tvp.cmd = 0x%08x (%s)\n"
969 ,dtv_cmds[ tvp->cmd ].name);
971 if(dtv_cmds[ tvp->cmd ].buffer) {
973 printk("%s() tvp.u.buffer.len = 0x%02x\n"
977 for(i = 0; i < tvp->u.buffer.len; i++)
978 printk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
981 ,tvp->u.buffer.data[i]);
984 printk("%s() tvp.u.data = 0x%08x\n", __FUNCTION__, tvp->u.data);
987 int is_legacy_delivery_system(fe_delivery_system_t s)
989 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
990 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
996 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
997 * drivers can use a single set_frontend tuning function, regardless of whether
998 * it's being used for the legacy or new API, reducing code and complexity.
1000 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
1002 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1004 printk("%s()\n", __FUNCTION__);
1006 c->frequency = p->frequency;
1007 c->inversion = p->inversion;
1009 switch (fe->ops.info.type) {
1011 c->symbol_rate = p->u.qpsk.symbol_rate;
1012 c->fec_inner = p->u.qpsk.fec_inner;
1015 c->symbol_rate = p->u.qam.symbol_rate;
1016 c->fec_inner = p->u.qam.fec_inner;
1017 c->modulation = p->u.qam.modulation;
1020 c->bandwidth = p->u.ofdm.bandwidth;
1021 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1022 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1023 c->modulation = p->u.ofdm.constellation;
1024 c->transmission_mode = p->u.ofdm.transmission_mode;
1025 c->guard_interval = p->u.ofdm.guard_interval;
1026 c->hierarchy = p->u.ofdm.hierarchy_information;
1029 c->modulation = p->u.vsb.modulation;
1034 /* Ensure the cached values are set correctly in the frontend
1035 * legacy tuning structures, for the advanced tuning API.
1037 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1039 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1040 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1041 struct dvb_frontend_parameters *p = &fepriv->parameters;
1043 printk("%s()\n", __FUNCTION__);
1045 p->frequency = c->frequency;
1046 p->inversion = c->inversion;
1048 switch (fe->ops.info.type) {
1050 printk("%s() Preparing QPSK req\n", __FUNCTION__);
1051 p->u.qpsk.symbol_rate = c->symbol_rate;
1052 p->u.qpsk.fec_inner = c->fec_inner;
1055 printk("%s() Preparing QAM req\n", __FUNCTION__);
1056 p->u.qam.symbol_rate = c->symbol_rate;
1057 p->u.qam.fec_inner = c->fec_inner;
1058 p->u.qam.modulation = c->modulation;
1061 printk("%s() Preparing OFDM req\n", __FUNCTION__);
1062 p->u.ofdm.bandwidth = c->bandwidth;
1063 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1064 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1065 p->u.ofdm.constellation = c->modulation;
1066 p->u.ofdm.transmission_mode = c->transmission_mode;
1067 p->u.ofdm.guard_interval = c->guard_interval;
1068 p->u.ofdm.hierarchy_information = c->hierarchy;
1071 printk("%s() Preparing VSB req\n", __FUNCTION__);
1072 p->u.vsb.modulation = c->modulation;
1077 /* Ensure the cached values are set correctly in the frontend
1078 * legacy tuning structures, for the legacy tuning API.
1080 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1082 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1083 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1084 struct dvb_frontend_parameters *p = &fepriv->parameters;
1086 printk("%s()\n", __FUNCTION__);
1088 p->frequency = c->frequency;
1089 p->inversion = c->inversion;
1091 switch(c->modulation) {
1095 p->u.qpsk.symbol_rate = c->symbol_rate;
1096 p->u.qpsk.fec_inner = c->fec_inner;
1102 if(c->delivery_system == SYS_ISDBT) {
1103 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1104 p->frequency = c->frequency;
1105 p->inversion = INVERSION_AUTO;
1106 p->u.ofdm.constellation = QAM_AUTO;
1107 p->u.ofdm.code_rate_HP = FEC_AUTO;
1108 p->u.ofdm.code_rate_LP = FEC_AUTO;
1109 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1110 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1111 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1112 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1116 void dtv_property_cache_submit(struct dvb_frontend *fe)
1118 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1120 printk("%s()\n", __FUNCTION__);
1122 /* For legacy delivery systems we don't need the delivery_system to
1123 * be specified, but we populate the older structures from the cache
1124 * so we can call set_frontend on older drivers.
1126 if(is_legacy_delivery_system(c->delivery_system)) {
1128 printk("%s() legacy, modulation = %d\n", __FUNCTION__, c->modulation);
1129 dtv_property_legacy_params_sync(fe);
1132 printk("%s() adv, modulation = %d\n", __FUNCTION__, c->modulation);
1134 /* For advanced delivery systems / modulation types ...
1135 * we seed the lecacy dvb_frontend_parameters structure
1136 * so that the sanity checking code later in the IOCTL processing
1137 * can validate our basic frequency ranges, symbolrates, modulation
1140 dtv_property_adv_params_sync(fe);
1144 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1145 unsigned int cmd, void *parg);
1146 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1147 unsigned int cmd, void *parg);
1149 int dtv_property_process(struct dvb_frontend *fe, struct dtv_property *tvp,
1150 struct inode *inode, struct file *file)
1153 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1154 printk("%s()\n", __FUNCTION__);
1155 dtv_property_dump(tvp);
1159 /* Reset a cache of data specific to the frontend here. This does
1160 * not effect hardware.
1162 printk("%s() Flushing property cache\n", __FUNCTION__);
1163 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1164 fe->dtv_property_cache.state = tvp->cmd;
1165 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1168 /* interpret the cache of data, build either a traditional frontend
1169 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1172 fe->dtv_property_cache.state = tvp->cmd;
1173 printk("%s() Finalised property cache\n", __FUNCTION__);
1174 dtv_property_cache_submit(fe);
1176 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1177 &fepriv->parameters);
1179 case DTV_SET_FREQUENCY:
1180 fe->dtv_property_cache.frequency = tvp->u.data;
1182 case DTV_GET_FREQUENCY:
1183 tvp->u.data = fe->dtv_property_cache.frequency;
1185 case DTV_SET_MODULATION:
1186 fe->dtv_property_cache.modulation = tvp->u.data;
1188 case DTV_GET_MODULATION:
1189 tvp->u.data = fe->dtv_property_cache.modulation;
1191 case DTV_SET_BANDWIDTH:
1192 fe->dtv_property_cache.bandwidth = tvp->u.data;
1194 case DTV_GET_BANDWIDTH:
1195 tvp->u.data = fe->dtv_property_cache.bandwidth;
1197 case DTV_SET_INVERSION:
1198 fe->dtv_property_cache.inversion = tvp->u.data;
1200 case DTV_GET_INVERSION:
1201 tvp->u.data = fe->dtv_property_cache.inversion;
1203 case DTV_SET_SYMBOL_RATE:
1204 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1206 case DTV_GET_SYMBOL_RATE:
1207 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1209 case DTV_SET_INNER_FEC:
1210 fe->dtv_property_cache.fec_inner = tvp->u.data;
1212 case DTV_GET_INNER_FEC:
1213 tvp->u.data = fe->dtv_property_cache.fec_inner;
1216 fe->dtv_property_cache.pilot = tvp->u.data;
1219 tvp->u.data = fe->dtv_property_cache.pilot;
1221 case DTV_SET_ROLLOFF:
1222 fe->dtv_property_cache.rolloff = tvp->u.data;
1224 case DTV_GET_ROLLOFF:
1225 tvp->u.data = fe->dtv_property_cache.rolloff;
1227 case DTV_SET_DELIVERY_SYSTEM:
1228 fe->dtv_property_cache.delivery_system = tvp->u.data;
1230 case DTV_GET_DELIVERY_SYSTEM:
1231 tvp->u.data = fe->dtv_property_cache.delivery_system;
1234 /* ISDB-T Support here */
1235 case DTV_SET_ISDB_SEGMENT_NUM:
1236 fe->dtv_property_cache.isdb_segment_num = tvp->u.data;
1238 case DTV_GET_ISDB_SEGMENT_NUM:
1239 tvp->u.data = fe->dtv_property_cache.isdb_segment_num;
1241 case DTV_SET_ISDB_SEGMENT_WIDTH:
1242 fe->dtv_property_cache.isdb_segment_width = tvp->u.data;
1244 case DTV_GET_ISDB_SEGMENT_WIDTH:
1245 tvp->u.data = fe->dtv_property_cache.isdb_segment_width;
1247 case DTV_GET_ISDB_LAYERA_FEC:
1248 tvp->u.data = fe->dtv_property_cache.isdb_layera_fec;
1250 case DTV_GET_ISDB_LAYERA_MODULATION:
1251 tvp->u.data = fe->dtv_property_cache.isdb_layera_modulation;
1253 case DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH:
1254 tvp->u.data = fe->dtv_property_cache.isdb_layera_segment_width;
1256 case DTV_GET_ISDB_LAYERB_FEC:
1257 tvp->u.data = fe->dtv_property_cache.isdb_layerb_fec;
1259 case DTV_GET_ISDB_LAYERB_MODULATION:
1260 tvp->u.data = fe->dtv_property_cache.isdb_layerb_modulation;
1262 case DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH:
1263 tvp->u.data = fe->dtv_property_cache.isdb_layerb_segment_width;
1265 case DTV_GET_ISDB_LAYERC_FEC:
1266 tvp->u.data = fe->dtv_property_cache.isdb_layerc_fec;
1268 case DTV_GET_ISDB_LAYERC_MODULATION:
1269 tvp->u.data = fe->dtv_property_cache.isdb_layerc_modulation;
1271 case DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH:
1272 tvp->u.data = fe->dtv_property_cache.isdb_layerc_segment_width;
1274 case DTV_SET_VOLTAGE:
1275 fe->dtv_property_cache.voltage = tvp->u.data;
1276 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1277 (void *)fe->dtv_property_cache.voltage);
1279 case DTV_GET_VOLTAGE:
1280 tvp->u.data = fe->dtv_property_cache.voltage;
1283 fe->dtv_property_cache.sectone = tvp->u.data;
1284 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1285 (void *)fe->dtv_property_cache.sectone);
1288 tvp->u.data = fe->dtv_property_cache.sectone;
1295 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1296 unsigned int cmd, void *parg)
1298 struct dvb_device *dvbdev = file->private_data;
1299 struct dvb_frontend *fe = dvbdev->priv;
1300 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1301 int err = -EOPNOTSUPP;
1303 dprintk ("%s\n", __func__);
1308 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1309 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1310 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1313 if (down_interruptible (&fepriv->sem))
1314 return -ERESTARTSYS;
1316 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1317 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1319 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1325 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1326 unsigned int cmd, void *parg)
1328 struct dvb_device *dvbdev = file->private_data;
1329 struct dvb_frontend *fe = dvbdev->priv;
1330 int err = -EOPNOTSUPP;
1332 struct dtv_properties *tvps = NULL;
1333 struct dtv_property *tvp = NULL;
1336 dprintk("%s\n", __func__);
1338 if(cmd == FE_SET_PROPERTY) {
1339 printk("%s() FE_SET_PROPERTY\n", __FUNCTION__);
1341 tvps = (struct dtv_properties __user *)parg;
1343 printk("%s() properties.num = %d\n", __FUNCTION__, tvps->num);
1344 printk("%s() properties.props = %p\n", __FUNCTION__, tvps->props);
1346 /* Put an arbitrary limit on the number of messages that can
1347 * be sent at once */
1348 if (tvps->num > DTV_IOCTL_MAX_MSGS)
1351 tvp = (struct dtv_property *) kmalloc(tvps->num *
1352 sizeof(struct dtv_property), GFP_KERNEL);
1358 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1363 for (i = 0; i < tvps->num; i++)
1364 dtv_property_process(fe, tvp + i, inode, file);
1366 if(fe->dtv_property_cache.state == DTV_TUNE) {
1367 printk("%s() Property cache is full, tuning\n", __FUNCTION__);
1376 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1377 unsigned int cmd, void *parg)
1379 struct dvb_device *dvbdev = file->private_data;
1380 struct dvb_frontend *fe = dvbdev->priv;
1381 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1382 int err = -EOPNOTSUPP;
1386 struct dvb_frontend_info* info = parg;
1387 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1388 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1390 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1391 * do it, it is done for it. */
1392 info->caps |= FE_CAN_INVERSION_AUTO;
1397 case FE_READ_STATUS: {
1398 fe_status_t* status = parg;
1400 /* if retune was requested but hasn't occured yet, prevent
1401 * that user get signal state from previous tuning */
1402 if(fepriv->state == FESTATE_RETUNE) {
1408 if (fe->ops.read_status)
1409 err = fe->ops.read_status(fe, status);
1413 if (fe->ops.read_ber)
1414 err = fe->ops.read_ber(fe, (__u32*) parg);
1417 case FE_READ_SIGNAL_STRENGTH:
1418 if (fe->ops.read_signal_strength)
1419 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1423 if (fe->ops.read_snr)
1424 err = fe->ops.read_snr(fe, (__u16*) parg);
1427 case FE_READ_UNCORRECTED_BLOCKS:
1428 if (fe->ops.read_ucblocks)
1429 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1433 case FE_DISEQC_RESET_OVERLOAD:
1434 if (fe->ops.diseqc_reset_overload) {
1435 err = fe->ops.diseqc_reset_overload(fe);
1436 fepriv->state = FESTATE_DISEQC;
1441 case FE_DISEQC_SEND_MASTER_CMD:
1442 if (fe->ops.diseqc_send_master_cmd) {
1443 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1444 fepriv->state = FESTATE_DISEQC;
1449 case FE_DISEQC_SEND_BURST:
1450 if (fe->ops.diseqc_send_burst) {
1451 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1452 fepriv->state = FESTATE_DISEQC;
1458 if (fe->ops.set_tone) {
1459 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1460 fepriv->tone = (fe_sec_tone_mode_t) parg;
1461 fepriv->state = FESTATE_DISEQC;
1466 case FE_SET_VOLTAGE:
1467 if (fe->ops.set_voltage) {
1468 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1469 fepriv->voltage = (fe_sec_voltage_t) parg;
1470 fepriv->state = FESTATE_DISEQC;
1475 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1476 if (fe->ops.dishnetwork_send_legacy_command) {
1477 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1478 fepriv->state = FESTATE_DISEQC;
1480 } else if (fe->ops.set_voltage) {
1482 * NOTE: This is a fallback condition. Some frontends
1483 * (stv0299 for instance) take longer than 8msec to
1484 * respond to a set_voltage command. Those switches
1485 * need custom routines to switch properly. For all
1486 * other frontends, the following shoule work ok.
1487 * Dish network legacy switches (as used by Dish500)
1488 * are controlled by sending 9-bit command words
1489 * spaced 8msec apart.
1490 * the actual command word is switch/port dependant
1491 * so it is up to the userspace application to send
1492 * the right command.
1493 * The command must always start with a '0' after
1494 * initialization, so parg is 8 bits and does not
1495 * include the initialization or start bit
1497 unsigned long swcmd = ((unsigned long) parg) << 1;
1498 struct timeval nexttime;
1499 struct timeval tv[10];
1502 if (dvb_frontend_debug)
1503 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1504 do_gettimeofday(&nexttime);
1505 if (dvb_frontend_debug)
1506 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1507 /* before sending a command, initialize by sending
1508 * a 32ms 18V to the switch
1510 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1511 dvb_frontend_sleep_until(&nexttime, 32000);
1513 for (i = 0; i < 9; i++) {
1514 if (dvb_frontend_debug)
1515 do_gettimeofday(&tv[i + 1]);
1516 if ((swcmd & 0x01) != last) {
1517 /* set voltage to (last ? 13V : 18V) */
1518 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1519 last = (last) ? 0 : 1;
1523 dvb_frontend_sleep_until(&nexttime, 8000);
1525 if (dvb_frontend_debug) {
1526 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1527 __func__, fe->dvb->num);
1528 for (i = 1; i < 10; i++)
1529 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1532 fepriv->state = FESTATE_DISEQC;
1537 case FE_DISEQC_RECV_SLAVE_REPLY:
1538 if (fe->ops.diseqc_recv_slave_reply)
1539 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1542 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1543 if (fe->ops.enable_high_lnb_voltage)
1544 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1547 case FE_SET_FRONTEND: {
1548 struct dvb_frontend_tune_settings fetunesettings;
1550 dtv_property_cache_sync(fe, &fepriv->parameters);
1552 if(fe->dtv_property_cache.state == DTV_TUNE) {
1553 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1558 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1563 memcpy (&fepriv->parameters, parg,
1564 sizeof (struct dvb_frontend_parameters));
1567 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1568 memcpy(&fetunesettings.parameters, parg,
1569 sizeof (struct dvb_frontend_parameters));
1571 /* force auto frequency inversion if requested */
1572 if (dvb_force_auto_inversion) {
1573 fepriv->parameters.inversion = INVERSION_AUTO;
1574 fetunesettings.parameters.inversion = INVERSION_AUTO;
1576 if (fe->ops.info.type == FE_OFDM) {
1577 /* without hierarchical coding code_rate_LP is irrelevant,
1578 * so we tolerate the otherwise invalid FEC_NONE setting */
1579 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1580 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1581 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1584 /* get frontend-specific tuning settings */
1585 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1586 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1587 fepriv->max_drift = fetunesettings.max_drift;
1588 fepriv->step_size = fetunesettings.step_size;
1590 /* default values */
1591 switch(fe->ops.info.type) {
1593 fepriv->min_delay = HZ/20;
1594 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1595 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1599 fepriv->min_delay = HZ/20;
1600 fepriv->step_size = 0; /* no zigzag */
1601 fepriv->max_drift = 0;
1605 fepriv->min_delay = HZ/20;
1606 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1607 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1610 fepriv->min_delay = HZ/20;
1611 fepriv->step_size = 0;
1612 fepriv->max_drift = 0;
1616 if (dvb_override_tune_delay > 0)
1617 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1619 fepriv->state = FESTATE_RETUNE;
1620 dvb_frontend_wakeup(fe);
1621 dvb_frontend_add_event(fe, 0);
1628 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1631 case FE_GET_FRONTEND:
1632 if (fe->ops.get_frontend) {
1633 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1634 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1638 case FE_SET_FRONTEND_TUNE_MODE:
1639 fepriv->tune_mode_flags = (unsigned long) parg;
1648 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1650 struct dvb_device *dvbdev = file->private_data;
1651 struct dvb_frontend *fe = dvbdev->priv;
1652 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1654 dprintk ("%s\n", __func__);
1656 poll_wait (file, &fepriv->events.wait_queue, wait);
1658 if (fepriv->events.eventw != fepriv->events.eventr)
1659 return (POLLIN | POLLRDNORM | POLLPRI);
1664 static int dvb_frontend_open(struct inode *inode, struct file *file)
1666 struct dvb_device *dvbdev = file->private_data;
1667 struct dvb_frontend *fe = dvbdev->priv;
1668 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1671 dprintk ("%s\n", __func__);
1673 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1674 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1678 if ((ret = dvb_generic_open (inode, file)) < 0)
1681 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1682 /* normal tune mode when opened R/W */
1683 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1685 fepriv->voltage = -1;
1687 ret = dvb_frontend_start (fe);
1691 /* empty event queue */
1692 fepriv->events.eventr = fepriv->events.eventw = 0;
1698 dvb_generic_release(inode, file);
1700 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1701 fe->ops.ts_bus_ctrl(fe, 0);
1705 static int dvb_frontend_release(struct inode *inode, struct file *file)
1707 struct dvb_device *dvbdev = file->private_data;
1708 struct dvb_frontend *fe = dvbdev->priv;
1709 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1712 dprintk ("%s\n", __func__);
1714 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1715 fepriv->release_jiffies = jiffies;
1717 ret = dvb_generic_release (inode, file);
1719 if (dvbdev->users == -1) {
1720 if (fepriv->exit == 1) {
1721 fops_put(file->f_op);
1723 wake_up(&dvbdev->wait_queue);
1725 if (fe->ops.ts_bus_ctrl)
1726 fe->ops.ts_bus_ctrl(fe, 0);
1732 static struct file_operations dvb_frontend_fops = {
1733 .owner = THIS_MODULE,
1734 .ioctl = dvb_generic_ioctl,
1735 .poll = dvb_frontend_poll,
1736 .open = dvb_frontend_open,
1737 .release = dvb_frontend_release
1740 int dvb_register_frontend(struct dvb_adapter* dvb,
1741 struct dvb_frontend* fe)
1743 struct dvb_frontend_private *fepriv;
1744 static const struct dvb_device dvbdev_template = {
1748 .fops = &dvb_frontend_fops,
1749 .kernel_ioctl = dvb_frontend_ioctl
1752 dprintk ("%s\n", __func__);
1754 if (mutex_lock_interruptible(&frontend_mutex))
1755 return -ERESTARTSYS;
1757 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1758 if (fe->frontend_priv == NULL) {
1759 mutex_unlock(&frontend_mutex);
1762 fepriv = fe->frontend_priv;
1764 init_MUTEX (&fepriv->sem);
1765 init_waitqueue_head (&fepriv->wait_queue);
1766 init_waitqueue_head (&fepriv->events.wait_queue);
1767 mutex_init(&fepriv->events.mtx);
1769 fepriv->inversion = INVERSION_OFF;
1771 printk ("DVB: registering frontend %i (%s)...\n",
1775 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1776 fe, DVB_DEVICE_FRONTEND);
1778 mutex_unlock(&frontend_mutex);
1781 EXPORT_SYMBOL(dvb_register_frontend);
1783 int dvb_unregister_frontend(struct dvb_frontend* fe)
1785 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1786 dprintk ("%s\n", __func__);
1788 mutex_lock(&frontend_mutex);
1789 dvb_frontend_stop (fe);
1790 mutex_unlock(&frontend_mutex);
1792 if (fepriv->dvbdev->users < -1)
1793 wait_event(fepriv->dvbdev->wait_queue,
1794 fepriv->dvbdev->users==-1);
1796 mutex_lock(&frontend_mutex);
1797 dvb_unregister_device (fepriv->dvbdev);
1799 /* fe is invalid now */
1801 mutex_unlock(&frontend_mutex);
1804 EXPORT_SYMBOL(dvb_unregister_frontend);
1806 #ifdef CONFIG_MEDIA_ATTACH
1807 void dvb_frontend_detach(struct dvb_frontend* fe)
1811 if (fe->ops.release_sec) {
1812 fe->ops.release_sec(fe);
1813 symbol_put_addr(fe->ops.release_sec);
1815 if (fe->ops.tuner_ops.release) {
1816 fe->ops.tuner_ops.release(fe);
1817 symbol_put_addr(fe->ops.tuner_ops.release);
1819 if (fe->ops.analog_ops.release) {
1820 fe->ops.analog_ops.release(fe);
1821 symbol_put_addr(fe->ops.analog_ops.release);
1823 ptr = (void*)fe->ops.release;
1825 fe->ops.release(fe);
1826 symbol_put_addr(ptr);
1830 void dvb_frontend_detach(struct dvb_frontend* fe)
1832 if (fe->ops.release_sec)
1833 fe->ops.release_sec(fe);
1834 if (fe->ops.tuner_ops.release)
1835 fe->ops.tuner_ops.release(fe);
1836 if (fe->ops.analog_ops.release)
1837 fe->ops.analog_ops.release(fe);
1838 if (fe->ops.release)
1839 fe->ops.release(fe);
1842 EXPORT_SYMBOL(dvb_frontend_detach);