Skip to contentSkip to conversation

USB Audio Gadget refactoring

PATCH Superseded Ruslan Bilovol 4 participants 13 messages Aug 14, 2016 – Aug 16, 2016

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover USB Audio Gadget refactoring

I came to this patch series when wanted to do two things:
- use UAC1 as virtual ALSA sound card on gadget side, just like UAC2 is used so it's possible to do rate resampling
- have both playback/capture support in UAC1

Since I wanted to have same behavior for both UAC1/UAC2, obviously I've got an utility part (u_audio.c) for virtual ALSA sound card handling like we have for ethernet(u_ether) or serial(u_serial) functions. Function-specific parts (f_uac1/f_uac2) became almost as storage for class-specific USB descriptors, some boilerplate for configfs, binding and few USB config request handling.

Originally in RFC [1] I've posted before, there was major change to f_uac1 after that it couldn't do direct play to existing ALSA sound card anymore, representing audio on gadget side as virtual ALSA sound card where audio streams are simply sinked to and sourced from it, so it may break current usecase for some people (and that's why it was RFC).

During RFC discussion, it was agreed to not touch existing f_uac1 implementation and create new one instead. This patchset (v2) introduced new function named f_uac1_newapi and doesn't touch current f_uac1 implementation, so people still can use old behavior

Now, it's possible to use existing user-space applications for audio routing between Audio Gadget and real sound card. I personally use alsaloop tool from alsautils and have ability to create PCM loopback between two different ALSA cards using rate resampling, which was not possible with previous "direct play to ALSA card" approach in f_uac1.

While here, also dropped redundant platform driver/device creation in f_uac2 driver (as well as didn't add "never implemented" volume/mute functionality in f_uac1 to f_uac1_newapi) that made this work even easier to do.

This series is tested with both legacy g_audio.ko and modern configfs approaches under Ubuntu 14.04 (UAC1 and UAC2) and under Windows7 x64 (UAC1 only) having perfect results in all cases.

Comments, testing are welcome.

v2 changes:
- do not touch f_uac1, instead created f_uac1_newapi
- added documentation for f_uac1_newapi
- rebased on top of v4.8-rc1

[1] https://lkml.org/lkml/2016/5/23/649

Ruslan Bilovol (3):
usb: gadget: f_uac2: remove platform driver/device creation
usb: gadget: f_uac2: split out audio core
usb: gadget: add f_uac1 variant based on new u_audio api

 .../ABI/testing/configfs-usb-gadget-uac1_newapi    |  12 +
 Documentation/usb/gadget-testing.txt               |  41 ++
 drivers/usb/gadget/Kconfig                         |  25 +
 drivers/usb/gadget/function/Makefile               |   3 +
 drivers/usb/gadget/function/f_uac1_newapi.c        | 795 +++++++++++++++++++++
 drivers/usb/gadget/function/f_uac2.c               | 778 +++-----------------
 drivers/usb/gadget/function/u_audio.c              | 632 ++++++++++++++++
 drivers/usb/gadget/function/u_audio.h              |  93 +++
 drivers/usb/gadget/function/u_uac1_newapi.h        |  39 +
 drivers/usb/gadget/legacy/Kconfig                  |  14 +-
 drivers/usb/gadget/legacy/audio.c                  |  56 +-
 11 files changed, 1803 insertions(+), 685 deletions(-)
 create mode 100644 Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi
 create mode 100644 drivers/usb/gadget/function/f_uac1_newapi.c
 create mode 100644 drivers/usb/gadget/function/u_audio.c
 create mode 100644 drivers/usb/gadget/function/u_audio.h
 create mode 100644 drivers/usb/gadget/function/u_uac1_newapi.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/3 usb: gadget: f_uac2: remove platform driver/device creation

Simplify f_uac2 by removing platform driver/device creation; use composite's usb_gadget device as parent for sound card and for debug prints. This removes extra layer of code without any functional change.

---
 drivers/usb/gadget/function/f_uac2.c | 107 +++++++++--------------------------
 1 file changed, 28 insertions(+), 79 deletions(-)
drivers/usb/gadget/function/f_uac2.c +28 -79
index cd214ec8..e14628c 100644
@@ -13,7 +13,6 @@
 
 #include <linux/usb/audio.h>
 #include <linux/usb/audio-v2.h>
-#include <linux/platform_device.h>
 #include <linux/module.h>
 
 #include <sound/core.h>
@@ -54,8 +53,6 @@
 #define UNFLW_CTRL	8
 #define OVFLW_CTRL	10
 
-static const char *uac2_name = "snd_uac2";
-
 struct uac2_req {
 	struct uac2_rtd_params *pp; /* parent param */
 	struct usb_request *req;
@@ -84,9 +81,6 @@ struct uac2_rtd_params {
 };
 
 struct snd_uac2_chip {
-	struct platform_device pdev;
-	struct platform_driver pdrv;
-
 	struct uac2_rtd_params p_prm;
 	struct uac2_rtd_params c_prm;
 
@@ -125,6 +119,7 @@ struct audio_dev {
 
 	struct usb_ep *in_ep, *out_ep;
 	struct usb_function func;
+	struct usb_gadget *gadget;
 
 	/* The ALSA Sound Card it represents on the USB-Client side */
 	struct snd_uac2_chip uac2;
@@ -143,12 +138,6 @@ struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
 }
 
 static inline
-struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
-{
-	return container_of(p, struct snd_uac2_chip, pdev);
-}
-
-static inline
 struct f_uac2_opts *agdev_to_uac2_opts(struct audio_dev *agdev)
 {
 	return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
@@ -257,7 +246,7 @@ agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
 
 exit:
 	if (usb_ep_queue(ep, req, GFP_ATOMIC))
-		dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
+		dev_err(uac2->card->dev, "%d Error!\n", __LINE__);
 
 	if (update_alsa)
 		snd_pcm_period_elapsed(substream);
@@ -441,23 +430,22 @@ static struct snd_pcm_ops uac2_pcm_ops = {
 	.prepare = uac2_pcm_null,
 };
 
-static int snd_uac2_probe(struct platform_device *pdev)
+static int snd_uac2_probe(struct audio_dev *audio_dev)
 {
-	struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
+	struct snd_uac2_chip *uac2 = &audio_dev->uac2;
 	struct snd_card *card;
 	struct snd_pcm *pcm;
-	struct audio_dev *audio_dev;
 	struct f_uac2_opts *opts;
 	int err;
 	int p_chmask, c_chmask;
 
-	audio_dev = uac2_to_agdev(uac2);
 	opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
 	p_chmask = opts->p_chmask;
 	c_chmask = opts->c_chmask;
 
 	/* Choose any slot, with no id */
-	err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
+	err = snd_card_new(&audio_dev->gadget->dev,
+			-1, NULL, THIS_MODULE, 0, &card);
 	if (err < 0)
 		return err;
 
@@ -482,16 +470,15 @@ static int snd_uac2_probe(struct platform_device *pdev)
 
 	strcpy(card->driver, "UAC2_Gadget");
 	strcpy(card->shortname, "UAC2_Gadget");
-	sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
+	sprintf(card->longname, "UAC2_Gadget %i", card->dev->id);
 
 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
 		snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
 
 	err = snd_card_register(card);
-	if (!err) {
-		platform_set_drvdata(pdev, card);
+
+	if (!err)
 		return 0;
-	}
 
 snd_fail:
 	snd_card_free(card);
@@ -502,9 +489,9 @@ snd_fail:
 	return err;
 }
 
-static int snd_uac2_remove(struct platform_device *pdev)
+static int snd_uac2_remove(struct audio_dev *audio_dev)
 {
-	struct snd_card *card = platform_get_drvdata(pdev);
+	struct snd_card *card = audio_dev->uac2.card;
 
 	if (card)
 		return snd_card_free(card);
@@ -512,45 +499,6 @@ static int snd_uac2_remove(struct platform_device *pdev)
 	return 0;
 }
 
-static void snd_uac2_release(struct device *dev)
-{
-	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
-}
-
-static int alsa_uac2_init(struct audio_dev *agdev)
-{
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
-	int err;
-
-	uac2->pdrv.probe = snd_uac2_probe;
-	uac2->pdrv.remove = snd_uac2_remove;
-	uac2->pdrv.driver.name = uac2_name;
-
-	uac2->pdev.id = 0;
-	uac2->pdev.name = uac2_name;
-	uac2->pdev.dev.release = snd_uac2_release;
-
-	/* Register snd_uac2 driver */
-	err = platform_driver_register(&uac2->pdrv);
-	if (err)
-		return err;
-
-	/* Register snd_uac2 device */
-	err = platform_device_register(&uac2->pdev);
-	if (err)
-		platform_driver_unregister(&uac2->pdrv);
-
-	return err;
-}
-
-static void alsa_uac2_exit(struct audio_dev *agdev)
-{
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
-
-	platform_driver_unregister(&uac2->pdrv);
-	platform_device_unregister(&uac2->pdev);
-}
-
 
 /* --------- USB Function Interface ------------- */
 
@@ -959,7 +907,7 @@ free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
 	}
 
 	if (usb_ep_disable(ep))
-		dev_err(&uac2->pdev.dev,
+		dev_err(uac2->card->dev,
 			"%s:%d Error!\n", __func__, __LINE__);
 }
 
@@ -993,7 +941,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	struct snd_uac2_chip *uac2 = &agdev->uac2;
 	struct usb_composite_dev *cdev = cfg->cdev;
 	struct usb_gadget *gadget = cdev->gadget;
-	struct device *dev = &uac2->pdev.dev;
+	struct device *dev = &gadget->dev;
 	struct uac2_rtd_params *prm;
 	struct f_uac2_opts *uac2_opts;
 	struct usb_string *us;
@@ -1064,6 +1012,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	agdev->as_in_intf = ret;
 	agdev->as_in_alt = 0;
 
+	agdev->gadget = gadget;
+
 	agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
 	if (!agdev->out_ep) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
@@ -1109,7 +1059,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		goto err_free_descs;
 	}
 
-	ret = alsa_uac2_init(agdev);
+	ret = snd_uac2_probe(agdev);
 	if (ret)
 		goto err_free_descs;
 	return 0;
@@ -1119,6 +1069,7 @@ err_free_descs:
 err:
 	kfree(agdev->uac2.p_prm.rbuf);
 	kfree(agdev->uac2.c_prm.rbuf);
+	agdev->gadget = NULL;
 	return -EINVAL;
 }
 
@@ -1129,7 +1080,7 @@ afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
 	struct audio_dev *agdev = func_to_agdev(fn);
 	struct snd_uac2_chip *uac2 = &agdev->uac2;
 	struct usb_gadget *gadget = cdev->gadget;
-	struct device *dev = &uac2->pdev.dev;
+	struct device *dev = &gadget->dev;
 	struct usb_request *req;
 	struct usb_ep *ep;
 	struct uac2_rtd_params *prm;
@@ -1230,7 +1181,6 @@ static int
 afunc_get_alt(struct usb_function *fn, unsigned intf)
 {
 	struct audio_dev *agdev = func_to_agdev(fn);
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
 
 	if (intf == agdev->ac_intf)
 		return agdev->ac_alt;
@@ -1239,7 +1189,7 @@ afunc_get_alt(struct usb_function *fn, unsigned intf)
 	else if (intf == agdev->as_in_intf)
 		return agdev->as_in_alt;
 	else
-		dev_err(&uac2->pdev.dev,
+		dev_err(&agdev->gadget->dev,
 			"%s:%d Invalid Interface %d!\n",
 			__func__, __LINE__, intf);
 
@@ -1264,7 +1214,6 @@ in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 {
 	struct usb_request *req = fn->config->cdev->req;
 	struct audio_dev *agdev = func_to_agdev(fn);
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
 	struct f_uac2_opts *opts;
 	u16 w_length = le16_to_cpu(cr->wLength);
 	u16 w_index = le16_to_cpu(cr->wIndex);
@@ -1293,7 +1242,7 @@ in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 		*(u8 *)req->buf = 1;
 		value = min_t(unsigned, w_length, 1);
 	} else {
-		dev_err(&uac2->pdev.dev,
+		dev_err(&agdev->gadget->dev,
 			"%s:%d control_selector=%d TODO!\n",
 			__func__, __LINE__, control_selector);
 	}
@@ -1306,7 +1255,6 @@ in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 {
 	struct usb_request *req = fn->config->cdev->req;
 	struct audio_dev *agdev = func_to_agdev(fn);
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
 	struct f_uac2_opts *opts;
 	u16 w_length = le16_to_cpu(cr->wLength);
 	u16 w_index = le16_to_cpu(cr->wIndex);
@@ -1336,7 +1284,7 @@ in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 		value = min_t(unsigned, w_length, sizeof r);
 		memcpy(req->buf, &r, value);
 	} else {
-		dev_err(&uac2->pdev.dev,
+		dev_err(&agdev->gadget->dev,
 			"%s:%d control_selector=%d TODO!\n",
 			__func__, __LINE__, control_selector);
 	}
@@ -1372,12 +1320,11 @@ static int
 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 {
 	struct audio_dev *agdev = func_to_agdev(fn);
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
 	u16 w_index = le16_to_cpu(cr->wIndex);
 	u8 intf = w_index & 0xff;
 
 	if (intf != agdev->ac_intf) {
-		dev_err(&uac2->pdev.dev,
+		dev_err(&agdev->gadget->dev,
 			"%s:%d Error!\n", __func__, __LINE__);
 		return -EOPNOTSUPP;
 	}
@@ -1395,7 +1342,6 @@ afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 {
 	struct usb_composite_dev *cdev = fn->config->cdev;
 	struct audio_dev *agdev = func_to_agdev(fn);
-	struct snd_uac2_chip *uac2 = &agdev->uac2;
 	struct usb_request *req = cdev->req;
 	u16 w_length = le16_to_cpu(cr->wLength);
 	int value = -EOPNOTSUPP;
@@ -1407,14 +1353,15 @@ afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 	if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
 		value = setup_rq_inf(fn, cr);
 	else
-		dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
+		dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
+				__func__, __LINE__);
 
 	if (value >= 0) {
 		req->length = value;
 		req->zero = value < w_length;
 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
 		if (value < 0) {
-			dev_err(&uac2->pdev.dev,
+			dev_err(&agdev->gadget->dev,
 				"%s:%d Error!\n", __func__, __LINE__);
 			req->status = 0;
 		}
@@ -1553,7 +1500,7 @@ static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
 	struct audio_dev *agdev = func_to_agdev(f);
 	struct uac2_rtd_params *prm;
 
-	alsa_uac2_exit(agdev);
+	snd_uac2_remove(agdev);
 
 	prm = &agdev->uac2.p_prm;
 	kfree(prm->rbuf);
@@ -1561,6 +1508,8 @@ static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
 	prm = &agdev->uac2.c_prm;
 	kfree(prm->rbuf);
 	usb_free_all_descriptors(f);
+
+	agdev->gadget = NULL;
 }
 
 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
Signature · 1 line
1.9.1
#2 lore raw

PATCH v2 2/3 usb: gadget: f_uac2: split out audio core

In reply to Ruslan Bilovol

Abstract the peripheral side ALSA sound card code from the f_uac2 function into a component that can be called by various functions, so the various flavors can be split apart and selectively reused.

Visible changes:
- add uac_params structure to pass audio paramteres for gaudio_setup
- make ALSA sound card's name configurable
- add [in/out]_ep_maxpsize
- allocate snd_uac_chip structure during gaudio_setup
- add gaudio_[start/stop]_[capture/playback] functions

---
 drivers/usb/gadget/Kconfig            |   4 +
 drivers/usb/gadget/function/Makefile  |   1 +
 drivers/usb/gadget/function/f_uac2.c  | 699 ++++------------------------------
 drivers/usb/gadget/function/u_audio.c | 632 ++++++++++++++++++++++++++++++
 drivers/usb/gadget/function/u_audio.h |  93 +++++
 drivers/usb/gadget/legacy/Kconfig     |   1 +
 6 files changed, 813 insertions(+), 617 deletions(-)
 create mode 100644 drivers/usb/gadget/function/u_audio.c
 create mode 100644 drivers/usb/gadget/function/u_audio.h
drivers/usb/gadget/Kconfig +4 -0
index 3c3f31c..a25afd8 100644
@@ -158,6 +158,9 @@ config USB_U_SERIAL
 config USB_U_ETHER
 	tristate
 
+config USB_U_AUDIO
+	tristate
+
 config USB_F_SERIAL
 	tristate
 
@@ -400,6 +403,7 @@ config USB_CONFIGFS_F_UAC2
 	depends on SND
 	select USB_LIBCOMPOSITE
 	select SND_PCM
+	select USB_U_AUDIO
 	select USB_F_UAC2
 	help
 	  This Audio function is compatible with USB Audio Class
drivers/usb/gadget/function/Makefile +1 -0
index cb8c225..b29f2ae 100644
@@ -32,6 +32,7 @@ usb_f_mass_storage-y		:= f_mass_storage.o storage_common.o
 obj-$(CONFIG_USB_F_MASS_STORAGE)+= usb_f_mass_storage.o
 usb_f_fs-y			:= f_fs.o
 obj-$(CONFIG_USB_F_FS)		+= usb_f_fs.o
+obj-$(CONFIG_USB_U_AUDIO)	+= u_audio.o
 usb_f_uac1-y			:= f_uac1.o u_uac1.o
 obj-$(CONFIG_USB_F_UAC1)	+= usb_f_uac1.o
 usb_f_uac2-y			:= f_uac2.o
drivers/usb/gadget/function/f_uac2.c +82 -617

892 lines not on this page · raw message

drivers/usb/gadget/function/u_audio.c new file +632 -0

632 lines not on this page · raw message

drivers/usb/gadget/function/u_audio.h new file +93 -0

93 lines not on this page · raw message

drivers/usb/gadget/legacy/Kconfig +1 -0
index 0b36878..5344064 100644
@@ -56,6 +56,7 @@ config USB_AUDIO
 	select SND_PCM
 	select USB_F_UAC1 if GADGET_UAC1
 	select USB_F_UAC2 if !GADGET_UAC1
+	select USB_U_AUDIO if USB_F_UAC2
 	help
 	  This Gadget Audio driver is compatible with USB Audio Class
 	  specification 2.0. It implements 1 AudioControl interface,
Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH v2 2/3] usb: gadget: f_uac2: split out audio core

reproduce:

Hi Ruslan,

[auto build test ERROR on balbi-usb/next]
[also build test ERROR on v4.8-rc2 next-20160816]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Ruslan-Bilovol/USB-Audio-Gadget-refactoring/20160814-185318
base:   https://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git next
config: x86_64-randconfig-a0-08162223 (attached as .config)
compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

drivers/usb/gadget/function/usb_f_uac1.o: In function `gaudio_setup':

>> (.text+0x16ce): multiple definition of `gaudio_setup'
   drivers/usb/gadget/function/u_audio.o:u_audio.c:(.text+0x937): first defined here
   drivers/usb/gadget/function/usb_f_uac1.o: In function `gaudio_cleanup':
>> (.text+0x1147): multiple definition of `gaudio_cleanup'

drivers/usb/gadget/function/u_audio.o:u_audio.c:(.text+0x3f): first defined here

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

Re: [PATCH v2 2/3] usb: gadget: f_uac2: split out audio core

Quoted 14 lines · kbuild test robot

Hi Ruslan,

[auto build test ERROR on balbi-usb/next]
[also build test ERROR on v4.8-rc2 next-20160816]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Ruslan-Bilovol/USB-Audio-Gadget-refactoring/20160814-185318
base:   https://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git next
config: x86_64-randconfig-a0-08162223 (attached as .config)
compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64
Quoted 9 lines

All errors (new ones prefixed by >>):

drivers/usb/gadget/function/usb_f_uac1.o: In function `gaudio_setup':

Quoted 1 line
Quoted 1 line

(.text+0x16ce): multiple definition of `gaudio_setup'

drivers/usb/gadget/function/u_audio.o:u_audio.c:(.text+0x937): first defined here drivers/usb/gadget/function/usb_f_uac1.o: In function `gaudio_cleanup':

Quoted 1 line
Quoted 1 line

(.text+0x1147): multiple definition of `gaudio_cleanup'

drivers/usb/gadget/function/u_audio.o:u_audio.c:(.text+0x3f): first defined here

Arrgh, it now conflicts in this patchset where we keep old f_uac1 functionality. Will fix it in next patchset.

Best regards,
Ruslan

#5 lore raw

PATCH v2 3/3 usb: gadget: add f_uac1 variant based on new u_audio api

In reply to Ruslan Bilovol

This patch adds new function f_uac1_newapi that
uses recently created u_audio api. This makes
f_uac1_newapi implementation much simpler by
reusing existing u_audio core utilities.

This also drops previous f_uac1 approach (write
audio samples directly to existing ALSA sound
card) and moves to more generic/flexible
one - create an f_uac1 ALSA sound card that
represents USB Audio function and allows to
be used by userspace tools.

f_uac1_newapi also has capture support (gadget->host). By default, capture interface has 48000kHz/2ch configuration, same as playback channel has.

f_uac1_newapi descriptors naming conventios
uses f_uac2 driver naming convention that
makes it more common and meaningful.

Comparing to f_uac1, the f_uac1_newapi doesn't
have volume/mute functionality. This is because
the volume/mute feature unit was dummy
implementation since that driver creation (2009)
and never had real volume control or mute
functionality.

g_audio can be built using one of existing
uac functions (f_uac1, f_uac1_newapi or f_uac2)

---
 .../ABI/testing/configfs-usb-gadget-uac1_newapi    |  12 +
 Documentation/usb/gadget-testing.txt               |  41 ++
 drivers/usb/gadget/Kconfig                         |  21 +
 drivers/usb/gadget/function/Makefile               |   2 +
 drivers/usb/gadget/function/f_uac1_newapi.c        | 795 +++++++++++++++++++++
 drivers/usb/gadget/function/u_uac1_newapi.h        |  39 +
 drivers/usb/gadget/legacy/Kconfig                  |  15 +-
 drivers/usb/gadget/legacy/audio.c                  |  52 ++
 8 files changed, 975 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi
 create mode 100644 drivers/usb/gadget/function/f_uac1_newapi.c
 create mode 100644 drivers/usb/gadget/function/u_uac1_newapi.h
Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi new file +12 -0
new file mode 100644
index 0000000..d355275
@@ -0,0 +1,12 @@
+What:		/config/usb-gadget/gadget/functions/uac1_newapi.name
+Date:		Aug 2016
+KernelVersion:	4.9
+Description:
+		The attributes:
+
+		c_chmask - capture channel mask
+		c_srate - capture sampling rate
+		c_ssize - capture sample size (bytes)
+		p_chmask - playback channel mask
+		p_srate - playback sampling rate
+		p_ssize - playback sample size (bytes)
Documentation/usb/gadget-testing.txt +41 -0

50 lines not on this page · raw message

drivers/usb/gadget/Kconfig +21 -0
index a25afd8..abcb539 100644
@@ -194,6 +194,9 @@ config USB_F_FS
 config USB_F_UAC1
 	tristate
 
+config USB_F_UAC1_NEWAPI
+	tristate
+
 config USB_F_UAC2
 	tristate
 
@@ -397,6 +400,24 @@ config USB_CONFIGFS_F_UAC1
 	  This driver requires a real Audio codec to be present
 	  on the device.
 
+config USB_CONFIGFS_F_UAC1_NEWAPI
+	bool "Audio Class 1.0 (new API)"
+	depends on USB_CONFIGFS
+	depends on SND
+	select USB_LIBCOMPOSITE
+	select SND_PCM
+	select USB_U_AUDIO
+	select USB_F_UAC1_NEWAPI
+	help
+	  This Audio function implements 1 AudioControl interface,
+	  1 AudioStreaming Interface each for USB-OUT and USB-IN.
+	  This driver doesn't expect any real Audio codec to be present
+	  on the device - the audio streams are simply sinked to and
+	  sourced from a virtual ALSA sound card created. The user-space
+	  application may choose to do whatever it wants with the data
+	  received from the USB Host and choose to provide whatever it
+	  wants as audio data to the USB Host.
+
 config USB_CONFIGFS_F_UAC2
 	bool "Audio Class 2.0"
 	depends on USB_CONFIGFS
drivers/usb/gadget/function/Makefile +2 -0
index b29f2ae..eeef5f8 100644
@@ -35,6 +35,8 @@ obj-$(CONFIG_USB_F_FS)		+= usb_f_fs.o
 obj-$(CONFIG_USB_U_AUDIO)	+= u_audio.o
 usb_f_uac1-y			:= f_uac1.o u_uac1.o
 obj-$(CONFIG_USB_F_UAC1)	+= usb_f_uac1.o
+usb_f_uac1_newapi-y		:= f_uac1_newapi.o
+obj-$(CONFIG_USB_F_UAC1_NEWAPI)	+= usb_f_uac1_newapi.o
 usb_f_uac2-y			:= f_uac2.o
 obj-$(CONFIG_USB_F_UAC2)	+= usb_f_uac2.o
 usb_f_uvc-y			:= f_uvc.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_configfs.o
drivers/usb/gadget/function/f_uac1_newapi.c new file +795 -0

795 lines not on this page · raw message

drivers/usb/gadget/function/u_uac1_newapi.h new file +39 -0
new file mode 100644
index 0000000..2fcc3cf2
@@ -0,0 +1,39 @@
+/*
+ * u_uac1_newapi.h - Utility definitions for UAC1 function
+ *
+ * Copyright (C) 2016 Ruslan Bilovol <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __U_UAC1_NEWAPI_H
+#define __U_UAC1_NEWAPI_H
+
+#include <linux/usb/composite.h>
+
+#define UAC1_OUT_EP_MAX_PACKET_SIZE	200
+#define UAC1_DEF_CCHMASK	0x3
+#define UAC1_DEF_CSRATE		48000
+#define UAC1_DEF_CSSIZE		2
+#define UAC1_DEF_PCHMASK	0x3
+#define UAC1_DEF_PSRATE		48000
+#define UAC1_DEF_PSSIZE		2
+
+
+struct f_uac1_newapi_opts {
+	struct usb_function_instance	func_inst;
+	int				c_chmask;
+	int				c_srate;
+	int				c_ssize;
+	int				p_chmask;
+	int				p_srate;
+	int				p_ssize;
+	unsigned			bound:1;
+
+	struct mutex			lock;
+	int				refcnt;
+};
+
+#endif /* __U_UAC1_NEWAPI_H */
drivers/usb/gadget/legacy/Kconfig +13 -2
index 5344064..9757a3a 100644
@@ -54,9 +54,10 @@ config USB_AUDIO
 	depends on SND
 	select USB_LIBCOMPOSITE
 	select SND_PCM
-	select USB_F_UAC1 if GADGET_UAC1
+	select USB_F_UAC1 if (GADGET_UAC1 && !GADGET_UAC1_NEWAPI)
+	select USB_F_UAC1_NEWAPI if (GADGET_UAC1 && GADGET_UAC1_NEWAPI)
 	select USB_F_UAC2 if !GADGET_UAC1
-	select USB_U_AUDIO if USB_F_UAC2
+	select USB_U_AUDIO if (USB_F_UAC2 || USB_F_UAC1_NEWAPI)
 	help
 	  This Gadget Audio driver is compatible with USB Audio Class
 	  specification 2.0. It implements 1 AudioControl interface,
@@ -81,6 +82,16 @@ config GADGET_UAC1
 	  paths hardwired to the Audio codec chip on-board and doesn't work
 	  without one.
 
+config GADGET_UAC1_NEWAPI
+	bool "Use new API (u_audio) for UAC 1.0 driver"
+	depends on GADGET_UAC1
+	help
+	  This is an alternative UAC Spec-1.0 driver implementation
+	  that uses new API (u_audio), and doesn't expect any real
+	  audio codec to be present on the device (the audio streams
+	  are simply sinked to and sourced from a virtual ALSA sound
+	  card created)
+
 config USB_ETH
 	tristate "Ethernet Gadget (with CDC Ethernet support)"
 	depends on NET
drivers/usb/gadget/legacy/audio.c +52 -0

91 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

Re: [PATCH v2 3/3] usb: gadget: add f_uac1 variant based on new u_audio api

Quoted 138 lines · Ruslan Bilovol

This patch adds new function f_uac1_newapi that uses recently created u_audio api. This makes f_uac1_newapi implementation much simpler by reusing existing u_audio core utilities.

This also drops previous f_uac1 approach (write audio samples directly to existing ALSA sound card) and moves to more generic/flexible
one - create an f_uac1 ALSA sound card that represents USB Audio function and allows to be used by userspace tools.

f_uac1_newapi also has capture support (gadget->host). By default, capture interface has 48000kHz/2ch configuration, same as playback channel has.

f_uac1_newapi descriptors naming conventios uses f_uac2 driver naming convention that makes it more common and meaningful.

Comparing to f_uac1, the f_uac1_newapi doesn't have volume/mute functionality. This is because the volume/mute feature unit was dummy implementation since that driver creation (2009) and never had real volume control or mute functionality.

g_audio can be built using one of existing uac functions (f_uac1, f_uac1_newapi or f_uac2)

---
 .../ABI/testing/configfs-usb-gadget-uac1_newapi    |  12 +
 Documentation/usb/gadget-testing.txt               |  41 ++
 drivers/usb/gadget/Kconfig                         |  21 +
 drivers/usb/gadget/function/Makefile               |   2 +
 drivers/usb/gadget/function/f_uac1_newapi.c        | 795 +++++++++++++++++++++
 drivers/usb/gadget/function/u_uac1_newapi.h        |  39 +
 drivers/usb/gadget/legacy/Kconfig                  |  15 +-
 drivers/usb/gadget/legacy/audio.c                  |  52 ++
 8 files changed, 975 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi
 create mode 100644 drivers/usb/gadget/function/f_uac1_newapi.c
 create mode 100644 drivers/usb/gadget/function/u_uac1_newapi.h
Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi new file +12 -0
new file mode 100644
index 0000000..d355275
@@ -0,0 +1,12 @@
+What:		/config/usb-gadget/gadget/functions/uac1_newapi.name
+Date:		Aug 2016
+KernelVersion:	4.9
+Description:
+		The attributes:
+
+		c_chmask - capture channel mask
+		c_srate - capture sampling rate
+		c_ssize - capture sample size (bytes)
+		p_chmask - playback channel mask
+		p_srate - playback sampling rate
+		p_ssize - playback sample size (bytes)
Documentation/usb/gadget-testing.txt +41 -0
index 5819605..4598d7f 100644
@@ -20,6 +20,7 @@ provided by gadgets.
 17. UAC2 function
 18. UVC function
 19. PRINTER function
+20. UAC1 function (new API)
 
 
 1. ACM function
@@ -770,3 +771,43 @@ host:
 
 More advanced testing can be done with the prn_example
 described in Documentation/usb/gadget-printer.txt.
+
+
+20. UAC1 function (new API, using u_audio)
+=================
+
+The function is provided by usb_f_uac1_newapi.ko module.
+
+Function-specific configfs interface
+------------------------------------
+
+The function name to use when creating the function directory
+is "uac1_newapi". The uac1_newapi function provides these attributes
+in its function directory:
+
+	c_chmask - capture channel mask
+	c_srate - capture sampling rate
+	c_ssize - capture sample size (bytes)
+	p_chmask - playback channel mask
+	p_srate - playback sampling rate
+	p_ssize - playback sample size (bytes)
+
+The attributes have sane default values.
+
+Testing the UAC1 function
+-------------------------
+
+device: run the gadget
+host: aplay -l # should list our USB Audio Gadget
+
+This function does not require real hardware support, it just
+sends a stream of audio data to/from the host. In order to
+actually hear something at the device side, a command similar
+to this must be used at the device side:
+
+$ arecord -f dat -t wav -D hw:2,0 | aplay -D hw:0,0 &
+
+e.g.:
+
+$ arecord -f dat -t wav -D hw:CARD=UAC1Gadget,DEV=0 | \
+aplay -D default:CARD=OdroidU3
drivers/usb/gadget/Kconfig +4 -0
index a25afd8..abcb539 100644
@@ -194,6 +194,9 @@ config USB_F_FS
 config USB_F_UAC1
 	tristate
 
+config USB_F_UAC1_NEWAPI
+	tristate
+
 config USB_F_UAC2
 	tristate
 
@@ -397,6 +400,24 @@ config USB_CONFIGFS_F_UAC1
 	  This driver requires a real Audio codec to be present
 	  on the device.
 
+config USB_CONFIGFS_F_UAC1_NEWAPI

drivers/usb/gadget/Kconfig config USB_CONFIGFS_F_UAC1 · line 413

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+	bool "Audio Class 1.0 (new API)"
+	depends on USB_CONFIGFS
+	depends on SND
+	select USB_LIBCOMPOSITE
+	select SND_PCM
+	select USB_U_AUDIO
+	select USB_F_UAC1_NEWAPI
+	help
+	  This Audio function implements 1 AudioControl interface,
+	  1 AudioStreaming Interface each for USB-OUT and USB-IN.

%s/1/one

Signature · 2 lines
Best Regards,
Peter Chen
#7 lore raw

Re: [PATCH v2 3/3] usb: gadget: add f_uac1 variant based on new u_audio api

Quoted 138 lines · Peter Chen
Quoted 137 lines · Ruslan Bilovol

This patch adds new function f_uac1_newapi that uses recently created u_audio api. This makes f_uac1_newapi implementation much simpler by reusing existing u_audio core utilities.

This also drops previous f_uac1 approach (write audio samples directly to existing ALSA sound card) and moves to more generic/flexible
one - create an f_uac1 ALSA sound card that represents USB Audio function and allows to be used by userspace tools.

f_uac1_newapi also has capture support (gadget->host). By default, capture interface has 48000kHz/2ch configuration, same as playback channel has.

f_uac1_newapi descriptors naming conventios uses f_uac2 driver naming convention that makes it more common and meaningful.

Comparing to f_uac1, the f_uac1_newapi doesn't have volume/mute functionality. This is because the volume/mute feature unit was dummy implementation since that driver creation (2009) and never had real volume control or mute functionality.

g_audio can be built using one of existing uac functions (f_uac1, f_uac1_newapi or f_uac2)

---
 .../ABI/testing/configfs-usb-gadget-uac1_newapi    |  12 +
 Documentation/usb/gadget-testing.txt               |  41 ++
 drivers/usb/gadget/Kconfig                         |  21 +
 drivers/usb/gadget/function/Makefile               |   2 +
 drivers/usb/gadget/function/f_uac1_newapi.c        | 795 +++++++++++++++++++++
 drivers/usb/gadget/function/u_uac1_newapi.h        |  39 +
 drivers/usb/gadget/legacy/Kconfig                  |  15 +-
 drivers/usb/gadget/legacy/audio.c                  |  52 ++
 8 files changed, 975 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi
 create mode 100644 drivers/usb/gadget/function/f_uac1_newapi.c
 create mode 100644 drivers/usb/gadget/function/u_uac1_newapi.h
Documentation/ABI/testing/configfs-usb-gadget-uac1_newapi new file +12 -0
new file mode 100644
index 0000000..d355275
@@ -0,0 +1,12 @@
+What:                /config/usb-gadget/gadget/functions/uac1_newapi.name
+Date:                Aug 2016
+KernelVersion:       4.9
+Description:
+             The attributes:
+
+             c_chmask - capture channel mask
+             c_srate - capture sampling rate
+             c_ssize - capture sample size (bytes)
+             p_chmask - playback channel mask
+             p_srate - playback sampling rate
+             p_ssize - playback sample size (bytes)
Documentation/usb/gadget-testing.txt +41 -0
index 5819605..4598d7f 100644
@@ -20,6 +20,7 @@ provided by gadgets.
 17. UAC2 function
 18. UVC function
 19. PRINTER function
+20. UAC1 function (new API)
 
 
 1. ACM function
@@ -770,3 +771,43 @@ host:
 
 More advanced testing can be done with the prn_example
 described in Documentation/usb/gadget-printer.txt.
+
+
+20. UAC1 function (new API, using u_audio)
+=================
+
+The function is provided by usb_f_uac1_newapi.ko module.
+
+Function-specific configfs interface
+------------------------------------
+
+The function name to use when creating the function directory
+is "uac1_newapi". The uac1_newapi function provides these attributes
+in its function directory:
+
+     c_chmask - capture channel mask
+     c_srate - capture sampling rate
+     c_ssize - capture sample size (bytes)
+     p_chmask - playback channel mask
+     p_srate - playback sampling rate
+     p_ssize - playback sample size (bytes)
+
+The attributes have sane default values.
+
+Testing the UAC1 function
+-------------------------
+
+device: run the gadget
+host: aplay -l # should list our USB Audio Gadget
+
+This function does not require real hardware support, it just
+sends a stream of audio data to/from the host. In order to
+actually hear something at the device side, a command similar
+to this must be used at the device side:
+
+$ arecord -f dat -t wav -D hw:2,0 | aplay -D hw:0,0 &
+
+e.g.:
+
+$ arecord -f dat -t wav -D hw:CARD=UAC1Gadget,DEV=0 | \
+aplay -D default:CARD=OdroidU3
drivers/usb/gadget/Kconfig +3 -0
index a25afd8..abcb539 100644
@@ -194,6 +194,9 @@ config USB_F_FS
 config USB_F_UAC1
      tristate
 
+config USB_F_UAC1_NEWAPI
+     tristate
+
 config USB_F_UAC2
      tristate
 
@@ -397,6 +400,24 @@ config USB_CONFIGFS_F_UAC1
        This driver requires a real Audio codec to be present
        on the device.
 
Quoted 14 lines

drivers/usb/gadget/Kconfig config USB_CONFIGFS_F_UAC1 · line 413

Quoted 11 lines
drivers/usb/gadget/Kconfig +11 -0
+config USB_CONFIGFS_F_UAC1_NEWAPI
+     bool "Audio Class 1.0 (new API)"
+     depends on USB_CONFIGFS
+     depends on SND
+     select USB_LIBCOMPOSITE
+     select SND_PCM
+     select USB_U_AUDIO
+     select USB_F_UAC1_NEWAPI
+     help
+       This Audio function implements 1 AudioControl interface,
+       1 AudioStreaming Interface each for USB-OUT and USB-IN.

%s/1/one

That's same naming as already used for UAC1 and UAC2 description in this Kconfig file. Let's keep it "1" for consistency

Best regards,
Ruslan

#8 lore raw
In reply to Ruslan Bilovol
Quoted 44 lines · Ruslan Bilovol

I came to this patch series when wanted to do two things:
- use UAC1 as virtual ALSA sound card on gadget side, just like UAC2 is used so it's possible to do rate resampling
- have both playback/capture support in UAC1

Since I wanted to have same behavior for both UAC1/UAC2, obviously I've got an utility part (u_audio.c) for virtual ALSA sound card handling like we have for ethernet(u_ether) or serial(u_serial) functions. Function-specific parts (f_uac1/f_uac2) became almost as storage for class-specific USB descriptors, some boilerplate for configfs, binding and few USB config request handling.

Originally in RFC [1] I've posted before, there was major change to f_uac1 after that it couldn't do direct play to existing ALSA sound card anymore, representing audio on gadget side as virtual ALSA sound card where audio streams are simply sinked to and sourced from it, so it may break current usecase for some people (and that's why it was RFC).

During RFC discussion, it was agreed to not touch existing f_uac1 implementation and create new one instead. This patchset (v2) introduced new function named f_uac1_newapi and doesn't touch current f_uac1 implementation, so people still can use old behavior

Now, it's possible to use existing user-space applications for audio routing between Audio Gadget and real sound card. I personally use alsaloop tool from alsautils and have ability to create PCM loopback between two different ALSA cards using rate resampling, which was not possible with previous "direct play to ALSA card" approach in f_uac1.

While here, also dropped redundant platform driver/device creation in f_uac2 driver (as well as didn't add "never implemented" volume/mute functionality in f_uac1 to f_uac1_newapi) that made this work even easier to do.

Quoted 5 lines

This series is tested with both legacy g_audio.ko and modern configfs approaches under Ubuntu 14.04 (UAC1 and UAC2) and under Windows7 x64 (UAC1 only) having perfect results in all cases.

I find UAC2 (UAC1 is ok) support is not well with the latest mainline kernel w/o your patch set. The windows7 can't install the driver successfully and the playback shows underrun (using local codec) using Linux host.

Do you use the unchanged mainline kernel?

My configfs parameters like below:
echo 2 > functions/uac2.1/c_ssize
echo 48000 > functions/uac2.1/c_srate
echo 3 > functions/uac2.1/c_chmask
echo 2 > functions/uac2.1/p_ssize
echo 48000 > functions/uac2.1/p_srate
echo 3 > functions/uac2.1/p_chmask

Console output:

root@imx6qdlsolo:~# arecord -f dat -t wav -D hw:1,0 | aplay -D hw:0,0 &
[1] 859
root@imx6qdlsolo:~# 
root@imx6qdlsolo:~# Recording WAVE 'stdin' : Signed 16 bit Little
Endian, Rate 48000 Hz, Stereo
Playing WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz,
Stereo
underrun!!! (at least 36.634 ms long)
underrun!!! (at least 36.117 ms long)
underrun!!! (at least 42.132 ms long)
underrun!!! (at least 40.157 ms long)
underrun!!! (at least 36.207 ms long)
underrun!!! (at least 39.173 ms long)
underrun!!! (at least 36.119 ms long)
underrun!!! (at least 36.164 ms long)
Signature · 2 lines
Best Regards,
Peter Chen
#9 lore raw
Quoted 3 lines · Peter Chen

I find UAC2 (UAC1 is ok) support is not well with the latest mainline kernel w/o your patch set. The windows7 can't install the driver successfully

Windows does not have UAC2 support.

Quoted 4 lines

and the playback shows underrun (using local codec) using Linux host.

# arecord -f dat -t wav -D hw:1,0 | aplay -D hw:0,0 &

The clocks of the two devices are not synchronized.

In the ALSA API, a PCM device is assumed to have its own clock, so it is not possible to synchronize the USB gadget to the actual sound device without some separate mechanism (like the old uac1 gadget probably has).

Regards,
Clemens

#10 lore raw
Quoted 7 lines · Clemens Ladisch
Quoted 3 lines · Peter Chen

I find UAC2 (UAC1 is ok) support is not well with the latest mainline kernel w/o your patch set. The windows7 can't install the driver successfully

Windows does not have UAC2 support.

Thanks, before windows7 or all windows versions have no UAC2 support?

Quoted 11 lines
Quoted 4 lines

and the playback shows underrun (using local codec) using Linux host.

# arecord -f dat -t wav -D hw:1,0 | aplay -D hw:0,0 &

The clocks of the two devices are not synchronized.

In the ALSA API, a PCM device is assumed to have its own clock, so it is not possible to synchronize the USB gadget to the actual sound device without some separate mechanism (like the old uac1 gadget probably has).

The reason for immediate underrun is the small USB request number. The default request number of USB is 2 for UAC2, but 256 for UAC1. I have a internal 4.1.y version which works fine for UAC2 with only two requests, maybe ALSA has changed some recently.

Yes, we need feedback endpoint to adjust sample rate for mismatch clocks between USB and codec.

Signature · 2 lines
Best Regards,
Peter Chen
#11 lore raw
Quoted 4 lines · Peter Chen
Quoted 1 line · Clemens Ladisch

Windows does not have UAC2 support.

Thanks, before windows7 or all windows versions have no UAC2 support?

So far, no version has it.

Regards,
Clemens

#12 lore raw
In reply to Peter Chen
Quoted 51 lines · Peter Chen
Quoted 44 lines · Ruslan Bilovol

I came to this patch series when wanted to do two things:
- use UAC1 as virtual ALSA sound card on gadget side, just like UAC2 is used so it's possible to do rate resampling
- have both playback/capture support in UAC1

Since I wanted to have same behavior for both UAC1/UAC2, obviously I've got an utility part (u_audio.c) for virtual ALSA sound card handling like we have for ethernet(u_ether) or serial(u_serial) functions. Function-specific parts (f_uac1/f_uac2) became almost as storage for class-specific USB descriptors, some boilerplate for configfs, binding and few USB config request handling.

Originally in RFC [1] I've posted before, there was major change to f_uac1 after that it couldn't do direct play to existing ALSA sound card anymore, representing audio on gadget side as virtual ALSA sound card where audio streams are simply sinked to and sourced from it, so it may break current usecase for some people (and that's why it was RFC).

During RFC discussion, it was agreed to not touch existing f_uac1 implementation and create new one instead. This patchset (v2) introduced new function named f_uac1_newapi and doesn't touch current f_uac1 implementation, so people still can use old behavior

Now, it's possible to use existing user-space applications for audio routing between Audio Gadget and real sound card. I personally use alsaloop tool from alsautils and have ability to create PCM loopback between two different ALSA cards using rate resampling, which was not possible with previous "direct play to ALSA card" approach in f_uac1.

While here, also dropped redundant platform driver/device creation in f_uac2 driver (as well as didn't add "never implemented" volume/mute functionality in f_uac1 to f_uac1_newapi) that made this work even easier to do.

Quoted 5 lines

This series is tested with both legacy g_audio.ko and modern configfs approaches under Ubuntu 14.04 (UAC1 and UAC2) and under Windows7 x64 (UAC1 only) having perfect results in all cases.

Quoted 4 lines

I find UAC2 (UAC1 is ok) support is not well with the latest mainline kernel w/o your patch set. The windows7 can't install the driver successfully and the playback shows underrun (using local codec) using Linux host.

As Clemens already pointed, Windows 7 doesn't have
UAC2 support "out of the box". That's why I tested only UAC1 on it.

Quoted 2 lines

Do you use the unchanged mainline kernel?

Yes, this patchset is based on v4.8-rc1 tag.

Quoted 8 lines

My configfs parameters like below:
echo 2 > functions/uac2.1/c_ssize
echo 48000 > functions/uac2.1/c_srate
echo 3 > functions/uac2.1/c_chmask
echo 2 > functions/uac2.1/p_ssize
echo 48000 > functions/uac2.1/p_srate
echo 3 > functions/uac2.1/p_chmask

I usually don't change these (use default values here)

I use BeagleBone Black for this patchset verification. Here is how I configure it through configfs:

uac_2
------------
mkdir cfg
mount none cfg -t configfs
mkdir cfg/usb_gadget/g1
cd cfg/usb_gadget/g1
mkdir configs/c.1
mkdir functions/uac2.0
mkdir strings/0x409
mkdir configs/c.1/strings/0x409
echo 0x0101 > idProduct
echo 0x1d6b > idVendor
echo my-serial-num > strings/0x409/serialnumber echo my-manufacturer > strings/0x409/manufacturer echo "Test gadget" > strings/0x409/product echo "Conf 1" > configs/c.1/strings/0x409/configuration echo 120 > configs/c.1/MaxPower
ln -s functions/uac2.0 configs/c.1
echo musb-hdrc.0.auto > UDC

uac_1 (new api)
-------------
mkdir cfg
mount none cfg -t configfs
mkdir cfg/usb_gadget/g1
cd cfg/usb_gadget/g1
mkdir configs/c.1
mkdir functions/uac1_newapi.0
mkdir strings/0x409
mkdir configs/c.1/strings/0x409
echo 0x0101 > idProduct
echo 0x1d6b > idVendor
echo my-serial-num > strings/0x409/serialnumber echo my-manufacturer > strings/0x409/manufacturer echo "Test gadget" > strings/0x409/product echo "Conf 1" > configs/c.1/strings/0x409/configuration echo 120 > configs/c.1/MaxPower
ln -s functions/uac1_newapi.0 configs/c.1
echo musb-hdrc.0.auto > UDC

Quoted 20 lines

Console output:

root@imx6qdlsolo:~# arecord -f dat -t wav -D hw:1,0 | aplay -D hw:0,0 &
[1] 859
root@imx6qdlsolo:~#
root@imx6qdlsolo:~# Recording WAVE 'stdin' : Signed 16 bit Little
Endian, Rate 48000 Hz, Stereo
Playing WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz,
Stereo
underrun!!! (at least 36.634 ms long)
underrun!!! (at least 36.117 ms long)
underrun!!! (at least 42.132 ms long)
underrun!!! (at least 40.157 ms long)
underrun!!! (at least 36.207 ms long)
underrun!!! (at least 39.173 ms long)
underrun!!! (at least 36.119 ms long)
underrun!!! (at least 36.164 ms long)

I use alsaloop tool that is more powerful than just arecord/aplay through pipe. It is compiled with libsamplerate so can do resampling on the fly. Also it has few clever algorithms for processing overruns/underruns. There are many possible configuration option combinations. For example, one of used by me (but I use other combinations as well) was:

$ alsaloop -C hw:0,0 -P hw:1,0 -t 10000  -A 3 -S 1 -b -v

This is for routing audio from gadget to real ALSA card

Hope this may be helpful for you and for other people who want to try this new u_audio API.

Best regards,
Ruslan