netdev.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* NIC driver registry: see netdev.h. */
  2. #include "netdev.h"
  3. #include "ne2000.h"
  4. #include "r8168.h"
  5. static const struct netdev *const netdev_drivers[] = {
  6. /* PCIe Gigabit first: on the AT3IONT-I this is the real NIC, while
  7. * the NE2000 only exists under QEMU/ISA-era hardware. */
  8. &r8168_netdev,
  9. &ne2000_netdev,
  10. };
  11. static const struct netdev *active;
  12. static int probed;
  13. static unsigned int busy;
  14. void netdev_acquire(void)
  15. {
  16. busy++;
  17. }
  18. void netdev_release(void)
  19. {
  20. if (busy)
  21. busy--;
  22. }
  23. int netdev_in_use(void)
  24. {
  25. return busy != 0;
  26. }
  27. const struct netdev *netdev_active(void)
  28. {
  29. unsigned int i;
  30. if (!probed) {
  31. probed = 1;
  32. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
  33. if (netdev_drivers[i]->init() == 0) {
  34. active = netdev_drivers[i];
  35. break;
  36. }
  37. }
  38. }
  39. return active;
  40. }
  41. int netdev_get_mac(unsigned char mac[6])
  42. {
  43. const struct netdev *nd = netdev_active();
  44. unsigned int i;
  45. if (!nd) {
  46. for (i = 0; i < 6; i++)
  47. mac[i] = 0;
  48. return -1;
  49. }
  50. nd->get_mac(mac);
  51. return 0;
  52. }