]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
i2c: implement i2c_verify_adapter
authorStephen Warren <swarren@nvidia.com>
Tue, 17 Apr 2012 18:43:33 +0000 (12:43 -0600)
committerWolfram Sang <w.sang@pengutronix.de>
Sat, 12 May 2012 12:28:16 +0000 (14:28 +0200)
This converts a struct device * to a struct i2c_adapter * while verifying
that the device really is an I2C adapter. Just like i2c_verify_client.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Wolfram Sang <w.sang@pengutronix.de>
drivers/i2c/i2c-core.c
include/linux/i2c.h

index feb7dc359186495ead9dbff484e0ab76507eff0c..a6ad32bc0a96d633629f5a2a4cd85cdf3420c57d 100644 (file)
@@ -772,6 +772,23 @@ struct device_type i2c_adapter_type = {
 };
 EXPORT_SYMBOL_GPL(i2c_adapter_type);
 
+/**
+ * i2c_verify_adapter - return parameter as i2c_adapter or NULL
+ * @dev: device, probably from some driver model iterator
+ *
+ * When traversing the driver model tree, perhaps using driver model
+ * iterators like @device_for_each_child(), you can't assume very much
+ * about the nodes you find.  Use this function to avoid oopses caused
+ * by wrongly treating some non-I2C device as an i2c_adapter.
+ */
+struct i2c_adapter *i2c_verify_adapter(struct device *dev)
+{
+       return (dev->type == &i2c_adapter_type)
+                       ? to_i2c_adapter(dev)
+                       : NULL;
+}
+EXPORT_SYMBOL(i2c_verify_adapter);
+
 #ifdef CONFIG_I2C_COMPAT
 static struct class_compat *i2c_adapter_compat_class;
 #endif
index 195d8b3d9cfbf9b9363367f5604aaec029c68e20..b66cb601435fa732388a3f2c79b3b1af00ebbc4b 100644 (file)
@@ -232,6 +232,7 @@ struct i2c_client {
 #define to_i2c_client(d) container_of(d, struct i2c_client, dev)
 
 extern struct i2c_client *i2c_verify_client(struct device *dev);
+extern struct i2c_adapter *i2c_verify_adapter(struct device *dev);
 
 static inline struct i2c_client *kobj_to_i2c_client(struct kobject *kobj)
 {