mdio_device.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Framework for MDIO devices, other than PHYs.
  2. *
  3. * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mdio.h>
  17. #include <linux/mii.h>
  18. #include <linux/module.h>
  19. #include <linux/phy.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/unistd.h>
  23. void mdio_device_free(struct mdio_device *mdiodev)
  24. {
  25. put_device(&mdiodev->dev);
  26. }
  27. EXPORT_SYMBOL(mdio_device_free);
  28. static void mdio_device_release(struct device *dev)
  29. {
  30. kfree(to_mdio_device(dev));
  31. }
  32. struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
  33. {
  34. struct mdio_device *mdiodev;
  35. /* We allocate the device, and initialize the default values */
  36. mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
  37. if (!mdiodev)
  38. return ERR_PTR(-ENOMEM);
  39. mdiodev->dev.release = mdio_device_release;
  40. mdiodev->dev.parent = &bus->dev;
  41. mdiodev->dev.bus = &mdio_bus_type;
  42. mdiodev->device_free = mdio_device_free;
  43. mdiodev->device_remove = mdio_device_remove;
  44. mdiodev->bus = bus;
  45. mdiodev->addr = addr;
  46. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  47. device_initialize(&mdiodev->dev);
  48. return mdiodev;
  49. }
  50. EXPORT_SYMBOL(mdio_device_create);
  51. /**
  52. * mdio_device_register - Register the mdio device on the MDIO bus
  53. * @mdiodev: mdio_device structure to be added to the MDIO bus
  54. */
  55. int mdio_device_register(struct mdio_device *mdiodev)
  56. {
  57. int err;
  58. dev_info(&mdiodev->dev, "mdio_device_register\n");
  59. err = mdiobus_register_device(mdiodev);
  60. if (err)
  61. return err;
  62. err = device_add(&mdiodev->dev);
  63. if (err) {
  64. pr_err("MDIO %d failed to add\n", mdiodev->addr);
  65. goto out;
  66. }
  67. return 0;
  68. out:
  69. mdiobus_unregister_device(mdiodev);
  70. return err;
  71. }
  72. EXPORT_SYMBOL(mdio_device_register);
  73. /**
  74. * mdio_device_remove - Remove a previously registered mdio device from the
  75. * MDIO bus
  76. * @mdiodev: mdio_device structure to remove
  77. *
  78. * This doesn't free the mdio_device itself, it merely reverses the effects
  79. * of mdio_device_register(). Use mdio_device_free() to free the device
  80. * after calling this function.
  81. */
  82. void mdio_device_remove(struct mdio_device *mdiodev)
  83. {
  84. device_del(&mdiodev->dev);
  85. mdiobus_unregister_device(mdiodev);
  86. }
  87. EXPORT_SYMBOL(mdio_device_remove);
  88. /**
  89. * mdio_probe - probe an MDIO device
  90. * @dev: device to probe
  91. *
  92. * Description: Take care of setting up the mdio_device structure
  93. * and calling the driver to probe the device.
  94. */
  95. static int mdio_probe(struct device *dev)
  96. {
  97. struct mdio_device *mdiodev = to_mdio_device(dev);
  98. struct device_driver *drv = mdiodev->dev.driver;
  99. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  100. int err = 0;
  101. if (mdiodrv->probe)
  102. err = mdiodrv->probe(mdiodev);
  103. return err;
  104. }
  105. static int mdio_remove(struct device *dev)
  106. {
  107. struct mdio_device *mdiodev = to_mdio_device(dev);
  108. struct device_driver *drv = mdiodev->dev.driver;
  109. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  110. if (mdiodrv->remove)
  111. mdiodrv->remove(mdiodev);
  112. return 0;
  113. }
  114. /**
  115. * mdio_driver_register - register an mdio_driver with the MDIO layer
  116. * @new_driver: new mdio_driver to register
  117. */
  118. int mdio_driver_register(struct mdio_driver *drv)
  119. {
  120. struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
  121. int retval;
  122. pr_info("mdio_driver_register: %s\n", mdiodrv->driver.name);
  123. mdiodrv->driver.bus = &mdio_bus_type;
  124. mdiodrv->driver.probe = mdio_probe;
  125. mdiodrv->driver.remove = mdio_remove;
  126. retval = driver_register(&mdiodrv->driver);
  127. if (retval) {
  128. pr_err("%s: Error %d in registering driver\n",
  129. mdiodrv->driver.name, retval);
  130. return retval;
  131. }
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(mdio_driver_register);
  135. void mdio_driver_unregister(struct mdio_driver *drv)
  136. {
  137. struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
  138. driver_unregister(&mdiodrv->driver);
  139. }
  140. EXPORT_SYMBOL(mdio_driver_unregister);