b0def22e46bd459888f937a252a966d2b9cd21c1
[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  *                    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         changed &= ~gpio_outen(0, 0);
560
561         for (b = platform.buttons; b->name; b++) { 
562                 struct event_t *event;
563
564                 if (!(b->gpio & changed)) continue;
565
566                 b->pressed ^= 1;
567
568                 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
569                         int i;
570                         char *scratch = event->buf;
571
572                         i = 0;
573                         event->argv[i++] = hotplug_path;
574                         event->argv[i++] = "button";
575                         event->argv[i] = 0;
576
577                         i = 0;
578                         event->envp[i++] = "HOME=/";
579                         event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
580                         event->envp[i++] = scratch;
581                         scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
582                         event->envp[i++] = scratch;
583                         scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
584                         event->envp[i++] = scratch;
585                         scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
586                         event->envp[i] = 0;
587
588                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
589                         schedule_work(&event->wq);
590                 }
591
592                 b->seen = jiffies;
593         }
594         return IRQ_HANDLED;
595 }
596
597 static void register_leds(struct led_t *l)
598 {
599         struct proc_dir_entry *p;
600         u32 mask = 0;
601         u32 val = 0;
602
603         leds = proc_mkdir("led", diag);
604         if (!leds) 
605                 return;
606
607         for(; l->name; l++) {
608                 if (l->gpio & gpiomask)
609                         continue;
610         
611                 if (l->gpio & GPIO_TYPE_EXTIF) {
612                         l->state = 0;
613                         set_led_extif(l);
614                 } else {
615                         mask |= l->gpio;
616                         val |= (l->polarity == NORMAL)?0:l->gpio;
617                 }
618
619                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
620                         l->proc.type = PROC_LED;
621                         l->proc.ptr = l;
622                         p->data = (void *) &l->proc;
623                         p->proc_fops = &diag_proc_fops;
624                 }
625         }
626
627         gpio_outen(mask, mask);
628         gpio_control(mask, 0);
629         gpio_out(mask, val);
630 }
631
632 static void unregister_leds(struct led_t *l)
633 {
634         for(; l->name; l++)
635                 remove_proc_entry(l->name, leds);
636
637         remove_proc_entry("led", diag);
638 }
639
640 static void set_led_extif(struct led_t *led)
641 {
642         gpio_set_extif(led->gpio, led->state);
643 }
644
645 static void led_flash(unsigned long dummy) {
646         struct led_t *l;
647         u32 mask = 0;
648         u8 extif_blink = 0;
649
650         for (l = platform.leds; l->name; l++) {
651                 if (l->flash) {
652                         if (l->gpio & GPIO_TYPE_EXTIF) {
653                                 extif_blink = 1;
654                                 l->state = !l->state;
655                                 set_led_extif(l);
656                         } else {
657                                 mask |= l->gpio;
658                         }
659                 }
660         }
661
662         mask &= ~gpiomask;
663         if (mask) {
664                 u32 val = ~gpio_in();
665
666                 gpio_outen(mask, mask);
667                 gpio_control(mask, 0);
668                 gpio_out(mask, val);
669         }
670         if (mask || extif_blink) {
671                 mod_timer(&led_timer, jiffies + FLASH_TIME);
672         }
673 }
674
675 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
676 {
677 #ifdef LINUX_2_4
678         struct inode *inode = file->f_dentry->d_inode;
679         struct proc_dir_entry *dent = inode->u.generic_ip;
680 #else
681         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
682 #endif
683         char *page;
684         int len = 0;
685         
686         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
687                 return -ENOBUFS;
688         
689         if (dent->data != NULL) {
690                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
691                 switch (handler->type) {
692                         case PROC_LED: {
693                                 struct led_t * led = (struct led_t *) handler->ptr;
694                                 if (led->flash) {
695                                         len = sprintf(page, "f\n");
696                                 } else {
697                                         if (led->gpio & GPIO_TYPE_EXTIF) {
698                                                 len = sprintf(page, "%d\n", led->state);
699                                         } else {
700                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
701                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
702                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
703                                         }
704                                 }
705                                 break;
706                         }
707                         case PROC_MODEL:
708                                 len = sprintf(page, "%s\n", platform.name);
709                                 break;
710                         case PROC_GPIOMASK:
711                                 len = sprintf(page, "0x%04x\n", gpiomask);
712                                 break;
713                 }
714         }
715         len += 1;
716
717         if (*ppos < len) {
718                 len = min_t(int, len - *ppos, count);
719                 if (copy_to_user(buf, (page + *ppos), len)) {
720                         kfree(page);
721                         return -EFAULT;
722                 }
723                 *ppos += len;
724         } else {
725                 len = 0;
726         }
727
728         return len;
729 }
730
731
732 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
733 {
734 #ifdef LINUX_2_4
735         struct inode *inode = file->f_dentry->d_inode;
736         struct proc_dir_entry *dent = inode->u.generic_ip;
737 #else
738         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
739 #endif
740         char *page;
741         int ret = -EINVAL;
742
743         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
744                 return -ENOBUFS;
745
746         if (copy_from_user(page, buf, count)) {
747                 kfree(page);
748                 return -EINVAL;
749         }
750         page[count] = 0;
751         
752         if (dent->data != NULL) {
753                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
754                 switch (handler->type) {
755                         case PROC_LED: {
756                                 struct led_t *led = (struct led_t *) handler->ptr;
757                                 int p = (led->polarity == NORMAL ? 0 : 1);
758                                 
759                                 if (page[0] == 'f') {
760                                         led->flash = 1;
761                                         led_flash(0);
762                                 } else {
763                                         led->flash = 0;
764                                         if (led->gpio & GPIO_TYPE_EXTIF) {
765                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
766                                                 set_led_extif(led);
767                                         } else {
768                                                 gpio_outen(led->gpio, led->gpio);
769                                                 gpio_control(led->gpio, 0);
770                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
771                                         }
772                                 }
773                                 break;
774                         }
775                         case PROC_GPIOMASK:
776                                 gpiomask = simple_strtoul(page, NULL, 0);
777
778                                 if (platform.buttons) {
779                                         unregister_buttons(platform.buttons);
780                                         register_buttons(platform.buttons);
781                                 }
782
783                                 if (platform.leds) {
784                                         unregister_leds(platform.leds);
785                                         register_leds(platform.leds);
786                                 }
787                                 break;
788                 }
789                 ret = count;
790         }
791
792         kfree(page);
793         return ret;
794 }
795
796 static int __init diag_init(void)
797 {
798         static struct proc_dir_entry *p;
799         static struct platform_t *detected;
800
801         detected = platform_detect();
802         if (!detected) {
803                 printk(MODULE_NAME ": Router model not detected.\n");
804                 return -ENODEV;
805         }
806         memcpy(&platform, detected, sizeof(struct platform_t));
807
808         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
809
810         if (!(diag = proc_mkdir("diag", NULL))) {
811                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
812                 return -EINVAL;
813         }
814
815         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
816                 p->data = (void *) &proc_model;
817                 p->proc_fops = &diag_proc_fops;
818         }
819
820         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
821                 p->data = (void *) &proc_gpiomask;
822                 p->proc_fops = &diag_proc_fops;
823         }
824
825         if (platform.buttons)
826                 register_buttons(platform.buttons);
827
828         if (platform.leds)
829                 register_leds(platform.leds);
830
831         return 0;
832 }
833
834 static void __exit diag_exit(void)
835 {
836
837         del_timer(&led_timer);
838
839         if (platform.buttons)
840                 unregister_buttons(platform.buttons);
841
842         if (platform.leds)
843                 unregister_leds(platform.leds);
844
845         remove_proc_entry("model", diag);
846         remove_proc_entry("gpiomask", diag);
847         remove_proc_entry("diag", NULL);
848 }
849
850 module_init(diag_init);
851 module_exit(diag_exit);
852
853 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
854 MODULE_LICENSE("GPL");