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>
32 #include <linux/workqueue.h>
33 #include <linux/skbuff.h>
34 #include <linux/netlink.h>
36 extern struct sock *uevent_sock;
37 extern u64 uevent_next_seqnum(void);
39 #include <linux/tqueue.h>
40 #define INIT_WORK INIT_TQUEUE
41 #define schedule_work schedule_task
42 #define work_struct tq_struct
47 #define getvar(str) (nvram_get(str)?:"")
49 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
50 static int fill_event(struct event_t *);
51 static unsigned int gpiomask = 0;
52 module_param(gpiomask, int, 0644);
136 /* Western Digital */
149 static void __init bcm4780_init(void) {
152 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
153 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
154 gpio_outen(pin, pin);
155 gpio_control(pin, 0);
158 /* Wait 5s, so the HDD can spin up */
159 set_current_state(TASK_INTERRUPTIBLE);
160 schedule_timeout(HZ * 5);
163 static void __init NetCenter_init(void) {
164 /* unset pin 6 (+12V) */
166 gpio_outen(pin, pin);
167 gpio_control(pin, 0);
169 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
171 gpio_outen(pin, pin);
172 gpio_control(pin, 0);
174 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
178 static void __init bcm57xx_init(void) {
182 /* FIXME: switch comes up, but port mappings/vlans not right */
183 gpio_outen(pin, pin);
184 gpio_control(pin, 0);
189 static struct platform_t __initdata platforms[] = {
192 .name = "Linksys WAP54G V1",
194 { .name = "reset", .gpio = 1 << 0 },
197 { .name = "diag", .gpio = 1 << 3 },
198 { .name = "wlan", .gpio = 1 << 4 },
202 .name = "Linksys WAP54G V2",
204 { .name = "reset", .gpio = 1 << 0 },
207 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
208 /* GPIO 6 is b44 (eth0, LAN) PHY power */
212 .name = "Linksys WAP54G V3",
214 /* FIXME: verify this */
215 { .name = "reset", .gpio = 1 << 7 },
216 { .name = "ses", .gpio = 1 << 0 },
220 { .name = "ses", .gpio = 1 << 1 },
224 .name = "Linksys WRT54G V1.x",
226 { .name = "reset", .gpio = 1 << 6 },
229 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
230 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
234 .name = "Linksys WRT54G/GS/GL",
236 { .name = "reset", .gpio = 1 << 6 },
237 { .name = "ses", .gpio = 1 << 4 },
240 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
241 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
242 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
243 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
244 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
248 .name = "Linksys WRTSL54GS",
250 { .name = "reset", .gpio = 1 << 6 },
251 { .name = "ses", .gpio = 1 << 4 },
254 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
255 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
256 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
257 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
261 .name = "Linksys WRT54G3G",
263 { .name = "reset", .gpio = 1 << 6 },
264 { .name = "3g", .gpio = 1 << 4 },
267 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
268 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
269 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
270 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
271 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
275 .name = "Linksys WRT160N",
277 { .name = "reset", .gpio = 1 << 6 },
278 { .name = "ses", .gpio = 1 << 4 },
281 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
282 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
283 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
287 .name = "Linksys WRT300N V1.1",
289 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
290 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
293 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
294 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
295 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
297 .platform_init = bcm57xx_init,
300 .name = "Linksys WRT350N",
302 { .name = "reset", .gpio = 1 << 6 },
303 { .name = "ses", .gpio = 1 << 8 },
306 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
307 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
308 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
309 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
310 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
312 .platform_init = bcm57xx_init,
315 .name = "Linksys WRT600N",
317 { .name = "reset", .gpio = 1 << 6 },
318 { .name = "ses", .gpio = 1 << 7 },
321 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
322 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
323 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
324 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
325 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
326 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
328 .platform_init = bcm57xx_init,
331 .name = "Linksys WRT600N V1.1",
333 { .name = "reset", .gpio = 1 << 6 },
334 { .name = "ses", .gpio = 1 << 7 },
337 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
338 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
339 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
340 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
341 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
342 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
344 .platform_init = bcm57xx_init,
347 .name = "Linksys WRT610N",
349 { .name = "reset", .gpio = 1 << 6 },
350 { .name = "ses", .gpio = 1 << 8 },
353 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
354 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
355 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
356 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
361 .name = "ASUS WL-HDD",
363 { .name = "reset", .gpio = 1 << 6 },
366 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
367 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
371 .name = "ASUS WL-300g",
373 { .name = "reset", .gpio = 1 << 6 },
376 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
380 .name = "ASUS WL-320gE/WL-320gP",
382 { .name = "reset", .gpio = 1 << 6 },
385 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
386 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
387 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
391 .name = "ASUS WL-330gE",
393 { .name = "reset", .gpio = 1 << 2 },
396 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
400 .name = "ASUS WL-500g",
402 { .name = "reset", .gpio = 1 << 6 },
405 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
409 .name = "ASUS WL-500g Deluxe",
411 { .name = "reset", .gpio = 1 << 6 },
414 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
418 .name = "ASUS WL-500g Premium",
420 { .name = "reset", .gpio = 1 << 0 },
421 { .name = "ses", .gpio = 1 << 4 },
424 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
428 .name = "ASUS WL-500g Premium V2",
430 { .name = "reset", .gpio = 1 << 2 },
431 { .name = "ses", .gpio = 1 << 3 },
434 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
435 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
439 .name = "ASUS WL-500W",
441 { .name = "reset", .gpio = 1 << 6 },
442 { .name = "ses", .gpio = 1 << 7 },
445 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
449 .name = "ASUS WL-520GC",
451 { .name = "reset", .gpio = 1 << 2 },
452 { .name = "ses", .gpio = 1 << 3 },
455 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
456 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
460 .name = "ASUS WL-520gU",
462 { .name = "reset", .gpio = 1 << 2 },
463 { .name = "ses", .gpio = 1 << 3 },
466 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
467 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
471 .name = "ASUS (unknown, BCM4702)",
473 { .name = "reset", .gpio = 1 << 6 },
476 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
480 .name = "ASUS WL-700gE",
482 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
483 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
484 { .name = "power", .gpio = 1 << 0 }, // on front
485 { .name = "copy", .gpio = 1 << 6 }, // on front
489 // GPIO that controls power led also enables/disables some essential functions
492 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
494 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
496 .platform_init = bcm4780_init,
500 .name = "Buffalo WHR-G54S",
502 { .name = "reset", .gpio = 1 << 4 },
503 { .name = "bridge", .gpio = 1 << 5 },
504 { .name = "ses", .gpio = 1 << 0 },
507 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
508 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
509 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
510 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
511 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
515 .name = "Buffalo WBR2-G54",
518 { .name = "reset", .gpio = 1 << 7 },
521 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
525 .name = "Buffalo WHR-HP-G54",
527 { .name = "reset", .gpio = 1 << 4 },
528 { .name = "bridge", .gpio = 1 << 5 },
529 { .name = "ses", .gpio = 1 << 0 },
532 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
533 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
534 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
535 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
536 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
540 .name = "Buffalo WHR-G125",
542 { .name = "reset", .gpio = 1 << 4 },
543 { .name = "bridge", .gpio = 1 << 5 },
544 { .name = "ses", .gpio = 1 << 0 },
547 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
548 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
549 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
550 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
551 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
555 .name = "Buffalo WHR2-A54G54",
557 { .name = "reset", .gpio = 1 << 4 },
560 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
564 .name = "Buffalo WLA2-G54L",
567 { .name = "reset", .gpio = 1 << 7 },
570 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
574 .name = "Buffalo WZR-G300N",
576 { .name = "reset", .gpio = 1 << 4 },
579 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
580 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
581 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
585 .name = "Buffalo WZR-RS-G54",
587 { .name = "ses", .gpio = 1 << 0 },
588 { .name = "reset", .gpio = 1 << 4 },
591 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
592 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
593 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
597 .name = "Buffalo WZR-RS-G54HP",
599 { .name = "ses", .gpio = 1 << 0 },
600 { .name = "reset", .gpio = 1 << 4 },
603 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
604 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
605 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
608 [BUFFALO_UNKNOWN] = {
609 .name = "Buffalo (unknown)",
611 { .name = "reset", .gpio = 1 << 7 },
614 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
617 [BUFFALO_UNKNOWN_4710] = {
618 .name = "Buffalo (unknown, BCM4710)",
620 { .name = "reset", .gpio = 1 << 4 },
623 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
628 .name = "Siemens SE505 V1",
630 /* No usable buttons */
633 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
634 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
635 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
639 .name = "Siemens SE505 V2",
641 /* No usable buttons */
644 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
645 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
646 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
651 .name = "U.S. Robotics USR5461",
653 /* No usable buttons */
656 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
657 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
662 .name = "Dell TrueMobile 2300",
664 { .name = "reset", .gpio = 1 << 0 },
667 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
668 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
672 .name = "Dell TrueMobile 2300 v2",
674 { .name = "reset", .gpio = 1 << 0 },
677 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
678 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
683 .name = "Motorola WE800G",
685 { .name = "reset", .gpio = 1 << 0 },
688 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
689 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
690 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
694 .name = "Motorola WR850G V1",
696 { .name = "reset", .gpio = 1 << 0 },
699 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
700 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
701 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
702 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
703 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
707 .name = "Motorola WR850G V2/V3",
709 { .name = "reset", .gpio = 1 << 5 },
712 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
713 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
714 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
715 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
719 .name = "Motorola WR850GP",
721 { .name = "reset", .gpio = 1 << 5 },
724 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
725 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
726 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
727 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
733 .name = "Belkin (unknown)",
734 /* FIXME: verify & add detection */
736 { .name = "reset", .gpio = 1 << 7 },
739 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
740 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
741 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
746 .name = "Netgear WGT634U",
748 { .name = "reset", .gpio = 1 << 2 },
751 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
756 .name = "Trendware TEW411BRP+",
758 { /* No usable buttons */ },
761 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
762 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
763 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
768 .name = "SimpleTech SimpleShare NAS",
770 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
771 { .name = "power", .gpio = 1 << 0 }, // on back
774 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
776 .platform_init = bcm4780_init,
780 .name = "D-Link DIR-130",
782 { .name = "reset", .gpio = 1 << 3},
783 { .name = "reserved", .gpio = 1 << 7},
786 { .name = "diag", .gpio = 1 << 0},
787 { .name = "blue", .gpio = 1 << 6},
791 .name = "D-Link DIR-320",
793 { .name = "reserved", .gpio = 1 << 6},
794 { .name = "reset", .gpio = 1 << 7},
797 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
798 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
799 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
800 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
801 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
805 .name = "D-Link DIR-330",
807 { .name = "reset", .gpio = 1 << 3},
808 { .name = "reserved", .gpio = 1 << 7},
811 { .name = "diag", .gpio = 1 << 0},
812 { .name = "usb", .gpio = 1 << 4},
813 { .name = "blue", .gpio = 1 << 6},
817 .name = "D-Link DWL-3150",
819 { .name = "reset", .gpio = 1 << 7},
822 { .name = "diag", .gpio = 1 << 2},
823 { .name = "status", .gpio = 1 << 1},
828 .name = "Sitecom WL-105b",
830 { .name = "reset", .gpio = 1 << 10},
833 { .name = "wlan", .gpio = 1 << 4},
834 { .name = "power", .gpio = 1 << 3},
837 /* Western Digital Net Center */
839 .name = "Western Digital NetCenter",
841 { .name = "power", .gpio = 1 << 0},
842 { .name = "reset", .gpio = 1 << 7},
844 .platform_init = NetCenter_init,
846 /* Askey (and clones) */
848 .name = "Askey RT210W",
850 /* Power button is hard-wired to hardware reset */
851 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
852 { .name = "power", .gpio = 1 << 7},
855 /* These were verified and named based on Belkin F5D4230-4 v1112 */
856 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
857 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
858 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
862 .name = "OvisLink WL-1600GL",
864 { .name = "reset", .gpio = 1 << 3 },
865 { .name = "ses", .gpio = 1 << 4 },
868 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
869 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
870 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
871 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
876 .name = "Microsoft MN-700",
878 { .name = "reset", .gpio = 1 << 7 },
881 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
886 static struct platform_t __init *platform_detect(void)
888 char *boardnum, *boardtype, *buf;
890 if (strcmp(getvar("nvram_type"), "cfe") == 0)
891 return &platforms[WGT634U];
893 /* Look for a model identifier */
895 /* Based on "model_name" */
896 if ((buf = nvram_get("model_name"))) {
897 if (!strcmp(buf, "DIR-130"))
898 return &platforms[DIR130];
899 if (!strcmp(buf, "DIR-330"))
900 return &platforms[DIR330];
903 /* Based on "wsc_modelname */
904 if ((buf = nvram_get("wsc_modelname"))) {
905 if (!strcmp(buf, "WRT610N"))
906 return &platforms[WRT610N];
909 /* Based on "model_no" */
910 if ((buf = nvram_get("model_no"))) {
911 if (startswith(buf,"WL700")) /* WL700* */
912 return &platforms[WL700GE];
915 /* Based on "hardware_version" */
916 if ((buf = nvram_get("hardware_version"))) {
917 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
918 return &platforms[WL500GPV2];
919 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
920 return &platforms[WL520GC];
921 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
922 return &platforms[WL520GU];
923 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
924 return &platforms[WL330GE];
927 /* Based on "ModelId" */
928 if ((buf = nvram_get("ModelId"))) {
929 if (!strcmp(buf, "WR850GP"))
930 return &platforms[WR850GP];
931 if (!strcmp(buf, "WR850G"))
932 return &platforms[WR850GV2V3];
933 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
934 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
935 if (startswith(buf,"WE800G")) /* WE800G* */
936 return &platforms[WE800G];
940 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
941 /* Buffalo hardware, check id for specific hardware matches */
942 if (!strcmp(buf, "29bb0332"))
943 return &platforms[WBR2_G54];
944 if (!strcmp(buf, "29129"))
945 return &platforms[WLA2_G54L];
946 if (!strcmp(buf, "30189"))
947 return &platforms[WHR_HP_G54];
948 if (!strcmp(buf, "32093"))
949 return &platforms[WHR_G125];
950 if (!strcmp(buf, "30182"))
951 return &platforms[WHR_G54S];
952 if (!strcmp(buf, "290441dd"))
953 return &platforms[WHR2_A54G54];
954 if (!strcmp(buf, "31120"))
955 return &platforms[WZR_G300N];
956 if (!strcmp(buf, "30083"))
957 return &platforms[WZR_RS_G54];
958 if (!strcmp(buf, "30103"))
959 return &platforms[WZR_RS_G54HP];
962 /* no easy model number, attempt to guess */
963 boardnum = getvar("boardnum");
964 boardtype = getvar("boardtype");
966 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
967 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
968 return &platforms[WRT600NV11];
970 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
971 return &platforms[WRT600N];
974 if (startswith(getvar("pmon_ver"), "CFE")) {
975 /* CFE based - newer hardware */
976 if (!strcmp(boardnum, "42")) { /* Linksys */
977 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
978 return &platforms[WRT300NV11];
980 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
981 return &platforms[WRT350N];
983 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
984 return &platforms[WRT54G3G];
986 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
987 return &platforms[WRTSL54GS];
989 if (!strcmp(boardtype, "0x0472"))
990 return &platforms[WRT160N];
992 /* default to WRT54G */
993 return &platforms[WRT54G];
995 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
996 return &platforms[WAP54GV2];
998 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
999 return &platforms[WL1600GL];
1002 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1003 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1004 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1007 if (!strcmp(boardnum, "45")) { /* ASUS */
1008 if (!strcmp(boardtype,"0x042f"))
1009 return &platforms[WL500GP];
1010 else if (!strcmp(boardtype,"0x0472"))
1011 return &platforms[WL500W];
1012 else if (!strcmp(boardtype,"0x467"))
1013 return &platforms[WL320GE];
1015 return &platforms[WL500GD];
1018 if (!strcmp(boardnum, "10496"))
1019 return &platforms[USR5461];
1021 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1022 return &platforms[SE505V2];
1024 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1025 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1026 return &platforms[DIR320];
1028 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1029 return &platforms[WDNetCenter];
1032 } else { /* PMON based - old stuff */
1033 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1034 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1035 return &platforms[WR850GV1];
1037 if (startswith(boardtype, "bcm94710dev")) {
1038 if (!strcmp(boardnum, "42"))
1039 return &platforms[WRT54GV1];
1040 if (simple_strtoul(boardnum, NULL, 0) == 2)
1041 return &platforms[WAP54GV1];
1043 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1044 if (startswith(getvar("hardware_version"), "WL500-")) {
1045 if (!strcmp(getvar("boardnum"), "mn700"))
1046 return &platforms[MN700];
1048 return &platforms[WL500G];
1050 if (startswith(getvar("hardware_version"), "WL300-")) {
1051 /* Either WL-300g or WL-HDD, do more extensive checks */
1052 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1053 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1054 return &platforms[WLHDD];
1055 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1056 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1057 return &platforms[WL300G];
1059 /* Sitecom WL-105b */
1060 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1061 return &platforms[WL105B];
1063 /* unknown asus stuff, probably bcm4702 */
1064 if (startswith(boardnum, "asusX"))
1065 return &platforms[ASUS_4702];
1067 /* bcm4702 based Askey RT210W clones, Including:
1068 * Askey RT210W (duh?)
1070 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1072 if (!strcmp(boardtype,"bcm94710r4")
1073 && !strcmp(boardnum ,"100")
1074 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1076 return &platforms[RT210W];
1080 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1081 if (startswith(boardtype, "bcm94710ap"))
1082 return &platforms[BUFFALO_UNKNOWN_4710];
1084 return &platforms[BUFFALO_UNKNOWN];
1087 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1088 startswith(getvar("CFEver"), "MotoWRv3") ||
1089 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1091 return &platforms[WR850GV2V3];
1094 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1095 return &platforms[TEW411BRPP];
1098 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
1099 return &platforms[STI_NAS];
1101 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1102 return &platforms[DWL3150];
1108 static void register_buttons(struct button_t *b)
1110 for (; b->name; b++)
1111 platform.button_mask |= b->gpio;
1113 platform.button_mask &= ~gpiomask;
1115 gpio_outen(platform.button_mask, 0);
1116 gpio_control(platform.button_mask, 0);
1117 platform.button_polarity = gpio_in() & platform.button_mask;
1118 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1119 gpio_setintmask(platform.button_mask, platform.button_mask);
1121 gpio_set_irqenable(1, button_handler);
1124 static void unregister_buttons(struct button_t *b)
1126 gpio_setintmask(platform.button_mask, 0);
1128 gpio_set_irqenable(0, button_handler);
1133 static void add_msg(struct event_t *event, char *msg, int argv)
1140 s = skb_put(event->skb, strlen(msg) + 1);
1144 static void hotplug_button(struct work_struct *work)
1146 struct event_t *event = container_of(work, struct event_t, wq);
1152 event->skb = alloc_skb(2048, GFP_KERNEL);
1154 s = skb_put(event->skb, strlen(event->action) + 2);
1155 sprintf(s, "%s@", event->action);
1158 NETLINK_CB(event->skb).dst_group = 1;
1159 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1164 #else /* !LINUX_2_4 */
1165 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1169 p = kmalloc(size, gfp);
1178 static void add_msg(struct event_t *event, char *msg, int argv)
1181 event->argv[event->anr++] = event->scratch;
1183 event->envp[event->enr++] = event->scratch;
1185 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1188 static void hotplug_button(struct event_t *event)
1190 char *scratch = kzalloc(256, GFP_KERNEL);
1191 event->scratch = scratch;
1193 add_msg(event, hotplug_path, 1);
1194 add_msg(event, "button", 1);
1196 call_usermodehelper (event->argv[0], event->argv, event->envp);
1200 #endif /* !LINUX_2_4 */
1202 static int fill_event (struct event_t *event)
1204 static char buf[128];
1206 add_msg(event, "HOME=/", 0);
1207 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1208 add_msg(event, "SUBSYSTEM=button", 0);
1209 snprintf(buf, 128, "ACTION=%s", event->action);
1210 add_msg(event, buf, 0);
1211 snprintf(buf, 128, "BUTTON=%s", event->name);
1212 add_msg(event, buf, 0);
1213 snprintf(buf, 128, "SEEN=%ld", event->seen);
1214 add_msg(event, buf, 0);
1216 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1217 add_msg(event, buf, 0);
1225 static irqreturn_t button_handler(int irq, void *dev_id)
1227 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1233 in = gpio_in() & platform.button_mask;
1234 gpio_intpolarity(platform.button_mask, in);
1235 changed = platform.button_polarity ^ in;
1236 platform.button_polarity = in;
1238 changed &= ~gpio_outen(0, 0);
1240 for (b = platform.buttons; b->name; b++) {
1241 struct event_t *event;
1243 if (!(b->gpio & changed)) continue;
1247 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1248 event->seen = (jiffies - b->seen)/HZ;
1249 event->name = b->name;
1250 event->action = b->pressed ? "pressed" : "released";
1251 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1252 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1254 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1256 schedule_work(&event->wq);
1264 static void register_leds(struct led_t *l)
1266 struct proc_dir_entry *p;
1271 leds = proc_mkdir("led", diag);
1275 for(; l->name; l++) {
1276 if (l->gpio & gpiomask)
1279 if (l->gpio & GPIO_TYPE_EXTIF) {
1283 if (l->polarity != INPUT) oe_mask |= l->gpio;
1285 val |= (l->polarity == NORMAL)?0:l->gpio;
1288 if (l->polarity == INPUT) continue;
1290 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1291 l->proc.type = PROC_LED;
1293 p->data = (void *) &l->proc;
1294 p->proc_fops = &diag_proc_fops;
1298 gpio_outen(mask, oe_mask);
1299 gpio_control(mask, 0);
1300 gpio_out(mask, val);
1301 gpio_setintmask(mask, 0);
1304 static void unregister_leds(struct led_t *l)
1307 remove_proc_entry(l->name, leds);
1309 remove_proc_entry("led", diag);
1312 static void set_led_extif(struct led_t *led)
1314 gpio_set_extif(led->gpio, led->state);
1317 static void led_flash(unsigned long dummy) {
1322 for (l = platform.leds; l->name; l++) {
1324 if (l->gpio & GPIO_TYPE_EXTIF) {
1326 l->state = !l->state;
1336 u32 val = ~gpio_in();
1338 gpio_outen(mask, mask);
1339 gpio_control(mask, 0);
1340 gpio_out(mask, val);
1342 if (mask || extif_blink) {
1343 mod_timer(&led_timer, jiffies + FLASH_TIME);
1347 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1350 struct inode *inode = file->f_dentry->d_inode;
1351 struct proc_dir_entry *dent = inode->u.generic_ip;
1353 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1358 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1361 if (dent->data != NULL) {
1362 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1363 switch (handler->type) {
1365 struct led_t * led = (struct led_t *) handler->ptr;
1367 len = sprintf(page, "f\n");
1369 if (led->gpio & GPIO_TYPE_EXTIF) {
1370 len = sprintf(page, "%d\n", led->state);
1372 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1373 u8 p = (led->polarity == NORMAL ? 0 : 1);
1374 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1380 len = sprintf(page, "%s\n", platform.name);
1383 len = sprintf(page, "0x%04x\n", gpiomask);
1390 len = min_t(int, len - *ppos, count);
1391 if (copy_to_user(buf, (page + *ppos), len)) {
1405 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1408 struct inode *inode = file->f_dentry->d_inode;
1409 struct proc_dir_entry *dent = inode->u.generic_ip;
1411 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1416 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1419 if (copy_from_user(page, buf, count)) {
1425 if (dent->data != NULL) {
1426 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1427 switch (handler->type) {
1429 struct led_t *led = (struct led_t *) handler->ptr;
1430 int p = (led->polarity == NORMAL ? 0 : 1);
1432 if (page[0] == 'f') {
1437 if (led->gpio & GPIO_TYPE_EXTIF) {
1438 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1441 gpio_outen(led->gpio, led->gpio);
1442 gpio_control(led->gpio, 0);
1443 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1449 gpiomask = simple_strtoul(page, NULL, 0);
1451 if (platform.buttons) {
1452 unregister_buttons(platform.buttons);
1453 register_buttons(platform.buttons);
1456 if (platform.leds) {
1457 unregister_leds(platform.leds);
1458 register_leds(platform.leds);
1469 static int __init diag_init(void)
1471 static struct proc_dir_entry *p;
1472 static struct platform_t *detected;
1474 detected = platform_detect();
1476 printk(MODULE_NAME ": Router model not detected.\n");
1479 memcpy(&platform, detected, sizeof(struct platform_t));
1481 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1482 if (platform.platform_init != NULL) {
1483 platform.platform_init();
1486 if (!(diag = proc_mkdir("diag", NULL))) {
1487 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1491 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1492 p->data = (void *) &proc_model;
1493 p->proc_fops = &diag_proc_fops;
1496 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1497 p->data = (void *) &proc_gpiomask;
1498 p->proc_fops = &diag_proc_fops;
1501 if (platform.buttons)
1502 register_buttons(platform.buttons);
1505 register_leds(platform.leds);
1510 static void __exit diag_exit(void)
1512 del_timer(&led_timer);
1514 if (platform.buttons)
1515 unregister_buttons(platform.buttons);
1518 unregister_leds(platform.leds);
1520 remove_proc_entry("model", diag);
1521 remove_proc_entry("gpiomask", diag);
1522 remove_proc_entry("diag", NULL);
1525 module_init(diag_init);
1526 module_exit(diag_exit);
1528 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1529 MODULE_LICENSE("GPL");