1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dvb_frontend.c: DVB frontend tuning interface/thread
5 * Copyright (C) 1999-2001 Ralph Metzler
8 * for convergence integrated media GmbH
10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
13 /* Enables DVBv3 compatibility bits at the headers */
16 #define pr_fmt(fmt) "dvb_frontend: " fmt
18 #include <linux/string.h>
19 #include <linux/kernel.h>
20 #include <linux/sched/signal.h>
21 #include <linux/wait.h>
22 #include <linux/slab.h>
23 #include <linux/poll.h>
24 #include <linux/semaphore.h>
25 #include <linux/module.h>
26 #include <linux/nospec.h>
27 #include <linux/list.h>
28 #include <linux/freezer.h>
29 #include <linux/jiffies.h>
30 #include <linux/kthread.h>
31 #include <linux/ktime.h>
32 #include <linux/compat.h>
33 #include <asm/processor.h>
35 #include <media/dvb_frontend.h>
36 #include <media/dvbdev.h>
37 #include <linux/dvb/version.h>
39 static int dvb_frontend_debug;
40 static int dvb_shutdown_timeout;
41 static int dvb_force_auto_inversion;
42 static int dvb_override_tune_delay;
43 static int dvb_powerdown_on_sleep = 1;
44 static int dvb_mfe_wait_time = 5;
46 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
47 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
48 module_param(dvb_shutdown_timeout, int, 0644);
49 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
50 module_param(dvb_force_auto_inversion, int, 0644);
51 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
52 module_param(dvb_override_tune_delay, int, 0644);
53 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
54 module_param(dvb_powerdown_on_sleep, int, 0644);
55 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
56 module_param(dvb_mfe_wait_time, int, 0644);
57 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)");
59 #define dprintk(fmt, arg...) \
60 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
62 #define FESTATE_IDLE 1
63 #define FESTATE_RETUNE 2
64 #define FESTATE_TUNING_FAST 4
65 #define FESTATE_TUNING_SLOW 8
66 #define FESTATE_TUNED 16
67 #define FESTATE_ZIGZAG_FAST 32
68 #define FESTATE_ZIGZAG_SLOW 64
69 #define FESTATE_DISEQC 128
70 #define FESTATE_ERROR 256
71 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
72 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
73 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
74 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
77 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
78 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
79 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
80 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
81 * FESTATE_TUNED. The frontend has successfully locked on.
82 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
83 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
84 * FESTATE_DISEQC. A DISEQC command has just been issued.
85 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
86 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
87 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
88 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
91 static DEFINE_MUTEX(frontend_mutex);
93 struct dvb_frontend_private {
94 /* thread/frontend values */
95 struct dvb_device *dvbdev;
96 struct dvb_frontend_parameters parameters_out;
97 struct dvb_fe_events events;
99 struct list_head list_head;
100 wait_queue_head_t wait_queue;
101 struct task_struct *thread;
102 unsigned long release_jiffies;
104 enum fe_status status;
105 unsigned long tune_mode_flags;
107 unsigned int reinitialise;
111 /* swzigzag values */
113 unsigned int bending;
115 unsigned int inversion;
116 unsigned int auto_step;
117 unsigned int auto_sub_step;
118 unsigned int started_auto_step;
119 unsigned int min_delay;
120 unsigned int max_drift;
121 unsigned int step_size;
123 unsigned int check_wrapped;
124 enum dvbfe_search algo_status;
126 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
127 struct media_pipeline pipe;
131 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
132 void (*release)(struct dvb_frontend *fe));
134 static void __dvb_frontend_free(struct dvb_frontend *fe)
136 struct dvb_frontend_private *fepriv = fe->frontend_priv;
139 dvb_device_put(fepriv->dvbdev);
141 dvb_frontend_invoke_release(fe, fe->ops.release);
146 static void dvb_frontend_free(struct kref *ref)
148 struct dvb_frontend *fe =
149 container_of(ref, struct dvb_frontend, refcount);
151 __dvb_frontend_free(fe);
154 static void dvb_frontend_put(struct dvb_frontend *fe)
156 /* call detach before dropping the reference count */
160 * Check if the frontend was registered, as otherwise
161 * kref was not initialized yet.
163 if (fe->frontend_priv)
164 kref_put(&fe->refcount, dvb_frontend_free);
166 __dvb_frontend_free(fe);
169 static void dvb_frontend_get(struct dvb_frontend *fe)
171 kref_get(&fe->refcount);
174 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
175 static int dtv_get_frontend(struct dvb_frontend *fe,
176 struct dtv_frontend_properties *c,
177 struct dvb_frontend_parameters *p_out);
179 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
180 const struct dtv_frontend_properties *c,
181 struct dvb_frontend_parameters *p);
183 static bool has_get_frontend(struct dvb_frontend *fe)
185 return fe->ops.get_frontend;
189 * Due to DVBv3 API calls, a delivery system should be mapped into one of
190 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
191 * otherwise, a DVBv3 call will fail.
193 enum dvbv3_emulation_type {
201 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
203 switch (delivery_system) {
204 case SYS_DVBC_ANNEX_A:
205 case SYS_DVBC_ANNEX_C:
220 case SYS_DVBC_ANNEX_B:
228 * Doesn't know how to emulate those types and/or
229 * there's no frontend driver from this type yet
230 * with some emulation code, so, we're not sure yet how
231 * to handle them, or they're not compatible with a DVBv3 call.
233 return DVBV3_UNKNOWN;
237 static void dvb_frontend_add_event(struct dvb_frontend *fe,
238 enum fe_status status)
240 struct dvb_frontend_private *fepriv = fe->frontend_priv;
241 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
242 struct dvb_fe_events *events = &fepriv->events;
243 struct dvb_frontend_event *e;
246 dev_dbg(fe->dvb->device, "%s:\n", __func__);
248 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
249 dtv_get_frontend(fe, c, &fepriv->parameters_out);
251 mutex_lock(&events->mtx);
253 wp = (events->eventw + 1) % MAX_EVENT;
254 if (wp == events->eventr) {
255 events->overflow = 1;
256 events->eventr = (events->eventr + 1) % MAX_EVENT;
259 e = &events->events[events->eventw];
261 e->parameters = fepriv->parameters_out;
265 mutex_unlock(&events->mtx);
267 wake_up_interruptible(&events->wait_queue);
270 static int dvb_frontend_test_event(struct dvb_frontend_private *fepriv,
271 struct dvb_fe_events *events)
276 ret = events->eventw != events->eventr;
282 static int dvb_frontend_get_event(struct dvb_frontend *fe,
283 struct dvb_frontend_event *event, int flags)
285 struct dvb_frontend_private *fepriv = fe->frontend_priv;
286 struct dvb_fe_events *events = &fepriv->events;
288 dev_dbg(fe->dvb->device, "%s:\n", __func__);
290 if (events->overflow) {
291 events->overflow = 0;
295 if (events->eventw == events->eventr) {
296 struct wait_queue_entry wait;
299 if (flags & O_NONBLOCK)
302 init_waitqueue_entry(&wait, current);
303 add_wait_queue(&events->wait_queue, &wait);
304 while (!dvb_frontend_test_event(fepriv, events)) {
305 wait_woken(&wait, TASK_INTERRUPTIBLE, 0);
306 if (signal_pending(current)) {
311 remove_wait_queue(&events->wait_queue, &wait);
316 mutex_lock(&events->mtx);
317 *event = events->events[events->eventr];
318 events->eventr = (events->eventr + 1) % MAX_EVENT;
319 mutex_unlock(&events->mtx);
324 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
326 struct dvb_frontend_private *fepriv = fe->frontend_priv;
327 struct dvb_fe_events *events = &fepriv->events;
329 mutex_lock(&events->mtx);
330 events->eventr = events->eventw;
331 mutex_unlock(&events->mtx);
334 static void dvb_frontend_init(struct dvb_frontend *fe)
336 dev_dbg(fe->dvb->device,
337 "%s: initialising adapter %i frontend %i (%s)...\n",
338 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
342 if (fe->ops.tuner_ops.init) {
343 if (fe->ops.i2c_gate_ctrl)
344 fe->ops.i2c_gate_ctrl(fe, 1);
345 fe->ops.tuner_ops.init(fe);
346 if (fe->ops.i2c_gate_ctrl)
347 fe->ops.i2c_gate_ctrl(fe, 0);
351 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
353 struct dvb_frontend_private *fepriv = fe->frontend_priv;
355 fepriv->reinitialise = 1;
356 dvb_frontend_wakeup(fe);
358 EXPORT_SYMBOL(dvb_frontend_reinitialise);
360 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
363 struct dvb_frontend *fe = fepriv->dvbdev->priv;
365 dev_dbg(fe->dvb->device, "%s:\n", __func__);
368 (fepriv->quality) = (fepriv->quality * 220 + 36 * 256) / 256;
370 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
372 q2 = fepriv->quality - 128;
375 fepriv->delay = fepriv->min_delay + q2 * HZ / (128 * 128);
379 * dvb_frontend_swzigzag_autotune - Performs automatic twiddling of frontend
382 * @fe: The frontend concerned.
383 * @check_wrapped: Checks if an iteration has completed.
384 * DO NOT SET ON THE FIRST ATTEMPT.
386 * return: Number of complete iterations that have been performed.
388 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
393 struct dvb_frontend_private *fepriv = fe->frontend_priv;
394 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
395 int original_inversion = c->inversion;
396 u32 original_frequency = c->frequency;
398 /* are we using autoinversion? */
399 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
400 (c->inversion == INVERSION_AUTO));
402 /* setup parameters correctly */
404 /* calculate the lnb_drift */
405 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
407 /* wrap the auto_step if we've exceeded the maximum drift */
408 if (fepriv->lnb_drift > fepriv->max_drift) {
409 fepriv->auto_step = 0;
410 fepriv->auto_sub_step = 0;
411 fepriv->lnb_drift = 0;
414 /* perform inversion and +/- zigzag */
415 switch (fepriv->auto_sub_step) {
417 /* try with the current inversion and current drift setting */
422 if (!autoinversion) break;
424 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
429 if (fepriv->lnb_drift == 0) break;
431 fepriv->lnb_drift = -fepriv->lnb_drift;
436 if (fepriv->lnb_drift == 0) break;
437 if (!autoinversion) break;
439 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
440 fepriv->lnb_drift = -fepriv->lnb_drift;
446 fepriv->auto_sub_step = 0;
450 if (!ready) fepriv->auto_sub_step++;
453 /* if this attempt would hit where we started, indicate a complete
454 * iteration has occurred */
455 if ((fepriv->auto_step == fepriv->started_auto_step) &&
456 (fepriv->auto_sub_step == 0) && check_wrapped) {
460 dev_dbg(fe->dvb->device,
461 "%s: drift:%i inversion:%i auto_step:%i auto_sub_step:%i started_auto_step:%i\n",
462 __func__, fepriv->lnb_drift, fepriv->inversion,
463 fepriv->auto_step, fepriv->auto_sub_step,
464 fepriv->started_auto_step);
466 /* set the frontend itself */
467 c->frequency += fepriv->lnb_drift;
469 c->inversion = fepriv->inversion;
471 if (fe->ops.set_frontend)
472 fe_set_err = fe->ops.set_frontend(fe);
474 if (fe_set_err < 0) {
475 fepriv->state = FESTATE_ERROR;
479 c->frequency = original_frequency;
480 c->inversion = original_inversion;
482 fepriv->auto_sub_step++;
486 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
488 enum fe_status s = FE_NONE;
490 struct dvb_frontend_private *fepriv = fe->frontend_priv;
491 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
493 if (fepriv->max_drift)
494 dev_warn_once(fe->dvb->device,
495 "Frontend requested software zigzag, but didn't set the frequency step size\n");
497 /* if we've got no parameters, just keep idling */
498 if (fepriv->state & FESTATE_IDLE) {
499 fepriv->delay = 3 * HZ;
504 /* in SCAN mode, we just set the frontend when asked and leave it alone */
505 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
506 if (fepriv->state & FESTATE_RETUNE) {
508 if (fe->ops.set_frontend)
509 retval = fe->ops.set_frontend(fe);
512 fepriv->state = FESTATE_ERROR;
514 fepriv->state = FESTATE_TUNED;
516 fepriv->delay = 3 * HZ;
521 /* get the frontend status */
522 if (fepriv->state & FESTATE_RETUNE) {
525 if (fe->ops.read_status)
526 fe->ops.read_status(fe, &s);
527 if (s != fepriv->status) {
528 dvb_frontend_add_event(fe, s);
533 /* if we're not tuned, and we have a lock, move to the TUNED state */
534 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
535 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
536 fepriv->state = FESTATE_TUNED;
538 /* if we're tuned, then we have determined the correct inversion */
539 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
540 (c->inversion == INVERSION_AUTO)) {
541 c->inversion = fepriv->inversion;
546 /* if we are tuned already, check we're still locked */
547 if (fepriv->state & FESTATE_TUNED) {
548 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
550 /* we're tuned, and the lock is still good... */
551 if (s & FE_HAS_LOCK) {
553 } else { /* if we _WERE_ tuned, but now don't have a lock */
554 fepriv->state = FESTATE_ZIGZAG_FAST;
555 fepriv->started_auto_step = fepriv->auto_step;
556 fepriv->check_wrapped = 0;
560 /* don't actually do anything if we're in the LOSTLOCK state,
561 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
562 if ((fepriv->state & FESTATE_LOSTLOCK) &&
563 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
564 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
568 /* don't do anything if we're in the DISEQC state, since this
569 * might be someone with a motorized dish controlled by DISEQC.
570 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
571 if (fepriv->state & FESTATE_DISEQC) {
572 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
576 /* if we're in the RETUNE state, set everything up for a brand
577 * new scan, keeping the current inversion setting, as the next
578 * tune is _very_ likely to require the same */
579 if (fepriv->state & FESTATE_RETUNE) {
580 fepriv->lnb_drift = 0;
581 fepriv->auto_step = 0;
582 fepriv->auto_sub_step = 0;
583 fepriv->started_auto_step = 0;
584 fepriv->check_wrapped = 0;
588 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
589 fepriv->delay = fepriv->min_delay;
592 retval = dvb_frontend_swzigzag_autotune(fe,
593 fepriv->check_wrapped);
597 /* OK, if we've run out of trials at the fast speed.
598 * Drop back to slow for the _next_ attempt */
599 fepriv->state = FESTATE_SEARCHING_SLOW;
600 fepriv->started_auto_step = fepriv->auto_step;
603 fepriv->check_wrapped = 1;
605 /* if we've just re-tuned, enter the ZIGZAG_FAST state.
606 * This ensures we cannot return from an
607 * FE_SET_FRONTEND ioctl before the first frontend tune
609 if (fepriv->state & FESTATE_RETUNE) {
610 fepriv->state = FESTATE_TUNING_FAST;
615 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
616 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
618 /* Note: don't bother checking for wrapping; we stay in this
619 * state until we get a lock */
620 dvb_frontend_swzigzag_autotune(fe, 0);
624 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
626 struct dvb_frontend_private *fepriv = fe->frontend_priv;
628 if (fe->exit != DVB_FE_NO_EXIT)
631 if (fepriv->dvbdev->writers == 1)
632 if (time_after_eq(jiffies, fepriv->release_jiffies +
633 dvb_shutdown_timeout * HZ))
639 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
641 struct dvb_frontend_private *fepriv = fe->frontend_priv;
643 if (fepriv->wakeup) {
647 return dvb_frontend_is_exiting(fe);
650 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
652 struct dvb_frontend_private *fepriv = fe->frontend_priv;
655 wake_up_interruptible(&fepriv->wait_queue);
658 static int dvb_frontend_thread(void *data)
660 struct dvb_frontend *fe = data;
661 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
662 struct dvb_frontend_private *fepriv = fe->frontend_priv;
663 enum fe_status s = FE_NONE;
664 enum dvbfe_algo algo;
665 bool re_tune = false;
666 bool semheld = false;
668 dev_dbg(fe->dvb->device, "%s:\n", __func__);
670 fepriv->check_wrapped = 0;
672 fepriv->delay = 3 * HZ;
675 fepriv->reinitialise = 0;
677 dvb_frontend_init(fe);
681 up(&fepriv->sem); /* is locked when we enter the thread... */
682 wait_event_freezable_timeout(fepriv->wait_queue,
683 dvb_frontend_should_wakeup(fe) ||
684 kthread_should_stop(),
687 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
688 /* got signal or quitting */
689 if (!down_interruptible(&fepriv->sem))
691 fe->exit = DVB_FE_NORMAL_EXIT;
695 if (down_interruptible(&fepriv->sem))
698 if (fepriv->reinitialise) {
699 dvb_frontend_init(fe);
700 if (fe->ops.set_tone && fepriv->tone != -1)
701 fe->ops.set_tone(fe, fepriv->tone);
702 if (fe->ops.set_voltage && fepriv->voltage != -1)
703 fe->ops.set_voltage(fe, fepriv->voltage);
704 fepriv->reinitialise = 0;
707 /* do an iteration of the tuning loop */
708 if (fe->ops.get_frontend_algo) {
709 algo = fe->ops.get_frontend_algo(fe);
712 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
714 if (fepriv->state & FESTATE_RETUNE) {
715 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
717 fepriv->state = FESTATE_TUNED;
723 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
725 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
726 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
727 dvb_frontend_add_event(fe, s);
732 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
733 dvb_frontend_swzigzag(fe);
735 case DVBFE_ALGO_CUSTOM:
736 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
737 if (fepriv->state & FESTATE_RETUNE) {
738 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
739 fepriv->state = FESTATE_TUNED;
741 /* Case where we are going to search for a carrier
742 * User asked us to retune again for some reason, possibly
743 * requesting a search with a new set of parameters
745 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
746 if (fe->ops.search) {
747 fepriv->algo_status = fe->ops.search(fe);
748 /* We did do a search as was requested, the flags are
749 * now unset as well and has the flags wrt to search.
752 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
755 /* Track the carrier if the search was successful */
756 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
757 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
758 fepriv->delay = HZ / 2;
760 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
761 fe->ops.read_status(fe, &s);
762 if (s != fepriv->status) {
763 dvb_frontend_add_event(fe, s); /* update event list */
765 if (!(s & FE_HAS_LOCK)) {
766 fepriv->delay = HZ / 10;
767 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
769 fepriv->delay = 60 * HZ;
774 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
778 dvb_frontend_swzigzag(fe);
782 if (dvb_powerdown_on_sleep) {
783 if (fe->ops.set_voltage)
784 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
785 if (fe->ops.tuner_ops.sleep) {
786 if (fe->ops.i2c_gate_ctrl)
787 fe->ops.i2c_gate_ctrl(fe, 1);
788 fe->ops.tuner_ops.sleep(fe);
789 if (fe->ops.i2c_gate_ctrl)
790 fe->ops.i2c_gate_ctrl(fe, 0);
796 fepriv->thread = NULL;
797 if (kthread_should_stop())
798 fe->exit = DVB_FE_DEVICE_REMOVED;
800 fe->exit = DVB_FE_NO_EXIT;
805 dvb_frontend_wakeup(fe);
809 static void dvb_frontend_stop(struct dvb_frontend *fe)
811 struct dvb_frontend_private *fepriv = fe->frontend_priv;
813 dev_dbg(fe->dvb->device, "%s:\n", __func__);
815 if (fe->exit != DVB_FE_DEVICE_REMOVED)
816 fe->exit = DVB_FE_NORMAL_EXIT;
822 kthread_stop(fepriv->thread);
824 sema_init(&fepriv->sem, 1);
825 fepriv->state = FESTATE_IDLE;
827 /* paranoia check in case a signal arrived */
829 dev_warn(fe->dvb->device,
830 "dvb_frontend_stop: warning: thread %p won't exit\n",
835 * Sleep for the amount of time given by add_usec parameter
837 * This needs to be as precise as possible, as it affects the detection of
838 * the dish tone command at the satellite subsystem. The precision is improved
839 * by using a scheduled msleep followed by udelay for the remainder.
841 void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
845 *waketime = ktime_add_us(*waketime, add_usec);
846 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
848 msleep((delta - 1500) / 1000);
849 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
854 EXPORT_SYMBOL(dvb_frontend_sleep_until);
856 static int dvb_frontend_start(struct dvb_frontend *fe)
859 struct dvb_frontend_private *fepriv = fe->frontend_priv;
860 struct task_struct *fe_thread;
862 dev_dbg(fe->dvb->device, "%s:\n", __func__);
864 if (fepriv->thread) {
865 if (fe->exit == DVB_FE_NO_EXIT)
868 dvb_frontend_stop(fe);
871 if (signal_pending(current))
873 if (down_interruptible(&fepriv->sem))
876 fepriv->state = FESTATE_IDLE;
877 fe->exit = DVB_FE_NO_EXIT;
878 fepriv->thread = NULL;
881 fe_thread = kthread_run(dvb_frontend_thread, fe,
882 "kdvb-ad-%i-fe-%i", fe->dvb->num, fe->id);
883 if (IS_ERR(fe_thread)) {
884 ret = PTR_ERR(fe_thread);
885 dev_warn(fe->dvb->device,
886 "dvb_frontend_start: failed to start kthread (%d)\n",
891 fepriv->thread = fe_thread;
895 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
896 u32 *freq_min, u32 *freq_max,
899 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
900 u32 tuner_min = fe->ops.tuner_ops.info.frequency_min_hz;
901 u32 tuner_max = fe->ops.tuner_ops.info.frequency_max_hz;
902 u32 frontend_min = fe->ops.info.frequency_min_hz;
903 u32 frontend_max = fe->ops.info.frequency_max_hz;
905 *freq_min = max(frontend_min, tuner_min);
907 if (frontend_max == 0)
908 *freq_max = tuner_max;
909 else if (tuner_max == 0)
910 *freq_max = frontend_max;
912 *freq_max = min(frontend_max, tuner_max);
914 if (*freq_min == 0 || *freq_max == 0)
915 dev_warn(fe->dvb->device,
916 "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
917 fe->dvb->num, fe->id);
919 dev_dbg(fe->dvb->device, "frequency interval: tuner: %u...%u, frontend: %u...%u",
920 tuner_min, tuner_max, frontend_min, frontend_max);
922 /* If the standard is for satellite, convert frequencies to kHz */
923 switch (c->delivery_system) {
932 *tolerance = fe->ops.info.frequency_tolerance_hz / kHz;
937 *tolerance = fe->ops.info.frequency_tolerance_hz;
942 static u32 dvb_frontend_get_stepsize(struct dvb_frontend *fe)
944 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
945 u32 fe_step = fe->ops.info.frequency_stepsize_hz;
946 u32 tuner_step = fe->ops.tuner_ops.info.frequency_step_hz;
947 u32 step = max(fe_step, tuner_step);
949 switch (c->delivery_system) {
964 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
966 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
970 /* range check: frequency */
971 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max, NULL);
972 if ((freq_min && c->frequency < freq_min) ||
973 (freq_max && c->frequency > freq_max)) {
974 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
975 fe->dvb->num, fe->id, c->frequency,
980 /* range check: symbol rate */
981 switch (c->delivery_system) {
986 case SYS_DVBC_ANNEX_A:
987 case SYS_DVBC_ANNEX_C:
988 if ((fe->ops.info.symbol_rate_min &&
989 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
990 (fe->ops.info.symbol_rate_max &&
991 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
992 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
993 fe->dvb->num, fe->id, c->symbol_rate,
994 fe->ops.info.symbol_rate_min,
995 fe->ops.info.symbol_rate_max);
1006 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
1008 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1012 delsys = c->delivery_system;
1013 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
1014 c->delivery_system = delsys;
1016 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
1017 __func__, c->delivery_system);
1019 c->transmission_mode = TRANSMISSION_MODE_AUTO;
1020 c->bandwidth_hz = 0; /* AUTO */
1021 c->guard_interval = GUARD_INTERVAL_AUTO;
1022 c->hierarchy = HIERARCHY_AUTO;
1024 c->code_rate_HP = FEC_AUTO;
1025 c->code_rate_LP = FEC_AUTO;
1026 c->fec_inner = FEC_AUTO;
1027 c->rolloff = ROLLOFF_AUTO;
1028 c->voltage = SEC_VOLTAGE_OFF;
1029 c->sectone = SEC_TONE_OFF;
1030 c->pilot = PILOT_AUTO;
1032 c->isdbt_partial_reception = 0;
1033 c->isdbt_sb_mode = 0;
1034 c->isdbt_sb_subchannel = 0;
1035 c->isdbt_sb_segment_idx = 0;
1036 c->isdbt_sb_segment_count = 0;
1037 c->isdbt_layer_enabled = 7; /* All layers (A,B,C) */
1038 for (i = 0; i < 3; i++) {
1039 c->layer[i].fec = FEC_AUTO;
1040 c->layer[i].modulation = QAM_AUTO;
1041 c->layer[i].interleaving = 0;
1042 c->layer[i].segment_count = 0;
1045 c->stream_id = NO_STREAM_ID_FILTER;
1046 c->scrambling_sequence_index = 0;/* default sequence */
1048 switch (c->delivery_system) {
1050 c->modulation = QPSK;
1051 c->rolloff = ROLLOFF_20;
1056 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1057 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1060 c->modulation = VSB_8;
1063 c->symbol_rate = 28860000;
1064 c->rolloff = ROLLOFF_35;
1065 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1068 c->modulation = QAM_AUTO;
1077 #define _DTV_CMD(n) \
1080 static char *dtv_cmds[DTV_MAX_COMMAND + 1] = {
1082 _DTV_CMD(DTV_CLEAR),
1085 _DTV_CMD(DTV_FREQUENCY),
1086 _DTV_CMD(DTV_BANDWIDTH_HZ),
1087 _DTV_CMD(DTV_MODULATION),
1088 _DTV_CMD(DTV_INVERSION),
1089 _DTV_CMD(DTV_DISEQC_MASTER),
1090 _DTV_CMD(DTV_SYMBOL_RATE),
1091 _DTV_CMD(DTV_INNER_FEC),
1092 _DTV_CMD(DTV_VOLTAGE),
1094 _DTV_CMD(DTV_PILOT),
1095 _DTV_CMD(DTV_ROLLOFF),
1096 _DTV_CMD(DTV_DELIVERY_SYSTEM),
1097 _DTV_CMD(DTV_HIERARCHY),
1098 _DTV_CMD(DTV_CODE_RATE_HP),
1099 _DTV_CMD(DTV_CODE_RATE_LP),
1100 _DTV_CMD(DTV_GUARD_INTERVAL),
1101 _DTV_CMD(DTV_TRANSMISSION_MODE),
1102 _DTV_CMD(DTV_INTERLEAVING),
1104 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION),
1105 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING),
1106 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID),
1107 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX),
1108 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT),
1109 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED),
1110 _DTV_CMD(DTV_ISDBT_LAYERA_FEC),
1111 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION),
1112 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT),
1113 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING),
1114 _DTV_CMD(DTV_ISDBT_LAYERB_FEC),
1115 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION),
1116 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT),
1117 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING),
1118 _DTV_CMD(DTV_ISDBT_LAYERC_FEC),
1119 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION),
1120 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT),
1121 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING),
1123 _DTV_CMD(DTV_STREAM_ID),
1124 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY),
1125 _DTV_CMD(DTV_SCRAMBLING_SEQUENCE_INDEX),
1129 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY),
1130 _DTV_CMD(DTV_API_VERSION),
1132 _DTV_CMD(DTV_ENUM_DELSYS),
1134 _DTV_CMD(DTV_ATSCMH_PARADE_ID),
1135 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE),
1137 _DTV_CMD(DTV_ATSCMH_FIC_VER),
1138 _DTV_CMD(DTV_ATSCMH_NOG),
1139 _DTV_CMD(DTV_ATSCMH_TNOG),
1140 _DTV_CMD(DTV_ATSCMH_SGN),
1141 _DTV_CMD(DTV_ATSCMH_PRC),
1142 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE),
1143 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI),
1144 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC),
1145 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE),
1146 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A),
1147 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B),
1148 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C),
1149 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D),
1151 /* Statistics API */
1152 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH),
1153 _DTV_CMD(DTV_STAT_CNR),
1154 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT),
1155 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT),
1156 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT),
1157 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT),
1158 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT),
1159 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT),
1162 static char *dtv_cmd_name(u32 cmd)
1164 cmd = array_index_nospec(cmd, DTV_MAX_COMMAND);
1165 return dtv_cmds[cmd];
1168 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1169 * drivers can use a single set_frontend tuning function, regardless of whether
1170 * it's being used for the legacy or new API, reducing code and complexity.
1172 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1173 struct dtv_frontend_properties *c,
1174 const struct dvb_frontend_parameters *p)
1176 c->frequency = p->frequency;
1177 c->inversion = p->inversion;
1179 switch (dvbv3_type(c->delivery_system)) {
1181 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1182 c->symbol_rate = p->u.qpsk.symbol_rate;
1183 c->fec_inner = p->u.qpsk.fec_inner;
1186 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1187 c->symbol_rate = p->u.qam.symbol_rate;
1188 c->fec_inner = p->u.qam.fec_inner;
1189 c->modulation = p->u.qam.modulation;
1192 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1194 switch (p->u.ofdm.bandwidth) {
1195 case BANDWIDTH_10_MHZ:
1196 c->bandwidth_hz = 10000000;
1198 case BANDWIDTH_8_MHZ:
1199 c->bandwidth_hz = 8000000;
1201 case BANDWIDTH_7_MHZ:
1202 c->bandwidth_hz = 7000000;
1204 case BANDWIDTH_6_MHZ:
1205 c->bandwidth_hz = 6000000;
1207 case BANDWIDTH_5_MHZ:
1208 c->bandwidth_hz = 5000000;
1210 case BANDWIDTH_1_712_MHZ:
1211 c->bandwidth_hz = 1712000;
1213 case BANDWIDTH_AUTO:
1214 c->bandwidth_hz = 0;
1217 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1218 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1219 c->modulation = p->u.ofdm.constellation;
1220 c->transmission_mode = p->u.ofdm.transmission_mode;
1221 c->guard_interval = p->u.ofdm.guard_interval;
1222 c->hierarchy = p->u.ofdm.hierarchy_information;
1225 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1226 c->modulation = p->u.vsb.modulation;
1227 if (c->delivery_system == SYS_ATSCMH)
1229 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1230 c->delivery_system = SYS_ATSC;
1232 c->delivery_system = SYS_DVBC_ANNEX_B;
1235 dev_err(fe->dvb->device,
1236 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1237 __func__, c->delivery_system);
1244 /* Ensure the cached values are set correctly in the frontend
1245 * legacy tuning structures, for the advanced tuning API.
1248 dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1249 const struct dtv_frontend_properties *c,
1250 struct dvb_frontend_parameters *p)
1252 p->frequency = c->frequency;
1253 p->inversion = c->inversion;
1255 switch (dvbv3_type(c->delivery_system)) {
1257 dev_err(fe->dvb->device,
1258 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1259 __func__, c->delivery_system);
1262 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1263 p->u.qpsk.symbol_rate = c->symbol_rate;
1264 p->u.qpsk.fec_inner = c->fec_inner;
1267 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1268 p->u.qam.symbol_rate = c->symbol_rate;
1269 p->u.qam.fec_inner = c->fec_inner;
1270 p->u.qam.modulation = c->modulation;
1273 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1274 switch (c->bandwidth_hz) {
1276 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1279 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1282 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1285 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1288 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1291 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1295 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1297 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1298 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1299 p->u.ofdm.constellation = c->modulation;
1300 p->u.ofdm.transmission_mode = c->transmission_mode;
1301 p->u.ofdm.guard_interval = c->guard_interval;
1302 p->u.ofdm.hierarchy_information = c->hierarchy;
1305 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1306 p->u.vsb.modulation = c->modulation;
1313 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1314 * @fe: struct dvb_frontend pointer
1315 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1316 * @p_out: struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1318 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1319 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1320 * If p_out is not null, it will update the DVBv3 params pointed by it.
1322 static int dtv_get_frontend(struct dvb_frontend *fe,
1323 struct dtv_frontend_properties *c,
1324 struct dvb_frontend_parameters *p_out)
1328 if (fe->ops.get_frontend) {
1329 r = fe->ops.get_frontend(fe, c);
1330 if (unlikely(r < 0))
1333 dtv_property_legacy_params_sync(fe, c, p_out);
1337 /* As everything is in cache, get_frontend fops are always supported */
1341 static int dvb_frontend_handle_ioctl(struct file *file,
1342 unsigned int cmd, void *parg);
1344 static int dtv_property_process_get(struct dvb_frontend *fe,
1345 const struct dtv_frontend_properties *c,
1346 struct dtv_property *tvp,
1350 unsigned int len = 1;
1353 case DTV_ENUM_DELSYS:
1355 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1356 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1359 tvp->u.buffer.len = ncaps;
1363 tvp->u.data = c->frequency;
1365 case DTV_MODULATION:
1366 tvp->u.data = c->modulation;
1368 case DTV_BANDWIDTH_HZ:
1369 tvp->u.data = c->bandwidth_hz;
1372 tvp->u.data = c->inversion;
1374 case DTV_SYMBOL_RATE:
1375 tvp->u.data = c->symbol_rate;
1378 tvp->u.data = c->fec_inner;
1381 tvp->u.data = c->pilot;
1384 tvp->u.data = c->rolloff;
1386 case DTV_DELIVERY_SYSTEM:
1387 tvp->u.data = c->delivery_system;
1390 tvp->u.data = c->voltage;
1393 tvp->u.data = c->sectone;
1395 case DTV_API_VERSION:
1396 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1398 case DTV_CODE_RATE_HP:
1399 tvp->u.data = c->code_rate_HP;
1401 case DTV_CODE_RATE_LP:
1402 tvp->u.data = c->code_rate_LP;
1404 case DTV_GUARD_INTERVAL:
1405 tvp->u.data = c->guard_interval;
1407 case DTV_TRANSMISSION_MODE:
1408 tvp->u.data = c->transmission_mode;
1411 tvp->u.data = c->hierarchy;
1413 case DTV_INTERLEAVING:
1414 tvp->u.data = c->interleaving;
1417 /* ISDB-T Support here */
1418 case DTV_ISDBT_PARTIAL_RECEPTION:
1419 tvp->u.data = c->isdbt_partial_reception;
1421 case DTV_ISDBT_SOUND_BROADCASTING:
1422 tvp->u.data = c->isdbt_sb_mode;
1424 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1425 tvp->u.data = c->isdbt_sb_subchannel;
1427 case DTV_ISDBT_SB_SEGMENT_IDX:
1428 tvp->u.data = c->isdbt_sb_segment_idx;
1430 case DTV_ISDBT_SB_SEGMENT_COUNT:
1431 tvp->u.data = c->isdbt_sb_segment_count;
1433 case DTV_ISDBT_LAYER_ENABLED:
1434 tvp->u.data = c->isdbt_layer_enabled;
1436 case DTV_ISDBT_LAYERA_FEC:
1437 tvp->u.data = c->layer[0].fec;
1439 case DTV_ISDBT_LAYERA_MODULATION:
1440 tvp->u.data = c->layer[0].modulation;
1442 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1443 tvp->u.data = c->layer[0].segment_count;
1445 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1446 tvp->u.data = c->layer[0].interleaving;
1448 case DTV_ISDBT_LAYERB_FEC:
1449 tvp->u.data = c->layer[1].fec;
1451 case DTV_ISDBT_LAYERB_MODULATION:
1452 tvp->u.data = c->layer[1].modulation;
1454 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1455 tvp->u.data = c->layer[1].segment_count;
1457 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1458 tvp->u.data = c->layer[1].interleaving;
1460 case DTV_ISDBT_LAYERC_FEC:
1461 tvp->u.data = c->layer[2].fec;
1463 case DTV_ISDBT_LAYERC_MODULATION:
1464 tvp->u.data = c->layer[2].modulation;
1466 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1467 tvp->u.data = c->layer[2].segment_count;
1469 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1470 tvp->u.data = c->layer[2].interleaving;
1473 /* Multistream support */
1475 case DTV_DVBT2_PLP_ID_LEGACY:
1476 tvp->u.data = c->stream_id;
1479 /* Physical layer scrambling support */
1480 case DTV_SCRAMBLING_SEQUENCE_INDEX:
1481 tvp->u.data = c->scrambling_sequence_index;
1485 case DTV_ATSCMH_FIC_VER:
1486 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1488 case DTV_ATSCMH_PARADE_ID:
1489 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1491 case DTV_ATSCMH_NOG:
1492 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1494 case DTV_ATSCMH_TNOG:
1495 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1497 case DTV_ATSCMH_SGN:
1498 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1500 case DTV_ATSCMH_PRC:
1501 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1503 case DTV_ATSCMH_RS_FRAME_MODE:
1504 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1506 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1507 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1509 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1510 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1512 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1513 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1515 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1516 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1518 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1519 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1521 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1522 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1524 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1525 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1527 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1528 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1532 tvp->u.data = c->lna;
1535 /* Fill quality measures */
1536 case DTV_STAT_SIGNAL_STRENGTH:
1537 tvp->u.st = c->strength;
1538 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1539 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1540 len = tvp->u.buffer.len;
1544 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1545 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1546 len = tvp->u.buffer.len;
1548 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1549 tvp->u.st = c->pre_bit_error;
1550 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1551 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1552 len = tvp->u.buffer.len;
1554 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1555 tvp->u.st = c->pre_bit_count;
1556 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1557 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1558 len = tvp->u.buffer.len;
1560 case DTV_STAT_POST_ERROR_BIT_COUNT:
1561 tvp->u.st = c->post_bit_error;
1562 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1563 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1564 len = tvp->u.buffer.len;
1566 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1567 tvp->u.st = c->post_bit_count;
1568 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1569 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1570 len = tvp->u.buffer.len;
1572 case DTV_STAT_ERROR_BLOCK_COUNT:
1573 tvp->u.st = c->block_error;
1574 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1575 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1576 len = tvp->u.buffer.len;
1578 case DTV_STAT_TOTAL_BLOCK_COUNT:
1579 tvp->u.st = c->block_count;
1580 if (tvp->u.buffer.len > MAX_DTV_STATS * sizeof(u32))
1581 tvp->u.buffer.len = MAX_DTV_STATS * sizeof(u32);
1582 len = tvp->u.buffer.len;
1585 dev_dbg(fe->dvb->device,
1586 "%s: FE property %d doesn't exist\n",
1587 __func__, tvp->cmd);
1594 dev_dbg(fe->dvb->device,
1595 "%s: GET cmd 0x%08x (%s) len %d: %*ph\n",
1596 __func__, tvp->cmd, dtv_cmd_name(tvp->cmd),
1597 tvp->u.buffer.len, tvp->u.buffer.len, tvp->u.buffer.data);
1602 static int dtv_set_frontend(struct dvb_frontend *fe);
1604 static bool is_dvbv3_delsys(u32 delsys)
1606 return (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1607 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1611 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1612 * @fe: struct frontend;
1613 * @delsys: DVBv5 type that will be used for emulation
1615 * Provides emulation for delivery systems that are compatible with the old
1616 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1617 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontend
1618 * parameters are compatible with DVB-S spec.
1620 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1623 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1625 c->delivery_system = delsys;
1628 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1630 if (c->delivery_system == SYS_ISDBT) {
1631 dev_dbg(fe->dvb->device,
1632 "%s: Using defaults for SYS_ISDBT\n",
1635 if (!c->bandwidth_hz)
1636 c->bandwidth_hz = 6000000;
1638 c->isdbt_partial_reception = 0;
1639 c->isdbt_sb_mode = 0;
1640 c->isdbt_sb_subchannel = 0;
1641 c->isdbt_sb_segment_idx = 0;
1642 c->isdbt_sb_segment_count = 0;
1643 c->isdbt_layer_enabled = 7;
1644 for (i = 0; i < 3; i++) {
1645 c->layer[i].fec = FEC_AUTO;
1646 c->layer[i].modulation = QAM_AUTO;
1647 c->layer[i].interleaving = 0;
1648 c->layer[i].segment_count = 0;
1651 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1652 __func__, c->delivery_system);
1658 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1659 * @fe: frontend struct
1660 * @desired_system: delivery system requested by the user
1662 * A DVBv5 call know what's the desired system it wants. So, set it.
1664 * There are, however, a few known issues with early DVBv5 applications that
1665 * are also handled by this logic:
1667 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1668 * This is an API violation, but, as we don't want to break userspace,
1669 * convert it to the first supported delivery system.
1670 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1671 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1672 * ISDB-T provided backward compat with DVB-T.
1674 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1678 u32 delsys = SYS_UNDEFINED;
1679 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1680 enum dvbv3_emulation_type type;
1683 * It was reported that some old DVBv5 applications were
1684 * filling delivery_system with SYS_UNDEFINED. If this happens,
1685 * assume that the application wants to use the first supported
1688 if (desired_system == SYS_UNDEFINED)
1689 desired_system = fe->ops.delsys[0];
1692 * This is a DVBv5 call. So, it likely knows the supported
1693 * delivery systems. So, check if the desired delivery system is
1697 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1698 if (fe->ops.delsys[ncaps] == desired_system) {
1699 c->delivery_system = desired_system;
1700 dev_dbg(fe->dvb->device,
1701 "%s: Changing delivery system to %d\n",
1702 __func__, desired_system);
1709 * The requested delivery system isn't supported. Maybe userspace
1710 * is requesting a DVBv3 compatible delivery system.
1712 * The emulation only works if the desired system is one of the
1713 * delivery systems supported by DVBv3 API
1715 if (!is_dvbv3_delsys(desired_system)) {
1716 dev_dbg(fe->dvb->device,
1717 "%s: Delivery system %d not supported.\n",
1718 __func__, desired_system);
1722 type = dvbv3_type(desired_system);
1725 * Get the last non-DVBv3 delivery system that has the same type
1726 * of the desired system
1729 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1730 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1731 delsys = fe->ops.delsys[ncaps];
1735 /* There's nothing compatible with the desired delivery system */
1736 if (delsys == SYS_UNDEFINED) {
1737 dev_dbg(fe->dvb->device,
1738 "%s: Delivery system %d not supported on emulation mode.\n",
1739 __func__, desired_system);
1743 dev_dbg(fe->dvb->device,
1744 "%s: Using delivery system %d emulated as if it were %d\n",
1745 __func__, delsys, desired_system);
1747 return emulate_delivery_system(fe, desired_system);
1751 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1752 * @fe: frontend struct
1754 * A DVBv3 call doesn't know what's the desired system it wants. It also
1755 * doesn't allow to switch between different types. Due to that, userspace
1756 * should use DVBv5 instead.
1757 * However, in order to avoid breaking userspace API, limited backward
1758 * compatibility support is provided.
1760 * There are some delivery systems that are incompatible with DVBv3 calls.
1762 * This routine should work fine for frontends that support just one delivery
1765 * For frontends that support multiple frontends:
1766 * 1) It defaults to use the first supported delivery system. There's an
1767 * userspace application that allows changing it at runtime;
1769 * 2) If the current delivery system is not compatible with DVBv3, it gets
1770 * the first one that it is compatible.
1772 * NOTE: in order for this to work with applications like Kaffeine that
1773 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1774 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1775 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1778 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1781 u32 delsys = SYS_UNDEFINED;
1782 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1784 /* If not set yet, defaults to the first supported delivery system */
1785 if (c->delivery_system == SYS_UNDEFINED)
1786 c->delivery_system = fe->ops.delsys[0];
1789 * Trivial case: just use the current one, if it already a DVBv3
1792 if (is_dvbv3_delsys(c->delivery_system)) {
1793 dev_dbg(fe->dvb->device,
1794 "%s: Using delivery system to %d\n",
1795 __func__, c->delivery_system);
1800 * Seek for the first delivery system that it is compatible with a
1804 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1805 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1806 delsys = fe->ops.delsys[ncaps];
1811 if (delsys == SYS_UNDEFINED) {
1812 dev_dbg(fe->dvb->device,
1813 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1817 return emulate_delivery_system(fe, delsys);
1820 static void prepare_tuning_algo_parameters(struct dvb_frontend *fe)
1822 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1823 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1824 struct dvb_frontend_tune_settings fetunesettings = { 0 };
1826 /* get frontend-specific tuning settings */
1827 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1828 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1829 fepriv->max_drift = fetunesettings.max_drift;
1830 fepriv->step_size = fetunesettings.step_size;
1832 /* default values */
1833 switch (c->delivery_system) {
1839 case SYS_DVBC_ANNEX_A:
1840 case SYS_DVBC_ANNEX_C:
1841 fepriv->min_delay = HZ / 20;
1842 fepriv->step_size = c->symbol_rate / 16000;
1843 fepriv->max_drift = c->symbol_rate / 2000;
1849 fepriv->min_delay = HZ / 20;
1850 fepriv->step_size = dvb_frontend_get_stepsize(fe) * 2;
1851 fepriv->max_drift = fepriv->step_size + 1;
1855 * FIXME: This sounds wrong! if freqency_stepsize is
1856 * defined by the frontend, why not use it???
1858 fepriv->min_delay = HZ / 20;
1859 fepriv->step_size = 0; /* no zigzag */
1860 fepriv->max_drift = 0;
1864 if (dvb_override_tune_delay > 0)
1865 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1869 * dtv_property_process_set - Sets a single DTV property
1870 * @fe: Pointer to &struct dvb_frontend
1871 * @file: Pointer to &struct file
1872 * @cmd: Digital TV command
1873 * @data: An unsigned 32-bits number
1875 * This routine assigns the property
1876 * value to the corresponding member of
1877 * &struct dtv_frontend_properties
1880 * Zero on success, negative errno on failure.
1882 static int dtv_property_process_set(struct dvb_frontend *fe,
1887 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1889 /** Dump DTV command name and value*/
1890 if (!cmd || cmd > DTV_MAX_COMMAND)
1891 dev_warn(fe->dvb->device, "%s: SET cmd 0x%08x undefined\n",
1894 dev_dbg(fe->dvb->device,
1895 "%s: SET cmd 0x%08x (%s) to 0x%08x\n",
1896 __func__, cmd, dtv_cmd_name(cmd), data);
1900 * Reset a cache of data specific to the frontend here. This does
1901 * not effect hardware.
1903 dvb_frontend_clear_cache(fe);
1907 * Use the cached Digital TV properties to tune the
1910 dev_dbg(fe->dvb->device,
1911 "%s: Setting the frontend from property cache\n",
1914 r = dtv_set_frontend(fe);
1917 c->frequency = data;
1919 case DTV_MODULATION:
1920 c->modulation = data;
1922 case DTV_BANDWIDTH_HZ:
1923 c->bandwidth_hz = data;
1926 c->inversion = data;
1928 case DTV_SYMBOL_RATE:
1929 c->symbol_rate = data;
1932 c->fec_inner = data;
1940 case DTV_DELIVERY_SYSTEM:
1941 r = dvbv5_set_delivery_system(fe, data);
1945 r = dvb_frontend_handle_ioctl(file, FE_SET_VOLTAGE,
1946 (void *)c->voltage);
1950 r = dvb_frontend_handle_ioctl(file, FE_SET_TONE,
1951 (void *)c->sectone);
1953 case DTV_CODE_RATE_HP:
1954 c->code_rate_HP = data;
1956 case DTV_CODE_RATE_LP:
1957 c->code_rate_LP = data;
1959 case DTV_GUARD_INTERVAL:
1960 c->guard_interval = data;
1962 case DTV_TRANSMISSION_MODE:
1963 c->transmission_mode = data;
1966 c->hierarchy = data;
1968 case DTV_INTERLEAVING:
1969 c->interleaving = data;
1972 /* ISDB-T Support here */
1973 case DTV_ISDBT_PARTIAL_RECEPTION:
1974 c->isdbt_partial_reception = data;
1976 case DTV_ISDBT_SOUND_BROADCASTING:
1977 c->isdbt_sb_mode = data;
1979 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1980 c->isdbt_sb_subchannel = data;
1982 case DTV_ISDBT_SB_SEGMENT_IDX:
1983 c->isdbt_sb_segment_idx = data;
1985 case DTV_ISDBT_SB_SEGMENT_COUNT:
1986 c->isdbt_sb_segment_count = data;
1988 case DTV_ISDBT_LAYER_ENABLED:
1989 c->isdbt_layer_enabled = data;
1991 case DTV_ISDBT_LAYERA_FEC:
1992 c->layer[0].fec = data;
1994 case DTV_ISDBT_LAYERA_MODULATION:
1995 c->layer[0].modulation = data;
1997 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1998 c->layer[0].segment_count = data;
2000 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
2001 c->layer[0].interleaving = data;
2003 case DTV_ISDBT_LAYERB_FEC:
2004 c->layer[1].fec = data;
2006 case DTV_ISDBT_LAYERB_MODULATION:
2007 c->layer[1].modulation = data;
2009 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
2010 c->layer[1].segment_count = data;
2012 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
2013 c->layer[1].interleaving = data;
2015 case DTV_ISDBT_LAYERC_FEC:
2016 c->layer[2].fec = data;
2018 case DTV_ISDBT_LAYERC_MODULATION:
2019 c->layer[2].modulation = data;
2021 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
2022 c->layer[2].segment_count = data;
2024 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
2025 c->layer[2].interleaving = data;
2028 /* Multistream support */
2030 case DTV_DVBT2_PLP_ID_LEGACY:
2031 c->stream_id = data;
2034 /* Physical layer scrambling support */
2035 case DTV_SCRAMBLING_SEQUENCE_INDEX:
2036 c->scrambling_sequence_index = data;
2040 case DTV_ATSCMH_PARADE_ID:
2041 fe->dtv_property_cache.atscmh_parade_id = data;
2043 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
2044 fe->dtv_property_cache.atscmh_rs_frame_ensemble = data;
2049 if (fe->ops.set_lna)
2050 r = fe->ops.set_lna(fe);
2062 static int dvb_frontend_do_ioctl(struct file *file, unsigned int cmd,
2065 struct dvb_device *dvbdev = file->private_data;
2066 struct dvb_frontend *fe = dvbdev->priv;
2067 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2070 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
2071 if (down_interruptible(&fepriv->sem))
2072 return -ERESTARTSYS;
2074 if (fe->exit != DVB_FE_NO_EXIT) {
2080 * If the frontend is opened in read-only mode, only the ioctls
2081 * that don't interfere with the tune logic should be accepted.
2082 * That allows an external application to monitor the DVB QoS and
2083 * statistics parameters.
2085 * That matches all _IOR() ioctls, except for two special cases:
2086 * - FE_GET_EVENT is part of the tuning logic on a DVB application;
2087 * - FE_DISEQC_RECV_SLAVE_REPLY is part of DiSEqC 2.0
2089 * So, those two ioctls should also return -EPERM, as otherwise
2090 * reading from them would interfere with a DVB tune application
2092 if ((file->f_flags & O_ACCMODE) == O_RDONLY
2093 && (_IOC_DIR(cmd) != _IOC_READ
2094 || cmd == FE_GET_EVENT
2095 || cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
2100 err = dvb_frontend_handle_ioctl(file, cmd, parg);
2106 static long dvb_frontend_ioctl(struct file *file, unsigned int cmd,
2109 struct dvb_device *dvbdev = file->private_data;
2114 return dvb_usercopy(file, cmd, arg, dvb_frontend_do_ioctl);
2117 #ifdef CONFIG_COMPAT
2118 struct compat_dtv_property {
2123 struct dtv_fe_stats st;
2128 compat_uptr_t reserved2;
2132 } __attribute__ ((packed));
2134 struct compat_dtv_properties {
2136 compat_uptr_t props;
2139 #define COMPAT_FE_SET_PROPERTY _IOW('o', 82, struct compat_dtv_properties)
2140 #define COMPAT_FE_GET_PROPERTY _IOR('o', 83, struct compat_dtv_properties)
2142 static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd,
2145 struct dvb_device *dvbdev = file->private_data;
2146 struct dvb_frontend *fe = dvbdev->priv;
2147 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2150 if (cmd == COMPAT_FE_SET_PROPERTY) {
2151 struct compat_dtv_properties prop, *tvps = NULL;
2152 struct compat_dtv_property *tvp = NULL;
2154 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
2160 * Put an arbitrary limit on the number of messages that can
2163 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
2166 tvp = memdup_array_user(compat_ptr(tvps->props),
2167 tvps->num, sizeof(*tvp));
2169 return PTR_ERR(tvp);
2171 for (i = 0; i < tvps->num; i++) {
2172 err = dtv_property_process_set(fe, file,
2181 } else if (cmd == COMPAT_FE_GET_PROPERTY) {
2182 struct compat_dtv_properties prop, *tvps = NULL;
2183 struct compat_dtv_property *tvp = NULL;
2184 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2186 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
2192 * Put an arbitrary limit on the number of messages that can
2195 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
2198 tvp = memdup_array_user(compat_ptr(tvps->props),
2199 tvps->num, sizeof(*tvp));
2201 return PTR_ERR(tvp);
2204 * Let's use our own copy of property cache, in order to
2205 * avoid mangling with DTV zigzag logic, as drivers might
2206 * return crap, if they don't check if the data is available
2207 * before updating the properties cache.
2209 if (fepriv->state != FESTATE_IDLE) {
2210 err = dtv_get_frontend(fe, &getp, NULL);
2216 for (i = 0; i < tvps->num; i++) {
2217 err = dtv_property_process_get(
2218 fe, &getp, (struct dtv_property *)(tvp + i), file);
2225 if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
2226 tvps->num * sizeof(struct compat_dtv_property))) {
2236 static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd,
2239 struct dvb_device *dvbdev = file->private_data;
2240 struct dvb_frontend *fe = dvbdev->priv;
2241 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2244 if (cmd == COMPAT_FE_SET_PROPERTY || cmd == COMPAT_FE_GET_PROPERTY) {
2245 if (down_interruptible(&fepriv->sem))
2246 return -ERESTARTSYS;
2248 err = dvb_frontend_handle_compat_ioctl(file, cmd, arg);
2254 return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2258 static int dtv_set_frontend(struct dvb_frontend *fe)
2260 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2261 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2264 if (dvb_frontend_check_parameters(fe) < 0)
2268 * Initialize output parameters to match the values given by
2269 * the user. FE_SET_FRONTEND triggers an initial frontend event
2270 * with status = 0, which copies output parameters to userspace.
2272 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
2275 * Be sure that the bandwidth will be filled for all
2276 * non-satellite systems, as tuners need to know what
2277 * low pass/Nyquist half filter should be applied, in
2278 * order to avoid inter-channel noise.
2280 * ISDB-T and DVB-T/T2 already sets bandwidth.
2281 * ATSC and DVB-C don't set, so, the core should fill it.
2283 * On DVB-C Annex A and C, the bandwidth is a function of
2284 * the roll-off and symbol rate. Annex B defines different
2285 * roll-off factors depending on the modulation. Fortunately,
2286 * Annex B is only used with 6MHz, so there's no need to
2289 * While not officially supported, a side effect of handling it at
2290 * the cache level is that a program could retrieve the bandwidth
2291 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2293 switch (c->delivery_system) {
2295 case SYS_DVBC_ANNEX_B:
2296 c->bandwidth_hz = 6000000;
2298 case SYS_DVBC_ANNEX_A:
2301 case SYS_DVBC_ANNEX_C:
2313 switch (c->rolloff) {
2329 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
2331 /* force auto frequency inversion if requested */
2332 if (dvb_force_auto_inversion)
2333 c->inversion = INVERSION_AUTO;
2336 * without hierarchical coding code_rate_LP is irrelevant,
2337 * so we tolerate the otherwise invalid FEC_NONE setting
2339 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2340 c->code_rate_LP = FEC_AUTO;
2342 prepare_tuning_algo_parameters(fe);
2344 fepriv->state = FESTATE_RETUNE;
2346 /* Request the search algorithm to search */
2347 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2349 dvb_frontend_clear_events(fe);
2350 dvb_frontend_add_event(fe, 0);
2351 dvb_frontend_wakeup(fe);
2357 static int dvb_get_property(struct dvb_frontend *fe, struct file *file,
2358 struct dtv_properties *tvps)
2360 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2361 struct dtv_property *tvp = NULL;
2362 struct dtv_frontend_properties getp;
2365 memcpy(&getp, &fe->dtv_property_cache, sizeof(getp));
2367 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n",
2368 __func__, tvps->num);
2369 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n",
2370 __func__, tvps->props);
2373 * Put an arbitrary limit on the number of messages that can
2376 if (!tvps->num || tvps->num > DTV_IOCTL_MAX_MSGS)
2379 tvp = memdup_array_user((void __user *)tvps->props,
2380 tvps->num, sizeof(*tvp));
2382 return PTR_ERR(tvp);
2385 * Let's use our own copy of property cache, in order to
2386 * avoid mangling with DTV zigzag logic, as drivers might
2387 * return crap, if they don't check if the data is available
2388 * before updating the properties cache.
2390 if (fepriv->state != FESTATE_IDLE) {
2391 err = dtv_get_frontend(fe, &getp, NULL);
2395 for (i = 0; i < tvps->num; i++) {
2396 err = dtv_property_process_get(fe, &getp,
2402 if (copy_to_user((void __user *)tvps->props, tvp,
2403 tvps->num * sizeof(struct dtv_property))) {
2414 static int dvb_get_frontend(struct dvb_frontend *fe,
2415 struct dvb_frontend_parameters *p_out)
2417 struct dtv_frontend_properties getp;
2420 * Let's use our own copy of property cache, in order to
2421 * avoid mangling with DTV zigzag logic, as drivers might
2422 * return crap, if they don't check if the data is available
2423 * before updating the properties cache.
2425 memcpy(&getp, &fe->dtv_property_cache, sizeof(getp));
2427 return dtv_get_frontend(fe, &getp, p_out);
2430 static int dvb_frontend_handle_ioctl(struct file *file,
2431 unsigned int cmd, void *parg)
2433 struct dvb_device *dvbdev = file->private_data;
2434 struct dvb_frontend *fe = dvbdev->priv;
2435 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2436 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2437 int i, err = -ENOTSUPP;
2439 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2442 case FE_SET_PROPERTY: {
2443 struct dtv_properties *tvps = parg;
2444 struct dtv_property *tvp = NULL;
2446 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n",
2447 __func__, tvps->num);
2448 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n",
2449 __func__, tvps->props);
2452 * Put an arbitrary limit on the number of messages that can
2455 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
2458 tvp = memdup_array_user((void __user *)tvps->props,
2459 tvps->num, sizeof(*tvp));
2461 return PTR_ERR(tvp);
2463 for (i = 0; i < tvps->num; i++) {
2464 err = dtv_property_process_set(fe, file,
2476 case FE_GET_PROPERTY:
2477 err = dvb_get_property(fe, file, parg);
2481 struct dvb_frontend_info *info = parg;
2482 memset(info, 0, sizeof(*info));
2484 strscpy(info->name, fe->ops.info.name, sizeof(info->name));
2485 info->symbol_rate_min = fe->ops.info.symbol_rate_min;
2486 info->symbol_rate_max = fe->ops.info.symbol_rate_max;
2487 info->symbol_rate_tolerance = fe->ops.info.symbol_rate_tolerance;
2488 info->caps = fe->ops.info.caps;
2489 info->frequency_stepsize = dvb_frontend_get_stepsize(fe);
2490 dvb_frontend_get_frequency_limits(fe, &info->frequency_min,
2491 &info->frequency_max,
2492 &info->frequency_tolerance);
2495 * Associate the 4 delivery systems supported by DVBv3
2496 * API with their DVBv5 counterpart. For the other standards,
2497 * use the closest type, assuming that it would hopefully
2498 * work with a DVBv3 application.
2499 * It should be noticed that, on multi-frontend devices with
2500 * different types (terrestrial and cable, for example),
2501 * a pure DVBv3 application won't be able to use all delivery
2502 * systems. Yet, changing the DVBv5 cache to the other delivery
2503 * system should be enough for making it work.
2505 switch (dvbv3_type(c->delivery_system)) {
2507 info->type = FE_QPSK;
2510 info->type = FE_ATSC;
2513 info->type = FE_QAM;
2516 info->type = FE_OFDM;
2519 dev_err(fe->dvb->device,
2520 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2521 __func__, c->delivery_system);
2522 info->type = FE_OFDM;
2524 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2525 __func__, c->delivery_system, info->type);
2527 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2528 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2529 info->caps |= FE_CAN_INVERSION_AUTO;
2534 case FE_READ_STATUS: {
2535 enum fe_status *status = parg;
2537 /* if retune was requested but hasn't occurred yet, prevent
2538 * that user get signal state from previous tuning */
2539 if (fepriv->state == FESTATE_RETUNE ||
2540 fepriv->state == FESTATE_ERROR) {
2546 if (fe->ops.read_status)
2547 err = fe->ops.read_status(fe, status);
2551 case FE_DISEQC_RESET_OVERLOAD:
2552 if (fe->ops.diseqc_reset_overload) {
2553 err = fe->ops.diseqc_reset_overload(fe);
2554 fepriv->state = FESTATE_DISEQC;
2559 case FE_DISEQC_SEND_MASTER_CMD:
2560 if (fe->ops.diseqc_send_master_cmd) {
2561 struct dvb_diseqc_master_cmd *cmd = parg;
2563 if (cmd->msg_len > sizeof(cmd->msg)) {
2567 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
2568 fepriv->state = FESTATE_DISEQC;
2573 case FE_DISEQC_SEND_BURST:
2574 if (fe->ops.diseqc_send_burst) {
2575 err = fe->ops.diseqc_send_burst(fe, (long)parg);
2576 fepriv->state = FESTATE_DISEQC;
2582 if (fe->ops.set_tone) {
2583 fepriv->tone = (long)parg;
2584 err = fe->ops.set_tone(fe, fepriv->tone);
2585 fepriv->state = FESTATE_DISEQC;
2590 case FE_SET_VOLTAGE:
2591 if (fe->ops.set_voltage) {
2592 fepriv->voltage = (long)parg;
2593 err = fe->ops.set_voltage(fe, fepriv->voltage);
2594 fepriv->state = FESTATE_DISEQC;
2599 case FE_DISEQC_RECV_SLAVE_REPLY:
2600 if (fe->ops.diseqc_recv_slave_reply)
2601 err = fe->ops.diseqc_recv_slave_reply(fe, parg);
2604 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2605 if (fe->ops.enable_high_lnb_voltage)
2606 err = fe->ops.enable_high_lnb_voltage(fe, (long)parg);
2609 case FE_SET_FRONTEND_TUNE_MODE:
2610 fepriv->tune_mode_flags = (unsigned long)parg;
2613 /* DEPRECATED dish control ioctls */
2615 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2616 if (fe->ops.dishnetwork_send_legacy_command) {
2617 err = fe->ops.dishnetwork_send_legacy_command(fe,
2618 (unsigned long)parg);
2619 fepriv->state = FESTATE_DISEQC;
2621 } else if (fe->ops.set_voltage) {
2623 * NOTE: This is a fallback condition. Some frontends
2624 * (stv0299 for instance) take longer than 8msec to
2625 * respond to a set_voltage command. Those switches
2626 * need custom routines to switch properly. For all
2627 * other frontends, the following should work ok.
2628 * Dish network legacy switches (as used by Dish500)
2629 * are controlled by sending 9-bit command words
2630 * spaced 8msec apart.
2631 * the actual command word is switch/port dependent
2632 * so it is up to the userspace application to send
2633 * the right command.
2634 * The command must always start with a '0' after
2635 * initialization, so parg is 8 bits and does not
2636 * include the initialization or start bit
2638 unsigned long swcmd = ((unsigned long)parg) << 1;
2644 if (dvb_frontend_debug)
2645 dprintk("switch command: 0x%04lx\n",
2647 nexttime = ktime_get_boottime();
2648 if (dvb_frontend_debug)
2650 /* before sending a command, initialize by sending
2651 * a 32ms 18V to the switch
2653 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2654 dvb_frontend_sleep_until(&nexttime, 32000);
2656 for (i = 0; i < 9; i++) {
2657 if (dvb_frontend_debug)
2658 tv[i + 1] = ktime_get_boottime();
2659 if ((swcmd & 0x01) != last) {
2660 /* set voltage to (last ? 13V : 18V) */
2661 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2662 last = (last) ? 0 : 1;
2666 dvb_frontend_sleep_until(&nexttime, 8000);
2668 if (dvb_frontend_debug) {
2669 dprintk("(adapter %d): switch delay (should be 32k followed by all 8k)\n",
2671 for (i = 1; i < 10; i++)
2672 pr_info("%d: %d\n", i,
2673 (int)ktime_us_delta(tv[i], tv[i - 1]));
2676 fepriv->state = FESTATE_DISEQC;
2681 /* DEPRECATED statistics ioctls */
2684 if (fe->ops.read_ber) {
2686 err = fe->ops.read_ber(fe, parg);
2692 case FE_READ_SIGNAL_STRENGTH:
2693 if (fe->ops.read_signal_strength) {
2695 err = fe->ops.read_signal_strength(fe, parg);
2702 if (fe->ops.read_snr) {
2704 err = fe->ops.read_snr(fe, parg);
2710 case FE_READ_UNCORRECTED_BLOCKS:
2711 if (fe->ops.read_ucblocks) {
2713 err = fe->ops.read_ucblocks(fe, parg);
2719 /* DEPRECATED DVBv3 ioctls */
2721 case FE_SET_FRONTEND:
2722 err = dvbv3_set_delivery_system(fe);
2726 err = dtv_property_cache_sync(fe, c, parg);
2729 err = dtv_set_frontend(fe);
2733 err = dvb_frontend_get_event(fe, parg, file->f_flags);
2736 case FE_GET_FRONTEND:
2737 err = dvb_get_frontend(fe, parg);
2747 static __poll_t dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2749 struct dvb_device *dvbdev = file->private_data;
2750 struct dvb_frontend *fe = dvbdev->priv;
2751 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2753 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2755 poll_wait(file, &fepriv->events.wait_queue, wait);
2757 if (fepriv->events.eventw != fepriv->events.eventr)
2758 return (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
2763 static int dvb_frontend_open(struct inode *inode, struct file *file)
2765 struct dvb_device *dvbdev = file->private_data;
2766 struct dvb_frontend *fe = dvbdev->priv;
2767 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2768 struct dvb_adapter *adapter = fe->dvb;
2771 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2772 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2775 if (adapter->mfe_shared == 2) {
2776 mutex_lock(&adapter->mfe_lock);
2777 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2778 if (adapter->mfe_dvbdev &&
2779 !adapter->mfe_dvbdev->writers) {
2780 mutex_unlock(&adapter->mfe_lock);
2783 adapter->mfe_dvbdev = dvbdev;
2785 } else if (adapter->mfe_shared) {
2786 mutex_lock(&adapter->mfe_lock);
2788 if (!adapter->mfe_dvbdev)
2789 adapter->mfe_dvbdev = dvbdev;
2791 else if (adapter->mfe_dvbdev != dvbdev) {
2793 *mfedev = adapter->mfe_dvbdev;
2795 *mfe = mfedev->priv;
2796 struct dvb_frontend_private
2797 *mfepriv = mfe->frontend_priv;
2798 int mferetry = (dvb_mfe_wait_time << 1);
2800 mutex_unlock(&adapter->mfe_lock);
2801 while (mferetry-- && (mfedev->users != -1 ||
2803 if (msleep_interruptible(500)) {
2804 if (signal_pending(current))
2809 mutex_lock(&adapter->mfe_lock);
2810 if (adapter->mfe_dvbdev != dvbdev) {
2811 mfedev = adapter->mfe_dvbdev;
2813 mfepriv = mfe->frontend_priv;
2814 if (mfedev->users != -1 ||
2816 mutex_unlock(&adapter->mfe_lock);
2819 adapter->mfe_dvbdev = dvbdev;
2824 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2825 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2828 /* If we took control of the bus, we need to force
2829 reinitialization. This is because many ts_bus_ctrl()
2830 functions strobe the RESET pin on the demod, and if the
2831 frontend thread already exists then the dvb_init() routine
2832 won't get called (which is what usually does initial
2833 register configuration). */
2834 fepriv->reinitialise = 1;
2837 if ((ret = dvb_generic_open(inode, file)) < 0)
2840 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2841 /* normal tune mode when opened R/W */
2842 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2844 fepriv->voltage = -1;
2846 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2847 mutex_lock(&fe->dvb->mdev_lock);
2848 if (fe->dvb->mdev) {
2849 mutex_lock(&fe->dvb->mdev->graph_mutex);
2850 if (fe->dvb->mdev->enable_source)
2851 ret = fe->dvb->mdev->enable_source(
2854 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2856 mutex_unlock(&fe->dvb->mdev_lock);
2857 dev_err(fe->dvb->device,
2858 "Tuner is busy. Error %d\n", ret);
2862 mutex_unlock(&fe->dvb->mdev_lock);
2864 ret = dvb_frontend_start(fe);
2868 /* empty event queue */
2869 fepriv->events.eventr = fepriv->events.eventw = 0;
2872 dvb_frontend_get(fe);
2874 if (adapter->mfe_shared)
2875 mutex_unlock(&adapter->mfe_lock);
2879 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2880 mutex_lock(&fe->dvb->mdev_lock);
2881 if (fe->dvb->mdev) {
2882 mutex_lock(&fe->dvb->mdev->graph_mutex);
2883 if (fe->dvb->mdev->disable_source)
2884 fe->dvb->mdev->disable_source(dvbdev->entity);
2885 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2887 mutex_unlock(&fe->dvb->mdev_lock);
2890 dvb_generic_release(inode, file);
2892 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2893 fe->ops.ts_bus_ctrl(fe, 0);
2895 if (adapter->mfe_shared)
2896 mutex_unlock(&adapter->mfe_lock);
2900 static int dvb_frontend_release(struct inode *inode, struct file *file)
2902 struct dvb_device *dvbdev = file->private_data;
2903 struct dvb_frontend *fe = dvbdev->priv;
2904 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2907 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2909 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2910 fepriv->release_jiffies = jiffies;
2914 ret = dvb_generic_release(inode, file);
2916 if (dvbdev->users == -1) {
2917 wake_up(&fepriv->wait_queue);
2918 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
2919 mutex_lock(&fe->dvb->mdev_lock);
2920 if (fe->dvb->mdev) {
2921 mutex_lock(&fe->dvb->mdev->graph_mutex);
2922 if (fe->dvb->mdev->disable_source)
2923 fe->dvb->mdev->disable_source(dvbdev->entity);
2924 mutex_unlock(&fe->dvb->mdev->graph_mutex);
2926 mutex_unlock(&fe->dvb->mdev_lock);
2928 if (fe->exit != DVB_FE_NO_EXIT)
2929 wake_up(&dvbdev->wait_queue);
2930 if (fe->ops.ts_bus_ctrl)
2931 fe->ops.ts_bus_ctrl(fe, 0);
2934 dvb_frontend_put(fe);
2939 static const struct file_operations dvb_frontend_fops = {
2940 .owner = THIS_MODULE,
2941 .unlocked_ioctl = dvb_frontend_ioctl,
2942 #ifdef CONFIG_COMPAT
2943 .compat_ioctl = dvb_frontend_compat_ioctl,
2945 .poll = dvb_frontend_poll,
2946 .open = dvb_frontend_open,
2947 .release = dvb_frontend_release,
2948 .llseek = noop_llseek,
2951 int dvb_frontend_suspend(struct dvb_frontend *fe)
2955 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2958 if (fe->ops.tuner_ops.suspend)
2959 ret = fe->ops.tuner_ops.suspend(fe);
2960 else if (fe->ops.tuner_ops.sleep)
2961 ret = fe->ops.tuner_ops.sleep(fe);
2963 if (fe->ops.suspend)
2964 ret = fe->ops.suspend(fe);
2965 else if (fe->ops.sleep)
2966 ret = fe->ops.sleep(fe);
2970 EXPORT_SYMBOL(dvb_frontend_suspend);
2972 int dvb_frontend_resume(struct dvb_frontend *fe)
2974 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2977 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2980 fe->exit = DVB_FE_DEVICE_RESUME;
2982 ret = fe->ops.resume(fe);
2983 else if (fe->ops.init)
2984 ret = fe->ops.init(fe);
2986 if (fe->ops.tuner_ops.resume)
2987 ret = fe->ops.tuner_ops.resume(fe);
2988 else if (fe->ops.tuner_ops.init)
2989 ret = fe->ops.tuner_ops.init(fe);
2991 if (fe->ops.set_tone && fepriv->tone != -1)
2992 fe->ops.set_tone(fe, fepriv->tone);
2993 if (fe->ops.set_voltage && fepriv->voltage != -1)
2994 fe->ops.set_voltage(fe, fepriv->voltage);
2996 fe->exit = DVB_FE_NO_EXIT;
2997 fepriv->state = FESTATE_RETUNE;
2998 dvb_frontend_wakeup(fe);
3002 EXPORT_SYMBOL(dvb_frontend_resume);
3004 int dvb_register_frontend(struct dvb_adapter *dvb,
3005 struct dvb_frontend *fe)
3007 struct dvb_frontend_private *fepriv;
3008 const struct dvb_device dvbdev_template = {
3011 .readers = (~0) - 1,
3012 .fops = &dvb_frontend_fops,
3013 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
3014 .name = fe->ops.info.name,
3019 dev_dbg(dvb->device, "%s:\n", __func__);
3021 if (mutex_lock_interruptible(&frontend_mutex))
3022 return -ERESTARTSYS;
3024 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
3025 if (!fe->frontend_priv) {
3026 mutex_unlock(&frontend_mutex);
3029 fepriv = fe->frontend_priv;
3031 kref_init(&fe->refcount);
3034 * After initialization, there need to be two references: one
3035 * for dvb_unregister_frontend(), and another one for
3036 * dvb_frontend_detach().
3038 dvb_frontend_get(fe);
3040 sema_init(&fepriv->sem, 1);
3041 init_waitqueue_head(&fepriv->wait_queue);
3042 init_waitqueue_head(&fepriv->events.wait_queue);
3043 mutex_init(&fepriv->events.mtx);
3045 fepriv->inversion = INVERSION_OFF;
3047 dev_info(fe->dvb->device,
3048 "DVB: registering adapter %i frontend %i (%s)...\n",
3049 fe->dvb->num, fe->id, fe->ops.info.name);
3051 ret = dvb_register_device(fe->dvb, &fepriv->dvbdev, &dvbdev_template,
3052 fe, DVB_DEVICE_FRONTEND, 0);
3054 dvb_frontend_put(fe);
3055 mutex_unlock(&frontend_mutex);
3060 * Initialize the cache to the proper values according with the
3061 * first supported delivery system (ops->delsys[0])
3064 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
3065 dvb_frontend_clear_cache(fe);
3067 mutex_unlock(&frontend_mutex);
3070 EXPORT_SYMBOL(dvb_register_frontend);
3072 int dvb_unregister_frontend(struct dvb_frontend *fe)
3074 struct dvb_frontend_private *fepriv = fe->frontend_priv;
3076 dev_dbg(fe->dvb->device, "%s:\n", __func__);
3078 mutex_lock(&frontend_mutex);
3079 dvb_frontend_stop(fe);
3080 dvb_remove_device(fepriv->dvbdev);
3082 /* fe is invalid now */
3083 mutex_unlock(&frontend_mutex);
3084 dvb_frontend_put(fe);
3087 EXPORT_SYMBOL(dvb_unregister_frontend);
3089 static void dvb_frontend_invoke_release(struct dvb_frontend *fe,
3090 void (*release)(struct dvb_frontend *fe))
3094 #ifdef CONFIG_MEDIA_ATTACH
3095 dvb_detach(release);
3100 void dvb_frontend_detach(struct dvb_frontend *fe)
3102 dvb_frontend_invoke_release(fe, fe->ops.release_sec);
3103 dvb_frontend_invoke_release(fe, fe->ops.tuner_ops.release);
3104 dvb_frontend_invoke_release(fe, fe->ops.analog_ops.release);
3105 dvb_frontend_put(fe);
3107 EXPORT_SYMBOL(dvb_frontend_detach);