First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
blobddf639ed2fd8de2d59c4ddfc663bcceb46f95ee2
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/module.h>
35 #include <linux/list.h>
36 #include <linux/freezer.h>
37 #include <linux/jiffies.h>
38 #include <linux/kthread.h>
39 #include <asm/processor.h>
41 #include "dvb_frontend.h"
42 #include "dvbdev.h"
43 #include <linux/dvb/version.h>
45 static int dvb_frontend_debug;
46 static int dvb_shutdown_timeout;
47 static int dvb_force_auto_inversion;
48 static int dvb_override_tune_delay;
49 static int dvb_powerdown_on_sleep = 1;
50 static int dvb_mfe_wait_time = 5;
52 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
53 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
54 module_param(dvb_shutdown_timeout, int, 0644);
55 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
56 module_param(dvb_force_auto_inversion, int, 0644);
57 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
58 module_param(dvb_override_tune_delay, int, 0644);
59 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
60 module_param(dvb_powerdown_on_sleep, int, 0644);
61 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
62 module_param(dvb_mfe_wait_time, int, 0644);
63 MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
65 #define dprintk if (dvb_frontend_debug) printk
67 #define FESTATE_IDLE 1
68 #define FESTATE_RETUNE 2
69 #define FESTATE_TUNING_FAST 4
70 #define FESTATE_TUNING_SLOW 8
71 #define FESTATE_TUNED 16
72 #define FESTATE_ZIGZAG_FAST 32
73 #define FESTATE_ZIGZAG_SLOW 64
74 #define FESTATE_DISEQC 128
75 #define FESTATE_ERROR 256
76 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
77 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
78 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
79 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
81 #define FE_ALGO_HW 1
83 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
84 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
85 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
86 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
87 * FESTATE_TUNED. The frontend has successfully locked on.
88 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
89 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
90 * FESTATE_DISEQC. A DISEQC command has just been issued.
91 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
92 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
93 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
94 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
97 static DEFINE_MUTEX(frontend_mutex);
99 struct dvb_frontend_private {
101 /* thread/frontend values */
102 struct dvb_device *dvbdev;
103 struct dvb_frontend_parameters parameters;
104 struct dvb_fe_events events;
105 struct semaphore sem;
106 struct list_head list_head;
107 wait_queue_head_t wait_queue;
108 struct task_struct *thread;
109 unsigned long release_jiffies;
110 unsigned int exit;
111 unsigned int wakeup;
112 fe_status_t status;
113 unsigned long tune_mode_flags;
114 unsigned int delay;
115 unsigned int reinitialise;
116 int tone;
117 int voltage;
119 /* swzigzag values */
120 unsigned int state;
121 unsigned int bending;
122 int lnb_drift;
123 unsigned int inversion;
124 unsigned int auto_step;
125 unsigned int auto_sub_step;
126 unsigned int started_auto_step;
127 unsigned int min_delay;
128 unsigned int max_drift;
129 unsigned int step_size;
130 int quality;
131 unsigned int check_wrapped;
132 enum dvbfe_search algo_status;
135 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
137 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
139 struct dvb_frontend_private *fepriv = fe->frontend_priv;
140 struct dvb_fe_events *events = &fepriv->events;
141 struct dvb_frontend_event *e;
142 int wp;
144 dprintk ("%s\n", __func__);
146 if (mutex_lock_interruptible (&events->mtx))
147 return;
149 wp = (events->eventw + 1) % MAX_EVENT;
151 if (wp == events->eventr) {
152 events->overflow = 1;
153 events->eventr = (events->eventr + 1) % MAX_EVENT;
156 e = &events->events[events->eventw];
158 memcpy (&e->parameters, &fepriv->parameters,
159 sizeof (struct dvb_frontend_parameters));
161 if (status & FE_HAS_LOCK)
162 if (fe->ops.get_frontend)
163 fe->ops.get_frontend(fe, &e->parameters);
165 events->eventw = wp;
167 mutex_unlock(&events->mtx);
169 e->status = status;
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 if (mutex_lock_interruptible (&events->mtx))
206 return -ERESTARTSYS;
208 memcpy (event, &events->events[events->eventr],
209 sizeof(struct dvb_frontend_event));
211 events->eventr = (events->eventr + 1) % MAX_EVENT;
213 mutex_unlock(&events->mtx);
215 return 0;
218 static void dvb_frontend_init(struct dvb_frontend *fe)
220 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
221 fe->dvb->num,
222 fe->id,
223 fe->ops.info.name);
225 if (fe->ops.init)
226 fe->ops.init(fe);
227 if (fe->ops.tuner_ops.init) {
228 if (fe->ops.i2c_gate_ctrl)
229 fe->ops.i2c_gate_ctrl(fe, 1);
230 fe->ops.tuner_ops.init(fe);
231 if (fe->ops.i2c_gate_ctrl)
232 fe->ops.i2c_gate_ctrl(fe, 0);
236 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
238 struct dvb_frontend_private *fepriv = fe->frontend_priv;
240 fepriv->reinitialise = 1;
241 dvb_frontend_wakeup(fe);
243 EXPORT_SYMBOL(dvb_frontend_reinitialise);
245 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
247 int q2;
249 dprintk ("%s\n", __func__);
251 if (locked)
252 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
253 else
254 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
256 q2 = fepriv->quality - 128;
257 q2 *= q2;
259 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
263 * Performs automatic twiddling of frontend parameters.
265 * @param fe The frontend concerned.
266 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
267 * @returns Number of complete iterations that have been performed.
269 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
271 int autoinversion;
272 int ready = 0;
273 int fe_set_err = 0;
274 struct dvb_frontend_private *fepriv = fe->frontend_priv;
275 int original_inversion = fepriv->parameters.inversion;
276 u32 original_frequency = fepriv->parameters.frequency;
278 /* are we using autoinversion? */
279 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
280 (fepriv->parameters.inversion == INVERSION_AUTO));
282 /* setup parameters correctly */
283 while(!ready) {
284 /* calculate the lnb_drift */
285 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
287 /* wrap the auto_step if we've exceeded the maximum drift */
288 if (fepriv->lnb_drift > fepriv->max_drift) {
289 fepriv->auto_step = 0;
290 fepriv->auto_sub_step = 0;
291 fepriv->lnb_drift = 0;
294 /* perform inversion and +/- zigzag */
295 switch(fepriv->auto_sub_step) {
296 case 0:
297 /* try with the current inversion and current drift setting */
298 ready = 1;
299 break;
301 case 1:
302 if (!autoinversion) break;
304 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
305 ready = 1;
306 break;
308 case 2:
309 if (fepriv->lnb_drift == 0) break;
311 fepriv->lnb_drift = -fepriv->lnb_drift;
312 ready = 1;
313 break;
315 case 3:
316 if (fepriv->lnb_drift == 0) break;
317 if (!autoinversion) break;
319 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
320 fepriv->lnb_drift = -fepriv->lnb_drift;
321 ready = 1;
322 break;
324 default:
325 fepriv->auto_step++;
326 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
327 break;
330 if (!ready) fepriv->auto_sub_step++;
333 /* if this attempt would hit where we started, indicate a complete
334 * iteration has occurred */
335 if ((fepriv->auto_step == fepriv->started_auto_step) &&
336 (fepriv->auto_sub_step == 0) && check_wrapped) {
337 return 1;
340 dprintk("%s: drift:%i inversion:%i auto_step:%i "
341 "auto_sub_step:%i started_auto_step:%i\n",
342 __func__, fepriv->lnb_drift, fepriv->inversion,
343 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
345 /* set the frontend itself */
346 fepriv->parameters.frequency += fepriv->lnb_drift;
347 if (autoinversion)
348 fepriv->parameters.inversion = fepriv->inversion;
349 if (fe->ops.set_frontend)
350 fe_set_err = fe->ops.set_frontend(fe, &fepriv->parameters);
351 if (fe_set_err < 0) {
352 fepriv->state = FESTATE_ERROR;
353 return fe_set_err;
356 fepriv->parameters.frequency = original_frequency;
357 fepriv->parameters.inversion = original_inversion;
359 fepriv->auto_sub_step++;
360 return 0;
363 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
365 fe_status_t s = 0;
366 int retval = 0;
367 struct dvb_frontend_private *fepriv = fe->frontend_priv;
369 /* if we've got no parameters, just keep idling */
370 if (fepriv->state & FESTATE_IDLE) {
371 fepriv->delay = 3*HZ;
372 fepriv->quality = 0;
373 return;
376 /* in SCAN mode, we just set the frontend when asked and leave it alone */
377 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
378 if (fepriv->state & FESTATE_RETUNE) {
379 if (fe->ops.set_frontend)
380 retval = fe->ops.set_frontend(fe,
381 &fepriv->parameters);
382 if (retval < 0)
383 fepriv->state = FESTATE_ERROR;
384 else
385 fepriv->state = FESTATE_TUNED;
387 fepriv->delay = 3*HZ;
388 fepriv->quality = 0;
389 return;
392 /* get the frontend status */
393 if (fepriv->state & FESTATE_RETUNE) {
394 s = 0;
395 } else {
396 if (fe->ops.read_status)
397 fe->ops.read_status(fe, &s);
398 if (s != fepriv->status) {
399 dvb_frontend_add_event(fe, s);
400 fepriv->status = s;
404 /* if we're not tuned, and we have a lock, move to the TUNED state */
405 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
406 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
407 fepriv->state = FESTATE_TUNED;
409 /* if we're tuned, then we have determined the correct inversion */
410 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
411 (fepriv->parameters.inversion == INVERSION_AUTO)) {
412 fepriv->parameters.inversion = fepriv->inversion;
414 return;
417 /* if we are tuned already, check we're still locked */
418 if (fepriv->state & FESTATE_TUNED) {
419 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
421 /* we're tuned, and the lock is still good... */
422 if (s & FE_HAS_LOCK) {
423 return;
424 } else { /* if we _WERE_ tuned, but now don't have a lock */
425 fepriv->state = FESTATE_ZIGZAG_FAST;
426 fepriv->started_auto_step = fepriv->auto_step;
427 fepriv->check_wrapped = 0;
431 /* don't actually do anything if we're in the LOSTLOCK state,
432 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
433 if ((fepriv->state & FESTATE_LOSTLOCK) &&
434 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
435 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
436 return;
439 /* don't do anything if we're in the DISEQC state, since this
440 * might be someone with a motorized dish controlled by DISEQC.
441 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
442 if (fepriv->state & FESTATE_DISEQC) {
443 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
444 return;
447 /* if we're in the RETUNE state, set everything up for a brand
448 * new scan, keeping the current inversion setting, as the next
449 * tune is _very_ likely to require the same */
450 if (fepriv->state & FESTATE_RETUNE) {
451 fepriv->lnb_drift = 0;
452 fepriv->auto_step = 0;
453 fepriv->auto_sub_step = 0;
454 fepriv->started_auto_step = 0;
455 fepriv->check_wrapped = 0;
458 /* fast zigzag. */
459 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
460 fepriv->delay = fepriv->min_delay;
462 /* peform a tune */
463 retval = dvb_frontend_swzigzag_autotune(fe,
464 fepriv->check_wrapped);
465 if (retval < 0) {
466 return;
467 } else if (retval) {
468 /* OK, if we've run out of trials at the fast speed.
469 * Drop back to slow for the _next_ attempt */
470 fepriv->state = FESTATE_SEARCHING_SLOW;
471 fepriv->started_auto_step = fepriv->auto_step;
472 return;
474 fepriv->check_wrapped = 1;
476 /* if we've just retuned, enter the ZIGZAG_FAST state.
477 * This ensures we cannot return from an
478 * FE_SET_FRONTEND ioctl before the first frontend tune
479 * occurs */
480 if (fepriv->state & FESTATE_RETUNE) {
481 fepriv->state = FESTATE_TUNING_FAST;
485 /* slow zigzag */
486 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
487 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
489 /* Note: don't bother checking for wrapping; we stay in this
490 * state until we get a lock */
491 dvb_frontend_swzigzag_autotune(fe, 0);
495 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
497 struct dvb_frontend_private *fepriv = fe->frontend_priv;
499 if (fepriv->exit)
500 return 1;
502 if (fepriv->dvbdev->writers == 1)
503 if (time_after(jiffies, fepriv->release_jiffies +
504 dvb_shutdown_timeout * HZ))
505 return 1;
507 return 0;
510 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
512 struct dvb_frontend_private *fepriv = fe->frontend_priv;
514 if (fepriv->wakeup) {
515 fepriv->wakeup = 0;
516 return 1;
518 return dvb_frontend_is_exiting(fe);
521 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
523 struct dvb_frontend_private *fepriv = fe->frontend_priv;
525 fepriv->wakeup = 1;
526 wake_up_interruptible(&fepriv->wait_queue);
529 static int dvb_frontend_thread(void *data)
531 struct dvb_frontend *fe = data;
532 struct dvb_frontend_private *fepriv = fe->frontend_priv;
533 unsigned long timeout;
534 fe_status_t s;
535 enum dvbfe_algo algo;
537 struct dvb_frontend_parameters *params;
539 dprintk("%s\n", __func__);
541 fepriv->check_wrapped = 0;
542 fepriv->quality = 0;
543 fepriv->delay = 3*HZ;
544 fepriv->status = 0;
545 fepriv->wakeup = 0;
546 fepriv->reinitialise = 0;
548 dvb_frontend_init(fe);
550 set_freezable();
551 while (1) {
552 up(&fepriv->sem); /* is locked when we enter the thread... */
553 restart:
554 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
555 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
556 || freezing(current),
557 fepriv->delay);
559 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
560 /* got signal or quitting */
561 fepriv->exit = 1;
562 break;
565 if (try_to_freeze())
566 goto restart;
568 if (down_interruptible(&fepriv->sem))
569 break;
571 if (fepriv->reinitialise) {
572 dvb_frontend_init(fe);
573 if (fepriv->tone != -1) {
574 fe->ops.set_tone(fe, fepriv->tone);
576 if (fepriv->voltage != -1) {
577 fe->ops.set_voltage(fe, fepriv->voltage);
579 fepriv->reinitialise = 0;
582 /* do an iteration of the tuning loop */
583 if (fe->ops.get_frontend_algo) {
584 algo = fe->ops.get_frontend_algo(fe);
585 switch (algo) {
586 case DVBFE_ALGO_HW:
587 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
588 params = NULL; /* have we been asked to RETUNE ? */
590 if (fepriv->state & FESTATE_RETUNE) {
591 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
592 params = &fepriv->parameters;
593 fepriv->state = FESTATE_TUNED;
596 if (fe->ops.tune)
597 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
599 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
600 dprintk("%s: state changed, adding current state\n", __func__);
601 dvb_frontend_add_event(fe, s);
602 fepriv->status = s;
604 break;
605 case DVBFE_ALGO_SW:
606 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
607 dvb_frontend_swzigzag(fe);
608 break;
609 case DVBFE_ALGO_CUSTOM:
610 params = NULL; /* have we been asked to RETUNE ? */
611 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
612 if (fepriv->state & FESTATE_RETUNE) {
613 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
614 params = &fepriv->parameters;
615 fepriv->state = FESTATE_TUNED;
617 /* Case where we are going to search for a carrier
618 * User asked us to retune again for some reason, possibly
619 * requesting a search with a new set of parameters
621 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
622 if (fe->ops.search) {
623 fepriv->algo_status = fe->ops.search(fe, &fepriv->parameters);
624 /* We did do a search as was requested, the flags are
625 * now unset as well and has the flags wrt to search.
627 } else {
628 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
631 /* Track the carrier if the search was successful */
632 if (fepriv->algo_status == DVBFE_ALGO_SEARCH_SUCCESS) {
633 if (fe->ops.track)
634 fe->ops.track(fe, &fepriv->parameters);
635 } else {
636 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
637 fepriv->delay = HZ / 2;
639 fe->ops.read_status(fe, &s);
640 if (s != fepriv->status) {
641 dvb_frontend_add_event(fe, s); /* update event list */
642 fepriv->status = s;
643 if (!(s & FE_HAS_LOCK)) {
644 fepriv->delay = HZ / 10;
645 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
646 } else {
647 fepriv->delay = 60 * HZ;
650 break;
651 default:
652 dprintk("%s: UNDEFINED ALGO !\n", __func__);
653 break;
655 } else {
656 dvb_frontend_swzigzag(fe);
660 if (dvb_powerdown_on_sleep) {
661 if (fe->ops.set_voltage)
662 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
663 if (fe->ops.tuner_ops.sleep) {
664 if (fe->ops.i2c_gate_ctrl)
665 fe->ops.i2c_gate_ctrl(fe, 1);
666 fe->ops.tuner_ops.sleep(fe);
667 if (fe->ops.i2c_gate_ctrl)
668 fe->ops.i2c_gate_ctrl(fe, 0);
670 if (fe->ops.sleep)
671 fe->ops.sleep(fe);
674 fepriv->thread = NULL;
675 fepriv->exit = 0;
676 mb();
678 dvb_frontend_wakeup(fe);
679 return 0;
682 static void dvb_frontend_stop(struct dvb_frontend *fe)
684 struct dvb_frontend_private *fepriv = fe->frontend_priv;
686 dprintk ("%s\n", __func__);
688 fepriv->exit = 1;
689 mb();
691 if (!fepriv->thread)
692 return;
694 kthread_stop(fepriv->thread);
696 init_MUTEX (&fepriv->sem);
697 fepriv->state = FESTATE_IDLE;
699 /* paranoia check in case a signal arrived */
700 if (fepriv->thread)
701 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
702 fepriv->thread);
705 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
707 return ((curtime.tv_usec < lasttime.tv_usec) ?
708 1000000 - lasttime.tv_usec + curtime.tv_usec :
709 curtime.tv_usec - lasttime.tv_usec);
711 EXPORT_SYMBOL(timeval_usec_diff);
713 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
715 curtime->tv_usec += add_usec;
716 if (curtime->tv_usec >= 1000000) {
717 curtime->tv_usec -= 1000000;
718 curtime->tv_sec++;
723 * Sleep until gettimeofday() > waketime + add_usec
724 * This needs to be as precise as possible, but as the delay is
725 * usually between 2ms and 32ms, it is done using a scheduled msleep
726 * followed by usleep (normally a busy-wait loop) for the remainder
728 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
730 struct timeval lasttime;
731 s32 delta, newdelta;
733 timeval_usec_add(waketime, add_usec);
735 do_gettimeofday(&lasttime);
736 delta = timeval_usec_diff(lasttime, *waketime);
737 if (delta > 2500) {
738 msleep((delta - 1500) / 1000);
739 do_gettimeofday(&lasttime);
740 newdelta = timeval_usec_diff(lasttime, *waketime);
741 delta = (newdelta > delta) ? 0 : newdelta;
743 if (delta > 0)
744 udelay(delta);
746 EXPORT_SYMBOL(dvb_frontend_sleep_until);
748 static int dvb_frontend_start(struct dvb_frontend *fe)
750 int ret;
751 struct dvb_frontend_private *fepriv = fe->frontend_priv;
752 struct task_struct *fe_thread;
754 dprintk ("%s\n", __func__);
756 if (fepriv->thread) {
757 if (!fepriv->exit)
758 return 0;
759 else
760 dvb_frontend_stop (fe);
763 if (signal_pending(current))
764 return -EINTR;
765 if (down_interruptible (&fepriv->sem))
766 return -EINTR;
768 fepriv->state = FESTATE_IDLE;
769 fepriv->exit = 0;
770 fepriv->thread = NULL;
771 mb();
773 fe_thread = kthread_run(dvb_frontend_thread, fe,
774 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
775 if (IS_ERR(fe_thread)) {
776 ret = PTR_ERR(fe_thread);
777 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
778 up(&fepriv->sem);
779 return ret;
781 fepriv->thread = fe_thread;
782 return 0;
785 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
786 u32 *freq_min, u32 *freq_max)
788 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
790 if (fe->ops.info.frequency_max == 0)
791 *freq_max = fe->ops.tuner_ops.info.frequency_max;
792 else if (fe->ops.tuner_ops.info.frequency_max == 0)
793 *freq_max = fe->ops.info.frequency_max;
794 else
795 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
797 if (*freq_min == 0 || *freq_max == 0)
798 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
799 fe->dvb->num,fe->id);
802 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
803 struct dvb_frontend_parameters *parms)
805 u32 freq_min;
806 u32 freq_max;
808 /* range check: frequency */
809 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
810 if ((freq_min && parms->frequency < freq_min) ||
811 (freq_max && parms->frequency > freq_max)) {
812 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
813 fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
814 return -EINVAL;
817 /* range check: symbol rate */
818 if (fe->ops.info.type == FE_QPSK) {
819 if ((fe->ops.info.symbol_rate_min &&
820 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
821 (fe->ops.info.symbol_rate_max &&
822 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
823 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
824 fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
825 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
826 return -EINVAL;
829 } else if (fe->ops.info.type == FE_QAM) {
830 if ((fe->ops.info.symbol_rate_min &&
831 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
832 (fe->ops.info.symbol_rate_max &&
833 parms->u.qam.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.qam.symbol_rate,
836 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
837 return -EINVAL;
841 /* check for supported modulation */
842 if (fe->ops.info.type == FE_QAM &&
843 (parms->u.qam.modulation > QAM_AUTO ||
844 !((1 << (parms->u.qam.modulation + 10)) & fe->ops.info.caps))) {
845 printk(KERN_WARNING "DVB: adapter %i frontend %i modulation %u not supported\n",
846 fe->dvb->num, fe->id, parms->u.qam.modulation);
847 return -EINVAL;
850 return 0;
853 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
855 int i;
857 memset(&(fe->dtv_property_cache), 0,
858 sizeof(struct dtv_frontend_properties));
860 fe->dtv_property_cache.state = DTV_CLEAR;
861 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
862 fe->dtv_property_cache.inversion = INVERSION_AUTO;
863 fe->dtv_property_cache.fec_inner = FEC_AUTO;
864 fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
865 fe->dtv_property_cache.bandwidth_hz = BANDWIDTH_AUTO;
866 fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
867 fe->dtv_property_cache.hierarchy = HIERARCHY_AUTO;
868 fe->dtv_property_cache.symbol_rate = QAM_AUTO;
869 fe->dtv_property_cache.code_rate_HP = FEC_AUTO;
870 fe->dtv_property_cache.code_rate_LP = FEC_AUTO;
872 fe->dtv_property_cache.isdbt_partial_reception = -1;
873 fe->dtv_property_cache.isdbt_sb_mode = -1;
874 fe->dtv_property_cache.isdbt_sb_subchannel = -1;
875 fe->dtv_property_cache.isdbt_sb_segment_idx = -1;
876 fe->dtv_property_cache.isdbt_sb_segment_count = -1;
877 fe->dtv_property_cache.isdbt_layer_enabled = 0x7;
878 for (i = 0; i < 3; i++) {
879 fe->dtv_property_cache.layer[i].fec = FEC_AUTO;
880 fe->dtv_property_cache.layer[i].modulation = QAM_AUTO;
881 fe->dtv_property_cache.layer[i].interleaving = -1;
882 fe->dtv_property_cache.layer[i].segment_count = -1;
885 return 0;
888 #define _DTV_CMD(n, s, b) \
889 [n] = { \
890 .name = #n, \
891 .cmd = n, \
892 .set = s,\
893 .buffer = b \
896 static struct dtv_cmds_h dtv_cmds[] = {
897 [DTV_TUNE] = {
898 .name = "DTV_TUNE",
899 .cmd = DTV_TUNE,
900 .set = 1,
902 [DTV_CLEAR] = {
903 .name = "DTV_CLEAR",
904 .cmd = DTV_CLEAR,
905 .set = 1,
908 /* Set */
909 [DTV_FREQUENCY] = {
910 .name = "DTV_FREQUENCY",
911 .cmd = DTV_FREQUENCY,
912 .set = 1,
914 [DTV_BANDWIDTH_HZ] = {
915 .name = "DTV_BANDWIDTH_HZ",
916 .cmd = DTV_BANDWIDTH_HZ,
917 .set = 1,
919 [DTV_MODULATION] = {
920 .name = "DTV_MODULATION",
921 .cmd = DTV_MODULATION,
922 .set = 1,
924 [DTV_INVERSION] = {
925 .name = "DTV_INVERSION",
926 .cmd = DTV_INVERSION,
927 .set = 1,
929 [DTV_DISEQC_MASTER] = {
930 .name = "DTV_DISEQC_MASTER",
931 .cmd = DTV_DISEQC_MASTER,
932 .set = 1,
933 .buffer = 1,
935 [DTV_SYMBOL_RATE] = {
936 .name = "DTV_SYMBOL_RATE",
937 .cmd = DTV_SYMBOL_RATE,
938 .set = 1,
940 [DTV_INNER_FEC] = {
941 .name = "DTV_INNER_FEC",
942 .cmd = DTV_INNER_FEC,
943 .set = 1,
945 [DTV_VOLTAGE] = {
946 .name = "DTV_VOLTAGE",
947 .cmd = DTV_VOLTAGE,
948 .set = 1,
950 [DTV_TONE] = {
951 .name = "DTV_TONE",
952 .cmd = DTV_TONE,
953 .set = 1,
955 [DTV_PILOT] = {
956 .name = "DTV_PILOT",
957 .cmd = DTV_PILOT,
958 .set = 1,
960 [DTV_ROLLOFF] = {
961 .name = "DTV_ROLLOFF",
962 .cmd = DTV_ROLLOFF,
963 .set = 1,
965 [DTV_DELIVERY_SYSTEM] = {
966 .name = "DTV_DELIVERY_SYSTEM",
967 .cmd = DTV_DELIVERY_SYSTEM,
968 .set = 1,
970 [DTV_HIERARCHY] = {
971 .name = "DTV_HIERARCHY",
972 .cmd = DTV_HIERARCHY,
973 .set = 1,
975 [DTV_CODE_RATE_HP] = {
976 .name = "DTV_CODE_RATE_HP",
977 .cmd = DTV_CODE_RATE_HP,
978 .set = 1,
980 [DTV_CODE_RATE_LP] = {
981 .name = "DTV_CODE_RATE_LP",
982 .cmd = DTV_CODE_RATE_LP,
983 .set = 1,
985 [DTV_GUARD_INTERVAL] = {
986 .name = "DTV_GUARD_INTERVAL",
987 .cmd = DTV_GUARD_INTERVAL,
988 .set = 1,
990 [DTV_TRANSMISSION_MODE] = {
991 .name = "DTV_TRANSMISSION_MODE",
992 .cmd = DTV_TRANSMISSION_MODE,
993 .set = 1,
996 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
997 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
998 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
999 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1000 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1001 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1002 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1003 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1004 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1005 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1006 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1007 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 0, 0),
1016 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 0, 0),
1017 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 0, 0),
1018 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 0, 0),
1019 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 0, 0),
1020 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 0, 0),
1021 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 0, 0),
1022 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 0, 0),
1023 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 0, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 0, 0),
1025 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 0, 0),
1026 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 0, 0),
1027 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 0, 0),
1028 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 0, 0),
1029 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 0, 0),
1030 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 0, 0),
1031 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 0, 0),
1032 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 0, 0),
1034 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
1036 /* Get */
1037 [DTV_DISEQC_SLAVE_REPLY] = {
1038 .name = "DTV_DISEQC_SLAVE_REPLY",
1039 .cmd = DTV_DISEQC_SLAVE_REPLY,
1040 .set = 0,
1041 .buffer = 1,
1044 [DTV_API_VERSION] = {
1045 .name = "DTV_API_VERSION",
1046 .cmd = DTV_API_VERSION,
1047 .set = 0,
1049 [DTV_CODE_RATE_HP] = {
1050 .name = "DTV_CODE_RATE_HP",
1051 .cmd = DTV_CODE_RATE_HP,
1052 .set = 0,
1054 [DTV_CODE_RATE_LP] = {
1055 .name = "DTV_CODE_RATE_LP",
1056 .cmd = DTV_CODE_RATE_LP,
1057 .set = 0,
1059 [DTV_GUARD_INTERVAL] = {
1060 .name = "DTV_GUARD_INTERVAL",
1061 .cmd = DTV_GUARD_INTERVAL,
1062 .set = 0,
1064 [DTV_TRANSMISSION_MODE] = {
1065 .name = "DTV_TRANSMISSION_MODE",
1066 .cmd = DTV_TRANSMISSION_MODE,
1067 .set = 0,
1069 [DTV_HIERARCHY] = {
1070 .name = "DTV_HIERARCHY",
1071 .cmd = DTV_HIERARCHY,
1072 .set = 0,
1076 static void dtv_property_dump(struct dtv_property *tvp)
1078 int i;
1080 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1081 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
1082 __func__, tvp->cmd);
1083 return;
1086 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1087 ,__func__
1088 ,tvp->cmd
1089 ,dtv_cmds[ tvp->cmd ].name);
1091 if(dtv_cmds[ tvp->cmd ].buffer) {
1093 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1094 ,__func__
1095 ,tvp->u.buffer.len);
1097 for(i = 0; i < tvp->u.buffer.len; i++)
1098 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1099 ,__func__
1101 ,tvp->u.buffer.data[i]);
1103 } else
1104 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1107 static int is_legacy_delivery_system(fe_delivery_system_t s)
1109 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
1110 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS) ||
1111 (s == SYS_ATSC))
1112 return 1;
1114 return 0;
1117 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1118 * drivers can use a single set_frontend tuning function, regardless of whether
1119 * it's being used for the legacy or new API, reducing code and complexity.
1121 static void dtv_property_cache_sync(struct dvb_frontend *fe,
1122 struct dvb_frontend_parameters *p)
1124 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1126 c->frequency = p->frequency;
1127 c->inversion = p->inversion;
1129 switch (fe->ops.info.type) {
1130 case FE_QPSK:
1131 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1132 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1133 c->symbol_rate = p->u.qpsk.symbol_rate;
1134 c->fec_inner = p->u.qpsk.fec_inner;
1135 c->delivery_system = SYS_DVBS;
1136 break;
1137 case FE_QAM:
1138 c->symbol_rate = p->u.qam.symbol_rate;
1139 c->fec_inner = p->u.qam.fec_inner;
1140 c->modulation = p->u.qam.modulation;
1141 c->delivery_system = SYS_DVBC_ANNEX_AC;
1142 break;
1143 case FE_OFDM:
1144 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
1145 c->bandwidth_hz = 6000000;
1146 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
1147 c->bandwidth_hz = 7000000;
1148 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
1149 c->bandwidth_hz = 8000000;
1150 else
1151 /* Including BANDWIDTH_AUTO */
1152 c->bandwidth_hz = 0;
1153 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1154 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1155 c->modulation = p->u.ofdm.constellation;
1156 c->transmission_mode = p->u.ofdm.transmission_mode;
1157 c->guard_interval = p->u.ofdm.guard_interval;
1158 c->hierarchy = p->u.ofdm.hierarchy_information;
1159 c->delivery_system = SYS_DVBT;
1160 break;
1161 case FE_ATSC:
1162 c->modulation = p->u.vsb.modulation;
1163 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1164 c->delivery_system = SYS_ATSC;
1165 else
1166 c->delivery_system = SYS_DVBC_ANNEX_B;
1167 break;
1171 /* Ensure the cached values are set correctly in the frontend
1172 * legacy tuning structures, for the advanced tuning API.
1174 static void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
1176 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1177 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1178 struct dvb_frontend_parameters *p = &fepriv->parameters;
1180 p->frequency = c->frequency;
1181 p->inversion = c->inversion;
1183 switch (fe->ops.info.type) {
1184 case FE_QPSK:
1185 dprintk("%s() Preparing QPSK req\n", __func__);
1186 p->u.qpsk.symbol_rate = c->symbol_rate;
1187 p->u.qpsk.fec_inner = c->fec_inner;
1188 c->delivery_system = SYS_DVBS;
1189 break;
1190 case FE_QAM:
1191 dprintk("%s() Preparing QAM req\n", __func__);
1192 p->u.qam.symbol_rate = c->symbol_rate;
1193 p->u.qam.fec_inner = c->fec_inner;
1194 p->u.qam.modulation = c->modulation;
1195 c->delivery_system = SYS_DVBC_ANNEX_AC;
1196 break;
1197 case FE_OFDM:
1198 dprintk("%s() Preparing OFDM req\n", __func__);
1199 if (c->bandwidth_hz == 6000000)
1200 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1201 else if (c->bandwidth_hz == 7000000)
1202 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1203 else if (c->bandwidth_hz == 8000000)
1204 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1205 else
1206 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1207 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1208 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1209 p->u.ofdm.constellation = c->modulation;
1210 p->u.ofdm.transmission_mode = c->transmission_mode;
1211 p->u.ofdm.guard_interval = c->guard_interval;
1212 p->u.ofdm.hierarchy_information = c->hierarchy;
1213 c->delivery_system = SYS_DVBT;
1214 break;
1215 case FE_ATSC:
1216 dprintk("%s() Preparing VSB req\n", __func__);
1217 p->u.vsb.modulation = c->modulation;
1218 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1219 c->delivery_system = SYS_ATSC;
1220 else
1221 c->delivery_system = SYS_DVBC_ANNEX_B;
1222 break;
1226 /* Ensure the cached values are set correctly in the frontend
1227 * legacy tuning structures, for the legacy tuning API.
1229 static void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1231 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1232 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1233 struct dvb_frontend_parameters *p = &fepriv->parameters;
1235 p->frequency = c->frequency;
1236 p->inversion = c->inversion;
1238 switch(c->modulation) {
1239 case PSK_8:
1240 case APSK_16:
1241 case APSK_32:
1242 case QPSK:
1243 p->u.qpsk.symbol_rate = c->symbol_rate;
1244 p->u.qpsk.fec_inner = c->fec_inner;
1245 break;
1246 default:
1247 break;
1250 if(c->delivery_system == SYS_ISDBT) {
1251 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1252 p->frequency = c->frequency;
1253 p->inversion = c->inversion;
1254 p->u.ofdm.constellation = QAM_AUTO;
1255 p->u.ofdm.code_rate_HP = FEC_AUTO;
1256 p->u.ofdm.code_rate_LP = FEC_AUTO;
1257 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1258 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1259 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1260 if (c->bandwidth_hz == 8000000)
1261 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1262 else if (c->bandwidth_hz == 7000000)
1263 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1264 else if (c->bandwidth_hz == 6000000)
1265 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1266 else
1267 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1271 static void dtv_property_cache_submit(struct dvb_frontend *fe)
1273 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1275 /* For legacy delivery systems we don't need the delivery_system to
1276 * be specified, but we populate the older structures from the cache
1277 * so we can call set_frontend on older drivers.
1279 if(is_legacy_delivery_system(c->delivery_system)) {
1281 dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
1282 dtv_property_legacy_params_sync(fe);
1284 } else {
1285 dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
1287 /* For advanced delivery systems / modulation types ...
1288 * we seed the lecacy dvb_frontend_parameters structure
1289 * so that the sanity checking code later in the IOCTL processing
1290 * can validate our basic frequency ranges, symbolrates, modulation
1291 * etc.
1293 dtv_property_adv_params_sync(fe);
1297 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1298 unsigned int cmd, void *parg);
1299 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1300 unsigned int cmd, void *parg);
1302 static int dtv_property_process_get(struct dvb_frontend *fe,
1303 struct dtv_property *tvp,
1304 struct inode *inode, struct file *file)
1306 int r = 0;
1308 dtv_property_dump(tvp);
1310 /* Allow the frontend to validate incoming properties */
1311 if (fe->ops.get_property)
1312 r = fe->ops.get_property(fe, tvp);
1314 if (r < 0)
1315 return r;
1317 switch(tvp->cmd) {
1318 case DTV_FREQUENCY:
1319 tvp->u.data = fe->dtv_property_cache.frequency;
1320 break;
1321 case DTV_MODULATION:
1322 tvp->u.data = fe->dtv_property_cache.modulation;
1323 break;
1324 case DTV_BANDWIDTH_HZ:
1325 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1326 break;
1327 case DTV_INVERSION:
1328 tvp->u.data = fe->dtv_property_cache.inversion;
1329 break;
1330 case DTV_SYMBOL_RATE:
1331 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1332 break;
1333 case DTV_INNER_FEC:
1334 tvp->u.data = fe->dtv_property_cache.fec_inner;
1335 break;
1336 case DTV_PILOT:
1337 tvp->u.data = fe->dtv_property_cache.pilot;
1338 break;
1339 case DTV_ROLLOFF:
1340 tvp->u.data = fe->dtv_property_cache.rolloff;
1341 break;
1342 case DTV_DELIVERY_SYSTEM:
1343 tvp->u.data = fe->dtv_property_cache.delivery_system;
1344 break;
1345 case DTV_VOLTAGE:
1346 tvp->u.data = fe->dtv_property_cache.voltage;
1347 break;
1348 case DTV_TONE:
1349 tvp->u.data = fe->dtv_property_cache.sectone;
1350 break;
1351 case DTV_API_VERSION:
1352 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1353 break;
1354 case DTV_CODE_RATE_HP:
1355 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1356 break;
1357 case DTV_CODE_RATE_LP:
1358 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1359 break;
1360 case DTV_GUARD_INTERVAL:
1361 tvp->u.data = fe->dtv_property_cache.guard_interval;
1362 break;
1363 case DTV_TRANSMISSION_MODE:
1364 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1365 break;
1366 case DTV_HIERARCHY:
1367 tvp->u.data = fe->dtv_property_cache.hierarchy;
1368 break;
1370 /* ISDB-T Support here */
1371 case DTV_ISDBT_PARTIAL_RECEPTION:
1372 tvp->u.data = fe->dtv_property_cache.isdbt_partial_reception;
1373 break;
1374 case DTV_ISDBT_SOUND_BROADCASTING:
1375 tvp->u.data = fe->dtv_property_cache.isdbt_sb_mode;
1376 break;
1377 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1378 tvp->u.data = fe->dtv_property_cache.isdbt_sb_subchannel;
1379 break;
1380 case DTV_ISDBT_SB_SEGMENT_IDX:
1381 tvp->u.data = fe->dtv_property_cache.isdbt_sb_segment_idx;
1382 break;
1383 case DTV_ISDBT_SB_SEGMENT_COUNT:
1384 tvp->u.data = fe->dtv_property_cache.isdbt_sb_segment_count;
1385 break;
1386 case DTV_ISDBT_LAYER_ENABLED:
1387 tvp->u.data = fe->dtv_property_cache.isdbt_layer_enabled;
1388 break;
1389 case DTV_ISDBT_LAYERA_FEC:
1390 tvp->u.data = fe->dtv_property_cache.layer[0].fec;
1391 break;
1392 case DTV_ISDBT_LAYERA_MODULATION:
1393 tvp->u.data = fe->dtv_property_cache.layer[0].modulation;
1394 break;
1395 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1396 tvp->u.data = fe->dtv_property_cache.layer[0].segment_count;
1397 break;
1398 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1399 tvp->u.data = fe->dtv_property_cache.layer[0].interleaving;
1400 break;
1401 case DTV_ISDBT_LAYERB_FEC:
1402 tvp->u.data = fe->dtv_property_cache.layer[1].fec;
1403 break;
1404 case DTV_ISDBT_LAYERB_MODULATION:
1405 tvp->u.data = fe->dtv_property_cache.layer[1].modulation;
1406 break;
1407 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1408 tvp->u.data = fe->dtv_property_cache.layer[1].segment_count;
1409 break;
1410 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1411 tvp->u.data = fe->dtv_property_cache.layer[1].interleaving;
1412 break;
1413 case DTV_ISDBT_LAYERC_FEC:
1414 tvp->u.data = fe->dtv_property_cache.layer[2].fec;
1415 break;
1416 case DTV_ISDBT_LAYERC_MODULATION:
1417 tvp->u.data = fe->dtv_property_cache.layer[2].modulation;
1418 break;
1419 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1420 tvp->u.data = fe->dtv_property_cache.layer[2].segment_count;
1421 break;
1422 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1423 tvp->u.data = fe->dtv_property_cache.layer[2].interleaving;
1424 break;
1425 case DTV_ISDBS_TS_ID:
1426 tvp->u.data = fe->dtv_property_cache.isdbs_ts_id;
1427 break;
1428 default:
1429 r = -1;
1432 return r;
1435 static int dtv_property_process_set(struct dvb_frontend *fe,
1436 struct dtv_property *tvp,
1437 struct inode *inode,
1438 struct file *file)
1440 int r = 0;
1441 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1442 dtv_property_dump(tvp);
1444 /* Allow the frontend to validate incoming properties */
1445 if (fe->ops.set_property)
1446 r = fe->ops.set_property(fe, tvp);
1448 if (r < 0)
1449 return r;
1451 switch(tvp->cmd) {
1452 case DTV_CLEAR:
1453 /* Reset a cache of data specific to the frontend here. This does
1454 * not effect hardware.
1456 dvb_frontend_clear_cache(fe);
1457 dprintk("%s() Flushing property cache\n", __func__);
1458 break;
1459 case DTV_TUNE:
1460 /* interpret the cache of data, build either a traditional frontend
1461 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1462 * ioctl.
1464 fe->dtv_property_cache.state = tvp->cmd;
1465 dprintk("%s() Finalised property cache\n", __func__);
1466 dtv_property_cache_submit(fe);
1468 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1469 &fepriv->parameters);
1470 break;
1471 case DTV_FREQUENCY:
1472 fe->dtv_property_cache.frequency = tvp->u.data;
1473 break;
1474 case DTV_MODULATION:
1475 fe->dtv_property_cache.modulation = tvp->u.data;
1476 break;
1477 case DTV_BANDWIDTH_HZ:
1478 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1479 break;
1480 case DTV_INVERSION:
1481 fe->dtv_property_cache.inversion = tvp->u.data;
1482 break;
1483 case DTV_SYMBOL_RATE:
1484 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1485 break;
1486 case DTV_INNER_FEC:
1487 fe->dtv_property_cache.fec_inner = tvp->u.data;
1488 break;
1489 case DTV_PILOT:
1490 fe->dtv_property_cache.pilot = tvp->u.data;
1491 break;
1492 case DTV_ROLLOFF:
1493 fe->dtv_property_cache.rolloff = tvp->u.data;
1494 break;
1495 case DTV_DELIVERY_SYSTEM:
1496 fe->dtv_property_cache.delivery_system = tvp->u.data;
1497 break;
1498 case DTV_VOLTAGE:
1499 fe->dtv_property_cache.voltage = tvp->u.data;
1500 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1501 (void *)fe->dtv_property_cache.voltage);
1502 break;
1503 case DTV_TONE:
1504 fe->dtv_property_cache.sectone = tvp->u.data;
1505 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1506 (void *)fe->dtv_property_cache.sectone);
1507 break;
1508 case DTV_CODE_RATE_HP:
1509 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1510 break;
1511 case DTV_CODE_RATE_LP:
1512 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1513 break;
1514 case DTV_GUARD_INTERVAL:
1515 fe->dtv_property_cache.guard_interval = tvp->u.data;
1516 break;
1517 case DTV_TRANSMISSION_MODE:
1518 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1519 break;
1520 case DTV_HIERARCHY:
1521 fe->dtv_property_cache.hierarchy = tvp->u.data;
1522 break;
1524 /* ISDB-T Support here */
1525 case DTV_ISDBT_PARTIAL_RECEPTION:
1526 fe->dtv_property_cache.isdbt_partial_reception = tvp->u.data;
1527 break;
1528 case DTV_ISDBT_SOUND_BROADCASTING:
1529 fe->dtv_property_cache.isdbt_sb_mode = tvp->u.data;
1530 break;
1531 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1532 fe->dtv_property_cache.isdbt_sb_subchannel = tvp->u.data;
1533 break;
1534 case DTV_ISDBT_SB_SEGMENT_IDX:
1535 fe->dtv_property_cache.isdbt_sb_segment_idx = tvp->u.data;
1536 break;
1537 case DTV_ISDBT_SB_SEGMENT_COUNT:
1538 fe->dtv_property_cache.isdbt_sb_segment_count = tvp->u.data;
1539 break;
1540 case DTV_ISDBT_LAYER_ENABLED:
1541 fe->dtv_property_cache.isdbt_layer_enabled = tvp->u.data;
1542 break;
1543 case DTV_ISDBT_LAYERA_FEC:
1544 fe->dtv_property_cache.layer[0].fec = tvp->u.data;
1545 break;
1546 case DTV_ISDBT_LAYERA_MODULATION:
1547 fe->dtv_property_cache.layer[0].modulation = tvp->u.data;
1548 break;
1549 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1550 fe->dtv_property_cache.layer[0].segment_count = tvp->u.data;
1551 break;
1552 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1553 fe->dtv_property_cache.layer[0].interleaving = tvp->u.data;
1554 break;
1555 case DTV_ISDBT_LAYERB_FEC:
1556 fe->dtv_property_cache.layer[1].fec = tvp->u.data;
1557 break;
1558 case DTV_ISDBT_LAYERB_MODULATION:
1559 fe->dtv_property_cache.layer[1].modulation = tvp->u.data;
1560 break;
1561 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1562 fe->dtv_property_cache.layer[1].segment_count = tvp->u.data;
1563 break;
1564 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1565 fe->dtv_property_cache.layer[1].interleaving = tvp->u.data;
1566 break;
1567 case DTV_ISDBT_LAYERC_FEC:
1568 fe->dtv_property_cache.layer[2].fec = tvp->u.data;
1569 break;
1570 case DTV_ISDBT_LAYERC_MODULATION:
1571 fe->dtv_property_cache.layer[2].modulation = tvp->u.data;
1572 break;
1573 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1574 fe->dtv_property_cache.layer[2].segment_count = tvp->u.data;
1575 break;
1576 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1577 fe->dtv_property_cache.layer[2].interleaving = tvp->u.data;
1578 break;
1579 case DTV_ISDBS_TS_ID:
1580 fe->dtv_property_cache.isdbs_ts_id = tvp->u.data;
1581 break;
1582 default:
1583 r = -1;
1586 return r;
1589 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1590 unsigned int cmd, void *parg)
1592 struct dvb_device *dvbdev = file->private_data;
1593 struct dvb_frontend *fe = dvbdev->priv;
1594 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1595 int err = -EOPNOTSUPP;
1597 dprintk ("%s\n", __func__);
1599 if (fepriv->exit)
1600 return -ENODEV;
1602 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1603 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1604 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1605 return -EPERM;
1607 if (down_interruptible (&fepriv->sem))
1608 return -ERESTARTSYS;
1610 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1611 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1612 else {
1613 fe->dtv_property_cache.state = DTV_UNDEFINED;
1614 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1617 up(&fepriv->sem);
1618 return err;
1621 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1622 unsigned int cmd, void *parg)
1624 struct dvb_device *dvbdev = file->private_data;
1625 struct dvb_frontend *fe = dvbdev->priv;
1626 int err = 0;
1628 struct dtv_properties *tvps = NULL;
1629 struct dtv_property *tvp = NULL;
1630 int i;
1632 dprintk("%s\n", __func__);
1634 if(cmd == FE_SET_PROPERTY) {
1635 tvps = (struct dtv_properties __user *)parg;
1637 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1638 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1640 /* Put an arbitrary limit on the number of messages that can
1641 * be sent at once */
1642 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1643 return -EINVAL;
1645 tvp = (struct dtv_property *) kmalloc(tvps->num *
1646 sizeof(struct dtv_property), GFP_KERNEL);
1647 if (!tvp) {
1648 err = -ENOMEM;
1649 goto out;
1652 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1653 err = -EFAULT;
1654 goto out;
1657 for (i = 0; i < tvps->num; i++) {
1658 (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1659 err |= (tvp + i)->result;
1662 if(fe->dtv_property_cache.state == DTV_TUNE)
1663 dprintk("%s() Property cache is full, tuning\n", __func__);
1665 } else
1666 if(cmd == FE_GET_PROPERTY) {
1668 tvps = (struct dtv_properties __user *)parg;
1670 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1671 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1673 /* Put an arbitrary limit on the number of messages that can
1674 * be sent at once */
1675 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1676 return -EINVAL;
1678 tvp = (struct dtv_property *) kmalloc(tvps->num *
1679 sizeof(struct dtv_property), GFP_KERNEL);
1680 if (!tvp) {
1681 err = -ENOMEM;
1682 goto out;
1685 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1686 err = -EFAULT;
1687 goto out;
1690 for (i = 0; i < tvps->num; i++) {
1691 (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1692 err |= (tvp + i)->result;
1695 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1696 err = -EFAULT;
1697 goto out;
1700 } else
1701 err = -EOPNOTSUPP;
1703 out:
1704 kfree(tvp);
1705 return err;
1708 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1709 unsigned int cmd, void *parg)
1711 struct dvb_device *dvbdev = file->private_data;
1712 struct dvb_frontend *fe = dvbdev->priv;
1713 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1714 int err = -EOPNOTSUPP;
1716 switch (cmd) {
1717 case FE_GET_INFO: {
1718 struct dvb_frontend_info* info = parg;
1719 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1720 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1722 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1723 * do it, it is done for it. */
1724 info->caps |= FE_CAN_INVERSION_AUTO;
1725 err = 0;
1726 break;
1729 case FE_READ_STATUS: {
1730 fe_status_t* status = parg;
1732 /* if retune was requested but hasn't occured yet, prevent
1733 * that user get signal state from previous tuning */
1734 if (fepriv->state == FESTATE_RETUNE ||
1735 fepriv->state == FESTATE_ERROR) {
1736 err=0;
1737 *status = 0;
1738 break;
1741 if (fe->ops.read_status)
1742 err = fe->ops.read_status(fe, status);
1743 break;
1745 case FE_READ_BER:
1746 if (fe->ops.read_ber)
1747 err = fe->ops.read_ber(fe, (__u32*) parg);
1748 break;
1750 case FE_READ_SIGNAL_STRENGTH:
1751 if (fe->ops.read_signal_strength)
1752 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1753 break;
1755 case FE_READ_SNR:
1756 if (fe->ops.read_snr)
1757 err = fe->ops.read_snr(fe, (__u16*) parg);
1758 break;
1760 case FE_READ_UNCORRECTED_BLOCKS:
1761 if (fe->ops.read_ucblocks)
1762 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1763 break;
1766 case FE_DISEQC_RESET_OVERLOAD:
1767 if (fe->ops.diseqc_reset_overload) {
1768 err = fe->ops.diseqc_reset_overload(fe);
1769 fepriv->state = FESTATE_DISEQC;
1770 fepriv->status = 0;
1772 break;
1774 case FE_DISEQC_SEND_MASTER_CMD:
1775 if (fe->ops.diseqc_send_master_cmd) {
1776 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1777 fepriv->state = FESTATE_DISEQC;
1778 fepriv->status = 0;
1780 break;
1782 case FE_DISEQC_SEND_BURST:
1783 if (fe->ops.diseqc_send_burst) {
1784 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1785 fepriv->state = FESTATE_DISEQC;
1786 fepriv->status = 0;
1788 break;
1790 case FE_SET_TONE:
1791 if (fe->ops.set_tone) {
1792 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1793 fepriv->tone = (fe_sec_tone_mode_t) parg;
1794 fepriv->state = FESTATE_DISEQC;
1795 fepriv->status = 0;
1797 break;
1799 case FE_SET_VOLTAGE:
1800 if (fe->ops.set_voltage) {
1801 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1802 fepriv->voltage = (fe_sec_voltage_t) parg;
1803 fepriv->state = FESTATE_DISEQC;
1804 fepriv->status = 0;
1806 break;
1808 case FE_DISHNETWORK_SEND_LEGACY_CMD:
1809 if (fe->ops.dishnetwork_send_legacy_command) {
1810 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1811 fepriv->state = FESTATE_DISEQC;
1812 fepriv->status = 0;
1813 } else if (fe->ops.set_voltage) {
1815 * NOTE: This is a fallback condition. Some frontends
1816 * (stv0299 for instance) take longer than 8msec to
1817 * respond to a set_voltage command. Those switches
1818 * need custom routines to switch properly. For all
1819 * other frontends, the following shoule work ok.
1820 * Dish network legacy switches (as used by Dish500)
1821 * are controlled by sending 9-bit command words
1822 * spaced 8msec apart.
1823 * the actual command word is switch/port dependant
1824 * so it is up to the userspace application to send
1825 * the right command.
1826 * The command must always start with a '0' after
1827 * initialization, so parg is 8 bits and does not
1828 * include the initialization or start bit
1830 unsigned long swcmd = ((unsigned long) parg) << 1;
1831 struct timeval nexttime;
1832 struct timeval tv[10];
1833 int i;
1834 u8 last = 1;
1835 if (dvb_frontend_debug)
1836 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1837 do_gettimeofday(&nexttime);
1838 if (dvb_frontend_debug)
1839 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1840 /* before sending a command, initialize by sending
1841 * a 32ms 18V to the switch
1843 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1844 dvb_frontend_sleep_until(&nexttime, 32000);
1846 for (i = 0; i < 9; i++) {
1847 if (dvb_frontend_debug)
1848 do_gettimeofday(&tv[i + 1]);
1849 if ((swcmd & 0x01) != last) {
1850 /* set voltage to (last ? 13V : 18V) */
1851 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1852 last = (last) ? 0 : 1;
1854 swcmd = swcmd >> 1;
1855 if (i != 8)
1856 dvb_frontend_sleep_until(&nexttime, 8000);
1858 if (dvb_frontend_debug) {
1859 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1860 __func__, fe->dvb->num);
1861 for (i = 1; i < 10; i++)
1862 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1864 err = 0;
1865 fepriv->state = FESTATE_DISEQC;
1866 fepriv->status = 0;
1868 break;
1870 case FE_DISEQC_RECV_SLAVE_REPLY:
1871 if (fe->ops.diseqc_recv_slave_reply)
1872 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1873 break;
1875 case FE_ENABLE_HIGH_LNB_VOLTAGE:
1876 if (fe->ops.enable_high_lnb_voltage)
1877 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1878 break;
1880 case FE_SET_FRONTEND: {
1881 struct dvb_frontend_tune_settings fetunesettings;
1883 if(fe->dtv_property_cache.state == DTV_TUNE) {
1884 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1885 err = -EINVAL;
1886 break;
1888 } else {
1889 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1890 err = -EINVAL;
1891 break;
1894 memcpy (&fepriv->parameters, parg,
1895 sizeof (struct dvb_frontend_parameters));
1896 dtv_property_cache_sync(fe, &fepriv->parameters);
1899 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1900 memcpy(&fetunesettings.parameters, parg,
1901 sizeof (struct dvb_frontend_parameters));
1903 /* force auto frequency inversion if requested */
1904 if (dvb_force_auto_inversion) {
1905 fepriv->parameters.inversion = INVERSION_AUTO;
1906 fetunesettings.parameters.inversion = INVERSION_AUTO;
1908 if (fe->ops.info.type == FE_OFDM) {
1909 /* without hierarchical coding code_rate_LP is irrelevant,
1910 * so we tolerate the otherwise invalid FEC_NONE setting */
1911 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1912 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1913 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1916 /* get frontend-specific tuning settings */
1917 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1918 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1919 fepriv->max_drift = fetunesettings.max_drift;
1920 fepriv->step_size = fetunesettings.step_size;
1921 } else {
1922 /* default values */
1923 switch(fe->ops.info.type) {
1924 case FE_QPSK:
1925 fepriv->min_delay = HZ/20;
1926 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1927 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1928 break;
1930 case FE_QAM:
1931 fepriv->min_delay = HZ/20;
1932 fepriv->step_size = 0; /* no zigzag */
1933 fepriv->max_drift = 0;
1934 break;
1936 case FE_OFDM:
1937 fepriv->min_delay = HZ/20;
1938 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1939 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1940 break;
1941 case FE_ATSC:
1942 fepriv->min_delay = HZ/20;
1943 fepriv->step_size = 0;
1944 fepriv->max_drift = 0;
1945 break;
1948 if (dvb_override_tune_delay > 0)
1949 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1951 fepriv->state = FESTATE_RETUNE;
1953 /* Request the search algorithm to search */
1954 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1956 dvb_frontend_wakeup(fe);
1957 dvb_frontend_add_event(fe, 0);
1958 fepriv->status = 0;
1959 err = 0;
1960 break;
1963 case FE_GET_EVENT:
1964 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1965 break;
1967 case FE_GET_FRONTEND:
1968 if (fe->ops.get_frontend) {
1969 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1970 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1972 break;
1974 case FE_SET_FRONTEND_TUNE_MODE:
1975 fepriv->tune_mode_flags = (unsigned long) parg;
1976 err = 0;
1977 break;
1980 return err;
1984 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1986 struct dvb_device *dvbdev = file->private_data;
1987 struct dvb_frontend *fe = dvbdev->priv;
1988 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1990 dprintk ("%s\n", __func__);
1992 poll_wait (file, &fepriv->events.wait_queue, wait);
1994 if (fepriv->events.eventw != fepriv->events.eventr)
1995 return (POLLIN | POLLRDNORM | POLLPRI);
1997 return 0;
2000 static int dvb_frontend_open(struct inode *inode, struct file *file)
2002 struct dvb_device *dvbdev = file->private_data;
2003 struct dvb_frontend *fe = dvbdev->priv;
2004 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2005 struct dvb_adapter *adapter = fe->dvb;
2006 int ret;
2008 dprintk ("%s\n", __func__);
2010 if (adapter->mfe_shared) {
2011 mutex_lock (&adapter->mfe_lock);
2013 if (adapter->mfe_dvbdev == NULL)
2014 adapter->mfe_dvbdev = dvbdev;
2016 else if (adapter->mfe_dvbdev != dvbdev) {
2017 struct dvb_device
2018 *mfedev = adapter->mfe_dvbdev;
2019 struct dvb_frontend
2020 *mfe = mfedev->priv;
2021 struct dvb_frontend_private
2022 *mfepriv = mfe->frontend_priv;
2023 int mferetry = (dvb_mfe_wait_time << 1);
2025 mutex_unlock (&adapter->mfe_lock);
2026 while (mferetry-- && (mfedev->users != -1 ||
2027 mfepriv->thread != NULL)) {
2028 if(msleep_interruptible(500)) {
2029 if(signal_pending(current))
2030 return -EINTR;
2034 mutex_lock (&adapter->mfe_lock);
2035 if(adapter->mfe_dvbdev != dvbdev) {
2036 mfedev = adapter->mfe_dvbdev;
2037 mfe = mfedev->priv;
2038 mfepriv = mfe->frontend_priv;
2039 if (mfedev->users != -1 ||
2040 mfepriv->thread != NULL) {
2041 mutex_unlock (&adapter->mfe_lock);
2042 return -EBUSY;
2044 adapter->mfe_dvbdev = dvbdev;
2049 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2050 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2051 goto err0;
2054 if ((ret = dvb_generic_open (inode, file)) < 0)
2055 goto err1;
2057 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2058 /* normal tune mode when opened R/W */
2059 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2060 fepriv->tone = -1;
2061 fepriv->voltage = -1;
2063 ret = dvb_frontend_start (fe);
2064 if (ret)
2065 goto err2;
2067 /* empty event queue */
2068 fepriv->events.eventr = fepriv->events.eventw = 0;
2071 if (adapter->mfe_shared)
2072 mutex_unlock (&adapter->mfe_lock);
2073 return ret;
2075 err2:
2076 dvb_generic_release(inode, file);
2077 err1:
2078 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2079 fe->ops.ts_bus_ctrl(fe, 0);
2080 err0:
2081 if (adapter->mfe_shared)
2082 mutex_unlock (&adapter->mfe_lock);
2083 return ret;
2086 static int dvb_frontend_release(struct inode *inode, struct file *file)
2088 struct dvb_device *dvbdev = file->private_data;
2089 struct dvb_frontend *fe = dvbdev->priv;
2090 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2091 int ret;
2093 dprintk ("%s\n", __func__);
2095 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
2096 fepriv->release_jiffies = jiffies;
2098 ret = dvb_generic_release (inode, file);
2100 if (dvbdev->users == -1) {
2101 if (fepriv->exit == 1) {
2102 fops_put(file->f_op);
2103 file->f_op = NULL;
2104 wake_up(&dvbdev->wait_queue);
2106 if (fe->ops.ts_bus_ctrl)
2107 fe->ops.ts_bus_ctrl(fe, 0);
2110 return ret;
2113 static const struct file_operations dvb_frontend_fops = {
2114 .owner = THIS_MODULE,
2115 .ioctl = dvb_generic_ioctl,
2116 .poll = dvb_frontend_poll,
2117 .open = dvb_frontend_open,
2118 .release = dvb_frontend_release
2121 int dvb_register_frontend(struct dvb_adapter* dvb,
2122 struct dvb_frontend* fe)
2124 struct dvb_frontend_private *fepriv;
2125 static const struct dvb_device dvbdev_template = {
2126 .users = ~0,
2127 .writers = 1,
2128 .readers = (~0)-1,
2129 .fops = &dvb_frontend_fops,
2130 .kernel_ioctl = dvb_frontend_ioctl
2133 dprintk ("%s\n", __func__);
2135 if (mutex_lock_interruptible(&frontend_mutex))
2136 return -ERESTARTSYS;
2138 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2139 if (fe->frontend_priv == NULL) {
2140 mutex_unlock(&frontend_mutex);
2141 return -ENOMEM;
2143 fepriv = fe->frontend_priv;
2145 init_MUTEX (&fepriv->sem);
2146 init_waitqueue_head (&fepriv->wait_queue);
2147 init_waitqueue_head (&fepriv->events.wait_queue);
2148 mutex_init(&fepriv->events.mtx);
2149 fe->dvb = dvb;
2150 fepriv->inversion = INVERSION_OFF;
2152 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2153 fe->dvb->num,
2154 fe->id,
2155 fe->ops.info.name);
2157 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2158 fe, DVB_DEVICE_FRONTEND);
2160 mutex_unlock(&frontend_mutex);
2161 return 0;
2163 EXPORT_SYMBOL(dvb_register_frontend);
2165 int dvb_unregister_frontend(struct dvb_frontend* fe)
2167 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2168 dprintk ("%s\n", __func__);
2170 mutex_lock(&frontend_mutex);
2171 dvb_frontend_stop (fe);
2172 mutex_unlock(&frontend_mutex);
2174 if (fepriv->dvbdev->users < -1)
2175 wait_event(fepriv->dvbdev->wait_queue,
2176 fepriv->dvbdev->users==-1);
2178 mutex_lock(&frontend_mutex);
2179 dvb_unregister_device (fepriv->dvbdev);
2181 /* fe is invalid now */
2182 kfree(fepriv);
2183 mutex_unlock(&frontend_mutex);
2184 return 0;
2186 EXPORT_SYMBOL(dvb_unregister_frontend);
2188 #ifdef CONFIG_MEDIA_ATTACH
2189 void dvb_frontend_detach(struct dvb_frontend* fe)
2191 void *ptr;
2193 if (fe->ops.release_sec) {
2194 fe->ops.release_sec(fe);
2195 symbol_put_addr(fe->ops.release_sec);
2197 if (fe->ops.tuner_ops.release) {
2198 fe->ops.tuner_ops.release(fe);
2199 symbol_put_addr(fe->ops.tuner_ops.release);
2201 if (fe->ops.analog_ops.release) {
2202 fe->ops.analog_ops.release(fe);
2203 symbol_put_addr(fe->ops.analog_ops.release);
2205 ptr = (void*)fe->ops.release;
2206 if (ptr) {
2207 fe->ops.release(fe);
2208 symbol_put_addr(ptr);
2211 #else
2212 void dvb_frontend_detach(struct dvb_frontend* fe)
2214 if (fe->ops.release_sec)
2215 fe->ops.release_sec(fe);
2216 if (fe->ops.tuner_ops.release)
2217 fe->ops.tuner_ops.release(fe);
2218 if (fe->ops.analog_ops.release)
2219 fe->ops.analog_ops.release(fe);
2220 if (fe->ops.release)
2221 fe->ops.release(fe);
2223 #endif
2224 EXPORT_SYMBOL(dvb_frontend_detach);