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