i2c: allow i2c core to instantiate
PATCH 1/3 i2c: allow i2c core to instantiate
Hi Nathan:
OK, I have finished reviewing this patch in detail. Note that some of the comments are just me testing my understanding.
Summary: the biggest immediate problem here is that i2c_add_client() leaks a struct i2c_client by design, AFAICT.
I do see that this approach addresses a requirement of embedded systems and others who know a priori what hardware is present. It is also nice that it does not force any changes to current drivers.
However, I think the i2c_add_client() function is awkward. You say that this function could be called by an adapter that knows what devices it has... but what if the adapter is a generic driver like i2c-i801? For the embedded scenario, it would be preferable to further decouple these two pieces:
[ adapter driver for bus foo ]
[ bus foo has devices x, y & z ]
On the other hand, I'm not sure how to do that right now; it will take more study on my part.
Nonetheless, I'm not completely opposed to merging (something like) this patch as an interim solution if it will lead to concrete improvements in the behavior of some v4l drivers. I would like to see at least one example of that first. I don't think the v4l drivers that you did convert in patch #2 qualify, though they do demonstrate that the conversion itself is not difficult.
I would argue against a general move to this method for hwmon and others. There would be no benefit that I can tell; in the long term I would expect the i2c_add_client() function to disappear anyway.
Finally, the patch itself...
Quoted 14 lines · Nathan Lutchansky
Add a new function
i2c_add_client()which can instantiate and bind i2c clients directly by adapter, address and driver ID. Client drivers can support this by implementing theprobe()andremove()functions which accept a pre-filledi2c_clientstruct to manage.Signed-off-by Nathan Lutchansky <lutchann at litech.org>drivers/i2c/i2c-core.c | 157 +++++++++++++++++++++++++++++++++++--------- include/linux/i2c.h | 8 ++ 2 files changed, 136 insertions(+), 29 deletions(-)
drivers/i2c/i2c-core.c static LIST_HEAD(drivers); · line 45
Quoted 7 lines
drivers/i2c/i2c-core.c +3
-1@@ -40,10 +40,15 @@ static LIST_HEAD(drivers);static DEFINE_MUTEX (core_lists);static DEFINE_IDR (i2c_adapter_idr); -/* match always succeeds, as we want the probe() to tell if we really accept this match */ +static void i2c_notify_adapter (struct i2c_client *client); +static void i2c_register_client (struct i2c_client *client); +
The second declaration is unnecessary.
drivers/i2c/i2c-core.c static LIST_HEAD(drivers); · line 52
Quoted 8 lines
drivers/i2c/i2c-core.c +4
-1static int i2c_device_match (struct device *dev,struct device_driver *drv) { -return 1 ; +struct i2c_client *client =to_i2c_client (dev); +struct i2c_driver *driver =to_i2c_driver (drv); + +return client->driver_id = driver->id; }
As I mentioned previously - I think this function will ultimately need to change. As you have it, it short-circuits the driver model probing for any client without a specific driver ID. I understand that this doesn't matter at the moment.
Quoted 10 lines
drivers/i2c/i2c-core.c +4
-1static int i2c_bus_suspend (struct device * dev,pm_message_t state)@@ -66,11 +71,31 @@ static int i2c_bus_resume(struct devicestatic int i2c_device_probe (struct device *dev) { -return -ENODEV ; +struct i2c_client *client =to_i2c_client (dev); +struct i2c_driver *driver =to_i2c_driver (dev->driver); +int error = -ENODEV ; +
drivers/i2c/i2c-core.c static int i2c_bus_resume(struct device · line 87
Quoted 10 lines
drivers/i2c/i2c-core.c +9
-0+if (driver->probe) { + client->driver = driver; + error = driver->probe (client); +if (error <0 ) + client->driver =NULL ; +else +i2c_notify_adapter (client); + } +return error <0 ? error :0 ; }
What's wrong with "if (error)" here? Likewise "return error" at the end?
drivers/i2c/i2c-core.c static int i2c_bus_resume(struct device · line 99
Quoted 12 lines
drivers/i2c/i2c-core.c +8
-0static int i2c_device_remove (struct device *dev) { +struct i2c_client *client =to_i2c_client (dev); + +if (client->driver) { +if (client->driver->remove) + client->driver->remove (client); +if (client->driver->probe) + client->driver =NULL ; + }return 0 ; }
I would like to see "struct i2c_driver *driver = client->driver" at the top of this function... the rest would be more readable.
drivers/i2c/i2c-core.c out_unlock: · line 234
Quoted 3 lines
drivers/i2c/i2c-core.c +0
-0@@ -208,6 +233,19 @@ out_unlock:return res; }
From here...
drivers/i2c/i2c-core.c out_unlock: · line 242
Quoted 8 lines
drivers/i2c/i2c-core.c +8
-0+static int __i2c_detach_client (struct i2c_client *client) +{ +struct i2c_driver *driver = client->driver; +int error; + +if (driver && driver->detach_client) +return driver->detach_client (client); +
[ (!driver) is impossible there, i.e. indicates a bug, no? ]
Quoted 9 lines
drivers/i2c/i2c-core.c +5
-0+ error =i2c_detach_client (client); +if (error >=0 ) +kfree (client); +return error; +}int i2c_del_adapter (struct i2c_adapter *adap) {
drivers/i2c/i2c-core.c int i2c_del_adapter(struct i2c_adapter * · line 291
Quoted 8 lines
drivers/i2c/i2c-core.c +1
-1@@ -247,7 +285,7 @@ int i2c_del_adapter(struct i2c_adapter *list_for_each_safe (item, _n, &adap->clients) { client = list_entry(item, struct i2c_client, list); -if ((res=client->driver->detach_client (client))) { +if ((res=__i2c_detach_client (client))) {dev_err (&adap->dev,"detach_client failed for client " "[%s] at address 0x%02x\n" , client->name, client->addr);
... to here you're just making the driver->detach_client() callback optional. That's a problem: I don't see where you're going to kfree the struct i2c_client that i2c_add_client() allocates. This might explain the "can't unload then reload a client driver module" caveat that you mentioned earlier.
From here...
Quoted 51 lines
drivers/i2c/i2c-core.c +16
-6@@ -316,16 +354,13 @@ int i2c_register_driver(struct module *o }EXPORT_SYMBOL (i2c_register_driver); -int i2c_del_driver (struct i2c_driver *driver) +static int i2c_remove_all_clients (struct i2c_driver *driver) {struct list_head *item1, *item2, *_n;struct i2c_client *client;struct i2c_adapter *adap; -int res =0 ; -mutex_lock (&core_lists); -/* Have a look at each adapter, if clients of this driver are still * attached. If so, detach them to be able to kill the driver * afterwards. @@ -337,7 +372,7 @@ int i2c_del_driver(struct i2c_driver *drdev_err (&adap->dev,"detach_adapter failed " "for driver [%s]\n" , driver->driver.name); - goto out_unlock; +return res; } }else {list_for_each_safe (item2, _n, &adap->clients) {@@ -352,19 +387,33 @@ int i2c_del_driver(struct i2c_driver *dr"failed for client [%s] at " "0x%02x\n" , client->name, client->addr); - goto out_unlock; +return res; } } } } +return 0 ; +} + +int i2c_del_driver (struct i2c_driver *driver) +{ +int error =0 ; + +mutex_lock (&core_lists); + +if (driver->detach_adapter || driver->detach_client) { + error =i2c_remove_all_clients (driver); +if (error <0 ) + goto out_unlock;
drivers/i2c/i2c-core.c int i2c_del_driver(struct i2c_driver *dr · line 417
Quoted 10 lines
drivers/i2c/i2c-core.c +2
-1+ }driver_unregister (&driver->driver);list_del (&driver->list);pr_debug ("i2c-core: driver [%s] unregistered\n" , driver->driver.name); out_unlock:mutex_unlock (&core_lists); -return 0 ; +return error; }
... down to here is a refactoring of i2c_del_driver(), the point of which is to make driver->detach_client() optional - same problem as above.
I might have refactored further, splitting up the driver->detach_adapter() bits from the driver->detach_client() bits.
From here...
Quoted 58 lines
drivers/i2c/i2c-core.c +22
-13static int __i2c_check_addr (struct i2c_adapter *adapter,unsigned int addr)@@ -391,7 +440,7 @@ int i2c_check_addr(struct i2c_adapter *areturn rval; } -int i2c_attach_client (struct i2c_client *client) +static int i2c_add_to_bus (struct i2c_client *client) {struct i2c_adapter *adapter = client->adapter;@@ -402,32 +451,52 @@ int i2c_attach_client(struct i2c_client }list_add_tail (&client->list,&adapter->clients);mutex_unlock (&adapter->clist_lock); - -if (adapter->client_register) { -if (adapter->client_register (client)) { -dev_dbg (&adapter->dev,"client_register " -"failed for client [%s] at 0x%02x\n" , - client->name, client->addr); - } +return 0 ; +} + +static void i2c_notify_adapter (struct i2c_client *client) +{ +struct i2c_adapter *adapter = client->adapter; + +if (!adapter->client_register) +return ; +if (adapter->client_register (client)) { +dev_dbg (&adapter->dev,"client_register " +"failed for client [%s] at 0x%02x\n" , + client->name, client->addr); } +} +static void i2c_register_client (struct i2c_client *client) +{ client->usage_count =0 ; client->dev.parent = &client->adapter->dev; - client->dev.driver = &client->driver->driver; client->dev.bus = &i2c_bus_type; client->dev.release = &i2c_client_release;snprintf (&client->dev.bus_id[0 ],sizeof (client->dev.bus_id), -"%d-%04x" ,i2c_adapter_id (adapter), client->addr); -dev_dbg (&adapter->dev,"client [%s] registered with bus id %s\n" , - client->name, client->dev.bus_id); +"%d-%04x" ,i2c_adapter_id (client->adapter), client->addr); + +dev_dbg (&client->adapter->dev,"client [%s] registered with bus id " +"%s\n" , client->name, client->dev.bus_id); +device_register (&client->dev);device_create_file (&client->dev, &dev_attr_client_name); -
drivers/i2c/i2c-core.c int i2c_attach_client(struct i2c_client · line 494
Quoted 10 lines
drivers/i2c/i2c-core.c +7
-1-return 0 ; } +int i2c_attach_client (struct i2c_client *client) +{ +int error; + + error =i2c_add_to_bus (client); +if (error) +return error;
[ I prefer "if ((error = i2c_add_to_bus(client)))" here ]
drivers/i2c/i2c-core.c int i2c_attach_client(struct i2c_client · line 499
Quoted 5 lines
drivers/i2c/i2c-core.c +5
-0+i2c_notify_adapter (client); + client->dev.driver = &client->driver->driver; +i2c_register_client (client); +return 0 ; +}
... down to here is a refactoring of i2c_attach_client(), the point of which is to reuse pieces of it in i2c_add_client().
Quoted 17 lines
drivers/i2c/i2c-core.c +6
-3int i2c_detach_client (struct i2c_client *client) {@@ -462,11 +531,37 @@ int i2c_detach_client(struct i2c_clientreturn res; } -static int i2c_inc_use_client (struct i2c_client *client) +int i2c_add_client (struct i2c_adapter *adap,int address, +int driver_id,int kind) { +struct i2c_client *client; +int error; -if (!try_module_get (client->driver->driver.owner)) -return -ENODEV ; + client =kzalloc (sizeof (*client),GFP_KERNEL ); +if (client =NULL )
drivers/i2c/i2c-core.c int i2c_detach_client(struct i2c_client · line 551
Quoted 10 lines
drivers/i2c/i2c-core.c +10
-0+return -ENOMEM ; + client->driver_id = driver_id; + client->addr = address; + client->kind = kind; + client->adapter = adap; +snprintf (client->name,sizeof (client->name) -1 ,"device type %x, " +"kind %d" , driver_id, kind); + + error =i2c_add_to_bus (client); +if (error) {
if ((error = i2c_add_to_bus(client)))
Quoted 17 lines
drivers/i2c/i2c-core.c +13
-0+kfree (client); +return error; + } +i2c_register_client (client); + +return 0 ; +} + +static int i2c_inc_use_client (struct i2c_client *client) +{ +if (client->driver) +if (!try_module_get (client->driver->driver.owner)) +return -ENODEV ;if (!try_module_get (client->adapter->owner)) {module_put (client->driver->driver.owner);return -ENODEV ;
drivers/i2c/i2c-core.c static int i2c_inc_use_client(struct i2c · line 578
Quoted 8 lines
drivers/i2c/i2c-core.c +2
-1@@ -477,7 +572,8 @@ static int i2c_inc_use_client(struct i2cstatic void i2c_dec_use_client (struct i2c_client *client) { -module_put (client->driver->driver.owner); +if (client->driver) +module_put (client->driver->driver.owner);module_put (client->adapter->owner); }
As Jean Delvare pointed out... !(client->driver) is a bug. In this case "BUG_ON" is no better than just leaving it as it was - it will oops either way.
Quoted 43 lines
drivers/i2c/i2c-core.c +3
-0@@ -516,6 +612,8 @@ void i2c_clients_command(struct i2c_adapmutex_lock (&adap->clist_lock);list_for_each (item,&adap->clients) { client = list_entry(item, struct i2c_client, list); +if (!client->driver) +continue ;if (!try_module_get (client->driver->driver.owner))continue ;if (NULL != client->driver->command) {@@ -1138,6 +1236,7 @@ EXPORT_SYMBOL(i2c_del_adapter);EXPORT_SYMBOL (i2c_del_driver);EXPORT_SYMBOL (i2c_attach_client);EXPORT_SYMBOL (i2c_detach_client); +EXPORT_SYMBOL (i2c_add_client);EXPORT_SYMBOL (i2c_use_client);EXPORT_SYMBOL (i2c_release_client);EXPORT_SYMBOL (i2c_clients_command);include/linux/i2c.h +5
-0@@ -117,6 +117,9 @@ struct i2c_driver {int id;unsigned int class; +int (*probe)(struct i2c_client *);/* new device detected */ +void (*remove)(struct i2c_client *);/* device/adapter removed */ +/* Notifies the driver that a new bus has appeared. This routine * can be used by the driver to test if the bus meets its conditions * & seek for the presence of the chip(s) it supports. If found, it @@ -151,10 +154,12 @@ struct i2c_driver {* function is mainly used for lookup & other admin. functions. */ struct i2c_client { +int driver_id;/* match against i2c_driver.id */ unsigned int flags;/* div., see below */ unsigned short addr;/* chip address - NOTE: 7bit */ /* addresses are stored in the */ /* _LOWER_ 7 bits */ +int kind;/* forced chip kind, if needed */ struct i2c_adapter *adapter;/* the adapter we sit on */ struct i2c_driver *driver;/* and our access routines */ int usage_count;/* How many accesses currently */
include/linux/i2c.h static inline int i2c_add_driver(struct · line 318
Quoted 9 lines
include/linux/i2c.h +3
-0@@ -305,6 +310,9 @@ static inline int i2c_add_driver(structextern int i2c_attach_client (struct i2c_client *);extern int i2c_detach_client (struct i2c_client *); +extern int i2c_add_client (struct i2c_adapter *adap,int address, +int driver_id,int kind); +/* Should be used to make sure that client-struct is valid and that it is okay to access the i2c-client. returns -ENODEV if client has gone in the meantime */
Regards,
Signature · 2 lines
Mark M. Hoffman mhoffman at lightlink.com
PATCH 1/3 i2c: allow i2c core to instantiate
Hi Nathan:
Quoted 4 lines · Nathan Lutchansky
I was hoping I'd have more time to finish this up, but it's becoming apparent that it's not going to fit anywhere in my schedule. I'm sorry I have to drop this in a really unfinished state, but ignoring your emails isn't helping anything.
Hrm, I hope I haven't put you off somehow.
Quoted 4 lines
I'll try to address your major questions below, though:
On Sat, 27 May 2006, Mark M. Hoffman wrote:
Quoted 8 lines
Quoted 2 lines
Summary: the biggest immediate problem here is that
i2c_add_client()leaks a structi2c_clientby design, AFAICT.Right, clients added with
i2c_add_client()remain present until the bus is removed from the system, even if the driver for that client is removed. This is the behavior you would expect from i2c buses with a fixed, known list of clients present. However, for auto-probed clients, it is a change in behavior that users probably would not appreciate.
I meant leak literally... as in never kfree'ed. But now I see that it would get kfree'ed from within i2c_del_adapter(). I'm not sure how I missed that.
Quoted 13 lines
Quoted 4 lines
I do see that this approach addresses a requirement of embedded systems and others who know a priori what hardware is present. It is also nice that it does not force any changes to current drivers.
Quoted 8 lines
However, I think the
i2c_add_client()function is awkward. You say that this function could be called by an adapter that knows what devices it has... but what if the adapter is a generic driver like i2c-i801? For the embedded scenario, it would be preferable to further decouple these two pieces:[ adapter driver for bus foo ]
[ bus foo has devices x, y & z ]
Quoted 6 lines
Yes, it would be nice to be able to pass a list of attached devices to some adapter drivers. Possibly this could be passed in as a module parameter or as a field in the platform data structure for platform devices. However, this list might be dependent upon the contents of an EEPROM device located on that bus, so it would be good to leave this part as flexible as possible.
In the embedded scenario, would it be appropriate to pass such a list from platform_notify()? That looks like the right place to me.
Quoted 8 lines
Quoted 3 lines
I would argue against a general move to this method for hwmon and others. There would be no benefit that I can tell; in the long term I would expect the
i2c_add_client()function to disappear anyway.What do you expect would be a more suitable method for hwmon to instantiate clients? The current
i2c_probe()mechanism is rather lacking; requiring users to pass arcane module parameters to each chip module is not ideal,
It may not be ideal, but it's rarely necessary for hwmon at any rate. [see below for an answer to "what do you expect..."]
Quoted 2 lines
and is completely impossible to use where the client drivers are compiled directly into the kernel.
Documentation/kernel-parameters.txt: 3rd paragraph
Quoted 6 lines
I suspect you'll eventually need
to go to a system where drivers can be bound at runtime to specific addresses using sysfs, similar to the SCSI subsystem where new devices can be probed by echoing "scsi add-single-device 4 0 6 0" to/proc/scsi/scsi. Such a mechanism would probably use something likei2c_add_clienton the backend.
There are some i2c devices that are 100% not detectable by probing. This would be a good alternative for those.
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
static int
i2c_device_match(struct device *dev, struct device_driver *drv){(quoted diff) +2
-1- return 1; + struct i2c_client *client = to_i2c_client(dev); + struct i2c_driver *driver = to_i2c_driver(drv);Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ + return client->driver_id = driver->id; }As I mentioned previously - I think this function will ultimately need to change. As you have it, it short-circuits the driver model probing for any client without a specific driver ID. I understand that this doesn't matter at the moment.
Quoted 4 lines
I'm still not sure I understand why some drivers would not have IDs assigned. For embedded systems or V4L devices, where there is a fixed list of clients known to be present, how else would the proper client driver for each address be specified?
I will try to answer this and "What do you expect ..." above at the same time, but it probably takes a patch to make it clear.
If we're going to better integrate the i2c subsystem with the driver model, I think the i2c core has to instantiate all client structs. It would enumerate through the i2c address space looking to match (i2c_device_match) at each address. The client->driver_id may or may not be filled in at that point; depends on whether the adapter has a known list of devices. If the client does have a driver_id, that will serve the purpose of *ruling out* all other devices. That is why I suggested (the equivalent to) this earlier:
if (client->driver_id) return client->driver_id = driver->id;
If we make it past that, we look for a match with the driver's address data. Assuming that's good, we'll do i2c_device_probe() much as you have it. We would continue to use adapter->class to restrict or modify probing behavior, but we might be able to reduce it to PROBE and NOPROBE. At least for the embedded scenario, there should be an I2C_CLASS_NONE.
That's my vision for now. I will send patches as soon as I can.
Quoted 36 lines
Quoted 11 lines
Quoted 11 lines
static int
i2c_device_probe(struct device *dev)
{(quoted diff) +8
-1- return -ENODEV; + struct i2c_client *client = to_i2c_client(dev); + struct i2c_driver *driver = to_i2c_driver(dev->driver); + int error = -ENODEV; + + if (driver->probe) { + client->driver = driver; + error = driver->probe(client); + if (error < 0)Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +5
-0+ client->driver = NULL; + else + i2c_notify_adapter(client); + } + return error < 0 ? error : 0; }What's wrong with "if (error)" here? Likewise "return error" at the end?
I believe I intended for the
driver->probe()function to return either 0 or a positive value on success, although I don't remember what those would have indicated. Buti2c_device_probe()would always need to return only 0 on success, which is why the strange comparisons are there.Quoted 10 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+static int __i2c_detach_client(struct i2c_client *client) +{ + struct i2c_driver *driver = client->driver; + int error; + + if (driver && driver->detach_client) + return driver->detach_client(client); +[ (!driver) is impossible there, i.e. indicates a bug, no? ]
Quoted 6 lines
No, it's perfectly fine for a client to exist on a bus with no associated client driver. This will happen if a client is instantiated with
i2c_add_client()and the indicated driver ID does not correspond to an existing driver, perhaps because a module is not loaded or the driver is not compiled into the kernel. Allowing driverless clients to exist was the point of the patch, really...
OK, thanks. Your second patch had no examples of calling i2c_add_client() from an adapter rather than from a driver, so I didn't consider that case.
Quoted 26 lines
Quoted 5 lines
... to here you're just making the
driver->detach_client()callback optional. That's a problem: I don't see where you're going to kfree the structi2c_clientthati2c_add_client()allocates. This might explain the "can't unload then reload a client driver module" caveat that you mentioned earlier.Right, yes. Any
i2c_clientstruct created byi2c_add_client()will only ever get destroyed byi2c_del_adapter(), as discussed above.Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ error = i2c_add_to_bus(client); + if (error) + return error;[ I prefer "if ((error =
i2c_add_to_bus(client)))" here ]I'm not fond of hiding assignments in
if()statements, but I think that's just personal style.Quoted 7 lines
Review
Quoted 3 lines
(quoted diff) +2
-1- module_put(client->driver->driver.owner); + if (client->driver) + module_put(client->driver->driver.owner);As Jean Delvare pointed out... !(client->driver) is a bug. In this case "
BUG_ON" is no better than just leaving it as it was - it will oops either way.
Quoted 6 lines
Yeah, I didn't think about the module reference counting, it definitely needs to be fixed.
Again, I'm really sorry to have to drop this, but I have too much going on at the moment. I'd still be interested in following future discussion on the topic, though. -Nathan
No problem. I'm sorry it took so long to review as well.
Regards,
Signature · 2 lines
Mark M. Hoffman mhoffman at lightlink.com
PATCH 1/3 i2c: allow i2c core to instantiate
In reply to Mark M. HoffmanQuoted 7 lines · Mark M. Hoffman
Quoted 4 lines · Nathan Lutchansky
I was hoping I'd have more time to finish this up, but it's becoming apparent that it's not going to fit anywhere in my schedule. I'm sorry I have to drop this in a really unfinished state, but ignoring your emails isn't helping anything.
Hrm, I hope I haven't put you off somehow.
No, not at all, in fact I'm glad you've taken an interest in this patchset. I wish I had time right now to really work with you on it.
Quoted 17 lines
Quoted 10 lines · Mark M. Hoffman
Quoted 8 lines
However, I think the
i2c_add_client()function is awkward. You say that this function could be called by an adapter that knows what devices it has... but what if the adapter is a generic driver like i2c-i801? For the embedded scenario, it would be preferable to further decouple these two pieces:[ adapter driver for bus foo ]
[ bus foo has devices x, y & z ]Quoted 6 lines
Yes, it would be nice to be able to pass a list of attached devices to some adapter drivers. Possibly this could be passed in as a module parameter or as a field in the platform data structure for platform devices. However, this list might be dependent upon the contents of an EEPROM device located on that bus, so it would be good to leave this part as flexible as possible.
Quoted 2 lines
In the embedded scenario, would it be appropriate to pass such a list from
platform_notify()? That looks like the right place to me.
I was just thinking of sticking it in dev.platform_data for the I2C adapter declaration in the static array passed to platform_add_devices(). I'm sure there are other ways to do it that people would find useful.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
I would argue against a general move to this method for hwmon and others.
What do you expect would be a more suitable method for hwmon to instantiate clients? The current
i2c_probe()mechanism is rather lacking; requiring users to pass arcane module parameters to each chip module is not ideal,It may not be ideal, but it's rarely necessary for hwmon at any rate.
At the moment, anyway. It will become more and more necessary as the number of different I2C sensor chips on the market increases. V4L/DVB has already hit this problem in a big way.
Quoted 4 lines
Quoted 2 lines
and is completely impossible to use where the client drivers are compiled directly into the kernel.
Documentation/kernel-parameters.txt: 3rd paragraph
Sure, but if you've got 10 different chips on a bus, that's a few too many parameters on the command line.
Quoted 4 lines
If we're going to better integrate the i2c subsystem with the driver model, I think the i2c core has to instantiate all client structs. It would enumerate through the i2c address space looking to match (
i2c_device_match) at each address.
It would certainly be helpful if we could enumerate through all possible I2C addresses to discover devices, but I don't think it's going to work in the general case.
- Some devices interpret a quick write as a command and do something
potentially undesirable. I think the 24RF08 EEPROMs fall into this category, right? And who knows how future chips might behave...
- Some devices are very sensitive to how they are accessed immediately
after boot. One of the tuner modules we use requires a firmware upload over I2C before it behaves properly, and a quick write before the firmware upload will cause it to lock up the entire I2C bus.
- Tuner devices and data collection chips often sit behind an I2C gate
that isolates the chip from the I2C bus in order to reduce noise. These devices can't be probed until the gate is bridged.
- Some I2C adapters are extremely slow/resource-intensive or can't
perform quick write operations, making probing 127 addresses either impractical or impossible.
Even if you could enumerate all addresses, how would you know which client driver is required for each? The address alone is not enough data, of course, and adding more probing commands on top of the quick write would become very, very dangerous.
So, that's why the patch is as limited as it is. It takes care of all the obvious parts of adapting the I2C subsystem to the device model and leaves the existing probing mechanism the way it is, but provides a clean path for embedded platforms to explicitly instantiate their devices without probing.
I hope that makes my motivations more clear. -Nathan
PATCH 1/3 i2c: allow i2c core to instantiate
In reply to Mark M. HoffmanHi Nathan:
Quoted 5 lines · Mark M. Hoffman
Quoted 4 lines
If we're going to better integrate the i2c subsystem with the driver model, I think the i2c core has to instantiate all client structs. It would enumerate through the i2c address space looking to match (
i2c_device_match) at each address.
Quoted 13 lines · Nathan Lutchansky
It would certainly be helpful if we could enumerate through all possible I2C addresses to discover devices, but I don't think it's going to work in the general case.
- Some devices interpret a quick write as a command and do something
potentially undesirable. I think the 24RF08 EEPROMs fall into this category, right? And who knows how future chips might behave...- Some devices are very sensitive to how they are accessed immediately
after boot. One of the tuner modules we use requires a firmware upload over I2C before it behaves properly, and a quick write before the firmware upload will cause it to lock up the entire I2C bus.
Quoted 7 lines
- Tuner devices and data collection chips often sit behind an I2C gate
that isolates the chip from the I2C bus in order to reduce noise. These devices can't be probed until the gate is bridged.- Some I2C adapters are extremely slow/resource-intensive or can't
perform quick write operations, making probing 127 addresses either impractical or impossible.
I was *not* suggesting that we blindly perform quick writes on all addresses.
Quoted 4 lines
Even if you could enumerate all addresses, how would you know which client driver is required for each? The address alone is not enough data, of course, and adding more probing commands on top of the quick write would become very, very dangerous.
Again, I was *not* suggesting that we add any more bus xfers to the probing. What I have in mind will *reduce* if not *eliminate* that traffic. I must have explained it really poorly for you to get that impression. I'll try to fix that by sending patches.
Quoted 7 lines
So, that's why the patch is as limited as it is. It takes care of all the obvious parts of adapting the I2C subsystem to the device model and leaves the existing probing mechanism the way it is, but provides a clean path for embedded platforms to explicitly instantiate their devices without probing.
I hope that makes my motivations more clear. -Nathan
I already understood these two goals:
(1) Adapt I2C to better fit the kernel driver model.
(2) Allow client devices to be attached without probing.
I am beginning to get the impression, from later emails in this thread by Hans and Manu, that (2) is really closer to...
(3) Allow precise control over I2C bus traffic for adapters that require it.
What I have in mind should satisfy these goals. Again, I'll try to get out some patches as soon as I can.
Regards,
Signature · 2 lines
Mark M. Hoffman mhoffman at lightlink.com