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