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", .gpio
= 1 << 3, .polarity
= REVERSE
},
222 .name
= "ASUS WL-HDD",
224 { .name
= "reset", .gpio
= 1 << 6 },
227 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
228 { .name
= "usb", .gpio
= 1 << 2, .polarity
= NORMAL
},
232 .name
= "ASUS WL-300g",
234 { .name
= "reset", .gpio
= 1 << 6 },
237 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
241 .name
= "ASUS WL-500g",
243 { .name
= "reset", .gpio
= 1 << 6 },
246 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
250 .name
= "ASUS WL-500g Deluxe",
252 { .name
= "reset", .gpio
= 1 << 6 },
255 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
259 .name
= "ASUS WL-500g Premium",
261 { .name
= "reset", .gpio
= 1 << 0 },
262 { .name
= "ses", .gpio
= 1 << 4 },
265 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
269 .name
= "ASUS WL-500W",
271 { .name
= "reset", .gpio
= 1 << 6 },
272 { .name
= "ses", .gpio
= 1 << 7 },
275 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
279 .name
= "ASUS (unknown, BCM4702)",
281 { .name
= "reset", .gpio
= 1 << 6 },
284 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
288 .name
= "ASUS WL-700gE",
290 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
291 { .name
= "ses", .gpio
= 1 << 4 }, // on back, actual name ezsetup
292 { .name
= "power", .gpio
= 1 << 0 }, // on front
293 { .name
= "copy", .gpio
= 1 << 6 }, // on front
297 // GPIO that controls power led also enables/disables some essential functions
300 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
}, // actual name power
302 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
304 .platform_init
= bcm4780_init
,
308 .name
= "Buffalo WHR-G54S",
310 { .name
= "reset", .gpio
= 1 << 4 },
311 { .name
= "bridge", .gpio
= 1 << 5 },
312 { .name
= "ses", .gpio
= 1 << 0 },
315 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
316 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
317 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
318 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
319 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
323 .name
= "Buffalo WBR2-G54",
326 { .name
= "reset", .gpio
= 1 << 7 },
329 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
333 .name
= "Buffalo WHR-HP-G54",
335 { .name
= "reset", .gpio
= 1 << 4 },
336 { .name
= "bridge", .gpio
= 1 << 5 },
337 { .name
= "ses", .gpio
= 1 << 0 },
340 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
341 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
342 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
343 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
344 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
348 .name
= "Buffalo WHR-G125",
350 { .name
= "reset", .gpio
= 1 << 4 },
351 { .name
= "bridge", .gpio
= 1 << 5 },
352 { .name
= "ses", .gpio
= 1 << 0 },
355 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
356 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
357 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
358 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
359 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
363 .name
= "Buffalo WHR2-A54G54",
365 { .name
= "reset", .gpio
= 1 << 4 },
368 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
372 .name
= "Buffalo WLA2-G54L",
375 { .name
= "reset", .gpio
= 1 << 7 },
378 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
382 .name
= "Buffalo WZR-G300N",
384 { .name
= "reset", .gpio
= 1 << 4 },
387 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
388 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
389 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
393 .name
= "Buffalo WZR-RS-G54",
395 { .name
= "ses", .gpio
= 1 << 0 },
396 { .name
= "reset", .gpio
= 1 << 4 },
399 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
400 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
401 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
405 .name
= "Buffalo WZR-RS-G54HP",
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
},
416 [BUFFALO_UNKNOWN
] = {
417 .name
= "Buffalo (unknown)",
419 { .name
= "reset", .gpio
= 1 << 7 },
422 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
425 [BUFFALO_UNKNOWN_4710
] = {
426 .name
= "Buffalo (unknown, BCM4710)",
428 { .name
= "reset", .gpio
= 1 << 4 },
431 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
436 .name
= "Siemens SE505 V1",
438 /* No usable buttons */
441 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
},
442 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
446 .name
= "Siemens SE505 V2",
448 /* No usable buttons */
451 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
452 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
453 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
458 .name
= "U.S. Robotics USR5461",
460 /* No usable buttons */
463 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
464 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
469 .name
= "Dell TrueMobile 2300",
471 { .name
= "reset", .gpio
= 1 << 0 },
474 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
475 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
480 .name
= "Motorola WE800G",
482 { .name
= "reset", .gpio
= 1 << 0 },
485 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
486 { .name
= "diag", .gpio
= 1 << 2, .polarity
= REVERSE
},
487 { .name
= "wlan_amber", .gpio
= 1 << 1, .polarity
= NORMAL
},
491 .name
= "Motorola WR850G V1",
493 { .name
= "reset", .gpio
= 1 << 0 },
496 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
497 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
498 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= NORMAL
},
499 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
500 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
504 .name
= "Motorola WR850G V2/V3",
506 { .name
= "reset", .gpio
= 1 << 5 },
509 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
510 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
511 { .name
= "wan", .gpio
= 1 << 6, .polarity
= INPUT
},
512 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
516 .name
= "Motorola WR850GP",
518 { .name
= "reset", .gpio
= 1 << 5 },
521 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
522 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
523 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
524 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
530 .name
= "Belkin (unknown)",
531 /* FIXME: verify & add detection */
533 { .name
= "reset", .gpio
= 1 << 7 },
536 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
537 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
538 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
543 .name
= "Netgear WGT634U",
545 { .name
= "reset", .gpio
= 1 << 2 },
548 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
553 .name
= "Trendware TEW411BRP+",
555 { /* No usable buttons */ },
558 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
559 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
560 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
565 .name
= "SimpleTech SimpleShare NAS",
567 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
568 { .name
= "power", .gpio
= 1 << 0 }, // on back
571 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
573 .platform_init
= bcm4780_init
,
577 .name
= "D-Link DIR-130",
579 { .name
= "reset", .gpio
= 1 << 3},
580 { .name
= "reserved", .gpio
= 1 << 7},
583 { .name
= "diag", .gpio
= 1 << 0},
584 { .name
= "blue", .gpio
= 1 << 6},
588 .name
= "D-Link DIR-330",
590 { .name
= "reset", .gpio
= 1 << 3},
591 { .name
= "reserved", .gpio
= 1 << 7},
594 { .name
= "diag", .gpio
= 1 << 0},
595 { .name
= "usb", .gpio
= 1 << 4},
596 { .name
= "blue", .gpio
= 1 << 6},
600 .name
= "D-Link DWL-3150",
602 { .name
= "reset", .gpio
= 1 << 7},
605 { .name
= "diag", .gpio
= 1 << 2},
606 { .name
= "status", .gpio
= 1 << 1},
611 static struct platform_t __init
*platform_detect(void)
613 char *boardnum
, *boardtype
, *buf
;
615 if (strcmp(getvar("nvram_type"), "cfe") == 0)
616 return &platforms
[WGT634U
];
618 /* Look for a model identifier */
620 /* Based on "model_name" */
621 if (buf
= nvram_get("model_name")) {
622 if (!strcmp(buf
, "DIR-130"))
623 return &platforms
[DIR130
];
624 if (!strcmp(buf
, "DIR-330"))
625 return &platforms
[DIR330
];
628 /* Based on "model_no" */
629 if (buf
= nvram_get("model_no")) {
630 if (!strncmp(buf
,"WL700", 5)) /* WL700* */
631 return &platforms
[WL700GE
];
634 /* Based on "ModelId" */
635 if (buf
= nvram_get("ModelId")) {
636 if (!strcmp(buf
, "WR850GP"))
637 return &platforms
[WR850GP
];
638 if (!strcmp(buf
,"WX-5565"))
639 return &platforms
[TM2300
];
640 if (!strncmp(buf
,"WE800G", 6)) /* WE800G* */
641 return &platforms
[WE800G
];
645 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
646 /* Buffalo hardware, check id for specific hardware matches */
647 if (!strcmp(buf
, "29bb0332"))
648 return &platforms
[WBR2_G54
];
649 if (!strcmp(buf
, "29129"))
650 return &platforms
[WLA2_G54L
];
651 if (!strcmp(buf
, "30189"))
652 return &platforms
[WHR_HP_G54
];
653 if (!strcmp(buf
, "32093"))
654 return &platforms
[WHR_G125
];
655 if (!strcmp(buf
, "30182"))
656 return &platforms
[WHR_G54S
];
657 if (!strcmp(buf
, "290441dd"))
658 return &platforms
[WHR2_A54G54
];
659 if (!strcmp(buf
, "31120"))
660 return &platforms
[WZR_G300N
];
661 if (!strcmp(buf
, "30083"))
662 return &platforms
[WZR_RS_G54
];
663 if (!strcmp(buf
, "30103"))
664 return &platforms
[WZR_RS_G54HP
];
667 /* no easy model number, attempt to guess */
668 boardnum
= getvar("boardnum");
669 boardtype
= getvar("boardtype");
671 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
672 /* CFE based - newer hardware */
673 if (!strcmp(boardnum
, "42")) { /* Linksys */
674 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
675 return &platforms
[WRT350N
];
677 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
678 return &platforms
[WRT54G3G
];
680 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
681 return &platforms
[WRTSL54GS
];
683 /* default to WRT54G */
684 return &platforms
[WRT54G
];
687 if (!strcmp(boardnum
, "45")) { /* ASUS */
688 if (!strcmp(boardtype
,"0x042f"))
689 return &platforms
[WL500GP
];
690 else if (!strcmp(boardtype
,"0x0472"))
691 return &platforms
[WL500W
];
693 return &platforms
[WL500GD
];
696 if (!strcmp(boardnum
, "10496"))
697 return &platforms
[USR5461
];
699 } else { /* PMON based - old stuff */
700 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
701 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
702 return &platforms
[WR850GV1
];
704 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
705 if (!strcmp(boardnum
, "42"))
706 return &platforms
[WRT54GV1
];
707 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
708 return &platforms
[WAP54GV1
];
710 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
711 return &platforms
[WL500G
];
712 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
713 /* Either WL-300g or WL-HDD, do more extensive checks */
714 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
715 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 1))
716 return &platforms
[WLHDD
];
717 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
718 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 10))
719 return &platforms
[WL300G
];
722 /* unknown asus stuff, probably bcm4702 */
723 if (!strncmp(boardnum
, "asusX", 5))
724 return &platforms
[ASUS_4702
];
727 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
728 if (!strncmp(boardtype
, "bcm94710ap", 10))
729 return &platforms
[BUFFALO_UNKNOWN_4710
];
731 return &platforms
[BUFFALO_UNKNOWN
];
734 if (!strncmp(getvar("CFEver"), "MotoWRv2", 8) ||
735 !strncmp(getvar("CFEver"), "MotoWRv3", 8) ||
736 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
738 return &platforms
[WR850GV2V3
];
741 if (!strcmp(boardnum
, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
742 return &platforms
[TEW411BRPP
];
745 if (!strncmp(boardnum
, "04FN52", 6)) /* SimpleTech SimpleShare */
746 return &platforms
[STI_NAS
];
748 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
749 return &platforms
[DWL3150
];
755 static void register_buttons(struct button_t
*b
)
758 platform
.button_mask
|= b
->gpio
;
760 platform
.button_mask
&= ~gpiomask
;
762 gpio_outen(platform
.button_mask
, 0);
763 gpio_control(platform
.button_mask
, 0);
764 platform
.button_polarity
= gpio_in() & platform
.button_mask
;
765 gpio_intpolarity(platform
.button_mask
, platform
.button_polarity
);
766 gpio_intmask(platform
.button_mask
, platform
.button_mask
);
768 gpio_set_irqenable(1, button_handler
);
771 static void unregister_buttons(struct button_t
*b
)
773 gpio_intmask(platform
.button_mask
, 0);
775 gpio_set_irqenable(0, button_handler
);
780 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
787 s
= skb_put(event
->skb
, strlen(msg
) + 1);
791 static void hotplug_button(struct work_struct
*work
)
793 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
799 event
->skb
= alloc_skb(2048, GFP_KERNEL
);
801 s
= skb_put(event
->skb
, strlen(event
->action
) + 2);
802 sprintf(s
, "%s@", event
->action
);
805 NETLINK_CB(event
->skb
).dst_group
= 1;
806 netlink_broadcast(uevent_sock
, event
->skb
, 0, 1, GFP_KERNEL
);
811 #else /* !LINUX_2_4 */
812 static inline char *kzalloc(unsigned int size
, unsigned int gfp
)
816 p
= kmalloc(size
, gfp
);
825 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
828 event
->argv
[event
->anr
++] = event
->scratch
;
830 event
->envp
[event
->enr
++] = event
->scratch
;
832 event
->scratch
+= sprintf(event
->scratch
, "%s", msg
) + 1;
835 static void hotplug_button(struct event_t
*event
)
837 char *scratch
= kzalloc(256, GFP_KERNEL
);
838 event
->scratch
= scratch
;
840 add_msg(event
, hotplug_path
, 1);
841 add_msg(event
, "button", 1);
843 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
847 #endif /* !LINUX_2_4 */
849 static int fill_event (struct event_t
*event
)
851 static char buf
[128];
853 add_msg(event
, "HOME=/", 0);
854 add_msg(event
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
855 add_msg(event
, "SUBSYSTEM=button", 0);
856 snprintf(buf
, 128, "ACTION=%s", event
->action
);
857 add_msg(event
, buf
, 0);
858 snprintf(buf
, 128, "BUTTON=%s", event
->name
);
859 add_msg(event
, buf
, 0);
860 snprintf(buf
, 128, "SEEN=%ld", event
->seen
);
861 add_msg(event
, buf
, 0);
863 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
864 add_msg(event
, buf
, 0);
872 static irqreturn_t
button_handler(int irq
, void *dev_id
)
874 static irqreturn_t
button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
880 in
= gpio_in() & platform
.button_mask
;
881 gpio_intpolarity(platform
.button_mask
, in
);
882 changed
= platform
.button_polarity
^ in
;
883 platform
.button_polarity
= in
;
885 changed
&= ~gpio_outen(0, 0);
887 for (b
= platform
.buttons
; b
->name
; b
++) {
888 struct event_t
*event
;
890 if (!(b
->gpio
& changed
)) continue;
894 if ((event
= (struct event_t
*)kzalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
895 event
->seen
= (jiffies
- b
->seen
)/HZ
;
896 event
->name
= b
->name
;
897 event
->action
= b
->pressed
? "pressed" : "released";
898 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
899 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
901 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
, (void *)event
);
903 schedule_work(&event
->wq
);
911 static void register_leds(struct led_t
*l
)
913 struct proc_dir_entry
*p
;
918 leds
= proc_mkdir("led", diag
);
922 for(; l
->name
; l
++) {
923 if (l
->gpio
& gpiomask
)
926 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
930 if (l
->polarity
!= INPUT
) oe_mask
!= l
->gpio
;
932 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
935 if (l
->polarity
== INPUT
) continue;
937 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
938 l
->proc
.type
= PROC_LED
;
940 p
->data
= (void *) &l
->proc
;
941 p
->proc_fops
= &diag_proc_fops
;
945 gpio_outen(mask
, oe_mask
);
946 gpio_control(mask
, 0);
950 static void unregister_leds(struct led_t
*l
)
953 remove_proc_entry(l
->name
, leds
);
955 remove_proc_entry("led", diag
);
958 static void set_led_extif(struct led_t
*led
)
960 gpio_set_extif(led
->gpio
, led
->state
);
963 static void led_flash(unsigned long dummy
) {
968 for (l
= platform
.leds
; l
->name
; l
++) {
970 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
972 l
->state
= !l
->state
;
982 u32 val
= ~gpio_in();
984 gpio_outen(mask
, mask
);
985 gpio_control(mask
, 0);
988 if (mask
|| extif_blink
) {
989 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
993 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
996 struct inode
*inode
= file
->f_dentry
->d_inode
;
997 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
999 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1004 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
1007 if (dent
->data
!= NULL
) {
1008 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1009 switch (handler
->type
) {
1011 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
1013 len
= sprintf(page
, "f\n");
1015 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1016 len
= sprintf(page
, "%d\n", led
->state
);
1018 u32 in
= (gpio_in() & led
->gpio
? 1 : 0);
1019 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
1020 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
1026 len
= sprintf(page
, "%s\n", platform
.name
);
1029 len
= sprintf(page
, "0x%04x\n", gpiomask
);
1036 len
= min_t(int, len
- *ppos
, count
);
1037 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
1051 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
1054 struct inode
*inode
= file
->f_dentry
->d_inode
;
1055 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1057 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1062 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
1065 if (copy_from_user(page
, buf
, count
)) {
1071 if (dent
->data
!= NULL
) {
1072 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1073 switch (handler
->type
) {
1075 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
1076 int p
= (led
->polarity
== NORMAL
? 0 : 1);
1078 if (page
[0] == 'f') {
1083 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1084 led
->state
= p
^ ((page
[0] == '1') ? 1 : 0);
1087 gpio_outen(led
->gpio
, led
->gpio
);
1088 gpio_control(led
->gpio
, 0);
1089 gpio_out(led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
1095 gpiomask
= simple_strtoul(page
, NULL
, 0);
1097 if (platform
.buttons
) {
1098 unregister_buttons(platform
.buttons
);
1099 register_buttons(platform
.buttons
);
1102 if (platform
.leds
) {
1103 unregister_leds(platform
.leds
);
1104 register_leds(platform
.leds
);
1115 static int __init
diag_init(void)
1117 static struct proc_dir_entry
*p
;
1118 static struct platform_t
*detected
;
1120 detected
= platform_detect();
1122 printk(MODULE_NAME
": Router model not detected.\n");
1125 memcpy(&platform
, detected
, sizeof(struct platform_t
));
1127 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
1128 if (platform
.platform_init
!= NULL
) {
1129 platform
.platform_init();
1132 if (!(diag
= proc_mkdir("diag", NULL
))) {
1133 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1137 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
1138 p
->data
= (void *) &proc_model
;
1139 p
->proc_fops
= &diag_proc_fops
;
1142 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
1143 p
->data
= (void *) &proc_gpiomask
;
1144 p
->proc_fops
= &diag_proc_fops
;
1147 if (platform
.buttons
)
1148 register_buttons(platform
.buttons
);
1151 register_leds(platform
.leds
);
1156 static void __exit
diag_exit(void)
1158 del_timer(&led_timer
);
1160 if (platform
.buttons
)
1161 unregister_buttons(platform
.buttons
);
1164 unregister_leds(platform
.leds
);
1166 remove_proc_entry("model", diag
);
1167 remove_proc_entry("gpiomask", diag
);
1168 remove_proc_entry("diag", NULL
);
1171 module_init(diag_init
);
1172 module_exit(diag_exit
);
1174 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1175 MODULE_LICENSE("GPL");