aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/i2c-core.c
diff options
context:
space:
mode:
authorDavid Brownell <david-b@pacbell.net>2007-05-01 17:26:30 -0400
committerJean Delvare <khali@hyperion.delvare>2007-05-01 17:26:30 -0400
commit7b4fbc50fabb810523be522fe7ec5cc40f85c7a1 (patch)
tree688fa45d9b8067d72eb462e6863cad6d2e6b84cd /drivers/i2c/i2c-core.c
parent5cedb05db3c3084c9641403dd24c310a6b3ea19f (diff)
i2c: i2c stack can probe()
One of a series of I2C infrastructure updates to support enumeration using the standard Linux driver model. This patch updates probe() and associated hotplug/coldplug support, but not remove(). Nothing yet _uses_ it to create I2C devices, so those hotplug/coldplug mechanisms will be the only externally visible change. This patch will be an overall NOP since the I2C stack doesn't yet create clients/devices except as part of binding them to legacy drivers. Some code is moved earlier in the source code, helping group more of the per-device infrastructure in one place and simplifying handling per-device attributes. Terminology being adopted: "legacy drivers" create devices (i2c_client) themselves, while "new style" ones follow the driver model (the i2c_client is handed to the probe routine). It's an either/or thing; the two models don't mix, and drivers that try mixing them won't even be registered. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers/i2c/i2c-core.c')
-rw-r--r--drivers/i2c/i2c-core.c141
1 files changed, 99 insertions, 42 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index fd921ce0b75b..a2ad83ad0f53 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -44,15 +44,58 @@ static DEFINE_IDR(i2c_adapter_idr);
44 44
45/* ------------------------------------------------------------------------- */ 45/* ------------------------------------------------------------------------- */
46 46
47/* match always succeeds, as we want the probe() to tell if we really accept this match */
48static int i2c_device_match(struct device *dev, struct device_driver *drv) 47static int i2c_device_match(struct device *dev, struct device_driver *drv)
49{ 48{
50 return 1; 49 struct i2c_client *client = to_i2c_client(dev);
50 struct i2c_driver *driver = to_i2c_driver(drv);
51
52 /* make legacy i2c drivers bypass driver model probing entirely;
53 * such drivers scan each i2c adapter/bus themselves.
54 */
55 if (!driver->probe)
56 return 0;
57
58 /* new style drivers use the same kind of driver matching policy
59 * as platform devices or SPI: compare device and driver IDs.
60 */
61 return strcmp(client->driver_name, drv->name) == 0;
51} 62}
52 63
64#ifdef CONFIG_HOTPLUG
65
66/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
67static int i2c_device_uevent(struct device *dev, char **envp, int num_envp,
68 char *buffer, int buffer_size)
69{
70 struct i2c_client *client = to_i2c_client(dev);
71 int i = 0, length = 0;
72
73 /* by definition, legacy drivers can't hotplug */
74 if (dev->driver || !client->driver_name)
75 return 0;
76
77 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
78 "MODALIAS=%s", client->driver_name))
79 return -ENOMEM;
80 envp[i] = NULL;
81 dev_dbg(dev, "uevent\n");
82 return 0;
83}
84
85#else
86#define i2c_device_uevent NULL
87#endif /* CONFIG_HOTPLUG */
88
53static int i2c_device_probe(struct device *dev) 89static int i2c_device_probe(struct device *dev)
54{ 90{
55 return -ENODEV; 91 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
93
94 if (!driver->probe)
95 return -ENODEV;
96 client->driver = driver;
97 dev_dbg(dev, "probe\n");
98 return driver->probe(client);
56} 99}
57 100
58static int i2c_device_remove(struct device *dev) 101static int i2c_device_remove(struct device *dev)
@@ -95,9 +138,38 @@ static int i2c_device_resume(struct device * dev)
95 return driver->resume(to_i2c_client(dev)); 138 return driver->resume(to_i2c_client(dev));
96} 139}
97 140
141static void i2c_client_release(struct device *dev)
142{
143 struct i2c_client *client = to_i2c_client(dev);
144 complete(&client->released);
145}
146
147static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
148{
149 struct i2c_client *client = to_i2c_client(dev);
150 return sprintf(buf, "%s\n", client->name);
151}
152
153static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
154{
155 struct i2c_client *client = to_i2c_client(dev);
156 return client->driver_name
157 ? sprintf(buf, "%s\n", client->driver_name)
158 : 0;
159}
160
161static struct device_attribute i2c_dev_attrs[] = {
162 __ATTR(name, S_IRUGO, show_client_name, NULL),
163 /* modalias helps coldplug: modprobe $(cat .../modalias) */
164 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
165 { },
166};
167
98struct bus_type i2c_bus_type = { 168struct bus_type i2c_bus_type = {
99 .name = "i2c", 169 .name = "i2c",
170 .dev_attrs = i2c_dev_attrs,
100 .match = i2c_device_match, 171 .match = i2c_device_match,
172 .uevent = i2c_device_uevent,
101 .probe = i2c_device_probe, 173 .probe = i2c_device_probe,
102 .remove = i2c_device_remove, 174 .remove = i2c_device_remove,
103 .shutdown = i2c_device_shutdown, 175 .shutdown = i2c_device_shutdown,
@@ -134,31 +206,6 @@ struct class i2c_adapter_class = {
134}; 206};
135 207
136 208
137static void i2c_client_release(struct device *dev)
138{
139 struct i2c_client *client = to_i2c_client(dev);
140 complete(&client->released);
141}
142
143static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
144{
145 struct i2c_client *client = to_i2c_client(dev);
146 return sprintf(buf, "%s\n", client->name);
147}
148
149/*
150 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
151 * an i2c adapter attribute (above).
152 */
153static struct device_attribute dev_attr_client_name =
154 __ATTR(name, S_IRUGO, &show_client_name, NULL);
155
156
157/* ---------------------------------------------------
158 * registering functions
159 * ---------------------------------------------------
160 */
161
162/* ----- 209/* -----
163 * i2c_add_adapter is called from within the algorithm layer, 210 * i2c_add_adapter is called from within the algorithm layer,
164 * when a new hw adapter registers. A new device is register to be 211 * when a new hw adapter registers. A new device is register to be
@@ -208,7 +255,7 @@ int i2c_add_adapter(struct i2c_adapter *adap)
208 255
209 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); 256 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
210 257
211 /* inform drivers of new adapters */ 258 /* let legacy drivers scan this bus for matching devices */
212 list_for_each(item,&drivers) { 259 list_for_each(item,&drivers) {
213 driver = list_entry(item, struct i2c_driver, list); 260 driver = list_entry(item, struct i2c_driver, list);
214 if (driver->attach_adapter) 261 if (driver->attach_adapter)
@@ -292,16 +339,32 @@ int i2c_del_adapter(struct i2c_adapter *adap)
292} 339}
293 340
294 341
295/* ----- 342/* ------------------------------------------------------------------------- */
296 * What follows is the "upwards" interface: commands for talking to clients, 343
297 * which implement the functions to access the physical information of the 344/*
298 * chips. 345 * An i2c_driver is used with one or more i2c_client (device) nodes to access
346 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
347 * are two models for binding the driver to its device: "new style" drivers
348 * follow the standard Linux driver model and just respond to probe() calls
349 * issued if the driver core sees they match(); "legacy" drivers create device
350 * nodes themselves.
299 */ 351 */
300 352
301int i2c_register_driver(struct module *owner, struct i2c_driver *driver) 353int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
302{ 354{
303 int res; 355 int res;
304 356
357 /* new style driver methods can't mix with legacy ones */
358 if (driver->probe) {
359 if (driver->attach_adapter || driver->detach_adapter
360 || driver->detach_client) {
361 printk(KERN_WARNING
362 "i2c-core: driver [%s] is confused\n",
363 driver->driver.name);
364 return -EINVAL;
365 }
366 }
367
305 /* add the driver to the list of i2c drivers in the driver core */ 368 /* add the driver to the list of i2c drivers in the driver core */
306 driver->driver.owner = owner; 369 driver->driver.owner = owner;
307 driver->driver.bus = &i2c_bus_type; 370 driver->driver.bus = &i2c_bus_type;
@@ -315,7 +378,7 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
315 list_add_tail(&driver->list,&drivers); 378 list_add_tail(&driver->list,&drivers);
316 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); 379 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
317 380
318 /* now look for instances of driver on our adapters */ 381 /* legacy drivers scan i2c busses directly */
319 if (driver->attach_adapter) { 382 if (driver->attach_adapter) {
320 struct i2c_adapter *adapter; 383 struct i2c_adapter *adapter;
321 384
@@ -380,6 +443,8 @@ int i2c_del_driver(struct i2c_driver *driver)
380 return 0; 443 return 0;
381} 444}
382 445
446/* ------------------------------------------------------------------------- */
447
383static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) 448static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
384{ 449{
385 struct list_head *item; 450 struct list_head *item;
@@ -430,9 +495,6 @@ int i2c_attach_client(struct i2c_client *client)
430 res = device_register(&client->dev); 495 res = device_register(&client->dev);
431 if (res) 496 if (res)
432 goto out_list; 497 goto out_list;
433 res = device_create_file(&client->dev, &dev_attr_client_name);
434 if (res)
435 goto out_unregister;
436 mutex_unlock(&adapter->clist_lock); 498 mutex_unlock(&adapter->clist_lock);
437 499
438 if (adapter->client_register) { 500 if (adapter->client_register) {
@@ -445,10 +507,6 @@ int i2c_attach_client(struct i2c_client *client)
445 507
446 return 0; 508 return 0;
447 509
448out_unregister:
449 init_completion(&client->released); /* Needed? */
450 device_unregister(&client->dev);
451 wait_for_completion(&client->released);
452out_list: 510out_list:
453 list_del(&client->list); 511 list_del(&client->list);
454 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " 512 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
@@ -483,7 +541,6 @@ int i2c_detach_client(struct i2c_client *client)
483 mutex_lock(&adapter->clist_lock); 541 mutex_lock(&adapter->clist_lock);
484 list_del(&client->list); 542 list_del(&client->list);
485 init_completion(&client->released); 543 init_completion(&client->released);
486 device_remove_file(&client->dev, &dev_attr_client_name);
487 device_unregister(&client->dev); 544 device_unregister(&client->dev);
488 mutex_unlock(&adapter->clist_lock); 545 mutex_unlock(&adapter->clist_lock);
489 wait_for_completion(&client->released); 546 wait_for_completion(&client->released);