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.
21 * To obtain the license, point your browser to
22 * http://www.gnu.org/copyleft/gpl.html
25 /* Enables DVBv3 compatibility bits at the headers */
28 #define pr_fmt(fmt) "dvb_frontend: " fmt
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/sched/signal.h>
33 #include <linux/wait.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/semaphore.h>
37 #include <linux/module.h>
38 #include <linux/list.h>
39 #include <linux/freezer.h>
40 #include <linux/jiffies.h>
41 #include <linux/kthread.h>
42 #include <linux/ktime.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 dprintk(fmt, arg...) \
70 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
72 #define FESTATE_IDLE 1
73 #define FESTATE_RETUNE 2
74 #define FESTATE_TUNING_FAST 4
75 #define FESTATE_TUNING_SLOW 8
76 #define FESTATE_TUNED 16
77 #define FESTATE_ZIGZAG_FAST 32
78 #define FESTATE_ZIGZAG_SLOW 64
79 #define FESTATE_DISEQC 128
80 #define FESTATE_ERROR 256
81 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
82 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
83 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
84 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
87 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
88 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
89 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
90 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
91 * FESTATE_TUNED. The frontend has successfully locked on.
92 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
93 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
94 * FESTATE_DISEQC. A DISEQC command has just been issued.
95 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
96 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
97 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
98 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
101 static DEFINE_MUTEX(frontend_mutex);
103 struct dvb_frontend_private {
104 /* thread/frontend values */
105 struct dvb_device *dvbdev;
106 struct dvb_frontend_parameters parameters_out;
107 struct dvb_fe_events events;
108 struct semaphore sem;
109 struct list_head list_head;
110 wait_queue_head_t wait_queue;
111 struct task_struct *thread;
112 unsigned long release_jiffies;
114 enum fe_status status;
115 unsigned long tune_mode_flags;
117 unsigned int reinitialise;
121 /* swzigzag values */
123 unsigned int bending;
125 unsigned int inversion;
126 unsigned int auto_step;
127 unsigned int auto_sub_step;
128 unsigned int started_auto_step;
129 unsigned int min_delay;
130 unsigned int max_drift;
131 unsigned int step_size;
133 unsigned int check_wrapped;
134 enum dvbfe_search algo_status;
136 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
137 struct media_pipeline pipe;
141 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
142 void (*release)(struct dvb_frontend *fe));
144 static void __dvb_frontend_free(struct dvb_frontend *fe)
146 struct dvb_frontend_private *fepriv = fe->frontend_priv;
149 dvb_device_put(fepriv->dvbdev);
151 dvb_frontend_invoke_release(fe, fe->ops.release);
157 static void dvb_frontend_free(struct kref *ref)
159 struct dvb_frontend *fe =
160 container_of(ref, struct dvb_frontend, refcount);
162 __dvb_frontend_free(fe);
165 static void dvb_frontend_put(struct dvb_frontend *fe)
168 * Check if the frontend was registered, as otherwise
169 * kref was not initialized yet.
171 if (fe->frontend_priv)
172 kref_put(&fe->refcount, dvb_frontend_free);
174 __dvb_frontend_free(fe);
177 static void dvb_frontend_get(struct dvb_frontend *fe)
179 kref_get(&fe->refcount);
182 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
183 static int dtv_get_frontend(struct dvb_frontend *fe,
184 struct dtv_frontend_properties *c,
185 struct dvb_frontend_parameters *p_out);
187 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
188 const struct dtv_frontend_properties *c,
189 struct dvb_frontend_parameters *p);
191 static bool has_get_frontend(struct dvb_frontend *fe)
193 return fe->ops.get_frontend != NULL;
197 * Due to DVBv3 API calls, a delivery system should be mapped into one of
198 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
199 * otherwise, a DVBv3 call will fail.
201 enum dvbv3_emulation_type {
209 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
211 switch (delivery_system) {
212 case SYS_DVBC_ANNEX_A:
213 case SYS_DVBC_ANNEX_C:
228 case SYS_DVBC_ANNEX_B:
236 * Doesn't know how to emulate those types and/or
237 * there's no frontend driver from this type yet
238 * with some emulation code, so, we're not sure yet how
239 * to handle them, or they're not compatible with a DVBv3 call.
241 return DVBV3_UNKNOWN;
245 static void dvb_frontend_add_event(struct dvb_frontend *fe,
246 enum fe_status status)
248 struct dvb_frontend_private *fepriv = fe->frontend_priv;
249 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
250 struct dvb_fe_events *events = &fepriv->events;
251 struct dvb_frontend_event *e;
254 dev_dbg(fe->dvb->device, "%s:\n", __func__);
256 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
257 dtv_get_frontend(fe, c, &fepriv->parameters_out);
259 mutex_lock(&events->mtx);
261 wp = (events->eventw + 1) % MAX_EVENT;
262 if (wp == events->eventr) {
263 events->overflow = 1;
264 events->eventr = (events->eventr + 1) % MAX_EVENT;
267 e = &events->events[events->eventw];
269 e->parameters = fepriv->parameters_out;
273 mutex_unlock(&events->mtx);
275 wake_up_interruptible (&events->wait_queue);
278 static int dvb_frontend_test_event(struct dvb_frontend_private *fepriv,
279 struct dvb_fe_events *events)
284 ret = events->eventw != events->eventr;
290 static int dvb_frontend_get_event(struct dvb_frontend *fe,
291 struct dvb_frontend_event *event, int flags)
293 struct dvb_frontend_private *fepriv = fe->frontend_priv;
294 struct dvb_fe_events *events = &fepriv->events;
296 dev_dbg(fe->dvb->device, "%s:\n", __func__);
298 if (events->overflow) {
299 events->overflow = 0;
303 if (events->eventw == events->eventr) {
304 struct wait_queue_entry wait;
307 if (flags & O_NONBLOCK)
310 init_waitqueue_entry(&wait, current);
311 add_wait_queue(&events->wait_queue, &wait);
312 while (!dvb_frontend_test_event(fepriv, events)) {
313 wait_woken(&wait, TASK_INTERRUPTIBLE, 0);
314 if (signal_pending(current)) {
319 remove_wait_queue(&events->wait_queue, &wait);
324 mutex_lock(&events->mtx);
325 *event = events->events[events->eventr];
326 events->eventr = (events->eventr + 1) % MAX_EVENT;
327 mutex_unlock(&events->mtx);
332 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
334 struct dvb_frontend_private *fepriv = fe->frontend_priv;
335 struct dvb_fe_events *events = &fepriv->events;
337 mutex_lock(&events->mtx);
338 events->eventr = events->eventw;
339 mutex_unlock(&events->mtx);
342 static void dvb_frontend_init(struct dvb_frontend *fe)
344 dev_dbg(fe->dvb->device,
345 "%s: initialising adapter %i frontend %i (%s)...\n",
346 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
350 if (fe->ops.tuner_ops.init) {
351 if (fe->ops.i2c_gate_ctrl)
352 fe->ops.i2c_gate_ctrl(fe, 1);
353 fe->ops.tuner_ops.init(fe);
354 if (fe->ops.i2c_gate_ctrl)
355 fe->ops.i2c_gate_ctrl(fe, 0);
359 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
361 struct dvb_frontend_private *fepriv = fe->frontend_priv;
363 fepriv->reinitialise = 1;
364 dvb_frontend_wakeup(fe);
366 EXPORT_SYMBOL(dvb_frontend_reinitialise);
368 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
371 struct dvb_frontend *fe = fepriv->dvbdev->priv;
373 dev_dbg(fe->dvb->device, "%s:\n", __func__);
376 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
378 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
380 q2 = fepriv->quality - 128;
383 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
387 * Performs automatic twiddling of frontend parameters.
389 * @param fe The frontend concerned.
390 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
391 * @returns Number of complete iterations that have been performed.
393 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
398 struct dvb_frontend_private *fepriv = fe->frontend_priv;
399 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
400 int original_inversion = c->inversion;
401 u32 original_frequency = c->frequency;
403 /* are we using autoinversion? */
404 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
405 (c->inversion == INVERSION_AUTO));
407 /* setup parameters correctly */
409 /* calculate the lnb_drift */
410 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
412 /* wrap the auto_step if we've exceeded the maximum drift */
413 if (fepriv->lnb_drift > fepriv->max_drift) {
414 fepriv->auto_step = 0;
415 fepriv->auto_sub_step = 0;
416 fepriv->lnb_drift = 0;
419 /* perform inversion and +/- zigzag */
420 switch(fepriv->auto_sub_step) {
422 /* try with the current inversion and current drift setting */
427 if (!autoinversion) break;
429 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
434 if (fepriv->lnb_drift == 0) break;
436 fepriv->lnb_drift = -fepriv->lnb_drift;
441 if (fepriv->lnb_drift == 0) break;
442 if (!autoinversion) break;
444 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
445 fepriv->lnb_drift = -fepriv->lnb_drift;
451 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
455 if (!ready) fepriv->auto_sub_step++;
458 /* if this attempt would hit where we started, indicate a complete
459 * iteration has occurred */
460 if ((fepriv->auto_step == fepriv->started_auto_step) &&
461 (fepriv->auto_sub_step == 0) && check_wrapped) {
465 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
466 "auto_sub_step:%i started_auto_step:%i\n",
467 __func__, fepriv->lnb_drift, fepriv->inversion,
468 fepriv->auto_step, fepriv->auto_sub_step,
469 fepriv->started_auto_step);
471 /* set the frontend itself */
472 c->frequency += fepriv->lnb_drift;
474 c->inversion = fepriv->inversion;
476 if (fe->ops.set_frontend)
477 fe_set_err = fe->ops.set_frontend(fe);
479 if (fe_set_err < 0) {
480 fepriv->state = FESTATE_ERROR;
484 c->frequency = original_frequency;
485 c->inversion = original_inversion;
487 fepriv->auto_sub_step++;
491 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
493 enum fe_status s = FE_NONE;
495 struct dvb_frontend_private *fepriv = fe->frontend_priv;
496 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
498 /* if we've got no parameters, just keep idling */
499 if (fepriv->state & FESTATE_IDLE) {
500 fepriv->delay = 3*HZ;
505 /* in SCAN mode, we just set the frontend when asked and leave it alone */
506 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
507 if (fepriv->state & FESTATE_RETUNE) {
509 if (fe->ops.set_frontend)
510 retval = fe->ops.set_frontend(fe);
513 fepriv->state = FESTATE_ERROR;
515 fepriv->state = FESTATE_TUNED;
517 fepriv->delay = 3*HZ;
522 /* get the frontend status */
523 if (fepriv->state & FESTATE_RETUNE) {
526 if (fe->ops.read_status)
527 fe->ops.read_status(fe, &s);
528 if (s != fepriv->status) {
529 dvb_frontend_add_event(fe, s);
534 /* if we're not tuned, and we have a lock, move to the TUNED state */
535 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
536 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
537 fepriv->state = FESTATE_TUNED;
539 /* if we're tuned, then we have determined the correct inversion */
540 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
541 (c->inversion == INVERSION_AUTO)) {
542 c->inversion = fepriv->inversion;
547 /* if we are tuned already, check we're still locked */
548 if (fepriv->state & FESTATE_TUNED) {
549 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
551 /* we're tuned, and the lock is still good... */
552 if (s & FE_HAS_LOCK) {
554 } else { /* if we _WERE_ tuned, but now don't have a lock */
555 fepriv->state = FESTATE_ZIGZAG_FAST;
556 fepriv->started_auto_step = fepriv->auto_step;
557 fepriv->check_wrapped = 0;
561 /* don't actually do anything if we're in the LOSTLOCK state,
562 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
563 if ((fepriv->state & FESTATE_LOSTLOCK) &&
564 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
565 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
569 /* don't do anything if we're in the DISEQC state, since this
570 * might be someone with a motorized dish controlled by DISEQC.
571 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
572 if (fepriv->state & FESTATE_DISEQC) {
573 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
577 /* if we're in the RETUNE state, set everything up for a brand
578 * new scan, keeping the current inversion setting, as the next
579 * tune is _very_ likely to require the same */
580 if (fepriv->state & FESTATE_RETUNE) {
581 fepriv->lnb_drift = 0;
582 fepriv->auto_step = 0;
583 fepriv->auto_sub_step = 0;
584 fepriv->started_auto_step = 0;
585 fepriv->check_wrapped = 0;
589 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
590 fepriv->delay = fepriv->min_delay;
593 retval = dvb_frontend_swzigzag_autotune(fe,
594 fepriv->check_wrapped);
598 /* OK, if we've run out of trials at the fast speed.
599 * Drop back to slow for the _next_ attempt */
600 fepriv->state = FESTATE_SEARCHING_SLOW;
601 fepriv->started_auto_step = fepriv->auto_step;
604 fepriv->check_wrapped = 1;
606 /* if we've just retuned, enter the ZIGZAG_FAST state.
607 * This ensures we cannot return from an
608 * FE_SET_FRONTEND ioctl before the first frontend tune
610 if (fepriv->state & FESTATE_RETUNE) {
611 fepriv->state = FESTATE_TUNING_FAST;
616 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
617 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
619 /* Note: don't bother checking for wrapping; we stay in this
620 * state until we get a lock */
621 dvb_frontend_swzigzag_autotune(fe, 0);
625 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
627 struct dvb_frontend_private *fepriv = fe->frontend_priv;
629 if (fe->exit != DVB_FE_NO_EXIT)
632 if (fepriv->dvbdev->writers == 1)
633 if (time_after_eq(jiffies, fepriv->release_jiffies +
634 dvb_shutdown_timeout * HZ))
640 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
642 struct dvb_frontend_private *fepriv = fe->frontend_priv;
644 if (fepriv->wakeup) {
648 return dvb_frontend_is_exiting(fe);
651 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
653 struct dvb_frontend_private *fepriv = fe->frontend_priv;
656 wake_up_interruptible(&fepriv->wait_queue);
659 static int dvb_frontend_thread(void *data)
661 struct dvb_frontend *fe = data;
662 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
663 struct dvb_frontend_private *fepriv = fe->frontend_priv;
664 enum fe_status s = FE_NONE;
665 enum dvbfe_algo algo;
666 bool re_tune = false;
667 bool semheld = false;
669 dev_dbg(fe->dvb->device, "%s:\n", __func__);
671 fepriv->check_wrapped = 0;
673 fepriv->delay = 3*HZ;
676 fepriv->reinitialise = 0;
678 dvb_frontend_init(fe);
682 up(&fepriv->sem); /* is locked when we enter the thread... */
684 wait_event_interruptible_timeout(fepriv->wait_queue,
685 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
686 || freezing(current),
689 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
690 /* got signal or quitting */
691 if (!down_interruptible(&fepriv->sem))
693 fe->exit = DVB_FE_NORMAL_EXIT;
700 if (down_interruptible(&fepriv->sem))
703 if (fepriv->reinitialise) {
704 dvb_frontend_init(fe);
705 if (fe->ops.set_tone && fepriv->tone != -1)
706 fe->ops.set_tone(fe, fepriv->tone);
707 if (fe->ops.set_voltage && fepriv->voltage != -1)
708 fe->ops.set_voltage(fe, fepriv->voltage);
709 fepriv->reinitialise = 0;
712 /* do an iteration of the tuning loop */
713 if (fe->ops.get_frontend_algo) {
714 algo = fe->ops.get_frontend_algo(fe);
717 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
719 if (fepriv->state & FESTATE_RETUNE) {
720 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
722 fepriv->state = FESTATE_TUNED;
728 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
730 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
731 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
732 dvb_frontend_add_event(fe, s);
737 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
738 dvb_frontend_swzigzag(fe);
740 case DVBFE_ALGO_CUSTOM:
741 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
742 if (fepriv->state & FESTATE_RETUNE) {
743 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
744 fepriv->state = FESTATE_TUNED;
746 /* Case where we are going to search for a carrier
747 * User asked us to retune again for some reason, possibly
748 * requesting a search with a new set of parameters
750 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
751 if (fe->ops.search) {
752 fepriv->algo_status = fe->ops.search(fe);
753 /* We did do a search as was requested, the flags are
754 * now unset as well and has the flags wrt to search.
757 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
760 /* Track the carrier if the search was successful */
761 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
762 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
763 fepriv->delay = HZ / 2;
765 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
766 fe->ops.read_status(fe, &s);
767 if (s != fepriv->status) {
768 dvb_frontend_add_event(fe, s); /* update event list */
770 if (!(s & FE_HAS_LOCK)) {
771 fepriv->delay = HZ / 10;
772 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
774 fepriv->delay = 60 * HZ;
779 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
783 dvb_frontend_swzigzag(fe);
787 if (dvb_powerdown_on_sleep) {
788 if (fe->ops.set_voltage)
789 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
790 if (fe->ops.tuner_ops.sleep) {
791 if (fe->ops.i2c_gate_ctrl)
792 fe->ops.i2c_gate_ctrl(fe, 1);
793 fe->ops.tuner_ops.sleep(fe);
794 if (fe->ops.i2c_gate_ctrl)
795 fe->ops.i2c_gate_ctrl(fe, 0);
801 fepriv->thread = NULL;
802 if (kthread_should_stop())
803 fe->exit = DVB_FE_DEVICE_REMOVED;
805 fe->exit = DVB_FE_NO_EXIT;
810 dvb_frontend_wakeup(fe);
814 static void dvb_frontend_stop(struct dvb_frontend *fe)
816 struct dvb_frontend_private *fepriv = fe->frontend_priv;
818 dev_dbg(fe->dvb->device, "%s:\n", __func__);
820 if (fe->exit != DVB_FE_DEVICE_REMOVED)
821 fe->exit = DVB_FE_NORMAL_EXIT;
827 kthread_stop(fepriv->thread);
829 sema_init(&fepriv->sem, 1);
830 fepriv->state = FESTATE_IDLE;
832 /* paranoia check in case a signal arrived */
834 dev_warn(fe->dvb->device,
835 "dvb_frontend_stop: warning: thread %p won't exit\n",
840 * Sleep for the amount of time given by add_usec parameter
842 * This needs to be as precise as possible, as it affects the detection of
843 * the dish tone command at the satellite subsystem. The precision is improved
844 * by using a scheduled msleep followed by udelay for the remainder.
846 void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
850 *waketime = ktime_add_us(*waketime, add_usec);
851 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
853 msleep((delta - 1500) / 1000);
854 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
859 EXPORT_SYMBOL(dvb_frontend_sleep_until);
861 static int dvb_frontend_start(struct dvb_frontend *fe)
864 struct dvb_frontend_private *fepriv = fe->frontend_priv;
865 struct task_struct *fe_thread;
867 dev_dbg(fe->dvb->device, "%s:\n", __func__);
869 if (fepriv->thread) {
870 if (fe->exit == DVB_FE_NO_EXIT)
873 dvb_frontend_stop (fe);
876 if (signal_pending(current))
878 if (down_interruptible (&fepriv->sem))
881 fepriv->state = FESTATE_IDLE;
882 fe->exit = DVB_FE_NO_EXIT;
883 fepriv->thread = NULL;
886 fe_thread = kthread_run(dvb_frontend_thread, fe,
887 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
888 if (IS_ERR(fe_thread)) {
889 ret = PTR_ERR(fe_thread);
890 dev_warn(fe->dvb->device,
891 "dvb_frontend_start: failed to start kthread (%d)\n",
896 fepriv->thread = fe_thread;
900 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
901 u32 *freq_min, u32 *freq_max)
903 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
905 if (fe->ops.info.frequency_max == 0)
906 *freq_max = fe->ops.tuner_ops.info.frequency_max;
907 else if (fe->ops.tuner_ops.info.frequency_max == 0)
908 *freq_max = fe->ops.info.frequency_max;
910 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
912 if (*freq_min == 0 || *freq_max == 0)
913 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
914 fe->dvb->num, fe->id);
917 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
919 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
923 /* range check: frequency */
924 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
925 if ((freq_min && c->frequency < freq_min) ||
926 (freq_max && c->frequency > freq_max)) {
927 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
928 fe->dvb->num, fe->id, c->frequency,
933 /* range check: symbol rate */
934 switch (c->delivery_system) {
938 case SYS_DVBC_ANNEX_A:
939 case SYS_DVBC_ANNEX_C:
940 if ((fe->ops.info.symbol_rate_min &&
941 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
942 (fe->ops.info.symbol_rate_max &&
943 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
944 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
945 fe->dvb->num, fe->id, c->symbol_rate,
946 fe->ops.info.symbol_rate_min,
947 fe->ops.info.symbol_rate_max);
957 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
959 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
963 delsys = c->delivery_system;
964 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
965 c->delivery_system = delsys;
967 c->state = DTV_CLEAR;
969 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
970 __func__, c->delivery_system);
972 c->transmission_mode = TRANSMISSION_MODE_AUTO;
973 c->bandwidth_hz = 0; /* AUTO */
974 c->guard_interval = GUARD_INTERVAL_AUTO;
975 c->hierarchy = HIERARCHY_AUTO;
977 c->code_rate_HP = FEC_AUTO;
978 c->code_rate_LP = FEC_AUTO;
979 c->fec_inner = FEC_AUTO;
980 c->rolloff = ROLLOFF_AUTO;
981 c->voltage = SEC_VOLTAGE_OFF;
982 c->sectone = SEC_TONE_OFF;
983 c->pilot = PILOT_AUTO;
985 c->isdbt_partial_reception = 0;
986 c->isdbt_sb_mode = 0;
987 c->isdbt_sb_subchannel = 0;
988 c->isdbt_sb_segment_idx = 0;
989 c->isdbt_sb_segment_count = 0;
990 c->isdbt_layer_enabled = 0;
991 for (i = 0; i < 3; i++) {
992 c->layer[i].fec = FEC_AUTO;
993 c->layer[i].modulation = QAM_AUTO;
994 c->layer[i].interleaving = 0;
995 c->layer[i].segment_count = 0;
998 c->stream_id = NO_STREAM_ID_FILTER;
1000 switch (c->delivery_system) {
1004 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1005 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1008 c->modulation = VSB_8;
1011 c->symbol_rate = 28860000;
1012 c->rolloff = ROLLOFF_35;
1013 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1016 c->modulation = QAM_AUTO;
1025 #define _DTV_CMD(n, s, b) \
1034 char *name; /* A display name for debugging purposes */
1036 __u32 cmd; /* A unique ID */
1039 __u32 set:1; /* Either a set or get property */
1040 __u32 buffer:1; /* Does this property use the buffer? */
1041 __u32 reserved:30; /* Align */
1044 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1045 _DTV_CMD(DTV_TUNE, 1, 0),
1046 _DTV_CMD(DTV_CLEAR, 1, 0),
1049 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1050 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1051 _DTV_CMD(DTV_MODULATION, 1, 0),
1052 _DTV_CMD(DTV_INVERSION, 1, 0),
1053 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1054 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1055 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1056 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1057 _DTV_CMD(DTV_TONE, 1, 0),
1058 _DTV_CMD(DTV_PILOT, 1, 0),
1059 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1060 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1061 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1062 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1063 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1064 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1065 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1066 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1068 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1069 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1070 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1071 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1072 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1073 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1074 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1075 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1076 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1077 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1078 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1079 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1080 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1081 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1082 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1083 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1084 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1085 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1087 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1088 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1089 _DTV_CMD(DTV_LNA, 1, 0),
1092 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1093 _DTV_CMD(DTV_API_VERSION, 0, 0),
1095 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1097 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1098 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1100 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1101 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1102 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1103 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1104 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1105 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1106 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1107 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1108 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1109 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1110 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1111 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1112 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1114 /* Statistics API */
1115 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1116 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1117 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1118 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1119 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1120 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1121 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1122 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1125 static void dtv_property_dump(struct dvb_frontend *fe,
1127 struct dtv_property *tvp)
1131 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1132 dev_warn(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x undefined\n",
1134 is_set ? "SET" : "GET",
1139 dev_dbg(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x (%s)\n", __func__,
1140 is_set ? "SET" : "GET",
1142 dtv_cmds[tvp->cmd].name);
1144 if (dtv_cmds[tvp->cmd].buffer) {
1145 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1146 __func__, tvp->u.buffer.len);
1148 for(i = 0; i < tvp->u.buffer.len; i++)
1149 dev_dbg(fe->dvb->device,
1150 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1151 __func__, i, tvp->u.buffer.data[i]);
1153 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1158 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1159 * drivers can use a single set_frontend tuning function, regardless of whether
1160 * it's being used for the legacy or new API, reducing code and complexity.
1162 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1163 struct dtv_frontend_properties *c,
1164 const struct dvb_frontend_parameters *p)
1166 c->frequency = p->frequency;
1167 c->inversion = p->inversion;
1169 switch (dvbv3_type(c->delivery_system)) {
1171 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1172 c->symbol_rate = p->u.qpsk.symbol_rate;
1173 c->fec_inner = p->u.qpsk.fec_inner;
1176 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1177 c->symbol_rate = p->u.qam.symbol_rate;
1178 c->fec_inner = p->u.qam.fec_inner;
1179 c->modulation = p->u.qam.modulation;
1182 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1184 switch (p->u.ofdm.bandwidth) {
1185 case BANDWIDTH_10_MHZ:
1186 c->bandwidth_hz = 10000000;
1188 case BANDWIDTH_8_MHZ:
1189 c->bandwidth_hz = 8000000;
1191 case BANDWIDTH_7_MHZ:
1192 c->bandwidth_hz = 7000000;
1194 case BANDWIDTH_6_MHZ:
1195 c->bandwidth_hz = 6000000;
1197 case BANDWIDTH_5_MHZ:
1198 c->bandwidth_hz = 5000000;
1200 case BANDWIDTH_1_712_MHZ:
1201 c->bandwidth_hz = 1712000;
1203 case BANDWIDTH_AUTO:
1204 c->bandwidth_hz = 0;
1207 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1208 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1209 c->modulation = p->u.ofdm.constellation;
1210 c->transmission_mode = p->u.ofdm.transmission_mode;
1211 c->guard_interval = p->u.ofdm.guard_interval;
1212 c->hierarchy = p->u.ofdm.hierarchy_information;
1215 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1216 c->modulation = p->u.vsb.modulation;
1217 if (c->delivery_system == SYS_ATSCMH)
1219 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1220 c->delivery_system = SYS_ATSC;
1222 c->delivery_system = SYS_DVBC_ANNEX_B;
1225 dev_err(fe->dvb->device,
1226 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1227 __func__, c->delivery_system);
1234 /* Ensure the cached values are set correctly in the frontend
1235 * legacy tuning structures, for the advanced tuning API.
1238 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1239 const struct dtv_frontend_properties *c,
1240 struct dvb_frontend_parameters *p)
1242 p->frequency = c->frequency;
1243 p->inversion = c->inversion;
1245 switch (dvbv3_type(c->delivery_system)) {
1247 dev_err(fe->dvb->device,
1248 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1249 __func__, c->delivery_system);
1252 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1253 p->u.qpsk.symbol_rate = c->symbol_rate;
1254 p->u.qpsk.fec_inner = c->fec_inner;
1257 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1258 p->u.qam.symbol_rate = c->symbol_rate;
1259 p->u.qam.fec_inner = c->fec_inner;
1260 p->u.qam.modulation = c->modulation;
1263 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1264 switch (c->bandwidth_hz) {
1266 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1269 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1272 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1275 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1278 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1281 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1285 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1287 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1288 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1289 p->u.ofdm.constellation = c->modulation;
1290 p->u.ofdm.transmission_mode = c->transmission_mode;
1291 p->u.ofdm.guard_interval = c->guard_interval;
1292 p->u.ofdm.hierarchy_information = c->hierarchy;
1295 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1296 p->u.vsb.modulation = c->modulation;
1303 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1304 * @fe: struct dvb_frontend pointer
1305 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1306 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1308 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1309 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1310 * If p_out is not null, it will update the DVBv3 params pointed by it.
1312 static int dtv_get_frontend(struct dvb_frontend *fe,
1313 struct dtv_frontend_properties *c,
1314 struct dvb_frontend_parameters *p_out)
1318 if (fe->ops.get_frontend) {
1319 r = fe->ops.get_frontend(fe, c);
1320 if (unlikely(r < 0))
1323 dtv_property_legacy_params_sync(fe, c, p_out);
1327 /* As everything is in cache, get_frontend fops are always supported */
1331 static int dvb_frontend_ioctl_legacy(struct file *file,
1332 unsigned int cmd, void *parg);
1333 static int dvb_frontend_ioctl_properties(struct file *file,
1334 unsigned int cmd, void *parg);
1336 static int dtv_property_process_get(struct dvb_frontend *fe,
1337 const struct dtv_frontend_properties *c,
1338 struct dtv_property *tvp,
1344 case DTV_ENUM_DELSYS:
1346 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1347 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1350 tvp->u.buffer.len = ncaps;
1353 tvp->u.data = c->frequency;
1355 case DTV_MODULATION:
1356 tvp->u.data = c->modulation;
1358 case DTV_BANDWIDTH_HZ:
1359 tvp->u.data = c->bandwidth_hz;
1362 tvp->u.data = c->inversion;
1364 case DTV_SYMBOL_RATE:
1365 tvp->u.data = c->symbol_rate;
1368 tvp->u.data = c->fec_inner;
1371 tvp->u.data = c->pilot;
1374 tvp->u.data = c->rolloff;
1376 case DTV_DELIVERY_SYSTEM:
1377 tvp->u.data = c->delivery_system;
1380 tvp->u.data = c->voltage;
1383 tvp->u.data = c->sectone;
1385 case DTV_API_VERSION:
1386 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1388 case DTV_CODE_RATE_HP:
1389 tvp->u.data = c->code_rate_HP;
1391 case DTV_CODE_RATE_LP:
1392 tvp->u.data = c->code_rate_LP;
1394 case DTV_GUARD_INTERVAL:
1395 tvp->u.data = c->guard_interval;
1397 case DTV_TRANSMISSION_MODE:
1398 tvp->u.data = c->transmission_mode;
1401 tvp->u.data = c->hierarchy;
1403 case DTV_INTERLEAVING:
1404 tvp->u.data = c->interleaving;
1407 /* ISDB-T Support here */
1408 case DTV_ISDBT_PARTIAL_RECEPTION:
1409 tvp->u.data = c->isdbt_partial_reception;
1411 case DTV_ISDBT_SOUND_BROADCASTING:
1412 tvp->u.data = c->isdbt_sb_mode;
1414 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1415 tvp->u.data = c->isdbt_sb_subchannel;
1417 case DTV_ISDBT_SB_SEGMENT_IDX:
1418 tvp->u.data = c->isdbt_sb_segment_idx;
1420 case DTV_ISDBT_SB_SEGMENT_COUNT:
1421 tvp->u.data = c->isdbt_sb_segment_count;
1423 case DTV_ISDBT_LAYER_ENABLED:
1424 tvp->u.data = c->isdbt_layer_enabled;
1426 case DTV_ISDBT_LAYERA_FEC:
1427 tvp->u.data = c->layer[0].fec;
1429 case DTV_ISDBT_LAYERA_MODULATION:
1430 tvp->u.data = c->layer[0].modulation;
1432 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1433 tvp->u.data = c->layer[0].segment_count;
1435 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1436 tvp->u.data = c->layer[0].interleaving;
1438 case DTV_ISDBT_LAYERB_FEC:
1439 tvp->u.data = c->layer[1].fec;
1441 case DTV_ISDBT_LAYERB_MODULATION:
1442 tvp->u.data = c->layer[1].modulation;
1444 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1445 tvp->u.data = c->layer[1].segment_count;
1447 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1448 tvp->u.data = c->layer[1].interleaving;
1450 case DTV_ISDBT_LAYERC_FEC:
1451 tvp->u.data = c->layer[2].fec;
1453 case DTV_ISDBT_LAYERC_MODULATION:
1454 tvp->u.data = c->layer[2].modulation;
1456 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1457 tvp->u.data = c->layer[2].segment_count;
1459 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1460 tvp->u.data = c->layer[2].interleaving;
1463 /* Multistream support */
1465 case DTV_DVBT2_PLP_ID_LEGACY:
1466 tvp->u.data = c->stream_id;
1470 case DTV_ATSCMH_FIC_VER:
1471 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1473 case DTV_ATSCMH_PARADE_ID:
1474 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1476 case DTV_ATSCMH_NOG:
1477 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1479 case DTV_ATSCMH_TNOG:
1480 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1482 case DTV_ATSCMH_SGN:
1483 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1485 case DTV_ATSCMH_PRC:
1486 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1488 case DTV_ATSCMH_RS_FRAME_MODE:
1489 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1491 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1492 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1494 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1495 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1497 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1498 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1500 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1501 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1503 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1504 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1506 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1507 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1509 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1510 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1512 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1513 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1517 tvp->u.data = c->lna;
1520 /* Fill quality measures */
1521 case DTV_STAT_SIGNAL_STRENGTH:
1522 tvp->u.st = c->strength;
1527 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1528 tvp->u.st = c->pre_bit_error;
1530 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1531 tvp->u.st = c->pre_bit_count;
1533 case DTV_STAT_POST_ERROR_BIT_COUNT:
1534 tvp->u.st = c->post_bit_error;
1536 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1537 tvp->u.st = c->post_bit_count;
1539 case DTV_STAT_ERROR_BLOCK_COUNT:
1540 tvp->u.st = c->block_error;
1542 case DTV_STAT_TOTAL_BLOCK_COUNT:
1543 tvp->u.st = c->block_count;
1546 dev_dbg(fe->dvb->device,
1547 "%s: FE property %d doesn't exist\n",
1548 __func__, tvp->cmd);
1552 /* Allow the frontend to override outgoing properties */
1553 if (fe->ops.get_property) {
1554 r = fe->ops.get_property(fe, tvp);
1559 dtv_property_dump(fe, false, tvp);
1564 static int dtv_set_frontend(struct dvb_frontend *fe);
1566 static bool is_dvbv3_delsys(u32 delsys)
1568 return (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1569 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1573 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1574 * @fe: struct frontend;
1575 * @delsys: DVBv5 type that will be used for emulation
1577 * Provides emulation for delivery systems that are compatible with the old
1578 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1579 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1580 * parameters are compatible with DVB-S spec.
1582 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1585 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1587 c->delivery_system = delsys;
1590 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1592 if (c->delivery_system == SYS_ISDBT) {
1593 dev_dbg(fe->dvb->device,
1594 "%s: Using defaults for SYS_ISDBT\n",
1597 if (!c->bandwidth_hz)
1598 c->bandwidth_hz = 6000000;
1600 c->isdbt_partial_reception = 0;
1601 c->isdbt_sb_mode = 0;
1602 c->isdbt_sb_subchannel = 0;
1603 c->isdbt_sb_segment_idx = 0;
1604 c->isdbt_sb_segment_count = 0;
1605 c->isdbt_layer_enabled = 7;
1606 for (i = 0; i < 3; i++) {
1607 c->layer[i].fec = FEC_AUTO;
1608 c->layer[i].modulation = QAM_AUTO;
1609 c->layer[i].interleaving = 0;
1610 c->layer[i].segment_count = 0;
1613 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1614 __func__, c->delivery_system);
1620 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1621 * @fe: frontend struct
1622 * @desired_system: delivery system requested by the user
1624 * A DVBv5 call know what's the desired system it wants. So, set it.
1626 * There are, however, a few known issues with early DVBv5 applications that
1627 * are also handled by this logic:
1629 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1630 * This is an API violation, but, as we don't want to break userspace,
1631 * convert it to the first supported delivery system.
1632 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1633 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1634 * ISDB-T provided backward compat with DVB-T.
1636 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1640 u32 delsys = SYS_UNDEFINED;
1641 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1642 enum dvbv3_emulation_type type;
1645 * It was reported that some old DVBv5 applications were
1646 * filling delivery_system with SYS_UNDEFINED. If this happens,
1647 * assume that the application wants to use the first supported
1650 if (desired_system == SYS_UNDEFINED)
1651 desired_system = fe->ops.delsys[0];
1654 * This is a DVBv5 call. So, it likely knows the supported
1655 * delivery systems. So, check if the desired delivery system is
1659 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1660 if (fe->ops.delsys[ncaps] == desired_system) {
1661 c->delivery_system = desired_system;
1662 dev_dbg(fe->dvb->device,
1663 "%s: Changing delivery system to %d\n",
1664 __func__, desired_system);
1671 * The requested delivery system isn't supported. Maybe userspace
1672 * is requesting a DVBv3 compatible delivery system.
1674 * The emulation only works if the desired system is one of the
1675 * delivery systems supported by DVBv3 API
1677 if (!is_dvbv3_delsys(desired_system)) {
1678 dev_dbg(fe->dvb->device,
1679 "%s: Delivery system %d not supported.\n",
1680 __func__, desired_system);
1684 type = dvbv3_type(desired_system);
1687 * Get the last non-DVBv3 delivery system that has the same type
1688 * of the desired system
1691 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1692 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1693 delsys = fe->ops.delsys[ncaps];
1697 /* There's nothing compatible with the desired delivery system */
1698 if (delsys == SYS_UNDEFINED) {
1699 dev_dbg(fe->dvb->device,
1700 "%s: Delivery system %d not supported on emulation mode.\n",
1701 __func__, desired_system);
1705 dev_dbg(fe->dvb->device,
1706 "%s: Using delivery system %d emulated as if it were %d\n",
1707 __func__, delsys, desired_system);
1709 return emulate_delivery_system(fe, desired_system);
1713 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1714 * @fe: frontend struct
1716 * A DVBv3 call doesn't know what's the desired system it wants. It also
1717 * doesn't allow to switch between different types. Due to that, userspace
1718 * should use DVBv5 instead.
1719 * However, in order to avoid breaking userspace API, limited backward
1720 * compatibility support is provided.
1722 * There are some delivery systems that are incompatible with DVBv3 calls.
1724 * This routine should work fine for frontends that support just one delivery
1727 * For frontends that support multiple frontends:
1728 * 1) It defaults to use the first supported delivery system. There's an
1729 * userspace application that allows changing it at runtime;
1731 * 2) If the current delivery system is not compatible with DVBv3, it gets
1732 * the first one that it is compatible.
1734 * NOTE: in order for this to work with applications like Kaffeine that
1735 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1736 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1737 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1740 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1743 u32 delsys = SYS_UNDEFINED;
1744 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1746 /* If not set yet, defaults to the first supported delivery system */
1747 if (c->delivery_system == SYS_UNDEFINED)
1748 c->delivery_system = fe->ops.delsys[0];
1751 * Trivial case: just use the current one, if it already a DVBv3
1754 if (is_dvbv3_delsys(c->delivery_system)) {
1755 dev_dbg(fe->dvb->device,
1756 "%s: Using delivery system to %d\n",
1757 __func__, c->delivery_system);
1762 * Seek for the first delivery system that it is compatible with a
1766 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1767 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1768 delsys = fe->ops.delsys[ncaps];
1773 if (delsys == SYS_UNDEFINED) {
1774 dev_dbg(fe->dvb->device,
1775 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1779 return emulate_delivery_system(fe, delsys);
1782 static int dtv_property_process_set(struct dvb_frontend *fe,
1783 struct dtv_property *tvp,
1787 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1789 /* Allow the frontend to validate incoming properties */
1790 if (fe->ops.set_property) {
1791 r = fe->ops.set_property(fe, tvp);
1796 dtv_property_dump(fe, true, tvp);
1801 * Reset a cache of data specific to the frontend here. This does
1802 * not effect hardware.
1804 dvb_frontend_clear_cache(fe);
1807 /* interpret the cache of data, build either a traditional frontend
1808 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1811 c->state = tvp->cmd;
1812 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1815 r = dtv_set_frontend(fe);
1818 c->frequency = tvp->u.data;
1820 case DTV_MODULATION:
1821 c->modulation = tvp->u.data;
1823 case DTV_BANDWIDTH_HZ:
1824 c->bandwidth_hz = tvp->u.data;
1827 c->inversion = tvp->u.data;
1829 case DTV_SYMBOL_RATE:
1830 c->symbol_rate = tvp->u.data;
1833 c->fec_inner = tvp->u.data;
1836 c->pilot = tvp->u.data;
1839 c->rolloff = tvp->u.data;
1841 case DTV_DELIVERY_SYSTEM:
1842 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1845 c->voltage = tvp->u.data;
1846 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1847 (void *)c->voltage);
1850 c->sectone = tvp->u.data;
1851 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1852 (void *)c->sectone);
1854 case DTV_CODE_RATE_HP:
1855 c->code_rate_HP = tvp->u.data;
1857 case DTV_CODE_RATE_LP:
1858 c->code_rate_LP = tvp->u.data;
1860 case DTV_GUARD_INTERVAL:
1861 c->guard_interval = tvp->u.data;
1863 case DTV_TRANSMISSION_MODE:
1864 c->transmission_mode = tvp->u.data;
1867 c->hierarchy = tvp->u.data;
1869 case DTV_INTERLEAVING:
1870 c->interleaving = tvp->u.data;
1873 /* ISDB-T Support here */
1874 case DTV_ISDBT_PARTIAL_RECEPTION:
1875 c->isdbt_partial_reception = tvp->u.data;
1877 case DTV_ISDBT_SOUND_BROADCASTING:
1878 c->isdbt_sb_mode = tvp->u.data;
1880 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1881 c->isdbt_sb_subchannel = tvp->u.data;
1883 case DTV_ISDBT_SB_SEGMENT_IDX:
1884 c->isdbt_sb_segment_idx = tvp->u.data;
1886 case DTV_ISDBT_SB_SEGMENT_COUNT:
1887 c->isdbt_sb_segment_count = tvp->u.data;
1889 case DTV_ISDBT_LAYER_ENABLED:
1890 c->isdbt_layer_enabled = tvp->u.data;
1892 case DTV_ISDBT_LAYERA_FEC:
1893 c->layer[0].fec = tvp->u.data;
1895 case DTV_ISDBT_LAYERA_MODULATION:
1896 c->layer[0].modulation = tvp->u.data;
1898 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1899 c->layer[0].segment_count = tvp->u.data;
1901 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1902 c->layer[0].interleaving = tvp->u.data;
1904 case DTV_ISDBT_LAYERB_FEC:
1905 c->layer[1].fec = tvp->u.data;
1907 case DTV_ISDBT_LAYERB_MODULATION:
1908 c->layer[1].modulation = tvp->u.data;
1910 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1911 c->layer[1].segment_count = tvp->u.data;
1913 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1914 c->layer[1].interleaving = tvp->u.data;
1916 case DTV_ISDBT_LAYERC_FEC:
1917 c->layer[2].fec = tvp->u.data;
1919 case DTV_ISDBT_LAYERC_MODULATION:
1920 c->layer[2].modulation = tvp->u.data;
1922 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1923 c->layer[2].segment_count = tvp->u.data;
1925 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1926 c->layer[2].interleaving = tvp->u.data;
1929 /* Multistream support */
1931 case DTV_DVBT2_PLP_ID_LEGACY:
1932 c->stream_id = tvp->u.data;
1936 case DTV_ATSCMH_PARADE_ID:
1937 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1939 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1940 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1944 c->lna = tvp->u.data;
1945 if (fe->ops.set_lna)
1946 r = fe->ops.set_lna(fe);
1958 static int dvb_frontend_ioctl(struct file *file,
1959 unsigned int cmd, void *parg)
1961 struct dvb_device *dvbdev = file->private_data;
1962 struct dvb_frontend *fe = dvbdev->priv;
1963 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1964 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1965 int err = -EOPNOTSUPP;
1967 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
1968 if (down_interruptible(&fepriv->sem))
1969 return -ERESTARTSYS;
1971 if (fe->exit != DVB_FE_NO_EXIT) {
1976 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1977 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1978 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1983 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1984 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1986 c->state = DTV_UNDEFINED;
1987 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1994 static int dvb_frontend_ioctl_properties(struct file *file,
1995 unsigned int cmd, void *parg)
1997 struct dvb_device *dvbdev = file->private_data;
1998 struct dvb_frontend *fe = dvbdev->priv;
1999 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2000 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2003 struct dtv_properties *tvps = parg;
2004 struct dtv_property *tvp = NULL;
2007 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2009 if (cmd == FE_SET_PROPERTY) {
2010 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2011 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2013 /* Put an arbitrary limit on the number of messages that can
2014 * be sent at once */
2015 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2018 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
2020 return PTR_ERR(tvp);
2022 for (i = 0; i < tvps->num; i++) {
2023 err = dtv_property_process_set(fe, tvp + i, file);
2026 (tvp + i)->result = err;
2029 if (c->state == DTV_TUNE)
2030 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
2032 } else if (cmd == FE_GET_PROPERTY) {
2033 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2035 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2036 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2038 /* Put an arbitrary limit on the number of messages that can
2039 * be sent at once */
2040 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2043 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
2045 return PTR_ERR(tvp);
2048 * Let's use our own copy of property cache, in order to
2049 * avoid mangling with DTV zigzag logic, as drivers might
2050 * return crap, if they don't check if the data is available
2051 * before updating the properties cache.
2053 if (fepriv->state != FESTATE_IDLE) {
2054 err = dtv_get_frontend(fe, &getp, NULL);
2058 for (i = 0; i < tvps->num; i++) {
2059 err = dtv_property_process_get(fe, &getp, tvp + i, file);
2062 (tvp + i)->result = err;
2065 if (copy_to_user((void __user *)tvps->props, tvp,
2066 tvps->num * sizeof(struct dtv_property))) {
2079 static int dtv_set_frontend(struct dvb_frontend *fe)
2081 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2082 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2083 struct dvb_frontend_tune_settings fetunesettings;
2086 if (dvb_frontend_check_parameters(fe) < 0)
2090 * Initialize output parameters to match the values given by
2091 * the user. FE_SET_FRONTEND triggers an initial frontend event
2092 * with status = 0, which copies output parameters to userspace.
2094 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
2097 * Be sure that the bandwidth will be filled for all
2098 * non-satellite systems, as tuners need to know what
2099 * low pass/Nyquist half filter should be applied, in
2100 * order to avoid inter-channel noise.
2102 * ISDB-T and DVB-T/T2 already sets bandwidth.
2103 * ATSC and DVB-C don't set, so, the core should fill it.
2105 * On DVB-C Annex A and C, the bandwidth is a function of
2106 * the roll-off and symbol rate. Annex B defines different
2107 * roll-off factors depending on the modulation. Fortunately,
2108 * Annex B is only used with 6MHz, so there's no need to
2111 * While not officially supported, a side effect of handling it at
2112 * the cache level is that a program could retrieve the bandwidth
2113 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2115 switch (c->delivery_system) {
2117 case SYS_DVBC_ANNEX_B:
2118 c->bandwidth_hz = 6000000;
2120 case SYS_DVBC_ANNEX_A:
2123 case SYS_DVBC_ANNEX_C:
2132 switch (c->rolloff) {
2148 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
2150 /* force auto frequency inversion if requested */
2151 if (dvb_force_auto_inversion)
2152 c->inversion = INVERSION_AUTO;
2155 * without hierarchical coding code_rate_LP is irrelevant,
2156 * so we tolerate the otherwise invalid FEC_NONE setting
2158 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2159 c->code_rate_LP = FEC_AUTO;
2161 /* get frontend-specific tuning settings */
2162 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2163 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2164 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2165 fepriv->max_drift = fetunesettings.max_drift;
2166 fepriv->step_size = fetunesettings.step_size;
2168 /* default values */
2169 switch (c->delivery_system) {
2174 case SYS_DVBC_ANNEX_A:
2175 case SYS_DVBC_ANNEX_C:
2176 fepriv->min_delay = HZ / 20;
2177 fepriv->step_size = c->symbol_rate / 16000;
2178 fepriv->max_drift = c->symbol_rate / 2000;
2184 fepriv->min_delay = HZ / 20;
2185 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2186 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2190 * FIXME: This sounds wrong! if freqency_stepsize is
2191 * defined by the frontend, why not use it???
2193 fepriv->min_delay = HZ / 20;
2194 fepriv->step_size = 0; /* no zigzag */
2195 fepriv->max_drift = 0;
2199 if (dvb_override_tune_delay > 0)
2200 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2202 fepriv->state = FESTATE_RETUNE;
2204 /* Request the search algorithm to search */
2205 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2207 dvb_frontend_clear_events(fe);
2208 dvb_frontend_add_event(fe, 0);
2209 dvb_frontend_wakeup(fe);
2216 static int dvb_frontend_ioctl_legacy(struct file *file,
2217 unsigned int cmd, void *parg)
2219 struct dvb_device *dvbdev = file->private_data;
2220 struct dvb_frontend *fe = dvbdev->priv;
2221 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2222 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2223 int err = -EOPNOTSUPP;
2227 struct dvb_frontend_info* info = parg;
2229 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2230 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2233 * Associate the 4 delivery systems supported by DVBv3
2234 * API with their DVBv5 counterpart. For the other standards,
2235 * use the closest type, assuming that it would hopefully
2236 * work with a DVBv3 application.
2237 * It should be noticed that, on multi-frontend devices with
2238 * different types (terrestrial and cable, for example),
2239 * a pure DVBv3 application won't be able to use all delivery
2240 * systems. Yet, changing the DVBv5 cache to the other delivery
2241 * system should be enough for making it work.
2243 switch (dvbv3_type(c->delivery_system)) {
2245 info->type = FE_QPSK;
2248 info->type = FE_ATSC;
2251 info->type = FE_QAM;
2254 info->type = FE_OFDM;
2257 dev_err(fe->dvb->device,
2258 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2259 __func__, c->delivery_system);
2260 fe->ops.info.type = FE_OFDM;
2262 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2263 __func__, c->delivery_system, fe->ops.info.type);
2265 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2266 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2267 info->caps |= FE_CAN_INVERSION_AUTO;
2272 case FE_READ_STATUS: {
2273 enum fe_status *status = parg;
2275 /* if retune was requested but hasn't occurred yet, prevent
2276 * that user get signal state from previous tuning */
2277 if (fepriv->state == FESTATE_RETUNE ||
2278 fepriv->state == FESTATE_ERROR) {
2284 if (fe->ops.read_status)
2285 err = fe->ops.read_status(fe, status);
2290 if (fe->ops.read_ber) {
2292 err = fe->ops.read_ber(fe, (__u32 *) parg);
2298 case FE_READ_SIGNAL_STRENGTH:
2299 if (fe->ops.read_signal_strength) {
2301 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2308 if (fe->ops.read_snr) {
2310 err = fe->ops.read_snr(fe, (__u16 *) parg);
2316 case FE_READ_UNCORRECTED_BLOCKS:
2317 if (fe->ops.read_ucblocks) {
2319 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2325 case FE_DISEQC_RESET_OVERLOAD:
2326 if (fe->ops.diseqc_reset_overload) {
2327 err = fe->ops.diseqc_reset_overload(fe);
2328 fepriv->state = FESTATE_DISEQC;
2333 case FE_DISEQC_SEND_MASTER_CMD:
2334 if (fe->ops.diseqc_send_master_cmd) {
2335 struct dvb_diseqc_master_cmd *cmd = parg;
2337 if (cmd->msg_len > sizeof(cmd->msg)) {
2341 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
2342 fepriv->state = FESTATE_DISEQC;
2347 case FE_DISEQC_SEND_BURST:
2348 if (fe->ops.diseqc_send_burst) {
2349 err = fe->ops.diseqc_send_burst(fe,
2350 (enum fe_sec_mini_cmd)parg);
2351 fepriv->state = FESTATE_DISEQC;
2357 if (fe->ops.set_tone) {
2358 err = fe->ops.set_tone(fe,
2359 (enum fe_sec_tone_mode)parg);
2360 fepriv->tone = (enum fe_sec_tone_mode)parg;
2361 fepriv->state = FESTATE_DISEQC;
2366 case FE_SET_VOLTAGE:
2367 if (fe->ops.set_voltage) {
2368 err = fe->ops.set_voltage(fe,
2369 (enum fe_sec_voltage)parg);
2370 fepriv->voltage = (enum fe_sec_voltage)parg;
2371 fepriv->state = FESTATE_DISEQC;
2376 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2377 if (fe->ops.dishnetwork_send_legacy_command) {
2378 err = fe->ops.dishnetwork_send_legacy_command(fe,
2379 (unsigned long)parg);
2380 fepriv->state = FESTATE_DISEQC;
2382 } else if (fe->ops.set_voltage) {
2384 * NOTE: This is a fallback condition. Some frontends
2385 * (stv0299 for instance) take longer than 8msec to
2386 * respond to a set_voltage command. Those switches
2387 * need custom routines to switch properly. For all
2388 * other frontends, the following should work ok.
2389 * Dish network legacy switches (as used by Dish500)
2390 * are controlled by sending 9-bit command words
2391 * spaced 8msec apart.
2392 * the actual command word is switch/port dependent
2393 * so it is up to the userspace application to send
2394 * the right command.
2395 * The command must always start with a '0' after
2396 * initialization, so parg is 8 bits and does not
2397 * include the initialization or start bit
2399 unsigned long swcmd = ((unsigned long) parg) << 1;
2404 if (dvb_frontend_debug)
2405 dprintk("%s switch command: 0x%04lx\n",
2407 nexttime = ktime_get_boottime();
2408 if (dvb_frontend_debug)
2410 /* before sending a command, initialize by sending
2411 * a 32ms 18V to the switch
2413 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2414 dvb_frontend_sleep_until(&nexttime, 32000);
2416 for (i = 0; i < 9; i++) {
2417 if (dvb_frontend_debug)
2418 tv[i+1] = ktime_get_boottime();
2419 if ((swcmd & 0x01) != last) {
2420 /* set voltage to (last ? 13V : 18V) */
2421 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2422 last = (last) ? 0 : 1;
2426 dvb_frontend_sleep_until(&nexttime, 8000);
2428 if (dvb_frontend_debug) {
2429 dprintk("%s(%d): switch delay (should be 32k followed by all 8k)\n",
2430 __func__, fe->dvb->num);
2431 for (i = 1; i < 10; i++)
2432 pr_info("%d: %d\n", i,
2433 (int) ktime_us_delta(tv[i], tv[i-1]));
2436 fepriv->state = FESTATE_DISEQC;
2441 case FE_DISEQC_RECV_SLAVE_REPLY:
2442 if (fe->ops.diseqc_recv_slave_reply)
2443 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2446 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2447 if (fe->ops.enable_high_lnb_voltage)
2448 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2451 case FE_SET_FRONTEND:
2452 err = dvbv3_set_delivery_system(fe);
2456 err = dtv_property_cache_sync(fe, c, parg);
2459 err = dtv_set_frontend(fe);
2462 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2465 case FE_GET_FRONTEND: {
2466 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2469 * Let's use our own copy of property cache, in order to
2470 * avoid mangling with DTV zigzag logic, as drivers might
2471 * return crap, if they don't check if the data is available
2472 * before updating the properties cache.
2474 err = dtv_get_frontend(fe, &getp, parg);
2477 case FE_SET_FRONTEND_TUNE_MODE:
2478 fepriv->tune_mode_flags = (unsigned long) parg;
2487 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2489 struct dvb_device *dvbdev = file->private_data;
2490 struct dvb_frontend *fe = dvbdev->priv;
2491 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2493 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2495 poll_wait (file, &fepriv->events.wait_queue, wait);
2497 if (fepriv->events.eventw != fepriv->events.eventr)
2498 return (POLLIN | POLLRDNORM | POLLPRI);
2503 static int dvb_frontend_open(struct inode *inode, struct file *file)
2505 struct dvb_device *dvbdev = file->private_data;
2506 struct dvb_frontend *fe = dvbdev->priv;
2507 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2508 struct dvb_adapter *adapter = fe->dvb;
2511 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2512 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2515 if (adapter->mfe_shared) {
2516 mutex_lock (&adapter->mfe_lock);
2518 if (adapter->mfe_dvbdev == NULL)
2519 adapter->mfe_dvbdev = dvbdev;
2521 else if (adapter->mfe_dvbdev != dvbdev) {
2523 *mfedev = adapter->mfe_dvbdev;
2525 *mfe = mfedev->priv;
2526 struct dvb_frontend_private
2527 *mfepriv = mfe->frontend_priv;
2528 int mferetry = (dvb_mfe_wait_time << 1);
2530 mutex_unlock (&adapter->mfe_lock);
2531 while (mferetry-- && (mfedev->users != -1 ||
2532 mfepriv->thread != NULL)) {
2533 if(msleep_interruptible(500)) {
2534 if(signal_pending(current))
2539 mutex_lock (&adapter->mfe_lock);
2540 if(adapter->mfe_dvbdev != dvbdev) {
2541 mfedev = adapter->mfe_dvbdev;
2543 mfepriv = mfe->frontend_priv;
2544 if (mfedev->users != -1 ||
2545 mfepriv->thread != NULL) {
2546 mutex_unlock (&adapter->mfe_lock);
2549 adapter->mfe_dvbdev = dvbdev;
2554 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2555 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2558 /* If we took control of the bus, we need to force
2559 reinitialization. This is because many ts_bus_ctrl()
2560 functions strobe the RESET pin on the demod, and if the
2561 frontend thread already exists then the dvb_init() routine
2562 won't get called (which is what usually does initial
2563 register configuration). */
2564 fepriv->reinitialise = 1;
2567 if ((ret = dvb_generic_open (inode, file)) < 0)
2570 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2571 /* normal tune mode when opened R/W */
2572 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2574 fepriv->voltage = -1;
2576 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2577 if (fe->dvb->mdev) {
2578 mutex_lock(&fe->dvb->mdev->graph_mutex);
2579 if (fe->dvb->mdev->enable_source)
2580 ret = fe->dvb->mdev->enable_source(
2583 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2585 dev_err(fe->dvb->device,
2586 "Tuner is busy. Error %d\n", ret);
2591 ret = dvb_frontend_start (fe);
2595 /* empty event queue */
2596 fepriv->events.eventr = fepriv->events.eventw = 0;
2599 dvb_frontend_get(fe);
2601 if (adapter->mfe_shared)
2602 mutex_unlock (&adapter->mfe_lock);
2606 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2607 if (fe->dvb->mdev) {
2608 mutex_lock(&fe->dvb->mdev->graph_mutex);
2609 if (fe->dvb->mdev->disable_source)
2610 fe->dvb->mdev->disable_source(dvbdev->entity);
2611 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2615 dvb_generic_release(inode, file);
2617 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2618 fe->ops.ts_bus_ctrl(fe, 0);
2620 if (adapter->mfe_shared)
2621 mutex_unlock (&adapter->mfe_lock);
2625 static int dvb_frontend_release(struct inode *inode, struct file *file)
2627 struct dvb_device *dvbdev = file->private_data;
2628 struct dvb_frontend *fe = dvbdev->priv;
2629 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2632 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2634 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2635 fepriv->release_jiffies = jiffies;
2639 ret = dvb_generic_release (inode, file);
2641 if (dvbdev->users == -1) {
2642 wake_up(&fepriv->wait_queue);
2643 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2644 if (fe->dvb->mdev) {
2645 mutex_lock(&fe->dvb->mdev->graph_mutex);
2646 if (fe->dvb->mdev->disable_source)
2647 fe->dvb->mdev->disable_source(dvbdev->entity);
2648 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2651 if (fe->exit != DVB_FE_NO_EXIT)
2652 wake_up(&dvbdev->wait_queue);
2653 if (fe->ops.ts_bus_ctrl)
2654 fe->ops.ts_bus_ctrl(fe, 0);
2657 dvb_frontend_put(fe);
2662 static const struct file_operations dvb_frontend_fops = {
2663 .owner = THIS_MODULE,
2664 .unlocked_ioctl = dvb_generic_ioctl,
2665 .poll = dvb_frontend_poll,
2666 .open = dvb_frontend_open,
2667 .release = dvb_frontend_release,
2668 .llseek = noop_llseek,
2671 int dvb_frontend_suspend(struct dvb_frontend *fe)
2675 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2678 if (fe->ops.tuner_ops.suspend)
2679 ret = fe->ops.tuner_ops.suspend(fe);
2680 else if (fe->ops.tuner_ops.sleep)
2681 ret = fe->ops.tuner_ops.sleep(fe);
2684 ret = fe->ops.sleep(fe);
2688 EXPORT_SYMBOL(dvb_frontend_suspend);
2690 int dvb_frontend_resume(struct dvb_frontend *fe)
2692 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2695 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2698 fe->exit = DVB_FE_DEVICE_RESUME;
2700 ret = fe->ops.init(fe);
2702 if (fe->ops.tuner_ops.resume)
2703 ret = fe->ops.tuner_ops.resume(fe);
2704 else if (fe->ops.tuner_ops.init)
2705 ret = fe->ops.tuner_ops.init(fe);
2707 if (fe->ops.set_tone && fepriv->tone != -1)
2708 fe->ops.set_tone(fe, fepriv->tone);
2709 if (fe->ops.set_voltage && fepriv->voltage != -1)
2710 fe->ops.set_voltage(fe, fepriv->voltage);
2712 fe->exit = DVB_FE_NO_EXIT;
2713 fepriv->state = FESTATE_RETUNE;
2714 dvb_frontend_wakeup(fe);
2718 EXPORT_SYMBOL(dvb_frontend_resume);
2720 int dvb_register_frontend(struct dvb_adapter* dvb,
2721 struct dvb_frontend* fe)
2723 struct dvb_frontend_private *fepriv;
2724 const struct dvb_device dvbdev_template = {
2728 .fops = &dvb_frontend_fops,
2729 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2730 .name = fe->ops.info.name,
2732 .kernel_ioctl = dvb_frontend_ioctl
2735 dev_dbg(dvb->device, "%s:\n", __func__);
2737 if (mutex_lock_interruptible(&frontend_mutex))
2738 return -ERESTARTSYS;
2740 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2741 if (fe->frontend_priv == NULL) {
2742 mutex_unlock(&frontend_mutex);
2745 fepriv = fe->frontend_priv;
2747 kref_init(&fe->refcount);
2750 * After initialization, there need to be two references: one
2751 * for dvb_unregister_frontend(), and another one for
2752 * dvb_frontend_detach().
2754 dvb_frontend_get(fe);
2756 sema_init(&fepriv->sem, 1);
2757 init_waitqueue_head (&fepriv->wait_queue);
2758 init_waitqueue_head (&fepriv->events.wait_queue);
2759 mutex_init(&fepriv->events.mtx);
2761 fepriv->inversion = INVERSION_OFF;
2763 dev_info(fe->dvb->device,
2764 "DVB: registering adapter %i frontend %i (%s)...\n",
2765 fe->dvb->num, fe->id, fe->ops.info.name);
2767 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2768 fe, DVB_DEVICE_FRONTEND, 0);
2771 * Initialize the cache to the proper values according with the
2772 * first supported delivery system (ops->delsys[0])
2775 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2776 dvb_frontend_clear_cache(fe);
2778 mutex_unlock(&frontend_mutex);
2781 EXPORT_SYMBOL(dvb_register_frontend);
2783 int dvb_unregister_frontend(struct dvb_frontend* fe)
2785 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2786 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2788 mutex_lock(&frontend_mutex);
2789 dvb_frontend_stop(fe);
2790 dvb_remove_device(fepriv->dvbdev);
2792 /* fe is invalid now */
2793 mutex_unlock(&frontend_mutex);
2794 dvb_frontend_put(fe);
2797 EXPORT_SYMBOL(dvb_unregister_frontend);
2799 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
2800 void (*release)(struct dvb_frontend *fe))
2804 #ifdef CONFIG_MEDIA_ATTACH
2805 dvb_detach(release);
2810 void dvb_frontend_detach(struct dvb_frontend* fe)
2812 dvb_frontend_invoke_release(fe, fe->ops.release_sec);
2813 dvb_frontend_invoke_release(fe, fe->ops.tuner_ops.release);
2814 dvb_frontend_invoke_release(fe, fe->ops.analog_ops.release);
2815 dvb_frontend_invoke_release(fe, fe->ops.detach);
2816 dvb_frontend_put(fe);
2818 EXPORT_SYMBOL(dvb_frontend_detach);