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