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);
127 static void __init bcm4780_init(void) {
130 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
131 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
132 gpio_outen(pin, pin);
133 gpio_control(pin, 0);
136 /* Wait 5s, so the HDD can spin up */
137 set_current_state(TASK_INTERRUPTIBLE);
138 schedule_timeout(HZ * 5);
141 static void __init bcm57xx_init(void) {
144 gpio_outen(pin, pin);
145 gpio_control(pin, 0);
149 static struct platform_t __initdata platforms[] = {
152 .name = "Linksys WAP54G V1",
154 { .name = "reset", .gpio = 1 << 0 },
157 { .name = "diag", .gpio = 1 << 3 },
158 { .name = "wlan", .gpio = 1 << 4 },
162 .name = "Linksys WAP54G V3",
164 /* FIXME: verify this */
165 { .name = "reset", .gpio = 1 << 7 },
166 { .name = "ses", .gpio = 1 << 0 },
170 { .name = "ses", .gpio = 1 << 1 },
174 .name = "Linksys WRT54G V1.x",
176 { .name = "reset", .gpio = 1 << 6 },
179 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
180 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
184 .name = "Linksys WRT54G/GS/GL",
186 { .name = "reset", .gpio = 1 << 6 },
187 { .name = "ses", .gpio = 1 << 4 },
190 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
191 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
192 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
193 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
194 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
198 .name = "Linksys WRTSL54GS",
200 { .name = "reset", .gpio = 1 << 6 },
201 { .name = "ses", .gpio = 1 << 4 },
204 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
205 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
206 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
207 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
211 .name = "Linksys WRT54G3G",
213 { .name = "reset", .gpio = 1 << 6 },
214 { .name = "3g", .gpio = 1 << 4 },
217 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
218 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
219 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
220 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
221 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
225 .name = "Linksys WRT350N",
227 { .name = "reset", .gpio = 1 << 6 },
228 { .name = "ses", .gpio = 1 << 8 },
231 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
232 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
233 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
234 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
235 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
237 .platform_init = bcm57xx_init,
241 .name = "ASUS WL-HDD",
243 { .name = "reset", .gpio = 1 << 6 },
246 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
247 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
251 .name = "ASUS WL-300g",
253 { .name = "reset", .gpio = 1 << 6 },
256 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
260 .name = "ASUS WL-500g",
262 { .name = "reset", .gpio = 1 << 6 },
265 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
269 .name = "ASUS WL-500g Deluxe",
271 { .name = "reset", .gpio = 1 << 6 },
274 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
278 .name = "ASUS WL-500g Premium",
280 { .name = "reset", .gpio = 1 << 0 },
281 { .name = "ses", .gpio = 1 << 4 },
284 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
288 .name = "ASUS WL-500W",
290 { .name = "reset", .gpio = 1 << 6 },
291 { .name = "ses", .gpio = 1 << 7 },
294 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
298 .name = "ASUS WL-520gU",
300 { .name = "reset", .gpio = 1 << 2 },
301 { .name = "ses", .gpio = 1 << 3 },
304 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
305 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
309 .name = "ASUS (unknown, BCM4702)",
311 { .name = "reset", .gpio = 1 << 6 },
314 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
318 .name = "ASUS WL-700gE",
320 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
321 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
322 { .name = "power", .gpio = 1 << 0 }, // on front
323 { .name = "copy", .gpio = 1 << 6 }, // on front
327 // GPIO that controls power led also enables/disables some essential functions
330 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
332 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
334 .platform_init = bcm4780_init,
338 .name = "Buffalo WHR-G54S",
340 { .name = "reset", .gpio = 1 << 4 },
341 { .name = "bridge", .gpio = 1 << 5 },
342 { .name = "ses", .gpio = 1 << 0 },
345 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
346 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
347 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
348 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
349 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
353 .name = "Buffalo WBR2-G54",
356 { .name = "reset", .gpio = 1 << 7 },
359 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
363 .name = "Buffalo WHR-HP-G54",
365 { .name = "reset", .gpio = 1 << 4 },
366 { .name = "bridge", .gpio = 1 << 5 },
367 { .name = "ses", .gpio = 1 << 0 },
370 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
371 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
372 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
373 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
374 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
378 .name = "Buffalo WHR-G125",
380 { .name = "reset", .gpio = 1 << 4 },
381 { .name = "bridge", .gpio = 1 << 5 },
382 { .name = "ses", .gpio = 1 << 0 },
385 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
386 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
387 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
388 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
389 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
393 .name = "Buffalo WHR2-A54G54",
395 { .name = "reset", .gpio = 1 << 4 },
398 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
402 .name = "Buffalo WLA2-G54L",
405 { .name = "reset", .gpio = 1 << 7 },
408 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
412 .name = "Buffalo WZR-G300N",
414 { .name = "reset", .gpio = 1 << 4 },
417 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
418 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
419 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
423 .name = "Buffalo WZR-RS-G54",
425 { .name = "ses", .gpio = 1 << 0 },
426 { .name = "reset", .gpio = 1 << 4 },
429 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
430 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
431 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
435 .name = "Buffalo WZR-RS-G54HP",
437 { .name = "ses", .gpio = 1 << 0 },
438 { .name = "reset", .gpio = 1 << 4 },
441 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
442 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
443 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
446 [BUFFALO_UNKNOWN] = {
447 .name = "Buffalo (unknown)",
449 { .name = "reset", .gpio = 1 << 7 },
452 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
455 [BUFFALO_UNKNOWN_4710] = {
456 .name = "Buffalo (unknown, BCM4710)",
458 { .name = "reset", .gpio = 1 << 4 },
461 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
466 .name = "Siemens SE505 V1",
468 /* No usable buttons */
471 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
472 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
476 .name = "Siemens SE505 V2",
478 /* No usable buttons */
481 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
482 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
483 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
488 .name = "U.S. Robotics USR5461",
490 /* No usable buttons */
493 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
494 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
499 .name = "Dell TrueMobile 2300",
501 { .name = "reset", .gpio = 1 << 0 },
504 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
505 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
510 .name = "Motorola WE800G",
512 { .name = "reset", .gpio = 1 << 0 },
515 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
516 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
517 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
521 .name = "Motorola WR850G V1",
523 { .name = "reset", .gpio = 1 << 0 },
526 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
527 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
528 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
529 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
530 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
534 .name = "Motorola WR850G V2/V3",
536 { .name = "reset", .gpio = 1 << 5 },
539 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
540 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
541 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
542 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
546 .name = "Motorola WR850GP",
548 { .name = "reset", .gpio = 1 << 5 },
551 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
552 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
553 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
554 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
560 .name = "Belkin (unknown)",
561 /* FIXME: verify & add detection */
563 { .name = "reset", .gpio = 1 << 7 },
566 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
567 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
568 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
573 .name = "Netgear WGT634U",
575 { .name = "reset", .gpio = 1 << 2 },
578 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
583 .name = "Trendware TEW411BRP+",
585 { /* No usable buttons */ },
588 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
589 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
590 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
595 .name = "SimpleTech SimpleShare NAS",
597 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
598 { .name = "power", .gpio = 1 << 0 }, // on back
601 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
603 .platform_init = bcm4780_init,
607 .name = "D-Link DIR-130",
609 { .name = "reset", .gpio = 1 << 3},
610 { .name = "reserved", .gpio = 1 << 7},
613 { .name = "diag", .gpio = 1 << 0},
614 { .name = "blue", .gpio = 1 << 6},
618 .name = "D-Link DIR-330",
620 { .name = "reset", .gpio = 1 << 3},
621 { .name = "reserved", .gpio = 1 << 7},
624 { .name = "diag", .gpio = 1 << 0},
625 { .name = "usb", .gpio = 1 << 4},
626 { .name = "blue", .gpio = 1 << 6},
630 .name = "D-Link DWL-3150",
632 { .name = "reset", .gpio = 1 << 7},
635 { .name = "diag", .gpio = 1 << 2},
636 { .name = "status", .gpio = 1 << 1},
641 .name = "Sitecom WL-105b",
643 { .name = "reset", .gpio = 1 << 10},
646 { .name = "wlan", .gpio = 1 << 4},
647 { .name = "power", .gpio = 1 << 3},
652 static struct platform_t __init *platform_detect(void)
654 char *boardnum, *boardtype, *buf;
656 if (strcmp(getvar("nvram_type"), "cfe") == 0)
657 return &platforms[WGT634U];
659 /* Look for a model identifier */
661 /* Based on "model_name" */
662 if (buf = nvram_get("model_name")) {
663 if (!strcmp(buf, "DIR-130"))
664 return &platforms[DIR130];
665 if (!strcmp(buf, "DIR-330"))
666 return &platforms[DIR330];
669 /* Based on "model_no" */
670 if (buf = nvram_get("model_no")) {
671 if (startswith(buf,"WL700")) /* WL700* */
672 return &platforms[WL700GE];
675 /* Based on "hardware_version" */
676 if (buf = nvram_get("hardware_version")) {
677 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
678 return &platforms[WL520GU];
681 /* Based on "ModelId" */
682 if (buf = nvram_get("ModelId")) {
683 if (!strcmp(buf, "WR850GP"))
684 return &platforms[WR850GP];
685 if (!strcmp(buf,"WX-5565"))
686 return &platforms[TM2300];
687 if (startswith(buf,"WE800G")) /* WE800G* */
688 return &platforms[WE800G];
692 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
693 /* Buffalo hardware, check id for specific hardware matches */
694 if (!strcmp(buf, "29bb0332"))
695 return &platforms[WBR2_G54];
696 if (!strcmp(buf, "29129"))
697 return &platforms[WLA2_G54L];
698 if (!strcmp(buf, "30189"))
699 return &platforms[WHR_HP_G54];
700 if (!strcmp(buf, "32093"))
701 return &platforms[WHR_G125];
702 if (!strcmp(buf, "30182"))
703 return &platforms[WHR_G54S];
704 if (!strcmp(buf, "290441dd"))
705 return &platforms[WHR2_A54G54];
706 if (!strcmp(buf, "31120"))
707 return &platforms[WZR_G300N];
708 if (!strcmp(buf, "30083"))
709 return &platforms[WZR_RS_G54];
710 if (!strcmp(buf, "30103"))
711 return &platforms[WZR_RS_G54HP];
714 /* no easy model number, attempt to guess */
715 boardnum = getvar("boardnum");
716 boardtype = getvar("boardtype");
718 if (startswith(getvar("pmon_ver"), "CFE")) {
719 /* CFE based - newer hardware */
720 if (!strcmp(boardnum, "42")) { /* Linksys */
721 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
722 return &platforms[WRT350N];
724 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
725 return &platforms[WRT54G3G];
727 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
728 return &platforms[WRTSL54GS];
730 /* default to WRT54G */
731 return &platforms[WRT54G];
734 if (!strcmp(boardnum, "45")) { /* ASUS */
735 if (!strcmp(boardtype,"0x042f"))
736 return &platforms[WL500GP];
737 else if (!strcmp(boardtype,"0x0472"))
738 return &platforms[WL500W];
740 return &platforms[WL500GD];
743 if (!strcmp(boardnum, "10496"))
744 return &platforms[USR5461];
746 } else { /* PMON based - old stuff */
747 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
748 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
749 return &platforms[WR850GV1];
751 if (startswith(boardtype, "bcm94710dev")) {
752 if (!strcmp(boardnum, "42"))
753 return &platforms[WRT54GV1];
754 if (simple_strtoul(boardnum, NULL, 0) == 2)
755 return &platforms[WAP54GV1];
757 if (startswith(getvar("hardware_version"), "WL500-"))
758 return &platforms[WL500G];
759 if (startswith(getvar("hardware_version"), "WL300-")) {
760 /* Either WL-300g or WL-HDD, do more extensive checks */
761 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
762 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
763 return &platforms[WLHDD];
764 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
765 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
766 return &platforms[WL300G];
768 /* Sitecom WL-105b */
769 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
770 return &platforms[WL105B];
772 /* unknown asus stuff, probably bcm4702 */
773 if (startswith(boardnum, "asusX"))
774 return &platforms[ASUS_4702];
777 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
778 if (startswith(boardtype, "bcm94710ap"))
779 return &platforms[BUFFALO_UNKNOWN_4710];
781 return &platforms[BUFFALO_UNKNOWN];
784 if (startswith(getvar("CFEver"), "MotoWRv2") ||
785 startswith(getvar("CFEver"), "MotoWRv3") ||
786 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
788 return &platforms[WR850GV2V3];
791 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
792 return &platforms[TEW411BRPP];
795 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
796 return &platforms[STI_NAS];
798 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
799 return &platforms[DWL3150];
805 static void register_buttons(struct button_t *b)
808 platform.button_mask |= b->gpio;
810 platform.button_mask &= ~gpiomask;
812 gpio_outen(platform.button_mask, 0);
813 gpio_control(platform.button_mask, 0);
814 platform.button_polarity = gpio_in() & platform.button_mask;
815 gpio_intpolarity(platform.button_mask, platform.button_polarity);
816 gpio_intmask(platform.button_mask, platform.button_mask);
818 gpio_set_irqenable(1, button_handler);
821 static void unregister_buttons(struct button_t *b)
823 gpio_intmask(platform.button_mask, 0);
825 gpio_set_irqenable(0, button_handler);
830 static void add_msg(struct event_t *event, char *msg, int argv)
837 s = skb_put(event->skb, strlen(msg) + 1);
841 static void hotplug_button(struct work_struct *work)
843 struct event_t *event = container_of(work, struct event_t, wq);
849 event->skb = alloc_skb(2048, GFP_KERNEL);
851 s = skb_put(event->skb, strlen(event->action) + 2);
852 sprintf(s, "%s@", event->action);
855 NETLINK_CB(event->skb).dst_group = 1;
856 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
861 #else /* !LINUX_2_4 */
862 static inline char *kzalloc(unsigned int size, unsigned int gfp)
866 p = kmalloc(size, gfp);
875 static void add_msg(struct event_t *event, char *msg, int argv)
878 event->argv[event->anr++] = event->scratch;
880 event->envp[event->enr++] = event->scratch;
882 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
885 static void hotplug_button(struct event_t *event)
887 char *scratch = kzalloc(256, GFP_KERNEL);
888 event->scratch = scratch;
890 add_msg(event, hotplug_path, 1);
891 add_msg(event, "button", 1);
893 call_usermodehelper (event->argv[0], event->argv, event->envp);
897 #endif /* !LINUX_2_4 */
899 static int fill_event (struct event_t *event)
901 static char buf[128];
903 add_msg(event, "HOME=/", 0);
904 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
905 add_msg(event, "SUBSYSTEM=button", 0);
906 snprintf(buf, 128, "ACTION=%s", event->action);
907 add_msg(event, buf, 0);
908 snprintf(buf, 128, "BUTTON=%s", event->name);
909 add_msg(event, buf, 0);
910 snprintf(buf, 128, "SEEN=%ld", event->seen);
911 add_msg(event, buf, 0);
913 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
914 add_msg(event, buf, 0);
922 static irqreturn_t button_handler(int irq, void *dev_id)
924 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
930 in = gpio_in() & platform.button_mask;
931 gpio_intpolarity(platform.button_mask, in);
932 changed = platform.button_polarity ^ in;
933 platform.button_polarity = in;
935 changed &= ~gpio_outen(0, 0);
937 for (b = platform.buttons; b->name; b++) {
938 struct event_t *event;
940 if (!(b->gpio & changed)) continue;
944 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
945 event->seen = (jiffies - b->seen)/HZ;
946 event->name = b->name;
947 event->action = b->pressed ? "pressed" : "released";
948 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
949 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
951 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
953 schedule_work(&event->wq);
961 static void register_leds(struct led_t *l)
963 struct proc_dir_entry *p;
968 leds = proc_mkdir("led", diag);
972 for(; l->name; l++) {
973 if (l->gpio & gpiomask)
976 if (l->gpio & GPIO_TYPE_EXTIF) {
980 if (l->polarity != INPUT) oe_mask != l->gpio;
982 val |= (l->polarity == NORMAL)?0:l->gpio;
985 if (l->polarity == INPUT) continue;
987 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
988 l->proc.type = PROC_LED;
990 p->data = (void *) &l->proc;
991 p->proc_fops = &diag_proc_fops;
995 gpio_outen(mask, oe_mask);
996 gpio_control(mask, 0);
1000 static void unregister_leds(struct led_t *l)
1003 remove_proc_entry(l->name, leds);
1005 remove_proc_entry("led", diag);
1008 static void set_led_extif(struct led_t *led)
1010 gpio_set_extif(led->gpio, led->state);
1013 static void led_flash(unsigned long dummy) {
1018 for (l = platform.leds; l->name; l++) {
1020 if (l->gpio & GPIO_TYPE_EXTIF) {
1022 l->state = !l->state;
1032 u32 val = ~gpio_in();
1034 gpio_outen(mask, mask);
1035 gpio_control(mask, 0);
1036 gpio_out(mask, val);
1038 if (mask || extif_blink) {
1039 mod_timer(&led_timer, jiffies + FLASH_TIME);
1043 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1046 struct inode *inode = file->f_dentry->d_inode;
1047 struct proc_dir_entry *dent = inode->u.generic_ip;
1049 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1054 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1057 if (dent->data != NULL) {
1058 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1059 switch (handler->type) {
1061 struct led_t * led = (struct led_t *) handler->ptr;
1063 len = sprintf(page, "f\n");
1065 if (led->gpio & GPIO_TYPE_EXTIF) {
1066 len = sprintf(page, "%d\n", led->state);
1068 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1069 u8 p = (led->polarity == NORMAL ? 0 : 1);
1070 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1076 len = sprintf(page, "%s\n", platform.name);
1079 len = sprintf(page, "0x%04x\n", gpiomask);
1086 len = min_t(int, len - *ppos, count);
1087 if (copy_to_user(buf, (page + *ppos), len)) {
1101 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1104 struct inode *inode = file->f_dentry->d_inode;
1105 struct proc_dir_entry *dent = inode->u.generic_ip;
1107 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1112 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1115 if (copy_from_user(page, buf, count)) {
1121 if (dent->data != NULL) {
1122 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1123 switch (handler->type) {
1125 struct led_t *led = (struct led_t *) handler->ptr;
1126 int p = (led->polarity == NORMAL ? 0 : 1);
1128 if (page[0] == 'f') {
1133 if (led->gpio & GPIO_TYPE_EXTIF) {
1134 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1137 gpio_outen(led->gpio, led->gpio);
1138 gpio_control(led->gpio, 0);
1139 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1145 gpiomask = simple_strtoul(page, NULL, 0);
1147 if (platform.buttons) {
1148 unregister_buttons(platform.buttons);
1149 register_buttons(platform.buttons);
1152 if (platform.leds) {
1153 unregister_leds(platform.leds);
1154 register_leds(platform.leds);
1165 static int __init diag_init(void)
1167 static struct proc_dir_entry *p;
1168 static struct platform_t *detected;
1170 detected = platform_detect();
1172 printk(MODULE_NAME ": Router model not detected.\n");
1175 memcpy(&platform, detected, sizeof(struct platform_t));
1177 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1178 if (platform.platform_init != NULL) {
1179 platform.platform_init();
1182 if (!(diag = proc_mkdir("diag", NULL))) {
1183 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1187 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1188 p->data = (void *) &proc_model;
1189 p->proc_fops = &diag_proc_fops;
1192 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1193 p->data = (void *) &proc_gpiomask;
1194 p->proc_fops = &diag_proc_fops;
1197 if (platform.buttons)
1198 register_buttons(platform.buttons);
1201 register_leds(platform.leds);
1206 static void __exit diag_exit(void)
1208 del_timer(&led_timer);
1210 if (platform.buttons)
1211 unregister_buttons(platform.buttons);
1214 unregister_leds(platform.leds);
1216 remove_proc_entry("model", diag);
1217 remove_proc_entry("gpiomask", diag);
1218 remove_proc_entry("diag", NULL);
1221 module_init(diag_init);
1222 module_exit(diag_exit);
1224 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1225 MODULE_LICENSE("GPL");