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/suspend.h>
38 #include <linux/jiffies.h>
39 #include <asm/processor.h>
40 #include <asm/semaphore.h>
42 #include "dvb_frontend.h"
45 static int dvb_frontend_debug;
46 static int dvb_shutdown_timeout = 5;
47 static int dvb_force_auto_inversion;
48 static int dvb_override_tune_delay;
49 static int dvb_powerdown_on_sleep = 1;
51 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
52 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
53 module_param(dvb_shutdown_timeout, int, 0444);
54 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
55 module_param(dvb_force_auto_inversion, int, 0444);
56 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
57 module_param(dvb_override_tune_delay, int, 0444);
58 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
59 module_param(dvb_powerdown_on_sleep, int, 0444);
60 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB volatage off on sleep (default)");
62 #define dprintk if (dvb_frontend_debug) printk
64 #define FESTATE_IDLE 1
65 #define FESTATE_RETUNE 2
66 #define FESTATE_TUNING_FAST 4
67 #define FESTATE_TUNING_SLOW 8
68 #define FESTATE_TUNED 16
69 #define FESTATE_ZIGZAG_FAST 32
70 #define FESTATE_ZIGZAG_SLOW 64
71 #define FESTATE_DISEQC 128
72 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
73 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
74 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
75 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
77 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
78 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
79 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
80 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
81 * FESTATE_TUNED. The frontend has successfully locked on.
82 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
83 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
84 * FESTATE_DISEQC. A DISEQC command has just been issued.
85 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
86 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
87 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
88 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
91 static DECLARE_MUTEX(frontend_mutex);
93 struct dvb_frontend_private {
95 struct dvb_device *dvbdev;
96 struct dvb_frontend_parameters parameters;
97 struct dvb_fe_events events;
99 struct list_head list_head;
100 wait_queue_head_t wait_queue;
102 unsigned long release_jiffies;
109 int started_auto_step;
116 fe_sec_tone_mode_t tone;
120 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
122 struct dvb_frontend_private *fepriv = fe->frontend_priv;
123 struct dvb_fe_events *events = &fepriv->events;
124 struct dvb_frontend_event *e;
127 dprintk ("%s\n", __FUNCTION__);
129 if (down_interruptible (&events->sem))
132 wp = (events->eventw + 1) % MAX_EVENT;
134 if (wp == events->eventr) {
135 events->overflow = 1;
136 events->eventr = (events->eventr + 1) % MAX_EVENT;
139 e = &events->events[events->eventw];
141 memcpy (&e->parameters, &fepriv->parameters,
142 sizeof (struct dvb_frontend_parameters));
144 if (status & FE_HAS_LOCK)
145 if (fe->ops->get_frontend)
146 fe->ops->get_frontend(fe, &e->parameters);
154 wake_up_interruptible (&events->wait_queue);
157 static int dvb_frontend_get_event(struct dvb_frontend *fe,
158 struct dvb_frontend_event *event, int flags)
160 struct dvb_frontend_private *fepriv = fe->frontend_priv;
161 struct dvb_fe_events *events = &fepriv->events;
163 dprintk ("%s\n", __FUNCTION__);
165 if (events->overflow) {
166 events->overflow = 0;
170 if (events->eventw == events->eventr) {
173 if (flags & O_NONBLOCK)
178 ret = wait_event_interruptible (events->wait_queue,
179 events->eventw != events->eventr);
181 if (down_interruptible (&fepriv->sem))
188 if (down_interruptible (&events->sem))
191 memcpy (event, &events->events[events->eventr],
192 sizeof(struct dvb_frontend_event));
194 events->eventr = (events->eventr + 1) % MAX_EVENT;
201 static void dvb_frontend_init(struct dvb_frontend *fe)
203 dprintk ("DVB: initialising frontend %i (%s)...\n",
211 static void update_delay(int *quality, int *delay, int min_delay, int locked)
215 dprintk ("%s\n", __FUNCTION__);
218 (*quality) = (*quality * 220 + 36*256) / 256;
220 (*quality) = (*quality * 220 + 0) / 256;
225 *delay = min_delay + q2 * HZ / (128*128);
229 * Performs automatic twiddling of frontend parameters.
231 * @param fe The frontend concerned.
232 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
233 * @returns Number of complete iterations that have been performed.
235 static int dvb_frontend_autotune(struct dvb_frontend *fe, int check_wrapped)
239 struct dvb_frontend_private *fepriv = fe->frontend_priv;
240 int original_inversion = fepriv->parameters.inversion;
241 u32 original_frequency = fepriv->parameters.frequency;
243 /* are we using autoinversion? */
244 autoinversion = ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
245 (fepriv->parameters.inversion == INVERSION_AUTO));
247 /* setup parameters correctly */
249 /* calculate the lnb_drift */
250 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
252 /* wrap the auto_step if we've exceeded the maximum drift */
253 if (fepriv->lnb_drift > fepriv->max_drift) {
254 fepriv->auto_step = 0;
255 fepriv->auto_sub_step = 0;
256 fepriv->lnb_drift = 0;
259 /* perform inversion and +/- zigzag */
260 switch(fepriv->auto_sub_step) {
262 /* try with the current inversion and current drift setting */
267 if (!autoinversion) break;
269 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
274 if (fepriv->lnb_drift == 0) break;
276 fepriv->lnb_drift = -fepriv->lnb_drift;
281 if (fepriv->lnb_drift == 0) break;
282 if (!autoinversion) break;
284 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
285 fepriv->lnb_drift = -fepriv->lnb_drift;
291 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
295 if (!ready) fepriv->auto_sub_step++;
298 /* if this attempt would hit where we started, indicate a complete
299 * iteration has occurred */
300 if ((fepriv->auto_step == fepriv->started_auto_step) &&
301 (fepriv->auto_sub_step == 0) && check_wrapped) {
305 dprintk("%s: drift:%i inversion:%i auto_step:%i "
306 "auto_sub_step:%i started_auto_step:%i\n",
307 __FUNCTION__, fepriv->lnb_drift, fepriv->inversion,
308 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
310 /* set the frontend itself */
311 fepriv->parameters.frequency += fepriv->lnb_drift;
313 fepriv->parameters.inversion = fepriv->inversion;
314 if (fe->ops->set_frontend)
315 fe->ops->set_frontend(fe, &fepriv->parameters);
317 fepriv->parameters.frequency = original_frequency;
318 fepriv->parameters.inversion = original_inversion;
320 fepriv->auto_sub_step++;
324 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
326 struct dvb_frontend_private *fepriv = fe->frontend_priv;
331 if (fepriv->dvbdev->writers == 1)
332 if (time_after(jiffies, fepriv->release_jiffies +
333 dvb_shutdown_timeout * HZ))
339 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
341 struct dvb_frontend_private *fepriv = fe->frontend_priv;
343 if (fepriv->wakeup) {
347 return dvb_frontend_is_exiting(fe);
350 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
352 struct dvb_frontend_private *fepriv = fe->frontend_priv;
355 wake_up_interruptible(&fepriv->wait_queue);
359 * FIXME: use linux/kthread.h
361 static int dvb_frontend_thread(void *data)
363 struct dvb_frontend *fe = data;
364 struct dvb_frontend_private *fepriv = fe->frontend_priv;
365 unsigned long timeout;
367 int quality = 0, delay = 3*HZ;
369 int check_wrapped = 0;
371 dprintk("%s\n", __FUNCTION__);
373 snprintf (name, sizeof(name), "kdvb-fe-%i", fe->dvb->num);
377 sigfillset(¤t->blocked);
381 dvb_frontend_init(fe);
385 up(&fepriv->sem); /* is locked when we enter the thread... */
387 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
388 dvb_frontend_should_wakeup(fe),
390 if (0 != dvb_frontend_is_exiting(fe)) {
391 /* got signal or quitting */
397 if (down_interruptible(&fepriv->sem))
400 /* if we've got no parameters, just keep idling */
401 if (fepriv->state & FESTATE_IDLE) {
407 /* get the frontend status */
408 if (fepriv->state & FESTATE_RETUNE) {
411 if (fe->ops->read_status)
412 fe->ops->read_status(fe, &s);
413 if (s != fepriv->status) {
414 dvb_frontend_add_event(fe, s);
418 /* if we're not tuned, and we have a lock, move to the TUNED state */
419 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
420 update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
421 fepriv->state = FESTATE_TUNED;
423 /* if we're tuned, then we have determined the correct inversion */
424 if ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
425 (fepriv->parameters.inversion == INVERSION_AUTO)) {
426 fepriv->parameters.inversion = fepriv->inversion;
431 /* if we are tuned already, check we're still locked */
432 if (fepriv->state & FESTATE_TUNED) {
433 update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
435 /* we're tuned, and the lock is still good... */
438 else { /* if we _WERE_ tuned, but now don't have a lock */
439 fepriv->state = FESTATE_ZIGZAG_FAST;
440 fepriv->started_auto_step = fepriv->auto_step;
445 /* don't actually do anything if we're in the LOSTLOCK state,
446 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
447 if ((fepriv->state & FESTATE_LOSTLOCK) &&
448 (fe->ops->info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
449 update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
453 /* don't do anything if we're in the DISEQC state, since this
454 * might be someone with a motorized dish controlled by DISEQC.
455 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
456 if (fepriv->state & FESTATE_DISEQC) {
457 update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
461 /* if we're in the RETUNE state, set everything up for a brand
462 * new scan, keeping the current inversion setting, as the next
463 * tune is _very_ likely to require the same */
464 if (fepriv->state & FESTATE_RETUNE) {
465 fepriv->lnb_drift = 0;
466 fepriv->auto_step = 0;
467 fepriv->auto_sub_step = 0;
468 fepriv->started_auto_step = 0;
473 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
474 delay = fepriv->min_delay;
477 if (dvb_frontend_autotune(fe, check_wrapped)) {
478 /* OK, if we've run out of trials at the fast speed.
479 * Drop back to slow for the _next_ attempt */
480 fepriv->state = FESTATE_SEARCHING_SLOW;
481 fepriv->started_auto_step = fepriv->auto_step;
486 /* if we've just retuned, enter the ZIGZAG_FAST state.
487 * This ensures we cannot return from an
488 * FE_SET_FRONTEND ioctl before the first frontend tune
490 if (fepriv->state & FESTATE_RETUNE) {
491 fepriv->state = FESTATE_TUNING_FAST;
496 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
497 update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
499 /* Note: don't bother checking for wrapping; we stay in this
500 * state until we get a lock */
501 dvb_frontend_autotune(fe, 0);
505 if (dvb_shutdown_timeout) {
506 if (dvb_powerdown_on_sleep)
507 if (fe->ops->set_voltage)
508 fe->ops->set_voltage(fe, SEC_VOLTAGE_OFF);
513 fepriv->thread_pid = 0;
516 dvb_frontend_wakeup(fe);
520 static void dvb_frontend_stop(struct dvb_frontend *fe)
523 struct dvb_frontend_private *fepriv = fe->frontend_priv;
525 dprintk ("%s\n", __FUNCTION__);
530 if (!fepriv->thread_pid)
533 /* check if the thread is really alive */
534 if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) {
535 printk("dvb_frontend_stop: thread PID %d already died\n",
537 /* make sure the mutex was not held by the thread */
538 init_MUTEX (&fepriv->sem);
542 /* wake up the frontend thread, so it notices that fe->exit == 1 */
543 dvb_frontend_wakeup(fe);
545 /* wait until the frontend thread has exited */
546 ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid);
547 if (-ERESTARTSYS != ret) {
548 fepriv->state = FESTATE_IDLE;
551 fepriv->state = FESTATE_IDLE;
553 /* paranoia check in case a signal arrived */
554 if (fepriv->thread_pid)
555 printk("dvb_frontend_stop: warning: thread PID %d won't exit\n",
559 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
561 return ((curtime.tv_usec < lasttime.tv_usec) ?
562 1000000 - lasttime.tv_usec + curtime.tv_usec :
563 curtime.tv_usec - lasttime.tv_usec);
565 EXPORT_SYMBOL(timeval_usec_diff);
567 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
569 curtime->tv_usec += add_usec;
570 if (curtime->tv_usec >= 1000000) {
571 curtime->tv_usec -= 1000000;
577 * Sleep until gettimeofday() > waketime + add_usec
578 * This needs to be as precise as possible, but as the delay is
579 * usually between 2ms and 32ms, it is done using a scheduled msleep
580 * followed by usleep (normally a busy-wait loop) for the remainder
582 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
584 struct timeval lasttime;
587 timeval_usec_add(waketime, add_usec);
589 do_gettimeofday(&lasttime);
590 delta = timeval_usec_diff(lasttime, *waketime);
592 msleep((delta - 1500) / 1000);
593 do_gettimeofday(&lasttime);
594 newdelta = timeval_usec_diff(lasttime, *waketime);
595 delta = (newdelta > delta) ? 0 : newdelta;
600 EXPORT_SYMBOL(dvb_frontend_sleep_until);
602 static int dvb_frontend_start(struct dvb_frontend *fe)
605 struct dvb_frontend_private *fepriv = fe->frontend_priv;
607 dprintk ("%s\n", __FUNCTION__);
609 if (fepriv->thread_pid) {
613 dvb_frontend_stop (fe);
616 if (signal_pending(current))
618 if (down_interruptible (&fepriv->sem))
621 fepriv->state = FESTATE_IDLE;
623 fepriv->thread_pid = 0;
626 ret = kernel_thread (dvb_frontend_thread, fe, 0);
629 printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret);
633 fepriv->thread_pid = ret;
638 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
639 unsigned int cmd, void *parg)
641 struct dvb_device *dvbdev = file->private_data;
642 struct dvb_frontend *fe = dvbdev->priv;
643 struct dvb_frontend_private *fepriv = fe->frontend_priv;
644 int err = -EOPNOTSUPP;
646 dprintk ("%s\n", __FUNCTION__);
648 if (!fe || fepriv->exit)
651 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
652 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
653 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
656 if (down_interruptible (&fepriv->sem))
661 struct dvb_frontend_info* info = parg;
662 memcpy(info, &fe->ops->info, sizeof(struct dvb_frontend_info));
664 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
665 * do it, it is done for it. */
666 info->caps |= FE_CAN_INVERSION_AUTO;
671 case FE_READ_STATUS: {
672 fe_status_t* status = parg;
674 /* if retune was requested but hasn't occured yet, prevent
675 * that user get signal state from previous tuning */
676 if(fepriv->state == FESTATE_RETUNE) {
682 if (fe->ops->read_status)
683 err = fe->ops->read_status(fe, status);
687 if (fe->ops->read_ber)
688 err = fe->ops->read_ber(fe, (__u32*) parg);
691 case FE_READ_SIGNAL_STRENGTH:
692 if (fe->ops->read_signal_strength)
693 err = fe->ops->read_signal_strength(fe, (__u16*) parg);
697 if (fe->ops->read_snr)
698 err = fe->ops->read_snr(fe, (__u16*) parg);
701 case FE_READ_UNCORRECTED_BLOCKS:
702 if (fe->ops->read_ucblocks)
703 err = fe->ops->read_ucblocks(fe, (__u32*) parg);
707 case FE_DISEQC_RESET_OVERLOAD:
708 if (fe->ops->diseqc_reset_overload) {
709 err = fe->ops->diseqc_reset_overload(fe);
710 fepriv->state = FESTATE_DISEQC;
715 case FE_DISEQC_SEND_MASTER_CMD:
716 if (fe->ops->diseqc_send_master_cmd) {
717 err = fe->ops->diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
718 fepriv->state = FESTATE_DISEQC;
723 case FE_DISEQC_SEND_BURST:
724 if (fe->ops->diseqc_send_burst) {
725 err = fe->ops->diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
726 fepriv->state = FESTATE_DISEQC;
732 if (fe->ops->set_tone) {
733 err = fe->ops->set_tone(fe, (fe_sec_tone_mode_t) parg);
734 fepriv->state = FESTATE_DISEQC;
736 fepriv->tone = (fe_sec_tone_mode_t) parg;
741 if (fe->ops->set_voltage) {
742 err = fe->ops->set_voltage(fe, (fe_sec_voltage_t) parg);
743 fepriv->state = FESTATE_DISEQC;
748 case FE_DISHNETWORK_SEND_LEGACY_CMD:
749 if (fe->ops->dishnetwork_send_legacy_command) {
750 err = fe->ops->dishnetwork_send_legacy_command(fe, (unsigned int) parg);
751 fepriv->state = FESTATE_DISEQC;
753 } else if (fe->ops->set_voltage) {
755 * NOTE: This is a fallback condition. Some frontends
756 * (stv0299 for instance) take longer than 8msec to
757 * respond to a set_voltage command. Those switches
758 * need custom routines to switch properly. For all
759 * other frontends, the following shoule work ok.
760 * Dish network legacy switches (as used by Dish500)
761 * are controlled by sending 9-bit command words
762 * spaced 8msec apart.
763 * the actual command word is switch/port dependant
764 * so it is up to the userspace application to send
766 * The command must always start with a '0' after
767 * initialization, so parg is 8 bits and does not
768 * include the initialization or start bit
770 unsigned int cmd = ((unsigned int) parg) << 1;
771 struct timeval nexttime;
772 struct timeval tv[10];
775 if (dvb_frontend_debug)
776 printk("%s switch command: 0x%04x\n", __FUNCTION__, cmd);
777 do_gettimeofday(&nexttime);
778 if (dvb_frontend_debug)
779 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
780 /* before sending a command, initialize by sending
781 * a 32ms 18V to the switch
783 fe->ops->set_voltage(fe, SEC_VOLTAGE_18);
784 dvb_frontend_sleep_until(&nexttime, 32000);
786 for (i = 0; i < 9; i++) {
787 if (dvb_frontend_debug)
788 do_gettimeofday(&tv[i + 1]);
789 if ((cmd & 0x01) != last) {
790 /* set voltage to (last ? 13V : 18V) */
791 fe->ops->set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
792 last = (last) ? 0 : 1;
796 dvb_frontend_sleep_until(&nexttime, 8000);
798 if (dvb_frontend_debug) {
799 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
800 __FUNCTION__, fe->dvb->num);
801 for (i = 1; i < 10; i++)
802 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
805 fepriv->state = FESTATE_DISEQC;
810 case FE_DISEQC_RECV_SLAVE_REPLY:
811 if (fe->ops->diseqc_recv_slave_reply)
812 err = fe->ops->diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
815 case FE_ENABLE_HIGH_LNB_VOLTAGE:
816 if (fe->ops->enable_high_lnb_voltage)
817 err = fe->ops->enable_high_lnb_voltage(fe, (int) parg);
820 case FE_SET_FRONTEND: {
821 struct dvb_frontend_tune_settings fetunesettings;
823 memcpy (&fepriv->parameters, parg,
824 sizeof (struct dvb_frontend_parameters));
826 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
827 memcpy(&fetunesettings.parameters, parg,
828 sizeof (struct dvb_frontend_parameters));
830 /* force auto frequency inversion if requested */
831 if (dvb_force_auto_inversion) {
832 fepriv->parameters.inversion = INVERSION_AUTO;
833 fetunesettings.parameters.inversion = INVERSION_AUTO;
835 if (fe->ops->info.type == FE_OFDM) {
836 /* without hierachical coding code_rate_LP is irrelevant,
837 * so we tolerate the otherwise invalid FEC_NONE setting */
838 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
839 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
840 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
843 /* get frontend-specific tuning settings */
844 if (fe->ops->get_tune_settings && (fe->ops->get_tune_settings(fe, &fetunesettings) == 0)) {
845 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
846 fepriv->max_drift = fetunesettings.max_drift;
847 fepriv->step_size = fetunesettings.step_size;
850 switch(fe->ops->info.type) {
852 fepriv->min_delay = HZ/20;
853 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
854 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
858 fepriv->min_delay = HZ/20;
859 fepriv->step_size = 0; /* no zigzag */
860 fepriv->max_drift = 0;
864 fepriv->min_delay = HZ/20;
865 fepriv->step_size = fe->ops->info.frequency_stepsize * 2;
866 fepriv->max_drift = (fe->ops->info.frequency_stepsize * 2) + 1;
869 printk("dvb-core: FE_ATSC not handled yet.\n");
873 if (dvb_override_tune_delay > 0)
874 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
876 fepriv->state = FESTATE_RETUNE;
877 dvb_frontend_wakeup(fe);
878 dvb_frontend_add_event(fe, 0);
885 err = dvb_frontend_get_event (fe, parg, file->f_flags);
888 case FE_GET_FRONTEND:
889 if (fe->ops->get_frontend) {
890 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
891 err = fe->ops->get_frontend(fe, (struct dvb_frontend_parameters*) parg);
900 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
902 struct dvb_device *dvbdev = file->private_data;
903 struct dvb_frontend *fe = dvbdev->priv;
904 struct dvb_frontend_private *fepriv = fe->frontend_priv;
906 dprintk ("%s\n", __FUNCTION__);
908 poll_wait (file, &fepriv->events.wait_queue, wait);
910 if (fepriv->events.eventw != fepriv->events.eventr)
911 return (POLLIN | POLLRDNORM | POLLPRI);
916 static int dvb_frontend_open(struct inode *inode, struct file *file)
918 struct dvb_device *dvbdev = file->private_data;
919 struct dvb_frontend *fe = dvbdev->priv;
920 struct dvb_frontend_private *fepriv = fe->frontend_priv;
923 dprintk ("%s\n", __FUNCTION__);
925 if ((ret = dvb_generic_open (inode, file)) < 0)
928 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
929 ret = dvb_frontend_start (fe);
931 dvb_generic_release (inode, file);
933 /* empty event queue */
934 fepriv->events.eventr = fepriv->events.eventw = 0;
940 static int dvb_frontend_release(struct inode *inode, struct file *file)
942 struct dvb_device *dvbdev = file->private_data;
943 struct dvb_frontend *fe = dvbdev->priv;
944 struct dvb_frontend_private *fepriv = fe->frontend_priv;
946 dprintk ("%s\n", __FUNCTION__);
948 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
949 fepriv->release_jiffies = jiffies;
951 return dvb_generic_release (inode, file);
954 static struct file_operations dvb_frontend_fops = {
955 .owner = THIS_MODULE,
956 .ioctl = dvb_generic_ioctl,
957 .poll = dvb_frontend_poll,
958 .open = dvb_frontend_open,
959 .release = dvb_frontend_release
962 int dvb_register_frontend(struct dvb_adapter* dvb,
963 struct dvb_frontend* fe)
965 struct dvb_frontend_private *fepriv;
966 static const struct dvb_device dvbdev_template = {
970 .fops = &dvb_frontend_fops,
971 .kernel_ioctl = dvb_frontend_ioctl
974 dprintk ("%s\n", __FUNCTION__);
976 if (down_interruptible (&frontend_mutex))
979 fe->frontend_priv = kmalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
980 if (fe->frontend_priv == NULL) {
984 fepriv = fe->frontend_priv;
985 memset(fe->frontend_priv, 0, sizeof(struct dvb_frontend_private));
987 init_MUTEX (&fepriv->sem);
988 init_waitqueue_head (&fepriv->wait_queue);
989 init_waitqueue_head (&fepriv->events.wait_queue);
990 init_MUTEX (&fepriv->events.sem);
992 fepriv->inversion = INVERSION_OFF;
993 fepriv->tone = SEC_TONE_OFF;
995 printk ("DVB: registering frontend %i (%s)...\n",
999 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1000 fe, DVB_DEVICE_FRONTEND);
1002 up (&frontend_mutex);
1005 EXPORT_SYMBOL(dvb_register_frontend);
1007 int dvb_unregister_frontend(struct dvb_frontend* fe)
1009 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1010 dprintk ("%s\n", __FUNCTION__);
1012 down (&frontend_mutex);
1013 dvb_unregister_device (fepriv->dvbdev);
1014 dvb_frontend_stop (fe);
1015 if (fe->ops->release)
1016 fe->ops->release(fe);
1018 printk("dvb_frontend: Demodulator (%s) does not have a release callback!\n", fe->ops->info.name);
1019 /* fe is invalid now */
1021 up (&frontend_mutex);
1024 EXPORT_SYMBOL(dvb_unregister_frontend);