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;
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, fe_status_t status)
203 struct dvb_frontend_private *fepriv = fe->frontend_priv;
204 struct dvb_fe_events *events = &fepriv->events;
205 struct dvb_frontend_event *e;
208 dev_dbg(fe->dvb->device, "%s:\n", __func__);
210 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
211 dtv_get_frontend(fe, &fepriv->parameters_out);
213 mutex_lock(&events->mtx);
215 wp = (events->eventw + 1) % MAX_EVENT;
216 if (wp == events->eventr) {
217 events->overflow = 1;
218 events->eventr = (events->eventr + 1) % MAX_EVENT;
221 e = &events->events[events->eventw];
223 e->parameters = fepriv->parameters_out;
227 mutex_unlock(&events->mtx);
229 wake_up_interruptible (&events->wait_queue);
232 static int dvb_frontend_get_event(struct dvb_frontend *fe,
233 struct dvb_frontend_event *event, int flags)
235 struct dvb_frontend_private *fepriv = fe->frontend_priv;
236 struct dvb_fe_events *events = &fepriv->events;
238 dev_dbg(fe->dvb->device, "%s:\n", __func__);
240 if (events->overflow) {
241 events->overflow = 0;
245 if (events->eventw == events->eventr) {
248 if (flags & O_NONBLOCK)
253 ret = wait_event_interruptible (events->wait_queue,
254 events->eventw != events->eventr);
256 if (down_interruptible (&fepriv->sem))
263 mutex_lock(&events->mtx);
264 *event = events->events[events->eventr];
265 events->eventr = (events->eventr + 1) % MAX_EVENT;
266 mutex_unlock(&events->mtx);
271 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
273 struct dvb_frontend_private *fepriv = fe->frontend_priv;
274 struct dvb_fe_events *events = &fepriv->events;
276 mutex_lock(&events->mtx);
277 events->eventr = events->eventw;
278 mutex_unlock(&events->mtx);
281 static void dvb_frontend_init(struct dvb_frontend *fe)
283 dev_dbg(fe->dvb->device,
284 "%s: initialising adapter %i frontend %i (%s)...\n",
285 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
289 if (fe->ops.tuner_ops.init) {
290 if (fe->ops.i2c_gate_ctrl)
291 fe->ops.i2c_gate_ctrl(fe, 1);
292 fe->ops.tuner_ops.init(fe);
293 if (fe->ops.i2c_gate_ctrl)
294 fe->ops.i2c_gate_ctrl(fe, 0);
298 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
300 struct dvb_frontend_private *fepriv = fe->frontend_priv;
302 fepriv->reinitialise = 1;
303 dvb_frontend_wakeup(fe);
305 EXPORT_SYMBOL(dvb_frontend_reinitialise);
307 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
310 struct dvb_frontend *fe = fepriv->dvbdev->priv;
312 dev_dbg(fe->dvb->device, "%s:\n", __func__);
315 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
317 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
319 q2 = fepriv->quality - 128;
322 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
326 * Performs automatic twiddling of frontend parameters.
328 * @param fe The frontend concerned.
329 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
330 * @returns Number of complete iterations that have been performed.
332 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
337 struct dvb_frontend_private *fepriv = fe->frontend_priv;
338 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
339 int original_inversion = c->inversion;
340 u32 original_frequency = c->frequency;
342 /* are we using autoinversion? */
343 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
344 (c->inversion == INVERSION_AUTO));
346 /* setup parameters correctly */
348 /* calculate the lnb_drift */
349 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
351 /* wrap the auto_step if we've exceeded the maximum drift */
352 if (fepriv->lnb_drift > fepriv->max_drift) {
353 fepriv->auto_step = 0;
354 fepriv->auto_sub_step = 0;
355 fepriv->lnb_drift = 0;
358 /* perform inversion and +/- zigzag */
359 switch(fepriv->auto_sub_step) {
361 /* try with the current inversion and current drift setting */
366 if (!autoinversion) break;
368 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
373 if (fepriv->lnb_drift == 0) break;
375 fepriv->lnb_drift = -fepriv->lnb_drift;
380 if (fepriv->lnb_drift == 0) break;
381 if (!autoinversion) break;
383 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
384 fepriv->lnb_drift = -fepriv->lnb_drift;
390 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
394 if (!ready) fepriv->auto_sub_step++;
397 /* if this attempt would hit where we started, indicate a complete
398 * iteration has occurred */
399 if ((fepriv->auto_step == fepriv->started_auto_step) &&
400 (fepriv->auto_sub_step == 0) && check_wrapped) {
404 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
405 "auto_sub_step:%i started_auto_step:%i\n",
406 __func__, fepriv->lnb_drift, fepriv->inversion,
407 fepriv->auto_step, fepriv->auto_sub_step,
408 fepriv->started_auto_step);
410 /* set the frontend itself */
411 c->frequency += fepriv->lnb_drift;
413 c->inversion = fepriv->inversion;
415 if (fe->ops.set_frontend)
416 fe_set_err = fe->ops.set_frontend(fe);
418 if (fe_set_err < 0) {
419 fepriv->state = FESTATE_ERROR;
423 c->frequency = original_frequency;
424 c->inversion = original_inversion;
426 fepriv->auto_sub_step++;
430 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
434 struct dvb_frontend_private *fepriv = fe->frontend_priv;
435 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
437 /* if we've got no parameters, just keep idling */
438 if (fepriv->state & FESTATE_IDLE) {
439 fepriv->delay = 3*HZ;
444 /* in SCAN mode, we just set the frontend when asked and leave it alone */
445 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
446 if (fepriv->state & FESTATE_RETUNE) {
448 if (fe->ops.set_frontend)
449 retval = fe->ops.set_frontend(fe);
452 fepriv->state = FESTATE_ERROR;
454 fepriv->state = FESTATE_TUNED;
456 fepriv->delay = 3*HZ;
461 /* get the frontend status */
462 if (fepriv->state & FESTATE_RETUNE) {
465 if (fe->ops.read_status)
466 fe->ops.read_status(fe, &s);
467 if (s != fepriv->status) {
468 dvb_frontend_add_event(fe, s);
473 /* if we're not tuned, and we have a lock, move to the TUNED state */
474 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
475 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
476 fepriv->state = FESTATE_TUNED;
478 /* if we're tuned, then we have determined the correct inversion */
479 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
480 (c->inversion == INVERSION_AUTO)) {
481 c->inversion = fepriv->inversion;
486 /* if we are tuned already, check we're still locked */
487 if (fepriv->state & FESTATE_TUNED) {
488 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
490 /* we're tuned, and the lock is still good... */
491 if (s & FE_HAS_LOCK) {
493 } else { /* if we _WERE_ tuned, but now don't have a lock */
494 fepriv->state = FESTATE_ZIGZAG_FAST;
495 fepriv->started_auto_step = fepriv->auto_step;
496 fepriv->check_wrapped = 0;
500 /* don't actually do anything if we're in the LOSTLOCK state,
501 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
502 if ((fepriv->state & FESTATE_LOSTLOCK) &&
503 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
504 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
508 /* don't do anything if we're in the DISEQC state, since this
509 * might be someone with a motorized dish controlled by DISEQC.
510 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
511 if (fepriv->state & FESTATE_DISEQC) {
512 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
516 /* if we're in the RETUNE state, set everything up for a brand
517 * new scan, keeping the current inversion setting, as the next
518 * tune is _very_ likely to require the same */
519 if (fepriv->state & FESTATE_RETUNE) {
520 fepriv->lnb_drift = 0;
521 fepriv->auto_step = 0;
522 fepriv->auto_sub_step = 0;
523 fepriv->started_auto_step = 0;
524 fepriv->check_wrapped = 0;
528 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
529 fepriv->delay = fepriv->min_delay;
532 retval = dvb_frontend_swzigzag_autotune(fe,
533 fepriv->check_wrapped);
537 /* OK, if we've run out of trials at the fast speed.
538 * Drop back to slow for the _next_ attempt */
539 fepriv->state = FESTATE_SEARCHING_SLOW;
540 fepriv->started_auto_step = fepriv->auto_step;
543 fepriv->check_wrapped = 1;
545 /* if we've just retuned, enter the ZIGZAG_FAST state.
546 * This ensures we cannot return from an
547 * FE_SET_FRONTEND ioctl before the first frontend tune
549 if (fepriv->state & FESTATE_RETUNE) {
550 fepriv->state = FESTATE_TUNING_FAST;
555 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
556 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
558 /* Note: don't bother checking for wrapping; we stay in this
559 * state until we get a lock */
560 dvb_frontend_swzigzag_autotune(fe, 0);
564 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
566 struct dvb_frontend_private *fepriv = fe->frontend_priv;
568 if (fe->exit != DVB_FE_NO_EXIT)
571 if (fepriv->dvbdev->writers == 1)
572 if (time_after_eq(jiffies, fepriv->release_jiffies +
573 dvb_shutdown_timeout * HZ))
579 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
581 struct dvb_frontend_private *fepriv = fe->frontend_priv;
583 if (fepriv->wakeup) {
587 return dvb_frontend_is_exiting(fe);
590 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
592 struct dvb_frontend_private *fepriv = fe->frontend_priv;
595 wake_up_interruptible(&fepriv->wait_queue);
599 * dvb_enable_media_tuner() - tries to enable the DVB tuner
601 * @fe: struct dvb_frontend pointer
603 * This function ensures that just one media tuner is enabled for a given
604 * frontend. It has two different behaviors:
605 * - For trivial devices with just one tuner:
606 * it just enables the existing tuner->fe link
607 * - For devices with more than one tuner:
608 * It is up to the driver to implement the logic that will enable one tuner
609 * and disable the other ones. However, if more than one tuner is enabled for
610 * the same frontend, it will print an error message and return -EINVAL.
612 * At return, it will return the error code returned by media_entity_setup_link,
613 * or 0 if everything is OK, if no tuner is linked to the frontend or if the
616 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
617 static int dvb_enable_media_tuner(struct dvb_frontend *fe)
619 struct dvb_frontend_private *fepriv = fe->frontend_priv;
620 struct dvb_adapter *adapter = fe->dvb;
621 struct media_device *mdev = adapter->mdev;
622 struct media_entity *entity, *source;
623 struct media_link *link, *found_link = NULL;
624 int i, ret, n_links = 0, active_links = 0;
626 fepriv->pipe_start_entity = NULL;
631 entity = fepriv->dvbdev->entity;
632 fepriv->pipe_start_entity = entity;
634 for (i = 0; i < entity->num_links; i++) {
635 link = &entity->links[i];
636 if (link->sink->entity == entity) {
639 if (link->flags & MEDIA_LNK_FL_ENABLED)
644 if (!n_links || active_links == 1 || !found_link)
648 * If a frontend has more than one tuner linked, it is up to the driver
649 * to select with one will be the active one, as the frontend core can't
650 * guess. If the driver doesn't do that, it is a bug.
652 if (n_links > 1 && active_links != 1) {
653 dev_err(fe->dvb->device,
654 "WARNING: there are %d active links among %d tuners. This is a driver's bug!\n",
655 active_links, n_links);
659 source = found_link->source->entity;
660 fepriv->pipe_start_entity = source;
661 for (i = 0; i < source->num_links; i++) {
662 struct media_entity *sink;
665 link = &source->links[i];
666 sink = link->sink->entity;
669 flags = MEDIA_LNK_FL_ENABLED;
671 ret = media_entity_setup_link(link, flags);
673 dev_err(fe->dvb->device,
674 "Couldn't change link %s->%s to %s. Error %d\n",
675 source->name, sink->name,
676 flags ? "enabled" : "disabled",
680 dev_dbg(fe->dvb->device,
681 "link %s->%s was %s\n",
682 source->name, sink->name,
683 flags ? "ENABLED" : "disabled");
689 static int dvb_frontend_thread(void *data)
691 struct dvb_frontend *fe = data;
692 struct dvb_frontend_private *fepriv = fe->frontend_priv;
694 enum dvbfe_algo algo;
695 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
699 bool re_tune = false;
700 bool semheld = false;
702 dev_dbg(fe->dvb->device, "%s:\n", __func__);
704 fepriv->check_wrapped = 0;
706 fepriv->delay = 3*HZ;
709 fepriv->reinitialise = 0;
711 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
712 ret = dvb_enable_media_tuner(fe);
714 /* FIXME: return an error if it fails */
715 dev_info(fe->dvb->device,
716 "proceeding with FE task\n");
717 } else if (fepriv->pipe_start_entity) {
718 ret = media_entity_pipeline_start(fepriv->pipe_start_entity,
725 dvb_frontend_init(fe);
729 up(&fepriv->sem); /* is locked when we enter the thread... */
731 wait_event_interruptible_timeout(fepriv->wait_queue,
732 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
733 || freezing(current),
736 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
737 /* got signal or quitting */
738 if (!down_interruptible(&fepriv->sem))
740 fe->exit = DVB_FE_NORMAL_EXIT;
747 if (down_interruptible(&fepriv->sem))
750 if (fepriv->reinitialise) {
751 dvb_frontend_init(fe);
752 if (fe->ops.set_tone && fepriv->tone != -1)
753 fe->ops.set_tone(fe, fepriv->tone);
754 if (fe->ops.set_voltage && fepriv->voltage != -1)
755 fe->ops.set_voltage(fe, fepriv->voltage);
756 fepriv->reinitialise = 0;
759 /* do an iteration of the tuning loop */
760 if (fe->ops.get_frontend_algo) {
761 algo = fe->ops.get_frontend_algo(fe);
764 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
766 if (fepriv->state & FESTATE_RETUNE) {
767 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
769 fepriv->state = FESTATE_TUNED;
775 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
777 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
778 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
779 dvb_frontend_add_event(fe, s);
784 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
785 dvb_frontend_swzigzag(fe);
787 case DVBFE_ALGO_CUSTOM:
788 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
789 if (fepriv->state & FESTATE_RETUNE) {
790 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
791 fepriv->state = FESTATE_TUNED;
793 /* Case where we are going to search for a carrier
794 * User asked us to retune again for some reason, possibly
795 * requesting a search with a new set of parameters
797 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
798 if (fe->ops.search) {
799 fepriv->algo_status = fe->ops.search(fe);
800 /* We did do a search as was requested, the flags are
801 * now unset as well and has the flags wrt to search.
804 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
807 /* Track the carrier if the search was successful */
808 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
809 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
810 fepriv->delay = HZ / 2;
812 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
813 fe->ops.read_status(fe, &s);
814 if (s != fepriv->status) {
815 dvb_frontend_add_event(fe, s); /* update event list */
817 if (!(s & FE_HAS_LOCK)) {
818 fepriv->delay = HZ / 10;
819 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
821 fepriv->delay = 60 * HZ;
826 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
830 dvb_frontend_swzigzag(fe);
834 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
835 if (fepriv->pipe_start_entity)
836 media_entity_pipeline_stop(fepriv->pipe_start_entity);
837 fepriv->pipe_start_entity = NULL;
840 if (dvb_powerdown_on_sleep) {
841 if (fe->ops.set_voltage)
842 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
843 if (fe->ops.tuner_ops.sleep) {
844 if (fe->ops.i2c_gate_ctrl)
845 fe->ops.i2c_gate_ctrl(fe, 1);
846 fe->ops.tuner_ops.sleep(fe);
847 if (fe->ops.i2c_gate_ctrl)
848 fe->ops.i2c_gate_ctrl(fe, 0);
854 fepriv->thread = NULL;
855 if (kthread_should_stop())
856 fe->exit = DVB_FE_DEVICE_REMOVED;
858 fe->exit = DVB_FE_NO_EXIT;
863 dvb_frontend_wakeup(fe);
867 static void dvb_frontend_stop(struct dvb_frontend *fe)
869 struct dvb_frontend_private *fepriv = fe->frontend_priv;
871 dev_dbg(fe->dvb->device, "%s:\n", __func__);
873 if (fe->exit != DVB_FE_DEVICE_REMOVED)
874 fe->exit = DVB_FE_NORMAL_EXIT;
880 kthread_stop(fepriv->thread);
882 sema_init(&fepriv->sem, 1);
883 fepriv->state = FESTATE_IDLE;
885 /* paranoia check in case a signal arrived */
887 dev_warn(fe->dvb->device,
888 "dvb_frontend_stop: warning: thread %p won't exit\n",
892 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
894 return ((curtime.tv_usec < lasttime.tv_usec) ?
895 1000000 - lasttime.tv_usec + curtime.tv_usec :
896 curtime.tv_usec - lasttime.tv_usec);
898 EXPORT_SYMBOL(timeval_usec_diff);
900 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
902 curtime->tv_usec += add_usec;
903 if (curtime->tv_usec >= 1000000) {
904 curtime->tv_usec -= 1000000;
910 * Sleep until gettimeofday() > waketime + add_usec
911 * This needs to be as precise as possible, but as the delay is
912 * usually between 2ms and 32ms, it is done using a scheduled msleep
913 * followed by usleep (normally a busy-wait loop) for the remainder
915 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
917 struct timeval lasttime;
920 timeval_usec_add(waketime, add_usec);
922 do_gettimeofday(&lasttime);
923 delta = timeval_usec_diff(lasttime, *waketime);
925 msleep((delta - 1500) / 1000);
926 do_gettimeofday(&lasttime);
927 newdelta = timeval_usec_diff(lasttime, *waketime);
928 delta = (newdelta > delta) ? 0 : newdelta;
933 EXPORT_SYMBOL(dvb_frontend_sleep_until);
935 static int dvb_frontend_start(struct dvb_frontend *fe)
938 struct dvb_frontend_private *fepriv = fe->frontend_priv;
939 struct task_struct *fe_thread;
941 dev_dbg(fe->dvb->device, "%s:\n", __func__);
943 if (fepriv->thread) {
944 if (fe->exit == DVB_FE_NO_EXIT)
947 dvb_frontend_stop (fe);
950 if (signal_pending(current))
952 if (down_interruptible (&fepriv->sem))
955 fepriv->state = FESTATE_IDLE;
956 fe->exit = DVB_FE_NO_EXIT;
957 fepriv->thread = NULL;
960 fe_thread = kthread_run(dvb_frontend_thread, fe,
961 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
962 if (IS_ERR(fe_thread)) {
963 ret = PTR_ERR(fe_thread);
964 dev_warn(fe->dvb->device,
965 "dvb_frontend_start: failed to start kthread (%d)\n",
970 fepriv->thread = fe_thread;
974 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
975 u32 *freq_min, u32 *freq_max)
977 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
979 if (fe->ops.info.frequency_max == 0)
980 *freq_max = fe->ops.tuner_ops.info.frequency_max;
981 else if (fe->ops.tuner_ops.info.frequency_max == 0)
982 *freq_max = fe->ops.info.frequency_max;
984 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
986 if (*freq_min == 0 || *freq_max == 0)
987 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
988 fe->dvb->num, fe->id);
991 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
993 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
997 /* range check: frequency */
998 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
999 if ((freq_min && c->frequency < freq_min) ||
1000 (freq_max && c->frequency > freq_max)) {
1001 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
1002 fe->dvb->num, fe->id, c->frequency,
1003 freq_min, freq_max);
1007 /* range check: symbol rate */
1008 switch (c->delivery_system) {
1012 case SYS_DVBC_ANNEX_A:
1013 case SYS_DVBC_ANNEX_C:
1014 if ((fe->ops.info.symbol_rate_min &&
1015 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
1016 (fe->ops.info.symbol_rate_max &&
1017 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
1018 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
1019 fe->dvb->num, fe->id, c->symbol_rate,
1020 fe->ops.info.symbol_rate_min,
1021 fe->ops.info.symbol_rate_max);
1031 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
1033 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1037 delsys = c->delivery_system;
1038 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
1039 c->delivery_system = delsys;
1041 c->state = DTV_CLEAR;
1043 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
1044 __func__, c->delivery_system);
1046 c->transmission_mode = TRANSMISSION_MODE_AUTO;
1047 c->bandwidth_hz = 0; /* AUTO */
1048 c->guard_interval = GUARD_INTERVAL_AUTO;
1049 c->hierarchy = HIERARCHY_AUTO;
1051 c->code_rate_HP = FEC_AUTO;
1052 c->code_rate_LP = FEC_AUTO;
1053 c->fec_inner = FEC_AUTO;
1054 c->rolloff = ROLLOFF_AUTO;
1055 c->voltage = SEC_VOLTAGE_OFF;
1056 c->sectone = SEC_TONE_OFF;
1057 c->pilot = PILOT_AUTO;
1059 c->isdbt_partial_reception = 0;
1060 c->isdbt_sb_mode = 0;
1061 c->isdbt_sb_subchannel = 0;
1062 c->isdbt_sb_segment_idx = 0;
1063 c->isdbt_sb_segment_count = 0;
1064 c->isdbt_layer_enabled = 0;
1065 for (i = 0; i < 3; i++) {
1066 c->layer[i].fec = FEC_AUTO;
1067 c->layer[i].modulation = QAM_AUTO;
1068 c->layer[i].interleaving = 0;
1069 c->layer[i].segment_count = 0;
1072 c->stream_id = NO_STREAM_ID_FILTER;
1074 switch (c->delivery_system) {
1078 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1079 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1082 c->modulation = VSB_8;
1085 c->symbol_rate = 28860000;
1086 c->rolloff = ROLLOFF_35;
1087 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1090 c->modulation = QAM_AUTO;
1099 #define _DTV_CMD(n, s, b) \
1107 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1108 _DTV_CMD(DTV_TUNE, 1, 0),
1109 _DTV_CMD(DTV_CLEAR, 1, 0),
1112 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1113 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1114 _DTV_CMD(DTV_MODULATION, 1, 0),
1115 _DTV_CMD(DTV_INVERSION, 1, 0),
1116 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1117 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1118 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1119 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1120 _DTV_CMD(DTV_TONE, 1, 0),
1121 _DTV_CMD(DTV_PILOT, 1, 0),
1122 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1123 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1124 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1125 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1126 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1127 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1128 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1129 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1131 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1132 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1133 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1134 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1135 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1136 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1137 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1138 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1139 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1140 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1141 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1142 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1143 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1144 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1145 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1146 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1147 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1148 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1150 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1151 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1152 _DTV_CMD(DTV_LNA, 1, 0),
1155 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1156 _DTV_CMD(DTV_API_VERSION, 0, 0),
1158 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1160 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1161 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1163 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1164 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1165 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1166 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1167 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1168 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1169 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1170 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1171 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1172 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1173 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1174 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1175 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1177 /* Statistics API */
1178 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1179 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1180 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1181 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1182 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1183 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1184 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1185 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1188 static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
1192 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1193 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1194 __func__, tvp->cmd);
1198 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1199 tvp->cmd, dtv_cmds[tvp->cmd].name);
1201 if (dtv_cmds[tvp->cmd].buffer) {
1202 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1203 __func__, tvp->u.buffer.len);
1205 for(i = 0; i < tvp->u.buffer.len; i++)
1206 dev_dbg(fe->dvb->device,
1207 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1208 __func__, i, tvp->u.buffer.data[i]);
1210 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1215 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1216 * drivers can use a single set_frontend tuning function, regardless of whether
1217 * it's being used for the legacy or new API, reducing code and complexity.
1219 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1220 struct dtv_frontend_properties *c,
1221 const struct dvb_frontend_parameters *p)
1223 c->frequency = p->frequency;
1224 c->inversion = p->inversion;
1226 switch (dvbv3_type(c->delivery_system)) {
1228 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1229 c->symbol_rate = p->u.qpsk.symbol_rate;
1230 c->fec_inner = p->u.qpsk.fec_inner;
1233 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1234 c->symbol_rate = p->u.qam.symbol_rate;
1235 c->fec_inner = p->u.qam.fec_inner;
1236 c->modulation = p->u.qam.modulation;
1239 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1241 switch (p->u.ofdm.bandwidth) {
1242 case BANDWIDTH_10_MHZ:
1243 c->bandwidth_hz = 10000000;
1245 case BANDWIDTH_8_MHZ:
1246 c->bandwidth_hz = 8000000;
1248 case BANDWIDTH_7_MHZ:
1249 c->bandwidth_hz = 7000000;
1251 case BANDWIDTH_6_MHZ:
1252 c->bandwidth_hz = 6000000;
1254 case BANDWIDTH_5_MHZ:
1255 c->bandwidth_hz = 5000000;
1257 case BANDWIDTH_1_712_MHZ:
1258 c->bandwidth_hz = 1712000;
1260 case BANDWIDTH_AUTO:
1261 c->bandwidth_hz = 0;
1264 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1265 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1266 c->modulation = p->u.ofdm.constellation;
1267 c->transmission_mode = p->u.ofdm.transmission_mode;
1268 c->guard_interval = p->u.ofdm.guard_interval;
1269 c->hierarchy = p->u.ofdm.hierarchy_information;
1272 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1273 c->modulation = p->u.vsb.modulation;
1274 if (c->delivery_system == SYS_ATSCMH)
1276 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1277 c->delivery_system = SYS_ATSC;
1279 c->delivery_system = SYS_DVBC_ANNEX_B;
1282 dev_err(fe->dvb->device,
1283 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1284 __func__, c->delivery_system);
1291 /* Ensure the cached values are set correctly in the frontend
1292 * legacy tuning structures, for the advanced tuning API.
1294 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1295 struct dvb_frontend_parameters *p)
1297 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1299 p->frequency = c->frequency;
1300 p->inversion = c->inversion;
1302 switch (dvbv3_type(c->delivery_system)) {
1304 dev_err(fe->dvb->device,
1305 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1306 __func__, c->delivery_system);
1309 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1310 p->u.qpsk.symbol_rate = c->symbol_rate;
1311 p->u.qpsk.fec_inner = c->fec_inner;
1314 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1315 p->u.qam.symbol_rate = c->symbol_rate;
1316 p->u.qam.fec_inner = c->fec_inner;
1317 p->u.qam.modulation = c->modulation;
1320 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1321 switch (c->bandwidth_hz) {
1323 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1326 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1329 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1332 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1335 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1338 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1342 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1344 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1345 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1346 p->u.ofdm.constellation = c->modulation;
1347 p->u.ofdm.transmission_mode = c->transmission_mode;
1348 p->u.ofdm.guard_interval = c->guard_interval;
1349 p->u.ofdm.hierarchy_information = c->hierarchy;
1352 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1353 p->u.vsb.modulation = c->modulation;
1360 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1361 * @fe: struct dvb_frontend pointer
1362 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1363 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1365 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1366 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1367 * If p_out is not null, it will update the DVBv3 params pointed by it.
1369 static int dtv_get_frontend(struct dvb_frontend *fe,
1370 struct dvb_frontend_parameters *p_out)
1374 if (fe->ops.get_frontend) {
1375 r = fe->ops.get_frontend(fe);
1376 if (unlikely(r < 0))
1379 dtv_property_legacy_params_sync(fe, p_out);
1383 /* As everything is in cache, get_frontend fops are always supported */
1387 static int dvb_frontend_ioctl_legacy(struct file *file,
1388 unsigned int cmd, void *parg);
1389 static int dvb_frontend_ioctl_properties(struct file *file,
1390 unsigned int cmd, void *parg);
1392 static int dtv_property_process_get(struct dvb_frontend *fe,
1393 const struct dtv_frontend_properties *c,
1394 struct dtv_property *tvp,
1400 case DTV_ENUM_DELSYS:
1402 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1403 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1406 tvp->u.buffer.len = ncaps;
1409 tvp->u.data = c->frequency;
1411 case DTV_MODULATION:
1412 tvp->u.data = c->modulation;
1414 case DTV_BANDWIDTH_HZ:
1415 tvp->u.data = c->bandwidth_hz;
1418 tvp->u.data = c->inversion;
1420 case DTV_SYMBOL_RATE:
1421 tvp->u.data = c->symbol_rate;
1424 tvp->u.data = c->fec_inner;
1427 tvp->u.data = c->pilot;
1430 tvp->u.data = c->rolloff;
1432 case DTV_DELIVERY_SYSTEM:
1433 tvp->u.data = c->delivery_system;
1436 tvp->u.data = c->voltage;
1439 tvp->u.data = c->sectone;
1441 case DTV_API_VERSION:
1442 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1444 case DTV_CODE_RATE_HP:
1445 tvp->u.data = c->code_rate_HP;
1447 case DTV_CODE_RATE_LP:
1448 tvp->u.data = c->code_rate_LP;
1450 case DTV_GUARD_INTERVAL:
1451 tvp->u.data = c->guard_interval;
1453 case DTV_TRANSMISSION_MODE:
1454 tvp->u.data = c->transmission_mode;
1457 tvp->u.data = c->hierarchy;
1459 case DTV_INTERLEAVING:
1460 tvp->u.data = c->interleaving;
1463 /* ISDB-T Support here */
1464 case DTV_ISDBT_PARTIAL_RECEPTION:
1465 tvp->u.data = c->isdbt_partial_reception;
1467 case DTV_ISDBT_SOUND_BROADCASTING:
1468 tvp->u.data = c->isdbt_sb_mode;
1470 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1471 tvp->u.data = c->isdbt_sb_subchannel;
1473 case DTV_ISDBT_SB_SEGMENT_IDX:
1474 tvp->u.data = c->isdbt_sb_segment_idx;
1476 case DTV_ISDBT_SB_SEGMENT_COUNT:
1477 tvp->u.data = c->isdbt_sb_segment_count;
1479 case DTV_ISDBT_LAYER_ENABLED:
1480 tvp->u.data = c->isdbt_layer_enabled;
1482 case DTV_ISDBT_LAYERA_FEC:
1483 tvp->u.data = c->layer[0].fec;
1485 case DTV_ISDBT_LAYERA_MODULATION:
1486 tvp->u.data = c->layer[0].modulation;
1488 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1489 tvp->u.data = c->layer[0].segment_count;
1491 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1492 tvp->u.data = c->layer[0].interleaving;
1494 case DTV_ISDBT_LAYERB_FEC:
1495 tvp->u.data = c->layer[1].fec;
1497 case DTV_ISDBT_LAYERB_MODULATION:
1498 tvp->u.data = c->layer[1].modulation;
1500 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1501 tvp->u.data = c->layer[1].segment_count;
1503 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1504 tvp->u.data = c->layer[1].interleaving;
1506 case DTV_ISDBT_LAYERC_FEC:
1507 tvp->u.data = c->layer[2].fec;
1509 case DTV_ISDBT_LAYERC_MODULATION:
1510 tvp->u.data = c->layer[2].modulation;
1512 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1513 tvp->u.data = c->layer[2].segment_count;
1515 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1516 tvp->u.data = c->layer[2].interleaving;
1519 /* Multistream support */
1521 case DTV_DVBT2_PLP_ID_LEGACY:
1522 tvp->u.data = c->stream_id;
1526 case DTV_ATSCMH_FIC_VER:
1527 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1529 case DTV_ATSCMH_PARADE_ID:
1530 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1532 case DTV_ATSCMH_NOG:
1533 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1535 case DTV_ATSCMH_TNOG:
1536 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1538 case DTV_ATSCMH_SGN:
1539 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1541 case DTV_ATSCMH_PRC:
1542 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1544 case DTV_ATSCMH_RS_FRAME_MODE:
1545 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1547 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1548 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1550 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1551 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1553 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1554 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1556 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1557 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1559 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1560 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1562 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1563 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1565 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1566 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1568 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1569 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1573 tvp->u.data = c->lna;
1576 /* Fill quality measures */
1577 case DTV_STAT_SIGNAL_STRENGTH:
1578 tvp->u.st = c->strength;
1583 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1584 tvp->u.st = c->pre_bit_error;
1586 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1587 tvp->u.st = c->pre_bit_count;
1589 case DTV_STAT_POST_ERROR_BIT_COUNT:
1590 tvp->u.st = c->post_bit_error;
1592 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1593 tvp->u.st = c->post_bit_count;
1595 case DTV_STAT_ERROR_BLOCK_COUNT:
1596 tvp->u.st = c->block_error;
1598 case DTV_STAT_TOTAL_BLOCK_COUNT:
1599 tvp->u.st = c->block_count;
1602 dev_dbg(fe->dvb->device,
1603 "%s: FE property %d doesn't exist\n",
1604 __func__, tvp->cmd);
1608 /* Allow the frontend to override outgoing properties */
1609 if (fe->ops.get_property) {
1610 r = fe->ops.get_property(fe, tvp);
1615 dtv_property_dump(fe, tvp);
1620 static int dtv_set_frontend(struct dvb_frontend *fe);
1622 static bool is_dvbv3_delsys(u32 delsys)
1626 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1627 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1633 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1634 * @fe: struct frontend;
1635 * @delsys: DVBv5 type that will be used for emulation
1637 * Provides emulation for delivery systems that are compatible with the old
1638 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1639 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1640 * parameters are compatible with DVB-S spec.
1642 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1645 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1647 c->delivery_system = delsys;
1650 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1652 if (c->delivery_system == SYS_ISDBT) {
1653 dev_dbg(fe->dvb->device,
1654 "%s: Using defaults for SYS_ISDBT\n",
1657 if (!c->bandwidth_hz)
1658 c->bandwidth_hz = 6000000;
1660 c->isdbt_partial_reception = 0;
1661 c->isdbt_sb_mode = 0;
1662 c->isdbt_sb_subchannel = 0;
1663 c->isdbt_sb_segment_idx = 0;
1664 c->isdbt_sb_segment_count = 0;
1665 c->isdbt_layer_enabled = 7;
1666 for (i = 0; i < 3; i++) {
1667 c->layer[i].fec = FEC_AUTO;
1668 c->layer[i].modulation = QAM_AUTO;
1669 c->layer[i].interleaving = 0;
1670 c->layer[i].segment_count = 0;
1673 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1674 __func__, c->delivery_system);
1680 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1681 * @fe: frontend struct
1682 * @desired_system: delivery system requested by the user
1684 * A DVBv5 call know what's the desired system it wants. So, set it.
1686 * There are, however, a few known issues with early DVBv5 applications that
1687 * are also handled by this logic:
1689 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1690 * This is an API violation, but, as we don't want to break userspace,
1691 * convert it to the first supported delivery system.
1692 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1693 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1694 * ISDB-T provided backward compat with DVB-T.
1696 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1700 u32 delsys = SYS_UNDEFINED;
1701 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1702 enum dvbv3_emulation_type type;
1705 * It was reported that some old DVBv5 applications were
1706 * filling delivery_system with SYS_UNDEFINED. If this happens,
1707 * assume that the application wants to use the first supported
1710 if (desired_system == SYS_UNDEFINED)
1711 desired_system = fe->ops.delsys[0];
1714 * This is a DVBv5 call. So, it likely knows the supported
1715 * delivery systems. So, check if the desired delivery system is
1719 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1720 if (fe->ops.delsys[ncaps] == desired_system) {
1721 c->delivery_system = desired_system;
1722 dev_dbg(fe->dvb->device,
1723 "%s: Changing delivery system to %d\n",
1724 __func__, desired_system);
1731 * The requested delivery system isn't supported. Maybe userspace
1732 * is requesting a DVBv3 compatible delivery system.
1734 * The emulation only works if the desired system is one of the
1735 * delivery systems supported by DVBv3 API
1737 if (!is_dvbv3_delsys(desired_system)) {
1738 dev_dbg(fe->dvb->device,
1739 "%s: Delivery system %d not supported.\n",
1740 __func__, desired_system);
1744 type = dvbv3_type(desired_system);
1747 * Get the last non-DVBv3 delivery system that has the same type
1748 * of the desired system
1751 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1752 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1753 delsys = fe->ops.delsys[ncaps];
1757 /* There's nothing compatible with the desired delivery system */
1758 if (delsys == SYS_UNDEFINED) {
1759 dev_dbg(fe->dvb->device,
1760 "%s: Delivery system %d not supported on emulation mode.\n",
1761 __func__, desired_system);
1765 dev_dbg(fe->dvb->device,
1766 "%s: Using delivery system %d emulated as if it were %d\n",
1767 __func__, delsys, desired_system);
1769 return emulate_delivery_system(fe, desired_system);
1773 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1774 * @fe: frontend struct
1776 * A DVBv3 call doesn't know what's the desired system it wants. It also
1777 * doesn't allow to switch between different types. Due to that, userspace
1778 * should use DVBv5 instead.
1779 * However, in order to avoid breaking userspace API, limited backward
1780 * compatibility support is provided.
1782 * There are some delivery systems that are incompatible with DVBv3 calls.
1784 * This routine should work fine for frontends that support just one delivery
1787 * For frontends that support multiple frontends:
1788 * 1) It defaults to use the first supported delivery system. There's an
1789 * userspace application that allows changing it at runtime;
1791 * 2) If the current delivery system is not compatible with DVBv3, it gets
1792 * the first one that it is compatible.
1794 * NOTE: in order for this to work with applications like Kaffeine that
1795 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1796 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1797 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1800 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1803 u32 delsys = SYS_UNDEFINED;
1804 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1806 /* If not set yet, defaults to the first supported delivery system */
1807 if (c->delivery_system == SYS_UNDEFINED)
1808 c->delivery_system = fe->ops.delsys[0];
1811 * Trivial case: just use the current one, if it already a DVBv3
1814 if (is_dvbv3_delsys(c->delivery_system)) {
1815 dev_dbg(fe->dvb->device,
1816 "%s: Using delivery system to %d\n",
1817 __func__, c->delivery_system);
1822 * Seek for the first delivery system that it is compatible with a
1826 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1827 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1828 delsys = fe->ops.delsys[ncaps];
1833 if (delsys == SYS_UNDEFINED) {
1834 dev_dbg(fe->dvb->device,
1835 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1839 return emulate_delivery_system(fe, delsys);
1842 static int dtv_property_process_set(struct dvb_frontend *fe,
1843 struct dtv_property *tvp,
1847 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1849 /* Allow the frontend to validate incoming properties */
1850 if (fe->ops.set_property) {
1851 r = fe->ops.set_property(fe, tvp);
1859 * Reset a cache of data specific to the frontend here. This does
1860 * not effect hardware.
1862 dvb_frontend_clear_cache(fe);
1865 /* interpret the cache of data, build either a traditional frontend
1866 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1869 c->state = tvp->cmd;
1870 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1873 r = dtv_set_frontend(fe);
1876 c->frequency = tvp->u.data;
1878 case DTV_MODULATION:
1879 c->modulation = tvp->u.data;
1881 case DTV_BANDWIDTH_HZ:
1882 c->bandwidth_hz = tvp->u.data;
1885 c->inversion = tvp->u.data;
1887 case DTV_SYMBOL_RATE:
1888 c->symbol_rate = tvp->u.data;
1891 c->fec_inner = tvp->u.data;
1894 c->pilot = tvp->u.data;
1897 c->rolloff = tvp->u.data;
1899 case DTV_DELIVERY_SYSTEM:
1900 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1903 c->voltage = tvp->u.data;
1904 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1905 (void *)c->voltage);
1908 c->sectone = tvp->u.data;
1909 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1910 (void *)c->sectone);
1912 case DTV_CODE_RATE_HP:
1913 c->code_rate_HP = tvp->u.data;
1915 case DTV_CODE_RATE_LP:
1916 c->code_rate_LP = tvp->u.data;
1918 case DTV_GUARD_INTERVAL:
1919 c->guard_interval = tvp->u.data;
1921 case DTV_TRANSMISSION_MODE:
1922 c->transmission_mode = tvp->u.data;
1925 c->hierarchy = tvp->u.data;
1927 case DTV_INTERLEAVING:
1928 c->interleaving = tvp->u.data;
1931 /* ISDB-T Support here */
1932 case DTV_ISDBT_PARTIAL_RECEPTION:
1933 c->isdbt_partial_reception = tvp->u.data;
1935 case DTV_ISDBT_SOUND_BROADCASTING:
1936 c->isdbt_sb_mode = tvp->u.data;
1938 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1939 c->isdbt_sb_subchannel = tvp->u.data;
1941 case DTV_ISDBT_SB_SEGMENT_IDX:
1942 c->isdbt_sb_segment_idx = tvp->u.data;
1944 case DTV_ISDBT_SB_SEGMENT_COUNT:
1945 c->isdbt_sb_segment_count = tvp->u.data;
1947 case DTV_ISDBT_LAYER_ENABLED:
1948 c->isdbt_layer_enabled = tvp->u.data;
1950 case DTV_ISDBT_LAYERA_FEC:
1951 c->layer[0].fec = tvp->u.data;
1953 case DTV_ISDBT_LAYERA_MODULATION:
1954 c->layer[0].modulation = tvp->u.data;
1956 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1957 c->layer[0].segment_count = tvp->u.data;
1959 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1960 c->layer[0].interleaving = tvp->u.data;
1962 case DTV_ISDBT_LAYERB_FEC:
1963 c->layer[1].fec = tvp->u.data;
1965 case DTV_ISDBT_LAYERB_MODULATION:
1966 c->layer[1].modulation = tvp->u.data;
1968 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1969 c->layer[1].segment_count = tvp->u.data;
1971 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1972 c->layer[1].interleaving = tvp->u.data;
1974 case DTV_ISDBT_LAYERC_FEC:
1975 c->layer[2].fec = tvp->u.data;
1977 case DTV_ISDBT_LAYERC_MODULATION:
1978 c->layer[2].modulation = tvp->u.data;
1980 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1981 c->layer[2].segment_count = tvp->u.data;
1983 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1984 c->layer[2].interleaving = tvp->u.data;
1987 /* Multistream support */
1989 case DTV_DVBT2_PLP_ID_LEGACY:
1990 c->stream_id = tvp->u.data;
1994 case DTV_ATSCMH_PARADE_ID:
1995 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1997 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1998 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
2002 c->lna = tvp->u.data;
2003 if (fe->ops.set_lna)
2004 r = fe->ops.set_lna(fe);
2016 static int dvb_frontend_ioctl(struct file *file,
2017 unsigned int cmd, void *parg)
2019 struct dvb_device *dvbdev = file->private_data;
2020 struct dvb_frontend *fe = dvbdev->priv;
2021 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2022 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2023 int err = -EOPNOTSUPP;
2025 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
2026 if (down_interruptible(&fepriv->sem))
2027 return -ERESTARTSYS;
2029 if (fe->exit != DVB_FE_NO_EXIT) {
2034 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
2035 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
2036 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
2041 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
2042 err = dvb_frontend_ioctl_properties(file, cmd, parg);
2044 c->state = DTV_UNDEFINED;
2045 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
2052 static int dvb_frontend_ioctl_properties(struct file *file,
2053 unsigned int cmd, void *parg)
2055 struct dvb_device *dvbdev = file->private_data;
2056 struct dvb_frontend *fe = dvbdev->priv;
2057 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2058 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2061 struct dtv_properties *tvps = parg;
2062 struct dtv_property *tvp = NULL;
2065 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2067 if (cmd == FE_SET_PROPERTY) {
2068 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2069 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2071 /* Put an arbitrary limit on the number of messages that can
2072 * be sent at once */
2073 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2076 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2082 if (copy_from_user(tvp, (void __user *)tvps->props,
2083 tvps->num * sizeof(struct dtv_property))) {
2088 for (i = 0; i < tvps->num; i++) {
2089 err = dtv_property_process_set(fe, tvp + i, file);
2092 (tvp + i)->result = err;
2095 if (c->state == DTV_TUNE)
2096 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
2098 } else if (cmd == FE_GET_PROPERTY) {
2099 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2100 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2102 /* Put an arbitrary limit on the number of messages that can
2103 * be sent at once */
2104 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2107 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2113 if (copy_from_user(tvp, (void __user *)tvps->props,
2114 tvps->num * sizeof(struct dtv_property))) {
2120 * Fills the cache out struct with the cache contents, plus
2121 * the data retrieved from get_frontend, if the frontend
2122 * is not idle. Otherwise, returns the cached content
2124 if (fepriv->state != FESTATE_IDLE) {
2125 err = dtv_get_frontend(fe, NULL);
2129 for (i = 0; i < tvps->num; i++) {
2130 err = dtv_property_process_get(fe, c, tvp + i, file);
2133 (tvp + i)->result = err;
2136 if (copy_to_user((void __user *)tvps->props, tvp,
2137 tvps->num * sizeof(struct dtv_property))) {
2150 static int dtv_set_frontend(struct dvb_frontend *fe)
2152 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2153 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2154 struct dvb_frontend_tune_settings fetunesettings;
2157 if (dvb_frontend_check_parameters(fe) < 0)
2161 * Initialize output parameters to match the values given by
2162 * the user. FE_SET_FRONTEND triggers an initial frontend event
2163 * with status = 0, which copies output parameters to userspace.
2165 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2168 * Be sure that the bandwidth will be filled for all
2169 * non-satellite systems, as tuners need to know what
2170 * low pass/Nyquist half filter should be applied, in
2171 * order to avoid inter-channel noise.
2173 * ISDB-T and DVB-T/T2 already sets bandwidth.
2174 * ATSC and DVB-C don't set, so, the core should fill it.
2176 * On DVB-C Annex A and C, the bandwidth is a function of
2177 * the roll-off and symbol rate. Annex B defines different
2178 * roll-off factors depending on the modulation. Fortunately,
2179 * Annex B is only used with 6MHz, so there's no need to
2182 * While not officially supported, a side effect of handling it at
2183 * the cache level is that a program could retrieve the bandwidth
2184 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2186 switch (c->delivery_system) {
2188 case SYS_DVBC_ANNEX_B:
2189 c->bandwidth_hz = 6000000;
2191 case SYS_DVBC_ANNEX_A:
2194 case SYS_DVBC_ANNEX_C:
2203 switch (c->rolloff) {
2219 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
2221 /* force auto frequency inversion if requested */
2222 if (dvb_force_auto_inversion)
2223 c->inversion = INVERSION_AUTO;
2226 * without hierarchical coding code_rate_LP is irrelevant,
2227 * so we tolerate the otherwise invalid FEC_NONE setting
2229 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2230 c->code_rate_LP = FEC_AUTO;
2232 /* get frontend-specific tuning settings */
2233 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2234 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2235 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2236 fepriv->max_drift = fetunesettings.max_drift;
2237 fepriv->step_size = fetunesettings.step_size;
2239 /* default values */
2240 switch (c->delivery_system) {
2245 case SYS_DVBC_ANNEX_A:
2246 case SYS_DVBC_ANNEX_C:
2247 fepriv->min_delay = HZ / 20;
2248 fepriv->step_size = c->symbol_rate / 16000;
2249 fepriv->max_drift = c->symbol_rate / 2000;
2255 fepriv->min_delay = HZ / 20;
2256 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2257 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2261 * FIXME: This sounds wrong! if freqency_stepsize is
2262 * defined by the frontend, why not use it???
2264 fepriv->min_delay = HZ / 20;
2265 fepriv->step_size = 0; /* no zigzag */
2266 fepriv->max_drift = 0;
2270 if (dvb_override_tune_delay > 0)
2271 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2273 fepriv->state = FESTATE_RETUNE;
2275 /* Request the search algorithm to search */
2276 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2278 dvb_frontend_clear_events(fe);
2279 dvb_frontend_add_event(fe, 0);
2280 dvb_frontend_wakeup(fe);
2287 static int dvb_frontend_ioctl_legacy(struct file *file,
2288 unsigned int cmd, void *parg)
2290 struct dvb_device *dvbdev = file->private_data;
2291 struct dvb_frontend *fe = dvbdev->priv;
2292 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2293 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2294 int err = -EOPNOTSUPP;
2298 struct dvb_frontend_info* info = parg;
2300 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2301 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2304 * Associate the 4 delivery systems supported by DVBv3
2305 * API with their DVBv5 counterpart. For the other standards,
2306 * use the closest type, assuming that it would hopefully
2307 * work with a DVBv3 application.
2308 * It should be noticed that, on multi-frontend devices with
2309 * different types (terrestrial and cable, for example),
2310 * a pure DVBv3 application won't be able to use all delivery
2311 * systems. Yet, changing the DVBv5 cache to the other delivery
2312 * system should be enough for making it work.
2314 switch (dvbv3_type(c->delivery_system)) {
2316 info->type = FE_QPSK;
2319 info->type = FE_ATSC;
2322 info->type = FE_QAM;
2325 info->type = FE_OFDM;
2328 dev_err(fe->dvb->device,
2329 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2330 __func__, c->delivery_system);
2331 fe->ops.info.type = FE_OFDM;
2333 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2334 __func__, c->delivery_system, fe->ops.info.type);
2336 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2337 * do it, it is done for it. */
2338 info->caps |= FE_CAN_INVERSION_AUTO;
2343 case FE_READ_STATUS: {
2344 fe_status_t* status = parg;
2346 /* if retune was requested but hasn't occurred yet, prevent
2347 * that user get signal state from previous tuning */
2348 if (fepriv->state == FESTATE_RETUNE ||
2349 fepriv->state == FESTATE_ERROR) {
2355 if (fe->ops.read_status)
2356 err = fe->ops.read_status(fe, status);
2361 if (fe->ops.read_ber) {
2363 err = fe->ops.read_ber(fe, (__u32 *) parg);
2369 case FE_READ_SIGNAL_STRENGTH:
2370 if (fe->ops.read_signal_strength) {
2372 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2379 if (fe->ops.read_snr) {
2381 err = fe->ops.read_snr(fe, (__u16 *) parg);
2387 case FE_READ_UNCORRECTED_BLOCKS:
2388 if (fe->ops.read_ucblocks) {
2390 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2396 case FE_DISEQC_RESET_OVERLOAD:
2397 if (fe->ops.diseqc_reset_overload) {
2398 err = fe->ops.diseqc_reset_overload(fe);
2399 fepriv->state = FESTATE_DISEQC;
2404 case FE_DISEQC_SEND_MASTER_CMD:
2405 if (fe->ops.diseqc_send_master_cmd) {
2406 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2407 fepriv->state = FESTATE_DISEQC;
2412 case FE_DISEQC_SEND_BURST:
2413 if (fe->ops.diseqc_send_burst) {
2414 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2415 fepriv->state = FESTATE_DISEQC;
2421 if (fe->ops.set_tone) {
2422 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2423 fepriv->tone = (fe_sec_tone_mode_t) parg;
2424 fepriv->state = FESTATE_DISEQC;
2429 case FE_SET_VOLTAGE:
2430 if (fe->ops.set_voltage) {
2431 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2432 fepriv->voltage = (fe_sec_voltage_t) parg;
2433 fepriv->state = FESTATE_DISEQC;
2438 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2439 if (fe->ops.dishnetwork_send_legacy_command) {
2440 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2441 fepriv->state = FESTATE_DISEQC;
2443 } else if (fe->ops.set_voltage) {
2445 * NOTE: This is a fallback condition. Some frontends
2446 * (stv0299 for instance) take longer than 8msec to
2447 * respond to a set_voltage command. Those switches
2448 * need custom routines to switch properly. For all
2449 * other frontends, the following should work ok.
2450 * Dish network legacy switches (as used by Dish500)
2451 * are controlled by sending 9-bit command words
2452 * spaced 8msec apart.
2453 * the actual command word is switch/port dependent
2454 * so it is up to the userspace application to send
2455 * the right command.
2456 * The command must always start with a '0' after
2457 * initialization, so parg is 8 bits and does not
2458 * include the initialization or start bit
2460 unsigned long swcmd = ((unsigned long) parg) << 1;
2461 struct timeval nexttime;
2462 struct timeval tv[10];
2465 if (dvb_frontend_debug)
2466 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2467 do_gettimeofday(&nexttime);
2468 if (dvb_frontend_debug)
2470 /* before sending a command, initialize by sending
2471 * a 32ms 18V to the switch
2473 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2474 dvb_frontend_sleep_until(&nexttime, 32000);
2476 for (i = 0; i < 9; i++) {
2477 if (dvb_frontend_debug)
2478 do_gettimeofday(&tv[i + 1]);
2479 if ((swcmd & 0x01) != last) {
2480 /* set voltage to (last ? 13V : 18V) */
2481 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2482 last = (last) ? 0 : 1;
2486 dvb_frontend_sleep_until(&nexttime, 8000);
2488 if (dvb_frontend_debug) {
2489 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2490 __func__, fe->dvb->num);
2491 for (i = 1; i < 10; i++)
2492 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2495 fepriv->state = FESTATE_DISEQC;
2500 case FE_DISEQC_RECV_SLAVE_REPLY:
2501 if (fe->ops.diseqc_recv_slave_reply)
2502 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2505 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2506 if (fe->ops.enable_high_lnb_voltage)
2507 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2510 case FE_SET_FRONTEND:
2511 err = dvbv3_set_delivery_system(fe);
2515 err = dtv_property_cache_sync(fe, c, parg);
2518 err = dtv_set_frontend(fe);
2521 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2524 case FE_GET_FRONTEND:
2525 err = dtv_get_frontend(fe, parg);
2528 case FE_SET_FRONTEND_TUNE_MODE:
2529 fepriv->tune_mode_flags = (unsigned long) parg;
2538 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2540 struct dvb_device *dvbdev = file->private_data;
2541 struct dvb_frontend *fe = dvbdev->priv;
2542 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2544 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2546 poll_wait (file, &fepriv->events.wait_queue, wait);
2548 if (fepriv->events.eventw != fepriv->events.eventr)
2549 return (POLLIN | POLLRDNORM | POLLPRI);
2554 static int dvb_frontend_open(struct inode *inode, struct file *file)
2556 struct dvb_device *dvbdev = file->private_data;
2557 struct dvb_frontend *fe = dvbdev->priv;
2558 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2559 struct dvb_adapter *adapter = fe->dvb;
2562 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2563 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2566 if (adapter->mfe_shared) {
2567 mutex_lock (&adapter->mfe_lock);
2569 if (adapter->mfe_dvbdev == NULL)
2570 adapter->mfe_dvbdev = dvbdev;
2572 else if (adapter->mfe_dvbdev != dvbdev) {
2574 *mfedev = adapter->mfe_dvbdev;
2576 *mfe = mfedev->priv;
2577 struct dvb_frontend_private
2578 *mfepriv = mfe->frontend_priv;
2579 int mferetry = (dvb_mfe_wait_time << 1);
2581 mutex_unlock (&adapter->mfe_lock);
2582 while (mferetry-- && (mfedev->users != -1 ||
2583 mfepriv->thread != NULL)) {
2584 if(msleep_interruptible(500)) {
2585 if(signal_pending(current))
2590 mutex_lock (&adapter->mfe_lock);
2591 if(adapter->mfe_dvbdev != dvbdev) {
2592 mfedev = adapter->mfe_dvbdev;
2594 mfepriv = mfe->frontend_priv;
2595 if (mfedev->users != -1 ||
2596 mfepriv->thread != NULL) {
2597 mutex_unlock (&adapter->mfe_lock);
2600 adapter->mfe_dvbdev = dvbdev;
2605 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2606 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2609 /* If we took control of the bus, we need to force
2610 reinitialization. This is because many ts_bus_ctrl()
2611 functions strobe the RESET pin on the demod, and if the
2612 frontend thread already exists then the dvb_init() routine
2613 won't get called (which is what usually does initial
2614 register configuration). */
2615 fepriv->reinitialise = 1;
2618 if ((ret = dvb_generic_open (inode, file)) < 0)
2621 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2622 /* normal tune mode when opened R/W */
2623 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2625 fepriv->voltage = -1;
2627 ret = dvb_frontend_start (fe);
2631 /* empty event queue */
2632 fepriv->events.eventr = fepriv->events.eventw = 0;
2635 if (adapter->mfe_shared)
2636 mutex_unlock (&adapter->mfe_lock);
2640 dvb_generic_release(inode, file);
2642 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2643 fe->ops.ts_bus_ctrl(fe, 0);
2645 if (adapter->mfe_shared)
2646 mutex_unlock (&adapter->mfe_lock);
2650 static int dvb_frontend_release(struct inode *inode, struct file *file)
2652 struct dvb_device *dvbdev = file->private_data;
2653 struct dvb_frontend *fe = dvbdev->priv;
2654 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2657 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2659 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2660 fepriv->release_jiffies = jiffies;
2664 ret = dvb_generic_release (inode, file);
2666 if (dvbdev->users == -1) {
2667 wake_up(&fepriv->wait_queue);
2668 if (fe->exit != DVB_FE_NO_EXIT)
2669 wake_up(&dvbdev->wait_queue);
2670 if (fe->ops.ts_bus_ctrl)
2671 fe->ops.ts_bus_ctrl(fe, 0);
2677 static const struct file_operations dvb_frontend_fops = {
2678 .owner = THIS_MODULE,
2679 .unlocked_ioctl = dvb_generic_ioctl,
2680 .poll = dvb_frontend_poll,
2681 .open = dvb_frontend_open,
2682 .release = dvb_frontend_release,
2683 .llseek = noop_llseek,
2686 int dvb_frontend_suspend(struct dvb_frontend *fe)
2690 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2693 if (fe->ops.tuner_ops.suspend)
2694 ret = fe->ops.tuner_ops.suspend(fe);
2695 else if (fe->ops.tuner_ops.sleep)
2696 ret = fe->ops.tuner_ops.sleep(fe);
2699 ret = fe->ops.sleep(fe);
2703 EXPORT_SYMBOL(dvb_frontend_suspend);
2705 int dvb_frontend_resume(struct dvb_frontend *fe)
2707 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2710 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2713 fe->exit = DVB_FE_DEVICE_RESUME;
2715 ret = fe->ops.init(fe);
2717 if (fe->ops.tuner_ops.resume)
2718 ret = fe->ops.tuner_ops.resume(fe);
2719 else if (fe->ops.tuner_ops.init)
2720 ret = fe->ops.tuner_ops.init(fe);
2722 fe->exit = DVB_FE_NO_EXIT;
2723 fepriv->state = FESTATE_RETUNE;
2724 dvb_frontend_wakeup(fe);
2728 EXPORT_SYMBOL(dvb_frontend_resume);
2730 int dvb_register_frontend(struct dvb_adapter* dvb,
2731 struct dvb_frontend* fe)
2733 struct dvb_frontend_private *fepriv;
2734 const struct dvb_device dvbdev_template = {
2738 .fops = &dvb_frontend_fops,
2739 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2740 .name = fe->ops.info.name,
2742 .kernel_ioctl = dvb_frontend_ioctl
2745 dev_dbg(dvb->device, "%s:\n", __func__);
2747 if (mutex_lock_interruptible(&frontend_mutex))
2748 return -ERESTARTSYS;
2750 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2751 if (fe->frontend_priv == NULL) {
2752 mutex_unlock(&frontend_mutex);
2755 fepriv = fe->frontend_priv;
2757 sema_init(&fepriv->sem, 1);
2758 init_waitqueue_head (&fepriv->wait_queue);
2759 init_waitqueue_head (&fepriv->events.wait_queue);
2760 mutex_init(&fepriv->events.mtx);
2762 fepriv->inversion = INVERSION_OFF;
2764 dev_info(fe->dvb->device,
2765 "DVB: registering adapter %i frontend %i (%s)...\n",
2766 fe->dvb->num, fe->id, fe->ops.info.name);
2768 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2769 fe, DVB_DEVICE_FRONTEND);
2772 * Initialize the cache to the proper values according with the
2773 * first supported delivery system (ops->delsys[0])
2776 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2777 dvb_frontend_clear_cache(fe);
2779 mutex_unlock(&frontend_mutex);
2782 EXPORT_SYMBOL(dvb_register_frontend);
2784 int dvb_unregister_frontend(struct dvb_frontend* fe)
2786 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2787 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2789 mutex_lock(&frontend_mutex);
2790 dvb_frontend_stop (fe);
2791 mutex_unlock(&frontend_mutex);
2793 if (fepriv->dvbdev->users < -1)
2794 wait_event(fepriv->dvbdev->wait_queue,
2795 fepriv->dvbdev->users==-1);
2797 mutex_lock(&frontend_mutex);
2798 dvb_unregister_device (fepriv->dvbdev);
2800 /* fe is invalid now */
2802 mutex_unlock(&frontend_mutex);
2805 EXPORT_SYMBOL(dvb_unregister_frontend);
2807 #ifdef CONFIG_MEDIA_ATTACH
2808 void dvb_frontend_detach(struct dvb_frontend* fe)
2812 if (fe->ops.release_sec) {
2813 fe->ops.release_sec(fe);
2814 dvb_detach(fe->ops.release_sec);
2816 if (fe->ops.tuner_ops.release) {
2817 fe->ops.tuner_ops.release(fe);
2818 dvb_detach(fe->ops.tuner_ops.release);
2820 if (fe->ops.analog_ops.release) {
2821 fe->ops.analog_ops.release(fe);
2822 dvb_detach(fe->ops.analog_ops.release);
2824 ptr = (void*)fe->ops.release;
2826 fe->ops.release(fe);
2831 void dvb_frontend_detach(struct dvb_frontend* fe)
2833 if (fe->ops.release_sec)
2834 fe->ops.release_sec(fe);
2835 if (fe->ops.tuner_ops.release)
2836 fe->ops.tuner_ops.release(fe);
2837 if (fe->ops.analog_ops.release)
2838 fe->ops.analog_ops.release(fe);
2839 if (fe->ops.release)
2840 fe->ops.release(fe);
2843 EXPORT_SYMBOL(dvb_frontend_detach);