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_ERROR 256
76 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
77 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
78 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
79 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
83 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
84 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
85 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
86 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
87 * FESTATE_TUNED. The frontend has successfully locked on.
88 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
89 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
90 * FESTATE_DISEQC. A DISEQC command has just been issued.
91 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
92 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
93 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
94 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
97 static DEFINE_MUTEX(frontend_mutex);
99 struct dvb_frontend_private {
101 /* thread/frontend values */
102 struct dvb_device *dvbdev;
103 struct dvb_frontend_parameters parameters;
104 struct dvb_fe_events events;
105 struct semaphore sem;
106 struct list_head list_head;
107 wait_queue_head_t wait_queue;
108 struct task_struct *thread;
109 unsigned long release_jiffies;
113 unsigned long tune_mode_flags;
115 unsigned int reinitialise;
119 /* swzigzag values */
121 unsigned int bending;
123 unsigned int inversion;
124 unsigned int auto_step;
125 unsigned int auto_sub_step;
126 unsigned int started_auto_step;
127 unsigned int min_delay;
128 unsigned int max_drift;
129 unsigned int step_size;
131 unsigned int check_wrapped;
132 enum dvbfe_search algo_status;
135 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
137 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
139 struct dvb_frontend_private *fepriv = fe->frontend_priv;
140 struct dvb_fe_events *events = &fepriv->events;
141 struct dvb_frontend_event *e;
144 dprintk ("%s\n", __func__);
146 if (mutex_lock_interruptible (&events->mtx))
149 wp = (events->eventw + 1) % MAX_EVENT;
151 if (wp == events->eventr) {
152 events->overflow = 1;
153 events->eventr = (events->eventr + 1) % MAX_EVENT;
156 e = &events->events[events->eventw];
158 memcpy (&e->parameters, &fepriv->parameters,
159 sizeof (struct dvb_frontend_parameters));
161 if (status & FE_HAS_LOCK)
162 if (fe->ops.get_frontend)
163 fe->ops.get_frontend(fe, &e->parameters);
167 mutex_unlock(&events->mtx);
171 wake_up_interruptible (&events->wait_queue);
174 static int dvb_frontend_get_event(struct dvb_frontend *fe,
175 struct dvb_frontend_event *event, int flags)
177 struct dvb_frontend_private *fepriv = fe->frontend_priv;
178 struct dvb_fe_events *events = &fepriv->events;
180 dprintk ("%s\n", __func__);
182 if (events->overflow) {
183 events->overflow = 0;
187 if (events->eventw == events->eventr) {
190 if (flags & O_NONBLOCK)
195 ret = wait_event_interruptible (events->wait_queue,
196 events->eventw != events->eventr);
198 if (down_interruptible (&fepriv->sem))
205 if (mutex_lock_interruptible (&events->mtx))
208 memcpy (event, &events->events[events->eventr],
209 sizeof(struct dvb_frontend_event));
211 events->eventr = (events->eventr + 1) % MAX_EVENT;
213 mutex_unlock(&events->mtx);
218 static void dvb_frontend_init(struct dvb_frontend *fe)
220 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
227 if (fe->ops.tuner_ops.init) {
228 if (fe->ops.i2c_gate_ctrl)
229 fe->ops.i2c_gate_ctrl(fe, 1);
230 fe->ops.tuner_ops.init(fe);
231 if (fe->ops.i2c_gate_ctrl)
232 fe->ops.i2c_gate_ctrl(fe, 0);
236 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
238 struct dvb_frontend_private *fepriv = fe->frontend_priv;
240 fepriv->reinitialise = 1;
241 dvb_frontend_wakeup(fe);
243 EXPORT_SYMBOL(dvb_frontend_reinitialise);
245 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
249 dprintk ("%s\n", __func__);
252 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
254 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
256 q2 = fepriv->quality - 128;
259 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
263 * Performs automatic twiddling of frontend parameters.
265 * @param fe The frontend concerned.
266 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
267 * @returns Number of complete iterations that have been performed.
269 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
274 struct dvb_frontend_private *fepriv = fe->frontend_priv;
275 int original_inversion = fepriv->parameters.inversion;
276 u32 original_frequency = fepriv->parameters.frequency;
278 /* are we using autoinversion? */
279 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
280 (fepriv->parameters.inversion == INVERSION_AUTO));
282 /* setup parameters correctly */
284 /* calculate the lnb_drift */
285 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
287 /* wrap the auto_step if we've exceeded the maximum drift */
288 if (fepriv->lnb_drift > fepriv->max_drift) {
289 fepriv->auto_step = 0;
290 fepriv->auto_sub_step = 0;
291 fepriv->lnb_drift = 0;
294 /* perform inversion and +/- zigzag */
295 switch(fepriv->auto_sub_step) {
297 /* try with the current inversion and current drift setting */
302 if (!autoinversion) break;
304 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
309 if (fepriv->lnb_drift == 0) break;
311 fepriv->lnb_drift = -fepriv->lnb_drift;
316 if (fepriv->lnb_drift == 0) break;
317 if (!autoinversion) break;
319 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
320 fepriv->lnb_drift = -fepriv->lnb_drift;
326 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
330 if (!ready) fepriv->auto_sub_step++;
333 /* if this attempt would hit where we started, indicate a complete
334 * iteration has occurred */
335 if ((fepriv->auto_step == fepriv->started_auto_step) &&
336 (fepriv->auto_sub_step == 0) && check_wrapped) {
340 dprintk("%s: drift:%i inversion:%i auto_step:%i "
341 "auto_sub_step:%i started_auto_step:%i\n",
342 __func__, fepriv->lnb_drift, fepriv->inversion,
343 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
345 /* set the frontend itself */
346 fepriv->parameters.frequency += fepriv->lnb_drift;
348 fepriv->parameters.inversion = fepriv->inversion;
349 if (fe->ops.set_frontend)
350 fe_set_err = fe->ops.set_frontend(fe, &fepriv->parameters);
351 if (fe_set_err < 0) {
352 fepriv->state = FESTATE_ERROR;
356 fepriv->parameters.frequency = original_frequency;
357 fepriv->parameters.inversion = original_inversion;
359 fepriv->auto_sub_step++;
363 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
367 struct dvb_frontend_private *fepriv = fe->frontend_priv;
369 /* if we've got no parameters, just keep idling */
370 if (fepriv->state & FESTATE_IDLE) {
371 fepriv->delay = 3*HZ;
376 /* in SCAN mode, we just set the frontend when asked and leave it alone */
377 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
378 if (fepriv->state & FESTATE_RETUNE) {
379 if (fe->ops.set_frontend)
380 retval = fe->ops.set_frontend(fe,
381 &fepriv->parameters);
383 fepriv->state = FESTATE_ERROR;
385 fepriv->state = FESTATE_TUNED;
387 fepriv->delay = 3*HZ;
392 /* get the frontend status */
393 if (fepriv->state & FESTATE_RETUNE) {
396 if (fe->ops.read_status)
397 fe->ops.read_status(fe, &s);
398 if (s != fepriv->status) {
399 dvb_frontend_add_event(fe, s);
404 /* if we're not tuned, and we have a lock, move to the TUNED state */
405 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
406 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
407 fepriv->state = FESTATE_TUNED;
409 /* if we're tuned, then we have determined the correct inversion */
410 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
411 (fepriv->parameters.inversion == INVERSION_AUTO)) {
412 fepriv->parameters.inversion = fepriv->inversion;
417 /* if we are tuned already, check we're still locked */
418 if (fepriv->state & FESTATE_TUNED) {
419 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
421 /* we're tuned, and the lock is still good... */
422 if (s & FE_HAS_LOCK) {
424 } else { /* if we _WERE_ tuned, but now don't have a lock */
425 fepriv->state = FESTATE_ZIGZAG_FAST;
426 fepriv->started_auto_step = fepriv->auto_step;
427 fepriv->check_wrapped = 0;
431 /* don't actually do anything if we're in the LOSTLOCK state,
432 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
433 if ((fepriv->state & FESTATE_LOSTLOCK) &&
434 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
435 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
439 /* don't do anything if we're in the DISEQC state, since this
440 * might be someone with a motorized dish controlled by DISEQC.
441 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
442 if (fepriv->state & FESTATE_DISEQC) {
443 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
447 /* if we're in the RETUNE state, set everything up for a brand
448 * new scan, keeping the current inversion setting, as the next
449 * tune is _very_ likely to require the same */
450 if (fepriv->state & FESTATE_RETUNE) {
451 fepriv->lnb_drift = 0;
452 fepriv->auto_step = 0;
453 fepriv->auto_sub_step = 0;
454 fepriv->started_auto_step = 0;
455 fepriv->check_wrapped = 0;
459 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
460 fepriv->delay = fepriv->min_delay;
463 retval = dvb_frontend_swzigzag_autotune(fe,
464 fepriv->check_wrapped);
468 /* OK, if we've run out of trials at the fast speed.
469 * Drop back to slow for the _next_ attempt */
470 fepriv->state = FESTATE_SEARCHING_SLOW;
471 fepriv->started_auto_step = fepriv->auto_step;
474 fepriv->check_wrapped = 1;
476 /* if we've just retuned, enter the ZIGZAG_FAST state.
477 * This ensures we cannot return from an
478 * FE_SET_FRONTEND ioctl before the first frontend tune
480 if (fepriv->state & FESTATE_RETUNE) {
481 fepriv->state = FESTATE_TUNING_FAST;
486 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
487 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
489 /* Note: don't bother checking for wrapping; we stay in this
490 * state until we get a lock */
491 dvb_frontend_swzigzag_autotune(fe, 0);
495 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
497 struct dvb_frontend_private *fepriv = fe->frontend_priv;
502 if (fepriv->dvbdev->writers == 1)
503 if (time_after(jiffies, fepriv->release_jiffies +
504 dvb_shutdown_timeout * HZ))
510 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
512 struct dvb_frontend_private *fepriv = fe->frontend_priv;
514 if (fepriv->wakeup) {
518 return dvb_frontend_is_exiting(fe);
521 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
523 struct dvb_frontend_private *fepriv = fe->frontend_priv;
526 wake_up_interruptible(&fepriv->wait_queue);
529 static int dvb_frontend_thread(void *data)
531 struct dvb_frontend *fe = data;
532 struct dvb_frontend_private *fepriv = fe->frontend_priv;
533 unsigned long timeout;
535 enum dvbfe_algo algo;
537 struct dvb_frontend_parameters *params;
539 dprintk("%s\n", __func__);
541 fepriv->check_wrapped = 0;
543 fepriv->delay = 3*HZ;
546 fepriv->reinitialise = 0;
548 dvb_frontend_init(fe);
552 up(&fepriv->sem); /* is locked when we enter the thread... */
554 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
555 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
556 || freezing(current),
559 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
560 /* got signal or quitting */
568 if (down_interruptible(&fepriv->sem))
571 if (fepriv->reinitialise) {
572 dvb_frontend_init(fe);
573 if (fepriv->tone != -1) {
574 fe->ops.set_tone(fe, fepriv->tone);
576 if (fepriv->voltage != -1) {
577 fe->ops.set_voltage(fe, fepriv->voltage);
579 fepriv->reinitialise = 0;
582 /* do an iteration of the tuning loop */
583 if (fe->ops.get_frontend_algo) {
584 algo = fe->ops.get_frontend_algo(fe);
587 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
588 params = NULL; /* have we been asked to RETUNE ? */
590 if (fepriv->state & FESTATE_RETUNE) {
591 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
592 params = &fepriv->parameters;
593 fepriv->state = FESTATE_TUNED;
597 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
599 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
600 dprintk("%s: state changed, adding current state\n", __func__);
601 dvb_frontend_add_event(fe, s);
606 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
607 dvb_frontend_swzigzag(fe);
609 case DVBFE_ALGO_CUSTOM:
610 params = NULL; /* have we been asked to RETUNE ? */
611 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
612 if (fepriv->state & FESTATE_RETUNE) {
613 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
614 params = &fepriv->parameters;
615 fepriv->state = FESTATE_TUNED;
617 /* Case where we are going to search for a carrier
618 * User asked us to retune again for some reason, possibly
619 * requesting a search with a new set of parameters
621 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
622 if (fe->ops.search) {
623 fepriv->algo_status = fe->ops.search(fe, &fepriv->parameters);
624 /* We did do a search as was requested, the flags are
625 * now unset as well and has the flags wrt to search.
628 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
631 /* Track the carrier if the search was successful */
632 if (fepriv->algo_status == DVBFE_ALGO_SEARCH_SUCCESS) {
634 fe->ops.track(fe, &fepriv->parameters);
636 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
637 fepriv->delay = HZ / 2;
639 fe->ops.read_status(fe, &s);
640 if (s != fepriv->status) {
641 dvb_frontend_add_event(fe, s); /* update event list */
643 if (!(s & FE_HAS_LOCK)) {
644 fepriv->delay = HZ / 10;
645 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
647 fepriv->delay = 60 * HZ;
652 dprintk("%s: UNDEFINED ALGO !\n", __func__);
656 dvb_frontend_swzigzag(fe);
660 if (dvb_powerdown_on_sleep) {
661 if (fe->ops.set_voltage)
662 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
663 if (fe->ops.tuner_ops.sleep) {
664 if (fe->ops.i2c_gate_ctrl)
665 fe->ops.i2c_gate_ctrl(fe, 1);
666 fe->ops.tuner_ops.sleep(fe);
667 if (fe->ops.i2c_gate_ctrl)
668 fe->ops.i2c_gate_ctrl(fe, 0);
674 fepriv->thread = NULL;
678 dvb_frontend_wakeup(fe);
682 static void dvb_frontend_stop(struct dvb_frontend *fe)
684 struct dvb_frontend_private *fepriv = fe->frontend_priv;
686 dprintk ("%s\n", __func__);
694 kthread_stop(fepriv->thread);
696 init_MUTEX (&fepriv->sem);
697 fepriv->state = FESTATE_IDLE;
699 /* paranoia check in case a signal arrived */
701 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
705 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
707 return ((curtime.tv_usec < lasttime.tv_usec) ?
708 1000000 - lasttime.tv_usec + curtime.tv_usec :
709 curtime.tv_usec - lasttime.tv_usec);
711 EXPORT_SYMBOL(timeval_usec_diff);
713 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
715 curtime->tv_usec += add_usec;
716 if (curtime->tv_usec >= 1000000) {
717 curtime->tv_usec -= 1000000;
723 * Sleep until gettimeofday() > waketime + add_usec
724 * This needs to be as precise as possible, but as the delay is
725 * usually between 2ms and 32ms, it is done using a scheduled msleep
726 * followed by usleep (normally a busy-wait loop) for the remainder
728 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
730 struct timeval lasttime;
733 timeval_usec_add(waketime, add_usec);
735 do_gettimeofday(&lasttime);
736 delta = timeval_usec_diff(lasttime, *waketime);
738 msleep((delta - 1500) / 1000);
739 do_gettimeofday(&lasttime);
740 newdelta = timeval_usec_diff(lasttime, *waketime);
741 delta = (newdelta > delta) ? 0 : newdelta;
746 EXPORT_SYMBOL(dvb_frontend_sleep_until);
748 static int dvb_frontend_start(struct dvb_frontend *fe)
751 struct dvb_frontend_private *fepriv = fe->frontend_priv;
752 struct task_struct *fe_thread;
754 dprintk ("%s\n", __func__);
756 if (fepriv->thread) {
760 dvb_frontend_stop (fe);
763 if (signal_pending(current))
765 if (down_interruptible (&fepriv->sem))
768 fepriv->state = FESTATE_IDLE;
770 fepriv->thread = NULL;
773 fe_thread = kthread_run(dvb_frontend_thread, fe,
774 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
775 if (IS_ERR(fe_thread)) {
776 ret = PTR_ERR(fe_thread);
777 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
781 fepriv->thread = fe_thread;
785 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
786 u32 *freq_min, u32 *freq_max)
788 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
790 if (fe->ops.info.frequency_max == 0)
791 *freq_max = fe->ops.tuner_ops.info.frequency_max;
792 else if (fe->ops.tuner_ops.info.frequency_max == 0)
793 *freq_max = fe->ops.info.frequency_max;
795 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
797 if (*freq_min == 0 || *freq_max == 0)
798 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
799 fe->dvb->num,fe->id);
802 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
803 struct dvb_frontend_parameters *parms)
808 /* range check: frequency */
809 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
810 if ((freq_min && parms->frequency < freq_min) ||
811 (freq_max && parms->frequency > freq_max)) {
812 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
813 fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
817 /* range check: symbol rate */
818 if (fe->ops.info.type == FE_QPSK) {
819 if ((fe->ops.info.symbol_rate_min &&
820 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
821 (fe->ops.info.symbol_rate_max &&
822 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
823 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
824 fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
825 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
829 } else if (fe->ops.info.type == FE_QAM) {
830 if ((fe->ops.info.symbol_rate_min &&
831 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
832 (fe->ops.info.symbol_rate_max &&
833 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
834 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
835 fe->dvb->num, fe->id, parms->u.qam.symbol_rate,
836 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
844 static struct dtv_cmds_h dtv_cmds[] = {
858 .name = "DTV_FREQUENCY",
859 .cmd = DTV_FREQUENCY,
862 [DTV_BANDWIDTH_HZ] = {
863 .name = "DTV_BANDWIDTH_HZ",
864 .cmd = DTV_BANDWIDTH_HZ,
868 .name = "DTV_MODULATION",
869 .cmd = DTV_MODULATION,
873 .name = "DTV_INVERSION",
874 .cmd = DTV_INVERSION,
877 [DTV_DISEQC_MASTER] = {
878 .name = "DTV_DISEQC_MASTER",
879 .cmd = DTV_DISEQC_MASTER,
883 [DTV_SYMBOL_RATE] = {
884 .name = "DTV_SYMBOL_RATE",
885 .cmd = DTV_SYMBOL_RATE,
889 .name = "DTV_INNER_FEC",
890 .cmd = DTV_INNER_FEC,
894 .name = "DTV_VOLTAGE",
909 .name = "DTV_ROLLOFF",
913 [DTV_DELIVERY_SYSTEM] = {
914 .name = "DTV_DELIVERY_SYSTEM",
915 .cmd = DTV_DELIVERY_SYSTEM,
919 .name = "DTV_HIERARCHY",
920 .cmd = DTV_HIERARCHY,
923 [DTV_CODE_RATE_HP] = {
924 .name = "DTV_CODE_RATE_HP",
925 .cmd = DTV_CODE_RATE_HP,
928 [DTV_CODE_RATE_LP] = {
929 .name = "DTV_CODE_RATE_LP",
930 .cmd = DTV_CODE_RATE_LP,
933 [DTV_GUARD_INTERVAL] = {
934 .name = "DTV_GUARD_INTERVAL",
935 .cmd = DTV_GUARD_INTERVAL,
938 [DTV_TRANSMISSION_MODE] = {
939 .name = "DTV_TRANSMISSION_MODE",
940 .cmd = DTV_TRANSMISSION_MODE,
944 [DTV_DISEQC_SLAVE_REPLY] = {
945 .name = "DTV_DISEQC_SLAVE_REPLY",
946 .cmd = DTV_DISEQC_SLAVE_REPLY,
950 [DTV_API_VERSION] = {
951 .name = "DTV_API_VERSION",
952 .cmd = DTV_API_VERSION,
955 [DTV_CODE_RATE_HP] = {
956 .name = "DTV_CODE_RATE_HP",
957 .cmd = DTV_CODE_RATE_HP,
960 [DTV_CODE_RATE_LP] = {
961 .name = "DTV_CODE_RATE_LP",
962 .cmd = DTV_CODE_RATE_LP,
965 [DTV_GUARD_INTERVAL] = {
966 .name = "DTV_GUARD_INTERVAL",
967 .cmd = DTV_GUARD_INTERVAL,
970 [DTV_TRANSMISSION_MODE] = {
971 .name = "DTV_TRANSMISSION_MODE",
972 .cmd = DTV_TRANSMISSION_MODE,
976 .name = "DTV_HIERARCHY",
977 .cmd = DTV_HIERARCHY,
982 static void dtv_property_dump(struct dtv_property *tvp)
986 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
987 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
992 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
995 ,dtv_cmds[ tvp->cmd ].name);
997 if(dtv_cmds[ tvp->cmd ].buffer) {
999 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1001 ,tvp->u.buffer.len);
1003 for(i = 0; i < tvp->u.buffer.len; i++)
1004 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1007 ,tvp->u.buffer.data[i]);
1010 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1013 static int is_legacy_delivery_system(fe_delivery_system_t s)
1015 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
1016 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS) ||
1023 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1024 * drivers can use a single set_frontend tuning function, regardless of whether
1025 * it's being used for the legacy or new API, reducing code and complexity.
1027 static void dtv_property_cache_sync(struct dvb_frontend *fe,
1028 struct dvb_frontend_parameters *p)
1030 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1032 c->frequency = p->frequency;
1033 c->inversion = p->inversion;
1035 switch (fe->ops.info.type) {
1037 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1038 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1039 c->symbol_rate = p->u.qpsk.symbol_rate;
1040 c->fec_inner = p->u.qpsk.fec_inner;
1041 c->delivery_system = SYS_DVBS;
1044 c->symbol_rate = p->u.qam.symbol_rate;
1045 c->fec_inner = p->u.qam.fec_inner;
1046 c->modulation = p->u.qam.modulation;
1047 c->delivery_system = SYS_DVBC_ANNEX_AC;
1050 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
1051 c->bandwidth_hz = 6000000;
1052 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
1053 c->bandwidth_hz = 7000000;
1054 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
1055 c->bandwidth_hz = 8000000;
1057 /* Including BANDWIDTH_AUTO */
1058 c->bandwidth_hz = 0;
1059 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1060 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1061 c->modulation = p->u.ofdm.constellation;
1062 c->transmission_mode = p->u.ofdm.transmission_mode;
1063 c->guard_interval = p->u.ofdm.guard_interval;
1064 c->hierarchy = p->u.ofdm.hierarchy_information;
1065 c->delivery_system = SYS_DVBT;
1068 c->modulation = p->u.vsb.modulation;
1069 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1070 c->delivery_system = SYS_ATSC;
1072 c->delivery_system = SYS_DVBC_ANNEX_B;
1077 /* Ensure the cached values are set correctly in the frontend
1078 * legacy tuning structures, for the advanced tuning API.
1080 static void dtv_property_legacy_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 p->frequency = c->frequency;
1087 p->inversion = c->inversion;
1089 switch (fe->ops.info.type) {
1091 dprintk("%s() Preparing QPSK req\n", __func__);
1092 p->u.qpsk.symbol_rate = c->symbol_rate;
1093 p->u.qpsk.fec_inner = c->fec_inner;
1094 c->delivery_system = SYS_DVBS;
1097 dprintk("%s() Preparing QAM req\n", __func__);
1098 p->u.qam.symbol_rate = c->symbol_rate;
1099 p->u.qam.fec_inner = c->fec_inner;
1100 p->u.qam.modulation = c->modulation;
1101 c->delivery_system = SYS_DVBC_ANNEX_AC;
1104 dprintk("%s() Preparing OFDM req\n", __func__);
1105 if (c->bandwidth_hz == 6000000)
1106 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1107 else if (c->bandwidth_hz == 7000000)
1108 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1109 else if (c->bandwidth_hz == 8000000)
1110 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1112 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1113 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1114 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1115 p->u.ofdm.constellation = c->modulation;
1116 p->u.ofdm.transmission_mode = c->transmission_mode;
1117 p->u.ofdm.guard_interval = c->guard_interval;
1118 p->u.ofdm.hierarchy_information = c->hierarchy;
1119 c->delivery_system = SYS_DVBT;
1122 dprintk("%s() Preparing VSB req\n", __func__);
1123 p->u.vsb.modulation = c->modulation;
1124 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1125 c->delivery_system = SYS_ATSC;
1127 c->delivery_system = SYS_DVBC_ANNEX_B;
1132 /* Ensure the cached values are set correctly in the frontend
1133 * legacy tuning structures, for the legacy tuning API.
1135 static void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1137 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1138 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1139 struct dvb_frontend_parameters *p = &fepriv->parameters;
1141 p->frequency = c->frequency;
1142 p->inversion = c->inversion;
1144 switch(c->modulation) {
1149 p->u.qpsk.symbol_rate = c->symbol_rate;
1150 p->u.qpsk.fec_inner = c->fec_inner;
1156 if(c->delivery_system == SYS_ISDBT) {
1157 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1158 p->frequency = c->frequency;
1159 p->inversion = INVERSION_AUTO;
1160 p->u.ofdm.constellation = QAM_AUTO;
1161 p->u.ofdm.code_rate_HP = FEC_AUTO;
1162 p->u.ofdm.code_rate_LP = FEC_AUTO;
1163 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1164 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1165 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1166 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1170 static void dtv_property_cache_submit(struct dvb_frontend *fe)
1172 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1174 /* For legacy delivery systems we don't need the delivery_system to
1175 * be specified, but we populate the older structures from the cache
1176 * so we can call set_frontend on older drivers.
1178 if(is_legacy_delivery_system(c->delivery_system)) {
1180 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1181 dtv_property_legacy_params_sync(fe);
1184 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1186 /* For advanced delivery systems / modulation types ...
1187 * we seed the lecacy dvb_frontend_parameters structure
1188 * so that the sanity checking code later in the IOCTL processing
1189 * can validate our basic frequency ranges, symbolrates, modulation
1192 dtv_property_adv_params_sync(fe);
1196 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1197 unsigned int cmd, void *parg);
1198 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1199 unsigned int cmd, void *parg);
1201 static int dtv_property_process_get(struct dvb_frontend *fe,
1202 struct dtv_property *tvp,
1203 struct inode *inode, struct file *file)
1207 dtv_property_dump(tvp);
1209 /* Allow the frontend to validate incoming properties */
1210 if (fe->ops.get_property)
1211 r = fe->ops.get_property(fe, tvp);
1218 tvp->u.data = fe->dtv_property_cache.frequency;
1220 case DTV_MODULATION:
1221 tvp->u.data = fe->dtv_property_cache.modulation;
1223 case DTV_BANDWIDTH_HZ:
1224 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1227 tvp->u.data = fe->dtv_property_cache.inversion;
1229 case DTV_SYMBOL_RATE:
1230 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1233 tvp->u.data = fe->dtv_property_cache.fec_inner;
1236 tvp->u.data = fe->dtv_property_cache.pilot;
1239 tvp->u.data = fe->dtv_property_cache.rolloff;
1241 case DTV_DELIVERY_SYSTEM:
1242 tvp->u.data = fe->dtv_property_cache.delivery_system;
1245 tvp->u.data = fe->dtv_property_cache.voltage;
1248 tvp->u.data = fe->dtv_property_cache.sectone;
1250 case DTV_API_VERSION:
1251 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1253 case DTV_CODE_RATE_HP:
1254 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1256 case DTV_CODE_RATE_LP:
1257 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1259 case DTV_GUARD_INTERVAL:
1260 tvp->u.data = fe->dtv_property_cache.guard_interval;
1262 case DTV_TRANSMISSION_MODE:
1263 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1266 tvp->u.data = fe->dtv_property_cache.hierarchy;
1275 static int dtv_property_process_set(struct dvb_frontend *fe,
1276 struct dtv_property *tvp,
1277 struct inode *inode,
1281 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1282 dtv_property_dump(tvp);
1284 /* Allow the frontend to validate incoming properties */
1285 if (fe->ops.set_property)
1286 r = fe->ops.set_property(fe, tvp);
1293 /* Reset a cache of data specific to the frontend here. This does
1294 * not effect hardware.
1296 dprintk("%s() Flushing property cache\n", __func__);
1297 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1298 fe->dtv_property_cache.state = tvp->cmd;
1299 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1302 /* interpret the cache of data, build either a traditional frontend
1303 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1306 fe->dtv_property_cache.state = tvp->cmd;
1307 dprintk("%s() Finalised property cache\n", __func__);
1308 dtv_property_cache_submit(fe);
1310 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1311 &fepriv->parameters);
1314 fe->dtv_property_cache.frequency = tvp->u.data;
1316 case DTV_MODULATION:
1317 fe->dtv_property_cache.modulation = tvp->u.data;
1319 case DTV_BANDWIDTH_HZ:
1320 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1323 fe->dtv_property_cache.inversion = tvp->u.data;
1325 case DTV_SYMBOL_RATE:
1326 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1329 fe->dtv_property_cache.fec_inner = tvp->u.data;
1332 fe->dtv_property_cache.pilot = tvp->u.data;
1335 fe->dtv_property_cache.rolloff = tvp->u.data;
1337 case DTV_DELIVERY_SYSTEM:
1338 fe->dtv_property_cache.delivery_system = tvp->u.data;
1341 fe->dtv_property_cache.voltage = tvp->u.data;
1342 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1343 (void *)fe->dtv_property_cache.voltage);
1346 fe->dtv_property_cache.sectone = tvp->u.data;
1347 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1348 (void *)fe->dtv_property_cache.sectone);
1350 case DTV_CODE_RATE_HP:
1351 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1353 case DTV_CODE_RATE_LP:
1354 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1356 case DTV_GUARD_INTERVAL:
1357 fe->dtv_property_cache.guard_interval = tvp->u.data;
1359 case DTV_TRANSMISSION_MODE:
1360 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1363 fe->dtv_property_cache.hierarchy = tvp->u.data;
1372 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1373 unsigned int cmd, void *parg)
1375 struct dvb_device *dvbdev = file->private_data;
1376 struct dvb_frontend *fe = dvbdev->priv;
1377 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1378 int err = -EOPNOTSUPP;
1380 dprintk ("%s\n", __func__);
1385 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1386 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1387 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1390 if (down_interruptible (&fepriv->sem))
1391 return -ERESTARTSYS;
1393 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1394 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1396 fe->dtv_property_cache.state = DTV_UNDEFINED;
1397 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1404 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1405 unsigned int cmd, void *parg)
1407 struct dvb_device *dvbdev = file->private_data;
1408 struct dvb_frontend *fe = dvbdev->priv;
1411 struct dtv_properties *tvps = NULL;
1412 struct dtv_property *tvp = NULL;
1415 dprintk("%s\n", __func__);
1417 if(cmd == FE_SET_PROPERTY) {
1418 tvps = (struct dtv_properties __user *)parg;
1420 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1421 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1423 /* Put an arbitrary limit on the number of messages that can
1424 * be sent at once */
1425 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1428 tvp = (struct dtv_property *) kmalloc(tvps->num *
1429 sizeof(struct dtv_property), GFP_KERNEL);
1435 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1440 for (i = 0; i < tvps->num; i++) {
1441 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1442 err |= (tvp + i)->result;
1445 if(fe->dtv_property_cache.state == DTV_TUNE)
1446 dprintk("%s() Property cache is full, tuning\n", __func__);
1449 if(cmd == FE_GET_PROPERTY) {
1451 tvps = (struct dtv_properties __user *)parg;
1453 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1454 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1456 /* Put an arbitrary limit on the number of messages that can
1457 * be sent at once */
1458 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1461 tvp = (struct dtv_property *) kmalloc(tvps->num *
1462 sizeof(struct dtv_property), GFP_KERNEL);
1468 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1473 for (i = 0; i < tvps->num; i++) {
1474 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1475 err |= (tvp + i)->result;
1478 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1491 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1492 unsigned int cmd, void *parg)
1494 struct dvb_device *dvbdev = file->private_data;
1495 struct dvb_frontend *fe = dvbdev->priv;
1496 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1497 int err = -EOPNOTSUPP;
1501 struct dvb_frontend_info* info = parg;
1502 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1503 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1505 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1506 * do it, it is done for it. */
1507 info->caps |= FE_CAN_INVERSION_AUTO;
1512 case FE_READ_STATUS: {
1513 fe_status_t* status = parg;
1515 /* if retune was requested but hasn't occured yet, prevent
1516 * that user get signal state from previous tuning */
1517 if (fepriv->state == FESTATE_RETUNE ||
1518 fepriv->state == FESTATE_ERROR) {
1524 if (fe->ops.read_status)
1525 err = fe->ops.read_status(fe, status);
1529 if (fe->ops.read_ber)
1530 err = fe->ops.read_ber(fe, (__u32*) parg);
1533 case FE_READ_SIGNAL_STRENGTH:
1534 if (fe->ops.read_signal_strength)
1535 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1539 if (fe->ops.read_snr)
1540 err = fe->ops.read_snr(fe, (__u16*) parg);
1543 case FE_READ_UNCORRECTED_BLOCKS:
1544 if (fe->ops.read_ucblocks)
1545 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1549 case FE_DISEQC_RESET_OVERLOAD:
1550 if (fe->ops.diseqc_reset_overload) {
1551 err = fe->ops.diseqc_reset_overload(fe);
1552 fepriv->state = FESTATE_DISEQC;
1557 case FE_DISEQC_SEND_MASTER_CMD:
1558 if (fe->ops.diseqc_send_master_cmd) {
1559 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1560 fepriv->state = FESTATE_DISEQC;
1565 case FE_DISEQC_SEND_BURST:
1566 if (fe->ops.diseqc_send_burst) {
1567 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1568 fepriv->state = FESTATE_DISEQC;
1574 if (fe->ops.set_tone) {
1575 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1576 fepriv->tone = (fe_sec_tone_mode_t) parg;
1577 fepriv->state = FESTATE_DISEQC;
1582 case FE_SET_VOLTAGE:
1583 if (fe->ops.set_voltage) {
1584 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1585 fepriv->voltage = (fe_sec_voltage_t) parg;
1586 fepriv->state = FESTATE_DISEQC;
1591 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1592 if (fe->ops.dishnetwork_send_legacy_command) {
1593 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1594 fepriv->state = FESTATE_DISEQC;
1596 } else if (fe->ops.set_voltage) {
1598 * NOTE: This is a fallback condition. Some frontends
1599 * (stv0299 for instance) take longer than 8msec to
1600 * respond to a set_voltage command. Those switches
1601 * need custom routines to switch properly. For all
1602 * other frontends, the following shoule work ok.
1603 * Dish network legacy switches (as used by Dish500)
1604 * are controlled by sending 9-bit command words
1605 * spaced 8msec apart.
1606 * the actual command word is switch/port dependant
1607 * so it is up to the userspace application to send
1608 * the right command.
1609 * The command must always start with a '0' after
1610 * initialization, so parg is 8 bits and does not
1611 * include the initialization or start bit
1613 unsigned long swcmd = ((unsigned long) parg) << 1;
1614 struct timeval nexttime;
1615 struct timeval tv[10];
1618 if (dvb_frontend_debug)
1619 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1620 do_gettimeofday(&nexttime);
1621 if (dvb_frontend_debug)
1622 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1623 /* before sending a command, initialize by sending
1624 * a 32ms 18V to the switch
1626 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1627 dvb_frontend_sleep_until(&nexttime, 32000);
1629 for (i = 0; i < 9; i++) {
1630 if (dvb_frontend_debug)
1631 do_gettimeofday(&tv[i + 1]);
1632 if ((swcmd & 0x01) != last) {
1633 /* set voltage to (last ? 13V : 18V) */
1634 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1635 last = (last) ? 0 : 1;
1639 dvb_frontend_sleep_until(&nexttime, 8000);
1641 if (dvb_frontend_debug) {
1642 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1643 __func__, fe->dvb->num);
1644 for (i = 1; i < 10; i++)
1645 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1648 fepriv->state = FESTATE_DISEQC;
1653 case FE_DISEQC_RECV_SLAVE_REPLY:
1654 if (fe->ops.diseqc_recv_slave_reply)
1655 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1658 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1659 if (fe->ops.enable_high_lnb_voltage)
1660 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1663 case FE_SET_FRONTEND: {
1664 struct dvb_frontend_tune_settings fetunesettings;
1666 if(fe->dtv_property_cache.state == DTV_TUNE) {
1667 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1672 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1677 memcpy (&fepriv->parameters, parg,
1678 sizeof (struct dvb_frontend_parameters));
1679 dtv_property_cache_sync(fe, &fepriv->parameters);
1682 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1683 memcpy(&fetunesettings.parameters, parg,
1684 sizeof (struct dvb_frontend_parameters));
1686 /* force auto frequency inversion if requested */
1687 if (dvb_force_auto_inversion) {
1688 fepriv->parameters.inversion = INVERSION_AUTO;
1689 fetunesettings.parameters.inversion = INVERSION_AUTO;
1691 if (fe->ops.info.type == FE_OFDM) {
1692 /* without hierarchical coding code_rate_LP is irrelevant,
1693 * so we tolerate the otherwise invalid FEC_NONE setting */
1694 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1695 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1696 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1699 /* get frontend-specific tuning settings */
1700 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1701 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1702 fepriv->max_drift = fetunesettings.max_drift;
1703 fepriv->step_size = fetunesettings.step_size;
1705 /* default values */
1706 switch(fe->ops.info.type) {
1708 fepriv->min_delay = HZ/20;
1709 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1710 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1714 fepriv->min_delay = HZ/20;
1715 fepriv->step_size = 0; /* no zigzag */
1716 fepriv->max_drift = 0;
1720 fepriv->min_delay = HZ/20;
1721 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1722 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1725 fepriv->min_delay = HZ/20;
1726 fepriv->step_size = 0;
1727 fepriv->max_drift = 0;
1731 if (dvb_override_tune_delay > 0)
1732 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1734 fepriv->state = FESTATE_RETUNE;
1736 /* Request the search algorithm to search */
1737 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1739 dvb_frontend_wakeup(fe);
1740 dvb_frontend_add_event(fe, 0);
1747 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1750 case FE_GET_FRONTEND:
1751 if (fe->ops.get_frontend) {
1752 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1753 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1757 case FE_SET_FRONTEND_TUNE_MODE:
1758 fepriv->tune_mode_flags = (unsigned long) parg;
1767 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1769 struct dvb_device *dvbdev = file->private_data;
1770 struct dvb_frontend *fe = dvbdev->priv;
1771 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1773 dprintk ("%s\n", __func__);
1775 poll_wait (file, &fepriv->events.wait_queue, wait);
1777 if (fepriv->events.eventw != fepriv->events.eventr)
1778 return (POLLIN | POLLRDNORM | POLLPRI);
1783 static int dvb_frontend_open(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;
1788 struct dvb_adapter *adapter = fe->dvb;
1791 dprintk ("%s\n", __func__);
1793 if (adapter->mfe_shared) {
1794 mutex_lock (&adapter->mfe_lock);
1796 if (adapter->mfe_dvbdev == NULL)
1797 adapter->mfe_dvbdev = dvbdev;
1799 else if (adapter->mfe_dvbdev != dvbdev) {
1801 *mfedev = adapter->mfe_dvbdev;
1803 *mfe = mfedev->priv;
1804 struct dvb_frontend_private
1805 *mfepriv = mfe->frontend_priv;
1806 int mferetry = (dvb_mfe_wait_time << 1);
1808 mutex_unlock (&adapter->mfe_lock);
1809 while (mferetry-- && (mfedev->users != -1 ||
1810 mfepriv->thread != NULL)) {
1811 if(msleep_interruptible(500)) {
1812 if(signal_pending(current))
1817 mutex_lock (&adapter->mfe_lock);
1818 if(adapter->mfe_dvbdev != dvbdev) {
1819 mfedev = adapter->mfe_dvbdev;
1821 mfepriv = mfe->frontend_priv;
1822 if (mfedev->users != -1 ||
1823 mfepriv->thread != NULL) {
1824 mutex_unlock (&adapter->mfe_lock);
1827 adapter->mfe_dvbdev = dvbdev;
1832 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1833 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1837 if ((ret = dvb_generic_open (inode, file)) < 0)
1840 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1841 /* normal tune mode when opened R/W */
1842 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1844 fepriv->voltage = -1;
1846 ret = dvb_frontend_start (fe);
1850 /* empty event queue */
1851 fepriv->events.eventr = fepriv->events.eventw = 0;
1854 if (adapter->mfe_shared)
1855 mutex_unlock (&adapter->mfe_lock);
1859 dvb_generic_release(inode, file);
1861 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1862 fe->ops.ts_bus_ctrl(fe, 0);
1864 if (adapter->mfe_shared)
1865 mutex_unlock (&adapter->mfe_lock);
1869 static int dvb_frontend_release(struct inode *inode, struct file *file)
1871 struct dvb_device *dvbdev = file->private_data;
1872 struct dvb_frontend *fe = dvbdev->priv;
1873 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1876 dprintk ("%s\n", __func__);
1878 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1879 fepriv->release_jiffies = jiffies;
1881 ret = dvb_generic_release (inode, file);
1883 if (dvbdev->users == -1) {
1884 if (fepriv->exit == 1) {
1885 fops_put(file->f_op);
1887 wake_up(&dvbdev->wait_queue);
1889 if (fe->ops.ts_bus_ctrl)
1890 fe->ops.ts_bus_ctrl(fe, 0);
1896 static const struct file_operations dvb_frontend_fops = {
1897 .owner = THIS_MODULE,
1898 .ioctl = dvb_generic_ioctl,
1899 .poll = dvb_frontend_poll,
1900 .open = dvb_frontend_open,
1901 .release = dvb_frontend_release
1904 int dvb_register_frontend(struct dvb_adapter* dvb,
1905 struct dvb_frontend* fe)
1907 struct dvb_frontend_private *fepriv;
1908 static const struct dvb_device dvbdev_template = {
1912 .fops = &dvb_frontend_fops,
1913 .kernel_ioctl = dvb_frontend_ioctl
1916 dprintk ("%s\n", __func__);
1918 if (mutex_lock_interruptible(&frontend_mutex))
1919 return -ERESTARTSYS;
1921 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1922 if (fe->frontend_priv == NULL) {
1923 mutex_unlock(&frontend_mutex);
1926 fepriv = fe->frontend_priv;
1928 init_MUTEX (&fepriv->sem);
1929 init_waitqueue_head (&fepriv->wait_queue);
1930 init_waitqueue_head (&fepriv->events.wait_queue);
1931 mutex_init(&fepriv->events.mtx);
1933 fepriv->inversion = INVERSION_OFF;
1935 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1940 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1941 fe, DVB_DEVICE_FRONTEND);
1943 mutex_unlock(&frontend_mutex);
1946 EXPORT_SYMBOL(dvb_register_frontend);
1948 int dvb_unregister_frontend(struct dvb_frontend* fe)
1950 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1951 dprintk ("%s\n", __func__);
1953 mutex_lock(&frontend_mutex);
1954 dvb_frontend_stop (fe);
1955 mutex_unlock(&frontend_mutex);
1957 if (fepriv->dvbdev->users < -1)
1958 wait_event(fepriv->dvbdev->wait_queue,
1959 fepriv->dvbdev->users==-1);
1961 mutex_lock(&frontend_mutex);
1962 dvb_unregister_device (fepriv->dvbdev);
1964 /* fe is invalid now */
1966 mutex_unlock(&frontend_mutex);
1969 EXPORT_SYMBOL(dvb_unregister_frontend);
1971 #ifdef CONFIG_MEDIA_ATTACH
1972 void dvb_frontend_detach(struct dvb_frontend* fe)
1976 if (fe->ops.release_sec) {
1977 fe->ops.release_sec(fe);
1978 symbol_put_addr(fe->ops.release_sec);
1980 if (fe->ops.tuner_ops.release) {
1981 fe->ops.tuner_ops.release(fe);
1982 symbol_put_addr(fe->ops.tuner_ops.release);
1984 if (fe->ops.analog_ops.release) {
1985 fe->ops.analog_ops.release(fe);
1986 symbol_put_addr(fe->ops.analog_ops.release);
1988 ptr = (void*)fe->ops.release;
1990 fe->ops.release(fe);
1991 symbol_put_addr(ptr);
1995 void dvb_frontend_detach(struct dvb_frontend* fe)
1997 if (fe->ops.release_sec)
1998 fe->ops.release_sec(fe);
1999 if (fe->ops.tuner_ops.release)
2000 fe->ops.tuner_ops.release(fe);
2001 if (fe->ops.analog_ops.release)
2002 fe->ops.analog_ops.release(fe);
2003 if (fe->ops.release)
2004 fe->ops.release(fe);
2007 EXPORT_SYMBOL(dvb_frontend_detach);