rename_devices.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/mount.h>
  8. #include <linux/device.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/blkdev.h>
  14. #define PATH_SIZE 64
  15. static void rename_blk_device_name(struct device_node *np)
  16. {
  17. dev_t devt;
  18. int index = 0;
  19. struct device_node *node = np;
  20. char dev_path[PATH_SIZE];
  21. const char *actual_name;
  22. char *modified_name;
  23. struct device *dev;
  24. struct block_device *bdev;
  25. while (!of_property_read_string_index(node, "actual-dev", index,
  26. &actual_name)) {
  27. memset(dev_path, '\0', PATH_SIZE);
  28. snprintf(dev_path, PATH_SIZE, "/dev/%s", actual_name);
  29. devt = name_to_dev_t(dev_path);
  30. if (!devt) {
  31. pr_err("rename-devices: No device path : %s\n", dev_path);
  32. return;
  33. }
  34. bdev = blkdev_get_by_dev(devt, FMODE_WRITE | FMODE_READ, NULL);
  35. dev = &bdev->bd_device;
  36. if (!dev) {
  37. pr_err("rename-devices: No device with dev path : %s\n", dev_path);
  38. return;
  39. }
  40. if (!of_property_read_string_index(node, "rename-dev", index,
  41. (const char **)&modified_name)) {
  42. device_rename(dev, modified_name);
  43. } else {
  44. pr_err("rename-devices: rename-dev for actual-dev = %s is missing\n",
  45. actual_name);
  46. return;
  47. }
  48. index++;
  49. }
  50. }
  51. static int __init rename_devices_init(void)
  52. {
  53. struct device_node *node = NULL, *child = NULL;
  54. const char *device_type;
  55. node = of_find_compatible_node(NULL, NULL, "qcom,rename-devices");
  56. if (!node) {
  57. pr_err("rename-devices: qcom,rename-devices node is missing\n");
  58. goto out;
  59. }
  60. for_each_child_of_node(node, child) {
  61. if (!of_property_read_string(child, "device-type", &device_type)) {
  62. if (strcmp(device_type, "block") == 0)
  63. rename_blk_device_name(child);
  64. else
  65. pr_err("rename-devices: unsupported device\n");
  66. } else
  67. pr_err("rename-devices: device-type is missing\n");
  68. }
  69. out:
  70. of_node_put(node);
  71. return 0;
  72. }
  73. late_initcall(rename_devices_init);
  74. MODULE_DESCRIPTION("Rename devices");
  75. MODULE_LICENSE("GPL");
  76. MODULE_SOFTDEP("pre: virtio_blk");
  77. MODULE_SOFTDEP("pre: virtio_mmio");