xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/media/dtv-frontend.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunDigital TV Frontend kABI
4*4882a593Smuzhiyun------------------------
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunDigital TV Frontend
7*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunThe Digital TV Frontend kABI defines a driver-internal interface for
10*4882a593Smuzhiyunregistering low-level, hardware specific driver to a hardware independent
11*4882a593Smuzhiyunfrontend layer. It is only of interest for Digital TV device driver writers.
12*4882a593SmuzhiyunThe header file for this API is named ``dvb_frontend.h`` and located in
13*4882a593Smuzhiyun``include/media/``.
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunDemodulator driver
16*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunThe demodulator driver is responsible for talking with the decoding part of the
19*4882a593Smuzhiyunhardware. Such driver should implement :c:type:`dvb_frontend_ops`, which
20*4882a593Smuzhiyuntells what type of digital TV standards are supported, and points to a
21*4882a593Smuzhiyunseries of functions that allow the DVB core to command the hardware via
22*4882a593Smuzhiyunthe code under ``include/media/dvb_frontend.c``.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunA typical example of such struct in a driver ``foo`` is::
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun	static struct dvb_frontend_ops foo_ops = {
27*4882a593Smuzhiyun		.delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
28*4882a593Smuzhiyun		.info = {
29*4882a593Smuzhiyun			.name	= "foo DVB-T/T2/C driver",
30*4882a593Smuzhiyun			.caps = FE_CAN_FEC_1_2 |
31*4882a593Smuzhiyun				FE_CAN_FEC_2_3 |
32*4882a593Smuzhiyun				FE_CAN_FEC_3_4 |
33*4882a593Smuzhiyun				FE_CAN_FEC_5_6 |
34*4882a593Smuzhiyun				FE_CAN_FEC_7_8 |
35*4882a593Smuzhiyun				FE_CAN_FEC_AUTO |
36*4882a593Smuzhiyun				FE_CAN_QPSK |
37*4882a593Smuzhiyun				FE_CAN_QAM_16 |
38*4882a593Smuzhiyun				FE_CAN_QAM_32 |
39*4882a593Smuzhiyun				FE_CAN_QAM_64 |
40*4882a593Smuzhiyun				FE_CAN_QAM_128 |
41*4882a593Smuzhiyun				FE_CAN_QAM_256 |
42*4882a593Smuzhiyun				FE_CAN_QAM_AUTO |
43*4882a593Smuzhiyun				FE_CAN_TRANSMISSION_MODE_AUTO |
44*4882a593Smuzhiyun				FE_CAN_GUARD_INTERVAL_AUTO |
45*4882a593Smuzhiyun				FE_CAN_HIERARCHY_AUTO |
46*4882a593Smuzhiyun				FE_CAN_MUTE_TS |
47*4882a593Smuzhiyun				FE_CAN_2G_MODULATION,
48*4882a593Smuzhiyun			.frequency_min = 42000000, /* Hz */
49*4882a593Smuzhiyun			.frequency_max = 1002000000, /* Hz */
50*4882a593Smuzhiyun			.symbol_rate_min = 870000,
51*4882a593Smuzhiyun			.symbol_rate_max = 11700000
52*4882a593Smuzhiyun		},
53*4882a593Smuzhiyun		.init = foo_init,
54*4882a593Smuzhiyun		.sleep = foo_sleep,
55*4882a593Smuzhiyun		.release = foo_release,
56*4882a593Smuzhiyun		.set_frontend = foo_set_frontend,
57*4882a593Smuzhiyun		.get_frontend = foo_get_frontend,
58*4882a593Smuzhiyun		.read_status = foo_get_status_and_stats,
59*4882a593Smuzhiyun		.tune = foo_tune,
60*4882a593Smuzhiyun		.i2c_gate_ctrl = foo_i2c_gate_ctrl,
61*4882a593Smuzhiyun		.get_frontend_algo = foo_get_algo,
62*4882a593Smuzhiyun	};
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunA typical example of such struct in a driver ``bar`` meant to be used on
65*4882a593SmuzhiyunSatellite TV reception is::
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun	static const struct dvb_frontend_ops bar_ops = {
68*4882a593Smuzhiyun		.delsys = { SYS_DVBS, SYS_DVBS2 },
69*4882a593Smuzhiyun		.info = {
70*4882a593Smuzhiyun			.name		= "Bar DVB-S/S2 demodulator",
71*4882a593Smuzhiyun			.frequency_min	= 500000, /* KHz */
72*4882a593Smuzhiyun			.frequency_max	= 2500000, /* KHz */
73*4882a593Smuzhiyun			.frequency_stepsize	= 0,
74*4882a593Smuzhiyun			.symbol_rate_min = 1000000,
75*4882a593Smuzhiyun			.symbol_rate_max = 45000000,
76*4882a593Smuzhiyun			.symbol_rate_tolerance = 500,
77*4882a593Smuzhiyun			.caps = FE_CAN_INVERSION_AUTO |
78*4882a593Smuzhiyun				FE_CAN_FEC_AUTO |
79*4882a593Smuzhiyun				FE_CAN_QPSK,
80*4882a593Smuzhiyun		},
81*4882a593Smuzhiyun		.init = bar_init,
82*4882a593Smuzhiyun		.sleep = bar_sleep,
83*4882a593Smuzhiyun		.release = bar_release,
84*4882a593Smuzhiyun		.set_frontend = bar_set_frontend,
85*4882a593Smuzhiyun		.get_frontend = bar_get_frontend,
86*4882a593Smuzhiyun		.read_status = bar_get_status_and_stats,
87*4882a593Smuzhiyun		.i2c_gate_ctrl = bar_i2c_gate_ctrl,
88*4882a593Smuzhiyun		.get_frontend_algo = bar_get_algo,
89*4882a593Smuzhiyun		.tune = bar_tune,
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun		/* Satellite-specific */
92*4882a593Smuzhiyun		.diseqc_send_master_cmd = bar_send_diseqc_msg,
93*4882a593Smuzhiyun		.diseqc_send_burst = bar_send_burst,
94*4882a593Smuzhiyun		.set_tone = bar_set_tone,
95*4882a593Smuzhiyun		.set_voltage = bar_set_voltage,
96*4882a593Smuzhiyun	};
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun.. note::
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun   #) For satellite digital TV standards (DVB-S, DVB-S2, ISDB-S), the
101*4882a593Smuzhiyun      frequencies are specified in kHz, while, for terrestrial and cable
102*4882a593Smuzhiyun      standards, they're specified in Hz. Due to that, if the same frontend
103*4882a593Smuzhiyun      supports both types, you'll need to have two separate
104*4882a593Smuzhiyun      :c:type:`dvb_frontend_ops` structures, one for each standard.
105*4882a593Smuzhiyun   #) The ``.i2c_gate_ctrl`` field is present only when the hardware has
106*4882a593Smuzhiyun      allows controlling an I2C gate (either directly of via some GPIO pin),
107*4882a593Smuzhiyun      in order to remove the tuner from the I2C bus after a channel is
108*4882a593Smuzhiyun      tuned.
109*4882a593Smuzhiyun   #) All new drivers should implement the
110*4882a593Smuzhiyun      :ref:`DVBv5 statistics <dvbv5_stats>` via ``.read_status``.
111*4882a593Smuzhiyun      Yet, there are a number of callbacks meant to get statistics for
112*4882a593Smuzhiyun      signal strength, S/N and UCB. Those are there to provide backward
113*4882a593Smuzhiyun      compatibility with legacy applications that don't support the DVBv5
114*4882a593Smuzhiyun      API. Implementing those callbacks are optional. Those callbacks may be
115*4882a593Smuzhiyun      removed in the future, after we have all existing drivers supporting
116*4882a593Smuzhiyun      DVBv5 stats.
117*4882a593Smuzhiyun   #) Other callbacks are required for satellite TV standards, in order to
118*4882a593Smuzhiyun      control LNBf and DiSEqC: ``.diseqc_send_master_cmd``,
119*4882a593Smuzhiyun      ``.diseqc_send_burst``, ``.set_tone``, ``.set_voltage``.
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun.. |delta|   unicode:: U+00394
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunThe ``include/media/dvb_frontend.c`` has a kernel thread which is
124*4882a593Smuzhiyunresponsible for tuning the device. It supports multiple algorithms to
125*4882a593Smuzhiyundetect a channel, as defined at enum :c:func:`dvbfe_algo`.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunThe algorithm to be used is obtained via ``.get_frontend_algo``. If the driver
128*4882a593Smuzhiyundoesn't fill its field at struct dvb_frontend_ops, it will default to
129*4882a593Smuzhiyun``DVBFE_ALGO_SW``, meaning that the dvb-core will do a zigzag when tuning,
130*4882a593Smuzhiyune. g. it will try first to use the specified center frequency ``f``,
131*4882a593Smuzhiyunthen, it will do ``f`` + |delta|, ``f`` - |delta|, ``f`` + 2 x |delta|,
132*4882a593Smuzhiyun``f`` - 2 x |delta| and so on.
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunIf the hardware has internally a some sort of zigzag algorithm, you should
135*4882a593Smuzhiyundefine a ``.get_frontend_algo`` function that would return ``DVBFE_ALGO_HW``.
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun.. note::
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun   The core frontend support also supports
140*4882a593Smuzhiyun   a third type (``DVBFE_ALGO_CUSTOM``), in order to allow the driver to
141*4882a593Smuzhiyun   define its own hardware-assisted algorithm. Very few hardware need to
142*4882a593Smuzhiyun   use it nowadays. Using ``DVBFE_ALGO_CUSTOM`` require to provide other
143*4882a593Smuzhiyun   function callbacks at struct dvb_frontend_ops.
144*4882a593Smuzhiyun
145*4882a593SmuzhiyunAttaching frontend driver to the bridge driver
146*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunBefore using the Digital TV frontend core, the bridge driver should attach
149*4882a593Smuzhiyunthe frontend demod, tuner and SEC devices and call
150*4882a593Smuzhiyun:c:func:`dvb_register_frontend()`,
151*4882a593Smuzhiyunin order to register the new frontend at the subsystem. At device
152*4882a593Smuzhiyundetach/removal, the bridge driver should call
153*4882a593Smuzhiyun:c:func:`dvb_unregister_frontend()` to
154*4882a593Smuzhiyunremove the frontend from the core and then :c:func:`dvb_frontend_detach()`
155*4882a593Smuzhiyunto free the memory allocated by the frontend drivers.
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunThe drivers should also call :c:func:`dvb_frontend_suspend()` as part of
158*4882a593Smuzhiyuntheir handler for the :c:type:`device_driver`.\ ``suspend()``, and
159*4882a593Smuzhiyun:c:func:`dvb_frontend_resume()` as
160*4882a593Smuzhiyunpart of their handler for :c:type:`device_driver`.\ ``resume()``.
161*4882a593Smuzhiyun
162*4882a593SmuzhiyunA few other optional functions are provided to handle some special cases.
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun.. _dvbv5_stats:
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunDigital TV Frontend statistics
167*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168*4882a593Smuzhiyun
169*4882a593SmuzhiyunIntroduction
170*4882a593Smuzhiyun^^^^^^^^^^^^
171*4882a593Smuzhiyun
172*4882a593SmuzhiyunDigital TV frontends provide a range of
173*4882a593Smuzhiyun:ref:`statistics <frontend-stat-properties>` meant to help tuning the device
174*4882a593Smuzhiyunand measuring the quality of service.
175*4882a593Smuzhiyun
176*4882a593SmuzhiyunFor each statistics measurement, the driver should set the type of scale used,
177*4882a593Smuzhiyunor ``FE_SCALE_NOT_AVAILABLE`` if the statistics is not available on a given
178*4882a593Smuzhiyuntime. Drivers should also provide the number of statistics for each type.
179*4882a593Smuzhiyunthat's usually 1 for most video standards [#f2]_.
180*4882a593Smuzhiyun
181*4882a593SmuzhiyunDrivers should initialize each statistic counters with length and
182*4882a593Smuzhiyunscale at its init code. For example, if the frontend provides signal
183*4882a593Smuzhiyunstrength, it should have, on its init code::
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun	struct dtv_frontend_properties *c = &state->fe.dtv_property_cache;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun	c->strength.len = 1;
188*4882a593Smuzhiyun	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunAnd, when the statistics got updated, set the scale::
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun	c->strength.stat[0].scale = FE_SCALE_DECIBEL;
193*4882a593Smuzhiyun	c->strength.stat[0].uvalue = strength;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun.. [#f2] For ISDB-T, it may provide both a global statistics and a per-layer
196*4882a593Smuzhiyun   set of statistics. On such cases, len should be equal to 4. The first
197*4882a593Smuzhiyun   value corresponds to the global stat; the other ones to each layer, e. g.:
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun   - c->cnr.stat[0] for global S/N carrier ratio,
200*4882a593Smuzhiyun   - c->cnr.stat[1] for Layer A S/N carrier ratio,
201*4882a593Smuzhiyun   - c->cnr.stat[2] for layer B S/N carrier ratio,
202*4882a593Smuzhiyun   - c->cnr.stat[3] for layer C S/N carrier ratio.
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun.. note:: Please prefer to use ``FE_SCALE_DECIBEL`` instead of
205*4882a593Smuzhiyun   ``FE_SCALE_RELATIVE`` for signal strength and CNR measurements.
206*4882a593Smuzhiyun
207*4882a593SmuzhiyunGroups of statistics
208*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^
209*4882a593Smuzhiyun
210*4882a593SmuzhiyunThere are several groups of statistics currently supported:
211*4882a593Smuzhiyun
212*4882a593SmuzhiyunSignal strength (:ref:`DTV-STAT-SIGNAL-STRENGTH`)
213*4882a593Smuzhiyun  - Measures the signal strength level at the analog part of the tuner or
214*4882a593Smuzhiyun    demod.
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun  - Typically obtained from the gain applied to the tuner and/or frontend
217*4882a593Smuzhiyun    in order to detect the carrier. When no carrier is detected, the gain is
218*4882a593Smuzhiyun    at the maximum value (so, strength is on its minimal).
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun  - As the gain is visible through the set of registers that adjust the gain,
221*4882a593Smuzhiyun    typically, this statistics is always available [#f3]_.
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun  - Drivers should try to make it available all the times, as these statistics
224*4882a593Smuzhiyun    can be used when adjusting an antenna position and to check for troubles
225*4882a593Smuzhiyun    at the cabling.
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun  .. [#f3] On a few devices, the gain keeps floating if there is no carrier.
228*4882a593Smuzhiyun     On such devices, strength report should check first if carrier is
229*4882a593Smuzhiyun     detected at the tuner (``FE_HAS_CARRIER``, see :c:type:`fe_status`),
230*4882a593Smuzhiyun     and otherwise return the lowest possible value.
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunCarrier Signal to Noise ratio (:ref:`DTV-STAT-CNR`)
233*4882a593Smuzhiyun  - Signal to Noise ratio for the main carrier.
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun  - Signal to Noise measurement depends on the device. On some hardware, it is
236*4882a593Smuzhiyun    available when the main carrier is detected. On those hardware, CNR
237*4882a593Smuzhiyun    measurement usually comes from the tuner (e. g. after ``FE_HAS_CARRIER``,
238*4882a593Smuzhiyun    see :c:type:`fe_status`).
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun    On other devices, it requires inner FEC decoding,
241*4882a593Smuzhiyun    as the frontend measures it indirectly from other parameters (e. g. after
242*4882a593Smuzhiyun    ``FE_HAS_VITERBI``, see :c:type:`fe_status`).
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun    Having it available after inner FEC is more common.
245*4882a593Smuzhiyun
246*4882a593SmuzhiyunBit counts post-FEC (:ref:`DTV-STAT-POST-ERROR-BIT-COUNT` and :ref:`DTV-STAT-POST-TOTAL-BIT-COUNT`)
247*4882a593Smuzhiyun  - Those counters measure the number of bits and bit errors errors after
248*4882a593Smuzhiyun    the forward error correction (FEC) on the inner coding block
249*4882a593Smuzhiyun    (after Viterbi, LDPC or other inner code).
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun  - Due to its nature, those statistics depend on full coding lock
252*4882a593Smuzhiyun    (e. g. after ``FE_HAS_SYNC`` or after ``FE_HAS_LOCK``,
253*4882a593Smuzhiyun    see :c:type:`fe_status`).
254*4882a593Smuzhiyun
255*4882a593SmuzhiyunBit counts pre-FEC (:ref:`DTV-STAT-PRE-ERROR-BIT-COUNT` and :ref:`DTV-STAT-PRE-TOTAL-BIT-COUNT`)
256*4882a593Smuzhiyun  - Those counters measure the number of bits and bit errors errors before
257*4882a593Smuzhiyun    the forward error correction (FEC) on the inner coding block
258*4882a593Smuzhiyun    (before Viterbi, LDPC or other inner code).
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun  - Not all frontends provide this kind of statistics.
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun  - Due to its nature, those statistics depend on inner coding lock (e. g.
263*4882a593Smuzhiyun    after ``FE_HAS_VITERBI``, see :c:type:`fe_status`).
264*4882a593Smuzhiyun
265*4882a593SmuzhiyunBlock counts (:ref:`DTV-STAT-ERROR-BLOCK-COUNT` and :ref:`DTV-STAT-TOTAL-BLOCK-COUNT`)
266*4882a593Smuzhiyun  - Those counters measure the number of blocks and block errors errors after
267*4882a593Smuzhiyun    the forward error correction (FEC) on the inner coding block
268*4882a593Smuzhiyun    (before Viterbi, LDPC or other inner code).
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun  - Due to its nature, those statistics depend on full coding lock
271*4882a593Smuzhiyun    (e. g. after ``FE_HAS_SYNC`` or after
272*4882a593Smuzhiyun    ``FE_HAS_LOCK``, see :c:type:`fe_status`).
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun.. note:: All counters should be monotonically increased as they're
275*4882a593Smuzhiyun   collected from the hardware.
276*4882a593Smuzhiyun
277*4882a593SmuzhiyunA typical example of the logic that handle status and statistics is::
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun	static int foo_get_status_and_stats(struct dvb_frontend *fe)
280*4882a593Smuzhiyun	{
281*4882a593Smuzhiyun		struct foo_state *state = fe->demodulator_priv;
282*4882a593Smuzhiyun		struct dtv_frontend_properties *c = &fe->dtv_property_cache;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun		int rc;
285*4882a593Smuzhiyun		enum fe_status *status;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun		/* Both status and strength are always available */
288*4882a593Smuzhiyun		rc = foo_read_status(fe, &status);
289*4882a593Smuzhiyun		if (rc < 0)
290*4882a593Smuzhiyun			return rc;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun		rc = foo_read_strength(fe);
293*4882a593Smuzhiyun		if (rc < 0)
294*4882a593Smuzhiyun			return rc;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun		/* Check if CNR is available */
297*4882a593Smuzhiyun		if (!(fe->status & FE_HAS_CARRIER))
298*4882a593Smuzhiyun			return 0;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun		rc = foo_read_cnr(fe);
301*4882a593Smuzhiyun		if (rc < 0)
302*4882a593Smuzhiyun			return rc;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun		/* Check if pre-BER stats are available */
305*4882a593Smuzhiyun		if (!(fe->status & FE_HAS_VITERBI))
306*4882a593Smuzhiyun			return 0;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun		rc = foo_get_pre_ber(fe);
309*4882a593Smuzhiyun		if (rc < 0)
310*4882a593Smuzhiyun			return rc;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun		/* Check if post-BER stats are available */
313*4882a593Smuzhiyun		if (!(fe->status & FE_HAS_SYNC))
314*4882a593Smuzhiyun			return 0;
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun		rc = foo_get_post_ber(fe);
317*4882a593Smuzhiyun		if (rc < 0)
318*4882a593Smuzhiyun			return rc;
319*4882a593Smuzhiyun	}
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun	static const struct dvb_frontend_ops ops = {
322*4882a593Smuzhiyun		/* ... */
323*4882a593Smuzhiyun		.read_status = foo_get_status_and_stats,
324*4882a593Smuzhiyun	};
325*4882a593Smuzhiyun
326*4882a593SmuzhiyunStatistics collection
327*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^
328*4882a593Smuzhiyun
329*4882a593SmuzhiyunOn almost all frontend hardware, the bit and byte counts are stored by
330*4882a593Smuzhiyunthe hardware after a certain amount of time or after the total bit/block
331*4882a593Smuzhiyuncounter reaches a certain value (usually programmable), for example, on
332*4882a593Smuzhiyunevery 1000 ms or after receiving 1,000,000 bits.
333*4882a593Smuzhiyun
334*4882a593SmuzhiyunSo, if you read the registers too soon, you'll end by reading the same
335*4882a593Smuzhiyunvalue as in the previous reading, causing the monotonic value to be
336*4882a593Smuzhiyunincremented too often.
337*4882a593Smuzhiyun
338*4882a593SmuzhiyunDrivers should take the responsibility to avoid too often reads. That
339*4882a593Smuzhiyuncan be done using two approaches:
340*4882a593Smuzhiyun
341*4882a593Smuzhiyunif the driver have a bit that indicates when a collected data is ready
342*4882a593Smuzhiyun%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343*4882a593Smuzhiyun
344*4882a593SmuzhiyunDriver should check such bit before making the statistics available.
345*4882a593Smuzhiyun
346*4882a593SmuzhiyunAn example of such behavior can be found at this code snippet (adapted
347*4882a593Smuzhiyunfrom mb86a20s driver's logic)::
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun	static int foo_get_pre_ber(struct dvb_frontend *fe)
350*4882a593Smuzhiyun	{
351*4882a593Smuzhiyun		struct foo_state *state = fe->demodulator_priv;
352*4882a593Smuzhiyun		struct dtv_frontend_properties *c = &fe->dtv_property_cache;
353*4882a593Smuzhiyun		int rc, bit_error;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun		/* Check if the BER measures are already available */
356*4882a593Smuzhiyun		rc = foo_read_u8(state, 0x54);
357*4882a593Smuzhiyun		if (rc < 0)
358*4882a593Smuzhiyun			return rc;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun		if (!rc)
361*4882a593Smuzhiyun			return 0;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun		/* Read Bit Error Count */
364*4882a593Smuzhiyun		bit_error = foo_read_u32(state, 0x55);
365*4882a593Smuzhiyun		if (bit_error < 0)
366*4882a593Smuzhiyun			return bit_error;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun		/* Read Total Bit Count */
369*4882a593Smuzhiyun		rc = foo_read_u32(state, 0x51);
370*4882a593Smuzhiyun		if (rc < 0)
371*4882a593Smuzhiyun			return rc;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun		c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
374*4882a593Smuzhiyun		c->pre_bit_error.stat[0].uvalue += bit_error;
375*4882a593Smuzhiyun		c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
376*4882a593Smuzhiyun		c->pre_bit_count.stat[0].uvalue += rc;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun		return 0;
379*4882a593Smuzhiyun	}
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunIf the driver doesn't provide a statistics available check bit
382*4882a593Smuzhiyun%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383*4882a593Smuzhiyun
384*4882a593SmuzhiyunA few devices, however, may not provide a way to check if the stats are
385*4882a593Smuzhiyunavailable (or the way to check it is unknown). They may not even provide
386*4882a593Smuzhiyuna way to directly read the total number of bits or blocks.
387*4882a593Smuzhiyun
388*4882a593SmuzhiyunOn those devices, the driver need to ensure that it won't be reading from
389*4882a593Smuzhiyunthe register too often and/or estimate the total number of bits/blocks.
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunOn such drivers, a typical routine to get statistics would be like
392*4882a593Smuzhiyun(adapted from dib8000 driver's logic)::
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun	struct foo_state {
395*4882a593Smuzhiyun		/* ... */
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun		unsigned long per_jiffies_stats;
398*4882a593Smuzhiyun	}
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun	static int foo_get_pre_ber(struct dvb_frontend *fe)
401*4882a593Smuzhiyun	{
402*4882a593Smuzhiyun		struct foo_state *state = fe->demodulator_priv;
403*4882a593Smuzhiyun		struct dtv_frontend_properties *c = &fe->dtv_property_cache;
404*4882a593Smuzhiyun		int rc, bit_error;
405*4882a593Smuzhiyun		u64 bits;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun		/* Check if time for stats was elapsed */
408*4882a593Smuzhiyun		if (!time_after(jiffies, state->per_jiffies_stats))
409*4882a593Smuzhiyun			return 0;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun		/* Next stat should be collected in 1000 ms */
412*4882a593Smuzhiyun		state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun		/* Read Bit Error Count */
415*4882a593Smuzhiyun		bit_error = foo_read_u32(state, 0x55);
416*4882a593Smuzhiyun		if (bit_error < 0)
417*4882a593Smuzhiyun			return bit_error;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun		/*
420*4882a593Smuzhiyun		 * On this particular frontend, there's no register that
421*4882a593Smuzhiyun		 * would provide the number of bits per 1000ms sample. So,
422*4882a593Smuzhiyun		 * some function would calculate it based on DTV properties
423*4882a593Smuzhiyun		 */
424*4882a593Smuzhiyun		bits = get_number_of_bits_per_1000ms(fe);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun		c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
427*4882a593Smuzhiyun		c->pre_bit_error.stat[0].uvalue += bit_error;
428*4882a593Smuzhiyun		c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
429*4882a593Smuzhiyun		c->pre_bit_count.stat[0].uvalue += bits;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun		return 0;
432*4882a593Smuzhiyun	}
433*4882a593Smuzhiyun
434*4882a593SmuzhiyunPlease notice that, on both cases, we're getting the statistics using the
435*4882a593Smuzhiyun:c:type:`dvb_frontend_ops` ``.read_status`` callback. The rationale is that
436*4882a593Smuzhiyunthe frontend core will automatically call this function periodically
437*4882a593Smuzhiyun(usually, 3 times per second, when the frontend is locked).
438*4882a593Smuzhiyun
439*4882a593SmuzhiyunThat warrants that we won't miss to collect a counter and increment the
440*4882a593Smuzhiyunmonotonic stats at the right time.
441*4882a593Smuzhiyun
442*4882a593SmuzhiyunDigital TV Frontend functions and types
443*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun.. kernel-doc:: include/media/dvb_frontend.h
446