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