forward port diag changes from whiterussian, add extra abstraction for the gpio stuff
[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 << 7, .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"))
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 (!strncmp(boardtype, "bcm94710dev", 11)) {
425                         if (!strcmp(boardnum, "42"))
426                                 return &platforms[WRT54GV1];
427                         if (simple_strtoul(boardnum, NULL, 0) == 2)
428                                 return &platforms[WAP54GV1];
429                 }
430                 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
431                         return &platforms[WL500G];
432                 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
433                         /* Either WL-300g or WL-HDD, do more extensive checks */
434                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
435                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
436                                 return &platforms[WLHDD];
437                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
438                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
439                                 return &platforms[WL300G];
440                 }
441
442                 /* unknown asus stuff, probably bcm4702 */
443                 if (!strncmp(boardnum, "asusX", 5))
444                         return &platforms[ASUS_4702];
445
446                 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
447                         (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
448                         if (!strncmp(getvar("ModelId"),"WE800G", 6))
449                                 return &platforms[WE800G];
450                         else
451                                 return &platforms[WR850GV1];
452                 }
453         }
454
455         if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
456                 /* Buffalo hardware, check id for specific hardware matches */
457                 if (!strcmp(buf, "29bb0332"))
458                         return &platforms[WBR2_G54];
459                 if (!strcmp(buf, "29129"))
460                         return &platforms[WLA2_G54L];
461                 if (!strcmp(buf, "30189"))
462                         return &platforms[WHR_HP_G54];
463                 if (!strcmp(buf, "30182"))
464                         return &platforms[WHR_G54S];
465                 if (!strcmp(buf, "290441dd"))
466                         return &platforms[WHR2_A54G54];
467         }
468
469         if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
470                 if (!strncmp(boardtype, "bcm94710ap", 10))
471                         return &platforms[BUFFALO_UNKNOWN_4710];
472                 else
473                         return &platforms[BUFFALO_UNKNOWN];
474         }
475
476
477         if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
478                 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
479
480                 return &platforms[WR850GV2V3];
481         }
482
483         /* not found */
484         return NULL;
485 }
486
487 static void register_buttons(struct button_t *b)
488 {
489         for (; b->name; b++)
490                 platform.button_mask |= b->gpio;
491
492         platform.button_mask &= ~gpiomask;
493
494         gpio_outen(platform.button_mask, 0);
495         gpio_control(platform.button_mask, 0);
496         platform.button_polarity = gpio_in() & platform.button_mask;
497         gpio_intpolarity(platform.button_mask, platform.button_polarity);
498         gpio_intmask(platform.button_mask, platform.button_mask);
499
500         gpio_set_irqenable(1, button_handler);
501 }
502
503 static void unregister_buttons(struct button_t *b)
504 {
505         gpio_intmask(platform.button_mask, 0);
506
507         gpio_set_irqenable(0, button_handler);
508 }
509
510 static void hotplug_button(struct event_t *event)
511 {
512 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
513         call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
514 #else
515         call_usermodehelper (event->argv[0], event->argv, event->envp);
516 #endif
517         kfree(event);
518 }
519
520 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
521 {
522         struct button_t *b;
523         u32 in, changed;
524
525         in = gpio_in() & platform.button_mask;
526         gpio_intpolarity(platform.button_mask, in);
527         changed = platform.button_polarity ^ in;
528         platform.button_polarity = in;
529
530         for (b = platform.buttons; b->name; b++) { 
531                 struct event_t *event;
532
533                 if (!(b->gpio & changed)) continue;
534
535                 b->pressed ^= 1;
536
537                 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
538                         int i;
539                         char *scratch = event->buf;
540
541                         i = 0;
542                         event->argv[i++] = hotplug_path;
543                         event->argv[i++] = "button";
544                         event->argv[i] = 0;
545
546                         i = 0;
547                         event->envp[i++] = "HOME=/";
548                         event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
549                         event->envp[i++] = scratch;
550                         scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
551                         event->envp[i++] = scratch;
552                         scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
553                         event->envp[i++] = scratch;
554                         scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
555                         event->envp[i] = 0;
556
557                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
558                         schedule_work(&event->wq);
559                 }
560
561                 b->seen = jiffies;
562         }
563         return IRQ_HANDLED;
564 }
565
566 static void register_leds(struct led_t *l)
567 {
568         struct proc_dir_entry *p;
569         u32 mask = 0;
570         u32 val = 0;
571
572         leds = proc_mkdir("led", diag);
573         if (!leds) 
574                 return;
575
576         for(; l->name; l++) {
577                 if (l->gpio & gpiomask)
578                         continue;
579         
580                 if (l->gpio & GPIO_TYPE_EXTIF) {
581                         l->state = 0;
582                         set_led_extif(l);
583                 } else {
584                         mask |= l->gpio;
585                         val |= (l->polarity == NORMAL)?0:l->gpio;
586                 }
587
588                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
589                         l->proc.type = PROC_LED;
590                         l->proc.ptr = l;
591                         p->data = (void *) &l->proc;
592                         p->proc_fops = &diag_proc_fops;
593                 }
594         }
595
596         gpio_outen(mask, mask);
597         gpio_control(mask, 0);
598         gpio_out(mask, val);
599 }
600
601 static void unregister_leds(struct led_t *l)
602 {
603         for(; l->name; l++)
604                 remove_proc_entry(l->name, leds);
605
606         remove_proc_entry("led", diag);
607 }
608
609 static void set_led_extif(struct led_t *led)
610 {
611         gpio_set_extif(led->gpio, led->state);
612 }
613
614 static void led_flash(unsigned long dummy) {
615         struct led_t *l;
616         u32 mask = 0;
617         u8 extif_blink = 0;
618
619         for (l = platform.leds; l->name; l++) {
620                 if (l->flash) {
621                         if (l->gpio & GPIO_TYPE_EXTIF) {
622                                 extif_blink = 1;
623                                 l->state = !l->state;
624                                 set_led_extif(l);
625                         } else {
626                                 mask |= l->gpio;
627                         }
628                 }
629         }
630
631         mask &= ~gpiomask;
632         if (mask) {
633                 u32 val = ~gpio_in();
634
635                 gpio_outen(mask, mask);
636                 gpio_control(mask, 0);
637                 gpio_out(mask, val);
638         }
639         if (mask || extif_blink) {
640                 mod_timer(&led_timer, jiffies + FLASH_TIME);
641         }
642 }
643
644 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
645 {
646 #ifdef LINUX_2_4
647         struct inode *inode = file->f_dentry->d_inode;
648         struct proc_dir_entry *dent = inode->u.generic_ip;
649 #else
650         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
651 #endif
652         char *page;
653         int len = 0;
654         
655         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
656                 return -ENOBUFS;
657         
658         if (dent->data != NULL) {
659                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
660                 switch (handler->type) {
661                         case PROC_LED: {
662                                 struct led_t * led = (struct led_t *) handler->ptr;
663                                 if (led->flash) {
664                                         len = sprintf(page, "f\n");
665                                 } else {
666                                         if (led->gpio & GPIO_TYPE_EXTIF) {
667                                                 len = sprintf(page, "%d\n", led->state);
668                                         } else {
669                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
670                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
671                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
672                                         }
673                                 }
674                                 break;
675                         }
676                         case PROC_MODEL:
677                                 len = sprintf(page, "%s\n", platform.name);
678                                 break;
679                         case PROC_GPIOMASK:
680                                 len = sprintf(page, "0x%04x\n", gpiomask);
681                                 break;
682                 }
683         }
684         len += 1;
685
686         if (*ppos < len) {
687                 len = min_t(int, len - *ppos, count);
688                 if (copy_to_user(buf, (page + *ppos), len)) {
689                         kfree(page);
690                         return -EFAULT;
691                 }
692                 *ppos += len;
693         } else {
694                 len = 0;
695         }
696
697         return len;
698 }
699
700
701 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
702 {
703 #ifdef LINUX_2_4
704         struct inode *inode = file->f_dentry->d_inode;
705         struct proc_dir_entry *dent = inode->u.generic_ip;
706 #else
707         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
708 #endif
709         char *page;
710         int ret = -EINVAL;
711
712         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
713                 return -ENOBUFS;
714
715         if (copy_from_user(page, buf, count)) {
716                 kfree(page);
717                 return -EINVAL;
718         }
719         page[count] = 0;
720         
721         if (dent->data != NULL) {
722                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
723                 switch (handler->type) {
724                         case PROC_LED: {
725                                 struct led_t *led = (struct led_t *) handler->ptr;
726                                 int p = (led->polarity == NORMAL ? 0 : 1);
727                                 
728                                 if (page[0] == 'f') {
729                                         led->flash = 1;
730                                         led_flash(0);
731                                 } else {
732                                         led->flash = 0;
733                                         if (led->gpio & GPIO_TYPE_EXTIF) {
734                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
735                                                 set_led_extif(led);
736                                         } else {
737                                                 gpio_outen(led->gpio, led->gpio);
738                                                 gpio_control(led->gpio, 0);
739                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
740                                         }
741                                 }
742                                 break;
743                         }
744                         case PROC_GPIOMASK:
745                                 gpiomask = simple_strtoul(page, NULL, 0);
746
747                                 if (platform.buttons) {
748                                         unregister_buttons(platform.buttons);
749                                         register_buttons(platform.buttons);
750                                 }
751
752                                 if (platform.leds) {
753                                         unregister_leds(platform.leds);
754                                         register_leds(platform.leds);
755                                 }
756                                 break;
757                 }
758                 ret = count;
759         }
760
761         kfree(page);
762         return ret;
763 }
764
765 static int __init diag_init(void)
766 {
767         static struct proc_dir_entry *p;
768         static struct platform_t *detected;
769
770         detected = platform_detect();
771         if (!detected) {
772                 printk(MODULE_NAME ": Router model not detected.\n");
773                 return -ENODEV;
774         }
775         memcpy(&platform, detected, sizeof(struct platform_t));
776
777         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
778
779         if (!(diag = proc_mkdir("diag", NULL))) {
780                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
781                 return -EINVAL;
782         }
783
784         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
785                 p->data = (void *) &proc_model;
786                 p->proc_fops = &diag_proc_fops;
787         }
788
789         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
790                 p->data = (void *) &proc_gpiomask;
791                 p->proc_fops = &diag_proc_fops;
792         }
793
794         if (platform.buttons)
795                 register_buttons(platform.buttons);
796
797         if (platform.leds)
798                 register_leds(platform.leds);
799
800         return 0;
801 }
802
803 static void __exit diag_exit(void)
804 {
805
806         del_timer(&led_timer);
807
808         if (platform.buttons)
809                 unregister_buttons(platform.buttons);
810
811         if (platform.leds)
812                 unregister_leds(platform.leds);
813
814         remove_proc_entry("model", diag);
815         remove_proc_entry("gpiomask", diag);
816         remove_proc_entry("diag", NULL);
817 }
818
819 module_init(diag_init);
820 module_exit(diag_exit);
821
822 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
823 MODULE_LICENSE("GPL");