18c2ecf20Sopenharmony_ci===================
28c2ecf20Sopenharmony_ciTracepoints in ALSA
38c2ecf20Sopenharmony_ci===================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci2017/07/02
68c2ecf20Sopenharmony_ciTakasahi Sakamoto
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciTracepoints in ALSA PCM core
98c2ecf20Sopenharmony_ci============================
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciALSA PCM core registers ``snd_pcm`` subsystem to kernel tracepoint system.
128c2ecf20Sopenharmony_ciThis subsystem includes two categories of tracepoints; for state of PCM buffer
138c2ecf20Sopenharmony_ciand for processing of PCM hardware parameters. These tracepoints are available
148c2ecf20Sopenharmony_ciwhen corresponding kernel configurations are enabled. When ``CONFIG_SND_DEBUG``
158c2ecf20Sopenharmony_ciis enabled, the latter tracepoints are available. When additional
168c2ecf20Sopenharmony_ci``SND_PCM_XRUN_DEBUG`` is enabled too, the former trace points are enabled.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciTracepoints for state of PCM buffer
198c2ecf20Sopenharmony_ci------------------------------------
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciThis category includes four tracepoints; ``hwptr``, ``applptr``, ``xrun`` and
228c2ecf20Sopenharmony_ci``hw_ptr_error``.
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciTracepoints for processing of PCM hardware parameters
258c2ecf20Sopenharmony_ci-----------------------------------------------------
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciThis category includes two tracepoints; ``hw_mask_param`` and
288c2ecf20Sopenharmony_ci``hw_interval_param``.
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciIn a design of ALSA PCM core, data transmission is abstracted as PCM substream.
318c2ecf20Sopenharmony_ciApplications manage PCM substream to maintain data transmission for PCM frames.
328c2ecf20Sopenharmony_ciBefore starting the data transmission, applications need to configure PCM
338c2ecf20Sopenharmony_cisubstream. In this procedure, PCM hardware parameters are decided by
348c2ecf20Sopenharmony_ciinteraction between applications and ALSA PCM core. Once decided, runtime of
358c2ecf20Sopenharmony_cithe PCM substream keeps the parameters.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciThe parameters are described in struct snd_pcm_hw_params. This
388c2ecf20Sopenharmony_cistructure includes several types of parameters. Applications set preferable
398c2ecf20Sopenharmony_civalue to these parameters, then execute ioctl(2) with SNDRV_PCM_IOCTL_HW_REFINE
408c2ecf20Sopenharmony_cior SNDRV_PCM_IOCTL_HW_PARAMS. The former is used just for refining available
418c2ecf20Sopenharmony_ciset of parameters. The latter is used for an actual decision of the parameters.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciThe struct snd_pcm_hw_params structure has below members:
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci``flags``
468c2ecf20Sopenharmony_ci        Configurable. ALSA PCM core and some drivers handle this flag to select
478c2ecf20Sopenharmony_ci        convenient parameters or change their behaviour.
488c2ecf20Sopenharmony_ci``masks``
498c2ecf20Sopenharmony_ci        Configurable. This type of parameter is described in
508c2ecf20Sopenharmony_ci        struct snd_mask and represent mask values. As of PCM protocol
518c2ecf20Sopenharmony_ci        v2.0.13, three types are defined.
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_ACCESS
548c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_FORMAT
558c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_SUBFORMAT
568c2ecf20Sopenharmony_ci``intervals``
578c2ecf20Sopenharmony_ci        Configurable. This type of parameter is described in
588c2ecf20Sopenharmony_ci        struct snd_interval and represent values with a range. As of
598c2ecf20Sopenharmony_ci        PCM protocol v2.0.13, twelve types are defined.
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_SAMPLE_BITS
628c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_FRAME_BITS
638c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_CHANNELS
648c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_RATE
658c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_PERIOD_TIME
668c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_PERIOD_SIZE
678c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_PERIOD_BYTES
688c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_PERIODS
698c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_BUFFER_TIME
708c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_BUFFER_SIZE
718c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_BUFFER_BYTES
728c2ecf20Sopenharmony_ci        - SNDRV_PCM_HW_PARAM_TICK_TIME
738c2ecf20Sopenharmony_ci``rmask``
748c2ecf20Sopenharmony_ci        Configurable. This is evaluated at ioctl(2) with
758c2ecf20Sopenharmony_ci        SNDRV_PCM_IOCTL_HW_REFINE only. Applications can select which
768c2ecf20Sopenharmony_ci        mask/interval parameter can be changed by ALSA PCM core. For
778c2ecf20Sopenharmony_ci        SNDRV_PCM_IOCTL_HW_PARAMS, this mask is ignored and all of parameters
788c2ecf20Sopenharmony_ci        are going to be changed.
798c2ecf20Sopenharmony_ci``cmask``
808c2ecf20Sopenharmony_ci        Read-only. After returning from ioctl(2), buffer in user space for
818c2ecf20Sopenharmony_ci        struct snd_pcm_hw_params includes result of each operation.
828c2ecf20Sopenharmony_ci        This mask represents which mask/interval parameter is actually changed.
838c2ecf20Sopenharmony_ci``info``
848c2ecf20Sopenharmony_ci        Read-only. This represents hardware/driver capabilities as bit flags
858c2ecf20Sopenharmony_ci        with SNDRV_PCM_INFO_XXX. Typically, applications execute ioctl(2) with
868c2ecf20Sopenharmony_ci        SNDRV_PCM_IOCTL_HW_REFINE to retrieve this flag, then decide candidates
878c2ecf20Sopenharmony_ci        of parameters and execute ioctl(2) with SNDRV_PCM_IOCTL_HW_PARAMS to
888c2ecf20Sopenharmony_ci        configure PCM substream.
898c2ecf20Sopenharmony_ci``msbits``
908c2ecf20Sopenharmony_ci        Read-only. This value represents available bit width in MSB side of
918c2ecf20Sopenharmony_ci        a PCM sample. When a parameter of SNDRV_PCM_HW_PARAM_SAMPLE_BITS was
928c2ecf20Sopenharmony_ci        decided as a fixed number, this value is also calculated according to
938c2ecf20Sopenharmony_ci        it. Else, zero. But this behaviour depends on implementations in driver
948c2ecf20Sopenharmony_ci        side.
958c2ecf20Sopenharmony_ci``rate_num``
968c2ecf20Sopenharmony_ci        Read-only. This value represents numerator of sampling rate in fraction
978c2ecf20Sopenharmony_ci        notation. Basically, when a parameter of SNDRV_PCM_HW_PARAM_RATE was
988c2ecf20Sopenharmony_ci        decided as a single value, this value is also calculated according to
998c2ecf20Sopenharmony_ci        it. Else, zero. But this behaviour depends on implementations in driver
1008c2ecf20Sopenharmony_ci        side.
1018c2ecf20Sopenharmony_ci``rate_den``
1028c2ecf20Sopenharmony_ci        Read-only. This value represents denominator of sampling rate in
1038c2ecf20Sopenharmony_ci        fraction notation. Basically, when a parameter of
1048c2ecf20Sopenharmony_ci        SNDRV_PCM_HW_PARAM_RATE was decided as a single value, this value is
1058c2ecf20Sopenharmony_ci        also calculated according to it. Else, zero. But this behaviour depends
1068c2ecf20Sopenharmony_ci        on implementations in driver side.
1078c2ecf20Sopenharmony_ci``fifo_size``
1088c2ecf20Sopenharmony_ci        Read-only. This value represents the size of FIFO in serial sound
1098c2ecf20Sopenharmony_ci        interface of hardware. Basically, each driver can assigns a proper
1108c2ecf20Sopenharmony_ci        value to this parameter but some drivers intentionally set zero with
1118c2ecf20Sopenharmony_ci        a care of hardware design or data transmission protocol.
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciALSA PCM core handles buffer of struct snd_pcm_hw_params when
1148c2ecf20Sopenharmony_ciapplications execute ioctl(2) with SNDRV_PCM_HW_REFINE or SNDRV_PCM_HW_PARAMS.
1158c2ecf20Sopenharmony_ciParameters in the buffer are changed according to
1168c2ecf20Sopenharmony_cistruct snd_pcm_hardware and rules of constraints in the runtime. The
1178c2ecf20Sopenharmony_cistructure describes capabilities of handled hardware. The rules describes
1188c2ecf20Sopenharmony_cidependencies on which a parameter is decided according to several parameters.
1198c2ecf20Sopenharmony_ciA rule has a callback function, and drivers can register arbitrary functions
1208c2ecf20Sopenharmony_cito compute the target parameter. ALSA PCM core registers some rules to the
1218c2ecf20Sopenharmony_ciruntime as a default.
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciEach driver can join in the interaction as long as it prepared for two stuffs
1248c2ecf20Sopenharmony_ciin a callback of struct snd_pcm_ops.open.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci1. In the callback, drivers are expected to change a member of
1278c2ecf20Sopenharmony_ci   struct snd_pcm_hardware type in the runtime, according to
1288c2ecf20Sopenharmony_ci   capacities of corresponding hardware.
1298c2ecf20Sopenharmony_ci2. In the same callback, drivers are also expected to register additional rules
1308c2ecf20Sopenharmony_ci   of constraints into the runtime when several parameters have dependencies
1318c2ecf20Sopenharmony_ci   due to hardware design.
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciThe driver can refers to result of the interaction in a callback of
1348c2ecf20Sopenharmony_cistruct snd_pcm_ops.hw_params, however it should not change the
1358c2ecf20Sopenharmony_cicontent.
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciTracepoints in this category are designed to trace changes of the
1388c2ecf20Sopenharmony_cimask/interval parameters. When ALSA PCM core changes them, ``hw_mask_param`` or
1398c2ecf20Sopenharmony_ci``hw_interval_param`` event is probed according to type of the changed parameter.
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciALSA PCM core also has a pretty print format for each of the tracepoints. Below
1428c2ecf20Sopenharmony_ciis an example for ``hw_mask_param``.
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci::
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci    hw_mask_param: pcmC0D0p 001/023 FORMAT 00000000000000000000001000000044 00000000000000000000001000000044
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciBelow is an example for ``hw_interval_param``.
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci::
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci    hw_interval_param: pcmC0D0p 000/023 BUFFER_SIZE 0 0 [0 4294967295] 0 1 [0 4294967295]
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciThe first three fields are common. They represent name of ALSA PCM character
1568c2ecf20Sopenharmony_cidevice, rules of constraint and name of the changed parameter, in order. The
1578c2ecf20Sopenharmony_cifield for rules of constraint consists of two sub-fields; index of applied rule
1588c2ecf20Sopenharmony_ciand total number of rules added to the runtime. As an exception, the index 000
1598c2ecf20Sopenharmony_cimeans that the parameter is changed by ALSA PCM core, regardless of the rules.
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciThe rest of field represent state of the parameter before/after changing. These
1628c2ecf20Sopenharmony_cifields are different according to type of the parameter. For parameters of mask
1638c2ecf20Sopenharmony_citype, the fields represent hexadecimal dump of content of the parameter. For
1648c2ecf20Sopenharmony_ciparameters of interval type, the fields represent values of each member of
1658c2ecf20Sopenharmony_ci``empty``, ``integer``, ``openmin``, ``min``, ``max``, ``openmax`` in
1668c2ecf20Sopenharmony_cistruct snd_interval in this order.
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciTracepoints in drivers
1698c2ecf20Sopenharmony_ci======================
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciSome drivers have tracepoints for developers' convenience. For them, please
1728c2ecf20Sopenharmony_cirefer to each documentation or implementation.
173