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.
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>
30 #include <linux/workqueue.h>
31 #include <linux/skbuff.h>
32 #include <linux/netlink.h>
33 #include <linux/kobject.h>
35 extern u64 uevent_next_seqnum(void);
39 #define getvar(str) (nvram_get(str)?:"")
41 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
42 static int fill_event(struct event_t *);
43 static unsigned int gpiomask = 0;
44 module_param(gpiomask, int, 0644);
130 /* Western Digital */
143 static void __init bcm4780_init(void) {
146 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
147 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
148 gpio_outen(pin, pin);
149 gpio_control(pin, 0);
152 /* Wait 5s, so the HDD can spin up */
153 set_current_state(TASK_INTERRUPTIBLE);
154 schedule_timeout(HZ * 5);
157 static void __init NetCenter_init(void) {
158 /* unset pin 6 (+12V) */
160 gpio_outen(pin, pin);
161 gpio_control(pin, 0);
163 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
165 gpio_outen(pin, pin);
166 gpio_control(pin, 0);
168 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
172 static void __init bcm57xx_init(void) {
175 /* FIXME: switch comes up, but port mappings/vlans not right */
176 gpio_outen(pin, pin);
177 gpio_control(pin, 0);
181 static struct platform_t __initdata platforms[] = {
184 .name = "Linksys WAP54G V1",
186 { .name = "reset", .gpio = 1 << 0 },
189 { .name = "diag", .gpio = 1 << 3 },
190 { .name = "wlan", .gpio = 1 << 4 },
194 .name = "Linksys WAP54G V2",
196 { .name = "reset", .gpio = 1 << 0 },
199 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
200 /* GPIO 6 is b44 (eth0, LAN) PHY power */
204 .name = "Linksys WAP54G V3",
206 /* FIXME: verify this */
207 { .name = "reset", .gpio = 1 << 7 },
208 { .name = "ses", .gpio = 1 << 0 },
212 { .name = "ses", .gpio = 1 << 1 },
216 .name = "Linksys WRT54G V1.x",
218 { .name = "reset", .gpio = 1 << 6 },
221 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
222 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
226 .name = "Linksys WRT54G/GS/GL",
228 { .name = "reset", .gpio = 1 << 6 },
229 { .name = "ses", .gpio = 1 << 4 },
232 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
233 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
234 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
235 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
236 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
240 .name = "Linksys WRTSL54GS",
242 { .name = "reset", .gpio = 1 << 6 },
243 { .name = "ses", .gpio = 1 << 4 },
246 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
247 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
248 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
249 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
253 .name = "Linksys WRT54G3G",
255 { .name = "reset", .gpio = 1 << 6 },
256 { .name = "3g", .gpio = 1 << 4 },
259 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
260 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
261 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
262 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
263 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
267 .name = "Linksys WRT54G3GV2-VF",
269 { .name = "reset", .gpio = 1 << 6 },
270 { .name = "3g", .gpio = 1 << 5 },
273 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
274 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
275 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
279 .name = "Linksys WRT160N",
281 { .name = "reset", .gpio = 1 << 6 },
282 { .name = "ses", .gpio = 1 << 4 },
285 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
286 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
287 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
291 .name = "Linksys WRT300N V1.1",
293 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
294 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
297 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
298 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
299 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
301 .platform_init = bcm57xx_init,
304 .name = "Linksys WRT350N",
306 { .name = "reset", .gpio = 1 << 6 },
307 { .name = "ses", .gpio = 1 << 8 },
310 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
311 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
312 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
313 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
314 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
316 .platform_init = bcm57xx_init,
319 .name = "Linksys WRT600N",
321 { .name = "reset", .gpio = 1 << 6 },
322 { .name = "ses", .gpio = 1 << 7 },
325 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
326 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
327 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
328 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
329 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
330 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
332 .platform_init = bcm57xx_init,
335 .name = "Linksys WRT600N V1.1",
337 { .name = "reset", .gpio = 1 << 6 },
338 { .name = "ses", .gpio = 1 << 7 },
341 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
342 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
343 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
344 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
345 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
346 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
348 .platform_init = bcm57xx_init,
351 .name = "Linksys WRT610N",
353 { .name = "reset", .gpio = 1 << 6 },
354 { .name = "ses", .gpio = 1 << 8 },
357 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
358 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
359 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
360 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
365 .name = "ASUS WL-HDD",
367 { .name = "reset", .gpio = 1 << 6 },
370 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
371 { .name = "usb", .gpio = 1 << 2, .polarity = REVERSE },
375 .name = "ASUS WL-300g",
377 { .name = "reset", .gpio = 1 << 6 },
380 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
384 .name = "ASUS WL-320gE/WL-320gP",
386 { .name = "reset", .gpio = 1 << 6 },
389 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
390 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
391 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
395 .name = "ASUS WL-330gE",
397 { .name = "reset", .gpio = 1 << 2 },
400 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
404 .name = "ASUS WL-500g",
406 { .name = "reset", .gpio = 1 << 6 },
409 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
413 .name = "ASUS WL-500g Deluxe",
415 { .name = "reset", .gpio = 1 << 6 },
418 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
422 .name = "ASUS WL-500g Premium",
424 { .name = "reset", .gpio = 1 << 0 },
425 { .name = "ses", .gpio = 1 << 4 },
428 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
432 .name = "ASUS WL-500g Premium V2",
434 { .name = "reset", .gpio = 1 << 2 },
435 { .name = "ses", .gpio = 1 << 3 },
438 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
439 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
443 .name = "ASUS WL-500W",
445 { .name = "reset", .gpio = 1 << 6 },
446 { .name = "ses", .gpio = 1 << 7 },
449 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
453 .name = "ASUS WL-520GC",
455 { .name = "reset", .gpio = 1 << 2 },
456 { .name = "ses", .gpio = 1 << 3 },
459 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
460 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
464 .name = "ASUS WL-520gU",
466 { .name = "reset", .gpio = 1 << 2 },
467 { .name = "ses", .gpio = 1 << 3 },
470 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
471 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
475 .name = "ASUS (unknown, BCM4702)",
477 { .name = "reset", .gpio = 1 << 6 },
480 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
484 .name = "ASUS WL-700gE",
486 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
487 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
488 { .name = "power", .gpio = 1 << 0 }, // on front
489 { .name = "copy", .gpio = 1 << 6 }, // on front
493 // GPIO that controls power led also enables/disables some essential functions
496 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
498 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
500 .platform_init = bcm4780_init,
504 .name = "Buffalo WHR-G54S",
506 { .name = "reset", .gpio = 1 << 4 },
507 { .name = "bridge", .gpio = 1 << 5 },
508 { .name = "ses", .gpio = 1 << 0 },
511 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
512 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
513 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
514 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
515 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
519 .name = "Buffalo WBR2-G54",
522 { .name = "reset", .gpio = 1 << 7 },
525 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
529 .name = "Buffalo WHR-HP-G54",
531 { .name = "reset", .gpio = 1 << 4 },
532 { .name = "bridge", .gpio = 1 << 5 },
533 { .name = "ses", .gpio = 1 << 0 },
536 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
537 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
538 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
539 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
540 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
544 .name = "Buffalo WHR-G125",
546 { .name = "reset", .gpio = 1 << 4 },
547 { .name = "bridge", .gpio = 1 << 5 },
548 { .name = "ses", .gpio = 1 << 0 },
551 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
552 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
553 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
554 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
555 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
559 .name = "Buffalo WHR2-A54G54",
561 { .name = "reset", .gpio = 1 << 4 },
564 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
568 .name = "Buffalo WLA2-G54L",
571 { .name = "reset", .gpio = 1 << 7 },
574 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
578 .name = "Buffalo WZR-G300N",
580 { .name = "reset", .gpio = 1 << 4 },
583 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
584 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
585 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
589 .name = "Buffalo WZR-RS-G54",
591 { .name = "ses", .gpio = 1 << 0 },
592 { .name = "reset", .gpio = 1 << 4 },
595 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
596 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
597 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
601 .name = "Buffalo WZR-RS-G54HP",
603 { .name = "ses", .gpio = 1 << 0 },
604 { .name = "reset", .gpio = 1 << 4 },
607 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
608 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
609 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
612 [BUFFALO_UNKNOWN] = {
613 .name = "Buffalo (unknown)",
615 { .name = "reset", .gpio = 1 << 7 },
618 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
621 [BUFFALO_UNKNOWN_4710] = {
622 .name = "Buffalo (unknown, BCM4710)",
624 { .name = "reset", .gpio = 1 << 4 },
627 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
632 .name = "Siemens SE505 V1",
634 /* No usable buttons */
637 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
638 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
639 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
643 .name = "Siemens SE505 V2",
645 /* No usable buttons */
648 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
649 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
650 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
655 .name = "U.S. Robotics USR5461",
657 /* No usable buttons */
660 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
661 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
666 .name = "Dell TrueMobile 2300",
668 { .name = "reset", .gpio = 1 << 0 },
671 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
672 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
676 .name = "Dell TrueMobile 2300 v2",
678 { .name = "reset", .gpio = 1 << 0 },
681 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
682 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
687 .name = "Motorola WE800G",
689 { .name = "reset", .gpio = 1 << 0 },
692 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
693 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
694 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
698 .name = "Motorola WR850G V1",
700 { .name = "reset", .gpio = 1 << 0 },
703 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
704 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
705 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
706 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
707 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
711 .name = "Motorola WR850G V2/V3",
713 { .name = "reset", .gpio = 1 << 5 },
716 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
717 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
718 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
719 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
723 .name = "Motorola WR850GP",
725 { .name = "reset", .gpio = 1 << 5 },
728 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
729 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
730 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
731 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
737 .name = "Belkin (unknown)",
738 /* FIXME: verify & add detection */
740 { .name = "reset", .gpio = 1 << 7 },
743 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
744 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
745 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
750 .name = "Netgear WGT634U",
752 { .name = "reset", .gpio = 1 << 2 },
755 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
759 .name = "Netgear WNR834B V2",
761 { .name = "reset", .gpio = 1 << 6 },
764 { .name = "power", .gpio = 1 << 2, .polarity = NORMAL },
765 { .name = "diag", .gpio = 1 << 3, .polarity = NORMAL },
766 { .name = "connected", .gpio = 1 << 7, .polarity = NORMAL },
771 .name = "Trendware TEW411BRP+",
773 { /* No usable buttons */ },
776 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
777 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
778 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
783 .name = "SimpleTech SimpleShare NAS",
785 { .name = "reset", .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
788 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
790 .platform_init = bcm4780_init,
794 .name = "D-Link DIR-130",
796 { .name = "reset", .gpio = 1 << 3},
797 { .name = "reserved", .gpio = 1 << 7},
800 { .name = "diag", .gpio = 1 << 0},
801 { .name = "blue", .gpio = 1 << 6},
805 .name = "D-Link DIR-320",
807 { .name = "reserved", .gpio = 1 << 6},
808 { .name = "reset", .gpio = 1 << 7},
811 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
812 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
813 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
814 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
815 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
819 .name = "D-Link DIR-330",
821 { .name = "reset", .gpio = 1 << 3},
822 { .name = "reserved", .gpio = 1 << 7},
825 { .name = "diag", .gpio = 1 << 0},
826 { .name = "usb", .gpio = 1 << 4},
827 { .name = "blue", .gpio = 1 << 6},
831 .name = "D-Link DWL-3150",
833 { .name = "reset", .gpio = 1 << 7},
836 { .name = "diag", .gpio = 1 << 2},
837 { .name = "status", .gpio = 1 << 1},
842 .name = "Sitecom WL-105b",
844 { .name = "reset", .gpio = 1 << 10},
847 { .name = "wlan", .gpio = 1 << 4},
848 { .name = "power", .gpio = 1 << 3},
851 /* Western Digital Net Center */
853 .name = "Western Digital NetCenter",
855 { .name = "power", .gpio = 1 << 0},
856 { .name = "reset", .gpio = 1 << 7},
858 .platform_init = NetCenter_init,
860 /* Askey (and clones) */
862 .name = "Askey RT210W",
864 /* Power button is hard-wired to hardware reset */
865 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
866 { .name = "power", .gpio = 1 << 7},
869 /* These were verified and named based on Belkin F5D4230-4 v1112 */
870 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
871 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
872 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
876 .name = "OvisLink WL-1600GL",
878 { .name = "reset", .gpio = 1 << 3 },
879 { .name = "ses", .gpio = 1 << 4 },
882 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
883 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
884 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
885 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
890 .name = "Microsoft MN-700",
892 { .name = "reset", .gpio = 1 << 7 },
895 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
900 static struct platform_t __init *platform_detect(void)
902 char *boardnum, *boardtype, *buf;
904 if (strcmp(getvar("nvram_type"), "cfe") == 0)
905 return &platforms[WGT634U];
907 /* Look for a model identifier */
909 /* Based on "model_name" */
910 if ((buf = nvram_get("model_name"))) {
911 if (!strcmp(buf, "DIR-130"))
912 return &platforms[DIR130];
913 if (!strcmp(buf, "DIR-330"))
914 return &platforms[DIR330];
917 /* Based on "wsc_modelname */
918 if ((buf = nvram_get("wsc_modelname"))) {
919 if (!strcmp(buf, "WRT610N"))
920 return &platforms[WRT610N];
923 /* Based on "model_no" */
924 if ((buf = nvram_get("model_no"))) {
925 if (startswith(buf,"WL700")) /* WL700* */
926 return &platforms[WL700GE];
929 /* Based on "hardware_version" */
930 if ((buf = nvram_get("hardware_version"))) {
931 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
932 return &platforms[WL500GPV2];
933 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
934 return &platforms[WL520GC];
935 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
936 return &platforms[WL520GU];
937 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
938 return &platforms[WL330GE];
941 /* Based on "ModelId" */
942 if ((buf = nvram_get("ModelId"))) {
943 if (!strcmp(buf, "WR850GP"))
944 return &platforms[WR850GP];
945 if (!strcmp(buf, "WR850G"))
946 return &platforms[WR850GV2V3];
947 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
948 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
949 if (startswith(buf,"WE800G")) /* WE800G* */
950 return &platforms[WE800G];
954 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
955 /* Buffalo hardware, check id for specific hardware matches */
956 if (!strcmp(buf, "29bb0332"))
957 return &platforms[WBR2_G54];
958 if (!strcmp(buf, "29129"))
959 return &platforms[WLA2_G54L];
960 if (!strcmp(buf, "30189"))
961 return &platforms[WHR_HP_G54];
962 if (!strcmp(buf, "32093"))
963 return &platforms[WHR_G125];
964 if (!strcmp(buf, "30182"))
965 return &platforms[WHR_G54S];
966 if (!strcmp(buf, "290441dd"))
967 return &platforms[WHR2_A54G54];
968 if (!strcmp(buf, "31120"))
969 return &platforms[WZR_G300N];
970 if (!strcmp(buf, "30083"))
971 return &platforms[WZR_RS_G54];
972 if (!strcmp(buf, "30103"))
973 return &platforms[WZR_RS_G54HP];
976 /* no easy model number, attempt to guess */
977 boardnum = getvar("boardnum");
978 boardtype = getvar("boardtype");
980 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
981 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
982 return &platforms[WRT600NV11];
984 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
985 return &platforms[WRT600N];
988 if (startswith(getvar("pmon_ver"), "CFE")) {
989 /* CFE based - newer hardware */
990 if (!strcmp(boardnum, "42")) { /* Linksys */
991 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
992 return &platforms[WRT300NV11];
994 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
995 return &platforms[WRT350N];
997 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
998 return &platforms[WRT54G3G];
1000 if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1001 return &platforms[WRT54G3GV2_VF];
1003 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1004 return &platforms[WRTSL54GS];
1006 if (!strcmp(boardtype, "0x0472"))
1007 return &platforms[WRT160N];
1009 /* default to WRT54G */
1010 return &platforms[WRT54G];
1012 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1013 return &platforms[WAP54GV2];
1015 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1016 return &platforms[WL1600GL];
1019 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1020 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1021 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1024 if (!strcmp(boardnum, "45")) { /* ASUS */
1025 if (!strcmp(boardtype,"0x042f"))
1026 return &platforms[WL500GP];
1027 else if (!strcmp(boardtype,"0x0472"))
1028 return &platforms[WL500W];
1029 else if (!strcmp(boardtype,"0x467"))
1030 return &platforms[WL320GE];
1032 return &platforms[WL500GD];
1035 if (!strcmp(boardnum, "10496"))
1036 return &platforms[USR5461];
1038 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1039 return &platforms[SE505V2];
1041 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1042 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1043 return &platforms[DIR320];
1045 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1046 return &platforms[WDNetCenter];
1049 if ((!strcmp(boardnum, "08") || !strcmp(boardnum, "01")) &&
1050 !strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B V1 and V2*/
1051 /* TODO: Check for version. Default platform is V2 for now. */
1052 return &platforms[WNR834BV2];
1055 } else { /* PMON based - old stuff */
1056 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1057 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1058 return &platforms[WR850GV1];
1060 if (startswith(boardtype, "bcm94710dev")) {
1061 if (!strcmp(boardnum, "42"))
1062 return &platforms[WRT54GV1];
1063 if (simple_strtoul(boardnum, NULL, 0) == 2)
1064 return &platforms[WAP54GV1];
1066 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1067 if (startswith(getvar("hardware_version"), "WL500-")) {
1068 if (!strcmp(getvar("boardnum"), "mn700"))
1069 return &platforms[MN700];
1071 return &platforms[WL500G];
1073 if (startswith(getvar("hardware_version"), "WL300-")) {
1074 /* Either WL-300g or WL-HDD, do more extensive checks */
1075 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1076 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1077 return &platforms[WLHDD];
1078 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1079 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1080 return &platforms[WL300G];
1082 /* Sitecom WL-105b */
1083 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1084 return &platforms[WL105B];
1086 /* unknown asus stuff, probably bcm4702 */
1087 if (startswith(boardnum, "asusX"))
1088 return &platforms[ASUS_4702];
1090 /* bcm4702 based Askey RT210W clones, Including:
1091 * Askey RT210W (duh?)
1093 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1095 if (!strcmp(boardtype,"bcm94710r4")
1096 && !strcmp(boardnum ,"100")
1097 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1099 return &platforms[RT210W];
1103 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1104 if (startswith(boardtype, "bcm94710ap"))
1105 return &platforms[BUFFALO_UNKNOWN_4710];
1107 return &platforms[BUFFALO_UNKNOWN];
1110 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1111 startswith(getvar("CFEver"), "MotoWRv3") ||
1112 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1114 return &platforms[WR850GV2V3];
1117 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1118 return &platforms[TEW411BRPP];
1121 if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1122 return &platforms[STI_NAS];
1124 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1125 return &platforms[DWL3150];
1131 static void register_buttons(struct button_t *b)
1133 for (; b->name; b++)
1134 platform.button_mask |= b->gpio;
1136 platform.button_mask &= ~gpiomask;
1138 gpio_outen(platform.button_mask, 0);
1139 gpio_control(platform.button_mask, 0);
1140 platform.button_polarity = gpio_in() & platform.button_mask;
1141 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1142 gpio_setintmask(platform.button_mask, platform.button_mask);
1144 gpio_set_irqenable(1, button_handler);
1147 static void unregister_buttons(struct button_t *b)
1149 gpio_setintmask(platform.button_mask, 0);
1151 gpio_set_irqenable(0, button_handler);
1155 static void add_msg(struct event_t *event, char *msg, int argv)
1162 s = skb_put(event->skb, strlen(msg) + 1);
1166 static void hotplug_button(struct work_struct *work)
1168 struct event_t *event = container_of(work, struct event_t, wq);
1171 event->skb = alloc_skb(2048, GFP_KERNEL);
1173 s = skb_put(event->skb, strlen(event->action) + 2);
1174 sprintf(s, "%s@", event->action);
1177 NETLINK_CB(event->skb).dst_group = 1;
1178 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
1184 static int fill_event (struct event_t *event)
1186 static char buf[128];
1188 add_msg(event, "HOME=/", 0);
1189 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1190 add_msg(event, "SUBSYSTEM=button", 0);
1191 snprintf(buf, 128, "ACTION=%s", event->action);
1192 add_msg(event, buf, 0);
1193 snprintf(buf, 128, "BUTTON=%s", event->name);
1194 add_msg(event, buf, 0);
1195 snprintf(buf, 128, "SEEN=%ld", event->seen);
1196 add_msg(event, buf, 0);
1197 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1198 add_msg(event, buf, 0);
1204 static irqreturn_t button_handler(int irq, void *dev_id)
1209 in = gpio_in() & platform.button_mask;
1210 gpio_intpolarity(platform.button_mask, in);
1211 changed = platform.button_polarity ^ in;
1212 platform.button_polarity = in;
1214 changed &= ~gpio_outen(0, 0);
1216 for (b = platform.buttons; b->name; b++) {
1217 struct event_t *event;
1219 if (!(b->gpio & changed)) continue;
1223 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1224 event->seen = (jiffies - b->seen)/HZ;
1225 event->name = b->name;
1226 event->action = b->pressed ? "pressed" : "released";
1227 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1228 schedule_work(&event->wq);
1236 static void register_leds(struct led_t *l)
1238 struct proc_dir_entry *p;
1243 leds = proc_mkdir("led", diag);
1247 for(; l->name; l++) {
1248 if (l->gpio & gpiomask)
1251 if (l->gpio & GPIO_TYPE_EXTIF) {
1255 if (l->polarity != INPUT) oe_mask |= l->gpio;
1257 val |= (l->polarity == NORMAL)?0:l->gpio;
1260 if (l->polarity == INPUT) continue;
1262 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1263 l->proc.type = PROC_LED;
1265 p->data = (void *) &l->proc;
1266 p->proc_fops = &diag_proc_fops;
1270 gpio_outen(mask, oe_mask);
1271 gpio_control(mask, 0);
1272 gpio_out(mask, val);
1273 gpio_setintmask(mask, 0);
1276 static void unregister_leds(struct led_t *l)
1279 remove_proc_entry(l->name, leds);
1281 remove_proc_entry("led", diag);
1284 static void set_led_extif(struct led_t *led)
1286 gpio_set_extif(led->gpio, led->state);
1289 static void led_flash(unsigned long dummy) {
1294 for (l = platform.leds; l->name; l++) {
1296 if (l->gpio & GPIO_TYPE_EXTIF) {
1298 l->state = !l->state;
1308 u32 val = ~gpio_in();
1310 gpio_outen(mask, mask);
1311 gpio_control(mask, 0);
1312 gpio_out(mask, val);
1314 if (mask || extif_blink) {
1315 mod_timer(&led_timer, jiffies + FLASH_TIME);
1319 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1321 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1325 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1328 if (dent->data != NULL) {
1329 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1330 switch (handler->type) {
1332 struct led_t * led = (struct led_t *) handler->ptr;
1334 len = sprintf(page, "f\n");
1336 if (led->gpio & GPIO_TYPE_EXTIF) {
1337 len = sprintf(page, "%d\n", led->state);
1339 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1340 u8 p = (led->polarity == NORMAL ? 0 : 1);
1341 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1347 len = sprintf(page, "%s\n", platform.name);
1350 len = sprintf(page, "0x%04x\n", gpiomask);
1357 len = min_t(int, len - *ppos, count);
1358 if (copy_to_user(buf, (page + *ppos), len)) {
1372 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1374 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1378 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1381 if (copy_from_user(page, buf, count)) {
1387 if (dent->data != NULL) {
1388 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1389 switch (handler->type) {
1391 struct led_t *led = (struct led_t *) handler->ptr;
1392 int p = (led->polarity == NORMAL ? 0 : 1);
1394 if (page[0] == 'f') {
1399 if (led->gpio & GPIO_TYPE_EXTIF) {
1400 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1403 gpio_outen(led->gpio, led->gpio);
1404 gpio_control(led->gpio, 0);
1405 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1411 gpiomask = simple_strtoul(page, NULL, 0);
1413 if (platform.buttons) {
1414 unregister_buttons(platform.buttons);
1415 register_buttons(platform.buttons);
1418 if (platform.leds) {
1419 unregister_leds(platform.leds);
1420 register_leds(platform.leds);
1431 static int __init diag_init(void)
1433 static struct proc_dir_entry *p;
1434 static struct platform_t *detected;
1436 detected = platform_detect();
1438 printk(MODULE_NAME ": Router model not detected.\n");
1441 memcpy(&platform, detected, sizeof(struct platform_t));
1443 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1444 if (platform.platform_init != NULL) {
1445 platform.platform_init();
1448 if (!(diag = proc_mkdir("diag", NULL))) {
1449 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1453 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1454 p->data = (void *) &proc_model;
1455 p->proc_fops = &diag_proc_fops;
1458 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1459 p->data = (void *) &proc_gpiomask;
1460 p->proc_fops = &diag_proc_fops;
1463 if (platform.buttons)
1464 register_buttons(platform.buttons);
1467 register_leds(platform.leds);
1472 static void __exit diag_exit(void)
1474 del_timer(&led_timer);
1476 if (platform.buttons)
1477 unregister_buttons(platform.buttons);
1480 unregister_leds(platform.leds);
1482 remove_proc_entry("model", diag);
1483 remove_proc_entry("gpiomask", diag);
1484 remove_proc_entry("diag", NULL);
1487 module_init(diag_init);
1488 module_exit(diag_exit);
1490 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1491 MODULE_LICENSE("GPL");