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