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);
125 static void __init bcm4780_init(void) {
128 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
129 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
130 gpio_outen(pin, pin);
131 gpio_control(pin, 0);
134 /* Wait 5s, so the HDD can spin up */
135 set_current_state(TASK_INTERRUPTIBLE);
136 schedule_timeout(HZ * 5);
139 static void __init bcm57xx_init(void) {
142 gpio_outen(pin, pin);
143 gpio_control(pin, 0);
147 static struct platform_t __initdata platforms[] = {
150 .name = "Linksys WAP54G V1",
152 { .name = "reset", .gpio = 1 << 0 },
155 { .name = "diag", .gpio = 1 << 3 },
156 { .name = "wlan", .gpio = 1 << 4 },
160 .name = "Linksys WAP54G V3",
162 /* FIXME: verify this */
163 { .name = "reset", .gpio = 1 << 7 },
164 { .name = "ses", .gpio = 1 << 0 },
168 { .name = "ses", .gpio = 1 << 1 },
172 .name = "Linksys WRT54G V1.x",
174 { .name = "reset", .gpio = 1 << 6 },
177 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
178 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
182 .name = "Linksys WRT54G/GS/GL",
184 { .name = "reset", .gpio = 1 << 6 },
185 { .name = "ses", .gpio = 1 << 4 },
188 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
189 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
190 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
191 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
192 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
196 .name = "Linksys WRTSL54GS",
198 { .name = "reset", .gpio = 1 << 6 },
199 { .name = "ses", .gpio = 1 << 4 },
202 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
203 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
204 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
205 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
209 .name = "Linksys WRT54G3G",
211 { .name = "reset", .gpio = 1 << 6 },
212 { .name = "3g", .gpio = 1 << 4 },
215 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
216 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
217 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
218 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
219 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
223 .name = "Linksys WRT350N",
225 { .name = "reset", .gpio = 1 << 6 },
226 { .name = "ses", .gpio = 1 << 8 },
229 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
230 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
231 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
232 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
233 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
235 .platform_init = bcm57xx_init,
239 .name = "ASUS WL-HDD",
241 { .name = "reset", .gpio = 1 << 6 },
244 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
245 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
249 .name = "ASUS WL-300g",
251 { .name = "reset", .gpio = 1 << 6 },
254 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
258 .name = "ASUS WL-500g",
260 { .name = "reset", .gpio = 1 << 6 },
263 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
267 .name = "ASUS WL-500g Deluxe",
269 { .name = "reset", .gpio = 1 << 6 },
272 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
276 .name = "ASUS WL-500g Premium",
278 { .name = "reset", .gpio = 1 << 0 },
279 { .name = "ses", .gpio = 1 << 4 },
282 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
286 .name = "ASUS WL-500W",
288 { .name = "reset", .gpio = 1 << 6 },
289 { .name = "ses", .gpio = 1 << 7 },
292 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
296 .name = "ASUS (unknown, BCM4702)",
298 { .name = "reset", .gpio = 1 << 6 },
301 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
305 .name = "ASUS WL-700gE",
307 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
308 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
309 { .name = "power", .gpio = 1 << 0 }, // on front
310 { .name = "copy", .gpio = 1 << 6 }, // on front
314 // GPIO that controls power led also enables/disables some essential functions
317 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
319 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
321 .platform_init = bcm4780_init,
325 .name = "Buffalo WHR-G54S",
327 { .name = "reset", .gpio = 1 << 4 },
328 { .name = "bridge", .gpio = 1 << 5 },
329 { .name = "ses", .gpio = 1 << 0 },
332 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
333 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
334 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
335 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
336 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
340 .name = "Buffalo WBR2-G54",
343 { .name = "reset", .gpio = 1 << 7 },
346 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
350 .name = "Buffalo WHR-HP-G54",
352 { .name = "reset", .gpio = 1 << 4 },
353 { .name = "bridge", .gpio = 1 << 5 },
354 { .name = "ses", .gpio = 1 << 0 },
357 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
358 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
359 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
360 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
361 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
365 .name = "Buffalo WHR-G125",
367 { .name = "reset", .gpio = 1 << 4 },
368 { .name = "bridge", .gpio = 1 << 5 },
369 { .name = "ses", .gpio = 1 << 0 },
372 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
373 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
374 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
375 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
376 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
380 .name = "Buffalo WHR2-A54G54",
382 { .name = "reset", .gpio = 1 << 4 },
385 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
389 .name = "Buffalo WLA2-G54L",
392 { .name = "reset", .gpio = 1 << 7 },
395 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
399 .name = "Buffalo WZR-G300N",
401 { .name = "reset", .gpio = 1 << 4 },
404 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
405 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
406 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
410 .name = "Buffalo WZR-RS-G54",
412 { .name = "ses", .gpio = 1 << 0 },
413 { .name = "reset", .gpio = 1 << 4 },
416 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
417 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
418 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
422 .name = "Buffalo WZR-RS-G54HP",
424 { .name = "ses", .gpio = 1 << 0 },
425 { .name = "reset", .gpio = 1 << 4 },
428 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
429 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
430 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
433 [BUFFALO_UNKNOWN] = {
434 .name = "Buffalo (unknown)",
436 { .name = "reset", .gpio = 1 << 7 },
439 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
442 [BUFFALO_UNKNOWN_4710] = {
443 .name = "Buffalo (unknown, BCM4710)",
445 { .name = "reset", .gpio = 1 << 4 },
448 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
453 .name = "Siemens SE505 V1",
455 /* No usable buttons */
458 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
459 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
463 .name = "Siemens SE505 V2",
465 /* No usable buttons */
468 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
469 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
470 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
475 .name = "U.S. Robotics USR5461",
477 /* No usable buttons */
480 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
481 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
486 .name = "Dell TrueMobile 2300",
488 { .name = "reset", .gpio = 1 << 0 },
491 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
492 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
497 .name = "Motorola WE800G",
499 { .name = "reset", .gpio = 1 << 0 },
502 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
503 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
504 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
508 .name = "Motorola WR850G V1",
510 { .name = "reset", .gpio = 1 << 0 },
513 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
514 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
515 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
516 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
517 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
521 .name = "Motorola WR850G V2/V3",
523 { .name = "reset", .gpio = 1 << 5 },
526 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
527 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
528 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
529 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
533 .name = "Motorola WR850GP",
535 { .name = "reset", .gpio = 1 << 5 },
538 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
539 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
540 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
541 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
547 .name = "Belkin (unknown)",
548 /* FIXME: verify & add detection */
550 { .name = "reset", .gpio = 1 << 7 },
553 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
554 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
555 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
560 .name = "Netgear WGT634U",
562 { .name = "reset", .gpio = 1 << 2 },
565 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
570 .name = "Trendware TEW411BRP+",
572 { /* No usable buttons */ },
575 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
576 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
577 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
582 .name = "SimpleTech SimpleShare NAS",
584 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
585 { .name = "power", .gpio = 1 << 0 }, // on back
588 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
590 .platform_init = bcm4780_init,
594 .name = "D-Link DIR-130",
596 { .name = "reset", .gpio = 1 << 3},
597 { .name = "reserved", .gpio = 1 << 7},
600 { .name = "diag", .gpio = 1 << 0},
601 { .name = "blue", .gpio = 1 << 6},
605 .name = "D-Link DIR-330",
607 { .name = "reset", .gpio = 1 << 3},
608 { .name = "reserved", .gpio = 1 << 7},
611 { .name = "diag", .gpio = 1 << 0},
612 { .name = "usb", .gpio = 1 << 4},
613 { .name = "blue", .gpio = 1 << 6},
617 .name = "D-Link DWL-3150",
619 { .name = "reset", .gpio = 1 << 7},
622 { .name = "diag", .gpio = 1 << 2},
623 { .name = "status", .gpio = 1 << 1},
628 .name = "Sitecom WL-105b",
630 { .name = "reset", .gpio = 1 << 10},
633 { .name = "wlan", .gpio = 1 << 4},
634 { .name = "power", .gpio = 1 << 3},
639 static struct platform_t __init *platform_detect(void)
641 char *boardnum, *boardtype, *buf;
643 if (strcmp(getvar("nvram_type"), "cfe") == 0)
644 return &platforms[WGT634U];
646 /* Look for a model identifier */
648 /* Based on "model_name" */
649 if (buf = nvram_get("model_name")) {
650 if (!strcmp(buf, "DIR-130"))
651 return &platforms[DIR130];
652 if (!strcmp(buf, "DIR-330"))
653 return &platforms[DIR330];
656 /* Based on "model_no" */
657 if (buf = nvram_get("model_no")) {
658 if (startswith(buf,"WL700")) /* WL700* */
659 return &platforms[WL700GE];
662 /* Based on "ModelId" */
663 if (buf = nvram_get("ModelId")) {
664 if (!strcmp(buf, "WR850GP"))
665 return &platforms[WR850GP];
666 if (!strcmp(buf,"WX-5565"))
667 return &platforms[TM2300];
668 if (startswith(buf,"WE800G")) /* WE800G* */
669 return &platforms[WE800G];
673 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
674 /* Buffalo hardware, check id for specific hardware matches */
675 if (!strcmp(buf, "29bb0332"))
676 return &platforms[WBR2_G54];
677 if (!strcmp(buf, "29129"))
678 return &platforms[WLA2_G54L];
679 if (!strcmp(buf, "30189"))
680 return &platforms[WHR_HP_G54];
681 if (!strcmp(buf, "32093"))
682 return &platforms[WHR_G125];
683 if (!strcmp(buf, "30182"))
684 return &platforms[WHR_G54S];
685 if (!strcmp(buf, "290441dd"))
686 return &platforms[WHR2_A54G54];
687 if (!strcmp(buf, "31120"))
688 return &platforms[WZR_G300N];
689 if (!strcmp(buf, "30083"))
690 return &platforms[WZR_RS_G54];
691 if (!strcmp(buf, "30103"))
692 return &platforms[WZR_RS_G54HP];
695 /* no easy model number, attempt to guess */
696 boardnum = getvar("boardnum");
697 boardtype = getvar("boardtype");
699 if (startswith(getvar("pmon_ver"), "CFE")) {
700 /* CFE based - newer hardware */
701 if (!strcmp(boardnum, "42")) { /* Linksys */
702 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
703 return &platforms[WRT350N];
705 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
706 return &platforms[WRT54G3G];
708 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
709 return &platforms[WRTSL54GS];
711 /* default to WRT54G */
712 return &platforms[WRT54G];
715 if (!strcmp(boardnum, "45")) { /* ASUS */
716 if (!strcmp(boardtype,"0x042f"))
717 return &platforms[WL500GP];
718 else if (!strcmp(boardtype,"0x0472"))
719 return &platforms[WL500W];
721 return &platforms[WL500GD];
724 if (!strcmp(boardnum, "10496"))
725 return &platforms[USR5461];
727 } else { /* PMON based - old stuff */
728 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
729 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
730 return &platforms[WR850GV1];
732 if (startswith(boardtype, "bcm94710dev")) {
733 if (!strcmp(boardnum, "42"))
734 return &platforms[WRT54GV1];
735 if (simple_strtoul(boardnum, NULL, 0) == 2)
736 return &platforms[WAP54GV1];
738 if (startswith(getvar("hardware_version"), "WL500-"))
739 return &platforms[WL500G];
740 if (startswith(getvar("hardware_version"), "WL300-")) {
741 /* Either WL-300g or WL-HDD, do more extensive checks */
742 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
743 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
744 return &platforms[WLHDD];
745 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
746 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
747 return &platforms[WL300G];
749 /* Sitecom WL-105b */
750 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
751 return &platforms[WL105B];
753 /* unknown asus stuff, probably bcm4702 */
754 if (startswith(boardnum, "asusX"))
755 return &platforms[ASUS_4702];
758 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
759 if (startswith(boardtype, "bcm94710ap"))
760 return &platforms[BUFFALO_UNKNOWN_4710];
762 return &platforms[BUFFALO_UNKNOWN];
765 if (startswith(getvar("CFEver"), "MotoWRv2") ||
766 startswith(getvar("CFEver"), "MotoWRv3") ||
767 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
769 return &platforms[WR850GV2V3];
772 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
773 return &platforms[TEW411BRPP];
776 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
777 return &platforms[STI_NAS];
779 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
780 return &platforms[DWL3150];
786 static void register_buttons(struct button_t *b)
789 platform.button_mask |= b->gpio;
791 platform.button_mask &= ~gpiomask;
793 gpio_outen(platform.button_mask, 0);
794 gpio_control(platform.button_mask, 0);
795 platform.button_polarity = gpio_in() & platform.button_mask;
796 gpio_intpolarity(platform.button_mask, platform.button_polarity);
797 gpio_intmask(platform.button_mask, platform.button_mask);
799 gpio_set_irqenable(1, button_handler);
802 static void unregister_buttons(struct button_t *b)
804 gpio_intmask(platform.button_mask, 0);
806 gpio_set_irqenable(0, button_handler);
811 static void add_msg(struct event_t *event, char *msg, int argv)
818 s = skb_put(event->skb, strlen(msg) + 1);
822 static void hotplug_button(struct work_struct *work)
824 struct event_t *event = container_of(work, struct event_t, wq);
830 event->skb = alloc_skb(2048, GFP_KERNEL);
832 s = skb_put(event->skb, strlen(event->action) + 2);
833 sprintf(s, "%s@", event->action);
836 NETLINK_CB(event->skb).dst_group = 1;
837 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
842 #else /* !LINUX_2_4 */
843 static inline char *kzalloc(unsigned int size, unsigned int gfp)
847 p = kmalloc(size, gfp);
856 static void add_msg(struct event_t *event, char *msg, int argv)
859 event->argv[event->anr++] = event->scratch;
861 event->envp[event->enr++] = event->scratch;
863 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
866 static void hotplug_button(struct event_t *event)
868 char *scratch = kzalloc(256, GFP_KERNEL);
869 event->scratch = scratch;
871 add_msg(event, hotplug_path, 1);
872 add_msg(event, "button", 1);
874 call_usermodehelper (event->argv[0], event->argv, event->envp);
878 #endif /* !LINUX_2_4 */
880 static int fill_event (struct event_t *event)
882 static char buf[128];
884 add_msg(event, "HOME=/", 0);
885 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
886 add_msg(event, "SUBSYSTEM=button", 0);
887 snprintf(buf, 128, "ACTION=%s", event->action);
888 add_msg(event, buf, 0);
889 snprintf(buf, 128, "BUTTON=%s", event->name);
890 add_msg(event, buf, 0);
891 snprintf(buf, 128, "SEEN=%ld", event->seen);
892 add_msg(event, buf, 0);
894 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
895 add_msg(event, buf, 0);
903 static irqreturn_t button_handler(int irq, void *dev_id)
905 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
911 in = gpio_in() & platform.button_mask;
912 gpio_intpolarity(platform.button_mask, in);
913 changed = platform.button_polarity ^ in;
914 platform.button_polarity = in;
916 changed &= ~gpio_outen(0, 0);
918 for (b = platform.buttons; b->name; b++) {
919 struct event_t *event;
921 if (!(b->gpio & changed)) continue;
925 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
926 event->seen = (jiffies - b->seen)/HZ;
927 event->name = b->name;
928 event->action = b->pressed ? "pressed" : "released";
929 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
930 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
932 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
934 schedule_work(&event->wq);
942 static void register_leds(struct led_t *l)
944 struct proc_dir_entry *p;
949 leds = proc_mkdir("led", diag);
953 for(; l->name; l++) {
954 if (l->gpio & gpiomask)
957 if (l->gpio & GPIO_TYPE_EXTIF) {
961 if (l->polarity != INPUT) oe_mask != l->gpio;
963 val |= (l->polarity == NORMAL)?0:l->gpio;
966 if (l->polarity == INPUT) continue;
968 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
969 l->proc.type = PROC_LED;
971 p->data = (void *) &l->proc;
972 p->proc_fops = &diag_proc_fops;
976 gpio_outen(mask, oe_mask);
977 gpio_control(mask, 0);
981 static void unregister_leds(struct led_t *l)
984 remove_proc_entry(l->name, leds);
986 remove_proc_entry("led", diag);
989 static void set_led_extif(struct led_t *led)
991 gpio_set_extif(led->gpio, led->state);
994 static void led_flash(unsigned long dummy) {
999 for (l = platform.leds; l->name; l++) {
1001 if (l->gpio & GPIO_TYPE_EXTIF) {
1003 l->state = !l->state;
1013 u32 val = ~gpio_in();
1015 gpio_outen(mask, mask);
1016 gpio_control(mask, 0);
1017 gpio_out(mask, val);
1019 if (mask || extif_blink) {
1020 mod_timer(&led_timer, jiffies + FLASH_TIME);
1024 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1027 struct inode *inode = file->f_dentry->d_inode;
1028 struct proc_dir_entry *dent = inode->u.generic_ip;
1030 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1035 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1038 if (dent->data != NULL) {
1039 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1040 switch (handler->type) {
1042 struct led_t * led = (struct led_t *) handler->ptr;
1044 len = sprintf(page, "f\n");
1046 if (led->gpio & GPIO_TYPE_EXTIF) {
1047 len = sprintf(page, "%d\n", led->state);
1049 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1050 u8 p = (led->polarity == NORMAL ? 0 : 1);
1051 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1057 len = sprintf(page, "%s\n", platform.name);
1060 len = sprintf(page, "0x%04x\n", gpiomask);
1067 len = min_t(int, len - *ppos, count);
1068 if (copy_to_user(buf, (page + *ppos), len)) {
1082 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1085 struct inode *inode = file->f_dentry->d_inode;
1086 struct proc_dir_entry *dent = inode->u.generic_ip;
1088 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1093 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1096 if (copy_from_user(page, buf, count)) {
1102 if (dent->data != NULL) {
1103 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1104 switch (handler->type) {
1106 struct led_t *led = (struct led_t *) handler->ptr;
1107 int p = (led->polarity == NORMAL ? 0 : 1);
1109 if (page[0] == 'f') {
1114 if (led->gpio & GPIO_TYPE_EXTIF) {
1115 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1118 gpio_outen(led->gpio, led->gpio);
1119 gpio_control(led->gpio, 0);
1120 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1126 gpiomask = simple_strtoul(page, NULL, 0);
1128 if (platform.buttons) {
1129 unregister_buttons(platform.buttons);
1130 register_buttons(platform.buttons);
1133 if (platform.leds) {
1134 unregister_leds(platform.leds);
1135 register_leds(platform.leds);
1146 static int __init diag_init(void)
1148 static struct proc_dir_entry *p;
1149 static struct platform_t *detected;
1151 detected = platform_detect();
1153 printk(MODULE_NAME ": Router model not detected.\n");
1156 memcpy(&platform, detected, sizeof(struct platform_t));
1158 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1159 if (platform.platform_init != NULL) {
1160 platform.platform_init();
1163 if (!(diag = proc_mkdir("diag", NULL))) {
1164 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1168 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1169 p->data = (void *) &proc_model;
1170 p->proc_fops = &diag_proc_fops;
1173 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1174 p->data = (void *) &proc_gpiomask;
1175 p->proc_fops = &diag_proc_fops;
1178 if (platform.buttons)
1179 register_buttons(platform.buttons);
1182 register_leds(platform.leds);
1187 static void __exit diag_exit(void)
1189 del_timer(&led_timer);
1191 if (platform.buttons)
1192 unregister_buttons(platform.buttons);
1195 unregister_leds(platform.leds);
1197 remove_proc_entry("model", diag);
1198 remove_proc_entry("gpiomask", diag);
1199 remove_proc_entry("diag", NULL);
1202 module_init(diag_init);
1203 module_exit(diag_exit);
1205 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1206 MODULE_LICENSE("GPL");