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