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