Skip to contentSkip to conversation

Sharing media resources across ALSA and au0828 drivers

PATCH Superseded Shuah Khan 3 participants 11 messages Jan 21, 2016 – Feb 1, 2016

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Shuah Khan

author
#0 lore raw

cover Sharing media resources across ALSA and au0828 drivers

Please note that I am sending just the 4 patches that changed during series rebase to Linux 4.5. The rebase was pain free. media_device_init() type change required changes to patches 18 and 26. I re-tested the series and everything looks good.

[PATCH 16/31] media: au0828 video remove au0828_enable_analog_tuner() [PATCH 18/31] media: au0828 change to use Managed Media Controller API [PATCH 22/31] media: dvb-core create tuner to demod pad link in disabled state [PATCH v3 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Links:
PATCH Series that contains all 31 patches:
https://lkml.org/lkml/2016/1/6/668

ALSA patches went through reviews and links
to PATCH V3 (ALSA):
https://lkml.org/lkml/2016/1/18/556
https://lkml.org/lkml/2016/1/18/557
https://lkml.org/lkml/2016/1/18/558

Compile warns patches:
https://lkml.org/lkml/2016/1/19/520
https://lkml.org/lkml/2016/1/19/518
https://lkml.org/lkml/2016/1/19/519

Mauro and Takashi:
Please let me know if you would rather see the entire patch series generated for 4.5-rc1 and sent out in a complete series instead of just the rebased patches.

Mauro Carvalho Chehab (1):
uapi/media.h: Declare interface types for ALSA

Shuah Khan (30):
media: Add ALSA Media Controller function entities
media: Media Controller register/unregister entity_notify API
media: Media Controller enable/disable source handler API
media: Media Controller fix to not let stream_count go negative
media: Media Controller export non locking __media_entity_setup_link()
media: Media Controller non-locking
__media_entity_pipeline_start/stop()
media: v4l-core add v4l_enable/disable_media_tuner() helper functions
media: v4l2-core add v4l_vb2q_enable_media_tuner() helper
media: Move au8522_media_pads enum to au8522.h from au8522_priv.h
media: au8522 change to create MC pad for ALSA Audio Out
media: au0828 Use au8522_media_pads enum for pad defines
media: au0828 fix au0828_create_media_graph() entity checks
media: Change v4l-core to check for tuner availability
media: dvb-frontend invoke enable/disable_source handlers
media: au0828 video remove au0828_enable_analog_tuner()
media: au0828 video change to use v4l_enable_media_tuner()
media: au0828 change to use Managed Media Controller API
media: au0828 handle media_init and media_register window
media: au0828 change to register/unregister entity_notify hook
media: au0828 create tuner to decoder link in deactivated state
media: dvb-core create tuner to demod pad link in disabled state
media: au0828 implement enable_source and disable_source handlers
media: au0828 fix null pointer reference in

    au0828_create_media_graph()

media: au0828 fix to not call media_device_unregister_entity_notify() sound/usb: Update ALSA driver to use Managed Media Controller API sound/usb: Create media mixer function and control interface entities
media: au0828 create link between ALSA Mixer and decoder
media: track media device unregister in progress sound/usb: Check media device unregister progress state
media: au0828 change to check media device unregister progress state

 drivers/media/dvb-core/dvb_frontend.c        | 139 ++---------
 drivers/media/dvb-core/dvb_frontend.h        |   3 +
 drivers/media/dvb-core/dvbdev.c              |   4 +-
 drivers/media/dvb-frontends/au8522.h         |   8 +
 drivers/media/dvb-frontends/au8522_decoder.c |   1 +
 drivers/media/dvb-frontends/au8522_priv.h    |   8 -
 drivers/media/media-device.c                 |  66 +++++-
 drivers/media/media-entity.c                 |  65 +++--
 drivers/media/usb/au0828/au0828-core.c       | 340 +++++++++++++++++++++++----
 drivers/media/usb/au0828/au0828-video.c      |  75 +-----
 drivers/media/usb/au0828/au0828.h            |   9 +
 drivers/media/v4l2-core/v4l2-dev.c           |  48 ++++
 drivers/media/v4l2-core/v4l2-fh.c            |   1 +
 drivers/media/v4l2-core/v4l2-ioctl.c         |  29 +++
 drivers/media/v4l2-core/videobuf2-core.c     |   4 +
 include/media/media-device.h                 |  61 +++++
 include/media/media-entity.h                 |  12 +
 include/media/v4l2-dev.h                     |   5 +
 include/uapi/linux/media.h                   |  17 +-
 sound/usb/Kconfig                            |   4 +
 sound/usb/Makefile                           |   2 +
 sound/usb/card.c                             |  16 ++
 sound/usb/card.h                             |   1 +
 sound/usb/media.c                            | 299 +++++++++++++++++++++++
 sound/usb/media.h                            |  76 ++++++
 sound/usb/mixer.h                            |   1 +
 sound/usb/pcm.c                              |  28 ++-
 sound/usb/quirks-table.h                     |   1 +
 sound/usb/quirks.c                           |   5 +
 sound/usb/stream.c                           |   1 +
 sound/usb/usbaudio.h                         |   2 +
 31 files changed, 1070 insertions(+), 261 deletions(-)
 create mode 100644 sound/usb/media.c
 create mode 100644 sound/usb/media.h
Signature · 1 line
2.5.0
author
#1 lore raw

PATCH 16/31 media: au0828 video remove au0828_enable_analog_tuner()

au0828_enable_analog_tuner() is no longer needed with v4l2-core and au0828-video invoking enable_source and disable_source handlers. In addition, it is unnecessary to check for tuner availability in queue_setup() as v4l2-core handles the tuner availability checks.

---
 drivers/media/usb/au0828/au0828-video.c | 61 ---------------------------------
 1 file changed, 61 deletions(-)
drivers/media/usb/au0828/au0828-video.c +0 -61
index 8c54fd2..81952c8 100644
@@ -638,64 +638,6 @@ static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 	return rc;
 }
 
-static int au0828_enable_analog_tuner(struct au0828_dev *dev)
-{
-#ifdef CONFIG_MEDIA_CONTROLLER
-	struct media_device *mdev = dev->media_dev;
-	struct media_entity *source;
-	struct media_link *link, *found_link = NULL;
-	int ret, active_links = 0;
-
-	if (!mdev || !dev->decoder)
-		return 0;
-
-	/*
-	 * This will find the tuner that is connected into the decoder.
-	 * Technically, this is not 100% correct, as the device may be
-	 * using an analog input instead of the tuner. However, as we can't
-	 * do DVB streaming while the DMA engine is being used for V4L2,
-	 * this should be enough for the actual needs.
-	 */
-	list_for_each_entry(link, &dev->decoder->links, list) {
-		if (link->sink->entity == dev->decoder) {
-			found_link = link;
-			if (link->flags & MEDIA_LNK_FL_ENABLED)
-				active_links++;
-			break;
-		}
-	}
-
-	if (active_links == 1 || !found_link)
-		return 0;
-
-	source = found_link->source->entity;
-	list_for_each_entry(link, &source->links, list) {
-		struct media_entity *sink;
-		int flags = 0;
-
-		sink = link->sink->entity;
-
-		if (sink == dev->decoder)
-			flags = MEDIA_LNK_FL_ENABLED;
-
-		ret = media_entity_setup_link(link, flags);
-		if (ret) {
-			pr_err(
-				"Couldn't change link %s->%s to %s. Error %d\n",
-				source->name, sink->name,
-				flags ? "enabled" : "disabled",
-				ret);
-			return ret;
-		} else
-			au0828_isocdbg(
-				"link %s->%s was %s\n",
-				source->name, sink->name,
-				flags ? "ENABLED" : "disabled");
-	}
-#endif
-	return 0;
-}
-
 static int queue_setup(struct vb2_queue *vq,
 		       unsigned int *nbuffers, unsigned int *nplanes,
 		       unsigned int sizes[], void *alloc_ctxs[])
@@ -707,9 +649,6 @@ static int queue_setup(struct vb2_queue *vq,
 		return sizes[0] < size ? -EINVAL : 0;
 	*nplanes = 1;
 	sizes[0] = size;
-
-	au0828_enable_analog_tuner(dev);
-
 	return 0;
 }
 
Signature · 1 line
2.5.0
author
#2 lore raw

PATCH 18/31 media: au0828 change to use Managed Media Controller API

In reply to Shuah Khan

Change au0828 to use Managed Media Controller API to coordinate creating/deleting media device on parent usb device it shares with the snd-usb-audio driver. With this change, au0828 uses media_device_get_devres() to allocate a new media device devres or return an existing one, if it finds one.

---
 drivers/media/usb/au0828/au0828-core.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/media/usb/au0828/au0828-core.c +15 -14
index d0fb40b..10c50f5 100644
@@ -135,10 +135,10 @@ static void au0828_unregister_media_device(struct au0828_dev *dev)
 {
 
 #ifdef CONFIG_MEDIA_CONTROLLER
-	if (dev->media_dev) {
+	if (dev->media_dev &&
+		media_devnode_is_registered(&dev->media_dev->devnode)) {
 		media_device_unregister(dev->media_dev);
 		media_device_cleanup(dev->media_dev);
-		kfree(dev->media_dev);
 		dev->media_dev = NULL;
 	}
 #endif
@@ -224,23 +224,24 @@ static int au0828_media_device_init(struct au0828_dev *dev,
 #ifdef CONFIG_MEDIA_CONTROLLER
 	struct media_device *mdev;
 
-	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+	mdev = media_device_get_devres(&udev->dev);
 	if (!mdev)
 		return -ENOMEM;
 
-	mdev->dev = &udev->dev;
+	if (!media_devnode_is_registered(&mdev->devnode)) {
+		mdev->dev = &udev->dev;
 
-	if (!dev->board.name)
-		strlcpy(mdev->model, "unknown au0828", sizeof(mdev->model));
-	else
-		strlcpy(mdev->model, dev->board.name, sizeof(mdev->model));
-	if (udev->serial)
-		strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
-	strcpy(mdev->bus_info, udev->devpath);
-	mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
-	mdev->driver_version = LINUX_VERSION_CODE;
+		if (udev->product)
+			strlcpy(mdev->model, udev->product,
+				sizeof(mdev->model));
+		if (udev->serial)
+			strlcpy(mdev->serial, udev->serial,
+				sizeof(mdev->serial));
+		strcpy(mdev->bus_info, udev->devpath);
+		mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
 
-	media_device_init(mdev);
+		media_device_init(mdev);
+	}
 
 	dev->media_dev = mdev;
 #endif
Signature · 1 line
2.5.0
author
#3 lore raw

PATCH 22/31 media: dvb-core create tuner to demod pad link in disabled state

In reply to Shuah Khan

Create tuner to demod pad link in disabled state to help avoid disable step when tuner resource is requested by video or audio.

---
 drivers/media/dvb-core/dvbdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/dvb-core/dvbdev.c +2 -2
index 560450a..189f85e 100644
@@ -659,12 +659,12 @@ int dvb_create_media_graph(struct dvb_adapter *adap,
 	}
 
 	if (ntuner && ndemod) {
+		/* create tuner to demod link deactivated */
 		ret = media_create_pad_links(mdev,
 					     MEDIA_ENT_F_TUNER,
 					     tuner, TUNER_PAD_IF_OUTPUT,
 					     MEDIA_ENT_F_DTV_DEMOD,
-					     demod, 0, MEDIA_LNK_FL_ENABLED,
-					     false);
+					     demod, 0, 0, false);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.5.0
author
#4 lore raw

PATCH 26/31 sound/usb: Update ALSA driver to use Managed Media Controller API

In reply to Shuah Khan

Change ALSA driver to use Managed Media Managed Controller API to share tuner with DVB and V4L2 drivers that control AU0828 media device. Media device is created based on a newly added field value in the struct snd_usb_audio_quirk. Using this approach, the media controller API usage can be added for a specific device. In this patch, Media Controller API is enabled for AU0828 hw. snd_usb_create_quirk() will check this new field, if set will create a media device using media_device_get_devres() interface.

media_device_get_devres() will allocate a new media device devres or return an existing one, if it finds one.

During probe, media usb driver could have created the media device devres. It will then initialze (if necessary) and register the media device if it isn't already initialized and registered. Media device unregister is done from usb_audio_disconnect().

During probe, media usb driver could have created the media device devres. It will then register the media device if it isn't already registered. Media device unregister is done from usb_audio_disconnect().

New structure media_ctl is added to group the new fields to support media entity and links. This new structure is added to struct snd_usb_substream.

A new entity_notify hook and a new ALSA capture media entity are registered from snd_usb_pcm_open() after setting up hardware information for the PCM device.

When a new entity is registered, Media Controller API interface media_device_register_entity() invokes all registered entity_notify hooks for the media device. ALSA entity_notify hook parses all the entity list to find a link from decoder it ALSA entity. This indicates that the bridge driver created a link from decoder to ALSA capture entity.

ALSA will attempt to enable the tuner to link the tuner to the decoder calling enable_source handler if one is provided by the bridge driver prior to starting Media pipeline from snd_usb_hw_params(). If enable_source returns with tuner busy condition, then snd_usb_hw_params() will fail with -EBUSY. Media pipeline is stopped from snd_usb_hw_free().

---
 sound/usb/Kconfig        |   4 ++
 sound/usb/Makefile       |   2 +
 sound/usb/card.c         |   7 ++
 sound/usb/card.h         |   1 +
 sound/usb/media.c        | 184 +++++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/media.h        |  55 ++++++++++++++
 sound/usb/pcm.c          |  28 ++++++--
 sound/usb/quirks-table.h |   1 +
 sound/usb/quirks.c       |   5 ++
 sound/usb/stream.c       |   2 +
 sound/usb/usbaudio.h     |   1 +
 11 files changed, 285 insertions(+), 5 deletions(-)
 create mode 100644 sound/usb/media.c
 create mode 100644 sound/usb/media.h
sound/usb/Kconfig +4 -0
index a452ad7..1a71d93 100644
@@ -15,6 +15,7 @@ config SND_USB_AUDIO
 	select SND_RAWMIDI
 	select SND_PCM
 	select BITREVERSE
+	select SND_USB_AUDIO_USE_MEDIA_CONTROLLER	if MEDIA_CONTROLLER
 	help
 	  Say Y here to include support for USB audio and USB MIDI
 	  devices.
@@ -22,6 +23,9 @@ config SND_USB_AUDIO
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-usb-audio.
 
+config SND_USB_AUDIO_USE_MEDIA_CONTROLLER
+	bool
+
 config SND_USB_UA101
 	tristate "Edirol UA-101/UA-1000 driver"
 	select SND_PCM
sound/usb/Makefile +2 -0
index 2d2d122..8dca3c4 100644
@@ -15,6 +15,8 @@ snd-usb-audio-objs := 	card.o \
 			quirks.o \
 			stream.o
 
+snd-usb-audio-$(CONFIG_SND_USB_AUDIO_USE_MEDIA_CONTROLLER) += media.o
+
 snd-usbmidi-lib-objs := midi.o
 
 # Toplevel Module Dependency
sound/usb/card.c +7 -0
index 1f09d95..3b226ed 100644
@@ -66,6 +66,7 @@
 #include "format.h"
 #include "power.h"
 #include "stream.h"
+#include "media.h"
 
 MODULE_AUTHOR("Takashi Iwai <[email protected]>");
 MODULE_DESCRIPTION("USB Audio");
@@ -621,6 +622,12 @@ static void usb_audio_disconnect(struct usb_interface *intf)
 		list_for_each_entry(mixer, &chip->mixer_list, list) {
 			snd_usb_mixer_disconnect(mixer);
 		}
+		/*
+		 * Nice to check quirk && quirk->media_device
+		 * need some special handlings. Doesn't look like
+		 * we have access to quirk here
+		*/
+		media_device_delete(intf);
 	}
 
 	chip->num_interfaces--;
sound/usb/card.h +1 -0
index 71778ca..c15a03c 100644
@@ -156,6 +156,7 @@ struct snd_usb_substream {
 	} dsd_dop;
 
 	bool trigger_tstamp_pending_update; /* trigger timestamp being updated from initial estimate */
+	void *media_ctl;
 };
 
 struct snd_usb_stream {
sound/usb/media.c new file +184 -0

184 lines not on this page · raw message

sound/usb/media.h new file +55 -0

55 lines not on this page · raw message

sound/usb/pcm.c +23 -5
index 9245f52..c3b8486 100644
@@ -35,6 +35,7 @@
 #include "pcm.h"
 #include "clock.h"
 #include "power.h"
+#include "media.h"
 
 #define SUBSTREAM_FLAG_DATA_EP_STARTED	0
 #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
@@ -715,10 +716,14 @@ static int snd_usb_hw_params(struct snd_pcm_substream *substream,
 	struct audioformat *fmt;
 	int ret;
 
+	ret = media_start_pipeline(subs);
+	if (ret)
+		return ret;
+
 	ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
 					       params_buffer_bytes(hw_params));
 	if (ret < 0)
-		return ret;
+		goto err_ret;
 
 	subs->pcm_format = params_format(hw_params);
 	subs->period_bytes = params_period_bytes(hw_params);
@@ -732,22 +737,27 @@ static int snd_usb_hw_params(struct snd_pcm_substream *substream,
 		dev_dbg(&subs->dev->dev,
 			"cannot set format: format = %#x, rate = %d, channels = %d\n",
 			   subs->pcm_format, subs->cur_rate, subs->channels);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto err_ret;
 	}
 
 	ret = snd_usb_lock_shutdown(subs->stream->chip);
 	if (ret < 0)
-		return ret;
+		goto err_ret;
 	ret = set_format(subs, fmt);
 	snd_usb_unlock_shutdown(subs->stream->chip);
 	if (ret < 0)
-		return ret;
+		goto err_ret;
 
 	subs->interface = fmt->iface;
 	subs->altset_idx = fmt->altset_idx;
 	subs->need_setup_ep = true;
 
 	return 0;
+
+err_ret:
+	media_stop_pipeline(subs);
+	return ret;
 }
 
 /*
@@ -759,6 +769,7 @@ static int snd_usb_hw_free(struct snd_pcm_substream *substream)
 {
 	struct snd_usb_substream *subs = substream->runtime->private_data;
 
+	media_stop_pipeline(subs);
 	subs->cur_audiofmt = NULL;
 	subs->cur_rate = 0;
 	subs->period_bytes = 0;
@@ -1219,6 +1230,7 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct snd_usb_substream *subs = &as->substream[direction];
+	int ret;
 
 	subs->interface = -1;
 	subs->altset_idx = 0;
@@ -1232,7 +1244,12 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
 	subs->dsd_dop.channel = 0;
 	subs->dsd_dop.marker = 1;
 
-	return setup_hw_info(runtime, subs);
+	ret = setup_hw_info(runtime, subs);
+	if (ret == 0)
+		ret = media_stream_init(subs, as->pcm, direction);
+	if (ret)
+		snd_usb_autosuspend(subs->stream->chip);
+	return ret;
 }
 
 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
@@ -1241,6 +1258,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
 	struct snd_usb_substream *subs = &as->substream[direction];
 
 	stop_endpoints(subs, true);
+	media_stop_pipeline(subs);
 
 	if (subs->interface >= 0 &&
 	    !snd_usb_lock_shutdown(subs->stream->chip)) {
sound/usb/quirks-table.h +1 -0
index c60a776..9d087b1 100644
@@ -2886,6 +2886,7 @@ YAMAHA_DEVICE(0x7010, "UB99"),
 		.product_name = pname, \
 		.ifnum = QUIRK_ANY_INTERFACE, \
 		.type = QUIRK_AUDIO_ALIGN_TRANSFER, \
+		.media_device = 1, \
 	} \
 }
 
sound/usb/quirks.c +5 -0
index 23ea6d8..fbf056a 100644
@@ -36,6 +36,7 @@
 #include "pcm.h"
 #include "clock.h"
 #include "stream.h"
+#include "media.h"
 
 /*
  * handle the quirks for the contained interfaces
@@ -546,6 +547,10 @@ int snd_usb_create_quirk(struct snd_usb_audio *chip,
 		[QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
 	};
 
+	if (quirk->media_device) {
+		/* don't want to fail when media_device_create() fails */
+		media_device_create(chip, iface);
+	}
 	if (quirk->type < QUIRK_TYPE_COUNT) {
 		return quirk_funcs[quirk->type](chip, iface, driver, quirk);
 	} else {
sound/usb/stream.c +2 -0
index c4dc577..6c7f5c1 100644
@@ -36,6 +36,7 @@
 #include "format.h"
 #include "clock.h"
 #include "stream.h"
+#include "media.h"
 
 /*
  * free a substream
@@ -52,6 +53,7 @@ static void free_substream(struct snd_usb_substream *subs)
 		kfree(fp);
 	}
 	kfree(subs->rate_list.list);
+	media_stream_delete(subs);
 }
 
 
sound/usb/usbaudio.h +1 -0
index b665d85..335f48c 100644
@@ -110,6 +110,7 @@ struct snd_usb_audio_quirk {
 	const char *product_name;
 	int16_t ifnum;
 	uint16_t type;
+	bool media_device;
 	const void *data;
 };
 
Signature · 1 line
2.5.0
#5 lore raw

Re: [PATCH REBASE 4.5 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Quoted 188 lines · Shuah Khan

Change ALSA driver to use Managed Media Managed Controller API to share tuner with DVB and V4L2 drivers that control AU0828 media device. Media device is created based on a newly added field value in the struct snd_usb_audio_quirk. Using this approach, the media controller API usage can be added for a specific device. In this patch, Media Controller API is enabled for AU0828 hw. snd_usb_create_quirk() will check this new field, if set will create a media device using media_device_get_devres() interface.

media_device_get_devres() will allocate a new media device devres or return an existing one, if it finds one.

During probe, media usb driver could have created the media device devres. It will then initialze (if necessary) and register the media device if it isn't already initialized and registered. Media device unregister is done from usb_audio_disconnect().

During probe, media usb driver could have created the media device devres. It will then register the media device if it isn't already registered. Media device unregister is done from usb_audio_disconnect().

New structure media_ctl is added to group the new fields to support media entity and links. This new structure is added to struct snd_usb_substream.

A new entity_notify hook and a new ALSA capture media entity are registered from snd_usb_pcm_open() after setting up hardware information for the PCM device.

When a new entity is registered, Media Controller API interface media_device_register_entity() invokes all registered entity_notify hooks for the media device. ALSA entity_notify hook parses all the entity list to find a link from decoder it ALSA entity. This indicates that the bridge driver created a link from decoder to ALSA capture entity.

ALSA will attempt to enable the tuner to link the tuner to the decoder calling enable_source handler if one is provided by the bridge driver prior to starting Media pipeline from snd_usb_hw_params(). If enable_source returns with tuner busy condition, then snd_usb_hw_params() will fail with -EBUSY. Media pipeline is stopped from snd_usb_hw_free().

---
 sound/usb/Kconfig        |   4 ++
 sound/usb/Makefile       |   2 +
 sound/usb/card.c         |   7 ++
 sound/usb/card.h         |   1 +
 sound/usb/media.c        | 184 +++++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/media.h        |  55 ++++++++++++++
 sound/usb/pcm.c          |  28 ++++++--
 sound/usb/quirks-table.h |   1 +
 sound/usb/quirks.c       |   5 ++
 sound/usb/stream.c       |   2 +
 sound/usb/usbaudio.h     |   1 +
 11 files changed, 285 insertions(+), 5 deletions(-)
 create mode 100644 sound/usb/media.c
 create mode 100644 sound/usb/media.h
sound/usb/Kconfig +4 -0
index a452ad7..1a71d93 100644
@@ -15,6 +15,7 @@ config SND_USB_AUDIO
 	select SND_RAWMIDI
 	select SND_PCM
 	select BITREVERSE
+	select SND_USB_AUDIO_USE_MEDIA_CONTROLLER	if MEDIA_CONTROLLER
 	help
 	  Say Y here to include support for USB audio and USB MIDI
 	  devices.
@@ -22,6 +23,9 @@ config SND_USB_AUDIO
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-usb-audio.
 
+config SND_USB_AUDIO_USE_MEDIA_CONTROLLER
+	bool
+
 config SND_USB_UA101
 	tristate "Edirol UA-101/UA-1000 driver"
 	select SND_PCM
sound/usb/Makefile +2 -0
index 2d2d122..8dca3c4 100644
@@ -15,6 +15,8 @@ snd-usb-audio-objs := 	card.o \
 			quirks.o \
 			stream.o
 
+snd-usb-audio-$(CONFIG_SND_USB_AUDIO_USE_MEDIA_CONTROLLER) += media.o
+
 snd-usbmidi-lib-objs := midi.o
 
 # Toplevel Module Dependency
sound/usb/card.c +7 -0
index 1f09d95..3b226ed 100644
@@ -66,6 +66,7 @@
 #include "format.h"
 #include "power.h"
 #include "stream.h"
+#include "media.h"
 
 MODULE_AUTHOR("Takashi Iwai <[email protected]>");
 MODULE_DESCRIPTION("USB Audio");
@@ -621,6 +622,12 @@ static void usb_audio_disconnect(struct usb_interface *intf)
 		list_for_each_entry(mixer, &chip->mixer_list, list) {
 			snd_usb_mixer_disconnect(mixer);
 		}
+		/*
+		 * Nice to check quirk && quirk->media_device
+		 * need some special handlings. Doesn't look like
+		 * we have access to quirk here
+		*/
+		media_device_delete(intf);
 	}
 
 	chip->num_interfaces--;
sound/usb/card.h +1 -0
index 71778ca..c15a03c 100644
@@ -156,6 +156,7 @@ struct snd_usb_substream {
 	} dsd_dop;
 
 	bool trigger_tstamp_pending_update; /* trigger timestamp being updated from initial estimate */
+	void *media_ctl;
 };
 
 struct snd_usb_stream {
sound/usb/media.c new file +45 -0
new file mode 100644
index 0000000..0987f40
@@ -0,0 +1,184 @@
+/*
+ * media.c - Media Controller specific ALSA driver code
+ *
+ * Copyright (c) 2015 Shuah Khan <[email protected]>
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * This file is released under the GPLv2.
+ */
+
+/*
+ * This file adds Media Controller support to ALSA driver
+ * to use the Media Controller API to share tuner with DVB
+ * and V4L2 drivers that control media device. Media device
+ * is created based on existing quirks framework. Using this
+ * approach, the media controller API usage can be added for
+ * a specific device.
+*/
+
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+#include <sound/pcm.h>
+
+#include "usbaudio.h"
+#include "card.h"
+#include "media.h"
+
+int media_device_create(struct snd_usb_audio *chip,
+			struct usb_interface *iface)
+{
+	struct media_device *mdev;
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+	int ret;
+
+	mdev = media_device_get_devres(&usbdev->dev);
+	if (!mdev)
+		return -ENOMEM;
+	if (!mdev->dev) {
+		/* register media device */
+		mdev->dev = &usbdev->dev;
+		if (usbdev->product)
+			strlcpy(mdev->model, usbdev->product,

sound/usb/media.c line 55

Quoted 10 lines
sound/usb/media.c new file +10 -0
+				sizeof(mdev->model));
+		if (usbdev->serial)
+			strlcpy(mdev->serial, usbdev->serial,
+				sizeof(mdev->serial));
+		strcpy(mdev->bus_info, usbdev->devpath);
+		mdev->hw_revision = le16_to_cpu(usbdev->descriptor.bcdDevice);
+		media_device_init(mdev);
+	}
+	if (!media_devnode_is_registered(&mdev->devnode)) {
+		ret = media_device_register(mdev);

The device registration in ALSA happens not immediately at the call of create but triggered via device dev_register callback. I don't think this would be a big problem but still this may leave some race, e.g. MC device appears without the actual sound device during probe.

Quoted 57 lines
sound/usb/media.c new file +57 -0
+		if (ret) {
+			dev_err(&usbdev->dev,
+				"Couldn't register media device. Error: %d\n",
+				ret);
+			return ret;
+		}
+	}
+	return 0;
+}
+
+void media_device_delete(struct usb_interface *iface)
+{
+	struct media_device *mdev;
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+
+	mdev = media_device_find_devres(&usbdev->dev);
+	if (mdev && media_devnode_is_registered(&mdev->devnode))
+		media_device_unregister(mdev);
+}
+
+static int media_enable_source(struct media_ctl *mctl)
+{
+	if (mctl && mctl->media_dev->enable_source)
+		return mctl->media_dev->enable_source(&mctl->media_entity,
+						      &mctl->media_pipe);
+	return 0;
+}
+
+static void media_disable_source(struct media_ctl *mctl)
+{
+	if (mctl && mctl->media_dev->disable_source)
+		mctl->media_dev->disable_source(&mctl->media_entity);
+}
+
+int media_stream_init(struct snd_usb_substream *subs, struct snd_pcm *pcm,
+			int stream)
+{
+	struct media_device *mdev;
+	struct media_ctl *mctl;
+	struct device *pcm_dev = &pcm->streams[stream].dev;
+	u32 intf_type;
+	int ret = 0;
+
+	mdev = media_device_find_devres(&subs->dev->dev);
+	if (!mdev)
+		return -ENODEV;
+
+	if (subs->media_ctl)
+		return 0;
+
+	/* allocate media_ctl */
+	mctl = kzalloc(sizeof(*mctl), GFP_KERNEL);
+	if (!mctl)
+		return -ENOMEM;
+
+	mctl->media_dev = mdev;
+	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {

sound/usb/media.c line 122

Quoted 10 lines
sound/usb/media.c new file +10 -0
+		intf_type = MEDIA_INTF_T_ALSA_PCM_PLAYBACK;
+		mctl->media_entity.function = MEDIA_ENT_F_AUDIO_PLAYBACK;
+	} else {
+		intf_type = MEDIA_INTF_T_ALSA_PCM_CAPTURE;
+		mctl->media_entity.function = MEDIA_ENT_F_AUDIO_CAPTURE;
+	}
+	mctl->media_entity.name = pcm->name;
+	mctl->media_entity.info.dev.major = MAJOR(pcm_dev->devt);
+	mctl->media_entity.info.dev.minor = MINOR(pcm_dev->devt);
+	mctl->media_pad.flags = MEDIA_PAD_FL_SINK;

Is it always a sink, no source, no matter which direction, playback or capture?

thanks,

Takashi

author
#6 lore raw

Re: [PATCH REBASE 4.5 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Quoted 197 lines · Takashi Iwai
Quoted 195 lines · Shuah Khan

Change ALSA driver to use Managed Media Managed Controller API to share tuner with DVB and V4L2 drivers that control AU0828 media device. Media device is created based on a newly added field value in the struct snd_usb_audio_quirk. Using this approach, the media controller API usage can be added for a specific device. In this patch, Media Controller API is enabled for AU0828 hw. snd_usb_create_quirk() will check this new field, if set will create a media device using media_device_get_devres() interface.

media_device_get_devres() will allocate a new media device devres or return an existing one, if it finds one.

During probe, media usb driver could have created the media device devres. It will then initialze (if necessary) and register the media device if it isn't already initialized and registered. Media device unregister is done from usb_audio_disconnect().

During probe, media usb driver could have created the media device devres. It will then register the media device if it isn't already registered. Media device unregister is done from usb_audio_disconnect().

New structure media_ctl is added to group the new fields to support media entity and links. This new structure is added to struct snd_usb_substream.

A new entity_notify hook and a new ALSA capture media entity are registered from snd_usb_pcm_open() after setting up hardware information for the PCM device.

When a new entity is registered, Media Controller API interface media_device_register_entity() invokes all registered entity_notify hooks for the media device. ALSA entity_notify hook parses all the entity list to find a link from decoder it ALSA entity. This indicates that the bridge driver created a link from decoder to ALSA capture entity.

ALSA will attempt to enable the tuner to link the tuner to the decoder calling enable_source handler if one is provided by the bridge driver prior to starting Media pipeline from snd_usb_hw_params(). If enable_source returns with tuner busy condition, then snd_usb_hw_params() will fail with -EBUSY. Media pipeline is stopped from snd_usb_hw_free().

---
 sound/usb/Kconfig        |   4 ++
 sound/usb/Makefile       |   2 +
 sound/usb/card.c         |   7 ++
 sound/usb/card.h         |   1 +
 sound/usb/media.c        | 184 +++++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/media.h        |  55 ++++++++++++++
 sound/usb/pcm.c          |  28 ++++++--
 sound/usb/quirks-table.h |   1 +
 sound/usb/quirks.c       |   5 ++
 sound/usb/stream.c       |   2 +
 sound/usb/usbaudio.h     |   1 +
 11 files changed, 285 insertions(+), 5 deletions(-)
 create mode 100644 sound/usb/media.c
 create mode 100644 sound/usb/media.h
sound/usb/Kconfig +4 -0
index a452ad7..1a71d93 100644
@@ -15,6 +15,7 @@ config SND_USB_AUDIO
 	select SND_RAWMIDI
 	select SND_PCM
 	select BITREVERSE
+	select SND_USB_AUDIO_USE_MEDIA_CONTROLLER	if MEDIA_CONTROLLER
 	help
 	  Say Y here to include support for USB audio and USB MIDI
 	  devices.
@@ -22,6 +23,9 @@ config SND_USB_AUDIO
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-usb-audio.
 
+config SND_USB_AUDIO_USE_MEDIA_CONTROLLER
+	bool
+
 config SND_USB_UA101
 	tristate "Edirol UA-101/UA-1000 driver"
 	select SND_PCM
sound/usb/Makefile +2 -0
index 2d2d122..8dca3c4 100644
@@ -15,6 +15,8 @@ snd-usb-audio-objs := 	card.o \
 			quirks.o \
 			stream.o
 
+snd-usb-audio-$(CONFIG_SND_USB_AUDIO_USE_MEDIA_CONTROLLER) += media.o
+
 snd-usbmidi-lib-objs := midi.o
 
 # Toplevel Module Dependency
sound/usb/card.c +7 -0
index 1f09d95..3b226ed 100644
@@ -66,6 +66,7 @@
 #include "format.h"
 #include "power.h"
 #include "stream.h"
+#include "media.h"
 
 MODULE_AUTHOR("Takashi Iwai <[email protected]>");
 MODULE_DESCRIPTION("USB Audio");
@@ -621,6 +622,12 @@ static void usb_audio_disconnect(struct usb_interface *intf)
 		list_for_each_entry(mixer, &chip->mixer_list, list) {
 			snd_usb_mixer_disconnect(mixer);
 		}
+		/*
+		 * Nice to check quirk && quirk->media_device
+		 * need some special handlings. Doesn't look like
+		 * we have access to quirk here
+		*/
+		media_device_delete(intf);
 	}
 
 	chip->num_interfaces--;
sound/usb/card.h +1 -0
index 71778ca..c15a03c 100644
@@ -156,6 +156,7 @@ struct snd_usb_substream {
 	} dsd_dop;
 
 	bool trigger_tstamp_pending_update; /* trigger timestamp being updated from initial estimate */
+	void *media_ctl;
 };
 
 struct snd_usb_stream {
sound/usb/media.c new file +52 -0
new file mode 100644
index 0000000..0987f40
@@ -0,0 +1,184 @@
+/*
+ * media.c - Media Controller specific ALSA driver code
+ *
+ * Copyright (c) 2015 Shuah Khan <[email protected]>
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * This file is released under the GPLv2.
+ */
+
+/*
+ * This file adds Media Controller support to ALSA driver
+ * to use the Media Controller API to share tuner with DVB
+ * and V4L2 drivers that control media device. Media device
+ * is created based on existing quirks framework. Using this
+ * approach, the media controller API usage can be added for
+ * a specific device.
+*/
+
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+#include <sound/pcm.h>
+
+#include "usbaudio.h"
+#include "card.h"
+#include "media.h"
+
+int media_device_create(struct snd_usb_audio *chip,
+			struct usb_interface *iface)
+{
+	struct media_device *mdev;
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+	int ret;
+
+	mdev = media_device_get_devres(&usbdev->dev);
+	if (!mdev)
+		return -ENOMEM;
+	if (!mdev->dev) {
+		/* register media device */
+		mdev->dev = &usbdev->dev;
+		if (usbdev->product)
+			strlcpy(mdev->model, usbdev->product,
+				sizeof(mdev->model));
+		if (usbdev->serial)
+			strlcpy(mdev->serial, usbdev->serial,
+				sizeof(mdev->serial));
+		strcpy(mdev->bus_info, usbdev->devpath);
+		mdev->hw_revision = le16_to_cpu(usbdev->descriptor.bcdDevice);
+		media_device_init(mdev);
Quoted 8 lines

sound/usb/media.c line 55

Quoted 3 lines
sound/usb/media.c new file +3 -0
+	}
+	if (!media_devnode_is_registered(&mdev->devnode)) {
+		ret = media_device_register(mdev);

The device registration in ALSA happens not immediately at the call of create but triggered via device dev_register callback. I don't think this would be a big problem but still this may leave some race, e.g. MC device appears without the actual sound device during probe.

Creation and register both happen in media_device_create() when it gets called during quirk handling for au0828 device. You are right that ALSA attempts to init and register media device before sound device during probe. As such, au0828 gets to this step of initializing and registering media device before snd-usb-audio probe runs.

To avoid races with au0828, the following logic is in place.

The reason for the checks if (!mdev->dev) is that au0828 could have done the media_device_init(mdev) by the time ALSA attempts media_device_create() and it could have registered the device as well. Hence, the reason for the second check if (!media_devnode_is_registered(&mdev->devnode))

Are you concerned about other races within snd-usb-audio? I can change it to do the media_device_create() step towards the end of sound probe after the snd_card_register(0 step. Please let me know your preference.

Quoted 64 lines
Quoted 64 lines
sound/usb/media.c new file +62 -0
+		if (ret) {
+			dev_err(&usbdev->dev,
+				"Couldn't register media device. Error: %d\n",
+				ret);
+			return ret;
+		}
+	}
+	return 0;
+}
+
+void media_device_delete(struct usb_interface *iface)
+{
+	struct media_device *mdev;
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+
+	mdev = media_device_find_devres(&usbdev->dev);
+	if (mdev && media_devnode_is_registered(&mdev->devnode))
+		media_device_unregister(mdev);
+}
+
+static int media_enable_source(struct media_ctl *mctl)
+{
+	if (mctl && mctl->media_dev->enable_source)
+		return mctl->media_dev->enable_source(&mctl->media_entity,
+						      &mctl->media_pipe);
+	return 0;
+}
+
+static void media_disable_source(struct media_ctl *mctl)
+{
+	if (mctl && mctl->media_dev->disable_source)
+		mctl->media_dev->disable_source(&mctl->media_entity);
+}
+
+int media_stream_init(struct snd_usb_substream *subs, struct snd_pcm *pcm,
+			int stream)
+{
+	struct media_device *mdev;
+	struct media_ctl *mctl;
+	struct device *pcm_dev = &pcm->streams[stream].dev;
+	u32 intf_type;
+	int ret = 0;
+
+	mdev = media_device_find_devres(&subs->dev->dev);
+	if (!mdev)
+		return -ENODEV;
+
+	if (subs->media_ctl)
+		return 0;
+
+	/* allocate media_ctl */
+	mctl = kzalloc(sizeof(*mctl), GFP_KERNEL);
+	if (!mctl)
+		return -ENOMEM;
+
+	mctl->media_dev = mdev;
+	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		intf_type = MEDIA_INTF_T_ALSA_PCM_PLAYBACK;
+		mctl->media_entity.function = MEDIA_ENT_F_AUDIO_PLAYBACK;
+	} else {
+		intf_type = MEDIA_INTF_T_ALSA_PCM_CAPTURE;
+		mctl->media_entity.function = MEDIA_ENT_F_AUDIO_CAPTURE;
Quoted 9 lines

sound/usb/media.c line 122

Quoted 5 lines
sound/usb/media.c new file +5 -0
+	}
+	mctl->media_entity.name = pcm->name;
+	mctl->media_entity.info.dev.major = MAJOR(pcm_dev->devt);
+	mctl->media_entity.info.dev.minor = MINOR(pcm_dev->devt);
+	mctl->media_pad.flags = MEDIA_PAD_FL_SINK;

Is it always a sink, no source, no matter which direction, playback or capture?

Yes it is always a SINK in both cases. The source is the audio decoder for both PCM capture and Playback nodes.

thanks,
-- Shuah

Signature · 5 lines
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
[email protected] | (970) 217-8978
#7 lore raw

Re: [PATCH REBASE 4.5 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Em Mon, 01 Feb 2016 11:00:47 -0700
Shuah Khan <[email protected]> escreveu:

Quoted 198 lines · Takashi Iwai
Quoted 197 lines
Quoted 195 lines · Shuah Khan

Change ALSA driver to use Managed Media Managed Controller API to share tuner with DVB and V4L2 drivers that control AU0828 media device. Media device is created based on a newly added field value in the struct snd_usb_audio_quirk. Using this approach, the media controller API usage can be added for a specific device. In this patch, Media Controller API is enabled for AU0828 hw. snd_usb_create_quirk() will check this new field, if set will create a media device using media_device_get_devres() interface.

media_device_get_devres() will allocate a new media device devres or return an existing one, if it finds one.

During probe, media usb driver could have created the media device devres. It will then initialze (if necessary) and register the media device if it isn't already initialized and registered. Media device unregister is done from usb_audio_disconnect().

During probe, media usb driver could have created the media device devres. It will then register the media device if it isn't already registered. Media device unregister is done from usb_audio_disconnect().

New structure media_ctl is added to group the new fields to support media entity and links. This new structure is added to struct snd_usb_substream.

A new entity_notify hook and a new ALSA capture media entity are registered from snd_usb_pcm_open() after setting up hardware information for the PCM device.

When a new entity is registered, Media Controller API interface media_device_register_entity() invokes all registered entity_notify hooks for the media device. ALSA entity_notify hook parses all the entity list to find a link from decoder it ALSA entity. This indicates that the bridge driver created a link from decoder to ALSA capture entity.

ALSA will attempt to enable the tuner to link the tuner to the decoder calling enable_source handler if one is provided by the bridge driver prior to starting Media pipeline from snd_usb_hw_params(). If enable_source returns with tuner busy condition, then snd_usb_hw_params() will fail with -EBUSY. Media pipeline is stopped from snd_usb_hw_free().

---
 sound/usb/Kconfig        |   4 ++
 sound/usb/Makefile       |   2 +
 sound/usb/card.c         |   7 ++
 sound/usb/card.h         |   1 +
 sound/usb/media.c        | 184 +++++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/media.h        |  55 ++++++++++++++
 sound/usb/pcm.c          |  28 ++++++--
 sound/usb/quirks-table.h |   1 +
 sound/usb/quirks.c       |   5 ++
 sound/usb/stream.c       |   2 +
 sound/usb/usbaudio.h     |   1 +
 11 files changed, 285 insertions(+), 5 deletions(-)
 create mode 100644 sound/usb/media.c
 create mode 100644 sound/usb/media.h
sound/usb/Kconfig +4 -0
index a452ad7..1a71d93 100644
@@ -15,6 +15,7 @@ config SND_USB_AUDIO
 	select SND_RAWMIDI
 	select SND_PCM
 	select BITREVERSE
+	select SND_USB_AUDIO_USE_MEDIA_CONTROLLER	if MEDIA_CONTROLLER
 	help
 	  Say Y here to include support for USB audio and USB MIDI
 	  devices.
@@ -22,6 +23,9 @@ config SND_USB_AUDIO
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-usb-audio.
 
+config SND_USB_AUDIO_USE_MEDIA_CONTROLLER
+	bool
+
 config SND_USB_UA101
 	tristate "Edirol UA-101/UA-1000 driver"
 	select SND_PCM
sound/usb/Makefile +2 -0
index 2d2d122..8dca3c4 100644
@@ -15,6 +15,8 @@ snd-usb-audio-objs := 	card.o \
 			quirks.o \
 			stream.o
 
+snd-usb-audio-$(CONFIG_SND_USB_AUDIO_USE_MEDIA_CONTROLLER) += media.o
+
 snd-usbmidi-lib-objs := midi.o
 
 # Toplevel Module Dependency
sound/usb/card.c +7 -0
index 1f09d95..3b226ed 100644
@@ -66,6 +66,7 @@
 #include "format.h"
 #include "power.h"
 #include "stream.h"
+#include "media.h"
 
 MODULE_AUTHOR("Takashi Iwai <[email protected]>");
 MODULE_DESCRIPTION("USB Audio");
@@ -621,6 +622,12 @@ static void usb_audio_disconnect(struct usb_interface *intf)
 		list_for_each_entry(mixer, &chip->mixer_list, list) {
 			snd_usb_mixer_disconnect(mixer);
 		}
+		/*
+		 * Nice to check quirk && quirk->media_device
+		 * need some special handlings. Doesn't look like
+		 * we have access to quirk here
+		*/
+		media_device_delete(intf);
 	}
 
 	chip->num_interfaces--;
sound/usb/card.h +1 -0
index 71778ca..c15a03c 100644
@@ -156,6 +156,7 @@ struct snd_usb_substream {
 	} dsd_dop;
 
 	bool trigger_tstamp_pending_update; /* trigger timestamp being updated from initial estimate */
+	void *media_ctl;
 };
 
 struct snd_usb_stream {
sound/usb/media.c new file +52 -0
new file mode 100644
index 0000000..0987f40
@@ -0,0 +1,184 @@
+/*
+ * media.c - Media Controller specific ALSA driver code
+ *
+ * Copyright (c) 2015 Shuah Khan <[email protected]>
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * This file is released under the GPLv2.
+ */
+
+/*
+ * This file adds Media Controller support to ALSA driver
+ * to use the Media Controller API to share tuner with DVB
+ * and V4L2 drivers that control media device. Media device
+ * is created based on existing quirks framework. Using this
+ * approach, the media controller API usage can be added for
+ * a specific device.
+*/
+
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+#include <sound/pcm.h>
+
+#include "usbaudio.h"
+#include "card.h"
+#include "media.h"
+
+int media_device_create(struct snd_usb_audio *chip,
+			struct usb_interface *iface)
+{
+	struct media_device *mdev;
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+	int ret;
+
+	mdev = media_device_get_devres(&usbdev->dev);
+	if (!mdev)
+		return -ENOMEM;
+	if (!mdev->dev) {
+		/* register media device */
+		mdev->dev = &usbdev->dev;
+		if (usbdev->product)
+			strlcpy(mdev->model, usbdev->product,
+				sizeof(mdev->model));
+		if (usbdev->serial)
+			strlcpy(mdev->serial, usbdev->serial,
+				sizeof(mdev->serial));
+		strcpy(mdev->bus_info, usbdev->devpath);
+		mdev->hw_revision = le16_to_cpu(usbdev->descriptor.bcdDevice);
+		media_device_init(mdev);
Quoted 8 lines
Quoted 8 lines

sound/usb/media.c line 55

Quoted 3 lines
sound/usb/media.c new file +3 -0
+	}
+	if (!media_devnode_is_registered(&mdev->devnode)) {
+		ret = media_device_register(mdev);  

The device registration in ALSA happens not immediately at the call of create but triggered via device dev_register callback. I don't think this would be a big problem but still this may leave some race, e.g. MC device appears without the actual sound device during probe.

The best would be to register the media devnode at the callback (if not registered already by the v4l2 driver), after the creation of the sound devnodes.

Quoted 90 lines

Creation and register both happen in media_device_create() when it gets called during quirk handling for au0828 device. You are right that ALSA attempts to init and register media device before sound device during probe. As such, au0828 gets to this step of initializing and registering media device before snd-usb-audio probe runs.

To avoid races with au0828, the following logic is in place.

The reason for the checks if (!mdev->dev) is that au0828 could have done the media_device_init(mdev) by the time ALSA attempts media_device_create() and it could have registered the device as well. Hence, the reason for the second check if (!media_devnode_is_registered(&mdev->devnode))

Are you concerned about other races within snd-usb-audio? I can change it to do the media_device_create() step towards the end of sound probe after the snd_card_register(0 step. Please let me know your preference.

Quoted 66 lines
Quoted 66 lines
sound/usb/media.c new file +64 -0
+		if (ret) {
+			dev_err(&usbdev->dev,
+				"Couldn't register media device. Error: %d\n",
+				ret);
+			return ret;
+		}
+	}
+	return 0;
+}
+
+void media_device_delete(struct usb_interface *iface)
+{
+	struct media_device *mdev;
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+
+	mdev = media_device_find_devres(&usbdev->dev);
+	if (mdev && media_devnode_is_registered(&mdev->devnode))
+		media_device_unregister(mdev);
+}
+
+static int media_enable_source(struct media_ctl *mctl)
+{
+	if (mctl && mctl->media_dev->enable_source)
+		return mctl->media_dev->enable_source(&mctl->media_entity,
+						      &mctl->media_pipe);
+	return 0;
+}
+
+static void media_disable_source(struct media_ctl *mctl)
+{
+	if (mctl && mctl->media_dev->disable_source)
+		mctl->media_dev->disable_source(&mctl->media_entity);
+}
+
+int media_stream_init(struct snd_usb_substream *subs, struct snd_pcm *pcm,
+			int stream)
+{
+	struct media_device *mdev;
+	struct media_ctl *mctl;
+	struct device *pcm_dev = &pcm->streams[stream].dev;
+	u32 intf_type;
+	int ret = 0;
+
+	mdev = media_device_find_devres(&subs->dev->dev);
+	if (!mdev)
+		return -ENODEV;
+
+	if (subs->media_ctl)
+		return 0;
+
+	/* allocate media_ctl */
+	mctl = kzalloc(sizeof(*mctl), GFP_KERNEL);
+	if (!mctl)
+		return -ENOMEM;
+
+	mctl->media_dev = mdev;
+	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		intf_type = MEDIA_INTF_T_ALSA_PCM_PLAYBACK;
+		mctl->media_entity.function = MEDIA_ENT_F_AUDIO_PLAYBACK;
+	} else {
+		intf_type = MEDIA_INTF_T_ALSA_PCM_CAPTURE;
+		mctl->media_entity.function = MEDIA_ENT_F_AUDIO_CAPTURE;
+	}
+	mctl->media_entity.name = pcm->name;
Quoted 2 lines
Quoted 2 lines

sound/usb/media.c line 121

Quoted 2 lines
sound/usb/media.c new file +2 -0
+	mctl->media_entity.info.dev.major = MAJOR(pcm_dev->devt);
+	mctl->media_entity.info.dev.minor = MINOR(pcm_dev->devt);

Please, don't initialize major/minor at the entity level. This should be done only at interface level (and where backward compatible is needed). Those fields are obsolete!

Quoted 9 lines
Quoted 5 lines

sound/usb/media.c line 122

Quoted 1 line
sound/usb/media.c new file +1 -0
+	mctl->media_pad.flags = MEDIA_PAD_FL_SINK;  

Is it always a sink, no source, no matter which direction, playback or capture?

Yes it is always a SINK in both cases. The source is the audio decoder for both PCM capture and Playback nodes.

That doesn't sound right: the direction is relevant.

What happens is that, from Kernelspace side, when a hardware is producing data, it is a SOURCE, and when it is consuming data, it is a SINK.

The entities associated with an interface is weird, as a SINK entity is actually sending data to userspace (so, the data disappears from the Kernel), and a SOURCE entity is receiving data from userspace.

So, for SNDRV_PCM_STREAM_CAPTURE, we have a SINK, and, for SNDRV_PCM_STREAM_PLAYBACK, we have a SOURCE.

Regards,
Mauro

author
#8 lore raw
In reply to Shuah Khan
Quoted 26 lines · Shuah Khan

Please note that I am sending just the 4 patches that changed during series rebase to Linux 4.5. The rebase was pain free. media_device_init() type change required changes to patches 18 and 26. I re-tested the series and everything looks good.

[PATCH 16/31] media: au0828 video remove au0828_enable_analog_tuner() [PATCH 18/31] media: au0828 change to use Managed Media Controller API [PATCH 22/31] media: dvb-core create tuner to demod pad link in disabled state [PATCH v3 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Links:
PATCH Series that contains all 31 patches:
https://lkml.org/lkml/2016/1/6/668

ALSA patches went through reviews and links
to PATCH V3 (ALSA):
https://lkml.org/lkml/2016/1/18/556
https://lkml.org/lkml/2016/1/18/557
https://lkml.org/lkml/2016/1/18/558

Compile warns patches:
https://lkml.org/lkml/2016/1/19/520
https://lkml.org/lkml/2016/1/19/518
https://lkml.org/lkml/2016/1/19/519

Quoted 5 lines

Mauro and Takashi:
Please let me know if you would rather see the entire patch series generated for 4.5-rc1 and sent out in a complete series instead of just the rebased patches.

Hi Takashi,

Are you okay with this ALSA patch series? If so could you please Ack these patches so they can be pulled into media tree.

Mauro,

Could you please look these over and give me feedback and let me know if you would like me to resend all the patches in final series.

thanks,
-- Shuah

Signature · 5 lines
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
[email protected] | (970) 217-8978
#9 lore raw
Quoted 34 lines · Shuah Khan
Quoted 26 lines · Shuah Khan

Please note that I am sending just the 4 patches that changed during series rebase to Linux 4.5. The rebase was pain free. media_device_init() type change required changes to patches 18 and 26. I re-tested the series and everything looks good.

[PATCH 16/31] media: au0828 video remove au0828_enable_analog_tuner() [PATCH 18/31] media: au0828 change to use Managed Media Controller API [PATCH 22/31] media: dvb-core create tuner to demod pad link in disabled state [PATCH v3 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Links:
PATCH Series that contains all 31 patches:
https://lkml.org/lkml/2016/1/6/668

ALSA patches went through reviews and links
to PATCH V3 (ALSA):
https://lkml.org/lkml/2016/1/18/556
https://lkml.org/lkml/2016/1/18/557
https://lkml.org/lkml/2016/1/18/558

Compile warns patches:
https://lkml.org/lkml/2016/1/19/520
https://lkml.org/lkml/2016/1/19/518
https://lkml.org/lkml/2016/1/19/519

Quoted 5 lines

Mauro and Takashi:
Please let me know if you would rather see the entire patch series generated for 4.5-rc1 and sent out in a complete series instead of just the rebased patches.

Quoted 3 lines

Hi Takashi,

Are you okay with this ALSA patch series?

Sorry, too little time until now.
I know there are still a few potential problems. Will do detailed reviews later.

Takashi

author
#10 lore raw
Quoted 36 lines · Takashi Iwai
Quoted 34 lines · Shuah Khan
Quoted 31 lines · Shuah Khan

Please note that I am sending just the 4 patches that changed during series rebase to Linux 4.5. The rebase was pain free. media_device_init() type change required changes to patches 18 and 26. I re-tested the series and everything looks good.

[PATCH 16/31] media: au0828 video remove au0828_enable_analog_tuner() [PATCH 18/31] media: au0828 change to use Managed Media Controller API [PATCH 22/31] media: dvb-core create tuner to demod pad link in disabled state [PATCH v3 26/31] sound/usb: Update ALSA driver to use Managed Media Controller API

Links:
PATCH Series that contains all 31 patches:
https://lkml.org/lkml/2016/1/6/668

ALSA patches went through reviews and links
to PATCH V3 (ALSA):
https://lkml.org/lkml/2016/1/18/556
https://lkml.org/lkml/2016/1/18/557
https://lkml.org/lkml/2016/1/18/558

Compile warns patches:
https://lkml.org/lkml/2016/1/19/520
https://lkml.org/lkml/2016/1/19/518
https://lkml.org/lkml/2016/1/19/519

Mauro and Takashi:
Please let me know if you would rather see the entire patch series generated for 4.5-rc1 and sent out in a complete series instead of just the rebased patches.

Quoted 8 lines
Quoted 3 lines

Hi Takashi,

Are you okay with this ALSA patch series?

Sorry, too little time until now.
I know there are still a few potential problems. Will do detailed reviews later.

I would like to wrap this up and hopefully get this into 4.6. Please let me know if you will be able to review in time for that.

I have a few comments work on from Mauro and would like to address comments from you as well at the same time.

thanks,
-- Shuah

Signature · 5 lines
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
[email protected] | (970) 217-8978