Description:modify network port driver code.

Feature or Bugfix:Feature
Binary Source: No

Signed-off-by: juchenyang <juchenyang@huawei.com>
This commit is contained in:
juchenyang
2021-07-29 03:44:54 +00:00
parent 787ccce8b5
commit fda15e0783
10 changed files with 754 additions and 14 deletions
+58
View File
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef ETH_CHIP_DRIVER_H
#define ETH_CHIP_DRIVER_H
#include "eth_device.h"
#include "net_device.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_CHIPDRIVER_COUNT 16
struct EthMacOps {
void (*MacInit)(void);
int32_t (*PortReset)(struct EthDevice *ethDevice);
int32_t (*PortInit)(struct EthDevice *ethDevice);
};
struct HdfEthMacChipDriver {
struct EthMacOps *ethMacOps; /**< Ethernet Mac some basic methods */
};
struct HdfEthNetDeviceData {
struct HdfEthMacChipDriver *macChipDriver; /**< Mac ChipDriver */
};
struct HdfEthNetDeviceData *GetEthNetDeviceData(const struct NetDevice *netDev);
struct HdfEthChipDriverFactory {
const char *driverName;
int32_t (*InitEthDriver)(struct EthDevice *ethDevice);
int32_t (*DeinitEthDriver)(struct EthDevice *ethDevice);
struct HdfEthMacChipDriver *(*BuildMacDriver)(void);
void (*ReleaseMacDriver)(struct HdfEthMacChipDriver *chipDriver);
void (*GetMacAddr)(unsigned char *addr, int len);
};
struct HdfEthChipDriverManager {
struct HdfEthChipDriverFactory **chipFactoryInsts;
int32_t (*RegChipDriver)(struct HdfEthChipDriverFactory *factoryInst);
struct HdfEthChipDriverFactory *(*GetEthChipDriverByName)(const char *name);
};
struct HdfEthChipDriverManager *HdfEthGetChipDriverMgr(void);
#ifdef __cplusplus
}
#endif
#endif /* ETH_CHIP_DRIVER_H */
+20 -4
View File
@@ -465,7 +465,7 @@ typedef struct NetDevice {
char name[IFNAMSIZ]; /**< Network device name {@link IFNAMSIZ} */
NetLinkType LinkLayerType; /**< Data link layer type */
IfType funType; /**< Network port type */
unsigned char macAddr[MAC_ADDR_SIZE]; /**< MAC address {@link MAC_ADDR_SIZE} */
uint8_t macAddr[MAC_ADDR_SIZE]; /**< MAC address {@link MAC_ADDR_SIZE} */
uint32_t flags; /**< Network port status */
uint32_t mtu; /**< Maximum transmission unit */
int32_t watchdogTime; /**< Watchdog duration */
@@ -506,6 +506,9 @@ struct NetDeviceInterFace {
int32_t (*changeMtu)(struct NetDevice *netDev, int32_t newMtu); /**< Changes the maximum number of
* transmission units.
*/
void (*linkStatusChanged)(struct NetDevice *netDev); /**< Detects the change of
* the Ethernet port connection status.
*/
ProcessingResult (*specialEtherTypeProcess)(const struct NetDevice *netDev, NetBuf *buff);
/**< Performs private processing without
* involving network-layer data.
@@ -517,6 +520,7 @@ struct NetDeviceInterFace {
*
* @param ifName Indicates the pointer to the network device name.
* @param len Indicates the length of the network device name.
* @param type Indicates the data link type.
* @param ifCategory Indicates the network port category.
*
* @return Returns the structure {@link NetDevice} for the initialized network device if the operation is successful;
@@ -525,7 +529,7 @@ struct NetDeviceInterFace {
* @since 1.0
* @version 1.0
*/
struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetIfCategory ifCategory);
struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetLinkType type, NetIfCategory ifCategory);
/**
* @brief Deletes a network device.
@@ -544,7 +548,6 @@ int32_t NetDeviceDeInit(struct NetDevice *netDevice);
* @brief Adds a network device to a protocol stack.
*
* @param netDevice Indicates the pointer to the network device structure obtained during initialization.
* @param ifType Indicates the network port type, as enumerated in {@link Protocol80211IfType}.
*
* @return Returns <b>0</b> if the operation is successful; returns a negative value representing {@link HDF_STATUS}
* if the operation fails.
@@ -552,7 +555,7 @@ int32_t NetDeviceDeInit(struct NetDevice *netDevice);
* @since 1.0
* @version 1.0
*/
int32_t NetDeviceAdd(struct NetDevice *netDevice, Protocol80211IfType ifType);
int32_t NetDeviceAdd(struct NetDevice *netDevice);
/**
* @brief Deletes a network device from a protocol stack.
@@ -695,6 +698,19 @@ int32_t NetIfSetMacAddr(struct NetDevice *netDevice, const unsigned char *macAdd
*/
int32_t NetIfSetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus status);
/**
* @brief Get the netdevice data link layer status.
*
* @param netDevice Indicates the pointer to the network device obtained during initialization.
* @param status save link layer status, as enumerated in {@link NetIfLinkSatus}.
*
* @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
*
* @since 1.0
* @version 1.0
*/
int32_t NetIfGetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus *status);
/**
* @brief Transfers the input data packets from the network side to a protocol stack.
*
+23 -3
View File
@@ -121,7 +121,7 @@ static struct NetDeviceImpl *GetImplByNetDevice(const struct NetDevice *netDevic
return ndImpl;
}
struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetIfCategory ifCategory)
struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetLinkType type, NetIfCategory ifCategory)
{
NetDevice *netDevice = NULL;
struct NetDeviceImpl *ndImpl = NULL;
@@ -167,6 +167,7 @@ struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetIfCategory
DeInitNetDeviceImpl(ndImpl);
return NULL;
}
netDevice->LinkLayerType = type;
HDF_LOGI("Init Net Device success!");
return netDevice;
}
@@ -190,7 +191,7 @@ int32_t NetDeviceDeInit(struct NetDevice *netDevice)
return HDF_SUCCESS;
}
int32_t NetDeviceAdd(struct NetDevice *netDevice, Protocol80211IfType ifType)
int32_t NetDeviceAdd(struct NetDevice *netDevice)
{
struct NetDeviceImplOp *op = NULL;
struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
@@ -204,7 +205,7 @@ int32_t NetDeviceAdd(struct NetDevice *netDevice, Protocol80211IfType ifType)
HDF_LOGE("%s fail: Impl Add not exist.", __func__);
return HDF_ERR_INVALID_PARAM;
}
return op->add(ndImpl, ifType);
return op->add(ndImpl);
}
int32_t NetDeviceDelete(struct NetDevice *netDevice)
@@ -339,7 +340,9 @@ static int32_t NetIfRxImpl(const struct NetDevice *netDevice, NetBuf *buff, Rece
int32_t NetIfSetMacAddr(struct NetDevice *netDevice, const unsigned char *macAddr, unsigned char length)
{
HDF_STATUS ret;
struct NetDeviceImpl *ndImpl = NULL;
if (macAddr == NULL || length != MAC_ADDR_SIZE) {
HDF_LOGE("%s fail: input param error!", __func__);
return HDF_ERR_INVALID_PARAM;
@@ -348,6 +351,13 @@ int32_t NetIfSetMacAddr(struct NetDevice *netDevice, const unsigned char *macAdd
HDF_LOGE("%s fail : memcpy_s fail!", __func__);
return HDF_FAILURE;
}
if (netDevice->netDeviceIf != NULL && netDevice->netDeviceIf->setMacAddr != NULL) {
ret = netDevice->netDeviceIf->setMacAddr(netDevice, (void*)macAddr);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s fail : setMacAddr fail!", __func__);
return ret;
}
}
ndImpl = GetImplByNetDevice(netDevice);
if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->changeMacAddr != NULL) {
return ndImpl->interFace->changeMacAddr(ndImpl);
@@ -385,6 +395,16 @@ int32_t NetIfSetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus st
return HDF_ERR_INVALID_PARAM;
}
int32_t NetIfGetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus *status)
{
struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->getLinkStatus != NULL) {
return ndImpl->interFace->getLinkStatus(ndImpl, status);
}
HDF_LOGE("%s: netDevice not init or already free.", __func__);
return HDF_ERR_INVALID_PARAM;
}
int32_t NetIfDhcpsStart(const struct NetDevice *netDevice, char *ip, uint16_t ipNum)
{
struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
@@ -27,10 +27,11 @@ typedef enum {
struct NetDeviceImplOp {
int32_t (*init)(struct NetDeviceImpl *netDevice);
int32_t (*deInit)(struct NetDeviceImpl *netDevice);
int32_t (*add)(struct NetDeviceImpl *netDevice, Protocol80211IfType ifType);
int32_t (*add)(struct NetDeviceImpl *netDevice);
int32_t (*delete)(struct NetDeviceImpl *netDevice);
int32_t (*setStatus)(struct NetDeviceImpl *netDevice, NetIfStatus status);
int32_t (*setLinkStatus)(struct NetDeviceImpl *netDevice, NetIfLinkStatus status);
int32_t (*getLinkStatus)(struct NetDeviceImpl *netDevice, NetIfLinkStatus *status);
int32_t (*receive)(struct NetDeviceImpl *netDevice, NetBuf *buff, ReceiveFlag flag);
int32_t (*setIpAddr)(struct NetDeviceImpl *netDevice, const IpV4Addr *ipAddr, const IpV4Addr *netMask,
const IpV4Addr *gw);
+72
View File
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef ETH_DEVICE_H
#define ETH_DEVICE_H
#include "net_device.h"
#include "hdf_device_desc.h"
#include <lwip/netif.h>
#define MAX_NAME_LENGTH 16
#define ETH_DEVICE_MAX 6
#define DELAY_TIME_LONG 3000
#define DELAY_TIME_MEDIUM 50
#define DELAY_TIME_SHORT 5
#define SLEEP_TIME_SHORT 20
#define SLEEP_TIME_MEDIUM 30
#define SLEEP_TIME_COMMON 60
#define SLEEP_TIME_LONG 250
#define MAC_ADDR_OFFSET_L8 8
#define MAC_ADDR_OFFSET_L16 16
#define MAC_ADDR_OFFSET_L24 24
struct EthDevice {
struct NetDevice *netdev;
struct ConfigEthDevList *config;
const char *name;
void *priv;
};
struct HdfConfigEthMac {
uint32_t regBase;
uint32_t irqVector;
uint8_t mdioFrqDiv;
uint8_t txBusy;
uint32_t iobase;
uint32_t regOffSize;
};
struct HdfConfigEthPhy {
uint8_t phyMode;
};
struct ConfigEthDevList {
uint8_t deviceInstId;
uint8_t isSetDefault;
const char *driverName;
uint8_t hwXmitq;
uint8_t qSize;
uint8_t port;
struct HdfConfigEthMac ethMac;
struct HdfConfigEthPhy ethPhy;
};
struct EthConfig {
struct ConfigEthDevList deviceInst[ETH_DEVICE_MAX];
uint16_t deviceListSize;
};
struct EthDevice *CreateEthDevice(const struct ConfigEthDevList *configEthDevList);
int32_t ReleaseEthDevice(struct EthDevice *ethDevice);
int32_t GetEthIfName(const struct ConfigEthDevList *configEthDevList, char *ifName, uint32_t ifNameSize);
struct EthConfig *GetEthConfig(const struct DeviceResourceNode *node);
#endif /* ETH_DEVICE_H */
+66
View File
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "eth_chip_driver.h"
#include "hdf_log.h"
#define HDF_LOG_TAG HDF_ETH_CORE
static struct HdfEthChipDriverFactory *g_ethChipDriverFactory[MAX_CHIPDRIVER_COUNT] = {NULL};
struct HdfEthChipDriverFactory *HdfEthGetChipDriverByName(const char *driverName)
{
int32_t i;
if (driverName == NULL) {
HDF_LOGE("%s fail: drivreName is NULL", __func__);
return NULL;
}
for (i = 0; i < MAX_CHIPDRIVER_COUNT; i++) {
if (g_ethChipDriverFactory[i] != NULL && g_ethChipDriverFactory[i]->driverName != NULL) {
struct HdfEthChipDriverFactory *factory = g_ethChipDriverFactory[i];
if (strcmp(factory->driverName, driverName) == 0) {
return factory;
}
}
}
return NULL;
}
int32_t HdfEthRegChipDriver(struct HdfEthChipDriverFactory *obj)
{
int32_t index;
if (obj == NULL || obj->driverName == NULL) {
HDF_LOGE("%s: HdfEthChipDriverFactory obj is NULL", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (HdfEthGetChipDriverByName(obj->driverName) != NULL) {
HDF_LOGW("%s: chipDriver factory is already registed. name = %s", __func__, obj->driverName);
return HDF_FAILURE;
}
for (index = 0; index < MAX_CHIPDRIVER_COUNT; index++) {
if (g_ethChipDriverFactory[index] == NULL) {
g_ethChipDriverFactory[index] = obj;
HDF_LOGI("%s:Chip driver %s registed.", __func__, obj->driverName);
return HDF_SUCCESS;
}
}
HDF_LOGE("%s: Factory table is NULL", __func__);
return HDF_FAILURE;
}
static struct HdfEthChipDriverManager g_chipDriverManager = {
.chipFactoryInsts = g_ethChipDriverFactory,
.RegChipDriver = HdfEthRegChipDriver,
.GetEthChipDriverByName = HdfEthGetChipDriverByName,
};
struct HdfEthChipDriverManager *HdfEthGetChipDriverMgr(void)
{
return &g_chipDriverManager;
}
+291
View File
@@ -0,0 +1,291 @@
/*
* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "eth_device.h"
#include "eth_chip_driver.h"
#include <string.h>
#include "device_resource_if.h"
#include "hdf_log.h"
#include "los_vm_zone.h"
#include "osal_mem.h"
#define HDF_LOG_TAG eth_device
int32_t GetEthIfName(const struct ConfigEthDevList *configEthDevList, char *ifName, uint32_t ifNameSize)
{
if (configEthDevList == NULL || ifName == NULL || ifNameSize == 0) {
HDF_LOGE("%s:para is null!", __func__);
return HDF_FAILURE;
}
if (snprintf_s(ifName, ifNameSize, ifNameSize - 1, "eth%d", configEthDevList->deviceInstId) < 0) {
HDF_LOGE("%s:format ifName failed!deviceInstId = %d.", __func__, configEthDevList->deviceInstId);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
struct EthDevice *CreateEthDevice(const struct ConfigEthDevList *configEthDevList)
{
int32_t ret;
struct HdfEthNetDeviceData *data = NULL;
struct NetDevice *netDevice = NULL;
struct EthDevice *ethDevice = NULL;
char ethIfName[IFNAMSIZ] = {0};
if (configEthDevList == NULL) {
HDF_LOGE("%s input is NULL!", __func__);
return NULL;
}
data = (struct HdfEthNetDeviceData *)OsalMemCalloc(sizeof(struct HdfEthNetDeviceData));
if (data == NULL) {
HDF_LOGE("%s failed to OsalMemCalloc HdfEthNetDeviceData", __func__);
return NULL;
}
ret = GetEthIfName(configEthDevList, ethIfName, IFNAMSIZ);
if (ret != HDF_SUCCESS) {
OsalMemFree(data);
return NULL;
}
netDevice = NetDeviceInit(ethIfName, strlen(ethIfName), ETHERNET_LINK, LITE_OS);
if (netDevice == NULL) {
HDF_LOGE("%s failed to init netDevice", __func__);
OsalMemFree(data);
return NULL;
}
ethDevice = (struct EthDevice *)OsalMemCalloc(sizeof(struct EthDevice));
if (ethDevice == NULL) {
HDF_LOGE("%s failed to OsalMemCalloc ethDevice", __func__);
NetDeviceDeInit(netDevice);
OsalMemFree(data);
return NULL;
}
netDevice->mlPriv = ethDevice;
ethDevice->netdev = netDevice;
ethDevice->netdev->classDriverPriv = data;
ethDevice->name = configEthDevList->driverName;
return ethDevice;
}
static int32_t ParseEthMacConfig(const struct DeviceResourceNode *node, struct HdfConfigEthMac *ethMacConfig)
{
struct DeviceResourceIface *drsOps = NULL;
if (node == NULL || ethMacConfig == NULL) {
HDF_LOGE("%s: invalid node or ethMacConfig!", __func__);
return HDF_FAILURE;
}
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
if (drsOps == NULL || drsOps->GetUint8 == NULL || drsOps->GetUint32 == NULL) {
HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint32(node, "regBase", &ethMacConfig->regBase, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read regBase fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint32(node, "irqVector", &ethMacConfig->irqVector, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read irqVector fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "mdioFrqDiv", &ethMacConfig->mdioFrqDiv, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read mdioFrqDiv fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "txBusy", &ethMacConfig->txBusy, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read txBusy fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint32(node, "iobase", &ethMacConfig->iobase, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read iobase fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint32(node, "regOffSize", &ethMacConfig->regOffSize, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read regOffSize fail", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t ParseEthPhyConfig(const struct DeviceResourceNode *node, struct HdfConfigEthPhy *ethPhyConfig)
{
struct DeviceResourceIface *drsOps = NULL;
if (node == NULL || ethPhyConfig == NULL) {
HDF_LOGE("%s: invalid node or ethPhyConfig!", __func__);
return HDF_FAILURE;
}
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
if (drsOps == NULL || drsOps->GetUint8 == NULL) {
HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "phyMode", &ethPhyConfig->phyMode, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read phyMode fail", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t ParseEthDevInstConfig(const struct DeviceResourceNode *node, struct ConfigEthDevList *devLstConfig)
{
struct DeviceResourceIface *drsOps = NULL;
const struct DeviceResourceNode *ethMacNode = NULL;
const struct DeviceResourceNode *ethPhyNode = NULL;
if (node == NULL || devLstConfig == NULL) {
return HDF_FAILURE;
}
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
if (drsOps == NULL || drsOps->GetUint8 == NULL || drsOps->GetChildNode == NULL) {
HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "deviceInstId", &devLstConfig->deviceInstId, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: deviceInstId fail!", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "isSetDefault", &devLstConfig->isSetDefault, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: isSetDefault fail!", __func__);
return HDF_FAILURE;
}
if (drsOps->GetString(node, "driverName", &devLstConfig->driverName, NULL) != HDF_SUCCESS) {
HDF_LOGE("%s: driverName fail!", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "hwXmitq", &devLstConfig->hwXmitq, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read hwXmitq fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "qSize", &devLstConfig->qSize, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read qSize fail", __func__);
return HDF_FAILURE;
}
if (drsOps->GetUint8(node, "port", &devLstConfig->port, 0) != HDF_SUCCESS) {
HDF_LOGE("%s: read port fail", __func__);
return HDF_FAILURE;
}
ethMacNode = drsOps->GetChildNode(node, "MAC");
if (ethMacNode == NULL) {
HDF_LOGE("%s: GetChildNode fail!", __func__);
return HDF_FAILURE;
}
if (ParseEthMacConfig(ethMacNode, &devLstConfig->ethMac) != HDF_SUCCESS) {
return HDF_FAILURE;
}
ethPhyNode = drsOps->GetChildNode(node, "PHY");
if (ethPhyNode == NULL) {
HDF_LOGE("%s: GetChildNode fail!", __func__);
return HDF_FAILURE;
}
if (ParseEthPhyConfig(ethPhyNode, &devLstConfig->ethPhy) != HDF_SUCCESS) {
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t ParseEthDevListNode(const struct DeviceResourceNode *node, struct EthConfig *ethConfig)
{
struct DeviceResourceNode *childNode = NULL;
uint32_t index = 0;
if (node == NULL || ethConfig == NULL) {
HDF_LOGE("%s: invalid node or ethConfig!", __func__);
return HDF_FAILURE;
}
DEV_RES_NODE_FOR_EACH_CHILD_NODE(node, childNode)
{
if (ParseEthDevInstConfig(childNode, &ethConfig->deviceInst[index]) != HDF_SUCCESS) {
return HDF_FAILURE;
}
index++;
ethConfig->deviceListSize++;
}
HDF_LOGD("%s: deviceListSize=%d", __func__, ethConfig->deviceListSize);
return HDF_SUCCESS;
}
static int32_t ParseConfigFromProperty(const struct DeviceResourceNode *node, struct EthConfig *config)
{
struct DeviceResourceIface *drsOps = NULL;
const struct DeviceResourceNode *devListNode = NULL;
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
if (drsOps == NULL || drsOps->GetChildNode == NULL) {
HDF_LOGE("%s: invalid drs ops fail!", __func__);
return HDF_FAILURE;
}
devListNode = drsOps->GetChildNode(node, "ethList");
if (devListNode == NULL) {
HDF_LOGE("%s: get child node fail!", __func__);
return HDF_FAILURE;
}
return ParseEthDevListNode(devListNode, config);
}
struct EthConfig *GetEthConfig(const struct DeviceResourceNode *node)
{
if (node == NULL) {
HDF_LOGE("%s input is NULL", __func__);
return NULL;
}
struct EthConfig *config = NULL;
config = (struct EthConfig *)OsalMemCalloc(sizeof(*config));
if (config == NULL) {
HDF_LOGE("%s failed to OsalMemCalloc config", __func__);
return NULL;
}
if (ParseConfigFromProperty(node, config) != HDF_SUCCESS) {
HDF_LOGE("%s failed to parse config from property", __func__);
return NULL;
}
return config;
}
struct HdfEthNetDeviceData *GetEthNetDeviceData(const struct NetDevice *netDev)
{
if (netDev == NULL) {
return NULL;
}
return (struct HdfEthNetDeviceData *)(netDev->classDriverPriv);
}
int32_t ReleaseEthDevice(struct EthDevice *ethDevice)
{
int32_t ret;
struct HdfEthNetDeviceData *data = NULL;
if (ethDevice == NULL) {
HDF_LOGE("%s:NULL ptr!", __func__);
return HDF_FAILURE;
}
data = GetEthNetDeviceData(ethDevice->netdev);
if (data == NULL) {
HDF_LOGE("%s: GetEthNetDeviceData failed!", __func__);
return HDF_FAILURE;
}
struct HdfEthMacChipDriver *macChipDriver = data->macChipDriver;
if (macChipDriver != NULL) {
OsalMemFree(macChipDriver);
macChipDriver = NULL;
}
ret = NetDeviceDeInit(ethDevice->netdev);
if (ret != HDF_SUCCESS) {
return ret;
}
OsalMemFree(ethDevice);
OsalMemFree(data);
return HDF_SUCCESS;
}
+215
View File
@@ -0,0 +1,215 @@
/*
* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "eth_chip_driver.h"
#include "eth_device.h"
#include "hdf_log.h"
#include "osal_mem.h"
#define HDF_LOG_TAG eth_core
struct EthConfig *g_ethConfig = NULL;
static struct HdfEthChipDriverFactory *HdfEthGetDriverFactory(const char *driverName)
{
struct HdfEthChipDriverManager *initMgr = NULL;
if (driverName == NULL) {
HDF_LOGE("%s: driverName is ", __func__);
return NULL;
}
initMgr = HdfEthGetChipDriverMgr();
if (initMgr == NULL) {
HDF_LOGE("%s: initMgr is NULL", __func__);
return NULL;
}
return initMgr->GetEthChipDriverByName(driverName);
}
static int32_t DeinitEth(struct EthDevice *ethDevice)
{
HDF_LOGD("%s enter", __func__);
struct HdfEthChipDriverFactory *ethChipDriverFact = NULL;
if (ethDevice == NULL) {
HDF_LOGE("%s the input ethDevice is NULL!", __func__);
return HDF_ERR_INVALID_PARAM;
}
ethChipDriverFact = HdfEthGetDriverFactory(ethDevice->name);
if (ethChipDriverFact == NULL) {
HDF_LOGE("%s: get ethChipDriverFact failed! driverName = %s", __func__, ethDevice->name);
return HDF_FAILURE;
}
if (ethChipDriverFact->DeinitEthDriver != NULL) {
return ethChipDriverFact->DeinitEthDriver(ethDevice);
}
return HDF_SUCCESS;
}
#if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
static int32_t SetEthNetworkAddr(struct NetDevice *netDev)
{
IpV4Addr ip, netmask, gw;
ip.addr = 0x0A01a8c0UL; /* 192, 168, 1. 10 */
netmask.addr = 0x00ffffffUL; /* 255, 255, 255, 0 */
gw.addr = 0x0101a8c0UL; /* 192, 168, 12, 1 */
if (NetIfSetAddr(netDev, &ip, &netmask, &gw) != HDF_SUCCESS) {
HDF_LOGE("%s fail: NetIfSetAddr error!", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
#endif
static int32_t InitMacInterface(struct EthDevice *ethDevice, struct HdfEthMacChipDriver *macChipDriver)
{
int32_t ret;
if (macChipDriver->ethMacOps == NULL) {
HDF_LOGE("%s:ethMacOps is NULL.", __func__);
return HDF_FAILURE;
}
if (macChipDriver->ethMacOps->MacInit == NULL) {
HDF_LOGE("%s:MacInit is not implement.", __func__);
return HDF_FAILURE;
}
macChipDriver->ethMacOps->MacInit();
if (macChipDriver->ethMacOps->PortReset == NULL) {
HDF_LOGE("%s:PortReset is not implement.", __func__);
return HDF_FAILURE;
}
ret = macChipDriver->ethMacOps->PortReset(ethDevice);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s:PortReset failed! ret = %d.", __func__, ret);
return HDF_FAILURE;
}
if (macChipDriver->ethMacOps->PortInit == NULL) {
HDF_LOGE("%s:PortInit is not implement.", __func__);
return HDF_FAILURE;
}
ret = macChipDriver->ethMacOps->PortInit(ethDevice);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s:PortInit failed! ret = %d.", __func__, ret);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t InitEth(struct EthDevice *ethDevice, const uint8_t isSetDefault,
struct HdfEthChipDriverFactory *ethChipDriverFact)
{
int32_t ret;
struct HdfEthNetDeviceData *data = NULL;
struct HdfEthMacChipDriver *macChipDriver = NULL;
unsigned char enaddr[MAC_ADDR_SIZE] = {0};
macChipDriver = ethChipDriverFact->BuildMacDriver();
if (macChipDriver == NULL) {
HDF_LOGE("%s:mac chip driver build fail!", __func__);
return HDF_FAILURE;
}
data = GetEthNetDeviceData(ethDevice->netdev);
if (data == NULL) {
HDF_LOGE("%s: data is NULL!", __func__);
ethChipDriverFact->ReleaseMacDriver(macChipDriver);
return HDF_FAILURE;
}
data->macChipDriver = macChipDriver;
ret = ethChipDriverFact->InitEthDriver(ethDevice);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: failed to init eth phy", __func__);
DeinitEth(ethDevice);
ReleaseEthDevice(ethDevice);
return ret;
}
ret = InitMacInterface(ethDevice, macChipDriver);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: InitMacInterface error!", __func__);
DeinitEth(ethDevice);
ReleaseEthDevice(ethDevice);
return HDF_FAILURE;
}
LOS_Msleep(DELAY_TIME_LONG);
ethChipDriverFact->GetMacAddr(enaddr, MAC_ADDR_SIZE);
NetIfSetMacAddr(ethDevice->netdev, enaddr, MAC_ADDR_SIZE);
#if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
ret = SetEthNetworkAddr(ethDevice->netdev);
if (ret != HDF_SUCCESS) {
PRINTK("%s set eth network addr fail\n", __func__);
DeinitEth(ethDevice);
ReleaseEthDevice(ethDevice);
return ret;
}
#endif
NetIfSetStatus(ethDevice->netdev, NETIF_UP);
return HDF_SUCCESS;
}
static int32_t HdfEthDriverInit(struct HdfDeviceObject *deviceObject)
{
int32_t ret;
uint8_t i;
if (deviceObject == NULL) {
HDF_LOGE("%s deviceObject is NULL", __func__);
return HDF_ERR_INVALID_PARAM;
}
g_ethConfig = GetEthConfig(deviceObject->property);
if (g_ethConfig == NULL) {
HDF_LOGE("%s failed to get g_ethConfig!", __func__);
return HDF_FAILURE;
}
for (i = 0; i < g_ethConfig->deviceListSize; i++) {
struct EthDevice *ethDevice = CreateEthDevice(&g_ethConfig->deviceInst[i]);
if (ethDevice == NULL) {
return HDF_FAILURE;
}
ethDevice->config = &g_ethConfig->deviceInst[i];
struct HdfEthChipDriverFactory *ethChipDriverFact = HdfEthGetDriverFactory(ethDevice->name);
if (ethChipDriverFact == NULL) {
HDF_LOGE("%s: get ethChipDriverFact failed! driverName = %s", __func__, ethDevice->name);
return HDF_FAILURE;
}
ret = InitEth(ethDevice, g_ethConfig->deviceInst[i].isSetDefault, ethChipDriverFact);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s failed to init eth driver, ret: %d", __func__, ret);
return ret;
}
}
PRINTK("%s hdf eth driver framework init success\n", __func__);
return ret;
}
static int32_t HdfEthDriverBind(struct HdfDeviceObject *deviceObject)
{
(void)deviceObject;
return HDF_SUCCESS;
}
static void HdfEthDriverRelease(struct HdfDeviceObject *deviceObject)
{
(void)deviceObject;
}
struct HdfDriverEntry g_ethEntry = {
.moduleVersion = 1,
.Bind = HdfEthDriverBind,
.Init = HdfEthDriverInit,
.Release = HdfEthDriverRelease,
.moduleName = "HDF_ETHERNET"
};
HDF_INIT(g_ethEntry);
@@ -96,9 +96,9 @@ int32_t RenewNetDevice(NetDevice **netDev)
}
*netDev = NULL;
#ifdef _PRE_HDF_LINUX
result = NetDeviceInit(ifName, strlen(ifName), FULL_OS);
result = NetDeviceInit(ifName, strlen(ifName), WIFI_LINK, FULL_OS);
#else
result = NetDeviceInit(ifName, strlen(ifName), LITE_OS);
result = NetDeviceInit(ifName, strlen(ifName), WIFI_LINK, LITE_OS);
#endif
if (result == NULL) {
HDF_LOGE("%s:alloc NetDevice return NULL!", __func__);
@@ -163,9 +163,9 @@ struct NetDevice *AllocPlatformNetDevice(struct HdfWlanDevice *device)
break;
}
#ifdef _PRE_HDF_LINUX
result = NetDeviceInit(ifName, strlen(ifName), FULL_OS);
result = NetDeviceInit(ifName, strlen(ifName), WIFI_LINK, FULL_OS);
#else
result = NetDeviceInit(ifName, strlen(ifName), LITE_OS);
result = NetDeviceInit(ifName, strlen(ifName), WIFI_LINK, LITE_OS);
#endif
} while (false);
if (result == NULL) {
@@ -25,11 +25,12 @@ static bool WiFiNetDeviceTestEnv(void)
HDF_LOGE("%s: strcpy_s fail", __func__);
return false;
}
g_netDevice = NetDeviceInit(devName, strlen(devName), LITE_OS);
g_netDevice = NetDeviceInit(devName, strlen(devName), WIFI_LINK, LITE_OS);
if (g_netDevice == NULL) {
HDF_LOGE("%s fail ", __func__);
return false;
}
g_netDevice->funType.wlanType = PROTOCOL_80211_IFTYPE_STATION;
HDF_LOGE("%s success ", __func__);
return true;
}
@@ -62,7 +63,7 @@ int32_t WiFiNetDviceTestAdd(void)
return HDF_FAILURE;
}
}
if (NetDeviceAdd(g_netDevice, PROTOCOL_80211_IFTYPE_STATION) != HDF_SUCCESS) {
if (NetDeviceAdd(g_netDevice) != HDF_SUCCESS) {
HDF_LOGE("%s add fail!", __func__);
return HDF_FAILURE;
}