Media Device Allocator API
Subsystems (4)
v7 cover Media Device Allocator API
Media Device Allocator API to allows multiple drivers share a media device. Using this API, drivers can allocate a media device with the shared struct device as the key. Once the media device is allocated by a driver, other drivers can get a reference to it. The media device is released when all the references are released.
Patches 0001 and 0002 are rebased to 4.9-rc7. Patch 0003 for snd-usb-audio is a rebase of the patch that was tested with the original Media Device Allocator patch series.
snd-usb-audio patch includes the fixes found during 4.7-rc1 time in the original snd-usb-audio patch.
Changes to patches in this series:
Changes to patch 0001 since v6:
- No changes
Changes to patch 0002 since v6:
- No changes
Changes to patch 0003 since v6:
- Addressed Takashi's review comments on patch v6
Depends on:
https://lkml.org/lkml/2016/11/29/1001
Changes to patch 0001 since v5: (comments from Mauro and Sakari)
- Removed struct device from media_device_instance. mdev.dev is used instead.
- Added documentation.
Changes to patch 0002:
- No changes since patch v2, applies cleanly on top of the following:
media: Protect enable_source and disable_source handler code paths
https://lkml.org/lkml/2016/11/29/1001
Changes to patch 0003:
- Changed to hold graph_mutex to check and call enable_source and disable_source handlers - to match au0828 doing the same in:
media: Protect enable_source and disable_source handler code paths
https://lkml.org/lkml/2016/11/29/1001
Changes to patch 0001 since v4:
- Addressed Sakari's review comments with the exception of opting to not introduce media_device_usb_allocate() macro, and to not add a new routine to find media device instance to avoid a one line check.
Changes to patch 0001 since v3:
- Fixed undefined reference to `__media_device_usb_init compile error when CONFIG_USB is disabled.
- Fixed kernel paging error when accessing /dev/mediaX after rmmod of the module that owns the media_device. The fix bumps the reference count for the owner when second driver comes along to share the media_device. If au0828 owns the media_device, then snd_usb_audio will bump the refcount for au0828, so it won't get deleted and vice versa.
Changes to patch 0002 since v2:
- Updated media_device_delete() to pass in module name.
Changes to patch 0003 since the last version in 4.7-rc1:
- Included fixes to bugs found during testing.
- Updated to use the Media Allocator API.
This patch series has been tested with au0828 and snd-usb-audio drivers. Ran bind and unbind loop tests on each driver with mc_nextgen_test and media_device_test app loop tests while checking lsmod and dmesg.
Please refer to tools/testing/selftests/media_tests/regression_test.txt for testing done on this series.
Shuah Khan (3):
media: Media Device Allocator API
media: change au0828 to use Media Device Allocator API sound/usb: Use Media Controller API to share media resources
Documentation/media/kapi/mc-core.rst | 37 ++++ drivers/media/Makefile | 3 +- drivers/media/media-dev-allocator.c | 133 ++++++++++++++ drivers/media/usb/au0828/au0828-core.c | 12 +- drivers/media/usb/au0828/au0828.h | 1 + include/media/media-dev-allocator.h | 54 ++++++ sound/usb/Kconfig | 4 + sound/usb/Makefile | 2 + sound/usb/card.c | 14 ++ sound/usb/card.h | 3 + sound/usb/media.c | 321 +++++++++++++++++++++++++++++++++ sound/usb/media.h | 73 ++++++++ sound/usb/mixer.h | 3 + sound/usb/pcm.c | 29 ++- sound/usb/quirks-table.h | 1 + sound/usb/stream.c | 2 + sound/usb/usbaudio.h | 6 + 17 files changed, 684 insertions(+), 14 deletions(-) create mode 100644 drivers/media/media-dev-allocator.c create mode 100644 include/media/media-dev-allocator.h create mode 100644 sound/usb/media.c create mode 100644 sound/usb/media.h
Signature · 1 line
2.7.4
PATCH v7 1/3 media: Media Device Allocator API
Media Device Allocator API to allows multiple drivers share a media device. Using this API, drivers can allocate a media device with the shared struct device as the key. Once the media device is allocated by a driver, other drivers can get a reference to it. The media device is released when all the references are released.
---
No changes since v6
Documentation/media/kapi/mc-core.rst | 37 ++++++++++ drivers/media/Makefile | 3 +- drivers/media/media-dev-allocator.c | 133 +++++++++++++++++++++++++++++++++++ include/media/media-dev-allocator.h | 54 ++++++++++++++ 4 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 drivers/media/media-dev-allocator.c create mode 100644 include/media/media-dev-allocator.h
Documentation/media/kapi/mc-core.rst +37 -0
@@ -257,8 +257,45 @@ Subsystems should facilitate link validation by providing subsystem specific helper functions to provide easy access for commonly needed information, and in the end provide a way to use driver-specific callbacks. +Media Controller Device Allocator API +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When media device belongs to more than one driver, the shared media device +is allocated with the shared struct device as the key for look ups. + +Shared media device should stay in registered state until the last driver +unregisters it. In addition, media device should be released when all the +references are released. Each driver gets a reference to the media device +during probe, when it allocates the media device. If media device is already +allocated, allocate API bumps up the refcount and return the existing media +device. Driver puts the reference back from its disconnect routine when it +calls :c:func:`media_device_delete()`. + +Media device is unregistered and cleaned up from the kref put handler to +ensure that the media device stays in registered state until the last driver +unregisters the media device. + +**Driver Usage** + +Drivers should use the media-core routines to get register reference and +call :c:func:`media_device_delete()` routine to make sure the shared media +device delete is handled correctly. + +**driver probe:** +Call :c:func:`media_device_usb_allocate()` to allocate or get a reference +Call :c:func:`media_device_register()`, if media devnode isn't registered + +**driver disconnect:** +Call :c:func:`media_device_delete()` to free the media_device. Free'ing is +handled by the kref put handler. + +API Definitions +^^^^^^^^^^^^^^^ + .. kernel-doc:: include/media/media-device.h .. kernel-doc:: include/media/media-devnode.h .. kernel-doc:: include/media/media-entity.h + +.. kernel-doc:: include/media/media-dev-allocator.h
drivers/media/Makefile +2 -1
@@ -6,7 +6,8 @@ ifeq ($(CONFIG_MEDIA_CEC_EDID),y) obj-$(CONFIG_MEDIA_SUPPORT) += cec-edid.o endif -media-objs := media-device.o media-devnode.o media-entity.o +media-objs := media-device.o media-devnode.o media-entity.o \ + media-dev-allocator.o # # I2C drivers should come before other drivers, otherwise they'll fail
drivers/media/media-dev-allocator.c new file +133 -0
133 lines not on this page · raw message
include/media/media-dev-allocator.h new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v7 2/3 media: change au0828 to use Media Device Allocator API
In reply to Shuah KhanChange au0828 to use Media Device Allocator API to allocate media device with the parent usb struct device as the key, so it can be shared with the snd_usb_audio driver.
---
No changes since v6
drivers/media/usb/au0828/au0828-core.c | 12 ++++-------- drivers/media/usb/au0828/au0828.h | 1 + 2 files changed, 5 insertions(+), 8 deletions(-)
drivers/media/usb/au0828/au0828-core.c +4 -8
@@ -159,9 +159,7 @@ static void au0828_unregister_media_device(struct au0828_dev *dev) dev->media_dev->disable_source =NULL ;mutex_unlock (&mdev->graph_mutex); -media_device_unregister (dev->media_dev); -media_device_cleanup (dev->media_dev); -kfree (dev->media_dev); +media_device_delete (dev->media_dev,KBUILD_MODNAME ); dev->media_dev =NULL ;#endif }
@@ -214,14 +212,10 @@ 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_usb_allocate (udev,KBUILD_MODNAME );if (!mdev)return -ENOMEM ; -/* check if media device is already initialized */ -if (!mdev->dev) -media_device_usb_init (mdev, udev, udev->product); - dev->media_dev = mdev;#endif return 0 ;
@@ -482,6 +476,8 @@ static int au0828_media_device_register(struct au0828_dev *dev,/* register media device */ ret =media_device_register (dev->media_dev);if (ret) { +media_device_delete (dev->media_dev,KBUILD_MODNAME ); + dev->media_dev =NULL ;dev_err (&udev->dev,"Media Device Register Error: %d\n" , ret);return ret;
drivers/media/usb/au0828/au0828.h +1 -0
@@ -35,6 +35,7 @@#include <media/v4l2-ctrls.h> #include <media/v4l2-fh.h> #include <media/media-device.h> +#include <media/media-dev-allocator.h> /* DVB */ #include "demux.h"
Signature · 1 line
2.7.4
PATCH v7 3/3 sound/usb: Use Media Controller API to share media resources
In reply to Shuah KhanChange ALSA driver to use Media Controller API to share media resources with DVB, and V4L2 drivers on a AU0828 media device.
Media Controller specific initialization is done after sound card is registered. ALSA creates Media interface and entity function graph nodes for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler interface to request the media resource. If resource request is granted, it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is returned.
Media specific cleanup is done in usb_audio_disconnect().
---
Changes to patch 0003 since v6:
- Addressed Takashi's review comments on patch v6
sound/usb/Kconfig | 4 + sound/usb/Makefile | 2 + sound/usb/card.c | 14 +++ sound/usb/card.h | 3 + sound/usb/media.c | 321 +++++++++++++++++++++++++++++++++++++++++++++++ sound/usb/media.h | 73 +++++++++++ sound/usb/mixer.h | 3 + sound/usb/pcm.c | 29 ++++- sound/usb/quirks-table.h | 1 + sound/usb/stream.c | 2 + sound/usb/usbaudio.h | 6 + 11 files changed, 453 insertions(+), 5 deletions(-) create mode 100644 sound/usb/media.c create mode 100644 sound/usb/media.h
sound/usb/Kconfig +4 -0
@@ -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 && (MEDIA_SUPPORT=y || MEDIA_SUPPORT=SND_USB_AUDIO) 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
@@ -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 +14 -0
@@ -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" );
@@ -616,6 +617,11 @@ static int usb_audio_probe(struct usb_interface *intf,if (err <0 ) goto __error; +if (quirk && quirk->media_device) { +/* don't want to fail when media_snd_device_create() fails */ +media_snd_device_create (chip, intf); + } + usb_chip[chip->index] = chip; chip->num_interfaces++;usb_set_intfdata (intf, chip);
@@ -672,6 +678,14 @@ static void usb_audio_disconnect(struct usb_interface *intf)list_for_each (p, &chip->midi_list) {snd_usbmidi_disconnect (p); } +/* +* Nice to check quirk && quirk->media_device and +* then call media_snd_device_delete(). Don't have +* access to quirk here. media_snd_device_delete() +* acceses mixer_list +*/ +media_snd_device_delete (chip); +/* release mixer resources */ list_for_each_entry (mixer, &chip->mixer_list, list) {snd_usb_mixer_disconnect (mixer);
sound/usb/card.h +3 -0
@@ -105,6 +105,8 @@ struct snd_usb_endpoint {struct list_head list; }; +struct media_ctl ; +struct snd_usb_substream {struct snd_usb_stream *stream;struct usb_device *dev;
@@ -156,6 +158,7 @@ struct snd_usb_substream { } dsd_dop;bool trigger_tstamp_pending_update;/* trigger timestamp being updated from initial estimate */ +struct media_ctl *media_ctl; };struct snd_usb_stream {
sound/usb/media.c new file +321 -0
321 lines not on this page · raw message
sound/usb/media.h new file +73 -0
73 lines not on this page · raw message
sound/usb/mixer.h +3 -0
@@ -3,6 +3,8 @@#include <sound/info.h> +struct media_mixer_ctl ; +struct usb_mixer_interface {struct snd_usb_audio *chip;struct usb_host_interface *hostif;
@@ -22,6 +24,7 @@ struct usb_mixer_interface {struct urb *rc_urb;struct usb_ctrlrequest *rc_setup_packet;u8 rc_buffer[6 ]; +struct media_mixer_ctl *media_mixer_ctl; };#define MAX_CHANNELS 16/* max logical channels */
sound/usb/pcm.c +24 -5
@@ -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
@@ -717,10 +718,14 @@ static int snd_usb_hw_params(struct snd_pcm_substream *substream,struct audioformat *fmt;int ret; + ret =media_snd_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);
@@ -734,22 +739,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_snd_stop_pipeline (subs); +return ret; }/*
@@ -761,6 +771,7 @@ static int snd_usb_hw_free(struct snd_pcm_substream *substream) {struct snd_usb_substream *subs = substream->runtime->private_data; +media_snd_stop_pipeline (subs); subs->cur_audiofmt =NULL ; subs->cur_rate =0 ; subs->period_bytes =0 ;
@@ -1221,6 +1232,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 ;
@@ -1234,7 +1246,13 @@ 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_snd_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)
@@ -1243,6 +1261,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_snd_stop_pipeline (subs);if (subs->interface >=0 && !snd_usb_lock_shutdown (subs->stream->chip)) {
sound/usb/quirks-table.h +1 -0
@@ -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/stream.c +2 -0
@@ -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_snd_stream_delete (subs); }
sound/usb/usbaudio.h +6 -0
@@ -30,6 +30,9 @@* */ +struct media_device ; +struct media_intf_devnode ; +struct snd_usb_audio {int index;struct usb_device *dev;
@@ -61,6 +64,8 @@ struct snd_usb_audio {bool autoclock;/* from the 'autoclock' module param */ struct usb_host_interface *ctrl_intf;/* the audio control interface */ +struct media_device *media_dev; +struct media_intf_devnode *ctl_intf_media_devnode; }; #define usb_audio_err(chip, fmt, args...) \
@@ -111,6 +116,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.7.4