blob: 7515c26e8b44edd3a4cc2e8184776cc97a4612c5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rafal Jaworowskia19be782008-01-09 19:39:36 +01002/*
3 * (C) Copyright 2007 Semihalf
4 *
5 * Written by: Rafal Jaworowski <raj@semihalf.com>
Rafal Jaworowskia19be782008-01-09 19:39:36 +01006 */
7
8#include <config.h>
Rafal Jaworowskia19be782008-01-09 19:39:36 +01009#include <common.h>
10#include <net.h>
11#include <linux/types.h>
12#include <api_public.h>
13
Rafal Jaworowskia19be782008-01-09 19:39:36 +010014#define DEBUG
15#undef DEBUG
16
Rafal Jaworowskia19be782008-01-09 19:39:36 +010017#ifdef DEBUG
18#define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
19#else
20#define debugf(fmt, args...)
21#endif
22
23#define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
24
Michal Simeka5520922016-06-06 10:58:40 +020025#if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
Rafal Jaworowskia19be782008-01-09 19:39:36 +010026
27static int dev_valid_net(void *cookie)
28{
29 return ((void *)eth_get_dev() == cookie) ? 1 : 0;
30}
31
32int dev_open_net(void *cookie)
33{
34 if (!dev_valid_net(cookie))
35 return API_ENODEV;
36
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050037 if (eth_init() < 0)
Rafal Jaworowskia19be782008-01-09 19:39:36 +010038 return API_EIO;
39
40 return 0;
41}
42
43int dev_close_net(void *cookie)
44{
45 if (!dev_valid_net(cookie))
46 return API_ENODEV;
47
48 eth_halt();
49 return 0;
50}
51
Wolfgang Denk545b06a2008-01-10 00:55:14 +010052/*
Rafal Jaworowskia19be782008-01-09 19:39:36 +010053 * There can only be one active eth interface at a time - use what is
54 * currently set to eth_current
55 */
56int dev_enum_net(struct device_info *di)
57{
58 struct eth_device *eth_current = eth_get_dev();
59
60 di->type = DEV_TYP_NET;
61 di->cookie = (void *)eth_current;
62 if (di->cookie == NULL)
63 return 0;
64
65 memcpy(di->di_net.hwaddr, eth_current->enetaddr, 6);
66
67 debugf("device found, returning cookie 0x%08x\n",
68 (u_int32_t)di->cookie);
69
70 return 1;
71}
72
73int dev_write_net(void *cookie, void *buf, int len)
74{
75 /* XXX verify that cookie points to a valid net device??? */
76
77 return eth_send(buf, len);
78}
79
80int dev_read_net(void *cookie, void *buf, int len)
81{
82 /* XXX verify that cookie points to a valid net device??? */
83
84 return eth_receive(buf, len);
85}
Jeroen Hofsteeabeeffb2014-08-10 00:30:55 +020086
87#else
88
89int dev_open_net(void *cookie)
90{
91 return API_ENODEV;
92}
93
94int dev_close_net(void *cookie)
95{
96 return API_ENODEV;
97}
98
99int dev_enum_net(struct device_info *di)
100{
101 return 0;
102}
103
104int dev_write_net(void *cookie, void *buf, int len)
105{
106 return API_ENODEV;
107}
108
109int dev_read_net(void *cookie, void *buf, int len)
110{
111 return API_ENODEV;
112}
113
114#endif