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