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