2 * diag.c - GPIO interface driver for Broadcom boards
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>
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.
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.
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.
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 #include <linux/workqueue.h>
31 #include <linux/skbuff.h>
32 #include <linux/netlink.h>
33 #include <linux/kobject.h>
35 extern u64 uevent_next_seqnum(void);
39 #define getvar(str) (nvram_get(str)?:"")
41 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
42 static int fill_event(struct event_t *);
43 static unsigned int gpiomask = 0;
44 module_param(gpiomask, int, 0644);
135 /* Western Digital */
151 static void __init bcm4780_init(void) {
154 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
155 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
156 gpio_outen(pin, pin);
157 gpio_control(pin, 0);
160 /* Wait 5s, so the HDD can spin up */
161 set_current_state(TASK_INTERRUPTIBLE);
162 schedule_timeout(HZ * 5);
165 static void __init NetCenter_init(void) {
166 /* unset pin 6 (+12V) */
168 gpio_outen(pin, pin);
169 gpio_control(pin, 0);
171 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
173 gpio_outen(pin, pin);
174 gpio_control(pin, 0);
176 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
180 static void __init bcm57xx_init(void) {
183 /* FIXME: switch comes up, but port mappings/vlans not right */
184 gpio_outen(pin, pin);
185 gpio_control(pin, 0);
189 static struct platform_t __initdata platforms[] = {
192 .name = "Linksys WAP54G V1",
194 { .name = "reset", .gpio = 1 << 0 },
197 { .name = "diag", .gpio = 1 << 3 },
198 { .name = "wlan", .gpio = 1 << 4 },
202 .name = "Linksys WAP54G V2",
204 { .name = "reset", .gpio = 1 << 0 },
207 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
208 /* GPIO 6 is b44 (eth0, LAN) PHY power */
212 .name = "Linksys WAP54G V3",
214 /* FIXME: verify this */
215 { .name = "reset", .gpio = 1 << 7 },
216 { .name = "ses", .gpio = 1 << 0 },
220 { .name = "ses", .gpio = 1 << 1 },
224 .name = "Linksys WRT54G V1.x",
226 { .name = "reset", .gpio = 1 << 6 },
229 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
230 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
234 .name = "Linksys WRT54G/GS/GL",
236 { .name = "reset", .gpio = 1 << 6 },
237 { .name = "ses", .gpio = 1 << 4 },
240 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
241 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
242 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
243 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
244 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
248 .name = "Linksys WRTSL54GS",
250 { .name = "reset", .gpio = 1 << 6 },
251 { .name = "ses", .gpio = 1 << 4 },
254 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
255 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
256 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
257 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
261 .name = "Linksys WRT54G3G",
263 { .name = "reset", .gpio = 1 << 6 },
264 { .name = "3g", .gpio = 1 << 4 },
267 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
268 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
269 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
270 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
271 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
275 .name = "Linksys WRT54G3GV2-VF",
277 { .name = "reset", .gpio = 1 << 6 },
278 { .name = "3g", .gpio = 1 << 5 },
281 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
282 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
283 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
287 .name = "Linksys WRT160N",
289 { .name = "reset", .gpio = 1 << 6 },
290 { .name = "ses", .gpio = 1 << 4 },
293 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
294 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
295 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
299 .name = "Linksys WRT300N V1.1",
301 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
302 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
305 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
306 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
307 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
309 .platform_init = bcm57xx_init,
312 .name = "Linksys WRT350N",
314 { .name = "reset", .gpio = 1 << 6 },
315 { .name = "ses", .gpio = 1 << 8 },
318 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
319 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
320 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
321 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
322 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
324 .platform_init = bcm57xx_init,
327 .name = "Linksys WRT600N",
329 { .name = "reset", .gpio = 1 << 6 },
330 { .name = "ses", .gpio = 1 << 7 },
333 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
334 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
335 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
336 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
337 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
338 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
340 .platform_init = bcm57xx_init,
343 .name = "Linksys WRT600N V1.1",
345 { .name = "reset", .gpio = 1 << 6 },
346 { .name = "ses", .gpio = 1 << 7 },
349 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
350 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
351 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
352 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
353 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
354 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
356 .platform_init = bcm57xx_init,
359 .name = "Linksys WRT610N",
361 { .name = "reset", .gpio = 1 << 6 },
362 { .name = "ses", .gpio = 1 << 8 },
365 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
366 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
367 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
368 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
372 .name = "Linksys WRT610N V2",
374 { .name = "reset", .gpio = 1 << 6 },
375 { .name = "ses", .gpio = 1 << 4 },
378 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL }, // Power LED
379 { .name = "usb", .gpio = 1 << 7, .polarity = NORMAL }, // USB LED
380 { .name = "ses_amber", .gpio = 1 << 0, .polarity = REVERSE }, // WiFi protected setup LED amber
381 { .name = "ses_blue", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED blue
382 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL }, // Wireless LED
386 .name = "Linksys E3000 V1",
388 { .name = "reset", .gpio = 1 << 6 },
389 { .name = "ses", .gpio = 1 << 4 },
392 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL }, // Power LED
393 { .name = "usb", .gpio = 1 << 7, .polarity = NORMAL }, // USB LED
394 { .name = "ses_amber", .gpio = 1 << 0, .polarity = REVERSE }, // WiFi protected setup LED amber
395 { .name = "ses_blue", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED blue
396 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL }, // Wireless LED
401 .name = "ASUS WL-HDD",
403 { .name = "reset", .gpio = 1 << 6 },
406 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
407 { .name = "usb", .gpio = 1 << 2, .polarity = REVERSE },
411 .name = "ASUS WL-300g",
413 { .name = "reset", .gpio = 1 << 6 },
416 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
420 .name = "ASUS WL-320gE/WL-320gP",
422 { .name = "reset", .gpio = 1 << 6 },
425 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
426 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
427 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
431 .name = "ASUS WL-330gE",
433 { .name = "reset", .gpio = 1 << 2 },
436 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
440 .name = "ASUS WL-500g",
442 { .name = "reset", .gpio = 1 << 6 },
445 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
449 .name = "ASUS WL-500g Deluxe",
451 { .name = "reset", .gpio = 1 << 6 },
454 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
458 .name = "ASUS WL-500g Premium",
460 { .name = "reset", .gpio = 1 << 0 },
461 { .name = "ses", .gpio = 1 << 4 },
464 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
468 .name = "ASUS WL-500g Premium V2",
470 { .name = "reset", .gpio = 1 << 2 },
471 { .name = "ses", .gpio = 1 << 3 },
474 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
475 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
479 .name = "ASUS WL-500W",
481 { .name = "reset", .gpio = 1 << 6 },
482 { .name = "ses", .gpio = 1 << 7 },
485 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
489 .name = "ASUS WL-520GC",
491 { .name = "reset", .gpio = 1 << 2 },
492 { .name = "ses", .gpio = 1 << 3 },
495 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
496 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
500 .name = "ASUS WL-520gU",
502 { .name = "reset", .gpio = 1 << 2 },
503 { .name = "ses", .gpio = 1 << 3 },
506 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
507 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
511 .name = "ASUS (unknown, BCM4702)",
513 { .name = "reset", .gpio = 1 << 6 },
516 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
520 .name = "ASUS WL-700gE",
522 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
523 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
524 { .name = "power", .gpio = 1 << 0 }, // on front
525 { .name = "copy", .gpio = 1 << 6 }, // on front
529 // GPIO that controls power led also enables/disables some essential functions
532 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
534 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
536 .platform_init = bcm4780_init,
539 .name = "ASUS RT-N16",
541 { .name = "reset", .gpio = 1 << 8 },
542 { .name = "ses", .gpio = 1 << 5 },
545 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
546 { .name = "wlan", .gpio = 1 << 7, .polarity = NORMAL },
551 .name = "Buffalo WHR-G54S",
553 { .name = "reset", .gpio = 1 << 4 },
554 { .name = "bridge", .gpio = 1 << 5 },
555 { .name = "ses", .gpio = 1 << 0 },
558 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
559 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
560 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
561 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
562 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
566 .name = "Buffalo WBR2-G54",
569 { .name = "reset", .gpio = 1 << 7 },
572 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
576 .name = "Buffalo WHR-HP-G54",
578 { .name = "reset", .gpio = 1 << 4 },
579 { .name = "bridge", .gpio = 1 << 5 },
580 { .name = "ses", .gpio = 1 << 0 },
583 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
584 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
585 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
586 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
587 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
591 .name = "Buffalo WHR-G125",
593 { .name = "reset", .gpio = 1 << 4 },
594 { .name = "bridge", .gpio = 1 << 5 },
595 { .name = "ses", .gpio = 1 << 0 },
598 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
599 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
600 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
601 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
602 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
606 .name = "Buffalo WHR2-A54G54",
608 { .name = "reset", .gpio = 1 << 4 },
611 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
615 .name = "Buffalo WLA2-G54L",
618 { .name = "reset", .gpio = 1 << 7 },
621 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
625 .name = "Buffalo WZR-G300N",
627 { .name = "reset", .gpio = 1 << 4 },
630 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
631 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
632 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
636 .name = "Buffalo WZR-RS-G54",
638 { .name = "ses", .gpio = 1 << 0 },
639 { .name = "reset", .gpio = 1 << 4 },
642 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
643 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
644 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
648 .name = "Buffalo WZR-RS-G54HP",
650 { .name = "ses", .gpio = 1 << 0 },
651 { .name = "reset", .gpio = 1 << 4 },
654 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
655 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
656 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
659 [BUFFALO_UNKNOWN] = {
660 .name = "Buffalo (unknown)",
662 { .name = "reset", .gpio = 1 << 7 },
665 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
668 [BUFFALO_UNKNOWN_4710] = {
669 .name = "Buffalo (unknown, BCM4710)",
671 { .name = "reset", .gpio = 1 << 4 },
674 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
679 .name = "Siemens SE505 V1",
681 /* No usable buttons */
684 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
685 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
686 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
690 .name = "Siemens SE505 V2",
692 /* No usable buttons */
695 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
696 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
697 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
702 .name = "U.S. Robotics USR5461",
704 /* No usable buttons */
707 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
708 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
713 .name = "Dell TrueMobile 2300",
715 { .name = "reset", .gpio = 1 << 0 },
718 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
719 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
723 .name = "Dell TrueMobile 2300 v2",
725 { .name = "reset", .gpio = 1 << 0 },
728 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
729 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
734 .name = "Motorola WE800G",
736 { .name = "reset", .gpio = 1 << 0 },
739 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
740 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
741 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
745 .name = "Motorola WR850G V1",
747 { .name = "reset", .gpio = 1 << 0 },
750 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
751 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
752 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
753 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
754 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
758 .name = "Motorola WR850G V2/V3",
760 { .name = "reset", .gpio = 1 << 5 },
763 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
764 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
765 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
766 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
770 .name = "Motorola WR850GP",
772 { .name = "reset", .gpio = 1 << 5 },
775 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
776 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
777 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
778 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
784 .name = "Belkin (unknown)",
785 /* FIXME: verify & add detection */
787 { .name = "reset", .gpio = 1 << 7 },
790 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
791 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
792 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
796 .name = "Belkin PlayMax F7D4301",
798 { .name = "reset", .gpio = 1 << 6 },
799 { .name = "wps", .gpio = 1 << 8 },
802 { .name = "power", .gpio = 1 << 11, .polarity = REVERSE },
803 { .name = "wlan", .gpio = 1 << 13, .polarity = REVERSE },
804 { .name = "led0", .gpio = 1 << 14, .polarity = REVERSE },
805 { .name = "led1", .gpio = 1 << 15, .polarity = REVERSE },
810 .name = "Netgear WGT634U",
812 { .name = "reset", .gpio = 1 << 2 },
815 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
820 .name = "Netgear WNR834B V1",
821 .buttons = { /* TODO: add reset button and confirm LEDs - GPIO from dd-wrt */ },
823 { .name = "power", .gpio = 1 << 4, .polarity = REVERSE },
824 { .name = "diag", .gpio = 1 << 5, .polarity = REVERSE },
825 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
830 .name = "Netgear WNR834B V2",
832 { .name = "reset", .gpio = 1 << 6 },
835 { .name = "power", .gpio = 1 << 2, .polarity = NORMAL },
836 { .name = "diag", .gpio = 1 << 3, .polarity = NORMAL },
837 { .name = "connected", .gpio = 1 << 7, .polarity = NORMAL },
842 .name = "Trendware TEW411BRP+",
844 { /* No usable buttons */ },
847 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
848 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
849 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
854 .name = "SimpleTech SimpleShare NAS",
856 { .name = "reset", .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
859 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
861 .platform_init = bcm4780_init,
865 .name = "D-Link DIR-130",
867 { .name = "reset", .gpio = 1 << 3},
868 { .name = "reserved", .gpio = 1 << 7},
871 { .name = "diag", .gpio = 1 << 0},
872 { .name = "blue", .gpio = 1 << 6},
876 .name = "D-Link DIR-320",
878 { .name = "reserved", .gpio = 1 << 6},
879 { .name = "reset", .gpio = 1 << 7},
882 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
883 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
884 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
885 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
886 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
890 .name = "D-Link DIR-330",
892 { .name = "reset", .gpio = 1 << 3},
893 { .name = "reserved", .gpio = 1 << 7},
896 { .name = "diag", .gpio = 1 << 0},
897 { .name = "usb", .gpio = 1 << 4},
898 { .name = "blue", .gpio = 1 << 6},
902 .name = "D-Link DWL-3150",
904 { .name = "reset", .gpio = 1 << 7},
907 { .name = "diag", .gpio = 1 << 2},
908 { .name = "status", .gpio = 1 << 1},
913 .name = "Sitecom WL-105b",
915 { .name = "reset", .gpio = 1 << 10},
918 { .name = "wlan", .gpio = 1 << 4},
919 { .name = "power", .gpio = 1 << 3},
922 /* Western Digital Net Center */
924 .name = "Western Digital NetCenter",
926 { .name = "power", .gpio = 1 << 0},
927 { .name = "reset", .gpio = 1 << 7},
929 .platform_init = NetCenter_init,
931 /* Askey (and clones) */
933 .name = "Askey RT210W",
935 /* Power button is hard-wired to hardware reset */
936 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
937 { .name = "power", .gpio = 1 << 7},
940 /* These were verified and named based on Belkin F5D4230-4 v1112 */
941 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
942 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
943 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
947 .name = "OvisLink WL-1600GL",
949 { .name = "reset", .gpio = 1 << 3 },
950 { .name = "ses", .gpio = 1 << 4 },
953 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
954 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
955 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
956 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
961 .name = "Microsoft MN-700",
963 { .name = "reset", .gpio = 1 << 7 },
966 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
971 .name = "Edimax PS-1208MFG",
973 { .name = "reset", .gpio = 1 << 4 },
976 { .name = "status", .gpio = 1 << 1, .polarity = NORMAL },
977 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
982 static struct platform_t __init *platform_detect(void)
984 char *boardnum, *boardtype, *buf;
986 if (strcmp(getvar("nvram_type"), "cfe") == 0)
987 return &platforms[WGT634U];
989 /* Look for a model identifier */
991 /* Based on "model_name" */
992 if ((buf = nvram_get("model_name"))) {
993 if (!strcmp(buf, "DIR-130"))
994 return &platforms[DIR130];
995 if (!strcmp(buf, "DIR-330"))
996 return &platforms[DIR330];
999 /* Based on "wsc_modelname */
1000 if ((buf = nvram_get("wsc_modelname"))) {
1001 if (!strcmp(buf, "WRT610N"))
1002 return &platforms[WRT610N];
1005 /* Based on "model_no" */
1006 if ((buf = nvram_get("model_no"))) {
1007 if (startswith(buf,"WL700")) /* WL700* */
1008 return &platforms[WL700GE];
1011 /* Based on "hardware_version" */
1012 if ((buf = nvram_get("hardware_version"))) {
1013 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
1014 return &platforms[WL500GPV2];
1015 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
1016 return &platforms[WL520GC];
1017 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
1018 return &platforms[WL520GU];
1019 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
1020 return &platforms[WL330GE];
1021 if (startswith(buf,"RT-N16-")) /* RT-N16-* */
1022 return &platforms[RTN16];
1023 if (startswith(buf,"F7D4301")) /* F7D4301* */
1024 return &platforms[BELKIN_F7D4301];
1027 /* Based on "ModelId" */
1028 if ((buf = nvram_get("ModelId"))) {
1029 if (!strcmp(buf, "WR850GP"))
1030 return &platforms[WR850GP];
1031 if (!strcmp(buf, "WR850G"))
1032 return &platforms[WR850GV2V3];
1033 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
1034 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
1035 if (startswith(buf,"WE800G")) /* WE800G* */
1036 return &platforms[WE800G];
1040 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
1041 /* Buffalo hardware, check id for specific hardware matches */
1042 if (!strcmp(buf, "29bb0332"))
1043 return &platforms[WBR2_G54];
1044 if (!strcmp(buf, "29129"))
1045 return &platforms[WLA2_G54L];
1046 if (!strcmp(buf, "30189"))
1047 return &platforms[WHR_HP_G54];
1048 if (!strcmp(buf, "32093"))
1049 return &platforms[WHR_G125];
1050 if (!strcmp(buf, "30182"))
1051 return &platforms[WHR_G54S];
1052 if (!strcmp(buf, "290441dd"))
1053 return &platforms[WHR2_A54G54];
1054 if (!strcmp(buf, "31120"))
1055 return &platforms[WZR_G300N];
1056 if (!strcmp(buf, "30083"))
1057 return &platforms[WZR_RS_G54];
1058 if (!strcmp(buf, "30103"))
1059 return &platforms[WZR_RS_G54HP];
1062 /* no easy model number, attempt to guess */
1063 boardnum = getvar("boardnum");
1064 boardtype = getvar("boardtype");
1066 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
1067 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
1068 return &platforms[WRT600NV11];
1070 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
1071 return &platforms[WRT600N];
1075 * Normally, these would go inside the "CFE based - newer hardware" block below; however, during early init, the
1076 * "pmon_ver" variable is not available on the E3000v1 (and probably the WRT610Nv2 also). Until this is figured out,
1077 * these will need to remain here in order for platform detection to work.
1079 if (!strcmp(boardnum, "42")) { /* Linksys */
1080 if (!strcmp(boardtype, "0x04cf") && !strcmp(getvar("boot_hw_model"), "E300") && !strcmp(getvar("boot_hw_ver"), "1.0"))
1081 return &platforms[E3000V1];
1083 if (!strcmp(boardtype, "0x04cf") && !strcmp(getvar("boot_hw_model"), "WRT610N") && !strcmp(getvar("boot_hw_ver"), "2.0"))
1084 return &platforms[WRT610NV2];
1087 if (startswith(getvar("pmon_ver"), "CFE")) {
1088 /* CFE based - newer hardware */
1089 if (!strcmp(boardnum, "42")) { /* Linksys */
1090 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
1091 return &platforms[WRT300NV11];
1093 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
1094 return &platforms[WRT350N];
1096 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
1097 return &platforms[WRT54G3G];
1099 if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1100 return &platforms[WRT54G3GV2_VF];
1102 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1103 return &platforms[WRTSL54GS];
1105 if (!strcmp(boardtype, "0x0472"))
1106 return &platforms[WRT160N];
1108 /* default to WRT54G */
1109 return &platforms[WRT54G];
1111 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1112 return &platforms[WAP54GV2];
1114 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1115 return &platforms[WL1600GL];
1118 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1119 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1120 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1123 if (!strcmp(boardnum, "45")) { /* ASUS */
1124 if (!strcmp(boardtype,"0x042f"))
1125 return &platforms[WL500GP];
1126 else if (!strcmp(boardtype,"0x0472"))
1127 return &platforms[WL500W];
1128 else if (!strcmp(boardtype,"0x467"))
1129 return &platforms[WL320GE];
1131 return &platforms[WL500GD];
1134 if (!strcmp(boardnum, "10496"))
1135 return &platforms[USR5461];
1137 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1138 return &platforms[SE505V2];
1140 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1141 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1142 return &platforms[DIR320];
1144 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1145 return &platforms[WDNetCenter];
1148 if (!strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B V1 and V2*/
1149 if (!strcmp(boardnum, "08") || !strcmp(boardnum, "8"))
1150 return &platforms[WNR834BV1];
1151 if (!strcmp(boardnum, "01") || !strcmp(boardnum, "1"))
1152 return &platforms[WNR834BV2];
1155 } else { /* PMON based - old stuff */
1156 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1157 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1158 return &platforms[WR850GV1];
1160 if (startswith(boardtype, "bcm94710dev")) {
1161 if (!strcmp(boardnum, "42"))
1162 return &platforms[WRT54GV1];
1163 if (simple_strtoul(boardnum, NULL, 0) == 2)
1164 return &platforms[WAP54GV1];
1166 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1167 if (startswith(getvar("hardware_version"), "WL500-")) {
1168 if (!strcmp(getvar("boardnum"), "mn700"))
1169 return &platforms[MN700];
1171 return &platforms[WL500G];
1173 if (startswith(getvar("hardware_version"), "WL300-")) {
1174 /* Either WL-300g or WL-HDD, do more extensive checks */
1175 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1176 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1177 return &platforms[WLHDD];
1178 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1179 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1180 return &platforms[WL300G];
1182 /* Sitecom WL-105b */
1183 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1184 return &platforms[WL105B];
1186 /* unknown asus stuff, probably bcm4702 */
1187 if (startswith(boardnum, "asusX"))
1188 return &platforms[ASUS_4702];
1190 /* bcm4702 based Askey RT210W clones, Including:
1191 * Askey RT210W (duh?)
1193 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1195 if (!strcmp(boardtype,"bcm94710r4")
1196 && !strcmp(boardnum ,"100")
1197 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1199 return &platforms[RT210W];
1203 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1204 if (startswith(boardtype, "bcm94710ap"))
1205 return &platforms[BUFFALO_UNKNOWN_4710];
1207 return &platforms[BUFFALO_UNKNOWN];
1210 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1211 startswith(getvar("CFEver"), "MotoWRv3") ||
1212 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1214 return &platforms[WR850GV2V3];
1217 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1218 return &platforms[TEW411BRPP];
1221 if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1222 return &platforms[STI_NAS];
1224 if (!strcmp(boardnum, "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1225 return &platforms[DWL3150];
1227 if (!strcmp(boardnum, "01") && !strcmp(boardtype, "0x048e") && /* Edimax PS1208MFG */
1228 !strcmp(getvar("status_gpio"), "1")) /* gpio based detection */
1229 return &platforms[PS1208MFG];
1235 static void register_buttons(struct button_t *b)
1237 for (; b->name; b++)
1238 platform.button_mask |= b->gpio;
1240 platform.button_mask &= ~gpiomask;
1242 gpio_outen(platform.button_mask, 0);
1243 gpio_control(platform.button_mask, 0);
1244 platform.button_polarity = gpio_in() & platform.button_mask;
1245 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1246 gpio_setintmask(platform.button_mask, platform.button_mask);
1248 gpio_set_irqenable(1, button_handler);
1251 static void unregister_buttons(struct button_t *b)
1253 gpio_setintmask(platform.button_mask, 0);
1255 gpio_set_irqenable(0, button_handler);
1259 static void add_msg(struct event_t *event, char *msg, int argv)
1266 s = skb_put(event->skb, strlen(msg) + 1);
1270 static void hotplug_button(struct work_struct *work)
1272 struct event_t *event = container_of(work, struct event_t, wq);
1275 event->skb = alloc_skb(2048, GFP_KERNEL);
1277 s = skb_put(event->skb, strlen(event->action) + 2);
1278 sprintf(s, "%s@", event->action);
1281 NETLINK_CB(event->skb).dst_group = 1;
1282 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
1288 static int fill_event (struct event_t *event)
1290 static char buf[128];
1292 add_msg(event, "HOME=/", 0);
1293 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1294 add_msg(event, "SUBSYSTEM=button", 0);
1295 snprintf(buf, 128, "ACTION=%s", event->action);
1296 add_msg(event, buf, 0);
1297 snprintf(buf, 128, "BUTTON=%s", event->name);
1298 add_msg(event, buf, 0);
1299 snprintf(buf, 128, "SEEN=%ld", event->seen);
1300 add_msg(event, buf, 0);
1301 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1302 add_msg(event, buf, 0);
1308 static irqreturn_t button_handler(int irq, void *dev_id)
1313 in = gpio_in() & platform.button_mask;
1314 gpio_intpolarity(platform.button_mask, in);
1315 changed = platform.button_polarity ^ in;
1316 platform.button_polarity = in;
1318 changed &= ~gpio_outen(0, 0);
1320 for (b = platform.buttons; b->name; b++) {
1321 struct event_t *event;
1323 if (!(b->gpio & changed)) continue;
1327 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1328 event->seen = (jiffies - b->seen)/HZ;
1329 event->name = b->name;
1330 event->action = b->pressed ? "pressed" : "released";
1331 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1332 schedule_work(&event->wq);
1340 static void register_leds(struct led_t *l)
1342 struct proc_dir_entry *p;
1347 leds = proc_mkdir("led", diag);
1351 for(; l->name; l++) {
1352 if (l->gpio & gpiomask)
1355 if (l->gpio & GPIO_TYPE_EXTIF) {
1359 if (l->polarity != INPUT) oe_mask |= l->gpio;
1361 val |= (l->polarity == NORMAL)?0:l->gpio;
1364 if (l->polarity == INPUT) continue;
1366 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1367 l->proc.type = PROC_LED;
1369 p->data = (void *) &l->proc;
1370 p->proc_fops = &diag_proc_fops;
1374 gpio_outen(mask, oe_mask);
1375 gpio_control(mask, 0);
1376 gpio_out(mask, val);
1377 gpio_setintmask(mask, 0);
1380 static void unregister_leds(struct led_t *l)
1383 remove_proc_entry(l->name, leds);
1385 remove_proc_entry("led", diag);
1388 static void set_led_extif(struct led_t *led)
1390 gpio_set_extif(led->gpio, led->state);
1393 static void led_flash(unsigned long dummy) {
1398 for (l = platform.leds; l->name; l++) {
1400 if (l->gpio & GPIO_TYPE_EXTIF) {
1402 l->state = !l->state;
1412 u32 val = ~gpio_in();
1414 gpio_outen(mask, mask);
1415 gpio_control(mask, 0);
1416 gpio_out(mask, val);
1418 if (mask || extif_blink) {
1419 mod_timer(&led_timer, jiffies + FLASH_TIME);
1423 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1425 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1429 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1432 if (dent->data != NULL) {
1433 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1434 switch (handler->type) {
1436 struct led_t * led = (struct led_t *) handler->ptr;
1438 len = sprintf(page, "f\n");
1440 if (led->gpio & GPIO_TYPE_EXTIF) {
1441 len = sprintf(page, "%d\n", led->state);
1443 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1444 u8 p = (led->polarity == NORMAL ? 0 : 1);
1445 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1451 len = sprintf(page, "%s\n", platform.name);
1454 len = sprintf(page, "0x%04x\n", gpiomask);
1461 len = min_t(int, len - *ppos, count);
1462 if (copy_to_user(buf, (page + *ppos), len)) {
1476 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1478 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1482 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1485 if (copy_from_user(page, buf, count)) {
1491 if (dent->data != NULL) {
1492 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1493 switch (handler->type) {
1495 struct led_t *led = (struct led_t *) handler->ptr;
1496 int p = (led->polarity == NORMAL ? 0 : 1);
1498 if (page[0] == 'f') {
1503 if (led->gpio & GPIO_TYPE_EXTIF) {
1504 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1507 gpio_outen(led->gpio, led->gpio);
1508 gpio_control(led->gpio, 0);
1509 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1515 gpiomask = simple_strtoul(page, NULL, 0);
1517 if (platform.buttons) {
1518 unregister_buttons(platform.buttons);
1519 register_buttons(platform.buttons);
1522 if (platform.leds) {
1523 unregister_leds(platform.leds);
1524 register_leds(platform.leds);
1535 static int __init diag_init(void)
1537 static struct proc_dir_entry *p;
1538 static struct platform_t *detected;
1540 detected = platform_detect();
1542 printk(MODULE_NAME ": Router model not detected.\n");
1545 memcpy(&platform, detected, sizeof(struct platform_t));
1547 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1548 if (platform.platform_init != NULL) {
1549 platform.platform_init();
1552 if (!(diag = proc_mkdir("diag", NULL))) {
1553 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1557 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1558 p->data = (void *) &proc_model;
1559 p->proc_fops = &diag_proc_fops;
1562 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1563 p->data = (void *) &proc_gpiomask;
1564 p->proc_fops = &diag_proc_fops;
1567 if (platform.buttons)
1568 register_buttons(platform.buttons);
1571 register_leds(platform.leds);
1576 static void __exit diag_exit(void)
1578 del_timer(&led_timer);
1580 if (platform.buttons)
1581 unregister_buttons(platform.buttons);
1584 unregister_leds(platform.leds);
1586 remove_proc_entry("model", diag);
1587 remove_proc_entry("gpiomask", diag);
1588 remove_proc_entry("diag", NULL);
1591 module_init(diag_init);
1592 module_exit(diag_exit);
1594 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1595 MODULE_LICENSE("GPL");