Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007 Semihalf |
| 4 | * |
| 5 | * Written by: Rafal Jaworowski <raj@semihalf.com> |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <config.h> |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 9 | #include <net.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <api_public.h> |
| 12 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 13 | #define DEBUG |
| 14 | #undef DEBUG |
| 15 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 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 | |
Michal Simek | 2118157 | 2016-06-06 10:58:40 +0200 | [diff] [blame] | 24 | #if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH) |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 25 | |
| 26 | static int dev_valid_net(void *cookie) |
| 27 | { |
| 28 | return ((void *)eth_get_dev() == cookie) ? 1 : 0; |
| 29 | } |
| 30 | |
| 31 | int dev_open_net(void *cookie) |
| 32 | { |
| 33 | if (!dev_valid_net(cookie)) |
| 34 | return API_ENODEV; |
| 35 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 36 | if (eth_init() < 0) |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 37 | return API_EIO; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 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 | |
Wolfgang Denk | d3a6532 | 2008-01-10 00:55:14 +0100 | [diff] [blame] | 51 | /* |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 52 | * There can only be one active eth interface at a time - use what is |
| 53 | * currently set to eth_current |
| 54 | */ |
| 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 | |
| 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 | |
| 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 | } |
Jeroen Hofstee | 11db3ab | 2014-08-10 00:30:55 +0200 | [diff] [blame] | 85 | |
| 86 | #else |
| 87 | |
| 88 | int dev_open_net(void *cookie) |
| 89 | { |
| 90 | return API_ENODEV; |
| 91 | } |
| 92 | |
| 93 | int dev_close_net(void *cookie) |
| 94 | { |
| 95 | return API_ENODEV; |
| 96 | } |
| 97 | |
| 98 | int dev_enum_net(struct device_info *di) |
| 99 | { |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int dev_write_net(void *cookie, void *buf, int len) |
| 104 | { |
| 105 | return API_ENODEV; |
| 106 | } |
| 107 | |
| 108 | int dev_read_net(void *cookie, void *buf, int len) |
| 109 | { |
| 110 | return API_ENODEV; |
| 111 | } |
| 112 | |
| 113 | #endif |