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