dtv-frontend.rst 15 KB

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