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 tv_cmds_h tv_cmds[] = {
759 [TV_SEQ_UNDEFINED] = {
760 .name = "TV_SEQ_UNDEFINED",
761 .cmd = TV_SEQ_UNDEFINED,
765 .name = "TV_SEQ_START",
769 [TV_SEQ_CONTINUE] = {
770 .name = "TV_SEQ_CONTINUE",
771 .cmd = TV_SEQ_CONTINUE,
774 [TV_SEQ_COMPLETE] = {
775 .name = "TV_SEQ_COMPLETE",
776 .cmd = TV_SEQ_COMPLETE,
779 [TV_SEQ_TERMINATE] = {
780 .name = "TV_SEQ_TERMINATE",
781 .cmd = TV_SEQ_TERMINATE,
786 [TV_SET_FREQUENCY] = {
787 .name = "TV_SET_FREQUENCY",
788 .cmd = TV_SET_FREQUENCY,
791 [TV_SET_BANDWIDTH] = {
792 .name = "TV_SET_BANDWIDTH",
793 .cmd = TV_SET_BANDWIDTH,
796 [TV_SET_MODULATION] = {
797 .name = "TV_SET_MODULATION",
798 .cmd = TV_SET_MODULATION,
801 [TV_SET_INVERSION] = {
802 .name = "TV_SET_INVERSION",
803 .cmd = TV_SET_INVERSION,
806 [TV_SET_DISEQC_MASTER] = {
807 .name = "TV_SET_DISEQC_MASTER",
808 .cmd = TV_SET_DISEQC_MASTER,
812 [TV_SET_SYMBOLRATE] = {
813 .name = "TV_SET_SYMBOLRATE",
814 .cmd = TV_SET_SYMBOLRATE,
817 [TV_SET_INNERFEC] = {
818 .name = "TV_SET_INNERFEC",
819 .cmd = TV_SET_INNERFEC,
823 .name = "TV_SET_VOLTAGE",
824 .cmd = TV_SET_VOLTAGE,
828 .name = "TV_SET_TONE",
833 .name = "TV_SET_PILOT",
838 .name = "TV_SET_ROLLOFF",
839 .cmd = TV_SET_ROLLOFF,
842 [TV_SET_DELIVERY_SYSTEM] = {
843 .name = "TV_SET_DELIVERY_SYSTEM",
844 .cmd = TV_SET_DELIVERY_SYSTEM,
847 [TV_SET_ISDB_SEGMENT_NUM] = {
848 .name = "TV_SET_ISDB_SEGMENT_NUM",
849 .cmd = TV_SET_ISDB_SEGMENT_NUM,
852 [TV_SET_ISDB_SEGMENT_WIDTH] = {
853 .name = "TV_SET_ISDB_SEGMENT_WIDTH",
854 .cmd = TV_SET_ISDB_SEGMENT_WIDTH,
859 [TV_GET_FREQUENCY] = {
860 .name = "TV_GET_FREQUENCY",
861 .cmd = TV_GET_FREQUENCY,
864 [TV_GET_BANDWIDTH] = {
865 .name = "TV_GET_BANDWIDTH",
866 .cmd = TV_GET_BANDWIDTH,
869 [TV_GET_MODULATION] = {
870 .name = "TV_GET_MODULATION",
871 .cmd = TV_GET_MODULATION,
874 [TV_GET_INVERSION] = {
875 .name = "TV_GET_INVERSION",
876 .cmd = TV_GET_INVERSION,
879 [TV_GET_DISEQC_SLAVE_REPLY] = {
880 .name = "TV_GET_DISEQC_SLAVE_REPLY",
881 .cmd = TV_GET_DISEQC_SLAVE_REPLY,
885 [TV_GET_SYMBOLRATE] = {
886 .name = "TV_GET_SYMBOLRATE",
887 .cmd = TV_GET_SYMBOLRATE,
890 [TV_GET_INNERFEC] = {
891 .name = "TV_GET_INNERFEC",
892 .cmd = TV_GET_INNERFEC,
896 .name = "TV_GET_VOLTAGE",
897 .cmd = TV_GET_VOLTAGE,
901 .name = "TV_GET_TONE",
906 .name = "TV_GET_PILOT",
911 .name = "TV_GET_ROLLOFF",
912 .cmd = TV_GET_ROLLOFF,
915 [TV_GET_DELIVERY_SYSTEM] = {
916 .name = "TV_GET_DELIVERY_SYSTEM",
917 .cmd = TV_GET_DELIVERY_SYSTEM,
920 [TV_GET_ISDB_SEGMENT_NUM] = {
921 .name = "TV_GET_ISDB_SEGMENT_NUM",
922 .cmd = TV_GET_ISDB_SEGMENT_NUM,
925 [TV_GET_ISDB_SEGMENT_WIDTH] = {
926 .name = "TV_GET_ISDB_SEGMENT_WIDTH",
927 .cmd = TV_GET_ISDB_SEGMENT_WIDTH,
930 [TV_GET_ISDB_LAYERA_FEC] = {
931 .name = "TV_GET_ISDB_LAYERA_FEC",
932 .cmd = TV_GET_ISDB_LAYERA_FEC,
935 [TV_GET_ISDB_LAYERA_MODULATION] = {
936 .name = "TV_GET_ISDB_LAYERA_MODULATION",
937 .cmd = TV_GET_ISDB_LAYERA_MODULATION,
940 [TV_GET_ISDB_LAYERA_SEGMENT_WIDTH] = {
941 .name = "TV_GET_ISDB_LAYERA_SEGMENT_WIDTH",
942 .cmd = TV_GET_ISDB_LAYERA_SEGMENT_WIDTH,
945 [TV_GET_ISDB_LAYERB_FEC] = {
946 .name = "TV_GET_ISDB_LAYERB_FEC",
947 .cmd = TV_GET_ISDB_LAYERB_FEC,
950 [TV_GET_ISDB_LAYERB_MODULATION] = {
951 .name = "TV_GET_ISDB_LAYERB_MODULATION",
952 .cmd = TV_GET_ISDB_LAYERB_MODULATION,
955 [TV_GET_ISDB_LAYERB_SEGMENT_WIDTH] = {
956 .name = "TV_GET_ISDB_LAYERB_SEGMENT_WIDTH",
957 .cmd = TV_GET_ISDB_LAYERB_SEGMENT_WIDTH,
960 [TV_GET_ISDB_LAYERC_FEC] = {
961 .name = "TV_GET_ISDB_LAYERC_FEC",
962 .cmd = TV_GET_ISDB_LAYERC_FEC,
965 [TV_GET_ISDB_LAYERC_MODULATION] = {
966 .name = "TV_GET_ISDB_LAYERC_MODULATION",
967 .cmd = TV_GET_ISDB_LAYERC_MODULATION,
970 [TV_GET_ISDB_LAYERC_SEGMENT_WIDTH] = {
971 .name = "TV_GET_ISDB_LAYERC_SEGMENT_WIDTH",
972 .cmd = TV_GET_ISDB_LAYERC_SEGMENT_WIDTH,
977 void tv_property_dump(tv_property_t *tvp)
981 printk("%s() tvp.cmd = 0x%08x (%s)\n"
984 ,tv_cmds[ tvp->cmd ].name);
986 if(tv_cmds[ tvp->cmd ].buffer) {
988 printk("%s() tvp.u.buffer.len = 0x%02x\n"
992 for(i = 0; i < tvp->u.buffer.len; i++)
993 printk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
996 ,tvp->u.buffer.data[i]);
999 printk("%s() tvp.u.data = 0x%08x\n", __FUNCTION__, tvp->u.data);
1002 int is_legacy_delivery_system(fe_delivery_system_t s)
1004 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
1005 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
1011 int tv_property_cache_submit(struct dvb_frontend *fe)
1014 /* We have to do one of two things:
1015 * To support legacy devices using the new API we take values from
1016 * the tv_cache and generate a legacy truning structure.
1020 * To support advanced tuning devices with the new API we
1021 * notify the new advance driver type that a tuning operation is required
1022 * and let it pull values from the cache as is, we don't need to
1025 * We'll use the modulation type to assess how this is handled. as the API
1026 * progresses we'll probably want to have a flag in dvb_frontend_ops
1027 * to allow the frontend driver to dictate how it likes to be tuned.
1029 * Because of how this is attached to the ioctl handler for legacy support,
1030 * it's important to return an appropriate result code with atleast the following
1032 * < 0 = processing error
1033 * 0 = lecagy ioctl handler to submit a traditional set_frontend() call.
1034 * 1 = lecagy ioctl handler should NOT submit a traditional set_frontend() call.
1039 struct tv_frontend_properties *c = &fe->tv_property_cache;
1040 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1041 struct dvb_frontend_parameters p;
1043 printk("%s()\n", __FUNCTION__);
1045 /* For legacy delivery systems we don't need the delivery_system to be specified */
1046 if(is_legacy_delivery_system(c->delivery_system)) {
1047 switch(c->modulation) {
1049 printk("%s() Preparing QPSK req\n", __FUNCTION__);
1050 p.frequency = c->frequency;
1051 p.inversion = c->inversion;
1052 p.u.qpsk.symbol_rate = c->symbol_rate;
1053 p.u.qpsk.fec_inner = c->fec_inner;
1054 memcpy(&fepriv->parameters, &p,
1055 sizeof (struct dvb_frontend_parameters));
1057 /* Call the traditional tuning mechanisms. */
1067 printk("%s() Preparing QAM req\n", __FUNCTION__);
1068 p.frequency = c->frequency;
1069 p.inversion = c->inversion;
1070 p.u.qam.symbol_rate = c->symbol_rate;
1071 p.u.vsb.modulation = c->modulation;
1072 printk("%s() frequency = %d\n", __FUNCTION__, p.frequency);
1073 printk("%s() QAM = %d\n", __FUNCTION__, p.u.vsb.modulation);
1074 memcpy(&fepriv->parameters, &p,
1075 sizeof (struct dvb_frontend_parameters));
1077 /* At this point we're fully formed for backwards
1078 * compatability and we need to return this
1079 * via the ioctl handler as SET_FRONTEND (arg).
1080 * We've already patched the new values into the
1081 * frontends tuning structures so the ioctl code just
1082 * continues as if a legacy tune structure was passed
1090 printk("%s() Preparing VSB req\n", __FUNCTION__);
1091 p.frequency = c->frequency;
1092 p.u.vsb.modulation = c->modulation;
1093 memcpy(&fepriv->parameters, &p,
1094 sizeof (struct dvb_frontend_parameters));
1096 /* Call the traditional tuning mechanisms. */
1100 /* TODO: Add any missing modulation types */
1105 /* For advanced delivery systems / modulation types ...
1106 * we seed the lecacy dvb_frontend_parameters structure
1107 * so that the sanity checking code later in the IOCTL processing
1108 * can validate our basic frequency ranges, symbolrates, modulation
1113 switch(c->modulation) {
1117 /* Just post a notification to the demod driver and let it pull
1118 * the specific values it wants from its tv_property_cache.
1119 * It can decide how best to use those parameters.
1120 * IOCTL will call set_frontend (by default) due to zigzag
1123 if (fe->ops.set_params)
1124 r = fe->ops.set_params(fe);
1126 p.frequency = c->frequency;
1127 p.inversion = c->inversion;
1128 p.u.qpsk.symbol_rate = c->symbol_rate;
1129 p.u.qpsk.fec_inner = c->fec_inner;
1130 memcpy(&fepriv->parameters, &p,
1131 sizeof (struct dvb_frontend_parameters));
1139 if(c->delivery_system == SYS_ISDBT) {
1140 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1141 p.frequency = c->frequency;
1142 p.inversion = INVERSION_AUTO;
1143 p.u.ofdm.constellation = QAM_AUTO;
1144 p.u.ofdm.code_rate_HP = FEC_AUTO;
1145 p.u.ofdm.code_rate_LP = FEC_AUTO;
1146 p.u.ofdm.bandwidth = BANDWIDTH_AUTO;
1147 p.u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1148 p.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1149 p.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1150 memcpy(&fepriv->parameters, &p,
1151 sizeof (struct dvb_frontend_parameters));
1159 int tv_property_process(struct dvb_frontend *fe, tv_property_t *tvp)
1162 printk("%s()\n", __FUNCTION__);
1163 tv_property_dump(tvp);
1167 case TV_SEQ_TERMINATE:
1168 /* Reset a cache of data specific to the frontend here. This does
1169 * not effect hardware.
1171 printk("%s() Flushing property cache\n", __FUNCTION__);
1172 memset(&fe->tv_property_cache, 0, sizeof(struct tv_frontend_properties));
1173 fe->tv_property_cache.state = TV_SEQ_START;
1174 fe->tv_property_cache.delivery_system = SYS_UNDEFINED;
1176 case TV_SEQ_COMPLETE:
1177 /* interpret the cache of data, build either a traditional frontend
1178 * tunerequest and submit it to a subset of the ioctl handler,
1179 * or, call a new undefined method on the frontend to deal with
1180 * all new tune requests.
1182 fe->tv_property_cache.state = TV_SEQ_COMPLETE;
1183 printk("%s() Finalised property cache\n", __FUNCTION__);
1184 r = tv_property_cache_submit(fe);
1186 case TV_SET_FREQUENCY:
1187 fe->tv_property_cache.frequency = tvp->u.data;
1189 case TV_GET_FREQUENCY:
1190 tvp->u.data = fe->tv_property_cache.frequency;
1192 case TV_SET_MODULATION:
1193 fe->tv_property_cache.modulation = tvp->u.data;
1195 case TV_GET_MODULATION:
1196 tvp->u.data = fe->tv_property_cache.modulation;
1198 case TV_SET_BANDWIDTH:
1199 fe->tv_property_cache.bandwidth = tvp->u.data;
1201 case TV_GET_BANDWIDTH:
1202 tvp->u.data = fe->tv_property_cache.bandwidth;
1204 case TV_SET_INVERSION:
1205 fe->tv_property_cache.inversion = tvp->u.data;
1207 case TV_GET_INVERSION:
1208 tvp->u.data = fe->tv_property_cache.inversion;
1210 case TV_SET_SYMBOLRATE:
1211 fe->tv_property_cache.symbol_rate = tvp->u.data;
1213 case TV_GET_SYMBOLRATE:
1214 tvp->u.data = fe->tv_property_cache.symbol_rate;
1216 case TV_SET_INNERFEC:
1217 fe->tv_property_cache.fec_inner = tvp->u.data;
1219 case TV_GET_INNERFEC:
1220 tvp->u.data = fe->tv_property_cache.fec_inner;
1223 fe->tv_property_cache.pilot = tvp->u.data;
1226 tvp->u.data = fe->tv_property_cache.pilot;
1228 case TV_SET_ROLLOFF:
1229 fe->tv_property_cache.rolloff = tvp->u.data;
1231 case TV_GET_ROLLOFF:
1232 tvp->u.data = fe->tv_property_cache.rolloff;
1234 case TV_SET_DELIVERY_SYSTEM:
1235 fe->tv_property_cache.delivery_system = tvp->u.data;
1237 case TV_GET_DELIVERY_SYSTEM:
1238 tvp->u.data = fe->tv_property_cache.delivery_system;
1241 /* ISDB-T Support here */
1242 case TV_SET_ISDB_SEGMENT_NUM:
1243 fe->tv_property_cache.isdb_segment_num = tvp->u.data;
1245 case TV_GET_ISDB_SEGMENT_NUM:
1246 tvp->u.data = fe->tv_property_cache.isdb_segment_num;
1248 case TV_SET_ISDB_SEGMENT_WIDTH:
1249 fe->tv_property_cache.isdb_segment_width = tvp->u.data;
1251 case TV_GET_ISDB_SEGMENT_WIDTH:
1252 tvp->u.data = fe->tv_property_cache.isdb_segment_width;
1254 case TV_GET_ISDB_LAYERA_FEC:
1255 tvp->u.data = fe->tv_property_cache.isdb_layera_fec;
1257 case TV_GET_ISDB_LAYERA_MODULATION:
1258 tvp->u.data = fe->tv_property_cache.isdb_layera_modulation;
1260 case TV_GET_ISDB_LAYERA_SEGMENT_WIDTH:
1261 tvp->u.data = fe->tv_property_cache.isdb_layera_segment_width;
1263 case TV_GET_ISDB_LAYERB_FEC:
1264 tvp->u.data = fe->tv_property_cache.isdb_layerb_fec;
1266 case TV_GET_ISDB_LAYERB_MODULATION:
1267 tvp->u.data = fe->tv_property_cache.isdb_layerb_modulation;
1269 case TV_GET_ISDB_LAYERB_SEGMENT_WIDTH:
1270 tvp->u.data = fe->tv_property_cache.isdb_layerb_segment_width;
1272 case TV_GET_ISDB_LAYERC_FEC:
1273 tvp->u.data = fe->tv_property_cache.isdb_layerc_fec;
1275 case TV_GET_ISDB_LAYERC_MODULATION:
1276 tvp->u.data = fe->tv_property_cache.isdb_layerc_modulation;
1278 case TV_GET_ISDB_LAYERC_SEGMENT_WIDTH:
1279 tvp->u.data = fe->tv_property_cache.isdb_layerc_segment_width;
1287 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1288 unsigned int cmd, void *parg)
1290 struct dvb_device *dvbdev = file->private_data;
1291 struct dvb_frontend *fe = dvbdev->priv;
1292 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1293 int err = -EOPNOTSUPP;
1296 dprintk ("%s\n", __func__);
1301 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1302 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1303 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1306 if (down_interruptible (&fepriv->sem))
1307 return -ERESTARTSYS;
1309 if(cmd == FE_SET_PROPERTY) {
1310 printk("%s() FE_SET_PROPERTY\n", __FUNCTION__);
1312 /* TODO: basic property validation here */
1314 /* TODO: ioctl userdata out of range check here */
1316 while(tvp->cmd != TV_SEQ_UNDEFINED) {
1317 tv_property_process(fe, tvp);
1318 if( (tvp->cmd == TV_SEQ_TERMINATE) || (tvp->cmd == TV_SEQ_COMPLETE) )
1323 if(fe->tv_property_cache.state == TV_SEQ_COMPLETE) {
1324 printk("%s() Property cache is full, tuning\n", __FUNCTION__);
1325 cmd = FE_SET_FRONTEND;
1332 struct dvb_frontend_info* info = parg;
1333 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1334 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1336 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1337 * do it, it is done for it. */
1338 info->caps |= FE_CAN_INVERSION_AUTO;
1343 case FE_READ_STATUS: {
1344 fe_status_t* status = parg;
1346 /* if retune was requested but hasn't occured yet, prevent
1347 * that user get signal state from previous tuning */
1348 if(fepriv->state == FESTATE_RETUNE) {
1354 if (fe->ops.read_status)
1355 err = fe->ops.read_status(fe, status);
1359 if (fe->ops.read_ber)
1360 err = fe->ops.read_ber(fe, (__u32*) parg);
1363 case FE_READ_SIGNAL_STRENGTH:
1364 if (fe->ops.read_signal_strength)
1365 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1369 if (fe->ops.read_snr)
1370 err = fe->ops.read_snr(fe, (__u16*) parg);
1373 case FE_READ_UNCORRECTED_BLOCKS:
1374 if (fe->ops.read_ucblocks)
1375 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1379 case FE_DISEQC_RESET_OVERLOAD:
1380 if (fe->ops.diseqc_reset_overload) {
1381 err = fe->ops.diseqc_reset_overload(fe);
1382 fepriv->state = FESTATE_DISEQC;
1387 case FE_DISEQC_SEND_MASTER_CMD:
1388 if (fe->ops.diseqc_send_master_cmd) {
1389 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1390 fepriv->state = FESTATE_DISEQC;
1395 case FE_DISEQC_SEND_BURST:
1396 if (fe->ops.diseqc_send_burst) {
1397 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1398 fepriv->state = FESTATE_DISEQC;
1404 if (fe->ops.set_tone) {
1405 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1406 fepriv->tone = (fe_sec_tone_mode_t) parg;
1407 fepriv->state = FESTATE_DISEQC;
1412 case FE_SET_VOLTAGE:
1413 if (fe->ops.set_voltage) {
1414 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1415 fepriv->voltage = (fe_sec_voltage_t) parg;
1416 fepriv->state = FESTATE_DISEQC;
1421 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1422 if (fe->ops.dishnetwork_send_legacy_command) {
1423 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1424 fepriv->state = FESTATE_DISEQC;
1426 } else if (fe->ops.set_voltage) {
1428 * NOTE: This is a fallback condition. Some frontends
1429 * (stv0299 for instance) take longer than 8msec to
1430 * respond to a set_voltage command. Those switches
1431 * need custom routines to switch properly. For all
1432 * other frontends, the following shoule work ok.
1433 * Dish network legacy switches (as used by Dish500)
1434 * are controlled by sending 9-bit command words
1435 * spaced 8msec apart.
1436 * the actual command word is switch/port dependant
1437 * so it is up to the userspace application to send
1438 * the right command.
1439 * The command must always start with a '0' after
1440 * initialization, so parg is 8 bits and does not
1441 * include the initialization or start bit
1443 unsigned long swcmd = ((unsigned long) parg) << 1;
1444 struct timeval nexttime;
1445 struct timeval tv[10];
1448 if (dvb_frontend_debug)
1449 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1450 do_gettimeofday(&nexttime);
1451 if (dvb_frontend_debug)
1452 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1453 /* before sending a command, initialize by sending
1454 * a 32ms 18V to the switch
1456 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1457 dvb_frontend_sleep_until(&nexttime, 32000);
1459 for (i = 0; i < 9; i++) {
1460 if (dvb_frontend_debug)
1461 do_gettimeofday(&tv[i + 1]);
1462 if ((swcmd & 0x01) != last) {
1463 /* set voltage to (last ? 13V : 18V) */
1464 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1465 last = (last) ? 0 : 1;
1469 dvb_frontend_sleep_until(&nexttime, 8000);
1471 if (dvb_frontend_debug) {
1472 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1473 __func__, fe->dvb->num);
1474 for (i = 1; i < 10; i++)
1475 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1478 fepriv->state = FESTATE_DISEQC;
1483 case FE_DISEQC_RECV_SLAVE_REPLY:
1484 if (fe->ops.diseqc_recv_slave_reply)
1485 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1488 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1489 if (fe->ops.enable_high_lnb_voltage)
1490 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1493 case FE_SET_FRONTEND: {
1494 struct dvb_frontend_tune_settings fetunesettings;
1496 if(fe->tv_property_cache.state == TV_SEQ_COMPLETE) {
1497 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1502 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1507 memcpy (&fepriv->parameters, parg,
1508 sizeof (struct dvb_frontend_parameters));
1511 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1512 memcpy(&fetunesettings.parameters, parg,
1513 sizeof (struct dvb_frontend_parameters));
1515 /* force auto frequency inversion if requested */
1516 if (dvb_force_auto_inversion) {
1517 fepriv->parameters.inversion = INVERSION_AUTO;
1518 fetunesettings.parameters.inversion = INVERSION_AUTO;
1520 if (fe->ops.info.type == FE_OFDM) {
1521 /* without hierarchical coding code_rate_LP is irrelevant,
1522 * so we tolerate the otherwise invalid FEC_NONE setting */
1523 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1524 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1525 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1528 /* get frontend-specific tuning settings */
1529 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1530 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1531 fepriv->max_drift = fetunesettings.max_drift;
1532 fepriv->step_size = fetunesettings.step_size;
1534 /* default values */
1535 switch(fe->ops.info.type) {
1537 fepriv->min_delay = HZ/20;
1538 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1539 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1543 fepriv->min_delay = HZ/20;
1544 fepriv->step_size = 0; /* no zigzag */
1545 fepriv->max_drift = 0;
1549 fepriv->min_delay = HZ/20;
1550 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1551 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1554 fepriv->min_delay = HZ/20;
1555 fepriv->step_size = 0;
1556 fepriv->max_drift = 0;
1560 if (dvb_override_tune_delay > 0)
1561 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1563 fepriv->state = FESTATE_RETUNE;
1564 dvb_frontend_wakeup(fe);
1565 dvb_frontend_add_event(fe, 0);
1572 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1575 case FE_GET_FRONTEND:
1576 if (fe->ops.get_frontend) {
1577 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1578 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1582 case FE_SET_FRONTEND_TUNE_MODE:
1583 fepriv->tune_mode_flags = (unsigned long) parg;
1593 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1595 struct dvb_device *dvbdev = file->private_data;
1596 struct dvb_frontend *fe = dvbdev->priv;
1597 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1599 dprintk ("%s\n", __func__);
1601 poll_wait (file, &fepriv->events.wait_queue, wait);
1603 if (fepriv->events.eventw != fepriv->events.eventr)
1604 return (POLLIN | POLLRDNORM | POLLPRI);
1609 static int dvb_frontend_open(struct inode *inode, struct file *file)
1611 struct dvb_device *dvbdev = file->private_data;
1612 struct dvb_frontend *fe = dvbdev->priv;
1613 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1616 dprintk ("%s\n", __func__);
1618 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1619 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1623 if ((ret = dvb_generic_open (inode, file)) < 0)
1626 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1627 /* normal tune mode when opened R/W */
1628 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1630 fepriv->voltage = -1;
1632 ret = dvb_frontend_start (fe);
1636 /* empty event queue */
1637 fepriv->events.eventr = fepriv->events.eventw = 0;
1643 dvb_generic_release(inode, file);
1645 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1646 fe->ops.ts_bus_ctrl(fe, 0);
1650 static int dvb_frontend_release(struct inode *inode, struct file *file)
1652 struct dvb_device *dvbdev = file->private_data;
1653 struct dvb_frontend *fe = dvbdev->priv;
1654 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1657 dprintk ("%s\n", __func__);
1659 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1660 fepriv->release_jiffies = jiffies;
1662 ret = dvb_generic_release (inode, file);
1664 if (dvbdev->users == -1) {
1665 if (fepriv->exit == 1) {
1666 fops_put(file->f_op);
1668 wake_up(&dvbdev->wait_queue);
1670 if (fe->ops.ts_bus_ctrl)
1671 fe->ops.ts_bus_ctrl(fe, 0);
1677 static struct file_operations dvb_frontend_fops = {
1678 .owner = THIS_MODULE,
1679 .ioctl = dvb_generic_ioctl,
1680 .poll = dvb_frontend_poll,
1681 .open = dvb_frontend_open,
1682 .release = dvb_frontend_release
1685 int dvb_register_frontend(struct dvb_adapter* dvb,
1686 struct dvb_frontend* fe)
1688 struct dvb_frontend_private *fepriv;
1689 static const struct dvb_device dvbdev_template = {
1693 .fops = &dvb_frontend_fops,
1694 .kernel_ioctl = dvb_frontend_ioctl
1697 dprintk ("%s\n", __func__);
1699 if (mutex_lock_interruptible(&frontend_mutex))
1700 return -ERESTARTSYS;
1702 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1703 if (fe->frontend_priv == NULL) {
1704 mutex_unlock(&frontend_mutex);
1707 fepriv = fe->frontend_priv;
1709 init_MUTEX (&fepriv->sem);
1710 init_waitqueue_head (&fepriv->wait_queue);
1711 init_waitqueue_head (&fepriv->events.wait_queue);
1712 mutex_init(&fepriv->events.mtx);
1714 fepriv->inversion = INVERSION_OFF;
1716 printk ("DVB: registering frontend %i (%s)...\n",
1720 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1721 fe, DVB_DEVICE_FRONTEND);
1723 mutex_unlock(&frontend_mutex);
1726 EXPORT_SYMBOL(dvb_register_frontend);
1728 int dvb_unregister_frontend(struct dvb_frontend* fe)
1730 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1731 dprintk ("%s\n", __func__);
1733 mutex_lock(&frontend_mutex);
1734 dvb_frontend_stop (fe);
1735 mutex_unlock(&frontend_mutex);
1737 if (fepriv->dvbdev->users < -1)
1738 wait_event(fepriv->dvbdev->wait_queue,
1739 fepriv->dvbdev->users==-1);
1741 mutex_lock(&frontend_mutex);
1742 dvb_unregister_device (fepriv->dvbdev);
1744 /* fe is invalid now */
1746 mutex_unlock(&frontend_mutex);
1749 EXPORT_SYMBOL(dvb_unregister_frontend);
1751 #ifdef CONFIG_MEDIA_ATTACH
1752 void dvb_frontend_detach(struct dvb_frontend* fe)
1756 if (fe->ops.release_sec) {
1757 fe->ops.release_sec(fe);
1758 symbol_put_addr(fe->ops.release_sec);
1760 if (fe->ops.tuner_ops.release) {
1761 fe->ops.tuner_ops.release(fe);
1762 symbol_put_addr(fe->ops.tuner_ops.release);
1764 if (fe->ops.analog_ops.release) {
1765 fe->ops.analog_ops.release(fe);
1766 symbol_put_addr(fe->ops.analog_ops.release);
1768 ptr = (void*)fe->ops.release;
1770 fe->ops.release(fe);
1771 symbol_put_addr(ptr);
1775 void dvb_frontend_detach(struct dvb_frontend* fe)
1777 if (fe->ops.release_sec)
1778 fe->ops.release_sec(fe);
1779 if (fe->ops.tuner_ops.release)
1780 fe->ops.tuner_ops.release(fe);
1781 if (fe->ops.analog_ops.release)
1782 fe->ops.analog_ops.release(fe);
1783 if (fe->ops.release)
1784 fe->ops.release(fe);
1787 EXPORT_SYMBOL(dvb_frontend_detach);