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