Make MAC address configurable using hw files.
[bertos.git] / bertos / drv / eth.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief Ethernet standard descriptors
36  *
37  * $WIZ$ module_name = "eth"
38  * $WIZ$ module_configuration = "bertos/cfg/cfg_eth.h"
39  * $WIZ$ module_supports = "at91sam7x or sam3x"
40  * $WIZ$ module_hw = "bertos/hw/hw_eth.h", "bertos/hw/hw_eth.c"
41  */
42
43 #ifndef DRV_ETH_H
44 #define DRV_ETH_H
45
46 #include "hw/hw_eth.h"
47 #include <cpu/types.h>
48
49 #define ETH_ADDR_LEN    6
50 #define ETH_HEAD_LEN    14
51 #define ETH_DATA_LEN    1500
52 #define ETH_FRAME_LEN   (ETH_HEAD_LEN + ETH_DATA_LEN)
53
54 #define ETH_TYPE_IP     0x0800
55
56 typedef union Ethernet
57 {
58         struct
59         {
60                 uint8_t  dst[ETH_ADDR_LEN];
61                 uint8_t  src[ETH_ADDR_LEN];
62                 uint16_t type;
63                 uint8_t data[0];
64         };
65         uint8_t raw[ETH_FRAME_LEN];
66 } PACKED Ethernet;
67
68 /**
69  * Determine if ethernet address \a addr is a all zero.
70  */
71 INLINE int eth_addrIsZero(const uint8_t *addr)
72 {
73         return !(addr[0] | addr[1] | addr[2] |
74                  addr[3] | addr[4] | addr[5]);
75 }
76
77 /**
78  * Determine if ethernet address \a addr is a multicast address.
79  */
80 INLINE int eth_addrIsMcast(const uint8_t *addr)
81 {
82         return (0x01 & addr[0]);
83 }
84
85 /**
86  * Determine if ethernet address \a addr is locally-assigned (IEEE 802).
87  */
88 INLINE int eth_addrIsLocal(const uint8_t *addr)
89 {
90         return (0x02 & addr[0]);
91 }
92
93 /**
94  * Determine if ethernet address \a addr is broadcast.
95  */
96 INLINE bool eth_addrIsBcast(const uint8_t *addr)
97 {
98         return (addr[0] & addr[1] & addr[2] &
99                 addr[3] & addr[4] & addr[5]) == 0xff;
100 }
101
102 /**
103  * Check if the ethernet address \a addr is not all zero, is not a multicast
104  * address, and is not broadcast.
105  */
106 INLINE bool eth_addrIsValid(const uint8_t *addr)
107 {
108         return !eth_addrIsMcast(addr) && !eth_addrIsZero(addr);
109 }
110
111 /**
112  * Compare two ethernet addresses: \a addr1 and \a addr2, returns 0 if equal.
113  */
114 INLINE bool eth_addrCmp(const uint8_t *addr1, const uint8_t *addr2)
115 {
116         return !!((addr1[0] ^ addr2[0]) |
117                         (addr1[1] ^ addr2[1]) |
118                         (addr1[2] ^ addr2[2]) |
119                         (addr1[3] ^ addr2[3]) |
120                         (addr1[4] ^ addr2[4]) |
121                         (addr1[5] ^ addr2[5]));
122 }
123
124 ssize_t eth_putFrame(const uint8_t *buf, size_t len);
125 void eth_sendFrame(void);
126
127 size_t eth_getFrameLen(void);
128 ssize_t eth_getFrame(uint8_t *buf, size_t len);
129
130 ssize_t eth_send(const uint8_t *buf, size_t len);
131 ssize_t eth_recv(uint8_t *buf, size_t len);
132
133 int eth_init(void);
134
135 #endif /* DRV_ETH_H */