device.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/of_device.h>
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/slab.h>
  9. #include <asm/errno.h>
  10. /**
  11. * of_match_device - Tell if an of_device structure has a matching
  12. * of_match structure
  13. * @ids: array of of device match structures to search in
  14. * @dev: the of device structure to match against
  15. *
  16. * Used by a driver to check whether an of_device present in the
  17. * system is in its list of supported devices.
  18. */
  19. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  20. const struct of_device *dev)
  21. {
  22. if (!dev->node)
  23. return NULL;
  24. return of_match_node(matches, dev->node);
  25. }
  26. EXPORT_SYMBOL(of_match_device);
  27. struct of_device *of_dev_get(struct of_device *dev)
  28. {
  29. struct device *tmp;
  30. if (!dev)
  31. return NULL;
  32. tmp = get_device(&dev->dev);
  33. if (tmp)
  34. return to_of_device(tmp);
  35. else
  36. return NULL;
  37. }
  38. EXPORT_SYMBOL(of_dev_get);
  39. void of_dev_put(struct of_device *dev)
  40. {
  41. if (dev)
  42. put_device(&dev->dev);
  43. }
  44. EXPORT_SYMBOL(of_dev_put);
  45. static ssize_t devspec_show(struct device *dev,
  46. struct device_attribute *attr, char *buf)
  47. {
  48. struct of_device *ofdev;
  49. ofdev = to_of_device(dev);
  50. return sprintf(buf, "%s\n", ofdev->node->full_name);
  51. }
  52. static ssize_t modalias_show(struct device *dev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. struct of_device *ofdev = to_of_device(dev);
  56. ssize_t len = 0;
  57. len = of_device_get_modalias(ofdev, buf, PAGE_SIZE - 2);
  58. buf[len] = '\n';
  59. buf[len+1] = 0;
  60. return len+1;
  61. }
  62. struct device_attribute of_platform_device_attrs[] = {
  63. __ATTR_RO(devspec),
  64. __ATTR_RO(modalias),
  65. __ATTR_NULL
  66. };
  67. /**
  68. * of_release_dev - free an of device structure when all users of it are finished.
  69. * @dev: device that's been disconnected
  70. *
  71. * Will be called only by the device core when all users of this of device are
  72. * done.
  73. */
  74. void of_release_dev(struct device *dev)
  75. {
  76. struct of_device *ofdev;
  77. ofdev = to_of_device(dev);
  78. of_node_put(ofdev->node);
  79. kfree(ofdev);
  80. }
  81. EXPORT_SYMBOL(of_release_dev);
  82. int of_device_register(struct of_device *ofdev)
  83. {
  84. BUG_ON(ofdev->node == NULL);
  85. return device_register(&ofdev->dev);
  86. }
  87. EXPORT_SYMBOL(of_device_register);
  88. void of_device_unregister(struct of_device *ofdev)
  89. {
  90. device_unregister(&ofdev->dev);
  91. }
  92. EXPORT_SYMBOL(of_device_unregister);