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
40 #define sbh bcm947xx_sbh
41 #define sbh_lock bcm947xx_sbh_lock
50 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
56 #define MODULE_NAME "diag"
59 #define FLASH_TIME HZ/6
61 #define EXTIF_ADDR 0x1f000000
62 #define EXTIF_UART (EXTIF_ADDR + 0x00800000)
65 #define GPIO_TYPE_NORMAL (0x0 << 24)
66 #define GPIO_TYPE_EXTIF (0x1 << 24)
67 #define GPIO_TYPE_MASK (0xf << 24)
69 static unsigned int gpiomask = 0;
70 module_param(gpiomask, int, 0644);
84 struct prochandler_t {
90 struct prochandler_t proc;
99 struct prochandler_t proc;
109 struct button_t buttons[MAX_GPIO];
110 struct led_t leds[MAX_GPIO];
137 BUFFALO_UNKNOWN_4710,
158 static struct platform_t platforms[] = {
161 .name = "Linksys WAP54G V1",
163 { .name = "reset", .gpio = 1 << 0 },
166 { .name = "diag", .gpio = 1 << 3 },
167 { .name = "wlan", .gpio = 1 << 4 },
171 .name = "Linksys WAP54G V3",
173 /* FIXME: verify this */
174 { .name = "reset", .gpio = 1 << 7 },
175 { .name = "ses", .gpio = 1 << 0 },
179 { .name = "ses", .gpio = 1 << 1 },
183 .name = "Linksys WRT54G V1.x",
185 { .name = "reset", .gpio = 1 << 6 },
188 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
189 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
193 .name = "Linksys WRT54G*",
195 { .name = "reset", .gpio = 1 << 6 },
196 { .name = "ses", .gpio = 1 << 4 },
199 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
200 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
201 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
202 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
203 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
207 .name = "Linksys WRTSL54GS",
209 { .name = "reset", .gpio = 1 << 6 },
210 { .name = "ses", .gpio = 1 << 4 },
213 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
214 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
215 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
216 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
220 .name = "Linksys WRT54G3G",
222 { .name = "reset", .gpio = 1 << 6 },
223 { .name = "3g", .gpio = 1 << 4 },
226 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
227 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
228 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
229 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
230 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
235 .name = "ASUS WL-HDD",
237 { .name = "reset", .gpio = 1 << 6 },
240 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
244 .name = "ASUS WL-500g",
246 { .name = "reset", .gpio = 1 << 6 },
249 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
253 .name = "ASUS WL-500g",
255 { .name = "reset", .gpio = 1 << 6 },
258 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
262 .name = "ASUS WL-500g Deluxe",
264 { .name = "reset", .gpio = 1 << 6 },
267 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
271 .name = "ASUS WL-500g Premium",
273 { .name = "reset", .gpio = 1 << 0 },
274 { .name = "ses", .gpio = 1 << 4 },
277 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
281 .name = "ASUS (unknown, BCM4702)",
283 { .name = "reset", .gpio = 1 << 6 },
286 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
291 .name = "Buffalo WHR-G54S",
293 { .name = "reset", .gpio = 1 << 4 },
294 { .name = "ses", .gpio = 1 << 0 },
297 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
298 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
299 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
303 .name = "Buffalo WBR2-G54",
306 { .name = "reset", .gpio = 1 << 7 },
309 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
313 .name = "Buffalo WHR-HP-G54",
315 { .name = "reset", .gpio = 1 << 4 },
316 { .name = "bridge", .gpio = 1 << 5 },
317 { .name = "ses", .gpio = 1 << 0 },
320 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
321 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
322 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
326 .name = "Buffalo WHR2-A54G54",
328 { .name = "reset", .gpio = 1 << 4 },
331 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
335 .name = "Buffalo WLA2-G54L",
338 { .name = "reset", .gpio = 1 << 7 },
341 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
344 [BUFFALO_UNKNOWN] = {
345 .name = "Buffalo (unknown)",
347 { .name = "reset", .gpio = 1 << 7 },
350 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
353 [BUFFALO_UNKNOWN_4710] = {
354 .name = "Buffalo (unknown, BCM4710)",
356 { .name = "reset", .gpio = 1 << 4 },
359 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
364 .name = "Siemens SE505 V1",
366 /* No usable buttons */
369 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
370 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
374 .name = "Siemens SE505 V2",
376 /* No usable buttons */
379 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
380 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
381 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
386 .name = "U.S. Robotics USR5461",
388 /* No usable buttons */
391 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
392 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
397 .name = "Dell TrueMobile 2300",
399 { .name = "reset", .gpio = 1 << 0 },
402 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
407 .name = "Motorola WE800G",
409 { .name = "reset", .gpio = 1 << 0 },
412 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
413 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
414 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
418 .name = "Motorola WR850G V1",
420 { .name = "reset", .gpio = 1 << 0 },
423 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
424 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
425 { .name = "modem", .gpio = 1 << 6, .polarity = NORMAL },
426 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
427 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
431 .name = "Motorola WR850G V2",
433 { .name = "reset", .gpio = 1 << 5 },
436 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
437 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
438 { .name = "modem_green",.gpio = 1 << 6, .polarity = REVERSE },
439 { .name = "modem_red", .gpio = 1 << 7, .polarity = REVERSE },
444 .name = "Belkin (unknown)",
445 /* FIXME: verify & add detection */
447 { .name = "reset", .gpio = 1 << 7 },
450 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
451 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
452 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
458 extern spinlock_t sbh_lock;
459 extern char *nvram_get(char *str);
461 static struct proc_dir_entry *diag, *leds;
462 static struct platform_t platform;
463 static void led_flash(unsigned long dummy);
465 #define getvar(str) (nvram_get(str)?:"")
467 static void set_led_extif(struct led_t *led)
469 volatile u8 *addr = (volatile u8 *) KSEG1ADDR(EXTIF_UART) + (led->gpio & ~GPIO_TYPE_MASK);
476 static struct platform_t __init *platform_detect(void)
478 char *boardnum, *boardtype, *buf;
480 boardnum = getvar("boardnum");
481 boardtype = getvar("boardtype");
482 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
483 /* CFE based - newer hardware */
484 if (!strcmp(boardnum, "42")) { /* Linksys */
485 if (!strcmp(boardtype, "0x0101"))
486 return &platforms[WRT54G3G];
488 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
489 return &platforms[WRTSL54GS];
491 /* default to WRT54G */
492 return &platforms[WRT54G];
495 if (!strcmp(boardnum, "45")) { /* ASUS */
496 if (!strcmp(boardtype,"0x042f"))
497 return &platforms[WL500GP];
499 return &platforms[WL500GD];
502 if (!strcmp(boardnum, "10496"))
503 return &platforms[USR5461];
504 } else { /* PMON based - old stuff */
505 if (!strncmp(boardtype, "bcm94710dev", 11)) {
506 if (!strcmp(boardnum, "42"))
507 return &platforms[WRT54GV1];
508 if (simple_strtoul(boardnum, NULL, 9) == 2)
509 return &platforms[WAP54GV1];
511 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
512 return &platforms[WL500G];
513 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
514 /* Either WL-300g or WL-HDD, do more extensive checks */
515 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
516 (simple_strtoul(getvar("et1phyaddr"), NULL, 9) == 1))
517 return &platforms[WLHDD];
518 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
519 (simple_strtoul(getvar("et1phyaddr"), NULL, 9) == 10))
520 return &platforms[WL300G];
523 /* unknown asus stuff, probably bcm4702 */
524 if (!strncmp(boardnum, "asusX", 5))
525 return &platforms[ASUS_4702];
527 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
528 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
529 if (!strncmp(getvar("ModelId"),"WE800G", 6))
530 return &platforms[WE800G];
532 return &platforms[WR850GV1];
536 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
537 /* Buffalo hardware, check id for specific hardware matches */
538 if (!strcmp(buf, "29bb0332"))
539 return &platforms[WBR2_G54];
540 if (!strcmp(buf, "29129"))
541 return &platforms[WLA2_G54L];
542 if (!strcmp(buf, "30189"))
543 return &platforms[WHR_HP_G54];
544 if (!strcmp(buf, "30182"))
545 return &platforms[WHR_G54S];
546 if (!strcmp(buf, "290441dd"))
547 return &platforms[WHR2_A54G54];
550 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
551 if (!strncmp(boardtype, "bcm94710ap", 10))
552 return &platforms[BUFFALO_UNKNOWN_4710];
554 return &platforms[BUFFALO_UNKNOWN];
557 if (!strcmp(getvar("CFEver"), "MotoWRv203"))
558 return &platforms[WR850GV2];
564 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
565 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, void *data);
566 static struct file_operations diag_proc_fops = {
567 read: diag_proc_read,
568 write: diag_proc_write
573 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
576 struct inode *inode = file->f_dentry->d_inode;
577 struct proc_dir_entry *dent = inode->u.generic_ip;
579 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
584 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
587 if (dent->data != NULL) {
588 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
589 switch (handler->type) {
591 struct led_t * led = (struct led_t *) handler->ptr;
593 len = sprintf(page, "f\n");
595 if (led->gpio & GPIO_TYPE_EXTIF) {
596 len = sprintf(page, "%d\n", led->state);
598 int in = (sb_gpioin(sbh) & led->gpio ? 1 : 0);
599 int p = (led->polarity == NORMAL ? 0 : 1);
600 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
606 len = sprintf(page, "%s\n", platform.name);
609 len = sprintf(page, "%d\n", gpiomask);
616 len = min_t(int, len - *ppos, count);
617 if (copy_to_user(buf, (page + *ppos), len)) {
630 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, void *data)
633 struct inode *inode = file->f_dentry->d_inode;
634 struct proc_dir_entry *dent = inode->u.generic_ip;
636 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
641 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
644 if (copy_from_user(page, buf, count)) {
650 if (dent->data != NULL) {
651 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
652 switch (handler->type) {
654 struct led_t *led = (struct led_t *) handler->ptr;
655 int p = (led->polarity == NORMAL ? 0 : 1);
657 if (!(led->gpio & GPIO_TYPE_EXTIF) && (led->gpio & gpiomask))
660 if (page[0] == 'f') {
665 if (led->gpio & GPIO_TYPE_EXTIF) {
666 led->state = p ^ ((page[0] == '1') ? 1 : 0);
669 sb_gpioouten(sbh, led->gpio, led->gpio, GPIO_DRV_PRIORITY);
670 sb_gpiocontrol(sbh, led->gpio, 0, GPIO_DRV_PRIORITY);
671 sb_gpioout(sbh, led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0), GPIO_DRV_PRIORITY);
677 gpiomask = simple_strtoul(page, NULL, 16);
688 struct work_struct tq;
694 static void hotplug_button(struct event_t *event)
696 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
697 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
699 call_usermodehelper (event->argv[0], event->argv, event->envp);
704 static void set_irqenable(int enabled)
706 unsigned int coreidx;
710 spin_lock_irqsave(sbh_lock, flags);
711 coreidx = sb_coreidx(sbh);
712 if ((cc = sb_setcore(sbh, SB_CC, 0))) {
715 intmask = readl(&cc->intmask);
720 writel(intmask, &cc->intmask);
722 sb_setcoreidx(sbh, coreidx);
723 spin_unlock_irqrestore(sbh_lock, flags);
727 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
730 int in = sb_gpioin(sbh);
731 struct event_t *event;
734 for (b = platform.buttons; b->name; b++) {
735 if (b->gpio & gpiomask)
738 if (b->polarity != (in & b->gpio)) {
741 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
743 char *scratch = event->buf;
746 event->argv[i++] = hotplug_path;
747 event->argv[i++] = "button";
751 event->envp[i++] = "HOME=/";
752 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
753 event->envp[i++] = scratch;
754 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
755 event->envp[i++] = scratch;
756 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
757 event->envp[i++] = scratch;
758 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
761 INIT_WORK(&event->tq, (void *)(void *)hotplug_button, (void *)event);
762 schedule_work(&event->tq);
766 b->polarity ^= b->gpio;
767 sb_gpiointpolarity(sbh, b->gpio, b->polarity, GPIO_DRV_PRIORITY);
774 static struct timer_list led_timer = {
778 static void led_flash(unsigned long dummy) {
781 unsigned extif_blink = 0;
783 for (l = platform.leds; l->name; l++) {
785 if (l->gpio & GPIO_TYPE_EXTIF) {
787 l->state = !l->state;
799 val = ~sb_gpioin(sbh);
802 sb_gpioouten(sbh, mask, mask, GPIO_DRV_PRIORITY);
803 sb_gpiocontrol(sbh, mask, 0, GPIO_DRV_PRIORITY);
804 sb_gpioout(sbh, mask, val, GPIO_DRV_PRIORITY);
806 if (mask || extif_blink) {
807 mod_timer(&led_timer, jiffies + FLASH_TIME);
811 static void __init register_buttons(struct button_t *b)
813 int irq = sb_irq(sbh) + 2;
815 request_irq(irq, button_handler, SA_SHIRQ | SA_SAMPLE_RANDOM, "gpio", button_handler);
817 for (; b->name; b++) {
818 if (b->gpio & gpiomask)
821 sb_gpioouten(sbh, b->gpio,0, GPIO_DRV_PRIORITY);
822 sb_gpiocontrol(sbh, b->gpio,0, GPIO_DRV_PRIORITY);
823 b->polarity = sb_gpioin(sbh) & b->gpio;
824 sb_gpiointpolarity(sbh, b->gpio, b->polarity, GPIO_DRV_PRIORITY);
825 sb_gpiointmask(sbh, b->gpio, b->gpio, GPIO_DRV_PRIORITY);
830 static void __exit unregister_buttons(struct button_t *b)
832 int irq = sb_irq(sbh) + 2;
835 sb_gpiointmask(sbh, b->gpio, 0, GPIO_DRV_PRIORITY);
837 free_irq(irq, button_handler);
840 static void __init register_leds(struct led_t *l)
842 struct proc_dir_entry *p;
844 leds = proc_mkdir("led", diag);
848 for(; l->name; l++) {
849 if (l->gpio & gpiomask)
852 if (l->gpio & GPIO_TYPE_EXTIF) {
856 sb_gpioouten(sbh, l->gpio, l->gpio, GPIO_DRV_PRIORITY);
857 sb_gpiocontrol(sbh, l->gpio, 0, GPIO_DRV_PRIORITY);
858 sb_gpioout(sbh, l->gpio, (l->polarity == NORMAL)?0:l->gpio, GPIO_DRV_PRIORITY);
861 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
862 l->proc.type = PROC_LED;
864 p->data = (void *) &l->proc;
865 p->proc_fops = &diag_proc_fops;
870 static void __exit unregister_leds(struct led_t *l)
873 remove_proc_entry(l->name, leds);
875 remove_proc_entry("led", diag);
878 static void __exit diag_exit(void)
881 del_timer(&led_timer);
883 if (platform.buttons)
884 unregister_buttons(platform.buttons);
887 unregister_leds(platform.leds);
889 remove_proc_entry("model", diag);
890 remove_proc_entry("gpiomask", diag);
891 remove_proc_entry("diag", NULL);
894 static struct prochandler_t proc_model = { .type = PROC_MODEL };
895 static struct prochandler_t proc_gpiomask = { .type = PROC_GPIOMASK };
897 static int __init diag_init(void)
899 static struct proc_dir_entry *p;
900 static struct platform_t *detected;
902 detected = platform_detect();
904 printk(MODULE_NAME ": Router model not detected.\n");
907 memcpy(&platform, detected, sizeof(struct platform_t));
909 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
911 if (!(diag = proc_mkdir("diag", NULL))) {
912 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
916 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
917 p->data = (void *) &proc_model;
918 p->proc_fops = &diag_proc_fops;
921 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
922 p->data = (void *) &proc_gpiomask;
923 p->proc_fops = &diag_proc_fops;
926 if (platform.buttons)
927 register_buttons(platform.buttons);
930 register_leds(platform.leds);
935 module_init(diag_init);
936 module_exit(diag_exit);
938 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
939 MODULE_LICENSE("GPL");