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