diff options
author | Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | 2012-10-30 22:00:16 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2012-11-15 20:20:28 +0100 |
commit | 520cd660cd7b697a47d109e0b00d0672b080b1b3 (patch) | |
tree | cc9566b7317eddf4baf021b5e58b0ef39b1cdc93 /include | |
parent | e14a55a4c0e39f5fc9533621c685451b8b4c65fc (diff) | |
download | barebox-520cd660cd7b697a47d109e0b00d0672b080b1b3.tar.gz barebox-520cd660cd7b697a47d109e0b00d0672b080b1b3.tar.xz |
w1: introduce helper to generate mac address from 1-wire id
use the first 3 byte of the id of a 1-wire
or 6 if no OUI provided device to provide an Ethernet address
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include')
-rw-r--r-- | include/w1_mac_address.h | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/include/w1_mac_address.h b/include/w1_mac_address.h new file mode 100644 index 0000000000..89dd914095 --- /dev/null +++ b/include/w1_mac_address.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnio@jcrosoft.com> + * + * Under GPLv2 only + */ + +#ifndef __W1_MAC_ADDRESS_H__ +#define __W1_MAC_ADDRESS_H__ + +/** + * w1_local_mac_address_register - use the first 3 byte of the id of a 1-wire + * or 6 if no OUI provided device to provide an Ethernet address + * @ethid: ethernet device id + * @oui: Ethernet OUI (3 bytes) + * @w1_dev: 1-wire device name + * + * Generate a local Ethernet address (MAC) that is not multicast using a 1-wire id. + */ +static inline int w1_local_mac_address_register(int ethid, char * oui, char *w1_dev) +{ + char addr[6]; + const char *val; + u64 id; + int nb_oui = 0; + int i, shift; + char *tmp = NULL; + int ret = 0; + + if (oui) { + nb_oui = 3; + + for (i = 0; i < nb_oui; i++) + addr[i] = oui[i]; + } + + tmp = asprintf("%s.id", w1_dev); + if (!tmp) + return -ENOMEM; + + val = getenv(tmp); + if (!val) { + ret = -EINVAL; + goto err; + } + + id = simple_strtoull(val, NULL, 16); + if (!id) { + ret = -EINVAL; + goto err; + } + + for (i = nb_oui, shift = 40; i < 6; i++, shift -= 8) + addr[i] = (id >> shift) & 0xff; + + addr[0] &= 0xfe; /* clear multicast bit */ + addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ + + eth_register_ethaddr(ethid, addr); + +err: + free(tmp); + return ret; +} + +#endif /* __W1_MAC_ADDRESS_H__ */ |