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 void __init
bcm57xx_init(void) {
137 gpio_outen(pin
, pin
);
138 gpio_control(pin
, 0);
142 static struct platform_t __initdata platforms
[] = {
145 .name
= "Linksys WAP54G V1",
147 { .name
= "reset", .gpio
= 1 << 0 },
150 { .name
= "diag", .gpio
= 1 << 3 },
151 { .name
= "wlan", .gpio
= 1 << 4 },
155 .name
= "Linksys WAP54G V3",
157 /* FIXME: verify this */
158 { .name
= "reset", .gpio
= 1 << 7 },
159 { .name
= "ses", .gpio
= 1 << 0 },
163 { .name
= "ses", .gpio
= 1 << 1 },
167 .name
= "Linksys WRT54G V1.x",
169 { .name
= "reset", .gpio
= 1 << 6 },
172 { .name
= "diag", .gpio
= 0x13 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
173 { .name
= "dmz", .gpio
= 0x12 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
177 .name
= "Linksys WRT54G/GS/GL",
179 { .name
= "reset", .gpio
= 1 << 6 },
180 { .name
= "ses", .gpio
= 1 << 4 },
183 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
184 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
185 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
186 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
187 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
191 .name
= "Linksys WRTSL54GS",
193 { .name
= "reset", .gpio
= 1 << 6 },
194 { .name
= "ses", .gpio
= 1 << 4 },
197 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
198 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
199 { .name
= "ses_white", .gpio
= 1 << 5, .polarity
= REVERSE
},
200 { .name
= "ses_orange", .gpio
= 1 << 7, .polarity
= REVERSE
},
204 .name
= "Linksys WRT54G3G",
206 { .name
= "reset", .gpio
= 1 << 6 },
207 { .name
= "3g", .gpio
= 1 << 4 },
210 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
211 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
212 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
213 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
214 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
218 .name
= "Linksys WRT350N",
220 { .name
= "reset", .gpio
= 1 << 6 },
221 { .name
= "ses", .gpio
= 1 << 8 },
224 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
225 { .name
= "ses_amber", .gpio
= 1 << 3, .polarity
= REVERSE
},
226 { .name
= "ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
},
227 { .name
= "usb_blink", .gpio
= 1 << 10, .polarity
= REVERSE
},
228 { .name
= "usb", .gpio
= 1 << 11, .polarity
= REVERSE
},
230 .platform_init
= bcm57xx_init
,
234 .name
= "ASUS WL-HDD",
236 { .name
= "reset", .gpio
= 1 << 6 },
239 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
240 { .name
= "usb", .gpio
= 1 << 2, .polarity
= NORMAL
},
244 .name
= "ASUS WL-300g",
246 { .name
= "reset", .gpio
= 1 << 6 },
249 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
253 .name
= "ASUS WL-500g",
255 { .name
= "reset", .gpio
= 1 << 6 },
258 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
262 .name
= "ASUS WL-500g Deluxe",
264 { .name
= "reset", .gpio
= 1 << 6 },
267 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
271 .name
= "ASUS WL-500g Premium",
273 { .name
= "reset", .gpio
= 1 << 0 },
274 { .name
= "ses", .gpio
= 1 << 4 },
277 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
281 .name
= "ASUS WL-500W",
283 { .name
= "reset", .gpio
= 1 << 6 },
284 { .name
= "ses", .gpio
= 1 << 7 },
287 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
291 .name
= "ASUS (unknown, BCM4702)",
293 { .name
= "reset", .gpio
= 1 << 6 },
296 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
300 .name
= "ASUS WL-700gE",
302 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
303 { .name
= "ses", .gpio
= 1 << 4 }, // on back, actual name ezsetup
304 { .name
= "power", .gpio
= 1 << 0 }, // on front
305 { .name
= "copy", .gpio
= 1 << 6 }, // on front
309 // GPIO that controls power led also enables/disables some essential functions
312 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
}, // actual name power
314 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
316 .platform_init
= bcm4780_init
,
320 .name
= "Buffalo WHR-G54S",
322 { .name
= "reset", .gpio
= 1 << 4 },
323 { .name
= "bridge", .gpio
= 1 << 5 },
324 { .name
= "ses", .gpio
= 1 << 0 },
327 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
328 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
329 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
330 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
331 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
335 .name
= "Buffalo WBR2-G54",
338 { .name
= "reset", .gpio
= 1 << 7 },
341 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
345 .name
= "Buffalo WHR-HP-G54",
347 { .name
= "reset", .gpio
= 1 << 4 },
348 { .name
= "bridge", .gpio
= 1 << 5 },
349 { .name
= "ses", .gpio
= 1 << 0 },
352 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
353 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
354 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
355 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
356 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
360 .name
= "Buffalo WHR-G125",
362 { .name
= "reset", .gpio
= 1 << 4 },
363 { .name
= "bridge", .gpio
= 1 << 5 },
364 { .name
= "ses", .gpio
= 1 << 0 },
367 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
368 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
369 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
370 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
371 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
375 .name
= "Buffalo WHR2-A54G54",
377 { .name
= "reset", .gpio
= 1 << 4 },
380 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
384 .name
= "Buffalo WLA2-G54L",
387 { .name
= "reset", .gpio
= 1 << 7 },
390 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
394 .name
= "Buffalo WZR-G300N",
396 { .name
= "reset", .gpio
= 1 << 4 },
399 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
400 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
401 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
405 .name
= "Buffalo WZR-RS-G54",
407 { .name
= "ses", .gpio
= 1 << 0 },
408 { .name
= "reset", .gpio
= 1 << 4 },
411 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
412 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
413 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
417 .name
= "Buffalo WZR-RS-G54HP",
419 { .name
= "ses", .gpio
= 1 << 0 },
420 { .name
= "reset", .gpio
= 1 << 4 },
423 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
424 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
425 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
428 [BUFFALO_UNKNOWN
] = {
429 .name
= "Buffalo (unknown)",
431 { .name
= "reset", .gpio
= 1 << 7 },
434 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
437 [BUFFALO_UNKNOWN_4710
] = {
438 .name
= "Buffalo (unknown, BCM4710)",
440 { .name
= "reset", .gpio
= 1 << 4 },
443 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
448 .name
= "Siemens SE505 V1",
450 /* No usable buttons */
453 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
},
454 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
458 .name
= "Siemens SE505 V2",
460 /* No usable buttons */
463 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
464 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
465 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
470 .name
= "U.S. Robotics USR5461",
472 /* No usable buttons */
475 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
476 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
481 .name
= "Dell TrueMobile 2300",
483 { .name
= "reset", .gpio
= 1 << 0 },
486 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
487 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
492 .name
= "Motorola WE800G",
494 { .name
= "reset", .gpio
= 1 << 0 },
497 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
498 { .name
= "diag", .gpio
= 1 << 2, .polarity
= REVERSE
},
499 { .name
= "wlan_amber", .gpio
= 1 << 1, .polarity
= NORMAL
},
503 .name
= "Motorola WR850G V1",
505 { .name
= "reset", .gpio
= 1 << 0 },
508 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
509 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
510 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= NORMAL
},
511 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
512 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
516 .name
= "Motorola WR850G V2/V3",
518 { .name
= "reset", .gpio
= 1 << 5 },
521 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
522 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
523 { .name
= "wan", .gpio
= 1 << 6, .polarity
= INPUT
},
524 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
528 .name
= "Motorola WR850GP",
530 { .name
= "reset", .gpio
= 1 << 5 },
533 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
534 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
535 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
536 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
542 .name
= "Belkin (unknown)",
543 /* FIXME: verify & add detection */
545 { .name
= "reset", .gpio
= 1 << 7 },
548 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
549 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
550 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
555 .name
= "Netgear WGT634U",
557 { .name
= "reset", .gpio
= 1 << 2 },
560 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
565 .name
= "Trendware TEW411BRP+",
567 { /* No usable buttons */ },
570 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
571 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
572 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
577 .name
= "SimpleTech SimpleShare NAS",
579 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
580 { .name
= "power", .gpio
= 1 << 0 }, // on back
583 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
585 .platform_init
= bcm4780_init
,
589 .name
= "D-Link DIR-130",
591 { .name
= "reset", .gpio
= 1 << 3},
592 { .name
= "reserved", .gpio
= 1 << 7},
595 { .name
= "diag", .gpio
= 1 << 0},
596 { .name
= "blue", .gpio
= 1 << 6},
600 .name
= "D-Link DIR-330",
602 { .name
= "reset", .gpio
= 1 << 3},
603 { .name
= "reserved", .gpio
= 1 << 7},
606 { .name
= "diag", .gpio
= 1 << 0},
607 { .name
= "usb", .gpio
= 1 << 4},
608 { .name
= "blue", .gpio
= 1 << 6},
612 .name
= "D-Link DWL-3150",
614 { .name
= "reset", .gpio
= 1 << 7},
617 { .name
= "diag", .gpio
= 1 << 2},
618 { .name
= "status", .gpio
= 1 << 1},
623 static struct platform_t __init
*platform_detect(void)
625 char *boardnum
, *boardtype
, *buf
;
627 if (strcmp(getvar("nvram_type"), "cfe") == 0)
628 return &platforms
[WGT634U
];
630 /* Look for a model identifier */
632 /* Based on "model_name" */
633 if (buf
= nvram_get("model_name")) {
634 if (!strcmp(buf
, "DIR-130"))
635 return &platforms
[DIR130
];
636 if (!strcmp(buf
, "DIR-330"))
637 return &platforms
[DIR330
];
640 /* Based on "model_no" */
641 if (buf
= nvram_get("model_no")) {
642 if (!strncmp(buf
,"WL700", 5)) /* WL700* */
643 return &platforms
[WL700GE
];
646 /* Based on "ModelId" */
647 if (buf
= nvram_get("ModelId")) {
648 if (!strcmp(buf
, "WR850GP"))
649 return &platforms
[WR850GP
];
650 if (!strcmp(buf
,"WX-5565"))
651 return &platforms
[TM2300
];
652 if (!strncmp(buf
,"WE800G", 6)) /* WE800G* */
653 return &platforms
[WE800G
];
657 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
658 /* Buffalo hardware, check id for specific hardware matches */
659 if (!strcmp(buf
, "29bb0332"))
660 return &platforms
[WBR2_G54
];
661 if (!strcmp(buf
, "29129"))
662 return &platforms
[WLA2_G54L
];
663 if (!strcmp(buf
, "30189"))
664 return &platforms
[WHR_HP_G54
];
665 if (!strcmp(buf
, "32093"))
666 return &platforms
[WHR_G125
];
667 if (!strcmp(buf
, "30182"))
668 return &platforms
[WHR_G54S
];
669 if (!strcmp(buf
, "290441dd"))
670 return &platforms
[WHR2_A54G54
];
671 if (!strcmp(buf
, "31120"))
672 return &platforms
[WZR_G300N
];
673 if (!strcmp(buf
, "30083"))
674 return &platforms
[WZR_RS_G54
];
675 if (!strcmp(buf
, "30103"))
676 return &platforms
[WZR_RS_G54HP
];
679 /* no easy model number, attempt to guess */
680 boardnum
= getvar("boardnum");
681 boardtype
= getvar("boardtype");
683 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
684 /* CFE based - newer hardware */
685 if (!strcmp(boardnum
, "42")) { /* Linksys */
686 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
687 return &platforms
[WRT350N
];
689 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
690 return &platforms
[WRT54G3G
];
692 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
693 return &platforms
[WRTSL54GS
];
695 /* default to WRT54G */
696 return &platforms
[WRT54G
];
699 if (!strcmp(boardnum
, "45")) { /* ASUS */
700 if (!strcmp(boardtype
,"0x042f"))
701 return &platforms
[WL500GP
];
702 else if (!strcmp(boardtype
,"0x0472"))
703 return &platforms
[WL500W
];
705 return &platforms
[WL500GD
];
708 if (!strcmp(boardnum
, "10496"))
709 return &platforms
[USR5461
];
711 } else { /* PMON based - old stuff */
712 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
713 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
714 return &platforms
[WR850GV1
];
716 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
717 if (!strcmp(boardnum
, "42"))
718 return &platforms
[WRT54GV1
];
719 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
720 return &platforms
[WAP54GV1
];
722 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
723 return &platforms
[WL500G
];
724 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
725 /* Either WL-300g or WL-HDD, do more extensive checks */
726 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
727 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 1))
728 return &platforms
[WLHDD
];
729 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
730 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 10))
731 return &platforms
[WL300G
];
734 /* unknown asus stuff, probably bcm4702 */
735 if (!strncmp(boardnum
, "asusX", 5))
736 return &platforms
[ASUS_4702
];
739 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
740 if (!strncmp(boardtype
, "bcm94710ap", 10))
741 return &platforms
[BUFFALO_UNKNOWN_4710
];
743 return &platforms
[BUFFALO_UNKNOWN
];
746 if (!strncmp(getvar("CFEver"), "MotoWRv2", 8) ||
747 !strncmp(getvar("CFEver"), "MotoWRv3", 8) ||
748 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
750 return &platforms
[WR850GV2V3
];
753 if (!strcmp(boardnum
, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
754 return &platforms
[TEW411BRPP
];
757 if (!strncmp(boardnum
, "04FN52", 6)) /* SimpleTech SimpleShare */
758 return &platforms
[STI_NAS
];
760 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
761 return &platforms
[DWL3150
];
767 static void register_buttons(struct button_t
*b
)
770 platform
.button_mask
|= b
->gpio
;
772 platform
.button_mask
&= ~gpiomask
;
774 gpio_outen(platform
.button_mask
, 0);
775 gpio_control(platform
.button_mask
, 0);
776 platform
.button_polarity
= gpio_in() & platform
.button_mask
;
777 gpio_intpolarity(platform
.button_mask
, platform
.button_polarity
);
778 gpio_intmask(platform
.button_mask
, platform
.button_mask
);
780 gpio_set_irqenable(1, button_handler
);
783 static void unregister_buttons(struct button_t
*b
)
785 gpio_intmask(platform
.button_mask
, 0);
787 gpio_set_irqenable(0, button_handler
);
792 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
799 s
= skb_put(event
->skb
, strlen(msg
) + 1);
803 static void hotplug_button(struct work_struct
*work
)
805 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
811 event
->skb
= alloc_skb(2048, GFP_KERNEL
);
813 s
= skb_put(event
->skb
, strlen(event
->action
) + 2);
814 sprintf(s
, "%s@", event
->action
);
817 NETLINK_CB(event
->skb
).dst_group
= 1;
818 netlink_broadcast(uevent_sock
, event
->skb
, 0, 1, GFP_KERNEL
);
823 #else /* !LINUX_2_4 */
824 static inline char *kzalloc(unsigned int size
, unsigned int gfp
)
828 p
= kmalloc(size
, gfp
);
837 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
840 event
->argv
[event
->anr
++] = event
->scratch
;
842 event
->envp
[event
->enr
++] = event
->scratch
;
844 event
->scratch
+= sprintf(event
->scratch
, "%s", msg
) + 1;
847 static void hotplug_button(struct event_t
*event
)
849 char *scratch
= kzalloc(256, GFP_KERNEL
);
850 event
->scratch
= scratch
;
852 add_msg(event
, hotplug_path
, 1);
853 add_msg(event
, "button", 1);
855 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
859 #endif /* !LINUX_2_4 */
861 static int fill_event (struct event_t
*event
)
863 static char buf
[128];
865 add_msg(event
, "HOME=/", 0);
866 add_msg(event
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
867 add_msg(event
, "SUBSYSTEM=button", 0);
868 snprintf(buf
, 128, "ACTION=%s", event
->action
);
869 add_msg(event
, buf
, 0);
870 snprintf(buf
, 128, "BUTTON=%s", event
->name
);
871 add_msg(event
, buf
, 0);
872 snprintf(buf
, 128, "SEEN=%ld", event
->seen
);
873 add_msg(event
, buf
, 0);
875 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
876 add_msg(event
, buf
, 0);
884 static irqreturn_t
button_handler(int irq
, void *dev_id
)
886 static irqreturn_t
button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
892 in
= gpio_in() & platform
.button_mask
;
893 gpio_intpolarity(platform
.button_mask
, in
);
894 changed
= platform
.button_polarity
^ in
;
895 platform
.button_polarity
= in
;
897 changed
&= ~gpio_outen(0, 0);
899 for (b
= platform
.buttons
; b
->name
; b
++) {
900 struct event_t
*event
;
902 if (!(b
->gpio
& changed
)) continue;
906 if ((event
= (struct event_t
*)kzalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
907 event
->seen
= (jiffies
- b
->seen
)/HZ
;
908 event
->name
= b
->name
;
909 event
->action
= b
->pressed
? "pressed" : "released";
910 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
911 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
913 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
, (void *)event
);
915 schedule_work(&event
->wq
);
923 static void register_leds(struct led_t
*l
)
925 struct proc_dir_entry
*p
;
930 leds
= proc_mkdir("led", diag
);
934 for(; l
->name
; l
++) {
935 if (l
->gpio
& gpiomask
)
938 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
942 if (l
->polarity
!= INPUT
) oe_mask
!= l
->gpio
;
944 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
947 if (l
->polarity
== INPUT
) continue;
949 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
950 l
->proc
.type
= PROC_LED
;
952 p
->data
= (void *) &l
->proc
;
953 p
->proc_fops
= &diag_proc_fops
;
957 gpio_outen(mask
, oe_mask
);
958 gpio_control(mask
, 0);
962 static void unregister_leds(struct led_t
*l
)
965 remove_proc_entry(l
->name
, leds
);
967 remove_proc_entry("led", diag
);
970 static void set_led_extif(struct led_t
*led
)
972 gpio_set_extif(led
->gpio
, led
->state
);
975 static void led_flash(unsigned long dummy
) {
980 for (l
= platform
.leds
; l
->name
; l
++) {
982 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
984 l
->state
= !l
->state
;
994 u32 val
= ~gpio_in();
996 gpio_outen(mask
, mask
);
997 gpio_control(mask
, 0);
1000 if (mask
|| extif_blink
) {
1001 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
1005 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
1008 struct inode
*inode
= file
->f_dentry
->d_inode
;
1009 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1011 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1016 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
1019 if (dent
->data
!= NULL
) {
1020 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1021 switch (handler
->type
) {
1023 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
1025 len
= sprintf(page
, "f\n");
1027 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1028 len
= sprintf(page
, "%d\n", led
->state
);
1030 u32 in
= (gpio_in() & led
->gpio
? 1 : 0);
1031 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
1032 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
1038 len
= sprintf(page
, "%s\n", platform
.name
);
1041 len
= sprintf(page
, "0x%04x\n", gpiomask
);
1048 len
= min_t(int, len
- *ppos
, count
);
1049 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
1063 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
1066 struct inode
*inode
= file
->f_dentry
->d_inode
;
1067 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1069 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1074 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
1077 if (copy_from_user(page
, buf
, count
)) {
1083 if (dent
->data
!= NULL
) {
1084 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1085 switch (handler
->type
) {
1087 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
1088 int p
= (led
->polarity
== NORMAL
? 0 : 1);
1090 if (page
[0] == 'f') {
1095 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1096 led
->state
= p
^ ((page
[0] == '1') ? 1 : 0);
1099 gpio_outen(led
->gpio
, led
->gpio
);
1100 gpio_control(led
->gpio
, 0);
1101 gpio_out(led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
1107 gpiomask
= simple_strtoul(page
, NULL
, 0);
1109 if (platform
.buttons
) {
1110 unregister_buttons(platform
.buttons
);
1111 register_buttons(platform
.buttons
);
1114 if (platform
.leds
) {
1115 unregister_leds(platform
.leds
);
1116 register_leds(platform
.leds
);
1127 static int __init
diag_init(void)
1129 static struct proc_dir_entry
*p
;
1130 static struct platform_t
*detected
;
1132 detected
= platform_detect();
1134 printk(MODULE_NAME
": Router model not detected.\n");
1137 memcpy(&platform
, detected
, sizeof(struct platform_t
));
1139 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
1140 if (platform
.platform_init
!= NULL
) {
1141 platform
.platform_init();
1144 if (!(diag
= proc_mkdir("diag", NULL
))) {
1145 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1149 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
1150 p
->data
= (void *) &proc_model
;
1151 p
->proc_fops
= &diag_proc_fops
;
1154 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
1155 p
->data
= (void *) &proc_gpiomask
;
1156 p
->proc_fops
= &diag_proc_fops
;
1159 if (platform
.buttons
)
1160 register_buttons(platform
.buttons
);
1163 register_leds(platform
.leds
);
1168 static void __exit
diag_exit(void)
1170 del_timer(&led_timer
);
1172 if (platform
.buttons
)
1173 unregister_buttons(platform
.buttons
);
1176 unregister_leds(platform
.leds
);
1178 remove_proc_entry("model", diag
);
1179 remove_proc_entry("gpiomask", diag
);
1180 remove_proc_entry("diag", NULL
);
1183 module_init(diag_init
);
1184 module_exit(diag_exit
);
1186 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1187 MODULE_LICENSE("GPL");