Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.49 KB)

1
/*
2
 * Copyright 2010-2011 Ettus Research LLC
3
 * 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
#include "i2c.h"
24
#include "usrp2/fw_common.h"
25

    
26
static bool
27
unprogrammed(const void *t, size_t len)
28
{
29
  int i;
30
  uint8_t *p = (uint8_t *)t;
31
  bool all_zeros = true;
32
  bool all_ones =  true;
33
  for (i = 0; i < len; i++){
34
    all_zeros &= p[i] == 0x00;
35
    all_ones  &= p[i] == 0xff;
36
  }
37
  return all_ones | all_zeros;
38
}
39

    
40
//////////////////// MAC Addr Stuff ///////////////////////
41

    
42
static bool src_mac_addr_initialized = false;
43

    
44
static const eth_mac_addr_t default_mac_addr = {{
45
    0x00, 0x50, 0xC2, 0x85, 0x3f, 0xff
46
  }};
47

    
48
static eth_mac_addr_t src_mac_addr = {{
49
    0x00, 0x50, 0xC2, 0x85, 0x3f, 0xff
50
  }};
51
  
52
void set_default_mac_addr(void)
53
{
54
    src_mac_addr_initialized = true;
55
    src_mac_addr = default_mac_addr;
56
}
57

    
58
const eth_mac_addr_t *
59
ethernet_mac_addr(void)
60
{
61
  if (!src_mac_addr_initialized){    // fetch from eeprom
62
    src_mac_addr_initialized = true;
63

    
64
    // if we're simulating, don't read the EEPROM model, it's REALLY slow
65
    if (hwconfig_simulation_p())
66
      return &src_mac_addr;
67

    
68
    eth_mac_addr_t tmp;
69
    bool ok = eeprom_read(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_MAC_ADDR, &tmp, sizeof(tmp));
70
    if (!ok || unprogrammed(&tmp, sizeof(tmp))){
71
      // use the default
72
    }
73
    else
74
      src_mac_addr = tmp;
75
  }
76

    
77
  return &src_mac_addr;
78
}
79

    
80
bool
81
ethernet_set_mac_addr(const eth_mac_addr_t *t)
82
{
83
  bool ok = eeprom_write(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_MAC_ADDR, t, sizeof(eth_mac_addr_t));
84
  if (ok){
85
    src_mac_addr = *t;
86
    src_mac_addr_initialized = true;
87
    //eth_mac_set_addr(t); //this breaks the link
88
  }
89

    
90
  return ok;
91
}
92

    
93
//////////////////// IP Addr Stuff ///////////////////////
94

    
95
static bool src_ip_addr_initialized = false;
96

    
97
static const struct ip_addr default_ip_addr = {
98
    (192 << 24 | 168 << 16 | 10 << 8 | 2 << 0)
99
};
100

    
101
static struct ip_addr src_ip_addr = {
102
    (192 << 24 | 168 << 16 | 10 << 8 | 2 << 0)
103
};
104

    
105
void set_default_ip_addr(void)
106
{
107
    src_ip_addr_initialized = true;
108
    src_ip_addr = default_ip_addr;
109
}
110

    
111
const struct ip_addr *get_ip_addr(void)
112
{
113
  if (!src_ip_addr_initialized){    // fetch from eeprom
114
    src_ip_addr_initialized = true;
115

    
116
    // if we're simulating, don't read the EEPROM model, it's REALLY slow
117
    if (hwconfig_simulation_p())
118
      return &src_ip_addr;
119

    
120
    struct ip_addr tmp;
121
    bool ok = eeprom_read(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_IP_ADDR, &tmp, sizeof(tmp));
122
    if (!ok || unprogrammed(&tmp, sizeof(tmp))){
123
      // use the default
124
    }
125
    else
126
      src_ip_addr = tmp;
127
  }
128

    
129
  return &src_ip_addr;
130
}
131

    
132
bool set_ip_addr(const struct ip_addr *t){
133
  bool ok = eeprom_write(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_IP_ADDR, t, sizeof(struct ip_addr));
134
  if (ok){
135
    src_ip_addr = *t;
136
    src_ip_addr_initialized = true;
137
  }
138

    
139
  return ok;
140
}