1f9f848faSopenharmony_ci/*
2f9f848faSopenharmony_ci * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
3f9f848faSopenharmony_ci * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
4f9f848faSopenharmony_ci *
5f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
6f9f848faSopenharmony_ci * are permitted provided that the following conditions are met:
7f9f848faSopenharmony_ci *
8f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
9f9f848faSopenharmony_ci *    conditions and the following disclaimer.
10f9f848faSopenharmony_ci *
11f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12f9f848faSopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
13f9f848faSopenharmony_ci *    provided with the distribution.
14f9f848faSopenharmony_ci *
15f9f848faSopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16f9f848faSopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
17f9f848faSopenharmony_ci *    permission.
18f9f848faSopenharmony_ci *
19f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20f9f848faSopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21f9f848faSopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f9f848faSopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23f9f848faSopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24f9f848faSopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25f9f848faSopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26f9f848faSopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27f9f848faSopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28f9f848faSopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29f9f848faSopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30f9f848faSopenharmony_ci */
31f9f848faSopenharmony_ci
32f9f848faSopenharmony_ci#include <lwip/err.h>
33f9f848faSopenharmony_ci#include <lwip/pbuf.h>
34f9f848faSopenharmony_ci#include <lwip/dhcp.h>
35f9f848faSopenharmony_ci#include <lwip/netifapi.h>
36f9f848faSopenharmony_ci
37f9f848faSopenharmony_ci#include "usb_eth_drv.h"
38f9f848faSopenharmony_ci#include "securec.h"
39f9f848faSopenharmony_ci#include "los_printf.h"
40f9f848faSopenharmony_ci
41f9f848faSopenharmony_ci
42f9f848faSopenharmony_cistatic void eth_drv_send(struct netif *usb_netif, struct pbuf *p);
43f9f848faSopenharmony_ci
44f9f848faSopenharmony_ci#if PF_PKT_SUPPORT
45f9f848faSopenharmony_cistatic void
46f9f848faSopenharmony_cieth_drv_config(struct netif *ni, u32_t cflags, u8_t setBit)
47f9f848faSopenharmony_ci{
48f9f848faSopenharmony_ci	LWIP_UNUSED_ARG(ni);
49f9f848faSopenharmony_ci	LWIP_UNUSED_ARG(cflags);
50f9f848faSopenharmony_ci	LWIP_UNUSED_ARG(setBit);
51f9f848faSopenharmony_ci	/* Nothing to be done for now */
52f9f848faSopenharmony_ci}
53f9f848faSopenharmony_ci#endif
54f9f848faSopenharmony_ci
55f9f848faSopenharmony_ci/*
56f9f848faSopenharmony_ci * This function is called during system initialization to register a
57f9f848faSopenharmony_ci * network interface with the system.
58f9f848faSopenharmony_ci */
59f9f848faSopenharmony_ci
60f9f848faSopenharmony_cistatic void
61f9f848faSopenharmony_cieth_drv_init(struct los_eth_driver *sc, unsigned char *enaddr)
62f9f848faSopenharmony_ci{
63f9f848faSopenharmony_ci	struct netif *usb_netif = &sc->ac_if;
64f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc*)sc->driver_context;
65f9f848faSopenharmony_ci	ip4_addr_t ipaddr, netmask, gw;
66f9f848faSopenharmony_ci	err_t ret;
67f9f848faSopenharmony_ci
68f9f848faSopenharmony_ci	IP4_ADDR(&gw, 192, 168, 0, 1);
69f9f848faSopenharmony_ci	IP4_ADDR(&ipaddr, 192, 168, 0, 100);
70f9f848faSopenharmony_ci	IP4_ADDR(&netmask, 255, 255, 255, 0);
71f9f848faSopenharmony_ci
72f9f848faSopenharmony_ci	usb_netif->state = sc;
73f9f848faSopenharmony_ci	usb_netif->drv_send = eth_drv_send;
74f9f848faSopenharmony_ci	usb_netif->drv_set_hwaddr = NULL;
75f9f848faSopenharmony_ci	usb_netif->link_layer_type = ETHERNET_DRIVER_IF;
76f9f848faSopenharmony_ci	usb_netif->hwaddr_len = NETIF_MAX_HWADDR_LEN;
77f9f848faSopenharmony_ci#if PF_PKT_SUPPORT
78f9f848faSopenharmony_ci	usb_netif->drv_config = eth_drv_config;
79f9f848faSopenharmony_ci#endif
80f9f848faSopenharmony_ci#if LWIP_NETIF_ETHTOOL
81f9f848faSopenharmony_ci	usb_netif->ethtool_ops = NULL;
82f9f848faSopenharmony_ci#endif
83f9f848faSopenharmony_ci	if (enaddr != 0) {
84f9f848faSopenharmony_ci		(void)memcpy_s(usb_netif->hwaddr, NETIF_MAX_HWADDR_LEN, enaddr, NETIF_MAX_HWADDR_LEN);
85f9f848faSopenharmony_ci		(drv_sc->funs->start)(sc, NULL, 0);
86f9f848faSopenharmony_ci	}
87f9f848faSopenharmony_ci	ret = netifapi_netif_add(usb_netif, &ipaddr, &netmask, &gw);
88f9f848faSopenharmony_ci	if (ret) {
89f9f848faSopenharmony_ci		PRINT_ERR("Fatal Error, unable to add USB network interface! ret:%d\n", ret);
90f9f848faSopenharmony_ci		return;
91f9f848faSopenharmony_ci	}
92f9f848faSopenharmony_ci
93f9f848faSopenharmony_ci	ret = netifapi_netif_set_default(usb_netif);
94f9f848faSopenharmony_ci	if (ret) {
95f9f848faSopenharmony_ci		PRINT_ERR("%s, %d, netif set default failed, ret:%d\n", __FUNCTION__, __LINE__, ret);
96f9f848faSopenharmony_ci		return;
97f9f848faSopenharmony_ci	}
98f9f848faSopenharmony_ci
99f9f848faSopenharmony_ci	ret = netifapi_netif_set_up(usb_netif);
100f9f848faSopenharmony_ci	if (ret) {
101f9f848faSopenharmony_ci		PRINT_ERR("%s, %d, netif set up failed, ret:%d\n", __FUNCTION__, __LINE__, ret);
102f9f848faSopenharmony_ci		return;
103f9f848faSopenharmony_ci	}
104f9f848faSopenharmony_ci
105f9f848faSopenharmony_ci	ret = netifapi_dhcp_start(usb_netif);
106f9f848faSopenharmony_ci	if (ret) {
107f9f848faSopenharmony_ci		PRINT_ERR("%s, %d, dhcp start failed, ret:%d\n", __FUNCTION__, __LINE__, ret);
108f9f848faSopenharmony_ci		return;
109f9f848faSopenharmony_ci	}
110f9f848faSopenharmony_ci}
111f9f848faSopenharmony_ci
112f9f848faSopenharmony_ci/*
113f9f848faSopenharmony_ci * This function is called from the hardware driver when an output operation
114f9f848faSopenharmony_ci * has completed - i.e. the packet has been sent.
115f9f848faSopenharmony_ci */
116f9f848faSopenharmony_cistatic void
117f9f848faSopenharmony_cieth_drv_tx_done(struct eth_drv_sc *sc, unsigned int key, int status)
118f9f848faSopenharmony_ci{
119f9f848faSopenharmony_ci
120f9f848faSopenharmony_ci}
121f9f848faSopenharmony_ci
122f9f848faSopenharmony_ci/*
123f9f848faSopenharmony_ci * This function is called from a hardware driver to indicate that an input
124f9f848faSopenharmony_ci * packet has arrived.  The routine will set up appropriate network resources
125f9f848faSopenharmony_ci * to hold the data and call back into the driver to retrieve the data.
126f9f848faSopenharmony_ci */
127f9f848faSopenharmony_cistatic void
128f9f848faSopenharmony_cieth_drv_recv(struct los_eth_driver *sc, int total_len)
129f9f848faSopenharmony_ci{
130f9f848faSopenharmony_ci	struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
131f9f848faSopenharmony_ci	struct netif *usb_netif = &sc->ac_if;
132f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc*)sc->driver_context;
133f9f848faSopenharmony_ci	struct pbuf *p, *q;
134f9f848faSopenharmony_ci	int sg_len = 0;
135f9f848faSopenharmony_ci
136f9f848faSopenharmony_ci	if ((total_len > MAX_ETH_MSG) || (total_len < 0)) {
137f9f848faSopenharmony_ci		total_len = MAX_ETH_MSG;
138f9f848faSopenharmony_ci	}
139f9f848faSopenharmony_ci
140f9f848faSopenharmony_ci	p = pbuf_alloc(PBUF_RAW, (total_len + ETH_PAD_SIZE), PBUF_RAM);
141f9f848faSopenharmony_ci	if (p == NULL) {
142f9f848faSopenharmony_ci		PRINTK("eth_drv_recv : pbuf_alloc failed\n");
143f9f848faSopenharmony_ci		return;
144f9f848faSopenharmony_ci	}
145f9f848faSopenharmony_ci
146f9f848faSopenharmony_ci#if ETH_PAD_SIZE
147f9f848faSopenharmony_ci	(void)pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
148f9f848faSopenharmony_ci#endif
149f9f848faSopenharmony_ci
150f9f848faSopenharmony_ci	for (q = p; q != NULL; q = q->next) {
151f9f848faSopenharmony_ci		sg_list[sg_len].buf = (UINTPTR)q->payload;
152f9f848faSopenharmony_ci		sg_list[sg_len++].len = q->len;
153f9f848faSopenharmony_ci	}
154f9f848faSopenharmony_ci
155f9f848faSopenharmony_ci	(drv_sc->funs->recv)(sc, sg_list, sg_len);
156f9f848faSopenharmony_ci
157f9f848faSopenharmony_ci#if ETH_PAD_SIZE
158f9f848faSopenharmony_ci	(void)pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
159f9f848faSopenharmony_ci#endif
160f9f848faSopenharmony_ci
161f9f848faSopenharmony_ci	driverif_input(usb_netif,p);
162f9f848faSopenharmony_ci}
163f9f848faSopenharmony_cistatic void
164f9f848faSopenharmony_cieth_drv_send(struct netif *usb_netif, struct pbuf *p)
165f9f848faSopenharmony_ci{
166f9f848faSopenharmony_ci	struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
167f9f848faSopenharmony_ci	struct los_eth_driver *sc = (struct los_eth_driver *)usb_netif->state;
168f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc*)sc->driver_context;
169f9f848faSopenharmony_ci	int sg_len = 0;
170f9f848faSopenharmony_ci	struct pbuf *q;
171f9f848faSopenharmony_ci
172f9f848faSopenharmony_ci	while (!(drv_sc->funs->can_send)(sc));
173f9f848faSopenharmony_ci
174f9f848faSopenharmony_ci	for (q = p; q != NULL; q = q->next) {
175f9f848faSopenharmony_ci		sg_list[sg_len].buf = (UINTPTR)q->payload;
176f9f848faSopenharmony_ci		sg_list[sg_len++].len = q->len;
177f9f848faSopenharmony_ci	}
178f9f848faSopenharmony_ci
179f9f848faSopenharmony_ci	(drv_sc->funs->send) (sc, sg_list, sg_len, p ? p->tot_len : 0, (UINTPTR)p);
180f9f848faSopenharmony_ci}
181f9f848faSopenharmony_ci
182f9f848faSopenharmony_ciconst struct los_eth_funs eth_drv_funs_usb = {
183f9f848faSopenharmony_ci	(void (*)(struct los_eth_driver *, unsigned char *))eth_drv_init,
184f9f848faSopenharmony_ci	(void (*)(struct los_eth_driver *, int))eth_drv_recv,
185f9f848faSopenharmony_ci	(void (*)(struct los_eth_driver *, unsigned int, int))eth_drv_tx_done
186f9f848faSopenharmony_ci};
187f9f848faSopenharmony_ci
188