blob: 8a06724e029e4fd1f8cee862b95bd7101839ed20 [file] [log] [blame]
Paul Mackerras2e686bc2005-10-06 13:22:17 +10001#include <linux/string.h>
2#include <linux/kernel.h>
3#include <linux/init.h>
4#include <linux/module.h>
5#include <linux/mod_devicetable.h>
Paul Mackerras734d6522005-10-31 13:57:01 +11006#include <linux/slab.h>
7
Paul Mackerras2e686bc2005-10-06 13:22:17 +10008#include <asm/errno.h>
9#include <asm/of_device.h>
10
11/**
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110012 * of_match_node - Tell if an device_node has a matching of_match structure
13 * @ids: array of of device match structures to search in
14 * @node: the of device structure to match against
15 *
16 * Low level utility function used by device matching.
17 */
18const struct of_device_id *of_match_node(const struct of_device_id *matches,
19 const struct device_node *node)
20{
21 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
22 int match = 1;
23 if (matches->name[0])
24 match &= node->name
25 && !strcmp(matches->name, node->name);
26 if (matches->type[0])
27 match &= node->type
28 && !strcmp(matches->type, node->type);
29 if (matches->compatible[0])
30 match &= device_is_compatible(node,
31 matches->compatible);
32 if (match)
33 return matches;
34 matches++;
35 }
36 return NULL;
37}
38
39/**
Paul Mackerras2e686bc2005-10-06 13:22:17 +100040 * of_match_device - Tell if an of_device structure has a matching
41 * of_match structure
42 * @ids: array of of device match structures to search in
43 * @dev: the of device structure to match against
44 *
45 * Used by a driver to check whether an of_device present in the
46 * system is in its list of supported devices.
47 */
48const struct of_device_id *of_match_device(const struct of_device_id *matches,
49 const struct of_device *dev)
50{
51 if (!dev->node)
52 return NULL;
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110053 return of_match_node(matches, dev->node);
Paul Mackerras2e686bc2005-10-06 13:22:17 +100054}
55
56struct of_device *of_dev_get(struct of_device *dev)
57{
58 struct device *tmp;
59
60 if (!dev)
61 return NULL;
62 tmp = get_device(&dev->dev);
63 if (tmp)
64 return to_of_device(tmp);
65 else
66 return NULL;
67}
68
69void of_dev_put(struct of_device *dev)
70{
71 if (dev)
72 put_device(&dev->dev);
73}
74
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110075static ssize_t dev_show_devspec(struct device *dev,
76 struct device_attribute *attr, char *buf)
Paul Mackerras2e686bc2005-10-06 13:22:17 +100077{
78 struct of_device *ofdev;
79
80 ofdev = to_of_device(dev);
81 return sprintf(buf, "%s", ofdev->node->full_name);
82}
83
84static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
85
86/**
87 * of_release_dev - free an of device structure when all users of it are finished.
88 * @dev: device that's been disconnected
89 *
90 * Will be called only by the device core when all users of this of device are
91 * done.
92 */
93void of_release_dev(struct device *dev)
94{
95 struct of_device *ofdev;
96
97 ofdev = to_of_device(dev);
98 of_node_put(ofdev->node);
99 kfree(ofdev);
100}
101
102int of_device_register(struct of_device *ofdev)
103{
104 int rc;
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000105
106 BUG_ON(ofdev->node == NULL);
107
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000108 rc = device_register(&ofdev->dev);
109 if (rc)
110 return rc;
111
112 device_create_file(&ofdev->dev, &dev_attr_devspec);
113
114 return 0;
115}
116
117void of_device_unregister(struct of_device *ofdev)
118{
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000119 device_remove_file(&ofdev->dev, &dev_attr_devspec);
120
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000121 device_unregister(&ofdev->dev);
122}
123
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000124
Grant Likelyd8594d62006-12-04 17:29:12 -0700125EXPORT_SYMBOL(of_match_node);
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000126EXPORT_SYMBOL(of_match_device);
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000127EXPORT_SYMBOL(of_device_register);
128EXPORT_SYMBOL(of_device_unregister);
129EXPORT_SYMBOL(of_dev_get);
130EXPORT_SYMBOL(of_dev_put);
Paul Mackerras2e686bc2005-10-06 13:22:17 +1000131EXPORT_SYMBOL(of_release_dev);