api_net.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * (C) Copyright 2007 Semihalf
  3. *
  4. * Written by: Rafal Jaworowski <raj@semihalf.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <common.h>
  10. #include <net.h>
  11. #include <linux/types.h>
  12. #include <api_public.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #define DEBUG
  15. #undef DEBUG
  16. #ifdef DEBUG
  17. #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
  18. #else
  19. #define debugf(fmt, args...)
  20. #endif
  21. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  22. static int dev_valid_net(void *cookie)
  23. {
  24. return ((void *)eth_get_dev() == cookie) ? 1 : 0;
  25. }
  26. int dev_open_net(void *cookie)
  27. {
  28. if (!dev_valid_net(cookie))
  29. return API_ENODEV;
  30. if (eth_init(gd->bd) < 0)
  31. return API_EIO;
  32. return 0;
  33. }
  34. int dev_close_net(void *cookie)
  35. {
  36. if (!dev_valid_net(cookie))
  37. return API_ENODEV;
  38. eth_halt();
  39. return 0;
  40. }
  41. /*
  42. * There can only be one active eth interface at a time - use what is
  43. * currently set to eth_current
  44. */
  45. int dev_enum_net(struct device_info *di)
  46. {
  47. struct eth_device *eth_current = eth_get_dev();
  48. di->type = DEV_TYP_NET;
  49. di->cookie = (void *)eth_current;
  50. if (di->cookie == NULL)
  51. return 0;
  52. memcpy(di->di_net.hwaddr, eth_current->enetaddr, 6);
  53. debugf("device found, returning cookie 0x%08x\n",
  54. (u_int32_t)di->cookie);
  55. return 1;
  56. }
  57. int dev_write_net(void *cookie, void *buf, int len)
  58. {
  59. /* XXX verify that cookie points to a valid net device??? */
  60. return eth_send(buf, len);
  61. }
  62. int dev_read_net(void *cookie, void *buf, int len)
  63. {
  64. /* XXX verify that cookie points to a valid net device??? */
  65. return eth_receive(buf, len);
  66. }