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);
117 static void __init bcm4780_init(void) {
120 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
121 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
122 gpio_outen(pin, pin);
123 gpio_control(pin, 0);
126 /* Wait 5s, so the HDD can spin up */
127 set_current_state(TASK_INTERRUPTIBLE);
128 schedule_timeout(HZ * 5);
131 static struct platform_t __initdata platforms[] = {
134 .name = "Linksys WAP54G V1",
136 { .name = "reset", .gpio = 1 << 0 },
139 { .name = "diag", .gpio = 1 << 3 },
140 { .name = "wlan", .gpio = 1 << 4 },
144 .name = "Linksys WAP54G V3",
146 /* FIXME: verify this */
147 { .name = "reset", .gpio = 1 << 7 },
148 { .name = "ses", .gpio = 1 << 0 },
152 { .name = "ses", .gpio = 1 << 1 },
156 .name = "Linksys WRT54G V1.x",
158 { .name = "reset", .gpio = 1 << 6 },
161 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
162 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
166 .name = "Linksys WRT54G/GS/GL",
168 { .name = "reset", .gpio = 1 << 6 },
169 { .name = "ses", .gpio = 1 << 4 },
172 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
173 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
174 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
175 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
176 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
180 .name = "Linksys WRTSL54GS",
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 << 0, .polarity = REVERSE },
188 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
189 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
193 .name = "Linksys WRT54G3G",
195 { .name = "reset", .gpio = 1 << 6 },
196 { .name = "3g", .gpio = 1 << 4 },
199 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
200 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
201 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
202 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
203 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
207 .name = "Linksys WRT350N",
209 { .name = "reset", .gpio = 1 << 6 },
210 { .name = "ses", .gpio = 1 << 8 },
213 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
214 { .name = "ses", .gpio = 1 << 3, .polarity = REVERSE },
219 .name = "ASUS WL-HDD",
221 { .name = "reset", .gpio = 1 << 6 },
224 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
225 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
229 .name = "ASUS WL-300g",
231 { .name = "reset", .gpio = 1 << 6 },
234 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
238 .name = "ASUS WL-500g",
240 { .name = "reset", .gpio = 1 << 6 },
243 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
247 .name = "ASUS WL-500g Deluxe",
249 { .name = "reset", .gpio = 1 << 6 },
252 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
256 .name = "ASUS WL-500g Premium",
258 { .name = "reset", .gpio = 1 << 0 },
259 { .name = "ses", .gpio = 1 << 4 },
262 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
266 .name = "ASUS WL-500W",
268 { .name = "reset", .gpio = 1 << 6 },
269 { .name = "ses", .gpio = 1 << 7 },
272 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
276 .name = "ASUS (unknown, BCM4702)",
278 { .name = "reset", .gpio = 1 << 6 },
281 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
285 .name = "ASUS WL-700gE",
287 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
288 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
289 { .name = "power", .gpio = 1 << 0 }, // on front
290 { .name = "copy", .gpio = 1 << 6 }, // on front
294 // GPIO that controls power led also enables/disables some essential functions
297 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
299 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
301 .platform_init = bcm4780_init,
305 .name = "Buffalo WHR-G54S",
307 { .name = "reset", .gpio = 1 << 4 },
308 { .name = "bridge", .gpio = 1 << 5 },
309 { .name = "ses", .gpio = 1 << 0 },
312 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
313 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
314 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
315 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
316 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
320 .name = "Buffalo WBR2-G54",
323 { .name = "reset", .gpio = 1 << 7 },
326 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
330 .name = "Buffalo WHR-HP-G54",
332 { .name = "reset", .gpio = 1 << 4 },
333 { .name = "bridge", .gpio = 1 << 5 },
334 { .name = "ses", .gpio = 1 << 0 },
337 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
338 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
339 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
340 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
341 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
345 .name = "Buffalo WHR2-A54G54",
347 { .name = "reset", .gpio = 1 << 4 },
350 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
354 .name = "Buffalo WLA2-G54L",
357 { .name = "reset", .gpio = 1 << 7 },
360 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
364 .name = "Buffalo WZR-G300N",
366 { .name = "reset", .gpio = 1 << 4 },
369 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
370 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
371 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
375 .name = "Buffalo WZR-RS-G54",
377 { .name = "ses", .gpio = 1 << 0 },
378 { .name = "reset", .gpio = 1 << 4 },
381 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
382 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
383 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
387 .name = "Buffalo WZR-RS-G54HP",
389 { .name = "ses", .gpio = 1 << 0 },
390 { .name = "reset", .gpio = 1 << 4 },
393 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
394 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
395 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
398 [BUFFALO_UNKNOWN] = {
399 .name = "Buffalo (unknown)",
401 { .name = "reset", .gpio = 1 << 7 },
404 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
407 [BUFFALO_UNKNOWN_4710] = {
408 .name = "Buffalo (unknown, BCM4710)",
410 { .name = "reset", .gpio = 1 << 4 },
413 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
418 .name = "Siemens SE505 V1",
420 /* No usable buttons */
423 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
424 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
428 .name = "Siemens SE505 V2",
430 /* No usable buttons */
433 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
434 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
435 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
440 .name = "U.S. Robotics USR5461",
442 /* No usable buttons */
445 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
446 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
451 .name = "Dell TrueMobile 2300",
453 { .name = "reset", .gpio = 1 << 0 },
456 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
457 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
462 .name = "Motorola WE800G",
464 { .name = "reset", .gpio = 1 << 0 },
467 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
468 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
469 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
473 .name = "Motorola WR850G V1",
475 { .name = "reset", .gpio = 1 << 0 },
478 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
479 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
480 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
481 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
482 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
486 .name = "Motorola WR850G V2/V3",
488 { .name = "reset", .gpio = 1 << 5 },
491 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
492 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
493 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
494 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
499 .name = "Belkin (unknown)",
500 /* FIXME: verify & add detection */
502 { .name = "reset", .gpio = 1 << 7 },
505 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
506 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
507 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
512 .name = "Netgear WGT634U",
514 { .name = "reset", .gpio = 1 << 2 },
517 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
522 .name = "Trendware TEW411BRP+",
524 { /* No usable buttons */ },
527 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
528 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
529 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
534 .name = "SimpleTech SimpleShare NAS",
536 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
537 { .name = "power", .gpio = 1 << 0 }, // on back
540 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
542 .platform_init = bcm4780_init,
546 .name = "D-Link DIR-130",
548 { .name = "reset", .gpio = 1 << 3},
549 { .name = "reserved", .gpio = 1 << 7},
552 { .name = "diag", .gpio = 1 << 0},
553 { .name = "blue", .gpio = 1 << 6},
557 .name = "D-Link DIR-330",
559 { .name = "reset", .gpio = 1 << 3},
560 { .name = "reserved", .gpio = 1 << 7},
563 { .name = "diag", .gpio = 1 << 0},
564 { .name = "usb", .gpio = 1 << 4},
565 { .name = "blue", .gpio = 1 << 6},
570 static struct platform_t __init *platform_detect(void)
572 char *boardnum, *boardtype, *buf;
574 boardnum = getvar("boardnum");
575 boardtype = getvar("boardtype");
577 if (strcmp(getvar("nvram_type"), "cfe") == 0)
578 return &platforms[WGT634U];
580 if (strncmp(getvar("model_no"), "WL700",5) == 0)
581 return &platforms[WL700GE];
583 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
584 /* CFE based - newer hardware */
585 if (!strcmp(boardnum, "42")) { /* Linksys */
586 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
587 return &platforms[WRT350N];
589 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
590 return &platforms[WRT54G3G];
592 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
593 return &platforms[WRTSL54GS];
595 /* default to WRT54G */
596 return &platforms[WRT54G];
599 if (!strcmp(boardnum, "45")) { /* ASUS */
600 if (!strcmp(boardtype,"0x042f"))
601 return &platforms[WL500GP];
602 else if (!strcmp(boardtype,"0x0472"))
603 return &platforms[WL500W];
605 return &platforms[WL500GD];
608 if (!strcmp(boardnum, "10496"))
609 return &platforms[USR5461];
612 if (!strcmp(getvar("model_name"), "DIR-130"))
613 return &platforms[DIR130];
614 if (!strcmp(getvar("model_name"), "DIR-330"))
615 return &platforms[DIR330];
617 } else { /* PMON based - old stuff */
619 /* Dell TrueMobile 2300 */
620 if (!strcmp(getvar("ModelId"),"WX-5565"))
621 return &platforms[TM2300];
623 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
624 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
625 if (!strncmp(getvar("ModelId"),"WE800G", 6))
626 return &platforms[WE800G];
628 return &platforms[WR850GV1];
630 if (!strncmp(boardtype, "bcm94710dev", 11)) {
631 if (!strcmp(boardnum, "42"))
632 return &platforms[WRT54GV1];
633 if (simple_strtoul(boardnum, NULL, 0) == 2)
634 return &platforms[WAP54GV1];
636 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
637 return &platforms[WL500G];
638 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
639 /* Either WL-300g or WL-HDD, do more extensive checks */
640 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
641 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
642 return &platforms[WLHDD];
643 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
644 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
645 return &platforms[WL300G];
648 /* unknown asus stuff, probably bcm4702 */
649 if (!strncmp(boardnum, "asusX", 5))
650 return &platforms[ASUS_4702];
653 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
654 /* Buffalo hardware, check id for specific hardware matches */
655 if (!strcmp(buf, "29bb0332"))
656 return &platforms[WBR2_G54];
657 if (!strcmp(buf, "29129"))
658 return &platforms[WLA2_G54L];
659 if (!strcmp(buf, "30189"))
660 return &platforms[WHR_HP_G54];
661 if (!strcmp(buf, "30182"))
662 return &platforms[WHR_G54S];
663 if (!strcmp(buf, "290441dd"))
664 return &platforms[WHR2_A54G54];
665 if (!strcmp(buf, "31120"))
666 return &platforms[WZR_G300N];
667 if (!strcmp(buf, "30083"))
668 return &platforms[WZR_RS_G54];
669 if (!strcmp(buf, "30103"))
670 return &platforms[WZR_RS_G54HP];
673 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
674 if (!strncmp(boardtype, "bcm94710ap", 10))
675 return &platforms[BUFFALO_UNKNOWN_4710];
677 return &platforms[BUFFALO_UNKNOWN];
680 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
681 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
683 return &platforms[WR850GV2V3];
686 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
687 return &platforms[TEW411BRPP];
690 if (!strncmp(boardnum, "04FN52", 6)) /* SimpleTech SimpleShare */
691 return &platforms[STI_NAS];
697 static void register_buttons(struct button_t *b)
700 platform.button_mask |= b->gpio;
702 platform.button_mask &= ~gpiomask;
704 gpio_outen(platform.button_mask, 0);
705 gpio_control(platform.button_mask, 0);
706 platform.button_polarity = gpio_in() & platform.button_mask;
707 gpio_intpolarity(platform.button_mask, platform.button_polarity);
708 gpio_intmask(platform.button_mask, platform.button_mask);
710 gpio_set_irqenable(1, button_handler);
713 static void unregister_buttons(struct button_t *b)
715 gpio_intmask(platform.button_mask, 0);
717 gpio_set_irqenable(0, button_handler);
722 static void add_msg(struct event_t *event, char *msg, int argv)
729 s = skb_put(event->skb, strlen(msg) + 1);
733 static void hotplug_button(struct work_struct *work)
735 struct event_t *event = container_of(work, struct event_t, wq);
741 event->skb = alloc_skb(2048, GFP_KERNEL);
743 s = skb_put(event->skb, strlen(event->action) + 2);
744 sprintf(s, "%s@", event->action);
747 NETLINK_CB(event->skb).dst_group = 1;
748 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
753 #else /* !LINUX_2_4 */
754 static inline char *kzalloc(unsigned int size, unsigned int gfp)
758 p = kmalloc(size, gfp);
767 static void add_msg(struct event_t *event, char *msg, int argv)
770 event->argv[event->anr++] = event->scratch;
772 event->envp[event->enr++] = event->scratch;
774 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
777 static void hotplug_button(struct event_t *event)
779 char *scratch = kzalloc(256, GFP_KERNEL);
780 event->scratch = scratch;
782 add_msg(event, hotplug_path, 1);
783 add_msg(event, "button", 1);
785 call_usermodehelper (event->argv[0], event->argv, event->envp);
789 #endif /* !LINUX_2_4 */
791 static int fill_event (struct event_t *event)
793 static char buf[128];
795 add_msg(event, "HOME=/", 0);
796 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
797 add_msg(event, "SUBSYSTEM=button", 0);
798 snprintf(buf, 128, "ACTION=%s", event->action);
799 add_msg(event, buf, 0);
800 snprintf(buf, 128, "BUTTON=%s", event->name);
801 add_msg(event, buf, 0);
802 snprintf(buf, 128, "SEEN=%ld", event->seen);
803 add_msg(event, buf, 0);
805 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
806 add_msg(event, buf, 0);
814 static irqreturn_t button_handler(int irq, void *dev_id)
816 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
822 in = gpio_in() & platform.button_mask;
823 gpio_intpolarity(platform.button_mask, in);
824 changed = platform.button_polarity ^ in;
825 platform.button_polarity = in;
827 changed &= ~gpio_outen(0, 0);
829 for (b = platform.buttons; b->name; b++) {
830 struct event_t *event;
832 if (!(b->gpio & changed)) continue;
836 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
837 event->seen = (jiffies - b->seen)/HZ;
838 event->name = b->name;
839 event->action = b->pressed ? "pressed" : "released";
840 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
841 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
843 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
845 schedule_work(&event->wq);
853 static void register_leds(struct led_t *l)
855 struct proc_dir_entry *p;
859 leds = proc_mkdir("led", diag);
863 for(; l->name; l++) {
864 if (l->gpio & gpiomask)
867 if (l->gpio & GPIO_TYPE_EXTIF) {
872 val |= (l->polarity == NORMAL)?0:l->gpio;
875 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
876 l->proc.type = PROC_LED;
878 p->data = (void *) &l->proc;
879 p->proc_fops = &diag_proc_fops;
883 gpio_outen(mask, mask);
884 gpio_control(mask, 0);
888 static void unregister_leds(struct led_t *l)
891 remove_proc_entry(l->name, leds);
893 remove_proc_entry("led", diag);
896 static void set_led_extif(struct led_t *led)
898 gpio_set_extif(led->gpio, led->state);
901 static void led_flash(unsigned long dummy) {
906 for (l = platform.leds; l->name; l++) {
908 if (l->gpio & GPIO_TYPE_EXTIF) {
910 l->state = !l->state;
920 u32 val = ~gpio_in();
922 gpio_outen(mask, mask);
923 gpio_control(mask, 0);
926 if (mask || extif_blink) {
927 mod_timer(&led_timer, jiffies + FLASH_TIME);
931 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
934 struct inode *inode = file->f_dentry->d_inode;
935 struct proc_dir_entry *dent = inode->u.generic_ip;
937 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
942 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
945 if (dent->data != NULL) {
946 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
947 switch (handler->type) {
949 struct led_t * led = (struct led_t *) handler->ptr;
951 len = sprintf(page, "f\n");
953 if (led->gpio & GPIO_TYPE_EXTIF) {
954 len = sprintf(page, "%d\n", led->state);
956 u32 in = (gpio_in() & led->gpio ? 1 : 0);
957 u8 p = (led->polarity == NORMAL ? 0 : 1);
958 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
964 len = sprintf(page, "%s\n", platform.name);
967 len = sprintf(page, "0x%04x\n", gpiomask);
974 len = min_t(int, len - *ppos, count);
975 if (copy_to_user(buf, (page + *ppos), len)) {
989 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
992 struct inode *inode = file->f_dentry->d_inode;
993 struct proc_dir_entry *dent = inode->u.generic_ip;
995 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1000 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1003 if (copy_from_user(page, buf, count)) {
1009 if (dent->data != NULL) {
1010 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1011 switch (handler->type) {
1013 struct led_t *led = (struct led_t *) handler->ptr;
1014 int p = (led->polarity == NORMAL ? 0 : 1);
1016 if (page[0] == 'f') {
1021 if (led->gpio & GPIO_TYPE_EXTIF) {
1022 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1025 gpio_outen(led->gpio, led->gpio);
1026 gpio_control(led->gpio, 0);
1027 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1033 gpiomask = simple_strtoul(page, NULL, 0);
1035 if (platform.buttons) {
1036 unregister_buttons(platform.buttons);
1037 register_buttons(platform.buttons);
1040 if (platform.leds) {
1041 unregister_leds(platform.leds);
1042 register_leds(platform.leds);
1053 static int __init diag_init(void)
1055 static struct proc_dir_entry *p;
1056 static struct platform_t *detected;
1058 detected = platform_detect();
1060 printk(MODULE_NAME ": Router model not detected.\n");
1063 memcpy(&platform, detected, sizeof(struct platform_t));
1065 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1066 if (platform.platform_init != NULL) {
1067 platform.platform_init();
1070 if (!(diag = proc_mkdir("diag", NULL))) {
1071 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1075 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1076 p->data = (void *) &proc_model;
1077 p->proc_fops = &diag_proc_fops;
1080 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1081 p->data = (void *) &proc_gpiomask;
1082 p->proc_fops = &diag_proc_fops;
1085 if (platform.buttons)
1086 register_buttons(platform.buttons);
1089 register_leds(platform.leds);
1094 static void __exit diag_exit(void)
1096 del_timer(&led_timer);
1098 if (platform.buttons)
1099 unregister_buttons(platform.buttons);
1102 unregister_leds(platform.leds);
1104 remove_proc_entry("model", diag);
1105 remove_proc_entry("gpiomask", diag);
1106 remove_proc_entry("diag", NULL);
1109 module_init(diag_init);
1110 module_exit(diag_exit);
1112 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1113 MODULE_LICENSE("GPL");