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);
130 static void __init
bcm4780_init(void) {
133 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
134 printk(MODULE_NAME
": Spinning up HDD and enabling leds\n");
135 gpio_outen(pin
, pin
);
136 gpio_control(pin
, 0);
139 /* Wait 5s, so the HDD can spin up */
140 set_current_state(TASK_INTERRUPTIBLE
);
141 schedule_timeout(HZ
* 5);
144 static void __init
bcm57xx_init(void) {
148 /* FIXME: switch comes up, but port mappings/vlans not right */
149 gpio_outen(pin
, pin
);
150 gpio_control(pin
, 0);
155 static struct platform_t __initdata platforms
[] = {
158 .name
= "Linksys WAP54G V1",
160 { .name
= "reset", .gpio
= 1 << 0 },
163 { .name
= "diag", .gpio
= 1 << 3 },
164 { .name
= "wlan", .gpio
= 1 << 4 },
168 .name
= "Linksys WAP54G V3",
170 /* FIXME: verify this */
171 { .name
= "reset", .gpio
= 1 << 7 },
172 { .name
= "ses", .gpio
= 1 << 0 },
176 { .name
= "ses", .gpio
= 1 << 1 },
180 .name
= "Linksys WRT54G V1.x",
182 { .name
= "reset", .gpio
= 1 << 6 },
185 { .name
= "diag", .gpio
= 0x13 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
186 { .name
= "dmz", .gpio
= 0x12 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
190 .name
= "Linksys WRT54G/GS/GL",
192 { .name
= "reset", .gpio
= 1 << 6 },
193 { .name
= "ses", .gpio
= 1 << 4 },
196 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
197 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
198 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
199 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
200 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
204 .name
= "Linksys WRTSL54GS",
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 << 0, .polarity
= REVERSE
},
212 { .name
= "ses_white", .gpio
= 1 << 5, .polarity
= REVERSE
},
213 { .name
= "ses_orange", .gpio
= 1 << 7, .polarity
= REVERSE
},
217 .name
= "Linksys WRT54G3G",
219 { .name
= "reset", .gpio
= 1 << 6 },
220 { .name
= "3g", .gpio
= 1 << 4 },
223 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
224 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
225 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
226 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
227 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
231 .name
= "Linksys WRT350N",
233 { .name
= "reset", .gpio
= 1 << 6 },
234 { .name
= "ses", .gpio
= 1 << 8 },
237 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
238 { .name
= "ses_amber", .gpio
= 1 << 3, .polarity
= REVERSE
},
239 { .name
= "ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
},
240 { .name
= "usb_blink", .gpio
= 1 << 10, .polarity
= REVERSE
},
241 { .name
= "usb", .gpio
= 1 << 11, .polarity
= REVERSE
},
243 .platform_init
= bcm57xx_init
,
246 .name
= "Linksys WRT600N",
248 { .name
= "reset", .gpio
= 1 << 6 },
249 { .name
= "ses", .gpio
= 1 << 7 },
252 { .name
= "power", .gpio
= 1 << 2, .polarity
= REVERSE
}, // Power LED
253 { .name
= "usb", .gpio
= 1 << 3, .polarity
= REVERSE
}, // USB LED
254 { .name
= "wl0_ses_amber", .gpio
= 1 << 8, .polarity
= REVERSE
}, // 2.4Ghz LED Amber
255 { .name
= "wl0_ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
}, // 2.4Ghz LED Green
256 { .name
= "wl1_ses_amber", .gpio
= 1 << 10, .polarity
= REVERSE
}, // 5.6Ghz LED Amber
257 { .name
= "wl1_ses_green", .gpio
= 1 << 11, .polarity
= REVERSE
}, // 5.6Ghz LED Green
259 .platform_init
= bcm57xx_init
,
262 .name
= "Linksys WRT600N V1.1",
264 { .name
= "reset", .gpio
= 1 << 6 },
265 { .name
= "ses", .gpio
= 1 << 7 },
268 { .name
= "power", .gpio
= 1 << 2, .polarity
= REVERSE
}, // Power LED
269 { .name
= "usb", .gpio
= 1 << 3, .polarity
= REVERSE
}, // USB LED
270 { .name
= "wl0_ses_amber", .gpio
= 1 << 8, .polarity
= REVERSE
}, // 2.4Ghz LED Amber
271 { .name
= "wl0_ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
}, // 2.4Ghz LED Green
272 { .name
= "wl1_ses_amber", .gpio
= 1 << 10, .polarity
= REVERSE
}, // 5.6Ghz LED Amber
273 { .name
= "wl1_ses_green", .gpio
= 1 << 11, .polarity
= REVERSE
}, // 5.6Ghz LED Green
275 .platform_init
= bcm57xx_init
,
279 .name
= "ASUS WL-HDD",
281 { .name
= "reset", .gpio
= 1 << 6 },
284 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
285 { .name
= "usb", .gpio
= 1 << 2, .polarity
= NORMAL
},
289 .name
= "ASUS WL-300g",
291 { .name
= "reset", .gpio
= 1 << 6 },
294 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
298 .name
= "ASUS WL-500g",
300 { .name
= "reset", .gpio
= 1 << 6 },
303 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
307 .name
= "ASUS WL-500g Deluxe",
309 { .name
= "reset", .gpio
= 1 << 6 },
312 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
316 .name
= "ASUS WL-500g Premium",
318 { .name
= "reset", .gpio
= 1 << 0 },
319 { .name
= "ses", .gpio
= 1 << 4 },
322 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
326 .name
= "ASUS WL-500g Premium V2",
328 { .name
= "reset", .gpio
= 1 << 2 },
329 { .name
= "ses", .gpio
= 1 << 3 },
332 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
333 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
337 .name
= "ASUS WL-500W",
339 { .name
= "reset", .gpio
= 1 << 6 },
340 { .name
= "ses", .gpio
= 1 << 7 },
343 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
347 .name
= "ASUS WL-520GC",
349 { .name
= "reset", .gpio
= 1 << 2 },
350 { .name
= "ses", .gpio
= 1 << 3 },
353 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
354 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
358 .name
= "ASUS WL-520gU",
360 { .name
= "reset", .gpio
= 1 << 2 },
361 { .name
= "ses", .gpio
= 1 << 3 },
364 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
365 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
369 .name
= "ASUS (unknown, BCM4702)",
371 { .name
= "reset", .gpio
= 1 << 6 },
374 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
378 .name
= "ASUS WL-700gE",
380 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
381 { .name
= "ses", .gpio
= 1 << 4 }, // on back, actual name ezsetup
382 { .name
= "power", .gpio
= 1 << 0 }, // on front
383 { .name
= "copy", .gpio
= 1 << 6 }, // on front
387 // GPIO that controls power led also enables/disables some essential functions
390 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
}, // actual name power
392 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
394 .platform_init
= bcm4780_init
,
398 .name
= "Buffalo WHR-G54S",
400 { .name
= "reset", .gpio
= 1 << 4 },
401 { .name
= "bridge", .gpio
= 1 << 5 },
402 { .name
= "ses", .gpio
= 1 << 0 },
405 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
406 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
407 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
408 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
409 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
413 .name
= "Buffalo WBR2-G54",
416 { .name
= "reset", .gpio
= 1 << 7 },
419 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
423 .name
= "Buffalo WHR-HP-G54",
425 { .name
= "reset", .gpio
= 1 << 4 },
426 { .name
= "bridge", .gpio
= 1 << 5 },
427 { .name
= "ses", .gpio
= 1 << 0 },
430 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
431 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
432 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
433 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
434 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
438 .name
= "Buffalo WHR-G125",
440 { .name
= "reset", .gpio
= 1 << 4 },
441 { .name
= "bridge", .gpio
= 1 << 5 },
442 { .name
= "ses", .gpio
= 1 << 0 },
445 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
446 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
447 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
448 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
449 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
453 .name
= "Buffalo WHR2-A54G54",
455 { .name
= "reset", .gpio
= 1 << 4 },
458 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
462 .name
= "Buffalo WLA2-G54L",
465 { .name
= "reset", .gpio
= 1 << 7 },
468 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
472 .name
= "Buffalo WZR-G300N",
474 { .name
= "reset", .gpio
= 1 << 4 },
477 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
478 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
479 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
483 .name
= "Buffalo WZR-RS-G54",
485 { .name
= "ses", .gpio
= 1 << 0 },
486 { .name
= "reset", .gpio
= 1 << 4 },
489 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
490 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
491 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
495 .name
= "Buffalo WZR-RS-G54HP",
497 { .name
= "ses", .gpio
= 1 << 0 },
498 { .name
= "reset", .gpio
= 1 << 4 },
501 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
502 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
503 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
506 [BUFFALO_UNKNOWN
] = {
507 .name
= "Buffalo (unknown)",
509 { .name
= "reset", .gpio
= 1 << 7 },
512 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
515 [BUFFALO_UNKNOWN_4710
] = {
516 .name
= "Buffalo (unknown, BCM4710)",
518 { .name
= "reset", .gpio
= 1 << 4 },
521 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
526 .name
= "Siemens SE505 V1",
528 /* No usable buttons */
531 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
532 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
}, // actual name WWW
533 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
537 .name
= "Siemens SE505 V2",
539 /* No usable buttons */
542 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
543 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
}, // actual name WWW
544 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
549 .name
= "U.S. Robotics USR5461",
551 /* No usable buttons */
554 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
555 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
560 .name
= "Dell TrueMobile 2300",
562 { .name
= "reset", .gpio
= 1 << 0 },
565 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
566 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
571 .name
= "Motorola WE800G",
573 { .name
= "reset", .gpio
= 1 << 0 },
576 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
577 { .name
= "diag", .gpio
= 1 << 2, .polarity
= REVERSE
},
578 { .name
= "wlan_amber", .gpio
= 1 << 1, .polarity
= NORMAL
},
582 .name
= "Motorola WR850G V1",
584 { .name
= "reset", .gpio
= 1 << 0 },
587 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
588 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
589 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= NORMAL
},
590 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
591 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
595 .name
= "Motorola WR850G V2/V3",
597 { .name
= "reset", .gpio
= 1 << 5 },
600 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
601 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
602 { .name
= "wan", .gpio
= 1 << 6, .polarity
= INPUT
},
603 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
607 .name
= "Motorola WR850GP",
609 { .name
= "reset", .gpio
= 1 << 5 },
612 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
613 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
614 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
615 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
621 .name
= "Belkin (unknown)",
622 /* FIXME: verify & add detection */
624 { .name
= "reset", .gpio
= 1 << 7 },
627 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
628 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
629 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
634 .name
= "Netgear WGT634U",
636 { .name
= "reset", .gpio
= 1 << 2 },
639 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
644 .name
= "Trendware TEW411BRP+",
646 { /* No usable buttons */ },
649 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
650 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
651 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
656 .name
= "SimpleTech SimpleShare NAS",
658 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
659 { .name
= "power", .gpio
= 1 << 0 }, // on back
662 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
664 .platform_init
= bcm4780_init
,
668 .name
= "D-Link DIR-130",
670 { .name
= "reset", .gpio
= 1 << 3},
671 { .name
= "reserved", .gpio
= 1 << 7},
674 { .name
= "diag", .gpio
= 1 << 0},
675 { .name
= "blue", .gpio
= 1 << 6},
679 .name
= "D-Link DIR-330",
681 { .name
= "reset", .gpio
= 1 << 3},
682 { .name
= "reserved", .gpio
= 1 << 7},
685 { .name
= "diag", .gpio
= 1 << 0},
686 { .name
= "usb", .gpio
= 1 << 4},
687 { .name
= "blue", .gpio
= 1 << 6},
691 .name
= "D-Link DWL-3150",
693 { .name
= "reset", .gpio
= 1 << 7},
696 { .name
= "diag", .gpio
= 1 << 2},
697 { .name
= "status", .gpio
= 1 << 1},
702 .name
= "Sitecom WL-105b",
704 { .name
= "reset", .gpio
= 1 << 10},
707 { .name
= "wlan", .gpio
= 1 << 4},
708 { .name
= "power", .gpio
= 1 << 3},
713 static struct platform_t __init
*platform_detect(void)
715 char *boardnum
, *boardtype
, *buf
;
717 if (strcmp(getvar("nvram_type"), "cfe") == 0)
718 return &platforms
[WGT634U
];
720 /* Look for a model identifier */
722 /* Based on "model_name" */
723 if ((buf
= nvram_get("model_name"))) {
724 if (!strcmp(buf
, "DIR-130"))
725 return &platforms
[DIR130
];
726 if (!strcmp(buf
, "DIR-330"))
727 return &platforms
[DIR330
];
730 /* Based on "model_no" */
731 if ((buf
= nvram_get("model_no"))) {
732 if (startswith(buf
,"WL700")) /* WL700* */
733 return &platforms
[WL700GE
];
736 /* Based on "hardware_version" */
737 if ((buf
= nvram_get("hardware_version"))) {
738 if (startswith(buf
,"WL500GPV2-")) /* WL500GPV2-* */
739 return &platforms
[WL500GPV2
];
740 if (startswith(buf
,"WL520GC-")) /* WL520GU-* */
741 return &platforms
[WL520GC
];
742 if (startswith(buf
,"WL520GU-")) /* WL520GU-* */
743 return &platforms
[WL520GU
];
746 /* Based on "ModelId" */
747 if ((buf
= nvram_get("ModelId"))) {
748 if (!strcmp(buf
, "WR850GP"))
749 return &platforms
[WR850GP
];
750 if (!strcmp(buf
,"WX-5565"))
751 return &platforms
[TM2300
];
752 if (startswith(buf
,"WE800G")) /* WE800G* */
753 return &platforms
[WE800G
];
757 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
758 /* Buffalo hardware, check id for specific hardware matches */
759 if (!strcmp(buf
, "29bb0332"))
760 return &platforms
[WBR2_G54
];
761 if (!strcmp(buf
, "29129"))
762 return &platforms
[WLA2_G54L
];
763 if (!strcmp(buf
, "30189"))
764 return &platforms
[WHR_HP_G54
];
765 if (!strcmp(buf
, "32093"))
766 return &platforms
[WHR_G125
];
767 if (!strcmp(buf
, "30182"))
768 return &platforms
[WHR_G54S
];
769 if (!strcmp(buf
, "290441dd"))
770 return &platforms
[WHR2_A54G54
];
771 if (!strcmp(buf
, "31120"))
772 return &platforms
[WZR_G300N
];
773 if (!strcmp(buf
, "30083"))
774 return &platforms
[WZR_RS_G54
];
775 if (!strcmp(buf
, "30103"))
776 return &platforms
[WZR_RS_G54HP
];
779 /* no easy model number, attempt to guess */
780 boardnum
= getvar("boardnum");
781 boardtype
= getvar("boardtype");
783 if (!strcmp(boardnum
, "20070615")) { /* Linksys WRT600N v1/V1.1 */
784 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
785 return &platforms
[WRT600NV11
];
787 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "0"))
788 return &platforms
[WRT600N
];
791 if (startswith(getvar("pmon_ver"), "CFE")) {
792 /* CFE based - newer hardware */
793 if (!strcmp(boardnum
, "42")) { /* Linksys */
794 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
795 return &platforms
[WRT350N
];
797 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
798 return &platforms
[WRT54G3G
];
800 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
801 return &platforms
[WRTSL54GS
];
803 /* default to WRT54G */
804 return &platforms
[WRT54G
];
807 if (!strcmp(boardnum
, "45")) { /* ASUS */
808 if (!strcmp(boardtype
,"0x042f"))
809 return &platforms
[WL500GP
];
810 else if (!strcmp(boardtype
,"0x0472"))
811 return &platforms
[WL500W
];
813 return &platforms
[WL500GD
];
816 if (!strcmp(boardnum
, "10496"))
817 return &platforms
[USR5461
];
819 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
820 return &platforms
[SE505V2
];
822 } else { /* PMON based - old stuff */
823 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
824 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
825 return &platforms
[WR850GV1
];
827 if (startswith(boardtype
, "bcm94710dev")) {
828 if (!strcmp(boardnum
, "42"))
829 return &platforms
[WRT54GV1
];
830 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
831 return &platforms
[WAP54GV1
];
833 if (startswith(getvar("hardware_version"), "WL500-"))
834 return &platforms
[WL500G
];
835 if (startswith(getvar("hardware_version"), "WL300-")) {
836 /* Either WL-300g or WL-HDD, do more extensive checks */
837 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
838 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 1))
839 return &platforms
[WLHDD
];
840 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
841 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 10))
842 return &platforms
[WL300G
];
844 /* Sitecom WL-105b */
845 if (startswith(boardnum
, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 1)
846 return &platforms
[WL105B
];
848 /* unknown asus stuff, probably bcm4702 */
849 if (startswith(boardnum
, "asusX"))
850 return &platforms
[ASUS_4702
];
853 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
854 if (startswith(boardtype
, "bcm94710ap"))
855 return &platforms
[BUFFALO_UNKNOWN_4710
];
857 return &platforms
[BUFFALO_UNKNOWN
];
860 if (startswith(getvar("CFEver"), "MotoWRv2") ||
861 startswith(getvar("CFEver"), "MotoWRv3") ||
862 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
864 return &platforms
[WR850GV2V3
];
867 if (!strcmp(boardnum
, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
868 return &platforms
[TEW411BRPP
];
871 if (startswith(boardnum
, "04FN52")) /* SimpleTech SimpleShare */
872 return &platforms
[STI_NAS
];
874 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
875 return &platforms
[DWL3150
];
881 static void register_buttons(struct button_t
*b
)
884 platform
.button_mask
|= b
->gpio
;
886 platform
.button_mask
&= ~gpiomask
;
888 gpio_outen(platform
.button_mask
, 0);
889 gpio_control(platform
.button_mask
, 0);
890 platform
.button_polarity
= gpio_in() & platform
.button_mask
;
891 gpio_intpolarity(platform
.button_mask
, platform
.button_polarity
);
892 gpio_intmask(platform
.button_mask
, platform
.button_mask
);
894 gpio_set_irqenable(1, button_handler
);
897 static void unregister_buttons(struct button_t
*b
)
899 gpio_intmask(platform
.button_mask
, 0);
901 gpio_set_irqenable(0, button_handler
);
906 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
913 s
= skb_put(event
->skb
, strlen(msg
) + 1);
917 static void hotplug_button(struct work_struct
*work
)
919 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
925 event
->skb
= alloc_skb(2048, GFP_KERNEL
);
927 s
= skb_put(event
->skb
, strlen(event
->action
) + 2);
928 sprintf(s
, "%s@", event
->action
);
931 NETLINK_CB(event
->skb
).dst_group
= 1;
932 netlink_broadcast(uevent_sock
, event
->skb
, 0, 1, GFP_KERNEL
);
937 #else /* !LINUX_2_4 */
938 static inline char *kzalloc(unsigned int size
, unsigned int gfp
)
942 p
= kmalloc(size
, gfp
);
951 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
954 event
->argv
[event
->anr
++] = event
->scratch
;
956 event
->envp
[event
->enr
++] = event
->scratch
;
958 event
->scratch
+= sprintf(event
->scratch
, "%s", msg
) + 1;
961 static void hotplug_button(struct event_t
*event
)
963 char *scratch
= kzalloc(256, GFP_KERNEL
);
964 event
->scratch
= scratch
;
966 add_msg(event
, hotplug_path
, 1);
967 add_msg(event
, "button", 1);
969 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
973 #endif /* !LINUX_2_4 */
975 static int fill_event (struct event_t
*event
)
977 static char buf
[128];
979 add_msg(event
, "HOME=/", 0);
980 add_msg(event
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
981 add_msg(event
, "SUBSYSTEM=button", 0);
982 snprintf(buf
, 128, "ACTION=%s", event
->action
);
983 add_msg(event
, buf
, 0);
984 snprintf(buf
, 128, "BUTTON=%s", event
->name
);
985 add_msg(event
, buf
, 0);
986 snprintf(buf
, 128, "SEEN=%ld", event
->seen
);
987 add_msg(event
, buf
, 0);
989 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
990 add_msg(event
, buf
, 0);
998 static irqreturn_t
button_handler(int irq
, void *dev_id
)
1000 static irqreturn_t
button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1006 in
= gpio_in() & platform
.button_mask
;
1007 gpio_intpolarity(platform
.button_mask
, in
);
1008 changed
= platform
.button_polarity
^ in
;
1009 platform
.button_polarity
= in
;
1011 changed
&= ~gpio_outen(0, 0);
1013 for (b
= platform
.buttons
; b
->name
; b
++) {
1014 struct event_t
*event
;
1016 if (!(b
->gpio
& changed
)) continue;
1020 if ((event
= (struct event_t
*)kzalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
1021 event
->seen
= (jiffies
- b
->seen
)/HZ
;
1022 event
->name
= b
->name
;
1023 event
->action
= b
->pressed
? "pressed" : "released";
1024 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1025 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
1027 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
, (void *)event
);
1029 schedule_work(&event
->wq
);
1037 static void register_leds(struct led_t
*l
)
1039 struct proc_dir_entry
*p
;
1044 leds
= proc_mkdir("led", diag
);
1048 for(; l
->name
; l
++) {
1049 if (l
->gpio
& gpiomask
)
1052 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
1056 if (l
->polarity
!= INPUT
) oe_mask
|= l
->gpio
;
1058 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
1061 if (l
->polarity
== INPUT
) continue;
1063 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
1064 l
->proc
.type
= PROC_LED
;
1066 p
->data
= (void *) &l
->proc
;
1067 p
->proc_fops
= &diag_proc_fops
;
1071 gpio_outen(mask
, oe_mask
);
1072 gpio_control(mask
, 0);
1073 gpio_out(mask
, val
);
1076 static void unregister_leds(struct led_t
*l
)
1079 remove_proc_entry(l
->name
, leds
);
1081 remove_proc_entry("led", diag
);
1084 static void set_led_extif(struct led_t
*led
)
1086 gpio_set_extif(led
->gpio
, led
->state
);
1089 static void led_flash(unsigned long dummy
) {
1094 for (l
= platform
.leds
; l
->name
; l
++) {
1096 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
1098 l
->state
= !l
->state
;
1108 u32 val
= ~gpio_in();
1110 gpio_outen(mask
, mask
);
1111 gpio_control(mask
, 0);
1112 gpio_out(mask
, val
);
1114 if (mask
|| extif_blink
) {
1115 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
1119 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
1122 struct inode
*inode
= file
->f_dentry
->d_inode
;
1123 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1125 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1130 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
1133 if (dent
->data
!= NULL
) {
1134 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1135 switch (handler
->type
) {
1137 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
1139 len
= sprintf(page
, "f\n");
1141 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1142 len
= sprintf(page
, "%d\n", led
->state
);
1144 u32 in
= (gpio_in() & led
->gpio
? 1 : 0);
1145 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
1146 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
1152 len
= sprintf(page
, "%s\n", platform
.name
);
1155 len
= sprintf(page
, "0x%04x\n", gpiomask
);
1162 len
= min_t(int, len
- *ppos
, count
);
1163 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
1177 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
1180 struct inode
*inode
= file
->f_dentry
->d_inode
;
1181 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
1183 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
1188 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
1191 if (copy_from_user(page
, buf
, count
)) {
1197 if (dent
->data
!= NULL
) {
1198 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
1199 switch (handler
->type
) {
1201 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
1202 int p
= (led
->polarity
== NORMAL
? 0 : 1);
1204 if (page
[0] == 'f') {
1209 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
1210 led
->state
= p
^ ((page
[0] == '1') ? 1 : 0);
1213 gpio_outen(led
->gpio
, led
->gpio
);
1214 gpio_control(led
->gpio
, 0);
1215 gpio_out(led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
1221 gpiomask
= simple_strtoul(page
, NULL
, 0);
1223 if (platform
.buttons
) {
1224 unregister_buttons(platform
.buttons
);
1225 register_buttons(platform
.buttons
);
1228 if (platform
.leds
) {
1229 unregister_leds(platform
.leds
);
1230 register_leds(platform
.leds
);
1241 static int __init
diag_init(void)
1243 static struct proc_dir_entry
*p
;
1244 static struct platform_t
*detected
;
1246 detected
= platform_detect();
1248 printk(MODULE_NAME
": Router model not detected.\n");
1251 memcpy(&platform
, detected
, sizeof(struct platform_t
));
1253 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
1254 if (platform
.platform_init
!= NULL
) {
1255 platform
.platform_init();
1258 if (!(diag
= proc_mkdir("diag", NULL
))) {
1259 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1263 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
1264 p
->data
= (void *) &proc_model
;
1265 p
->proc_fops
= &diag_proc_fops
;
1268 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
1269 p
->data
= (void *) &proc_gpiomask
;
1270 p
->proc_fops
= &diag_proc_fops
;
1273 if (platform
.buttons
)
1274 register_buttons(platform
.buttons
);
1277 register_leds(platform
.leds
);
1282 static void __exit
diag_exit(void)
1284 del_timer(&led_timer
);
1286 if (platform
.buttons
)
1287 unregister_buttons(platform
.buttons
);
1290 unregister_leds(platform
.leds
);
1292 remove_proc_entry("model", diag
);
1293 remove_proc_entry("gpiomask", diag
);
1294 remove_proc_entry("diag", NULL
);
1297 module_init(diag_init
);
1298 module_exit(diag_exit
);
1300 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1301 MODULE_LICENSE("GPL");