static struct snd_pcm_ops my_pcm_ops = {
.open = my_pcm_open,
.close = my_pcm_close,
.ioctl = snd_pcm_lib_ioctl,
.hw_params = my_hw_params,
.hw_free = my_hw_free,
.prepare = my_pcm_prepare,
.trigger = my_pcm_trigger,
.pointer = my_pcm_pointer,
.copy = my_pcm_copy,
};
First let's start off with the open and close methods defined in this structure. This is where your driver gets notified that someone has opened the capture device (file open) and subsequently closed it.
static int my_pcm_open(struct snd_pcm_substream *ss)
{
ss->runtime->hw = my_pcm_hw;
ss->private_data = my_dev;
return 0;
}
static int my_pcm_close(struct snd_pcm_substream *ss)
{
ss->private_data = NULL;
return 0;
}
This is the minimum you would do for these two functions. If needed, you would allocate private data for this stream and free it on close.
For the ioctl handler, unless you need something special, you can just use the standard snd_pcm_lib_ioctl callback.
The next three callbacks handle hardware setup.
static int my_hw_params(struct snd_pcm_substream *ss,
struct snd_pcm_hw_params *hw_params)
{
return snd_pcm_lib_malloc_pages(ss,
params_buffer_bytes(hw_params));
}
static int my_hw_free(struct snd_pcm_substream *ss)
{
return snd_pcm_lib_free_pages(ss);
}
static int my_pcm_prepare(struct snd_pcm_substream *ss)
{
return 0;
}
Since we've been using standard memory allocation routines from ALSA, these functions stay fairly simple. If you have some special exceptions between different versions of the hardware supported by your driver, you can make changes to the ss->hw structure here (e.g. if one version of your card supports 96khz, but the rest only support 48khz max).
The PCM prepare callback should handle anything your driver needs to do before alsa-lib can ask it to start sending buffers. My driver doesn't do anything special here, so I have an empty callback.
This next handler tells your driver when ALSA is going to start and stop capturing buffers from your device. Most likely you will enable and disable interrupts here.
static int my_pcm_trigger(struct snd_pcm_substream *ss,
int cmd)
{
struct my_device *my_dev = snd_pcm_substream_chip(ss);
int ret = 0;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
// Start the hardware capture
break;
case SNDRV_PCM_TRIGGER_STOP:
// Stop the hardware capture
break;
default:
ret = -EINVAL;
}
return ret;
}
Let's move on to the handlers that are the work horse in my driver. Since the hardware that I'm writing my driver for cannot directly DMA into memory that ALSA has supplied for us to communicate with userspace, I need to make use of the copy handler to perform this operation.
static snd_pcm_uframes_t my_pcm_pointer(struct snd_pcm_substream *ss)
{
struct my_device *my_dev = snd_pcm_substream_chip(ss);
return my_dev->hw_idx;
}
static int my_pcm_copy(struct snd_pcm_substream *ss,
int channel, snd_pcm_uframes_t pos,
void __user *dst,
snd_pcm_uframes_t count)
{
struct my_device *my_dev = snd_pcm_substream_chip(ss);
return copy_to_user(dst, my_dev->buffer + pos, count);
}
So here we've defined a pointer function which gets called by userspace to find our where the hardware is in writing to the buffer.
Next, we have the actual copy function. You should note that count and pos are in sample sizes, not bytes. The buffer I've shown we assume to have been filled during interrupt.
Speaking of interrupt, that is where you should also signal to ALSA that you have more data to consume. In my ISR (interrupt service routine), I have this:
snd_pcm_period_elapsed(my_dev->ss);
And I think we're done. Hopefully now you have at least the stubs in place for a working driver, and will be able to fill in the details for your hardware. One day I may come back and write another post on how to add mixer controls (e.g. volume).
Hope this series has helped you out!
<< Prev
COngrats for the tutorial. THanks
ReplyDeleteexcellent guide...
ReplyDeleteThanks for this!
ReplyDeletethank u so much.........wat information u gave i didnot get any where in whole internet as clearly as u explained...........thanks so much.........keep giving like this information and in more depth.........
ReplyDeleteVery Good Material
ReplyDeleteThank you for this material.I have been struggling for longtime to find something like this
ReplyDeletei still have zero, nada, no clue where to start....
ReplyDeleteThank you very much for this article. Its simple and clear. Good start to understand and write a asla driver.
ReplyDeleteThanks, Ben, for putting this together. I have been struggling to find something of this sort for a while. Please also make good your intent to write on mixer controls!
ReplyDeleteHII.... Its a very good tutorial for new learner .. actually can u please tell me where to implement "channel positions" in a playback time....
ReplyDeleteswadhinsekhar05@gmail.com
Great article, very very helpful, very clear and to the point ... thank you!
ReplyDeleteDo any one have above code implemented for platform driver. please share if available. i want to learn this .
ReplyDeletesnd_pcm_substream_chip()
ReplyDeletewhat it does?
I want to know meaning and usage of this function..
Thank you