dtv-frontend.rst 15 KB

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