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