1 Digital TV Frontend kABI
2 ------------------------
7 The Digital TV Frontend kABI defines a driver-internal interface for
8 registering low-level, hardware specific driver to a hardware independent
9 frontend layer. It is only of interest for Digital TV device driver writers.
10 The header file for this API is named ``dvb_frontend.h`` and located in
16 The demodulator driver is responsible to talk with the decoding part of the
17 hardware. Such driver should implement :c:type:`dvb_frontend_ops`, with
18 tells what type of digital TV standards are supported, and points to a
19 series of functions that allow the DVB core to command the hardware via
20 the code under ``include/media/dvb_frontend.c``.
22 A typical example of such struct in a driver ``foo`` is::
24 static struct dvb_frontend_ops foo_ops = {
25 .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
27 .name = "foo DVB-T/T2/C driver",
28 .caps = FE_CAN_FEC_1_2 |
41 FE_CAN_TRANSMISSION_MODE_AUTO |
42 FE_CAN_GUARD_INTERVAL_AUTO |
43 FE_CAN_HIERARCHY_AUTO |
46 .frequency_min = 42000000, /* Hz */
47 .frequency_max = 1002000000, /* Hz */
48 .symbol_rate_min = 870000,
49 .symbol_rate_max = 11700000
53 .release = foo_release,
54 .set_frontend = foo_set_frontend,
55 .get_frontend = foo_get_frontend,
56 .read_status = foo_get_status_and_stats,
58 .i2c_gate_ctrl = foo_i2c_gate_ctrl,
59 .get_frontend_algo = foo_get_algo,
62 A typical example of such struct in a driver ``bar`` meant to be used on
63 Satellite TV reception is::
65 static const struct dvb_frontend_ops bar_ops = {
66 .delsys = { SYS_DVBS, SYS_DVBS2 },
68 .name = "Bar DVB-S/S2 demodulator",
69 .frequency_min = 500000, /* KHz */
70 .frequency_max = 2500000, /* KHz */
71 .frequency_stepsize = 0,
72 .symbol_rate_min = 1000000,
73 .symbol_rate_max = 45000000,
74 .symbol_rate_tolerance = 500,
75 .caps = FE_CAN_INVERSION_AUTO |
81 .release = bar_release,
82 .set_frontend = bar_set_frontend,
83 .get_frontend = bar_get_frontend,
84 .read_status = bar_get_status_and_stats,
85 .i2c_gate_ctrl = bar_i2c_gate_ctrl,
86 .get_frontend_algo = bar_get_algo,
89 /* Satellite-specific */
90 .diseqc_send_master_cmd = bar_send_diseqc_msg,
91 .diseqc_send_burst = bar_send_burst,
92 .set_tone = bar_set_tone,
93 .set_voltage = bar_set_voltage,
98 #) For satellite digital TV standards (DVB-S, DVB-S2, ISDB-S), the
99 frequencies are specified in kHz, while, for terrestrial and cable
100 standards, they're specified in Hz. Due to that, if the same frontend
101 supports both types, you'll need to have two separate
102 :c:type:`dvb_frontend_ops` structures, one for each standard.
103 #) The ``.i2c_gate_ctrl`` field is present only when the hardware has
104 allows controlling an I2C gate (either directly of via some GPIO pin),
105 in order to remove the tuner from the I2C bus after a channel is
107 #) All new drivers should implement the
108 :ref:`DVBv5 statistics <dvbv5_stats>` via ``.read_status``.
109 Yet, there are a number of callbacks meant to get statistics for
110 signal strength, S/N and UCB. Those are there to provide backward
111 compatibility with legacy applications that don't support the DVBv5
112 API. Implementing those callbacks are optional. Those callbacks may be
113 removed in the future, after we have all existing drivers supporting
115 #) Other callbacks are required for satellite TV standards, in order to
116 control LNBf and DiSEqC: ``.diseqc_send_master_cmd``,
117 ``.diseqc_send_burst``, ``.set_tone``, ``.set_voltage``.
119 .. |delta| unicode:: U+00394
121 The ``include/media/dvb_frontend.c`` has a kernel thread with is
122 responsible for tuning the device. It supports multiple algorithms to
123 detect a channel, as defined at enum :c:func:`dvbfe_algo`.
125 The algorithm to be used is obtained via ``.get_frontend_algo``. If the driver
126 doesn't fill its field at struct :c:type:`dvb_frontend_ops`, it will default to
127 ``DVBFE_ALGO_SW``, meaning that the dvb-core will do a zigzag when tuning,
128 e. g. it will try first to use the specified center frequency ``f``,
129 then, it will do ``f`` + |delta|, ``f`` - |delta|, ``f`` + 2 x |delta|,
130 ``f`` - 2 x |delta| and so on.
132 If the hardware has internally a some sort of zigzag algorithm, you should
133 define a ``.get_frontend_algo`` function that would return ``DVBFE_ALGO_HW``.
137 The core frontend support also supports
138 a third type (``DVBFE_ALGO_CUSTOM``), in order to allow the driver to
139 define its own hardware-assisted algorithm. Very few hardware need to
140 use it nowadays. Using ``DVBFE_ALGO_CUSTOM`` require to provide other
141 function callbacks at struct :c:type:`dvb_frontend_ops`.
143 Attaching frontend driver to the bridge driver
144 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146 Before using the Digital TV frontend core, the bridge driver should attach
147 the frontend demod, tuner and SEC devices and call
148 :c:func:`dvb_register_frontend()`,
149 in order to register the new frontend at the subsystem. At device
150 detach/removal, the bridge driver should call
151 :c:func:`dvb_unregister_frontend()` to
152 remove the frontend from the core and then :c:func:`dvb_frontend_detach()`
153 to free the memory allocated by the frontend drivers.
155 The drivers should also call :c:func:`dvb_frontend_suspend()` as part of
156 their handler for the :c:type:`device_driver`.\ ``suspend()``, and
157 :c:func:`dvb_frontend_resume()` as
158 part of their handler for :c:type:`device_driver`.\ ``resume()``.
160 A few other optional functions are provided to handle some special cases.
164 Digital TV Frontend statistics
165 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170 Digital TV frontends provide a range of
171 :ref:`statistics <frontend-stat-properties>` meant to help tuning the device
172 and measuring the quality of service.
174 For each statistics measurement, the driver should set the type of scale used,
175 or ``FE_SCALE_NOT_AVAILABLE`` if the statistics is not available on a given
176 time. Drivers should also provide the number of statistics for each type.
177 that's usually 1 for most video standards [#f2]_.
179 Drivers should initialize each statistic counters with length and
180 scale at its init code. For example, if the frontend provides signal
181 strength, it should have, on its init code::
183 struct dtv_frontend_properties *c = &state->fe.dtv_property_cache;
186 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
188 And, when the statistics got updated, set the scale::
190 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
191 c->strength.stat[0].uvalue = strength;
193 .. [#f2] For ISDB-T, it may provide both a global statistics and a per-layer
194 set of statistics. On such cases, len should be equal to 4. The first
195 value corresponds to the global stat; the other ones to each layer, e. g.:
197 - c->cnr.stat[0] for global S/N carrier ratio,
198 - c->cnr.stat[1] for Layer A S/N carrier ratio,
199 - c->cnr.stat[2] for layer B S/N carrier ratio,
200 - c->cnr.stat[3] for layer C S/N carrier ratio.
202 .. note:: Please prefer to use ``FE_SCALE_DECIBEL`` instead of
203 ``FE_SCALE_RELATIVE`` for signal strength and CNR measurements.
208 There are several groups of statistics currently supported:
210 Signal strength (:ref:`DTV-STAT-SIGNAL-STRENGTH`)
211 - Measures the signal strength level at the analog part of the tuner or
214 - Typically obtained from the gain applied to the tuner and/or frontend
215 in order to detect the carrier. When no carrier is detected, the gain is
216 at the maximum value (so, strength is on its minimal).
218 - As the gain is visible through the set of registers that adjust the gain,
219 typically, this statistics is always available [#f3]_.
221 - Drivers should try to make it available all the times, as this statistics
222 can be used when adjusting an antenna position and to check for troubles
225 .. [#f3] On a few devices, the gain keeps floating if no carrier.
226 On such devices, strength report should check first if carrier is
227 detected at the tuner (``FE_HAS_CARRIER``, see :c:type:`fe_status`),
228 and otherwise return the lowest possible value.
230 Carrier Signal to Noise ratio (:ref:`DTV-STAT-CNR`)
231 - Signal to Noise ratio for the main carrier.
233 - Signal to Noise measurement depends on the device. On some hardware, is
234 available when the main carrier is detected. On those hardware, CNR
235 measurement usually comes from the tuner (e. g. after ``FE_HAS_CARRIER``,
236 see :c:type:`fe_status`).
238 On other devices, it requires inner FEC decoding,
239 as the frontend measures it indirectly from other parameters (e. g. after
240 ``FE_HAS_VITERBI``, see :c:type:`fe_status`).
242 Having it available after inner FEC is more common.
244 Bit counts post-FEC (:ref:`DTV-STAT-POST-ERROR-BIT-COUNT` and :ref:`DTV-STAT-POST-TOTAL-BIT-COUNT`)
245 - Those counters measure the number of bits and bit errors errors after
246 the forward error correction (FEC) on the inner coding block
247 (after Viterbi, LDPC or other inner code).
249 - Due to its nature, those statistics depend on full coding lock
250 (e. g. after ``FE_HAS_SYNC`` or after ``FE_HAS_LOCK``,
251 see :c:type:`fe_status`).
253 Bit counts pre-FEC (:ref:`DTV-STAT-PRE-ERROR-BIT-COUNT` and :ref:`DTV-STAT-PRE-TOTAL-BIT-COUNT`)
254 - Those counters measure the number of bits and bit errors errors before
255 the forward error correction (FEC) on the inner coding block
256 (before Viterbi, LDPC or other inner code).
258 - Not all frontends provide this kind of statistics.
260 - Due to its nature, those statistics depend on inner coding lock (e. g.
261 after ``FE_HAS_VITERBI``, see :c:type:`fe_status`).
263 Block counts (:ref:`DTV-STAT-ERROR-BLOCK-COUNT` and :ref:`DTV-STAT-TOTAL-BLOCK-COUNT`)
264 - Those counters measure the number of blocks and block errors errors after
265 the forward error correction (FEC) on the inner coding block
266 (before Viterbi, LDPC or other inner code).
268 - Due to its nature, those statistics depend on full coding lock
269 (e. g. after ``FE_HAS_SYNC`` or after
270 ``FE_HAS_LOCK``, see :c:type:`fe_status`).
272 .. note:: All counters should be monotonically increased as they're
273 collected from the hardware.
275 A typical example of the logic that handle status and statistics is::
277 static int foo_get_status_and_stats(struct dvb_frontend *fe)
279 struct foo_state *state = fe->demodulator_priv;
280 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
283 enum fe_status *status;
285 /* Both status and strength are always available */
286 rc = foo_read_status(fe, &status);
290 rc = foo_read_strength(fe);
294 /* Check if CNR is available */
295 if (!(fe->status & FE_HAS_CARRIER))
298 rc = foo_read_cnr(fe);
302 /* Check if pre-BER stats are available */
303 if (!(fe->status & FE_HAS_VITERBI))
306 rc = foo_get_pre_ber(fe);
310 /* Check if post-BER stats are available */
311 if (!(fe->status & FE_HAS_SYNC))
314 rc = foo_get_post_ber(fe);
319 static const struct dvb_frontend_ops ops = {
321 .read_status = foo_get_status_and_stats,
327 On almost all frontend hardware, the bit and byte counts are stored by
328 the hardware after a certain amount of time or after the total bit/block
329 counter reaches a certain value (usually programable), for example, on
330 every 1000 ms or after receiving 1,000,000 bits.
332 So, if you read the registers too soon, you'll end by reading the same
333 value as in the previous reading, causing the monotonic value to be
334 incremented too often.
336 Drivers should take the responsibility to avoid too often reads. That
337 can be done using two approaches:
339 if the driver have a bit that indicates when a collected data is ready
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 Driver should check such bit before making the statistics available.
344 An example of such behavior can be found at this code snippet (adapted
345 from mb86a20s driver's logic)::
347 static int foo_get_pre_ber(struct dvb_frontend *fe)
349 struct foo_state *state = fe->demodulator_priv;
350 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
353 /* Check if the BER measures are already available */
354 rc = foo_read_u8(state, 0x54);
361 /* Read Bit Error Count */
362 bit_error = foo_read_u32(state, 0x55);
366 /* Read Total Bit Count */
367 rc = foo_read_u32(state, 0x51);
371 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
372 c->pre_bit_error.stat[0].uvalue += bit_error;
373 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
374 c->pre_bit_count.stat[0].uvalue += rc;
379 If the driver doesn't provide a statistics available check bit
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 A few devices, however, may not provide a way to check if the stats are
383 available (or the way to check it is unknown). They may not even provide
384 a way to directly read the total number of bits or blocks.
386 On those devices, the driver need to ensure that it won't be reading from
387 the register too often and/or estimate the total number of bits/blocks.
389 On such drivers, a typical routine to get statistics would be like
390 (adapted from dib8000 driver's logic)::
395 unsigned long per_jiffies_stats;
398 static int foo_get_pre_ber(struct dvb_frontend *fe)
400 struct foo_state *state = fe->demodulator_priv;
401 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
405 /* Check if time for stats was elapsed */
406 if (!time_after(jiffies, state->per_jiffies_stats))
409 /* Next stat should be collected in 1000 ms */
410 state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
412 /* Read Bit Error Count */
413 bit_error = foo_read_u32(state, 0x55);
418 * On this particular frontend, there's no register that
419 * would provide the number of bits per 1000ms sample. So,
420 * some function would calculate it based on DTV properties
422 bits = get_number_of_bits_per_1000ms(fe);
424 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
425 c->pre_bit_error.stat[0].uvalue += bit_error;
426 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
427 c->pre_bit_count.stat[0].uvalue += bits;
432 Please notice that, on both cases, we're getting the statistics using the
433 :c:type:`dvb_frontend_ops` ``.read_status`` callback. The rationale is that
434 the frontend core will automatically call this function periodically
435 (usually, 3 times per second, when the frontend is locked).
437 That warrants that we won't miss to collect a counter and increment the
438 monotonic stats at the right time.
440 Digital TV Frontend functions and types
441 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
443 .. kernel-doc:: include/media/dvb_frontend.h