fix gpios for wzr-rs-g54, add support for the wzr-rs-g54hp
[librecmc/librecmc.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  *                    Felix Fietkau <nbd@openwrt.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * $Id$
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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
32 #include <linux/kobject.h>
33 #include <linux/workqueue.h>
34 #define hotplug_path uevent_helper
35 #else
36 #include <linux/tqueue.h>
37 #define INIT_WORK INIT_TQUEUE
38 #define schedule_work schedule_task
39 #define work_struct tq_struct
40 #endif
41
42 #include "gpio.h"
43 #include "diag.h"
44 #define getvar(str) (nvram_get(str)?:"")
45
46 static unsigned int gpiomask = 0;
47 module_param(gpiomask, int, 0644);
48
49 enum {
50         /* Linksys */
51         WAP54GV1,
52         WAP54GV3,
53         WRT54GV1,
54         WRT54G,
55         WRTSL54GS,
56         WRT54G3G,
57         
58         /* ASUS */
59         WLHDD,
60         WL300G,
61         WL500G,
62         WL500GD,
63         WL500GP,
64         ASUS_4702,
65         
66         /* Buffalo */
67         WBR2_G54,
68         WHR_G54S,
69         WHR_HP_G54,
70         WHR2_A54G54,
71         WLA2_G54L,
72         WZR_RS_G54,
73         WZR_RS_G54HP,
74         BUFFALO_UNKNOWN,
75         BUFFALO_UNKNOWN_4710,
76
77         /* Siemens */
78         SE505V1,
79         SE505V2,
80         
81         /* US Robotics */
82         USR5461,
83
84         /* Dell */
85         TM2300,
86
87         /* Motorola */
88         WE800G,
89         WR850GV1,
90         WR850GV2V3,
91
92         /* Belkin */
93         BELKIN_UNKNOWN,
94 };
95
96 static struct platform_t __initdata platforms[] = {
97         /* Linksys */
98         [WAP54GV1] = {
99                 .name           = "Linksys WAP54G V1",
100                 .buttons        = {
101                         { .name = "reset",      .gpio = 1 << 0 },
102                 },
103                 .leds           = { 
104                         { .name = "diag",       .gpio = 1 << 3 },
105                         { .name = "wlan",       .gpio = 1 << 4 },
106                 },
107         },
108         [WAP54GV3] = {
109                 .name           = "Linksys WAP54G V3",
110                 .buttons        = {
111                         /* FIXME: verify this */
112                         { .name = "reset",      .gpio = 1 << 7 },
113                         { .name = "ses",        .gpio = 1 << 0 },
114                 },
115                 .leds           = { 
116                         /* FIXME: diag? */
117                         { .name = "ses",        .gpio = 1 << 1 },
118                 },
119         },
120         [WRT54GV1] = {
121                 .name           = "Linksys WRT54G V1.x",
122                 .buttons        = {
123                         { .name = "reset",      .gpio = 1 << 6 },
124                 },
125                 .leds           = { 
126                         { .name = "diag",       .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
127                         { .name = "dmz",        .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
128                 },
129         },
130         [WRT54G] = {
131                 .name           = "Linksys WRT54G/GS/GL",
132                 .buttons        = {
133                         { .name = "reset",      .gpio = 1 << 6 },
134                         { .name = "ses",        .gpio = 1 << 4 },
135                 },
136                 .leds           = {
137                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
138                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
139                         { .name = "ses_white",  .gpio = 1 << 2, .polarity = REVERSE },
140                         { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
141                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
142                 },
143         },
144         [WRTSL54GS] = {
145                 .name           = "Linksys WRTSL54GS",
146                 .buttons        = {
147                         { .name = "reset",      .gpio = 1 << 6 },
148                         { .name = "ses",        .gpio = 1 << 4 },
149                 },
150                 .leds           = {
151                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
152                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },
153                         { .name = "ses_white",  .gpio = 1 << 5, .polarity = REVERSE },
154                         { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
155                 },
156         },
157         [WRT54G3G] = {
158                 .name           = "Linksys WRT54G3G",
159                 .buttons        = {
160                         { .name = "reset",      .gpio = 1 << 6 },
161                         { .name = "3g",         .gpio = 1 << 4 },
162                 },
163                 .leds           = {
164                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
165                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
166                         { .name = "3g_green",   .gpio = 1 << 2, .polarity = NORMAL },
167                         { .name = "3g_blue",    .gpio = 1 << 3, .polarity = NORMAL },
168                         { .name = "3g_blink",   .gpio = 1 << 5, .polarity = NORMAL },
169                 },
170         },
171         /* Asus */
172         [WLHDD] = {
173                 .name           = "ASUS WL-HDD",
174                 .buttons        = {
175                         { .name = "reset",      .gpio = 1 << 6 },
176                 },
177                 .leds           = {
178                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
179                 },
180         },
181         [WL300G] = {
182                 .name           = "ASUS WL-300g",
183                 .buttons        = {
184                         { .name = "reset",      .gpio = 1 << 6 },
185                 },
186                 .leds           = {
187                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
188                 },
189         },
190         [WL500G] = {
191                 .name           = "ASUS WL-500g",
192                 .buttons        = {
193                         { .name = "reset",      .gpio = 1 << 6 },
194                 },
195                 .leds           = {
196                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
197                 },
198         },
199         [WL500GD] = {
200                 .name           = "ASUS WL-500g Deluxe",
201                 .buttons        = {
202                         { .name = "reset",      .gpio = 1 << 6 },
203                 },
204                 .leds           = {
205                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
206                 },
207         },
208         [WL500GP] = {
209                 .name           = "ASUS WL-500g Premium",
210                 .buttons        = {
211                         { .name = "reset",      .gpio = 1 << 0 },
212                         { .name = "ses",        .gpio = 1 << 4 },
213                 },
214                 .leds           = {
215                         { .name = "power",      .gpio = 1 << 1, .polarity = REVERSE },
216                 },
217         },
218         [ASUS_4702] = {
219                 .name           = "ASUS (unknown, BCM4702)",
220                 .buttons        = {
221                         { .name = "reset",      .gpio = 1 << 6 },
222                 },
223                 .leds           = {
224                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
225                 },
226         },
227         /* Buffalo */
228         [WHR_G54S] = {
229                 .name           = "Buffalo WHR-G54S",
230                 .buttons        = {
231                         { .name = "reset",      .gpio = 1 << 4 },
232                         { .name = "bridge",     .gpio = 1 << 5 },
233                         { .name = "ses",        .gpio = 1 << 0 },
234                 },
235                 .leds           = {
236                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
237                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
238                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
239                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
240                 },
241         },
242         [WBR2_G54] = {
243                 .name           = "Buffalo WBR2-G54",
244                 /* FIXME: verify */
245                 .buttons        = {
246                         { .name = "reset",      .gpio = 1 << 7 },
247                 },
248                 .leds           = {
249                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
250                 },
251         },
252         [WHR_HP_G54] = {
253                 .name           = "Buffalo WHR-HP-G54",
254                 .buttons        = {
255                         { .name = "reset",      .gpio = 1 << 4 },
256                         { .name = "bridge",     .gpio = 1 << 5 },
257                         { .name = "ses",        .gpio = 1 << 0 },
258                 },
259                 .leds           = {
260                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
261                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
262                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
263                 },
264         },
265         [WHR2_A54G54] = {
266                 .name           = "Buffalo WHR2-A54G54",
267                 .buttons        = {
268                         { .name = "reset",      .gpio = 1 << 4 },
269                 },
270                 .leds           = {
271                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
272                 },
273         },
274         [WLA2_G54L] = {
275                 .name           = "Buffalo WLA2-G54L",
276                 /* FIXME: verify */
277                 .buttons        = {
278                         { .name = "reset",      .gpio = 1 << 7 },
279                 },
280                 .leds           = {
281                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
282                 },
283         },
284         [WZR_RS_G54] = {
285                 .name           = "Buffalo WZR-RS-G54",
286                 .buttons        = {
287                         { .name = "ses",        .gpio = 1 << 0 },
288                         { .name = "reset",      .gpio = 1 << 4 },
289                 },
290                 .leds           = {
291                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
292                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
293                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
294                 },
295         },
296         [WZR_RS_G54HP] = {
297                 .name           = "Buffalo WZR-RS-G54HP",
298                 .buttons        = {
299                         { .name = "ses",        .gpio = 1 << 0 },
300                         { .name = "reset",      .gpio = 1 << 4 },
301                 },
302                 .leds           = {
303                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
304                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
305                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
306                 },
307         },
308         [BUFFALO_UNKNOWN] = {
309                 .name           = "Buffalo (unknown)",
310                 .buttons        = {
311                         { .name = "reset",      .gpio = 1 << 7 },
312                 },
313                 .leds           = {
314                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
315                 },
316         },
317         [BUFFALO_UNKNOWN_4710] = {
318                 .name           = "Buffalo (unknown, BCM4710)",
319                 .buttons        = {
320                         { .name = "reset",      .gpio = 1 << 4 },
321                 },
322                 .leds           = {
323                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
324                 },
325         },
326         /* Siemens */
327         [SE505V1] = {
328                 .name           = "Siemens SE505 V1",
329                 .buttons        = {
330                         /* No usable buttons */
331                 },
332                 .leds           = {
333                         { .name = "dmz",        .gpio = 1 << 4, .polarity = REVERSE },
334                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
335                 },
336         },
337         [SE505V2] = {
338                 .name           = "Siemens SE505 V2",
339                 .buttons        = {
340                         /* No usable buttons */
341                 },
342                 .leds           = {
343                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
344                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },
345                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
346                 },
347         },
348         /* US Robotics */
349         [USR5461] = {
350                 .name           = "U.S. Robotics USR5461",
351                 .buttons        = {
352                         /* No usable buttons */
353                 },
354                 .leds           = {
355                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
356                         { .name = "printer",    .gpio = 1 << 1, .polarity = REVERSE },
357                 },
358         },
359         /* Dell */
360         [TM2300] = {
361                 .name           = "Dell TrueMobile 2300",
362                 .buttons        = {
363                         { .name = "reset",      .gpio = 1 << 0 },
364                 },
365                 .leds           = {
366                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
367                 },
368         },
369         /* Motorola */
370         [WE800G] = {
371                 .name           = "Motorola WE800G",
372                 .buttons        = {
373                         { .name = "reset",      .gpio = 1 << 0 },
374                 },
375                 .leds           = {
376                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
377                         { .name = "diag",       .gpio = 1 << 2, .polarity = REVERSE },
378                         { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
379                 },
380         },
381         [WR850GV1] = {
382                 .name           = "Motorola WR850G V1",
383                 .buttons        = {
384                         { .name = "reset",      .gpio = 1 << 0 },
385                 },
386                 .leds           = {
387                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
388                         { .name = "diag",       .gpio = 1 << 3, .polarity = REVERSE },
389                         { .name = "dmz",        .gpio = 1 << 6, .polarity = NORMAL },
390                         { .name = "wlan_red",   .gpio = 1 << 5, .polarity = REVERSE },
391                         { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
392                 },
393         },
394         [WR850GV2V3] = {
395                 .name           = "Motorola WR850G V2/V3",
396                 .buttons        = {
397                         { .name = "reset",      .gpio = 1 << 5 },
398                 },
399                 .leds           = {
400                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
401                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
402                         { .name = "dmz",        .gpio = 1 << 6, .polarity = REVERSE },
403                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
404                 },
405         },
406         /* Belkin */
407         [BELKIN_UNKNOWN] = {
408                 .name           = "Belkin (unknown)",
409                 /* FIXME: verify & add detection */
410                 .buttons        = {
411                         { .name = "reset",      .gpio = 1 << 7 },
412                 },
413                 .leds           = {
414                         { .name = "power",      .gpio = 1 << 5, .polarity = NORMAL },
415                         { .name = "wlan",       .gpio = 1 << 3, .polarity = NORMAL },
416                         { .name = "connected",  .gpio = 1 << 0, .polarity = NORMAL },
417                 },
418         },
419 };
420
421 static struct platform_t __init *platform_detect(void)
422 {
423         char *boardnum, *boardtype, *buf;
424
425         boardnum = getvar("boardnum");
426         boardtype = getvar("boardtype");
427         if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
428                 /* CFE based - newer hardware */
429                 if (!strcmp(boardnum, "42")) { /* Linksys */
430                         if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
431                                 return &platforms[WRT54G3G];
432
433                         if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
434                                 return &platforms[WRTSL54GS];
435                         
436                         /* default to WRT54G */
437                         return &platforms[WRT54G];
438                 }
439                 
440                 if (!strcmp(boardnum, "45")) { /* ASUS */
441                         if (!strcmp(boardtype,"0x042f"))
442                                 return &platforms[WL500GP];
443                         else
444                                 return &platforms[WL500GD];
445                 }
446                 
447                 if (!strcmp(boardnum, "10496"))
448                         return &platforms[USR5461];
449         } else { /* PMON based - old stuff */
450                 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
451                         (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
452                         if (!strncmp(getvar("ModelId"),"WE800G", 6))
453                                 return &platforms[WE800G];
454                         else
455                                 return &platforms[WR850GV1];
456                 }
457                 if (!strncmp(boardtype, "bcm94710dev", 11)) {
458                         if (!strcmp(boardnum, "42"))
459                                 return &platforms[WRT54GV1];
460                         if (simple_strtoul(boardnum, NULL, 0) == 2)
461                                 return &platforms[WAP54GV1];
462                 }
463                 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
464                         return &platforms[WL500G];
465                 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
466                         /* Either WL-300g or WL-HDD, do more extensive checks */
467                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
468                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
469                                 return &platforms[WLHDD];
470                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
471                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
472                                 return &platforms[WL300G];
473                 }
474
475                 /* unknown asus stuff, probably bcm4702 */
476                 if (!strncmp(boardnum, "asusX", 5))
477                         return &platforms[ASUS_4702];
478         }
479
480         if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
481                 /* Buffalo hardware, check id for specific hardware matches */
482                 if (!strcmp(buf, "29bb0332"))
483                         return &platforms[WBR2_G54];
484                 if (!strcmp(buf, "29129"))
485                         return &platforms[WLA2_G54L];
486                 if (!strcmp(buf, "30189"))
487                         return &platforms[WHR_HP_G54];
488                 if (!strcmp(buf, "30182"))
489                         return &platforms[WHR_G54S];
490                 if (!strcmp(buf, "290441dd"))
491                         return &platforms[WHR2_A54G54];
492                 if (!strcmp(buf, "30083"))
493                         return &platforms[WZR_RS_G54];
494                 if (!strcmp(buf, "30103"))
495                         return &platforms[WZR_RS_G54HP];
496         }
497
498         if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
499                 if (!strncmp(boardtype, "bcm94710ap", 10))
500                         return &platforms[BUFFALO_UNKNOWN_4710];
501                 else
502                         return &platforms[BUFFALO_UNKNOWN];
503         }
504
505
506         if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
507                 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
508
509                 return &platforms[WR850GV2V3];
510         }
511
512         /* not found */
513         return NULL;
514 }
515
516 static void register_buttons(struct button_t *b)
517 {
518         for (; b->name; b++)
519                 platform.button_mask |= b->gpio;
520
521         platform.button_mask &= ~gpiomask;
522
523         gpio_outen(platform.button_mask, 0);
524         gpio_control(platform.button_mask, 0);
525         platform.button_polarity = gpio_in() & platform.button_mask;
526         gpio_intpolarity(platform.button_mask, platform.button_polarity);
527         gpio_intmask(platform.button_mask, platform.button_mask);
528
529         gpio_set_irqenable(1, button_handler);
530 }
531
532 static void unregister_buttons(struct button_t *b)
533 {
534         gpio_intmask(platform.button_mask, 0);
535
536         gpio_set_irqenable(0, button_handler);
537 }
538
539 static void hotplug_button(struct event_t *event)
540 {
541 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
542         call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
543 #else
544         call_usermodehelper (event->argv[0], event->argv, event->envp);
545 #endif
546         kfree(event);
547 }
548
549 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
550 {
551         struct button_t *b;
552         u32 in, changed;
553
554         in = gpio_in() & platform.button_mask;
555         gpio_intpolarity(platform.button_mask, in);
556         changed = platform.button_polarity ^ in;
557         platform.button_polarity = in;
558
559         for (b = platform.buttons; b->name; b++) { 
560                 struct event_t *event;
561
562                 if (!(b->gpio & changed)) continue;
563
564                 b->pressed ^= 1;
565
566                 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
567                         int i;
568                         char *scratch = event->buf;
569
570                         i = 0;
571                         event->argv[i++] = hotplug_path;
572                         event->argv[i++] = "button";
573                         event->argv[i] = 0;
574
575                         i = 0;
576                         event->envp[i++] = "HOME=/";
577                         event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
578                         event->envp[i++] = scratch;
579                         scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
580                         event->envp[i++] = scratch;
581                         scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
582                         event->envp[i++] = scratch;
583                         scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
584                         event->envp[i] = 0;
585
586                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
587                         schedule_work(&event->wq);
588                 }
589
590                 b->seen = jiffies;
591         }
592         return IRQ_HANDLED;
593 }
594
595 static void register_leds(struct led_t *l)
596 {
597         struct proc_dir_entry *p;
598         u32 mask = 0;
599         u32 val = 0;
600
601         leds = proc_mkdir("led", diag);
602         if (!leds) 
603                 return;
604
605         for(; l->name; l++) {
606                 if (l->gpio & gpiomask)
607                         continue;
608         
609                 if (l->gpio & GPIO_TYPE_EXTIF) {
610                         l->state = 0;
611                         set_led_extif(l);
612                 } else {
613                         mask |= l->gpio;
614                         val |= (l->polarity == NORMAL)?0:l->gpio;
615                 }
616
617                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
618                         l->proc.type = PROC_LED;
619                         l->proc.ptr = l;
620                         p->data = (void *) &l->proc;
621                         p->proc_fops = &diag_proc_fops;
622                 }
623         }
624
625         gpio_outen(mask, mask);
626         gpio_control(mask, 0);
627         gpio_out(mask, val);
628 }
629
630 static void unregister_leds(struct led_t *l)
631 {
632         for(; l->name; l++)
633                 remove_proc_entry(l->name, leds);
634
635         remove_proc_entry("led", diag);
636 }
637
638 static void set_led_extif(struct led_t *led)
639 {
640         gpio_set_extif(led->gpio, led->state);
641 }
642
643 static void led_flash(unsigned long dummy) {
644         struct led_t *l;
645         u32 mask = 0;
646         u8 extif_blink = 0;
647
648         for (l = platform.leds; l->name; l++) {
649                 if (l->flash) {
650                         if (l->gpio & GPIO_TYPE_EXTIF) {
651                                 extif_blink = 1;
652                                 l->state = !l->state;
653                                 set_led_extif(l);
654                         } else {
655                                 mask |= l->gpio;
656                         }
657                 }
658         }
659
660         mask &= ~gpiomask;
661         if (mask) {
662                 u32 val = ~gpio_in();
663
664                 gpio_outen(mask, mask);
665                 gpio_control(mask, 0);
666                 gpio_out(mask, val);
667         }
668         if (mask || extif_blink) {
669                 mod_timer(&led_timer, jiffies + FLASH_TIME);
670         }
671 }
672
673 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
674 {
675 #ifdef LINUX_2_4
676         struct inode *inode = file->f_dentry->d_inode;
677         struct proc_dir_entry *dent = inode->u.generic_ip;
678 #else
679         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
680 #endif
681         char *page;
682         int len = 0;
683         
684         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
685                 return -ENOBUFS;
686         
687         if (dent->data != NULL) {
688                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
689                 switch (handler->type) {
690                         case PROC_LED: {
691                                 struct led_t * led = (struct led_t *) handler->ptr;
692                                 if (led->flash) {
693                                         len = sprintf(page, "f\n");
694                                 } else {
695                                         if (led->gpio & GPIO_TYPE_EXTIF) {
696                                                 len = sprintf(page, "%d\n", led->state);
697                                         } else {
698                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
699                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
700                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
701                                         }
702                                 }
703                                 break;
704                         }
705                         case PROC_MODEL:
706                                 len = sprintf(page, "%s\n", platform.name);
707                                 break;
708                         case PROC_GPIOMASK:
709                                 len = sprintf(page, "0x%04x\n", gpiomask);
710                                 break;
711                 }
712         }
713         len += 1;
714
715         if (*ppos < len) {
716                 len = min_t(int, len - *ppos, count);
717                 if (copy_to_user(buf, (page + *ppos), len)) {
718                         kfree(page);
719                         return -EFAULT;
720                 }
721                 *ppos += len;
722         } else {
723                 len = 0;
724         }
725
726         return len;
727 }
728
729
730 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
731 {
732 #ifdef LINUX_2_4
733         struct inode *inode = file->f_dentry->d_inode;
734         struct proc_dir_entry *dent = inode->u.generic_ip;
735 #else
736         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
737 #endif
738         char *page;
739         int ret = -EINVAL;
740
741         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
742                 return -ENOBUFS;
743
744         if (copy_from_user(page, buf, count)) {
745                 kfree(page);
746                 return -EINVAL;
747         }
748         page[count] = 0;
749         
750         if (dent->data != NULL) {
751                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
752                 switch (handler->type) {
753                         case PROC_LED: {
754                                 struct led_t *led = (struct led_t *) handler->ptr;
755                                 int p = (led->polarity == NORMAL ? 0 : 1);
756                                 
757                                 if (page[0] == 'f') {
758                                         led->flash = 1;
759                                         led_flash(0);
760                                 } else {
761                                         led->flash = 0;
762                                         if (led->gpio & GPIO_TYPE_EXTIF) {
763                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
764                                                 set_led_extif(led);
765                                         } else {
766                                                 gpio_outen(led->gpio, led->gpio);
767                                                 gpio_control(led->gpio, 0);
768                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
769                                         }
770                                 }
771                                 break;
772                         }
773                         case PROC_GPIOMASK:
774                                 gpiomask = simple_strtoul(page, NULL, 0);
775
776                                 if (platform.buttons) {
777                                         unregister_buttons(platform.buttons);
778                                         register_buttons(platform.buttons);
779                                 }
780
781                                 if (platform.leds) {
782                                         unregister_leds(platform.leds);
783                                         register_leds(platform.leds);
784                                 }
785                                 break;
786                 }
787                 ret = count;
788         }
789
790         kfree(page);
791         return ret;
792 }
793
794 static int __init diag_init(void)
795 {
796         static struct proc_dir_entry *p;
797         static struct platform_t *detected;
798
799         detected = platform_detect();
800         if (!detected) {
801                 printk(MODULE_NAME ": Router model not detected.\n");
802                 return -ENODEV;
803         }
804         memcpy(&platform, detected, sizeof(struct platform_t));
805
806         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
807
808         if (!(diag = proc_mkdir("diag", NULL))) {
809                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
810                 return -EINVAL;
811         }
812
813         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
814                 p->data = (void *) &proc_model;
815                 p->proc_fops = &diag_proc_fops;
816         }
817
818         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
819                 p->data = (void *) &proc_gpiomask;
820                 p->proc_fops = &diag_proc_fops;
821         }
822
823         if (platform.buttons)
824                 register_buttons(platform.buttons);
825
826         if (platform.leds)
827                 register_leds(platform.leds);
828
829         return 0;
830 }
831
832 static void __exit diag_exit(void)
833 {
834
835         del_timer(&led_timer);
836
837         if (platform.buttons)
838                 unregister_buttons(platform.buttons);
839
840         if (platform.leds)
841                 unregister_leds(platform.leds);
842
843         remove_proc_entry("model", diag);
844         remove_proc_entry("gpiomask", diag);
845         remove_proc_entry("diag", NULL);
846 }
847
848 module_init(diag_init);
849 module_exit(diag_exit);
850
851 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
852 MODULE_LICENSE("GPL");