Friday, April 30, 2010

Writing an ALSA driver

Over the past week I've been writing an ALSA driver for an MPEG-4 capture board (4/8/16 channel). What I discovered is there are not many good documents on the basics of writing a simple ALSA driver. So I wanted to share my experience in the hopes that it would help others.

My driver needed to be pretty simple. The encoder produced 8Khz mono G.723-24 ADPCM. So you can avoid the wikepedia trip, that's 3-bits per sample, or 24000 bits per second. The card produced this at a rate of 128 samples per interrupt (48 bytes) for every channel available (you cannot disable each channel).

The card delivered this data in a 32kbyte buffer, split into 32 pages. Each page was written as 48*20 channels, which took up 960 bytes of the 1024 byte page (it could do up to this number, but for my purposes I was only using 4, 8 or 16 channels of encoded data depending on the capabilities of the card).

Now, let's set aside the fact that ALSA does not have a format spec for G.723-24, so my usage entails dumping out the 48 bytes to userspace as unsigned 8-bit PCM (and my userspace application handles the G.723-24 decoding, knowing that it is getting this data).

First, where to start in ALSA. I had to decide how to expose these capture interfaces. I could have exposed a capture device for each channel, but instead I chose to expose one capture interface with a subdevice for each channel. This made programming a bit easier, gave a better overview of the devices as perceived by ALSA, and kept /dev/snd/ less cluttered (especially when you had multiple 16-channel cards installed). It also made programming userspace easier since it kept channels hierarchically under the card/device.

Next post, I'll discuss how the initial ALSA driver is setup and exposed to userspace.

Next >>


  1. Despite the bulk of information online we often fail to get the specific information which is needed this post is good & contains relevant information that I was in quest of .I appreciate your efforts in preparing this post.

  2. This series of posts has certainly been useful to me - clear and well written. Much obliged.


  3. Great works Ben ...

    I would like to take this opportunity to let you know that I'm trying to write an audio driver for a RTOS (ThreadX) on spear300 from st microelectronics. Since it's an RTOS it does not offer the infrastructure like Linux. But still I'm keeping a linux driver with me for ready reference. I'm facing some problem while understanding the spear300 user manual's i2s block. Can u please refer to some material that throws some insight into how an audio hardware works on embedded platforms. E-mail :

  4. I'd like to second the previous post. Many thanks for taking the time and effort to post these articles. It's been very, very appreciated.

  5. Oops. It appears the first poster was a spam-bot. I should have looked more closely. However, I am still appreciative of your effort. It has given me a very nice starting point to my ALSA device-driver writing efforts.

    Well done.

  6. Wow... the best driver dev. tutorial i've read so far.
    Do you mind if I ask for a copy of the whole code? It would help me a lot in developing my own.
    I am currently developing drivers for both playback and capture and if you could make a similar tutorial for the playback feature that would be so great and really helpful, sir.

    Thank you so much and more power Sir Collins.
    Jose /Philippines