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);
132 static void __init bcm4780_init(void) {
135 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
136 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
137 gpio_outen(pin, pin);
138 gpio_control(pin, 0);
141 /* Wait 5s, so the HDD can spin up */
142 set_current_state(TASK_INTERRUPTIBLE);
143 schedule_timeout(HZ * 5);
146 static void __init bcm57xx_init(void) {
150 /* FIXME: switch comes up, but port mappings/vlans not right */
151 gpio_outen(pin, pin);
152 gpio_control(pin, 0);
157 static struct platform_t __initdata platforms[] = {
160 .name = "Linksys WAP54G V1",
162 { .name = "reset", .gpio = 1 << 0 },
165 { .name = "diag", .gpio = 1 << 3 },
166 { .name = "wlan", .gpio = 1 << 4 },
170 .name = "Linksys WAP54G V3",
172 /* FIXME: verify this */
173 { .name = "reset", .gpio = 1 << 7 },
174 { .name = "ses", .gpio = 1 << 0 },
178 { .name = "ses", .gpio = 1 << 1 },
182 .name = "Linksys WRT54G V1.x",
184 { .name = "reset", .gpio = 1 << 6 },
187 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
188 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
192 .name = "Linksys WRT54G/GS/GL",
194 { .name = "reset", .gpio = 1 << 6 },
195 { .name = "ses", .gpio = 1 << 4 },
198 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
199 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
200 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
201 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
202 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
206 .name = "Linksys WRTSL54GS",
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 << 0, .polarity = REVERSE },
214 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
215 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
219 .name = "Linksys WRT54G3G",
221 { .name = "reset", .gpio = 1 << 6 },
222 { .name = "3g", .gpio = 1 << 4 },
225 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
226 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
227 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
228 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
229 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
233 .name = "Linksys WRT350N",
235 { .name = "reset", .gpio = 1 << 6 },
236 { .name = "ses", .gpio = 1 << 8 },
239 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
240 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
241 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
242 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
243 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
245 .platform_init = bcm57xx_init,
248 .name = "Linksys WRT600N",
250 { .name = "reset", .gpio = 1 << 6 },
251 { .name = "ses", .gpio = 1 << 7 },
254 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
255 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
256 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
257 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
258 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
259 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
261 .platform_init = bcm57xx_init,
264 .name = "Linksys WRT600N V1.1",
266 { .name = "reset", .gpio = 1 << 6 },
267 { .name = "ses", .gpio = 1 << 7 },
270 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
271 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
272 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
273 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
274 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
275 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
277 .platform_init = bcm57xx_init,
281 .name = "ASUS WL-HDD",
283 { .name = "reset", .gpio = 1 << 6 },
286 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
287 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
291 .name = "ASUS WL-300g",
293 { .name = "reset", .gpio = 1 << 6 },
296 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
300 .name = "ASUS WL-320gE/WL-320gP",
302 { .name = "reset", .gpio = 1 << 6 },
305 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
306 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
307 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
311 .name = "ASUS WL-500g",
313 { .name = "reset", .gpio = 1 << 6 },
316 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
320 .name = "ASUS WL-500g Deluxe",
322 { .name = "reset", .gpio = 1 << 6 },
325 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
329 .name = "ASUS WL-500g Premium",
331 { .name = "reset", .gpio = 1 << 0 },
332 { .name = "ses", .gpio = 1 << 4 },
335 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
339 .name = "ASUS WL-500g Premium V2",
341 { .name = "reset", .gpio = 1 << 2 },
342 { .name = "ses", .gpio = 1 << 3 },
345 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
346 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
350 .name = "ASUS WL-500W",
352 { .name = "reset", .gpio = 1 << 6 },
353 { .name = "ses", .gpio = 1 << 7 },
356 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
360 .name = "ASUS WL-520GC",
362 { .name = "reset", .gpio = 1 << 2 },
363 { .name = "ses", .gpio = 1 << 3 },
366 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
367 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
371 .name = "ASUS WL-520gU",
373 { .name = "reset", .gpio = 1 << 2 },
374 { .name = "ses", .gpio = 1 << 3 },
377 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
378 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
382 .name = "ASUS (unknown, BCM4702)",
384 { .name = "reset", .gpio = 1 << 6 },
387 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
391 .name = "ASUS WL-700gE",
393 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
394 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
395 { .name = "power", .gpio = 1 << 0 }, // on front
396 { .name = "copy", .gpio = 1 << 6 }, // on front
400 // GPIO that controls power led also enables/disables some essential functions
403 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
405 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
407 .platform_init = bcm4780_init,
411 .name = "Buffalo WHR-G54S",
413 { .name = "reset", .gpio = 1 << 4 },
414 { .name = "bridge", .gpio = 1 << 5 },
415 { .name = "ses", .gpio = 1 << 0 },
418 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
419 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
420 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
421 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
422 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
426 .name = "Buffalo WBR2-G54",
429 { .name = "reset", .gpio = 1 << 7 },
432 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
436 .name = "Buffalo WHR-HP-G54",
438 { .name = "reset", .gpio = 1 << 4 },
439 { .name = "bridge", .gpio = 1 << 5 },
440 { .name = "ses", .gpio = 1 << 0 },
443 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
444 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
445 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
446 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
447 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
451 .name = "Buffalo WHR-G125",
453 { .name = "reset", .gpio = 1 << 4 },
454 { .name = "bridge", .gpio = 1 << 5 },
455 { .name = "ses", .gpio = 1 << 0 },
458 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
459 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
460 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
461 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
462 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
466 .name = "Buffalo WHR2-A54G54",
468 { .name = "reset", .gpio = 1 << 4 },
471 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
475 .name = "Buffalo WLA2-G54L",
478 { .name = "reset", .gpio = 1 << 7 },
481 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
485 .name = "Buffalo WZR-G300N",
487 { .name = "reset", .gpio = 1 << 4 },
490 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
491 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
492 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
496 .name = "Buffalo WZR-RS-G54",
498 { .name = "ses", .gpio = 1 << 0 },
499 { .name = "reset", .gpio = 1 << 4 },
502 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
503 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
504 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
508 .name = "Buffalo WZR-RS-G54HP",
510 { .name = "ses", .gpio = 1 << 0 },
511 { .name = "reset", .gpio = 1 << 4 },
514 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
515 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
516 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
519 [BUFFALO_UNKNOWN] = {
520 .name = "Buffalo (unknown)",
522 { .name = "reset", .gpio = 1 << 7 },
525 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
528 [BUFFALO_UNKNOWN_4710] = {
529 .name = "Buffalo (unknown, BCM4710)",
531 { .name = "reset", .gpio = 1 << 4 },
534 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
539 .name = "Siemens SE505 V1",
541 /* No usable buttons */
544 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
545 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
546 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
550 .name = "Siemens SE505 V2",
552 /* No usable buttons */
555 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
556 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
557 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
562 .name = "U.S. Robotics USR5461",
564 /* No usable buttons */
567 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
568 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
573 .name = "Dell TrueMobile 2300",
575 { .name = "reset", .gpio = 1 << 0 },
578 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
579 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
583 .name = "Dell TrueMobile 2300 v2",
585 { .name = "reset", .gpio = 1 << 0 },
588 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
589 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
594 .name = "Motorola WE800G",
596 { .name = "reset", .gpio = 1 << 0 },
599 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
600 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
601 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
605 .name = "Motorola WR850G V1",
607 { .name = "reset", .gpio = 1 << 0 },
610 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
611 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
612 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
613 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
614 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
618 .name = "Motorola WR850G V2/V3",
620 { .name = "reset", .gpio = 1 << 5 },
623 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
624 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
625 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
626 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
630 .name = "Motorola WR850GP",
632 { .name = "reset", .gpio = 1 << 5 },
635 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
636 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
637 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
638 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
644 .name = "Belkin (unknown)",
645 /* FIXME: verify & add detection */
647 { .name = "reset", .gpio = 1 << 7 },
650 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
651 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
652 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
657 .name = "Netgear WGT634U",
659 { .name = "reset", .gpio = 1 << 2 },
662 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
667 .name = "Trendware TEW411BRP+",
669 { /* No usable buttons */ },
672 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
673 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
674 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
679 .name = "SimpleTech SimpleShare NAS",
681 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
682 { .name = "power", .gpio = 1 << 0 }, // on back
685 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
687 .platform_init = bcm4780_init,
691 .name = "D-Link DIR-130",
693 { .name = "reset", .gpio = 1 << 3},
694 { .name = "reserved", .gpio = 1 << 7},
697 { .name = "diag", .gpio = 1 << 0},
698 { .name = "blue", .gpio = 1 << 6},
702 .name = "D-Link DIR-330",
704 { .name = "reset", .gpio = 1 << 3},
705 { .name = "reserved", .gpio = 1 << 7},
708 { .name = "diag", .gpio = 1 << 0},
709 { .name = "usb", .gpio = 1 << 4},
710 { .name = "blue", .gpio = 1 << 6},
714 .name = "D-Link DWL-3150",
716 { .name = "reset", .gpio = 1 << 7},
719 { .name = "diag", .gpio = 1 << 2},
720 { .name = "status", .gpio = 1 << 1},
725 .name = "Sitecom WL-105b",
727 { .name = "reset", .gpio = 1 << 10},
730 { .name = "wlan", .gpio = 1 << 4},
731 { .name = "power", .gpio = 1 << 3},
736 static struct platform_t __init *platform_detect(void)
738 char *boardnum, *boardtype, *buf;
740 if (strcmp(getvar("nvram_type"), "cfe") == 0)
741 return &platforms[WGT634U];
743 /* Look for a model identifier */
745 /* Based on "model_name" */
746 if ((buf = nvram_get("model_name"))) {
747 if (!strcmp(buf, "DIR-130"))
748 return &platforms[DIR130];
749 if (!strcmp(buf, "DIR-330"))
750 return &platforms[DIR330];
753 /* Based on "model_no" */
754 if ((buf = nvram_get("model_no"))) {
755 if (startswith(buf,"WL700")) /* WL700* */
756 return &platforms[WL700GE];
759 /* Based on "hardware_version" */
760 if ((buf = nvram_get("hardware_version"))) {
761 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
762 return &platforms[WL500GPV2];
763 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
764 return &platforms[WL520GC];
765 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
766 return &platforms[WL520GU];
769 /* Based on "ModelId" */
770 if ((buf = nvram_get("ModelId"))) {
771 if (!strcmp(buf, "WR850GP"))
772 return &platforms[WR850GP];
773 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
774 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
775 if (startswith(buf,"WE800G")) /* WE800G* */
776 return &platforms[WE800G];
780 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
781 /* Buffalo hardware, check id for specific hardware matches */
782 if (!strcmp(buf, "29bb0332"))
783 return &platforms[WBR2_G54];
784 if (!strcmp(buf, "29129"))
785 return &platforms[WLA2_G54L];
786 if (!strcmp(buf, "30189"))
787 return &platforms[WHR_HP_G54];
788 if (!strcmp(buf, "32093"))
789 return &platforms[WHR_G125];
790 if (!strcmp(buf, "30182"))
791 return &platforms[WHR_G54S];
792 if (!strcmp(buf, "290441dd"))
793 return &platforms[WHR2_A54G54];
794 if (!strcmp(buf, "31120"))
795 return &platforms[WZR_G300N];
796 if (!strcmp(buf, "30083"))
797 return &platforms[WZR_RS_G54];
798 if (!strcmp(buf, "30103"))
799 return &platforms[WZR_RS_G54HP];
802 /* no easy model number, attempt to guess */
803 boardnum = getvar("boardnum");
804 boardtype = getvar("boardtype");
806 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
807 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
808 return &platforms[WRT600NV11];
810 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
811 return &platforms[WRT600N];
814 if (startswith(getvar("pmon_ver"), "CFE")) {
815 /* CFE based - newer hardware */
816 if (!strcmp(boardnum, "42")) { /* Linksys */
817 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
818 return &platforms[WRT350N];
820 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
821 return &platforms[WRT54G3G];
823 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
824 return &platforms[WRTSL54GS];
826 /* default to WRT54G */
827 return &platforms[WRT54G];
830 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
831 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
832 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
835 if (!strcmp(boardnum, "45")) { /* ASUS */
836 if (!strcmp(boardtype,"0x042f"))
837 return &platforms[WL500GP];
838 else if (!strcmp(boardtype,"0x0472"))
839 return &platforms[WL500W];
840 else if (!strcmp(boardtype,"0x467"))
841 return &platforms[WL320GE];
843 return &platforms[WL500GD];
846 if (!strcmp(boardnum, "10496"))
847 return &platforms[USR5461];
849 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
850 return &platforms[SE505V2];
852 } else { /* PMON based - old stuff */
853 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
854 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
855 return &platforms[WR850GV1];
857 if (startswith(boardtype, "bcm94710dev")) {
858 if (!strcmp(boardnum, "42"))
859 return &platforms[WRT54GV1];
860 if (simple_strtoul(boardnum, NULL, 0) == 2)
861 return &platforms[WAP54GV1];
863 if (startswith(getvar("hardware_version"), "WL500-"))
864 return &platforms[WL500G];
865 if (startswith(getvar("hardware_version"), "WL300-")) {
866 /* Either WL-300g or WL-HDD, do more extensive checks */
867 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
868 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
869 return &platforms[WLHDD];
870 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
871 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
872 return &platforms[WL300G];
874 /* Sitecom WL-105b */
875 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
876 return &platforms[WL105B];
878 /* unknown asus stuff, probably bcm4702 */
879 if (startswith(boardnum, "asusX"))
880 return &platforms[ASUS_4702];
883 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
884 if (startswith(boardtype, "bcm94710ap"))
885 return &platforms[BUFFALO_UNKNOWN_4710];
887 return &platforms[BUFFALO_UNKNOWN];
890 if (startswith(getvar("CFEver"), "MotoWRv2") ||
891 startswith(getvar("CFEver"), "MotoWRv3") ||
892 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
894 return &platforms[WR850GV2V3];
897 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
898 return &platforms[TEW411BRPP];
901 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
902 return &platforms[STI_NAS];
904 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
905 return &platforms[DWL3150];
911 static void register_buttons(struct button_t *b)
914 platform.button_mask |= b->gpio;
916 platform.button_mask &= ~gpiomask;
918 gpio_outen(platform.button_mask, 0);
919 gpio_control(platform.button_mask, 0);
920 platform.button_polarity = gpio_in() & platform.button_mask;
921 gpio_intpolarity(platform.button_mask, platform.button_polarity);
922 gpio_setintmask(platform.button_mask, platform.button_mask);
924 gpio_set_irqenable(1, button_handler);
927 static void unregister_buttons(struct button_t *b)
929 gpio_setintmask(platform.button_mask, 0);
931 gpio_set_irqenable(0, button_handler);
936 static void add_msg(struct event_t *event, char *msg, int argv)
943 s = skb_put(event->skb, strlen(msg) + 1);
947 static void hotplug_button(struct work_struct *work)
949 struct event_t *event = container_of(work, struct event_t, wq);
955 event->skb = alloc_skb(2048, GFP_KERNEL);
957 s = skb_put(event->skb, strlen(event->action) + 2);
958 sprintf(s, "%s@", event->action);
961 NETLINK_CB(event->skb).dst_group = 1;
962 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
967 #else /* !LINUX_2_4 */
968 static inline char *kzalloc(unsigned int size, unsigned int gfp)
972 p = kmalloc(size, gfp);
981 static void add_msg(struct event_t *event, char *msg, int argv)
984 event->argv[event->anr++] = event->scratch;
986 event->envp[event->enr++] = event->scratch;
988 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
991 static void hotplug_button(struct event_t *event)
993 char *scratch = kzalloc(256, GFP_KERNEL);
994 event->scratch = scratch;
996 add_msg(event, hotplug_path, 1);
997 add_msg(event, "button", 1);
999 call_usermodehelper (event->argv[0], event->argv, event->envp);
1003 #endif /* !LINUX_2_4 */
1005 static int fill_event (struct event_t *event)
1007 static char buf[128];
1009 add_msg(event, "HOME=/", 0);
1010 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1011 add_msg(event, "SUBSYSTEM=button", 0);
1012 snprintf(buf, 128, "ACTION=%s", event->action);
1013 add_msg(event, buf, 0);
1014 snprintf(buf, 128, "BUTTON=%s", event->name);
1015 add_msg(event, buf, 0);
1016 snprintf(buf, 128, "SEEN=%ld", event->seen);
1017 add_msg(event, buf, 0);
1019 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1020 add_msg(event, buf, 0);
1028 static irqreturn_t button_handler(int irq, void *dev_id)
1030 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1036 in = gpio_in() & platform.button_mask;
1037 gpio_intpolarity(platform.button_mask, in);
1038 changed = platform.button_polarity ^ in;
1039 platform.button_polarity = in;
1041 changed &= ~gpio_outen(0, 0);
1043 for (b = platform.buttons; b->name; b++) {
1044 struct event_t *event;
1046 if (!(b->gpio & changed)) continue;
1050 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1051 event->seen = (jiffies - b->seen)/HZ;
1052 event->name = b->name;
1053 event->action = b->pressed ? "pressed" : "released";
1054 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1055 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1057 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1059 schedule_work(&event->wq);
1067 static void register_leds(struct led_t *l)
1069 struct proc_dir_entry *p;
1074 leds = proc_mkdir("led", diag);
1078 for(; l->name; l++) {
1079 if (l->gpio & gpiomask)
1082 if (l->gpio & GPIO_TYPE_EXTIF) {
1086 if (l->polarity != INPUT) oe_mask |= l->gpio;
1088 val |= (l->polarity == NORMAL)?0:l->gpio;
1091 if (l->polarity == INPUT) continue;
1093 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1094 l->proc.type = PROC_LED;
1096 p->data = (void *) &l->proc;
1097 p->proc_fops = &diag_proc_fops;
1101 gpio_outen(mask, oe_mask);
1102 gpio_control(mask, 0);
1103 gpio_out(mask, val);
1104 gpio_setintmask(mask, 0);
1107 static void unregister_leds(struct led_t *l)
1110 remove_proc_entry(l->name, leds);
1112 remove_proc_entry("led", diag);
1115 static void set_led_extif(struct led_t *led)
1117 gpio_set_extif(led->gpio, led->state);
1120 static void led_flash(unsigned long dummy) {
1125 for (l = platform.leds; l->name; l++) {
1127 if (l->gpio & GPIO_TYPE_EXTIF) {
1129 l->state = !l->state;
1139 u32 val = ~gpio_in();
1141 gpio_outen(mask, mask);
1142 gpio_control(mask, 0);
1143 gpio_out(mask, val);
1145 if (mask || extif_blink) {
1146 mod_timer(&led_timer, jiffies + FLASH_TIME);
1150 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1153 struct inode *inode = file->f_dentry->d_inode;
1154 struct proc_dir_entry *dent = inode->u.generic_ip;
1156 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1161 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1164 if (dent->data != NULL) {
1165 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1166 switch (handler->type) {
1168 struct led_t * led = (struct led_t *) handler->ptr;
1170 len = sprintf(page, "f\n");
1172 if (led->gpio & GPIO_TYPE_EXTIF) {
1173 len = sprintf(page, "%d\n", led->state);
1175 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1176 u8 p = (led->polarity == NORMAL ? 0 : 1);
1177 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1183 len = sprintf(page, "%s\n", platform.name);
1186 len = sprintf(page, "0x%04x\n", gpiomask);
1193 len = min_t(int, len - *ppos, count);
1194 if (copy_to_user(buf, (page + *ppos), len)) {
1208 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1211 struct inode *inode = file->f_dentry->d_inode;
1212 struct proc_dir_entry *dent = inode->u.generic_ip;
1214 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1219 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1222 if (copy_from_user(page, buf, count)) {
1228 if (dent->data != NULL) {
1229 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1230 switch (handler->type) {
1232 struct led_t *led = (struct led_t *) handler->ptr;
1233 int p = (led->polarity == NORMAL ? 0 : 1);
1235 if (page[0] == 'f') {
1240 if (led->gpio & GPIO_TYPE_EXTIF) {
1241 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1244 gpio_outen(led->gpio, led->gpio);
1245 gpio_control(led->gpio, 0);
1246 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1252 gpiomask = simple_strtoul(page, NULL, 0);
1254 if (platform.buttons) {
1255 unregister_buttons(platform.buttons);
1256 register_buttons(platform.buttons);
1259 if (platform.leds) {
1260 unregister_leds(platform.leds);
1261 register_leds(platform.leds);
1272 static int __init diag_init(void)
1274 static struct proc_dir_entry *p;
1275 static struct platform_t *detected;
1277 detected = platform_detect();
1279 printk(MODULE_NAME ": Router model not detected.\n");
1282 memcpy(&platform, detected, sizeof(struct platform_t));
1284 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1285 if (platform.platform_init != NULL) {
1286 platform.platform_init();
1289 if (!(diag = proc_mkdir("diag", NULL))) {
1290 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1294 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1295 p->data = (void *) &proc_model;
1296 p->proc_fops = &diag_proc_fops;
1299 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1300 p->data = (void *) &proc_gpiomask;
1301 p->proc_fops = &diag_proc_fops;
1304 if (platform.buttons)
1305 register_buttons(platform.buttons);
1308 register_leds(platform.leds);
1313 static void __exit diag_exit(void)
1315 del_timer(&led_timer);
1317 if (platform.buttons)
1318 unregister_buttons(platform.buttons);
1321 unregister_leds(platform.leds);
1323 remove_proc_entry("model", diag);
1324 remove_proc_entry("gpiomask", diag);
1325 remove_proc_entry("diag", NULL);
1328 module_init(diag_init);
1329 module_exit(diag_exit);
1331 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1332 MODULE_LICENSE("GPL");