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