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);
119 static void __init bcm4780_init(void) {
122 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
123 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
124 gpio_outen(pin, pin);
125 gpio_control(pin, 0);
128 /* Wait 5s, so the HDD can spin up */
129 set_current_state(TASK_INTERRUPTIBLE);
130 schedule_timeout(HZ * 5);
133 static struct platform_t __initdata platforms[] = {
136 .name = "Linksys WAP54G V1",
138 { .name = "reset", .gpio = 1 << 0 },
141 { .name = "diag", .gpio = 1 << 3 },
142 { .name = "wlan", .gpio = 1 << 4 },
146 .name = "Linksys WAP54G V3",
148 /* FIXME: verify this */
149 { .name = "reset", .gpio = 1 << 7 },
150 { .name = "ses", .gpio = 1 << 0 },
154 { .name = "ses", .gpio = 1 << 1 },
158 .name = "Linksys WRT54G V1.x",
160 { .name = "reset", .gpio = 1 << 6 },
163 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
164 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
168 .name = "Linksys WRT54G/GS/GL",
170 { .name = "reset", .gpio = 1 << 6 },
171 { .name = "ses", .gpio = 1 << 4 },
174 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
175 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
176 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
177 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
178 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
182 .name = "Linksys WRTSL54GS",
184 { .name = "reset", .gpio = 1 << 6 },
185 { .name = "ses", .gpio = 1 << 4 },
188 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
189 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
190 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
191 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
195 .name = "Linksys WRT54G3G",
197 { .name = "reset", .gpio = 1 << 6 },
198 { .name = "3g", .gpio = 1 << 4 },
201 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
202 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
203 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
204 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
205 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
209 .name = "Linksys WRT350N",
211 { .name = "reset", .gpio = 1 << 6 },
212 { .name = "ses", .gpio = 1 << 8 },
215 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
216 { .name = "ses", .gpio = 1 << 3, .polarity = REVERSE },
221 .name = "ASUS WL-HDD",
223 { .name = "reset", .gpio = 1 << 6 },
226 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
227 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
231 .name = "ASUS WL-300g",
233 { .name = "reset", .gpio = 1 << 6 },
236 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
240 .name = "ASUS WL-500g",
242 { .name = "reset", .gpio = 1 << 6 },
245 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
249 .name = "ASUS WL-500g Deluxe",
251 { .name = "reset", .gpio = 1 << 6 },
254 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
258 .name = "ASUS WL-500g Premium",
260 { .name = "reset", .gpio = 1 << 0 },
261 { .name = "ses", .gpio = 1 << 4 },
264 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
268 .name = "ASUS WL-500W",
270 { .name = "reset", .gpio = 1 << 6 },
271 { .name = "ses", .gpio = 1 << 7 },
274 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
278 .name = "ASUS (unknown, BCM4702)",
280 { .name = "reset", .gpio = 1 << 6 },
283 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
287 .name = "ASUS WL-700gE",
289 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
290 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
291 { .name = "power", .gpio = 1 << 0 }, // on front
292 { .name = "copy", .gpio = 1 << 6 }, // on front
296 // GPIO that controls power led also enables/disables some essential functions
299 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
301 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
303 .platform_init = bcm4780_init,
307 .name = "Buffalo WHR-G54S",
309 { .name = "reset", .gpio = 1 << 4 },
310 { .name = "bridge", .gpio = 1 << 5 },
311 { .name = "ses", .gpio = 1 << 0 },
314 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
315 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
316 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
317 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
318 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
322 .name = "Buffalo WBR2-G54",
325 { .name = "reset", .gpio = 1 << 7 },
328 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
332 .name = "Buffalo WHR-HP-G54",
334 { .name = "reset", .gpio = 1 << 4 },
335 { .name = "bridge", .gpio = 1 << 5 },
336 { .name = "ses", .gpio = 1 << 0 },
339 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
340 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
341 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
342 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
343 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
347 .name = "Buffalo WHR2-A54G54",
349 { .name = "reset", .gpio = 1 << 4 },
352 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
356 .name = "Buffalo WLA2-G54L",
359 { .name = "reset", .gpio = 1 << 7 },
362 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
366 .name = "Buffalo WZR-G300N",
368 { .name = "reset", .gpio = 1 << 4 },
371 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
372 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
373 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
377 .name = "Buffalo WZR-RS-G54",
379 { .name = "ses", .gpio = 1 << 0 },
380 { .name = "reset", .gpio = 1 << 4 },
383 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
384 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
385 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
389 .name = "Buffalo WZR-RS-G54HP",
391 { .name = "ses", .gpio = 1 << 0 },
392 { .name = "reset", .gpio = 1 << 4 },
395 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
396 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
397 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
400 [BUFFALO_UNKNOWN] = {
401 .name = "Buffalo (unknown)",
403 { .name = "reset", .gpio = 1 << 7 },
406 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
409 [BUFFALO_UNKNOWN_4710] = {
410 .name = "Buffalo (unknown, BCM4710)",
412 { .name = "reset", .gpio = 1 << 4 },
415 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
420 .name = "Siemens SE505 V1",
422 /* No usable buttons */
425 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
426 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
430 .name = "Siemens SE505 V2",
432 /* No usable buttons */
435 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
436 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
437 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
442 .name = "U.S. Robotics USR5461",
444 /* No usable buttons */
447 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
448 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
453 .name = "Dell TrueMobile 2300",
455 { .name = "reset", .gpio = 1 << 0 },
458 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
459 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
464 .name = "Motorola WE800G",
466 { .name = "reset", .gpio = 1 << 0 },
469 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
470 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
471 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
475 .name = "Motorola WR850G V1",
477 { .name = "reset", .gpio = 1 << 0 },
480 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
481 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
482 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
483 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
484 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
488 .name = "Motorola WR850G V2/V3",
490 { .name = "reset", .gpio = 1 << 5 },
493 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
494 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
495 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
496 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
500 .name = "Motorola WR850GP",
502 { .name = "reset", .gpio = 1 << 5 },
505 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
506 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
507 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
508 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
514 .name = "Belkin (unknown)",
515 /* FIXME: verify & add detection */
517 { .name = "reset", .gpio = 1 << 7 },
520 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
521 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
522 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
527 .name = "Netgear WGT634U",
529 { .name = "reset", .gpio = 1 << 2 },
532 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
537 .name = "Trendware TEW411BRP+",
539 { /* No usable buttons */ },
542 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
543 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
544 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
549 .name = "SimpleTech SimpleShare NAS",
551 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
552 { .name = "power", .gpio = 1 << 0 }, // on back
555 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
557 .platform_init = bcm4780_init,
561 .name = "D-Link DIR-130",
563 { .name = "reset", .gpio = 1 << 3},
564 { .name = "reserved", .gpio = 1 << 7},
567 { .name = "diag", .gpio = 1 << 0},
568 { .name = "blue", .gpio = 1 << 6},
572 .name = "D-Link DIR-330",
574 { .name = "reset", .gpio = 1 << 3},
575 { .name = "reserved", .gpio = 1 << 7},
578 { .name = "diag", .gpio = 1 << 0},
579 { .name = "usb", .gpio = 1 << 4},
580 { .name = "blue", .gpio = 1 << 6},
584 .name = "D-Link DWL-3150",
586 { .name = "reset", .gpio = 1 << 7},
589 { .name = "diag", .gpio = 1 << 2},
590 { .name = "status", .gpio = 1 << 1},
595 static struct platform_t __init *platform_detect(void)
597 char *boardnum, *boardtype, *buf;
599 if (strcmp(getvar("nvram_type"), "cfe") == 0)
600 return &platforms[WGT634U];
602 /* Look for a model identifier */
604 /* Based on "model_name" */
605 if (buf = nvram_get("model_name")) {
606 if (!strcmp(buf, "DIR-130"))
607 return &platforms[DIR130];
608 if (!strcmp(buf, "DIR-330"))
609 return &platforms[DIR330];
612 /* Based on "model_no" */
613 if (buf = nvram_get("model_no")) {
614 if (!strncmp(buf,"WL700", 5)) /* WL700* */
615 return &platforms[WL700GE];
618 /* Based on "ModelId" */
619 if (buf = nvram_get("ModelId")) {
620 if (!strcmp(buf, "WR850GP"))
621 return &platforms[WR850GP];
622 if (!strcmp(buf,"WX-5565"))
623 return &platforms[TM2300];
624 if (!strncmp(buf,"WE800G", 6)) /* WE800G* */
625 return &platforms[WE800G];
629 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
630 /* Buffalo hardware, check id for specific hardware matches */
631 if (!strcmp(buf, "29bb0332"))
632 return &platforms[WBR2_G54];
633 if (!strcmp(buf, "29129"))
634 return &platforms[WLA2_G54L];
635 if (!strcmp(buf, "30189"))
636 return &platforms[WHR_HP_G54];
637 if (!strcmp(buf, "30182"))
638 return &platforms[WHR_G54S];
639 if (!strcmp(buf, "290441dd"))
640 return &platforms[WHR2_A54G54];
641 if (!strcmp(buf, "31120"))
642 return &platforms[WZR_G300N];
643 if (!strcmp(buf, "30083"))
644 return &platforms[WZR_RS_G54];
645 if (!strcmp(buf, "30103"))
646 return &platforms[WZR_RS_G54HP];
649 /* no easy model number, attempt to guess */
650 boardnum = getvar("boardnum");
651 boardtype = getvar("boardtype");
653 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
654 /* CFE based - newer hardware */
655 if (!strcmp(boardnum, "42")) { /* Linksys */
656 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
657 return &platforms[WRT350N];
659 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
660 return &platforms[WRT54G3G];
662 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
663 return &platforms[WRTSL54GS];
665 /* default to WRT54G */
666 return &platforms[WRT54G];
669 if (!strcmp(boardnum, "45")) { /* ASUS */
670 if (!strcmp(boardtype,"0x042f"))
671 return &platforms[WL500GP];
672 else if (!strcmp(boardtype,"0x0472"))
673 return &platforms[WL500W];
675 return &platforms[WL500GD];
678 if (!strcmp(boardnum, "10496") && !strcmp(boardtype, "0x467"))
679 return &platforms[USR5461];
681 } else { /* PMON based - old stuff */
682 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
683 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
684 return &platforms[WR850GV1];
686 if (!strncmp(boardtype, "bcm94710dev", 11)) {
687 if (!strcmp(boardnum, "42"))
688 return &platforms[WRT54GV1];
689 if (simple_strtoul(boardnum, NULL, 0) == 2)
690 return &platforms[WAP54GV1];
692 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
693 return &platforms[WL500G];
694 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
695 /* Either WL-300g or WL-HDD, do more extensive checks */
696 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
697 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
698 return &platforms[WLHDD];
699 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
700 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
701 return &platforms[WL300G];
704 /* unknown asus stuff, probably bcm4702 */
705 if (!strncmp(boardnum, "asusX", 5))
706 return &platforms[ASUS_4702];
709 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
710 if (!strncmp(boardtype, "bcm94710ap", 10))
711 return &platforms[BUFFALO_UNKNOWN_4710];
713 return &platforms[BUFFALO_UNKNOWN];
716 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
717 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
719 return &platforms[WR850GV2V3];
722 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
723 return &platforms[TEW411BRPP];
726 if (!strncmp(boardnum, "04FN52", 6)) /* SimpleTech SimpleShare */
727 return &platforms[STI_NAS];
729 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
730 return &platforms[DWL3150];
736 static void register_buttons(struct button_t *b)
739 platform.button_mask |= b->gpio;
741 platform.button_mask &= ~gpiomask;
743 gpio_outen(platform.button_mask, 0);
744 gpio_control(platform.button_mask, 0);
745 platform.button_polarity = gpio_in() & platform.button_mask;
746 gpio_intpolarity(platform.button_mask, platform.button_polarity);
747 gpio_intmask(platform.button_mask, platform.button_mask);
749 gpio_set_irqenable(1, button_handler);
752 static void unregister_buttons(struct button_t *b)
754 gpio_intmask(platform.button_mask, 0);
756 gpio_set_irqenable(0, button_handler);
761 static void add_msg(struct event_t *event, char *msg, int argv)
768 s = skb_put(event->skb, strlen(msg) + 1);
772 static void hotplug_button(struct work_struct *work)
774 struct event_t *event = container_of(work, struct event_t, wq);
780 event->skb = alloc_skb(2048, GFP_KERNEL);
782 s = skb_put(event->skb, strlen(event->action) + 2);
783 sprintf(s, "%s@", event->action);
786 NETLINK_CB(event->skb).dst_group = 1;
787 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
792 #else /* !LINUX_2_4 */
793 static inline char *kzalloc(unsigned int size, unsigned int gfp)
797 p = kmalloc(size, gfp);
806 static void add_msg(struct event_t *event, char *msg, int argv)
809 event->argv[event->anr++] = event->scratch;
811 event->envp[event->enr++] = event->scratch;
813 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
816 static void hotplug_button(struct event_t *event)
818 char *scratch = kzalloc(256, GFP_KERNEL);
819 event->scratch = scratch;
821 add_msg(event, hotplug_path, 1);
822 add_msg(event, "button", 1);
824 call_usermodehelper (event->argv[0], event->argv, event->envp);
828 #endif /* !LINUX_2_4 */
830 static int fill_event (struct event_t *event)
832 static char buf[128];
834 add_msg(event, "HOME=/", 0);
835 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
836 add_msg(event, "SUBSYSTEM=button", 0);
837 snprintf(buf, 128, "ACTION=%s", event->action);
838 add_msg(event, buf, 0);
839 snprintf(buf, 128, "BUTTON=%s", event->name);
840 add_msg(event, buf, 0);
841 snprintf(buf, 128, "SEEN=%ld", event->seen);
842 add_msg(event, buf, 0);
844 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
845 add_msg(event, buf, 0);
853 static irqreturn_t button_handler(int irq, void *dev_id)
855 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
861 in = gpio_in() & platform.button_mask;
862 gpio_intpolarity(platform.button_mask, in);
863 changed = platform.button_polarity ^ in;
864 platform.button_polarity = in;
866 changed &= ~gpio_outen(0, 0);
868 for (b = platform.buttons; b->name; b++) {
869 struct event_t *event;
871 if (!(b->gpio & changed)) continue;
875 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
876 event->seen = (jiffies - b->seen)/HZ;
877 event->name = b->name;
878 event->action = b->pressed ? "pressed" : "released";
879 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
880 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
882 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
884 schedule_work(&event->wq);
892 static void register_leds(struct led_t *l)
894 struct proc_dir_entry *p;
898 leds = proc_mkdir("led", diag);
902 for(; l->name; l++) {
903 if (l->gpio & gpiomask)
906 if (l->gpio & GPIO_TYPE_EXTIF) {
911 val |= (l->polarity == NORMAL)?0:l->gpio;
914 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
915 l->proc.type = PROC_LED;
917 p->data = (void *) &l->proc;
918 p->proc_fops = &diag_proc_fops;
922 gpio_outen(mask, mask);
923 gpio_control(mask, 0);
927 static void unregister_leds(struct led_t *l)
930 remove_proc_entry(l->name, leds);
932 remove_proc_entry("led", diag);
935 static void set_led_extif(struct led_t *led)
937 gpio_set_extif(led->gpio, led->state);
940 static void led_flash(unsigned long dummy) {
945 for (l = platform.leds; l->name; l++) {
947 if (l->gpio & GPIO_TYPE_EXTIF) {
949 l->state = !l->state;
959 u32 val = ~gpio_in();
961 gpio_outen(mask, mask);
962 gpio_control(mask, 0);
965 if (mask || extif_blink) {
966 mod_timer(&led_timer, jiffies + FLASH_TIME);
970 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
973 struct inode *inode = file->f_dentry->d_inode;
974 struct proc_dir_entry *dent = inode->u.generic_ip;
976 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
981 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
984 if (dent->data != NULL) {
985 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
986 switch (handler->type) {
988 struct led_t * led = (struct led_t *) handler->ptr;
990 len = sprintf(page, "f\n");
992 if (led->gpio & GPIO_TYPE_EXTIF) {
993 len = sprintf(page, "%d\n", led->state);
995 u32 in = (gpio_in() & led->gpio ? 1 : 0);
996 u8 p = (led->polarity == NORMAL ? 0 : 1);
997 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1003 len = sprintf(page, "%s\n", platform.name);
1006 len = sprintf(page, "0x%04x\n", gpiomask);
1013 len = min_t(int, len - *ppos, count);
1014 if (copy_to_user(buf, (page + *ppos), len)) {
1028 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1031 struct inode *inode = file->f_dentry->d_inode;
1032 struct proc_dir_entry *dent = inode->u.generic_ip;
1034 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1039 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1042 if (copy_from_user(page, buf, count)) {
1048 if (dent->data != NULL) {
1049 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1050 switch (handler->type) {
1052 struct led_t *led = (struct led_t *) handler->ptr;
1053 int p = (led->polarity == NORMAL ? 0 : 1);
1055 if (page[0] == 'f') {
1060 if (led->gpio & GPIO_TYPE_EXTIF) {
1061 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1064 gpio_outen(led->gpio, led->gpio);
1065 gpio_control(led->gpio, 0);
1066 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1072 gpiomask = simple_strtoul(page, NULL, 0);
1074 if (platform.buttons) {
1075 unregister_buttons(platform.buttons);
1076 register_buttons(platform.buttons);
1079 if (platform.leds) {
1080 unregister_leds(platform.leds);
1081 register_leds(platform.leds);
1092 static int __init diag_init(void)
1094 static struct proc_dir_entry *p;
1095 static struct platform_t *detected;
1097 detected = platform_detect();
1099 printk(MODULE_NAME ": Router model not detected.\n");
1102 memcpy(&platform, detected, sizeof(struct platform_t));
1104 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1105 if (platform.platform_init != NULL) {
1106 platform.platform_init();
1109 if (!(diag = proc_mkdir("diag", NULL))) {
1110 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1114 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1115 p->data = (void *) &proc_model;
1116 p->proc_fops = &diag_proc_fops;
1119 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1120 p->data = (void *) &proc_gpiomask;
1121 p->proc_fops = &diag_proc_fops;
1124 if (platform.buttons)
1125 register_buttons(platform.buttons);
1128 register_leds(platform.leds);
1133 static void __exit diag_exit(void)
1135 del_timer(&led_timer);
1137 if (platform.buttons)
1138 unregister_buttons(platform.buttons);
1141 unregister_leds(platform.leds);
1143 remove_proc_entry("model", diag);
1144 remove_proc_entry("gpiomask", diag);
1145 remove_proc_entry("diag", NULL);
1148 module_init(diag_init);
1149 module_exit(diag_exit);
1151 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1152 MODULE_LICENSE("GPL");