<?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>Non-Contiguous Buffers</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="ch11.html" title="Chapter 11. Buffer and Memory Management" /><link rel="prev" href="ch11s02.html" title="External Hardware Buffers" /><link rel="next" href="ch11s04.html" title="Vmalloc'ed Buffers" /></head><body><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="ch11s02.html">Prev</a> </td><th width="60%" align="center">Chapter 11. Buffer and Memory Management</th><td width="20%" align="right"> <a accesskey="n" href="ch11s04.html">Next</a></td></tr></table><hr /></div><div class="section" title="Non-Contiguous Buffers"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="buffer-and-memory-non-contiguous"></a>Non-Contiguous Buffers</h2></div></div></div><p> If your hardware supports the page table as in emu10k1 or the buffer descriptors as in via82xx, you can use the scatter-gather (SG) DMA. ALSA provides an interface for handling SG-buffers. The API is provided in <code class="filename"><sound/pcm.h></code>. </p><p> For creating the SG-buffer handler, call <code class="function">snd_pcm_lib_preallocate_pages()</code> or <code class="function">snd_pcm_lib_preallocate_pages_for_all()</code> with <code class="constant">SNDRV_DMA_TYPE_DEV_SG</code> in the PCM constructor like other PCI pre-allocator. You need to pass <code class="function">snd_dma_pci_data(pci)</code>, where pci is the struct <span class="structname">pci_dev</span> pointer of the chip as well. The <span class="type">struct snd_sg_buf</span> instance is created as substream->dma_private. You can cast the pointer like: </p><div class="informalexample"><pre class="programlisting"> struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private; </pre></div><p> </p><p> Then call <code class="function">snd_pcm_lib_malloc_pages()</code> in the <em class="structfield"><code>hw_params</code></em> callback as well as in the case of normal PCI buffer. The SG-buffer handler will allocate the non-contiguous kernel pages of the given size and map them onto the virtually contiguous memory. The virtual pointer is addressed in runtime->dma_area. The physical address (runtime->dma_addr) is set to zero, because the buffer is physically non-contigous. The physical address table is set up in sgbuf->table. You can get the physical address at a certain offset via <code class="function">snd_pcm_sgbuf_get_addr()</code>. </p><p> When a SG-handler is used, you need to set <code class="function">snd_pcm_sgbuf_ops_page</code> as the <em class="structfield"><code>page</code></em> callback. (See <a class="link" href="ch05s06.html#pcm-interface-operators-page-callback" title="page callback"> <em class="citetitle">page callback section</em></a>.) </p><p> To release the data, call <code class="function">snd_pcm_lib_free_pages()</code> in the <em class="structfield"><code>hw_free</code></em> callback as usual. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch11s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch11.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch11s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">External Hardware Buffers </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Vmalloc'ed Buffers</td></tr></table></div></body></html>