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 /* Enables DVBv3 compatibility bits at the headers */
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <asm/processor.h>
45 #include "dvb_frontend.h"
47 #include <linux/dvb/version.h>
49 static int dvb_frontend_debug
;
50 static int dvb_shutdown_timeout
;
51 static int dvb_force_auto_inversion
;
52 static int dvb_override_tune_delay
;
53 static int dvb_powerdown_on_sleep
= 1;
54 static int dvb_mfe_wait_time
= 5;
56 module_param_named(frontend_debug
, dvb_frontend_debug
, int, 0644);
57 MODULE_PARM_DESC(frontend_debug
, "Turn on/off frontend core debugging (default:off).");
58 module_param(dvb_shutdown_timeout
, int, 0644);
59 MODULE_PARM_DESC(dvb_shutdown_timeout
, "wait <shutdown_timeout> seconds after close() before suspending hardware");
60 module_param(dvb_force_auto_inversion
, int, 0644);
61 MODULE_PARM_DESC(dvb_force_auto_inversion
, "0: normal (default), 1: INVERSION_AUTO forced always");
62 module_param(dvb_override_tune_delay
, int, 0644);
63 MODULE_PARM_DESC(dvb_override_tune_delay
, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
64 module_param(dvb_powerdown_on_sleep
, int, 0644);
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep
, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
66 module_param(dvb_mfe_wait_time
, int, 0644);
67 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)");
69 #define dprintk if (dvb_frontend_debug) printk
71 #define FESTATE_IDLE 1
72 #define FESTATE_RETUNE 2
73 #define FESTATE_TUNING_FAST 4
74 #define FESTATE_TUNING_SLOW 8
75 #define FESTATE_TUNED 16
76 #define FESTATE_ZIGZAG_FAST 32
77 #define FESTATE_ZIGZAG_SLOW 64
78 #define FESTATE_DISEQC 128
79 #define FESTATE_ERROR 256
80 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
81 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
82 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
83 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
87 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
88 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
89 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
90 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
91 * FESTATE_TUNED. The frontend has successfully locked on.
92 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
93 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
94 * FESTATE_DISEQC. A DISEQC command has just been issued.
95 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
96 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
97 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
98 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
101 #define DVB_FE_NO_EXIT 0
102 #define DVB_FE_NORMAL_EXIT 1
103 #define DVB_FE_DEVICE_REMOVED 2
105 static DEFINE_MUTEX(frontend_mutex
);
107 struct dvb_frontend_private
{
109 /* thread/frontend values */
110 struct dvb_device
*dvbdev
;
111 struct dvb_frontend_parameters parameters_in
;
112 struct dvb_frontend_parameters parameters_out
;
113 struct dvb_fe_events events
;
114 struct semaphore sem
;
115 struct list_head list_head
;
116 wait_queue_head_t wait_queue
;
117 struct task_struct
*thread
;
118 unsigned long release_jiffies
;
122 unsigned long tune_mode_flags
;
124 unsigned int reinitialise
;
128 /* swzigzag values */
130 unsigned int bending
;
132 unsigned int inversion
;
133 unsigned int auto_step
;
134 unsigned int auto_sub_step
;
135 unsigned int started_auto_step
;
136 unsigned int min_delay
;
137 unsigned int max_drift
;
138 unsigned int step_size
;
140 unsigned int check_wrapped
;
141 enum dvbfe_search algo_status
;
144 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
);
145 static int dtv_get_frontend(struct dvb_frontend
*fe
,
146 struct dvb_frontend_parameters
*p_out
);
148 static bool has_get_frontend(struct dvb_frontend
*fe
)
150 return fe
->ops
.get_frontend
;
154 * Due to DVBv3 API calls, a delivery system should be mapped into one of
155 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
156 * otherwise, a DVBv3 call will fail.
158 enum dvbv3_emulation_type
{
166 static enum dvbv3_emulation_type
dvbv3_type(u32 delivery_system
)
168 switch (delivery_system
) {
169 case SYS_DVBC_ANNEX_A
:
170 case SYS_DVBC_ANNEX_C
:
184 case SYS_DVBC_ANNEX_B
:
193 * Doesn't know how to emulate those types and/or
194 * there's no frontend driver from this type yet
195 * with some emulation code, so, we're not sure yet how
196 * to handle them, or they're not compatible with a DVBv3 call.
198 return DVBV3_UNKNOWN
;
202 static void dvb_frontend_add_event(struct dvb_frontend
*fe
, fe_status_t status
)
204 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
205 struct dvb_fe_events
*events
= &fepriv
->events
;
206 struct dvb_frontend_event
*e
;
209 dprintk ("%s\n", __func__
);
211 if ((status
& FE_HAS_LOCK
) && has_get_frontend(fe
))
212 dtv_get_frontend(fe
, &fepriv
->parameters_out
);
214 mutex_lock(&events
->mtx
);
216 wp
= (events
->eventw
+ 1) % MAX_EVENT
;
217 if (wp
== events
->eventr
) {
218 events
->overflow
= 1;
219 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
222 e
= &events
->events
[events
->eventw
];
224 e
->parameters
= fepriv
->parameters_out
;
228 mutex_unlock(&events
->mtx
);
230 wake_up_interruptible (&events
->wait_queue
);
233 static int dvb_frontend_get_event(struct dvb_frontend
*fe
,
234 struct dvb_frontend_event
*event
, int flags
)
236 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
237 struct dvb_fe_events
*events
= &fepriv
->events
;
239 dprintk ("%s\n", __func__
);
241 if (events
->overflow
) {
242 events
->overflow
= 0;
246 if (events
->eventw
== events
->eventr
) {
249 if (flags
& O_NONBLOCK
)
254 ret
= wait_event_interruptible (events
->wait_queue
,
255 events
->eventw
!= events
->eventr
);
257 if (down_interruptible (&fepriv
->sem
))
264 mutex_lock(&events
->mtx
);
265 *event
= events
->events
[events
->eventr
];
266 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
267 mutex_unlock(&events
->mtx
);
272 static void dvb_frontend_clear_events(struct dvb_frontend
*fe
)
274 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
275 struct dvb_fe_events
*events
= &fepriv
->events
;
277 mutex_lock(&events
->mtx
);
278 events
->eventr
= events
->eventw
;
279 mutex_unlock(&events
->mtx
);
282 static void dvb_frontend_init(struct dvb_frontend
*fe
)
284 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
291 if (fe
->ops
.tuner_ops
.init
) {
292 if (fe
->ops
.i2c_gate_ctrl
)
293 fe
->ops
.i2c_gate_ctrl(fe
, 1);
294 fe
->ops
.tuner_ops
.init(fe
);
295 if (fe
->ops
.i2c_gate_ctrl
)
296 fe
->ops
.i2c_gate_ctrl(fe
, 0);
300 void dvb_frontend_reinitialise(struct dvb_frontend
*fe
)
302 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
304 fepriv
->reinitialise
= 1;
305 dvb_frontend_wakeup(fe
);
307 EXPORT_SYMBOL(dvb_frontend_reinitialise
);
309 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private
*fepriv
, int locked
)
313 dprintk ("%s\n", __func__
);
316 (fepriv
->quality
) = (fepriv
->quality
* 220 + 36*256) / 256;
318 (fepriv
->quality
) = (fepriv
->quality
* 220 + 0) / 256;
320 q2
= fepriv
->quality
- 128;
323 fepriv
->delay
= fepriv
->min_delay
+ q2
* HZ
/ (128*128);
327 * Performs automatic twiddling of frontend parameters.
329 * @param fe The frontend concerned.
330 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
331 * @returns Number of complete iterations that have been performed.
333 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend
*fe
, int check_wrapped
)
338 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
339 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
, tmp
;
340 int original_inversion
= c
->inversion
;
341 u32 original_frequency
= c
->frequency
;
343 /* are we using autoinversion? */
344 autoinversion
= ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
345 (c
->inversion
== INVERSION_AUTO
));
347 /* setup parameters correctly */
349 /* calculate the lnb_drift */
350 fepriv
->lnb_drift
= fepriv
->auto_step
* fepriv
->step_size
;
352 /* wrap the auto_step if we've exceeded the maximum drift */
353 if (fepriv
->lnb_drift
> fepriv
->max_drift
) {
354 fepriv
->auto_step
= 0;
355 fepriv
->auto_sub_step
= 0;
356 fepriv
->lnb_drift
= 0;
359 /* perform inversion and +/- zigzag */
360 switch(fepriv
->auto_sub_step
) {
362 /* try with the current inversion and current drift setting */
367 if (!autoinversion
) break;
369 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
374 if (fepriv
->lnb_drift
== 0) break;
376 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
381 if (fepriv
->lnb_drift
== 0) break;
382 if (!autoinversion
) break;
384 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
385 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
391 fepriv
->auto_sub_step
= -1; /* it'll be incremented to 0 in a moment */
395 if (!ready
) fepriv
->auto_sub_step
++;
398 /* if this attempt would hit where we started, indicate a complete
399 * iteration has occurred */
400 if ((fepriv
->auto_step
== fepriv
->started_auto_step
) &&
401 (fepriv
->auto_sub_step
== 0) && check_wrapped
) {
405 dprintk("%s: drift:%i inversion:%i auto_step:%i "
406 "auto_sub_step:%i started_auto_step:%i\n",
407 __func__
, fepriv
->lnb_drift
, fepriv
->inversion
,
408 fepriv
->auto_step
, fepriv
->auto_sub_step
, fepriv
->started_auto_step
);
410 /* set the frontend itself */
411 c
->frequency
+= fepriv
->lnb_drift
;
413 c
->inversion
= fepriv
->inversion
;
415 if (fe
->ops
.set_frontend
)
416 fe_set_err
= fe
->ops
.set_frontend(fe
);
418 if (fe_set_err
< 0) {
419 fepriv
->state
= FESTATE_ERROR
;
423 c
->frequency
= original_frequency
;
424 c
->inversion
= original_inversion
;
426 fepriv
->auto_sub_step
++;
430 static void dvb_frontend_swzigzag(struct dvb_frontend
*fe
)
434 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
435 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
, tmp
;
437 /* if we've got no parameters, just keep idling */
438 if (fepriv
->state
& FESTATE_IDLE
) {
439 fepriv
->delay
= 3*HZ
;
444 /* in SCAN mode, we just set the frontend when asked and leave it alone */
445 if (fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
) {
446 if (fepriv
->state
& FESTATE_RETUNE
) {
448 if (fe
->ops
.set_frontend
)
449 retval
= fe
->ops
.set_frontend(fe
);
452 fepriv
->state
= FESTATE_ERROR
;
454 fepriv
->state
= FESTATE_TUNED
;
456 fepriv
->delay
= 3*HZ
;
461 /* get the frontend status */
462 if (fepriv
->state
& FESTATE_RETUNE
) {
465 if (fe
->ops
.read_status
)
466 fe
->ops
.read_status(fe
, &s
);
467 if (s
!= fepriv
->status
) {
468 dvb_frontend_add_event(fe
, s
);
473 /* if we're not tuned, and we have a lock, move to the TUNED state */
474 if ((fepriv
->state
& FESTATE_WAITFORLOCK
) && (s
& FE_HAS_LOCK
)) {
475 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
476 fepriv
->state
= FESTATE_TUNED
;
478 /* if we're tuned, then we have determined the correct inversion */
479 if ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
480 (c
->inversion
== INVERSION_AUTO
)) {
481 c
->inversion
= fepriv
->inversion
;
486 /* if we are tuned already, check we're still locked */
487 if (fepriv
->state
& FESTATE_TUNED
) {
488 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
490 /* we're tuned, and the lock is still good... */
491 if (s
& FE_HAS_LOCK
) {
493 } else { /* if we _WERE_ tuned, but now don't have a lock */
494 fepriv
->state
= FESTATE_ZIGZAG_FAST
;
495 fepriv
->started_auto_step
= fepriv
->auto_step
;
496 fepriv
->check_wrapped
= 0;
500 /* don't actually do anything if we're in the LOSTLOCK state,
501 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
502 if ((fepriv
->state
& FESTATE_LOSTLOCK
) &&
503 (fe
->ops
.info
.caps
& FE_CAN_RECOVER
) && (fepriv
->max_drift
== 0)) {
504 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
508 /* don't do anything if we're in the DISEQC state, since this
509 * might be someone with a motorized dish controlled by DISEQC.
510 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
511 if (fepriv
->state
& FESTATE_DISEQC
) {
512 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
516 /* if we're in the RETUNE state, set everything up for a brand
517 * new scan, keeping the current inversion setting, as the next
518 * tune is _very_ likely to require the same */
519 if (fepriv
->state
& FESTATE_RETUNE
) {
520 fepriv
->lnb_drift
= 0;
521 fepriv
->auto_step
= 0;
522 fepriv
->auto_sub_step
= 0;
523 fepriv
->started_auto_step
= 0;
524 fepriv
->check_wrapped
= 0;
528 if ((fepriv
->state
& FESTATE_SEARCHING_FAST
) || (fepriv
->state
& FESTATE_RETUNE
)) {
529 fepriv
->delay
= fepriv
->min_delay
;
532 retval
= dvb_frontend_swzigzag_autotune(fe
,
533 fepriv
->check_wrapped
);
537 /* OK, if we've run out of trials at the fast speed.
538 * Drop back to slow for the _next_ attempt */
539 fepriv
->state
= FESTATE_SEARCHING_SLOW
;
540 fepriv
->started_auto_step
= fepriv
->auto_step
;
543 fepriv
->check_wrapped
= 1;
545 /* if we've just retuned, enter the ZIGZAG_FAST state.
546 * This ensures we cannot return from an
547 * FE_SET_FRONTEND ioctl before the first frontend tune
549 if (fepriv
->state
& FESTATE_RETUNE
) {
550 fepriv
->state
= FESTATE_TUNING_FAST
;
555 if (fepriv
->state
& FESTATE_SEARCHING_SLOW
) {
556 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
558 /* Note: don't bother checking for wrapping; we stay in this
559 * state until we get a lock */
560 dvb_frontend_swzigzag_autotune(fe
, 0);
564 static int dvb_frontend_is_exiting(struct dvb_frontend
*fe
)
566 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
568 if (fepriv
->exit
!= DVB_FE_NO_EXIT
)
571 if (fepriv
->dvbdev
->writers
== 1)
572 if (time_after_eq(jiffies
, fepriv
->release_jiffies
+
573 dvb_shutdown_timeout
* HZ
))
579 static int dvb_frontend_should_wakeup(struct dvb_frontend
*fe
)
581 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
583 if (fepriv
->wakeup
) {
587 return dvb_frontend_is_exiting(fe
);
590 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
)
592 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
595 wake_up_interruptible(&fepriv
->wait_queue
);
598 static int dvb_frontend_thread(void *data
)
600 struct dvb_frontend
*fe
= data
;
601 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
603 enum dvbfe_algo algo
;
605 bool re_tune
= false;
607 dprintk("%s\n", __func__
);
609 fepriv
->check_wrapped
= 0;
611 fepriv
->delay
= 3*HZ
;
614 fepriv
->reinitialise
= 0;
616 dvb_frontend_init(fe
);
620 up(&fepriv
->sem
); /* is locked when we enter the thread... */
622 wait_event_interruptible_timeout(fepriv
->wait_queue
,
623 dvb_frontend_should_wakeup(fe
) || kthread_should_stop()
624 || freezing(current
),
627 if (kthread_should_stop() || dvb_frontend_is_exiting(fe
)) {
628 /* got signal or quitting */
629 fepriv
->exit
= DVB_FE_NORMAL_EXIT
;
636 if (down_interruptible(&fepriv
->sem
))
639 if (fepriv
->reinitialise
) {
640 dvb_frontend_init(fe
);
641 if (fe
->ops
.set_tone
&& fepriv
->tone
!= -1)
642 fe
->ops
.set_tone(fe
, fepriv
->tone
);
643 if (fe
->ops
.set_voltage
&& fepriv
->voltage
!= -1)
644 fe
->ops
.set_voltage(fe
, fepriv
->voltage
);
645 fepriv
->reinitialise
= 0;
648 /* do an iteration of the tuning loop */
649 if (fe
->ops
.get_frontend_algo
) {
650 algo
= fe
->ops
.get_frontend_algo(fe
);
653 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__
);
655 if (fepriv
->state
& FESTATE_RETUNE
) {
656 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__
);
658 fepriv
->state
= FESTATE_TUNED
;
662 fe
->ops
.tune(fe
, re_tune
, fepriv
->tune_mode_flags
, &fepriv
->delay
, &s
);
664 if (s
!= fepriv
->status
&& !(fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
)) {
665 dprintk("%s: state changed, adding current state\n", __func__
);
666 dvb_frontend_add_event(fe
, s
);
671 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__
);
672 dvb_frontend_swzigzag(fe
);
674 case DVBFE_ALGO_CUSTOM
:
675 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__
, fepriv
->state
);
676 if (fepriv
->state
& FESTATE_RETUNE
) {
677 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__
);
678 fepriv
->state
= FESTATE_TUNED
;
680 /* Case where we are going to search for a carrier
681 * User asked us to retune again for some reason, possibly
682 * requesting a search with a new set of parameters
684 if (fepriv
->algo_status
& DVBFE_ALGO_SEARCH_AGAIN
) {
685 if (fe
->ops
.search
) {
686 fepriv
->algo_status
= fe
->ops
.search(fe
);
687 /* We did do a search as was requested, the flags are
688 * now unset as well and has the flags wrt to search.
691 fepriv
->algo_status
&= ~DVBFE_ALGO_SEARCH_AGAIN
;
694 /* Track the carrier if the search was successful */
695 if (fepriv
->algo_status
!= DVBFE_ALGO_SEARCH_SUCCESS
) {
696 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
697 fepriv
->delay
= HZ
/ 2;
699 fepriv
->parameters_out
= fepriv
->parameters_in
;
700 fe
->ops
.read_status(fe
, &s
);
701 if (s
!= fepriv
->status
) {
702 dvb_frontend_add_event(fe
, s
); /* update event list */
704 if (!(s
& FE_HAS_LOCK
)) {
705 fepriv
->delay
= HZ
/ 10;
706 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
708 fepriv
->delay
= 60 * HZ
;
713 dprintk("%s: UNDEFINED ALGO !\n", __func__
);
717 dvb_frontend_swzigzag(fe
);
721 if (dvb_powerdown_on_sleep
) {
722 if (fe
->ops
.set_voltage
)
723 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_OFF
);
724 if (fe
->ops
.tuner_ops
.sleep
) {
725 if (fe
->ops
.i2c_gate_ctrl
)
726 fe
->ops
.i2c_gate_ctrl(fe
, 1);
727 fe
->ops
.tuner_ops
.sleep(fe
);
728 if (fe
->ops
.i2c_gate_ctrl
)
729 fe
->ops
.i2c_gate_ctrl(fe
, 0);
735 fepriv
->thread
= NULL
;
736 if (kthread_should_stop())
737 fepriv
->exit
= DVB_FE_DEVICE_REMOVED
;
739 fepriv
->exit
= DVB_FE_NO_EXIT
;
742 dvb_frontend_wakeup(fe
);
746 static void dvb_frontend_stop(struct dvb_frontend
*fe
)
748 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
750 dprintk ("%s\n", __func__
);
752 fepriv
->exit
= DVB_FE_NORMAL_EXIT
;
758 kthread_stop(fepriv
->thread
);
760 sema_init(&fepriv
->sem
, 1);
761 fepriv
->state
= FESTATE_IDLE
;
763 /* paranoia check in case a signal arrived */
765 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
769 s32
timeval_usec_diff(struct timeval lasttime
, struct timeval curtime
)
771 return ((curtime
.tv_usec
< lasttime
.tv_usec
) ?
772 1000000 - lasttime
.tv_usec
+ curtime
.tv_usec
:
773 curtime
.tv_usec
- lasttime
.tv_usec
);
775 EXPORT_SYMBOL(timeval_usec_diff
);
777 static inline void timeval_usec_add(struct timeval
*curtime
, u32 add_usec
)
779 curtime
->tv_usec
+= add_usec
;
780 if (curtime
->tv_usec
>= 1000000) {
781 curtime
->tv_usec
-= 1000000;
787 * Sleep until gettimeofday() > waketime + add_usec
788 * This needs to be as precise as possible, but as the delay is
789 * usually between 2ms and 32ms, it is done using a scheduled msleep
790 * followed by usleep (normally a busy-wait loop) for the remainder
792 void dvb_frontend_sleep_until(struct timeval
*waketime
, u32 add_usec
)
794 struct timeval lasttime
;
797 timeval_usec_add(waketime
, add_usec
);
799 do_gettimeofday(&lasttime
);
800 delta
= timeval_usec_diff(lasttime
, *waketime
);
802 msleep((delta
- 1500) / 1000);
803 do_gettimeofday(&lasttime
);
804 newdelta
= timeval_usec_diff(lasttime
, *waketime
);
805 delta
= (newdelta
> delta
) ? 0 : newdelta
;
810 EXPORT_SYMBOL(dvb_frontend_sleep_until
);
812 static int dvb_frontend_start(struct dvb_frontend
*fe
)
815 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
816 struct task_struct
*fe_thread
;
818 dprintk ("%s\n", __func__
);
820 if (fepriv
->thread
) {
821 if (fepriv
->exit
== DVB_FE_NO_EXIT
)
824 dvb_frontend_stop (fe
);
827 if (signal_pending(current
))
829 if (down_interruptible (&fepriv
->sem
))
832 fepriv
->state
= FESTATE_IDLE
;
833 fepriv
->exit
= DVB_FE_NO_EXIT
;
834 fepriv
->thread
= NULL
;
837 fe_thread
= kthread_run(dvb_frontend_thread
, fe
,
838 "kdvb-ad-%i-fe-%i", fe
->dvb
->num
,fe
->id
);
839 if (IS_ERR(fe_thread
)) {
840 ret
= PTR_ERR(fe_thread
);
841 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret
);
845 fepriv
->thread
= fe_thread
;
849 static void dvb_frontend_get_frequency_limits(struct dvb_frontend
*fe
,
850 u32
*freq_min
, u32
*freq_max
)
852 *freq_min
= max(fe
->ops
.info
.frequency_min
, fe
->ops
.tuner_ops
.info
.frequency_min
);
854 if (fe
->ops
.info
.frequency_max
== 0)
855 *freq_max
= fe
->ops
.tuner_ops
.info
.frequency_max
;
856 else if (fe
->ops
.tuner_ops
.info
.frequency_max
== 0)
857 *freq_max
= fe
->ops
.info
.frequency_max
;
859 *freq_max
= min(fe
->ops
.info
.frequency_max
, fe
->ops
.tuner_ops
.info
.frequency_max
);
861 if (*freq_min
== 0 || *freq_max
== 0)
862 printk(KERN_WARNING
"DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
863 fe
->dvb
->num
,fe
->id
);
866 static int dvb_frontend_check_parameters(struct dvb_frontend
*fe
)
868 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
872 /* range check: frequency */
873 dvb_frontend_get_frequency_limits(fe
, &freq_min
, &freq_max
);
874 if ((freq_min
&& c
->frequency
< freq_min
) ||
875 (freq_max
&& c
->frequency
> freq_max
)) {
876 printk(KERN_WARNING
"DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
877 fe
->dvb
->num
, fe
->id
, c
->frequency
, freq_min
, freq_max
);
881 /* range check: symbol rate */
882 switch (c
->delivery_system
) {
886 case SYS_DVBC_ANNEX_A
:
887 case SYS_DVBC_ANNEX_C
:
888 if ((fe
->ops
.info
.symbol_rate_min
&&
889 c
->symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
890 (fe
->ops
.info
.symbol_rate_max
&&
891 c
->symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
892 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
893 fe
->dvb
->num
, fe
->id
, c
->symbol_rate
,
894 fe
->ops
.info
.symbol_rate_min
,
895 fe
->ops
.info
.symbol_rate_max
);
903 * check for supported modulation
905 * This is currently hacky. Also, it only works for DVB-S & friends,
906 * and not all modulations has FE_CAN flags
908 switch (c
->delivery_system
) {
912 if ((c
->modulation
> QAM_AUTO
||
913 !((1 << (c
->modulation
+ 10)) & fe
->ops
.info
.caps
))) {
915 "DVB: adapter %i frontend %i modulation %u not supported\n",
916 fe
->dvb
->num
, fe
->id
, c
->modulation
);
921 /* FIXME: it makes sense to validate othere delsys here */
928 static int dvb_frontend_clear_cache(struct dvb_frontend
*fe
)
930 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
933 memset(c
, 0, sizeof(struct dtv_frontend_properties
));
935 c
->state
= DTV_CLEAR
;
937 c
->delivery_system
= fe
->ops
.delsys
[0];
938 dprintk("%s() Clearing cache for delivery system %d\n", __func__
,
941 c
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
942 c
->bandwidth_hz
= 0; /* AUTO */
943 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
944 c
->hierarchy
= HIERARCHY_AUTO
;
946 c
->code_rate_HP
= FEC_AUTO
;
947 c
->code_rate_LP
= FEC_AUTO
;
948 c
->fec_inner
= FEC_AUTO
;
949 c
->rolloff
= ROLLOFF_AUTO
;
950 c
->voltage
= SEC_VOLTAGE_OFF
;
951 c
->sectone
= SEC_TONE_OFF
;
952 c
->pilot
= PILOT_AUTO
;
954 c
->isdbt_partial_reception
= 0;
955 c
->isdbt_sb_mode
= 0;
956 c
->isdbt_sb_subchannel
= 0;
957 c
->isdbt_sb_segment_idx
= 0;
958 c
->isdbt_sb_segment_count
= 0;
959 c
->isdbt_layer_enabled
= 0;
960 for (i
= 0; i
< 3; i
++) {
961 c
->layer
[i
].fec
= FEC_AUTO
;
962 c
->layer
[i
].modulation
= QAM_AUTO
;
963 c
->layer
[i
].interleaving
= 0;
964 c
->layer
[i
].segment_count
= 0;
970 switch (c
->delivery_system
) {
974 c
->modulation
= QPSK
; /* implied for DVB-S in legacy API */
975 c
->rolloff
= ROLLOFF_35
;/* implied for DVB-S */
978 c
->modulation
= VSB_8
;
981 c
->modulation
= QAM_AUTO
;
988 #define _DTV_CMD(n, s, b) \
996 static struct dtv_cmds_h dtv_cmds
[DTV_MAX_COMMAND
+ 1] = {
997 _DTV_CMD(DTV_TUNE
, 1, 0),
998 _DTV_CMD(DTV_CLEAR
, 1, 0),
1001 _DTV_CMD(DTV_FREQUENCY
, 1, 0),
1002 _DTV_CMD(DTV_BANDWIDTH_HZ
, 1, 0),
1003 _DTV_CMD(DTV_MODULATION
, 1, 0),
1004 _DTV_CMD(DTV_INVERSION
, 1, 0),
1005 _DTV_CMD(DTV_DISEQC_MASTER
, 1, 1),
1006 _DTV_CMD(DTV_SYMBOL_RATE
, 1, 0),
1007 _DTV_CMD(DTV_INNER_FEC
, 1, 0),
1008 _DTV_CMD(DTV_VOLTAGE
, 1, 0),
1009 _DTV_CMD(DTV_TONE
, 1, 0),
1010 _DTV_CMD(DTV_PILOT
, 1, 0),
1011 _DTV_CMD(DTV_ROLLOFF
, 1, 0),
1012 _DTV_CMD(DTV_DELIVERY_SYSTEM
, 1, 0),
1013 _DTV_CMD(DTV_HIERARCHY
, 1, 0),
1014 _DTV_CMD(DTV_CODE_RATE_HP
, 1, 0),
1015 _DTV_CMD(DTV_CODE_RATE_LP
, 1, 0),
1016 _DTV_CMD(DTV_GUARD_INTERVAL
, 1, 0),
1017 _DTV_CMD(DTV_TRANSMISSION_MODE
, 1, 0),
1019 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION
, 1, 0),
1020 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING
, 1, 0),
1021 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID
, 1, 0),
1022 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX
, 1, 0),
1023 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT
, 1, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED
, 1, 0),
1025 _DTV_CMD(DTV_ISDBT_LAYERA_FEC
, 1, 0),
1026 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION
, 1, 0),
1027 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT
, 1, 0),
1028 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING
, 1, 0),
1029 _DTV_CMD(DTV_ISDBT_LAYERB_FEC
, 1, 0),
1030 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION
, 1, 0),
1031 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT
, 1, 0),
1032 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING
, 1, 0),
1033 _DTV_CMD(DTV_ISDBT_LAYERC_FEC
, 1, 0),
1034 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION
, 1, 0),
1035 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT
, 1, 0),
1036 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING
, 1, 0),
1038 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION
, 0, 0),
1039 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING
, 0, 0),
1040 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID
, 0, 0),
1041 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX
, 0, 0),
1042 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT
, 0, 0),
1043 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED
, 0, 0),
1044 _DTV_CMD(DTV_ISDBT_LAYERA_FEC
, 0, 0),
1045 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION
, 0, 0),
1046 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT
, 0, 0),
1047 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING
, 0, 0),
1048 _DTV_CMD(DTV_ISDBT_LAYERB_FEC
, 0, 0),
1049 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION
, 0, 0),
1050 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT
, 0, 0),
1051 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING
, 0, 0),
1052 _DTV_CMD(DTV_ISDBT_LAYERC_FEC
, 0, 0),
1053 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION
, 0, 0),
1054 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT
, 0, 0),
1055 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING
, 0, 0),
1057 _DTV_CMD(DTV_ISDBS_TS_ID
, 1, 0),
1058 _DTV_CMD(DTV_DVBT2_PLP_ID
, 1, 0),
1061 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY
, 0, 1),
1062 _DTV_CMD(DTV_API_VERSION
, 0, 0),
1063 _DTV_CMD(DTV_CODE_RATE_HP
, 0, 0),
1064 _DTV_CMD(DTV_CODE_RATE_LP
, 0, 0),
1065 _DTV_CMD(DTV_GUARD_INTERVAL
, 0, 0),
1066 _DTV_CMD(DTV_TRANSMISSION_MODE
, 0, 0),
1067 _DTV_CMD(DTV_HIERARCHY
, 0, 0),
1069 _DTV_CMD(DTV_ENUM_DELSYS
, 0, 0),
1072 static void dtv_property_dump(struct dtv_property
*tvp
)
1076 if (tvp
->cmd
<= 0 || tvp
->cmd
> DTV_MAX_COMMAND
) {
1077 printk(KERN_WARNING
"%s: tvp.cmd = 0x%08x undefined\n",
1078 __func__
, tvp
->cmd
);
1082 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1085 ,dtv_cmds
[ tvp
->cmd
].name
);
1087 if(dtv_cmds
[ tvp
->cmd
].buffer
) {
1089 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1091 ,tvp
->u
.buffer
.len
);
1093 for(i
= 0; i
< tvp
->u
.buffer
.len
; i
++)
1094 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1097 ,tvp
->u
.buffer
.data
[i
]);
1100 dprintk("%s() tvp.u.data = 0x%08x\n", __func__
, tvp
->u
.data
);
1103 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1104 * drivers can use a single set_frontend tuning function, regardless of whether
1105 * it's being used for the legacy or new API, reducing code and complexity.
1107 static int dtv_property_cache_sync(struct dvb_frontend
*fe
,
1108 struct dtv_frontend_properties
*c
,
1109 const struct dvb_frontend_parameters
*p
)
1111 c
->frequency
= p
->frequency
;
1112 c
->inversion
= p
->inversion
;
1114 switch (dvbv3_type(c
->delivery_system
)) {
1116 dprintk("%s() Preparing QPSK req\n", __func__
);
1117 c
->symbol_rate
= p
->u
.qpsk
.symbol_rate
;
1118 c
->fec_inner
= p
->u
.qpsk
.fec_inner
;
1121 dprintk("%s() Preparing QAM req\n", __func__
);
1122 c
->symbol_rate
= p
->u
.qam
.symbol_rate
;
1123 c
->fec_inner
= p
->u
.qam
.fec_inner
;
1124 c
->modulation
= p
->u
.qam
.modulation
;
1127 dprintk("%s() Preparing OFDM req\n", __func__
);
1128 switch (p
->u
.ofdm
.bandwidth
) {
1129 case BANDWIDTH_10_MHZ
:
1130 c
->bandwidth_hz
= 10000000;
1132 case BANDWIDTH_8_MHZ
:
1133 c
->bandwidth_hz
= 8000000;
1135 case BANDWIDTH_7_MHZ
:
1136 c
->bandwidth_hz
= 7000000;
1138 case BANDWIDTH_6_MHZ
:
1139 c
->bandwidth_hz
= 6000000;
1141 case BANDWIDTH_5_MHZ
:
1142 c
->bandwidth_hz
= 5000000;
1144 case BANDWIDTH_1_712_MHZ
:
1145 c
->bandwidth_hz
= 1712000;
1147 case BANDWIDTH_AUTO
:
1148 c
->bandwidth_hz
= 0;
1151 c
->code_rate_HP
= p
->u
.ofdm
.code_rate_HP
;
1152 c
->code_rate_LP
= p
->u
.ofdm
.code_rate_LP
;
1153 c
->modulation
= p
->u
.ofdm
.constellation
;
1154 c
->transmission_mode
= p
->u
.ofdm
.transmission_mode
;
1155 c
->guard_interval
= p
->u
.ofdm
.guard_interval
;
1156 c
->hierarchy
= p
->u
.ofdm
.hierarchy_information
;
1159 dprintk("%s() Preparing ATSC req\n", __func__
);
1160 c
->modulation
= p
->u
.vsb
.modulation
;
1161 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1162 c
->delivery_system
= SYS_ATSC
;
1164 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1168 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1169 __func__
, c
->delivery_system
);
1176 /* Ensure the cached values are set correctly in the frontend
1177 * legacy tuning structures, for the advanced tuning API.
1179 static int dtv_property_legacy_params_sync(struct dvb_frontend
*fe
,
1180 struct dvb_frontend_parameters
*p
)
1182 const struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1184 p
->frequency
= c
->frequency
;
1185 p
->inversion
= c
->inversion
;
1187 switch (dvbv3_type(c
->delivery_system
)) {
1190 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1191 __func__
, c
->delivery_system
);
1194 dprintk("%s() Preparing QPSK req\n", __func__
);
1195 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1196 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1199 dprintk("%s() Preparing QAM req\n", __func__
);
1200 p
->u
.qam
.symbol_rate
= c
->symbol_rate
;
1201 p
->u
.qam
.fec_inner
= c
->fec_inner
;
1202 p
->u
.qam
.modulation
= c
->modulation
;
1205 dprintk("%s() Preparing OFDM req\n", __func__
);
1207 switch (c
->bandwidth_hz
) {
1209 p
->u
.ofdm
.bandwidth
= BANDWIDTH_10_MHZ
;
1212 p
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
1215 p
->u
.ofdm
.bandwidth
= BANDWIDTH_7_MHZ
;
1218 p
->u
.ofdm
.bandwidth
= BANDWIDTH_6_MHZ
;
1221 p
->u
.ofdm
.bandwidth
= BANDWIDTH_5_MHZ
;
1224 p
->u
.ofdm
.bandwidth
= BANDWIDTH_1_712_MHZ
;
1228 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1230 p
->u
.ofdm
.code_rate_HP
= c
->code_rate_HP
;
1231 p
->u
.ofdm
.code_rate_LP
= c
->code_rate_LP
;
1232 p
->u
.ofdm
.constellation
= c
->modulation
;
1233 p
->u
.ofdm
.transmission_mode
= c
->transmission_mode
;
1234 p
->u
.ofdm
.guard_interval
= c
->guard_interval
;
1235 p
->u
.ofdm
.hierarchy_information
= c
->hierarchy
;
1238 dprintk("%s() Preparing VSB req\n", __func__
);
1239 p
->u
.vsb
.modulation
= c
->modulation
;
1246 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1247 * @fe: struct dvb_frontend pointer
1248 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1249 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1251 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1252 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1253 * If p_out is not null, it will update the DVBv3 params pointed by it.
1255 static int dtv_get_frontend(struct dvb_frontend
*fe
,
1256 struct dvb_frontend_parameters
*p_out
)
1260 if (fe
->ops
.get_frontend
) {
1261 r
= fe
->ops
.get_frontend(fe
);
1262 if (unlikely(r
< 0))
1265 dtv_property_legacy_params_sync(fe
, p_out
);
1269 /* As everything is in cache, get_frontend fops are always supported */
1273 static int dvb_frontend_ioctl_legacy(struct file
*file
,
1274 unsigned int cmd
, void *parg
);
1275 static int dvb_frontend_ioctl_properties(struct file
*file
,
1276 unsigned int cmd
, void *parg
);
1278 static int dtv_property_process_get(struct dvb_frontend
*fe
,
1279 const struct dtv_frontend_properties
*c
,
1280 struct dtv_property
*tvp
,
1286 case DTV_ENUM_DELSYS
:
1288 while (fe
->ops
.delsys
[ncaps
] && ncaps
< MAX_DELSYS
) {
1289 tvp
->u
.buffer
.data
[ncaps
] = fe
->ops
.delsys
[ncaps
];
1292 tvp
->u
.buffer
.len
= ncaps
;
1295 tvp
->u
.data
= c
->frequency
;
1297 case DTV_MODULATION
:
1298 tvp
->u
.data
= c
->modulation
;
1300 case DTV_BANDWIDTH_HZ
:
1301 tvp
->u
.data
= c
->bandwidth_hz
;
1304 tvp
->u
.data
= c
->inversion
;
1306 case DTV_SYMBOL_RATE
:
1307 tvp
->u
.data
= c
->symbol_rate
;
1310 tvp
->u
.data
= c
->fec_inner
;
1313 tvp
->u
.data
= c
->pilot
;
1316 tvp
->u
.data
= c
->rolloff
;
1318 case DTV_DELIVERY_SYSTEM
:
1319 tvp
->u
.data
= c
->delivery_system
;
1322 tvp
->u
.data
= c
->voltage
;
1325 tvp
->u
.data
= c
->sectone
;
1327 case DTV_API_VERSION
:
1328 tvp
->u
.data
= (DVB_API_VERSION
<< 8) | DVB_API_VERSION_MINOR
;
1330 case DTV_CODE_RATE_HP
:
1331 tvp
->u
.data
= c
->code_rate_HP
;
1333 case DTV_CODE_RATE_LP
:
1334 tvp
->u
.data
= c
->code_rate_LP
;
1336 case DTV_GUARD_INTERVAL
:
1337 tvp
->u
.data
= c
->guard_interval
;
1339 case DTV_TRANSMISSION_MODE
:
1340 tvp
->u
.data
= c
->transmission_mode
;
1343 tvp
->u
.data
= c
->hierarchy
;
1346 /* ISDB-T Support here */
1347 case DTV_ISDBT_PARTIAL_RECEPTION
:
1348 tvp
->u
.data
= c
->isdbt_partial_reception
;
1350 case DTV_ISDBT_SOUND_BROADCASTING
:
1351 tvp
->u
.data
= c
->isdbt_sb_mode
;
1353 case DTV_ISDBT_SB_SUBCHANNEL_ID
:
1354 tvp
->u
.data
= c
->isdbt_sb_subchannel
;
1356 case DTV_ISDBT_SB_SEGMENT_IDX
:
1357 tvp
->u
.data
= c
->isdbt_sb_segment_idx
;
1359 case DTV_ISDBT_SB_SEGMENT_COUNT
:
1360 tvp
->u
.data
= c
->isdbt_sb_segment_count
;
1362 case DTV_ISDBT_LAYER_ENABLED
:
1363 tvp
->u
.data
= c
->isdbt_layer_enabled
;
1365 case DTV_ISDBT_LAYERA_FEC
:
1366 tvp
->u
.data
= c
->layer
[0].fec
;
1368 case DTV_ISDBT_LAYERA_MODULATION
:
1369 tvp
->u
.data
= c
->layer
[0].modulation
;
1371 case DTV_ISDBT_LAYERA_SEGMENT_COUNT
:
1372 tvp
->u
.data
= c
->layer
[0].segment_count
;
1374 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING
:
1375 tvp
->u
.data
= c
->layer
[0].interleaving
;
1377 case DTV_ISDBT_LAYERB_FEC
:
1378 tvp
->u
.data
= c
->layer
[1].fec
;
1380 case DTV_ISDBT_LAYERB_MODULATION
:
1381 tvp
->u
.data
= c
->layer
[1].modulation
;
1383 case DTV_ISDBT_LAYERB_SEGMENT_COUNT
:
1384 tvp
->u
.data
= c
->layer
[1].segment_count
;
1386 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING
:
1387 tvp
->u
.data
= c
->layer
[1].interleaving
;
1389 case DTV_ISDBT_LAYERC_FEC
:
1390 tvp
->u
.data
= c
->layer
[2].fec
;
1392 case DTV_ISDBT_LAYERC_MODULATION
:
1393 tvp
->u
.data
= c
->layer
[2].modulation
;
1395 case DTV_ISDBT_LAYERC_SEGMENT_COUNT
:
1396 tvp
->u
.data
= c
->layer
[2].segment_count
;
1398 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING
:
1399 tvp
->u
.data
= c
->layer
[2].interleaving
;
1401 case DTV_ISDBS_TS_ID
:
1402 tvp
->u
.data
= c
->isdbs_ts_id
;
1404 case DTV_DVBT2_PLP_ID
:
1405 tvp
->u
.data
= c
->dvbt2_plp_id
;
1411 /* Allow the frontend to override outgoing properties */
1412 if (fe
->ops
.get_property
) {
1413 r
= fe
->ops
.get_property(fe
, tvp
);
1418 dtv_property_dump(tvp
);
1423 static int dtv_set_frontend(struct dvb_frontend
*fe
);
1425 static bool is_dvbv3_delsys(u32 delsys
)
1429 status
= (delsys
== SYS_DVBT
) || (delsys
== SYS_DVBC_ANNEX_A
) ||
1430 (delsys
== SYS_DVBS
) || (delsys
== SYS_ATSC
);
1435 static int set_delivery_system(struct dvb_frontend
*fe
, u32 desired_system
)
1438 u32 delsys
= SYS_UNDEFINED
;
1439 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1440 enum dvbv3_emulation_type type
;
1442 if (desired_system
== SYS_UNDEFINED
) {
1444 * A DVBv3 call doesn't know what's the desired system.
1445 * So, don't change the current delivery system. Instead,
1446 * find the closest DVBv3 system that matches the delivery
1449 if (is_dvbv3_delsys(c
->delivery_system
)) {
1450 dprintk("%s() Using delivery system to %d\n",
1451 __func__
, c
->delivery_system
);
1454 type
= dvbv3_type(c
->delivery_system
);
1457 desired_system
= FE_QPSK
;
1460 desired_system
= FE_QAM
;
1463 desired_system
= FE_ATSC
;
1466 desired_system
= FE_OFDM
;
1469 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1473 delsys
= c
->delivery_system
;
1476 * Check if the desired delivery system is supported
1479 while (fe
->ops
.delsys
[ncaps
] && ncaps
< MAX_DELSYS
) {
1480 if (fe
->ops
.delsys
[ncaps
] == desired_system
) {
1481 c
->delivery_system
= desired_system
;
1482 dprintk("%s() Changing delivery system to %d\n",
1483 __func__
, desired_system
);
1487 type
= dvbv3_type(desired_system
);
1490 * The delivery system is not supported. See if it can be
1492 * The emulation only works if the desired system is one of the
1493 * DVBv3 delivery systems
1495 if (!is_dvbv3_delsys(desired_system
)) {
1496 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1502 * Get the last non-DVBv3 delivery system that has the same type
1503 * of the desired system
1506 while (fe
->ops
.delsys
[ncaps
] && ncaps
< MAX_DELSYS
) {
1507 if ((dvbv3_type(fe
->ops
.delsys
[ncaps
]) == type
) &&
1508 !is_dvbv3_delsys(fe
->ops
.delsys
[ncaps
]))
1509 delsys
= fe
->ops
.delsys
[ncaps
];
1512 /* There's nothing compatible with the desired delivery system */
1513 if (delsys
== SYS_UNDEFINED
) {
1514 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1518 c
->delivery_system
= delsys
;
1522 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1523 * for older DVBv5 applications. The emulation will try to use
1524 * the auto mode for most things, and will assume that the desired
1525 * delivery system is the last one at the ops.delsys[] array
1527 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1528 __func__
, delsys
, desired_system
);
1531 * For now, handles ISDB-T calls. More code may be needed here for the
1532 * other emulated stuff
1534 if (type
== DVBV3_OFDM
) {
1535 if (c
->delivery_system
== SYS_ISDBT
) {
1536 dprintk("%s() Using defaults for SYS_ISDBT\n",
1538 if (!c
->bandwidth_hz
)
1539 c
->bandwidth_hz
= 6000000;
1541 c
->isdbt_partial_reception
= 0;
1542 c
->isdbt_sb_mode
= 0;
1543 c
->isdbt_sb_subchannel
= 0;
1544 c
->isdbt_sb_segment_idx
= 0;
1545 c
->isdbt_sb_segment_count
= 0;
1546 c
->isdbt_layer_enabled
= 0;
1547 for (i
= 0; i
< 3; i
++) {
1548 c
->layer
[i
].fec
= FEC_AUTO
;
1549 c
->layer
[i
].modulation
= QAM_AUTO
;
1550 c
->layer
[i
].interleaving
= 0;
1551 c
->layer
[i
].segment_count
= 0;
1558 static int dtv_property_process_set(struct dvb_frontend
*fe
,
1559 struct dtv_property
*tvp
,
1563 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1564 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1565 dtv_property_dump(tvp
);
1567 /* Allow the frontend to validate incoming properties */
1568 if (fe
->ops
.set_property
) {
1569 r
= fe
->ops
.set_property(fe
, tvp
);
1577 * Reset a cache of data specific to the frontend here. This does
1578 * not effect hardware.
1580 dvb_frontend_clear_cache(fe
);
1583 /* interpret the cache of data, build either a traditional frontend
1584 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1587 c
->state
= tvp
->cmd
;
1588 dprintk("%s() Finalised property cache\n", __func__
);
1590 /* Needed, due to status update */
1591 dtv_property_legacy_params_sync(fe
, &fepriv
->parameters_in
);
1593 r
= dtv_set_frontend(fe
);
1596 c
->frequency
= tvp
->u
.data
;
1598 case DTV_MODULATION
:
1599 c
->modulation
= tvp
->u
.data
;
1601 case DTV_BANDWIDTH_HZ
:
1602 c
->bandwidth_hz
= tvp
->u
.data
;
1605 c
->inversion
= tvp
->u
.data
;
1607 case DTV_SYMBOL_RATE
:
1608 c
->symbol_rate
= tvp
->u
.data
;
1611 c
->fec_inner
= tvp
->u
.data
;
1614 c
->pilot
= tvp
->u
.data
;
1617 c
->rolloff
= tvp
->u
.data
;
1619 case DTV_DELIVERY_SYSTEM
:
1620 r
= set_delivery_system(fe
, tvp
->u
.data
);
1623 c
->voltage
= tvp
->u
.data
;
1624 r
= dvb_frontend_ioctl_legacy(file
, FE_SET_VOLTAGE
,
1625 (void *)c
->voltage
);
1628 c
->sectone
= tvp
->u
.data
;
1629 r
= dvb_frontend_ioctl_legacy(file
, FE_SET_TONE
,
1630 (void *)c
->sectone
);
1632 case DTV_CODE_RATE_HP
:
1633 c
->code_rate_HP
= tvp
->u
.data
;
1635 case DTV_CODE_RATE_LP
:
1636 c
->code_rate_LP
= tvp
->u
.data
;
1638 case DTV_GUARD_INTERVAL
:
1639 c
->guard_interval
= tvp
->u
.data
;
1641 case DTV_TRANSMISSION_MODE
:
1642 c
->transmission_mode
= tvp
->u
.data
;
1645 c
->hierarchy
= tvp
->u
.data
;
1648 /* ISDB-T Support here */
1649 case DTV_ISDBT_PARTIAL_RECEPTION
:
1650 c
->isdbt_partial_reception
= tvp
->u
.data
;
1652 case DTV_ISDBT_SOUND_BROADCASTING
:
1653 c
->isdbt_sb_mode
= tvp
->u
.data
;
1655 case DTV_ISDBT_SB_SUBCHANNEL_ID
:
1656 c
->isdbt_sb_subchannel
= tvp
->u
.data
;
1658 case DTV_ISDBT_SB_SEGMENT_IDX
:
1659 c
->isdbt_sb_segment_idx
= tvp
->u
.data
;
1661 case DTV_ISDBT_SB_SEGMENT_COUNT
:
1662 c
->isdbt_sb_segment_count
= tvp
->u
.data
;
1664 case DTV_ISDBT_LAYER_ENABLED
:
1665 c
->isdbt_layer_enabled
= tvp
->u
.data
;
1667 case DTV_ISDBT_LAYERA_FEC
:
1668 c
->layer
[0].fec
= tvp
->u
.data
;
1670 case DTV_ISDBT_LAYERA_MODULATION
:
1671 c
->layer
[0].modulation
= tvp
->u
.data
;
1673 case DTV_ISDBT_LAYERA_SEGMENT_COUNT
:
1674 c
->layer
[0].segment_count
= tvp
->u
.data
;
1676 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING
:
1677 c
->layer
[0].interleaving
= tvp
->u
.data
;
1679 case DTV_ISDBT_LAYERB_FEC
:
1680 c
->layer
[1].fec
= tvp
->u
.data
;
1682 case DTV_ISDBT_LAYERB_MODULATION
:
1683 c
->layer
[1].modulation
= tvp
->u
.data
;
1685 case DTV_ISDBT_LAYERB_SEGMENT_COUNT
:
1686 c
->layer
[1].segment_count
= tvp
->u
.data
;
1688 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING
:
1689 c
->layer
[1].interleaving
= tvp
->u
.data
;
1691 case DTV_ISDBT_LAYERC_FEC
:
1692 c
->layer
[2].fec
= tvp
->u
.data
;
1694 case DTV_ISDBT_LAYERC_MODULATION
:
1695 c
->layer
[2].modulation
= tvp
->u
.data
;
1697 case DTV_ISDBT_LAYERC_SEGMENT_COUNT
:
1698 c
->layer
[2].segment_count
= tvp
->u
.data
;
1700 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING
:
1701 c
->layer
[2].interleaving
= tvp
->u
.data
;
1703 case DTV_ISDBS_TS_ID
:
1704 c
->isdbs_ts_id
= tvp
->u
.data
;
1706 case DTV_DVBT2_PLP_ID
:
1707 c
->dvbt2_plp_id
= tvp
->u
.data
;
1716 static int dvb_frontend_ioctl(struct file
*file
,
1717 unsigned int cmd
, void *parg
)
1719 struct dvb_device
*dvbdev
= file
->private_data
;
1720 struct dvb_frontend
*fe
= dvbdev
->priv
;
1721 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1722 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1723 int err
= -EOPNOTSUPP
;
1725 dprintk("%s (%d)\n", __func__
, _IOC_NR(cmd
));
1727 if (fepriv
->exit
!= DVB_FE_NO_EXIT
)
1730 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
&&
1731 (_IOC_DIR(cmd
) != _IOC_READ
|| cmd
== FE_GET_EVENT
||
1732 cmd
== FE_DISEQC_RECV_SLAVE_REPLY
))
1735 if (down_interruptible (&fepriv
->sem
))
1736 return -ERESTARTSYS
;
1738 if ((cmd
== FE_SET_PROPERTY
) || (cmd
== FE_GET_PROPERTY
))
1739 err
= dvb_frontend_ioctl_properties(file
, cmd
, parg
);
1741 c
->state
= DTV_UNDEFINED
;
1742 err
= dvb_frontend_ioctl_legacy(file
, cmd
, parg
);
1749 static int dvb_frontend_ioctl_properties(struct file
*file
,
1750 unsigned int cmd
, void *parg
)
1752 struct dvb_device
*dvbdev
= file
->private_data
;
1753 struct dvb_frontend
*fe
= dvbdev
->priv
;
1754 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1757 struct dtv_properties
*tvps
= NULL
;
1758 struct dtv_property
*tvp
= NULL
;
1761 dprintk("%s\n", __func__
);
1763 if(cmd
== FE_SET_PROPERTY
) {
1764 tvps
= (struct dtv_properties __user
*)parg
;
1766 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1767 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1769 /* Put an arbitrary limit on the number of messages that can
1770 * be sent at once */
1771 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1774 tvp
= kmalloc(tvps
->num
* sizeof(struct dtv_property
), GFP_KERNEL
);
1780 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1785 for (i
= 0; i
< tvps
->num
; i
++) {
1786 err
= dtv_property_process_set(fe
, tvp
+ i
, file
);
1789 (tvp
+ i
)->result
= err
;
1792 if (c
->state
== DTV_TUNE
)
1793 dprintk("%s() Property cache is full, tuning\n", __func__
);
1796 if(cmd
== FE_GET_PROPERTY
) {
1797 tvps
= (struct dtv_properties __user
*)parg
;
1799 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1800 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1802 /* Put an arbitrary limit on the number of messages that can
1803 * be sent at once */
1804 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1807 tvp
= kmalloc(tvps
->num
* sizeof(struct dtv_property
), GFP_KERNEL
);
1813 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1819 * Fills the cache out struct with the cache contents, plus
1820 * the data retrieved from get_frontend.
1822 dtv_get_frontend(fe
, NULL
);
1823 for (i
= 0; i
< tvps
->num
; i
++) {
1824 err
= dtv_property_process_get(fe
, c
, tvp
+ i
, file
);
1827 (tvp
+ i
)->result
= err
;
1830 if (copy_to_user(tvps
->props
, tvp
, tvps
->num
* sizeof(struct dtv_property
))) {
1843 static int dtv_set_frontend(struct dvb_frontend
*fe
)
1845 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1846 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1847 struct dvb_frontend_tune_settings fetunesettings
;
1850 if (dvb_frontend_check_parameters(fe
) < 0)
1854 * Initialize output parameters to match the values given by
1855 * the user. FE_SET_FRONTEND triggers an initial frontend event
1856 * with status = 0, which copies output parameters to userspace.
1858 * This is still needed for DVBv5 calls, due to event state update.
1860 fepriv
->parameters_out
= fepriv
->parameters_in
;
1863 * Be sure that the bandwidth will be filled for all
1864 * non-satellite systems, as tuners need to know what
1865 * low pass/Nyquist half filter should be applied, in
1866 * order to avoid inter-channel noise.
1868 * ISDB-T and DVB-T/T2 already sets bandwidth.
1869 * ATSC and DVB-C don't set, so, the core should fill it.
1871 * On DVB-C Annex A and C, the bandwidth is a function of
1872 * the roll-off and symbol rate. Annex B defines different
1873 * roll-off factors depending on the modulation. Fortunately,
1874 * Annex B is only used with 6MHz, so there's no need to
1877 * While not officially supported, a side effect of handling it at
1878 * the cache level is that a program could retrieve the bandwidth
1879 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1881 switch (c
->delivery_system
) {
1883 case SYS_DVBC_ANNEX_B
:
1884 c
->bandwidth_hz
= 6000000;
1886 case SYS_DVBC_ANNEX_A
:
1889 case SYS_DVBC_ANNEX_C
:
1896 c
->bandwidth_hz
= (c
->symbol_rate
* rolloff
) / 100;
1898 /* force auto frequency inversion if requested */
1899 if (dvb_force_auto_inversion
)
1900 c
->inversion
= INVERSION_AUTO
;
1903 * without hierarchical coding code_rate_LP is irrelevant,
1904 * so we tolerate the otherwise invalid FEC_NONE setting
1906 if (c
->hierarchy
== HIERARCHY_NONE
&& c
->code_rate_LP
== FEC_NONE
)
1907 c
->code_rate_LP
= FEC_AUTO
;
1909 /* get frontend-specific tuning settings */
1910 memset(&fetunesettings
, 0, sizeof(struct dvb_frontend_tune_settings
));
1911 if (fe
->ops
.get_tune_settings
&& (fe
->ops
.get_tune_settings(fe
, &fetunesettings
) == 0)) {
1912 fepriv
->min_delay
= (fetunesettings
.min_delay_ms
* HZ
) / 1000;
1913 fepriv
->max_drift
= fetunesettings
.max_drift
;
1914 fepriv
->step_size
= fetunesettings
.step_size
;
1916 /* default values */
1917 switch (c
->delivery_system
) {
1918 case SYS_DVBC_ANNEX_A
:
1919 case SYS_DVBC_ANNEX_C
:
1920 fepriv
->min_delay
= HZ
/ 20;
1921 fepriv
->step_size
= c
->symbol_rate
/ 16000;
1922 fepriv
->max_drift
= c
->symbol_rate
/ 2000;
1928 fepriv
->min_delay
= HZ
/ 20;
1929 fepriv
->step_size
= fe
->ops
.info
.frequency_stepsize
* 2;
1930 fepriv
->max_drift
= (fe
->ops
.info
.frequency_stepsize
* 2) + 1;
1934 * FIXME: This sounds wrong! if freqency_stepsize is
1935 * defined by the frontend, why not use it???
1937 fepriv
->min_delay
= HZ
/ 20;
1938 fepriv
->step_size
= 0; /* no zigzag */
1939 fepriv
->max_drift
= 0;
1943 if (dvb_override_tune_delay
> 0)
1944 fepriv
->min_delay
= (dvb_override_tune_delay
* HZ
) / 1000;
1946 fepriv
->state
= FESTATE_RETUNE
;
1948 /* Request the search algorithm to search */
1949 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
1951 dvb_frontend_clear_events(fe
);
1952 dvb_frontend_add_event(fe
, 0);
1953 dvb_frontend_wakeup(fe
);
1960 static int dvb_frontend_ioctl_legacy(struct file
*file
,
1961 unsigned int cmd
, void *parg
)
1963 struct dvb_device
*dvbdev
= file
->private_data
;
1964 struct dvb_frontend
*fe
= dvbdev
->priv
;
1965 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1966 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1967 int cb_err
, err
= -EOPNOTSUPP
;
1969 if (fe
->dvb
->fe_ioctl_override
) {
1970 cb_err
= fe
->dvb
->fe_ioctl_override(fe
, cmd
, parg
,
1976 /* fe_ioctl_override returning 0 allows
1977 * dvb-core to continue handling the ioctl */
1982 struct dvb_frontend_info
* info
= parg
;
1983 memcpy(info
, &fe
->ops
.info
, sizeof(struct dvb_frontend_info
));
1984 dvb_frontend_get_frequency_limits(fe
, &info
->frequency_min
, &info
->frequency_max
);
1987 * Associate the 4 delivery systems supported by DVBv3
1988 * API with their DVBv5 counterpart. For the other standards,
1989 * use the closest type, assuming that it would hopefully
1990 * work with a DVBv3 application.
1991 * It should be noticed that, on multi-frontend devices with
1992 * different types (terrestrial and cable, for example),
1993 * a pure DVBv3 application won't be able to use all delivery
1994 * systems. Yet, changing the DVBv5 cache to the other delivery
1995 * system should be enough for making it work.
1997 switch (dvbv3_type(c
->delivery_system
)) {
1999 fe
->ops
.info
.type
= FE_QPSK
;
2002 fe
->ops
.info
.type
= FE_ATSC
;
2005 fe
->ops
.info
.type
= FE_QAM
;
2008 fe
->ops
.info
.type
= FE_OFDM
;
2012 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2013 __func__
, c
->delivery_system
);
2014 fe
->ops
.info
.type
= FE_OFDM
;
2017 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2018 * do it, it is done for it. */
2019 info
->caps
|= FE_CAN_INVERSION_AUTO
;
2024 case FE_READ_STATUS
: {
2025 fe_status_t
* status
= parg
;
2027 /* if retune was requested but hasn't occurred yet, prevent
2028 * that user get signal state from previous tuning */
2029 if (fepriv
->state
== FESTATE_RETUNE
||
2030 fepriv
->state
== FESTATE_ERROR
) {
2036 if (fe
->ops
.read_status
)
2037 err
= fe
->ops
.read_status(fe
, status
);
2041 if (fe
->ops
.read_ber
)
2042 err
= fe
->ops
.read_ber(fe
, (__u32
*) parg
);
2045 case FE_READ_SIGNAL_STRENGTH
:
2046 if (fe
->ops
.read_signal_strength
)
2047 err
= fe
->ops
.read_signal_strength(fe
, (__u16
*) parg
);
2051 if (fe
->ops
.read_snr
)
2052 err
= fe
->ops
.read_snr(fe
, (__u16
*) parg
);
2055 case FE_READ_UNCORRECTED_BLOCKS
:
2056 if (fe
->ops
.read_ucblocks
)
2057 err
= fe
->ops
.read_ucblocks(fe
, (__u32
*) parg
);
2061 case FE_DISEQC_RESET_OVERLOAD
:
2062 if (fe
->ops
.diseqc_reset_overload
) {
2063 err
= fe
->ops
.diseqc_reset_overload(fe
);
2064 fepriv
->state
= FESTATE_DISEQC
;
2069 case FE_DISEQC_SEND_MASTER_CMD
:
2070 if (fe
->ops
.diseqc_send_master_cmd
) {
2071 err
= fe
->ops
.diseqc_send_master_cmd(fe
, (struct dvb_diseqc_master_cmd
*) parg
);
2072 fepriv
->state
= FESTATE_DISEQC
;
2077 case FE_DISEQC_SEND_BURST
:
2078 if (fe
->ops
.diseqc_send_burst
) {
2079 err
= fe
->ops
.diseqc_send_burst(fe
, (fe_sec_mini_cmd_t
) parg
);
2080 fepriv
->state
= FESTATE_DISEQC
;
2086 if (fe
->ops
.set_tone
) {
2087 err
= fe
->ops
.set_tone(fe
, (fe_sec_tone_mode_t
) parg
);
2088 fepriv
->tone
= (fe_sec_tone_mode_t
) parg
;
2089 fepriv
->state
= FESTATE_DISEQC
;
2094 case FE_SET_VOLTAGE
:
2095 if (fe
->ops
.set_voltage
) {
2096 err
= fe
->ops
.set_voltage(fe
, (fe_sec_voltage_t
) parg
);
2097 fepriv
->voltage
= (fe_sec_voltage_t
) parg
;
2098 fepriv
->state
= FESTATE_DISEQC
;
2103 case FE_DISHNETWORK_SEND_LEGACY_CMD
:
2104 if (fe
->ops
.dishnetwork_send_legacy_command
) {
2105 err
= fe
->ops
.dishnetwork_send_legacy_command(fe
, (unsigned long) parg
);
2106 fepriv
->state
= FESTATE_DISEQC
;
2108 } else if (fe
->ops
.set_voltage
) {
2110 * NOTE: This is a fallback condition. Some frontends
2111 * (stv0299 for instance) take longer than 8msec to
2112 * respond to a set_voltage command. Those switches
2113 * need custom routines to switch properly. For all
2114 * other frontends, the following should work ok.
2115 * Dish network legacy switches (as used by Dish500)
2116 * are controlled by sending 9-bit command words
2117 * spaced 8msec apart.
2118 * the actual command word is switch/port dependent
2119 * so it is up to the userspace application to send
2120 * the right command.
2121 * The command must always start with a '0' after
2122 * initialization, so parg is 8 bits and does not
2123 * include the initialization or start bit
2125 unsigned long swcmd
= ((unsigned long) parg
) << 1;
2126 struct timeval nexttime
;
2127 struct timeval tv
[10];
2130 if (dvb_frontend_debug
)
2131 printk("%s switch command: 0x%04lx\n", __func__
, swcmd
);
2132 do_gettimeofday(&nexttime
);
2133 if (dvb_frontend_debug
)
2134 memcpy(&tv
[0], &nexttime
, sizeof(struct timeval
));
2135 /* before sending a command, initialize by sending
2136 * a 32ms 18V to the switch
2138 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_18
);
2139 dvb_frontend_sleep_until(&nexttime
, 32000);
2141 for (i
= 0; i
< 9; i
++) {
2142 if (dvb_frontend_debug
)
2143 do_gettimeofday(&tv
[i
+ 1]);
2144 if ((swcmd
& 0x01) != last
) {
2145 /* set voltage to (last ? 13V : 18V) */
2146 fe
->ops
.set_voltage(fe
, (last
) ? SEC_VOLTAGE_13
: SEC_VOLTAGE_18
);
2147 last
= (last
) ? 0 : 1;
2151 dvb_frontend_sleep_until(&nexttime
, 8000);
2153 if (dvb_frontend_debug
) {
2154 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2155 __func__
, fe
->dvb
->num
);
2156 for (i
= 1; i
< 10; i
++)
2157 printk("%d: %d\n", i
, timeval_usec_diff(tv
[i
-1] , tv
[i
]));
2160 fepriv
->state
= FESTATE_DISEQC
;
2165 case FE_DISEQC_RECV_SLAVE_REPLY
:
2166 if (fe
->ops
.diseqc_recv_slave_reply
)
2167 err
= fe
->ops
.diseqc_recv_slave_reply(fe
, (struct dvb_diseqc_slave_reply
*) parg
);
2170 case FE_ENABLE_HIGH_LNB_VOLTAGE
:
2171 if (fe
->ops
.enable_high_lnb_voltage
)
2172 err
= fe
->ops
.enable_high_lnb_voltage(fe
, (long) parg
);
2175 case FE_SET_FRONTEND
:
2176 /* Synchronise DVBv5 parameters from DVBv3 */
2177 memcpy (&fepriv
->parameters_in
, parg
,
2178 sizeof (struct dvb_frontend_parameters
));
2180 err
= set_delivery_system(fe
, SYS_UNDEFINED
);
2184 err
= dtv_property_cache_sync(fe
, c
, &fepriv
->parameters_in
);
2187 err
= dtv_set_frontend(fe
);
2190 err
= dvb_frontend_get_event (fe
, parg
, file
->f_flags
);
2193 case FE_GET_FRONTEND
:
2194 err
= dtv_get_frontend(fe
, &fepriv
->parameters_out
);
2196 memcpy(parg
, &fepriv
->parameters_out
,
2197 sizeof(struct dvb_frontend_parameters
));
2200 case FE_SET_FRONTEND_TUNE_MODE
:
2201 fepriv
->tune_mode_flags
= (unsigned long) parg
;
2206 if (fe
->dvb
->fe_ioctl_override
) {
2207 cb_err
= fe
->dvb
->fe_ioctl_override(fe
, cmd
, parg
,
2217 static unsigned int dvb_frontend_poll(struct file
*file
, struct poll_table_struct
*wait
)
2219 struct dvb_device
*dvbdev
= file
->private_data
;
2220 struct dvb_frontend
*fe
= dvbdev
->priv
;
2221 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
2223 dprintk ("%s\n", __func__
);
2225 poll_wait (file
, &fepriv
->events
.wait_queue
, wait
);
2227 if (fepriv
->events
.eventw
!= fepriv
->events
.eventr
)
2228 return (POLLIN
| POLLRDNORM
| POLLPRI
);
2233 static int dvb_frontend_open(struct inode
*inode
, struct file
*file
)
2235 struct dvb_device
*dvbdev
= file
->private_data
;
2236 struct dvb_frontend
*fe
= dvbdev
->priv
;
2237 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
2238 struct dvb_adapter
*adapter
= fe
->dvb
;
2241 dprintk ("%s\n", __func__
);
2242 if (fepriv
->exit
== DVB_FE_DEVICE_REMOVED
)
2245 if (adapter
->mfe_shared
) {
2246 mutex_lock (&adapter
->mfe_lock
);
2248 if (adapter
->mfe_dvbdev
== NULL
)
2249 adapter
->mfe_dvbdev
= dvbdev
;
2251 else if (adapter
->mfe_dvbdev
!= dvbdev
) {
2253 *mfedev
= adapter
->mfe_dvbdev
;
2255 *mfe
= mfedev
->priv
;
2256 struct dvb_frontend_private
2257 *mfepriv
= mfe
->frontend_priv
;
2258 int mferetry
= (dvb_mfe_wait_time
<< 1);
2260 mutex_unlock (&adapter
->mfe_lock
);
2261 while (mferetry
-- && (mfedev
->users
!= -1 ||
2262 mfepriv
->thread
!= NULL
)) {
2263 if(msleep_interruptible(500)) {
2264 if(signal_pending(current
))
2269 mutex_lock (&adapter
->mfe_lock
);
2270 if(adapter
->mfe_dvbdev
!= dvbdev
) {
2271 mfedev
= adapter
->mfe_dvbdev
;
2273 mfepriv
= mfe
->frontend_priv
;
2274 if (mfedev
->users
!= -1 ||
2275 mfepriv
->thread
!= NULL
) {
2276 mutex_unlock (&adapter
->mfe_lock
);
2279 adapter
->mfe_dvbdev
= dvbdev
;
2284 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
) {
2285 if ((ret
= fe
->ops
.ts_bus_ctrl(fe
, 1)) < 0)
2288 /* If we took control of the bus, we need to force
2289 reinitialization. This is because many ts_bus_ctrl()
2290 functions strobe the RESET pin on the demod, and if the
2291 frontend thread already exists then the dvb_init() routine
2292 won't get called (which is what usually does initial
2293 register configuration). */
2294 fepriv
->reinitialise
= 1;
2297 if ((ret
= dvb_generic_open (inode
, file
)) < 0)
2300 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
2301 /* normal tune mode when opened R/W */
2302 fepriv
->tune_mode_flags
&= ~FE_TUNE_MODE_ONESHOT
;
2304 fepriv
->voltage
= -1;
2306 ret
= dvb_frontend_start (fe
);
2310 /* empty event queue */
2311 fepriv
->events
.eventr
= fepriv
->events
.eventw
= 0;
2314 if (adapter
->mfe_shared
)
2315 mutex_unlock (&adapter
->mfe_lock
);
2319 dvb_generic_release(inode
, file
);
2321 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
)
2322 fe
->ops
.ts_bus_ctrl(fe
, 0);
2324 if (adapter
->mfe_shared
)
2325 mutex_unlock (&adapter
->mfe_lock
);
2329 static int dvb_frontend_release(struct inode
*inode
, struct file
*file
)
2331 struct dvb_device
*dvbdev
= file
->private_data
;
2332 struct dvb_frontend
*fe
= dvbdev
->priv
;
2333 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
2336 dprintk ("%s\n", __func__
);
2338 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
2339 fepriv
->release_jiffies
= jiffies
;
2343 ret
= dvb_generic_release (inode
, file
);
2345 if (dvbdev
->users
== -1) {
2346 wake_up(&fepriv
->wait_queue
);
2347 if (fepriv
->exit
!= DVB_FE_NO_EXIT
) {
2348 fops_put(file
->f_op
);
2350 wake_up(&dvbdev
->wait_queue
);
2352 if (fe
->ops
.ts_bus_ctrl
)
2353 fe
->ops
.ts_bus_ctrl(fe
, 0);
2359 static const struct file_operations dvb_frontend_fops
= {
2360 .owner
= THIS_MODULE
,
2361 .unlocked_ioctl
= dvb_generic_ioctl
,
2362 .poll
= dvb_frontend_poll
,
2363 .open
= dvb_frontend_open
,
2364 .release
= dvb_frontend_release
,
2365 .llseek
= noop_llseek
,
2368 int dvb_register_frontend(struct dvb_adapter
* dvb
,
2369 struct dvb_frontend
* fe
)
2371 struct dvb_frontend_private
*fepriv
;
2372 static const struct dvb_device dvbdev_template
= {
2376 .fops
= &dvb_frontend_fops
,
2377 .kernel_ioctl
= dvb_frontend_ioctl
2380 dprintk ("%s\n", __func__
);
2382 if (mutex_lock_interruptible(&frontend_mutex
))
2383 return -ERESTARTSYS
;
2385 fe
->frontend_priv
= kzalloc(sizeof(struct dvb_frontend_private
), GFP_KERNEL
);
2386 if (fe
->frontend_priv
== NULL
) {
2387 mutex_unlock(&frontend_mutex
);
2390 fepriv
= fe
->frontend_priv
;
2392 sema_init(&fepriv
->sem
, 1);
2393 init_waitqueue_head (&fepriv
->wait_queue
);
2394 init_waitqueue_head (&fepriv
->events
.wait_queue
);
2395 mutex_init(&fepriv
->events
.mtx
);
2397 fepriv
->inversion
= INVERSION_OFF
;
2399 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2404 dvb_register_device (fe
->dvb
, &fepriv
->dvbdev
, &dvbdev_template
,
2405 fe
, DVB_DEVICE_FRONTEND
);
2408 * Initialize the cache to the proper values according with the
2409 * first supported delivery system (ops->delsys[0])
2411 dvb_frontend_clear_cache(fe
);
2413 mutex_unlock(&frontend_mutex
);
2416 EXPORT_SYMBOL(dvb_register_frontend
);
2418 int dvb_unregister_frontend(struct dvb_frontend
* fe
)
2420 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
2421 dprintk ("%s\n", __func__
);
2423 mutex_lock(&frontend_mutex
);
2424 dvb_frontend_stop (fe
);
2425 mutex_unlock(&frontend_mutex
);
2427 if (fepriv
->dvbdev
->users
< -1)
2428 wait_event(fepriv
->dvbdev
->wait_queue
,
2429 fepriv
->dvbdev
->users
==-1);
2431 mutex_lock(&frontend_mutex
);
2432 dvb_unregister_device (fepriv
->dvbdev
);
2434 /* fe is invalid now */
2436 mutex_unlock(&frontend_mutex
);
2439 EXPORT_SYMBOL(dvb_unregister_frontend
);
2441 #ifdef CONFIG_MEDIA_ATTACH
2442 void dvb_frontend_detach(struct dvb_frontend
* fe
)
2446 if (fe
->ops
.release_sec
) {
2447 fe
->ops
.release_sec(fe
);
2448 symbol_put_addr(fe
->ops
.release_sec
);
2450 if (fe
->ops
.tuner_ops
.release
) {
2451 fe
->ops
.tuner_ops
.release(fe
);
2452 symbol_put_addr(fe
->ops
.tuner_ops
.release
);
2454 if (fe
->ops
.analog_ops
.release
) {
2455 fe
->ops
.analog_ops
.release(fe
);
2456 symbol_put_addr(fe
->ops
.analog_ops
.release
);
2458 ptr
= (void*)fe
->ops
.release
;
2460 fe
->ops
.release(fe
);
2461 symbol_put_addr(ptr
);
2465 void dvb_frontend_detach(struct dvb_frontend
* fe
)
2467 if (fe
->ops
.release_sec
)
2468 fe
->ops
.release_sec(fe
);
2469 if (fe
->ops
.tuner_ops
.release
)
2470 fe
->ops
.tuner_ops
.release(fe
);
2471 if (fe
->ops
.analog_ops
.release
)
2472 fe
->ops
.analog_ops
.release(fe
);
2473 if (fe
->ops
.release
)
2474 fe
->ops
.release(fe
);
2477 EXPORT_SYMBOL(dvb_frontend_detach
);