[2/3] i2c: Mark adapter as initialised
diff mbox

Message ID 1465462435-5240-2-git-send-email-chris@chris-wilson.co.uk
State New
Headers show

Commit Message

Chris Wilson June 9, 2016, 8:53 a.m. UTC
In order to allow drivers to call i2c_init_adapter as a separate step,
and to allow other drivers to continue skipping the call, mark the
adapter as initialised upon i2c_init_adapter() and ignore multiple
calls.

The choice of how to introduce an "already-initialised" check into the
existing i2c_add_adapter() callers is tricky as we cannot make too many
assumptions about the state of memory, i.e. whether the struct was
cleared before being passed to i2c_add_adapter. This poses an issue if
we wanted to add a boolean flag to mark when the adapter is initialised
(as that flag may be set due to previous contents of memory). Instead,
we opt to use the userspace_client_lists as that should be empty from
initialisation through to registration - but will be reported as !empty
if it has either random junk or zeroes (with a very small chance that we
fail to register an adapter that just happens to have a prior empty list
at that location in memory).

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>
Cc: Wolfram Sang <wsa@the-dreams.de>
Cc: linux-i2c@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
---
 drivers/i2c/i2c-core.c | 7 +++++++
 1 file changed, 7 insertions(+)

Patch
diff mbox

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 743c38a63da1..91ff70d31ec8 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1513,6 +1513,10 @@  static int __process_new_adapter(struct device_driver *d, void *data)
 
 static int i2c_init_adapter(struct i2c_adapter *adap)
 {
+	/* Only initialise the adapter once. */
+	if (list_empty(&adap->userspace_clients))
+		return 0;
+
 	/* Sanity checks */
 	if (unlikely(adap->name[0] == '\0')) {
 		pr_err("i2c-core: Attempt to register an adapter with "
@@ -1632,6 +1636,9 @@  out_list:
 	mutex_lock(&core_lock);
 	idr_remove(&i2c_adapter_idr, adap->nr);
 	mutex_unlock(&core_lock);
+
+	/* Force initialisation if this struct gets reused */
+	memset(&adap->userspace_clients, 0, sizeof(adap->userspace_clients));
 	return res;
 }