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