linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: linux-media@vger.kernel.org
Cc: mchehab@osg.samsung.com, shuahkh@osg.samsung.com,
	laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl,
	Sakari Ailus <sakari.ailus@iki.fi>
Subject: [RFC 06/16] media: Dynamically allocate the media device
Date: Fri, 15 Jul 2016 01:35:01 +0300	[thread overview]
Message-ID: <1468535711-13836-7-git-send-email-sakari.ailus@linux.intel.com> (raw)
In-Reply-To: <1468535711-13836-1-git-send-email-sakari.ailus@linux.intel.com>

From: Sakari Ailus <sakari.ailus@iki.fi>

Allow allocating the media device dynamically. As the struct media_device
embeds struct media_devnode, the lifetime of that object is that same than
that of the media_device.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/media-device.c | 28 ++++++++++++++++++++++++++--
 include/media/media-device.h | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/drivers/media/media-device.c b/drivers/media/media-device.c
index a11b3be..1bab2c6 100644
--- a/drivers/media/media-device.c
+++ b/drivers/media/media-device.c
@@ -543,9 +543,18 @@ static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  * Registration/unregistration
  */
 
-static void media_device_release(struct media_devnode *mdev)
+static void media_device_release(struct media_devnode *devnode)
 {
-	dev_dbg(mdev->parent, "Media device released\n");
+	struct media_device *mdev = to_media_device(devnode);
+
+	ida_destroy(&mdev->entity_internal_idx);
+	mdev->entity_internal_idx_max = 0;
+	media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
+	mutex_destroy(&mdev->graph_mutex);
+	dev_dbg(devnode->parent, "Media device released\n");
+
+	if (devnode->use_kref)
+		kfree(mdev);
 }
 
 /**
@@ -692,6 +701,21 @@ void media_device_init(struct media_device *mdev)
 }
 EXPORT_SYMBOL_GPL(media_device_init);
 
+struct media_device *media_device_alloc(void)
+{
+	struct media_device *mdev;
+
+	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+	if (!mdev)
+		return NULL;
+
+	media_devnode_init(&mdev->devnode);
+	media_device_init(mdev);
+
+	return mdev;
+}
+EXPORT_SYMBOL_GPL(media_device_alloc);
+
 void media_device_cleanup(struct media_device *mdev)
 {
 	ida_destroy(&mdev->entity_internal_idx);
diff --git a/include/media/media-device.h b/include/media/media-device.h
index a9b33c4..cb5722b 100644
--- a/include/media/media-device.h
+++ b/include/media/media-device.h
@@ -428,6 +428,42 @@ static inline __must_check int media_entity_enum_init(
 void media_device_init(struct media_device *mdev);
 
 /**
+ * media_device_alloc() - Allocate and initialise a media device
+ *
+ * Allocate and initialise a media device. Returns a media device.
+ * The media device is refcounted, and this function returns a media
+ * device the refcount of which is one (1).
+ *
+ * References are taken and given using media_device_get() and
+ * media_device_put().
+ */
+struct media_device *media_device_alloc(void);
+
+/**
+ * media_device_get() - Get a reference to a media device
+ *
+ * mdev: media device
+ */
+#define media_device_get(mdev)						\
+	do {								\
+		dev_dbg((mdev)->dev, "%s: get media device %s\n",	\
+			__func__, (mdev)->bus_info);			\
+		media_devnode_get(&(mdev)->devnode);			\
+	} while (0)
+
+/**
+ * media_device_put() - Put a reference to a media device
+ *
+ * mdev: media device
+ */
+#define media_device_put(mdev)						\
+	do {								\
+		dev_dbg((mdev)->dev, "%s: put media device %s\n",	\
+			__func__, (mdev)->bus_info);			\
+		media_devnode_put(&(mdev)->devnode);			\
+	} while (0)
+
+/**
  * media_device_cleanup() - Cleanups a media device element
  *
  * @mdev:	pointer to struct &media_device
@@ -654,6 +690,8 @@ void __media_device_usb_init(struct media_device *mdev,
 			     const char *driver_name);
 
 #else
+#define media_device_get(mdev) do { } while (0)
+#define media_device_put(mdev) do { } while (0)
 static inline int media_device_register(struct media_device *mdev)
 {
 	return 0;
-- 
2.1.4


  parent reply	other threads:[~2016-07-14 22:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-14 22:34 [RFC 00/16] Make use of kref in media device, grab references as needed Sakari Ailus
2016-07-14 22:34 ` [RFC 01/16] Revert "[media] media: fix media devnode ioctl/syscall and unregister race" Sakari Ailus
2016-07-14 22:34 ` [RFC 02/16] Revert "[media] media: fix use-after-free in cdev_put() when app exits after driver unbind" Sakari Ailus
2016-07-14 22:34 ` [RFC 03/16] Revert "[media] media-device: dynamically allocate struct media_devnode" Sakari Ailus
2016-07-14 22:34 ` [RFC 04/16] media: Remove useless curly braces and parentheses Sakari Ailus
2016-07-14 22:35 ` [RFC 05/16] media: devnode: Refcount the media devnode Sakari Ailus
2016-08-02 12:12   ` Hans Verkuil
2016-08-05 10:59     ` Sakari Ailus
2016-07-14 22:35 ` Sakari Ailus [this message]
2016-07-14 22:35 ` [RFC 07/16] media-device: struct media_device requires struct device Sakari Ailus
2016-07-14 22:35 ` [RFC 08/16] media: Provide a way to the driver to set a private pointer Sakari Ailus
2016-07-14 22:35 ` [RFC 09/16] media: Add release callback for media device Sakari Ailus
2016-07-14 22:35 ` [RFC 10/16] media: Document media device allocation API Sakari Ailus
2016-07-14 22:35 ` [RFC 11/16] v4l: Acquire a reference to the media device for every video device Sakari Ailus
2016-07-14 22:35 ` [RFC 12/16] media: Shuffle functions around Sakari Ailus
2016-07-14 22:35 ` [RFC 13/16] media-device: Postpone graph object removal until free Sakari Ailus
2016-07-14 22:35 ` [RFC 14/16] omap3isp: Allocate the media device dynamically Sakari Ailus
2016-07-14 22:35 ` [RFC 15/16] omap3isp: Release the isp device struct by media device callback Sakari Ailus
2016-07-14 22:35 ` [RFC 16/16] omap3isp: Don't rely on devm for memory resource management Sakari Ailus
2016-07-15 10:19 ` [RFC 00/16] Make use of kref in media device, grab references as needed Mauro Carvalho Chehab
2016-07-19  7:27   ` Sakari Ailus
2016-07-19  7:52     ` Sakari Ailus
2016-07-19 11:50     ` Mauro Carvalho Chehab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1468535711-13836-7-git-send-email-sakari.ailus@linux.intel.com \
    --to=sakari.ailus@linux.intel.com \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@osg.samsung.com \
    --cc=sakari.ailus@iki.fi \
    --cc=shuahkh@osg.samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).