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>
32 #include <linux/workqueue.h>
33 #include <linux/skbuff.h>
34 #include <linux/netlink.h>
36 extern struct sock *uevent_sock;
37 extern u64 uevent_next_seqnum(void);
39 #include <linux/tqueue.h>
40 #define INIT_WORK INIT_TQUEUE
41 #define schedule_work schedule_task
42 #define work_struct tq_struct
47 #define getvar(str) (nvram_get(str)?:"")
49 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
50 static int fill_event(struct event_t *);
51 static unsigned int gpiomask = 0;
52 module_param(gpiomask, int, 0644);
137 /* Western Digital */
150 static void __init bcm4780_init(void) {
153 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
154 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
155 gpio_outen(pin, pin);
156 gpio_control(pin, 0);
159 /* Wait 5s, so the HDD can spin up */
160 set_current_state(TASK_INTERRUPTIBLE);
161 schedule_timeout(HZ * 5);
164 static void __init NetCenter_init(void) {
165 /* unset pin 6 (+12V) */
167 gpio_outen(pin, pin);
168 gpio_control(pin, 0);
170 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
172 gpio_outen(pin, pin);
173 gpio_control(pin, 0);
175 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
179 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);
190 static struct platform_t __initdata platforms[] = {
193 .name = "Linksys WAP54G V1",
195 { .name = "reset", .gpio = 1 << 0 },
198 { .name = "diag", .gpio = 1 << 3 },
199 { .name = "wlan", .gpio = 1 << 4 },
203 .name = "Linksys WAP54G V2",
205 { .name = "reset", .gpio = 1 << 0 },
208 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
209 /* GPIO 6 is b44 (eth0, LAN) PHY power */
213 .name = "Linksys WAP54G V3",
215 /* FIXME: verify this */
216 { .name = "reset", .gpio = 1 << 7 },
217 { .name = "ses", .gpio = 1 << 0 },
221 { .name = "ses", .gpio = 1 << 1 },
225 .name = "Linksys WRT54G V1.x",
227 { .name = "reset", .gpio = 1 << 6 },
230 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
231 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
235 .name = "Linksys WRT54G/GS/GL",
237 { .name = "reset", .gpio = 1 << 6 },
238 { .name = "ses", .gpio = 1 << 4 },
241 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
242 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
243 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
244 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
245 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
249 .name = "Linksys WRTSL54GS",
251 { .name = "reset", .gpio = 1 << 6 },
252 { .name = "ses", .gpio = 1 << 4 },
255 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
256 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
257 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
258 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
262 .name = "Linksys WRT54G3G",
264 { .name = "reset", .gpio = 1 << 6 },
265 { .name = "3g", .gpio = 1 << 4 },
268 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
269 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
270 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
271 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
272 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
276 .name = "Linksys WRT54G3GV2-VF",
278 { .name = "reset", .gpio = 1 << 6 },
279 { .name = "3g", .gpio = 1 << 4 },
282 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
283 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
284 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
288 .name = "Linksys WRT160N",
290 { .name = "reset", .gpio = 1 << 6 },
291 { .name = "ses", .gpio = 1 << 4 },
294 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
295 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
296 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
300 .name = "Linksys WRT300N V1.1",
302 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
303 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
306 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
307 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
308 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
310 .platform_init = bcm57xx_init,
313 .name = "Linksys WRT350N",
315 { .name = "reset", .gpio = 1 << 6 },
316 { .name = "ses", .gpio = 1 << 8 },
319 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
320 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
321 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
322 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
323 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
325 .platform_init = bcm57xx_init,
328 .name = "Linksys WRT600N",
330 { .name = "reset", .gpio = 1 << 6 },
331 { .name = "ses", .gpio = 1 << 7 },
334 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
335 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
336 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
337 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
338 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
339 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
341 .platform_init = bcm57xx_init,
344 .name = "Linksys WRT600N V1.1",
346 { .name = "reset", .gpio = 1 << 6 },
347 { .name = "ses", .gpio = 1 << 7 },
350 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
351 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
352 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
353 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
354 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
355 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
357 .platform_init = bcm57xx_init,
360 .name = "Linksys WRT610N",
362 { .name = "reset", .gpio = 1 << 6 },
363 { .name = "ses", .gpio = 1 << 8 },
366 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
367 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
368 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
369 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
374 .name = "ASUS WL-HDD",
376 { .name = "reset", .gpio = 1 << 6 },
379 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
380 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
384 .name = "ASUS WL-300g",
386 { .name = "reset", .gpio = 1 << 6 },
389 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
393 .name = "ASUS WL-320gE/WL-320gP",
395 { .name = "reset", .gpio = 1 << 6 },
398 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
399 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
400 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
404 .name = "ASUS WL-330gE",
406 { .name = "reset", .gpio = 1 << 2 },
409 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
413 .name = "ASUS WL-500g",
415 { .name = "reset", .gpio = 1 << 6 },
418 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
422 .name = "ASUS WL-500g Deluxe",
424 { .name = "reset", .gpio = 1 << 6 },
427 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
431 .name = "ASUS WL-500g Premium",
433 { .name = "reset", .gpio = 1 << 0 },
434 { .name = "ses", .gpio = 1 << 4 },
437 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
441 .name = "ASUS WL-500g Premium V2",
443 { .name = "reset", .gpio = 1 << 2 },
444 { .name = "ses", .gpio = 1 << 3 },
447 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
448 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
452 .name = "ASUS WL-500W",
454 { .name = "reset", .gpio = 1 << 6 },
455 { .name = "ses", .gpio = 1 << 7 },
458 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
462 .name = "ASUS WL-520GC",
464 { .name = "reset", .gpio = 1 << 2 },
465 { .name = "ses", .gpio = 1 << 3 },
468 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
469 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
473 .name = "ASUS WL-520gU",
475 { .name = "reset", .gpio = 1 << 2 },
476 { .name = "ses", .gpio = 1 << 3 },
479 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
480 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
484 .name = "ASUS (unknown, BCM4702)",
486 { .name = "reset", .gpio = 1 << 6 },
489 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
493 .name = "ASUS WL-700gE",
495 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
496 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
497 { .name = "power", .gpio = 1 << 0 }, // on front
498 { .name = "copy", .gpio = 1 << 6 }, // on front
502 // GPIO that controls power led also enables/disables some essential functions
505 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
507 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
509 .platform_init = bcm4780_init,
513 .name = "Buffalo WHR-G54S",
515 { .name = "reset", .gpio = 1 << 4 },
516 { .name = "bridge", .gpio = 1 << 5 },
517 { .name = "ses", .gpio = 1 << 0 },
520 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
521 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
522 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
523 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
524 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
528 .name = "Buffalo WBR2-G54",
531 { .name = "reset", .gpio = 1 << 7 },
534 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
538 .name = "Buffalo WHR-HP-G54",
540 { .name = "reset", .gpio = 1 << 4 },
541 { .name = "bridge", .gpio = 1 << 5 },
542 { .name = "ses", .gpio = 1 << 0 },
545 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
546 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
547 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
548 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
549 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
553 .name = "Buffalo WHR-G125",
555 { .name = "reset", .gpio = 1 << 4 },
556 { .name = "bridge", .gpio = 1 << 5 },
557 { .name = "ses", .gpio = 1 << 0 },
560 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
561 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
562 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
563 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
564 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
568 .name = "Buffalo WHR2-A54G54",
570 { .name = "reset", .gpio = 1 << 4 },
573 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
577 .name = "Buffalo WLA2-G54L",
580 { .name = "reset", .gpio = 1 << 7 },
583 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
587 .name = "Buffalo WZR-G300N",
589 { .name = "reset", .gpio = 1 << 4 },
592 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
593 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
594 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
598 .name = "Buffalo WZR-RS-G54",
600 { .name = "ses", .gpio = 1 << 0 },
601 { .name = "reset", .gpio = 1 << 4 },
604 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
605 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
606 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
610 .name = "Buffalo WZR-RS-G54HP",
612 { .name = "ses", .gpio = 1 << 0 },
613 { .name = "reset", .gpio = 1 << 4 },
616 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
617 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
618 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
621 [BUFFALO_UNKNOWN] = {
622 .name = "Buffalo (unknown)",
624 { .name = "reset", .gpio = 1 << 7 },
627 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
630 [BUFFALO_UNKNOWN_4710] = {
631 .name = "Buffalo (unknown, BCM4710)",
633 { .name = "reset", .gpio = 1 << 4 },
636 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
641 .name = "Siemens SE505 V1",
643 /* No usable buttons */
646 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
647 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
648 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
652 .name = "Siemens SE505 V2",
654 /* No usable buttons */
657 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
658 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
659 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
664 .name = "U.S. Robotics USR5461",
666 /* No usable buttons */
669 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
670 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
675 .name = "Dell TrueMobile 2300",
677 { .name = "reset", .gpio = 1 << 0 },
680 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
681 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
685 .name = "Dell TrueMobile 2300 v2",
687 { .name = "reset", .gpio = 1 << 0 },
690 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
691 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
696 .name = "Motorola WE800G",
698 { .name = "reset", .gpio = 1 << 0 },
701 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
702 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
703 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
707 .name = "Motorola WR850G V1",
709 { .name = "reset", .gpio = 1 << 0 },
712 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
713 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
714 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
715 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
716 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
720 .name = "Motorola WR850G V2/V3",
722 { .name = "reset", .gpio = 1 << 5 },
725 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
726 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
727 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
728 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
732 .name = "Motorola WR850GP",
734 { .name = "reset", .gpio = 1 << 5 },
737 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
738 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
739 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
740 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
746 .name = "Belkin (unknown)",
747 /* FIXME: verify & add detection */
749 { .name = "reset", .gpio = 1 << 7 },
752 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
753 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
754 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
759 .name = "Netgear WGT634U",
761 { .name = "reset", .gpio = 1 << 2 },
764 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
769 .name = "Trendware TEW411BRP+",
771 { /* No usable buttons */ },
774 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
775 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
776 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
781 .name = "SimpleTech SimpleShare NAS",
783 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
784 { .name = "power", .gpio = 1 << 0 }, // on back
787 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
789 .platform_init = bcm4780_init,
793 .name = "D-Link DIR-130",
795 { .name = "reset", .gpio = 1 << 3},
796 { .name = "reserved", .gpio = 1 << 7},
799 { .name = "diag", .gpio = 1 << 0},
800 { .name = "blue", .gpio = 1 << 6},
804 .name = "D-Link DIR-320",
806 { .name = "reserved", .gpio = 1 << 6},
807 { .name = "reset", .gpio = 1 << 7},
810 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
811 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
812 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
813 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
814 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
818 .name = "D-Link DIR-330",
820 { .name = "reset", .gpio = 1 << 3},
821 { .name = "reserved", .gpio = 1 << 7},
824 { .name = "diag", .gpio = 1 << 0},
825 { .name = "usb", .gpio = 1 << 4},
826 { .name = "blue", .gpio = 1 << 6},
830 .name = "D-Link DWL-3150",
832 { .name = "reset", .gpio = 1 << 7},
835 { .name = "diag", .gpio = 1 << 2},
836 { .name = "status", .gpio = 1 << 1},
841 .name = "Sitecom WL-105b",
843 { .name = "reset", .gpio = 1 << 10},
846 { .name = "wlan", .gpio = 1 << 4},
847 { .name = "power", .gpio = 1 << 3},
850 /* Western Digital Net Center */
852 .name = "Western Digital NetCenter",
854 { .name = "power", .gpio = 1 << 0},
855 { .name = "reset", .gpio = 1 << 7},
857 .platform_init = NetCenter_init,
859 /* Askey (and clones) */
861 .name = "Askey RT210W",
863 /* Power button is hard-wired to hardware reset */
864 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
865 { .name = "power", .gpio = 1 << 7},
868 /* These were verified and named based on Belkin F5D4230-4 v1112 */
869 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
870 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
871 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
875 .name = "OvisLink WL-1600GL",
877 { .name = "reset", .gpio = 1 << 3 },
878 { .name = "ses", .gpio = 1 << 4 },
881 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
882 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
883 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
884 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
889 .name = "Microsoft MN-700",
891 { .name = "reset", .gpio = 1 << 7 },
894 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
899 static struct platform_t __init *platform_detect(void)
901 char *boardnum, *boardtype, *buf;
903 if (strcmp(getvar("nvram_type"), "cfe") == 0)
904 return &platforms[WGT634U];
906 /* Look for a model identifier */
908 /* Based on "model_name" */
909 if ((buf = nvram_get("model_name"))) {
910 if (!strcmp(buf, "DIR-130"))
911 return &platforms[DIR130];
912 if (!strcmp(buf, "DIR-330"))
913 return &platforms[DIR330];
916 /* Based on "wsc_modelname */
917 if ((buf = nvram_get("wsc_modelname"))) {
918 if (!strcmp(buf, "WRT610N"))
919 return &platforms[WRT610N];
922 /* Based on "model_no" */
923 if ((buf = nvram_get("model_no"))) {
924 if (startswith(buf,"WL700")) /* WL700* */
925 return &platforms[WL700GE];
928 /* Based on "hardware_version" */
929 if ((buf = nvram_get("hardware_version"))) {
930 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
931 return &platforms[WL500GPV2];
932 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
933 return &platforms[WL520GC];
934 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
935 return &platforms[WL520GU];
936 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
937 return &platforms[WL330GE];
940 /* Based on "ModelId" */
941 if ((buf = nvram_get("ModelId"))) {
942 if (!strcmp(buf, "WR850GP"))
943 return &platforms[WR850GP];
944 if (!strcmp(buf, "WR850G"))
945 return &platforms[WR850GV2V3];
946 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
947 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
948 if (startswith(buf,"WE800G")) /* WE800G* */
949 return &platforms[WE800G];
953 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
954 /* Buffalo hardware, check id for specific hardware matches */
955 if (!strcmp(buf, "29bb0332"))
956 return &platforms[WBR2_G54];
957 if (!strcmp(buf, "29129"))
958 return &platforms[WLA2_G54L];
959 if (!strcmp(buf, "30189"))
960 return &platforms[WHR_HP_G54];
961 if (!strcmp(buf, "32093"))
962 return &platforms[WHR_G125];
963 if (!strcmp(buf, "30182"))
964 return &platforms[WHR_G54S];
965 if (!strcmp(buf, "290441dd"))
966 return &platforms[WHR2_A54G54];
967 if (!strcmp(buf, "31120"))
968 return &platforms[WZR_G300N];
969 if (!strcmp(buf, "30083"))
970 return &platforms[WZR_RS_G54];
971 if (!strcmp(buf, "30103"))
972 return &platforms[WZR_RS_G54HP];
975 /* no easy model number, attempt to guess */
976 boardnum = getvar("boardnum");
977 boardtype = getvar("boardtype");
979 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
980 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
981 return &platforms[WRT600NV11];
983 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
984 return &platforms[WRT600N];
987 if (startswith(getvar("pmon_ver"), "CFE")) {
988 /* CFE based - newer hardware */
989 if (!strcmp(boardnum, "42")) { /* Linksys */
990 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
991 return &platforms[WRT300NV11];
993 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
994 return &platforms[WRT350N];
996 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
997 return &platforms[WRT54G3G];
999 if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1000 return &platforms[WRT54G3GV2_VF];
1002 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1003 return &platforms[WRTSL54GS];
1005 if (!strcmp(boardtype, "0x0472"))
1006 return &platforms[WRT160N];
1008 /* default to WRT54G */
1009 return &platforms[WRT54G];
1011 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1012 return &platforms[WAP54GV2];
1014 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1015 return &platforms[WL1600GL];
1018 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1019 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1020 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1023 if (!strcmp(boardnum, "45")) { /* ASUS */
1024 if (!strcmp(boardtype,"0x042f"))
1025 return &platforms[WL500GP];
1026 else if (!strcmp(boardtype,"0x0472"))
1027 return &platforms[WL500W];
1028 else if (!strcmp(boardtype,"0x467"))
1029 return &platforms[WL320GE];
1031 return &platforms[WL500GD];
1034 if (!strcmp(boardnum, "10496"))
1035 return &platforms[USR5461];
1037 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1038 return &platforms[SE505V2];
1040 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1041 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1042 return &platforms[DIR320];
1044 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1045 return &platforms[WDNetCenter];
1048 } else { /* PMON based - old stuff */
1049 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1050 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1051 return &platforms[WR850GV1];
1053 if (startswith(boardtype, "bcm94710dev")) {
1054 if (!strcmp(boardnum, "42"))
1055 return &platforms[WRT54GV1];
1056 if (simple_strtoul(boardnum, NULL, 0) == 2)
1057 return &platforms[WAP54GV1];
1059 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1060 if (startswith(getvar("hardware_version"), "WL500-")) {
1061 if (!strcmp(getvar("boardnum"), "mn700"))
1062 return &platforms[MN700];
1064 return &platforms[WL500G];
1066 if (startswith(getvar("hardware_version"), "WL300-")) {
1067 /* Either WL-300g or WL-HDD, do more extensive checks */
1068 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1069 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1070 return &platforms[WLHDD];
1071 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1072 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1073 return &platforms[WL300G];
1075 /* Sitecom WL-105b */
1076 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1077 return &platforms[WL105B];
1079 /* unknown asus stuff, probably bcm4702 */
1080 if (startswith(boardnum, "asusX"))
1081 return &platforms[ASUS_4702];
1083 /* bcm4702 based Askey RT210W clones, Including:
1084 * Askey RT210W (duh?)
1086 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1088 if (!strcmp(boardtype,"bcm94710r4")
1089 && !strcmp(boardnum ,"100")
1090 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1092 return &platforms[RT210W];
1096 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1097 if (startswith(boardtype, "bcm94710ap"))
1098 return &platforms[BUFFALO_UNKNOWN_4710];
1100 return &platforms[BUFFALO_UNKNOWN];
1103 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1104 startswith(getvar("CFEver"), "MotoWRv3") ||
1105 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1107 return &platforms[WR850GV2V3];
1110 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1111 return &platforms[TEW411BRPP];
1114 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
1115 return &platforms[STI_NAS];
1117 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1118 return &platforms[DWL3150];
1124 static void register_buttons(struct button_t *b)
1126 for (; b->name; b++)
1127 platform.button_mask |= b->gpio;
1129 platform.button_mask &= ~gpiomask;
1131 gpio_outen(platform.button_mask, 0);
1132 gpio_control(platform.button_mask, 0);
1133 platform.button_polarity = gpio_in() & platform.button_mask;
1134 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1135 gpio_setintmask(platform.button_mask, platform.button_mask);
1137 gpio_set_irqenable(1, button_handler);
1140 static void unregister_buttons(struct button_t *b)
1142 gpio_setintmask(platform.button_mask, 0);
1144 gpio_set_irqenable(0, button_handler);
1149 static void add_msg(struct event_t *event, char *msg, int argv)
1156 s = skb_put(event->skb, strlen(msg) + 1);
1160 static void hotplug_button(struct work_struct *work)
1162 struct event_t *event = container_of(work, struct event_t, wq);
1168 event->skb = alloc_skb(2048, GFP_KERNEL);
1170 s = skb_put(event->skb, strlen(event->action) + 2);
1171 sprintf(s, "%s@", event->action);
1174 NETLINK_CB(event->skb).dst_group = 1;
1175 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1180 #else /* !LINUX_2_4 */
1181 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1185 p = kmalloc(size, gfp);
1194 static void add_msg(struct event_t *event, char *msg, int argv)
1197 event->argv[event->anr++] = event->scratch;
1199 event->envp[event->enr++] = event->scratch;
1201 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1204 static void hotplug_button(struct event_t *event)
1206 char *scratch = kzalloc(256, GFP_KERNEL);
1207 event->scratch = scratch;
1209 add_msg(event, hotplug_path, 1);
1210 add_msg(event, "button", 1);
1212 call_usermodehelper (event->argv[0], event->argv, event->envp);
1216 #endif /* !LINUX_2_4 */
1218 static int fill_event (struct event_t *event)
1220 static char buf[128];
1222 add_msg(event, "HOME=/", 0);
1223 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1224 add_msg(event, "SUBSYSTEM=button", 0);
1225 snprintf(buf, 128, "ACTION=%s", event->action);
1226 add_msg(event, buf, 0);
1227 snprintf(buf, 128, "BUTTON=%s", event->name);
1228 add_msg(event, buf, 0);
1229 snprintf(buf, 128, "SEEN=%ld", event->seen);
1230 add_msg(event, buf, 0);
1232 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1233 add_msg(event, buf, 0);
1241 static irqreturn_t button_handler(int irq, void *dev_id)
1243 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1249 in = gpio_in() & platform.button_mask;
1250 gpio_intpolarity(platform.button_mask, in);
1251 changed = platform.button_polarity ^ in;
1252 platform.button_polarity = in;
1254 changed &= ~gpio_outen(0, 0);
1256 for (b = platform.buttons; b->name; b++) {
1257 struct event_t *event;
1259 if (!(b->gpio & changed)) continue;
1263 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1264 event->seen = (jiffies - b->seen)/HZ;
1265 event->name = b->name;
1266 event->action = b->pressed ? "pressed" : "released";
1267 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1268 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1270 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1272 schedule_work(&event->wq);
1280 static void register_leds(struct led_t *l)
1282 struct proc_dir_entry *p;
1287 leds = proc_mkdir("led", diag);
1291 for(; l->name; l++) {
1292 if (l->gpio & gpiomask)
1295 if (l->gpio & GPIO_TYPE_EXTIF) {
1299 if (l->polarity != INPUT) oe_mask |= l->gpio;
1301 val |= (l->polarity == NORMAL)?0:l->gpio;
1304 if (l->polarity == INPUT) continue;
1306 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1307 l->proc.type = PROC_LED;
1309 p->data = (void *) &l->proc;
1310 p->proc_fops = &diag_proc_fops;
1314 gpio_outen(mask, oe_mask);
1315 gpio_control(mask, 0);
1316 gpio_out(mask, val);
1317 gpio_setintmask(mask, 0);
1320 static void unregister_leds(struct led_t *l)
1323 remove_proc_entry(l->name, leds);
1325 remove_proc_entry("led", diag);
1328 static void set_led_extif(struct led_t *led)
1330 gpio_set_extif(led->gpio, led->state);
1333 static void led_flash(unsigned long dummy) {
1338 for (l = platform.leds; l->name; l++) {
1340 if (l->gpio & GPIO_TYPE_EXTIF) {
1342 l->state = !l->state;
1352 u32 val = ~gpio_in();
1354 gpio_outen(mask, mask);
1355 gpio_control(mask, 0);
1356 gpio_out(mask, val);
1358 if (mask || extif_blink) {
1359 mod_timer(&led_timer, jiffies + FLASH_TIME);
1363 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1366 struct inode *inode = file->f_dentry->d_inode;
1367 struct proc_dir_entry *dent = inode->u.generic_ip;
1369 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1374 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1377 if (dent->data != NULL) {
1378 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1379 switch (handler->type) {
1381 struct led_t * led = (struct led_t *) handler->ptr;
1383 len = sprintf(page, "f\n");
1385 if (led->gpio & GPIO_TYPE_EXTIF) {
1386 len = sprintf(page, "%d\n", led->state);
1388 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1389 u8 p = (led->polarity == NORMAL ? 0 : 1);
1390 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1396 len = sprintf(page, "%s\n", platform.name);
1399 len = sprintf(page, "0x%04x\n", gpiomask);
1406 len = min_t(int, len - *ppos, count);
1407 if (copy_to_user(buf, (page + *ppos), len)) {
1421 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1424 struct inode *inode = file->f_dentry->d_inode;
1425 struct proc_dir_entry *dent = inode->u.generic_ip;
1427 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1432 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1435 if (copy_from_user(page, buf, count)) {
1441 if (dent->data != NULL) {
1442 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1443 switch (handler->type) {
1445 struct led_t *led = (struct led_t *) handler->ptr;
1446 int p = (led->polarity == NORMAL ? 0 : 1);
1448 if (page[0] == 'f') {
1453 if (led->gpio & GPIO_TYPE_EXTIF) {
1454 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1457 gpio_outen(led->gpio, led->gpio);
1458 gpio_control(led->gpio, 0);
1459 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1465 gpiomask = simple_strtoul(page, NULL, 0);
1467 if (platform.buttons) {
1468 unregister_buttons(platform.buttons);
1469 register_buttons(platform.buttons);
1472 if (platform.leds) {
1473 unregister_leds(platform.leds);
1474 register_leds(platform.leds);
1485 static int __init diag_init(void)
1487 static struct proc_dir_entry *p;
1488 static struct platform_t *detected;
1490 detected = platform_detect();
1492 printk(MODULE_NAME ": Router model not detected.\n");
1495 memcpy(&platform, detected, sizeof(struct platform_t));
1497 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1498 if (platform.platform_init != NULL) {
1499 platform.platform_init();
1502 if (!(diag = proc_mkdir("diag", NULL))) {
1503 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1507 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1508 p->data = (void *) &proc_model;
1509 p->proc_fops = &diag_proc_fops;
1512 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1513 p->data = (void *) &proc_gpiomask;
1514 p->proc_fops = &diag_proc_fops;
1517 if (platform.buttons)
1518 register_buttons(platform.buttons);
1521 register_leds(platform.leds);
1526 static void __exit diag_exit(void)
1528 del_timer(&led_timer);
1530 if (platform.buttons)
1531 unregister_buttons(platform.buttons);
1534 unregister_leds(platform.leds);
1536 remove_proc_entry("model", diag);
1537 remove_proc_entry("gpiomask", diag);
1538 remove_proc_entry("diag", NULL);
1541 module_init(diag_init);
1542 module_exit(diag_exit);
1544 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1545 MODULE_LICENSE("GPL");