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);
136 /* Western Digital */
146 static void __init bcm4780_init(void) {
149 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
150 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
151 gpio_outen(pin, pin);
152 gpio_control(pin, 0);
155 /* Wait 5s, so the HDD can spin up */
156 set_current_state(TASK_INTERRUPTIBLE);
157 schedule_timeout(HZ * 5);
160 static void __init NetCenter_init(void) {
161 /* unset pin 6 (+12V) */
163 gpio_outen(pin, pin);
164 gpio_control(pin, 0);
166 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
168 gpio_outen(pin, pin);
169 gpio_control(pin, 0);
171 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
175 static void __init bcm57xx_init(void) {
179 /* FIXME: switch comes up, but port mappings/vlans not right */
180 gpio_outen(pin, pin);
181 gpio_control(pin, 0);
186 static struct platform_t __initdata platforms[] = {
189 .name = "Linksys WAP54G V1",
191 { .name = "reset", .gpio = 1 << 0 },
194 { .name = "diag", .gpio = 1 << 3 },
195 { .name = "wlan", .gpio = 1 << 4 },
199 .name = "Linksys WAP54G V2",
201 { .name = "reset", .gpio = 1 << 0 },
204 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
205 /* GPIO 6 is b44 (eth0, LAN) PHY power */
209 .name = "Linksys WAP54G V3",
211 /* FIXME: verify this */
212 { .name = "reset", .gpio = 1 << 7 },
213 { .name = "ses", .gpio = 1 << 0 },
217 { .name = "ses", .gpio = 1 << 1 },
221 .name = "Linksys WRT54G V1.x",
223 { .name = "reset", .gpio = 1 << 6 },
226 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
227 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
231 .name = "Linksys WRT54G/GS/GL",
233 { .name = "reset", .gpio = 1 << 6 },
234 { .name = "ses", .gpio = 1 << 4 },
237 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
238 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
239 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
240 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
241 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
245 .name = "Linksys WRTSL54GS",
247 { .name = "reset", .gpio = 1 << 6 },
248 { .name = "ses", .gpio = 1 << 4 },
251 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
252 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
253 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
254 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
258 .name = "Linksys WRT54G3G",
260 { .name = "reset", .gpio = 1 << 6 },
261 { .name = "3g", .gpio = 1 << 4 },
264 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
265 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
266 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
267 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
268 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
272 .name = "Linksys WRT160N",
274 { .name = "reset", .gpio = 1 << 6 },
275 { .name = "ses", .gpio = 1 << 4 },
278 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
279 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
280 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
284 .name = "Linksys WRT300N V1.1",
286 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
287 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
290 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
291 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
292 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
294 .platform_init = bcm57xx_init,
297 .name = "Linksys WRT350N",
299 { .name = "reset", .gpio = 1 << 6 },
300 { .name = "ses", .gpio = 1 << 8 },
303 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
304 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
305 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
306 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
307 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
309 .platform_init = bcm57xx_init,
312 .name = "Linksys WRT600N",
314 { .name = "reset", .gpio = 1 << 6 },
315 { .name = "ses", .gpio = 1 << 7 },
318 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
319 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
320 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
321 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
322 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
323 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
325 .platform_init = bcm57xx_init,
328 .name = "Linksys WRT600N V1.1",
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 WRT610N",
346 { .name = "reset", .gpio = 1 << 6 },
347 { .name = "ses", .gpio = 1 << 8 },
350 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
351 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
352 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
353 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
358 .name = "ASUS WL-HDD",
360 { .name = "reset", .gpio = 1 << 6 },
363 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
364 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
368 .name = "ASUS WL-300g",
370 { .name = "reset", .gpio = 1 << 6 },
373 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
377 .name = "ASUS WL-320gE/WL-320gP",
379 { .name = "reset", .gpio = 1 << 6 },
382 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
383 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
384 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
388 .name = "ASUS WL-330gE",
390 { .name = "reset", .gpio = 1 << 2 },
393 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
397 .name = "ASUS WL-500g",
399 { .name = "reset", .gpio = 1 << 6 },
402 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
406 .name = "ASUS WL-500g Deluxe",
408 { .name = "reset", .gpio = 1 << 6 },
411 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
415 .name = "ASUS WL-500g Premium",
417 { .name = "reset", .gpio = 1 << 0 },
418 { .name = "ses", .gpio = 1 << 4 },
421 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
425 .name = "ASUS WL-500g Premium V2",
427 { .name = "reset", .gpio = 1 << 2 },
428 { .name = "ses", .gpio = 1 << 3 },
431 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
432 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
436 .name = "ASUS WL-500W",
438 { .name = "reset", .gpio = 1 << 6 },
439 { .name = "ses", .gpio = 1 << 7 },
442 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
446 .name = "ASUS WL-520GC",
448 { .name = "reset", .gpio = 1 << 2 },
449 { .name = "ses", .gpio = 1 << 3 },
452 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
453 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
457 .name = "ASUS WL-520gU",
459 { .name = "reset", .gpio = 1 << 2 },
460 { .name = "ses", .gpio = 1 << 3 },
463 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
464 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
468 .name = "ASUS (unknown, BCM4702)",
470 { .name = "reset", .gpio = 1 << 6 },
473 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
477 .name = "ASUS WL-700gE",
479 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
480 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
481 { .name = "power", .gpio = 1 << 0 }, // on front
482 { .name = "copy", .gpio = 1 << 6 }, // on front
486 // GPIO that controls power led also enables/disables some essential functions
489 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
491 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
493 .platform_init = bcm4780_init,
497 .name = "Buffalo WHR-G54S",
499 { .name = "reset", .gpio = 1 << 4 },
500 { .name = "bridge", .gpio = 1 << 5 },
501 { .name = "ses", .gpio = 1 << 0 },
504 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
505 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
506 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
507 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
508 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
512 .name = "Buffalo WBR2-G54",
515 { .name = "reset", .gpio = 1 << 7 },
518 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
522 .name = "Buffalo WHR-HP-G54",
524 { .name = "reset", .gpio = 1 << 4 },
525 { .name = "bridge", .gpio = 1 << 5 },
526 { .name = "ses", .gpio = 1 << 0 },
529 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
530 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
531 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
532 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
533 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
537 .name = "Buffalo WHR-G125",
539 { .name = "reset", .gpio = 1 << 4 },
540 { .name = "bridge", .gpio = 1 << 5 },
541 { .name = "ses", .gpio = 1 << 0 },
544 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
545 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
546 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
547 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
548 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
552 .name = "Buffalo WHR2-A54G54",
554 { .name = "reset", .gpio = 1 << 4 },
557 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
561 .name = "Buffalo WLA2-G54L",
564 { .name = "reset", .gpio = 1 << 7 },
567 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
571 .name = "Buffalo WZR-G300N",
573 { .name = "reset", .gpio = 1 << 4 },
576 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
577 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
578 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
582 .name = "Buffalo WZR-RS-G54",
584 { .name = "ses", .gpio = 1 << 0 },
585 { .name = "reset", .gpio = 1 << 4 },
588 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
589 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
590 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
594 .name = "Buffalo WZR-RS-G54HP",
596 { .name = "ses", .gpio = 1 << 0 },
597 { .name = "reset", .gpio = 1 << 4 },
600 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
601 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
602 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
605 [BUFFALO_UNKNOWN] = {
606 .name = "Buffalo (unknown)",
608 { .name = "reset", .gpio = 1 << 7 },
611 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
614 [BUFFALO_UNKNOWN_4710] = {
615 .name = "Buffalo (unknown, BCM4710)",
617 { .name = "reset", .gpio = 1 << 4 },
620 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
625 .name = "Siemens SE505 V1",
627 /* No usable buttons */
630 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
631 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
632 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
636 .name = "Siemens SE505 V2",
638 /* No usable buttons */
641 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
642 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
643 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
648 .name = "U.S. Robotics USR5461",
650 /* No usable buttons */
653 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
654 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
659 .name = "Dell TrueMobile 2300",
661 { .name = "reset", .gpio = 1 << 0 },
664 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
665 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
669 .name = "Dell TrueMobile 2300 v2",
671 { .name = "reset", .gpio = 1 << 0 },
674 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
675 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
680 .name = "Motorola WE800G",
682 { .name = "reset", .gpio = 1 << 0 },
685 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
686 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
687 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
691 .name = "Motorola WR850G V1",
693 { .name = "reset", .gpio = 1 << 0 },
696 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
697 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
698 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
699 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
700 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
704 .name = "Motorola WR850G V2/V3",
706 { .name = "reset", .gpio = 1 << 5 },
709 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
710 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
711 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
712 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
716 .name = "Motorola WR850GP",
718 { .name = "reset", .gpio = 1 << 5 },
721 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
722 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
723 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
724 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
730 .name = "Belkin (unknown)",
731 /* FIXME: verify & add detection */
733 { .name = "reset", .gpio = 1 << 7 },
736 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
737 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
738 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
743 .name = "Netgear WGT634U",
745 { .name = "reset", .gpio = 1 << 2 },
748 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
753 .name = "Trendware TEW411BRP+",
755 { /* No usable buttons */ },
758 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
759 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
760 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
765 .name = "SimpleTech SimpleShare NAS",
767 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
768 { .name = "power", .gpio = 1 << 0 }, // on back
771 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
773 .platform_init = bcm4780_init,
777 .name = "D-Link DIR-130",
779 { .name = "reset", .gpio = 1 << 3},
780 { .name = "reserved", .gpio = 1 << 7},
783 { .name = "diag", .gpio = 1 << 0},
784 { .name = "blue", .gpio = 1 << 6},
788 .name = "D-Link DIR-320",
790 { .name = "reserved", .gpio = 1 << 6},
791 { .name = "reset", .gpio = 1 << 7},
794 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
795 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
796 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
797 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
798 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
802 .name = "D-Link DIR-330",
804 { .name = "reset", .gpio = 1 << 3},
805 { .name = "reserved", .gpio = 1 << 7},
808 { .name = "diag", .gpio = 1 << 0},
809 { .name = "usb", .gpio = 1 << 4},
810 { .name = "blue", .gpio = 1 << 6},
814 .name = "D-Link DWL-3150",
816 { .name = "reset", .gpio = 1 << 7},
819 { .name = "diag", .gpio = 1 << 2},
820 { .name = "status", .gpio = 1 << 1},
825 .name = "Sitecom WL-105b",
827 { .name = "reset", .gpio = 1 << 10},
830 { .name = "wlan", .gpio = 1 << 4},
831 { .name = "power", .gpio = 1 << 3},
834 /* Western Digital Net Center */
836 .name = "Western Digital NetCenter",
838 { .name = "power", .gpio = 1 << 0},
839 { .name = "reset", .gpio = 1 << 7},
841 .platform_init = NetCenter_init,
843 /* Askey (and clones) */
845 .name = "Askey RT210W",
847 /* Power button is hard-wired to hardware reset */
848 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
849 { .name = "power", .gpio = 1 << 7},
852 /* These were verified and named based on Belkin F5D4230-4 v1112 */
853 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
854 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
855 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
859 .name = "OvisLink WL-1600GL",
861 { .name = "reset", .gpio = 1 << 3 },
862 { .name = "ses", .gpio = 1 << 4 },
865 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
866 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
867 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
868 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
873 static struct platform_t __init *platform_detect(void)
875 char *boardnum, *boardtype, *buf;
877 if (strcmp(getvar("nvram_type"), "cfe") == 0)
878 return &platforms[WGT634U];
880 /* Look for a model identifier */
882 /* Based on "model_name" */
883 if ((buf = nvram_get("model_name"))) {
884 if (!strcmp(buf, "DIR-130"))
885 return &platforms[DIR130];
886 if (!strcmp(buf, "DIR-330"))
887 return &platforms[DIR330];
890 /* Based on "wsc_modelname */
891 if ((buf = nvram_get("wsc_modelname"))) {
892 if (!strcmp(buf, "WRT610N"))
893 return &platforms[WRT610N];
896 /* Based on "model_no" */
897 if ((buf = nvram_get("model_no"))) {
898 if (startswith(buf,"WL700")) /* WL700* */
899 return &platforms[WL700GE];
902 /* Based on "hardware_version" */
903 if ((buf = nvram_get("hardware_version"))) {
904 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
905 return &platforms[WL500GPV2];
906 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
907 return &platforms[WL520GC];
908 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
909 return &platforms[WL520GU];
910 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
911 return &platforms[WL330GE];
914 /* Based on "ModelId" */
915 if ((buf = nvram_get("ModelId"))) {
916 if (!strcmp(buf, "WR850GP"))
917 return &platforms[WR850GP];
918 if (!strcmp(buf, "WR850G"))
919 return &platforms[WR850GV2V3];
920 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
921 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
922 if (startswith(buf,"WE800G")) /* WE800G* */
923 return &platforms[WE800G];
927 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
928 /* Buffalo hardware, check id for specific hardware matches */
929 if (!strcmp(buf, "29bb0332"))
930 return &platforms[WBR2_G54];
931 if (!strcmp(buf, "29129"))
932 return &platforms[WLA2_G54L];
933 if (!strcmp(buf, "30189"))
934 return &platforms[WHR_HP_G54];
935 if (!strcmp(buf, "32093"))
936 return &platforms[WHR_G125];
937 if (!strcmp(buf, "30182"))
938 return &platforms[WHR_G54S];
939 if (!strcmp(buf, "290441dd"))
940 return &platforms[WHR2_A54G54];
941 if (!strcmp(buf, "31120"))
942 return &platforms[WZR_G300N];
943 if (!strcmp(buf, "30083"))
944 return &platforms[WZR_RS_G54];
945 if (!strcmp(buf, "30103"))
946 return &platforms[WZR_RS_G54HP];
949 /* no easy model number, attempt to guess */
950 boardnum = getvar("boardnum");
951 boardtype = getvar("boardtype");
953 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
954 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
955 return &platforms[WRT600NV11];
957 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
958 return &platforms[WRT600N];
961 if (startswith(getvar("pmon_ver"), "CFE")) {
962 /* CFE based - newer hardware */
963 if (!strcmp(boardnum, "42")) { /* Linksys */
964 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
965 return &platforms[WRT300NV11];
967 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
968 return &platforms[WRT350N];
970 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
971 return &platforms[WRT54G3G];
973 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
974 return &platforms[WRTSL54GS];
976 if (!strcmp(boardtype, "0x0472"))
977 return &platforms[WRT160N];
979 /* default to WRT54G */
980 return &platforms[WRT54G];
982 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
983 return &platforms[WAP54GV2];
985 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
986 return &platforms[WL1600GL];
989 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
990 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
991 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
994 if (!strcmp(boardnum, "45")) { /* ASUS */
995 if (!strcmp(boardtype,"0x042f"))
996 return &platforms[WL500GP];
997 else if (!strcmp(boardtype,"0x0472"))
998 return &platforms[WL500W];
999 else if (!strcmp(boardtype,"0x467"))
1000 return &platforms[WL320GE];
1002 return &platforms[WL500GD];
1005 if (!strcmp(boardnum, "10496"))
1006 return &platforms[USR5461];
1008 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1009 return &platforms[SE505V2];
1011 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1012 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1013 return &platforms[DIR320];
1015 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1016 return &platforms[WDNetCenter];
1019 } else { /* PMON based - old stuff */
1020 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1021 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1022 return &platforms[WR850GV1];
1024 if (startswith(boardtype, "bcm94710dev")) {
1025 if (!strcmp(boardnum, "42"))
1026 return &platforms[WRT54GV1];
1027 if (simple_strtoul(boardnum, NULL, 0) == 2)
1028 return &platforms[WAP54GV1];
1030 if (startswith(getvar("hardware_version"), "WL500-"))
1031 return &platforms[WL500G];
1032 if (startswith(getvar("hardware_version"), "WL300-")) {
1033 /* Either WL-300g or WL-HDD, do more extensive checks */
1034 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1035 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1036 return &platforms[WLHDD];
1037 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1038 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1039 return &platforms[WL300G];
1041 /* Sitecom WL-105b */
1042 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1043 return &platforms[WL105B];
1045 /* unknown asus stuff, probably bcm4702 */
1046 if (startswith(boardnum, "asusX"))
1047 return &platforms[ASUS_4702];
1049 /* bcm4702 based Askey RT210W clones, Including:
1050 * Askey RT210W (duh?)
1052 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1054 if (!strcmp(boardtype,"bcm94710r4")
1055 && !strcmp(boardnum ,"100")
1056 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1058 return &platforms[RT210W];
1062 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1063 if (startswith(boardtype, "bcm94710ap"))
1064 return &platforms[BUFFALO_UNKNOWN_4710];
1066 return &platforms[BUFFALO_UNKNOWN];
1069 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1070 startswith(getvar("CFEver"), "MotoWRv3") ||
1071 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1073 return &platforms[WR850GV2V3];
1076 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1077 return &platforms[TEW411BRPP];
1080 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
1081 return &platforms[STI_NAS];
1083 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1084 return &platforms[DWL3150];
1090 static void register_buttons(struct button_t *b)
1092 for (; b->name; b++)
1093 platform.button_mask |= b->gpio;
1095 platform.button_mask &= ~gpiomask;
1097 gpio_outen(platform.button_mask, 0);
1098 gpio_control(platform.button_mask, 0);
1099 platform.button_polarity = gpio_in() & platform.button_mask;
1100 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1101 gpio_setintmask(platform.button_mask, platform.button_mask);
1103 gpio_set_irqenable(1, button_handler);
1106 static void unregister_buttons(struct button_t *b)
1108 gpio_setintmask(platform.button_mask, 0);
1110 gpio_set_irqenable(0, button_handler);
1115 static void add_msg(struct event_t *event, char *msg, int argv)
1122 s = skb_put(event->skb, strlen(msg) + 1);
1126 static void hotplug_button(struct work_struct *work)
1128 struct event_t *event = container_of(work, struct event_t, wq);
1134 event->skb = alloc_skb(2048, GFP_KERNEL);
1136 s = skb_put(event->skb, strlen(event->action) + 2);
1137 sprintf(s, "%s@", event->action);
1140 NETLINK_CB(event->skb).dst_group = 1;
1141 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1146 #else /* !LINUX_2_4 */
1147 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1151 p = kmalloc(size, gfp);
1160 static void add_msg(struct event_t *event, char *msg, int argv)
1163 event->argv[event->anr++] = event->scratch;
1165 event->envp[event->enr++] = event->scratch;
1167 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1170 static void hotplug_button(struct event_t *event)
1172 char *scratch = kzalloc(256, GFP_KERNEL);
1173 event->scratch = scratch;
1175 add_msg(event, hotplug_path, 1);
1176 add_msg(event, "button", 1);
1178 call_usermodehelper (event->argv[0], event->argv, event->envp);
1182 #endif /* !LINUX_2_4 */
1184 static int fill_event (struct event_t *event)
1186 static char buf[128];
1188 add_msg(event, "HOME=/", 0);
1189 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1190 add_msg(event, "SUBSYSTEM=button", 0);
1191 snprintf(buf, 128, "ACTION=%s", event->action);
1192 add_msg(event, buf, 0);
1193 snprintf(buf, 128, "BUTTON=%s", event->name);
1194 add_msg(event, buf, 0);
1195 snprintf(buf, 128, "SEEN=%ld", event->seen);
1196 add_msg(event, buf, 0);
1198 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1199 add_msg(event, buf, 0);
1207 static irqreturn_t button_handler(int irq, void *dev_id)
1209 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1215 in = gpio_in() & platform.button_mask;
1216 gpio_intpolarity(platform.button_mask, in);
1217 changed = platform.button_polarity ^ in;
1218 platform.button_polarity = in;
1220 changed &= ~gpio_outen(0, 0);
1222 for (b = platform.buttons; b->name; b++) {
1223 struct event_t *event;
1225 if (!(b->gpio & changed)) continue;
1229 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1230 event->seen = (jiffies - b->seen)/HZ;
1231 event->name = b->name;
1232 event->action = b->pressed ? "pressed" : "released";
1233 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1234 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1236 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1238 schedule_work(&event->wq);
1246 static void register_leds(struct led_t *l)
1248 struct proc_dir_entry *p;
1253 leds = proc_mkdir("led", diag);
1257 for(; l->name; l++) {
1258 if (l->gpio & gpiomask)
1261 if (l->gpio & GPIO_TYPE_EXTIF) {
1265 if (l->polarity != INPUT) oe_mask |= l->gpio;
1267 val |= (l->polarity == NORMAL)?0:l->gpio;
1270 if (l->polarity == INPUT) continue;
1272 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1273 l->proc.type = PROC_LED;
1275 p->data = (void *) &l->proc;
1276 p->proc_fops = &diag_proc_fops;
1280 gpio_outen(mask, oe_mask);
1281 gpio_control(mask, 0);
1282 gpio_out(mask, val);
1283 gpio_setintmask(mask, 0);
1286 static void unregister_leds(struct led_t *l)
1289 remove_proc_entry(l->name, leds);
1291 remove_proc_entry("led", diag);
1294 static void set_led_extif(struct led_t *led)
1296 gpio_set_extif(led->gpio, led->state);
1299 static void led_flash(unsigned long dummy) {
1304 for (l = platform.leds; l->name; l++) {
1306 if (l->gpio & GPIO_TYPE_EXTIF) {
1308 l->state = !l->state;
1318 u32 val = ~gpio_in();
1320 gpio_outen(mask, mask);
1321 gpio_control(mask, 0);
1322 gpio_out(mask, val);
1324 if (mask || extif_blink) {
1325 mod_timer(&led_timer, jiffies + FLASH_TIME);
1329 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1332 struct inode *inode = file->f_dentry->d_inode;
1333 struct proc_dir_entry *dent = inode->u.generic_ip;
1335 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1340 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1343 if (dent->data != NULL) {
1344 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1345 switch (handler->type) {
1347 struct led_t * led = (struct led_t *) handler->ptr;
1349 len = sprintf(page, "f\n");
1351 if (led->gpio & GPIO_TYPE_EXTIF) {
1352 len = sprintf(page, "%d\n", led->state);
1354 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1355 u8 p = (led->polarity == NORMAL ? 0 : 1);
1356 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1362 len = sprintf(page, "%s\n", platform.name);
1365 len = sprintf(page, "0x%04x\n", gpiomask);
1372 len = min_t(int, len - *ppos, count);
1373 if (copy_to_user(buf, (page + *ppos), len)) {
1387 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1390 struct inode *inode = file->f_dentry->d_inode;
1391 struct proc_dir_entry *dent = inode->u.generic_ip;
1393 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1398 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1401 if (copy_from_user(page, buf, count)) {
1407 if (dent->data != NULL) {
1408 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1409 switch (handler->type) {
1411 struct led_t *led = (struct led_t *) handler->ptr;
1412 int p = (led->polarity == NORMAL ? 0 : 1);
1414 if (page[0] == 'f') {
1419 if (led->gpio & GPIO_TYPE_EXTIF) {
1420 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1423 gpio_outen(led->gpio, led->gpio);
1424 gpio_control(led->gpio, 0);
1425 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1431 gpiomask = simple_strtoul(page, NULL, 0);
1433 if (platform.buttons) {
1434 unregister_buttons(platform.buttons);
1435 register_buttons(platform.buttons);
1438 if (platform.leds) {
1439 unregister_leds(platform.leds);
1440 register_leds(platform.leds);
1451 static int __init diag_init(void)
1453 static struct proc_dir_entry *p;
1454 static struct platform_t *detected;
1456 detected = platform_detect();
1458 printk(MODULE_NAME ": Router model not detected.\n");
1461 memcpy(&platform, detected, sizeof(struct platform_t));
1463 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1464 if (platform.platform_init != NULL) {
1465 platform.platform_init();
1468 if (!(diag = proc_mkdir("diag", NULL))) {
1469 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1473 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1474 p->data = (void *) &proc_model;
1475 p->proc_fops = &diag_proc_fops;
1478 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1479 p->data = (void *) &proc_gpiomask;
1480 p->proc_fops = &diag_proc_fops;
1483 if (platform.buttons)
1484 register_buttons(platform.buttons);
1487 register_leds(platform.leds);
1492 static void __exit diag_exit(void)
1494 del_timer(&led_timer);
1496 if (platform.buttons)
1497 unregister_buttons(platform.buttons);
1500 unregister_leds(platform.leds);
1502 remove_proc_entry("model", diag);
1503 remove_proc_entry("gpiomask", diag);
1504 remove_proc_entry("diag", NULL);
1507 module_init(diag_init);
1508 module_exit(diag_exit);
1510 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1511 MODULE_LICENSE("GPL");