api_net.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #define DEBUG
  14. #undef DEBUG
  15. #ifdef DEBUG
  16. #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
  17. #else
  18. #define debugf(fmt, args...)
  19. #endif
  20. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  21. #if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
  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() < 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. }
  67. #else
  68. int dev_open_net(void *cookie)
  69. {
  70. return API_ENODEV;
  71. }
  72. int dev_close_net(void *cookie)
  73. {
  74. return API_ENODEV;
  75. }
  76. int dev_enum_net(struct device_info *di)
  77. {
  78. return 0;
  79. }
  80. int dev_write_net(void *cookie, void *buf, int len)
  81. {
  82. return API_ENODEV;
  83. }
  84. int dev_read_net(void *cookie, void *buf, int len)
  85. {
  86. return API_ENODEV;
  87. }
  88. #endif