Skip to content

add mmap support and some cleanups to bcm2835 ALSA driver - #286

Merged
popcornmix merged 5 commits into
raspberrypi:rpi-3.6.yfrom
martinezjavier:rpi-3.6.y-dev
Apr 26, 2013
Merged

popcornmix merged 5 commits into
raspberrypi:rpi-3.6.yfrom
martinezjavier:rpi-3.6.y-dev

Conversation

@martinezjavier

Copy link
Copy Markdown
Contributor

Hello,

This series contains the following improvements to the ALSA bcm2835 driver:

  • Use the kernel provided facility to wait for completion instead of an
    ad-hoc implementation using semaphores.
  • Add defined constants to specify the deferred action to be processed by the
    driver workqueue instead of using magic numbers.
  • Include PCM samples write in the deferred operations handled by the workqueue
  • Add mmap support to allow Direct Read/Write transfers.
  • Remove unnecessary log when the driver probe function is called if debug is
    not enabled. Also replace printk() usage by more suitable functions.

and fixes these open issues:

#158 "bcm2835 ALSA driver hardware mixing or mmap?"
raspberrypi/firmware#173 "ALSA is not supporting 16 bits signed audio"
#282 "ALSA boot messages"

The following changes since commit 07b8b7d:

Merge pull request #279 from P33M/rpi-3.6.y (2013-04-22 08:08:49 -0700)

are available in the git repository at:

git://github.com/martinezjavier/linux-rpi.git rpi-3.6.y-dev

Javier Martinez Canillas (5):
ALSA: bcm2835: show card and subdev creation msg only on debug
ALSA: bcm2835: use a completion instead of a semaphore for sync
ALSA: bcm2835: don't use magic numbers on audio start/stop workqueue
ALSA: bcm2835: defer bcm2835 PCM write using the workqueue
ALSA: bcm2835: add memory-mapped I/O mode for audio stream

sound/arm/bcm2835-pcm.c | 69 ++++++++++++++++++++++-------------
sound/arm/bcm2835-vchiq.c | 89 +++++++++++++++++++++++++++++++--------------
sound/arm/bcm2835.c | 34 +++++++++---------
sound/arm/bcm2835.h | 2 +
4 files changed, 124 insertions(+), 70 deletions(-)

Thanks a lot and best regards,
Javier

Javier Martinez Canillas added 5 commits April 25, 2013 21:12
On driver probe a message showing that a card and its subdevices
have been created is shown. Probably this message is not needed
unless we have debug enabled on the driver. So, use the driver
audio_info() debug macro instead of just printk().

Also is better to use dev_err() and pr_err() instead KERN_ERR.

Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
The kernel completion interface is a simple synchronization
mechanism that is preferable over semaphores to signal the
occurence of an event.

Semaphores are optimized for the non-contention case since
in most situations they will be available. But the wait for
completion is the opposite case since the code calling down
will wait on most cases.

So, is better to use the completion mechanism that has been
developed for this exact use case instead of semaphores.

Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
The bcm2835 driver uses a workqueue to defer PCM playback start
and stop. Commands are passed to the function as magic numbers
so is more readable to use macro constants.

Also, while being there change the generic name "x" to "cmd"
for the var used to pass the start/stop commands.

Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
Only PCM playback and stop are deferred using a workqueue on
the ALSA bcm2835 driver. The actual writing of PCM samples
is synchronous.

This works well for the read/write transfer method since
the snd_pcm_ops .copy function pointer runs on process context.

But the snd_pcm_ops .ack function pointer used to implement
direct read/write transfer using a memory-mapped I/O, runs
with interrupts disabled. Since the Kernel to VideoCore
interface driver has to be able to sleep, PCM write has to be
deferred to in preparation to add mmap support to the driver.

Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
ALSA supports two transfers methods for PCM playback: Read/Write
transfer where samples are writtern to the device using standard
read and write functions and Direct Read/Write transfers where
samples can be written directly to a mapped memory area and the
driver is signaled once this has been done.

The bcm2835 driver only supported Read/Write transfer method so
this patch adds mmap support to the driver.

The ARM CPU is not able to directly address the audio device
hardware buffer so audio samples are sent to the device using
a message passing interface (vchiq).

Since hardware buffers can't be directly mapped to user-space
memory, an intermediate buffer (using the PCM indirect API) is
needed to store the audio samples and push them to the device
through videocore.

Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
@popcornmix

Copy link
Copy Markdown
Collaborator

With original driver:

$ sudo espeak "hello from me"
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_direct.c:877:(snd1_pcm_direct_initialize_slave) slave plugin does not support mmap interleaved or mmap noninterleaved access
ALSA lib pcm_dmix.c:1030:(snd_pcm_dmix_open) unable to initialize slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started

Lots of complaints, but I hear the audio.

With your patch

$ sudo espeak "hello from me"
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started

and no audio output. Any idea why? (and why so many warnings).

@popcornmix

Copy link
Copy Markdown
Collaborator
 mplayer  audio/Franz\ Ferdinand\ -\ Take\ Me\ Out.mp3

Works correctly without patch. I've had silence, squawks and a hang with the patch.

@martinezjavier

Copy link
Copy Markdown
Contributor Author

weird, I tested with aplay, mpg123, jackd and this Gstreamer pipeline:

$ gst-launch-0.10 -v audiotestsrc ! queue ! audio/x-raw-int, width=16, signed=true ! alsasink

all of them worked for me.

I'll test with mplayer and espeak to see why you are having these issues.

Btw, did you remove the /etc/asound.conf that is adding mmap emulation? You shouldn't use that plug-in since the driver now supports it (in fact it won't work when using).

I'm using this asound.conf:

pcm.!default {
    type hw
    card 0
}

ctl.!default {
    type hw
    card 0
}

But is works when trying with no asound at all too.

@martinezjavier

Copy link
Copy Markdown
Contributor Author

When you said with your patch, you mean with the patch-set right? or are you testing each patch separately?

These two patches should be applied together:
ALSA: bcm2835: defer bcm2835 PCM write using the workqueue
ALSA: bcm2835: add memory-mapped I/O mode for audio stream

Since the later won't work without the former. I tried to keep the changes separated to make it easier to review to you, maybe I should squash these two...

@popcornmix

Copy link
Copy Markdown
Collaborator

I was using (I think) the default asound.conf:

pcm.mmap0 {
    type mmap_emul;
    slave {
      pcm "hw:0,0";
    }
}

pcm.!default {
  type plug;
  slave {
    pcm mmap0;
  }
}

It was still largely working. mplayer worked with a wav file, but not mp3. mpg123 worked with mp3 file.
mame worked, flite worked.

I've just tried with your asound and it works in the two cases I'd reported as not working.
Also works without an asound.conf. What do you recommend?

I think we'll get some confusion with people updating kernel on a standard raspbian image (so using mmap_emul), but perhaps that the price of progress. We'll remove mmap_emul in next official image.

(I'm testing the patch as a whole (286.diff).

@asb

asb commented Apr 26, 2013

Copy link
Copy Markdown

We can sort out asound.conf upon upgrade (for those updating via apt-get
anyway).

On 26 April 2013 17:25, popcornmix notifications@github.com wrote:

I was using (I think) the default asound.conf:

pcm.mmap0 {
type mmap_emul;
slave {
pcm "hw:0,0";
}
}

pcm.!default {
type plug;
slave {
pcm mmap0;
}
}

It was still largely working. mplayer worked with a wav file, but not mp3.
mpg123 worked with mp3 file.
mame worked, flite worked.

I've just tried with your asound and it works in the two cases I'd
reported as not working.
Also works without an asound.conf. What do you recommend?

I think we'll get some confusion with people updating kernel on a standard
raspbian image (so using mmap_emul), but perhaps that the price of
progress. We'll remove mmap_emul in next official image.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/286#issuecomment-17084783
.

@martinezjavier

Copy link
Copy Markdown
Contributor Author

@popcornmix

Yes, if you have mmap emulation on your asound.conf and the driver supports mmap then it will work as long as applications are only using ALSA read/write PCM transfer API (snd_pcm_{writei, readi}) but is not going to work if is using mmap direct r/w API calls (snd_pcm_mmap_{begin, commit, readi, writei})

You can see the difference with aplay:

$ aplay test.wav

works but

$ aplay -M test.wav

does not.

I'm not familiarized with asound mmap emulation plug-in but I guess that when an application is trying to use the ALSA mmap API then the PCM is written twice: one by ALSA when snd_pcm_mmap_commit() is called and another time by libasound mmap emulation plug-in that breaks the buffer in many calls to snd_pcm_writei().

Anyway, as you said the solution is to remove mmap_emul.

I don't know what option is better for raspbian, if an asound.conf without mmap emulation should be shipped or no asound.conf at all.

But as @asb said, this can be sorted out for people updating via apt.

@popcornmix

Copy link
Copy Markdown
Collaborator

Yes, new images, and apt-get upgrade users will have no problem.
rpi-update (and kernel compilers) may have a problem. I'll go with "sudo rm /etc/asound.conf" as the advice for now.

Thanks for the PR, it looks good.

popcornmix added a commit that referenced this pull request Apr 26, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
@popcornmix
popcornmix merged commit e29067c into raspberrypi:rpi-3.6.y Apr 26, 2013
popcornmix added a commit that referenced this pull request Apr 26, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
@martinezjavier

Copy link
Copy Markdown
Contributor Author

thanks a lot for feedback and testing!

popcornmix added a commit that referenced this pull request Apr 27, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 1, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 1, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 8, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 8, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 12, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 12, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 20, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request May 26, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
@popcornmix

Copy link
Copy Markdown
Collaborator

There's a report of a regression from this patch in espeak:
#304

@martinezjavier

Copy link
Copy Markdown
Contributor Author

@popcornmix

Thanks for the notice. I'll take a look at this bug.

Regards

popcornmix added a commit that referenced this pull request Jun 8, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Oct 18, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Oct 22, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Oct 22, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 4, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 4, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 4, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 9, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 16, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 17, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 17, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 22, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 22, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 30, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Nov 30, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Dec 5, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Dec 9, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Dec 23, 2013
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Jan 10, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
@popcornmix

Copy link
Copy Markdown
Collaborator

@martinezjavier
Any news on the espeak breakage? Another report of problems here:
#388 (comment)

popcornmix added a commit that referenced this pull request Jan 22, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Jan 28, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Jan 30, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Feb 10, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Feb 14, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Feb 19, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Feb 22, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Feb 23, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Feb 23, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
popcornmix added a commit that referenced this pull request Mar 7, 2014
add mmap support and some cleanups to bcm2835 ALSA driver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants