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