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