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