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