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.
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kmod.h>
27 #include <linux/proc_fs.h>
28 #include <linux/timer.h>
29 #include <linux/version.h>
30 #include <asm/uaccess.h>
33 #include <linux/workqueue.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
37 extern struct sock *uevent_sock;
38 extern u64 uevent_next_seqnum(void);
40 #include <linux/tqueue.h>
41 #define INIT_WORK INIT_TQUEUE
42 #define schedule_work schedule_task
43 #define work_struct tq_struct
48 #define getvar(str) (nvram_get(str)?:"")
50 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
51 static int fill_event(struct event_t *);
52 static unsigned int gpiomask = 0;
53 module_param(gpiomask, int, 0644);
128 static void __init bcm4780_init(void) {
131 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
132 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
133 gpio_outen(pin, pin);
134 gpio_control(pin, 0);
137 /* Wait 5s, so the HDD can spin up */
138 set_current_state(TASK_INTERRUPTIBLE);
139 schedule_timeout(HZ * 5);
142 static void __init bcm57xx_init(void) {
145 gpio_outen(pin, pin);
146 gpio_control(pin, 0);
150 static struct platform_t __initdata platforms[] = {
153 .name = "Linksys WAP54G V1",
155 { .name = "reset", .gpio = 1 << 0 },
158 { .name = "diag", .gpio = 1 << 3 },
159 { .name = "wlan", .gpio = 1 << 4 },
163 .name = "Linksys WAP54G V3",
165 /* FIXME: verify this */
166 { .name = "reset", .gpio = 1 << 7 },
167 { .name = "ses", .gpio = 1 << 0 },
171 { .name = "ses", .gpio = 1 << 1 },
175 .name = "Linksys WRT54G V1.x",
177 { .name = "reset", .gpio = 1 << 6 },
180 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
181 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
185 .name = "Linksys WRT54G/GS/GL",
187 { .name = "reset", .gpio = 1 << 6 },
188 { .name = "ses", .gpio = 1 << 4 },
191 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
192 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
193 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
194 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
195 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
199 .name = "Linksys WRTSL54GS",
201 { .name = "reset", .gpio = 1 << 6 },
202 { .name = "ses", .gpio = 1 << 4 },
205 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
206 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
207 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
208 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
212 .name = "Linksys WRT54G3G",
214 { .name = "reset", .gpio = 1 << 6 },
215 { .name = "3g", .gpio = 1 << 4 },
218 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
219 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
220 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
221 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
222 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
226 .name = "Linksys WRT350N",
228 { .name = "reset", .gpio = 1 << 6 },
229 { .name = "ses", .gpio = 1 << 8 },
232 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
233 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
234 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
235 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
236 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
238 .platform_init = bcm57xx_init,
242 .name = "ASUS WL-HDD",
244 { .name = "reset", .gpio = 1 << 6 },
247 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
248 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
252 .name = "ASUS WL-300g",
254 { .name = "reset", .gpio = 1 << 6 },
257 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
261 .name = "ASUS WL-500g",
263 { .name = "reset", .gpio = 1 << 6 },
266 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
270 .name = "ASUS WL-500g Deluxe",
272 { .name = "reset", .gpio = 1 << 6 },
275 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
279 .name = "ASUS WL-500g Premium",
281 { .name = "reset", .gpio = 1 << 0 },
282 { .name = "ses", .gpio = 1 << 4 },
285 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
289 .name = "ASUS WL-500g Premium V2",
291 { .name = "reset", .gpio = 1 << 2 },
292 { .name = "ses", .gpio = 1 << 3 },
295 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
296 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
300 .name = "ASUS WL-500W",
302 { .name = "reset", .gpio = 1 << 6 },
303 { .name = "ses", .gpio = 1 << 7 },
306 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
310 .name = "ASUS WL-520GC",
312 { .name = "reset", .gpio = 1 << 2 },
313 { .name = "ses", .gpio = 1 << 3 },
316 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
317 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
321 .name = "ASUS WL-520gU",
323 { .name = "reset", .gpio = 1 << 2 },
324 { .name = "ses", .gpio = 1 << 3 },
327 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
328 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
332 .name = "ASUS (unknown, BCM4702)",
334 { .name = "reset", .gpio = 1 << 6 },
337 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
341 .name = "ASUS WL-700gE",
343 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
344 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
345 { .name = "power", .gpio = 1 << 0 }, // on front
346 { .name = "copy", .gpio = 1 << 6 }, // on front
350 // GPIO that controls power led also enables/disables some essential functions
353 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
355 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
357 .platform_init = bcm4780_init,
361 .name = "Buffalo WHR-G54S",
363 { .name = "reset", .gpio = 1 << 4 },
364 { .name = "bridge", .gpio = 1 << 5 },
365 { .name = "ses", .gpio = 1 << 0 },
368 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
369 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
370 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
371 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
372 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
376 .name = "Buffalo WBR2-G54",
379 { .name = "reset", .gpio = 1 << 7 },
382 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
386 .name = "Buffalo WHR-HP-G54",
388 { .name = "reset", .gpio = 1 << 4 },
389 { .name = "bridge", .gpio = 1 << 5 },
390 { .name = "ses", .gpio = 1 << 0 },
393 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
394 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
395 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
396 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
397 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
401 .name = "Buffalo WHR-G125",
403 { .name = "reset", .gpio = 1 << 4 },
404 { .name = "bridge", .gpio = 1 << 5 },
405 { .name = "ses", .gpio = 1 << 0 },
408 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
409 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
410 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
411 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
412 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
416 .name = "Buffalo WHR2-A54G54",
418 { .name = "reset", .gpio = 1 << 4 },
421 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
425 .name = "Buffalo WLA2-G54L",
428 { .name = "reset", .gpio = 1 << 7 },
431 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
435 .name = "Buffalo WZR-G300N",
437 { .name = "reset", .gpio = 1 << 4 },
440 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
441 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
442 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
446 .name = "Buffalo WZR-RS-G54",
448 { .name = "ses", .gpio = 1 << 0 },
449 { .name = "reset", .gpio = 1 << 4 },
452 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
453 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
454 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
458 .name = "Buffalo WZR-RS-G54HP",
460 { .name = "ses", .gpio = 1 << 0 },
461 { .name = "reset", .gpio = 1 << 4 },
464 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
465 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
466 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
469 [BUFFALO_UNKNOWN] = {
470 .name = "Buffalo (unknown)",
472 { .name = "reset", .gpio = 1 << 7 },
475 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
478 [BUFFALO_UNKNOWN_4710] = {
479 .name = "Buffalo (unknown, BCM4710)",
481 { .name = "reset", .gpio = 1 << 4 },
484 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
489 .name = "Siemens SE505 V1",
491 /* No usable buttons */
494 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
495 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
496 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
500 .name = "Siemens SE505 V2",
502 /* No usable buttons */
505 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
506 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
507 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
512 .name = "U.S. Robotics USR5461",
514 /* No usable buttons */
517 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
518 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
523 .name = "Dell TrueMobile 2300",
525 { .name = "reset", .gpio = 1 << 0 },
528 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
529 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
534 .name = "Motorola WE800G",
536 { .name = "reset", .gpio = 1 << 0 },
539 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
540 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
541 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
545 .name = "Motorola WR850G V1",
547 { .name = "reset", .gpio = 1 << 0 },
550 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
551 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
552 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
553 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
554 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
558 .name = "Motorola WR850G V2/V3",
560 { .name = "reset", .gpio = 1 << 5 },
563 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
564 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
565 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
566 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
570 .name = "Motorola WR850GP",
572 { .name = "reset", .gpio = 1 << 5 },
575 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
576 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
577 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
578 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
584 .name = "Belkin (unknown)",
585 /* FIXME: verify & add detection */
587 { .name = "reset", .gpio = 1 << 7 },
590 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
591 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
592 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
597 .name = "Netgear WGT634U",
599 { .name = "reset", .gpio = 1 << 2 },
602 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
607 .name = "Trendware TEW411BRP+",
609 { /* No usable buttons */ },
612 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
613 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
614 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
619 .name = "SimpleTech SimpleShare NAS",
621 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
622 { .name = "power", .gpio = 1 << 0 }, // on back
625 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
627 .platform_init = bcm4780_init,
631 .name = "D-Link DIR-130",
633 { .name = "reset", .gpio = 1 << 3},
634 { .name = "reserved", .gpio = 1 << 7},
637 { .name = "diag", .gpio = 1 << 0},
638 { .name = "blue", .gpio = 1 << 6},
642 .name = "D-Link DIR-330",
644 { .name = "reset", .gpio = 1 << 3},
645 { .name = "reserved", .gpio = 1 << 7},
648 { .name = "diag", .gpio = 1 << 0},
649 { .name = "usb", .gpio = 1 << 4},
650 { .name = "blue", .gpio = 1 << 6},
654 .name = "D-Link DWL-3150",
656 { .name = "reset", .gpio = 1 << 7},
659 { .name = "diag", .gpio = 1 << 2},
660 { .name = "status", .gpio = 1 << 1},
665 .name = "Sitecom WL-105b",
667 { .name = "reset", .gpio = 1 << 10},
670 { .name = "wlan", .gpio = 1 << 4},
671 { .name = "power", .gpio = 1 << 3},
676 static struct platform_t __init *platform_detect(void)
678 char *boardnum, *boardtype, *buf;
680 if (strcmp(getvar("nvram_type"), "cfe") == 0)
681 return &platforms[WGT634U];
683 /* Look for a model identifier */
685 /* Based on "model_name" */
686 if ((buf = nvram_get("model_name"))) {
687 if (!strcmp(buf, "DIR-130"))
688 return &platforms[DIR130];
689 if (!strcmp(buf, "DIR-330"))
690 return &platforms[DIR330];
693 /* Based on "model_no" */
694 if ((buf = nvram_get("model_no"))) {
695 if (startswith(buf,"WL700")) /* WL700* */
696 return &platforms[WL700GE];
699 /* Based on "hardware_version" */
700 if ((buf = nvram_get("hardware_version"))) {
701 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
702 return &platforms[WL500GPV2];
703 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
704 return &platforms[WL520GC];
705 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
706 return &platforms[WL520GU];
709 /* Based on "ModelId" */
710 if ((buf = nvram_get("ModelId"))) {
711 if (!strcmp(buf, "WR850GP"))
712 return &platforms[WR850GP];
713 if (!strcmp(buf,"WX-5565"))
714 return &platforms[TM2300];
715 if (startswith(buf,"WE800G")) /* WE800G* */
716 return &platforms[WE800G];
720 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
721 /* Buffalo hardware, check id for specific hardware matches */
722 if (!strcmp(buf, "29bb0332"))
723 return &platforms[WBR2_G54];
724 if (!strcmp(buf, "29129"))
725 return &platforms[WLA2_G54L];
726 if (!strcmp(buf, "30189"))
727 return &platforms[WHR_HP_G54];
728 if (!strcmp(buf, "32093"))
729 return &platforms[WHR_G125];
730 if (!strcmp(buf, "30182"))
731 return &platforms[WHR_G54S];
732 if (!strcmp(buf, "290441dd"))
733 return &platforms[WHR2_A54G54];
734 if (!strcmp(buf, "31120"))
735 return &platforms[WZR_G300N];
736 if (!strcmp(buf, "30083"))
737 return &platforms[WZR_RS_G54];
738 if (!strcmp(buf, "30103"))
739 return &platforms[WZR_RS_G54HP];
742 /* no easy model number, attempt to guess */
743 boardnum = getvar("boardnum");
744 boardtype = getvar("boardtype");
746 if (startswith(getvar("pmon_ver"), "CFE")) {
747 /* CFE based - newer hardware */
748 if (!strcmp(boardnum, "42")) { /* Linksys */
749 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
750 return &platforms[WRT350N];
752 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
753 return &platforms[WRT54G3G];
755 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
756 return &platforms[WRTSL54GS];
758 /* default to WRT54G */
759 return &platforms[WRT54G];
762 if (!strcmp(boardnum, "45")) { /* ASUS */
763 if (!strcmp(boardtype,"0x042f"))
764 return &platforms[WL500GP];
765 else if (!strcmp(boardtype,"0x0472"))
766 return &platforms[WL500W];
768 return &platforms[WL500GD];
771 if (!strcmp(boardnum, "10496"))
772 return &platforms[USR5461];
774 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
775 return &platforms[SE505V2];
777 } else { /* PMON based - old stuff */
778 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
779 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
780 return &platforms[WR850GV1];
782 if (startswith(boardtype, "bcm94710dev")) {
783 if (!strcmp(boardnum, "42"))
784 return &platforms[WRT54GV1];
785 if (simple_strtoul(boardnum, NULL, 0) == 2)
786 return &platforms[WAP54GV1];
788 if (startswith(getvar("hardware_version"), "WL500-"))
789 return &platforms[WL500G];
790 if (startswith(getvar("hardware_version"), "WL300-")) {
791 /* Either WL-300g or WL-HDD, do more extensive checks */
792 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
793 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
794 return &platforms[WLHDD];
795 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
796 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
797 return &platforms[WL300G];
799 /* Sitecom WL-105b */
800 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
801 return &platforms[WL105B];
803 /* unknown asus stuff, probably bcm4702 */
804 if (startswith(boardnum, "asusX"))
805 return &platforms[ASUS_4702];
808 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
809 if (startswith(boardtype, "bcm94710ap"))
810 return &platforms[BUFFALO_UNKNOWN_4710];
812 return &platforms[BUFFALO_UNKNOWN];
815 if (startswith(getvar("CFEver"), "MotoWRv2") ||
816 startswith(getvar("CFEver"), "MotoWRv3") ||
817 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
819 return &platforms[WR850GV2V3];
822 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
823 return &platforms[TEW411BRPP];
826 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
827 return &platforms[STI_NAS];
829 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
830 return &platforms[DWL3150];
836 static void register_buttons(struct button_t *b)
839 platform.button_mask |= b->gpio;
841 platform.button_mask &= ~gpiomask;
843 gpio_outen(platform.button_mask, 0);
844 gpio_control(platform.button_mask, 0);
845 platform.button_polarity = gpio_in() & platform.button_mask;
846 gpio_intpolarity(platform.button_mask, platform.button_polarity);
847 gpio_intmask(platform.button_mask, platform.button_mask);
849 gpio_set_irqenable(1, button_handler);
852 static void unregister_buttons(struct button_t *b)
854 gpio_intmask(platform.button_mask, 0);
856 gpio_set_irqenable(0, button_handler);
861 static void add_msg(struct event_t *event, char *msg, int argv)
868 s = skb_put(event->skb, strlen(msg) + 1);
872 static void hotplug_button(struct work_struct *work)
874 struct event_t *event = container_of(work, struct event_t, wq);
880 event->skb = alloc_skb(2048, GFP_KERNEL);
882 s = skb_put(event->skb, strlen(event->action) + 2);
883 sprintf(s, "%s@", event->action);
886 NETLINK_CB(event->skb).dst_group = 1;
887 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
892 #else /* !LINUX_2_4 */
893 static inline char *kzalloc(unsigned int size, unsigned int gfp)
897 p = kmalloc(size, gfp);
906 static void add_msg(struct event_t *event, char *msg, int argv)
909 event->argv[event->anr++] = event->scratch;
911 event->envp[event->enr++] = event->scratch;
913 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
916 static void hotplug_button(struct event_t *event)
918 char *scratch = kzalloc(256, GFP_KERNEL);
919 event->scratch = scratch;
921 add_msg(event, hotplug_path, 1);
922 add_msg(event, "button", 1);
924 call_usermodehelper (event->argv[0], event->argv, event->envp);
928 #endif /* !LINUX_2_4 */
930 static int fill_event (struct event_t *event)
932 static char buf[128];
934 add_msg(event, "HOME=/", 0);
935 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
936 add_msg(event, "SUBSYSTEM=button", 0);
937 snprintf(buf, 128, "ACTION=%s", event->action);
938 add_msg(event, buf, 0);
939 snprintf(buf, 128, "BUTTON=%s", event->name);
940 add_msg(event, buf, 0);
941 snprintf(buf, 128, "SEEN=%ld", event->seen);
942 add_msg(event, buf, 0);
944 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
945 add_msg(event, buf, 0);
953 static irqreturn_t button_handler(int irq, void *dev_id)
955 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
961 in = gpio_in() & platform.button_mask;
962 gpio_intpolarity(platform.button_mask, in);
963 changed = platform.button_polarity ^ in;
964 platform.button_polarity = in;
966 changed &= ~gpio_outen(0, 0);
968 for (b = platform.buttons; b->name; b++) {
969 struct event_t *event;
971 if (!(b->gpio & changed)) continue;
975 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
976 event->seen = (jiffies - b->seen)/HZ;
977 event->name = b->name;
978 event->action = b->pressed ? "pressed" : "released";
979 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
980 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
982 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
984 schedule_work(&event->wq);
992 static void register_leds(struct led_t *l)
994 struct proc_dir_entry *p;
999 leds = proc_mkdir("led", diag);
1003 for(; l->name; l++) {
1004 if (l->gpio & gpiomask)
1007 if (l->gpio & GPIO_TYPE_EXTIF) {
1011 if (l->polarity != INPUT) oe_mask |= l->gpio;
1013 val |= (l->polarity == NORMAL)?0:l->gpio;
1016 if (l->polarity == INPUT) continue;
1018 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1019 l->proc.type = PROC_LED;
1021 p->data = (void *) &l->proc;
1022 p->proc_fops = &diag_proc_fops;
1026 gpio_outen(mask, oe_mask);
1027 gpio_control(mask, 0);
1028 gpio_out(mask, val);
1031 static void unregister_leds(struct led_t *l)
1034 remove_proc_entry(l->name, leds);
1036 remove_proc_entry("led", diag);
1039 static void set_led_extif(struct led_t *led)
1041 gpio_set_extif(led->gpio, led->state);
1044 static void led_flash(unsigned long dummy) {
1049 for (l = platform.leds; l->name; l++) {
1051 if (l->gpio & GPIO_TYPE_EXTIF) {
1053 l->state = !l->state;
1063 u32 val = ~gpio_in();
1065 gpio_outen(mask, mask);
1066 gpio_control(mask, 0);
1067 gpio_out(mask, val);
1069 if (mask || extif_blink) {
1070 mod_timer(&led_timer, jiffies + FLASH_TIME);
1074 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1077 struct inode *inode = file->f_dentry->d_inode;
1078 struct proc_dir_entry *dent = inode->u.generic_ip;
1080 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1085 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1088 if (dent->data != NULL) {
1089 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1090 switch (handler->type) {
1092 struct led_t * led = (struct led_t *) handler->ptr;
1094 len = sprintf(page, "f\n");
1096 if (led->gpio & GPIO_TYPE_EXTIF) {
1097 len = sprintf(page, "%d\n", led->state);
1099 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1100 u8 p = (led->polarity == NORMAL ? 0 : 1);
1101 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1107 len = sprintf(page, "%s\n", platform.name);
1110 len = sprintf(page, "0x%04x\n", gpiomask);
1117 len = min_t(int, len - *ppos, count);
1118 if (copy_to_user(buf, (page + *ppos), len)) {
1132 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1135 struct inode *inode = file->f_dentry->d_inode;
1136 struct proc_dir_entry *dent = inode->u.generic_ip;
1138 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1143 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1146 if (copy_from_user(page, buf, count)) {
1152 if (dent->data != NULL) {
1153 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1154 switch (handler->type) {
1156 struct led_t *led = (struct led_t *) handler->ptr;
1157 int p = (led->polarity == NORMAL ? 0 : 1);
1159 if (page[0] == 'f') {
1164 if (led->gpio & GPIO_TYPE_EXTIF) {
1165 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1168 gpio_outen(led->gpio, led->gpio);
1169 gpio_control(led->gpio, 0);
1170 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1176 gpiomask = simple_strtoul(page, NULL, 0);
1178 if (platform.buttons) {
1179 unregister_buttons(platform.buttons);
1180 register_buttons(platform.buttons);
1183 if (platform.leds) {
1184 unregister_leds(platform.leds);
1185 register_leds(platform.leds);
1196 static int __init diag_init(void)
1198 static struct proc_dir_entry *p;
1199 static struct platform_t *detected;
1201 detected = platform_detect();
1203 printk(MODULE_NAME ": Router model not detected.\n");
1206 memcpy(&platform, detected, sizeof(struct platform_t));
1208 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1209 if (platform.platform_init != NULL) {
1210 platform.platform_init();
1213 if (!(diag = proc_mkdir("diag", NULL))) {
1214 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1218 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1219 p->data = (void *) &proc_model;
1220 p->proc_fops = &diag_proc_fops;
1223 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1224 p->data = (void *) &proc_gpiomask;
1225 p->proc_fops = &diag_proc_fops;
1228 if (platform.buttons)
1229 register_buttons(platform.buttons);
1232 register_leds(platform.leds);
1237 static void __exit diag_exit(void)
1239 del_timer(&led_timer);
1241 if (platform.buttons)
1242 unregister_buttons(platform.buttons);
1245 unregister_leds(platform.leds);
1247 remove_proc_entry("model", diag);
1248 remove_proc_entry("gpiomask", diag);
1249 remove_proc_entry("diag", NULL);
1252 module_init(diag_init);
1253 module_exit(diag_exit);
1255 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1256 MODULE_LICENSE("GPL");