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);
147 static bool has_get_frontend(struct dvb_frontend *fe)
149 return fe->ops.get_frontend;
153 * Due to DVBv3 API calls, a delivery system should be mapped into one of
154 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
155 * otherwise, a DVBv3 call will fail.
157 enum dvbv3_emulation_type {
165 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
167 switch (delivery_system) {
168 case SYS_DVBC_ANNEX_A:
169 case SYS_DVBC_ANNEX_C:
183 case SYS_DVBC_ANNEX_B:
192 * Doesn't know how to emulate those types and/or
193 * there's no frontend driver from this type yet
194 * with some emulation code, so, we're not sure yet how
195 * to handle them, or they're not compatible with a DVBv3 call.
197 return DVBV3_UNKNOWN;
201 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
203 struct dvb_frontend_private *fepriv = fe->frontend_priv;
204 struct dvb_fe_events *events = &fepriv->events;
205 struct dvb_frontend_event *e;
208 dprintk ("%s\n", __func__);
210 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
211 dtv_get_frontend(fe, &fepriv->parameters_out);
213 mutex_lock(&events->mtx);
215 wp = (events->eventw + 1) % MAX_EVENT;
216 if (wp == events->eventr) {
217 events->overflow = 1;
218 events->eventr = (events->eventr + 1) % MAX_EVENT;
221 e = &events->events[events->eventw];
223 e->parameters = fepriv->parameters_out;
227 mutex_unlock(&events->mtx);
229 wake_up_interruptible (&events->wait_queue);
232 static int dvb_frontend_get_event(struct dvb_frontend *fe,
233 struct dvb_frontend_event *event, int flags)
235 struct dvb_frontend_private *fepriv = fe->frontend_priv;
236 struct dvb_fe_events *events = &fepriv->events;
238 dprintk ("%s\n", __func__);
240 if (events->overflow) {
241 events->overflow = 0;
245 if (events->eventw == events->eventr) {
248 if (flags & O_NONBLOCK)
253 ret = wait_event_interruptible (events->wait_queue,
254 events->eventw != events->eventr);
256 if (down_interruptible (&fepriv->sem))
263 mutex_lock(&events->mtx);
264 *event = events->events[events->eventr];
265 events->eventr = (events->eventr + 1) % MAX_EVENT;
266 mutex_unlock(&events->mtx);
271 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
273 struct dvb_frontend_private *fepriv = fe->frontend_priv;
274 struct dvb_fe_events *events = &fepriv->events;
276 mutex_lock(&events->mtx);
277 events->eventr = events->eventw;
278 mutex_unlock(&events->mtx);
281 static void dvb_frontend_init(struct dvb_frontend *fe)
283 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
290 if (fe->ops.tuner_ops.init) {
291 if (fe->ops.i2c_gate_ctrl)
292 fe->ops.i2c_gate_ctrl(fe, 1);
293 fe->ops.tuner_ops.init(fe);
294 if (fe->ops.i2c_gate_ctrl)
295 fe->ops.i2c_gate_ctrl(fe, 0);
299 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
301 struct dvb_frontend_private *fepriv = fe->frontend_priv;
303 fepriv->reinitialise = 1;
304 dvb_frontend_wakeup(fe);
306 EXPORT_SYMBOL(dvb_frontend_reinitialise);
308 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
312 dprintk ("%s\n", __func__);
315 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
317 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
319 q2 = fepriv->quality - 128;
322 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
326 * Performs automatic twiddling of frontend parameters.
328 * @param fe The frontend concerned.
329 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
330 * @returns Number of complete iterations that have been performed.
332 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
337 struct dvb_frontend_private *fepriv = fe->frontend_priv;
338 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
339 int original_inversion = c->inversion;
340 u32 original_frequency = c->frequency;
342 /* are we using autoinversion? */
343 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
344 (c->inversion == INVERSION_AUTO));
346 /* setup parameters correctly */
348 /* calculate the lnb_drift */
349 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
351 /* wrap the auto_step if we've exceeded the maximum drift */
352 if (fepriv->lnb_drift > fepriv->max_drift) {
353 fepriv->auto_step = 0;
354 fepriv->auto_sub_step = 0;
355 fepriv->lnb_drift = 0;
358 /* perform inversion and +/- zigzag */
359 switch(fepriv->auto_sub_step) {
361 /* try with the current inversion and current drift setting */
366 if (!autoinversion) break;
368 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
373 if (fepriv->lnb_drift == 0) break;
375 fepriv->lnb_drift = -fepriv->lnb_drift;
380 if (fepriv->lnb_drift == 0) break;
381 if (!autoinversion) break;
383 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
384 fepriv->lnb_drift = -fepriv->lnb_drift;
390 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
394 if (!ready) fepriv->auto_sub_step++;
397 /* if this attempt would hit where we started, indicate a complete
398 * iteration has occurred */
399 if ((fepriv->auto_step == fepriv->started_auto_step) &&
400 (fepriv->auto_sub_step == 0) && check_wrapped) {
404 dprintk("%s: drift:%i inversion:%i auto_step:%i "
405 "auto_sub_step:%i started_auto_step:%i\n",
406 __func__, fepriv->lnb_drift, fepriv->inversion,
407 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
409 /* set the frontend itself */
410 c->frequency += fepriv->lnb_drift;
412 c->inversion = fepriv->inversion;
414 if (fe->ops.set_frontend)
415 fe_set_err = fe->ops.set_frontend(fe);
417 if (fe_set_err < 0) {
418 fepriv->state = FESTATE_ERROR;
422 c->frequency = original_frequency;
423 c->inversion = original_inversion;
425 fepriv->auto_sub_step++;
429 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
433 struct dvb_frontend_private *fepriv = fe->frontend_priv;
434 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
436 /* if we've got no parameters, just keep idling */
437 if (fepriv->state & FESTATE_IDLE) {
438 fepriv->delay = 3*HZ;
443 /* in SCAN mode, we just set the frontend when asked and leave it alone */
444 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
445 if (fepriv->state & FESTATE_RETUNE) {
447 if (fe->ops.set_frontend)
448 retval = fe->ops.set_frontend(fe);
451 fepriv->state = FESTATE_ERROR;
453 fepriv->state = FESTATE_TUNED;
455 fepriv->delay = 3*HZ;
460 /* get the frontend status */
461 if (fepriv->state & FESTATE_RETUNE) {
464 if (fe->ops.read_status)
465 fe->ops.read_status(fe, &s);
466 if (s != fepriv->status) {
467 dvb_frontend_add_event(fe, s);
472 /* if we're not tuned, and we have a lock, move to the TUNED state */
473 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
474 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
475 fepriv->state = FESTATE_TUNED;
477 /* if we're tuned, then we have determined the correct inversion */
478 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
479 (c->inversion == INVERSION_AUTO)) {
480 c->inversion = fepriv->inversion;
485 /* if we are tuned already, check we're still locked */
486 if (fepriv->state & FESTATE_TUNED) {
487 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
489 /* we're tuned, and the lock is still good... */
490 if (s & FE_HAS_LOCK) {
492 } else { /* if we _WERE_ tuned, but now don't have a lock */
493 fepriv->state = FESTATE_ZIGZAG_FAST;
494 fepriv->started_auto_step = fepriv->auto_step;
495 fepriv->check_wrapped = 0;
499 /* don't actually do anything if we're in the LOSTLOCK state,
500 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
501 if ((fepriv->state & FESTATE_LOSTLOCK) &&
502 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
503 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
507 /* don't do anything if we're in the DISEQC state, since this
508 * might be someone with a motorized dish controlled by DISEQC.
509 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
510 if (fepriv->state & FESTATE_DISEQC) {
511 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
515 /* if we're in the RETUNE state, set everything up for a brand
516 * new scan, keeping the current inversion setting, as the next
517 * tune is _very_ likely to require the same */
518 if (fepriv->state & FESTATE_RETUNE) {
519 fepriv->lnb_drift = 0;
520 fepriv->auto_step = 0;
521 fepriv->auto_sub_step = 0;
522 fepriv->started_auto_step = 0;
523 fepriv->check_wrapped = 0;
527 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
528 fepriv->delay = fepriv->min_delay;
531 retval = dvb_frontend_swzigzag_autotune(fe,
532 fepriv->check_wrapped);
536 /* OK, if we've run out of trials at the fast speed.
537 * Drop back to slow for the _next_ attempt */
538 fepriv->state = FESTATE_SEARCHING_SLOW;
539 fepriv->started_auto_step = fepriv->auto_step;
542 fepriv->check_wrapped = 1;
544 /* if we've just retuned, enter the ZIGZAG_FAST state.
545 * This ensures we cannot return from an
546 * FE_SET_FRONTEND ioctl before the first frontend tune
548 if (fepriv->state & FESTATE_RETUNE) {
549 fepriv->state = FESTATE_TUNING_FAST;
554 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
555 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
557 /* Note: don't bother checking for wrapping; we stay in this
558 * state until we get a lock */
559 dvb_frontend_swzigzag_autotune(fe, 0);
563 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
565 struct dvb_frontend_private *fepriv = fe->frontend_priv;
567 if (fepriv->exit != DVB_FE_NO_EXIT)
570 if (fepriv->dvbdev->writers == 1)
571 if (time_after_eq(jiffies, fepriv->release_jiffies +
572 dvb_shutdown_timeout * HZ))
578 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
580 struct dvb_frontend_private *fepriv = fe->frontend_priv;
582 if (fepriv->wakeup) {
586 return dvb_frontend_is_exiting(fe);
589 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
591 struct dvb_frontend_private *fepriv = fe->frontend_priv;
594 wake_up_interruptible(&fepriv->wait_queue);
597 static int dvb_frontend_thread(void *data)
599 struct dvb_frontend *fe = data;
600 struct dvb_frontend_private *fepriv = fe->frontend_priv;
602 enum dvbfe_algo algo;
604 bool re_tune = false;
606 dprintk("%s\n", __func__);
608 fepriv->check_wrapped = 0;
610 fepriv->delay = 3*HZ;
613 fepriv->reinitialise = 0;
615 dvb_frontend_init(fe);
619 up(&fepriv->sem); /* is locked when we enter the thread... */
621 wait_event_interruptible_timeout(fepriv->wait_queue,
622 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
623 || freezing(current),
626 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
627 /* got signal or quitting */
628 fepriv->exit = DVB_FE_NORMAL_EXIT;
635 if (down_interruptible(&fepriv->sem))
638 if (fepriv->reinitialise) {
639 dvb_frontend_init(fe);
640 if (fe->ops.set_tone && fepriv->tone != -1)
641 fe->ops.set_tone(fe, fepriv->tone);
642 if (fe->ops.set_voltage && fepriv->voltage != -1)
643 fe->ops.set_voltage(fe, fepriv->voltage);
644 fepriv->reinitialise = 0;
647 /* do an iteration of the tuning loop */
648 if (fe->ops.get_frontend_algo) {
649 algo = fe->ops.get_frontend_algo(fe);
652 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
654 if (fepriv->state & FESTATE_RETUNE) {
655 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
657 fepriv->state = FESTATE_TUNED;
663 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
665 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
666 dprintk("%s: state changed, adding current state\n", __func__);
667 dvb_frontend_add_event(fe, s);
672 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
673 dvb_frontend_swzigzag(fe);
675 case DVBFE_ALGO_CUSTOM:
676 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
677 if (fepriv->state & FESTATE_RETUNE) {
678 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
679 fepriv->state = FESTATE_TUNED;
681 /* Case where we are going to search for a carrier
682 * User asked us to retune again for some reason, possibly
683 * requesting a search with a new set of parameters
685 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
686 if (fe->ops.search) {
687 fepriv->algo_status = fe->ops.search(fe);
688 /* We did do a search as was requested, the flags are
689 * now unset as well and has the flags wrt to search.
692 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
695 /* Track the carrier if the search was successful */
696 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
697 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
698 fepriv->delay = HZ / 2;
700 fe->ops.read_status(fe, &s);
701 if (s != fepriv->status) {
702 dvb_frontend_add_event(fe, s); /* update event list */
704 if (!(s & FE_HAS_LOCK)) {
705 fepriv->delay = HZ / 10;
706 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
708 fepriv->delay = 60 * HZ;
713 dprintk("%s: UNDEFINED ALGO !\n", __func__);
717 dvb_frontend_swzigzag(fe);
721 if (dvb_powerdown_on_sleep) {
722 if (fe->ops.set_voltage)
723 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
724 if (fe->ops.tuner_ops.sleep) {
725 if (fe->ops.i2c_gate_ctrl)
726 fe->ops.i2c_gate_ctrl(fe, 1);
727 fe->ops.tuner_ops.sleep(fe);
728 if (fe->ops.i2c_gate_ctrl)
729 fe->ops.i2c_gate_ctrl(fe, 0);
735 fepriv->thread = NULL;
736 if (kthread_should_stop())
737 fepriv->exit = DVB_FE_DEVICE_REMOVED;
739 fepriv->exit = DVB_FE_NO_EXIT;
742 dvb_frontend_wakeup(fe);
746 static void dvb_frontend_stop(struct dvb_frontend *fe)
748 struct dvb_frontend_private *fepriv = fe->frontend_priv;
750 dprintk ("%s\n", __func__);
752 fepriv->exit = DVB_FE_NORMAL_EXIT;
758 kthread_stop(fepriv->thread);
760 sema_init(&fepriv->sem, 1);
761 fepriv->state = FESTATE_IDLE;
763 /* paranoia check in case a signal arrived */
765 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
769 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
771 return ((curtime.tv_usec < lasttime.tv_usec) ?
772 1000000 - lasttime.tv_usec + curtime.tv_usec :
773 curtime.tv_usec - lasttime.tv_usec);
775 EXPORT_SYMBOL(timeval_usec_diff);
777 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
779 curtime->tv_usec += add_usec;
780 if (curtime->tv_usec >= 1000000) {
781 curtime->tv_usec -= 1000000;
787 * Sleep until gettimeofday() > waketime + add_usec
788 * This needs to be as precise as possible, but as the delay is
789 * usually between 2ms and 32ms, it is done using a scheduled msleep
790 * followed by usleep (normally a busy-wait loop) for the remainder
792 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
794 struct timeval lasttime;
797 timeval_usec_add(waketime, add_usec);
799 do_gettimeofday(&lasttime);
800 delta = timeval_usec_diff(lasttime, *waketime);
802 msleep((delta - 1500) / 1000);
803 do_gettimeofday(&lasttime);
804 newdelta = timeval_usec_diff(lasttime, *waketime);
805 delta = (newdelta > delta) ? 0 : newdelta;
810 EXPORT_SYMBOL(dvb_frontend_sleep_until);
812 static int dvb_frontend_start(struct dvb_frontend *fe)
815 struct dvb_frontend_private *fepriv = fe->frontend_priv;
816 struct task_struct *fe_thread;
818 dprintk ("%s\n", __func__);
820 if (fepriv->thread) {
821 if (fepriv->exit == DVB_FE_NO_EXIT)
824 dvb_frontend_stop (fe);
827 if (signal_pending(current))
829 if (down_interruptible (&fepriv->sem))
832 fepriv->state = FESTATE_IDLE;
833 fepriv->exit = DVB_FE_NO_EXIT;
834 fepriv->thread = NULL;
837 fe_thread = kthread_run(dvb_frontend_thread, fe,
838 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
839 if (IS_ERR(fe_thread)) {
840 ret = PTR_ERR(fe_thread);
841 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
845 fepriv->thread = fe_thread;
849 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
850 u32 *freq_min, u32 *freq_max)
852 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
854 if (fe->ops.info.frequency_max == 0)
855 *freq_max = fe->ops.tuner_ops.info.frequency_max;
856 else if (fe->ops.tuner_ops.info.frequency_max == 0)
857 *freq_max = fe->ops.info.frequency_max;
859 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
861 if (*freq_min == 0 || *freq_max == 0)
862 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
863 fe->dvb->num,fe->id);
866 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
868 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
872 /* range check: frequency */
873 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
874 if ((freq_min && c->frequency < freq_min) ||
875 (freq_max && c->frequency > freq_max)) {
876 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
877 fe->dvb->num, fe->id, c->frequency, freq_min, freq_max);
881 /* range check: symbol rate */
882 switch (c->delivery_system) {
886 case SYS_DVBC_ANNEX_A:
887 case SYS_DVBC_ANNEX_C:
888 if ((fe->ops.info.symbol_rate_min &&
889 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
890 (fe->ops.info.symbol_rate_max &&
891 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
892 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
893 fe->dvb->num, fe->id, c->symbol_rate,
894 fe->ops.info.symbol_rate_min,
895 fe->ops.info.symbol_rate_max);
905 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
907 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
911 delsys = c->delivery_system;
912 memset(c, 0, sizeof(struct dtv_frontend_properties));
913 c->delivery_system = delsys;
915 c->state = DTV_CLEAR;
917 dprintk("%s() Clearing cache for delivery system %d\n", __func__,
920 c->transmission_mode = TRANSMISSION_MODE_AUTO;
921 c->bandwidth_hz = 0; /* AUTO */
922 c->guard_interval = GUARD_INTERVAL_AUTO;
923 c->hierarchy = HIERARCHY_AUTO;
925 c->code_rate_HP = FEC_AUTO;
926 c->code_rate_LP = FEC_AUTO;
927 c->fec_inner = FEC_AUTO;
928 c->rolloff = ROLLOFF_AUTO;
929 c->voltage = SEC_VOLTAGE_OFF;
930 c->sectone = SEC_TONE_OFF;
931 c->pilot = PILOT_AUTO;
933 c->isdbt_partial_reception = 0;
934 c->isdbt_sb_mode = 0;
935 c->isdbt_sb_subchannel = 0;
936 c->isdbt_sb_segment_idx = 0;
937 c->isdbt_sb_segment_count = 0;
938 c->isdbt_layer_enabled = 0;
939 for (i = 0; i < 3; i++) {
940 c->layer[i].fec = FEC_AUTO;
941 c->layer[i].modulation = QAM_AUTO;
942 c->layer[i].interleaving = 0;
943 c->layer[i].segment_count = 0;
949 switch (c->delivery_system) {
953 c->modulation = QPSK; /* implied for DVB-S in legacy API */
954 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
957 c->modulation = VSB_8;
960 c->modulation = QAM_AUTO;
967 #define _DTV_CMD(n, s, b) \
975 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
976 _DTV_CMD(DTV_TUNE, 1, 0),
977 _DTV_CMD(DTV_CLEAR, 1, 0),
980 _DTV_CMD(DTV_FREQUENCY, 1, 0),
981 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
982 _DTV_CMD(DTV_MODULATION, 1, 0),
983 _DTV_CMD(DTV_INVERSION, 1, 0),
984 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
985 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
986 _DTV_CMD(DTV_INNER_FEC, 1, 0),
987 _DTV_CMD(DTV_VOLTAGE, 1, 0),
988 _DTV_CMD(DTV_TONE, 1, 0),
989 _DTV_CMD(DTV_PILOT, 1, 0),
990 _DTV_CMD(DTV_ROLLOFF, 1, 0),
991 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
992 _DTV_CMD(DTV_HIERARCHY, 1, 0),
993 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
994 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
995 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
996 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
998 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
999 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1000 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1001 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1002 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1003 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1004 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1005 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1006 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1007 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1014 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1017 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
1018 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
1021 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1022 _DTV_CMD(DTV_API_VERSION, 0, 0),
1023 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1024 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1025 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
1026 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
1027 _DTV_CMD(DTV_HIERARCHY, 0, 0),
1029 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1032 static void dtv_property_dump(struct dtv_property *tvp)
1036 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1037 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
1038 __func__, tvp->cmd);
1042 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1045 ,dtv_cmds[ tvp->cmd ].name);
1047 if(dtv_cmds[ tvp->cmd ].buffer) {
1049 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1051 ,tvp->u.buffer.len);
1053 for(i = 0; i < tvp->u.buffer.len; i++)
1054 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1057 ,tvp->u.buffer.data[i]);
1060 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1063 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1064 * drivers can use a single set_frontend tuning function, regardless of whether
1065 * it's being used for the legacy or new API, reducing code and complexity.
1067 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1068 struct dtv_frontend_properties *c,
1069 const struct dvb_frontend_parameters *p)
1071 c->frequency = p->frequency;
1072 c->inversion = p->inversion;
1074 switch (dvbv3_type(c->delivery_system)) {
1076 dprintk("%s() Preparing QPSK req\n", __func__);
1077 c->symbol_rate = p->u.qpsk.symbol_rate;
1078 c->fec_inner = p->u.qpsk.fec_inner;
1081 dprintk("%s() Preparing QAM req\n", __func__);
1082 c->symbol_rate = p->u.qam.symbol_rate;
1083 c->fec_inner = p->u.qam.fec_inner;
1084 c->modulation = p->u.qam.modulation;
1087 dprintk("%s() Preparing OFDM req\n", __func__);
1088 switch (p->u.ofdm.bandwidth) {
1089 case BANDWIDTH_10_MHZ:
1090 c->bandwidth_hz = 10000000;
1092 case BANDWIDTH_8_MHZ:
1093 c->bandwidth_hz = 8000000;
1095 case BANDWIDTH_7_MHZ:
1096 c->bandwidth_hz = 7000000;
1098 case BANDWIDTH_6_MHZ:
1099 c->bandwidth_hz = 6000000;
1101 case BANDWIDTH_5_MHZ:
1102 c->bandwidth_hz = 5000000;
1104 case BANDWIDTH_1_712_MHZ:
1105 c->bandwidth_hz = 1712000;
1107 case BANDWIDTH_AUTO:
1108 c->bandwidth_hz = 0;
1111 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1112 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1113 c->modulation = p->u.ofdm.constellation;
1114 c->transmission_mode = p->u.ofdm.transmission_mode;
1115 c->guard_interval = p->u.ofdm.guard_interval;
1116 c->hierarchy = p->u.ofdm.hierarchy_information;
1119 dprintk("%s() Preparing ATSC req\n", __func__);
1120 c->modulation = p->u.vsb.modulation;
1121 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1122 c->delivery_system = SYS_ATSC;
1124 c->delivery_system = SYS_DVBC_ANNEX_B;
1128 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1129 __func__, c->delivery_system);
1136 /* Ensure the cached values are set correctly in the frontend
1137 * legacy tuning structures, for the advanced tuning API.
1139 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1140 struct dvb_frontend_parameters *p)
1142 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1144 p->frequency = c->frequency;
1145 p->inversion = c->inversion;
1147 switch (dvbv3_type(c->delivery_system)) {
1150 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1151 __func__, c->delivery_system);
1154 dprintk("%s() Preparing QPSK req\n", __func__);
1155 p->u.qpsk.symbol_rate = c->symbol_rate;
1156 p->u.qpsk.fec_inner = c->fec_inner;
1159 dprintk("%s() Preparing QAM req\n", __func__);
1160 p->u.qam.symbol_rate = c->symbol_rate;
1161 p->u.qam.fec_inner = c->fec_inner;
1162 p->u.qam.modulation = c->modulation;
1165 dprintk("%s() Preparing OFDM req\n", __func__);
1167 switch (c->bandwidth_hz) {
1169 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1172 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1175 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1178 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1181 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1184 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1188 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1190 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1191 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1192 p->u.ofdm.constellation = c->modulation;
1193 p->u.ofdm.transmission_mode = c->transmission_mode;
1194 p->u.ofdm.guard_interval = c->guard_interval;
1195 p->u.ofdm.hierarchy_information = c->hierarchy;
1198 dprintk("%s() Preparing VSB req\n", __func__);
1199 p->u.vsb.modulation = c->modulation;
1206 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1207 * @fe: struct dvb_frontend pointer
1208 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1209 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1211 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1212 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1213 * If p_out is not null, it will update the DVBv3 params pointed by it.
1215 static int dtv_get_frontend(struct dvb_frontend *fe,
1216 struct dvb_frontend_parameters *p_out)
1220 if (fe->ops.get_frontend) {
1221 r = fe->ops.get_frontend(fe);
1222 if (unlikely(r < 0))
1225 dtv_property_legacy_params_sync(fe, p_out);
1229 /* As everything is in cache, get_frontend fops are always supported */
1233 static int dvb_frontend_ioctl_legacy(struct file *file,
1234 unsigned int cmd, void *parg);
1235 static int dvb_frontend_ioctl_properties(struct file *file,
1236 unsigned int cmd, void *parg);
1238 static int dtv_property_process_get(struct dvb_frontend *fe,
1239 const struct dtv_frontend_properties *c,
1240 struct dtv_property *tvp,
1246 case DTV_ENUM_DELSYS:
1248 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1249 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1252 tvp->u.buffer.len = ncaps;
1255 tvp->u.data = c->frequency;
1257 case DTV_MODULATION:
1258 tvp->u.data = c->modulation;
1260 case DTV_BANDWIDTH_HZ:
1261 tvp->u.data = c->bandwidth_hz;
1264 tvp->u.data = c->inversion;
1266 case DTV_SYMBOL_RATE:
1267 tvp->u.data = c->symbol_rate;
1270 tvp->u.data = c->fec_inner;
1273 tvp->u.data = c->pilot;
1276 tvp->u.data = c->rolloff;
1278 case DTV_DELIVERY_SYSTEM:
1279 tvp->u.data = c->delivery_system;
1282 tvp->u.data = c->voltage;
1285 tvp->u.data = c->sectone;
1287 case DTV_API_VERSION:
1288 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1290 case DTV_CODE_RATE_HP:
1291 tvp->u.data = c->code_rate_HP;
1293 case DTV_CODE_RATE_LP:
1294 tvp->u.data = c->code_rate_LP;
1296 case DTV_GUARD_INTERVAL:
1297 tvp->u.data = c->guard_interval;
1299 case DTV_TRANSMISSION_MODE:
1300 tvp->u.data = c->transmission_mode;
1303 tvp->u.data = c->hierarchy;
1306 /* ISDB-T Support here */
1307 case DTV_ISDBT_PARTIAL_RECEPTION:
1308 tvp->u.data = c->isdbt_partial_reception;
1310 case DTV_ISDBT_SOUND_BROADCASTING:
1311 tvp->u.data = c->isdbt_sb_mode;
1313 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1314 tvp->u.data = c->isdbt_sb_subchannel;
1316 case DTV_ISDBT_SB_SEGMENT_IDX:
1317 tvp->u.data = c->isdbt_sb_segment_idx;
1319 case DTV_ISDBT_SB_SEGMENT_COUNT:
1320 tvp->u.data = c->isdbt_sb_segment_count;
1322 case DTV_ISDBT_LAYER_ENABLED:
1323 tvp->u.data = c->isdbt_layer_enabled;
1325 case DTV_ISDBT_LAYERA_FEC:
1326 tvp->u.data = c->layer[0].fec;
1328 case DTV_ISDBT_LAYERA_MODULATION:
1329 tvp->u.data = c->layer[0].modulation;
1331 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1332 tvp->u.data = c->layer[0].segment_count;
1334 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1335 tvp->u.data = c->layer[0].interleaving;
1337 case DTV_ISDBT_LAYERB_FEC:
1338 tvp->u.data = c->layer[1].fec;
1340 case DTV_ISDBT_LAYERB_MODULATION:
1341 tvp->u.data = c->layer[1].modulation;
1343 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1344 tvp->u.data = c->layer[1].segment_count;
1346 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1347 tvp->u.data = c->layer[1].interleaving;
1349 case DTV_ISDBT_LAYERC_FEC:
1350 tvp->u.data = c->layer[2].fec;
1352 case DTV_ISDBT_LAYERC_MODULATION:
1353 tvp->u.data = c->layer[2].modulation;
1355 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1356 tvp->u.data = c->layer[2].segment_count;
1358 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1359 tvp->u.data = c->layer[2].interleaving;
1361 case DTV_ISDBS_TS_ID:
1362 tvp->u.data = c->isdbs_ts_id;
1364 case DTV_DVBT2_PLP_ID:
1365 tvp->u.data = c->dvbt2_plp_id;
1371 /* Allow the frontend to override outgoing properties */
1372 if (fe->ops.get_property) {
1373 r = fe->ops.get_property(fe, tvp);
1378 dtv_property_dump(tvp);
1383 static int dtv_set_frontend(struct dvb_frontend *fe);
1385 static bool is_dvbv3_delsys(u32 delsys)
1389 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1390 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1395 static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1398 u32 delsys = SYS_UNDEFINED;
1399 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1400 enum dvbv3_emulation_type type;
1403 * It was reported that some old DVBv5 applications were
1404 * filling delivery_system with SYS_UNDEFINED. If this happens,
1405 * assume that the application wants to use the first supported
1408 if (c->delivery_system == SYS_UNDEFINED)
1409 c->delivery_system = fe->ops.delsys[0];
1411 if (desired_system == SYS_UNDEFINED) {
1413 * A DVBv3 call doesn't know what's the desired system.
1414 * Also, DVBv3 applications don't know that ops.info->type
1415 * could be changed, and they simply dies when it doesn't
1417 * So, don't change the current delivery system, as it
1418 * may be trying to do the wrong thing, like setting an
1419 * ISDB-T frontend as DVB-T. Instead, find the closest
1420 * DVBv3 system that matches the delivery system.
1422 if (is_dvbv3_delsys(c->delivery_system)) {
1423 dprintk("%s() Using delivery system to %d\n",
1424 __func__, c->delivery_system);
1427 type = dvbv3_type(c->delivery_system);
1430 desired_system = SYS_DVBS;
1433 desired_system = SYS_DVBC_ANNEX_A;
1436 desired_system = SYS_ATSC;
1439 desired_system = SYS_DVBT;
1442 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1448 * This is a DVBv5 call. So, it likely knows the supported
1452 /* Check if the desired delivery system is supported */
1454 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1455 if (fe->ops.delsys[ncaps] == desired_system) {
1456 c->delivery_system = desired_system;
1457 dprintk("%s() Changing delivery system to %d\n",
1458 __func__, desired_system);
1463 type = dvbv3_type(desired_system);
1466 * The delivery system is not supported. See if it can be
1468 * The emulation only works if the desired system is one of the
1469 * DVBv3 delivery systems
1471 if (!is_dvbv3_delsys(desired_system)) {
1472 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1478 * Get the last non-DVBv3 delivery system that has the same type
1479 * of the desired system
1482 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1483 if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1484 !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1485 delsys = fe->ops.delsys[ncaps];
1488 /* There's nothing compatible with the desired delivery system */
1489 if (delsys == SYS_UNDEFINED) {
1490 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1494 c->delivery_system = delsys;
1498 * The DVBv3 or DVBv5 call is requesting a different system. So,
1499 * emulation is needed.
1501 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1502 * for older DVBv5 applications. The emulation will try to use
1503 * the auto mode for most things, and will assume that the desired
1504 * delivery system is the last one at the ops.delsys[] array
1506 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1507 __func__, delsys, desired_system);
1510 * For now, handles ISDB-T calls. More code may be needed here for the
1511 * other emulated stuff
1513 if (type == DVBV3_OFDM) {
1514 if (c->delivery_system == SYS_ISDBT) {
1515 dprintk("%s() Using defaults for SYS_ISDBT\n",
1517 if (!c->bandwidth_hz)
1518 c->bandwidth_hz = 6000000;
1520 c->isdbt_partial_reception = 0;
1521 c->isdbt_sb_mode = 0;
1522 c->isdbt_sb_subchannel = 0;
1523 c->isdbt_sb_segment_idx = 0;
1524 c->isdbt_sb_segment_count = 0;
1525 c->isdbt_layer_enabled = 0;
1526 for (i = 0; i < 3; i++) {
1527 c->layer[i].fec = FEC_AUTO;
1528 c->layer[i].modulation = QAM_AUTO;
1529 c->layer[i].interleaving = 0;
1530 c->layer[i].segment_count = 0;
1534 dprintk("change delivery system on cache to %d\n", c->delivery_system);
1539 static int dtv_property_process_set(struct dvb_frontend *fe,
1540 struct dtv_property *tvp,
1544 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1546 /* Allow the frontend to validate incoming properties */
1547 if (fe->ops.set_property) {
1548 r = fe->ops.set_property(fe, tvp);
1556 * Reset a cache of data specific to the frontend here. This does
1557 * not effect hardware.
1559 dvb_frontend_clear_cache(fe);
1562 /* interpret the cache of data, build either a traditional frontend
1563 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1566 c->state = tvp->cmd;
1567 dprintk("%s() Finalised property cache\n", __func__);
1569 r = dtv_set_frontend(fe);
1572 c->frequency = tvp->u.data;
1574 case DTV_MODULATION:
1575 c->modulation = tvp->u.data;
1577 case DTV_BANDWIDTH_HZ:
1578 c->bandwidth_hz = tvp->u.data;
1581 c->inversion = tvp->u.data;
1583 case DTV_SYMBOL_RATE:
1584 c->symbol_rate = tvp->u.data;
1587 c->fec_inner = tvp->u.data;
1590 c->pilot = tvp->u.data;
1593 c->rolloff = tvp->u.data;
1595 case DTV_DELIVERY_SYSTEM:
1596 r = set_delivery_system(fe, tvp->u.data);
1599 c->voltage = tvp->u.data;
1600 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1601 (void *)c->voltage);
1604 c->sectone = tvp->u.data;
1605 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1606 (void *)c->sectone);
1608 case DTV_CODE_RATE_HP:
1609 c->code_rate_HP = tvp->u.data;
1611 case DTV_CODE_RATE_LP:
1612 c->code_rate_LP = tvp->u.data;
1614 case DTV_GUARD_INTERVAL:
1615 c->guard_interval = tvp->u.data;
1617 case DTV_TRANSMISSION_MODE:
1618 c->transmission_mode = tvp->u.data;
1621 c->hierarchy = tvp->u.data;
1624 /* ISDB-T Support here */
1625 case DTV_ISDBT_PARTIAL_RECEPTION:
1626 c->isdbt_partial_reception = tvp->u.data;
1628 case DTV_ISDBT_SOUND_BROADCASTING:
1629 c->isdbt_sb_mode = tvp->u.data;
1631 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1632 c->isdbt_sb_subchannel = tvp->u.data;
1634 case DTV_ISDBT_SB_SEGMENT_IDX:
1635 c->isdbt_sb_segment_idx = tvp->u.data;
1637 case DTV_ISDBT_SB_SEGMENT_COUNT:
1638 c->isdbt_sb_segment_count = tvp->u.data;
1640 case DTV_ISDBT_LAYER_ENABLED:
1641 c->isdbt_layer_enabled = tvp->u.data;
1643 case DTV_ISDBT_LAYERA_FEC:
1644 c->layer[0].fec = tvp->u.data;
1646 case DTV_ISDBT_LAYERA_MODULATION:
1647 c->layer[0].modulation = tvp->u.data;
1649 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1650 c->layer[0].segment_count = tvp->u.data;
1652 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1653 c->layer[0].interleaving = tvp->u.data;
1655 case DTV_ISDBT_LAYERB_FEC:
1656 c->layer[1].fec = tvp->u.data;
1658 case DTV_ISDBT_LAYERB_MODULATION:
1659 c->layer[1].modulation = tvp->u.data;
1661 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1662 c->layer[1].segment_count = tvp->u.data;
1664 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1665 c->layer[1].interleaving = tvp->u.data;
1667 case DTV_ISDBT_LAYERC_FEC:
1668 c->layer[2].fec = tvp->u.data;
1670 case DTV_ISDBT_LAYERC_MODULATION:
1671 c->layer[2].modulation = tvp->u.data;
1673 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1674 c->layer[2].segment_count = tvp->u.data;
1676 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1677 c->layer[2].interleaving = tvp->u.data;
1679 case DTV_ISDBS_TS_ID:
1680 c->isdbs_ts_id = tvp->u.data;
1682 case DTV_DVBT2_PLP_ID:
1683 c->dvbt2_plp_id = tvp->u.data;
1692 static int dvb_frontend_ioctl(struct file *file,
1693 unsigned int cmd, void *parg)
1695 struct dvb_device *dvbdev = file->private_data;
1696 struct dvb_frontend *fe = dvbdev->priv;
1697 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1698 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1699 int err = -EOPNOTSUPP;
1701 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1703 if (fepriv->exit != DVB_FE_NO_EXIT)
1706 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1707 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1708 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1711 if (down_interruptible (&fepriv->sem))
1712 return -ERESTARTSYS;
1714 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1715 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1717 c->state = DTV_UNDEFINED;
1718 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1725 static int dvb_frontend_ioctl_properties(struct file *file,
1726 unsigned int cmd, void *parg)
1728 struct dvb_device *dvbdev = file->private_data;
1729 struct dvb_frontend *fe = dvbdev->priv;
1730 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1731 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1734 struct dtv_properties *tvps = NULL;
1735 struct dtv_property *tvp = NULL;
1738 dprintk("%s\n", __func__);
1740 if(cmd == FE_SET_PROPERTY) {
1741 tvps = (struct dtv_properties __user *)parg;
1743 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1744 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1746 /* Put an arbitrary limit on the number of messages that can
1747 * be sent at once */
1748 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1751 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1757 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1762 for (i = 0; i < tvps->num; i++) {
1763 err = dtv_property_process_set(fe, tvp + i, file);
1766 (tvp + i)->result = err;
1769 if (c->state == DTV_TUNE)
1770 dprintk("%s() Property cache is full, tuning\n", __func__);
1773 if(cmd == FE_GET_PROPERTY) {
1774 tvps = (struct dtv_properties __user *)parg;
1776 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1777 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1779 /* Put an arbitrary limit on the number of messages that can
1780 * be sent at once */
1781 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1784 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1790 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1796 * Fills the cache out struct with the cache contents, plus
1797 * the data retrieved from get_frontend, if the frontend
1798 * is not idle. Otherwise, returns the cached content
1800 if (fepriv->state != FESTATE_IDLE) {
1801 err = dtv_get_frontend(fe, NULL);
1805 for (i = 0; i < tvps->num; i++) {
1806 err = dtv_property_process_get(fe, c, tvp + i, file);
1809 (tvp + i)->result = err;
1812 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1825 static int dtv_set_frontend(struct dvb_frontend *fe)
1827 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1828 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1829 struct dvb_frontend_tune_settings fetunesettings;
1832 if (dvb_frontend_check_parameters(fe) < 0)
1836 * Be sure that the bandwidth will be filled for all
1837 * non-satellite systems, as tuners need to know what
1838 * low pass/Nyquist half filter should be applied, in
1839 * order to avoid inter-channel noise.
1841 * ISDB-T and DVB-T/T2 already sets bandwidth.
1842 * ATSC and DVB-C don't set, so, the core should fill it.
1844 * On DVB-C Annex A and C, the bandwidth is a function of
1845 * the roll-off and symbol rate. Annex B defines different
1846 * roll-off factors depending on the modulation. Fortunately,
1847 * Annex B is only used with 6MHz, so there's no need to
1850 * While not officially supported, a side effect of handling it at
1851 * the cache level is that a program could retrieve the bandwidth
1852 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1854 switch (c->delivery_system) {
1856 case SYS_DVBC_ANNEX_B:
1857 c->bandwidth_hz = 6000000;
1859 case SYS_DVBC_ANNEX_A:
1862 case SYS_DVBC_ANNEX_C:
1869 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
1871 /* force auto frequency inversion if requested */
1872 if (dvb_force_auto_inversion)
1873 c->inversion = INVERSION_AUTO;
1876 * without hierarchical coding code_rate_LP is irrelevant,
1877 * so we tolerate the otherwise invalid FEC_NONE setting
1879 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1880 c->code_rate_LP = FEC_AUTO;
1882 /* get frontend-specific tuning settings */
1883 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1884 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1885 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1886 fepriv->max_drift = fetunesettings.max_drift;
1887 fepriv->step_size = fetunesettings.step_size;
1889 /* default values */
1890 switch (c->delivery_system) {
1891 case SYS_DVBC_ANNEX_A:
1892 case SYS_DVBC_ANNEX_C:
1893 fepriv->min_delay = HZ / 20;
1894 fepriv->step_size = c->symbol_rate / 16000;
1895 fepriv->max_drift = c->symbol_rate / 2000;
1901 fepriv->min_delay = HZ / 20;
1902 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1903 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1907 * FIXME: This sounds wrong! if freqency_stepsize is
1908 * defined by the frontend, why not use it???
1910 fepriv->min_delay = HZ / 20;
1911 fepriv->step_size = 0; /* no zigzag */
1912 fepriv->max_drift = 0;
1916 if (dvb_override_tune_delay > 0)
1917 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1919 fepriv->state = FESTATE_RETUNE;
1921 /* Request the search algorithm to search */
1922 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1924 dvb_frontend_clear_events(fe);
1925 dvb_frontend_add_event(fe, 0);
1926 dvb_frontend_wakeup(fe);
1933 static int dvb_frontend_ioctl_legacy(struct file *file,
1934 unsigned int cmd, void *parg)
1936 struct dvb_device *dvbdev = file->private_data;
1937 struct dvb_frontend *fe = dvbdev->priv;
1938 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1939 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1940 int cb_err, err = -EOPNOTSUPP;
1942 if (fe->dvb->fe_ioctl_override) {
1943 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1949 /* fe_ioctl_override returning 0 allows
1950 * dvb-core to continue handling the ioctl */
1955 struct dvb_frontend_info* info = parg;
1957 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1958 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1961 * Associate the 4 delivery systems supported by DVBv3
1962 * API with their DVBv5 counterpart. For the other standards,
1963 * use the closest type, assuming that it would hopefully
1964 * work with a DVBv3 application.
1965 * It should be noticed that, on multi-frontend devices with
1966 * different types (terrestrial and cable, for example),
1967 * a pure DVBv3 application won't be able to use all delivery
1968 * systems. Yet, changing the DVBv5 cache to the other delivery
1969 * system should be enough for making it work.
1971 switch (dvbv3_type(c->delivery_system)) {
1973 info->type = FE_QPSK;
1976 info->type = FE_ATSC;
1979 info->type = FE_QAM;
1982 info->type = FE_OFDM;
1986 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1987 __func__, c->delivery_system);
1988 fe->ops.info.type = FE_OFDM;
1990 dprintk("current delivery system on cache: %d, V3 type: %d\n",
1991 c->delivery_system, fe->ops.info.type);
1993 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1994 * do it, it is done for it. */
1995 info->caps |= FE_CAN_INVERSION_AUTO;
2000 case FE_READ_STATUS: {
2001 fe_status_t* status = parg;
2003 /* if retune was requested but hasn't occurred yet, prevent
2004 * that user get signal state from previous tuning */
2005 if (fepriv->state == FESTATE_RETUNE ||
2006 fepriv->state == FESTATE_ERROR) {
2012 if (fe->ops.read_status)
2013 err = fe->ops.read_status(fe, status);
2017 if (fe->ops.read_ber)
2018 err = fe->ops.read_ber(fe, (__u32*) parg);
2021 case FE_READ_SIGNAL_STRENGTH:
2022 if (fe->ops.read_signal_strength)
2023 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
2027 if (fe->ops.read_snr)
2028 err = fe->ops.read_snr(fe, (__u16*) parg);
2031 case FE_READ_UNCORRECTED_BLOCKS:
2032 if (fe->ops.read_ucblocks)
2033 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
2037 case FE_DISEQC_RESET_OVERLOAD:
2038 if (fe->ops.diseqc_reset_overload) {
2039 err = fe->ops.diseqc_reset_overload(fe);
2040 fepriv->state = FESTATE_DISEQC;
2045 case FE_DISEQC_SEND_MASTER_CMD:
2046 if (fe->ops.diseqc_send_master_cmd) {
2047 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2048 fepriv->state = FESTATE_DISEQC;
2053 case FE_DISEQC_SEND_BURST:
2054 if (fe->ops.diseqc_send_burst) {
2055 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2056 fepriv->state = FESTATE_DISEQC;
2062 if (fe->ops.set_tone) {
2063 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2064 fepriv->tone = (fe_sec_tone_mode_t) parg;
2065 fepriv->state = FESTATE_DISEQC;
2070 case FE_SET_VOLTAGE:
2071 if (fe->ops.set_voltage) {
2072 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2073 fepriv->voltage = (fe_sec_voltage_t) parg;
2074 fepriv->state = FESTATE_DISEQC;
2079 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2080 if (fe->ops.dishnetwork_send_legacy_command) {
2081 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2082 fepriv->state = FESTATE_DISEQC;
2084 } else if (fe->ops.set_voltage) {
2086 * NOTE: This is a fallback condition. Some frontends
2087 * (stv0299 for instance) take longer than 8msec to
2088 * respond to a set_voltage command. Those switches
2089 * need custom routines to switch properly. For all
2090 * other frontends, the following should work ok.
2091 * Dish network legacy switches (as used by Dish500)
2092 * are controlled by sending 9-bit command words
2093 * spaced 8msec apart.
2094 * the actual command word is switch/port dependent
2095 * so it is up to the userspace application to send
2096 * the right command.
2097 * The command must always start with a '0' after
2098 * initialization, so parg is 8 bits and does not
2099 * include the initialization or start bit
2101 unsigned long swcmd = ((unsigned long) parg) << 1;
2102 struct timeval nexttime;
2103 struct timeval tv[10];
2106 if (dvb_frontend_debug)
2107 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2108 do_gettimeofday(&nexttime);
2109 if (dvb_frontend_debug)
2110 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2111 /* before sending a command, initialize by sending
2112 * a 32ms 18V to the switch
2114 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2115 dvb_frontend_sleep_until(&nexttime, 32000);
2117 for (i = 0; i < 9; i++) {
2118 if (dvb_frontend_debug)
2119 do_gettimeofday(&tv[i + 1]);
2120 if ((swcmd & 0x01) != last) {
2121 /* set voltage to (last ? 13V : 18V) */
2122 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2123 last = (last) ? 0 : 1;
2127 dvb_frontend_sleep_until(&nexttime, 8000);
2129 if (dvb_frontend_debug) {
2130 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2131 __func__, fe->dvb->num);
2132 for (i = 1; i < 10; i++)
2133 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2136 fepriv->state = FESTATE_DISEQC;
2141 case FE_DISEQC_RECV_SLAVE_REPLY:
2142 if (fe->ops.diseqc_recv_slave_reply)
2143 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2146 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2147 if (fe->ops.enable_high_lnb_voltage)
2148 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2151 case FE_SET_FRONTEND:
2152 err = set_delivery_system(fe, SYS_UNDEFINED);
2156 err = dtv_property_cache_sync(fe, c, parg);
2159 err = dtv_set_frontend(fe);
2162 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2165 case FE_GET_FRONTEND:
2166 err = dtv_get_frontend(fe, parg);
2169 case FE_SET_FRONTEND_TUNE_MODE:
2170 fepriv->tune_mode_flags = (unsigned long) parg;
2175 if (fe->dvb->fe_ioctl_override) {
2176 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2186 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2188 struct dvb_device *dvbdev = file->private_data;
2189 struct dvb_frontend *fe = dvbdev->priv;
2190 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2192 dprintk ("%s\n", __func__);
2194 poll_wait (file, &fepriv->events.wait_queue, wait);
2196 if (fepriv->events.eventw != fepriv->events.eventr)
2197 return (POLLIN | POLLRDNORM | POLLPRI);
2202 static int dvb_frontend_open(struct inode *inode, struct file *file)
2204 struct dvb_device *dvbdev = file->private_data;
2205 struct dvb_frontend *fe = dvbdev->priv;
2206 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2207 struct dvb_adapter *adapter = fe->dvb;
2210 dprintk ("%s\n", __func__);
2211 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2214 if (adapter->mfe_shared) {
2215 mutex_lock (&adapter->mfe_lock);
2217 if (adapter->mfe_dvbdev == NULL)
2218 adapter->mfe_dvbdev = dvbdev;
2220 else if (adapter->mfe_dvbdev != dvbdev) {
2222 *mfedev = adapter->mfe_dvbdev;
2224 *mfe = mfedev->priv;
2225 struct dvb_frontend_private
2226 *mfepriv = mfe->frontend_priv;
2227 int mferetry = (dvb_mfe_wait_time << 1);
2229 mutex_unlock (&adapter->mfe_lock);
2230 while (mferetry-- && (mfedev->users != -1 ||
2231 mfepriv->thread != NULL)) {
2232 if(msleep_interruptible(500)) {
2233 if(signal_pending(current))
2238 mutex_lock (&adapter->mfe_lock);
2239 if(adapter->mfe_dvbdev != dvbdev) {
2240 mfedev = adapter->mfe_dvbdev;
2242 mfepriv = mfe->frontend_priv;
2243 if (mfedev->users != -1 ||
2244 mfepriv->thread != NULL) {
2245 mutex_unlock (&adapter->mfe_lock);
2248 adapter->mfe_dvbdev = dvbdev;
2253 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2254 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2257 /* If we took control of the bus, we need to force
2258 reinitialization. This is because many ts_bus_ctrl()
2259 functions strobe the RESET pin on the demod, and if the
2260 frontend thread already exists then the dvb_init() routine
2261 won't get called (which is what usually does initial
2262 register configuration). */
2263 fepriv->reinitialise = 1;
2266 if ((ret = dvb_generic_open (inode, file)) < 0)
2269 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2270 /* normal tune mode when opened R/W */
2271 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2273 fepriv->voltage = -1;
2275 ret = dvb_frontend_start (fe);
2279 /* empty event queue */
2280 fepriv->events.eventr = fepriv->events.eventw = 0;
2283 if (adapter->mfe_shared)
2284 mutex_unlock (&adapter->mfe_lock);
2288 dvb_generic_release(inode, file);
2290 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2291 fe->ops.ts_bus_ctrl(fe, 0);
2293 if (adapter->mfe_shared)
2294 mutex_unlock (&adapter->mfe_lock);
2298 static int dvb_frontend_release(struct inode *inode, struct file *file)
2300 struct dvb_device *dvbdev = file->private_data;
2301 struct dvb_frontend *fe = dvbdev->priv;
2302 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2305 dprintk ("%s\n", __func__);
2307 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2308 fepriv->release_jiffies = jiffies;
2312 ret = dvb_generic_release (inode, file);
2314 if (dvbdev->users == -1) {
2315 wake_up(&fepriv->wait_queue);
2316 if (fepriv->exit != DVB_FE_NO_EXIT) {
2317 fops_put(file->f_op);
2319 wake_up(&dvbdev->wait_queue);
2321 if (fe->ops.ts_bus_ctrl)
2322 fe->ops.ts_bus_ctrl(fe, 0);
2328 static const struct file_operations dvb_frontend_fops = {
2329 .owner = THIS_MODULE,
2330 .unlocked_ioctl = dvb_generic_ioctl,
2331 .poll = dvb_frontend_poll,
2332 .open = dvb_frontend_open,
2333 .release = dvb_frontend_release,
2334 .llseek = noop_llseek,
2337 int dvb_register_frontend(struct dvb_adapter* dvb,
2338 struct dvb_frontend* fe)
2340 struct dvb_frontend_private *fepriv;
2341 static const struct dvb_device dvbdev_template = {
2345 .fops = &dvb_frontend_fops,
2346 .kernel_ioctl = dvb_frontend_ioctl
2349 dprintk ("%s\n", __func__);
2351 if (mutex_lock_interruptible(&frontend_mutex))
2352 return -ERESTARTSYS;
2354 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2355 if (fe->frontend_priv == NULL) {
2356 mutex_unlock(&frontend_mutex);
2359 fepriv = fe->frontend_priv;
2361 sema_init(&fepriv->sem, 1);
2362 init_waitqueue_head (&fepriv->wait_queue);
2363 init_waitqueue_head (&fepriv->events.wait_queue);
2364 mutex_init(&fepriv->events.mtx);
2366 fepriv->inversion = INVERSION_OFF;
2368 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2373 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2374 fe, DVB_DEVICE_FRONTEND);
2377 * Initialize the cache to the proper values according with the
2378 * first supported delivery system (ops->delsys[0])
2381 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2382 dvb_frontend_clear_cache(fe);
2384 mutex_unlock(&frontend_mutex);
2387 EXPORT_SYMBOL(dvb_register_frontend);
2389 int dvb_unregister_frontend(struct dvb_frontend* fe)
2391 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2392 dprintk ("%s\n", __func__);
2394 mutex_lock(&frontend_mutex);
2395 dvb_frontend_stop (fe);
2396 mutex_unlock(&frontend_mutex);
2398 if (fepriv->dvbdev->users < -1)
2399 wait_event(fepriv->dvbdev->wait_queue,
2400 fepriv->dvbdev->users==-1);
2402 mutex_lock(&frontend_mutex);
2403 dvb_unregister_device (fepriv->dvbdev);
2405 /* fe is invalid now */
2407 mutex_unlock(&frontend_mutex);
2410 EXPORT_SYMBOL(dvb_unregister_frontend);
2412 #ifdef CONFIG_MEDIA_ATTACH
2413 void dvb_frontend_detach(struct dvb_frontend* fe)
2417 if (fe->ops.release_sec) {
2418 fe->ops.release_sec(fe);
2419 symbol_put_addr(fe->ops.release_sec);
2421 if (fe->ops.tuner_ops.release) {
2422 fe->ops.tuner_ops.release(fe);
2423 symbol_put_addr(fe->ops.tuner_ops.release);
2425 if (fe->ops.analog_ops.release) {
2426 fe->ops.analog_ops.release(fe);
2427 symbol_put_addr(fe->ops.analog_ops.release);
2429 ptr = (void*)fe->ops.release;
2431 fe->ops.release(fe);
2432 symbol_put_addr(ptr);
2436 void dvb_frontend_detach(struct dvb_frontend* fe)
2438 if (fe->ops.release_sec)
2439 fe->ops.release_sec(fe);
2440 if (fe->ops.tuner_ops.release)
2441 fe->ops.tuner_ops.release(fe);
2442 if (fe->ops.analog_ops.release)
2443 fe->ops.analog_ops.release(fe);
2444 if (fe->ops.release)
2445 fe->ops.release(fe);
2448 EXPORT_SYMBOL(dvb_frontend_detach);