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.
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kmod.h>
27 #include <linux/proc_fs.h>
28 #include <linux/timer.h>
29 #include <linux/version.h>
30 #include <asm/uaccess.h>
33 #include <linux/workqueue.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
37 extern struct sock *uevent_sock;
38 extern u64 uevent_next_seqnum(void);
40 #include <linux/tqueue.h>
41 #define INIT_WORK INIT_TQUEUE
42 #define schedule_work schedule_task
43 #define work_struct tq_struct
48 #define getvar(str) (nvram_get(str)?:"")
50 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
51 static int fill_event(struct event_t *);
52 static unsigned int gpiomask = 0;
53 module_param(gpiomask, int, 0644);
133 static void __init bcm4780_init(void) {
136 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
137 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
138 gpio_outen(pin, pin);
139 gpio_control(pin, 0);
142 /* Wait 5s, so the HDD can spin up */
143 set_current_state(TASK_INTERRUPTIBLE);
144 schedule_timeout(HZ * 5);
147 static void __init bcm57xx_init(void) {
151 /* FIXME: switch comes up, but port mappings/vlans not right */
152 gpio_outen(pin, pin);
153 gpio_control(pin, 0);
158 static struct platform_t __initdata platforms[] = {
161 .name = "Linksys WAP54G V1",
163 { .name = "reset", .gpio = 1 << 0 },
166 { .name = "diag", .gpio = 1 << 3 },
167 { .name = "wlan", .gpio = 1 << 4 },
171 .name = "Linksys WAP54G V3",
173 /* FIXME: verify this */
174 { .name = "reset", .gpio = 1 << 7 },
175 { .name = "ses", .gpio = 1 << 0 },
179 { .name = "ses", .gpio = 1 << 1 },
183 .name = "Linksys WRT54G V1.x",
185 { .name = "reset", .gpio = 1 << 6 },
188 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
189 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
193 .name = "Linksys WRT54G/GS/GL",
195 { .name = "reset", .gpio = 1 << 6 },
196 { .name = "ses", .gpio = 1 << 4 },
199 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
200 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
201 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
202 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
203 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
207 .name = "Linksys WRTSL54GS",
209 { .name = "reset", .gpio = 1 << 6 },
210 { .name = "ses", .gpio = 1 << 4 },
213 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
214 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
215 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
216 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
220 .name = "Linksys WRT54G3G",
222 { .name = "reset", .gpio = 1 << 6 },
223 { .name = "3g", .gpio = 1 << 4 },
226 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
227 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
228 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
229 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
230 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
234 .name = "Linksys WRT350N",
236 { .name = "reset", .gpio = 1 << 6 },
237 { .name = "ses", .gpio = 1 << 8 },
240 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
241 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
242 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
243 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
244 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
246 .platform_init = bcm57xx_init,
249 .name = "Linksys WRT600N",
251 { .name = "reset", .gpio = 1 << 6 },
252 { .name = "ses", .gpio = 1 << 7 },
255 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
256 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
257 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
258 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
259 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
260 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
262 .platform_init = bcm57xx_init,
265 .name = "Linksys WRT600N V1.1",
267 { .name = "reset", .gpio = 1 << 6 },
268 { .name = "ses", .gpio = 1 << 7 },
271 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
272 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
273 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
274 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
275 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
276 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
278 .platform_init = bcm57xx_init,
282 .name = "ASUS WL-HDD",
284 { .name = "reset", .gpio = 1 << 6 },
287 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
288 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
292 .name = "ASUS WL-300g",
294 { .name = "reset", .gpio = 1 << 6 },
297 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
301 .name = "ASUS WL-320gE/WL-320gP",
303 { .name = "reset", .gpio = 1 << 6 },
306 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
307 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
308 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
312 .name = "ASUS WL-330gE",
314 { .name = "reset", .gpio = 1 << 2 },
317 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
321 .name = "ASUS WL-500g",
323 { .name = "reset", .gpio = 1 << 6 },
326 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
330 .name = "ASUS WL-500g Deluxe",
332 { .name = "reset", .gpio = 1 << 6 },
335 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
339 .name = "ASUS WL-500g Premium",
341 { .name = "reset", .gpio = 1 << 0 },
342 { .name = "ses", .gpio = 1 << 4 },
345 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
349 .name = "ASUS WL-500g Premium V2",
351 { .name = "reset", .gpio = 1 << 2 },
352 { .name = "ses", .gpio = 1 << 3 },
355 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
356 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
360 .name = "ASUS WL-500W",
362 { .name = "reset", .gpio = 1 << 6 },
363 { .name = "ses", .gpio = 1 << 7 },
366 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
370 .name = "ASUS WL-520GC",
372 { .name = "reset", .gpio = 1 << 2 },
373 { .name = "ses", .gpio = 1 << 3 },
376 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
377 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
381 .name = "ASUS WL-520gU",
383 { .name = "reset", .gpio = 1 << 2 },
384 { .name = "ses", .gpio = 1 << 3 },
387 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
388 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
392 .name = "ASUS (unknown, BCM4702)",
394 { .name = "reset", .gpio = 1 << 6 },
397 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
401 .name = "ASUS WL-700gE",
403 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
404 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
405 { .name = "power", .gpio = 1 << 0 }, // on front
406 { .name = "copy", .gpio = 1 << 6 }, // on front
410 // GPIO that controls power led also enables/disables some essential functions
413 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
415 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
417 .platform_init = bcm4780_init,
421 .name = "Buffalo WHR-G54S",
423 { .name = "reset", .gpio = 1 << 4 },
424 { .name = "bridge", .gpio = 1 << 5 },
425 { .name = "ses", .gpio = 1 << 0 },
428 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
429 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
430 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
431 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
432 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
436 .name = "Buffalo WBR2-G54",
439 { .name = "reset", .gpio = 1 << 7 },
442 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
446 .name = "Buffalo WHR-HP-G54",
448 { .name = "reset", .gpio = 1 << 4 },
449 { .name = "bridge", .gpio = 1 << 5 },
450 { .name = "ses", .gpio = 1 << 0 },
453 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
454 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
455 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
456 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
457 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
461 .name = "Buffalo WHR-G125",
463 { .name = "reset", .gpio = 1 << 4 },
464 { .name = "bridge", .gpio = 1 << 5 },
465 { .name = "ses", .gpio = 1 << 0 },
468 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
469 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
470 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
471 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
472 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
476 .name = "Buffalo WHR2-A54G54",
478 { .name = "reset", .gpio = 1 << 4 },
481 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
485 .name = "Buffalo WLA2-G54L",
488 { .name = "reset", .gpio = 1 << 7 },
491 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
495 .name = "Buffalo WZR-G300N",
497 { .name = "reset", .gpio = 1 << 4 },
500 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
501 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
502 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
506 .name = "Buffalo WZR-RS-G54",
508 { .name = "ses", .gpio = 1 << 0 },
509 { .name = "reset", .gpio = 1 << 4 },
512 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
513 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
514 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
518 .name = "Buffalo WZR-RS-G54HP",
520 { .name = "ses", .gpio = 1 << 0 },
521 { .name = "reset", .gpio = 1 << 4 },
524 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
525 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
526 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
529 [BUFFALO_UNKNOWN] = {
530 .name = "Buffalo (unknown)",
532 { .name = "reset", .gpio = 1 << 7 },
535 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
538 [BUFFALO_UNKNOWN_4710] = {
539 .name = "Buffalo (unknown, BCM4710)",
541 { .name = "reset", .gpio = 1 << 4 },
544 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
549 .name = "Siemens SE505 V1",
551 /* No usable buttons */
554 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
555 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
556 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
560 .name = "Siemens SE505 V2",
562 /* No usable buttons */
565 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
566 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
567 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
572 .name = "U.S. Robotics USR5461",
574 /* No usable buttons */
577 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
578 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
583 .name = "Dell TrueMobile 2300",
585 { .name = "reset", .gpio = 1 << 0 },
588 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
589 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
593 .name = "Dell TrueMobile 2300 v2",
595 { .name = "reset", .gpio = 1 << 0 },
598 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
599 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
604 .name = "Motorola WE800G",
606 { .name = "reset", .gpio = 1 << 0 },
609 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
610 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
611 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
615 .name = "Motorola WR850G V1",
617 { .name = "reset", .gpio = 1 << 0 },
620 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
621 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
622 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
623 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
624 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
628 .name = "Motorola WR850G V2/V3",
630 { .name = "reset", .gpio = 1 << 5 },
633 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
634 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
635 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
636 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
640 .name = "Motorola WR850GP",
642 { .name = "reset", .gpio = 1 << 5 },
645 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
646 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
647 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
648 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
654 .name = "Belkin (unknown)",
655 /* FIXME: verify & add detection */
657 { .name = "reset", .gpio = 1 << 7 },
660 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
661 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
662 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
667 .name = "Netgear WGT634U",
669 { .name = "reset", .gpio = 1 << 2 },
672 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
677 .name = "Trendware TEW411BRP+",
679 { /* No usable buttons */ },
682 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
683 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
684 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
689 .name = "SimpleTech SimpleShare NAS",
691 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
692 { .name = "power", .gpio = 1 << 0 }, // on back
695 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
697 .platform_init = bcm4780_init,
701 .name = "D-Link DIR-130",
703 { .name = "reset", .gpio = 1 << 3},
704 { .name = "reserved", .gpio = 1 << 7},
707 { .name = "diag", .gpio = 1 << 0},
708 { .name = "blue", .gpio = 1 << 6},
712 .name = "D-Link DIR-330",
714 { .name = "reset", .gpio = 1 << 3},
715 { .name = "reserved", .gpio = 1 << 7},
718 { .name = "diag", .gpio = 1 << 0},
719 { .name = "usb", .gpio = 1 << 4},
720 { .name = "blue", .gpio = 1 << 6},
724 .name = "D-Link DWL-3150",
726 { .name = "reset", .gpio = 1 << 7},
729 { .name = "diag", .gpio = 1 << 2},
730 { .name = "status", .gpio = 1 << 1},
735 .name = "Sitecom WL-105b",
737 { .name = "reset", .gpio = 1 << 10},
740 { .name = "wlan", .gpio = 1 << 4},
741 { .name = "power", .gpio = 1 << 3},
746 static struct platform_t __init *platform_detect(void)
748 char *boardnum, *boardtype, *buf;
750 if (strcmp(getvar("nvram_type"), "cfe") == 0)
751 return &platforms[WGT634U];
753 /* Look for a model identifier */
755 /* Based on "model_name" */
756 if ((buf = nvram_get("model_name"))) {
757 if (!strcmp(buf, "DIR-130"))
758 return &platforms[DIR130];
759 if (!strcmp(buf, "DIR-330"))
760 return &platforms[DIR330];
763 /* Based on "model_no" */
764 if ((buf = nvram_get("model_no"))) {
765 if (startswith(buf,"WL700")) /* WL700* */
766 return &platforms[WL700GE];
769 /* Based on "hardware_version" */
770 if ((buf = nvram_get("hardware_version"))) {
771 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
772 return &platforms[WL500GPV2];
773 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
774 return &platforms[WL520GC];
775 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
776 return &platforms[WL520GU];
777 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
778 return &platforms[WL330GE];
781 /* Based on "ModelId" */
782 if ((buf = nvram_get("ModelId"))) {
783 if (!strcmp(buf, "WR850GP"))
784 return &platforms[WR850GP];
785 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
786 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
787 if (startswith(buf,"WE800G")) /* WE800G* */
788 return &platforms[WE800G];
792 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
793 /* Buffalo hardware, check id for specific hardware matches */
794 if (!strcmp(buf, "29bb0332"))
795 return &platforms[WBR2_G54];
796 if (!strcmp(buf, "29129"))
797 return &platforms[WLA2_G54L];
798 if (!strcmp(buf, "30189"))
799 return &platforms[WHR_HP_G54];
800 if (!strcmp(buf, "32093"))
801 return &platforms[WHR_G125];
802 if (!strcmp(buf, "30182"))
803 return &platforms[WHR_G54S];
804 if (!strcmp(buf, "290441dd"))
805 return &platforms[WHR2_A54G54];
806 if (!strcmp(buf, "31120"))
807 return &platforms[WZR_G300N];
808 if (!strcmp(buf, "30083"))
809 return &platforms[WZR_RS_G54];
810 if (!strcmp(buf, "30103"))
811 return &platforms[WZR_RS_G54HP];
814 /* no easy model number, attempt to guess */
815 boardnum = getvar("boardnum");
816 boardtype = getvar("boardtype");
818 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
819 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
820 return &platforms[WRT600NV11];
822 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
823 return &platforms[WRT600N];
826 if (startswith(getvar("pmon_ver"), "CFE")) {
827 /* CFE based - newer hardware */
828 if (!strcmp(boardnum, "42")) { /* Linksys */
829 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
830 return &platforms[WRT350N];
832 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
833 return &platforms[WRT54G3G];
835 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
836 return &platforms[WRTSL54GS];
838 /* default to WRT54G */
839 return &platforms[WRT54G];
842 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
843 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
844 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
847 if (!strcmp(boardnum, "45")) { /* ASUS */
848 if (!strcmp(boardtype,"0x042f"))
849 return &platforms[WL500GP];
850 else if (!strcmp(boardtype,"0x0472"))
851 return &platforms[WL500W];
852 else if (!strcmp(boardtype,"0x467"))
853 return &platforms[WL320GE];
855 return &platforms[WL500GD];
858 if (!strcmp(boardnum, "10496"))
859 return &platforms[USR5461];
861 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
862 return &platforms[SE505V2];
864 } else { /* PMON based - old stuff */
865 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
866 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
867 return &platforms[WR850GV1];
869 if (startswith(boardtype, "bcm94710dev")) {
870 if (!strcmp(boardnum, "42"))
871 return &platforms[WRT54GV1];
872 if (simple_strtoul(boardnum, NULL, 0) == 2)
873 return &platforms[WAP54GV1];
875 if (startswith(getvar("hardware_version"), "WL500-"))
876 return &platforms[WL500G];
877 if (startswith(getvar("hardware_version"), "WL300-")) {
878 /* Either WL-300g or WL-HDD, do more extensive checks */
879 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
880 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
881 return &platforms[WLHDD];
882 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
883 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
884 return &platforms[WL300G];
886 /* Sitecom WL-105b */
887 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
888 return &platforms[WL105B];
890 /* unknown asus stuff, probably bcm4702 */
891 if (startswith(boardnum, "asusX"))
892 return &platforms[ASUS_4702];
895 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
896 if (startswith(boardtype, "bcm94710ap"))
897 return &platforms[BUFFALO_UNKNOWN_4710];
899 return &platforms[BUFFALO_UNKNOWN];
902 if (startswith(getvar("CFEver"), "MotoWRv2") ||
903 startswith(getvar("CFEver"), "MotoWRv3") ||
904 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
906 return &platforms[WR850GV2V3];
909 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
910 return &platforms[TEW411BRPP];
913 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
914 return &platforms[STI_NAS];
916 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
917 return &platforms[DWL3150];
923 static void register_buttons(struct button_t *b)
926 platform.button_mask |= b->gpio;
928 platform.button_mask &= ~gpiomask;
930 gpio_outen(platform.button_mask, 0);
931 gpio_control(platform.button_mask, 0);
932 platform.button_polarity = gpio_in() & platform.button_mask;
933 gpio_intpolarity(platform.button_mask, platform.button_polarity);
934 gpio_setintmask(platform.button_mask, platform.button_mask);
936 gpio_set_irqenable(1, button_handler);
939 static void unregister_buttons(struct button_t *b)
941 gpio_setintmask(platform.button_mask, 0);
943 gpio_set_irqenable(0, button_handler);
948 static void add_msg(struct event_t *event, char *msg, int argv)
955 s = skb_put(event->skb, strlen(msg) + 1);
959 static void hotplug_button(struct work_struct *work)
961 struct event_t *event = container_of(work, struct event_t, wq);
967 event->skb = alloc_skb(2048, GFP_KERNEL);
969 s = skb_put(event->skb, strlen(event->action) + 2);
970 sprintf(s, "%s@", event->action);
973 NETLINK_CB(event->skb).dst_group = 1;
974 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
979 #else /* !LINUX_2_4 */
980 static inline char *kzalloc(unsigned int size, unsigned int gfp)
984 p = kmalloc(size, gfp);
993 static void add_msg(struct event_t *event, char *msg, int argv)
996 event->argv[event->anr++] = event->scratch;
998 event->envp[event->enr++] = event->scratch;
1000 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1003 static void hotplug_button(struct event_t *event)
1005 char *scratch = kzalloc(256, GFP_KERNEL);
1006 event->scratch = scratch;
1008 add_msg(event, hotplug_path, 1);
1009 add_msg(event, "button", 1);
1011 call_usermodehelper (event->argv[0], event->argv, event->envp);
1015 #endif /* !LINUX_2_4 */
1017 static int fill_event (struct event_t *event)
1019 static char buf[128];
1021 add_msg(event, "HOME=/", 0);
1022 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1023 add_msg(event, "SUBSYSTEM=button", 0);
1024 snprintf(buf, 128, "ACTION=%s", event->action);
1025 add_msg(event, buf, 0);
1026 snprintf(buf, 128, "BUTTON=%s", event->name);
1027 add_msg(event, buf, 0);
1028 snprintf(buf, 128, "SEEN=%ld", event->seen);
1029 add_msg(event, buf, 0);
1031 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1032 add_msg(event, buf, 0);
1040 static irqreturn_t button_handler(int irq, void *dev_id)
1042 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1048 in = gpio_in() & platform.button_mask;
1049 gpio_intpolarity(platform.button_mask, in);
1050 changed = platform.button_polarity ^ in;
1051 platform.button_polarity = in;
1053 changed &= ~gpio_outen(0, 0);
1055 for (b = platform.buttons; b->name; b++) {
1056 struct event_t *event;
1058 if (!(b->gpio & changed)) continue;
1062 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1063 event->seen = (jiffies - b->seen)/HZ;
1064 event->name = b->name;
1065 event->action = b->pressed ? "pressed" : "released";
1066 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1067 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1069 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1071 schedule_work(&event->wq);
1079 static void register_leds(struct led_t *l)
1081 struct proc_dir_entry *p;
1086 leds = proc_mkdir("led", diag);
1090 for(; l->name; l++) {
1091 if (l->gpio & gpiomask)
1094 if (l->gpio & GPIO_TYPE_EXTIF) {
1098 if (l->polarity != INPUT) oe_mask |= l->gpio;
1100 val |= (l->polarity == NORMAL)?0:l->gpio;
1103 if (l->polarity == INPUT) continue;
1105 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1106 l->proc.type = PROC_LED;
1108 p->data = (void *) &l->proc;
1109 p->proc_fops = &diag_proc_fops;
1113 gpio_outen(mask, oe_mask);
1114 gpio_control(mask, 0);
1115 gpio_out(mask, val);
1116 gpio_setintmask(mask, 0);
1119 static void unregister_leds(struct led_t *l)
1122 remove_proc_entry(l->name, leds);
1124 remove_proc_entry("led", diag);
1127 static void set_led_extif(struct led_t *led)
1129 gpio_set_extif(led->gpio, led->state);
1132 static void led_flash(unsigned long dummy) {
1137 for (l = platform.leds; l->name; l++) {
1139 if (l->gpio & GPIO_TYPE_EXTIF) {
1141 l->state = !l->state;
1151 u32 val = ~gpio_in();
1153 gpio_outen(mask, mask);
1154 gpio_control(mask, 0);
1155 gpio_out(mask, val);
1157 if (mask || extif_blink) {
1158 mod_timer(&led_timer, jiffies + FLASH_TIME);
1162 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1165 struct inode *inode = file->f_dentry->d_inode;
1166 struct proc_dir_entry *dent = inode->u.generic_ip;
1168 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1173 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1176 if (dent->data != NULL) {
1177 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1178 switch (handler->type) {
1180 struct led_t * led = (struct led_t *) handler->ptr;
1182 len = sprintf(page, "f\n");
1184 if (led->gpio & GPIO_TYPE_EXTIF) {
1185 len = sprintf(page, "%d\n", led->state);
1187 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1188 u8 p = (led->polarity == NORMAL ? 0 : 1);
1189 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1195 len = sprintf(page, "%s\n", platform.name);
1198 len = sprintf(page, "0x%04x\n", gpiomask);
1205 len = min_t(int, len - *ppos, count);
1206 if (copy_to_user(buf, (page + *ppos), len)) {
1220 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1223 struct inode *inode = file->f_dentry->d_inode;
1224 struct proc_dir_entry *dent = inode->u.generic_ip;
1226 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1231 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1234 if (copy_from_user(page, buf, count)) {
1240 if (dent->data != NULL) {
1241 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1242 switch (handler->type) {
1244 struct led_t *led = (struct led_t *) handler->ptr;
1245 int p = (led->polarity == NORMAL ? 0 : 1);
1247 if (page[0] == 'f') {
1252 if (led->gpio & GPIO_TYPE_EXTIF) {
1253 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1256 gpio_outen(led->gpio, led->gpio);
1257 gpio_control(led->gpio, 0);
1258 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1264 gpiomask = simple_strtoul(page, NULL, 0);
1266 if (platform.buttons) {
1267 unregister_buttons(platform.buttons);
1268 register_buttons(platform.buttons);
1271 if (platform.leds) {
1272 unregister_leds(platform.leds);
1273 register_leds(platform.leds);
1284 static int __init diag_init(void)
1286 static struct proc_dir_entry *p;
1287 static struct platform_t *detected;
1289 detected = platform_detect();
1291 printk(MODULE_NAME ": Router model not detected.\n");
1294 memcpy(&platform, detected, sizeof(struct platform_t));
1296 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1297 if (platform.platform_init != NULL) {
1298 platform.platform_init();
1301 if (!(diag = proc_mkdir("diag", NULL))) {
1302 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1306 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1307 p->data = (void *) &proc_model;
1308 p->proc_fops = &diag_proc_fops;
1311 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1312 p->data = (void *) &proc_gpiomask;
1313 p->proc_fops = &diag_proc_fops;
1316 if (platform.buttons)
1317 register_buttons(platform.buttons);
1320 register_leds(platform.leds);
1325 static void __exit diag_exit(void)
1327 del_timer(&led_timer);
1329 if (platform.buttons)
1330 unregister_buttons(platform.buttons);
1333 unregister_leds(platform.leds);
1335 remove_proc_entry("model", diag);
1336 remove_proc_entry("gpiomask", diag);
1337 remove_proc_entry("diag", NULL);
1340 module_init(diag_init);
1341 module_exit(diag_exit);
1343 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1344 MODULE_LICENSE("GPL");