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