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);
134 static void __init bcm4780_init(void) {
137 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
138 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
139 gpio_outen(pin, pin);
140 gpio_control(pin, 0);
143 /* Wait 5s, so the HDD can spin up */
144 set_current_state(TASK_INTERRUPTIBLE);
145 schedule_timeout(HZ * 5);
148 static void __init bcm57xx_init(void) {
152 /* FIXME: switch comes up, but port mappings/vlans not right */
153 gpio_outen(pin, pin);
154 gpio_control(pin, 0);
159 static struct platform_t __initdata platforms[] = {
162 .name = "Linksys WAP54G V1",
164 { .name = "reset", .gpio = 1 << 0 },
167 { .name = "diag", .gpio = 1 << 3 },
168 { .name = "wlan", .gpio = 1 << 4 },
172 .name = "Linksys WAP54G V2",
174 { .name = "reset", .gpio = 1 << 0 },
177 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
178 /* GPIO 6 is b44 (eth0, LAN) PHY power */
182 .name = "Linksys WAP54G V3",
184 /* FIXME: verify this */
185 { .name = "reset", .gpio = 1 << 7 },
186 { .name = "ses", .gpio = 1 << 0 },
190 { .name = "ses", .gpio = 1 << 1 },
194 .name = "Linksys WRT54G V1.x",
196 { .name = "reset", .gpio = 1 << 6 },
199 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
200 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
204 .name = "Linksys WRT54G/GS/GL",
206 { .name = "reset", .gpio = 1 << 6 },
207 { .name = "ses", .gpio = 1 << 4 },
210 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
211 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
212 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
213 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
214 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
218 .name = "Linksys WRTSL54GS",
220 { .name = "reset", .gpio = 1 << 6 },
221 { .name = "ses", .gpio = 1 << 4 },
224 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
225 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
226 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
227 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
231 .name = "Linksys WRT54G3G",
233 { .name = "reset", .gpio = 1 << 6 },
234 { .name = "3g", .gpio = 1 << 4 },
237 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
238 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
239 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
240 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
241 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
245 .name = "Linksys WRT350N",
247 { .name = "reset", .gpio = 1 << 6 },
248 { .name = "ses", .gpio = 1 << 8 },
251 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
252 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
253 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
254 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
255 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
257 .platform_init = bcm57xx_init,
260 .name = "Linksys WRT600N",
262 { .name = "reset", .gpio = 1 << 6 },
263 { .name = "ses", .gpio = 1 << 7 },
266 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
267 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
268 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
269 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
270 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
271 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
273 .platform_init = bcm57xx_init,
276 .name = "Linksys WRT600N V1.1",
278 { .name = "reset", .gpio = 1 << 6 },
279 { .name = "ses", .gpio = 1 << 7 },
282 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
283 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
284 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
285 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
286 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
287 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
289 .platform_init = bcm57xx_init,
293 .name = "ASUS WL-HDD",
295 { .name = "reset", .gpio = 1 << 6 },
298 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
299 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
303 .name = "ASUS WL-300g",
305 { .name = "reset", .gpio = 1 << 6 },
308 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
312 .name = "ASUS WL-320gE/WL-320gP",
314 { .name = "reset", .gpio = 1 << 6 },
317 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
318 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
319 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
323 .name = "ASUS WL-330gE",
325 { .name = "reset", .gpio = 1 << 2 },
328 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
332 .name = "ASUS WL-500g",
334 { .name = "reset", .gpio = 1 << 6 },
337 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
341 .name = "ASUS WL-500g Deluxe",
343 { .name = "reset", .gpio = 1 << 6 },
346 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
350 .name = "ASUS WL-500g Premium",
352 { .name = "reset", .gpio = 1 << 0 },
353 { .name = "ses", .gpio = 1 << 4 },
356 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
360 .name = "ASUS WL-500g Premium V2",
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-500W",
373 { .name = "reset", .gpio = 1 << 6 },
374 { .name = "ses", .gpio = 1 << 7 },
377 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
381 .name = "ASUS WL-520GC",
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 WL-520gU",
394 { .name = "reset", .gpio = 1 << 2 },
395 { .name = "ses", .gpio = 1 << 3 },
398 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
399 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
403 .name = "ASUS (unknown, BCM4702)",
405 { .name = "reset", .gpio = 1 << 6 },
408 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
412 .name = "ASUS WL-700gE",
414 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
415 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
416 { .name = "power", .gpio = 1 << 0 }, // on front
417 { .name = "copy", .gpio = 1 << 6 }, // on front
421 // GPIO that controls power led also enables/disables some essential functions
424 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
426 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
428 .platform_init = bcm4780_init,
432 .name = "Buffalo WHR-G54S",
434 { .name = "reset", .gpio = 1 << 4 },
435 { .name = "bridge", .gpio = 1 << 5 },
436 { .name = "ses", .gpio = 1 << 0 },
439 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
440 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
441 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
442 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
443 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
447 .name = "Buffalo WBR2-G54",
450 { .name = "reset", .gpio = 1 << 7 },
453 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
457 .name = "Buffalo WHR-HP-G54",
459 { .name = "reset", .gpio = 1 << 4 },
460 { .name = "bridge", .gpio = 1 << 5 },
461 { .name = "ses", .gpio = 1 << 0 },
464 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
465 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
466 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
467 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
468 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
472 .name = "Buffalo WHR-G125",
474 { .name = "reset", .gpio = 1 << 4 },
475 { .name = "bridge", .gpio = 1 << 5 },
476 { .name = "ses", .gpio = 1 << 0 },
479 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
480 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
481 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
482 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
483 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
487 .name = "Buffalo WHR2-A54G54",
489 { .name = "reset", .gpio = 1 << 4 },
492 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
496 .name = "Buffalo WLA2-G54L",
499 { .name = "reset", .gpio = 1 << 7 },
502 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
506 .name = "Buffalo WZR-G300N",
508 { .name = "reset", .gpio = 1 << 4 },
511 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
512 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
513 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
517 .name = "Buffalo WZR-RS-G54",
519 { .name = "ses", .gpio = 1 << 0 },
520 { .name = "reset", .gpio = 1 << 4 },
523 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
524 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
525 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
529 .name = "Buffalo WZR-RS-G54HP",
531 { .name = "ses", .gpio = 1 << 0 },
532 { .name = "reset", .gpio = 1 << 4 },
535 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
536 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
537 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
540 [BUFFALO_UNKNOWN] = {
541 .name = "Buffalo (unknown)",
543 { .name = "reset", .gpio = 1 << 7 },
546 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
549 [BUFFALO_UNKNOWN_4710] = {
550 .name = "Buffalo (unknown, BCM4710)",
552 { .name = "reset", .gpio = 1 << 4 },
555 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
560 .name = "Siemens SE505 V1",
562 /* No usable buttons */
565 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
566 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
567 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
571 .name = "Siemens SE505 V2",
573 /* No usable buttons */
576 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
577 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
578 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
583 .name = "U.S. Robotics USR5461",
585 /* No usable buttons */
588 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
589 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
594 .name = "Dell TrueMobile 2300",
596 { .name = "reset", .gpio = 1 << 0 },
599 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
600 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
604 .name = "Dell TrueMobile 2300 v2",
606 { .name = "reset", .gpio = 1 << 0 },
609 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
610 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
615 .name = "Motorola WE800G",
617 { .name = "reset", .gpio = 1 << 0 },
620 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
621 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
622 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
626 .name = "Motorola WR850G V1",
628 { .name = "reset", .gpio = 1 << 0 },
631 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
632 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
633 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
634 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
635 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
639 .name = "Motorola WR850G V2/V3",
641 { .name = "reset", .gpio = 1 << 5 },
644 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
645 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
646 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
647 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
651 .name = "Motorola WR850GP",
653 { .name = "reset", .gpio = 1 << 5 },
656 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
657 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
658 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
659 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
665 .name = "Belkin (unknown)",
666 /* FIXME: verify & add detection */
668 { .name = "reset", .gpio = 1 << 7 },
671 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
672 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
673 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
678 .name = "Netgear WGT634U",
680 { .name = "reset", .gpio = 1 << 2 },
683 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
688 .name = "Trendware TEW411BRP+",
690 { /* No usable buttons */ },
693 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
694 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
695 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
700 .name = "SimpleTech SimpleShare NAS",
702 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
703 { .name = "power", .gpio = 1 << 0 }, // on back
706 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
708 .platform_init = bcm4780_init,
712 .name = "D-Link DIR-130",
714 { .name = "reset", .gpio = 1 << 3},
715 { .name = "reserved", .gpio = 1 << 7},
718 { .name = "diag", .gpio = 1 << 0},
719 { .name = "blue", .gpio = 1 << 6},
723 .name = "D-Link DIR-320",
725 { .name = "reserved", .gpio = 1 << 6},
726 { .name = "reset", .gpio = 1 << 7},
729 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
730 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
731 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
732 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
733 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
737 .name = "D-Link DIR-330",
739 { .name = "reset", .gpio = 1 << 3},
740 { .name = "reserved", .gpio = 1 << 7},
743 { .name = "diag", .gpio = 1 << 0},
744 { .name = "usb", .gpio = 1 << 4},
745 { .name = "blue", .gpio = 1 << 6},
749 .name = "D-Link DWL-3150",
751 { .name = "reset", .gpio = 1 << 7},
754 { .name = "diag", .gpio = 1 << 2},
755 { .name = "status", .gpio = 1 << 1},
760 .name = "Sitecom WL-105b",
762 { .name = "reset", .gpio = 1 << 10},
765 { .name = "wlan", .gpio = 1 << 4},
766 { .name = "power", .gpio = 1 << 3},
771 static struct platform_t __init *platform_detect(void)
773 char *boardnum, *boardtype, *buf;
775 if (strcmp(getvar("nvram_type"), "cfe") == 0)
776 return &platforms[WGT634U];
778 /* Look for a model identifier */
780 /* Based on "model_name" */
781 if ((buf = nvram_get("model_name"))) {
782 if (!strcmp(buf, "DIR-130"))
783 return &platforms[DIR130];
784 if (!strcmp(buf, "DIR-330"))
785 return &platforms[DIR330];
788 /* Based on "model_no" */
789 if ((buf = nvram_get("model_no"))) {
790 if (startswith(buf,"WL700")) /* WL700* */
791 return &platforms[WL700GE];
794 /* Based on "hardware_version" */
795 if ((buf = nvram_get("hardware_version"))) {
796 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
797 return &platforms[WL500GPV2];
798 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
799 return &platforms[WL520GC];
800 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
801 return &platforms[WL520GU];
802 if (startswith(buf,"WL330GE-")) /* WL330GE-* */
803 return &platforms[WL330GE];
806 /* Based on "ModelId" */
807 if ((buf = nvram_get("ModelId"))) {
808 if (!strcmp(buf, "WR850GP"))
809 return &platforms[WR850GP];
810 if (!strcmp(buf, "WR850G"))
811 return &platforms[WR850GV2V3];
812 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
813 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
814 if (startswith(buf,"WE800G")) /* WE800G* */
815 return &platforms[WE800G];
819 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
820 /* Buffalo hardware, check id for specific hardware matches */
821 if (!strcmp(buf, "29bb0332"))
822 return &platforms[WBR2_G54];
823 if (!strcmp(buf, "29129"))
824 return &platforms[WLA2_G54L];
825 if (!strcmp(buf, "30189"))
826 return &platforms[WHR_HP_G54];
827 if (!strcmp(buf, "32093"))
828 return &platforms[WHR_G125];
829 if (!strcmp(buf, "30182"))
830 return &platforms[WHR_G54S];
831 if (!strcmp(buf, "290441dd"))
832 return &platforms[WHR2_A54G54];
833 if (!strcmp(buf, "31120"))
834 return &platforms[WZR_G300N];
835 if (!strcmp(buf, "30083"))
836 return &platforms[WZR_RS_G54];
837 if (!strcmp(buf, "30103"))
838 return &platforms[WZR_RS_G54HP];
841 /* no easy model number, attempt to guess */
842 boardnum = getvar("boardnum");
843 boardtype = getvar("boardtype");
845 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
846 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
847 return &platforms[WRT600NV11];
849 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
850 return &platforms[WRT600N];
853 if (startswith(getvar("pmon_ver"), "CFE")) {
854 /* CFE based - newer hardware */
855 if (!strcmp(boardnum, "42")) { /* Linksys */
856 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
857 return &platforms[WRT350N];
859 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
860 return &platforms[WRT54G3G];
862 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
863 return &platforms[WRTSL54GS];
865 /* default to WRT54G */
866 return &platforms[WRT54G];
868 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
869 return &platforms[WAP54GV2];
871 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
872 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
873 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
876 if (!strcmp(boardnum, "45")) { /* ASUS */
877 if (!strcmp(boardtype,"0x042f"))
878 return &platforms[WL500GP];
879 else if (!strcmp(boardtype,"0x0472"))
880 return &platforms[WL500W];
881 else if (!strcmp(boardtype,"0x467"))
882 return &platforms[WL320GE];
884 return &platforms[WL500GD];
887 if (!strcmp(boardnum, "10496"))
888 return &platforms[USR5461];
890 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
891 return &platforms[SE505V2];
893 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
894 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
895 return &platforms[DIR320];
897 } else { /* PMON based - old stuff */
898 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
899 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
900 return &platforms[WR850GV1];
902 if (startswith(boardtype, "bcm94710dev")) {
903 if (!strcmp(boardnum, "42"))
904 return &platforms[WRT54GV1];
905 if (simple_strtoul(boardnum, NULL, 0) == 2)
906 return &platforms[WAP54GV1];
908 if (startswith(getvar("hardware_version"), "WL500-"))
909 return &platforms[WL500G];
910 if (startswith(getvar("hardware_version"), "WL300-")) {
911 /* Either WL-300g or WL-HDD, do more extensive checks */
912 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
913 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
914 return &platforms[WLHDD];
915 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
916 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
917 return &platforms[WL300G];
919 /* Sitecom WL-105b */
920 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
921 return &platforms[WL105B];
923 /* unknown asus stuff, probably bcm4702 */
924 if (startswith(boardnum, "asusX"))
925 return &platforms[ASUS_4702];
928 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
929 if (startswith(boardtype, "bcm94710ap"))
930 return &platforms[BUFFALO_UNKNOWN_4710];
932 return &platforms[BUFFALO_UNKNOWN];
935 if (startswith(getvar("CFEver"), "MotoWRv2") ||
936 startswith(getvar("CFEver"), "MotoWRv3") ||
937 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
939 return &platforms[WR850GV2V3];
942 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
943 return &platforms[TEW411BRPP];
946 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
947 return &platforms[STI_NAS];
949 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
950 return &platforms[DWL3150];
956 static void register_buttons(struct button_t *b)
959 platform.button_mask |= b->gpio;
961 platform.button_mask &= ~gpiomask;
963 gpio_outen(platform.button_mask, 0);
964 gpio_control(platform.button_mask, 0);
965 platform.button_polarity = gpio_in() & platform.button_mask;
966 gpio_intpolarity(platform.button_mask, platform.button_polarity);
967 gpio_setintmask(platform.button_mask, platform.button_mask);
969 gpio_set_irqenable(1, button_handler);
972 static void unregister_buttons(struct button_t *b)
974 gpio_setintmask(platform.button_mask, 0);
976 gpio_set_irqenable(0, button_handler);
981 static void add_msg(struct event_t *event, char *msg, int argv)
988 s = skb_put(event->skb, strlen(msg) + 1);
992 static void hotplug_button(struct work_struct *work)
994 struct event_t *event = container_of(work, struct event_t, wq);
1000 event->skb = alloc_skb(2048, GFP_KERNEL);
1002 s = skb_put(event->skb, strlen(event->action) + 2);
1003 sprintf(s, "%s@", event->action);
1006 NETLINK_CB(event->skb).dst_group = 1;
1007 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1012 #else /* !LINUX_2_4 */
1013 static inline char *kzalloc(unsigned int size, unsigned int gfp)
1017 p = kmalloc(size, gfp);
1026 static void add_msg(struct event_t *event, char *msg, int argv)
1029 event->argv[event->anr++] = event->scratch;
1031 event->envp[event->enr++] = event->scratch;
1033 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1036 static void hotplug_button(struct event_t *event)
1038 char *scratch = kzalloc(256, GFP_KERNEL);
1039 event->scratch = scratch;
1041 add_msg(event, hotplug_path, 1);
1042 add_msg(event, "button", 1);
1044 call_usermodehelper (event->argv[0], event->argv, event->envp);
1048 #endif /* !LINUX_2_4 */
1050 static int fill_event (struct event_t *event)
1052 static char buf[128];
1054 add_msg(event, "HOME=/", 0);
1055 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1056 add_msg(event, "SUBSYSTEM=button", 0);
1057 snprintf(buf, 128, "ACTION=%s", event->action);
1058 add_msg(event, buf, 0);
1059 snprintf(buf, 128, "BUTTON=%s", event->name);
1060 add_msg(event, buf, 0);
1061 snprintf(buf, 128, "SEEN=%ld", event->seen);
1062 add_msg(event, buf, 0);
1064 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1065 add_msg(event, buf, 0);
1073 static irqreturn_t button_handler(int irq, void *dev_id)
1075 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1081 in = gpio_in() & platform.button_mask;
1082 gpio_intpolarity(platform.button_mask, in);
1083 changed = platform.button_polarity ^ in;
1084 platform.button_polarity = in;
1086 changed &= ~gpio_outen(0, 0);
1088 for (b = platform.buttons; b->name; b++) {
1089 struct event_t *event;
1091 if (!(b->gpio & changed)) continue;
1095 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1096 event->seen = (jiffies - b->seen)/HZ;
1097 event->name = b->name;
1098 event->action = b->pressed ? "pressed" : "released";
1099 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1100 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1102 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1104 schedule_work(&event->wq);
1112 static void register_leds(struct led_t *l)
1114 struct proc_dir_entry *p;
1119 leds = proc_mkdir("led", diag);
1123 for(; l->name; l++) {
1124 if (l->gpio & gpiomask)
1127 if (l->gpio & GPIO_TYPE_EXTIF) {
1131 if (l->polarity != INPUT) oe_mask |= l->gpio;
1133 val |= (l->polarity == NORMAL)?0:l->gpio;
1136 if (l->polarity == INPUT) continue;
1138 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1139 l->proc.type = PROC_LED;
1141 p->data = (void *) &l->proc;
1142 p->proc_fops = &diag_proc_fops;
1146 gpio_outen(mask, oe_mask);
1147 gpio_control(mask, 0);
1148 gpio_out(mask, val);
1149 gpio_setintmask(mask, 0);
1152 static void unregister_leds(struct led_t *l)
1155 remove_proc_entry(l->name, leds);
1157 remove_proc_entry("led", diag);
1160 static void set_led_extif(struct led_t *led)
1162 gpio_set_extif(led->gpio, led->state);
1165 static void led_flash(unsigned long dummy) {
1170 for (l = platform.leds; l->name; l++) {
1172 if (l->gpio & GPIO_TYPE_EXTIF) {
1174 l->state = !l->state;
1184 u32 val = ~gpio_in();
1186 gpio_outen(mask, mask);
1187 gpio_control(mask, 0);
1188 gpio_out(mask, val);
1190 if (mask || extif_blink) {
1191 mod_timer(&led_timer, jiffies + FLASH_TIME);
1195 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1198 struct inode *inode = file->f_dentry->d_inode;
1199 struct proc_dir_entry *dent = inode->u.generic_ip;
1201 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1206 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1209 if (dent->data != NULL) {
1210 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1211 switch (handler->type) {
1213 struct led_t * led = (struct led_t *) handler->ptr;
1215 len = sprintf(page, "f\n");
1217 if (led->gpio & GPIO_TYPE_EXTIF) {
1218 len = sprintf(page, "%d\n", led->state);
1220 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1221 u8 p = (led->polarity == NORMAL ? 0 : 1);
1222 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1228 len = sprintf(page, "%s\n", platform.name);
1231 len = sprintf(page, "0x%04x\n", gpiomask);
1238 len = min_t(int, len - *ppos, count);
1239 if (copy_to_user(buf, (page + *ppos), len)) {
1253 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1256 struct inode *inode = file->f_dentry->d_inode;
1257 struct proc_dir_entry *dent = inode->u.generic_ip;
1259 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1264 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1267 if (copy_from_user(page, buf, count)) {
1273 if (dent->data != NULL) {
1274 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1275 switch (handler->type) {
1277 struct led_t *led = (struct led_t *) handler->ptr;
1278 int p = (led->polarity == NORMAL ? 0 : 1);
1280 if (page[0] == 'f') {
1285 if (led->gpio & GPIO_TYPE_EXTIF) {
1286 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1289 gpio_outen(led->gpio, led->gpio);
1290 gpio_control(led->gpio, 0);
1291 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1297 gpiomask = simple_strtoul(page, NULL, 0);
1299 if (platform.buttons) {
1300 unregister_buttons(platform.buttons);
1301 register_buttons(platform.buttons);
1304 if (platform.leds) {
1305 unregister_leds(platform.leds);
1306 register_leds(platform.leds);
1317 static int __init diag_init(void)
1319 static struct proc_dir_entry *p;
1320 static struct platform_t *detected;
1322 detected = platform_detect();
1324 printk(MODULE_NAME ": Router model not detected.\n");
1327 memcpy(&platform, detected, sizeof(struct platform_t));
1329 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1330 if (platform.platform_init != NULL) {
1331 platform.platform_init();
1334 if (!(diag = proc_mkdir("diag", NULL))) {
1335 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1339 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1340 p->data = (void *) &proc_model;
1341 p->proc_fops = &diag_proc_fops;
1344 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1345 p->data = (void *) &proc_gpiomask;
1346 p->proc_fops = &diag_proc_fops;
1349 if (platform.buttons)
1350 register_buttons(platform.buttons);
1353 register_leds(platform.leds);
1358 static void __exit diag_exit(void)
1360 del_timer(&led_timer);
1362 if (platform.buttons)
1363 unregister_buttons(platform.buttons);
1366 unregister_leds(platform.leds);
1368 remove_proc_entry("model", diag);
1369 remove_proc_entry("gpiomask", diag);
1370 remove_proc_entry("diag", NULL);
1373 module_init(diag_init);
1374 module_exit(diag_exit);
1376 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1377 MODULE_LICENSE("GPL");