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;
113 enum fe_status status;
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;
135 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
136 struct media_pipeline pipe;
137 struct media_entity *pipe_start_entity;
141 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
142 static int dtv_get_frontend(struct dvb_frontend *fe,
143 struct dvb_frontend_parameters *p_out);
144 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
145 struct dvb_frontend_parameters *p);
147 static bool has_get_frontend(struct dvb_frontend *fe)
149 return fe->ops.get_frontend != NULL;
153 * Due to DVBv3 API calls, a delivery system should be mapped into one of
154 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
155 * otherwise, a DVBv3 call will fail.
157 enum dvbv3_emulation_type {
165 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
167 switch (delivery_system) {
168 case SYS_DVBC_ANNEX_A:
169 case SYS_DVBC_ANNEX_C:
184 case SYS_DVBC_ANNEX_B:
192 * Doesn't know how to emulate those types and/or
193 * there's no frontend driver from this type yet
194 * with some emulation code, so, we're not sure yet how
195 * to handle them, or they're not compatible with a DVBv3 call.
197 return DVBV3_UNKNOWN;
201 static void dvb_frontend_add_event(struct dvb_frontend *fe,
202 enum fe_status status)
204 struct dvb_frontend_private *fepriv = fe->frontend_priv;
205 struct dvb_fe_events *events = &fepriv->events;
206 struct dvb_frontend_event *e;
209 dev_dbg(fe->dvb->device, "%s:\n", __func__);
211 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
212 dtv_get_frontend(fe, &fepriv->parameters_out);
214 mutex_lock(&events->mtx);
216 wp = (events->eventw + 1) % MAX_EVENT;
217 if (wp == events->eventr) {
218 events->overflow = 1;
219 events->eventr = (events->eventr + 1) % MAX_EVENT;
222 e = &events->events[events->eventw];
224 e->parameters = fepriv->parameters_out;
228 mutex_unlock(&events->mtx);
230 wake_up_interruptible (&events->wait_queue);
233 static int dvb_frontend_get_event(struct dvb_frontend *fe,
234 struct dvb_frontend_event *event, int flags)
236 struct dvb_frontend_private *fepriv = fe->frontend_priv;
237 struct dvb_fe_events *events = &fepriv->events;
239 dev_dbg(fe->dvb->device, "%s:\n", __func__);
241 if (events->overflow) {
242 events->overflow = 0;
246 if (events->eventw == events->eventr) {
249 if (flags & O_NONBLOCK)
254 ret = wait_event_interruptible (events->wait_queue,
255 events->eventw != events->eventr);
257 if (down_interruptible (&fepriv->sem))
264 mutex_lock(&events->mtx);
265 *event = events->events[events->eventr];
266 events->eventr = (events->eventr + 1) % MAX_EVENT;
267 mutex_unlock(&events->mtx);
272 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
274 struct dvb_frontend_private *fepriv = fe->frontend_priv;
275 struct dvb_fe_events *events = &fepriv->events;
277 mutex_lock(&events->mtx);
278 events->eventr = events->eventw;
279 mutex_unlock(&events->mtx);
282 static void dvb_frontend_init(struct dvb_frontend *fe)
284 dev_dbg(fe->dvb->device,
285 "%s: initialising adapter %i frontend %i (%s)...\n",
286 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
290 if (fe->ops.tuner_ops.init) {
291 if (fe->ops.i2c_gate_ctrl)
292 fe->ops.i2c_gate_ctrl(fe, 1);
293 fe->ops.tuner_ops.init(fe);
294 if (fe->ops.i2c_gate_ctrl)
295 fe->ops.i2c_gate_ctrl(fe, 0);
299 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
301 struct dvb_frontend_private *fepriv = fe->frontend_priv;
303 fepriv->reinitialise = 1;
304 dvb_frontend_wakeup(fe);
306 EXPORT_SYMBOL(dvb_frontend_reinitialise);
308 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
311 struct dvb_frontend *fe = fepriv->dvbdev->priv;
313 dev_dbg(fe->dvb->device, "%s:\n", __func__);
316 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
318 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
320 q2 = fepriv->quality - 128;
323 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
327 * Performs automatic twiddling of frontend parameters.
329 * @param fe The frontend concerned.
330 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
331 * @returns Number of complete iterations that have been performed.
333 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
338 struct dvb_frontend_private *fepriv = fe->frontend_priv;
339 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
340 int original_inversion = c->inversion;
341 u32 original_frequency = c->frequency;
343 /* are we using autoinversion? */
344 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
345 (c->inversion == INVERSION_AUTO));
347 /* setup parameters correctly */
349 /* calculate the lnb_drift */
350 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
352 /* wrap the auto_step if we've exceeded the maximum drift */
353 if (fepriv->lnb_drift > fepriv->max_drift) {
354 fepriv->auto_step = 0;
355 fepriv->auto_sub_step = 0;
356 fepriv->lnb_drift = 0;
359 /* perform inversion and +/- zigzag */
360 switch(fepriv->auto_sub_step) {
362 /* try with the current inversion and current drift setting */
367 if (!autoinversion) break;
369 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
374 if (fepriv->lnb_drift == 0) break;
376 fepriv->lnb_drift = -fepriv->lnb_drift;
381 if (fepriv->lnb_drift == 0) break;
382 if (!autoinversion) break;
384 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
385 fepriv->lnb_drift = -fepriv->lnb_drift;
391 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
395 if (!ready) fepriv->auto_sub_step++;
398 /* if this attempt would hit where we started, indicate a complete
399 * iteration has occurred */
400 if ((fepriv->auto_step == fepriv->started_auto_step) &&
401 (fepriv->auto_sub_step == 0) && check_wrapped) {
405 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
406 "auto_sub_step:%i started_auto_step:%i\n",
407 __func__, fepriv->lnb_drift, fepriv->inversion,
408 fepriv->auto_step, fepriv->auto_sub_step,
409 fepriv->started_auto_step);
411 /* set the frontend itself */
412 c->frequency += fepriv->lnb_drift;
414 c->inversion = fepriv->inversion;
416 if (fe->ops.set_frontend)
417 fe_set_err = fe->ops.set_frontend(fe);
419 if (fe_set_err < 0) {
420 fepriv->state = FESTATE_ERROR;
424 c->frequency = original_frequency;
425 c->inversion = original_inversion;
427 fepriv->auto_sub_step++;
431 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
433 enum fe_status s = 0;
435 struct dvb_frontend_private *fepriv = fe->frontend_priv;
436 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
438 /* if we've got no parameters, just keep idling */
439 if (fepriv->state & FESTATE_IDLE) {
440 fepriv->delay = 3*HZ;
445 /* in SCAN mode, we just set the frontend when asked and leave it alone */
446 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
447 if (fepriv->state & FESTATE_RETUNE) {
449 if (fe->ops.set_frontend)
450 retval = fe->ops.set_frontend(fe);
453 fepriv->state = FESTATE_ERROR;
455 fepriv->state = FESTATE_TUNED;
457 fepriv->delay = 3*HZ;
462 /* get the frontend status */
463 if (fepriv->state & FESTATE_RETUNE) {
466 if (fe->ops.read_status)
467 fe->ops.read_status(fe, &s);
468 if (s != fepriv->status) {
469 dvb_frontend_add_event(fe, s);
474 /* if we're not tuned, and we have a lock, move to the TUNED state */
475 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
476 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
477 fepriv->state = FESTATE_TUNED;
479 /* if we're tuned, then we have determined the correct inversion */
480 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
481 (c->inversion == INVERSION_AUTO)) {
482 c->inversion = fepriv->inversion;
487 /* if we are tuned already, check we're still locked */
488 if (fepriv->state & FESTATE_TUNED) {
489 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
491 /* we're tuned, and the lock is still good... */
492 if (s & FE_HAS_LOCK) {
494 } else { /* if we _WERE_ tuned, but now don't have a lock */
495 fepriv->state = FESTATE_ZIGZAG_FAST;
496 fepriv->started_auto_step = fepriv->auto_step;
497 fepriv->check_wrapped = 0;
501 /* don't actually do anything if we're in the LOSTLOCK state,
502 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
503 if ((fepriv->state & FESTATE_LOSTLOCK) &&
504 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
505 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
509 /* don't do anything if we're in the DISEQC state, since this
510 * might be someone with a motorized dish controlled by DISEQC.
511 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
512 if (fepriv->state & FESTATE_DISEQC) {
513 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
517 /* if we're in the RETUNE state, set everything up for a brand
518 * new scan, keeping the current inversion setting, as the next
519 * tune is _very_ likely to require the same */
520 if (fepriv->state & FESTATE_RETUNE) {
521 fepriv->lnb_drift = 0;
522 fepriv->auto_step = 0;
523 fepriv->auto_sub_step = 0;
524 fepriv->started_auto_step = 0;
525 fepriv->check_wrapped = 0;
529 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
530 fepriv->delay = fepriv->min_delay;
533 retval = dvb_frontend_swzigzag_autotune(fe,
534 fepriv->check_wrapped);
538 /* OK, if we've run out of trials at the fast speed.
539 * Drop back to slow for the _next_ attempt */
540 fepriv->state = FESTATE_SEARCHING_SLOW;
541 fepriv->started_auto_step = fepriv->auto_step;
544 fepriv->check_wrapped = 1;
546 /* if we've just retuned, enter the ZIGZAG_FAST state.
547 * This ensures we cannot return from an
548 * FE_SET_FRONTEND ioctl before the first frontend tune
550 if (fepriv->state & FESTATE_RETUNE) {
551 fepriv->state = FESTATE_TUNING_FAST;
556 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
557 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
559 /* Note: don't bother checking for wrapping; we stay in this
560 * state until we get a lock */
561 dvb_frontend_swzigzag_autotune(fe, 0);
565 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
567 struct dvb_frontend_private *fepriv = fe->frontend_priv;
569 if (fe->exit != DVB_FE_NO_EXIT)
572 if (fepriv->dvbdev->writers == 1)
573 if (time_after_eq(jiffies, fepriv->release_jiffies +
574 dvb_shutdown_timeout * HZ))
580 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
582 struct dvb_frontend_private *fepriv = fe->frontend_priv;
584 if (fepriv->wakeup) {
588 return dvb_frontend_is_exiting(fe);
591 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
593 struct dvb_frontend_private *fepriv = fe->frontend_priv;
596 wake_up_interruptible(&fepriv->wait_queue);
600 * dvb_enable_media_tuner() - tries to enable the DVB tuner
602 * @fe: struct dvb_frontend pointer
604 * This function ensures that just one media tuner is enabled for a given
605 * frontend. It has two different behaviors:
606 * - For trivial devices with just one tuner:
607 * it just enables the existing tuner->fe link
608 * - For devices with more than one tuner:
609 * It is up to the driver to implement the logic that will enable one tuner
610 * and disable the other ones. However, if more than one tuner is enabled for
611 * the same frontend, it will print an error message and return -EINVAL.
613 * At return, it will return the error code returned by media_entity_setup_link,
614 * or 0 if everything is OK, if no tuner is linked to the frontend or if the
617 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
618 static int dvb_enable_media_tuner(struct dvb_frontend *fe)
620 struct dvb_frontend_private *fepriv = fe->frontend_priv;
621 struct dvb_adapter *adapter = fe->dvb;
622 struct media_device *mdev = adapter->mdev;
623 struct media_entity *entity, *source;
624 struct media_link *link, *found_link = NULL;
625 int i, ret, n_links = 0, active_links = 0;
627 fepriv->pipe_start_entity = NULL;
632 entity = fepriv->dvbdev->entity;
633 fepriv->pipe_start_entity = entity;
635 for (i = 0; i < entity->num_links; i++) {
636 link = &entity->links[i];
637 if (link->sink->entity == entity) {
640 if (link->flags & MEDIA_LNK_FL_ENABLED)
645 if (!n_links || active_links == 1 || !found_link)
649 * If a frontend has more than one tuner linked, it is up to the driver
650 * to select with one will be the active one, as the frontend core can't
651 * guess. If the driver doesn't do that, it is a bug.
653 if (n_links > 1 && active_links != 1) {
654 dev_err(fe->dvb->device,
655 "WARNING: there are %d active links among %d tuners. This is a driver's bug!\n",
656 active_links, n_links);
660 source = found_link->source->entity;
661 fepriv->pipe_start_entity = source;
662 for (i = 0; i < source->num_links; i++) {
663 struct media_entity *sink;
666 link = &source->links[i];
667 sink = link->sink->entity;
670 flags = MEDIA_LNK_FL_ENABLED;
672 ret = media_entity_setup_link(link, flags);
674 dev_err(fe->dvb->device,
675 "Couldn't change link %s->%s to %s. Error %d\n",
676 source->name, sink->name,
677 flags ? "enabled" : "disabled",
681 dev_dbg(fe->dvb->device,
682 "link %s->%s was %s\n",
683 source->name, sink->name,
684 flags ? "ENABLED" : "disabled");
690 static int dvb_frontend_thread(void *data)
692 struct dvb_frontend *fe = data;
693 struct dvb_frontend_private *fepriv = fe->frontend_priv;
695 enum dvbfe_algo algo;
696 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
700 bool re_tune = false;
701 bool semheld = false;
703 dev_dbg(fe->dvb->device, "%s:\n", __func__);
705 fepriv->check_wrapped = 0;
707 fepriv->delay = 3*HZ;
710 fepriv->reinitialise = 0;
712 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
713 ret = dvb_enable_media_tuner(fe);
715 /* FIXME: return an error if it fails */
716 dev_info(fe->dvb->device,
717 "proceeding with FE task\n");
718 } else if (fepriv->pipe_start_entity) {
719 ret = media_entity_pipeline_start(fepriv->pipe_start_entity,
726 dvb_frontend_init(fe);
730 up(&fepriv->sem); /* is locked when we enter the thread... */
732 wait_event_interruptible_timeout(fepriv->wait_queue,
733 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
734 || freezing(current),
737 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
738 /* got signal or quitting */
739 if (!down_interruptible(&fepriv->sem))
741 fe->exit = DVB_FE_NORMAL_EXIT;
748 if (down_interruptible(&fepriv->sem))
751 if (fepriv->reinitialise) {
752 dvb_frontend_init(fe);
753 if (fe->ops.set_tone && fepriv->tone != -1)
754 fe->ops.set_tone(fe, fepriv->tone);
755 if (fe->ops.set_voltage && fepriv->voltage != -1)
756 fe->ops.set_voltage(fe, fepriv->voltage);
757 fepriv->reinitialise = 0;
760 /* do an iteration of the tuning loop */
761 if (fe->ops.get_frontend_algo) {
762 algo = fe->ops.get_frontend_algo(fe);
765 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
767 if (fepriv->state & FESTATE_RETUNE) {
768 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
770 fepriv->state = FESTATE_TUNED;
776 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
778 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
779 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
780 dvb_frontend_add_event(fe, s);
785 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
786 dvb_frontend_swzigzag(fe);
788 case DVBFE_ALGO_CUSTOM:
789 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
790 if (fepriv->state & FESTATE_RETUNE) {
791 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
792 fepriv->state = FESTATE_TUNED;
794 /* Case where we are going to search for a carrier
795 * User asked us to retune again for some reason, possibly
796 * requesting a search with a new set of parameters
798 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
799 if (fe->ops.search) {
800 fepriv->algo_status = fe->ops.search(fe);
801 /* We did do a search as was requested, the flags are
802 * now unset as well and has the flags wrt to search.
805 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
808 /* Track the carrier if the search was successful */
809 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
810 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
811 fepriv->delay = HZ / 2;
813 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
814 fe->ops.read_status(fe, &s);
815 if (s != fepriv->status) {
816 dvb_frontend_add_event(fe, s); /* update event list */
818 if (!(s & FE_HAS_LOCK)) {
819 fepriv->delay = HZ / 10;
820 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
822 fepriv->delay = 60 * HZ;
827 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
831 dvb_frontend_swzigzag(fe);
835 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
836 if (fepriv->pipe_start_entity)
837 media_entity_pipeline_stop(fepriv->pipe_start_entity);
838 fepriv->pipe_start_entity = NULL;
841 if (dvb_powerdown_on_sleep) {
842 if (fe->ops.set_voltage)
843 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
844 if (fe->ops.tuner_ops.sleep) {
845 if (fe->ops.i2c_gate_ctrl)
846 fe->ops.i2c_gate_ctrl(fe, 1);
847 fe->ops.tuner_ops.sleep(fe);
848 if (fe->ops.i2c_gate_ctrl)
849 fe->ops.i2c_gate_ctrl(fe, 0);
855 fepriv->thread = NULL;
856 if (kthread_should_stop())
857 fe->exit = DVB_FE_DEVICE_REMOVED;
859 fe->exit = DVB_FE_NO_EXIT;
864 dvb_frontend_wakeup(fe);
868 static void dvb_frontend_stop(struct dvb_frontend *fe)
870 struct dvb_frontend_private *fepriv = fe->frontend_priv;
872 dev_dbg(fe->dvb->device, "%s:\n", __func__);
874 if (fe->exit != DVB_FE_DEVICE_REMOVED)
875 fe->exit = DVB_FE_NORMAL_EXIT;
881 kthread_stop(fepriv->thread);
883 sema_init(&fepriv->sem, 1);
884 fepriv->state = FESTATE_IDLE;
886 /* paranoia check in case a signal arrived */
888 dev_warn(fe->dvb->device,
889 "dvb_frontend_stop: warning: thread %p won't exit\n",
893 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
895 return ((curtime.tv_usec < lasttime.tv_usec) ?
896 1000000 - lasttime.tv_usec + curtime.tv_usec :
897 curtime.tv_usec - lasttime.tv_usec);
899 EXPORT_SYMBOL(timeval_usec_diff);
901 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
903 curtime->tv_usec += add_usec;
904 if (curtime->tv_usec >= 1000000) {
905 curtime->tv_usec -= 1000000;
911 * Sleep until gettimeofday() > waketime + add_usec
912 * This needs to be as precise as possible, but as the delay is
913 * usually between 2ms and 32ms, it is done using a scheduled msleep
914 * followed by usleep (normally a busy-wait loop) for the remainder
916 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
918 struct timeval lasttime;
921 timeval_usec_add(waketime, add_usec);
923 do_gettimeofday(&lasttime);
924 delta = timeval_usec_diff(lasttime, *waketime);
926 msleep((delta - 1500) / 1000);
927 do_gettimeofday(&lasttime);
928 newdelta = timeval_usec_diff(lasttime, *waketime);
929 delta = (newdelta > delta) ? 0 : newdelta;
934 EXPORT_SYMBOL(dvb_frontend_sleep_until);
936 static int dvb_frontend_start(struct dvb_frontend *fe)
939 struct dvb_frontend_private *fepriv = fe->frontend_priv;
940 struct task_struct *fe_thread;
942 dev_dbg(fe->dvb->device, "%s:\n", __func__);
944 if (fepriv->thread) {
945 if (fe->exit == DVB_FE_NO_EXIT)
948 dvb_frontend_stop (fe);
951 if (signal_pending(current))
953 if (down_interruptible (&fepriv->sem))
956 fepriv->state = FESTATE_IDLE;
957 fe->exit = DVB_FE_NO_EXIT;
958 fepriv->thread = NULL;
961 fe_thread = kthread_run(dvb_frontend_thread, fe,
962 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
963 if (IS_ERR(fe_thread)) {
964 ret = PTR_ERR(fe_thread);
965 dev_warn(fe->dvb->device,
966 "dvb_frontend_start: failed to start kthread (%d)\n",
971 fepriv->thread = fe_thread;
975 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
976 u32 *freq_min, u32 *freq_max)
978 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
980 if (fe->ops.info.frequency_max == 0)
981 *freq_max = fe->ops.tuner_ops.info.frequency_max;
982 else if (fe->ops.tuner_ops.info.frequency_max == 0)
983 *freq_max = fe->ops.info.frequency_max;
985 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
987 if (*freq_min == 0 || *freq_max == 0)
988 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
989 fe->dvb->num, fe->id);
992 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
994 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
998 /* range check: frequency */
999 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
1000 if ((freq_min && c->frequency < freq_min) ||
1001 (freq_max && c->frequency > freq_max)) {
1002 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
1003 fe->dvb->num, fe->id, c->frequency,
1004 freq_min, freq_max);
1008 /* range check: symbol rate */
1009 switch (c->delivery_system) {
1013 case SYS_DVBC_ANNEX_A:
1014 case SYS_DVBC_ANNEX_C:
1015 if ((fe->ops.info.symbol_rate_min &&
1016 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
1017 (fe->ops.info.symbol_rate_max &&
1018 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
1019 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
1020 fe->dvb->num, fe->id, c->symbol_rate,
1021 fe->ops.info.symbol_rate_min,
1022 fe->ops.info.symbol_rate_max);
1032 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
1034 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1038 delsys = c->delivery_system;
1039 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
1040 c->delivery_system = delsys;
1042 c->state = DTV_CLEAR;
1044 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
1045 __func__, c->delivery_system);
1047 c->transmission_mode = TRANSMISSION_MODE_AUTO;
1048 c->bandwidth_hz = 0; /* AUTO */
1049 c->guard_interval = GUARD_INTERVAL_AUTO;
1050 c->hierarchy = HIERARCHY_AUTO;
1052 c->code_rate_HP = FEC_AUTO;
1053 c->code_rate_LP = FEC_AUTO;
1054 c->fec_inner = FEC_AUTO;
1055 c->rolloff = ROLLOFF_AUTO;
1056 c->voltage = SEC_VOLTAGE_OFF;
1057 c->sectone = SEC_TONE_OFF;
1058 c->pilot = PILOT_AUTO;
1060 c->isdbt_partial_reception = 0;
1061 c->isdbt_sb_mode = 0;
1062 c->isdbt_sb_subchannel = 0;
1063 c->isdbt_sb_segment_idx = 0;
1064 c->isdbt_sb_segment_count = 0;
1065 c->isdbt_layer_enabled = 0;
1066 for (i = 0; i < 3; i++) {
1067 c->layer[i].fec = FEC_AUTO;
1068 c->layer[i].modulation = QAM_AUTO;
1069 c->layer[i].interleaving = 0;
1070 c->layer[i].segment_count = 0;
1073 c->stream_id = NO_STREAM_ID_FILTER;
1075 switch (c->delivery_system) {
1079 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1080 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1083 c->modulation = VSB_8;
1086 c->symbol_rate = 28860000;
1087 c->rolloff = ROLLOFF_35;
1088 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1091 c->modulation = QAM_AUTO;
1100 #define _DTV_CMD(n, s, b) \
1108 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1109 _DTV_CMD(DTV_TUNE, 1, 0),
1110 _DTV_CMD(DTV_CLEAR, 1, 0),
1113 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1114 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1115 _DTV_CMD(DTV_MODULATION, 1, 0),
1116 _DTV_CMD(DTV_INVERSION, 1, 0),
1117 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1118 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1119 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1120 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1121 _DTV_CMD(DTV_TONE, 1, 0),
1122 _DTV_CMD(DTV_PILOT, 1, 0),
1123 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1124 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1125 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1126 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1127 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1128 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1129 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1130 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1132 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1133 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1134 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1135 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1136 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1137 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1138 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1139 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1140 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1141 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1142 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1143 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1144 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1145 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1146 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1147 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1148 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1149 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1151 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1152 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1153 _DTV_CMD(DTV_LNA, 1, 0),
1156 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1157 _DTV_CMD(DTV_API_VERSION, 0, 0),
1159 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1161 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1162 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1164 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1165 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1166 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1167 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1168 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1169 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1170 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1171 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1172 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1173 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1174 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1175 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1176 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1178 /* Statistics API */
1179 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1180 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1181 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1182 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1183 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1184 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1185 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1186 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1189 static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
1193 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1194 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1195 __func__, tvp->cmd);
1199 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1200 tvp->cmd, dtv_cmds[tvp->cmd].name);
1202 if (dtv_cmds[tvp->cmd].buffer) {
1203 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1204 __func__, tvp->u.buffer.len);
1206 for(i = 0; i < tvp->u.buffer.len; i++)
1207 dev_dbg(fe->dvb->device,
1208 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1209 __func__, i, tvp->u.buffer.data[i]);
1211 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1216 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1217 * drivers can use a single set_frontend tuning function, regardless of whether
1218 * it's being used for the legacy or new API, reducing code and complexity.
1220 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1221 struct dtv_frontend_properties *c,
1222 const struct dvb_frontend_parameters *p)
1224 c->frequency = p->frequency;
1225 c->inversion = p->inversion;
1227 switch (dvbv3_type(c->delivery_system)) {
1229 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1230 c->symbol_rate = p->u.qpsk.symbol_rate;
1231 c->fec_inner = p->u.qpsk.fec_inner;
1234 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1235 c->symbol_rate = p->u.qam.symbol_rate;
1236 c->fec_inner = p->u.qam.fec_inner;
1237 c->modulation = p->u.qam.modulation;
1240 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1242 switch (p->u.ofdm.bandwidth) {
1243 case BANDWIDTH_10_MHZ:
1244 c->bandwidth_hz = 10000000;
1246 case BANDWIDTH_8_MHZ:
1247 c->bandwidth_hz = 8000000;
1249 case BANDWIDTH_7_MHZ:
1250 c->bandwidth_hz = 7000000;
1252 case BANDWIDTH_6_MHZ:
1253 c->bandwidth_hz = 6000000;
1255 case BANDWIDTH_5_MHZ:
1256 c->bandwidth_hz = 5000000;
1258 case BANDWIDTH_1_712_MHZ:
1259 c->bandwidth_hz = 1712000;
1261 case BANDWIDTH_AUTO:
1262 c->bandwidth_hz = 0;
1265 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1266 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1267 c->modulation = p->u.ofdm.constellation;
1268 c->transmission_mode = p->u.ofdm.transmission_mode;
1269 c->guard_interval = p->u.ofdm.guard_interval;
1270 c->hierarchy = p->u.ofdm.hierarchy_information;
1273 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1274 c->modulation = p->u.vsb.modulation;
1275 if (c->delivery_system == SYS_ATSCMH)
1277 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1278 c->delivery_system = SYS_ATSC;
1280 c->delivery_system = SYS_DVBC_ANNEX_B;
1283 dev_err(fe->dvb->device,
1284 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1285 __func__, c->delivery_system);
1292 /* Ensure the cached values are set correctly in the frontend
1293 * legacy tuning structures, for the advanced tuning API.
1295 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1296 struct dvb_frontend_parameters *p)
1298 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1300 p->frequency = c->frequency;
1301 p->inversion = c->inversion;
1303 switch (dvbv3_type(c->delivery_system)) {
1305 dev_err(fe->dvb->device,
1306 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1307 __func__, c->delivery_system);
1310 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1311 p->u.qpsk.symbol_rate = c->symbol_rate;
1312 p->u.qpsk.fec_inner = c->fec_inner;
1315 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1316 p->u.qam.symbol_rate = c->symbol_rate;
1317 p->u.qam.fec_inner = c->fec_inner;
1318 p->u.qam.modulation = c->modulation;
1321 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1322 switch (c->bandwidth_hz) {
1324 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1327 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1330 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1333 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1336 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1339 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1343 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1345 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1346 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1347 p->u.ofdm.constellation = c->modulation;
1348 p->u.ofdm.transmission_mode = c->transmission_mode;
1349 p->u.ofdm.guard_interval = c->guard_interval;
1350 p->u.ofdm.hierarchy_information = c->hierarchy;
1353 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1354 p->u.vsb.modulation = c->modulation;
1361 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1362 * @fe: struct dvb_frontend pointer
1363 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1364 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1366 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1367 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1368 * If p_out is not null, it will update the DVBv3 params pointed by it.
1370 static int dtv_get_frontend(struct dvb_frontend *fe,
1371 struct dvb_frontend_parameters *p_out)
1375 if (fe->ops.get_frontend) {
1376 r = fe->ops.get_frontend(fe);
1377 if (unlikely(r < 0))
1380 dtv_property_legacy_params_sync(fe, p_out);
1384 /* As everything is in cache, get_frontend fops are always supported */
1388 static int dvb_frontend_ioctl_legacy(struct file *file,
1389 unsigned int cmd, void *parg);
1390 static int dvb_frontend_ioctl_properties(struct file *file,
1391 unsigned int cmd, void *parg);
1393 static int dtv_property_process_get(struct dvb_frontend *fe,
1394 const struct dtv_frontend_properties *c,
1395 struct dtv_property *tvp,
1401 case DTV_ENUM_DELSYS:
1403 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1404 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1407 tvp->u.buffer.len = ncaps;
1410 tvp->u.data = c->frequency;
1412 case DTV_MODULATION:
1413 tvp->u.data = c->modulation;
1415 case DTV_BANDWIDTH_HZ:
1416 tvp->u.data = c->bandwidth_hz;
1419 tvp->u.data = c->inversion;
1421 case DTV_SYMBOL_RATE:
1422 tvp->u.data = c->symbol_rate;
1425 tvp->u.data = c->fec_inner;
1428 tvp->u.data = c->pilot;
1431 tvp->u.data = c->rolloff;
1433 case DTV_DELIVERY_SYSTEM:
1434 tvp->u.data = c->delivery_system;
1437 tvp->u.data = c->voltage;
1440 tvp->u.data = c->sectone;
1442 case DTV_API_VERSION:
1443 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1445 case DTV_CODE_RATE_HP:
1446 tvp->u.data = c->code_rate_HP;
1448 case DTV_CODE_RATE_LP:
1449 tvp->u.data = c->code_rate_LP;
1451 case DTV_GUARD_INTERVAL:
1452 tvp->u.data = c->guard_interval;
1454 case DTV_TRANSMISSION_MODE:
1455 tvp->u.data = c->transmission_mode;
1458 tvp->u.data = c->hierarchy;
1460 case DTV_INTERLEAVING:
1461 tvp->u.data = c->interleaving;
1464 /* ISDB-T Support here */
1465 case DTV_ISDBT_PARTIAL_RECEPTION:
1466 tvp->u.data = c->isdbt_partial_reception;
1468 case DTV_ISDBT_SOUND_BROADCASTING:
1469 tvp->u.data = c->isdbt_sb_mode;
1471 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1472 tvp->u.data = c->isdbt_sb_subchannel;
1474 case DTV_ISDBT_SB_SEGMENT_IDX:
1475 tvp->u.data = c->isdbt_sb_segment_idx;
1477 case DTV_ISDBT_SB_SEGMENT_COUNT:
1478 tvp->u.data = c->isdbt_sb_segment_count;
1480 case DTV_ISDBT_LAYER_ENABLED:
1481 tvp->u.data = c->isdbt_layer_enabled;
1483 case DTV_ISDBT_LAYERA_FEC:
1484 tvp->u.data = c->layer[0].fec;
1486 case DTV_ISDBT_LAYERA_MODULATION:
1487 tvp->u.data = c->layer[0].modulation;
1489 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1490 tvp->u.data = c->layer[0].segment_count;
1492 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1493 tvp->u.data = c->layer[0].interleaving;
1495 case DTV_ISDBT_LAYERB_FEC:
1496 tvp->u.data = c->layer[1].fec;
1498 case DTV_ISDBT_LAYERB_MODULATION:
1499 tvp->u.data = c->layer[1].modulation;
1501 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1502 tvp->u.data = c->layer[1].segment_count;
1504 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1505 tvp->u.data = c->layer[1].interleaving;
1507 case DTV_ISDBT_LAYERC_FEC:
1508 tvp->u.data = c->layer[2].fec;
1510 case DTV_ISDBT_LAYERC_MODULATION:
1511 tvp->u.data = c->layer[2].modulation;
1513 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1514 tvp->u.data = c->layer[2].segment_count;
1516 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1517 tvp->u.data = c->layer[2].interleaving;
1520 /* Multistream support */
1522 case DTV_DVBT2_PLP_ID_LEGACY:
1523 tvp->u.data = c->stream_id;
1527 case DTV_ATSCMH_FIC_VER:
1528 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1530 case DTV_ATSCMH_PARADE_ID:
1531 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1533 case DTV_ATSCMH_NOG:
1534 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1536 case DTV_ATSCMH_TNOG:
1537 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1539 case DTV_ATSCMH_SGN:
1540 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1542 case DTV_ATSCMH_PRC:
1543 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1545 case DTV_ATSCMH_RS_FRAME_MODE:
1546 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1548 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1549 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1551 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1552 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1554 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1555 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1557 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1558 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1560 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1561 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1563 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1564 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1566 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1567 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1569 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1570 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1574 tvp->u.data = c->lna;
1577 /* Fill quality measures */
1578 case DTV_STAT_SIGNAL_STRENGTH:
1579 tvp->u.st = c->strength;
1584 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1585 tvp->u.st = c->pre_bit_error;
1587 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1588 tvp->u.st = c->pre_bit_count;
1590 case DTV_STAT_POST_ERROR_BIT_COUNT:
1591 tvp->u.st = c->post_bit_error;
1593 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1594 tvp->u.st = c->post_bit_count;
1596 case DTV_STAT_ERROR_BLOCK_COUNT:
1597 tvp->u.st = c->block_error;
1599 case DTV_STAT_TOTAL_BLOCK_COUNT:
1600 tvp->u.st = c->block_count;
1603 dev_dbg(fe->dvb->device,
1604 "%s: FE property %d doesn't exist\n",
1605 __func__, tvp->cmd);
1609 /* Allow the frontend to override outgoing properties */
1610 if (fe->ops.get_property) {
1611 r = fe->ops.get_property(fe, tvp);
1616 dtv_property_dump(fe, tvp);
1621 static int dtv_set_frontend(struct dvb_frontend *fe);
1623 static bool is_dvbv3_delsys(u32 delsys)
1627 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1628 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1634 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1635 * @fe: struct frontend;
1636 * @delsys: DVBv5 type that will be used for emulation
1638 * Provides emulation for delivery systems that are compatible with the old
1639 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1640 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1641 * parameters are compatible with DVB-S spec.
1643 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1646 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1648 c->delivery_system = delsys;
1651 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1653 if (c->delivery_system == SYS_ISDBT) {
1654 dev_dbg(fe->dvb->device,
1655 "%s: Using defaults for SYS_ISDBT\n",
1658 if (!c->bandwidth_hz)
1659 c->bandwidth_hz = 6000000;
1661 c->isdbt_partial_reception = 0;
1662 c->isdbt_sb_mode = 0;
1663 c->isdbt_sb_subchannel = 0;
1664 c->isdbt_sb_segment_idx = 0;
1665 c->isdbt_sb_segment_count = 0;
1666 c->isdbt_layer_enabled = 7;
1667 for (i = 0; i < 3; i++) {
1668 c->layer[i].fec = FEC_AUTO;
1669 c->layer[i].modulation = QAM_AUTO;
1670 c->layer[i].interleaving = 0;
1671 c->layer[i].segment_count = 0;
1674 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1675 __func__, c->delivery_system);
1681 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1682 * @fe: frontend struct
1683 * @desired_system: delivery system requested by the user
1685 * A DVBv5 call know what's the desired system it wants. So, set it.
1687 * There are, however, a few known issues with early DVBv5 applications that
1688 * are also handled by this logic:
1690 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1691 * This is an API violation, but, as we don't want to break userspace,
1692 * convert it to the first supported delivery system.
1693 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1694 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1695 * ISDB-T provided backward compat with DVB-T.
1697 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1701 u32 delsys = SYS_UNDEFINED;
1702 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1703 enum dvbv3_emulation_type type;
1706 * It was reported that some old DVBv5 applications were
1707 * filling delivery_system with SYS_UNDEFINED. If this happens,
1708 * assume that the application wants to use the first supported
1711 if (desired_system == SYS_UNDEFINED)
1712 desired_system = fe->ops.delsys[0];
1715 * This is a DVBv5 call. So, it likely knows the supported
1716 * delivery systems. So, check if the desired delivery system is
1720 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1721 if (fe->ops.delsys[ncaps] == desired_system) {
1722 c->delivery_system = desired_system;
1723 dev_dbg(fe->dvb->device,
1724 "%s: Changing delivery system to %d\n",
1725 __func__, desired_system);
1732 * The requested delivery system isn't supported. Maybe userspace
1733 * is requesting a DVBv3 compatible delivery system.
1735 * The emulation only works if the desired system is one of the
1736 * delivery systems supported by DVBv3 API
1738 if (!is_dvbv3_delsys(desired_system)) {
1739 dev_dbg(fe->dvb->device,
1740 "%s: Delivery system %d not supported.\n",
1741 __func__, desired_system);
1745 type = dvbv3_type(desired_system);
1748 * Get the last non-DVBv3 delivery system that has the same type
1749 * of the desired system
1752 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1753 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1754 delsys = fe->ops.delsys[ncaps];
1758 /* There's nothing compatible with the desired delivery system */
1759 if (delsys == SYS_UNDEFINED) {
1760 dev_dbg(fe->dvb->device,
1761 "%s: Delivery system %d not supported on emulation mode.\n",
1762 __func__, desired_system);
1766 dev_dbg(fe->dvb->device,
1767 "%s: Using delivery system %d emulated as if it were %d\n",
1768 __func__, delsys, desired_system);
1770 return emulate_delivery_system(fe, desired_system);
1774 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1775 * @fe: frontend struct
1777 * A DVBv3 call doesn't know what's the desired system it wants. It also
1778 * doesn't allow to switch between different types. Due to that, userspace
1779 * should use DVBv5 instead.
1780 * However, in order to avoid breaking userspace API, limited backward
1781 * compatibility support is provided.
1783 * There are some delivery systems that are incompatible with DVBv3 calls.
1785 * This routine should work fine for frontends that support just one delivery
1788 * For frontends that support multiple frontends:
1789 * 1) It defaults to use the first supported delivery system. There's an
1790 * userspace application that allows changing it at runtime;
1792 * 2) If the current delivery system is not compatible with DVBv3, it gets
1793 * the first one that it is compatible.
1795 * NOTE: in order for this to work with applications like Kaffeine that
1796 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1797 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1798 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1801 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1804 u32 delsys = SYS_UNDEFINED;
1805 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1807 /* If not set yet, defaults to the first supported delivery system */
1808 if (c->delivery_system == SYS_UNDEFINED)
1809 c->delivery_system = fe->ops.delsys[0];
1812 * Trivial case: just use the current one, if it already a DVBv3
1815 if (is_dvbv3_delsys(c->delivery_system)) {
1816 dev_dbg(fe->dvb->device,
1817 "%s: Using delivery system to %d\n",
1818 __func__, c->delivery_system);
1823 * Seek for the first delivery system that it is compatible with a
1827 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1828 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1829 delsys = fe->ops.delsys[ncaps];
1834 if (delsys == SYS_UNDEFINED) {
1835 dev_dbg(fe->dvb->device,
1836 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1840 return emulate_delivery_system(fe, delsys);
1843 static int dtv_property_process_set(struct dvb_frontend *fe,
1844 struct dtv_property *tvp,
1848 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1850 /* Allow the frontend to validate incoming properties */
1851 if (fe->ops.set_property) {
1852 r = fe->ops.set_property(fe, tvp);
1860 * Reset a cache of data specific to the frontend here. This does
1861 * not effect hardware.
1863 dvb_frontend_clear_cache(fe);
1866 /* interpret the cache of data, build either a traditional frontend
1867 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1870 c->state = tvp->cmd;
1871 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1874 r = dtv_set_frontend(fe);
1877 c->frequency = tvp->u.data;
1879 case DTV_MODULATION:
1880 c->modulation = tvp->u.data;
1882 case DTV_BANDWIDTH_HZ:
1883 c->bandwidth_hz = tvp->u.data;
1886 c->inversion = tvp->u.data;
1888 case DTV_SYMBOL_RATE:
1889 c->symbol_rate = tvp->u.data;
1892 c->fec_inner = tvp->u.data;
1895 c->pilot = tvp->u.data;
1898 c->rolloff = tvp->u.data;
1900 case DTV_DELIVERY_SYSTEM:
1901 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1904 c->voltage = tvp->u.data;
1905 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1906 (void *)c->voltage);
1909 c->sectone = tvp->u.data;
1910 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1911 (void *)c->sectone);
1913 case DTV_CODE_RATE_HP:
1914 c->code_rate_HP = tvp->u.data;
1916 case DTV_CODE_RATE_LP:
1917 c->code_rate_LP = tvp->u.data;
1919 case DTV_GUARD_INTERVAL:
1920 c->guard_interval = tvp->u.data;
1922 case DTV_TRANSMISSION_MODE:
1923 c->transmission_mode = tvp->u.data;
1926 c->hierarchy = tvp->u.data;
1928 case DTV_INTERLEAVING:
1929 c->interleaving = tvp->u.data;
1932 /* ISDB-T Support here */
1933 case DTV_ISDBT_PARTIAL_RECEPTION:
1934 c->isdbt_partial_reception = tvp->u.data;
1936 case DTV_ISDBT_SOUND_BROADCASTING:
1937 c->isdbt_sb_mode = tvp->u.data;
1939 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1940 c->isdbt_sb_subchannel = tvp->u.data;
1942 case DTV_ISDBT_SB_SEGMENT_IDX:
1943 c->isdbt_sb_segment_idx = tvp->u.data;
1945 case DTV_ISDBT_SB_SEGMENT_COUNT:
1946 c->isdbt_sb_segment_count = tvp->u.data;
1948 case DTV_ISDBT_LAYER_ENABLED:
1949 c->isdbt_layer_enabled = tvp->u.data;
1951 case DTV_ISDBT_LAYERA_FEC:
1952 c->layer[0].fec = tvp->u.data;
1954 case DTV_ISDBT_LAYERA_MODULATION:
1955 c->layer[0].modulation = tvp->u.data;
1957 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1958 c->layer[0].segment_count = tvp->u.data;
1960 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1961 c->layer[0].interleaving = tvp->u.data;
1963 case DTV_ISDBT_LAYERB_FEC:
1964 c->layer[1].fec = tvp->u.data;
1966 case DTV_ISDBT_LAYERB_MODULATION:
1967 c->layer[1].modulation = tvp->u.data;
1969 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1970 c->layer[1].segment_count = tvp->u.data;
1972 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1973 c->layer[1].interleaving = tvp->u.data;
1975 case DTV_ISDBT_LAYERC_FEC:
1976 c->layer[2].fec = tvp->u.data;
1978 case DTV_ISDBT_LAYERC_MODULATION:
1979 c->layer[2].modulation = tvp->u.data;
1981 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1982 c->layer[2].segment_count = tvp->u.data;
1984 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1985 c->layer[2].interleaving = tvp->u.data;
1988 /* Multistream support */
1990 case DTV_DVBT2_PLP_ID_LEGACY:
1991 c->stream_id = tvp->u.data;
1995 case DTV_ATSCMH_PARADE_ID:
1996 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1998 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1999 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
2003 c->lna = tvp->u.data;
2004 if (fe->ops.set_lna)
2005 r = fe->ops.set_lna(fe);
2017 static int dvb_frontend_ioctl(struct file *file,
2018 unsigned int cmd, void *parg)
2020 struct dvb_device *dvbdev = file->private_data;
2021 struct dvb_frontend *fe = dvbdev->priv;
2022 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2023 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2024 int err = -EOPNOTSUPP;
2026 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
2027 if (down_interruptible(&fepriv->sem))
2028 return -ERESTARTSYS;
2030 if (fe->exit != DVB_FE_NO_EXIT) {
2035 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
2036 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
2037 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
2042 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
2043 err = dvb_frontend_ioctl_properties(file, cmd, parg);
2045 c->state = DTV_UNDEFINED;
2046 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
2053 static int dvb_frontend_ioctl_properties(struct file *file,
2054 unsigned int cmd, void *parg)
2056 struct dvb_device *dvbdev = file->private_data;
2057 struct dvb_frontend *fe = dvbdev->priv;
2058 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2059 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2062 struct dtv_properties *tvps = parg;
2063 struct dtv_property *tvp = NULL;
2066 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2068 if (cmd == FE_SET_PROPERTY) {
2069 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2070 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2072 /* Put an arbitrary limit on the number of messages that can
2073 * be sent at once */
2074 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2077 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2083 if (copy_from_user(tvp, (void __user *)tvps->props,
2084 tvps->num * sizeof(struct dtv_property))) {
2089 for (i = 0; i < tvps->num; i++) {
2090 err = dtv_property_process_set(fe, tvp + i, file);
2093 (tvp + i)->result = err;
2096 if (c->state == DTV_TUNE)
2097 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
2099 } else if (cmd == FE_GET_PROPERTY) {
2100 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2101 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2103 /* Put an arbitrary limit on the number of messages that can
2104 * be sent at once */
2105 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2108 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2114 if (copy_from_user(tvp, (void __user *)tvps->props,
2115 tvps->num * sizeof(struct dtv_property))) {
2121 * Fills the cache out struct with the cache contents, plus
2122 * the data retrieved from get_frontend, if the frontend
2123 * is not idle. Otherwise, returns the cached content
2125 if (fepriv->state != FESTATE_IDLE) {
2126 err = dtv_get_frontend(fe, NULL);
2130 for (i = 0; i < tvps->num; i++) {
2131 err = dtv_property_process_get(fe, c, tvp + i, file);
2134 (tvp + i)->result = err;
2137 if (copy_to_user((void __user *)tvps->props, tvp,
2138 tvps->num * sizeof(struct dtv_property))) {
2151 static int dtv_set_frontend(struct dvb_frontend *fe)
2153 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2154 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2155 struct dvb_frontend_tune_settings fetunesettings;
2158 if (dvb_frontend_check_parameters(fe) < 0)
2162 * Initialize output parameters to match the values given by
2163 * the user. FE_SET_FRONTEND triggers an initial frontend event
2164 * with status = 0, which copies output parameters to userspace.
2166 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2169 * Be sure that the bandwidth will be filled for all
2170 * non-satellite systems, as tuners need to know what
2171 * low pass/Nyquist half filter should be applied, in
2172 * order to avoid inter-channel noise.
2174 * ISDB-T and DVB-T/T2 already sets bandwidth.
2175 * ATSC and DVB-C don't set, so, the core should fill it.
2177 * On DVB-C Annex A and C, the bandwidth is a function of
2178 * the roll-off and symbol rate. Annex B defines different
2179 * roll-off factors depending on the modulation. Fortunately,
2180 * Annex B is only used with 6MHz, so there's no need to
2183 * While not officially supported, a side effect of handling it at
2184 * the cache level is that a program could retrieve the bandwidth
2185 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2187 switch (c->delivery_system) {
2189 case SYS_DVBC_ANNEX_B:
2190 c->bandwidth_hz = 6000000;
2192 case SYS_DVBC_ANNEX_A:
2195 case SYS_DVBC_ANNEX_C:
2204 switch (c->rolloff) {
2220 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
2222 /* force auto frequency inversion if requested */
2223 if (dvb_force_auto_inversion)
2224 c->inversion = INVERSION_AUTO;
2227 * without hierarchical coding code_rate_LP is irrelevant,
2228 * so we tolerate the otherwise invalid FEC_NONE setting
2230 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2231 c->code_rate_LP = FEC_AUTO;
2233 /* get frontend-specific tuning settings */
2234 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2235 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2236 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2237 fepriv->max_drift = fetunesettings.max_drift;
2238 fepriv->step_size = fetunesettings.step_size;
2240 /* default values */
2241 switch (c->delivery_system) {
2246 case SYS_DVBC_ANNEX_A:
2247 case SYS_DVBC_ANNEX_C:
2248 fepriv->min_delay = HZ / 20;
2249 fepriv->step_size = c->symbol_rate / 16000;
2250 fepriv->max_drift = c->symbol_rate / 2000;
2256 fepriv->min_delay = HZ / 20;
2257 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2258 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2262 * FIXME: This sounds wrong! if freqency_stepsize is
2263 * defined by the frontend, why not use it???
2265 fepriv->min_delay = HZ / 20;
2266 fepriv->step_size = 0; /* no zigzag */
2267 fepriv->max_drift = 0;
2271 if (dvb_override_tune_delay > 0)
2272 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2274 fepriv->state = FESTATE_RETUNE;
2276 /* Request the search algorithm to search */
2277 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2279 dvb_frontend_clear_events(fe);
2280 dvb_frontend_add_event(fe, 0);
2281 dvb_frontend_wakeup(fe);
2288 static int dvb_frontend_ioctl_legacy(struct file *file,
2289 unsigned int cmd, void *parg)
2291 struct dvb_device *dvbdev = file->private_data;
2292 struct dvb_frontend *fe = dvbdev->priv;
2293 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2294 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2295 int err = -EOPNOTSUPP;
2299 struct dvb_frontend_info* info = parg;
2301 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2302 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2305 * Associate the 4 delivery systems supported by DVBv3
2306 * API with their DVBv5 counterpart. For the other standards,
2307 * use the closest type, assuming that it would hopefully
2308 * work with a DVBv3 application.
2309 * It should be noticed that, on multi-frontend devices with
2310 * different types (terrestrial and cable, for example),
2311 * a pure DVBv3 application won't be able to use all delivery
2312 * systems. Yet, changing the DVBv5 cache to the other delivery
2313 * system should be enough for making it work.
2315 switch (dvbv3_type(c->delivery_system)) {
2317 info->type = FE_QPSK;
2320 info->type = FE_ATSC;
2323 info->type = FE_QAM;
2326 info->type = FE_OFDM;
2329 dev_err(fe->dvb->device,
2330 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2331 __func__, c->delivery_system);
2332 fe->ops.info.type = FE_OFDM;
2334 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2335 __func__, c->delivery_system, fe->ops.info.type);
2337 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2338 * do it, it is done for it. */
2339 info->caps |= FE_CAN_INVERSION_AUTO;
2344 case FE_READ_STATUS: {
2345 enum fe_status *status = parg;
2347 /* if retune was requested but hasn't occurred yet, prevent
2348 * that user get signal state from previous tuning */
2349 if (fepriv->state == FESTATE_RETUNE ||
2350 fepriv->state == FESTATE_ERROR) {
2356 if (fe->ops.read_status)
2357 err = fe->ops.read_status(fe, status);
2362 if (fe->ops.read_ber) {
2364 err = fe->ops.read_ber(fe, (__u32 *) parg);
2370 case FE_READ_SIGNAL_STRENGTH:
2371 if (fe->ops.read_signal_strength) {
2373 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2380 if (fe->ops.read_snr) {
2382 err = fe->ops.read_snr(fe, (__u16 *) parg);
2388 case FE_READ_UNCORRECTED_BLOCKS:
2389 if (fe->ops.read_ucblocks) {
2391 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2397 case FE_DISEQC_RESET_OVERLOAD:
2398 if (fe->ops.diseqc_reset_overload) {
2399 err = fe->ops.diseqc_reset_overload(fe);
2400 fepriv->state = FESTATE_DISEQC;
2405 case FE_DISEQC_SEND_MASTER_CMD:
2406 if (fe->ops.diseqc_send_master_cmd) {
2407 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2408 fepriv->state = FESTATE_DISEQC;
2413 case FE_DISEQC_SEND_BURST:
2414 if (fe->ops.diseqc_send_burst) {
2415 err = fe->ops.diseqc_send_burst(fe,
2416 (enum fe_sec_mini_cmd)parg);
2417 fepriv->state = FESTATE_DISEQC;
2423 if (fe->ops.set_tone) {
2424 err = fe->ops.set_tone(fe,
2425 (enum fe_sec_tone_mode)parg);
2426 fepriv->tone = (enum fe_sec_tone_mode)parg;
2427 fepriv->state = FESTATE_DISEQC;
2432 case FE_SET_VOLTAGE:
2433 if (fe->ops.set_voltage) {
2434 err = fe->ops.set_voltage(fe,
2435 (enum fe_sec_voltage)parg);
2436 fepriv->voltage = (enum fe_sec_voltage)parg;
2437 fepriv->state = FESTATE_DISEQC;
2442 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2443 if (fe->ops.dishnetwork_send_legacy_command) {
2444 err = fe->ops.dishnetwork_send_legacy_command(fe,
2445 (unsigned long)parg);
2446 fepriv->state = FESTATE_DISEQC;
2448 } else if (fe->ops.set_voltage) {
2450 * NOTE: This is a fallback condition. Some frontends
2451 * (stv0299 for instance) take longer than 8msec to
2452 * respond to a set_voltage command. Those switches
2453 * need custom routines to switch properly. For all
2454 * other frontends, the following should work ok.
2455 * Dish network legacy switches (as used by Dish500)
2456 * are controlled by sending 9-bit command words
2457 * spaced 8msec apart.
2458 * the actual command word is switch/port dependent
2459 * so it is up to the userspace application to send
2460 * the right command.
2461 * The command must always start with a '0' after
2462 * initialization, so parg is 8 bits and does not
2463 * include the initialization or start bit
2465 unsigned long swcmd = ((unsigned long) parg) << 1;
2466 struct timeval nexttime;
2467 struct timeval tv[10];
2470 if (dvb_frontend_debug)
2471 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2472 do_gettimeofday(&nexttime);
2473 if (dvb_frontend_debug)
2475 /* before sending a command, initialize by sending
2476 * a 32ms 18V to the switch
2478 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2479 dvb_frontend_sleep_until(&nexttime, 32000);
2481 for (i = 0; i < 9; i++) {
2482 if (dvb_frontend_debug)
2483 do_gettimeofday(&tv[i + 1]);
2484 if ((swcmd & 0x01) != last) {
2485 /* set voltage to (last ? 13V : 18V) */
2486 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2487 last = (last) ? 0 : 1;
2491 dvb_frontend_sleep_until(&nexttime, 8000);
2493 if (dvb_frontend_debug) {
2494 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2495 __func__, fe->dvb->num);
2496 for (i = 1; i < 10; i++)
2497 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2500 fepriv->state = FESTATE_DISEQC;
2505 case FE_DISEQC_RECV_SLAVE_REPLY:
2506 if (fe->ops.diseqc_recv_slave_reply)
2507 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2510 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2511 if (fe->ops.enable_high_lnb_voltage)
2512 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2515 case FE_SET_FRONTEND:
2516 err = dvbv3_set_delivery_system(fe);
2520 err = dtv_property_cache_sync(fe, c, parg);
2523 err = dtv_set_frontend(fe);
2526 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2529 case FE_GET_FRONTEND:
2530 err = dtv_get_frontend(fe, parg);
2533 case FE_SET_FRONTEND_TUNE_MODE:
2534 fepriv->tune_mode_flags = (unsigned long) parg;
2543 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2545 struct dvb_device *dvbdev = file->private_data;
2546 struct dvb_frontend *fe = dvbdev->priv;
2547 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2549 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2551 poll_wait (file, &fepriv->events.wait_queue, wait);
2553 if (fepriv->events.eventw != fepriv->events.eventr)
2554 return (POLLIN | POLLRDNORM | POLLPRI);
2559 static int dvb_frontend_open(struct inode *inode, struct file *file)
2561 struct dvb_device *dvbdev = file->private_data;
2562 struct dvb_frontend *fe = dvbdev->priv;
2563 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2564 struct dvb_adapter *adapter = fe->dvb;
2567 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2568 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2571 if (adapter->mfe_shared) {
2572 mutex_lock (&adapter->mfe_lock);
2574 if (adapter->mfe_dvbdev == NULL)
2575 adapter->mfe_dvbdev = dvbdev;
2577 else if (adapter->mfe_dvbdev != dvbdev) {
2579 *mfedev = adapter->mfe_dvbdev;
2581 *mfe = mfedev->priv;
2582 struct dvb_frontend_private
2583 *mfepriv = mfe->frontend_priv;
2584 int mferetry = (dvb_mfe_wait_time << 1);
2586 mutex_unlock (&adapter->mfe_lock);
2587 while (mferetry-- && (mfedev->users != -1 ||
2588 mfepriv->thread != NULL)) {
2589 if(msleep_interruptible(500)) {
2590 if(signal_pending(current))
2595 mutex_lock (&adapter->mfe_lock);
2596 if(adapter->mfe_dvbdev != dvbdev) {
2597 mfedev = adapter->mfe_dvbdev;
2599 mfepriv = mfe->frontend_priv;
2600 if (mfedev->users != -1 ||
2601 mfepriv->thread != NULL) {
2602 mutex_unlock (&adapter->mfe_lock);
2605 adapter->mfe_dvbdev = dvbdev;
2610 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2611 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2614 /* If we took control of the bus, we need to force
2615 reinitialization. This is because many ts_bus_ctrl()
2616 functions strobe the RESET pin on the demod, and if the
2617 frontend thread already exists then the dvb_init() routine
2618 won't get called (which is what usually does initial
2619 register configuration). */
2620 fepriv->reinitialise = 1;
2623 if ((ret = dvb_generic_open (inode, file)) < 0)
2626 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2627 /* normal tune mode when opened R/W */
2628 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2630 fepriv->voltage = -1;
2632 ret = dvb_frontend_start (fe);
2636 /* empty event queue */
2637 fepriv->events.eventr = fepriv->events.eventw = 0;
2640 if (adapter->mfe_shared)
2641 mutex_unlock (&adapter->mfe_lock);
2645 dvb_generic_release(inode, file);
2647 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2648 fe->ops.ts_bus_ctrl(fe, 0);
2650 if (adapter->mfe_shared)
2651 mutex_unlock (&adapter->mfe_lock);
2655 static int dvb_frontend_release(struct inode *inode, struct file *file)
2657 struct dvb_device *dvbdev = file->private_data;
2658 struct dvb_frontend *fe = dvbdev->priv;
2659 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2662 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2664 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2665 fepriv->release_jiffies = jiffies;
2669 ret = dvb_generic_release (inode, file);
2671 if (dvbdev->users == -1) {
2672 wake_up(&fepriv->wait_queue);
2673 if (fe->exit != DVB_FE_NO_EXIT)
2674 wake_up(&dvbdev->wait_queue);
2675 if (fe->ops.ts_bus_ctrl)
2676 fe->ops.ts_bus_ctrl(fe, 0);
2682 static const struct file_operations dvb_frontend_fops = {
2683 .owner = THIS_MODULE,
2684 .unlocked_ioctl = dvb_generic_ioctl,
2685 .poll = dvb_frontend_poll,
2686 .open = dvb_frontend_open,
2687 .release = dvb_frontend_release,
2688 .llseek = noop_llseek,
2691 int dvb_frontend_suspend(struct dvb_frontend *fe)
2695 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2698 if (fe->ops.tuner_ops.suspend)
2699 ret = fe->ops.tuner_ops.suspend(fe);
2700 else if (fe->ops.tuner_ops.sleep)
2701 ret = fe->ops.tuner_ops.sleep(fe);
2704 ret = fe->ops.sleep(fe);
2708 EXPORT_SYMBOL(dvb_frontend_suspend);
2710 int dvb_frontend_resume(struct dvb_frontend *fe)
2712 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2715 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2718 fe->exit = DVB_FE_DEVICE_RESUME;
2720 ret = fe->ops.init(fe);
2722 if (fe->ops.tuner_ops.resume)
2723 ret = fe->ops.tuner_ops.resume(fe);
2724 else if (fe->ops.tuner_ops.init)
2725 ret = fe->ops.tuner_ops.init(fe);
2727 fe->exit = DVB_FE_NO_EXIT;
2728 fepriv->state = FESTATE_RETUNE;
2729 dvb_frontend_wakeup(fe);
2733 EXPORT_SYMBOL(dvb_frontend_resume);
2735 int dvb_register_frontend(struct dvb_adapter* dvb,
2736 struct dvb_frontend* fe)
2738 struct dvb_frontend_private *fepriv;
2739 const struct dvb_device dvbdev_template = {
2743 .fops = &dvb_frontend_fops,
2744 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2745 .name = fe->ops.info.name,
2747 .kernel_ioctl = dvb_frontend_ioctl
2750 dev_dbg(dvb->device, "%s:\n", __func__);
2752 if (mutex_lock_interruptible(&frontend_mutex))
2753 return -ERESTARTSYS;
2755 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2756 if (fe->frontend_priv == NULL) {
2757 mutex_unlock(&frontend_mutex);
2760 fepriv = fe->frontend_priv;
2762 sema_init(&fepriv->sem, 1);
2763 init_waitqueue_head (&fepriv->wait_queue);
2764 init_waitqueue_head (&fepriv->events.wait_queue);
2765 mutex_init(&fepriv->events.mtx);
2767 fepriv->inversion = INVERSION_OFF;
2769 dev_info(fe->dvb->device,
2770 "DVB: registering adapter %i frontend %i (%s)...\n",
2771 fe->dvb->num, fe->id, fe->ops.info.name);
2773 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2774 fe, DVB_DEVICE_FRONTEND);
2777 * Initialize the cache to the proper values according with the
2778 * first supported delivery system (ops->delsys[0])
2781 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2782 dvb_frontend_clear_cache(fe);
2784 mutex_unlock(&frontend_mutex);
2787 EXPORT_SYMBOL(dvb_register_frontend);
2789 int dvb_unregister_frontend(struct dvb_frontend* fe)
2791 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2792 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2794 mutex_lock(&frontend_mutex);
2795 dvb_frontend_stop (fe);
2796 mutex_unlock(&frontend_mutex);
2798 if (fepriv->dvbdev->users < -1)
2799 wait_event(fepriv->dvbdev->wait_queue,
2800 fepriv->dvbdev->users==-1);
2802 mutex_lock(&frontend_mutex);
2803 dvb_unregister_device (fepriv->dvbdev);
2805 /* fe is invalid now */
2807 mutex_unlock(&frontend_mutex);
2810 EXPORT_SYMBOL(dvb_unregister_frontend);
2812 #ifdef CONFIG_MEDIA_ATTACH
2813 void dvb_frontend_detach(struct dvb_frontend* fe)
2817 if (fe->ops.release_sec) {
2818 fe->ops.release_sec(fe);
2819 dvb_detach(fe->ops.release_sec);
2821 if (fe->ops.tuner_ops.release) {
2822 fe->ops.tuner_ops.release(fe);
2823 dvb_detach(fe->ops.tuner_ops.release);
2825 if (fe->ops.analog_ops.release) {
2826 fe->ops.analog_ops.release(fe);
2827 dvb_detach(fe->ops.analog_ops.release);
2829 ptr = (void*)fe->ops.release;
2831 fe->ops.release(fe);
2836 void dvb_frontend_detach(struct dvb_frontend* fe)
2838 if (fe->ops.release_sec)
2839 fe->ops.release_sec(fe);
2840 if (fe->ops.tuner_ops.release)
2841 fe->ops.tuner_ops.release(fe);
2842 if (fe->ops.analog_ops.release)
2843 fe->ops.analog_ops.release(fe);
2844 if (fe->ops.release)
2845 fe->ops.release(fe);
2848 EXPORT_SYMBOL(dvb_frontend_detach);