api_net.c 2.0 KB

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