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>
34 extern struct sock *uevent_sock;
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);
129 /* Western Digital */
142 static void __init bcm4780_init(void) {
145 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
146 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
147 gpio_outen(pin, pin);
148 gpio_control(pin, 0);
151 /* Wait 5s, so the HDD can spin up */
152 set_current_state(TASK_INTERRUPTIBLE);
153 schedule_timeout(HZ * 5);
156 static void __init NetCenter_init(void) {
157 /* unset pin 6 (+12V) */
159 gpio_outen(pin, pin);
160 gpio_control(pin, 0);
162 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
164 gpio_outen(pin, pin);
165 gpio_control(pin, 0);
167 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
171 static void __init bcm57xx_init(void) {
174 /* FIXME: switch comes up, but port mappings/vlans not right */
175 gpio_outen(pin, pin);
176 gpio_control(pin, 0);
180 static struct platform_t __initdata platforms[] = {
183 .name = "Linksys WAP54G V1",
185 { .name = "reset", .gpio = 1 << 0 },
188 { .name = "diag", .gpio = 1 << 3 },
189 { .name = "wlan", .gpio = 1 << 4 },
193 .name = "Linksys WAP54G V2",
195 { .name = "reset", .gpio = 1 << 0 },
198 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
199 /* GPIO 6 is b44 (eth0, LAN) PHY power */
203 .name = "Linksys WAP54G V3",
205 /* FIXME: verify this */
206 { .name = "reset", .gpio = 1 << 7 },
207 { .name = "ses", .gpio = 1 << 0 },
211 { .name = "ses", .gpio = 1 << 1 },
215 .name = "Linksys WRT54G V1.x",
217 { .name = "reset", .gpio = 1 << 6 },
220 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
221 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
225 .name = "Linksys WRT54G/GS/GL",
227 { .name = "reset", .gpio = 1 << 6 },
228 { .name = "ses", .gpio = 1 << 4 },
231 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
232 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
233 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
234 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
235 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
239 .name = "Linksys WRTSL54GS",
241 { .name = "reset", .gpio = 1 << 6 },
242 { .name = "ses", .gpio = 1 << 4 },
245 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
246 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
247 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
248 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
252 .name = "Linksys WRT54G3G",
254 { .name = "reset", .gpio = 1 << 6 },
255 { .name = "3g", .gpio = 1 << 4 },
258 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
259 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
260 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
261 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
262 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
266 .name = "Linksys WRT54G3GV2-VF",
268 { .name = "reset", .gpio = 1 << 6 },
269 { .name = "3g", .gpio = 1 << 5 },
272 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
273 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
274 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
278 .name = "Linksys WRT160N",
280 { .name = "reset", .gpio = 1 << 6 },
281 { .name = "ses", .gpio = 1 << 4 },
284 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
285 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
286 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
290 .name = "Linksys WRT300N V1.1",
292 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
293 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
296 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
297 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
298 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
300 .platform_init = bcm57xx_init,
303 .name = "Linksys WRT350N",
305 { .name = "reset", .gpio = 1 << 6 },
306 { .name = "ses", .gpio = 1 << 8 },
309 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
310 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
311 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
312 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
313 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
315 .platform_init = bcm57xx_init,
318 .name = "Linksys WRT600N",
320 { .name = "reset", .gpio = 1 << 6 },
321 { .name = "ses", .gpio = 1 << 7 },
324 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
325 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
326 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
327 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
328 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
329 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
331 .platform_init = bcm57xx_init,
334 .name = "Linksys WRT600N V1.1",
336 { .name = "reset", .gpio = 1 << 6 },
337 { .name = "ses", .gpio = 1 << 7 },
340 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
341 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
342 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
343 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
344 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
345 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
347 .platform_init = bcm57xx_init,
350 .name = "Linksys WRT610N",
352 { .name = "reset", .gpio = 1 << 6 },
353 { .name = "ses", .gpio = 1 << 8 },
356 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
357 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
358 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
359 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
364 .name = "ASUS WL-HDD",
366 { .name = "reset", .gpio = 1 << 6 },
369 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
370 { .name = "usb", .gpio = 1 << 2, .polarity = REVERSE },
374 .name = "ASUS WL-300g",
376 { .name = "reset", .gpio = 1 << 6 },
379 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
383 .name = "ASUS WL-320gE/WL-320gP",
385 { .name = "reset", .gpio = 1 << 6 },
388 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
389 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
390 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
394 .name = "ASUS WL-330gE",
396 { .name = "reset", .gpio = 1 << 2 },
399 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
403 .name = "ASUS WL-500g",
405 { .name = "reset", .gpio = 1 << 6 },
408 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
412 .name = "ASUS WL-500g Deluxe",
414 { .name = "reset", .gpio = 1 << 6 },
417 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
421 .name = "ASUS WL-500g Premium",
423 { .name = "reset", .gpio = 1 << 0 },
424 { .name = "ses", .gpio = 1 << 4 },
427 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
431 .name = "ASUS WL-500g Premium V2",
433 { .name = "reset", .gpio = 1 << 2 },
434 { .name = "ses", .gpio = 1 << 3 },
437 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
438 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
442 .name = "ASUS WL-500W",
444 { .name = "reset", .gpio = 1 << 6 },
445 { .name = "ses", .gpio = 1 << 7 },
448 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
452 .name = "ASUS WL-520GC",
454 { .name = "reset", .gpio = 1 << 2 },
455 { .name = "ses", .gpio = 1 << 3 },
458 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
459 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
463 .name = "ASUS WL-520gU",
465 { .name = "reset", .gpio = 1 << 2 },
466 { .name = "ses", .gpio = 1 << 3 },
469 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
470 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
474 .name = "ASUS (unknown, BCM4702)",
476 { .name = "reset", .gpio = 1 << 6 },
479 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
483 .name = "ASUS WL-700gE",
485 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
486 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
487 { .name = "power", .gpio = 1 << 0 }, // on front
488 { .name = "copy", .gpio = 1 << 6 }, // on front
492 // GPIO that controls power led also enables/disables some essential functions
495 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
497 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
499 .platform_init = bcm4780_init,
503 .name = "Buffalo WHR-G54S",
505 { .name = "reset", .gpio = 1 << 4 },
506 { .name = "bridge", .gpio = 1 << 5 },
507 { .name = "ses", .gpio = 1 << 0 },
510 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
511 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
512 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
513 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
514 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
518 .name = "Buffalo WBR2-G54",
521 { .name = "reset", .gpio = 1 << 7 },
524 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
528 .name = "Buffalo WHR-HP-G54",
530 { .name = "reset", .gpio = 1 << 4 },
531 { .name = "bridge", .gpio = 1 << 5 },
532 { .name = "ses", .gpio = 1 << 0 },
535 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
536 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
537 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
538 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
539 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
543 .name = "Buffalo WHR-G125",
545 { .name = "reset", .gpio = 1 << 4 },
546 { .name = "bridge", .gpio = 1 << 5 },
547 { .name = "ses", .gpio = 1 << 0 },
550 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
551 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
552 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
553 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
554 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
558 .name = "Buffalo WHR2-A54G54",
560 { .name = "reset", .gpio = 1 << 4 },
563 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
567 .name = "Buffalo WLA2-G54L",
570 { .name = "reset", .gpio = 1 << 7 },
573 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
577 .name = "Buffalo WZR-G300N",
579 { .name = "reset", .gpio = 1 << 4 },
582 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
583 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
584 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
588 .name = "Buffalo WZR-RS-G54",
590 { .name = "ses", .gpio = 1 << 0 },
591 { .name = "reset", .gpio = 1 << 4 },
594 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
595 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
596 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
600 .name = "Buffalo WZR-RS-G54HP",
602 { .name = "ses", .gpio = 1 << 0 },
603 { .name = "reset", .gpio = 1 << 4 },
606 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
607 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
608 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
611 [BUFFALO_UNKNOWN] = {
612 .name = "Buffalo (unknown)",
614 { .name = "reset", .gpio = 1 << 7 },
617 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
620 [BUFFALO_UNKNOWN_4710] = {
621 .name = "Buffalo (unknown, BCM4710)",
623 { .name = "reset", .gpio = 1 << 4 },
626 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
631 .name = "Siemens SE505 V1",
633 /* No usable buttons */
636 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
637 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
638 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
642 .name = "Siemens SE505 V2",
644 /* No usable buttons */
647 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
648 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
649 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
654 .name = "U.S. Robotics USR5461",
656 /* No usable buttons */
659 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
660 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
665 .name = "Dell TrueMobile 2300",
667 { .name = "reset", .gpio = 1 << 0 },
670 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
671 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
675 .name = "Dell TrueMobile 2300 v2",
677 { .name = "reset", .gpio = 1 << 0 },
680 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
681 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
686 .name = "Motorola WE800G",
688 { .name = "reset", .gpio = 1 << 0 },
691 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
692 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
693 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
697 .name = "Motorola WR850G V1",
699 { .name = "reset", .gpio = 1 << 0 },
702 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
703 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
704 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
705 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
706 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
710 .name = "Motorola WR850G V2/V3",
712 { .name = "reset", .gpio = 1 << 5 },
715 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
716 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
717 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
718 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
722 .name = "Motorola WR850GP",
724 { .name = "reset", .gpio = 1 << 5 },
727 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
728 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
729 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
730 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
736 .name = "Belkin (unknown)",
737 /* FIXME: verify & add detection */
739 { .name = "reset", .gpio = 1 << 7 },
742 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
743 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
744 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
749 .name = "Netgear WGT634U",
751 { .name = "reset", .gpio = 1 << 2 },
754 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
759 .name = "Trendware TEW411BRP+",
761 { /* No usable buttons */ },
764 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
765 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
766 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
771 .name = "SimpleTech SimpleShare NAS",
773 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
774 { .name = "power", .gpio = 1 << 0 }, // on back
777 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
779 .platform_init = bcm4780_init,
783 .name = "D-Link DIR-130",
785 { .name = "reset", .gpio = 1 << 3},
786 { .name = "reserved", .gpio = 1 << 7},
789 { .name = "diag", .gpio = 1 << 0},
790 { .name = "blue", .gpio = 1 << 6},
794 .name = "D-Link DIR-320",
796 { .name = "reserved", .gpio = 1 << 6},
797 { .name = "reset", .gpio = 1 << 7},
800 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
801 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
802 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
803 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
804 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
808 .name = "D-Link DIR-330",
810 { .name = "reset", .gpio = 1 << 3},
811 { .name = "reserved", .gpio = 1 << 7},
814 { .name = "diag", .gpio = 1 << 0},
815 { .name = "usb", .gpio = 1 << 4},
816 { .name = "blue", .gpio = 1 << 6},
820 .name = "D-Link DWL-3150",
822 { .name = "reset", .gpio = 1 << 7},
825 { .name = "diag", .gpio = 1 << 2},
826 { .name = "status", .gpio = 1 << 1},
831 .name = "Sitecom WL-105b",
833 { .name = "reset", .gpio = 1 << 10},
836 { .name = "wlan", .gpio = 1 << 4},
837 { .name = "power", .gpio = 1 << 3},
840 /* Western Digital Net Center */
842 .name = "Western Digital NetCenter",
844 { .name = "power", .gpio = 1 << 0},
845 { .name = "reset", .gpio = 1 << 7},
847 .platform_init = NetCenter_init,
849 /* Askey (and clones) */
851 .name = "Askey RT210W",
853 /* Power button is hard-wired to hardware reset */
854 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
855 { .name = "power", .gpio = 1 << 7},
858 /* These were verified and named based on Belkin F5D4230-4 v1112 */
859 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
860 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
861 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
865 .name = "OvisLink WL-1600GL",
867 { .name = "reset", .gpio = 1 << 3 },
868 { .name = "ses", .gpio = 1 << 4 },
871 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
872 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
873 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
874 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
879 .name = "Microsoft MN-700",
881 { .name = "reset", .gpio = 1 << 7 },
884 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
889 static struct platform_t __init *platform_detect(void)
891 char *boardnum, *boardtype, *buf;
893 if (strcmp(getvar("nvram_type"), "cfe") == 0)
894 return &platforms[WGT634U];
896 /* Look for a model identifier */
898 /* Based on "model_name" */
899 if ((buf = nvram_get("model_name"))) {
900 if (!strcmp(buf, "DIR-130"))
901 return &platforms[DIR130];
902 if (!strcmp(buf, "DIR-330"))
903 return &platforms[DIR330];
906 /* Based on "wsc_modelname */
907 if ((buf = nvram_get("wsc_modelname"))) {
908 if (!strcmp(buf, "WRT610N"))
909 return &platforms[WRT610N];
912 /* Based on "model_no" */
913 if ((buf = nvram_get("model_no"))) {
914 if (startswith(buf,"WL700")) /* WL700* */
915 return &platforms[WL700GE];
918 /* Based on "hardware_version" */
919 if ((buf = nvram_get("hardware_version"))) {
920 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
921 return &platforms[WL500GPV2];
922 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
923 return &platforms[WL520GC];
924 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
925 return &platforms[WL520GU];
926 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
927 return &platforms[WL330GE];
930 /* Based on "ModelId" */
931 if ((buf = nvram_get("ModelId"))) {
932 if (!strcmp(buf, "WR850GP"))
933 return &platforms[WR850GP];
934 if (!strcmp(buf, "WR850G"))
935 return &platforms[WR850GV2V3];
936 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
937 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
938 if (startswith(buf,"WE800G")) /* WE800G* */
939 return &platforms[WE800G];
943 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
944 /* Buffalo hardware, check id for specific hardware matches */
945 if (!strcmp(buf, "29bb0332"))
946 return &platforms[WBR2_G54];
947 if (!strcmp(buf, "29129"))
948 return &platforms[WLA2_G54L];
949 if (!strcmp(buf, "30189"))
950 return &platforms[WHR_HP_G54];
951 if (!strcmp(buf, "32093"))
952 return &platforms[WHR_G125];
953 if (!strcmp(buf, "30182"))
954 return &platforms[WHR_G54S];
955 if (!strcmp(buf, "290441dd"))
956 return &platforms[WHR2_A54G54];
957 if (!strcmp(buf, "31120"))
958 return &platforms[WZR_G300N];
959 if (!strcmp(buf, "30083"))
960 return &platforms[WZR_RS_G54];
961 if (!strcmp(buf, "30103"))
962 return &platforms[WZR_RS_G54HP];
965 /* no easy model number, attempt to guess */
966 boardnum = getvar("boardnum");
967 boardtype = getvar("boardtype");
969 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
970 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
971 return &platforms[WRT600NV11];
973 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
974 return &platforms[WRT600N];
977 if (startswith(getvar("pmon_ver"), "CFE")) {
978 /* CFE based - newer hardware */
979 if (!strcmp(boardnum, "42")) { /* Linksys */
980 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
981 return &platforms[WRT300NV11];
983 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
984 return &platforms[WRT350N];
986 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
987 return &platforms[WRT54G3G];
989 if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
990 return &platforms[WRT54G3GV2_VF];
992 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
993 return &platforms[WRTSL54GS];
995 if (!strcmp(boardtype, "0x0472"))
996 return &platforms[WRT160N];
998 /* default to WRT54G */
999 return &platforms[WRT54G];
1001 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1002 return &platforms[WAP54GV2];
1004 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1005 return &platforms[WL1600GL];
1008 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1009 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1010 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1013 if (!strcmp(boardnum, "45")) { /* ASUS */
1014 if (!strcmp(boardtype,"0x042f"))
1015 return &platforms[WL500GP];
1016 else if (!strcmp(boardtype,"0x0472"))
1017 return &platforms[WL500W];
1018 else if (!strcmp(boardtype,"0x467"))
1019 return &platforms[WL320GE];
1021 return &platforms[WL500GD];
1024 if (!strcmp(boardnum, "10496"))
1025 return &platforms[USR5461];
1027 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1028 return &platforms[SE505V2];
1030 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1031 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1032 return &platforms[DIR320];
1034 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1035 return &platforms[WDNetCenter];
1038 } else { /* PMON based - old stuff */
1039 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1040 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1041 return &platforms[WR850GV1];
1043 if (startswith(boardtype, "bcm94710dev")) {
1044 if (!strcmp(boardnum, "42"))
1045 return &platforms[WRT54GV1];
1046 if (simple_strtoul(boardnum, NULL, 0) == 2)
1047 return &platforms[WAP54GV1];
1049 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1050 if (startswith(getvar("hardware_version"), "WL500-")) {
1051 if (!strcmp(getvar("boardnum"), "mn700"))
1052 return &platforms[MN700];
1054 return &platforms[WL500G];
1056 if (startswith(getvar("hardware_version"), "WL300-")) {
1057 /* Either WL-300g or WL-HDD, do more extensive checks */
1058 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1059 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1060 return &platforms[WLHDD];
1061 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1062 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1063 return &platforms[WL300G];
1065 /* Sitecom WL-105b */
1066 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1067 return &platforms[WL105B];
1069 /* unknown asus stuff, probably bcm4702 */
1070 if (startswith(boardnum, "asusX"))
1071 return &platforms[ASUS_4702];
1073 /* bcm4702 based Askey RT210W clones, Including:
1074 * Askey RT210W (duh?)
1076 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1078 if (!strcmp(boardtype,"bcm94710r4")
1079 && !strcmp(boardnum ,"100")
1080 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1082 return &platforms[RT210W];
1086 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1087 if (startswith(boardtype, "bcm94710ap"))
1088 return &platforms[BUFFALO_UNKNOWN_4710];
1090 return &platforms[BUFFALO_UNKNOWN];
1093 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1094 startswith(getvar("CFEver"), "MotoWRv3") ||
1095 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1097 return &platforms[WR850GV2V3];
1100 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1101 return &platforms[TEW411BRPP];
1104 if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1105 return &platforms[STI_NAS];
1107 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1108 return &platforms[DWL3150];
1114 static void register_buttons(struct button_t *b)
1116 for (; b->name; b++)
1117 platform.button_mask |= b->gpio;
1119 platform.button_mask &= ~gpiomask;
1121 gpio_outen(platform.button_mask, 0);
1122 gpio_control(platform.button_mask, 0);
1123 platform.button_polarity = gpio_in() & platform.button_mask;
1124 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1125 gpio_setintmask(platform.button_mask, platform.button_mask);
1127 gpio_set_irqenable(1, button_handler);
1130 static void unregister_buttons(struct button_t *b)
1132 gpio_setintmask(platform.button_mask, 0);
1134 gpio_set_irqenable(0, button_handler);
1138 static void add_msg(struct event_t *event, char *msg, int argv)
1145 s = skb_put(event->skb, strlen(msg) + 1);
1149 static void hotplug_button(struct work_struct *work)
1151 struct event_t *event = container_of(work, struct event_t, wq);
1157 event->skb = alloc_skb(2048, GFP_KERNEL);
1159 s = skb_put(event->skb, strlen(event->action) + 2);
1160 sprintf(s, "%s@", event->action);
1163 NETLINK_CB(event->skb).dst_group = 1;
1164 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1170 static int fill_event (struct event_t *event)
1172 static char buf[128];
1174 add_msg(event, "HOME=/", 0);
1175 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1176 add_msg(event, "SUBSYSTEM=button", 0);
1177 snprintf(buf, 128, "ACTION=%s", event->action);
1178 add_msg(event, buf, 0);
1179 snprintf(buf, 128, "BUTTON=%s", event->name);
1180 add_msg(event, buf, 0);
1181 snprintf(buf, 128, "SEEN=%ld", event->seen);
1182 add_msg(event, buf, 0);
1183 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1184 add_msg(event, buf, 0);
1190 static irqreturn_t button_handler(int irq, void *dev_id)
1195 in = gpio_in() & platform.button_mask;
1196 gpio_intpolarity(platform.button_mask, in);
1197 changed = platform.button_polarity ^ in;
1198 platform.button_polarity = in;
1200 changed &= ~gpio_outen(0, 0);
1202 for (b = platform.buttons; b->name; b++) {
1203 struct event_t *event;
1205 if (!(b->gpio & changed)) continue;
1209 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1210 event->seen = (jiffies - b->seen)/HZ;
1211 event->name = b->name;
1212 event->action = b->pressed ? "pressed" : "released";
1213 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1214 schedule_work(&event->wq);
1222 static void register_leds(struct led_t *l)
1224 struct proc_dir_entry *p;
1229 leds = proc_mkdir("led", diag);
1233 for(; l->name; l++) {
1234 if (l->gpio & gpiomask)
1237 if (l->gpio & GPIO_TYPE_EXTIF) {
1241 if (l->polarity != INPUT) oe_mask |= l->gpio;
1243 val |= (l->polarity == NORMAL)?0:l->gpio;
1246 if (l->polarity == INPUT) continue;
1248 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1249 l->proc.type = PROC_LED;
1251 p->data = (void *) &l->proc;
1252 p->proc_fops = &diag_proc_fops;
1256 gpio_outen(mask, oe_mask);
1257 gpio_control(mask, 0);
1258 gpio_out(mask, val);
1259 gpio_setintmask(mask, 0);
1262 static void unregister_leds(struct led_t *l)
1265 remove_proc_entry(l->name, leds);
1267 remove_proc_entry("led", diag);
1270 static void set_led_extif(struct led_t *led)
1272 gpio_set_extif(led->gpio, led->state);
1275 static void led_flash(unsigned long dummy) {
1280 for (l = platform.leds; l->name; l++) {
1282 if (l->gpio & GPIO_TYPE_EXTIF) {
1284 l->state = !l->state;
1294 u32 val = ~gpio_in();
1296 gpio_outen(mask, mask);
1297 gpio_control(mask, 0);
1298 gpio_out(mask, val);
1300 if (mask || extif_blink) {
1301 mod_timer(&led_timer, jiffies + FLASH_TIME);
1305 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1307 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1311 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1314 if (dent->data != NULL) {
1315 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1316 switch (handler->type) {
1318 struct led_t * led = (struct led_t *) handler->ptr;
1320 len = sprintf(page, "f\n");
1322 if (led->gpio & GPIO_TYPE_EXTIF) {
1323 len = sprintf(page, "%d\n", led->state);
1325 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1326 u8 p = (led->polarity == NORMAL ? 0 : 1);
1327 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1333 len = sprintf(page, "%s\n", platform.name);
1336 len = sprintf(page, "0x%04x\n", gpiomask);
1343 len = min_t(int, len - *ppos, count);
1344 if (copy_to_user(buf, (page + *ppos), len)) {
1358 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1360 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1364 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1367 if (copy_from_user(page, buf, count)) {
1373 if (dent->data != NULL) {
1374 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1375 switch (handler->type) {
1377 struct led_t *led = (struct led_t *) handler->ptr;
1378 int p = (led->polarity == NORMAL ? 0 : 1);
1380 if (page[0] == 'f') {
1385 if (led->gpio & GPIO_TYPE_EXTIF) {
1386 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1389 gpio_outen(led->gpio, led->gpio);
1390 gpio_control(led->gpio, 0);
1391 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1397 gpiomask = simple_strtoul(page, NULL, 0);
1399 if (platform.buttons) {
1400 unregister_buttons(platform.buttons);
1401 register_buttons(platform.buttons);
1404 if (platform.leds) {
1405 unregister_leds(platform.leds);
1406 register_leds(platform.leds);
1417 static int __init diag_init(void)
1419 static struct proc_dir_entry *p;
1420 static struct platform_t *detected;
1422 detected = platform_detect();
1424 printk(MODULE_NAME ": Router model not detected.\n");
1427 memcpy(&platform, detected, sizeof(struct platform_t));
1429 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1430 if (platform.platform_init != NULL) {
1431 platform.platform_init();
1434 if (!(diag = proc_mkdir("diag", NULL))) {
1435 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1439 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1440 p->data = (void *) &proc_model;
1441 p->proc_fops = &diag_proc_fops;
1444 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1445 p->data = (void *) &proc_gpiomask;
1446 p->proc_fops = &diag_proc_fops;
1449 if (platform.buttons)
1450 register_buttons(platform.buttons);
1453 register_leds(platform.leds);
1458 static void __exit diag_exit(void)
1460 del_timer(&led_timer);
1462 if (platform.buttons)
1463 unregister_buttons(platform.buttons);
1466 unregister_leds(platform.leds);
1468 remove_proc_entry("model", diag);
1469 remove_proc_entry("gpiomask", diag);
1470 remove_proc_entry("diag", NULL);
1473 module_init(diag_init);
1474 module_exit(diag_exit);
1476 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1477 MODULE_LICENSE("GPL");