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 <common.h>
10 #include <net.h>
11 #include <linux/types.h>
12 #include <api_public.h>
13 
14 #define DEBUG
15 #undef DEBUG
16 
17 #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 
25 #if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
26 
dev_valid_net(void * cookie)27 static int dev_valid_net(void *cookie)
28 {
29 	return ((void *)eth_get_dev() == cookie) ? 1 : 0;
30 }
31 
dev_open_net(void * cookie)32 int dev_open_net(void *cookie)
33 {
34 	if (!dev_valid_net(cookie))
35 		return API_ENODEV;
36 
37 	if (eth_init() < 0)
38 		return API_EIO;
39 
40 	return 0;
41 }
42 
dev_close_net(void * cookie)43 int 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 
52 /*
53  * There can only be one active eth interface at a time - use what is
54  * currently set to eth_current
55  */
dev_enum_net(struct device_info * di)56 int 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 
dev_write_net(void * cookie,void * buf,int len)73 int 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 
dev_read_net(void * cookie,void * buf,int len)80 int 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 }
86 
87 #else
88 
dev_open_net(void * cookie)89 int dev_open_net(void *cookie)
90 {
91 	return API_ENODEV;
92 }
93 
dev_close_net(void * cookie)94 int dev_close_net(void *cookie)
95 {
96 	return API_ENODEV;
97 }
98 
dev_enum_net(struct device_info * di)99 int dev_enum_net(struct device_info *di)
100 {
101 	return 0;
102 }
103 
dev_write_net(void * cookie,void * buf,int len)104 int dev_write_net(void *cookie, void *buf, int len)
105 {
106 	return API_ENODEV;
107 }
108 
dev_read_net(void * cookie,void * buf,int len)109 int dev_read_net(void *cookie, void *buf, int len)
110 {
111 	return API_ENODEV;
112 }
113 
114 #endif
115