1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Constraints</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Writing an ALSA Driver"><link rel="up" href="pcm-interface.html" title="Chapter&#160;5.&#160;PCM Interface"><link rel="prev" href="pcm-interface-atomicity.html" title="Atomicity"><link rel="next" href="control-interface.html" title="Chapter&#160;6.&#160;Control Interface"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Constraints</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pcm-interface-atomicity.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;5.&#160;PCM Interface</th><td width="20%" align="right">&#160;<a accesskey="n" href="control-interface.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="pcm-interface-constraints"></a>Constraints</h2></div></div></div><p>
2        If your chip supports unconventional sample rates, or only the
3      limited samples, you need to set a constraint for the
4      condition. 
5      </p><p>
6        For example, in order to restrict the sample rates in the some
7        supported values, use
8	<code class="function">snd_pcm_hw_constraint_list()</code>.
9	You need to call this function in the open callback.
10
11        </p><div class="example"><a name="idp1094149308"></a><p class="title"><b>Example&#160;5.5.&#160;Example of Hardware Constraints</b></p><div class="example-contents"><pre class="programlisting">
12
13  static unsigned int rates[] =
14          {4000, 10000, 22050, 44100};
15  static struct snd_pcm_hw_constraint_list constraints_rates = {
16          .count = ARRAY_SIZE(rates),
17          .list = rates,
18          .mask = 0,
19  };
20
21  static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
22  {
23          int err;
24          ....
25          err = snd_pcm_hw_constraint_list(substream-&gt;runtime, 0,
26                                           SNDRV_PCM_HW_PARAM_RATE,
27                                           &amp;constraints_rates);
28          if (err &lt; 0)
29                  return err;
30          ....
31  }
32
33          </pre></div></div><p><br class="example-break">
34      </p><p>
35        There are many different constraints.
36        Look at <code class="filename">sound/pcm.h</code> for a complete list.
37        You can even define your own constraint rules.
38        For example, let's suppose my_chip can manage a substream of 1 channel
39        if and only if the format is S16_LE, otherwise it supports any format
40        specified in the <span class="structname">snd_pcm_hardware</span> structure (or in any
41        other constraint_list). You can build a rule like this:
42
43        </p><div class="example"><a name="idp1094152404"></a><p class="title"><b>Example&#160;5.6.&#160;Example of Hardware Constraints for Channels</b></p><div class="example-contents"><pre class="programlisting">
44
45  static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
46                                        struct snd_pcm_hw_rule *rule)
47  {
48          struct snd_interval *c = hw_param_interval(params,
49                        SNDRV_PCM_HW_PARAM_CHANNELS);
50          struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
51          struct snd_interval ch;
52
53          snd_interval_any(&amp;ch);
54          if (f-&gt;bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
55                  ch.min = ch.max = 1;
56                  ch.integer = 1;
57                  return snd_interval_refine(c, &amp;ch);
58          }
59          return 0;
60  }
61
62          </pre></div></div><p><br class="example-break">
63      </p><p>
64        Then you need to call this function to add your rule:
65
66       </p><div class="informalexample"><pre class="programlisting">
67
68  snd_pcm_hw_rule_add(substream-&gt;runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
69                      hw_rule_channels_by_format, NULL,
70                      SNDRV_PCM_HW_PARAM_FORMAT, -1);
71
72          </pre></div><p>
73      </p><p>
74        The rule function is called when an application sets the PCM
75	format, and it refines the number of channels accordingly.
76        But an application may set the number of channels before
77	setting the format. Thus you also need to define the inverse rule:
78
79       </p><div class="example"><a name="idp1094156172"></a><p class="title"><b>Example&#160;5.7.&#160;Example of Hardware Constraints for Formats</b></p><div class="example-contents"><pre class="programlisting">
80
81  static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
82                                        struct snd_pcm_hw_rule *rule)
83  {
84          struct snd_interval *c = hw_param_interval(params,
85                SNDRV_PCM_HW_PARAM_CHANNELS);
86          struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
87          struct snd_mask fmt;
88
89          snd_mask_any(&amp;fmt);    /* Init the struct */
90          if (c-&gt;min &lt; 2) {
91                  fmt.bits[0] &amp;= SNDRV_PCM_FMTBIT_S16_LE;
92                  return snd_mask_refine(f, &amp;fmt);
93          }
94          return 0;
95  }
96
97          </pre></div></div><p><br class="example-break">
98      </p><p>
99      ...and in the open callback:
100       </p><div class="informalexample"><pre class="programlisting">
101
102  snd_pcm_hw_rule_add(substream-&gt;runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
103                      hw_rule_format_by_channels, NULL,
104                      SNDRV_PCM_HW_PARAM_CHANNELS, -1);
105
106          </pre></div><p>
107      </p><p>
108        I won't give more details here, rather I
109        would like to say, <span class="quote">&#8220;<span class="quote">Luke, use the source.</span>&#8221;</span>
110      </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pcm-interface-atomicity.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="pcm-interface.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="control-interface.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Atomicity&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Chapter&#160;6.&#160;Control Interface</td></tr></table></div></body></html>
111