1/*
2 TODO: merge 8367 control into one
3*/
4
5#include "rc.h"
6
7#include <termios.h>
8#include <dirent.h>
9#include <sys/ioctl.h>
10#include <sys/mount.h>
11#include <time.h>
12#include <errno.h>
13#include <paths.h>
14#include <sys/wait.h>
15#include <sys/reboot.h>
16#include <sys/klog.h>
17#ifdef LINUX26
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/sysinfo.h>
21#endif
22#include <wlutils.h>
23#include <bcmdevs.h>
24
25#include <shared.h>
26
27#ifdef RTCONFIG_RALINK
28#include <ralink.h>
29#endif
30
31#ifdef RTCONFIG_RALINK_RT3052
32#include <ra3052.h>
33#endif
34
35void check_upgrade_from_old_ui(void)
36{
37// 1=old ui
38// dsl_web_ui_ver is reserved for new web ui
39// when upgrade to new ui, it check this field and format NVRAM
40	if (nvram_match("dsl_web_ui_ver", "1")) {
41		dbg("Upgrade from old ui, erase NVRAM and reboot\n");
42		eval("mtd-erase","-d","nvram");
43		/* FIXME: all stop-wan, umount logic will not be called
44		 * prevous sys_exit (kill(1, SIGTERM) was ok
45		 * since nvram isn't valid stop_wan should just kill possible daemons,
46		 * nothing else, maybe with flag */
47		sync();
48		reboot(RB_AUTOBOOT);
49	}
50	return;
51}
52
53void init_switch_dsl()
54{
55	// DSLTODO
56
57	// Eth2 => SoC RGMII 0 => Because we use VLAN, we do not use this interface directly
58	// Eth2.1 => WAN to modem , send packet to modem , used on modem driver
59	// Eth2.2 => LAN PORT
60	// Eth 2.1.50 => for stats, ethernet driver modify modem packets to this VLAN ID
61	// Eth 2.1.51 => bridge for Ethernet and modem, ethernet driver modify modem packet to VLAN ID (51)
62	// and then bridge eth2.2 and eth2.1.51. thus, PC could control modem from LAN port
63	// Eth2.3 => IPTV port
64	// Eth2.4 => WAN to Ethernet this is ethernet WAN ifname
65	int stbport;
66	int enable_dsl_wan_dsl_if = 0;
67	int enable_dsl_wan_eth_if = 0;
68	int enable_dsl_wan_iptv_if = 0;
69
70
71	eval("ifconfig", "eth2", "up");
72	// eth2.1 = WAN
73	eval("vconfig", "add", "eth2", "1");
74	eval("ifconfig", "eth2.1", "up");
75	// eth2.2 = LAN
76	eval("vconfig", "add", "eth2", "2");
77	eval("ifconfig", "eth2.2", "up");
78	// eth2.3 = IPTV
79
80#ifdef RTCONFIG_DUALWAN
81	if (get_dualwan_secondary()==WANS_DUALWAN_IF_NONE)
82	{
83		if (get_dualwan_primary()==WANS_DUALWAN_IF_DSL)
84		{
85			enable_dsl_wan_dsl_if = 1;
86		}
87		else if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN)
88		{
89			enable_dsl_wan_eth_if = 1;
90		}
91	}
92	else
93	{
94		if (get_dualwan_primary()==WANS_DUALWAN_IF_DSL)
95		{
96			enable_dsl_wan_dsl_if = 1;
97		}
98		else if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN)
99		{
100			enable_dsl_wan_eth_if = 1;
101		}
102
103		if (get_dualwan_secondary()==WANS_DUALWAN_IF_DSL)
104		{
105			enable_dsl_wan_dsl_if = 1;
106		}
107		else if (get_dualwan_secondary()==WANS_DUALWAN_IF_LAN)
108		{
109			enable_dsl_wan_eth_if = 1;
110		}
111	}
112#else
113	enable_dsl_wan_dsl_if = 1;
114#endif
115
116	if (is_routing_enabled())
117	{
118		stbport = nvram_get_int("switch_stb_x");
119		if (stbport < 0 || stbport > 6) stbport = 0;
120		dbG("STB port: %d\n", stbport);
121
122		if (stbport > 0)
123		{
124			enable_dsl_wan_iptv_if = 1;
125		}
126	}
127
128
129	if (enable_dsl_wan_dsl_if)
130	{
131		// DSL WAN MODE
132		eval("vconfig", "add", "eth2.1", "1");
133		eval("ifconfig", "eth2.1.1", "up");
134		eval("ifconfig", "eth2.1", "hw", "ether", nvram_safe_get("et0macaddr"));
135	}
136
137	if (enable_dsl_wan_eth_if)
138	{
139		// eth2.4 = ethenet WAN
140		eval("vconfig", "add", "eth2", "4");
141		eval("ifconfig", "eth2.4", "up");
142		// ethernet WAN , it needs to use another MAC address
143		eval("ifconfig", "eth2.4", "hw", "ether", nvram_safe_get("et1macaddr"));
144	}
145
146	if (enable_dsl_wan_iptv_if)
147	{
148		// eth2.3 = IPTV
149		eval("vconfig", "add", "eth2", "3");
150		eval("ifconfig", "eth2.3", "up");
151		// bypass , no need to have another MAC address
152	}
153
154	// for a dummy interface from trendchip
155	// this is only for stats viewing , non-necessary
156	// do not power up dummay interface. If so, it will send packet out
157	eval("vconfig", "add", "eth2.1", "50");
158	eval("vconfig", "add", "eth2.1", "51");
159
160}
161
162void config_switch_dsl_set_dsl()
163{
164	// DSL WAN , no IPTV , USB also goes here
165	dbG("wan port = dsl\n");
166	eval("rtkswitch", "8", "0");
167}
168
169void config_switch_dsl_set_iptv(int stbport)
170{
171	char param_buf[32];
172	sprintf(param_buf, "%d", stbport);
173	eval("rtkswitch", "8", param_buf);
174}
175
176void config_switch_dsl_set_lan_iptv(int stbport)
177{
178	// ethernet wan port = high-byte
179	// iptv port = low-byte
180	char param_buf[32];
181	// ethernet WAN , has IPTV
182	if (nvram_match("wans_lanport","1"))
183	{
184		// 0x100
185		dbG("wan port = lan1\n");
186		sprintf(param_buf, "%d", 0x100 + stbport);
187		eval("rtkswitch", "8", param_buf);
188	}
189	else if (nvram_match("wans_lanport","2"))
190	{
191		// 0x200
192		dbG("wan port = lan2\n");
193		sprintf(param_buf, "%d", 0x200 + stbport);
194		eval("rtkswitch", "8", param_buf);
195	}
196	else if (nvram_match("wans_lanport","3"))
197	{
198		// 0x300
199		dbG("wan port = lan3\n");
200		sprintf(param_buf, "%d", 0x300 + stbport);
201		eval("rtkswitch", "8", param_buf);
202	}
203	else
204	{
205		// 0x400
206		dbG("wan port = lan4\n");
207		sprintf(param_buf, "%d", 0x400 + stbport);
208		eval("rtkswitch", "8", param_buf);
209	}
210}
211
212void config_switch_dsl_set_lan()
213{
214	// ethernet wan port = high-byte
215	// iptv port = low-byte
216	// DSLWAN , ethernet WAN , no IPTV
217	if (nvram_match("wans_lanport","1"))
218	{
219		// 0x100
220		dbG("wan port = lan1\n");
221		eval("rtkswitch", "8", "256");
222	}
223	else if (nvram_match("wans_lanport","2"))
224	{
225		// 0x200
226		dbG("wan port = lan2\n");
227		eval("rtkswitch", "8", "512");
228	}
229	else if (nvram_match("wans_lanport","3"))
230	{
231		// 0x300
232		dbG("wan port = lan3\n");
233		eval("rtkswitch", "8", "768");
234	}
235	else
236	{
237		// 0x400
238		dbG("wan port = lan4\n");
239		eval("rtkswitch", "8", "1024");
240	}
241}
242
243void config_switch_dsl()
244{
245	int stbport;
246	dbG("config ethernet switch IC\n");
247
248	if (is_routing_enabled())
249	{
250		stbport = nvram_get_int("switch_stb_x");
251		if (stbport < 0 || stbport > 6) stbport = 0;
252		dbG("STB port: %d\n", stbport);
253
254#ifdef RTCONFIG_DUALWAN
255		if (stbport == 0)
256		{
257			if (get_dualwan_secondary()==WANS_DUALWAN_IF_LAN)
258			{
259				config_switch_dsl_set_lan();
260			}
261			else
262			{
263				if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN)
264				{
265					config_switch_dsl_set_lan();
266				}
267				else
268				{
269					config_switch_dsl_set_dsl();
270				}
271			}
272		}
273		else
274		{
275			if (get_dualwan_secondary()==WANS_DUALWAN_IF_LAN)
276			{
277				config_switch_dsl_set_lan_iptv(stbport);
278			}
279			else
280			{
281				if (get_dualwan_primary()==WANS_DUALWAN_IF_LAN)
282				{
283					config_switch_dsl_set_lan_iptv(stbport);
284				}
285				else
286				{
287					config_switch_dsl_set_iptv(stbport);
288				}
289			}
290		}
291#else
292		if (stbport == 0)
293		{
294			config_switch_dsl_set_dsl();
295		}
296		else
297		{
298			config_switch_dsl_set_iptv(stbport);
299		}
300#endif
301	}
302}
303
304