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
,
129 static struct platform_t __init platforms
[] = {
132 .name
= "Linksys WAP54G V1",
134 { .name
= "reset", .gpio
= 1 << 0 },
137 { .name
= "diag", .gpio
= 1 << 3 },
138 { .name
= "wlan", .gpio
= 1 << 4 },
142 .name
= "Linksys WAP54G V3",
144 /* FIXME: verify this */
145 { .name
= "reset", .gpio
= 1 << 7 },
146 { .name
= "ses", .gpio
= 1 << 0 },
150 { .name
= "ses", .gpio
= 1 << 1 },
154 .name
= "Linksys WRT54G V1.x",
156 { .name
= "reset", .gpio
= 1 << 6 },
160 { .name
= "diag", .gpio
= 1 << 1 },
161 { .name
= "dmz", .gpio
= 1 << 7 },
162 { .name
= "wlan", .gpio
= 1 << 0 },
166 .name
= "Linksys WRT54G*",
168 { .name
= "reset", .gpio
= 1 << 6 },
169 { .name
= "ses", .gpio
= 1 << 4 },
172 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
173 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
174 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
175 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
176 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
180 .name
= "Linksys WRTSL54GS",
182 { .name
= "reset", .gpio
= 1 << 6 },
183 { .name
= "ses", .gpio
= 1 << 4 },
186 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
187 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
188 { .name
= "ses_white", .gpio
= 1 << 5, .polarity
= REVERSE
},
189 { .name
= "ses_orange", .gpio
= 1 << 7, .polarity
= REVERSE
},
193 .name
= "Linksys WRT54G3G",
195 { .name
= "reset", .gpio
= 1 << 6 },
196 { .name
= "3g", .gpio
= 1 << 4 },
199 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
200 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
201 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
202 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
203 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
208 .name
= "ASUS WL-HDD",
210 { .name
= "reset", .gpio
= 1 << 6 },
213 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
217 .name
= "ASUS WL-500g",
219 { .name
= "reset", .gpio
= 1 << 6 },
222 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
226 .name
= "ASUS WL-500g",
228 { .name
= "reset", .gpio
= 1 << 6 },
231 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
235 .name
= "ASUS WL-500g Deluxe",
237 { .name
= "reset", .gpio
= 1 << 6 },
240 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
244 .name
= "ASUS WL-500g Premium",
246 { .name
= "reset", .gpio
= 1 << 0 },
249 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
250 { .name
= "ses", .gpio
= 1 << 4, .polarity
= REVERSE
},
254 .name
= "ASUS (unknown, BCM4702)",
256 { .name
= "reset", .gpio
= 1 << 6 },
259 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
264 .name
= "Buffalo WHR-G54S",
266 { .name
= "reset", .gpio
= 1 << 4 },
267 { .name
= "ses", .gpio
= 1 << 0 },
270 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
271 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
272 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
276 .name
= "Buffalo WBR2-G54",
279 { .name
= "reset", .gpio
= 1 << 7 },
282 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
286 .name
= "Buffalo WHR-HP-G54",
288 { .name
= "reset", .gpio
= 1 << 4 },
289 { .name
= "bridge", .gpio
= 1 << 5 },
290 { .name
= "ses", .gpio
= 1 << 0 },
293 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
294 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
295 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
299 .name
= "Buffalo WLA2-G54L",
302 { .name
= "reset", .gpio
= 1 << 7 },
305 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
308 [BUFFALO_UNKNOWN
] = {
309 .name
= "Buffalo (unknown)",
311 { .name
= "reset", .gpio
= 1 << 7 },
314 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
317 [BUFFALO_UNKNOWN_4710
] = {
318 .name
= "Buffalo (unknown, BCM4710)",
320 { .name
= "reset", .gpio
= 1 << 4 },
323 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
328 .name
= "Siemens SE505 V1",
330 /* No usable buttons */
333 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
},
334 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
338 .name
= "Siemens SE505 V2",
340 /* No usable buttons */
343 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
344 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
345 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
350 .name
= "U.S. Robotics USR5461",
352 /* No usable buttons */
355 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
356 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
361 .name
= "Dell TrueMobile 2300",
363 { .name
= "reset", .gpio
= 1 << 0 },
366 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
371 .name
= "Motorola WR850G V1",
373 { .name
= "reset", .gpio
= 1 << 0 },
376 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
377 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
378 { .name
= "modem", .gpio
= 1 << 6, .polarity
= NORMAL
},
379 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
380 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
384 .name
= "Motorola WR850G V2",
386 { .name
= "reset", .gpio
= 1 << 5 },
389 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
390 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= NORMAL
},
391 { .name
= "modem_green",.gpio
= 1 << 6, .polarity
= REVERSE
},
392 { .name
= "modem_red", .gpio
= 1 << 7, .polarity
= REVERSE
},
397 .name
= "Belkin (unknown)",
398 /* FIXME: verify & add detection */
400 { .name
= "reset", .gpio
= 1 << 7 },
403 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
404 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
405 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
410 static struct proc_dir_entry
*diag
, *leds
;
412 extern void *bcm947xx_sbh
;
413 #define sbh bcm947xx_sbh
415 static int sb_irq(void *sbh
);
416 static struct platform_t platform
;
418 extern char *nvram_get(char *str
);
420 static void led_flash(unsigned long dummy
);
422 static inline char __init
*getvar(char *str
)
424 return nvram_get(str
)?:"";
427 static struct platform_t __init
*platform_detect(void)
429 char *boardnum
, *boardtype
, *buf
;
431 boardnum
= getvar("boardnum");
432 boardtype
= getvar("boardtype");
433 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
434 /* CFE based - newer hardware */
435 if (!strcmp(boardnum
, "42")) { /* Linksys */
436 if (!strcmp(boardtype
, "0x0101"))
437 return &platforms
[WRT54G3G
];
439 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
440 return &platforms
[WRTSL54GS
];
442 /* default to WRT54G */
443 return &platforms
[WRT54G
];
446 if (!strcmp(boardnum
, "45")) { /* ASUS */
447 if (!strcmp(boardtype
,"0x042f"))
448 return &platforms
[WL500GP
];
450 return &platforms
[WL500GD
];
453 if (!strcmp(boardnum
, "10496"))
454 return &platforms
[USR5461
];
455 } else { /* PMON based - old stuff */
456 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
457 if (!strcmp(boardtype
, "42"))
458 return &platforms
[WRT54GV1
];
459 if (simple_strtoul(boardnum
, NULL
, 9) == 2)
460 return &platforms
[WAP54GV1
];
462 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
463 return &platforms
[WL500G
];
464 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
465 /* Either WL-300g or WL-HDD, do more extensive checks */
466 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
467 (simple_strtoul(getvar("et1phyaddr"), NULL
, 9) == 1))
468 return &platforms
[WLHDD
];
469 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
470 (simple_strtoul(getvar("et1phyaddr"), NULL
, 9) == 10))
471 return &platforms
[WL300G
];
474 /* unknown asus stuff, probably bcm4702 */
475 if (!strncmp(boardnum
, "asusX", 5))
476 return &platforms
[ASUS_4702
];
478 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
479 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30))
480 return &platforms
[WR850GV1
];
483 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
484 /* Buffalo hardware, check id for specific hardware matches */
485 if (!strcmp(buf
, "29bb0332"))
486 return &platforms
[WBR2_G54
];
487 if (!strcmp(buf
, "29129"))
488 return &platforms
[WLA2_G54L
];
489 if (!strcmp(buf
, "30189"))
490 return &platforms
[WHR_HP_G54
];
491 if (!strcmp(buf
, "30182"))
492 return &platforms
[WHR_G54S
];
495 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
496 if (!strncmp(boardtype
, "bcm94710ap", 10))
497 return &platforms
[BUFFALO_UNKNOWN_4710
];
499 return &platforms
[BUFFALO_UNKNOWN
];
502 if (!strcmp(getvar("CFEver"), "MotoWRv203"))
503 return &platforms
[WR850GV2
];
509 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
);
510 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
);
511 static struct file_operations diag_proc_fops
= {
512 read
: diag_proc_read
,
513 write
: diag_proc_write
518 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
521 struct inode
*inode
= file
->f_dentry
->d_inode
;
522 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
524 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
529 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
532 if (dent
->data
!= NULL
) {
533 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
534 switch (handler
->type
) {
536 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
538 len
= sprintf(page
, "f\n");
540 int in
= (sb_gpioin(sbh
) & led
->gpio
? 1 : 0);
541 int p
= (led
->polarity
== NORMAL
? 0 : 1);
542 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
547 len
= sprintf(page
, "%s\n", platform
.name
);
550 len
= sprintf(page
, "%d\n", gpiomask
);
557 len
= min_t(int, len
- *ppos
, count
);
558 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
571 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
)
574 struct inode
*inode
= file
->f_dentry
->d_inode
;
575 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
577 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
582 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
585 if (copy_from_user(page
, buf
, count
)) {
591 if (dent
->data
!= NULL
) {
592 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
593 switch (handler
->type
) {
595 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
596 int p
= (led
->polarity
== NORMAL
? 0 : 1);
598 if (led
->gpio
& gpiomask
)
601 if (page
[0] == 'f') {
606 sb_gpioouten(sbh
, led
->gpio
, led
->gpio
);
607 sb_gpiocontrol(sbh
, led
->gpio
, 0);
608 sb_gpioout(sbh
, led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
613 gpiomask
= simple_strtoul(page
, NULL
, 16);
630 static void hotplug_button(struct event_t
*event
)
632 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
636 static void button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
639 int in
= sb_gpioin(sbh
);
640 struct event_t
*event
;
642 for (b
= platform
.buttons
; b
->name
; b
++) {
643 if (b
->gpio
& gpiomask
)
646 if (b
->polarity
!= (in
& b
->gpio
)) {
650 if ((event
= (struct event_t
*)kmalloc (256, GFP_KERNEL
))) {
652 char *scratch
= event
->buf
;
655 event
->argv
[i
++] = hotplug_path
;
656 event
->argv
[i
++] = "button";
660 event
->envp
[i
++] = "HOME=/";
661 event
->envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
662 event
->envp
[i
++] = scratch
;
663 scratch
+= sprintf (scratch
, "ACTION=%s", b
->pressed
?"pressed":"released") + 1;
664 event
->envp
[i
++] = scratch
;
665 scratch
+= sprintf (scratch
, "BUTTON=%s", b
->name
) + 1;
666 event
->envp
[i
++] = scratch
;
667 scratch
+= sprintf (scratch
, "SEEN=%ld", (jiffies
- b
->seen
)/HZ
) + 1;
670 INIT_TQUEUE(&event
->tq
, (void *)(void *)hotplug_button
, (void *)event
);
671 schedule_task(&event
->tq
);
675 b
->polarity
^= b
->gpio
;
676 sb_gpiointpolarity(sbh
, b
->gpio
, b
->polarity
);
681 static struct timer_list led_timer
= {
685 static void led_flash(unsigned long dummy
) {
689 for (l
= platform
.leds
; l
->name
; l
++) {
699 val
= ~sb_gpioin(sbh
);
702 sb_gpioouten(sbh
, mask
, mask
);
703 sb_gpiocontrol(sbh
, mask
, 0);
704 sb_gpioout(sbh
, mask
, val
);
706 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
710 static void __init
register_buttons(struct button_t
*b
)
712 int irq
= sb_irq(sbh
) + 2;
715 request_irq(irq
, button_handler
, SA_SHIRQ
| SA_SAMPLE_RANDOM
, "gpio", button_handler
);
717 for (; b
->name
; b
++) {
718 if (b
->gpio
& gpiomask
)
721 sb_gpioouten(sbh
, b
->gpio
,0);
722 sb_gpiocontrol(sbh
, b
->gpio
,0);
723 b
->polarity
= sb_gpioin(sbh
) & b
->gpio
;
724 sb_gpiointpolarity(sbh
, b
->gpio
, b
->polarity
);
725 sb_gpiointmask(sbh
, b
->gpio
, b
->gpio
);
728 if ((cc
= sb_setcore(sbh
, SB_CC
, 0))) {
731 intmask
= readl(&cc
->intmask
);
733 writel(intmask
, &cc
->intmask
);
737 static void __exit
unregister_buttons(struct button_t
*b
)
739 int irq
= sb_irq(sbh
) + 2;
742 sb_gpiointmask(sbh
, b
->gpio
, 0);
744 free_irq(irq
, button_handler
);
747 static void __init
register_leds(struct led_t
*l
)
749 struct proc_dir_entry
*p
;
751 leds
= proc_mkdir("led", diag
);
755 for(; l
->name
; l
++) {
756 if (l
->gpio
& gpiomask
)
759 sb_gpioouten(sbh
, l
->gpio
, l
->gpio
);
760 sb_gpiocontrol(sbh
, l
->gpio
, 0);
761 sb_gpioout(sbh
, l
->gpio
, (l
->polarity
== NORMAL
)?0:l
->gpio
);
763 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
764 l
->proc
.type
= PROC_LED
;
766 p
->data
= (void *) &l
->proc
;
767 p
->proc_fops
= &diag_proc_fops
;
772 static void __exit
unregister_leds(struct led_t
*l
)
775 remove_proc_entry(l
->name
, leds
);
777 remove_proc_entry("led", diag
);
780 static void __exit
diag_exit(void)
783 del_timer(&led_timer
);
785 if (platform
.buttons
)
786 unregister_buttons(platform
.buttons
);
789 unregister_leds(platform
.leds
);
791 remove_proc_entry("model", diag
);
792 remove_proc_entry("gpiomask", diag
);
793 remove_proc_entry("diag", NULL
);
796 static struct prochandler_t proc_model
= { .type
= PROC_MODEL
};
797 static struct prochandler_t proc_gpiomask
= { .type
= PROC_GPIOMASK
};
799 static int __init
diag_init(void)
801 static struct proc_dir_entry
*p
;
802 static struct platform_t
*detected
;
804 detected
= platform_detect();
806 printk(MODULE_NAME
": Router model not detected.\n");
809 memcpy(&platform
, detected
, sizeof(struct platform_t
));
811 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
813 if (!(diag
= proc_mkdir("diag", NULL
))) {
814 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
818 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
819 p
->data
= (void *) &proc_model
;
820 p
->proc_fops
= &diag_proc_fops
;
823 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
824 p
->data
= (void *) &proc_gpiomask
;
825 p
->proc_fops
= &diag_proc_fops
;
828 if (platform
.buttons
)
829 register_buttons(platform
.buttons
);
832 register_leds(platform
.leds
);
839 module_init(diag_init
);
840 module_exit(diag_exit
);
842 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
843 MODULE_LICENSE("GPL");
845 /* TODO: export existing sb_irq instead */
846 static int sb_irq(void *sbh
)
851 uint32 flag
, sbipsflag
;
854 regs
= sb_coreregs(sbh
);
855 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
856 flag
= (R_REG(&sb
->sbtpsflag
) & SBTPS_NUM0_MASK
);
858 idx
= sb_coreidx(sbh
);
860 if ((regs
= sb_setcore(sbh
, SB_MIPS
, 0)) ||
861 (regs
= sb_setcore(sbh
, SB_MIPS33
, 0))) {
862 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
864 /* sbipsflag specifies which core is routed to interrupts 1 to 4 */
865 sbipsflag
= R_REG(&sb
->sbipsflag
);
866 for (irq
= 1; irq
<= 4; irq
++, sbipsflag
>>= 8) {
867 if ((sbipsflag
& 0x3f) == flag
)
874 sb_setcoreidx(sbh
, idx
);