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);
137 static void __init bcm4780_init(void) {
140 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
141 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
142 gpio_outen(pin, pin);
143 gpio_control(pin, 0);
146 /* Wait 5s, so the HDD can spin up */
147 set_current_state(TASK_INTERRUPTIBLE);
148 schedule_timeout(HZ * 5);
151 static void __init bcm57xx_init(void) {
155 /* FIXME: switch comes up, but port mappings/vlans not right */
156 gpio_outen(pin, pin);
157 gpio_control(pin, 0);
162 static struct platform_t __initdata platforms[] = {
165 .name = "Linksys WAP54G V1",
167 { .name = "reset", .gpio = 1 << 0 },
170 { .name = "diag", .gpio = 1 << 3 },
171 { .name = "wlan", .gpio = 1 << 4 },
175 .name = "Linksys WAP54G V2",
177 { .name = "reset", .gpio = 1 << 0 },
180 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
181 /* GPIO 6 is b44 (eth0, LAN) PHY power */
185 .name = "Linksys WAP54G V3",
187 /* FIXME: verify this */
188 { .name = "reset", .gpio = 1 << 7 },
189 { .name = "ses", .gpio = 1 << 0 },
193 { .name = "ses", .gpio = 1 << 1 },
197 .name = "Linksys WRT54G V1.x",
199 { .name = "reset", .gpio = 1 << 6 },
202 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
203 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
207 .name = "Linksys WRT54G/GS/GL",
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 << 7, .polarity = REVERSE },
215 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
216 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
217 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
221 .name = "Linksys WRTSL54GS",
223 { .name = "reset", .gpio = 1 << 6 },
224 { .name = "ses", .gpio = 1 << 4 },
227 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
228 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
229 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
230 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
234 .name = "Linksys WRT54G3G",
236 { .name = "reset", .gpio = 1 << 6 },
237 { .name = "3g", .gpio = 1 << 4 },
240 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
241 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
242 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
243 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
244 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
248 .name = "Linksys WRT160N",
250 { .name = "reset", .gpio = 1 << 6 },
251 { .name = "ses", .gpio = 1 << 4 },
254 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
255 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
256 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
260 .name = "Linksys WRT300N V1.1",
262 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
263 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
266 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
267 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
268 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
270 .platform_init = bcm57xx_init,
273 .name = "Linksys WRT350N",
275 { .name = "reset", .gpio = 1 << 6 },
276 { .name = "ses", .gpio = 1 << 8 },
279 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
280 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
281 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
282 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
283 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
285 .platform_init = bcm57xx_init,
288 .name = "Linksys WRT600N",
290 { .name = "reset", .gpio = 1 << 6 },
291 { .name = "ses", .gpio = 1 << 7 },
294 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
295 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
296 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
297 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
298 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
299 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
301 .platform_init = bcm57xx_init,
304 .name = "Linksys WRT600N V1.1",
306 { .name = "reset", .gpio = 1 << 6 },
307 { .name = "ses", .gpio = 1 << 7 },
310 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
311 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
312 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
313 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
314 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
315 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
317 .platform_init = bcm57xx_init,
320 .name = "Linksys WRT610N",
322 { .name = "reset", .gpio = 1 << 6 },
323 { .name = "ses", .gpio = 1 << 8 },
326 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
327 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
328 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
329 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
334 .name = "ASUS WL-HDD",
336 { .name = "reset", .gpio = 1 << 6 },
339 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
340 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
344 .name = "ASUS WL-300g",
346 { .name = "reset", .gpio = 1 << 6 },
349 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
353 .name = "ASUS WL-320gE/WL-320gP",
355 { .name = "reset", .gpio = 1 << 6 },
358 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
359 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
360 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
364 .name = "ASUS WL-330gE",
366 { .name = "reset", .gpio = 1 << 2 },
369 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
373 .name = "ASUS WL-500g",
375 { .name = "reset", .gpio = 1 << 6 },
378 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
382 .name = "ASUS WL-500g Deluxe",
384 { .name = "reset", .gpio = 1 << 6 },
387 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
391 .name = "ASUS WL-500g Premium",
393 { .name = "reset", .gpio = 1 << 0 },
394 { .name = "ses", .gpio = 1 << 4 },
397 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
401 .name = "ASUS WL-500g Premium V2",
403 { .name = "reset", .gpio = 1 << 2 },
404 { .name = "ses", .gpio = 1 << 3 },
407 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
408 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
412 .name = "ASUS WL-500W",
414 { .name = "reset", .gpio = 1 << 6 },
415 { .name = "ses", .gpio = 1 << 7 },
418 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
422 .name = "ASUS WL-520GC",
424 { .name = "reset", .gpio = 1 << 2 },
425 { .name = "ses", .gpio = 1 << 3 },
428 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
429 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
433 .name = "ASUS WL-520gU",
435 { .name = "reset", .gpio = 1 << 2 },
436 { .name = "ses", .gpio = 1 << 3 },
439 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
440 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
444 .name = "ASUS (unknown, BCM4702)",
446 { .name = "reset", .gpio = 1 << 6 },
449 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
453 .name = "ASUS WL-700gE",
455 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
456 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
457 { .name = "power", .gpio = 1 << 0 }, // on front
458 { .name = "copy", .gpio = 1 << 6 }, // on front
462 // GPIO that controls power led also enables/disables some essential functions
465 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
467 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
469 .platform_init = bcm4780_init,
473 .name = "Buffalo WHR-G54S",
475 { .name = "reset", .gpio = 1 << 4 },
476 { .name = "bridge", .gpio = 1 << 5 },
477 { .name = "ses", .gpio = 1 << 0 },
480 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
481 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
482 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
483 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
484 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
488 .name = "Buffalo WBR2-G54",
491 { .name = "reset", .gpio = 1 << 7 },
494 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
498 .name = "Buffalo WHR-HP-G54",
500 { .name = "reset", .gpio = 1 << 4 },
501 { .name = "bridge", .gpio = 1 << 5 },
502 { .name = "ses", .gpio = 1 << 0 },
505 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
506 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
507 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
508 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
509 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
513 .name = "Buffalo WHR-G125",
515 { .name = "reset", .gpio = 1 << 4 },
516 { .name = "bridge", .gpio = 1 << 5 },
517 { .name = "ses", .gpio = 1 << 0 },
520 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
521 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
522 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
523 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
524 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
528 .name = "Buffalo WHR2-A54G54",
530 { .name = "reset", .gpio = 1 << 4 },
533 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
537 .name = "Buffalo WLA2-G54L",
540 { .name = "reset", .gpio = 1 << 7 },
543 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
547 .name = "Buffalo WZR-G300N",
549 { .name = "reset", .gpio = 1 << 4 },
552 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
553 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
554 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
558 .name = "Buffalo WZR-RS-G54",
560 { .name = "ses", .gpio = 1 << 0 },
561 { .name = "reset", .gpio = 1 << 4 },
564 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
565 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
566 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
570 .name = "Buffalo WZR-RS-G54HP",
572 { .name = "ses", .gpio = 1 << 0 },
573 { .name = "reset", .gpio = 1 << 4 },
576 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
577 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
578 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
581 [BUFFALO_UNKNOWN] = {
582 .name = "Buffalo (unknown)",
584 { .name = "reset", .gpio = 1 << 7 },
587 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
590 [BUFFALO_UNKNOWN_4710] = {
591 .name = "Buffalo (unknown, BCM4710)",
593 { .name = "reset", .gpio = 1 << 4 },
596 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
601 .name = "Siemens SE505 V1",
603 /* No usable buttons */
606 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
607 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
608 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
612 .name = "Siemens SE505 V2",
614 /* No usable buttons */
617 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
618 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
619 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
624 .name = "U.S. Robotics USR5461",
626 /* No usable buttons */
629 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
630 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
635 .name = "Dell TrueMobile 2300",
637 { .name = "reset", .gpio = 1 << 0 },
640 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
641 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
645 .name = "Dell TrueMobile 2300 v2",
647 { .name = "reset", .gpio = 1 << 0 },
650 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
651 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
656 .name = "Motorola WE800G",
658 { .name = "reset", .gpio = 1 << 0 },
661 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
662 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
663 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
667 .name = "Motorola WR850G V1",
669 { .name = "reset", .gpio = 1 << 0 },
672 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
673 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
674 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
675 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
676 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
680 .name = "Motorola WR850G V2/V3",
682 { .name = "reset", .gpio = 1 << 5 },
685 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
686 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
687 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
688 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
692 .name = "Motorola WR850GP",
694 { .name = "reset", .gpio = 1 << 5 },
697 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
698 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
699 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
700 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
706 .name = "Belkin (unknown)",
707 /* FIXME: verify & add detection */
709 { .name = "reset", .gpio = 1 << 7 },
712 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
713 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
714 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
719 .name = "Netgear WGT634U",
721 { .name = "reset", .gpio = 1 << 2 },
724 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
729 .name = "Trendware TEW411BRP+",
731 { /* No usable buttons */ },
734 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
735 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
736 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
741 .name = "SimpleTech SimpleShare NAS",
743 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
744 { .name = "power", .gpio = 1 << 0 }, // on back
747 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
749 .platform_init = bcm4780_init,
753 .name = "D-Link DIR-130",
755 { .name = "reset", .gpio = 1 << 3},
756 { .name = "reserved", .gpio = 1 << 7},
759 { .name = "diag", .gpio = 1 << 0},
760 { .name = "blue", .gpio = 1 << 6},
764 .name = "D-Link DIR-320",
766 { .name = "reserved", .gpio = 1 << 6},
767 { .name = "reset", .gpio = 1 << 7},
770 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
771 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
772 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
773 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
774 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
778 .name = "D-Link DIR-330",
780 { .name = "reset", .gpio = 1 << 3},
781 { .name = "reserved", .gpio = 1 << 7},
784 { .name = "diag", .gpio = 1 << 0},
785 { .name = "usb", .gpio = 1 << 4},
786 { .name = "blue", .gpio = 1 << 6},
790 .name = "D-Link DWL-3150",
792 { .name = "reset", .gpio = 1 << 7},
795 { .name = "diag", .gpio = 1 << 2},
796 { .name = "status", .gpio = 1 << 1},
801 .name = "Sitecom WL-105b",
803 { .name = "reset", .gpio = 1 << 10},
806 { .name = "wlan", .gpio = 1 << 4},
807 { .name = "power", .gpio = 1 << 3},
812 static struct platform_t __init *platform_detect(void)
814 char *boardnum, *boardtype, *buf;
816 if (strcmp(getvar("nvram_type"), "cfe") == 0)
817 return &platforms[WGT634U];
819 /* Look for a model identifier */
821 /* Based on "model_name" */
822 if ((buf = nvram_get("model_name"))) {
823 if (!strcmp(buf, "DIR-130"))
824 return &platforms[DIR130];
825 if (!strcmp(buf, "DIR-330"))
826 return &platforms[DIR330];
829 /* Based on "wsc_modelname */
830 if ((buf = nvram_get("wsc_modelname"))) {
831 if (!strcmp(buf, "WRT610N"))
832 return &platforms[WRT610N];
835 /* Based on "model_no" */
836 if ((buf = nvram_get("model_no"))) {
837 if (startswith(buf,"WL700")) /* WL700* */
838 return &platforms[WL700GE];
841 /* Based on "hardware_version" */
842 if ((buf = nvram_get("hardware_version"))) {
843 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
844 return &platforms[WL500GPV2];
845 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
846 return &platforms[WL520GC];
847 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
848 return &platforms[WL520GU];
849 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
850 return &platforms[WL330GE];
853 /* Based on "ModelId" */
854 if ((buf = nvram_get("ModelId"))) {
855 if (!strcmp(buf, "WR850GP"))
856 return &platforms[WR850GP];
857 if (!strcmp(buf, "WR850G"))
858 return &platforms[WR850GV2V3];
859 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
860 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
861 if (startswith(buf,"WE800G")) /* WE800G* */
862 return &platforms[WE800G];
866 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
867 /* Buffalo hardware, check id for specific hardware matches */
868 if (!strcmp(buf, "29bb0332"))
869 return &platforms[WBR2_G54];
870 if (!strcmp(buf, "29129"))
871 return &platforms[WLA2_G54L];
872 if (!strcmp(buf, "30189"))
873 return &platforms[WHR_HP_G54];
874 if (!strcmp(buf, "32093"))
875 return &platforms[WHR_G125];
876 if (!strcmp(buf, "30182"))
877 return &platforms[WHR_G54S];
878 if (!strcmp(buf, "290441dd"))
879 return &platforms[WHR2_A54G54];
880 if (!strcmp(buf, "31120"))
881 return &platforms[WZR_G300N];
882 if (!strcmp(buf, "30083"))
883 return &platforms[WZR_RS_G54];
884 if (!strcmp(buf, "30103"))
885 return &platforms[WZR_RS_G54HP];
888 /* no easy model number, attempt to guess */
889 boardnum = getvar("boardnum");
890 boardtype = getvar("boardtype");
892 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
893 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
894 return &platforms[WRT600NV11];
896 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
897 return &platforms[WRT600N];
900 if (startswith(getvar("pmon_ver"), "CFE")) {
901 /* CFE based - newer hardware */
902 if (!strcmp(boardnum, "42")) { /* Linksys */
903 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
904 return &platforms[WRT300NV11];
906 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
907 return &platforms[WRT350N];
909 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
910 return &platforms[WRT54G3G];
912 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
913 return &platforms[WRTSL54GS];
915 if (!strcmp(boardtype, "0x0472"))
916 return &platforms[WRT160N];
918 /* default to WRT54G */
919 return &platforms[WRT54G];
921 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
922 return &platforms[WAP54GV2];
924 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
925 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
926 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
929 if (!strcmp(boardnum, "45")) { /* ASUS */
930 if (!strcmp(boardtype,"0x042f"))
931 return &platforms[WL500GP];
932 else if (!strcmp(boardtype,"0x0472"))
933 return &platforms[WL500W];
934 else if (!strcmp(boardtype,"0x467"))
935 return &platforms[WL320GE];
937 return &platforms[WL500GD];
940 if (!strcmp(boardnum, "10496"))
941 return &platforms[USR5461];
943 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
944 return &platforms[SE505V2];
946 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
947 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
948 return &platforms[DIR320];
950 } else { /* PMON based - old stuff */
951 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
952 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
953 return &platforms[WR850GV1];
955 if (startswith(boardtype, "bcm94710dev")) {
956 if (!strcmp(boardnum, "42"))
957 return &platforms[WRT54GV1];
958 if (simple_strtoul(boardnum, NULL, 0) == 2)
959 return &platforms[WAP54GV1];
961 if (startswith(getvar("hardware_version"), "WL500-"))
962 return &platforms[WL500G];
963 if (startswith(getvar("hardware_version"), "WL300-")) {
964 /* Either WL-300g or WL-HDD, do more extensive checks */
965 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
966 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
967 return &platforms[WLHDD];
968 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
969 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
970 return &platforms[WL300G];
972 /* Sitecom WL-105b */
973 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
974 return &platforms[WL105B];
976 /* unknown asus stuff, probably bcm4702 */
977 if (startswith(boardnum, "asusX"))
978 return &platforms[ASUS_4702];
981 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
982 if (startswith(boardtype, "bcm94710ap"))
983 return &platforms[BUFFALO_UNKNOWN_4710];
985 return &platforms[BUFFALO_UNKNOWN];
988 if (startswith(getvar("CFEver"), "MotoWRv2") ||
989 startswith(getvar("CFEver"), "MotoWRv3") ||
990 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
992 return &platforms[WR850GV2V3];
995 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
996 return &platforms[TEW411BRPP];
999 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
1000 return &platforms[STI_NAS];
1002 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1003 return &platforms[DWL3150];
1009 static void register_buttons(struct button_t *b)
1011 for (; b->name; b++)
1012 platform.button_mask |= b->gpio;
1014 platform.button_mask &= ~gpiomask;
1016 gpio_outen(platform.button_mask, 0);
1017 gpio_control(platform.button_mask, 0);
1018 platform.button_polarity = gpio_in() & platform.button_mask;
1019 gpio_intpolarity(platform.button_mask, platform.button_polarity);
1020 gpio_setintmask(platform.button_mask, platform.button_mask);
1022 gpio_set_irqenable(1, button_handler);
1025 static void unregister_buttons(struct button_t *b)
1027 gpio_setintmask(platform.button_mask, 0);
1029 gpio_set_irqenable(0, button_handler);
1034 static void add_msg(struct event_t *event, char *msg, int argv)
1041 s = skb_put(event->skb, strlen(msg) + 1);
1045 static void hotplug_button(struct work_struct *work)
1047 struct event_t *event = container_of(work, struct event_t, wq);
1053 event->skb = alloc_skb(2048, GFP_KERNEL);
1055 s = skb_put(event->skb, strlen(event->action) + 2);
1056 sprintf(s, "%s@", event->action);
1059 NETLINK_CB(event->skb).dst_group = 1;
1060 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1065 #else /* !LINUX_2_4 */
1066 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1070 p = kmalloc(size, gfp);
1079 static void add_msg(struct event_t *event, char *msg, int argv)
1082 event->argv[event->anr++] = event->scratch;
1084 event->envp[event->enr++] = event->scratch;
1086 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1089 static void hotplug_button(struct event_t *event)
1091 char *scratch = kzalloc(256, GFP_KERNEL);
1092 event->scratch = scratch;
1094 add_msg(event, hotplug_path, 1);
1095 add_msg(event, "button", 1);
1097 call_usermodehelper (event->argv[0], event->argv, event->envp);
1101 #endif /* !LINUX_2_4 */
1103 static int fill_event (struct event_t *event)
1105 static char buf[128];
1107 add_msg(event, "HOME=/", 0);
1108 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1109 add_msg(event, "SUBSYSTEM=button", 0);
1110 snprintf(buf, 128, "ACTION=%s", event->action);
1111 add_msg(event, buf, 0);
1112 snprintf(buf, 128, "BUTTON=%s", event->name);
1113 add_msg(event, buf, 0);
1114 snprintf(buf, 128, "SEEN=%ld", event->seen);
1115 add_msg(event, buf, 0);
1117 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1118 add_msg(event, buf, 0);
1126 static irqreturn_t button_handler(int irq, void *dev_id)
1128 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1134 in = gpio_in() & platform.button_mask;
1135 gpio_intpolarity(platform.button_mask, in);
1136 changed = platform.button_polarity ^ in;
1137 platform.button_polarity = in;
1139 changed &= ~gpio_outen(0, 0);
1141 for (b = platform.buttons; b->name; b++) {
1142 struct event_t *event;
1144 if (!(b->gpio & changed)) continue;
1148 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1149 event->seen = (jiffies - b->seen)/HZ;
1150 event->name = b->name;
1151 event->action = b->pressed ? "pressed" : "released";
1152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1153 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1155 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1157 schedule_work(&event->wq);
1165 static void register_leds(struct led_t *l)
1167 struct proc_dir_entry *p;
1172 leds = proc_mkdir("led", diag);
1176 for(; l->name; l++) {
1177 if (l->gpio & gpiomask)
1180 if (l->gpio & GPIO_TYPE_EXTIF) {
1184 if (l->polarity != INPUT) oe_mask |= l->gpio;
1186 val |= (l->polarity == NORMAL)?0:l->gpio;
1189 if (l->polarity == INPUT) continue;
1191 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1192 l->proc.type = PROC_LED;
1194 p->data = (void *) &l->proc;
1195 p->proc_fops = &diag_proc_fops;
1199 gpio_outen(mask, oe_mask);
1200 gpio_control(mask, 0);
1201 gpio_out(mask, val);
1202 gpio_setintmask(mask, 0);
1205 static void unregister_leds(struct led_t *l)
1208 remove_proc_entry(l->name, leds);
1210 remove_proc_entry("led", diag);
1213 static void set_led_extif(struct led_t *led)
1215 gpio_set_extif(led->gpio, led->state);
1218 static void led_flash(unsigned long dummy) {
1223 for (l = platform.leds; l->name; l++) {
1225 if (l->gpio & GPIO_TYPE_EXTIF) {
1227 l->state = !l->state;
1237 u32 val = ~gpio_in();
1239 gpio_outen(mask, mask);
1240 gpio_control(mask, 0);
1241 gpio_out(mask, val);
1243 if (mask || extif_blink) {
1244 mod_timer(&led_timer, jiffies + FLASH_TIME);
1248 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1251 struct inode *inode = file->f_dentry->d_inode;
1252 struct proc_dir_entry *dent = inode->u.generic_ip;
1254 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1259 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1262 if (dent->data != NULL) {
1263 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1264 switch (handler->type) {
1266 struct led_t * led = (struct led_t *) handler->ptr;
1268 len = sprintf(page, "f\n");
1270 if (led->gpio & GPIO_TYPE_EXTIF) {
1271 len = sprintf(page, "%d\n", led->state);
1273 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1274 u8 p = (led->polarity == NORMAL ? 0 : 1);
1275 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1281 len = sprintf(page, "%s\n", platform.name);
1284 len = sprintf(page, "0x%04x\n", gpiomask);
1291 len = min_t(int, len - *ppos, count);
1292 if (copy_to_user(buf, (page + *ppos), len)) {
1306 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1309 struct inode *inode = file->f_dentry->d_inode;
1310 struct proc_dir_entry *dent = inode->u.generic_ip;
1312 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1317 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1320 if (copy_from_user(page, buf, count)) {
1326 if (dent->data != NULL) {
1327 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1328 switch (handler->type) {
1330 struct led_t *led = (struct led_t *) handler->ptr;
1331 int p = (led->polarity == NORMAL ? 0 : 1);
1333 if (page[0] == 'f') {
1338 if (led->gpio & GPIO_TYPE_EXTIF) {
1339 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1342 gpio_outen(led->gpio, led->gpio);
1343 gpio_control(led->gpio, 0);
1344 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1350 gpiomask = simple_strtoul(page, NULL, 0);
1352 if (platform.buttons) {
1353 unregister_buttons(platform.buttons);
1354 register_buttons(platform.buttons);
1357 if (platform.leds) {
1358 unregister_leds(platform.leds);
1359 register_leds(platform.leds);
1370 static int __init diag_init(void)
1372 static struct proc_dir_entry *p;
1373 static struct platform_t *detected;
1375 detected = platform_detect();
1377 printk(MODULE_NAME ": Router model not detected.\n");
1380 memcpy(&platform, detected, sizeof(struct platform_t));
1382 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1383 if (platform.platform_init != NULL) {
1384 platform.platform_init();
1387 if (!(diag = proc_mkdir("diag", NULL))) {
1388 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1392 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1393 p->data = (void *) &proc_model;
1394 p->proc_fops = &diag_proc_fops;
1397 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1398 p->data = (void *) &proc_gpiomask;
1399 p->proc_fops = &diag_proc_fops;
1402 if (platform.buttons)
1403 register_buttons(platform.buttons);
1406 register_leds(platform.leds);
1411 static void __exit diag_exit(void)
1413 del_timer(&led_timer);
1415 if (platform.buttons)
1416 unregister_buttons(platform.buttons);
1419 unregister_leds(platform.leds);
1421 remove_proc_entry("model", diag);
1422 remove_proc_entry("gpiomask", diag);
1423 remove_proc_entry("diag", NULL);
1426 module_init(diag_init);
1427 module_exit(diag_exit);
1429 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1430 MODULE_LICENSE("GPL");