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 static void __init bcm4780_init(void) {
139 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
140 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
141 gpio_outen(pin, pin);
142 gpio_control(pin, 0);
145 /* Wait 5s, so the HDD can spin up */
146 set_current_state(TASK_INTERRUPTIBLE);
147 schedule_timeout(HZ * 5);
150 static void __init bcm57xx_init(void) {
154 /* FIXME: switch comes up, but port mappings/vlans not right */
155 gpio_outen(pin, pin);
156 gpio_control(pin, 0);
161 static struct platform_t __initdata platforms[] = {
164 .name = "Linksys WAP54G V1",
166 { .name = "reset", .gpio = 1 << 0 },
169 { .name = "diag", .gpio = 1 << 3 },
170 { .name = "wlan", .gpio = 1 << 4 },
174 .name = "Linksys WAP54G V2",
176 { .name = "reset", .gpio = 1 << 0 },
179 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
180 /* GPIO 6 is b44 (eth0, LAN) PHY power */
184 .name = "Linksys WAP54G V3",
186 /* FIXME: verify this */
187 { .name = "reset", .gpio = 1 << 7 },
188 { .name = "ses", .gpio = 1 << 0 },
192 { .name = "ses", .gpio = 1 << 1 },
196 .name = "Linksys WRT54G V1.x",
198 { .name = "reset", .gpio = 1 << 6 },
201 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
202 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
206 .name = "Linksys WRT54G/GS/GL",
208 { .name = "reset", .gpio = 1 << 6 },
209 { .name = "ses", .gpio = 1 << 4 },
212 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
213 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
214 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
215 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
216 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
220 .name = "Linksys WRTSL54GS",
222 { .name = "reset", .gpio = 1 << 6 },
223 { .name = "ses", .gpio = 1 << 4 },
226 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
227 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
228 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
229 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
233 .name = "Linksys WRT54G3G",
235 { .name = "reset", .gpio = 1 << 6 },
236 { .name = "3g", .gpio = 1 << 4 },
239 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
240 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
241 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
242 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
243 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
247 .name = "Linksys WRT160N",
249 { .name = "reset", .gpio = 1 << 6 },
250 { .name = "ses", .gpio = 1 << 4 },
253 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
254 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
255 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
259 .name = "Linksys WRT350N",
261 { .name = "reset", .gpio = 1 << 6 },
262 { .name = "ses", .gpio = 1 << 8 },
265 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
266 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
267 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
268 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
269 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
271 .platform_init = bcm57xx_init,
274 .name = "Linksys WRT600N",
276 { .name = "reset", .gpio = 1 << 6 },
277 { .name = "ses", .gpio = 1 << 7 },
280 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
281 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
282 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
283 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
284 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
285 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
287 .platform_init = bcm57xx_init,
290 .name = "Linksys WRT600N V1.1",
292 { .name = "reset", .gpio = 1 << 6 },
293 { .name = "ses", .gpio = 1 << 7 },
296 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
297 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
298 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
299 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
300 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
301 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
303 .platform_init = bcm57xx_init,
306 .name = "Linksys WRT610N",
308 { .name = "reset", .gpio = 1 << 6 },
309 { .name = "ses", .gpio = 1 << 8 },
312 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
313 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
314 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
315 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
320 .name = "ASUS WL-HDD",
322 { .name = "reset", .gpio = 1 << 6 },
325 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
326 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
330 .name = "ASUS WL-300g",
332 { .name = "reset", .gpio = 1 << 6 },
335 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
339 .name = "ASUS WL-320gE/WL-320gP",
341 { .name = "reset", .gpio = 1 << 6 },
344 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
345 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
346 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
350 .name = "ASUS WL-330gE",
352 { .name = "reset", .gpio = 1 << 2 },
355 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
359 .name = "ASUS WL-500g",
361 { .name = "reset", .gpio = 1 << 6 },
364 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
368 .name = "ASUS WL-500g Deluxe",
370 { .name = "reset", .gpio = 1 << 6 },
373 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
377 .name = "ASUS WL-500g Premium",
379 { .name = "reset", .gpio = 1 << 0 },
380 { .name = "ses", .gpio = 1 << 4 },
383 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
387 .name = "ASUS WL-500g Premium V2",
389 { .name = "reset", .gpio = 1 << 2 },
390 { .name = "ses", .gpio = 1 << 3 },
393 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
394 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
398 .name = "ASUS WL-500W",
400 { .name = "reset", .gpio = 1 << 6 },
401 { .name = "ses", .gpio = 1 << 7 },
404 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
408 .name = "ASUS WL-520GC",
410 { .name = "reset", .gpio = 1 << 2 },
411 { .name = "ses", .gpio = 1 << 3 },
414 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
415 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
419 .name = "ASUS WL-520gU",
421 { .name = "reset", .gpio = 1 << 2 },
422 { .name = "ses", .gpio = 1 << 3 },
425 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
426 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
430 .name = "ASUS (unknown, BCM4702)",
432 { .name = "reset", .gpio = 1 << 6 },
435 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
439 .name = "ASUS WL-700gE",
441 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
442 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
443 { .name = "power", .gpio = 1 << 0 }, // on front
444 { .name = "copy", .gpio = 1 << 6 }, // on front
448 // GPIO that controls power led also enables/disables some essential functions
451 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
453 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
455 .platform_init = bcm4780_init,
459 .name = "Buffalo WHR-G54S",
461 { .name = "reset", .gpio = 1 << 4 },
462 { .name = "bridge", .gpio = 1 << 5 },
463 { .name = "ses", .gpio = 1 << 0 },
466 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
467 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
468 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
469 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
470 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
474 .name = "Buffalo WBR2-G54",
477 { .name = "reset", .gpio = 1 << 7 },
480 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
484 .name = "Buffalo WHR-HP-G54",
486 { .name = "reset", .gpio = 1 << 4 },
487 { .name = "bridge", .gpio = 1 << 5 },
488 { .name = "ses", .gpio = 1 << 0 },
491 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
492 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
493 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
494 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
495 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
499 .name = "Buffalo WHR-G125",
501 { .name = "reset", .gpio = 1 << 4 },
502 { .name = "bridge", .gpio = 1 << 5 },
503 { .name = "ses", .gpio = 1 << 0 },
506 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
507 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
508 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
509 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
510 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
514 .name = "Buffalo WHR2-A54G54",
516 { .name = "reset", .gpio = 1 << 4 },
519 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
523 .name = "Buffalo WLA2-G54L",
526 { .name = "reset", .gpio = 1 << 7 },
529 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
533 .name = "Buffalo WZR-G300N",
535 { .name = "reset", .gpio = 1 << 4 },
538 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
539 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
540 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
544 .name = "Buffalo WZR-RS-G54",
546 { .name = "ses", .gpio = 1 << 0 },
547 { .name = "reset", .gpio = 1 << 4 },
550 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
551 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
552 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
556 .name = "Buffalo WZR-RS-G54HP",
558 { .name = "ses", .gpio = 1 << 0 },
559 { .name = "reset", .gpio = 1 << 4 },
562 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
563 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
564 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
567 [BUFFALO_UNKNOWN] = {
568 .name = "Buffalo (unknown)",
570 { .name = "reset", .gpio = 1 << 7 },
573 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
576 [BUFFALO_UNKNOWN_4710] = {
577 .name = "Buffalo (unknown, BCM4710)",
579 { .name = "reset", .gpio = 1 << 4 },
582 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
587 .name = "Siemens SE505 V1",
589 /* No usable buttons */
592 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
593 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
594 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
598 .name = "Siemens SE505 V2",
600 /* No usable buttons */
603 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
604 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
605 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
610 .name = "U.S. Robotics USR5461",
612 /* No usable buttons */
615 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
616 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
621 .name = "Dell TrueMobile 2300",
623 { .name = "reset", .gpio = 1 << 0 },
626 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
627 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
631 .name = "Dell TrueMobile 2300 v2",
633 { .name = "reset", .gpio = 1 << 0 },
636 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
637 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
642 .name = "Motorola WE800G",
644 { .name = "reset", .gpio = 1 << 0 },
647 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
648 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
649 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
653 .name = "Motorola WR850G V1",
655 { .name = "reset", .gpio = 1 << 0 },
658 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
659 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
660 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
661 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
662 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
666 .name = "Motorola WR850G V2/V3",
668 { .name = "reset", .gpio = 1 << 5 },
671 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
672 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
673 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
674 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
678 .name = "Motorola WR850GP",
680 { .name = "reset", .gpio = 1 << 5 },
683 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
684 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
685 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
686 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
692 .name = "Belkin (unknown)",
693 /* FIXME: verify & add detection */
695 { .name = "reset", .gpio = 1 << 7 },
698 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
699 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
700 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
705 .name = "Netgear WGT634U",
707 { .name = "reset", .gpio = 1 << 2 },
710 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
715 .name = "Trendware TEW411BRP+",
717 { /* No usable buttons */ },
720 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
721 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
722 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
727 .name = "SimpleTech SimpleShare NAS",
729 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
730 { .name = "power", .gpio = 1 << 0 }, // on back
733 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
735 .platform_init = bcm4780_init,
739 .name = "D-Link DIR-130",
741 { .name = "reset", .gpio = 1 << 3},
742 { .name = "reserved", .gpio = 1 << 7},
745 { .name = "diag", .gpio = 1 << 0},
746 { .name = "blue", .gpio = 1 << 6},
750 .name = "D-Link DIR-320",
752 { .name = "reserved", .gpio = 1 << 6},
753 { .name = "reset", .gpio = 1 << 7},
756 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
757 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
758 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
759 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
760 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
764 .name = "D-Link DIR-330",
766 { .name = "reset", .gpio = 1 << 3},
767 { .name = "reserved", .gpio = 1 << 7},
770 { .name = "diag", .gpio = 1 << 0},
771 { .name = "usb", .gpio = 1 << 4},
772 { .name = "blue", .gpio = 1 << 6},
776 .name = "D-Link DWL-3150",
778 { .name = "reset", .gpio = 1 << 7},
781 { .name = "diag", .gpio = 1 << 2},
782 { .name = "status", .gpio = 1 << 1},
787 .name = "Sitecom WL-105b",
789 { .name = "reset", .gpio = 1 << 10},
792 { .name = "wlan", .gpio = 1 << 4},
793 { .name = "power", .gpio = 1 << 3},
798 static struct platform_t __init *platform_detect(void)
800 char *boardnum, *boardtype, *buf;
802 if (strcmp(getvar("nvram_type"), "cfe") == 0)
803 return &platforms[WGT634U];
805 /* Look for a model identifier */
807 /* Based on "model_name" */
808 if ((buf = nvram_get("model_name"))) {
809 if (!strcmp(buf, "DIR-130"))
810 return &platforms[DIR130];
811 if (!strcmp(buf, "DIR-330"))
812 return &platforms[DIR330];
815 /* Based on "wsc_modelname */
816 if ((buf = nvram_get("wsc_modelname"))) {
817 if (!strcmp(buf, "WRT610N"))
818 return &platforms[WRT610N];
821 /* Based on "model_no" */
822 if ((buf = nvram_get("model_no"))) {
823 if (startswith(buf,"WL700")) /* WL700* */
824 return &platforms[WL700GE];
827 /* Based on "hardware_version" */
828 if ((buf = nvram_get("hardware_version"))) {
829 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
830 return &platforms[WL500GPV2];
831 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
832 return &platforms[WL520GC];
833 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
834 return &platforms[WL520GU];
835 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
836 return &platforms[WL330GE];
839 /* Based on "ModelId" */
840 if ((buf = nvram_get("ModelId"))) {
841 if (!strcmp(buf, "WR850GP"))
842 return &platforms[WR850GP];
843 if (!strcmp(buf, "WR850G"))
844 return &platforms[WR850GV2V3];
845 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
846 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
847 if (startswith(buf,"WE800G")) /* WE800G* */
848 return &platforms[WE800G];
852 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
853 /* Buffalo hardware, check id for specific hardware matches */
854 if (!strcmp(buf, "29bb0332"))
855 return &platforms[WBR2_G54];
856 if (!strcmp(buf, "29129"))
857 return &platforms[WLA2_G54L];
858 if (!strcmp(buf, "30189"))
859 return &platforms[WHR_HP_G54];
860 if (!strcmp(buf, "32093"))
861 return &platforms[WHR_G125];
862 if (!strcmp(buf, "30182"))
863 return &platforms[WHR_G54S];
864 if (!strcmp(buf, "290441dd"))
865 return &platforms[WHR2_A54G54];
866 if (!strcmp(buf, "31120"))
867 return &platforms[WZR_G300N];
868 if (!strcmp(buf, "30083"))
869 return &platforms[WZR_RS_G54];
870 if (!strcmp(buf, "30103"))
871 return &platforms[WZR_RS_G54HP];
874 /* no easy model number, attempt to guess */
875 boardnum = getvar("boardnum");
876 boardtype = getvar("boardtype");
878 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
879 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
880 return &platforms[WRT600NV11];
882 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
883 return &platforms[WRT600N];
886 if (startswith(getvar("pmon_ver"), "CFE")) {
887 /* CFE based - newer hardware */
888 if (!strcmp(boardnum, "42")) { /* Linksys */
889 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
890 return &platforms[WRT350N];
892 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
893 return &platforms[WRT54G3G];
895 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
896 return &platforms[WRTSL54GS];
898 if (!strcmp(boardtype, "0x0472"))
899 return &platforms[WRT160N];
901 /* default to WRT54G */
902 return &platforms[WRT54G];
904 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
905 return &platforms[WAP54GV2];
907 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
908 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
909 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
912 if (!strcmp(boardnum, "45")) { /* ASUS */
913 if (!strcmp(boardtype,"0x042f"))
914 return &platforms[WL500GP];
915 else if (!strcmp(boardtype,"0x0472"))
916 return &platforms[WL500W];
917 else if (!strcmp(boardtype,"0x467"))
918 return &platforms[WL320GE];
920 return &platforms[WL500GD];
923 if (!strcmp(boardnum, "10496"))
924 return &platforms[USR5461];
926 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
927 return &platforms[SE505V2];
929 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
930 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
931 return &platforms[DIR320];
933 } else { /* PMON based - old stuff */
934 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
935 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
936 return &platforms[WR850GV1];
938 if (startswith(boardtype, "bcm94710dev")) {
939 if (!strcmp(boardnum, "42"))
940 return &platforms[WRT54GV1];
941 if (simple_strtoul(boardnum, NULL, 0) == 2)
942 return &platforms[WAP54GV1];
944 if (startswith(getvar("hardware_version"), "WL500-"))
945 return &platforms[WL500G];
946 if (startswith(getvar("hardware_version"), "WL300-")) {
947 /* Either WL-300g or WL-HDD, do more extensive checks */
948 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
949 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
950 return &platforms[WLHDD];
951 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
952 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
953 return &platforms[WL300G];
955 /* Sitecom WL-105b */
956 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
957 return &platforms[WL105B];
959 /* unknown asus stuff, probably bcm4702 */
960 if (startswith(boardnum, "asusX"))
961 return &platforms[ASUS_4702];
964 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
965 if (startswith(boardtype, "bcm94710ap"))
966 return &platforms[BUFFALO_UNKNOWN_4710];
968 return &platforms[BUFFALO_UNKNOWN];
971 if (startswith(getvar("CFEver"), "MotoWRv2") ||
972 startswith(getvar("CFEver"), "MotoWRv3") ||
973 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
975 return &platforms[WR850GV2V3];
978 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
979 return &platforms[TEW411BRPP];
982 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
983 return &platforms[STI_NAS];
985 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
986 return &platforms[DWL3150];
992 static void register_buttons(struct button_t *b)
995 platform.button_mask |= b->gpio;
997 platform.button_mask &= ~gpiomask;
999 gpio_outen(platform.button_mask, 0);
1000 gpio_control(platform.button_mask, 0);
1001 platform.button_polarity = gpio_in() & platform.button_mask;
1002 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1003 gpio_setintmask(platform.button_mask, platform.button_mask);
1005 gpio_set_irqenable(1, button_handler);
1008 static void unregister_buttons(struct button_t *b)
1010 gpio_setintmask(platform.button_mask, 0);
1012 gpio_set_irqenable(0, button_handler);
1017 static void add_msg(struct event_t *event, char *msg, int argv)
1024 s = skb_put(event->skb, strlen(msg) + 1);
1028 static void hotplug_button(struct work_struct *work)
1030 struct event_t *event = container_of(work, struct event_t, wq);
1036 event->skb = alloc_skb(2048, GFP_KERNEL);
1038 s = skb_put(event->skb, strlen(event->action) + 2);
1039 sprintf(s, "%s@", event->action);
1042 NETLINK_CB(event->skb).dst_group = 1;
1043 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1048 #else /* !LINUX_2_4 */
1049 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1053 p = kmalloc(size, gfp);
1062 static void add_msg(struct event_t *event, char *msg, int argv)
1065 event->argv[event->anr++] = event->scratch;
1067 event->envp[event->enr++] = event->scratch;
1069 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1072 static void hotplug_button(struct event_t *event)
1074 char *scratch = kzalloc(256, GFP_KERNEL);
1075 event->scratch = scratch;
1077 add_msg(event, hotplug_path, 1);
1078 add_msg(event, "button", 1);
1080 call_usermodehelper (event->argv[0], event->argv, event->envp);
1084 #endif /* !LINUX_2_4 */
1086 static int fill_event (struct event_t *event)
1088 static char buf[128];
1090 add_msg(event, "HOME=/", 0);
1091 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1092 add_msg(event, "SUBSYSTEM=button", 0);
1093 snprintf(buf, 128, "ACTION=%s", event->action);
1094 add_msg(event, buf, 0);
1095 snprintf(buf, 128, "BUTTON=%s", event->name);
1096 add_msg(event, buf, 0);
1097 snprintf(buf, 128, "SEEN=%ld", event->seen);
1098 add_msg(event, buf, 0);
1100 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1101 add_msg(event, buf, 0);
1109 static irqreturn_t button_handler(int irq, void *dev_id)
1111 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1117 in = gpio_in() & platform.button_mask;
1118 gpio_intpolarity(platform.button_mask, in);
1119 changed = platform.button_polarity ^ in;
1120 platform.button_polarity = in;
1122 changed &= ~gpio_outen(0, 0);
1124 for (b = platform.buttons; b->name; b++) {
1125 struct event_t *event;
1127 if (!(b->gpio & changed)) continue;
1131 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1132 event->seen = (jiffies - b->seen)/HZ;
1133 event->name = b->name;
1134 event->action = b->pressed ? "pressed" : "released";
1135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1136 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1138 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1140 schedule_work(&event->wq);
1148 static void register_leds(struct led_t *l)
1150 struct proc_dir_entry *p;
1155 leds = proc_mkdir("led", diag);
1159 for(; l->name; l++) {
1160 if (l->gpio & gpiomask)
1163 if (l->gpio & GPIO_TYPE_EXTIF) {
1167 if (l->polarity != INPUT) oe_mask |= l->gpio;
1169 val |= (l->polarity == NORMAL)?0:l->gpio;
1172 if (l->polarity == INPUT) continue;
1174 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1175 l->proc.type = PROC_LED;
1177 p->data = (void *) &l->proc;
1178 p->proc_fops = &diag_proc_fops;
1182 gpio_outen(mask, oe_mask);
1183 gpio_control(mask, 0);
1184 gpio_out(mask, val);
1185 gpio_setintmask(mask, 0);
1188 static void unregister_leds(struct led_t *l)
1191 remove_proc_entry(l->name, leds);
1193 remove_proc_entry("led", diag);
1196 static void set_led_extif(struct led_t *led)
1198 gpio_set_extif(led->gpio, led->state);
1201 static void led_flash(unsigned long dummy) {
1206 for (l = platform.leds; l->name; l++) {
1208 if (l->gpio & GPIO_TYPE_EXTIF) {
1210 l->state = !l->state;
1220 u32 val = ~gpio_in();
1222 gpio_outen(mask, mask);
1223 gpio_control(mask, 0);
1224 gpio_out(mask, val);
1226 if (mask || extif_blink) {
1227 mod_timer(&led_timer, jiffies + FLASH_TIME);
1231 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1234 struct inode *inode = file->f_dentry->d_inode;
1235 struct proc_dir_entry *dent = inode->u.generic_ip;
1237 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1242 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1245 if (dent->data != NULL) {
1246 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1247 switch (handler->type) {
1249 struct led_t * led = (struct led_t *) handler->ptr;
1251 len = sprintf(page, "f\n");
1253 if (led->gpio & GPIO_TYPE_EXTIF) {
1254 len = sprintf(page, "%d\n", led->state);
1256 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1257 u8 p = (led->polarity == NORMAL ? 0 : 1);
1258 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1264 len = sprintf(page, "%s\n", platform.name);
1267 len = sprintf(page, "0x%04x\n", gpiomask);
1274 len = min_t(int, len - *ppos, count);
1275 if (copy_to_user(buf, (page + *ppos), len)) {
1289 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1292 struct inode *inode = file->f_dentry->d_inode;
1293 struct proc_dir_entry *dent = inode->u.generic_ip;
1295 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1300 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1303 if (copy_from_user(page, buf, count)) {
1309 if (dent->data != NULL) {
1310 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1311 switch (handler->type) {
1313 struct led_t *led = (struct led_t *) handler->ptr;
1314 int p = (led->polarity == NORMAL ? 0 : 1);
1316 if (page[0] == 'f') {
1321 if (led->gpio & GPIO_TYPE_EXTIF) {
1322 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1325 gpio_outen(led->gpio, led->gpio);
1326 gpio_control(led->gpio, 0);
1327 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1333 gpiomask = simple_strtoul(page, NULL, 0);
1335 if (platform.buttons) {
1336 unregister_buttons(platform.buttons);
1337 register_buttons(platform.buttons);
1340 if (platform.leds) {
1341 unregister_leds(platform.leds);
1342 register_leds(platform.leds);
1353 static int __init diag_init(void)
1355 static struct proc_dir_entry *p;
1356 static struct platform_t *detected;
1358 detected = platform_detect();
1360 printk(MODULE_NAME ": Router model not detected.\n");
1363 memcpy(&platform, detected, sizeof(struct platform_t));
1365 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1366 if (platform.platform_init != NULL) {
1367 platform.platform_init();
1370 if (!(diag = proc_mkdir("diag", NULL))) {
1371 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1375 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1376 p->data = (void *) &proc_model;
1377 p->proc_fops = &diag_proc_fops;
1380 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1381 p->data = (void *) &proc_gpiomask;
1382 p->proc_fops = &diag_proc_fops;
1385 if (platform.buttons)
1386 register_buttons(platform.buttons);
1389 register_leds(platform.leds);
1394 static void __exit diag_exit(void)
1396 del_timer(&led_timer);
1398 if (platform.buttons)
1399 unregister_buttons(platform.buttons);
1402 unregister_leds(platform.leds);
1404 remove_proc_entry("model", diag);
1405 remove_proc_entry("gpiomask", diag);
1406 remove_proc_entry("diag", NULL);
1409 module_init(diag_init);
1410 module_exit(diag_exit);
1412 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1413 MODULE_LICENSE("GPL");