1#include "rc.h"
2#include "shared.h"
3
4int cur_ewan_vid = 0;
5
6static int  check_ewan_dot1q_enable(const int enable, const int vid)
7{
8	if(enable)
9	{
10		if(vid >= 4 && !(vid >= 100 && vid <= 107))
11			return 1;
12	}
13	return 0;
14}
15
16
17void init_switch_dsl()
18{
19	// vlan1 => LAN PORT
20	// vlan2 => WAN to modem , send packet to modem , used on modem driver
21	// vlan3 => IPTV port
22	// vlan4 => WAN to Ethernet this is ethernet WAN ifname
23	int stbport;
24	int enable_dsl_wan_dsl_if = 0;
25	int enable_dsl_wan_eth_if = 0;
26	int enable_dsl_wan_iptv_if = 0;
27
28#ifdef RTCONFIG_DUALWAN
29	if (get_dualwan_secondary()==WANS_DUALWAN_IF_NONE)
30	{
31		if (get_dualwan_primary()==WANS_DUALWAN_IF_DSL)
32		{
33			enable_dsl_wan_dsl_if = 1;
34		}
35		else if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN)
36		{
37			enable_dsl_wan_eth_if = 1;
38		}
39	}
40	else
41	{
42		if (get_dualwan_primary()==WANS_DUALWAN_IF_DSL)
43		{
44			enable_dsl_wan_dsl_if = 1;
45		}
46		else if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN)
47		{
48			enable_dsl_wan_eth_if = 1;
49		}
50
51		if (get_dualwan_secondary()==WANS_DUALWAN_IF_DSL)
52		{
53			enable_dsl_wan_dsl_if = 1;
54		}
55		else if (get_dualwan_secondary()==WANS_DUALWAN_IF_LAN)
56		{
57			enable_dsl_wan_eth_if = 1;
58		}
59	}
60#else
61	enable_dsl_wan_dsl_if = 1;
62#endif
63
64	if (is_routing_enabled())
65	{
66		stbport = atoi(nvram_safe_get("switch_stb_x"));
67		if (stbport < 0 || stbport > 6) stbport = 0;
68		dbG("STB port: %d\n", stbport);
69
70		if (stbport > 0)
71		{
72			enable_dsl_wan_iptv_if = 1;
73		}
74	}
75
76	eval("ifconfig", "eth0", "up");
77	// vlan2 = WAN
78	eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
79	eval("vconfig", "add", "eth0", "2");
80	eval("ifconfig", "vlan2", "up");
81
82	if (enable_dsl_wan_dsl_if)
83	{
84		// DSL WAN MODE
85		eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
86		eval("vconfig", "add", "eth0", "100");
87		eval("ifconfig", "vlan100", "up");
88		eval("ifconfig", "vlan100", "hw", "ether", nvram_safe_get("et0macaddr"));
89	}
90
91	if (enable_dsl_wan_eth_if)
92	{
93		int ewan_dot1q = nvram_get_int("ewan_dot1q");	//Andy Chiu, 2015/09/08
94		int ewan_vid = nvram_get_int("ewan_vid");		//Andy Chiu, 2015/09/08
95		int ewan_dot1p = nvram_get_int("ewan_dot1p");	//Andy Chiu, 2015/09/08
96		char buf[32];
97
98		// vlan4 = ethenet WAN
99		eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
100		if(check_ewan_dot1q_enable(ewan_dot1q, ewan_vid))
101		{
102			sprintf(buf, "%d", ewan_vid);
103			eval("vconfig", "add", "eth0", buf);
104			sprintf(buf, "vlan%d", ewan_vid);
105			eval("ifconfig", buf, "up");
106			// ethernet WAN , it needs to use another MAC address
107			eval("ifconfig", buf, "hw", "ether", nvram_safe_get("et1macaddr"));
108			cur_ewan_vid = ewan_vid;
109		}
110		else
111		{
112			eval("vconfig", "add", "eth0", "4");
113			eval("ifconfig", "vlan4", "up");
114			// ethernet WAN , it needs to use another MAC address
115			eval("ifconfig", "vlan4", "hw", "ether", nvram_safe_get("et1macaddr"));
116			cur_ewan_vid = 4;
117		}
118	}
119
120	if (enable_dsl_wan_iptv_if)
121	{
122		// vlan3 = IPTV
123		eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
124		eval("vconfig", "add", "eth0", "3");
125		eval("ifconfig", "vlan3", "up");
126		// bypass , no need to have another MAC address
127	}
128
129}
130
131void config_switch_dsl_set_dsl()
132{
133	eval("et", "robowr", "0x05", "0x83", "0x0021");
134	eval("et", "robowr", "0x05", "0x81", "0x0064");
135	eval("et", "robowr", "0x05", "0x80", "0x0000");
136	eval("et", "robowr", "0x05", "0x80", "0x0080");
137}
138
139void config_switch_dsl_set_iptv(int stbport)
140{
141	if(stbport < 1 || stbport > 6)
142		return;
143
144	switch(stbport) {
145	case 1:
146		eval("et", "robowr", "0x34", "0x12", "0x0003");
147		eval("et", "robowr", "0x05", "0x83", "0x0422");
148		break;
149	case 2:
150		eval("et", "robowr", "0x34", "0x14", "0x0003");
151		eval("et", "robowr", "0x05", "0x83", "0x0824");
152		break;
153	case 3:
154		eval("et", "robowr", "0x34", "0x16", "0x0003");
155		eval("et", "robowr", "0x05", "0x83", "0x1028");
156		break;
157	case 4:
158		eval("et", "robowr", "0x34", "0x18", "0x0003");
159		eval("et", "robowr", "0x05", "0x83", "0x2030");
160		break;
161	case 5: // 1 & 2
162		eval("et", "robowr", "0x34", "0x12", "0x0003");
163		eval("et", "robowr", "0x34", "0x14", "0x0003");
164		eval("et", "robowr", "0x05", "0x83", "0x0C26");
165		break;
166	case 6:	// 3 & 4
167		eval("et", "robowr", "0x34", "0x16", "0x0003");
168		eval("et", "robowr", "0x34", "0x18", "0x0003");
169		eval("et", "robowr", "0x05", "0x83", "0x3038");
170		break;
171	}
172	eval("et", "robowr", "0x05", "0x81", "0x0003");
173	eval("et", "robowr", "0x05", "0x80", "0x0000");
174	eval("et", "robowr", "0x05", "0x80", "0x0080");
175
176}
177
178void config_switch_dsl_set_lan()
179{
180	int wans_lanport = nvram_get_int("wans_lanport");
181
182	char vid[32], buf[32];	//Andy Chiu, 2015/09/09
183	int ewan_dot1q = nvram_get_int("ewan_dot1q");	//Andy Chiu, 2015/09/08
184	int ewan_vid = nvram_get_int("ewan_vid");	//Andy Chiu, 2015/09/08
185	int ewan_dot1p = nvram_get_int("ewan_dot1p");	//Andy Chiu, 2015/09/08
186
187	if(wans_lanport < 1 || wans_lanport > 4)
188		return;
189
190	dbG("ewan_dot1q=%d, ewan_vid =%d, ewan_dot1p=%d\n", ewan_dot1q, ewan_vid, ewan_dot1p);
191	//Andy Chiu, 2015/09/09
192	if(check_ewan_dot1q_enable(ewan_dot1q, ewan_vid))
193		sprintf(vid, "0x%04x", ewan_vid);
194	else
195		strcpy(vid, "0x0004");
196
197	dbG("vid=%s\n", vid);
198
199	switch(wans_lanport) {
200	case 1:
201		dbG("wan port = lan1\n");
202		eval("et", "robowr", "0x34", "0x12", vid);
203		eval("et", "robowr", "0x05", "0x83", check_ewan_dot1q_enable(ewan_dot1q, ewan_vid)? "0x0022": "0x0422");
204		break;
205	case 2:
206		dbG("wan port = lan2\n");
207		eval("et", "robowr", "0x34", "0x14", vid);
208		eval("et", "robowr", "0x05", "0x83", check_ewan_dot1q_enable(ewan_dot1q, ewan_vid)? "0x0024": "0x0824");
209		break;
210	case 3:
211		dbG("wan port = lan3\n");
212		eval("et", "robowr", "0x34", "0x16", vid);
213		eval("et", "robowr", "0x05", "0x83", check_ewan_dot1q_enable(ewan_dot1q, ewan_vid)? "0x0028": "0x1028");
214		break;
215	case 4:
216		dbG("wan port = lan4\n");
217		eval("et", "robowr", "0x34", "0x18", vid);
218		eval("et", "robowr", "0x05", "0x83", check_ewan_dot1q_enable(ewan_dot1q, ewan_vid)? "0x0030": "0x2030");
219		break;
220	}
221	eval("et", "robowr", "0x05", "0x81", vid);
222	eval("et", "robowr", "0x05", "0x80", "0x0000");
223	eval("et", "robowr", "0x05", "0x80", "0x0080");
224
225	//Set proi, NOT work, need checking
226	dbG("checking<%d, %d>\n", check_ewan_dot1q_enable(ewan_dot1q, ewan_vid), ewan_dot1p);
227	if(check_ewan_dot1q_enable(ewan_dot1q, ewan_vid) && ewan_dot1p > 0)
228	{
229		sprintf(buf, "vlan%d", ewan_vid);
230		dbG("<%s, %s>\n", buf, nvram_get("ewan_dot1p"));
231		eval("vconfig", "set_egress_map", buf, "0", nvram_get("ewan_dot1p"));
232	}
233}
234
235void config_switch_dsl()
236{
237	int stbport;
238	dbG("config ethernet switch IC\n");
239
240	if (is_routing_enabled())
241	{
242		stbport = atoi(nvram_safe_get("switch_stb_x"));
243		if (stbport < 0 || stbport > 6) stbport = 0;
244		dbG("STB port: %d\n", stbport);
245
246#ifdef RTCONFIG_DUALWAN
247		if (get_dualwan_primary()==WANS_DUALWAN_IF_DSL)
248			config_switch_dsl_set_dsl();
249		if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN || get_dualwan_secondary()==WANS_DUALWAN_IF_LAN)
250			config_switch_dsl_set_lan();
251		config_switch_dsl_set_iptv(stbport);
252#else
253		config_switch_dsl_set_dsl();
254		config_switch_dsl_set_iptv(stbport);
255#endif
256	}
257}
258
259