Sophie

Sophie

distrib > CentOS > 6 > i386 > by-pkgid > 2c51d8eb79f8810ada971ee8c30ce1e5 > files > 4129

kernel-doc-2.6.32-71.14.1.el6.noarch.rpm

<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><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.75.2" /><link rel="home" href="index.html" title="Writing an ALSA Driver" /><link rel="up" href="ch05.html" title="Chapter&#160;5.&#160;PCM Interface" /><link rel="prev" href="ch05s08.html" title="Atomicity" /><link rel="next" href="ch06.html" title="Chapter&#160;6.&#160;Control Interface" /></head><body><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="ch05s08.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="ch06.html">Next</a></td></tr></table><hr /></div><div class="section" title="Constraints"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="pcm-interface-constraints"></a>Constraints</h2></div></div></div><p>
        If your chip supports unconventional sample rates, or only the
      limited samples, you need to set a constraint for the
      condition. 
      </p><p>
        For example, in order to restrict the sample rates in the some
        supported values, use
	<code class="function">snd_pcm_hw_constraint_list()</code>.
	You need to call this function in the open callback.

        </p><div class="example"><a id="id2948496"></a><p class="title"><b>Example&#160;5.5.&#160;Example of Hardware Constraints</b></p><div class="example-contents"><pre class="programlisting">

  static unsigned int rates[] =
          {4000, 10000, 22050, 44100};
  static struct snd_pcm_hw_constraint_list constraints_rates = {
          .count = ARRAY_SIZE(rates),
          .list = rates,
          .mask = 0,
  };

  static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
  {
          int err;
          ....
          err = snd_pcm_hw_constraint_list(substream-&gt;runtime, 0,
                                           SNDRV_PCM_HW_PARAM_RATE,
                                           &amp;constraints_rates);
          if (err &lt; 0)
                  return err;
          ....
  }

          </pre></div></div><p><br class="example-break" />
      </p><p>
        There are many different constraints.
        Look at <code class="filename">sound/pcm.h</code> for a complete list.
        You can even define your own constraint rules.
        For example, let's suppose my_chip can manage a substream of 1 channel
        if and only if the format is S16_LE, otherwise it supports any format
        specified in the <span class="structname">snd_pcm_hardware</span> structure (or in any
        other constraint_list). You can build a rule like this:

        </p><div class="example"><a id="id2948548"></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">

  static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
                                        struct snd_pcm_hw_rule *rule)
  {
          struct snd_interval *c = hw_param_interval(params,
                SNDRV_PCM_HW_PARAM_CHANNELS);
          struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
          struct snd_mask fmt;

          snd_mask_any(&amp;fmt);    /* Init the struct */
          if (c-&gt;min &lt; 2) {
                  fmt.bits[0] &amp;= SNDRV_PCM_FMTBIT_S16_LE;
                  return snd_mask_refine(f, &amp;fmt);
          }
          return 0;
  }

          </pre></div></div><p><br class="example-break" />
      </p><p>
        Then you need to call this function to add your rule:

       </p><div class="informalexample"><pre class="programlisting">

  snd_pcm_hw_rule_add(substream-&gt;runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
                      hw_rule_channels_by_format, 0, SNDRV_PCM_HW_PARAM_FORMAT,
                      -1);

          </pre></div><p>
      </p><p>
        The rule function is called when an application sets the number of
        channels. But an application can set the format before the number of
        channels. Thus you also need to define the inverse rule:

       </p><div class="example"><a id="id2948609"></a><p class="title"><b>Example&#160;5.7.&#160;Example of Hardware Constraints for Channels</b></p><div class="example-contents"><pre class="programlisting">

  static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
                                        struct snd_pcm_hw_rule *rule)
  {
          struct snd_interval *c = hw_param_interval(params,
                        SNDRV_PCM_HW_PARAM_CHANNELS);
          struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
          struct snd_interval ch;

          snd_interval_any(&amp;ch);
          if (f-&gt;bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
                  ch.min = ch.max = 1;
                  ch.integer = 1;
                  return snd_interval_refine(c, &amp;ch);
          }
          return 0;
  }

          </pre></div></div><p><br class="example-break" />
      </p><p>
      ...and in the open callback:
       </p><div class="informalexample"><pre class="programlisting">

  snd_pcm_hw_rule_add(substream-&gt;runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
                      hw_rule_format_by_channels, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
                      -1);

          </pre></div><p>
      </p><p>
        I won't give more details here, rather I
        would like to say, <span class="quote">&#8220;<span class="quote">Luke, use the source.</span>&#8221;</span>
      </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch05s08.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="ch05.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="ch06.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>