linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] i2c: Split adapter initialisation from registers
@ 2016-06-09  8:53 Chris Wilson
  2016-06-09  8:53 ` [PATCH 2/3] i2c: Mark adapter as initialised Chris Wilson
  2016-06-09  8:53 ` [PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation Chris Wilson
  0 siblings, 2 replies; 4+ messages in thread
From: Chris Wilson @ 2016-06-09  8:53 UTC (permalink / raw)
  To: linux-i2c
  Cc: Chris Wilson, Ville Syrjälä,
	Wolfram Sang, linux-kernel, dri-devel

In order to handle devices that need to use the adapter prior to it
being registered with the system, we first need to split out the
initialisation of the adapter out of i2c_register_adapter.

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 | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index af11b658984d..743c38a63da1 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1511,16 +1511,8 @@ static int __process_new_adapter(struct device_driver *d, void *data)
 	return i2c_do_add_adapter(to_i2c_driver(d), data);
 }
 
-static int i2c_register_adapter(struct i2c_adapter *adap)
+static int i2c_init_adapter(struct i2c_adapter *adap)
 {
-	int res = 0;
-
-	/* Can't register until after driver model init */
-	if (WARN_ON(!is_registered)) {
-		res = -EAGAIN;
-		goto out_list;
-	}
-
 	/* Sanity checks */
 	if (unlikely(adap->name[0] == '\0')) {
 		pr_err("i2c-core: Attempt to register an adapter with "
@@ -1548,6 +1540,23 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	if (adap->timeout == 0)
 		adap->timeout = HZ;
 
+	return 0;
+}
+
+static int i2c_register_adapter(struct i2c_adapter *adap)
+{
+	int res = 0;
+
+	/* Can't register until after driver model init */
+	if (WARN_ON(!is_registered)) {
+		res = -EAGAIN;
+		goto out_list;
+	}
+
+	res = i2c_init_adapter(adap);
+	if (res)
+		goto out_list;
+
 	dev_set_name(&adap->dev, "i2c-%d", adap->nr);
 	adap->dev.bus = &i2c_bus_type;
 	adap->dev.type = &i2c_adapter_type;
-- 
2.8.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] i2c: Mark adapter as initialised
  2016-06-09  8:53 [PATCH 1/3] i2c: Split adapter initialisation from registers Chris Wilson
@ 2016-06-09  8:53 ` Chris Wilson
  2016-06-09  8:53 ` [PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation Chris Wilson
  1 sibling, 0 replies; 4+ messages in thread
From: Chris Wilson @ 2016-06-09  8:53 UTC (permalink / raw)
  To: linux-i2c
  Cc: Chris Wilson, Ville Syrjälä,
	Wolfram Sang, linux-kernel, dri-devel

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(+)

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;
 }
 
-- 
2.8.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation
  2016-06-09  8:53 [PATCH 1/3] i2c: Split adapter initialisation from registers Chris Wilson
  2016-06-09  8:53 ` [PATCH 2/3] i2c: Mark adapter as initialised Chris Wilson
@ 2016-06-09  8:53 ` Chris Wilson
  2016-08-23 21:33   ` Wolfram Sang
  1 sibling, 1 reply; 4+ messages in thread
From: Chris Wilson @ 2016-06-09  8:53 UTC (permalink / raw)
  To: linux-i2c
  Cc: Chris Wilson, Ville Syrjälä,
	Wolfram Sang, linux-kernel, dri-devel

Some drivers require use of their i2c adapter long before they can add
the adapter to userspace (i.e. before they establish and expose their
objects in the sysfs kobject tree). Currently i2c_add_adapter()
registers the adapter with userspace, which causes a conflict in the
ordering of the driver's initialisation. Exporting i2c_init_adapter()
allows for the i2c adapter to be used early in the initialisation in
order to detect whether the device the adapter is attached to is even
present. The drivers still need to call i2c_add_adapter() (or
i2c_add_number_adapter()) as per normal to complete their registration.

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 | 16 +++++++++++++++-
 include/linux/i2c.h    |  1 +
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 91ff70d31ec8..0a3bdb048d65 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1511,7 +1511,20 @@ static int __process_new_adapter(struct device_driver *d, void *data)
 	return i2c_do_add_adapter(to_i2c_driver(d), data);
 }
 
-static int i2c_init_adapter(struct i2c_adapter *adap)
+/**
+ * i2c_init_adapter - initialise i2c adapter for internal use
+ * @adapter: the adapter to initialise
+ * Context: any
+ *
+ * This routine is used to initialise an I2C adapter for internal use
+ * prior to registering it with third parties (including userspace). The
+ * driver should still call i2c_add_adapter() or i2c_add_numbered_adapter()
+ * when it is ready to expose the adapter to userspace.
+ *
+ * Returns: 0 on success, or a negative error value if the adapter is not
+ * fully specified for use.
+ */
+int i2c_init_adapter(struct i2c_adapter *adap)
 {
 	/* Only initialise the adapter once. */
 	if (list_empty(&adap->userspace_clients))
@@ -1546,6 +1559,7 @@ static int i2c_init_adapter(struct i2c_adapter *adap)
 
 	return 0;
 }
+EXPORT_SYMBOL(i2c_init_adapter);
 
 static int i2c_register_adapter(struct i2c_adapter *adap)
 {
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 96a25ae14494..72c76c5efba5 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -639,6 +639,7 @@ i2c_unlock_adapter(struct i2c_adapter *adapter)
 /* administration...
  */
 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
+extern int i2c_init_adapter(struct i2c_adapter *);
 extern int i2c_add_adapter(struct i2c_adapter *);
 extern void i2c_del_adapter(struct i2c_adapter *);
 extern int i2c_add_numbered_adapter(struct i2c_adapter *);
-- 
2.8.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation
  2016-06-09  8:53 ` [PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation Chris Wilson
@ 2016-08-23 21:33   ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2016-08-23 21:33 UTC (permalink / raw)
  To: Chris Wilson; +Cc: linux-i2c, Ville Syrjälä, linux-kernel, dri-devel

[-- Attachment #1: Type: text/plain, Size: 1044 bytes --]

On Thu, Jun 09, 2016 at 09:53:55AM +0100, Chris Wilson wrote:
> Some drivers require use of their i2c adapter long before they can add
> the adapter to userspace (i.e. before they establish and expose their
> objects in the sysfs kobject tree). Currently i2c_add_adapter()
> registers the adapter with userspace, which causes a conflict in the
> ordering of the driver's initialisation. Exporting i2c_init_adapter()
> allows for the i2c adapter to be used early in the initialisation in
> order to detect whether the device the adapter is attached to is even
> present. The drivers still need to call i2c_add_adapter() (or
> i2c_add_number_adapter()) as per normal to complete their registration.
> 
> 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

Do you have a patch for some i2c driver making use of this change?


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-08-23 21:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-09  8:53 [PATCH 1/3] i2c: Split adapter initialisation from registers Chris Wilson
2016-06-09  8:53 ` [PATCH 2/3] i2c: Mark adapter as initialised Chris Wilson
2016-06-09  8:53 ` [PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation Chris Wilson
2016-08-23 21:33   ` Wolfram Sang

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).