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",
289 { .name
= "reset", .gpio
= 1 << 4 },
290 { .name
= "ses", .gpio
= 1 << 0 },
293 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
294 { .name
= "internal", .gpio
= 1 << 3, .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
},
396 .name
= "Belkin (unknown)"
397 /* FIXME: verify & add detection */
399 { .name
= "reset", .gpio
= 1 << 7 },
402 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
403 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
404 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
409 static struct proc_dir_entry
*diag
, *leds
;
411 extern void *bcm947xx_sbh
;
412 #define sbh bcm947xx_sbh
414 static int sb_irq(void *sbh
);
415 static struct platform_t platform
;
417 extern char *nvram_get(char *str
);
419 static void led_flash(unsigned long dummy
);
421 static inline char __init
*getvar(char *str
)
423 return nvram_get(str
)?:"";
426 static struct platform_t __init
*platform_detect(void)
428 char *boardnum
, *boardtype
, *buf
;
430 boardnum
= getvar("boardnum");
431 boardtype
= getvar("boardtype");
432 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
433 /* CFE based - newer hardware */
434 if (!strcmp(boardnum
, "42")) { /* Linksys */
435 if (!strcmp(boardtype
, "0x0101"))
436 return &platforms
[WRT54G3G
];
438 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
439 return &platforms
[WRTSL54GS
];
441 /* default to WRT54G */
442 return &platforms
[WRT54G
];
445 if (!strcmp(boardnum
, "45")) { /* ASUS */
446 if (!strcmp(boardtype
,"0x042f"))
447 return &platforms
[WL500GP
];
449 return &platforms
[WL500GD
];
452 if (!strcmp(boardnum
, "10496"))
453 return &platforms
[USR5461
];
454 } else { /* PMON based - old stuff */
455 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
456 if (!strcmp(boardtype
, "42"))
457 return &platforms
[WRT54GV1
];
458 if (simple_strtoul(boardnum
, NULL
, 9) == 2)
459 return &platforms
[WAP54GV1
];
461 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
462 return &platforms
[WL500G
];
463 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
464 /* Either WL-300g or WL-HDD, do more extensive checks */
465 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
466 (simple_strtoul(getvar("et1phyaddr"), NULL
, 9) == 1))
467 return &platforms
[WLHDD
];
468 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
469 (simple_strtoul(getvar("et1phyaddr"), NULL
, 9) == 10))
470 return &platforms
[WL300G
];
473 /* unknown asus stuff, probably bcm4702 */
474 if (!strncmp(boardnum
, "asusX", 5))
475 return &platforms
[ASUS_4702
];
477 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
478 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30))
479 return &platforms
[WR850GV1
];
482 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
483 /* Buffalo hardware, check id for specific hardware matches */
484 if (!strcmp(buf
, "29bb0332"))
485 return &platforms
[WBR2_G54
];
486 if (!strcmp(buf
, "29129"))
487 return &platforms
[WLA2_G54L
];
488 if (!strcmp(buf
, "30189"))
489 return &platforms
[WHR_HP_G54
];
490 if (!strcmp(buf
, "30182"))
491 return &platforms
[WHR_G54S
];
494 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
495 if (!strncmp(boardtype
, "bcm94710ap", 10))
496 return &platforms
[BUFFALO_UNKNOWN_4710
];
498 return &platforms
[BUFFALO_UNKNOWN
];
501 if (!strcmp(getvar("CFEver"), "MotoWRv203"))
502 return &platforms
[WR850GV2
];
508 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
);
509 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
);
510 static struct file_operations diag_proc_fops
= {
511 read
: diag_proc_read
,
512 write
: diag_proc_write
517 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
520 struct inode
*inode
= file
->f_dentry
->d_inode
;
521 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
523 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
528 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
531 if (dent
->data
!= NULL
) {
532 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
533 switch (handler
->type
) {
535 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
537 len
= sprintf(page
, "f\n");
539 int in
= (sb_gpioin(sbh
) & led
->gpio
? 1 : 0);
540 int p
= (led
->polarity
== NORMAL
? 0 : 1);
541 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
546 len
= sprintf(page
, "%s\n", platform
.name
);
549 len
= sprintf(page
, "%d\n", gpiomask
);
556 len
= min_t(int, len
- *ppos
, count
);
557 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
570 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
)
573 struct inode
*inode
= file
->f_dentry
->d_inode
;
574 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
576 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
581 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
584 if (copy_from_user(page
, buf
, count
)) {
590 if (dent
->data
!= NULL
) {
591 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
592 switch (handler
->type
) {
594 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
595 int p
= (led
->polarity
== NORMAL
? 0 : 1);
597 if (led
->gpio
& gpiomask
)
600 if (page
[0] == 'f') {
605 sb_gpioouten(sbh
, led
->gpio
, led
->gpio
);
606 sb_gpiocontrol(sbh
, led
->gpio
, 0);
607 sb_gpioout(sbh
, led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
612 gpiomask
= simple_strtoul(page
, NULL
, 16);
629 static void hotplug_button(struct event_t
*event
)
631 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
635 static void button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
638 int in
= sb_gpioin(sbh
);
639 struct event_t
*event
;
641 for (b
= platform
.buttons
; b
->name
; b
++) {
642 if (b
->gpio
& gpiomask
)
645 if (b
->polarity
!= (in
& b
->gpio
)) {
649 if ((event
= (struct event_t
*)kmalloc (256, GFP_KERNEL
))) {
651 char *scratch
= event
->buf
;
654 event
->argv
[i
++] = hotplug_path
;
655 event
->argv
[i
++] = "button";
659 event
->envp
[i
++] = "HOME=/";
660 event
->envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
661 event
->envp
[i
++] = scratch
;
662 scratch
+= sprintf (scratch
, "ACTION=%s", b
->pressed
?"pressed":"released") + 1;
663 event
->envp
[i
++] = scratch
;
664 scratch
+= sprintf (scratch
, "BUTTON=%s", b
->name
) + 1;
665 event
->envp
[i
++] = scratch
;
666 scratch
+= sprintf (scratch
, "SEEN=%ld", (jiffies
- b
->seen
)/HZ
) + 1;
669 INIT_TQUEUE(&event
->tq
, (void *)(void *)hotplug_button
, (void *)event
);
670 schedule_task(&event
->tq
);
674 b
->polarity
^= b
->gpio
;
675 sb_gpiointpolarity(sbh
, b
->gpio
, b
->polarity
);
680 static struct timer_list led_timer
= {
684 static void led_flash(unsigned long dummy
) {
688 for (l
= platform
.leds
; l
->name
; l
++) {
698 val
= ~sb_gpioin(sbh
);
701 sb_gpioouten(sbh
, mask
, mask
);
702 sb_gpiocontrol(sbh
, mask
, 0);
703 sb_gpioout(sbh
, mask
, val
);
705 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
709 static void __init
register_buttons(struct button_t
*b
)
711 int irq
= sb_irq(sbh
) + 2;
714 request_irq(irq
, button_handler
, SA_SHIRQ
| SA_SAMPLE_RANDOM
, "gpio", button_handler
);
716 for (; b
->name
; b
++) {
717 if (b
->gpio
& gpiomask
)
720 sb_gpioouten(sbh
, b
->gpio
,0);
721 sb_gpiocontrol(sbh
, b
->gpio
,0);
722 b
->polarity
= sb_gpioin(sbh
) & b
->gpio
;
723 sb_gpiointpolarity(sbh
, b
->gpio
, b
->polarity
);
724 sb_gpiointmask(sbh
, b
->gpio
, b
->gpio
);
727 if ((cc
= sb_setcore(sbh
, SB_CC
, 0))) {
730 intmask
= readl(&cc
->intmask
);
732 writel(intmask
, &cc
->intmask
);
736 static void __exit
unregister_buttons(struct button_t
*b
)
738 int irq
= sb_irq(sbh
) + 2;
741 sb_gpiointmask(sbh
, b
->gpio
, 0);
743 free_irq(irq
, button_handler
);
746 static void __init
register_leds(struct led_t
*l
)
748 struct proc_dir_entry
*p
;
750 leds
= proc_mkdir("led", diag
);
754 for(; l
->name
; l
++) {
755 if (l
->gpio
& gpiomask
)
758 sb_gpioouten(sbh
, l
->gpio
, l
->gpio
);
759 sb_gpiocontrol(sbh
, l
->gpio
, 0);
760 sb_gpioout(sbh
, l
->gpio
, (l
->polarity
== NORMAL
)?0:l
->gpio
);
762 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
763 l
->proc
.type
= PROC_LED
;
765 p
->data
= (void *) &l
->proc
;
766 p
->proc_fops
= &diag_proc_fops
;
771 static void __exit
unregister_leds(struct led_t
*l
)
774 remove_proc_entry(l
->name
, leds
);
776 remove_proc_entry("led", diag
);
779 static void __exit
diag_exit(void)
782 del_timer(&led_timer
);
784 if (platform
.buttons
)
785 unregister_buttons(platform
.buttons
);
788 unregister_leds(platform
.leds
);
790 remove_proc_entry("model", diag
);
791 remove_proc_entry("gpiomask", diag
);
792 remove_proc_entry("diag", NULL
);
795 static struct prochandler_t proc_model
= { .type
= PROC_MODEL
};
796 static struct prochandler_t proc_gpiomask
= { .type
= PROC_GPIOMASK
};
798 static int __init
diag_init(void)
800 static struct proc_dir_entry
*p
;
801 static struct platform_t
*detected
;
803 detected
= platform_detect();
805 printk(MODULE_NAME
": Router model not detected.\n");
808 memcpy(&platform
, detected
, sizeof(struct platform_t
));
810 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
812 if (!(diag
= proc_mkdir("diag", NULL
))) {
813 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
817 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
818 p
->data
= (void *) &proc_model
;
819 p
->proc_fops
= &diag_proc_fops
;
822 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
823 p
->data
= (void *) &proc_gpiomask
;
824 p
->proc_fops
= &diag_proc_fops
;
827 if (platform
.buttons
)
828 register_buttons(platform
.buttons
);
831 register_leds(platform
.leds
);
838 module_init(diag_init
);
839 module_exit(diag_exit
);
841 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
842 MODULE_LICENSE("GPL");
844 /* TODO: export existing sb_irq instead */
845 static int sb_irq(void *sbh
)
850 uint32 flag
, sbipsflag
;
853 regs
= sb_coreregs(sbh
);
854 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
855 flag
= (R_REG(&sb
->sbtpsflag
) & SBTPS_NUM0_MASK
);
857 idx
= sb_coreidx(sbh
);
859 if ((regs
= sb_setcore(sbh
, SB_MIPS
, 0)) ||
860 (regs
= sb_setcore(sbh
, SB_MIPS33
, 0))) {
861 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
863 /* sbipsflag specifies which core is routed to interrupts 1 to 4 */
864 sbipsflag
= R_REG(&sb
->sbipsflag
);
865 for (irq
= 1; irq
<= 4; irq
++, sbipsflag
>>= 8) {
866 if ((sbipsflag
& 0x3f) == flag
)
873 sb_setcoreidx(sbh
, idx
);