Rumination on ALSA Drivers
Takashi Iwai
SuSE Linux AG, Nuremberg, Germany
LAD Meeting, March 14, 2003
Karlsruhe, Germany
(page 1)
Outline
- Characteristics and History of ALSA
(page 2)
Characteristics of ALSA
Advanced Linux Sound Architecture
- Multi-platform (32 and 64bit)
- ix86, ia64, x86-64, alpha, ppc, ppc64, mips, arm, sparc, parisc...
- Multiple cards and devices
- up to 8 cards, 8 PCMs, 8 MIDIs per card
- Support of high-end soundcards
- multi-channel, 24/32bit, 96/192kHz, SPDIF...
- Flexible and extensible control/mixer
- Operations on user-space via ALSA-library
(page 3)
History - CVS Check-In Statistics
(page 4)
Structure
(page 5)
We Are Not Alone...
Different format types
- Linear Format
- 8bit (usually unsigned)
- 16bit (usually signed)
- Little-endian, Big-endian
- 24bit in 3 bytes
- 24bit in 4 bytes (MSB)
- 24bit in 4 bytes (LSB)
- 32bit
- Non-Linear Format
- mu-Law, a-Law
- Float
- ADPCM
- MPEG
(page 6)
Plugins - Behind the Scene
- Route plugin
- Reprograms the channel configuration.
- Attenuation
- Operation models: average, copy, duplicate
- Share plugin
- Shares a pcm device with several applications
- For multi-channel pcm chip
- Multi plugin
- Combines several pcm devices as if one.
- Will create a "complex" access type
- needs re-routing of channels for interleaved data
- Dmix plugin
- New plugin to mix streams dynamically
(page 7)
OSS Emulation
- Through kernel modules (snd-pcm-oss, snd-mixer-oss)
- No extra LD_PRELOAD hack
- Configuration in /etc/modules.conf (for kmod)
- Mapping is possible only via module options
- Through ALSA-OSS library (libaoss)
- LD_PRELOAD hack
- Configured in ~/.asoundrc
- Can use ALSA plugins (route etc.)
- Supports all PCM formats
- Can be mmapped on multi-channel cards
(page 8)
Driver Hierarchy
- Top-Level
- Detection of the device(s)
- Creation of ALSA components
- Destructors
- Middle-Level (ALSA common layer)
- Device manager
- PCM, rawmidi, synth, etc.
- Low-Level
- Access to the hardware
- Defined as callback functions
(page 9)
Different Hardwares
- ISA
- Old good things
- 16MB DMA space
- PCI
- Nowadays standard
- Wide range of devices
- Most of devices using AC97 codec
- Still problems with memory allocation
- USB
- Getting more popular
- Incompatible protocols (esp. MIDI)
- Low band-width (USB 1.x)
- Hotplug support
(page 10)
Different Hardwares (cont'd)
- PCMCIA
- Only a few devices
- Hotplug support
- Needs firmware
- PCCard - communication with cardmgr
- CardBus - compatible with PCI
- IEEE1394 (Firewire)
- Not many devices (yet)
- Standard protocol?
- Implementation on kernel or user-space?
(page 11)
Memory Management
Memory Allocation
- Using standard Linux functions.
- Provides the uniform API for different BUS types.
Large continuous physical memory
- Pre-allocation
- Allocating bulk buffers in advance per PCM stream.
- Configurable via /proc interface.
- Buffer-cache
- Keep the pre-alloc'ed buffers in an independent module.
- Preserve buffers at the early boot stage (NYI).
(page 12)
Buffer Types
- DMA with Linear Buffer
- Most of hardwares.
- Continuous physical area.
- DMA with Scatter-Gather Buffer
- Emu10k1, Trident, etc.
- Having own Page Table or Buffer-Descriptors.
- Allocates small discrete pages and put them on the table.
- Pages are mapped virtually linear.
- Hardware-Buffer on Memory Mapped IO
- Depending on architecture, mmap or copy.
- Using Intermediate Buffer
- Non-DMA or special hardware buffers.
- Intermediate buffer can be SG-buffer and mmaped.
(page 13)
Example - Emu10k1 Buffers
- Dynamic remapping on H/W page table
- PCM buffers prior to Synth buffers
(page 14)
Memory Mapping
To Be or Not To Be?
- More efficient - no copy of data between buffers.
- ALSA maps the control record, too.
- minimum kernel <> user switch
- Not all devices support mmap.
- Different API from standard read/write.
- (Solvable on alsa-lib, though)
(page 15)
Mixers
Handled via ALSA control API
- The id consists of
- name string (e.g. "Master Playback Volume")
- index number
- Several data types
- integer (ranged)
- boolean
- enumerated list
- bytes array, IEC958 status, etc.
OSS mixer elements are composed from the IDs
Line = Line Playback Volume, Line Playback Switch, Line Capture Volume...
(page 16)
More Is Better...?
There are too many mixer elements
- Difficult to detect the existence of parts
- SPDIF, Mono, Headphone, etc.
- Controls per PCM stream
- No routing information unlike 0.5.x
Need to handle card-specific mixer-topology.
- External database
- Implementation of GUI
(page 17)
Future Works / TODOs
- Clock / sample-rate synchronization
- Allow variable rate in the pcm?
- PCMs more than the current limit (8)
- Reimplementation of ALSA sequencer
(page 18)
Resources
ALSA project homepage
http://www.alsa-project.org
ALSA Wiki
http://alsa.opensrc.org
Linux Audio Developers
http://www.linuxdj.com/audio/lad/
ALSA Driver's HOWTO
http://www.alsa-project.org/~iwai/writing-an-alsa-driver/
HTTP version of these slides
http://www.alsa-project.org/~iwai/lad2003/lad.html
(page 19)