620f29171cee828de1b9bd9b03d94e69bf1b412b
[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 <net/sock.h>
34 extern struct sock *uevent_sock;
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 << 7 }, // on back, hardwired, always resets device regardless OS state
786                         { .name = "power",      .gpio = 1 << 0 }, // on back
787                 },
788                 .leds      = {
789                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
790                 },
791                 .platform_init = bcm4780_init,
792         },
793         /* D-Link */
794         [DIR130] = {
795                 .name     = "D-Link DIR-130",
796                 .buttons        = {
797                         { .name = "reset",      .gpio = 1 << 3},
798                         { .name = "reserved",   .gpio = 1 << 7},
799                 },
800                 .leds      = {
801                         { .name = "diag",       .gpio = 1 << 0},
802                         { .name = "blue",       .gpio = 1 << 6},
803                 },
804         },
805         [DIR320] = {
806                 .name     = "D-Link DIR-320",
807                 .buttons        = {
808                         { .name = "reserved",   .gpio = 1 << 6},
809                         { .name = "reset",      .gpio = 1 << 7},
810                 },
811                 .leds      = {
812                         { .name = "wlan",       .gpio = 1 << 0, .polarity = NORMAL },
813                         { .name = "diag",       .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
814                         { .name = "red",        .gpio = 1 << 3, .polarity = REVERSE },
815                         { .name = "blue",       .gpio = 1 << 4, .polarity = REVERSE },
816                         { .name = "usb",        .gpio = 1 << 5, .polarity = NORMAL },
817                 },
818         },
819         [DIR330] = {
820                 .name     = "D-Link DIR-330",
821                 .buttons        = {
822                         { .name = "reset",      .gpio = 1 << 3},
823                         { .name = "reserved",   .gpio = 1 << 7},
824                 },
825                 .leds      = {
826                         { .name = "diag",       .gpio = 1 << 0},
827                         { .name = "usb",        .gpio = 1 << 4},
828                         { .name = "blue",       .gpio = 1 << 6},
829                 },
830         },
831         [DWL3150] = {
832                 .name   = "D-Link DWL-3150",
833                 .buttons        = {
834                         { .name = "reset",      .gpio = 1 << 7},
835                 },
836                 .leds     = {
837                         { .name = "diag",       .gpio = 1 << 2},
838                         { .name = "status",     .gpio = 1 << 1},
839                 },
840         },
841         /* Double check */
842         [WL105B] = {
843                 .name   = "Sitecom WL-105b",
844                 .buttons        = {
845                         { .name = "reset",      .gpio = 1 << 10},
846                 },
847                 .leds     = {
848                         { .name = "wlan",       .gpio = 1 << 4},
849                         { .name = "power",      .gpio = 1 << 3},
850                 },
851         },
852         /* Western Digital Net Center */
853         [WDNetCenter] = {
854                 .name   = "Western Digital NetCenter",
855                 .buttons        = {
856                         { .name = "power",      .gpio = 1 << 0},
857                         { .name = "reset",      .gpio = 1 << 7},
858                 },
859                 .platform_init = NetCenter_init,
860         },
861         /* Askey (and clones) */
862         [RT210W] = {
863                 .name           = "Askey RT210W",
864                 .buttons        = {
865                         /* Power button is hard-wired to hardware reset */
866                         /* but is also connected to GPIO 7 (probably for bootloader recovery)  */
867                         { .name = "power",      .gpio = 1 << 7},
868                 },
869                 .leds           = {
870                         /* These were verified and named based on Belkin F5D4230-4 v1112 */
871                         { .name = "connected",  .gpio = 1 << 0, .polarity = REVERSE },
872                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
873                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
874                 },
875         },
876         [WL1600GL] = {
877                 .name           = "OvisLink WL-1600GL",
878                 .buttons        = {
879                         { .name = "reset",      .gpio = 1 << 3 },
880                         { .name = "ses",        .gpio = 1 << 4 },
881                 },
882                 .leds           = {
883                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
884                         { .name = "wps",        .gpio = 1 << 2, .polarity = REVERSE },
885                         { .name = "wlan",       .gpio = 1 << 1, .polarity = REVERSE },
886                         { .name = "connected",  .gpio = 1 << 0, .polarity = REVERSE },
887                 },
888         },
889         /* Microsoft */
890         [MN700] = {
891                 .name   = "Microsoft MN-700",
892                 .buttons        = {
893                         { .name = "reset",      .gpio = 1 << 7 },
894                 },
895                 .leds     = {
896                         { .name = "power",      .gpio = 1 << 6, .polarity = NORMAL },
897                 },
898         },
899 };
900
901 static struct platform_t __init *platform_detect(void)
902 {
903         char *boardnum, *boardtype, *buf;
904
905         if (strcmp(getvar("nvram_type"), "cfe") == 0)
906                 return &platforms[WGT634U];
907
908         /* Look for a model identifier */
909
910         /* Based on "model_name" */
911         if ((buf = nvram_get("model_name"))) {
912                 if (!strcmp(buf, "DIR-130"))
913                         return &platforms[DIR130];
914                 if (!strcmp(buf, "DIR-330"))
915                         return &platforms[DIR330];
916         }
917
918         /* Based on "wsc_modelname */
919         if ((buf = nvram_get("wsc_modelname"))) {
920                 if (!strcmp(buf, "WRT610N"))
921                         return &platforms[WRT610N];
922         }
923
924         /* Based on "model_no" */
925         if ((buf = nvram_get("model_no"))) {
926                 if (startswith(buf,"WL700")) /* WL700* */
927                         return &platforms[WL700GE];
928         }
929
930         /* Based on "hardware_version" */
931         if ((buf = nvram_get("hardware_version"))) {
932                 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
933                         return &platforms[WL500GPV2];
934                 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
935                         return &platforms[WL520GC];
936                 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
937                         return &platforms[WL520GU];
938                 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
939                         return &platforms[WL330GE];
940         }
941
942         /* Based on "ModelId" */
943         if ((buf = nvram_get("ModelId"))) {
944                 if (!strcmp(buf, "WR850GP"))
945                         return &platforms[WR850GP];
946                 if (!strcmp(buf, "WR850G"))
947                         return &platforms[WR850GV2V3];
948                 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
949                         return &platforms[TM2300]; /* Dell TrueMobile 2300 */
950                 if (startswith(buf,"WE800G")) /* WE800G* */
951                         return &platforms[WE800G];
952         }
953
954         /* Buffalo */
955         if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
956                 /* Buffalo hardware, check id for specific hardware matches */
957                 if (!strcmp(buf, "29bb0332"))
958                         return &platforms[WBR2_G54];
959                 if (!strcmp(buf, "29129"))
960                         return &platforms[WLA2_G54L];
961                 if (!strcmp(buf, "30189"))
962                         return &platforms[WHR_HP_G54];
963                 if (!strcmp(buf, "32093"))
964                         return &platforms[WHR_G125];
965                 if (!strcmp(buf, "30182"))
966                         return &platforms[WHR_G54S];
967                 if (!strcmp(buf, "290441dd"))
968                         return &platforms[WHR2_A54G54];
969                 if (!strcmp(buf, "31120"))
970                         return &platforms[WZR_G300N];
971                 if (!strcmp(buf, "30083"))
972                         return &platforms[WZR_RS_G54];
973                 if (!strcmp(buf, "30103"))
974                         return &platforms[WZR_RS_G54HP];
975         }
976
977         /* no easy model number, attempt to guess */
978         boardnum = getvar("boardnum");
979         boardtype = getvar("boardtype");
980
981         if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N  v1/V1.1 */
982                 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
983                         return &platforms[WRT600NV11];
984
985         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
986                         return &platforms[WRT600N];
987         }
988
989         if (startswith(getvar("pmon_ver"), "CFE")) {
990                 /* CFE based - newer hardware */
991                 if (!strcmp(boardnum, "42")) { /* Linksys */
992                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
993                                 return &platforms[WRT300NV11];
994
995                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
996                                 return &platforms[WRT350N];
997
998                         if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
999                                 return &platforms[WRT54G3G];
1000
1001                         if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1002                                 return &platforms[WRT54G3GV2_VF];
1003
1004                         if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1005                                 return &platforms[WRTSL54GS];
1006
1007                         if (!strcmp(boardtype, "0x0472"))
1008                                 return &platforms[WRT160N];
1009
1010                         /* default to WRT54G */
1011                         return &platforms[WRT54G];
1012                 }
1013                 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1014                         return &platforms[WAP54GV2];
1015
1016                 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1017                         return &platforms[WL1600GL];
1018
1019
1020                 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1021                         if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1022                                 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1023                 }
1024
1025                 if (!strcmp(boardnum, "45")) { /* ASUS */
1026                         if (!strcmp(boardtype,"0x042f"))
1027                                 return &platforms[WL500GP];
1028                         else if (!strcmp(boardtype,"0x0472"))
1029                                 return &platforms[WL500W];
1030                         else if (!strcmp(boardtype,"0x467"))
1031                                 return &platforms[WL320GE];
1032                         else
1033                                 return &platforms[WL500GD];
1034                 }
1035
1036                 if (!strcmp(boardnum, "10496"))
1037                         return &platforms[USR5461];
1038
1039                 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1040                         return &platforms[SE505V2];
1041
1042                 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1043                                 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1044                         return &platforms[DIR320];
1045
1046                 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1047                         return &platforms[WDNetCenter];
1048                 }
1049
1050                 if (!strcmp(boardnum, "08") || !strcmp(boardnum, "01") &&         
1051                                 !strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B  V1 and V2*/
1052                         /* TODO: Check for version. Default platform is V2 for now. */
1053                         return &platforms[WNR834BV2];
1054                 }
1055
1056         } else { /* PMON based - old stuff */
1057                 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1058                         (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1059                         return &platforms[WR850GV1];
1060                 }
1061                 if (startswith(boardtype, "bcm94710dev")) {
1062                         if (!strcmp(boardnum, "42"))
1063                                 return &platforms[WRT54GV1];
1064                         if (simple_strtoul(boardnum, NULL, 0) == 2)
1065                                 return &platforms[WAP54GV1];
1066                 }
1067                 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1068                 if (startswith(getvar("hardware_version"), "WL500-")) {
1069                         if (!strcmp(getvar("boardnum"), "mn700"))
1070                                 return &platforms[MN700];
1071                         else
1072                                 return &platforms[WL500G];
1073                 }
1074                 if (startswith(getvar("hardware_version"), "WL300-")) {
1075                         /* Either WL-300g or WL-HDD, do more extensive checks */
1076                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1077                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1078                                 return &platforms[WLHDD];
1079                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1080                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1081                                 return &platforms[WL300G];
1082                 }
1083                 /* Sitecom WL-105b */
1084                 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1085                         return &platforms[WL105B];
1086
1087                 /* unknown asus stuff, probably bcm4702 */
1088                 if (startswith(boardnum, "asusX"))
1089                         return &platforms[ASUS_4702];
1090
1091                 /* bcm4702 based Askey RT210W clones, Including:
1092                  * Askey RT210W (duh?)
1093                  * Siemens SE505v1
1094                  * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1095                  */
1096                 if (!strcmp(boardtype,"bcm94710r4")
1097                  && !strcmp(boardnum ,"100")
1098                  && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1099                    ){
1100                         return &platforms[RT210W];
1101                 }
1102         }
1103
1104         if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1105                 if (startswith(boardtype, "bcm94710ap"))
1106                         return &platforms[BUFFALO_UNKNOWN_4710];
1107                 else
1108                         return &platforms[BUFFALO_UNKNOWN];
1109         }
1110
1111         if (startswith(getvar("CFEver"), "MotoWRv2") ||
1112                 startswith(getvar("CFEver"), "MotoWRv3") ||
1113                 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1114
1115                 return &platforms[WR850GV2V3];
1116         }
1117
1118         if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) {  /* Trendware TEW-411BRP+ */
1119                 return &platforms[TEW411BRPP];
1120         }
1121
1122         if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1123                 return &platforms[STI_NAS];
1124
1125         if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1126                 return &platforms[DWL3150];
1127
1128         /* not found */
1129         return NULL;
1130 }
1131
1132 static void register_buttons(struct button_t *b)
1133 {
1134         for (; b->name; b++)
1135                 platform.button_mask |= b->gpio;
1136
1137         platform.button_mask &= ~gpiomask;
1138
1139         gpio_outen(platform.button_mask, 0);
1140         gpio_control(platform.button_mask, 0);
1141         platform.button_polarity = gpio_in() & platform.button_mask;
1142         gpio_intpolarity(platform.button_mask, platform.button_polarity);
1143         gpio_setintmask(platform.button_mask, platform.button_mask);
1144
1145         gpio_set_irqenable(1, button_handler);
1146 }
1147
1148 static void unregister_buttons(struct button_t *b)
1149 {
1150         gpio_setintmask(platform.button_mask, 0);
1151
1152         gpio_set_irqenable(0, button_handler);
1153 }
1154
1155
1156 static void add_msg(struct event_t *event, char *msg, int argv)
1157 {
1158         char *s;
1159
1160         if (argv)
1161                 return;
1162
1163         s = skb_put(event->skb, strlen(msg) + 1);
1164         strcpy(s, msg);
1165 }
1166
1167 static void hotplug_button(struct work_struct *work)
1168 {
1169         struct event_t *event = container_of(work, struct event_t, wq);
1170         char *s;
1171
1172         if (!uevent_sock)
1173                 return;
1174
1175         event->skb = alloc_skb(2048, GFP_KERNEL);
1176
1177         s = skb_put(event->skb, strlen(event->action) + 2);
1178         sprintf(s, "%s@", event->action);
1179         fill_event(event);
1180
1181         NETLINK_CB(event->skb).dst_group = 1;
1182         netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1183
1184         kfree(event);
1185 }
1186
1187
1188 static int fill_event (struct event_t *event)
1189 {
1190         static char buf[128];
1191
1192         add_msg(event, "HOME=/", 0);
1193         add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1194         add_msg(event, "SUBSYSTEM=button", 0);
1195         snprintf(buf, 128, "ACTION=%s", event->action);
1196         add_msg(event, buf, 0);
1197         snprintf(buf, 128, "BUTTON=%s", event->name);
1198         add_msg(event, buf, 0);
1199         snprintf(buf, 128, "SEEN=%ld", event->seen);
1200         add_msg(event, buf, 0);
1201         snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1202         add_msg(event, buf, 0);
1203
1204         return 0;
1205 }
1206
1207
1208 static irqreturn_t button_handler(int irq, void *dev_id)
1209 {
1210         struct button_t *b;
1211         u32 in, changed;
1212
1213         in = gpio_in() & platform.button_mask;
1214         gpio_intpolarity(platform.button_mask, in);
1215         changed = platform.button_polarity ^ in;
1216         platform.button_polarity = in;
1217
1218         changed &= ~gpio_outen(0, 0);
1219
1220         for (b = platform.buttons; b->name; b++) {
1221                 struct event_t *event;
1222
1223                 if (!(b->gpio & changed)) continue;
1224
1225                 b->pressed ^= 1;
1226
1227                 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1228                         event->seen = (jiffies - b->seen)/HZ;
1229                         event->name = b->name;
1230                         event->action = b->pressed ? "pressed" : "released";
1231                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1232                         schedule_work(&event->wq);
1233                 }
1234
1235                 b->seen = jiffies;
1236         }
1237         return IRQ_HANDLED;
1238 }
1239
1240 static void register_leds(struct led_t *l)
1241 {
1242         struct proc_dir_entry *p;
1243         u32 mask = 0;
1244         u32 oe_mask = 0;
1245         u32 val = 0;
1246
1247         leds = proc_mkdir("led", diag);
1248         if (!leds)
1249                 return;
1250
1251         for(; l->name; l++) {
1252                 if (l->gpio & gpiomask)
1253                         continue;
1254
1255                 if (l->gpio & GPIO_TYPE_EXTIF) {
1256                         l->state = 0;
1257                         set_led_extif(l);
1258                 } else {
1259                         if (l->polarity != INPUT) oe_mask |= l->gpio;
1260                         mask |= l->gpio;
1261                         val |= (l->polarity == NORMAL)?0:l->gpio;
1262                 }
1263
1264                 if (l->polarity == INPUT) continue;
1265
1266                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1267                         l->proc.type = PROC_LED;
1268                         l->proc.ptr = l;
1269                         p->data = (void *) &l->proc;
1270                         p->proc_fops = &diag_proc_fops;
1271                 }
1272         }
1273
1274         gpio_outen(mask, oe_mask);
1275         gpio_control(mask, 0);
1276         gpio_out(mask, val);
1277         gpio_setintmask(mask, 0);
1278 }
1279
1280 static void unregister_leds(struct led_t *l)
1281 {
1282         for(; l->name; l++)
1283                 remove_proc_entry(l->name, leds);
1284
1285         remove_proc_entry("led", diag);
1286 }
1287
1288 static void set_led_extif(struct led_t *led)
1289 {
1290         gpio_set_extif(led->gpio, led->state);
1291 }
1292
1293 static void led_flash(unsigned long dummy) {
1294         struct led_t *l;
1295         u32 mask = 0;
1296         u8 extif_blink = 0;
1297
1298         for (l = platform.leds; l->name; l++) {
1299                 if (l->flash) {
1300                         if (l->gpio & GPIO_TYPE_EXTIF) {
1301                                 extif_blink = 1;
1302                                 l->state = !l->state;
1303                                 set_led_extif(l);
1304                         } else {
1305                                 mask |= l->gpio;
1306                         }
1307                 }
1308         }
1309
1310         mask &= ~gpiomask;
1311         if (mask) {
1312                 u32 val = ~gpio_in();
1313
1314                 gpio_outen(mask, mask);
1315                 gpio_control(mask, 0);
1316                 gpio_out(mask, val);
1317         }
1318         if (mask || extif_blink) {
1319                 mod_timer(&led_timer, jiffies + FLASH_TIME);
1320         }
1321 }
1322
1323 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1324 {
1325         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1326         char *page;
1327         int len = 0;
1328
1329         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1330                 return -ENOBUFS;
1331
1332         if (dent->data != NULL) {
1333                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1334                 switch (handler->type) {
1335                         case PROC_LED: {
1336                                 struct led_t * led = (struct led_t *) handler->ptr;
1337                                 if (led->flash) {
1338                                         len = sprintf(page, "f\n");
1339                                 } else {
1340                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1341                                                 len = sprintf(page, "%d\n", led->state);
1342                                         } else {
1343                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1344                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
1345                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1346                                         }
1347                                 }
1348                                 break;
1349                         }
1350                         case PROC_MODEL:
1351                                 len = sprintf(page, "%s\n", platform.name);
1352                                 break;
1353                         case PROC_GPIOMASK:
1354                                 len = sprintf(page, "0x%04x\n", gpiomask);
1355                                 break;
1356                 }
1357         }
1358         len += 1;
1359
1360         if (*ppos < len) {
1361                 len = min_t(int, len - *ppos, count);
1362                 if (copy_to_user(buf, (page + *ppos), len)) {
1363                         kfree(page);
1364                         return -EFAULT;
1365                 }
1366                 *ppos += len;
1367         } else {
1368                 len = 0;
1369         }
1370
1371         kfree(page);
1372         return len;
1373 }
1374
1375
1376 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1377 {
1378         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1379         char *page;
1380         int ret = -EINVAL;
1381
1382         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1383                 return -ENOBUFS;
1384
1385         if (copy_from_user(page, buf, count)) {
1386                 kfree(page);
1387                 return -EINVAL;
1388         }
1389         page[count] = 0;
1390
1391         if (dent->data != NULL) {
1392                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1393                 switch (handler->type) {
1394                         case PROC_LED: {
1395                                 struct led_t *led = (struct led_t *) handler->ptr;
1396                                 int p = (led->polarity == NORMAL ? 0 : 1);
1397
1398                                 if (page[0] == 'f') {
1399                                         led->flash = 1;
1400                                         led_flash(0);
1401                                 } else {
1402                                         led->flash = 0;
1403                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1404                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1405                                                 set_led_extif(led);
1406                                         } else {
1407                                                 gpio_outen(led->gpio, led->gpio);
1408                                                 gpio_control(led->gpio, 0);
1409                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1410                                         }
1411                                 }
1412                                 break;
1413                         }
1414                         case PROC_GPIOMASK:
1415                                 gpiomask = simple_strtoul(page, NULL, 0);
1416
1417                                 if (platform.buttons) {
1418                                         unregister_buttons(platform.buttons);
1419                                         register_buttons(platform.buttons);
1420                                 }
1421
1422                                 if (platform.leds) {
1423                                         unregister_leds(platform.leds);
1424                                         register_leds(platform.leds);
1425                                 }
1426                                 break;
1427                 }
1428                 ret = count;
1429         }
1430
1431         kfree(page);
1432         return ret;
1433 }
1434
1435 static int __init diag_init(void)
1436 {
1437         static struct proc_dir_entry *p;
1438         static struct platform_t *detected;
1439
1440         detected = platform_detect();
1441         if (!detected) {
1442                 printk(MODULE_NAME ": Router model not detected.\n");
1443                 return -ENODEV;
1444         }
1445         memcpy(&platform, detected, sizeof(struct platform_t));
1446
1447         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1448         if (platform.platform_init != NULL) {
1449                 platform.platform_init();
1450         }
1451
1452         if (!(diag = proc_mkdir("diag", NULL))) {
1453                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1454                 return -EINVAL;
1455         }
1456
1457         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1458                 p->data = (void *) &proc_model;
1459                 p->proc_fops = &diag_proc_fops;
1460         }
1461
1462         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1463                 p->data = (void *) &proc_gpiomask;
1464                 p->proc_fops = &diag_proc_fops;
1465         }
1466
1467         if (platform.buttons)
1468                 register_buttons(platform.buttons);
1469
1470         if (platform.leds)
1471                 register_leds(platform.leds);
1472
1473         return 0;
1474 }
1475
1476 static void __exit diag_exit(void)
1477 {
1478         del_timer(&led_timer);
1479
1480         if (platform.buttons)
1481                 unregister_buttons(platform.buttons);
1482
1483         if (platform.leds)
1484                 unregister_leds(platform.leds);
1485
1486         remove_proc_entry("model", diag);
1487         remove_proc_entry("gpiomask", diag);
1488         remove_proc_entry("diag", NULL);
1489 }
1490
1491 module_init(diag_init);
1492 module_exit(diag_exit);
1493
1494 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1495 MODULE_LICENSE("GPL");