summaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2020-10-07 11:50:52 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2020-10-09 08:48:02 +0200
commit8ef0ae4842c1cabf0e6fe00a26d15e3b3b942da8 (patch)
tree229b1029e86d14cd2dc1b321d91f0b9b68124a96 /drivers
parent578ba96236e155311c45bcab3536541e7532c41a (diff)
downloadbarebox-8ef0ae4842c1cabf0e6fe00a26d15e3b3b942da8.tar.gz
barebox-8ef0ae4842c1cabf0e6fe00a26d15e3b3b942da8.tar.xz
driver: introduce less error-prone dev_get_drvdata alternative
We use dev_get_drvdata to get the driver match data associated with a device. This has two shortcomings: - Linux has dev_get_drvdata too, which returns a private pointer for driver specific info to associate with a device. We use dev->priv (or more often container_of) for that in barebox instead - It nearly always involves a cast to a double pointer, which is error-prone as size and alignment match need to be ensured on the programmer's part and can easily be gotten wrong: enum dev_type type; dev_get_drvdata(dev, (const void **)&type); // UB! Add a new function that instead of using a double pointer argument, returns the pointer directly: - For normal pointer driver data, no cast is necessary - For integer driver data casted to a pointer for storage, the cast is still necessary, but it's only a single pointer this way Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/base/driver.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 412db6c406..3205bbc3c3 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -500,3 +500,14 @@ int dev_get_drvdata(struct device_d *dev, const void **data)
return -ENODEV;
}
+
+const void *device_get_match_data(struct device_d *dev)
+{
+ if (dev->of_id_entry)
+ return dev->of_id_entry->data;
+
+ if (dev->id_entry)
+ return (void *)dev->id_entry->driver_data;
+
+ return NULL;
+}