Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[linux-btrfs-devel.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
blobefe9c30605e890688b6a7557156436fe0c4611a9
1 /*
2 * dvb_frontend.c: DVB frontend tuning interface/thread
5 * Copyright (C) 1999-2001 Ralph Metzler
6 * Marcus Metzler
7 * Holger Waechtler
8 * for convergence integrated media GmbH
10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/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"
43 #include "dvbdev.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)
82 #define FE_ALGO_HW 1
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_in;
109 struct dvb_frontend_parameters parameters_out;
110 struct dvb_fe_events events;
111 struct semaphore sem;
112 struct list_head list_head;
113 wait_queue_head_t wait_queue;
114 struct task_struct *thread;
115 unsigned long release_jiffies;
116 unsigned int exit;
117 unsigned int wakeup;
118 fe_status_t status;
119 unsigned long tune_mode_flags;
120 unsigned int delay;
121 unsigned int reinitialise;
122 int tone;
123 int voltage;
125 /* swzigzag values */
126 unsigned int state;
127 unsigned int bending;
128 int lnb_drift;
129 unsigned int inversion;
130 unsigned int auto_step;
131 unsigned int auto_sub_step;
132 unsigned int started_auto_step;
133 unsigned int min_delay;
134 unsigned int max_drift;
135 unsigned int step_size;
136 int quality;
137 unsigned int check_wrapped;
138 enum dvbfe_search algo_status;
141 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
143 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
145 struct dvb_frontend_private *fepriv = fe->frontend_priv;
146 struct dvb_fe_events *events = &fepriv->events;
147 struct dvb_frontend_event *e;
148 int wp;
150 dprintk ("%s\n", __func__);
152 if (mutex_lock_interruptible (&events->mtx))
153 return;
155 wp = (events->eventw + 1) % MAX_EVENT;
157 if (wp == events->eventr) {
158 events->overflow = 1;
159 events->eventr = (events->eventr + 1) % MAX_EVENT;
162 e = &events->events[events->eventw];
164 if (status & FE_HAS_LOCK)
165 if (fe->ops.get_frontend)
166 fe->ops.get_frontend(fe, &fepriv->parameters_out);
168 e->parameters = fepriv->parameters_out;
170 events->eventw = wp;
172 mutex_unlock(&events->mtx);
174 e->status = status;
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;
189 return -EOVERFLOW;
192 if (events->eventw == events->eventr) {
193 int ret;
195 if (flags & O_NONBLOCK)
196 return -EWOULDBLOCK;
198 up(&fepriv->sem);
200 ret = wait_event_interruptible (events->wait_queue,
201 events->eventw != events->eventr);
203 if (down_interruptible (&fepriv->sem))
204 return -ERESTARTSYS;
206 if (ret < 0)
207 return ret;
210 if (mutex_lock_interruptible (&events->mtx))
211 return -ERESTARTSYS;
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);
220 return 0;
223 static void dvb_frontend_init(struct dvb_frontend *fe)
225 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
226 fe->dvb->num,
227 fe->id,
228 fe->ops.info.name);
230 if (fe->ops.init)
231 fe->ops.init(fe);
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)
252 int q2;
254 dprintk ("%s\n", __func__);
256 if (locked)
257 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
258 else
259 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
261 q2 = fepriv->quality - 128;
262 q2 *= q2;
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)
276 int autoinversion;
277 int ready = 0;
278 int fe_set_err = 0;
279 struct dvb_frontend_private *fepriv = fe->frontend_priv;
280 int original_inversion = fepriv->parameters_in.inversion;
281 u32 original_frequency = fepriv->parameters_in.frequency;
283 /* are we using autoinversion? */
284 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
285 (fepriv->parameters_in.inversion == INVERSION_AUTO));
287 /* setup parameters correctly */
288 while(!ready) {
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) {
301 case 0:
302 /* try with the current inversion and current drift setting */
303 ready = 1;
304 break;
306 case 1:
307 if (!autoinversion) break;
309 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
310 ready = 1;
311 break;
313 case 2:
314 if (fepriv->lnb_drift == 0) break;
316 fepriv->lnb_drift = -fepriv->lnb_drift;
317 ready = 1;
318 break;
320 case 3:
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;
326 ready = 1;
327 break;
329 default:
330 fepriv->auto_step++;
331 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
332 break;
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) {
342 return 1;
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_in.frequency += fepriv->lnb_drift;
352 if (autoinversion)
353 fepriv->parameters_in.inversion = fepriv->inversion;
354 if (fe->ops.set_frontend)
355 fe_set_err = fe->ops.set_frontend(fe, &fepriv->parameters_in);
356 fepriv->parameters_out = fepriv->parameters_in;
357 if (fe_set_err < 0) {
358 fepriv->state = FESTATE_ERROR;
359 return fe_set_err;
362 fepriv->parameters_in.frequency = original_frequency;
363 fepriv->parameters_in.inversion = original_inversion;
365 fepriv->auto_sub_step++;
366 return 0;
369 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
371 fe_status_t s = 0;
372 int retval = 0;
373 struct dvb_frontend_private *fepriv = fe->frontend_priv;
375 /* if we've got no parameters, just keep idling */
376 if (fepriv->state & FESTATE_IDLE) {
377 fepriv->delay = 3*HZ;
378 fepriv->quality = 0;
379 return;
382 /* in SCAN mode, we just set the frontend when asked and leave it alone */
383 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
384 if (fepriv->state & FESTATE_RETUNE) {
385 if (fe->ops.set_frontend)
386 retval = fe->ops.set_frontend(fe,
387 &fepriv->parameters_in);
388 fepriv->parameters_out = fepriv->parameters_in;
389 if (retval < 0)
390 fepriv->state = FESTATE_ERROR;
391 else
392 fepriv->state = FESTATE_TUNED;
394 fepriv->delay = 3*HZ;
395 fepriv->quality = 0;
396 return;
399 /* get the frontend status */
400 if (fepriv->state & FESTATE_RETUNE) {
401 s = 0;
402 } else {
403 if (fe->ops.read_status)
404 fe->ops.read_status(fe, &s);
405 if (s != fepriv->status) {
406 dvb_frontend_add_event(fe, s);
407 fepriv->status = s;
411 /* if we're not tuned, and we have a lock, move to the TUNED state */
412 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
413 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
414 fepriv->state = FESTATE_TUNED;
416 /* if we're tuned, then we have determined the correct inversion */
417 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
418 (fepriv->parameters_in.inversion == INVERSION_AUTO)) {
419 fepriv->parameters_in.inversion = fepriv->inversion;
421 return;
424 /* if we are tuned already, check we're still locked */
425 if (fepriv->state & FESTATE_TUNED) {
426 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
428 /* we're tuned, and the lock is still good... */
429 if (s & FE_HAS_LOCK) {
430 return;
431 } else { /* if we _WERE_ tuned, but now don't have a lock */
432 fepriv->state = FESTATE_ZIGZAG_FAST;
433 fepriv->started_auto_step = fepriv->auto_step;
434 fepriv->check_wrapped = 0;
438 /* don't actually do anything if we're in the LOSTLOCK state,
439 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
440 if ((fepriv->state & FESTATE_LOSTLOCK) &&
441 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
442 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
443 return;
446 /* don't do anything if we're in the DISEQC state, since this
447 * might be someone with a motorized dish controlled by DISEQC.
448 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
449 if (fepriv->state & FESTATE_DISEQC) {
450 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
451 return;
454 /* if we're in the RETUNE state, set everything up for a brand
455 * new scan, keeping the current inversion setting, as the next
456 * tune is _very_ likely to require the same */
457 if (fepriv->state & FESTATE_RETUNE) {
458 fepriv->lnb_drift = 0;
459 fepriv->auto_step = 0;
460 fepriv->auto_sub_step = 0;
461 fepriv->started_auto_step = 0;
462 fepriv->check_wrapped = 0;
465 /* fast zigzag. */
466 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
467 fepriv->delay = fepriv->min_delay;
469 /* perform a tune */
470 retval = dvb_frontend_swzigzag_autotune(fe,
471 fepriv->check_wrapped);
472 if (retval < 0) {
473 return;
474 } else if (retval) {
475 /* OK, if we've run out of trials at the fast speed.
476 * Drop back to slow for the _next_ attempt */
477 fepriv->state = FESTATE_SEARCHING_SLOW;
478 fepriv->started_auto_step = fepriv->auto_step;
479 return;
481 fepriv->check_wrapped = 1;
483 /* if we've just retuned, enter the ZIGZAG_FAST state.
484 * This ensures we cannot return from an
485 * FE_SET_FRONTEND ioctl before the first frontend tune
486 * occurs */
487 if (fepriv->state & FESTATE_RETUNE) {
488 fepriv->state = FESTATE_TUNING_FAST;
492 /* slow zigzag */
493 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
494 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
496 /* Note: don't bother checking for wrapping; we stay in this
497 * state until we get a lock */
498 dvb_frontend_swzigzag_autotune(fe, 0);
502 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
504 struct dvb_frontend_private *fepriv = fe->frontend_priv;
506 if (fepriv->exit != DVB_FE_NO_EXIT)
507 return 1;
509 if (fepriv->dvbdev->writers == 1)
510 if (time_after(jiffies, fepriv->release_jiffies +
511 dvb_shutdown_timeout * HZ))
512 return 1;
514 return 0;
517 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
519 struct dvb_frontend_private *fepriv = fe->frontend_priv;
521 if (fepriv->wakeup) {
522 fepriv->wakeup = 0;
523 return 1;
525 return dvb_frontend_is_exiting(fe);
528 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
530 struct dvb_frontend_private *fepriv = fe->frontend_priv;
532 fepriv->wakeup = 1;
533 wake_up_interruptible(&fepriv->wait_queue);
536 static int dvb_frontend_thread(void *data)
538 struct dvb_frontend *fe = data;
539 struct dvb_frontend_private *fepriv = fe->frontend_priv;
540 unsigned long timeout;
541 fe_status_t s;
542 enum dvbfe_algo algo;
544 struct dvb_frontend_parameters *params;
546 dprintk("%s\n", __func__);
548 fepriv->check_wrapped = 0;
549 fepriv->quality = 0;
550 fepriv->delay = 3*HZ;
551 fepriv->status = 0;
552 fepriv->wakeup = 0;
553 fepriv->reinitialise = 0;
555 dvb_frontend_init(fe);
557 set_freezable();
558 while (1) {
559 up(&fepriv->sem); /* is locked when we enter the thread... */
560 restart:
561 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
562 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
563 || freezing(current),
564 fepriv->delay);
566 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
567 /* got signal or quitting */
568 fepriv->exit = DVB_FE_NORMAL_EXIT;
569 break;
572 if (try_to_freeze())
573 goto restart;
575 if (down_interruptible(&fepriv->sem))
576 break;
578 if (fepriv->reinitialise) {
579 dvb_frontend_init(fe);
580 if (fepriv->tone != -1) {
581 fe->ops.set_tone(fe, fepriv->tone);
583 if (fepriv->voltage != -1) {
584 fe->ops.set_voltage(fe, fepriv->voltage);
586 fepriv->reinitialise = 0;
589 /* do an iteration of the tuning loop */
590 if (fe->ops.get_frontend_algo) {
591 algo = fe->ops.get_frontend_algo(fe);
592 switch (algo) {
593 case DVBFE_ALGO_HW:
594 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
595 params = NULL; /* have we been asked to RETUNE ? */
597 if (fepriv->state & FESTATE_RETUNE) {
598 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
599 params = &fepriv->parameters_in;
600 fepriv->state = FESTATE_TUNED;
603 if (fe->ops.tune)
604 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
605 if (params)
606 fepriv->parameters_out = *params;
608 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
609 dprintk("%s: state changed, adding current state\n", __func__);
610 dvb_frontend_add_event(fe, s);
611 fepriv->status = s;
613 break;
614 case DVBFE_ALGO_SW:
615 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
616 dvb_frontend_swzigzag(fe);
617 break;
618 case DVBFE_ALGO_CUSTOM:
619 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
620 if (fepriv->state & FESTATE_RETUNE) {
621 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
622 fepriv->state = FESTATE_TUNED;
624 /* Case where we are going to search for a carrier
625 * User asked us to retune again for some reason, possibly
626 * requesting a search with a new set of parameters
628 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
629 if (fe->ops.search) {
630 fepriv->algo_status = fe->ops.search(fe, &fepriv->parameters_in);
631 /* We did do a search as was requested, the flags are
632 * now unset as well and has the flags wrt to search.
634 } else {
635 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
638 /* Track the carrier if the search was successful */
639 if (fepriv->algo_status == DVBFE_ALGO_SEARCH_SUCCESS) {
640 if (fe->ops.track)
641 fe->ops.track(fe, &fepriv->parameters_in);
642 } else {
643 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
644 fepriv->delay = HZ / 2;
646 fepriv->parameters_out = fepriv->parameters_in;
647 fe->ops.read_status(fe, &s);
648 if (s != fepriv->status) {
649 dvb_frontend_add_event(fe, s); /* update event list */
650 fepriv->status = s;
651 if (!(s & FE_HAS_LOCK)) {
652 fepriv->delay = HZ / 10;
653 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
654 } else {
655 fepriv->delay = 60 * HZ;
658 break;
659 default:
660 dprintk("%s: UNDEFINED ALGO !\n", __func__);
661 break;
663 } else {
664 dvb_frontend_swzigzag(fe);
668 if (dvb_powerdown_on_sleep) {
669 if (fe->ops.set_voltage)
670 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
671 if (fe->ops.tuner_ops.sleep) {
672 if (fe->ops.i2c_gate_ctrl)
673 fe->ops.i2c_gate_ctrl(fe, 1);
674 fe->ops.tuner_ops.sleep(fe);
675 if (fe->ops.i2c_gate_ctrl)
676 fe->ops.i2c_gate_ctrl(fe, 0);
678 if (fe->ops.sleep)
679 fe->ops.sleep(fe);
682 fepriv->thread = NULL;
683 if (kthread_should_stop())
684 fepriv->exit = DVB_FE_DEVICE_REMOVED;
685 else
686 fepriv->exit = DVB_FE_NO_EXIT;
687 mb();
689 dvb_frontend_wakeup(fe);
690 return 0;
693 static void dvb_frontend_stop(struct dvb_frontend *fe)
695 struct dvb_frontend_private *fepriv = fe->frontend_priv;
697 dprintk ("%s\n", __func__);
699 fepriv->exit = DVB_FE_NORMAL_EXIT;
700 mb();
702 if (!fepriv->thread)
703 return;
705 kthread_stop(fepriv->thread);
707 sema_init(&fepriv->sem, 1);
708 fepriv->state = FESTATE_IDLE;
710 /* paranoia check in case a signal arrived */
711 if (fepriv->thread)
712 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
713 fepriv->thread);
716 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
718 return ((curtime.tv_usec < lasttime.tv_usec) ?
719 1000000 - lasttime.tv_usec + curtime.tv_usec :
720 curtime.tv_usec - lasttime.tv_usec);
722 EXPORT_SYMBOL(timeval_usec_diff);
724 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
726 curtime->tv_usec += add_usec;
727 if (curtime->tv_usec >= 1000000) {
728 curtime->tv_usec -= 1000000;
729 curtime->tv_sec++;
734 * Sleep until gettimeofday() > waketime + add_usec
735 * This needs to be as precise as possible, but as the delay is
736 * usually between 2ms and 32ms, it is done using a scheduled msleep
737 * followed by usleep (normally a busy-wait loop) for the remainder
739 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
741 struct timeval lasttime;
742 s32 delta, newdelta;
744 timeval_usec_add(waketime, add_usec);
746 do_gettimeofday(&lasttime);
747 delta = timeval_usec_diff(lasttime, *waketime);
748 if (delta > 2500) {
749 msleep((delta - 1500) / 1000);
750 do_gettimeofday(&lasttime);
751 newdelta = timeval_usec_diff(lasttime, *waketime);
752 delta = (newdelta > delta) ? 0 : newdelta;
754 if (delta > 0)
755 udelay(delta);
757 EXPORT_SYMBOL(dvb_frontend_sleep_until);
759 static int dvb_frontend_start(struct dvb_frontend *fe)
761 int ret;
762 struct dvb_frontend_private *fepriv = fe->frontend_priv;
763 struct task_struct *fe_thread;
765 dprintk ("%s\n", __func__);
767 if (fepriv->thread) {
768 if (fepriv->exit == DVB_FE_NO_EXIT)
769 return 0;
770 else
771 dvb_frontend_stop (fe);
774 if (signal_pending(current))
775 return -EINTR;
776 if (down_interruptible (&fepriv->sem))
777 return -EINTR;
779 fepriv->state = FESTATE_IDLE;
780 fepriv->exit = DVB_FE_NO_EXIT;
781 fepriv->thread = NULL;
782 mb();
784 fe_thread = kthread_run(dvb_frontend_thread, fe,
785 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
786 if (IS_ERR(fe_thread)) {
787 ret = PTR_ERR(fe_thread);
788 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
789 up(&fepriv->sem);
790 return ret;
792 fepriv->thread = fe_thread;
793 return 0;
796 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
797 u32 *freq_min, u32 *freq_max)
799 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
801 if (fe->ops.info.frequency_max == 0)
802 *freq_max = fe->ops.tuner_ops.info.frequency_max;
803 else if (fe->ops.tuner_ops.info.frequency_max == 0)
804 *freq_max = fe->ops.info.frequency_max;
805 else
806 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
808 if (*freq_min == 0 || *freq_max == 0)
809 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
810 fe->dvb->num,fe->id);
813 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
814 struct dvb_frontend_parameters *parms)
816 u32 freq_min;
817 u32 freq_max;
819 /* range check: frequency */
820 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
821 if ((freq_min && parms->frequency < freq_min) ||
822 (freq_max && parms->frequency > freq_max)) {
823 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
824 fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
825 return -EINVAL;
828 /* range check: symbol rate */
829 if (fe->ops.info.type == FE_QPSK) {
830 if ((fe->ops.info.symbol_rate_min &&
831 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
832 (fe->ops.info.symbol_rate_max &&
833 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
834 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
835 fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
836 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
837 return -EINVAL;
840 } else if (fe->ops.info.type == FE_QAM) {
841 if ((fe->ops.info.symbol_rate_min &&
842 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
843 (fe->ops.info.symbol_rate_max &&
844 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
845 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
846 fe->dvb->num, fe->id, parms->u.qam.symbol_rate,
847 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
848 return -EINVAL;
852 /* check for supported modulation */
853 if (fe->ops.info.type == FE_QAM &&
854 (parms->u.qam.modulation > QAM_AUTO ||
855 !((1 << (parms->u.qam.modulation + 10)) & fe->ops.info.caps))) {
856 printk(KERN_WARNING "DVB: adapter %i frontend %i modulation %u not supported\n",
857 fe->dvb->num, fe->id, parms->u.qam.modulation);
858 return -EINVAL;
861 return 0;
864 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
866 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
867 int i;
869 memset(c, 0, sizeof(struct dtv_frontend_properties));
871 c->state = DTV_CLEAR;
872 c->delivery_system = SYS_UNDEFINED;
873 c->inversion = INVERSION_AUTO;
874 c->fec_inner = FEC_AUTO;
875 c->transmission_mode = TRANSMISSION_MODE_AUTO;
876 c->bandwidth_hz = BANDWIDTH_AUTO;
877 c->guard_interval = GUARD_INTERVAL_AUTO;
878 c->hierarchy = HIERARCHY_AUTO;
879 c->symbol_rate = QAM_AUTO;
880 c->code_rate_HP = FEC_AUTO;
881 c->code_rate_LP = FEC_AUTO;
883 c->isdbt_partial_reception = -1;
884 c->isdbt_sb_mode = -1;
885 c->isdbt_sb_subchannel = -1;
886 c->isdbt_sb_segment_idx = -1;
887 c->isdbt_sb_segment_count = -1;
888 c->isdbt_layer_enabled = 0x7;
889 for (i = 0; i < 3; i++) {
890 c->layer[i].fec = FEC_AUTO;
891 c->layer[i].modulation = QAM_AUTO;
892 c->layer[i].interleaving = -1;
893 c->layer[i].segment_count = -1;
896 return 0;
899 #define _DTV_CMD(n, s, b) \
900 [n] = { \
901 .name = #n, \
902 .cmd = n, \
903 .set = s,\
904 .buffer = b \
907 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
908 _DTV_CMD(DTV_TUNE, 1, 0),
909 _DTV_CMD(DTV_CLEAR, 1, 0),
911 /* Set */
912 _DTV_CMD(DTV_FREQUENCY, 1, 0),
913 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
914 _DTV_CMD(DTV_MODULATION, 1, 0),
915 _DTV_CMD(DTV_INVERSION, 1, 0),
916 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
917 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
918 _DTV_CMD(DTV_INNER_FEC, 1, 0),
919 _DTV_CMD(DTV_VOLTAGE, 1, 0),
920 _DTV_CMD(DTV_TONE, 1, 0),
921 _DTV_CMD(DTV_PILOT, 1, 0),
922 _DTV_CMD(DTV_ROLLOFF, 1, 0),
923 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
924 _DTV_CMD(DTV_HIERARCHY, 1, 0),
925 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
926 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
927 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
928 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
930 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
931 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
932 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
933 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
934 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
935 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
936 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
937 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
938 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
939 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
940 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
941 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
942 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
943 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
944 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
945 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
946 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
947 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
949 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 0, 0),
950 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 0, 0),
951 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 0, 0),
952 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 0, 0),
953 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 0, 0),
954 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 0, 0),
955 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 0, 0),
956 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 0, 0),
957 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 0, 0),
958 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 0, 0),
959 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 0, 0),
960 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 0, 0),
961 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 0, 0),
962 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 0, 0),
963 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 0, 0),
964 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 0, 0),
965 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 0, 0),
966 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 0, 0),
968 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
969 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
971 /* Get */
972 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
973 _DTV_CMD(DTV_API_VERSION, 0, 0),
974 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
975 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
976 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
977 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
978 _DTV_CMD(DTV_HIERARCHY, 0, 0),
981 static void dtv_property_dump(struct dtv_property *tvp)
983 int i;
985 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
986 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
987 __func__, tvp->cmd);
988 return;
991 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
992 ,__func__
993 ,tvp->cmd
994 ,dtv_cmds[ tvp->cmd ].name);
996 if(dtv_cmds[ tvp->cmd ].buffer) {
998 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
999 ,__func__
1000 ,tvp->u.buffer.len);
1002 for(i = 0; i < tvp->u.buffer.len; i++)
1003 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1004 ,__func__
1006 ,tvp->u.buffer.data[i]);
1008 } else
1009 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1012 static int is_legacy_delivery_system(fe_delivery_system_t s)
1014 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
1015 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS) ||
1016 (s == SYS_ATSC))
1017 return 1;
1019 return 0;
1022 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1023 * drivers can use a single set_frontend tuning function, regardless of whether
1024 * it's being used for the legacy or new API, reducing code and complexity.
1026 static void dtv_property_cache_sync(struct dvb_frontend *fe,
1027 struct dtv_frontend_properties *c,
1028 const struct dvb_frontend_parameters *p)
1030 c->frequency = p->frequency;
1031 c->inversion = p->inversion;
1033 switch (fe->ops.info.type) {
1034 case FE_QPSK:
1035 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1036 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1037 c->symbol_rate = p->u.qpsk.symbol_rate;
1038 c->fec_inner = p->u.qpsk.fec_inner;
1039 c->delivery_system = SYS_DVBS;
1040 break;
1041 case FE_QAM:
1042 c->symbol_rate = p->u.qam.symbol_rate;
1043 c->fec_inner = p->u.qam.fec_inner;
1044 c->modulation = p->u.qam.modulation;
1045 c->delivery_system = SYS_DVBC_ANNEX_AC;
1046 break;
1047 case FE_OFDM:
1048 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
1049 c->bandwidth_hz = 6000000;
1050 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
1051 c->bandwidth_hz = 7000000;
1052 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
1053 c->bandwidth_hz = 8000000;
1054 else
1055 /* Including BANDWIDTH_AUTO */
1056 c->bandwidth_hz = 0;
1057 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1058 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1059 c->modulation = p->u.ofdm.constellation;
1060 c->transmission_mode = p->u.ofdm.transmission_mode;
1061 c->guard_interval = p->u.ofdm.guard_interval;
1062 c->hierarchy = p->u.ofdm.hierarchy_information;
1063 c->delivery_system = SYS_DVBT;
1064 break;
1065 case FE_ATSC:
1066 c->modulation = p->u.vsb.modulation;
1067 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1068 c->delivery_system = SYS_ATSC;
1069 else
1070 c->delivery_system = SYS_DVBC_ANNEX_B;
1071 break;
1075 /* Ensure the cached values are set correctly in the frontend
1076 * legacy tuning structures, for the advanced tuning API.
1078 static void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1080 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1081 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1082 struct dvb_frontend_parameters *p = &fepriv->parameters_in;
1084 p->frequency = c->frequency;
1085 p->inversion = c->inversion;
1087 switch (fe->ops.info.type) {
1088 case FE_QPSK:
1089 dprintk("%s() Preparing QPSK req\n", __func__);
1090 p->u.qpsk.symbol_rate = c->symbol_rate;
1091 p->u.qpsk.fec_inner = c->fec_inner;
1092 break;
1093 case FE_QAM:
1094 dprintk("%s() Preparing QAM req\n", __func__);
1095 p->u.qam.symbol_rate = c->symbol_rate;
1096 p->u.qam.fec_inner = c->fec_inner;
1097 p->u.qam.modulation = c->modulation;
1098 break;
1099 case FE_OFDM:
1100 dprintk("%s() Preparing OFDM req\n", __func__);
1101 if (c->bandwidth_hz == 6000000)
1102 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1103 else if (c->bandwidth_hz == 7000000)
1104 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1105 else if (c->bandwidth_hz == 8000000)
1106 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1107 else
1108 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1109 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1110 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1111 p->u.ofdm.constellation = c->modulation;
1112 p->u.ofdm.transmission_mode = c->transmission_mode;
1113 p->u.ofdm.guard_interval = c->guard_interval;
1114 p->u.ofdm.hierarchy_information = c->hierarchy;
1115 break;
1116 case FE_ATSC:
1117 dprintk("%s() Preparing VSB req\n", __func__);
1118 p->u.vsb.modulation = c->modulation;
1119 break;
1123 /* Ensure the cached values are set correctly in the frontend
1124 * legacy tuning structures, for the legacy tuning API.
1126 static void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1128 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1129 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1130 struct dvb_frontend_parameters *p = &fepriv->parameters_in;
1132 p->frequency = c->frequency;
1133 p->inversion = c->inversion;
1135 switch(c->modulation) {
1136 case PSK_8:
1137 case APSK_16:
1138 case APSK_32:
1139 case QPSK:
1140 p->u.qpsk.symbol_rate = c->symbol_rate;
1141 p->u.qpsk.fec_inner = c->fec_inner;
1142 break;
1143 default:
1144 break;
1147 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1148 if ((c->delivery_system == SYS_ISDBT) ||
1149 (c->delivery_system == SYS_DVBT2)) {
1150 p->u.ofdm.constellation = QAM_AUTO;
1151 p->u.ofdm.code_rate_HP = FEC_AUTO;
1152 p->u.ofdm.code_rate_LP = FEC_AUTO;
1153 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1154 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1155 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1156 if (c->bandwidth_hz == 8000000)
1157 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1158 else if (c->bandwidth_hz == 7000000)
1159 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1160 else if (c->bandwidth_hz == 6000000)
1161 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1162 else
1163 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1167 static void dtv_property_cache_submit(struct dvb_frontend *fe)
1169 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1171 /* For legacy delivery systems we don't need the delivery_system to
1172 * be specified, but we populate the older structures from the cache
1173 * so we can call set_frontend on older drivers.
1175 if(is_legacy_delivery_system(c->delivery_system)) {
1177 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1178 dtv_property_legacy_params_sync(fe);
1180 } else {
1181 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1183 /* For advanced delivery systems / modulation types ...
1184 * we seed the lecacy dvb_frontend_parameters structure
1185 * so that the sanity checking code later in the IOCTL processing
1186 * can validate our basic frequency ranges, symbolrates, modulation
1187 * etc.
1189 dtv_property_adv_params_sync(fe);
1193 static int dvb_frontend_ioctl_legacy(struct file *file,
1194 unsigned int cmd, void *parg);
1195 static int dvb_frontend_ioctl_properties(struct file *file,
1196 unsigned int cmd, void *parg);
1198 static int dtv_property_process_get(struct dvb_frontend *fe,
1199 struct dtv_property *tvp,
1200 struct file *file)
1202 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1203 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1204 struct dtv_frontend_properties cdetected;
1205 int r;
1208 * If the driver implements a get_frontend function, then convert
1209 * detected parameters to S2API properties.
1211 if (fe->ops.get_frontend) {
1212 cdetected = *c;
1213 dtv_property_cache_sync(fe, &cdetected, &fepriv->parameters_out);
1214 c = &cdetected;
1217 switch(tvp->cmd) {
1218 case DTV_FREQUENCY:
1219 tvp->u.data = c->frequency;
1220 break;
1221 case DTV_MODULATION:
1222 tvp->u.data = c->modulation;
1223 break;
1224 case DTV_BANDWIDTH_HZ:
1225 tvp->u.data = c->bandwidth_hz;
1226 break;
1227 case DTV_INVERSION:
1228 tvp->u.data = c->inversion;
1229 break;
1230 case DTV_SYMBOL_RATE:
1231 tvp->u.data = c->symbol_rate;
1232 break;
1233 case DTV_INNER_FEC:
1234 tvp->u.data = c->fec_inner;
1235 break;
1236 case DTV_PILOT:
1237 tvp->u.data = c->pilot;
1238 break;
1239 case DTV_ROLLOFF:
1240 tvp->u.data = c->rolloff;
1241 break;
1242 case DTV_DELIVERY_SYSTEM:
1243 tvp->u.data = c->delivery_system;
1244 break;
1245 case DTV_VOLTAGE:
1246 tvp->u.data = c->voltage;
1247 break;
1248 case DTV_TONE:
1249 tvp->u.data = c->sectone;
1250 break;
1251 case DTV_API_VERSION:
1252 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1253 break;
1254 case DTV_CODE_RATE_HP:
1255 tvp->u.data = c->code_rate_HP;
1256 break;
1257 case DTV_CODE_RATE_LP:
1258 tvp->u.data = c->code_rate_LP;
1259 break;
1260 case DTV_GUARD_INTERVAL:
1261 tvp->u.data = c->guard_interval;
1262 break;
1263 case DTV_TRANSMISSION_MODE:
1264 tvp->u.data = c->transmission_mode;
1265 break;
1266 case DTV_HIERARCHY:
1267 tvp->u.data = c->hierarchy;
1268 break;
1270 /* ISDB-T Support here */
1271 case DTV_ISDBT_PARTIAL_RECEPTION:
1272 tvp->u.data = c->isdbt_partial_reception;
1273 break;
1274 case DTV_ISDBT_SOUND_BROADCASTING:
1275 tvp->u.data = c->isdbt_sb_mode;
1276 break;
1277 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1278 tvp->u.data = c->isdbt_sb_subchannel;
1279 break;
1280 case DTV_ISDBT_SB_SEGMENT_IDX:
1281 tvp->u.data = c->isdbt_sb_segment_idx;
1282 break;
1283 case DTV_ISDBT_SB_SEGMENT_COUNT:
1284 tvp->u.data = c->isdbt_sb_segment_count;
1285 break;
1286 case DTV_ISDBT_LAYER_ENABLED:
1287 tvp->u.data = c->isdbt_layer_enabled;
1288 break;
1289 case DTV_ISDBT_LAYERA_FEC:
1290 tvp->u.data = c->layer[0].fec;
1291 break;
1292 case DTV_ISDBT_LAYERA_MODULATION:
1293 tvp->u.data = c->layer[0].modulation;
1294 break;
1295 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1296 tvp->u.data = c->layer[0].segment_count;
1297 break;
1298 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1299 tvp->u.data = c->layer[0].interleaving;
1300 break;
1301 case DTV_ISDBT_LAYERB_FEC:
1302 tvp->u.data = c->layer[1].fec;
1303 break;
1304 case DTV_ISDBT_LAYERB_MODULATION:
1305 tvp->u.data = c->layer[1].modulation;
1306 break;
1307 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1308 tvp->u.data = c->layer[1].segment_count;
1309 break;
1310 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1311 tvp->u.data = c->layer[1].interleaving;
1312 break;
1313 case DTV_ISDBT_LAYERC_FEC:
1314 tvp->u.data = c->layer[2].fec;
1315 break;
1316 case DTV_ISDBT_LAYERC_MODULATION:
1317 tvp->u.data = c->layer[2].modulation;
1318 break;
1319 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1320 tvp->u.data = c->layer[2].segment_count;
1321 break;
1322 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1323 tvp->u.data = c->layer[2].interleaving;
1324 break;
1325 case DTV_ISDBS_TS_ID:
1326 tvp->u.data = c->isdbs_ts_id;
1327 break;
1328 case DTV_DVBT2_PLP_ID:
1329 tvp->u.data = c->dvbt2_plp_id;
1330 break;
1331 default:
1332 return -EINVAL;
1335 /* Allow the frontend to override outgoing properties */
1336 if (fe->ops.get_property) {
1337 r = fe->ops.get_property(fe, tvp);
1338 if (r < 0)
1339 return r;
1342 dtv_property_dump(tvp);
1344 return 0;
1347 static int dtv_property_process_set(struct dvb_frontend *fe,
1348 struct dtv_property *tvp,
1349 struct file *file)
1351 int r = 0;
1352 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1353 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1354 dtv_property_dump(tvp);
1356 /* Allow the frontend to validate incoming properties */
1357 if (fe->ops.set_property) {
1358 r = fe->ops.set_property(fe, tvp);
1359 if (r < 0)
1360 return r;
1363 switch(tvp->cmd) {
1364 case DTV_CLEAR:
1365 /* Reset a cache of data specific to the frontend here. This does
1366 * not effect hardware.
1368 dvb_frontend_clear_cache(fe);
1369 dprintk("%s() Flushing property cache\n", __func__);
1370 break;
1371 case DTV_TUNE:
1372 /* interpret the cache of data, build either a traditional frontend
1373 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1374 * ioctl.
1376 c->state = tvp->cmd;
1377 dprintk("%s() Finalised property cache\n", __func__);
1378 dtv_property_cache_submit(fe);
1380 r = dvb_frontend_ioctl_legacy(file, FE_SET_FRONTEND,
1381 &fepriv->parameters_in);
1382 break;
1383 case DTV_FREQUENCY:
1384 c->frequency = tvp->u.data;
1385 break;
1386 case DTV_MODULATION:
1387 c->modulation = tvp->u.data;
1388 break;
1389 case DTV_BANDWIDTH_HZ:
1390 c->bandwidth_hz = tvp->u.data;
1391 break;
1392 case DTV_INVERSION:
1393 c->inversion = tvp->u.data;
1394 break;
1395 case DTV_SYMBOL_RATE:
1396 c->symbol_rate = tvp->u.data;
1397 break;
1398 case DTV_INNER_FEC:
1399 c->fec_inner = tvp->u.data;
1400 break;
1401 case DTV_PILOT:
1402 c->pilot = tvp->u.data;
1403 break;
1404 case DTV_ROLLOFF:
1405 c->rolloff = tvp->u.data;
1406 break;
1407 case DTV_DELIVERY_SYSTEM:
1408 c->delivery_system = tvp->u.data;
1409 break;
1410 case DTV_VOLTAGE:
1411 c->voltage = tvp->u.data;
1412 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1413 (void *)c->voltage);
1414 break;
1415 case DTV_TONE:
1416 c->sectone = tvp->u.data;
1417 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1418 (void *)c->sectone);
1419 break;
1420 case DTV_CODE_RATE_HP:
1421 c->code_rate_HP = tvp->u.data;
1422 break;
1423 case DTV_CODE_RATE_LP:
1424 c->code_rate_LP = tvp->u.data;
1425 break;
1426 case DTV_GUARD_INTERVAL:
1427 c->guard_interval = tvp->u.data;
1428 break;
1429 case DTV_TRANSMISSION_MODE:
1430 c->transmission_mode = tvp->u.data;
1431 break;
1432 case DTV_HIERARCHY:
1433 c->hierarchy = tvp->u.data;
1434 break;
1436 /* ISDB-T Support here */
1437 case DTV_ISDBT_PARTIAL_RECEPTION:
1438 c->isdbt_partial_reception = tvp->u.data;
1439 break;
1440 case DTV_ISDBT_SOUND_BROADCASTING:
1441 c->isdbt_sb_mode = tvp->u.data;
1442 break;
1443 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1444 c->isdbt_sb_subchannel = tvp->u.data;
1445 break;
1446 case DTV_ISDBT_SB_SEGMENT_IDX:
1447 c->isdbt_sb_segment_idx = tvp->u.data;
1448 break;
1449 case DTV_ISDBT_SB_SEGMENT_COUNT:
1450 c->isdbt_sb_segment_count = tvp->u.data;
1451 break;
1452 case DTV_ISDBT_LAYER_ENABLED:
1453 c->isdbt_layer_enabled = tvp->u.data;
1454 break;
1455 case DTV_ISDBT_LAYERA_FEC:
1456 c->layer[0].fec = tvp->u.data;
1457 break;
1458 case DTV_ISDBT_LAYERA_MODULATION:
1459 c->layer[0].modulation = tvp->u.data;
1460 break;
1461 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1462 c->layer[0].segment_count = tvp->u.data;
1463 break;
1464 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1465 c->layer[0].interleaving = tvp->u.data;
1466 break;
1467 case DTV_ISDBT_LAYERB_FEC:
1468 c->layer[1].fec = tvp->u.data;
1469 break;
1470 case DTV_ISDBT_LAYERB_MODULATION:
1471 c->layer[1].modulation = tvp->u.data;
1472 break;
1473 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1474 c->layer[1].segment_count = tvp->u.data;
1475 break;
1476 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1477 c->layer[1].interleaving = tvp->u.data;
1478 break;
1479 case DTV_ISDBT_LAYERC_FEC:
1480 c->layer[2].fec = tvp->u.data;
1481 break;
1482 case DTV_ISDBT_LAYERC_MODULATION:
1483 c->layer[2].modulation = tvp->u.data;
1484 break;
1485 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1486 c->layer[2].segment_count = tvp->u.data;
1487 break;
1488 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1489 c->layer[2].interleaving = tvp->u.data;
1490 break;
1491 case DTV_ISDBS_TS_ID:
1492 c->isdbs_ts_id = tvp->u.data;
1493 break;
1494 case DTV_DVBT2_PLP_ID:
1495 c->dvbt2_plp_id = tvp->u.data;
1496 break;
1497 default:
1498 return -EINVAL;
1501 return r;
1504 static int dvb_frontend_ioctl(struct file *file,
1505 unsigned int cmd, void *parg)
1507 struct dvb_device *dvbdev = file->private_data;
1508 struct dvb_frontend *fe = dvbdev->priv;
1509 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1510 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1511 int err = -EOPNOTSUPP;
1513 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1515 if (fepriv->exit != DVB_FE_NO_EXIT)
1516 return -ENODEV;
1518 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1519 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1520 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1521 return -EPERM;
1523 if (down_interruptible (&fepriv->sem))
1524 return -ERESTARTSYS;
1526 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1527 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1528 else {
1529 c->state = DTV_UNDEFINED;
1530 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1533 up(&fepriv->sem);
1534 return err;
1537 static int dvb_frontend_ioctl_properties(struct file *file,
1538 unsigned int cmd, void *parg)
1540 struct dvb_device *dvbdev = file->private_data;
1541 struct dvb_frontend *fe = dvbdev->priv;
1542 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1543 int err = 0;
1545 struct dtv_properties *tvps = NULL;
1546 struct dtv_property *tvp = NULL;
1547 int i;
1549 dprintk("%s\n", __func__);
1551 if(cmd == FE_SET_PROPERTY) {
1552 tvps = (struct dtv_properties __user *)parg;
1554 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1555 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1557 /* Put an arbitrary limit on the number of messages that can
1558 * be sent at once */
1559 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1560 return -EINVAL;
1562 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1563 if (!tvp) {
1564 err = -ENOMEM;
1565 goto out;
1568 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1569 err = -EFAULT;
1570 goto out;
1573 for (i = 0; i < tvps->num; i++) {
1574 err = dtv_property_process_set(fe, tvp + i, file);
1575 if (err < 0)
1576 goto out;
1577 (tvp + i)->result = err;
1580 if (c->state == DTV_TUNE)
1581 dprintk("%s() Property cache is full, tuning\n", __func__);
1583 } else
1584 if(cmd == FE_GET_PROPERTY) {
1586 tvps = (struct dtv_properties __user *)parg;
1588 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1589 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1591 /* Put an arbitrary limit on the number of messages that can
1592 * be sent at once */
1593 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1594 return -EINVAL;
1596 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1597 if (!tvp) {
1598 err = -ENOMEM;
1599 goto out;
1602 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1603 err = -EFAULT;
1604 goto out;
1607 for (i = 0; i < tvps->num; i++) {
1608 err = dtv_property_process_get(fe, tvp + i, file);
1609 if (err < 0)
1610 goto out;
1611 (tvp + i)->result = err;
1614 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1615 err = -EFAULT;
1616 goto out;
1619 } else
1620 err = -EOPNOTSUPP;
1622 out:
1623 kfree(tvp);
1624 return err;
1627 static int dvb_frontend_ioctl_legacy(struct file *file,
1628 unsigned int cmd, void *parg)
1630 struct dvb_device *dvbdev = file->private_data;
1631 struct dvb_frontend *fe = dvbdev->priv;
1632 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1633 int cb_err, err = -EOPNOTSUPP;
1635 if (fe->dvb->fe_ioctl_override) {
1636 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1637 DVB_FE_IOCTL_PRE);
1638 if (cb_err < 0)
1639 return cb_err;
1640 if (cb_err > 0)
1641 return 0;
1642 /* fe_ioctl_override returning 0 allows
1643 * dvb-core to continue handling the ioctl */
1646 switch (cmd) {
1647 case FE_GET_INFO: {
1648 struct dvb_frontend_info* info = parg;
1649 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1650 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1652 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1653 * do it, it is done for it. */
1654 info->caps |= FE_CAN_INVERSION_AUTO;
1655 err = 0;
1656 break;
1659 case FE_READ_STATUS: {
1660 fe_status_t* status = parg;
1662 /* if retune was requested but hasn't occurred yet, prevent
1663 * that user get signal state from previous tuning */
1664 if (fepriv->state == FESTATE_RETUNE ||
1665 fepriv->state == FESTATE_ERROR) {
1666 err=0;
1667 *status = 0;
1668 break;
1671 if (fe->ops.read_status)
1672 err = fe->ops.read_status(fe, status);
1673 break;
1675 case FE_READ_BER:
1676 if (fe->ops.read_ber)
1677 err = fe->ops.read_ber(fe, (__u32*) parg);
1678 break;
1680 case FE_READ_SIGNAL_STRENGTH:
1681 if (fe->ops.read_signal_strength)
1682 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1683 break;
1685 case FE_READ_SNR:
1686 if (fe->ops.read_snr)
1687 err = fe->ops.read_snr(fe, (__u16*) parg);
1688 break;
1690 case FE_READ_UNCORRECTED_BLOCKS:
1691 if (fe->ops.read_ucblocks)
1692 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1693 break;
1696 case FE_DISEQC_RESET_OVERLOAD:
1697 if (fe->ops.diseqc_reset_overload) {
1698 err = fe->ops.diseqc_reset_overload(fe);
1699 fepriv->state = FESTATE_DISEQC;
1700 fepriv->status = 0;
1702 break;
1704 case FE_DISEQC_SEND_MASTER_CMD:
1705 if (fe->ops.diseqc_send_master_cmd) {
1706 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1707 fepriv->state = FESTATE_DISEQC;
1708 fepriv->status = 0;
1710 break;
1712 case FE_DISEQC_SEND_BURST:
1713 if (fe->ops.diseqc_send_burst) {
1714 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1715 fepriv->state = FESTATE_DISEQC;
1716 fepriv->status = 0;
1718 break;
1720 case FE_SET_TONE:
1721 if (fe->ops.set_tone) {
1722 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1723 fepriv->tone = (fe_sec_tone_mode_t) parg;
1724 fepriv->state = FESTATE_DISEQC;
1725 fepriv->status = 0;
1727 break;
1729 case FE_SET_VOLTAGE:
1730 if (fe->ops.set_voltage) {
1731 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1732 fepriv->voltage = (fe_sec_voltage_t) parg;
1733 fepriv->state = FESTATE_DISEQC;
1734 fepriv->status = 0;
1736 break;
1738 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1739 if (fe->ops.dishnetwork_send_legacy_command) {
1740 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1741 fepriv->state = FESTATE_DISEQC;
1742 fepriv->status = 0;
1743 } else if (fe->ops.set_voltage) {
1745 * NOTE: This is a fallback condition. Some frontends
1746 * (stv0299 for instance) take longer than 8msec to
1747 * respond to a set_voltage command. Those switches
1748 * need custom routines to switch properly. For all
1749 * other frontends, the following should work ok.
1750 * Dish network legacy switches (as used by Dish500)
1751 * are controlled by sending 9-bit command words
1752 * spaced 8msec apart.
1753 * the actual command word is switch/port dependent
1754 * so it is up to the userspace application to send
1755 * the right command.
1756 * The command must always start with a '0' after
1757 * initialization, so parg is 8 bits and does not
1758 * include the initialization or start bit
1760 unsigned long swcmd = ((unsigned long) parg) << 1;
1761 struct timeval nexttime;
1762 struct timeval tv[10];
1763 int i;
1764 u8 last = 1;
1765 if (dvb_frontend_debug)
1766 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1767 do_gettimeofday(&nexttime);
1768 if (dvb_frontend_debug)
1769 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1770 /* before sending a command, initialize by sending
1771 * a 32ms 18V to the switch
1773 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1774 dvb_frontend_sleep_until(&nexttime, 32000);
1776 for (i = 0; i < 9; i++) {
1777 if (dvb_frontend_debug)
1778 do_gettimeofday(&tv[i + 1]);
1779 if ((swcmd & 0x01) != last) {
1780 /* set voltage to (last ? 13V : 18V) */
1781 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1782 last = (last) ? 0 : 1;
1784 swcmd = swcmd >> 1;
1785 if (i != 8)
1786 dvb_frontend_sleep_until(&nexttime, 8000);
1788 if (dvb_frontend_debug) {
1789 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1790 __func__, fe->dvb->num);
1791 for (i = 1; i < 10; i++)
1792 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1794 err = 0;
1795 fepriv->state = FESTATE_DISEQC;
1796 fepriv->status = 0;
1798 break;
1800 case FE_DISEQC_RECV_SLAVE_REPLY:
1801 if (fe->ops.diseqc_recv_slave_reply)
1802 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1803 break;
1805 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1806 if (fe->ops.enable_high_lnb_voltage)
1807 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1808 break;
1810 case FE_SET_FRONTEND: {
1811 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1812 struct dvb_frontend_tune_settings fetunesettings;
1814 if (c->state == DTV_TUNE) {
1815 if (dvb_frontend_check_parameters(fe, &fepriv->parameters_in) < 0) {
1816 err = -EINVAL;
1817 break;
1819 } else {
1820 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1821 err = -EINVAL;
1822 break;
1825 memcpy (&fepriv->parameters_in, parg,
1826 sizeof (struct dvb_frontend_parameters));
1827 dtv_property_cache_sync(fe, c, &fepriv->parameters_in);
1830 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1831 memcpy(&fetunesettings.parameters, parg,
1832 sizeof (struct dvb_frontend_parameters));
1834 /* force auto frequency inversion if requested */
1835 if (dvb_force_auto_inversion) {
1836 fepriv->parameters_in.inversion = INVERSION_AUTO;
1837 fetunesettings.parameters.inversion = INVERSION_AUTO;
1839 if (fe->ops.info.type == FE_OFDM) {
1840 /* without hierarchical coding code_rate_LP is irrelevant,
1841 * so we tolerate the otherwise invalid FEC_NONE setting */
1842 if (fepriv->parameters_in.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1843 fepriv->parameters_in.u.ofdm.code_rate_LP == FEC_NONE)
1844 fepriv->parameters_in.u.ofdm.code_rate_LP = FEC_AUTO;
1847 /* get frontend-specific tuning settings */
1848 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1849 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1850 fepriv->max_drift = fetunesettings.max_drift;
1851 fepriv->step_size = fetunesettings.step_size;
1852 } else {
1853 /* default values */
1854 switch(fe->ops.info.type) {
1855 case FE_QPSK:
1856 fepriv->min_delay = HZ/20;
1857 fepriv->step_size = fepriv->parameters_in.u.qpsk.symbol_rate / 16000;
1858 fepriv->max_drift = fepriv->parameters_in.u.qpsk.symbol_rate / 2000;
1859 break;
1861 case FE_QAM:
1862 fepriv->min_delay = HZ/20;
1863 fepriv->step_size = 0; /* no zigzag */
1864 fepriv->max_drift = 0;
1865 break;
1867 case FE_OFDM:
1868 fepriv->min_delay = HZ/20;
1869 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1870 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1871 break;
1872 case FE_ATSC:
1873 fepriv->min_delay = HZ/20;
1874 fepriv->step_size = 0;
1875 fepriv->max_drift = 0;
1876 break;
1879 if (dvb_override_tune_delay > 0)
1880 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1882 fepriv->state = FESTATE_RETUNE;
1884 /* Request the search algorithm to search */
1885 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1887 dvb_frontend_wakeup(fe);
1888 dvb_frontend_add_event(fe, 0);
1889 fepriv->status = 0;
1890 err = 0;
1891 break;
1894 case FE_GET_EVENT:
1895 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1896 break;
1898 case FE_GET_FRONTEND:
1899 if (fe->ops.get_frontend) {
1900 err = fe->ops.get_frontend(fe, &fepriv->parameters_out);
1901 memcpy(parg, &fepriv->parameters_out, sizeof(struct dvb_frontend_parameters));
1903 break;
1905 case FE_SET_FRONTEND_TUNE_MODE:
1906 fepriv->tune_mode_flags = (unsigned long) parg;
1907 err = 0;
1908 break;
1911 if (fe->dvb->fe_ioctl_override) {
1912 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1913 DVB_FE_IOCTL_POST);
1914 if (cb_err < 0)
1915 return cb_err;
1918 return err;
1922 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1924 struct dvb_device *dvbdev = file->private_data;
1925 struct dvb_frontend *fe = dvbdev->priv;
1926 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1928 dprintk ("%s\n", __func__);
1930 poll_wait (file, &fepriv->events.wait_queue, wait);
1932 if (fepriv->events.eventw != fepriv->events.eventr)
1933 return (POLLIN | POLLRDNORM | POLLPRI);
1935 return 0;
1938 static int dvb_frontend_open(struct inode *inode, struct file *file)
1940 struct dvb_device *dvbdev = file->private_data;
1941 struct dvb_frontend *fe = dvbdev->priv;
1942 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1943 struct dvb_adapter *adapter = fe->dvb;
1944 int ret;
1946 dprintk ("%s\n", __func__);
1947 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
1948 return -ENODEV;
1950 if (adapter->mfe_shared) {
1951 mutex_lock (&adapter->mfe_lock);
1953 if (adapter->mfe_dvbdev == NULL)
1954 adapter->mfe_dvbdev = dvbdev;
1956 else if (adapter->mfe_dvbdev != dvbdev) {
1957 struct dvb_device
1958 *mfedev = adapter->mfe_dvbdev;
1959 struct dvb_frontend
1960 *mfe = mfedev->priv;
1961 struct dvb_frontend_private
1962 *mfepriv = mfe->frontend_priv;
1963 int mferetry = (dvb_mfe_wait_time << 1);
1965 mutex_unlock (&adapter->mfe_lock);
1966 while (mferetry-- && (mfedev->users != -1 ||
1967 mfepriv->thread != NULL)) {
1968 if(msleep_interruptible(500)) {
1969 if(signal_pending(current))
1970 return -EINTR;
1974 mutex_lock (&adapter->mfe_lock);
1975 if(adapter->mfe_dvbdev != dvbdev) {
1976 mfedev = adapter->mfe_dvbdev;
1977 mfe = mfedev->priv;
1978 mfepriv = mfe->frontend_priv;
1979 if (mfedev->users != -1 ||
1980 mfepriv->thread != NULL) {
1981 mutex_unlock (&adapter->mfe_lock);
1982 return -EBUSY;
1984 adapter->mfe_dvbdev = dvbdev;
1989 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1990 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1991 goto err0;
1993 /* If we took control of the bus, we need to force
1994 reinitialization. This is because many ts_bus_ctrl()
1995 functions strobe the RESET pin on the demod, and if the
1996 frontend thread already exists then the dvb_init() routine
1997 won't get called (which is what usually does initial
1998 register configuration). */
1999 fepriv->reinitialise = 1;
2002 if ((ret = dvb_generic_open (inode, file)) < 0)
2003 goto err1;
2005 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2006 /* normal tune mode when opened R/W */
2007 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2008 fepriv->tone = -1;
2009 fepriv->voltage = -1;
2011 ret = dvb_frontend_start (fe);
2012 if (ret)
2013 goto err2;
2015 /* empty event queue */
2016 fepriv->events.eventr = fepriv->events.eventw = 0;
2019 if (adapter->mfe_shared)
2020 mutex_unlock (&adapter->mfe_lock);
2021 return ret;
2023 err2:
2024 dvb_generic_release(inode, file);
2025 err1:
2026 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2027 fe->ops.ts_bus_ctrl(fe, 0);
2028 err0:
2029 if (adapter->mfe_shared)
2030 mutex_unlock (&adapter->mfe_lock);
2031 return ret;
2034 static int dvb_frontend_release(struct inode *inode, struct file *file)
2036 struct dvb_device *dvbdev = file->private_data;
2037 struct dvb_frontend *fe = dvbdev->priv;
2038 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2039 int ret;
2041 dprintk ("%s\n", __func__);
2043 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
2044 fepriv->release_jiffies = jiffies;
2046 ret = dvb_generic_release (inode, file);
2048 if (dvbdev->users == -1) {
2049 if (fepriv->exit != DVB_FE_NO_EXIT) {
2050 fops_put(file->f_op);
2051 file->f_op = NULL;
2052 wake_up(&dvbdev->wait_queue);
2054 if (fe->ops.ts_bus_ctrl)
2055 fe->ops.ts_bus_ctrl(fe, 0);
2058 return ret;
2061 static const struct file_operations dvb_frontend_fops = {
2062 .owner = THIS_MODULE,
2063 .unlocked_ioctl = dvb_generic_ioctl,
2064 .poll = dvb_frontend_poll,
2065 .open = dvb_frontend_open,
2066 .release = dvb_frontend_release,
2067 .llseek = noop_llseek,
2070 int dvb_register_frontend(struct dvb_adapter* dvb,
2071 struct dvb_frontend* fe)
2073 struct dvb_frontend_private *fepriv;
2074 static const struct dvb_device dvbdev_template = {
2075 .users = ~0,
2076 .writers = 1,
2077 .readers = (~0)-1,
2078 .fops = &dvb_frontend_fops,
2079 .kernel_ioctl = dvb_frontend_ioctl
2082 dprintk ("%s\n", __func__);
2084 if (mutex_lock_interruptible(&frontend_mutex))
2085 return -ERESTARTSYS;
2087 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2088 if (fe->frontend_priv == NULL) {
2089 mutex_unlock(&frontend_mutex);
2090 return -ENOMEM;
2092 fepriv = fe->frontend_priv;
2094 sema_init(&fepriv->sem, 1);
2095 init_waitqueue_head (&fepriv->wait_queue);
2096 init_waitqueue_head (&fepriv->events.wait_queue);
2097 mutex_init(&fepriv->events.mtx);
2098 fe->dvb = dvb;
2099 fepriv->inversion = INVERSION_OFF;
2101 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2102 fe->dvb->num,
2103 fe->id,
2104 fe->ops.info.name);
2106 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2107 fe, DVB_DEVICE_FRONTEND);
2109 mutex_unlock(&frontend_mutex);
2110 return 0;
2112 EXPORT_SYMBOL(dvb_register_frontend);
2114 int dvb_unregister_frontend(struct dvb_frontend* fe)
2116 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2117 dprintk ("%s\n", __func__);
2119 mutex_lock(&frontend_mutex);
2120 dvb_frontend_stop (fe);
2121 mutex_unlock(&frontend_mutex);
2123 if (fepriv->dvbdev->users < -1)
2124 wait_event(fepriv->dvbdev->wait_queue,
2125 fepriv->dvbdev->users==-1);
2127 mutex_lock(&frontend_mutex);
2128 dvb_unregister_device (fepriv->dvbdev);
2130 /* fe is invalid now */
2131 kfree(fepriv);
2132 mutex_unlock(&frontend_mutex);
2133 return 0;
2135 EXPORT_SYMBOL(dvb_unregister_frontend);
2137 #ifdef CONFIG_MEDIA_ATTACH
2138 void dvb_frontend_detach(struct dvb_frontend* fe)
2140 void *ptr;
2142 if (fe->ops.release_sec) {
2143 fe->ops.release_sec(fe);
2144 symbol_put_addr(fe->ops.release_sec);
2146 if (fe->ops.tuner_ops.release) {
2147 fe->ops.tuner_ops.release(fe);
2148 symbol_put_addr(fe->ops.tuner_ops.release);
2150 if (fe->ops.analog_ops.release) {
2151 fe->ops.analog_ops.release(fe);
2152 symbol_put_addr(fe->ops.analog_ops.release);
2154 ptr = (void*)fe->ops.release;
2155 if (ptr) {
2156 fe->ops.release(fe);
2157 symbol_put_addr(ptr);
2160 #else
2161 void dvb_frontend_detach(struct dvb_frontend* fe)
2163 if (fe->ops.release_sec)
2164 fe->ops.release_sec(fe);
2165 if (fe->ops.tuner_ops.release)
2166 fe->ops.tuner_ops.release(fe);
2167 if (fe->ops.analog_ops.release)
2168 fe->ops.analog_ops.release(fe);
2169 if (fe->ops.release)
2170 fe->ops.release(fe);
2172 #endif
2173 EXPORT_SYMBOL(dvb_frontend_detach);