usb: Netlogic: Use CPU_XLR in place of NLM_XLR
[zen-stable.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
blob2c0acdb4d81144b00b1381742505d18f208addb2
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 ((status & FE_HAS_LOCK) && fe->ops.get_frontend)
153 fe->ops.get_frontend(fe, &fepriv->parameters_out);
155 mutex_lock(&events->mtx);
157 wp = (events->eventw + 1) % MAX_EVENT;
158 if (wp == events->eventr) {
159 events->overflow = 1;
160 events->eventr = (events->eventr + 1) % MAX_EVENT;
163 e = &events->events[events->eventw];
164 e->status = status;
165 e->parameters = fepriv->parameters_out;
167 events->eventw = wp;
169 mutex_unlock(&events->mtx);
171 wake_up_interruptible (&events->wait_queue);
174 static int dvb_frontend_get_event(struct dvb_frontend *fe,
175 struct dvb_frontend_event *event, int flags)
177 struct dvb_frontend_private *fepriv = fe->frontend_priv;
178 struct dvb_fe_events *events = &fepriv->events;
180 dprintk ("%s\n", __func__);
182 if (events->overflow) {
183 events->overflow = 0;
184 return -EOVERFLOW;
187 if (events->eventw == events->eventr) {
188 int ret;
190 if (flags & O_NONBLOCK)
191 return -EWOULDBLOCK;
193 up(&fepriv->sem);
195 ret = wait_event_interruptible (events->wait_queue,
196 events->eventw != events->eventr);
198 if (down_interruptible (&fepriv->sem))
199 return -ERESTARTSYS;
201 if (ret < 0)
202 return ret;
205 mutex_lock(&events->mtx);
206 *event = events->events[events->eventr];
207 events->eventr = (events->eventr + 1) % MAX_EVENT;
208 mutex_unlock(&events->mtx);
210 return 0;
213 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
215 struct dvb_frontend_private *fepriv = fe->frontend_priv;
216 struct dvb_fe_events *events = &fepriv->events;
218 mutex_lock(&events->mtx);
219 events->eventr = events->eventw;
220 mutex_unlock(&events->mtx);
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 fe_status_t s;
541 enum dvbfe_algo algo;
543 struct dvb_frontend_parameters *params;
545 dprintk("%s\n", __func__);
547 fepriv->check_wrapped = 0;
548 fepriv->quality = 0;
549 fepriv->delay = 3*HZ;
550 fepriv->status = 0;
551 fepriv->wakeup = 0;
552 fepriv->reinitialise = 0;
554 dvb_frontend_init(fe);
556 set_freezable();
557 while (1) {
558 up(&fepriv->sem); /* is locked when we enter the thread... */
559 restart:
560 wait_event_interruptible_timeout(fepriv->wait_queue,
561 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
562 || freezing(current),
563 fepriv->delay);
565 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
566 /* got signal or quitting */
567 fepriv->exit = DVB_FE_NORMAL_EXIT;
568 break;
571 if (try_to_freeze())
572 goto restart;
574 if (down_interruptible(&fepriv->sem))
575 break;
577 if (fepriv->reinitialise) {
578 dvb_frontend_init(fe);
579 if (fe->ops.set_tone && fepriv->tone != -1)
580 fe->ops.set_tone(fe, fepriv->tone);
581 if (fe->ops.set_voltage && fepriv->voltage != -1)
582 fe->ops.set_voltage(fe, fepriv->voltage);
583 fepriv->reinitialise = 0;
586 /* do an iteration of the tuning loop */
587 if (fe->ops.get_frontend_algo) {
588 algo = fe->ops.get_frontend_algo(fe);
589 switch (algo) {
590 case DVBFE_ALGO_HW:
591 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
592 params = NULL; /* have we been asked to RETUNE ? */
594 if (fepriv->state & FESTATE_RETUNE) {
595 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
596 params = &fepriv->parameters_in;
597 fepriv->state = FESTATE_TUNED;
600 if (fe->ops.tune)
601 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
602 if (params)
603 fepriv->parameters_out = *params;
605 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
606 dprintk("%s: state changed, adding current state\n", __func__);
607 dvb_frontend_add_event(fe, s);
608 fepriv->status = s;
610 break;
611 case DVBFE_ALGO_SW:
612 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
613 dvb_frontend_swzigzag(fe);
614 break;
615 case DVBFE_ALGO_CUSTOM:
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 fepriv->state = FESTATE_TUNED;
621 /* Case where we are going to search for a carrier
622 * User asked us to retune again for some reason, possibly
623 * requesting a search with a new set of parameters
625 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
626 if (fe->ops.search) {
627 fepriv->algo_status = fe->ops.search(fe, &fepriv->parameters_in);
628 /* We did do a search as was requested, the flags are
629 * now unset as well and has the flags wrt to search.
631 } else {
632 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
635 /* Track the carrier if the search was successful */
636 if (fepriv->algo_status == DVBFE_ALGO_SEARCH_SUCCESS) {
637 if (fe->ops.track)
638 fe->ops.track(fe, &fepriv->parameters_in);
639 } else {
640 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
641 fepriv->delay = HZ / 2;
643 fepriv->parameters_out = fepriv->parameters_in;
644 fe->ops.read_status(fe, &s);
645 if (s != fepriv->status) {
646 dvb_frontend_add_event(fe, s); /* update event list */
647 fepriv->status = s;
648 if (!(s & FE_HAS_LOCK)) {
649 fepriv->delay = HZ / 10;
650 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
651 } else {
652 fepriv->delay = 60 * HZ;
655 break;
656 default:
657 dprintk("%s: UNDEFINED ALGO !\n", __func__);
658 break;
660 } else {
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);
675 if (fe->ops.sleep)
676 fe->ops.sleep(fe);
679 fepriv->thread = NULL;
680 if (kthread_should_stop())
681 fepriv->exit = DVB_FE_DEVICE_REMOVED;
682 else
683 fepriv->exit = DVB_FE_NO_EXIT;
684 mb();
686 dvb_frontend_wakeup(fe);
687 return 0;
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;
697 mb();
699 if (!fepriv->thread)
700 return;
702 kthread_stop(fepriv->thread);
704 sema_init(&fepriv->sem, 1);
705 fepriv->state = FESTATE_IDLE;
707 /* paranoia check in case a signal arrived */
708 if (fepriv->thread)
709 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
710 fepriv->thread);
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;
726 curtime->tv_sec++;
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;
739 s32 delta, newdelta;
741 timeval_usec_add(waketime, add_usec);
743 do_gettimeofday(&lasttime);
744 delta = timeval_usec_diff(lasttime, *waketime);
745 if (delta > 2500) {
746 msleep((delta - 1500) / 1000);
747 do_gettimeofday(&lasttime);
748 newdelta = timeval_usec_diff(lasttime, *waketime);
749 delta = (newdelta > delta) ? 0 : newdelta;
751 if (delta > 0)
752 udelay(delta);
754 EXPORT_SYMBOL(dvb_frontend_sleep_until);
756 static int dvb_frontend_start(struct dvb_frontend *fe)
758 int ret;
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)
766 return 0;
767 else
768 dvb_frontend_stop (fe);
771 if (signal_pending(current))
772 return -EINTR;
773 if (down_interruptible (&fepriv->sem))
774 return -EINTR;
776 fepriv->state = FESTATE_IDLE;
777 fepriv->exit = DVB_FE_NO_EXIT;
778 fepriv->thread = NULL;
779 mb();
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);
786 up(&fepriv->sem);
787 return ret;
789 fepriv->thread = fe_thread;
790 return 0;
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;
802 else
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)
813 u32 freq_min;
814 u32 freq_max;
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);
822 return -EINVAL;
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);
834 return -EINVAL;
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);
845 return -EINVAL;
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);
855 return -EINVAL;
858 return 0;
861 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
863 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
864 int i;
866 memset(c, 0, sizeof(struct dtv_frontend_properties));
868 c->state = DTV_CLEAR;
869 c->delivery_system = SYS_UNDEFINED;
870 c->inversion = INVERSION_AUTO;
871 c->fec_inner = FEC_AUTO;
872 c->transmission_mode = TRANSMISSION_MODE_AUTO;
873 c->bandwidth_hz = BANDWIDTH_AUTO;
874 c->guard_interval = GUARD_INTERVAL_AUTO;
875 c->hierarchy = HIERARCHY_AUTO;
876 c->symbol_rate = QAM_AUTO;
877 c->code_rate_HP = FEC_AUTO;
878 c->code_rate_LP = FEC_AUTO;
880 c->isdbt_partial_reception = -1;
881 c->isdbt_sb_mode = -1;
882 c->isdbt_sb_subchannel = -1;
883 c->isdbt_sb_segment_idx = -1;
884 c->isdbt_sb_segment_count = -1;
885 c->isdbt_layer_enabled = 0x7;
886 for (i = 0; i < 3; i++) {
887 c->layer[i].fec = FEC_AUTO;
888 c->layer[i].modulation = QAM_AUTO;
889 c->layer[i].interleaving = -1;
890 c->layer[i].segment_count = -1;
893 return 0;
896 #define _DTV_CMD(n, s, b) \
897 [n] = { \
898 .name = #n, \
899 .cmd = n, \
900 .set = s,\
901 .buffer = b \
904 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
905 _DTV_CMD(DTV_TUNE, 1, 0),
906 _DTV_CMD(DTV_CLEAR, 1, 0),
908 /* Set */
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),
966 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
968 /* Get */
969 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
970 _DTV_CMD(DTV_API_VERSION, 0, 0),
971 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
972 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
973 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
974 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
975 _DTV_CMD(DTV_HIERARCHY, 0, 0),
978 static void dtv_property_dump(struct dtv_property *tvp)
980 int i;
982 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
983 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
984 __func__, tvp->cmd);
985 return;
988 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
989 ,__func__
990 ,tvp->cmd
991 ,dtv_cmds[ tvp->cmd ].name);
993 if(dtv_cmds[ tvp->cmd ].buffer) {
995 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
996 ,__func__
997 ,tvp->u.buffer.len);
999 for(i = 0; i < tvp->u.buffer.len; i++)
1000 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1001 ,__func__
1003 ,tvp->u.buffer.data[i]);
1005 } else
1006 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1009 static int is_legacy_delivery_system(fe_delivery_system_t s)
1011 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
1012 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS) ||
1013 (s == SYS_ATSC))
1014 return 1;
1016 return 0;
1019 /* Initialize the cache with some default values derived from the
1020 * legacy frontend_info structure.
1022 static void dtv_property_cache_init(struct dvb_frontend *fe,
1023 struct dtv_frontend_properties *c)
1025 switch (fe->ops.info.type) {
1026 case FE_QPSK:
1027 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1028 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1029 c->delivery_system = SYS_DVBS;
1030 break;
1031 case FE_QAM:
1032 c->delivery_system = SYS_DVBC_ANNEX_AC;
1033 break;
1034 case FE_OFDM:
1035 c->delivery_system = SYS_DVBT;
1036 break;
1037 case FE_ATSC:
1038 break;
1042 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1043 * drivers can use a single set_frontend tuning function, regardless of whether
1044 * it's being used for the legacy or new API, reducing code and complexity.
1046 static void dtv_property_cache_sync(struct dvb_frontend *fe,
1047 struct dtv_frontend_properties *c,
1048 const struct dvb_frontend_parameters *p)
1050 c->frequency = p->frequency;
1051 c->inversion = p->inversion;
1053 switch (fe->ops.info.type) {
1054 case FE_QPSK:
1055 c->symbol_rate = p->u.qpsk.symbol_rate;
1056 c->fec_inner = p->u.qpsk.fec_inner;
1057 break;
1058 case FE_QAM:
1059 c->symbol_rate = p->u.qam.symbol_rate;
1060 c->fec_inner = p->u.qam.fec_inner;
1061 c->modulation = p->u.qam.modulation;
1062 break;
1063 case FE_OFDM:
1064 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
1065 c->bandwidth_hz = 6000000;
1066 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
1067 c->bandwidth_hz = 7000000;
1068 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
1069 c->bandwidth_hz = 8000000;
1070 else
1071 /* Including BANDWIDTH_AUTO */
1072 c->bandwidth_hz = 0;
1073 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1074 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1075 c->modulation = p->u.ofdm.constellation;
1076 c->transmission_mode = p->u.ofdm.transmission_mode;
1077 c->guard_interval = p->u.ofdm.guard_interval;
1078 c->hierarchy = p->u.ofdm.hierarchy_information;
1079 break;
1080 case FE_ATSC:
1081 c->modulation = p->u.vsb.modulation;
1082 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1083 c->delivery_system = SYS_ATSC;
1084 else
1085 c->delivery_system = SYS_DVBC_ANNEX_B;
1086 break;
1090 /* Ensure the cached values are set correctly in the frontend
1091 * legacy tuning structures, for the advanced tuning API.
1093 static void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1095 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1096 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1097 struct dvb_frontend_parameters *p = &fepriv->parameters_in;
1099 p->frequency = c->frequency;
1100 p->inversion = c->inversion;
1102 switch (fe->ops.info.type) {
1103 case FE_QPSK:
1104 dprintk("%s() Preparing QPSK req\n", __func__);
1105 p->u.qpsk.symbol_rate = c->symbol_rate;
1106 p->u.qpsk.fec_inner = c->fec_inner;
1107 break;
1108 case FE_QAM:
1109 dprintk("%s() Preparing QAM req\n", __func__);
1110 p->u.qam.symbol_rate = c->symbol_rate;
1111 p->u.qam.fec_inner = c->fec_inner;
1112 p->u.qam.modulation = c->modulation;
1113 break;
1114 case FE_OFDM:
1115 dprintk("%s() Preparing OFDM req\n", __func__);
1116 if (c->bandwidth_hz == 6000000)
1117 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1118 else if (c->bandwidth_hz == 7000000)
1119 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1120 else if (c->bandwidth_hz == 8000000)
1121 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1122 else
1123 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1124 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1125 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1126 p->u.ofdm.constellation = c->modulation;
1127 p->u.ofdm.transmission_mode = c->transmission_mode;
1128 p->u.ofdm.guard_interval = c->guard_interval;
1129 p->u.ofdm.hierarchy_information = c->hierarchy;
1130 break;
1131 case FE_ATSC:
1132 dprintk("%s() Preparing VSB req\n", __func__);
1133 p->u.vsb.modulation = c->modulation;
1134 break;
1138 /* Ensure the cached values are set correctly in the frontend
1139 * legacy tuning structures, for the legacy tuning API.
1141 static void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1143 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1144 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1145 struct dvb_frontend_parameters *p = &fepriv->parameters_in;
1147 p->frequency = c->frequency;
1148 p->inversion = c->inversion;
1150 if (c->delivery_system == SYS_DSS ||
1151 c->delivery_system == SYS_DVBS ||
1152 c->delivery_system == SYS_DVBS2 ||
1153 c->delivery_system == SYS_ISDBS ||
1154 c->delivery_system == SYS_TURBO) {
1155 p->u.qpsk.symbol_rate = c->symbol_rate;
1156 p->u.qpsk.fec_inner = c->fec_inner;
1159 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1160 if ((c->delivery_system == SYS_ISDBT) ||
1161 (c->delivery_system == SYS_DVBT2)) {
1162 p->u.ofdm.constellation = QAM_AUTO;
1163 p->u.ofdm.code_rate_HP = FEC_AUTO;
1164 p->u.ofdm.code_rate_LP = FEC_AUTO;
1165 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1166 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1167 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1168 if (c->bandwidth_hz == 8000000)
1169 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1170 else if (c->bandwidth_hz == 7000000)
1171 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1172 else if (c->bandwidth_hz == 6000000)
1173 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1174 else
1175 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1179 static void dtv_property_cache_submit(struct dvb_frontend *fe)
1181 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1183 /* For legacy delivery systems we don't need the delivery_system to
1184 * be specified, but we populate the older structures from the cache
1185 * so we can call set_frontend on older drivers.
1187 if(is_legacy_delivery_system(c->delivery_system)) {
1189 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1190 dtv_property_legacy_params_sync(fe);
1192 } else {
1193 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1195 /* For advanced delivery systems / modulation types ...
1196 * we seed the lecacy dvb_frontend_parameters structure
1197 * so that the sanity checking code later in the IOCTL processing
1198 * can validate our basic frequency ranges, symbolrates, modulation
1199 * etc.
1201 dtv_property_adv_params_sync(fe);
1205 static int dvb_frontend_ioctl_legacy(struct file *file,
1206 unsigned int cmd, void *parg);
1207 static int dvb_frontend_ioctl_properties(struct file *file,
1208 unsigned int cmd, void *parg);
1210 static int dtv_property_process_get(struct dvb_frontend *fe,
1211 struct dtv_property *tvp,
1212 struct file *file)
1214 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1215 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1216 struct dtv_frontend_properties cdetected;
1217 int r;
1220 * If the driver implements a get_frontend function, then convert
1221 * detected parameters to S2API properties.
1223 if (fe->ops.get_frontend) {
1224 cdetected = *c;
1225 dtv_property_cache_sync(fe, &cdetected, &fepriv->parameters_out);
1226 c = &cdetected;
1229 switch(tvp->cmd) {
1230 case DTV_FREQUENCY:
1231 tvp->u.data = c->frequency;
1232 break;
1233 case DTV_MODULATION:
1234 tvp->u.data = c->modulation;
1235 break;
1236 case DTV_BANDWIDTH_HZ:
1237 tvp->u.data = c->bandwidth_hz;
1238 break;
1239 case DTV_INVERSION:
1240 tvp->u.data = c->inversion;
1241 break;
1242 case DTV_SYMBOL_RATE:
1243 tvp->u.data = c->symbol_rate;
1244 break;
1245 case DTV_INNER_FEC:
1246 tvp->u.data = c->fec_inner;
1247 break;
1248 case DTV_PILOT:
1249 tvp->u.data = c->pilot;
1250 break;
1251 case DTV_ROLLOFF:
1252 tvp->u.data = c->rolloff;
1253 break;
1254 case DTV_DELIVERY_SYSTEM:
1255 tvp->u.data = c->delivery_system;
1256 break;
1257 case DTV_VOLTAGE:
1258 tvp->u.data = c->voltage;
1259 break;
1260 case DTV_TONE:
1261 tvp->u.data = c->sectone;
1262 break;
1263 case DTV_API_VERSION:
1264 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1265 break;
1266 case DTV_CODE_RATE_HP:
1267 tvp->u.data = c->code_rate_HP;
1268 break;
1269 case DTV_CODE_RATE_LP:
1270 tvp->u.data = c->code_rate_LP;
1271 break;
1272 case DTV_GUARD_INTERVAL:
1273 tvp->u.data = c->guard_interval;
1274 break;
1275 case DTV_TRANSMISSION_MODE:
1276 tvp->u.data = c->transmission_mode;
1277 break;
1278 case DTV_HIERARCHY:
1279 tvp->u.data = c->hierarchy;
1280 break;
1282 /* ISDB-T Support here */
1283 case DTV_ISDBT_PARTIAL_RECEPTION:
1284 tvp->u.data = c->isdbt_partial_reception;
1285 break;
1286 case DTV_ISDBT_SOUND_BROADCASTING:
1287 tvp->u.data = c->isdbt_sb_mode;
1288 break;
1289 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1290 tvp->u.data = c->isdbt_sb_subchannel;
1291 break;
1292 case DTV_ISDBT_SB_SEGMENT_IDX:
1293 tvp->u.data = c->isdbt_sb_segment_idx;
1294 break;
1295 case DTV_ISDBT_SB_SEGMENT_COUNT:
1296 tvp->u.data = c->isdbt_sb_segment_count;
1297 break;
1298 case DTV_ISDBT_LAYER_ENABLED:
1299 tvp->u.data = c->isdbt_layer_enabled;
1300 break;
1301 case DTV_ISDBT_LAYERA_FEC:
1302 tvp->u.data = c->layer[0].fec;
1303 break;
1304 case DTV_ISDBT_LAYERA_MODULATION:
1305 tvp->u.data = c->layer[0].modulation;
1306 break;
1307 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1308 tvp->u.data = c->layer[0].segment_count;
1309 break;
1310 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1311 tvp->u.data = c->layer[0].interleaving;
1312 break;
1313 case DTV_ISDBT_LAYERB_FEC:
1314 tvp->u.data = c->layer[1].fec;
1315 break;
1316 case DTV_ISDBT_LAYERB_MODULATION:
1317 tvp->u.data = c->layer[1].modulation;
1318 break;
1319 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1320 tvp->u.data = c->layer[1].segment_count;
1321 break;
1322 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1323 tvp->u.data = c->layer[1].interleaving;
1324 break;
1325 case DTV_ISDBT_LAYERC_FEC:
1326 tvp->u.data = c->layer[2].fec;
1327 break;
1328 case DTV_ISDBT_LAYERC_MODULATION:
1329 tvp->u.data = c->layer[2].modulation;
1330 break;
1331 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1332 tvp->u.data = c->layer[2].segment_count;
1333 break;
1334 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1335 tvp->u.data = c->layer[2].interleaving;
1336 break;
1337 case DTV_ISDBS_TS_ID:
1338 tvp->u.data = c->isdbs_ts_id;
1339 break;
1340 case DTV_DVBT2_PLP_ID:
1341 tvp->u.data = c->dvbt2_plp_id;
1342 break;
1343 default:
1344 return -EINVAL;
1347 /* Allow the frontend to override outgoing properties */
1348 if (fe->ops.get_property) {
1349 r = fe->ops.get_property(fe, tvp);
1350 if (r < 0)
1351 return r;
1354 dtv_property_dump(tvp);
1356 return 0;
1359 static int dtv_property_process_set(struct dvb_frontend *fe,
1360 struct dtv_property *tvp,
1361 struct file *file)
1363 int r = 0;
1364 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1365 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1366 dtv_property_dump(tvp);
1368 /* Allow the frontend to validate incoming properties */
1369 if (fe->ops.set_property) {
1370 r = fe->ops.set_property(fe, tvp);
1371 if (r < 0)
1372 return r;
1375 switch(tvp->cmd) {
1376 case DTV_CLEAR:
1377 /* Reset a cache of data specific to the frontend here. This does
1378 * not effect hardware.
1380 dvb_frontend_clear_cache(fe);
1381 dprintk("%s() Flushing property cache\n", __func__);
1382 break;
1383 case DTV_TUNE:
1384 /* interpret the cache of data, build either a traditional frontend
1385 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1386 * ioctl.
1388 c->state = tvp->cmd;
1389 dprintk("%s() Finalised property cache\n", __func__);
1390 dtv_property_cache_submit(fe);
1392 r = dvb_frontend_ioctl_legacy(file, FE_SET_FRONTEND,
1393 &fepriv->parameters_in);
1394 break;
1395 case DTV_FREQUENCY:
1396 c->frequency = tvp->u.data;
1397 break;
1398 case DTV_MODULATION:
1399 c->modulation = tvp->u.data;
1400 break;
1401 case DTV_BANDWIDTH_HZ:
1402 c->bandwidth_hz = tvp->u.data;
1403 break;
1404 case DTV_INVERSION:
1405 c->inversion = tvp->u.data;
1406 break;
1407 case DTV_SYMBOL_RATE:
1408 c->symbol_rate = tvp->u.data;
1409 break;
1410 case DTV_INNER_FEC:
1411 c->fec_inner = tvp->u.data;
1412 break;
1413 case DTV_PILOT:
1414 c->pilot = tvp->u.data;
1415 break;
1416 case DTV_ROLLOFF:
1417 c->rolloff = tvp->u.data;
1418 break;
1419 case DTV_DELIVERY_SYSTEM:
1420 c->delivery_system = tvp->u.data;
1421 break;
1422 case DTV_VOLTAGE:
1423 c->voltage = tvp->u.data;
1424 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1425 (void *)c->voltage);
1426 break;
1427 case DTV_TONE:
1428 c->sectone = tvp->u.data;
1429 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1430 (void *)c->sectone);
1431 break;
1432 case DTV_CODE_RATE_HP:
1433 c->code_rate_HP = tvp->u.data;
1434 break;
1435 case DTV_CODE_RATE_LP:
1436 c->code_rate_LP = tvp->u.data;
1437 break;
1438 case DTV_GUARD_INTERVAL:
1439 c->guard_interval = tvp->u.data;
1440 break;
1441 case DTV_TRANSMISSION_MODE:
1442 c->transmission_mode = tvp->u.data;
1443 break;
1444 case DTV_HIERARCHY:
1445 c->hierarchy = tvp->u.data;
1446 break;
1448 /* ISDB-T Support here */
1449 case DTV_ISDBT_PARTIAL_RECEPTION:
1450 c->isdbt_partial_reception = tvp->u.data;
1451 break;
1452 case DTV_ISDBT_SOUND_BROADCASTING:
1453 c->isdbt_sb_mode = tvp->u.data;
1454 break;
1455 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1456 c->isdbt_sb_subchannel = tvp->u.data;
1457 break;
1458 case DTV_ISDBT_SB_SEGMENT_IDX:
1459 c->isdbt_sb_segment_idx = tvp->u.data;
1460 break;
1461 case DTV_ISDBT_SB_SEGMENT_COUNT:
1462 c->isdbt_sb_segment_count = tvp->u.data;
1463 break;
1464 case DTV_ISDBT_LAYER_ENABLED:
1465 c->isdbt_layer_enabled = tvp->u.data;
1466 break;
1467 case DTV_ISDBT_LAYERA_FEC:
1468 c->layer[0].fec = tvp->u.data;
1469 break;
1470 case DTV_ISDBT_LAYERA_MODULATION:
1471 c->layer[0].modulation = tvp->u.data;
1472 break;
1473 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1474 c->layer[0].segment_count = tvp->u.data;
1475 break;
1476 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1477 c->layer[0].interleaving = tvp->u.data;
1478 break;
1479 case DTV_ISDBT_LAYERB_FEC:
1480 c->layer[1].fec = tvp->u.data;
1481 break;
1482 case DTV_ISDBT_LAYERB_MODULATION:
1483 c->layer[1].modulation = tvp->u.data;
1484 break;
1485 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1486 c->layer[1].segment_count = tvp->u.data;
1487 break;
1488 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1489 c->layer[1].interleaving = tvp->u.data;
1490 break;
1491 case DTV_ISDBT_LAYERC_FEC:
1492 c->layer[2].fec = tvp->u.data;
1493 break;
1494 case DTV_ISDBT_LAYERC_MODULATION:
1495 c->layer[2].modulation = tvp->u.data;
1496 break;
1497 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1498 c->layer[2].segment_count = tvp->u.data;
1499 break;
1500 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1501 c->layer[2].interleaving = tvp->u.data;
1502 break;
1503 case DTV_ISDBS_TS_ID:
1504 c->isdbs_ts_id = tvp->u.data;
1505 break;
1506 case DTV_DVBT2_PLP_ID:
1507 c->dvbt2_plp_id = tvp->u.data;
1508 break;
1509 default:
1510 return -EINVAL;
1513 return r;
1516 static int dvb_frontend_ioctl(struct file *file,
1517 unsigned int cmd, void *parg)
1519 struct dvb_device *dvbdev = file->private_data;
1520 struct dvb_frontend *fe = dvbdev->priv;
1521 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1522 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1523 int err = -EOPNOTSUPP;
1525 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1527 if (fepriv->exit != DVB_FE_NO_EXIT)
1528 return -ENODEV;
1530 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1531 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1532 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1533 return -EPERM;
1535 if (down_interruptible (&fepriv->sem))
1536 return -ERESTARTSYS;
1538 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1539 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1540 else {
1541 c->state = DTV_UNDEFINED;
1542 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1545 up(&fepriv->sem);
1546 return err;
1549 static int dvb_frontend_ioctl_properties(struct file *file,
1550 unsigned int cmd, void *parg)
1552 struct dvb_device *dvbdev = file->private_data;
1553 struct dvb_frontend *fe = dvbdev->priv;
1554 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1555 int err = 0;
1557 struct dtv_properties *tvps = NULL;
1558 struct dtv_property *tvp = NULL;
1559 int i;
1561 dprintk("%s\n", __func__);
1563 if(cmd == FE_SET_PROPERTY) {
1564 tvps = (struct dtv_properties __user *)parg;
1566 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1567 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1569 /* Put an arbitrary limit on the number of messages that can
1570 * be sent at once */
1571 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1572 return -EINVAL;
1574 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1575 if (!tvp) {
1576 err = -ENOMEM;
1577 goto out;
1580 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1581 err = -EFAULT;
1582 goto out;
1585 for (i = 0; i < tvps->num; i++) {
1586 err = dtv_property_process_set(fe, tvp + i, file);
1587 if (err < 0)
1588 goto out;
1589 (tvp + i)->result = err;
1592 if (c->state == DTV_TUNE)
1593 dprintk("%s() Property cache is full, tuning\n", __func__);
1595 } else
1596 if(cmd == FE_GET_PROPERTY) {
1598 tvps = (struct dtv_properties __user *)parg;
1600 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1601 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1603 /* Put an arbitrary limit on the number of messages that can
1604 * be sent at once */
1605 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1606 return -EINVAL;
1608 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1609 if (!tvp) {
1610 err = -ENOMEM;
1611 goto out;
1614 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1615 err = -EFAULT;
1616 goto out;
1619 for (i = 0; i < tvps->num; i++) {
1620 err = dtv_property_process_get(fe, tvp + i, file);
1621 if (err < 0)
1622 goto out;
1623 (tvp + i)->result = err;
1626 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1627 err = -EFAULT;
1628 goto out;
1631 } else
1632 err = -EOPNOTSUPP;
1634 out:
1635 kfree(tvp);
1636 return err;
1639 static int dvb_frontend_ioctl_legacy(struct file *file,
1640 unsigned int cmd, void *parg)
1642 struct dvb_device *dvbdev = file->private_data;
1643 struct dvb_frontend *fe = dvbdev->priv;
1644 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1645 int cb_err, err = -EOPNOTSUPP;
1647 if (fe->dvb->fe_ioctl_override) {
1648 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1649 DVB_FE_IOCTL_PRE);
1650 if (cb_err < 0)
1651 return cb_err;
1652 if (cb_err > 0)
1653 return 0;
1654 /* fe_ioctl_override returning 0 allows
1655 * dvb-core to continue handling the ioctl */
1658 switch (cmd) {
1659 case FE_GET_INFO: {
1660 struct dvb_frontend_info* info = parg;
1661 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1662 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1664 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1665 * do it, it is done for it. */
1666 info->caps |= FE_CAN_INVERSION_AUTO;
1667 err = 0;
1668 break;
1671 case FE_READ_STATUS: {
1672 fe_status_t* status = parg;
1674 /* if retune was requested but hasn't occurred yet, prevent
1675 * that user get signal state from previous tuning */
1676 if (fepriv->state == FESTATE_RETUNE ||
1677 fepriv->state == FESTATE_ERROR) {
1678 err=0;
1679 *status = 0;
1680 break;
1683 if (fe->ops.read_status)
1684 err = fe->ops.read_status(fe, status);
1685 break;
1687 case FE_READ_BER:
1688 if (fe->ops.read_ber)
1689 err = fe->ops.read_ber(fe, (__u32*) parg);
1690 break;
1692 case FE_READ_SIGNAL_STRENGTH:
1693 if (fe->ops.read_signal_strength)
1694 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1695 break;
1697 case FE_READ_SNR:
1698 if (fe->ops.read_snr)
1699 err = fe->ops.read_snr(fe, (__u16*) parg);
1700 break;
1702 case FE_READ_UNCORRECTED_BLOCKS:
1703 if (fe->ops.read_ucblocks)
1704 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1705 break;
1708 case FE_DISEQC_RESET_OVERLOAD:
1709 if (fe->ops.diseqc_reset_overload) {
1710 err = fe->ops.diseqc_reset_overload(fe);
1711 fepriv->state = FESTATE_DISEQC;
1712 fepriv->status = 0;
1714 break;
1716 case FE_DISEQC_SEND_MASTER_CMD:
1717 if (fe->ops.diseqc_send_master_cmd) {
1718 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1719 fepriv->state = FESTATE_DISEQC;
1720 fepriv->status = 0;
1722 break;
1724 case FE_DISEQC_SEND_BURST:
1725 if (fe->ops.diseqc_send_burst) {
1726 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1727 fepriv->state = FESTATE_DISEQC;
1728 fepriv->status = 0;
1730 break;
1732 case FE_SET_TONE:
1733 if (fe->ops.set_tone) {
1734 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1735 fepriv->tone = (fe_sec_tone_mode_t) parg;
1736 fepriv->state = FESTATE_DISEQC;
1737 fepriv->status = 0;
1739 break;
1741 case FE_SET_VOLTAGE:
1742 if (fe->ops.set_voltage) {
1743 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1744 fepriv->voltage = (fe_sec_voltage_t) parg;
1745 fepriv->state = FESTATE_DISEQC;
1746 fepriv->status = 0;
1748 break;
1750 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1751 if (fe->ops.dishnetwork_send_legacy_command) {
1752 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1753 fepriv->state = FESTATE_DISEQC;
1754 fepriv->status = 0;
1755 } else if (fe->ops.set_voltage) {
1757 * NOTE: This is a fallback condition. Some frontends
1758 * (stv0299 for instance) take longer than 8msec to
1759 * respond to a set_voltage command. Those switches
1760 * need custom routines to switch properly. For all
1761 * other frontends, the following should work ok.
1762 * Dish network legacy switches (as used by Dish500)
1763 * are controlled by sending 9-bit command words
1764 * spaced 8msec apart.
1765 * the actual command word is switch/port dependent
1766 * so it is up to the userspace application to send
1767 * the right command.
1768 * The command must always start with a '0' after
1769 * initialization, so parg is 8 bits and does not
1770 * include the initialization or start bit
1772 unsigned long swcmd = ((unsigned long) parg) << 1;
1773 struct timeval nexttime;
1774 struct timeval tv[10];
1775 int i;
1776 u8 last = 1;
1777 if (dvb_frontend_debug)
1778 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1779 do_gettimeofday(&nexttime);
1780 if (dvb_frontend_debug)
1781 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1782 /* before sending a command, initialize by sending
1783 * a 32ms 18V to the switch
1785 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1786 dvb_frontend_sleep_until(&nexttime, 32000);
1788 for (i = 0; i < 9; i++) {
1789 if (dvb_frontend_debug)
1790 do_gettimeofday(&tv[i + 1]);
1791 if ((swcmd & 0x01) != last) {
1792 /* set voltage to (last ? 13V : 18V) */
1793 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1794 last = (last) ? 0 : 1;
1796 swcmd = swcmd >> 1;
1797 if (i != 8)
1798 dvb_frontend_sleep_until(&nexttime, 8000);
1800 if (dvb_frontend_debug) {
1801 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1802 __func__, fe->dvb->num);
1803 for (i = 1; i < 10; i++)
1804 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1806 err = 0;
1807 fepriv->state = FESTATE_DISEQC;
1808 fepriv->status = 0;
1810 break;
1812 case FE_DISEQC_RECV_SLAVE_REPLY:
1813 if (fe->ops.diseqc_recv_slave_reply)
1814 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1815 break;
1817 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1818 if (fe->ops.enable_high_lnb_voltage)
1819 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1820 break;
1822 case FE_SET_FRONTEND: {
1823 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1824 struct dvb_frontend_tune_settings fetunesettings;
1826 if (c->state == DTV_TUNE) {
1827 if (dvb_frontend_check_parameters(fe, &fepriv->parameters_in) < 0) {
1828 err = -EINVAL;
1829 break;
1831 } else {
1832 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1833 err = -EINVAL;
1834 break;
1837 memcpy (&fepriv->parameters_in, parg,
1838 sizeof (struct dvb_frontend_parameters));
1839 dtv_property_cache_init(fe, c);
1840 dtv_property_cache_sync(fe, c, &fepriv->parameters_in);
1844 * Initialize output parameters to match the values given by
1845 * the user. FE_SET_FRONTEND triggers an initial frontend event
1846 * with status = 0, which copies output parameters to userspace.
1848 fepriv->parameters_out = fepriv->parameters_in;
1850 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1851 memcpy(&fetunesettings.parameters, parg,
1852 sizeof (struct dvb_frontend_parameters));
1854 /* force auto frequency inversion if requested */
1855 if (dvb_force_auto_inversion) {
1856 fepriv->parameters_in.inversion = INVERSION_AUTO;
1857 fetunesettings.parameters.inversion = INVERSION_AUTO;
1859 if (fe->ops.info.type == FE_OFDM) {
1860 /* without hierarchical coding code_rate_LP is irrelevant,
1861 * so we tolerate the otherwise invalid FEC_NONE setting */
1862 if (fepriv->parameters_in.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1863 fepriv->parameters_in.u.ofdm.code_rate_LP == FEC_NONE)
1864 fepriv->parameters_in.u.ofdm.code_rate_LP = FEC_AUTO;
1867 /* get frontend-specific tuning settings */
1868 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1869 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1870 fepriv->max_drift = fetunesettings.max_drift;
1871 fepriv->step_size = fetunesettings.step_size;
1872 } else {
1873 /* default values */
1874 switch(fe->ops.info.type) {
1875 case FE_QPSK:
1876 fepriv->min_delay = HZ/20;
1877 fepriv->step_size = fepriv->parameters_in.u.qpsk.symbol_rate / 16000;
1878 fepriv->max_drift = fepriv->parameters_in.u.qpsk.symbol_rate / 2000;
1879 break;
1881 case FE_QAM:
1882 fepriv->min_delay = HZ/20;
1883 fepriv->step_size = 0; /* no zigzag */
1884 fepriv->max_drift = 0;
1885 break;
1887 case FE_OFDM:
1888 fepriv->min_delay = HZ/20;
1889 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1890 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1891 break;
1892 case FE_ATSC:
1893 fepriv->min_delay = HZ/20;
1894 fepriv->step_size = 0;
1895 fepriv->max_drift = 0;
1896 break;
1899 if (dvb_override_tune_delay > 0)
1900 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1902 fepriv->state = FESTATE_RETUNE;
1904 /* Request the search algorithm to search */
1905 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1907 dvb_frontend_clear_events(fe);
1908 dvb_frontend_add_event(fe, 0);
1909 dvb_frontend_wakeup(fe);
1910 fepriv->status = 0;
1911 err = 0;
1912 break;
1915 case FE_GET_EVENT:
1916 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1917 break;
1919 case FE_GET_FRONTEND:
1920 if (fe->ops.get_frontend) {
1921 err = fe->ops.get_frontend(fe, &fepriv->parameters_out);
1922 memcpy(parg, &fepriv->parameters_out, sizeof(struct dvb_frontend_parameters));
1924 break;
1926 case FE_SET_FRONTEND_TUNE_MODE:
1927 fepriv->tune_mode_flags = (unsigned long) parg;
1928 err = 0;
1929 break;
1932 if (fe->dvb->fe_ioctl_override) {
1933 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1934 DVB_FE_IOCTL_POST);
1935 if (cb_err < 0)
1936 return cb_err;
1939 return err;
1943 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1945 struct dvb_device *dvbdev = file->private_data;
1946 struct dvb_frontend *fe = dvbdev->priv;
1947 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1949 dprintk ("%s\n", __func__);
1951 poll_wait (file, &fepriv->events.wait_queue, wait);
1953 if (fepriv->events.eventw != fepriv->events.eventr)
1954 return (POLLIN | POLLRDNORM | POLLPRI);
1956 return 0;
1959 static int dvb_frontend_open(struct inode *inode, struct file *file)
1961 struct dvb_device *dvbdev = file->private_data;
1962 struct dvb_frontend *fe = dvbdev->priv;
1963 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1964 struct dvb_adapter *adapter = fe->dvb;
1965 int ret;
1967 dprintk ("%s\n", __func__);
1968 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
1969 return -ENODEV;
1971 if (adapter->mfe_shared) {
1972 mutex_lock (&adapter->mfe_lock);
1974 if (adapter->mfe_dvbdev == NULL)
1975 adapter->mfe_dvbdev = dvbdev;
1977 else if (adapter->mfe_dvbdev != dvbdev) {
1978 struct dvb_device
1979 *mfedev = adapter->mfe_dvbdev;
1980 struct dvb_frontend
1981 *mfe = mfedev->priv;
1982 struct dvb_frontend_private
1983 *mfepriv = mfe->frontend_priv;
1984 int mferetry = (dvb_mfe_wait_time << 1);
1986 mutex_unlock (&adapter->mfe_lock);
1987 while (mferetry-- && (mfedev->users != -1 ||
1988 mfepriv->thread != NULL)) {
1989 if(msleep_interruptible(500)) {
1990 if(signal_pending(current))
1991 return -EINTR;
1995 mutex_lock (&adapter->mfe_lock);
1996 if(adapter->mfe_dvbdev != dvbdev) {
1997 mfedev = adapter->mfe_dvbdev;
1998 mfe = mfedev->priv;
1999 mfepriv = mfe->frontend_priv;
2000 if (mfedev->users != -1 ||
2001 mfepriv->thread != NULL) {
2002 mutex_unlock (&adapter->mfe_lock);
2003 return -EBUSY;
2005 adapter->mfe_dvbdev = dvbdev;
2010 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2011 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2012 goto err0;
2014 /* If we took control of the bus, we need to force
2015 reinitialization. This is because many ts_bus_ctrl()
2016 functions strobe the RESET pin on the demod, and if the
2017 frontend thread already exists then the dvb_init() routine
2018 won't get called (which is what usually does initial
2019 register configuration). */
2020 fepriv->reinitialise = 1;
2023 if ((ret = dvb_generic_open (inode, file)) < 0)
2024 goto err1;
2026 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2027 /* normal tune mode when opened R/W */
2028 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2029 fepriv->tone = -1;
2030 fepriv->voltage = -1;
2032 ret = dvb_frontend_start (fe);
2033 if (ret)
2034 goto err2;
2036 /* empty event queue */
2037 fepriv->events.eventr = fepriv->events.eventw = 0;
2040 if (adapter->mfe_shared)
2041 mutex_unlock (&adapter->mfe_lock);
2042 return ret;
2044 err2:
2045 dvb_generic_release(inode, file);
2046 err1:
2047 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2048 fe->ops.ts_bus_ctrl(fe, 0);
2049 err0:
2050 if (adapter->mfe_shared)
2051 mutex_unlock (&adapter->mfe_lock);
2052 return ret;
2055 static int dvb_frontend_release(struct inode *inode, struct file *file)
2057 struct dvb_device *dvbdev = file->private_data;
2058 struct dvb_frontend *fe = dvbdev->priv;
2059 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2060 int ret;
2062 dprintk ("%s\n", __func__);
2064 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
2065 fepriv->release_jiffies = jiffies;
2067 ret = dvb_generic_release (inode, file);
2069 if (dvbdev->users == -1) {
2070 if (fepriv->exit != DVB_FE_NO_EXIT) {
2071 fops_put(file->f_op);
2072 file->f_op = NULL;
2073 wake_up(&dvbdev->wait_queue);
2075 if (fe->ops.ts_bus_ctrl)
2076 fe->ops.ts_bus_ctrl(fe, 0);
2079 return ret;
2082 static const struct file_operations dvb_frontend_fops = {
2083 .owner = THIS_MODULE,
2084 .unlocked_ioctl = dvb_generic_ioctl,
2085 .poll = dvb_frontend_poll,
2086 .open = dvb_frontend_open,
2087 .release = dvb_frontend_release,
2088 .llseek = noop_llseek,
2091 int dvb_register_frontend(struct dvb_adapter* dvb,
2092 struct dvb_frontend* fe)
2094 struct dvb_frontend_private *fepriv;
2095 static const struct dvb_device dvbdev_template = {
2096 .users = ~0,
2097 .writers = 1,
2098 .readers = (~0)-1,
2099 .fops = &dvb_frontend_fops,
2100 .kernel_ioctl = dvb_frontend_ioctl
2103 dprintk ("%s\n", __func__);
2105 if (mutex_lock_interruptible(&frontend_mutex))
2106 return -ERESTARTSYS;
2108 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2109 if (fe->frontend_priv == NULL) {
2110 mutex_unlock(&frontend_mutex);
2111 return -ENOMEM;
2113 fepriv = fe->frontend_priv;
2115 sema_init(&fepriv->sem, 1);
2116 init_waitqueue_head (&fepriv->wait_queue);
2117 init_waitqueue_head (&fepriv->events.wait_queue);
2118 mutex_init(&fepriv->events.mtx);
2119 fe->dvb = dvb;
2120 fepriv->inversion = INVERSION_OFF;
2122 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2123 fe->dvb->num,
2124 fe->id,
2125 fe->ops.info.name);
2127 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2128 fe, DVB_DEVICE_FRONTEND);
2130 mutex_unlock(&frontend_mutex);
2131 return 0;
2133 EXPORT_SYMBOL(dvb_register_frontend);
2135 int dvb_unregister_frontend(struct dvb_frontend* fe)
2137 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2138 dprintk ("%s\n", __func__);
2140 mutex_lock(&frontend_mutex);
2141 dvb_frontend_stop (fe);
2142 mutex_unlock(&frontend_mutex);
2144 if (fepriv->dvbdev->users < -1)
2145 wait_event(fepriv->dvbdev->wait_queue,
2146 fepriv->dvbdev->users==-1);
2148 mutex_lock(&frontend_mutex);
2149 dvb_unregister_device (fepriv->dvbdev);
2151 /* fe is invalid now */
2152 kfree(fepriv);
2153 mutex_unlock(&frontend_mutex);
2154 return 0;
2156 EXPORT_SYMBOL(dvb_unregister_frontend);
2158 #ifdef CONFIG_MEDIA_ATTACH
2159 void dvb_frontend_detach(struct dvb_frontend* fe)
2161 void *ptr;
2163 if (fe->ops.release_sec) {
2164 fe->ops.release_sec(fe);
2165 symbol_put_addr(fe->ops.release_sec);
2167 if (fe->ops.tuner_ops.release) {
2168 fe->ops.tuner_ops.release(fe);
2169 symbol_put_addr(fe->ops.tuner_ops.release);
2171 if (fe->ops.analog_ops.release) {
2172 fe->ops.analog_ops.release(fe);
2173 symbol_put_addr(fe->ops.analog_ops.release);
2175 ptr = (void*)fe->ops.release;
2176 if (ptr) {
2177 fe->ops.release(fe);
2178 symbol_put_addr(ptr);
2181 #else
2182 void dvb_frontend_detach(struct dvb_frontend* fe)
2184 if (fe->ops.release_sec)
2185 fe->ops.release_sec(fe);
2186 if (fe->ops.tuner_ops.release)
2187 fe->ops.tuner_ops.release(fe);
2188 if (fe->ops.analog_ops.release)
2189 fe->ops.analog_ops.release(fe);
2190 if (fe->ops.release)
2191 fe->ops.release(fe);
2193 #endif
2194 EXPORT_SYMBOL(dvb_frontend_detach);