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