host-uclass.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Uclass for sandbox host interface, used to access files on the host which
  4. * contain partitions and filesystem
  5. *
  6. * Copyright 2022 Google LLC
  7. * Written by Simon Glass <sjg@chromium.org>
  8. */
  9. #define LOG_CATEGORY UCLASS_HOST
  10. #include <common.h>
  11. #include <blk.h>
  12. #include <dm.h>
  13. #include <malloc.h>
  14. #include <sandbox_host.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/lists.h>
  17. #include <dm/uclass-internal.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /**
  20. * struct host_priv - information kept by the host uclass
  21. *
  22. * @cur_dev: Currently selected host device, or NULL if none
  23. */
  24. struct host_priv {
  25. struct udevice *cur_dev;
  26. };
  27. int host_create_device(const char *label, bool removable, struct udevice **devp)
  28. {
  29. char dev_name[30], *str, *label_new;
  30. struct host_sb_plat *plat;
  31. struct udevice *dev, *blk;
  32. int ret;
  33. /* unbind any existing device with this label */
  34. dev = host_find_by_label(label);
  35. if (dev) {
  36. ret = host_detach_file(dev);
  37. if (ret)
  38. return log_msg_ret("det", ret);
  39. ret = device_unbind(dev);
  40. if (ret)
  41. return log_msg_ret("unb", ret);
  42. }
  43. snprintf(dev_name, sizeof(dev_name), "host-%s", label);
  44. str = strdup(dev_name);
  45. if (!str)
  46. return -ENOMEM;
  47. label_new = strdup(label);
  48. if (!label_new) {
  49. ret = -ENOMEM;
  50. goto no_label;
  51. }
  52. ret = device_bind_driver(gd->dm_root, "host_sb_drv", str, &dev);
  53. if (ret)
  54. goto no_dev;
  55. device_set_name_alloced(dev);
  56. if (!blk_find_from_parent(dev, &blk)) {
  57. struct blk_desc *desc = dev_get_uclass_plat(blk);
  58. desc->removable = removable;
  59. }
  60. plat = dev_get_plat(dev);
  61. plat->label = label_new;
  62. *devp = dev;
  63. return 0;
  64. no_dev:
  65. free(label_new);
  66. no_label:
  67. free(str);
  68. return ret;
  69. }
  70. int host_attach_file(struct udevice *dev, const char *filename)
  71. {
  72. struct host_ops *ops = host_get_ops(dev);
  73. if (!ops->attach_file)
  74. return -ENOSYS;
  75. return ops->attach_file(dev, filename);
  76. }
  77. int host_create_attach_file(const char *label, const char *filename,
  78. bool removable, struct udevice **devp)
  79. {
  80. struct udevice *dev;
  81. int ret;
  82. ret = host_create_device(label, removable, &dev);
  83. if (ret)
  84. return log_msg_ret("cre", ret);
  85. ret = host_attach_file(dev, filename);
  86. if (ret) {
  87. device_unbind(dev);
  88. return log_msg_ret("att", ret);
  89. }
  90. *devp = dev;
  91. return 0;
  92. }
  93. int host_detach_file(struct udevice *dev)
  94. {
  95. struct host_ops *ops = host_get_ops(dev);
  96. if (!ops->detach_file)
  97. return -ENOSYS;
  98. if (dev == host_get_cur_dev())
  99. host_set_cur_dev(NULL);
  100. return ops->detach_file(dev);
  101. }
  102. struct udevice *host_find_by_label(const char *label)
  103. {
  104. struct udevice *dev;
  105. struct uclass *uc;
  106. uclass_id_foreach_dev(UCLASS_HOST, dev, uc) {
  107. struct host_sb_plat *plat = dev_get_plat(dev);
  108. if (plat->label && !strcmp(label, plat->label))
  109. return dev;
  110. }
  111. return NULL;
  112. }
  113. struct udevice *host_get_cur_dev(void)
  114. {
  115. struct uclass *uc = uclass_find(UCLASS_HOST);
  116. if (uc) {
  117. struct host_priv *priv = uclass_get_priv(uc);
  118. return priv->cur_dev;
  119. }
  120. return NULL;
  121. }
  122. void host_set_cur_dev(struct udevice *dev)
  123. {
  124. struct uclass *uc = uclass_find(UCLASS_HOST);
  125. if (uc) {
  126. struct host_priv *priv = uclass_get_priv(uc);
  127. priv->cur_dev = dev;
  128. }
  129. }
  130. UCLASS_DRIVER(host) = {
  131. .id = UCLASS_HOST,
  132. .name = "host",
  133. #if CONFIG_IS_ENABLED(OF_REAL)
  134. .post_bind = dm_scan_fdt_dev,
  135. #endif
  136. .priv_auto = sizeof(struct host_priv),
  137. };