Statistics
| Branch: | Tag: | Revision:

root / firmware / zpu / lib / eth_addrs.c @ a967cbd3

History | View | Annotate | Download (3.11 KB)

1 8a695c06 Josh Blum
/*
2 a967cbd3 Josh Blum
 * Copyright 2010-2012 Ettus Research LLC
3 8a695c06 Josh Blum
 * Copyright 2007 Free Software Foundation, Inc.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#include "ethernet.h"
20
#include "memory_map.h"
21
#include "nonstdio.h"
22
#include <stdbool.h>
23 a967cbd3 Josh Blum
#include <string.h>
24 8a695c06 Josh Blum
#include "i2c.h"
25
#include "usrp2/fw_common.h"
26
27
static bool
28
unprogrammed(const void *t, size_t len)
29
{
30
  int i;
31
  uint8_t *p = (uint8_t *)t;
32
  bool all_zeros = true;
33
  bool all_ones =  true;
34
  for (i = 0; i < len; i++){
35
    all_zeros &= p[i] == 0x00;
36
    all_ones  &= p[i] == 0xff;
37
  }
38
  return all_ones | all_zeros;
39
}
40
41 a967cbd3 Josh Blum
typedef struct{
42
    eth_mac_addr_t mac_addr;
43
    struct ip_addr ip_addr;
44
    struct ip_addr gateway;
45
    struct ip_addr subnet;
46
} eth_addrs_t;
47 8a695c06 Josh Blum
48 a967cbd3 Josh Blum
static bool eth_addrs_initialized = false;
49 a2dfa198 Nick Foster
50 a967cbd3 Josh Blum
static const eth_addrs_t default_eth_addrs = {
51
    .mac_addr = {{0x00, 0x50, 0xC2, 0x85, 0x3f, 0xff}},
52
    .ip_addr = {(192 << 24 | 168 << 16 | 10  << 8  | 2 << 0)},
53
    .gateway = {(192 << 24 | 168 << 16 | 10  << 8  | 1 << 0)},
54
    .subnet  = {(255 << 24 | 255 << 16 | 255 << 8  | 0 << 0)},
55
};
56 a2dfa198 Nick Foster
57 a967cbd3 Josh Blum
static eth_addrs_t current_eth_addrs;
58 8a695c06 Josh Blum
59 a967cbd3 Josh Blum
static void eth_addrs_init(void){
60
    if (eth_addrs_initialized) return;
61
    eth_addrs_initialized = true;
62 8a695c06 Josh Blum
63 a967cbd3 Josh Blum
    #define eth_addrs_init_x(addr, x){ \
64
        const bool ok = eeprom_read(USRP2_I2C_ADDR_MBOARD, addr, &current_eth_addrs.x, sizeof(current_eth_addrs.x)); \
65
        if (!ok || unprogrammed(&current_eth_addrs.x, sizeof(current_eth_addrs.x))){ \
66
            memcpy(&current_eth_addrs.x, &default_eth_addrs.x, sizeof(current_eth_addrs.x)); \
67
        } \
68 8a695c06 Josh Blum
    }
69
70 a967cbd3 Josh Blum
    eth_addrs_init_x(USRP2_EE_MBOARD_MAC_ADDR, mac_addr);
71
    eth_addrs_init_x(USRP2_EE_MBOARD_IP_ADDR,  ip_addr);
72
    eth_addrs_init_x(USRP2_EE_MBOARD_GATEWAY,  gateway);
73
    eth_addrs_init_x(USRP2_EE_MBOARD_SUBNET,   subnet);
74 8a695c06 Josh Blum
75
}
76
77 a967cbd3 Josh Blum
const eth_mac_addr_t *ethernet_mac_addr(void){
78
    eth_addrs_init();
79
    return &current_eth_addrs.mac_addr;
80 a2dfa198 Nick Foster
}
81 8a695c06 Josh Blum
82 a967cbd3 Josh Blum
const struct ip_addr *get_ip_addr(void){
83
    eth_addrs_init();
84
    return &current_eth_addrs.ip_addr;
85
}
86 8a695c06 Josh Blum
87 a967cbd3 Josh Blum
const struct ip_addr *get_subnet(void){
88
    eth_addrs_init();
89
    return &current_eth_addrs.subnet;
90
}
91 8a695c06 Josh Blum
92 a967cbd3 Josh Blum
const struct ip_addr *get_gateway(void){
93
    eth_addrs_init();
94
    return &current_eth_addrs.gateway;
95 8a695c06 Josh Blum
}
96
97
bool set_ip_addr(const struct ip_addr *t){
98 a967cbd3 Josh Blum
    const bool ok = eeprom_write(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_IP_ADDR, t, sizeof(struct ip_addr));
99
    if (ok) current_eth_addrs.ip_addr = *t;
100
    return ok;
101
}
102 8a695c06 Josh Blum
103 a967cbd3 Josh Blum
void eth_addrs_set_default(void){
104
    eth_addrs_initialized = true;
105
    memcpy(&current_eth_addrs, &default_eth_addrs, sizeof(default_eth_addrs));
106 8a695c06 Josh Blum
}