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