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/moduleparam.h>
28 #include <linux/tqueue.h>
29 #include <linux/timer.h>
30 #include <asm/uaccess.h>
38 #define MODULE_NAME "diag"
41 #define FLASH_TIME HZ/6
43 static unsigned int gpiomask
= 0;
44 module_param(gpiomask
, int, 0644);
58 struct prochandler_t
{
64 struct prochandler_t proc
;
73 struct prochandler_t proc
;
82 struct button_t buttons
[MAX_GPIO
];
83 struct led_t leds
[MAX_GPIO
];
109 BUFFALO_UNKNOWN_4710
,
126 static struct platform_t __init platforms
[] = {
129 .name
= "Linksys WAP54G V1",
131 { .name
= "reset", .gpio
= 1 << 0 },
134 { .name
= "diag", .gpio
= 1 << 3 },
135 { .name
= "wlan", .gpio
= 1 << 4 },
139 .name
= "Linksys WAP54G V3",
141 /* FIXME: verify this */
142 { .name
= "reset", .gpio
= 1 << 7 },
143 { .name
= "ses", .gpio
= 1 << 0 },
147 { .name
= "ses", .gpio
= 1 << 1 },
151 .name
= "Linksys WRT54G V1.x",
153 { .name
= "reset", .gpio
= 1 << 6 },
157 { .name
= "diag", .gpio
= 1 << 1 },
158 { .name
= "dmz", .gpio
= 1 << 7 },
159 { .name
= "wlan", .gpio
= 1 << 0 },
163 .name
= "Linksys WRT54G*",
165 { .name
= "reset", .gpio
= 1 << 6 },
166 { .name
= "ses", .gpio
= 1 << 4 },
169 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
170 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
171 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
172 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
173 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
177 .name
= "Linksys WRTSL54GS",
179 { .name
= "reset", .gpio
= 1 << 6 },
180 { .name
= "ses", .gpio
= 1 << 4 },
183 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
184 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
185 { .name
= "ses_white", .gpio
= 1 << 5, .polarity
= REVERSE
},
186 { .name
= "ses_orange", .gpio
= 1 << 7, .polarity
= REVERSE
},
190 .name
= "Linksys WRT54G3G",
192 { .name
= "reset", .gpio
= 1 << 6 },
193 { .name
= "3g", .gpio
= 1 << 4 },
196 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
197 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
198 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
199 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
200 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
205 .name
= "ASUS WL-HDD",
207 { .name
= "reset", .gpio
= 1 << 6 },
210 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
214 .name
= "ASUS WL-500g",
216 { .name
= "reset", .gpio
= 1 << 6 },
219 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
223 .name
= "ASUS WL-500g",
225 { .name
= "reset", .gpio
= 1 << 6 },
228 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
232 .name
= "ASUS WL-500g Deluxe",
234 { .name
= "reset", .gpio
= 1 << 6 },
237 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
241 .name
= "ASUS WL-500g Premium",
243 { .name
= "reset", .gpio
= 1 << 0 },
246 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
247 { .name
= "ses", .gpio
= 1 << 4, .polarity
= REVERSE
},
251 .name
= "ASUS (unknown, BCM4702)",
253 { .name
= "reset", .gpio
= 1 << 6 },
256 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
261 .name
= "Buffalo WHR-G54S",
263 { .name
= "reset", .gpio
= 1 << 4 },
264 { .name
= "ses", .gpio
= 1 << 0 },
267 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
268 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
269 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
273 .name
= "Buffalo WBR2-G54",
276 { .name
= "reset", .gpio
= 1 << 7 },
279 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
283 .name
= "Buffalo WHR-HP-G54",
286 { .name
= "reset", .gpio
= 1 << 4 },
287 { .name
= "ses", .gpio
= 1 << 0 },
290 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
291 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
292 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
296 .name
= "Buffalo WLA2-G54L",
299 { .name
= "reset", .gpio
= 1 << 7 },
302 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
305 [BUFFALO_UNKNOWN
] = {
306 .name
= "Buffalo (unknown)",
308 { .name
= "reset", .gpio
= 1 << 7 },
311 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
314 [BUFFALO_UNKNOWN_4710
] = {
315 .name
= "Buffalo (unknown, BCM4710)",
317 { .name
= "reset", .gpio
= 1 << 4 },
320 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
325 .name
= "Siemens SE505 V1",
327 /* No usable buttons */
330 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
},
331 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
335 .name
= "Siemens SE505 V2",
337 /* No usable buttons */
340 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
341 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
342 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
347 .name
= "U.S. Robotics USR5461",
349 /* No usable buttons */
352 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
353 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
358 .name
= "Dell TrueMobile 2300",
360 { .name
= "reset", .gpio
= 1 << 0 },
363 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
368 .name
= "Motorola WR850G V1",
370 { .name
= "reset", .gpio
= 1 << 0 },
373 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
374 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= NORMAL
},
375 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= NORMAL
},
379 .name
= "Motorola WR850G V2",
381 { .name
= "reset", .gpio
= 1 << 5 },
384 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
385 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
386 { .name
= "modem", .gpio
= 1 << 7, .polarity
= NORMAL
},
391 static struct proc_dir_entry
*diag
, *leds
;
393 extern void *bcm947xx_sbh
;
394 #define sbh bcm947xx_sbh
396 static int sb_irq(void *sbh
);
397 static struct platform_t platform
;
399 extern char *nvram_get(char *str
);
401 static void led_flash(unsigned long dummy
);
403 static inline char __init
*getvar(char *str
)
405 return nvram_get(str
)?:"";
408 static struct platform_t __init
*platform_detect(void)
410 char *boardnum
, *boardtype
, *buf
;
412 boardnum
= getvar("boardnum");
413 boardtype
= getvar("boardtype");
414 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
415 /* CFE based - newer hardware */
416 if (!strcmp(boardnum
, "42")) { /* Linksys */
417 if (!strcmp(boardtype
, "0x0101"))
418 return &platforms
[WRT54G3G
];
420 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
421 return &platforms
[WRTSL54GS
];
423 /* default to WRT54G */
424 return &platforms
[WRT54G
];
427 if (!strcmp(boardnum
, "45")) { /* ASUS */
428 if (!strcmp(boardtype
,"0x042f"))
429 return &platforms
[WL500GP
];
431 return &platforms
[WL500GD
];
434 if (!strcmp(boardnum
, "10496"))
435 return &platforms
[USR5461
];
436 } else { /* PMON based - old stuff */
437 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
438 if (!strcmp(boardtype
, "42"))
439 return &platforms
[WRT54GV1
];
440 if (simple_strtoul(boardnum
, NULL
, 9) == 2)
441 return &platforms
[WAP54GV1
];
443 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
444 return &platforms
[WL500G
];
445 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
446 /* Either WL-300g or WL-HDD, do more extensive checks */
447 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
448 (simple_strtoul(getvar("et1phyaddr"), NULL
, 9) == 1))
449 return &platforms
[WLHDD
];
450 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
451 (simple_strtoul(getvar("et1phyaddr"), NULL
, 9) == 10))
452 return &platforms
[WL300G
];
455 /* unknown asus stuff, probably bcm4702 */
456 if (!strncmp(boardnum
, "asusX", 5))
457 return &platforms
[ASUS_4702
];
459 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
460 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30))
461 return &platforms
[WR850GV1
];
464 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
465 /* Buffalo hardware, check id for specific hardware matches */
466 if (!strcmp(buf
, "29bb0332"))
467 return &platforms
[WBR2_G54
];
468 if (!strcmp(buf
, "29129"))
469 return &platforms
[WLA2_G54L
];
470 if (!strcmp(buf
, "30189"))
471 return &platforms
[WHR_HP_G54
];
472 if (!strcmp(buf
, "30182"))
473 return &platforms
[WHR_G54S
];
476 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
477 if (!strncmp(boardtype
, "bcm94710ap", 10))
478 return &platforms
[BUFFALO_UNKNOWN_4710
];
480 return &platforms
[BUFFALO_UNKNOWN
];
483 if (!strcmp(getvar("CFEver"), "MotoWRv203"))
484 return &platforms
[WR850GV2
];
490 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
);
491 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
);
492 static struct file_operations diag_proc_fops
= {
493 read
: diag_proc_read
,
494 write
: diag_proc_write
499 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
502 struct inode
*inode
= file
->f_dentry
->d_inode
;
503 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
505 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
510 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
513 if (dent
->data
!= NULL
) {
514 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
515 switch (handler
->type
) {
517 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
519 len
= sprintf(page
, "f\n");
521 int in
= (sb_gpioin(sbh
) & led
->gpio
? 1 : 0);
522 int p
= (led
->polarity
== NORMAL
? 0 : 1);
523 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
528 len
= sprintf(page
, "%s\n", platform
.name
);
531 len
= sprintf(page
, "%d\n", gpiomask
);
538 len
= min_t(int, len
- *ppos
, count
);
539 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
552 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
)
555 struct inode
*inode
= file
->f_dentry
->d_inode
;
556 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
558 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
563 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
566 if (copy_from_user(page
, buf
, count
)) {
572 if (dent
->data
!= NULL
) {
573 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
574 switch (handler
->type
) {
576 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
577 int p
= (led
->polarity
== NORMAL
? 0 : 1);
579 if (led
->gpio
& gpiomask
)
582 if (page
[0] == 'f') {
587 sb_gpioouten(sbh
, led
->gpio
, led
->gpio
);
588 sb_gpiocontrol(sbh
, led
->gpio
, 0);
589 sb_gpioout(sbh
, led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
594 gpiomask
= simple_strtoul(page
, NULL
, 16);
611 static void hotplug_button(struct event_t
*event
)
613 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
617 static void button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
620 int in
= sb_gpioin(sbh
);
621 struct event_t
*event
;
623 for (b
= platform
.buttons
; b
->name
; b
++) {
624 if (b
->gpio
& gpiomask
)
627 if (b
->polarity
!= (in
& b
->gpio
)) {
631 if ((event
= (struct event_t
*)kmalloc (256, GFP_KERNEL
))) {
633 char *scratch
= event
->buf
;
636 event
->argv
[i
++] = hotplug_path
;
637 event
->argv
[i
++] = "button";
641 event
->envp
[i
++] = "HOME=/";
642 event
->envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
643 event
->envp
[i
++] = scratch
;
644 scratch
+= sprintf (scratch
, "ACTION=%s", b
->pressed
?"pressed":"released") + 1;
645 event
->envp
[i
++] = scratch
;
646 scratch
+= sprintf (scratch
, "BUTTON=%s", b
->name
) + 1;
647 event
->envp
[i
++] = scratch
;
648 scratch
+= sprintf (scratch
, "SEEN=%ld", (jiffies
- b
->seen
)/HZ
) + 1;
651 INIT_TQUEUE(&event
->tq
, (void *)(void *)hotplug_button
, (void *)event
);
652 schedule_task(&event
->tq
);
656 b
->polarity
^= b
->gpio
;
657 sb_gpiointpolarity(sbh
, b
->gpio
, b
->polarity
);
662 static struct timer_list led_timer
= {
666 static void led_flash(unsigned long dummy
) {
670 for (l
= platform
.leds
; l
->name
; l
++) {
680 val
= ~sb_gpioin(sbh
);
683 sb_gpioouten(sbh
, mask
, mask
);
684 sb_gpiocontrol(sbh
, mask
, 0);
685 sb_gpioout(sbh
, mask
, val
);
687 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
691 static void __init
register_buttons(struct button_t
*b
)
693 int irq
= sb_irq(sbh
) + 2;
696 request_irq(irq
, button_handler
, SA_SHIRQ
| SA_SAMPLE_RANDOM
, "gpio", button_handler
);
698 for (; b
->name
; b
++) {
699 if (b
->gpio
& gpiomask
)
702 sb_gpioouten(sbh
, b
->gpio
,0);
703 sb_gpiocontrol(sbh
, b
->gpio
,0);
704 b
->polarity
= sb_gpioin(sbh
) & b
->gpio
;
705 sb_gpiointpolarity(sbh
, b
->gpio
, b
->polarity
);
706 sb_gpiointmask(sbh
, b
->gpio
, b
->gpio
);
709 if ((cc
= sb_setcore(sbh
, SB_CC
, 0))) {
712 intmask
= readl(&cc
->intmask
);
714 writel(intmask
, &cc
->intmask
);
718 static void __exit
unregister_buttons(struct button_t
*b
)
720 int irq
= sb_irq(sbh
) + 2;
723 sb_gpiointmask(sbh
, b
->gpio
, 0);
725 free_irq(irq
, button_handler
);
728 static void __init
register_leds(struct led_t
*l
)
730 struct proc_dir_entry
*p
;
732 leds
= proc_mkdir("led", diag
);
736 for(; l
->name
; l
++) {
737 if (l
->gpio
& gpiomask
)
740 sb_gpioouten(sbh
, l
->gpio
, l
->gpio
);
741 sb_gpiocontrol(sbh
, l
->gpio
, 0);
742 sb_gpioout(sbh
, l
->gpio
, (l
->polarity
== NORMAL
)?0:l
->gpio
);
744 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
745 l
->proc
.type
= PROC_LED
;
747 p
->data
= (void *) &l
->proc
;
748 p
->proc_fops
= &diag_proc_fops
;
753 static void __exit
unregister_leds(struct led_t
*l
)
756 remove_proc_entry(l
->name
, leds
);
758 remove_proc_entry("led", diag
);
761 static void __exit
diag_exit(void)
764 del_timer(&led_timer
);
766 if (platform
.buttons
)
767 unregister_buttons(platform
.buttons
);
770 unregister_leds(platform
.leds
);
772 remove_proc_entry("model", diag
);
773 remove_proc_entry("gpiomask", diag
);
774 remove_proc_entry("diag", NULL
);
777 static struct prochandler_t proc_model
= { .type
= PROC_MODEL
};
778 static struct prochandler_t proc_gpiomask
= { .type
= PROC_GPIOMASK
};
780 static int __init
diag_init(void)
782 static struct proc_dir_entry
*p
;
783 static struct platform_t
*detected
;
785 detected
= platform_detect();
787 printk(MODULE_NAME
": Router model not detected.\n");
790 memcpy(&platform
, detected
, sizeof(struct platform_t
));
792 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
794 if (!(diag
= proc_mkdir("diag", NULL
))) {
795 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
799 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
800 p
->data
= (void *) &proc_model
;
801 p
->proc_fops
= &diag_proc_fops
;
804 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
805 p
->data
= (void *) &proc_gpiomask
;
806 p
->proc_fops
= &diag_proc_fops
;
809 if (platform
.buttons
)
810 register_buttons(platform
.buttons
);
813 register_leds(platform
.leds
);
820 module_init(diag_init
);
821 module_exit(diag_exit
);
823 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
824 MODULE_LICENSE("GPL");
826 /* TODO: export existing sb_irq instead */
827 static int sb_irq(void *sbh
)
832 uint32 flag
, sbipsflag
;
835 regs
= sb_coreregs(sbh
);
836 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
837 flag
= (R_REG(&sb
->sbtpsflag
) & SBTPS_NUM0_MASK
);
839 idx
= sb_coreidx(sbh
);
841 if ((regs
= sb_setcore(sbh
, SB_MIPS
, 0)) ||
842 (regs
= sb_setcore(sbh
, SB_MIPS33
, 0))) {
843 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
845 /* sbipsflag specifies which core is routed to interrupts 1 to 4 */
846 sbipsflag
= R_REG(&sb
->sbipsflag
);
847 for (irq
= 1; irq
<= 4; irq
++, sbipsflag
>>= 8) {
848 if ((sbipsflag
& 0x3f) == flag
)
855 sb_setcoreidx(sbh
, idx
);