1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Non-Contiguous Buffers</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="buffer-and-memory.html" title="Chapter&#160;11.&#160;Buffer and Memory Management"><link rel="prev" href="buffer-and-memory-external-hardware.html" title="External Hardware Buffers"><link rel="next" href="buffer-and-memory-vmalloced.html" title="Vmalloc'ed Buffers"></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">Non-Contiguous Buffers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="buffer-and-memory-external-hardware.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;11.&#160;Buffer and Memory Management</th><td width="20%" align="right">&#160;<a accesskey="n" href="buffer-and-memory-vmalloced.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="buffer-and-memory-non-contiguous"></a>Non-Contiguous Buffers</h2></div></div></div><p>
2        If your hardware supports the page table as in emu10k1 or the
3      buffer descriptors as in via82xx, you can use the scatter-gather
4      (SG) DMA. ALSA provides an interface for handling SG-buffers.
5      The API is provided in <code class="filename">&lt;sound/pcm.h&gt;</code>. 
6      </p><p>
7        For creating the SG-buffer handler, call
8        <code class="function">snd_pcm_lib_preallocate_pages()</code> or
9        <code class="function">snd_pcm_lib_preallocate_pages_for_all()</code>
10        with <code class="constant">SNDRV_DMA_TYPE_DEV_SG</code>
11	in the PCM constructor like other PCI pre-allocator.
12        You need to pass <code class="function">snd_dma_pci_data(pci)</code>,
13        where pci is the struct <span class="structname">pci_dev</span> pointer
14        of the chip as well.
15        The <span class="type">struct snd_sg_buf</span> instance is created as
16        substream-&gt;dma_private. You can cast
17        the pointer like: 
18
19        </p><div class="informalexample"><pre class="programlisting">
20
21  struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream-&gt;dma_private;
22
23          </pre></div><p>
24      </p><p>
25        Then call <code class="function">snd_pcm_lib_malloc_pages()</code>
26      in the <em class="structfield"><code>hw_params</code></em> callback
27      as well as in the case of normal PCI buffer.
28      The SG-buffer handler will allocate the non-contiguous kernel
29      pages of the given size and map them onto the virtually contiguous
30      memory.  The virtual pointer is addressed in runtime-&gt;dma_area.
31      The physical address (runtime-&gt;dma_addr) is set to zero,
32      because the buffer is physically non-contiguous.
33      The physical address table is set up in sgbuf-&gt;table.
34      You can get the physical address at a certain offset via
35      <code class="function">snd_pcm_sgbuf_get_addr()</code>. 
36      </p><p>
37        When a SG-handler is used, you need to set
38      <code class="function">snd_pcm_sgbuf_ops_page</code> as
39      the <em class="structfield"><code>page</code></em> callback.
40      (See <a class="link" href="pcm-interface-operators.html#pcm-interface-operators-page-callback" title="page callback">
41      <em class="citetitle">page callback section</em></a>.)
42      </p><p>
43        To release the data, call
44      <code class="function">snd_pcm_lib_free_pages()</code> in the
45      <em class="structfield"><code>hw_free</code></em> callback as usual.
46      </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="buffer-and-memory-external-hardware.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="buffer-and-memory.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="buffer-and-memory-vmalloced.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">External Hardware Buffers&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Vmalloc'ed Buffers</td></tr></table></div></body></html>
47