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 = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
495 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
499 .name = "Siemens SE505 V2",
501 /* No usable buttons */
504 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
505 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
506 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
511 .name = "U.S. Robotics USR5461",
513 /* No usable buttons */
516 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
517 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
522 .name = "Dell TrueMobile 2300",
524 { .name = "reset", .gpio = 1 << 0 },
527 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
528 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
533 .name = "Motorola WE800G",
535 { .name = "reset", .gpio = 1 << 0 },
538 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
539 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
540 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
544 .name = "Motorola WR850G V1",
546 { .name = "reset", .gpio = 1 << 0 },
549 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
550 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
551 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
552 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
553 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
557 .name = "Motorola WR850G V2/V3",
559 { .name = "reset", .gpio = 1 << 5 },
562 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
563 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
564 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
565 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
569 .name = "Motorola WR850GP",
571 { .name = "reset", .gpio = 1 << 5 },
574 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
575 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
576 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
577 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
583 .name = "Belkin (unknown)",
584 /* FIXME: verify & add detection */
586 { .name = "reset", .gpio = 1 << 7 },
589 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
590 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
591 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
596 .name = "Netgear WGT634U",
598 { .name = "reset", .gpio = 1 << 2 },
601 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
606 .name = "Trendware TEW411BRP+",
608 { /* No usable buttons */ },
611 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
612 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
613 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
618 .name = "SimpleTech SimpleShare NAS",
620 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
621 { .name = "power", .gpio = 1 << 0 }, // on back
624 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
626 .platform_init = bcm4780_init,
630 .name = "D-Link DIR-130",
632 { .name = "reset", .gpio = 1 << 3},
633 { .name = "reserved", .gpio = 1 << 7},
636 { .name = "diag", .gpio = 1 << 0},
637 { .name = "blue", .gpio = 1 << 6},
641 .name = "D-Link DIR-330",
643 { .name = "reset", .gpio = 1 << 3},
644 { .name = "reserved", .gpio = 1 << 7},
647 { .name = "diag", .gpio = 1 << 0},
648 { .name = "usb", .gpio = 1 << 4},
649 { .name = "blue", .gpio = 1 << 6},
653 .name = "D-Link DWL-3150",
655 { .name = "reset", .gpio = 1 << 7},
658 { .name = "diag", .gpio = 1 << 2},
659 { .name = "status", .gpio = 1 << 1},
664 .name = "Sitecom WL-105b",
666 { .name = "reset", .gpio = 1 << 10},
669 { .name = "wlan", .gpio = 1 << 4},
670 { .name = "power", .gpio = 1 << 3},
675 static struct platform_t __init *platform_detect(void)
677 char *boardnum, *boardtype, *buf;
679 if (strcmp(getvar("nvram_type"), "cfe") == 0)
680 return &platforms[WGT634U];
682 /* Look for a model identifier */
684 /* Based on "model_name" */
685 if (buf = nvram_get("model_name")) {
686 if (!strcmp(buf, "DIR-130"))
687 return &platforms[DIR130];
688 if (!strcmp(buf, "DIR-330"))
689 return &platforms[DIR330];
692 /* Based on "model_no" */
693 if (buf = nvram_get("model_no")) {
694 if (startswith(buf,"WL700")) /* WL700* */
695 return &platforms[WL700GE];
698 /* Based on "hardware_version" */
699 if (buf = nvram_get("hardware_version")) {
700 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
701 return &platforms[WL500GPV2];
702 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
703 return &platforms[WL520GC];
704 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
705 return &platforms[WL520GU];
708 /* Based on "ModelId" */
709 if (buf = nvram_get("ModelId")) {
710 if (!strcmp(buf, "WR850GP"))
711 return &platforms[WR850GP];
712 if (!strcmp(buf,"WX-5565"))
713 return &platforms[TM2300];
714 if (startswith(buf,"WE800G")) /* WE800G* */
715 return &platforms[WE800G];
719 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
720 /* Buffalo hardware, check id for specific hardware matches */
721 if (!strcmp(buf, "29bb0332"))
722 return &platforms[WBR2_G54];
723 if (!strcmp(buf, "29129"))
724 return &platforms[WLA2_G54L];
725 if (!strcmp(buf, "30189"))
726 return &platforms[WHR_HP_G54];
727 if (!strcmp(buf, "32093"))
728 return &platforms[WHR_G125];
729 if (!strcmp(buf, "30182"))
730 return &platforms[WHR_G54S];
731 if (!strcmp(buf, "290441dd"))
732 return &platforms[WHR2_A54G54];
733 if (!strcmp(buf, "31120"))
734 return &platforms[WZR_G300N];
735 if (!strcmp(buf, "30083"))
736 return &platforms[WZR_RS_G54];
737 if (!strcmp(buf, "30103"))
738 return &platforms[WZR_RS_G54HP];
741 /* no easy model number, attempt to guess */
742 boardnum = getvar("boardnum");
743 boardtype = getvar("boardtype");
745 if (startswith(getvar("pmon_ver"), "CFE")) {
746 /* CFE based - newer hardware */
747 if (!strcmp(boardnum, "42")) { /* Linksys */
748 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
749 return &platforms[WRT350N];
751 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
752 return &platforms[WRT54G3G];
754 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
755 return &platforms[WRTSL54GS];
757 /* default to WRT54G */
758 return &platforms[WRT54G];
761 if (!strcmp(boardnum, "45")) { /* ASUS */
762 if (!strcmp(boardtype,"0x042f"))
763 return &platforms[WL500GP];
764 else if (!strcmp(boardtype,"0x0472"))
765 return &platforms[WL500W];
767 return &platforms[WL500GD];
770 if (!strcmp(boardnum, "10496"))
771 return &platforms[USR5461];
773 } else { /* PMON based - old stuff */
774 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
775 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
776 return &platforms[WR850GV1];
778 if (startswith(boardtype, "bcm94710dev")) {
779 if (!strcmp(boardnum, "42"))
780 return &platforms[WRT54GV1];
781 if (simple_strtoul(boardnum, NULL, 0) == 2)
782 return &platforms[WAP54GV1];
784 if (startswith(getvar("hardware_version"), "WL500-"))
785 return &platforms[WL500G];
786 if (startswith(getvar("hardware_version"), "WL300-")) {
787 /* Either WL-300g or WL-HDD, do more extensive checks */
788 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
789 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
790 return &platforms[WLHDD];
791 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
792 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
793 return &platforms[WL300G];
795 /* Sitecom WL-105b */
796 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
797 return &platforms[WL105B];
799 /* unknown asus stuff, probably bcm4702 */
800 if (startswith(boardnum, "asusX"))
801 return &platforms[ASUS_4702];
804 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
805 if (startswith(boardtype, "bcm94710ap"))
806 return &platforms[BUFFALO_UNKNOWN_4710];
808 return &platforms[BUFFALO_UNKNOWN];
811 if (startswith(getvar("CFEver"), "MotoWRv2") ||
812 startswith(getvar("CFEver"), "MotoWRv3") ||
813 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
815 return &platforms[WR850GV2V3];
818 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
819 return &platforms[TEW411BRPP];
822 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
823 return &platforms[STI_NAS];
825 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
826 return &platforms[DWL3150];
832 static void register_buttons(struct button_t *b)
835 platform.button_mask |= b->gpio;
837 platform.button_mask &= ~gpiomask;
839 gpio_outen(platform.button_mask, 0);
840 gpio_control(platform.button_mask, 0);
841 platform.button_polarity = gpio_in() & platform.button_mask;
842 gpio_intpolarity(platform.button_mask, platform.button_polarity);
843 gpio_intmask(platform.button_mask, platform.button_mask);
845 gpio_set_irqenable(1, button_handler);
848 static void unregister_buttons(struct button_t *b)
850 gpio_intmask(platform.button_mask, 0);
852 gpio_set_irqenable(0, button_handler);
857 static void add_msg(struct event_t *event, char *msg, int argv)
864 s = skb_put(event->skb, strlen(msg) + 1);
868 static void hotplug_button(struct work_struct *work)
870 struct event_t *event = container_of(work, struct event_t, wq);
876 event->skb = alloc_skb(2048, GFP_KERNEL);
878 s = skb_put(event->skb, strlen(event->action) + 2);
879 sprintf(s, "%s@", event->action);
882 NETLINK_CB(event->skb).dst_group = 1;
883 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
888 #else /* !LINUX_2_4 */
889 static inline char *kzalloc(unsigned int size, unsigned int gfp)
893 p = kmalloc(size, gfp);
902 static void add_msg(struct event_t *event, char *msg, int argv)
905 event->argv[event->anr++] = event->scratch;
907 event->envp[event->enr++] = event->scratch;
909 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
912 static void hotplug_button(struct event_t *event)
914 char *scratch = kzalloc(256, GFP_KERNEL);
915 event->scratch = scratch;
917 add_msg(event, hotplug_path, 1);
918 add_msg(event, "button", 1);
920 call_usermodehelper (event->argv[0], event->argv, event->envp);
924 #endif /* !LINUX_2_4 */
926 static int fill_event (struct event_t *event)
928 static char buf[128];
930 add_msg(event, "HOME=/", 0);
931 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
932 add_msg(event, "SUBSYSTEM=button", 0);
933 snprintf(buf, 128, "ACTION=%s", event->action);
934 add_msg(event, buf, 0);
935 snprintf(buf, 128, "BUTTON=%s", event->name);
936 add_msg(event, buf, 0);
937 snprintf(buf, 128, "SEEN=%ld", event->seen);
938 add_msg(event, buf, 0);
940 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
941 add_msg(event, buf, 0);
949 static irqreturn_t button_handler(int irq, void *dev_id)
951 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
957 in = gpio_in() & platform.button_mask;
958 gpio_intpolarity(platform.button_mask, in);
959 changed = platform.button_polarity ^ in;
960 platform.button_polarity = in;
962 changed &= ~gpio_outen(0, 0);
964 for (b = platform.buttons; b->name; b++) {
965 struct event_t *event;
967 if (!(b->gpio & changed)) continue;
971 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
972 event->seen = (jiffies - b->seen)/HZ;
973 event->name = b->name;
974 event->action = b->pressed ? "pressed" : "released";
975 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
976 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
978 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
980 schedule_work(&event->wq);
988 static void register_leds(struct led_t *l)
990 struct proc_dir_entry *p;
995 leds = proc_mkdir("led", diag);
999 for(; l->name; l++) {
1000 if (l->gpio & gpiomask)
1003 if (l->gpio & GPIO_TYPE_EXTIF) {
1007 if (l->polarity != INPUT) oe_mask |= l->gpio;
1009 val |= (l->polarity == NORMAL)?0:l->gpio;
1012 if (l->polarity == INPUT) continue;
1014 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1015 l->proc.type = PROC_LED;
1017 p->data = (void *) &l->proc;
1018 p->proc_fops = &diag_proc_fops;
1022 gpio_outen(mask, oe_mask);
1023 gpio_control(mask, 0);
1024 gpio_out(mask, val);
1027 static void unregister_leds(struct led_t *l)
1030 remove_proc_entry(l->name, leds);
1032 remove_proc_entry("led", diag);
1035 static void set_led_extif(struct led_t *led)
1037 gpio_set_extif(led->gpio, led->state);
1040 static void led_flash(unsigned long dummy) {
1045 for (l = platform.leds; l->name; l++) {
1047 if (l->gpio & GPIO_TYPE_EXTIF) {
1049 l->state = !l->state;
1059 u32 val = ~gpio_in();
1061 gpio_outen(mask, mask);
1062 gpio_control(mask, 0);
1063 gpio_out(mask, val);
1065 if (mask || extif_blink) {
1066 mod_timer(&led_timer, jiffies + FLASH_TIME);
1070 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1073 struct inode *inode = file->f_dentry->d_inode;
1074 struct proc_dir_entry *dent = inode->u.generic_ip;
1076 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1081 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1084 if (dent->data != NULL) {
1085 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1086 switch (handler->type) {
1088 struct led_t * led = (struct led_t *) handler->ptr;
1090 len = sprintf(page, "f\n");
1092 if (led->gpio & GPIO_TYPE_EXTIF) {
1093 len = sprintf(page, "%d\n", led->state);
1095 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1096 u8 p = (led->polarity == NORMAL ? 0 : 1);
1097 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1103 len = sprintf(page, "%s\n", platform.name);
1106 len = sprintf(page, "0x%04x\n", gpiomask);
1113 len = min_t(int, len - *ppos, count);
1114 if (copy_to_user(buf, (page + *ppos), len)) {
1128 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1131 struct inode *inode = file->f_dentry->d_inode;
1132 struct proc_dir_entry *dent = inode->u.generic_ip;
1134 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1139 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1142 if (copy_from_user(page, buf, count)) {
1148 if (dent->data != NULL) {
1149 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1150 switch (handler->type) {
1152 struct led_t *led = (struct led_t *) handler->ptr;
1153 int p = (led->polarity == NORMAL ? 0 : 1);
1155 if (page[0] == 'f') {
1160 if (led->gpio & GPIO_TYPE_EXTIF) {
1161 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1164 gpio_outen(led->gpio, led->gpio);
1165 gpio_control(led->gpio, 0);
1166 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1172 gpiomask = simple_strtoul(page, NULL, 0);
1174 if (platform.buttons) {
1175 unregister_buttons(platform.buttons);
1176 register_buttons(platform.buttons);
1179 if (platform.leds) {
1180 unregister_leds(platform.leds);
1181 register_leds(platform.leds);
1192 static int __init diag_init(void)
1194 static struct proc_dir_entry *p;
1195 static struct platform_t *detected;
1197 detected = platform_detect();
1199 printk(MODULE_NAME ": Router model not detected.\n");
1202 memcpy(&platform, detected, sizeof(struct platform_t));
1204 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1205 if (platform.platform_init != NULL) {
1206 platform.platform_init();
1209 if (!(diag = proc_mkdir("diag", NULL))) {
1210 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1214 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1215 p->data = (void *) &proc_model;
1216 p->proc_fops = &diag_proc_fops;
1219 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1220 p->data = (void *) &proc_gpiomask;
1221 p->proc_fops = &diag_proc_fops;
1224 if (platform.buttons)
1225 register_buttons(platform.buttons);
1228 register_leds(platform.leds);
1233 static void __exit diag_exit(void)
1235 del_timer(&led_timer);
1237 if (platform.buttons)
1238 unregister_buttons(platform.buttons);
1241 unregister_leds(platform.leds);
1243 remove_proc_entry("model", diag);
1244 remove_proc_entry("gpiomask", diag);
1245 remove_proc_entry("diag", NULL);
1248 module_init(diag_init);
1249 module_exit(diag_exit);
1251 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1252 MODULE_LICENSE("GPL");