diff mbox series

[1/2] media: add generic device allocate interface to media-dev-allocator

Message ID f4b11067f523b5727b9a44cf415ce265deffdfe0.1558667245.git.skhan@linuxfoundation.org (mailing list archive)
State New, archived
Headers show
Series Use Media Dev Allocator to fix vimc dev lifetime bugs | expand

Commit Message

Shuah Khan May 24, 2019, 3:31 a.m. UTC
Media Device Allocator API supports just USB devices. Enhance it
adding a genetic device allocate interface to support other media
drivers.

The new interface takes pointer to struct device instead and creates
media device. This interface allows a group of drivers that have a
common root device to share media device resource and ensure media
device doesn't get deleted as long as one of the drivers holds its
reference.

The new interface has been tested with vimc component driver to fix
panics when vimc module is removed while streaming is in progress.

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 drivers/media/Makefile              |  4 +--
 drivers/media/media-dev-allocator.c | 39 ++++++++++++++++++++++++
 include/media/media-dev-allocator.h | 46 +++++++++++++++++++++++++----
 3 files changed, 80 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/drivers/media/Makefile b/drivers/media/Makefile
index 4a330d0e5e40..3a4f7f74301d 100644
--- a/drivers/media/Makefile
+++ b/drivers/media/Makefile
@@ -7,9 +7,7 @@  media-objs	:= media-device.o media-devnode.o media-entity.o \
 		   media-request.o
 
 ifeq ($(CONFIG_MEDIA_CONTROLLER),y)
-	ifeq ($(CONFIG_USB),y)
-		media-objs += media-dev-allocator.o
-	endif
+	media-objs += media-dev-allocator.o
 endif
 
 #
diff --git a/drivers/media/media-dev-allocator.c b/drivers/media/media-dev-allocator.c
index ae17887dec59..4078e098cede 100644
--- a/drivers/media/media-dev-allocator.c
+++ b/drivers/media/media-dev-allocator.c
@@ -94,6 +94,7 @@  static struct media_device *__media_device_get(struct device *dev,
 	return &mdi->mdev;
 }
 
+#if IS_ENABLED(CONFIG_USB)
 struct media_device *media_device_usb_allocate(struct usb_device *udev,
 					       const char *module_name,
 					       struct module *owner)
@@ -115,6 +116,44 @@  struct media_device *media_device_usb_allocate(struct usb_device *udev,
 	return mdev;
 }
 EXPORT_SYMBOL_GPL(media_device_usb_allocate);
+#endif
+
+struct media_device *media_device_allocate(struct device *dev,
+					   const char *model,
+					   const char *bus_info,
+					   const char *module_name,
+					   struct module *owner)
+{
+	struct media_device *mdev;
+
+	mutex_lock(&media_device_lock);
+	mdev = __media_device_get(dev, module_name, owner);
+	if (!mdev) {
+		mutex_unlock(&media_device_lock);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	if (!mdev->dev) {
+		/* Initialize media device */
+		if (model)
+			strscpy(mdev->model, model, sizeof(mdev->model));
+		else
+			strscpy(mdev->model, "Unkonw model",
+				sizeof(mdev->model));
+		if (bus_info)
+			strscpy(mdev->bus_info, bus_info,
+				sizeof(mdev->bus_info));
+		else
+			strscpy(mdev->bus_info, "Unknown bus_info",
+				sizeof(mdev->bus_info));
+		mdev->dev = dev;
+		media_device_init(mdev);
+	}
+
+	mutex_unlock(&media_device_lock);
+	return mdev;
+}
+EXPORT_SYMBOL_GPL(media_device_allocate);
 
 void media_device_delete(struct media_device *mdev, const char *module_name,
 			 struct module *owner)
diff --git a/include/media/media-dev-allocator.h b/include/media/media-dev-allocator.h
index b35ea6062596..479a3c52cf89 100644
--- a/include/media/media-dev-allocator.h
+++ b/include/media/media-dev-allocator.h
@@ -19,7 +19,8 @@ 
 
 struct usb_device;
 
-#if defined(CONFIG_MEDIA_CONTROLLER) && defined(CONFIG_USB)
+#if defined(CONFIG_MEDIA_CONTROLLER)
+#if defined(CONFIG_USB)
 /**
  * media_device_usb_allocate() - Allocate and return struct &media device
  *
@@ -38,6 +39,36 @@  struct usb_device;
 struct media_device *media_device_usb_allocate(struct usb_device *udev,
 					       const char *module_name,
 					       struct module *owner);
+#else
+static inline struct media_device *media_device_usb_allocate(
+			struct usb_device *udev, const char *module_name,
+			struct module *owner)
+			{ return NULL; }
+#endif /* CONFIG_USB */
+/**
+ * media_device_allocate() - Allocate and return struct &media device
+ *
+ * @udev:		struct &device pointer
+ * @model:		should be filled with device model name
+ * @bus_info:		should be filled with device bus information:
+ *			Unique and stable device location identifier
+ *			as defined in struct media_device
+ * @module_name:	should be filled with %KBUILD_MODNAME
+ * @owner:		struct module pointer %THIS_MODULE for the driver.
+ *			%THIS_MODULE is null for a built-in driver.
+ *			It is safe even when %THIS_MODULE is null.
+ *
+ * This interface should be called to allocate a Media Device when multiple
+ * drivers/sub-drivers share device and the media device. This interface
+ * allocates &media_device structure and calls media_device_init() to
+ * initialize it.
+ *
+ */
+struct media_device *media_device_allocate(struct device *dev,
+					   const char *model,
+					   const char *bus_info,
+					   const char *module_name,
+					   struct module *owner);
 /**
  * media_device_delete() - Release media device. Calls kref_put().
  *
@@ -52,12 +83,15 @@  struct media_device *media_device_usb_allocate(struct usb_device *udev,
 void media_device_delete(struct media_device *mdev, const char *module_name,
 			 struct module *owner);
 #else
-static inline struct media_device *media_device_usb_allocate(
-			struct usb_device *udev, const char *module_name,
-			struct module *owner)
-			{ return NULL; }
+static inline struct media_device *media_device_allocate(
+					struct device *dev,
+					const char *model,
+					const char *bus_info,
+					const char *module_name,
+					struct module *owner)
+					{ return NULL; }
 static inline void media_device_delete(
 			struct media_device *mdev, const char *module_name,
 			struct module *owner) { }
-#endif /* CONFIG_MEDIA_CONTROLLER && CONFIG_USB */
+#endif /* CONFIG_MEDIA_CONTROLLER */
 #endif /* _MEDIA_DEV_ALLOCATOR_H */