api_net.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * (C) Copyright 2007 Semihalf
  3. *
  4. * Written by: Rafal Jaworowski <raj@semihalf.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <config.h>
  26. #if defined(CONFIG_API)
  27. #include <common.h>
  28. #include <net.h>
  29. #include <linux/types.h>
  30. #include <api_public.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #define DEBUG
  33. #undef DEBUG
  34. #if !defined(CONFIG_NET_MULTI)
  35. #error "API/net is currently only available for platforms with CONFIG_NET_MULTI"
  36. #endif
  37. #ifdef DEBUG
  38. #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
  39. #else
  40. #define debugf(fmt, args...)
  41. #endif
  42. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  43. static int dev_valid_net(void *cookie)
  44. {
  45. return ((void *)eth_get_dev() == cookie) ? 1 : 0;
  46. }
  47. int dev_open_net(void *cookie)
  48. {
  49. if (!dev_valid_net(cookie))
  50. return API_ENODEV;
  51. if (eth_init(gd->bd) < 0)
  52. return API_EIO;
  53. return 0;
  54. }
  55. int dev_close_net(void *cookie)
  56. {
  57. if (!dev_valid_net(cookie))
  58. return API_ENODEV;
  59. eth_halt();
  60. return 0;
  61. }
  62. /*
  63. * There can only be one active eth interface at a time - use what is
  64. * currently set to eth_current
  65. */
  66. int dev_enum_net(struct device_info *di)
  67. {
  68. struct eth_device *eth_current = eth_get_dev();
  69. di->type = DEV_TYP_NET;
  70. di->cookie = (void *)eth_current;
  71. if (di->cookie == NULL)
  72. return 0;
  73. memcpy(di->di_net.hwaddr, eth_current->enetaddr, 6);
  74. debugf("device found, returning cookie 0x%08x\n",
  75. (u_int32_t)di->cookie);
  76. return 1;
  77. }
  78. int dev_write_net(void *cookie, void *buf, int len)
  79. {
  80. /* XXX verify that cookie points to a valid net device??? */
  81. return eth_send(buf, len);
  82. }
  83. int dev_read_net(void *cookie, void *buf, int len)
  84. {
  85. /* XXX verify that cookie points to a valid net device??? */
  86. return eth_receive(buf, len);
  87. }
  88. #endif /* CONFIG_API */