2 * diag.c - GPIO interface driver for Broadcom boards
4 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
5 * Felix Fietkau <nbd@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * 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>
31 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
32 #include <linux/kobject.h>
33 #include <linux/workqueue.h>
34 #define hotplug_path uevent_helper
36 #include <linux/tqueue.h>
37 #define INIT_WORK INIT_TQUEUE
38 #define schedule_work schedule_task
39 #define work_struct tq_struct
44 #define getvar(str) (nvram_get(str)?:"")
46 static unsigned int gpiomask = 0;
47 module_param(gpiomask, int, 0644);
104 static struct platform_t __initdata platforms[] = {
107 .name = "Linksys WAP54G V1",
109 { .name = "reset", .gpio = 1 << 0 },
112 { .name = "diag", .gpio = 1 << 3 },
113 { .name = "wlan", .gpio = 1 << 4 },
117 .name = "Linksys WAP54G V3",
119 /* FIXME: verify this */
120 { .name = "reset", .gpio = 1 << 7 },
121 { .name = "ses", .gpio = 1 << 0 },
125 { .name = "ses", .gpio = 1 << 1 },
129 .name = "Linksys WRT54G V1.x",
131 { .name = "reset", .gpio = 1 << 6 },
134 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
135 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
139 .name = "Linksys WRT54G/GS/GL",
141 { .name = "reset", .gpio = 1 << 6 },
142 { .name = "ses", .gpio = 1 << 4 },
145 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
146 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
147 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
148 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
149 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
153 .name = "Linksys WRTSL54GS",
155 { .name = "reset", .gpio = 1 << 6 },
156 { .name = "ses", .gpio = 1 << 4 },
159 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
160 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
161 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
162 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
166 .name = "Linksys WRT54G3G",
168 { .name = "reset", .gpio = 1 << 6 },
169 { .name = "3g", .gpio = 1 << 4 },
172 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
173 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
174 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
175 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
176 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
180 .name = "Linksys WRT350N",
182 { .name = "reset", .gpio = 1 << 6 },
183 { .name = "ses", .gpio = 1 << 8 },
186 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
187 { .name = "ses", .gpio = 1 << 3, .polarity = REVERSE },
192 .name = "ASUS WL-HDD",
194 { .name = "reset", .gpio = 1 << 6 },
197 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
198 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
202 .name = "ASUS WL-300g",
204 { .name = "reset", .gpio = 1 << 6 },
207 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
211 .name = "ASUS WL-500g",
213 { .name = "reset", .gpio = 1 << 6 },
216 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
220 .name = "ASUS WL-500g Deluxe",
222 { .name = "reset", .gpio = 1 << 6 },
225 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
229 .name = "ASUS WL-500g Premium",
231 { .name = "reset", .gpio = 1 << 0 },
232 { .name = "ses", .gpio = 1 << 4 },
235 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
239 .name = "ASUS (unknown, BCM4702)",
241 { .name = "reset", .gpio = 1 << 6 },
244 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
249 .name = "Buffalo WHR-G54S",
251 { .name = "reset", .gpio = 1 << 4 },
252 { .name = "bridge", .gpio = 1 << 5 },
253 { .name = "ses", .gpio = 1 << 0 },
256 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
257 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
258 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
259 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
263 .name = "Buffalo WBR2-G54",
266 { .name = "reset", .gpio = 1 << 7 },
269 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
273 .name = "Buffalo WHR-HP-G54",
275 { .name = "reset", .gpio = 1 << 4 },
276 { .name = "bridge", .gpio = 1 << 5 },
277 { .name = "ses", .gpio = 1 << 0 },
280 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
281 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
282 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
286 .name = "Buffalo WHR2-A54G54",
288 { .name = "reset", .gpio = 1 << 4 },
291 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
295 .name = "Buffalo WLA2-G54L",
298 { .name = "reset", .gpio = 1 << 7 },
301 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
305 .name = "Buffalo WZR-G300N",
307 { .name = "reset", .gpio = 1 << 4 },
310 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
311 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
312 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
316 .name = "Buffalo WZR-RS-G54",
318 { .name = "ses", .gpio = 1 << 0 },
319 { .name = "reset", .gpio = 1 << 4 },
322 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
323 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
324 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
328 .name = "Buffalo WZR-RS-G54HP",
330 { .name = "ses", .gpio = 1 << 0 },
331 { .name = "reset", .gpio = 1 << 4 },
334 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
335 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
336 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
339 [BUFFALO_UNKNOWN] = {
340 .name = "Buffalo (unknown)",
342 { .name = "reset", .gpio = 1 << 7 },
345 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
348 [BUFFALO_UNKNOWN_4710] = {
349 .name = "Buffalo (unknown, BCM4710)",
351 { .name = "reset", .gpio = 1 << 4 },
354 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
359 .name = "Siemens SE505 V1",
361 /* No usable buttons */
364 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
365 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
369 .name = "Siemens SE505 V2",
371 /* No usable buttons */
374 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
375 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
376 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
381 .name = "U.S. Robotics USR5461",
383 /* No usable buttons */
386 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
387 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
392 .name = "Dell TrueMobile 2300",
394 { .name = "reset", .gpio = 1 << 0 },
397 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
398 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
403 .name = "Motorola WE800G",
405 { .name = "reset", .gpio = 1 << 0 },
408 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
409 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
410 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
414 .name = "Motorola WR850G V1",
416 { .name = "reset", .gpio = 1 << 0 },
419 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
420 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
421 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
422 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
423 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
427 .name = "Motorola WR850G V2/V3",
429 { .name = "reset", .gpio = 1 << 5 },
432 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
433 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
434 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
435 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
440 .name = "Belkin (unknown)",
441 /* FIXME: verify & add detection */
443 { .name = "reset", .gpio = 1 << 7 },
446 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
447 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
448 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
453 .name = "Netgear WGT634U",
455 { .name = "reset", .gpio = 1 << 2 },
458 { .name = "power", .gpio = 1 << 3, .polarity = REVERSE },
463 .name = "Trendware TEW411BRP+",
465 { /* No usable buttons */ },
468 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
469 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
470 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
475 static struct platform_t __init *platform_detect(void)
477 char *boardnum, *boardtype, *buf;
479 boardnum = getvar("boardnum");
480 boardtype = getvar("boardtype");
481 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
482 /* CFE based - newer hardware */
483 if (!strcmp(boardnum, "42")) { /* Linksys */
484 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), 1))
485 return &platforms[WRT350N];
487 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
488 return &platforms[WRT54G3G];
490 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
491 return &platforms[WRTSL54GS];
493 /* default to WRT54G */
494 return &platforms[WRT54G];
497 if (!strcmp(boardnum, "45")) { /* ASUS */
498 if (!strcmp(boardtype,"0x042f"))
499 return &platforms[WL500GP];
501 return &platforms[WL500GD];
504 if (!strcmp(boardnum, "10496"))
505 return &platforms[USR5461];
506 } else { /* PMON based - old stuff */
508 /* Dell TrueMobile 2300 */
509 if (!strcmp(getvar("ModelId"),"WX-5565"))
510 return &platforms[TM2300];
512 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
513 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
514 if (!strncmp(getvar("ModelId"),"WE800G", 6))
515 return &platforms[WE800G];
517 return &platforms[WR850GV1];
519 if (!strncmp(boardtype, "bcm94710dev", 11)) {
520 if (!strcmp(boardnum, "42"))
521 return &platforms[WRT54GV1];
522 if (simple_strtoul(boardnum, NULL, 0) == 2)
523 return &platforms[WAP54GV1];
525 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
526 return &platforms[WL500G];
527 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
528 /* Either WL-300g or WL-HDD, do more extensive checks */
529 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
530 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
531 return &platforms[WLHDD];
532 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
533 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
534 return &platforms[WL300G];
537 /* unknown asus stuff, probably bcm4702 */
538 if (!strncmp(boardnum, "asusX", 5))
539 return &platforms[ASUS_4702];
542 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
543 /* Buffalo hardware, check id for specific hardware matches */
544 if (!strcmp(buf, "29bb0332"))
545 return &platforms[WBR2_G54];
546 if (!strcmp(buf, "29129"))
547 return &platforms[WLA2_G54L];
548 if (!strcmp(buf, "30189"))
549 return &platforms[WHR_HP_G54];
550 if (!strcmp(buf, "30182"))
551 return &platforms[WHR_G54S];
552 if (!strcmp(buf, "290441dd"))
553 return &platforms[WHR2_A54G54];
554 if (!strcmp(buf, "31120"))
555 return &platforms[WZR_G300N];
556 if (!strcmp(buf, "30083"))
557 return &platforms[WZR_RS_G54];
558 if (!strcmp(buf, "30103"))
559 return &platforms[WZR_RS_G54HP];
562 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
563 if (!strncmp(boardtype, "bcm94710ap", 10))
564 return &platforms[BUFFALO_UNKNOWN_4710];
566 return &platforms[BUFFALO_UNKNOWN];
569 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
570 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
572 return &platforms[WR850GV2V3];
575 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
576 return &platforms[TEW411BRPP];
583 static void register_buttons(struct button_t *b)
586 platform.button_mask |= b->gpio;
588 platform.button_mask &= ~gpiomask;
590 gpio_outen(platform.button_mask, 0);
591 gpio_control(platform.button_mask, 0);
592 platform.button_polarity = gpio_in() & platform.button_mask;
593 gpio_intpolarity(platform.button_mask, platform.button_polarity);
594 gpio_intmask(platform.button_mask, platform.button_mask);
596 gpio_set_irqenable(1, button_handler);
599 static void unregister_buttons(struct button_t *b)
601 gpio_intmask(platform.button_mask, 0);
603 gpio_set_irqenable(0, button_handler);
606 static void hotplug_button(struct event_t *event)
608 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
609 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
611 call_usermodehelper (event->argv[0], event->argv, event->envp);
616 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
621 in = gpio_in() & platform.button_mask;
622 gpio_intpolarity(platform.button_mask, in);
623 changed = platform.button_polarity ^ in;
624 platform.button_polarity = in;
626 changed &= ~gpio_outen(0, 0);
628 for (b = platform.buttons; b->name; b++) {
629 struct event_t *event;
631 if (!(b->gpio & changed)) continue;
635 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
637 char *scratch = event->buf;
640 event->argv[i++] = hotplug_path;
641 event->argv[i++] = "button";
645 event->envp[i++] = "HOME=/";
646 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
647 event->envp[i++] = scratch;
648 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
649 event->envp[i++] = scratch;
650 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
651 event->envp[i++] = scratch;
652 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
655 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
656 schedule_work(&event->wq);
664 static void register_leds(struct led_t *l)
666 struct proc_dir_entry *p;
670 leds = proc_mkdir("led", diag);
674 for(; l->name; l++) {
675 if (l->gpio & gpiomask)
678 if (l->gpio & GPIO_TYPE_EXTIF) {
683 val |= (l->polarity == NORMAL)?0:l->gpio;
686 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
687 l->proc.type = PROC_LED;
689 p->data = (void *) &l->proc;
690 p->proc_fops = &diag_proc_fops;
694 gpio_outen(mask, mask);
695 gpio_control(mask, 0);
699 static void unregister_leds(struct led_t *l)
702 remove_proc_entry(l->name, leds);
704 remove_proc_entry("led", diag);
707 static void set_led_extif(struct led_t *led)
709 gpio_set_extif(led->gpio, led->state);
712 static void led_flash(unsigned long dummy) {
717 for (l = platform.leds; l->name; l++) {
719 if (l->gpio & GPIO_TYPE_EXTIF) {
721 l->state = !l->state;
731 u32 val = ~gpio_in();
733 gpio_outen(mask, mask);
734 gpio_control(mask, 0);
737 if (mask || extif_blink) {
738 mod_timer(&led_timer, jiffies + FLASH_TIME);
742 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
745 struct inode *inode = file->f_dentry->d_inode;
746 struct proc_dir_entry *dent = inode->u.generic_ip;
748 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
753 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
756 if (dent->data != NULL) {
757 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
758 switch (handler->type) {
760 struct led_t * led = (struct led_t *) handler->ptr;
762 len = sprintf(page, "f\n");
764 if (led->gpio & GPIO_TYPE_EXTIF) {
765 len = sprintf(page, "%d\n", led->state);
767 u32 in = (gpio_in() & led->gpio ? 1 : 0);
768 u8 p = (led->polarity == NORMAL ? 0 : 1);
769 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
775 len = sprintf(page, "%s\n", platform.name);
778 len = sprintf(page, "0x%04x\n", gpiomask);
785 len = min_t(int, len - *ppos, count);
786 if (copy_to_user(buf, (page + *ppos), len)) {
799 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
802 struct inode *inode = file->f_dentry->d_inode;
803 struct proc_dir_entry *dent = inode->u.generic_ip;
805 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
810 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
813 if (copy_from_user(page, buf, count)) {
819 if (dent->data != NULL) {
820 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
821 switch (handler->type) {
823 struct led_t *led = (struct led_t *) handler->ptr;
824 int p = (led->polarity == NORMAL ? 0 : 1);
826 if (page[0] == 'f') {
831 if (led->gpio & GPIO_TYPE_EXTIF) {
832 led->state = p ^ ((page[0] == '1') ? 1 : 0);
835 gpio_outen(led->gpio, led->gpio);
836 gpio_control(led->gpio, 0);
837 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
843 gpiomask = simple_strtoul(page, NULL, 0);
845 if (platform.buttons) {
846 unregister_buttons(platform.buttons);
847 register_buttons(platform.buttons);
851 unregister_leds(platform.leds);
852 register_leds(platform.leds);
863 static int __init diag_init(void)
865 static struct proc_dir_entry *p;
866 static struct platform_t *detected;
868 detected = platform_detect();
870 printk(MODULE_NAME ": Router model not detected.\n");
873 memcpy(&platform, detected, sizeof(struct platform_t));
875 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
877 if (!(diag = proc_mkdir("diag", NULL))) {
878 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
882 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
883 p->data = (void *) &proc_model;
884 p->proc_fops = &diag_proc_fops;
887 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
888 p->data = (void *) &proc_gpiomask;
889 p->proc_fops = &diag_proc_fops;
892 if (platform.buttons)
893 register_buttons(platform.buttons);
896 register_leds(platform.leds);
901 static void __exit diag_exit(void)
904 del_timer(&led_timer);
906 if (platform.buttons)
907 unregister_buttons(platform.buttons);
910 unregister_leds(platform.leds);
912 remove_proc_entry("model", diag);
913 remove_proc_entry("gpiomask", diag);
914 remove_proc_entry("diag", NULL);
917 module_init(diag_init);
918 module_exit(diag_exit);
920 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
921 MODULE_LICENSE("GPL");