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_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
76 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
77 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
78 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
82 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
83 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
84 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
85 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
86 * FESTATE_TUNED. The frontend has successfully locked on.
87 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
88 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
89 * FESTATE_DISEQC. A DISEQC command has just been issued.
90 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
91 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
92 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
93 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
96 static DEFINE_MUTEX(frontend_mutex
);
98 struct dvb_frontend_private
{
100 /* thread/frontend values */
101 struct dvb_device
*dvbdev
;
102 struct dvb_frontend_parameters parameters
;
103 struct dvb_fe_events events
;
104 struct semaphore sem
;
105 struct list_head list_head
;
106 wait_queue_head_t wait_queue
;
107 struct task_struct
*thread
;
108 unsigned long release_jiffies
;
112 unsigned long tune_mode_flags
;
114 unsigned int reinitialise
;
118 /* swzigzag values */
120 unsigned int bending
;
122 unsigned int inversion
;
123 unsigned int auto_step
;
124 unsigned int auto_sub_step
;
125 unsigned int started_auto_step
;
126 unsigned int min_delay
;
127 unsigned int max_drift
;
128 unsigned int step_size
;
130 unsigned int check_wrapped
;
131 enum dvbfe_search algo_status
;
134 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
);
136 static void dvb_frontend_add_event(struct dvb_frontend
*fe
, fe_status_t status
)
138 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
139 struct dvb_fe_events
*events
= &fepriv
->events
;
140 struct dvb_frontend_event
*e
;
143 dprintk ("%s\n", __func__
);
145 if (mutex_lock_interruptible (&events
->mtx
))
148 wp
= (events
->eventw
+ 1) % MAX_EVENT
;
150 if (wp
== events
->eventr
) {
151 events
->overflow
= 1;
152 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
155 e
= &events
->events
[events
->eventw
];
157 memcpy (&e
->parameters
, &fepriv
->parameters
,
158 sizeof (struct dvb_frontend_parameters
));
160 if (status
& FE_HAS_LOCK
)
161 if (fe
->ops
.get_frontend
)
162 fe
->ops
.get_frontend(fe
, &e
->parameters
);
166 mutex_unlock(&events
->mtx
);
170 wake_up_interruptible (&events
->wait_queue
);
173 static int dvb_frontend_get_event(struct dvb_frontend
*fe
,
174 struct dvb_frontend_event
*event
, int flags
)
176 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
177 struct dvb_fe_events
*events
= &fepriv
->events
;
179 dprintk ("%s\n", __func__
);
181 if (events
->overflow
) {
182 events
->overflow
= 0;
186 if (events
->eventw
== events
->eventr
) {
189 if (flags
& O_NONBLOCK
)
194 ret
= wait_event_interruptible (events
->wait_queue
,
195 events
->eventw
!= events
->eventr
);
197 if (down_interruptible (&fepriv
->sem
))
204 if (mutex_lock_interruptible (&events
->mtx
))
207 memcpy (event
, &events
->events
[events
->eventr
],
208 sizeof(struct dvb_frontend_event
));
210 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
212 mutex_unlock(&events
->mtx
);
217 static void dvb_frontend_init(struct dvb_frontend
*fe
)
219 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
226 if (fe
->ops
.tuner_ops
.init
) {
227 if (fe
->ops
.i2c_gate_ctrl
)
228 fe
->ops
.i2c_gate_ctrl(fe
, 1);
229 fe
->ops
.tuner_ops
.init(fe
);
230 if (fe
->ops
.i2c_gate_ctrl
)
231 fe
->ops
.i2c_gate_ctrl(fe
, 0);
235 void dvb_frontend_reinitialise(struct dvb_frontend
*fe
)
237 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
239 fepriv
->reinitialise
= 1;
240 dvb_frontend_wakeup(fe
);
242 EXPORT_SYMBOL(dvb_frontend_reinitialise
);
244 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private
*fepriv
, int locked
)
248 dprintk ("%s\n", __func__
);
251 (fepriv
->quality
) = (fepriv
->quality
* 220 + 36*256) / 256;
253 (fepriv
->quality
) = (fepriv
->quality
* 220 + 0) / 256;
255 q2
= fepriv
->quality
- 128;
258 fepriv
->delay
= fepriv
->min_delay
+ q2
* HZ
/ (128*128);
262 * Performs automatic twiddling of frontend parameters.
264 * @param fe The frontend concerned.
265 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
266 * @returns Number of complete iterations that have been performed.
268 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend
*fe
, int check_wrapped
)
272 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
273 int original_inversion
= fepriv
->parameters
.inversion
;
274 u32 original_frequency
= fepriv
->parameters
.frequency
;
276 /* are we using autoinversion? */
277 autoinversion
= ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
278 (fepriv
->parameters
.inversion
== INVERSION_AUTO
));
280 /* setup parameters correctly */
282 /* calculate the lnb_drift */
283 fepriv
->lnb_drift
= fepriv
->auto_step
* fepriv
->step_size
;
285 /* wrap the auto_step if we've exceeded the maximum drift */
286 if (fepriv
->lnb_drift
> fepriv
->max_drift
) {
287 fepriv
->auto_step
= 0;
288 fepriv
->auto_sub_step
= 0;
289 fepriv
->lnb_drift
= 0;
292 /* perform inversion and +/- zigzag */
293 switch(fepriv
->auto_sub_step
) {
295 /* try with the current inversion and current drift setting */
300 if (!autoinversion
) break;
302 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
307 if (fepriv
->lnb_drift
== 0) break;
309 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
314 if (fepriv
->lnb_drift
== 0) break;
315 if (!autoinversion
) break;
317 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
318 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
324 fepriv
->auto_sub_step
= -1; /* it'll be incremented to 0 in a moment */
328 if (!ready
) fepriv
->auto_sub_step
++;
331 /* if this attempt would hit where we started, indicate a complete
332 * iteration has occurred */
333 if ((fepriv
->auto_step
== fepriv
->started_auto_step
) &&
334 (fepriv
->auto_sub_step
== 0) && check_wrapped
) {
338 dprintk("%s: drift:%i inversion:%i auto_step:%i "
339 "auto_sub_step:%i started_auto_step:%i\n",
340 __func__
, fepriv
->lnb_drift
, fepriv
->inversion
,
341 fepriv
->auto_step
, fepriv
->auto_sub_step
, fepriv
->started_auto_step
);
343 /* set the frontend itself */
344 fepriv
->parameters
.frequency
+= fepriv
->lnb_drift
;
346 fepriv
->parameters
.inversion
= fepriv
->inversion
;
347 if (fe
->ops
.set_frontend
)
348 fe
->ops
.set_frontend(fe
, &fepriv
->parameters
);
350 fepriv
->parameters
.frequency
= original_frequency
;
351 fepriv
->parameters
.inversion
= original_inversion
;
353 fepriv
->auto_sub_step
++;
357 static void dvb_frontend_swzigzag(struct dvb_frontend
*fe
)
360 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
362 /* if we've got no parameters, just keep idling */
363 if (fepriv
->state
& FESTATE_IDLE
) {
364 fepriv
->delay
= 3*HZ
;
369 /* in SCAN mode, we just set the frontend when asked and leave it alone */
370 if (fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
) {
371 if (fepriv
->state
& FESTATE_RETUNE
) {
372 if (fe
->ops
.set_frontend
)
373 fe
->ops
.set_frontend(fe
, &fepriv
->parameters
);
374 fepriv
->state
= FESTATE_TUNED
;
376 fepriv
->delay
= 3*HZ
;
381 /* get the frontend status */
382 if (fepriv
->state
& FESTATE_RETUNE
) {
385 if (fe
->ops
.read_status
)
386 fe
->ops
.read_status(fe
, &s
);
387 if (s
!= fepriv
->status
) {
388 dvb_frontend_add_event(fe
, s
);
393 /* if we're not tuned, and we have a lock, move to the TUNED state */
394 if ((fepriv
->state
& FESTATE_WAITFORLOCK
) && (s
& FE_HAS_LOCK
)) {
395 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
396 fepriv
->state
= FESTATE_TUNED
;
398 /* if we're tuned, then we have determined the correct inversion */
399 if ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
400 (fepriv
->parameters
.inversion
== INVERSION_AUTO
)) {
401 fepriv
->parameters
.inversion
= fepriv
->inversion
;
406 /* if we are tuned already, check we're still locked */
407 if (fepriv
->state
& FESTATE_TUNED
) {
408 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
410 /* we're tuned, and the lock is still good... */
411 if (s
& FE_HAS_LOCK
) {
413 } else { /* if we _WERE_ tuned, but now don't have a lock */
414 fepriv
->state
= FESTATE_ZIGZAG_FAST
;
415 fepriv
->started_auto_step
= fepriv
->auto_step
;
416 fepriv
->check_wrapped
= 0;
420 /* don't actually do anything if we're in the LOSTLOCK state,
421 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
422 if ((fepriv
->state
& FESTATE_LOSTLOCK
) &&
423 (fe
->ops
.info
.caps
& FE_CAN_RECOVER
) && (fepriv
->max_drift
== 0)) {
424 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
428 /* don't do anything if we're in the DISEQC state, since this
429 * might be someone with a motorized dish controlled by DISEQC.
430 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
431 if (fepriv
->state
& FESTATE_DISEQC
) {
432 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
436 /* if we're in the RETUNE state, set everything up for a brand
437 * new scan, keeping the current inversion setting, as the next
438 * tune is _very_ likely to require the same */
439 if (fepriv
->state
& FESTATE_RETUNE
) {
440 fepriv
->lnb_drift
= 0;
441 fepriv
->auto_step
= 0;
442 fepriv
->auto_sub_step
= 0;
443 fepriv
->started_auto_step
= 0;
444 fepriv
->check_wrapped
= 0;
448 if ((fepriv
->state
& FESTATE_SEARCHING_FAST
) || (fepriv
->state
& FESTATE_RETUNE
)) {
449 fepriv
->delay
= fepriv
->min_delay
;
452 if (dvb_frontend_swzigzag_autotune(fe
, fepriv
->check_wrapped
)) {
453 /* OK, if we've run out of trials at the fast speed.
454 * Drop back to slow for the _next_ attempt */
455 fepriv
->state
= FESTATE_SEARCHING_SLOW
;
456 fepriv
->started_auto_step
= fepriv
->auto_step
;
459 fepriv
->check_wrapped
= 1;
461 /* if we've just retuned, enter the ZIGZAG_FAST state.
462 * This ensures we cannot return from an
463 * FE_SET_FRONTEND ioctl before the first frontend tune
465 if (fepriv
->state
& FESTATE_RETUNE
) {
466 fepriv
->state
= FESTATE_TUNING_FAST
;
471 if (fepriv
->state
& FESTATE_SEARCHING_SLOW
) {
472 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
474 /* Note: don't bother checking for wrapping; we stay in this
475 * state until we get a lock */
476 dvb_frontend_swzigzag_autotune(fe
, 0);
480 static int dvb_frontend_is_exiting(struct dvb_frontend
*fe
)
482 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
487 if (fepriv
->dvbdev
->writers
== 1)
488 if (time_after(jiffies
, fepriv
->release_jiffies
+
489 dvb_shutdown_timeout
* HZ
))
495 static int dvb_frontend_should_wakeup(struct dvb_frontend
*fe
)
497 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
499 if (fepriv
->wakeup
) {
503 return dvb_frontend_is_exiting(fe
);
506 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
)
508 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
511 wake_up_interruptible(&fepriv
->wait_queue
);
514 static int dvb_frontend_thread(void *data
)
516 struct dvb_frontend
*fe
= data
;
517 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
518 unsigned long timeout
;
520 enum dvbfe_algo algo
;
522 struct dvb_frontend_parameters
*params
;
524 dprintk("%s\n", __func__
);
526 fepriv
->check_wrapped
= 0;
528 fepriv
->delay
= 3*HZ
;
531 fepriv
->reinitialise
= 0;
533 dvb_frontend_init(fe
);
537 up(&fepriv
->sem
); /* is locked when we enter the thread... */
539 timeout
= wait_event_interruptible_timeout(fepriv
->wait_queue
,
540 dvb_frontend_should_wakeup(fe
) || kthread_should_stop()
541 || freezing(current
),
544 if (kthread_should_stop() || dvb_frontend_is_exiting(fe
)) {
545 /* got signal or quitting */
553 if (down_interruptible(&fepriv
->sem
))
556 if (fepriv
->reinitialise
) {
557 dvb_frontend_init(fe
);
558 if (fepriv
->tone
!= -1) {
559 fe
->ops
.set_tone(fe
, fepriv
->tone
);
561 if (fepriv
->voltage
!= -1) {
562 fe
->ops
.set_voltage(fe
, fepriv
->voltage
);
564 fepriv
->reinitialise
= 0;
567 /* do an iteration of the tuning loop */
568 if (fe
->ops
.get_frontend_algo
) {
569 algo
= fe
->ops
.get_frontend_algo(fe
);
572 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__
);
573 params
= NULL
; /* have we been asked to RETUNE ? */
575 if (fepriv
->state
& FESTATE_RETUNE
) {
576 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__
);
577 params
= &fepriv
->parameters
;
578 fepriv
->state
= FESTATE_TUNED
;
582 fe
->ops
.tune(fe
, params
, fepriv
->tune_mode_flags
, &fepriv
->delay
, &s
);
584 if (s
!= fepriv
->status
&& !(fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
)) {
585 dprintk("%s: state changed, adding current state\n", __func__
);
586 dvb_frontend_add_event(fe
, s
);
591 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__
);
592 dvb_frontend_swzigzag(fe
);
594 case DVBFE_ALGO_CUSTOM
:
595 params
= NULL
; /* have we been asked to RETUNE ? */
596 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__
, fepriv
->state
);
597 if (fepriv
->state
& FESTATE_RETUNE
) {
598 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__
);
599 params
= &fepriv
->parameters
;
600 fepriv
->state
= FESTATE_TUNED
;
602 /* Case where we are going to search for a carrier
603 * User asked us to retune again for some reason, possibly
604 * requesting a search with a new set of parameters
606 if (fepriv
->algo_status
& DVBFE_ALGO_SEARCH_AGAIN
) {
607 if (fe
->ops
.search
) {
608 fepriv
->algo_status
= fe
->ops
.search(fe
, &fepriv
->parameters
);
609 /* We did do a search as was requested, the flags are
610 * now unset as well and has the flags wrt to search.
613 fepriv
->algo_status
&= ~DVBFE_ALGO_SEARCH_AGAIN
;
616 /* Track the carrier if the search was successful */
617 if (fepriv
->algo_status
== DVBFE_ALGO_SEARCH_SUCCESS
) {
619 fe
->ops
.track(fe
, &fepriv
->parameters
);
621 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
622 fepriv
->delay
= HZ
/ 2;
624 fe
->ops
.read_status(fe
, &s
);
625 if (s
!= fepriv
->status
) {
626 dvb_frontend_add_event(fe
, s
); /* update event list */
628 if (!(s
& FE_HAS_LOCK
)) {
629 fepriv
->delay
= HZ
/ 10;
630 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
632 fepriv
->delay
= 60 * HZ
;
637 dprintk("%s: UNDEFINED ALGO !\n", __func__
);
641 dvb_frontend_swzigzag(fe
);
645 if (dvb_powerdown_on_sleep
) {
646 if (fe
->ops
.set_voltage
)
647 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_OFF
);
648 if (fe
->ops
.tuner_ops
.sleep
) {
649 if (fe
->ops
.i2c_gate_ctrl
)
650 fe
->ops
.i2c_gate_ctrl(fe
, 1);
651 fe
->ops
.tuner_ops
.sleep(fe
);
652 if (fe
->ops
.i2c_gate_ctrl
)
653 fe
->ops
.i2c_gate_ctrl(fe
, 0);
659 fepriv
->thread
= NULL
;
663 dvb_frontend_wakeup(fe
);
667 static void dvb_frontend_stop(struct dvb_frontend
*fe
)
669 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
671 dprintk ("%s\n", __func__
);
679 kthread_stop(fepriv
->thread
);
681 init_MUTEX (&fepriv
->sem
);
682 fepriv
->state
= FESTATE_IDLE
;
684 /* paranoia check in case a signal arrived */
686 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
690 s32
timeval_usec_diff(struct timeval lasttime
, struct timeval curtime
)
692 return ((curtime
.tv_usec
< lasttime
.tv_usec
) ?
693 1000000 - lasttime
.tv_usec
+ curtime
.tv_usec
:
694 curtime
.tv_usec
- lasttime
.tv_usec
);
696 EXPORT_SYMBOL(timeval_usec_diff
);
698 static inline void timeval_usec_add(struct timeval
*curtime
, u32 add_usec
)
700 curtime
->tv_usec
+= add_usec
;
701 if (curtime
->tv_usec
>= 1000000) {
702 curtime
->tv_usec
-= 1000000;
708 * Sleep until gettimeofday() > waketime + add_usec
709 * This needs to be as precise as possible, but as the delay is
710 * usually between 2ms and 32ms, it is done using a scheduled msleep
711 * followed by usleep (normally a busy-wait loop) for the remainder
713 void dvb_frontend_sleep_until(struct timeval
*waketime
, u32 add_usec
)
715 struct timeval lasttime
;
718 timeval_usec_add(waketime
, add_usec
);
720 do_gettimeofday(&lasttime
);
721 delta
= timeval_usec_diff(lasttime
, *waketime
);
723 msleep((delta
- 1500) / 1000);
724 do_gettimeofday(&lasttime
);
725 newdelta
= timeval_usec_diff(lasttime
, *waketime
);
726 delta
= (newdelta
> delta
) ? 0 : newdelta
;
731 EXPORT_SYMBOL(dvb_frontend_sleep_until
);
733 static int dvb_frontend_start(struct dvb_frontend
*fe
)
736 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
737 struct task_struct
*fe_thread
;
739 dprintk ("%s\n", __func__
);
741 if (fepriv
->thread
) {
745 dvb_frontend_stop (fe
);
748 if (signal_pending(current
))
750 if (down_interruptible (&fepriv
->sem
))
753 fepriv
->state
= FESTATE_IDLE
;
755 fepriv
->thread
= NULL
;
758 fe_thread
= kthread_run(dvb_frontend_thread
, fe
,
759 "kdvb-ad-%i-fe-%i", fe
->dvb
->num
,fe
->id
);
760 if (IS_ERR(fe_thread
)) {
761 ret
= PTR_ERR(fe_thread
);
762 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret
);
766 fepriv
->thread
= fe_thread
;
770 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend
*fe
,
771 u32
*freq_min
, u32
*freq_max
)
773 *freq_min
= max(fe
->ops
.info
.frequency_min
, fe
->ops
.tuner_ops
.info
.frequency_min
);
775 if (fe
->ops
.info
.frequency_max
== 0)
776 *freq_max
= fe
->ops
.tuner_ops
.info
.frequency_max
;
777 else if (fe
->ops
.tuner_ops
.info
.frequency_max
== 0)
778 *freq_max
= fe
->ops
.info
.frequency_max
;
780 *freq_max
= min(fe
->ops
.info
.frequency_max
, fe
->ops
.tuner_ops
.info
.frequency_max
);
782 if (*freq_min
== 0 || *freq_max
== 0)
783 printk(KERN_WARNING
"DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
784 fe
->dvb
->num
,fe
->id
);
787 static int dvb_frontend_check_parameters(struct dvb_frontend
*fe
,
788 struct dvb_frontend_parameters
*parms
)
793 /* range check: frequency */
794 dvb_frontend_get_frequeny_limits(fe
, &freq_min
, &freq_max
);
795 if ((freq_min
&& parms
->frequency
< freq_min
) ||
796 (freq_max
&& parms
->frequency
> freq_max
)) {
797 printk(KERN_WARNING
"DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
798 fe
->dvb
->num
, fe
->id
, parms
->frequency
, freq_min
, freq_max
);
802 /* range check: symbol rate */
803 if (fe
->ops
.info
.type
== FE_QPSK
) {
804 if ((fe
->ops
.info
.symbol_rate_min
&&
805 parms
->u
.qpsk
.symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
806 (fe
->ops
.info
.symbol_rate_max
&&
807 parms
->u
.qpsk
.symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
808 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
809 fe
->dvb
->num
, fe
->id
, parms
->u
.qpsk
.symbol_rate
,
810 fe
->ops
.info
.symbol_rate_min
, fe
->ops
.info
.symbol_rate_max
);
814 } else if (fe
->ops
.info
.type
== FE_QAM
) {
815 if ((fe
->ops
.info
.symbol_rate_min
&&
816 parms
->u
.qam
.symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
817 (fe
->ops
.info
.symbol_rate_max
&&
818 parms
->u
.qam
.symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
819 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
820 fe
->dvb
->num
, fe
->id
, parms
->u
.qam
.symbol_rate
,
821 fe
->ops
.info
.symbol_rate_min
, fe
->ops
.info
.symbol_rate_max
);
829 static struct dtv_cmds_h dtv_cmds
[] = {
843 .name
= "DTV_FREQUENCY",
844 .cmd
= DTV_FREQUENCY
,
847 [DTV_BANDWIDTH_HZ
] = {
848 .name
= "DTV_BANDWIDTH_HZ",
849 .cmd
= DTV_BANDWIDTH_HZ
,
853 .name
= "DTV_MODULATION",
854 .cmd
= DTV_MODULATION
,
858 .name
= "DTV_INVERSION",
859 .cmd
= DTV_INVERSION
,
862 [DTV_DISEQC_MASTER
] = {
863 .name
= "DTV_DISEQC_MASTER",
864 .cmd
= DTV_DISEQC_MASTER
,
868 [DTV_SYMBOL_RATE
] = {
869 .name
= "DTV_SYMBOL_RATE",
870 .cmd
= DTV_SYMBOL_RATE
,
874 .name
= "DTV_INNER_FEC",
875 .cmd
= DTV_INNER_FEC
,
879 .name
= "DTV_VOLTAGE",
894 .name
= "DTV_ROLLOFF",
898 [DTV_DELIVERY_SYSTEM
] = {
899 .name
= "DTV_DELIVERY_SYSTEM",
900 .cmd
= DTV_DELIVERY_SYSTEM
,
904 .name
= "DTV_HIERARCHY",
905 .cmd
= DTV_HIERARCHY
,
908 [DTV_CODE_RATE_HP
] = {
909 .name
= "DTV_CODE_RATE_HP",
910 .cmd
= DTV_CODE_RATE_HP
,
913 [DTV_CODE_RATE_LP
] = {
914 .name
= "DTV_CODE_RATE_LP",
915 .cmd
= DTV_CODE_RATE_LP
,
918 [DTV_GUARD_INTERVAL
] = {
919 .name
= "DTV_GUARD_INTERVAL",
920 .cmd
= DTV_GUARD_INTERVAL
,
923 [DTV_TRANSMISSION_MODE
] = {
924 .name
= "DTV_TRANSMISSION_MODE",
925 .cmd
= DTV_TRANSMISSION_MODE
,
929 [DTV_DISEQC_SLAVE_REPLY
] = {
930 .name
= "DTV_DISEQC_SLAVE_REPLY",
931 .cmd
= DTV_DISEQC_SLAVE_REPLY
,
935 [DTV_API_VERSION
] = {
936 .name
= "DTV_API_VERSION",
937 .cmd
= DTV_API_VERSION
,
940 [DTV_CODE_RATE_HP
] = {
941 .name
= "DTV_CODE_RATE_HP",
942 .cmd
= DTV_CODE_RATE_HP
,
945 [DTV_CODE_RATE_LP
] = {
946 .name
= "DTV_CODE_RATE_LP",
947 .cmd
= DTV_CODE_RATE_LP
,
950 [DTV_GUARD_INTERVAL
] = {
951 .name
= "DTV_GUARD_INTERVAL",
952 .cmd
= DTV_GUARD_INTERVAL
,
955 [DTV_TRANSMISSION_MODE
] = {
956 .name
= "DTV_TRANSMISSION_MODE",
957 .cmd
= DTV_TRANSMISSION_MODE
,
961 .name
= "DTV_HIERARCHY",
962 .cmd
= DTV_HIERARCHY
,
967 static void dtv_property_dump(struct dtv_property
*tvp
)
971 if (tvp
->cmd
<= 0 || tvp
->cmd
> DTV_MAX_COMMAND
) {
972 printk(KERN_WARNING
"%s: tvp.cmd = 0x%08x undefined\n",
977 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
980 ,dtv_cmds
[ tvp
->cmd
].name
);
982 if(dtv_cmds
[ tvp
->cmd
].buffer
) {
984 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
988 for(i
= 0; i
< tvp
->u
.buffer
.len
; i
++)
989 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
992 ,tvp
->u
.buffer
.data
[i
]);
995 dprintk("%s() tvp.u.data = 0x%08x\n", __func__
, tvp
->u
.data
);
998 static int is_legacy_delivery_system(fe_delivery_system_t s
)
1000 if((s
== SYS_UNDEFINED
) || (s
== SYS_DVBC_ANNEX_AC
) ||
1001 (s
== SYS_DVBC_ANNEX_B
) || (s
== SYS_DVBT
) || (s
== SYS_DVBS
) ||
1008 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1009 * drivers can use a single set_frontend tuning function, regardless of whether
1010 * it's being used for the legacy or new API, reducing code and complexity.
1012 static void dtv_property_cache_sync(struct dvb_frontend
*fe
,
1013 struct dvb_frontend_parameters
*p
)
1015 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1017 c
->frequency
= p
->frequency
;
1018 c
->inversion
= p
->inversion
;
1020 switch (fe
->ops
.info
.type
) {
1022 c
->modulation
= QPSK
; /* implied for DVB-S in legacy API */
1023 c
->rolloff
= ROLLOFF_35
;/* implied for DVB-S */
1024 c
->symbol_rate
= p
->u
.qpsk
.symbol_rate
;
1025 c
->fec_inner
= p
->u
.qpsk
.fec_inner
;
1026 c
->delivery_system
= SYS_DVBS
;
1029 c
->symbol_rate
= p
->u
.qam
.symbol_rate
;
1030 c
->fec_inner
= p
->u
.qam
.fec_inner
;
1031 c
->modulation
= p
->u
.qam
.modulation
;
1032 c
->delivery_system
= SYS_DVBC_ANNEX_AC
;
1035 if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_6_MHZ
)
1036 c
->bandwidth_hz
= 6000000;
1037 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_7_MHZ
)
1038 c
->bandwidth_hz
= 7000000;
1039 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_8_MHZ
)
1040 c
->bandwidth_hz
= 8000000;
1042 /* Including BANDWIDTH_AUTO */
1043 c
->bandwidth_hz
= 0;
1044 c
->code_rate_HP
= p
->u
.ofdm
.code_rate_HP
;
1045 c
->code_rate_LP
= p
->u
.ofdm
.code_rate_LP
;
1046 c
->modulation
= p
->u
.ofdm
.constellation
;
1047 c
->transmission_mode
= p
->u
.ofdm
.transmission_mode
;
1048 c
->guard_interval
= p
->u
.ofdm
.guard_interval
;
1049 c
->hierarchy
= p
->u
.ofdm
.hierarchy_information
;
1050 c
->delivery_system
= SYS_DVBT
;
1053 c
->modulation
= p
->u
.vsb
.modulation
;
1054 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1055 c
->delivery_system
= SYS_ATSC
;
1057 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1062 /* Ensure the cached values are set correctly in the frontend
1063 * legacy tuning structures, for the advanced tuning API.
1065 static void dtv_property_legacy_params_sync(struct dvb_frontend
*fe
)
1067 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1068 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1069 struct dvb_frontend_parameters
*p
= &fepriv
->parameters
;
1071 p
->frequency
= c
->frequency
;
1072 p
->inversion
= c
->inversion
;
1074 switch (fe
->ops
.info
.type
) {
1076 dprintk("%s() Preparing QPSK req\n", __func__
);
1077 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1078 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1079 c
->delivery_system
= SYS_DVBS
;
1082 dprintk("%s() Preparing QAM req\n", __func__
);
1083 p
->u
.qam
.symbol_rate
= c
->symbol_rate
;
1084 p
->u
.qam
.fec_inner
= c
->fec_inner
;
1085 p
->u
.qam
.modulation
= c
->modulation
;
1086 c
->delivery_system
= SYS_DVBC_ANNEX_AC
;
1089 dprintk("%s() Preparing OFDM req\n", __func__
);
1090 if (c
->bandwidth_hz
== 6000000)
1091 p
->u
.ofdm
.bandwidth
= BANDWIDTH_6_MHZ
;
1092 else if (c
->bandwidth_hz
== 7000000)
1093 p
->u
.ofdm
.bandwidth
= BANDWIDTH_7_MHZ
;
1094 else if (c
->bandwidth_hz
== 8000000)
1095 p
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
1097 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1098 p
->u
.ofdm
.code_rate_HP
= c
->code_rate_HP
;
1099 p
->u
.ofdm
.code_rate_LP
= c
->code_rate_LP
;
1100 p
->u
.ofdm
.constellation
= c
->modulation
;
1101 p
->u
.ofdm
.transmission_mode
= c
->transmission_mode
;
1102 p
->u
.ofdm
.guard_interval
= c
->guard_interval
;
1103 p
->u
.ofdm
.hierarchy_information
= c
->hierarchy
;
1104 c
->delivery_system
= SYS_DVBT
;
1107 dprintk("%s() Preparing VSB req\n", __func__
);
1108 p
->u
.vsb
.modulation
= c
->modulation
;
1109 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1110 c
->delivery_system
= SYS_ATSC
;
1112 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1117 /* Ensure the cached values are set correctly in the frontend
1118 * legacy tuning structures, for the legacy tuning API.
1120 static void dtv_property_adv_params_sync(struct dvb_frontend
*fe
)
1122 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1123 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1124 struct dvb_frontend_parameters
*p
= &fepriv
->parameters
;
1126 p
->frequency
= c
->frequency
;
1127 p
->inversion
= c
->inversion
;
1129 switch(c
->modulation
) {
1134 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1135 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1141 if(c
->delivery_system
== SYS_ISDBT
) {
1142 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1143 p
->frequency
= c
->frequency
;
1144 p
->inversion
= INVERSION_AUTO
;
1145 p
->u
.ofdm
.constellation
= QAM_AUTO
;
1146 p
->u
.ofdm
.code_rate_HP
= FEC_AUTO
;
1147 p
->u
.ofdm
.code_rate_LP
= FEC_AUTO
;
1148 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1149 p
->u
.ofdm
.transmission_mode
= TRANSMISSION_MODE_AUTO
;
1150 p
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_AUTO
;
1151 p
->u
.ofdm
.hierarchy_information
= HIERARCHY_AUTO
;
1155 static void dtv_property_cache_submit(struct dvb_frontend
*fe
)
1157 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1159 /* For legacy delivery systems we don't need the delivery_system to
1160 * be specified, but we populate the older structures from the cache
1161 * so we can call set_frontend on older drivers.
1163 if(is_legacy_delivery_system(c
->delivery_system
)) {
1165 dprintk("%s() legacy, modulation = %d\n", __func__
, c
->modulation
);
1166 dtv_property_legacy_params_sync(fe
);
1169 dprintk("%s() adv, modulation = %d\n", __func__
, c
->modulation
);
1171 /* For advanced delivery systems / modulation types ...
1172 * we seed the lecacy dvb_frontend_parameters structure
1173 * so that the sanity checking code later in the IOCTL processing
1174 * can validate our basic frequency ranges, symbolrates, modulation
1177 dtv_property_adv_params_sync(fe
);
1181 static int dvb_frontend_ioctl_legacy(struct inode
*inode
, struct file
*file
,
1182 unsigned int cmd
, void *parg
);
1183 static int dvb_frontend_ioctl_properties(struct inode
*inode
, struct file
*file
,
1184 unsigned int cmd
, void *parg
);
1186 static int dtv_property_process_get(struct dvb_frontend
*fe
,
1187 struct dtv_property
*tvp
,
1188 struct inode
*inode
, struct file
*file
)
1192 dtv_property_dump(tvp
);
1194 /* Allow the frontend to validate incoming properties */
1195 if (fe
->ops
.get_property
)
1196 r
= fe
->ops
.get_property(fe
, tvp
);
1203 tvp
->u
.data
= fe
->dtv_property_cache
.frequency
;
1205 case DTV_MODULATION
:
1206 tvp
->u
.data
= fe
->dtv_property_cache
.modulation
;
1208 case DTV_BANDWIDTH_HZ
:
1209 tvp
->u
.data
= fe
->dtv_property_cache
.bandwidth_hz
;
1212 tvp
->u
.data
= fe
->dtv_property_cache
.inversion
;
1214 case DTV_SYMBOL_RATE
:
1215 tvp
->u
.data
= fe
->dtv_property_cache
.symbol_rate
;
1218 tvp
->u
.data
= fe
->dtv_property_cache
.fec_inner
;
1221 tvp
->u
.data
= fe
->dtv_property_cache
.pilot
;
1224 tvp
->u
.data
= fe
->dtv_property_cache
.rolloff
;
1226 case DTV_DELIVERY_SYSTEM
:
1227 tvp
->u
.data
= fe
->dtv_property_cache
.delivery_system
;
1230 tvp
->u
.data
= fe
->dtv_property_cache
.voltage
;
1233 tvp
->u
.data
= fe
->dtv_property_cache
.sectone
;
1235 case DTV_API_VERSION
:
1236 tvp
->u
.data
= (DVB_API_VERSION
<< 8) | DVB_API_VERSION_MINOR
;
1238 case DTV_CODE_RATE_HP
:
1239 tvp
->u
.data
= fe
->dtv_property_cache
.code_rate_HP
;
1241 case DTV_CODE_RATE_LP
:
1242 tvp
->u
.data
= fe
->dtv_property_cache
.code_rate_LP
;
1244 case DTV_GUARD_INTERVAL
:
1245 tvp
->u
.data
= fe
->dtv_property_cache
.guard_interval
;
1247 case DTV_TRANSMISSION_MODE
:
1248 tvp
->u
.data
= fe
->dtv_property_cache
.transmission_mode
;
1251 tvp
->u
.data
= fe
->dtv_property_cache
.hierarchy
;
1260 static int dtv_property_process_set(struct dvb_frontend
*fe
,
1261 struct dtv_property
*tvp
,
1262 struct inode
*inode
,
1266 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1267 dtv_property_dump(tvp
);
1269 /* Allow the frontend to validate incoming properties */
1270 if (fe
->ops
.set_property
)
1271 r
= fe
->ops
.set_property(fe
, tvp
);
1278 /* Reset a cache of data specific to the frontend here. This does
1279 * not effect hardware.
1281 dprintk("%s() Flushing property cache\n", __func__
);
1282 memset(&fe
->dtv_property_cache
, 0, sizeof(struct dtv_frontend_properties
));
1283 fe
->dtv_property_cache
.state
= tvp
->cmd
;
1284 fe
->dtv_property_cache
.delivery_system
= SYS_UNDEFINED
;
1287 /* interpret the cache of data, build either a traditional frontend
1288 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1291 fe
->dtv_property_cache
.state
= tvp
->cmd
;
1292 dprintk("%s() Finalised property cache\n", __func__
);
1293 dtv_property_cache_submit(fe
);
1295 r
|= dvb_frontend_ioctl_legacy(inode
, file
, FE_SET_FRONTEND
,
1296 &fepriv
->parameters
);
1299 fe
->dtv_property_cache
.frequency
= tvp
->u
.data
;
1301 case DTV_MODULATION
:
1302 fe
->dtv_property_cache
.modulation
= tvp
->u
.data
;
1304 case DTV_BANDWIDTH_HZ
:
1305 fe
->dtv_property_cache
.bandwidth_hz
= tvp
->u
.data
;
1308 fe
->dtv_property_cache
.inversion
= tvp
->u
.data
;
1310 case DTV_SYMBOL_RATE
:
1311 fe
->dtv_property_cache
.symbol_rate
= tvp
->u
.data
;
1314 fe
->dtv_property_cache
.fec_inner
= tvp
->u
.data
;
1317 fe
->dtv_property_cache
.pilot
= tvp
->u
.data
;
1320 fe
->dtv_property_cache
.rolloff
= tvp
->u
.data
;
1322 case DTV_DELIVERY_SYSTEM
:
1323 fe
->dtv_property_cache
.delivery_system
= tvp
->u
.data
;
1326 fe
->dtv_property_cache
.voltage
= tvp
->u
.data
;
1327 r
= dvb_frontend_ioctl_legacy(inode
, file
, FE_SET_VOLTAGE
,
1328 (void *)fe
->dtv_property_cache
.voltage
);
1331 fe
->dtv_property_cache
.sectone
= tvp
->u
.data
;
1332 r
= dvb_frontend_ioctl_legacy(inode
, file
, FE_SET_TONE
,
1333 (void *)fe
->dtv_property_cache
.sectone
);
1335 case DTV_CODE_RATE_HP
:
1336 fe
->dtv_property_cache
.code_rate_HP
= tvp
->u
.data
;
1338 case DTV_CODE_RATE_LP
:
1339 fe
->dtv_property_cache
.code_rate_LP
= tvp
->u
.data
;
1341 case DTV_GUARD_INTERVAL
:
1342 fe
->dtv_property_cache
.guard_interval
= tvp
->u
.data
;
1344 case DTV_TRANSMISSION_MODE
:
1345 fe
->dtv_property_cache
.transmission_mode
= tvp
->u
.data
;
1348 fe
->dtv_property_cache
.hierarchy
= tvp
->u
.data
;
1357 static int dvb_frontend_ioctl(struct inode
*inode
, struct file
*file
,
1358 unsigned int cmd
, void *parg
)
1360 struct dvb_device
*dvbdev
= file
->private_data
;
1361 struct dvb_frontend
*fe
= dvbdev
->priv
;
1362 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1363 int err
= -EOPNOTSUPP
;
1365 dprintk ("%s\n", __func__
);
1370 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
&&
1371 (_IOC_DIR(cmd
) != _IOC_READ
|| cmd
== FE_GET_EVENT
||
1372 cmd
== FE_DISEQC_RECV_SLAVE_REPLY
))
1375 if (down_interruptible (&fepriv
->sem
))
1376 return -ERESTARTSYS
;
1378 if ((cmd
== FE_SET_PROPERTY
) || (cmd
== FE_GET_PROPERTY
))
1379 err
= dvb_frontend_ioctl_properties(inode
, file
, cmd
, parg
);
1381 fe
->dtv_property_cache
.state
= DTV_UNDEFINED
;
1382 err
= dvb_frontend_ioctl_legacy(inode
, file
, cmd
, parg
);
1389 static int dvb_frontend_ioctl_properties(struct inode
*inode
, struct file
*file
,
1390 unsigned int cmd
, void *parg
)
1392 struct dvb_device
*dvbdev
= file
->private_data
;
1393 struct dvb_frontend
*fe
= dvbdev
->priv
;
1396 struct dtv_properties
*tvps
= NULL
;
1397 struct dtv_property
*tvp
= NULL
;
1400 dprintk("%s\n", __func__
);
1402 if(cmd
== FE_SET_PROPERTY
) {
1403 tvps
= (struct dtv_properties __user
*)parg
;
1405 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1406 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1408 /* Put an arbitrary limit on the number of messages that can
1409 * be sent at once */
1410 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1413 tvp
= (struct dtv_property
*) kmalloc(tvps
->num
*
1414 sizeof(struct dtv_property
), GFP_KERNEL
);
1420 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1425 for (i
= 0; i
< tvps
->num
; i
++) {
1426 (tvp
+ i
)->result
= dtv_property_process_set(fe
, tvp
+ i
, inode
, file
);
1427 err
|= (tvp
+ i
)->result
;
1430 if(fe
->dtv_property_cache
.state
== DTV_TUNE
)
1431 dprintk("%s() Property cache is full, tuning\n", __func__
);
1434 if(cmd
== FE_GET_PROPERTY
) {
1436 tvps
= (struct dtv_properties __user
*)parg
;
1438 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1439 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1441 /* Put an arbitrary limit on the number of messages that can
1442 * be sent at once */
1443 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1446 tvp
= (struct dtv_property
*) kmalloc(tvps
->num
*
1447 sizeof(struct dtv_property
), GFP_KERNEL
);
1453 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1458 for (i
= 0; i
< tvps
->num
; i
++) {
1459 (tvp
+ i
)->result
= dtv_property_process_get(fe
, tvp
+ i
, inode
, file
);
1460 err
|= (tvp
+ i
)->result
;
1463 if (copy_to_user(tvps
->props
, tvp
, tvps
->num
* sizeof(struct dtv_property
))) {
1476 static int dvb_frontend_ioctl_legacy(struct inode
*inode
, struct file
*file
,
1477 unsigned int cmd
, void *parg
)
1479 struct dvb_device
*dvbdev
= file
->private_data
;
1480 struct dvb_frontend
*fe
= dvbdev
->priv
;
1481 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1482 int err
= -EOPNOTSUPP
;
1486 struct dvb_frontend_info
* info
= parg
;
1487 memcpy(info
, &fe
->ops
.info
, sizeof(struct dvb_frontend_info
));
1488 dvb_frontend_get_frequeny_limits(fe
, &info
->frequency_min
, &info
->frequency_max
);
1490 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1491 * do it, it is done for it. */
1492 info
->caps
|= FE_CAN_INVERSION_AUTO
;
1497 case FE_READ_STATUS
: {
1498 fe_status_t
* status
= parg
;
1500 /* if retune was requested but hasn't occured yet, prevent
1501 * that user get signal state from previous tuning */
1502 if(fepriv
->state
== FESTATE_RETUNE
) {
1508 if (fe
->ops
.read_status
)
1509 err
= fe
->ops
.read_status(fe
, status
);
1513 if (fe
->ops
.read_ber
)
1514 err
= fe
->ops
.read_ber(fe
, (__u32
*) parg
);
1517 case FE_READ_SIGNAL_STRENGTH
:
1518 if (fe
->ops
.read_signal_strength
)
1519 err
= fe
->ops
.read_signal_strength(fe
, (__u16
*) parg
);
1523 if (fe
->ops
.read_snr
)
1524 err
= fe
->ops
.read_snr(fe
, (__u16
*) parg
);
1527 case FE_READ_UNCORRECTED_BLOCKS
:
1528 if (fe
->ops
.read_ucblocks
)
1529 err
= fe
->ops
.read_ucblocks(fe
, (__u32
*) parg
);
1533 case FE_DISEQC_RESET_OVERLOAD
:
1534 if (fe
->ops
.diseqc_reset_overload
) {
1535 err
= fe
->ops
.diseqc_reset_overload(fe
);
1536 fepriv
->state
= FESTATE_DISEQC
;
1541 case FE_DISEQC_SEND_MASTER_CMD
:
1542 if (fe
->ops
.diseqc_send_master_cmd
) {
1543 err
= fe
->ops
.diseqc_send_master_cmd(fe
, (struct dvb_diseqc_master_cmd
*) parg
);
1544 fepriv
->state
= FESTATE_DISEQC
;
1549 case FE_DISEQC_SEND_BURST
:
1550 if (fe
->ops
.diseqc_send_burst
) {
1551 err
= fe
->ops
.diseqc_send_burst(fe
, (fe_sec_mini_cmd_t
) parg
);
1552 fepriv
->state
= FESTATE_DISEQC
;
1558 if (fe
->ops
.set_tone
) {
1559 err
= fe
->ops
.set_tone(fe
, (fe_sec_tone_mode_t
) parg
);
1560 fepriv
->tone
= (fe_sec_tone_mode_t
) parg
;
1561 fepriv
->state
= FESTATE_DISEQC
;
1566 case FE_SET_VOLTAGE
:
1567 if (fe
->ops
.set_voltage
) {
1568 err
= fe
->ops
.set_voltage(fe
, (fe_sec_voltage_t
) parg
);
1569 fepriv
->voltage
= (fe_sec_voltage_t
) parg
;
1570 fepriv
->state
= FESTATE_DISEQC
;
1575 case FE_DISHNETWORK_SEND_LEGACY_CMD
:
1576 if (fe
->ops
.dishnetwork_send_legacy_command
) {
1577 err
= fe
->ops
.dishnetwork_send_legacy_command(fe
, (unsigned long) parg
);
1578 fepriv
->state
= FESTATE_DISEQC
;
1580 } else if (fe
->ops
.set_voltage
) {
1582 * NOTE: This is a fallback condition. Some frontends
1583 * (stv0299 for instance) take longer than 8msec to
1584 * respond to a set_voltage command. Those switches
1585 * need custom routines to switch properly. For all
1586 * other frontends, the following shoule work ok.
1587 * Dish network legacy switches (as used by Dish500)
1588 * are controlled by sending 9-bit command words
1589 * spaced 8msec apart.
1590 * the actual command word is switch/port dependant
1591 * so it is up to the userspace application to send
1592 * the right command.
1593 * The command must always start with a '0' after
1594 * initialization, so parg is 8 bits and does not
1595 * include the initialization or start bit
1597 unsigned long swcmd
= ((unsigned long) parg
) << 1;
1598 struct timeval nexttime
;
1599 struct timeval tv
[10];
1602 if (dvb_frontend_debug
)
1603 printk("%s switch command: 0x%04lx\n", __func__
, swcmd
);
1604 do_gettimeofday(&nexttime
);
1605 if (dvb_frontend_debug
)
1606 memcpy(&tv
[0], &nexttime
, sizeof(struct timeval
));
1607 /* before sending a command, initialize by sending
1608 * a 32ms 18V to the switch
1610 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_18
);
1611 dvb_frontend_sleep_until(&nexttime
, 32000);
1613 for (i
= 0; i
< 9; i
++) {
1614 if (dvb_frontend_debug
)
1615 do_gettimeofday(&tv
[i
+ 1]);
1616 if ((swcmd
& 0x01) != last
) {
1617 /* set voltage to (last ? 13V : 18V) */
1618 fe
->ops
.set_voltage(fe
, (last
) ? SEC_VOLTAGE_13
: SEC_VOLTAGE_18
);
1619 last
= (last
) ? 0 : 1;
1623 dvb_frontend_sleep_until(&nexttime
, 8000);
1625 if (dvb_frontend_debug
) {
1626 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1627 __func__
, fe
->dvb
->num
);
1628 for (i
= 1; i
< 10; i
++)
1629 printk("%d: %d\n", i
, timeval_usec_diff(tv
[i
-1] , tv
[i
]));
1632 fepriv
->state
= FESTATE_DISEQC
;
1637 case FE_DISEQC_RECV_SLAVE_REPLY
:
1638 if (fe
->ops
.diseqc_recv_slave_reply
)
1639 err
= fe
->ops
.diseqc_recv_slave_reply(fe
, (struct dvb_diseqc_slave_reply
*) parg
);
1642 case FE_ENABLE_HIGH_LNB_VOLTAGE
:
1643 if (fe
->ops
.enable_high_lnb_voltage
)
1644 err
= fe
->ops
.enable_high_lnb_voltage(fe
, (long) parg
);
1647 case FE_SET_FRONTEND
: {
1648 struct dvb_frontend_tune_settings fetunesettings
;
1650 if(fe
->dtv_property_cache
.state
== DTV_TUNE
) {
1651 if (dvb_frontend_check_parameters(fe
, &fepriv
->parameters
) < 0) {
1656 if (dvb_frontend_check_parameters(fe
, parg
) < 0) {
1661 memcpy (&fepriv
->parameters
, parg
,
1662 sizeof (struct dvb_frontend_parameters
));
1663 dtv_property_cache_sync(fe
, &fepriv
->parameters
);
1666 memset(&fetunesettings
, 0, sizeof(struct dvb_frontend_tune_settings
));
1667 memcpy(&fetunesettings
.parameters
, parg
,
1668 sizeof (struct dvb_frontend_parameters
));
1670 /* force auto frequency inversion if requested */
1671 if (dvb_force_auto_inversion
) {
1672 fepriv
->parameters
.inversion
= INVERSION_AUTO
;
1673 fetunesettings
.parameters
.inversion
= INVERSION_AUTO
;
1675 if (fe
->ops
.info
.type
== FE_OFDM
) {
1676 /* without hierarchical coding code_rate_LP is irrelevant,
1677 * so we tolerate the otherwise invalid FEC_NONE setting */
1678 if (fepriv
->parameters
.u
.ofdm
.hierarchy_information
== HIERARCHY_NONE
&&
1679 fepriv
->parameters
.u
.ofdm
.code_rate_LP
== FEC_NONE
)
1680 fepriv
->parameters
.u
.ofdm
.code_rate_LP
= FEC_AUTO
;
1683 /* get frontend-specific tuning settings */
1684 if (fe
->ops
.get_tune_settings
&& (fe
->ops
.get_tune_settings(fe
, &fetunesettings
) == 0)) {
1685 fepriv
->min_delay
= (fetunesettings
.min_delay_ms
* HZ
) / 1000;
1686 fepriv
->max_drift
= fetunesettings
.max_drift
;
1687 fepriv
->step_size
= fetunesettings
.step_size
;
1689 /* default values */
1690 switch(fe
->ops
.info
.type
) {
1692 fepriv
->min_delay
= HZ
/20;
1693 fepriv
->step_size
= fepriv
->parameters
.u
.qpsk
.symbol_rate
/ 16000;
1694 fepriv
->max_drift
= fepriv
->parameters
.u
.qpsk
.symbol_rate
/ 2000;
1698 fepriv
->min_delay
= HZ
/20;
1699 fepriv
->step_size
= 0; /* no zigzag */
1700 fepriv
->max_drift
= 0;
1704 fepriv
->min_delay
= HZ
/20;
1705 fepriv
->step_size
= fe
->ops
.info
.frequency_stepsize
* 2;
1706 fepriv
->max_drift
= (fe
->ops
.info
.frequency_stepsize
* 2) + 1;
1709 fepriv
->min_delay
= HZ
/20;
1710 fepriv
->step_size
= 0;
1711 fepriv
->max_drift
= 0;
1715 if (dvb_override_tune_delay
> 0)
1716 fepriv
->min_delay
= (dvb_override_tune_delay
* HZ
) / 1000;
1718 fepriv
->state
= FESTATE_RETUNE
;
1720 /* Request the search algorithm to search */
1721 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
1723 dvb_frontend_wakeup(fe
);
1724 dvb_frontend_add_event(fe
, 0);
1731 err
= dvb_frontend_get_event (fe
, parg
, file
->f_flags
);
1734 case FE_GET_FRONTEND
:
1735 if (fe
->ops
.get_frontend
) {
1736 memcpy (parg
, &fepriv
->parameters
, sizeof (struct dvb_frontend_parameters
));
1737 err
= fe
->ops
.get_frontend(fe
, (struct dvb_frontend_parameters
*) parg
);
1741 case FE_SET_FRONTEND_TUNE_MODE
:
1742 fepriv
->tune_mode_flags
= (unsigned long) parg
;
1751 static unsigned int dvb_frontend_poll(struct file
*file
, struct poll_table_struct
*wait
)
1753 struct dvb_device
*dvbdev
= file
->private_data
;
1754 struct dvb_frontend
*fe
= dvbdev
->priv
;
1755 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1757 dprintk ("%s\n", __func__
);
1759 poll_wait (file
, &fepriv
->events
.wait_queue
, wait
);
1761 if (fepriv
->events
.eventw
!= fepriv
->events
.eventr
)
1762 return (POLLIN
| POLLRDNORM
| POLLPRI
);
1767 static int dvb_frontend_open(struct inode
*inode
, struct file
*file
)
1769 struct dvb_device
*dvbdev
= file
->private_data
;
1770 struct dvb_frontend
*fe
= dvbdev
->priv
;
1771 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1772 struct dvb_adapter
*adapter
= fe
->dvb
;
1775 dprintk ("%s\n", __func__
);
1777 if (adapter
->mfe_shared
) {
1778 mutex_lock (&adapter
->mfe_lock
);
1780 if (adapter
->mfe_dvbdev
== NULL
)
1781 adapter
->mfe_dvbdev
= dvbdev
;
1783 else if (adapter
->mfe_dvbdev
!= dvbdev
) {
1785 *mfedev
= adapter
->mfe_dvbdev
;
1787 *mfe
= mfedev
->priv
;
1788 struct dvb_frontend_private
1789 *mfepriv
= mfe
->frontend_priv
;
1790 int mferetry
= (dvb_mfe_wait_time
<< 1);
1792 mutex_unlock (&adapter
->mfe_lock
);
1793 while (mferetry
-- && (mfedev
->users
!= -1 ||
1794 mfepriv
->thread
!= NULL
)) {
1795 if(msleep_interruptible(500)) {
1796 if(signal_pending(current
))
1801 mutex_lock (&adapter
->mfe_lock
);
1802 if(adapter
->mfe_dvbdev
!= dvbdev
) {
1803 mfedev
= adapter
->mfe_dvbdev
;
1805 mfepriv
= mfe
->frontend_priv
;
1806 if (mfedev
->users
!= -1 ||
1807 mfepriv
->thread
!= NULL
) {
1808 mutex_unlock (&adapter
->mfe_lock
);
1811 adapter
->mfe_dvbdev
= dvbdev
;
1816 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
) {
1817 if ((ret
= fe
->ops
.ts_bus_ctrl(fe
, 1)) < 0)
1821 if ((ret
= dvb_generic_open (inode
, file
)) < 0)
1824 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
1825 /* normal tune mode when opened R/W */
1826 fepriv
->tune_mode_flags
&= ~FE_TUNE_MODE_ONESHOT
;
1828 fepriv
->voltage
= -1;
1830 ret
= dvb_frontend_start (fe
);
1834 /* empty event queue */
1835 fepriv
->events
.eventr
= fepriv
->events
.eventw
= 0;
1838 if (adapter
->mfe_shared
)
1839 mutex_unlock (&adapter
->mfe_lock
);
1843 dvb_generic_release(inode
, file
);
1845 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
)
1846 fe
->ops
.ts_bus_ctrl(fe
, 0);
1848 if (adapter
->mfe_shared
)
1849 mutex_unlock (&adapter
->mfe_lock
);
1853 static int dvb_frontend_release(struct inode
*inode
, struct file
*file
)
1855 struct dvb_device
*dvbdev
= file
->private_data
;
1856 struct dvb_frontend
*fe
= dvbdev
->priv
;
1857 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1860 dprintk ("%s\n", __func__
);
1862 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
)
1863 fepriv
->release_jiffies
= jiffies
;
1865 ret
= dvb_generic_release (inode
, file
);
1867 if (dvbdev
->users
== -1) {
1868 if (fepriv
->exit
== 1) {
1869 fops_put(file
->f_op
);
1871 wake_up(&dvbdev
->wait_queue
);
1873 if (fe
->ops
.ts_bus_ctrl
)
1874 fe
->ops
.ts_bus_ctrl(fe
, 0);
1880 static const struct file_operations dvb_frontend_fops
= {
1881 .owner
= THIS_MODULE
,
1882 .ioctl
= dvb_generic_ioctl
,
1883 .poll
= dvb_frontend_poll
,
1884 .open
= dvb_frontend_open
,
1885 .release
= dvb_frontend_release
1888 int dvb_register_frontend(struct dvb_adapter
* dvb
,
1889 struct dvb_frontend
* fe
)
1891 struct dvb_frontend_private
*fepriv
;
1892 static const struct dvb_device dvbdev_template
= {
1896 .fops
= &dvb_frontend_fops
,
1897 .kernel_ioctl
= dvb_frontend_ioctl
1900 dprintk ("%s\n", __func__
);
1902 if (mutex_lock_interruptible(&frontend_mutex
))
1903 return -ERESTARTSYS
;
1905 fe
->frontend_priv
= kzalloc(sizeof(struct dvb_frontend_private
), GFP_KERNEL
);
1906 if (fe
->frontend_priv
== NULL
) {
1907 mutex_unlock(&frontend_mutex
);
1910 fepriv
= fe
->frontend_priv
;
1912 init_MUTEX (&fepriv
->sem
);
1913 init_waitqueue_head (&fepriv
->wait_queue
);
1914 init_waitqueue_head (&fepriv
->events
.wait_queue
);
1915 mutex_init(&fepriv
->events
.mtx
);
1917 fepriv
->inversion
= INVERSION_OFF
;
1919 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
1924 dvb_register_device (fe
->dvb
, &fepriv
->dvbdev
, &dvbdev_template
,
1925 fe
, DVB_DEVICE_FRONTEND
);
1927 mutex_unlock(&frontend_mutex
);
1930 EXPORT_SYMBOL(dvb_register_frontend
);
1932 int dvb_unregister_frontend(struct dvb_frontend
* fe
)
1934 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1935 dprintk ("%s\n", __func__
);
1937 mutex_lock(&frontend_mutex
);
1938 dvb_frontend_stop (fe
);
1939 mutex_unlock(&frontend_mutex
);
1941 if (fepriv
->dvbdev
->users
< -1)
1942 wait_event(fepriv
->dvbdev
->wait_queue
,
1943 fepriv
->dvbdev
->users
==-1);
1945 mutex_lock(&frontend_mutex
);
1946 dvb_unregister_device (fepriv
->dvbdev
);
1948 /* fe is invalid now */
1950 mutex_unlock(&frontend_mutex
);
1953 EXPORT_SYMBOL(dvb_unregister_frontend
);
1955 #ifdef CONFIG_MEDIA_ATTACH
1956 void dvb_frontend_detach(struct dvb_frontend
* fe
)
1960 if (fe
->ops
.release_sec
) {
1961 fe
->ops
.release_sec(fe
);
1962 symbol_put_addr(fe
->ops
.release_sec
);
1964 if (fe
->ops
.tuner_ops
.release
) {
1965 fe
->ops
.tuner_ops
.release(fe
);
1966 symbol_put_addr(fe
->ops
.tuner_ops
.release
);
1968 if (fe
->ops
.analog_ops
.release
) {
1969 fe
->ops
.analog_ops
.release(fe
);
1970 symbol_put_addr(fe
->ops
.analog_ops
.release
);
1972 ptr
= (void*)fe
->ops
.release
;
1974 fe
->ops
.release(fe
);
1975 symbol_put_addr(ptr
);
1979 void dvb_frontend_detach(struct dvb_frontend
* fe
)
1981 if (fe
->ops
.release_sec
)
1982 fe
->ops
.release_sec(fe
);
1983 if (fe
->ops
.tuner_ops
.release
)
1984 fe
->ops
.tuner_ops
.release(fe
);
1985 if (fe
->ops
.analog_ops
.release
)
1986 fe
->ops
.analog_ops
.release(fe
);
1987 if (fe
->ops
.release
)
1988 fe
->ops
.release(fe
);
1991 EXPORT_SYMBOL(dvb_frontend_detach
);