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);
140 static void __init bcm4780_init(void) {
143 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
144 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
145 gpio_outen(pin, pin);
146 gpio_control(pin, 0);
149 /* Wait 5s, so the HDD can spin up */
150 set_current_state(TASK_INTERRUPTIBLE);
151 schedule_timeout(HZ * 5);
154 static void __init bcm57xx_init(void) {
158 /* FIXME: switch comes up, but port mappings/vlans not right */
159 gpio_outen(pin, pin);
160 gpio_control(pin, 0);
165 static struct platform_t __initdata platforms[] = {
168 .name = "Linksys WAP54G V1",
170 { .name = "reset", .gpio = 1 << 0 },
173 { .name = "diag", .gpio = 1 << 3 },
174 { .name = "wlan", .gpio = 1 << 4 },
178 .name = "Linksys WAP54G V2",
180 { .name = "reset", .gpio = 1 << 0 },
183 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
184 /* GPIO 6 is b44 (eth0, LAN) PHY power */
188 .name = "Linksys WAP54G V3",
190 /* FIXME: verify this */
191 { .name = "reset", .gpio = 1 << 7 },
192 { .name = "ses", .gpio = 1 << 0 },
196 { .name = "ses", .gpio = 1 << 1 },
200 .name = "Linksys WRT54G V1.x",
202 { .name = "reset", .gpio = 1 << 6 },
205 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
206 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
210 .name = "Linksys WRT54G/GS/GL",
212 { .name = "reset", .gpio = 1 << 6 },
213 { .name = "ses", .gpio = 1 << 4 },
216 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
217 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
218 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
219 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
220 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
224 .name = "Linksys WRTSL54GS",
226 { .name = "reset", .gpio = 1 << 6 },
227 { .name = "ses", .gpio = 1 << 4 },
230 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
231 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
232 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
233 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
237 .name = "Linksys WRT54G3G",
239 { .name = "reset", .gpio = 1 << 6 },
240 { .name = "3g", .gpio = 1 << 4 },
243 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
244 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
245 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
246 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
247 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
251 .name = "Linksys WRT160N",
253 { .name = "reset", .gpio = 1 << 6 },
254 { .name = "ses", .gpio = 1 << 4 },
257 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
258 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
259 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
263 .name = "Linksys WRT300N V1.1",
265 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
266 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
269 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
270 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
271 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
273 .platform_init = bcm57xx_init,
276 .name = "Linksys WRT350N",
278 { .name = "reset", .gpio = 1 << 6 },
279 { .name = "ses", .gpio = 1 << 8 },
282 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
283 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
284 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
285 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
286 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
288 .platform_init = bcm57xx_init,
291 .name = "Linksys WRT600N",
293 { .name = "reset", .gpio = 1 << 6 },
294 { .name = "ses", .gpio = 1 << 7 },
297 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
298 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
299 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
300 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
301 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
302 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
304 .platform_init = bcm57xx_init,
307 .name = "Linksys WRT600N V1.1",
309 { .name = "reset", .gpio = 1 << 6 },
310 { .name = "ses", .gpio = 1 << 7 },
313 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
314 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
315 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
316 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
317 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
318 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
320 .platform_init = bcm57xx_init,
323 .name = "Linksys WRT610N",
325 { .name = "reset", .gpio = 1 << 6 },
326 { .name = "ses", .gpio = 1 << 8 },
329 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
330 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
331 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
332 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
337 .name = "ASUS WL-HDD",
339 { .name = "reset", .gpio = 1 << 6 },
342 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
343 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
347 .name = "ASUS WL-300g",
349 { .name = "reset", .gpio = 1 << 6 },
352 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
356 .name = "ASUS WL-320gE/WL-320gP",
358 { .name = "reset", .gpio = 1 << 6 },
361 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
362 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
363 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
367 .name = "ASUS WL-330gE",
369 { .name = "reset", .gpio = 1 << 2 },
372 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
376 .name = "ASUS WL-500g",
378 { .name = "reset", .gpio = 1 << 6 },
381 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
385 .name = "ASUS WL-500g Deluxe",
387 { .name = "reset", .gpio = 1 << 6 },
390 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
394 .name = "ASUS WL-500g Premium",
396 { .name = "reset", .gpio = 1 << 0 },
397 { .name = "ses", .gpio = 1 << 4 },
400 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
404 .name = "ASUS WL-500g Premium V2",
406 { .name = "reset", .gpio = 1 << 2 },
407 { .name = "ses", .gpio = 1 << 3 },
410 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
411 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
415 .name = "ASUS WL-500W",
417 { .name = "reset", .gpio = 1 << 6 },
418 { .name = "ses", .gpio = 1 << 7 },
421 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
425 .name = "ASUS WL-520GC",
427 { .name = "reset", .gpio = 1 << 2 },
428 { .name = "ses", .gpio = 1 << 3 },
431 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
432 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
436 .name = "ASUS WL-520gU",
438 { .name = "reset", .gpio = 1 << 2 },
439 { .name = "ses", .gpio = 1 << 3 },
442 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
443 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
447 .name = "ASUS (unknown, BCM4702)",
449 { .name = "reset", .gpio = 1 << 6 },
452 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
456 .name = "ASUS WL-700gE",
458 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
459 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
460 { .name = "power", .gpio = 1 << 0 }, // on front
461 { .name = "copy", .gpio = 1 << 6 }, // on front
465 // GPIO that controls power led also enables/disables some essential functions
468 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
470 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
472 .platform_init = bcm4780_init,
476 .name = "Buffalo WHR-G54S",
478 { .name = "reset", .gpio = 1 << 4 },
479 { .name = "bridge", .gpio = 1 << 5 },
480 { .name = "ses", .gpio = 1 << 0 },
483 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
484 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
485 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
486 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
487 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
491 .name = "Buffalo WBR2-G54",
494 { .name = "reset", .gpio = 1 << 7 },
497 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
501 .name = "Buffalo WHR-HP-G54",
503 { .name = "reset", .gpio = 1 << 4 },
504 { .name = "bridge", .gpio = 1 << 5 },
505 { .name = "ses", .gpio = 1 << 0 },
508 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
509 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
510 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
511 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
512 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
516 .name = "Buffalo WHR-G125",
518 { .name = "reset", .gpio = 1 << 4 },
519 { .name = "bridge", .gpio = 1 << 5 },
520 { .name = "ses", .gpio = 1 << 0 },
523 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
524 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
525 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
526 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
527 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
531 .name = "Buffalo WHR2-A54G54",
533 { .name = "reset", .gpio = 1 << 4 },
536 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
540 .name = "Buffalo WLA2-G54L",
543 { .name = "reset", .gpio = 1 << 7 },
546 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
550 .name = "Buffalo WZR-G300N",
552 { .name = "reset", .gpio = 1 << 4 },
555 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
556 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
557 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
561 .name = "Buffalo WZR-RS-G54",
563 { .name = "ses", .gpio = 1 << 0 },
564 { .name = "reset", .gpio = 1 << 4 },
567 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
568 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
569 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
573 .name = "Buffalo WZR-RS-G54HP",
575 { .name = "ses", .gpio = 1 << 0 },
576 { .name = "reset", .gpio = 1 << 4 },
579 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
580 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
581 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
584 [BUFFALO_UNKNOWN] = {
585 .name = "Buffalo (unknown)",
587 { .name = "reset", .gpio = 1 << 7 },
590 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
593 [BUFFALO_UNKNOWN_4710] = {
594 .name = "Buffalo (unknown, BCM4710)",
596 { .name = "reset", .gpio = 1 << 4 },
599 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
604 .name = "Siemens SE505 V1",
606 /* No usable buttons */
609 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
610 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
611 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
615 .name = "Siemens SE505 V2",
617 /* No usable buttons */
620 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
621 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
622 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
627 .name = "U.S. Robotics USR5461",
629 /* No usable buttons */
632 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
633 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
638 .name = "Dell TrueMobile 2300",
640 { .name = "reset", .gpio = 1 << 0 },
643 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
644 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
648 .name = "Dell TrueMobile 2300 v2",
650 { .name = "reset", .gpio = 1 << 0 },
653 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
654 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
659 .name = "Motorola WE800G",
661 { .name = "reset", .gpio = 1 << 0 },
664 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
665 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
666 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
670 .name = "Motorola WR850G V1",
672 { .name = "reset", .gpio = 1 << 0 },
675 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
676 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
677 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
678 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
679 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
683 .name = "Motorola WR850G V2/V3",
685 { .name = "reset", .gpio = 1 << 5 },
688 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
689 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
690 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
691 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
695 .name = "Motorola WR850GP",
697 { .name = "reset", .gpio = 1 << 5 },
700 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
701 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
702 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
703 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
709 .name = "Belkin (unknown)",
710 /* FIXME: verify & add detection */
712 { .name = "reset", .gpio = 1 << 7 },
715 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
716 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
717 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
722 .name = "Netgear WGT634U",
724 { .name = "reset", .gpio = 1 << 2 },
727 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
732 .name = "Trendware TEW411BRP+",
734 { /* No usable buttons */ },
737 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
738 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
739 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
744 .name = "SimpleTech SimpleShare NAS",
746 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
747 { .name = "power", .gpio = 1 << 0 }, // on back
750 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
752 .platform_init = bcm4780_init,
756 .name = "D-Link DIR-130",
758 { .name = "reset", .gpio = 1 << 3},
759 { .name = "reserved", .gpio = 1 << 7},
762 { .name = "diag", .gpio = 1 << 0},
763 { .name = "blue", .gpio = 1 << 6},
767 .name = "D-Link DIR-320",
769 { .name = "reserved", .gpio = 1 << 6},
770 { .name = "reset", .gpio = 1 << 7},
773 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
774 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
775 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
776 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
777 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
781 .name = "D-Link DIR-330",
783 { .name = "reset", .gpio = 1 << 3},
784 { .name = "reserved", .gpio = 1 << 7},
787 { .name = "diag", .gpio = 1 << 0},
788 { .name = "usb", .gpio = 1 << 4},
789 { .name = "blue", .gpio = 1 << 6},
793 .name = "D-Link DWL-3150",
795 { .name = "reset", .gpio = 1 << 7},
798 { .name = "diag", .gpio = 1 << 2},
799 { .name = "status", .gpio = 1 << 1},
804 .name = "Sitecom WL-105b",
806 { .name = "reset", .gpio = 1 << 10},
809 { .name = "wlan", .gpio = 1 << 4},
810 { .name = "power", .gpio = 1 << 3},
813 /* Askey (and clones) */
815 .name = "Askey RT210W",
817 /* Power button is hard-wired to hardware reset */
818 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
819 { .name = "power", .gpio = 1 << 7},
822 /* These were verified and named based on Belkin F5D4230-4 v1112 */
823 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
824 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
825 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
830 static struct platform_t __init *platform_detect(void)
832 char *boardnum, *boardtype, *buf;
834 if (strcmp(getvar("nvram_type"), "cfe") == 0)
835 return &platforms[WGT634U];
837 /* Look for a model identifier */
839 /* Based on "model_name" */
840 if ((buf = nvram_get("model_name"))) {
841 if (!strcmp(buf, "DIR-130"))
842 return &platforms[DIR130];
843 if (!strcmp(buf, "DIR-330"))
844 return &platforms[DIR330];
847 /* Based on "wsc_modelname */
848 if ((buf = nvram_get("wsc_modelname"))) {
849 if (!strcmp(buf, "WRT610N"))
850 return &platforms[WRT610N];
853 /* Based on "model_no" */
854 if ((buf = nvram_get("model_no"))) {
855 if (startswith(buf,"WL700")) /* WL700* */
856 return &platforms[WL700GE];
859 /* Based on "hardware_version" */
860 if ((buf = nvram_get("hardware_version"))) {
861 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
862 return &platforms[WL500GPV2];
863 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
864 return &platforms[WL520GC];
865 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
866 return &platforms[WL520GU];
867 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
868 return &platforms[WL330GE];
871 /* Based on "ModelId" */
872 if ((buf = nvram_get("ModelId"))) {
873 if (!strcmp(buf, "WR850GP"))
874 return &platforms[WR850GP];
875 if (!strcmp(buf, "WR850G"))
876 return &platforms[WR850GV2V3];
877 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
878 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
879 if (startswith(buf,"WE800G")) /* WE800G* */
880 return &platforms[WE800G];
884 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
885 /* Buffalo hardware, check id for specific hardware matches */
886 if (!strcmp(buf, "29bb0332"))
887 return &platforms[WBR2_G54];
888 if (!strcmp(buf, "29129"))
889 return &platforms[WLA2_G54L];
890 if (!strcmp(buf, "30189"))
891 return &platforms[WHR_HP_G54];
892 if (!strcmp(buf, "32093"))
893 return &platforms[WHR_G125];
894 if (!strcmp(buf, "30182"))
895 return &platforms[WHR_G54S];
896 if (!strcmp(buf, "290441dd"))
897 return &platforms[WHR2_A54G54];
898 if (!strcmp(buf, "31120"))
899 return &platforms[WZR_G300N];
900 if (!strcmp(buf, "30083"))
901 return &platforms[WZR_RS_G54];
902 if (!strcmp(buf, "30103"))
903 return &platforms[WZR_RS_G54HP];
906 /* no easy model number, attempt to guess */
907 boardnum = getvar("boardnum");
908 boardtype = getvar("boardtype");
910 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
911 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
912 return &platforms[WRT600NV11];
914 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
915 return &platforms[WRT600N];
918 if (startswith(getvar("pmon_ver"), "CFE")) {
919 /* CFE based - newer hardware */
920 if (!strcmp(boardnum, "42")) { /* Linksys */
921 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
922 return &platforms[WRT300NV11];
924 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
925 return &platforms[WRT350N];
927 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
928 return &platforms[WRT54G3G];
930 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
931 return &platforms[WRTSL54GS];
933 if (!strcmp(boardtype, "0x0472"))
934 return &platforms[WRT160N];
936 /* default to WRT54G */
937 return &platforms[WRT54G];
939 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
940 return &platforms[WAP54GV2];
942 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
943 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
944 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
947 if (!strcmp(boardnum, "45")) { /* ASUS */
948 if (!strcmp(boardtype,"0x042f"))
949 return &platforms[WL500GP];
950 else if (!strcmp(boardtype,"0x0472"))
951 return &platforms[WL500W];
952 else if (!strcmp(boardtype,"0x467"))
953 return &platforms[WL320GE];
955 return &platforms[WL500GD];
958 if (!strcmp(boardnum, "10496"))
959 return &platforms[USR5461];
961 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
962 return &platforms[SE505V2];
964 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
965 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
966 return &platforms[DIR320];
968 } else { /* PMON based - old stuff */
969 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
970 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
971 return &platforms[WR850GV1];
973 if (startswith(boardtype, "bcm94710dev")) {
974 if (!strcmp(boardnum, "42"))
975 return &platforms[WRT54GV1];
976 if (simple_strtoul(boardnum, NULL, 0) == 2)
977 return &platforms[WAP54GV1];
979 if (startswith(getvar("hardware_version"), "WL500-"))
980 return &platforms[WL500G];
981 if (startswith(getvar("hardware_version"), "WL300-")) {
982 /* Either WL-300g or WL-HDD, do more extensive checks */
983 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
984 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
985 return &platforms[WLHDD];
986 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
987 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
988 return &platforms[WL300G];
990 /* Sitecom WL-105b */
991 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
992 return &platforms[WL105B];
994 /* unknown asus stuff, probably bcm4702 */
995 if (startswith(boardnum, "asusX"))
996 return &platforms[ASUS_4702];
998 /* bcm4702 based Askey RT210W clones, Including:
999 * Askey RT210W (duh?)
1001 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1003 if (!strcmp(boardtype,"bcm94710r4")
1004 && !strcmp(boardnum ,"100")
1005 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1007 return &platforms[RT210W];
1011 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1012 if (startswith(boardtype, "bcm94710ap"))
1013 return &platforms[BUFFALO_UNKNOWN_4710];
1015 return &platforms[BUFFALO_UNKNOWN];
1018 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1019 startswith(getvar("CFEver"), "MotoWRv3") ||
1020 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1022 return &platforms[WR850GV2V3];
1025 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1026 return &platforms[TEW411BRPP];
1029 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
1030 return &platforms[STI_NAS];
1032 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1033 return &platforms[DWL3150];
1039 static void register_buttons(struct button_t *b)
1041 for (; b->name; b++)
1042 platform.button_mask |= b->gpio;
1044 platform.button_mask &= ~gpiomask;
1046 gpio_outen(platform.button_mask, 0);
1047 gpio_control(platform.button_mask, 0);
1048 platform.button_polarity = gpio_in() & platform.button_mask;
1049 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1050 gpio_setintmask(platform.button_mask, platform.button_mask);
1052 gpio_set_irqenable(1, button_handler);
1055 static void unregister_buttons(struct button_t *b)
1057 gpio_setintmask(platform.button_mask, 0);
1059 gpio_set_irqenable(0, button_handler);
1064 static void add_msg(struct event_t *event, char *msg, int argv)
1071 s = skb_put(event->skb, strlen(msg) + 1);
1075 static void hotplug_button(struct work_struct *work)
1077 struct event_t *event = container_of(work, struct event_t, wq);
1083 event->skb = alloc_skb(2048, GFP_KERNEL);
1085 s = skb_put(event->skb, strlen(event->action) + 2);
1086 sprintf(s, "%s@", event->action);
1089 NETLINK_CB(event->skb).dst_group = 1;
1090 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1095 #else /* !LINUX_2_4 */
1096 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1100 p = kmalloc(size, gfp);
1109 static void add_msg(struct event_t *event, char *msg, int argv)
1112 event->argv[event->anr++] = event->scratch;
1114 event->envp[event->enr++] = event->scratch;
1116 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1119 static void hotplug_button(struct event_t *event)
1121 char *scratch = kzalloc(256, GFP_KERNEL);
1122 event->scratch = scratch;
1124 add_msg(event, hotplug_path, 1);
1125 add_msg(event, "button", 1);
1127 call_usermodehelper (event->argv[0], event->argv, event->envp);
1131 #endif /* !LINUX_2_4 */
1133 static int fill_event (struct event_t *event)
1135 static char buf[128];
1137 add_msg(event, "HOME=/", 0);
1138 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1139 add_msg(event, "SUBSYSTEM=button", 0);
1140 snprintf(buf, 128, "ACTION=%s", event->action);
1141 add_msg(event, buf, 0);
1142 snprintf(buf, 128, "BUTTON=%s", event->name);
1143 add_msg(event, buf, 0);
1144 snprintf(buf, 128, "SEEN=%ld", event->seen);
1145 add_msg(event, buf, 0);
1147 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1148 add_msg(event, buf, 0);
1156 static irqreturn_t button_handler(int irq, void *dev_id)
1158 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1164 in = gpio_in() & platform.button_mask;
1165 gpio_intpolarity(platform.button_mask, in);
1166 changed = platform.button_polarity ^ in;
1167 platform.button_polarity = in;
1169 changed &= ~gpio_outen(0, 0);
1171 for (b = platform.buttons; b->name; b++) {
1172 struct event_t *event;
1174 if (!(b->gpio & changed)) continue;
1178 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1179 event->seen = (jiffies - b->seen)/HZ;
1180 event->name = b->name;
1181 event->action = b->pressed ? "pressed" : "released";
1182 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1183 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1185 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1187 schedule_work(&event->wq);
1195 static void register_leds(struct led_t *l)
1197 struct proc_dir_entry *p;
1202 leds = proc_mkdir("led", diag);
1206 for(; l->name; l++) {
1207 if (l->gpio & gpiomask)
1210 if (l->gpio & GPIO_TYPE_EXTIF) {
1214 if (l->polarity != INPUT) oe_mask |= l->gpio;
1216 val |= (l->polarity == NORMAL)?0:l->gpio;
1219 if (l->polarity == INPUT) continue;
1221 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1222 l->proc.type = PROC_LED;
1224 p->data = (void *) &l->proc;
1225 p->proc_fops = &diag_proc_fops;
1229 gpio_outen(mask, oe_mask);
1230 gpio_control(mask, 0);
1231 gpio_out(mask, val);
1232 gpio_setintmask(mask, 0);
1235 static void unregister_leds(struct led_t *l)
1238 remove_proc_entry(l->name, leds);
1240 remove_proc_entry("led", diag);
1243 static void set_led_extif(struct led_t *led)
1245 gpio_set_extif(led->gpio, led->state);
1248 static void led_flash(unsigned long dummy) {
1253 for (l = platform.leds; l->name; l++) {
1255 if (l->gpio & GPIO_TYPE_EXTIF) {
1257 l->state = !l->state;
1267 u32 val = ~gpio_in();
1269 gpio_outen(mask, mask);
1270 gpio_control(mask, 0);
1271 gpio_out(mask, val);
1273 if (mask || extif_blink) {
1274 mod_timer(&led_timer, jiffies + FLASH_TIME);
1278 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1281 struct inode *inode = file->f_dentry->d_inode;
1282 struct proc_dir_entry *dent = inode->u.generic_ip;
1284 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1289 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1292 if (dent->data != NULL) {
1293 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1294 switch (handler->type) {
1296 struct led_t * led = (struct led_t *) handler->ptr;
1298 len = sprintf(page, "f\n");
1300 if (led->gpio & GPIO_TYPE_EXTIF) {
1301 len = sprintf(page, "%d\n", led->state);
1303 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1304 u8 p = (led->polarity == NORMAL ? 0 : 1);
1305 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1311 len = sprintf(page, "%s\n", platform.name);
1314 len = sprintf(page, "0x%04x\n", gpiomask);
1321 len = min_t(int, len - *ppos, count);
1322 if (copy_to_user(buf, (page + *ppos), len)) {
1336 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1339 struct inode *inode = file->f_dentry->d_inode;
1340 struct proc_dir_entry *dent = inode->u.generic_ip;
1342 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1347 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1350 if (copy_from_user(page, buf, count)) {
1356 if (dent->data != NULL) {
1357 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1358 switch (handler->type) {
1360 struct led_t *led = (struct led_t *) handler->ptr;
1361 int p = (led->polarity == NORMAL ? 0 : 1);
1363 if (page[0] == 'f') {
1368 if (led->gpio & GPIO_TYPE_EXTIF) {
1369 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1372 gpio_outen(led->gpio, led->gpio);
1373 gpio_control(led->gpio, 0);
1374 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1380 gpiomask = simple_strtoul(page, NULL, 0);
1382 if (platform.buttons) {
1383 unregister_buttons(platform.buttons);
1384 register_buttons(platform.buttons);
1387 if (platform.leds) {
1388 unregister_leds(platform.leds);
1389 register_leds(platform.leds);
1400 static int __init diag_init(void)
1402 static struct proc_dir_entry *p;
1403 static struct platform_t *detected;
1405 detected = platform_detect();
1407 printk(MODULE_NAME ": Router model not detected.\n");
1410 memcpy(&platform, detected, sizeof(struct platform_t));
1412 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1413 if (platform.platform_init != NULL) {
1414 platform.platform_init();
1417 if (!(diag = proc_mkdir("diag", NULL))) {
1418 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1422 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1423 p->data = (void *) &proc_model;
1424 p->proc_fops = &diag_proc_fops;
1427 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1428 p->data = (void *) &proc_gpiomask;
1429 p->proc_fops = &diag_proc_fops;
1432 if (platform.buttons)
1433 register_buttons(platform.buttons);
1436 register_leds(platform.leds);
1441 static void __exit diag_exit(void)
1443 del_timer(&led_timer);
1445 if (platform.buttons)
1446 unregister_buttons(platform.buttons);
1449 unregister_leds(platform.leds);
1451 remove_proc_entry("model", diag);
1452 remove_proc_entry("gpiomask", diag);
1453 remove_proc_entry("diag", NULL);
1456 module_init(diag_init);
1457 module_exit(diag_exit);
1459 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1460 MODULE_LICENSE("GPL");