mdio-boardinfo.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * mdio-boardinfo - Collect pre-declarations for MDIO devices
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <linux/mutex.h>
  13. #include <linux/list.h>
  14. #include "mdio-boardinfo.h"
  15. static LIST_HEAD(mdio_board_list);
  16. static DEFINE_MUTEX(mdio_board_lock);
  17. /**
  18. * mdiobus_setup_mdiodev_from_board_info - create and setup MDIO devices
  19. * from pre-collected board specific MDIO information
  20. * @mdiodev: MDIO device pointer
  21. * Context: can sleep
  22. */
  23. void mdiobus_setup_mdiodev_from_board_info(struct mii_bus *bus,
  24. int (*cb)
  25. (struct mii_bus *bus,
  26. struct mdio_board_info *bi))
  27. {
  28. struct mdio_board_entry *be;
  29. struct mdio_board_entry *tmp;
  30. struct mdio_board_info *bi;
  31. int ret;
  32. mutex_lock(&mdio_board_lock);
  33. list_for_each_entry_safe(be, tmp, &mdio_board_list, list) {
  34. bi = &be->board_info;
  35. if (strcmp(bus->id, bi->bus_id))
  36. continue;
  37. mutex_unlock(&mdio_board_lock);
  38. ret = cb(bus, bi);
  39. mutex_lock(&mdio_board_lock);
  40. if (ret)
  41. continue;
  42. }
  43. mutex_unlock(&mdio_board_lock);
  44. }
  45. EXPORT_SYMBOL(mdiobus_setup_mdiodev_from_board_info);
  46. /**
  47. * mdio_register_board_info - register MDIO devices for a given board
  48. * @info: array of devices descriptors
  49. * @n: number of descriptors provided
  50. * Context: can sleep
  51. *
  52. * The board info passed can be marked with __initdata but be pointers
  53. * such as platform_data etc. are copied as-is
  54. */
  55. int mdiobus_register_board_info(const struct mdio_board_info *info,
  56. unsigned int n)
  57. {
  58. struct mdio_board_entry *be;
  59. unsigned int i;
  60. be = kcalloc(n, sizeof(*be), GFP_KERNEL);
  61. if (!be)
  62. return -ENOMEM;
  63. for (i = 0; i < n; i++, be++, info++) {
  64. memcpy(&be->board_info, info, sizeof(*info));
  65. mutex_lock(&mdio_board_lock);
  66. list_add_tail(&be->list, &mdio_board_list);
  67. mutex_unlock(&mdio_board_lock);
  68. }
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(mdiobus_register_board_info);