diff options
author | Jean Delvare <khali@linux-fr.org> | 2009-12-06 11:06:27 -0500 |
---|---|---|
committer | Jean Delvare <khali@linux-fr.org> | 2009-12-06 11:06:27 -0500 |
commit | 69b0089a6750a0435570df3ba8456c77b352af55 (patch) | |
tree | 6d8826c7fec2ff30eb294dd04a1677079158afab /drivers/i2c | |
parent | 7e2193a8f942a80eef4ae87e3850615450966d0a (diff) |
i2c: Refactor for_each callbacks
Functions i2c_do_add_adapter() and __attach_adapter() do essentially
the same thing, differing only in how the parameters are passed. Same
for i2c_do_add_adapter() and __detach_adapter(). Introduce wrappers to
normalize the parameters, so that we do not have to duplicate the
code.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Cc: David Brownell <dbrownell@users.sourceforge.net>
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/i2c-core.c | 70 |
1 files changed, 22 insertions, 48 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 37df0f14c38..4f34823e86b 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -558,11 +558,9 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter) | |||
558 | up_read(&__i2c_board_lock); | 558 | up_read(&__i2c_board_lock); |
559 | } | 559 | } |
560 | 560 | ||
561 | static int i2c_do_add_adapter(struct device_driver *d, void *data) | 561 | static int i2c_do_add_adapter(struct i2c_driver *driver, |
562 | struct i2c_adapter *adap) | ||
562 | { | 563 | { |
563 | struct i2c_driver *driver = to_i2c_driver(d); | ||
564 | struct i2c_adapter *adap = data; | ||
565 | |||
566 | /* Detect supported devices on that bus, and instantiate them */ | 564 | /* Detect supported devices on that bus, and instantiate them */ |
567 | i2c_detect(adap, driver); | 565 | i2c_detect(adap, driver); |
568 | 566 | ||
@@ -574,6 +572,11 @@ static int i2c_do_add_adapter(struct device_driver *d, void *data) | |||
574 | return 0; | 572 | return 0; |
575 | } | 573 | } |
576 | 574 | ||
575 | static int __process_new_adapter(struct device_driver *d, void *data) | ||
576 | { | ||
577 | return i2c_do_add_adapter(to_i2c_driver(d), data); | ||
578 | } | ||
579 | |||
577 | static int i2c_register_adapter(struct i2c_adapter *adap) | 580 | static int i2c_register_adapter(struct i2c_adapter *adap) |
578 | { | 581 | { |
579 | int res = 0, dummy; | 582 | int res = 0, dummy; |
@@ -614,7 +617,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap) | |||
614 | /* Notify drivers */ | 617 | /* Notify drivers */ |
615 | mutex_lock(&core_lock); | 618 | mutex_lock(&core_lock); |
616 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, | 619 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
617 | i2c_do_add_adapter); | 620 | __process_new_adapter); |
618 | mutex_unlock(&core_lock); | 621 | mutex_unlock(&core_lock); |
619 | 622 | ||
620 | return 0; | 623 | return 0; |
@@ -715,10 +718,9 @@ retry: | |||
715 | } | 718 | } |
716 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | 719 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); |
717 | 720 | ||
718 | static int i2c_do_del_adapter(struct device_driver *d, void *data) | 721 | static int i2c_do_del_adapter(struct i2c_driver *driver, |
722 | struct i2c_adapter *adapter) | ||
719 | { | 723 | { |
720 | struct i2c_driver *driver = to_i2c_driver(d); | ||
721 | struct i2c_adapter *adapter = data; | ||
722 | struct i2c_client *client, *_n; | 724 | struct i2c_client *client, *_n; |
723 | int res; | 725 | int res; |
724 | 726 | ||
@@ -750,6 +752,11 @@ static int __unregister_client(struct device *dev, void *dummy) | |||
750 | return 0; | 752 | return 0; |
751 | } | 753 | } |
752 | 754 | ||
755 | static int __process_removed_adapter(struct device_driver *d, void *data) | ||
756 | { | ||
757 | return i2c_do_del_adapter(to_i2c_driver(d), data); | ||
758 | } | ||
759 | |||
753 | /** | 760 | /** |
754 | * i2c_del_adapter - unregister I2C adapter | 761 | * i2c_del_adapter - unregister I2C adapter |
755 | * @adap: the adapter being unregistered | 762 | * @adap: the adapter being unregistered |
@@ -777,7 +784,7 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
777 | /* Tell drivers about this removal */ | 784 | /* Tell drivers about this removal */ |
778 | mutex_lock(&core_lock); | 785 | mutex_lock(&core_lock); |
779 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, | 786 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
780 | i2c_do_del_adapter); | 787 | __process_removed_adapter); |
781 | mutex_unlock(&core_lock); | 788 | mutex_unlock(&core_lock); |
782 | if (res) | 789 | if (res) |
783 | return res; | 790 | return res; |
@@ -826,22 +833,11 @@ EXPORT_SYMBOL(i2c_del_adapter); | |||
826 | 833 | ||
827 | /* ------------------------------------------------------------------------- */ | 834 | /* ------------------------------------------------------------------------- */ |
828 | 835 | ||
829 | static int __attach_adapter(struct device *dev, void *data) | 836 | static int __process_new_driver(struct device *dev, void *data) |
830 | { | 837 | { |
831 | struct i2c_adapter *adapter; | ||
832 | struct i2c_driver *driver = data; | ||
833 | |||
834 | if (dev->type != &i2c_adapter_type) | 838 | if (dev->type != &i2c_adapter_type) |
835 | return 0; | 839 | return 0; |
836 | adapter = to_i2c_adapter(dev); | 840 | return i2c_do_add_adapter(data, to_i2c_adapter(dev)); |
837 | |||
838 | i2c_detect(adapter, driver); | ||
839 | |||
840 | /* Legacy drivers scan i2c busses directly */ | ||
841 | if (driver->attach_adapter) | ||
842 | driver->attach_adapter(adapter); | ||
843 | |||
844 | return 0; | ||
845 | } | 841 | } |
846 | 842 | ||
847 | /* | 843 | /* |
@@ -873,40 +869,18 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver) | |||
873 | INIT_LIST_HEAD(&driver->clients); | 869 | INIT_LIST_HEAD(&driver->clients); |
874 | /* Walk the adapters that are already present */ | 870 | /* Walk the adapters that are already present */ |
875 | mutex_lock(&core_lock); | 871 | mutex_lock(&core_lock); |
876 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter); | 872 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver); |
877 | mutex_unlock(&core_lock); | 873 | mutex_unlock(&core_lock); |
878 | 874 | ||
879 | return 0; | 875 | return 0; |
880 | } | 876 | } |
881 | EXPORT_SYMBOL(i2c_register_driver); | 877 | EXPORT_SYMBOL(i2c_register_driver); |
882 | 878 | ||
883 | static int __detach_adapter(struct device *dev, void *data) | 879 | static int __process_removed_driver(struct device *dev, void *data) |
884 | { | 880 | { |
885 | struct i2c_adapter *adapter; | ||
886 | struct i2c_driver *driver = data; | ||
887 | struct i2c_client *client, *_n; | ||
888 | |||
889 | if (dev->type != &i2c_adapter_type) | 881 | if (dev->type != &i2c_adapter_type) |
890 | return 0; | 882 | return 0; |
891 | adapter = to_i2c_adapter(dev); | 883 | return i2c_do_del_adapter(data, to_i2c_adapter(dev)); |
892 | |||
893 | /* Remove the devices we created ourselves as the result of hardware | ||
894 | * probing (using a driver's detect method) */ | ||
895 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { | ||
896 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", | ||
897 | client->name, client->addr); | ||
898 | list_del(&client->detected); | ||
899 | i2c_unregister_device(client); | ||
900 | } | ||
901 | |||
902 | if (driver->detach_adapter) { | ||
903 | if (driver->detach_adapter(adapter)) | ||
904 | dev_err(&adapter->dev, | ||
905 | "detach_adapter failed for driver [%s]\n", | ||
906 | driver->driver.name); | ||
907 | } | ||
908 | |||
909 | return 0; | ||
910 | } | 884 | } |
911 | 885 | ||
912 | /** | 886 | /** |
@@ -917,7 +891,7 @@ static int __detach_adapter(struct device *dev, void *data) | |||
917 | void i2c_del_driver(struct i2c_driver *driver) | 891 | void i2c_del_driver(struct i2c_driver *driver) |
918 | { | 892 | { |
919 | mutex_lock(&core_lock); | 893 | mutex_lock(&core_lock); |
920 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter); | 894 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver); |
921 | mutex_unlock(&core_lock); | 895 | mutex_unlock(&core_lock); |
922 | 896 | ||
923 | driver_unregister(&driver->driver); | 897 | driver_unregister(&driver->driver); |