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>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/kmod.h>
26 #include <linux/proc_fs.h>
27 #include <linux/timer.h>
28 #include <linux/version.h>
29 #include <asm/uaccess.h>
32 #include <linux/workqueue.h>
33 #include <linux/skbuff.h>
34 #include <linux/netlink.h>
36 extern struct sock *uevent_sock;
37 extern u64 uevent_next_seqnum(void);
39 #include <linux/tqueue.h>
40 #define INIT_WORK INIT_TQUEUE
41 #define schedule_work schedule_task
42 #define work_struct tq_struct
47 #define getvar(str) (nvram_get(str)?:"")
49 static int fill_event(struct event_t *);
50 static unsigned int gpiomask = 0;
51 module_param(gpiomask, int, 0644);
123 static void __init bcm4780_init(void) {
126 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
127 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
128 gpio_outen(pin, pin);
129 gpio_control(pin, 0);
132 /* Wait 5s, so the HDD can spin up */
133 set_current_state(TASK_INTERRUPTIBLE);
134 schedule_timeout(HZ * 5);
137 static void __init bcm57xx_init(void) {
140 gpio_outen(pin, pin);
141 gpio_control(pin, 0);
145 static struct platform_t __initdata platforms[] = {
148 .name = "Linksys WAP54G V1",
150 { .name = "reset", .gpio = 1 << 0 },
153 { .name = "diag", .gpio = 1 << 3 },
154 { .name = "wlan", .gpio = 1 << 4 },
158 .name = "Linksys WAP54G V3",
160 /* FIXME: verify this */
161 { .name = "reset", .gpio = 1 << 7 },
162 { .name = "ses", .gpio = 1 << 0 },
166 { .name = "ses", .gpio = 1 << 1 },
170 .name = "Linksys WRT54G V1.x",
172 { .name = "reset", .gpio = 1 << 6 },
175 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
176 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
180 .name = "Linksys WRT54G/GS/GL",
182 { .name = "reset", .gpio = 1 << 6 },
183 { .name = "ses", .gpio = 1 << 4 },
186 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
187 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
188 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
189 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
190 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
194 .name = "Linksys WRTSL54GS",
196 { .name = "reset", .gpio = 1 << 6 },
197 { .name = "ses", .gpio = 1 << 4 },
200 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
201 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
202 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
203 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
207 .name = "Linksys WRT54G3G",
209 { .name = "reset", .gpio = 1 << 6 },
210 { .name = "3g", .gpio = 1 << 4 },
213 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
214 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
215 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
216 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
217 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
221 .name = "Linksys WRT350N",
223 { .name = "reset", .gpio = 1 << 6 },
224 { .name = "ses", .gpio = 1 << 8 },
227 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
228 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
229 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
230 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
231 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
233 .platform_init = bcm57xx_init,
237 .name = "ASUS WL-HDD",
239 { .name = "reset", .gpio = 1 << 6 },
242 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
243 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
247 .name = "ASUS WL-300g",
249 { .name = "reset", .gpio = 1 << 6 },
252 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
256 .name = "ASUS WL-500g",
258 { .name = "reset", .gpio = 1 << 6 },
261 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
265 .name = "ASUS WL-500g Deluxe",
267 { .name = "reset", .gpio = 1 << 6 },
270 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
274 .name = "ASUS WL-500g Premium",
276 { .name = "reset", .gpio = 1 << 0 },
277 { .name = "ses", .gpio = 1 << 4 },
280 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
284 .name = "ASUS WL-500W",
286 { .name = "reset", .gpio = 1 << 6 },
287 { .name = "ses", .gpio = 1 << 7 },
290 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
294 .name = "ASUS (unknown, BCM4702)",
296 { .name = "reset", .gpio = 1 << 6 },
299 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
303 .name = "ASUS WL-700gE",
305 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
306 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
307 { .name = "power", .gpio = 1 << 0 }, // on front
308 { .name = "copy", .gpio = 1 << 6 }, // on front
312 // GPIO that controls power led also enables/disables some essential functions
315 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
317 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
319 .platform_init = bcm4780_init,
323 .name = "Buffalo WHR-G54S",
325 { .name = "reset", .gpio = 1 << 4 },
326 { .name = "bridge", .gpio = 1 << 5 },
327 { .name = "ses", .gpio = 1 << 0 },
330 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
331 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
332 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
333 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
334 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
338 .name = "Buffalo WBR2-G54",
341 { .name = "reset", .gpio = 1 << 7 },
344 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
348 .name = "Buffalo WHR-HP-G54",
350 { .name = "reset", .gpio = 1 << 4 },
351 { .name = "bridge", .gpio = 1 << 5 },
352 { .name = "ses", .gpio = 1 << 0 },
355 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
356 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
357 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
358 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
359 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
363 .name = "Buffalo WHR-G125",
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 WHR2-A54G54",
380 { .name = "reset", .gpio = 1 << 4 },
383 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
387 .name = "Buffalo WLA2-G54L",
390 { .name = "reset", .gpio = 1 << 7 },
393 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
397 .name = "Buffalo WZR-G300N",
399 { .name = "reset", .gpio = 1 << 4 },
402 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
403 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
404 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
408 .name = "Buffalo WZR-RS-G54",
410 { .name = "ses", .gpio = 1 << 0 },
411 { .name = "reset", .gpio = 1 << 4 },
414 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
415 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
416 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
420 .name = "Buffalo WZR-RS-G54HP",
422 { .name = "ses", .gpio = 1 << 0 },
423 { .name = "reset", .gpio = 1 << 4 },
426 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
427 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
428 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
431 [BUFFALO_UNKNOWN] = {
432 .name = "Buffalo (unknown)",
434 { .name = "reset", .gpio = 1 << 7 },
437 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
440 [BUFFALO_UNKNOWN_4710] = {
441 .name = "Buffalo (unknown, BCM4710)",
443 { .name = "reset", .gpio = 1 << 4 },
446 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
451 .name = "Siemens SE505 V1",
453 /* No usable buttons */
456 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
457 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
461 .name = "Siemens SE505 V2",
463 /* No usable buttons */
466 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
467 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
468 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
473 .name = "U.S. Robotics USR5461",
475 /* No usable buttons */
478 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
479 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
484 .name = "Dell TrueMobile 2300",
486 { .name = "reset", .gpio = 1 << 0 },
489 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
490 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
495 .name = "Motorola WE800G",
497 { .name = "reset", .gpio = 1 << 0 },
500 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
501 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
502 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
506 .name = "Motorola WR850G V1",
508 { .name = "reset", .gpio = 1 << 0 },
511 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
512 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
513 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
514 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
515 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
519 .name = "Motorola WR850G V2/V3",
521 { .name = "reset", .gpio = 1 << 5 },
524 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
525 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
526 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
527 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
531 .name = "Motorola WR850GP",
533 { .name = "reset", .gpio = 1 << 5 },
536 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
537 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
538 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
539 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
545 .name = "Belkin (unknown)",
546 /* FIXME: verify & add detection */
548 { .name = "reset", .gpio = 1 << 7 },
551 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
552 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
553 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
558 .name = "Netgear WGT634U",
560 { .name = "reset", .gpio = 1 << 2 },
563 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
568 .name = "Trendware TEW411BRP+",
570 { /* No usable buttons */ },
573 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
574 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
575 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
580 .name = "SimpleTech SimpleShare NAS",
582 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
583 { .name = "power", .gpio = 1 << 0 }, // on back
586 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
588 .platform_init = bcm4780_init,
592 .name = "D-Link DIR-130",
594 { .name = "reset", .gpio = 1 << 3},
595 { .name = "reserved", .gpio = 1 << 7},
598 { .name = "diag", .gpio = 1 << 0},
599 { .name = "blue", .gpio = 1 << 6},
603 .name = "D-Link DIR-330",
605 { .name = "reset", .gpio = 1 << 3},
606 { .name = "reserved", .gpio = 1 << 7},
609 { .name = "diag", .gpio = 1 << 0},
610 { .name = "usb", .gpio = 1 << 4},
611 { .name = "blue", .gpio = 1 << 6},
615 .name = "D-Link DWL-3150",
617 { .name = "reset", .gpio = 1 << 7},
620 { .name = "diag", .gpio = 1 << 2},
621 { .name = "status", .gpio = 1 << 1},
626 .name = "Sitecom WL-105b",
628 { .name = "reset", .gpio = 1 << 10},
631 { .name = "wlan", .gpio = 1 << 4},
632 { .name = "power", .gpio = 1 << 3},
637 static struct platform_t __init *platform_detect(void)
639 char *boardnum, *boardtype, *buf;
641 if (strcmp(getvar("nvram_type"), "cfe") == 0)
642 return &platforms[WGT634U];
644 /* Look for a model identifier */
646 /* Based on "model_name" */
647 if (buf = nvram_get("model_name")) {
648 if (!strcmp(buf, "DIR-130"))
649 return &platforms[DIR130];
650 if (!strcmp(buf, "DIR-330"))
651 return &platforms[DIR330];
654 /* Based on "model_no" */
655 if (buf = nvram_get("model_no")) {
656 if (!strncmp(buf,"WL700", 5)) /* WL700* */
657 return &platforms[WL700GE];
660 /* Based on "ModelId" */
661 if (buf = nvram_get("ModelId")) {
662 if (!strcmp(buf, "WR850GP"))
663 return &platforms[WR850GP];
664 if (!strcmp(buf,"WX-5565"))
665 return &platforms[TM2300];
666 if (!strncmp(buf,"WE800G", 6)) /* WE800G* */
667 return &platforms[WE800G];
671 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
672 /* Buffalo hardware, check id for specific hardware matches */
673 if (!strcmp(buf, "29bb0332"))
674 return &platforms[WBR2_G54];
675 if (!strcmp(buf, "29129"))
676 return &platforms[WLA2_G54L];
677 if (!strcmp(buf, "30189"))
678 return &platforms[WHR_HP_G54];
679 if (!strcmp(buf, "32093"))
680 return &platforms[WHR_G125];
681 if (!strcmp(buf, "30182"))
682 return &platforms[WHR_G54S];
683 if (!strcmp(buf, "290441dd"))
684 return &platforms[WHR2_A54G54];
685 if (!strcmp(buf, "31120"))
686 return &platforms[WZR_G300N];
687 if (!strcmp(buf, "30083"))
688 return &platforms[WZR_RS_G54];
689 if (!strcmp(buf, "30103"))
690 return &platforms[WZR_RS_G54HP];
693 /* no easy model number, attempt to guess */
694 boardnum = getvar("boardnum");
695 boardtype = getvar("boardtype");
697 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
698 /* CFE based - newer hardware */
699 if (!strcmp(boardnum, "42")) { /* Linksys */
700 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
701 return &platforms[WRT350N];
703 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
704 return &platforms[WRT54G3G];
706 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
707 return &platforms[WRTSL54GS];
709 /* default to WRT54G */
710 return &platforms[WRT54G];
713 if (!strcmp(boardnum, "45")) { /* ASUS */
714 if (!strcmp(boardtype,"0x042f"))
715 return &platforms[WL500GP];
716 else if (!strcmp(boardtype,"0x0472"))
717 return &platforms[WL500W];
719 return &platforms[WL500GD];
722 if (!strcmp(boardnum, "10496"))
723 return &platforms[USR5461];
725 } else { /* PMON based - old stuff */
726 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
727 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
728 return &platforms[WR850GV1];
730 if (!strncmp(boardtype, "bcm94710dev", 11)) {
731 if (!strcmp(boardnum, "42"))
732 return &platforms[WRT54GV1];
733 if (simple_strtoul(boardnum, NULL, 0) == 2)
734 return &platforms[WAP54GV1];
736 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
737 return &platforms[WL500G];
738 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
739 /* Either WL-300g or WL-HDD, do more extensive checks */
740 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
741 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
742 return &platforms[WLHDD];
743 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
744 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
745 return &platforms[WL300G];
747 /* Sitecom WL-105b */
748 if (!strncmp(boardnum, "2", 1) && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
749 return &platforms[WL105B];
751 /* unknown asus stuff, probably bcm4702 */
752 if (!strncmp(boardnum, "asusX", 5))
753 return &platforms[ASUS_4702];
756 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
757 if (!strncmp(boardtype, "bcm94710ap", 10))
758 return &platforms[BUFFALO_UNKNOWN_4710];
760 return &platforms[BUFFALO_UNKNOWN];
763 if (!strncmp(getvar("CFEver"), "MotoWRv2", 8) ||
764 !strncmp(getvar("CFEver"), "MotoWRv3", 8) ||
765 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
767 return &platforms[WR850GV2V3];
770 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
771 return &platforms[TEW411BRPP];
774 if (!strncmp(boardnum, "04FN52", 6)) /* SimpleTech SimpleShare */
775 return &platforms[STI_NAS];
777 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
778 return &platforms[DWL3150];
784 static void register_buttons(struct button_t *b)
787 platform.button_mask |= b->gpio;
789 platform.button_mask &= ~gpiomask;
791 gpio_outen(platform.button_mask, 0);
792 gpio_control(platform.button_mask, 0);
793 platform.button_polarity = gpio_in() & platform.button_mask;
794 gpio_intpolarity(platform.button_mask, platform.button_polarity);
795 gpio_intmask(platform.button_mask, platform.button_mask);
797 gpio_set_irqenable(1, button_handler);
800 static void unregister_buttons(struct button_t *b)
802 gpio_intmask(platform.button_mask, 0);
804 gpio_set_irqenable(0, button_handler);
809 static void add_msg(struct event_t *event, char *msg, int argv)
816 s = skb_put(event->skb, strlen(msg) + 1);
820 static void hotplug_button(struct work_struct *work)
822 struct event_t *event = container_of(work, struct event_t, wq);
828 event->skb = alloc_skb(2048, GFP_KERNEL);
830 s = skb_put(event->skb, strlen(event->action) + 2);
831 sprintf(s, "%s@", event->action);
834 NETLINK_CB(event->skb).dst_group = 1;
835 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
840 #else /* !LINUX_2_4 */
841 static inline char *kzalloc(unsigned int size, unsigned int gfp)
845 p = kmalloc(size, gfp);
854 static void add_msg(struct event_t *event, char *msg, int argv)
857 event->argv[event->anr++] = event->scratch;
859 event->envp[event->enr++] = event->scratch;
861 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
864 static void hotplug_button(struct event_t *event)
866 char *scratch = kzalloc(256, GFP_KERNEL);
867 event->scratch = scratch;
869 add_msg(event, hotplug_path, 1);
870 add_msg(event, "button", 1);
872 call_usermodehelper (event->argv[0], event->argv, event->envp);
876 #endif /* !LINUX_2_4 */
878 static int fill_event (struct event_t *event)
880 static char buf[128];
882 add_msg(event, "HOME=/", 0);
883 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
884 add_msg(event, "SUBSYSTEM=button", 0);
885 snprintf(buf, 128, "ACTION=%s", event->action);
886 add_msg(event, buf, 0);
887 snprintf(buf, 128, "BUTTON=%s", event->name);
888 add_msg(event, buf, 0);
889 snprintf(buf, 128, "SEEN=%ld", event->seen);
890 add_msg(event, buf, 0);
892 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
893 add_msg(event, buf, 0);
901 static irqreturn_t button_handler(int irq, void *dev_id)
903 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
909 in = gpio_in() & platform.button_mask;
910 gpio_intpolarity(platform.button_mask, in);
911 changed = platform.button_polarity ^ in;
912 platform.button_polarity = in;
914 changed &= ~gpio_outen(0, 0);
916 for (b = platform.buttons; b->name; b++) {
917 struct event_t *event;
919 if (!(b->gpio & changed)) continue;
923 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
924 event->seen = (jiffies - b->seen)/HZ;
925 event->name = b->name;
926 event->action = b->pressed ? "pressed" : "released";
927 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
928 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
930 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
932 schedule_work(&event->wq);
940 static void register_leds(struct led_t *l)
942 struct proc_dir_entry *p;
947 leds = proc_mkdir("led", diag);
951 for(; l->name; l++) {
952 if (l->gpio & gpiomask)
955 if (l->gpio & GPIO_TYPE_EXTIF) {
959 if (l->polarity != INPUT) oe_mask != l->gpio;
961 val |= (l->polarity == NORMAL)?0:l->gpio;
964 if (l->polarity == INPUT) continue;
966 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
967 l->proc.type = PROC_LED;
969 p->data = (void *) &l->proc;
970 p->proc_fops = &diag_proc_fops;
974 gpio_outen(mask, oe_mask);
975 gpio_control(mask, 0);
979 static void unregister_leds(struct led_t *l)
982 remove_proc_entry(l->name, leds);
984 remove_proc_entry("led", diag);
987 static void set_led_extif(struct led_t *led)
989 gpio_set_extif(led->gpio, led->state);
992 static void led_flash(unsigned long dummy) {
997 for (l = platform.leds; l->name; l++) {
999 if (l->gpio & GPIO_TYPE_EXTIF) {
1001 l->state = !l->state;
1011 u32 val = ~gpio_in();
1013 gpio_outen(mask, mask);
1014 gpio_control(mask, 0);
1015 gpio_out(mask, val);
1017 if (mask || extif_blink) {
1018 mod_timer(&led_timer, jiffies + FLASH_TIME);
1022 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1025 struct inode *inode = file->f_dentry->d_inode;
1026 struct proc_dir_entry *dent = inode->u.generic_ip;
1028 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1033 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1036 if (dent->data != NULL) {
1037 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1038 switch (handler->type) {
1040 struct led_t * led = (struct led_t *) handler->ptr;
1042 len = sprintf(page, "f\n");
1044 if (led->gpio & GPIO_TYPE_EXTIF) {
1045 len = sprintf(page, "%d\n", led->state);
1047 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1048 u8 p = (led->polarity == NORMAL ? 0 : 1);
1049 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1055 len = sprintf(page, "%s\n", platform.name);
1058 len = sprintf(page, "0x%04x\n", gpiomask);
1065 len = min_t(int, len - *ppos, count);
1066 if (copy_to_user(buf, (page + *ppos), len)) {
1080 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1083 struct inode *inode = file->f_dentry->d_inode;
1084 struct proc_dir_entry *dent = inode->u.generic_ip;
1086 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1091 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1094 if (copy_from_user(page, buf, count)) {
1100 if (dent->data != NULL) {
1101 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1102 switch (handler->type) {
1104 struct led_t *led = (struct led_t *) handler->ptr;
1105 int p = (led->polarity == NORMAL ? 0 : 1);
1107 if (page[0] == 'f') {
1112 if (led->gpio & GPIO_TYPE_EXTIF) {
1113 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1116 gpio_outen(led->gpio, led->gpio);
1117 gpio_control(led->gpio, 0);
1118 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1124 gpiomask = simple_strtoul(page, NULL, 0);
1126 if (platform.buttons) {
1127 unregister_buttons(platform.buttons);
1128 register_buttons(platform.buttons);
1131 if (platform.leds) {
1132 unregister_leds(platform.leds);
1133 register_leds(platform.leds);
1144 static int __init diag_init(void)
1146 static struct proc_dir_entry *p;
1147 static struct platform_t *detected;
1149 detected = platform_detect();
1151 printk(MODULE_NAME ": Router model not detected.\n");
1154 memcpy(&platform, detected, sizeof(struct platform_t));
1156 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1157 if (platform.platform_init != NULL) {
1158 platform.platform_init();
1161 if (!(diag = proc_mkdir("diag", NULL))) {
1162 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1166 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1167 p->data = (void *) &proc_model;
1168 p->proc_fops = &diag_proc_fops;
1171 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1172 p->data = (void *) &proc_gpiomask;
1173 p->proc_fops = &diag_proc_fops;
1176 if (platform.buttons)
1177 register_buttons(platform.buttons);
1180 register_leds(platform.leds);
1185 static void __exit diag_exit(void)
1187 del_timer(&led_timer);
1189 if (platform.buttons)
1190 unregister_buttons(platform.buttons);
1193 unregister_leds(platform.leds);
1195 remove_proc_entry("model", diag);
1196 remove_proc_entry("gpiomask", diag);
1197 remove_proc_entry("diag", NULL);
1200 module_init(diag_init);
1201 module_exit(diag_exit);
1203 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1204 MODULE_LICENSE("GPL");