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