[scripts] ext-toolchain.sh: properly handle toolchains without sysroot support
[openwrt.git] / package / broadcom-diag / src / diag.c
1 /*
2  * diag.c - GPIO interface driver for Broadcom boards
3  *
4  * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
5  * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
6  * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/kmod.h>
26 #include <linux/proc_fs.h>
27 #include <linux/timer.h>
28 #include <linux/version.h>
29 #include <asm/uaccess.h>
30 #include <linux/workqueue.h>
31 #include <linux/skbuff.h>
32 #include <linux/netlink.h>
33 #include <linux/kobject.h>
34 #include <net/sock.h>
35 extern u64 uevent_next_seqnum(void);
36
37 #include "gpio.h"
38 #include "diag.h"
39 #define getvar(str) (nvram_get(str)?:"")
40
41 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
42 static int fill_event(struct event_t *);
43 static unsigned int gpiomask = 0;
44 module_param(gpiomask, int, 0644);
45
46 enum {
47         /* Linksys */
48         WAP54GV1,
49         WAP54GV2,
50         WAP54GV3,
51         WRT54GV1,
52         WRT54G,
53         WRTSL54GS,
54         WRT54G3G,
55         WRT54G3GV2_VF,
56         WRT160N,
57         WRT300NV11,
58         WRT350N,
59         WRT600N,
60         WRT600NV11,
61         WRT610N,
62         WRT610NV2,
63         E3000V1,
64
65         /* ASUS */
66         WLHDD,
67         WL300G,
68         WL320GE,
69         WL330GE,
70         WL500G,
71         WL500GD,
72         WL500GP,
73         WL500GPV2,
74         WL500W,
75         WL520GC,
76         WL520GU,
77         ASUS_4702,
78         WL700GE,
79         RTN16,
80
81         /* Buffalo */
82         WBR2_G54,
83         WHR_G54S,
84         WHR_HP_G54,
85         WHR_G125,
86         WHR2_A54G54,
87         WLA2_G54L,
88         WZR_G300N,
89         WZR_RS_G54,
90         WZR_RS_G54HP,
91         BUFFALO_UNKNOWN,
92         BUFFALO_UNKNOWN_4710,
93
94         /* Siemens */
95         SE505V1,
96         SE505V2,
97
98         /* US Robotics */
99         USR5461,
100
101         /* Dell */
102         TM2300,
103         TM2300V2,
104
105         /* Motorola */
106         WE800G,
107         WR850GV1,
108         WR850GV2V3,
109         WR850GP,
110
111         /* Belkin */
112         BELKIN_UNKNOWN,
113         BELKIN_F7D4301,
114
115         /* Netgear */
116         WGT634U,
117         WNR834BV1,
118         WNR834BV2,
119
120         /* Trendware */
121         TEW411BRPP,
122
123         /* SimpleTech */
124         STI_NAS,
125
126         /* D-Link */
127         DIR130,
128         DIR320,
129         DIR330,
130         DWL3150,
131
132         /* Sitecom */
133         WL105B,
134
135         /* Western Digital */
136         WDNetCenter,
137
138         /* Askey */
139         RT210W,
140
141         /* OvisLink */
142         WL1600GL,
143
144         /* Microsoft */
145         MN700,
146
147         /* Edimax */
148         PS1208MFG,
149 };
150
151 static void __init bcm4780_init(void) {
152                 int pin = 1 << 3;
153
154                 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
155                 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
156                 gpio_outen(pin, pin);
157                 gpio_control(pin, 0);
158                 gpio_out(pin, pin);
159
160                 /* Wait 5s, so the HDD can spin up */
161                 set_current_state(TASK_INTERRUPTIBLE);
162                 schedule_timeout(HZ * 5);
163 }
164
165 static void __init NetCenter_init(void) {
166                 /* unset pin 6 (+12V) */
167                 int pin = 1 << 6;
168                 gpio_outen(pin, pin);
169                 gpio_control(pin, 0);
170                 gpio_out(pin, pin);
171                 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
172                 pin = 1 << 1;
173                 gpio_outen(pin, pin);
174                 gpio_control(pin, 0);
175                 gpio_out(pin, pin);
176                 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
177                 bcm4780_init();
178 }
179
180 static void __init bcm57xx_init(void) {
181         int pin = 1 << 2;
182
183         /* FIXME: switch comes up, but port mappings/vlans not right */
184         gpio_outen(pin, pin);
185         gpio_control(pin, 0);
186         gpio_out(pin, pin);
187 }
188
189 static struct platform_t __initdata platforms[] = {
190         /* Linksys */
191         [WAP54GV1] = {
192                 .name           = "Linksys WAP54G V1",
193                 .buttons        = {
194                         { .name = "reset",      .gpio = 1 << 0 },
195                 },
196                 .leds           = {
197                         { .name = "diag",       .gpio = 1 << 3 },
198                         { .name = "wlan",       .gpio = 1 << 4 },
199                 },
200         },
201         [WAP54GV2] = {
202                 .name           = "Linksys WAP54G V2",
203                 .buttons        = {
204                         { .name = "reset",      .gpio = 1 << 0 },
205                 },
206                 .leds           = {
207                         { .name = "wlan",       .gpio = 1 << 5, .polarity = REVERSE },
208                         /* GPIO 6 is b44 (eth0, LAN) PHY power */
209                 },
210         },
211         [WAP54GV3] = {
212                 .name           = "Linksys WAP54G V3",
213                 .buttons        = {
214                         /* FIXME: verify this */
215                         { .name = "reset",      .gpio = 1 << 7 },
216                         { .name = "ses",        .gpio = 1 << 0 },
217                 },
218                 .leds           = {
219                         /* FIXME: diag? */
220                         { .name = "ses",        .gpio = 1 << 1 },
221                 },
222         },
223         [WRT54GV1] = {
224                 .name           = "Linksys WRT54G V1.x",
225                 .buttons        = {
226                         { .name = "reset",      .gpio = 1 << 6 },
227                 },
228                 .leds           = {
229                         { .name = "diag",       .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
230                         { .name = "dmz",        .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
231                 },
232         },
233         [WRT54G] = {
234                 .name           = "Linksys WRT54G/GS/GL",
235                 .buttons        = {
236                         { .name = "reset",      .gpio = 1 << 6 },
237                         { .name = "ses",        .gpio = 1 << 4 },
238                 },
239                 .leds           = {
240                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
241                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
242                         { .name = "ses_white",  .gpio = 1 << 2, .polarity = REVERSE },
243                         { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
244                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
245                 },
246         },
247         [WRTSL54GS] = {
248                 .name           = "Linksys WRTSL54GS",
249                 .buttons        = {
250                         { .name = "reset",      .gpio = 1 << 6 },
251                         { .name = "ses",        .gpio = 1 << 4 },
252                 },
253                 .leds           = {
254                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
255                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },
256                         { .name = "ses_white",  .gpio = 1 << 5, .polarity = REVERSE },
257                         { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
258                 },
259         },
260         [WRT54G3G] = {
261                 .name           = "Linksys WRT54G3G",
262                 .buttons        = {
263                         { .name = "reset",      .gpio = 1 << 6 },
264                         { .name = "3g",         .gpio = 1 << 4 },
265                 },
266                 .leds           = {
267                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
268                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
269                         { .name = "3g_green",   .gpio = 1 << 2, .polarity = NORMAL },
270                         { .name = "3g_blue",    .gpio = 1 << 3, .polarity = NORMAL },
271                         { .name = "3g_blink",   .gpio = 1 << 5, .polarity = NORMAL },
272                 },
273         },
274         [WRT54G3GV2_VF] = {
275                 .name           = "Linksys WRT54G3GV2-VF",
276                 .buttons        = {
277                         { .name = "reset",      .gpio = 1 << 6 },
278                         { .name = "3g",         .gpio = 1 << 5 },
279                 },
280                 .leds           = {
281                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
282                         { .name = "3g_green",   .gpio = 1 << 2, .polarity = NORMAL },
283                         { .name = "3g_blue",    .gpio = 1 << 3, .polarity = NORMAL },
284                 },
285         },
286         [WRT160N] = {
287                 .name           = "Linksys WRT160N",
288                 .buttons        = {
289                         { .name = "reset",      .gpio = 1 << 6 },
290                         { .name = "ses",        .gpio = 1 << 4 },
291                 },
292                 .leds           = {
293                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
294                         { .name = "ses_blue",   .gpio = 1 << 5, .polarity = REVERSE },
295                         { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
296                 },
297         },
298         [WRT300NV11] = {
299                 .name           = "Linksys WRT300N V1.1",
300                 .buttons        = {
301                         { .name = "reset",     .gpio = 1 << 6 }, // "Reset" on back panel
302                         { .name = "ses",       .gpio = 1 << 4 }, // "Reserved" on top panel
303                 },
304                 .leds           = {
305                         { .name = "power",     .gpio = 1 << 1, .polarity = NORMAL  }, // "Power"
306                         { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
307                         { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
308                 },
309                 .platform_init = bcm57xx_init,
310         },
311         [WRT350N] = {
312                 .name           = "Linksys WRT350N",
313                 .buttons        = {
314                         { .name = "reset",      .gpio = 1 << 6 },
315                         { .name = "ses",        .gpio = 1 << 8 },
316                 },
317                 .leds           = {
318                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
319                         { .name = "ses_amber",  .gpio = 1 << 3, .polarity = REVERSE },
320                         { .name = "ses_green",  .gpio = 1 << 9, .polarity = REVERSE },
321                         { .name = "usb_blink",  .gpio = 1 << 10, .polarity = REVERSE },
322                         { .name = "usb",        .gpio = 1 << 11, .polarity = REVERSE },
323                 },
324                 .platform_init = bcm57xx_init,
325         },
326         [WRT600N] = {
327                 .name           = "Linksys WRT600N",
328                 .buttons        = {
329                         { .name = "reset",      .gpio = 1 << 6 },
330                         { .name = "ses",        .gpio = 1 << 7 },
331                 },
332                 .leds           = {
333                         { .name = "power",              .gpio = 1 << 2,  .polarity = REVERSE }, // Power LED
334                         { .name = "usb",                .gpio = 1 << 3,  .polarity = REVERSE }, // USB LED
335                         { .name = "wl0_ses_amber",      .gpio = 1 << 8,  .polarity = REVERSE }, // 2.4Ghz LED Amber
336                         { .name = "wl0_ses_green",      .gpio = 1 << 9,  .polarity = REVERSE }, // 2.4Ghz LED Green
337                         { .name = "wl1_ses_amber",      .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
338                         { .name = "wl1_ses_green",      .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
339                 },
340                 .platform_init = bcm57xx_init,
341         },
342         [WRT600NV11] = {
343                 .name           = "Linksys WRT600N V1.1",
344                 .buttons        = {
345                         { .name = "reset",      .gpio = 1 << 6 },
346                         { .name = "ses",        .gpio = 1 << 7 },
347                 },
348                 .leds           = {
349                         { .name = "power",             .gpio = 1 << 2,  .polarity = REVERSE }, // Power LED
350                         { .name = "usb",                .gpio = 1 << 3,  .polarity = REVERSE }, // USB LED
351                         { .name = "wl0_ses_amber",      .gpio = 1 << 8,  .polarity = REVERSE }, // 2.4Ghz LED Amber
352                         { .name = "wl0_ses_green",     .gpio = 1 << 9,  .polarity = REVERSE }, // 2.4Ghz LED Green
353                         { .name = "wl1_ses_amber",      .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
354                         { .name = "wl1_ses_green",      .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
355                 },
356                 .platform_init = bcm57xx_init,
357         },
358         [WRT610N] = {
359                 .name           = "Linksys WRT610N",
360                 .buttons        = {
361                         { .name = "reset",      .gpio = 1 << 6 },
362                         { .name = "ses",        .gpio = 1 << 8 },
363                 },
364                 .leds           = {
365                         { .name = "power",      .gpio = 1 << 1,  .polarity = NORMAL }, // Power LED
366                         { .name = "usb",        .gpio = 1 << 0,  .polarity = REVERSE }, // USB LED
367                         { .name = "ses_amber",  .gpio = 1 << 3,  .polarity = REVERSE }, // WiFi protected setup LED amber
368                         { .name = "ses_blue",   .gpio = 1 << 9,  .polarity = REVERSE }, // WiFi protected setup LED blue
369                 },
370         },
371         [WRT610NV2] = {
372                 .name           = "Linksys WRT610N V2",
373                 .buttons        = {
374                         { .name = "reset",      .gpio = 1 << 6 },
375                         { .name = "ses",        .gpio = 1 << 4 },
376                 },
377                 .leds           = {
378                         { .name = "power",      .gpio = 1 << 5, .polarity = NORMAL },   // Power LED
379                         { .name = "usb",        .gpio = 1 << 7, .polarity = NORMAL },   // USB LED
380                         { .name = "ses_amber",  .gpio = 1 << 0, .polarity = REVERSE },  // WiFi protected setup LED amber
381                         { .name = "ses_blue",   .gpio = 1 << 3, .polarity = REVERSE },  // WiFi protected setup LED blue
382                         { .name = "wlan",       .gpio = 1 << 1, .polarity = NORMAL },   // Wireless LED
383                 },
384         },
385         [E3000V1] = {
386                 .name           = "Linksys E3000 V1",
387                 .buttons        = {
388                         { .name = "reset",      .gpio = 1 << 6 },
389                         { .name = "ses",        .gpio = 1 << 4 },
390                 },
391                 .leds           = {
392                         { .name = "power",      .gpio = 1 << 5, .polarity = NORMAL },   // Power LED
393                         { .name = "usb",        .gpio = 1 << 7, .polarity = NORMAL },   // USB LED
394                         { .name = "ses_amber",  .gpio = 1 << 0, .polarity = REVERSE },  // WiFi protected setup LED amber
395                         { .name = "ses_blue",   .gpio = 1 << 3, .polarity = REVERSE },  // WiFi protected setup LED blue
396                         { .name = "wlan",       .gpio = 1 << 1, .polarity = NORMAL },   // Wireless LED
397                 },
398         },
399         /* Asus */
400         [WLHDD] = {
401                 .name           = "ASUS WL-HDD",
402                 .buttons        = {
403                         { .name = "reset",      .gpio = 1 << 6 },
404                 },
405                 .leds           = {
406                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
407                         { .name = "usb",        .gpio = 1 << 2, .polarity = REVERSE },
408                 },
409         },
410         [WL300G] = {
411                 .name           = "ASUS WL-300g",
412                 .buttons        = {
413                         { .name = "reset",      .gpio = 1 << 6 },
414                 },
415                 .leds           = {
416                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
417                 },
418         },
419         [WL320GE] = {
420                 .name           = "ASUS WL-320gE/WL-320gP",
421                 .buttons        = {
422                         { .name = "reset",      .gpio = 1 << 6 },
423                 },
424                 .leds           = {
425                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
426                         { .name = "power",      .gpio = 1 << 2, .polarity = REVERSE },
427                         { .name = "link",       .gpio = 1 << 11, .polarity = REVERSE },
428                 },
429         },
430         [WL330GE] = {
431                 .name           = "ASUS WL-330gE",
432                 .buttons        = {
433                         { .name = "reset",      .gpio = 1 << 2 },
434                 },
435                 .leds           = {
436                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
437                 },
438         },
439         [WL500G] = {
440                 .name           = "ASUS WL-500g",
441                 .buttons        = {
442                         { .name = "reset",      .gpio = 1 << 6 },
443                 },
444                 .leds           = {
445                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
446                 },
447         },
448         [WL500GD] = {
449                 .name           = "ASUS WL-500g Deluxe",
450                 .buttons        = {
451                         { .name = "reset",      .gpio = 1 << 6 },
452                 },
453                 .leds           = {
454                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
455                 },
456         },
457         [WL500GP] = {
458                 .name           = "ASUS WL-500g Premium",
459                 .buttons        = {
460                         { .name = "reset",      .gpio = 1 << 0 },
461                         { .name = "ses",        .gpio = 1 << 4 },
462                 },
463                 .leds           = {
464                         { .name = "power",      .gpio = 1 << 1, .polarity = REVERSE },
465                 },
466         },
467         [WL500GPV2] = {
468                 .name           = "ASUS WL-500g Premium V2",
469                 .buttons        = {
470                         { .name = "reset",      .gpio = 1 << 2 },
471                         { .name = "ses",        .gpio = 1 << 3 },
472                 },
473                 .leds           = {
474                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
475                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
476                 },
477         },
478         [WL500W] = {
479                 .name           = "ASUS WL-500W",
480                 .buttons        = {
481                         { .name = "reset",      .gpio = 1 << 6 },
482                         { .name = "ses",        .gpio = 1 << 7 },
483                 },
484                 .leds           = {
485                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
486                 },
487         },
488         [WL520GC] = {
489                 .name           = "ASUS WL-520GC",
490                 .buttons        = {
491                         { .name = "reset",      .gpio = 1 << 2 },
492                         { .name = "ses",        .gpio = 1 << 3 },
493                 },
494                 .leds           = {
495                 { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
496                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
497                 },
498         },
499         [WL520GU] = {
500                 .name           = "ASUS WL-520gU",
501                 .buttons        = {
502                         { .name = "reset",      .gpio = 1 << 2 },
503                         { .name = "ses",        .gpio = 1 << 3 },
504                 },
505                 .leds           = {
506                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
507                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
508                 },
509         },
510         [ASUS_4702] = {
511                 .name           = "ASUS (unknown, BCM4702)",
512                 .buttons        = {
513                         { .name = "reset",      .gpio = 1 << 6 },
514                 },
515                 .leds           = {
516                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
517                 },
518         },
519         [WL700GE] = {
520                 .name           = "ASUS WL-700gE",
521                 .buttons        = {
522                         { .name = "reset",      .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
523                         { .name = "ses",        .gpio = 1 << 4 }, // on back, actual name ezsetup
524                         { .name = "power",      .gpio = 1 << 0 }, // on front
525                         { .name = "copy",       .gpio = 1 << 6 }, // on front
526                 },
527                 .leds           = {
528 #if 0
529                         // GPIO that controls power led also enables/disables some essential functions
530                         // - power to HDD
531                         // - switch leds
532                         { .name = "power",      .gpio = 1 << 3, .polarity = NORMAL },  // actual name power
533 #endif
534                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
535                 },
536                 .platform_init = bcm4780_init,
537         },
538         [RTN16] = {
539                 .name           = "ASUS RT-N16",
540                 .buttons        = {
541                         { .name = "reset",      .gpio = 1 << 8 },
542                         { .name = "ses",        .gpio = 1 << 5 },
543                 },
544                 .leds           = {
545                         { .name = "power",      .gpio = 1 << 1, .polarity = REVERSE },
546                         { .name = "wlan",       .gpio = 1 << 7, .polarity = NORMAL },
547                 },
548         },
549         /* Buffalo */
550         [WHR_G54S] = {
551                 .name           = "Buffalo WHR-G54S",
552                 .buttons        = {
553                         { .name = "reset",      .gpio = 1 << 4 },
554                         { .name = "bridge",     .gpio = 1 << 5 },
555                         { .name = "ses",        .gpio = 1 << 0 },
556                 },
557                 .leds           = {
558                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
559                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
560                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
561                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
562                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
563                 },
564         },
565         [WBR2_G54] = {
566                 .name           = "Buffalo WBR2-G54",
567                 /* FIXME: verify */
568                 .buttons        = {
569                         { .name = "reset",      .gpio = 1 << 7 },
570                 },
571                 .leds           = {
572                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
573                 },
574         },
575         [WHR_HP_G54] = {
576                 .name           = "Buffalo WHR-HP-G54",
577                 .buttons        = {
578                         { .name = "reset",      .gpio = 1 << 4 },
579                         { .name = "bridge",     .gpio = 1 << 5 },
580                         { .name = "ses",        .gpio = 1 << 0 },
581                 },
582                 .leds           = {
583                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
584                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
585                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
586                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
587                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
588                 },
589         },
590         [WHR_G125] = {
591                 .name           = "Buffalo WHR-G125",
592                 .buttons        = {
593                         { .name = "reset",      .gpio = 1 << 4 },
594                         { .name = "bridge",     .gpio = 1 << 5 },
595                         { .name = "ses",        .gpio = 1 << 0 },
596                 },
597                 .leds           = {
598                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
599                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
600                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
601                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
602                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
603                 },
604         },
605         [WHR2_A54G54] = {
606                 .name           = "Buffalo WHR2-A54G54",
607                 .buttons        = {
608                         { .name = "reset",      .gpio = 1 << 4 },
609                 },
610                 .leds           = {
611                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
612                 },
613         },
614         [WLA2_G54L] = {
615                 .name           = "Buffalo WLA2-G54L",
616                 /* FIXME: verify */
617                 .buttons        = {
618                         { .name = "reset",      .gpio = 1 << 7 },
619                 },
620                 .leds           = {
621                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
622                 },
623         },
624         [WZR_G300N] = {
625                 .name           = "Buffalo WZR-G300N",
626                 .buttons        = {
627                         { .name = "reset",      .gpio = 1 << 4 },
628                 },
629                 .leds           = {
630                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
631                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
632                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
633                 },
634         },
635         [WZR_RS_G54] = {
636                 .name           = "Buffalo WZR-RS-G54",
637                 .buttons        = {
638                         { .name = "ses",        .gpio = 1 << 0 },
639                         { .name = "reset",      .gpio = 1 << 4 },
640                 },
641                 .leds           = {
642                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
643                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
644                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
645                 },
646         },
647         [WZR_RS_G54HP] = {
648                 .name           = "Buffalo WZR-RS-G54HP",
649                 .buttons        = {
650                         { .name = "ses",        .gpio = 1 << 0 },
651                         { .name = "reset",      .gpio = 1 << 4 },
652                 },
653                 .leds           = {
654                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
655                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
656                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
657                 },
658         },
659         [BUFFALO_UNKNOWN] = {
660                 .name           = "Buffalo (unknown)",
661                 .buttons        = {
662                         { .name = "reset",      .gpio = 1 << 7 },
663                 },
664                 .leds           = {
665                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
666                 },
667         },
668         [BUFFALO_UNKNOWN_4710] = {
669                 .name           = "Buffalo (unknown, BCM4710)",
670                 .buttons        = {
671                         { .name = "reset",      .gpio = 1 << 4 },
672                 },
673                 .leds           = {
674                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
675                 },
676         },
677         /* Siemens */
678         [SE505V1] = {
679                 .name           = "Siemens SE505 V1",
680                 .buttons        = {
681                         /* No usable buttons */
682                 },
683                 .leds           = {
684 //                      { .name = "power",      .gpio = 1 << 0  .polarity = REVERSE },  // Usable when retrofitting D26 (?)
685                         { .name = "dmz",        .gpio = 1 << 4, .polarity = REVERSE },  // actual name WWW
686                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
687                 },
688         },
689         [SE505V2] = {
690                 .name           = "Siemens SE505 V2",
691                 .buttons        = {
692                         /* No usable buttons */
693                 },
694                 .leds           = {
695                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
696                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },  // actual name WWW
697                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
698                 },
699         },
700         /* US Robotics */
701         [USR5461] = {
702                 .name           = "U.S. Robotics USR5461",
703                 .buttons        = {
704                         /* No usable buttons */
705                 },
706                 .leds           = {
707                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
708                         { .name = "printer",    .gpio = 1 << 1, .polarity = REVERSE },
709                 },
710         },
711         /* Dell */
712         [TM2300] = {
713                 .name           = "Dell TrueMobile 2300",
714                 .buttons        = {
715                         { .name = "reset",      .gpio = 1 << 0 },
716                 },
717                 .leds           = {
718                         { .name = "wlan",       .gpio = 1 << 6, .polarity = REVERSE },
719                         { .name = "power",      .gpio = 1 << 7, .polarity = REVERSE },
720                 },
721         },
722         [TM2300V2] = {
723                 .name           = "Dell TrueMobile 2300 v2",
724                 .buttons        = {
725                         { .name = "reset",      .gpio = 1 << 0 },
726                 },
727                 .leds           = {
728                         { .name = "wlan",       .gpio = 1 << 6, .polarity = REVERSE },
729                         { .name = "power",      .gpio = 1 << 7, .polarity = REVERSE },
730                 },
731         },
732         /* Motorola */
733         [WE800G] = {
734                 .name           = "Motorola WE800G",
735                 .buttons        = {
736                         { .name = "reset",      .gpio = 1 << 0 },
737                 },
738                 .leds           = {
739                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
740                         { .name = "diag",       .gpio = 1 << 2, .polarity = REVERSE },
741                         { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
742                 },
743         },
744         [WR850GV1] = {
745                 .name           = "Motorola WR850G V1",
746                 .buttons        = {
747                         { .name = "reset",      .gpio = 1 << 0 },
748                 },
749                 .leds           = {
750                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
751                         { .name = "diag",       .gpio = 1 << 3, .polarity = REVERSE },
752                         { .name = "dmz",        .gpio = 1 << 6, .polarity = NORMAL },
753                         { .name = "wlan_red",   .gpio = 1 << 5, .polarity = REVERSE },
754                         { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
755                 },
756         },
757         [WR850GV2V3] = {
758                 .name           = "Motorola WR850G V2/V3",
759                 .buttons        = {
760                         { .name = "reset",      .gpio = 1 << 5 },
761                 },
762                 .leds           = {
763                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
764                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
765                         { .name = "wan",        .gpio = 1 << 6, .polarity = INPUT },
766                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
767                 },
768         },
769         [WR850GP] = {
770                 .name           = "Motorola WR850GP",
771                 .buttons        = {
772                         { .name = "reset",      .gpio = 1 << 5 },
773                 },
774                 .leds           = {
775                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
776                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
777                         { .name = "dmz",        .gpio = 1 << 6, .polarity = REVERSE },
778                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
779                 },
780         },
781
782         /* Belkin */
783         [BELKIN_UNKNOWN] = {
784                 .name           = "Belkin (unknown)",
785                 /* FIXME: verify & add detection */
786                 .buttons        = {
787                         { .name = "reset",      .gpio = 1 << 7 },
788                 },
789                 .leds           = {
790                         { .name = "power",      .gpio = 1 << 5, .polarity = NORMAL },
791                         { .name = "wlan",       .gpio = 1 << 3, .polarity = NORMAL },
792                         { .name = "connected",  .gpio = 1 << 0, .polarity = NORMAL },
793                 },
794         },
795         [BELKIN_F7D4301] = {
796                 .name           = "Belkin PlayMax F7D4301",
797                 .buttons        = {
798                         { .name = "reset",      .gpio = 1 << 6 },
799                         { .name = "wps",        .gpio = 1 << 8 },
800                 },
801                 .leds           = {
802                         { .name = "power",      .gpio = 1 << 11, .polarity = REVERSE },
803                         { .name = "wlan",       .gpio = 1 << 13, .polarity = REVERSE },
804                         { .name = "led0",       .gpio = 1 << 14, .polarity = REVERSE },
805                         { .name = "led1",       .gpio = 1 << 15, .polarity = REVERSE },
806                 },
807         },
808         /* Netgear */
809         [WGT634U] = {
810                 .name           = "Netgear WGT634U",
811                 .buttons        = {
812                         { .name = "reset",      .gpio = 1 << 2 },
813                 },
814                 .leds           = {
815                         { .name = "power",      .gpio = 1 << 3, .polarity = NORMAL },
816                 },
817         },
818         /* Netgear */
819         [WNR834BV1] = {
820                 .name           = "Netgear WNR834B V1",
821                 .buttons        = { /* TODO: add reset button and confirm LEDs - GPIO from dd-wrt */ },
822                 .leds           = {
823                         { .name = "power",      .gpio = 1 << 4, .polarity = REVERSE },
824                         { .name = "diag",       .gpio = 1 << 5, .polarity = REVERSE },
825                         { .name = "wlan",       .gpio = 1 << 6, .polarity = REVERSE },
826                 },
827         },
828         /* Netgear */
829         [WNR834BV2] = {
830                 .name           = "Netgear WNR834B V2",
831                 .buttons        = {
832                         { .name = "reset",      .gpio = 1 << 6 },
833                 },
834                 .leds           = {
835                         { .name = "power",      .gpio = 1 << 2, .polarity = NORMAL },
836                         { .name = "diag",       .gpio = 1 << 3, .polarity = NORMAL },
837                         { .name = "connected",  .gpio = 1 << 7, .polarity = NORMAL },
838                 },
839         },
840         /* Trendware */
841         [TEW411BRPP] = {
842                 .name           = "Trendware TEW411BRP+",
843                 .buttons        = {
844                         { /* No usable buttons */ },
845                 },
846                 .leds           = {
847                         { .name = "power",      .gpio = 1 << 7, .polarity = NORMAL },
848                         { .name = "wlan",       .gpio = 1 << 1, .polarity = NORMAL },
849                         { .name = "bridge",     .gpio = 1 << 6, .polarity = NORMAL },
850                 },
851         },
852         /* SimpleTech */
853         [STI_NAS] = {
854                 .name      = "SimpleTech SimpleShare NAS",
855                 .buttons        = {
856                         { .name = "reset",      .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
857                 },
858                 .leds      = {
859                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
860                 },
861                 .platform_init = bcm4780_init,
862         },
863         /* D-Link */
864         [DIR130] = {
865                 .name     = "D-Link DIR-130",
866                 .buttons        = {
867                         { .name = "reset",      .gpio = 1 << 3},
868                         { .name = "reserved",   .gpio = 1 << 7},
869                 },
870                 .leds      = {
871                         { .name = "diag",       .gpio = 1 << 0},
872                         { .name = "blue",       .gpio = 1 << 6},
873                 },
874         },
875         [DIR320] = {
876                 .name     = "D-Link DIR-320",
877                 .buttons        = {
878                         { .name = "reserved",   .gpio = 1 << 6},
879                         { .name = "reset",      .gpio = 1 << 7},
880                 },
881                 .leds      = {
882                         { .name = "wlan",       .gpio = 1 << 0, .polarity = NORMAL },
883                         { .name = "diag",       .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
884                         { .name = "red",        .gpio = 1 << 3, .polarity = REVERSE },
885                         { .name = "blue",       .gpio = 1 << 4, .polarity = REVERSE },
886                         { .name = "usb",        .gpio = 1 << 5, .polarity = NORMAL },
887                 },
888         },
889         [DIR330] = {
890                 .name     = "D-Link DIR-330",
891                 .buttons        = {
892                         { .name = "reset",      .gpio = 1 << 3},
893                         { .name = "reserved",   .gpio = 1 << 7},
894                 },
895                 .leds      = {
896                         { .name = "diag",       .gpio = 1 << 0},
897                         { .name = "usb",        .gpio = 1 << 4},
898                         { .name = "blue",       .gpio = 1 << 6},
899                 },
900         },
901         [DWL3150] = {
902                 .name   = "D-Link DWL-3150",
903                 .buttons        = {
904                         { .name = "reset",      .gpio = 1 << 7},
905                 },
906                 .leds     = {
907                         { .name = "diag",       .gpio = 1 << 2},
908                         { .name = "status",     .gpio = 1 << 1},
909                 },
910         },
911         /* Double check */
912         [WL105B] = {
913                 .name   = "Sitecom WL-105b",
914                 .buttons        = {
915                         { .name = "reset",      .gpio = 1 << 10},
916                 },
917                 .leds     = {
918                         { .name = "wlan",       .gpio = 1 << 4},
919                         { .name = "power",      .gpio = 1 << 3},
920                 },
921         },
922         /* Western Digital Net Center */
923         [WDNetCenter] = {
924                 .name   = "Western Digital NetCenter",
925                 .buttons        = {
926                         { .name = "power",      .gpio = 1 << 0},
927                         { .name = "reset",      .gpio = 1 << 7},
928                 },
929                 .platform_init = NetCenter_init,
930         },
931         /* Askey (and clones) */
932         [RT210W] = {
933                 .name           = "Askey RT210W",
934                 .buttons        = {
935                         /* Power button is hard-wired to hardware reset */
936                         /* but is also connected to GPIO 7 (probably for bootloader recovery)  */
937                         { .name = "power",      .gpio = 1 << 7},
938                 },
939                 .leds           = {
940                         /* These were verified and named based on Belkin F5D4230-4 v1112 */
941                         { .name = "connected",  .gpio = 1 << 0, .polarity = REVERSE },
942                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
943                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
944                 },
945         },
946         [WL1600GL] = {
947                 .name           = "OvisLink WL-1600GL",
948                 .buttons        = {
949                         { .name = "reset",      .gpio = 1 << 3 },
950                         { .name = "ses",        .gpio = 1 << 4 },
951                 },
952                 .leds           = {
953                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
954                         { .name = "wps",        .gpio = 1 << 2, .polarity = REVERSE },
955                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
956                         { .name = "connected",  .gpio = 1 << 0, .polarity = REVERSE },
957                 },
958         },
959         /* Microsoft */
960         [MN700] = {
961                 .name   = "Microsoft MN-700",
962                 .buttons        = {
963                         { .name = "reset",      .gpio = 1 << 7 },
964                 },
965                 .leds     = {
966                         { .name = "power",      .gpio = 1 << 6, .polarity = NORMAL },
967                 },
968         },
969         /* Edimax */
970         [PS1208MFG] = {
971                 .name   = "Edimax PS-1208MFG",
972                 .buttons        = {
973                         { .name = "reset",      .gpio = 1 << 4 },
974                 },
975                 .leds     = {
976                         { .name = "status",     .gpio = 1 << 1, .polarity = NORMAL },
977                         { .name = "wlan",       .gpio = 1 << 0, .polarity = NORMAL },
978                 },
979         },
980 };
981
982 static struct platform_t __init *platform_detect(void)
983 {
984         char *boardnum, *boardtype, *buf;
985
986         if (strcmp(getvar("nvram_type"), "cfe") == 0)
987                 return &platforms[WGT634U];
988
989         /* Look for a model identifier */
990
991         /* Based on "model_name" */
992         if ((buf = nvram_get("model_name"))) {
993                 if (!strcmp(buf, "DIR-130"))
994                         return &platforms[DIR130];
995                 if (!strcmp(buf, "DIR-330"))
996                         return &platforms[DIR330];
997         }
998
999         /* Based on "wsc_modelname */
1000         if ((buf = nvram_get("wsc_modelname"))) {
1001                 if (!strcmp(buf, "WRT610N"))
1002                         return &platforms[WRT610N];
1003         }
1004
1005         /* Based on "model_no" */
1006         if ((buf = nvram_get("model_no"))) {
1007                 if (startswith(buf,"WL700")) /* WL700* */
1008                         return &platforms[WL700GE];
1009         }
1010
1011         /* Based on "hardware_version" */
1012         if ((buf = nvram_get("hardware_version"))) {
1013                 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
1014                         return &platforms[WL500GPV2];
1015                 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
1016                         return &platforms[WL520GC];
1017                 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
1018                         return &platforms[WL520GU];
1019                 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
1020                         return &platforms[WL330GE];
1021                 if (startswith(buf,"RT-N16-")) /* RT-N16-* */
1022                         return &platforms[RTN16];
1023                 if (startswith(buf,"F7D4301")) /* F7D4301* */
1024                         return &platforms[BELKIN_F7D4301];
1025         }
1026
1027         /* Based on "ModelId" */
1028         if ((buf = nvram_get("ModelId"))) {
1029                 if (!strcmp(buf, "WR850GP"))
1030                         return &platforms[WR850GP];
1031                 if (!strcmp(buf, "WR850G"))
1032                         return &platforms[WR850GV2V3];
1033                 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
1034                         return &platforms[TM2300]; /* Dell TrueMobile 2300 */
1035                 if (startswith(buf,"WE800G")) /* WE800G* */
1036                         return &platforms[WE800G];
1037         }
1038
1039         /* Buffalo */
1040         if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
1041                 /* Buffalo hardware, check id for specific hardware matches */
1042                 if (!strcmp(buf, "29bb0332"))
1043                         return &platforms[WBR2_G54];
1044                 if (!strcmp(buf, "29129"))
1045                         return &platforms[WLA2_G54L];
1046                 if (!strcmp(buf, "30189"))
1047                         return &platforms[WHR_HP_G54];
1048                 if (!strcmp(buf, "32093"))
1049                         return &platforms[WHR_G125];
1050                 if (!strcmp(buf, "30182"))
1051                         return &platforms[WHR_G54S];
1052                 if (!strcmp(buf, "290441dd"))
1053                         return &platforms[WHR2_A54G54];
1054                 if (!strcmp(buf, "31120"))
1055                         return &platforms[WZR_G300N];
1056                 if (!strcmp(buf, "30083"))
1057                         return &platforms[WZR_RS_G54];
1058                 if (!strcmp(buf, "30103"))
1059                         return &platforms[WZR_RS_G54HP];
1060         }
1061
1062         /* no easy model number, attempt to guess */
1063         boardnum = getvar("boardnum");
1064         boardtype = getvar("boardtype");
1065
1066         if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N  v1/V1.1 */
1067                 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
1068                         return &platforms[WRT600NV11];
1069
1070         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
1071                         return &platforms[WRT600N];
1072         }
1073
1074         /*
1075          * Normally, these would go inside the "CFE based - newer hardware" block below; however, during early init, the
1076          * "pmon_ver" variable is not available on the E3000v1 (and probably the WRT610Nv2 also).  Until this is figured out,
1077          * these will need to remain here in order for platform detection to work.
1078          */
1079         if (!strcmp(boardnum, "42")) { /* Linksys */
1080                 if (!strcmp(boardtype, "0x04cf") && !strcmp(getvar("boot_hw_model"), "E300") && !strcmp(getvar("boot_hw_ver"), "1.0"))
1081                         return &platforms[E3000V1];
1082
1083                 if (!strcmp(boardtype, "0x04cf") && !strcmp(getvar("boot_hw_model"), "WRT610N") && !strcmp(getvar("boot_hw_ver"), "2.0"))
1084                         return &platforms[WRT610NV2];
1085         }
1086
1087         if (startswith(getvar("pmon_ver"), "CFE")) {
1088                 /* CFE based - newer hardware */
1089                 if (!strcmp(boardnum, "42")) { /* Linksys */
1090                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
1091                                 return &platforms[WRT300NV11];
1092
1093                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
1094                                 return &platforms[WRT350N];
1095
1096                         if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
1097                                 return &platforms[WRT54G3G];
1098
1099                         if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1100                                 return &platforms[WRT54G3GV2_VF];
1101
1102                         if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1103                                 return &platforms[WRTSL54GS];
1104
1105                         if (!strcmp(boardtype, "0x0472"))
1106                                 return &platforms[WRT160N];
1107
1108                         /* default to WRT54G */
1109                         return &platforms[WRT54G];
1110                 }
1111                 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1112                         return &platforms[WAP54GV2];
1113
1114                 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1115                         return &platforms[WL1600GL];
1116
1117
1118                 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1119                         if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1120                                 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1121                 }
1122
1123                 if (!strcmp(boardnum, "45")) { /* ASUS */
1124                         if (!strcmp(boardtype,"0x042f"))
1125                                 return &platforms[WL500GP];
1126                         else if (!strcmp(boardtype,"0x0472"))
1127                                 return &platforms[WL500W];
1128                         else if (!strcmp(boardtype,"0x467"))
1129                                 return &platforms[WL320GE];
1130                         else
1131                                 return &platforms[WL500GD];
1132                 }
1133
1134                 if (!strcmp(boardnum, "10496"))
1135                         return &platforms[USR5461];
1136
1137                 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1138                         return &platforms[SE505V2];
1139
1140                 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1141                                 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1142                         return &platforms[DIR320];
1143
1144                 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1145                         return &platforms[WDNetCenter];
1146                 }
1147
1148                 if (!strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B  V1 and V2*/
1149                         if (!strcmp(boardnum, "08") || !strcmp(boardnum, "8"))
1150                                 return &platforms[WNR834BV1];
1151                         if (!strcmp(boardnum, "01") || !strcmp(boardnum, "1"))
1152                                 return &platforms[WNR834BV2];
1153                 }
1154
1155         } else { /* PMON based - old stuff */
1156                 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1157                         (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1158                         return &platforms[WR850GV1];
1159                 }
1160                 if (startswith(boardtype, "bcm94710dev")) {
1161                         if (!strcmp(boardnum, "42"))
1162                                 return &platforms[WRT54GV1];
1163                         if (simple_strtoul(boardnum, NULL, 0) == 2)
1164                                 return &platforms[WAP54GV1];
1165                 }
1166                 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1167                 if (startswith(getvar("hardware_version"), "WL500-")) {
1168                         if (!strcmp(getvar("boardnum"), "mn700"))
1169                                 return &platforms[MN700];
1170                         else
1171                                 return &platforms[WL500G];
1172                 }
1173                 if (startswith(getvar("hardware_version"), "WL300-")) {
1174                         /* Either WL-300g or WL-HDD, do more extensive checks */
1175                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1176                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1177                                 return &platforms[WLHDD];
1178                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1179                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1180                                 return &platforms[WL300G];
1181                 }
1182                 /* Sitecom WL-105b */
1183                 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1184                         return &platforms[WL105B];
1185
1186                 /* unknown asus stuff, probably bcm4702 */
1187                 if (startswith(boardnum, "asusX"))
1188                         return &platforms[ASUS_4702];
1189
1190                 /* bcm4702 based Askey RT210W clones, Including:
1191                  * Askey RT210W (duh?)
1192                  * Siemens SE505v1
1193                  * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1194                  */
1195                 if (!strcmp(boardtype,"bcm94710r4")
1196                  && !strcmp(boardnum ,"100")
1197                  && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1198                    ){
1199                         return &platforms[RT210W];
1200                 }
1201         }
1202
1203         if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1204                 if (startswith(boardtype, "bcm94710ap"))
1205                         return &platforms[BUFFALO_UNKNOWN_4710];
1206                 else
1207                         return &platforms[BUFFALO_UNKNOWN];
1208         }
1209
1210         if (startswith(getvar("CFEver"), "MotoWRv2") ||
1211                 startswith(getvar("CFEver"), "MotoWRv3") ||
1212                 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1213
1214                 return &platforms[WR850GV2V3];
1215         }
1216
1217         if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) {  /* Trendware TEW-411BRP+ */
1218                 return &platforms[TEW411BRPP];
1219         }
1220
1221         if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1222                 return &platforms[STI_NAS];
1223
1224         if (!strcmp(boardnum, "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1225                 return &platforms[DWL3150];
1226
1227         if (!strcmp(boardnum, "01") && !strcmp(boardtype, "0x048e") && /* Edimax PS1208MFG */
1228                 !strcmp(getvar("status_gpio"), "1")) /* gpio based detection */
1229                 return &platforms[PS1208MFG];
1230
1231         /* not found */
1232         return NULL;
1233 }
1234
1235 static void register_buttons(struct button_t *b)
1236 {
1237         for (; b->name; b++)
1238                 platform.button_mask |= b->gpio;
1239
1240         platform.button_mask &= ~gpiomask;
1241
1242         gpio_outen(platform.button_mask, 0);
1243         gpio_control(platform.button_mask, 0);
1244         platform.button_polarity = gpio_in() & platform.button_mask;
1245         gpio_intpolarity(platform.button_mask, platform.button_polarity);
1246         gpio_setintmask(platform.button_mask, platform.button_mask);
1247
1248         gpio_set_irqenable(1, button_handler);
1249 }
1250
1251 static void unregister_buttons(struct button_t *b)
1252 {
1253         gpio_setintmask(platform.button_mask, 0);
1254
1255         gpio_set_irqenable(0, button_handler);
1256 }
1257
1258
1259 static void add_msg(struct event_t *event, char *msg, int argv)
1260 {
1261         char *s;
1262
1263         if (argv)
1264                 return;
1265
1266         s = skb_put(event->skb, strlen(msg) + 1);
1267         strcpy(s, msg);
1268 }
1269
1270 static void hotplug_button(struct work_struct *work)
1271 {
1272         struct event_t *event = container_of(work, struct event_t, wq);
1273         char *s;
1274
1275         event->skb = alloc_skb(2048, GFP_KERNEL);
1276
1277         s = skb_put(event->skb, strlen(event->action) + 2);
1278         sprintf(s, "%s@", event->action);
1279         fill_event(event);
1280
1281         NETLINK_CB(event->skb).dst_group = 1;
1282         broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
1283
1284         kfree(event);
1285 }
1286
1287
1288 static int fill_event (struct event_t *event)
1289 {
1290         static char buf[128];
1291
1292         add_msg(event, "HOME=/", 0);
1293         add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1294         add_msg(event, "SUBSYSTEM=button", 0);
1295         snprintf(buf, 128, "ACTION=%s", event->action);
1296         add_msg(event, buf, 0);
1297         snprintf(buf, 128, "BUTTON=%s", event->name);
1298         add_msg(event, buf, 0);
1299         snprintf(buf, 128, "SEEN=%ld", event->seen);
1300         add_msg(event, buf, 0);
1301         snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1302         add_msg(event, buf, 0);
1303
1304         return 0;
1305 }
1306
1307
1308 static irqreturn_t button_handler(int irq, void *dev_id)
1309 {
1310         struct button_t *b;
1311         u32 in, changed;
1312
1313         in = gpio_in() & platform.button_mask;
1314         gpio_intpolarity(platform.button_mask, in);
1315         changed = platform.button_polarity ^ in;
1316         platform.button_polarity = in;
1317
1318         changed &= ~gpio_outen(0, 0);
1319
1320         for (b = platform.buttons; b->name; b++) {
1321                 struct event_t *event;
1322
1323                 if (!(b->gpio & changed)) continue;
1324
1325                 b->pressed ^= 1;
1326
1327                 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1328                         event->seen = (jiffies - b->seen)/HZ;
1329                         event->name = b->name;
1330                         event->action = b->pressed ? "pressed" : "released";
1331                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1332                         schedule_work(&event->wq);
1333                 }
1334
1335                 b->seen = jiffies;
1336         }
1337         return IRQ_HANDLED;
1338 }
1339
1340 static void register_leds(struct led_t *l)
1341 {
1342         struct proc_dir_entry *p;
1343         u32 mask = 0;
1344         u32 oe_mask = 0;
1345         u32 val = 0;
1346
1347         leds = proc_mkdir("led", diag);
1348         if (!leds)
1349                 return;
1350
1351         for(; l->name; l++) {
1352                 if (l->gpio & gpiomask)
1353                         continue;
1354
1355                 if (l->gpio & GPIO_TYPE_EXTIF) {
1356                         l->state = 0;
1357                         set_led_extif(l);
1358                 } else {
1359                         if (l->polarity != INPUT) oe_mask |= l->gpio;
1360                         mask |= l->gpio;
1361                         val |= (l->polarity == NORMAL)?0:l->gpio;
1362                 }
1363
1364                 if (l->polarity == INPUT) continue;
1365
1366                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1367                         l->proc.type = PROC_LED;
1368                         l->proc.ptr = l;
1369                         p->data = (void *) &l->proc;
1370                         p->proc_fops = &diag_proc_fops;
1371                 }
1372         }
1373
1374         gpio_outen(mask, oe_mask);
1375         gpio_control(mask, 0);
1376         gpio_out(mask, val);
1377         gpio_setintmask(mask, 0);
1378 }
1379
1380 static void unregister_leds(struct led_t *l)
1381 {
1382         for(; l->name; l++)
1383                 remove_proc_entry(l->name, leds);
1384
1385         remove_proc_entry("led", diag);
1386 }
1387
1388 static void set_led_extif(struct led_t *led)
1389 {
1390         gpio_set_extif(led->gpio, led->state);
1391 }
1392
1393 static void led_flash(unsigned long dummy) {
1394         struct led_t *l;
1395         u32 mask = 0;
1396         u8 extif_blink = 0;
1397
1398         for (l = platform.leds; l->name; l++) {
1399                 if (l->flash) {
1400                         if (l->gpio & GPIO_TYPE_EXTIF) {
1401                                 extif_blink = 1;
1402                                 l->state = !l->state;
1403                                 set_led_extif(l);
1404                         } else {
1405                                 mask |= l->gpio;
1406                         }
1407                 }
1408         }
1409
1410         mask &= ~gpiomask;
1411         if (mask) {
1412                 u32 val = ~gpio_in();
1413
1414                 gpio_outen(mask, mask);
1415                 gpio_control(mask, 0);
1416                 gpio_out(mask, val);
1417         }
1418         if (mask || extif_blink) {
1419                 mod_timer(&led_timer, jiffies + FLASH_TIME);
1420         }
1421 }
1422
1423 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1424 {
1425         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1426         char *page;
1427         int len = 0;
1428
1429         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1430                 return -ENOBUFS;
1431
1432         if (dent->data != NULL) {
1433                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1434                 switch (handler->type) {
1435                         case PROC_LED: {
1436                                 struct led_t * led = (struct led_t *) handler->ptr;
1437                                 if (led->flash) {
1438                                         len = sprintf(page, "f\n");
1439                                 } else {
1440                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1441                                                 len = sprintf(page, "%d\n", led->state);
1442                                         } else {
1443                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1444                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
1445                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1446                                         }
1447                                 }
1448                                 break;
1449                         }
1450                         case PROC_MODEL:
1451                                 len = sprintf(page, "%s\n", platform.name);
1452                                 break;
1453                         case PROC_GPIOMASK:
1454                                 len = sprintf(page, "0x%04x\n", gpiomask);
1455                                 break;
1456                 }
1457         }
1458         len += 1;
1459
1460         if (*ppos < len) {
1461                 len = min_t(int, len - *ppos, count);
1462                 if (copy_to_user(buf, (page + *ppos), len)) {
1463                         kfree(page);
1464                         return -EFAULT;
1465                 }
1466                 *ppos += len;
1467         } else {
1468                 len = 0;
1469         }
1470
1471         kfree(page);
1472         return len;
1473 }
1474
1475
1476 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1477 {
1478         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1479         char *page;
1480         int ret = -EINVAL;
1481
1482         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1483                 return -ENOBUFS;
1484
1485         if (copy_from_user(page, buf, count)) {
1486                 kfree(page);
1487                 return -EINVAL;
1488         }
1489         page[count] = 0;
1490
1491         if (dent->data != NULL) {
1492                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1493                 switch (handler->type) {
1494                         case PROC_LED: {
1495                                 struct led_t *led = (struct led_t *) handler->ptr;
1496                                 int p = (led->polarity == NORMAL ? 0 : 1);
1497
1498                                 if (page[0] == 'f') {
1499                                         led->flash = 1;
1500                                         led_flash(0);
1501                                 } else {
1502                                         led->flash = 0;
1503                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1504                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1505                                                 set_led_extif(led);
1506                                         } else {
1507                                                 gpio_outen(led->gpio, led->gpio);
1508                                                 gpio_control(led->gpio, 0);
1509                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1510                                         }
1511                                 }
1512                                 break;
1513                         }
1514                         case PROC_GPIOMASK:
1515                                 gpiomask = simple_strtoul(page, NULL, 0);
1516
1517                                 if (platform.buttons) {
1518                                         unregister_buttons(platform.buttons);
1519                                         register_buttons(platform.buttons);
1520                                 }
1521
1522                                 if (platform.leds) {
1523                                         unregister_leds(platform.leds);
1524                                         register_leds(platform.leds);
1525                                 }
1526                                 break;
1527                 }
1528                 ret = count;
1529         }
1530
1531         kfree(page);
1532         return ret;
1533 }
1534
1535 static int __init diag_init(void)
1536 {
1537         static struct proc_dir_entry *p;
1538         static struct platform_t *detected;
1539
1540         detected = platform_detect();
1541         if (!detected) {
1542                 printk(MODULE_NAME ": Router model not detected.\n");
1543                 return -ENODEV;
1544         }
1545         memcpy(&platform, detected, sizeof(struct platform_t));
1546
1547         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1548         if (platform.platform_init != NULL) {
1549                 platform.platform_init();
1550         }
1551
1552         if (!(diag = proc_mkdir("diag", NULL))) {
1553                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1554                 return -EINVAL;
1555         }
1556
1557         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1558                 p->data = (void *) &proc_model;
1559                 p->proc_fops = &diag_proc_fops;
1560         }
1561
1562         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1563                 p->data = (void *) &proc_gpiomask;
1564                 p->proc_fops = &diag_proc_fops;
1565         }
1566
1567         if (platform.buttons)
1568                 register_buttons(platform.buttons);
1569
1570         if (platform.leds)
1571                 register_leds(platform.leds);
1572
1573         return 0;
1574 }
1575
1576 static void __exit diag_exit(void)
1577 {
1578         del_timer(&led_timer);
1579
1580         if (platform.buttons)
1581                 unregister_buttons(platform.buttons);
1582
1583         if (platform.leds)
1584                 unregister_leds(platform.leds);
1585
1586         remove_proc_entry("model", diag);
1587         remove_proc_entry("gpiomask", diag);
1588         remove_proc_entry("diag", NULL);
1589 }
1590
1591 module_init(diag_init);
1592 module_exit(diag_exit);
1593
1594 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1595 MODULE_LICENSE("GPL");