Support Asus RT-N10U
[tomato.git] / release / src / router / shared / led.c
blobe8a4922b444baaedf17238540c1b61241c1921cc
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <bcmnvram.h>
16 #include "utils.h"
17 #include "shutils.h"
18 #include "shared.h"
21 const char *led_names[] = { "wlan", "diag", "white", "amber", "dmz", "aoss", "bridge", "usb" };
23 #ifdef LINUX26
24 #define GPIO_IOCTL
25 #endif
27 // --- move begin ---
28 #ifdef GPIO_IOCTL
30 #include <sys/ioctl.h>
31 #include <linux_gpio.h>
33 static int _gpio_ioctl(int f, int gpioreg, unsigned int mask, unsigned int val)
35 struct gpio_ioctl gpio;
37 gpio.val = val;
38 gpio.mask = mask;
40 if (ioctl(f, gpioreg, &gpio) < 0) {
41 _dprintf("Invalid gpioreg %d\n", gpioreg);
42 return -1;
44 return (gpio.val);
47 static int _gpio_open()
49 int f = open("/dev/gpio", O_RDWR);
50 if (f < 0)
51 _dprintf ("Failed to open /dev/gpio\n");
52 return f;
55 int gpio_open(uint32_t mask)
57 uint32_t bit;
58 int i;
59 int f = _gpio_open();
61 if ((f >= 0) && mask) {
62 for (i = 0; i <= 15; i++) {
63 bit = 1 << i;
64 if ((mask & bit) == bit) {
65 _gpio_ioctl(f, GPIO_IOC_RESERVE, bit, bit);
66 _gpio_ioctl(f, GPIO_IOC_OUTEN, bit, 0);
69 close(f);
70 f = _gpio_open();
73 return f;
76 void gpio_write(uint32_t bit, int en)
78 int f;
80 if ((f = gpio_open(0)) < 0) return;
82 _gpio_ioctl(f, GPIO_IOC_RESERVE, bit, bit);
83 _gpio_ioctl(f, GPIO_IOC_OUTEN, bit, bit);
84 _gpio_ioctl(f, GPIO_IOC_OUT, bit, en ? bit : 0);
85 close(f);
88 uint32_t _gpio_read(int f)
90 uint32_t r;
91 // r = _gpio_ioctl(f, GPIO_IOC_IN, 0xFFFF, 0);
92 r = _gpio_ioctl(f, GPIO_IOC_IN, 0x07FF, 0);
93 if (r < 0) r = ~0;
94 return r;
97 uint32_t gpio_read(void)
99 int f;
100 uint32_t r;
102 if ((f = gpio_open(0)) < 0) return ~0;
103 r = _gpio_read(f);
104 close(f);
105 return r;
108 #else
110 int gpio_open(uint32_t mask)
112 int f = open(DEV_GPIO(in), O_RDONLY|O_SYNC);
113 if (f < 0)
114 _dprintf ("Failed to open %s\n", DEV_GPIO(in));
115 return f;
118 void gpio_write(uint32_t bit, int en)
120 int f;
121 uint32_t r;
123 if ((f = open(DEV_GPIO(control), O_RDWR)) < 0) return;
124 read(f, &r, sizeof(r));
125 r &= ~bit;
126 write(f, &r, sizeof(r));
127 close(f);
129 if ((f = open(DEV_GPIO(outen), O_RDWR)) < 0) return;
130 read(f, &r, sizeof(r));
131 r |= bit;
132 write(f, &r, sizeof(r));
133 close(f);
135 if ((f = open(DEV_GPIO(out), O_RDWR)) < 0) return;
136 read(f, &r, sizeof(r));
137 if (en) r |= bit;
138 else r &= ~bit;
139 write(f, &r, sizeof(r));
140 close(f);
143 uint32_t _gpio_read(int f)
145 uint32_t v;
146 return (read(f, &v, sizeof(v)) == sizeof(v)) ? v : ~0;
149 uint32_t gpio_read(void)
151 int f;
152 uint32_t r;
154 if ((f = open(DEV_GPIO(in), O_RDONLY)) < 0) return ~0;
155 r = _gpio_read(f);
156 close(f);
157 return r;
160 #endif
162 int nvget_gpio(const char *name, int *gpio, int *inv)
164 char *p;
165 uint32_t n;
167 if (((p = nvram_get(name)) != NULL) && (*p)) {
168 n = strtoul(p, NULL, 0);
169 if ((n & 0xFFFFFF70) == 0) {
170 *gpio = (n & 15);
171 *inv = ((n & 0x80) != 0);
172 return 1;
175 return 0;
177 // --- move end ---
180 int do_led(int which, int mode)
182 // WLAN DIAG WHITE AMBER DMZ AOSS BRIDG MYST/USB
183 // ----- ----- ----- ----- ----- ----- ----- -----
184 static int wrt54g[] = { 255, 1, 2, 3, 7, 255, 255, 255 };
185 static int wrtsl[] = { 255, 1, 5, 7, 0, 255, 255, 255 };
186 static int whrg54[] = { 2, 7, 255, 255, 255, 6, 1, 3 };
187 static int wbr2g54[] = { 255, -1, 255, 255, 255, -6, 255, 255 };
188 static int wzrg54[] = { 2, 7, 255, 255, 255, 6, 255, 255 };
189 static int wr850g1[] = { 7, 3, 255, 255, 255, 255, 255, 255 };
190 static int wr850g2[] = { 0, 1, 255, 255, 255, 255, 255, 255 };
191 static int wtr54gs[] = { 1, -1, 255, 255, 255, 255, 255, 255 };
192 static int dir320[] = { -99, 1, 4, 3, 255, 255, 255, -5 };
193 static int h618b[] = { 255, -1, 255, 255, 255, -5, -3, -4 };
194 static int wl1600gl[] = { 1, -5, 0, 255, 255, 2, 255, 255 };
195 static int wrt310nv1[] = { 255, 1, 9, 3, 255, 255, 255, 255 };
196 static int wrt160nv1[] = { 255, 1, 5, 3, 255, 255, 255, 255 };
197 #ifdef CONFIG_BCMWL5
198 static int wnr3500[] = { 255, 255, 2, 255, 255, -1, 255, 255 };
199 static int wnr2000v2[] = { 255, 255, 255, 255, 255, -7, 255, 255 };
200 static int f7d[] = { 255, 255, 255, 255, 12, 13, 255, 14 };
201 static int wrt160nv3[] = { 255, 1, 4, 2, 255, 255, 255, 255 };
202 static int wrt320n[] = { 255, 2, 3, 4, 255, 255, 255, 255 };
203 static int wrt610nv2[] = { 255, 5, 3, 0, 255, 255, 255, -7 };
204 static int e4200[] = { 255, 5, -3, 255, 255, 255, 255, 255 };
205 static int rtn10u[] = { 255, 255, 255, 255, 255, -7, 255, 8 };
206 static int rtn66u[] = { 255, -12, 255, 255, 255, 255, 255, 15 };
207 #endif
209 char s[16];
210 int n;
211 int b = 255, c = 255;
212 int ret = 255;
214 if ((which < 0) || (which >= LED_COUNT)) return ret;
216 switch (nvram_match("led_override", "1") ? MODEL_UNKNOWN : get_model()) {
217 case MODEL_WRT54G:
218 if (check_hw_type() == HW_BCM4702) {
219 // G v1.x
220 if ((which != LED_DIAG) && (which != LED_DMZ)) return ret;
221 b = (which == LED_DMZ) ? 1 : 4;
222 if (mode != LED_PROBE) {
223 if (f_read_string("/proc/sys/diag", s, sizeof(s)) > 0) {
224 n = atoi(s);
225 sprintf(s, "%u", mode ? (n | b) : (n & ~b));
226 f_write_string("/proc/sys/diag", s, 0, 0);
229 return b;
231 switch (which) {
232 case LED_AMBER:
233 case LED_WHITE:
234 if (!supports(SUP_WHAM_LED)) return ret;
235 break;
237 b = wrt54g[which];
238 break;
239 case MODEL_WTR54GS:
240 b = wtr54gs[which];
241 break;
242 case MODEL_WRTSL54GS:
243 b = wrtsl[which];
244 break;
245 case MODEL_WHRG54S:
246 case MODEL_WHRHPG54:
247 case MODEL_WHRG125:
248 b = whrg54[which];
249 break;
250 case MODEL_WZRG54:
251 case MODEL_WZRHPG54:
252 case MODEL_WZRRSG54:
253 case MODEL_WZRRSG54HP:
254 case MODEL_WVRG54NF:
255 case MODEL_WHR2A54G54:
256 case MODEL_WHR3AG54:
257 case MODEL_WZRG108:
258 b = wzrg54[which];
259 break;
261 case MODEL_WHR2A54G54:
262 if (which != LED_DIAG) return ret;
263 b = 7;
264 break;
266 case MODEL_WBRG54:
267 if (which != LED_DIAG) return ret;
268 b = 7;
269 break;
270 case MODEL_WBR2G54:
271 b = wbr2g54[which];
272 break;
273 case MODEL_WR850GV1:
274 b = wr850g1[which];
275 break;
276 case MODEL_WR850GV2:
277 case MODEL_WR100:
278 b = wr850g2[which];
279 break;
280 case MODEL_WL500GP:
281 if (which != LED_DIAG) return ret;
282 b = -1; // power light
283 break;
284 case MODEL_WL500W:
285 if (which != LED_DIAG) return ret;
286 b = -5; // power light
287 break;
288 case MODEL_DIR320:
289 b = dir320[which];
290 break;
291 case MODEL_H618B:
292 b = h618b[which];
293 break;
294 case MODEL_WL1600GL:
295 b = wl1600gl[which];
296 break;
297 case MODEL_WL500GPv2:
298 case MODEL_WL500GD:
299 case MODEL_WL520GU:
300 if (which != LED_DIAG) return ret;
301 b = -99; // Invert power light as diag indicator
302 break;
303 #ifdef CONFIG_BCMWL5
304 case MODEL_RTN12:
305 if (which != LED_DIAG) return ret;
306 b = -2; // power light
307 break;
308 case MODEL_RTN10:
309 case MODEL_RTN16:
310 if (which != LED_DIAG) return ret;
311 b = -1; // power light
312 break;
313 case MODEL_RTN66U:
314 b = rtn66u[which];
315 break;
316 case MODEL_WNR3500L:
317 if (which == LED_DIAG) {
318 // power led gpio: 0x03 - green, 0x07 - amber
319 b = (mode) ? 7 : 3;
320 c = (mode) ? 3 : 7;
321 } else
322 b = wnr3500[which];
323 break;
324 case MODEL_WNR2000v2:
325 if (which == LED_DIAG) {
326 // power led gpio: 0x01 - green, 0x02 - amber
327 b = (mode) ? 2 : 1;
328 c = (mode) ? 1 : 2;
329 } else
330 b = wnr2000v2[which];
331 break;
332 case MODEL_F7D3301:
333 case MODEL_F7D3302:
334 case MODEL_F7D4301:
335 case MODEL_F7D4302:
336 case MODEL_F5D8235v3:
337 if (which == LED_DIAG) {
338 // power led gpio: 10 - green, 11 - red
339 b = (mode) ? 11 : -10;
340 c = (mode) ? -10 : 11;
341 } else
342 b = f7d[which];
343 break;
344 case MODEL_WRT160Nv3:
345 b = wrt160nv3[which];
346 break;
347 case MODEL_WRT320N:
348 b = wrt320n[which];
349 break;
350 case MODEL_WRT610Nv2:
351 b = wrt610nv2[which];
352 break;
353 case MODEL_E4200:
354 b = e4200[which];
355 break;
356 case MODEL_RTN10U:
357 b = rtn10u[which];
358 break;
359 #endif
361 case MODEL_RT390W:
362 break;
364 case MODEL_MN700:
365 if (which != LED_DIAG) return ret;
366 b = 6;
367 break;
368 case MODEL_WLA2G54L:
369 if (which != LED_DIAG) return ret;
370 b = 1;
371 break;
372 case MODEL_WRT300N:
373 if (which != LED_DIAG) return ret;
374 b = 1;
375 break;
376 case MODEL_WRT310Nv1:
377 b = wrt310nv1[which];
378 break;
379 case MODEL_WRT160Nv1:
380 b = wrt160nv1[which];
381 break;
382 default:
383 sprintf(s, "led_%s", led_names[which]);
384 if (nvget_gpio(s, &b, &n)) {
385 if ((mode != LED_PROBE) && (n)) mode = !mode;
386 ret = (n) ? b : ((b) ? -b : -99);
387 goto SET;
389 return ret;
392 ret = b;
393 if (b < 0) {
394 if (b == -99) b = 0; // -0 substitute
395 else b = -b;
397 else if (mode != LED_PROBE) {
398 mode = !mode;
401 SET:
402 if (b < 16) {
403 if (mode != LED_PROBE) {
404 gpio_write(1 << b, mode);
406 if (c < 0) {
407 if (c == -99) c = 0;
408 else c = -c;
410 else mode = !mode;
411 if (c < 16) gpio_write(1 << c, mode);
415 return ret;