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:
185 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),
1035 static void dtv_property_dump(struct dtv_property *tvp)
1039 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1040 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
1041 __func__, tvp->cmd);
1045 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1048 ,dtv_cmds[ tvp->cmd ].name);
1050 if(dtv_cmds[ tvp->cmd ].buffer) {
1052 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1054 ,tvp->u.buffer.len);
1056 for(i = 0; i < tvp->u.buffer.len; i++)
1057 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1060 ,tvp->u.buffer.data[i]);
1063 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1066 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1067 * drivers can use a single set_frontend tuning function, regardless of whether
1068 * it's being used for the legacy or new API, reducing code and complexity.
1070 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1071 struct dtv_frontend_properties *c,
1072 const struct dvb_frontend_parameters *p)
1074 c->frequency = p->frequency;
1075 c->inversion = p->inversion;
1077 switch (dvbv3_type(c->delivery_system)) {
1079 dprintk("%s() Preparing QPSK req\n", __func__);
1080 c->symbol_rate = p->u.qpsk.symbol_rate;
1081 c->fec_inner = p->u.qpsk.fec_inner;
1084 dprintk("%s() Preparing QAM req\n", __func__);
1085 c->symbol_rate = p->u.qam.symbol_rate;
1086 c->fec_inner = p->u.qam.fec_inner;
1087 c->modulation = p->u.qam.modulation;
1090 dprintk("%s() Preparing OFDM req\n", __func__);
1091 switch (p->u.ofdm.bandwidth) {
1092 case BANDWIDTH_10_MHZ:
1093 c->bandwidth_hz = 10000000;
1095 case BANDWIDTH_8_MHZ:
1096 c->bandwidth_hz = 8000000;
1098 case BANDWIDTH_7_MHZ:
1099 c->bandwidth_hz = 7000000;
1101 case BANDWIDTH_6_MHZ:
1102 c->bandwidth_hz = 6000000;
1104 case BANDWIDTH_5_MHZ:
1105 c->bandwidth_hz = 5000000;
1107 case BANDWIDTH_1_712_MHZ:
1108 c->bandwidth_hz = 1712000;
1110 case BANDWIDTH_AUTO:
1111 c->bandwidth_hz = 0;
1114 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1115 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1116 c->modulation = p->u.ofdm.constellation;
1117 c->transmission_mode = p->u.ofdm.transmission_mode;
1118 c->guard_interval = p->u.ofdm.guard_interval;
1119 c->hierarchy = p->u.ofdm.hierarchy_information;
1122 dprintk("%s() Preparing ATSC req\n", __func__);
1123 c->modulation = p->u.vsb.modulation;
1124 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1125 c->delivery_system = SYS_ATSC;
1127 c->delivery_system = SYS_DVBC_ANNEX_B;
1131 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1132 __func__, c->delivery_system);
1139 /* Ensure the cached values are set correctly in the frontend
1140 * legacy tuning structures, for the advanced tuning API.
1142 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1143 struct dvb_frontend_parameters *p)
1145 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1147 p->frequency = c->frequency;
1148 p->inversion = c->inversion;
1150 switch (dvbv3_type(c->delivery_system)) {
1153 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1154 __func__, c->delivery_system);
1157 dprintk("%s() Preparing QPSK req\n", __func__);
1158 p->u.qpsk.symbol_rate = c->symbol_rate;
1159 p->u.qpsk.fec_inner = c->fec_inner;
1162 dprintk("%s() Preparing QAM req\n", __func__);
1163 p->u.qam.symbol_rate = c->symbol_rate;
1164 p->u.qam.fec_inner = c->fec_inner;
1165 p->u.qam.modulation = c->modulation;
1168 dprintk("%s() Preparing OFDM req\n", __func__);
1170 switch (c->bandwidth_hz) {
1172 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1175 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1178 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1181 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1184 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1187 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1191 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1193 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1194 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1195 p->u.ofdm.constellation = c->modulation;
1196 p->u.ofdm.transmission_mode = c->transmission_mode;
1197 p->u.ofdm.guard_interval = c->guard_interval;
1198 p->u.ofdm.hierarchy_information = c->hierarchy;
1201 dprintk("%s() Preparing VSB req\n", __func__);
1202 p->u.vsb.modulation = c->modulation;
1209 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1210 * @fe: struct dvb_frontend pointer
1211 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1212 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1214 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1215 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1216 * If p_out is not null, it will update the DVBv3 params pointed by it.
1218 static int dtv_get_frontend(struct dvb_frontend *fe,
1219 struct dvb_frontend_parameters *p_out)
1223 if (fe->ops.get_frontend) {
1224 r = fe->ops.get_frontend(fe);
1225 if (unlikely(r < 0))
1228 dtv_property_legacy_params_sync(fe, p_out);
1232 /* As everything is in cache, get_frontend fops are always supported */
1236 static int dvb_frontend_ioctl_legacy(struct file *file,
1237 unsigned int cmd, void *parg);
1238 static int dvb_frontend_ioctl_properties(struct file *file,
1239 unsigned int cmd, void *parg);
1241 static int dtv_property_process_get(struct dvb_frontend *fe,
1242 const struct dtv_frontend_properties *c,
1243 struct dtv_property *tvp,
1249 case DTV_ENUM_DELSYS:
1251 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1252 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1255 tvp->u.buffer.len = ncaps;
1258 tvp->u.data = c->frequency;
1260 case DTV_MODULATION:
1261 tvp->u.data = c->modulation;
1263 case DTV_BANDWIDTH_HZ:
1264 tvp->u.data = c->bandwidth_hz;
1267 tvp->u.data = c->inversion;
1269 case DTV_SYMBOL_RATE:
1270 tvp->u.data = c->symbol_rate;
1273 tvp->u.data = c->fec_inner;
1276 tvp->u.data = c->pilot;
1279 tvp->u.data = c->rolloff;
1281 case DTV_DELIVERY_SYSTEM:
1282 tvp->u.data = c->delivery_system;
1285 tvp->u.data = c->voltage;
1288 tvp->u.data = c->sectone;
1290 case DTV_API_VERSION:
1291 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1293 case DTV_CODE_RATE_HP:
1294 tvp->u.data = c->code_rate_HP;
1296 case DTV_CODE_RATE_LP:
1297 tvp->u.data = c->code_rate_LP;
1299 case DTV_GUARD_INTERVAL:
1300 tvp->u.data = c->guard_interval;
1302 case DTV_TRANSMISSION_MODE:
1303 tvp->u.data = c->transmission_mode;
1306 tvp->u.data = c->hierarchy;
1309 /* ISDB-T Support here */
1310 case DTV_ISDBT_PARTIAL_RECEPTION:
1311 tvp->u.data = c->isdbt_partial_reception;
1313 case DTV_ISDBT_SOUND_BROADCASTING:
1314 tvp->u.data = c->isdbt_sb_mode;
1316 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1317 tvp->u.data = c->isdbt_sb_subchannel;
1319 case DTV_ISDBT_SB_SEGMENT_IDX:
1320 tvp->u.data = c->isdbt_sb_segment_idx;
1322 case DTV_ISDBT_SB_SEGMENT_COUNT:
1323 tvp->u.data = c->isdbt_sb_segment_count;
1325 case DTV_ISDBT_LAYER_ENABLED:
1326 tvp->u.data = c->isdbt_layer_enabled;
1328 case DTV_ISDBT_LAYERA_FEC:
1329 tvp->u.data = c->layer[0].fec;
1331 case DTV_ISDBT_LAYERA_MODULATION:
1332 tvp->u.data = c->layer[0].modulation;
1334 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1335 tvp->u.data = c->layer[0].segment_count;
1337 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1338 tvp->u.data = c->layer[0].interleaving;
1340 case DTV_ISDBT_LAYERB_FEC:
1341 tvp->u.data = c->layer[1].fec;
1343 case DTV_ISDBT_LAYERB_MODULATION:
1344 tvp->u.data = c->layer[1].modulation;
1346 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1347 tvp->u.data = c->layer[1].segment_count;
1349 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1350 tvp->u.data = c->layer[1].interleaving;
1352 case DTV_ISDBT_LAYERC_FEC:
1353 tvp->u.data = c->layer[2].fec;
1355 case DTV_ISDBT_LAYERC_MODULATION:
1356 tvp->u.data = c->layer[2].modulation;
1358 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1359 tvp->u.data = c->layer[2].segment_count;
1361 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1362 tvp->u.data = c->layer[2].interleaving;
1364 case DTV_ISDBS_TS_ID:
1365 tvp->u.data = c->isdbs_ts_id;
1367 case DTV_DVBT2_PLP_ID:
1368 tvp->u.data = c->dvbt2_plp_id;
1374 /* Allow the frontend to override outgoing properties */
1375 if (fe->ops.get_property) {
1376 r = fe->ops.get_property(fe, tvp);
1381 dtv_property_dump(tvp);
1386 static int dtv_set_frontend(struct dvb_frontend *fe);
1388 static bool is_dvbv3_delsys(u32 delsys)
1392 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1393 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1398 static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1401 u32 delsys = SYS_UNDEFINED;
1402 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1403 enum dvbv3_emulation_type type;
1406 * It was reported that some old DVBv5 applications were
1407 * filling delivery_system with SYS_UNDEFINED. If this happens,
1408 * assume that the application wants to use the first supported
1411 if (c->delivery_system == SYS_UNDEFINED)
1412 c->delivery_system = fe->ops.delsys[0];
1414 if (desired_system == SYS_UNDEFINED) {
1416 * A DVBv3 call doesn't know what's the desired system.
1417 * Also, DVBv3 applications don't know that ops.info->type
1418 * could be changed, and they simply dies when it doesn't
1420 * So, don't change the current delivery system, as it
1421 * may be trying to do the wrong thing, like setting an
1422 * ISDB-T frontend as DVB-T. Instead, find the closest
1423 * DVBv3 system that matches the delivery system.
1425 if (is_dvbv3_delsys(c->delivery_system)) {
1426 dprintk("%s() Using delivery system to %d\n",
1427 __func__, c->delivery_system);
1430 type = dvbv3_type(c->delivery_system);
1433 desired_system = SYS_DVBS;
1436 desired_system = SYS_DVBC_ANNEX_A;
1439 desired_system = SYS_ATSC;
1442 desired_system = SYS_DVBT;
1445 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1450 * Get a delivery system that is compatible with DVBv3
1451 * NOTE: in order for this to work with softwares like Kaffeine that
1452 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1453 * DVB-S, drivers that support both should put the SYS_DVBS entry
1454 * before the SYS_DVBS2, otherwise it won't switch back to DVB-S.
1455 * The real fix is that userspace applications should not use DVBv3
1456 * and not trust on calling FE_SET_FRONTEND to switch the delivery
1460 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1461 if (fe->ops.delsys[ncaps] == desired_system) {
1462 delsys = desired_system;
1467 if (delsys == SYS_UNDEFINED) {
1468 dprintk("%s() Couldn't find a delivery system that matches %d\n",
1469 __func__, desired_system);
1473 * This is a DVBv5 call. So, it likely knows the supported
1477 /* Check if the desired delivery system is supported */
1479 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1480 if (fe->ops.delsys[ncaps] == desired_system) {
1481 c->delivery_system = desired_system;
1482 dprintk("%s() Changing delivery system to %d\n",
1483 __func__, desired_system);
1488 type = dvbv3_type(desired_system);
1491 * The delivery system is not supported. See if it can be
1493 * The emulation only works if the desired system is one of the
1494 * DVBv3 delivery systems
1496 if (!is_dvbv3_delsys(desired_system)) {
1497 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1503 * Get the last non-DVBv3 delivery system that has the same type
1504 * of the desired system
1507 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1508 if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1509 !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1510 delsys = fe->ops.delsys[ncaps];
1513 /* There's nothing compatible with the desired delivery system */
1514 if (delsys == SYS_UNDEFINED) {
1515 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1521 c->delivery_system = delsys;
1524 * The DVBv3 or DVBv5 call is requesting a different system. So,
1525 * emulation is needed.
1527 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1528 * for older DVBv5 applications. The emulation will try to use
1529 * the auto mode for most things, and will assume that the desired
1530 * delivery system is the last one at the ops.delsys[] array
1532 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1533 __func__, delsys, desired_system);
1536 * For now, handles ISDB-T calls. More code may be needed here for the
1537 * other emulated stuff
1539 if (type == DVBV3_OFDM) {
1540 if (c->delivery_system == SYS_ISDBT) {
1541 dprintk("%s() Using defaults for SYS_ISDBT\n",
1543 if (!c->bandwidth_hz)
1544 c->bandwidth_hz = 6000000;
1546 c->isdbt_partial_reception = 0;
1547 c->isdbt_sb_mode = 0;
1548 c->isdbt_sb_subchannel = 0;
1549 c->isdbt_sb_segment_idx = 0;
1550 c->isdbt_sb_segment_count = 0;
1551 c->isdbt_layer_enabled = 0;
1552 for (i = 0; i < 3; i++) {
1553 c->layer[i].fec = FEC_AUTO;
1554 c->layer[i].modulation = QAM_AUTO;
1555 c->layer[i].interleaving = 0;
1556 c->layer[i].segment_count = 0;
1560 dprintk("change delivery system on cache to %d\n", c->delivery_system);
1565 static int dtv_property_process_set(struct dvb_frontend *fe,
1566 struct dtv_property *tvp,
1570 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1572 /* Allow the frontend to validate incoming properties */
1573 if (fe->ops.set_property) {
1574 r = fe->ops.set_property(fe, tvp);
1582 * Reset a cache of data specific to the frontend here. This does
1583 * not effect hardware.
1585 dvb_frontend_clear_cache(fe);
1588 /* interpret the cache of data, build either a traditional frontend
1589 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1592 c->state = tvp->cmd;
1593 dprintk("%s() Finalised property cache\n", __func__);
1595 r = dtv_set_frontend(fe);
1598 c->frequency = tvp->u.data;
1600 case DTV_MODULATION:
1601 c->modulation = tvp->u.data;
1603 case DTV_BANDWIDTH_HZ:
1604 c->bandwidth_hz = tvp->u.data;
1607 c->inversion = tvp->u.data;
1609 case DTV_SYMBOL_RATE:
1610 c->symbol_rate = tvp->u.data;
1613 c->fec_inner = tvp->u.data;
1616 c->pilot = tvp->u.data;
1619 c->rolloff = tvp->u.data;
1621 case DTV_DELIVERY_SYSTEM:
1622 r = set_delivery_system(fe, tvp->u.data);
1625 c->voltage = tvp->u.data;
1626 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1627 (void *)c->voltage);
1630 c->sectone = tvp->u.data;
1631 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1632 (void *)c->sectone);
1634 case DTV_CODE_RATE_HP:
1635 c->code_rate_HP = tvp->u.data;
1637 case DTV_CODE_RATE_LP:
1638 c->code_rate_LP = tvp->u.data;
1640 case DTV_GUARD_INTERVAL:
1641 c->guard_interval = tvp->u.data;
1643 case DTV_TRANSMISSION_MODE:
1644 c->transmission_mode = tvp->u.data;
1647 c->hierarchy = tvp->u.data;
1650 /* ISDB-T Support here */
1651 case DTV_ISDBT_PARTIAL_RECEPTION:
1652 c->isdbt_partial_reception = tvp->u.data;
1654 case DTV_ISDBT_SOUND_BROADCASTING:
1655 c->isdbt_sb_mode = tvp->u.data;
1657 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1658 c->isdbt_sb_subchannel = tvp->u.data;
1660 case DTV_ISDBT_SB_SEGMENT_IDX:
1661 c->isdbt_sb_segment_idx = tvp->u.data;
1663 case DTV_ISDBT_SB_SEGMENT_COUNT:
1664 c->isdbt_sb_segment_count = tvp->u.data;
1666 case DTV_ISDBT_LAYER_ENABLED:
1667 c->isdbt_layer_enabled = tvp->u.data;
1669 case DTV_ISDBT_LAYERA_FEC:
1670 c->layer[0].fec = tvp->u.data;
1672 case DTV_ISDBT_LAYERA_MODULATION:
1673 c->layer[0].modulation = tvp->u.data;
1675 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1676 c->layer[0].segment_count = tvp->u.data;
1678 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1679 c->layer[0].interleaving = tvp->u.data;
1681 case DTV_ISDBT_LAYERB_FEC:
1682 c->layer[1].fec = tvp->u.data;
1684 case DTV_ISDBT_LAYERB_MODULATION:
1685 c->layer[1].modulation = tvp->u.data;
1687 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1688 c->layer[1].segment_count = tvp->u.data;
1690 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1691 c->layer[1].interleaving = tvp->u.data;
1693 case DTV_ISDBT_LAYERC_FEC:
1694 c->layer[2].fec = tvp->u.data;
1696 case DTV_ISDBT_LAYERC_MODULATION:
1697 c->layer[2].modulation = tvp->u.data;
1699 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1700 c->layer[2].segment_count = tvp->u.data;
1702 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1703 c->layer[2].interleaving = tvp->u.data;
1705 case DTV_ISDBS_TS_ID:
1706 c->isdbs_ts_id = tvp->u.data;
1708 case DTV_DVBT2_PLP_ID:
1709 c->dvbt2_plp_id = tvp->u.data;
1718 static int dvb_frontend_ioctl(struct file *file,
1719 unsigned int cmd, void *parg)
1721 struct dvb_device *dvbdev = file->private_data;
1722 struct dvb_frontend *fe = dvbdev->priv;
1723 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1724 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1725 int err = -EOPNOTSUPP;
1727 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1729 if (fepriv->exit != DVB_FE_NO_EXIT)
1732 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1733 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1734 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1737 if (down_interruptible (&fepriv->sem))
1738 return -ERESTARTSYS;
1740 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1741 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1743 c->state = DTV_UNDEFINED;
1744 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1751 static int dvb_frontend_ioctl_properties(struct file *file,
1752 unsigned int cmd, void *parg)
1754 struct dvb_device *dvbdev = file->private_data;
1755 struct dvb_frontend *fe = dvbdev->priv;
1756 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1757 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1760 struct dtv_properties *tvps = NULL;
1761 struct dtv_property *tvp = NULL;
1764 dprintk("%s\n", __func__);
1766 if(cmd == FE_SET_PROPERTY) {
1767 tvps = (struct dtv_properties __user *)parg;
1769 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1770 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1772 /* Put an arbitrary limit on the number of messages that can
1773 * be sent at once */
1774 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1777 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1783 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1788 for (i = 0; i < tvps->num; i++) {
1789 err = dtv_property_process_set(fe, tvp + i, file);
1792 (tvp + i)->result = err;
1795 if (c->state == DTV_TUNE)
1796 dprintk("%s() Property cache is full, tuning\n", __func__);
1799 if(cmd == FE_GET_PROPERTY) {
1800 tvps = (struct dtv_properties __user *)parg;
1802 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1803 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1805 /* Put an arbitrary limit on the number of messages that can
1806 * be sent at once */
1807 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1810 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1816 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1822 * Fills the cache out struct with the cache contents, plus
1823 * the data retrieved from get_frontend, if the frontend
1824 * is not idle. Otherwise, returns the cached content
1826 if (fepriv->state != FESTATE_IDLE) {
1827 err = dtv_get_frontend(fe, NULL);
1831 for (i = 0; i < tvps->num; i++) {
1832 err = dtv_property_process_get(fe, c, tvp + i, file);
1835 (tvp + i)->result = err;
1838 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1851 static int dtv_set_frontend(struct dvb_frontend *fe)
1853 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1854 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1855 struct dvb_frontend_tune_settings fetunesettings;
1858 if (dvb_frontend_check_parameters(fe) < 0)
1862 * Initialize output parameters to match the values given by
1863 * the user. FE_SET_FRONTEND triggers an initial frontend event
1864 * with status = 0, which copies output parameters to userspace.
1866 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
1869 * Be sure that the bandwidth will be filled for all
1870 * non-satellite systems, as tuners need to know what
1871 * low pass/Nyquist half filter should be applied, in
1872 * order to avoid inter-channel noise.
1874 * ISDB-T and DVB-T/T2 already sets bandwidth.
1875 * ATSC and DVB-C don't set, so, the core should fill it.
1877 * On DVB-C Annex A and C, the bandwidth is a function of
1878 * the roll-off and symbol rate. Annex B defines different
1879 * roll-off factors depending on the modulation. Fortunately,
1880 * Annex B is only used with 6MHz, so there's no need to
1883 * While not officially supported, a side effect of handling it at
1884 * the cache level is that a program could retrieve the bandwidth
1885 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1887 switch (c->delivery_system) {
1889 case SYS_DVBC_ANNEX_B:
1890 c->bandwidth_hz = 6000000;
1892 case SYS_DVBC_ANNEX_A:
1895 case SYS_DVBC_ANNEX_C:
1902 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
1904 /* force auto frequency inversion if requested */
1905 if (dvb_force_auto_inversion)
1906 c->inversion = INVERSION_AUTO;
1909 * without hierarchical coding code_rate_LP is irrelevant,
1910 * so we tolerate the otherwise invalid FEC_NONE setting
1912 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1913 c->code_rate_LP = FEC_AUTO;
1915 /* get frontend-specific tuning settings */
1916 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1917 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1918 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1919 fepriv->max_drift = fetunesettings.max_drift;
1920 fepriv->step_size = fetunesettings.step_size;
1922 /* default values */
1923 switch (c->delivery_system) {
1928 case SYS_DVBC_ANNEX_A:
1929 case SYS_DVBC_ANNEX_C:
1930 fepriv->min_delay = HZ / 20;
1931 fepriv->step_size = c->symbol_rate / 16000;
1932 fepriv->max_drift = c->symbol_rate / 2000;
1938 fepriv->min_delay = HZ / 20;
1939 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1940 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1944 * FIXME: This sounds wrong! if freqency_stepsize is
1945 * defined by the frontend, why not use it???
1947 fepriv->min_delay = HZ / 20;
1948 fepriv->step_size = 0; /* no zigzag */
1949 fepriv->max_drift = 0;
1953 if (dvb_override_tune_delay > 0)
1954 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1956 fepriv->state = FESTATE_RETUNE;
1958 /* Request the search algorithm to search */
1959 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1961 dvb_frontend_clear_events(fe);
1962 dvb_frontend_add_event(fe, 0);
1963 dvb_frontend_wakeup(fe);
1970 static int dvb_frontend_ioctl_legacy(struct file *file,
1971 unsigned int cmd, void *parg)
1973 struct dvb_device *dvbdev = file->private_data;
1974 struct dvb_frontend *fe = dvbdev->priv;
1975 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1976 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1977 int cb_err, err = -EOPNOTSUPP;
1979 if (fe->dvb->fe_ioctl_override) {
1980 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1986 /* fe_ioctl_override returning 0 allows
1987 * dvb-core to continue handling the ioctl */
1992 struct dvb_frontend_info* info = parg;
1994 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1995 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1998 * Associate the 4 delivery systems supported by DVBv3
1999 * API with their DVBv5 counterpart. For the other standards,
2000 * use the closest type, assuming that it would hopefully
2001 * work with a DVBv3 application.
2002 * It should be noticed that, on multi-frontend devices with
2003 * different types (terrestrial and cable, for example),
2004 * a pure DVBv3 application won't be able to use all delivery
2005 * systems. Yet, changing the DVBv5 cache to the other delivery
2006 * system should be enough for making it work.
2008 switch (dvbv3_type(c->delivery_system)) {
2010 info->type = FE_QPSK;
2013 info->type = FE_ATSC;
2016 info->type = FE_QAM;
2019 info->type = FE_OFDM;
2023 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2024 __func__, c->delivery_system);
2025 fe->ops.info.type = FE_OFDM;
2027 dprintk("current delivery system on cache: %d, V3 type: %d\n",
2028 c->delivery_system, fe->ops.info.type);
2030 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2031 * do it, it is done for it. */
2032 info->caps |= FE_CAN_INVERSION_AUTO;
2037 case FE_READ_STATUS: {
2038 fe_status_t* status = parg;
2040 /* if retune was requested but hasn't occurred yet, prevent
2041 * that user get signal state from previous tuning */
2042 if (fepriv->state == FESTATE_RETUNE ||
2043 fepriv->state == FESTATE_ERROR) {
2049 if (fe->ops.read_status)
2050 err = fe->ops.read_status(fe, status);
2054 if (fe->ops.read_ber)
2055 err = fe->ops.read_ber(fe, (__u32*) parg);
2058 case FE_READ_SIGNAL_STRENGTH:
2059 if (fe->ops.read_signal_strength)
2060 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
2064 if (fe->ops.read_snr)
2065 err = fe->ops.read_snr(fe, (__u16*) parg);
2068 case FE_READ_UNCORRECTED_BLOCKS:
2069 if (fe->ops.read_ucblocks)
2070 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
2074 case FE_DISEQC_RESET_OVERLOAD:
2075 if (fe->ops.diseqc_reset_overload) {
2076 err = fe->ops.diseqc_reset_overload(fe);
2077 fepriv->state = FESTATE_DISEQC;
2082 case FE_DISEQC_SEND_MASTER_CMD:
2083 if (fe->ops.diseqc_send_master_cmd) {
2084 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2085 fepriv->state = FESTATE_DISEQC;
2090 case FE_DISEQC_SEND_BURST:
2091 if (fe->ops.diseqc_send_burst) {
2092 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2093 fepriv->state = FESTATE_DISEQC;
2099 if (fe->ops.set_tone) {
2100 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2101 fepriv->tone = (fe_sec_tone_mode_t) parg;
2102 fepriv->state = FESTATE_DISEQC;
2107 case FE_SET_VOLTAGE:
2108 if (fe->ops.set_voltage) {
2109 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2110 fepriv->voltage = (fe_sec_voltage_t) parg;
2111 fepriv->state = FESTATE_DISEQC;
2116 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2117 if (fe->ops.dishnetwork_send_legacy_command) {
2118 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2119 fepriv->state = FESTATE_DISEQC;
2121 } else if (fe->ops.set_voltage) {
2123 * NOTE: This is a fallback condition. Some frontends
2124 * (stv0299 for instance) take longer than 8msec to
2125 * respond to a set_voltage command. Those switches
2126 * need custom routines to switch properly. For all
2127 * other frontends, the following should work ok.
2128 * Dish network legacy switches (as used by Dish500)
2129 * are controlled by sending 9-bit command words
2130 * spaced 8msec apart.
2131 * the actual command word is switch/port dependent
2132 * so it is up to the userspace application to send
2133 * the right command.
2134 * The command must always start with a '0' after
2135 * initialization, so parg is 8 bits and does not
2136 * include the initialization or start bit
2138 unsigned long swcmd = ((unsigned long) parg) << 1;
2139 struct timeval nexttime;
2140 struct timeval tv[10];
2143 if (dvb_frontend_debug)
2144 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2145 do_gettimeofday(&nexttime);
2146 if (dvb_frontend_debug)
2147 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2148 /* before sending a command, initialize by sending
2149 * a 32ms 18V to the switch
2151 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2152 dvb_frontend_sleep_until(&nexttime, 32000);
2154 for (i = 0; i < 9; i++) {
2155 if (dvb_frontend_debug)
2156 do_gettimeofday(&tv[i + 1]);
2157 if ((swcmd & 0x01) != last) {
2158 /* set voltage to (last ? 13V : 18V) */
2159 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2160 last = (last) ? 0 : 1;
2164 dvb_frontend_sleep_until(&nexttime, 8000);
2166 if (dvb_frontend_debug) {
2167 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2168 __func__, fe->dvb->num);
2169 for (i = 1; i < 10; i++)
2170 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2173 fepriv->state = FESTATE_DISEQC;
2178 case FE_DISEQC_RECV_SLAVE_REPLY:
2179 if (fe->ops.diseqc_recv_slave_reply)
2180 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2183 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2184 if (fe->ops.enable_high_lnb_voltage)
2185 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2188 case FE_SET_FRONTEND:
2189 err = set_delivery_system(fe, SYS_UNDEFINED);
2193 err = dtv_property_cache_sync(fe, c, parg);
2196 err = dtv_set_frontend(fe);
2199 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2202 case FE_GET_FRONTEND:
2203 err = dtv_get_frontend(fe, parg);
2206 case FE_SET_FRONTEND_TUNE_MODE:
2207 fepriv->tune_mode_flags = (unsigned long) parg;
2212 if (fe->dvb->fe_ioctl_override) {
2213 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2223 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2225 struct dvb_device *dvbdev = file->private_data;
2226 struct dvb_frontend *fe = dvbdev->priv;
2227 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2229 dprintk ("%s\n", __func__);
2231 poll_wait (file, &fepriv->events.wait_queue, wait);
2233 if (fepriv->events.eventw != fepriv->events.eventr)
2234 return (POLLIN | POLLRDNORM | POLLPRI);
2239 static int dvb_frontend_open(struct inode *inode, struct file *file)
2241 struct dvb_device *dvbdev = file->private_data;
2242 struct dvb_frontend *fe = dvbdev->priv;
2243 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2244 struct dvb_adapter *adapter = fe->dvb;
2247 dprintk ("%s\n", __func__);
2248 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2251 if (adapter->mfe_shared) {
2252 mutex_lock (&adapter->mfe_lock);
2254 if (adapter->mfe_dvbdev == NULL)
2255 adapter->mfe_dvbdev = dvbdev;
2257 else if (adapter->mfe_dvbdev != dvbdev) {
2259 *mfedev = adapter->mfe_dvbdev;
2261 *mfe = mfedev->priv;
2262 struct dvb_frontend_private
2263 *mfepriv = mfe->frontend_priv;
2264 int mferetry = (dvb_mfe_wait_time << 1);
2266 mutex_unlock (&adapter->mfe_lock);
2267 while (mferetry-- && (mfedev->users != -1 ||
2268 mfepriv->thread != NULL)) {
2269 if(msleep_interruptible(500)) {
2270 if(signal_pending(current))
2275 mutex_lock (&adapter->mfe_lock);
2276 if(adapter->mfe_dvbdev != dvbdev) {
2277 mfedev = adapter->mfe_dvbdev;
2279 mfepriv = mfe->frontend_priv;
2280 if (mfedev->users != -1 ||
2281 mfepriv->thread != NULL) {
2282 mutex_unlock (&adapter->mfe_lock);
2285 adapter->mfe_dvbdev = dvbdev;
2290 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2291 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2294 /* If we took control of the bus, we need to force
2295 reinitialization. This is because many ts_bus_ctrl()
2296 functions strobe the RESET pin on the demod, and if the
2297 frontend thread already exists then the dvb_init() routine
2298 won't get called (which is what usually does initial
2299 register configuration). */
2300 fepriv->reinitialise = 1;
2303 if ((ret = dvb_generic_open (inode, file)) < 0)
2306 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2307 /* normal tune mode when opened R/W */
2308 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2310 fepriv->voltage = -1;
2312 ret = dvb_frontend_start (fe);
2316 /* empty event queue */
2317 fepriv->events.eventr = fepriv->events.eventw = 0;
2320 if (adapter->mfe_shared)
2321 mutex_unlock (&adapter->mfe_lock);
2325 dvb_generic_release(inode, file);
2327 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2328 fe->ops.ts_bus_ctrl(fe, 0);
2330 if (adapter->mfe_shared)
2331 mutex_unlock (&adapter->mfe_lock);
2335 static int dvb_frontend_release(struct inode *inode, struct file *file)
2337 struct dvb_device *dvbdev = file->private_data;
2338 struct dvb_frontend *fe = dvbdev->priv;
2339 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2342 dprintk ("%s\n", __func__);
2344 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2345 fepriv->release_jiffies = jiffies;
2349 ret = dvb_generic_release (inode, file);
2351 if (dvbdev->users == -1) {
2352 wake_up(&fepriv->wait_queue);
2353 if (fepriv->exit != DVB_FE_NO_EXIT) {
2354 fops_put(file->f_op);
2356 wake_up(&dvbdev->wait_queue);
2358 if (fe->ops.ts_bus_ctrl)
2359 fe->ops.ts_bus_ctrl(fe, 0);
2365 static const struct file_operations dvb_frontend_fops = {
2366 .owner = THIS_MODULE,
2367 .unlocked_ioctl = dvb_generic_ioctl,
2368 .poll = dvb_frontend_poll,
2369 .open = dvb_frontend_open,
2370 .release = dvb_frontend_release,
2371 .llseek = noop_llseek,
2374 int dvb_register_frontend(struct dvb_adapter* dvb,
2375 struct dvb_frontend* fe)
2377 struct dvb_frontend_private *fepriv;
2378 static const struct dvb_device dvbdev_template = {
2382 .fops = &dvb_frontend_fops,
2383 .kernel_ioctl = dvb_frontend_ioctl
2386 dprintk ("%s\n", __func__);
2388 if (mutex_lock_interruptible(&frontend_mutex))
2389 return -ERESTARTSYS;
2391 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2392 if (fe->frontend_priv == NULL) {
2393 mutex_unlock(&frontend_mutex);
2396 fepriv = fe->frontend_priv;
2398 sema_init(&fepriv->sem, 1);
2399 init_waitqueue_head (&fepriv->wait_queue);
2400 init_waitqueue_head (&fepriv->events.wait_queue);
2401 mutex_init(&fepriv->events.mtx);
2403 fepriv->inversion = INVERSION_OFF;
2405 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2410 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2411 fe, DVB_DEVICE_FRONTEND);
2414 * Initialize the cache to the proper values according with the
2415 * first supported delivery system (ops->delsys[0])
2418 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2419 dvb_frontend_clear_cache(fe);
2421 mutex_unlock(&frontend_mutex);
2424 EXPORT_SYMBOL(dvb_register_frontend);
2426 int dvb_unregister_frontend(struct dvb_frontend* fe)
2428 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2429 dprintk ("%s\n", __func__);
2431 mutex_lock(&frontend_mutex);
2432 dvb_frontend_stop (fe);
2433 mutex_unlock(&frontend_mutex);
2435 if (fepriv->dvbdev->users < -1)
2436 wait_event(fepriv->dvbdev->wait_queue,
2437 fepriv->dvbdev->users==-1);
2439 mutex_lock(&frontend_mutex);
2440 dvb_unregister_device (fepriv->dvbdev);
2442 /* fe is invalid now */
2444 mutex_unlock(&frontend_mutex);
2447 EXPORT_SYMBOL(dvb_unregister_frontend);
2449 #ifdef CONFIG_MEDIA_ATTACH
2450 void dvb_frontend_detach(struct dvb_frontend* fe)
2454 if (fe->ops.release_sec) {
2455 fe->ops.release_sec(fe);
2456 symbol_put_addr(fe->ops.release_sec);
2458 if (fe->ops.tuner_ops.release) {
2459 fe->ops.tuner_ops.release(fe);
2460 symbol_put_addr(fe->ops.tuner_ops.release);
2462 if (fe->ops.analog_ops.release) {
2463 fe->ops.analog_ops.release(fe);
2464 symbol_put_addr(fe->ops.analog_ops.release);
2466 ptr = (void*)fe->ops.release;
2468 fe->ops.release(fe);
2469 symbol_put_addr(ptr);
2473 void dvb_frontend_detach(struct dvb_frontend* fe)
2475 if (fe->ops.release_sec)
2476 fe->ops.release_sec(fe);
2477 if (fe->ops.tuner_ops.release)
2478 fe->ops.tuner_ops.release(fe);
2479 if (fe->ops.analog_ops.release)
2480 fe->ops.analog_ops.release(fe);
2481 if (fe->ops.release)
2482 fe->ops.release(fe);
2485 EXPORT_SYMBOL(dvb_frontend_detach);