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);
118 static void __init
bcm4780_init(void) {
121 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
122 printk(MODULE_NAME
": Spinning up HDD and enabling leds\n");
123 gpio_outen(pin
, pin
);
124 gpio_control(pin
, 0);
127 /* Wait 5s, so the HDD can spin up */
128 set_current_state(TASK_INTERRUPTIBLE
);
129 schedule_timeout(HZ
* 5);
132 static struct platform_t __initdata platforms
[] = {
135 .name
= "Linksys WAP54G V1",
137 { .name
= "reset", .gpio
= 1 << 0 },
140 { .name
= "diag", .gpio
= 1 << 3 },
141 { .name
= "wlan", .gpio
= 1 << 4 },
145 .name
= "Linksys WAP54G V3",
147 /* FIXME: verify this */
148 { .name
= "reset", .gpio
= 1 << 7 },
149 { .name
= "ses", .gpio
= 1 << 0 },
153 { .name
= "ses", .gpio
= 1 << 1 },
157 .name
= "Linksys WRT54G V1.x",
159 { .name
= "reset", .gpio
= 1 << 6 },
162 { .name
= "diag", .gpio
= 0x13 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
163 { .name
= "dmz", .gpio
= 0x12 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
167 .name
= "Linksys WRT54G/GS/GL",
169 { .name
= "reset", .gpio
= 1 << 6 },
170 { .name
= "ses", .gpio
= 1 << 4 },
173 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
174 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
175 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
176 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
177 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
181 .name
= "Linksys WRTSL54GS",
183 { .name
= "reset", .gpio
= 1 << 6 },
184 { .name
= "ses", .gpio
= 1 << 4 },
187 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
188 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
189 { .name
= "ses_white", .gpio
= 1 << 5, .polarity
= REVERSE
},
190 { .name
= "ses_orange", .gpio
= 1 << 7, .polarity
= REVERSE
},
194 .name
= "Linksys WRT54G3G",
196 { .name
= "reset", .gpio
= 1 << 6 },
197 { .name
= "3g", .gpio
= 1 << 4 },
200 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
201 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
202 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
203 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
204 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
208 .name
= "Linksys WRT350N",
210 { .name
= "reset", .gpio
= 1 << 6 },
211 { .name
= "ses", .gpio
= 1 << 8 },
214 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
215 { .name
= "ses", .gpio
= 1 << 3, .polarity
= REVERSE
},
220 .name
= "ASUS WL-HDD",
222 { .name
= "reset", .gpio
= 1 << 6 },
225 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
226 { .name
= "usb", .gpio
= 1 << 2, .polarity
= NORMAL
},
230 .name
= "ASUS WL-300g",
232 { .name
= "reset", .gpio
= 1 << 6 },
235 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
239 .name
= "ASUS WL-500g",
241 { .name
= "reset", .gpio
= 1 << 6 },
244 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
248 .name
= "ASUS WL-500g Deluxe",
250 { .name
= "reset", .gpio
= 1 << 6 },
253 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
257 .name
= "ASUS WL-500g Premium",
259 { .name
= "reset", .gpio
= 1 << 0 },
260 { .name
= "ses", .gpio
= 1 << 4 },
263 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
267 .name
= "ASUS WL-500W",
269 { .name
= "reset", .gpio
= 1 << 6 },
270 { .name
= "ses", .gpio
= 1 << 7 },
273 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
277 .name
= "ASUS (unknown, BCM4702)",
279 { .name
= "reset", .gpio
= 1 << 6 },
282 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
286 .name
= "ASUS WL-700gE",
288 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
289 { .name
= "ses", .gpio
= 1 << 4 }, // on back, actual name ezsetup
290 { .name
= "power", .gpio
= 1 << 0 }, // on front
291 { .name
= "copy", .gpio
= 1 << 6 }, // on front
295 // GPIO that controls power led also enables/disables some essential functions
298 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
}, // actual name power
300 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
302 .platform_init
= bcm4780_init
,
306 .name
= "Buffalo WHR-G54S",
308 { .name
= "reset", .gpio
= 1 << 4 },
309 { .name
= "bridge", .gpio
= 1 << 5 },
310 { .name
= "ses", .gpio
= 1 << 0 },
313 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
314 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
315 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
316 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
317 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
321 .name
= "Buffalo WBR2-G54",
324 { .name
= "reset", .gpio
= 1 << 7 },
327 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
331 .name
= "Buffalo WHR-HP-G54",
333 { .name
= "reset", .gpio
= 1 << 4 },
334 { .name
= "bridge", .gpio
= 1 << 5 },
335 { .name
= "ses", .gpio
= 1 << 0 },
338 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
339 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
340 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
341 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
342 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
346 .name
= "Buffalo WHR2-A54G54",
348 { .name
= "reset", .gpio
= 1 << 4 },
351 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
355 .name
= "Buffalo WLA2-G54L",
358 { .name
= "reset", .gpio
= 1 << 7 },
361 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
365 .name
= "Buffalo WZR-G300N",
367 { .name
= "reset", .gpio
= 1 << 4 },
370 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
371 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
372 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
376 .name
= "Buffalo WZR-RS-G54",
378 { .name
= "ses", .gpio
= 1 << 0 },
379 { .name
= "reset", .gpio
= 1 << 4 },
382 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
383 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
384 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
388 .name
= "Buffalo WZR-RS-G54HP",
390 { .name
= "ses", .gpio
= 1 << 0 },
391 { .name
= "reset", .gpio
= 1 << 4 },
394 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
395 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
396 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
399 [BUFFALO_UNKNOWN
] = {
400 .name
= "Buffalo (unknown)",
402 { .name
= "reset", .gpio
= 1 << 7 },
405 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
408 [BUFFALO_UNKNOWN_4710
] = {
409 .name
= "Buffalo (unknown, BCM4710)",
411 { .name
= "reset", .gpio
= 1 << 4 },
414 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
419 .name
= "Siemens SE505 V1",
421 /* No usable buttons */
424 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
},
425 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
429 .name
= "Siemens SE505 V2",
431 /* No usable buttons */
434 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
435 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
436 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
441 .name
= "U.S. Robotics USR5461",
443 /* No usable buttons */
446 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
447 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
452 .name
= "Dell TrueMobile 2300",
454 { .name
= "reset", .gpio
= 1 << 0 },
457 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
458 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
463 .name
= "Motorola WE800G",
465 { .name
= "reset", .gpio
= 1 << 0 },
468 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
469 { .name
= "diag", .gpio
= 1 << 2, .polarity
= REVERSE
},
470 { .name
= "wlan_amber", .gpio
= 1 << 1, .polarity
= NORMAL
},
474 .name
= "Motorola WR850G V1",
476 { .name
= "reset", .gpio
= 1 << 0 },
479 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
480 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
481 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= NORMAL
},
482 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
483 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
487 .name
= "Motorola WR850G V2/V3",
489 { .name
= "reset", .gpio
= 1 << 5 },
492 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
493 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
494 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
495 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
499 .name
= "Motorola WR850GP",
501 { .name
= "reset", .gpio
= 1 << 5 },
504 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
505 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
506 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
507 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
513 .name
= "Belkin (unknown)",
514 /* FIXME: verify & add detection */
516 { .name
= "reset", .gpio
= 1 << 7 },
519 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
520 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
521 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
526 .name
= "Netgear WGT634U",
528 { .name
= "reset", .gpio
= 1 << 2 },
531 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
536 .name
= "Trendware TEW411BRP+",
538 { /* No usable buttons */ },
541 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
542 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
543 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
548 .name
= "SimpleTech SimpleShare NAS",
550 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
551 { .name
= "power", .gpio
= 1 << 0 }, // on back
554 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
556 .platform_init
= bcm4780_init
,
560 .name
= "D-Link DIR-130",
562 { .name
= "reset", .gpio
= 1 << 3},
563 { .name
= "reserved", .gpio
= 1 << 7},
566 { .name
= "diag", .gpio
= 1 << 0},
567 { .name
= "blue", .gpio
= 1 << 6},
571 .name
= "D-Link DIR-330",
573 { .name
= "reset", .gpio
= 1 << 3},
574 { .name
= "reserved", .gpio
= 1 << 7},
577 { .name
= "diag", .gpio
= 1 << 0},
578 { .name
= "usb", .gpio
= 1 << 4},
579 { .name
= "blue", .gpio
= 1 << 6},
584 static struct platform_t __init
*platform_detect(void)
586 char *boardnum
, *boardtype
, *buf
;
588 if (strcmp(getvar("nvram_type"), "cfe") == 0)
589 return &platforms
[WGT634U
];
591 /* Look for a model identifier */
593 /* Based on "model_name" */
594 if (buf
= nvram_get("model_name")) {
595 if (!strcmp(buf
, "DIR-130"))
596 return &platforms
[DIR130
];
597 if (!strcmp(buf
, "DIR-330"))
598 return &platforms
[DIR330
];
601 /* Based on "model_no" */
602 if (buf
= nvram_get("model_no")) {
603 if (!strncmp(buf
,"WL700", 5)) /* WL700* */
604 return &platforms
[WL700GE
];
607 /* Based on "ModelId" */
608 if (buf
= nvram_get("ModelId")) {
609 if (!strcmp(buf
, "WR850GP"))
610 return &platforms
[WR850GP
];
611 if (!strcmp(buf
,"WX-5565"))
612 return &platforms
[TM2300
];
613 if (!strncmp(buf
,"WE800G", 6)) /* WE800G* */
614 return &platforms
[WE800G
];
618 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
619 /* Buffalo hardware, check id for specific hardware matches */
620 if (!strcmp(buf
, "29bb0332"))
621 return &platforms
[WBR2_G54
];
622 if (!strcmp(buf
, "29129"))
623 return &platforms
[WLA2_G54L
];
624 if (!strcmp(buf
, "30189"))
625 return &platforms
[WHR_HP_G54
];
626 if (!strcmp(buf
, "30182"))
627 return &platforms
[WHR_G54S
];
628 if (!strcmp(buf
, "290441dd"))
629 return &platforms
[WHR2_A54G54
];
630 if (!strcmp(buf
, "31120"))
631 return &platforms
[WZR_G300N
];
632 if (!strcmp(buf
, "30083"))
633 return &platforms
[WZR_RS_G54
];
634 if (!strcmp(buf
, "30103"))
635 return &platforms
[WZR_RS_G54HP
];
638 /* no easy model number, attempt to guess */
639 boardnum
= getvar("boardnum");
640 boardtype
= getvar("boardtype");
642 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
643 /* CFE based - newer hardware */
644 if (!strcmp(boardnum
, "42")) { /* Linksys */
645 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
646 return &platforms
[WRT350N
];
648 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
649 return &platforms
[WRT54G3G
];
651 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
652 return &platforms
[WRTSL54GS
];
654 /* default to WRT54G */
655 return &platforms
[WRT54G
];
658 if (!strcmp(boardnum
, "45")) { /* ASUS */
659 if (!strcmp(boardtype
,"0x042f"))
660 return &platforms
[WL500GP
];
661 else if (!strcmp(boardtype
,"0x0472"))
662 return &platforms
[WL500W
];
664 return &platforms
[WL500GD
];
667 if (!strcmp(boardnum
, "10496"))
668 return &platforms
[USR5461
];
670 } else { /* PMON based - old stuff */
671 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
672 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
673 return &platforms
[WR850GV1
];
675 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
676 if (!strcmp(boardnum
, "42"))
677 return &platforms
[WRT54GV1
];
678 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
679 return &platforms
[WAP54GV1
];
681 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
682 return &platforms
[WL500G
];
683 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
684 /* Either WL-300g or WL-HDD, do more extensive checks */
685 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
686 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 1))
687 return &platforms
[WLHDD
];
688 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
689 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 10))
690 return &platforms
[WL300G
];
693 /* unknown asus stuff, probably bcm4702 */
694 if (!strncmp(boardnum
, "asusX", 5))
695 return &platforms
[ASUS_4702
];
698 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
699 if (!strncmp(boardtype
, "bcm94710ap", 10))
700 return &platforms
[BUFFALO_UNKNOWN_4710
];
702 return &platforms
[BUFFALO_UNKNOWN
];
705 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
706 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
708 return &platforms
[WR850GV2V3
];
711 if (!strcmp(boardnum
, "44")) { /* Trendware TEW-411BRP+ */
712 return &platforms
[TEW411BRPP
];
715 if (!strncmp(boardnum
, "04FN52", 6)) /* SimpleTech SimpleShare */
716 return &platforms
[STI_NAS
];
722 static void register_buttons(struct button_t
*b
)
725 platform
.button_mask
|= b
->gpio
;
727 platform
.button_mask
&= ~gpiomask
;
729 gpio_outen(platform
.button_mask
, 0);
730 gpio_control(platform
.button_mask
, 0);
731 platform
.button_polarity
= gpio_in() & platform
.button_mask
;
732 gpio_intpolarity(platform
.button_mask
, platform
.button_polarity
);
733 gpio_intmask(platform
.button_mask
, platform
.button_mask
);
735 gpio_set_irqenable(1, button_handler
);
738 static void unregister_buttons(struct button_t
*b
)
740 gpio_intmask(platform
.button_mask
, 0);
742 gpio_set_irqenable(0, button_handler
);
747 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
754 s
= skb_put(event
->skb
, strlen(msg
) + 1);
758 static void hotplug_button(struct work_struct
*work
)
760 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
766 event
->skb
= alloc_skb(2048, GFP_KERNEL
);
768 s
= skb_put(event
->skb
, strlen(event
->action
) + 2);
769 sprintf(s
, "%s@", event
->action
);
772 NETLINK_CB(event
->skb
).dst_group
= 1;
773 netlink_broadcast(uevent_sock
, event
->skb
, 0, 1, GFP_KERNEL
);
778 #else /* !LINUX_2_4 */
779 static inline char *kzalloc(unsigned int size
, unsigned int gfp
)
783 p
= kmalloc(size
, gfp
);
792 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
795 event
->argv
[event
->anr
++] = event
->scratch
;
797 event
->envp
[event
->enr
++] = event
->scratch
;
799 event
->scratch
+= sprintf(event
->scratch
, "%s", msg
) + 1;
802 static void hotplug_button(struct event_t
*event
)
804 char *scratch
= kzalloc(256, GFP_KERNEL
);
805 event
->scratch
= scratch
;
807 add_msg(event
, hotplug_path
, 1);
808 add_msg(event
, "button", 1);
810 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
814 #endif /* !LINUX_2_4 */
816 static int fill_event (struct event_t
*event
)
818 static char buf
[128];
820 add_msg(event
, "HOME=/", 0);
821 add_msg(event
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
822 add_msg(event
, "SUBSYSTEM=button", 0);
823 snprintf(buf
, 128, "ACTION=%s", event
->action
);
824 add_msg(event
, buf
, 0);
825 snprintf(buf
, 128, "BUTTON=%s", event
->name
);
826 add_msg(event
, buf
, 0);
827 snprintf(buf
, 128, "SEEN=%ld", event
->seen
);
828 add_msg(event
, buf
, 0);
830 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
831 add_msg(event
, buf
, 0);
839 static irqreturn_t
button_handler(int irq
, void *dev_id
)
841 static irqreturn_t
button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
847 in
= gpio_in() & platform
.button_mask
;
848 gpio_intpolarity(platform
.button_mask
, in
);
849 changed
= platform
.button_polarity
^ in
;
850 platform
.button_polarity
= in
;
852 changed
&= ~gpio_outen(0, 0);
854 for (b
= platform
.buttons
; b
->name
; b
++) {
855 struct event_t
*event
;
857 if (!(b
->gpio
& changed
)) continue;
861 if ((event
= (struct event_t
*)kzalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
862 event
->seen
= (jiffies
- b
->seen
)/HZ
;
863 event
->name
= b
->name
;
864 event
->action
= b
->pressed
? "pressed" : "released";
865 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
866 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
868 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
, (void *)event
);
870 schedule_work(&event
->wq
);
878 static void register_leds(struct led_t
*l
)
880 struct proc_dir_entry
*p
;
884 leds
= proc_mkdir("led", diag
);
888 for(; l
->name
; l
++) {
889 if (l
->gpio
& gpiomask
)
892 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
897 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
900 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
901 l
->proc
.type
= PROC_LED
;
903 p
->data
= (void *) &l
->proc
;
904 p
->proc_fops
= &diag_proc_fops
;
908 gpio_outen(mask
, mask
);
909 gpio_control(mask
, 0);
913 static void unregister_leds(struct led_t
*l
)
916 remove_proc_entry(l
->name
, leds
);
918 remove_proc_entry("led", diag
);
921 static void set_led_extif(struct led_t
*led
)
923 gpio_set_extif(led
->gpio
, led
->state
);
926 static void led_flash(unsigned long dummy
) {
931 for (l
= platform
.leds
; l
->name
; l
++) {
933 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
935 l
->state
= !l
->state
;
945 u32 val
= ~gpio_in();
947 gpio_outen(mask
, mask
);
948 gpio_control(mask
, 0);
951 if (mask
|| extif_blink
) {
952 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
956 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
959 struct inode
*inode
= file
->f_dentry
->d_inode
;
960 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
962 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
967 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
970 if (dent
->data
!= NULL
) {
971 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
972 switch (handler
->type
) {
974 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
976 len
= sprintf(page
, "f\n");
978 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
979 len
= sprintf(page
, "%d\n", led
->state
);
981 u32 in
= (gpio_in() & led
->gpio
? 1 : 0);
982 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
983 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
989 len
= sprintf(page
, "%s\n", platform
.name
);
992 len
= sprintf(page
, "0x%04x\n", gpiomask
);
999 len
= min_t(int, len
- *ppos
, count
);
1000 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
1014 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
1017 struct inode
*inode
= file
->f_dentry
->d_inode
;
1018 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1020 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1025 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
1028 if (copy_from_user(page
, buf
, count
)) {
1034 if (dent
->data
!= NULL
) {
1035 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1036 switch (handler
->type
) {
1038 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
1039 int p
= (led
->polarity
== NORMAL
? 0 : 1);
1041 if (page
[0] == 'f') {
1046 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1047 led
->state
= p
^ ((page
[0] == '1') ? 1 : 0);
1050 gpio_outen(led
->gpio
, led
->gpio
);
1051 gpio_control(led
->gpio
, 0);
1052 gpio_out(led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
1058 gpiomask
= simple_strtoul(page
, NULL
, 0);
1060 if (platform
.buttons
) {
1061 unregister_buttons(platform
.buttons
);
1062 register_buttons(platform
.buttons
);
1065 if (platform
.leds
) {
1066 unregister_leds(platform
.leds
);
1067 register_leds(platform
.leds
);
1078 static int __init
diag_init(void)
1080 static struct proc_dir_entry
*p
;
1081 static struct platform_t
*detected
;
1083 detected
= platform_detect();
1085 printk(MODULE_NAME
": Router model not detected.\n");
1088 memcpy(&platform
, detected
, sizeof(struct platform_t
));
1090 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
1091 if (platform
.platform_init
!= NULL
) {
1092 platform
.platform_init();
1095 if (!(diag
= proc_mkdir("diag", NULL
))) {
1096 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1100 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
1101 p
->data
= (void *) &proc_model
;
1102 p
->proc_fops
= &diag_proc_fops
;
1105 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
1106 p
->data
= (void *) &proc_gpiomask
;
1107 p
->proc_fops
= &diag_proc_fops
;
1110 if (platform
.buttons
)
1111 register_buttons(platform
.buttons
);
1114 register_leds(platform
.leds
);
1119 static void __exit
diag_exit(void)
1121 del_timer(&led_timer
);
1123 if (platform
.buttons
)
1124 unregister_buttons(platform
.buttons
);
1127 unregister_leds(platform
.leds
);
1129 remove_proc_entry("model", diag
);
1130 remove_proc_entry("gpiomask", diag
);
1131 remove_proc_entry("diag", NULL
);
1134 module_init(diag_init
);
1135 module_exit(diag_exit
);
1137 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1138 MODULE_LICENSE("GPL");