Skip to contentSkip to conversation

USB Audio Gadget refactoring

PATCH Superseded Ruslan Bilovol 2 participants 6 messages Aug 17, 2016 – Feb 4, 2017

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 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.

v3 changes:
- renamed u_audio exported symbols so they don't conflict with old f_uac1 if both are built-in.

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                  |  52 ++
 11 files changed, 1801 insertions(+), 683 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 v3 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 v3 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 g_audio_setup
- make ALSA sound card's name configurable
- add [in/out]_ep_maxpsize
- allocate snd_uac_chip structure during g_audio_setup
- add u_audio_[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

PATCH v3 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
#4 lore raw
In reply to Ruslan Bilovol

Hi,

Quoted 49 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.

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 8 lines

Comments, testing are welcome.

v3 changes:
- renamed u_audio exported symbols so they don't conflict with old f_uac1 if both are built-in.

v2 changes:
- do not touch f_uac1, instead created f_uac1_newapi

f_uac1_newapi???? What the hell man? :-s

Sure you can't change f_uac1 to newapi without introducing userland visible changes? We really don't want to add another copy of f_uac1, sorry.

Signature · 1 line
balbi
#5 lore raw

Hi Felipe,

Quoted 55 lines · Felipe Balbi

Hi,

Quoted 51 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.

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.

Quoted 8 lines
Quoted 6 lines

v3 changes:
- renamed u_audio exported symbols so they don't conflict with old f_uac1 if both are built-in.

v2 changes:
- do not touch f_uac1, instead created f_uac1_newapi

f_uac1_newapi???? What the hell man? :-s

Yes, sometimes it's difficult to create laconic name that shows difference to old f_uac1. We can name it alternatively "f_uac1_alsa" or maybe anybody else have good idea here. I just wanted to show here that it's f_uac1 with completely different functionality (expose ALSA card to userspace instead of consuming existing one)

Quoted 4 lines

Sure you can't change f_uac1 to newapi without introducing userland visible changes? We really don't want to add another copy of f_uac1, sorry.

Do you mean same f_uac1.ko module will be able to use existing ALSA card (old api) and create it own as well (new api) in the same time at runtime?

By the way, while I understand your desire to not introduce userland visible changes, in this particular case it doesn't make much sense, since that's, again, two opposite approaches to consume and transfer data to userspace
Moreover, now old f_uac1 and new f_uac1_newapi have different functionality exposed to host (first one has only playback, second one - playback+capture), so even USB descriptors are different.

So userspace compatibility in this case will make f_uac1 driver too difficult to maintain and add new functionality.

So I prefer to keep the new version of f_uac1 function and let it move forward with new features.

Best regards,
Ruslan