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>
6 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kmod.h>
27 #include <linux/proc_fs.h>
28 #include <linux/timer.h>
29 #include <linux/version.h>
30 #include <asm/uaccess.h>
33 #include <linux/workqueue.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
37 extern struct sock *uevent_sock;
38 extern u64 uevent_next_seqnum(void);
40 #include <linux/tqueue.h>
41 #define INIT_WORK INIT_TQUEUE
42 #define schedule_work schedule_task
43 #define work_struct tq_struct
48 #define getvar(str) (nvram_get(str)?:"")
50 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
51 static int fill_event(struct event_t *);
52 static unsigned int gpiomask = 0;
53 module_param(gpiomask, int, 0644);
127 static void __init bcm4780_init(void) {
130 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
131 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
132 gpio_outen(pin, pin);
133 gpio_control(pin, 0);
136 /* Wait 5s, so the HDD can spin up */
137 set_current_state(TASK_INTERRUPTIBLE);
138 schedule_timeout(HZ * 5);
141 static void __init bcm57xx_init(void) {
144 gpio_outen(pin, pin);
145 gpio_control(pin, 0);
149 static struct platform_t __initdata platforms[] = {
152 .name = "Linksys WAP54G V1",
154 { .name = "reset", .gpio = 1 << 0 },
157 { .name = "diag", .gpio = 1 << 3 },
158 { .name = "wlan", .gpio = 1 << 4 },
162 .name = "Linksys WAP54G V3",
164 /* FIXME: verify this */
165 { .name = "reset", .gpio = 1 << 7 },
166 { .name = "ses", .gpio = 1 << 0 },
170 { .name = "ses", .gpio = 1 << 1 },
174 .name = "Linksys WRT54G V1.x",
176 { .name = "reset", .gpio = 1 << 6 },
179 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
180 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
184 .name = "Linksys WRT54G/GS/GL",
186 { .name = "reset", .gpio = 1 << 6 },
187 { .name = "ses", .gpio = 1 << 4 },
190 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
191 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
192 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
193 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
194 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
198 .name = "Linksys WRTSL54GS",
200 { .name = "reset", .gpio = 1 << 6 },
201 { .name = "ses", .gpio = 1 << 4 },
204 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
205 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
206 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
207 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
211 .name = "Linksys WRT54G3G",
213 { .name = "reset", .gpio = 1 << 6 },
214 { .name = "3g", .gpio = 1 << 4 },
217 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
218 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
219 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
220 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
221 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
225 .name = "Linksys WRT350N",
227 { .name = "reset", .gpio = 1 << 6 },
228 { .name = "ses", .gpio = 1 << 8 },
231 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
232 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
233 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
234 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
235 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
237 .platform_init = bcm57xx_init,
241 .name = "ASUS WL-HDD",
243 { .name = "reset", .gpio = 1 << 6 },
246 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
247 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
251 .name = "ASUS WL-300g",
253 { .name = "reset", .gpio = 1 << 6 },
256 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
260 .name = "ASUS WL-500g",
262 { .name = "reset", .gpio = 1 << 6 },
265 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
269 .name = "ASUS WL-500g Deluxe",
271 { .name = "reset", .gpio = 1 << 6 },
274 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
278 .name = "ASUS WL-500g Premium",
280 { .name = "reset", .gpio = 1 << 0 },
281 { .name = "ses", .gpio = 1 << 4 },
284 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
288 .name = "ASUS WL-500g Premium V2",
290 { .name = "reset", .gpio = 1 << 2 },
291 { .name = "ses", .gpio = 1 << 3 },
294 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
295 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
299 .name = "ASUS WL-500W",
301 { .name = "reset", .gpio = 1 << 6 },
302 { .name = "ses", .gpio = 1 << 7 },
305 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
309 .name = "ASUS WL-520gU",
311 { .name = "reset", .gpio = 1 << 2 },
312 { .name = "ses", .gpio = 1 << 3 },
315 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
316 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
320 .name = "ASUS (unknown, BCM4702)",
322 { .name = "reset", .gpio = 1 << 6 },
325 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
329 .name = "ASUS WL-700gE",
331 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
332 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
333 { .name = "power", .gpio = 1 << 0 }, // on front
334 { .name = "copy", .gpio = 1 << 6 }, // on front
338 // GPIO that controls power led also enables/disables some essential functions
341 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
343 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
345 .platform_init = bcm4780_init,
349 .name = "Buffalo WHR-G54S",
351 { .name = "reset", .gpio = 1 << 4 },
352 { .name = "bridge", .gpio = 1 << 5 },
353 { .name = "ses", .gpio = 1 << 0 },
356 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
357 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
358 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
359 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
360 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
364 .name = "Buffalo WBR2-G54",
367 { .name = "reset", .gpio = 1 << 7 },
370 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
374 .name = "Buffalo WHR-HP-G54",
376 { .name = "reset", .gpio = 1 << 4 },
377 { .name = "bridge", .gpio = 1 << 5 },
378 { .name = "ses", .gpio = 1 << 0 },
381 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
382 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
383 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
384 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
385 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
389 .name = "Buffalo WHR-G125",
391 { .name = "reset", .gpio = 1 << 4 },
392 { .name = "bridge", .gpio = 1 << 5 },
393 { .name = "ses", .gpio = 1 << 0 },
396 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
397 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
398 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
399 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
400 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
404 .name = "Buffalo WHR2-A54G54",
406 { .name = "reset", .gpio = 1 << 4 },
409 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
413 .name = "Buffalo WLA2-G54L",
416 { .name = "reset", .gpio = 1 << 7 },
419 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
423 .name = "Buffalo WZR-G300N",
425 { .name = "reset", .gpio = 1 << 4 },
428 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
429 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
430 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
434 .name = "Buffalo WZR-RS-G54",
436 { .name = "ses", .gpio = 1 << 0 },
437 { .name = "reset", .gpio = 1 << 4 },
440 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
441 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
442 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
446 .name = "Buffalo WZR-RS-G54HP",
448 { .name = "ses", .gpio = 1 << 0 },
449 { .name = "reset", .gpio = 1 << 4 },
452 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
453 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
454 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
457 [BUFFALO_UNKNOWN] = {
458 .name = "Buffalo (unknown)",
460 { .name = "reset", .gpio = 1 << 7 },
463 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
466 [BUFFALO_UNKNOWN_4710] = {
467 .name = "Buffalo (unknown, BCM4710)",
469 { .name = "reset", .gpio = 1 << 4 },
472 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
477 .name = "Siemens SE505 V1",
479 /* No usable buttons */
482 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
483 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
487 .name = "Siemens SE505 V2",
489 /* No usable buttons */
492 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
493 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
494 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
499 .name = "U.S. Robotics USR5461",
501 /* No usable buttons */
504 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
505 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
510 .name = "Dell TrueMobile 2300",
512 { .name = "reset", .gpio = 1 << 0 },
515 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
516 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
521 .name = "Motorola WE800G",
523 { .name = "reset", .gpio = 1 << 0 },
526 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
527 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
528 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
532 .name = "Motorola WR850G V1",
534 { .name = "reset", .gpio = 1 << 0 },
537 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
538 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
539 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
540 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
541 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
545 .name = "Motorola WR850G V2/V3",
547 { .name = "reset", .gpio = 1 << 5 },
550 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
551 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
552 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
553 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
557 .name = "Motorola WR850GP",
559 { .name = "reset", .gpio = 1 << 5 },
562 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
563 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
564 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
565 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
571 .name = "Belkin (unknown)",
572 /* FIXME: verify & add detection */
574 { .name = "reset", .gpio = 1 << 7 },
577 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
578 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
579 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
584 .name = "Netgear WGT634U",
586 { .name = "reset", .gpio = 1 << 2 },
589 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
594 .name = "Trendware TEW411BRP+",
596 { /* No usable buttons */ },
599 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
600 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
601 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
606 .name = "SimpleTech SimpleShare NAS",
608 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
609 { .name = "power", .gpio = 1 << 0 }, // on back
612 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
614 .platform_init = bcm4780_init,
618 .name = "D-Link DIR-130",
620 { .name = "reset", .gpio = 1 << 3},
621 { .name = "reserved", .gpio = 1 << 7},
624 { .name = "diag", .gpio = 1 << 0},
625 { .name = "blue", .gpio = 1 << 6},
629 .name = "D-Link DIR-330",
631 { .name = "reset", .gpio = 1 << 3},
632 { .name = "reserved", .gpio = 1 << 7},
635 { .name = "diag", .gpio = 1 << 0},
636 { .name = "usb", .gpio = 1 << 4},
637 { .name = "blue", .gpio = 1 << 6},
641 .name = "D-Link DWL-3150",
643 { .name = "reset", .gpio = 1 << 7},
646 { .name = "diag", .gpio = 1 << 2},
647 { .name = "status", .gpio = 1 << 1},
652 .name = "Sitecom WL-105b",
654 { .name = "reset", .gpio = 1 << 10},
657 { .name = "wlan", .gpio = 1 << 4},
658 { .name = "power", .gpio = 1 << 3},
663 static struct platform_t __init *platform_detect(void)
665 char *boardnum, *boardtype, *buf;
667 if (strcmp(getvar("nvram_type"), "cfe") == 0)
668 return &platforms[WGT634U];
670 /* Look for a model identifier */
672 /* Based on "model_name" */
673 if (buf = nvram_get("model_name")) {
674 if (!strcmp(buf, "DIR-130"))
675 return &platforms[DIR130];
676 if (!strcmp(buf, "DIR-330"))
677 return &platforms[DIR330];
680 /* Based on "model_no" */
681 if (buf = nvram_get("model_no")) {
682 if (startswith(buf,"WL700")) /* WL700* */
683 return &platforms[WL700GE];
686 /* Based on "hardware_version" */
687 if (buf = nvram_get("hardware_version")) {
688 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
689 return &platforms[WL500GPV2];
690 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
691 return &platforms[WL520GU];
694 /* Based on "ModelId" */
695 if (buf = nvram_get("ModelId")) {
696 if (!strcmp(buf, "WR850GP"))
697 return &platforms[WR850GP];
698 if (!strcmp(buf,"WX-5565"))
699 return &platforms[TM2300];
700 if (startswith(buf,"WE800G")) /* WE800G* */
701 return &platforms[WE800G];
705 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
706 /* Buffalo hardware, check id for specific hardware matches */
707 if (!strcmp(buf, "29bb0332"))
708 return &platforms[WBR2_G54];
709 if (!strcmp(buf, "29129"))
710 return &platforms[WLA2_G54L];
711 if (!strcmp(buf, "30189"))
712 return &platforms[WHR_HP_G54];
713 if (!strcmp(buf, "32093"))
714 return &platforms[WHR_G125];
715 if (!strcmp(buf, "30182"))
716 return &platforms[WHR_G54S];
717 if (!strcmp(buf, "290441dd"))
718 return &platforms[WHR2_A54G54];
719 if (!strcmp(buf, "31120"))
720 return &platforms[WZR_G300N];
721 if (!strcmp(buf, "30083"))
722 return &platforms[WZR_RS_G54];
723 if (!strcmp(buf, "30103"))
724 return &platforms[WZR_RS_G54HP];
727 /* no easy model number, attempt to guess */
728 boardnum = getvar("boardnum");
729 boardtype = getvar("boardtype");
731 if (startswith(getvar("pmon_ver"), "CFE")) {
732 /* CFE based - newer hardware */
733 if (!strcmp(boardnum, "42")) { /* Linksys */
734 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
735 return &platforms[WRT350N];
737 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
738 return &platforms[WRT54G3G];
740 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
741 return &platforms[WRTSL54GS];
743 /* default to WRT54G */
744 return &platforms[WRT54G];
747 if (!strcmp(boardnum, "45")) { /* ASUS */
748 if (!strcmp(boardtype,"0x042f"))
749 return &platforms[WL500GP];
750 else if (!strcmp(boardtype,"0x0472"))
751 return &platforms[WL500W];
753 return &platforms[WL500GD];
756 if (!strcmp(boardnum, "10496"))
757 return &platforms[USR5461];
759 } else { /* PMON based - old stuff */
760 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
761 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
762 return &platforms[WR850GV1];
764 if (startswith(boardtype, "bcm94710dev")) {
765 if (!strcmp(boardnum, "42"))
766 return &platforms[WRT54GV1];
767 if (simple_strtoul(boardnum, NULL, 0) == 2)
768 return &platforms[WAP54GV1];
770 if (startswith(getvar("hardware_version"), "WL500-"))
771 return &platforms[WL500G];
772 if (startswith(getvar("hardware_version"), "WL300-")) {
773 /* Either WL-300g or WL-HDD, do more extensive checks */
774 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
775 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
776 return &platforms[WLHDD];
777 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
778 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
779 return &platforms[WL300G];
781 /* Sitecom WL-105b */
782 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
783 return &platforms[WL105B];
785 /* unknown asus stuff, probably bcm4702 */
786 if (startswith(boardnum, "asusX"))
787 return &platforms[ASUS_4702];
790 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
791 if (startswith(boardtype, "bcm94710ap"))
792 return &platforms[BUFFALO_UNKNOWN_4710];
794 return &platforms[BUFFALO_UNKNOWN];
797 if (startswith(getvar("CFEver"), "MotoWRv2") ||
798 startswith(getvar("CFEver"), "MotoWRv3") ||
799 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
801 return &platforms[WR850GV2V3];
804 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
805 return &platforms[TEW411BRPP];
808 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
809 return &platforms[STI_NAS];
811 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
812 return &platforms[DWL3150];
818 static void register_buttons(struct button_t *b)
821 platform.button_mask |= b->gpio;
823 platform.button_mask &= ~gpiomask;
825 gpio_outen(platform.button_mask, 0);
826 gpio_control(platform.button_mask, 0);
827 platform.button_polarity = gpio_in() & platform.button_mask;
828 gpio_intpolarity(platform.button_mask, platform.button_polarity);
829 gpio_intmask(platform.button_mask, platform.button_mask);
831 gpio_set_irqenable(1, button_handler);
834 static void unregister_buttons(struct button_t *b)
836 gpio_intmask(platform.button_mask, 0);
838 gpio_set_irqenable(0, button_handler);
843 static void add_msg(struct event_t *event, char *msg, int argv)
850 s = skb_put(event->skb, strlen(msg) + 1);
854 static void hotplug_button(struct work_struct *work)
856 struct event_t *event = container_of(work, struct event_t, wq);
862 event->skb = alloc_skb(2048, GFP_KERNEL);
864 s = skb_put(event->skb, strlen(event->action) + 2);
865 sprintf(s, "%s@", event->action);
868 NETLINK_CB(event->skb).dst_group = 1;
869 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
874 #else /* !LINUX_2_4 */
875 static inline char *kzalloc(unsigned int size, unsigned int gfp)
879 p = kmalloc(size, gfp);
888 static void add_msg(struct event_t *event, char *msg, int argv)
891 event->argv[event->anr++] = event->scratch;
893 event->envp[event->enr++] = event->scratch;
895 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
898 static void hotplug_button(struct event_t *event)
900 char *scratch = kzalloc(256, GFP_KERNEL);
901 event->scratch = scratch;
903 add_msg(event, hotplug_path, 1);
904 add_msg(event, "button", 1);
906 call_usermodehelper (event->argv[0], event->argv, event->envp);
910 #endif /* !LINUX_2_4 */
912 static int fill_event (struct event_t *event)
914 static char buf[128];
916 add_msg(event, "HOME=/", 0);
917 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
918 add_msg(event, "SUBSYSTEM=button", 0);
919 snprintf(buf, 128, "ACTION=%s", event->action);
920 add_msg(event, buf, 0);
921 snprintf(buf, 128, "BUTTON=%s", event->name);
922 add_msg(event, buf, 0);
923 snprintf(buf, 128, "SEEN=%ld", event->seen);
924 add_msg(event, buf, 0);
926 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
927 add_msg(event, buf, 0);
935 static irqreturn_t button_handler(int irq, void *dev_id)
937 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
943 in = gpio_in() & platform.button_mask;
944 gpio_intpolarity(platform.button_mask, in);
945 changed = platform.button_polarity ^ in;
946 platform.button_polarity = in;
948 changed &= ~gpio_outen(0, 0);
950 for (b = platform.buttons; b->name; b++) {
951 struct event_t *event;
953 if (!(b->gpio & changed)) continue;
957 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
958 event->seen = (jiffies - b->seen)/HZ;
959 event->name = b->name;
960 event->action = b->pressed ? "pressed" : "released";
961 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
962 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
964 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
966 schedule_work(&event->wq);
974 static void register_leds(struct led_t *l)
976 struct proc_dir_entry *p;
981 leds = proc_mkdir("led", diag);
985 for(; l->name; l++) {
986 if (l->gpio & gpiomask)
989 if (l->gpio & GPIO_TYPE_EXTIF) {
993 if (l->polarity != INPUT) oe_mask != l->gpio;
995 val |= (l->polarity == NORMAL)?0:l->gpio;
998 if (l->polarity == INPUT) continue;
1000 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1001 l->proc.type = PROC_LED;
1003 p->data = (void *) &l->proc;
1004 p->proc_fops = &diag_proc_fops;
1008 gpio_outen(mask, oe_mask);
1009 gpio_control(mask, 0);
1010 gpio_out(mask, val);
1013 static void unregister_leds(struct led_t *l)
1016 remove_proc_entry(l->name, leds);
1018 remove_proc_entry("led", diag);
1021 static void set_led_extif(struct led_t *led)
1023 gpio_set_extif(led->gpio, led->state);
1026 static void led_flash(unsigned long dummy) {
1031 for (l = platform.leds; l->name; l++) {
1033 if (l->gpio & GPIO_TYPE_EXTIF) {
1035 l->state = !l->state;
1045 u32 val = ~gpio_in();
1047 gpio_outen(mask, mask);
1048 gpio_control(mask, 0);
1049 gpio_out(mask, val);
1051 if (mask || extif_blink) {
1052 mod_timer(&led_timer, jiffies + FLASH_TIME);
1056 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1059 struct inode *inode = file->f_dentry->d_inode;
1060 struct proc_dir_entry *dent = inode->u.generic_ip;
1062 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1067 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1070 if (dent->data != NULL) {
1071 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1072 switch (handler->type) {
1074 struct led_t * led = (struct led_t *) handler->ptr;
1076 len = sprintf(page, "f\n");
1078 if (led->gpio & GPIO_TYPE_EXTIF) {
1079 len = sprintf(page, "%d\n", led->state);
1081 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1082 u8 p = (led->polarity == NORMAL ? 0 : 1);
1083 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1089 len = sprintf(page, "%s\n", platform.name);
1092 len = sprintf(page, "0x%04x\n", gpiomask);
1099 len = min_t(int, len - *ppos, count);
1100 if (copy_to_user(buf, (page + *ppos), len)) {
1114 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1117 struct inode *inode = file->f_dentry->d_inode;
1118 struct proc_dir_entry *dent = inode->u.generic_ip;
1120 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1125 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1128 if (copy_from_user(page, buf, count)) {
1134 if (dent->data != NULL) {
1135 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1136 switch (handler->type) {
1138 struct led_t *led = (struct led_t *) handler->ptr;
1139 int p = (led->polarity == NORMAL ? 0 : 1);
1141 if (page[0] == 'f') {
1146 if (led->gpio & GPIO_TYPE_EXTIF) {
1147 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1150 gpio_outen(led->gpio, led->gpio);
1151 gpio_control(led->gpio, 0);
1152 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1158 gpiomask = simple_strtoul(page, NULL, 0);
1160 if (platform.buttons) {
1161 unregister_buttons(platform.buttons);
1162 register_buttons(platform.buttons);
1165 if (platform.leds) {
1166 unregister_leds(platform.leds);
1167 register_leds(platform.leds);
1178 static int __init diag_init(void)
1180 static struct proc_dir_entry *p;
1181 static struct platform_t *detected;
1183 detected = platform_detect();
1185 printk(MODULE_NAME ": Router model not detected.\n");
1188 memcpy(&platform, detected, sizeof(struct platform_t));
1190 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1191 if (platform.platform_init != NULL) {
1192 platform.platform_init();
1195 if (!(diag = proc_mkdir("diag", NULL))) {
1196 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1200 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1201 p->data = (void *) &proc_model;
1202 p->proc_fops = &diag_proc_fops;
1205 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1206 p->data = (void *) &proc_gpiomask;
1207 p->proc_fops = &diag_proc_fops;
1210 if (platform.buttons)
1211 register_buttons(platform.buttons);
1214 register_leds(platform.leds);
1219 static void __exit diag_exit(void)
1221 del_timer(&led_timer);
1223 if (platform.buttons)
1224 unregister_buttons(platform.buttons);
1227 unregister_leds(platform.leds);
1229 remove_proc_entry("model", diag);
1230 remove_proc_entry("gpiomask", diag);
1231 remove_proc_entry("diag", NULL);
1234 module_init(diag_init);
1235 module_exit(diag_exit);
1237 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1238 MODULE_LICENSE("GPL");