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