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