David S. Miller | fd53143 | 2006-06-23 15:55:17 -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 ebus_bus_type = { |
| 134 | .name = "ebus", |
| 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(ebus_bus_type); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 142 | #endif |
| 143 | |
| 144 | #ifdef CONFIG_SBUS |
| 145 | struct bus_type sbus_bus_type = { |
| 146 | .name = "sbus", |
| 147 | .match = of_platform_bus_match, |
| 148 | .probe = of_device_probe, |
| 149 | .remove = of_device_remove, |
| 150 | .suspend = of_device_suspend, |
| 151 | .resume = of_device_resume, |
| 152 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 153 | EXPORT_SYMBOL(sbus_bus_type); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 154 | #endif |
| 155 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 156 | struct bus_type of_bus_type = { |
| 157 | .name = "of", |
| 158 | .match = of_platform_bus_match, |
| 159 | .probe = of_device_probe, |
| 160 | .remove = of_device_remove, |
| 161 | .suspend = of_device_suspend, |
| 162 | .resume = of_device_resume, |
| 163 | }; |
| 164 | EXPORT_SYMBOL(of_bus_type); |
| 165 | |
| 166 | static inline u64 of_read_addr(u32 *cell, int size) |
| 167 | { |
| 168 | u64 r = 0; |
| 169 | while (size--) |
| 170 | r = (r << 32) | *(cell++); |
| 171 | return r; |
| 172 | } |
| 173 | |
| 174 | static void __init get_cells(struct device_node *dp, |
| 175 | int *addrc, int *sizec) |
| 176 | { |
| 177 | if (addrc) |
| 178 | *addrc = of_n_addr_cells(dp); |
| 179 | if (sizec) |
| 180 | *sizec = of_n_size_cells(dp); |
| 181 | } |
| 182 | |
| 183 | /* Max address size we deal with */ |
| 184 | #define OF_MAX_ADDR_CELLS 4 |
| 185 | |
| 186 | struct of_bus { |
| 187 | const char *name; |
| 188 | const char *addr_prop_name; |
| 189 | int (*match)(struct device_node *parent); |
| 190 | void (*count_cells)(struct device_node *child, |
| 191 | int *addrc, int *sizec); |
| 192 | u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna); |
| 193 | int (*translate)(u32 *addr, u64 offset, int na); |
| 194 | unsigned int (*get_flags)(u32 *addr); |
| 195 | }; |
| 196 | |
| 197 | /* |
| 198 | * Default translator (generic bus) |
| 199 | */ |
| 200 | |
| 201 | static void of_bus_default_count_cells(struct device_node *dev, |
| 202 | int *addrc, int *sizec) |
| 203 | { |
| 204 | get_cells(dev, addrc, sizec); |
| 205 | } |
| 206 | |
| 207 | static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna) |
| 208 | { |
| 209 | u64 cp, s, da; |
| 210 | |
| 211 | cp = of_read_addr(range, na); |
| 212 | s = of_read_addr(range + na + pna, ns); |
| 213 | da = of_read_addr(addr, na); |
| 214 | |
| 215 | if (da < cp || da >= (cp + s)) |
| 216 | return OF_BAD_ADDR; |
| 217 | return da - cp; |
| 218 | } |
| 219 | |
| 220 | static int of_bus_default_translate(u32 *addr, u64 offset, int na) |
| 221 | { |
| 222 | u64 a = of_read_addr(addr, na); |
| 223 | memset(addr, 0, na * 4); |
| 224 | a += offset; |
| 225 | if (na > 1) |
| 226 | addr[na - 2] = a >> 32; |
| 227 | addr[na - 1] = a & 0xffffffffu; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static unsigned int of_bus_default_get_flags(u32 *addr) |
| 233 | { |
| 234 | return IORESOURCE_MEM; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /* |
| 239 | * PCI bus specific translator |
| 240 | */ |
| 241 | |
| 242 | static int of_bus_pci_match(struct device_node *np) |
| 243 | { |
| 244 | return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex"); |
| 245 | } |
| 246 | |
| 247 | static void of_bus_pci_count_cells(struct device_node *np, |
| 248 | int *addrc, int *sizec) |
| 249 | { |
| 250 | if (addrc) |
| 251 | *addrc = 3; |
| 252 | if (sizec) |
| 253 | *sizec = 2; |
| 254 | } |
| 255 | |
| 256 | static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna) |
| 257 | { |
| 258 | u64 cp, s, da; |
| 259 | |
| 260 | /* Check address type match */ |
| 261 | if ((addr[0] ^ range[0]) & 0x03000000) |
| 262 | return OF_BAD_ADDR; |
| 263 | |
| 264 | /* Read address values, skipping high cell */ |
| 265 | cp = of_read_addr(range + 1, na - 1); |
| 266 | s = of_read_addr(range + na + pna, ns); |
| 267 | da = of_read_addr(addr + 1, na - 1); |
| 268 | |
| 269 | if (da < cp || da >= (cp + s)) |
| 270 | return OF_BAD_ADDR; |
| 271 | return da - cp; |
| 272 | } |
| 273 | |
| 274 | static int of_bus_pci_translate(u32 *addr, u64 offset, int na) |
| 275 | { |
| 276 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 277 | } |
| 278 | |
| 279 | static unsigned int of_bus_pci_get_flags(u32 *addr) |
| 280 | { |
| 281 | unsigned int flags = 0; |
| 282 | u32 w = addr[0]; |
| 283 | |
| 284 | switch((w >> 24) & 0x03) { |
| 285 | case 0x01: |
| 286 | flags |= IORESOURCE_IO; |
| 287 | case 0x02: /* 32 bits */ |
| 288 | case 0x03: /* 64 bits */ |
| 289 | flags |= IORESOURCE_MEM; |
| 290 | } |
| 291 | if (w & 0x40000000) |
| 292 | flags |= IORESOURCE_PREFETCH; |
| 293 | return flags; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * SBUS bus specific translator |
| 298 | */ |
| 299 | |
| 300 | static int of_bus_sbus_match(struct device_node *np) |
| 301 | { |
| 302 | return !strcmp(np->name, "sbus") || |
| 303 | !strcmp(np->name, "sbi"); |
| 304 | } |
| 305 | |
| 306 | static void of_bus_sbus_count_cells(struct device_node *child, |
| 307 | int *addrc, int *sizec) |
| 308 | { |
| 309 | if (addrc) |
| 310 | *addrc = 2; |
| 311 | if (sizec) |
| 312 | *sizec = 1; |
| 313 | } |
| 314 | |
| 315 | static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna) |
| 316 | { |
| 317 | return of_bus_default_map(addr, range, na, ns, pna); |
| 318 | } |
| 319 | |
| 320 | static int of_bus_sbus_translate(u32 *addr, u64 offset, int na) |
| 321 | { |
| 322 | return of_bus_default_translate(addr, offset, na); |
| 323 | } |
| 324 | |
| 325 | static unsigned int of_bus_sbus_get_flags(u32 *addr) |
| 326 | { |
| 327 | return IORESOURCE_MEM; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /* |
| 332 | * Array of bus specific translators |
| 333 | */ |
| 334 | |
| 335 | static struct of_bus of_busses[] = { |
| 336 | /* PCI */ |
| 337 | { |
| 338 | .name = "pci", |
| 339 | .addr_prop_name = "assigned-addresses", |
| 340 | .match = of_bus_pci_match, |
| 341 | .count_cells = of_bus_pci_count_cells, |
| 342 | .map = of_bus_pci_map, |
| 343 | .translate = of_bus_pci_translate, |
| 344 | .get_flags = of_bus_pci_get_flags, |
| 345 | }, |
| 346 | /* SBUS */ |
| 347 | { |
| 348 | .name = "sbus", |
| 349 | .addr_prop_name = "reg", |
| 350 | .match = of_bus_sbus_match, |
| 351 | .count_cells = of_bus_sbus_count_cells, |
| 352 | .map = of_bus_sbus_map, |
| 353 | .translate = of_bus_sbus_translate, |
| 354 | .get_flags = of_bus_sbus_get_flags, |
| 355 | }, |
| 356 | /* Default */ |
| 357 | { |
| 358 | .name = "default", |
| 359 | .addr_prop_name = "reg", |
| 360 | .match = NULL, |
| 361 | .count_cells = of_bus_default_count_cells, |
| 362 | .map = of_bus_default_map, |
| 363 | .translate = of_bus_default_translate, |
| 364 | .get_flags = of_bus_default_get_flags, |
| 365 | }, |
| 366 | }; |
| 367 | |
| 368 | static struct of_bus *of_match_bus(struct device_node *np) |
| 369 | { |
| 370 | int i; |
| 371 | |
| 372 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) |
| 373 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 374 | return &of_busses[i]; |
| 375 | BUG(); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | static int __init build_one_resource(struct device_node *parent, |
| 380 | struct of_bus *bus, |
| 381 | struct of_bus *pbus, |
| 382 | u32 *addr, |
| 383 | int na, int ns, int pna) |
| 384 | { |
| 385 | u32 *ranges; |
| 386 | unsigned int rlen; |
| 387 | int rone; |
| 388 | u64 offset = OF_BAD_ADDR; |
| 389 | |
| 390 | ranges = of_get_property(parent, "ranges", &rlen); |
| 391 | if (ranges == NULL || rlen == 0) { |
| 392 | offset = of_read_addr(addr, na); |
| 393 | memset(addr, 0, pna * 4); |
| 394 | goto finish; |
| 395 | } |
| 396 | |
| 397 | /* Now walk through the ranges */ |
| 398 | rlen /= 4; |
| 399 | rone = na + pna + ns; |
| 400 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 401 | offset = bus->map(addr, ranges, na, ns, pna); |
| 402 | if (offset != OF_BAD_ADDR) |
| 403 | break; |
| 404 | } |
| 405 | if (offset == OF_BAD_ADDR) |
| 406 | return 1; |
| 407 | |
| 408 | memcpy(addr, ranges + na, 4 * pna); |
| 409 | |
| 410 | finish: |
| 411 | /* Translate it into parent bus space */ |
| 412 | return pbus->translate(addr, offset, pna); |
| 413 | } |
| 414 | |
| 415 | static void __init build_device_resources(struct of_device *op, |
| 416 | struct device *parent) |
| 417 | { |
| 418 | struct of_device *p_op; |
| 419 | struct of_bus *bus; |
| 420 | int na, ns; |
| 421 | int index, num_reg; |
| 422 | void *preg; |
| 423 | |
| 424 | if (!parent) |
| 425 | return; |
| 426 | |
| 427 | p_op = to_of_device(parent); |
| 428 | bus = of_match_bus(p_op->node); |
| 429 | bus->count_cells(op->node, &na, &ns); |
| 430 | |
| 431 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); |
| 432 | if (!preg || num_reg == 0) |
| 433 | return; |
| 434 | |
| 435 | /* Convert to num-cells. */ |
| 436 | num_reg /= 4; |
| 437 | |
| 438 | /* Conver to num-entries. */ |
| 439 | num_reg /= na + ns; |
| 440 | |
| 441 | for (index = 0; index < num_reg; index++) { |
| 442 | struct resource *r = &op->resource[index]; |
| 443 | u32 addr[OF_MAX_ADDR_CELLS]; |
| 444 | u32 *reg = (preg + (index * ((na + ns) * 4))); |
| 445 | struct device_node *dp = op->node; |
| 446 | struct device_node *pp = p_op->node; |
| 447 | struct of_bus *pbus; |
| 448 | u64 size, result = OF_BAD_ADDR; |
| 449 | unsigned long flags; |
| 450 | int dna, dns; |
| 451 | int pna, pns; |
| 452 | |
| 453 | size = of_read_addr(reg + na, ns); |
| 454 | flags = bus->get_flags(reg); |
| 455 | |
| 456 | memcpy(addr, reg, na * 4); |
| 457 | |
| 458 | /* If the immediate parent has no ranges property to apply, |
| 459 | * just use a 1<->1 mapping. |
| 460 | */ |
| 461 | if (of_find_property(pp, "ranges", NULL) == NULL) { |
| 462 | result = of_read_addr(addr, na); |
| 463 | goto build_res; |
| 464 | } |
| 465 | |
| 466 | dna = na; |
| 467 | dns = ns; |
| 468 | |
| 469 | while (1) { |
| 470 | dp = pp; |
| 471 | pp = dp->parent; |
| 472 | if (!pp) { |
| 473 | result = of_read_addr(addr, dna); |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | pbus = of_match_bus(pp); |
| 478 | pbus->count_cells(dp, &pna, &pns); |
| 479 | |
| 480 | if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna)) |
| 481 | break; |
| 482 | |
| 483 | dna = pna; |
| 484 | dns = pns; |
| 485 | bus = pbus; |
| 486 | } |
| 487 | |
| 488 | build_res: |
| 489 | memset(r, 0, sizeof(*r)); |
| 490 | if (result != OF_BAD_ADDR) { |
David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 491 | r->start = result & 0xffffffff; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 492 | r->end = result + size - 1; |
David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 493 | r->flags = flags | ((result >> 32ULL) & 0xffUL); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 494 | } else { |
| 495 | r->start = ~0UL; |
| 496 | r->end = ~0UL; |
| 497 | } |
| 498 | r->name = op->node->name; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static struct of_device * __init scan_one_device(struct device_node *dp, |
| 503 | struct device *parent) |
| 504 | { |
| 505 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); |
| 506 | unsigned int *irq; |
| 507 | int len; |
| 508 | |
| 509 | if (!op) |
| 510 | return NULL; |
| 511 | |
| 512 | op->node = dp; |
| 513 | |
| 514 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", |
| 515 | (25*1000*1000)); |
| 516 | op->portid = of_getintprop_default(dp, "upa-portid", -1); |
| 517 | if (op->portid == -1) |
| 518 | op->portid = of_getintprop_default(dp, "portid", -1); |
| 519 | |
| 520 | irq = of_get_property(dp, "interrupts", &len); |
| 521 | if (irq) |
| 522 | op->irq = *irq; |
| 523 | else |
| 524 | op->irq = 0xffffffff; |
| 525 | |
| 526 | build_device_resources(op, parent); |
| 527 | |
| 528 | op->dev.parent = parent; |
| 529 | op->dev.bus = &of_bus_type; |
| 530 | if (!parent) |
| 531 | strcpy(op->dev.bus_id, "root"); |
| 532 | else |
| 533 | strcpy(op->dev.bus_id, dp->path_component_name); |
| 534 | |
| 535 | if (of_device_register(op)) { |
| 536 | printk("%s: Could not register of device.\n", |
| 537 | dp->full_name); |
| 538 | kfree(op); |
| 539 | op = NULL; |
| 540 | } |
| 541 | |
| 542 | return op; |
| 543 | } |
| 544 | |
| 545 | static void __init scan_tree(struct device_node *dp, struct device *parent) |
| 546 | { |
| 547 | while (dp) { |
| 548 | struct of_device *op = scan_one_device(dp, parent); |
| 549 | |
| 550 | if (op) |
| 551 | scan_tree(dp->child, &op->dev); |
| 552 | |
| 553 | dp = dp->sibling; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static void __init scan_of_devices(void) |
| 558 | { |
| 559 | struct device_node *root = of_find_node_by_path("/"); |
| 560 | struct of_device *parent; |
| 561 | |
| 562 | parent = scan_one_device(root, NULL); |
| 563 | if (!parent) |
| 564 | return; |
| 565 | |
| 566 | scan_tree(root->child, &parent->dev); |
| 567 | } |
| 568 | |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 569 | static int __init of_bus_driver_init(void) |
| 570 | { |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 571 | int err; |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 572 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 573 | err = bus_register(&of_bus_type); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 574 | #ifdef CONFIG_PCI |
| 575 | if (!err) |
| 576 | err = bus_register(&ebus_bus_type); |
| 577 | #endif |
| 578 | #ifdef CONFIG_SBUS |
| 579 | if (!err) |
| 580 | err = bus_register(&sbus_bus_type); |
| 581 | #endif |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 582 | |
| 583 | if (!err) |
| 584 | scan_of_devices(); |
| 585 | |
| 586 | return err; |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | postcore_initcall(of_bus_driver_init); |
| 590 | |
| 591 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
| 592 | { |
| 593 | /* initialize common driver fields */ |
| 594 | drv->driver.name = drv->name; |
| 595 | drv->driver.bus = bus; |
| 596 | |
| 597 | /* register with core */ |
| 598 | return driver_register(&drv->driver); |
| 599 | } |
| 600 | |
| 601 | void of_unregister_driver(struct of_platform_driver *drv) |
| 602 | { |
| 603 | driver_unregister(&drv->driver); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) |
| 608 | { |
| 609 | struct of_device *ofdev; |
| 610 | |
| 611 | ofdev = to_of_device(dev); |
| 612 | return sprintf(buf, "%s", ofdev->node->full_name); |
| 613 | } |
| 614 | |
| 615 | static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); |
| 616 | |
| 617 | /** |
| 618 | * of_release_dev - free an of device structure when all users of it are finished. |
| 619 | * @dev: device that's been disconnected |
| 620 | * |
| 621 | * Will be called only by the device core when all users of this of device are |
| 622 | * done. |
| 623 | */ |
| 624 | void of_release_dev(struct device *dev) |
| 625 | { |
| 626 | struct of_device *ofdev; |
| 627 | |
| 628 | ofdev = to_of_device(dev); |
| 629 | |
| 630 | kfree(ofdev); |
| 631 | } |
| 632 | |
| 633 | int of_device_register(struct of_device *ofdev) |
| 634 | { |
| 635 | int rc; |
| 636 | |
| 637 | BUG_ON(ofdev->node == NULL); |
| 638 | |
| 639 | rc = device_register(&ofdev->dev); |
| 640 | if (rc) |
| 641 | return rc; |
| 642 | |
| 643 | device_create_file(&ofdev->dev, &dev_attr_devspec); |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | void of_device_unregister(struct of_device *ofdev) |
| 649 | { |
| 650 | device_remove_file(&ofdev->dev, &dev_attr_devspec); |
| 651 | device_unregister(&ofdev->dev); |
| 652 | } |
| 653 | |
| 654 | struct of_device* of_platform_device_create(struct device_node *np, |
| 655 | const char *bus_id, |
| 656 | struct device *parent, |
| 657 | struct bus_type *bus) |
| 658 | { |
| 659 | struct of_device *dev; |
| 660 | |
| 661 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); |
| 662 | if (!dev) |
| 663 | return NULL; |
| 664 | memset(dev, 0, sizeof(*dev)); |
| 665 | |
| 666 | dev->dev.parent = parent; |
| 667 | dev->dev.bus = bus; |
| 668 | dev->dev.release = of_release_dev; |
| 669 | |
| 670 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); |
| 671 | |
| 672 | if (of_device_register(dev) != 0) { |
| 673 | kfree(dev); |
| 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | return dev; |
| 678 | } |
| 679 | |
| 680 | EXPORT_SYMBOL(of_match_device); |
| 681 | EXPORT_SYMBOL(of_register_driver); |
| 682 | EXPORT_SYMBOL(of_unregister_driver); |
| 683 | EXPORT_SYMBOL(of_device_register); |
| 684 | EXPORT_SYMBOL(of_device_unregister); |
| 685 | EXPORT_SYMBOL(of_dev_get); |
| 686 | EXPORT_SYMBOL(of_dev_put); |
| 687 | EXPORT_SYMBOL(of_platform_device_create); |
| 688 | EXPORT_SYMBOL(of_release_dev); |