501b835f477ceb066d06f6e3ecf83848edbe6629
[15.05/openwrt.git] / package / 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 extern u64 uevent_next_seqnum(void);
36
37 #include "gpio.h"
38 #include "diag.h"
39 #define getvar(str) (nvram_get(str)?:"")
40
41 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
42 static int fill_event(struct event_t *);
43 static unsigned int gpiomask = 0;
44 module_param(gpiomask, int, 0644);
45
46 enum {
47         /* Linksys */
48         WAP54GV1,
49         WAP54GV2,
50         WAP54GV3,
51         WRT54GV1,
52         WRT54G,
53         WRTSL54GS,
54         WRT54G3G,
55         WRT54G3GV2_VF,
56         WRT160N,
57         WRT300NV11,
58         WRT350N,
59         WRT600N,
60         WRT600NV11,
61         WRT610N,
62
63         /* ASUS */
64         WLHDD,
65         WL300G,
66         WL320GE,
67         WL330GE,
68         WL500G,
69         WL500GD,
70         WL500GP,
71         WL500GPV2,
72         WL500W,
73         WL520GC,
74         WL520GU,
75         ASUS_4702,
76         WL700GE,
77
78         /* Buffalo */
79         WBR2_G54,
80         WHR_G54S,
81         WHR_HP_G54,
82         WHR_G125,
83         WHR2_A54G54,
84         WLA2_G54L,
85         WZR_G300N,
86         WZR_RS_G54,
87         WZR_RS_G54HP,
88         BUFFALO_UNKNOWN,
89         BUFFALO_UNKNOWN_4710,
90
91         /* Siemens */
92         SE505V1,
93         SE505V2,
94
95         /* US Robotics */
96         USR5461,
97
98         /* Dell */
99         TM2300,
100         TM2300V2,
101
102         /* Motorola */
103         WE800G,
104         WR850GV1,
105         WR850GV2V3,
106         WR850GP,
107
108         /* Belkin */
109         BELKIN_UNKNOWN,
110
111         /* Netgear */
112         WGT634U,
113         WNR834BV2,
114
115         /* Trendware */
116         TEW411BRPP,
117
118         /* SimpleTech */
119         STI_NAS,
120
121         /* D-Link */
122         DIR130,
123         DIR320,
124         DIR330,
125         DWL3150,
126
127         /* Sitecom */
128         WL105B,
129
130         /* Western Digital */
131         WDNetCenter,
132
133         /* Askey */
134         RT210W,
135
136         /* OvisLink */
137         WL1600GL,
138
139         /* Microsoft */
140         MN700,
141 };
142
143 static void __init bcm4780_init(void) {
144                 int pin = 1 << 3;
145
146                 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
147                 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
148                 gpio_outen(pin, pin);
149                 gpio_control(pin, 0);
150                 gpio_out(pin, pin);
151
152                 /* Wait 5s, so the HDD can spin up */
153                 set_current_state(TASK_INTERRUPTIBLE);
154                 schedule_timeout(HZ * 5);
155 }
156
157 static void __init NetCenter_init(void) {
158                 /* unset pin 6 (+12V) */
159                 int pin = 1 << 6;
160                 gpio_outen(pin, pin);
161                 gpio_control(pin, 0);
162                 gpio_out(pin, pin);
163                 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
164                 pin = 1 << 1;
165                 gpio_outen(pin, pin);
166                 gpio_control(pin, 0);
167                 gpio_out(pin, pin);
168                 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
169                 bcm4780_init();
170 }
171
172 static void __init bcm57xx_init(void) {
173         int pin = 1 << 2;
174
175         /* FIXME: switch comes up, but port mappings/vlans not right */
176         gpio_outen(pin, pin);
177         gpio_control(pin, 0);
178         gpio_out(pin, pin);
179 }
180
181 static struct platform_t __initdata platforms[] = {
182         /* Linksys */
183         [WAP54GV1] = {
184                 .name           = "Linksys WAP54G V1",
185                 .buttons        = {
186                         { .name = "reset",      .gpio = 1 << 0 },
187                 },
188                 .leds           = {
189                         { .name = "diag",       .gpio = 1 << 3 },
190                         { .name = "wlan",       .gpio = 1 << 4 },
191                 },
192         },
193         [WAP54GV2] = {
194                 .name           = "Linksys WAP54G V2",
195                 .buttons        = {
196                         { .name = "reset",      .gpio = 1 << 0 },
197                 },
198                 .leds           = {
199                         { .name = "wlan",       .gpio = 1 << 5, .polarity = REVERSE },
200                         /* GPIO 6 is b44 (eth0, LAN) PHY power */
201                 },
202         },
203         [WAP54GV3] = {
204                 .name           = "Linksys WAP54G V3",
205                 .buttons        = {
206                         /* FIXME: verify this */
207                         { .name = "reset",      .gpio = 1 << 7 },
208                         { .name = "ses",        .gpio = 1 << 0 },
209                 },
210                 .leds           = {
211                         /* FIXME: diag? */
212                         { .name = "ses",        .gpio = 1 << 1 },
213                 },
214         },
215         [WRT54GV1] = {
216                 .name           = "Linksys WRT54G V1.x",
217                 .buttons        = {
218                         { .name = "reset",      .gpio = 1 << 6 },
219                 },
220                 .leds           = {
221                         { .name = "diag",       .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
222                         { .name = "dmz",        .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
223                 },
224         },
225         [WRT54G] = {
226                 .name           = "Linksys WRT54G/GS/GL",
227                 .buttons        = {
228                         { .name = "reset",      .gpio = 1 << 6 },
229                         { .name = "ses",        .gpio = 1 << 4 },
230                 },
231                 .leds           = {
232                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
233                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
234                         { .name = "ses_white",  .gpio = 1 << 2, .polarity = REVERSE },
235                         { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
236                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
237                 },
238         },
239         [WRTSL54GS] = {
240                 .name           = "Linksys WRTSL54GS",
241                 .buttons        = {
242                         { .name = "reset",      .gpio = 1 << 6 },
243                         { .name = "ses",        .gpio = 1 << 4 },
244                 },
245                 .leds           = {
246                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
247                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },
248                         { .name = "ses_white",  .gpio = 1 << 5, .polarity = REVERSE },
249                         { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
250                 },
251         },
252         [WRT54G3G] = {
253                 .name           = "Linksys WRT54G3G",
254                 .buttons        = {
255                         { .name = "reset",      .gpio = 1 << 6 },
256                         { .name = "3g",         .gpio = 1 << 4 },
257                 },
258                 .leds           = {
259                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
260                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
261                         { .name = "3g_green",   .gpio = 1 << 2, .polarity = NORMAL },
262                         { .name = "3g_blue",    .gpio = 1 << 3, .polarity = NORMAL },
263                         { .name = "3g_blink",   .gpio = 1 << 5, .polarity = NORMAL },
264                 },
265         },
266         [WRT54G3GV2_VF] = {
267                 .name           = "Linksys WRT54G3GV2-VF",
268                 .buttons        = {
269                         { .name = "reset",      .gpio = 1 << 6 },
270                         { .name = "3g",         .gpio = 1 << 5 },
271                 },
272                 .leds           = {
273                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
274                         { .name = "3g_green",   .gpio = 1 << 2, .polarity = NORMAL },
275                         { .name = "3g_blue",    .gpio = 1 << 3, .polarity = NORMAL },
276                 },
277         },
278         [WRT160N] = {
279                 .name           = "Linksys WRT160N",
280                 .buttons        = {
281                         { .name = "reset",      .gpio = 1 << 6 },
282                         { .name = "ses",        .gpio = 1 << 4 },
283                 },
284                 .leds           = {
285                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
286                         { .name = "ses_blue",   .gpio = 1 << 5, .polarity = REVERSE },
287                         { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
288                 },
289         },
290         [WRT300NV11] = {
291                 .name           = "Linksys WRT300N V1.1",
292                 .buttons        = {
293                         { .name = "reset",     .gpio = 1 << 6 }, // "Reset" on back panel
294                         { .name = "ses",       .gpio = 1 << 4 }, // "Reserved" on top panel
295                 },
296                 .leds           = {
297                         { .name = "power",     .gpio = 1 << 1, .polarity = NORMAL  }, // "Power"
298                         { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
299                         { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
300                 },
301                 .platform_init = bcm57xx_init,
302         },
303         [WRT350N] = {
304                 .name           = "Linksys WRT350N",
305                 .buttons        = {
306                         { .name = "reset",      .gpio = 1 << 6 },
307                         { .name = "ses",        .gpio = 1 << 8 },
308                 },
309                 .leds           = {
310                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
311                         { .name = "ses_amber",  .gpio = 1 << 3, .polarity = REVERSE },
312                         { .name = "ses_green",  .gpio = 1 << 9, .polarity = REVERSE },
313                         { .name = "usb_blink",  .gpio = 1 << 10, .polarity = REVERSE },
314                         { .name = "usb",        .gpio = 1 << 11, .polarity = REVERSE },
315                 },
316                 .platform_init = bcm57xx_init,
317         },
318         [WRT600N] = {
319                 .name           = "Linksys WRT600N",
320                 .buttons        = {
321                         { .name = "reset",      .gpio = 1 << 6 },
322                         { .name = "ses",        .gpio = 1 << 7 },
323                 },
324                 .leds           = {
325                         { .name = "power",              .gpio = 1 << 2,  .polarity = REVERSE }, // Power LED
326                         { .name = "usb",                .gpio = 1 << 3,  .polarity = REVERSE }, // USB LED
327                         { .name = "wl0_ses_amber",      .gpio = 1 << 8,  .polarity = REVERSE }, // 2.4Ghz LED Amber
328                         { .name = "wl0_ses_green",      .gpio = 1 << 9,  .polarity = REVERSE }, // 2.4Ghz LED Green
329                         { .name = "wl1_ses_amber",      .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
330                         { .name = "wl1_ses_green",      .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
331                 },
332                 .platform_init = bcm57xx_init,
333         },
334         [WRT600NV11] = {
335                 .name           = "Linksys WRT600N V1.1",
336                 .buttons        = {
337                         { .name = "reset",      .gpio = 1 << 6 },
338                         { .name = "ses",        .gpio = 1 << 7 },
339                 },
340                 .leds           = {
341                         { .name = "power",             .gpio = 1 << 2,  .polarity = REVERSE }, // Power LED
342                         { .name = "usb",                .gpio = 1 << 3,  .polarity = REVERSE }, // USB LED
343                         { .name = "wl0_ses_amber",      .gpio = 1 << 8,  .polarity = REVERSE }, // 2.4Ghz LED Amber
344                         { .name = "wl0_ses_green",     .gpio = 1 << 9,  .polarity = REVERSE }, // 2.4Ghz LED Green
345                         { .name = "wl1_ses_amber",      .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
346                         { .name = "wl1_ses_green",      .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
347                 },
348                 .platform_init = bcm57xx_init,
349         },
350         [WRT610N] = {
351                 .name           = "Linksys WRT610N",
352                 .buttons        = {
353                         { .name = "reset",      .gpio = 1 << 6 },
354                         { .name = "ses",        .gpio = 1 << 8 },
355                 },
356                 .leds           = {
357                         { .name = "power",      .gpio = 1 << 1,  .polarity = NORMAL }, // Power LED
358                         { .name = "usb",        .gpio = 1 << 0,  .polarity = REVERSE }, // USB LED
359                         { .name = "ses_amber",  .gpio = 1 << 3,  .polarity = REVERSE }, // WiFi protected setup LED amber
360                         { .name = "ses_blue",   .gpio = 1 << 9,  .polarity = REVERSE }, // WiFi protected setup LED blue
361                 },
362         },
363         /* Asus */
364         [WLHDD] = {
365                 .name           = "ASUS WL-HDD",
366                 .buttons        = {
367                         { .name = "reset",      .gpio = 1 << 6 },
368                 },
369                 .leds           = {
370                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
371                         { .name = "usb",        .gpio = 1 << 2, .polarity = REVERSE },
372                 },
373         },
374         [WL300G] = {
375                 .name           = "ASUS WL-300g",
376                 .buttons        = {
377                         { .name = "reset",      .gpio = 1 << 6 },
378                 },
379                 .leds           = {
380                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
381                 },
382         },
383         [WL320GE] = {
384                 .name           = "ASUS WL-320gE/WL-320gP",
385                 .buttons        = {
386                         { .name = "reset",      .gpio = 1 << 6 },
387                 },
388                 .leds           = {
389                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
390                         { .name = "power",      .gpio = 1 << 2, .polarity = REVERSE },
391                         { .name = "link",       .gpio = 1 << 11, .polarity = REVERSE },
392                 },
393         },
394         [WL330GE] = {
395                 .name           = "ASUS WL-330gE",
396                 .buttons        = {
397                         { .name = "reset",      .gpio = 1 << 2 },
398                 },
399                 .leds           = {
400                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
401                 },
402         },
403         [WL500G] = {
404                 .name           = "ASUS WL-500g",
405                 .buttons        = {
406                         { .name = "reset",      .gpio = 1 << 6 },
407                 },
408                 .leds           = {
409                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
410                 },
411         },
412         [WL500GD] = {
413                 .name           = "ASUS WL-500g Deluxe",
414                 .buttons        = {
415                         { .name = "reset",      .gpio = 1 << 6 },
416                 },
417                 .leds           = {
418                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
419                 },
420         },
421         [WL500GP] = {
422                 .name           = "ASUS WL-500g Premium",
423                 .buttons        = {
424                         { .name = "reset",      .gpio = 1 << 0 },
425                         { .name = "ses",        .gpio = 1 << 4 },
426                 },
427                 .leds           = {
428                         { .name = "power",      .gpio = 1 << 1, .polarity = REVERSE },
429                 },
430         },
431         [WL500GPV2] = {
432                 .name           = "ASUS WL-500g Premium V2",
433                 .buttons        = {
434                         { .name = "reset",      .gpio = 1 << 2 },
435                         { .name = "ses",        .gpio = 1 << 3 },
436                 },
437                 .leds           = {
438                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
439                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
440                 },
441         },
442         [WL500W] = {
443                 .name           = "ASUS WL-500W",
444                 .buttons        = {
445                         { .name = "reset",      .gpio = 1 << 6 },
446                         { .name = "ses",        .gpio = 1 << 7 },
447                 },
448                 .leds           = {
449                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
450                 },
451         },
452         [WL520GC] = {
453                 .name           = "ASUS WL-520GC",
454                 .buttons        = {
455                         { .name = "reset",      .gpio = 1 << 2 },
456                         { .name = "ses",        .gpio = 1 << 3 },
457                 },
458                 .leds           = {
459                 { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
460                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
461                 },
462         },
463         [WL520GU] = {
464                 .name           = "ASUS WL-520gU",
465                 .buttons        = {
466                         { .name = "reset",      .gpio = 1 << 2 },
467                         { .name = "ses",        .gpio = 1 << 3 },
468                 },
469                 .leds           = {
470                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
471                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
472                 },
473         },
474         [ASUS_4702] = {
475                 .name           = "ASUS (unknown, BCM4702)",
476                 .buttons        = {
477                         { .name = "reset",      .gpio = 1 << 6 },
478                 },
479                 .leds           = {
480                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
481                 },
482         },
483         [WL700GE] = {
484                 .name           = "ASUS WL-700gE",
485                 .buttons        = {
486                         { .name = "reset",      .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
487                         { .name = "ses",        .gpio = 1 << 4 }, // on back, actual name ezsetup
488                         { .name = "power",      .gpio = 1 << 0 }, // on front
489                         { .name = "copy",       .gpio = 1 << 6 }, // on front
490                 },
491                 .leds           = {
492 #if 0
493                         // GPIO that controls power led also enables/disables some essential functions
494                         // - power to HDD
495                         // - switch leds
496                         { .name = "power",      .gpio = 1 << 3, .polarity = NORMAL },  // actual name power
497 #endif
498                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
499                 },
500                 .platform_init = bcm4780_init,
501         },
502         /* Buffalo */
503         [WHR_G54S] = {
504                 .name           = "Buffalo WHR-G54S",
505                 .buttons        = {
506                         { .name = "reset",      .gpio = 1 << 4 },
507                         { .name = "bridge",     .gpio = 1 << 5 },
508                         { .name = "ses",        .gpio = 1 << 0 },
509                 },
510                 .leds           = {
511                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
512                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
513                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
514                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
515                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
516                 },
517         },
518         [WBR2_G54] = {
519                 .name           = "Buffalo WBR2-G54",
520                 /* FIXME: verify */
521                 .buttons        = {
522                         { .name = "reset",      .gpio = 1 << 7 },
523                 },
524                 .leds           = {
525                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
526                 },
527         },
528         [WHR_HP_G54] = {
529                 .name           = "Buffalo WHR-HP-G54",
530                 .buttons        = {
531                         { .name = "reset",      .gpio = 1 << 4 },
532                         { .name = "bridge",     .gpio = 1 << 5 },
533                         { .name = "ses",        .gpio = 1 << 0 },
534                 },
535                 .leds           = {
536                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
537                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
538                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
539                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
540                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
541                 },
542         },
543         [WHR_G125] = {
544                 .name           = "Buffalo WHR-G125",
545                 .buttons        = {
546                         { .name = "reset",      .gpio = 1 << 4 },
547                         { .name = "bridge",     .gpio = 1 << 5 },
548                         { .name = "ses",        .gpio = 1 << 0 },
549                 },
550                 .leds           = {
551                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
552                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
553                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
554                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
555                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
556                 },
557         },
558         [WHR2_A54G54] = {
559                 .name           = "Buffalo WHR2-A54G54",
560                 .buttons        = {
561                         { .name = "reset",      .gpio = 1 << 4 },
562                 },
563                 .leds           = {
564                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
565                 },
566         },
567         [WLA2_G54L] = {
568                 .name           = "Buffalo WLA2-G54L",
569                 /* FIXME: verify */
570                 .buttons        = {
571                         { .name = "reset",      .gpio = 1 << 7 },
572                 },
573                 .leds           = {
574                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
575                 },
576         },
577         [WZR_G300N] = {
578                 .name           = "Buffalo WZR-G300N",
579                 .buttons        = {
580                         { .name = "reset",      .gpio = 1 << 4 },
581                 },
582                 .leds           = {
583                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
584                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
585                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
586                 },
587         },
588         [WZR_RS_G54] = {
589                 .name           = "Buffalo WZR-RS-G54",
590                 .buttons        = {
591                         { .name = "ses",        .gpio = 1 << 0 },
592                         { .name = "reset",      .gpio = 1 << 4 },
593                 },
594                 .leds           = {
595                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
596                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
597                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
598                 },
599         },
600         [WZR_RS_G54HP] = {
601                 .name           = "Buffalo WZR-RS-G54HP",
602                 .buttons        = {
603                         { .name = "ses",        .gpio = 1 << 0 },
604                         { .name = "reset",      .gpio = 1 << 4 },
605                 },
606                 .leds           = {
607                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
608                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
609                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
610                 },
611         },
612         [BUFFALO_UNKNOWN] = {
613                 .name           = "Buffalo (unknown)",
614                 .buttons        = {
615                         { .name = "reset",      .gpio = 1 << 7 },
616                 },
617                 .leds           = {
618                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
619                 },
620         },
621         [BUFFALO_UNKNOWN_4710] = {
622                 .name           = "Buffalo (unknown, BCM4710)",
623                 .buttons        = {
624                         { .name = "reset",      .gpio = 1 << 4 },
625                 },
626                 .leds           = {
627                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
628                 },
629         },
630         /* Siemens */
631         [SE505V1] = {
632                 .name           = "Siemens SE505 V1",
633                 .buttons        = {
634                         /* No usable buttons */
635                 },
636                 .leds           = {
637 //                      { .name = "power",      .gpio = 1 << 0  .polarity = REVERSE },  // Usable when retrofitting D26 (?)
638                         { .name = "dmz",        .gpio = 1 << 4, .polarity = REVERSE },  // actual name WWW
639                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
640                 },
641         },
642         [SE505V2] = {
643                 .name           = "Siemens SE505 V2",
644                 .buttons        = {
645                         /* No usable buttons */
646                 },
647                 .leds           = {
648                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
649                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },  // actual name WWW
650                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
651                 },
652         },
653         /* US Robotics */
654         [USR5461] = {
655                 .name           = "U.S. Robotics USR5461",
656                 .buttons        = {
657                         /* No usable buttons */
658                 },
659                 .leds           = {
660                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
661                         { .name = "printer",    .gpio = 1 << 1, .polarity = REVERSE },
662                 },
663         },
664         /* Dell */
665         [TM2300] = {
666                 .name           = "Dell TrueMobile 2300",
667                 .buttons        = {
668                         { .name = "reset",      .gpio = 1 << 0 },
669                 },
670                 .leds           = {
671                         { .name = "wlan",       .gpio = 1 << 6, .polarity = REVERSE },
672                         { .name = "power",      .gpio = 1 << 7, .polarity = REVERSE },
673                 },
674         },
675         [TM2300V2] = {
676                 .name           = "Dell TrueMobile 2300 v2",
677                 .buttons        = {
678                         { .name = "reset",      .gpio = 1 << 0 },
679                 },
680                 .leds           = {
681                         { .name = "wlan",       .gpio = 1 << 6, .polarity = REVERSE },
682                         { .name = "power",      .gpio = 1 << 7, .polarity = REVERSE },
683                 },
684         },
685         /* Motorola */
686         [WE800G] = {
687                 .name           = "Motorola WE800G",
688                 .buttons        = {
689                         { .name = "reset",      .gpio = 1 << 0 },
690                 },
691                 .leds           = {
692                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
693                         { .name = "diag",       .gpio = 1 << 2, .polarity = REVERSE },
694                         { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
695                 },
696         },
697         [WR850GV1] = {
698                 .name           = "Motorola WR850G V1",
699                 .buttons        = {
700                         { .name = "reset",      .gpio = 1 << 0 },
701                 },
702                 .leds           = {
703                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
704                         { .name = "diag",       .gpio = 1 << 3, .polarity = REVERSE },
705                         { .name = "dmz",        .gpio = 1 << 6, .polarity = NORMAL },
706                         { .name = "wlan_red",   .gpio = 1 << 5, .polarity = REVERSE },
707                         { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
708                 },
709         },
710         [WR850GV2V3] = {
711                 .name           = "Motorola WR850G V2/V3",
712                 .buttons        = {
713                         { .name = "reset",      .gpio = 1 << 5 },
714                 },
715                 .leds           = {
716                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
717                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
718                         { .name = "wan",        .gpio = 1 << 6, .polarity = INPUT },
719                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
720                 },
721         },
722         [WR850GP] = {
723                 .name           = "Motorola WR850GP",
724                 .buttons        = {
725                         { .name = "reset",      .gpio = 1 << 5 },
726                 },
727                 .leds           = {
728                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
729                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
730                         { .name = "dmz",        .gpio = 1 << 6, .polarity = REVERSE },
731                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
732                 },
733         },
734
735         /* Belkin */
736         [BELKIN_UNKNOWN] = {
737                 .name           = "Belkin (unknown)",
738                 /* FIXME: verify & add detection */
739                 .buttons        = {
740                         { .name = "reset",      .gpio = 1 << 7 },
741                 },
742                 .leds           = {
743                         { .name = "power",      .gpio = 1 << 5, .polarity = NORMAL },
744                         { .name = "wlan",       .gpio = 1 << 3, .polarity = NORMAL },
745                         { .name = "connected",  .gpio = 1 << 0, .polarity = NORMAL },
746                 },
747         },
748         /* Netgear */
749         [WGT634U] = {
750                 .name           = "Netgear WGT634U",
751                 .buttons        = {
752                         { .name = "reset",      .gpio = 1 << 2 },
753                 },
754                 .leds           = {
755                         { .name = "power",      .gpio = 1 << 3, .polarity = NORMAL },
756                 },
757         },
758         [WNR834BV2] = {
759                 .name           = "Netgear WNR834B V2",
760                 .buttons        = {
761                         { .name = "reset",      .gpio = 1 << 6 },
762                 },
763                 .leds           = {
764                         { .name = "power",      .gpio = 1 << 2, .polarity = NORMAL },
765                         { .name = "diag",       .gpio = 1 << 3, .polarity = NORMAL },
766                         { .name = "connected",  .gpio = 1 << 7, .polarity = NORMAL },
767                 },
768         },
769         /* Trendware */
770         [TEW411BRPP] = {
771                 .name           = "Trendware TEW411BRP+",
772                 .buttons        = {
773                         { /* No usable buttons */ },
774                 },
775                 .leds           = {
776                         { .name = "power",      .gpio = 1 << 7, .polarity = NORMAL },
777                         { .name = "wlan",       .gpio = 1 << 1, .polarity = NORMAL },
778                         { .name = "bridge",     .gpio = 1 << 6, .polarity = NORMAL },
779                 },
780         },
781         /* SimpleTech */
782         [STI_NAS] = {
783                 .name      = "SimpleTech SimpleShare NAS",
784                 .buttons        = {
785                         { .name = "reset",      .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
786                 },
787                 .leds      = {
788                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
789                 },
790                 .platform_init = bcm4780_init,
791         },
792         /* D-Link */
793         [DIR130] = {
794                 .name     = "D-Link DIR-130",
795                 .buttons        = {
796                         { .name = "reset",      .gpio = 1 << 3},
797                         { .name = "reserved",   .gpio = 1 << 7},
798                 },
799                 .leds      = {
800                         { .name = "diag",       .gpio = 1 << 0},
801                         { .name = "blue",       .gpio = 1 << 6},
802                 },
803         },
804         [DIR320] = {
805                 .name     = "D-Link DIR-320",
806                 .buttons        = {
807                         { .name = "reserved",   .gpio = 1 << 6},
808                         { .name = "reset",      .gpio = 1 << 7},
809                 },
810                 .leds      = {
811                         { .name = "wlan",       .gpio = 1 << 0, .polarity = NORMAL },
812                         { .name = "diag",       .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
813                         { .name = "red",        .gpio = 1 << 3, .polarity = REVERSE },
814                         { .name = "blue",       .gpio = 1 << 4, .polarity = REVERSE },
815                         { .name = "usb",        .gpio = 1 << 5, .polarity = NORMAL },
816                 },
817         },
818         [DIR330] = {
819                 .name     = "D-Link DIR-330",
820                 .buttons        = {
821                         { .name = "reset",      .gpio = 1 << 3},
822                         { .name = "reserved",   .gpio = 1 << 7},
823                 },
824                 .leds      = {
825                         { .name = "diag",       .gpio = 1 << 0},
826                         { .name = "usb",        .gpio = 1 << 4},
827                         { .name = "blue",       .gpio = 1 << 6},
828                 },
829         },
830         [DWL3150] = {
831                 .name   = "D-Link DWL-3150",
832                 .buttons        = {
833                         { .name = "reset",      .gpio = 1 << 7},
834                 },
835                 .leds     = {
836                         { .name = "diag",       .gpio = 1 << 2},
837                         { .name = "status",     .gpio = 1 << 1},
838                 },
839         },
840         /* Double check */
841         [WL105B] = {
842                 .name   = "Sitecom WL-105b",
843                 .buttons        = {
844                         { .name = "reset",      .gpio = 1 << 10},
845                 },
846                 .leds     = {
847                         { .name = "wlan",       .gpio = 1 << 4},
848                         { .name = "power",      .gpio = 1 << 3},
849                 },
850         },
851         /* Western Digital Net Center */
852         [WDNetCenter] = {
853                 .name   = "Western Digital NetCenter",
854                 .buttons        = {
855                         { .name = "power",      .gpio = 1 << 0},
856                         { .name = "reset",      .gpio = 1 << 7},
857                 },
858                 .platform_init = NetCenter_init,
859         },
860         /* Askey (and clones) */
861         [RT210W] = {
862                 .name           = "Askey RT210W",
863                 .buttons        = {
864                         /* Power button is hard-wired to hardware reset */
865                         /* but is also connected to GPIO 7 (probably for bootloader recovery)  */
866                         { .name = "power",      .gpio = 1 << 7},
867                 },
868                 .leds           = {
869                         /* These were verified and named based on Belkin F5D4230-4 v1112 */
870                         { .name = "connected",  .gpio = 1 << 0, .polarity = REVERSE },
871                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
872                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
873                 },
874         },
875         [WL1600GL] = {
876                 .name           = "OvisLink WL-1600GL",
877                 .buttons        = {
878                         { .name = "reset",      .gpio = 1 << 3 },
879                         { .name = "ses",        .gpio = 1 << 4 },
880                 },
881                 .leds           = {
882                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
883                         { .name = "wps",        .gpio = 1 << 2, .polarity = REVERSE },
884                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
885                         { .name = "connected",  .gpio = 1 << 0, .polarity = REVERSE },
886                 },
887         },
888         /* Microsoft */
889         [MN700] = {
890                 .name   = "Microsoft MN-700",
891                 .buttons        = {
892                         { .name = "reset",      .gpio = 1 << 7 },
893                 },
894                 .leds     = {
895                         { .name = "power",      .gpio = 1 << 6, .polarity = NORMAL },
896                 },
897         },
898 };
899
900 static struct platform_t __init *platform_detect(void)
901 {
902         char *boardnum, *boardtype, *buf;
903
904         if (strcmp(getvar("nvram_type"), "cfe") == 0)
905                 return &platforms[WGT634U];
906
907         /* Look for a model identifier */
908
909         /* Based on "model_name" */
910         if ((buf = nvram_get("model_name"))) {
911                 if (!strcmp(buf, "DIR-130"))
912                         return &platforms[DIR130];
913                 if (!strcmp(buf, "DIR-330"))
914                         return &platforms[DIR330];
915         }
916
917         /* Based on "wsc_modelname */
918         if ((buf = nvram_get("wsc_modelname"))) {
919                 if (!strcmp(buf, "WRT610N"))
920                         return &platforms[WRT610N];
921         }
922
923         /* Based on "model_no" */
924         if ((buf = nvram_get("model_no"))) {
925                 if (startswith(buf,"WL700")) /* WL700* */
926                         return &platforms[WL700GE];
927         }
928
929         /* Based on "hardware_version" */
930         if ((buf = nvram_get("hardware_version"))) {
931                 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
932                         return &platforms[WL500GPV2];
933                 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
934                         return &platforms[WL520GC];
935                 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
936                         return &platforms[WL520GU];
937                 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
938                         return &platforms[WL330GE];
939         }
940
941         /* Based on "ModelId" */
942         if ((buf = nvram_get("ModelId"))) {
943                 if (!strcmp(buf, "WR850GP"))
944                         return &platforms[WR850GP];
945                 if (!strcmp(buf, "WR850G"))
946                         return &platforms[WR850GV2V3];
947                 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
948                         return &platforms[TM2300]; /* Dell TrueMobile 2300 */
949                 if (startswith(buf,"WE800G")) /* WE800G* */
950                         return &platforms[WE800G];
951         }
952
953         /* Buffalo */
954         if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
955                 /* Buffalo hardware, check id for specific hardware matches */
956                 if (!strcmp(buf, "29bb0332"))
957                         return &platforms[WBR2_G54];
958                 if (!strcmp(buf, "29129"))
959                         return &platforms[WLA2_G54L];
960                 if (!strcmp(buf, "30189"))
961                         return &platforms[WHR_HP_G54];
962                 if (!strcmp(buf, "32093"))
963                         return &platforms[WHR_G125];
964                 if (!strcmp(buf, "30182"))
965                         return &platforms[WHR_G54S];
966                 if (!strcmp(buf, "290441dd"))
967                         return &platforms[WHR2_A54G54];
968                 if (!strcmp(buf, "31120"))
969                         return &platforms[WZR_G300N];
970                 if (!strcmp(buf, "30083"))
971                         return &platforms[WZR_RS_G54];
972                 if (!strcmp(buf, "30103"))
973                         return &platforms[WZR_RS_G54HP];
974         }
975
976         /* no easy model number, attempt to guess */
977         boardnum = getvar("boardnum");
978         boardtype = getvar("boardtype");
979
980         if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N  v1/V1.1 */
981                 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
982                         return &platforms[WRT600NV11];
983
984         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
985                         return &platforms[WRT600N];
986         }
987
988         if (startswith(getvar("pmon_ver"), "CFE")) {
989                 /* CFE based - newer hardware */
990                 if (!strcmp(boardnum, "42")) { /* Linksys */
991                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
992                                 return &platforms[WRT300NV11];
993
994                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
995                                 return &platforms[WRT350N];
996
997                         if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
998                                 return &platforms[WRT54G3G];
999
1000                         if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1001                                 return &platforms[WRT54G3GV2_VF];
1002
1003                         if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1004                                 return &platforms[WRTSL54GS];
1005
1006                         if (!strcmp(boardtype, "0x0472"))
1007                                 return &platforms[WRT160N];
1008
1009                         /* default to WRT54G */
1010                         return &platforms[WRT54G];
1011                 }
1012                 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1013                         return &platforms[WAP54GV2];
1014
1015                 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1016                         return &platforms[WL1600GL];
1017
1018
1019                 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1020                         if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1021                                 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1022                 }
1023
1024                 if (!strcmp(boardnum, "45")) { /* ASUS */
1025                         if (!strcmp(boardtype,"0x042f"))
1026                                 return &platforms[WL500GP];
1027                         else if (!strcmp(boardtype,"0x0472"))
1028                                 return &platforms[WL500W];
1029                         else if (!strcmp(boardtype,"0x467"))
1030                                 return &platforms[WL320GE];
1031                         else
1032                                 return &platforms[WL500GD];
1033                 }
1034
1035                 if (!strcmp(boardnum, "10496"))
1036                         return &platforms[USR5461];
1037
1038                 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1039                         return &platforms[SE505V2];
1040
1041                 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1042                                 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1043                         return &platforms[DIR320];
1044
1045                 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1046                         return &platforms[WDNetCenter];
1047                 }
1048
1049                 if ((!strcmp(boardnum, "08") || !strcmp(boardnum, "01")) &&
1050                                 !strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B  V1 and V2*/
1051                         /* TODO: Check for version. Default platform is V2 for now. */
1052                         return &platforms[WNR834BV2];
1053                 }
1054
1055         } else { /* PMON based - old stuff */
1056                 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1057                         (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1058                         return &platforms[WR850GV1];
1059                 }
1060                 if (startswith(boardtype, "bcm94710dev")) {
1061                         if (!strcmp(boardnum, "42"))
1062                                 return &platforms[WRT54GV1];
1063                         if (simple_strtoul(boardnum, NULL, 0) == 2)
1064                                 return &platforms[WAP54GV1];
1065                 }
1066                 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1067                 if (startswith(getvar("hardware_version"), "WL500-")) {
1068                         if (!strcmp(getvar("boardnum"), "mn700"))
1069                                 return &platforms[MN700];
1070                         else
1071                                 return &platforms[WL500G];
1072                 }
1073                 if (startswith(getvar("hardware_version"), "WL300-")) {
1074                         /* Either WL-300g or WL-HDD, do more extensive checks */
1075                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1076                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1077                                 return &platforms[WLHDD];
1078                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1079                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1080                                 return &platforms[WL300G];
1081                 }
1082                 /* Sitecom WL-105b */
1083                 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1084                         return &platforms[WL105B];
1085
1086                 /* unknown asus stuff, probably bcm4702 */
1087                 if (startswith(boardnum, "asusX"))
1088                         return &platforms[ASUS_4702];
1089
1090                 /* bcm4702 based Askey RT210W clones, Including:
1091                  * Askey RT210W (duh?)
1092                  * Siemens SE505v1
1093                  * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1094                  */
1095                 if (!strcmp(boardtype,"bcm94710r4")
1096                  && !strcmp(boardnum ,"100")
1097                  && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1098                    ){
1099                         return &platforms[RT210W];
1100                 }
1101         }
1102
1103         if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1104                 if (startswith(boardtype, "bcm94710ap"))
1105                         return &platforms[BUFFALO_UNKNOWN_4710];
1106                 else
1107                         return &platforms[BUFFALO_UNKNOWN];
1108         }
1109
1110         if (startswith(getvar("CFEver"), "MotoWRv2") ||
1111                 startswith(getvar("CFEver"), "MotoWRv3") ||
1112                 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1113
1114                 return &platforms[WR850GV2V3];
1115         }
1116
1117         if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) {  /* Trendware TEW-411BRP+ */
1118                 return &platforms[TEW411BRPP];
1119         }
1120
1121         if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1122                 return &platforms[STI_NAS];
1123
1124         if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1125                 return &platforms[DWL3150];
1126
1127         /* not found */
1128         return NULL;
1129 }
1130
1131 static void register_buttons(struct button_t *b)
1132 {
1133         for (; b->name; b++)
1134                 platform.button_mask |= b->gpio;
1135
1136         platform.button_mask &= ~gpiomask;
1137
1138         gpio_outen(platform.button_mask, 0);
1139         gpio_control(platform.button_mask, 0);
1140         platform.button_polarity = gpio_in() & platform.button_mask;
1141         gpio_intpolarity(platform.button_mask, platform.button_polarity);
1142         gpio_setintmask(platform.button_mask, platform.button_mask);
1143
1144         gpio_set_irqenable(1, button_handler);
1145 }
1146
1147 static void unregister_buttons(struct button_t *b)
1148 {
1149         gpio_setintmask(platform.button_mask, 0);
1150
1151         gpio_set_irqenable(0, button_handler);
1152 }
1153
1154
1155 static void add_msg(struct event_t *event, char *msg, int argv)
1156 {
1157         char *s;
1158
1159         if (argv)
1160                 return;
1161
1162         s = skb_put(event->skb, strlen(msg) + 1);
1163         strcpy(s, msg);
1164 }
1165
1166 static void hotplug_button(struct work_struct *work)
1167 {
1168         struct event_t *event = container_of(work, struct event_t, wq);
1169         char *s;
1170
1171         event->skb = alloc_skb(2048, GFP_KERNEL);
1172
1173         s = skb_put(event->skb, strlen(event->action) + 2);
1174         sprintf(s, "%s@", event->action);
1175         fill_event(event);
1176
1177         NETLINK_CB(event->skb).dst_group = 1;
1178         broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
1179
1180         kfree(event);
1181 }
1182
1183
1184 static int fill_event (struct event_t *event)
1185 {
1186         static char buf[128];
1187
1188         add_msg(event, "HOME=/", 0);
1189         add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1190         add_msg(event, "SUBSYSTEM=button", 0);
1191         snprintf(buf, 128, "ACTION=%s", event->action);
1192         add_msg(event, buf, 0);
1193         snprintf(buf, 128, "BUTTON=%s", event->name);
1194         add_msg(event, buf, 0);
1195         snprintf(buf, 128, "SEEN=%ld", event->seen);
1196         add_msg(event, buf, 0);
1197         snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1198         add_msg(event, buf, 0);
1199
1200         return 0;
1201 }
1202
1203
1204 static irqreturn_t button_handler(int irq, void *dev_id)
1205 {
1206         struct button_t *b;
1207         u32 in, changed;
1208
1209         in = gpio_in() & platform.button_mask;
1210         gpio_intpolarity(platform.button_mask, in);
1211         changed = platform.button_polarity ^ in;
1212         platform.button_polarity = in;
1213
1214         changed &= ~gpio_outen(0, 0);
1215
1216         for (b = platform.buttons; b->name; b++) {
1217                 struct event_t *event;
1218
1219                 if (!(b->gpio & changed)) continue;
1220
1221                 b->pressed ^= 1;
1222
1223                 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1224                         event->seen = (jiffies - b->seen)/HZ;
1225                         event->name = b->name;
1226                         event->action = b->pressed ? "pressed" : "released";
1227                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1228                         schedule_work(&event->wq);
1229                 }
1230
1231                 b->seen = jiffies;
1232         }
1233         return IRQ_HANDLED;
1234 }
1235
1236 static void register_leds(struct led_t *l)
1237 {
1238         struct proc_dir_entry *p;
1239         u32 mask = 0;
1240         u32 oe_mask = 0;
1241         u32 val = 0;
1242
1243         leds = proc_mkdir("led", diag);
1244         if (!leds)
1245                 return;
1246
1247         for(; l->name; l++) {
1248                 if (l->gpio & gpiomask)
1249                         continue;
1250
1251                 if (l->gpio & GPIO_TYPE_EXTIF) {
1252                         l->state = 0;
1253                         set_led_extif(l);
1254                 } else {
1255                         if (l->polarity != INPUT) oe_mask |= l->gpio;
1256                         mask |= l->gpio;
1257                         val |= (l->polarity == NORMAL)?0:l->gpio;
1258                 }
1259
1260                 if (l->polarity == INPUT) continue;
1261
1262                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1263                         l->proc.type = PROC_LED;
1264                         l->proc.ptr = l;
1265                         p->data = (void *) &l->proc;
1266                         p->proc_fops = &diag_proc_fops;
1267                 }
1268         }
1269
1270         gpio_outen(mask, oe_mask);
1271         gpio_control(mask, 0);
1272         gpio_out(mask, val);
1273         gpio_setintmask(mask, 0);
1274 }
1275
1276 static void unregister_leds(struct led_t *l)
1277 {
1278         for(; l->name; l++)
1279                 remove_proc_entry(l->name, leds);
1280
1281         remove_proc_entry("led", diag);
1282 }
1283
1284 static void set_led_extif(struct led_t *led)
1285 {
1286         gpio_set_extif(led->gpio, led->state);
1287 }
1288
1289 static void led_flash(unsigned long dummy) {
1290         struct led_t *l;
1291         u32 mask = 0;
1292         u8 extif_blink = 0;
1293
1294         for (l = platform.leds; l->name; l++) {
1295                 if (l->flash) {
1296                         if (l->gpio & GPIO_TYPE_EXTIF) {
1297                                 extif_blink = 1;
1298                                 l->state = !l->state;
1299                                 set_led_extif(l);
1300                         } else {
1301                                 mask |= l->gpio;
1302                         }
1303                 }
1304         }
1305
1306         mask &= ~gpiomask;
1307         if (mask) {
1308                 u32 val = ~gpio_in();
1309
1310                 gpio_outen(mask, mask);
1311                 gpio_control(mask, 0);
1312                 gpio_out(mask, val);
1313         }
1314         if (mask || extif_blink) {
1315                 mod_timer(&led_timer, jiffies + FLASH_TIME);
1316         }
1317 }
1318
1319 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1320 {
1321         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1322         char *page;
1323         int len = 0;
1324
1325         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1326                 return -ENOBUFS;
1327
1328         if (dent->data != NULL) {
1329                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1330                 switch (handler->type) {
1331                         case PROC_LED: {
1332                                 struct led_t * led = (struct led_t *) handler->ptr;
1333                                 if (led->flash) {
1334                                         len = sprintf(page, "f\n");
1335                                 } else {
1336                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1337                                                 len = sprintf(page, "%d\n", led->state);
1338                                         } else {
1339                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1340                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
1341                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1342                                         }
1343                                 }
1344                                 break;
1345                         }
1346                         case PROC_MODEL:
1347                                 len = sprintf(page, "%s\n", platform.name);
1348                                 break;
1349                         case PROC_GPIOMASK:
1350                                 len = sprintf(page, "0x%04x\n", gpiomask);
1351                                 break;
1352                 }
1353         }
1354         len += 1;
1355
1356         if (*ppos < len) {
1357                 len = min_t(int, len - *ppos, count);
1358                 if (copy_to_user(buf, (page + *ppos), len)) {
1359                         kfree(page);
1360                         return -EFAULT;
1361                 }
1362                 *ppos += len;
1363         } else {
1364                 len = 0;
1365         }
1366
1367         kfree(page);
1368         return len;
1369 }
1370
1371
1372 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1373 {
1374         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1375         char *page;
1376         int ret = -EINVAL;
1377
1378         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1379                 return -ENOBUFS;
1380
1381         if (copy_from_user(page, buf, count)) {
1382                 kfree(page);
1383                 return -EINVAL;
1384         }
1385         page[count] = 0;
1386
1387         if (dent->data != NULL) {
1388                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1389                 switch (handler->type) {
1390                         case PROC_LED: {
1391                                 struct led_t *led = (struct led_t *) handler->ptr;
1392                                 int p = (led->polarity == NORMAL ? 0 : 1);
1393
1394                                 if (page[0] == 'f') {
1395                                         led->flash = 1;
1396                                         led_flash(0);
1397                                 } else {
1398                                         led->flash = 0;
1399                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1400                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1401                                                 set_led_extif(led);
1402                                         } else {
1403                                                 gpio_outen(led->gpio, led->gpio);
1404                                                 gpio_control(led->gpio, 0);
1405                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1406                                         }
1407                                 }
1408                                 break;
1409                         }
1410                         case PROC_GPIOMASK:
1411                                 gpiomask = simple_strtoul(page, NULL, 0);
1412
1413                                 if (platform.buttons) {
1414                                         unregister_buttons(platform.buttons);
1415                                         register_buttons(platform.buttons);
1416                                 }
1417
1418                                 if (platform.leds) {
1419                                         unregister_leds(platform.leds);
1420                                         register_leds(platform.leds);
1421                                 }
1422                                 break;
1423                 }
1424                 ret = count;
1425         }
1426
1427         kfree(page);
1428         return ret;
1429 }
1430
1431 static int __init diag_init(void)
1432 {
1433         static struct proc_dir_entry *p;
1434         static struct platform_t *detected;
1435
1436         detected = platform_detect();
1437         if (!detected) {
1438                 printk(MODULE_NAME ": Router model not detected.\n");
1439                 return -ENODEV;
1440         }
1441         memcpy(&platform, detected, sizeof(struct platform_t));
1442
1443         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1444         if (platform.platform_init != NULL) {
1445                 platform.platform_init();
1446         }
1447
1448         if (!(diag = proc_mkdir("diag", NULL))) {
1449                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1450                 return -EINVAL;
1451         }
1452
1453         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1454                 p->data = (void *) &proc_model;
1455                 p->proc_fops = &diag_proc_fops;
1456         }
1457
1458         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1459                 p->data = (void *) &proc_gpiomask;
1460                 p->proc_fops = &diag_proc_fops;
1461         }
1462
1463         if (platform.buttons)
1464                 register_buttons(platform.buttons);
1465
1466         if (platform.leds)
1467                 register_leds(platform.leds);
1468
1469         return 0;
1470 }
1471
1472 static void __exit diag_exit(void)
1473 {
1474         del_timer(&led_timer);
1475
1476         if (platform.buttons)
1477                 unregister_buttons(platform.buttons);
1478
1479         if (platform.leds)
1480                 unregister_leds(platform.leds);
1481
1482         remove_proc_entry("model", diag);
1483         remove_proc_entry("gpiomask", diag);
1484         remove_proc_entry("diag", NULL);
1485 }
1486
1487 module_init(diag_init);
1488 module_exit(diag_exit);
1489
1490 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1491 MODULE_LICENSE("GPL");