<?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.76.1" /><link rel="home" href="index.html" title="Writing an ALSA Driver" /><link rel="up" href="pcm-interface.html" title="Chapter 5. 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><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> </td><th width="60%" align="center">Chapter 5. PCM Interface</th><td width="20%" align="right"> <a accesskey="n" href="pcm-interface-destructor.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->card, "My Chip", 0, 1, 1, &pcm); if (err < 0) return err; pcm->private_data = chip; strcpy(pcm->name, "My Chip"); chip->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->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, &snd_mychip_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &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="pcm-interface-operators.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->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="buffer-and-memory.html" title="Chapter 11. 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->info_flags. The available values are defined as <code class="constant">SNDRV_PCM_INFO_XXX</code> in <code class="filename"><sound/asound.h></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->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="pcm-interface-example.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="pcm-interface.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pcm-interface-destructor.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Full Code Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> ... And the Destructor?</td></tr></table></div></body></html>