blob: 264ff530563b913a2db352176901c00f99c4f01c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rafal Jaworowski500856e2008-01-09 19:39:36 +01002/*
3 * (C) Copyright 2007 Semihalf
4 *
5 * Written by: Rafal Jaworowski <raj@semihalf.com>
Rafal Jaworowski500856e2008-01-09 19:39:36 +01006 */
7
8#include <config.h>
Rafal Jaworowski500856e2008-01-09 19:39:36 +01009#include <net.h>
10#include <linux/types.h>
11#include <api_public.h>
12
Rafal Jaworowski500856e2008-01-09 19:39:36 +010013#define DEBUG
14#undef DEBUG
15
Rafal Jaworowski500856e2008-01-09 19:39:36 +010016#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
Michal Simek21181572016-06-06 10:58:40 +020024#if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
Rafal Jaworowski500856e2008-01-09 19:39:36 +010025
26static int dev_valid_net(void *cookie)
27{
28 return ((void *)eth_get_dev() == cookie) ? 1 : 0;
29}
30
31int dev_open_net(void *cookie)
32{
33 if (!dev_valid_net(cookie))
34 return API_ENODEV;
35
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050036 if (eth_init() < 0)
Rafal Jaworowski500856e2008-01-09 19:39:36 +010037 return API_EIO;
38
39 return 0;
40}
41
42int 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
Wolfgang Denkd3a65322008-01-10 00:55:14 +010051/*
Rafal Jaworowski500856e2008-01-09 19:39:36 +010052 * There can only be one active eth interface at a time - use what is
53 * currently set to eth_current
54 */
55int 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
72int 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
79int 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}
Jeroen Hofstee11db3ab2014-08-10 00:30:55 +020085
86#else
87
88int dev_open_net(void *cookie)
89{
90 return API_ENODEV;
91}
92
93int dev_close_net(void *cookie)
94{
95 return API_ENODEV;
96}
97
98int dev_enum_net(struct device_info *di)
99{
100 return 0;
101}
102
103int dev_write_net(void *cookie, void *buf, int len)
104{
105 return API_ENODEV;
106}
107
108int dev_read_net(void *cookie, void *buf, int len)
109{
110 return API_ENODEV;
111}
112
113#endif