qdev-fw.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * qdev fw helpers
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "hw/fw-path-provider.h"
  19. #include "hw/qdev-core.h"
  20. const char *qdev_fw_name(DeviceState *dev)
  21. {
  22. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  23. if (dc->fw_name) {
  24. return dc->fw_name;
  25. }
  26. return object_get_typename(OBJECT(dev));
  27. }
  28. static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
  29. {
  30. BusClass *bc = BUS_GET_CLASS(bus);
  31. if (bc->get_fw_dev_path) {
  32. return bc->get_fw_dev_path(dev);
  33. }
  34. return NULL;
  35. }
  36. static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
  37. {
  38. Object *obj = OBJECT(dev);
  39. char *d = NULL;
  40. while (!d && obj->parent) {
  41. obj = obj->parent;
  42. d = fw_path_provider_try_get_dev_path(obj, bus, dev);
  43. }
  44. return d;
  45. }
  46. char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
  47. {
  48. Object *obj = OBJECT(dev);
  49. return fw_path_provider_try_get_dev_path(obj, bus, dev);
  50. }
  51. static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
  52. {
  53. int l = 0;
  54. if (dev && dev->parent_bus) {
  55. char *d;
  56. l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
  57. d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
  58. if (!d) {
  59. d = bus_get_fw_dev_path(dev->parent_bus, dev);
  60. }
  61. if (d) {
  62. l += snprintf(p + l, size - l, "%s", d);
  63. g_free(d);
  64. } else {
  65. return l;
  66. }
  67. }
  68. l += snprintf(p + l , size - l, "/");
  69. return l;
  70. }
  71. char *qdev_get_fw_dev_path(DeviceState *dev)
  72. {
  73. char path[128];
  74. int l;
  75. l = qdev_get_fw_dev_path_helper(dev, path, 128);
  76. path[l - 1] = '\0';
  77. return g_strdup(path);
  78. }