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/semaphore.h>
35 #include <linux/module.h>
36 #include <linux/list.h>
37 #include <linux/freezer.h>
38 #include <linux/jiffies.h>
39 #include <linux/kthread.h>
40 #include <asm/processor.h>
42 #include "dvb_frontend.h"
44 #include <linux/dvb/version.h>
46 static int dvb_frontend_debug
;
47 static int dvb_shutdown_timeout
;
48 static int dvb_force_auto_inversion
;
49 static int dvb_override_tune_delay
;
50 static int dvb_powerdown_on_sleep
= 1;
51 static int dvb_mfe_wait_time
= 5;
53 module_param_named(frontend_debug
, dvb_frontend_debug
, int, 0644);
54 MODULE_PARM_DESC(frontend_debug
, "Turn on/off frontend core debugging (default:off).");
55 module_param(dvb_shutdown_timeout
, int, 0644);
56 MODULE_PARM_DESC(dvb_shutdown_timeout
, "wait <shutdown_timeout> seconds after close() before suspending hardware");
57 module_param(dvb_force_auto_inversion
, int, 0644);
58 MODULE_PARM_DESC(dvb_force_auto_inversion
, "0: normal (default), 1: INVERSION_AUTO forced always");
59 module_param(dvb_override_tune_delay
, int, 0644);
60 MODULE_PARM_DESC(dvb_override_tune_delay
, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
61 module_param(dvb_powerdown_on_sleep
, int, 0644);
62 MODULE_PARM_DESC(dvb_powerdown_on_sleep
, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
63 module_param(dvb_mfe_wait_time
, int, 0644);
64 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)");
66 #define dprintk if (dvb_frontend_debug) printk
68 #define FESTATE_IDLE 1
69 #define FESTATE_RETUNE 2
70 #define FESTATE_TUNING_FAST 4
71 #define FESTATE_TUNING_SLOW 8
72 #define FESTATE_TUNED 16
73 #define FESTATE_ZIGZAG_FAST 32
74 #define FESTATE_ZIGZAG_SLOW 64
75 #define FESTATE_DISEQC 128
76 #define FESTATE_ERROR 256
77 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
78 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
79 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
80 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
84 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
85 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
86 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
87 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
88 * FESTATE_TUNED. The frontend has successfully locked on.
89 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
90 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
91 * FESTATE_DISEQC. A DISEQC command has just been issued.
92 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
93 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
94 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
95 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
98 #define DVB_FE_NO_EXIT 0
99 #define DVB_FE_NORMAL_EXIT 1
100 #define DVB_FE_DEVICE_REMOVED 2
102 static DEFINE_MUTEX(frontend_mutex
);
104 struct dvb_frontend_private
{
106 /* thread/frontend values */
107 struct dvb_device
*dvbdev
;
108 struct dvb_frontend_parameters parameters
;
109 struct dvb_fe_events events
;
110 struct semaphore sem
;
111 struct list_head list_head
;
112 wait_queue_head_t wait_queue
;
113 struct task_struct
*thread
;
114 unsigned long release_jiffies
;
118 unsigned long tune_mode_flags
;
120 unsigned int reinitialise
;
124 /* swzigzag values */
126 unsigned int bending
;
128 unsigned int inversion
;
129 unsigned int auto_step
;
130 unsigned int auto_sub_step
;
131 unsigned int started_auto_step
;
132 unsigned int min_delay
;
133 unsigned int max_drift
;
134 unsigned int step_size
;
136 unsigned int check_wrapped
;
137 enum dvbfe_search algo_status
;
140 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
);
142 static void dvb_frontend_add_event(struct dvb_frontend
*fe
, fe_status_t status
)
144 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
145 struct dvb_fe_events
*events
= &fepriv
->events
;
146 struct dvb_frontend_event
*e
;
149 dprintk ("%s\n", __func__
);
151 if (mutex_lock_interruptible (&events
->mtx
))
154 wp
= (events
->eventw
+ 1) % MAX_EVENT
;
156 if (wp
== events
->eventr
) {
157 events
->overflow
= 1;
158 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
161 e
= &events
->events
[events
->eventw
];
163 memcpy (&e
->parameters
, &fepriv
->parameters
,
164 sizeof (struct dvb_frontend_parameters
));
166 if (status
& FE_HAS_LOCK
)
167 if (fe
->ops
.get_frontend
)
168 fe
->ops
.get_frontend(fe
, &e
->parameters
);
172 mutex_unlock(&events
->mtx
);
176 wake_up_interruptible (&events
->wait_queue
);
179 static int dvb_frontend_get_event(struct dvb_frontend
*fe
,
180 struct dvb_frontend_event
*event
, int flags
)
182 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
183 struct dvb_fe_events
*events
= &fepriv
->events
;
185 dprintk ("%s\n", __func__
);
187 if (events
->overflow
) {
188 events
->overflow
= 0;
192 if (events
->eventw
== events
->eventr
) {
195 if (flags
& O_NONBLOCK
)
200 ret
= wait_event_interruptible (events
->wait_queue
,
201 events
->eventw
!= events
->eventr
);
203 if (down_interruptible (&fepriv
->sem
))
210 if (mutex_lock_interruptible (&events
->mtx
))
213 memcpy (event
, &events
->events
[events
->eventr
],
214 sizeof(struct dvb_frontend_event
));
216 events
->eventr
= (events
->eventr
+ 1) % MAX_EVENT
;
218 mutex_unlock(&events
->mtx
);
223 static void dvb_frontend_init(struct dvb_frontend
*fe
)
225 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
232 if (fe
->ops
.tuner_ops
.init
) {
233 if (fe
->ops
.i2c_gate_ctrl
)
234 fe
->ops
.i2c_gate_ctrl(fe
, 1);
235 fe
->ops
.tuner_ops
.init(fe
);
236 if (fe
->ops
.i2c_gate_ctrl
)
237 fe
->ops
.i2c_gate_ctrl(fe
, 0);
241 void dvb_frontend_reinitialise(struct dvb_frontend
*fe
)
243 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
245 fepriv
->reinitialise
= 1;
246 dvb_frontend_wakeup(fe
);
248 EXPORT_SYMBOL(dvb_frontend_reinitialise
);
250 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private
*fepriv
, int locked
)
254 dprintk ("%s\n", __func__
);
257 (fepriv
->quality
) = (fepriv
->quality
* 220 + 36*256) / 256;
259 (fepriv
->quality
) = (fepriv
->quality
* 220 + 0) / 256;
261 q2
= fepriv
->quality
- 128;
264 fepriv
->delay
= fepriv
->min_delay
+ q2
* HZ
/ (128*128);
268 * Performs automatic twiddling of frontend parameters.
270 * @param fe The frontend concerned.
271 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
272 * @returns Number of complete iterations that have been performed.
274 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend
*fe
, int check_wrapped
)
279 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
280 int original_inversion
= fepriv
->parameters
.inversion
;
281 u32 original_frequency
= fepriv
->parameters
.frequency
;
283 /* are we using autoinversion? */
284 autoinversion
= ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
285 (fepriv
->parameters
.inversion
== INVERSION_AUTO
));
287 /* setup parameters correctly */
289 /* calculate the lnb_drift */
290 fepriv
->lnb_drift
= fepriv
->auto_step
* fepriv
->step_size
;
292 /* wrap the auto_step if we've exceeded the maximum drift */
293 if (fepriv
->lnb_drift
> fepriv
->max_drift
) {
294 fepriv
->auto_step
= 0;
295 fepriv
->auto_sub_step
= 0;
296 fepriv
->lnb_drift
= 0;
299 /* perform inversion and +/- zigzag */
300 switch(fepriv
->auto_sub_step
) {
302 /* try with the current inversion and current drift setting */
307 if (!autoinversion
) break;
309 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
314 if (fepriv
->lnb_drift
== 0) break;
316 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
321 if (fepriv
->lnb_drift
== 0) break;
322 if (!autoinversion
) break;
324 fepriv
->inversion
= (fepriv
->inversion
== INVERSION_OFF
) ? INVERSION_ON
: INVERSION_OFF
;
325 fepriv
->lnb_drift
= -fepriv
->lnb_drift
;
331 fepriv
->auto_sub_step
= -1; /* it'll be incremented to 0 in a moment */
335 if (!ready
) fepriv
->auto_sub_step
++;
338 /* if this attempt would hit where we started, indicate a complete
339 * iteration has occurred */
340 if ((fepriv
->auto_step
== fepriv
->started_auto_step
) &&
341 (fepriv
->auto_sub_step
== 0) && check_wrapped
) {
345 dprintk("%s: drift:%i inversion:%i auto_step:%i "
346 "auto_sub_step:%i started_auto_step:%i\n",
347 __func__
, fepriv
->lnb_drift
, fepriv
->inversion
,
348 fepriv
->auto_step
, fepriv
->auto_sub_step
, fepriv
->started_auto_step
);
350 /* set the frontend itself */
351 fepriv
->parameters
.frequency
+= fepriv
->lnb_drift
;
353 fepriv
->parameters
.inversion
= fepriv
->inversion
;
354 if (fe
->ops
.set_frontend
)
355 fe_set_err
= fe
->ops
.set_frontend(fe
, &fepriv
->parameters
);
356 if (fe_set_err
< 0) {
357 fepriv
->state
= FESTATE_ERROR
;
361 fepriv
->parameters
.frequency
= original_frequency
;
362 fepriv
->parameters
.inversion
= original_inversion
;
364 fepriv
->auto_sub_step
++;
368 static void dvb_frontend_swzigzag(struct dvb_frontend
*fe
)
372 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
374 /* if we've got no parameters, just keep idling */
375 if (fepriv
->state
& FESTATE_IDLE
) {
376 fepriv
->delay
= 3*HZ
;
381 /* in SCAN mode, we just set the frontend when asked and leave it alone */
382 if (fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
) {
383 if (fepriv
->state
& FESTATE_RETUNE
) {
384 if (fe
->ops
.set_frontend
)
385 retval
= fe
->ops
.set_frontend(fe
,
386 &fepriv
->parameters
);
388 fepriv
->state
= FESTATE_ERROR
;
390 fepriv
->state
= FESTATE_TUNED
;
392 fepriv
->delay
= 3*HZ
;
397 /* get the frontend status */
398 if (fepriv
->state
& FESTATE_RETUNE
) {
401 if (fe
->ops
.read_status
)
402 fe
->ops
.read_status(fe
, &s
);
403 if (s
!= fepriv
->status
) {
404 dvb_frontend_add_event(fe
, s
);
409 /* if we're not tuned, and we have a lock, move to the TUNED state */
410 if ((fepriv
->state
& FESTATE_WAITFORLOCK
) && (s
& FE_HAS_LOCK
)) {
411 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
412 fepriv
->state
= FESTATE_TUNED
;
414 /* if we're tuned, then we have determined the correct inversion */
415 if ((!(fe
->ops
.info
.caps
& FE_CAN_INVERSION_AUTO
)) &&
416 (fepriv
->parameters
.inversion
== INVERSION_AUTO
)) {
417 fepriv
->parameters
.inversion
= fepriv
->inversion
;
422 /* if we are tuned already, check we're still locked */
423 if (fepriv
->state
& FESTATE_TUNED
) {
424 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
426 /* we're tuned, and the lock is still good... */
427 if (s
& FE_HAS_LOCK
) {
429 } else { /* if we _WERE_ tuned, but now don't have a lock */
430 fepriv
->state
= FESTATE_ZIGZAG_FAST
;
431 fepriv
->started_auto_step
= fepriv
->auto_step
;
432 fepriv
->check_wrapped
= 0;
436 /* don't actually do anything if we're in the LOSTLOCK state,
437 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
438 if ((fepriv
->state
& FESTATE_LOSTLOCK
) &&
439 (fe
->ops
.info
.caps
& FE_CAN_RECOVER
) && (fepriv
->max_drift
== 0)) {
440 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
444 /* don't do anything if we're in the DISEQC state, since this
445 * might be someone with a motorized dish controlled by DISEQC.
446 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
447 if (fepriv
->state
& FESTATE_DISEQC
) {
448 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
452 /* if we're in the RETUNE state, set everything up for a brand
453 * new scan, keeping the current inversion setting, as the next
454 * tune is _very_ likely to require the same */
455 if (fepriv
->state
& FESTATE_RETUNE
) {
456 fepriv
->lnb_drift
= 0;
457 fepriv
->auto_step
= 0;
458 fepriv
->auto_sub_step
= 0;
459 fepriv
->started_auto_step
= 0;
460 fepriv
->check_wrapped
= 0;
464 if ((fepriv
->state
& FESTATE_SEARCHING_FAST
) || (fepriv
->state
& FESTATE_RETUNE
)) {
465 fepriv
->delay
= fepriv
->min_delay
;
468 retval
= dvb_frontend_swzigzag_autotune(fe
,
469 fepriv
->check_wrapped
);
473 /* OK, if we've run out of trials at the fast speed.
474 * Drop back to slow for the _next_ attempt */
475 fepriv
->state
= FESTATE_SEARCHING_SLOW
;
476 fepriv
->started_auto_step
= fepriv
->auto_step
;
479 fepriv
->check_wrapped
= 1;
481 /* if we've just retuned, enter the ZIGZAG_FAST state.
482 * This ensures we cannot return from an
483 * FE_SET_FRONTEND ioctl before the first frontend tune
485 if (fepriv
->state
& FESTATE_RETUNE
) {
486 fepriv
->state
= FESTATE_TUNING_FAST
;
491 if (fepriv
->state
& FESTATE_SEARCHING_SLOW
) {
492 dvb_frontend_swzigzag_update_delay(fepriv
, s
& FE_HAS_LOCK
);
494 /* Note: don't bother checking for wrapping; we stay in this
495 * state until we get a lock */
496 dvb_frontend_swzigzag_autotune(fe
, 0);
500 static int dvb_frontend_is_exiting(struct dvb_frontend
*fe
)
502 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
504 if (fepriv
->exit
!= DVB_FE_NO_EXIT
)
507 if (fepriv
->dvbdev
->writers
== 1)
508 if (time_after(jiffies
, fepriv
->release_jiffies
+
509 dvb_shutdown_timeout
* HZ
))
515 static int dvb_frontend_should_wakeup(struct dvb_frontend
*fe
)
517 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
519 if (fepriv
->wakeup
) {
523 return dvb_frontend_is_exiting(fe
);
526 static void dvb_frontend_wakeup(struct dvb_frontend
*fe
)
528 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
531 wake_up_interruptible(&fepriv
->wait_queue
);
534 static int dvb_frontend_thread(void *data
)
536 struct dvb_frontend
*fe
= data
;
537 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
538 unsigned long timeout
;
540 enum dvbfe_algo algo
;
542 struct dvb_frontend_parameters
*params
;
544 dprintk("%s\n", __func__
);
546 fepriv
->check_wrapped
= 0;
548 fepriv
->delay
= 3*HZ
;
551 fepriv
->reinitialise
= 0;
553 dvb_frontend_init(fe
);
557 up(&fepriv
->sem
); /* is locked when we enter the thread... */
559 timeout
= wait_event_interruptible_timeout(fepriv
->wait_queue
,
560 dvb_frontend_should_wakeup(fe
) || kthread_should_stop()
561 || freezing(current
),
564 if (kthread_should_stop() || dvb_frontend_is_exiting(fe
)) {
565 /* got signal or quitting */
566 fepriv
->exit
= DVB_FE_NORMAL_EXIT
;
573 if (down_interruptible(&fepriv
->sem
))
576 if (fepriv
->reinitialise
) {
577 dvb_frontend_init(fe
);
578 if (fepriv
->tone
!= -1) {
579 fe
->ops
.set_tone(fe
, fepriv
->tone
);
581 if (fepriv
->voltage
!= -1) {
582 fe
->ops
.set_voltage(fe
, fepriv
->voltage
);
584 fepriv
->reinitialise
= 0;
587 /* do an iteration of the tuning loop */
588 if (fe
->ops
.get_frontend_algo
) {
589 algo
= fe
->ops
.get_frontend_algo(fe
);
592 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__
);
593 params
= NULL
; /* have we been asked to RETUNE ? */
595 if (fepriv
->state
& FESTATE_RETUNE
) {
596 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__
);
597 params
= &fepriv
->parameters
;
598 fepriv
->state
= FESTATE_TUNED
;
602 fe
->ops
.tune(fe
, params
, fepriv
->tune_mode_flags
, &fepriv
->delay
, &s
);
604 if (s
!= fepriv
->status
&& !(fepriv
->tune_mode_flags
& FE_TUNE_MODE_ONESHOT
)) {
605 dprintk("%s: state changed, adding current state\n", __func__
);
606 dvb_frontend_add_event(fe
, s
);
611 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__
);
612 dvb_frontend_swzigzag(fe
);
614 case DVBFE_ALGO_CUSTOM
:
615 params
= NULL
; /* have we been asked to RETUNE ? */
616 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__
, fepriv
->state
);
617 if (fepriv
->state
& FESTATE_RETUNE
) {
618 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__
);
619 params
= &fepriv
->parameters
;
620 fepriv
->state
= FESTATE_TUNED
;
622 /* Case where we are going to search for a carrier
623 * User asked us to retune again for some reason, possibly
624 * requesting a search with a new set of parameters
626 if (fepriv
->algo_status
& DVBFE_ALGO_SEARCH_AGAIN
) {
627 if (fe
->ops
.search
) {
628 fepriv
->algo_status
= fe
->ops
.search(fe
, &fepriv
->parameters
);
629 /* We did do a search as was requested, the flags are
630 * now unset as well and has the flags wrt to search.
633 fepriv
->algo_status
&= ~DVBFE_ALGO_SEARCH_AGAIN
;
636 /* Track the carrier if the search was successful */
637 if (fepriv
->algo_status
== DVBFE_ALGO_SEARCH_SUCCESS
) {
639 fe
->ops
.track(fe
, &fepriv
->parameters
);
641 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
642 fepriv
->delay
= HZ
/ 2;
644 fe
->ops
.read_status(fe
, &s
);
645 if (s
!= fepriv
->status
) {
646 dvb_frontend_add_event(fe
, s
); /* update event list */
648 if (!(s
& FE_HAS_LOCK
)) {
649 fepriv
->delay
= HZ
/ 10;
650 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
652 fepriv
->delay
= 60 * HZ
;
657 dprintk("%s: UNDEFINED ALGO !\n", __func__
);
661 dvb_frontend_swzigzag(fe
);
665 if (dvb_powerdown_on_sleep
) {
666 if (fe
->ops
.set_voltage
)
667 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_OFF
);
668 if (fe
->ops
.tuner_ops
.sleep
) {
669 if (fe
->ops
.i2c_gate_ctrl
)
670 fe
->ops
.i2c_gate_ctrl(fe
, 1);
671 fe
->ops
.tuner_ops
.sleep(fe
);
672 if (fe
->ops
.i2c_gate_ctrl
)
673 fe
->ops
.i2c_gate_ctrl(fe
, 0);
679 fepriv
->thread
= NULL
;
680 if (kthread_should_stop())
681 fepriv
->exit
= DVB_FE_DEVICE_REMOVED
;
683 fepriv
->exit
= DVB_FE_NO_EXIT
;
686 dvb_frontend_wakeup(fe
);
690 static void dvb_frontend_stop(struct dvb_frontend
*fe
)
692 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
694 dprintk ("%s\n", __func__
);
696 fepriv
->exit
= DVB_FE_NORMAL_EXIT
;
702 kthread_stop(fepriv
->thread
);
704 sema_init(&fepriv
->sem
, 1);
705 fepriv
->state
= FESTATE_IDLE
;
707 /* paranoia check in case a signal arrived */
709 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
713 s32
timeval_usec_diff(struct timeval lasttime
, struct timeval curtime
)
715 return ((curtime
.tv_usec
< lasttime
.tv_usec
) ?
716 1000000 - lasttime
.tv_usec
+ curtime
.tv_usec
:
717 curtime
.tv_usec
- lasttime
.tv_usec
);
719 EXPORT_SYMBOL(timeval_usec_diff
);
721 static inline void timeval_usec_add(struct timeval
*curtime
, u32 add_usec
)
723 curtime
->tv_usec
+= add_usec
;
724 if (curtime
->tv_usec
>= 1000000) {
725 curtime
->tv_usec
-= 1000000;
731 * Sleep until gettimeofday() > waketime + add_usec
732 * This needs to be as precise as possible, but as the delay is
733 * usually between 2ms and 32ms, it is done using a scheduled msleep
734 * followed by usleep (normally a busy-wait loop) for the remainder
736 void dvb_frontend_sleep_until(struct timeval
*waketime
, u32 add_usec
)
738 struct timeval lasttime
;
741 timeval_usec_add(waketime
, add_usec
);
743 do_gettimeofday(&lasttime
);
744 delta
= timeval_usec_diff(lasttime
, *waketime
);
746 msleep((delta
- 1500) / 1000);
747 do_gettimeofday(&lasttime
);
748 newdelta
= timeval_usec_diff(lasttime
, *waketime
);
749 delta
= (newdelta
> delta
) ? 0 : newdelta
;
754 EXPORT_SYMBOL(dvb_frontend_sleep_until
);
756 static int dvb_frontend_start(struct dvb_frontend
*fe
)
759 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
760 struct task_struct
*fe_thread
;
762 dprintk ("%s\n", __func__
);
764 if (fepriv
->thread
) {
765 if (fepriv
->exit
== DVB_FE_NO_EXIT
)
768 dvb_frontend_stop (fe
);
771 if (signal_pending(current
))
773 if (down_interruptible (&fepriv
->sem
))
776 fepriv
->state
= FESTATE_IDLE
;
777 fepriv
->exit
= DVB_FE_NO_EXIT
;
778 fepriv
->thread
= NULL
;
781 fe_thread
= kthread_run(dvb_frontend_thread
, fe
,
782 "kdvb-ad-%i-fe-%i", fe
->dvb
->num
,fe
->id
);
783 if (IS_ERR(fe_thread
)) {
784 ret
= PTR_ERR(fe_thread
);
785 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret
);
789 fepriv
->thread
= fe_thread
;
793 static void dvb_frontend_get_frequency_limits(struct dvb_frontend
*fe
,
794 u32
*freq_min
, u32
*freq_max
)
796 *freq_min
= max(fe
->ops
.info
.frequency_min
, fe
->ops
.tuner_ops
.info
.frequency_min
);
798 if (fe
->ops
.info
.frequency_max
== 0)
799 *freq_max
= fe
->ops
.tuner_ops
.info
.frequency_max
;
800 else if (fe
->ops
.tuner_ops
.info
.frequency_max
== 0)
801 *freq_max
= fe
->ops
.info
.frequency_max
;
803 *freq_max
= min(fe
->ops
.info
.frequency_max
, fe
->ops
.tuner_ops
.info
.frequency_max
);
805 if (*freq_min
== 0 || *freq_max
== 0)
806 printk(KERN_WARNING
"DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
807 fe
->dvb
->num
,fe
->id
);
810 static int dvb_frontend_check_parameters(struct dvb_frontend
*fe
,
811 struct dvb_frontend_parameters
*parms
)
816 /* range check: frequency */
817 dvb_frontend_get_frequency_limits(fe
, &freq_min
, &freq_max
);
818 if ((freq_min
&& parms
->frequency
< freq_min
) ||
819 (freq_max
&& parms
->frequency
> freq_max
)) {
820 printk(KERN_WARNING
"DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
821 fe
->dvb
->num
, fe
->id
, parms
->frequency
, freq_min
, freq_max
);
825 /* range check: symbol rate */
826 if (fe
->ops
.info
.type
== FE_QPSK
) {
827 if ((fe
->ops
.info
.symbol_rate_min
&&
828 parms
->u
.qpsk
.symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
829 (fe
->ops
.info
.symbol_rate_max
&&
830 parms
->u
.qpsk
.symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
831 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
832 fe
->dvb
->num
, fe
->id
, parms
->u
.qpsk
.symbol_rate
,
833 fe
->ops
.info
.symbol_rate_min
, fe
->ops
.info
.symbol_rate_max
);
837 } else if (fe
->ops
.info
.type
== FE_QAM
) {
838 if ((fe
->ops
.info
.symbol_rate_min
&&
839 parms
->u
.qam
.symbol_rate
< fe
->ops
.info
.symbol_rate_min
) ||
840 (fe
->ops
.info
.symbol_rate_max
&&
841 parms
->u
.qam
.symbol_rate
> fe
->ops
.info
.symbol_rate_max
)) {
842 printk(KERN_WARNING
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
843 fe
->dvb
->num
, fe
->id
, parms
->u
.qam
.symbol_rate
,
844 fe
->ops
.info
.symbol_rate_min
, fe
->ops
.info
.symbol_rate_max
);
849 /* check for supported modulation */
850 if (fe
->ops
.info
.type
== FE_QAM
&&
851 (parms
->u
.qam
.modulation
> QAM_AUTO
||
852 !((1 << (parms
->u
.qam
.modulation
+ 10)) & fe
->ops
.info
.caps
))) {
853 printk(KERN_WARNING
"DVB: adapter %i frontend %i modulation %u not supported\n",
854 fe
->dvb
->num
, fe
->id
, parms
->u
.qam
.modulation
);
861 static int dvb_frontend_clear_cache(struct dvb_frontend
*fe
)
865 memset(&(fe
->dtv_property_cache
), 0,
866 sizeof(struct dtv_frontend_properties
));
868 fe
->dtv_property_cache
.state
= DTV_CLEAR
;
869 fe
->dtv_property_cache
.delivery_system
= SYS_UNDEFINED
;
870 fe
->dtv_property_cache
.inversion
= INVERSION_AUTO
;
871 fe
->dtv_property_cache
.fec_inner
= FEC_AUTO
;
872 fe
->dtv_property_cache
.transmission_mode
= TRANSMISSION_MODE_AUTO
;
873 fe
->dtv_property_cache
.bandwidth_hz
= BANDWIDTH_AUTO
;
874 fe
->dtv_property_cache
.guard_interval
= GUARD_INTERVAL_AUTO
;
875 fe
->dtv_property_cache
.hierarchy
= HIERARCHY_AUTO
;
876 fe
->dtv_property_cache
.symbol_rate
= QAM_AUTO
;
877 fe
->dtv_property_cache
.code_rate_HP
= FEC_AUTO
;
878 fe
->dtv_property_cache
.code_rate_LP
= FEC_AUTO
;
880 fe
->dtv_property_cache
.isdbt_partial_reception
= -1;
881 fe
->dtv_property_cache
.isdbt_sb_mode
= -1;
882 fe
->dtv_property_cache
.isdbt_sb_subchannel
= -1;
883 fe
->dtv_property_cache
.isdbt_sb_segment_idx
= -1;
884 fe
->dtv_property_cache
.isdbt_sb_segment_count
= -1;
885 fe
->dtv_property_cache
.isdbt_layer_enabled
= 0x7;
886 for (i
= 0; i
< 3; i
++) {
887 fe
->dtv_property_cache
.layer
[i
].fec
= FEC_AUTO
;
888 fe
->dtv_property_cache
.layer
[i
].modulation
= QAM_AUTO
;
889 fe
->dtv_property_cache
.layer
[i
].interleaving
= -1;
890 fe
->dtv_property_cache
.layer
[i
].segment_count
= -1;
896 #define _DTV_CMD(n, s, b) \
904 static struct dtv_cmds_h dtv_cmds
[] = {
905 _DTV_CMD(DTV_TUNE
, 1, 0),
906 _DTV_CMD(DTV_CLEAR
, 1, 0),
909 _DTV_CMD(DTV_FREQUENCY
, 1, 0),
910 _DTV_CMD(DTV_BANDWIDTH_HZ
, 1, 0),
911 _DTV_CMD(DTV_MODULATION
, 1, 0),
912 _DTV_CMD(DTV_INVERSION
, 1, 0),
913 _DTV_CMD(DTV_DISEQC_MASTER
, 1, 1),
914 _DTV_CMD(DTV_SYMBOL_RATE
, 1, 0),
915 _DTV_CMD(DTV_INNER_FEC
, 1, 0),
916 _DTV_CMD(DTV_VOLTAGE
, 1, 0),
917 _DTV_CMD(DTV_TONE
, 1, 0),
918 _DTV_CMD(DTV_PILOT
, 1, 0),
919 _DTV_CMD(DTV_ROLLOFF
, 1, 0),
920 _DTV_CMD(DTV_DELIVERY_SYSTEM
, 1, 0),
921 _DTV_CMD(DTV_HIERARCHY
, 1, 0),
922 _DTV_CMD(DTV_CODE_RATE_HP
, 1, 0),
923 _DTV_CMD(DTV_CODE_RATE_LP
, 1, 0),
924 _DTV_CMD(DTV_GUARD_INTERVAL
, 1, 0),
925 _DTV_CMD(DTV_TRANSMISSION_MODE
, 1, 0),
927 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION
, 1, 0),
928 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING
, 1, 0),
929 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID
, 1, 0),
930 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX
, 1, 0),
931 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT
, 1, 0),
932 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED
, 1, 0),
933 _DTV_CMD(DTV_ISDBT_LAYERA_FEC
, 1, 0),
934 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION
, 1, 0),
935 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT
, 1, 0),
936 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING
, 1, 0),
937 _DTV_CMD(DTV_ISDBT_LAYERB_FEC
, 1, 0),
938 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION
, 1, 0),
939 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT
, 1, 0),
940 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING
, 1, 0),
941 _DTV_CMD(DTV_ISDBT_LAYERC_FEC
, 1, 0),
942 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION
, 1, 0),
943 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT
, 1, 0),
944 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING
, 1, 0),
946 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION
, 0, 0),
947 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING
, 0, 0),
948 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID
, 0, 0),
949 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX
, 0, 0),
950 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT
, 0, 0),
951 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED
, 0, 0),
952 _DTV_CMD(DTV_ISDBT_LAYERA_FEC
, 0, 0),
953 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION
, 0, 0),
954 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT
, 0, 0),
955 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING
, 0, 0),
956 _DTV_CMD(DTV_ISDBT_LAYERB_FEC
, 0, 0),
957 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION
, 0, 0),
958 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT
, 0, 0),
959 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING
, 0, 0),
960 _DTV_CMD(DTV_ISDBT_LAYERC_FEC
, 0, 0),
961 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION
, 0, 0),
962 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT
, 0, 0),
963 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING
, 0, 0),
965 _DTV_CMD(DTV_ISDBS_TS_ID
, 1, 0),
968 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY
, 0, 1),
969 _DTV_CMD(DTV_API_VERSION
, 0, 0),
970 _DTV_CMD(DTV_CODE_RATE_HP
, 0, 0),
971 _DTV_CMD(DTV_CODE_RATE_LP
, 0, 0),
972 _DTV_CMD(DTV_GUARD_INTERVAL
, 0, 0),
973 _DTV_CMD(DTV_TRANSMISSION_MODE
, 0, 0),
974 _DTV_CMD(DTV_HIERARCHY
, 0, 0),
977 static void dtv_property_dump(struct dtv_property
*tvp
)
981 if (tvp
->cmd
<= 0 || tvp
->cmd
> DTV_MAX_COMMAND
) {
982 printk(KERN_WARNING
"%s: tvp.cmd = 0x%08x undefined\n",
987 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
990 ,dtv_cmds
[ tvp
->cmd
].name
);
992 if(dtv_cmds
[ tvp
->cmd
].buffer
) {
994 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
998 for(i
= 0; i
< tvp
->u
.buffer
.len
; i
++)
999 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1002 ,tvp
->u
.buffer
.data
[i
]);
1005 dprintk("%s() tvp.u.data = 0x%08x\n", __func__
, tvp
->u
.data
);
1008 static int is_legacy_delivery_system(fe_delivery_system_t s
)
1010 if((s
== SYS_UNDEFINED
) || (s
== SYS_DVBC_ANNEX_AC
) ||
1011 (s
== SYS_DVBC_ANNEX_B
) || (s
== SYS_DVBT
) || (s
== SYS_DVBS
) ||
1018 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1019 * drivers can use a single set_frontend tuning function, regardless of whether
1020 * it's being used for the legacy or new API, reducing code and complexity.
1022 static void dtv_property_cache_sync(struct dvb_frontend
*fe
,
1023 struct dvb_frontend_parameters
*p
)
1025 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1027 c
->frequency
= p
->frequency
;
1028 c
->inversion
= p
->inversion
;
1030 switch (fe
->ops
.info
.type
) {
1032 c
->modulation
= QPSK
; /* implied for DVB-S in legacy API */
1033 c
->rolloff
= ROLLOFF_35
;/* implied for DVB-S */
1034 c
->symbol_rate
= p
->u
.qpsk
.symbol_rate
;
1035 c
->fec_inner
= p
->u
.qpsk
.fec_inner
;
1036 c
->delivery_system
= SYS_DVBS
;
1039 c
->symbol_rate
= p
->u
.qam
.symbol_rate
;
1040 c
->fec_inner
= p
->u
.qam
.fec_inner
;
1041 c
->modulation
= p
->u
.qam
.modulation
;
1042 c
->delivery_system
= SYS_DVBC_ANNEX_AC
;
1045 if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_6_MHZ
)
1046 c
->bandwidth_hz
= 6000000;
1047 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_7_MHZ
)
1048 c
->bandwidth_hz
= 7000000;
1049 else if (p
->u
.ofdm
.bandwidth
== BANDWIDTH_8_MHZ
)
1050 c
->bandwidth_hz
= 8000000;
1052 /* Including BANDWIDTH_AUTO */
1053 c
->bandwidth_hz
= 0;
1054 c
->code_rate_HP
= p
->u
.ofdm
.code_rate_HP
;
1055 c
->code_rate_LP
= p
->u
.ofdm
.code_rate_LP
;
1056 c
->modulation
= p
->u
.ofdm
.constellation
;
1057 c
->transmission_mode
= p
->u
.ofdm
.transmission_mode
;
1058 c
->guard_interval
= p
->u
.ofdm
.guard_interval
;
1059 c
->hierarchy
= p
->u
.ofdm
.hierarchy_information
;
1060 c
->delivery_system
= SYS_DVBT
;
1063 c
->modulation
= p
->u
.vsb
.modulation
;
1064 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1065 c
->delivery_system
= SYS_ATSC
;
1067 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1072 /* Ensure the cached values are set correctly in the frontend
1073 * legacy tuning structures, for the advanced tuning API.
1075 static void dtv_property_legacy_params_sync(struct dvb_frontend
*fe
)
1077 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1078 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1079 struct dvb_frontend_parameters
*p
= &fepriv
->parameters
;
1081 p
->frequency
= c
->frequency
;
1082 p
->inversion
= c
->inversion
;
1084 switch (fe
->ops
.info
.type
) {
1086 dprintk("%s() Preparing QPSK req\n", __func__
);
1087 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1088 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1089 c
->delivery_system
= SYS_DVBS
;
1092 dprintk("%s() Preparing QAM req\n", __func__
);
1093 p
->u
.qam
.symbol_rate
= c
->symbol_rate
;
1094 p
->u
.qam
.fec_inner
= c
->fec_inner
;
1095 p
->u
.qam
.modulation
= c
->modulation
;
1096 c
->delivery_system
= SYS_DVBC_ANNEX_AC
;
1099 dprintk("%s() Preparing OFDM req\n", __func__
);
1100 if (c
->bandwidth_hz
== 6000000)
1101 p
->u
.ofdm
.bandwidth
= BANDWIDTH_6_MHZ
;
1102 else if (c
->bandwidth_hz
== 7000000)
1103 p
->u
.ofdm
.bandwidth
= BANDWIDTH_7_MHZ
;
1104 else if (c
->bandwidth_hz
== 8000000)
1105 p
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
1107 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1108 p
->u
.ofdm
.code_rate_HP
= c
->code_rate_HP
;
1109 p
->u
.ofdm
.code_rate_LP
= c
->code_rate_LP
;
1110 p
->u
.ofdm
.constellation
= c
->modulation
;
1111 p
->u
.ofdm
.transmission_mode
= c
->transmission_mode
;
1112 p
->u
.ofdm
.guard_interval
= c
->guard_interval
;
1113 p
->u
.ofdm
.hierarchy_information
= c
->hierarchy
;
1114 c
->delivery_system
= SYS_DVBT
;
1117 dprintk("%s() Preparing VSB req\n", __func__
);
1118 p
->u
.vsb
.modulation
= c
->modulation
;
1119 if ((c
->modulation
== VSB_8
) || (c
->modulation
== VSB_16
))
1120 c
->delivery_system
= SYS_ATSC
;
1122 c
->delivery_system
= SYS_DVBC_ANNEX_B
;
1127 /* Ensure the cached values are set correctly in the frontend
1128 * legacy tuning structures, for the legacy tuning API.
1130 static void dtv_property_adv_params_sync(struct dvb_frontend
*fe
)
1132 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1133 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1134 struct dvb_frontend_parameters
*p
= &fepriv
->parameters
;
1136 p
->frequency
= c
->frequency
;
1137 p
->inversion
= c
->inversion
;
1139 switch(c
->modulation
) {
1144 p
->u
.qpsk
.symbol_rate
= c
->symbol_rate
;
1145 p
->u
.qpsk
.fec_inner
= c
->fec_inner
;
1151 if(c
->delivery_system
== SYS_ISDBT
) {
1152 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1153 p
->frequency
= c
->frequency
;
1154 p
->inversion
= c
->inversion
;
1155 p
->u
.ofdm
.constellation
= QAM_AUTO
;
1156 p
->u
.ofdm
.code_rate_HP
= FEC_AUTO
;
1157 p
->u
.ofdm
.code_rate_LP
= FEC_AUTO
;
1158 p
->u
.ofdm
.transmission_mode
= TRANSMISSION_MODE_AUTO
;
1159 p
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_AUTO
;
1160 p
->u
.ofdm
.hierarchy_information
= HIERARCHY_AUTO
;
1161 if (c
->bandwidth_hz
== 8000000)
1162 p
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
1163 else if (c
->bandwidth_hz
== 7000000)
1164 p
->u
.ofdm
.bandwidth
= BANDWIDTH_7_MHZ
;
1165 else if (c
->bandwidth_hz
== 6000000)
1166 p
->u
.ofdm
.bandwidth
= BANDWIDTH_6_MHZ
;
1168 p
->u
.ofdm
.bandwidth
= BANDWIDTH_AUTO
;
1172 static void dtv_property_cache_submit(struct dvb_frontend
*fe
)
1174 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1176 /* For legacy delivery systems we don't need the delivery_system to
1177 * be specified, but we populate the older structures from the cache
1178 * so we can call set_frontend on older drivers.
1180 if(is_legacy_delivery_system(c
->delivery_system
)) {
1182 dprintk("%s() legacy, modulation = %d\n", __func__
, c
->modulation
);
1183 dtv_property_legacy_params_sync(fe
);
1186 dprintk("%s() adv, modulation = %d\n", __func__
, c
->modulation
);
1188 /* For advanced delivery systems / modulation types ...
1189 * we seed the lecacy dvb_frontend_parameters structure
1190 * so that the sanity checking code later in the IOCTL processing
1191 * can validate our basic frequency ranges, symbolrates, modulation
1194 dtv_property_adv_params_sync(fe
);
1198 static int dvb_frontend_ioctl_legacy(struct file
*file
,
1199 unsigned int cmd
, void *parg
);
1200 static int dvb_frontend_ioctl_properties(struct file
*file
,
1201 unsigned int cmd
, void *parg
);
1203 static int dtv_property_process_get(struct dvb_frontend
*fe
,
1204 struct dtv_property
*tvp
,
1209 /* Allow the frontend to validate incoming properties */
1210 if (fe
->ops
.get_property
)
1211 r
= fe
->ops
.get_property(fe
, tvp
);
1218 tvp
->u
.data
= fe
->dtv_property_cache
.frequency
;
1220 case DTV_MODULATION
:
1221 tvp
->u
.data
= fe
->dtv_property_cache
.modulation
;
1223 case DTV_BANDWIDTH_HZ
:
1224 tvp
->u
.data
= fe
->dtv_property_cache
.bandwidth_hz
;
1227 tvp
->u
.data
= fe
->dtv_property_cache
.inversion
;
1229 case DTV_SYMBOL_RATE
:
1230 tvp
->u
.data
= fe
->dtv_property_cache
.symbol_rate
;
1233 tvp
->u
.data
= fe
->dtv_property_cache
.fec_inner
;
1236 tvp
->u
.data
= fe
->dtv_property_cache
.pilot
;
1239 tvp
->u
.data
= fe
->dtv_property_cache
.rolloff
;
1241 case DTV_DELIVERY_SYSTEM
:
1242 tvp
->u
.data
= fe
->dtv_property_cache
.delivery_system
;
1245 tvp
->u
.data
= fe
->dtv_property_cache
.voltage
;
1248 tvp
->u
.data
= fe
->dtv_property_cache
.sectone
;
1250 case DTV_API_VERSION
:
1251 tvp
->u
.data
= (DVB_API_VERSION
<< 8) | DVB_API_VERSION_MINOR
;
1253 case DTV_CODE_RATE_HP
:
1254 tvp
->u
.data
= fe
->dtv_property_cache
.code_rate_HP
;
1256 case DTV_CODE_RATE_LP
:
1257 tvp
->u
.data
= fe
->dtv_property_cache
.code_rate_LP
;
1259 case DTV_GUARD_INTERVAL
:
1260 tvp
->u
.data
= fe
->dtv_property_cache
.guard_interval
;
1262 case DTV_TRANSMISSION_MODE
:
1263 tvp
->u
.data
= fe
->dtv_property_cache
.transmission_mode
;
1266 tvp
->u
.data
= fe
->dtv_property_cache
.hierarchy
;
1269 /* ISDB-T Support here */
1270 case DTV_ISDBT_PARTIAL_RECEPTION
:
1271 tvp
->u
.data
= fe
->dtv_property_cache
.isdbt_partial_reception
;
1273 case DTV_ISDBT_SOUND_BROADCASTING
:
1274 tvp
->u
.data
= fe
->dtv_property_cache
.isdbt_sb_mode
;
1276 case DTV_ISDBT_SB_SUBCHANNEL_ID
:
1277 tvp
->u
.data
= fe
->dtv_property_cache
.isdbt_sb_subchannel
;
1279 case DTV_ISDBT_SB_SEGMENT_IDX
:
1280 tvp
->u
.data
= fe
->dtv_property_cache
.isdbt_sb_segment_idx
;
1282 case DTV_ISDBT_SB_SEGMENT_COUNT
:
1283 tvp
->u
.data
= fe
->dtv_property_cache
.isdbt_sb_segment_count
;
1285 case DTV_ISDBT_LAYER_ENABLED
:
1286 tvp
->u
.data
= fe
->dtv_property_cache
.isdbt_layer_enabled
;
1288 case DTV_ISDBT_LAYERA_FEC
:
1289 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[0].fec
;
1291 case DTV_ISDBT_LAYERA_MODULATION
:
1292 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[0].modulation
;
1294 case DTV_ISDBT_LAYERA_SEGMENT_COUNT
:
1295 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[0].segment_count
;
1297 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING
:
1298 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[0].interleaving
;
1300 case DTV_ISDBT_LAYERB_FEC
:
1301 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[1].fec
;
1303 case DTV_ISDBT_LAYERB_MODULATION
:
1304 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[1].modulation
;
1306 case DTV_ISDBT_LAYERB_SEGMENT_COUNT
:
1307 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[1].segment_count
;
1309 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING
:
1310 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[1].interleaving
;
1312 case DTV_ISDBT_LAYERC_FEC
:
1313 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[2].fec
;
1315 case DTV_ISDBT_LAYERC_MODULATION
:
1316 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[2].modulation
;
1318 case DTV_ISDBT_LAYERC_SEGMENT_COUNT
:
1319 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[2].segment_count
;
1321 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING
:
1322 tvp
->u
.data
= fe
->dtv_property_cache
.layer
[2].interleaving
;
1324 case DTV_ISDBS_TS_ID
:
1325 tvp
->u
.data
= fe
->dtv_property_cache
.isdbs_ts_id
;
1331 dtv_property_dump(tvp
);
1336 static int dtv_property_process_set(struct dvb_frontend
*fe
,
1337 struct dtv_property
*tvp
,
1341 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1342 dtv_property_dump(tvp
);
1344 /* Allow the frontend to validate incoming properties */
1345 if (fe
->ops
.set_property
)
1346 r
= fe
->ops
.set_property(fe
, tvp
);
1353 /* Reset a cache of data specific to the frontend here. This does
1354 * not effect hardware.
1356 dvb_frontend_clear_cache(fe
);
1357 dprintk("%s() Flushing property cache\n", __func__
);
1360 /* interpret the cache of data, build either a traditional frontend
1361 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1364 fe
->dtv_property_cache
.state
= tvp
->cmd
;
1365 dprintk("%s() Finalised property cache\n", __func__
);
1366 dtv_property_cache_submit(fe
);
1368 r
|= dvb_frontend_ioctl_legacy(file
, FE_SET_FRONTEND
,
1369 &fepriv
->parameters
);
1372 fe
->dtv_property_cache
.frequency
= tvp
->u
.data
;
1374 case DTV_MODULATION
:
1375 fe
->dtv_property_cache
.modulation
= tvp
->u
.data
;
1377 case DTV_BANDWIDTH_HZ
:
1378 fe
->dtv_property_cache
.bandwidth_hz
= tvp
->u
.data
;
1381 fe
->dtv_property_cache
.inversion
= tvp
->u
.data
;
1383 case DTV_SYMBOL_RATE
:
1384 fe
->dtv_property_cache
.symbol_rate
= tvp
->u
.data
;
1387 fe
->dtv_property_cache
.fec_inner
= tvp
->u
.data
;
1390 fe
->dtv_property_cache
.pilot
= tvp
->u
.data
;
1393 fe
->dtv_property_cache
.rolloff
= tvp
->u
.data
;
1395 case DTV_DELIVERY_SYSTEM
:
1396 fe
->dtv_property_cache
.delivery_system
= tvp
->u
.data
;
1399 fe
->dtv_property_cache
.voltage
= tvp
->u
.data
;
1400 r
= dvb_frontend_ioctl_legacy(file
, FE_SET_VOLTAGE
,
1401 (void *)fe
->dtv_property_cache
.voltage
);
1404 fe
->dtv_property_cache
.sectone
= tvp
->u
.data
;
1405 r
= dvb_frontend_ioctl_legacy(file
, FE_SET_TONE
,
1406 (void *)fe
->dtv_property_cache
.sectone
);
1408 case DTV_CODE_RATE_HP
:
1409 fe
->dtv_property_cache
.code_rate_HP
= tvp
->u
.data
;
1411 case DTV_CODE_RATE_LP
:
1412 fe
->dtv_property_cache
.code_rate_LP
= tvp
->u
.data
;
1414 case DTV_GUARD_INTERVAL
:
1415 fe
->dtv_property_cache
.guard_interval
= tvp
->u
.data
;
1417 case DTV_TRANSMISSION_MODE
:
1418 fe
->dtv_property_cache
.transmission_mode
= tvp
->u
.data
;
1421 fe
->dtv_property_cache
.hierarchy
= tvp
->u
.data
;
1424 /* ISDB-T Support here */
1425 case DTV_ISDBT_PARTIAL_RECEPTION
:
1426 fe
->dtv_property_cache
.isdbt_partial_reception
= tvp
->u
.data
;
1428 case DTV_ISDBT_SOUND_BROADCASTING
:
1429 fe
->dtv_property_cache
.isdbt_sb_mode
= tvp
->u
.data
;
1431 case DTV_ISDBT_SB_SUBCHANNEL_ID
:
1432 fe
->dtv_property_cache
.isdbt_sb_subchannel
= tvp
->u
.data
;
1434 case DTV_ISDBT_SB_SEGMENT_IDX
:
1435 fe
->dtv_property_cache
.isdbt_sb_segment_idx
= tvp
->u
.data
;
1437 case DTV_ISDBT_SB_SEGMENT_COUNT
:
1438 fe
->dtv_property_cache
.isdbt_sb_segment_count
= tvp
->u
.data
;
1440 case DTV_ISDBT_LAYER_ENABLED
:
1441 fe
->dtv_property_cache
.isdbt_layer_enabled
= tvp
->u
.data
;
1443 case DTV_ISDBT_LAYERA_FEC
:
1444 fe
->dtv_property_cache
.layer
[0].fec
= tvp
->u
.data
;
1446 case DTV_ISDBT_LAYERA_MODULATION
:
1447 fe
->dtv_property_cache
.layer
[0].modulation
= tvp
->u
.data
;
1449 case DTV_ISDBT_LAYERA_SEGMENT_COUNT
:
1450 fe
->dtv_property_cache
.layer
[0].segment_count
= tvp
->u
.data
;
1452 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING
:
1453 fe
->dtv_property_cache
.layer
[0].interleaving
= tvp
->u
.data
;
1455 case DTV_ISDBT_LAYERB_FEC
:
1456 fe
->dtv_property_cache
.layer
[1].fec
= tvp
->u
.data
;
1458 case DTV_ISDBT_LAYERB_MODULATION
:
1459 fe
->dtv_property_cache
.layer
[1].modulation
= tvp
->u
.data
;
1461 case DTV_ISDBT_LAYERB_SEGMENT_COUNT
:
1462 fe
->dtv_property_cache
.layer
[1].segment_count
= tvp
->u
.data
;
1464 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING
:
1465 fe
->dtv_property_cache
.layer
[1].interleaving
= tvp
->u
.data
;
1467 case DTV_ISDBT_LAYERC_FEC
:
1468 fe
->dtv_property_cache
.layer
[2].fec
= tvp
->u
.data
;
1470 case DTV_ISDBT_LAYERC_MODULATION
:
1471 fe
->dtv_property_cache
.layer
[2].modulation
= tvp
->u
.data
;
1473 case DTV_ISDBT_LAYERC_SEGMENT_COUNT
:
1474 fe
->dtv_property_cache
.layer
[2].segment_count
= tvp
->u
.data
;
1476 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING
:
1477 fe
->dtv_property_cache
.layer
[2].interleaving
= tvp
->u
.data
;
1479 case DTV_ISDBS_TS_ID
:
1480 fe
->dtv_property_cache
.isdbs_ts_id
= tvp
->u
.data
;
1489 static int dvb_frontend_ioctl(struct file
*file
,
1490 unsigned int cmd
, void *parg
)
1492 struct dvb_device
*dvbdev
= file
->private_data
;
1493 struct dvb_frontend
*fe
= dvbdev
->priv
;
1494 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1495 int err
= -EOPNOTSUPP
;
1497 dprintk("%s (%d)\n", __func__
, _IOC_NR(cmd
));
1499 if (fepriv
->exit
!= DVB_FE_NO_EXIT
)
1502 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
&&
1503 (_IOC_DIR(cmd
) != _IOC_READ
|| cmd
== FE_GET_EVENT
||
1504 cmd
== FE_DISEQC_RECV_SLAVE_REPLY
))
1507 if (down_interruptible (&fepriv
->sem
))
1508 return -ERESTARTSYS
;
1510 if ((cmd
== FE_SET_PROPERTY
) || (cmd
== FE_GET_PROPERTY
))
1511 err
= dvb_frontend_ioctl_properties(file
, cmd
, parg
);
1513 fe
->dtv_property_cache
.state
= DTV_UNDEFINED
;
1514 err
= dvb_frontend_ioctl_legacy(file
, cmd
, parg
);
1521 static int dvb_frontend_ioctl_properties(struct file
*file
,
1522 unsigned int cmd
, void *parg
)
1524 struct dvb_device
*dvbdev
= file
->private_data
;
1525 struct dvb_frontend
*fe
= dvbdev
->priv
;
1528 struct dtv_properties
*tvps
= NULL
;
1529 struct dtv_property
*tvp
= NULL
;
1532 dprintk("%s\n", __func__
);
1534 if(cmd
== FE_SET_PROPERTY
) {
1535 tvps
= (struct dtv_properties __user
*)parg
;
1537 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1538 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1540 /* Put an arbitrary limit on the number of messages that can
1541 * be sent at once */
1542 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1545 tvp
= kmalloc(tvps
->num
* sizeof(struct dtv_property
), GFP_KERNEL
);
1551 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1556 for (i
= 0; i
< tvps
->num
; i
++) {
1557 (tvp
+ i
)->result
= dtv_property_process_set(fe
, tvp
+ i
, file
);
1558 err
|= (tvp
+ i
)->result
;
1561 if(fe
->dtv_property_cache
.state
== DTV_TUNE
)
1562 dprintk("%s() Property cache is full, tuning\n", __func__
);
1565 if(cmd
== FE_GET_PROPERTY
) {
1567 tvps
= (struct dtv_properties __user
*)parg
;
1569 dprintk("%s() properties.num = %d\n", __func__
, tvps
->num
);
1570 dprintk("%s() properties.props = %p\n", __func__
, tvps
->props
);
1572 /* Put an arbitrary limit on the number of messages that can
1573 * be sent at once */
1574 if ((tvps
->num
== 0) || (tvps
->num
> DTV_IOCTL_MAX_MSGS
))
1577 tvp
= kmalloc(tvps
->num
* sizeof(struct dtv_property
), GFP_KERNEL
);
1583 if (copy_from_user(tvp
, tvps
->props
, tvps
->num
* sizeof(struct dtv_property
))) {
1588 for (i
= 0; i
< tvps
->num
; i
++) {
1589 (tvp
+ i
)->result
= dtv_property_process_get(fe
, tvp
+ i
, file
);
1590 err
|= (tvp
+ i
)->result
;
1593 if (copy_to_user(tvps
->props
, tvp
, tvps
->num
* sizeof(struct dtv_property
))) {
1606 static int dvb_frontend_ioctl_legacy(struct file
*file
,
1607 unsigned int cmd
, void *parg
)
1609 struct dvb_device
*dvbdev
= file
->private_data
;
1610 struct dvb_frontend
*fe
= dvbdev
->priv
;
1611 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1612 int cb_err
, err
= -EOPNOTSUPP
;
1614 if (fe
->dvb
->fe_ioctl_override
) {
1615 cb_err
= fe
->dvb
->fe_ioctl_override(fe
, cmd
, parg
,
1621 /* fe_ioctl_override returning 0 allows
1622 * dvb-core to continue handling the ioctl */
1627 struct dvb_frontend_info
* info
= parg
;
1628 memcpy(info
, &fe
->ops
.info
, sizeof(struct dvb_frontend_info
));
1629 dvb_frontend_get_frequency_limits(fe
, &info
->frequency_min
, &info
->frequency_max
);
1631 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1632 * do it, it is done for it. */
1633 info
->caps
|= FE_CAN_INVERSION_AUTO
;
1638 case FE_READ_STATUS
: {
1639 fe_status_t
* status
= parg
;
1641 /* if retune was requested but hasn't occured yet, prevent
1642 * that user get signal state from previous tuning */
1643 if (fepriv
->state
== FESTATE_RETUNE
||
1644 fepriv
->state
== FESTATE_ERROR
) {
1650 if (fe
->ops
.read_status
)
1651 err
= fe
->ops
.read_status(fe
, status
);
1655 if (fe
->ops
.read_ber
)
1656 err
= fe
->ops
.read_ber(fe
, (__u32
*) parg
);
1659 case FE_READ_SIGNAL_STRENGTH
:
1660 if (fe
->ops
.read_signal_strength
)
1661 err
= fe
->ops
.read_signal_strength(fe
, (__u16
*) parg
);
1665 if (fe
->ops
.read_snr
)
1666 err
= fe
->ops
.read_snr(fe
, (__u16
*) parg
);
1669 case FE_READ_UNCORRECTED_BLOCKS
:
1670 if (fe
->ops
.read_ucblocks
)
1671 err
= fe
->ops
.read_ucblocks(fe
, (__u32
*) parg
);
1675 case FE_DISEQC_RESET_OVERLOAD
:
1676 if (fe
->ops
.diseqc_reset_overload
) {
1677 err
= fe
->ops
.diseqc_reset_overload(fe
);
1678 fepriv
->state
= FESTATE_DISEQC
;
1683 case FE_DISEQC_SEND_MASTER_CMD
:
1684 if (fe
->ops
.diseqc_send_master_cmd
) {
1685 err
= fe
->ops
.diseqc_send_master_cmd(fe
, (struct dvb_diseqc_master_cmd
*) parg
);
1686 fepriv
->state
= FESTATE_DISEQC
;
1691 case FE_DISEQC_SEND_BURST
:
1692 if (fe
->ops
.diseqc_send_burst
) {
1693 err
= fe
->ops
.diseqc_send_burst(fe
, (fe_sec_mini_cmd_t
) parg
);
1694 fepriv
->state
= FESTATE_DISEQC
;
1700 if (fe
->ops
.set_tone
) {
1701 err
= fe
->ops
.set_tone(fe
, (fe_sec_tone_mode_t
) parg
);
1702 fepriv
->tone
= (fe_sec_tone_mode_t
) parg
;
1703 fepriv
->state
= FESTATE_DISEQC
;
1708 case FE_SET_VOLTAGE
:
1709 if (fe
->ops
.set_voltage
) {
1710 err
= fe
->ops
.set_voltage(fe
, (fe_sec_voltage_t
) parg
);
1711 fepriv
->voltage
= (fe_sec_voltage_t
) parg
;
1712 fepriv
->state
= FESTATE_DISEQC
;
1717 case FE_DISHNETWORK_SEND_LEGACY_CMD
:
1718 if (fe
->ops
.dishnetwork_send_legacy_command
) {
1719 err
= fe
->ops
.dishnetwork_send_legacy_command(fe
, (unsigned long) parg
);
1720 fepriv
->state
= FESTATE_DISEQC
;
1722 } else if (fe
->ops
.set_voltage
) {
1724 * NOTE: This is a fallback condition. Some frontends
1725 * (stv0299 for instance) take longer than 8msec to
1726 * respond to a set_voltage command. Those switches
1727 * need custom routines to switch properly. For all
1728 * other frontends, the following should work ok.
1729 * Dish network legacy switches (as used by Dish500)
1730 * are controlled by sending 9-bit command words
1731 * spaced 8msec apart.
1732 * the actual command word is switch/port dependant
1733 * so it is up to the userspace application to send
1734 * the right command.
1735 * The command must always start with a '0' after
1736 * initialization, so parg is 8 bits and does not
1737 * include the initialization or start bit
1739 unsigned long swcmd
= ((unsigned long) parg
) << 1;
1740 struct timeval nexttime
;
1741 struct timeval tv
[10];
1744 if (dvb_frontend_debug
)
1745 printk("%s switch command: 0x%04lx\n", __func__
, swcmd
);
1746 do_gettimeofday(&nexttime
);
1747 if (dvb_frontend_debug
)
1748 memcpy(&tv
[0], &nexttime
, sizeof(struct timeval
));
1749 /* before sending a command, initialize by sending
1750 * a 32ms 18V to the switch
1752 fe
->ops
.set_voltage(fe
, SEC_VOLTAGE_18
);
1753 dvb_frontend_sleep_until(&nexttime
, 32000);
1755 for (i
= 0; i
< 9; i
++) {
1756 if (dvb_frontend_debug
)
1757 do_gettimeofday(&tv
[i
+ 1]);
1758 if ((swcmd
& 0x01) != last
) {
1759 /* set voltage to (last ? 13V : 18V) */
1760 fe
->ops
.set_voltage(fe
, (last
) ? SEC_VOLTAGE_13
: SEC_VOLTAGE_18
);
1761 last
= (last
) ? 0 : 1;
1765 dvb_frontend_sleep_until(&nexttime
, 8000);
1767 if (dvb_frontend_debug
) {
1768 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1769 __func__
, fe
->dvb
->num
);
1770 for (i
= 1; i
< 10; i
++)
1771 printk("%d: %d\n", i
, timeval_usec_diff(tv
[i
-1] , tv
[i
]));
1774 fepriv
->state
= FESTATE_DISEQC
;
1779 case FE_DISEQC_RECV_SLAVE_REPLY
:
1780 if (fe
->ops
.diseqc_recv_slave_reply
)
1781 err
= fe
->ops
.diseqc_recv_slave_reply(fe
, (struct dvb_diseqc_slave_reply
*) parg
);
1784 case FE_ENABLE_HIGH_LNB_VOLTAGE
:
1785 if (fe
->ops
.enable_high_lnb_voltage
)
1786 err
= fe
->ops
.enable_high_lnb_voltage(fe
, (long) parg
);
1789 case FE_SET_FRONTEND
: {
1790 struct dvb_frontend_tune_settings fetunesettings
;
1792 if(fe
->dtv_property_cache
.state
== DTV_TUNE
) {
1793 if (dvb_frontend_check_parameters(fe
, &fepriv
->parameters
) < 0) {
1798 if (dvb_frontend_check_parameters(fe
, parg
) < 0) {
1803 memcpy (&fepriv
->parameters
, parg
,
1804 sizeof (struct dvb_frontend_parameters
));
1805 dtv_property_cache_sync(fe
, &fepriv
->parameters
);
1808 memset(&fetunesettings
, 0, sizeof(struct dvb_frontend_tune_settings
));
1809 memcpy(&fetunesettings
.parameters
, parg
,
1810 sizeof (struct dvb_frontend_parameters
));
1812 /* force auto frequency inversion if requested */
1813 if (dvb_force_auto_inversion
) {
1814 fepriv
->parameters
.inversion
= INVERSION_AUTO
;
1815 fetunesettings
.parameters
.inversion
= INVERSION_AUTO
;
1817 if (fe
->ops
.info
.type
== FE_OFDM
) {
1818 /* without hierarchical coding code_rate_LP is irrelevant,
1819 * so we tolerate the otherwise invalid FEC_NONE setting */
1820 if (fepriv
->parameters
.u
.ofdm
.hierarchy_information
== HIERARCHY_NONE
&&
1821 fepriv
->parameters
.u
.ofdm
.code_rate_LP
== FEC_NONE
)
1822 fepriv
->parameters
.u
.ofdm
.code_rate_LP
= FEC_AUTO
;
1825 /* get frontend-specific tuning settings */
1826 if (fe
->ops
.get_tune_settings
&& (fe
->ops
.get_tune_settings(fe
, &fetunesettings
) == 0)) {
1827 fepriv
->min_delay
= (fetunesettings
.min_delay_ms
* HZ
) / 1000;
1828 fepriv
->max_drift
= fetunesettings
.max_drift
;
1829 fepriv
->step_size
= fetunesettings
.step_size
;
1831 /* default values */
1832 switch(fe
->ops
.info
.type
) {
1834 fepriv
->min_delay
= HZ
/20;
1835 fepriv
->step_size
= fepriv
->parameters
.u
.qpsk
.symbol_rate
/ 16000;
1836 fepriv
->max_drift
= fepriv
->parameters
.u
.qpsk
.symbol_rate
/ 2000;
1840 fepriv
->min_delay
= HZ
/20;
1841 fepriv
->step_size
= 0; /* no zigzag */
1842 fepriv
->max_drift
= 0;
1846 fepriv
->min_delay
= HZ
/20;
1847 fepriv
->step_size
= fe
->ops
.info
.frequency_stepsize
* 2;
1848 fepriv
->max_drift
= (fe
->ops
.info
.frequency_stepsize
* 2) + 1;
1851 fepriv
->min_delay
= HZ
/20;
1852 fepriv
->step_size
= 0;
1853 fepriv
->max_drift
= 0;
1857 if (dvb_override_tune_delay
> 0)
1858 fepriv
->min_delay
= (dvb_override_tune_delay
* HZ
) / 1000;
1860 fepriv
->state
= FESTATE_RETUNE
;
1862 /* Request the search algorithm to search */
1863 fepriv
->algo_status
|= DVBFE_ALGO_SEARCH_AGAIN
;
1865 dvb_frontend_wakeup(fe
);
1866 dvb_frontend_add_event(fe
, 0);
1873 err
= dvb_frontend_get_event (fe
, parg
, file
->f_flags
);
1876 case FE_GET_FRONTEND
:
1877 if (fe
->ops
.get_frontend
) {
1878 memcpy (parg
, &fepriv
->parameters
, sizeof (struct dvb_frontend_parameters
));
1879 err
= fe
->ops
.get_frontend(fe
, (struct dvb_frontend_parameters
*) parg
);
1883 case FE_SET_FRONTEND_TUNE_MODE
:
1884 fepriv
->tune_mode_flags
= (unsigned long) parg
;
1889 if (fe
->dvb
->fe_ioctl_override
) {
1890 cb_err
= fe
->dvb
->fe_ioctl_override(fe
, cmd
, parg
,
1900 static unsigned int dvb_frontend_poll(struct file
*file
, struct poll_table_struct
*wait
)
1902 struct dvb_device
*dvbdev
= file
->private_data
;
1903 struct dvb_frontend
*fe
= dvbdev
->priv
;
1904 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1906 dprintk ("%s\n", __func__
);
1908 poll_wait (file
, &fepriv
->events
.wait_queue
, wait
);
1910 if (fepriv
->events
.eventw
!= fepriv
->events
.eventr
)
1911 return (POLLIN
| POLLRDNORM
| POLLPRI
);
1916 static int dvb_frontend_open(struct inode
*inode
, struct file
*file
)
1918 struct dvb_device
*dvbdev
= file
->private_data
;
1919 struct dvb_frontend
*fe
= dvbdev
->priv
;
1920 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
1921 struct dvb_adapter
*adapter
= fe
->dvb
;
1924 dprintk ("%s\n", __func__
);
1925 if (fepriv
->exit
== DVB_FE_DEVICE_REMOVED
)
1928 if (adapter
->mfe_shared
) {
1929 mutex_lock (&adapter
->mfe_lock
);
1931 if (adapter
->mfe_dvbdev
== NULL
)
1932 adapter
->mfe_dvbdev
= dvbdev
;
1934 else if (adapter
->mfe_dvbdev
!= dvbdev
) {
1936 *mfedev
= adapter
->mfe_dvbdev
;
1938 *mfe
= mfedev
->priv
;
1939 struct dvb_frontend_private
1940 *mfepriv
= mfe
->frontend_priv
;
1941 int mferetry
= (dvb_mfe_wait_time
<< 1);
1943 mutex_unlock (&adapter
->mfe_lock
);
1944 while (mferetry
-- && (mfedev
->users
!= -1 ||
1945 mfepriv
->thread
!= NULL
)) {
1946 if(msleep_interruptible(500)) {
1947 if(signal_pending(current
))
1952 mutex_lock (&adapter
->mfe_lock
);
1953 if(adapter
->mfe_dvbdev
!= dvbdev
) {
1954 mfedev
= adapter
->mfe_dvbdev
;
1956 mfepriv
= mfe
->frontend_priv
;
1957 if (mfedev
->users
!= -1 ||
1958 mfepriv
->thread
!= NULL
) {
1959 mutex_unlock (&adapter
->mfe_lock
);
1962 adapter
->mfe_dvbdev
= dvbdev
;
1967 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
) {
1968 if ((ret
= fe
->ops
.ts_bus_ctrl(fe
, 1)) < 0)
1972 if ((ret
= dvb_generic_open (inode
, file
)) < 0)
1975 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
1976 /* normal tune mode when opened R/W */
1977 fepriv
->tune_mode_flags
&= ~FE_TUNE_MODE_ONESHOT
;
1979 fepriv
->voltage
= -1;
1981 ret
= dvb_frontend_start (fe
);
1985 /* empty event queue */
1986 fepriv
->events
.eventr
= fepriv
->events
.eventw
= 0;
1989 if (adapter
->mfe_shared
)
1990 mutex_unlock (&adapter
->mfe_lock
);
1994 dvb_generic_release(inode
, file
);
1996 if (dvbdev
->users
== -1 && fe
->ops
.ts_bus_ctrl
)
1997 fe
->ops
.ts_bus_ctrl(fe
, 0);
1999 if (adapter
->mfe_shared
)
2000 mutex_unlock (&adapter
->mfe_lock
);
2004 static int dvb_frontend_release(struct inode
*inode
, struct file
*file
)
2006 struct dvb_device
*dvbdev
= file
->private_data
;
2007 struct dvb_frontend
*fe
= dvbdev
->priv
;
2008 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
2011 dprintk ("%s\n", __func__
);
2013 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
)
2014 fepriv
->release_jiffies
= jiffies
;
2016 ret
= dvb_generic_release (inode
, file
);
2018 if (dvbdev
->users
== -1) {
2019 if (fepriv
->exit
!= DVB_FE_NO_EXIT
) {
2020 fops_put(file
->f_op
);
2022 wake_up(&dvbdev
->wait_queue
);
2024 if (fe
->ops
.ts_bus_ctrl
)
2025 fe
->ops
.ts_bus_ctrl(fe
, 0);
2031 static const struct file_operations dvb_frontend_fops
= {
2032 .owner
= THIS_MODULE
,
2033 .unlocked_ioctl
= dvb_generic_ioctl
,
2034 .poll
= dvb_frontend_poll
,
2035 .open
= dvb_frontend_open
,
2036 .release
= dvb_frontend_release
,
2037 .llseek
= noop_llseek
,
2040 int dvb_register_frontend(struct dvb_adapter
* dvb
,
2041 struct dvb_frontend
* fe
)
2043 struct dvb_frontend_private
*fepriv
;
2044 static const struct dvb_device dvbdev_template
= {
2048 .fops
= &dvb_frontend_fops
,
2049 .kernel_ioctl
= dvb_frontend_ioctl
2052 dprintk ("%s\n", __func__
);
2054 if (mutex_lock_interruptible(&frontend_mutex
))
2055 return -ERESTARTSYS
;
2057 fe
->frontend_priv
= kzalloc(sizeof(struct dvb_frontend_private
), GFP_KERNEL
);
2058 if (fe
->frontend_priv
== NULL
) {
2059 mutex_unlock(&frontend_mutex
);
2062 fepriv
= fe
->frontend_priv
;
2064 sema_init(&fepriv
->sem
, 1);
2065 init_waitqueue_head (&fepriv
->wait_queue
);
2066 init_waitqueue_head (&fepriv
->events
.wait_queue
);
2067 mutex_init(&fepriv
->events
.mtx
);
2069 fepriv
->inversion
= INVERSION_OFF
;
2071 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2076 dvb_register_device (fe
->dvb
, &fepriv
->dvbdev
, &dvbdev_template
,
2077 fe
, DVB_DEVICE_FRONTEND
);
2079 mutex_unlock(&frontend_mutex
);
2082 EXPORT_SYMBOL(dvb_register_frontend
);
2084 int dvb_unregister_frontend(struct dvb_frontend
* fe
)
2086 struct dvb_frontend_private
*fepriv
= fe
->frontend_priv
;
2087 dprintk ("%s\n", __func__
);
2089 mutex_lock(&frontend_mutex
);
2090 dvb_frontend_stop (fe
);
2091 mutex_unlock(&frontend_mutex
);
2093 if (fepriv
->dvbdev
->users
< -1)
2094 wait_event(fepriv
->dvbdev
->wait_queue
,
2095 fepriv
->dvbdev
->users
==-1);
2097 mutex_lock(&frontend_mutex
);
2098 dvb_unregister_device (fepriv
->dvbdev
);
2100 /* fe is invalid now */
2102 mutex_unlock(&frontend_mutex
);
2105 EXPORT_SYMBOL(dvb_unregister_frontend
);
2107 #ifdef CONFIG_MEDIA_ATTACH
2108 void dvb_frontend_detach(struct dvb_frontend
* fe
)
2112 if (fe
->ops
.release_sec
) {
2113 fe
->ops
.release_sec(fe
);
2114 symbol_put_addr(fe
->ops
.release_sec
);
2116 if (fe
->ops
.tuner_ops
.release
) {
2117 fe
->ops
.tuner_ops
.release(fe
);
2118 symbol_put_addr(fe
->ops
.tuner_ops
.release
);
2120 if (fe
->ops
.analog_ops
.release
) {
2121 fe
->ops
.analog_ops
.release(fe
);
2122 symbol_put_addr(fe
->ops
.analog_ops
.release
);
2124 ptr
= (void*)fe
->ops
.release
;
2126 fe
->ops
.release(fe
);
2127 symbol_put_addr(ptr
);
2131 void dvb_frontend_detach(struct dvb_frontend
* fe
)
2133 if (fe
->ops
.release_sec
)
2134 fe
->ops
.release_sec(fe
);
2135 if (fe
->ops
.tuner_ops
.release
)
2136 fe
->ops
.tuner_ops
.release(fe
);
2137 if (fe
->ops
.analog_ops
.release
)
2138 fe
->ops
.analog_ops
.release(fe
);
2139 if (fe
->ops
.release
)
2140 fe
->ops
.release(fe
);
2143 EXPORT_SYMBOL(dvb_frontend_detach
);