1/*
2
3	Copyright 2005, Broadcom Corporation
4	All Rights Reserved.
5
6	THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
7	KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
8	SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
9	FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
10
11*/
12
13#include "rc.h"
14#include "shared.h"
15#include "version.h"
16#include "interface.h"
17
18#include <termios.h>
19#include <dirent.h>
20#include <sys/ioctl.h>
21#include <sys/mount.h>
22#include <time.h>
23#include <errno.h>
24#include <paths.h>
25#include <sys/wait.h>
26#include <sys/reboot.h>
27#include <sys/klog.h>
28#ifdef LINUX26
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/sysinfo.h>
32#endif
33#include <wlutils.h>
34#include <bcmdevs.h>
35
36#include <linux/major.h>
37
38int wan_phyid = -1;
39
40void init_devs(void)
41{
42}
43
44
45void generate_switch_para(void)
46{
47	int model, cfg;
48	char lan[SWCFG_BUFSIZE], wan[SWCFG_BUFSIZE];
49#ifdef RTCONFIG_GMAC3
50	char glan[2*SWCFG_BUFSIZE];
51	char var[32], *lists, *next;
52
53	int gmac3_enable = nvram_get_int("gmac3_enable");
54	memset(glan, 0, sizeof(glan));
55#endif
56
57	// generate nvram nvram according to system setting
58	model = get_model();
59
60	if (is_routing_enabled()) {
61		cfg = nvram_get_int("switch_stb_x");
62		if (cfg < SWCFG_DEFAULT || cfg > SWCFG_STB34)
63			cfg = SWCFG_DEFAULT;
64#ifdef RTCONFIG_MULTICAST_IPTV
65		if (cfg == 7)
66			cfg = SWCFG_STB3;
67#endif
68	}
69	/* don't do this to save ports */
70	//else if (nvram_get_int("sw_mode") == SW_MODE_REPEATER ||
71	//	((nvram_get_int("sw_mode") == SW_MODE_AP) && (nvram_get_int("wlc_psta")))
72	//	cfg = SWCFG_PSTA;
73	else if (nvram_get_int("sw_mode") == SW_MODE_AP)
74		cfg = SWCFG_BRIDGE;
75	else
76		cfg = SWCFG_DEFAULT;	// keep wan port, but get ip from bridge
77
78	switch(model) {
79		/* BCM5325 series */
80		case MODEL_APN12HP:
81		{					/* WAN L1 L2 L3 L4 CPU */
82			const int ports[SWPORT_COUNT] = { 4, 3, 2, 1, 0, 5 };
83			/* TODO: switch_wantag? */
84
85			wan_phyid = ports[0];	// record the phy num of the wan port on the case
86			//if (!is_routing_enabled())
87			//	nvram_set("lan_ifnames", "eth0 eth1");	// override
88			switch_gen_config(lan, ports, SWCFG_BRIDGE, 0, "*");
89			switch_gen_config(wan, ports, SWCFG_BRIDGE, 1, "u");
90			nvram_set("vlan0ports", lan);
91			nvram_set("vlan1ports", wan);
92#ifdef RTCONFIG_LANWAN_LED
93			// for led, always keep original port map
94			cfg = SWCFG_DEFAULT;
95#endif
96			switch_gen_config(lan, ports, cfg, 0, NULL);
97			switch_gen_config(wan, ports, cfg, 1, NULL);
98			nvram_set("lanports", lan);
99			nvram_set("wanports", wan);
100			break;
101		}
102
103		/* BCM5325 series */
104		case MODEL_RTN14UHP:
105		{
106			/* WAN L1 L2 L3 L4 CPU */
107			const int ports[SWPORT_COUNT] = { 4, 0, 1, 2, 3, 5 };
108			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
109
110#ifdef RTCONFIG_DUALWAN
111			int wan1cfg = nvram_get_int("wans_lanport");
112
113			nvram_unset("vlan1ports");
114			nvram_unset("vlan1hwname");
115			nvram_unset("vlan2ports");
116			nvram_unset("vlan2hwname");
117
118			wan_phyid = ports[0];	// record the phy num of the wan port on the case
119			// The first WAN port.
120			if (get_wans_dualwan()&WANSCAP_WAN) {
121				switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
122				nvram_set("vlan1ports", wan);
123				nvram_set("vlan1hwname", "et0");
124			}
125
126			// The second WAN port.
127			if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
128				wan1cfg += WAN1PORT1-1;
129				if (wancfg != SWCFG_DEFAULT) {
130					gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
131					nvram_set("vlan0ports", lan);
132					gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
133					nvram_set("lanports", lan);
134				}
135				else {
136					switch_gen_config(lan, ports, wan1cfg, 0, "*");
137					nvram_set("vlan0ports", lan);
138					switch_gen_config(lan, ports, wan1cfg, 0, NULL);
139					nvram_set("lanports", lan);
140				}
141
142				switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
143				if (get_wans_dualwan()&WANSCAP_WAN) {
144					nvram_set("vlan2ports", wan);
145					nvram_set("vlan2hwname", "et0");
146				}
147				else {
148					nvram_set("vlan1ports", wan);
149					nvram_set("vlan1hwname", "et0");
150				}
151			}
152			else {
153				switch_gen_config(lan, ports, cfg, 0, "*");
154				nvram_set("vlan0ports", lan);
155				switch_gen_config(lan, ports, cfg, 0, NULL);
156				nvram_set("lanports", lan);
157			}
158
159			int unit;
160			char prefix[8], nvram_ports[16];
161
162			for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
163				memset(prefix, 0, 8);
164				sprintf(prefix, "%d", unit);
165
166				memset(nvram_ports, 0, 16);
167				sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
168
169				if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
170					switch_gen_config(wan, ports, wancfg, 1, NULL);
171					nvram_set(nvram_ports, wan);
172				}
173				else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
174					switch_gen_config(wan, ports, wan1cfg, 1, NULL);
175					nvram_set(nvram_ports, wan);
176				}
177				else
178					nvram_unset(nvram_ports);
179			}
180#else
181			switch_gen_config(lan, ports, cfg, 0, "*");
182			switch_gen_config(wan, ports, wancfg, 1, "u");
183			nvram_set("vlan0ports", lan);
184			nvram_set("vlan1ports", wan);
185#ifdef RTCONFIG_LANWAN_LED
186			// for led, always keep original port map
187			wancfg = SWCFG_DEFAULT;
188#endif
189			switch_gen_config(lan, ports, cfg, 0, NULL);
190			switch_gen_config(wan, ports, wancfg, 1, NULL);
191			nvram_set("lanports", lan);
192			nvram_set("wanports", wan);
193#endif
194			break;
195		}
196
197		/* BCM5325 series */
198		case MODEL_RTN53:
199		case MODEL_RTN12:
200		case MODEL_RTN12B1:
201		case MODEL_RTN12C1:
202		case MODEL_RTN12D1:
203		case MODEL_RTN12VP:
204		case MODEL_RTN12HP:
205		case MODEL_RTN12HP_B1:
206		case MODEL_RTN10P:
207		case MODEL_RTN10D1:
208		case MODEL_RTN10PV2:
209		{					/* WAN L1 L2 L3 L4 CPU */
210			const int ports[SWPORT_COUNT] = { 4, 3, 2, 1, 0, 5 };
211			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
212
213			wan_phyid = ports[0];	// record the phy num of the wan port on the case
214#ifdef RTCONFIG_DUALWAN
215			if (cfg != SWCFG_BRIDGE) {
216				int wan1cfg = nvram_get_int("wans_lanport");
217
218				nvram_unset("vlan1ports");
219				nvram_unset("vlan1hwname");
220				nvram_unset("vlan2ports");
221				nvram_unset("vlan2hwname");
222
223				// The first WAN port.
224				if (get_wans_dualwan()&WANSCAP_WAN) {
225					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
226					nvram_set("vlan1ports", wan);
227					nvram_set("vlan1hwname", "et0");
228				}
229
230				// The second WAN port.
231				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
232					wan1cfg += WAN1PORT1-1;
233					if (wancfg != SWCFG_DEFAULT) {
234						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
235						nvram_set("vlan0ports", lan);
236						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
237						nvram_set("lanports", lan);
238					}
239					else {
240						switch_gen_config(lan, ports, wan1cfg, 0, "*");
241						nvram_set("vlan0ports", lan);
242						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
243						nvram_set("lanports", lan);
244					}
245
246					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
247					if (get_wans_dualwan()&WANSCAP_WAN) {
248						nvram_set("vlan2ports", wan);
249						nvram_set("vlan2hwname", "et0");
250					}
251					else {
252						nvram_set("vlan1ports", wan);
253						nvram_set("vlan1hwname", "et0");
254					}
255				}
256				else {
257					switch_gen_config(lan, ports, cfg, 0, "*");
258					nvram_set("vlan0ports", lan);
259					switch_gen_config(lan, ports, cfg, 0, NULL);
260					nvram_set("lanports", lan);
261				}
262
263				int unit;
264				char prefix[8], nvram_ports[16];
265
266				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
267					memset(prefix, 0, 8);
268					sprintf(prefix, "%d", unit);
269
270					memset(nvram_ports, 0, 16);
271					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
272
273					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
274						switch_gen_config(wan, ports, wancfg, 1, NULL);
275						nvram_set(nvram_ports, wan);
276					}
277					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
278						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
279						nvram_set(nvram_ports, wan);
280					}
281					else
282						nvram_unset(nvram_ports);
283				}
284			}
285			else {
286				switch_gen_config(lan, ports, cfg, 0, "*");
287				switch_gen_config(wan, ports, wancfg, 1, "");
288				nvram_set("vlan0ports", lan);
289				nvram_set("vlan1ports", wan);
290				switch_gen_config(lan, ports, cfg, 0, NULL);
291				switch_gen_config(wan, ports, wancfg, 1, NULL);
292				nvram_set("lanports", lan);
293				nvram_set("wanports", wan);
294				nvram_unset("wan1ports");
295			}
296#else
297			switch_gen_config(lan, ports, cfg, 0, "*");
298			switch_gen_config(wan, ports, wancfg, 1, "u");
299			nvram_set("vlan0ports", lan);
300			nvram_set("vlan1ports", wan);
301#ifdef RTCONFIG_LANWAN_LED
302			// for led, always keep original port map
303			cfg = SWCFG_DEFAULT;
304#endif
305			switch_gen_config(lan, ports, cfg, 0, NULL);
306			switch_gen_config(wan, ports, wancfg, 1, NULL);
307			nvram_set("lanports", lan);
308			nvram_set("wanports", wan);
309#endif
310			break;
311		}
312
313		case MODEL_RTN10U:
314		{					/* WAN L1 L2 L3 L4 CPU */
315			const int ports[SWPORT_COUNT] = { 0, 4, 3, 2, 1, 5 };
316			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
317
318			wan_phyid = ports[0];	// record the phy num of the wan port on the case
319#ifdef RTCONFIG_DUALWAN
320			if (cfg != SWCFG_BRIDGE) {
321				int wan1cfg = nvram_get_int("wans_lanport");
322
323				nvram_unset("vlan1ports");
324				nvram_unset("vlan1hwname");
325				nvram_unset("vlan2ports");
326				nvram_unset("vlan2hwname");
327
328				// The first WAN port.
329				if (get_wans_dualwan()&WANSCAP_WAN) {
330					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
331					nvram_set("vlan1ports", wan);
332					nvram_set("vlan1hwname", "et0");
333				}
334
335				// The second WAN port.
336				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
337					wan1cfg += WAN1PORT1-1;
338					if (wancfg != SWCFG_DEFAULT) {
339						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
340						nvram_set("vlan0ports", lan);
341						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
342						nvram_set("lanports", lan);
343					}
344					else {
345						switch_gen_config(lan, ports, wan1cfg, 0, "*");
346						nvram_set("vlan0ports", lan);
347						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
348						nvram_set("lanports", lan);
349					}
350
351					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
352					if (get_wans_dualwan()&WANSCAP_WAN) {
353						nvram_set("vlan2ports", wan);
354						nvram_set("vlan2hwname", "et0");
355					}
356					else {
357						nvram_set("vlan1ports", wan);
358						nvram_set("vlan1hwname", "et0");
359					}
360				}
361				else {
362					switch_gen_config(lan, ports, cfg, 0, "*");
363					nvram_set("vlan0ports", lan);
364					switch_gen_config(lan, ports, cfg, 0, NULL);
365					nvram_set("lanports", lan);
366				}
367
368				int unit;
369				char prefix[8], nvram_ports[16];
370
371				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
372					memset(prefix, 0, 8);
373					sprintf(prefix, "%d", unit);
374
375					memset(nvram_ports, 0, 16);
376					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
377
378					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
379						switch_gen_config(wan, ports, wancfg, 1, NULL);
380						nvram_set(nvram_ports, wan);
381					}
382					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
383						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
384						nvram_set(nvram_ports, wan);
385					}
386					else
387						nvram_unset(nvram_ports);
388				}
389			}
390			else {
391				switch_gen_config(lan, ports, cfg, 0, "*");
392				switch_gen_config(wan, ports, wancfg, 1, "");
393				nvram_set("vlan0ports", lan);
394				nvram_set("vlan1ports", wan);
395				switch_gen_config(lan, ports, cfg, 0, NULL);
396				switch_gen_config(wan, ports, wancfg, 1, NULL);
397				nvram_set("lanports", lan);
398				nvram_set("wanports", wan);
399				nvram_unset("wan1ports");
400			}
401#else
402			switch_gen_config(lan, ports, cfg, 0, "*");
403			switch_gen_config(wan, ports, wancfg, 1, "u");
404			nvram_set("vlan0ports", lan);
405			nvram_set("vlan1ports", wan);
406			switch_gen_config(lan, ports, cfg, 0, NULL);
407			switch_gen_config(wan, ports, wancfg, 1, NULL);
408			nvram_set("lanports", lan);
409			nvram_set("wanports", wan);
410#endif
411			break;
412		}
413
414		/* BCM53125 series */
415		case MODEL_RTN15U:
416		{					/* WAN L1 L2 L3 L4 CPU */
417			const int ports[SWPORT_COUNT] = { 4, 3, 2, 1, 0, 8 };
418			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
419
420			wan_phyid = ports[0];	// record the phy num of the wan port on the case
421#ifdef RTCONFIG_DUALWAN
422			int wan1cfg = nvram_get_int("wans_lanport");
423
424			nvram_unset("vlan2ports");
425			nvram_unset("vlan2hwname");
426			nvram_unset("vlan3ports");
427			nvram_unset("vlan3hwname");
428
429			// The first WAN port.
430			if (get_wans_dualwan()&WANSCAP_WAN) {
431				switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
432				nvram_set("vlan2ports", wan);
433				nvram_set("vlan2hwname", "et0");
434			}
435
436			// The second WAN port.
437			if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
438				wan1cfg += WAN1PORT1-1;
439				if (wancfg != SWCFG_DEFAULT) {
440					gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
441					nvram_set("vlan1ports", lan);
442					gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
443					nvram_set("lanports", lan);
444				}
445				else {
446					switch_gen_config(lan, ports, wan1cfg, 0, "*");
447					nvram_set("vlan1ports", lan);
448					switch_gen_config(lan, ports, wan1cfg, 0, NULL);
449					nvram_set("lanports", lan);
450				}
451
452				switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
453				if (get_wans_dualwan()&WANSCAP_WAN) {
454					nvram_set("vlan3ports", wan);
455					nvram_set("vlan3hwname", "et0");
456				}
457				else {
458					nvram_set("vlan2ports", wan);
459					nvram_set("vlan2hwname", "et0");
460				}
461			}
462			else {
463				switch_gen_config(lan, ports, cfg, 0, "*");
464				nvram_set("vlan1ports", lan);
465				switch_gen_config(lan, ports, cfg, 0, NULL);
466				nvram_set("lanports", lan);
467			}
468
469			int unit;
470			char prefix[8], nvram_ports[16];
471
472			for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
473				memset(prefix, 0, 8);
474				sprintf(prefix, "%d", unit);
475
476				memset(nvram_ports, 0, 16);
477				sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
478
479				if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
480					switch_gen_config(wan, ports, wancfg, 1, NULL);
481					nvram_set(nvram_ports, wan);
482				}
483				else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
484					switch_gen_config(wan, ports, wan1cfg, 1, NULL);
485					nvram_set(nvram_ports, wan);
486				}
487				else
488					nvram_unset(nvram_ports);
489			}
490#else
491			switch_gen_config(lan, ports, cfg, 0, "*");
492			switch_gen_config(wan, ports, wancfg, 1, "u");
493			nvram_set("vlan1ports", lan);
494			nvram_set("vlan2ports", wan);
495			switch_gen_config(lan, ports, cfg, 0, NULL);
496			switch_gen_config(wan, ports, wancfg, 1, NULL);
497			nvram_set("lanports", lan);
498			nvram_set("wanports", wan);
499#endif
500			break;
501		}
502
503		case MODEL_RTN16:
504		{					/* WAN L1 L2 L3 L4 CPU */
505			const int ports[SWPORT_COUNT] = { 0, 4, 3, 2, 1, 8 };
506			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
507
508			wan_phyid = ports[0];	// record the phy num of the wan port on the case
509#ifdef RTCONFIG_DUALWAN
510			if (cfg != SWCFG_BRIDGE) {
511				int wan1cfg = nvram_get_int("wans_lanport");
512
513				nvram_unset("vlan2ports");
514				nvram_unset("vlan2hwname");
515				nvram_unset("vlan3ports");
516				nvram_unset("vlan3hwname");
517
518				// The first WAN port.
519				if (get_wans_dualwan()&WANSCAP_WAN) {
520					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
521					nvram_set("vlan2ports", wan);
522					nvram_set("vlan2hwname", "et0");
523				}
524
525				// The second WAN port.
526				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
527					wan1cfg += WAN1PORT1-1;
528					if (wancfg != SWCFG_DEFAULT) {
529						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
530						nvram_set("vlan1ports", lan);
531						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
532						nvram_set("lanports", lan);
533					}
534					else {
535						switch_gen_config(lan, ports, wan1cfg, 0, "*");
536						nvram_set("vlan1ports", lan);
537						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
538						nvram_set("lanports", lan);
539					}
540
541					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
542					if (get_wans_dualwan()&WANSCAP_WAN) {
543						nvram_set("vlan3ports", wan);
544						nvram_set("vlan3hwname", "et0");
545					}
546					else {
547						nvram_set("vlan2ports", wan);
548						nvram_set("vlan2hwname", "et0");
549					}
550				}
551				else {
552					switch_gen_config(lan, ports, cfg, 0, "*");
553					nvram_set("vlan1ports", lan);
554					switch_gen_config(lan, ports, cfg, 0, NULL);
555					nvram_set("lanports", lan);
556				}
557
558				int unit;
559				char prefix[8], nvram_ports[16];
560
561				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
562					memset(prefix, 0, 8);
563					sprintf(prefix, "%d", unit);
564
565					memset(nvram_ports, 0, 16);
566					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
567
568					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
569						switch_gen_config(wan, ports, wancfg, 1, NULL);
570						nvram_set(nvram_ports, wan);
571					}
572					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
573						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
574						nvram_set(nvram_ports, wan);
575					}
576					else
577						nvram_unset(nvram_ports);
578				}
579			}
580			else {
581				switch_gen_config(lan, ports, cfg, 0, "*");
582				switch_gen_config(wan, ports, wancfg, 1, "");
583				nvram_set("vlan1ports", lan);
584				nvram_set("vlan2ports", wan);
585				switch_gen_config(lan, ports, cfg, 0, NULL);
586				switch_gen_config(wan, ports, wancfg, 1, NULL);
587				nvram_set("lanports", lan);
588				nvram_set("wanports", wan);
589				nvram_unset("wan1ports");
590			}
591#else
592			switch_gen_config(lan, ports, cfg, 0, "*");
593			switch_gen_config(wan, ports, wancfg, 1, "u");
594			nvram_set("vlan1ports", lan);
595			nvram_set("vlan2ports", wan);
596			switch_gen_config(lan, ports, cfg, 0, NULL);
597			switch_gen_config(wan, ports, wancfg, 1, NULL);
598			nvram_set("lanports", lan);
599			nvram_set("wanports", wan);
600#endif
601			break;
602		}
603
604		case MODEL_RTAC3200:
605		{					/* WAN L1 L2 L3 L4 CPU */	/*vision: WAN L4 L3 L2 L1 */
606			int ports[SWPORT_COUNT] = { 0, 4, 3, 2, 1, 5 };
607#ifdef RTCONFIG_GMAC3
608			if (gmac3_enable)
609				ports[SWPORT_COUNT-1] = 8;
610#endif
611
612			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
613
614			wan_phyid = ports[0];   // record the phy num of the wan port on the case
615#ifdef RTCONFIG_DUALWAN
616			if (cfg != SWCFG_BRIDGE) {
617				int wan1cfg = nvram_get_int("wans_lanport");
618
619				nvram_unset("vlan2ports");
620				nvram_unset("vlan2hwname");
621				nvram_unset("vlan3ports");
622				nvram_unset("vlan3hwname");
623
624				// The first WAN port.
625				if (get_wans_dualwan()&WANSCAP_WAN) {
626					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
627					nvram_set("vlan2ports", wan);
628					nvram_set("vlan2hwname", "et0");
629				}
630
631				// The second WAN port.
632				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
633					wan1cfg += WAN1PORT1-1;
634					if (wancfg != SWCFG_DEFAULT) {
635						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
636						nvram_set("vlan1ports", lan);
637						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
638						nvram_set("lanports", lan);
639					}
640					else {
641						switch_gen_config(lan, ports, wan1cfg, 0, "*");
642						nvram_set("vlan1ports", lan);
643						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
644						nvram_set("lanports", lan);
645					}
646
647					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
648					if (get_wans_dualwan()&WANSCAP_WAN) {
649						nvram_set("vlan3ports", wan);
650						nvram_set("vlan3hwname", "et0");
651					}
652					else {
653						nvram_set("vlan2ports", wan);
654						nvram_set("vlan2hwname", "et0");
655					}
656				}
657				else {
658					switch_gen_config(lan, ports, cfg, 0, "*");
659					nvram_set("vlan1ports", lan);
660					switch_gen_config(lan, ports, cfg, 0, NULL);
661					nvram_set("lanports", lan);
662				}
663
664				int unit;
665				char prefix[8], nvram_ports[16];
666
667				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
668					memset(prefix, 0, 8);
669					sprintf(prefix, "%d", unit);
670
671					memset(nvram_ports, 0, 16);
672					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
673
674					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
675						switch_gen_config(wan, ports, wancfg, 1, NULL);
676						nvram_set(nvram_ports, wan);
677					}
678					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
679						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
680						nvram_set(nvram_ports, wan);
681					}
682					else
683						nvram_unset(nvram_ports);
684				}
685			}
686			else {
687				switch_gen_config(lan, ports, cfg, 0, "*");
688				switch_gen_config(wan, ports, wancfg, 1, "");
689				nvram_set("vlan1ports", lan);
690				nvram_set("vlan2ports", wan);
691				switch_gen_config(lan, ports, cfg, 0, NULL);
692				switch_gen_config(wan, ports, wancfg, 1, NULL);
693				nvram_set("lanports", lan);
694				nvram_set("wanports", wan);
695				nvram_unset("wan1ports");
696			}
697#else
698			switch_gen_config(lan, ports, cfg, 0, "*");
699			switch_gen_config(wan, ports, wancfg, 1, "u");
700			nvram_set("vlan1ports", lan);
701			nvram_set("vlan2ports", wan);
702			switch_gen_config(lan, ports, cfg, 0, NULL);
703			switch_gen_config(wan, ports, wancfg, 1, NULL);
704			nvram_set("lanports", lan);
705			nvram_set("wanports", wan);
706#endif
707			break;
708		}
709
710
711		case MODEL_RPAC68U:						/* 0  1  2  3  4 */
712		case MODEL_RTAC68U:						/* 0  1  2  3  4 */
713		case MODEL_RTN18U:						/* 0  1  2  3  4 */
714		case MODEL_RTAC53U:
715		{				/* WAN L1 L2 L3 L4 CPU */	/*vision: WAN L1 L2 L3 L4 */
716			const int ports[SWPORT_COUNT] = { 0, 1, 2, 3, 4, 5 };
717			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
718
719			wan_phyid = ports[0];	// record the phy num of the wan port on the case
720#ifdef RTCONFIG_DUALWAN
721			if (cfg != SWCFG_BRIDGE) {
722				int wan1cfg = nvram_get_int("wans_lanport");
723
724				nvram_unset("vlan2ports");
725				nvram_unset("vlan2hwname");
726				nvram_unset("vlan3ports");
727				nvram_unset("vlan3hwname");
728
729				// The first WAN port.
730				if (get_wans_dualwan()&WANSCAP_WAN) {
731					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
732					nvram_set("vlan2ports", wan);
733					nvram_set("vlan2hwname", "et0");
734				}
735
736				// The second WAN port.
737				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
738					wan1cfg += WAN1PORT1-1;
739					if (wancfg != SWCFG_DEFAULT) {
740						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
741						nvram_set("vlan1ports", lan);
742						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
743						nvram_set("lanports", lan);
744					}
745					else {
746						switch_gen_config(lan, ports, wan1cfg, 0, "*");
747						nvram_set("vlan1ports", lan);
748						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
749						nvram_set("lanports", lan);
750					}
751
752					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
753					if (get_wans_dualwan()&WANSCAP_WAN) {
754						nvram_set("vlan3ports", wan);
755						nvram_set("vlan3hwname", "et0");
756					}
757					else {
758						nvram_set("vlan2ports", wan);
759						nvram_set("vlan2hwname", "et0");
760					}
761				}
762				else {
763					switch_gen_config(lan, ports, cfg, 0, "*");
764					nvram_set("vlan1ports", lan);
765					switch_gen_config(lan, ports, cfg, 0, NULL);
766					nvram_set("lanports", lan);
767				}
768
769				int unit;
770				char prefix[8], nvram_ports[16];
771
772				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
773					memset(prefix, 0, 8);
774					sprintf(prefix, "%d", unit);
775
776					memset(nvram_ports, 0, 16);
777					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
778
779					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
780						switch_gen_config(wan, ports, wancfg, 1, NULL);
781						nvram_set(nvram_ports, wan);
782					}
783					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
784						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
785						nvram_set(nvram_ports, wan);
786					}
787					else
788						nvram_unset(nvram_ports);
789				}
790			}
791			else {
792				switch_gen_config(lan, ports, cfg, 0, "*");
793				switch_gen_config(wan, ports, wancfg, 1, "");
794				nvram_set("vlan1ports", lan);
795				nvram_set("vlan2ports", wan);
796				switch_gen_config(lan, ports, cfg, 0, NULL);
797				switch_gen_config(wan, ports, wancfg, 1, NULL);
798				nvram_set("lanports", lan);
799				nvram_set("wanports", wan);
800				nvram_unset("wan1ports");
801			}
802#else
803			switch_gen_config(lan, ports, cfg, 0, "*");
804			switch_gen_config(wan, ports, wancfg, 1, "u");
805			nvram_set("vlan1ports", lan);
806			nvram_set("vlan2ports", wan);
807			switch_gen_config(lan, ports, cfg, 0, NULL);
808			switch_gen_config(wan, ports, wancfg, 1, NULL);
809			nvram_set("lanports", lan);
810			nvram_set("wanports", wan);
811#endif
812			break;
813		}
814
815		case MODEL_DSLAC68U:
816		{
817			const int ports[SWPORT_COUNT] = { 0, 1, 2, 3, 4, 5 };
818			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
819
820			wan_phyid = ports[0];	// record the phy num of the wan port on the case
821#ifdef RTCONFIG_DUALWAN
822			if (cfg != SWCFG_BRIDGE) {
823				int wan1cfg = nvram_get_int("wans_lanport");
824				int unit;
825				char nv[16];
826				int vid;
827
828				nvram_unset("vlan3ports");
829				nvram_unset("vlan3hwname");
830
831				switch_gen_config(wan, ports, wancfg, 1, "t");
832				nvram_set("vlan2ports", wan);
833				nvram_set("vlan2hwname", "et0");
834
835				// port for DSL
836				if (get_wans_dualwan()&WANSCAP_DSL) {
837					switch_gen_config(wan, ports, wancfg, 1, "t");
838					nvram_set("vlan100ports", wan);
839					nvram_set("vlan100hwname", "et0");
840				}
841
842				// port for LAN/WAN
843				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
844					wan1cfg += WAN1PORT1-1;
845					if (wancfg != SWCFG_DEFAULT) {
846						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
847						nvram_set("vlan1ports", lan);
848						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
849						nvram_set("lanports", lan);
850					}
851					else {
852						switch_gen_config(lan, ports, wan1cfg, 0, "*");
853						nvram_set("vlan1ports", lan);
854						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
855						nvram_set("lanports", lan);
856					}
857
858					switch_gen_config(wan, ports, wan1cfg, 1, "t");
859					if(nvram_match("ewan_dot1q", "1"))
860						vid = nvram_get_int("ewan_vid");
861					else
862						vid = 4;
863					snprintf(nv, sizeof(nv), "vlan%dports", vid);
864					nvram_set(nv, wan);
865					snprintf(nv, sizeof(nv), "vlan%dhwname", vid);
866					nvram_set(nv, "et0");
867				}
868				else {
869					switch_gen_config(lan, ports, cfg, 0, "*");
870					nvram_set("vlan1ports", lan);
871					switch_gen_config(lan, ports, cfg, 0, NULL);
872					nvram_set("lanports", lan);
873				}
874
875				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
876					if(unit == WAN_UNIT_FIRST)
877						snprintf(nv, sizeof(nv), "wanports");
878					else
879						snprintf(nv, sizeof(nv), "wan%dports", unit);
880
881					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
882						switch_gen_config(wan, ports, wancfg, 1, NULL);
883						nvram_set(nv, wan);
884					}
885					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
886						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
887						nvram_set(nv, wan);
888					}
889					else
890						nvram_unset(nv);
891				}
892			}
893			else {
894				switch_gen_config(lan, ports, cfg, 0, "*");
895				switch_gen_config(wan, ports, wancfg, 1, "");
896				nvram_set("vlan1ports", lan);
897				nvram_set("vlan2ports", wan);
898				switch_gen_config(lan, ports, cfg, 0, NULL);
899				switch_gen_config(wan, ports, wancfg, 1, NULL);
900				nvram_set("lanports", lan);
901				nvram_set("wanports", wan);
902				nvram_unset("wan1ports");
903			}
904#else
905			switch_gen_config(lan, ports, cfg, 0, "*");
906			switch_gen_config(wan, ports, wancfg, 1, "t");
907			nvram_set("vlan1ports", lan);
908			nvram_set("vlan2ports", wan);
909			switch_gen_config(lan, ports, cfg, 0, NULL);
910			switch_gen_config(wan, ports, wancfg, 1, NULL);
911			nvram_set("lanports", lan);
912			nvram_set("wanports", wan);
913#endif
914			break;
915		}
916
917		case MODEL_RTAC87U:						/* 0  1  2  3  4 */
918		{				/* WAN L1 L2 L3 L4 CPU */	/*vision: WAN L1 L2 L3 L4 */
919			int ports[SWPORT_COUNT] = { 0, 5, 3, 2, 1, 7 };
920#ifdef RTCONFIG_GMAC3
921			if (gmac3_enable)
922				ports[SWPORT_COUNT-1] = 8;
923#endif
924			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
925
926			wan_phyid = ports[0];	// record the phy num of the wan port on the case
927#ifdef RTCONFIG_DUALWAN
928			if (cfg != SWCFG_BRIDGE) {
929				int wan1cfg = nvram_get_int("wans_lanport");
930
931				nvram_unset("vlan2ports");
932				nvram_unset("vlan2hwname");
933				nvram_unset("vlan3ports");
934				nvram_unset("vlan3hwname");
935
936				// The first WAN port.
937				if (get_wans_dualwan()&WANSCAP_WAN) {
938					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
939					nvram_set("vlan2ports", wan);
940					nvram_set("vlan2hwname", "et1");
941				}
942
943				// The second WAN port.
944				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
945					wan1cfg += WAN1PORT1-1;
946					if (wancfg != SWCFG_DEFAULT) {
947						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
948						nvram_set("vlan1ports", lan);
949						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
950						nvram_set("lanports", lan);
951					}
952					else {
953						switch_gen_config(lan, ports, wan1cfg, 0, "*");
954						nvram_set("vlan1ports", lan);
955						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
956						nvram_set("lanports", lan);
957					}
958
959					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
960					if (get_wans_dualwan()&WANSCAP_WAN) {
961						nvram_set("vlan3ports", wan);
962						nvram_set("vlan3hwname", "et1");
963					}
964					else {
965						nvram_set("vlan2ports", wan);
966						nvram_set("vlan2hwname", "et1");
967					}
968				}
969				else {
970					switch_gen_config(lan, ports, cfg, 0, "*");
971					nvram_set("vlan1ports", lan);
972					switch_gen_config(lan, ports, cfg, 0, NULL);
973					nvram_set("lanports", lan);
974				}
975
976				int unit;
977				char prefix[8], nvram_ports[16];
978
979				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
980					memset(prefix, 0, 8);
981					sprintf(prefix, "%d", unit);
982
983					memset(nvram_ports, 0, 16);
984					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
985
986					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
987						switch_gen_config(wan, ports, wancfg, 1, NULL);
988						nvram_set(nvram_ports, wan);
989					}
990					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
991						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
992						nvram_set(nvram_ports, wan);
993					}
994					else
995						nvram_unset(nvram_ports);
996				}
997			}
998			else {
999				switch_gen_config(lan, ports, cfg, 0, "*");
1000				switch_gen_config(wan, ports, wancfg, 1, "");
1001				nvram_set("vlan1ports", lan);
1002				nvram_set("vlan2ports", wan);
1003				switch_gen_config(lan, ports, cfg, 0, NULL);
1004				switch_gen_config(wan, ports, wancfg, 1, NULL);
1005				nvram_set("lanports", lan);
1006				nvram_set("wanports", wan);
1007				nvram_unset("wan1ports");
1008			}
1009#else
1010			switch_gen_config(lan, ports, cfg, 0, "*");
1011			switch_gen_config(wan, ports, wancfg, 1, "u");
1012			nvram_set("vlan1ports", lan);
1013			nvram_set("vlan2ports", wan);
1014			switch_gen_config(lan, ports, cfg, 0, NULL);
1015			switch_gen_config(wan, ports, wancfg, 1, NULL);
1016			nvram_set("lanports", lan);
1017			nvram_set("wanports", wan);
1018#endif
1019			break;
1020		}
1021
1022		case MODEL_RTAC56S:						/* 0  1  2  3  4 */
1023		case MODEL_RTAC56U:
1024		{				/* WAN L1 L2 L3 L4 CPU */	/*vision: L1 L2 L3 L4 WAN  POW*/
1025			const int ports[SWPORT_COUNT] = { 4, 0, 1, 2, 3, 5 };
1026			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
1027
1028			wan_phyid = ports[0];	// record the phy num of the wan port on the case
1029#ifdef RTCONFIG_DUALWAN
1030			if (cfg != SWCFG_BRIDGE) {
1031				int wan1cfg = nvram_get_int("wans_lanport");
1032
1033				nvram_unset("vlan2ports");
1034				nvram_unset("vlan2hwname");
1035				nvram_unset("vlan3ports");
1036				nvram_unset("vlan3hwname");
1037
1038				// The first WAN port.
1039				if (get_wans_dualwan()&WANSCAP_WAN) {
1040					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
1041					nvram_set("vlan2ports", wan);
1042					nvram_set("vlan2hwname", "et0");
1043				}
1044
1045				// The second WAN port.
1046				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
1047					wan1cfg += WAN1PORT1-1;
1048					if (wancfg != SWCFG_DEFAULT) {
1049						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
1050						nvram_set("vlan1ports", lan);
1051						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
1052						nvram_set("lanports", lan);
1053					}
1054					else {
1055						switch_gen_config(lan, ports, wan1cfg, 0, "*");
1056						nvram_set("vlan1ports", lan);
1057						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
1058						nvram_set("lanports", lan);
1059					}
1060
1061					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
1062					if (get_wans_dualwan()&WANSCAP_WAN) {
1063						nvram_set("vlan3ports", wan);
1064						nvram_set("vlan3hwname", "et0");
1065					}
1066					else {
1067						nvram_set("vlan2ports", wan);
1068						nvram_set("vlan2hwname", "et0");
1069					}
1070				}
1071				else {
1072					switch_gen_config(lan, ports, cfg, 0, "*");
1073					nvram_set("vlan1ports", lan);
1074					switch_gen_config(lan, ports, cfg, 0, NULL);
1075					nvram_set("lanports", lan);
1076				}
1077
1078				int unit;
1079				char prefix[8], nvram_ports[16];
1080
1081				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
1082					memset(prefix, 0, 8);
1083					sprintf(prefix, "%d", unit);
1084
1085					memset(nvram_ports, 0, 16);
1086					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
1087
1088					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
1089						switch_gen_config(wan, ports, wancfg, 1, NULL);
1090						nvram_set(nvram_ports, wan);
1091					}
1092					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
1093						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
1094						nvram_set(nvram_ports, wan);
1095					}
1096					else
1097						nvram_unset(nvram_ports);
1098				}
1099			}
1100			else {
1101				switch_gen_config(lan, ports, cfg, 0, "*");
1102				switch_gen_config(wan, ports, wancfg, 1, "");
1103				nvram_set("vlan1ports", lan);
1104				nvram_set("vlan2ports", wan);
1105				switch_gen_config(lan, ports, cfg, 0, NULL);
1106				switch_gen_config(wan, ports, wancfg, 1, NULL);
1107				nvram_set("lanports", lan);
1108				nvram_set("wanports", wan);
1109				nvram_unset("wan1ports");
1110			}
1111#else
1112			switch_gen_config(lan, ports, cfg, 0, "*");
1113			switch_gen_config(wan, ports, wancfg, 1, "u");
1114			nvram_set("vlan1ports", lan);
1115			nvram_set("vlan2ports", wan);
1116			switch_gen_config(lan, ports, cfg, 0, NULL);
1117			switch_gen_config(wan, ports, wancfg, 1, NULL);
1118			nvram_set("lanports", lan);
1119			nvram_set("wanports", wan);
1120#endif
1121			break;
1122		}
1123
1124		case MODEL_RTAC5300:
1125		case MODEL_RTAC5300R:
1126		{
1127			char *hw_name = "et1";
1128
1129#ifdef RTCONFIG_EXT_RTL8365MB
1130								/*vision:    (L5 L6 L7 L8)*/
1131			/* WAN L1 L2 L3 L4 (L5 L6 L7 L8) CPU */	/*vision: WAN L1 L2 L3 L4 */
1132			int ports[SWPORT_COUNT] = { 0, 1, 2, 3, 4, 5, 7 };
1133#else	// RTCONFIG_EXT_RTL8365MB
1134			/* WAN L1 L2 L3 L4 CPU */		/*vision: WAN L1 L2 L3 L4 */
1135			int ports[SWPORT_COUNT] = { 0, 1, 2, 3, 4, 7 };
1136#endif
1137			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
1138			wan_phyid = ports[0];	// record the phy num of the wan port on the case
1139#ifdef RTCONFIG_GMAC3
1140			if (gmac3_enable) {
1141				ports[SWPORT_COUNT-1] = 8;
1142				hw_name = "et2";
1143			}
1144#endif
1145
1146#ifdef RTCONFIG_DUALWAN
1147			if (get_wans_dualwan()&WANSCAP_WAN && get_wans_dualwan()&WANSCAP_LAN)
1148				nvram_set("vlan3hwname", hw_name);
1149			else
1150				nvram_unset("vlan3hwname");
1151			if (get_wans_dualwan()&WANSCAP_WAN || get_wans_dualwan()&WANSCAP_LAN)
1152				nvram_set("vlan2hwname", hw_name);
1153			nvram_set("vlan1hwname", hw_name);
1154
1155			if (cfg != SWCFG_BRIDGE) {
1156				int wan1cfg = nvram_get_int("wans_lanport");
1157
1158				nvram_unset("vlan2ports");
1159				nvram_unset("vlan3ports");
1160
1161				/* The first WAN port. */
1162				if (get_wans_dualwan()&WANSCAP_WAN) {
1163					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
1164					nvram_set("vlan2ports", wan);
1165				}
1166
1167				/* The second WAN port. */
1168				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
1169					wan1cfg += WAN1PORT1-1;
1170					if (wancfg != SWCFG_DEFAULT) {
1171						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
1172						nvram_set("vlan1ports", lan);
1173						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
1174						nvram_set("lanports", lan);
1175					}
1176					else {
1177						switch_gen_config(lan, ports, wan1cfg, 0, "*");
1178						nvram_set("vlan1ports", lan);
1179						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
1180						nvram_set("lanports", lan);
1181					}
1182
1183					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
1184					if (get_wans_dualwan()&WANSCAP_WAN)
1185						nvram_set("vlan3ports", wan);
1186					else
1187						nvram_set("vlan2ports", wan);
1188				}
1189				else {
1190					switch_gen_config(lan, ports, cfg, 0, "*");
1191					nvram_set("vlan1ports", lan);
1192					switch_gen_config(lan, ports, cfg, 0, NULL);
1193					nvram_set("lanports", lan);
1194				}
1195
1196				int unit;
1197				char prefix[8], nvram_ports[16];
1198
1199				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
1200					memset(prefix, 0, 8);
1201					sprintf(prefix, "%d", unit);
1202
1203					memset(nvram_ports, 0, 16);
1204					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
1205
1206					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
1207						switch_gen_config(wan, ports, wancfg, 1, NULL);
1208						nvram_set(nvram_ports, wan);
1209					}
1210					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
1211						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
1212						nvram_set(nvram_ports, wan);
1213					}
1214					else
1215						nvram_unset(nvram_ports);
1216				}
1217			}
1218			else {
1219				switch_gen_config(lan, ports, cfg, 0, "*");
1220				switch_gen_config(wan, ports, wancfg, 1, "u");
1221				nvram_set("vlan1ports", lan);
1222				nvram_set("vlan2ports", wan);
1223				switch_gen_config(lan, ports, cfg, 0, NULL);
1224				switch_gen_config(wan, ports, wancfg, 1, NULL);
1225				nvram_set("lanports", lan);
1226				nvram_set("wanports", wan);
1227				nvram_unset("wan1ports");
1228			}
1229#else	// RTCONFIG_DUALWAN
1230			switch_gen_config(lan, ports, cfg, 0, "*");
1231			switch_gen_config(wan, ports, wancfg, 1, "u");
1232			nvram_set("vlan1ports", lan);
1233			nvram_set("vlan1hwname", hw_name);
1234			nvram_set("vlan2ports", wan);
1235			nvram_set("vlan2hwname", hw_name);
1236			switch_gen_config(lan, ports, cfg, 0, NULL);
1237			switch_gen_config(wan, ports, wancfg, 1, NULL);
1238			nvram_set("lanports", lan);
1239			nvram_set("wanports", wan);
1240#endif
1241			break;
1242		}
1243
1244		case MODEL_RTAC3100:
1245		case MODEL_RTAC88U:
1246		{
1247			char *hw_name = "et0";
1248
1249#ifdef RTCONFIG_EXT_RTL8365MB
1250			/* WAN L1 L2 L3 L4 (L5 L6 L7 L8) CPU */	/*vision: (L8 L7 L6 L5) L4 L3 L2 L1 WAN*/
1251			int ports[SWPORT_COUNT] = { 4, 3, 2, 1, 0, 5, 7 };
1252			hw_name = "et1";
1253#else	// RTCONFIG_EXT_RTL8365MB
1254			/* WAN L1 L2 L3 L4 CPU */	/*vision: WAN L1 L2 L3 L4 */
1255			int ports[SWPORT_COUNT] = { 4, 3, 2, 1, 0, 5 };
1256			hw_name = "et0";
1257#endif
1258			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
1259			wan_phyid = ports[0];	// record the phy num of the wan port on the case
1260#ifdef RTCONFIG_GMAC3
1261			if (gmac3_enable) {
1262				ports[SWPORT_COUNT-1] = 8;
1263				hw_name = "et2";
1264			}
1265#endif
1266
1267#ifdef RTCONFIG_DUALWAN
1268			if (get_wans_dualwan()&WANSCAP_WAN && get_wans_dualwan()&WANSCAP_LAN)
1269				nvram_set("vlan3hwname", hw_name);
1270			else
1271				nvram_unset("vlan3hwname");
1272			if (get_wans_dualwan()&WANSCAP_WAN || get_wans_dualwan()&WANSCAP_LAN)
1273				nvram_set("vlan2hwname", hw_name);
1274			nvram_set("vlan1hwname", hw_name);
1275
1276			if (cfg != SWCFG_BRIDGE) {
1277				int wan1cfg = nvram_get_int("wans_lanport");
1278
1279				nvram_unset("vlan2ports");
1280				nvram_unset("vlan3ports");
1281
1282				/* The first WAN port. */
1283				if (get_wans_dualwan()&WANSCAP_WAN) {
1284					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
1285					nvram_set("vlan2ports", wan);
1286				}
1287
1288				/* The second WAN port. */
1289				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
1290					wan1cfg += WAN1PORT1-1;
1291					if (wancfg != SWCFG_DEFAULT) {
1292						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
1293						nvram_set("vlan1ports", lan);
1294						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
1295						nvram_set("lanports", lan);
1296					}
1297					else {
1298						switch_gen_config(lan, ports, wan1cfg, 0, "*");
1299						nvram_set("vlan1ports", lan);
1300						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
1301						nvram_set("lanports", lan);
1302					}
1303
1304					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
1305					if (get_wans_dualwan()&WANSCAP_WAN)
1306						nvram_set("vlan3ports", wan);
1307					else
1308						nvram_set("vlan2ports", wan);
1309				}
1310				else {
1311					switch_gen_config(lan, ports, cfg, 0, "*");
1312					nvram_set("vlan1ports", lan);
1313					switch_gen_config(lan, ports, cfg, 0, NULL);
1314					nvram_set("lanports", lan);
1315				}
1316
1317				int unit;
1318				char prefix[8], nvram_ports[16];
1319
1320				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
1321					memset(prefix, 0, 8);
1322					sprintf(prefix, "%d", unit);
1323
1324					memset(nvram_ports, 0, 16);
1325					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
1326
1327					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
1328						switch_gen_config(wan, ports, wancfg, 1, NULL);
1329						nvram_set(nvram_ports, wan);
1330					}
1331					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
1332						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
1333						nvram_set(nvram_ports, wan);
1334					}
1335					else
1336						nvram_unset(nvram_ports);
1337				}
1338			}
1339			else {
1340				switch_gen_config(lan, ports, cfg, 0, "*");
1341				switch_gen_config(wan, ports, wancfg, 1, "u");
1342				nvram_set("vlan1ports", lan);
1343				nvram_set("vlan2ports", wan);
1344				switch_gen_config(lan, ports, cfg, 0, NULL);
1345				switch_gen_config(wan, ports, wancfg, 1, NULL);
1346				nvram_set("lanports", lan);
1347				nvram_set("wanports", wan);
1348				nvram_unset("wan1ports");
1349			}
1350#else	// RTCONFIG_DUALWAN
1351			switch_gen_config(lan, ports, cfg, 0, "*");
1352			switch_gen_config(wan, ports, wancfg, 1, "u");
1353			nvram_set("vlan1ports", lan);
1354			nvram_set("vlan1hwname", hw_name);
1355			nvram_set("vlan2ports", wan);
1356			nvram_set("vlan2hwname", hw_name);
1357			switch_gen_config(lan, ports, cfg, 0, NULL);
1358			switch_gen_config(wan, ports, wancfg, 1, NULL);
1359			nvram_set("lanports", lan);
1360			nvram_set("wanports", wan);
1361#endif
1362			break;
1363		}
1364
1365		case MODEL_RTN66U:
1366		case MODEL_RTAC66U:
1367		case MODEL_RTAC1200G:
1368		case MODEL_RTAC1200GP:
1369		{				/* WAN L1 L2 L3 L4 CPU */
1370			const int ports[SWPORT_COUNT] = { 0, 1, 2, 3, 4, 8 };
1371			int wancfg = (!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")) ? SWCFG_DEFAULT : cfg;
1372
1373			wan_phyid = ports[0];	// record the phy num of the wan port on the case
1374#ifdef RTCONFIG_DUALWAN
1375			if (cfg != SWCFG_BRIDGE) {
1376				int wan1cfg = nvram_get_int("wans_lanport");
1377
1378				nvram_unset("vlan2ports");
1379				nvram_unset("vlan2hwname");
1380				nvram_unset("vlan3ports");
1381				nvram_unset("vlan3hwname");
1382
1383				// The first WAN port.
1384				if (get_wans_dualwan()&WANSCAP_WAN) {
1385					switch_gen_config(wan, ports, wancfg, 1, (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4)?"":"u");
1386					nvram_set("vlan2ports", wan);
1387					nvram_set("vlan2hwname", "et0");
1388				}
1389
1390				// The second WAN port.
1391				if (get_wans_dualwan()&WANSCAP_LAN && wan1cfg >= 1 && wan1cfg <= 4) {
1392					wan1cfg += WAN1PORT1-1;
1393					if (wancfg != SWCFG_DEFAULT) {
1394						gen_lan_ports(lan, ports, wancfg, wan1cfg, "*");
1395						nvram_set("vlan1ports", lan);
1396						gen_lan_ports(lan, ports, wancfg, wan1cfg, NULL);
1397						nvram_set("lanports", lan);
1398					}
1399					else {
1400						switch_gen_config(lan, ports, wan1cfg, 0, "*");
1401						nvram_set("vlan1ports", lan);
1402						switch_gen_config(lan, ports, wan1cfg, 0, NULL);
1403						nvram_set("lanports", lan);
1404					}
1405
1406					switch_gen_config(wan, ports, wan1cfg, 1, (get_wans_dualwan()&WANSCAP_WAN)?"":"u");
1407					if (get_wans_dualwan()&WANSCAP_WAN) {
1408						nvram_set("vlan3ports", wan);
1409						nvram_set("vlan3hwname", "et0");
1410					}
1411					else {
1412						nvram_set("vlan2ports", wan);
1413						nvram_set("vlan2hwname", "et0");
1414					}
1415				}
1416				else {
1417					switch_gen_config(lan, ports, cfg, 0, "*");
1418					nvram_set("vlan1ports", lan);
1419					switch_gen_config(lan, ports, cfg, 0, NULL);
1420					nvram_set("lanports", lan);
1421				}
1422
1423				int unit;
1424				char prefix[8], nvram_ports[16];
1425
1426				for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
1427					memset(prefix, 0, 8);
1428					sprintf(prefix, "%d", unit);
1429
1430					memset(nvram_ports, 0, 16);
1431					sprintf(nvram_ports, "wan%sports", (unit == WAN_UNIT_FIRST)?"":prefix);
1432
1433					if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
1434						switch_gen_config(wan, ports, wancfg, 1, NULL);
1435						nvram_set(nvram_ports, wan);
1436					}
1437					else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
1438						switch_gen_config(wan, ports, wan1cfg, 1, NULL);
1439						nvram_set(nvram_ports, wan);
1440					}
1441					else
1442						nvram_unset(nvram_ports);
1443				}
1444			}
1445			else {
1446				switch_gen_config(lan, ports, cfg, 0, "*");
1447				switch_gen_config(wan, ports, wancfg, 1, "");
1448				nvram_set("vlan1ports", lan);
1449				nvram_set("vlan2ports", wan);
1450				switch_gen_config(lan, ports, cfg, 0, NULL);
1451				switch_gen_config(wan, ports, wancfg, 1, NULL);
1452				nvram_set("lanports", lan);
1453				nvram_set("wanports", wan);
1454				nvram_unset("wan1ports");
1455			}
1456#else
1457			switch_gen_config(lan, ports, cfg, 0, "*");
1458			switch_gen_config(wan, ports, wancfg, 1, "u");
1459			nvram_set("vlan1ports", lan);
1460			nvram_set("vlan2ports", wan);
1461			switch_gen_config(lan, ports, cfg, 0, NULL);
1462			switch_gen_config(wan, ports, wancfg, 1, NULL);
1463			nvram_set("lanports", lan);
1464			nvram_set("wanports", wan);
1465#endif
1466			break;
1467		}
1468	}
1469
1470#ifdef RTCONFIG_GMAC3
1471	/* gmac3 override */
1472	if (nvram_get_int("gmac3_enable") == 1) {
1473		lists = nvram_safe_get("vlan1ports");
1474		strncpy(glan, lists, strlen(lists));
1475
1476		foreach(var, lists, next) {
1477			if (strchr(var, '*') || strchr(var, 'u')) {
1478				remove_from_list(var, glan, sizeof(glan));
1479				break;
1480			}
1481		}
1482		/* add port 5, 7 and 8* */
1483		add_to_list("5", glan, sizeof(glan));
1484		add_to_list("7", glan, sizeof(glan));
1485		add_to_list("8*", glan, sizeof(glan));
1486		nvram_set("vlan1ports", glan);
1487	}
1488#endif
1489}
1490
1491void enable_jumbo_frame(void)
1492{
1493	int enable = nvram_get_int("jumbo_frame_enable");
1494
1495	if (!nvram_contains_word("rc_support", "switchctrl"))
1496		return;
1497
1498	switch (get_switch()) {
1499	case SWITCH_BCM53115:
1500	case SWITCH_BCM53125:
1501		eval("et", "robowr", "0x40", "0x01", enable ? "0x1f" : "0x00");
1502		break;
1503	case SWITCH_BCM5301x:
1504#ifdef RTCONFIG_BCMARM
1505		eval("et", "-i", "eth0", "robowr", "0x40", "0x01", enable ? "0x010001ff" : "0x00", "4");
1506#else
1507		eval("et", "robowr", "0x40", "0x01", enable ? "0x010001ff" : "0x00");
1508#endif
1509		break;
1510	}
1511}
1512
1513void ether_led()
1514{
1515	int model;
1516
1517	model = get_model();
1518	switch(model) {
1519//	case MODEL_RTAC68U:
1520	/* refer to 5301x datasheet page 2770 */
1521	case MODEL_RTAC56S:
1522	case MODEL_RTAC56U:
1523		eval("et", "robowr", "0x00", "0x10", "0x3000");
1524		break;
1525	case MODEL_RTN16:
1526		eval("et", "robowr", "0", "0x18", "0x01ff");
1527		eval("et", "robowr", "0", "0x1a", "0x01ff");
1528		break;
1529	case MODEL_RTAC1200G:
1530	case MODEL_RTAC1200GP:
1531		eval("et", "robowr", "0", "0x12", "0x24");
1532		break;
1533	}
1534}
1535
1536void init_switch()
1537{
1538	generate_switch_para();
1539
1540#if defined(RTCONFIG_EXT_RTL8365MB) || defined(RTCONFIG_EXT_RTL8370MB)
1541	eval("mknod", "/dev/rtkswitch", "c", "233", "0");
1542	modprobe("rtl8365mb");
1543#ifdef RTCONFIG_RESET_SWITCH
1544	int i, r, gpio_nr = atoi(nvram_safe_get("reset_switch_gpio"));
1545	led_control(LED_RESET_SWITCH, 0);
1546	usleep(400 * 1000);
1547
1548	for (i=0; i<10; ++i) {
1549		led_control(LED_RESET_SWITCH, 1);
1550		if ((r = get_gpio(gpio_nr)) != 1) {
1551			_dprintf("\n! reset LED_RESET_SWITCH failed:%d, reset again !\n", r);
1552			usleep(10 * 1000);
1553		} else {
1554			_dprintf("\nchk LED_RESET_SWITCH:%d\n", r);
1555			break;
1556		}
1557	}
1558#endif
1559#endif
1560
1561#ifdef CONFIG_BCMWL5
1562	// ctf should be disabled when some functions are enabled
1563	if ((nvram_get_int("qos_enable") == 1 && nvram_get_int("qos_type") == 0) || nvram_get_int("ctf_disable_force")
1564#ifndef RTCONFIG_BCMARM
1565	|| nvram_get_int("sw_mode") == SW_MODE_REPEATER
1566#endif
1567//#ifdef RTCONFIG_USB_MODEM
1568//	|| nvram_get_int("ctf_disable_modem")
1569//#endif
1570	) {
1571		nvram_set("ctf_disable", "1");
1572//		nvram_set("pktc_disable", "1");
1573	}
1574#ifdef RTCONFIG_BWDPI
1575	else if (check_bwdpi_nvram_setting() && nvram_get_int("sw_mode") == SW_MODE_ROUTER) {
1576		nvram_set("ctf_disable", "0");
1577	}
1578#endif
1579	else {
1580		nvram_set("ctf_disable", "0");
1581//		nvram_set("pktc_disable", "0");
1582	}
1583#ifdef RTCONFIG_BCMFA
1584	fa_nvram_adjust();
1585#endif
1586
1587/* Requires bridge netfilter, but slows down and breaks EMF/IGS IGMP IPTV Snooping
1588	if (nvram_get_int("sw_mode") == SW_MODE_ROUTER && nvram_get_int("qos_enable") == 1) {
1589		// enable netfilter bridge only when phydev is used
1590		f_write_string("/proc/sys/net/bridge/bridge-nf-call-iptables", "1", 0, 0);
1591		f_write_string("/proc/sys/net/bridge/bridge-nf-call-ip6tables", "1", 0, 0);
1592		f_write_string("/proc/sys/net/bridge/bridge-nf-filter-vlan-tagged", "1", 0, 0);
1593	}
1594*/
1595#endif
1596
1597#ifdef RTCONFIG_SHP
1598	if (nvram_get_int("qos_enable") == 1 || nvram_get_int("lfp_disable_force")) {
1599		nvram_set("lfp_disable", "1");
1600	} else {
1601		nvram_set("lfp_disable", "0");
1602	}
1603
1604	if (nvram_get_int("lfp_disable") == 0) {
1605		restart_lfp();
1606	}
1607#endif
1608
1609#if defined(RTCONFIG_BCMFA) && !defined(RTCONFIG_BCM_7114)
1610	if (!nvram_get("ctf_fa_cap")) {
1611		char ctf_fa_mode_bak[2];
1612		int nvram_ctf_fa_mode = 0;
1613
1614		if (nvram_get("ctf_fa_mode"))
1615		{
1616			nvram_ctf_fa_mode = 1;
1617			strcpy(ctf_fa_mode_bak, nvram_safe_get("ctf_fa_mode"));
1618		}
1619
1620		nvram_set_int("ctf_fa_mode", 2);
1621		modprobe("et");
1622		FILE *fp;
1623		if ((fp = fopen("/proc/fa", "r"))) {
1624			/* FA is capable */
1625			fclose(fp);
1626			nvram_set_int("ctf_fa_cap", 1);
1627		} else nvram_set_int("ctf_fa_cap", 0);
1628
1629		if (nvram_ctf_fa_mode)
1630			nvram_set("ctf_fa_mode", ctf_fa_mode_bak);
1631		else
1632			nvram_unset("ctf_fa_mode");
1633		nvram_commit();
1634
1635		modprobe_r("et");
1636	}
1637#endif
1638
1639	// ctf must be loaded prior to any other modules
1640	if (nvram_get_int("ctf_disable") == 0)
1641		modprobe("ctf");
1642
1643	modprobe("et");
1644	modprobe("bcm57xx");
1645	enable_jumbo_frame();
1646	ether_led();
1647
1648#ifdef RTCONFIG_DSL
1649	init_switch_dsl();
1650	config_switch_dsl();
1651#endif
1652#ifdef RTCONFIG_EXT_RTL8365MB
1653	/* link up rtkswitch after bcm rgmii init */
1654	eval("rtkswitch", "1");
1655#endif
1656#ifdef RTCONFIG_EXT_RTL8370MB
1657	/* link up rtkswitch after bcm rgmii init */
1658	eval("rtkswitch", "0");
1659#endif
1660#ifdef RTCONFIG_LACP
1661	config_lacp();
1662#endif
1663
1664}
1665
1666char *get_lan_hwaddr(void)
1667{
1668#ifdef RTCONFIG_BCMARM
1669	switch(get_model()) {
1670		case MODEL_RTAC87U:
1671		case MODEL_RTAC5300:
1672		case MODEL_RTAC5300R:
1673		case MODEL_RTAC88U:
1674			return cfe_nvram_safe_get("et1macaddr");
1675		default:
1676			return cfe_nvram_safe_get("et0macaddr");
1677	}
1678#endif
1679	return nvram_safe_get("et0macaddr");
1680}
1681
1682int
1683switch_exist(void)
1684{
1685	int ret = 1;
1686
1687	if (get_switch() == SWITCH_UNKNOWN) {
1688		_dprintf("No switch interface!!!\n");
1689		ret = 0;
1690	}
1691
1692	return ret;
1693}
1694
1695void config_switch(void)
1696{
1697	generate_switch_para();
1698
1699}
1700
1701#ifdef RTCONFIG_BCMWL6
1702extern struct nvram_tuple bcm4360ac_defaults[];
1703
1704static void
1705set_bcm4360ac_vars(void)
1706{
1707	struct nvram_tuple *t;
1708
1709	/* Restore defaults */
1710	for (t = bcm4360ac_defaults; t->name; t++) {
1711		if (!nvram_get(t->name))
1712			nvram_set(t->name, t->value);
1713	}
1714}
1715#endif
1716
1717static void
1718reset_mssid_hwaddr(int unit)
1719{
1720	char word[256], *next;
1721	char macaddr_str[18], macbuf[13];
1722	char *macaddr_strp;
1723	unsigned char mac_binary[6];
1724	unsigned long long macvalue;
1725	unsigned char *macp;
1726	int model = get_model();
1727	int unit_total = 0, idx, subunit;
1728	int max_mssid = num_of_mssid_support(unit);
1729	char tmp[100], prefix[]="wlXXXXXXX_";
1730
1731	foreach(word, nvram_safe_get("wl_ifnames"), next)
1732		unit_total++;
1733
1734	if (unit > (unit_total - 1))
1735		return;
1736
1737	for (idx = 0; idx < unit_total ; idx++) {
1738		memset(mac_binary, 0x0, 6);
1739		memset(macbuf, 0x0, 13);
1740
1741		switch(model) {
1742			case MODEL_RTN53:
1743			case MODEL_RTN16:
1744			case MODEL_RTN15U:
1745			case MODEL_RTN12:
1746			case MODEL_RTN12B1:
1747			case MODEL_RTN12C1:
1748			case MODEL_RTN12D1:
1749			case MODEL_RTN12VP:
1750			case MODEL_RTN12HP:
1751			case MODEL_RTN12HP_B1:
1752			case MODEL_APN12HP:
1753			case MODEL_RTN14UHP:
1754			case MODEL_RTN10U:
1755			case MODEL_RTN10P:
1756			case MODEL_RTN10D1:
1757			case MODEL_RTN10PV2:
1758			case MODEL_RTAC53U:
1759				if (unit == 0)	/* 2.4G */
1760					snprintf(macaddr_str, sizeof(macaddr_str), "sb/1/macaddr");
1761				else		/* 5G */
1762					snprintf(macaddr_str, sizeof(macaddr_str), "0:macaddr");
1763				break;
1764			case MODEL_RTN66U:
1765			case MODEL_RTAC66U:
1766				snprintf(macaddr_str, sizeof(macaddr_str), "pci/%d/1/macaddr", unit + 1);
1767				break;
1768			case MODEL_RTN18U:
1769			case MODEL_RPAC68U:
1770			case MODEL_RTAC68U:
1771			case MODEL_RTAC3200:
1772			case MODEL_DSLAC68U:
1773			case MODEL_RTAC87U:
1774			case MODEL_RTAC56S:
1775			case MODEL_RTAC56U:
1776			case MODEL_RTAC5300:
1777			case MODEL_RTAC5300R:
1778			case MODEL_RTAC88U:
1779			case MODEL_RTAC3100:
1780#ifdef RTAC3200
1781				if (unit < 2)
1782					snprintf(macaddr_str, sizeof(macaddr_str), "%d:macaddr", 1 - unit);
1783				else
1784#endif
1785				snprintf(macaddr_str, sizeof(macaddr_str), "%d:macaddr", unit);
1786				break;
1787			case MODEL_RTAC1200G:
1788			case MODEL_RTAC1200GP:
1789				if (unit == 0) 	/* 2.4G */
1790					snprintf(macaddr_str, sizeof(macaddr_str), "0:macaddr");
1791				else		/* 5G */
1792					snprintf(macaddr_str, sizeof(macaddr_str), "sb/1/macaddr");
1793				break;
1794			default:
1795#ifdef RTCONFIG_BCMARM
1796				snprintf(macaddr_str, sizeof(macaddr_str), "%d:macaddr", unit);
1797#else
1798				snprintf(macaddr_str, sizeof(macaddr_str), "pci/%d/1/macaddr", unit + 1);
1799#endif
1800				break;
1801		}
1802
1803		macaddr_strp = cfe_nvram_get(macaddr_str);
1804		if (macaddr_strp)
1805		{
1806			if (!mssid_mac_validate(macaddr_strp))
1807				return;
1808
1809			if (idx != unit)
1810				continue;
1811
1812			ether_atoe(macaddr_strp, mac_binary);
1813			sprintf(macbuf, "%02X%02X%02X%02X%02X%02X",
1814					mac_binary[0],
1815					mac_binary[1],
1816					mac_binary[2],
1817					mac_binary[3],
1818					mac_binary[4],
1819					mac_binary[5]);
1820			macvalue = strtoll(macbuf, (char **) NULL, 16);
1821
1822			/* including primary ssid */
1823			for (subunit = 1; subunit < max_mssid + 1 ; subunit++)
1824			{
1825				macvalue++;
1826
1827				macp = (unsigned char*) &macvalue;
1828				memset(macaddr_str, 0, sizeof(macaddr_str));
1829				sprintf(macaddr_str, "%02X:%02X:%02X:%02X:%02X:%02X", *(macp+5), *(macp+4), *(macp+3), *(macp+2), *(macp+1), *(macp+0));
1830				snprintf(prefix, sizeof(prefix), "wl%d.%d_", unit, subunit);
1831				nvram_set(strcat_r(prefix, "hwaddr", tmp), macaddr_str);
1832			}
1833		} else return;
1834	}
1835}
1836
1837#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_BCMARM) && defined(RTCONFIG_PROXYSTA)
1838void
1839reset_psr_hwaddr()
1840{
1841	char macaddr_name[10], macaddr_str[18], macbuf[13];
1842	char *macaddr_p;
1843	unsigned char mac_binary[6];
1844	unsigned long long macvalue;
1845	unsigned char *macp;
1846	int model = get_model();
1847	int unit = 0;
1848
1849	if (!(is_psr(nvram_get_int("wlc_band")) && !nvram_get_int("wlc_band")))
1850		return;
1851
1852	memset(mac_binary, 0x0, 6);
1853	memset(macbuf, 0x0, 13);
1854
1855	switch(model) {
1856		case MODEL_RTAC3200:
1857			unit = 1;
1858			break;
1859	}
1860
1861	snprintf(macaddr_name, sizeof(macaddr_name), "%d:macaddr", unit);
1862
1863	macaddr_p = cfe_nvram_get(macaddr_name);
1864	if (macaddr_p)
1865	{
1866		ether_atoe(macaddr_p, mac_binary);
1867		sprintf(macbuf, "%02X%02X%02X%02X%02X%02X",
1868				mac_binary[0],
1869				mac_binary[1],
1870				mac_binary[2],
1871				mac_binary[3],
1872				mac_binary[4],
1873				mac_binary[5]);
1874		macvalue = strtoll(macbuf, (char **) NULL, 16);
1875		macvalue++;
1876
1877		macp = (unsigned char*) &macvalue;
1878		memset(macaddr_str, 0, sizeof(macaddr_str));
1879		sprintf(macaddr_str, "%02X:%02X:%02X:%02X:%02X:%02X", *(macp+5), *(macp+4), *(macp+3), *(macp+2), *(macp+1), *(macp+0));
1880		nvram_set(macaddr_name, macaddr_str);
1881	}
1882}
1883#endif
1884
1885#if defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)
1886static int
1887net_dev_exist(const char *ifname)
1888{
1889	DIR *dir_to_open = NULL;
1890	char tmpstr[128];
1891
1892	if (ifname == NULL) return 0;
1893
1894	sprintf(tmpstr, "/sys/class/net/%s", ifname);
1895	dir_to_open = opendir(tmpstr);
1896	if (dir_to_open)
1897	{
1898		closedir(dir_to_open);
1899		return 1;
1900	}
1901		return 0;
1902}
1903
1904static int first_load = 1;
1905
1906void load_wl_war()
1907{
1908	first_load = 0;
1909
1910	eval("insmod", "wl");
1911
1912	while (!net_dev_exist("eth1"))
1913		sleep(1);
1914
1915	ifconfig("eth1", IFUP, NULL, NULL);
1916	ifconfig("eth2", IFUP, NULL, NULL);
1917	ifconfig("eth3", IFUP, NULL, NULL);
1918
1919	eval("rmmod", "wl");
1920}
1921
1922void load_wl()
1923{
1924	char module[80], *modules, *next;
1925#ifdef RTCONFIG_NOWL
1926#ifdef RTCONFIG_DPSTA
1927	modules = "dpsta dhd";
1928#else
1929	modules = "dhd";
1930#endif
1931#else	// NOWL
1932#ifdef RTCONFIG_DPSTA
1933#ifdef RTCONFIG_DHDAP
1934	modules = "dpsta dhd wl";
1935#else
1936	modules = "dpsta wl";
1937#endif
1938#else	// DPSTA
1939#ifdef RTCONFIG_DHDAP
1940	modules = "dhd wl";
1941#else
1942	modules = "wl";
1943#endif
1944#endif
1945#endif	// NOWL
1946	int i = 0, maxwl_eth = 0, maxunit = -1;
1947	int unit = -1;
1948	char ifname[16] = {0};
1949	char instance_base[128];
1950#ifndef RTCONFIG_NOWL
1951#ifndef RTCONFIG_DPSTA
1952	if (first_load) load_wl_war();
1953#endif
1954#endif
1955	foreach(module, modules, next) {
1956		if ((strcmp(module, "dhd") == 0) || (strcmp(module, "wl") == 0)) {
1957			/* Search for existing wl devices and the max unit number used */
1958			for (i = 1; i <= DEV_NUMIFS; i++) {
1959			snprintf(ifname, sizeof(ifname), "eth%d", i);
1960				if (!wl_probe(ifname)) {
1961					if (!wl_ioctl(ifname, WLC_GET_INSTANCE, &unit,
1962					sizeof(unit))) {
1963						maxwl_eth = i;
1964						maxunit = (unit > maxunit) ? unit : maxunit;
1965					}
1966				}
1967			}
1968			snprintf(instance_base, sizeof(instance_base), "instance_base=%d", maxunit + 1);
1969#if defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)
1970			if (strcmp(module, "dhd") == 0)
1971			snprintf(instance_base, sizeof(instance_base), "%s dhd_msg_level=%d", instance_base, nvram_get_int("dhd_msg_level"));
1972#endif
1973			eval("insmod", module, instance_base);
1974		}
1975		else {
1976			eval("insmod", module);
1977#ifndef RTCONFIG_NOWL
1978#ifdef RTCONFIG_DPSTA
1979			if (first_load) load_wl_war();
1980#endif
1981#endif
1982		}
1983	}
1984#ifdef WLCLMLOAD
1985	//if (strcmp(module, "wl") == 0) {
1986		download_clmblob_files();
1987	//}
1988#endif /* WLCLMLOAD */
1989}
1990#endif
1991
1992void init_wl(void)
1993{
1994#ifdef RTCONFIG_EMF
1995	modprobe("emf");
1996	modprobe("igs");
1997#endif
1998#ifdef RTCONFIG_BCMWL6
1999	switch(get_model()) {
2000		case MODEL_DSLAC68U:
2001		case MODEL_RTAC1200G:
2002		case MODEL_RTAC1200GP:
2003		case MODEL_RTAC3100:
2004		case MODEL_RTAC3200:
2005		case MODEL_RTAC5300:
2006		case MODEL_RTAC5300R:
2007		case MODEL_RTAC66U:
2008		case MODEL_RTAC68U:
2009		case MODEL_RTAC88U:
2010			set_bcm4360ac_vars();
2011			break;
2012	}
2013#endif
2014	check_wl_country();
2015#if defined(RTAC3200) || defined(RTAC68U) || defined(RTAC5300) || defined(RTAC5300R) || defined(RTAC88U) || defined(RTAC3100)
2016	wl_disband5grp();
2017#endif
2018	set_wltxpower();
2019#if defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)
2020	load_wl();
2021#else
2022#ifdef RTCONFIG_ALLNOWL
2023	if (!nvram_match("nowl", "1"))
2024#endif
2025	modprobe("wl");
2026#endif
2027#ifndef RTCONFIG_BCMARM
2028#if defined(NAS_GTK_PER_STA) && defined(PROXYARP)
2029	modprobe("proxyarp");
2030#endif
2031#endif
2032
2033#ifdef RTCONFIG_BRCM_USBAP
2034	/* We have to load USB modules after loading PCI wl driver so
2035	 * USB driver can decide its instance number based on PCI wl
2036	 * instance numbers (in hotplug_usb())
2037	 */
2038	modprobe("usbcore");
2039
2040#ifdef LINUX26
2041	mount("usbfs", "/proc/bus/usb", "usbfs", MS_MGC_VAL, NULL);
2042#else
2043	mount("usbdevfs", "/proc/bus/usb", "usbdevfs", MS_MGC_VAL, NULL);
2044#endif /* LINUX26 */
2045
2046	{
2047		char	insmod_arg[128];
2048		int	i = 0, maxwl_eth = 0, maxunit = -1;
2049		char	ifname[16] = {0};
2050		int	unit = -1;
2051		char arg1[20] = {0};
2052		char arg2[20] = {0};
2053		char arg3[20] = {0};
2054		char arg4[20] = {0};
2055		char arg5[20] = {0};
2056		char arg6[20] = {0};
2057		char arg7[20] = {0};
2058		const int wl_wait = 3;	/* max wait time for wl_high to up */
2059
2060		/* Save QTD cache params in nvram */
2061		sprintf(arg1, "log2_irq_thresh=%d", nvram_get_int("ehciirqt"));
2062		sprintf(arg2, "qtdc_pid=%d", nvram_get_int("qtdc_pid"));
2063		sprintf(arg3, "qtdc_vid=%d", nvram_get_int("qtdc_vid"));
2064		sprintf(arg4, "qtdc0_ep=%d", nvram_get_int("qtdc0_ep"));
2065		sprintf(arg5, "qtdc0_sz=%d", nvram_get_int("qtdc0_sz"));
2066		sprintf(arg6, "qtdc1_ep=%d", nvram_get_int("qtdc1_ep"));
2067		sprintf(arg7, "qtdc1_sz=%d", nvram_get_int("qtdc1_sz"));
2068
2069		modprobe("ehci-hcd", arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2070
2071		/* Search for existing PCI wl devices and the max unit number used.
2072		 * Note that PCI driver has to be loaded before USB hotplug event.
2073		 * This is enforced in rc.c
2074		 */
2075		#define DEV_NUMIFS 8
2076		for (i = 1; i <= DEV_NUMIFS; i++) {
2077			sprintf(ifname, "eth%d", i);
2078			if (!wl_probe(ifname)) {
2079				if (!wl_ioctl(ifname, WLC_GET_INSTANCE, &unit,
2080					sizeof(unit))) {
2081					maxwl_eth = i;
2082					maxunit = (unit > maxunit) ? unit : maxunit;
2083				}
2084			}
2085		}
2086
2087		/* Set instance base (starting unit number) for USB device */
2088		sprintf(insmod_arg, "instance_base=%d", maxunit + 1);
2089		modprobe("wl_high", insmod_arg);
2090
2091		/* Hold until the USB/HSIC interface is up (up to wl_wait sec) */
2092		sprintf(ifname, "eth%d", maxwl_eth + 1);
2093		i = wl_wait;
2094		while (wl_probe(ifname) && i--) {
2095			sleep(1);
2096		}
2097		if (!wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)))
2098			cprintf("wl%d is up in %d sec\n", unit, wl_wait - i);
2099		else
2100			cprintf("wl%d not up in %d sec\n", unit, wl_wait);
2101	}
2102#endif /* __CONFIG_USBAP__ */
2103}
2104
2105void init_wl_compact(void)
2106{
2107	int model = get_model();
2108
2109	if (nvram_get_int("init_wl_re") == 0)
2110	{
2111		nvram_set_int("init_wl_re", 1);
2112		return;
2113	}
2114
2115#ifdef RTCONFIG_EMF
2116	modprobe("emf");
2117	modprobe("igs");
2118#endif
2119#ifdef RTCONFIG_BCMWL6
2120	switch(model) {
2121		case MODEL_DSLAC68U:
2122		case MODEL_RTAC1200G:
2123		case MODEL_RTAC1200GP:
2124		case MODEL_RTAC3100:
2125		case MODEL_RTAC3200:
2126		case MODEL_RTAC5300:
2127		case MODEL_RTAC5300R:
2128		case MODEL_RTAC66U:
2129		case MODEL_RTAC68U:
2130		case MODEL_RTAC88U:
2131			set_bcm4360ac_vars();
2132			break;
2133	}
2134#endif
2135	check_wl_country();
2136#ifndef RTCONFIG_BRCM_USBAP
2137	if ((model == MODEL_DSLAC68U) ||
2138		(model == MODEL_RPAC68U) ||
2139		(model == MODEL_RTAC1200G) ||
2140		(model == MODEL_RTAC1200GP) ||
2141		(model == MODEL_RTAC3100) ||
2142		(model == MODEL_RTAC3200) ||
2143		(model == MODEL_RTAC5300) ||
2144		(model == MODEL_RTAC5300R) ||
2145		(model == MODEL_RTAC53U) ||
2146		(model == MODEL_RTAC66U) ||
2147		(model == MODEL_RTAC68U) ||
2148		(model == MODEL_RTAC87U) ||
2149		(model == MODEL_RTAC88U) ||
2150		(model == MODEL_RTN12HP_B1) ||
2151		(model == MODEL_RTN18U) ||
2152		(model == MODEL_RTN66U)) {
2153#if defined(RTAC3200) || defined(RTAC68U) || defined(RTAC5300) || defined(RTAC5300R)
2154		wl_disband5grp();
2155#endif
2156		set_wltxpower();
2157#if defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)
2158		load_wl();
2159#else
2160#ifdef RTCONFIG_ALLNOWL
2161		if (!nvram_match("nowl", "1"))
2162#endif
2163		modprobe("wl");
2164#endif
2165#ifndef RTCONFIG_BCMARM
2166#if defined(NAS_GTK_PER_STA) && defined(PROXYARP)
2167		modprobe("proxyarp");
2168#endif
2169#endif
2170	}
2171#endif
2172}
2173
2174void fini_wl(void)
2175{
2176	int model = get_model();
2177
2178#ifndef RTCONFIG_BCMARM
2179#if defined(NAS_GTK_PER_STA) && defined(PROXYARP)
2180	modprobe_r("proxyarp");
2181#endif
2182#endif
2183
2184#ifndef RTCONFIG_BRCM_USBAP
2185	if ((model == MODEL_DSLAC68U) ||
2186		(model == MODEL_RPAC68U) ||
2187		(model == MODEL_RTAC1200G) ||
2188		(model == MODEL_RTAC1200GP) ||
2189		(model == MODEL_RTAC3100) ||
2190		(model == MODEL_RTAC3200) ||
2191		(model == MODEL_RTAC5300) ||	/* will be removed in 7.x */
2192		(model == MODEL_RTAC5300R) ||	/* will be removed in 7.x */
2193		(model == MODEL_RTAC66U) ||
2194		(model == MODEL_RTAC68U) ||
2195		(model == MODEL_RTAC87U) ||
2196		(model == MODEL_RTAC88U) ||
2197		(model == MODEL_RTN12HP_B1) |
2198		(model == MODEL_RTN18U) ||
2199		(model == MODEL_RTN66U))
2200	eval("rmmod", "wl");
2201#endif
2202#ifdef RTCONFIG_NOWL
2203#ifdef RTCONFIG_DHDAP
2204	eval("rmmod", "dhd");
2205#endif
2206#endif
2207}
2208
2209void init_syspara(void)
2210{
2211	char *ptr;
2212	int model;
2213
2214	nvram_set("firmver", rt_version);
2215	nvram_set("productid", rt_buildname);
2216	nvram_set("buildno", rt_serialno);
2217	nvram_set("extendno", rt_extendno);
2218	nvram_set("buildinfo", rt_buildinfo);
2219	nvram_set("swpjverno", rt_swpjverno);
2220	ptr = nvram_get("regulation_domain");
2221
2222	model = get_model();
2223	switch(model) {
2224		case MODEL_RTN53:
2225		case MODEL_RTN16:
2226		case MODEL_RTN15U:
2227		case MODEL_RTN12:
2228		case MODEL_RTN12B1:
2229		case MODEL_RTN12C1:
2230		case MODEL_RTN12D1:
2231		case MODEL_RTN12VP:
2232		case MODEL_RTN12HP:
2233		case MODEL_RTN12HP_B1:
2234		case MODEL_APN12HP:
2235		case MODEL_RTN14UHP:
2236		case MODEL_RTN10U:
2237		case MODEL_RTN10P:
2238		case MODEL_RTN10D1:
2239		case MODEL_RTN10PV2:
2240			if (!nvram_get("et0macaddr"))	// eth0, eth1
2241				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2242			if (!nvram_get("0:macaddr"))	// eth2(5G)
2243				nvram_set("0:macaddr", "00:22:15:A5:03:04");
2244			if (nvram_get("regulation_domain_5G")) {// by ate command from asuswrt, prior than ui 2.0
2245				nvram_set("wl1_country_code", nvram_get("regulation_domain_5G"));
2246				nvram_set("0:ccode", nvram_get("regulation_domain_5G"));
2247			} else if (nvram_get("regulation_domain_5g")) {	// by ate command from ui 2.0
2248				nvram_set("wl1_country_code", nvram_get("regulation_domain_5g"));
2249				nvram_set("0:ccode", nvram_get("regulation_domain_5g"));
2250			}
2251			else {
2252				nvram_set("wl1_country_code", "US");
2253				nvram_set("0:ccode", "US");
2254			}
2255			nvram_set("sb/1/macaddr", nvram_safe_get("et0macaddr"));
2256			if (ptr && *ptr) {
2257				if ((strlen(ptr) == 6) && /* legacy format */
2258					!strncasecmp(ptr, "0x", 2))
2259				{
2260					nvram_set("wl0_country_code", ptr+4);
2261					nvram_set("sb/1/ccode", ptr+4);
2262				}
2263				else
2264				{
2265					nvram_set("wl0_country_code", ptr);
2266					nvram_set("sb/1/ccode", ptr);
2267				}
2268			}else {
2269				nvram_set("wl0_country_code", "US");
2270				nvram_set("sb/1/ccode", "US");
2271			}
2272			break;
2273
2274		case MODEL_RTN66U:
2275		case MODEL_RTAC66U:
2276			if (!nvram_get("et0macaddr"))		// eth0, eth1
2277				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2278			if (!nvram_get("pci/2/1/macaddr"))	// eth2(5G)
2279				nvram_set("pci/2/1/macaddr", "00:22:15:A5:03:04");
2280			if (nvram_get("regulation_domain_5G")) {
2281				nvram_set("wl1_country_code", nvram_get("regulation_domain_5G"));
2282				nvram_set("pci/2/1/ccode", nvram_get("regulation_domain_5G"));
2283			}else {
2284				nvram_set("wl1_country_code", "US");
2285				nvram_set("pci/2/1/ccode", "US");
2286			}
2287			nvram_set("pci/1/1/macaddr", nvram_safe_get("et0macaddr"));
2288			if (ptr) {
2289				nvram_set("wl0_country_code", ptr);
2290				nvram_set("pci/1/1/ccode", ptr);
2291			}else {
2292				nvram_set("wl0_country_code", "US");
2293				nvram_set("pci/1/1/ccode", "US");
2294			}
2295			break;
2296
2297		case MODEL_RTN18U:
2298			if (!nvram_get("et0macaddr"))	//eth0, eth1
2299				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2300			nvram_set("0:macaddr", nvram_safe_get("et0macaddr"));
2301
2302			if (nvram_match("0:ccode", "0")) {
2303				nvram_set("0:ccode","US");
2304			}
2305			break;
2306
2307		case MODEL_RTAC87U:
2308			if (!nvram_get("et1macaddr"))	//eth0, eth1
2309				nvram_set("et1macaddr", "00:22:15:A5:03:00");
2310			nvram_set("0:macaddr", nvram_safe_get("et1macaddr"));
2311			nvram_set("LANMACADDR", nvram_safe_get("et1macaddr"));
2312			break;
2313
2314		case MODEL_DSLAC68U:
2315		case MODEL_RPAC68U:
2316		case MODEL_RTAC68U:
2317		case MODEL_RTAC56S:
2318		case MODEL_RTAC56U:
2319			if (!nvram_get("et0macaddr"))	//eth0, eth1
2320				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2321			if (!nvram_get("1:macaddr"))	//eth2(5G)
2322				nvram_set("1:macaddr", "00:22:15:A5:03:04");
2323			nvram_set("0:macaddr", nvram_safe_get("et0macaddr"));
2324			break;
2325
2326		case MODEL_RTAC5300:
2327		case MODEL_RTAC5300R:
2328		case MODEL_RTAC88U:
2329			if (!nvram_get("lan_hwaddr"))
2330				nvram_set("lan_hwaddr", cfe_nvram_safe_get("et1macaddr"));
2331
2332			break;
2333
2334		case MODEL_RTAC3100:
2335			if (!nvram_get("lan_hwaddr"))
2336				nvram_set("lan_hwaddr", cfe_nvram_safe_get("et0macaddr"));
2337			break;
2338
2339		case MODEL_RTAC3200:
2340			if (!nvram_get("et0macaddr"))				// eth0 (ethernet)
2341				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2342			nvram_set("1:macaddr", nvram_safe_get("et0macaddr"));	// eth2 (2.4GHz)
2343			if (!nvram_get("0:macaddr"))				// eth1(5GHz)
2344				nvram_set("0:macaddr", "00:22:15:A5:03:04");
2345			if (!nvram_get("2:macaddr"))				// eth3(5GHz)
2346				nvram_set("2:macaddr", "00:22:15:A5:03:08");
2347			break;
2348
2349		case MODEL_RTAC53U:
2350			if (!nvram_get("et0macaddr"))	//eth0, eth1
2351				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2352			if (!nvram_get("0:macaddr"))	//eth2(5G)
2353				nvram_set("0:macaddr", "00:22:15:A5:03:04");
2354			nvram_set("sb/1/macaddr", nvram_safe_get("et0macaddr"));
2355			break;
2356
2357		case MODEL_RTAC1200G:
2358		case MODEL_RTAC1200GP:
2359			if (!nvram_get("et0macaddr"))
2360				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2361			if (!nvram_get("sb/1/macaddr"))	// (5GHz)
2362				nvram_set("sb/1/macaddr", "00:22:15:A5:03:04");
2363			nvram_set("0:macaddr", nvram_safe_get("et0macaddr")); // (2.4GHz)
2364
2365		default:
2366#ifdef RTCONFIG_RGMII_BRCM5301X
2367			if (!nvram_get("lan_hwaddr"))
2368				nvram_set("lan_hwaddr", cfe_nvram_get("et1macaddr"));
2369#else
2370			if (!nvram_get("et0macaddr"))
2371				nvram_set("et0macaddr", "00:22:15:A5:03:00");
2372#endif
2373			break;
2374	}
2375
2376#ifdef RTCONFIG_ODMPID
2377	if (nvram_match("odmpid", "ASUS") ||
2378		!is_valid_hostname(nvram_safe_get("odmpid")) ||
2379		!strcmp(RT_BUILD_NAME, nvram_safe_get("odmpid")))
2380		nvram_set("odmpid", "");
2381#endif
2382
2383	if (nvram_get("secret_code"))
2384		nvram_set("wps_device_pin", nvram_get("secret_code"));
2385	else
2386		nvram_set("wps_device_pin", "12345670");
2387}
2388
2389#ifdef RTCONFIG_BCMARM
2390#define ASUS_TWEAK
2391#ifdef RTCONFIG_BCM_7114
2392#define SMP_AFFINITY_WL	"1"
2393#else
2394#define SMP_AFFINITY_WL "3"
2395#endif
2396void tweak_smp_affinity(int enable_samba)
2397{
2398#ifndef RTCONFIG_BCM7
2399	if (nvram_get_int("stop_tweak_wl") == 1)
2400#endif
2401		return;
2402
2403#ifdef RTCONFIG_GMAC3
2404	if (nvram_match("gmac3_enable", "1"))
2405		return;
2406#endif
2407
2408#ifdef RTCONFIG_BCM7114
2409	if (enable_samba) {
2410		f_write_string("/proc/irq/163/smp_affinity", SMP_AFFINITY_WL, 0, 0);
2411		f_write_string("/proc/irq/169/smp_affinity", SMP_AFFINITY_WL, 0, 0);
2412	}
2413	else
2414#endif
2415	{
2416		f_write_string("/proc/irq/163/smp_affinity", "2", 0, 0);
2417		f_write_string("/proc/irq/169/smp_affinity", "2", 0, 0);
2418	}
2419}
2420
2421void init_others(void)
2422{
2423#ifdef SMP
2424	int fd;
2425
2426	if ((fd = open("/proc/irq/163/smp_affinity", O_RDWR)) >= 0) {
2427		close(fd);
2428#ifdef RTCONFIG_GMAC3
2429		if (nvram_match("gmac3_enable", "1")) {
2430			if (nvram_match("asus_tweak_usb_disable", "1")) {
2431				char *fwd_cpumap;
2432
2433				/* Place network interface vlan1/eth0 on CPU hosting 5G upper */
2434				fwd_cpumap = nvram_get("fwd_cpumap");
2435
2436				if (fwd_cpumap == NULL) {
2437					/* BCM4709acdcrh: Network interface GMAC on Core#0
2438					 * [5G+2G:163 on Core#0] and [5G:169 on Core#1].
2439					 * Bind et2:vlan1:eth0:181 to Core#0
2440					 * Note, USB3 xhci_hcd's irq#112 binds Core#1
2441					 * bind eth0:181 to Core#1 impacts USB3 performance
2442					 */
2443					f_write_string("/proc/irq/181/smp_affinity", "1", 0, 0);
2444				} else {
2445					char cpumap[32], *next;
2446
2447					foreach(cpumap, fwd_cpumap, next) {
2448						char mode, chan;
2449						int band, irq, cpu;
2450
2451						/* Format: mode:chan:band#:irq#:cpu# */
2452						if (sscanf(cpumap, "%c:%c:%d:%d:%d",
2453							&mode, &chan, &band, &irq, &cpu) != 5) {
2454							break;
2455						}
2456						if (cpu > 1) {
2457							break;
2458						}
2459						/* Find the single 5G upper */
2460						if ((chan == 'u') || (chan == 'U')) {
2461							char command[128];
2462							snprintf(command, sizeof(command),
2463								"echo %d > /proc/irq/181/smp_affinity",
2464								1 << cpu);
2465							system(command);
2466							break;
2467						}
2468					}
2469				}
2470			}
2471			else
2472#if defined(RTAC88U) || defined (RTAC3100) || defined (RTAC5300) || defined(RTAC5300R)
2473				f_write_string("/proc/irq/181/smp_affinity", "1", 0, 0);
2474#else
2475				f_write_string("/proc/irq/181/smp_affinity", "3", 0, 0);
2476#endif
2477		} else
2478#endif
2479		{
2480#ifdef ASUS_TWEAK
2481#if defined(RTAC88U) || defined (RTAC3100) || defined (RTAC5300) || defined(RTAC5300R)
2482			f_write_string("/proc/irq/180/smp_affinity", "1", 0, 0);
2483#endif
2484#ifndef RTCONFIG_BCM7
2485			if (nvram_match("asus_tweak", "1")) {	/* dbg ref ? */
2486				f_write_string("/proc/irq/179/smp_affinity", "1", 0, 0);	// eth0
2487				f_write_string("/proc/irq/163/smp_affinity", "2", 0, 0);	// eth1 or eth1/eth2
2488				f_write_string("/proc/irq/169/smp_affinity", "2", 0, 0);	// eth2 or eth3
2489			} else
2490#endif	// RTCONFIG_BCM7
2491				tweak_smp_affinity(0);
2492#endif	// ASUS_TWEAK
2493		}
2494
2495		if (!nvram_get_int("stop_tweak_usb")) {
2496			f_write_string("/proc/irq/111/smp_affinity", "2", 0, 0);		// ehci, ohci
2497			f_write_string("/proc/irq/112/smp_affinity", "2", 0, 0);		// xhci
2498		}
2499	}
2500#endif	// SMP
2501
2502#ifdef ASUS_TWEAK
2503#ifdef RTCONFIG_BCM_7114
2504	nvram_unset("txworkq");
2505#else
2506	if (nvram_match("enable_samba", "1")) {
2507#if !defined(RTCONFIG_BCM9) && !defined(RTCONFIG_BCM7)
2508		nvram_set("txworkq", factory_debug() ? "0" : "1");
2509		nvram_set("txworkq_wl", "1");
2510#else
2511		nvram_set("txworkq", "1");
2512#endif
2513	} else {
2514		nvram_unset("txworkq");
2515#if !defined(RTCONFIG_BCM9) && !defined(RTCONFIG_BCM7)
2516		nvram_unset("txworkq_wl");
2517#endif
2518	}
2519#endif
2520#endif // RTCONFIG_BCM_7114
2521
2522#ifdef RTAC68U
2523	update_cfe();
2524#endif
2525#ifdef RTAC3200
2526	update_cfe_ac3200();
2527#endif
2528}
2529#endif
2530
2531void chanspec_fix_5g(int unit)
2532{
2533	char tmp[100], prefix[]="wlXXXXXXX_";
2534	int channel;
2535
2536	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
2537	channel = nvram_get_int(strcat_r(prefix, "channel", tmp));
2538
2539	if ((channel == 36) || (channel == 44) || (channel == 52) || (channel == 60) || (channel == 100) || (channel == 108) || (channel == 116) || (channel == 124) || (channel == 132) || (channel == 149) || (channel == 157))
2540	{
2541		dbG("fix nctrlsb of channel %d as %s\n", channel, "lower");
2542		nvram_set(strcat_r(prefix, "nctrlsb", tmp), "lower");
2543	}
2544	else if ((channel == 40) || (channel == 48) || (channel == 56) || (channel == 64) || (channel == 104) || (channel == 112) || (channel == 120) || (channel == 128) || (channel == 136) || (channel == 153) || (channel == 161))
2545	{
2546		dbG("fix nctrlsb of channel %d as %s\n", channel, "upper");
2547		nvram_set(strcat_r(prefix, "nctrlsb", tmp), "upper");
2548	}
2549}
2550
2551// this function is used to jutisfy which band(unit) to be forced connected.
2552int is_ure(int unit)
2553{
2554	// forced to connect to which band
2555	// is it suitable
2556	if (nvram_get_int("sw_mode") == SW_MODE_REPEATER) {
2557		if (nvram_get_int("wlc_band") == unit) return 1;
2558	}
2559	return 0;
2560}
2561
2562int wl_max_no_vifs(int unit)
2563{
2564	char nv_interface[NVRAM_MAX_PARAM_LEN];
2565	char cap[WLC_IOCTL_SMLEN];
2566	char caps[WLC_IOCTL_SMLEN];
2567	char *name = NULL;
2568	char *next = NULL;
2569	int max_no_vifs = 0;
2570#ifdef RTCONFIG_QTN
2571	if (unit == 1)
2572		return 4;
2573#endif
2574	sprintf(nv_interface, "wl%d_ifname", unit);
2575	name = nvram_safe_get(nv_interface);
2576	if (!strlen(name))
2577	{
2578		sprintf(nv_interface, "eth%d", unit + 1);
2579		name = nv_interface;
2580	}
2581
2582	if (!wl_iovar_get(name, "cap", (void *)caps, WLC_IOCTL_SMLEN)) {
2583		foreach(cap, caps, next) {
2584			if (!strcmp(cap, "mbss16"))
2585				max_no_vifs = 16;
2586			else if (!strcmp(cap, "mbss4"))
2587				max_no_vifs = 4;
2588		}
2589	}
2590
2591	return max_no_vifs;
2592}
2593
2594#if (defined(RTCONFIG_BCM7)||defined(RTCONFIG_BCM_7114)) && defined(BCM_BSD)
2595
2596#define BSD_STA_SELECT_POLICY_NVRAM		"bsd_sta_select_policy"
2597#define BSD_STA_SELECT_POLICY_FLAG_NON_VHT	0x00000008	/* NON VHT STA */
2598#define BSD_IF_QUALIFY_POLICY_NVRAM		"bsd_if_qualify_policy"
2599#define BSD_QUALIFY_POLICY_FLAG_NON_VHT		0x00000004	/* NON VHT STA */
2600#if defined(RTAC5300) || defined(RTAC5300R)
2601#define BSD_STA_SELECT_POLICY_NVRAM_X		"bsd_sta_select_policy_x"
2602#define BSD_IF_QUALIFY_POLICY_NVRAM_X		"bsd_if_qualify_policy_x"
2603#endif
2604
2605int get_bsd_nonvht_status(int unit)
2606{
2607	char tmp[100], prefix[]="wlXXXXXXX_";
2608	char *str;
2609	int num;
2610	unsigned int i1,i2,i3,i4,i5,i6,i7,i8,i9,ia;
2611	unsigned int flags;
2612
2613	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
2614#if defined(RTAC5300) || defined(RTAC5300R)
2615	if (nvram_get_int("smart_connect_x") == 2) // 5GHz Only
2616		str = nvram_get(strcat_r(prefix, BSD_STA_SELECT_POLICY_NVRAM_X, tmp));
2617	else
2618#endif
2619		str = nvram_get(strcat_r(prefix, BSD_STA_SELECT_POLICY_NVRAM, tmp));
2620	if (str) {
2621		num = sscanf(str, "%d %d %d %d %d %d %d %d %d %d %x",
2622			&i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9, &ia, &flags);
2623		if ((num == 11) && (flags & BSD_STA_SELECT_POLICY_FLAG_NON_VHT))
2624			return 1;
2625	}
2626#if defined(RTAC5300) || defined(RTAC5300R)
2627	if (nvram_get_int("smart_connect_x") == 2) // 5GHz Only
2628		str = nvram_get(strcat_r(prefix, BSD_IF_QUALIFY_POLICY_NVRAM_X, tmp));
2629	else
2630#endif
2631		str = nvram_get(strcat_r(prefix, BSD_IF_QUALIFY_POLICY_NVRAM, tmp));
2632	if (str) {
2633		num = sscanf(str, "%d %x", &i1, &flags);
2634		if ((num == 2) && (flags & BSD_QUALIFY_POLICY_FLAG_NON_VHT))
2635			return 1;
2636	}
2637
2638	return 0;
2639}
2640#endif
2641
2642void generate_wl_para(int unit, int subunit)
2643{
2644	dbG("unit %d subunit %d\n", unit, subunit);
2645
2646	char tmp[100], prefix[]="wlXXXXXXX_";
2647	char tmp2[100], prefix2[]="wlXXXXXXX_";
2648	char *list;
2649	char *nv, *nvp, *b;
2650	char word[256], *next;
2651#ifndef RTCONFIG_BCMWL6
2652	int match;
2653#endif
2654	int max_no_vifs = wl_max_no_vifs(unit), i, mcast_rate;
2655	char interface_list[NVRAM_MAX_VALUE_LEN];
2656	int interface_list_size = sizeof(interface_list);
2657	char nv_interface[NVRAM_MAX_PARAM_LEN];
2658	int if_unit, ure;
2659#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
2660	char lan_ifnames[NVRAM_MAX_PARAM_LEN] = "lan_ifnames";
2661	bool psta = 0, psr = 0, db_rpt = 0;
2662#endif
2663#ifdef PXYSTA_DUALBAND
2664	char os_interface[NVRAM_MAX_PARAM_LEN];
2665#endif
2666
2667	if (subunit == -1)
2668	{
2669		snprintf(prefix, sizeof(prefix), "wl%d_", unit);
2670		if (nvram_match("wps_enable", "1") &&
2671			((unit == nvram_get_int("wps_band") || nvram_match("w_Setting", "0"))))
2672			nvram_set(strcat_r(prefix, "wps_mode", tmp), "enabled");
2673		else
2674			nvram_set(strcat_r(prefix, "wps_mode", tmp), "disabled");
2675
2676#ifdef BCM_BSD
2677		if (((unit == 0) && nvram_get_int("smart_connect_x") == 1) ||
2678		    ((unit == 1) && nvram_get_int("smart_connect_x") == 2)) {
2679
2680			int wlif_count = 0;
2681			foreach(word, nvram_safe_get("wl_ifnames"), next)
2682				wlif_count++;
2683
2684			for (i = unit + 1; i < wlif_count; i++) {
2685				snprintf(prefix2, sizeof(prefix2), "wl%d_", i);
2686				nvram_set(strcat_r(prefix2, "ssid", tmp2), nvram_safe_get(strcat_r(prefix, "ssid", tmp)));
2687				nvram_set(strcat_r(prefix2, "auth_mode_x", tmp2), nvram_safe_get(strcat_r(prefix, "auth_mode_x", tmp)));
2688				nvram_set(strcat_r(prefix2, "wep_x", tmp2), nvram_safe_get(strcat_r(prefix, "wep_x", tmp)));
2689				nvram_set(strcat_r(prefix2, "key", tmp2), nvram_safe_get(strcat_r(prefix, "key", tmp)));
2690				nvram_set(strcat_r(prefix2, "key1", tmp2), nvram_safe_get(strcat_r(prefix, "key1", tmp)));
2691				nvram_set(strcat_r(prefix2, "key2", tmp2), nvram_safe_get(strcat_r(prefix, "key2", tmp)));
2692				nvram_set(strcat_r(prefix2, "key3", tmp2), nvram_safe_get(strcat_r(prefix, "key3", tmp)));
2693				nvram_set(strcat_r(prefix2, "key4", tmp2), nvram_safe_get(strcat_r(prefix, "key4", tmp)));
2694				nvram_set(strcat_r(prefix2, "phrase_x", tmp2), nvram_safe_get(strcat_r(prefix, "phrase_x", tmp)));
2695				nvram_set(strcat_r(prefix2, "crypto", tmp2), nvram_safe_get(strcat_r(prefix, "crypto", tmp)));
2696				nvram_set(strcat_r(prefix2, "wpa_psk", tmp2), nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp)));
2697				nvram_set(strcat_r(prefix2, "radius_ipaddr", tmp2), nvram_safe_get(strcat_r(prefix, "radius_ipaddr", tmp)));
2698				nvram_set(strcat_r(prefix2, "radius_key", tmp2), nvram_safe_get(strcat_r(prefix, "radius_key", tmp)));
2699				nvram_set(strcat_r(prefix2, "radius_port", tmp2), nvram_safe_get(strcat_r(prefix, "radius_port", tmp)));
2700			}
2701		}
2702
2703		int acs = 1;
2704#if 0
2705		if ((unit == 0) && (nvram_get_int("smart_connect_x") == 1))
2706			nvram_set_int(strcat_r(prefix, "taf_enable", tmp), 1);
2707		else if ((unit == 1) && nvram_get_int("smart_connect_x"))
2708			nvram_set_int(strcat_r(prefix, "taf_enable", tmp), 1);
2709		else if ((unit == 2) && nvram_get_int("smart_connect_x"))
2710			nvram_set_int(strcat_r(prefix, "taf_enable", tmp), 1);
2711		else {
2712			nvram_set_int(strcat_r(prefix, "taf_enable", tmp), 0);
2713			acs = 0 ;
2714		}
2715#else
2716		if (!(((unit == 0) && (nvram_get_int("smart_connect_x") == 1)) ||
2717		      ((unit == 1) && nvram_get_int("smart_connect_x")) ||
2718		      ((unit == 2) && nvram_get_int("smart_connect_x"))))
2719			acs = 0;
2720#endif
2721
2722		if (acs) {
2723			nvram_set(strcat_r(prefix, "nmode_x", tmp), "0");
2724			nvram_set(strcat_r(prefix, "bw", tmp), "0");
2725			nvram_set(strcat_r(prefix, "chanspec", tmp), "0");
2726		}
2727#endif
2728
2729		if_unit = 0;
2730		foreach (word, nvram_safe_get("wl_ifnames"), next) {
2731			ure = is_ure(if_unit);
2732#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
2733			psta = is_psta(if_unit);
2734			psr = is_psr(if_unit);
2735#endif
2736
2737			if (ure
2738#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
2739				|| psta || psr
2740#endif
2741			) {
2742				nvram_set(strcat_r(prefix, "nmode_x", tmp), "0");
2743#ifdef RTCONFIG_BCMWL6
2744				nvram_set(strcat_r(prefix, "bw", tmp), "0");
2745#else
2746				nvram_set(strcat_r(prefix, "bw", tmp), "1");
2747#endif
2748				nvram_set(strcat_r(prefix, "chanspec", tmp), "0");
2749
2750				break;
2751			}
2752
2753			if_unit++;
2754		}
2755
2756#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
2757		/* See if other interface also has psta or psr enabled */
2758		char *if_next;
2759		if_unit = 0;
2760		foreach (word, nvram_safe_get("wl_ifnames"), if_next) {
2761			psta = is_psta(if_unit);
2762			psr = is_psr(if_unit);
2763			db_rpt |= ((psta_exist_except(if_unit) || psr_exist_except(if_unit)) &&
2764			  	(psta || psr));
2765			if_unit++;
2766		}
2767
2768		/* Disable all VIFS wlX.2 onwards */
2769		if (is_psta(unit) || is_psr(unit))
2770		{
2771			nvram_set(strcat_r(prefix, "bss_enabled", tmp), "1");
2772
2773			strncpy(interface_list, nvram_safe_get(lan_ifnames), interface_list_size);
2774
2775			/* While enabling proxy sta or repeater modes on second wl interface
2776			 * (dual band repeater) set nvram variable dpsta_ifnames to upstream
2777			 * interfaces.
2778			 */
2779#ifdef PXYSTA_DUALBAND
2780			if (db_rpt) {
2781				char if_list[NVRAM_MAX_VALUE_LEN];
2782				int if_list_size = sizeof(if_list);
2783
2784				memset(if_list, 0, sizeof(if_list));
2785				strcpy(tmp, "dpsta_ifnames");
2786				sprintf(nv_interface, "wl%s", nvram_safe_get("wlc_band"));
2787				nvifname_to_osifname(nv_interface, os_interface,
2788					sizeof(os_interface));
2789				add_to_list(os_interface, if_list, if_list_size);
2790				sprintf(nv_interface, "wl%s", nvram_safe_get("wlc_band_ex"));
2791				nvifname_to_osifname(nv_interface, os_interface,
2792					sizeof(os_interface));
2793				add_to_list(os_interface, if_list, if_list_size);
2794				nvram_set(tmp, if_list);
2795			} else if (!db_rpt)
2796#endif
2797				nvram_set("dpsta_ifnames", "");
2798
2799			sprintf(nv_interface, "wl%d.1", unit);
2800
2801			/* Set the wl mode for the virtual interface */
2802			sprintf(tmp, "wl%d.1_mode", unit);
2803			if (is_psta(unit)) {
2804				/* For Proxy we need to remove our ap interface */
2805				remove_from_list(nv_interface, interface_list, interface_list_size);
2806				nvram_set(lan_ifnames, interface_list);
2807
2808				/* Clear the ap mode */
2809				nvram_set(tmp, "");
2810			} else {
2811				/* For Repeater we need to add our ap interface to the bridged lan */
2812				add_to_list(nv_interface, interface_list, interface_list_size);
2813				nvram_set(lan_ifnames, interface_list);
2814
2815				/* Set the ap mode */
2816				nvram_set(tmp, "ap");
2817			}
2818
2819			for (i = 2; i < max_no_vifs; i++) {
2820				sprintf(tmp, "wl%d.%d_bss_enabled", unit, i);
2821				nvram_set(tmp, "0");
2822			}
2823		}
2824		else
2825#endif
2826		{
2827#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
2828			if (!db_rpt)
2829				nvram_set("dpsta_ifnames", "");
2830#endif
2831			memset(interface_list, 0, interface_list_size);
2832			for (i = 1; i < max_no_vifs; i++) {
2833				sprintf(nv_interface, "wl%d.%d", unit, i);
2834				add_to_list(nv_interface, interface_list, interface_list_size);
2835				nvram_set(strcat_r(nv_interface, "_hwaddr", tmp), "");
2836			}
2837
2838			reset_mssid_hwaddr(unit);
2839		}
2840	}
2841	else
2842	{
2843		snprintf(prefix, sizeof(prefix), "wl%d.%d_", unit, subunit);
2844		snprintf(prefix2, sizeof(prefix2), "wl%d_", unit);
2845#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
2846		if (is_psta(unit))
2847			nvram_set(strcat_r(prefix, "bss_enabled", tmp), "0");
2848		else if (is_psr(unit)) {
2849			if (subunit == 1)
2850				nvram_set(strcat_r(prefix, "bss_enabled", tmp), "1");
2851			else
2852				nvram_set(strcat_r(prefix, "bss_enabled", tmp), "0");
2853		}
2854#endif
2855	}
2856
2857	// convert wlc_xxx to wlX_ according to wlc_band == unit
2858	if (is_ure(unit)) {
2859		if (subunit == -1) {
2860			nvram_set("ure_disable", is_routing_enabled() ? "0" : "1");
2861
2862			nvram_set(strcat_r(prefix, "ssid", tmp), nvram_safe_get("wlc_ssid"));
2863			nvram_set(strcat_r(prefix, "auth_mode_x", tmp), nvram_safe_get("wlc_auth_mode"));
2864
2865			nvram_set(strcat_r(prefix, "wep_x", tmp), nvram_safe_get("wlc_wep"));
2866
2867			nvram_set(strcat_r(prefix, "key", tmp), nvram_safe_get("wlc_key"));
2868
2869			nvram_set(strcat_r(prefix, "key1", tmp), nvram_safe_get("wlc_wep_key"));
2870			nvram_set(strcat_r(prefix, "key2", tmp), nvram_safe_get("wlc_wep_key"));
2871			nvram_set(strcat_r(prefix, "key3", tmp), nvram_safe_get("wlc_wep_key"));
2872			nvram_set(strcat_r(prefix, "key4", tmp), nvram_safe_get("wlc_wep_key"));
2873			nvram_set(strcat_r(prefix, "crypto", tmp), nvram_safe_get("wlc_crypto"));
2874			nvram_set(strcat_r(prefix, "wpa_psk", tmp), nvram_safe_get("wlc_wpa_psk"));
2875		}
2876		else if (subunit == 1) {
2877			nvram_set(strcat_r(prefix, "bss_enabled", tmp), "1");
2878/*
2879			nvram_set(strcat_r(prefix, "ssid", tmp), nvram_safe_get("wlc_ure_ssid"));
2880			nvram_set(strcat_r(prefix, "auth_mode_x", tmp), nvram_safe_get("wlc_auth_mode"));
2881			nvram_set(strcat_r(prefix, "wep_x", tmp), nvram_safe_get("wlc_wep"));
2882			nvram_set(strcat_r(prefix, "key", tmp), nvram_safe_get("wlc_key"));
2883			nvram_set(strcat_r(prefix, "key1", tmp), nvram_safe_get("wlc_wep_key"));
2884			nvram_set(strcat_r(prefix, "key2", tmp), nvram_safe_get("wlc_wep_key"));
2885			nvram_set(strcat_r(prefix, "key3", tmp), nvram_safe_get("wlc_wep_key"));
2886			nvram_set(strcat_r(prefix, "key4", tmp), nvram_safe_get("wlc_wep_key"));
2887			nvram_set(strcat_r(prefix, "crypto", tmp), nvram_safe_get("wlc_crypto"));
2888			nvram_set(strcat_r(prefix, "wpa_psk", tmp), nvram_safe_get("wlc_wpa_psk"));
2889			nvram_set(strcat_r(prefix, "bw", tmp), nvram_safe_get("wlc_nbw_cap"));
2890*/
2891		}
2892	}
2893	// TODO: recover nvram from repeater
2894	else
2895#ifdef RTCONFIG_BCMWL6
2896#ifdef RTCONFIG_PROXYSTA
2897	if (is_psta(unit) || is_psr(unit)) {
2898		if (subunit == -1) {
2899			nvram_set("ure_disable", "1");
2900
2901			nvram_set(strcat_r(prefix, "ssid", tmp), nvram_safe_get("wlc_ssid"));
2902			nvram_set(strcat_r(prefix, "auth_mode_x", tmp), nvram_safe_get("wlc_auth_mode"));
2903			nvram_set(strcat_r(prefix, "wep_x", tmp), nvram_safe_get("wlc_wep"));
2904			nvram_set(strcat_r(prefix, "key", tmp), nvram_safe_get("wlc_key"));
2905			nvram_set(strcat_r(prefix, "key1", tmp), nvram_safe_get("wlc_wep_key"));
2906			nvram_set(strcat_r(prefix, "key2", tmp), nvram_safe_get("wlc_wep_key"));
2907			nvram_set(strcat_r(prefix, "key3", tmp), nvram_safe_get("wlc_wep_key"));
2908			nvram_set(strcat_r(prefix, "key4", tmp), nvram_safe_get("wlc_wep_key"));
2909			nvram_set(strcat_r(prefix, "crypto", tmp), nvram_safe_get("wlc_crypto"));
2910			nvram_set(strcat_r(prefix, "wpa_psk", tmp), nvram_safe_get("wlc_wpa_psk"));
2911			/* early set wlx_vifs for psr mode */
2912			if (is_psr(unit)) {
2913				sprintf(tmp2, "wl%d.1", unit);
2914				nvram_set(strcat_r(prefix, "vifs", tmp), tmp2);
2915			}
2916		}
2917
2918		if (is_psr(unit) && (subunit == 1)) {
2919			/* TODO: local AP profile */
2920		}
2921	}
2922#endif
2923#endif
2924
2925	memset(tmp, 0, sizeof(tmp));
2926	memset(tmp2, 0, sizeof(tmp2));
2927
2928	if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "shared"))
2929		nvram_set(strcat_r(prefix, "auth", tmp), "1");
2930	else nvram_set(strcat_r(prefix, "auth", tmp), "0");
2931
2932	if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "psk"))
2933		nvram_set(strcat_r(prefix, "akm", tmp), "psk");
2934	else if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "psk2"))
2935		nvram_set(strcat_r(prefix, "akm", tmp), "psk2");
2936	else if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "pskpsk2"))
2937		nvram_set(strcat_r(prefix, "akm", tmp), "psk psk2");
2938	else if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "wpa"))
2939	{
2940		nvram_set(strcat_r(prefix, "akm", tmp), "wpa");
2941	}
2942	else if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "wpa2"))
2943	{
2944		nvram_set(strcat_r(prefix, "akm", tmp), "wpa2");
2945	}
2946	else if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "wpawpa2"))
2947	{
2948		nvram_set(strcat_r(prefix, "akm", tmp), "wpa wpa2");
2949	}
2950	else nvram_set(strcat_r(prefix, "akm", tmp), "");
2951
2952	if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "radius"))
2953	{
2954		nvram_set(strcat_r(prefix, "auth_mode", tmp), "radius");
2955		nvram_set(strcat_r(prefix, "key", tmp), "2");
2956	}
2957	else nvram_set(strcat_r(prefix, "auth_mode", tmp), "none");
2958
2959	if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "radius"))
2960		nvram_set(strcat_r(prefix, "wep", tmp), "enabled");
2961	else if (nvram_invmatch(strcat_r(prefix, "akm", tmp), ""))
2962		nvram_set(strcat_r(prefix, "wep", tmp), "disabled");
2963	else if (nvram_get_int(strcat_r(prefix, "wep_x", tmp)) != 0)
2964		nvram_set(strcat_r(prefix, "wep", tmp), "enabled");
2965	else nvram_set(strcat_r(prefix, "wep", tmp), "disabled");
2966
2967	if (nvram_match(strcat_r(prefix, "mode", tmp), "ap") &&
2968	    strstr(nvram_safe_get(strcat_r(prefix, "akm", tmp2)), "wpa"))
2969		nvram_set(strcat_r(prefix, "preauth", tmp), "1");
2970	else
2971		nvram_set(strcat_r(prefix, "preauth", tmp), "");
2972
2973	if (!nvram_match(strcat_r(prefix, "macmode", tmp), "disabled")) {
2974
2975		nv = nvp = strdup(nvram_safe_get(strcat_r(prefix, "maclist_x", tmp)));
2976		list = (char*) malloc(sizeof(char) * (strlen(nv)+1));
2977		list[0] = 0;
2978
2979		if (nv) {
2980			while ((b = strsep(&nvp, "<")) != NULL) {
2981				if (strlen(b) == 0) continue;
2982				if (list[0] == 0)
2983					sprintf(list, "%s", b);
2984				else
2985					sprintf(list, "%s %s", list, b);
2986			}
2987			free(nv);
2988		}
2989		nvram_set(strcat_r(prefix, "maclist", tmp), list);
2990		free(list);
2991	}
2992	else
2993		nvram_set(strcat_r(prefix, "maclist", tmp), "");
2994
2995	if (subunit == -1)
2996	{
2997#ifdef RTCONFIG_BCM_7114
2998		/* for old fw(135x) compatibility, and don't use wlc_psta=3 afterwards  */
2999		if(nvram_get_int("wlc_psta") == 3)
3000			nvram_set("wlc_psta", "1");
3001#endif
3002		// wds mode control
3003		if (is_ure(unit)) nvram_set(strcat_r(prefix, "mode", tmp), "wet");
3004		else
3005#ifdef RTCONFIG_BCMWL6
3006#ifdef RTCONFIG_PROXYSTA
3007		if (is_psta(unit))
3008		{
3009			nvram_set(strcat_r(prefix, "mode", tmp), "psta");
3010		}
3011		else if (is_psr(unit))
3012		{
3013			nvram_set(strcat_r(prefix, "mode", tmp), "psr");
3014		}
3015		else
3016#endif
3017#endif
3018		if (nvram_match(strcat_r(prefix, "mode_x", tmp), "1"))		// wds only
3019			nvram_set(strcat_r(prefix, "mode", tmp), "wds");
3020
3021		else if (nvram_match(strcat_r(prefix, "mode_x", tmp), "3"))	// special defined for client
3022			nvram_set(strcat_r(prefix, "mode", tmp), "wet");
3023		else nvram_set(strcat_r(prefix, "mode", tmp), "ap");
3024
3025		// TODO use lazwds directly
3026		//if (!nvram_match(strcat_r(prefix, "wdsmode_ex", tmp), "2"))
3027		//	nvram_set(strcat_r(prefix, "lazywds", tmp), "1");
3028		//else nvram_set(strcat_r(prefix, "lazywds", tmp), "0");
3029
3030		// TODO need sw_mode ?
3031		// handle wireless wds list
3032		if (!nvram_match(strcat_r(prefix, "mode_x", tmp), "0")) {
3033			if (nvram_match(strcat_r(prefix, "wdsapply_x", tmp), "1")) {
3034				nv = nvp = strdup(nvram_safe_get(strcat_r(prefix, "wdslist", tmp)));
3035				list = (char*) malloc(sizeof(char) * (strlen(nv)+1));
3036				list[0] = 0;
3037
3038				if (nv) {
3039					while ((b = strsep(&nvp, "<")) != NULL) {
3040						if (strlen(b) == 0) continue;
3041						if (list[0] == 0)
3042							sprintf(list, "%s", b);
3043						else
3044							sprintf(list, "%s %s", list, b);
3045					}
3046					free(nv);
3047				}
3048				nvram_set(strcat_r(prefix, "wds", tmp), list);
3049				nvram_set(strcat_r(prefix, "lazywds", tmp), "0");
3050				free(list);
3051			}
3052			else {
3053				nvram_set(strcat_r(prefix, "wds", tmp), "");
3054				nvram_set(strcat_r(prefix, "lazywds", tmp), "1");
3055			}
3056		} else {
3057			nvram_set(strcat_r(prefix, "wds", tmp), "");
3058			nvram_set(strcat_r(prefix, "lazywds", tmp), "0");
3059		}
3060
3061		switch (nvram_get_int(strcat_r(prefix, "mrate_x", tmp))) {
3062		case 0: /* Auto */
3063			mcast_rate = 0;
3064			break;
3065		case 1: /* Legacy CCK 1Mbps */
3066			mcast_rate = 1000000;
3067			break;
3068		case 2: /* Legacy CCK 2Mbps */
3069			mcast_rate = 2000000;
3070			break;
3071		case 3: /* Legacy CCK 5.5Mbps */
3072			mcast_rate = 5500000;
3073			break;
3074		case 4: /* Legacy OFDM 6Mbps */
3075			mcast_rate = 6000000;
3076			break;
3077		case 5: /* Legacy OFDM 9Mbps */
3078			mcast_rate = 9000000;
3079			break;
3080		case 6: /* Legacy CCK 11Mbps */
3081			mcast_rate = 11000000;
3082			break;
3083		case 7: /* Legacy OFDM 12Mbps */
3084			mcast_rate = 12000000;
3085			break;
3086		case 8: /* Legacy OFDM 18Mbps */
3087			mcast_rate = 18000000;
3088			break;
3089		case 9: /* Legacy OFDM 24Mbps */
3090			mcast_rate = 24000000;
3091			break;
3092		case 10: /* Legacy OFDM 36Mbps */
3093			mcast_rate = 36000000;
3094			break;
3095		case 11: /* Legacy OFDM 48Mbps */
3096			mcast_rate = 48000000;
3097			break;
3098		case 12: /* Legacy OFDM 54Mbps */
3099			mcast_rate = 54000000;
3100			break;
3101		default: /* Auto */
3102			mcast_rate = 0;
3103			break;
3104		}
3105		nvram_set_int(strcat_r(prefix, "mrate", tmp), mcast_rate);
3106
3107		if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "0"))		// auto => b/g/n mixed or a/n(/ac) mixed
3108		{
3109			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-1");	// auto rate
3110			nvram_set(strcat_r(prefix, "nmode", tmp), "-1");
3111#ifndef RTCONFIG_BCMWL6
3112			nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3113#endif
3114			nvram_set(strcat_r(prefix, "vreqd", tmp), "1");
3115#ifdef RTCONFIG_BCM_7114
3116			nvram_set(strcat_r(prefix, "gmode", tmp), "1");
3117#else
3118			nvram_set(strcat_r(prefix, "gmode", tmp), nvram_match(strcat_r(prefix, "nband", tmp2), "2") ? "1" : "-1");	// 1: 54g Auto, 4: 4g Performance, 5: 54g LRS, 0: 802.11b Only
3119#endif
3120			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3121#ifdef RTCONFIG_BCMWL6
3122#if (defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)) && defined(BCM_BSD)
3123			if (nvram_get_int("smart_connect_x") && get_bsd_nonvht_status(unit))
3124				nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "3");	// devices must advertise VHT (11ac) capabilities to be allowed to associate
3125			else
3126#endif
3127			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "0");	// no requirements on joining devices
3128#endif
3129		}
3130		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "1"))	// n only
3131		{
3132			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-1");	// auto rate
3133			nvram_set(strcat_r(prefix, "nmode", tmp), "-1");
3134#ifndef RTCONFIG_BCMWL6
3135			nvram_set(strcat_r(prefix, "nreqd", tmp), "1");
3136#endif
3137			nvram_set(strcat_r(prefix, "vreqd", tmp), "0");
3138			nvram_set(strcat_r(prefix, "gmode", tmp), nvram_match(strcat_r(prefix, "nband", tmp2), "2") ? "1" : "-1");
3139			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3140#ifdef RTCONFIG_BCMWL6
3141			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "2");	// devices must advertise HT (11n) capabilities to be allowed to associate
3142#endif
3143		}
3144		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "4"))	// g/n mixed or a/n mixed
3145		{
3146			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-1");	// auto rate
3147			nvram_set(strcat_r(prefix, "nmode", tmp), "-1");
3148#ifndef RTCONFIG_BCMWL6
3149			nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3150#endif
3151			nvram_set(strcat_r(prefix, "vreqd", tmp), "0");
3152			nvram_set(strcat_r(prefix, "gmode", tmp), nvram_match(strcat_r(prefix, "nband", tmp2), "2") ? "1" : "-1");
3153			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3154#ifdef RTCONFIG_BCMWL6
3155			if (nvram_match(strcat_r(prefix, "nband", tmp), "2"))
3156			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "1");	// devices must advertise ERP (11g) capabilities to be allowed to associate
3157			else
3158			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "0");	// no requirements on joining devices
3159#endif
3160		}
3161		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "5"))	// g only
3162		{
3163			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-2");	// legacy rate
3164			nvram_set(strcat_r(prefix, "nmode", tmp), "0");
3165#ifndef RTCONFIG_BCMWL6
3166			nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3167#endif
3168			nvram_set(strcat_r(prefix, "vreqd", tmp), "0");
3169			nvram_set(strcat_r(prefix, "gmode", tmp), "2");
3170			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3171#ifdef RTCONFIG_BCMWL6
3172			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "1");	// devices must advertise ERP (11g) capabilities to be allowed to associate
3173#endif
3174		}
3175		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "6"))	// b only
3176		{
3177			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-2");	// legacy rate
3178			nvram_set(strcat_r(prefix, "nmode", tmp), "0");
3179#ifndef RTCONFIG_BCMWL6
3180			nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3181#endif
3182			nvram_set(strcat_r(prefix, "vreqd", tmp), "0");
3183			nvram_set(strcat_r(prefix, "gmode", tmp), "0");
3184			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3185#ifdef RTCONFIG_BCMWL6
3186			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "0");	// no requirements on joining devices
3187#endif
3188		}
3189		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "7"))	// a only
3190		{
3191			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-2");	// legacy rate
3192			nvram_set(strcat_r(prefix, "nmode", tmp), "0");
3193#ifndef RTCONFIG_BCMWL6
3194			nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3195#endif
3196			nvram_set(strcat_r(prefix, "vreqd", tmp), "0");
3197			nvram_set(strcat_r(prefix, "gmode", tmp), "-1");
3198			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3199#ifdef RTCONFIG_BCMWL6
3200			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "0");	// no requirements on joining devices
3201#endif
3202		}
3203#ifdef RTCONFIG_BCMWL6
3204		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "2"))	// b/g mixed
3205#else
3206		else								// b/g mixed
3207#endif
3208		{
3209			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-2");	// legacy rate
3210			nvram_set(strcat_r(prefix, "nmode", tmp), "0");
3211#ifndef RTCONFIG_BCMWL6
3212			nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3213#endif
3214			nvram_set(strcat_r(prefix, "vreqd", tmp), "0");
3215			nvram_set(strcat_r(prefix, "gmode", tmp), "1");
3216			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3217#ifdef RTCONFIG_BCMWL6
3218			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "0");	// no requirements on joining devices
3219#endif
3220		}
3221#ifdef RTCONFIG_BCMWL6
3222		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "3") && 	// ac only
3223			 nvram_match(strcat_r(prefix, "nband", tmp), "1"))
3224		{
3225			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-1");	// auto rate
3226			nvram_set(strcat_r(prefix, "nmode", tmp), "-1");
3227			nvram_set(strcat_r(prefix, "vreqd", tmp), "1");
3228			nvram_set(strcat_r(prefix, "gmode", tmp), "-1");
3229			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3230			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "3");	// devices must advertise VHT (11ac) capabilities to be allowed to associate
3231		}
3232		else if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "8"))	// n/ac mixed
3233		{
3234			nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-1");	// auto rate
3235			nvram_set(strcat_r(prefix, "nmode", tmp), "-1");
3236			nvram_set(strcat_r(prefix, "vreqd", tmp), "1");
3237			nvram_set(strcat_r(prefix, "gmode", tmp), "-1");
3238			nvram_set(strcat_r(prefix, "rate", tmp), "0");
3239			nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "2");	// devices must advertise HT (11n) capabilities to be allowed to associate
3240		}
3241#endif
3242
3243#ifdef RTCONFIG_BCMWL6
3244		if (nvram_match(strcat_r(prefix, "bw", tmp), "0"))			// Auto
3245		{
3246			if (nvram_match(strcat_r(prefix, "nband", tmp), "2"))		// 2.4G
3247			{
3248				if (nvram_match(strcat_r(prefix, "nmode", tmp), "-1"))
3249					nvram_set(strcat_r(prefix, "bw_cap", tmp), "3");// 40M
3250				else
3251					nvram_set(strcat_r(prefix, "bw_cap", tmp), "1");// 20M
3252			}
3253			else
3254			{
3255				if (nvram_match(strcat_r(prefix, "phytype", tmp), "v") &&
3256					nvram_match(strcat_r(prefix, "vreqd", tmp), "1"))
3257					nvram_set(strcat_r(prefix, "bw_cap", tmp), "7");// 80M
3258				else if (nvram_match(strcat_r(prefix, "nmode", tmp), "-1"))
3259					nvram_set(strcat_r(prefix, "bw_cap", tmp), "3");// 40M
3260				else
3261					nvram_set(strcat_r(prefix, "bw_cap", tmp), "1");// 20M
3262			}
3263
3264			nvram_set_int(strcat_r(prefix, "obss_coex", tmp),
3265				nvram_match(strcat_r(prefix, "nband", tmp2), "2") ? 1 : 0);
3266		}
3267		else if (nvram_match(strcat_r(prefix, "bw", tmp), "1") ||	// 20M
3268			 nvram_match(strcat_r(prefix, "nmcsidx", tmp), "-2"))
3269		{
3270			nvram_set(strcat_r(prefix, "bw_cap", tmp), "1");
3271			nvram_set(strcat_r(prefix, "obss_coex", tmp), "0");
3272		}
3273		else if (nvram_match(strcat_r(prefix, "bw", tmp), "2") &&	// 40M
3274			 nvram_match(strcat_r(prefix, "nmode", tmp2), "-1"))
3275		{
3276			nvram_set(strcat_r(prefix, "bw_cap", tmp), "3");
3277			nvram_set(strcat_r(prefix, "obss_coex", tmp), "0");
3278		}
3279		else if (nvram_match(strcat_r(prefix, "bw", tmp), "3") &&	// 80M
3280			 nvram_match(strcat_r(prefix, "vreqd", tmp2), "1"))
3281		{
3282			if (nvram_match(strcat_r(prefix, "nband", tmp), "2"))	// 2.4G
3283				nvram_set(strcat_r(prefix, "bw_cap", tmp), "3");
3284			else
3285				nvram_set(strcat_r(prefix, "bw_cap", tmp), "7");
3286			nvram_set(strcat_r(prefix, "obss_coex", tmp), "0");
3287		}
3288		else
3289		{
3290			nvram_set(strcat_r(prefix, "bw_cap", tmp), "1");
3291			nvram_set_int(strcat_r(prefix, "obss_coex", tmp),
3292				nvram_match(strcat_r(prefix, "nband", tmp2), "2") ? 1 : 0);
3293		}
3294
3295		if (nvram_match(strcat_r(prefix, "txbf", tmp), "1"))
3296		{
3297#ifdef RTCONFIG_MUMIMO
3298			if (nvram_match(strcat_r(prefix, "mumimo", tmp), "1"))
3299			{
3300				nvram_set(strcat_r(prefix, "txbf_bfr_cap", tmp), "2");
3301				nvram_set(strcat_r(prefix, "txbf_bfe_cap", tmp), "2");
3302			} else {
3303#endif
3304				nvram_set(strcat_r(prefix, "txbf_bfr_cap", tmp), "1");
3305				nvram_set(strcat_r(prefix, "txbf_bfe_cap", tmp), "1");
3306#ifdef RTCONFIG_MUMIMO
3307			}
3308#endif
3309		}
3310		else
3311		{
3312			nvram_set(strcat_r(prefix, "txbf_bfr_cap", tmp), "0");
3313			nvram_set(strcat_r(prefix, "txbf_bfe_cap", tmp), "0");
3314		}
3315
3316#ifdef RTCONFIG_MUMIMO
3317#ifdef RTCONFIG_PROXYSTA
3318		/* mu_feature is not enabled for client modes. */
3319        	if (is_psta(unit) || is_psr(unit)) {
3320			nvram_set(strcat_r(prefix, "mu_features", tmp), "0");
3321		} else {
3322#endif
3323		if (nvram_match(strcat_r(prefix, "mumimo", tmp), "1"))
3324			nvram_set(strcat_r(prefix, "mu_features", tmp), "0x8000");
3325		else
3326			nvram_set(strcat_r(prefix, "mu_features", tmp), "0");
3327#ifdef RTCONFIG_PROXYSTA
3328		}
3329#endif
3330#endif
3331
3332#ifdef RTCONFIG_BCMARM
3333		nvram_set(strcat_r(prefix, "txbf_imp", tmp), nvram_safe_get(strcat_r(prefix, "itxbf", tmp2)));
3334#endif
3335#else
3336		if (nvram_match(strcat_r(prefix, "bw", tmp), "1"))
3337		{
3338			nvram_set(strcat_r(prefix, "nbw_cap", tmp), "1");
3339			nvram_set(strcat_r(prefix, "obss_coex", tmp), "1");
3340		}
3341		else if (nvram_match(strcat_r(prefix, "bw", tmp), "2"))
3342		{
3343			nvram_set(strcat_r(prefix, "nbw_cap", tmp), "1");
3344			nvram_set(strcat_r(prefix, "obss_coex", tmp), "0");
3345		}
3346		else
3347		{
3348			nvram_set(strcat_r(prefix, "nbw_cap", tmp), "0");
3349			nvram_set(strcat_r(prefix, "obss_coex", tmp), "1");
3350		}
3351
3352		if (unit) chanspec_fix_5g(unit);
3353
3354		match = 0;
3355		foreach (word, "lower upper", next)
3356		{
3357			if (nvram_match(strcat_r(prefix, "nctrlsb", tmp), word))
3358			{
3359				match = 1;
3360				break;
3361			}
3362		}
3363/*
3364		if ((nvram_match(strcat_r(prefix, "channel", tmp), "0")))
3365			nvram_unset(strcat_r(prefix, "nctrlsb", tmp));
3366		else
3367*/
3368		if (!match)
3369			nvram_set(strcat_r(prefix, "nctrlsb", tmp), "lower");
3370#endif
3371
3372#ifdef RTCONFIG_EMF
3373		/* Wireless IGMP Snooping */
3374		i = nvram_get_int(strcat_r(prefix, "igs", tmp));
3375		nvram_set_int(strcat_r(prefix, "wmf_bss_enable", tmp), i ? 1 : 0);
3376#ifdef RTCONFIG_BCMWL6
3377		nvram_set_int(strcat_r(prefix, "wmf_ucigmp_query", tmp), 1);
3378		nvram_set_int(strcat_r(prefix, "wmf_mdata_sendup", tmp), 1);
3379#ifdef RTCONFIG_BCMARM
3380		nvram_set_int(strcat_r(prefix, "wmf_ucast_upnp", tmp), 1);
3381		nvram_set_int(strcat_r(prefix, "wmf_igmpq_filter", tmp), 1);
3382#endif
3383		nvram_set_int(strcat_r(prefix, "acs_fcs_mode", tmp), /*i && nvram_match(strcat_r(prefix, "nband", tmp2), "1") ? 1 :*/ 0);
3384		nvram_set_int(strcat_r(prefix, "dcs_csa_unicast", tmp), i ? 1 : 0);
3385#endif
3386#else // RTCONFIG_EMF
3387		nvram_set_int(strcat_r(prefix, "wmf_bss_enable", tmp), 0);
3388#ifdef RTCONFIG_BCMWL6
3389		nvram_set_int(strcat_r(prefix, "wmf_ucigmp_query", tmp), 0);
3390		nvram_set_int(strcat_r(prefix, "wmf_mdata_sendup", tmp), 0);
3391#ifdef RTCONFIG_BCMARM
3392		nvram_set_int(strcat_r(prefix, "wmf_ucast_upnp", tmp), 0);
3393		nvram_set_int(strcat_r(prefix, "wmf_igmpq_filter", tmp), 0);
3394#endif
3395		nvram_set_int(strcat_r(prefix, "acs_fcs_mode", tmp), 0);
3396		nvram_set_int(strcat_r(prefix, "dcs_csa_unicast", tmp), 0);
3397#endif
3398#endif // RTCONFIG_EMF
3399
3400		sprintf(tmp2, "%d", nvram_get_int(strcat_r(prefix, "pmk_cache", tmp)) * 60);
3401		nvram_set(strcat_r(prefix, "net_reauth", tmp), tmp2);
3402
3403		wl_dfs_support(unit);
3404
3405#if defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)
3406		if (nvram_get_int("smart_connect_x"))
3407			nvram_set_int(strcat_r(prefix, "probresp_sw", tmp), 1);
3408		else
3409			nvram_set_int(strcat_r(prefix, "probresp_sw", tmp), 0);
3410#endif
3411
3412		dbG("bw: %s\n", nvram_safe_get(strcat_r(prefix, "bw", tmp)));
3413#ifdef RTCONFIG_BCMWL6
3414		dbG("chanspec: %s\n", nvram_safe_get(strcat_r(prefix, "chanspec", tmp)));
3415		dbG("bw_cap: %s\n", nvram_safe_get(strcat_r(prefix, "bw_cap", tmp)));
3416#else
3417		dbG("channel: %s\n", nvram_safe_get(strcat_r(prefix, "channel", tmp)));
3418		dbG("nbw_cap: %s\n", nvram_safe_get(strcat_r(prefix, "nbw_cap", tmp)));
3419		dbG("nctrlsb: %s\n", nvram_safe_get(strcat_r(prefix, "nctrlsb", tmp)));
3420#endif
3421		dbG("obss_coex: %s\n", nvram_safe_get(strcat_r(prefix, "obss_coex", tmp)));
3422	}
3423	else
3424	{
3425		if (nvram_match(strcat_r(prefix, "bss_enabled", tmp), "1"))
3426		{
3427			nvram_set(strcat_r(prefix, "bss_maxassoc", tmp), nvram_safe_get(strcat_r(prefix2, "bss_maxassoc", tmp2)));
3428			nvram_set(strcat_r(prefix, "ap_isolate", tmp), nvram_safe_get(strcat_r(prefix2, "ap_isolate", tmp2)));
3429			nvram_set(strcat_r(prefix, "net_reauth", tmp), nvram_safe_get(strcat_r(prefix2, "net_reauth", tmp2)));
3430			nvram_set(strcat_r(prefix, "radius_ipaddr", tmp), nvram_safe_get(strcat_r(prefix2, "radius_ipaddr", tmp2)));
3431			nvram_set(strcat_r(prefix, "radius_key", tmp), nvram_safe_get(strcat_r(prefix2, "radius_key", tmp2)));
3432			nvram_set(strcat_r(prefix, "radius_port", tmp), nvram_safe_get(strcat_r(prefix2, "radius_port", tmp2)));
3433			nvram_set(strcat_r(prefix, "wme", tmp), nvram_safe_get(strcat_r(prefix2, "wme", tmp2)));
3434			nvram_set(strcat_r(prefix, "wme_bss_disable", tmp), nvram_safe_get(strcat_r(prefix2, "wme_bss_disable", tmp2)));
3435			nvram_set(strcat_r(prefix, "wpa_gtk_rekey", tmp), nvram_safe_get(strcat_r(prefix2, "wpa_gtk_rekey", tmp2)));
3436			nvram_set(strcat_r(prefix, "wmf_bss_enable", tmp), nvram_safe_get(strcat_r(prefix2, "wmf_bss_enable", tmp2)));
3437
3438			if (!nvram_match(strcat_r(prefix, "macmode", tmp), "disabled") &&
3439				nvram_match(strcat_r(prefix, "mode", tmp2), "ap")) {
3440				nv = nvp = strdup(nvram_safe_get(strcat_r(prefix, "maclist_x", tmp)));
3441				list = (char*) malloc(sizeof(char) * (strlen(nv)+1));
3442				list[0] = 0;
3443
3444				if (nv) {
3445					while ((b = strsep(&nvp, "<")) != NULL) {
3446						if (strlen(b) == 0) continue;
3447						if (list[0] == 0)
3448							sprintf(list, "%s", b);
3449						else
3450							sprintf(list, "%s %s", list, b);
3451					}
3452					free(nv);
3453				}
3454				nvram_set(strcat_r(prefix, "maclist", tmp), list);
3455				free(list);
3456			}
3457			else
3458				nvram_set(strcat_r(prefix, "maclist", tmp), "");
3459
3460#if defined(RTCONFIG_BCM7) || defined(RTCONFIG_BCM_7114)
3461			if (nvram_get_int("smart_connect_x"))
3462				nvram_set_int(strcat_r(prefix, "probresp_sw", tmp), 1);
3463			else
3464				nvram_set_int(strcat_r(prefix, "probresp_sw", tmp), 0);
3465#endif
3466		}
3467		else
3468		{
3469			nvram_set(strcat_r(prefix, "macmode", tmp), "disabled");
3470			nvram_set(strcat_r(prefix, "maclist", tmp), "");
3471		}
3472	}
3473
3474	/* Disable nmode for WEP and TKIP for TGN spec */
3475	if (nvram_match(strcat_r(prefix, "wep", tmp), "enabled") ||
3476		(nvram_invmatch(strcat_r(prefix, "akm", tmp), "") &&
3477		 nvram_match(strcat_r(prefix, "crypto", tmp), "tkip")))
3478	{
3479#ifdef RTCONFIG_BCMWL6
3480		if (subunit == -1)
3481		{
3482			strcpy(tmp2, nvram_safe_get(strcat_r(prefix, "chanspec", tmp)));
3483			if ((nvp = strchr(tmp2, '/')) || (nvp = strchr(tmp2, 'l'))
3484				|| (nvp = strchr(tmp2, 'u')))
3485			{
3486				*nvp = '\0';
3487				nvram_set(strcat_r(prefix, "chanspec", tmp), tmp2);
3488			}
3489		}
3490#endif
3491		snprintf(prefix, sizeof(prefix), "wl%d_", unit);
3492
3493		nvram_set(strcat_r(prefix, "nmcsidx", tmp), "-2");	// legacy rate
3494		nvram_set(strcat_r(prefix, "nmode", tmp), "0");
3495#ifndef RTCONFIG_BCMWL6
3496		nvram_set(strcat_r(prefix, "nreqd", tmp), "0");
3497#endif
3498		if (nvram_match(strcat_r(prefix, "nband", tmp), "2"))
3499		nvram_set(strcat_r(prefix, "gmode", tmp), "1");
3500		nvram_set(strcat_r(prefix, "bw", tmp), "1");		// reset to default setting
3501#ifdef RTCONFIG_BCMWL6
3502		nvram_set(strcat_r(prefix, "bw_cap", tmp), "1");
3503#else
3504		nvram_set(strcat_r(prefix, "nbw_cap", tmp), "0");
3505#endif
3506#ifdef RTCONFIG_BCMWL6
3507		nvram_set(strcat_r(prefix, "bss_opmode_cap_reqd", tmp), "0");	// no requirements on joining devices
3508#endif
3509	}
3510#ifdef RTCONFIG_QTN
3511	if (nvram_get_int("qtn_ready") == 1) {
3512		runtime_config_qtn(unit, subunit);
3513	}
3514#endif
3515}
3516
3517#define BCM5325_ventry(vid, inet_vid, iptv_vid, voip_vid) ( \
3518	0x01000000 | (vid << 12) | (1 << ports[SWPORT_WAN]) |		\
3519	((vid == inet_vid) ? (0x01 << ports[SWPORT_CPU]) : 0) |		\
3520	((vid == iptv_vid) ? (0x41 << ports[SWPORT_LAN4]) : 0) |	\
3521	((vid == voip_vid) ? (0x41 << ports[SWPORT_LAN3]) : 0)		\
3522)
3523
3524void
3525set_wan_tag(char *interface) {
3526	int model, wan_vid, iptv_vid, voip_vid, wan_prio, iptv_prio, voip_prio, switch_stb;
3527	char wan_dev[sizeof("vlan4096")], port_id[7];
3528	char tag_register[sizeof("0xffffffff")], vlan_entry[sizeof("0xffffffff")];
3529	int gmac3_enable = 0;
3530
3531	model = get_model();
3532	wan_vid = nvram_get_int("switch_wan0tagid") & 0x0fff;
3533	iptv_vid = nvram_get_int("switch_wan1tagid") & 0x0fff;
3534	voip_vid = nvram_get_int("switch_wan2tagid") & 0x0fff;
3535	wan_prio = nvram_get_int("switch_wan0prio") & 0x7;
3536	iptv_prio = nvram_get_int("switch_wan1prio") & 0x7;
3537	voip_prio = nvram_get_int("switch_wan2prio") & 0x7;
3538#ifdef RTCONFIG_MULTICAST_IPTV
3539	int mang_vid = nvram_get_int("switch_wan3tagid") & 0x0fff;
3540	int mang_prio = nvram_get_int("switch_wan3prio") & 0x7;
3541#endif
3542#ifdef RTCONFIG_GMAC3
3543	gmac3_enable = nvram_get_int("gmac3_enable");
3544#endif
3545	switch_stb = nvram_get_int("switch_stb_x");
3546
3547	sprintf(wan_dev, "vlan%d", wan_vid);
3548
3549	switch(model) {
3550				/* WAN L1 L2 L3 L4 CPU */
3551	case MODEL_RTN53:	/* P4  P3 P2 P1 P0 P5 */
3552	case MODEL_RTN12:	/* P4  P3 P2 P1 P0 P5 */
3553	case MODEL_RTN12B1:	/* P4  P3 P2 P1 P0 P5 */
3554	case MODEL_RTN12C1:	/* P4  P3 P2 P1 P0 P5 */
3555	case MODEL_RTN12D1:	/* P4  P3 P2 P1 P0 P5 */
3556	case MODEL_RTN12VP:	/* P4  P3 P2 P1 P0 P5 */
3557	case MODEL_RTN12HP:	/* P4  P3 P2 P1 P0 P5 */
3558	case MODEL_RTN12HP_B1:	/* P4  P3 P2 P1 P0 P5 */
3559	case MODEL_APN12HP:	/* P4  P3 P2 P1 P0 P5 */
3560	case MODEL_RTN10P:	/* P4  P3 P2 P1 P0 P5 */
3561	case MODEL_RTN10D1:	/* P4  P3 P2 P1 P0 P5 */
3562	case MODEL_RTN10PV2:	/* P4  P3 P2 P1 P0 P5 */
3563		/* Enable high bits check */
3564		eval("et", "robowr", "0x34", "0x3", "0x0080");
3565		/* Config WAN port */
3566		if (wan_vid) {
3567			eval("vconfig", "rem", "vlan1");
3568			eval("et", "robowr", "0x34", "0x8", "0x01001000");
3569			eval("et", "robowr", "0x34", "0x6", "0x3001");
3570			sprintf(port_id, "%d", wan_vid);
3571			eval("vconfig", "add", interface, port_id);
3572		}
3573		/* Set Wan prio*/
3574		if (!nvram_match("switch_wan0prio", "0"))
3575			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
3576
3577		if (nvram_match("switch_wantag", "unifi_home")) {
3578			/* vlan0ports= 1 2 3 5 */
3579			eval("et", "robowr", "0x34", "0x8", "0x010003ae");
3580			eval("et", "robowr", "0x34", "0x6", "0x3000");
3581			/* vlan500ports= 4 5 */
3582			eval("et", "robowr", "0x34", "0x8", "0x011f4030");
3583			eval("et", "robowr", "0x34", "0x6", "0x31f4");
3584			/* vlan600ports= 0 4 */
3585			eval("et", "robowr", "0x34", "0x8", "0x01258051");
3586			eval("et", "robowr", "0x34", "0x6", "0x3258");
3587			/* LAN4 vlan tag */
3588			eval("et", "robowr", "0x34", "0x10", "0x0258");
3589		}
3590		else if (nvram_match("switch_wantag", "unifi_biz")) {
3591			/* Modify vlan500ports= 4 5 */
3592			eval("et", "robowr", "0x34", "0x8", "0x011f4030");
3593			eval("et", "robowr", "0x34", "0x6", "0x31f4");
3594		}
3595		else if (nvram_match("switch_wantag", "singtel_mio")) {
3596			/* vlan0ports= 2 3 5 */
3597			eval("et", "robowr", "0x34", "0x8", "0x0100032c");
3598			eval("et", "robowr", "0x34", "0x6", "0x3000");
3599			/* vlan10ports= 4 5 */
3600			eval("et", "robowr", "0x34", "0x8", "0x0100a030");
3601			eval("et", "robowr", "0x34", "0x6", "0x300a");
3602			/* vlan20ports= 0 4 */
3603			eval("et", "robowr", "0x34", "0x8", "0x01014051");
3604			eval("et", "robowr", "0x34", "0x6", "0x3014");
3605			/* vlan30ports= 1 4 */
3606			eval("et", "robowr", "0x34", "0x8", "0x0101e012");	/*Just forward without untag*/
3607			eval("et", "robowr", "0x34", "0x6", "0x301e");
3608			/* LAN4 vlan tag & prio */
3609			eval("et", "robowr", "0x34", "0x10", "0x8014");
3610		}
3611		else if (nvram_match("switch_wantag", "singtel_others")) {
3612			/* vlan0ports= 1 2 3 5 */
3613			eval("et", "robowr", "0x34", "0x8", "0x010003ae");
3614			eval("et", "robowr", "0x34", "0x6", "0x3000");
3615			/* vlan10ports= 4 5 */
3616			eval("et", "robowr", "0x34", "0x8", "0x0100a030");
3617			eval("et", "robowr", "0x34", "0x6", "0x300a");
3618			/* vlan20ports= 0 4 */
3619			eval("et", "robowr", "0x34", "0x8", "0x01014051");
3620			eval("et", "robowr", "0x34", "0x6", "0x3014");
3621			/* LAN4 vlan tag & prio */
3622			eval("et", "robowr", "0x34", "0x10", "0x8014");
3623		}
3624		else if (nvram_match("switch_wantag", "m1_fiber")) {
3625			/* vlan0ports= 0 2 3 5 */				/*5432 1054 3210*/
3626			eval("et", "robowr", "0x34", "0x8", "0x0100036d");	/*0011|0110|1101*/
3627			eval("et", "robowr", "0x34", "0x6", "0x3000");
3628			/* vlan1103ports= 4 5 */
3629			eval("et", "robowr", "0x34", "0x8", "0x0144f030");	/*0000|0011|0000*/
3630			eval("et", "robowr", "0x34", "0x6", "0x344f");
3631			/* vlan1107ports= 1 4 */
3632			eval("et", "robowr", "0x34", "0x8", "0x01453012");	/*0000|0001|0010*/ /*Just forward without untag*/
3633			eval("et", "robowr", "0x34", "0x6", "0x3453");
3634		}
3635		else if (nvram_match("switch_wantag", "maxis_fiber")) {
3636			/* vlan0 ports= 0 2 3 5 */				/*5432 1054 3210*/
3637			eval("et", "robowr", "0x34", "0x8", "0x0100036d");	/*0011|0110|1101*/
3638			eval("et", "robowr", "0x34", "0x6", "0x3000");
3639			/* vlan621 ports= 4 5 */
3640			eval("et", "robowr", "0x34", "0x8", "0x0126d030");	/*0000|0011|0000*/
3641			eval("et", "robowr", "0x34", "0x6", "0x326d");
3642			/* vlan821/822 ports= 1 4 */
3643			eval("et", "robowr", "0x34", "0x8", "0x01335012");	/*0000|0001|0010*/ /*Just forward without untag*/
3644			eval("et", "robowr", "0x34", "0x6", "0x3335");
3645			eval("et", "robowr", "0x34", "0x8", "0x01336012");	/*0000|0001|0010*/ /*Just forward without untag*/
3646			eval("et", "robowr", "0x34", "0x6", "0x3336");
3647		}
3648		else if (nvram_match("switch_wantag", "maxis_fiber_sp")) {
3649			/* vlan0ports= 0 2 3 5 */				/*5432 1054 3210*/
3650			eval("et", "robowr", "0x34", "0x8", "0x0100036d");	/*0011|0110|1101*/
3651			eval("et", "robowr", "0x34", "0x6", "0x3000");
3652			/* vlan11ports= 4 5 */
3653			eval("et", "robowr", "0x34", "0x8", "0x0100b030");	/*0000|0011|0000*/
3654			eval("et", "robowr", "0x34", "0x6", "0x300b");
3655			/* vlan14ports= 1 4 */
3656			eval("et", "robowr", "0x34", "0x8", "0x0100e012");	/*0000|0001|0010*/ /*Just forward without untag*/
3657			eval("et", "robowr", "0x34", "0x6", "0x300e");
3658		}
3659		else {	/* manual */
3660							/* WAN L1 L2 L3 L4 CPU */
3661			const int ports[SWPORT_COUNT] = { 4, 3, 2, 1, 0, 5 };
3662
3663			if (switch_stb != SWCFG_STB4 && switch_stb != SWCFG_STB34)
3664				iptv_vid = 0;
3665			if (switch_stb != SWCFG_STB3 && switch_stb != SWCFG_STB34)
3666				voip_vid = 0;
3667			if (wan_vid) {
3668				sprintf(vlan_entry, "0x%x", BCM5325_ventry(wan_vid, wan_vid, iptv_vid, voip_vid));
3669				eval("et", "robowr", "0x34", "0x8", vlan_entry);
3670				eval("et", "robowr", "0x34", "0x6", "0x3001");
3671			}
3672			if (iptv_vid) {
3673				if (iptv_vid != wan_vid) {
3674					sprintf(vlan_entry, "0x%x", BCM5325_ventry(iptv_vid, wan_vid, iptv_vid, voip_vid));
3675					eval("et", "robowr", "0x34", "0x8", vlan_entry);
3676					eval("et", "robowr", "0x34", "0x6", "0x3002");
3677				}
3678				sprintf(tag_register, "0x%x", (iptv_prio << 13) | iptv_vid);
3679				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN4]);
3680				eval("et", "robowr", "0x34", port_id, tag_register);
3681			}
3682			if (voip_vid) {
3683				if (voip_vid != wan_vid && voip_vid != iptv_vid) {
3684					sprintf(vlan_entry, "0x%x", BCM5325_ventry(voip_vid, wan_vid, iptv_vid, voip_vid));
3685					eval("et", "robowr", "0x34", "0x8", vlan_entry);
3686					eval("et", "robowr", "0x34", "0x6", "0x3003");
3687				}
3688				sprintf(tag_register, "0x%x", (voip_prio << 13) | voip_vid);
3689				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN3]);
3690				eval("et", "robowr", "0x34", port_id, tag_register);
3691			}
3692		}
3693		break;
3694
3695				/* WAN L1 L2 L3 L4 CPU */
3696	case MODEL_RTN14UHP:	/* P4  P0 P1 P2 P3 P5 */
3697		/* Enable high bits check */
3698		eval("et", "robowr", "0x34", "0x3", "0x0080");
3699		/* Config WAN port */
3700		if (wan_vid) {
3701			eval("vconfig", "rem", "vlan1");
3702			eval("et", "robowr", "0x34", "0x8", "0x01001000");
3703			eval("et", "robowr", "0x34", "0x6", "0x3001");
3704			sprintf(port_id, "%d", wan_vid);
3705			eval("vconfig", "add", interface, port_id);
3706		}
3707		/* Set Wan prio*/
3708		if (!nvram_match("switch_wan0prio", "0"))
3709			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
3710
3711		if (nvram_match("switch_wantag", "unifi_home")) {
3712			/* vlan0ports= 0 1 2 5 */
3713			eval("et", "robowr", "0x34", "0x8", "0x010001e7");
3714			eval("et", "robowr", "0x34", "0x6", "0x3000");
3715			/* vlan500ports= 4 5 */
3716			eval("et", "robowr", "0x34", "0x8", "0x011f4030");
3717			eval("et", "robowr", "0x34", "0x6", "0x31f4");
3718			/* vlan600ports= 3 4 */
3719			eval("et", "robowr", "0x34", "0x8", "0x01258218");
3720			eval("et", "robowr", "0x34", "0x6", "0x3258");
3721			/* LAN4 vlan tag */
3722			eval("et", "robowr", "0x34", "0x16", "0x0258");
3723		}
3724		else if (nvram_match("switch_wantag", "unifi_biz")) {
3725			/* Modify vlan500ports= 4 5 */
3726			eval("et", "robowr", "0x34", "0x8", "0x011f4030");
3727			eval("et", "robowr", "0x34", "0x6", "0x31f4");
3728		}
3729		else if (nvram_match("switch_wantag", "singtel_mio")) {
3730			/* vlan0ports= 0 1 5 */
3731			eval("et", "robowr", "0x34", "0x8", "0x010000E3");
3732			eval("et", "robowr", "0x34", "0x6", "0x3000");
3733			/* vlan10ports= 4 5 */
3734			eval("et", "robowr", "0x34", "0x8", "0x0100a030");
3735			eval("et", "robowr", "0x34", "0x6", "0x300a");
3736			/* vlan20ports= 3 4 */
3737			eval("et", "robowr", "0x34", "0x8", "0x01014218");
3738			eval("et", "robowr", "0x34", "0x6", "0x3014");
3739			/* vlan30ports= 2 4 */
3740			eval("et", "robowr", "0x34", "0x8", "0x0101e014");	/*Just forward without untag*/
3741			eval("et", "robowr", "0x34", "0x6", "0x301e");
3742			/* LAN4 vlan tag & prio */
3743			eval("et", "robowr", "0x34", "0x16", "0x8014");
3744		}
3745		else if (nvram_match("switch_wantag", "singtel_others")) {
3746			/* vlan0ports= 0 1 2 5 */
3747			eval("et", "robowr", "0x34", "0x8", "0x010001e7");
3748			eval("et", "robowr", "0x34", "0x6", "0x3000");
3749			/* vlan10ports= 4 5 */
3750			eval("et", "robowr", "0x34", "0x8", "0x0100a030");
3751			eval("et", "robowr", "0x34", "0x6", "0x300a");
3752			/* vlan20ports= 3 4 */
3753			eval("et", "robowr", "0x34", "0x8", "0x01014218");
3754			eval("et", "robowr", "0x34", "0x6", "0x3014");
3755			/* LAN4 vlan tag & prio */
3756			eval("et", "robowr", "0x34", "0x16", "0x8014");
3757		}
3758		else if (nvram_match("switch_wantag", "m1_fiber")) {
3759			/* vlan0ports= 0 1 3 5 */				/*5432 1054 3210*/
3760			eval("et", "robowr", "0x34", "0x8", "0x010002eb");	/*0010|1110|1011*/
3761			eval("et", "robowr", "0x34", "0x6", "0x3000");
3762			/* vlan1103ports= 4 5 */
3763			eval("et", "robowr", "0x34", "0x8", "0x0144f030");	/*0000|0011|0000*/ /*Dont untag WAN port*/
3764			eval("et", "robowr", "0x34", "0x6", "0x344f");
3765			/* vlan1107ports= 2 4 */
3766			eval("et", "robowr", "0x34", "0x8", "0x01453014");	/*0000|0001|0100*/ /*Just forward without untag*/
3767			eval("et", "robowr", "0x34", "0x6", "0x3453");
3768		}
3769		else if (nvram_match("switch_wantag", "maxis_fiber")) {
3770			/* vlan0 ports= 0 1 3 5 */				/*5432 1054 3210*/
3771			eval("et", "robowr", "0x34", "0x8", "0x010002eb");	/*0010|1110|1011*/
3772			eval("et", "robowr", "0x34", "0x6", "0x3000");
3773			/* vlan621 ports= 4 5 */
3774			eval("et", "robowr", "0x34", "0x8", "0x0126d030");	/*0000|0011|0000*/ /*Dont untag WAN port*/
3775			eval("et", "robowr", "0x34", "0x6", "0x326d");
3776			/* vlan821/822 ports= 2 4 */
3777			eval("et", "robowr", "0x34", "0x8", "0x01335014");	/*0000|0001|0100*/ /*Just forward without untag*/
3778			eval("et", "robowr", "0x34", "0x6", "0x3335");
3779			eval("et", "robowr", "0x34", "0x8", "0x01336014");	/*0000|0001|0100*/ /*Just forward without untag*/
3780			eval("et", "robowr", "0x34", "0x6", "0x3336");
3781		}
3782		else if (nvram_match("switch_wantag", "maxis_fiber_sp")) {
3783			/* vlan0ports= 0 1 3 5 */				/*5432 1054 3210*/
3784			eval("et", "robowr", "0x34", "0x8", "0x010002eb");	/*0010|1110|1011*/
3785			eval("et", "robowr", "0x34", "0x6", "0x3000");
3786			/* vlan11ports= 4 5 */
3787			eval("et", "robowr", "0x34", "0x8", "0x0100b030");	/*0000|0011|0000*/ /*Dont untag WAN port*/
3788			eval("et", "robowr", "0x34", "0x6", "0x300b");
3789			/* vlan14ports= 2 4 */
3790			eval("et", "robowr", "0x34", "0x8", "0x0100e014");	/*0000|0000|0101*/ /*Just forward without untag*/
3791			eval("et", "robowr", "0x34", "0x6", "0x300e");
3792		}
3793                else if (nvram_match("switch_wantag", "meo")) {
3794                        /* vlan0ports= 0 1 2 5 */
3795                        eval("et", "robowr", "0x34", "0x8", "0x010001e7");
3796                        eval("et", "robowr", "0x34", "0x6", "0x3000");
3797                        /* vlan12ports= 3 4 5 */				/* untag||forward */
3798                        eval("et", "robowr", "0x34", "0x8", "0x0100c038");      /*0000|0011|1000*/ /*Just forward without untag*/
3799                        eval("et", "robowr", "0x34", "0x6", "0x300c");
3800		}
3801                else if (nvram_match("switch_wantag", "vodafone")) {
3802                        /* vlan0ports= 0 1 2 5t */
3803                        eval("et", "robowr", "0x34", "0x8", "0x010000E3");
3804                        eval("et", "robowr", "0x34", "0x6", "0x3000");
3805                        /* vlan100ports= 3t 4t 5t */
3806                        eval("et", "robowr", "0x34", "0x8", "0x01064038");
3807                        eval("et", "robowr", "0x34", "0x6", "0x3064");
3808                        /* vlan101ports= 3t 4t */
3809                        eval("et", "robowr", "0x34", "0x8", "0x01065018");
3810                        eval("et", "robowr", "0x34", "0x6", "0x3065");
3811                        /* vlan105ports= 2 3t 4t */
3812                        eval("et", "robowr", "0x34", "0x8", "0x0106961C");
3813                        eval("et", "robowr", "0x34", "0x6", "0x3069");
3814
3815                        /* WAN port: tag=100 & prio=1 */
3816                        eval("et", "robowr", "0x34", "0x10", "0x2064");
3817                        /* LAN4: tag=105 & prio=1 */
3818                        eval("et", "robowr", "0x34", "0x16", "0x2069");
3819                }
3820
3821		else {	/* manual */
3822							/* WAN L1 L2 L3 L4 CPU */
3823			const int ports[SWPORT_COUNT] = { 4, 0, 1, 2, 3, 5 };
3824
3825			if (switch_stb != SWCFG_STB4 && switch_stb != SWCFG_STB34)
3826				iptv_vid = 0;
3827			if (switch_stb != SWCFG_STB3 && switch_stb != SWCFG_STB34)
3828				voip_vid = 0;
3829			if (wan_vid) {
3830				sprintf(vlan_entry, "0x%x", BCM5325_ventry(wan_vid, wan_vid, iptv_vid, voip_vid));
3831				eval("et", "robowr", "0x34", "0x8", vlan_entry);
3832				eval("et", "robowr", "0x34", "0x6", "0x3001");
3833			}
3834			if (iptv_vid) {
3835				if (iptv_vid != wan_vid) {
3836					sprintf(vlan_entry, "0x%x", BCM5325_ventry(iptv_vid, wan_vid, iptv_vid, voip_vid));
3837					eval("et", "robowr", "0x34", "0x8", vlan_entry);
3838					eval("et", "robowr", "0x34", "0x6", "0x3002");
3839				}
3840				sprintf(tag_register, "0x%x", (iptv_prio << 13) | iptv_vid);
3841				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN4]);
3842				eval("et", "robowr", "0x34", port_id, tag_register);
3843			}
3844			if (voip_vid) {
3845				if (voip_vid != wan_vid && voip_vid != iptv_vid) {
3846					sprintf(vlan_entry, "0x%x", BCM5325_ventry(voip_vid, wan_vid, iptv_vid, voip_vid));
3847					eval("et", "robowr", "0x34", "0x8", vlan_entry);
3848					eval("et", "robowr", "0x34", "0x6", "0x3003");
3849				}
3850				sprintf(tag_register, "0x%x", (voip_prio << 13) | voip_vid);
3851				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN3]);
3852				eval("et", "robowr", "0x34", port_id, tag_register);
3853			}
3854		}
3855		break;
3856
3857				/* WAN L1 L2 L3 L4 CPU */
3858	case MODEL_RTN10U:	/* P0  P4 P3 P2 P1 P5 */
3859		/* Enable high bits check */
3860		eval("et", "robowr", "0x34", "0x3", "0x0080");
3861		/* Config WAN port */
3862		if (wan_vid) {
3863			eval("vconfig", "rem", "vlan1");
3864			eval("et", "robowr", "0x34", "0x8", "0x01001000");
3865			eval("et", "robowr", "0x34", "0x6", "0x3001");
3866			sprintf(port_id, "%d", wan_vid);
3867			eval("vconfig", "add", interface, port_id);
3868		}
3869		/* Set Wan prio*/
3870		if (!nvram_match("switch_wan0prio", "0"))
3871			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
3872
3873		if (nvram_match("switch_wantag", "unifi_home")) {
3874			/* vlan0ports= 2 3 4 5 */
3875			eval("et", "robowr", "0x34", "0x8", "0x0100073c");
3876			eval("et", "robowr", "0x34", "0x6", "0x3000");
3877			/* vlan500ports= 0 5 */
3878			eval("et", "robowr", "0x34", "0x8", "0x011f4021");
3879			eval("et", "robowr", "0x34", "0x6", "0x31f4");
3880			/* vlan600ports= 0 1 */
3881			eval("et", "robowr", "0x34", "0x8", "0x01258083");
3882			eval("et", "robowr", "0x34", "0x6", "0x3258");
3883			/* LAN4 vlan tag */
3884			eval("et", "robowr", "0x34", "0x12", "0x0258");
3885		}
3886		else if (nvram_match("switch_wantag", "unifi_biz")) {
3887			/* Modify vlan500ports= 0 5 */
3888			eval("et", "robowr", "0x34", "0x8", "0x011f4021");
3889			eval("et", "robowr", "0x34", "0x6", "0x31f4");
3890		}
3891		else if (nvram_match("switch_wantag", "singtel_mio")) {
3892			/* vlan0ports= 3 4 5 */
3893			eval("et", "robowr", "0x34", "0x8", "0x01000638");
3894			eval("et", "robowr", "0x34", "0x6", "0x3000");
3895			/* vlan10ports= 0 5 */
3896			eval("et", "robowr", "0x34", "0x8", "0x0100a021");
3897			eval("et", "robowr", "0x34", "0x6", "0x300a");
3898			/* vlan20ports= 1 0 */
3899			eval("et", "robowr", "0x34", "0x8", "0x01014083");
3900			eval("et", "robowr", "0x34", "0x6", "0x3014");
3901			/* vlan30ports= 2 0 */
3902			eval("et", "robowr", "0x34", "0x8", "0x0101e005");	/*Just forward without untag*/
3903			eval("et", "robowr", "0x34", "0x6", "0x301e");
3904			/* LAN4 vlan tag & prio */
3905			eval("et", "robowr", "0x34", "0x12", "0x8014");
3906		}
3907		else if (nvram_match("switch_wantag", "singtel_others")) {
3908			/* vlan0ports= 2 3 4 5 */
3909			eval("et", "robowr", "0x34", "0x8", "0x0100073c");
3910			eval("et", "robowr", "0x34", "0x6", "0x3000");
3911			/* vlan10ports= 0 5 */
3912			eval("et", "robowr", "0x34", "0x8", "0x0100a021");
3913			eval("et", "robowr", "0x34", "0x6", "0x300a");
3914			/* vlan20ports= 0 1 */
3915			eval("et", "robowr", "0x34", "0x8", "0x01014083");
3916			eval("et", "robowr", "0x34", "0x6", "0x3014");
3917			/* LAN4 vlan tag & prio */
3918			eval("et", "robowr", "0x34", "0x12", "0x8014");
3919		}
3920		else if (nvram_match("switch_wantag", "m1_fiber")) {
3921			/* vlan0ports= 1 3 4 5 */				/*5432 1054 3210*/
3922			eval("et", "robowr", "0x34", "0x8", "0x010006ba");	/*0110|1011|1010*/
3923			eval("et", "robowr", "0x34", "0x6", "0x3000");
3924			/* vlan1103ports= 0 5 */
3925			eval("et", "robowr", "0x34", "0x8", "0x0144f021");	/*0000|0010|0001*/ /*Dont untag WAN port*/
3926			eval("et", "robowr", "0x34", "0x6", "0x344f");
3927			/* vlan1107ports= 2 0 */
3928			eval("et", "robowr", "0x34", "0x8", "0x01453005");	/*0000|0000|0101*/ /*Just forward without untag*/
3929			eval("et", "robowr", "0x34", "0x6", "0x3453");
3930		}
3931		else if (nvram_match("switch_wantag", "maxis_fiber")) {
3932			/* vlan0 ports= 1 3 4 5 */				/*5432 1054 3210*/
3933			eval("et", "robowr", "0x34", "0x8", "0x010006ba");	/*0110|1011|1010*/
3934			eval("et", "robowr", "0x34", "0x6", "0x3000");
3935			/* vlan621 ports= 0 5 */
3936			eval("et", "robowr", "0x34", "0x8", "0x0126d021");	/*0000|0010|0001*/ /*Dont untag WAN port*/
3937			eval("et", "robowr", "0x34", "0x6", "0x326d");
3938			/* vlan821/822 ports= 2 0 */
3939			eval("et", "robowr", "0x34", "0x8", "0x01335005");	/*0000|0000|0101*/ /*Just forward without untag*/
3940			eval("et", "robowr", "0x34", "0x6", "0x3335");
3941			eval("et", "robowr", "0x34", "0x8", "0x01336005");	/*0000|0000|0101*/ /*Just forward without untag*/
3942			eval("et", "robowr", "0x34", "0x6", "0x3336");
3943		}
3944		else if (nvram_match("switch_wantag", "maxis_fiber_sp")) {
3945			/* vlan0ports= 1 3 4 5 */				/*5432 1054 3210*/
3946			eval("et", "robowr", "0x34", "0x8", "0x010006ba");	/*0110|1011|1010*/
3947			eval("et", "robowr", "0x34", "0x6", "0x3000");
3948			/* vlan11ports= 0 5 */
3949			eval("et", "robowr", "0x34", "0x8", "0x0100b021");	/*0000|0010|0001*/ /*Dont untag WAN port*/
3950			eval("et", "robowr", "0x34", "0x6", "0x300b");
3951			/* vlan14ports= 2 0 */
3952			eval("et", "robowr", "0x34", "0x8", "0x0100e005");	/*0000|0000|0101*/ /*Just forward without untag*/
3953			eval("et", "robowr", "0x34", "0x6", "0x300e");
3954		}
3955		else {	/* manual */
3956							/* WAN L1 L2 L3 L4 CPU */
3957			const int ports[SWPORT_COUNT] = { 0, 4, 3, 2, 1, 5 };
3958
3959			if (switch_stb != SWCFG_STB4 && switch_stb != SWCFG_STB34)
3960				iptv_vid = 0;
3961			if (switch_stb != SWCFG_STB3 && switch_stb != SWCFG_STB34)
3962				voip_vid = 0;
3963			if (wan_vid) {
3964				sprintf(vlan_entry, "0x%x", BCM5325_ventry(wan_vid, wan_vid, iptv_vid, voip_vid));
3965				eval("et", "robowr", "0x34", "0x8", vlan_entry);
3966				eval("et", "robowr", "0x34", "0x6", "0x3001");
3967			}
3968			if (iptv_vid) {
3969				if (iptv_vid != wan_vid) {
3970					sprintf(vlan_entry, "0x%x", BCM5325_ventry(iptv_vid, wan_vid, iptv_vid, voip_vid));
3971					eval("et", "robowr", "0x34", "0x8", vlan_entry);
3972					eval("et", "robowr", "0x34", "0x6", "0x3002");
3973				}
3974				sprintf(tag_register, "0x%x", (iptv_prio << 13) | iptv_vid);
3975				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN4]);
3976				eval("et", "robowr", "0x34", port_id, tag_register);
3977			}
3978			if (voip_vid) {
3979				if (voip_vid != wan_vid && voip_vid != iptv_vid) {
3980					sprintf(vlan_entry, "0x%x", BCM5325_ventry(voip_vid, wan_vid, iptv_vid, voip_vid));
3981					eval("et", "robowr", "0x34", "0x8", vlan_entry);
3982					eval("et", "robowr", "0x34", "0x6", "0x3003");
3983				}
3984				sprintf(tag_register, "0x%x", (voip_prio << 13) | voip_vid);
3985				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN3]);
3986				eval("et", "robowr", "0x34", port_id, tag_register);
3987			}
3988		}
3989		break;
3990
3991	case MODEL_RTN16:
3992		// config wan port
3993		if (wan_vid) {
3994			eval("vconfig", "rem", "vlan2");
3995			sprintf(port_id, "%d", wan_vid);
3996			eval("vconfig", "add", interface, port_id);
3997			sprintf(vlan_entry, "0x%x", wan_vid);
3998			eval("et", "robowr", "0x05", "0x83", "0x0101");
3999			eval("et", "robowr", "0x05", "0x81", vlan_entry);
4000			eval("et", "robowr", "0x05", "0x80", "0x0000");
4001			eval("et", "robowr", "0x05", "0x80", "0x0080");
4002		}
4003		// Set Wan port PRIO
4004		if (nvram_invmatch("switch_wan0prio", "0"))
4005			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
4006
4007		if (nvram_match("switch_stb_x", "3")) {
4008			// config LAN 3 = VoIP
4009			if (nvram_match("switch_wantag", "m1_fiber")) {
4010				// Just forward packets between port 0 & 3, without untag
4011				sprintf(vlan_entry, "0x%x", voip_vid);
4012				_dprintf("vlan entry: %s\n", vlan_entry);
4013				eval("et", "robowr", "0x05", "0x83", "0x0005");
4014				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4015				eval("et", "robowr", "0x05", "0x80", "0x0000");
4016				eval("et", "robowr", "0x05", "0x80", "0x0080");
4017			}
4018			else {	// Nomo case, untag it.
4019				voip_prio = voip_prio << 13;
4020				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4021				eval("et", "robowr", "0x34", "0x14", tag_register);
4022				_dprintf("lan 3 tag register: %s\n", tag_register);
4023				// Set vlan table entry register
4024				sprintf(vlan_entry, "0x%x", voip_vid);
4025				_dprintf("vlan entry: %s\n", vlan_entry);
4026				eval("et", "robowr", "0x05", "0x83", "0x0805");
4027				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4028				eval("et", "robowr", "0x05", "0x80", "0x0000");
4029				eval("et", "robowr", "0x05", "0x80", "0x0080");
4030			}
4031		}
4032		else if (nvram_match("switch_stb_x", "4")) {
4033			// config LAN 4 = IPTV
4034			iptv_prio = iptv_prio << 13;
4035			sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4036			eval("et", "robowr", "0x34", "0x12", tag_register);
4037			_dprintf("lan 4 tag register: %s\n", tag_register);
4038			// Set vlan table entry register
4039			sprintf(vlan_entry, "0x%x", iptv_vid);
4040			_dprintf("vlan entry: %s\n", vlan_entry);
4041			eval("et", "robowr", "0x05", "0x83", "0x0403");
4042			eval("et", "robowr", "0x05", "0x81", vlan_entry);
4043			eval("et", "robowr", "0x05", "0x80", "0x0000");
4044			eval("et", "robowr", "0x05", "0x80", "0x0080");
4045		}
4046		else if (nvram_match("switch_stb_x", "6")) {
4047			// config LAN 3 = VoIP
4048			if (nvram_match("switch_wantag", "singtel_mio")) {
4049				// Just forward packets between port 0 & 3, without untag
4050				sprintf(vlan_entry, "0x%x", voip_vid);
4051				_dprintf("vlan entry: %s\n", vlan_entry);
4052				eval("et", "robowr", "0x05", "0x83", "0x0005");
4053				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4054				eval("et", "robowr", "0x05", "0x80", "0x0000");
4055				eval("et", "robowr", "0x05", "0x80", "0x0080");
4056			}
4057			else {	// Nomo case, untag it.
4058			    if (voip_vid) {
4059				voip_prio = voip_prio << 13;
4060				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4061				eval("et", "robowr", "0x34", "0x14", tag_register);
4062				_dprintf("lan 3 tag register: %s\n", tag_register);
4063				// Set vlan table entry register
4064				sprintf(vlan_entry, "0x%x", voip_vid);
4065				_dprintf("vlan entry: %s\n", vlan_entry);
4066				if (voip_vid == iptv_vid)
4067					eval("et", "robowr", "0x05", "0x83", "0x0C07");
4068				else
4069					eval("et", "robowr", "0x05", "0x83", "0x0805");
4070				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4071				eval("et", "robowr", "0x05", "0x80", "0x0000");
4072				eval("et", "robowr", "0x05", "0x80", "0x0080");
4073			    }
4074			}
4075			// config LAN 4 = IPTV
4076			if (iptv_vid) {
4077				iptv_prio = iptv_prio << 13;
4078				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4079				eval("et", "robowr", "0x34", "0x12", tag_register);
4080				_dprintf("lan 4 tag register: %s\n", tag_register);
4081				// Set vlan table entry register
4082				sprintf(vlan_entry, "0x%x", iptv_vid);
4083				_dprintf("vlan entry: %s\n", vlan_entry);
4084				if (voip_vid == iptv_vid)
4085					eval("et", "robowr", "0x05", "0x83", "0x0C07");
4086				else
4087					eval("et", "robowr", "0x05", "0x83", "0x0403");
4088				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4089				eval("et", "robowr", "0x05", "0x80", "0x0000");
4090				eval("et", "robowr", "0x05", "0x80", "0x0080");
4091			}
4092		}
4093		break;
4094
4095				/* P0  P1 P2 P3 P4 P5 */
4096	case MODEL_RTAC3200: 	/* WAN L4 L3 L2 L1 CPU */
4097		if (wan_vid) { /* config wan port */
4098			eval("vconfig", "rem", "vlan2");
4099			sprintf(port_id, "%d", wan_vid);
4100			eval("vconfig", "add", interface, port_id);
4101			sprintf(vlan_entry, "0x%x", wan_vid);
4102			eval("et", "robowr", "0x05", "0x83", "0x0021");
4103			eval("et", "robowr", "0x05", "0x81", vlan_entry);
4104			eval("et", "robowr", "0x05", "0x80", "0x0000");
4105			eval("et", "robowr", "0x05", "0x80", "0x0080");
4106		}
4107		/* Set Wan port PRIO */
4108		if (nvram_invmatch("switch_wan0prio", "0"))
4109			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
4110
4111		if (nvram_match("switch_stb_x", "3")) { // P3
4112			if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
4113                                iptv_prio = iptv_prio << 13;
4114                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4115                                eval("et", "robowr", "0x34", "0x14", tag_register);
4116
4117                                char vlan_cmd[64];
4118                                sprintf(vlan_cmd, "robocfg vlan 1 ports \"2 3 4 5t\"");
4119                                system(vlan_cmd);
4120                                sprintf(vlan_cmd, "robocfg vlan 2 ports \"0 5\"");
4121                                system(vlan_cmd);
4122                                sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 1t 5t\"");
4123                                system(vlan_cmd);
4124                                sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 1t\"");
4125                                system(vlan_cmd);
4126                                sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 1t 2\"");
4127                                system(vlan_cmd);
4128                                break;
4129			}
4130			if (nvram_match("switch_wantag", "m1_fiber") ||
4131			   nvram_match("switch_wantag", "maxis_fiber_sp")
4132			) {
4133				/* Just forward packets between port 0 & 3, without untag */
4134				sprintf(vlan_entry, "0x%x", voip_vid);
4135				_dprintf("vlan entry: %s\n", vlan_entry);
4136				eval("et", "robowr", "0x05", "0x83", "0x0005");
4137				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4138				eval("et", "robowr", "0x05", "0x80", "0x0000");
4139				eval("et", "robowr", "0x05", "0x80", "0x0080");
4140			}
4141			else if (nvram_match("switch_wantag", "maxis_fiber")) {
4142				/* Just forward packets between port 0 & 3, without untag */
4143				eval("et", "robowr", "0x05", "0x83", "0x0005");
4144				eval("et", "robowr", "0x05", "0x81", "0x0335"); /* vlan id=821 */
4145				eval("et", "robowr", "0x05", "0x80", "0x0000");
4146				eval("et", "robowr", "0x05", "0x80", "0x0080");
4147				eval("et", "robowr", "0x05", "0x83", "0x0005");
4148				eval("et", "robowr", "0x05", "0x81", "0x0336"); /* vlan id=822 */
4149				eval("et", "robowr", "0x05", "0x80", "0x0000");
4150				eval("et", "robowr", "0x05", "0x80", "0x0080");
4151			}
4152			else {  /* Nomo case, untag it. */
4153				voip_prio = voip_prio << 13;
4154				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4155				eval("et", "robowr", "0x34", "0x14", tag_register);
4156				_dprintf("lan 3 tag register: %s\n", tag_register);
4157				/* Set vlan table entry register */
4158				sprintf(vlan_entry, "0x%x", voip_vid);
4159				_dprintf("vlan entry: %s\n", vlan_entry);
4160				eval("et", "robowr", "0x05", "0x83", "0x0805");
4161				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4162				eval("et", "robowr", "0x05", "0x80", "0x0000");
4163				eval("et", "robowr", "0x05", "0x80", "0x0080");
4164			}
4165		}
4166		else if (nvram_match("switch_stb_x", "4")) { // P4
4167			/* config LAN 4 = IPTV */
4168			if (nvram_match("switch_wantag", "meo")) {
4169				/* Just forward packets between port 0 & 1, without untag */
4170				sprintf(vlan_entry, "0x%x", iptv_vid);
4171				_dprintf("* vlan entry: %s\n", vlan_entry);
4172				eval("et", "robowr", "0x05", "0x83", "0x0023");
4173				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4174				eval("et", "robowr", "0x05", "0x80", "0x0080");
4175			}
4176			else {  /* Nomo case, untag it. */
4177				/* config LAN 4 = IPTV */
4178				iptv_prio = iptv_prio << 13;
4179				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4180				eval("et", "robowr", "0x34", "0x12", tag_register);
4181				_dprintf("lan 4 tag register: %s\n", tag_register);
4182				/* Set vlan table entry register */
4183				sprintf(vlan_entry, "0x%x", iptv_vid);
4184				_dprintf("vlan entry: %s\n", vlan_entry);
4185				eval("et", "robowr", "0x05", "0x83", "0x0403");
4186				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4187				eval("et", "robowr", "0x05", "0x80", "0x0000");
4188				eval("et", "robowr", "0x05", "0x80", "0x0080");
4189			}
4190		}
4191		else if (nvram_match("switch_stb_x", "6")) {
4192			/* config LAN 3 = VoIP */ // P3
4193			if (nvram_match("switch_wantag", "singtel_mio")) {
4194				/* Just forward packets between port 0 & 3, without untag */
4195				sprintf(vlan_entry, "0x%x", voip_vid);
4196				_dprintf("vlan entry: %s\n", vlan_entry);
4197				eval("et", "robowr", "0x05", "0x83", "0x0005");
4198				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4199				eval("et", "robowr", "0x05", "0x80", "0x0000");
4200				eval("et", "robowr", "0x05", "0x80", "0x0080");
4201			}
4202			else {  /* Nomo case, untag it. */
4203			    if (voip_vid) {
4204				voip_prio = voip_prio << 13;
4205				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4206				eval("et", "robowr", "0x34", "0x14", tag_register);
4207				_dprintf("lan 3 tag register: %s\n", tag_register);
4208				/* Set vlan table entry register */
4209				sprintf(vlan_entry, "0x%x", voip_vid);
4210				_dprintf("vlan entry: %s\n", vlan_entry);
4211				if (voip_vid == iptv_vid)
4212					eval("et", "robowr", "0x05", "0x83", "0x0C07");
4213				else
4214					eval("et", "robowr", "0x05", "0x83", "0x0805");
4215				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4216				eval("et", "robowr", "0x05", "0x80", "0x0000");
4217				eval("et", "robowr", "0x05", "0x80", "0x0080");
4218			    }
4219			}
4220			/* config LAN 4 = IPTV */ // P4
4221			if (iptv_vid) {
4222				iptv_prio = iptv_prio << 13;
4223				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4224				eval("et", "robowr", "0x34", "0x12", tag_register);
4225				_dprintf("lan 4 tag register: %s\n", tag_register);
4226				/* Set vlan table entry register */
4227				sprintf(vlan_entry, "0x%x", iptv_vid);
4228				_dprintf("vlan entry: %s\n", vlan_entry);
4229				if (voip_vid == iptv_vid)
4230					eval("et", "robowr", "0x05", "0x83", "0x0C07");
4231				else
4232					eval("et", "robowr", "0x05", "0x83", "0x0403");
4233				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4234				eval("et", "robowr", "0x05", "0x80", "0x0000");
4235				eval("et", "robowr", "0x05", "0x80", "0x0080");
4236			}
4237		}
4238#ifdef RTCONFIG_MULTICAST_IPTV
4239                if (switch_stb >= 7) {
4240                    if (iptv_vid) { /* config IPTV on wan port */
4241_dprintf("*** Multicast IPTV: config IPTV on wan port ***\n");
4242                        sprintf(wan_dev, "vlan%d", iptv_vid);
4243                        nvram_set("wan10_ifname", wan_dev);
4244                        sprintf(port_id, "%d", iptv_vid);
4245                        eval("vconfig", "add", interface, port_id);
4246                        sprintf(vlan_entry, "0x%x", iptv_vid);
4247                        eval("et", "robowr", "0x05", "0x83", "0x0021");
4248                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
4249                        eval("et", "robowr", "0x05", "0x80", "0x0000");
4250                        eval("et", "robowr", "0x05", "0x80", "0x0080");
4251
4252                        if (iptv_prio) { /* config priority */
4253                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4254                        }
4255                    }
4256                }
4257                if (switch_stb >= 8) {
4258                    if (voip_vid) { /* config voip on wan port */
4259_dprintf("*** Multicast IPTV: config VOIP on wan port ***\n");
4260                        sprintf(wan_dev, "vlan%d", voip_vid);
4261                        nvram_set("wan11_ifname", wan_dev);
4262                        sprintf(port_id, "%d", voip_vid);
4263                        eval("vconfig", "add", interface, port_id);
4264                        sprintf(vlan_entry, "0x%x", voip_vid);
4265                        eval("et", "robowr", "0x05", "0x83", "0x0021");
4266                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
4267                        eval("et", "robowr", "0x05", "0x80", "0x0000");
4268                        eval("et", "robowr", "0x05", "0x80", "0x0080");
4269
4270                        if (voip_prio) { /* config priority */
4271                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
4272                        }
4273                    }
4274                }
4275                if (switch_stb >=9 ) {
4276                    if (mang_vid) { /* config tr069 on wan port */
4277_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
4278                        sprintf(wan_dev, "vlan%d", mang_vid);
4279                        nvram_set("wan12_ifname", wan_dev);
4280                        sprintf(port_id, "%d", mang_vid);
4281                        eval("vconfig", "add", interface, port_id);
4282                        sprintf(vlan_entry, "0x%x", mang_vid);
4283                        eval("et", "robowr", "0x05", "0x83", "0x0021");
4284                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
4285                        eval("et", "robowr", "0x05", "0x80", "0x0000");
4286                        eval("et", "robowr", "0x05", "0x80", "0x0080");
4287
4288                        if (mang_prio) { /* config priority */
4289                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4290                        }
4291                    }
4292                }
4293#endif
4294		break;
4295
4296				/* P0  P1 P2 P3 P4 P5 */
4297	case MODEL_RTAC68U:	/* WAN L1 L2 L3 L4 CPU */
4298	case MODEL_RTN18U:	/* WAN L1 L2 L3 L4 CPU */
4299		if (wan_vid) { /* config wan port */
4300			eval("vconfig", "rem", "vlan2");
4301			sprintf(port_id, "%d", wan_vid);
4302			eval("vconfig", "add", interface, port_id);
4303			sprintf(vlan_entry, "0x%x", wan_vid);
4304			eval("et", "robowr", "0x05", "0x83", "0x0021");
4305			eval("et", "robowr", "0x05", "0x81", vlan_entry);
4306			eval("et", "robowr", "0x05", "0x80", "0x0000");
4307			eval("et", "robowr", "0x05", "0x80", "0x0080");
4308		}
4309		/* Set Wan port PRIO */
4310		if (nvram_invmatch("switch_wan0prio", "0"))
4311			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
4312
4313		if (nvram_match("switch_stb_x", "3")) {	// P3
4314                        if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
4315                                iptv_prio = iptv_prio << 13;
4316                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4317                                eval("et", "robowr", "0x34", "0x16", tag_register);
4318
4319                                char vlan_cmd[64];
4320                                sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 5t\"");
4321                                system(vlan_cmd);
4322                                sprintf(vlan_cmd, "robocfg vlan 2 ports \"0 5\"");
4323                                system(vlan_cmd);
4324                                sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 5t\"");
4325                                system(vlan_cmd);
4326                                sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4327                                system(vlan_cmd);
4328                                sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 3 4t\"");
4329                                system(vlan_cmd);
4330                                break;
4331                        }
4332			if (nvram_match("switch_wantag", "m1_fiber") ||
4333			   nvram_match("switch_wantag", "maxis_fiber_sp")
4334			) {
4335				/* Just forward packets between port 0 & 3, without untag */
4336				sprintf(vlan_entry, "0x%x", voip_vid);
4337				_dprintf("vlan entry: %s\n", vlan_entry);
4338				eval("et", "robowr", "0x05", "0x83", "0x0009");
4339				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4340				eval("et", "robowr", "0x05", "0x80", "0x0000");
4341				eval("et", "robowr", "0x05", "0x80", "0x0080");
4342			}
4343			else if (nvram_match("switch_wantag", "maxis_fiber")) {
4344				/* Just forward packets between port 0 & 3, without untag */
4345				eval("et", "robowr", "0x05", "0x83", "0x0009");
4346				eval("et", "robowr", "0x05", "0x81", "0x0335");	/* vlan id=821 */
4347				eval("et", "robowr", "0x05", "0x80", "0x0000");
4348				eval("et", "robowr", "0x05", "0x80", "0x0080");
4349				eval("et", "robowr", "0x05", "0x83", "0x0009");
4350				eval("et", "robowr", "0x05", "0x81", "0x0336");	/* vlan id=822 */
4351				eval("et", "robowr", "0x05", "0x80", "0x0000");
4352				eval("et", "robowr", "0x05", "0x80", "0x0080");
4353			}
4354			else {	/* Nomo case, untag it. */
4355				voip_prio = voip_prio << 13;
4356				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4357				eval("et", "robowr", "0x34", "0x16", tag_register);
4358				_dprintf("lan 3 tag register: %s\n", tag_register);
4359				/* Set vlan table entry register */
4360				sprintf(vlan_entry, "0x%x", voip_vid);
4361				_dprintf("vlan entry: %s\n", vlan_entry);
4362				eval("et", "robowr", "0x05", "0x83", "0x1009");
4363				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4364				eval("et", "robowr", "0x05", "0x80", "0x0000");
4365				eval("et", "robowr", "0x05", "0x80", "0x0080");
4366			}
4367		}
4368		else if (nvram_match("switch_stb_x", "4")) { // P4
4369			/* config LAN 4 = IPTV */
4370			if (nvram_match("switch_wantag", "meo")) {
4371				/* Just forward packets between port 0 & 4, without untag */
4372				sprintf(vlan_entry, "0x%x", iptv_vid);
4373				_dprintf("vlan entry: %s\n", vlan_entry);
4374				eval("et", "robowr", "0x05", "0x83", "0x0031");
4375				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4376				eval("et", "robowr", "0x05", "0x80", "0x0000");
4377				eval("et", "robowr", "0x05", "0x80", "0x0080");
4378			}
4379			else {  /* Nomo case, untag it. */
4380				iptv_prio = iptv_prio << 13;
4381				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4382				eval("et", "robowr", "0x34", "0x18", tag_register);
4383				_dprintf("lan 4 tag register: %s\n", tag_register);
4384				/* Set vlan table entry register */
4385				sprintf(vlan_entry, "0x%x", iptv_vid);
4386				_dprintf("vlan entry: %s\n", vlan_entry);
4387				eval("et", "robowr", "0x05", "0x83", "0x2011");
4388				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4389				eval("et", "robowr", "0x05", "0x80", "0x0000");
4390				eval("et", "robowr", "0x05", "0x80", "0x0080");
4391			}
4392		}
4393		else if (nvram_match("switch_stb_x", "6")) {
4394			/* config LAN 3 = VoIP */ // P3
4395			if (nvram_match("switch_wantag", "singtel_mio")) {
4396				/* Just forward packets between port 0 & 3, without untag */
4397				sprintf(vlan_entry, "0x%x", voip_vid);
4398				_dprintf("vlan entry: %s\n", vlan_entry);
4399				eval("et", "robowr", "0x05", "0x83", "0x0009");
4400				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4401				eval("et", "robowr", "0x05", "0x80", "0x0000");
4402				eval("et", "robowr", "0x05", "0x80", "0x0080");
4403			}
4404			else {	/* Nomo case, untag it. */
4405			    if (voip_vid) {
4406				voip_prio = voip_prio << 13;
4407				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4408				eval("et", "robowr", "0x34", "0x16", tag_register);
4409				_dprintf("lan 3 tag register: %s\n", tag_register);
4410				/* Set vlan table entry register */
4411				sprintf(vlan_entry, "0x%x", voip_vid);
4412				_dprintf("vlan entry: %s\n", vlan_entry);
4413				if (voip_vid == iptv_vid)
4414					eval("et", "robowr", "0x05", "0x83", "0x3019");
4415				else
4416					eval("et", "robowr", "0x05", "0x83", "0x1009");
4417				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4418				eval("et", "robowr", "0x05", "0x80", "0x0000");
4419				eval("et", "robowr", "0x05", "0x80", "0x0080");
4420			    }
4421			}
4422			/* config LAN 4 = IPTV */ // P4
4423			if (iptv_vid) {
4424				iptv_prio = iptv_prio << 13;
4425				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4426				eval("et", "robowr", "0x34", "0x18", tag_register);
4427				_dprintf("lan 4 tag register: %s\n", tag_register);
4428				/* Set vlan table entry register */
4429				sprintf(vlan_entry, "0x%x", iptv_vid);
4430				_dprintf("vlan entry: %s\n", vlan_entry);
4431				if (voip_vid == iptv_vid)
4432					eval("et", "robowr", "0x05", "0x83", "0x3019");
4433				else
4434					eval("et", "robowr", "0x05", "0x83", "0x2011");
4435				eval("et", "robowr", "0x05", "0x81", vlan_entry);
4436				eval("et", "robowr", "0x05", "0x80", "0x0000");
4437				eval("et", "robowr", "0x05", "0x80", "0x0080");
4438			}
4439		}
4440#ifdef RTCONFIG_MULTICAST_IPTV
4441		if (switch_stb >= 7) {
4442                    if (iptv_vid) { /* config IPTV on wan port */
4443_dprintf("*** Multicast IPTV: config IPTV on wan port ***\n");
4444                        sprintf(wan_dev, "vlan%d", iptv_vid);
4445                        nvram_set("wan10_ifname", wan_dev);
4446                        sprintf(port_id, "%d", iptv_vid);
4447                        eval("vconfig", "add", interface, port_id);
4448                        sprintf(vlan_entry, "0x%x", iptv_vid);
4449                        eval("et", "robowr", "0x05", "0x83", "0x0021");
4450                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
4451                        eval("et", "robowr", "0x05", "0x80", "0x0000");
4452                        eval("et", "robowr", "0x05", "0x80", "0x0080");
4453
4454                        if (iptv_prio) { /* config priority */
4455                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4456                        }
4457                    }
4458                }
4459                if (switch_stb >= 8) {
4460                    if (voip_vid) { /* config voip on wan port */
4461_dprintf("*** Multicast IPTV: config VOIP on wan port ***\n");
4462                        sprintf(wan_dev, "vlan%d", voip_vid);
4463                        nvram_set("wan11_ifname", wan_dev);
4464                        sprintf(port_id, "%d", voip_vid);
4465                        eval("vconfig", "add", interface, port_id);
4466                        sprintf(vlan_entry, "0x%x", voip_vid);
4467                        eval("et", "robowr", "0x05", "0x83", "0x0021");
4468                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
4469                        eval("et", "robowr", "0x05", "0x80", "0x0000");
4470                        eval("et", "robowr", "0x05", "0x80", "0x0080");
4471
4472                        if (voip_prio) { /* config priority */
4473                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
4474                        }
4475                    }
4476		}
4477		if (switch_stb >=9 ) {
4478                    if (mang_vid) { /* config tr069 on wan port */
4479_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
4480                        sprintf(wan_dev, "vlan%d", mang_vid);
4481                        nvram_set("wan12_ifname", wan_dev);
4482                        sprintf(port_id, "%d", mang_vid);
4483                        eval("vconfig", "add", interface, port_id);
4484                        sprintf(vlan_entry, "0x%x", mang_vid);
4485                        eval("et", "robowr", "0x05", "0x83", "0x0021");
4486                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
4487                        eval("et", "robowr", "0x05", "0x80", "0x0000");
4488                        eval("et", "robowr", "0x05", "0x80", "0x0080");
4489
4490                        if (mang_prio) { /* config priority */
4491                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4492                        }
4493                    }
4494                }
4495#endif
4496		break;
4497
4498	case MODEL_RTAC5300:
4499	case MODEL_RTAC5300R:
4500				/* If enable gmac3, CPU port is 8 */
4501#ifdef RTCONFIG_RGMII_BRCM5301X
4502				/* P0  P1 P2 P3 P4 P5 		P7 */
4503				/* WAN L1 L2 L3 L4 L5 L6 L7 L8 	CPU*/
4504#else
4505				/* P0  P1 P2 P3 P4 P5 */
4506				/* WAN L1 L2 L3 L4 CPU*/
4507#endif
4508		if (wan_vid) { /* config wan port */
4509			eval("vconfig", "rem", "vlan2");
4510			sprintf(port_id, "%d", wan_vid);
4511			eval("vconfig", "add", interface, port_id);
4512			sprintf(vlan_entry, "0x%x", wan_vid);
4513
4514			if (gmac3_enable) {
4515				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0101");	/* 8, 0 */
4516			} else {
4517#ifdef RTCONFIG_RGMII_BRCM5301X
4518				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0081");	/* 7, 0 */
4519#else
4520				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0021");	/* 5, 0 */
4521#endif
4522			}
4523			eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4524			eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4525			eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4526		}
4527		/* Set Wan port PRIO */
4528		if (nvram_invmatch("switch_wan0prio", "0"))
4529			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
4530
4531		if (nvram_match("switch_stb_x", "3")) {  // L3:p3, w:p0, c:p7/p5
4532                        if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
4533                                iptv_prio = iptv_prio << 13;
4534                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4535                                eval("et", "robowr", "0x34", "0x16", tag_register);
4536
4537                                char vlan_cmd[64];
4538                                if (gmac3_enable) {
4539                                        sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 5u 7 8t\"");
4540                                        system(vlan_cmd);
4541                                        sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 8t\"");
4542                                        system(vlan_cmd);
4543                                        sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4544                                        system(vlan_cmd);
4545                                        sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 3 4t\"");
4546                                        system(vlan_cmd);
4547                                } else {
4548#ifdef RTCONFIG_RGMII_BRCM5301X
4549	                                sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 7t\"");
4550        	                        system(vlan_cmd);
4551                                	sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 7t\"");
4552	                                system(vlan_cmd);
4553        	                        sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4554                	                system(vlan_cmd);
4555                        	        sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 3 4t\"");
4556                                	system(vlan_cmd);
4557#else
4558	                                sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 5t\"");
4559        	                        system(vlan_cmd);
4560                                	sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 5t\"");
4561	                                system(vlan_cmd);
4562        	                        sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4563                	                system(vlan_cmd);
4564                        	        sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 3 4t\"");
4565                                	system(vlan_cmd);
4566#endif
4567				}
4568                                break;
4569                        }
4570			if (nvram_match("switch_wantag", "m1_fiber") ||
4571			   nvram_match("switch_wantag", "maxis_fiber_sp")
4572			) {
4573				/* Just forward packets between WAN & L3, without untag */
4574				sprintf(vlan_entry, "0x%x", voip_vid);
4575				_dprintf("vlan entry: %s\n", vlan_entry);
4576				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0009");
4577				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4578				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4579				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4580			}
4581			else if (nvram_match("switch_wantag", "maxis_fiber")) {
4582				/* Just forward packets between WAN & L3, without untag */
4583				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0009");
4584				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", "0x0335"); /* vlan id=821 */
4585				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4586				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4587				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0009");
4588				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", "0x0336"); /* vlan id=822 */
4589				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4590				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4591			}
4592			else {  /* Nomo case. */
4593				voip_prio <<= 13;
4594				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4595				eval("et", "-i", "eth0", "robowr", "0x34", "0x16", tag_register);
4596				_dprintf("lan(3) tag register: %s\n", tag_register);
4597				/* untag port map */
4598				sprintf(vlan_entry, "0x%x", voip_vid);
4599				_dprintf("vlan entry: %s\n", vlan_entry);
4600				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x1009");	/* un:p3, f:p0 3*/
4601				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4602				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4603				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4604			}
4605		}
4606		else if (nvram_match("switch_stb_x", "4")) { // lan(4) = P4
4607			/* config LAN 4 = IPTV */
4608			if (nvram_match("switch_wantag", "meo")) {
4609				/* Just forward packets between wan & L4, without untag */
4610				sprintf(vlan_entry, "0x%x", iptv_vid);
4611				_dprintf("vlan entry: %s\n", vlan_entry);
4612
4613				if (gmac3_enable) {
4614					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x00111"); /* fwd: 0 4 8 */
4615				} else {
4616#ifdef RTCONFIG_RGMII_BRCM5301X
4617					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0091"); /* fwd: 0 4 7 */
4618#else
4619					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0031"); /* fwd: 0 4 5 */
4620#endif
4621				}
4622				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4623				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4624				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4625			}
4626			else {  /* Nomo case, untag it. */
4627				/* config LAN 4 = IPTV */
4628				iptv_prio <<= 13;
4629				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4630				eval("et", "-i", "eth0", "robowr", "0x34", "0x18", tag_register);
4631				_dprintf("lan(4) tag register: %s\n", tag_register);
4632				/* untag port map */
4633				sprintf(vlan_entry, "0x%x", iptv_vid);
4634				_dprintf("vlan entry: %s\n", vlan_entry);
4635				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x2011");
4636				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4637				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4638				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4639			}
4640		}
4641		else if (nvram_match("switch_stb_x", "6")) {	// lan(3)=P3, lan(4)=P4
4642			/* config lan(3)/P3 = VoIP */
4643			if (nvram_match("switch_wantag", "singtel_mio")) {
4644				/* Just forward packets between WAN & lan(3), without untag */
4645				sprintf(vlan_entry, "0x%x", voip_vid);
4646				_dprintf("vlan entry: %s\n", vlan_entry);
4647				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0009");		/* f:30 */
4648				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4649				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4650				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4651			}
4652			else {
4653			    if (voip_vid) {
4654				voip_prio <<= 13;
4655				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4656				eval("et", "-i", "eth0", "robowr", "0x34", "0x16", tag_register);	/* p3 */
4657				_dprintf("lan 3 tag register: %s\n", tag_register);
4658				/* untag port map */
4659				sprintf(vlan_entry, "0x%x", voip_vid);
4660				_dprintf("vlan entry: %s\n", vlan_entry);
4661				if (voip_vid == iptv_vid)
4662					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x3019"); /* un:43, f:430*/
4663				else
4664					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x2009"); /* un:3 , f:30 */
4665				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4666				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4667				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4668			    }
4669			}
4670			/* config lan(4)/P4 = IPTV */
4671			if (iptv_vid) {
4672				iptv_prio <<= 13;
4673				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4674				eval("et", "-i", "eth0", "robowr", "0x34", "0x18", tag_register);	/* p4 */
4675				_dprintf("lan 4 tag register: %s\n", tag_register);
4676				/* untag port map */
4677				sprintf(vlan_entry, "0x%x", iptv_vid);
4678				_dprintf("vlan entry: %s\n", vlan_entry);
4679				if (voip_vid == iptv_vid)
4680					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x3019"); /* un:43, f:430*/
4681				else
4682					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x2011");	/* un:4, f:40 */
4683				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4684				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4685				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4686			}
4687		}
4688#ifdef RTCONFIG_MULTICAST_IPTV
4689                if (switch_stb >= 7) {
4690                    if (iptv_vid) { /* config IPTV on wan port */
4691_dprintf("*** Multicast IPTV: config IPTV on wan port ***\n");
4692                        sprintf(wan_dev, "vlan%d", iptv_vid);
4693                        nvram_set("wan10_ifname", wan_dev);
4694                        sprintf(port_id, "%d", iptv_vid);
4695                        eval("vconfig", "add", interface, port_id);
4696                        sprintf(vlan_entry, "0x%x", iptv_vid);
4697			if (gmac3_enable) {
4698				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0101");	/* 8, 0 */
4699			} else {
4700#ifdef RTCONFIG_RGMII_BRCM5301X
4701				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0081");	/* 7, 0 */
4702#else
4703				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0021");	/* 5, 0 */
4704#endif
4705			}
4706                        eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4707                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4708                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4709
4710                        if (iptv_prio) { /* config priority */
4711                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4712                        }
4713                    }
4714                }
4715                if (switch_stb >= 8) {
4716                    if (voip_vid) { /* config voip on wan port */
4717_dprintf("*** Multicast IPTV: config VOIP on wan port ***\n");
4718                        sprintf(wan_dev, "vlan%d", voip_vid);
4719                        nvram_set("wan11_ifname", wan_dev);
4720                        sprintf(port_id, "%d", voip_vid);
4721                        eval("vconfig", "add", interface, port_id);
4722                        sprintf(vlan_entry, "0x%x", voip_vid);
4723			if (gmac3_enable) {
4724				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0101");	/* 8, 0 */
4725			} else {
4726#ifdef RTCONFIG_RGMII_BRCM5301X
4727				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0081");	/* 7, 0 */
4728#else
4729				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0021");	/* 5, 0 */
4730#endif
4731			}
4732                        eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4733                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4734                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4735
4736                        if (voip_prio) { /* config priority */
4737                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
4738                        }
4739                    }
4740                }
4741                if (switch_stb >=9 ) {
4742                    if (mang_vid) { /* config tr069 on wan port */
4743_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
4744                        sprintf(wan_dev, "vlan%d", mang_vid);
4745                        nvram_set("wan12_ifname", wan_dev);
4746                        sprintf(port_id, "%d", mang_vid);
4747                        eval("vconfig", "add", interface, port_id);
4748                        sprintf(vlan_entry, "0x%x", mang_vid);
4749			if (gmac3_enable) {
4750				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0101");	/* 8, 0 */
4751			} else {
4752#ifdef RTCONFIG_RGMII_BRCM5301X
4753				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0081");	/* 7, 0 */
4754#else
4755				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0021");	/* 5, 0 */
4756#endif
4757			}
4758                        eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4759                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4760                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4761
4762                        if (mang_prio) { /* config priority */
4763                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4764                        }
4765                    }
4766                }
4767#endif
4768		break;
4769
4770	case MODEL_RTAC3100:
4771	case MODEL_RTAC88U:
4772				/* If enable gmac3, CPU port is 8 */
4773#ifdef RTCONFIG_RGMII_BRCM5301X
4774				/* P4  P3 P2 P1 P0 P5 		P7 */
4775				/* WAN L1 L2 L3 L4 L5 L6 L7 L8 	CPU*/
4776#else
4777				/* P4  P3 P2 P1 P0 P5 */
4778				/* WAN L1 L2 L3 L4 CPU*/
4779#endif
4780		if (wan_vid) { /* config wan port */
4781			eval("vconfig", "rem", "vlan2");
4782			sprintf(port_id, "%d", wan_vid);
4783			eval("vconfig", "add", interface, port_id);
4784			sprintf(vlan_entry, "0x%x", wan_vid);
4785
4786			if (gmac3_enable) {
4787				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0110");	/* f: 4, 8 */
4788			} else {
4789#ifdef RTCONFIG_RGMII_BRCM5301X
4790				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0090");	/* f: 4, 7 */
4791#else
4792				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0030");	/* f: 4, 5 */
4793#endif
4794			}
4795			eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4796			eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4797			eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4798		}
4799		/* Set Wan port PRIO */
4800		if (nvram_invmatch("switch_wan0prio", "0"))
4801			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
4802
4803		if (nvram_match("switch_stb_x", "3")) {  // L3:p1 w:p4 c:p7/p5
4804                        if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
4805                                iptv_prio = iptv_prio << 13;
4806                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4807                                eval("et", "robowr", "0x34", "0x12", tag_register);
4808
4809                                char vlan_cmd[64];
4810	                        if (gmac3_enable) {
4811        	                        sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 5u 7 8t\"");
4812                	                system(vlan_cmd);
4813	                                sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 8t\"");
4814          	 	                system(vlan_cmd);
4815                        	        sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4816                                	system(vlan_cmd);
4817	                                sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 1 4t\"");
4818        	                        system(vlan_cmd);
4819                	        } else {
4820#ifdef RTCONFIG_RGMII_BRCM5301X
4821                        	        sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 7t\"");
4822	                                system(vlan_cmd);
4823                        	        sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 7t\"");
4824                                	system(vlan_cmd);
4825	                                sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4826        	                        system(vlan_cmd);
4827                	                sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 1 4t\"");
4828                        	        system(vlan_cmd);
4829#else
4830                                	sprintf(vlan_cmd, "robocfg vlan 1 ports \"1 2 3 5t\"");
4831	                                system(vlan_cmd);
4832                        	        sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 4t 5t\"");
4833                                	system(vlan_cmd);
4834	                                sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 4t\"");
4835        	                        system(vlan_cmd);
4836	                                sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 1 4t\"");
4837        	                        system(vlan_cmd);
4838#endif
4839				}
4840                                break;
4841                        }
4842			if (nvram_match("switch_wantag", "m1_fiber") ||
4843			   nvram_match("switch_wantag", "maxis_fiber_sp")
4844			) {
4845				/* Just forward packets between WAN & L3, without untag */
4846				sprintf(vlan_entry, "0x%x", voip_vid);
4847				_dprintf("vlan entry: %s\n", vlan_entry);
4848				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0012");	/* f: 4 1 */
4849				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4850				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4851				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4852			}
4853			else if (nvram_match("switch_wantag", "maxis_fiber")) {
4854				/* Just forward packets between WAN & L3, without untag */
4855				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0012");
4856				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", "0x0335"); /* vlan id=821 */
4857				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4858				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4859				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0012");
4860				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", "0x0336"); /* vlan id=822 */
4861				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4862				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4863			}
4864			else {  /* Nomo case. */
4865				voip_prio <<= 13;
4866				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4867				eval("et", "-i", "eth0", "robowr", "0x34", "0x12", tag_register);
4868				_dprintf("lan(3) tag register: %s\n", tag_register);
4869				/* untag port map */
4870				sprintf(vlan_entry, "0x%x", voip_vid);
4871				_dprintf("vlan entry: %s\n", vlan_entry);
4872				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0412");	/* un:1, f:41*/
4873				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4874				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4875				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4876			}
4877		}
4878		else if (nvram_match("switch_stb_x", "4")) { // L4:p0
4879			/* config LAN 4 = IPTV */
4880			if (nvram_match("switch_wantag", "meo")) {
4881				/* Just forward packets between wan & L4, without untag */
4882				sprintf(vlan_entry, "0x%x", iptv_vid);
4883				_dprintf("vlan entry: %s\n", vlan_entry);
4884
4885				if (gmac3_enable) {
4886					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0111");	/* fwd: 0 4 8 */
4887				} else {
4888#ifdef RTCONFIG_RGMII_BRCM5301X
4889					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0091");	/* fwd: 0 4 7 */
4890#else
4891					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0031");	/* fwd: 0 4 5 */
4892#endif
4893				}
4894				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4895				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4896				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4897			}
4898			else {  /* Nomo case, untag it. */
4899				/* config LAN 4 = IPTV */
4900				iptv_prio = iptv_prio << 13;
4901				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4902				eval("et", "-i", "eth0", "robowr", "0x34", "0x10", tag_register);
4903				_dprintf("lan(4) tag register: %s\n", tag_register);
4904				/* untag port map */
4905				sprintf(vlan_entry, "0x%x", iptv_vid);
4906				_dprintf("vlan entry: %s\n", vlan_entry);
4907				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0211");	/* un:0 f:40 */
4908				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4909				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4910				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4911			}
4912		}
4913		else if (nvram_match("switch_stb_x", "6")) {	// L3/p1, L4/p0
4914			/* config L3/p1 = VoIP */
4915			if (nvram_match("switch_wantag", "singtel_mio")) {
4916				/* Just forward packets between WAN & L3, without untag */
4917				sprintf(vlan_entry, "0x%x", voip_vid);
4918				_dprintf("vlan entry: %s\n", vlan_entry);
4919				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0012");		/* f:41 */
4920				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4921				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4922				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4923			}
4924			else {
4925			    if (voip_vid) {
4926				voip_prio = voip_prio << 13;
4927				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
4928				eval("et", "-i", "eth0", "robowr", "0x34", "0x12", tag_register);
4929				_dprintf("lan 3 tag register: %s\n", tag_register);
4930				/* untag port map */
4931				sprintf(vlan_entry, "0x%x", voip_vid);
4932				_dprintf("vlan entry: %s\n", vlan_entry);
4933				if (voip_vid == iptv_vid)
4934					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0613");	/* un:10, f:410*/
4935				else
4936					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0412");	/* un:1 , f:41 */
4937				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4938				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4939				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4940			    }
4941			}
4942			/* config L4/p0 = IPTV */
4943			if (iptv_vid) {
4944				iptv_prio <<= 13;
4945				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
4946				eval("et", "-i", "eth0", "robowr", "0x34", "0x10", tag_register);	/* p0 */
4947				_dprintf("lan 4 tag register: %s\n", tag_register);
4948				/* untag port map */
4949				sprintf(vlan_entry, "0x%x", iptv_vid);
4950				_dprintf("vlan entry: %s\n", vlan_entry);
4951				if (voip_vid == iptv_vid)
4952					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0613");	/* un:10 , f:410 */
4953				else
4954					eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0211"); /* un:0, f:40 */
4955				eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4956				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4957				eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4958			}
4959		}
4960#ifdef RTCONFIG_MULTICAST_IPTV
4961                if (switch_stb >= 7) {
4962                    if (iptv_vid) { /* config IPTV on wan port */
4963_dprintf("*** Multicast IPTV: config IPTV on wan port ***\n");
4964                        sprintf(wan_dev, "vlan%d", iptv_vid);
4965                        nvram_set("wan10_ifname", wan_dev);
4966                        sprintf(port_id, "%d", iptv_vid);
4967                        eval("vconfig", "add", interface, port_id);
4968                        sprintf(vlan_entry, "0x%x", iptv_vid);
4969			if (gmac3_enable) {
4970				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0110");	/* f: 4, 8 */
4971			} else {
4972#ifdef RTCONFIG_RGMII_BRCM5301X
4973				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0090");	/* f: 4, 7 */
4974#else
4975				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0030");	/* f: 4, 5 */
4976#endif
4977			}
4978                        eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
4979                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
4980                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
4981
4982                        if (iptv_prio) { /* config priority */
4983                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
4984                        }
4985                    }
4986                }
4987                if (switch_stb >= 8) {
4988                    if (voip_vid) { /* config voip on wan port */
4989_dprintf("*** Multicast IPTV: config VOIP on wan port ***\n");
4990                        sprintf(wan_dev, "vlan%d", voip_vid);
4991                        nvram_set("wan11_ifname", wan_dev);
4992                        sprintf(port_id, "%d", voip_vid);
4993                        eval("vconfig", "add", interface, port_id);
4994                        sprintf(vlan_entry, "0x%x", voip_vid);
4995			if (gmac3_enable) {
4996				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0110");	/* f: 4, 8 */
4997			} else {
4998#ifdef RTCONFIG_RGMII_BRCM5301X
4999				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0090");	/* f: 4, 7 */
5000#else
5001				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0030");	/* f: 4, 5 */
5002#endif
5003			}
5004                        eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
5005                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
5006					        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
5007
5008                        if (voip_prio) { /* config priority */
5009                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
5010                        }
5011                    }
5012                }
5013                if (switch_stb >=9 ) {
5014                    if (mang_vid) { /* config tr069 on wan port */
5015_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
5016                        sprintf(wan_dev, "vlan%d", mang_vid);
5017                        nvram_set("wan12_ifname", wan_dev);
5018                        sprintf(port_id, "%d", mang_vid);
5019                        eval("vconfig", "add", interface, port_id);
5020                        sprintf(vlan_entry, "0x%x", mang_vid);
5021			if (gmac3_enable) {
5022				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0110");	/* f: 4, 8 */
5023			} else {
5024#ifdef RTCONFIG_RGMII_BRCM5301X
5025				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0090");	/* f: 4, 7 */
5026#else
5027				eval("et", "-i", "eth0", "robowr", "0x05", "0x83", "0x0030");	/* f: 4, 5 */
5028#endif
5029			}
5030                        eval("et", "-i", "eth0", "robowr", "0x05", "0x81", vlan_entry);
5031                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0000");
5032                        eval("et", "-i", "eth0", "robowr", "0x05", "0x80", "0x0080");
5033
5034                        if (mang_prio) { /* config priority */
5035                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5036                        }
5037                    }
5038                }
5039#endif
5040		break;
5041				/* P0  P1 P2 P3 P5 P7 */
5042	case MODEL_RTAC87U:	/* WAN L4 L3 L2 L1 CPU */
5043		if (wan_vid) { /* config wan port */
5044			eval("vconfig", "rem", "vlan2");
5045			sprintf(port_id, "%d", wan_vid);
5046			eval("vconfig", "add", interface, port_id);
5047			sprintf(vlan_entry, "0x%x", wan_vid);
5048			eval("et", "robowr", "0x05", "0x83", "0x0081");
5049			eval("et", "robowr", "0x05", "0x81", vlan_entry);
5050			eval("et", "robowr", "0x05", "0x80", "0x0080");
5051		}
5052		/* Set Wan port PRIO */
5053		if (nvram_invmatch("switch_wan0prio", "0"))
5054			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
5055
5056		if (nvram_match("switch_stb_x", "3")) {  // P3
5057                        if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
5058                                iptv_prio = iptv_prio << 13;
5059                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5060                                eval("et", "robowr", "0x34", "0x14", tag_register);
5061
5062                                char vlan_cmd[64];
5063                                sprintf(vlan_cmd, "robocfg vlan 1 ports \"2 3 5u 7t\"");
5064                                system(vlan_cmd);
5065                                sprintf(vlan_cmd, "robocfg vlan 2 ports \"0 7\"");
5066                                system(vlan_cmd);
5067                                sprintf(vlan_cmd, "robocfg vlan 100 ports \"0t 1t 7t\"");
5068                                system(vlan_cmd);
5069                                sprintf(vlan_cmd, "robocfg vlan 101 ports \"0t 1t\"");
5070                                system(vlan_cmd);
5071                                sprintf(vlan_cmd, "robocfg vlan 105 ports \"0t 1t 2\"");
5072                                system(vlan_cmd);
5073                                break;
5074                        }
5075			if (nvram_match("switch_wantag", "m1_fiber") ||
5076			   nvram_match("switch_wantag", "maxis_fiber_sp")
5077			) {
5078				/* Just forward packets between port 0 & 3, without untag */
5079				sprintf(vlan_entry, "0x%x", voip_vid);
5080				_dprintf("vlan entry: %s\n", vlan_entry);
5081				eval("et", "robowr", "0x05", "0x83", "0x0005");
5082				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5083				eval("et", "robowr", "0x05", "0x80", "0x0080");
5084			}
5085			else if (nvram_match("switch_wantag", "maxis_fiber")) {
5086				/* Just forward packets between port 0 & 3, without untag */
5087				eval("et", "robowr", "0x05", "0x83", "0x0005");
5088				eval("et", "robowr", "0x05", "0x81", "0x0335");	/* vlan id=821 */
5089				eval("et", "robowr", "0x05", "0x80", "0x0080");
5090				eval("et", "robowr", "0x05", "0x83", "0x0005");
5091				eval("et", "robowr", "0x05", "0x81", "0x0336");	/* vlan id=822 */
5092				eval("et", "robowr", "0x05", "0x80", "0x0080");
5093			}
5094			else {  /* Nomo case, untag it. */
5095				voip_prio = voip_prio << 13;
5096				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5097				eval("et", "robowr", "0x34", "0x14", tag_register);
5098				_dprintf("lan 3 tag register: %s\n", tag_register);
5099				/* Set vlan table entry register */
5100				sprintf(vlan_entry, "0x%x", voip_vid);
5101				_dprintf("vlan entry: %s\n", vlan_entry);
5102				eval("et", "robowr", "0x05", "0x83", "0x0805");
5103				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5104				eval("et", "robowr", "0x05", "0x80", "0x0080");
5105			}
5106		}
5107		else if (nvram_match("switch_stb_x", "4")) { // P4
5108			/* config LAN 4 = IPTV */
5109			if (nvram_match("switch_wantag", "meo")) {
5110				/* Just forward packets between port 0 & 1, without untag */
5111				sprintf(vlan_entry, "0x%x", iptv_vid);
5112				_dprintf("* vlan entry: %s\n", vlan_entry);
5113				eval("et", "robowr", "0x05", "0x83", "0x0083");
5114				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5115				eval("et", "robowr", "0x05", "0x80", "0x0080");
5116			}
5117			else {  /* Nomo case, untag it. */
5118				iptv_prio = iptv_prio << 13;
5119				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5120				eval("et", "robowr", "0x34", "0x12", tag_register);
5121				_dprintf("lan 4 tag register: %s\n", tag_register);
5122				/* Set vlan table entry register */
5123				sprintf(vlan_entry, "0x%x", iptv_vid);
5124				_dprintf("vlan entry: %s\n", vlan_entry);
5125				eval("et", "robowr", "0x05", "0x83", "0x0403");		/* un: 1, fwd; 0,1*/
5126				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5127				eval("et", "robowr", "0x05", "0x80", "0x0080");
5128			}
5129		}
5130		else if (nvram_match("switch_stb_x", "6")) {
5131			/* config LAN 3 = VoIP */ // P3
5132			if (nvram_match("switch_wantag", "singtel_mio")) {
5133				/* Just forward packets between port 0 & 3, without untag */
5134				sprintf(vlan_entry, "0x%x", voip_vid);
5135				_dprintf("vlan entry: %s\n", vlan_entry);
5136				eval("et", "robowr", "0x05", "0x83", "0x0005");
5137				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5138				eval("et", "robowr", "0x05", "0x80", "0x0080");
5139			}
5140			else {  /* Nomo case, untag it. */
5141			    if (voip_vid) {
5142				voip_prio = voip_prio << 13;
5143				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5144				eval("et", "robowr", "0x34", "0x14", tag_register);
5145				_dprintf("lan 3 tag register: %s\n", tag_register);
5146				/* Set vlan table entry register */
5147				sprintf(vlan_entry, "0x%x", voip_vid);
5148				_dprintf("vlan entry: %s\n", vlan_entry);
5149				if (voip_vid == iptv_vid)
5150					eval("et", "robowr", "0x05", "0x83", "0x0C07");
5151				else
5152					eval("et", "robowr", "0x05", "0x83", "0x0805");
5153				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5154				eval("et", "robowr", "0x05", "0x80", "0x0080");
5155			    }
5156			}
5157			/* config LAN 4 = IPTV */ // P4
5158			if (iptv_vid) {
5159				iptv_prio = iptv_prio << 13;
5160				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5161				eval("et", "robowr", "0x34", "0x12", tag_register);
5162				_dprintf("lan 4 tag register: %s\n", tag_register);
5163				/* Set vlan table entry register */
5164				sprintf(vlan_entry, "0x%x", iptv_vid);
5165				_dprintf("vlan entry: %s\n", vlan_entry);
5166				if (voip_vid == iptv_vid)
5167					eval("et", "robowr", "0x05", "0x83", "0x0C07");
5168				else
5169					eval("et", "robowr", "0x05", "0x83", "0x0403");
5170				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5171				eval("et", "robowr", "0x05", "0x80", "0x0080");
5172			}
5173		}
5174#ifdef RTCONFIG_MULTICAST_IPTV
5175                if (switch_stb >= 7) {
5176                    if (iptv_vid) { /* config IPTV on wan port */
5177_dprintf("*** Multicast IPTV: config IPTV on wan port ***\n");
5178                        sprintf(wan_dev, "vlan%d", iptv_vid);
5179                        nvram_set("wan10_ifname", wan_dev);
5180                        sprintf(port_id, "%d", iptv_vid);
5181                        eval("vconfig", "add", interface, port_id);
5182                        sprintf(vlan_entry, "0x%x", iptv_vid);
5183                        eval("et", "robowr", "0x05", "0x83", "0x0081");
5184                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5185                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5186                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5187
5188                        if (iptv_prio) { /* config priority */
5189                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5190                        }
5191                    }
5192                }
5193                if (switch_stb >= 8) {
5194                    if (voip_vid) { /* config voip on wan port */
5195_dprintf("*** Multicast IPTV: config VOIP on wan port ***\n");
5196                        sprintf(wan_dev, "vlan%d", voip_vid);
5197                        nvram_set("wan11_ifname", wan_dev);
5198                        sprintf(port_id, "%d", voip_vid);
5199                        eval("vconfig", "add", interface, port_id);
5200                        sprintf(vlan_entry, "0x%x", voip_vid);
5201                        eval("et", "robowr", "0x05", "0x83", "0x0081");
5202                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5203                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5204                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5205
5206                        if (voip_prio) { /* config priority */
5207                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
5208                        }
5209                    }
5210                }
5211                if (switch_stb >=9 ) {
5212                    if (mang_vid) { /* config tr069 on wan port */
5213_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
5214                        sprintf(wan_dev, "vlan%d", mang_vid);
5215                        nvram_set("wan12_ifname", wan_dev);
5216                        sprintf(port_id, "%d", mang_vid);
5217                        eval("vconfig", "add", interface, port_id);
5218                        sprintf(vlan_entry, "0x%x", mang_vid);
5219                        eval("et", "robowr", "0x05", "0x83", "0x0081");
5220                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5221                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5222                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5223
5224                        if (mang_prio) { /* config priority */
5225                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5226                        }
5227                    }
5228                }
5229#endif
5230		break;
5231				/* P0 P1 P2 P3 P4 P5 */
5232	case MODEL_RTAC56S:	/* L1 L2 L3 L4 WAN CPU */
5233	case MODEL_RTAC56U:
5234		if (wan_vid) { /* config wan port */
5235			eval("vconfig", "rem", "vlan2");
5236			sprintf(port_id, "%d", wan_vid);
5237			eval("vconfig", "add", interface, port_id);
5238			sprintf(vlan_entry, "0x%x", wan_vid);
5239			eval("et", "robowr", "0x05", "0x83", "0x0030");
5240			eval("et", "robowr", "0x05", "0x81", vlan_entry);
5241			eval("et", "robowr", "0x05", "0x80", "0x0000");
5242			eval("et", "robowr", "0x05", "0x80", "0x0080");
5243		}
5244		/* Set Wan port PRIO */
5245		if (nvram_invmatch("switch_wan0prio", "0"))
5246			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
5247
5248		if (nvram_match("switch_stb_x", "3")) {	// P2
5249                        if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
5250                                iptv_prio = iptv_prio << 13;
5251                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5252                                eval("et", "robowr", "0x34", "0x14", tag_register);
5253
5254                                char vlan_cmd[64];
5255                                sprintf(vlan_cmd, "robocfg vlan 1 ports \"0 1 2 5t\"");
5256                                system(vlan_cmd);
5257                                sprintf(vlan_cmd, "robocfg vlan 2 ports \"4 5\"");
5258                                system(vlan_cmd);
5259                                sprintf(vlan_cmd, "robocfg vlan 100 ports \"3t 4t 5t\"");
5260                                system(vlan_cmd);
5261                                sprintf(vlan_cmd, "robocfg vlan 101 ports \"3t 4t\"");
5262                                system(vlan_cmd);
5263                                sprintf(vlan_cmd, "robocfg vlan 105 ports \"2 3t 4t\"");
5264                                system(vlan_cmd);
5265                                break;
5266                        }
5267			if (nvram_match("switch_wantag", "m1_fiber") ||
5268			   nvram_match("switch_wantag", "maxis_fiber_sp")
5269			) {
5270				/* Just forward packets between port 4 & 2, without untag */
5271				sprintf(vlan_entry, "0x%x", voip_vid);
5272				_dprintf("vlan entry: %s\n", vlan_entry);
5273				eval("et", "robowr", "0x05", "0x83", "0x0014");
5274				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5275				eval("et", "robowr", "0x05", "0x80", "0x0000");
5276				eval("et", "robowr", "0x05", "0x80", "0x0080");
5277			}
5278			else if (nvram_match("switch_wantag", "maxis_fiber")) {
5279				/* Just forward packets between port 4 & 2, without untag */
5280				eval("et", "robowr", "0x05", "0x83", "0x0014");
5281				eval("et", "robowr", "0x05", "0x81", "0x0335");	/* vlan id=821 */
5282				eval("et", "robowr", "0x05", "0x80", "0x0000");
5283				eval("et", "robowr", "0x05", "0x80", "0x0080");
5284				eval("et", "robowr", "0x05", "0x83", "0x0014");
5285				eval("et", "robowr", "0x05", "0x81", "0x0336");	/* vlan id=822 */
5286				eval("et", "robowr", "0x05", "0x80", "0x0000");
5287				eval("et", "robowr", "0x05", "0x80", "0x0080");
5288			}
5289			else {	/* Nomo case, untag it. */
5290				voip_prio = voip_prio << 13;
5291				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5292				eval("et", "robowr", "0x34", "0x14", tag_register);
5293				_dprintf("lan 3 tag register: %s\n", tag_register);
5294				/* Set vlan table entry register */
5295				sprintf(vlan_entry, "0x%x", voip_vid);
5296				_dprintf("vlan entry: %s\n", vlan_entry);
5297				eval("et", "robowr", "0x05", "0x83", "0x0814");
5298				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5299				eval("et", "robowr", "0x05", "0x80", "0x0000");
5300				eval("et", "robowr", "0x05", "0x80", "0x0080");
5301			}
5302		}
5303		else if (nvram_match("switch_stb_x", "4")) { // P3
5304			/* config LAN 4 = IPTV */
5305                        if (nvram_match("switch_wantag", "meo")) {
5306                                /* Just forward packets between port 0 & 4, without untag */
5307                                sprintf(vlan_entry, "0x%x", iptv_vid);
5308                                _dprintf("vlan entry: %s\n", vlan_entry);
5309                                eval("et", "robowr", "0x05", "0x83", "0x0038");
5310                                eval("et", "robowr", "0x05", "0x81", vlan_entry);
5311                                eval("et", "robowr", "0x05", "0x80", "0x0000");
5312                                eval("et", "robowr", "0x05", "0x80", "0x0080");
5313                        }
5314                        else {  /* Nomo case, untag it. */
5315				iptv_prio = iptv_prio << 13;
5316				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5317				eval("et", "robowr", "0x34", "0x16", tag_register);
5318				_dprintf("lan 4 tag register: %s\n", tag_register);
5319				/* Set vlan table entry register */
5320				sprintf(vlan_entry, "0x%x", iptv_vid);
5321				_dprintf("vlan entry: %s\n", vlan_entry);
5322				eval("et", "robowr", "0x05", "0x83", "0x1018");
5323				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5324				eval("et", "robowr", "0x05", "0x80", "0x0000");
5325				eval("et", "robowr", "0x05", "0x80", "0x0080");
5326			}
5327		}
5328		else if (nvram_match("switch_stb_x", "6")) {
5329			/* config LAN 3 = VoIP */	// P2
5330			if (nvram_match("switch_wantag", "singtel_mio")) {
5331				/* Just forward packets between port 2 & 4, without untag */
5332				sprintf(vlan_entry, "0x%x", voip_vid);
5333				_dprintf("vlan entry: %s\n", vlan_entry);
5334				eval("et", "robowr", "0x05", "0x83", "0x0014");
5335				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5336				eval("et", "robowr", "0x05", "0x80", "0x0000");
5337				eval("et", "robowr", "0x05", "0x80", "0x0080");
5338			}
5339			else {	/* Nomo case, untag it. */
5340			    if (voip_vid) {
5341				voip_prio = voip_prio << 13;
5342				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5343				eval("et", "robowr", "0x34", "0x14", tag_register);
5344				_dprintf("lan 3 tag register: %s\n", tag_register);
5345				/* Set vlan table entry register */
5346				sprintf(vlan_entry, "0x%x", voip_vid);
5347				_dprintf("vlan entry: %s\n", vlan_entry);
5348				if (voip_vid == iptv_vid)
5349					eval("et", "robowr", "0x05", "0x83", "0x081C");
5350				else
5351					eval("et", "robowr", "0x05", "0x83", "0x0814");
5352				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5353				eval("et", "robowr", "0x05", "0x80", "0x0000");
5354				eval("et", "robowr", "0x05", "0x80", "0x0080");
5355			    }
5356			}
5357			/* config LAN 4 = IPTV */ //P3
5358			if (iptv_vid) {
5359				iptv_prio = iptv_prio << 13;
5360				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5361				eval("et", "robowr", "0x34", "0x16", tag_register);
5362				_dprintf("lan 4 tag register: %s\n", tag_register);
5363				/* Set vlan table entry register */
5364				sprintf(vlan_entry, "0x%x", iptv_vid);
5365				_dprintf("vlan entry: %s\n", vlan_entry);
5366				if (voip_vid == iptv_vid)
5367					eval("et", "robowr", "0x05", "0x83", "0x081C");
5368				else
5369					eval("et", "robowr", "0x05", "0x83", "0x1018");
5370				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5371				eval("et", "robowr", "0x05", "0x80", "0x0000");
5372				eval("et", "robowr", "0x05", "0x80", "0x0080");
5373			}
5374		}
5375#ifdef RTCONFIG_MULTICAST_IPTV
5376                if (switch_stb >= 7) {
5377                    if (iptv_vid) { /* config IPTV on wan port */
5378_dprintf("*** Multicast IPTV: config IPTV on wan port ***\n");
5379                        sprintf(wan_dev, "vlan%d", iptv_vid);
5380                        nvram_set("wan10_ifname", wan_dev);
5381                        sprintf(port_id, "%d", iptv_vid);
5382                        eval("vconfig", "add", interface, port_id);
5383                        sprintf(vlan_entry, "0x%x", iptv_vid);
5384                        eval("et", "robowr", "0x05", "0x83", "0x0030");
5385                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5386                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5387                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5388
5389                        if (iptv_prio) { /* config priority */
5390                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5391                        }
5392                    }
5393                }
5394                if (switch_stb >= 8) {
5395                    if (voip_vid) { /* config voip on wan port */
5396_dprintf("*** Multicast IPTV: config VOIP on wan port ***\n");
5397                        sprintf(wan_dev, "vlan%d", voip_vid);
5398                        nvram_set("wan11_ifname", wan_dev);
5399                        sprintf(port_id, "%d", voip_vid);
5400                        eval("vconfig", "add", interface, port_id);
5401                        sprintf(vlan_entry, "0x%x", voip_vid);
5402                        eval("et", "robowr", "0x05", "0x83", "0x0030");
5403                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5404                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5405                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5406
5407
5408                        if (voip_prio) { /* config priority */
5409                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
5410                        }
5411                    }
5412                }
5413                if (switch_stb >=9 ) {
5414                    if (mang_vid) { /* config tr069 on wan port */
5415_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
5416                        sprintf(wan_dev, "vlan%d", mang_vid);
5417                        nvram_set("wan12_ifname", wan_dev);
5418                        sprintf(port_id, "%d", mang_vid);
5419                        eval("vconfig", "add", interface, port_id);
5420                        sprintf(vlan_entry, "0x%x", mang_vid);
5421                        eval("et", "robowr", "0x05", "0x83", "0x0030");
5422                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5423                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5424                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5425
5426                        if (mang_prio) { /* config priority */
5427                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5428                        }
5429                    }
5430                }
5431#endif
5432		break;
5433
5434	case MODEL_RTN66U:
5435	case MODEL_RTAC66U:
5436	case MODEL_RTAC1200G:
5437	case MODEL_RTAC1200GP:
5438		if (wan_vid) { /* config wan port */
5439			eval("vconfig", "rem", "vlan2");
5440			sprintf(port_id, "%d", wan_vid);
5441			eval("vconfig", "add", interface, port_id);
5442			sprintf(vlan_entry, "0x%x", wan_vid);
5443			eval("et", "robowr", "0x05", "0x83", "0x0101");
5444			eval("et", "robowr", "0x05", "0x81", vlan_entry);
5445			eval("et", "robowr", "0x05", "0x80", "0x0000");
5446			eval("et", "robowr", "0x05", "0x80", "0x0080");
5447		}
5448		/* Set Wan port PRIO */
5449		if (nvram_invmatch("switch_wan0prio", "0"))
5450			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
5451
5452		if (nvram_match("switch_stb_x", "3")) {
5453                        if (nvram_match("switch_wantag", "vodafone")) { //Config by robocfg
5454                                iptv_prio = iptv_prio << 13;
5455                                sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5456
5457				/* vlan 1 */
5458                                eval("et", "robowr", "0x05", "0x83", "0x1D0E");
5459                                eval("et", "robowr", "0x05", "0x81", "0x0001");
5460                                eval("et", "robowr", "0x05", "0x80", "0x0000");
5461                                eval("et", "robowr", "0x05", "0x80", "0x0080");
5462				/* vlan 100 */
5463                                eval("et", "robowr", "0x05", "0x83", "0x0111");
5464                                eval("et", "robowr", "0x05", "0x81", "0x0064");
5465                                eval("et", "robowr", "0x05", "0x80", "0x0000");
5466                                eval("et", "robowr", "0x05", "0x80", "0x0080");
5467				/* vlan 101 */
5468                                eval("et", "robowr", "0x05", "0x83", "0x0011");
5469                                eval("et", "robowr", "0x05", "0x81", "0x0065");
5470                                eval("et", "robowr", "0x05", "0x80", "0x0000");
5471                                eval("et", "robowr", "0x05", "0x80", "0x0080");
5472				/* vlan 105 */
5473                                eval("et", "robowr", "0x05", "0x83", "0x1019");
5474                                eval("et", "robowr", "0x05", "0x81", "0x0069");
5475                                eval("et", "robowr", "0x05", "0x80", "0x0000");
5476                                eval("et", "robowr", "0x05", "0x80", "0x0080");
5477                                break;
5478                        }
5479			if (nvram_match("switch_wantag", "m1_fiber") ||
5480			   nvram_match("switch_wantag", "maxis_fiber_sp")
5481			) {
5482				/* Just forward packets between port 0 & 3, without untag */
5483				sprintf(vlan_entry, "0x%x", voip_vid);
5484				_dprintf("vlan entry: %s\n", vlan_entry);
5485				eval("et", "robowr", "0x05", "0x83", "0x0009");
5486				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5487				eval("et", "robowr", "0x05", "0x80", "0x0000");
5488				eval("et", "robowr", "0x05", "0x80", "0x0080");
5489			}
5490			else if (nvram_match("switch_wantag", "maxis_fiber")) {
5491				/* Just forward packets between port 0 & 3, without untag */
5492				eval("et", "robowr", "0x05", "0x83", "0x0009");
5493				eval("et", "robowr", "0x05", "0x81", "0x0335");	/* vlan id=821 */
5494				eval("et", "robowr", "0x05", "0x80", "0x0000");
5495				eval("et", "robowr", "0x05", "0x80", "0x0080");
5496				eval("et", "robowr", "0x05", "0x83", "0x0009");
5497				eval("et", "robowr", "0x05", "0x81", "0x0336");	/* vlan id=822 */
5498				eval("et", "robowr", "0x05", "0x80", "0x0000");
5499				eval("et", "robowr", "0x05", "0x80", "0x0080");
5500			}
5501			else if (nvram_match("switch_wantag", "maxis_fiber_iptv")) {
5502				/* Just forward packets between port 0 & 3, without untag */
5503				eval("et", "robowr", "0x05", "0x83", "0x0009");
5504				eval("et", "robowr", "0x05", "0x81", "0x0335");	/* vlan id=821 */
5505				eval("et", "robowr", "0x05", "0x80", "0x0000");
5506				eval("et", "robowr", "0x05", "0x80", "0x0080");
5507				eval("et", "robowr", "0x05", "0x83", "0x0009");
5508				eval("et", "robowr", "0x05", "0x81", "0x0336");	/* vlan id=822 */
5509				eval("et", "robowr", "0x05", "0x80", "0x0000");
5510				eval("et", "robowr", "0x05", "0x80", "0x0080");
5511				/* Just forward packets between port 0 & 4 & untag */
5512				eval("et", "robowr", "0x05", "0x83", "0x2011");
5513				eval("et", "robowr", "0x05", "0x81", "0x0337");	/* vlan id=823 */
5514				eval("et", "robowr", "0x05", "0x80", "0x0000");
5515				eval("et", "robowr", "0x05", "0x80", "0x0080");
5516				eval("et", "robowr", "0x05", "0x83", "0x2011");
5517				eval("et", "robowr", "0x05", "0x81", "0x0338");	/* vlan id=824 */
5518				eval("et", "robowr", "0x05", "0x80", "0x0000");
5519				eval("et", "robowr", "0x05", "0x80", "0x0080");
5520			}
5521			else if (nvram_match("switch_wantag", "maxis_fiber_sp_iptv")) {
5522				/* Just forward packets between port 0 & 3, without untag */
5523				eval("et", "robowr", "0x05", "0x83", "0x0009");
5524				eval("et", "robowr", "0x05", "0x81", "0x0006");	/* vlan id= 6  */
5525				eval("et", "robowr", "0x05", "0x80", "0x0000");
5526				eval("et", "robowr", "0x05", "0x80", "0x0080");
5527				eval("et", "robowr", "0x05", "0x83", "0x0009");
5528				eval("et", "robowr", "0x05", "0x81", "0x000E");	/* vlan id= 14 */
5529				eval("et", "robowr", "0x05", "0x80", "0x0000");
5530				eval("et", "robowr", "0x05", "0x80", "0x0080");
5531				/* Just forward packets between port 0 & 4 & untag */
5532				eval("et", "robowr", "0x05", "0x83", "0x2011");
5533				eval("et", "robowr", "0x05", "0x81", "0x000F");	/* vlan id= 15 */
5534				eval("et", "robowr", "0x05", "0x80", "0x0000");
5535				eval("et", "robowr", "0x05", "0x80", "0x0080");
5536				eval("et", "robowr", "0x05", "0x83", "0x2011");
5537				eval("et", "robowr", "0x05", "0x81", "0x0011");	/* vlan id= 17 */
5538				eval("et", "robowr", "0x05", "0x80", "0x0000");
5539				eval("et", "robowr", "0x05", "0x80", "0x0080");
5540			}
5541			else {	/* Nomo case, untag it. */
5542				voip_prio = voip_prio << 13;
5543				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5544				eval("et", "robowr", "0x34", "0x16", tag_register);
5545				_dprintf("lan 3 tag register: %s\n", tag_register);
5546				/* Set vlan table entry register */
5547				sprintf(vlan_entry, "0x%x", voip_vid);
5548				_dprintf("vlan entry: %s\n", vlan_entry);
5549				eval("et", "robowr", "0x05", "0x83", "0x1009");
5550				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5551				eval("et", "robowr", "0x05", "0x80", "0x0000");
5552				eval("et", "robowr", "0x05", "0x80", "0x0080");
5553			}
5554		}
5555		else if (nvram_match("switch_stb_x", "4")) {
5556			/* config LAN 4 = IPTV */
5557			if (nvram_match("switch_wantag", "meo")) {
5558				/* Just forward packets between port 0 & 1, without untag */
5559				sprintf(vlan_entry, "0x%x", iptv_vid);
5560				_dprintf("* vlan entry: %s\n", vlan_entry);
5561				eval("et", "robowr", "0x05", "0x83", "0x0111");
5562				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5563				eval("et", "robowr", "0x05", "0x80", "0x0080");
5564			}
5565			else {  /* Nomo case, untag it. */
5566				/* config LAN 4 = IPTV */
5567				iptv_prio = iptv_prio << 13;
5568				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5569				eval("et", "robowr", "0x34", "0x18", tag_register);
5570				_dprintf("lan 4 tag register: %s\n", tag_register);
5571				/* Set vlan table entry register */
5572				sprintf(vlan_entry, "0x%x", iptv_vid);
5573				_dprintf("vlan entry: %s\n", vlan_entry);
5574				eval("et", "robowr", "0x05", "0x83", "0x2011");
5575				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5576				eval("et", "robowr", "0x05", "0x80", "0x0000");
5577				eval("et", "robowr", "0x05", "0x80", "0x0080");
5578			}
5579		}
5580		else if (nvram_match("switch_stb_x", "6")) {
5581			/* config LAN 3 = VoIP */
5582			if (nvram_match("switch_wantag", "singtel_mio")) {
5583				/* Just forward packets between port 0 & 3, without untag */
5584				sprintf(vlan_entry, "0x%x", voip_vid);
5585				_dprintf("vlan entry: %s\n", vlan_entry);
5586				eval("et", "robowr", "0x05", "0x83", "0x0009");
5587				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5588				eval("et", "robowr", "0x05", "0x80", "0x0000");
5589				eval("et", "robowr", "0x05", "0x80", "0x0080");
5590			}
5591			else {	/* Nomo case, untag it. */
5592			    if (voip_vid) {
5593				voip_prio = voip_prio << 13;
5594				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5595				eval("et", "robowr", "0x34", "0x16", tag_register);
5596				_dprintf("lan 3 tag register: %s\n", tag_register);
5597				/* Set vlan table entry register */
5598				sprintf(vlan_entry, "0x%x", voip_vid);
5599				_dprintf("vlan entry: %s\n", vlan_entry);
5600				if (voip_vid == iptv_vid)
5601					eval("et", "robowr", "0x05", "0x83", "0x3019");
5602				else
5603					eval("et", "robowr", "0x05", "0x83", "0x1009");
5604				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5605				eval("et", "robowr", "0x05", "0x80", "0x0000");
5606				eval("et", "robowr", "0x05", "0x80", "0x0080");
5607			    }
5608			}
5609			if (iptv_vid) { /* config LAN 4 = IPTV */
5610				iptv_prio = iptv_prio << 13;
5611				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5612				eval("et", "robowr", "0x34", "0x18", tag_register);
5613				_dprintf("lan 4 tag register: %s\n", tag_register);
5614				/* Set vlan table entry register */
5615				sprintf(vlan_entry, "0x%x", iptv_vid);
5616				_dprintf("vlan entry: %s\n", vlan_entry);
5617				if (voip_vid == iptv_vid)
5618					eval("et", "robowr", "0x05", "0x83", "0x3019");
5619				else
5620					eval("et", "robowr", "0x05", "0x83", "0x2011");
5621				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5622				eval("et", "robowr", "0x05", "0x80", "0x0000");
5623				eval("et", "robowr", "0x05", "0x80", "0x0080");
5624			}
5625		}
5626#ifdef RTCONFIG_MULTICAST_IPTV
5627                if (switch_stb >= 7) { //Maxis IPTV case
5628                    if (iptv_vid) { /* config IPTV on wan port */
5629_dprintf("*** Multicast IPTV: config Maxis IPTV on wan port ***\n");
5630                        sprintf(wan_dev, "vlan%d", iptv_vid);
5631                        nvram_set("wan10_ifname", wan_dev);
5632                        sprintf(port_id, "%d", iptv_vid);
5633                        eval("vconfig", "add", interface, port_id);
5634                        sprintf(vlan_entry, "0x%x", iptv_vid);
5635                        eval("et", "robowr", "0x05", "0x83", "0x0101");
5636                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5637                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5638                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5639
5640                        if (iptv_prio) { /* config priority */
5641                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5642                        }
5643                    }
5644                }
5645		if (switch_stb >= 8) { //Singtel IPTV case
5646                    if (voip_vid) { /* config voip on wan port */
5647_dprintf("*** Multicast IPTV: config Singtel VOIP on wan port ***\n");
5648                        sprintf(wan_dev, "vlan%d", voip_vid);
5649                        nvram_set("wan11_ifname", wan_dev);
5650                        sprintf(port_id, "%d", voip_vid);
5651                        eval("vconfig", "add", interface, port_id);
5652                        sprintf(vlan_entry, "0x%x", voip_vid);
5653                        eval("et", "robowr", "0x05", "0x83", "0x0101");
5654                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5655                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5656                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5657
5658                        if (voip_prio) { /* config priority */
5659                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
5660                        }
5661                    }
5662		}
5663                if (switch_stb >=9 ) {
5664                    if (mang_vid) { /* config tr069 on wan port */
5665_dprintf("*** Multicast IPTV: config Singtel TR069 on wan port ***\n");
5666                        sprintf(wan_dev, "vlan%d", mang_vid);
5667                        nvram_set("wan12_ifname", wan_dev);
5668                        sprintf(port_id, "%d", mang_vid);
5669                        eval("vconfig", "add", interface, port_id);
5670                        sprintf(vlan_entry, "0x%x", mang_vid);
5671                        eval("et", "robowr", "0x05", "0x83", "0x0101");
5672                        eval("et", "robowr", "0x05", "0x81", vlan_entry);
5673                        eval("et", "robowr", "0x05", "0x80", "0x0000");
5674                        eval("et", "robowr", "0x05", "0x80", "0x0080");
5675
5676                        if (mang_prio) { /* config priority */
5677                                eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
5678                        }
5679                    }
5680		}
5681#endif
5682		break;
5683
5684	case MODEL_RTN15U:
5685		if (wan_vid) { /* config wan port */
5686			eval("vconfig", "rem", "vlan2");
5687			sprintf(port_id, "%d", wan_vid);
5688			eval("vconfig", "add", interface, port_id);
5689			sprintf(vlan_entry, "0x%x", wan_vid);
5690			eval("et", "robowr", "0x05", "0x83", "0x0110");
5691			eval("et", "robowr", "0x05", "0x81", vlan_entry);
5692			eval("et", "robowr", "0x05", "0x80", "0x0000");
5693			eval("et", "robowr", "0x05", "0x80", "0x0080");
5694		}
5695		/* Set Wan port PRIO */
5696		if (nvram_invmatch("switch_wan0prio", "0"))
5697			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
5698
5699		if (nvram_match("switch_stb_x", "3")) {
5700			if (nvram_match("switch_wantag", "m1_fiber")) {
5701				/* Just forward packets between LAN3 & WAN(port1 & 4), without untag */
5702				sprintf(vlan_entry, "0x%x", voip_vid);
5703				_dprintf("vlan entry: %s\n", vlan_entry);
5704				eval("et", "robowr", "0x05", "0x83", "0x0012");
5705				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5706				eval("et", "robowr", "0x05", "0x80", "0x0000");
5707				eval("et", "robowr", "0x05", "0x80", "0x0080");
5708			}
5709			else {	/* Nomo case, untag it. */
5710				voip_prio = voip_prio << 13;
5711				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5712				eval("et", "robowr", "0x34", "0x12", tag_register);
5713				_dprintf("lan 3 tag register: %s\n", tag_register);
5714				/* Set vlan table entry register */
5715				sprintf(vlan_entry, "0x%x", voip_vid);
5716				_dprintf("vlan entry: %s\n", vlan_entry);
5717				eval("et", "robowr", "0x05", "0x83", "0x0412");
5718				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5719				eval("et", "robowr", "0x05", "0x80", "0x0000");
5720				eval("et", "robowr", "0x05", "0x80", "0x0080");
5721			}
5722		}
5723		else if (nvram_match("switch_stb_x", "4")) {
5724			/* config LAN 4 = IPTV */
5725			iptv_prio = iptv_prio << 13;
5726			sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5727			eval("et", "robowr", "0x34", "0x10", tag_register);
5728			_dprintf("lan 4 tag register: %s\n", tag_register);
5729			/* Set vlan table entry register */
5730			sprintf(vlan_entry, "0x%x", iptv_vid);
5731			_dprintf("vlan entry: %s\n", vlan_entry);
5732			eval("et", "robowr", "0x05", "0x83", "0x0211");
5733			eval("et", "robowr", "0x05", "0x81", vlan_entry);
5734			eval("et", "robowr", "0x05", "0x80", "0x0000");
5735			eval("et", "robowr", "0x05", "0x80", "0x0080");
5736		}
5737		else if (nvram_match("switch_stb_x", "6")) {
5738			/* config LAN 3 = VoIP */
5739			if (nvram_match("switch_wantag", "singtel_mio")) {
5740				/* Just forward packets between LAN3 & WAN(port1 & 4), without untag */
5741				sprintf(vlan_entry, "0x%x", voip_vid);
5742				_dprintf("vlan entry: %s\n", vlan_entry);
5743				eval("et", "robowr", "0x05", "0x83", "0x0012");
5744				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5745				eval("et", "robowr", "0x05", "0x80", "0x0000");
5746				eval("et", "robowr", "0x05", "0x80", "0x0080");
5747			}
5748			else {	/* Nomo case, untag it. */
5749			    if (voip_vid) {
5750				voip_prio = voip_prio << 13;
5751				sprintf(tag_register, "0x%x", (voip_prio | voip_vid));
5752				eval("et", "robowr", "0x34", "0x12", tag_register);
5753				_dprintf("lan 3 tag register: %s\n", tag_register);
5754				/* Set vlan table entry register */
5755				sprintf(vlan_entry, "0x%x", voip_vid);
5756				_dprintf("vlan entry: %s\n", vlan_entry);
5757				if (voip_vid == iptv_vid)
5758					eval("et", "robowr", "0x05", "0x83", "0x0613");
5759				else
5760					eval("et", "robowr", "0x05", "0x83", "0x0412");
5761				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5762				eval("et", "robowr", "0x05", "0x80", "0x0000");
5763				eval("et", "robowr", "0x05", "0x80", "0x0080");
5764			    }
5765			}
5766			if (iptv_vid) { /* config LAN 4 = IPTV */
5767				iptv_prio = iptv_prio << 13;
5768				sprintf(tag_register, "0x%x", (iptv_prio | iptv_vid));
5769				eval("et", "robowr", "0x34", "0x10", tag_register);
5770				_dprintf("lan 4 tag register: %s\n", tag_register);
5771				/* Set vlan table entry register */
5772				sprintf(vlan_entry, "0x%x", iptv_vid);
5773				_dprintf("vlan entry: %s\n", vlan_entry);
5774				if (voip_vid == iptv_vid)
5775					eval("et", "robowr", "0x05", "0x83", "0x0613");
5776				else
5777					eval("et", "robowr", "0x05", "0x83", "0x0211");
5778				eval("et", "robowr", "0x05", "0x81", vlan_entry);
5779				eval("et", "robowr", "0x05", "0x80", "0x0000");
5780				eval("et", "robowr", "0x05", "0x80", "0x0080");
5781			}
5782		}
5783		break;
5784
5785				/* WAN L1 L2 L3 L4 CPU */
5786	case MODEL_RTAC53U:	/* P0  P1 P2 P3 P4 P5 */
5787		/* Enable high bits check */
5788		eval("et", "robowr", "0x34", "0x3", "0x80", "0x1");
5789		/* Config WAN port */
5790		if (wan_vid) {
5791			eval("vconfig", "rem", "vlan2");
5792			eval("et", "robowr", "0x34", "0x8", "0x01002000", "0x4");
5793			eval("et", "robowr", "0x34", "0x6", "0x3002");
5794			sprintf(port_id, "%d", wan_vid);
5795			eval("vconfig", "add", interface, port_id);
5796		}
5797		/* Set Wan prio*/
5798		if (!nvram_match("switch_wan0prio", "0"))
5799			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
5800
5801		if (nvram_match("switch_wantag", "unifi_home")) {
5802			/* vlan1ports= 1 2 3 5 */
5803			eval("et", "robowr", "0x34", "0x8", "0x010013ae", "0x4");
5804			eval("et", "robowr", "0x34", "0x6", "0x3001");
5805			/* vlan500ports= 0 5 */
5806			eval("et", "robowr", "0x34", "0x8", "0x011f4021", "0x4");
5807			eval("et", "robowr", "0x34", "0x6", "0x31f4");
5808			/* vlan600ports= 0 4 */
5809			eval("et", "robowr", "0x34", "0x8", "0x01258411", "0x4");
5810			eval("et", "robowr", "0x34", "0x6", "0x3258");
5811			/* LAN4 vlan tag */
5812			eval("et", "robowr", "0x34", "0x18", "0x0258");
5813		}
5814		else if (nvram_match("switch_wantag", "unifi_biz")) {
5815			/* Modify vlan500ports= 0 5 */
5816			eval("et", "robowr", "0x34", "0x8", "0x011f4021", "0x4");
5817			eval("et", "robowr", "0x34", "0x6", "0x31f4");
5818		}
5819		else if (nvram_match("switch_wantag", "singtel_mio")) {
5820			/* vlan1ports= 1 2 5 */
5821			eval("et", "robowr", "0x34", "0x8", "0x010011a6", "0x4");
5822			eval("et", "robowr", "0x34", "0x6", "0x3001");
5823			/* vlan10ports= 0 5 */
5824			eval("et", "robowr", "0x34", "0x8", "0x0100a021", "0x4");
5825			eval("et", "robowr", "0x34", "0x6", "0x300a");
5826			/* vlan20ports= 4 0 */
5827			eval("et", "robowr", "0x34", "0x8", "0x01014411", "0x4");
5828			eval("et", "robowr", "0x34", "0x6", "0x3014");
5829			/* vlan30ports= 3 0 */
5830			eval("et", "robowr", "0x34", "0x8", "0x0101e009", "0x4");/*Just forward without untag*/
5831			eval("et", "robowr", "0x34", "0x6", "0x301e");
5832			/* LAN4 vlan tag & prio */
5833			eval("et", "robowr", "0x34", "0x18", "0x8014");
5834		}
5835		else if (nvram_match("switch_wantag", "singtel_others")) {
5836			/* vlan1ports= 1 2 3 5 */
5837			eval("et", "robowr", "0x34", "0x8", "0x010013ae", "0x4");
5838			eval("et", "robowr", "0x34", "0x6", "0x3001");
5839			/* vlan10ports= 0 5 */
5840			eval("et", "robowr", "0x34", "0x8", "0x0100a021", "0x4");
5841			eval("et", "robowr", "0x34", "0x6", "0x300a");
5842			/* vlan20ports= 0 4 */
5843			eval("et", "robowr", "0x34", "0x8", "0x01014411", "0x4");
5844			eval("et", "robowr", "0x34", "0x6", "0x3014");
5845			/* LAN4 vlan tag & prio */
5846			eval("et", "robowr", "0x34", "0x18", "0x8014");
5847		}
5848		else if (nvram_match("switch_wantag", "m1_fiber")) {
5849			/* vlan1ports= 1 2 4 5 */				 /*5432 1054 3210*/
5850			eval("et", "robowr", "0x34", "0x8", "0x010015b6", "0x4");/*0111|1011|0110*/
5851			eval("et", "robowr", "0x34", "0x6", "0x3001");
5852			/* vlan1103ports= 0 5 */
5853			eval("et", "robowr", "0x34", "0x8", "0x0144f021", "0x4");/*0000|0010|0001*/ /*Dont untag WAN port*/
5854			eval("et", "robowr", "0x34", "0x6", "0x344f");
5855			/* vlan1107ports= 3 0 */
5856			eval("et", "robowr", "0x34", "0x8", "0x01453009", "0x4");/*0000|0000|1001*/ /*Just forward without untag*/
5857			eval("et", "robowr", "0x34", "0x6", "0x3453");
5858		}
5859		else if (nvram_match("switch_wantag", "maxis_fiber")) {
5860			/* vlan1ports= 1 2 4 5 */				 /*5432 1054 3210*/
5861			eval("et", "robowr", "0x34", "0x8", "0x010015b6", "0x4");/*0111|1011|0110*/
5862			eval("et", "robowr", "0x34", "0x6", "0x3001");
5863			/* vlan621 ports= 0 5 */
5864			eval("et", "robowr", "0x34", "0x8", "0x0126d021", "0x4");/*0000|0010|0001*/ /*Dont untag WAN port*/
5865			eval("et", "robowr", "0x34", "0x6", "0x326d");
5866			/* vlan821/822 ports= 3 0 */
5867			eval("et", "robowr", "0x34", "0x8", "0x01335009", "0x4");/*0000|0000|1001*/ /*Just forward without untag*/
5868			eval("et", "robowr", "0x34", "0x6", "0x3335");
5869			eval("et", "robowr", "0x34", "0x8", "0x01336009", "0x4");/*0000|0000|1001*/ /*Just forward without untag*/
5870			eval("et", "robowr", "0x34", "0x6", "0x3336");
5871		}
5872		else if (nvram_match("switch_wantag", "maxis_fiber_sp")) {
5873			/* vlan1ports= 1 2 4 5 */				 /*5432 1054 3210*/
5874			eval("et", "robowr", "0x34", "0x8", "0x010015b6", "0x4");/*0111|1011|0110*/
5875			eval("et", "robowr", "0x34", "0x6", "0x3001");
5876			/* vlan11ports= 0 5 */
5877			eval("et", "robowr", "0x34", "0x8", "0x0100b021", "0x4");/*0000|0010|0001*/ /*Dont untag WAN port*/
5878			eval("et", "robowr", "0x34", "0x6", "0x300b");
5879			/* vlan14ports= 3 0 */
5880			eval("et", "robowr", "0x34", "0x8", "0x0100e009", "0x4");/*0000|0000|1001*/ /*Just forward without untag*/
5881			eval("et", "robowr", "0x34", "0x6", "0x300e");
5882		}
5883		else {	/* manual */
5884							/* WAN L1 L2 L3 L4 CPU */
5885			const int ports[SWPORT_COUNT] = { 0, 1, 2, 3, 4, 5 };
5886
5887			if (switch_stb != SWCFG_STB4 && switch_stb != SWCFG_STB34)
5888				iptv_vid = 0;
5889			if (switch_stb != SWCFG_STB3 && switch_stb != SWCFG_STB34)
5890				voip_vid = 0;
5891			if (wan_vid) {
5892				sprintf(vlan_entry, "0x%x", BCM5325_ventry(wan_vid, wan_vid, iptv_vid, voip_vid));
5893				eval("et", "robowr", "0x34", "0x8", vlan_entry, "0x4");
5894				eval("et", "robowr", "0x34", "0x6", "0x3002");
5895			}
5896			if (iptv_vid) {
5897				if (iptv_vid != wan_vid) {
5898					sprintf(vlan_entry, "0x%x", BCM5325_ventry(iptv_vid, wan_vid, iptv_vid, voip_vid));
5899					eval("et", "robowr", "0x34", "0x8", vlan_entry, "0x4");
5900					eval("et", "robowr", "0x34", "0x6", "0x3003");
5901				}
5902				sprintf(tag_register, "0x%x", (iptv_prio << 13) | iptv_vid);
5903				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN4]);
5904				eval("et", "robowr", "0x34", port_id, tag_register);
5905			}
5906			if (voip_vid) {
5907				if (voip_vid != wan_vid && voip_vid != iptv_vid) {
5908					sprintf(vlan_entry, "0x%x", BCM5325_ventry(voip_vid, wan_vid, iptv_vid, voip_vid));
5909					eval("et", "robowr", "0x34", "0x8", vlan_entry, "0x4");
5910					eval("et", "robowr", "0x34", "0x6", "0x3004");
5911				}
5912				sprintf(tag_register, "0x%x", (voip_prio << 13) | voip_vid);
5913				sprintf(port_id, "0x%x", 0x10 + 2*ports[SWPORT_LAN3]);
5914				eval("et", "robowr", "0x34", port_id, tag_register);
5915			}
5916		}
5917		break;
5918	}
5919	return;
5920}
5921
5922char *get_wlifname(int unit, int subunit, int subunit_x, char *buf)
5923{
5924	sprintf(buf, "wl%d.%d", unit, subunit);
5925
5926	return buf;
5927}
5928
5929int
5930wl_exist(char *ifname, int band)
5931{
5932	FILE *fp;
5933	char buf[128], *line;
5934	int ret = 1;
5935#ifdef RTCONFIG_QTN
5936	if (nvram_get_int("qtn_ready") == 1)
5937		return 1;
5938	else
5939		return -1;
5940#endif
5941	sprintf(buf, "wl -i %s bands", ifname);
5942	fp = popen(buf, "r");
5943	if (fp == NULL) {
5944		perror("popen");
5945		return 0;
5946	}
5947
5948	line = fgets(buf, sizeof(buf), fp);
5949	if ((line == NULL) ||
5950	    (strstr(line, "not found") != NULL) ||
5951	    (band == 1 && !strstr(line, "b ")) ||
5952	    (band == 2 && !strstr(line, "a "))) {
5953		_dprintf("No wireless %s interface!!!: %s\n", ifname, line ? : "");
5954		ret = 0;
5955	}
5956	pclose(fp);
5957
5958	return ret;
5959}
5960
5961void check_afterburner(void)
5962{
5963	char *p;
5964
5965	if (nvram_match("wl_afterburner", "off")) return;
5966	if ((p = nvram_get("boardflags")) == NULL) return;
5967
5968	if (strcmp(p, "0x0118") == 0) {			// G 2.2, 3.0, 3.1
5969		p = "0x0318";
5970	}
5971	else if (strcmp(p, "0x0188") == 0) {	// G 2.0
5972		p = "0x0388";
5973	}
5974	else if (strcmp(p, "0x2558") == 0) {	// G 4.0, GL 1.0, 1.1
5975		p = "0x2758";
5976	}
5977	else {
5978		return;
5979	}
5980
5981	nvram_set("boardflags", p);
5982
5983	if (!nvram_match("debug_abrst", "0")) {
5984		modprobe_r("wl");
5985		modprobe("wl");
5986	}
5987
5988
5989/*	safe?
5990
5991	unsigned long bf;
5992	char s[64];
5993
5994	bf = strtoul(p, &p, 0);
5995	if ((*p == 0) && ((bf & BFL_AFTERBURNER) == 0)) {
5996		sprintf(s, "0x%04lX", bf | BFL_AFTERBURNER);
5997		nvram_set("boardflags", s);
5998	}
5999*/
6000}
6001
6002void wlconf_pre()
6003{
6004	int unit = 0;
6005	char word[256], *next;
6006#ifdef RTCONFIG_BCMWL6
6007	char tmp[128], prefix[] = "wlXXXXXXXXXX_";
6008#endif
6009
6010	foreach (word, nvram_safe_get("wl_ifnames"), next) {
6011#ifdef RTCONFIG_BCMWL6
6012		snprintf(prefix, sizeof(prefix), "wl%d_", unit);
6013
6014		// early convertion for nmode setting
6015		generate_wl_para(unit, -1);
6016#ifdef RTCONFIG_QTN
6017		if (!strcmp(word, "wifi0")) break;
6018#endif
6019		if ((nvram_match(strcat_r(prefix, "nband", tmp), "1") &&
6020		     nvram_match(strcat_r(prefix, "vreqd", tmp), "1"))
6021#if !defined(RTCONFIG_BCM9) && !defined(RTAC56U) && !defined(RTAC56S)
6022		 || (nvram_match(strcat_r(prefix, "nband", tmp), "2") &&
6023		     nvram_get_int(strcat_r(prefix, "turbo_qam", tmp)))
6024#endif
6025		) {
6026#ifdef RTCONFIG_BCMARM
6027#if !defined(RTCONFIG_BCM9) && !defined(RTAC56U) && !defined(RTAC56S)
6028			if (nvram_match(strcat_r(prefix, "nband", tmp), "2"))
6029			{
6030				if (nvram_match(strcat_r(prefix, "turbo_qam", tmp), "1"))
6031					eval("wl", "-i", word, "vht_features", "3");
6032#ifdef RTCONFIG_BCM_7114
6033				else if (nvram_match(strcat_r(prefix, "turbo_qam", tmp), "2"))
6034					eval("wl", "-i", word, "vht_features", "7");
6035#endif
6036			}
6037#endif
6038#ifdef RTCONFIG_BCM_7114
6039			else if (nvram_match(strcat_r(prefix, "nband", tmp), "1")) {
6040				if (nvram_match(strcat_r(prefix, "turbo_qam", tmp), "2"))
6041					eval("wl", "-i", word, "vht_features", "4");
6042			}
6043#endif
6044#endif  // RTCONFIG_BCMARM
6045			dbG("set vhtmode 1\n");
6046			eval("wl", "-i", word, "vhtmode", "1");
6047		}
6048		else
6049		{
6050			dbG("set vhtmode 0\n");
6051			eval("wl", "-i", word, "vht_features", "0");
6052			eval("wl", "-i", word, "vhtmode", "0");
6053		}
6054#endif	// RTCONFIG_BCMWL6
6055		unit++;
6056	}
6057}
6058
6059void wlconf_post(const char *ifname)
6060{
6061	int unit = -1;
6062	char prefix[] = "wlXXXXXXXXXX_";
6063
6064	if (ifname == NULL) return;
6065
6066	// get the instance number of the wl i/f
6067	if (wl_ioctl((char *) ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)))
6068		return;
6069
6070	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
6071
6072#ifdef RTAC66U
6073	char tmp[100];
6074	if (!strcmp(ifname, "eth2")) {
6075		if (nvram_match(strcat_r(prefix, "country_code", tmp), "Q2") &&
6076			nvram_match(strcat_r(prefix, "country_rev", tmp), "33"))
6077		eval("wl", "-i", ifname, "radioreg", "0x892", "0x5068", "cr0");
6078	}
6079#endif
6080
6081#ifdef RTCONFIG_BCMWL6
6082	if (is_ure(unit))
6083		eval("wl", "-i", (char *) ifname, "allmulti", "1");
6084#endif
6085}
6086
6087#ifdef RTCONFIG_BCMWL6
6088void set_acs_ifnames()
6089{
6090	char acs_ifnames[64];
6091	char word[256], *next;
6092	char tmp[128], prefix[] = "wlXXXXXXXXXX_";
6093	int unit;
6094	int dfs_in_use = 0;
6095
6096	wl_check_5g_band_group();
6097
6098	unit = 0;
6099	memset(acs_ifnames, 0, sizeof(acs_ifnames));
6100
6101	foreach (word, nvram_safe_get("wl_ifnames"), next) {
6102#ifdef RTCONFIG_QTN
6103		if (!strcmp(word, "wifi0")) break;
6104#endif
6105		snprintf(prefix, sizeof(prefix), "wl%d_", unit);
6106
6107		if (nvram_match(strcat_r(prefix, "radio", tmp), "1") &&
6108			nvram_match(strcat_r(prefix, "mode", tmp), "ap") &&
6109			(	nvram_match(strcat_r(prefix, "chanspec", tmp), "0") ||
6110				nvram_match(strcat_r(prefix, "bw", tmp), "0")))
6111		{
6112#if 0
6113			if (nvram_match(strcat_r(prefix, "bw", tmp), "0"))
6114				nvram_set(strcat_r(prefix, "chanspec", tmp), "0");
6115#endif
6116			if (strlen(acs_ifnames))
6117				sprintf(acs_ifnames, "%s %s", acs_ifnames, word);
6118			else
6119				sprintf(acs_ifnames, "%s", word);
6120		}
6121
6122		unit++;
6123	}
6124
6125	nvram_set("acs_ifnames", acs_ifnames);
6126
6127	/* exclude acsd from selecting chanspec 12, 12u, 13, 13u, 14, 14u */
6128	nvram_set("wl0_acs_excl_chans", "0x100c,0x190a,0x100d,0x190b,0x100e,0x190c");
6129
6130#if defined(RTAC3200)
6131	if (nvram_match("wl1_country_code", "E0")) {
6132		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80, 100, 100l, 100/80, 104, 104u, 104/80, 108, 108l, 108/80, 112, 112u, 112/80, 116, 132, 132l, 136, 136u, 140 */
6133		nvram_set("wl1_acs_excl_chans",
6134			  "0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a,0xd064,0xd866,0xe06a,0xd068,0xd966,0xe16a,0xd06c,0xd86e,0xe26a,0xd070,0xd96e,0xe36a,0xd074,0xd084,0xd886,0xd088,0xd986,0xd08c");
6135		/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6136		nvram_set("wl2_acs_excl_chans",
6137			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a");
6138	} else if (nvram_match("wl1_country_code", "JP")) {
6139		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80, 100, 100l, 100/80, 104, 104u, 104/80, 108, 108l, 108/80, 112, 112u, 112/80, 116, 116l, 116/80, 120, 120u, 120/80, 124, 124l, 124/80, 128, 128u, 128/80, 132, 132l, 136, 136u, 140 */
6140		nvram_set("wl1_acs_excl_chans",
6141			  "0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a,0xd064,0xd866,0xe06a,0xd068,0xd966,0xe16a,0xd06c,0xd86e,0xe26a,0xd070,0xd96e,0xe36a,0xd074,0xd876,0xe07a,0xd078,0xd976,0xe17a,0xd07c,0xd87e,0xe27a,0xd080,0xd97e,0xe37a,0xd084,0xd886,0xd088,0xd986,0xd08c");
6142		/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6143		nvram_set("wl2_acs_excl_chans",
6144			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a");
6145	} else {
6146		/* exclude acsd from selecting chanspec 149, 149l, 149/80, 153, 153u, 153/80,157, 157l, 157/80, 161, 161u, 161/80, 165 */
6147		nvram_set("wl1_acs_excl_chans",
6148			  "0xd095,0xd897,0xe09b,0xd099,0xd997,0xe19b,0xd09d,0xd89f,0xe29b,0xd0a1,0xd99f,0xe39b,0xd0a5");
6149		/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 165 */
6150		nvram_set("wl2_acs_excl_chans",
6151			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd0a5");
6152	}
6153#elif defined(RTAC5300)|| defined(RTAC5300R)
6154	if (nvram_match("wl1_country_code", "E0")) {
6155		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80*/
6156		nvram_set("wl1_acs_excl_chans",
6157			  "0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a");
6158		/* exclude acsd from selecting chanspec 120, 120u, 120/80, 124, 124l, 124/80, 128, 128u, 128/80*/
6159		nvram_set("wl2_acs_excl_chans",
6160			  "0xd078,0xd976,0xe17a,0xd07c,0xd87e,0xe27a,0xd080,0xd97e,0xe37a");
6161	} else if (nvram_match("wl1_country_code", "JP")) {
6162		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80, 100, 100l, 100/80, 104, 104u, 104/80, 108, 108l, 108/80, 112, 112u, 112/80, 116, 116l, 116/80, 120, 120u, 120/80, 124, 124l, 124/80, 128, 128u, 128/80, 132, 132l, 136, 136u, 140 */
6163		nvram_set("wl1_acs_excl_chans",
6164			  "0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a,0xd064,0xd866,0xe06a,0xd068,0xd966,0xe16a,0xd06c,0xd86e,0xe26a,0xd070,0xd96e,0xe36a,0xd074,0xd876,0xe07a,0xd078,0xd976,0xe17a,0xd07c,0xd87e,0xe27a,0xd080,0xd97e,0xe37a,0xd084,0xd886,0xd088,0xd986,0xd08c");
6165		/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6166		nvram_set("wl2_acs_excl_chans",
6167			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a");
6168	} else {
6169		/* exclude acsd from selecting chanspec 149, 149l, 149/80, 153, 153u, 153/80,157, 157l, 157/80, 161, 161u, 161/80, 165 */
6170		nvram_set("wl1_acs_excl_chans",
6171			  "0xd095,0xd897,0xe09b,0xd099,0xd997,0xe19b,0xd09d,0xd89f,0xe29b,0xd0a1,0xd99f,0xe39b,0xd0a5");
6172		/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 165 */
6173		nvram_set("wl2_acs_excl_chans",
6174			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd0a5");
6175	}
6176#elif defined(RTAC88U) || defined(RTAC3100)
6177	if (nvram_match("wl1_country_code", "E0") || nvram_match("wl1_country_code", "JP") || nvram_match("mr_test", "1"))
6178		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6179		nvram_set("wl1_acs_excl_chans",
6180			  "0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a");
6181	else if (nvram_match("acs_band1", "1"))
6182		nvram_set("wl1_acs_excl_chans", "0xd0a5");
6183	else
6184		/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 165 */
6185		nvram_set("wl1_acs_excl_chans",
6186			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd0a5");
6187#elif defined(RTAC1200GP)
6188	if (nvram_match("wl1_country_code", "E0"))
6189		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6190		nvram_set("wl1_acs_excl_chans",
6191			  "0xd034,0xd836,0xe03a,0xd038,0xd936,0xe13a,0xd03c,0xd83e,0xe23a,0xd040,0xd93e,0xe33a");
6192	else if (nvram_match("acs_band1", "1"))
6193		/* exclude acsd from selecting chanspec 165 */
6194		nvram_set("wl1_acs_excl_chans", "0xd0a5");
6195	else	/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 165 */
6196		nvram_set("wl1_acs_excl_chans",
6197			  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd0a5");
6198#else
6199	if (nvram_match("wl1_country_code", "EU"))
6200	{
6201		if (nvram_match("acs_dfs", "1")
6202#ifdef RTAC66U
6203			&& nvram_match("wl1_dfs", "1")
6204#endif
6205		) {
6206			/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6207			nvram_set("wl1_acs_excl_chans",
6208				  "0xd034,0xe03a,0xd836,0xd038,0xe13a,0xd936,0xd03c,0xe23a,0xd83e,0xd040,0xe33a,0xd93e");
6209			dfs_in_use = 1;
6210		}
6211		else	/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80, 100, 100l, 100/80, 104, 104u, 104/80, 108, 108l, 108/80, 112, 112u, 112/80, 116, 132, 132l, 136, 136u, 140 */
6212			nvram_set("wl1_acs_excl_chans",
6213				  "0xd034,0xe03a,0xd836,0xd038,0xe13a,0xd936,0xd03c,0xe23a,0xd83e,0xd040,0xe33a,0xd93e,0xd064,0xd866,0xe06a,0xd068,0xd966,0xe16a,0xd06c,0xd86e,0xe26a,0xd070,0xd96e,0xe36a,0xd074,0xd084,0xd886,0xd088,0xd986,0xd08c");
6214	}
6215	else if (nvram_match("wl1_country_code", "JP"))
6216	{
6217		/* exclude acsd from selecting chanspec 52, 52l, 52/80, 56, 56u, 56/80, 60, 60l, 60/80, 64, 64u, 64/80 */
6218		nvram_set("wl1_acs_excl_chans",
6219			  "0xd034,0xe03a,0xd836,0xd038,0xe13a,0xd936,0xd03c,0xe23a,0xd83e,0xd040,0xe33a,0xd93e");
6220	}
6221	else
6222	{
6223		if (nvram_match("acs_band1", "1"))
6224			/* exclude acsd from selecting chanspec 165 */
6225			nvram_set("wl1_acs_excl_chans", "0xd0a5");
6226		else	/* exclude acsd from selecting chanspec 36, 36l, 36/80, 40, 40u, 40/80, 44, 44l, 44/80, 48, 48u, 48/80, 165 */
6227			nvram_set("wl1_acs_excl_chans",
6228				  "0xd024,0xd826,0xe02a,0xd028,0xd926,0xe12a,0xd02c,0xd82e,0xe22a,0xd030,0xd92e,0xe32a,0xd0a5");
6229	}
6230#endif
6231
6232	nvram_set_int("wl1_acs_dfs", dfs_in_use ? 2 : 0);
6233}
6234#endif
6235
6236#ifdef RTCONFIG_PORT_BASED_VLAN
6237int check_used_stb_voip_port(int lan)
6238{
6239	int used = 0;
6240	int used_port = 0;
6241
6242	/* L4 L3 L2 L1 */
6243	/* 8  4  2  1 */
6244	if (!nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", "")) {
6245		if (!strcmp(nvram_safe_get("switch_wantag"), "unifi_home"))
6246			used_port = 0x8;	/* LAN4 for stb */
6247		if (!strcmp(nvram_safe_get("switch_wantag"), "unifi_biz"))
6248			used_port = 0x0;
6249		else if(!strcmp(nvram_safe_get("switch_wantag"), "singtel_mio"))
6250			used_port = 0xc;	/* LAN4 for stb, LAN3 for voip */
6251		else if(!strcmp(nvram_safe_get("switch_wantag"), "singtel_others"))
6252			used_port = 0x8;	/* LAN4 for stb */
6253		else if(!strcmp(nvram_safe_get("switch_wantag"), "m1_fiber"))
6254			used_port = 0x4;	/* LAN3 for voip */
6255		else if(!strcmp(nvram_safe_get("switch_wantag"), "maxis_fiber"))
6256			used_port = 0x4;	/* LAN3 for voip */
6257		else if(!strcmp(nvram_safe_get("switch_wantag"), "maxis_fiber_sp"))
6258			used_port = 0x4;	/* LAN3 for voip */
6259		else	/* For manual */
6260		{
6261			if(strcmp(nvram_safe_get("switch_wan1tagid"), ""))
6262				used_port += 0x8;	/* LAN4 for stb */
6263
6264			if(strcmp(nvram_safe_get("switch_wan2tagid"), ""))
6265				used_port += 0x4;	/* LAN3 for voip */
6266		}
6267	}
6268	else	/* For none */
6269	{
6270		int stbport = 0;
6271
6272		stbport = nvram_get_int("switch_stb_x");
6273		if (stbport < 0 || stbport > 6)
6274			stbport = 0;
6275
6276		if (stbport >= 1 && stbport <= 4)
6277			used_port = 1 << (stbport - 1);
6278		else if (stbport == 5)	/* LAN1 & LAN2 */
6279			used_port = 0x3;
6280		else if (stbport == 6)	/* LAN3 & LAN4 */
6281			used_port = 0xc;
6282	}
6283
6284	if (lan & used_port)
6285		used = 1;
6286
6287	return used;
6288}
6289
6290unsigned int convert_vlan_entry(int tag_enable, int portset, char *tag_reg_val)
6291{
6292	int real_portset = 0;
6293	int model, i;
6294	int port_shift_bit[] = { 0, 0, 0, 0};	/* shift bit for LAN X */
6295	char *port_tag_reg[] = { "0x10", "0x12", "0x14", "0x16", "0x18", "0x1a" };
6296	unsigned int vlan_entry = 0;
6297
6298	model = get_model();
6299
6300	/* P0  P1 P2 P3 P4 */
6301	/* WAN L4 L3 L2 L1 */
6302	if (model == MODEL_RTN16) {
6303		port_shift_bit[0] = 4;
6304		port_shift_bit[1] = 3;
6305		port_shift_bit[2] = 2;
6306		port_shift_bit[3] = 1;
6307	}
6308	/* P0  P1 P2 P3 P5 */
6309	/* WAN L4 L3 L2 L1 */
6310	else if (model == MODEL_RTAC87U) {
6311		port_shift_bit[0] = 5;
6312		port_shift_bit[1] = 3;
6313		port_shift_bit[2] = 2;
6314		port_shift_bit[3] = 1;
6315	}
6316	/* P0  P1 P2 P3 P4 */
6317	/* WAN L1 L2 L3 L4 */
6318	else if (model == MODEL_RTAC68U || model == MODEL_RTN18U ||
6319		model == MODEL_RTN66U || model == MODEL_RTAC66U ||
6320		model == MODEL_DSLAC68U) {
6321		port_shift_bit[0] = 1;
6322		port_shift_bit[1] = 2;
6323		port_shift_bit[2] = 3;
6324		port_shift_bit[3] = 4;
6325	}
6326	/* P0 P1 P2 P3 P4 */
6327	/* L1 L2 L3 L4 WAN */
6328	else if (model == MODEL_RTAC56S || model == MODEL_RTAC56U) {
6329		port_shift_bit[0] = 0;
6330		port_shift_bit[1] = 1;
6331		port_shift_bit[2] = 2;
6332		port_shift_bit[3] = 3;
6333	}
6334	/* P0 P1 P2 P3 P4 */
6335	/* L4 L3 L2 L1 WAN */
6336	else if (model == MODEL_RTN15U) {
6337		port_shift_bit[0] = 3;
6338		port_shift_bit[1] = 2;
6339		port_shift_bit[2] = 1;
6340		port_shift_bit[3] = 0;
6341	}
6342
6343	/* Convert the port set of web to real port set of switch */
6344	_dprintf("%s: temp portset=0x%x\n", __FUNCTION__, portset);
6345	for (i = 0; i < sizeof(port_shift_bit)/sizeof(int); i++) {
6346#ifdef RTCONFIG_DUALWAN
6347		int wancfg = nvram_get_int("wans_lanport");
6348		if ((get_wans_dualwan() & WANSCAP_LAN) && wancfg >= 1 && wancfg <= 4) {
6349			/* Filter lan port as wan */
6350			if ((i == (wancfg -1)) && (portset & (1 << (wancfg - 1))))
6351				continue;
6352		}
6353#endif
6354		if (portset & (1 << i) && !check_used_stb_voip_port(1 << i))
6355			real_portset |= (1 << port_shift_bit[i]);
6356	}
6357	_dprintf("%s: real portset=0x%x\n", __FUNCTION__, real_portset);
6358
6359	/* Set the temporary value of vlan entry for port 0 ~ port 4 */
6360	for (i = 0; i < sizeof(port_shift_bit)/sizeof(int); i++) {
6361		int port_val = real_portset & (1 << port_shift_bit[i]);
6362
6363		if (port_val) {
6364			eval("et", "robowr", "0x34", port_tag_reg[port_shift_bit[i]], tag_reg_val);//
6365			_dprintf("%s: port tag reg=%s\n", __FUNCTION__, port_tag_reg[port_shift_bit[i]]);
6366
6367			if (tag_enable)
6368				vlan_entry |= (1 << port_shift_bit[i]);
6369			else
6370				vlan_entry |= ((1 << (9 + port_shift_bit[i])) | (1 << port_shift_bit[i]));
6371		}
6372	}
6373
6374	return vlan_entry;
6375}
6376
6377unsigned int convert_vlan_entry_bcm5325(int tag_enable, int portset, char *tag_reg_val)
6378{
6379	int real_portset = 0;
6380	int model, i;
6381	int port_shift_bit[] = { 0, 0, 0, 0};	/* shift bit for LAN X */
6382	char *port_tag_reg[] = { "0x10", "0x12", "0x14", "0x16", "0x18", "0x1a" };
6383	unsigned int vlan_entry = 0;
6384
6385	model = get_model();
6386
6387	/* P0 P1 P2 P3 P4 */
6388	/* L4 L3 L2 L1 WAN */
6389	if (model == MODEL_APN12HP || model == MODEL_RTN53 || model == MODEL_RTN12 ||
6390		model == MODEL_RTN12B1 || model == MODEL_RTN12C1 || model == MODEL_RTN12D1 ||
6391		model == MODEL_RTN12VP || model == MODEL_RTN12HP || model == MODEL_RTN12HP_B1 ||
6392		model == MODEL_RTN10P || model == MODEL_RTN10D1 || model == MODEL_RTN10PV2) {
6393		port_shift_bit[0] = 3;
6394		port_shift_bit[1] = 2;
6395		port_shift_bit[2] = 1;
6396		port_shift_bit[3] = 0;
6397	}
6398	/* P0 P1 P2 P3 P4 */
6399	/* L1 L2 L3 L4 WAN */
6400	else if (model == MODEL_RTN14UHP || model == MODEL_RTN10U) {
6401		port_shift_bit[0] = 0;
6402		port_shift_bit[1] = 1;
6403		port_shift_bit[2] = 2;
6404		port_shift_bit[3] = 3;
6405	}
6406	/* P0  P1 P2 P3 P4 */
6407	/* WAN L1 L2 L3 L4 */
6408	else if (model == MODEL_RTAC53U) {
6409		port_shift_bit[0] = 1;
6410		port_shift_bit[1] = 2;
6411		port_shift_bit[2] = 3;
6412		port_shift_bit[3] = 4;
6413	}
6414
6415	/* Convert the port set of web to real port set of switch */
6416	_dprintf("%s: temp portset=0x%x\n", __FUNCTION__, portset);
6417	for (i = 0; i < sizeof(port_shift_bit)/sizeof(int); i++) {
6418#ifdef RTCONFIG_DUALWAN
6419		int wancfg = nvram_get_int("wans_lanport");
6420		if ((get_wans_dualwan() & WANSCAP_LAN) && wancfg >= 1 && wancfg <= 4) {
6421			/* Filter lan port as wan */
6422			if ((i == (wancfg -1)) && (portset & (1 << (wancfg - 1))))
6423				continue;
6424		}
6425#endif
6426		if (portset & (1 << i) && !check_used_stb_voip_port(1 << i))
6427			real_portset |= (1 << port_shift_bit[i]);
6428	}
6429	_dprintf("%s: real portset=0x%x\n", __FUNCTION__, real_portset);
6430
6431	/* Set the temporary value of vlan entry for port 0 ~ port 4 */
6432	for (i = 0; i < sizeof(port_shift_bit)/sizeof(int); i++) {
6433		int port_val = real_portset & (1 << port_shift_bit[i]);
6434
6435		if (port_val) {
6436			eval("et", "robowr", "0x34", port_tag_reg[port_shift_bit[i]], tag_reg_val);//
6437			_dprintf("%s: port tag reg=0x%s\n", __FUNCTION__, port_tag_reg[port_shift_bit[i]]);
6438
6439			if (tag_enable)
6440				vlan_entry |= (1 << port_shift_bit[i]);
6441			else
6442				vlan_entry |= ((1 << (6 + port_shift_bit[i])) | (1 << port_shift_bit[i]));
6443		}
6444	}
6445
6446	return vlan_entry;
6447}
6448
6449void set_port_based_vlan_config(char *interface)
6450{
6451	char *nv, *nvp, *b;
6452	//char *enable, *vid, *priority, *portset, *wlmap, *subnet_name;
6453	//char *portset, *wlmap, *subnet_name;
6454	char *enable, *desc, *portset, *wlmap, *subnet_name, *intranet;
6455	int set_flag = (interface != NULL) ? 1 : 0;
6456
6457	/* Clean some parameters for vlan */
6458	//clean_vlan_ifnames();
6459
6460	if (vlan_enable()) {
6461		nv = nvp = strdup(nvram_safe_get("vlan_rulelist"));
6462
6463		if (nv) {
6464			int vlan_tag = 4;
6465			int model;
6466			int br_index = 1;
6467
6468			model = get_model();
6469
6470			if (model == MODEL_DSLAC68U)
6471				vlan_tag = 5;
6472
6473			while ((b = strsep(&nvp, "<")) != NULL) {
6474				//int real_portset = 0;
6475				char tag_reg_val[7], vlan_id[9], vlan_entry[12];
6476				unsigned int vlan_entry_tmp = 0, tag_reg_val_tmp = 0;
6477				int i = 0, vlan_id_tmp = 0;
6478				int cpu_port = 0;
6479
6480				//if ((vstrsep(b, ">", &enable, &vid, &priority, &portset, &wlmap, &subnet_name) != 6))
6481				//if ((vstrsep(b, ">", &portset, &wlmap, &subnet_name) != 3))
6482				if ((vstrsep(b, ">", &enable, &desc, &portset, &wlmap, &subnet_name, &intranet) != 6))
6483					continue;
6484
6485				//_dprintf("%s: %s %s %s %s %s %s\n", __FUNCTION__, enable, vid, priority, portset, wlmap, subnet_name);
6486				_dprintf("%s: %s %s %s %s %s %s\n", __FUNCTION__, enable, desc, portset, wlmap, subnet_name, intranet);
6487
6488				if (!strcmp(enable, "0") || strlen(enable) == 0)
6489					continue;
6490
6491				//real_portset = atoi(portset);
6492				//real_portset = convert_portset(atoi(portset));
6493				//_dprintf("%s: real port set=0x%x\n", __FUNCTION__, real_portset);
6494
6495				/* Port mapping of the switch for MODEL_RTN16 */
6496				/* P0  P1 P2 P3 P4 P8 */
6497				/* WAN L4 L3 L2 L1 CPU */
6498				/* Port mapping of the switch for MODEL_RTAC68U & MODEL_RTN18U */
6499				/* P0  P1 P2 P3 P4 P5 */
6500				/* WAN L1 L2 L3 L4 CPU */
6501				/* Port mapping of the switch for MODEL_RTAC87U */
6502				/* P0  P1 P2 P3 P5 P7 */
6503				/* WAN L4 L3 L2 L1 CPU */
6504				/* Port mapping of the switch for MODEL_RTAC56S & MODEL_RTAC56U */
6505				/* P0 P1 P2 P3 P4  P5 */
6506				/* L1 L2 L3 L4 WAN CPU */
6507				/* Port mapping of the switch for MODEL_RTN66U & MODEL_RTAC66U */
6508				/* P0  P1 P2 P3 P4 P8 */
6509				/* WAN L1 L2 L3 L4 CPU */
6510				/* Port mapping of the switch for MODEL_RTN15U */
6511				/* P0 P1 P2 P3 P4  P8 */
6512				/* L1 L2 L3 L4 WAN CPU */
6513				/* Port mapping of the switch for MODEL_DSLAC68U */
6514				/* P0  P1 P2 P3 P4 P5 */
6515				/* WAN L1 L2 L3 L4 CPU */
6516				if (model == MODEL_RTN16 ||
6517					model == MODEL_RTAC68U || model == MODEL_RTN18U ||
6518					model == MODEL_RTAC87U ||
6519					model == MODEL_RTAC56S || model == MODEL_RTAC56U ||
6520					model == MODEL_RTN66U || model == MODEL_RTAC66U ||
6521					model == MODEL_RTN15U ||
6522					model == MODEL_DSLAC68U) {
6523					/*char tag_reg_val[7], vlan_id[9], vlan_entry[7];
6524					unsigned int vlan_entry_tmp = 0, tag_reg_val_tmp = 0;
6525					int i = 0, vlan_id_tmp = 0;
6526					//char *port_tag_reg[] = { "0x12", "0x14", "0x16", "0x18" };
6527					int cpu_port = 0;*/
6528
6529					/* Decide cpu port by model */
6530					if (model == MODEL_RTAC68U || model == MODEL_RTN18U ||
6531						model == MODEL_RTAC56S || model == MODEL_RTAC56U ||
6532						model == MODEL_DSLAC68U)
6533						cpu_port = 5;
6534					else if (model == MODEL_RTAC87U)
6535						cpu_port = 7;
6536					else if (model == MODEL_RTN16 ||
6537						model == MODEL_RTN66U || model == MODEL_RTAC66U ||
6538						MODEL_RTN15U)
6539						cpu_port = 8;
6540
6541					if (atoi(portset) != 0) {
6542						vlan_id_tmp = vlan_tag;
6543						tag_reg_val_tmp = vlan_tag;
6544						vlan_tag++;
6545
6546						snprintf(tag_reg_val, sizeof(tag_reg_val), "0x%x", tag_reg_val_tmp);
6547						_dprintf("%s: tag register value=%s\n", __FUNCTION__, tag_reg_val);
6548
6549						if (set_flag) {
6550							/* Set vlan entry for port 0 ~ port 4 */
6551							//vlan_entry_tmp = convert_vlan_entry(atoi(enable), atoi(portset), tag_reg_val);
6552							vlan_entry_tmp = convert_vlan_entry(0, atoi(portset), tag_reg_val);
6553
6554							/* Set vlan entry for cpu port */
6555							vlan_entry_tmp |= (1 << cpu_port);
6556						}
6557
6558						/* Set vlan table entry register */
6559						snprintf(vlan_id, sizeof(vlan_id), "0x%x", vlan_id_tmp);
6560						_dprintf("%s: vlan id=%s\n", __FUNCTION__, vlan_id);
6561						snprintf(vlan_entry, sizeof(vlan_entry), "0x%x", vlan_entry_tmp);
6562						_dprintf("%s: vlan entry=%s\n", __FUNCTION__, vlan_entry);
6563						if (set_flag) {
6564							eval("et", "robowr", "0x05", "0x83", vlan_entry);
6565							eval("et", "robowr", "0x05", "0x81", vlan_id);
6566#if !defined(RTAC87U)
6567							eval("et", "robowr", "0x05", "0x80", "0x0000");
6568#endif
6569							eval("et", "robowr", "0x05", "0x80", "0x0080");
6570
6571							/* Create the VLAN interface */
6572							snprintf(vlan_id, sizeof(vlan_id), "%d", vlan_id_tmp);
6573							eval("vconfig", "add", interface, vlan_id);
6574
6575							/* Setup ingress map (vlan->priority => skb->priority) */
6576							snprintf(vlan_id, sizeof(vlan_id), "vlan%d", vlan_id_tmp);
6577							for (i = 0; i < VLAN_NUMPRIS; i++) {
6578								char prio[8];
6579
6580								snprintf(prio, sizeof(prio), "%d", i);
6581								eval("vconfig", "set_ingress_map", vlan_id, prio, prio);
6582							}
6583						}
6584
6585						snprintf(vlan_id, sizeof(vlan_id), "vlan%d", vlan_id_tmp);
6586						set_vlan_ifnames(br_index, atoi(wlmap), subnet_name, vlan_id);
6587					}
6588					else	/* portset is 0, only for wireless */
6589					{
6590						set_vlan_ifnames(br_index, atoi(wlmap), subnet_name, NULL);
6591					}
6592				}
6593				/* Port mapping of the switch for MODEL_APN12HP, MODEL_RTN53, MODEL_RTN12
6594				   MODEL_RTN12B1, MODEL_RTN12C1, MODEL_RTN12D1, MODEL_RTN12VP, MODEL_RTN12HP
6595				   MODEL_RTN12HP_B1, MODEL_RTN10P, MODEL_RTN10D1 and MODEL_RTN10PV2 */
6596				/* P0 P1 P2 P3 P4  P5 */
6597				/* L4 L3 L2 L1 WAN CPU */
6598				/* Port mapping of the switch for MODEL_RTN14UHP and MODEL_RTN10U */
6599				/* P0 P1 P2 P3 P4  P5 */
6600				/* L1 L2 L3 L4 WAN CPU */
6601				/* Port mapping of the switch for MODEL_RTAC53U */
6602				/* P0  P1 P2 P3 P4 P5 */
6603				/* WAN L1 L2 L3 L4 CPU */
6604				else
6605				{
6606					/*char tag_reg_val[7], vlan_id[9], vlan_entry[12];
6607					unsigned int vlan_entry_tmp = 0, tag_reg_val_tmp = 0;
6608					int i = 0, vlan_id_tmp = 0;*/
6609					cpu_port = 5;
6610
6611					/* Enable high bits check */
6612					eval("et", "robowr", "0x34", "0x3", "0x0080");
6613
6614					if (atoi(portset) != 0) {
6615						vlan_id_tmp = vlan_tag;
6616						tag_reg_val_tmp = vlan_tag;
6617						vlan_tag++;
6618
6619						snprintf(tag_reg_val, sizeof(tag_reg_val), "0x%x", tag_reg_val_tmp);
6620						_dprintf("%s: tag register value=%s\n", __FUNCTION__, tag_reg_val);
6621
6622						if (set_flag) {
6623							/* Set vlan entry for port 0 ~ port 4 */
6624							//vlan_entry_tmp = convert_vlan_entry_bcm5325(atoi(enable), atoi(portset), tag_reg_val);
6625							vlan_entry_tmp = convert_vlan_entry_bcm5325(0, atoi(portset), tag_reg_val);
6626
6627							/* Set vlan entry for cpu port */
6628							vlan_entry_tmp |= (1 << cpu_port);
6629						}
6630
6631						/* Set vlan table entry register */
6632						snprintf(vlan_id, sizeof(vlan_id), "0x%x", ((1 << 13) |	(1 << 12) | vlan_id_tmp));
6633						_dprintf("%s: vlan id=%s\n", __FUNCTION__, vlan_id);
6634						snprintf(vlan_entry, sizeof(vlan_entry), "0x%x", ((1 << 24) | (vlan_id_tmp << 12) | vlan_entry_tmp));
6635						_dprintf("%s: vlan entry=%s\n", __FUNCTION__, vlan_entry);
6636
6637						if (set_flag) {
6638							eval("et", "robowr", "0x34", "0x8", vlan_entry);
6639							eval("et", "robowr", "0x34", "0x6", vlan_id);
6640
6641							/* Create the VLAN interface */
6642							snprintf(vlan_id, sizeof(vlan_id), "%d", vlan_id_tmp);
6643							eval("vconfig", "add", interface, vlan_id);
6644
6645							/* Setup ingress map (vlan->priority => skb->priority) */
6646							snprintf(vlan_id, sizeof(vlan_id), "vlan%d", vlan_id_tmp);
6647							for (i = 0; i < VLAN_NUMPRIS; i++) {
6648								char prio[8];
6649
6650								snprintf(prio, sizeof(prio), "%d", i);
6651								eval("vconfig", "set_ingress_map", vlan_id, prio, prio);
6652							}
6653						}
6654
6655						snprintf(vlan_id, sizeof(vlan_id), "vlan%d", vlan_id_tmp);
6656						set_vlan_ifnames(br_index, atoi(wlmap), subnet_name, vlan_id);
6657					}
6658					else	/* portset is 0, only for wireless */
6659					{
6660						set_vlan_ifnames(br_index, atoi(wlmap), subnet_name, NULL);
6661					}
6662				}
6663
6664				br_index++;
6665			}
6666			free(nv);
6667		}
6668	}
6669	return;
6670}
6671#endif
6672