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);
594 * dvb_enable_media_tuner() - tries to enable the DVB tuner
596 * @fe: struct dvb_frontend pointer
598 * This function ensures that just one media tuner is enabled for a given
599 * frontend. It has two different behaviors:
600 * - For trivial devices with just one tuner:
601 * it just enables the existing tuner->fe link
602 * - For devices with more than one tuner:
603 * It is up to the driver to implement the logic that will enable one tuner
604 * and disable the other ones. However, if more than one tuner is enabled for
605 * the same frontend, it will print an error message and return -EINVAL.
607 * At return, it will return the error code returned by media_entity_setup_link,
608 * or 0 if everything is OK, if no tuner is linked to the frontend or if the
611 static int dvb_enable_media_tuner(struct dvb_frontend *fe)
613 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
614 struct dvb_frontend_private *fepriv = fe->frontend_priv;
615 struct dvb_adapter *adapter = fe->dvb;
616 struct media_device *mdev = adapter->mdev;
617 struct media_entity *entity, *source;
618 struct media_link *link, *found_link = NULL;
619 int i, ret, n_links = 0, active_links = 0;
624 entity = fepriv->dvbdev->entity;
625 for (i = 0; i < entity->num_links; i++) {
626 link = &entity->links[i];
627 if (link->sink->entity == entity) {
630 if (link->flags & MEDIA_LNK_FL_ENABLED)
635 if (!n_links || active_links == 1 || !found_link)
639 * If a frontend has more than one tuner linked, it is up to the driver
640 * to select with one will be the active one, as the frontend core can't
641 * guess. If the driver doesn't do that, it is a bug.
643 if (n_links > 1 && active_links != 1) {
644 dev_err(fe->dvb->device,
645 "WARNING: there are %d active links among %d tuners. This is a driver's bug!\n",
646 active_links, n_links);
650 source = found_link->source->entity;
651 for (i = 0; i < source->num_links; i++) {
652 struct media_entity *sink;
655 link = &source->links[i];
656 sink = link->sink->entity;
659 flags = MEDIA_LNK_FL_ENABLED;
661 ret = media_entity_setup_link(link, flags);
663 dev_err(fe->dvb->device,
664 "Couldn't change link %s->%s to %s. Error %d\n",
665 source->name, sink->name,
666 flags ? "enabled" : "disabled",
670 dev_dbg(fe->dvb->device,
671 "link %s->%s was %s\n",
672 source->name, sink->name,
673 flags ? "ENABLED" : "disabled");
679 static int dvb_frontend_thread(void *data)
681 struct dvb_frontend *fe = data;
682 struct dvb_frontend_private *fepriv = fe->frontend_priv;
684 enum dvbfe_algo algo;
687 bool re_tune = false;
688 bool semheld = false;
690 dev_dbg(fe->dvb->device, "%s:\n", __func__);
692 fepriv->check_wrapped = 0;
694 fepriv->delay = 3*HZ;
697 fepriv->reinitialise = 0;
699 ret = dvb_enable_media_tuner(fe);
701 /* FIXME: return an error if it fails */
702 dev_info(fe->dvb->device,
703 "proceeding with FE task\n");
706 dvb_frontend_init(fe);
710 up(&fepriv->sem); /* is locked when we enter the thread... */
712 wait_event_interruptible_timeout(fepriv->wait_queue,
713 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
714 || freezing(current),
717 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
718 /* got signal or quitting */
719 if (!down_interruptible(&fepriv->sem))
721 fe->exit = DVB_FE_NORMAL_EXIT;
728 if (down_interruptible(&fepriv->sem))
731 if (fepriv->reinitialise) {
732 dvb_frontend_init(fe);
733 if (fe->ops.set_tone && fepriv->tone != -1)
734 fe->ops.set_tone(fe, fepriv->tone);
735 if (fe->ops.set_voltage && fepriv->voltage != -1)
736 fe->ops.set_voltage(fe, fepriv->voltage);
737 fepriv->reinitialise = 0;
740 /* do an iteration of the tuning loop */
741 if (fe->ops.get_frontend_algo) {
742 algo = fe->ops.get_frontend_algo(fe);
745 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
747 if (fepriv->state & FESTATE_RETUNE) {
748 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
750 fepriv->state = FESTATE_TUNED;
756 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
758 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
759 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
760 dvb_frontend_add_event(fe, s);
765 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
766 dvb_frontend_swzigzag(fe);
768 case DVBFE_ALGO_CUSTOM:
769 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
770 if (fepriv->state & FESTATE_RETUNE) {
771 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
772 fepriv->state = FESTATE_TUNED;
774 /* Case where we are going to search for a carrier
775 * User asked us to retune again for some reason, possibly
776 * requesting a search with a new set of parameters
778 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
779 if (fe->ops.search) {
780 fepriv->algo_status = fe->ops.search(fe);
781 /* We did do a search as was requested, the flags are
782 * now unset as well and has the flags wrt to search.
785 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
788 /* Track the carrier if the search was successful */
789 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
790 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
791 fepriv->delay = HZ / 2;
793 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
794 fe->ops.read_status(fe, &s);
795 if (s != fepriv->status) {
796 dvb_frontend_add_event(fe, s); /* update event list */
798 if (!(s & FE_HAS_LOCK)) {
799 fepriv->delay = HZ / 10;
800 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
802 fepriv->delay = 60 * HZ;
807 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
811 dvb_frontend_swzigzag(fe);
815 if (dvb_powerdown_on_sleep) {
816 if (fe->ops.set_voltage)
817 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
818 if (fe->ops.tuner_ops.sleep) {
819 if (fe->ops.i2c_gate_ctrl)
820 fe->ops.i2c_gate_ctrl(fe, 1);
821 fe->ops.tuner_ops.sleep(fe);
822 if (fe->ops.i2c_gate_ctrl)
823 fe->ops.i2c_gate_ctrl(fe, 0);
829 fepriv->thread = NULL;
830 if (kthread_should_stop())
831 fe->exit = DVB_FE_DEVICE_REMOVED;
833 fe->exit = DVB_FE_NO_EXIT;
838 dvb_frontend_wakeup(fe);
842 static void dvb_frontend_stop(struct dvb_frontend *fe)
844 struct dvb_frontend_private *fepriv = fe->frontend_priv;
846 dev_dbg(fe->dvb->device, "%s:\n", __func__);
848 if (fe->exit != DVB_FE_DEVICE_REMOVED)
849 fe->exit = DVB_FE_NORMAL_EXIT;
855 kthread_stop(fepriv->thread);
857 sema_init(&fepriv->sem, 1);
858 fepriv->state = FESTATE_IDLE;
860 /* paranoia check in case a signal arrived */
862 dev_warn(fe->dvb->device,
863 "dvb_frontend_stop: warning: thread %p won't exit\n",
867 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
869 return ((curtime.tv_usec < lasttime.tv_usec) ?
870 1000000 - lasttime.tv_usec + curtime.tv_usec :
871 curtime.tv_usec - lasttime.tv_usec);
873 EXPORT_SYMBOL(timeval_usec_diff);
875 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
877 curtime->tv_usec += add_usec;
878 if (curtime->tv_usec >= 1000000) {
879 curtime->tv_usec -= 1000000;
885 * Sleep until gettimeofday() > waketime + add_usec
886 * This needs to be as precise as possible, but as the delay is
887 * usually between 2ms and 32ms, it is done using a scheduled msleep
888 * followed by usleep (normally a busy-wait loop) for the remainder
890 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
892 struct timeval lasttime;
895 timeval_usec_add(waketime, add_usec);
897 do_gettimeofday(&lasttime);
898 delta = timeval_usec_diff(lasttime, *waketime);
900 msleep((delta - 1500) / 1000);
901 do_gettimeofday(&lasttime);
902 newdelta = timeval_usec_diff(lasttime, *waketime);
903 delta = (newdelta > delta) ? 0 : newdelta;
908 EXPORT_SYMBOL(dvb_frontend_sleep_until);
910 static int dvb_frontend_start(struct dvb_frontend *fe)
913 struct dvb_frontend_private *fepriv = fe->frontend_priv;
914 struct task_struct *fe_thread;
916 dev_dbg(fe->dvb->device, "%s:\n", __func__);
918 if (fepriv->thread) {
919 if (fe->exit == DVB_FE_NO_EXIT)
922 dvb_frontend_stop (fe);
925 if (signal_pending(current))
927 if (down_interruptible (&fepriv->sem))
930 fepriv->state = FESTATE_IDLE;
931 fe->exit = DVB_FE_NO_EXIT;
932 fepriv->thread = NULL;
935 fe_thread = kthread_run(dvb_frontend_thread, fe,
936 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
937 if (IS_ERR(fe_thread)) {
938 ret = PTR_ERR(fe_thread);
939 dev_warn(fe->dvb->device,
940 "dvb_frontend_start: failed to start kthread (%d)\n",
945 fepriv->thread = fe_thread;
949 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
950 u32 *freq_min, u32 *freq_max)
952 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
954 if (fe->ops.info.frequency_max == 0)
955 *freq_max = fe->ops.tuner_ops.info.frequency_max;
956 else if (fe->ops.tuner_ops.info.frequency_max == 0)
957 *freq_max = fe->ops.info.frequency_max;
959 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
961 if (*freq_min == 0 || *freq_max == 0)
962 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
963 fe->dvb->num, fe->id);
966 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
968 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
972 /* range check: frequency */
973 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
974 if ((freq_min && c->frequency < freq_min) ||
975 (freq_max && c->frequency > freq_max)) {
976 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
977 fe->dvb->num, fe->id, c->frequency,
982 /* range check: symbol rate */
983 switch (c->delivery_system) {
987 case SYS_DVBC_ANNEX_A:
988 case SYS_DVBC_ANNEX_C:
989 if ((fe->ops.info.symbol_rate_min &&
990 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
991 (fe->ops.info.symbol_rate_max &&
992 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
993 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
994 fe->dvb->num, fe->id, c->symbol_rate,
995 fe->ops.info.symbol_rate_min,
996 fe->ops.info.symbol_rate_max);
1006 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
1008 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1012 delsys = c->delivery_system;
1013 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
1014 c->delivery_system = delsys;
1016 c->state = DTV_CLEAR;
1018 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
1019 __func__, c->delivery_system);
1021 c->transmission_mode = TRANSMISSION_MODE_AUTO;
1022 c->bandwidth_hz = 0; /* AUTO */
1023 c->guard_interval = GUARD_INTERVAL_AUTO;
1024 c->hierarchy = HIERARCHY_AUTO;
1026 c->code_rate_HP = FEC_AUTO;
1027 c->code_rate_LP = FEC_AUTO;
1028 c->fec_inner = FEC_AUTO;
1029 c->rolloff = ROLLOFF_AUTO;
1030 c->voltage = SEC_VOLTAGE_OFF;
1031 c->sectone = SEC_TONE_OFF;
1032 c->pilot = PILOT_AUTO;
1034 c->isdbt_partial_reception = 0;
1035 c->isdbt_sb_mode = 0;
1036 c->isdbt_sb_subchannel = 0;
1037 c->isdbt_sb_segment_idx = 0;
1038 c->isdbt_sb_segment_count = 0;
1039 c->isdbt_layer_enabled = 0;
1040 for (i = 0; i < 3; i++) {
1041 c->layer[i].fec = FEC_AUTO;
1042 c->layer[i].modulation = QAM_AUTO;
1043 c->layer[i].interleaving = 0;
1044 c->layer[i].segment_count = 0;
1047 c->stream_id = NO_STREAM_ID_FILTER;
1049 switch (c->delivery_system) {
1053 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1054 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1057 c->modulation = VSB_8;
1060 c->symbol_rate = 28860000;
1061 c->rolloff = ROLLOFF_35;
1062 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1065 c->modulation = QAM_AUTO;
1074 #define _DTV_CMD(n, s, b) \
1082 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1083 _DTV_CMD(DTV_TUNE, 1, 0),
1084 _DTV_CMD(DTV_CLEAR, 1, 0),
1087 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1088 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1089 _DTV_CMD(DTV_MODULATION, 1, 0),
1090 _DTV_CMD(DTV_INVERSION, 1, 0),
1091 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1092 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1093 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1094 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1095 _DTV_CMD(DTV_TONE, 1, 0),
1096 _DTV_CMD(DTV_PILOT, 1, 0),
1097 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1098 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1099 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1100 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1101 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1102 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1103 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1104 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1106 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1107 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1108 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1109 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1110 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1111 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1112 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1113 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1114 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1115 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1116 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1117 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1118 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1119 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1120 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1121 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1122 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1123 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1125 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1126 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1127 _DTV_CMD(DTV_LNA, 1, 0),
1130 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1131 _DTV_CMD(DTV_API_VERSION, 0, 0),
1133 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1135 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1136 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1138 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1139 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1140 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1141 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1142 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1143 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1144 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1145 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1146 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1147 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1148 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1149 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1150 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1152 /* Statistics API */
1153 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1154 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1155 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1156 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1157 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1158 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1159 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1160 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1163 static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
1167 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1168 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1169 __func__, tvp->cmd);
1173 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1174 tvp->cmd, dtv_cmds[tvp->cmd].name);
1176 if (dtv_cmds[tvp->cmd].buffer) {
1177 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1178 __func__, tvp->u.buffer.len);
1180 for(i = 0; i < tvp->u.buffer.len; i++)
1181 dev_dbg(fe->dvb->device,
1182 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1183 __func__, i, tvp->u.buffer.data[i]);
1185 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1190 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1191 * drivers can use a single set_frontend tuning function, regardless of whether
1192 * it's being used for the legacy or new API, reducing code and complexity.
1194 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1195 struct dtv_frontend_properties *c,
1196 const struct dvb_frontend_parameters *p)
1198 c->frequency = p->frequency;
1199 c->inversion = p->inversion;
1201 switch (dvbv3_type(c->delivery_system)) {
1203 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1204 c->symbol_rate = p->u.qpsk.symbol_rate;
1205 c->fec_inner = p->u.qpsk.fec_inner;
1208 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1209 c->symbol_rate = p->u.qam.symbol_rate;
1210 c->fec_inner = p->u.qam.fec_inner;
1211 c->modulation = p->u.qam.modulation;
1214 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1216 switch (p->u.ofdm.bandwidth) {
1217 case BANDWIDTH_10_MHZ:
1218 c->bandwidth_hz = 10000000;
1220 case BANDWIDTH_8_MHZ:
1221 c->bandwidth_hz = 8000000;
1223 case BANDWIDTH_7_MHZ:
1224 c->bandwidth_hz = 7000000;
1226 case BANDWIDTH_6_MHZ:
1227 c->bandwidth_hz = 6000000;
1229 case BANDWIDTH_5_MHZ:
1230 c->bandwidth_hz = 5000000;
1232 case BANDWIDTH_1_712_MHZ:
1233 c->bandwidth_hz = 1712000;
1235 case BANDWIDTH_AUTO:
1236 c->bandwidth_hz = 0;
1239 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1240 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1241 c->modulation = p->u.ofdm.constellation;
1242 c->transmission_mode = p->u.ofdm.transmission_mode;
1243 c->guard_interval = p->u.ofdm.guard_interval;
1244 c->hierarchy = p->u.ofdm.hierarchy_information;
1247 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1248 c->modulation = p->u.vsb.modulation;
1249 if (c->delivery_system == SYS_ATSCMH)
1251 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1252 c->delivery_system = SYS_ATSC;
1254 c->delivery_system = SYS_DVBC_ANNEX_B;
1257 dev_err(fe->dvb->device,
1258 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1259 __func__, c->delivery_system);
1266 /* Ensure the cached values are set correctly in the frontend
1267 * legacy tuning structures, for the advanced tuning API.
1269 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1270 struct dvb_frontend_parameters *p)
1272 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1274 p->frequency = c->frequency;
1275 p->inversion = c->inversion;
1277 switch (dvbv3_type(c->delivery_system)) {
1279 dev_err(fe->dvb->device,
1280 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1281 __func__, c->delivery_system);
1284 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1285 p->u.qpsk.symbol_rate = c->symbol_rate;
1286 p->u.qpsk.fec_inner = c->fec_inner;
1289 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1290 p->u.qam.symbol_rate = c->symbol_rate;
1291 p->u.qam.fec_inner = c->fec_inner;
1292 p->u.qam.modulation = c->modulation;
1295 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1296 switch (c->bandwidth_hz) {
1298 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1301 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1304 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1307 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1310 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1313 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1317 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1319 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1320 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1321 p->u.ofdm.constellation = c->modulation;
1322 p->u.ofdm.transmission_mode = c->transmission_mode;
1323 p->u.ofdm.guard_interval = c->guard_interval;
1324 p->u.ofdm.hierarchy_information = c->hierarchy;
1327 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1328 p->u.vsb.modulation = c->modulation;
1335 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1336 * @fe: struct dvb_frontend pointer
1337 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1338 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1340 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1341 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1342 * If p_out is not null, it will update the DVBv3 params pointed by it.
1344 static int dtv_get_frontend(struct dvb_frontend *fe,
1345 struct dvb_frontend_parameters *p_out)
1349 if (fe->ops.get_frontend) {
1350 r = fe->ops.get_frontend(fe);
1351 if (unlikely(r < 0))
1354 dtv_property_legacy_params_sync(fe, p_out);
1358 /* As everything is in cache, get_frontend fops are always supported */
1362 static int dvb_frontend_ioctl_legacy(struct file *file,
1363 unsigned int cmd, void *parg);
1364 static int dvb_frontend_ioctl_properties(struct file *file,
1365 unsigned int cmd, void *parg);
1367 static int dtv_property_process_get(struct dvb_frontend *fe,
1368 const struct dtv_frontend_properties *c,
1369 struct dtv_property *tvp,
1375 case DTV_ENUM_DELSYS:
1377 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1378 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1381 tvp->u.buffer.len = ncaps;
1384 tvp->u.data = c->frequency;
1386 case DTV_MODULATION:
1387 tvp->u.data = c->modulation;
1389 case DTV_BANDWIDTH_HZ:
1390 tvp->u.data = c->bandwidth_hz;
1393 tvp->u.data = c->inversion;
1395 case DTV_SYMBOL_RATE:
1396 tvp->u.data = c->symbol_rate;
1399 tvp->u.data = c->fec_inner;
1402 tvp->u.data = c->pilot;
1405 tvp->u.data = c->rolloff;
1407 case DTV_DELIVERY_SYSTEM:
1408 tvp->u.data = c->delivery_system;
1411 tvp->u.data = c->voltage;
1414 tvp->u.data = c->sectone;
1416 case DTV_API_VERSION:
1417 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1419 case DTV_CODE_RATE_HP:
1420 tvp->u.data = c->code_rate_HP;
1422 case DTV_CODE_RATE_LP:
1423 tvp->u.data = c->code_rate_LP;
1425 case DTV_GUARD_INTERVAL:
1426 tvp->u.data = c->guard_interval;
1428 case DTV_TRANSMISSION_MODE:
1429 tvp->u.data = c->transmission_mode;
1432 tvp->u.data = c->hierarchy;
1434 case DTV_INTERLEAVING:
1435 tvp->u.data = c->interleaving;
1438 /* ISDB-T Support here */
1439 case DTV_ISDBT_PARTIAL_RECEPTION:
1440 tvp->u.data = c->isdbt_partial_reception;
1442 case DTV_ISDBT_SOUND_BROADCASTING:
1443 tvp->u.data = c->isdbt_sb_mode;
1445 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1446 tvp->u.data = c->isdbt_sb_subchannel;
1448 case DTV_ISDBT_SB_SEGMENT_IDX:
1449 tvp->u.data = c->isdbt_sb_segment_idx;
1451 case DTV_ISDBT_SB_SEGMENT_COUNT:
1452 tvp->u.data = c->isdbt_sb_segment_count;
1454 case DTV_ISDBT_LAYER_ENABLED:
1455 tvp->u.data = c->isdbt_layer_enabled;
1457 case DTV_ISDBT_LAYERA_FEC:
1458 tvp->u.data = c->layer[0].fec;
1460 case DTV_ISDBT_LAYERA_MODULATION:
1461 tvp->u.data = c->layer[0].modulation;
1463 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1464 tvp->u.data = c->layer[0].segment_count;
1466 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1467 tvp->u.data = c->layer[0].interleaving;
1469 case DTV_ISDBT_LAYERB_FEC:
1470 tvp->u.data = c->layer[1].fec;
1472 case DTV_ISDBT_LAYERB_MODULATION:
1473 tvp->u.data = c->layer[1].modulation;
1475 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1476 tvp->u.data = c->layer[1].segment_count;
1478 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1479 tvp->u.data = c->layer[1].interleaving;
1481 case DTV_ISDBT_LAYERC_FEC:
1482 tvp->u.data = c->layer[2].fec;
1484 case DTV_ISDBT_LAYERC_MODULATION:
1485 tvp->u.data = c->layer[2].modulation;
1487 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1488 tvp->u.data = c->layer[2].segment_count;
1490 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1491 tvp->u.data = c->layer[2].interleaving;
1494 /* Multistream support */
1496 case DTV_DVBT2_PLP_ID_LEGACY:
1497 tvp->u.data = c->stream_id;
1501 case DTV_ATSCMH_FIC_VER:
1502 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1504 case DTV_ATSCMH_PARADE_ID:
1505 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1507 case DTV_ATSCMH_NOG:
1508 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1510 case DTV_ATSCMH_TNOG:
1511 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1513 case DTV_ATSCMH_SGN:
1514 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1516 case DTV_ATSCMH_PRC:
1517 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1519 case DTV_ATSCMH_RS_FRAME_MODE:
1520 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1522 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1523 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1525 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1526 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1528 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1529 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1531 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1532 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1534 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1535 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1537 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1538 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1540 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1541 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1543 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1544 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1548 tvp->u.data = c->lna;
1551 /* Fill quality measures */
1552 case DTV_STAT_SIGNAL_STRENGTH:
1553 tvp->u.st = c->strength;
1558 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1559 tvp->u.st = c->pre_bit_error;
1561 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1562 tvp->u.st = c->pre_bit_count;
1564 case DTV_STAT_POST_ERROR_BIT_COUNT:
1565 tvp->u.st = c->post_bit_error;
1567 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1568 tvp->u.st = c->post_bit_count;
1570 case DTV_STAT_ERROR_BLOCK_COUNT:
1571 tvp->u.st = c->block_error;
1573 case DTV_STAT_TOTAL_BLOCK_COUNT:
1574 tvp->u.st = c->block_count;
1577 dev_dbg(fe->dvb->device,
1578 "%s: FE property %d doesn't exist\n",
1579 __func__, tvp->cmd);
1583 /* Allow the frontend to override outgoing properties */
1584 if (fe->ops.get_property) {
1585 r = fe->ops.get_property(fe, tvp);
1590 dtv_property_dump(fe, tvp);
1595 static int dtv_set_frontend(struct dvb_frontend *fe);
1597 static bool is_dvbv3_delsys(u32 delsys)
1601 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1602 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1608 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1609 * @fe: struct frontend;
1610 * @delsys: DVBv5 type that will be used for emulation
1612 * Provides emulation for delivery systems that are compatible with the old
1613 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1614 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1615 * parameters are compatible with DVB-S spec.
1617 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1620 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1622 c->delivery_system = delsys;
1625 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1627 if (c->delivery_system == SYS_ISDBT) {
1628 dev_dbg(fe->dvb->device,
1629 "%s: Using defaults for SYS_ISDBT\n",
1632 if (!c->bandwidth_hz)
1633 c->bandwidth_hz = 6000000;
1635 c->isdbt_partial_reception = 0;
1636 c->isdbt_sb_mode = 0;
1637 c->isdbt_sb_subchannel = 0;
1638 c->isdbt_sb_segment_idx = 0;
1639 c->isdbt_sb_segment_count = 0;
1640 c->isdbt_layer_enabled = 7;
1641 for (i = 0; i < 3; i++) {
1642 c->layer[i].fec = FEC_AUTO;
1643 c->layer[i].modulation = QAM_AUTO;
1644 c->layer[i].interleaving = 0;
1645 c->layer[i].segment_count = 0;
1648 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1649 __func__, c->delivery_system);
1655 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1656 * @fe: frontend struct
1657 * @desired_system: delivery system requested by the user
1659 * A DVBv5 call know what's the desired system it wants. So, set it.
1661 * There are, however, a few known issues with early DVBv5 applications that
1662 * are also handled by this logic:
1664 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1665 * This is an API violation, but, as we don't want to break userspace,
1666 * convert it to the first supported delivery system.
1667 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1668 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1669 * ISDB-T provided backward compat with DVB-T.
1671 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1675 u32 delsys = SYS_UNDEFINED;
1676 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1677 enum dvbv3_emulation_type type;
1680 * It was reported that some old DVBv5 applications were
1681 * filling delivery_system with SYS_UNDEFINED. If this happens,
1682 * assume that the application wants to use the first supported
1685 if (desired_system == SYS_UNDEFINED)
1686 desired_system = fe->ops.delsys[0];
1689 * This is a DVBv5 call. So, it likely knows the supported
1690 * delivery systems. So, check if the desired delivery system is
1694 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1695 if (fe->ops.delsys[ncaps] == desired_system) {
1696 c->delivery_system = desired_system;
1697 dev_dbg(fe->dvb->device,
1698 "%s: Changing delivery system to %d\n",
1699 __func__, desired_system);
1706 * The requested delivery system isn't supported. Maybe userspace
1707 * is requesting a DVBv3 compatible delivery system.
1709 * The emulation only works if the desired system is one of the
1710 * delivery systems supported by DVBv3 API
1712 if (!is_dvbv3_delsys(desired_system)) {
1713 dev_dbg(fe->dvb->device,
1714 "%s: Delivery system %d not supported.\n",
1715 __func__, desired_system);
1719 type = dvbv3_type(desired_system);
1722 * Get the last non-DVBv3 delivery system that has the same type
1723 * of the desired system
1726 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1727 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1728 delsys = fe->ops.delsys[ncaps];
1732 /* There's nothing compatible with the desired delivery system */
1733 if (delsys == SYS_UNDEFINED) {
1734 dev_dbg(fe->dvb->device,
1735 "%s: Delivery system %d not supported on emulation mode.\n",
1736 __func__, desired_system);
1740 dev_dbg(fe->dvb->device,
1741 "%s: Using delivery system %d emulated as if it were %d\n",
1742 __func__, delsys, desired_system);
1744 return emulate_delivery_system(fe, desired_system);
1748 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1749 * @fe: frontend struct
1751 * A DVBv3 call doesn't know what's the desired system it wants. It also
1752 * doesn't allow to switch between different types. Due to that, userspace
1753 * should use DVBv5 instead.
1754 * However, in order to avoid breaking userspace API, limited backward
1755 * compatibility support is provided.
1757 * There are some delivery systems that are incompatible with DVBv3 calls.
1759 * This routine should work fine for frontends that support just one delivery
1762 * For frontends that support multiple frontends:
1763 * 1) It defaults to use the first supported delivery system. There's an
1764 * userspace application that allows changing it at runtime;
1766 * 2) If the current delivery system is not compatible with DVBv3, it gets
1767 * the first one that it is compatible.
1769 * NOTE: in order for this to work with applications like Kaffeine that
1770 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1771 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1772 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1775 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1778 u32 delsys = SYS_UNDEFINED;
1779 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1781 /* If not set yet, defaults to the first supported delivery system */
1782 if (c->delivery_system == SYS_UNDEFINED)
1783 c->delivery_system = fe->ops.delsys[0];
1786 * Trivial case: just use the current one, if it already a DVBv3
1789 if (is_dvbv3_delsys(c->delivery_system)) {
1790 dev_dbg(fe->dvb->device,
1791 "%s: Using delivery system to %d\n",
1792 __func__, c->delivery_system);
1797 * Seek for the first delivery system that it is compatible with a
1801 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1802 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1803 delsys = fe->ops.delsys[ncaps];
1808 if (delsys == SYS_UNDEFINED) {
1809 dev_dbg(fe->dvb->device,
1810 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1814 return emulate_delivery_system(fe, delsys);
1817 static int dtv_property_process_set(struct dvb_frontend *fe,
1818 struct dtv_property *tvp,
1822 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1824 /* Allow the frontend to validate incoming properties */
1825 if (fe->ops.set_property) {
1826 r = fe->ops.set_property(fe, tvp);
1834 * Reset a cache of data specific to the frontend here. This does
1835 * not effect hardware.
1837 dvb_frontend_clear_cache(fe);
1840 /* interpret the cache of data, build either a traditional frontend
1841 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1844 c->state = tvp->cmd;
1845 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1848 r = dtv_set_frontend(fe);
1851 c->frequency = tvp->u.data;
1853 case DTV_MODULATION:
1854 c->modulation = tvp->u.data;
1856 case DTV_BANDWIDTH_HZ:
1857 c->bandwidth_hz = tvp->u.data;
1860 c->inversion = tvp->u.data;
1862 case DTV_SYMBOL_RATE:
1863 c->symbol_rate = tvp->u.data;
1866 c->fec_inner = tvp->u.data;
1869 c->pilot = tvp->u.data;
1872 c->rolloff = tvp->u.data;
1874 case DTV_DELIVERY_SYSTEM:
1875 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1878 c->voltage = tvp->u.data;
1879 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1880 (void *)c->voltage);
1883 c->sectone = tvp->u.data;
1884 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1885 (void *)c->sectone);
1887 case DTV_CODE_RATE_HP:
1888 c->code_rate_HP = tvp->u.data;
1890 case DTV_CODE_RATE_LP:
1891 c->code_rate_LP = tvp->u.data;
1893 case DTV_GUARD_INTERVAL:
1894 c->guard_interval = tvp->u.data;
1896 case DTV_TRANSMISSION_MODE:
1897 c->transmission_mode = tvp->u.data;
1900 c->hierarchy = tvp->u.data;
1902 case DTV_INTERLEAVING:
1903 c->interleaving = tvp->u.data;
1906 /* ISDB-T Support here */
1907 case DTV_ISDBT_PARTIAL_RECEPTION:
1908 c->isdbt_partial_reception = tvp->u.data;
1910 case DTV_ISDBT_SOUND_BROADCASTING:
1911 c->isdbt_sb_mode = tvp->u.data;
1913 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1914 c->isdbt_sb_subchannel = tvp->u.data;
1916 case DTV_ISDBT_SB_SEGMENT_IDX:
1917 c->isdbt_sb_segment_idx = tvp->u.data;
1919 case DTV_ISDBT_SB_SEGMENT_COUNT:
1920 c->isdbt_sb_segment_count = tvp->u.data;
1922 case DTV_ISDBT_LAYER_ENABLED:
1923 c->isdbt_layer_enabled = tvp->u.data;
1925 case DTV_ISDBT_LAYERA_FEC:
1926 c->layer[0].fec = tvp->u.data;
1928 case DTV_ISDBT_LAYERA_MODULATION:
1929 c->layer[0].modulation = tvp->u.data;
1931 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1932 c->layer[0].segment_count = tvp->u.data;
1934 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1935 c->layer[0].interleaving = tvp->u.data;
1937 case DTV_ISDBT_LAYERB_FEC:
1938 c->layer[1].fec = tvp->u.data;
1940 case DTV_ISDBT_LAYERB_MODULATION:
1941 c->layer[1].modulation = tvp->u.data;
1943 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1944 c->layer[1].segment_count = tvp->u.data;
1946 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1947 c->layer[1].interleaving = tvp->u.data;
1949 case DTV_ISDBT_LAYERC_FEC:
1950 c->layer[2].fec = tvp->u.data;
1952 case DTV_ISDBT_LAYERC_MODULATION:
1953 c->layer[2].modulation = tvp->u.data;
1955 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1956 c->layer[2].segment_count = tvp->u.data;
1958 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1959 c->layer[2].interleaving = tvp->u.data;
1962 /* Multistream support */
1964 case DTV_DVBT2_PLP_ID_LEGACY:
1965 c->stream_id = tvp->u.data;
1969 case DTV_ATSCMH_PARADE_ID:
1970 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1972 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1973 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1977 c->lna = tvp->u.data;
1978 if (fe->ops.set_lna)
1979 r = fe->ops.set_lna(fe);
1991 static int dvb_frontend_ioctl(struct file *file,
1992 unsigned int cmd, void *parg)
1994 struct dvb_device *dvbdev = file->private_data;
1995 struct dvb_frontend *fe = dvbdev->priv;
1996 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1997 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1998 int err = -EOPNOTSUPP;
2000 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
2001 if (down_interruptible(&fepriv->sem))
2002 return -ERESTARTSYS;
2004 if (fe->exit != DVB_FE_NO_EXIT) {
2009 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
2010 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
2011 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
2016 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
2017 err = dvb_frontend_ioctl_properties(file, cmd, parg);
2019 c->state = DTV_UNDEFINED;
2020 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
2027 static int dvb_frontend_ioctl_properties(struct file *file,
2028 unsigned int cmd, void *parg)
2030 struct dvb_device *dvbdev = file->private_data;
2031 struct dvb_frontend *fe = dvbdev->priv;
2032 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2033 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2036 struct dtv_properties *tvps = parg;
2037 struct dtv_property *tvp = NULL;
2040 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2042 if (cmd == FE_SET_PROPERTY) {
2043 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2044 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2046 /* Put an arbitrary limit on the number of messages that can
2047 * be sent at once */
2048 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2051 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2057 if (copy_from_user(tvp, (void __user *)tvps->props,
2058 tvps->num * sizeof(struct dtv_property))) {
2063 for (i = 0; i < tvps->num; i++) {
2064 err = dtv_property_process_set(fe, tvp + i, file);
2067 (tvp + i)->result = err;
2070 if (c->state == DTV_TUNE)
2071 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
2073 } else if (cmd == FE_GET_PROPERTY) {
2074 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2075 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2077 /* Put an arbitrary limit on the number of messages that can
2078 * be sent at once */
2079 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2082 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2088 if (copy_from_user(tvp, (void __user *)tvps->props,
2089 tvps->num * sizeof(struct dtv_property))) {
2095 * Fills the cache out struct with the cache contents, plus
2096 * the data retrieved from get_frontend, if the frontend
2097 * is not idle. Otherwise, returns the cached content
2099 if (fepriv->state != FESTATE_IDLE) {
2100 err = dtv_get_frontend(fe, NULL);
2104 for (i = 0; i < tvps->num; i++) {
2105 err = dtv_property_process_get(fe, c, tvp + i, file);
2108 (tvp + i)->result = err;
2111 if (copy_to_user((void __user *)tvps->props, tvp,
2112 tvps->num * sizeof(struct dtv_property))) {
2125 static int dtv_set_frontend(struct dvb_frontend *fe)
2127 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2128 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2129 struct dvb_frontend_tune_settings fetunesettings;
2132 if (dvb_frontend_check_parameters(fe) < 0)
2136 * Initialize output parameters to match the values given by
2137 * the user. FE_SET_FRONTEND triggers an initial frontend event
2138 * with status = 0, which copies output parameters to userspace.
2140 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2143 * Be sure that the bandwidth will be filled for all
2144 * non-satellite systems, as tuners need to know what
2145 * low pass/Nyquist half filter should be applied, in
2146 * order to avoid inter-channel noise.
2148 * ISDB-T and DVB-T/T2 already sets bandwidth.
2149 * ATSC and DVB-C don't set, so, the core should fill it.
2151 * On DVB-C Annex A and C, the bandwidth is a function of
2152 * the roll-off and symbol rate. Annex B defines different
2153 * roll-off factors depending on the modulation. Fortunately,
2154 * Annex B is only used with 6MHz, so there's no need to
2157 * While not officially supported, a side effect of handling it at
2158 * the cache level is that a program could retrieve the bandwidth
2159 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2161 switch (c->delivery_system) {
2163 case SYS_DVBC_ANNEX_B:
2164 c->bandwidth_hz = 6000000;
2166 case SYS_DVBC_ANNEX_A:
2169 case SYS_DVBC_ANNEX_C:
2178 switch (c->rolloff) {
2194 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
2196 /* force auto frequency inversion if requested */
2197 if (dvb_force_auto_inversion)
2198 c->inversion = INVERSION_AUTO;
2201 * without hierarchical coding code_rate_LP is irrelevant,
2202 * so we tolerate the otherwise invalid FEC_NONE setting
2204 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2205 c->code_rate_LP = FEC_AUTO;
2207 /* get frontend-specific tuning settings */
2208 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2209 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2210 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2211 fepriv->max_drift = fetunesettings.max_drift;
2212 fepriv->step_size = fetunesettings.step_size;
2214 /* default values */
2215 switch (c->delivery_system) {
2220 case SYS_DVBC_ANNEX_A:
2221 case SYS_DVBC_ANNEX_C:
2222 fepriv->min_delay = HZ / 20;
2223 fepriv->step_size = c->symbol_rate / 16000;
2224 fepriv->max_drift = c->symbol_rate / 2000;
2230 fepriv->min_delay = HZ / 20;
2231 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2232 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2236 * FIXME: This sounds wrong! if freqency_stepsize is
2237 * defined by the frontend, why not use it???
2239 fepriv->min_delay = HZ / 20;
2240 fepriv->step_size = 0; /* no zigzag */
2241 fepriv->max_drift = 0;
2245 if (dvb_override_tune_delay > 0)
2246 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2248 fepriv->state = FESTATE_RETUNE;
2250 /* Request the search algorithm to search */
2251 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2253 dvb_frontend_clear_events(fe);
2254 dvb_frontend_add_event(fe, 0);
2255 dvb_frontend_wakeup(fe);
2262 static int dvb_frontend_ioctl_legacy(struct file *file,
2263 unsigned int cmd, void *parg)
2265 struct dvb_device *dvbdev = file->private_data;
2266 struct dvb_frontend *fe = dvbdev->priv;
2267 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2268 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2269 int err = -EOPNOTSUPP;
2273 struct dvb_frontend_info* info = parg;
2275 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2276 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2279 * Associate the 4 delivery systems supported by DVBv3
2280 * API with their DVBv5 counterpart. For the other standards,
2281 * use the closest type, assuming that it would hopefully
2282 * work with a DVBv3 application.
2283 * It should be noticed that, on multi-frontend devices with
2284 * different types (terrestrial and cable, for example),
2285 * a pure DVBv3 application won't be able to use all delivery
2286 * systems. Yet, changing the DVBv5 cache to the other delivery
2287 * system should be enough for making it work.
2289 switch (dvbv3_type(c->delivery_system)) {
2291 info->type = FE_QPSK;
2294 info->type = FE_ATSC;
2297 info->type = FE_QAM;
2300 info->type = FE_OFDM;
2303 dev_err(fe->dvb->device,
2304 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2305 __func__, c->delivery_system);
2306 fe->ops.info.type = FE_OFDM;
2308 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2309 __func__, c->delivery_system, fe->ops.info.type);
2311 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2312 * do it, it is done for it. */
2313 info->caps |= FE_CAN_INVERSION_AUTO;
2318 case FE_READ_STATUS: {
2319 fe_status_t* status = parg;
2321 /* if retune was requested but hasn't occurred yet, prevent
2322 * that user get signal state from previous tuning */
2323 if (fepriv->state == FESTATE_RETUNE ||
2324 fepriv->state == FESTATE_ERROR) {
2330 if (fe->ops.read_status)
2331 err = fe->ops.read_status(fe, status);
2336 if (fe->ops.read_ber) {
2338 err = fe->ops.read_ber(fe, (__u32 *) parg);
2344 case FE_READ_SIGNAL_STRENGTH:
2345 if (fe->ops.read_signal_strength) {
2347 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2354 if (fe->ops.read_snr) {
2356 err = fe->ops.read_snr(fe, (__u16 *) parg);
2362 case FE_READ_UNCORRECTED_BLOCKS:
2363 if (fe->ops.read_ucblocks) {
2365 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2371 case FE_DISEQC_RESET_OVERLOAD:
2372 if (fe->ops.diseqc_reset_overload) {
2373 err = fe->ops.diseqc_reset_overload(fe);
2374 fepriv->state = FESTATE_DISEQC;
2379 case FE_DISEQC_SEND_MASTER_CMD:
2380 if (fe->ops.diseqc_send_master_cmd) {
2381 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2382 fepriv->state = FESTATE_DISEQC;
2387 case FE_DISEQC_SEND_BURST:
2388 if (fe->ops.diseqc_send_burst) {
2389 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2390 fepriv->state = FESTATE_DISEQC;
2396 if (fe->ops.set_tone) {
2397 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2398 fepriv->tone = (fe_sec_tone_mode_t) parg;
2399 fepriv->state = FESTATE_DISEQC;
2404 case FE_SET_VOLTAGE:
2405 if (fe->ops.set_voltage) {
2406 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2407 fepriv->voltage = (fe_sec_voltage_t) parg;
2408 fepriv->state = FESTATE_DISEQC;
2413 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2414 if (fe->ops.dishnetwork_send_legacy_command) {
2415 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2416 fepriv->state = FESTATE_DISEQC;
2418 } else if (fe->ops.set_voltage) {
2420 * NOTE: This is a fallback condition. Some frontends
2421 * (stv0299 for instance) take longer than 8msec to
2422 * respond to a set_voltage command. Those switches
2423 * need custom routines to switch properly. For all
2424 * other frontends, the following should work ok.
2425 * Dish network legacy switches (as used by Dish500)
2426 * are controlled by sending 9-bit command words
2427 * spaced 8msec apart.
2428 * the actual command word is switch/port dependent
2429 * so it is up to the userspace application to send
2430 * the right command.
2431 * The command must always start with a '0' after
2432 * initialization, so parg is 8 bits and does not
2433 * include the initialization or start bit
2435 unsigned long swcmd = ((unsigned long) parg) << 1;
2436 struct timeval nexttime;
2437 struct timeval tv[10];
2440 if (dvb_frontend_debug)
2441 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2442 do_gettimeofday(&nexttime);
2443 if (dvb_frontend_debug)
2445 /* before sending a command, initialize by sending
2446 * a 32ms 18V to the switch
2448 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2449 dvb_frontend_sleep_until(&nexttime, 32000);
2451 for (i = 0; i < 9; i++) {
2452 if (dvb_frontend_debug)
2453 do_gettimeofday(&tv[i + 1]);
2454 if ((swcmd & 0x01) != last) {
2455 /* set voltage to (last ? 13V : 18V) */
2456 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2457 last = (last) ? 0 : 1;
2461 dvb_frontend_sleep_until(&nexttime, 8000);
2463 if (dvb_frontend_debug) {
2464 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2465 __func__, fe->dvb->num);
2466 for (i = 1; i < 10; i++)
2467 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2470 fepriv->state = FESTATE_DISEQC;
2475 case FE_DISEQC_RECV_SLAVE_REPLY:
2476 if (fe->ops.diseqc_recv_slave_reply)
2477 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2480 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2481 if (fe->ops.enable_high_lnb_voltage)
2482 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2485 case FE_SET_FRONTEND:
2486 err = dvbv3_set_delivery_system(fe);
2490 err = dtv_property_cache_sync(fe, c, parg);
2493 err = dtv_set_frontend(fe);
2496 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2499 case FE_GET_FRONTEND:
2500 err = dtv_get_frontend(fe, parg);
2503 case FE_SET_FRONTEND_TUNE_MODE:
2504 fepriv->tune_mode_flags = (unsigned long) parg;
2513 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2515 struct dvb_device *dvbdev = file->private_data;
2516 struct dvb_frontend *fe = dvbdev->priv;
2517 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2519 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2521 poll_wait (file, &fepriv->events.wait_queue, wait);
2523 if (fepriv->events.eventw != fepriv->events.eventr)
2524 return (POLLIN | POLLRDNORM | POLLPRI);
2529 static int dvb_frontend_open(struct inode *inode, struct file *file)
2531 struct dvb_device *dvbdev = file->private_data;
2532 struct dvb_frontend *fe = dvbdev->priv;
2533 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2534 struct dvb_adapter *adapter = fe->dvb;
2537 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2538 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2541 if (adapter->mfe_shared) {
2542 mutex_lock (&adapter->mfe_lock);
2544 if (adapter->mfe_dvbdev == NULL)
2545 adapter->mfe_dvbdev = dvbdev;
2547 else if (adapter->mfe_dvbdev != dvbdev) {
2549 *mfedev = adapter->mfe_dvbdev;
2551 *mfe = mfedev->priv;
2552 struct dvb_frontend_private
2553 *mfepriv = mfe->frontend_priv;
2554 int mferetry = (dvb_mfe_wait_time << 1);
2556 mutex_unlock (&adapter->mfe_lock);
2557 while (mferetry-- && (mfedev->users != -1 ||
2558 mfepriv->thread != NULL)) {
2559 if(msleep_interruptible(500)) {
2560 if(signal_pending(current))
2565 mutex_lock (&adapter->mfe_lock);
2566 if(adapter->mfe_dvbdev != dvbdev) {
2567 mfedev = adapter->mfe_dvbdev;
2569 mfepriv = mfe->frontend_priv;
2570 if (mfedev->users != -1 ||
2571 mfepriv->thread != NULL) {
2572 mutex_unlock (&adapter->mfe_lock);
2575 adapter->mfe_dvbdev = dvbdev;
2580 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2581 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2584 /* If we took control of the bus, we need to force
2585 reinitialization. This is because many ts_bus_ctrl()
2586 functions strobe the RESET pin on the demod, and if the
2587 frontend thread already exists then the dvb_init() routine
2588 won't get called (which is what usually does initial
2589 register configuration). */
2590 fepriv->reinitialise = 1;
2593 if ((ret = dvb_generic_open (inode, file)) < 0)
2596 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2597 /* normal tune mode when opened R/W */
2598 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2600 fepriv->voltage = -1;
2602 ret = dvb_frontend_start (fe);
2606 /* empty event queue */
2607 fepriv->events.eventr = fepriv->events.eventw = 0;
2610 if (adapter->mfe_shared)
2611 mutex_unlock (&adapter->mfe_lock);
2615 dvb_generic_release(inode, file);
2617 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2618 fe->ops.ts_bus_ctrl(fe, 0);
2620 if (adapter->mfe_shared)
2621 mutex_unlock (&adapter->mfe_lock);
2625 static int dvb_frontend_release(struct inode *inode, struct file *file)
2627 struct dvb_device *dvbdev = file->private_data;
2628 struct dvb_frontend *fe = dvbdev->priv;
2629 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2632 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2634 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2635 fepriv->release_jiffies = jiffies;
2639 ret = dvb_generic_release (inode, file);
2641 if (dvbdev->users == -1) {
2642 wake_up(&fepriv->wait_queue);
2643 if (fe->exit != DVB_FE_NO_EXIT)
2644 wake_up(&dvbdev->wait_queue);
2645 if (fe->ops.ts_bus_ctrl)
2646 fe->ops.ts_bus_ctrl(fe, 0);
2652 static const struct file_operations dvb_frontend_fops = {
2653 .owner = THIS_MODULE,
2654 .unlocked_ioctl = dvb_generic_ioctl,
2655 .poll = dvb_frontend_poll,
2656 .open = dvb_frontend_open,
2657 .release = dvb_frontend_release,
2658 .llseek = noop_llseek,
2661 int dvb_frontend_suspend(struct dvb_frontend *fe)
2665 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2668 if (fe->ops.tuner_ops.suspend)
2669 ret = fe->ops.tuner_ops.suspend(fe);
2670 else if (fe->ops.tuner_ops.sleep)
2671 ret = fe->ops.tuner_ops.sleep(fe);
2674 ret = fe->ops.sleep(fe);
2678 EXPORT_SYMBOL(dvb_frontend_suspend);
2680 int dvb_frontend_resume(struct dvb_frontend *fe)
2682 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2685 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2688 fe->exit = DVB_FE_DEVICE_RESUME;
2690 ret = fe->ops.init(fe);
2692 if (fe->ops.tuner_ops.resume)
2693 ret = fe->ops.tuner_ops.resume(fe);
2694 else if (fe->ops.tuner_ops.init)
2695 ret = fe->ops.tuner_ops.init(fe);
2697 fe->exit = DVB_FE_NO_EXIT;
2698 fepriv->state = FESTATE_RETUNE;
2699 dvb_frontend_wakeup(fe);
2703 EXPORT_SYMBOL(dvb_frontend_resume);
2705 int dvb_register_frontend(struct dvb_adapter* dvb,
2706 struct dvb_frontend* fe)
2708 struct dvb_frontend_private *fepriv;
2709 const struct dvb_device dvbdev_template = {
2713 .fops = &dvb_frontend_fops,
2714 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2715 .name = fe->ops.info.name,
2717 .kernel_ioctl = dvb_frontend_ioctl
2720 dev_dbg(dvb->device, "%s:\n", __func__);
2722 if (mutex_lock_interruptible(&frontend_mutex))
2723 return -ERESTARTSYS;
2725 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2726 if (fe->frontend_priv == NULL) {
2727 mutex_unlock(&frontend_mutex);
2730 fepriv = fe->frontend_priv;
2732 sema_init(&fepriv->sem, 1);
2733 init_waitqueue_head (&fepriv->wait_queue);
2734 init_waitqueue_head (&fepriv->events.wait_queue);
2735 mutex_init(&fepriv->events.mtx);
2737 fepriv->inversion = INVERSION_OFF;
2739 dev_info(fe->dvb->device,
2740 "DVB: registering adapter %i frontend %i (%s)...\n",
2741 fe->dvb->num, fe->id, fe->ops.info.name);
2743 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2744 fe, DVB_DEVICE_FRONTEND);
2747 * Initialize the cache to the proper values according with the
2748 * first supported delivery system (ops->delsys[0])
2751 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2752 dvb_frontend_clear_cache(fe);
2754 mutex_unlock(&frontend_mutex);
2757 EXPORT_SYMBOL(dvb_register_frontend);
2759 int dvb_unregister_frontend(struct dvb_frontend* fe)
2761 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2762 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2764 mutex_lock(&frontend_mutex);
2765 dvb_frontend_stop (fe);
2766 mutex_unlock(&frontend_mutex);
2768 if (fepriv->dvbdev->users < -1)
2769 wait_event(fepriv->dvbdev->wait_queue,
2770 fepriv->dvbdev->users==-1);
2772 mutex_lock(&frontend_mutex);
2773 dvb_unregister_device (fepriv->dvbdev);
2775 /* fe is invalid now */
2777 mutex_unlock(&frontend_mutex);
2780 EXPORT_SYMBOL(dvb_unregister_frontend);
2782 #ifdef CONFIG_MEDIA_ATTACH
2783 void dvb_frontend_detach(struct dvb_frontend* fe)
2787 if (fe->ops.release_sec) {
2788 fe->ops.release_sec(fe);
2789 dvb_detach(fe->ops.release_sec);
2791 if (fe->ops.tuner_ops.release) {
2792 fe->ops.tuner_ops.release(fe);
2793 dvb_detach(fe->ops.tuner_ops.release);
2795 if (fe->ops.analog_ops.release) {
2796 fe->ops.analog_ops.release(fe);
2797 dvb_detach(fe->ops.analog_ops.release);
2799 ptr = (void*)fe->ops.release;
2801 fe->ops.release(fe);
2806 void dvb_frontend_detach(struct dvb_frontend* fe)
2808 if (fe->ops.release_sec)
2809 fe->ops.release_sec(fe);
2810 if (fe->ops.tuner_ops.release)
2811 fe->ops.tuner_ops.release(fe);
2812 if (fe->ops.analog_ops.release)
2813 fe->ops.analog_ops.release(fe);
2814 if (fe->ops.release)
2815 fe->ops.release(fe);
2818 EXPORT_SYMBOL(dvb_frontend_detach);