681-NET-add-of_get_mac_address_mtd.patch 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. From: John Crispin <blogic@openwrt.org>
  2. Date: Sun, 27 Jul 2014 09:40:01 +0100
  3. Subject: NET: add of_get_mac_address_mtd()
  4. Many embedded devices have information such as mac addresses stored inside mtd
  5. devices. This patch allows us to add a property inside a node describing a
  6. network interface. The new property points at a mtd partition with an offset
  7. where the mac address can be found.
  8. Signed-off-by: John Crispin <blogic@openwrt.org>
  9. ---
  10. drivers/of/of_net.c | 37 +++++++++++++++++++++++++++++++++++++
  11. include/linux/of_net.h | 1 +
  12. 2 files changed, 38 insertions(+)
  13. --- a/drivers/of/of_net.c
  14. +++ b/drivers/of/of_net.c
  15. @@ -10,6 +10,7 @@
  16. #include <linux/of_net.h>
  17. #include <linux/phy.h>
  18. #include <linux/export.h>
  19. +#include <linux/mtd/mtd.h>
  20. /**
  21. * of_get_phy_mode - Get phy mode for given device_node
  22. @@ -75,3 +76,45 @@ const void *of_get_mac_address(struct de
  23. return NULL;
  24. }
  25. EXPORT_SYMBOL(of_get_mac_address);
  26. +
  27. +#ifdef CONFIG_MTD
  28. +int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac)
  29. +{
  30. + struct device_node *mtd_np = NULL;
  31. + size_t retlen;
  32. + int size, ret;
  33. + struct mtd_info *mtd;
  34. + const char *part;
  35. + const __be32 *list;
  36. + phandle phandle;
  37. + u32 mac_inc = 0;
  38. +
  39. + list = of_get_property(np, "mtd-mac-address", &size);
  40. + if (!list || (size != (2 * sizeof(*list))))
  41. + return -ENOENT;
  42. +
  43. + phandle = be32_to_cpup(list++);
  44. + if (phandle)
  45. + mtd_np = of_find_node_by_phandle(phandle);
  46. +
  47. + if (!mtd_np)
  48. + return -ENOENT;
  49. +
  50. + part = of_get_property(mtd_np, "label", NULL);
  51. + if (!part)
  52. + part = mtd_np->name;
  53. +
  54. + mtd = get_mtd_device_nm(part);
  55. + if (IS_ERR(mtd))
  56. + return PTR_ERR(mtd);
  57. +
  58. + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
  59. + put_mtd_device(mtd);
  60. +
  61. + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc))
  62. + mac[5] += mac_inc;
  63. +
  64. + return ret;
  65. +}
  66. +EXPORT_SYMBOL_GPL(of_get_mac_address_mtd);
  67. +#endif
  68. --- a/include/linux/of_net.h
  69. +++ b/include/linux/of_net.h
  70. @@ -11,6 +11,14 @@
  71. #include <linux/of.h>
  72. extern int of_get_phy_mode(struct device_node *np);
  73. extern const void *of_get_mac_address(struct device_node *np);
  74. +#ifdef CONFIG_MTD
  75. +extern int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac);
  76. +#else
  77. +static inline int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac)
  78. +{
  79. + return -ENOENT;
  80. +}
  81. +#endif
  82. #else
  83. static inline int of_get_phy_mode(struct device_node *np)
  84. {