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);
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-330gE",
313 { .name
= "reset", .gpio
= 1 << 2 },
316 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
320 .name
= "ASUS WL-500g",
322 { .name
= "reset", .gpio
= 1 << 6 },
325 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
329 .name
= "ASUS WL-500g Deluxe",
331 { .name
= "reset", .gpio
= 1 << 6 },
334 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
338 .name
= "ASUS WL-500g Premium",
340 { .name
= "reset", .gpio
= 1 << 0 },
341 { .name
= "ses", .gpio
= 1 << 4 },
344 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
348 .name
= "ASUS WL-500g Premium V2",
350 { .name
= "reset", .gpio
= 1 << 2 },
351 { .name
= "ses", .gpio
= 1 << 3 },
354 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
355 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
359 .name
= "ASUS WL-500W",
361 { .name
= "reset", .gpio
= 1 << 6 },
362 { .name
= "ses", .gpio
= 1 << 7 },
365 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
369 .name
= "ASUS WL-520GC",
371 { .name
= "reset", .gpio
= 1 << 2 },
372 { .name
= "ses", .gpio
= 1 << 3 },
375 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
376 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
380 .name
= "ASUS WL-520gU",
382 { .name
= "reset", .gpio
= 1 << 2 },
383 { .name
= "ses", .gpio
= 1 << 3 },
386 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
387 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
391 .name
= "ASUS (unknown, BCM4702)",
393 { .name
= "reset", .gpio
= 1 << 6 },
396 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
400 .name
= "ASUS WL-700gE",
402 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
403 { .name
= "ses", .gpio
= 1 << 4 }, // on back, actual name ezsetup
404 { .name
= "power", .gpio
= 1 << 0 }, // on front
405 { .name
= "copy", .gpio
= 1 << 6 }, // on front
409 // GPIO that controls power led also enables/disables some essential functions
412 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
}, // actual name power
414 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
416 .platform_init
= bcm4780_init
,
420 .name
= "Buffalo WHR-G54S",
422 { .name
= "reset", .gpio
= 1 << 4 },
423 { .name
= "bridge", .gpio
= 1 << 5 },
424 { .name
= "ses", .gpio
= 1 << 0 },
427 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
428 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
429 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
430 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
431 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
435 .name
= "Buffalo WBR2-G54",
438 { .name
= "reset", .gpio
= 1 << 7 },
441 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
445 .name
= "Buffalo WHR-HP-G54",
447 { .name
= "reset", .gpio
= 1 << 4 },
448 { .name
= "bridge", .gpio
= 1 << 5 },
449 { .name
= "ses", .gpio
= 1 << 0 },
452 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
453 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
454 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
455 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
456 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
460 .name
= "Buffalo WHR-G125",
462 { .name
= "reset", .gpio
= 1 << 4 },
463 { .name
= "bridge", .gpio
= 1 << 5 },
464 { .name
= "ses", .gpio
= 1 << 0 },
467 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
468 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
469 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
470 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
471 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
475 .name
= "Buffalo WHR2-A54G54",
477 { .name
= "reset", .gpio
= 1 << 4 },
480 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
484 .name
= "Buffalo WLA2-G54L",
487 { .name
= "reset", .gpio
= 1 << 7 },
490 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
494 .name
= "Buffalo WZR-G300N",
496 { .name
= "reset", .gpio
= 1 << 4 },
499 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
500 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
501 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
505 .name
= "Buffalo WZR-RS-G54",
507 { .name
= "ses", .gpio
= 1 << 0 },
508 { .name
= "reset", .gpio
= 1 << 4 },
511 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
512 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
513 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
517 .name
= "Buffalo WZR-RS-G54HP",
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
},
528 [BUFFALO_UNKNOWN
] = {
529 .name
= "Buffalo (unknown)",
531 { .name
= "reset", .gpio
= 1 << 7 },
534 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
537 [BUFFALO_UNKNOWN_4710
] = {
538 .name
= "Buffalo (unknown, BCM4710)",
540 { .name
= "reset", .gpio
= 1 << 4 },
543 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
548 .name
= "Siemens SE505 V1",
550 /* No usable buttons */
553 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
554 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
}, // actual name WWW
555 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
559 .name
= "Siemens SE505 V2",
561 /* No usable buttons */
564 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
565 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
}, // actual name WWW
566 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
571 .name
= "U.S. Robotics USR5461",
573 /* No usable buttons */
576 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
577 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
582 .name
= "Dell TrueMobile 2300",
584 { .name
= "reset", .gpio
= 1 << 0 },
587 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
588 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
592 .name
= "Dell TrueMobile 2300 v2",
594 { .name
= "reset", .gpio
= 1 << 0 },
597 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
598 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
603 .name
= "Motorola WE800G",
605 { .name
= "reset", .gpio
= 1 << 0 },
608 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
609 { .name
= "diag", .gpio
= 1 << 2, .polarity
= REVERSE
},
610 { .name
= "wlan_amber", .gpio
= 1 << 1, .polarity
= NORMAL
},
614 .name
= "Motorola WR850G V1",
616 { .name
= "reset", .gpio
= 1 << 0 },
619 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
620 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
621 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= NORMAL
},
622 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
623 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
627 .name
= "Motorola WR850G V2/V3",
629 { .name
= "reset", .gpio
= 1 << 5 },
632 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
633 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
634 { .name
= "wan", .gpio
= 1 << 6, .polarity
= INPUT
},
635 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
639 .name
= "Motorola WR850GP",
641 { .name
= "reset", .gpio
= 1 << 5 },
644 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
645 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
646 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
647 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
653 .name
= "Belkin (unknown)",
654 /* FIXME: verify & add detection */
656 { .name
= "reset", .gpio
= 1 << 7 },
659 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
660 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
661 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
666 .name
= "Netgear WGT634U",
668 { .name
= "reset", .gpio
= 1 << 2 },
671 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
676 .name
= "Trendware TEW411BRP+",
678 { /* No usable buttons */ },
681 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
682 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
683 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
688 .name
= "SimpleTech SimpleShare NAS",
690 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
691 { .name
= "power", .gpio
= 1 << 0 }, // on back
694 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
696 .platform_init
= bcm4780_init
,
700 .name
= "D-Link DIR-130",
702 { .name
= "reset", .gpio
= 1 << 3},
703 { .name
= "reserved", .gpio
= 1 << 7},
706 { .name
= "diag", .gpio
= 1 << 0},
707 { .name
= "blue", .gpio
= 1 << 6},
711 .name
= "D-Link DIR-330",
713 { .name
= "reset", .gpio
= 1 << 3},
714 { .name
= "reserved", .gpio
= 1 << 7},
717 { .name
= "diag", .gpio
= 1 << 0},
718 { .name
= "usb", .gpio
= 1 << 4},
719 { .name
= "blue", .gpio
= 1 << 6},
723 .name
= "D-Link DWL-3150",
725 { .name
= "reset", .gpio
= 1 << 7},
728 { .name
= "diag", .gpio
= 1 << 2},
729 { .name
= "status", .gpio
= 1 << 1},
734 .name
= "Sitecom WL-105b",
736 { .name
= "reset", .gpio
= 1 << 10},
739 { .name
= "wlan", .gpio
= 1 << 4},
740 { .name
= "power", .gpio
= 1 << 3},
745 static struct platform_t __init
*platform_detect(void)
747 char *boardnum
, *boardtype
, *buf
;
749 if (strcmp(getvar("nvram_type"), "cfe") == 0)
750 return &platforms
[WGT634U
];
752 /* Look for a model identifier */
754 /* Based on "model_name" */
755 if ((buf
= nvram_get("model_name"))) {
756 if (!strcmp(buf
, "DIR-130"))
757 return &platforms
[DIR130
];
758 if (!strcmp(buf
, "DIR-330"))
759 return &platforms
[DIR330
];
762 /* Based on "model_no" */
763 if ((buf
= nvram_get("model_no"))) {
764 if (startswith(buf
,"WL700")) /* WL700* */
765 return &platforms
[WL700GE
];
768 /* Based on "hardware_version" */
769 if ((buf
= nvram_get("hardware_version"))) {
770 if (startswith(buf
,"WL500GPV2-")) /* WL500GPV2-* */
771 return &platforms
[WL500GPV2
];
772 if (startswith(buf
,"WL520GC-")) /* WL520GU-* */
773 return &platforms
[WL520GC
];
774 if (startswith(buf
,"WL520GU-")) /* WL520GU-* */
775 return &platforms
[WL520GU
];
776 if (startswith(buf
,"WL330GE-")) /* WL330GE-* */
777 return &platforms
[WL330GE
];
780 /* Based on "ModelId" */
781 if ((buf
= nvram_get("ModelId"))) {
782 if (!strcmp(buf
, "WR850GP"))
783 return &platforms
[WR850GP
];
784 if (!strcmp(buf
, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
785 return &platforms
[TM2300
]; /* Dell TrueMobile 2300 */
786 if (startswith(buf
,"WE800G")) /* WE800G* */
787 return &platforms
[WE800G
];
791 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
792 /* Buffalo hardware, check id for specific hardware matches */
793 if (!strcmp(buf
, "29bb0332"))
794 return &platforms
[WBR2_G54
];
795 if (!strcmp(buf
, "29129"))
796 return &platforms
[WLA2_G54L
];
797 if (!strcmp(buf
, "30189"))
798 return &platforms
[WHR_HP_G54
];
799 if (!strcmp(buf
, "32093"))
800 return &platforms
[WHR_G125
];
801 if (!strcmp(buf
, "30182"))
802 return &platforms
[WHR_G54S
];
803 if (!strcmp(buf
, "290441dd"))
804 return &platforms
[WHR2_A54G54
];
805 if (!strcmp(buf
, "31120"))
806 return &platforms
[WZR_G300N
];
807 if (!strcmp(buf
, "30083"))
808 return &platforms
[WZR_RS_G54
];
809 if (!strcmp(buf
, "30103"))
810 return &platforms
[WZR_RS_G54HP
];
813 /* no easy model number, attempt to guess */
814 boardnum
= getvar("boardnum");
815 boardtype
= getvar("boardtype");
817 if (!strcmp(boardnum
, "20070615")) { /* Linksys WRT600N v1/V1.1 */
818 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
819 return &platforms
[WRT600NV11
];
821 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "0"))
822 return &platforms
[WRT600N
];
825 if (startswith(getvar("pmon_ver"), "CFE")) {
826 /* CFE based - newer hardware */
827 if (!strcmp(boardnum
, "42")) { /* Linksys */
828 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
829 return &platforms
[WRT350N
];
831 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
832 return &platforms
[WRT54G3G
];
834 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
835 return &platforms
[WRTSL54GS
];
837 /* default to WRT54G */
838 return &platforms
[WRT54G
];
841 if (!strcmp(boardnum
, "44") || !strcmp(boardnum
, "44\r")) {
842 if (!strcmp(boardtype
,"0x0101") || !strcmp(boardtype
, "0x0101\r"))
843 return &platforms
[TM2300V2
]; /* Dell TrueMobile 2300 v2 */
846 if (!strcmp(boardnum
, "45")) { /* ASUS */
847 if (!strcmp(boardtype
,"0x042f"))
848 return &platforms
[WL500GP
];
849 else if (!strcmp(boardtype
,"0x0472"))
850 return &platforms
[WL500W
];
851 else if (!strcmp(boardtype
,"0x467"))
852 return &platforms
[WL320GE
];
854 return &platforms
[WL500GD
];
857 if (!strcmp(boardnum
, "10496"))
858 return &platforms
[USR5461
];
860 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
861 return &platforms
[SE505V2
];
863 } else { /* PMON based - old stuff */
864 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
865 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
866 return &platforms
[WR850GV1
];
868 if (startswith(boardtype
, "bcm94710dev")) {
869 if (!strcmp(boardnum
, "42"))
870 return &platforms
[WRT54GV1
];
871 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
872 return &platforms
[WAP54GV1
];
874 if (startswith(getvar("hardware_version"), "WL500-"))
875 return &platforms
[WL500G
];
876 if (startswith(getvar("hardware_version"), "WL300-")) {
877 /* Either WL-300g or WL-HDD, do more extensive checks */
878 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
879 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 1))
880 return &platforms
[WLHDD
];
881 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
882 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 10))
883 return &platforms
[WL300G
];
885 /* Sitecom WL-105b */
886 if (startswith(boardnum
, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 1)
887 return &platforms
[WL105B
];
889 /* unknown asus stuff, probably bcm4702 */
890 if (startswith(boardnum
, "asusX"))
891 return &platforms
[ASUS_4702
];
894 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
895 if (startswith(boardtype
, "bcm94710ap"))
896 return &platforms
[BUFFALO_UNKNOWN_4710
];
898 return &platforms
[BUFFALO_UNKNOWN
];
901 if (startswith(getvar("CFEver"), "MotoWRv2") ||
902 startswith(getvar("CFEver"), "MotoWRv3") ||
903 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
905 return &platforms
[WR850GV2V3
];
908 if (!strcmp(boardnum
, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
909 return &platforms
[TEW411BRPP
];
912 if (startswith(boardnum
, "04FN52")) /* SimpleTech SimpleShare */
913 return &platforms
[STI_NAS
];
915 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
916 return &platforms
[DWL3150
];
922 static void register_buttons(struct button_t
*b
)
925 platform
.button_mask
|= b
->gpio
;
927 platform
.button_mask
&= ~gpiomask
;
929 gpio_outen(platform
.button_mask
, 0);
930 gpio_control(platform
.button_mask
, 0);
931 platform
.button_polarity
= gpio_in() & platform
.button_mask
;
932 gpio_intpolarity(platform
.button_mask
, platform
.button_polarity
);
933 gpio_setintmask(platform
.button_mask
, platform
.button_mask
);
935 gpio_set_irqenable(1, button_handler
);
938 static void unregister_buttons(struct button_t
*b
)
940 gpio_setintmask(platform
.button_mask
, 0);
942 gpio_set_irqenable(0, button_handler
);
947 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
954 s
= skb_put(event
->skb
, strlen(msg
) + 1);
958 static void hotplug_button(struct work_struct
*work
)
960 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
966 event
->skb
= alloc_skb(2048, GFP_KERNEL
);
968 s
= skb_put(event
->skb
, strlen(event
->action
) + 2);
969 sprintf(s
, "%s@", event
->action
);
972 NETLINK_CB(event
->skb
).dst_group
= 1;
973 netlink_broadcast(uevent_sock
, event
->skb
, 0, 1, GFP_KERNEL
);
978 #else /* !LINUX_2_4 */
979 static inline char *kzalloc(unsigned int size
, unsigned int gfp
)
983 p
= kmalloc(size
, gfp
);
992 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
995 event
->argv
[event
->anr
++] = event
->scratch
;
997 event
->envp
[event
->enr
++] = event
->scratch
;
999 event
->scratch
+= sprintf(event
->scratch
, "%s", msg
) + 1;
1002 static void hotplug_button(struct event_t
*event
)
1004 char *scratch
= kzalloc(256, GFP_KERNEL
);
1005 event
->scratch
= scratch
;
1007 add_msg(event
, hotplug_path
, 1);
1008 add_msg(event
, "button", 1);
1010 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
1014 #endif /* !LINUX_2_4 */
1016 static int fill_event (struct event_t
*event
)
1018 static char buf
[128];
1020 add_msg(event
, "HOME=/", 0);
1021 add_msg(event
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1022 add_msg(event
, "SUBSYSTEM=button", 0);
1023 snprintf(buf
, 128, "ACTION=%s", event
->action
);
1024 add_msg(event
, buf
, 0);
1025 snprintf(buf
, 128, "BUTTON=%s", event
->name
);
1026 add_msg(event
, buf
, 0);
1027 snprintf(buf
, 128, "SEEN=%ld", event
->seen
);
1028 add_msg(event
, buf
, 0);
1030 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
1031 add_msg(event
, buf
, 0);
1039 static irqreturn_t
button_handler(int irq
, void *dev_id
)
1041 static irqreturn_t
button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1047 in
= gpio_in() & platform
.button_mask
;
1048 gpio_intpolarity(platform
.button_mask
, in
);
1049 changed
= platform
.button_polarity
^ in
;
1050 platform
.button_polarity
= in
;
1052 changed
&= ~gpio_outen(0, 0);
1054 for (b
= platform
.buttons
; b
->name
; b
++) {
1055 struct event_t
*event
;
1057 if (!(b
->gpio
& changed
)) continue;
1061 if ((event
= (struct event_t
*)kzalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
1062 event
->seen
= (jiffies
- b
->seen
)/HZ
;
1063 event
->name
= b
->name
;
1064 event
->action
= b
->pressed
? "pressed" : "released";
1065 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1066 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
1068 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
, (void *)event
);
1070 schedule_work(&event
->wq
);
1078 static void register_leds(struct led_t
*l
)
1080 struct proc_dir_entry
*p
;
1085 leds
= proc_mkdir("led", diag
);
1089 for(; l
->name
; l
++) {
1090 if (l
->gpio
& gpiomask
)
1093 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
1097 if (l
->polarity
!= INPUT
) oe_mask
|= l
->gpio
;
1099 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
1102 if (l
->polarity
== INPUT
) continue;
1104 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
1105 l
->proc
.type
= PROC_LED
;
1107 p
->data
= (void *) &l
->proc
;
1108 p
->proc_fops
= &diag_proc_fops
;
1112 gpio_outen(mask
, oe_mask
);
1113 gpio_control(mask
, 0);
1114 gpio_out(mask
, val
);
1115 gpio_setintmask(mask
, 0);
1118 static void unregister_leds(struct led_t
*l
)
1121 remove_proc_entry(l
->name
, leds
);
1123 remove_proc_entry("led", diag
);
1126 static void set_led_extif(struct led_t
*led
)
1128 gpio_set_extif(led
->gpio
, led
->state
);
1131 static void led_flash(unsigned long dummy
) {
1136 for (l
= platform
.leds
; l
->name
; l
++) {
1138 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
1140 l
->state
= !l
->state
;
1150 u32 val
= ~gpio_in();
1152 gpio_outen(mask
, mask
);
1153 gpio_control(mask
, 0);
1154 gpio_out(mask
, val
);
1156 if (mask
|| extif_blink
) {
1157 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
1161 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
1164 struct inode
*inode
= file
->f_dentry
->d_inode
;
1165 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1167 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1172 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
1175 if (dent
->data
!= NULL
) {
1176 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1177 switch (handler
->type
) {
1179 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
1181 len
= sprintf(page
, "f\n");
1183 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1184 len
= sprintf(page
, "%d\n", led
->state
);
1186 u32 in
= (gpio_in() & led
->gpio
? 1 : 0);
1187 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
1188 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
1194 len
= sprintf(page
, "%s\n", platform
.name
);
1197 len
= sprintf(page
, "0x%04x\n", gpiomask
);
1204 len
= min_t(int, len
- *ppos
, count
);
1205 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
1219 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
1222 struct inode
*inode
= file
->f_dentry
->d_inode
;
1223 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1225 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1230 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
1233 if (copy_from_user(page
, buf
, count
)) {
1239 if (dent
->data
!= NULL
) {
1240 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1241 switch (handler
->type
) {
1243 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
1244 int p
= (led
->polarity
== NORMAL
? 0 : 1);
1246 if (page
[0] == 'f') {
1251 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1252 led
->state
= p
^ ((page
[0] == '1') ? 1 : 0);
1255 gpio_outen(led
->gpio
, led
->gpio
);
1256 gpio_control(led
->gpio
, 0);
1257 gpio_out(led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
1263 gpiomask
= simple_strtoul(page
, NULL
, 0);
1265 if (platform
.buttons
) {
1266 unregister_buttons(platform
.buttons
);
1267 register_buttons(platform
.buttons
);
1270 if (platform
.leds
) {
1271 unregister_leds(platform
.leds
);
1272 register_leds(platform
.leds
);
1283 static int __init
diag_init(void)
1285 static struct proc_dir_entry
*p
;
1286 static struct platform_t
*detected
;
1288 detected
= platform_detect();
1290 printk(MODULE_NAME
": Router model not detected.\n");
1293 memcpy(&platform
, detected
, sizeof(struct platform_t
));
1295 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
1296 if (platform
.platform_init
!= NULL
) {
1297 platform
.platform_init();
1300 if (!(diag
= proc_mkdir("diag", NULL
))) {
1301 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1305 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
1306 p
->data
= (void *) &proc_model
;
1307 p
->proc_fops
= &diag_proc_fops
;
1310 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
1311 p
->data
= (void *) &proc_gpiomask
;
1312 p
->proc_fops
= &diag_proc_fops
;
1315 if (platform
.buttons
)
1316 register_buttons(platform
.buttons
);
1319 register_leds(platform
.leds
);
1324 static void __exit
diag_exit(void)
1326 del_timer(&led_timer
);
1328 if (platform
.buttons
)
1329 unregister_buttons(platform
.buttons
);
1332 unregister_leds(platform
.leds
);
1334 remove_proc_entry("model", diag
);
1335 remove_proc_entry("gpiomask", diag
);
1336 remove_proc_entry("diag", NULL
);
1339 module_init(diag_init
);
1340 module_exit(diag_exit
);
1342 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1343 MODULE_LICENSE("GPL");