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