[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
blob771f32d889e6261de8b8c77d10fad85b55da20a4
1 /*
2 * dvb_frontend.c: DVB frontend tuning interface/thread
5 * Copyright (C) 1999-2001 Ralph Metzler
6 * Marcus Metzler
7 * Holger Waechtler
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"
43 #include "dvbdev.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 /* thread/frontend values */
96 struct dvb_device *dvbdev;
97 struct dvb_frontend_parameters parameters;
98 struct dvb_fe_events events;
99 struct semaphore sem;
100 struct list_head list_head;
101 wait_queue_head_t wait_queue;
102 pid_t thread_pid;
103 unsigned long release_jiffies;
104 unsigned int exit;
105 unsigned int wakeup;
106 fe_status_t status;
107 unsigned long tune_mode_flags;
108 unsigned int delay;
110 /* swzigzag values */
111 unsigned int state;
112 unsigned int bending;
113 int lnb_drift;
114 unsigned int inversion;
115 unsigned int auto_step;
116 unsigned int auto_sub_step;
117 unsigned int started_auto_step;
118 unsigned int min_delay;
119 unsigned int max_drift;
120 unsigned int step_size;
121 int quality;
122 unsigned int check_wrapped;
126 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
128 struct dvb_frontend_private *fepriv = fe->frontend_priv;
129 struct dvb_fe_events *events = &fepriv->events;
130 struct dvb_frontend_event *e;
131 int wp;
133 dprintk ("%s\n", __FUNCTION__);
135 if (down_interruptible (&events->sem))
136 return;
138 wp = (events->eventw + 1) % MAX_EVENT;
140 if (wp == events->eventr) {
141 events->overflow = 1;
142 events->eventr = (events->eventr + 1) % MAX_EVENT;
145 e = &events->events[events->eventw];
147 memcpy (&e->parameters, &fepriv->parameters,
148 sizeof (struct dvb_frontend_parameters));
150 if (status & FE_HAS_LOCK)
151 if (fe->ops->get_frontend)
152 fe->ops->get_frontend(fe, &e->parameters);
154 events->eventw = wp;
156 up (&events->sem);
158 e->status = status;
160 wake_up_interruptible (&events->wait_queue);
163 static int dvb_frontend_get_event(struct dvb_frontend *fe,
164 struct dvb_frontend_event *event, int flags)
166 struct dvb_frontend_private *fepriv = fe->frontend_priv;
167 struct dvb_fe_events *events = &fepriv->events;
169 dprintk ("%s\n", __FUNCTION__);
171 if (events->overflow) {
172 events->overflow = 0;
173 return -EOVERFLOW;
176 if (events->eventw == events->eventr) {
177 int ret;
179 if (flags & O_NONBLOCK)
180 return -EWOULDBLOCK;
182 up(&fepriv->sem);
184 ret = wait_event_interruptible (events->wait_queue,
185 events->eventw != events->eventr);
187 if (down_interruptible (&fepriv->sem))
188 return -ERESTARTSYS;
190 if (ret < 0)
191 return ret;
194 if (down_interruptible (&events->sem))
195 return -ERESTARTSYS;
197 memcpy (event, &events->events[events->eventr],
198 sizeof(struct dvb_frontend_event));
200 events->eventr = (events->eventr + 1) % MAX_EVENT;
202 up (&events->sem);
204 return 0;
207 static void dvb_frontend_init(struct dvb_frontend *fe)
209 dprintk ("DVB: initialising frontend %i (%s)...\n",
210 fe->dvb->num,
211 fe->ops->info.name);
213 if (fe->ops->init)
214 fe->ops->init(fe);
217 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
219 int q2;
221 dprintk ("%s\n", __FUNCTION__);
223 if (locked)
224 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
225 else
226 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
228 q2 = fepriv->quality - 128;
229 q2 *= q2;
231 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
235 * Performs automatic twiddling of frontend parameters.
237 * @param fe The frontend concerned.
238 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
239 * @returns Number of complete iterations that have been performed.
241 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
243 int autoinversion;
244 int ready = 0;
245 struct dvb_frontend_private *fepriv = fe->frontend_priv;
246 int original_inversion = fepriv->parameters.inversion;
247 u32 original_frequency = fepriv->parameters.frequency;
249 /* are we using autoinversion? */
250 autoinversion = ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
251 (fepriv->parameters.inversion == INVERSION_AUTO));
253 /* setup parameters correctly */
254 while(!ready) {
255 /* calculate the lnb_drift */
256 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
258 /* wrap the auto_step if we've exceeded the maximum drift */
259 if (fepriv->lnb_drift > fepriv->max_drift) {
260 fepriv->auto_step = 0;
261 fepriv->auto_sub_step = 0;
262 fepriv->lnb_drift = 0;
265 /* perform inversion and +/- zigzag */
266 switch(fepriv->auto_sub_step) {
267 case 0:
268 /* try with the current inversion and current drift setting */
269 ready = 1;
270 break;
272 case 1:
273 if (!autoinversion) break;
275 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
276 ready = 1;
277 break;
279 case 2:
280 if (fepriv->lnb_drift == 0) break;
282 fepriv->lnb_drift = -fepriv->lnb_drift;
283 ready = 1;
284 break;
286 case 3:
287 if (fepriv->lnb_drift == 0) break;
288 if (!autoinversion) break;
290 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
291 fepriv->lnb_drift = -fepriv->lnb_drift;
292 ready = 1;
293 break;
295 default:
296 fepriv->auto_step++;
297 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
298 break;
301 if (!ready) fepriv->auto_sub_step++;
304 /* if this attempt would hit where we started, indicate a complete
305 * iteration has occurred */
306 if ((fepriv->auto_step == fepriv->started_auto_step) &&
307 (fepriv->auto_sub_step == 0) && check_wrapped) {
308 return 1;
311 dprintk("%s: drift:%i inversion:%i auto_step:%i "
312 "auto_sub_step:%i started_auto_step:%i\n",
313 __FUNCTION__, fepriv->lnb_drift, fepriv->inversion,
314 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
316 /* set the frontend itself */
317 fepriv->parameters.frequency += fepriv->lnb_drift;
318 if (autoinversion)
319 fepriv->parameters.inversion = fepriv->inversion;
320 if (fe->ops->set_frontend)
321 fe->ops->set_frontend(fe, &fepriv->parameters);
323 fepriv->parameters.frequency = original_frequency;
324 fepriv->parameters.inversion = original_inversion;
326 fepriv->auto_sub_step++;
327 return 0;
330 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
332 fe_status_t s;
333 struct dvb_frontend_private *fepriv = fe->frontend_priv;
335 /* if we've got no parameters, just keep idling */
336 if (fepriv->state & FESTATE_IDLE) {
337 fepriv->delay = 3*HZ;
338 fepriv->quality = 0;
339 return;
342 /* in SCAN mode, we just set the frontend when asked and leave it alone */
343 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
344 if (fepriv->state & FESTATE_RETUNE) {
345 if (fe->ops->set_frontend)
346 fe->ops->set_frontend(fe, &fepriv->parameters);
347 fepriv->state = FESTATE_TUNED;
349 fepriv->delay = 3*HZ;
350 fepriv->quality = 0;
351 return;
354 /* get the frontend status */
355 if (fepriv->state & FESTATE_RETUNE) {
356 s = 0;
357 } else {
358 if (fe->ops->read_status)
359 fe->ops->read_status(fe, &s);
360 if (s != fepriv->status) {
361 dvb_frontend_add_event(fe, s);
362 fepriv->status = s;
366 /* if we're not tuned, and we have a lock, move to the TUNED state */
367 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
368 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
369 fepriv->state = FESTATE_TUNED;
371 /* if we're tuned, then we have determined the correct inversion */
372 if ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
373 (fepriv->parameters.inversion == INVERSION_AUTO)) {
374 fepriv->parameters.inversion = fepriv->inversion;
376 return;
379 /* if we are tuned already, check we're still locked */
380 if (fepriv->state & FESTATE_TUNED) {
381 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
383 /* we're tuned, and the lock is still good... */
384 if (s & FE_HAS_LOCK) {
385 return;
386 } else { /* if we _WERE_ tuned, but now don't have a lock */
387 fepriv->state = FESTATE_ZIGZAG_FAST;
388 fepriv->started_auto_step = fepriv->auto_step;
389 fepriv->check_wrapped = 0;
393 /* don't actually do anything if we're in the LOSTLOCK state,
394 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
395 if ((fepriv->state & FESTATE_LOSTLOCK) &&
396 (fe->ops->info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
397 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
398 return;
401 /* don't do anything if we're in the DISEQC state, since this
402 * might be someone with a motorized dish controlled by DISEQC.
403 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
404 if (fepriv->state & FESTATE_DISEQC) {
405 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
406 return;
409 /* if we're in the RETUNE state, set everything up for a brand
410 * new scan, keeping the current inversion setting, as the next
411 * tune is _very_ likely to require the same */
412 if (fepriv->state & FESTATE_RETUNE) {
413 fepriv->lnb_drift = 0;
414 fepriv->auto_step = 0;
415 fepriv->auto_sub_step = 0;
416 fepriv->started_auto_step = 0;
417 fepriv->check_wrapped = 0;
420 /* fast zigzag. */
421 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
422 fepriv->delay = fepriv->min_delay;
424 /* peform a tune */
425 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
426 /* OK, if we've run out of trials at the fast speed.
427 * Drop back to slow for the _next_ attempt */
428 fepriv->state = FESTATE_SEARCHING_SLOW;
429 fepriv->started_auto_step = fepriv->auto_step;
430 return;
432 fepriv->check_wrapped = 1;
434 /* if we've just retuned, enter the ZIGZAG_FAST state.
435 * This ensures we cannot return from an
436 * FE_SET_FRONTEND ioctl before the first frontend tune
437 * occurs */
438 if (fepriv->state & FESTATE_RETUNE) {
439 fepriv->state = FESTATE_TUNING_FAST;
443 /* slow zigzag */
444 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
445 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
447 /* Note: don't bother checking for wrapping; we stay in this
448 * state until we get a lock */
449 dvb_frontend_swzigzag_autotune(fe, 0);
453 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
455 struct dvb_frontend_private *fepriv = fe->frontend_priv;
457 if (fepriv->exit)
458 return 1;
460 if (fepriv->dvbdev->writers == 1)
461 if (time_after(jiffies, fepriv->release_jiffies +
462 dvb_shutdown_timeout * HZ))
463 return 1;
465 return 0;
468 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
470 struct dvb_frontend_private *fepriv = fe->frontend_priv;
472 if (fepriv->wakeup) {
473 fepriv->wakeup = 0;
474 return 1;
476 return dvb_frontend_is_exiting(fe);
479 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
481 struct dvb_frontend_private *fepriv = fe->frontend_priv;
483 fepriv->wakeup = 1;
484 wake_up_interruptible(&fepriv->wait_queue);
487 static int dvb_frontend_thread(void *data)
489 struct dvb_frontend *fe = data;
490 struct dvb_frontend_private *fepriv = fe->frontend_priv;
491 unsigned long timeout;
492 char name [15];
493 fe_status_t s;
494 struct dvb_frontend_parameters *params;
496 dprintk("%s\n", __FUNCTION__);
498 snprintf (name, sizeof(name), "kdvb-fe-%i", fe->dvb->num);
500 lock_kernel();
501 daemonize(name);
502 sigfillset(&current->blocked);
503 unlock_kernel();
505 fepriv->check_wrapped = 0;
506 fepriv->quality = 0;
507 fepriv->delay = 3*HZ;
508 fepriv->status = 0;
509 dvb_frontend_init(fe);
510 fepriv->wakeup = 0;
512 while (1) {
513 up(&fepriv->sem); /* is locked when we enter the thread... */
515 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
516 dvb_frontend_should_wakeup(fe),
517 fepriv->delay);
518 if (0 != dvb_frontend_is_exiting(fe)) {
519 /* got signal or quitting */
520 break;
523 try_to_freeze();
525 if (down_interruptible(&fepriv->sem))
526 break;
528 /* do an iteration of the tuning loop */
529 if (fe->ops->tune) {
530 /* have we been asked to retune? */
531 params = NULL;
532 if (fepriv->state & FESTATE_RETUNE) {
533 params = &fepriv->parameters;
534 fepriv->state = FESTATE_TUNED;
537 fe->ops->tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
538 if (s != fepriv->status) {
539 dvb_frontend_add_event(fe, s);
540 fepriv->status = s;
542 } else {
543 dvb_frontend_swzigzag(fe);
547 if (dvb_shutdown_timeout) {
548 if (dvb_powerdown_on_sleep)
549 if (fe->ops->set_voltage)
550 fe->ops->set_voltage(fe, SEC_VOLTAGE_OFF);
551 if (fe->ops->sleep)
552 fe->ops->sleep(fe);
555 fepriv->thread_pid = 0;
556 mb();
558 dvb_frontend_wakeup(fe);
559 return 0;
562 static void dvb_frontend_stop(struct dvb_frontend *fe)
564 unsigned long ret;
565 struct dvb_frontend_private *fepriv = fe->frontend_priv;
567 dprintk ("%s\n", __FUNCTION__);
569 fepriv->exit = 1;
570 mb();
572 if (!fepriv->thread_pid)
573 return;
575 /* check if the thread is really alive */
576 if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) {
577 printk("dvb_frontend_stop: thread PID %d already died\n",
578 fepriv->thread_pid);
579 /* make sure the mutex was not held by the thread */
580 init_MUTEX (&fepriv->sem);
581 return;
584 /* wake up the frontend thread, so it notices that fe->exit == 1 */
585 dvb_frontend_wakeup(fe);
587 /* wait until the frontend thread has exited */
588 ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid);
589 if (-ERESTARTSYS != ret) {
590 fepriv->state = FESTATE_IDLE;
591 return;
593 fepriv->state = FESTATE_IDLE;
595 /* paranoia check in case a signal arrived */
596 if (fepriv->thread_pid)
597 printk("dvb_frontend_stop: warning: thread PID %d won't exit\n",
598 fepriv->thread_pid);
601 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
603 return ((curtime.tv_usec < lasttime.tv_usec) ?
604 1000000 - lasttime.tv_usec + curtime.tv_usec :
605 curtime.tv_usec - lasttime.tv_usec);
607 EXPORT_SYMBOL(timeval_usec_diff);
609 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
611 curtime->tv_usec += add_usec;
612 if (curtime->tv_usec >= 1000000) {
613 curtime->tv_usec -= 1000000;
614 curtime->tv_sec++;
619 * Sleep until gettimeofday() > waketime + add_usec
620 * This needs to be as precise as possible, but as the delay is
621 * usually between 2ms and 32ms, it is done using a scheduled msleep
622 * followed by usleep (normally a busy-wait loop) for the remainder
624 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
626 struct timeval lasttime;
627 s32 delta, newdelta;
629 timeval_usec_add(waketime, add_usec);
631 do_gettimeofday(&lasttime);
632 delta = timeval_usec_diff(lasttime, *waketime);
633 if (delta > 2500) {
634 msleep((delta - 1500) / 1000);
635 do_gettimeofday(&lasttime);
636 newdelta = timeval_usec_diff(lasttime, *waketime);
637 delta = (newdelta > delta) ? 0 : newdelta;
639 if (delta > 0)
640 udelay(delta);
642 EXPORT_SYMBOL(dvb_frontend_sleep_until);
644 static int dvb_frontend_start(struct dvb_frontend *fe)
646 int ret;
647 struct dvb_frontend_private *fepriv = fe->frontend_priv;
649 dprintk ("%s\n", __FUNCTION__);
651 if (fepriv->thread_pid) {
652 if (!fepriv->exit)
653 return 0;
654 else
655 dvb_frontend_stop (fe);
658 if (signal_pending(current))
659 return -EINTR;
660 if (down_interruptible (&fepriv->sem))
661 return -EINTR;
663 fepriv->state = FESTATE_IDLE;
664 fepriv->exit = 0;
665 fepriv->thread_pid = 0;
666 mb();
668 ret = kernel_thread (dvb_frontend_thread, fe, 0);
670 if (ret < 0) {
671 printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret);
672 up(&fepriv->sem);
673 return ret;
675 fepriv->thread_pid = ret;
677 return 0;
680 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
681 unsigned int cmd, void *parg)
683 struct dvb_device *dvbdev = file->private_data;
684 struct dvb_frontend *fe = dvbdev->priv;
685 struct dvb_frontend_private *fepriv = fe->frontend_priv;
686 int err = -EOPNOTSUPP;
688 dprintk ("%s\n", __FUNCTION__);
690 if (!fe || fepriv->exit)
691 return -ENODEV;
693 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
694 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
695 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
696 return -EPERM;
698 if (down_interruptible (&fepriv->sem))
699 return -ERESTARTSYS;
701 switch (cmd) {
702 case FE_GET_INFO: {
703 struct dvb_frontend_info* info = parg;
704 memcpy(info, &fe->ops->info, sizeof(struct dvb_frontend_info));
706 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
707 * do it, it is done for it. */
708 info->caps |= FE_CAN_INVERSION_AUTO;
709 err = 0;
710 break;
713 case FE_READ_STATUS: {
714 fe_status_t* status = parg;
716 /* if retune was requested but hasn't occured yet, prevent
717 * that user get signal state from previous tuning */
718 if(fepriv->state == FESTATE_RETUNE) {
719 err=0;
720 *status = 0;
721 break;
724 if (fe->ops->read_status)
725 err = fe->ops->read_status(fe, status);
726 break;
728 case FE_READ_BER:
729 if (fe->ops->read_ber)
730 err = fe->ops->read_ber(fe, (__u32*) parg);
731 break;
733 case FE_READ_SIGNAL_STRENGTH:
734 if (fe->ops->read_signal_strength)
735 err = fe->ops->read_signal_strength(fe, (__u16*) parg);
736 break;
738 case FE_READ_SNR:
739 if (fe->ops->read_snr)
740 err = fe->ops->read_snr(fe, (__u16*) parg);
741 break;
743 case FE_READ_UNCORRECTED_BLOCKS:
744 if (fe->ops->read_ucblocks)
745 err = fe->ops->read_ucblocks(fe, (__u32*) parg);
746 break;
749 case FE_DISEQC_RESET_OVERLOAD:
750 if (fe->ops->diseqc_reset_overload) {
751 err = fe->ops->diseqc_reset_overload(fe);
752 fepriv->state = FESTATE_DISEQC;
753 fepriv->status = 0;
755 break;
757 case FE_DISEQC_SEND_MASTER_CMD:
758 if (fe->ops->diseqc_send_master_cmd) {
759 err = fe->ops->diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
760 fepriv->state = FESTATE_DISEQC;
761 fepriv->status = 0;
763 break;
765 case FE_DISEQC_SEND_BURST:
766 if (fe->ops->diseqc_send_burst) {
767 err = fe->ops->diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
768 fepriv->state = FESTATE_DISEQC;
769 fepriv->status = 0;
771 break;
773 case FE_SET_TONE:
774 if (fe->ops->set_tone) {
775 err = fe->ops->set_tone(fe, (fe_sec_tone_mode_t) parg);
776 fepriv->state = FESTATE_DISEQC;
777 fepriv->status = 0;
779 break;
781 case FE_SET_VOLTAGE:
782 if (fe->ops->set_voltage) {
783 err = fe->ops->set_voltage(fe, (fe_sec_voltage_t) parg);
784 fepriv->state = FESTATE_DISEQC;
785 fepriv->status = 0;
787 break;
789 case FE_DISHNETWORK_SEND_LEGACY_CMD:
790 if (fe->ops->dishnetwork_send_legacy_command) {
791 err = fe->ops->dishnetwork_send_legacy_command(fe, (unsigned long) parg);
792 fepriv->state = FESTATE_DISEQC;
793 fepriv->status = 0;
794 } else if (fe->ops->set_voltage) {
796 * NOTE: This is a fallback condition. Some frontends
797 * (stv0299 for instance) take longer than 8msec to
798 * respond to a set_voltage command. Those switches
799 * need custom routines to switch properly. For all
800 * other frontends, the following shoule work ok.
801 * Dish network legacy switches (as used by Dish500)
802 * are controlled by sending 9-bit command words
803 * spaced 8msec apart.
804 * the actual command word is switch/port dependant
805 * so it is up to the userspace application to send
806 * the right command.
807 * The command must always start with a '0' after
808 * initialization, so parg is 8 bits and does not
809 * include the initialization or start bit
811 unsigned long cmd = ((unsigned long) parg) << 1;
812 struct timeval nexttime;
813 struct timeval tv[10];
814 int i;
815 u8 last = 1;
816 if (dvb_frontend_debug)
817 printk("%s switch command: 0x%04lx\n", __FUNCTION__, cmd);
818 do_gettimeofday(&nexttime);
819 if (dvb_frontend_debug)
820 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
821 /* before sending a command, initialize by sending
822 * a 32ms 18V to the switch
824 fe->ops->set_voltage(fe, SEC_VOLTAGE_18);
825 dvb_frontend_sleep_until(&nexttime, 32000);
827 for (i = 0; i < 9; i++) {
828 if (dvb_frontend_debug)
829 do_gettimeofday(&tv[i + 1]);
830 if ((cmd & 0x01) != last) {
831 /* set voltage to (last ? 13V : 18V) */
832 fe->ops->set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
833 last = (last) ? 0 : 1;
835 cmd = cmd >> 1;
836 if (i != 8)
837 dvb_frontend_sleep_until(&nexttime, 8000);
839 if (dvb_frontend_debug) {
840 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
841 __FUNCTION__, fe->dvb->num);
842 for (i = 1; i < 10; i++)
843 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
845 err = 0;
846 fepriv->state = FESTATE_DISEQC;
847 fepriv->status = 0;
849 break;
851 case FE_DISEQC_RECV_SLAVE_REPLY:
852 if (fe->ops->diseqc_recv_slave_reply)
853 err = fe->ops->diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
854 break;
856 case FE_ENABLE_HIGH_LNB_VOLTAGE:
857 if (fe->ops->enable_high_lnb_voltage)
858 err = fe->ops->enable_high_lnb_voltage(fe, (long) parg);
859 break;
861 case FE_SET_FRONTEND: {
862 struct dvb_frontend_tune_settings fetunesettings;
864 memcpy (&fepriv->parameters, parg,
865 sizeof (struct dvb_frontend_parameters));
867 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
868 memcpy(&fetunesettings.parameters, parg,
869 sizeof (struct dvb_frontend_parameters));
871 /* force auto frequency inversion if requested */
872 if (dvb_force_auto_inversion) {
873 fepriv->parameters.inversion = INVERSION_AUTO;
874 fetunesettings.parameters.inversion = INVERSION_AUTO;
876 if (fe->ops->info.type == FE_OFDM) {
877 /* without hierachical coding code_rate_LP is irrelevant,
878 * so we tolerate the otherwise invalid FEC_NONE setting */
879 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
880 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
881 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
884 /* get frontend-specific tuning settings */
885 if (fe->ops->get_tune_settings && (fe->ops->get_tune_settings(fe, &fetunesettings) == 0)) {
886 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
887 fepriv->max_drift = fetunesettings.max_drift;
888 fepriv->step_size = fetunesettings.step_size;
889 } else {
890 /* default values */
891 switch(fe->ops->info.type) {
892 case FE_QPSK:
893 fepriv->min_delay = HZ/20;
894 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
895 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
896 break;
898 case FE_QAM:
899 fepriv->min_delay = HZ/20;
900 fepriv->step_size = 0; /* no zigzag */
901 fepriv->max_drift = 0;
902 break;
904 case FE_OFDM:
905 fepriv->min_delay = HZ/20;
906 fepriv->step_size = fe->ops->info.frequency_stepsize * 2;
907 fepriv->max_drift = (fe->ops->info.frequency_stepsize * 2) + 1;
908 break;
909 case FE_ATSC:
910 printk("dvb-core: FE_ATSC not handled yet.\n");
911 break;
914 if (dvb_override_tune_delay > 0)
915 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
917 fepriv->state = FESTATE_RETUNE;
918 dvb_frontend_wakeup(fe);
919 dvb_frontend_add_event(fe, 0);
920 fepriv->status = 0;
921 err = 0;
922 break;
925 case FE_GET_EVENT:
926 err = dvb_frontend_get_event (fe, parg, file->f_flags);
927 break;
929 case FE_GET_FRONTEND:
930 if (fe->ops->get_frontend) {
931 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
932 err = fe->ops->get_frontend(fe, (struct dvb_frontend_parameters*) parg);
934 break;
936 case FE_SET_FRONTEND_TUNE_MODE:
937 fepriv->tune_mode_flags = (unsigned long) parg;
938 break;
941 up (&fepriv->sem);
942 return err;
945 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
947 struct dvb_device *dvbdev = file->private_data;
948 struct dvb_frontend *fe = dvbdev->priv;
949 struct dvb_frontend_private *fepriv = fe->frontend_priv;
951 dprintk ("%s\n", __FUNCTION__);
953 poll_wait (file, &fepriv->events.wait_queue, wait);
955 if (fepriv->events.eventw != fepriv->events.eventr)
956 return (POLLIN | POLLRDNORM | POLLPRI);
958 return 0;
961 static int dvb_frontend_open(struct inode *inode, struct file *file)
963 struct dvb_device *dvbdev = file->private_data;
964 struct dvb_frontend *fe = dvbdev->priv;
965 struct dvb_frontend_private *fepriv = fe->frontend_priv;
966 int ret;
968 dprintk ("%s\n", __FUNCTION__);
970 if ((ret = dvb_generic_open (inode, file)) < 0)
971 return ret;
973 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
974 ret = dvb_frontend_start (fe);
975 if (ret)
976 dvb_generic_release (inode, file);
978 /* empty event queue */
979 fepriv->events.eventr = fepriv->events.eventw = 0;
981 /* normal tune mode when opened R/W */
982 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
985 return ret;
988 static int dvb_frontend_release(struct inode *inode, struct file *file)
990 struct dvb_device *dvbdev = file->private_data;
991 struct dvb_frontend *fe = dvbdev->priv;
992 struct dvb_frontend_private *fepriv = fe->frontend_priv;
994 dprintk ("%s\n", __FUNCTION__);
996 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
997 fepriv->release_jiffies = jiffies;
999 return dvb_generic_release (inode, file);
1002 static struct file_operations dvb_frontend_fops = {
1003 .owner = THIS_MODULE,
1004 .ioctl = dvb_generic_ioctl,
1005 .poll = dvb_frontend_poll,
1006 .open = dvb_frontend_open,
1007 .release = dvb_frontend_release
1010 int dvb_register_frontend(struct dvb_adapter* dvb,
1011 struct dvb_frontend* fe)
1013 struct dvb_frontend_private *fepriv;
1014 static const struct dvb_device dvbdev_template = {
1015 .users = ~0,
1016 .writers = 1,
1017 .readers = (~0)-1,
1018 .fops = &dvb_frontend_fops,
1019 .kernel_ioctl = dvb_frontend_ioctl
1022 dprintk ("%s\n", __FUNCTION__);
1024 if (down_interruptible (&frontend_mutex))
1025 return -ERESTARTSYS;
1027 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1028 if (fe->frontend_priv == NULL) {
1029 up(&frontend_mutex);
1030 return -ENOMEM;
1032 fepriv = fe->frontend_priv;
1034 init_MUTEX (&fepriv->sem);
1035 init_waitqueue_head (&fepriv->wait_queue);
1036 init_waitqueue_head (&fepriv->events.wait_queue);
1037 init_MUTEX (&fepriv->events.sem);
1038 fe->dvb = dvb;
1039 fepriv->inversion = INVERSION_OFF;
1041 printk ("DVB: registering frontend %i (%s)...\n",
1042 fe->dvb->num,
1043 fe->ops->info.name);
1045 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1046 fe, DVB_DEVICE_FRONTEND);
1048 up (&frontend_mutex);
1049 return 0;
1051 EXPORT_SYMBOL(dvb_register_frontend);
1053 int dvb_unregister_frontend(struct dvb_frontend* fe)
1055 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1056 dprintk ("%s\n", __FUNCTION__);
1058 down (&frontend_mutex);
1059 dvb_unregister_device (fepriv->dvbdev);
1060 dvb_frontend_stop (fe);
1061 if (fe->ops->release)
1062 fe->ops->release(fe);
1063 else
1064 printk("dvb_frontend: Demodulator (%s) does not have a release callback!\n", fe->ops->info.name);
1065 /* fe is invalid now */
1066 kfree(fepriv);
1067 up (&frontend_mutex);
1068 return 0;
1070 EXPORT_SYMBOL(dvb_unregister_frontend);