2 * dvb_frontend.c: DVB frontend tuning interface/thread
5 * Copyright (C) 1999-2001 Ralph Metzler
8 * for convergence integrated media GmbH
10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
28 /* Enables DVBv3 compatibility bits at the headers */
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <asm/processor.h>
45 #include "dvb_frontend.h"
47 #include <linux/dvb/version.h>
49 static int dvb_frontend_debug;
50 static int dvb_shutdown_timeout;
51 static int dvb_force_auto_inversion;
52 static int dvb_override_tune_delay;
53 static int dvb_powerdown_on_sleep = 1;
54 static int dvb_mfe_wait_time = 5;
56 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
57 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
58 module_param(dvb_shutdown_timeout, int, 0644);
59 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
60 module_param(dvb_force_auto_inversion, int, 0644);
61 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
62 module_param(dvb_override_tune_delay, int, 0644);
63 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
64 module_param(dvb_powerdown_on_sleep, int, 0644);
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
66 module_param(dvb_mfe_wait_time, int, 0644);
67 MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
69 #define dprintk if (dvb_frontend_debug) printk
71 #define FESTATE_IDLE 1
72 #define FESTATE_RETUNE 2
73 #define FESTATE_TUNING_FAST 4
74 #define FESTATE_TUNING_SLOW 8
75 #define FESTATE_TUNED 16
76 #define FESTATE_ZIGZAG_FAST 32
77 #define FESTATE_ZIGZAG_SLOW 64
78 #define FESTATE_DISEQC 128
79 #define FESTATE_ERROR 256
80 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
81 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
82 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
83 #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 #define DVB_FE_NO_EXIT 0
102 #define DVB_FE_NORMAL_EXIT 1
103 #define DVB_FE_DEVICE_REMOVED 2
105 static DEFINE_MUTEX(frontend_mutex);
107 struct dvb_frontend_private {
109 /* thread/frontend values */
110 struct dvb_device *dvbdev;
111 struct dvb_frontend_parameters parameters_out;
112 struct dvb_fe_events events;
113 struct semaphore sem;
114 struct list_head list_head;
115 wait_queue_head_t wait_queue;
116 struct task_struct *thread;
117 unsigned long release_jiffies;
121 unsigned long tune_mode_flags;
123 unsigned int reinitialise;
127 /* swzigzag values */
129 unsigned int bending;
131 unsigned int inversion;
132 unsigned int auto_step;
133 unsigned int auto_sub_step;
134 unsigned int started_auto_step;
135 unsigned int min_delay;
136 unsigned int max_drift;
137 unsigned int step_size;
139 unsigned int check_wrapped;
140 enum dvbfe_search algo_status;
143 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
144 static int dtv_get_frontend(struct dvb_frontend *fe,
145 struct dvb_frontend_parameters *p_out);
146 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
147 struct dvb_frontend_parameters *p);
149 static bool has_get_frontend(struct dvb_frontend *fe)
151 return fe->ops.get_frontend != NULL;
155 * Due to DVBv3 API calls, a delivery system should be mapped into one of
156 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
157 * otherwise, a DVBv3 call will fail.
159 enum dvbv3_emulation_type {
167 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
169 switch (delivery_system) {
170 case SYS_DVBC_ANNEX_A:
171 case SYS_DVBC_ANNEX_C:
186 case SYS_DVBC_ANNEX_B:
194 * Doesn't know how to emulate those types and/or
195 * there's no frontend driver from this type yet
196 * with some emulation code, so, we're not sure yet how
197 * to handle them, or they're not compatible with a DVBv3 call.
199 return DVBV3_UNKNOWN;
203 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
205 struct dvb_frontend_private *fepriv = fe->frontend_priv;
206 struct dvb_fe_events *events = &fepriv->events;
207 struct dvb_frontend_event *e;
210 dprintk ("%s\n", __func__);
212 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
213 dtv_get_frontend(fe, &fepriv->parameters_out);
215 mutex_lock(&events->mtx);
217 wp = (events->eventw + 1) % MAX_EVENT;
218 if (wp == events->eventr) {
219 events->overflow = 1;
220 events->eventr = (events->eventr + 1) % MAX_EVENT;
223 e = &events->events[events->eventw];
225 e->parameters = fepriv->parameters_out;
229 mutex_unlock(&events->mtx);
231 wake_up_interruptible (&events->wait_queue);
234 static int dvb_frontend_get_event(struct dvb_frontend *fe,
235 struct dvb_frontend_event *event, int flags)
237 struct dvb_frontend_private *fepriv = fe->frontend_priv;
238 struct dvb_fe_events *events = &fepriv->events;
240 dprintk ("%s\n", __func__);
242 if (events->overflow) {
243 events->overflow = 0;
247 if (events->eventw == events->eventr) {
250 if (flags & O_NONBLOCK)
255 ret = wait_event_interruptible (events->wait_queue,
256 events->eventw != events->eventr);
258 if (down_interruptible (&fepriv->sem))
265 mutex_lock(&events->mtx);
266 *event = events->events[events->eventr];
267 events->eventr = (events->eventr + 1) % MAX_EVENT;
268 mutex_unlock(&events->mtx);
273 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
275 struct dvb_frontend_private *fepriv = fe->frontend_priv;
276 struct dvb_fe_events *events = &fepriv->events;
278 mutex_lock(&events->mtx);
279 events->eventr = events->eventw;
280 mutex_unlock(&events->mtx);
283 static void dvb_frontend_init(struct dvb_frontend *fe)
285 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
292 if (fe->ops.tuner_ops.init) {
293 if (fe->ops.i2c_gate_ctrl)
294 fe->ops.i2c_gate_ctrl(fe, 1);
295 fe->ops.tuner_ops.init(fe);
296 if (fe->ops.i2c_gate_ctrl)
297 fe->ops.i2c_gate_ctrl(fe, 0);
301 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
303 struct dvb_frontend_private *fepriv = fe->frontend_priv;
305 fepriv->reinitialise = 1;
306 dvb_frontend_wakeup(fe);
308 EXPORT_SYMBOL(dvb_frontend_reinitialise);
310 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
314 dprintk ("%s\n", __func__);
317 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
319 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
321 q2 = fepriv->quality - 128;
324 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
328 * Performs automatic twiddling of frontend parameters.
330 * @param fe The frontend concerned.
331 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
332 * @returns Number of complete iterations that have been performed.
334 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
339 struct dvb_frontend_private *fepriv = fe->frontend_priv;
340 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
341 int original_inversion = c->inversion;
342 u32 original_frequency = c->frequency;
344 /* are we using autoinversion? */
345 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
346 (c->inversion == INVERSION_AUTO));
348 /* setup parameters correctly */
350 /* calculate the lnb_drift */
351 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
353 /* wrap the auto_step if we've exceeded the maximum drift */
354 if (fepriv->lnb_drift > fepriv->max_drift) {
355 fepriv->auto_step = 0;
356 fepriv->auto_sub_step = 0;
357 fepriv->lnb_drift = 0;
360 /* perform inversion and +/- zigzag */
361 switch(fepriv->auto_sub_step) {
363 /* try with the current inversion and current drift setting */
368 if (!autoinversion) break;
370 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
375 if (fepriv->lnb_drift == 0) break;
377 fepriv->lnb_drift = -fepriv->lnb_drift;
382 if (fepriv->lnb_drift == 0) break;
383 if (!autoinversion) break;
385 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
386 fepriv->lnb_drift = -fepriv->lnb_drift;
392 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
396 if (!ready) fepriv->auto_sub_step++;
399 /* if this attempt would hit where we started, indicate a complete
400 * iteration has occurred */
401 if ((fepriv->auto_step == fepriv->started_auto_step) &&
402 (fepriv->auto_sub_step == 0) && check_wrapped) {
406 dprintk("%s: drift:%i inversion:%i auto_step:%i "
407 "auto_sub_step:%i started_auto_step:%i\n",
408 __func__, fepriv->lnb_drift, fepriv->inversion,
409 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
411 /* set the frontend itself */
412 c->frequency += fepriv->lnb_drift;
414 c->inversion = fepriv->inversion;
416 if (fe->ops.set_frontend)
417 fe_set_err = fe->ops.set_frontend(fe);
419 if (fe_set_err < 0) {
420 fepriv->state = FESTATE_ERROR;
424 c->frequency = original_frequency;
425 c->inversion = original_inversion;
427 fepriv->auto_sub_step++;
431 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
435 struct dvb_frontend_private *fepriv = fe->frontend_priv;
436 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
438 /* if we've got no parameters, just keep idling */
439 if (fepriv->state & FESTATE_IDLE) {
440 fepriv->delay = 3*HZ;
445 /* in SCAN mode, we just set the frontend when asked and leave it alone */
446 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
447 if (fepriv->state & FESTATE_RETUNE) {
449 if (fe->ops.set_frontend)
450 retval = fe->ops.set_frontend(fe);
453 fepriv->state = FESTATE_ERROR;
455 fepriv->state = FESTATE_TUNED;
457 fepriv->delay = 3*HZ;
462 /* get the frontend status */
463 if (fepriv->state & FESTATE_RETUNE) {
466 if (fe->ops.read_status)
467 fe->ops.read_status(fe, &s);
468 if (s != fepriv->status) {
469 dvb_frontend_add_event(fe, s);
474 /* if we're not tuned, and we have a lock, move to the TUNED state */
475 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
476 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
477 fepriv->state = FESTATE_TUNED;
479 /* if we're tuned, then we have determined the correct inversion */
480 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
481 (c->inversion == INVERSION_AUTO)) {
482 c->inversion = fepriv->inversion;
487 /* if we are tuned already, check we're still locked */
488 if (fepriv->state & FESTATE_TUNED) {
489 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
491 /* we're tuned, and the lock is still good... */
492 if (s & FE_HAS_LOCK) {
494 } else { /* if we _WERE_ tuned, but now don't have a lock */
495 fepriv->state = FESTATE_ZIGZAG_FAST;
496 fepriv->started_auto_step = fepriv->auto_step;
497 fepriv->check_wrapped = 0;
501 /* don't actually do anything if we're in the LOSTLOCK state,
502 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
503 if ((fepriv->state & FESTATE_LOSTLOCK) &&
504 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
505 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
509 /* don't do anything if we're in the DISEQC state, since this
510 * might be someone with a motorized dish controlled by DISEQC.
511 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
512 if (fepriv->state & FESTATE_DISEQC) {
513 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
517 /* if we're in the RETUNE state, set everything up for a brand
518 * new scan, keeping the current inversion setting, as the next
519 * tune is _very_ likely to require the same */
520 if (fepriv->state & FESTATE_RETUNE) {
521 fepriv->lnb_drift = 0;
522 fepriv->auto_step = 0;
523 fepriv->auto_sub_step = 0;
524 fepriv->started_auto_step = 0;
525 fepriv->check_wrapped = 0;
529 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
530 fepriv->delay = fepriv->min_delay;
533 retval = dvb_frontend_swzigzag_autotune(fe,
534 fepriv->check_wrapped);
538 /* OK, if we've run out of trials at the fast speed.
539 * Drop back to slow for the _next_ attempt */
540 fepriv->state = FESTATE_SEARCHING_SLOW;
541 fepriv->started_auto_step = fepriv->auto_step;
544 fepriv->check_wrapped = 1;
546 /* if we've just retuned, enter the ZIGZAG_FAST state.
547 * This ensures we cannot return from an
548 * FE_SET_FRONTEND ioctl before the first frontend tune
550 if (fepriv->state & FESTATE_RETUNE) {
551 fepriv->state = FESTATE_TUNING_FAST;
556 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
557 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
559 /* Note: don't bother checking for wrapping; we stay in this
560 * state until we get a lock */
561 dvb_frontend_swzigzag_autotune(fe, 0);
565 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
567 struct dvb_frontend_private *fepriv = fe->frontend_priv;
569 if (fepriv->exit != DVB_FE_NO_EXIT)
572 if (fepriv->dvbdev->writers == 1)
573 if (time_after_eq(jiffies, fepriv->release_jiffies +
574 dvb_shutdown_timeout * HZ))
580 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
582 struct dvb_frontend_private *fepriv = fe->frontend_priv;
584 if (fepriv->wakeup) {
588 return dvb_frontend_is_exiting(fe);
591 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
593 struct dvb_frontend_private *fepriv = fe->frontend_priv;
596 wake_up_interruptible(&fepriv->wait_queue);
599 static int dvb_frontend_thread(void *data)
601 struct dvb_frontend *fe = data;
602 struct dvb_frontend_private *fepriv = fe->frontend_priv;
604 enum dvbfe_algo algo;
606 bool re_tune = false;
608 dprintk("%s\n", __func__);
610 fepriv->check_wrapped = 0;
612 fepriv->delay = 3*HZ;
615 fepriv->reinitialise = 0;
617 dvb_frontend_init(fe);
621 up(&fepriv->sem); /* is locked when we enter the thread... */
623 wait_event_interruptible_timeout(fepriv->wait_queue,
624 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
625 || freezing(current),
628 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
629 /* got signal or quitting */
630 fepriv->exit = DVB_FE_NORMAL_EXIT;
637 if (down_interruptible(&fepriv->sem))
640 if (fepriv->reinitialise) {
641 dvb_frontend_init(fe);
642 if (fe->ops.set_tone && fepriv->tone != -1)
643 fe->ops.set_tone(fe, fepriv->tone);
644 if (fe->ops.set_voltage && fepriv->voltage != -1)
645 fe->ops.set_voltage(fe, fepriv->voltage);
646 fepriv->reinitialise = 0;
649 /* do an iteration of the tuning loop */
650 if (fe->ops.get_frontend_algo) {
651 algo = fe->ops.get_frontend_algo(fe);
654 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
656 if (fepriv->state & FESTATE_RETUNE) {
657 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
659 fepriv->state = FESTATE_TUNED;
665 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
667 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
668 dprintk("%s: state changed, adding current state\n", __func__);
669 dvb_frontend_add_event(fe, s);
674 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
675 dvb_frontend_swzigzag(fe);
677 case DVBFE_ALGO_CUSTOM:
678 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
679 if (fepriv->state & FESTATE_RETUNE) {
680 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
681 fepriv->state = FESTATE_TUNED;
683 /* Case where we are going to search for a carrier
684 * User asked us to retune again for some reason, possibly
685 * requesting a search with a new set of parameters
687 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
688 if (fe->ops.search) {
689 fepriv->algo_status = fe->ops.search(fe);
690 /* We did do a search as was requested, the flags are
691 * now unset as well and has the flags wrt to search.
694 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
697 /* Track the carrier if the search was successful */
698 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
699 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
700 fepriv->delay = HZ / 2;
702 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
703 fe->ops.read_status(fe, &s);
704 if (s != fepriv->status) {
705 dvb_frontend_add_event(fe, s); /* update event list */
707 if (!(s & FE_HAS_LOCK)) {
708 fepriv->delay = HZ / 10;
709 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
711 fepriv->delay = 60 * HZ;
716 dprintk("%s: UNDEFINED ALGO !\n", __func__);
720 dvb_frontend_swzigzag(fe);
724 if (dvb_powerdown_on_sleep) {
725 if (fe->ops.set_voltage)
726 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
727 if (fe->ops.tuner_ops.sleep) {
728 if (fe->ops.i2c_gate_ctrl)
729 fe->ops.i2c_gate_ctrl(fe, 1);
730 fe->ops.tuner_ops.sleep(fe);
731 if (fe->ops.i2c_gate_ctrl)
732 fe->ops.i2c_gate_ctrl(fe, 0);
738 fepriv->thread = NULL;
739 if (kthread_should_stop())
740 fepriv->exit = DVB_FE_DEVICE_REMOVED;
742 fepriv->exit = DVB_FE_NO_EXIT;
745 dvb_frontend_wakeup(fe);
749 static void dvb_frontend_stop(struct dvb_frontend *fe)
751 struct dvb_frontend_private *fepriv = fe->frontend_priv;
753 dprintk ("%s\n", __func__);
755 fepriv->exit = DVB_FE_NORMAL_EXIT;
761 kthread_stop(fepriv->thread);
763 sema_init(&fepriv->sem, 1);
764 fepriv->state = FESTATE_IDLE;
766 /* paranoia check in case a signal arrived */
768 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
772 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
774 return ((curtime.tv_usec < lasttime.tv_usec) ?
775 1000000 - lasttime.tv_usec + curtime.tv_usec :
776 curtime.tv_usec - lasttime.tv_usec);
778 EXPORT_SYMBOL(timeval_usec_diff);
780 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
782 curtime->tv_usec += add_usec;
783 if (curtime->tv_usec >= 1000000) {
784 curtime->tv_usec -= 1000000;
790 * Sleep until gettimeofday() > waketime + add_usec
791 * This needs to be as precise as possible, but as the delay is
792 * usually between 2ms and 32ms, it is done using a scheduled msleep
793 * followed by usleep (normally a busy-wait loop) for the remainder
795 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
797 struct timeval lasttime;
800 timeval_usec_add(waketime, add_usec);
802 do_gettimeofday(&lasttime);
803 delta = timeval_usec_diff(lasttime, *waketime);
805 msleep((delta - 1500) / 1000);
806 do_gettimeofday(&lasttime);
807 newdelta = timeval_usec_diff(lasttime, *waketime);
808 delta = (newdelta > delta) ? 0 : newdelta;
813 EXPORT_SYMBOL(dvb_frontend_sleep_until);
815 static int dvb_frontend_start(struct dvb_frontend *fe)
818 struct dvb_frontend_private *fepriv = fe->frontend_priv;
819 struct task_struct *fe_thread;
821 dprintk ("%s\n", __func__);
823 if (fepriv->thread) {
824 if (fepriv->exit == DVB_FE_NO_EXIT)
827 dvb_frontend_stop (fe);
830 if (signal_pending(current))
832 if (down_interruptible (&fepriv->sem))
835 fepriv->state = FESTATE_IDLE;
836 fepriv->exit = DVB_FE_NO_EXIT;
837 fepriv->thread = NULL;
840 fe_thread = kthread_run(dvb_frontend_thread, fe,
841 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
842 if (IS_ERR(fe_thread)) {
843 ret = PTR_ERR(fe_thread);
844 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
848 fepriv->thread = fe_thread;
852 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
853 u32 *freq_min, u32 *freq_max)
855 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
857 if (fe->ops.info.frequency_max == 0)
858 *freq_max = fe->ops.tuner_ops.info.frequency_max;
859 else if (fe->ops.tuner_ops.info.frequency_max == 0)
860 *freq_max = fe->ops.info.frequency_max;
862 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
864 if (*freq_min == 0 || *freq_max == 0)
865 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
866 fe->dvb->num,fe->id);
869 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
871 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
875 /* range check: frequency */
876 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
877 if ((freq_min && c->frequency < freq_min) ||
878 (freq_max && c->frequency > freq_max)) {
879 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
880 fe->dvb->num, fe->id, c->frequency, freq_min, freq_max);
884 /* range check: symbol rate */
885 switch (c->delivery_system) {
889 case SYS_DVBC_ANNEX_A:
890 case SYS_DVBC_ANNEX_C:
891 if ((fe->ops.info.symbol_rate_min &&
892 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
893 (fe->ops.info.symbol_rate_max &&
894 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
895 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
896 fe->dvb->num, fe->id, c->symbol_rate,
897 fe->ops.info.symbol_rate_min,
898 fe->ops.info.symbol_rate_max);
908 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
910 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
914 delsys = c->delivery_system;
915 memset(c, 0, sizeof(struct dtv_frontend_properties));
916 c->delivery_system = delsys;
918 c->state = DTV_CLEAR;
920 dprintk("%s() Clearing cache for delivery system %d\n", __func__,
923 c->transmission_mode = TRANSMISSION_MODE_AUTO;
924 c->bandwidth_hz = 0; /* AUTO */
925 c->guard_interval = GUARD_INTERVAL_AUTO;
926 c->hierarchy = HIERARCHY_AUTO;
928 c->code_rate_HP = FEC_AUTO;
929 c->code_rate_LP = FEC_AUTO;
930 c->fec_inner = FEC_AUTO;
931 c->rolloff = ROLLOFF_AUTO;
932 c->voltage = SEC_VOLTAGE_OFF;
933 c->sectone = SEC_TONE_OFF;
934 c->pilot = PILOT_AUTO;
936 c->isdbt_partial_reception = 0;
937 c->isdbt_sb_mode = 0;
938 c->isdbt_sb_subchannel = 0;
939 c->isdbt_sb_segment_idx = 0;
940 c->isdbt_sb_segment_count = 0;
941 c->isdbt_layer_enabled = 0;
942 for (i = 0; i < 3; i++) {
943 c->layer[i].fec = FEC_AUTO;
944 c->layer[i].modulation = QAM_AUTO;
945 c->layer[i].interleaving = 0;
946 c->layer[i].segment_count = 0;
952 switch (c->delivery_system) {
956 c->modulation = QPSK; /* implied for DVB-S in legacy API */
957 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
960 c->modulation = VSB_8;
963 c->modulation = QAM_AUTO;
970 #define _DTV_CMD(n, s, b) \
978 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
979 _DTV_CMD(DTV_TUNE, 1, 0),
980 _DTV_CMD(DTV_CLEAR, 1, 0),
983 _DTV_CMD(DTV_FREQUENCY, 1, 0),
984 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
985 _DTV_CMD(DTV_MODULATION, 1, 0),
986 _DTV_CMD(DTV_INVERSION, 1, 0),
987 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
988 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
989 _DTV_CMD(DTV_INNER_FEC, 1, 0),
990 _DTV_CMD(DTV_VOLTAGE, 1, 0),
991 _DTV_CMD(DTV_TONE, 1, 0),
992 _DTV_CMD(DTV_PILOT, 1, 0),
993 _DTV_CMD(DTV_ROLLOFF, 1, 0),
994 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
995 _DTV_CMD(DTV_HIERARCHY, 1, 0),
996 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
997 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
998 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
999 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1001 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1002 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1003 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1004 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1005 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1006 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1007 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1014 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1016 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1017 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1018 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1020 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
1021 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
1024 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1025 _DTV_CMD(DTV_API_VERSION, 0, 0),
1026 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1027 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1028 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
1029 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
1030 _DTV_CMD(DTV_HIERARCHY, 0, 0),
1032 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1034 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1035 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1037 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1038 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 0, 0),
1039 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1040 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1041 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1042 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1043 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1044 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 0, 0),
1045 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1046 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1047 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1048 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1049 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1050 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1051 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1054 static void dtv_property_dump(struct dtv_property *tvp)
1058 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1059 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
1060 __func__, tvp->cmd);
1064 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1067 ,dtv_cmds[ tvp->cmd ].name);
1069 if(dtv_cmds[ tvp->cmd ].buffer) {
1071 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1073 ,tvp->u.buffer.len);
1075 for(i = 0; i < tvp->u.buffer.len; i++)
1076 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1079 ,tvp->u.buffer.data[i]);
1082 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1085 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1086 * drivers can use a single set_frontend tuning function, regardless of whether
1087 * it's being used for the legacy or new API, reducing code and complexity.
1089 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1090 struct dtv_frontend_properties *c,
1091 const struct dvb_frontend_parameters *p)
1093 c->frequency = p->frequency;
1094 c->inversion = p->inversion;
1096 switch (dvbv3_type(c->delivery_system)) {
1098 dprintk("%s() Preparing QPSK req\n", __func__);
1099 c->symbol_rate = p->u.qpsk.symbol_rate;
1100 c->fec_inner = p->u.qpsk.fec_inner;
1103 dprintk("%s() Preparing QAM req\n", __func__);
1104 c->symbol_rate = p->u.qam.symbol_rate;
1105 c->fec_inner = p->u.qam.fec_inner;
1106 c->modulation = p->u.qam.modulation;
1109 dprintk("%s() Preparing OFDM req\n", __func__);
1110 switch (p->u.ofdm.bandwidth) {
1111 case BANDWIDTH_10_MHZ:
1112 c->bandwidth_hz = 10000000;
1114 case BANDWIDTH_8_MHZ:
1115 c->bandwidth_hz = 8000000;
1117 case BANDWIDTH_7_MHZ:
1118 c->bandwidth_hz = 7000000;
1120 case BANDWIDTH_6_MHZ:
1121 c->bandwidth_hz = 6000000;
1123 case BANDWIDTH_5_MHZ:
1124 c->bandwidth_hz = 5000000;
1126 case BANDWIDTH_1_712_MHZ:
1127 c->bandwidth_hz = 1712000;
1129 case BANDWIDTH_AUTO:
1130 c->bandwidth_hz = 0;
1133 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1134 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1135 c->modulation = p->u.ofdm.constellation;
1136 c->transmission_mode = p->u.ofdm.transmission_mode;
1137 c->guard_interval = p->u.ofdm.guard_interval;
1138 c->hierarchy = p->u.ofdm.hierarchy_information;
1141 dprintk("%s() Preparing ATSC req\n", __func__);
1142 c->modulation = p->u.vsb.modulation;
1143 if (c->delivery_system == SYS_ATSCMH)
1145 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1146 c->delivery_system = SYS_ATSC;
1148 c->delivery_system = SYS_DVBC_ANNEX_B;
1152 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1153 __func__, c->delivery_system);
1160 /* Ensure the cached values are set correctly in the frontend
1161 * legacy tuning structures, for the advanced tuning API.
1163 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1164 struct dvb_frontend_parameters *p)
1166 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1168 p->frequency = c->frequency;
1169 p->inversion = c->inversion;
1171 switch (dvbv3_type(c->delivery_system)) {
1174 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1175 __func__, c->delivery_system);
1178 dprintk("%s() Preparing QPSK req\n", __func__);
1179 p->u.qpsk.symbol_rate = c->symbol_rate;
1180 p->u.qpsk.fec_inner = c->fec_inner;
1183 dprintk("%s() Preparing QAM req\n", __func__);
1184 p->u.qam.symbol_rate = c->symbol_rate;
1185 p->u.qam.fec_inner = c->fec_inner;
1186 p->u.qam.modulation = c->modulation;
1189 dprintk("%s() Preparing OFDM req\n", __func__);
1191 switch (c->bandwidth_hz) {
1193 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1196 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1199 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1202 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1205 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1208 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1212 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1214 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1215 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1216 p->u.ofdm.constellation = c->modulation;
1217 p->u.ofdm.transmission_mode = c->transmission_mode;
1218 p->u.ofdm.guard_interval = c->guard_interval;
1219 p->u.ofdm.hierarchy_information = c->hierarchy;
1222 dprintk("%s() Preparing VSB req\n", __func__);
1223 p->u.vsb.modulation = c->modulation;
1230 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1231 * @fe: struct dvb_frontend pointer
1232 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1233 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1235 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1236 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1237 * If p_out is not null, it will update the DVBv3 params pointed by it.
1239 static int dtv_get_frontend(struct dvb_frontend *fe,
1240 struct dvb_frontend_parameters *p_out)
1244 if (fe->ops.get_frontend) {
1245 r = fe->ops.get_frontend(fe);
1246 if (unlikely(r < 0))
1249 dtv_property_legacy_params_sync(fe, p_out);
1253 /* As everything is in cache, get_frontend fops are always supported */
1257 static int dvb_frontend_ioctl_legacy(struct file *file,
1258 unsigned int cmd, void *parg);
1259 static int dvb_frontend_ioctl_properties(struct file *file,
1260 unsigned int cmd, void *parg);
1262 static int dtv_property_process_get(struct dvb_frontend *fe,
1263 const struct dtv_frontend_properties *c,
1264 struct dtv_property *tvp,
1270 case DTV_ENUM_DELSYS:
1272 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1273 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1276 tvp->u.buffer.len = ncaps;
1279 tvp->u.data = c->frequency;
1281 case DTV_MODULATION:
1282 tvp->u.data = c->modulation;
1284 case DTV_BANDWIDTH_HZ:
1285 tvp->u.data = c->bandwidth_hz;
1288 tvp->u.data = c->inversion;
1290 case DTV_SYMBOL_RATE:
1291 tvp->u.data = c->symbol_rate;
1294 tvp->u.data = c->fec_inner;
1297 tvp->u.data = c->pilot;
1300 tvp->u.data = c->rolloff;
1302 case DTV_DELIVERY_SYSTEM:
1303 tvp->u.data = c->delivery_system;
1306 tvp->u.data = c->voltage;
1309 tvp->u.data = c->sectone;
1311 case DTV_API_VERSION:
1312 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1314 case DTV_CODE_RATE_HP:
1315 tvp->u.data = c->code_rate_HP;
1317 case DTV_CODE_RATE_LP:
1318 tvp->u.data = c->code_rate_LP;
1320 case DTV_GUARD_INTERVAL:
1321 tvp->u.data = c->guard_interval;
1323 case DTV_TRANSMISSION_MODE:
1324 tvp->u.data = c->transmission_mode;
1327 tvp->u.data = c->hierarchy;
1330 /* ISDB-T Support here */
1331 case DTV_ISDBT_PARTIAL_RECEPTION:
1332 tvp->u.data = c->isdbt_partial_reception;
1334 case DTV_ISDBT_SOUND_BROADCASTING:
1335 tvp->u.data = c->isdbt_sb_mode;
1337 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1338 tvp->u.data = c->isdbt_sb_subchannel;
1340 case DTV_ISDBT_SB_SEGMENT_IDX:
1341 tvp->u.data = c->isdbt_sb_segment_idx;
1343 case DTV_ISDBT_SB_SEGMENT_COUNT:
1344 tvp->u.data = c->isdbt_sb_segment_count;
1346 case DTV_ISDBT_LAYER_ENABLED:
1347 tvp->u.data = c->isdbt_layer_enabled;
1349 case DTV_ISDBT_LAYERA_FEC:
1350 tvp->u.data = c->layer[0].fec;
1352 case DTV_ISDBT_LAYERA_MODULATION:
1353 tvp->u.data = c->layer[0].modulation;
1355 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1356 tvp->u.data = c->layer[0].segment_count;
1358 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1359 tvp->u.data = c->layer[0].interleaving;
1361 case DTV_ISDBT_LAYERB_FEC:
1362 tvp->u.data = c->layer[1].fec;
1364 case DTV_ISDBT_LAYERB_MODULATION:
1365 tvp->u.data = c->layer[1].modulation;
1367 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1368 tvp->u.data = c->layer[1].segment_count;
1370 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1371 tvp->u.data = c->layer[1].interleaving;
1373 case DTV_ISDBT_LAYERC_FEC:
1374 tvp->u.data = c->layer[2].fec;
1376 case DTV_ISDBT_LAYERC_MODULATION:
1377 tvp->u.data = c->layer[2].modulation;
1379 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1380 tvp->u.data = c->layer[2].segment_count;
1382 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1383 tvp->u.data = c->layer[2].interleaving;
1385 case DTV_ISDBS_TS_ID:
1386 tvp->u.data = c->isdbs_ts_id;
1388 case DTV_DVBT2_PLP_ID:
1389 tvp->u.data = c->dvbt2_plp_id;
1393 case DTV_ATSCMH_FIC_VER:
1394 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1396 case DTV_ATSCMH_PARADE_ID:
1397 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1399 case DTV_ATSCMH_NOG:
1400 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1402 case DTV_ATSCMH_TNOG:
1403 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1405 case DTV_ATSCMH_SGN:
1406 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1408 case DTV_ATSCMH_PRC:
1409 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1411 case DTV_ATSCMH_RS_FRAME_MODE:
1412 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1414 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1415 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1417 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1418 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1420 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1421 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1423 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1424 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1426 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1427 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1429 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1430 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1432 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1433 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1435 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1436 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1443 /* Allow the frontend to override outgoing properties */
1444 if (fe->ops.get_property) {
1445 r = fe->ops.get_property(fe, tvp);
1450 dtv_property_dump(tvp);
1455 static int dtv_set_frontend(struct dvb_frontend *fe);
1457 static bool is_dvbv3_delsys(u32 delsys)
1461 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1462 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1467 static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1470 u32 delsys = SYS_UNDEFINED;
1471 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1472 enum dvbv3_emulation_type type;
1475 * It was reported that some old DVBv5 applications were
1476 * filling delivery_system with SYS_UNDEFINED. If this happens,
1477 * assume that the application wants to use the first supported
1480 if (c->delivery_system == SYS_UNDEFINED)
1481 c->delivery_system = fe->ops.delsys[0];
1483 if (desired_system == SYS_UNDEFINED) {
1485 * A DVBv3 call doesn't know what's the desired system.
1486 * Also, DVBv3 applications don't know that ops.info->type
1487 * could be changed, and they simply dies when it doesn't
1489 * So, don't change the current delivery system, as it
1490 * may be trying to do the wrong thing, like setting an
1491 * ISDB-T frontend as DVB-T. Instead, find the closest
1492 * DVBv3 system that matches the delivery system.
1494 if (is_dvbv3_delsys(c->delivery_system)) {
1495 dprintk("%s() Using delivery system to %d\n",
1496 __func__, c->delivery_system);
1499 type = dvbv3_type(c->delivery_system);
1502 desired_system = SYS_DVBS;
1505 desired_system = SYS_DVBC_ANNEX_A;
1508 desired_system = SYS_ATSC;
1511 desired_system = SYS_DVBT;
1514 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1519 * Get a delivery system that is compatible with DVBv3
1520 * NOTE: in order for this to work with softwares like Kaffeine that
1521 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1522 * DVB-S, drivers that support both should put the SYS_DVBS entry
1523 * before the SYS_DVBS2, otherwise it won't switch back to DVB-S.
1524 * The real fix is that userspace applications should not use DVBv3
1525 * and not trust on calling FE_SET_FRONTEND to switch the delivery
1529 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1530 if (fe->ops.delsys[ncaps] == desired_system) {
1531 delsys = desired_system;
1536 if (delsys == SYS_UNDEFINED) {
1537 dprintk("%s() Couldn't find a delivery system that matches %d\n",
1538 __func__, desired_system);
1542 * This is a DVBv5 call. So, it likely knows the supported
1546 /* Check if the desired delivery system is supported */
1548 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1549 if (fe->ops.delsys[ncaps] == desired_system) {
1550 c->delivery_system = desired_system;
1551 dprintk("%s() Changing delivery system to %d\n",
1552 __func__, desired_system);
1557 type = dvbv3_type(desired_system);
1560 * The delivery system is not supported. See if it can be
1562 * The emulation only works if the desired system is one of the
1563 * DVBv3 delivery systems
1565 if (!is_dvbv3_delsys(desired_system)) {
1566 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1572 * Get the last non-DVBv3 delivery system that has the same type
1573 * of the desired system
1576 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1577 if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1578 !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1579 delsys = fe->ops.delsys[ncaps];
1582 /* There's nothing compatible with the desired delivery system */
1583 if (delsys == SYS_UNDEFINED) {
1584 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1590 c->delivery_system = delsys;
1593 * The DVBv3 or DVBv5 call is requesting a different system. So,
1594 * emulation is needed.
1596 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1597 * for older DVBv5 applications. The emulation will try to use
1598 * the auto mode for most things, and will assume that the desired
1599 * delivery system is the last one at the ops.delsys[] array
1601 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1602 __func__, delsys, desired_system);
1605 * For now, handles ISDB-T calls. More code may be needed here for the
1606 * other emulated stuff
1608 if (type == DVBV3_OFDM) {
1609 if (c->delivery_system == SYS_ISDBT) {
1610 dprintk("%s() Using defaults for SYS_ISDBT\n",
1612 if (!c->bandwidth_hz)
1613 c->bandwidth_hz = 6000000;
1615 c->isdbt_partial_reception = 0;
1616 c->isdbt_sb_mode = 0;
1617 c->isdbt_sb_subchannel = 0;
1618 c->isdbt_sb_segment_idx = 0;
1619 c->isdbt_sb_segment_count = 0;
1620 c->isdbt_layer_enabled = 0;
1621 for (i = 0; i < 3; i++) {
1622 c->layer[i].fec = FEC_AUTO;
1623 c->layer[i].modulation = QAM_AUTO;
1624 c->layer[i].interleaving = 0;
1625 c->layer[i].segment_count = 0;
1629 dprintk("change delivery system on cache to %d\n", c->delivery_system);
1634 static int dtv_property_process_set(struct dvb_frontend *fe,
1635 struct dtv_property *tvp,
1639 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1641 /* Allow the frontend to validate incoming properties */
1642 if (fe->ops.set_property) {
1643 r = fe->ops.set_property(fe, tvp);
1651 * Reset a cache of data specific to the frontend here. This does
1652 * not effect hardware.
1654 dvb_frontend_clear_cache(fe);
1657 /* interpret the cache of data, build either a traditional frontend
1658 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1661 c->state = tvp->cmd;
1662 dprintk("%s() Finalised property cache\n", __func__);
1664 r = dtv_set_frontend(fe);
1667 c->frequency = tvp->u.data;
1669 case DTV_MODULATION:
1670 c->modulation = tvp->u.data;
1672 case DTV_BANDWIDTH_HZ:
1673 c->bandwidth_hz = tvp->u.data;
1676 c->inversion = tvp->u.data;
1678 case DTV_SYMBOL_RATE:
1679 c->symbol_rate = tvp->u.data;
1682 c->fec_inner = tvp->u.data;
1685 c->pilot = tvp->u.data;
1688 c->rolloff = tvp->u.data;
1690 case DTV_DELIVERY_SYSTEM:
1691 r = set_delivery_system(fe, tvp->u.data);
1694 c->voltage = tvp->u.data;
1695 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1696 (void *)c->voltage);
1699 c->sectone = tvp->u.data;
1700 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1701 (void *)c->sectone);
1703 case DTV_CODE_RATE_HP:
1704 c->code_rate_HP = tvp->u.data;
1706 case DTV_CODE_RATE_LP:
1707 c->code_rate_LP = tvp->u.data;
1709 case DTV_GUARD_INTERVAL:
1710 c->guard_interval = tvp->u.data;
1712 case DTV_TRANSMISSION_MODE:
1713 c->transmission_mode = tvp->u.data;
1716 c->hierarchy = tvp->u.data;
1719 /* ISDB-T Support here */
1720 case DTV_ISDBT_PARTIAL_RECEPTION:
1721 c->isdbt_partial_reception = tvp->u.data;
1723 case DTV_ISDBT_SOUND_BROADCASTING:
1724 c->isdbt_sb_mode = tvp->u.data;
1726 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1727 c->isdbt_sb_subchannel = tvp->u.data;
1729 case DTV_ISDBT_SB_SEGMENT_IDX:
1730 c->isdbt_sb_segment_idx = tvp->u.data;
1732 case DTV_ISDBT_SB_SEGMENT_COUNT:
1733 c->isdbt_sb_segment_count = tvp->u.data;
1735 case DTV_ISDBT_LAYER_ENABLED:
1736 c->isdbt_layer_enabled = tvp->u.data;
1738 case DTV_ISDBT_LAYERA_FEC:
1739 c->layer[0].fec = tvp->u.data;
1741 case DTV_ISDBT_LAYERA_MODULATION:
1742 c->layer[0].modulation = tvp->u.data;
1744 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1745 c->layer[0].segment_count = tvp->u.data;
1747 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1748 c->layer[0].interleaving = tvp->u.data;
1750 case DTV_ISDBT_LAYERB_FEC:
1751 c->layer[1].fec = tvp->u.data;
1753 case DTV_ISDBT_LAYERB_MODULATION:
1754 c->layer[1].modulation = tvp->u.data;
1756 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1757 c->layer[1].segment_count = tvp->u.data;
1759 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1760 c->layer[1].interleaving = tvp->u.data;
1762 case DTV_ISDBT_LAYERC_FEC:
1763 c->layer[2].fec = tvp->u.data;
1765 case DTV_ISDBT_LAYERC_MODULATION:
1766 c->layer[2].modulation = tvp->u.data;
1768 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1769 c->layer[2].segment_count = tvp->u.data;
1771 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1772 c->layer[2].interleaving = tvp->u.data;
1774 case DTV_ISDBS_TS_ID:
1775 c->isdbs_ts_id = tvp->u.data;
1777 case DTV_DVBT2_PLP_ID:
1778 c->dvbt2_plp_id = tvp->u.data;
1782 case DTV_ATSCMH_PARADE_ID:
1783 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1785 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1786 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1796 static int dvb_frontend_ioctl(struct file *file,
1797 unsigned int cmd, void *parg)
1799 struct dvb_device *dvbdev = file->private_data;
1800 struct dvb_frontend *fe = dvbdev->priv;
1801 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1802 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1803 int err = -EOPNOTSUPP;
1805 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1807 if (fepriv->exit != DVB_FE_NO_EXIT)
1810 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1811 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1812 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1815 if (down_interruptible (&fepriv->sem))
1816 return -ERESTARTSYS;
1818 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1819 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1821 c->state = DTV_UNDEFINED;
1822 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1829 static int dvb_frontend_ioctl_properties(struct file *file,
1830 unsigned int cmd, void *parg)
1832 struct dvb_device *dvbdev = file->private_data;
1833 struct dvb_frontend *fe = dvbdev->priv;
1834 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1835 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1838 struct dtv_properties *tvps = NULL;
1839 struct dtv_property *tvp = NULL;
1842 dprintk("%s\n", __func__);
1844 if(cmd == FE_SET_PROPERTY) {
1845 tvps = (struct dtv_properties __user *)parg;
1847 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1848 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1850 /* Put an arbitrary limit on the number of messages that can
1851 * be sent at once */
1852 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1855 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1861 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1866 for (i = 0; i < tvps->num; i++) {
1867 err = dtv_property_process_set(fe, tvp + i, file);
1870 (tvp + i)->result = err;
1873 if (c->state == DTV_TUNE)
1874 dprintk("%s() Property cache is full, tuning\n", __func__);
1877 if(cmd == FE_GET_PROPERTY) {
1878 tvps = (struct dtv_properties __user *)parg;
1880 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1881 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1883 /* Put an arbitrary limit on the number of messages that can
1884 * be sent at once */
1885 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1888 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1894 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1900 * Fills the cache out struct with the cache contents, plus
1901 * the data retrieved from get_frontend, if the frontend
1902 * is not idle. Otherwise, returns the cached content
1904 if (fepriv->state != FESTATE_IDLE) {
1905 err = dtv_get_frontend(fe, NULL);
1909 for (i = 0; i < tvps->num; i++) {
1910 err = dtv_property_process_get(fe, c, tvp + i, file);
1913 (tvp + i)->result = err;
1916 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1929 static int dtv_set_frontend(struct dvb_frontend *fe)
1931 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1932 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1933 struct dvb_frontend_tune_settings fetunesettings;
1936 if (dvb_frontend_check_parameters(fe) < 0)
1940 * Initialize output parameters to match the values given by
1941 * the user. FE_SET_FRONTEND triggers an initial frontend event
1942 * with status = 0, which copies output parameters to userspace.
1944 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
1947 * Be sure that the bandwidth will be filled for all
1948 * non-satellite systems, as tuners need to know what
1949 * low pass/Nyquist half filter should be applied, in
1950 * order to avoid inter-channel noise.
1952 * ISDB-T and DVB-T/T2 already sets bandwidth.
1953 * ATSC and DVB-C don't set, so, the core should fill it.
1955 * On DVB-C Annex A and C, the bandwidth is a function of
1956 * the roll-off and symbol rate. Annex B defines different
1957 * roll-off factors depending on the modulation. Fortunately,
1958 * Annex B is only used with 6MHz, so there's no need to
1961 * While not officially supported, a side effect of handling it at
1962 * the cache level is that a program could retrieve the bandwidth
1963 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1965 switch (c->delivery_system) {
1967 case SYS_DVBC_ANNEX_B:
1968 c->bandwidth_hz = 6000000;
1970 case SYS_DVBC_ANNEX_A:
1973 case SYS_DVBC_ANNEX_C:
1980 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
1982 /* force auto frequency inversion if requested */
1983 if (dvb_force_auto_inversion)
1984 c->inversion = INVERSION_AUTO;
1987 * without hierarchical coding code_rate_LP is irrelevant,
1988 * so we tolerate the otherwise invalid FEC_NONE setting
1990 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1991 c->code_rate_LP = FEC_AUTO;
1993 /* get frontend-specific tuning settings */
1994 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1995 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1996 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1997 fepriv->max_drift = fetunesettings.max_drift;
1998 fepriv->step_size = fetunesettings.step_size;
2000 /* default values */
2001 switch (c->delivery_system) {
2006 case SYS_DVBC_ANNEX_A:
2007 case SYS_DVBC_ANNEX_C:
2008 fepriv->min_delay = HZ / 20;
2009 fepriv->step_size = c->symbol_rate / 16000;
2010 fepriv->max_drift = c->symbol_rate / 2000;
2016 fepriv->min_delay = HZ / 20;
2017 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2018 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2022 * FIXME: This sounds wrong! if freqency_stepsize is
2023 * defined by the frontend, why not use it???
2025 fepriv->min_delay = HZ / 20;
2026 fepriv->step_size = 0; /* no zigzag */
2027 fepriv->max_drift = 0;
2031 if (dvb_override_tune_delay > 0)
2032 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2034 fepriv->state = FESTATE_RETUNE;
2036 /* Request the search algorithm to search */
2037 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2039 dvb_frontend_clear_events(fe);
2040 dvb_frontend_add_event(fe, 0);
2041 dvb_frontend_wakeup(fe);
2048 static int dvb_frontend_ioctl_legacy(struct file *file,
2049 unsigned int cmd, void *parg)
2051 struct dvb_device *dvbdev = file->private_data;
2052 struct dvb_frontend *fe = dvbdev->priv;
2053 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2054 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2055 int cb_err, err = -EOPNOTSUPP;
2057 if (fe->dvb->fe_ioctl_override) {
2058 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2064 /* fe_ioctl_override returning 0 allows
2065 * dvb-core to continue handling the ioctl */
2070 struct dvb_frontend_info* info = parg;
2072 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2073 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2076 * Associate the 4 delivery systems supported by DVBv3
2077 * API with their DVBv5 counterpart. For the other standards,
2078 * use the closest type, assuming that it would hopefully
2079 * work with a DVBv3 application.
2080 * It should be noticed that, on multi-frontend devices with
2081 * different types (terrestrial and cable, for example),
2082 * a pure DVBv3 application won't be able to use all delivery
2083 * systems. Yet, changing the DVBv5 cache to the other delivery
2084 * system should be enough for making it work.
2086 switch (dvbv3_type(c->delivery_system)) {
2088 info->type = FE_QPSK;
2091 info->type = FE_ATSC;
2094 info->type = FE_QAM;
2097 info->type = FE_OFDM;
2101 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2102 __func__, c->delivery_system);
2103 fe->ops.info.type = FE_OFDM;
2105 dprintk("current delivery system on cache: %d, V3 type: %d\n",
2106 c->delivery_system, fe->ops.info.type);
2108 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2109 * do it, it is done for it. */
2110 info->caps |= FE_CAN_INVERSION_AUTO;
2115 case FE_READ_STATUS: {
2116 fe_status_t* status = parg;
2118 /* if retune was requested but hasn't occurred yet, prevent
2119 * that user get signal state from previous tuning */
2120 if (fepriv->state == FESTATE_RETUNE ||
2121 fepriv->state == FESTATE_ERROR) {
2127 if (fe->ops.read_status)
2128 err = fe->ops.read_status(fe, status);
2132 if (fe->ops.read_ber)
2133 err = fe->ops.read_ber(fe, (__u32*) parg);
2136 case FE_READ_SIGNAL_STRENGTH:
2137 if (fe->ops.read_signal_strength)
2138 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
2142 if (fe->ops.read_snr)
2143 err = fe->ops.read_snr(fe, (__u16*) parg);
2146 case FE_READ_UNCORRECTED_BLOCKS:
2147 if (fe->ops.read_ucblocks)
2148 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
2152 case FE_DISEQC_RESET_OVERLOAD:
2153 if (fe->ops.diseqc_reset_overload) {
2154 err = fe->ops.diseqc_reset_overload(fe);
2155 fepriv->state = FESTATE_DISEQC;
2160 case FE_DISEQC_SEND_MASTER_CMD:
2161 if (fe->ops.diseqc_send_master_cmd) {
2162 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2163 fepriv->state = FESTATE_DISEQC;
2168 case FE_DISEQC_SEND_BURST:
2169 if (fe->ops.diseqc_send_burst) {
2170 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2171 fepriv->state = FESTATE_DISEQC;
2177 if (fe->ops.set_tone) {
2178 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2179 fepriv->tone = (fe_sec_tone_mode_t) parg;
2180 fepriv->state = FESTATE_DISEQC;
2185 case FE_SET_VOLTAGE:
2186 if (fe->ops.set_voltage) {
2187 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2188 fepriv->voltage = (fe_sec_voltage_t) parg;
2189 fepriv->state = FESTATE_DISEQC;
2194 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2195 if (fe->ops.dishnetwork_send_legacy_command) {
2196 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2197 fepriv->state = FESTATE_DISEQC;
2199 } else if (fe->ops.set_voltage) {
2201 * NOTE: This is a fallback condition. Some frontends
2202 * (stv0299 for instance) take longer than 8msec to
2203 * respond to a set_voltage command. Those switches
2204 * need custom routines to switch properly. For all
2205 * other frontends, the following should work ok.
2206 * Dish network legacy switches (as used by Dish500)
2207 * are controlled by sending 9-bit command words
2208 * spaced 8msec apart.
2209 * the actual command word is switch/port dependent
2210 * so it is up to the userspace application to send
2211 * the right command.
2212 * The command must always start with a '0' after
2213 * initialization, so parg is 8 bits and does not
2214 * include the initialization or start bit
2216 unsigned long swcmd = ((unsigned long) parg) << 1;
2217 struct timeval nexttime;
2218 struct timeval tv[10];
2221 if (dvb_frontend_debug)
2222 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2223 do_gettimeofday(&nexttime);
2224 if (dvb_frontend_debug)
2225 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2226 /* before sending a command, initialize by sending
2227 * a 32ms 18V to the switch
2229 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2230 dvb_frontend_sleep_until(&nexttime, 32000);
2232 for (i = 0; i < 9; i++) {
2233 if (dvb_frontend_debug)
2234 do_gettimeofday(&tv[i + 1]);
2235 if ((swcmd & 0x01) != last) {
2236 /* set voltage to (last ? 13V : 18V) */
2237 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2238 last = (last) ? 0 : 1;
2242 dvb_frontend_sleep_until(&nexttime, 8000);
2244 if (dvb_frontend_debug) {
2245 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2246 __func__, fe->dvb->num);
2247 for (i = 1; i < 10; i++)
2248 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2251 fepriv->state = FESTATE_DISEQC;
2256 case FE_DISEQC_RECV_SLAVE_REPLY:
2257 if (fe->ops.diseqc_recv_slave_reply)
2258 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2261 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2262 if (fe->ops.enable_high_lnb_voltage)
2263 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2266 case FE_SET_FRONTEND:
2267 err = set_delivery_system(fe, SYS_UNDEFINED);
2271 err = dtv_property_cache_sync(fe, c, parg);
2274 err = dtv_set_frontend(fe);
2277 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2280 case FE_GET_FRONTEND:
2281 err = dtv_get_frontend(fe, parg);
2284 case FE_SET_FRONTEND_TUNE_MODE:
2285 fepriv->tune_mode_flags = (unsigned long) parg;
2290 if (fe->dvb->fe_ioctl_override) {
2291 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2301 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2303 struct dvb_device *dvbdev = file->private_data;
2304 struct dvb_frontend *fe = dvbdev->priv;
2305 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2307 dprintk ("%s\n", __func__);
2309 poll_wait (file, &fepriv->events.wait_queue, wait);
2311 if (fepriv->events.eventw != fepriv->events.eventr)
2312 return (POLLIN | POLLRDNORM | POLLPRI);
2317 static int dvb_frontend_open(struct inode *inode, struct file *file)
2319 struct dvb_device *dvbdev = file->private_data;
2320 struct dvb_frontend *fe = dvbdev->priv;
2321 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2322 struct dvb_adapter *adapter = fe->dvb;
2325 dprintk ("%s\n", __func__);
2326 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2329 if (adapter->mfe_shared) {
2330 mutex_lock (&adapter->mfe_lock);
2332 if (adapter->mfe_dvbdev == NULL)
2333 adapter->mfe_dvbdev = dvbdev;
2335 else if (adapter->mfe_dvbdev != dvbdev) {
2337 *mfedev = adapter->mfe_dvbdev;
2339 *mfe = mfedev->priv;
2340 struct dvb_frontend_private
2341 *mfepriv = mfe->frontend_priv;
2342 int mferetry = (dvb_mfe_wait_time << 1);
2344 mutex_unlock (&adapter->mfe_lock);
2345 while (mferetry-- && (mfedev->users != -1 ||
2346 mfepriv->thread != NULL)) {
2347 if(msleep_interruptible(500)) {
2348 if(signal_pending(current))
2353 mutex_lock (&adapter->mfe_lock);
2354 if(adapter->mfe_dvbdev != dvbdev) {
2355 mfedev = adapter->mfe_dvbdev;
2357 mfepriv = mfe->frontend_priv;
2358 if (mfedev->users != -1 ||
2359 mfepriv->thread != NULL) {
2360 mutex_unlock (&adapter->mfe_lock);
2363 adapter->mfe_dvbdev = dvbdev;
2368 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2369 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2372 /* If we took control of the bus, we need to force
2373 reinitialization. This is because many ts_bus_ctrl()
2374 functions strobe the RESET pin on the demod, and if the
2375 frontend thread already exists then the dvb_init() routine
2376 won't get called (which is what usually does initial
2377 register configuration). */
2378 fepriv->reinitialise = 1;
2381 if ((ret = dvb_generic_open (inode, file)) < 0)
2384 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2385 /* normal tune mode when opened R/W */
2386 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2388 fepriv->voltage = -1;
2390 ret = dvb_frontend_start (fe);
2394 /* empty event queue */
2395 fepriv->events.eventr = fepriv->events.eventw = 0;
2398 if (adapter->mfe_shared)
2399 mutex_unlock (&adapter->mfe_lock);
2403 dvb_generic_release(inode, file);
2405 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2406 fe->ops.ts_bus_ctrl(fe, 0);
2408 if (adapter->mfe_shared)
2409 mutex_unlock (&adapter->mfe_lock);
2413 static int dvb_frontend_release(struct inode *inode, struct file *file)
2415 struct dvb_device *dvbdev = file->private_data;
2416 struct dvb_frontend *fe = dvbdev->priv;
2417 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2420 dprintk ("%s\n", __func__);
2422 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2423 fepriv->release_jiffies = jiffies;
2427 ret = dvb_generic_release (inode, file);
2429 if (dvbdev->users == -1) {
2430 wake_up(&fepriv->wait_queue);
2431 if (fepriv->exit != DVB_FE_NO_EXIT) {
2432 fops_put(file->f_op);
2434 wake_up(&dvbdev->wait_queue);
2436 if (fe->ops.ts_bus_ctrl)
2437 fe->ops.ts_bus_ctrl(fe, 0);
2443 static const struct file_operations dvb_frontend_fops = {
2444 .owner = THIS_MODULE,
2445 .unlocked_ioctl = dvb_generic_ioctl,
2446 .poll = dvb_frontend_poll,
2447 .open = dvb_frontend_open,
2448 .release = dvb_frontend_release,
2449 .llseek = noop_llseek,
2452 int dvb_register_frontend(struct dvb_adapter* dvb,
2453 struct dvb_frontend* fe)
2455 struct dvb_frontend_private *fepriv;
2456 static const struct dvb_device dvbdev_template = {
2460 .fops = &dvb_frontend_fops,
2461 .kernel_ioctl = dvb_frontend_ioctl
2464 dprintk ("%s\n", __func__);
2466 if (mutex_lock_interruptible(&frontend_mutex))
2467 return -ERESTARTSYS;
2469 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2470 if (fe->frontend_priv == NULL) {
2471 mutex_unlock(&frontend_mutex);
2474 fepriv = fe->frontend_priv;
2476 sema_init(&fepriv->sem, 1);
2477 init_waitqueue_head (&fepriv->wait_queue);
2478 init_waitqueue_head (&fepriv->events.wait_queue);
2479 mutex_init(&fepriv->events.mtx);
2481 fepriv->inversion = INVERSION_OFF;
2483 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2488 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2489 fe, DVB_DEVICE_FRONTEND);
2492 * Initialize the cache to the proper values according with the
2493 * first supported delivery system (ops->delsys[0])
2496 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2497 dvb_frontend_clear_cache(fe);
2499 mutex_unlock(&frontend_mutex);
2502 EXPORT_SYMBOL(dvb_register_frontend);
2504 int dvb_unregister_frontend(struct dvb_frontend* fe)
2506 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2507 dprintk ("%s\n", __func__);
2509 mutex_lock(&frontend_mutex);
2510 dvb_frontend_stop (fe);
2511 mutex_unlock(&frontend_mutex);
2513 if (fepriv->dvbdev->users < -1)
2514 wait_event(fepriv->dvbdev->wait_queue,
2515 fepriv->dvbdev->users==-1);
2517 mutex_lock(&frontend_mutex);
2518 dvb_unregister_device (fepriv->dvbdev);
2520 /* fe is invalid now */
2522 mutex_unlock(&frontend_mutex);
2525 EXPORT_SYMBOL(dvb_unregister_frontend);
2527 #ifdef CONFIG_MEDIA_ATTACH
2528 void dvb_frontend_detach(struct dvb_frontend* fe)
2532 if (fe->ops.release_sec) {
2533 fe->ops.release_sec(fe);
2534 symbol_put_addr(fe->ops.release_sec);
2536 if (fe->ops.tuner_ops.release) {
2537 fe->ops.tuner_ops.release(fe);
2538 symbol_put_addr(fe->ops.tuner_ops.release);
2540 if (fe->ops.analog_ops.release) {
2541 fe->ops.analog_ops.release(fe);
2542 symbol_put_addr(fe->ops.analog_ops.release);
2544 ptr = (void*)fe->ops.release;
2546 fe->ops.release(fe);
2547 symbol_put_addr(ptr);
2551 void dvb_frontend_detach(struct dvb_frontend* fe)
2553 if (fe->ops.release_sec)
2554 fe->ops.release_sec(fe);
2555 if (fe->ops.tuner_ops.release)
2556 fe->ops.tuner_ops.release(fe);
2557 if (fe->ops.analog_ops.release)
2558 fe->ops.analog_ops.release(fe);
2559 if (fe->ops.release)
2560 fe->ops.release(fe);
2563 EXPORT_SYMBOL(dvb_frontend_detach);