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 /* Enables DVBv3 compatibility bits at the headers */
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <asm/processor.h>
45 #include "dvb_frontend.h"
47 #include <linux/dvb/version.h>
49 static int dvb_frontend_debug;
50 static int dvb_shutdown_timeout;
51 static int dvb_force_auto_inversion;
52 static int dvb_override_tune_delay;
53 static int dvb_powerdown_on_sleep = 1;
54 static int dvb_mfe_wait_time = 5;
56 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
57 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
58 module_param(dvb_shutdown_timeout, int, 0644);
59 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
60 module_param(dvb_force_auto_inversion, int, 0644);
61 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
62 module_param(dvb_override_tune_delay, int, 0644);
63 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
64 module_param(dvb_powerdown_on_sleep, int, 0644);
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
66 module_param(dvb_mfe_wait_time, int, 0644);
67 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)");
69 #define FESTATE_IDLE 1
70 #define FESTATE_RETUNE 2
71 #define FESTATE_TUNING_FAST 4
72 #define FESTATE_TUNING_SLOW 8
73 #define FESTATE_TUNED 16
74 #define FESTATE_ZIGZAG_FAST 32
75 #define FESTATE_ZIGZAG_SLOW 64
76 #define FESTATE_DISEQC 128
77 #define FESTATE_ERROR 256
78 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
79 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
80 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
81 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
85 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
86 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
87 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
88 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
89 * FESTATE_TUNED. The frontend has successfully locked on.
90 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
91 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
92 * FESTATE_DISEQC. A DISEQC command has just been issued.
93 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
94 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
95 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
96 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
99 static DEFINE_MUTEX(frontend_mutex);
101 struct dvb_frontend_private {
103 /* thread/frontend values */
104 struct dvb_device *dvbdev;
105 struct dvb_frontend_parameters parameters_out;
106 struct dvb_fe_events events;
107 struct semaphore sem;
108 struct list_head list_head;
109 wait_queue_head_t wait_queue;
110 struct task_struct *thread;
111 unsigned long release_jiffies;
114 unsigned long tune_mode_flags;
116 unsigned int reinitialise;
120 /* swzigzag values */
122 unsigned int bending;
124 unsigned int inversion;
125 unsigned int auto_step;
126 unsigned int auto_sub_step;
127 unsigned int started_auto_step;
128 unsigned int min_delay;
129 unsigned int max_drift;
130 unsigned int step_size;
132 unsigned int check_wrapped;
133 enum dvbfe_search algo_status;
136 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
137 static int dtv_get_frontend(struct dvb_frontend *fe,
138 struct dvb_frontend_parameters *p_out);
139 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
140 struct dvb_frontend_parameters *p);
142 static bool has_get_frontend(struct dvb_frontend *fe)
144 return fe->ops.get_frontend != NULL;
148 * Due to DVBv3 API calls, a delivery system should be mapped into one of
149 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
150 * otherwise, a DVBv3 call will fail.
152 enum dvbv3_emulation_type {
160 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
162 switch (delivery_system) {
163 case SYS_DVBC_ANNEX_A:
164 case SYS_DVBC_ANNEX_C:
179 case SYS_DVBC_ANNEX_B:
187 * Doesn't know how to emulate those types and/or
188 * there's no frontend driver from this type yet
189 * with some emulation code, so, we're not sure yet how
190 * to handle them, or they're not compatible with a DVBv3 call.
192 return DVBV3_UNKNOWN;
196 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
198 struct dvb_frontend_private *fepriv = fe->frontend_priv;
199 struct dvb_fe_events *events = &fepriv->events;
200 struct dvb_frontend_event *e;
203 dev_dbg(fe->dvb->device, "%s:\n", __func__);
205 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
206 dtv_get_frontend(fe, &fepriv->parameters_out);
208 mutex_lock(&events->mtx);
210 wp = (events->eventw + 1) % MAX_EVENT;
211 if (wp == events->eventr) {
212 events->overflow = 1;
213 events->eventr = (events->eventr + 1) % MAX_EVENT;
216 e = &events->events[events->eventw];
218 e->parameters = fepriv->parameters_out;
222 mutex_unlock(&events->mtx);
224 wake_up_interruptible (&events->wait_queue);
227 static int dvb_frontend_get_event(struct dvb_frontend *fe,
228 struct dvb_frontend_event *event, int flags)
230 struct dvb_frontend_private *fepriv = fe->frontend_priv;
231 struct dvb_fe_events *events = &fepriv->events;
233 dev_dbg(fe->dvb->device, "%s:\n", __func__);
235 if (events->overflow) {
236 events->overflow = 0;
240 if (events->eventw == events->eventr) {
243 if (flags & O_NONBLOCK)
248 ret = wait_event_interruptible (events->wait_queue,
249 events->eventw != events->eventr);
251 if (down_interruptible (&fepriv->sem))
258 mutex_lock(&events->mtx);
259 *event = events->events[events->eventr];
260 events->eventr = (events->eventr + 1) % MAX_EVENT;
261 mutex_unlock(&events->mtx);
266 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
268 struct dvb_frontend_private *fepriv = fe->frontend_priv;
269 struct dvb_fe_events *events = &fepriv->events;
271 mutex_lock(&events->mtx);
272 events->eventr = events->eventw;
273 mutex_unlock(&events->mtx);
276 static void dvb_frontend_init(struct dvb_frontend *fe)
278 dev_dbg(fe->dvb->device,
279 "%s: initialising adapter %i frontend %i (%s)...\n",
280 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
284 if (fe->ops.tuner_ops.init) {
285 if (fe->ops.i2c_gate_ctrl)
286 fe->ops.i2c_gate_ctrl(fe, 1);
287 fe->ops.tuner_ops.init(fe);
288 if (fe->ops.i2c_gate_ctrl)
289 fe->ops.i2c_gate_ctrl(fe, 0);
293 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
295 struct dvb_frontend_private *fepriv = fe->frontend_priv;
297 fepriv->reinitialise = 1;
298 dvb_frontend_wakeup(fe);
300 EXPORT_SYMBOL(dvb_frontend_reinitialise);
302 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
305 struct dvb_frontend *fe = fepriv->dvbdev->priv;
307 dev_dbg(fe->dvb->device, "%s:\n", __func__);
310 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
312 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
314 q2 = fepriv->quality - 128;
317 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
321 * Performs automatic twiddling of frontend parameters.
323 * @param fe The frontend concerned.
324 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
325 * @returns Number of complete iterations that have been performed.
327 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
332 struct dvb_frontend_private *fepriv = fe->frontend_priv;
333 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
334 int original_inversion = c->inversion;
335 u32 original_frequency = c->frequency;
337 /* are we using autoinversion? */
338 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
339 (c->inversion == INVERSION_AUTO));
341 /* setup parameters correctly */
343 /* calculate the lnb_drift */
344 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
346 /* wrap the auto_step if we've exceeded the maximum drift */
347 if (fepriv->lnb_drift > fepriv->max_drift) {
348 fepriv->auto_step = 0;
349 fepriv->auto_sub_step = 0;
350 fepriv->lnb_drift = 0;
353 /* perform inversion and +/- zigzag */
354 switch(fepriv->auto_sub_step) {
356 /* try with the current inversion and current drift setting */
361 if (!autoinversion) break;
363 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
368 if (fepriv->lnb_drift == 0) break;
370 fepriv->lnb_drift = -fepriv->lnb_drift;
375 if (fepriv->lnb_drift == 0) break;
376 if (!autoinversion) break;
378 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
379 fepriv->lnb_drift = -fepriv->lnb_drift;
385 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
389 if (!ready) fepriv->auto_sub_step++;
392 /* if this attempt would hit where we started, indicate a complete
393 * iteration has occurred */
394 if ((fepriv->auto_step == fepriv->started_auto_step) &&
395 (fepriv->auto_sub_step == 0) && check_wrapped) {
399 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
400 "auto_sub_step:%i started_auto_step:%i\n",
401 __func__, fepriv->lnb_drift, fepriv->inversion,
402 fepriv->auto_step, fepriv->auto_sub_step,
403 fepriv->started_auto_step);
405 /* set the frontend itself */
406 c->frequency += fepriv->lnb_drift;
408 c->inversion = fepriv->inversion;
410 if (fe->ops.set_frontend)
411 fe_set_err = fe->ops.set_frontend(fe);
413 if (fe_set_err < 0) {
414 fepriv->state = FESTATE_ERROR;
418 c->frequency = original_frequency;
419 c->inversion = original_inversion;
421 fepriv->auto_sub_step++;
425 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
429 struct dvb_frontend_private *fepriv = fe->frontend_priv;
430 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
432 /* if we've got no parameters, just keep idling */
433 if (fepriv->state & FESTATE_IDLE) {
434 fepriv->delay = 3*HZ;
439 /* in SCAN mode, we just set the frontend when asked and leave it alone */
440 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
441 if (fepriv->state & FESTATE_RETUNE) {
443 if (fe->ops.set_frontend)
444 retval = fe->ops.set_frontend(fe);
447 fepriv->state = FESTATE_ERROR;
449 fepriv->state = FESTATE_TUNED;
451 fepriv->delay = 3*HZ;
456 /* get the frontend status */
457 if (fepriv->state & FESTATE_RETUNE) {
460 if (fe->ops.read_status)
461 fe->ops.read_status(fe, &s);
462 if (s != fepriv->status) {
463 dvb_frontend_add_event(fe, s);
468 /* if we're not tuned, and we have a lock, move to the TUNED state */
469 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
470 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
471 fepriv->state = FESTATE_TUNED;
473 /* if we're tuned, then we have determined the correct inversion */
474 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
475 (c->inversion == INVERSION_AUTO)) {
476 c->inversion = fepriv->inversion;
481 /* if we are tuned already, check we're still locked */
482 if (fepriv->state & FESTATE_TUNED) {
483 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
485 /* we're tuned, and the lock is still good... */
486 if (s & FE_HAS_LOCK) {
488 } else { /* if we _WERE_ tuned, but now don't have a lock */
489 fepriv->state = FESTATE_ZIGZAG_FAST;
490 fepriv->started_auto_step = fepriv->auto_step;
491 fepriv->check_wrapped = 0;
495 /* don't actually do anything if we're in the LOSTLOCK state,
496 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
497 if ((fepriv->state & FESTATE_LOSTLOCK) &&
498 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
499 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
503 /* don't do anything if we're in the DISEQC state, since this
504 * might be someone with a motorized dish controlled by DISEQC.
505 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
506 if (fepriv->state & FESTATE_DISEQC) {
507 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
511 /* if we're in the RETUNE state, set everything up for a brand
512 * new scan, keeping the current inversion setting, as the next
513 * tune is _very_ likely to require the same */
514 if (fepriv->state & FESTATE_RETUNE) {
515 fepriv->lnb_drift = 0;
516 fepriv->auto_step = 0;
517 fepriv->auto_sub_step = 0;
518 fepriv->started_auto_step = 0;
519 fepriv->check_wrapped = 0;
523 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
524 fepriv->delay = fepriv->min_delay;
527 retval = dvb_frontend_swzigzag_autotune(fe,
528 fepriv->check_wrapped);
532 /* OK, if we've run out of trials at the fast speed.
533 * Drop back to slow for the _next_ attempt */
534 fepriv->state = FESTATE_SEARCHING_SLOW;
535 fepriv->started_auto_step = fepriv->auto_step;
538 fepriv->check_wrapped = 1;
540 /* if we've just retuned, enter the ZIGZAG_FAST state.
541 * This ensures we cannot return from an
542 * FE_SET_FRONTEND ioctl before the first frontend tune
544 if (fepriv->state & FESTATE_RETUNE) {
545 fepriv->state = FESTATE_TUNING_FAST;
550 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
551 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
553 /* Note: don't bother checking for wrapping; we stay in this
554 * state until we get a lock */
555 dvb_frontend_swzigzag_autotune(fe, 0);
559 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
561 struct dvb_frontend_private *fepriv = fe->frontend_priv;
563 if (fe->exit != DVB_FE_NO_EXIT)
566 if (fepriv->dvbdev->writers == 1)
567 if (time_after_eq(jiffies, fepriv->release_jiffies +
568 dvb_shutdown_timeout * HZ))
574 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
576 struct dvb_frontend_private *fepriv = fe->frontend_priv;
578 if (fepriv->wakeup) {
582 return dvb_frontend_is_exiting(fe);
585 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
587 struct dvb_frontend_private *fepriv = fe->frontend_priv;
590 wake_up_interruptible(&fepriv->wait_queue);
593 static int dvb_frontend_thread(void *data)
595 struct dvb_frontend *fe = data;
596 struct dvb_frontend_private *fepriv = fe->frontend_priv;
598 enum dvbfe_algo algo;
600 bool re_tune = false;
601 bool semheld = false;
603 dev_dbg(fe->dvb->device, "%s:\n", __func__);
605 fepriv->check_wrapped = 0;
607 fepriv->delay = 3*HZ;
610 fepriv->reinitialise = 0;
612 dvb_frontend_init(fe);
616 up(&fepriv->sem); /* is locked when we enter the thread... */
618 wait_event_interruptible_timeout(fepriv->wait_queue,
619 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
620 || freezing(current),
623 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
624 /* got signal or quitting */
625 if (!down_interruptible(&fepriv->sem))
627 fe->exit = DVB_FE_NORMAL_EXIT;
634 if (down_interruptible(&fepriv->sem))
637 if (fepriv->reinitialise) {
638 dvb_frontend_init(fe);
639 if (fe->ops.set_tone && fepriv->tone != -1)
640 fe->ops.set_tone(fe, fepriv->tone);
641 if (fe->ops.set_voltage && fepriv->voltage != -1)
642 fe->ops.set_voltage(fe, fepriv->voltage);
643 fepriv->reinitialise = 0;
646 /* do an iteration of the tuning loop */
647 if (fe->ops.get_frontend_algo) {
648 algo = fe->ops.get_frontend_algo(fe);
651 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
653 if (fepriv->state & FESTATE_RETUNE) {
654 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
656 fepriv->state = FESTATE_TUNED;
662 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
664 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
665 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
666 dvb_frontend_add_event(fe, s);
671 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
672 dvb_frontend_swzigzag(fe);
674 case DVBFE_ALGO_CUSTOM:
675 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
676 if (fepriv->state & FESTATE_RETUNE) {
677 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
678 fepriv->state = FESTATE_TUNED;
680 /* Case where we are going to search for a carrier
681 * User asked us to retune again for some reason, possibly
682 * requesting a search with a new set of parameters
684 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
685 if (fe->ops.search) {
686 fepriv->algo_status = fe->ops.search(fe);
687 /* We did do a search as was requested, the flags are
688 * now unset as well and has the flags wrt to search.
691 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
694 /* Track the carrier if the search was successful */
695 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
696 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
697 fepriv->delay = HZ / 2;
699 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
700 fe->ops.read_status(fe, &s);
701 if (s != fepriv->status) {
702 dvb_frontend_add_event(fe, s); /* update event list */
704 if (!(s & FE_HAS_LOCK)) {
705 fepriv->delay = HZ / 10;
706 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
708 fepriv->delay = 60 * HZ;
713 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
717 dvb_frontend_swzigzag(fe);
721 if (dvb_powerdown_on_sleep) {
722 if (fe->ops.set_voltage)
723 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
724 if (fe->ops.tuner_ops.sleep) {
725 if (fe->ops.i2c_gate_ctrl)
726 fe->ops.i2c_gate_ctrl(fe, 1);
727 fe->ops.tuner_ops.sleep(fe);
728 if (fe->ops.i2c_gate_ctrl)
729 fe->ops.i2c_gate_ctrl(fe, 0);
735 fepriv->thread = NULL;
736 if (kthread_should_stop())
737 fe->exit = DVB_FE_DEVICE_REMOVED;
739 fe->exit = DVB_FE_NO_EXIT;
744 dvb_frontend_wakeup(fe);
748 static void dvb_frontend_stop(struct dvb_frontend *fe)
750 struct dvb_frontend_private *fepriv = fe->frontend_priv;
752 dev_dbg(fe->dvb->device, "%s:\n", __func__);
754 if (fe->exit != DVB_FE_DEVICE_REMOVED)
755 fe->exit = DVB_FE_NORMAL_EXIT;
761 kthread_stop(fepriv->thread);
763 sema_init(&fepriv->sem, 1);
764 fepriv->state = FESTATE_IDLE;
766 /* paranoia check in case a signal arrived */
768 dev_warn(fe->dvb->device,
769 "dvb_frontend_stop: warning: thread %p won't exit\n",
773 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
775 return ((curtime.tv_usec < lasttime.tv_usec) ?
776 1000000 - lasttime.tv_usec + curtime.tv_usec :
777 curtime.tv_usec - lasttime.tv_usec);
779 EXPORT_SYMBOL(timeval_usec_diff);
781 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
783 curtime->tv_usec += add_usec;
784 if (curtime->tv_usec >= 1000000) {
785 curtime->tv_usec -= 1000000;
791 * Sleep until gettimeofday() > waketime + add_usec
792 * This needs to be as precise as possible, but as the delay is
793 * usually between 2ms and 32ms, it is done using a scheduled msleep
794 * followed by usleep (normally a busy-wait loop) for the remainder
796 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
798 struct timeval lasttime;
801 timeval_usec_add(waketime, add_usec);
803 do_gettimeofday(&lasttime);
804 delta = timeval_usec_diff(lasttime, *waketime);
806 msleep((delta - 1500) / 1000);
807 do_gettimeofday(&lasttime);
808 newdelta = timeval_usec_diff(lasttime, *waketime);
809 delta = (newdelta > delta) ? 0 : newdelta;
814 EXPORT_SYMBOL(dvb_frontend_sleep_until);
816 static int dvb_frontend_start(struct dvb_frontend *fe)
819 struct dvb_frontend_private *fepriv = fe->frontend_priv;
820 struct task_struct *fe_thread;
822 dev_dbg(fe->dvb->device, "%s:\n", __func__);
824 if (fepriv->thread) {
825 if (fe->exit == DVB_FE_NO_EXIT)
828 dvb_frontend_stop (fe);
831 if (signal_pending(current))
833 if (down_interruptible (&fepriv->sem))
836 fepriv->state = FESTATE_IDLE;
837 fe->exit = DVB_FE_NO_EXIT;
838 fepriv->thread = NULL;
841 fe_thread = kthread_run(dvb_frontend_thread, fe,
842 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
843 if (IS_ERR(fe_thread)) {
844 ret = PTR_ERR(fe_thread);
845 dev_warn(fe->dvb->device,
846 "dvb_frontend_start: failed to start kthread (%d)\n",
851 fepriv->thread = fe_thread;
855 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
856 u32 *freq_min, u32 *freq_max)
858 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
860 if (fe->ops.info.frequency_max == 0)
861 *freq_max = fe->ops.tuner_ops.info.frequency_max;
862 else if (fe->ops.tuner_ops.info.frequency_max == 0)
863 *freq_max = fe->ops.info.frequency_max;
865 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
867 if (*freq_min == 0 || *freq_max == 0)
868 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
869 fe->dvb->num, fe->id);
872 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
874 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
878 /* range check: frequency */
879 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
880 if ((freq_min && c->frequency < freq_min) ||
881 (freq_max && c->frequency > freq_max)) {
882 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
883 fe->dvb->num, fe->id, c->frequency,
888 /* range check: symbol rate */
889 switch (c->delivery_system) {
893 case SYS_DVBC_ANNEX_A:
894 case SYS_DVBC_ANNEX_C:
895 if ((fe->ops.info.symbol_rate_min &&
896 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
897 (fe->ops.info.symbol_rate_max &&
898 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
899 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
900 fe->dvb->num, fe->id, c->symbol_rate,
901 fe->ops.info.symbol_rate_min,
902 fe->ops.info.symbol_rate_max);
912 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
914 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
918 delsys = c->delivery_system;
919 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
920 c->delivery_system = delsys;
922 c->state = DTV_CLEAR;
924 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
925 __func__, c->delivery_system);
927 c->transmission_mode = TRANSMISSION_MODE_AUTO;
928 c->bandwidth_hz = 0; /* AUTO */
929 c->guard_interval = GUARD_INTERVAL_AUTO;
930 c->hierarchy = HIERARCHY_AUTO;
932 c->code_rate_HP = FEC_AUTO;
933 c->code_rate_LP = FEC_AUTO;
934 c->fec_inner = FEC_AUTO;
935 c->rolloff = ROLLOFF_AUTO;
936 c->voltage = SEC_VOLTAGE_OFF;
937 c->sectone = SEC_TONE_OFF;
938 c->pilot = PILOT_AUTO;
940 c->isdbt_partial_reception = 0;
941 c->isdbt_sb_mode = 0;
942 c->isdbt_sb_subchannel = 0;
943 c->isdbt_sb_segment_idx = 0;
944 c->isdbt_sb_segment_count = 0;
945 c->isdbt_layer_enabled = 0;
946 for (i = 0; i < 3; i++) {
947 c->layer[i].fec = FEC_AUTO;
948 c->layer[i].modulation = QAM_AUTO;
949 c->layer[i].interleaving = 0;
950 c->layer[i].segment_count = 0;
953 c->stream_id = NO_STREAM_ID_FILTER;
955 switch (c->delivery_system) {
959 c->modulation = QPSK; /* implied for DVB-S in legacy API */
960 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
963 c->modulation = VSB_8;
966 c->modulation = QAM_AUTO;
975 #define _DTV_CMD(n, s, b) \
983 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
984 _DTV_CMD(DTV_TUNE, 1, 0),
985 _DTV_CMD(DTV_CLEAR, 1, 0),
988 _DTV_CMD(DTV_FREQUENCY, 1, 0),
989 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
990 _DTV_CMD(DTV_MODULATION, 1, 0),
991 _DTV_CMD(DTV_INVERSION, 1, 0),
992 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
993 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
994 _DTV_CMD(DTV_INNER_FEC, 1, 0),
995 _DTV_CMD(DTV_VOLTAGE, 1, 0),
996 _DTV_CMD(DTV_TONE, 1, 0),
997 _DTV_CMD(DTV_PILOT, 1, 0),
998 _DTV_CMD(DTV_ROLLOFF, 1, 0),
999 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1000 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1001 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1002 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1003 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1004 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1005 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1007 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1014 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1016 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1017 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1018 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1019 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1020 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1021 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1022 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1023 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1026 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1027 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1028 _DTV_CMD(DTV_LNA, 1, 0),
1031 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1032 _DTV_CMD(DTV_API_VERSION, 0, 0),
1034 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1036 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1037 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1039 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1040 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1041 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1042 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1043 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1044 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1045 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1046 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1047 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1048 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1049 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1050 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1051 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1053 /* Statistics API */
1054 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1055 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1056 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1057 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1058 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1059 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1060 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1061 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1064 static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
1068 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1069 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1070 __func__, tvp->cmd);
1074 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1075 tvp->cmd, dtv_cmds[tvp->cmd].name);
1077 if (dtv_cmds[tvp->cmd].buffer) {
1078 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1079 __func__, tvp->u.buffer.len);
1081 for(i = 0; i < tvp->u.buffer.len; i++)
1082 dev_dbg(fe->dvb->device,
1083 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1084 __func__, i, tvp->u.buffer.data[i]);
1086 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1091 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1092 * drivers can use a single set_frontend tuning function, regardless of whether
1093 * it's being used for the legacy or new API, reducing code and complexity.
1095 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1096 struct dtv_frontend_properties *c,
1097 const struct dvb_frontend_parameters *p)
1099 c->frequency = p->frequency;
1100 c->inversion = p->inversion;
1102 switch (dvbv3_type(c->delivery_system)) {
1104 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1105 c->symbol_rate = p->u.qpsk.symbol_rate;
1106 c->fec_inner = p->u.qpsk.fec_inner;
1109 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1110 c->symbol_rate = p->u.qam.symbol_rate;
1111 c->fec_inner = p->u.qam.fec_inner;
1112 c->modulation = p->u.qam.modulation;
1115 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1117 switch (p->u.ofdm.bandwidth) {
1118 case BANDWIDTH_10_MHZ:
1119 c->bandwidth_hz = 10000000;
1121 case BANDWIDTH_8_MHZ:
1122 c->bandwidth_hz = 8000000;
1124 case BANDWIDTH_7_MHZ:
1125 c->bandwidth_hz = 7000000;
1127 case BANDWIDTH_6_MHZ:
1128 c->bandwidth_hz = 6000000;
1130 case BANDWIDTH_5_MHZ:
1131 c->bandwidth_hz = 5000000;
1133 case BANDWIDTH_1_712_MHZ:
1134 c->bandwidth_hz = 1712000;
1136 case BANDWIDTH_AUTO:
1137 c->bandwidth_hz = 0;
1140 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1141 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1142 c->modulation = p->u.ofdm.constellation;
1143 c->transmission_mode = p->u.ofdm.transmission_mode;
1144 c->guard_interval = p->u.ofdm.guard_interval;
1145 c->hierarchy = p->u.ofdm.hierarchy_information;
1148 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1149 c->modulation = p->u.vsb.modulation;
1150 if (c->delivery_system == SYS_ATSCMH)
1152 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1153 c->delivery_system = SYS_ATSC;
1155 c->delivery_system = SYS_DVBC_ANNEX_B;
1158 dev_err(fe->dvb->device,
1159 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1160 __func__, c->delivery_system);
1167 /* Ensure the cached values are set correctly in the frontend
1168 * legacy tuning structures, for the advanced tuning API.
1170 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1171 struct dvb_frontend_parameters *p)
1173 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1175 p->frequency = c->frequency;
1176 p->inversion = c->inversion;
1178 switch (dvbv3_type(c->delivery_system)) {
1180 dev_err(fe->dvb->device,
1181 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1182 __func__, c->delivery_system);
1185 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1186 p->u.qpsk.symbol_rate = c->symbol_rate;
1187 p->u.qpsk.fec_inner = c->fec_inner;
1190 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1191 p->u.qam.symbol_rate = c->symbol_rate;
1192 p->u.qam.fec_inner = c->fec_inner;
1193 p->u.qam.modulation = c->modulation;
1196 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1197 switch (c->bandwidth_hz) {
1199 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1202 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1205 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1208 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1211 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1214 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1218 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1220 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1221 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1222 p->u.ofdm.constellation = c->modulation;
1223 p->u.ofdm.transmission_mode = c->transmission_mode;
1224 p->u.ofdm.guard_interval = c->guard_interval;
1225 p->u.ofdm.hierarchy_information = c->hierarchy;
1228 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1229 p->u.vsb.modulation = c->modulation;
1236 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1237 * @fe: struct dvb_frontend pointer
1238 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1239 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1241 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1242 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1243 * If p_out is not null, it will update the DVBv3 params pointed by it.
1245 static int dtv_get_frontend(struct dvb_frontend *fe,
1246 struct dvb_frontend_parameters *p_out)
1250 if (fe->ops.get_frontend) {
1251 r = fe->ops.get_frontend(fe);
1252 if (unlikely(r < 0))
1255 dtv_property_legacy_params_sync(fe, p_out);
1259 /* As everything is in cache, get_frontend fops are always supported */
1263 static int dvb_frontend_ioctl_legacy(struct file *file,
1264 unsigned int cmd, void *parg);
1265 static int dvb_frontend_ioctl_properties(struct file *file,
1266 unsigned int cmd, void *parg);
1268 static int dtv_property_process_get(struct dvb_frontend *fe,
1269 const struct dtv_frontend_properties *c,
1270 struct dtv_property *tvp,
1276 case DTV_ENUM_DELSYS:
1278 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1279 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1282 tvp->u.buffer.len = ncaps;
1285 tvp->u.data = c->frequency;
1287 case DTV_MODULATION:
1288 tvp->u.data = c->modulation;
1290 case DTV_BANDWIDTH_HZ:
1291 tvp->u.data = c->bandwidth_hz;
1294 tvp->u.data = c->inversion;
1296 case DTV_SYMBOL_RATE:
1297 tvp->u.data = c->symbol_rate;
1300 tvp->u.data = c->fec_inner;
1303 tvp->u.data = c->pilot;
1306 tvp->u.data = c->rolloff;
1308 case DTV_DELIVERY_SYSTEM:
1309 tvp->u.data = c->delivery_system;
1312 tvp->u.data = c->voltage;
1315 tvp->u.data = c->sectone;
1317 case DTV_API_VERSION:
1318 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1320 case DTV_CODE_RATE_HP:
1321 tvp->u.data = c->code_rate_HP;
1323 case DTV_CODE_RATE_LP:
1324 tvp->u.data = c->code_rate_LP;
1326 case DTV_GUARD_INTERVAL:
1327 tvp->u.data = c->guard_interval;
1329 case DTV_TRANSMISSION_MODE:
1330 tvp->u.data = c->transmission_mode;
1333 tvp->u.data = c->hierarchy;
1335 case DTV_INTERLEAVING:
1336 tvp->u.data = c->interleaving;
1339 /* ISDB-T Support here */
1340 case DTV_ISDBT_PARTIAL_RECEPTION:
1341 tvp->u.data = c->isdbt_partial_reception;
1343 case DTV_ISDBT_SOUND_BROADCASTING:
1344 tvp->u.data = c->isdbt_sb_mode;
1346 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1347 tvp->u.data = c->isdbt_sb_subchannel;
1349 case DTV_ISDBT_SB_SEGMENT_IDX:
1350 tvp->u.data = c->isdbt_sb_segment_idx;
1352 case DTV_ISDBT_SB_SEGMENT_COUNT:
1353 tvp->u.data = c->isdbt_sb_segment_count;
1355 case DTV_ISDBT_LAYER_ENABLED:
1356 tvp->u.data = c->isdbt_layer_enabled;
1358 case DTV_ISDBT_LAYERA_FEC:
1359 tvp->u.data = c->layer[0].fec;
1361 case DTV_ISDBT_LAYERA_MODULATION:
1362 tvp->u.data = c->layer[0].modulation;
1364 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1365 tvp->u.data = c->layer[0].segment_count;
1367 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1368 tvp->u.data = c->layer[0].interleaving;
1370 case DTV_ISDBT_LAYERB_FEC:
1371 tvp->u.data = c->layer[1].fec;
1373 case DTV_ISDBT_LAYERB_MODULATION:
1374 tvp->u.data = c->layer[1].modulation;
1376 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1377 tvp->u.data = c->layer[1].segment_count;
1379 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1380 tvp->u.data = c->layer[1].interleaving;
1382 case DTV_ISDBT_LAYERC_FEC:
1383 tvp->u.data = c->layer[2].fec;
1385 case DTV_ISDBT_LAYERC_MODULATION:
1386 tvp->u.data = c->layer[2].modulation;
1388 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1389 tvp->u.data = c->layer[2].segment_count;
1391 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1392 tvp->u.data = c->layer[2].interleaving;
1395 /* Multistream support */
1397 case DTV_DVBT2_PLP_ID_LEGACY:
1398 tvp->u.data = c->stream_id;
1402 case DTV_ATSCMH_FIC_VER:
1403 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1405 case DTV_ATSCMH_PARADE_ID:
1406 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1408 case DTV_ATSCMH_NOG:
1409 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1411 case DTV_ATSCMH_TNOG:
1412 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1414 case DTV_ATSCMH_SGN:
1415 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1417 case DTV_ATSCMH_PRC:
1418 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1420 case DTV_ATSCMH_RS_FRAME_MODE:
1421 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1423 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1424 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1426 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1427 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1429 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1430 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1432 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1433 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1435 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1436 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1438 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1439 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1441 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1442 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1444 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1445 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1449 tvp->u.data = c->lna;
1452 /* Fill quality measures */
1453 case DTV_STAT_SIGNAL_STRENGTH:
1454 tvp->u.st = c->strength;
1459 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1460 tvp->u.st = c->pre_bit_error;
1462 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1463 tvp->u.st = c->pre_bit_count;
1465 case DTV_STAT_POST_ERROR_BIT_COUNT:
1466 tvp->u.st = c->post_bit_error;
1468 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1469 tvp->u.st = c->post_bit_count;
1471 case DTV_STAT_ERROR_BLOCK_COUNT:
1472 tvp->u.st = c->block_error;
1474 case DTV_STAT_TOTAL_BLOCK_COUNT:
1475 tvp->u.st = c->block_count;
1478 dev_dbg(fe->dvb->device,
1479 "%s: FE property %d doesn't exist\n",
1480 __func__, tvp->cmd);
1484 /* Allow the frontend to override outgoing properties */
1485 if (fe->ops.get_property) {
1486 r = fe->ops.get_property(fe, tvp);
1491 dtv_property_dump(fe, tvp);
1496 static int dtv_set_frontend(struct dvb_frontend *fe);
1498 static bool is_dvbv3_delsys(u32 delsys)
1502 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1503 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1509 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1510 * @fe: struct frontend;
1511 * @delsys: DVBv5 type that will be used for emulation
1513 * Provides emulation for delivery systems that are compatible with the old
1514 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1515 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1516 * parameters are compatible with DVB-S spec.
1518 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1521 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1523 c->delivery_system = delsys;
1526 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1528 if (c->delivery_system == SYS_ISDBT) {
1529 dev_dbg(fe->dvb->device,
1530 "%s: Using defaults for SYS_ISDBT\n",
1533 if (!c->bandwidth_hz)
1534 c->bandwidth_hz = 6000000;
1536 c->isdbt_partial_reception = 0;
1537 c->isdbt_sb_mode = 0;
1538 c->isdbt_sb_subchannel = 0;
1539 c->isdbt_sb_segment_idx = 0;
1540 c->isdbt_sb_segment_count = 0;
1541 c->isdbt_layer_enabled = 7;
1542 for (i = 0; i < 3; i++) {
1543 c->layer[i].fec = FEC_AUTO;
1544 c->layer[i].modulation = QAM_AUTO;
1545 c->layer[i].interleaving = 0;
1546 c->layer[i].segment_count = 0;
1549 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1550 __func__, c->delivery_system);
1556 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1557 * @fe: frontend struct
1558 * @desired_system: delivery system requested by the user
1560 * A DVBv5 call know what's the desired system it wants. So, set it.
1562 * There are, however, a few known issues with early DVBv5 applications that
1563 * are also handled by this logic:
1565 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1566 * This is an API violation, but, as we don't want to break userspace,
1567 * convert it to the first supported delivery system.
1568 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1569 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1570 * ISDB-T provided backward compat with DVB-T.
1572 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1576 u32 delsys = SYS_UNDEFINED;
1577 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1578 enum dvbv3_emulation_type type;
1581 * It was reported that some old DVBv5 applications were
1582 * filling delivery_system with SYS_UNDEFINED. If this happens,
1583 * assume that the application wants to use the first supported
1586 if (desired_system == SYS_UNDEFINED)
1587 desired_system = fe->ops.delsys[0];
1590 * This is a DVBv5 call. So, it likely knows the supported
1591 * delivery systems. So, check if the desired delivery system is
1595 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1596 if (fe->ops.delsys[ncaps] == desired_system) {
1597 c->delivery_system = desired_system;
1598 dev_dbg(fe->dvb->device,
1599 "%s: Changing delivery system to %d\n",
1600 __func__, desired_system);
1607 * The requested delivery system isn't supported. Maybe userspace
1608 * is requesting a DVBv3 compatible delivery system.
1610 * The emulation only works if the desired system is one of the
1611 * delivery systems supported by DVBv3 API
1613 if (!is_dvbv3_delsys(desired_system)) {
1614 dev_dbg(fe->dvb->device,
1615 "%s: Delivery system %d not supported.\n",
1616 __func__, desired_system);
1620 type = dvbv3_type(desired_system);
1623 * Get the last non-DVBv3 delivery system that has the same type
1624 * of the desired system
1627 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1628 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1629 delsys = fe->ops.delsys[ncaps];
1633 /* There's nothing compatible with the desired delivery system */
1634 if (delsys == SYS_UNDEFINED) {
1635 dev_dbg(fe->dvb->device,
1636 "%s: Delivery system %d not supported on emulation mode.\n",
1637 __func__, desired_system);
1641 dev_dbg(fe->dvb->device,
1642 "%s: Using delivery system %d emulated as if it were %d\n",
1643 __func__, delsys, desired_system);
1645 return emulate_delivery_system(fe, desired_system);
1649 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1650 * @fe: frontend struct
1652 * A DVBv3 call doesn't know what's the desired system it wants. It also
1653 * doesn't allow to switch between different types. Due to that, userspace
1654 * should use DVBv5 instead.
1655 * However, in order to avoid breaking userspace API, limited backward
1656 * compatibility support is provided.
1658 * There are some delivery systems that are incompatible with DVBv3 calls.
1660 * This routine should work fine for frontends that support just one delivery
1663 * For frontends that support multiple frontends:
1664 * 1) It defaults to use the first supported delivery system. There's an
1665 * userspace application that allows changing it at runtime;
1667 * 2) If the current delivery system is not compatible with DVBv3, it gets
1668 * the first one that it is compatible.
1670 * NOTE: in order for this to work with applications like Kaffeine that
1671 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1672 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1673 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1676 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1679 u32 delsys = SYS_UNDEFINED;
1680 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1682 /* If not set yet, defaults to the first supported delivery system */
1683 if (c->delivery_system == SYS_UNDEFINED)
1684 c->delivery_system = fe->ops.delsys[0];
1687 * Trivial case: just use the current one, if it already a DVBv3
1690 if (is_dvbv3_delsys(c->delivery_system)) {
1691 dev_dbg(fe->dvb->device,
1692 "%s: Using delivery system to %d\n",
1693 __func__, c->delivery_system);
1698 * Seek for the first delivery system that it is compatible with a
1702 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1703 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1704 delsys = fe->ops.delsys[ncaps];
1709 if (delsys == SYS_UNDEFINED) {
1710 dev_dbg(fe->dvb->device,
1711 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1715 return emulate_delivery_system(fe, delsys);
1718 static int dtv_property_process_set(struct dvb_frontend *fe,
1719 struct dtv_property *tvp,
1723 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1725 /* Allow the frontend to validate incoming properties */
1726 if (fe->ops.set_property) {
1727 r = fe->ops.set_property(fe, tvp);
1735 * Reset a cache of data specific to the frontend here. This does
1736 * not effect hardware.
1738 dvb_frontend_clear_cache(fe);
1741 /* interpret the cache of data, build either a traditional frontend
1742 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1745 c->state = tvp->cmd;
1746 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1749 r = dtv_set_frontend(fe);
1752 c->frequency = tvp->u.data;
1754 case DTV_MODULATION:
1755 c->modulation = tvp->u.data;
1757 case DTV_BANDWIDTH_HZ:
1758 c->bandwidth_hz = tvp->u.data;
1761 c->inversion = tvp->u.data;
1763 case DTV_SYMBOL_RATE:
1764 c->symbol_rate = tvp->u.data;
1767 c->fec_inner = tvp->u.data;
1770 c->pilot = tvp->u.data;
1773 c->rolloff = tvp->u.data;
1775 case DTV_DELIVERY_SYSTEM:
1776 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1779 c->voltage = tvp->u.data;
1780 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1781 (void *)c->voltage);
1784 c->sectone = tvp->u.data;
1785 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1786 (void *)c->sectone);
1788 case DTV_CODE_RATE_HP:
1789 c->code_rate_HP = tvp->u.data;
1791 case DTV_CODE_RATE_LP:
1792 c->code_rate_LP = tvp->u.data;
1794 case DTV_GUARD_INTERVAL:
1795 c->guard_interval = tvp->u.data;
1797 case DTV_TRANSMISSION_MODE:
1798 c->transmission_mode = tvp->u.data;
1801 c->hierarchy = tvp->u.data;
1803 case DTV_INTERLEAVING:
1804 c->interleaving = tvp->u.data;
1807 /* ISDB-T Support here */
1808 case DTV_ISDBT_PARTIAL_RECEPTION:
1809 c->isdbt_partial_reception = tvp->u.data;
1811 case DTV_ISDBT_SOUND_BROADCASTING:
1812 c->isdbt_sb_mode = tvp->u.data;
1814 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1815 c->isdbt_sb_subchannel = tvp->u.data;
1817 case DTV_ISDBT_SB_SEGMENT_IDX:
1818 c->isdbt_sb_segment_idx = tvp->u.data;
1820 case DTV_ISDBT_SB_SEGMENT_COUNT:
1821 c->isdbt_sb_segment_count = tvp->u.data;
1823 case DTV_ISDBT_LAYER_ENABLED:
1824 c->isdbt_layer_enabled = tvp->u.data;
1826 case DTV_ISDBT_LAYERA_FEC:
1827 c->layer[0].fec = tvp->u.data;
1829 case DTV_ISDBT_LAYERA_MODULATION:
1830 c->layer[0].modulation = tvp->u.data;
1832 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1833 c->layer[0].segment_count = tvp->u.data;
1835 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1836 c->layer[0].interleaving = tvp->u.data;
1838 case DTV_ISDBT_LAYERB_FEC:
1839 c->layer[1].fec = tvp->u.data;
1841 case DTV_ISDBT_LAYERB_MODULATION:
1842 c->layer[1].modulation = tvp->u.data;
1844 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1845 c->layer[1].segment_count = tvp->u.data;
1847 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1848 c->layer[1].interleaving = tvp->u.data;
1850 case DTV_ISDBT_LAYERC_FEC:
1851 c->layer[2].fec = tvp->u.data;
1853 case DTV_ISDBT_LAYERC_MODULATION:
1854 c->layer[2].modulation = tvp->u.data;
1856 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1857 c->layer[2].segment_count = tvp->u.data;
1859 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1860 c->layer[2].interleaving = tvp->u.data;
1863 /* Multistream support */
1865 case DTV_DVBT2_PLP_ID_LEGACY:
1866 c->stream_id = tvp->u.data;
1870 case DTV_ATSCMH_PARADE_ID:
1871 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1873 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1874 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1878 c->lna = tvp->u.data;
1879 if (fe->ops.set_lna)
1880 r = fe->ops.set_lna(fe);
1892 static int dvb_frontend_ioctl(struct file *file,
1893 unsigned int cmd, void *parg)
1895 struct dvb_device *dvbdev = file->private_data;
1896 struct dvb_frontend *fe = dvbdev->priv;
1897 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1898 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1899 int err = -EOPNOTSUPP;
1901 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
1902 if (down_interruptible(&fepriv->sem))
1903 return -ERESTARTSYS;
1905 if (fe->exit != DVB_FE_NO_EXIT) {
1910 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1911 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1912 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1917 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1918 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1920 c->state = DTV_UNDEFINED;
1921 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1928 static int dvb_frontend_ioctl_properties(struct file *file,
1929 unsigned int cmd, void *parg)
1931 struct dvb_device *dvbdev = file->private_data;
1932 struct dvb_frontend *fe = dvbdev->priv;
1933 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1934 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1937 struct dtv_properties *tvps = parg;
1938 struct dtv_property *tvp = NULL;
1941 dev_dbg(fe->dvb->device, "%s:\n", __func__);
1943 if (cmd == FE_SET_PROPERTY) {
1944 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1945 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
1947 /* Put an arbitrary limit on the number of messages that can
1948 * be sent at once */
1949 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1952 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1958 if (copy_from_user(tvp, (void __user *)tvps->props,
1959 tvps->num * sizeof(struct dtv_property))) {
1964 for (i = 0; i < tvps->num; i++) {
1965 err = dtv_property_process_set(fe, tvp + i, file);
1968 (tvp + i)->result = err;
1971 if (c->state == DTV_TUNE)
1972 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
1974 } else if (cmd == FE_GET_PROPERTY) {
1975 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1976 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
1978 /* Put an arbitrary limit on the number of messages that can
1979 * be sent at once */
1980 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1983 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1989 if (copy_from_user(tvp, (void __user *)tvps->props,
1990 tvps->num * sizeof(struct dtv_property))) {
1996 * Fills the cache out struct with the cache contents, plus
1997 * the data retrieved from get_frontend, if the frontend
1998 * is not idle. Otherwise, returns the cached content
2000 if (fepriv->state != FESTATE_IDLE) {
2001 err = dtv_get_frontend(fe, NULL);
2005 for (i = 0; i < tvps->num; i++) {
2006 err = dtv_property_process_get(fe, c, tvp + i, file);
2009 (tvp + i)->result = err;
2012 if (copy_to_user((void __user *)tvps->props, tvp,
2013 tvps->num * sizeof(struct dtv_property))) {
2026 static int dtv_set_frontend(struct dvb_frontend *fe)
2028 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2029 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2030 struct dvb_frontend_tune_settings fetunesettings;
2033 if (dvb_frontend_check_parameters(fe) < 0)
2037 * Initialize output parameters to match the values given by
2038 * the user. FE_SET_FRONTEND triggers an initial frontend event
2039 * with status = 0, which copies output parameters to userspace.
2041 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2044 * Be sure that the bandwidth will be filled for all
2045 * non-satellite systems, as tuners need to know what
2046 * low pass/Nyquist half filter should be applied, in
2047 * order to avoid inter-channel noise.
2049 * ISDB-T and DVB-T/T2 already sets bandwidth.
2050 * ATSC and DVB-C don't set, so, the core should fill it.
2052 * On DVB-C Annex A and C, the bandwidth is a function of
2053 * the roll-off and symbol rate. Annex B defines different
2054 * roll-off factors depending on the modulation. Fortunately,
2055 * Annex B is only used with 6MHz, so there's no need to
2058 * While not officially supported, a side effect of handling it at
2059 * the cache level is that a program could retrieve the bandwidth
2060 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2062 switch (c->delivery_system) {
2064 case SYS_DVBC_ANNEX_B:
2065 c->bandwidth_hz = 6000000;
2067 case SYS_DVBC_ANNEX_A:
2070 case SYS_DVBC_ANNEX_C:
2078 switch (c->rolloff) {
2094 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
2096 /* force auto frequency inversion if requested */
2097 if (dvb_force_auto_inversion)
2098 c->inversion = INVERSION_AUTO;
2101 * without hierarchical coding code_rate_LP is irrelevant,
2102 * so we tolerate the otherwise invalid FEC_NONE setting
2104 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2105 c->code_rate_LP = FEC_AUTO;
2107 /* get frontend-specific tuning settings */
2108 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2109 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2110 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2111 fepriv->max_drift = fetunesettings.max_drift;
2112 fepriv->step_size = fetunesettings.step_size;
2114 /* default values */
2115 switch (c->delivery_system) {
2120 case SYS_DVBC_ANNEX_A:
2121 case SYS_DVBC_ANNEX_C:
2122 fepriv->min_delay = HZ / 20;
2123 fepriv->step_size = c->symbol_rate / 16000;
2124 fepriv->max_drift = c->symbol_rate / 2000;
2130 fepriv->min_delay = HZ / 20;
2131 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2132 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2136 * FIXME: This sounds wrong! if freqency_stepsize is
2137 * defined by the frontend, why not use it???
2139 fepriv->min_delay = HZ / 20;
2140 fepriv->step_size = 0; /* no zigzag */
2141 fepriv->max_drift = 0;
2145 if (dvb_override_tune_delay > 0)
2146 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2148 fepriv->state = FESTATE_RETUNE;
2150 /* Request the search algorithm to search */
2151 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2153 dvb_frontend_clear_events(fe);
2154 dvb_frontend_add_event(fe, 0);
2155 dvb_frontend_wakeup(fe);
2162 static int dvb_frontend_ioctl_legacy(struct file *file,
2163 unsigned int cmd, void *parg)
2165 struct dvb_device *dvbdev = file->private_data;
2166 struct dvb_frontend *fe = dvbdev->priv;
2167 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2168 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2169 int err = -EOPNOTSUPP;
2173 struct dvb_frontend_info* info = parg;
2175 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2176 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2179 * Associate the 4 delivery systems supported by DVBv3
2180 * API with their DVBv5 counterpart. For the other standards,
2181 * use the closest type, assuming that it would hopefully
2182 * work with a DVBv3 application.
2183 * It should be noticed that, on multi-frontend devices with
2184 * different types (terrestrial and cable, for example),
2185 * a pure DVBv3 application won't be able to use all delivery
2186 * systems. Yet, changing the DVBv5 cache to the other delivery
2187 * system should be enough for making it work.
2189 switch (dvbv3_type(c->delivery_system)) {
2191 info->type = FE_QPSK;
2194 info->type = FE_ATSC;
2197 info->type = FE_QAM;
2200 info->type = FE_OFDM;
2203 dev_err(fe->dvb->device,
2204 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2205 __func__, c->delivery_system);
2206 fe->ops.info.type = FE_OFDM;
2208 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2209 __func__, c->delivery_system, fe->ops.info.type);
2211 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2212 * do it, it is done for it. */
2213 info->caps |= FE_CAN_INVERSION_AUTO;
2218 case FE_READ_STATUS: {
2219 fe_status_t* status = parg;
2221 /* if retune was requested but hasn't occurred yet, prevent
2222 * that user get signal state from previous tuning */
2223 if (fepriv->state == FESTATE_RETUNE ||
2224 fepriv->state == FESTATE_ERROR) {
2230 if (fe->ops.read_status)
2231 err = fe->ops.read_status(fe, status);
2236 if (fe->ops.read_ber) {
2238 err = fe->ops.read_ber(fe, (__u32 *) parg);
2244 case FE_READ_SIGNAL_STRENGTH:
2245 if (fe->ops.read_signal_strength) {
2247 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2254 if (fe->ops.read_snr) {
2256 err = fe->ops.read_snr(fe, (__u16 *) parg);
2262 case FE_READ_UNCORRECTED_BLOCKS:
2263 if (fe->ops.read_ucblocks) {
2265 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2271 case FE_DISEQC_RESET_OVERLOAD:
2272 if (fe->ops.diseqc_reset_overload) {
2273 err = fe->ops.diseqc_reset_overload(fe);
2274 fepriv->state = FESTATE_DISEQC;
2279 case FE_DISEQC_SEND_MASTER_CMD:
2280 if (fe->ops.diseqc_send_master_cmd) {
2281 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2282 fepriv->state = FESTATE_DISEQC;
2287 case FE_DISEQC_SEND_BURST:
2288 if (fe->ops.diseqc_send_burst) {
2289 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2290 fepriv->state = FESTATE_DISEQC;
2296 if (fe->ops.set_tone) {
2297 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2298 fepriv->tone = (fe_sec_tone_mode_t) parg;
2299 fepriv->state = FESTATE_DISEQC;
2304 case FE_SET_VOLTAGE:
2305 if (fe->ops.set_voltage) {
2306 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2307 fepriv->voltage = (fe_sec_voltage_t) parg;
2308 fepriv->state = FESTATE_DISEQC;
2313 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2314 if (fe->ops.dishnetwork_send_legacy_command) {
2315 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2316 fepriv->state = FESTATE_DISEQC;
2318 } else if (fe->ops.set_voltage) {
2320 * NOTE: This is a fallback condition. Some frontends
2321 * (stv0299 for instance) take longer than 8msec to
2322 * respond to a set_voltage command. Those switches
2323 * need custom routines to switch properly. For all
2324 * other frontends, the following should work ok.
2325 * Dish network legacy switches (as used by Dish500)
2326 * are controlled by sending 9-bit command words
2327 * spaced 8msec apart.
2328 * the actual command word is switch/port dependent
2329 * so it is up to the userspace application to send
2330 * the right command.
2331 * The command must always start with a '0' after
2332 * initialization, so parg is 8 bits and does not
2333 * include the initialization or start bit
2335 unsigned long swcmd = ((unsigned long) parg) << 1;
2336 struct timeval nexttime;
2337 struct timeval tv[10];
2340 if (dvb_frontend_debug)
2341 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2342 do_gettimeofday(&nexttime);
2343 if (dvb_frontend_debug)
2345 /* before sending a command, initialize by sending
2346 * a 32ms 18V to the switch
2348 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2349 dvb_frontend_sleep_until(&nexttime, 32000);
2351 for (i = 0; i < 9; i++) {
2352 if (dvb_frontend_debug)
2353 do_gettimeofday(&tv[i + 1]);
2354 if ((swcmd & 0x01) != last) {
2355 /* set voltage to (last ? 13V : 18V) */
2356 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2357 last = (last) ? 0 : 1;
2361 dvb_frontend_sleep_until(&nexttime, 8000);
2363 if (dvb_frontend_debug) {
2364 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2365 __func__, fe->dvb->num);
2366 for (i = 1; i < 10; i++)
2367 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2370 fepriv->state = FESTATE_DISEQC;
2375 case FE_DISEQC_RECV_SLAVE_REPLY:
2376 if (fe->ops.diseqc_recv_slave_reply)
2377 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2380 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2381 if (fe->ops.enable_high_lnb_voltage)
2382 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2385 case FE_SET_FRONTEND:
2386 err = dvbv3_set_delivery_system(fe);
2390 err = dtv_property_cache_sync(fe, c, parg);
2393 err = dtv_set_frontend(fe);
2396 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2399 case FE_GET_FRONTEND:
2400 err = dtv_get_frontend(fe, parg);
2403 case FE_SET_FRONTEND_TUNE_MODE:
2404 fepriv->tune_mode_flags = (unsigned long) parg;
2413 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2415 struct dvb_device *dvbdev = file->private_data;
2416 struct dvb_frontend *fe = dvbdev->priv;
2417 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2419 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2421 poll_wait (file, &fepriv->events.wait_queue, wait);
2423 if (fepriv->events.eventw != fepriv->events.eventr)
2424 return (POLLIN | POLLRDNORM | POLLPRI);
2429 static int dvb_frontend_open(struct inode *inode, struct file *file)
2431 struct dvb_device *dvbdev = file->private_data;
2432 struct dvb_frontend *fe = dvbdev->priv;
2433 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2434 struct dvb_adapter *adapter = fe->dvb;
2437 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2438 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2441 if (adapter->mfe_shared) {
2442 mutex_lock (&adapter->mfe_lock);
2444 if (adapter->mfe_dvbdev == NULL)
2445 adapter->mfe_dvbdev = dvbdev;
2447 else if (adapter->mfe_dvbdev != dvbdev) {
2449 *mfedev = adapter->mfe_dvbdev;
2451 *mfe = mfedev->priv;
2452 struct dvb_frontend_private
2453 *mfepriv = mfe->frontend_priv;
2454 int mferetry = (dvb_mfe_wait_time << 1);
2456 mutex_unlock (&adapter->mfe_lock);
2457 while (mferetry-- && (mfedev->users != -1 ||
2458 mfepriv->thread != NULL)) {
2459 if(msleep_interruptible(500)) {
2460 if(signal_pending(current))
2465 mutex_lock (&adapter->mfe_lock);
2466 if(adapter->mfe_dvbdev != dvbdev) {
2467 mfedev = adapter->mfe_dvbdev;
2469 mfepriv = mfe->frontend_priv;
2470 if (mfedev->users != -1 ||
2471 mfepriv->thread != NULL) {
2472 mutex_unlock (&adapter->mfe_lock);
2475 adapter->mfe_dvbdev = dvbdev;
2480 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2481 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2484 /* If we took control of the bus, we need to force
2485 reinitialization. This is because many ts_bus_ctrl()
2486 functions strobe the RESET pin on the demod, and if the
2487 frontend thread already exists then the dvb_init() routine
2488 won't get called (which is what usually does initial
2489 register configuration). */
2490 fepriv->reinitialise = 1;
2493 if ((ret = dvb_generic_open (inode, file)) < 0)
2496 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2497 /* normal tune mode when opened R/W */
2498 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2500 fepriv->voltage = -1;
2502 ret = dvb_frontend_start (fe);
2506 /* empty event queue */
2507 fepriv->events.eventr = fepriv->events.eventw = 0;
2510 if (adapter->mfe_shared)
2511 mutex_unlock (&adapter->mfe_lock);
2515 dvb_generic_release(inode, file);
2517 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2518 fe->ops.ts_bus_ctrl(fe, 0);
2520 if (adapter->mfe_shared)
2521 mutex_unlock (&adapter->mfe_lock);
2525 static int dvb_frontend_release(struct inode *inode, struct file *file)
2527 struct dvb_device *dvbdev = file->private_data;
2528 struct dvb_frontend *fe = dvbdev->priv;
2529 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2532 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2534 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2535 fepriv->release_jiffies = jiffies;
2539 ret = dvb_generic_release (inode, file);
2541 if (dvbdev->users == -1) {
2542 wake_up(&fepriv->wait_queue);
2543 if (fe->exit != DVB_FE_NO_EXIT)
2544 wake_up(&dvbdev->wait_queue);
2545 if (fe->ops.ts_bus_ctrl)
2546 fe->ops.ts_bus_ctrl(fe, 0);
2552 static const struct file_operations dvb_frontend_fops = {
2553 .owner = THIS_MODULE,
2554 .unlocked_ioctl = dvb_generic_ioctl,
2555 .poll = dvb_frontend_poll,
2556 .open = dvb_frontend_open,
2557 .release = dvb_frontend_release,
2558 .llseek = noop_llseek,
2561 int dvb_frontend_suspend(struct dvb_frontend *fe)
2565 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2568 if (fe->ops.tuner_ops.suspend)
2569 ret = fe->ops.tuner_ops.suspend(fe);
2570 else if (fe->ops.tuner_ops.sleep)
2571 ret = fe->ops.tuner_ops.sleep(fe);
2574 ret = fe->ops.sleep(fe);
2578 EXPORT_SYMBOL(dvb_frontend_suspend);
2580 int dvb_frontend_resume(struct dvb_frontend *fe)
2582 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2585 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2588 fe->exit = DVB_FE_DEVICE_RESUME;
2590 ret = fe->ops.init(fe);
2592 if (fe->ops.tuner_ops.resume)
2593 ret = fe->ops.tuner_ops.resume(fe);
2594 else if (fe->ops.tuner_ops.init)
2595 ret = fe->ops.tuner_ops.init(fe);
2597 fe->exit = DVB_FE_NO_EXIT;
2598 fepriv->state = FESTATE_RETUNE;
2599 dvb_frontend_wakeup(fe);
2603 EXPORT_SYMBOL(dvb_frontend_resume);
2605 int dvb_register_frontend(struct dvb_adapter* dvb,
2606 struct dvb_frontend* fe)
2608 struct dvb_frontend_private *fepriv;
2609 static const struct dvb_device dvbdev_template = {
2613 .fops = &dvb_frontend_fops,
2614 .kernel_ioctl = dvb_frontend_ioctl
2617 dev_dbg(dvb->device, "%s:\n", __func__);
2619 if (mutex_lock_interruptible(&frontend_mutex))
2620 return -ERESTARTSYS;
2622 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2623 if (fe->frontend_priv == NULL) {
2624 mutex_unlock(&frontend_mutex);
2627 fepriv = fe->frontend_priv;
2629 sema_init(&fepriv->sem, 1);
2630 init_waitqueue_head (&fepriv->wait_queue);
2631 init_waitqueue_head (&fepriv->events.wait_queue);
2632 mutex_init(&fepriv->events.mtx);
2634 fepriv->inversion = INVERSION_OFF;
2636 dev_info(fe->dvb->device,
2637 "DVB: registering adapter %i frontend %i (%s)...\n",
2638 fe->dvb->num, fe->id, fe->ops.info.name);
2640 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2641 fe, DVB_DEVICE_FRONTEND);
2644 * Initialize the cache to the proper values according with the
2645 * first supported delivery system (ops->delsys[0])
2648 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2649 dvb_frontend_clear_cache(fe);
2651 mutex_unlock(&frontend_mutex);
2654 EXPORT_SYMBOL(dvb_register_frontend);
2656 int dvb_unregister_frontend(struct dvb_frontend* fe)
2658 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2659 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2661 mutex_lock(&frontend_mutex);
2662 dvb_frontend_stop (fe);
2663 mutex_unlock(&frontend_mutex);
2665 if (fepriv->dvbdev->users < -1)
2666 wait_event(fepriv->dvbdev->wait_queue,
2667 fepriv->dvbdev->users==-1);
2669 mutex_lock(&frontend_mutex);
2670 dvb_unregister_device (fepriv->dvbdev);
2672 /* fe is invalid now */
2674 mutex_unlock(&frontend_mutex);
2677 EXPORT_SYMBOL(dvb_unregister_frontend);
2679 #ifdef CONFIG_MEDIA_ATTACH
2680 void dvb_frontend_detach(struct dvb_frontend* fe)
2684 if (fe->ops.release_sec) {
2685 fe->ops.release_sec(fe);
2686 dvb_detach(fe->ops.release_sec);
2688 if (fe->ops.tuner_ops.release) {
2689 fe->ops.tuner_ops.release(fe);
2690 dvb_detach(fe->ops.tuner_ops.release);
2692 if (fe->ops.analog_ops.release) {
2693 fe->ops.analog_ops.release(fe);
2694 dvb_detach(fe->ops.analog_ops.release);
2696 ptr = (void*)fe->ops.release;
2698 fe->ops.release(fe);
2703 void dvb_frontend_detach(struct dvb_frontend* fe)
2705 if (fe->ops.release_sec)
2706 fe->ops.release_sec(fe);
2707 if (fe->ops.tuner_ops.release)
2708 fe->ops.tuner_ops.release(fe);
2709 if (fe->ops.analog_ops.release)
2710 fe->ops.analog_ops.release(fe);
2711 if (fe->ops.release)
2712 fe->ops.release(fe);
2715 EXPORT_SYMBOL(dvb_frontend_detach);