diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2009-06-19 20:45:51 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-06-19 20:45:51 -0400 |
commit | 5c6fb0059cec9a3c0f6d88a0ddf3d62ad323cd57 (patch) | |
tree | 49b4ceaeb5686fca4546315155d04633adf25b3c /drivers/i2c | |
parent | 31583d6acf940d2951bc8716557b06d9de5a0c4b (diff) | |
parent | c70366732f67dbdb32f7fe9c6aa59299b76feca6 (diff) |
Merge branch 'i2c-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvare/staging
* 'i2c-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvare/staging:
i2c: New macro to initialize i2c address lists on the fly
i2c: Don't advertise i2c functions when not available
i2c: Use rwsem instead of mutex for board info
i2c: Add a sysfs interface to instantiate devices
i2c: Limit core locking to the necessary sections
i2c: Kill the redundant client list
i2c: Kill is_newstyle_driver
i2c: Merge i2c_attach_client into i2c_new_device
i2c: Drop i2c_probe function
i2c: Get rid of the legacy binding model
i2c: Kill client_register and client_unregister methods
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/i2c-boardinfo.c | 7 | ||||
-rw-r--r-- | drivers/i2c/i2c-core.c | 557 | ||||
-rw-r--r-- | drivers/i2c/i2c-core.h | 4 |
3 files changed, 205 insertions, 363 deletions
diff --git a/drivers/i2c/i2c-boardinfo.c b/drivers/i2c/i2c-boardinfo.c index ffb35f09df03..a26a34a06641 100644 --- a/drivers/i2c/i2c-boardinfo.c +++ b/drivers/i2c/i2c-boardinfo.c | |||
@@ -18,6 +18,7 @@ | |||
18 | 18 | ||
19 | #include <linux/kernel.h> | 19 | #include <linux/kernel.h> |
20 | #include <linux/i2c.h> | 20 | #include <linux/i2c.h> |
21 | #include <linux/rwsem.h> | ||
21 | 22 | ||
22 | #include "i2c-core.h" | 23 | #include "i2c-core.h" |
23 | 24 | ||
@@ -25,7 +26,7 @@ | |||
25 | /* These symbols are exported ONLY FOR the i2c core. | 26 | /* These symbols are exported ONLY FOR the i2c core. |
26 | * No other users will be supported. | 27 | * No other users will be supported. |
27 | */ | 28 | */ |
28 | DEFINE_MUTEX(__i2c_board_lock); | 29 | DECLARE_RWSEM(__i2c_board_lock); |
29 | EXPORT_SYMBOL_GPL(__i2c_board_lock); | 30 | EXPORT_SYMBOL_GPL(__i2c_board_lock); |
30 | 31 | ||
31 | LIST_HEAD(__i2c_board_list); | 32 | LIST_HEAD(__i2c_board_list); |
@@ -63,7 +64,7 @@ i2c_register_board_info(int busnum, | |||
63 | { | 64 | { |
64 | int status; | 65 | int status; |
65 | 66 | ||
66 | mutex_lock(&__i2c_board_lock); | 67 | down_write(&__i2c_board_lock); |
67 | 68 | ||
68 | /* dynamic bus numbers will be assigned after the last static one */ | 69 | /* dynamic bus numbers will be assigned after the last static one */ |
69 | if (busnum >= __i2c_first_dynamic_bus_num) | 70 | if (busnum >= __i2c_first_dynamic_bus_num) |
@@ -84,7 +85,7 @@ i2c_register_board_info(int busnum, | |||
84 | list_add_tail(&devinfo->list, &__i2c_board_list); | 85 | list_add_tail(&devinfo->list, &__i2c_board_list); |
85 | } | 86 | } |
86 | 87 | ||
87 | mutex_unlock(&__i2c_board_lock); | 88 | up_write(&__i2c_board_lock); |
88 | 89 | ||
89 | return status; | 90 | return status; |
90 | } | 91 | } |
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 5ed622ee65c3..0e45c296d3d2 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -33,16 +33,20 @@ | |||
33 | #include <linux/completion.h> | 33 | #include <linux/completion.h> |
34 | #include <linux/hardirq.h> | 34 | #include <linux/hardirq.h> |
35 | #include <linux/irqflags.h> | 35 | #include <linux/irqflags.h> |
36 | #include <linux/rwsem.h> | ||
36 | #include <asm/uaccess.h> | 37 | #include <asm/uaccess.h> |
37 | 38 | ||
38 | #include "i2c-core.h" | 39 | #include "i2c-core.h" |
39 | 40 | ||
40 | 41 | ||
42 | /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees | ||
43 | that device detection, deletion of detected devices, and attach_adapter | ||
44 | and detach_adapter calls are serialized */ | ||
41 | static DEFINE_MUTEX(core_lock); | 45 | static DEFINE_MUTEX(core_lock); |
42 | static DEFINE_IDR(i2c_adapter_idr); | 46 | static DEFINE_IDR(i2c_adapter_idr); |
47 | static LIST_HEAD(userspace_devices); | ||
43 | 48 | ||
44 | #define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect) | 49 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr); |
45 | |||
46 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); | 50 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); |
47 | 51 | ||
48 | /* ------------------------------------------------------------------------- */ | 52 | /* ------------------------------------------------------------------------- */ |
@@ -63,12 +67,6 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv) | |||
63 | struct i2c_client *client = to_i2c_client(dev); | 67 | struct i2c_client *client = to_i2c_client(dev); |
64 | struct i2c_driver *driver = to_i2c_driver(drv); | 68 | struct i2c_driver *driver = to_i2c_driver(drv); |
65 | 69 | ||
66 | /* make legacy i2c drivers bypass driver model probing entirely; | ||
67 | * such drivers scan each i2c adapter/bus themselves. | ||
68 | */ | ||
69 | if (!is_newstyle_driver(driver)) | ||
70 | return 0; | ||
71 | |||
72 | /* match on an id table if there is one */ | 70 | /* match on an id table if there is one */ |
73 | if (driver->id_table) | 71 | if (driver->id_table) |
74 | return i2c_match_id(driver->id_table, client) != NULL; | 72 | return i2c_match_id(driver->id_table, client) != NULL; |
@@ -83,10 +81,6 @@ static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) | |||
83 | { | 81 | { |
84 | struct i2c_client *client = to_i2c_client(dev); | 82 | struct i2c_client *client = to_i2c_client(dev); |
85 | 83 | ||
86 | /* by definition, legacy drivers can't hotplug */ | ||
87 | if (dev->driver) | ||
88 | return 0; | ||
89 | |||
90 | if (add_uevent_var(env, "MODALIAS=%s%s", | 84 | if (add_uevent_var(env, "MODALIAS=%s%s", |
91 | I2C_MODULE_PREFIX, client->name)) | 85 | I2C_MODULE_PREFIX, client->name)) |
92 | return -ENOMEM; | 86 | return -ENOMEM; |
@@ -175,12 +169,6 @@ static int i2c_device_resume(struct device *dev) | |||
175 | return driver->resume(to_i2c_client(dev)); | 169 | return driver->resume(to_i2c_client(dev)); |
176 | } | 170 | } |
177 | 171 | ||
178 | static void i2c_client_release(struct device *dev) | ||
179 | { | ||
180 | struct i2c_client *client = to_i2c_client(dev); | ||
181 | complete(&client->released); | ||
182 | } | ||
183 | |||
184 | static void i2c_client_dev_release(struct device *dev) | 172 | static void i2c_client_dev_release(struct device *dev) |
185 | { | 173 | { |
186 | kfree(to_i2c_client(dev)); | 174 | kfree(to_i2c_client(dev)); |
@@ -240,15 +228,17 @@ EXPORT_SYMBOL(i2c_verify_client); | |||
240 | 228 | ||
241 | 229 | ||
242 | /** | 230 | /** |
243 | * i2c_new_device - instantiate an i2c device for use with a new style driver | 231 | * i2c_new_device - instantiate an i2c device |
244 | * @adap: the adapter managing the device | 232 | * @adap: the adapter managing the device |
245 | * @info: describes one I2C device; bus_num is ignored | 233 | * @info: describes one I2C device; bus_num is ignored |
246 | * Context: can sleep | 234 | * Context: can sleep |
247 | * | 235 | * |
248 | * Create a device to work with a new style i2c driver, where binding is | 236 | * Create an i2c device. Binding is handled through driver model |
249 | * handled through driver model probe()/remove() methods. This call is not | 237 | * probe()/remove() methods. A driver may be bound to this device when we |
250 | * appropriate for use by mainboad initialization logic, which usually runs | 238 | * return from this function, or any later moment (e.g. maybe hotplugging will |
251 | * during an arch_initcall() long before any i2c_adapter could exist. | 239 | * load the driver module). This call is not appropriate for use by mainboard |
240 | * initialization logic, which usually runs during an arch_initcall() long | ||
241 | * before any i2c_adapter could exist. | ||
252 | * | 242 | * |
253 | * This returns the new i2c client, which may be saved for later use with | 243 | * This returns the new i2c client, which may be saved for later use with |
254 | * i2c_unregister_device(); or NULL to indicate an error. | 244 | * i2c_unregister_device(); or NULL to indicate an error. |
@@ -276,17 +266,31 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | |||
276 | 266 | ||
277 | strlcpy(client->name, info->type, sizeof(client->name)); | 267 | strlcpy(client->name, info->type, sizeof(client->name)); |
278 | 268 | ||
279 | /* a new style driver may be bound to this device when we | 269 | /* Check for address business */ |
280 | * return from this function, or any later moment (e.g. maybe | 270 | status = i2c_check_addr(adap, client->addr); |
281 | * hotplugging will load the driver module). and the device | 271 | if (status) |
282 | * refcount model is the standard driver model one. | 272 | goto out_err; |
283 | */ | 273 | |
284 | status = i2c_attach_client(client); | 274 | client->dev.parent = &client->adapter->dev; |
285 | if (status < 0) { | 275 | client->dev.bus = &i2c_bus_type; |
286 | kfree(client); | 276 | client->dev.release = i2c_client_dev_release; |
287 | client = NULL; | 277 | |
288 | } | 278 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), |
279 | client->addr); | ||
280 | status = device_register(&client->dev); | ||
281 | if (status) | ||
282 | goto out_err; | ||
283 | |||
284 | dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", | ||
285 | client->name, dev_name(&client->dev)); | ||
286 | |||
289 | return client; | 287 | return client; |
288 | |||
289 | out_err: | ||
290 | dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " | ||
291 | "(%d)\n", client->name, client->addr, status); | ||
292 | kfree(client); | ||
293 | return NULL; | ||
290 | } | 294 | } |
291 | EXPORT_SYMBOL_GPL(i2c_new_device); | 295 | EXPORT_SYMBOL_GPL(i2c_new_device); |
292 | 296 | ||
@@ -298,28 +302,6 @@ EXPORT_SYMBOL_GPL(i2c_new_device); | |||
298 | */ | 302 | */ |
299 | void i2c_unregister_device(struct i2c_client *client) | 303 | void i2c_unregister_device(struct i2c_client *client) |
300 | { | 304 | { |
301 | struct i2c_adapter *adapter = client->adapter; | ||
302 | struct i2c_driver *driver = client->driver; | ||
303 | |||
304 | if (driver && !is_newstyle_driver(driver)) { | ||
305 | dev_err(&client->dev, "can't unregister devices " | ||
306 | "with legacy drivers\n"); | ||
307 | WARN_ON(1); | ||
308 | return; | ||
309 | } | ||
310 | |||
311 | if (adapter->client_unregister) { | ||
312 | if (adapter->client_unregister(client)) { | ||
313 | dev_warn(&client->dev, | ||
314 | "client_unregister [%s] failed\n", | ||
315 | client->name); | ||
316 | } | ||
317 | } | ||
318 | |||
319 | mutex_lock(&adapter->clist_lock); | ||
320 | list_del(&client->list); | ||
321 | mutex_unlock(&adapter->clist_lock); | ||
322 | |||
323 | device_unregister(&client->dev); | 305 | device_unregister(&client->dev); |
324 | } | 306 | } |
325 | EXPORT_SYMBOL_GPL(i2c_unregister_device); | 307 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
@@ -393,8 +375,128 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) | |||
393 | return sprintf(buf, "%s\n", adap->name); | 375 | return sprintf(buf, "%s\n", adap->name); |
394 | } | 376 | } |
395 | 377 | ||
378 | /* | ||
379 | * Let users instantiate I2C devices through sysfs. This can be used when | ||
380 | * platform initialization code doesn't contain the proper data for | ||
381 | * whatever reason. Also useful for drivers that do device detection and | ||
382 | * detection fails, either because the device uses an unexpected address, | ||
383 | * or this is a compatible device with different ID register values. | ||
384 | * | ||
385 | * Parameter checking may look overzealous, but we really don't want | ||
386 | * the user to provide incorrect parameters. | ||
387 | */ | ||
388 | static ssize_t | ||
389 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, | ||
390 | const char *buf, size_t count) | ||
391 | { | ||
392 | struct i2c_adapter *adap = to_i2c_adapter(dev); | ||
393 | struct i2c_board_info info; | ||
394 | struct i2c_client *client; | ||
395 | char *blank, end; | ||
396 | int res; | ||
397 | |||
398 | dev_warn(dev, "The new_device interface is still experimental " | ||
399 | "and may change in a near future\n"); | ||
400 | memset(&info, 0, sizeof(struct i2c_board_info)); | ||
401 | |||
402 | blank = strchr(buf, ' '); | ||
403 | if (!blank) { | ||
404 | dev_err(dev, "%s: Missing parameters\n", "new_device"); | ||
405 | return -EINVAL; | ||
406 | } | ||
407 | if (blank - buf > I2C_NAME_SIZE - 1) { | ||
408 | dev_err(dev, "%s: Invalid device name\n", "new_device"); | ||
409 | return -EINVAL; | ||
410 | } | ||
411 | memcpy(info.type, buf, blank - buf); | ||
412 | |||
413 | /* Parse remaining parameters, reject extra parameters */ | ||
414 | res = sscanf(++blank, "%hi%c", &info.addr, &end); | ||
415 | if (res < 1) { | ||
416 | dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); | ||
417 | return -EINVAL; | ||
418 | } | ||
419 | if (res > 1 && end != '\n') { | ||
420 | dev_err(dev, "%s: Extra parameters\n", "new_device"); | ||
421 | return -EINVAL; | ||
422 | } | ||
423 | |||
424 | if (info.addr < 0x03 || info.addr > 0x77) { | ||
425 | dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device", | ||
426 | info.addr); | ||
427 | return -EINVAL; | ||
428 | } | ||
429 | |||
430 | client = i2c_new_device(adap, &info); | ||
431 | if (!client) | ||
432 | return -EEXIST; | ||
433 | |||
434 | /* Keep track of the added device */ | ||
435 | mutex_lock(&core_lock); | ||
436 | list_add_tail(&client->detected, &userspace_devices); | ||
437 | mutex_unlock(&core_lock); | ||
438 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", | ||
439 | info.type, info.addr); | ||
440 | |||
441 | return count; | ||
442 | } | ||
443 | |||
444 | /* | ||
445 | * And of course let the users delete the devices they instantiated, if | ||
446 | * they got it wrong. This interface can only be used to delete devices | ||
447 | * instantiated by i2c_sysfs_new_device above. This guarantees that we | ||
448 | * don't delete devices to which some kernel code still has references. | ||
449 | * | ||
450 | * Parameter checking may look overzealous, but we really don't want | ||
451 | * the user to delete the wrong device. | ||
452 | */ | ||
453 | static ssize_t | ||
454 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | ||
455 | const char *buf, size_t count) | ||
456 | { | ||
457 | struct i2c_adapter *adap = to_i2c_adapter(dev); | ||
458 | struct i2c_client *client, *next; | ||
459 | unsigned short addr; | ||
460 | char end; | ||
461 | int res; | ||
462 | |||
463 | /* Parse parameters, reject extra parameters */ | ||
464 | res = sscanf(buf, "%hi%c", &addr, &end); | ||
465 | if (res < 1) { | ||
466 | dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); | ||
467 | return -EINVAL; | ||
468 | } | ||
469 | if (res > 1 && end != '\n') { | ||
470 | dev_err(dev, "%s: Extra parameters\n", "delete_device"); | ||
471 | return -EINVAL; | ||
472 | } | ||
473 | |||
474 | /* Make sure the device was added through sysfs */ | ||
475 | res = -ENOENT; | ||
476 | mutex_lock(&core_lock); | ||
477 | list_for_each_entry_safe(client, next, &userspace_devices, detected) { | ||
478 | if (client->addr == addr && client->adapter == adap) { | ||
479 | dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", | ||
480 | "delete_device", client->name, client->addr); | ||
481 | |||
482 | list_del(&client->detected); | ||
483 | i2c_unregister_device(client); | ||
484 | res = count; | ||
485 | break; | ||
486 | } | ||
487 | } | ||
488 | mutex_unlock(&core_lock); | ||
489 | |||
490 | if (res < 0) | ||
491 | dev_err(dev, "%s: Can't find device in list\n", | ||
492 | "delete_device"); | ||
493 | return res; | ||
494 | } | ||
495 | |||
396 | static struct device_attribute i2c_adapter_attrs[] = { | 496 | static struct device_attribute i2c_adapter_attrs[] = { |
397 | __ATTR(name, S_IRUGO, show_adapter_name, NULL), | 497 | __ATTR(name, S_IRUGO, show_adapter_name, NULL), |
498 | __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device), | ||
499 | __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device), | ||
398 | { }, | 500 | { }, |
399 | }; | 501 | }; |
400 | 502 | ||
@@ -408,7 +510,7 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter) | |||
408 | { | 510 | { |
409 | struct i2c_devinfo *devinfo; | 511 | struct i2c_devinfo *devinfo; |
410 | 512 | ||
411 | mutex_lock(&__i2c_board_lock); | 513 | down_read(&__i2c_board_lock); |
412 | list_for_each_entry(devinfo, &__i2c_board_list, list) { | 514 | list_for_each_entry(devinfo, &__i2c_board_list, list) { |
413 | if (devinfo->busnum == adapter->nr | 515 | if (devinfo->busnum == adapter->nr |
414 | && !i2c_new_device(adapter, | 516 | && !i2c_new_device(adapter, |
@@ -417,7 +519,7 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter) | |||
417 | "Can't create device at 0x%02x\n", | 519 | "Can't create device at 0x%02x\n", |
418 | devinfo->board_info.addr); | 520 | devinfo->board_info.addr); |
419 | } | 521 | } |
420 | mutex_unlock(&__i2c_board_lock); | 522 | up_read(&__i2c_board_lock); |
421 | } | 523 | } |
422 | 524 | ||
423 | static int i2c_do_add_adapter(struct device_driver *d, void *data) | 525 | static int i2c_do_add_adapter(struct device_driver *d, void *data) |
@@ -441,14 +543,12 @@ static int i2c_register_adapter(struct i2c_adapter *adap) | |||
441 | int res = 0, dummy; | 543 | int res = 0, dummy; |
442 | 544 | ||
443 | /* Can't register until after driver model init */ | 545 | /* Can't register until after driver model init */ |
444 | if (unlikely(WARN_ON(!i2c_bus_type.p))) | 546 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
445 | return -EAGAIN; | 547 | res = -EAGAIN; |
548 | goto out_list; | ||
549 | } | ||
446 | 550 | ||
447 | mutex_init(&adap->bus_lock); | 551 | mutex_init(&adap->bus_lock); |
448 | mutex_init(&adap->clist_lock); | ||
449 | INIT_LIST_HEAD(&adap->clients); | ||
450 | |||
451 | mutex_lock(&core_lock); | ||
452 | 552 | ||
453 | /* Set default timeout to 1 second if not already set */ | 553 | /* Set default timeout to 1 second if not already set */ |
454 | if (adap->timeout == 0) | 554 | if (adap->timeout == 0) |
@@ -463,21 +563,23 @@ static int i2c_register_adapter(struct i2c_adapter *adap) | |||
463 | 563 | ||
464 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); | 564 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
465 | 565 | ||
466 | /* create pre-declared device nodes for new-style drivers */ | 566 | /* create pre-declared device nodes */ |
467 | if (adap->nr < __i2c_first_dynamic_bus_num) | 567 | if (adap->nr < __i2c_first_dynamic_bus_num) |
468 | i2c_scan_static_board_info(adap); | 568 | i2c_scan_static_board_info(adap); |
469 | 569 | ||
470 | /* Notify drivers */ | 570 | /* Notify drivers */ |
571 | mutex_lock(&core_lock); | ||
471 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, | 572 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
472 | i2c_do_add_adapter); | 573 | i2c_do_add_adapter); |
473 | |||
474 | out_unlock: | ||
475 | mutex_unlock(&core_lock); | 574 | mutex_unlock(&core_lock); |
476 | return res; | 575 | |
576 | return 0; | ||
477 | 577 | ||
478 | out_list: | 578 | out_list: |
579 | mutex_lock(&core_lock); | ||
479 | idr_remove(&i2c_adapter_idr, adap->nr); | 580 | idr_remove(&i2c_adapter_idr, adap->nr); |
480 | goto out_unlock; | 581 | mutex_unlock(&core_lock); |
582 | return res; | ||
481 | } | 583 | } |
482 | 584 | ||
483 | /** | 585 | /** |
@@ -596,6 +698,14 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data) | |||
596 | return res; | 698 | return res; |
597 | } | 699 | } |
598 | 700 | ||
701 | static int __unregister_client(struct device *dev, void *dummy) | ||
702 | { | ||
703 | struct i2c_client *client = i2c_verify_client(dev); | ||
704 | if (client) | ||
705 | i2c_unregister_device(client); | ||
706 | return 0; | ||
707 | } | ||
708 | |||
599 | /** | 709 | /** |
600 | * i2c_del_adapter - unregister I2C adapter | 710 | * i2c_del_adapter - unregister I2C adapter |
601 | * @adap: the adapter being unregistered | 711 | * @adap: the adapter being unregistered |
@@ -606,46 +716,30 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data) | |||
606 | */ | 716 | */ |
607 | int i2c_del_adapter(struct i2c_adapter *adap) | 717 | int i2c_del_adapter(struct i2c_adapter *adap) |
608 | { | 718 | { |
609 | struct i2c_client *client, *_n; | ||
610 | int res = 0; | 719 | int res = 0; |
611 | 720 | struct i2c_adapter *found; | |
612 | mutex_lock(&core_lock); | ||
613 | 721 | ||
614 | /* First make sure that this adapter was ever added */ | 722 | /* First make sure that this adapter was ever added */ |
615 | if (idr_find(&i2c_adapter_idr, adap->nr) != adap) { | 723 | mutex_lock(&core_lock); |
724 | found = idr_find(&i2c_adapter_idr, adap->nr); | ||
725 | mutex_unlock(&core_lock); | ||
726 | if (found != adap) { | ||
616 | pr_debug("i2c-core: attempting to delete unregistered " | 727 | pr_debug("i2c-core: attempting to delete unregistered " |
617 | "adapter [%s]\n", adap->name); | 728 | "adapter [%s]\n", adap->name); |
618 | res = -EINVAL; | 729 | return -EINVAL; |
619 | goto out_unlock; | ||
620 | } | 730 | } |
621 | 731 | ||
622 | /* Tell drivers about this removal */ | 732 | /* Tell drivers about this removal */ |
733 | mutex_lock(&core_lock); | ||
623 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, | 734 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
624 | i2c_do_del_adapter); | 735 | i2c_do_del_adapter); |
736 | mutex_unlock(&core_lock); | ||
625 | if (res) | 737 | if (res) |
626 | goto out_unlock; | 738 | return res; |
627 | |||
628 | /* detach any active clients. This must be done first, because | ||
629 | * it can fail; in which case we give up. */ | ||
630 | list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) { | ||
631 | struct i2c_driver *driver; | ||
632 | |||
633 | driver = client->driver; | ||
634 | |||
635 | /* new style, follow standard driver model */ | ||
636 | if (!driver || is_newstyle_driver(driver)) { | ||
637 | i2c_unregister_device(client); | ||
638 | continue; | ||
639 | } | ||
640 | 739 | ||
641 | /* legacy drivers create and remove clients themselves */ | 740 | /* Detach any active clients. This can't fail, thus we do not |
642 | if ((res = driver->detach_client(client))) { | 741 | checking the returned value. */ |
643 | dev_err(&adap->dev, "detach_client failed for client " | 742 | res = device_for_each_child(&adap->dev, NULL, __unregister_client); |
644 | "[%s] at address 0x%02x\n", client->name, | ||
645 | client->addr); | ||
646 | goto out_unlock; | ||
647 | } | ||
648 | } | ||
649 | 743 | ||
650 | /* clean up the sysfs representation */ | 744 | /* clean up the sysfs representation */ |
651 | init_completion(&adap->dev_released); | 745 | init_completion(&adap->dev_released); |
@@ -655,7 +749,9 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
655 | wait_for_completion(&adap->dev_released); | 749 | wait_for_completion(&adap->dev_released); |
656 | 750 | ||
657 | /* free bus id */ | 751 | /* free bus id */ |
752 | mutex_lock(&core_lock); | ||
658 | idr_remove(&i2c_adapter_idr, adap->nr); | 753 | idr_remove(&i2c_adapter_idr, adap->nr); |
754 | mutex_unlock(&core_lock); | ||
659 | 755 | ||
660 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); | 756 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
661 | 757 | ||
@@ -663,9 +759,7 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
663 | added again */ | 759 | added again */ |
664 | memset(&adap->dev, 0, sizeof(adap->dev)); | 760 | memset(&adap->dev, 0, sizeof(adap->dev)); |
665 | 761 | ||
666 | out_unlock: | 762 | return 0; |
667 | mutex_unlock(&core_lock); | ||
668 | return res; | ||
669 | } | 763 | } |
670 | EXPORT_SYMBOL(i2c_del_adapter); | 764 | EXPORT_SYMBOL(i2c_del_adapter); |
671 | 765 | ||
@@ -688,11 +782,7 @@ static int __attach_adapter(struct device *dev, void *data) | |||
688 | 782 | ||
689 | /* | 783 | /* |
690 | * An i2c_driver is used with one or more i2c_client (device) nodes to access | 784 | * An i2c_driver is used with one or more i2c_client (device) nodes to access |
691 | * i2c slave chips, on a bus instance associated with some i2c_adapter. There | 785 | * i2c slave chips, on a bus instance associated with some i2c_adapter. |
692 | * are two models for binding the driver to its device: "new style" drivers | ||
693 | * follow the standard Linux driver model and just respond to probe() calls | ||
694 | * issued if the driver core sees they match(); "legacy" drivers create device | ||
695 | * nodes themselves. | ||
696 | */ | 786 | */ |
697 | 787 | ||
698 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) | 788 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
@@ -703,37 +793,26 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver) | |||
703 | if (unlikely(WARN_ON(!i2c_bus_type.p))) | 793 | if (unlikely(WARN_ON(!i2c_bus_type.p))) |
704 | return -EAGAIN; | 794 | return -EAGAIN; |
705 | 795 | ||
706 | /* new style driver methods can't mix with legacy ones */ | ||
707 | if (is_newstyle_driver(driver)) { | ||
708 | if (driver->detach_adapter || driver->detach_client) { | ||
709 | printk(KERN_WARNING | ||
710 | "i2c-core: driver [%s] is confused\n", | ||
711 | driver->driver.name); | ||
712 | return -EINVAL; | ||
713 | } | ||
714 | } | ||
715 | |||
716 | /* add the driver to the list of i2c drivers in the driver core */ | 796 | /* add the driver to the list of i2c drivers in the driver core */ |
717 | driver->driver.owner = owner; | 797 | driver->driver.owner = owner; |
718 | driver->driver.bus = &i2c_bus_type; | 798 | driver->driver.bus = &i2c_bus_type; |
719 | 799 | ||
720 | /* for new style drivers, when registration returns the driver core | 800 | /* When registration returns, the driver core |
721 | * will have called probe() for all matching-but-unbound devices. | 801 | * will have called probe() for all matching-but-unbound devices. |
722 | */ | 802 | */ |
723 | res = driver_register(&driver->driver); | 803 | res = driver_register(&driver->driver); |
724 | if (res) | 804 | if (res) |
725 | return res; | 805 | return res; |
726 | 806 | ||
727 | mutex_lock(&core_lock); | ||
728 | |||
729 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); | 807 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
730 | 808 | ||
731 | INIT_LIST_HEAD(&driver->clients); | 809 | INIT_LIST_HEAD(&driver->clients); |
732 | /* Walk the adapters that are already present */ | 810 | /* Walk the adapters that are already present */ |
811 | mutex_lock(&core_lock); | ||
733 | class_for_each_device(&i2c_adapter_class, NULL, driver, | 812 | class_for_each_device(&i2c_adapter_class, NULL, driver, |
734 | __attach_adapter); | 813 | __attach_adapter); |
735 | |||
736 | mutex_unlock(&core_lock); | 814 | mutex_unlock(&core_lock); |
815 | |||
737 | return 0; | 816 | return 0; |
738 | } | 817 | } |
739 | EXPORT_SYMBOL(i2c_register_driver); | 818 | EXPORT_SYMBOL(i2c_register_driver); |
@@ -753,32 +832,11 @@ static int __detach_adapter(struct device *dev, void *data) | |||
753 | i2c_unregister_device(client); | 832 | i2c_unregister_device(client); |
754 | } | 833 | } |
755 | 834 | ||
756 | if (is_newstyle_driver(driver)) | ||
757 | return 0; | ||
758 | |||
759 | /* Have a look at each adapter, if clients of this driver are still | ||
760 | * attached. If so, detach them to be able to kill the driver | ||
761 | * afterwards. | ||
762 | */ | ||
763 | if (driver->detach_adapter) { | 835 | if (driver->detach_adapter) { |
764 | if (driver->detach_adapter(adapter)) | 836 | if (driver->detach_adapter(adapter)) |
765 | dev_err(&adapter->dev, | 837 | dev_err(&adapter->dev, |
766 | "detach_adapter failed for driver [%s]\n", | 838 | "detach_adapter failed for driver [%s]\n", |
767 | driver->driver.name); | 839 | driver->driver.name); |
768 | } else { | ||
769 | struct i2c_client *client, *_n; | ||
770 | |||
771 | list_for_each_entry_safe(client, _n, &adapter->clients, list) { | ||
772 | if (client->driver != driver) | ||
773 | continue; | ||
774 | dev_dbg(&adapter->dev, | ||
775 | "detaching client [%s] at 0x%02x\n", | ||
776 | client->name, client->addr); | ||
777 | if (driver->detach_client(client)) | ||
778 | dev_err(&adapter->dev, "detach_client " | ||
779 | "failed for client [%s] at 0x%02x\n", | ||
780 | client->name, client->addr); | ||
781 | } | ||
782 | } | 840 | } |
783 | 841 | ||
784 | return 0; | 842 | return 0; |
@@ -792,14 +850,12 @@ static int __detach_adapter(struct device *dev, void *data) | |||
792 | void i2c_del_driver(struct i2c_driver *driver) | 850 | void i2c_del_driver(struct i2c_driver *driver) |
793 | { | 851 | { |
794 | mutex_lock(&core_lock); | 852 | mutex_lock(&core_lock); |
795 | |||
796 | class_for_each_device(&i2c_adapter_class, NULL, driver, | 853 | class_for_each_device(&i2c_adapter_class, NULL, driver, |
797 | __detach_adapter); | 854 | __detach_adapter); |
855 | mutex_unlock(&core_lock); | ||
798 | 856 | ||
799 | driver_unregister(&driver->driver); | 857 | driver_unregister(&driver->driver); |
800 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); | 858 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
801 | |||
802 | mutex_unlock(&core_lock); | ||
803 | } | 859 | } |
804 | EXPORT_SYMBOL(i2c_del_driver); | 860 | EXPORT_SYMBOL(i2c_del_driver); |
805 | 861 | ||
@@ -820,86 +876,6 @@ static int i2c_check_addr(struct i2c_adapter *adapter, int addr) | |||
820 | return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr); | 876 | return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr); |
821 | } | 877 | } |
822 | 878 | ||
823 | int i2c_attach_client(struct i2c_client *client) | ||
824 | { | ||
825 | struct i2c_adapter *adapter = client->adapter; | ||
826 | int res; | ||
827 | |||
828 | /* Check for address business */ | ||
829 | res = i2c_check_addr(adapter, client->addr); | ||
830 | if (res) | ||
831 | return res; | ||
832 | |||
833 | client->dev.parent = &client->adapter->dev; | ||
834 | client->dev.bus = &i2c_bus_type; | ||
835 | |||
836 | if (client->driver) | ||
837 | client->dev.driver = &client->driver->driver; | ||
838 | |||
839 | if (client->driver && !is_newstyle_driver(client->driver)) { | ||
840 | client->dev.release = i2c_client_release; | ||
841 | dev_set_uevent_suppress(&client->dev, 1); | ||
842 | } else | ||
843 | client->dev.release = i2c_client_dev_release; | ||
844 | |||
845 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter), | ||
846 | client->addr); | ||
847 | res = device_register(&client->dev); | ||
848 | if (res) | ||
849 | goto out_err; | ||
850 | |||
851 | mutex_lock(&adapter->clist_lock); | ||
852 | list_add_tail(&client->list, &adapter->clients); | ||
853 | mutex_unlock(&adapter->clist_lock); | ||
854 | |||
855 | dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n", | ||
856 | client->name, dev_name(&client->dev)); | ||
857 | |||
858 | if (adapter->client_register) { | ||
859 | if (adapter->client_register(client)) { | ||
860 | dev_dbg(&adapter->dev, "client_register " | ||
861 | "failed for client [%s] at 0x%02x\n", | ||
862 | client->name, client->addr); | ||
863 | } | ||
864 | } | ||
865 | |||
866 | return 0; | ||
867 | |||
868 | out_err: | ||
869 | dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " | ||
870 | "(%d)\n", client->name, client->addr, res); | ||
871 | return res; | ||
872 | } | ||
873 | EXPORT_SYMBOL(i2c_attach_client); | ||
874 | |||
875 | int i2c_detach_client(struct i2c_client *client) | ||
876 | { | ||
877 | struct i2c_adapter *adapter = client->adapter; | ||
878 | int res = 0; | ||
879 | |||
880 | if (adapter->client_unregister) { | ||
881 | res = adapter->client_unregister(client); | ||
882 | if (res) { | ||
883 | dev_err(&client->dev, | ||
884 | "client_unregister [%s] failed, " | ||
885 | "client not detached\n", client->name); | ||
886 | goto out; | ||
887 | } | ||
888 | } | ||
889 | |||
890 | mutex_lock(&adapter->clist_lock); | ||
891 | list_del(&client->list); | ||
892 | mutex_unlock(&adapter->clist_lock); | ||
893 | |||
894 | init_completion(&client->released); | ||
895 | device_unregister(&client->dev); | ||
896 | wait_for_completion(&client->released); | ||
897 | |||
898 | out: | ||
899 | return res; | ||
900 | } | ||
901 | EXPORT_SYMBOL(i2c_detach_client); | ||
902 | |||
903 | /** | 879 | /** |
904 | * i2c_use_client - increments the reference count of the i2c client structure | 880 | * i2c_use_client - increments the reference count of the i2c client structure |
905 | * @client: the client being referenced | 881 | * @client: the client being referenced |
@@ -1129,144 +1105,7 @@ EXPORT_SYMBOL(i2c_master_recv); | |||
1129 | * Will not work for 10-bit addresses! | 1105 | * Will not work for 10-bit addresses! |
1130 | * ---------------------------------------------------- | 1106 | * ---------------------------------------------------- |
1131 | */ | 1107 | */ |
1132 | static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind, | ||
1133 | int (*found_proc) (struct i2c_adapter *, int, int)) | ||
1134 | { | ||
1135 | int err; | ||
1136 | |||
1137 | /* Make sure the address is valid */ | ||
1138 | if (addr < 0x03 || addr > 0x77) { | ||
1139 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", | ||
1140 | addr); | ||
1141 | return -EINVAL; | ||
1142 | } | ||
1143 | |||
1144 | /* Skip if already in use */ | ||
1145 | if (i2c_check_addr(adapter, addr)) | ||
1146 | return 0; | ||
1147 | |||
1148 | /* Make sure there is something at this address, unless forced */ | ||
1149 | if (kind < 0) { | ||
1150 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, | ||
1151 | I2C_SMBUS_QUICK, NULL) < 0) | ||
1152 | return 0; | ||
1153 | |||
1154 | /* prevent 24RF08 corruption */ | ||
1155 | if ((addr & ~0x0f) == 0x50) | ||
1156 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, | ||
1157 | I2C_SMBUS_QUICK, NULL); | ||
1158 | } | ||
1159 | |||
1160 | /* Finally call the custom detection function */ | ||
1161 | err = found_proc(adapter, addr, kind); | ||
1162 | /* -ENODEV can be returned if there is a chip at the given address | ||
1163 | but it isn't supported by this chip driver. We catch it here as | ||
1164 | this isn't an error. */ | ||
1165 | if (err == -ENODEV) | ||
1166 | err = 0; | ||
1167 | |||
1168 | if (err) | ||
1169 | dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n", | ||
1170 | addr, err); | ||
1171 | return err; | ||
1172 | } | ||
1173 | |||
1174 | int i2c_probe(struct i2c_adapter *adapter, | ||
1175 | const struct i2c_client_address_data *address_data, | ||
1176 | int (*found_proc) (struct i2c_adapter *, int, int)) | ||
1177 | { | ||
1178 | int i, err; | ||
1179 | int adap_id = i2c_adapter_id(adapter); | ||
1180 | |||
1181 | /* Force entries are done first, and are not affected by ignore | ||
1182 | entries */ | ||
1183 | if (address_data->forces) { | ||
1184 | const unsigned short * const *forces = address_data->forces; | ||
1185 | int kind; | ||
1186 | |||
1187 | for (kind = 0; forces[kind]; kind++) { | ||
1188 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; | ||
1189 | i += 2) { | ||
1190 | if (forces[kind][i] == adap_id | ||
1191 | || forces[kind][i] == ANY_I2C_BUS) { | ||
1192 | dev_dbg(&adapter->dev, "found force " | ||
1193 | "parameter for adapter %d, " | ||
1194 | "addr 0x%02x, kind %d\n", | ||
1195 | adap_id, forces[kind][i + 1], | ||
1196 | kind); | ||
1197 | err = i2c_probe_address(adapter, | ||
1198 | forces[kind][i + 1], | ||
1199 | kind, found_proc); | ||
1200 | if (err) | ||
1201 | return err; | ||
1202 | } | ||
1203 | } | ||
1204 | } | ||
1205 | } | ||
1206 | |||
1207 | /* Stop here if we can't use SMBUS_QUICK */ | ||
1208 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { | ||
1209 | if (address_data->probe[0] == I2C_CLIENT_END | ||
1210 | && address_data->normal_i2c[0] == I2C_CLIENT_END) | ||
1211 | return 0; | ||
1212 | |||
1213 | dev_dbg(&adapter->dev, "SMBus Quick command not supported, " | ||
1214 | "can't probe for chips\n"); | ||
1215 | return -EOPNOTSUPP; | ||
1216 | } | ||
1217 | |||
1218 | /* Probe entries are done second, and are not affected by ignore | ||
1219 | entries either */ | ||
1220 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { | ||
1221 | if (address_data->probe[i] == adap_id | ||
1222 | || address_data->probe[i] == ANY_I2C_BUS) { | ||
1223 | dev_dbg(&adapter->dev, "found probe parameter for " | ||
1224 | "adapter %d, addr 0x%02x\n", adap_id, | ||
1225 | address_data->probe[i + 1]); | ||
1226 | err = i2c_probe_address(adapter, | ||
1227 | address_data->probe[i + 1], | ||
1228 | -1, found_proc); | ||
1229 | if (err) | ||
1230 | return err; | ||
1231 | } | ||
1232 | } | ||
1233 | |||
1234 | /* Normal entries are done last, unless shadowed by an ignore entry */ | ||
1235 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { | ||
1236 | int j, ignore; | ||
1237 | |||
1238 | ignore = 0; | ||
1239 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; | ||
1240 | j += 2) { | ||
1241 | if ((address_data->ignore[j] == adap_id || | ||
1242 | address_data->ignore[j] == ANY_I2C_BUS) | ||
1243 | && address_data->ignore[j + 1] | ||
1244 | == address_data->normal_i2c[i]) { | ||
1245 | dev_dbg(&adapter->dev, "found ignore " | ||
1246 | "parameter for adapter %d, " | ||
1247 | "addr 0x%02x\n", adap_id, | ||
1248 | address_data->ignore[j + 1]); | ||
1249 | ignore = 1; | ||
1250 | break; | ||
1251 | } | ||
1252 | } | ||
1253 | if (ignore) | ||
1254 | continue; | ||
1255 | |||
1256 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " | ||
1257 | "addr 0x%02x\n", adap_id, | ||
1258 | address_data->normal_i2c[i]); | ||
1259 | err = i2c_probe_address(adapter, address_data->normal_i2c[i], | ||
1260 | -1, found_proc); | ||
1261 | if (err) | ||
1262 | return err; | ||
1263 | } | ||
1264 | |||
1265 | return 0; | ||
1266 | } | ||
1267 | EXPORT_SYMBOL(i2c_probe); | ||
1268 | 1108 | ||
1269 | /* Separate detection function for new-style drivers */ | ||
1270 | static int i2c_detect_address(struct i2c_client *temp_client, int kind, | 1109 | static int i2c_detect_address(struct i2c_client *temp_client, int kind, |
1271 | struct i2c_driver *driver) | 1110 | struct i2c_driver *driver) |
1272 | { | 1111 | { |
diff --git a/drivers/i2c/i2c-core.h b/drivers/i2c/i2c-core.h index cd5bff874855..9f9c57ff6708 100644 --- a/drivers/i2c/i2c-core.h +++ b/drivers/i2c/i2c-core.h | |||
@@ -16,6 +16,8 @@ | |||
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | */ | 17 | */ |
18 | 18 | ||
19 | #include <linux/rwsem.h> | ||
20 | |||
19 | struct i2c_devinfo { | 21 | struct i2c_devinfo { |
20 | struct list_head list; | 22 | struct list_head list; |
21 | int busnum; | 23 | int busnum; |
@@ -25,7 +27,7 @@ struct i2c_devinfo { | |||
25 | /* board_lock protects board_list and first_dynamic_bus_num. | 27 | /* board_lock protects board_list and first_dynamic_bus_num. |
26 | * only i2c core components are allowed to use these symbols. | 28 | * only i2c core components are allowed to use these symbols. |
27 | */ | 29 | */ |
28 | extern struct mutex __i2c_board_lock; | 30 | extern struct rw_semaphore __i2c_board_lock; |
29 | extern struct list_head __i2c_board_list; | 31 | extern struct list_head __i2c_board_list; |
30 | extern int __i2c_first_dynamic_bus_num; | 32 | extern int __i2c_first_dynamic_bus_num; |
31 | 33 | ||