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