1 /*
2 * Copyright (C) 2022 Unionman Technology Co., Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <net/cfg80211.h>
20 #include <linux/netdevice.h>
21 #include "net_device.h"
22 #include "securec.h"
23 #include "hdf_base.h"
24 #include "hdf_wlan_utils.h"
25 #include "net_adapter.h"
26
27 #define OSAL_ERR_CODE_PTR_NULL 100
28
29 #ifdef _PRE_WLAN_FEATURE_MESH
30 #include "dmac_ext_if.h"
31 #include "hmac_vap.h"
32 #include "hmac_user.h"
33 #endif
34
35 #ifdef _PRE_WLAN_FEATURE_P2P
36 #include "wal_linux_cfg80211.h"
37 #endif
38 #include "eapol.h"
39
40 #ifdef __cplusplus
41 #if __cplusplus
42 extern "C" {
43 #endif
44 #endif
45
46 #define HDF_LOG_TAG RTL8822CS
47
48 #define MAC_NET_DEVICE_NAME_LENGTH (16)
49
50 #define WIFI_SHIFT_BIT (8)
51 #define WIFI_IFNAME_MAX_SIZE (16)
52
53 struct NetDevice *g_hdf_netdev = NULL;
54 struct net_device *g_linux_netdev = NULL;
55 unsigned char g_efuseMacExist = 0;
56
set_krn_netdev(struct net_device * netdev)57 void set_krn_netdev(struct net_device *netdev)
58 {
59 g_linux_netdev = (struct net_device *)netdev;
60 }
61
set_efuse_mac_exist(unsigned char exist)62 void set_efuse_mac_exist(unsigned char exist)
63 {
64 g_efuseMacExist = exist;
65 }
66
get_efuse_mac_exist(void)67 unsigned char get_efuse_mac_exist(void)
68 {
69 return g_efuseMacExist;
70 }
71
hdf_rtl8822cs_netdev_specialethertypeprocess(const struct NetDevice * netDev,NetBuf * buff)72 ProcessingResult hdf_rtl8822cs_netdev_specialethertypeprocess(const struct NetDevice *netDev, NetBuf *buff)
73 {
74 struct EtherHeader *header = NULL;
75 const struct Eapol *eapolInstance = NULL;
76 int ret = HDF_SUCCESS;
77 uint16_t protocol;
78 uint16_t etherType;
79 const int shift = 8;
80 const int pidx0 = 12, pidx1 = 13;
81
82 if (netDev == NULL || buff == NULL) {
83 return PROCESSING_ERROR;
84 }
85
86 header = (struct EtherHeader *)NetBufGetAddress(buff, E_DATA_BUF);
87 etherType = ntohs(header->etherType);
88 protocol = (buff->data[pidx0] << shift) | buff->data[pidx1];
89
90 if (protocol != ETHER_TYPE_PAE) {
91 return PROCESSING_CONTINUE;
92 }
93 if (netDev->specialProcPriv == NULL) {
94 HDF_LOGE("%s: return PROCESSING_ERROR", __func__);
95 return PROCESSING_ERROR;
96 }
97
98 eapolInstance = EapolGetInstance();
99 ret = eapolInstance->eapolOp->writeEapolToQueue(netDev, buff);
100 if (ret != HDF_SUCCESS) {
101 HDF_LOGE("%s: writeEapolToQueue failed", __func__);
102 NetBufFree(buff);
103 }
104
105 return PROCESSING_COMPLETE;
106 }
107
hdf_rtl8822cs_netdev_linkstatuschanged(struct NetDevice * netDev)108 void hdf_rtl8822cs_netdev_linkstatuschanged(struct NetDevice *netDev)
109 {
110 HDF_LOGE("%s: start...", __func__);
111 (void)netDev;
112 }
113
hdf_rtl8822cs_netdev_changemtu(struct NetDevice * netDev,int32_t mtu)114 int32_t hdf_rtl8822cs_netdev_changemtu(struct NetDevice *netDev, int32_t mtu)
115 {
116 int32_t retVal = 0;
117 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
118 HDF_LOGE("%s: start...", __func__);
119
120 if (netdev == NULL) {
121 HDF_LOGE("%s: netdev null!", __func__);
122 return HDF_FAILURE;
123 }
124 HDF_LOGE("%s: change mtu to %d\n", __FUNCTION__, mtu);
125
126 return retVal;
127 }
128
hdf_rtl8822cs_netdev_netifnotify(struct NetDevice * netDev,NetDevNotify * notify)129 uint32_t hdf_rtl8822cs_netdev_netifnotify(struct NetDevice *netDev, NetDevNotify *notify)
130 {
131 HDF_LOGE("%s: start...", __func__);
132 (void)netDev;
133 (void)notify;
134 return HDF_SUCCESS;
135 }
136
hdf_rtl8822cs_netdev_setnetifstats(struct NetDevice * netDev,NetIfStatus status)137 void hdf_rtl8822cs_netdev_setnetifstats(struct NetDevice *netDev, NetIfStatus status)
138 {
139 HDF_LOGE("%s: start...", __func__);
140 (void)netDev;
141 (void)status;
142 }
143
hdf_rtl8822cs_netdev_selectqueue(struct NetDevice * netDev,NetBuf * netBuff)144 uint16_t hdf_rtl8822cs_netdev_selectqueue(struct NetDevice *netDev, NetBuf *netBuff)
145 {
146 HDF_LOGE("%s: start...", __func__);
147 (void)netDev;
148 (void)netBuff;
149 return HDF_SUCCESS;
150 }
151
hdf_rtl8822cs_netdev_getstats(struct NetDevice * netDev)152 struct NetDevStats *hdf_rtl8822cs_netdev_getstats(struct NetDevice *netDev)
153 {
154 static struct NetDevStats devStat = {0};
155 struct net_device_stats *kdevStat = NULL;
156 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
157
158 HDF_LOGE("%s: start...", __func__);
159
160 if (netdev == NULL) {
161 HDF_LOGE("%s: netDev null!", __func__);
162 return NULL;
163 }
164
165 kdevStat = rtw_net_get_stats(netdev);
166 if (kdevStat == NULL) {
167 HDF_LOGE("%s: ndo_get_stats return null!", __func__);
168 return NULL;
169 }
170
171 devStat.rxPackets = kdevStat->rx_packets;
172 devStat.txPackets = kdevStat->tx_packets;
173 devStat.rxBytes = kdevStat->rx_bytes;
174 devStat.txBytes = kdevStat->tx_bytes;
175 devStat.rxErrors = kdevStat->rx_errors;
176 devStat.txErrors = kdevStat->tx_errors;
177 devStat.rxDropped = kdevStat->rx_dropped;
178 devStat.txDropped = kdevStat->tx_dropped;
179
180 return &devStat;
181 }
182
hdf_rtl8822cs_netdev_setmacaddr(struct NetDevice * netDev,void * addr)183 int32_t hdf_rtl8822cs_netdev_setmacaddr(struct NetDevice *netDev, void *addr)
184 {
185 int32_t retVal = 0;
186 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
187
188 HDF_LOGE("%s: start...", __func__);
189
190 if (netdev == NULL || addr == NULL) {
191 HDF_LOGE("%s: netDev or addr null!", __func__);
192 return HDF_FAILURE;
193 }
194
195 retVal = (int32_t)rtw_cfg80211_monitor_if_set_mac_address(netdev, addr);
196 if (retVal < 0) {
197 HDF_LOGE("%s: hdf net device setmacaddr failed! ret = %d", __func__, retVal);
198 }
199
200 return retVal;
201 }
202
hdf_rtl8822cs_netdev_ioctl(struct NetDevice * netDev,IfReq * req,int32_t cmd)203 int32_t hdf_rtl8822cs_netdev_ioctl(struct NetDevice *netDev, IfReq *req, int32_t cmd)
204 {
205 int32_t retVal = 0;
206 struct ifreq dhd_req = {0};
207 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
208
209 HDF_LOGE("%s: start...", __func__);
210
211 if (netdev == NULL || req == NULL) {
212 HDF_LOGE("%s: netdev or req null!", __func__);
213 return HDF_FAILURE;
214 }
215
216 dhd_req.ifr_ifru.ifru_data = req->ifrData;
217
218 retVal = (int32_t)rtw_ioctl(netdev, &dhd_req, cmd);
219 if (retVal < 0) {
220 HDF_LOGE("%s: hdf net device ioctl failed! ret = %d", __func__, retVal);
221 }
222
223 return retVal;
224 }
225
hdf_rtl8822cs_netdev_xmit(struct NetDevice * netDev,NetBuf * netBuff)226 int32_t hdf_rtl8822cs_netdev_xmit(struct NetDevice *netDev, NetBuf *netBuff)
227 {
228 int32_t retVal = 0;
229 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
230
231 if (netdev == NULL || netBuff == NULL) {
232 HDF_LOGE("%s: netdev or netBuff null!", __func__);
233 return HDF_FAILURE;
234 }
235
236 retVal = (int32_t)rtw_xmit_entry((struct sk_buff *)netBuff, netdev);
237 if (retVal < 0) {
238 HDF_LOGE("%s: hdf net device xmit failed! ret = %d", __func__, retVal);
239 }
240
241 return retVal;
242 }
243
hdf_rtl8822cs_netdev_stop(struct NetDevice * netDev)244 int32_t hdf_rtl8822cs_netdev_stop(struct NetDevice *netDev)
245 {
246 int32_t retVal = 0;
247 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
248
249 HDF_LOGE("%s: start...", __func__);
250
251 if (netdev == NULL) {
252 HDF_LOGE("%s: netDev null!", __func__);
253 return HDF_FAILURE;
254 }
255
256 retVal = (int32_t)netdev_close(netdev);
257 if (retVal < 0) {
258 HDF_LOGE("%s: hdf net device stop failed! ret = %d", __func__, retVal);
259 }
260
261 return retVal;
262 }
263
hdf_rtl8822cs_netdev_open(struct NetDevice * netDev)264 int32_t hdf_rtl8822cs_netdev_open(struct NetDevice *netDev)
265 {
266 int32_t retVal = 0;
267 const int idx0 = 0, idx1 = 1, idx2 = 2, idx3 = 3, idx4 = 4, idx5 = 5;
268 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
269
270 if (netdev == NULL) {
271 HDF_LOGE("%s: netDev null!", __func__);
272 return HDF_FAILURE;
273 }
274
275 HDF_LOGE("%s: ndo_open...", __func__);
276
277 retVal = (int32_t)netdev_open(netdev);
278 if (retVal < 0) {
279 HDF_LOGE("%s: netdev_open fail!", __func__);
280 }
281
282 netDev->ieee80211Ptr = netdev->ieee80211_ptr;
283 if (netDev->ieee80211Ptr == NULL) {
284 HDF_LOGE("%s: NULL == netDev->ieee80211Ptr", __func__);
285 }
286
287 // update mac addr to NetDevice object
288 memcpy_s(netDev->macAddr, MAC_ADDR_SIZE, netdev->dev_addr, netdev->addr_len);
289 HDF_LOGE("%s: %02x:%02x:%02x:%02x:%02x:%02x", __func__,
290 netDev->macAddr[idx0], netDev->macAddr[idx1], netDev->macAddr[idx2],
291 netDev->macAddr[idx3], netDev->macAddr[idx4], netDev->macAddr[idx5]);
292
293 return retVal;
294 }
295
hdf_rtl8822cs_netdev_deinit(struct NetDevice * netDev)296 void hdf_rtl8822cs_netdev_deinit(struct NetDevice *netDev)
297 {
298 HDF_LOGE("%s: start...", __func__);
299 (void)netDev;
300 }
301
302 int32_t hdf_rtl8822cs_netdev_init(struct NetDevice *netDev);
303
304 struct NetDeviceInterFace g_wal_rtl_net_dev_ops = {
305 .init = hdf_rtl8822cs_netdev_init,
306 .deInit = hdf_rtl8822cs_netdev_deinit,
307 .open = hdf_rtl8822cs_netdev_open,
308 .stop = hdf_rtl8822cs_netdev_stop,
309 .xmit = hdf_rtl8822cs_netdev_xmit,
310 .ioctl = hdf_rtl8822cs_netdev_ioctl,
311 .setMacAddr = hdf_rtl8822cs_netdev_setmacaddr,
312 .getStats = hdf_rtl8822cs_netdev_getstats,
313 .setNetIfStatus = hdf_rtl8822cs_netdev_setnetifstats,
314 .selectQueue = hdf_rtl8822cs_netdev_selectqueue,
315 .netifNotify = hdf_rtl8822cs_netdev_netifnotify,
316 .changeMtu = hdf_rtl8822cs_netdev_changemtu,
317 .linkStatusChanged = hdf_rtl8822cs_netdev_linkstatuschanged,
318 .specialEtherTypeProcess = hdf_rtl8822cs_netdev_specialethertypeprocess,
319 };
320
rtl_get_net_dev_ops(void)321 struct NetDeviceInterFace *rtl_get_net_dev_ops(void)
322 {
323 return &g_wal_rtl_net_dev_ops;
324 }
325
hdf_rtl8822cs_netdev_init(struct NetDevice * netDev)326 int32_t hdf_rtl8822cs_netdev_init(struct NetDevice *netDev)
327 {
328 HDF_LOGE("%s: start...", __func__);
329 if (netDev == NULL) {
330 HDF_LOGE("%s: netDev null!", __func__);
331 return HDF_FAILURE;
332 }
333
334 HDF_LOGE("%s: netDev->name:%s\n", __func__, netDev->name);
335 netDev->netDeviceIf = rtl_get_net_dev_ops();
336 CreateEapolData(netDev);
337
338 return HDF_SUCCESS;
339 }
340
get_rtl_priv_data(void)341 void* get_rtl_priv_data(void)
342 {
343 return g_hdf_netdev->mlPriv;
344 }
345
get_rtl_netdev(void)346 struct NetDevice* get_rtl_netdev(void)
347 {
348 return g_hdf_netdev;
349 }
350
InitNetdev(struct NetDevice * netDevice)351 int32_t InitNetdev(struct NetDevice *netDevice)
352 {
353 uint32_t i;
354 int8_t vap_netdev_name[2][MAC_NET_DEVICE_NAME_LENGTH] = {"wlan0", "p2p0"};
355 enum nl80211_iftype vap_types[2] = {NL80211_IFTYPE_STATION, NL80211_IFTYPE_P2P_DEVICE};
356 struct NetDevice *netdev = NULL;
357 struct HdfWifiNetDeviceData *data = NULL;
358 void *netdevLinux = NULL;
359
360 if (netDevice == NULL) {
361 HDF_LOGE("%s:para is null!", __func__);
362 return HDF_FAILURE;
363 }
364
365 netdevLinux = GetLinuxInfByNetDevice(netDevice);
366 if (netdevLinux == NULL) {
367 HDF_LOGE("%s net_device is null!", __func__);
368 return HDF_FAILURE;
369 }
370 set_krn_netdev(netdevLinux);
371
372 data = GetPlatformData(netDevice);
373 if (data == NULL) {
374 HDF_LOGE("%s:netdevice data null!", __func__);
375 return HDF_FAILURE;
376 }
377
378 hdf_rtl8822cs_netdev_init(netDevice);
379
380 for (i = 0; i < (sizeof(vap_netdev_name) / sizeof(vap_netdev_name[0])); i++) {
381 netdev = NetDeviceGetInstByName((const int8_t *)vap_netdev_name[i]);
382 if (netdev == NULL) {
383 HDF_LOGE("%s:get dev by name failed! %s", __func__, vap_netdev_name[i]);
384 continue;
385 }
386
387 netdev->classDriverName = netDevice->classDriverName;
388 netdev->classDriverPriv = data;
389 }
390
391 g_hdf_netdev = netDevice;
392 NetDeviceAdd(get_rtl_netdev());
393
394 rtw_drv_entry();
395
396 return HDF_SUCCESS;
397 }
398
DeinitNetdev(void)399 int32_t DeinitNetdev(void)
400 {
401 HDF_LOGE("DeinitNetdev rtl8822cs.\n");
402 return HDF_SUCCESS;
403 }
404
405 struct NetDeviceInterFace *rtl_get_net_p2p_dev_ops(void);
406
hdf_p2p_netdev_init(struct NetDevice * netDev)407 static int32_t hdf_p2p_netdev_init(struct NetDevice *netDev)
408 {
409 HDF_LOGE("%s: start %s...", __func__, netDev->name);
410
411 if (netDev == NULL) {
412 HDF_LOGE("%s: netDev null!", __func__);
413 return HDF_FAILURE;
414 }
415
416 HDF_LOGE("%s: netDev->name:%s\n", __func__, netDev->name);
417 netDev->netDeviceIf = rtl_get_net_p2p_dev_ops();
418 CreateEapolData(netDev);
419
420 return HDF_SUCCESS;
421 }
422
hdf_p2p_netdev_deinit(struct NetDevice * netDev)423 static void hdf_p2p_netdev_deinit(struct NetDevice *netDev)
424 {
425 HDF_LOGE("%s: start...", __func__);
426 (void)netDev;
427 }
428
hdf_p2p_netdev_open(struct NetDevice * netDevice)429 static int32_t hdf_p2p_netdev_open(struct NetDevice *netDevice)
430 {
431 int32_t retVal = 0;
432 struct net_device *netdev = GetLinuxInfByNetDevice(netDevice);
433
434 if (netdev == NULL) {
435 HDF_LOGE("%s: netDev null!", __func__);
436 return HDF_FAILURE;
437 }
438
439 HDF_LOGE("%s: ndo_open %s...", __func__, netDevice->name);
440
441 retVal = (int32_t)netdev_open(netdev);
442 if (retVal < 0) {
443 HDF_LOGE("%s: hdf net device open failed! ret = %d", __func__, retVal);
444 }
445
446 netDevice->ieee80211Ptr = netdev->ieee80211_ptr;
447 if (netDevice->ieee80211Ptr == NULL) {
448 HDF_LOGE("%s: NULL == netDevice->ieee80211Ptr", __func__);
449 }
450
451 // update mac addr to NetDevice object
452 memcpy_s(netDevice->macAddr, MAC_ADDR_SIZE, netdev->dev_addr, netdev->addr_len);
453
454 return retVal;
455 }
456
hdf_p2p_netdev_stop(struct NetDevice * netDev)457 static int32_t hdf_p2p_netdev_stop(struct NetDevice *netDev)
458 {
459 HDF_LOGE("%s: stop %s...", __func__, netDev->name);
460 return HDF_SUCCESS;
461 }
462
hdf_p2p_netdev_xmit(struct NetDevice * netDev,NetBuf * netBuff)463 static int32_t hdf_p2p_netdev_xmit(struct NetDevice *netDev, NetBuf *netBuff)
464 {
465 HDF_LOGI("%s: start %s...", __func__, netDev->name);
466
467 if (netBuff) {
468 dev_kfree_skb_any(netBuff);
469 }
470
471 return HDF_SUCCESS;
472 }
473
hdf_p2p_netdev_ioctl(struct NetDevice * netDev,IfReq * req,int32_t cmd)474 static int32_t hdf_p2p_netdev_ioctl(struct NetDevice *netDev, IfReq *req, int32_t cmd)
475 {
476 HDF_LOGE("%s: start %s...", __func__, netDev->name);
477 return HDF_SUCCESS;
478 }
479
hdf_p2p_netdev_setmacaddr(struct NetDevice * netDev,void * addr)480 static int32_t hdf_p2p_netdev_setmacaddr(struct NetDevice *netDev, void *addr)
481 {
482 int32_t retVal = 0;
483 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
484
485 HDF_LOGE("%s: start %s...", __func__, netDev->name);
486
487 if (netdev == NULL || addr == NULL) {
488 HDF_LOGE("%s: netDev or addr null!", __func__);
489 return HDF_FAILURE;
490 }
491
492 memcpy_s(netdev->dev_addr, netdev->addr_len, addr, netdev->addr_len);
493 memcpy_s(netDev->macAddr, MAC_ADDR_SIZE, netdev->dev_addr, netdev->addr_len);
494
495 return retVal;
496 }
497
hdf_p2p_netdev_getstats(struct NetDevice * netDev)498 static struct NetDevStats *hdf_p2p_netdev_getstats(struct NetDevice *netDev)
499 {
500 static struct NetDevStats devStat = {0};
501 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
502
503 HDF_LOGE("%s: start %s...", __func__, netDev->name);
504
505 if (netdev == NULL) {
506 HDF_LOGE("%s: netDev null!", __func__);
507 return NULL;
508 }
509
510 return &devStat;
511 }
512
hdf_p2p_netdev_changemtu(struct NetDevice * netDev,int32_t mtu)513 static int32_t hdf_p2p_netdev_changemtu(struct NetDevice *netDev, int32_t mtu)
514 {
515 struct net_device *netdev = GetLinuxInfByNetDevice(netDev);
516 HDF_LOGE("%s: start %s...", __func__, netDev->name);
517 netdev->mtu = mtu;
518 return HDF_SUCCESS;
519 }
520
hdf_p2p_netdev_setnetifstats(struct NetDevice * netDev,NetIfStatus status)521 static void hdf_p2p_netdev_setnetifstats(struct NetDevice *netDev, NetIfStatus status)
522 {
523 HDF_LOGE("%s: start...", __func__);
524 (void)netDev;
525 (void)status;
526 }
527
hdf_p2p_netdev_selectqueue(struct NetDevice * netDev,NetBuf * netBuff)528 static uint16_t hdf_p2p_netdev_selectqueue(struct NetDevice *netDev, NetBuf *netBuff)
529 {
530 HDF_LOGE("%s: start...", __func__);
531 (void)netDev;
532 (void)netBuff;
533 return HDF_SUCCESS;
534 }
535
hdf_p2p_netdev_netifnotify(struct NetDevice * netDev,NetDevNotify * notify)536 static uint32_t hdf_p2p_netdev_netifnotify(struct NetDevice *netDev, NetDevNotify *notify)
537 {
538 HDF_LOGE("%s: start...", __func__);
539 (void)netDev;
540 (void)notify;
541 return HDF_SUCCESS;
542 }
543
hdf_p2p_netdev_linkstatuschanged(struct NetDevice * netDev)544 static void hdf_p2p_netdev_linkstatuschanged(struct NetDevice *netDev)
545 {
546 HDF_LOGE("%s: start...", __func__);
547 (void)netDev;
548 }
549
hdf_p2p_netdev_specialethertypeprocess(const struct NetDevice * netDev,NetBuf * buff)550 static ProcessingResult hdf_p2p_netdev_specialethertypeprocess(const struct NetDevice *netDev, NetBuf *buff)
551 {
552 const struct Eapol *eapolInstance = NULL;
553 int ret;
554 uint16_t protocol;
555 const int pidx0 = 12, pidx1 = 13;
556
557 HDF_LOGE("%s: start...", __func__);
558
559 if (netDev == NULL || buff == NULL) {
560 return PROCESSING_ERROR;
561 }
562
563 protocol = (buff->data[pidx0] << WIFI_SHIFT_BIT) | buff->data[pidx1];
564 if (protocol != ETHER_TYPE_PAE) {
565 HDF_LOGE("%s: return PROCESSING_CONTINUE", __func__);
566 return PROCESSING_CONTINUE;
567 }
568
569 if (netDev->specialProcPriv == NULL) {
570 HDF_LOGE("%s: return PROCESSING_ERROR", __func__);
571 return PROCESSING_ERROR;
572 }
573
574 eapolInstance = EapolGetInstance();
575 ret = eapolInstance->eapolOp->writeEapolToQueue(netDev, buff);
576 if (ret != HDF_SUCCESS) {
577 HDF_LOGE("%s: writeEapolToQueue failed", __func__);
578 NetBufFree(buff);
579 }
580
581 return PROCESSING_COMPLETE;
582 }
583
584 struct NetDeviceInterFace g_wal_p2p_net_dev_ops = {
585 .init = hdf_p2p_netdev_init,
586 .deInit = hdf_p2p_netdev_deinit,
587 .open = hdf_p2p_netdev_open,
588 .stop = hdf_p2p_netdev_stop,
589 .xmit = hdf_p2p_netdev_xmit,
590 .ioctl = hdf_p2p_netdev_ioctl,
591 .setMacAddr = hdf_p2p_netdev_setmacaddr,
592 .getStats = hdf_p2p_netdev_getstats,
593 .setNetIfStatus = hdf_p2p_netdev_setnetifstats,
594 .selectQueue = hdf_p2p_netdev_selectqueue,
595 .netifNotify = hdf_p2p_netdev_netifnotify,
596 .changeMtu = hdf_p2p_netdev_changemtu,
597 .linkStatusChanged = hdf_p2p_netdev_linkstatuschanged,
598 .specialEtherTypeProcess = hdf_p2p_netdev_specialethertypeprocess,
599 };
600
rtl_get_net_p2p_dev_ops(void)601 struct NetDeviceInterFace *rtl_get_net_p2p_dev_ops(void)
602 {
603 return &g_wal_p2p_net_dev_ops;
604 }
605
p2p_netdev_init(struct NetDevice * netDev)606 int32_t p2p_netdev_init(struct NetDevice *netDev)
607 {
608 HDF_LOGE("%s: start %s...", __func__, netDev->name);
609
610 if (netDev == NULL) {
611 HDF_LOGE("%s: netDev null!", __func__);
612 return HDF_FAILURE;
613 }
614
615 HDF_LOGE("%s: netDev->name:%s\n", __func__, netDev->name);
616 netDev->netDeviceIf = rtl_get_net_p2p_dev_ops();
617 CreateEapolData(netDev);
618
619 return HDF_SUCCESS;
620 }
621
InitP2pNetDev(struct NetDevice * netDevice)622 int InitP2pNetDev(struct NetDevice *netDevice)
623 {
624 struct NetDevice *hnetdev = NULL;
625 struct HdfWifiNetDeviceData *data = NULL;
626 struct net_device *netdev = NULL;
627 char *ifname = "p2p0";
628
629 if (netDevice == NULL) {
630 HDF_LOGE("%s:para is null!", __func__);
631 return -1;
632 }
633
634 hnetdev = NetDeviceInit(ifname, strlen(ifname), WIFI_LINK, FULL_OS);
635 if (hnetdev == NULL) {
636 HDF_LOGE("%s:netdev is null!", __func__);
637 return -1;
638 }
639
640 data = GetPlatformData(netDevice);
641 if (data == NULL) {
642 HDF_LOGE("%s:netdevice data null!", __func__);
643 return -1;
644 }
645
646 p2p_netdev_init(hnetdev); // set net_dev_ops
647 hnetdev->classDriverPriv = data;
648 hnetdev->classDriverName = netDevice->classDriverName;
649
650 NetDeviceAdd(hnetdev);
651
652 return HDF_SUCCESS;
653 }
654
DeinitP2pNetDev(void)655 int DeinitP2pNetDev(void)
656 {
657 HDF_LOGE("DeinitP2pNetDev rtl8822cs.\n");
658 return HDF_SUCCESS;
659 }
660
661 /* ****************************************************************************
662 功能描述 : 初始化wlan设备
663 输入参数 : [1]type 设备类型
664 [2]mode 模式
665 [3]NetDevice
666 输出参数 : [1]ifname 设备名
667 [2]len 设备名长度
668 返 回 值 : 错误码
669 **************************************************************************** */
670 wal_dev_addr_stru g_dev_addr = { 0 };
671
672 /* 根据设备类型分配mac地址索引 */
wal_get_dev_addr_idx(unsigned char type)673 static wal_addr_idx wal_get_dev_addr_idx(unsigned char type)
674 {
675 wal_addr_idx addr_idx = WAL_ADDR_IDX_BUTT;
676
677 switch (type) {
678 case PROTOCOL_80211_IFTYPE_STATION:
679 addr_idx = WAL_ADDR_IDX_STA0;
680 break;
681 case PROTOCOL_80211_IFTYPE_AP:
682 case PROTOCOL_80211_IFTYPE_P2P_CLIENT:
683 case PROTOCOL_80211_IFTYPE_P2P_GO:
684 case PROTOCOL_80211_IFTYPE_MESH_POINT:
685 addr_idx = WAL_ADDR_IDX_AP0;
686 break;
687 case PROTOCOL_80211_IFTYPE_P2P_DEVICE:
688 addr_idx = WAL_ADDR_IDX_STA2;
689 break;
690 default:
691 HDF_LOGE("wal_get_dev_addr_idx:: dev type [%d] is not supported !", type);
692 break;
693 }
694
695 return addr_idx;
696 }
697
wal_get_dev_addr(unsigned char * pc_addr,unsigned char addr_len,unsigned char type)698 uint32_t wal_get_dev_addr(unsigned char *pc_addr, unsigned char addr_len, unsigned char type) /* 建议5.5误报,166行有元素赋值 */
699 {
700 uint16_t us_addr[ETHER_ADDR_LEN];
701 uint32_t tmp;
702 wal_addr_idx addr_idx;
703
704 if (pc_addr == NULL) {
705 HDF_LOGE("wal_get_dev_addr:: pc_addr is NULL!");
706 return HDF_FAILURE;
707 }
708
709 addr_idx = wal_get_dev_addr_idx(type);
710 if (addr_idx >= WAL_ADDR_IDX_BUTT) {
711 return HDF_FAILURE;
712 }
713
714 for (tmp = 0; tmp < ETHER_ADDR_LEN; tmp++) {
715 us_addr[tmp] = (uint16_t)g_dev_addr.ac_addr[tmp];
716 }
717
718 /* 1.低位自增 2.高位取其进位 3.低位将进位位置0 */
719 us_addr[5] += addr_idx; /* 5 地址第6位 */
720 us_addr[4] += ((us_addr[5] & (0x100)) >> 8); /* 4 地址第5位 5 地址第6位 8 右移8位 */
721 us_addr[5] = us_addr[5] & (0xff); /* 5 地址第6位 */
722 /* 最低位运算完成,下面类似 */
723 us_addr[3] += ((us_addr[4] & (0x100)) >> 8); /* 3 地址第4位 4 地址第5位 8 右移8位 */
724 us_addr[4] = us_addr[4] & (0xff); /* 4 地址第5位 */
725 us_addr[2] += ((us_addr[3] & (0x100)) >> 8); /* 2 地址第3位 3 地址第4位 8 右移8位 */
726 us_addr[3] = us_addr[3] & (0xff); /* 3 地址第4位 */
727 us_addr[1] += ((us_addr[2] & (0x100)) >> 8); /* 1 地址第2位 2 地址第3位 8 右移8位 */
728 us_addr[2] = us_addr[2] & (0xff); /* 2 地址第3位 */
729 us_addr[0] += ((us_addr[1] & (0x100)) >> 8); /* 8 右移8位 */
730 us_addr[1] = us_addr[1] & (0xff);
731 if (us_addr[0] > 0xff) {
732 us_addr[0] = 0;
733 }
734 us_addr[0] &= 0xFE;
735
736 for (tmp = 0; tmp < addr_len; tmp++) {
737 pc_addr[tmp] = (unsigned char)us_addr[tmp];
738 }
739
740 return HDF_SUCCESS;
741 }
742
mac_addr_is_zero(const unsigned char * mac_addr)743 unsigned char mac_addr_is_zero(const unsigned char *mac_addr)
744 {
745 unsigned char zero_mac_addr[MAC_ADDR_SIZE] = {0};
746
747 if (mac_addr == NULL) {
748 return true;
749 }
750
751 return (memcmp(zero_mac_addr, mac_addr, MAC_ADDR_SIZE) == 0);
752 }
753
rtl_macaddr_check(const unsigned char * mac_addr)754 uint32_t rtl_macaddr_check(const unsigned char *mac_addr)
755 {
756 if ((mac_addr_is_zero(mac_addr) == 1) || ((mac_addr[0] & 0x1) == 0x1)) {
757 return HDF_FAILURE;
758 }
759
760 return HDF_SUCCESS;
761 }
762
rtl_set_dev_addr_from_efuse(const char * pc_addr,unsigned char mac_len)763 uint32_t rtl_set_dev_addr_from_efuse(const char *pc_addr, unsigned char mac_len)
764 {
765 if (pc_addr == NULL) {
766 HDF_LOGE("rtl_set_dev_addr:: pc_addr is NULL!");
767 return HDF_FAILURE;
768 }
769
770 if (rtl_macaddr_check((unsigned char *)pc_addr) != HDF_SUCCESS) {
771 HDF_LOGE("rtl_set_dev_addr:: mac from efuse is zero!");
772 return HDF_FAILURE;
773 }
774
775 if (memcpy_s(g_dev_addr.ac_addr, ETHER_ADDR_LEN, pc_addr, mac_len) != EOK) {
776 HDF_LOGE("rtl_set_dev_addr:: memcpy_s FAILED");
777 return HDF_FAILURE;
778 }
779
780 set_efuse_mac_exist(1);
781
782 return HDF_SUCCESS;
783 }
784
rtl_set_dev_addr(const char * pc_addr,unsigned char mac_len)785 uint32_t rtl_set_dev_addr(const char *pc_addr, unsigned char mac_len)
786 {
787 uint32_t count = 0;
788
789 if (pc_addr == NULL) {
790 HDF_LOGE("rtl_set_dev_addr:: pc_addr is NULL!");
791 return HDF_FAILURE;
792 }
793
794 count = NetDevGetRegisterCount();
795 if (count > 1) {
796 HDF_LOGE("rtl_set_dev_addr::vap exist, could not set mac address!");
797 return HDF_FAILURE;
798 }
799
800 if (memcpy_s(g_dev_addr.ac_addr, ETHER_ADDR_LEN, pc_addr, mac_len) != EOK) {
801 HDF_LOGE("{rtl_set_dev_addr::mem safe function err!}");
802 return HDF_FAILURE;
803 }
804
805 return HDF_SUCCESS;
806 }
807
808 #ifdef __cplusplus
809 #if __cplusplus
810 }
811 #endif
812 #endif
813