David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1 | #include <linux/config.h> |
| 2 | #include <linux/string.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/init.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/mod_devicetable.h> |
| 7 | #include <linux/slab.h> |
| 8 | |
| 9 | #include <asm/errno.h> |
| 10 | #include <asm/of_device.h> |
| 11 | |
| 12 | /** |
| 13 | * of_match_device - Tell if an of_device structure has a matching |
| 14 | * of_match structure |
| 15 | * @ids: array of of device match structures to search in |
| 16 | * @dev: the of device structure to match against |
| 17 | * |
| 18 | * Used by a driver to check whether an of_device present in the |
| 19 | * system is in its list of supported devices. |
| 20 | */ |
| 21 | const struct of_device_id *of_match_device(const struct of_device_id *matches, |
| 22 | const struct of_device *dev) |
| 23 | { |
| 24 | if (!dev->node) |
| 25 | return NULL; |
| 26 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
| 27 | int match = 1; |
| 28 | if (matches->name[0]) |
| 29 | match &= dev->node->name |
| 30 | && !strcmp(matches->name, dev->node->name); |
| 31 | if (matches->type[0]) |
| 32 | match &= dev->node->type |
| 33 | && !strcmp(matches->type, dev->node->type); |
| 34 | if (matches->compatible[0]) |
| 35 | match &= of_device_is_compatible(dev->node, |
| 36 | matches->compatible); |
| 37 | if (match) |
| 38 | return matches; |
| 39 | matches++; |
| 40 | } |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) |
| 45 | { |
| 46 | struct of_device * of_dev = to_of_device(dev); |
| 47 | struct of_platform_driver * of_drv = to_of_platform_driver(drv); |
| 48 | const struct of_device_id * matches = of_drv->match_table; |
| 49 | |
| 50 | if (!matches) |
| 51 | return 0; |
| 52 | |
| 53 | return of_match_device(matches, of_dev) != NULL; |
| 54 | } |
| 55 | |
| 56 | struct 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 | |
| 69 | void of_dev_put(struct of_device *dev) |
| 70 | { |
| 71 | if (dev) |
| 72 | put_device(&dev->dev); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static int of_device_probe(struct device *dev) |
| 77 | { |
| 78 | int error = -ENODEV; |
| 79 | struct of_platform_driver *drv; |
| 80 | struct of_device *of_dev; |
| 81 | const struct of_device_id *match; |
| 82 | |
| 83 | drv = to_of_platform_driver(dev->driver); |
| 84 | of_dev = to_of_device(dev); |
| 85 | |
| 86 | if (!drv->probe) |
| 87 | return error; |
| 88 | |
| 89 | of_dev_get(of_dev); |
| 90 | |
| 91 | match = of_match_device(drv->match_table, of_dev); |
| 92 | if (match) |
| 93 | error = drv->probe(of_dev, match); |
| 94 | if (error) |
| 95 | of_dev_put(of_dev); |
| 96 | |
| 97 | return error; |
| 98 | } |
| 99 | |
| 100 | static int of_device_remove(struct device *dev) |
| 101 | { |
| 102 | struct of_device * of_dev = to_of_device(dev); |
| 103 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 104 | |
| 105 | if (dev->driver && drv->remove) |
| 106 | drv->remove(of_dev); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int of_device_suspend(struct device *dev, pm_message_t state) |
| 111 | { |
| 112 | struct of_device * of_dev = to_of_device(dev); |
| 113 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 114 | int error = 0; |
| 115 | |
| 116 | if (dev->driver && drv->suspend) |
| 117 | error = drv->suspend(of_dev, state); |
| 118 | return error; |
| 119 | } |
| 120 | |
| 121 | static int of_device_resume(struct device * dev) |
| 122 | { |
| 123 | struct of_device * of_dev = to_of_device(dev); |
| 124 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 125 | int error = 0; |
| 126 | |
| 127 | if (dev->driver && drv->resume) |
| 128 | error = drv->resume(of_dev); |
| 129 | return error; |
| 130 | } |
| 131 | |
| 132 | #ifdef CONFIG_PCI |
| 133 | struct bus_type isa_bus_type = { |
| 134 | .name = "isa", |
| 135 | .match = of_platform_bus_match, |
| 136 | .probe = of_device_probe, |
| 137 | .remove = of_device_remove, |
| 138 | .suspend = of_device_suspend, |
| 139 | .resume = of_device_resume, |
| 140 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame^] | 141 | EXPORT_SYMBOL(isa_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 142 | |
| 143 | struct bus_type ebus_bus_type = { |
| 144 | .name = "ebus", |
| 145 | .match = of_platform_bus_match, |
| 146 | .probe = of_device_probe, |
| 147 | .remove = of_device_remove, |
| 148 | .suspend = of_device_suspend, |
| 149 | .resume = of_device_resume, |
| 150 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame^] | 151 | EXPORT_SYMBOL(ebus_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 152 | #endif |
| 153 | |
| 154 | #ifdef CONFIG_SBUS |
| 155 | struct bus_type sbus_bus_type = { |
| 156 | .name = "sbus", |
| 157 | .match = of_platform_bus_match, |
| 158 | .probe = of_device_probe, |
| 159 | .remove = of_device_remove, |
| 160 | .suspend = of_device_suspend, |
| 161 | .resume = of_device_resume, |
| 162 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame^] | 163 | EXPORT_SYMBOL(sbus_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 164 | #endif |
| 165 | |
| 166 | static int __init of_bus_driver_init(void) |
| 167 | { |
| 168 | int err = 0; |
| 169 | |
| 170 | #ifdef CONFIG_PCI |
| 171 | if (!err) |
| 172 | err = bus_register(&isa_bus_type); |
| 173 | if (!err) |
| 174 | err = bus_register(&ebus_bus_type); |
| 175 | #endif |
| 176 | #ifdef CONFIG_SBUS |
| 177 | if (!err) |
| 178 | err = bus_register(&sbus_bus_type); |
| 179 | #endif |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | postcore_initcall(of_bus_driver_init); |
| 184 | |
| 185 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
| 186 | { |
| 187 | /* initialize common driver fields */ |
| 188 | drv->driver.name = drv->name; |
| 189 | drv->driver.bus = bus; |
| 190 | |
| 191 | /* register with core */ |
| 192 | return driver_register(&drv->driver); |
| 193 | } |
| 194 | |
| 195 | void of_unregister_driver(struct of_platform_driver *drv) |
| 196 | { |
| 197 | driver_unregister(&drv->driver); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) |
| 202 | { |
| 203 | struct of_device *ofdev; |
| 204 | |
| 205 | ofdev = to_of_device(dev); |
| 206 | return sprintf(buf, "%s", ofdev->node->full_name); |
| 207 | } |
| 208 | |
| 209 | static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); |
| 210 | |
| 211 | /** |
| 212 | * of_release_dev - free an of device structure when all users of it are finished. |
| 213 | * @dev: device that's been disconnected |
| 214 | * |
| 215 | * Will be called only by the device core when all users of this of device are |
| 216 | * done. |
| 217 | */ |
| 218 | void of_release_dev(struct device *dev) |
| 219 | { |
| 220 | struct of_device *ofdev; |
| 221 | |
| 222 | ofdev = to_of_device(dev); |
| 223 | |
| 224 | kfree(ofdev); |
| 225 | } |
| 226 | |
| 227 | int of_device_register(struct of_device *ofdev) |
| 228 | { |
| 229 | int rc; |
| 230 | |
| 231 | BUG_ON(ofdev->node == NULL); |
| 232 | |
| 233 | rc = device_register(&ofdev->dev); |
| 234 | if (rc) |
| 235 | return rc; |
| 236 | |
| 237 | device_create_file(&ofdev->dev, &dev_attr_devspec); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | void of_device_unregister(struct of_device *ofdev) |
| 243 | { |
| 244 | device_remove_file(&ofdev->dev, &dev_attr_devspec); |
| 245 | device_unregister(&ofdev->dev); |
| 246 | } |
| 247 | |
| 248 | struct of_device* of_platform_device_create(struct device_node *np, |
| 249 | const char *bus_id, |
| 250 | struct device *parent, |
| 251 | struct bus_type *bus) |
| 252 | { |
| 253 | struct of_device *dev; |
| 254 | |
| 255 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); |
| 256 | if (!dev) |
| 257 | return NULL; |
| 258 | memset(dev, 0, sizeof(*dev)); |
| 259 | |
| 260 | dev->dev.parent = parent; |
| 261 | dev->dev.bus = bus; |
| 262 | dev->dev.release = of_release_dev; |
| 263 | |
| 264 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); |
| 265 | |
| 266 | if (of_device_register(dev) != 0) { |
| 267 | kfree(dev); |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | return dev; |
| 272 | } |
| 273 | |
| 274 | EXPORT_SYMBOL(of_match_device); |
| 275 | EXPORT_SYMBOL(of_register_driver); |
| 276 | EXPORT_SYMBOL(of_unregister_driver); |
| 277 | EXPORT_SYMBOL(of_device_register); |
| 278 | EXPORT_SYMBOL(of_device_unregister); |
| 279 | EXPORT_SYMBOL(of_dev_get); |
| 280 | EXPORT_SYMBOL(of_dev_put); |
| 281 | EXPORT_SYMBOL(of_platform_device_create); |
| 282 | EXPORT_SYMBOL(of_release_dev); |