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