Sophie

Sophie

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

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>Constructor</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="ch05s02.html" title="Full Code Example" /><link rel="next" href="ch05s04.html" title="... And the Destructor?" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Constructor</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch05s02.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="ch05s04.html">Next</a></td></tr></table><hr /></div><div class="section" title="Constructor"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="pcm-interface-constructor"></a>Constructor</h2></div></div></div><p>
        A pcm instance is allocated by the <code class="function">snd_pcm_new()</code>
      function. It would be better to create a constructor for pcm,
      namely, 

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

  static int __devinit snd_mychip_new_pcm(struct mychip *chip)
  {
          struct snd_pcm *pcm;
          int err;

          err = snd_pcm_new(chip-&gt;card, "My Chip", 0, 1, 1, &amp;pcm);
          if (err &lt; 0) 
                  return err;
          pcm-&gt;private_data = chip;
          strcpy(pcm-&gt;name, "My Chip");
          chip-&gt;pcm = pcm;
	  ....
          return 0;
  }

          </pre></div><p>
      </p><p>
        The <code class="function">snd_pcm_new()</code> function takes four
      arguments. The first argument is the card pointer to which this
      pcm is assigned, and the second is the ID string. 
      </p><p>
        The third argument (<em class="parameter"><code>index</code></em>, 0 in the
      above) is the index of this new pcm. It begins from zero. If
      you create more than one pcm instances, specify the
      different numbers in this argument. For example,
      <em class="parameter"><code>index</code></em> = 1 for the second PCM device.  
      </p><p>
        The fourth and fifth arguments are the number of substreams
      for playback and capture, respectively. Here 1 is used for
      both arguments. When no playback or capture substreams are available,
      pass 0 to the corresponding argument.
      </p><p>
        If a chip supports multiple playbacks or captures, you can
      specify more numbers, but they must be handled properly in
      open/close, etc. callbacks.  When you need to know which
      substream you are referring to, then it can be obtained from
      struct <span class="structname">snd_pcm_substream</span> data passed to each callback
      as follows: 

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

  struct snd_pcm_substream *substream;
  int index = substream-&gt;number;

          </pre></div><p>
      </p><p>
        After the pcm is created, you need to set operators for each
        pcm stream. 

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

  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
                  &amp;snd_mychip_playback_ops);
  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
                  &amp;snd_mychip_capture_ops);

          </pre></div><p>
      </p><p>
        The operators are defined typically like this:

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

  static struct snd_pcm_ops snd_mychip_playback_ops = {
          .open =        snd_mychip_pcm_open,
          .close =       snd_mychip_pcm_close,
          .ioctl =       snd_pcm_lib_ioctl,
          .hw_params =   snd_mychip_pcm_hw_params,
          .hw_free =     snd_mychip_pcm_hw_free,
          .prepare =     snd_mychip_pcm_prepare,
          .trigger =     snd_mychip_pcm_trigger,
          .pointer =     snd_mychip_pcm_pointer,
  };

          </pre></div><p>

        All the callbacks are described in the
        <a class="link" href="ch05s06.html" title="Operators"><em class="citetitle">
        Operators</em></a> subsection.
      </p><p>
        After setting the operators, you probably will want to
        pre-allocate the buffer. For the pre-allocation, simply call
        the following: 

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

  snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
                                        snd_dma_pci_data(chip-&gt;pci),
                                        64*1024, 64*1024);

          </pre></div><p>

        It will allocate a buffer up to 64kB as default.
      Buffer management details will be described in the later section <a class="link" href="ch11.html" title="Chapter&#160;11.&#160;Buffer and Memory Management"><em class="citetitle">Buffer and Memory
      Management</em></a>. 
      </p><p>
        Additionally, you can set some extra information for this pcm
        in pcm-&gt;info_flags.
        The available values are defined as
        <code class="constant">SNDRV_PCM_INFO_XXX</code> in
        <code class="filename">&lt;sound/asound.h&gt;</code>, which is used for
        the hardware definition (described later). When your soundchip
        supports only half-duplex, specify like this: 

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

  pcm-&gt;info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;

          </pre></div><p>
      </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch05s02.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="ch05s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Full Code Example&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;... And the Destructor?</td></tr></table></div></body></html>