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