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