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);
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 << 7 }, // on back, hardwired, always resets device regardless OS state
786 { .name = "power", .gpio = 1 << 0 }, // on back
789 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
791 .platform_init = bcm4780_init,
795 .name = "D-Link DIR-130",
797 { .name = "reset", .gpio = 1 << 3},
798 { .name = "reserved", .gpio = 1 << 7},
801 { .name = "diag", .gpio = 1 << 0},
802 { .name = "blue", .gpio = 1 << 6},
806 .name = "D-Link DIR-320",
808 { .name = "reserved", .gpio = 1 << 6},
809 { .name = "reset", .gpio = 1 << 7},
812 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
813 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
814 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
815 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
816 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
820 .name = "D-Link DIR-330",
822 { .name = "reset", .gpio = 1 << 3},
823 { .name = "reserved", .gpio = 1 << 7},
826 { .name = "diag", .gpio = 1 << 0},
827 { .name = "usb", .gpio = 1 << 4},
828 { .name = "blue", .gpio = 1 << 6},
832 .name = "D-Link DWL-3150",
834 { .name = "reset", .gpio = 1 << 7},
837 { .name = "diag", .gpio = 1 << 2},
838 { .name = "status", .gpio = 1 << 1},
843 .name = "Sitecom WL-105b",
845 { .name = "reset", .gpio = 1 << 10},
848 { .name = "wlan", .gpio = 1 << 4},
849 { .name = "power", .gpio = 1 << 3},
852 /* Western Digital Net Center */
854 .name = "Western Digital NetCenter",
856 { .name = "power", .gpio = 1 << 0},
857 { .name = "reset", .gpio = 1 << 7},
859 .platform_init = NetCenter_init,
861 /* Askey (and clones) */
863 .name = "Askey RT210W",
865 /* Power button is hard-wired to hardware reset */
866 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
867 { .name = "power", .gpio = 1 << 7},
870 /* These were verified and named based on Belkin F5D4230-4 v1112 */
871 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
872 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
873 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
877 .name = "OvisLink WL-1600GL",
879 { .name = "reset", .gpio = 1 << 3 },
880 { .name = "ses", .gpio = 1 << 4 },
883 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
884 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
885 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
886 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
891 .name = "Microsoft MN-700",
893 { .name = "reset", .gpio = 1 << 7 },
896 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
901 static struct platform_t __init *platform_detect(void)
903 char *boardnum, *boardtype, *buf;
905 if (strcmp(getvar("nvram_type"), "cfe") == 0)
906 return &platforms[WGT634U];
908 /* Look for a model identifier */
910 /* Based on "model_name" */
911 if ((buf = nvram_get("model_name"))) {
912 if (!strcmp(buf, "DIR-130"))
913 return &platforms[DIR130];
914 if (!strcmp(buf, "DIR-330"))
915 return &platforms[DIR330];
918 /* Based on "wsc_modelname */
919 if ((buf = nvram_get("wsc_modelname"))) {
920 if (!strcmp(buf, "WRT610N"))
921 return &platforms[WRT610N];
924 /* Based on "model_no" */
925 if ((buf = nvram_get("model_no"))) {
926 if (startswith(buf,"WL700")) /* WL700* */
927 return &platforms[WL700GE];
930 /* Based on "hardware_version" */
931 if ((buf = nvram_get("hardware_version"))) {
932 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
933 return &platforms[WL500GPV2];
934 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
935 return &platforms[WL520GC];
936 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
937 return &platforms[WL520GU];
938 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
939 return &platforms[WL330GE];
942 /* Based on "ModelId" */
943 if ((buf = nvram_get("ModelId"))) {
944 if (!strcmp(buf, "WR850GP"))
945 return &platforms[WR850GP];
946 if (!strcmp(buf, "WR850G"))
947 return &platforms[WR850GV2V3];
948 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
949 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
950 if (startswith(buf,"WE800G")) /* WE800G* */
951 return &platforms[WE800G];
955 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
956 /* Buffalo hardware, check id for specific hardware matches */
957 if (!strcmp(buf, "29bb0332"))
958 return &platforms[WBR2_G54];
959 if (!strcmp(buf, "29129"))
960 return &platforms[WLA2_G54L];
961 if (!strcmp(buf, "30189"))
962 return &platforms[WHR_HP_G54];
963 if (!strcmp(buf, "32093"))
964 return &platforms[WHR_G125];
965 if (!strcmp(buf, "30182"))
966 return &platforms[WHR_G54S];
967 if (!strcmp(buf, "290441dd"))
968 return &platforms[WHR2_A54G54];
969 if (!strcmp(buf, "31120"))
970 return &platforms[WZR_G300N];
971 if (!strcmp(buf, "30083"))
972 return &platforms[WZR_RS_G54];
973 if (!strcmp(buf, "30103"))
974 return &platforms[WZR_RS_G54HP];
977 /* no easy model number, attempt to guess */
978 boardnum = getvar("boardnum");
979 boardtype = getvar("boardtype");
981 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
982 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
983 return &platforms[WRT600NV11];
985 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
986 return &platforms[WRT600N];
989 if (startswith(getvar("pmon_ver"), "CFE")) {
990 /* CFE based - newer hardware */
991 if (!strcmp(boardnum, "42")) { /* Linksys */
992 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
993 return &platforms[WRT300NV11];
995 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
996 return &platforms[WRT350N];
998 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
999 return &platforms[WRT54G3G];
1001 if (!strcmp(boardtype, "0x042f") && !strcmp(getvar("model_name"), "WRT54G3GV2-VF"))
1002 return &platforms[WRT54G3GV2_VF];
1004 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
1005 return &platforms[WRTSL54GS];
1007 if (!strcmp(boardtype, "0x0472"))
1008 return &platforms[WRT160N];
1010 /* default to WRT54G */
1011 return &platforms[WRT54G];
1013 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1014 return &platforms[WAP54GV2];
1016 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1017 return &platforms[WL1600GL];
1020 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1021 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1022 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1025 if (!strcmp(boardnum, "45")) { /* ASUS */
1026 if (!strcmp(boardtype,"0x042f"))
1027 return &platforms[WL500GP];
1028 else if (!strcmp(boardtype,"0x0472"))
1029 return &platforms[WL500W];
1030 else if (!strcmp(boardtype,"0x467"))
1031 return &platforms[WL320GE];
1033 return &platforms[WL500GD];
1036 if (!strcmp(boardnum, "10496"))
1037 return &platforms[USR5461];
1039 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1040 return &platforms[SE505V2];
1042 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1043 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1044 return &platforms[DIR320];
1046 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1047 return &platforms[WDNetCenter];
1050 if (!strcmp(boardnum, "08") || !strcmp(boardnum, "01") &&
1051 !strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B V1 and V2*/
1052 /* TODO: Check for version. Default platform is V2 for now. */
1053 return &platforms[WNR834BV2];
1056 } else { /* PMON based - old stuff */
1057 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1058 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1059 return &platforms[WR850GV1];
1061 if (startswith(boardtype, "bcm94710dev")) {
1062 if (!strcmp(boardnum, "42"))
1063 return &platforms[WRT54GV1];
1064 if (simple_strtoul(boardnum, NULL, 0) == 2)
1065 return &platforms[WAP54GV1];
1067 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1068 if (startswith(getvar("hardware_version"), "WL500-")) {
1069 if (!strcmp(getvar("boardnum"), "mn700"))
1070 return &platforms[MN700];
1072 return &platforms[WL500G];
1074 if (startswith(getvar("hardware_version"), "WL300-")) {
1075 /* Either WL-300g or WL-HDD, do more extensive checks */
1076 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1077 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1078 return &platforms[WLHDD];
1079 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1080 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1081 return &platforms[WL300G];
1083 /* Sitecom WL-105b */
1084 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1085 return &platforms[WL105B];
1087 /* unknown asus stuff, probably bcm4702 */
1088 if (startswith(boardnum, "asusX"))
1089 return &platforms[ASUS_4702];
1091 /* bcm4702 based Askey RT210W clones, Including:
1092 * Askey RT210W (duh?)
1094 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1096 if (!strcmp(boardtype,"bcm94710r4")
1097 && !strcmp(boardnum ,"100")
1098 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1100 return &platforms[RT210W];
1104 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1105 if (startswith(boardtype, "bcm94710ap"))
1106 return &platforms[BUFFALO_UNKNOWN_4710];
1108 return &platforms[BUFFALO_UNKNOWN];
1111 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1112 startswith(getvar("CFEver"), "MotoWRv3") ||
1113 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1115 return &platforms[WR850GV2V3];
1118 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1119 return &platforms[TEW411BRPP];
1122 if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1123 return &platforms[STI_NAS];
1125 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1126 return &platforms[DWL3150];
1132 static void register_buttons(struct button_t *b)
1134 for (; b->name; b++)
1135 platform.button_mask |= b->gpio;
1137 platform.button_mask &= ~gpiomask;
1139 gpio_outen(platform.button_mask, 0);
1140 gpio_control(platform.button_mask, 0);
1141 platform.button_polarity = gpio_in() & platform.button_mask;
1142 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1143 gpio_setintmask(platform.button_mask, platform.button_mask);
1145 gpio_set_irqenable(1, button_handler);
1148 static void unregister_buttons(struct button_t *b)
1150 gpio_setintmask(platform.button_mask, 0);
1152 gpio_set_irqenable(0, button_handler);
1156 static void add_msg(struct event_t *event, char *msg, int argv)
1163 s = skb_put(event->skb, strlen(msg) + 1);
1167 static void hotplug_button(struct work_struct *work)
1169 struct event_t *event = container_of(work, struct event_t, wq);
1175 event->skb = alloc_skb(2048, GFP_KERNEL);
1177 s = skb_put(event->skb, strlen(event->action) + 2);
1178 sprintf(s, "%s@", event->action);
1181 NETLINK_CB(event->skb).dst_group = 1;
1182 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1188 static int fill_event (struct event_t *event)
1190 static char buf[128];
1192 add_msg(event, "HOME=/", 0);
1193 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1194 add_msg(event, "SUBSYSTEM=button", 0);
1195 snprintf(buf, 128, "ACTION=%s", event->action);
1196 add_msg(event, buf, 0);
1197 snprintf(buf, 128, "BUTTON=%s", event->name);
1198 add_msg(event, buf, 0);
1199 snprintf(buf, 128, "SEEN=%ld", event->seen);
1200 add_msg(event, buf, 0);
1201 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1202 add_msg(event, buf, 0);
1208 static irqreturn_t button_handler(int irq, void *dev_id)
1213 in = gpio_in() & platform.button_mask;
1214 gpio_intpolarity(platform.button_mask, in);
1215 changed = platform.button_polarity ^ in;
1216 platform.button_polarity = in;
1218 changed &= ~gpio_outen(0, 0);
1220 for (b = platform.buttons; b->name; b++) {
1221 struct event_t *event;
1223 if (!(b->gpio & changed)) continue;
1227 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1228 event->seen = (jiffies - b->seen)/HZ;
1229 event->name = b->name;
1230 event->action = b->pressed ? "pressed" : "released";
1231 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1232 schedule_work(&event->wq);
1240 static void register_leds(struct led_t *l)
1242 struct proc_dir_entry *p;
1247 leds = proc_mkdir("led", diag);
1251 for(; l->name; l++) {
1252 if (l->gpio & gpiomask)
1255 if (l->gpio & GPIO_TYPE_EXTIF) {
1259 if (l->polarity != INPUT) oe_mask |= l->gpio;
1261 val |= (l->polarity == NORMAL)?0:l->gpio;
1264 if (l->polarity == INPUT) continue;
1266 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1267 l->proc.type = PROC_LED;
1269 p->data = (void *) &l->proc;
1270 p->proc_fops = &diag_proc_fops;
1274 gpio_outen(mask, oe_mask);
1275 gpio_control(mask, 0);
1276 gpio_out(mask, val);
1277 gpio_setintmask(mask, 0);
1280 static void unregister_leds(struct led_t *l)
1283 remove_proc_entry(l->name, leds);
1285 remove_proc_entry("led", diag);
1288 static void set_led_extif(struct led_t *led)
1290 gpio_set_extif(led->gpio, led->state);
1293 static void led_flash(unsigned long dummy) {
1298 for (l = platform.leds; l->name; l++) {
1300 if (l->gpio & GPIO_TYPE_EXTIF) {
1302 l->state = !l->state;
1312 u32 val = ~gpio_in();
1314 gpio_outen(mask, mask);
1315 gpio_control(mask, 0);
1316 gpio_out(mask, val);
1318 if (mask || extif_blink) {
1319 mod_timer(&led_timer, jiffies + FLASH_TIME);
1323 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1325 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1329 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1332 if (dent->data != NULL) {
1333 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1334 switch (handler->type) {
1336 struct led_t * led = (struct led_t *) handler->ptr;
1338 len = sprintf(page, "f\n");
1340 if (led->gpio & GPIO_TYPE_EXTIF) {
1341 len = sprintf(page, "%d\n", led->state);
1343 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1344 u8 p = (led->polarity == NORMAL ? 0 : 1);
1345 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1351 len = sprintf(page, "%s\n", platform.name);
1354 len = sprintf(page, "0x%04x\n", gpiomask);
1361 len = min_t(int, len - *ppos, count);
1362 if (copy_to_user(buf, (page + *ppos), len)) {
1376 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1378 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1382 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1385 if (copy_from_user(page, buf, count)) {
1391 if (dent->data != NULL) {
1392 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1393 switch (handler->type) {
1395 struct led_t *led = (struct led_t *) handler->ptr;
1396 int p = (led->polarity == NORMAL ? 0 : 1);
1398 if (page[0] == 'f') {
1403 if (led->gpio & GPIO_TYPE_EXTIF) {
1404 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1407 gpio_outen(led->gpio, led->gpio);
1408 gpio_control(led->gpio, 0);
1409 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1415 gpiomask = simple_strtoul(page, NULL, 0);
1417 if (platform.buttons) {
1418 unregister_buttons(platform.buttons);
1419 register_buttons(platform.buttons);
1422 if (platform.leds) {
1423 unregister_leds(platform.leds);
1424 register_leds(platform.leds);
1435 static int __init diag_init(void)
1437 static struct proc_dir_entry *p;
1438 static struct platform_t *detected;
1440 detected = platform_detect();
1442 printk(MODULE_NAME ": Router model not detected.\n");
1445 memcpy(&platform, detected, sizeof(struct platform_t));
1447 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1448 if (platform.platform_init != NULL) {
1449 platform.platform_init();
1452 if (!(diag = proc_mkdir("diag", NULL))) {
1453 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1457 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1458 p->data = (void *) &proc_model;
1459 p->proc_fops = &diag_proc_fops;
1462 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1463 p->data = (void *) &proc_gpiomask;
1464 p->proc_fops = &diag_proc_fops;
1467 if (platform.buttons)
1468 register_buttons(platform.buttons);
1471 register_leds(platform.leds);
1476 static void __exit diag_exit(void)
1478 del_timer(&led_timer);
1480 if (platform.buttons)
1481 unregister_buttons(platform.buttons);
1484 unregister_leds(platform.leds);
1486 remove_proc_entry("model", diag);
1487 remove_proc_entry("gpiomask", diag);
1488 remove_proc_entry("diag", NULL);
1491 module_init(diag_init);
1492 module_exit(diag_exit);
1494 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1495 MODULE_LICENSE("GPL");