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 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/list.h>
37 #include <linux/freezer.h>
38 #include <linux/jiffies.h>
39 #include <asm/processor.h>
41 #include "dvb_frontend.h"
44 static int dvb_frontend_debug;
45 static int dvb_shutdown_timeout = 5;
46 static int dvb_force_auto_inversion;
47 static int dvb_override_tune_delay;
48 static int dvb_powerdown_on_sleep = 1;
50 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
51 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
52 module_param(dvb_shutdown_timeout, int, 0644);
53 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
54 module_param(dvb_force_auto_inversion, int, 0644);
55 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
56 module_param(dvb_override_tune_delay, int, 0644);
57 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
58 module_param(dvb_powerdown_on_sleep, int, 0644);
59 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
61 #define dprintk if (dvb_frontend_debug) printk
63 #define FESTATE_IDLE 1
64 #define FESTATE_RETUNE 2
65 #define FESTATE_TUNING_FAST 4
66 #define FESTATE_TUNING_SLOW 8
67 #define FESTATE_TUNED 16
68 #define FESTATE_ZIGZAG_FAST 32
69 #define FESTATE_ZIGZAG_SLOW 64
70 #define FESTATE_DISEQC 128
71 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
72 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
73 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
74 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
78 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
79 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
80 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
81 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
82 * FESTATE_TUNED. The frontend has successfully locked on.
83 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
84 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
85 * FESTATE_DISEQC. A DISEQC command has just been issued.
86 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
87 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
88 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
89 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
92 static DEFINE_MUTEX(frontend_mutex);
94 struct dvb_frontend_private {
96 /* thread/frontend values */
97 struct dvb_device *dvbdev;
98 struct dvb_frontend_parameters parameters;
99 struct dvb_fe_events events;
100 struct semaphore sem;
101 struct list_head list_head;
102 wait_queue_head_t wait_queue;
104 unsigned long release_jiffies;
108 unsigned long tune_mode_flags;
110 unsigned int reinitialise;
114 /* swzigzag values */
116 unsigned int bending;
118 unsigned int inversion;
119 unsigned int auto_step;
120 unsigned int auto_sub_step;
121 unsigned int started_auto_step;
122 unsigned int min_delay;
123 unsigned int max_drift;
124 unsigned int step_size;
126 unsigned int check_wrapped;
129 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
131 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
133 struct dvb_frontend_private *fepriv = fe->frontend_priv;
134 struct dvb_fe_events *events = &fepriv->events;
135 struct dvb_frontend_event *e;
138 dprintk ("%s\n", __FUNCTION__);
140 if (down_interruptible (&events->sem))
143 wp = (events->eventw + 1) % MAX_EVENT;
145 if (wp == events->eventr) {
146 events->overflow = 1;
147 events->eventr = (events->eventr + 1) % MAX_EVENT;
150 e = &events->events[events->eventw];
152 memcpy (&e->parameters, &fepriv->parameters,
153 sizeof (struct dvb_frontend_parameters));
155 if (status & FE_HAS_LOCK)
156 if (fe->ops.get_frontend)
157 fe->ops.get_frontend(fe, &e->parameters);
165 wake_up_interruptible (&events->wait_queue);
168 static int dvb_frontend_get_event(struct dvb_frontend *fe,
169 struct dvb_frontend_event *event, int flags)
171 struct dvb_frontend_private *fepriv = fe->frontend_priv;
172 struct dvb_fe_events *events = &fepriv->events;
174 dprintk ("%s\n", __FUNCTION__);
176 if (events->overflow) {
177 events->overflow = 0;
181 if (events->eventw == events->eventr) {
184 if (flags & O_NONBLOCK)
189 ret = wait_event_interruptible (events->wait_queue,
190 events->eventw != events->eventr);
192 if (down_interruptible (&fepriv->sem))
199 if (down_interruptible (&events->sem))
202 memcpy (event, &events->events[events->eventr],
203 sizeof(struct dvb_frontend_event));
205 events->eventr = (events->eventr + 1) % MAX_EVENT;
212 static void dvb_frontend_init(struct dvb_frontend *fe)
214 dprintk ("DVB: initialising frontend %i (%s)...\n",
220 if (fe->ops.tuner_ops.init) {
221 fe->ops.tuner_ops.init(fe);
222 if (fe->ops.i2c_gate_ctrl)
223 fe->ops.i2c_gate_ctrl(fe, 0);
227 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
229 struct dvb_frontend_private *fepriv = fe->frontend_priv;
231 fepriv->reinitialise = 1;
232 dvb_frontend_wakeup(fe);
234 EXPORT_SYMBOL(dvb_frontend_reinitialise);
236 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
240 dprintk ("%s\n", __FUNCTION__);
243 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
245 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
247 q2 = fepriv->quality - 128;
250 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
254 * Performs automatic twiddling of frontend parameters.
256 * @param fe The frontend concerned.
257 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
258 * @returns Number of complete iterations that have been performed.
260 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
264 struct dvb_frontend_private *fepriv = fe->frontend_priv;
265 int original_inversion = fepriv->parameters.inversion;
266 u32 original_frequency = fepriv->parameters.frequency;
268 /* are we using autoinversion? */
269 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
270 (fepriv->parameters.inversion == INVERSION_AUTO));
272 /* setup parameters correctly */
274 /* calculate the lnb_drift */
275 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
277 /* wrap the auto_step if we've exceeded the maximum drift */
278 if (fepriv->lnb_drift > fepriv->max_drift) {
279 fepriv->auto_step = 0;
280 fepriv->auto_sub_step = 0;
281 fepriv->lnb_drift = 0;
284 /* perform inversion and +/- zigzag */
285 switch(fepriv->auto_sub_step) {
287 /* try with the current inversion and current drift setting */
292 if (!autoinversion) break;
294 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
299 if (fepriv->lnb_drift == 0) break;
301 fepriv->lnb_drift = -fepriv->lnb_drift;
306 if (fepriv->lnb_drift == 0) break;
307 if (!autoinversion) break;
309 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
310 fepriv->lnb_drift = -fepriv->lnb_drift;
316 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
320 if (!ready) fepriv->auto_sub_step++;
323 /* if this attempt would hit where we started, indicate a complete
324 * iteration has occurred */
325 if ((fepriv->auto_step == fepriv->started_auto_step) &&
326 (fepriv->auto_sub_step == 0) && check_wrapped) {
330 dprintk("%s: drift:%i inversion:%i auto_step:%i "
331 "auto_sub_step:%i started_auto_step:%i\n",
332 __FUNCTION__, fepriv->lnb_drift, fepriv->inversion,
333 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
335 /* set the frontend itself */
336 fepriv->parameters.frequency += fepriv->lnb_drift;
338 fepriv->parameters.inversion = fepriv->inversion;
339 if (fe->ops.set_frontend)
340 fe->ops.set_frontend(fe, &fepriv->parameters);
342 fepriv->parameters.frequency = original_frequency;
343 fepriv->parameters.inversion = original_inversion;
345 fepriv->auto_sub_step++;
349 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
352 struct dvb_frontend_private *fepriv = fe->frontend_priv;
354 /* if we've got no parameters, just keep idling */
355 if (fepriv->state & FESTATE_IDLE) {
356 fepriv->delay = 3*HZ;
361 /* in SCAN mode, we just set the frontend when asked and leave it alone */
362 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
363 if (fepriv->state & FESTATE_RETUNE) {
364 if (fe->ops.set_frontend)
365 fe->ops.set_frontend(fe, &fepriv->parameters);
366 fepriv->state = FESTATE_TUNED;
368 fepriv->delay = 3*HZ;
373 /* get the frontend status */
374 if (fepriv->state & FESTATE_RETUNE) {
377 if (fe->ops.read_status)
378 fe->ops.read_status(fe, &s);
379 if (s != fepriv->status) {
380 dvb_frontend_add_event(fe, s);
385 /* if we're not tuned, and we have a lock, move to the TUNED state */
386 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
387 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
388 fepriv->state = FESTATE_TUNED;
390 /* if we're tuned, then we have determined the correct inversion */
391 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
392 (fepriv->parameters.inversion == INVERSION_AUTO)) {
393 fepriv->parameters.inversion = fepriv->inversion;
398 /* if we are tuned already, check we're still locked */
399 if (fepriv->state & FESTATE_TUNED) {
400 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
402 /* we're tuned, and the lock is still good... */
403 if (s & FE_HAS_LOCK) {
405 } else { /* if we _WERE_ tuned, but now don't have a lock */
406 fepriv->state = FESTATE_ZIGZAG_FAST;
407 fepriv->started_auto_step = fepriv->auto_step;
408 fepriv->check_wrapped = 0;
412 /* don't actually do anything if we're in the LOSTLOCK state,
413 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
414 if ((fepriv->state & FESTATE_LOSTLOCK) &&
415 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
416 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
420 /* don't do anything if we're in the DISEQC state, since this
421 * might be someone with a motorized dish controlled by DISEQC.
422 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
423 if (fepriv->state & FESTATE_DISEQC) {
424 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
428 /* if we're in the RETUNE state, set everything up for a brand
429 * new scan, keeping the current inversion setting, as the next
430 * tune is _very_ likely to require the same */
431 if (fepriv->state & FESTATE_RETUNE) {
432 fepriv->lnb_drift = 0;
433 fepriv->auto_step = 0;
434 fepriv->auto_sub_step = 0;
435 fepriv->started_auto_step = 0;
436 fepriv->check_wrapped = 0;
440 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
441 fepriv->delay = fepriv->min_delay;
444 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
445 /* OK, if we've run out of trials at the fast speed.
446 * Drop back to slow for the _next_ attempt */
447 fepriv->state = FESTATE_SEARCHING_SLOW;
448 fepriv->started_auto_step = fepriv->auto_step;
451 fepriv->check_wrapped = 1;
453 /* if we've just retuned, enter the ZIGZAG_FAST state.
454 * This ensures we cannot return from an
455 * FE_SET_FRONTEND ioctl before the first frontend tune
457 if (fepriv->state & FESTATE_RETUNE) {
458 fepriv->state = FESTATE_TUNING_FAST;
463 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
464 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
466 /* Note: don't bother checking for wrapping; we stay in this
467 * state until we get a lock */
468 dvb_frontend_swzigzag_autotune(fe, 0);
472 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
474 struct dvb_frontend_private *fepriv = fe->frontend_priv;
479 if (fepriv->dvbdev->writers == 1)
480 if (time_after(jiffies, fepriv->release_jiffies +
481 dvb_shutdown_timeout * HZ))
487 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
489 struct dvb_frontend_private *fepriv = fe->frontend_priv;
491 if (fepriv->wakeup) {
495 return dvb_frontend_is_exiting(fe);
498 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
500 struct dvb_frontend_private *fepriv = fe->frontend_priv;
503 wake_up_interruptible(&fepriv->wait_queue);
506 static int dvb_frontend_thread(void *data)
508 struct dvb_frontend *fe = data;
509 struct dvb_frontend_private *fepriv = fe->frontend_priv;
510 unsigned long timeout;
513 struct dvb_frontend_parameters *params;
515 dprintk("%s\n", __FUNCTION__);
517 snprintf (name, sizeof(name), "kdvb-fe-%i", fe->dvb->num);
521 sigfillset(¤t->blocked);
524 fepriv->check_wrapped = 0;
526 fepriv->delay = 3*HZ;
529 fepriv->reinitialise = 0;
531 dvb_frontend_init(fe);
534 up(&fepriv->sem); /* is locked when we enter the thread... */
536 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
537 dvb_frontend_should_wakeup(fe),
539 if (0 != dvb_frontend_is_exiting(fe)) {
540 /* got signal or quitting */
546 if (down_interruptible(&fepriv->sem))
549 if (fepriv->reinitialise) {
550 dvb_frontend_init(fe);
551 if (fepriv->tone != -1) {
552 fe->ops.set_tone(fe, fepriv->tone);
554 if (fepriv->voltage != -1) {
555 fe->ops.set_voltage(fe, fepriv->voltage);
557 fepriv->reinitialise = 0;
560 /* do an iteration of the tuning loop */
561 if (fe->ops.get_frontend_algo) {
562 if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
563 /* have we been asked to retune? */
565 if (fepriv->state & FESTATE_RETUNE) {
566 params = &fepriv->parameters;
567 fepriv->state = FESTATE_TUNED;
570 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
571 if (s != fepriv->status) {
572 dvb_frontend_add_event(fe, s);
576 dvb_frontend_swzigzag(fe);
578 dvb_frontend_swzigzag(fe);
581 if (dvb_shutdown_timeout) {
582 if (dvb_powerdown_on_sleep)
583 if (fe->ops.set_voltage)
584 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
585 if (fe->ops.tuner_ops.sleep) {
586 fe->ops.tuner_ops.sleep(fe);
587 if (fe->ops.i2c_gate_ctrl)
588 fe->ops.i2c_gate_ctrl(fe, 0);
594 fepriv->thread_pid = 0;
597 dvb_frontend_wakeup(fe);
601 static void dvb_frontend_stop(struct dvb_frontend *fe)
604 struct dvb_frontend_private *fepriv = fe->frontend_priv;
606 dprintk ("%s\n", __FUNCTION__);
611 if (!fepriv->thread_pid)
614 /* check if the thread is really alive */
615 if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) {
616 printk("dvb_frontend_stop: thread PID %d already died\n",
618 /* make sure the mutex was not held by the thread */
619 init_MUTEX (&fepriv->sem);
623 /* wake up the frontend thread, so it notices that fe->exit == 1 */
624 dvb_frontend_wakeup(fe);
626 /* wait until the frontend thread has exited */
627 ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid);
628 if (-ERESTARTSYS != ret) {
629 fepriv->state = FESTATE_IDLE;
632 fepriv->state = FESTATE_IDLE;
634 /* paranoia check in case a signal arrived */
635 if (fepriv->thread_pid)
636 printk("dvb_frontend_stop: warning: thread PID %d won't exit\n",
640 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
642 return ((curtime.tv_usec < lasttime.tv_usec) ?
643 1000000 - lasttime.tv_usec + curtime.tv_usec :
644 curtime.tv_usec - lasttime.tv_usec);
646 EXPORT_SYMBOL(timeval_usec_diff);
648 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
650 curtime->tv_usec += add_usec;
651 if (curtime->tv_usec >= 1000000) {
652 curtime->tv_usec -= 1000000;
658 * Sleep until gettimeofday() > waketime + add_usec
659 * This needs to be as precise as possible, but as the delay is
660 * usually between 2ms and 32ms, it is done using a scheduled msleep
661 * followed by usleep (normally a busy-wait loop) for the remainder
663 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
665 struct timeval lasttime;
668 timeval_usec_add(waketime, add_usec);
670 do_gettimeofday(&lasttime);
671 delta = timeval_usec_diff(lasttime, *waketime);
673 msleep((delta - 1500) / 1000);
674 do_gettimeofday(&lasttime);
675 newdelta = timeval_usec_diff(lasttime, *waketime);
676 delta = (newdelta > delta) ? 0 : newdelta;
681 EXPORT_SYMBOL(dvb_frontend_sleep_until);
683 static int dvb_frontend_start(struct dvb_frontend *fe)
686 struct dvb_frontend_private *fepriv = fe->frontend_priv;
688 dprintk ("%s\n", __FUNCTION__);
690 if (fepriv->thread_pid) {
694 dvb_frontend_stop (fe);
697 if (signal_pending(current))
699 if (down_interruptible (&fepriv->sem))
702 fepriv->state = FESTATE_IDLE;
704 fepriv->thread_pid = 0;
707 ret = kernel_thread (dvb_frontend_thread, fe, 0);
710 printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret);
714 fepriv->thread_pid = ret;
719 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
720 unsigned int cmd, void *parg)
722 struct dvb_device *dvbdev = file->private_data;
723 struct dvb_frontend *fe = dvbdev->priv;
724 struct dvb_frontend_private *fepriv = fe->frontend_priv;
725 int err = -EOPNOTSUPP;
727 dprintk ("%s\n", __FUNCTION__);
729 if (!fe || fepriv->exit)
732 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
733 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
734 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
737 if (down_interruptible (&fepriv->sem))
742 struct dvb_frontend_info* info = parg;
743 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
745 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
746 * do it, it is done for it. */
747 info->caps |= FE_CAN_INVERSION_AUTO;
752 case FE_READ_STATUS: {
753 fe_status_t* status = parg;
755 /* if retune was requested but hasn't occured yet, prevent
756 * that user get signal state from previous tuning */
757 if(fepriv->state == FESTATE_RETUNE) {
763 if (fe->ops.read_status)
764 err = fe->ops.read_status(fe, status);
768 if (fe->ops.read_ber)
769 err = fe->ops.read_ber(fe, (__u32*) parg);
772 case FE_READ_SIGNAL_STRENGTH:
773 if (fe->ops.read_signal_strength)
774 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
778 if (fe->ops.read_snr)
779 err = fe->ops.read_snr(fe, (__u16*) parg);
782 case FE_READ_UNCORRECTED_BLOCKS:
783 if (fe->ops.read_ucblocks)
784 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
788 case FE_DISEQC_RESET_OVERLOAD:
789 if (fe->ops.diseqc_reset_overload) {
790 err = fe->ops.diseqc_reset_overload(fe);
791 fepriv->state = FESTATE_DISEQC;
796 case FE_DISEQC_SEND_MASTER_CMD:
797 if (fe->ops.diseqc_send_master_cmd) {
798 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
799 fepriv->state = FESTATE_DISEQC;
804 case FE_DISEQC_SEND_BURST:
805 if (fe->ops.diseqc_send_burst) {
806 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
807 fepriv->state = FESTATE_DISEQC;
813 if (fe->ops.set_tone) {
814 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
815 fepriv->tone = (fe_sec_tone_mode_t) parg;
816 fepriv->state = FESTATE_DISEQC;
822 if (fe->ops.set_voltage) {
823 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
824 fepriv->voltage = (fe_sec_voltage_t) parg;
825 fepriv->state = FESTATE_DISEQC;
830 case FE_DISHNETWORK_SEND_LEGACY_CMD:
831 if (fe->ops.dishnetwork_send_legacy_command) {
832 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
833 fepriv->state = FESTATE_DISEQC;
835 } else if (fe->ops.set_voltage) {
837 * NOTE: This is a fallback condition. Some frontends
838 * (stv0299 for instance) take longer than 8msec to
839 * respond to a set_voltage command. Those switches
840 * need custom routines to switch properly. For all
841 * other frontends, the following shoule work ok.
842 * Dish network legacy switches (as used by Dish500)
843 * are controlled by sending 9-bit command words
844 * spaced 8msec apart.
845 * the actual command word is switch/port dependant
846 * so it is up to the userspace application to send
848 * The command must always start with a '0' after
849 * initialization, so parg is 8 bits and does not
850 * include the initialization or start bit
852 unsigned long cmd = ((unsigned long) parg) << 1;
853 struct timeval nexttime;
854 struct timeval tv[10];
857 if (dvb_frontend_debug)
858 printk("%s switch command: 0x%04lx\n", __FUNCTION__, cmd);
859 do_gettimeofday(&nexttime);
860 if (dvb_frontend_debug)
861 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
862 /* before sending a command, initialize by sending
863 * a 32ms 18V to the switch
865 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
866 dvb_frontend_sleep_until(&nexttime, 32000);
868 for (i = 0; i < 9; i++) {
869 if (dvb_frontend_debug)
870 do_gettimeofday(&tv[i + 1]);
871 if ((cmd & 0x01) != last) {
872 /* set voltage to (last ? 13V : 18V) */
873 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
874 last = (last) ? 0 : 1;
878 dvb_frontend_sleep_until(&nexttime, 8000);
880 if (dvb_frontend_debug) {
881 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
882 __FUNCTION__, fe->dvb->num);
883 for (i = 1; i < 10; i++)
884 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
887 fepriv->state = FESTATE_DISEQC;
892 case FE_DISEQC_RECV_SLAVE_REPLY:
893 if (fe->ops.diseqc_recv_slave_reply)
894 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
897 case FE_ENABLE_HIGH_LNB_VOLTAGE:
898 if (fe->ops.enable_high_lnb_voltage)
899 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
902 case FE_SET_FRONTEND: {
903 struct dvb_frontend_tune_settings fetunesettings;
905 memcpy (&fepriv->parameters, parg,
906 sizeof (struct dvb_frontend_parameters));
908 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
909 memcpy(&fetunesettings.parameters, parg,
910 sizeof (struct dvb_frontend_parameters));
912 /* force auto frequency inversion if requested */
913 if (dvb_force_auto_inversion) {
914 fepriv->parameters.inversion = INVERSION_AUTO;
915 fetunesettings.parameters.inversion = INVERSION_AUTO;
917 if (fe->ops.info.type == FE_OFDM) {
918 /* without hierachical coding code_rate_LP is irrelevant,
919 * so we tolerate the otherwise invalid FEC_NONE setting */
920 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
921 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
922 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
925 /* get frontend-specific tuning settings */
926 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
927 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
928 fepriv->max_drift = fetunesettings.max_drift;
929 fepriv->step_size = fetunesettings.step_size;
932 switch(fe->ops.info.type) {
934 fepriv->min_delay = HZ/20;
935 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
936 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
940 fepriv->min_delay = HZ/20;
941 fepriv->step_size = 0; /* no zigzag */
942 fepriv->max_drift = 0;
946 fepriv->min_delay = HZ/20;
947 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
948 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
951 fepriv->min_delay = HZ/20;
952 fepriv->step_size = 0;
953 fepriv->max_drift = 0;
957 if (dvb_override_tune_delay > 0)
958 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
960 fepriv->state = FESTATE_RETUNE;
961 dvb_frontend_wakeup(fe);
962 dvb_frontend_add_event(fe, 0);
969 err = dvb_frontend_get_event (fe, parg, file->f_flags);
972 case FE_GET_FRONTEND:
973 if (fe->ops.get_frontend) {
974 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
975 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
979 case FE_SET_FRONTEND_TUNE_MODE:
980 fepriv->tune_mode_flags = (unsigned long) parg;
989 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
991 struct dvb_device *dvbdev = file->private_data;
992 struct dvb_frontend *fe = dvbdev->priv;
993 struct dvb_frontend_private *fepriv = fe->frontend_priv;
995 dprintk ("%s\n", __FUNCTION__);
997 poll_wait (file, &fepriv->events.wait_queue, wait);
999 if (fepriv->events.eventw != fepriv->events.eventr)
1000 return (POLLIN | POLLRDNORM | POLLPRI);
1005 static int dvb_frontend_open(struct inode *inode, struct file *file)
1007 struct dvb_device *dvbdev = file->private_data;
1008 struct dvb_frontend *fe = dvbdev->priv;
1009 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1012 dprintk ("%s\n", __FUNCTION__);
1014 if ((ret = dvb_generic_open (inode, file)) < 0)
1017 if (fe->ops.ts_bus_ctrl) {
1018 if ((ret = fe->ops.ts_bus_ctrl (fe, 1)) < 0) {
1019 dvb_generic_release (inode, file);
1024 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1026 /* normal tune mode when opened R/W */
1027 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1029 fepriv->voltage = -1;
1031 ret = dvb_frontend_start (fe);
1033 dvb_generic_release (inode, file);
1035 /* empty event queue */
1036 fepriv->events.eventr = fepriv->events.eventw = 0;
1042 static int dvb_frontend_release(struct inode *inode, struct file *file)
1044 struct dvb_device *dvbdev = file->private_data;
1045 struct dvb_frontend *fe = dvbdev->priv;
1046 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1048 dprintk ("%s\n", __FUNCTION__);
1050 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1051 fepriv->release_jiffies = jiffies;
1053 if (fe->ops.ts_bus_ctrl)
1054 fe->ops.ts_bus_ctrl (fe, 0);
1056 return dvb_generic_release (inode, file);
1059 static struct file_operations dvb_frontend_fops = {
1060 .owner = THIS_MODULE,
1061 .ioctl = dvb_generic_ioctl,
1062 .poll = dvb_frontend_poll,
1063 .open = dvb_frontend_open,
1064 .release = dvb_frontend_release
1067 int dvb_register_frontend(struct dvb_adapter* dvb,
1068 struct dvb_frontend* fe)
1070 struct dvb_frontend_private *fepriv;
1071 static const struct dvb_device dvbdev_template = {
1075 .fops = &dvb_frontend_fops,
1076 .kernel_ioctl = dvb_frontend_ioctl
1079 dprintk ("%s\n", __FUNCTION__);
1081 if (mutex_lock_interruptible(&frontend_mutex))
1082 return -ERESTARTSYS;
1084 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1085 if (fe->frontend_priv == NULL) {
1086 mutex_unlock(&frontend_mutex);
1089 fepriv = fe->frontend_priv;
1091 init_MUTEX (&fepriv->sem);
1092 init_waitqueue_head (&fepriv->wait_queue);
1093 init_waitqueue_head (&fepriv->events.wait_queue);
1094 init_MUTEX (&fepriv->events.sem);
1096 fepriv->inversion = INVERSION_OFF;
1098 printk ("DVB: registering frontend %i (%s)...\n",
1102 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1103 fe, DVB_DEVICE_FRONTEND);
1105 mutex_unlock(&frontend_mutex);
1108 EXPORT_SYMBOL(dvb_register_frontend);
1110 int dvb_unregister_frontend(struct dvb_frontend* fe)
1112 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1113 dprintk ("%s\n", __FUNCTION__);
1115 mutex_lock(&frontend_mutex);
1116 dvb_unregister_device (fepriv->dvbdev);
1117 dvb_frontend_stop (fe);
1119 /* fe is invalid now */
1121 mutex_unlock(&frontend_mutex);
1124 EXPORT_SYMBOL(dvb_unregister_frontend);
1126 #ifdef CONFIG_DVB_CORE_ATTACH
1127 void dvb_frontend_detach(struct dvb_frontend* fe)
1131 if (fe->ops.release_sec) {
1132 fe->ops.release_sec(fe);
1133 symbol_put_addr(fe->ops.release_sec);
1135 if (fe->ops.tuner_ops.release) {
1136 fe->ops.tuner_ops.release(fe);
1137 symbol_put_addr(fe->ops.tuner_ops.release);
1139 ptr = (void*)fe->ops.release;
1141 fe->ops.release(fe);
1142 symbol_put_addr(ptr);
1146 void dvb_frontend_detach(struct dvb_frontend* fe)
1148 if (fe->ops.release_sec)
1149 fe->ops.release_sec(fe);
1150 if (fe->ops.tuner_ops.release)
1151 fe->ops.tuner_ops.release(fe);
1152 if (fe->ops.release)
1153 fe->ops.release(fe);
1156 EXPORT_SYMBOL(dvb_frontend_detach);