1<html><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.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-example.html" title="Full Code Example"><link rel="next" href="pcm-interface-destructor.html" title="... And the Destructor?"></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">Constructor</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pcm-interface-example.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="pcm-interface-destructor.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-constructor"></a>Constructor</h2></div></div></div><p>
2        A pcm instance is allocated by the <code class="function">snd_pcm_new()</code>
3      function. It would be better to create a constructor for pcm,
4      namely, 
5
6        </p><div class="informalexample"><pre class="programlisting">
7
8  static int snd_mychip_new_pcm(struct mychip *chip)
9  {
10          struct snd_pcm *pcm;
11          int err;
12
13          err = snd_pcm_new(chip-&gt;card, "My Chip", 0, 1, 1, &amp;pcm);
14          if (err &lt; 0) 
15                  return err;
16          pcm-&gt;private_data = chip;
17          strcpy(pcm-&gt;name, "My Chip");
18          chip-&gt;pcm = pcm;
19	  ....
20          return 0;
21  }
22
23          </pre></div><p>
24      </p><p>
25        The <code class="function">snd_pcm_new()</code> function takes four
26      arguments. The first argument is the card pointer to which this
27      pcm is assigned, and the second is the ID string. 
28      </p><p>
29        The third argument (<em class="parameter"><code>index</code></em>, 0 in the
30      above) is the index of this new pcm. It begins from zero. If
31      you create more than one pcm instances, specify the
32      different numbers in this argument. For example,
33      <em class="parameter"><code>index</code></em> = 1 for the second PCM device.  
34      </p><p>
35        The fourth and fifth arguments are the number of substreams
36      for playback and capture, respectively. Here 1 is used for
37      both arguments. When no playback or capture substreams are available,
38      pass 0 to the corresponding argument.
39      </p><p>
40        If a chip supports multiple playbacks or captures, you can
41      specify more numbers, but they must be handled properly in
42      open/close, etc. callbacks.  When you need to know which
43      substream you are referring to, then it can be obtained from
44      struct <span class="structname">snd_pcm_substream</span> data passed to each callback
45      as follows: 
46
47        </p><div class="informalexample"><pre class="programlisting">
48
49  struct snd_pcm_substream *substream;
50  int index = substream-&gt;number;
51
52          </pre></div><p>
53      </p><p>
54        After the pcm is created, you need to set operators for each
55        pcm stream. 
56
57        </p><div class="informalexample"><pre class="programlisting">
58
59  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
60                  &amp;snd_mychip_playback_ops);
61  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
62                  &amp;snd_mychip_capture_ops);
63
64          </pre></div><p>
65      </p><p>
66        The operators are defined typically like this:
67
68        </p><div class="informalexample"><pre class="programlisting">
69
70  static struct snd_pcm_ops snd_mychip_playback_ops = {
71          .open =        snd_mychip_pcm_open,
72          .close =       snd_mychip_pcm_close,
73          .ioctl =       snd_pcm_lib_ioctl,
74          .hw_params =   snd_mychip_pcm_hw_params,
75          .hw_free =     snd_mychip_pcm_hw_free,
76          .prepare =     snd_mychip_pcm_prepare,
77          .trigger =     snd_mychip_pcm_trigger,
78          .pointer =     snd_mychip_pcm_pointer,
79  };
80
81          </pre></div><p>
82
83        All the callbacks are described in the
84        <a class="link" href="pcm-interface-operators.html" title="Operators"><em class="citetitle">
85        Operators</em></a> subsection.
86      </p><p>
87        After setting the operators, you probably will want to
88        pre-allocate the buffer. For the pre-allocation, simply call
89        the following: 
90
91        </p><div class="informalexample"><pre class="programlisting">
92
93  snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
94                                        snd_dma_pci_data(chip-&gt;pci),
95                                        64*1024, 64*1024);
96
97          </pre></div><p>
98
99        It will allocate a buffer up to 64kB as default.
100      Buffer management details will be described in the later section <a class="link" href="buffer-and-memory.html" title="Chapter&#160;11.&#160;Buffer and Memory Management"><em class="citetitle">Buffer and Memory
101      Management</em></a>. 
102      </p><p>
103        Additionally, you can set some extra information for this pcm
104        in pcm-&gt;info_flags.
105        The available values are defined as
106        <code class="constant">SNDRV_PCM_INFO_XXX</code> in
107        <code class="filename">&lt;sound/asound.h&gt;</code>, which is used for
108        the hardware definition (described later). When your soundchip
109        supports only half-duplex, specify like this: 
110
111        </p><div class="informalexample"><pre class="programlisting">
112
113  pcm-&gt;info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
114
115          </pre></div><p>
116      </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pcm-interface-example.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="pcm-interface-destructor.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>
117