api_net.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
  23. static int dev_valid_net(void *cookie)
  24. {
  25. return ((void *)eth_get_dev() == cookie) ? 1 : 0;
  26. }
  27. int dev_open_net(void *cookie)
  28. {
  29. if (!dev_valid_net(cookie))
  30. return API_ENODEV;
  31. if (eth_init() < 0)
  32. return API_EIO;
  33. return 0;
  34. }
  35. int dev_close_net(void *cookie)
  36. {
  37. if (!dev_valid_net(cookie))
  38. return API_ENODEV;
  39. eth_halt();
  40. return 0;
  41. }
  42. /*
  43. * There can only be one active eth interface at a time - use what is
  44. * currently set to eth_current
  45. */
  46. int dev_enum_net(struct device_info *di)
  47. {
  48. struct eth_device *eth_current = eth_get_dev();
  49. di->type = DEV_TYP_NET;
  50. di->cookie = (void *)eth_current;
  51. if (di->cookie == NULL)
  52. return 0;
  53. memcpy(di->di_net.hwaddr, eth_current->enetaddr, 6);
  54. debugf("device found, returning cookie 0x%08x\n",
  55. (u_int32_t)di->cookie);
  56. return 1;
  57. }
  58. int dev_write_net(void *cookie, void *buf, int len)
  59. {
  60. /* XXX verify that cookie points to a valid net device??? */
  61. return eth_send(buf, len);
  62. }
  63. int dev_read_net(void *cookie, void *buf, int len)
  64. {
  65. /* XXX verify that cookie points to a valid net device??? */
  66. return eth_receive(buf, len);
  67. }
  68. #else
  69. int dev_open_net(void *cookie)
  70. {
  71. return API_ENODEV;
  72. }
  73. int dev_close_net(void *cookie)
  74. {
  75. return API_ENODEV;
  76. }
  77. int dev_enum_net(struct device_info *di)
  78. {
  79. return 0;
  80. }
  81. int dev_write_net(void *cookie, void *buf, int len)
  82. {
  83. return API_ENODEV;
  84. }
  85. int dev_read_net(void *cookie, void *buf, int len)
  86. {
  87. return API_ENODEV;
  88. }
  89. #endif