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