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/list.h>
36 #include <linux/freezer.h>
37 #include <linux/jiffies.h>
38 #include <linux/kthread.h>
39 #include <asm/processor.h>
41 #include "dvb_frontend.h"
43 #include <linux/dvb/version.h>
45 static int dvb_frontend_debug
;
46 static int dvb_shutdown_timeout
;
47 static int dvb_force_auto_inversion
;
48 static int dvb_override_tune_delay
;
49 static int dvb_powerdown_on_sleep
= 1;
50 static int dvb_mfe_wait_time
= 5;
52 module_param_named(frontend_debug
, dvb_frontend_debug
, int, 0644);
53 MODULE_PARM_DESC(frontend_debug
, "Turn on/off frontend core debugging (default:off).");
54 module_param(dvb_shutdown_timeout
, int, 0644);
55 MODULE_PARM_DESC(dvb_shutdown_timeout
, "wait <shutdown_timeout> seconds after close() before suspending hardware");
56 module_param(dvb_force_auto_inversion
, int, 0644);
57 MODULE_PARM_DESC(dvb_force_auto_inversion
, "0: normal (default), 1: INVERSION_AUTO forced always");
58 module_param(dvb_override_tune_delay
, int, 0644);
59 MODULE_PARM_DESC(dvb_override_tune_delay
, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
60 module_param(dvb_powerdown_on_sleep
, int, 0644);
61 MODULE_PARM_DESC(dvb_powerdown_on_sleep
, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
62 module_param(dvb_mfe_wait_time
, int, 0644);
63 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)");
65 #define dprintk if (dvb_frontend_debug) printk
67 #define FESTATE_IDLE 1
68 #define FESTATE_RETUNE 2
69 #define FESTATE_TUNING_FAST 4
70 #define FESTATE_TUNING_SLOW 8
71 #define FESTATE_TUNED 16
72 #define FESTATE_ZIGZAG_FAST 32
73 #define FESTATE_ZIGZAG_SLOW 64
74 #define FESTATE_DISEQC 128
75 #define FESTATE_ERROR 256
76 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
77 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
78 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
79 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
83 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
84 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
85 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
86 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
87 * FESTATE_TUNED. The frontend has successfully locked on.
88 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
89 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
90 * FESTATE_DISEQC. A DISEQC command has just been issued.
91 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
92 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
93 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
94 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
97 static DEFINE_MUTEX(frontend_mutex
);
99 struct dvb_frontend_private
{
101 /* thread/frontend values */
102 struct dvb_device
*dvbdev
;
103 struct dvb_frontend_parameters parameters
;
104 struct dvb_fe_events events
;
105 struct semaphore sem
;
106 struct list_head list_head
;
107 wait_queue_head_t wait_queue
;
108 struct task_struct
*thread
;
109 unsigned long release_jiffies
;
113 unsigned long tune_mode_flags
;
115 unsigned int reinitialise
;
119 /* swzigzag values */
121 unsigned int bending
;
123 unsigned int inversion
;
124 unsigned int auto_step
;
125 unsigned int auto_sub_step
;
126 unsigned int started_auto_step
;
127 unsigned int min_delay
;
128 unsigned int max_drift
;
129 unsigned int step_size
;
131 unsigned int check_wrapped
;
132 enum dvbfe_search algo_status
;
135 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
);
137 static void dvb_frontend_add_event(struct dvb_frontend
*fe
, fe_status_t status
)
139 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
140 struct dvb_fe_events
*events
= &fepriv
->events
;
141 struct dvb_frontend_event
*e
;
144 dprintk ("%s\n", __func__
);
146 if (mutex_lock_interruptible (&events
->mtx
))
149 wp
= (events
->eventw
+ 1) % MAX_EVENT
;
151 if (wp
== events
->eventr
) {
152 events
->overflow
= 1;
153 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
156 e
= &events
->events
[events
->eventw
];
158 memcpy (&e
->parameters
, &fepriv
->parameters
,
159 sizeof (struct dvb_frontend_parameters
));
161 if (status
& FE_HAS_LOCK
)
162 if (fe
->ops
.get_frontend
)
163 fe
->ops
.get_frontend(fe
, &e
->parameters
);
167 mutex_unlock(&events
->mtx
);
171 wake_up_interruptible (&events
->wait_queue
);
174 static int dvb_frontend_get_event(struct dvb_frontend
*fe
,
175 struct dvb_frontend_event
*event
, int flags
)
177 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
178 struct dvb_fe_events
*events
= &fepriv
->events
;
180 dprintk ("%s\n", __func__
);
182 if (events
->overflow
) {
183 events
->overflow
= 0;
187 if (events
->eventw
== events
->eventr
) {
190 if (flags
& O_NONBLOCK
)
195 ret
= wait_event_interruptible (events
->wait_queue
,
196 events
->eventw
!= events
->eventr
);
198 if (down_interruptible (&fepriv
->sem
))
205 if (mutex_lock_interruptible (&events
->mtx
))
208 memcpy (event
, &events
->events
[events
->eventr
],
209 sizeof(struct dvb_frontend_event
));
211 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
213 mutex_unlock(&events
->mtx
);
218 static void dvb_frontend_init(struct dvb_frontend
*fe
)
220 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
227 if (fe
->ops
.tuner_ops
.init
) {
228 if (fe
->ops
.i2c_gate_ctrl
)
229 fe
->ops
.i2c_gate_ctrl(fe
, 1);
230 fe
->ops
.tuner_ops
.init(fe
);
231 if (fe
->ops
.i2c_gate_ctrl
)
232 fe
->ops
.i2c_gate_ctrl(fe
, 0);
236 void dvb_frontend_reinitialise(struct dvb_frontend
*fe
)
238 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
240 fepriv
->reinitialise
= 1;
241 dvb_frontend_wakeup(fe
);
243 EXPORT_SYMBOL(dvb_frontend_reinitialise
);
245 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private
*fepriv
, int locked
)
249 dprintk ("%s\n", __func__
);
252 (fepriv
->quality
) = (fepriv
->quality
* 220 + 36*256) / 256;
254 (fepriv
->quality
) = (fepriv
->quality
* 220 + 0) / 256;
256 q2
= fepriv
->quality
- 128;
259 fepriv
->delay
= fepriv
->min_delay
+ q2
* HZ
/ (128*128);
263 * Performs automatic twiddling of frontend parameters.
265 * @param fe The frontend concerned.
266 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
267 * @returns Number of complete iterations that have been performed.
269 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend
*fe
, int check_wrapped
)
274 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
275 int original_inversion
= fepriv
->parameters
.inversion
;
276 u32 original_frequency
= fepriv
->parameters
.frequency
;
278 /* are we using autoinversion? */
279 autoinversion
= ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
280 (fepriv
->parameters
.inversion
== INVERSION_AUTO
));
282 /* setup parameters correctly */
284 /* calculate the lnb_drift */
285 fepriv
->lnb_drift
= fepriv
->auto_step
* fepriv
->step_size
;
287 /* wrap the auto_step if we've exceeded the maximum drift */
288 if (fepriv
->lnb_drift
> fepriv
->max_drift
) {
289 fepriv
->auto_step
= 0;
290 fepriv
->auto_sub_step
= 0;
291 fepriv
->lnb_drift
= 0;
294 /* perform inversion and +/- zigzag */
295 switch(fepriv
->auto_sub_step
) {
297 /* try with the current inversion and current drift setting */
302 if (!autoinversion
) break;
304 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
309 if (fepriv
->lnb_drift
== 0) break;
311 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
316 if (fepriv
->lnb_drift
== 0) break;
317 if (!autoinversion
) break;
319 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
320 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
326 fepriv
->auto_sub_step
= -1; /* it'll be incremented to 0 in a moment */
330 if (!ready
) fepriv
->auto_sub_step
++;
333 /* if this attempt would hit where we started, indicate a complete
334 * iteration has occurred */
335 if ((fepriv
->auto_step
== fepriv
->started_auto_step
) &&
336 (fepriv
->auto_sub_step
== 0) && check_wrapped
) {
340 dprintk("%s: drift:%i inversion:%i auto_step:%i "
341 "auto_sub_step:%i started_auto_step:%i\n",
342 __func__
, fepriv
->lnb_drift
, fepriv
->inversion
,
343 fepriv
->auto_step
, fepriv
->auto_sub_step
, fepriv
->started_auto_step
);
345 /* set the frontend itself */
346 fepriv
->parameters
.frequency
+= fepriv
->lnb_drift
;
348 fepriv
->parameters
.inversion
= fepriv
->inversion
;
349 if (fe
->ops
.set_frontend
)
350 fe_set_err
= fe
->ops
.set_frontend(fe
, &fepriv
->parameters
);
351 if (fe_set_err
< 0) {
352 fepriv
->state
= FESTATE_ERROR
;
356 fepriv
->parameters
.frequency
= original_frequency
;
357 fepriv
->parameters
.inversion
= original_inversion
;
359 fepriv
->auto_sub_step
++;
363 static void dvb_frontend_swzigzag(struct dvb_frontend
*fe
)
367 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
369 /* if we've got no parameters, just keep idling */
370 if (fepriv
->state
& FESTATE_IDLE
) {
371 fepriv
->delay
= 3*HZ
;
376 /* in SCAN mode, we just set the frontend when asked and leave it alone */
377 if (fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
) {
378 if (fepriv
->state
& FESTATE_RETUNE
) {
379 if (fe
->ops
.set_frontend
)
380 retval
= fe
->ops
.set_frontend(fe
,
381 &fepriv
->parameters
);
383 fepriv
->state
= FESTATE_ERROR
;
385 fepriv
->state
= FESTATE_TUNED
;
387 fepriv
->delay
= 3*HZ
;
392 /* get the frontend status */
393 if (fepriv
->state
& FESTATE_RETUNE
) {
396 if (fe
->ops
.read_status
)
397 fe
->ops
.read_status(fe
, &s
);
398 if (s
!= fepriv
->status
) {
399 dvb_frontend_add_event(fe
, s
);
404 /* if we're not tuned, and we have a lock, move to the TUNED state */
405 if ((fepriv
->state
& FESTATE_WAITFORLOCK
) && (s
& FE_HAS_LOCK
)) {
406 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
407 fepriv
->state
= FESTATE_TUNED
;
409 /* if we're tuned, then we have determined the correct inversion */
410 if ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
411 (fepriv
->parameters
.inversion
== INVERSION_AUTO
)) {
412 fepriv
->parameters
.inversion
= fepriv
->inversion
;
417 /* if we are tuned already, check we're still locked */
418 if (fepriv
->state
& FESTATE_TUNED
) {
419 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
421 /* we're tuned, and the lock is still good... */
422 if (s
& FE_HAS_LOCK
) {
424 } else { /* if we _WERE_ tuned, but now don't have a lock */
425 fepriv
->state
= FESTATE_ZIGZAG_FAST
;
426 fepriv
->started_auto_step
= fepriv
->auto_step
;
427 fepriv
->check_wrapped
= 0;
431 /* don't actually do anything if we're in the LOSTLOCK state,
432 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
433 if ((fepriv
->state
& FESTATE_LOSTLOCK
) &&
434 (fe
->ops
.info
.caps
& FE_CAN_RECOVER
) && (fepriv
->max_drift
== 0)) {
435 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
439 /* don't do anything if we're in the DISEQC state, since this
440 * might be someone with a motorized dish controlled by DISEQC.
441 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
442 if (fepriv
->state
& FESTATE_DISEQC
) {
443 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
447 /* if we're in the RETUNE state, set everything up for a brand
448 * new scan, keeping the current inversion setting, as the next
449 * tune is _very_ likely to require the same */
450 if (fepriv
->state
& FESTATE_RETUNE
) {
451 fepriv
->lnb_drift
= 0;
452 fepriv
->auto_step
= 0;
453 fepriv
->auto_sub_step
= 0;
454 fepriv
->started_auto_step
= 0;
455 fepriv
->check_wrapped
= 0;
459 if ((fepriv
->state
& FESTATE_SEARCHING_FAST
) || (fepriv
->state
& FESTATE_RETUNE
)) {
460 fepriv
->delay
= fepriv
->min_delay
;
463 retval
= dvb_frontend_swzigzag_autotune(fe
,
464 fepriv
->check_wrapped
);
468 /* OK, if we've run out of trials at the fast speed.
469 * Drop back to slow for the _next_ attempt */
470 fepriv
->state
= FESTATE_SEARCHING_SLOW
;
471 fepriv
->started_auto_step
= fepriv
->auto_step
;
474 fepriv
->check_wrapped
= 1;
476 /* if we've just retuned, enter the ZIGZAG_FAST state.
477 * This ensures we cannot return from an
478 * FE_SET_FRONTEND ioctl before the first frontend tune
480 if (fepriv
->state
& FESTATE_RETUNE
) {
481 fepriv
->state
= FESTATE_TUNING_FAST
;
486 if (fepriv
->state
& FESTATE_SEARCHING_SLOW
) {
487 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
489 /* Note: don't bother checking for wrapping; we stay in this
490 * state until we get a lock */
491 dvb_frontend_swzigzag_autotune(fe
, 0);
495 static int dvb_frontend_is_exiting(struct dvb_frontend
*fe
)
497 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
502 if (fepriv
->dvbdev
->writers
== 1)
503 if (time_after(jiffies
, fepriv
->release_jiffies
+
504 dvb_shutdown_timeout
* HZ
))
510 static int dvb_frontend_should_wakeup(struct dvb_frontend
*fe
)
512 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
514 if (fepriv
->wakeup
) {
518 return dvb_frontend_is_exiting(fe
);
521 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
)
523 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
526 wake_up_interruptible(&fepriv
->wait_queue
);
529 static int dvb_frontend_thread(void *data
)
531 struct dvb_frontend
*fe
= data
;
532 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
533 unsigned long timeout
;
535 enum dvbfe_algo algo
;
537 struct dvb_frontend_parameters
*params
;
539 dprintk("%s\n", __func__
);
541 fepriv
->check_wrapped
= 0;
543 fepriv
->delay
= 3*HZ
;
546 fepriv
->reinitialise
= 0;
548 dvb_frontend_init(fe
);
552 up(&fepriv
->sem
); /* is locked when we enter the thread... */
554 timeout
= wait_event_interruptible_timeout(fepriv
->wait_queue
,
555 dvb_frontend_should_wakeup(fe
) || kthread_should_stop()
556 || freezing(current
),
559 if (kthread_should_stop() || dvb_frontend_is_exiting(fe
)) {
560 /* got signal or quitting */
568 if (down_interruptible(&fepriv
->sem
))
571 if (fepriv
->reinitialise
) {
572 dvb_frontend_init(fe
);
573 if (fepriv
->tone
!= -1) {
574 fe
->ops
.set_tone(fe
, fepriv
->tone
);
576 if (fepriv
->voltage
!= -1) {
577 fe
->ops
.set_voltage(fe
, fepriv
->voltage
);
579 fepriv
->reinitialise
= 0;
582 /* do an iteration of the tuning loop */
583 if (fe
->ops
.get_frontend_algo
) {
584 algo
= fe
->ops
.get_frontend_algo(fe
);
587 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__
);
588 params
= NULL
; /* have we been asked to RETUNE ? */
590 if (fepriv
->state
& FESTATE_RETUNE
) {
591 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__
);
592 params
= &fepriv
->parameters
;
593 fepriv
->state
= FESTATE_TUNED
;
597 fe
->ops
.tune(fe
, params
, fepriv
->tune_mode_flags
, &fepriv
->delay
, &s
);
599 if (s
!= fepriv
->status
&& !(fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
)) {
600 dprintk("%s: state changed, adding current state\n", __func__
);
601 dvb_frontend_add_event(fe
, s
);
606 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__
);
607 dvb_frontend_swzigzag(fe
);
609 case DVBFE_ALGO_CUSTOM
:
610 params
= NULL
; /* have we been asked to RETUNE ? */
611 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__
, fepriv
->state
);
612 if (fepriv
->state
& FESTATE_RETUNE
) {
613 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__
);
614 params
= &fepriv
->parameters
;
615 fepriv
->state
= FESTATE_TUNED
;
617 /* Case where we are going to search for a carrier
618 * User asked us to retune again for some reason, possibly
619 * requesting a search with a new set of parameters
621 if (fepriv
->algo_status
& DVBFE_ALGO_SEARCH_AGAIN
) {
622 if (fe
->ops
.search
) {
623 fepriv
->algo_status
= fe
->ops
.search(fe
, &fepriv
->parameters
);
624 /* We did do a search as was requested, the flags are
625 * now unset as well and has the flags wrt to search.
628 fepriv
->algo_status
&= ~DVBFE_ALGO_SEARCH_AGAIN
;
631 /* Track the carrier if the search was successful */
632 if (fepriv
->algo_status
== DVBFE_ALGO_SEARCH_SUCCESS
) {
634 fe
->ops
.track(fe
, &fepriv
->parameters
);
636 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
637 fepriv
->delay
= HZ
/ 2;
639 fe
->ops
.read_status(fe
, &s
);
640 if (s
!= fepriv
->status
) {
641 dvb_frontend_add_event(fe
, s
); /* update event list */
643 if (!(s
& FE_HAS_LOCK
)) {
644 fepriv
->delay
= HZ
/ 10;
645 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
647 fepriv
->delay
= 60 * HZ
;
652 dprintk("%s: UNDEFINED ALGO !\n", __func__
);
656 dvb_frontend_swzigzag(fe
);
660 if (dvb_powerdown_on_sleep
) {
661 if (fe
->ops
.set_voltage
)
662 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_OFF
);
663 if (fe
->ops
.tuner_ops
.sleep
) {
664 if (fe
->ops
.i2c_gate_ctrl
)
665 fe
->ops
.i2c_gate_ctrl(fe
, 1);
666 fe
->ops
.tuner_ops
.sleep(fe
);
667 if (fe
->ops
.i2c_gate_ctrl
)
668 fe
->ops
.i2c_gate_ctrl(fe
, 0);
674 fepriv
->thread
= NULL
;
678 dvb_frontend_wakeup(fe
);
682 static void dvb_frontend_stop(struct dvb_frontend
*fe
)
684 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
686 dprintk ("%s\n", __func__
);
694 kthread_stop(fepriv
->thread
);
696 init_MUTEX (&fepriv
->sem
);
697 fepriv
->state
= FESTATE_IDLE
;
699 /* paranoia check in case a signal arrived */
701 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
705 s32
timeval_usec_diff(struct timeval lasttime
, struct timeval curtime
)
707 return ((curtime
.tv_usec
< lasttime
.tv_usec
) ?
708 1000000 - lasttime
.tv_usec
+ curtime
.tv_usec
:
709 curtime
.tv_usec
- lasttime
.tv_usec
);
711 EXPORT_SYMBOL(timeval_usec_diff
);
713 static inline void timeval_usec_add(struct timeval
*curtime
, u32 add_usec
)
715 curtime
->tv_usec
+= add_usec
;
716 if (curtime
->tv_usec
>= 1000000) {
717 curtime
->tv_usec
-= 1000000;
723 * Sleep until gettimeofday() > waketime + add_usec
724 * This needs to be as precise as possible, but as the delay is
725 * usually between 2ms and 32ms, it is done using a scheduled msleep
726 * followed by usleep (normally a busy-wait loop) for the remainder
728 void dvb_frontend_sleep_until(struct timeval
*waketime
, u32 add_usec
)
730 struct timeval lasttime
;
733 timeval_usec_add(waketime
, add_usec
);
735 do_gettimeofday(&lasttime
);
736 delta
= timeval_usec_diff(lasttime
, *waketime
);
738 msleep((delta
- 1500) / 1000);
739 do_gettimeofday(&lasttime
);
740 newdelta
= timeval_usec_diff(lasttime
, *waketime
);
741 delta
= (newdelta
> delta
) ? 0 : newdelta
;
746 EXPORT_SYMBOL(dvb_frontend_sleep_until
);
748 static int dvb_frontend_start(struct dvb_frontend
*fe
)
751 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
752 struct task_struct
*fe_thread
;
754 dprintk ("%s\n", __func__
);
756 if (fepriv
->thread
) {
760 dvb_frontend_stop (fe
);
763 if (signal_pending(current
))
765 if (down_interruptible (&fepriv
->sem
))
768 fepriv
->state
= FESTATE_IDLE
;
770 fepriv
->thread
= NULL
;
773 fe_thread
= kthread_run(dvb_frontend_thread
, fe
,
774 "kdvb-ad-%i-fe-%i", fe
->dvb
->num
,fe
->id
);
775 if (IS_ERR(fe_thread
)) {
776 ret
= PTR_ERR(fe_thread
);
777 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret
);
781 fepriv
->thread
= fe_thread
;
785 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend
*fe
,
786 u32
*freq_min
, u32
*freq_max
)
788 *freq_min
= max(fe
->ops
.info
.frequency_min
, fe
->ops
.tuner_ops
.info
.frequency_min
);
790 if (fe
->ops
.info
.frequency_max
== 0)
791 *freq_max
= fe
->ops
.tuner_ops
.info
.frequency_max
;
792 else if (fe
->ops
.tuner_ops
.info
.frequency_max
== 0)
793 *freq_max
= fe
->ops
.info
.frequency_max
;
795 *freq_max
= min(fe
->ops
.info
.frequency_max
, fe
->ops
.tuner_ops
.info
.frequency_max
);
797 if (*freq_min
== 0 || *freq_max
== 0)
798 printk(KERN_WARNING
"DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
799 fe
->dvb
->num
,fe
->id
);
802 static int dvb_frontend_check_parameters(struct dvb_frontend
*fe
,
803 struct dvb_frontend_parameters
*parms
)
808 /* range check: frequency */
809 dvb_frontend_get_frequeny_limits(fe
, &freq_min
, &freq_max
);
810 if ((freq_min
&& parms
->frequency
< freq_min
) ||
811 (freq_max
&& parms
->frequency
> freq_max
)) {
812 printk(KERN_WARNING
"DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
813 fe
->dvb
->num
, fe
->id
, parms
->frequency
, freq_min
, freq_max
);
817 /* range check: symbol rate */
818 if (fe
->ops
.info
.type
== FE_QPSK
) {
819 if ((fe
->ops
.info
.symbol_rate_min
&&
820 parms
->u
.qpsk
.symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
821 (fe
->ops
.info
.symbol_rate_max
&&
822 parms
->u
.qpsk
.symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
823 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
824 fe
->dvb
->num
, fe
->id
, parms
->u
.qpsk
.symbol_rate
,
825 fe
->ops
.info
.symbol_rate_min
, fe
->ops
.info
.symbol_rate_max
);
829 } else if (fe
->ops
.info
.type
== FE_QAM
) {
830 if ((fe
->ops
.info
.symbol_rate_min
&&
831 parms
->u
.qam
.symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
832 (fe
->ops
.info
.symbol_rate_max
&&
833 parms
->u
.qam
.symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
834 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
835 fe
->dvb
->num
, fe
->id
, parms
->u
.qam
.symbol_rate
,
836 fe
->ops
.info
.symbol_rate_min
, fe
->ops
.info
.symbol_rate_max
);
841 /* check for supported modulation */
842 if (fe
->ops
.info
.type
== FE_QAM
&&
843 (parms
->u
.qam
.modulation
> QAM_AUTO
||
844 !((1 << (parms
->u
.qam
.modulation
+ 10)) & fe
->ops
.info
.caps
))) {
845 printk(KERN_WARNING
"DVB: adapter %i frontend %i modulation %u not supported\n",
846 fe
->dvb
->num
, fe
->id
, parms
->u
.qam
.modulation
);
853 static struct dtv_cmds_h dtv_cmds
[] = {
867 .name
= "DTV_FREQUENCY",
868 .cmd
= DTV_FREQUENCY
,
871 [DTV_BANDWIDTH_HZ
] = {
872 .name
= "DTV_BANDWIDTH_HZ",
873 .cmd
= DTV_BANDWIDTH_HZ
,
877 .name
= "DTV_MODULATION",
878 .cmd
= DTV_MODULATION
,
882 .name
= "DTV_INVERSION",
883 .cmd
= DTV_INVERSION
,
886 [DTV_DISEQC_MASTER
] = {
887 .name
= "DTV_DISEQC_MASTER",
888 .cmd
= DTV_DISEQC_MASTER
,
892 [DTV_SYMBOL_RATE
] = {
893 .name
= "DTV_SYMBOL_RATE",
894 .cmd
= DTV_SYMBOL_RATE
,
898 .name
= "DTV_INNER_FEC",
899 .cmd
= DTV_INNER_FEC
,
903 .name
= "DTV_VOLTAGE",
918 .name
= "DTV_ROLLOFF",
922 [DTV_DELIVERY_SYSTEM
] = {
923 .name
= "DTV_DELIVERY_SYSTEM",
924 .cmd
= DTV_DELIVERY_SYSTEM
,
928 .name
= "DTV_HIERARCHY",
929 .cmd
= DTV_HIERARCHY
,
932 [DTV_CODE_RATE_HP
] = {
933 .name
= "DTV_CODE_RATE_HP",
934 .cmd
= DTV_CODE_RATE_HP
,
937 [DTV_CODE_RATE_LP
] = {
938 .name
= "DTV_CODE_RATE_LP",
939 .cmd
= DTV_CODE_RATE_LP
,
942 [DTV_GUARD_INTERVAL
] = {
943 .name
= "DTV_GUARD_INTERVAL",
944 .cmd
= DTV_GUARD_INTERVAL
,
947 [DTV_TRANSMISSION_MODE
] = {
948 .name
= "DTV_TRANSMISSION_MODE",
949 .cmd
= DTV_TRANSMISSION_MODE
,
953 [DTV_DISEQC_SLAVE_REPLY
] = {
954 .name
= "DTV_DISEQC_SLAVE_REPLY",
955 .cmd
= DTV_DISEQC_SLAVE_REPLY
,
959 [DTV_API_VERSION
] = {
960 .name
= "DTV_API_VERSION",
961 .cmd
= DTV_API_VERSION
,
964 [DTV_CODE_RATE_HP
] = {
965 .name
= "DTV_CODE_RATE_HP",
966 .cmd
= DTV_CODE_RATE_HP
,
969 [DTV_CODE_RATE_LP
] = {
970 .name
= "DTV_CODE_RATE_LP",
971 .cmd
= DTV_CODE_RATE_LP
,
974 [DTV_GUARD_INTERVAL
] = {
975 .name
= "DTV_GUARD_INTERVAL",
976 .cmd
= DTV_GUARD_INTERVAL
,
979 [DTV_TRANSMISSION_MODE
] = {
980 .name
= "DTV_TRANSMISSION_MODE",
981 .cmd
= DTV_TRANSMISSION_MODE
,
985 .name
= "DTV_HIERARCHY",
986 .cmd
= DTV_HIERARCHY
,
991 static void dtv_property_dump(struct dtv_property
*tvp
)
995 if (tvp
->cmd
<= 0 || tvp
->cmd
> DTV_MAX_COMMAND
) {
996 printk(KERN_WARNING
"%s: tvp.cmd = 0x%08x undefined\n",
1001 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1004 ,dtv_cmds
[ tvp
->cmd
].name
);
1006 if(dtv_cmds
[ tvp
->cmd
].buffer
) {
1008 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1010 ,tvp
->u
.buffer
.len
);
1012 for(i
= 0; i
< tvp
->u
.buffer
.len
; i
++)
1013 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1016 ,tvp
->u
.buffer
.data
[i
]);
1019 dprintk("%s() tvp.u.data = 0x%08x\n", __func__
, tvp
->u
.data
);
1022 static int is_legacy_delivery_system(fe_delivery_system_t s
)
1024 if((s
== SYS_UNDEFINED
) || (s
== SYS_DVBC_ANNEX_AC
) ||
1025 (s
== SYS_DVBC_ANNEX_B
) || (s
== SYS_DVBT
) || (s
== SYS_DVBS
) ||
1032 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1033 * drivers can use a single set_frontend tuning function, regardless of whether
1034 * it's being used for the legacy or new API, reducing code and complexity.
1036 static void dtv_property_cache_sync(struct dvb_frontend
*fe
,
1037 struct dvb_frontend_parameters
*p
)
1039 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1041 c
->frequency
= p
->frequency
;
1042 c
->inversion
= p
->inversion
;
1044 switch (fe
->ops
.info
.type
) {
1046 c
->modulation
= QPSK
; /* implied for DVB-S in legacy API */
1047 c
->rolloff
= ROLLOFF_35
;/* implied for DVB-S */
1048 c
->symbol_rate
= p
->u
.qpsk
.symbol_rate
;
1049 c
->fec_inner
= p
->u
.qpsk
.fec_inner
;
1050 c
->delivery_system
= SYS_DVBS
;
1053 c
->symbol_rate
= p
->u
.qam
.symbol_rate
;
1054 c
->fec_inner
= p
->u
.qam
.fec_inner
;
1055 c
->modulation
= p
->u
.qam
.modulation
;
1056 c
->delivery_system
= SYS_DVBC_ANNEX_AC
;
1059 if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_6_MHZ
)
1060 c
->bandwidth_hz
= 6000000;
1061 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_7_MHZ
)
1062 c
->bandwidth_hz
= 7000000;
1063 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_8_MHZ
)
1064 c
->bandwidth_hz
= 8000000;
1066 /* Including BANDWIDTH_AUTO */
1067 c
->bandwidth_hz
= 0;
1068 c
->code_rate_HP
= p
->u
.ofdm
.code_rate_HP
;
1069 c
->code_rate_LP
= p
->u
.ofdm
.code_rate_LP
;
1070 c
->modulation
= p
->u
.ofdm
.constellation
;
1071 c
->transmission_mode
= p
->u
.ofdm
.transmission_mode
;
1072 c
->guard_interval
= p
->u
.ofdm
.guard_interval
;
1073 c
->hierarchy
= p
->u
.ofdm
.hierarchy_information
;
1074 c
->delivery_system
= SYS_DVBT
;
1077 c
->modulation
= p
->u
.vsb
.modulation
;
1078 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1079 c
->delivery_system
= SYS_ATSC
;
1081 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1086 /* Ensure the cached values are set correctly in the frontend
1087 * legacy tuning structures, for the advanced tuning API.
1089 static void dtv_property_legacy_params_sync(struct dvb_frontend
*fe
)
1091 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1092 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1093 struct dvb_frontend_parameters
*p
= &fepriv
->parameters
;
1095 p
->frequency
= c
->frequency
;
1096 p
->inversion
= c
->inversion
;
1098 switch (fe
->ops
.info
.type
) {
1100 dprintk("%s() Preparing QPSK req\n", __func__
);
1101 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1102 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1103 c
->delivery_system
= SYS_DVBS
;
1106 dprintk("%s() Preparing QAM req\n", __func__
);
1107 p
->u
.qam
.symbol_rate
= c
->symbol_rate
;
1108 p
->u
.qam
.fec_inner
= c
->fec_inner
;
1109 p
->u
.qam
.modulation
= c
->modulation
;
1110 c
->delivery_system
= SYS_DVBC_ANNEX_AC
;
1113 dprintk("%s() Preparing OFDM req\n", __func__
);
1114 if (c
->bandwidth_hz
== 6000000)
1115 p
->u
.ofdm
.bandwidth
= BANDWIDTH_6_MHZ
;
1116 else if (c
->bandwidth_hz
== 7000000)
1117 p
->u
.ofdm
.bandwidth
= BANDWIDTH_7_MHZ
;
1118 else if (c
->bandwidth_hz
== 8000000)
1119 p
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
1121 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1122 p
->u
.ofdm
.code_rate_HP
= c
->code_rate_HP
;
1123 p
->u
.ofdm
.code_rate_LP
= c
->code_rate_LP
;
1124 p
->u
.ofdm
.constellation
= c
->modulation
;
1125 p
->u
.ofdm
.transmission_mode
= c
->transmission_mode
;
1126 p
->u
.ofdm
.guard_interval
= c
->guard_interval
;
1127 p
->u
.ofdm
.hierarchy_information
= c
->hierarchy
;
1128 c
->delivery_system
= SYS_DVBT
;
1131 dprintk("%s() Preparing VSB req\n", __func__
);
1132 p
->u
.vsb
.modulation
= c
->modulation
;
1133 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1134 c
->delivery_system
= SYS_ATSC
;
1136 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1141 /* Ensure the cached values are set correctly in the frontend
1142 * legacy tuning structures, for the legacy tuning API.
1144 static void dtv_property_adv_params_sync(struct dvb_frontend
*fe
)
1146 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1147 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1148 struct dvb_frontend_parameters
*p
= &fepriv
->parameters
;
1150 p
->frequency
= c
->frequency
;
1151 p
->inversion
= c
->inversion
;
1153 switch(c
->modulation
) {
1158 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1159 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1165 if(c
->delivery_system
== SYS_ISDBT
) {
1166 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1167 p
->frequency
= c
->frequency
;
1168 p
->inversion
= INVERSION_AUTO
;
1169 p
->u
.ofdm
.constellation
= QAM_AUTO
;
1170 p
->u
.ofdm
.code_rate_HP
= FEC_AUTO
;
1171 p
->u
.ofdm
.code_rate_LP
= FEC_AUTO
;
1172 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1173 p
->u
.ofdm
.transmission_mode
= TRANSMISSION_MODE_AUTO
;
1174 p
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_AUTO
;
1175 p
->u
.ofdm
.hierarchy_information
= HIERARCHY_AUTO
;
1179 static void dtv_property_cache_submit(struct dvb_frontend
*fe
)
1181 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1183 /* For legacy delivery systems we don't need the delivery_system to
1184 * be specified, but we populate the older structures from the cache
1185 * so we can call set_frontend on older drivers.
1187 if(is_legacy_delivery_system(c
->delivery_system
)) {
1189 dprintk("%s() legacy, modulation = %d\n", __func__
, c
->modulation
);
1190 dtv_property_legacy_params_sync(fe
);
1193 dprintk("%s() adv, modulation = %d\n", __func__
, c
->modulation
);
1195 /* For advanced delivery systems / modulation types ...
1196 * we seed the lecacy dvb_frontend_parameters structure
1197 * so that the sanity checking code later in the IOCTL processing
1198 * can validate our basic frequency ranges, symbolrates, modulation
1201 dtv_property_adv_params_sync(fe
);
1205 static int dvb_frontend_ioctl_legacy(struct inode
*inode
, struct file
*file
,
1206 unsigned int cmd
, void *parg
);
1207 static int dvb_frontend_ioctl_properties(struct inode
*inode
, struct file
*file
,
1208 unsigned int cmd
, void *parg
);
1210 static int dtv_property_process_get(struct dvb_frontend
*fe
,
1211 struct dtv_property
*tvp
,
1212 struct inode
*inode
, struct file
*file
)
1216 dtv_property_dump(tvp
);
1218 /* Allow the frontend to validate incoming properties */
1219 if (fe
->ops
.get_property
)
1220 r
= fe
->ops
.get_property(fe
, tvp
);
1227 tvp
->u
.data
= fe
->dtv_property_cache
.frequency
;
1229 case DTV_MODULATION
:
1230 tvp
->u
.data
= fe
->dtv_property_cache
.modulation
;
1232 case DTV_BANDWIDTH_HZ
:
1233 tvp
->u
.data
= fe
->dtv_property_cache
.bandwidth_hz
;
1236 tvp
->u
.data
= fe
->dtv_property_cache
.inversion
;
1238 case DTV_SYMBOL_RATE
:
1239 tvp
->u
.data
= fe
->dtv_property_cache
.symbol_rate
;
1242 tvp
->u
.data
= fe
->dtv_property_cache
.fec_inner
;
1245 tvp
->u
.data
= fe
->dtv_property_cache
.pilot
;
1248 tvp
->u
.data
= fe
->dtv_property_cache
.rolloff
;
1250 case DTV_DELIVERY_SYSTEM
:
1251 tvp
->u
.data
= fe
->dtv_property_cache
.delivery_system
;
1254 tvp
->u
.data
= fe
->dtv_property_cache
.voltage
;
1257 tvp
->u
.data
= fe
->dtv_property_cache
.sectone
;
1259 case DTV_API_VERSION
:
1260 tvp
->u
.data
= (DVB_API_VERSION
<< 8) | DVB_API_VERSION_MINOR
;
1262 case DTV_CODE_RATE_HP
:
1263 tvp
->u
.data
= fe
->dtv_property_cache
.code_rate_HP
;
1265 case DTV_CODE_RATE_LP
:
1266 tvp
->u
.data
= fe
->dtv_property_cache
.code_rate_LP
;
1268 case DTV_GUARD_INTERVAL
:
1269 tvp
->u
.data
= fe
->dtv_property_cache
.guard_interval
;
1271 case DTV_TRANSMISSION_MODE
:
1272 tvp
->u
.data
= fe
->dtv_property_cache
.transmission_mode
;
1275 tvp
->u
.data
= fe
->dtv_property_cache
.hierarchy
;
1284 static int dtv_property_process_set(struct dvb_frontend
*fe
,
1285 struct dtv_property
*tvp
,
1286 struct inode
*inode
,
1290 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1291 dtv_property_dump(tvp
);
1293 /* Allow the frontend to validate incoming properties */
1294 if (fe
->ops
.set_property
)
1295 r
= fe
->ops
.set_property(fe
, tvp
);
1302 /* Reset a cache of data specific to the frontend here. This does
1303 * not effect hardware.
1305 dprintk("%s() Flushing property cache\n", __func__
);
1306 memset(&fe
->dtv_property_cache
, 0, sizeof(struct dtv_frontend_properties
));
1307 fe
->dtv_property_cache
.state
= tvp
->cmd
;
1308 fe
->dtv_property_cache
.delivery_system
= SYS_UNDEFINED
;
1311 /* interpret the cache of data, build either a traditional frontend
1312 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1315 fe
->dtv_property_cache
.state
= tvp
->cmd
;
1316 dprintk("%s() Finalised property cache\n", __func__
);
1317 dtv_property_cache_submit(fe
);
1319 r
|= dvb_frontend_ioctl_legacy(inode
, file
, FE_SET_FRONTEND
,
1320 &fepriv
->parameters
);
1323 fe
->dtv_property_cache
.frequency
= tvp
->u
.data
;
1325 case DTV_MODULATION
:
1326 fe
->dtv_property_cache
.modulation
= tvp
->u
.data
;
1328 case DTV_BANDWIDTH_HZ
:
1329 fe
->dtv_property_cache
.bandwidth_hz
= tvp
->u
.data
;
1332 fe
->dtv_property_cache
.inversion
= tvp
->u
.data
;
1334 case DTV_SYMBOL_RATE
:
1335 fe
->dtv_property_cache
.symbol_rate
= tvp
->u
.data
;
1338 fe
->dtv_property_cache
.fec_inner
= tvp
->u
.data
;
1341 fe
->dtv_property_cache
.pilot
= tvp
->u
.data
;
1344 fe
->dtv_property_cache
.rolloff
= tvp
->u
.data
;
1346 case DTV_DELIVERY_SYSTEM
:
1347 fe
->dtv_property_cache
.delivery_system
= tvp
->u
.data
;
1350 fe
->dtv_property_cache
.voltage
= tvp
->u
.data
;
1351 r
= dvb_frontend_ioctl_legacy(inode
, file
, FE_SET_VOLTAGE
,
1352 (void *)fe
->dtv_property_cache
.voltage
);
1355 fe
->dtv_property_cache
.sectone
= tvp
->u
.data
;
1356 r
= dvb_frontend_ioctl_legacy(inode
, file
, FE_SET_TONE
,
1357 (void *)fe
->dtv_property_cache
.sectone
);
1359 case DTV_CODE_RATE_HP
:
1360 fe
->dtv_property_cache
.code_rate_HP
= tvp
->u
.data
;
1362 case DTV_CODE_RATE_LP
:
1363 fe
->dtv_property_cache
.code_rate_LP
= tvp
->u
.data
;
1365 case DTV_GUARD_INTERVAL
:
1366 fe
->dtv_property_cache
.guard_interval
= tvp
->u
.data
;
1368 case DTV_TRANSMISSION_MODE
:
1369 fe
->dtv_property_cache
.transmission_mode
= tvp
->u
.data
;
1372 fe
->dtv_property_cache
.hierarchy
= tvp
->u
.data
;
1381 static int dvb_frontend_ioctl(struct inode
*inode
, struct file
*file
,
1382 unsigned int cmd
, void *parg
)
1384 struct dvb_device
*dvbdev
= file
->private_data
;
1385 struct dvb_frontend
*fe
= dvbdev
->priv
;
1386 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1387 int err
= -EOPNOTSUPP
;
1389 dprintk ("%s\n", __func__
);
1394 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
&&
1395 (_IOC_DIR(cmd
) != _IOC_READ
|| cmd
== FE_GET_EVENT
||
1396 cmd
== FE_DISEQC_RECV_SLAVE_REPLY
))
1399 if (down_interruptible (&fepriv
->sem
))
1400 return -ERESTARTSYS
;
1402 if ((cmd
== FE_SET_PROPERTY
) || (cmd
== FE_GET_PROPERTY
))
1403 err
= dvb_frontend_ioctl_properties(inode
, file
, cmd
, parg
);
1405 fe
->dtv_property_cache
.state
= DTV_UNDEFINED
;
1406 err
= dvb_frontend_ioctl_legacy(inode
, file
, cmd
, parg
);
1413 static int dvb_frontend_ioctl_properties(struct inode
*inode
, struct file
*file
,
1414 unsigned int cmd
, void *parg
)
1416 struct dvb_device
*dvbdev
= file
->private_data
;
1417 struct dvb_frontend
*fe
= dvbdev
->priv
;
1420 struct dtv_properties
*tvps
= NULL
;
1421 struct dtv_property
*tvp
= NULL
;
1424 dprintk("%s\n", __func__
);
1426 if(cmd
== FE_SET_PROPERTY
) {
1427 tvps
= (struct dtv_properties __user
*)parg
;
1429 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1430 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1432 /* Put an arbitrary limit on the number of messages that can
1433 * be sent at once */
1434 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1437 tvp
= (struct dtv_property
*) kmalloc(tvps
->num
*
1438 sizeof(struct dtv_property
), GFP_KERNEL
);
1444 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1449 for (i
= 0; i
< tvps
->num
; i
++) {
1450 (tvp
+ i
)->result
= dtv_property_process_set(fe
, tvp
+ i
, inode
, file
);
1451 err
|= (tvp
+ i
)->result
;
1454 if(fe
->dtv_property_cache
.state
== DTV_TUNE
)
1455 dprintk("%s() Property cache is full, tuning\n", __func__
);
1458 if(cmd
== FE_GET_PROPERTY
) {
1460 tvps
= (struct dtv_properties __user
*)parg
;
1462 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1463 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1465 /* Put an arbitrary limit on the number of messages that can
1466 * be sent at once */
1467 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1470 tvp
= (struct dtv_property
*) kmalloc(tvps
->num
*
1471 sizeof(struct dtv_property
), GFP_KERNEL
);
1477 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1482 for (i
= 0; i
< tvps
->num
; i
++) {
1483 (tvp
+ i
)->result
= dtv_property_process_get(fe
, tvp
+ i
, inode
, file
);
1484 err
|= (tvp
+ i
)->result
;
1487 if (copy_to_user(tvps
->props
, tvp
, tvps
->num
* sizeof(struct dtv_property
))) {
1500 static int dvb_frontend_ioctl_legacy(struct inode
*inode
, struct file
*file
,
1501 unsigned int cmd
, void *parg
)
1503 struct dvb_device
*dvbdev
= file
->private_data
;
1504 struct dvb_frontend
*fe
= dvbdev
->priv
;
1505 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1506 int err
= -EOPNOTSUPP
;
1510 struct dvb_frontend_info
* info
= parg
;
1511 memcpy(info
, &fe
->ops
.info
, sizeof(struct dvb_frontend_info
));
1512 dvb_frontend_get_frequeny_limits(fe
, &info
->frequency_min
, &info
->frequency_max
);
1514 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1515 * do it, it is done for it. */
1516 info
->caps
|= FE_CAN_INVERSION_AUTO
;
1521 case FE_READ_STATUS
: {
1522 fe_status_t
* status
= parg
;
1524 /* if retune was requested but hasn't occured yet, prevent
1525 * that user get signal state from previous tuning */
1526 if (fepriv
->state
== FESTATE_RETUNE
||
1527 fepriv
->state
== FESTATE_ERROR
) {
1533 if (fe
->ops
.read_status
)
1534 err
= fe
->ops
.read_status(fe
, status
);
1538 if (fe
->ops
.read_ber
)
1539 err
= fe
->ops
.read_ber(fe
, (__u32
*) parg
);
1542 case FE_READ_SIGNAL_STRENGTH
:
1543 if (fe
->ops
.read_signal_strength
)
1544 err
= fe
->ops
.read_signal_strength(fe
, (__u16
*) parg
);
1548 if (fe
->ops
.read_snr
)
1549 err
= fe
->ops
.read_snr(fe
, (__u16
*) parg
);
1552 case FE_READ_UNCORRECTED_BLOCKS
:
1553 if (fe
->ops
.read_ucblocks
)
1554 err
= fe
->ops
.read_ucblocks(fe
, (__u32
*) parg
);
1558 case FE_DISEQC_RESET_OVERLOAD
:
1559 if (fe
->ops
.diseqc_reset_overload
) {
1560 err
= fe
->ops
.diseqc_reset_overload(fe
);
1561 fepriv
->state
= FESTATE_DISEQC
;
1566 case FE_DISEQC_SEND_MASTER_CMD
:
1567 if (fe
->ops
.diseqc_send_master_cmd
) {
1568 err
= fe
->ops
.diseqc_send_master_cmd(fe
, (struct dvb_diseqc_master_cmd
*) parg
);
1569 fepriv
->state
= FESTATE_DISEQC
;
1574 case FE_DISEQC_SEND_BURST
:
1575 if (fe
->ops
.diseqc_send_burst
) {
1576 err
= fe
->ops
.diseqc_send_burst(fe
, (fe_sec_mini_cmd_t
) parg
);
1577 fepriv
->state
= FESTATE_DISEQC
;
1583 if (fe
->ops
.set_tone
) {
1584 err
= fe
->ops
.set_tone(fe
, (fe_sec_tone_mode_t
) parg
);
1585 fepriv
->tone
= (fe_sec_tone_mode_t
) parg
;
1586 fepriv
->state
= FESTATE_DISEQC
;
1591 case FE_SET_VOLTAGE
:
1592 if (fe
->ops
.set_voltage
) {
1593 err
= fe
->ops
.set_voltage(fe
, (fe_sec_voltage_t
) parg
);
1594 fepriv
->voltage
= (fe_sec_voltage_t
) parg
;
1595 fepriv
->state
= FESTATE_DISEQC
;
1600 case FE_DISHNETWORK_SEND_LEGACY_CMD
:
1601 if (fe
->ops
.dishnetwork_send_legacy_command
) {
1602 err
= fe
->ops
.dishnetwork_send_legacy_command(fe
, (unsigned long) parg
);
1603 fepriv
->state
= FESTATE_DISEQC
;
1605 } else if (fe
->ops
.set_voltage
) {
1607 * NOTE: This is a fallback condition. Some frontends
1608 * (stv0299 for instance) take longer than 8msec to
1609 * respond to a set_voltage command. Those switches
1610 * need custom routines to switch properly. For all
1611 * other frontends, the following shoule work ok.
1612 * Dish network legacy switches (as used by Dish500)
1613 * are controlled by sending 9-bit command words
1614 * spaced 8msec apart.
1615 * the actual command word is switch/port dependant
1616 * so it is up to the userspace application to send
1617 * the right command.
1618 * The command must always start with a '0' after
1619 * initialization, so parg is 8 bits and does not
1620 * include the initialization or start bit
1622 unsigned long swcmd
= ((unsigned long) parg
) << 1;
1623 struct timeval nexttime
;
1624 struct timeval tv
[10];
1627 if (dvb_frontend_debug
)
1628 printk("%s switch command: 0x%04lx\n", __func__
, swcmd
);
1629 do_gettimeofday(&nexttime
);
1630 if (dvb_frontend_debug
)
1631 memcpy(&tv
[0], &nexttime
, sizeof(struct timeval
));
1632 /* before sending a command, initialize by sending
1633 * a 32ms 18V to the switch
1635 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_18
);
1636 dvb_frontend_sleep_until(&nexttime
, 32000);
1638 for (i
= 0; i
< 9; i
++) {
1639 if (dvb_frontend_debug
)
1640 do_gettimeofday(&tv
[i
+ 1]);
1641 if ((swcmd
& 0x01) != last
) {
1642 /* set voltage to (last ? 13V : 18V) */
1643 fe
->ops
.set_voltage(fe
, (last
) ? SEC_VOLTAGE_13
: SEC_VOLTAGE_18
);
1644 last
= (last
) ? 0 : 1;
1648 dvb_frontend_sleep_until(&nexttime
, 8000);
1650 if (dvb_frontend_debug
) {
1651 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1652 __func__
, fe
->dvb
->num
);
1653 for (i
= 1; i
< 10; i
++)
1654 printk("%d: %d\n", i
, timeval_usec_diff(tv
[i
-1] , tv
[i
]));
1657 fepriv
->state
= FESTATE_DISEQC
;
1662 case FE_DISEQC_RECV_SLAVE_REPLY
:
1663 if (fe
->ops
.diseqc_recv_slave_reply
)
1664 err
= fe
->ops
.diseqc_recv_slave_reply(fe
, (struct dvb_diseqc_slave_reply
*) parg
);
1667 case FE_ENABLE_HIGH_LNB_VOLTAGE
:
1668 if (fe
->ops
.enable_high_lnb_voltage
)
1669 err
= fe
->ops
.enable_high_lnb_voltage(fe
, (long) parg
);
1672 case FE_SET_FRONTEND
: {
1673 struct dvb_frontend_tune_settings fetunesettings
;
1675 if(fe
->dtv_property_cache
.state
== DTV_TUNE
) {
1676 if (dvb_frontend_check_parameters(fe
, &fepriv
->parameters
) < 0) {
1681 if (dvb_frontend_check_parameters(fe
, parg
) < 0) {
1686 memcpy (&fepriv
->parameters
, parg
,
1687 sizeof (struct dvb_frontend_parameters
));
1688 dtv_property_cache_sync(fe
, &fepriv
->parameters
);
1691 memset(&fetunesettings
, 0, sizeof(struct dvb_frontend_tune_settings
));
1692 memcpy(&fetunesettings
.parameters
, parg
,
1693 sizeof (struct dvb_frontend_parameters
));
1695 /* force auto frequency inversion if requested */
1696 if (dvb_force_auto_inversion
) {
1697 fepriv
->parameters
.inversion
= INVERSION_AUTO
;
1698 fetunesettings
.parameters
.inversion
= INVERSION_AUTO
;
1700 if (fe
->ops
.info
.type
== FE_OFDM
) {
1701 /* without hierarchical coding code_rate_LP is irrelevant,
1702 * so we tolerate the otherwise invalid FEC_NONE setting */
1703 if (fepriv
->parameters
.u
.ofdm
.hierarchy_information
== HIERARCHY_NONE
&&
1704 fepriv
->parameters
.u
.ofdm
.code_rate_LP
== FEC_NONE
)
1705 fepriv
->parameters
.u
.ofdm
.code_rate_LP
= FEC_AUTO
;
1708 /* get frontend-specific tuning settings */
1709 if (fe
->ops
.get_tune_settings
&& (fe
->ops
.get_tune_settings(fe
, &fetunesettings
) == 0)) {
1710 fepriv
->min_delay
= (fetunesettings
.min_delay_ms
* HZ
) / 1000;
1711 fepriv
->max_drift
= fetunesettings
.max_drift
;
1712 fepriv
->step_size
= fetunesettings
.step_size
;
1714 /* default values */
1715 switch(fe
->ops
.info
.type
) {
1717 fepriv
->min_delay
= HZ
/20;
1718 fepriv
->step_size
= fepriv
->parameters
.u
.qpsk
.symbol_rate
/ 16000;
1719 fepriv
->max_drift
= fepriv
->parameters
.u
.qpsk
.symbol_rate
/ 2000;
1723 fepriv
->min_delay
= HZ
/20;
1724 fepriv
->step_size
= 0; /* no zigzag */
1725 fepriv
->max_drift
= 0;
1729 fepriv
->min_delay
= HZ
/20;
1730 fepriv
->step_size
= fe
->ops
.info
.frequency_stepsize
* 2;
1731 fepriv
->max_drift
= (fe
->ops
.info
.frequency_stepsize
* 2) + 1;
1734 fepriv
->min_delay
= HZ
/20;
1735 fepriv
->step_size
= 0;
1736 fepriv
->max_drift
= 0;
1740 if (dvb_override_tune_delay
> 0)
1741 fepriv
->min_delay
= (dvb_override_tune_delay
* HZ
) / 1000;
1743 fepriv
->state
= FESTATE_RETUNE
;
1745 /* Request the search algorithm to search */
1746 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
1748 dvb_frontend_wakeup(fe
);
1749 dvb_frontend_add_event(fe
, 0);
1756 err
= dvb_frontend_get_event (fe
, parg
, file
->f_flags
);
1759 case FE_GET_FRONTEND
:
1760 if (fe
->ops
.get_frontend
) {
1761 memcpy (parg
, &fepriv
->parameters
, sizeof (struct dvb_frontend_parameters
));
1762 err
= fe
->ops
.get_frontend(fe
, (struct dvb_frontend_parameters
*) parg
);
1766 case FE_SET_FRONTEND_TUNE_MODE
:
1767 fepriv
->tune_mode_flags
= (unsigned long) parg
;
1776 static unsigned int dvb_frontend_poll(struct file
*file
, struct poll_table_struct
*wait
)
1778 struct dvb_device
*dvbdev
= file
->private_data
;
1779 struct dvb_frontend
*fe
= dvbdev
->priv
;
1780 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1782 dprintk ("%s\n", __func__
);
1784 poll_wait (file
, &fepriv
->events
.wait_queue
, wait
);
1786 if (fepriv
->events
.eventw
!= fepriv
->events
.eventr
)
1787 return (POLLIN
| POLLRDNORM
| POLLPRI
);
1792 static int dvb_frontend_open(struct inode
*inode
, struct file
*file
)
1794 struct dvb_device
*dvbdev
= file
->private_data
;
1795 struct dvb_frontend
*fe
= dvbdev
->priv
;
1796 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1797 struct dvb_adapter
*adapter
= fe
->dvb
;
1800 dprintk ("%s\n", __func__
);
1802 if (adapter
->mfe_shared
) {
1803 mutex_lock (&adapter
->mfe_lock
);
1805 if (adapter
->mfe_dvbdev
== NULL
)
1806 adapter
->mfe_dvbdev
= dvbdev
;
1808 else if (adapter
->mfe_dvbdev
!= dvbdev
) {
1810 *mfedev
= adapter
->mfe_dvbdev
;
1812 *mfe
= mfedev
->priv
;
1813 struct dvb_frontend_private
1814 *mfepriv
= mfe
->frontend_priv
;
1815 int mferetry
= (dvb_mfe_wait_time
<< 1);
1817 mutex_unlock (&adapter
->mfe_lock
);
1818 while (mferetry
-- && (mfedev
->users
!= -1 ||
1819 mfepriv
->thread
!= NULL
)) {
1820 if(msleep_interruptible(500)) {
1821 if(signal_pending(current
))
1826 mutex_lock (&adapter
->mfe_lock
);
1827 if(adapter
->mfe_dvbdev
!= dvbdev
) {
1828 mfedev
= adapter
->mfe_dvbdev
;
1830 mfepriv
= mfe
->frontend_priv
;
1831 if (mfedev
->users
!= -1 ||
1832 mfepriv
->thread
!= NULL
) {
1833 mutex_unlock (&adapter
->mfe_lock
);
1836 adapter
->mfe_dvbdev
= dvbdev
;
1841 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
) {
1842 if ((ret
= fe
->ops
.ts_bus_ctrl(fe
, 1)) < 0)
1846 if ((ret
= dvb_generic_open (inode
, file
)) < 0)
1849 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
1850 /* normal tune mode when opened R/W */
1851 fepriv
->tune_mode_flags
&= ~FE_TUNE_MODE_ONESHOT
;
1853 fepriv
->voltage
= -1;
1855 ret
= dvb_frontend_start (fe
);
1859 /* empty event queue */
1860 fepriv
->events
.eventr
= fepriv
->events
.eventw
= 0;
1863 if (adapter
->mfe_shared
)
1864 mutex_unlock (&adapter
->mfe_lock
);
1868 dvb_generic_release(inode
, file
);
1870 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
)
1871 fe
->ops
.ts_bus_ctrl(fe
, 0);
1873 if (adapter
->mfe_shared
)
1874 mutex_unlock (&adapter
->mfe_lock
);
1878 static int dvb_frontend_release(struct inode
*inode
, struct file
*file
)
1880 struct dvb_device
*dvbdev
= file
->private_data
;
1881 struct dvb_frontend
*fe
= dvbdev
->priv
;
1882 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1885 dprintk ("%s\n", __func__
);
1887 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
)
1888 fepriv
->release_jiffies
= jiffies
;
1890 ret
= dvb_generic_release (inode
, file
);
1892 if (dvbdev
->users
== -1) {
1893 if (fepriv
->exit
== 1) {
1894 fops_put(file
->f_op
);
1896 wake_up(&dvbdev
->wait_queue
);
1898 if (fe
->ops
.ts_bus_ctrl
)
1899 fe
->ops
.ts_bus_ctrl(fe
, 0);
1905 static const struct file_operations dvb_frontend_fops
= {
1906 .owner
= THIS_MODULE
,
1907 .ioctl
= dvb_generic_ioctl
,
1908 .poll
= dvb_frontend_poll
,
1909 .open
= dvb_frontend_open
,
1910 .release
= dvb_frontend_release
1913 int dvb_register_frontend(struct dvb_adapter
* dvb
,
1914 struct dvb_frontend
* fe
)
1916 struct dvb_frontend_private
*fepriv
;
1917 static const struct dvb_device dvbdev_template
= {
1921 .fops
= &dvb_frontend_fops
,
1922 .kernel_ioctl
= dvb_frontend_ioctl
1925 dprintk ("%s\n", __func__
);
1927 if (mutex_lock_interruptible(&frontend_mutex
))
1928 return -ERESTARTSYS
;
1930 fe
->frontend_priv
= kzalloc(sizeof(struct dvb_frontend_private
), GFP_KERNEL
);
1931 if (fe
->frontend_priv
== NULL
) {
1932 mutex_unlock(&frontend_mutex
);
1935 fepriv
= fe
->frontend_priv
;
1937 init_MUTEX (&fepriv
->sem
);
1938 init_waitqueue_head (&fepriv
->wait_queue
);
1939 init_waitqueue_head (&fepriv
->events
.wait_queue
);
1940 mutex_init(&fepriv
->events
.mtx
);
1942 fepriv
->inversion
= INVERSION_OFF
;
1944 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1949 dvb_register_device (fe
->dvb
, &fepriv
->dvbdev
, &dvbdev_template
,
1950 fe
, DVB_DEVICE_FRONTEND
);
1952 mutex_unlock(&frontend_mutex
);
1955 EXPORT_SYMBOL(dvb_register_frontend
);
1957 int dvb_unregister_frontend(struct dvb_frontend
* fe
)
1959 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1960 dprintk ("%s\n", __func__
);
1962 mutex_lock(&frontend_mutex
);
1963 dvb_frontend_stop (fe
);
1964 mutex_unlock(&frontend_mutex
);
1966 if (fepriv
->dvbdev
->users
< -1)
1967 wait_event(fepriv
->dvbdev
->wait_queue
,
1968 fepriv
->dvbdev
->users
==-1);
1970 mutex_lock(&frontend_mutex
);
1971 dvb_unregister_device (fepriv
->dvbdev
);
1973 /* fe is invalid now */
1975 mutex_unlock(&frontend_mutex
);
1978 EXPORT_SYMBOL(dvb_unregister_frontend
);
1980 #ifdef CONFIG_MEDIA_ATTACH
1981 void dvb_frontend_detach(struct dvb_frontend
* fe
)
1985 if (fe
->ops
.release_sec
) {
1986 fe
->ops
.release_sec(fe
);
1987 symbol_put_addr(fe
->ops
.release_sec
);
1989 if (fe
->ops
.tuner_ops
.release
) {
1990 fe
->ops
.tuner_ops
.release(fe
);
1991 symbol_put_addr(fe
->ops
.tuner_ops
.release
);
1993 if (fe
->ops
.analog_ops
.release
) {
1994 fe
->ops
.analog_ops
.release(fe
);
1995 symbol_put_addr(fe
->ops
.analog_ops
.release
);
1997 ptr
= (void*)fe
->ops
.release
;
1999 fe
->ops
.release(fe
);
2000 symbol_put_addr(ptr
);
2004 void dvb_frontend_detach(struct dvb_frontend
* fe
)
2006 if (fe
->ops
.release_sec
)
2007 fe
->ops
.release_sec(fe
);
2008 if (fe
->ops
.tuner_ops
.release
)
2009 fe
->ops
.tuner_ops
.release(fe
);
2010 if (fe
->ops
.analog_ops
.release
)
2011 fe
->ops
.analog_ops
.release(fe
);
2012 if (fe
->ops
.release
)
2013 fe
->ops
.release(fe
);
2016 EXPORT_SYMBOL(dvb_frontend_detach
);