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