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