Skip to contentSkip to conversation

ASoC: DAPM: Allow multiple mixer sources to be routed via the same switch

PATCH Applied Lars-Peter Clausen 8 participants 28 messages Aug 15, 2011 – Aug 19, 2011 6 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

Lars-Peter Clausen

author maintainer
#0 lore raw

PATCH 1/4 ASoC: DAPM: Allow multiple mixer sources to be routed via the same switch

Currently it is only possible to route one source per switch into a mixer. This patch modifies the code, so that it is possible to route multiple sources into a mixer via the same switch. One use-case for this is routing a stereo channel pair into a mono-mixer via the same switch.

---
 sound/soc/soc-dapm.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
sound/soc/soc-dapm.c +5 -1
index c265311..170c4ff 100644
@@ -443,6 +443,11 @@ static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
 			if (path->name != (char *)w->kcontrol_news[i].name)
 				continue;
 
+			if (w->kcontrols[i]) {
+				path->kcontrol = w->kcontrols[i];
+				continue;
+			}
+
 			wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 				    sizeof(struct snd_soc_dapm_widget *),
 			wlist = kzalloc(wlistsize, GFP_KERNEL);
@@ -1556,7 +1561,6 @@ static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
 		/* found, now check type */
 		found = 1;
 		path->connect = connect;
-		break;
 	}
 
 	if (found)
Signature · 1 line
1.7.2.5
author maintainer
#1 lore raw

PATCH 2/4 ASoC: Add ADAU1373 codec support

This patch adds support for the Analog Devices ADAU1373 audio codec.

---
Changes since v1:
* Replace some magic values with defines
* Use TLV volume controls instead of switches for boost controls
* Made bias voltage platform data instead of exposing it as a user adjustable control
* Renamed "Mono Stero" controls to "LR Mux"

---
 include/sound/adau1373.h    |   34 +
 sound/soc/codecs/Kconfig    |    4 +
 sound/soc/codecs/Makefile   |    2 +
 sound/soc/codecs/adau1373.c | 1414 +++++++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/adau1373.h |   29 +
 5 files changed, 1483 insertions(+), 0 deletions(-)
 create mode 100644 include/sound/adau1373.h
 create mode 100644 sound/soc/codecs/adau1373.c
 create mode 100644 sound/soc/codecs/adau1373.h
include/sound/adau1373.h new file +34 -0
new file mode 100644
index 0000000..1b19c76
@@ -0,0 +1,34 @@
+/*
+ * Analog Devices ADAU1373 Audio Codec drive
+ *
+ * Copyright 2011 Analog Devices Inc.
+ * Author: Lars-Peter Clausen <[email protected]>
+ *
+ * Licensed under the GPL-2 or later.
+ */
+
+#ifndef __SOUND_ADAU1373_H__
+#define __SOUND_ADAU1373_H__
+
+enum adau1373_micbias_voltage {
+	ADAU1373_MICBIAS_2_9V = 0,
+	ADAU1373_MICBIAS_2_2V = 1,
+	ADAU1373_MICBIAS_2_6V = 2,
+	ADAU1373_MICBIAS_1_8V = 3,
+};
+
+#define ADAU1373_DRC_SIZE 13
+
+struct adau1373_platform_data {
+	bool input_differential[4];
+	bool lineout_differential;
+	bool lineout_ground_sense;
+
+	unsigned int num_drc;
+	uint8_t drc_setting[3][ADAU1373_DRC_SIZE];
+
+	enum adau1373_micbias_voltage micbias1;
+	enum adau1373_micbias_voltage micbias2;
+};
+
+#endif
sound/soc/codecs/Kconfig +4 -0
index 665d924..71b46c8 100644
@@ -17,6 +17,7 @@ config SND_SOC_ALL_CODECS
 	select SND_SOC_AD193X if SND_SOC_I2C_AND_SPI
 	select SND_SOC_AD1980 if SND_SOC_AC97_BUS
 	select SND_SOC_AD73311
+	select SND_SOC_ADAU1373 if I2C
 	select SND_SOC_ADAV80X
 	select SND_SOC_ADS117X
 	select SND_SOC_AK4104 if SPI_MASTER
@@ -139,6 +140,9 @@ config SND_SOC_ADAU1701
 	select SIGMA
 	tristate
 
+config SND_SOC_ADAU1373
+	tristate
+
 config SND_SOC_ADAV80X
 	tristate
 
sound/soc/codecs/Makefile +2 -0
index 5119a7e..70c1769 100644
@@ -5,6 +5,7 @@ snd-soc-ad193x-objs := ad193x.o
 snd-soc-ad1980-objs := ad1980.o
 snd-soc-ad73311-objs := ad73311.o
 snd-soc-adau1701-objs := adau1701.o
+snd-soc-adau1373-objs := adau1373.o
 snd-soc-adav80x-objs := adav80x.o
 snd-soc-ads117x-objs := ads117x.o
 snd-soc-ak4104-objs := ak4104.o
@@ -100,6 +101,7 @@ obj-$(CONFIG_SND_SOC_AD1836)	+= snd-soc-ad1836.o
 obj-$(CONFIG_SND_SOC_AD193X)	+= snd-soc-ad193x.o
 obj-$(CONFIG_SND_SOC_AD1980)	+= snd-soc-ad1980.o
 obj-$(CONFIG_SND_SOC_AD73311) += snd-soc-ad73311.o
+obj-$(CONFIG_SND_SOC_ADAU1373)	+= snd-soc-adau1373.o
 obj-$(CONFIG_SND_SOC_ADAU1701)  += snd-soc-adau1701.o
 obj-$(CONFIG_SND_SOC_ADAV80X)  += snd-soc-adav80x.o
 obj-$(CONFIG_SND_SOC_ADS117X)	+= snd-soc-ads117x.o
sound/soc/codecs/adau1373.c new file +1414 -0

1,414 lines not on this page · raw message

sound/soc/codecs/adau1373.h new file +29 -0
new file mode 100644
index 0000000..c6ab553
@@ -0,0 +1,29 @@
+#ifndef __ADAU1373_H__
+#define __ADAU1373_H__
+
+enum adau1373_pll_src {
+	ADAU1373_PLL_SRC_MCLK1 = 0,
+	ADAU1373_PLL_SRC_BCLK1 = 1,
+	ADAU1373_PLL_SRC_BCLK2 = 2,
+	ADAU1373_PLL_SRC_BCLK3 = 3,
+	ADAU1373_PLL_SRC_LRCLK1 = 4,
+	ADAU1373_PLL_SRC_LRCLK2 = 5,
+	ADAU1373_PLL_SRC_LRCLK3 = 6,
+	ADAU1373_PLL_SRC_GPIO1 = 7,
+	ADAU1373_PLL_SRC_GPIO2 = 8,
+	ADAU1373_PLL_SRC_GPIO3 = 9,
+	ADAU1373_PLL_SRC_GPIO4 = 10,
+	ADAU1373_PLL_SRC_MCLK2 = 11,
+};
+
+enum adau1373_pll {
+	ADAU1373_PLL1 = 0,
+	ADAU1373_PLL2 = 1,
+};
+
+enum adau1373_clk_src {
+	ADAU1373_CLK_SRC_PLL1 = 0,
+	ADAU1373_CLK_SRC_PLL2 = 1,
+};
+
+#endif
Signature · 1 line
1.7.2.5
#2 lore raw

USB Audio questions

Hi USB Audio experts,
I am trying to prototype an application where the key requirement is playback with minimal power on USB audio accessories while maintaining tight synchronization with video. With the default ALSA USB code, I am getting ~130 interrupts/s due to audio, a fairly high CPU load and a very poor sync, exactly the opposite of my goals...
After a careful analysis of the code I found a couple of issues where the help of experts would be welcome:

- Is there any good reason why the max number of packets per urb defaults to 8? This end-ups being the root cause for the large number of interrupts. My headset supports up to 192 bytes/packet, that means 1536 bytes/urb and 125 interrupts/s. Using more packets/urb results in a linear decrease of the interrupt rate.

- Increasing the number of packets/urbs solves my power issue but not the synchronization issue. If I reduce the number of urbs to reduce the interrupt rate, then the accuracy of the hw_pointer is decreased big time and it becomes difficult to synchronize with video. The .pointer routine only keeps track of retired urbs, it seems there's no way to know how many samples were played at a given time? It also makes pulseaudio behave somewhat strangely since the driver only provides an approximate position. In the CAIQ driver code, the .pointer routine seems to rely on a direct register read, a more efficient way to do things. Isn't there a direct way to query the usb core to know how many bytes have been transmitted and make the hw_ptr/delay information independent of the urb buffer size?

- Is there any reason why the driver relies on vmalloc and memcpy? It seems like using physically-contiguous memory would be more efficient, as done in the CAIQ driver? I hacked something to use prealloc_pages/malloc_pages and things seem to work fine.

- The period information is not updated when the urbs are allocated. In my case the period specified was 25ms, but with all the approximations the actual period (number of urbs*buffer_size_per_urb) was 24ms. In short there's a discrepancy between what is known to the application, what is used by the driver and the actual rate of period_elapsed events.

Thanks,
-Pierre

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Re: USB Audio questions

Hi,

Quoted 8 lines · Pierre-Louis Bossart

I am trying to prototype an application where the key requirement is playback with minimal power on USB audio accessories while maintaining tight synchronization with video. With the default ALSA USB code, I am getting ~130 interrupts/s due to audio, a fairly high CPU load and a very poor sync, exactly the opposite of my goals...
After a careful analysis of the code I found a couple of issues where the help of experts would be welcome:

Quoted 5 lines

- Is there any good reason why the max number of packets per urb defaults to 8? This end-ups being the root cause for the large number of interrupts. My headset supports up to 192 bytes/packet, that means 1536 bytes/urb and 125 interrupts/s. Using more packets/urb results in a linear decrease of the interrupt rate.

You cannot use more than 8 subframes per ISO urb, that's what how USB is designed. What exactly did you do in you test? Did you increase MAX_URBS?

Quoted 11 lines

- Increasing the number of packets/urbs solves my power issue but not the synchronization issue. If I reduce the number of urbs to reduce the interrupt rate, then the accuracy of the hw_pointer is decreased big time and it becomes difficult to synchronize with video. The .pointer routine only keeps track of retired urbs, it seems there's no way to know how many samples were played at a given time? It also makes pulseaudio behave somewhat strangely since the driver only provides an approximate position. In the CAIQ driver code, the .pointer routine seems to rely on a direct register read, a more efficient way to do things. Isn't there a direct way to query the usb core to know how many bytes have been transmitted and make the hw_ptr/delay information independent of the urb buffer size?

Just to be sure - you're talking about the driver in sound/usb/caiaq, right? After a short comparison, I'd say both the caiaq driver and the generic driver do more or less the same in regard to the .pointer value, as for the output stream, they both increase their internal counter when the packet is prepared, shortly before it is submitted to the host controller. Increasing the number of URBs. Admittedly, this approach is questionable, as a larger number of prepared output buffers will actually cause the client believe that more data has already been written already. You could try moving the hwptr_done increase statement into the retire callback (which is the first place we get to know about packets that have been sent to the device)?

Quoted 4 lines

- Is there any reason why the driver relies on vmalloc and memcpy? It seems like using physically-contiguous memory would be more efficient, as done in the CAIQ driver? I hacked something to use prealloc_pages/malloc_pages and things seem to work fine.

Can you show a patch?

Daniel

#5 lore raw

Re: USB Audio questions

Hi Daniel,

Quoted 9 lines
Quoted 5 lines

- Is there any good reason why the max number of packets per urb defaults to 8? This end-ups being the root cause for the large number of interrupts. My headset supports up to 192 bytes/packet, that means 1536 bytes/urb and 125 interrupts/s. Using more packets/urb results in a linear decrease of the interrupt rate.

You cannot use more than 8 subframes per ISO urb, that's what how USB is designed. What exactly did you do in you test? Did you increase MAX_URBS?

Can you point to a section of the spec where this restriction is mentioned? Playback works fine with a larger buffer size and less urbs. If indeed there was such a restriction, then why make does the module take a parameter to change the max packet number of urb in card.c?

module_param(nrpacks, int, 0644); MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");

To make this work, I increased MAX_PACKS, MAX_QUEUE. See attached (ugly!) patch.

Quoted 11 lines

Just to be sure - you're talking about the driver in sound/usb/caiaq, right? After a short comparison, I'd say both the caiaq driver and the generic driver do more or less the same in regard to the .pointer value, as for the output stream, they both increase their internal counter when the packet is prepared, shortly before it is submitted to the host controller. Increasing the number of URBs. Admittedly, this approach is questionable, as a larger number of prepared output buffers will actually cause the client believe that more data has already been written already. You could try moving the hwptr_done increase statement into the retire callback (which is the first place we get to know about packets that have been sent to the device)?

To really have a good video synchronization i'd need the ability to query the number of audio bytes/samples transmitted whenever the video renderer queries the pipeline clock. That makes A/V sync completely independent of the sampling rate and the buffer sizes (just like for HDAudio and most I2S outputs). This information on bytes transmitted is probably available at a lower level (DMA, etc). If the audio time only evolves in steps of 8ms, this is a deal-breaker for me. Having a delay information in terms of ~1ms steps would be enough.

Quoted 6 lines
Quoted 4 lines

- Is there any reason why the driver relies on vmalloc and memcpy? It seems like using physically-contiguous memory would be more efficient, as done in the CAIQ driver? I hacked something to use prealloc_pages/malloc_pages and things seem to work fine.

Can you show a patch?

Included in the same patch below. I only modified the allocation. It's ugly as can be, since I don't really prealloc anything. Looks similar to what is done in your CAIQ code, except that I am a much worse part-time programmer...
-Pierre

sound/core/pcm_memory.c +1 -0
index 150cb7e..09fb994 100644
@@ -107,6 +107,7 @@ int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
 #endif
 	return 0;
 }
+EXPORT_SYMBOL(snd_pcm_lib_preallocate_free);
 
 /**
  * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
sound/usb/card.h +4 -2
index ae4251d..18dfa38 100644
@@ -1,11 +1,13 @@
 #ifndef __USBAUDIO_CARD_H
 #define __USBAUDIO_CARD_H
 
-#define MAX_PACKS	20
+//#define MAX_PACKS	20
+#define MAX_PACKS	40
 #define MAX_PACKS_HS	(MAX_PACKS * 8)	/* in high speed mode */
 #define MAX_URBS	8
 #define SYNC_URBS	4	/* always four urbs for sync */
-#define MAX_QUEUE	24	/* try not to exceed this queue length, in ms */
+//#define MAX_QUEUE	24	/* try not to exceed this queue length, in ms */
+#define MAX_QUEUE	48	/* try not to exceed this queue length, in ms */
 
 struct audioformat {
 	struct list_head list;
sound/usb/pcm.c +65 -5

107 lines not on this page · raw message

sound/usb/urb.c +9 -1
index e184349..a091f57 100644
@@ -251,7 +251,8 @@ int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
 		packs_per_ms = 1;
 
 	if (is_playback) {
-		urb_packs = max(chip->nrpacks, 1);
+		//urb_packs = max(chip->nrpacks, 1);
+		urb_packs = 32; //max(chip->nrpacks, 1);
 		urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
 	} else
 		urb_packs = 1;
@@ -270,6 +271,9 @@ int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
 			minsize -= minsize >> 3;
 		minsize = max(minsize, 1u);
 		total_packs = (period_bytes + minsize - 1) / minsize;
+
+		printk(KERN_ERR "plb: urb_packs %d period_bytes %d minsize %d, total_packs %d \n", urb_packs, period_bytes, minsize, total_packs);
+
 		/* we need at least two URBs for queueing */
 		if (total_packs < 2) {
 			total_packs = 2;
@@ -284,6 +288,7 @@ int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
 		total_packs = MAX_URBS * urb_packs;
 	}
 	subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
+	printk(KERN_ERR "plb: total_packs %d nurbs %d \n", total_packs, subs->nurbs);
 	if (subs->nurbs > MAX_URBS) {
 		/* too much... */
 		subs->nurbs = MAX_URBS;
@@ -305,6 +310,9 @@ int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
 		u->buffer_size = maxsize * u->packets;
 		if (subs->fmt_type == UAC_FORMAT_TYPE_II)
 			u->packets++; /* for transfer delimiter */
+
+		printk(KERN_ERR "plb: urb %d packets %d buffer_size %d \n", i, u->packets, u->buffer_size);
+
 		u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
 		if (!u->urb)
 			goto out_of_memory;

A message that is not in the archive (sent off-list or not yet received): 000001cc5b8a$4a6577c0$df306740$@[email protected]

#7 lore raw

Re: USB Audio questions

Quoted 4 lines · Pierre-Louis Bossart

- Increasing the number of packets/urbs solves my power issue but not the synchronization issue. If I reduce the number of urbs to reduce the interrupt rate, then the accuracy of the hw_pointer is decreased big time and it becomes difficult to synchronize with video.

I think the same thing is a problem for quite a few other devices as well - I wonder if we need some kind of "pointer granularity" variable to be exported through the ALSA API? PulseAudio could use that to determine whether or not to enable timer-based scheduling. And in these cases, maybe a call to hw_pointer could return hw_pointer and time, and then PulseAudio etc could use that for extrapolation (or the extrapolation could be done in alsa-lib).

Signature · 2 lines
David Henningsson, Canonical Ltd.
http://launchpad.net/~diwic
#8 lore raw

Re: USB Audio questions

Quoted 8 lines · David Henningsson
Quoted 4 lines · Pierre-Louis Bossart

- Increasing the number of packets/urbs solves my power issue but not the synchronization issue. If I reduce the number of urbs to reduce the interrupt rate, then the accuracy of the hw_pointer is decreased big time and it becomes difficult to synchronize with video.

I think the same thing is a problem for quite a few other devices as well

Yes; sometimes, the period interrupt is the only information from which the driver can derive the position.

Quoted 2 lines

I wonder if we need some kind of "pointer granularity" variable to be exported through the ALSA API?

This is certainly possible, and by putting it into the hw_params, it would be possible to model any dependencies between granularity and period size. However, this isn't quite at the top of my steadily-growing ToDo list.

Quoted 1 line

a call to hw_pointer could return hw_pointer and time,

After calling snd_pcm_status(), you have snd_pcm_status_get_avail() and snd_pcm_status_get_(h)tstamp().

Quoted 1 line

and then PulseAudio etc could use that for extrapolation

IIRC PA does exactly this if it detects that the pointer values don't increase smoothly.

Regards,
Clemens

#9 lore raw

Re: USB Audio questions

In reply to a message not in the archive
Quoted 2 lines · Pierre-Louis Bossart

- Is there any good reason why the max number of packets per urb defaults to 8?

Not really. It is an attempt to compromise between interrupt frequency and the latency resulting from packet queueing.

Quoted 6 lines

- Increasing the number of packets/urbs solves my power issue but not the synchronization issue. If I reduce the number of urbs to reduce the interrupt rate, then the accuracy of the hw_pointer is decreased big time and it becomes difficult to synchronize with video. The .pointer routine only keeps track of retired urbs, it seems there's no way to know how many samples were played at a given time?

Only the URB completion callback tells us about finished packets.

For playback, we know how many bytes we had submitted, so we could derive the current position from the USB frame counter, but for capture, we don't know the precise byte count until the HCD has looked at the descriptors.

Quoted 3 lines

Isn't there a direct way to query the usb core to know how many bytes have been transmitted and make the hw_ptr/delay information independent of the urb buffer size?

No, this information isn't returned earlier than the completion callback.

It would be nice to be able to tell the HCD to collect information about all currently completed USB frames, regardless of whether the entire URB has finished or whether its interrupt flag is set, but this would require changes in all host controller drivers. (I'm contemplating something like this for FireWire, but there we have only one controller driver.)

Quoted 3 lines

- Is there any reason why the driver relies on vmalloc and memcpy? It seems like using physically-contiguous memory would be more efficient, as done in the CAIQ driver?

Why more efficient? You cannot avoid double-buffering when a USB packet wraps around the end of the ALSA ring buffer.

Quoted 3 lines

- The period information is not updated when the urbs are allocated. In my case the period specified was 25ms, but with all the approximations the actual period (number of urbs*buffer_size_per_urb) was 24ms.

Please note that URBs are usually not completely filled. For playback, the driver makes URBs shorter so that they end as nearly as possible on or after a period boundary; for capture, we cannot predict sample counts, so the driver just uses one URB per ms.

Regards,
Clemens

#10 lore raw

Re: USB Audio questions

Quoted 6 lines · Clemens Ladisch
Quoted 2 lines · Pierre-Louis Bossart

- Is there any good reason why the max number of packets per urb defaults to 8?

Not really.  It is an attempt to compromise between interrupt frequency and the latency resulting from packet queueing.

Ah, I thought there is a limit of frames per urb that is the same than subframes per packet on USB. That's not the case then?

Quoted 7 lines
Quoted 6 lines

- Increasing the number of packets/urbs solves my power issue but not the synchronization issue. If I reduce the number of urbs to reduce the interrupt rate, then the accuracy of the hw_pointer is decreased big time and it becomes difficult to synchronize with video. The .pointer routine only keeps track of retired urbs, it seems there's no way to know how many samples were played at a given time?

Quoted 6 lines

Only the URB completion callback tells us about finished packets.

For playback, we know how many bytes we had submitted, so we could derive the current position from the USB frame counter, but for capture, we don't know the precise byte count until the HCD has looked at the descriptors.

Wouldn't it be possible to not count what we submitted but look at the playback packets that return from the HCD and move the hwptr there? That information is presumably closer to the actual hardware position than the time when we queue.

Daniel

#11 lore raw

Re: USB Audio questions

Quoted 10 lines · Daniel Mack
Quoted 6 lines · Clemens Ladisch
Quoted 2 lines · Pierre-Louis Bossart

- Is there any good reason why the max number of packets per urb defaults to 8?

Not really. It is an attempt to compromise between interrupt frequency and the latency resulting from packet queueing.

Ah, I thought there is a limit of frames per urb that is the same than subframes per packet on USB. That's not the case then?

URBs are a Linux-defined data structure; they can be arbitrarily long (as long as you don't reach the undocumented limit of how far in the future the HCD can schedule packets).

Quoted 4 lines

Wouldn't it be possible to not count what we submitted but look at the playback packets that return from the HCD and move the hwptr there? That information is presumably closer to the actual hardware position than the time when we queue.

In this case, the "hardware position" is the read pointer in ALSA's ring buffer, which must be incremented whenever data is moved from there into the URBs' buffers.

Regards,
Clemens

#12 lore raw

Re: USB Audio questions

In reply to Clemens Ladisch

Hi Clemens,

Quoted 6 lines

For playback, we know how many bytes we had submitted, so we could derive the current position from the USB frame counter, but for capture,
we don't know the precise byte count until the HCD has looked at the descriptors.

Quoted 7 lines
Quoted 2 lines

Isn't there a direct way to query the usb core to know how many bytes have been transmitted and make the hw_ptr/delay information

independent

Quoted 1 line

of the urb buffer size?

No, this information isn't returned earlier than the completion callback.

I am lost here, bear with my ignorance of USB... What is this USB frame counter and what would to enable its use, at least for playback where there is scope for lots of power optimizations.

Quoted 3 lines

Why more efficient? You cannot avoid double-buffering when a USB packet
wraps around the end of the ALSA ring buffer.

What I had in mind is a case where the ring buffer is divided in separate periods, which would each correspond to an integer number of URBs/packets. This way there is _never_ a case of wrap-around and no need for memcpys. It's probably less generic than the current driver but a whole lot simpler/efficient.

Quoted 11 lines
Quoted 1 line

- The period information is not updated when the urbs are allocated.

In my

Quoted 1 line

case the period specified was 25ms, but with all the approximations

the

Quoted 1 line

actual period (number of urbs*buffer_size_per_urb) was 24ms.

Please note that URBs are usually not completely filled. For playback, the driver makes URBs shorter so that they end as nearly as possible on or after a period boundary; for capture, we cannot predict sample counts,
so the driver just uses one URB per ms.

Well I am precisely preaching for filling the URBs as much as possible to make things simpler for cases where latency doesn't matter. Of course for capture/speech cases latency does matter and I understand the need to do things differently there.
-Pierre

A message that is not in the archive (sent off-list or not yet received): 20110816145353.GB5233@xanatos

#14 lore raw

Re: USB Audio questions

Quoted 11 lines

For USB 3.0 host controllers, the HCD doesn't get the information about which frame exactly the transfer went out in. This is because all the scheduling is done in hardware, not software. The xHCI driver can only record the frame that was current when the interrupt for the URB happened.

Pierre, I'm not sure you can get the precise measurements you want out of a USB 3.0 host controller to do the audio/video sync. You're welcome
to suggest something to Steve McGowan to add to the xHCI spec, and you should probably double check with him that what I'm saying is correct.

Thanks Sarah for the heads-up. I was only looking at existing USB Audio solutions for now, looks like there's some work to be done for USB3 as well. We do need precise timing information for all audio drivers. Probably a good topic for LPC discussions...
-Pierre

A message that is not in the archive (sent off-list or not yet received): 000601cc5c2a$87a26350$96e729f0$@[email protected]

#16 lore raw

Re: USB Audio questions

Quoted 3 lines · Pierre-Louis Bossart

What is this USB frame counter and what would to enable its use, at least for playback where there is scope for lots of power optimizations.

USB timing is defined by frames, of which there are about 1000 per second. The audio driver schedules one packet to be sent/received for each frame. Multiple packets are grouped in a URB (USB request buffer); the controller raises an interrupt when the last packet of the URB has been completed.

By reading the current frame number, the driver could deduce how many packets (and therefore, how many samples) have been processed by the controller.

Quoted 5 lines
Quoted 2 lines

Why more efficient? You cannot avoid double-buffering when a USB packet wraps around the end of the ALSA ring buffer.

What I had in mind is a case where the ring buffer is divided in separate periods, which would each correspond to an integer number of URBs/packets.

This works only when the driver has control over how many samples actually are transmitted in each USB frame. The playback streams of many USB audio devices work this way, but quite a few others, and capture streams, don't.

Quoted 2 lines

It's probably less generic than the current driver but a whole lot simpler/efficient.

The code wouldn't be simpler because we'd still need the other algorithm. This would be just an optimization.

Regards,
Clemens

#17 lore raw

Re: USB Audio questions

Quoted 12 lines
Quoted 4 lines

What is this USB frame counter and what would to enable its use, at least for playback where there is scope for lots of power optimizations.

USB timing is defined by frames, of which there are about 1000 per second. The audio driver schedules one packet to be sent/received for each frame. Multiple packets are grouped in a URB (USB request buffer);
the controller raises an interrupt when the last packet of the URB has been completed.

Quoted 3 lines

By reading the current frame number, the driver could deduce how many packets (and therefore, how many samples) have been processed by the controller.

Thanks Clemens, this is valuable information.
If this frame number was used to update the delay field with more accuracy, I'd be able to use a larger number of packets while allowing for precise a/v sync. Removing the memcpys, etc, are probably a second-degree optimization. I agree it'd make the code more complex.
Any idea on how this frame number can be read, I can try to prototype this if this is supported by the USB core?
-Pierre

A message that is not in the archive (sent off-list or not yet received): 000001cc5c38$b07abee0$11703ca0$@[email protected]

#19 lore raw

Re: USB Audio questions

Quoted 1 line · Pierre-Louis Bossart

Any idea on how this frame number can be read,

With usb_get_current_frame_number().

This is not a technical problem; the problem is that nobody has found the time to implement this in the audio driver.

Regards,
Clemens

#20 lore raw

Re: USB Audio questions

Quoted 7 lines
Quoted 2 lines

Any idea on how this frame number can be read,

With usb_get_current_frame_number().

This is not a technical problem; the problem is that nobody has found the time to implement this in the audio driver.

Ok, will try to look at this. Thanks for the pointer.

#21 lore raw

Re: USB Audio questions

In reply to Clemens Ladisch
Quoted 4 lines
Quoted 2 lines

Any idea on how this frame number can be read,

With usb_get_current_frame_number().

I hacked something to refine the delay information based on the USB frame number. see attached patch.
Two points can be improved:
- I am not sure how to convert from USB frames to ALSA frames. I used a fixed value based what my headset supports
- Every second or so the estimate seems to be off by 96 samples, see below. Not sure why this happens, how accurate the counter is and if there a delay in handling retired urbs? Looks good enough though. Thanks,
-Pierre

[10880.896486] plb-urb -- actual delay 96 estimated delay 0 
[10881.917729] plb-urb -- actual delay 48 estimated delay 0 
[10882.937948] plb-urb -- actual delay 96 estimated delay 0 
[10883.966190] plb-urb -- actual delay 96 estimated delay 0 
[10884.987425] plb-urb -- actual delay 96 estimated delay 0 
[10886.008654] plb-urb -- actual delay 96 estimated delay 0 
[10887.029931] plb-urb -- actual delay 96 estimated delay 0 
[10888.050153] plb-urb -- actual delay 96 estimated delay 0 
[10889.071375] plb-urb -- actual delay 96 estimated delay 0
sound/usb/card.h +2 -0
index ae4251d..a39edcc 100644
@@ -94,6 +94,8 @@ struct snd_usb_substream {
 	spinlock_t lock;
 
 	struct snd_urb_ops ops;		/* callbacks (must be filled at init) */
+	int last_frame_number;          /* stored frame number */
+	int last_delay;                 /* stored delay */
 };
 
 struct snd_usb_stream {
sound/usb/pcm.c +22 -0
index b8dcbf4..ca17711 100644
@@ -34,6 +34,27 @@
 #include "clock.h"
 #include "power.h"
 
+/* return the estimated delay based on USB frame counters */
+snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs)
+{
+	int current_frame_number;
+	int frame_diff;
+	int est_delay;
+
+	current_frame_number= usb_get_current_frame_number(subs->dev);
+	frame_diff = current_frame_number-subs->last_frame_number;
+	if (frame_diff < 0)
+		frame_diff += 2048; /* handle 11-bit wrap-around */
+
+	/* FIXME: need to make this formula more general */
+	est_delay =  subs->last_delay-frame_diff*48;
+	if (est_delay < 0) {
+		//printk(KERN_ERR "\t est_delay %d last_delay %d frame_diff %d curr_frame %d last_frame %d \n", est_delay, subs->last_delay, frame_diff, current_frame_number, subs->last_frame_number);
+		est_delay = 0;
+	}
+	return est_delay;
+}
+
 /*
  * return the current pcm pointer.  just based on the hwptr_done value.
  */
@@ -45,6 +66,7 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream
 	subs = (struct snd_usb_substream *)substream->runtime->private_data;
 	spin_lock(&subs->lock);
 	hwptr_done = subs->hwptr_done;
+	substream->runtime->delay = snd_usb_pcm_delay(subs);
 	spin_unlock(&subs->lock);
 	return hwptr_done / (substream->runtime->frame_bits >> 3);
 }
sound/usb/pcm.h +2 -0
index ed3e283..11b7bd9 100644
@@ -1,6 +1,8 @@
 #ifndef __USBAUDIO_PCM_H
 #define __USBAUDIO_PCM_H
 
+snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs);
+
 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream);
 
 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
sound/usb/urb.c +21 -3
index e184349..a0897a5 100644
@@ -718,7 +718,16 @@ static int prepare_playback_urb(struct snd_usb_substream *subs,
 	subs->hwptr_done += bytes;
 	if (subs->hwptr_done >= runtime->buffer_size * stride)
 		subs->hwptr_done -= runtime->buffer_size * stride;
+
+	/* update delay with exact number of samples queued */
+	runtime->delay = subs->last_delay;
 	runtime->delay += frames;
+	subs->last_delay = runtime->delay;
+
+	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
+
+	//printk(KERN_ERR "\t\tplb-urb: increased delay %d current frame %d \n", runtime->delay, subs->last_frame_number);
+
 	spin_unlock_irqrestore(&subs->lock, flags);
 	urb->transfer_buffer_length = bytes;
 	if (period_elapsed)
@@ -737,12 +746,21 @@ static int retire_playback_urb(struct snd_usb_substream *subs,
 	unsigned long flags;
 	int stride = runtime->frame_bits >> 3;
 	int processed = urb->transfer_buffer_length / stride;
+	int est_delay;
 
 	spin_lock_irqsave(&subs->lock, flags);
-	if (processed > runtime->delay)
-		runtime->delay = 0;
+
+	est_delay = snd_usb_pcm_delay(subs);
+	/* update delay with exact number of samples played*/
+	if (processed > subs->last_delay)
+		subs->last_delay = 0;
 	else
-		runtime->delay -= processed;
+		subs->last_delay -= processed;
+	runtime->delay = subs->last_delay;
+
+	if (runtime->delay != est_delay)
+		printk(KERN_ERR "plb-urb -- actual delay %d estimated delay %d \n", runtime->delay, est_delay);
+
 	spin_unlock_irqrestore(&subs->lock, flags);
 	return 0;
 }
#22 lore raw

Re: USB Audio questions

In reply to a message not in the archive

Hi Pierre,

I also spent some time learning about USB and audio while developing the sound/usb/6fire driver. I made the following discoveries:

Quoted 5 lines

What I had in mind is a case where the ring buffer is divided in separate periods, which would each correspond to an integer number of URBs/packets. This way there is _never_ a case of wrap-around and no need for memcpys. It's probably less generic than the current driver but a whole lot simpler/efficient.

I was also not very happy to implement the double buffering with memcpy's. But from the experience with the 6fire device I can say this:

- The device/driver sends per urb a constant number of packets (as you already also discovered). The number of samples per packet differ however (+- 1 sample per packet). I accuse the asynchronousity between the urb interval and the sound card's internal clock for this fact. The device expects the driver to send back an urb with exactly the same structure. The device produced crackling noise if the samples per packet were not equal. I cannot say to what extend this is true (i.e. if you could wait some packets until catching up the difference without producing crackles) but due to this fact it's at least not a simple task to send urbs exactly corresponding to the period limit.

- Another thing came into my mind while reading your post, but I'm not sure if that would apply in praxis: If sending an urb is scheduled with the alsa buffer, it might be possible that the buffer is overwritten before the urb is sent, leading to complete audio-rubbish.

- And: at least the 6fire requires a header for each packet, which couldn't be placed directly in the alsa buffer. But perhaps this doesn't apply to most cards.

Greets,
Torsten

#23 lore raw

Re: USB Audio questions

Dne 16.8.2011 22:41, Torsten Schenk napsal(a):

Quoted 11 lines

Hi Pierre,

- The device/driver sends per urb a constant number of packets (as you already also discovered). The number of samples per packet differ however (+- 1 sample per packet). I accuse the asynchronousity between the urb interval and the sound card's internal clock for this fact. The device expects the driver to send back an urb with exactly the same structure. The device produced crackling noise if the samples per packet were not equal. I cannot say to what extend this is true (i.e. if you could wait some packets until catching up the difference without producing crackles)

Could we attribute this to the adaptive mode and PLL striving to produce steady stream of well-timed samples to the DAC? IMHO the lower variation in samples per packet, the lower jitter of the PLL-recovered clock.

Pavel.

#24 lore raw

Re: USB Audio questions

Quoted 11 lines · Pavel Hofman

Dne 16.8.2011 22:41, Torsten Schenk napsal(a):

Quoted 7 lines

- The device/driver sends per urb a constant number of packets (as you already also discovered). The number of samples per packet differ however (+- 1 sample per packet). I accuse the asynchronousity between the urb interval and the sound card's internal clock for this fact. The device expects the driver to send back an urb with exactly the same structure. The device produced crackling noise if the samples per packet were not equal.

Could we attribute this to the adaptive mode and PLL striving to produce steady stream of well-timed samples to the DAC?

No; those devices are not adaptive, they have their own clock and excpect the computer to derive the actual rate of the playback stream from the actual rate of the capture stream.

Quoted 2 lines

IMHO the lower variation in samples per packet, the lower jitter of the PLL-recovered clock.

For adaptive devices, the driver has complete control over sent packets, and thus they already have as little variation as possible.

Regards,
Clemens

A message that is not in the archive (sent off-list or not yet received): 000001cc5c5a$e7d5c3e0$b7814ba0$@[email protected]

#26 lore raw

Re: USB Audio questions

Quoted 13 lines

Thanks for sharing your experience.

Quoted 10 lines

- The device/driver sends per urb a constant number of packets (as you already also discovered). The number of samples per packet differ however (+- 1 sample per packet). I accuse the asynchronousity between the urb interval and the sound card's internal clock for this fact. The device expects the driver to send back an urb with exactly the same structure. The device produced crackling noise if the samples per packet were not equal. I cannot say to what extend this is true (i.e. if you could wait some packets until catching up the difference without producing crackles) but due to this fact it's at least not a simple task to send urbs exactly corresponding to the period limit.

Quoted 3 lines

I am still unclear on what makes the number of samples per packet vary, I didn't see this in the code. I understand that it could vary for 44.1 but for 48kHz (my case) I'd think it should be constant.

The variation is not directly in the code. I remember that the caiaq driver enlightened me since this behaviour is commented there. The variation comes from the device and these steps lead to this variation:
1. the driver sends n urbs containing m packets with size 0 (i.e. no audio) to the device
2. the device sends back each of the n urbs one-by-one, filled with the captured audio; the urbs will contain m packets, each packet having an individual number of samples s_p ("sample" being defined here as: 1 sample contains one value for each channel)
3. for each urb returned from the device, the driver sends another urb containing again m packets, but this time with s_p samples per packet (corresponding to the packet-dependent s_p received from the device)

I indeed found out that when using 48kHz, I first thought that the number of samples per packet were identical every time. When using this heuristic, crackling noise occured in the sound output and I looked at the number of samples per packet. They varied not very often but sometimes - around once per 2 seconds.

My explanation for this problem is, that the sound card has its own clock, that will not oscillate at exactly an integer multiple as the computer's/usb's internal clock. Even temperature will change the frequency a little bit, meaning that 48kHz is not always exactly 48kHz. I would have to start speculating about which clock drives the usb urb scheduling, so this question should be left for the more experienced USB programmers.

Quoted 8 lines
Quoted 5 lines

- Another thing came into my mind while reading your post, but I'm not sure if that would apply in praxis: If sending an urb is scheduled with the alsa buffer, it might be possible that the buffer is overwritten before the urb is sent, leading to complete audio-rubbish.

Indeed there could be some issues with pulseaudio rewinds, but the fact is that rewinds are broken anyway when the data is copied...

I gave this another thought and came to the conclusion: if you'd call snd_pcm_period_elapsed in the output urb retire callback, the theoretical problem wouldn't occur.

Torsten

author maintainer
#28 lore raw

PATCH 3/4 ASoC: Blackfin: ADAU1373 eval board support

In reply to Lars-Peter Clausen

Add a machine driver to support the EVAL-ADAU1373 board connected to a Analog Devices BF5XX evaluation board.

---
Changes since v1:
* Make the drivers Kconfig symbol depend on I2C instead of selecting it

---
 sound/soc/blackfin/Kconfig              |   13 ++
 sound/soc/blackfin/Makefile             |    2 +
 sound/soc/blackfin/bfin-eval-adau1373.c |  202 +++++++++++++++++++++++++++++++
 3 files changed, 217 insertions(+), 0 deletions(-)
 create mode 100644 sound/soc/blackfin/bfin-eval-adau1373.c
sound/soc/blackfin/Kconfig +13 -0
index fe9d548..9f6bc55 100644
@@ -27,6 +27,19 @@ config SND_SOC_BFIN_EVAL_ADAU1701
 	  board connected to one of the Blackfin evaluation boards like the
 	  BF5XX-STAMP or BF5XX-EZKIT.
 
+config SND_SOC_BFIN_EVAL_ADAU1373
+	tristate "Support for the EVAL-ADAU1373 board on Blackfin eval boards"
+	depends on SND_BF5XX_I2S && I2C
+	select SND_BF5XX_SOC_I2S
+	select SND_SOC_ADAU1373
+	help
+	  Say Y if you want to add support for the Analog Devices EVAL-ADAU1373
+	  board connected to one of the Blackfin evaluation boards like the
+	  BF5XX-STAMP or BF5XX-EZKIT.
+
+	  Note: This driver assumes that first ADAU1373 DAI is connected to the
+	  first SPORT port on the BF5XX board.
+
 config SND_SOC_BFIN_EVAL_ADAV80X
 	tristate "Support for the EVAL-ADAV80X boards on Blackfin eval boards"
 	depends on SND_BF5XX_I2S && (SPI_MASTER || I2C)
sound/soc/blackfin/Makefile +2 -0
index 6018bf5..1bf86cc 100644
@@ -21,6 +21,7 @@ snd-ad1980-objs := bf5xx-ad1980.o
 snd-ssm2602-objs := bf5xx-ssm2602.o
 snd-ad73311-objs := bf5xx-ad73311.o
 snd-ad193x-objs := bf5xx-ad193x.o
+snd-soc-bfin-eval-adau1373-objs := bfin-eval-adau1373.o
 snd-soc-bfin-eval-adau1701-objs := bfin-eval-adau1701.o
 snd-soc-bfin-eval-adav80x-objs := bfin-eval-adav80x.o
 
@@ -29,5 +30,6 @@ obj-$(CONFIG_SND_BF5XX_SOC_AD1980) += snd-ad1980.o
 obj-$(CONFIG_SND_BF5XX_SOC_SSM2602) += snd-ssm2602.o
 obj-$(CONFIG_SND_BF5XX_SOC_AD73311) += snd-ad73311.o
 obj-$(CONFIG_SND_BF5XX_SOC_AD193X) += snd-ad193x.o
+obj-$(CONFIG_SND_SOC_BFIN_EVAL_ADAU1373) += snd-soc-bfin-eval-adau1373.o
 obj-$(CONFIG_SND_SOC_BFIN_EVAL_ADAU1701) += snd-soc-bfin-eval-adau1701.o
 obj-$(CONFIG_SND_SOC_BFIN_EVAL_ADAV80X) += snd-soc-bfin-eval-adav80x.o
sound/soc/blackfin/bfin-eval-adau1373.c new file +202 -0

202 lines not on this page · raw message

Signature · 1 line
1.7.2.5
Applied
#29 lore raw

Re: [PATCH 3/4] ASoC: Blackfin: ADAU1373 eval board support

Quoted 4 lines · Lars-Peter Clausen

Add a machine driver to support the EVAL-ADAU1373 board connected to a Analog Devices BF5XX evaluation board.

Applied, thanks.

author maintainer
#30 lore raw

PATCH 4/4 Blackfin: bf537: Stamp: Register ASoC EVAL-ADAU1373 board driver

In reply to Lars-Peter Clausen
---
 arch/blackfin/mach-bf537/boards/stamp.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
arch/blackfin/mach-bf537/boards/stamp.c +12 -0
index 4037043..6662c37 100644
@@ -2674,6 +2674,13 @@ static struct platform_device iio_gpio_trigger = {
 };
 #endif
 
+#if defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1373) || \
+	defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1373_MODULE)
+static struct platform_device bf5xx_adau1373_device = {
+	.name = "bfin-eval-adau1373",
+};
+#endif
+
 #if defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1701) || \
 	defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1701_MODULE)
 static struct platform_device bf5xx_adau1701_device = {
@@ -2842,6 +2849,11 @@ static struct platform_device *stamp_devices[] __initdata = {
 	&iio_gpio_trigger,
 #endif
 
+#if defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1373) || \
+	defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1373_MODULE)
+	&bf5xx_adau1373_device,
+#endif
+
 #if defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1701) || \
 	defined(CONFIG_SND_SOC_BFIN_EVAL_ADAU1701_MODULE)
 	&bf5xx_adau1701_device,
Signature · 1 line
1.7.2.5
#31 lore raw

Re: [PATCH 4/4] Blackfin: bf537: Stamp: Register ASoC EVAL-ADAU1373 board driver

Quoted 2 lines · Lars-Peter Clausen

I'm OK with this but may as well go via the blackfin tree as there's no code dependency on any of the ASoC stuff and it reduces the risk of any collisions with other blackfin work. If you guys would prefer that it goes via ASoC then I can apply it there though.

author maintainer
#32 lore raw

Re: [PATCH 4/4] Blackfin: bf537: Stamp: Register ASoC EVAL-ADAU1373 board driver

Quoted 8 lines · Mark Brown
Quoted 2 lines · Lars-Peter Clausen

I'm OK with this but may as well go via the blackfin tree as there's no code dependency on any of the ASoC stuff and it reduces the risk of any collisions with other blackfin work. If you guys would prefer that it goes via ASoC then I can apply it there though.

We'll try to let this go through the blackfin tree.

Thanks
- Lars

Applied
#33 lore raw
In reply to Lars-Peter Clausen
Quoted 4 lines · Lars-Peter Clausen

Currently it is only possible to route one source per switch into a mixer. This patch modifies the code, so that it is possible to route multiple sources into a mixer via the same switch. One use-case for this is routing a stereo channel pair into a mono-mixer via the same switch.

Applied, thanks.