-
Anjan Chanda authoredAnjan Chanda authored
ethernet.c 5.87 KiB
/*
* ethernet.c - file implements library APIs
*
* Copyright (C) 2018 Inteno Broadband Technology AB. All rights reserved.
*
* Author: anjan.chanda@inteno.se
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <stdbool.h>
#include <linux/sockios.h>
#include <linux/mii.h>
#include "ethernet.h"
#ifdef IOPSYS_BROADCOM
extern const struct eth_ops bcm_eth_ops;
#else
extern const struct eth_ops ethsw_ops;
#endif
const struct eth_ops *eth_ops[] = {
#ifdef IOPSYS_BROADCOM
&bcm_eth_ops,
#else
ðsw_ops,
#endif
};
const struct eth_ops *get_eth_driver(char *name)
{
int i;
for (i = 0; i < sizeof(eth_ops)/sizeof(eth_ops[0]); i++)
if (!strncmp(eth_ops[i]->name, name,
strlen(eth_ops[i]->name)))
return eth_ops[i];
return NULL;
}
int eth_up(char *name)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->up)
return eth->up(name);
return -1;
}
int eth_down(char *name)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->down)
return eth->down(name);
return -1;
}
int eth_get_link_settings(char *name, struct eth_link *link)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_link_settings)
return eth->get_link_settings(name, link);
return -1;
}
int eth_set_link_settings(char *name, struct eth_link link)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->set_link_settings)
return eth->set_link_settings(name, link);
return -1;
}
int eth_get_operstate(char *name, enum eth_operstate *op)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_operstate)
return eth->get_operstate(name, op);
return -1;
}
int eth_set_operstate(char *name, enum eth_operstate op)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->set_operstate)
return eth->set_operstate(name, op);
return -1;
}
int eth_get_stats(char *name, struct eth_counters *c)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_stats)
return eth->get_stats(name, c);
return -1;
}
int eth_poweron_phy(char *name, struct eth_phy p)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->poweron_phy)
return eth->poweron_phy(name, p);
return -1;
}
int eth_poweroff_phy(char *name, struct eth_phy p)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->poweroff_phy)
return eth->poweroff_phy(name, p);
return -1;
}
int eth_reset_phy(char *name, int phy_id)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->reset_phy)
return eth->reset_phy(name, phy_id);
else
return eth_mii_reset_phy(name, phy_id);
}
int eth_get_phy_id(char *name, int port, int *phy_id)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_phy_id)
return eth->get_phy_id(name, port, phy_id);
else
return eth_mii_get_phy_id(name, port, phy_id);
}
int eth_ioctl(char *name, int cmd, void *in, int len)
{
struct ifreq ifr;
int s;
strncpy(ifr.ifr_name, name, IFNAMSIZ);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "SIOCGIFINDEX failed!\n");
close(s);
return -1;
}
if (len && in)
ifr.ifr_data = (caddr_t)in;
if (ioctl(s, cmd, &ifr) < 0) {
close(s);
return -1;
}
close(s);
return 0;
}
int eth_mii_get_phy_id(char *ifname, int port, int *phy_id)
{
struct mii_ioctl_data *mii;
struct ifreq ifr;
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "SIOCGIFINDEX failed!\n");
close(s);
return -1;
}
mii = if_mii(&ifr);
memset(mii, 0, sizeof(struct mii_ioctl_data));
mii->val_in = port;
if (ioctl(s, SIOCGMIIPHY, &ifr) < 0) {
fprintf(stderr, "SIOCGMIIPHY failed!\n");
close(s);
return -1;
}
if (phy_id)
*phy_id = mii->phy_id;
//fprintf(stderr, "%s: phy_id = %d\n", ifname, *phy_id);
close(s);
return 0;
}
static int eth_mii_ioctl(char *name, int cmd, int phy_id, int reg,
int in, int *out)
{
struct mii_ioctl_data *mii;
struct ifreq ifr;
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, name, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "SIOCGIFINDEX failed!\n");
close(s);
return -1;
}
mii = if_mii(&ifr);
memset(mii, 0, sizeof(struct mii_ioctl_data));
//PHYID_2_MII_IOCTL(phy_id, mii);
mii->phy_id = phy_id;
mii->reg_num = reg;
mii->val_in = in;
if (ioctl(s, cmd, &ifr) < 0) {
fprintf(stderr, "MII cmd on %s failed\n", ifr.ifr_name);
close(s);
return -1;
}
if (out)
*out = mii->val_out; // FIXME?
close(s);
return 0;
}
int eth_mii_read(char *name, int phy_id, int reg, int *out)
{
return eth_mii_ioctl(name, SIOCGMIIREG, phy_id, reg, 0, out);
}
int eth_mii_write(char *name, int phy_id, int reg, int in)
{
return eth_mii_ioctl(name, SIOCGMIIREG, phy_id, reg, in, NULL);
}
int eth_mii_reset_phy(char *name, int phy_id)
{
return eth_mii_ioctl(name, SIOCSMIIREG, phy_id,
MII_BMCR, BMCR_RESET, NULL);
}