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