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