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