device.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 dev_show_devspec(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", ofdev->node->full_name);
  51. }
  52. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  53. /**
  54. * of_release_dev - free an of device structure when all users of it are finished.
  55. * @dev: device that's been disconnected
  56. *
  57. * Will be called only by the device core when all users of this of device are
  58. * done.
  59. */
  60. void of_release_dev(struct device *dev)
  61. {
  62. struct of_device *ofdev;
  63. ofdev = to_of_device(dev);
  64. of_node_put(ofdev->node);
  65. kfree(ofdev);
  66. }
  67. EXPORT_SYMBOL(of_release_dev);
  68. int of_device_register(struct of_device *ofdev)
  69. {
  70. int rc;
  71. BUG_ON(ofdev->node == NULL);
  72. rc = device_register(&ofdev->dev);
  73. if (rc)
  74. return rc;
  75. rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
  76. if (rc)
  77. device_unregister(&ofdev->dev);
  78. return rc;
  79. }
  80. EXPORT_SYMBOL(of_device_register);
  81. void of_device_unregister(struct of_device *ofdev)
  82. {
  83. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  84. device_unregister(&ofdev->dev);
  85. }
  86. EXPORT_SYMBOL(of_device_unregister);