netdev.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /* Shared receive scratch: see netdev.h. One buffer, reused by every protocol
  15. * that polls frames (they never run at the same time: the NIC is owned by one
  16. * command via netdev_acquire()). */
  17. static unsigned char rx_scratch[NETDEV_FRAME_MAX];
  18. unsigned char *netdev_rx_scratch(void)
  19. {
  20. return rx_scratch;
  21. }
  22. void netdev_acquire(void)
  23. {
  24. busy++;
  25. }
  26. void netdev_release(void)
  27. {
  28. if (busy)
  29. busy--;
  30. }
  31. int netdev_in_use(void)
  32. {
  33. return busy != 0;
  34. }
  35. const struct netdev *netdev_active(void)
  36. {
  37. unsigned int i;
  38. if (!probed) {
  39. probed = 1;
  40. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
  41. if (netdev_drivers[i]->init() == 0) {
  42. active = netdev_drivers[i];
  43. break;
  44. }
  45. }
  46. }
  47. return active;
  48. }
  49. int netdev_get_mac(unsigned char mac[6])
  50. {
  51. const struct netdev *nd = netdev_active();
  52. unsigned int i;
  53. if (!nd) {
  54. for (i = 0; i < 6; i++)
  55. mac[i] = 0;
  56. return -1;
  57. }
  58. nd->get_mac(mac);
  59. return 0;
  60. }