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);
120 static void __init bcm4780_init(void) {
123 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
124 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
125 gpio_outen(pin, pin);
126 gpio_control(pin, 0);
129 /* Wait 5s, so the HDD can spin up */
130 set_current_state(TASK_INTERRUPTIBLE);
131 schedule_timeout(HZ * 5);
134 static struct platform_t __initdata platforms[] = {
137 .name = "Linksys WAP54G V1",
139 { .name = "reset", .gpio = 1 << 0 },
142 { .name = "diag", .gpio = 1 << 3 },
143 { .name = "wlan", .gpio = 1 << 4 },
147 .name = "Linksys WAP54G V3",
149 /* FIXME: verify this */
150 { .name = "reset", .gpio = 1 << 7 },
151 { .name = "ses", .gpio = 1 << 0 },
155 { .name = "ses", .gpio = 1 << 1 },
159 .name = "Linksys WRT54G V1.x",
161 { .name = "reset", .gpio = 1 << 6 },
164 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
165 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
169 .name = "Linksys WRT54G/GS/GL",
171 { .name = "reset", .gpio = 1 << 6 },
172 { .name = "ses", .gpio = 1 << 4 },
175 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
176 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
177 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
178 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
179 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
183 .name = "Linksys WRTSL54GS",
185 { .name = "reset", .gpio = 1 << 6 },
186 { .name = "ses", .gpio = 1 << 4 },
189 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
190 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
191 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
192 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
196 .name = "Linksys WRT54G3G",
198 { .name = "reset", .gpio = 1 << 6 },
199 { .name = "3g", .gpio = 1 << 4 },
202 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
203 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
204 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
205 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
206 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
210 .name = "Linksys WRT350N",
212 { .name = "reset", .gpio = 1 << 6 },
213 { .name = "ses", .gpio = 1 << 8 },
216 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
217 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
218 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
219 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
220 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
225 .name = "ASUS WL-HDD",
227 { .name = "reset", .gpio = 1 << 6 },
230 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
231 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
235 .name = "ASUS WL-300g",
237 { .name = "reset", .gpio = 1 << 6 },
240 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
244 .name = "ASUS WL-500g",
246 { .name = "reset", .gpio = 1 << 6 },
249 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
253 .name = "ASUS WL-500g Deluxe",
255 { .name = "reset", .gpio = 1 << 6 },
258 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
262 .name = "ASUS WL-500g Premium",
264 { .name = "reset", .gpio = 1 << 0 },
265 { .name = "ses", .gpio = 1 << 4 },
268 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
272 .name = "ASUS WL-500W",
274 { .name = "reset", .gpio = 1 << 6 },
275 { .name = "ses", .gpio = 1 << 7 },
278 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
282 .name = "ASUS (unknown, BCM4702)",
284 { .name = "reset", .gpio = 1 << 6 },
287 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
291 .name = "ASUS WL-700gE",
293 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
294 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
295 { .name = "power", .gpio = 1 << 0 }, // on front
296 { .name = "copy", .gpio = 1 << 6 }, // on front
300 // GPIO that controls power led also enables/disables some essential functions
303 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
305 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
307 .platform_init = bcm4780_init,
311 .name = "Buffalo WHR-G54S",
313 { .name = "reset", .gpio = 1 << 4 },
314 { .name = "bridge", .gpio = 1 << 5 },
315 { .name = "ses", .gpio = 1 << 0 },
318 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
319 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
320 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
321 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
322 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
326 .name = "Buffalo WBR2-G54",
329 { .name = "reset", .gpio = 1 << 7 },
332 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
336 .name = "Buffalo WHR-HP-G54",
338 { .name = "reset", .gpio = 1 << 4 },
339 { .name = "bridge", .gpio = 1 << 5 },
340 { .name = "ses", .gpio = 1 << 0 },
343 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
344 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
345 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
346 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
347 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
351 .name = "Buffalo WHR-G125",
353 { .name = "reset", .gpio = 1 << 4 },
354 { .name = "bridge", .gpio = 1 << 5 },
355 { .name = "ses", .gpio = 1 << 0 },
358 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
359 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
360 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
361 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
362 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
366 .name = "Buffalo WHR2-A54G54",
368 { .name = "reset", .gpio = 1 << 4 },
371 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
375 .name = "Buffalo WLA2-G54L",
378 { .name = "reset", .gpio = 1 << 7 },
381 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
385 .name = "Buffalo WZR-G300N",
387 { .name = "reset", .gpio = 1 << 4 },
390 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
391 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
392 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
396 .name = "Buffalo WZR-RS-G54",
398 { .name = "ses", .gpio = 1 << 0 },
399 { .name = "reset", .gpio = 1 << 4 },
402 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
403 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
404 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
408 .name = "Buffalo WZR-RS-G54HP",
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 },
419 [BUFFALO_UNKNOWN] = {
420 .name = "Buffalo (unknown)",
422 { .name = "reset", .gpio = 1 << 7 },
425 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
428 [BUFFALO_UNKNOWN_4710] = {
429 .name = "Buffalo (unknown, BCM4710)",
431 { .name = "reset", .gpio = 1 << 4 },
434 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
439 .name = "Siemens SE505 V1",
441 /* No usable buttons */
444 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
445 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
449 .name = "Siemens SE505 V2",
451 /* No usable buttons */
454 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
455 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
456 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
461 .name = "U.S. Robotics USR5461",
463 /* No usable buttons */
466 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
467 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
472 .name = "Dell TrueMobile 2300",
474 { .name = "reset", .gpio = 1 << 0 },
477 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
478 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
483 .name = "Motorola WE800G",
485 { .name = "reset", .gpio = 1 << 0 },
488 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
489 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
490 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
494 .name = "Motorola WR850G V1",
496 { .name = "reset", .gpio = 1 << 0 },
499 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
500 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
501 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
502 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
503 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
507 .name = "Motorola WR850G V2/V3",
509 { .name = "reset", .gpio = 1 << 5 },
512 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
513 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
514 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
515 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
519 .name = "Motorola WR850GP",
521 { .name = "reset", .gpio = 1 << 5 },
524 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
525 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
526 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
527 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
533 .name = "Belkin (unknown)",
534 /* FIXME: verify & add detection */
536 { .name = "reset", .gpio = 1 << 7 },
539 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
540 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
541 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
546 .name = "Netgear WGT634U",
548 { .name = "reset", .gpio = 1 << 2 },
551 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
556 .name = "Trendware TEW411BRP+",
558 { /* No usable buttons */ },
561 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
562 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
563 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
568 .name = "SimpleTech SimpleShare NAS",
570 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
571 { .name = "power", .gpio = 1 << 0 }, // on back
574 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
576 .platform_init = bcm4780_init,
580 .name = "D-Link DIR-130",
582 { .name = "reset", .gpio = 1 << 3},
583 { .name = "reserved", .gpio = 1 << 7},
586 { .name = "diag", .gpio = 1 << 0},
587 { .name = "blue", .gpio = 1 << 6},
591 .name = "D-Link DIR-330",
593 { .name = "reset", .gpio = 1 << 3},
594 { .name = "reserved", .gpio = 1 << 7},
597 { .name = "diag", .gpio = 1 << 0},
598 { .name = "usb", .gpio = 1 << 4},
599 { .name = "blue", .gpio = 1 << 6},
603 .name = "D-Link DWL-3150",
605 { .name = "reset", .gpio = 1 << 7},
608 { .name = "diag", .gpio = 1 << 2},
609 { .name = "status", .gpio = 1 << 1},
614 static struct platform_t __init *platform_detect(void)
616 char *boardnum, *boardtype, *buf;
618 if (strcmp(getvar("nvram_type"), "cfe") == 0)
619 return &platforms[WGT634U];
621 /* Look for a model identifier */
623 /* Based on "model_name" */
624 if (buf = nvram_get("model_name")) {
625 if (!strcmp(buf, "DIR-130"))
626 return &platforms[DIR130];
627 if (!strcmp(buf, "DIR-330"))
628 return &platforms[DIR330];
631 /* Based on "model_no" */
632 if (buf = nvram_get("model_no")) {
633 if (!strncmp(buf,"WL700", 5)) /* WL700* */
634 return &platforms[WL700GE];
637 /* Based on "ModelId" */
638 if (buf = nvram_get("ModelId")) {
639 if (!strcmp(buf, "WR850GP"))
640 return &platforms[WR850GP];
641 if (!strcmp(buf,"WX-5565"))
642 return &platforms[TM2300];
643 if (!strncmp(buf,"WE800G", 6)) /* WE800G* */
644 return &platforms[WE800G];
648 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
649 /* Buffalo hardware, check id for specific hardware matches */
650 if (!strcmp(buf, "29bb0332"))
651 return &platforms[WBR2_G54];
652 if (!strcmp(buf, "29129"))
653 return &platforms[WLA2_G54L];
654 if (!strcmp(buf, "30189"))
655 return &platforms[WHR_HP_G54];
656 if (!strcmp(buf, "32093"))
657 return &platforms[WHR_G125];
658 if (!strcmp(buf, "30182"))
659 return &platforms[WHR_G54S];
660 if (!strcmp(buf, "290441dd"))
661 return &platforms[WHR2_A54G54];
662 if (!strcmp(buf, "31120"))
663 return &platforms[WZR_G300N];
664 if (!strcmp(buf, "30083"))
665 return &platforms[WZR_RS_G54];
666 if (!strcmp(buf, "30103"))
667 return &platforms[WZR_RS_G54HP];
670 /* no easy model number, attempt to guess */
671 boardnum = getvar("boardnum");
672 boardtype = getvar("boardtype");
674 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
675 /* CFE based - newer hardware */
676 if (!strcmp(boardnum, "42")) { /* Linksys */
677 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
678 return &platforms[WRT350N];
680 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
681 return &platforms[WRT54G3G];
683 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
684 return &platforms[WRTSL54GS];
686 /* default to WRT54G */
687 return &platforms[WRT54G];
690 if (!strcmp(boardnum, "45")) { /* ASUS */
691 if (!strcmp(boardtype,"0x042f"))
692 return &platforms[WL500GP];
693 else if (!strcmp(boardtype,"0x0472"))
694 return &platforms[WL500W];
696 return &platforms[WL500GD];
699 if (!strcmp(boardnum, "10496"))
700 return &platforms[USR5461];
702 } else { /* PMON based - old stuff */
703 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
704 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
705 return &platforms[WR850GV1];
707 if (!strncmp(boardtype, "bcm94710dev", 11)) {
708 if (!strcmp(boardnum, "42"))
709 return &platforms[WRT54GV1];
710 if (simple_strtoul(boardnum, NULL, 0) == 2)
711 return &platforms[WAP54GV1];
713 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
714 return &platforms[WL500G];
715 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
716 /* Either WL-300g or WL-HDD, do more extensive checks */
717 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
718 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
719 return &platforms[WLHDD];
720 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
721 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
722 return &platforms[WL300G];
725 /* unknown asus stuff, probably bcm4702 */
726 if (!strncmp(boardnum, "asusX", 5))
727 return &platforms[ASUS_4702];
730 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
731 if (!strncmp(boardtype, "bcm94710ap", 10))
732 return &platforms[BUFFALO_UNKNOWN_4710];
734 return &platforms[BUFFALO_UNKNOWN];
737 if (!strncmp(getvar("CFEver"), "MotoWRv2", 8) ||
738 !strncmp(getvar("CFEver"), "MotoWRv3", 8) ||
739 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
741 return &platforms[WR850GV2V3];
744 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
745 return &platforms[TEW411BRPP];
748 if (!strncmp(boardnum, "04FN52", 6)) /* SimpleTech SimpleShare */
749 return &platforms[STI_NAS];
751 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
752 return &platforms[DWL3150];
758 static void register_buttons(struct button_t *b)
761 platform.button_mask |= b->gpio;
763 platform.button_mask &= ~gpiomask;
765 gpio_outen(platform.button_mask, 0);
766 gpio_control(platform.button_mask, 0);
767 platform.button_polarity = gpio_in() & platform.button_mask;
768 gpio_intpolarity(platform.button_mask, platform.button_polarity);
769 gpio_intmask(platform.button_mask, platform.button_mask);
771 gpio_set_irqenable(1, button_handler);
774 static void unregister_buttons(struct button_t *b)
776 gpio_intmask(platform.button_mask, 0);
778 gpio_set_irqenable(0, button_handler);
783 static void add_msg(struct event_t *event, char *msg, int argv)
790 s = skb_put(event->skb, strlen(msg) + 1);
794 static void hotplug_button(struct work_struct *work)
796 struct event_t *event = container_of(work, struct event_t, wq);
802 event->skb = alloc_skb(2048, GFP_KERNEL);
804 s = skb_put(event->skb, strlen(event->action) + 2);
805 sprintf(s, "%s@", event->action);
808 NETLINK_CB(event->skb).dst_group = 1;
809 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
814 #else /* !LINUX_2_4 */
815 static inline char *kzalloc(unsigned int size, unsigned int gfp)
819 p = kmalloc(size, gfp);
828 static void add_msg(struct event_t *event, char *msg, int argv)
831 event->argv[event->anr++] = event->scratch;
833 event->envp[event->enr++] = event->scratch;
835 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
838 static void hotplug_button(struct event_t *event)
840 char *scratch = kzalloc(256, GFP_KERNEL);
841 event->scratch = scratch;
843 add_msg(event, hotplug_path, 1);
844 add_msg(event, "button", 1);
846 call_usermodehelper (event->argv[0], event->argv, event->envp);
850 #endif /* !LINUX_2_4 */
852 static int fill_event (struct event_t *event)
854 static char buf[128];
856 add_msg(event, "HOME=/", 0);
857 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
858 add_msg(event, "SUBSYSTEM=button", 0);
859 snprintf(buf, 128, "ACTION=%s", event->action);
860 add_msg(event, buf, 0);
861 snprintf(buf, 128, "BUTTON=%s", event->name);
862 add_msg(event, buf, 0);
863 snprintf(buf, 128, "SEEN=%ld", event->seen);
864 add_msg(event, buf, 0);
866 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
867 add_msg(event, buf, 0);
875 static irqreturn_t button_handler(int irq, void *dev_id)
877 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
883 in = gpio_in() & platform.button_mask;
884 gpio_intpolarity(platform.button_mask, in);
885 changed = platform.button_polarity ^ in;
886 platform.button_polarity = in;
888 changed &= ~gpio_outen(0, 0);
890 for (b = platform.buttons; b->name; b++) {
891 struct event_t *event;
893 if (!(b->gpio & changed)) continue;
897 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
898 event->seen = (jiffies - b->seen)/HZ;
899 event->name = b->name;
900 event->action = b->pressed ? "pressed" : "released";
901 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
902 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
904 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
906 schedule_work(&event->wq);
914 static void register_leds(struct led_t *l)
916 struct proc_dir_entry *p;
921 leds = proc_mkdir("led", diag);
925 for(; l->name; l++) {
926 if (l->gpio & gpiomask)
929 if (l->gpio & GPIO_TYPE_EXTIF) {
933 if (l->polarity != INPUT) oe_mask != l->gpio;
935 val |= (l->polarity == NORMAL)?0:l->gpio;
938 if (l->polarity == INPUT) continue;
940 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
941 l->proc.type = PROC_LED;
943 p->data = (void *) &l->proc;
944 p->proc_fops = &diag_proc_fops;
948 gpio_outen(mask, oe_mask);
949 gpio_control(mask, 0);
953 static void unregister_leds(struct led_t *l)
956 remove_proc_entry(l->name, leds);
958 remove_proc_entry("led", diag);
961 static void set_led_extif(struct led_t *led)
963 gpio_set_extif(led->gpio, led->state);
966 static void led_flash(unsigned long dummy) {
971 for (l = platform.leds; l->name; l++) {
973 if (l->gpio & GPIO_TYPE_EXTIF) {
975 l->state = !l->state;
985 u32 val = ~gpio_in();
987 gpio_outen(mask, mask);
988 gpio_control(mask, 0);
991 if (mask || extif_blink) {
992 mod_timer(&led_timer, jiffies + FLASH_TIME);
996 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
999 struct inode *inode = file->f_dentry->d_inode;
1000 struct proc_dir_entry *dent = inode->u.generic_ip;
1002 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1007 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1010 if (dent->data != NULL) {
1011 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1012 switch (handler->type) {
1014 struct led_t * led = (struct led_t *) handler->ptr;
1016 len = sprintf(page, "f\n");
1018 if (led->gpio & GPIO_TYPE_EXTIF) {
1019 len = sprintf(page, "%d\n", led->state);
1021 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1022 u8 p = (led->polarity == NORMAL ? 0 : 1);
1023 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1029 len = sprintf(page, "%s\n", platform.name);
1032 len = sprintf(page, "0x%04x\n", gpiomask);
1039 len = min_t(int, len - *ppos, count);
1040 if (copy_to_user(buf, (page + *ppos), len)) {
1054 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1057 struct inode *inode = file->f_dentry->d_inode;
1058 struct proc_dir_entry *dent = inode->u.generic_ip;
1060 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1065 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1068 if (copy_from_user(page, buf, count)) {
1074 if (dent->data != NULL) {
1075 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1076 switch (handler->type) {
1078 struct led_t *led = (struct led_t *) handler->ptr;
1079 int p = (led->polarity == NORMAL ? 0 : 1);
1081 if (page[0] == 'f') {
1086 if (led->gpio & GPIO_TYPE_EXTIF) {
1087 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1090 gpio_outen(led->gpio, led->gpio);
1091 gpio_control(led->gpio, 0);
1092 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1098 gpiomask = simple_strtoul(page, NULL, 0);
1100 if (platform.buttons) {
1101 unregister_buttons(platform.buttons);
1102 register_buttons(platform.buttons);
1105 if (platform.leds) {
1106 unregister_leds(platform.leds);
1107 register_leds(platform.leds);
1118 static int __init diag_init(void)
1120 static struct proc_dir_entry *p;
1121 static struct platform_t *detected;
1123 detected = platform_detect();
1125 printk(MODULE_NAME ": Router model not detected.\n");
1128 memcpy(&platform, detected, sizeof(struct platform_t));
1130 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1131 if (platform.platform_init != NULL) {
1132 platform.platform_init();
1135 if (!(diag = proc_mkdir("diag", NULL))) {
1136 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1140 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1141 p->data = (void *) &proc_model;
1142 p->proc_fops = &diag_proc_fops;
1145 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1146 p->data = (void *) &proc_gpiomask;
1147 p->proc_fops = &diag_proc_fops;
1150 if (platform.buttons)
1151 register_buttons(platform.buttons);
1154 register_leds(platform.leds);
1159 static void __exit diag_exit(void)
1161 del_timer(&led_timer);
1163 if (platform.buttons)
1164 unregister_buttons(platform.buttons);
1167 unregister_leds(platform.leds);
1169 remove_proc_entry("model", diag);
1170 remove_proc_entry("gpiomask", diag);
1171 remove_proc_entry("diag", NULL);
1174 module_init(diag_init);
1175 module_exit(diag_exit);
1177 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1178 MODULE_LICENSE("GPL");