sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / dvb-core / dvb_frontend.c
blobdb74cb74d271b23d7566b82ab51e50d819a3d2db
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 /* Enables DVBv3 compatibility bits at the headers */
29 #define __DVB_CORE__
31 #define pr_fmt(fmt) "dvb_frontend: " fmt
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/wait.h>
37 #include <linux/slab.h>
38 #include <linux/poll.h>
39 #include <linux/semaphore.h>
40 #include <linux/module.h>
41 #include <linux/list.h>
42 #include <linux/freezer.h>
43 #include <linux/jiffies.h>
44 #include <linux/kthread.h>
45 #include <linux/ktime.h>
46 #include <asm/processor.h>
48 #include "dvb_frontend.h"
49 #include "dvbdev.h"
50 #include <linux/dvb/version.h>
52 static int dvb_frontend_debug;
53 static int dvb_shutdown_timeout;
54 static int dvb_force_auto_inversion;
55 static int dvb_override_tune_delay;
56 static int dvb_powerdown_on_sleep = 1;
57 static int dvb_mfe_wait_time = 5;
59 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
60 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
61 module_param(dvb_shutdown_timeout, int, 0644);
62 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
63 module_param(dvb_force_auto_inversion, int, 0644);
64 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
65 module_param(dvb_override_tune_delay, int, 0644);
66 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
67 module_param(dvb_powerdown_on_sleep, int, 0644);
68 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
69 module_param(dvb_mfe_wait_time, int, 0644);
70 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)");
72 #define dprintk(fmt, arg...) \
73 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
75 #define FESTATE_IDLE 1
76 #define FESTATE_RETUNE 2
77 #define FESTATE_TUNING_FAST 4
78 #define FESTATE_TUNING_SLOW 8
79 #define FESTATE_TUNED 16
80 #define FESTATE_ZIGZAG_FAST 32
81 #define FESTATE_ZIGZAG_SLOW 64
82 #define FESTATE_DISEQC 128
83 #define FESTATE_ERROR 256
84 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
85 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
86 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
87 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
90 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
91 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
92 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
93 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
94 * FESTATE_TUNED. The frontend has successfully locked on.
95 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
96 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
97 * FESTATE_DISEQC. A DISEQC command has just been issued.
98 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
99 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
100 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
101 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
104 static DEFINE_MUTEX(frontend_mutex);
106 struct dvb_frontend_private {
107 /* thread/frontend values */
108 struct dvb_device *dvbdev;
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 wakeup;
117 enum fe_status status;
118 unsigned long tune_mode_flags;
119 unsigned int delay;
120 unsigned int reinitialise;
121 int tone;
122 int voltage;
124 /* swzigzag values */
125 unsigned int state;
126 unsigned int bending;
127 int lnb_drift;
128 unsigned int inversion;
129 unsigned int auto_step;
130 unsigned int auto_sub_step;
131 unsigned int started_auto_step;
132 unsigned int min_delay;
133 unsigned int max_drift;
134 unsigned int step_size;
135 int quality;
136 unsigned int check_wrapped;
137 enum dvbfe_search algo_status;
139 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
140 struct media_pipeline pipe;
141 #endif
144 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
145 void (*release)(struct dvb_frontend *fe));
147 static void dvb_frontend_free(struct kref *ref)
149 struct dvb_frontend *fe =
150 container_of(ref, struct dvb_frontend, refcount);
151 struct dvb_frontend_private *fepriv = fe->frontend_priv;
153 dvb_free_device(fepriv->dvbdev);
155 dvb_frontend_invoke_release(fe, fe->ops.release);
157 kfree(fepriv);
160 static void dvb_frontend_put(struct dvb_frontend *fe)
162 kref_put(&fe->refcount, dvb_frontend_free);
165 static void dvb_frontend_get(struct dvb_frontend *fe)
167 kref_get(&fe->refcount);
170 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
171 static int dtv_get_frontend(struct dvb_frontend *fe,
172 struct dtv_frontend_properties *c,
173 struct dvb_frontend_parameters *p_out);
174 static int
175 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
176 const struct dtv_frontend_properties *c,
177 struct dvb_frontend_parameters *p);
179 static bool has_get_frontend(struct dvb_frontend *fe)
181 return fe->ops.get_frontend != NULL;
185 * Due to DVBv3 API calls, a delivery system should be mapped into one of
186 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
187 * otherwise, a DVBv3 call will fail.
189 enum dvbv3_emulation_type {
190 DVBV3_UNKNOWN,
191 DVBV3_QPSK,
192 DVBV3_QAM,
193 DVBV3_OFDM,
194 DVBV3_ATSC,
197 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
199 switch (delivery_system) {
200 case SYS_DVBC_ANNEX_A:
201 case SYS_DVBC_ANNEX_C:
202 return DVBV3_QAM;
203 case SYS_DVBS:
204 case SYS_DVBS2:
205 case SYS_TURBO:
206 case SYS_ISDBS:
207 case SYS_DSS:
208 return DVBV3_QPSK;
209 case SYS_DVBT:
210 case SYS_DVBT2:
211 case SYS_ISDBT:
212 case SYS_DTMB:
213 return DVBV3_OFDM;
214 case SYS_ATSC:
215 case SYS_ATSCMH:
216 case SYS_DVBC_ANNEX_B:
217 return DVBV3_ATSC;
218 case SYS_UNDEFINED:
219 case SYS_ISDBC:
220 case SYS_DVBH:
221 case SYS_DAB:
222 default:
224 * Doesn't know how to emulate those types and/or
225 * there's no frontend driver from this type yet
226 * with some emulation code, so, we're not sure yet how
227 * to handle them, or they're not compatible with a DVBv3 call.
229 return DVBV3_UNKNOWN;
233 static void dvb_frontend_add_event(struct dvb_frontend *fe,
234 enum fe_status status)
236 struct dvb_frontend_private *fepriv = fe->frontend_priv;
237 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
238 struct dvb_fe_events *events = &fepriv->events;
239 struct dvb_frontend_event *e;
240 int wp;
242 dev_dbg(fe->dvb->device, "%s:\n", __func__);
244 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
245 dtv_get_frontend(fe, c, &fepriv->parameters_out);
247 mutex_lock(&events->mtx);
249 wp = (events->eventw + 1) % MAX_EVENT;
250 if (wp == events->eventr) {
251 events->overflow = 1;
252 events->eventr = (events->eventr + 1) % MAX_EVENT;
255 e = &events->events[events->eventw];
256 e->status = status;
257 e->parameters = fepriv->parameters_out;
259 events->eventw = wp;
261 mutex_unlock(&events->mtx);
263 wake_up_interruptible (&events->wait_queue);
266 static int dvb_frontend_get_event(struct dvb_frontend *fe,
267 struct dvb_frontend_event *event, int flags)
269 struct dvb_frontend_private *fepriv = fe->frontend_priv;
270 struct dvb_fe_events *events = &fepriv->events;
272 dev_dbg(fe->dvb->device, "%s:\n", __func__);
274 if (events->overflow) {
275 events->overflow = 0;
276 return -EOVERFLOW;
279 if (events->eventw == events->eventr) {
280 int ret;
282 if (flags & O_NONBLOCK)
283 return -EWOULDBLOCK;
285 up(&fepriv->sem);
287 ret = wait_event_interruptible (events->wait_queue,
288 events->eventw != events->eventr);
290 if (down_interruptible (&fepriv->sem))
291 return -ERESTARTSYS;
293 if (ret < 0)
294 return ret;
297 mutex_lock(&events->mtx);
298 *event = events->events[events->eventr];
299 events->eventr = (events->eventr + 1) % MAX_EVENT;
300 mutex_unlock(&events->mtx);
302 return 0;
305 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
307 struct dvb_frontend_private *fepriv = fe->frontend_priv;
308 struct dvb_fe_events *events = &fepriv->events;
310 mutex_lock(&events->mtx);
311 events->eventr = events->eventw;
312 mutex_unlock(&events->mtx);
315 static void dvb_frontend_init(struct dvb_frontend *fe)
317 dev_dbg(fe->dvb->device,
318 "%s: initialising adapter %i frontend %i (%s)...\n",
319 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
321 if (fe->ops.init)
322 fe->ops.init(fe);
323 if (fe->ops.tuner_ops.init) {
324 if (fe->ops.i2c_gate_ctrl)
325 fe->ops.i2c_gate_ctrl(fe, 1);
326 fe->ops.tuner_ops.init(fe);
327 if (fe->ops.i2c_gate_ctrl)
328 fe->ops.i2c_gate_ctrl(fe, 0);
332 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
334 struct dvb_frontend_private *fepriv = fe->frontend_priv;
336 fepriv->reinitialise = 1;
337 dvb_frontend_wakeup(fe);
339 EXPORT_SYMBOL(dvb_frontend_reinitialise);
341 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
343 int q2;
344 struct dvb_frontend *fe = fepriv->dvbdev->priv;
346 dev_dbg(fe->dvb->device, "%s:\n", __func__);
348 if (locked)
349 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
350 else
351 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
353 q2 = fepriv->quality - 128;
354 q2 *= q2;
356 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
360 * Performs automatic twiddling of frontend parameters.
362 * @param fe The frontend concerned.
363 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
364 * @returns Number of complete iterations that have been performed.
366 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
368 int autoinversion;
369 int ready = 0;
370 int fe_set_err = 0;
371 struct dvb_frontend_private *fepriv = fe->frontend_priv;
372 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
373 int original_inversion = c->inversion;
374 u32 original_frequency = c->frequency;
376 /* are we using autoinversion? */
377 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
378 (c->inversion == INVERSION_AUTO));
380 /* setup parameters correctly */
381 while(!ready) {
382 /* calculate the lnb_drift */
383 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
385 /* wrap the auto_step if we've exceeded the maximum drift */
386 if (fepriv->lnb_drift > fepriv->max_drift) {
387 fepriv->auto_step = 0;
388 fepriv->auto_sub_step = 0;
389 fepriv->lnb_drift = 0;
392 /* perform inversion and +/- zigzag */
393 switch(fepriv->auto_sub_step) {
394 case 0:
395 /* try with the current inversion and current drift setting */
396 ready = 1;
397 break;
399 case 1:
400 if (!autoinversion) break;
402 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
403 ready = 1;
404 break;
406 case 2:
407 if (fepriv->lnb_drift == 0) break;
409 fepriv->lnb_drift = -fepriv->lnb_drift;
410 ready = 1;
411 break;
413 case 3:
414 if (fepriv->lnb_drift == 0) break;
415 if (!autoinversion) break;
417 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
418 fepriv->lnb_drift = -fepriv->lnb_drift;
419 ready = 1;
420 break;
422 default:
423 fepriv->auto_step++;
424 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
425 break;
428 if (!ready) fepriv->auto_sub_step++;
431 /* if this attempt would hit where we started, indicate a complete
432 * iteration has occurred */
433 if ((fepriv->auto_step == fepriv->started_auto_step) &&
434 (fepriv->auto_sub_step == 0) && check_wrapped) {
435 return 1;
438 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
439 "auto_sub_step:%i started_auto_step:%i\n",
440 __func__, fepriv->lnb_drift, fepriv->inversion,
441 fepriv->auto_step, fepriv->auto_sub_step,
442 fepriv->started_auto_step);
444 /* set the frontend itself */
445 c->frequency += fepriv->lnb_drift;
446 if (autoinversion)
447 c->inversion = fepriv->inversion;
448 tmp = *c;
449 if (fe->ops.set_frontend)
450 fe_set_err = fe->ops.set_frontend(fe);
451 *c = tmp;
452 if (fe_set_err < 0) {
453 fepriv->state = FESTATE_ERROR;
454 return fe_set_err;
457 c->frequency = original_frequency;
458 c->inversion = original_inversion;
460 fepriv->auto_sub_step++;
461 return 0;
464 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
466 enum fe_status s = 0;
467 int retval = 0;
468 struct dvb_frontend_private *fepriv = fe->frontend_priv;
469 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
471 /* if we've got no parameters, just keep idling */
472 if (fepriv->state & FESTATE_IDLE) {
473 fepriv->delay = 3*HZ;
474 fepriv->quality = 0;
475 return;
478 /* in SCAN mode, we just set the frontend when asked and leave it alone */
479 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
480 if (fepriv->state & FESTATE_RETUNE) {
481 tmp = *c;
482 if (fe->ops.set_frontend)
483 retval = fe->ops.set_frontend(fe);
484 *c = tmp;
485 if (retval < 0)
486 fepriv->state = FESTATE_ERROR;
487 else
488 fepriv->state = FESTATE_TUNED;
490 fepriv->delay = 3*HZ;
491 fepriv->quality = 0;
492 return;
495 /* get the frontend status */
496 if (fepriv->state & FESTATE_RETUNE) {
497 s = 0;
498 } else {
499 if (fe->ops.read_status)
500 fe->ops.read_status(fe, &s);
501 if (s != fepriv->status) {
502 dvb_frontend_add_event(fe, s);
503 fepriv->status = s;
507 /* if we're not tuned, and we have a lock, move to the TUNED state */
508 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
509 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
510 fepriv->state = FESTATE_TUNED;
512 /* if we're tuned, then we have determined the correct inversion */
513 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
514 (c->inversion == INVERSION_AUTO)) {
515 c->inversion = fepriv->inversion;
517 return;
520 /* if we are tuned already, check we're still locked */
521 if (fepriv->state & FESTATE_TUNED) {
522 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
524 /* we're tuned, and the lock is still good... */
525 if (s & FE_HAS_LOCK) {
526 return;
527 } else { /* if we _WERE_ tuned, but now don't have a lock */
528 fepriv->state = FESTATE_ZIGZAG_FAST;
529 fepriv->started_auto_step = fepriv->auto_step;
530 fepriv->check_wrapped = 0;
534 /* don't actually do anything if we're in the LOSTLOCK state,
535 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
536 if ((fepriv->state & FESTATE_LOSTLOCK) &&
537 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
538 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
539 return;
542 /* don't do anything if we're in the DISEQC state, since this
543 * might be someone with a motorized dish controlled by DISEQC.
544 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
545 if (fepriv->state & FESTATE_DISEQC) {
546 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
547 return;
550 /* if we're in the RETUNE state, set everything up for a brand
551 * new scan, keeping the current inversion setting, as the next
552 * tune is _very_ likely to require the same */
553 if (fepriv->state & FESTATE_RETUNE) {
554 fepriv->lnb_drift = 0;
555 fepriv->auto_step = 0;
556 fepriv->auto_sub_step = 0;
557 fepriv->started_auto_step = 0;
558 fepriv->check_wrapped = 0;
561 /* fast zigzag. */
562 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
563 fepriv->delay = fepriv->min_delay;
565 /* perform a tune */
566 retval = dvb_frontend_swzigzag_autotune(fe,
567 fepriv->check_wrapped);
568 if (retval < 0) {
569 return;
570 } else if (retval) {
571 /* OK, if we've run out of trials at the fast speed.
572 * Drop back to slow for the _next_ attempt */
573 fepriv->state = FESTATE_SEARCHING_SLOW;
574 fepriv->started_auto_step = fepriv->auto_step;
575 return;
577 fepriv->check_wrapped = 1;
579 /* if we've just retuned, enter the ZIGZAG_FAST state.
580 * This ensures we cannot return from an
581 * FE_SET_FRONTEND ioctl before the first frontend tune
582 * occurs */
583 if (fepriv->state & FESTATE_RETUNE) {
584 fepriv->state = FESTATE_TUNING_FAST;
588 /* slow zigzag */
589 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
590 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
592 /* Note: don't bother checking for wrapping; we stay in this
593 * state until we get a lock */
594 dvb_frontend_swzigzag_autotune(fe, 0);
598 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
600 struct dvb_frontend_private *fepriv = fe->frontend_priv;
602 if (fe->exit != DVB_FE_NO_EXIT)
603 return 1;
605 if (fepriv->dvbdev->writers == 1)
606 if (time_after_eq(jiffies, fepriv->release_jiffies +
607 dvb_shutdown_timeout * HZ))
608 return 1;
610 return 0;
613 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
615 struct dvb_frontend_private *fepriv = fe->frontend_priv;
617 if (fepriv->wakeup) {
618 fepriv->wakeup = 0;
619 return 1;
621 return dvb_frontend_is_exiting(fe);
624 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
626 struct dvb_frontend_private *fepriv = fe->frontend_priv;
628 fepriv->wakeup = 1;
629 wake_up_interruptible(&fepriv->wait_queue);
632 static int dvb_frontend_thread(void *data)
634 struct dvb_frontend *fe = data;
635 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
636 struct dvb_frontend_private *fepriv = fe->frontend_priv;
637 enum fe_status s;
638 enum dvbfe_algo algo;
639 bool re_tune = false;
640 bool semheld = false;
642 dev_dbg(fe->dvb->device, "%s:\n", __func__);
644 fepriv->check_wrapped = 0;
645 fepriv->quality = 0;
646 fepriv->delay = 3*HZ;
647 fepriv->status = 0;
648 fepriv->wakeup = 0;
649 fepriv->reinitialise = 0;
651 dvb_frontend_init(fe);
653 set_freezable();
654 while (1) {
655 up(&fepriv->sem); /* is locked when we enter the thread... */
656 restart:
657 wait_event_interruptible_timeout(fepriv->wait_queue,
658 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
659 || freezing(current),
660 fepriv->delay);
662 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
663 /* got signal or quitting */
664 if (!down_interruptible(&fepriv->sem))
665 semheld = true;
666 fe->exit = DVB_FE_NORMAL_EXIT;
667 break;
670 if (try_to_freeze())
671 goto restart;
673 if (down_interruptible(&fepriv->sem))
674 break;
676 if (fepriv->reinitialise) {
677 dvb_frontend_init(fe);
678 if (fe->ops.set_tone && fepriv->tone != -1)
679 fe->ops.set_tone(fe, fepriv->tone);
680 if (fe->ops.set_voltage && fepriv->voltage != -1)
681 fe->ops.set_voltage(fe, fepriv->voltage);
682 fepriv->reinitialise = 0;
685 /* do an iteration of the tuning loop */
686 if (fe->ops.get_frontend_algo) {
687 algo = fe->ops.get_frontend_algo(fe);
688 switch (algo) {
689 case DVBFE_ALGO_HW:
690 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
692 if (fepriv->state & FESTATE_RETUNE) {
693 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
694 re_tune = true;
695 fepriv->state = FESTATE_TUNED;
696 } else {
697 re_tune = false;
700 if (fe->ops.tune)
701 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
703 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
704 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
705 dvb_frontend_add_event(fe, s);
706 fepriv->status = s;
708 break;
709 case DVBFE_ALGO_SW:
710 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
711 dvb_frontend_swzigzag(fe);
712 break;
713 case DVBFE_ALGO_CUSTOM:
714 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
715 if (fepriv->state & FESTATE_RETUNE) {
716 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
717 fepriv->state = FESTATE_TUNED;
719 /* Case where we are going to search for a carrier
720 * User asked us to retune again for some reason, possibly
721 * requesting a search with a new set of parameters
723 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
724 if (fe->ops.search) {
725 fepriv->algo_status = fe->ops.search(fe);
726 /* We did do a search as was requested, the flags are
727 * now unset as well and has the flags wrt to search.
729 } else {
730 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
733 /* Track the carrier if the search was successful */
734 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
735 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
736 fepriv->delay = HZ / 2;
738 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
739 fe->ops.read_status(fe, &s);
740 if (s != fepriv->status) {
741 dvb_frontend_add_event(fe, s); /* update event list */
742 fepriv->status = s;
743 if (!(s & FE_HAS_LOCK)) {
744 fepriv->delay = HZ / 10;
745 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
746 } else {
747 fepriv->delay = 60 * HZ;
750 break;
751 default:
752 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
753 break;
755 } else {
756 dvb_frontend_swzigzag(fe);
760 if (dvb_powerdown_on_sleep) {
761 if (fe->ops.set_voltage)
762 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
763 if (fe->ops.tuner_ops.sleep) {
764 if (fe->ops.i2c_gate_ctrl)
765 fe->ops.i2c_gate_ctrl(fe, 1);
766 fe->ops.tuner_ops.sleep(fe);
767 if (fe->ops.i2c_gate_ctrl)
768 fe->ops.i2c_gate_ctrl(fe, 0);
770 if (fe->ops.sleep)
771 fe->ops.sleep(fe);
774 fepriv->thread = NULL;
775 if (kthread_should_stop())
776 fe->exit = DVB_FE_DEVICE_REMOVED;
777 else
778 fe->exit = DVB_FE_NO_EXIT;
779 mb();
781 if (semheld)
782 up(&fepriv->sem);
783 dvb_frontend_wakeup(fe);
784 return 0;
787 static void dvb_frontend_stop(struct dvb_frontend *fe)
789 struct dvb_frontend_private *fepriv = fe->frontend_priv;
791 dev_dbg(fe->dvb->device, "%s:\n", __func__);
793 if (fe->exit != DVB_FE_DEVICE_REMOVED)
794 fe->exit = DVB_FE_NORMAL_EXIT;
795 mb();
797 if (!fepriv->thread)
798 return;
800 kthread_stop(fepriv->thread);
802 sema_init(&fepriv->sem, 1);
803 fepriv->state = FESTATE_IDLE;
805 /* paranoia check in case a signal arrived */
806 if (fepriv->thread)
807 dev_warn(fe->dvb->device,
808 "dvb_frontend_stop: warning: thread %p won't exit\n",
809 fepriv->thread);
813 * Sleep for the amount of time given by add_usec parameter
815 * This needs to be as precise as possible, as it affects the detection of
816 * the dish tone command at the satellite subsystem. The precision is improved
817 * by using a scheduled msleep followed by udelay for the remainder.
819 void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
821 s32 delta;
823 *waketime = ktime_add_us(*waketime, add_usec);
824 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
825 if (delta > 2500) {
826 msleep((delta - 1500) / 1000);
827 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
829 if (delta > 0)
830 udelay(delta);
832 EXPORT_SYMBOL(dvb_frontend_sleep_until);
834 static int dvb_frontend_start(struct dvb_frontend *fe)
836 int ret;
837 struct dvb_frontend_private *fepriv = fe->frontend_priv;
838 struct task_struct *fe_thread;
840 dev_dbg(fe->dvb->device, "%s:\n", __func__);
842 if (fepriv->thread) {
843 if (fe->exit == DVB_FE_NO_EXIT)
844 return 0;
845 else
846 dvb_frontend_stop (fe);
849 if (signal_pending(current))
850 return -EINTR;
851 if (down_interruptible (&fepriv->sem))
852 return -EINTR;
854 fepriv->state = FESTATE_IDLE;
855 fe->exit = DVB_FE_NO_EXIT;
856 fepriv->thread = NULL;
857 mb();
859 fe_thread = kthread_run(dvb_frontend_thread, fe,
860 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
861 if (IS_ERR(fe_thread)) {
862 ret = PTR_ERR(fe_thread);
863 dev_warn(fe->dvb->device,
864 "dvb_frontend_start: failed to start kthread (%d)\n",
865 ret);
866 up(&fepriv->sem);
867 return ret;
869 fepriv->thread = fe_thread;
870 return 0;
873 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
874 u32 *freq_min, u32 *freq_max)
876 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
878 if (fe->ops.info.frequency_max == 0)
879 *freq_max = fe->ops.tuner_ops.info.frequency_max;
880 else if (fe->ops.tuner_ops.info.frequency_max == 0)
881 *freq_max = fe->ops.info.frequency_max;
882 else
883 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
885 if (*freq_min == 0 || *freq_max == 0)
886 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
887 fe->dvb->num, fe->id);
890 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
892 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
893 u32 freq_min;
894 u32 freq_max;
896 /* range check: frequency */
897 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
898 if ((freq_min && c->frequency < freq_min) ||
899 (freq_max && c->frequency > freq_max)) {
900 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
901 fe->dvb->num, fe->id, c->frequency,
902 freq_min, freq_max);
903 return -EINVAL;
906 /* range check: symbol rate */
907 switch (c->delivery_system) {
908 case SYS_DVBS:
909 case SYS_DVBS2:
910 case SYS_TURBO:
911 case SYS_DVBC_ANNEX_A:
912 case SYS_DVBC_ANNEX_C:
913 if ((fe->ops.info.symbol_rate_min &&
914 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
915 (fe->ops.info.symbol_rate_max &&
916 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
917 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
918 fe->dvb->num, fe->id, c->symbol_rate,
919 fe->ops.info.symbol_rate_min,
920 fe->ops.info.symbol_rate_max);
921 return -EINVAL;
923 default:
924 break;
927 return 0;
930 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
932 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
933 int i;
934 u32 delsys;
936 delsys = c->delivery_system;
937 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
938 c->delivery_system = delsys;
940 c->state = DTV_CLEAR;
942 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
943 __func__, c->delivery_system);
945 c->transmission_mode = TRANSMISSION_MODE_AUTO;
946 c->bandwidth_hz = 0; /* AUTO */
947 c->guard_interval = GUARD_INTERVAL_AUTO;
948 c->hierarchy = HIERARCHY_AUTO;
949 c->symbol_rate = 0;
950 c->code_rate_HP = FEC_AUTO;
951 c->code_rate_LP = FEC_AUTO;
952 c->fec_inner = FEC_AUTO;
953 c->rolloff = ROLLOFF_AUTO;
954 c->voltage = SEC_VOLTAGE_OFF;
955 c->sectone = SEC_TONE_OFF;
956 c->pilot = PILOT_AUTO;
958 c->isdbt_partial_reception = 0;
959 c->isdbt_sb_mode = 0;
960 c->isdbt_sb_subchannel = 0;
961 c->isdbt_sb_segment_idx = 0;
962 c->isdbt_sb_segment_count = 0;
963 c->isdbt_layer_enabled = 0;
964 for (i = 0; i < 3; i++) {
965 c->layer[i].fec = FEC_AUTO;
966 c->layer[i].modulation = QAM_AUTO;
967 c->layer[i].interleaving = 0;
968 c->layer[i].segment_count = 0;
971 c->stream_id = NO_STREAM_ID_FILTER;
973 switch (c->delivery_system) {
974 case SYS_DVBS:
975 case SYS_DVBS2:
976 case SYS_TURBO:
977 c->modulation = QPSK; /* implied for DVB-S in legacy API */
978 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
979 break;
980 case SYS_ATSC:
981 c->modulation = VSB_8;
982 break;
983 case SYS_ISDBS:
984 c->symbol_rate = 28860000;
985 c->rolloff = ROLLOFF_35;
986 c->bandwidth_hz = c->symbol_rate / 100 * 135;
987 break;
988 default:
989 c->modulation = QAM_AUTO;
990 break;
993 c->lna = LNA_AUTO;
995 return 0;
998 #define _DTV_CMD(n, s, b) \
999 [n] = { \
1000 .name = #n, \
1001 .cmd = n, \
1002 .set = s,\
1003 .buffer = b \
1006 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1007 _DTV_CMD(DTV_TUNE, 1, 0),
1008 _DTV_CMD(DTV_CLEAR, 1, 0),
1010 /* Set */
1011 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1012 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1013 _DTV_CMD(DTV_MODULATION, 1, 0),
1014 _DTV_CMD(DTV_INVERSION, 1, 0),
1015 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1016 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1017 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1018 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1019 _DTV_CMD(DTV_TONE, 1, 0),
1020 _DTV_CMD(DTV_PILOT, 1, 0),
1021 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1022 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1023 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1024 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1025 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1026 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1027 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1028 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1030 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1031 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1032 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1033 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1034 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1035 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1036 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1037 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1038 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1039 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1040 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1041 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1042 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1043 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1044 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1045 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1046 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1047 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1049 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1050 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1051 _DTV_CMD(DTV_LNA, 1, 0),
1053 /* Get */
1054 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1055 _DTV_CMD(DTV_API_VERSION, 0, 0),
1057 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1059 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1060 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1062 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1063 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1064 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1065 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1066 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1067 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1068 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1069 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1070 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1071 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1072 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1073 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1074 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1076 /* Statistics API */
1077 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1078 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1079 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1080 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1081 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1082 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1083 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1084 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1087 static void dtv_property_dump(struct dvb_frontend *fe,
1088 bool is_set,
1089 struct dtv_property *tvp)
1091 int i;
1093 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1094 dev_warn(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x undefined\n",
1095 __func__,
1096 is_set ? "SET" : "GET",
1097 tvp->cmd);
1098 return;
1101 dev_dbg(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x (%s)\n", __func__,
1102 is_set ? "SET" : "GET",
1103 tvp->cmd,
1104 dtv_cmds[tvp->cmd].name);
1106 if (dtv_cmds[tvp->cmd].buffer) {
1107 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1108 __func__, tvp->u.buffer.len);
1110 for(i = 0; i < tvp->u.buffer.len; i++)
1111 dev_dbg(fe->dvb->device,
1112 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1113 __func__, i, tvp->u.buffer.data[i]);
1114 } else {
1115 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1116 tvp->u.data);
1120 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1121 * drivers can use a single set_frontend tuning function, regardless of whether
1122 * it's being used for the legacy or new API, reducing code and complexity.
1124 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1125 struct dtv_frontend_properties *c,
1126 const struct dvb_frontend_parameters *p)
1128 c->frequency = p->frequency;
1129 c->inversion = p->inversion;
1131 switch (dvbv3_type(c->delivery_system)) {
1132 case DVBV3_QPSK:
1133 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1134 c->symbol_rate = p->u.qpsk.symbol_rate;
1135 c->fec_inner = p->u.qpsk.fec_inner;
1136 break;
1137 case DVBV3_QAM:
1138 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1139 c->symbol_rate = p->u.qam.symbol_rate;
1140 c->fec_inner = p->u.qam.fec_inner;
1141 c->modulation = p->u.qam.modulation;
1142 break;
1143 case DVBV3_OFDM:
1144 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1146 switch (p->u.ofdm.bandwidth) {
1147 case BANDWIDTH_10_MHZ:
1148 c->bandwidth_hz = 10000000;
1149 break;
1150 case BANDWIDTH_8_MHZ:
1151 c->bandwidth_hz = 8000000;
1152 break;
1153 case BANDWIDTH_7_MHZ:
1154 c->bandwidth_hz = 7000000;
1155 break;
1156 case BANDWIDTH_6_MHZ:
1157 c->bandwidth_hz = 6000000;
1158 break;
1159 case BANDWIDTH_5_MHZ:
1160 c->bandwidth_hz = 5000000;
1161 break;
1162 case BANDWIDTH_1_712_MHZ:
1163 c->bandwidth_hz = 1712000;
1164 break;
1165 case BANDWIDTH_AUTO:
1166 c->bandwidth_hz = 0;
1169 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1170 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1171 c->modulation = p->u.ofdm.constellation;
1172 c->transmission_mode = p->u.ofdm.transmission_mode;
1173 c->guard_interval = p->u.ofdm.guard_interval;
1174 c->hierarchy = p->u.ofdm.hierarchy_information;
1175 break;
1176 case DVBV3_ATSC:
1177 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1178 c->modulation = p->u.vsb.modulation;
1179 if (c->delivery_system == SYS_ATSCMH)
1180 break;
1181 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1182 c->delivery_system = SYS_ATSC;
1183 else
1184 c->delivery_system = SYS_DVBC_ANNEX_B;
1185 break;
1186 case DVBV3_UNKNOWN:
1187 dev_err(fe->dvb->device,
1188 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1189 __func__, c->delivery_system);
1190 return -EINVAL;
1193 return 0;
1196 /* Ensure the cached values are set correctly in the frontend
1197 * legacy tuning structures, for the advanced tuning API.
1199 static int
1200 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1201 const struct dtv_frontend_properties *c,
1202 struct dvb_frontend_parameters *p)
1204 p->frequency = c->frequency;
1205 p->inversion = c->inversion;
1207 switch (dvbv3_type(c->delivery_system)) {
1208 case DVBV3_UNKNOWN:
1209 dev_err(fe->dvb->device,
1210 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1211 __func__, c->delivery_system);
1212 return -EINVAL;
1213 case DVBV3_QPSK:
1214 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1215 p->u.qpsk.symbol_rate = c->symbol_rate;
1216 p->u.qpsk.fec_inner = c->fec_inner;
1217 break;
1218 case DVBV3_QAM:
1219 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1220 p->u.qam.symbol_rate = c->symbol_rate;
1221 p->u.qam.fec_inner = c->fec_inner;
1222 p->u.qam.modulation = c->modulation;
1223 break;
1224 case DVBV3_OFDM:
1225 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1226 switch (c->bandwidth_hz) {
1227 case 10000000:
1228 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1229 break;
1230 case 8000000:
1231 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1232 break;
1233 case 7000000:
1234 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1235 break;
1236 case 6000000:
1237 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1238 break;
1239 case 5000000:
1240 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1241 break;
1242 case 1712000:
1243 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1244 break;
1245 case 0:
1246 default:
1247 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1249 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1250 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1251 p->u.ofdm.constellation = c->modulation;
1252 p->u.ofdm.transmission_mode = c->transmission_mode;
1253 p->u.ofdm.guard_interval = c->guard_interval;
1254 p->u.ofdm.hierarchy_information = c->hierarchy;
1255 break;
1256 case DVBV3_ATSC:
1257 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1258 p->u.vsb.modulation = c->modulation;
1259 break;
1261 return 0;
1265 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1266 * @fe: struct dvb_frontend pointer
1267 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1268 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1270 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1271 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1272 * If p_out is not null, it will update the DVBv3 params pointed by it.
1274 static int dtv_get_frontend(struct dvb_frontend *fe,
1275 struct dtv_frontend_properties *c,
1276 struct dvb_frontend_parameters *p_out)
1278 int r;
1280 if (fe->ops.get_frontend) {
1281 r = fe->ops.get_frontend(fe, c);
1282 if (unlikely(r < 0))
1283 return r;
1284 if (p_out)
1285 dtv_property_legacy_params_sync(fe, c, p_out);
1286 return 0;
1289 /* As everything is in cache, get_frontend fops are always supported */
1290 return 0;
1293 static int dvb_frontend_ioctl_legacy(struct file *file,
1294 unsigned int cmd, void *parg);
1295 static int dvb_frontend_ioctl_properties(struct file *file,
1296 unsigned int cmd, void *parg);
1298 static int dtv_property_process_get(struct dvb_frontend *fe,
1299 const struct dtv_frontend_properties *c,
1300 struct dtv_property *tvp,
1301 struct file *file)
1303 int r, ncaps;
1305 switch(tvp->cmd) {
1306 case DTV_ENUM_DELSYS:
1307 ncaps = 0;
1308 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1309 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1310 ncaps++;
1312 tvp->u.buffer.len = ncaps;
1313 break;
1314 case DTV_FREQUENCY:
1315 tvp->u.data = c->frequency;
1316 break;
1317 case DTV_MODULATION:
1318 tvp->u.data = c->modulation;
1319 break;
1320 case DTV_BANDWIDTH_HZ:
1321 tvp->u.data = c->bandwidth_hz;
1322 break;
1323 case DTV_INVERSION:
1324 tvp->u.data = c->inversion;
1325 break;
1326 case DTV_SYMBOL_RATE:
1327 tvp->u.data = c->symbol_rate;
1328 break;
1329 case DTV_INNER_FEC:
1330 tvp->u.data = c->fec_inner;
1331 break;
1332 case DTV_PILOT:
1333 tvp->u.data = c->pilot;
1334 break;
1335 case DTV_ROLLOFF:
1336 tvp->u.data = c->rolloff;
1337 break;
1338 case DTV_DELIVERY_SYSTEM:
1339 tvp->u.data = c->delivery_system;
1340 break;
1341 case DTV_VOLTAGE:
1342 tvp->u.data = c->voltage;
1343 break;
1344 case DTV_TONE:
1345 tvp->u.data = c->sectone;
1346 break;
1347 case DTV_API_VERSION:
1348 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1349 break;
1350 case DTV_CODE_RATE_HP:
1351 tvp->u.data = c->code_rate_HP;
1352 break;
1353 case DTV_CODE_RATE_LP:
1354 tvp->u.data = c->code_rate_LP;
1355 break;
1356 case DTV_GUARD_INTERVAL:
1357 tvp->u.data = c->guard_interval;
1358 break;
1359 case DTV_TRANSMISSION_MODE:
1360 tvp->u.data = c->transmission_mode;
1361 break;
1362 case DTV_HIERARCHY:
1363 tvp->u.data = c->hierarchy;
1364 break;
1365 case DTV_INTERLEAVING:
1366 tvp->u.data = c->interleaving;
1367 break;
1369 /* ISDB-T Support here */
1370 case DTV_ISDBT_PARTIAL_RECEPTION:
1371 tvp->u.data = c->isdbt_partial_reception;
1372 break;
1373 case DTV_ISDBT_SOUND_BROADCASTING:
1374 tvp->u.data = c->isdbt_sb_mode;
1375 break;
1376 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1377 tvp->u.data = c->isdbt_sb_subchannel;
1378 break;
1379 case DTV_ISDBT_SB_SEGMENT_IDX:
1380 tvp->u.data = c->isdbt_sb_segment_idx;
1381 break;
1382 case DTV_ISDBT_SB_SEGMENT_COUNT:
1383 tvp->u.data = c->isdbt_sb_segment_count;
1384 break;
1385 case DTV_ISDBT_LAYER_ENABLED:
1386 tvp->u.data = c->isdbt_layer_enabled;
1387 break;
1388 case DTV_ISDBT_LAYERA_FEC:
1389 tvp->u.data = c->layer[0].fec;
1390 break;
1391 case DTV_ISDBT_LAYERA_MODULATION:
1392 tvp->u.data = c->layer[0].modulation;
1393 break;
1394 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1395 tvp->u.data = c->layer[0].segment_count;
1396 break;
1397 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1398 tvp->u.data = c->layer[0].interleaving;
1399 break;
1400 case DTV_ISDBT_LAYERB_FEC:
1401 tvp->u.data = c->layer[1].fec;
1402 break;
1403 case DTV_ISDBT_LAYERB_MODULATION:
1404 tvp->u.data = c->layer[1].modulation;
1405 break;
1406 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1407 tvp->u.data = c->layer[1].segment_count;
1408 break;
1409 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1410 tvp->u.data = c->layer[1].interleaving;
1411 break;
1412 case DTV_ISDBT_LAYERC_FEC:
1413 tvp->u.data = c->layer[2].fec;
1414 break;
1415 case DTV_ISDBT_LAYERC_MODULATION:
1416 tvp->u.data = c->layer[2].modulation;
1417 break;
1418 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1419 tvp->u.data = c->layer[2].segment_count;
1420 break;
1421 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1422 tvp->u.data = c->layer[2].interleaving;
1423 break;
1425 /* Multistream support */
1426 case DTV_STREAM_ID:
1427 case DTV_DVBT2_PLP_ID_LEGACY:
1428 tvp->u.data = c->stream_id;
1429 break;
1431 /* ATSC-MH */
1432 case DTV_ATSCMH_FIC_VER:
1433 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1434 break;
1435 case DTV_ATSCMH_PARADE_ID:
1436 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1437 break;
1438 case DTV_ATSCMH_NOG:
1439 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1440 break;
1441 case DTV_ATSCMH_TNOG:
1442 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1443 break;
1444 case DTV_ATSCMH_SGN:
1445 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1446 break;
1447 case DTV_ATSCMH_PRC:
1448 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1449 break;
1450 case DTV_ATSCMH_RS_FRAME_MODE:
1451 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1452 break;
1453 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1454 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1455 break;
1456 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1457 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1458 break;
1459 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1460 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1461 break;
1462 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1463 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1464 break;
1465 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1466 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1467 break;
1468 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1469 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1470 break;
1471 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1472 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1473 break;
1474 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1475 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1476 break;
1478 case DTV_LNA:
1479 tvp->u.data = c->lna;
1480 break;
1482 /* Fill quality measures */
1483 case DTV_STAT_SIGNAL_STRENGTH:
1484 tvp->u.st = c->strength;
1485 break;
1486 case DTV_STAT_CNR:
1487 tvp->u.st = c->cnr;
1488 break;
1489 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1490 tvp->u.st = c->pre_bit_error;
1491 break;
1492 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1493 tvp->u.st = c->pre_bit_count;
1494 break;
1495 case DTV_STAT_POST_ERROR_BIT_COUNT:
1496 tvp->u.st = c->post_bit_error;
1497 break;
1498 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1499 tvp->u.st = c->post_bit_count;
1500 break;
1501 case DTV_STAT_ERROR_BLOCK_COUNT:
1502 tvp->u.st = c->block_error;
1503 break;
1504 case DTV_STAT_TOTAL_BLOCK_COUNT:
1505 tvp->u.st = c->block_count;
1506 break;
1507 default:
1508 dev_dbg(fe->dvb->device,
1509 "%s: FE property %d doesn't exist\n",
1510 __func__, tvp->cmd);
1511 return -EINVAL;
1514 /* Allow the frontend to override outgoing properties */
1515 if (fe->ops.get_property) {
1516 r = fe->ops.get_property(fe, tvp);
1517 if (r < 0)
1518 return r;
1521 dtv_property_dump(fe, false, tvp);
1523 return 0;
1526 static int dtv_set_frontend(struct dvb_frontend *fe);
1528 static bool is_dvbv3_delsys(u32 delsys)
1530 return (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1531 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1535 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1536 * @fe: struct frontend;
1537 * @delsys: DVBv5 type that will be used for emulation
1539 * Provides emulation for delivery systems that are compatible with the old
1540 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1541 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1542 * parameters are compatible with DVB-S spec.
1544 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1546 int i;
1547 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1549 c->delivery_system = delsys;
1552 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1554 if (c->delivery_system == SYS_ISDBT) {
1555 dev_dbg(fe->dvb->device,
1556 "%s: Using defaults for SYS_ISDBT\n",
1557 __func__);
1559 if (!c->bandwidth_hz)
1560 c->bandwidth_hz = 6000000;
1562 c->isdbt_partial_reception = 0;
1563 c->isdbt_sb_mode = 0;
1564 c->isdbt_sb_subchannel = 0;
1565 c->isdbt_sb_segment_idx = 0;
1566 c->isdbt_sb_segment_count = 0;
1567 c->isdbt_layer_enabled = 7;
1568 for (i = 0; i < 3; i++) {
1569 c->layer[i].fec = FEC_AUTO;
1570 c->layer[i].modulation = QAM_AUTO;
1571 c->layer[i].interleaving = 0;
1572 c->layer[i].segment_count = 0;
1575 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1576 __func__, c->delivery_system);
1578 return 0;
1582 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1583 * @fe: frontend struct
1584 * @desired_system: delivery system requested by the user
1586 * A DVBv5 call know what's the desired system it wants. So, set it.
1588 * There are, however, a few known issues with early DVBv5 applications that
1589 * are also handled by this logic:
1591 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1592 * This is an API violation, but, as we don't want to break userspace,
1593 * convert it to the first supported delivery system.
1594 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1595 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1596 * ISDB-T provided backward compat with DVB-T.
1598 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1599 u32 desired_system)
1601 int ncaps;
1602 u32 delsys = SYS_UNDEFINED;
1603 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1604 enum dvbv3_emulation_type type;
1607 * It was reported that some old DVBv5 applications were
1608 * filling delivery_system with SYS_UNDEFINED. If this happens,
1609 * assume that the application wants to use the first supported
1610 * delivery system.
1612 if (desired_system == SYS_UNDEFINED)
1613 desired_system = fe->ops.delsys[0];
1616 * This is a DVBv5 call. So, it likely knows the supported
1617 * delivery systems. So, check if the desired delivery system is
1618 * supported
1620 ncaps = 0;
1621 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1622 if (fe->ops.delsys[ncaps] == desired_system) {
1623 c->delivery_system = desired_system;
1624 dev_dbg(fe->dvb->device,
1625 "%s: Changing delivery system to %d\n",
1626 __func__, desired_system);
1627 return 0;
1629 ncaps++;
1633 * The requested delivery system isn't supported. Maybe userspace
1634 * is requesting a DVBv3 compatible delivery system.
1636 * The emulation only works if the desired system is one of the
1637 * delivery systems supported by DVBv3 API
1639 if (!is_dvbv3_delsys(desired_system)) {
1640 dev_dbg(fe->dvb->device,
1641 "%s: Delivery system %d not supported.\n",
1642 __func__, desired_system);
1643 return -EINVAL;
1646 type = dvbv3_type(desired_system);
1649 * Get the last non-DVBv3 delivery system that has the same type
1650 * of the desired system
1652 ncaps = 0;
1653 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1654 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1655 delsys = fe->ops.delsys[ncaps];
1656 ncaps++;
1659 /* There's nothing compatible with the desired delivery system */
1660 if (delsys == SYS_UNDEFINED) {
1661 dev_dbg(fe->dvb->device,
1662 "%s: Delivery system %d not supported on emulation mode.\n",
1663 __func__, desired_system);
1664 return -EINVAL;
1667 dev_dbg(fe->dvb->device,
1668 "%s: Using delivery system %d emulated as if it were %d\n",
1669 __func__, delsys, desired_system);
1671 return emulate_delivery_system(fe, desired_system);
1675 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1676 * @fe: frontend struct
1678 * A DVBv3 call doesn't know what's the desired system it wants. It also
1679 * doesn't allow to switch between different types. Due to that, userspace
1680 * should use DVBv5 instead.
1681 * However, in order to avoid breaking userspace API, limited backward
1682 * compatibility support is provided.
1684 * There are some delivery systems that are incompatible with DVBv3 calls.
1686 * This routine should work fine for frontends that support just one delivery
1687 * system.
1689 * For frontends that support multiple frontends:
1690 * 1) It defaults to use the first supported delivery system. There's an
1691 * userspace application that allows changing it at runtime;
1693 * 2) If the current delivery system is not compatible with DVBv3, it gets
1694 * the first one that it is compatible.
1696 * NOTE: in order for this to work with applications like Kaffeine that
1697 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1698 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1699 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1700 * to DVB-S.
1702 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1704 int ncaps;
1705 u32 delsys = SYS_UNDEFINED;
1706 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1708 /* If not set yet, defaults to the first supported delivery system */
1709 if (c->delivery_system == SYS_UNDEFINED)
1710 c->delivery_system = fe->ops.delsys[0];
1713 * Trivial case: just use the current one, if it already a DVBv3
1714 * delivery system
1716 if (is_dvbv3_delsys(c->delivery_system)) {
1717 dev_dbg(fe->dvb->device,
1718 "%s: Using delivery system to %d\n",
1719 __func__, c->delivery_system);
1720 return 0;
1724 * Seek for the first delivery system that it is compatible with a
1725 * DVBv3 standard
1727 ncaps = 0;
1728 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1729 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1730 delsys = fe->ops.delsys[ncaps];
1731 break;
1733 ncaps++;
1735 if (delsys == SYS_UNDEFINED) {
1736 dev_dbg(fe->dvb->device,
1737 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1738 __func__);
1739 return -EINVAL;
1741 return emulate_delivery_system(fe, delsys);
1744 static int dtv_property_process_set(struct dvb_frontend *fe,
1745 struct dtv_property *tvp,
1746 struct file *file)
1748 int r = 0;
1749 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1751 /* Allow the frontend to validate incoming properties */
1752 if (fe->ops.set_property) {
1753 r = fe->ops.set_property(fe, tvp);
1754 if (r < 0)
1755 return r;
1758 dtv_property_dump(fe, true, tvp);
1760 switch(tvp->cmd) {
1761 case DTV_CLEAR:
1763 * Reset a cache of data specific to the frontend here. This does
1764 * not effect hardware.
1766 dvb_frontend_clear_cache(fe);
1767 break;
1768 case DTV_TUNE:
1769 /* interpret the cache of data, build either a traditional frontend
1770 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1771 * ioctl.
1773 c->state = tvp->cmd;
1774 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1775 __func__);
1777 r = dtv_set_frontend(fe);
1778 break;
1779 case DTV_FREQUENCY:
1780 c->frequency = tvp->u.data;
1781 break;
1782 case DTV_MODULATION:
1783 c->modulation = tvp->u.data;
1784 break;
1785 case DTV_BANDWIDTH_HZ:
1786 c->bandwidth_hz = tvp->u.data;
1787 break;
1788 case DTV_INVERSION:
1789 c->inversion = tvp->u.data;
1790 break;
1791 case DTV_SYMBOL_RATE:
1792 c->symbol_rate = tvp->u.data;
1793 break;
1794 case DTV_INNER_FEC:
1795 c->fec_inner = tvp->u.data;
1796 break;
1797 case DTV_PILOT:
1798 c->pilot = tvp->u.data;
1799 break;
1800 case DTV_ROLLOFF:
1801 c->rolloff = tvp->u.data;
1802 break;
1803 case DTV_DELIVERY_SYSTEM:
1804 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1805 break;
1806 case DTV_VOLTAGE:
1807 c->voltage = tvp->u.data;
1808 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1809 (void *)c->voltage);
1810 break;
1811 case DTV_TONE:
1812 c->sectone = tvp->u.data;
1813 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1814 (void *)c->sectone);
1815 break;
1816 case DTV_CODE_RATE_HP:
1817 c->code_rate_HP = tvp->u.data;
1818 break;
1819 case DTV_CODE_RATE_LP:
1820 c->code_rate_LP = tvp->u.data;
1821 break;
1822 case DTV_GUARD_INTERVAL:
1823 c->guard_interval = tvp->u.data;
1824 break;
1825 case DTV_TRANSMISSION_MODE:
1826 c->transmission_mode = tvp->u.data;
1827 break;
1828 case DTV_HIERARCHY:
1829 c->hierarchy = tvp->u.data;
1830 break;
1831 case DTV_INTERLEAVING:
1832 c->interleaving = tvp->u.data;
1833 break;
1835 /* ISDB-T Support here */
1836 case DTV_ISDBT_PARTIAL_RECEPTION:
1837 c->isdbt_partial_reception = tvp->u.data;
1838 break;
1839 case DTV_ISDBT_SOUND_BROADCASTING:
1840 c->isdbt_sb_mode = tvp->u.data;
1841 break;
1842 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1843 c->isdbt_sb_subchannel = tvp->u.data;
1844 break;
1845 case DTV_ISDBT_SB_SEGMENT_IDX:
1846 c->isdbt_sb_segment_idx = tvp->u.data;
1847 break;
1848 case DTV_ISDBT_SB_SEGMENT_COUNT:
1849 c->isdbt_sb_segment_count = tvp->u.data;
1850 break;
1851 case DTV_ISDBT_LAYER_ENABLED:
1852 c->isdbt_layer_enabled = tvp->u.data;
1853 break;
1854 case DTV_ISDBT_LAYERA_FEC:
1855 c->layer[0].fec = tvp->u.data;
1856 break;
1857 case DTV_ISDBT_LAYERA_MODULATION:
1858 c->layer[0].modulation = tvp->u.data;
1859 break;
1860 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1861 c->layer[0].segment_count = tvp->u.data;
1862 break;
1863 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1864 c->layer[0].interleaving = tvp->u.data;
1865 break;
1866 case DTV_ISDBT_LAYERB_FEC:
1867 c->layer[1].fec = tvp->u.data;
1868 break;
1869 case DTV_ISDBT_LAYERB_MODULATION:
1870 c->layer[1].modulation = tvp->u.data;
1871 break;
1872 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1873 c->layer[1].segment_count = tvp->u.data;
1874 break;
1875 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1876 c->layer[1].interleaving = tvp->u.data;
1877 break;
1878 case DTV_ISDBT_LAYERC_FEC:
1879 c->layer[2].fec = tvp->u.data;
1880 break;
1881 case DTV_ISDBT_LAYERC_MODULATION:
1882 c->layer[2].modulation = tvp->u.data;
1883 break;
1884 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1885 c->layer[2].segment_count = tvp->u.data;
1886 break;
1887 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1888 c->layer[2].interleaving = tvp->u.data;
1889 break;
1891 /* Multistream support */
1892 case DTV_STREAM_ID:
1893 case DTV_DVBT2_PLP_ID_LEGACY:
1894 c->stream_id = tvp->u.data;
1895 break;
1897 /* ATSC-MH */
1898 case DTV_ATSCMH_PARADE_ID:
1899 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1900 break;
1901 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1902 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1903 break;
1905 case DTV_LNA:
1906 c->lna = tvp->u.data;
1907 if (fe->ops.set_lna)
1908 r = fe->ops.set_lna(fe);
1909 if (r < 0)
1910 c->lna = LNA_AUTO;
1911 break;
1913 default:
1914 return -EINVAL;
1917 return r;
1920 static int dvb_frontend_ioctl(struct file *file,
1921 unsigned int cmd, void *parg)
1923 struct dvb_device *dvbdev = file->private_data;
1924 struct dvb_frontend *fe = dvbdev->priv;
1925 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1926 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1927 int err = -EOPNOTSUPP;
1929 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
1930 if (down_interruptible(&fepriv->sem))
1931 return -ERESTARTSYS;
1933 if (fe->exit != DVB_FE_NO_EXIT) {
1934 up(&fepriv->sem);
1935 return -ENODEV;
1938 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1939 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1940 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1941 up(&fepriv->sem);
1942 return -EPERM;
1945 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1946 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1947 else {
1948 c->state = DTV_UNDEFINED;
1949 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1952 up(&fepriv->sem);
1953 return err;
1956 static int dvb_frontend_ioctl_properties(struct file *file,
1957 unsigned int cmd, void *parg)
1959 struct dvb_device *dvbdev = file->private_data;
1960 struct dvb_frontend *fe = dvbdev->priv;
1961 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1962 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1963 int err = 0;
1965 struct dtv_properties *tvps = parg;
1966 struct dtv_property *tvp = NULL;
1967 int i;
1969 dev_dbg(fe->dvb->device, "%s:\n", __func__);
1971 if (cmd == FE_SET_PROPERTY) {
1972 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1973 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
1975 /* Put an arbitrary limit on the number of messages that can
1976 * be sent at once */
1977 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1978 return -EINVAL;
1980 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
1981 if (IS_ERR(tvp))
1982 return PTR_ERR(tvp);
1984 for (i = 0; i < tvps->num; i++) {
1985 err = dtv_property_process_set(fe, tvp + i, file);
1986 if (err < 0)
1987 goto out;
1988 (tvp + i)->result = err;
1991 if (c->state == DTV_TUNE)
1992 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
1994 } else if (cmd == FE_GET_PROPERTY) {
1995 struct dtv_frontend_properties getp = fe->dtv_property_cache;
1997 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1998 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2000 /* Put an arbitrary limit on the number of messages that can
2001 * be sent at once */
2002 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2003 return -EINVAL;
2005 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
2006 if (IS_ERR(tvp))
2007 return PTR_ERR(tvp);
2010 * Let's use our own copy of property cache, in order to
2011 * avoid mangling with DTV zigzag logic, as drivers might
2012 * return crap, if they don't check if the data is available
2013 * before updating the properties cache.
2015 if (fepriv->state != FESTATE_IDLE) {
2016 err = dtv_get_frontend(fe, &getp, NULL);
2017 if (err < 0)
2018 goto out;
2020 for (i = 0; i < tvps->num; i++) {
2021 err = dtv_property_process_get(fe, &getp, tvp + i, file);
2022 if (err < 0)
2023 goto out;
2024 (tvp + i)->result = err;
2027 if (copy_to_user((void __user *)tvps->props, tvp,
2028 tvps->num * sizeof(struct dtv_property))) {
2029 err = -EFAULT;
2030 goto out;
2033 } else
2034 err = -EOPNOTSUPP;
2036 out:
2037 kfree(tvp);
2038 return err;
2041 static int dtv_set_frontend(struct dvb_frontend *fe)
2043 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2044 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2045 struct dvb_frontend_tune_settings fetunesettings;
2046 u32 rolloff = 0;
2048 if (dvb_frontend_check_parameters(fe) < 0)
2049 return -EINVAL;
2052 * Initialize output parameters to match the values given by
2053 * the user. FE_SET_FRONTEND triggers an initial frontend event
2054 * with status = 0, which copies output parameters to userspace.
2056 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
2059 * Be sure that the bandwidth will be filled for all
2060 * non-satellite systems, as tuners need to know what
2061 * low pass/Nyquist half filter should be applied, in
2062 * order to avoid inter-channel noise.
2064 * ISDB-T and DVB-T/T2 already sets bandwidth.
2065 * ATSC and DVB-C don't set, so, the core should fill it.
2067 * On DVB-C Annex A and C, the bandwidth is a function of
2068 * the roll-off and symbol rate. Annex B defines different
2069 * roll-off factors depending on the modulation. Fortunately,
2070 * Annex B is only used with 6MHz, so there's no need to
2071 * calculate it.
2073 * While not officially supported, a side effect of handling it at
2074 * the cache level is that a program could retrieve the bandwidth
2075 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2077 switch (c->delivery_system) {
2078 case SYS_ATSC:
2079 case SYS_DVBC_ANNEX_B:
2080 c->bandwidth_hz = 6000000;
2081 break;
2082 case SYS_DVBC_ANNEX_A:
2083 rolloff = 115;
2084 break;
2085 case SYS_DVBC_ANNEX_C:
2086 rolloff = 113;
2087 break;
2088 case SYS_DVBS:
2089 case SYS_TURBO:
2090 case SYS_ISDBS:
2091 rolloff = 135;
2092 break;
2093 case SYS_DVBS2:
2094 switch (c->rolloff) {
2095 case ROLLOFF_20:
2096 rolloff = 120;
2097 break;
2098 case ROLLOFF_25:
2099 rolloff = 125;
2100 break;
2101 default:
2102 case ROLLOFF_35:
2103 rolloff = 135;
2105 break;
2106 default:
2107 break;
2109 if (rolloff)
2110 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
2112 /* force auto frequency inversion if requested */
2113 if (dvb_force_auto_inversion)
2114 c->inversion = INVERSION_AUTO;
2117 * without hierarchical coding code_rate_LP is irrelevant,
2118 * so we tolerate the otherwise invalid FEC_NONE setting
2120 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2121 c->code_rate_LP = FEC_AUTO;
2123 /* get frontend-specific tuning settings */
2124 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2125 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2126 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2127 fepriv->max_drift = fetunesettings.max_drift;
2128 fepriv->step_size = fetunesettings.step_size;
2129 } else {
2130 /* default values */
2131 switch (c->delivery_system) {
2132 case SYS_DVBS:
2133 case SYS_DVBS2:
2134 case SYS_ISDBS:
2135 case SYS_TURBO:
2136 case SYS_DVBC_ANNEX_A:
2137 case SYS_DVBC_ANNEX_C:
2138 fepriv->min_delay = HZ / 20;
2139 fepriv->step_size = c->symbol_rate / 16000;
2140 fepriv->max_drift = c->symbol_rate / 2000;
2141 break;
2142 case SYS_DVBT:
2143 case SYS_DVBT2:
2144 case SYS_ISDBT:
2145 case SYS_DTMB:
2146 fepriv->min_delay = HZ / 20;
2147 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2148 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2149 break;
2150 default:
2152 * FIXME: This sounds wrong! if freqency_stepsize is
2153 * defined by the frontend, why not use it???
2155 fepriv->min_delay = HZ / 20;
2156 fepriv->step_size = 0; /* no zigzag */
2157 fepriv->max_drift = 0;
2158 break;
2161 if (dvb_override_tune_delay > 0)
2162 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2164 fepriv->state = FESTATE_RETUNE;
2166 /* Request the search algorithm to search */
2167 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2169 dvb_frontend_clear_events(fe);
2170 dvb_frontend_add_event(fe, 0);
2171 dvb_frontend_wakeup(fe);
2172 fepriv->status = 0;
2174 return 0;
2178 static int dvb_frontend_ioctl_legacy(struct file *file,
2179 unsigned int cmd, void *parg)
2181 struct dvb_device *dvbdev = file->private_data;
2182 struct dvb_frontend *fe = dvbdev->priv;
2183 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2184 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2185 int err = -EOPNOTSUPP;
2187 switch (cmd) {
2188 case FE_GET_INFO: {
2189 struct dvb_frontend_info* info = parg;
2191 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2192 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2195 * Associate the 4 delivery systems supported by DVBv3
2196 * API with their DVBv5 counterpart. For the other standards,
2197 * use the closest type, assuming that it would hopefully
2198 * work with a DVBv3 application.
2199 * It should be noticed that, on multi-frontend devices with
2200 * different types (terrestrial and cable, for example),
2201 * a pure DVBv3 application won't be able to use all delivery
2202 * systems. Yet, changing the DVBv5 cache to the other delivery
2203 * system should be enough for making it work.
2205 switch (dvbv3_type(c->delivery_system)) {
2206 case DVBV3_QPSK:
2207 info->type = FE_QPSK;
2208 break;
2209 case DVBV3_ATSC:
2210 info->type = FE_ATSC;
2211 break;
2212 case DVBV3_QAM:
2213 info->type = FE_QAM;
2214 break;
2215 case DVBV3_OFDM:
2216 info->type = FE_OFDM;
2217 break;
2218 default:
2219 dev_err(fe->dvb->device,
2220 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2221 __func__, c->delivery_system);
2222 fe->ops.info.type = FE_OFDM;
2224 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2225 __func__, c->delivery_system, fe->ops.info.type);
2227 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2228 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2229 info->caps |= FE_CAN_INVERSION_AUTO;
2230 err = 0;
2231 break;
2234 case FE_READ_STATUS: {
2235 enum fe_status *status = parg;
2237 /* if retune was requested but hasn't occurred yet, prevent
2238 * that user get signal state from previous tuning */
2239 if (fepriv->state == FESTATE_RETUNE ||
2240 fepriv->state == FESTATE_ERROR) {
2241 err=0;
2242 *status = 0;
2243 break;
2246 if (fe->ops.read_status)
2247 err = fe->ops.read_status(fe, status);
2248 break;
2251 case FE_READ_BER:
2252 if (fe->ops.read_ber) {
2253 if (fepriv->thread)
2254 err = fe->ops.read_ber(fe, (__u32 *) parg);
2255 else
2256 err = -EAGAIN;
2258 break;
2260 case FE_READ_SIGNAL_STRENGTH:
2261 if (fe->ops.read_signal_strength) {
2262 if (fepriv->thread)
2263 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2264 else
2265 err = -EAGAIN;
2267 break;
2269 case FE_READ_SNR:
2270 if (fe->ops.read_snr) {
2271 if (fepriv->thread)
2272 err = fe->ops.read_snr(fe, (__u16 *) parg);
2273 else
2274 err = -EAGAIN;
2276 break;
2278 case FE_READ_UNCORRECTED_BLOCKS:
2279 if (fe->ops.read_ucblocks) {
2280 if (fepriv->thread)
2281 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2282 else
2283 err = -EAGAIN;
2285 break;
2287 case FE_DISEQC_RESET_OVERLOAD:
2288 if (fe->ops.diseqc_reset_overload) {
2289 err = fe->ops.diseqc_reset_overload(fe);
2290 fepriv->state = FESTATE_DISEQC;
2291 fepriv->status = 0;
2293 break;
2295 case FE_DISEQC_SEND_MASTER_CMD:
2296 if (fe->ops.diseqc_send_master_cmd) {
2297 struct dvb_diseqc_master_cmd *cmd = parg;
2299 if (cmd->msg_len > sizeof(cmd->msg)) {
2300 err = -EINVAL;
2301 break;
2303 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
2304 fepriv->state = FESTATE_DISEQC;
2305 fepriv->status = 0;
2307 break;
2309 case FE_DISEQC_SEND_BURST:
2310 if (fe->ops.diseqc_send_burst) {
2311 err = fe->ops.diseqc_send_burst(fe,
2312 (enum fe_sec_mini_cmd)parg);
2313 fepriv->state = FESTATE_DISEQC;
2314 fepriv->status = 0;
2316 break;
2318 case FE_SET_TONE:
2319 if (fe->ops.set_tone) {
2320 err = fe->ops.set_tone(fe,
2321 (enum fe_sec_tone_mode)parg);
2322 fepriv->tone = (enum fe_sec_tone_mode)parg;
2323 fepriv->state = FESTATE_DISEQC;
2324 fepriv->status = 0;
2326 break;
2328 case FE_SET_VOLTAGE:
2329 if (fe->ops.set_voltage) {
2330 err = fe->ops.set_voltage(fe,
2331 (enum fe_sec_voltage)parg);
2332 fepriv->voltage = (enum fe_sec_voltage)parg;
2333 fepriv->state = FESTATE_DISEQC;
2334 fepriv->status = 0;
2336 break;
2338 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2339 if (fe->ops.dishnetwork_send_legacy_command) {
2340 err = fe->ops.dishnetwork_send_legacy_command(fe,
2341 (unsigned long)parg);
2342 fepriv->state = FESTATE_DISEQC;
2343 fepriv->status = 0;
2344 } else if (fe->ops.set_voltage) {
2346 * NOTE: This is a fallback condition. Some frontends
2347 * (stv0299 for instance) take longer than 8msec to
2348 * respond to a set_voltage command. Those switches
2349 * need custom routines to switch properly. For all
2350 * other frontends, the following should work ok.
2351 * Dish network legacy switches (as used by Dish500)
2352 * are controlled by sending 9-bit command words
2353 * spaced 8msec apart.
2354 * the actual command word is switch/port dependent
2355 * so it is up to the userspace application to send
2356 * the right command.
2357 * The command must always start with a '0' after
2358 * initialization, so parg is 8 bits and does not
2359 * include the initialization or start bit
2361 unsigned long swcmd = ((unsigned long) parg) << 1;
2362 ktime_t nexttime;
2363 ktime_t tv[10];
2364 int i;
2365 u8 last = 1;
2366 if (dvb_frontend_debug)
2367 dprintk("%s switch command: 0x%04lx\n",
2368 __func__, swcmd);
2369 nexttime = ktime_get_boottime();
2370 if (dvb_frontend_debug)
2371 tv[0] = nexttime;
2372 /* before sending a command, initialize by sending
2373 * a 32ms 18V to the switch
2375 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2376 dvb_frontend_sleep_until(&nexttime, 32000);
2378 for (i = 0; i < 9; i++) {
2379 if (dvb_frontend_debug)
2380 tv[i+1] = ktime_get_boottime();
2381 if ((swcmd & 0x01) != last) {
2382 /* set voltage to (last ? 13V : 18V) */
2383 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2384 last = (last) ? 0 : 1;
2386 swcmd = swcmd >> 1;
2387 if (i != 8)
2388 dvb_frontend_sleep_until(&nexttime, 8000);
2390 if (dvb_frontend_debug) {
2391 dprintk("%s(%d): switch delay (should be 32k followed by all 8k)\n",
2392 __func__, fe->dvb->num);
2393 for (i = 1; i < 10; i++)
2394 pr_info("%d: %d\n", i,
2395 (int) ktime_us_delta(tv[i], tv[i-1]));
2397 err = 0;
2398 fepriv->state = FESTATE_DISEQC;
2399 fepriv->status = 0;
2401 break;
2403 case FE_DISEQC_RECV_SLAVE_REPLY:
2404 if (fe->ops.diseqc_recv_slave_reply)
2405 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2406 break;
2408 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2409 if (fe->ops.enable_high_lnb_voltage)
2410 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2411 break;
2413 case FE_SET_FRONTEND:
2414 err = dvbv3_set_delivery_system(fe);
2415 if (err)
2416 break;
2418 err = dtv_property_cache_sync(fe, c, parg);
2419 if (err)
2420 break;
2421 err = dtv_set_frontend(fe);
2422 break;
2423 case FE_GET_EVENT:
2424 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2425 break;
2427 case FE_GET_FRONTEND: {
2428 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2431 * Let's use our own copy of property cache, in order to
2432 * avoid mangling with DTV zigzag logic, as drivers might
2433 * return crap, if they don't check if the data is available
2434 * before updating the properties cache.
2436 err = dtv_get_frontend(fe, &getp, parg);
2437 break;
2439 case FE_SET_FRONTEND_TUNE_MODE:
2440 fepriv->tune_mode_flags = (unsigned long) parg;
2441 err = 0;
2442 break;
2445 return err;
2449 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2451 struct dvb_device *dvbdev = file->private_data;
2452 struct dvb_frontend *fe = dvbdev->priv;
2453 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2455 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2457 poll_wait (file, &fepriv->events.wait_queue, wait);
2459 if (fepriv->events.eventw != fepriv->events.eventr)
2460 return (POLLIN | POLLRDNORM | POLLPRI);
2462 return 0;
2465 static int dvb_frontend_open(struct inode *inode, struct file *file)
2467 struct dvb_device *dvbdev = file->private_data;
2468 struct dvb_frontend *fe = dvbdev->priv;
2469 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2470 struct dvb_adapter *adapter = fe->dvb;
2471 int ret;
2473 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2474 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2475 return -ENODEV;
2477 if (adapter->mfe_shared) {
2478 mutex_lock (&adapter->mfe_lock);
2480 if (adapter->mfe_dvbdev == NULL)
2481 adapter->mfe_dvbdev = dvbdev;
2483 else if (adapter->mfe_dvbdev != dvbdev) {
2484 struct dvb_device
2485 *mfedev = adapter->mfe_dvbdev;
2486 struct dvb_frontend
2487 *mfe = mfedev->priv;
2488 struct dvb_frontend_private
2489 *mfepriv = mfe->frontend_priv;
2490 int mferetry = (dvb_mfe_wait_time << 1);
2492 mutex_unlock (&adapter->mfe_lock);
2493 while (mferetry-- && (mfedev->users != -1 ||
2494 mfepriv->thread != NULL)) {
2495 if(msleep_interruptible(500)) {
2496 if(signal_pending(current))
2497 return -EINTR;
2501 mutex_lock (&adapter->mfe_lock);
2502 if(adapter->mfe_dvbdev != dvbdev) {
2503 mfedev = adapter->mfe_dvbdev;
2504 mfe = mfedev->priv;
2505 mfepriv = mfe->frontend_priv;
2506 if (mfedev->users != -1 ||
2507 mfepriv->thread != NULL) {
2508 mutex_unlock (&adapter->mfe_lock);
2509 return -EBUSY;
2511 adapter->mfe_dvbdev = dvbdev;
2516 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2517 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2518 goto err0;
2520 /* If we took control of the bus, we need to force
2521 reinitialization. This is because many ts_bus_ctrl()
2522 functions strobe the RESET pin on the demod, and if the
2523 frontend thread already exists then the dvb_init() routine
2524 won't get called (which is what usually does initial
2525 register configuration). */
2526 fepriv->reinitialise = 1;
2529 if ((ret = dvb_generic_open (inode, file)) < 0)
2530 goto err1;
2532 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2533 /* normal tune mode when opened R/W */
2534 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2535 fepriv->tone = -1;
2536 fepriv->voltage = -1;
2538 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2539 if (fe->dvb->mdev && fe->dvb->mdev->enable_source) {
2540 ret = fe->dvb->mdev->enable_source(dvbdev->entity,
2541 &fepriv->pipe);
2542 if (ret) {
2543 dev_err(fe->dvb->device,
2544 "Tuner is busy. Error %d\n", ret);
2545 goto err2;
2548 #endif
2549 ret = dvb_frontend_start (fe);
2550 if (ret)
2551 goto err3;
2553 /* empty event queue */
2554 fepriv->events.eventr = fepriv->events.eventw = 0;
2557 dvb_frontend_get(fe);
2559 if (adapter->mfe_shared)
2560 mutex_unlock (&adapter->mfe_lock);
2561 return ret;
2563 err3:
2564 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2565 if (fe->dvb->mdev && fe->dvb->mdev->disable_source)
2566 fe->dvb->mdev->disable_source(dvbdev->entity);
2567 err2:
2568 #endif
2569 dvb_generic_release(inode, file);
2570 err1:
2571 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2572 fe->ops.ts_bus_ctrl(fe, 0);
2573 err0:
2574 if (adapter->mfe_shared)
2575 mutex_unlock (&adapter->mfe_lock);
2576 return ret;
2579 static int dvb_frontend_release(struct inode *inode, struct file *file)
2581 struct dvb_device *dvbdev = file->private_data;
2582 struct dvb_frontend *fe = dvbdev->priv;
2583 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2584 int ret;
2586 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2588 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2589 fepriv->release_jiffies = jiffies;
2590 mb();
2593 ret = dvb_generic_release (inode, file);
2595 if (dvbdev->users == -1) {
2596 wake_up(&fepriv->wait_queue);
2597 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2598 if (fe->dvb->mdev && fe->dvb->mdev->disable_source)
2599 fe->dvb->mdev->disable_source(dvbdev->entity);
2600 #endif
2601 if (fe->exit != DVB_FE_NO_EXIT)
2602 wake_up(&dvbdev->wait_queue);
2603 if (fe->ops.ts_bus_ctrl)
2604 fe->ops.ts_bus_ctrl(fe, 0);
2607 dvb_frontend_put(fe);
2609 return ret;
2612 static const struct file_operations dvb_frontend_fops = {
2613 .owner = THIS_MODULE,
2614 .unlocked_ioctl = dvb_generic_ioctl,
2615 .poll = dvb_frontend_poll,
2616 .open = dvb_frontend_open,
2617 .release = dvb_frontend_release,
2618 .llseek = noop_llseek,
2621 int dvb_frontend_suspend(struct dvb_frontend *fe)
2623 int ret = 0;
2625 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2626 fe->id);
2628 if (fe->ops.tuner_ops.suspend)
2629 ret = fe->ops.tuner_ops.suspend(fe);
2630 else if (fe->ops.tuner_ops.sleep)
2631 ret = fe->ops.tuner_ops.sleep(fe);
2633 if (fe->ops.sleep)
2634 ret = fe->ops.sleep(fe);
2636 return ret;
2638 EXPORT_SYMBOL(dvb_frontend_suspend);
2640 int dvb_frontend_resume(struct dvb_frontend *fe)
2642 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2643 int ret = 0;
2645 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2646 fe->id);
2648 fe->exit = DVB_FE_DEVICE_RESUME;
2649 if (fe->ops.init)
2650 ret = fe->ops.init(fe);
2652 if (fe->ops.tuner_ops.resume)
2653 ret = fe->ops.tuner_ops.resume(fe);
2654 else if (fe->ops.tuner_ops.init)
2655 ret = fe->ops.tuner_ops.init(fe);
2657 if (fe->ops.set_tone && fepriv->tone != -1)
2658 fe->ops.set_tone(fe, fepriv->tone);
2659 if (fe->ops.set_voltage && fepriv->voltage != -1)
2660 fe->ops.set_voltage(fe, fepriv->voltage);
2662 fe->exit = DVB_FE_NO_EXIT;
2663 fepriv->state = FESTATE_RETUNE;
2664 dvb_frontend_wakeup(fe);
2666 return ret;
2668 EXPORT_SYMBOL(dvb_frontend_resume);
2670 int dvb_register_frontend(struct dvb_adapter* dvb,
2671 struct dvb_frontend* fe)
2673 struct dvb_frontend_private *fepriv;
2674 const struct dvb_device dvbdev_template = {
2675 .users = ~0,
2676 .writers = 1,
2677 .readers = (~0)-1,
2678 .fops = &dvb_frontend_fops,
2679 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2680 .name = fe->ops.info.name,
2681 #endif
2682 .kernel_ioctl = dvb_frontend_ioctl
2685 dev_dbg(dvb->device, "%s:\n", __func__);
2687 if (mutex_lock_interruptible(&frontend_mutex))
2688 return -ERESTARTSYS;
2690 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2691 if (fe->frontend_priv == NULL) {
2692 mutex_unlock(&frontend_mutex);
2693 return -ENOMEM;
2695 fepriv = fe->frontend_priv;
2697 kref_init(&fe->refcount);
2700 * After initialization, there need to be two references: one
2701 * for dvb_unregister_frontend(), and another one for
2702 * dvb_frontend_detach().
2704 dvb_frontend_get(fe);
2706 sema_init(&fepriv->sem, 1);
2707 init_waitqueue_head (&fepriv->wait_queue);
2708 init_waitqueue_head (&fepriv->events.wait_queue);
2709 mutex_init(&fepriv->events.mtx);
2710 fe->dvb = dvb;
2711 fepriv->inversion = INVERSION_OFF;
2713 dev_info(fe->dvb->device,
2714 "DVB: registering adapter %i frontend %i (%s)...\n",
2715 fe->dvb->num, fe->id, fe->ops.info.name);
2717 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2718 fe, DVB_DEVICE_FRONTEND, 0);
2721 * Initialize the cache to the proper values according with the
2722 * first supported delivery system (ops->delsys[0])
2725 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2726 dvb_frontend_clear_cache(fe);
2728 mutex_unlock(&frontend_mutex);
2729 return 0;
2731 EXPORT_SYMBOL(dvb_register_frontend);
2733 int dvb_unregister_frontend(struct dvb_frontend* fe)
2735 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2736 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2738 mutex_lock(&frontend_mutex);
2739 dvb_frontend_stop(fe);
2740 dvb_remove_device(fepriv->dvbdev);
2742 /* fe is invalid now */
2743 mutex_unlock(&frontend_mutex);
2744 dvb_frontend_put(fe);
2745 return 0;
2747 EXPORT_SYMBOL(dvb_unregister_frontend);
2749 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
2750 void (*release)(struct dvb_frontend *fe))
2752 if (release) {
2753 release(fe);
2754 #ifdef CONFIG_MEDIA_ATTACH
2755 dvb_detach(release);
2756 #endif
2760 void dvb_frontend_detach(struct dvb_frontend* fe)
2762 dvb_frontend_invoke_release(fe, fe->ops.release_sec);
2763 dvb_frontend_invoke_release(fe, fe->ops.tuner_ops.release);
2764 dvb_frontend_invoke_release(fe, fe->ops.analog_ops.release);
2765 dvb_frontend_invoke_release(fe, fe->ops.detach);
2766 dvb_frontend_put(fe);
2768 EXPORT_SYMBOL(dvb_frontend_detach);