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 | |
| 28 | |
| 29 | static int dev_valid_net(void *cookie) |
| 30 | { |
| 31 | return ((void *)eth_get_dev() == cookie) ? 1 : 0; |
| 32 | } |
| 33 | |
| 34 | int dev_open_net(void *cookie) |
| 35 | { |
| 36 | if (!dev_valid_net(cookie)) |
| 37 | return API_ENODEV; |
| 38 | |
| 39 | if (eth_init(gd->bd) < 0) |
| 40 | return API_EIO; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int dev_close_net(void *cookie) |
| 46 | { |
| 47 | if (!dev_valid_net(cookie)) |
| 48 | return API_ENODEV; |
| 49 | |
| 50 | eth_halt(); |
| 51 | return 0; |
| 52 | } |
| 53 | |
Wolfgang Denk | 545b06a | 2008-01-10 00:55:14 +0100 | [diff] [blame] | 54 | /* |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 55 | * There can only be one active eth interface at a time - use what is |
| 56 | * currently set to eth_current |
| 57 | */ |
| 58 | int dev_enum_net(struct device_info *di) |
| 59 | { |
| 60 | struct eth_device *eth_current = eth_get_dev(); |
| 61 | |
| 62 | di->type = DEV_TYP_NET; |
| 63 | di->cookie = (void *)eth_current; |
| 64 | if (di->cookie == NULL) |
| 65 | return 0; |
| 66 | |
| 67 | memcpy(di->di_net.hwaddr, eth_current->enetaddr, 6); |
| 68 | |
| 69 | debugf("device found, returning cookie 0x%08x\n", |
| 70 | (u_int32_t)di->cookie); |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | int dev_write_net(void *cookie, void *buf, int len) |
| 76 | { |
| 77 | /* XXX verify that cookie points to a valid net device??? */ |
| 78 | |
| 79 | return eth_send(buf, len); |
| 80 | } |
| 81 | |
| 82 | int dev_read_net(void *cookie, void *buf, int len) |
| 83 | { |
| 84 | /* XXX verify that cookie points to a valid net device??? */ |
| 85 | |
| 86 | return eth_receive(buf, len); |
| 87 | } |