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