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 kref *ref)
146 struct dvb_frontend *fe =
147 container_of(ref, struct dvb_frontend, refcount);
148 struct dvb_frontend_private *fepriv = fe->frontend_priv;
150 dvb_free_device(fepriv->dvbdev);
152 dvb_frontend_invoke_release(fe, fe->ops.release);
157 static void dvb_frontend_put(struct dvb_frontend *fe)
159 kref_put(&fe->refcount, dvb_frontend_free);
162 static void dvb_frontend_get(struct dvb_frontend *fe)
164 kref_get(&fe->refcount);
167 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
168 static int dtv_get_frontend(struct dvb_frontend *fe,
169 struct dtv_frontend_properties *c,
170 struct dvb_frontend_parameters *p_out);
172 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
173 const struct dtv_frontend_properties *c,
174 struct dvb_frontend_parameters *p);
176 static bool has_get_frontend(struct dvb_frontend *fe)
178 return fe->ops.get_frontend != NULL;
182 * Due to DVBv3 API calls, a delivery system should be mapped into one of
183 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
184 * otherwise, a DVBv3 call will fail.
186 enum dvbv3_emulation_type {
194 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
196 switch (delivery_system) {
197 case SYS_DVBC_ANNEX_A:
198 case SYS_DVBC_ANNEX_C:
213 case SYS_DVBC_ANNEX_B:
221 * Doesn't know how to emulate those types and/or
222 * there's no frontend driver from this type yet
223 * with some emulation code, so, we're not sure yet how
224 * to handle them, or they're not compatible with a DVBv3 call.
226 return DVBV3_UNKNOWN;
230 static void dvb_frontend_add_event(struct dvb_frontend *fe,
231 enum fe_status status)
233 struct dvb_frontend_private *fepriv = fe->frontend_priv;
234 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
235 struct dvb_fe_events *events = &fepriv->events;
236 struct dvb_frontend_event *e;
239 dev_dbg(fe->dvb->device, "%s:\n", __func__);
241 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
242 dtv_get_frontend(fe, c, &fepriv->parameters_out);
244 mutex_lock(&events->mtx);
246 wp = (events->eventw + 1) % MAX_EVENT;
247 if (wp == events->eventr) {
248 events->overflow = 1;
249 events->eventr = (events->eventr + 1) % MAX_EVENT;
252 e = &events->events[events->eventw];
254 e->parameters = fepriv->parameters_out;
258 mutex_unlock(&events->mtx);
260 wake_up_interruptible (&events->wait_queue);
263 static int dvb_frontend_get_event(struct dvb_frontend *fe,
264 struct dvb_frontend_event *event, int flags)
266 struct dvb_frontend_private *fepriv = fe->frontend_priv;
267 struct dvb_fe_events *events = &fepriv->events;
269 dev_dbg(fe->dvb->device, "%s:\n", __func__);
271 if (events->overflow) {
272 events->overflow = 0;
276 if (events->eventw == events->eventr) {
279 if (flags & O_NONBLOCK)
284 ret = wait_event_interruptible (events->wait_queue,
285 events->eventw != events->eventr);
287 if (down_interruptible (&fepriv->sem))
294 mutex_lock(&events->mtx);
295 *event = events->events[events->eventr];
296 events->eventr = (events->eventr + 1) % MAX_EVENT;
297 mutex_unlock(&events->mtx);
302 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
304 struct dvb_frontend_private *fepriv = fe->frontend_priv;
305 struct dvb_fe_events *events = &fepriv->events;
307 mutex_lock(&events->mtx);
308 events->eventr = events->eventw;
309 mutex_unlock(&events->mtx);
312 static void dvb_frontend_init(struct dvb_frontend *fe)
314 dev_dbg(fe->dvb->device,
315 "%s: initialising adapter %i frontend %i (%s)...\n",
316 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
320 if (fe->ops.tuner_ops.init) {
321 if (fe->ops.i2c_gate_ctrl)
322 fe->ops.i2c_gate_ctrl(fe, 1);
323 fe->ops.tuner_ops.init(fe);
324 if (fe->ops.i2c_gate_ctrl)
325 fe->ops.i2c_gate_ctrl(fe, 0);
329 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
331 struct dvb_frontend_private *fepriv = fe->frontend_priv;
333 fepriv->reinitialise = 1;
334 dvb_frontend_wakeup(fe);
336 EXPORT_SYMBOL(dvb_frontend_reinitialise);
338 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
341 struct dvb_frontend *fe = fepriv->dvbdev->priv;
343 dev_dbg(fe->dvb->device, "%s:\n", __func__);
346 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
348 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
350 q2 = fepriv->quality - 128;
353 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
357 * Performs automatic twiddling of frontend parameters.
359 * @param fe The frontend concerned.
360 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
361 * @returns Number of complete iterations that have been performed.
363 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
368 struct dvb_frontend_private *fepriv = fe->frontend_priv;
369 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
370 int original_inversion = c->inversion;
371 u32 original_frequency = c->frequency;
373 /* are we using autoinversion? */
374 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
375 (c->inversion == INVERSION_AUTO));
377 /* setup parameters correctly */
379 /* calculate the lnb_drift */
380 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
382 /* wrap the auto_step if we've exceeded the maximum drift */
383 if (fepriv->lnb_drift > fepriv->max_drift) {
384 fepriv->auto_step = 0;
385 fepriv->auto_sub_step = 0;
386 fepriv->lnb_drift = 0;
389 /* perform inversion and +/- zigzag */
390 switch(fepriv->auto_sub_step) {
392 /* try with the current inversion and current drift setting */
397 if (!autoinversion) break;
399 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
404 if (fepriv->lnb_drift == 0) break;
406 fepriv->lnb_drift = -fepriv->lnb_drift;
411 if (fepriv->lnb_drift == 0) break;
412 if (!autoinversion) break;
414 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
415 fepriv->lnb_drift = -fepriv->lnb_drift;
421 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
425 if (!ready) fepriv->auto_sub_step++;
428 /* if this attempt would hit where we started, indicate a complete
429 * iteration has occurred */
430 if ((fepriv->auto_step == fepriv->started_auto_step) &&
431 (fepriv->auto_sub_step == 0) && check_wrapped) {
435 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
436 "auto_sub_step:%i started_auto_step:%i\n",
437 __func__, fepriv->lnb_drift, fepriv->inversion,
438 fepriv->auto_step, fepriv->auto_sub_step,
439 fepriv->started_auto_step);
441 /* set the frontend itself */
442 c->frequency += fepriv->lnb_drift;
444 c->inversion = fepriv->inversion;
446 if (fe->ops.set_frontend)
447 fe_set_err = fe->ops.set_frontend(fe);
449 if (fe_set_err < 0) {
450 fepriv->state = FESTATE_ERROR;
454 c->frequency = original_frequency;
455 c->inversion = original_inversion;
457 fepriv->auto_sub_step++;
461 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
463 enum fe_status s = FE_NONE;
465 struct dvb_frontend_private *fepriv = fe->frontend_priv;
466 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
468 /* if we've got no parameters, just keep idling */
469 if (fepriv->state & FESTATE_IDLE) {
470 fepriv->delay = 3*HZ;
475 /* in SCAN mode, we just set the frontend when asked and leave it alone */
476 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
477 if (fepriv->state & FESTATE_RETUNE) {
479 if (fe->ops.set_frontend)
480 retval = fe->ops.set_frontend(fe);
483 fepriv->state = FESTATE_ERROR;
485 fepriv->state = FESTATE_TUNED;
487 fepriv->delay = 3*HZ;
492 /* get the frontend status */
493 if (fepriv->state & FESTATE_RETUNE) {
496 if (fe->ops.read_status)
497 fe->ops.read_status(fe, &s);
498 if (s != fepriv->status) {
499 dvb_frontend_add_event(fe, s);
504 /* if we're not tuned, and we have a lock, move to the TUNED state */
505 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
506 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
507 fepriv->state = FESTATE_TUNED;
509 /* if we're tuned, then we have determined the correct inversion */
510 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
511 (c->inversion == INVERSION_AUTO)) {
512 c->inversion = fepriv->inversion;
517 /* if we are tuned already, check we're still locked */
518 if (fepriv->state & FESTATE_TUNED) {
519 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
521 /* we're tuned, and the lock is still good... */
522 if (s & FE_HAS_LOCK) {
524 } else { /* if we _WERE_ tuned, but now don't have a lock */
525 fepriv->state = FESTATE_ZIGZAG_FAST;
526 fepriv->started_auto_step = fepriv->auto_step;
527 fepriv->check_wrapped = 0;
531 /* don't actually do anything if we're in the LOSTLOCK state,
532 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
533 if ((fepriv->state & FESTATE_LOSTLOCK) &&
534 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
535 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
539 /* don't do anything if we're in the DISEQC state, since this
540 * might be someone with a motorized dish controlled by DISEQC.
541 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
542 if (fepriv->state & FESTATE_DISEQC) {
543 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
547 /* if we're in the RETUNE state, set everything up for a brand
548 * new scan, keeping the current inversion setting, as the next
549 * tune is _very_ likely to require the same */
550 if (fepriv->state & FESTATE_RETUNE) {
551 fepriv->lnb_drift = 0;
552 fepriv->auto_step = 0;
553 fepriv->auto_sub_step = 0;
554 fepriv->started_auto_step = 0;
555 fepriv->check_wrapped = 0;
559 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
560 fepriv->delay = fepriv->min_delay;
563 retval = dvb_frontend_swzigzag_autotune(fe,
564 fepriv->check_wrapped);
568 /* OK, if we've run out of trials at the fast speed.
569 * Drop back to slow for the _next_ attempt */
570 fepriv->state = FESTATE_SEARCHING_SLOW;
571 fepriv->started_auto_step = fepriv->auto_step;
574 fepriv->check_wrapped = 1;
576 /* if we've just retuned, enter the ZIGZAG_FAST state.
577 * This ensures we cannot return from an
578 * FE_SET_FRONTEND ioctl before the first frontend tune
580 if (fepriv->state & FESTATE_RETUNE) {
581 fepriv->state = FESTATE_TUNING_FAST;
586 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
587 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
589 /* Note: don't bother checking for wrapping; we stay in this
590 * state until we get a lock */
591 dvb_frontend_swzigzag_autotune(fe, 0);
595 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
597 struct dvb_frontend_private *fepriv = fe->frontend_priv;
599 if (fe->exit != DVB_FE_NO_EXIT)
602 if (fepriv->dvbdev->writers == 1)
603 if (time_after_eq(jiffies, fepriv->release_jiffies +
604 dvb_shutdown_timeout * HZ))
610 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
612 struct dvb_frontend_private *fepriv = fe->frontend_priv;
614 if (fepriv->wakeup) {
618 return dvb_frontend_is_exiting(fe);
621 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
623 struct dvb_frontend_private *fepriv = fe->frontend_priv;
626 wake_up_interruptible(&fepriv->wait_queue);
629 static int dvb_frontend_thread(void *data)
631 struct dvb_frontend *fe = data;
632 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
633 struct dvb_frontend_private *fepriv = fe->frontend_priv;
634 enum fe_status s = FE_NONE;
635 enum dvbfe_algo algo;
636 bool re_tune = false;
637 bool semheld = false;
639 dev_dbg(fe->dvb->device, "%s:\n", __func__);
641 fepriv->check_wrapped = 0;
643 fepriv->delay = 3*HZ;
646 fepriv->reinitialise = 0;
648 dvb_frontend_init(fe);
652 up(&fepriv->sem); /* is locked when we enter the thread... */
654 wait_event_interruptible_timeout(fepriv->wait_queue,
655 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
656 || freezing(current),
659 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
660 /* got signal or quitting */
661 if (!down_interruptible(&fepriv->sem))
663 fe->exit = DVB_FE_NORMAL_EXIT;
670 if (down_interruptible(&fepriv->sem))
673 if (fepriv->reinitialise) {
674 dvb_frontend_init(fe);
675 if (fe->ops.set_tone && fepriv->tone != -1)
676 fe->ops.set_tone(fe, fepriv->tone);
677 if (fe->ops.set_voltage && fepriv->voltage != -1)
678 fe->ops.set_voltage(fe, fepriv->voltage);
679 fepriv->reinitialise = 0;
682 /* do an iteration of the tuning loop */
683 if (fe->ops.get_frontend_algo) {
684 algo = fe->ops.get_frontend_algo(fe);
687 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
689 if (fepriv->state & FESTATE_RETUNE) {
690 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
692 fepriv->state = FESTATE_TUNED;
698 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
700 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
701 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
702 dvb_frontend_add_event(fe, s);
707 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
708 dvb_frontend_swzigzag(fe);
710 case DVBFE_ALGO_CUSTOM:
711 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
712 if (fepriv->state & FESTATE_RETUNE) {
713 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
714 fepriv->state = FESTATE_TUNED;
716 /* Case where we are going to search for a carrier
717 * User asked us to retune again for some reason, possibly
718 * requesting a search with a new set of parameters
720 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
721 if (fe->ops.search) {
722 fepriv->algo_status = fe->ops.search(fe);
723 /* We did do a search as was requested, the flags are
724 * now unset as well and has the flags wrt to search.
727 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
730 /* Track the carrier if the search was successful */
731 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
732 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
733 fepriv->delay = HZ / 2;
735 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
736 fe->ops.read_status(fe, &s);
737 if (s != fepriv->status) {
738 dvb_frontend_add_event(fe, s); /* update event list */
740 if (!(s & FE_HAS_LOCK)) {
741 fepriv->delay = HZ / 10;
742 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
744 fepriv->delay = 60 * HZ;
749 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
753 dvb_frontend_swzigzag(fe);
757 if (dvb_powerdown_on_sleep) {
758 if (fe->ops.set_voltage)
759 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
760 if (fe->ops.tuner_ops.sleep) {
761 if (fe->ops.i2c_gate_ctrl)
762 fe->ops.i2c_gate_ctrl(fe, 1);
763 fe->ops.tuner_ops.sleep(fe);
764 if (fe->ops.i2c_gate_ctrl)
765 fe->ops.i2c_gate_ctrl(fe, 0);
771 fepriv->thread = NULL;
772 if (kthread_should_stop())
773 fe->exit = DVB_FE_DEVICE_REMOVED;
775 fe->exit = DVB_FE_NO_EXIT;
780 dvb_frontend_wakeup(fe);
784 static void dvb_frontend_stop(struct dvb_frontend *fe)
786 struct dvb_frontend_private *fepriv = fe->frontend_priv;
788 dev_dbg(fe->dvb->device, "%s:\n", __func__);
790 if (fe->exit != DVB_FE_DEVICE_REMOVED)
791 fe->exit = DVB_FE_NORMAL_EXIT;
797 kthread_stop(fepriv->thread);
799 sema_init(&fepriv->sem, 1);
800 fepriv->state = FESTATE_IDLE;
802 /* paranoia check in case a signal arrived */
804 dev_warn(fe->dvb->device,
805 "dvb_frontend_stop: warning: thread %p won't exit\n",
810 * Sleep for the amount of time given by add_usec parameter
812 * This needs to be as precise as possible, as it affects the detection of
813 * the dish tone command at the satellite subsystem. The precision is improved
814 * by using a scheduled msleep followed by udelay for the remainder.
816 void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
820 *waketime = ktime_add_us(*waketime, add_usec);
821 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
823 msleep((delta - 1500) / 1000);
824 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
829 EXPORT_SYMBOL(dvb_frontend_sleep_until);
831 static int dvb_frontend_start(struct dvb_frontend *fe)
834 struct dvb_frontend_private *fepriv = fe->frontend_priv;
835 struct task_struct *fe_thread;
837 dev_dbg(fe->dvb->device, "%s:\n", __func__);
839 if (fepriv->thread) {
840 if (fe->exit == DVB_FE_NO_EXIT)
843 dvb_frontend_stop (fe);
846 if (signal_pending(current))
848 if (down_interruptible (&fepriv->sem))
851 fepriv->state = FESTATE_IDLE;
852 fe->exit = DVB_FE_NO_EXIT;
853 fepriv->thread = NULL;
856 fe_thread = kthread_run(dvb_frontend_thread, fe,
857 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
858 if (IS_ERR(fe_thread)) {
859 ret = PTR_ERR(fe_thread);
860 dev_warn(fe->dvb->device,
861 "dvb_frontend_start: failed to start kthread (%d)\n",
866 fepriv->thread = fe_thread;
870 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
871 u32 *freq_min, u32 *freq_max)
873 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
875 if (fe->ops.info.frequency_max == 0)
876 *freq_max = fe->ops.tuner_ops.info.frequency_max;
877 else if (fe->ops.tuner_ops.info.frequency_max == 0)
878 *freq_max = fe->ops.info.frequency_max;
880 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
882 if (*freq_min == 0 || *freq_max == 0)
883 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
884 fe->dvb->num, fe->id);
887 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
889 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
893 /* range check: frequency */
894 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
895 if ((freq_min && c->frequency < freq_min) ||
896 (freq_max && c->frequency > freq_max)) {
897 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
898 fe->dvb->num, fe->id, c->frequency,
903 /* range check: symbol rate */
904 switch (c->delivery_system) {
908 case SYS_DVBC_ANNEX_A:
909 case SYS_DVBC_ANNEX_C:
910 if ((fe->ops.info.symbol_rate_min &&
911 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
912 (fe->ops.info.symbol_rate_max &&
913 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
914 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
915 fe->dvb->num, fe->id, c->symbol_rate,
916 fe->ops.info.symbol_rate_min,
917 fe->ops.info.symbol_rate_max);
927 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
929 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
933 delsys = c->delivery_system;
934 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
935 c->delivery_system = delsys;
937 c->state = DTV_CLEAR;
939 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
940 __func__, c->delivery_system);
942 c->transmission_mode = TRANSMISSION_MODE_AUTO;
943 c->bandwidth_hz = 0; /* AUTO */
944 c->guard_interval = GUARD_INTERVAL_AUTO;
945 c->hierarchy = HIERARCHY_AUTO;
947 c->code_rate_HP = FEC_AUTO;
948 c->code_rate_LP = FEC_AUTO;
949 c->fec_inner = FEC_AUTO;
950 c->rolloff = ROLLOFF_AUTO;
951 c->voltage = SEC_VOLTAGE_OFF;
952 c->sectone = SEC_TONE_OFF;
953 c->pilot = PILOT_AUTO;
955 c->isdbt_partial_reception = 0;
956 c->isdbt_sb_mode = 0;
957 c->isdbt_sb_subchannel = 0;
958 c->isdbt_sb_segment_idx = 0;
959 c->isdbt_sb_segment_count = 0;
960 c->isdbt_layer_enabled = 0;
961 for (i = 0; i < 3; i++) {
962 c->layer[i].fec = FEC_AUTO;
963 c->layer[i].modulation = QAM_AUTO;
964 c->layer[i].interleaving = 0;
965 c->layer[i].segment_count = 0;
968 c->stream_id = NO_STREAM_ID_FILTER;
970 switch (c->delivery_system) {
974 c->modulation = QPSK; /* implied for DVB-S in legacy API */
975 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
978 c->modulation = VSB_8;
981 c->symbol_rate = 28860000;
982 c->rolloff = ROLLOFF_35;
983 c->bandwidth_hz = c->symbol_rate / 100 * 135;
986 c->modulation = QAM_AUTO;
995 #define _DTV_CMD(n, s, b) \
1004 char *name; /* A display name for debugging purposes */
1006 __u32 cmd; /* A unique ID */
1009 __u32 set:1; /* Either a set or get property */
1010 __u32 buffer:1; /* Does this property use the buffer? */
1011 __u32 reserved:30; /* Align */
1014 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1015 _DTV_CMD(DTV_TUNE, 1, 0),
1016 _DTV_CMD(DTV_CLEAR, 1, 0),
1019 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1020 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1021 _DTV_CMD(DTV_MODULATION, 1, 0),
1022 _DTV_CMD(DTV_INVERSION, 1, 0),
1023 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1024 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1025 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1026 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1027 _DTV_CMD(DTV_TONE, 1, 0),
1028 _DTV_CMD(DTV_PILOT, 1, 0),
1029 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1030 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1031 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1032 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1033 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1034 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1035 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1036 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1038 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1039 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1040 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1041 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1042 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1043 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1044 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1045 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1046 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1047 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1048 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1049 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1050 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1051 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1052 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1053 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1054 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1055 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1057 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1058 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1059 _DTV_CMD(DTV_LNA, 1, 0),
1062 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1063 _DTV_CMD(DTV_API_VERSION, 0, 0),
1065 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1067 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1068 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1070 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1071 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1072 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1073 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1074 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1075 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1076 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1077 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1078 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1079 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1080 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1081 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1082 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1084 /* Statistics API */
1085 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1086 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1087 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1088 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1089 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1090 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1091 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1092 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1095 static void dtv_property_dump(struct dvb_frontend *fe,
1097 struct dtv_property *tvp)
1101 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1102 dev_warn(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x undefined\n",
1104 is_set ? "SET" : "GET",
1109 dev_dbg(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x (%s)\n", __func__,
1110 is_set ? "SET" : "GET",
1112 dtv_cmds[tvp->cmd].name);
1114 if (dtv_cmds[tvp->cmd].buffer) {
1115 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1116 __func__, tvp->u.buffer.len);
1118 for(i = 0; i < tvp->u.buffer.len; i++)
1119 dev_dbg(fe->dvb->device,
1120 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1121 __func__, i, tvp->u.buffer.data[i]);
1123 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1128 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1129 * drivers can use a single set_frontend tuning function, regardless of whether
1130 * it's being used for the legacy or new API, reducing code and complexity.
1132 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1133 struct dtv_frontend_properties *c,
1134 const struct dvb_frontend_parameters *p)
1136 c->frequency = p->frequency;
1137 c->inversion = p->inversion;
1139 switch (dvbv3_type(c->delivery_system)) {
1141 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1142 c->symbol_rate = p->u.qpsk.symbol_rate;
1143 c->fec_inner = p->u.qpsk.fec_inner;
1146 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1147 c->symbol_rate = p->u.qam.symbol_rate;
1148 c->fec_inner = p->u.qam.fec_inner;
1149 c->modulation = p->u.qam.modulation;
1152 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1154 switch (p->u.ofdm.bandwidth) {
1155 case BANDWIDTH_10_MHZ:
1156 c->bandwidth_hz = 10000000;
1158 case BANDWIDTH_8_MHZ:
1159 c->bandwidth_hz = 8000000;
1161 case BANDWIDTH_7_MHZ:
1162 c->bandwidth_hz = 7000000;
1164 case BANDWIDTH_6_MHZ:
1165 c->bandwidth_hz = 6000000;
1167 case BANDWIDTH_5_MHZ:
1168 c->bandwidth_hz = 5000000;
1170 case BANDWIDTH_1_712_MHZ:
1171 c->bandwidth_hz = 1712000;
1173 case BANDWIDTH_AUTO:
1174 c->bandwidth_hz = 0;
1177 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1178 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1179 c->modulation = p->u.ofdm.constellation;
1180 c->transmission_mode = p->u.ofdm.transmission_mode;
1181 c->guard_interval = p->u.ofdm.guard_interval;
1182 c->hierarchy = p->u.ofdm.hierarchy_information;
1185 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1186 c->modulation = p->u.vsb.modulation;
1187 if (c->delivery_system == SYS_ATSCMH)
1189 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1190 c->delivery_system = SYS_ATSC;
1192 c->delivery_system = SYS_DVBC_ANNEX_B;
1195 dev_err(fe->dvb->device,
1196 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1197 __func__, c->delivery_system);
1204 /* Ensure the cached values are set correctly in the frontend
1205 * legacy tuning structures, for the advanced tuning API.
1208 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1209 const struct dtv_frontend_properties *c,
1210 struct dvb_frontend_parameters *p)
1212 p->frequency = c->frequency;
1213 p->inversion = c->inversion;
1215 switch (dvbv3_type(c->delivery_system)) {
1217 dev_err(fe->dvb->device,
1218 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1219 __func__, c->delivery_system);
1222 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1223 p->u.qpsk.symbol_rate = c->symbol_rate;
1224 p->u.qpsk.fec_inner = c->fec_inner;
1227 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1228 p->u.qam.symbol_rate = c->symbol_rate;
1229 p->u.qam.fec_inner = c->fec_inner;
1230 p->u.qam.modulation = c->modulation;
1233 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1234 switch (c->bandwidth_hz) {
1236 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1239 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1242 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1245 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1248 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1251 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1255 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1257 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1258 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1259 p->u.ofdm.constellation = c->modulation;
1260 p->u.ofdm.transmission_mode = c->transmission_mode;
1261 p->u.ofdm.guard_interval = c->guard_interval;
1262 p->u.ofdm.hierarchy_information = c->hierarchy;
1265 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1266 p->u.vsb.modulation = c->modulation;
1273 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1274 * @fe: struct dvb_frontend pointer
1275 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1276 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1278 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1279 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1280 * If p_out is not null, it will update the DVBv3 params pointed by it.
1282 static int dtv_get_frontend(struct dvb_frontend *fe,
1283 struct dtv_frontend_properties *c,
1284 struct dvb_frontend_parameters *p_out)
1288 if (fe->ops.get_frontend) {
1289 r = fe->ops.get_frontend(fe, c);
1290 if (unlikely(r < 0))
1293 dtv_property_legacy_params_sync(fe, c, p_out);
1297 /* As everything is in cache, get_frontend fops are always supported */
1301 static int dvb_frontend_ioctl_legacy(struct file *file,
1302 unsigned int cmd, void *parg);
1303 static int dvb_frontend_ioctl_properties(struct file *file,
1304 unsigned int cmd, void *parg);
1306 static int dtv_property_process_get(struct dvb_frontend *fe,
1307 const struct dtv_frontend_properties *c,
1308 struct dtv_property *tvp,
1314 case DTV_ENUM_DELSYS:
1316 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1317 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1320 tvp->u.buffer.len = ncaps;
1323 tvp->u.data = c->frequency;
1325 case DTV_MODULATION:
1326 tvp->u.data = c->modulation;
1328 case DTV_BANDWIDTH_HZ:
1329 tvp->u.data = c->bandwidth_hz;
1332 tvp->u.data = c->inversion;
1334 case DTV_SYMBOL_RATE:
1335 tvp->u.data = c->symbol_rate;
1338 tvp->u.data = c->fec_inner;
1341 tvp->u.data = c->pilot;
1344 tvp->u.data = c->rolloff;
1346 case DTV_DELIVERY_SYSTEM:
1347 tvp->u.data = c->delivery_system;
1350 tvp->u.data = c->voltage;
1353 tvp->u.data = c->sectone;
1355 case DTV_API_VERSION:
1356 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1358 case DTV_CODE_RATE_HP:
1359 tvp->u.data = c->code_rate_HP;
1361 case DTV_CODE_RATE_LP:
1362 tvp->u.data = c->code_rate_LP;
1364 case DTV_GUARD_INTERVAL:
1365 tvp->u.data = c->guard_interval;
1367 case DTV_TRANSMISSION_MODE:
1368 tvp->u.data = c->transmission_mode;
1371 tvp->u.data = c->hierarchy;
1373 case DTV_INTERLEAVING:
1374 tvp->u.data = c->interleaving;
1377 /* ISDB-T Support here */
1378 case DTV_ISDBT_PARTIAL_RECEPTION:
1379 tvp->u.data = c->isdbt_partial_reception;
1381 case DTV_ISDBT_SOUND_BROADCASTING:
1382 tvp->u.data = c->isdbt_sb_mode;
1384 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1385 tvp->u.data = c->isdbt_sb_subchannel;
1387 case DTV_ISDBT_SB_SEGMENT_IDX:
1388 tvp->u.data = c->isdbt_sb_segment_idx;
1390 case DTV_ISDBT_SB_SEGMENT_COUNT:
1391 tvp->u.data = c->isdbt_sb_segment_count;
1393 case DTV_ISDBT_LAYER_ENABLED:
1394 tvp->u.data = c->isdbt_layer_enabled;
1396 case DTV_ISDBT_LAYERA_FEC:
1397 tvp->u.data = c->layer[0].fec;
1399 case DTV_ISDBT_LAYERA_MODULATION:
1400 tvp->u.data = c->layer[0].modulation;
1402 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1403 tvp->u.data = c->layer[0].segment_count;
1405 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1406 tvp->u.data = c->layer[0].interleaving;
1408 case DTV_ISDBT_LAYERB_FEC:
1409 tvp->u.data = c->layer[1].fec;
1411 case DTV_ISDBT_LAYERB_MODULATION:
1412 tvp->u.data = c->layer[1].modulation;
1414 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1415 tvp->u.data = c->layer[1].segment_count;
1417 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1418 tvp->u.data = c->layer[1].interleaving;
1420 case DTV_ISDBT_LAYERC_FEC:
1421 tvp->u.data = c->layer[2].fec;
1423 case DTV_ISDBT_LAYERC_MODULATION:
1424 tvp->u.data = c->layer[2].modulation;
1426 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1427 tvp->u.data = c->layer[2].segment_count;
1429 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1430 tvp->u.data = c->layer[2].interleaving;
1433 /* Multistream support */
1435 case DTV_DVBT2_PLP_ID_LEGACY:
1436 tvp->u.data = c->stream_id;
1440 case DTV_ATSCMH_FIC_VER:
1441 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1443 case DTV_ATSCMH_PARADE_ID:
1444 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1446 case DTV_ATSCMH_NOG:
1447 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1449 case DTV_ATSCMH_TNOG:
1450 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1452 case DTV_ATSCMH_SGN:
1453 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1455 case DTV_ATSCMH_PRC:
1456 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1458 case DTV_ATSCMH_RS_FRAME_MODE:
1459 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1461 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1462 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1464 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1465 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1467 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1468 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1470 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1471 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1473 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1474 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1476 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1477 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1479 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1480 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1482 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1483 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1487 tvp->u.data = c->lna;
1490 /* Fill quality measures */
1491 case DTV_STAT_SIGNAL_STRENGTH:
1492 tvp->u.st = c->strength;
1497 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1498 tvp->u.st = c->pre_bit_error;
1500 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1501 tvp->u.st = c->pre_bit_count;
1503 case DTV_STAT_POST_ERROR_BIT_COUNT:
1504 tvp->u.st = c->post_bit_error;
1506 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1507 tvp->u.st = c->post_bit_count;
1509 case DTV_STAT_ERROR_BLOCK_COUNT:
1510 tvp->u.st = c->block_error;
1512 case DTV_STAT_TOTAL_BLOCK_COUNT:
1513 tvp->u.st = c->block_count;
1516 dev_dbg(fe->dvb->device,
1517 "%s: FE property %d doesn't exist\n",
1518 __func__, tvp->cmd);
1522 /* Allow the frontend to override outgoing properties */
1523 if (fe->ops.get_property) {
1524 r = fe->ops.get_property(fe, tvp);
1529 dtv_property_dump(fe, false, tvp);
1534 static int dtv_set_frontend(struct dvb_frontend *fe);
1536 static bool is_dvbv3_delsys(u32 delsys)
1538 return (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1539 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1543 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1544 * @fe: struct frontend;
1545 * @delsys: DVBv5 type that will be used for emulation
1547 * Provides emulation for delivery systems that are compatible with the old
1548 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1549 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1550 * parameters are compatible with DVB-S spec.
1552 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1555 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1557 c->delivery_system = delsys;
1560 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1562 if (c->delivery_system == SYS_ISDBT) {
1563 dev_dbg(fe->dvb->device,
1564 "%s: Using defaults for SYS_ISDBT\n",
1567 if (!c->bandwidth_hz)
1568 c->bandwidth_hz = 6000000;
1570 c->isdbt_partial_reception = 0;
1571 c->isdbt_sb_mode = 0;
1572 c->isdbt_sb_subchannel = 0;
1573 c->isdbt_sb_segment_idx = 0;
1574 c->isdbt_sb_segment_count = 0;
1575 c->isdbt_layer_enabled = 7;
1576 for (i = 0; i < 3; i++) {
1577 c->layer[i].fec = FEC_AUTO;
1578 c->layer[i].modulation = QAM_AUTO;
1579 c->layer[i].interleaving = 0;
1580 c->layer[i].segment_count = 0;
1583 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1584 __func__, c->delivery_system);
1590 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1591 * @fe: frontend struct
1592 * @desired_system: delivery system requested by the user
1594 * A DVBv5 call know what's the desired system it wants. So, set it.
1596 * There are, however, a few known issues with early DVBv5 applications that
1597 * are also handled by this logic:
1599 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1600 * This is an API violation, but, as we don't want to break userspace,
1601 * convert it to the first supported delivery system.
1602 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1603 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1604 * ISDB-T provided backward compat with DVB-T.
1606 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1610 u32 delsys = SYS_UNDEFINED;
1611 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1612 enum dvbv3_emulation_type type;
1615 * It was reported that some old DVBv5 applications were
1616 * filling delivery_system with SYS_UNDEFINED. If this happens,
1617 * assume that the application wants to use the first supported
1620 if (desired_system == SYS_UNDEFINED)
1621 desired_system = fe->ops.delsys[0];
1624 * This is a DVBv5 call. So, it likely knows the supported
1625 * delivery systems. So, check if the desired delivery system is
1629 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1630 if (fe->ops.delsys[ncaps] == desired_system) {
1631 c->delivery_system = desired_system;
1632 dev_dbg(fe->dvb->device,
1633 "%s: Changing delivery system to %d\n",
1634 __func__, desired_system);
1641 * The requested delivery system isn't supported. Maybe userspace
1642 * is requesting a DVBv3 compatible delivery system.
1644 * The emulation only works if the desired system is one of the
1645 * delivery systems supported by DVBv3 API
1647 if (!is_dvbv3_delsys(desired_system)) {
1648 dev_dbg(fe->dvb->device,
1649 "%s: Delivery system %d not supported.\n",
1650 __func__, desired_system);
1654 type = dvbv3_type(desired_system);
1657 * Get the last non-DVBv3 delivery system that has the same type
1658 * of the desired system
1661 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1662 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1663 delsys = fe->ops.delsys[ncaps];
1667 /* There's nothing compatible with the desired delivery system */
1668 if (delsys == SYS_UNDEFINED) {
1669 dev_dbg(fe->dvb->device,
1670 "%s: Delivery system %d not supported on emulation mode.\n",
1671 __func__, desired_system);
1675 dev_dbg(fe->dvb->device,
1676 "%s: Using delivery system %d emulated as if it were %d\n",
1677 __func__, delsys, desired_system);
1679 return emulate_delivery_system(fe, desired_system);
1683 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1684 * @fe: frontend struct
1686 * A DVBv3 call doesn't know what's the desired system it wants. It also
1687 * doesn't allow to switch between different types. Due to that, userspace
1688 * should use DVBv5 instead.
1689 * However, in order to avoid breaking userspace API, limited backward
1690 * compatibility support is provided.
1692 * There are some delivery systems that are incompatible with DVBv3 calls.
1694 * This routine should work fine for frontends that support just one delivery
1697 * For frontends that support multiple frontends:
1698 * 1) It defaults to use the first supported delivery system. There's an
1699 * userspace application that allows changing it at runtime;
1701 * 2) If the current delivery system is not compatible with DVBv3, it gets
1702 * the first one that it is compatible.
1704 * NOTE: in order for this to work with applications like Kaffeine that
1705 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1706 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1707 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1710 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1713 u32 delsys = SYS_UNDEFINED;
1714 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1716 /* If not set yet, defaults to the first supported delivery system */
1717 if (c->delivery_system == SYS_UNDEFINED)
1718 c->delivery_system = fe->ops.delsys[0];
1721 * Trivial case: just use the current one, if it already a DVBv3
1724 if (is_dvbv3_delsys(c->delivery_system)) {
1725 dev_dbg(fe->dvb->device,
1726 "%s: Using delivery system to %d\n",
1727 __func__, c->delivery_system);
1732 * Seek for the first delivery system that it is compatible with a
1736 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1737 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1738 delsys = fe->ops.delsys[ncaps];
1743 if (delsys == SYS_UNDEFINED) {
1744 dev_dbg(fe->dvb->device,
1745 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1749 return emulate_delivery_system(fe, delsys);
1752 static int dtv_property_process_set(struct dvb_frontend *fe,
1753 struct dtv_property *tvp,
1757 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1759 /* Allow the frontend to validate incoming properties */
1760 if (fe->ops.set_property) {
1761 r = fe->ops.set_property(fe, tvp);
1766 dtv_property_dump(fe, true, tvp);
1771 * Reset a cache of data specific to the frontend here. This does
1772 * not effect hardware.
1774 dvb_frontend_clear_cache(fe);
1777 /* interpret the cache of data, build either a traditional frontend
1778 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1781 c->state = tvp->cmd;
1782 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1785 r = dtv_set_frontend(fe);
1788 c->frequency = tvp->u.data;
1790 case DTV_MODULATION:
1791 c->modulation = tvp->u.data;
1793 case DTV_BANDWIDTH_HZ:
1794 c->bandwidth_hz = tvp->u.data;
1797 c->inversion = tvp->u.data;
1799 case DTV_SYMBOL_RATE:
1800 c->symbol_rate = tvp->u.data;
1803 c->fec_inner = tvp->u.data;
1806 c->pilot = tvp->u.data;
1809 c->rolloff = tvp->u.data;
1811 case DTV_DELIVERY_SYSTEM:
1812 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1815 c->voltage = tvp->u.data;
1816 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1817 (void *)c->voltage);
1820 c->sectone = tvp->u.data;
1821 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1822 (void *)c->sectone);
1824 case DTV_CODE_RATE_HP:
1825 c->code_rate_HP = tvp->u.data;
1827 case DTV_CODE_RATE_LP:
1828 c->code_rate_LP = tvp->u.data;
1830 case DTV_GUARD_INTERVAL:
1831 c->guard_interval = tvp->u.data;
1833 case DTV_TRANSMISSION_MODE:
1834 c->transmission_mode = tvp->u.data;
1837 c->hierarchy = tvp->u.data;
1839 case DTV_INTERLEAVING:
1840 c->interleaving = tvp->u.data;
1843 /* ISDB-T Support here */
1844 case DTV_ISDBT_PARTIAL_RECEPTION:
1845 c->isdbt_partial_reception = tvp->u.data;
1847 case DTV_ISDBT_SOUND_BROADCASTING:
1848 c->isdbt_sb_mode = tvp->u.data;
1850 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1851 c->isdbt_sb_subchannel = tvp->u.data;
1853 case DTV_ISDBT_SB_SEGMENT_IDX:
1854 c->isdbt_sb_segment_idx = tvp->u.data;
1856 case DTV_ISDBT_SB_SEGMENT_COUNT:
1857 c->isdbt_sb_segment_count = tvp->u.data;
1859 case DTV_ISDBT_LAYER_ENABLED:
1860 c->isdbt_layer_enabled = tvp->u.data;
1862 case DTV_ISDBT_LAYERA_FEC:
1863 c->layer[0].fec = tvp->u.data;
1865 case DTV_ISDBT_LAYERA_MODULATION:
1866 c->layer[0].modulation = tvp->u.data;
1868 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1869 c->layer[0].segment_count = tvp->u.data;
1871 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1872 c->layer[0].interleaving = tvp->u.data;
1874 case DTV_ISDBT_LAYERB_FEC:
1875 c->layer[1].fec = tvp->u.data;
1877 case DTV_ISDBT_LAYERB_MODULATION:
1878 c->layer[1].modulation = tvp->u.data;
1880 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1881 c->layer[1].segment_count = tvp->u.data;
1883 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1884 c->layer[1].interleaving = tvp->u.data;
1886 case DTV_ISDBT_LAYERC_FEC:
1887 c->layer[2].fec = tvp->u.data;
1889 case DTV_ISDBT_LAYERC_MODULATION:
1890 c->layer[2].modulation = tvp->u.data;
1892 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1893 c->layer[2].segment_count = tvp->u.data;
1895 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1896 c->layer[2].interleaving = tvp->u.data;
1899 /* Multistream support */
1901 case DTV_DVBT2_PLP_ID_LEGACY:
1902 c->stream_id = tvp->u.data;
1906 case DTV_ATSCMH_PARADE_ID:
1907 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1909 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1910 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1914 c->lna = tvp->u.data;
1915 if (fe->ops.set_lna)
1916 r = fe->ops.set_lna(fe);
1928 static int dvb_frontend_ioctl(struct file *file,
1929 unsigned int cmd, void *parg)
1931 struct dvb_device *dvbdev = file->private_data;
1932 struct dvb_frontend *fe = dvbdev->priv;
1933 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1934 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1935 int err = -EOPNOTSUPP;
1937 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
1938 if (down_interruptible(&fepriv->sem))
1939 return -ERESTARTSYS;
1941 if (fe->exit != DVB_FE_NO_EXIT) {
1946 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1947 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1948 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1953 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1954 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1956 c->state = DTV_UNDEFINED;
1957 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1964 static int dvb_frontend_ioctl_properties(struct file *file,
1965 unsigned int cmd, void *parg)
1967 struct dvb_device *dvbdev = file->private_data;
1968 struct dvb_frontend *fe = dvbdev->priv;
1969 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1970 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1973 struct dtv_properties *tvps = parg;
1974 struct dtv_property *tvp = NULL;
1977 dev_dbg(fe->dvb->device, "%s:\n", __func__);
1979 if (cmd == FE_SET_PROPERTY) {
1980 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1981 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
1983 /* Put an arbitrary limit on the number of messages that can
1984 * be sent at once */
1985 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1988 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
1990 return PTR_ERR(tvp);
1992 for (i = 0; i < tvps->num; i++) {
1993 err = dtv_property_process_set(fe, tvp + i, file);
1996 (tvp + i)->result = err;
1999 if (c->state == DTV_TUNE)
2000 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
2002 } else if (cmd == FE_GET_PROPERTY) {
2003 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2005 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2006 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2008 /* Put an arbitrary limit on the number of messages that can
2009 * be sent at once */
2010 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2013 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
2015 return PTR_ERR(tvp);
2018 * Let's use our own copy of property cache, in order to
2019 * avoid mangling with DTV zigzag logic, as drivers might
2020 * return crap, if they don't check if the data is available
2021 * before updating the properties cache.
2023 if (fepriv->state != FESTATE_IDLE) {
2024 err = dtv_get_frontend(fe, &getp, NULL);
2028 for (i = 0; i < tvps->num; i++) {
2029 err = dtv_property_process_get(fe, &getp, tvp + i, file);
2032 (tvp + i)->result = err;
2035 if (copy_to_user((void __user *)tvps->props, tvp,
2036 tvps->num * sizeof(struct dtv_property))) {
2049 static int dtv_set_frontend(struct dvb_frontend *fe)
2051 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2052 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2053 struct dvb_frontend_tune_settings fetunesettings;
2056 if (dvb_frontend_check_parameters(fe) < 0)
2060 * Initialize output parameters to match the values given by
2061 * the user. FE_SET_FRONTEND triggers an initial frontend event
2062 * with status = 0, which copies output parameters to userspace.
2064 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
2067 * Be sure that the bandwidth will be filled for all
2068 * non-satellite systems, as tuners need to know what
2069 * low pass/Nyquist half filter should be applied, in
2070 * order to avoid inter-channel noise.
2072 * ISDB-T and DVB-T/T2 already sets bandwidth.
2073 * ATSC and DVB-C don't set, so, the core should fill it.
2075 * On DVB-C Annex A and C, the bandwidth is a function of
2076 * the roll-off and symbol rate. Annex B defines different
2077 * roll-off factors depending on the modulation. Fortunately,
2078 * Annex B is only used with 6MHz, so there's no need to
2081 * While not officially supported, a side effect of handling it at
2082 * the cache level is that a program could retrieve the bandwidth
2083 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2085 switch (c->delivery_system) {
2087 case SYS_DVBC_ANNEX_B:
2088 c->bandwidth_hz = 6000000;
2090 case SYS_DVBC_ANNEX_A:
2093 case SYS_DVBC_ANNEX_C:
2102 switch (c->rolloff) {
2118 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
2120 /* force auto frequency inversion if requested */
2121 if (dvb_force_auto_inversion)
2122 c->inversion = INVERSION_AUTO;
2125 * without hierarchical coding code_rate_LP is irrelevant,
2126 * so we tolerate the otherwise invalid FEC_NONE setting
2128 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2129 c->code_rate_LP = FEC_AUTO;
2131 /* get frontend-specific tuning settings */
2132 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2133 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2134 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2135 fepriv->max_drift = fetunesettings.max_drift;
2136 fepriv->step_size = fetunesettings.step_size;
2138 /* default values */
2139 switch (c->delivery_system) {
2144 case SYS_DVBC_ANNEX_A:
2145 case SYS_DVBC_ANNEX_C:
2146 fepriv->min_delay = HZ / 20;
2147 fepriv->step_size = c->symbol_rate / 16000;
2148 fepriv->max_drift = c->symbol_rate / 2000;
2154 fepriv->min_delay = HZ / 20;
2155 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2156 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2160 * FIXME: This sounds wrong! if freqency_stepsize is
2161 * defined by the frontend, why not use it???
2163 fepriv->min_delay = HZ / 20;
2164 fepriv->step_size = 0; /* no zigzag */
2165 fepriv->max_drift = 0;
2169 if (dvb_override_tune_delay > 0)
2170 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2172 fepriv->state = FESTATE_RETUNE;
2174 /* Request the search algorithm to search */
2175 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2177 dvb_frontend_clear_events(fe);
2178 dvb_frontend_add_event(fe, 0);
2179 dvb_frontend_wakeup(fe);
2186 static int dvb_frontend_ioctl_legacy(struct file *file,
2187 unsigned int cmd, void *parg)
2189 struct dvb_device *dvbdev = file->private_data;
2190 struct dvb_frontend *fe = dvbdev->priv;
2191 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2192 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2193 int err = -EOPNOTSUPP;
2197 struct dvb_frontend_info* info = parg;
2199 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2200 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2203 * Associate the 4 delivery systems supported by DVBv3
2204 * API with their DVBv5 counterpart. For the other standards,
2205 * use the closest type, assuming that it would hopefully
2206 * work with a DVBv3 application.
2207 * It should be noticed that, on multi-frontend devices with
2208 * different types (terrestrial and cable, for example),
2209 * a pure DVBv3 application won't be able to use all delivery
2210 * systems. Yet, changing the DVBv5 cache to the other delivery
2211 * system should be enough for making it work.
2213 switch (dvbv3_type(c->delivery_system)) {
2215 info->type = FE_QPSK;
2218 info->type = FE_ATSC;
2221 info->type = FE_QAM;
2224 info->type = FE_OFDM;
2227 dev_err(fe->dvb->device,
2228 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2229 __func__, c->delivery_system);
2230 fe->ops.info.type = FE_OFDM;
2232 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2233 __func__, c->delivery_system, fe->ops.info.type);
2235 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2236 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2237 info->caps |= FE_CAN_INVERSION_AUTO;
2242 case FE_READ_STATUS: {
2243 enum fe_status *status = parg;
2245 /* if retune was requested but hasn't occurred yet, prevent
2246 * that user get signal state from previous tuning */
2247 if (fepriv->state == FESTATE_RETUNE ||
2248 fepriv->state == FESTATE_ERROR) {
2254 if (fe->ops.read_status)
2255 err = fe->ops.read_status(fe, status);
2260 if (fe->ops.read_ber) {
2262 err = fe->ops.read_ber(fe, (__u32 *) parg);
2268 case FE_READ_SIGNAL_STRENGTH:
2269 if (fe->ops.read_signal_strength) {
2271 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2278 if (fe->ops.read_snr) {
2280 err = fe->ops.read_snr(fe, (__u16 *) parg);
2286 case FE_READ_UNCORRECTED_BLOCKS:
2287 if (fe->ops.read_ucblocks) {
2289 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2295 case FE_DISEQC_RESET_OVERLOAD:
2296 if (fe->ops.diseqc_reset_overload) {
2297 err = fe->ops.diseqc_reset_overload(fe);
2298 fepriv->state = FESTATE_DISEQC;
2303 case FE_DISEQC_SEND_MASTER_CMD:
2304 if (fe->ops.diseqc_send_master_cmd) {
2305 struct dvb_diseqc_master_cmd *cmd = parg;
2307 if (cmd->msg_len > sizeof(cmd->msg)) {
2311 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
2312 fepriv->state = FESTATE_DISEQC;
2317 case FE_DISEQC_SEND_BURST:
2318 if (fe->ops.diseqc_send_burst) {
2319 err = fe->ops.diseqc_send_burst(fe,
2320 (enum fe_sec_mini_cmd)parg);
2321 fepriv->state = FESTATE_DISEQC;
2327 if (fe->ops.set_tone) {
2328 err = fe->ops.set_tone(fe,
2329 (enum fe_sec_tone_mode)parg);
2330 fepriv->tone = (enum fe_sec_tone_mode)parg;
2331 fepriv->state = FESTATE_DISEQC;
2336 case FE_SET_VOLTAGE:
2337 if (fe->ops.set_voltage) {
2338 err = fe->ops.set_voltage(fe,
2339 (enum fe_sec_voltage)parg);
2340 fepriv->voltage = (enum fe_sec_voltage)parg;
2341 fepriv->state = FESTATE_DISEQC;
2346 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2347 if (fe->ops.dishnetwork_send_legacy_command) {
2348 err = fe->ops.dishnetwork_send_legacy_command(fe,
2349 (unsigned long)parg);
2350 fepriv->state = FESTATE_DISEQC;
2352 } else if (fe->ops.set_voltage) {
2354 * NOTE: This is a fallback condition. Some frontends
2355 * (stv0299 for instance) take longer than 8msec to
2356 * respond to a set_voltage command. Those switches
2357 * need custom routines to switch properly. For all
2358 * other frontends, the following should work ok.
2359 * Dish network legacy switches (as used by Dish500)
2360 * are controlled by sending 9-bit command words
2361 * spaced 8msec apart.
2362 * the actual command word is switch/port dependent
2363 * so it is up to the userspace application to send
2364 * the right command.
2365 * The command must always start with a '0' after
2366 * initialization, so parg is 8 bits and does not
2367 * include the initialization or start bit
2369 unsigned long swcmd = ((unsigned long) parg) << 1;
2374 if (dvb_frontend_debug)
2375 dprintk("%s switch command: 0x%04lx\n",
2377 nexttime = ktime_get_boottime();
2378 if (dvb_frontend_debug)
2380 /* before sending a command, initialize by sending
2381 * a 32ms 18V to the switch
2383 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2384 dvb_frontend_sleep_until(&nexttime, 32000);
2386 for (i = 0; i < 9; i++) {
2387 if (dvb_frontend_debug)
2388 tv[i+1] = ktime_get_boottime();
2389 if ((swcmd & 0x01) != last) {
2390 /* set voltage to (last ? 13V : 18V) */
2391 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2392 last = (last) ? 0 : 1;
2396 dvb_frontend_sleep_until(&nexttime, 8000);
2398 if (dvb_frontend_debug) {
2399 dprintk("%s(%d): switch delay (should be 32k followed by all 8k)\n",
2400 __func__, fe->dvb->num);
2401 for (i = 1; i < 10; i++)
2402 pr_info("%d: %d\n", i,
2403 (int) ktime_us_delta(tv[i], tv[i-1]));
2406 fepriv->state = FESTATE_DISEQC;
2411 case FE_DISEQC_RECV_SLAVE_REPLY:
2412 if (fe->ops.diseqc_recv_slave_reply)
2413 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2416 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2417 if (fe->ops.enable_high_lnb_voltage)
2418 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2421 case FE_SET_FRONTEND:
2422 err = dvbv3_set_delivery_system(fe);
2426 err = dtv_property_cache_sync(fe, c, parg);
2429 err = dtv_set_frontend(fe);
2432 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2435 case FE_GET_FRONTEND: {
2436 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2439 * Let's use our own copy of property cache, in order to
2440 * avoid mangling with DTV zigzag logic, as drivers might
2441 * return crap, if they don't check if the data is available
2442 * before updating the properties cache.
2444 err = dtv_get_frontend(fe, &getp, parg);
2447 case FE_SET_FRONTEND_TUNE_MODE:
2448 fepriv->tune_mode_flags = (unsigned long) parg;
2457 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2459 struct dvb_device *dvbdev = file->private_data;
2460 struct dvb_frontend *fe = dvbdev->priv;
2461 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2463 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2465 poll_wait (file, &fepriv->events.wait_queue, wait);
2467 if (fepriv->events.eventw != fepriv->events.eventr)
2468 return (POLLIN | POLLRDNORM | POLLPRI);
2473 static int dvb_frontend_open(struct inode *inode, struct file *file)
2475 struct dvb_device *dvbdev = file->private_data;
2476 struct dvb_frontend *fe = dvbdev->priv;
2477 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2478 struct dvb_adapter *adapter = fe->dvb;
2481 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2482 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2485 if (adapter->mfe_shared) {
2486 mutex_lock (&adapter->mfe_lock);
2488 if (adapter->mfe_dvbdev == NULL)
2489 adapter->mfe_dvbdev = dvbdev;
2491 else if (adapter->mfe_dvbdev != dvbdev) {
2493 *mfedev = adapter->mfe_dvbdev;
2495 *mfe = mfedev->priv;
2496 struct dvb_frontend_private
2497 *mfepriv = mfe->frontend_priv;
2498 int mferetry = (dvb_mfe_wait_time << 1);
2500 mutex_unlock (&adapter->mfe_lock);
2501 while (mferetry-- && (mfedev->users != -1 ||
2502 mfepriv->thread != NULL)) {
2503 if(msleep_interruptible(500)) {
2504 if(signal_pending(current))
2509 mutex_lock (&adapter->mfe_lock);
2510 if(adapter->mfe_dvbdev != dvbdev) {
2511 mfedev = adapter->mfe_dvbdev;
2513 mfepriv = mfe->frontend_priv;
2514 if (mfedev->users != -1 ||
2515 mfepriv->thread != NULL) {
2516 mutex_unlock (&adapter->mfe_lock);
2519 adapter->mfe_dvbdev = dvbdev;
2524 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2525 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2528 /* If we took control of the bus, we need to force
2529 reinitialization. This is because many ts_bus_ctrl()
2530 functions strobe the RESET pin on the demod, and if the
2531 frontend thread already exists then the dvb_init() routine
2532 won't get called (which is what usually does initial
2533 register configuration). */
2534 fepriv->reinitialise = 1;
2537 if ((ret = dvb_generic_open (inode, file)) < 0)
2540 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2541 /* normal tune mode when opened R/W */
2542 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2544 fepriv->voltage = -1;
2546 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2547 if (fe->dvb->mdev) {
2548 mutex_lock(&fe->dvb->mdev->graph_mutex);
2549 if (fe->dvb->mdev->enable_source)
2550 ret = fe->dvb->mdev->enable_source(
2553 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2555 dev_err(fe->dvb->device,
2556 "Tuner is busy. Error %d\n", ret);
2561 ret = dvb_frontend_start (fe);
2565 /* empty event queue */
2566 fepriv->events.eventr = fepriv->events.eventw = 0;
2569 dvb_frontend_get(fe);
2571 if (adapter->mfe_shared)
2572 mutex_unlock (&adapter->mfe_lock);
2576 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2577 if (fe->dvb->mdev) {
2578 mutex_lock(&fe->dvb->mdev->graph_mutex);
2579 if (fe->dvb->mdev->disable_source)
2580 fe->dvb->mdev->disable_source(dvbdev->entity);
2581 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2585 dvb_generic_release(inode, file);
2587 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2588 fe->ops.ts_bus_ctrl(fe, 0);
2590 if (adapter->mfe_shared)
2591 mutex_unlock (&adapter->mfe_lock);
2595 static int dvb_frontend_release(struct inode *inode, struct file *file)
2597 struct dvb_device *dvbdev = file->private_data;
2598 struct dvb_frontend *fe = dvbdev->priv;
2599 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2602 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2604 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2605 fepriv->release_jiffies = jiffies;
2609 ret = dvb_generic_release (inode, file);
2611 if (dvbdev->users == -1) {
2612 wake_up(&fepriv->wait_queue);
2613 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2614 if (fe->dvb->mdev) {
2615 mutex_lock(&fe->dvb->mdev->graph_mutex);
2616 if (fe->dvb->mdev->disable_source)
2617 fe->dvb->mdev->disable_source(dvbdev->entity);
2618 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2621 if (fe->exit != DVB_FE_NO_EXIT)
2622 wake_up(&dvbdev->wait_queue);
2623 if (fe->ops.ts_bus_ctrl)
2624 fe->ops.ts_bus_ctrl(fe, 0);
2627 dvb_frontend_put(fe);
2632 static const struct file_operations dvb_frontend_fops = {
2633 .owner = THIS_MODULE,
2634 .unlocked_ioctl = dvb_generic_ioctl,
2635 .poll = dvb_frontend_poll,
2636 .open = dvb_frontend_open,
2637 .release = dvb_frontend_release,
2638 .llseek = noop_llseek,
2641 int dvb_frontend_suspend(struct dvb_frontend *fe)
2645 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2648 if (fe->ops.tuner_ops.suspend)
2649 ret = fe->ops.tuner_ops.suspend(fe);
2650 else if (fe->ops.tuner_ops.sleep)
2651 ret = fe->ops.tuner_ops.sleep(fe);
2654 ret = fe->ops.sleep(fe);
2658 EXPORT_SYMBOL(dvb_frontend_suspend);
2660 int dvb_frontend_resume(struct dvb_frontend *fe)
2662 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2665 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2668 fe->exit = DVB_FE_DEVICE_RESUME;
2670 ret = fe->ops.init(fe);
2672 if (fe->ops.tuner_ops.resume)
2673 ret = fe->ops.tuner_ops.resume(fe);
2674 else if (fe->ops.tuner_ops.init)
2675 ret = fe->ops.tuner_ops.init(fe);
2677 if (fe->ops.set_tone && fepriv->tone != -1)
2678 fe->ops.set_tone(fe, fepriv->tone);
2679 if (fe->ops.set_voltage && fepriv->voltage != -1)
2680 fe->ops.set_voltage(fe, fepriv->voltage);
2682 fe->exit = DVB_FE_NO_EXIT;
2683 fepriv->state = FESTATE_RETUNE;
2684 dvb_frontend_wakeup(fe);
2688 EXPORT_SYMBOL(dvb_frontend_resume);
2690 int dvb_register_frontend(struct dvb_adapter* dvb,
2691 struct dvb_frontend* fe)
2693 struct dvb_frontend_private *fepriv;
2694 const struct dvb_device dvbdev_template = {
2698 .fops = &dvb_frontend_fops,
2699 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2700 .name = fe->ops.info.name,
2702 .kernel_ioctl = dvb_frontend_ioctl
2705 dev_dbg(dvb->device, "%s:\n", __func__);
2707 if (mutex_lock_interruptible(&frontend_mutex))
2708 return -ERESTARTSYS;
2710 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2711 if (fe->frontend_priv == NULL) {
2712 mutex_unlock(&frontend_mutex);
2715 fepriv = fe->frontend_priv;
2717 kref_init(&fe->refcount);
2720 * After initialization, there need to be two references: one
2721 * for dvb_unregister_frontend(), and another one for
2722 * dvb_frontend_detach().
2724 dvb_frontend_get(fe);
2726 sema_init(&fepriv->sem, 1);
2727 init_waitqueue_head (&fepriv->wait_queue);
2728 init_waitqueue_head (&fepriv->events.wait_queue);
2729 mutex_init(&fepriv->events.mtx);
2731 fepriv->inversion = INVERSION_OFF;
2733 dev_info(fe->dvb->device,
2734 "DVB: registering adapter %i frontend %i (%s)...\n",
2735 fe->dvb->num, fe->id, fe->ops.info.name);
2737 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2738 fe, DVB_DEVICE_FRONTEND, 0);
2741 * Initialize the cache to the proper values according with the
2742 * first supported delivery system (ops->delsys[0])
2745 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2746 dvb_frontend_clear_cache(fe);
2748 mutex_unlock(&frontend_mutex);
2751 EXPORT_SYMBOL(dvb_register_frontend);
2753 int dvb_unregister_frontend(struct dvb_frontend* fe)
2755 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2756 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2758 mutex_lock(&frontend_mutex);
2759 dvb_frontend_stop(fe);
2760 dvb_remove_device(fepriv->dvbdev);
2762 /* fe is invalid now */
2763 mutex_unlock(&frontend_mutex);
2764 dvb_frontend_put(fe);
2767 EXPORT_SYMBOL(dvb_unregister_frontend);
2769 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
2770 void (*release)(struct dvb_frontend *fe))
2774 #ifdef CONFIG_MEDIA_ATTACH
2775 dvb_detach(release);
2780 void dvb_frontend_detach(struct dvb_frontend* fe)
2782 dvb_frontend_invoke_release(fe, fe->ops.release_sec);
2783 dvb_frontend_invoke_release(fe, fe->ops.tuner_ops.release);
2784 dvb_frontend_invoke_release(fe, fe->ops.analog_ops.release);
2785 dvb_frontend_invoke_release(fe, fe->ops.detach);
2786 dvb_frontend_put(fe);
2788 EXPORT_SYMBOL(dvb_frontend_detach);