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 },
201 .name = "ASUS WL-300g",
203 { .name = "reset", .gpio = 1 << 6 },
206 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
210 .name = "ASUS WL-500g",
212 { .name = "reset", .gpio = 1 << 6 },
215 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
219 .name = "ASUS WL-500g Deluxe",
221 { .name = "reset", .gpio = 1 << 6 },
224 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
228 .name = "ASUS WL-500g Premium",
230 { .name = "reset", .gpio = 1 << 0 },
231 { .name = "ses", .gpio = 1 << 4 },
234 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
238 .name = "ASUS (unknown, BCM4702)",
240 { .name = "reset", .gpio = 1 << 6 },
243 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
248 .name = "Buffalo WHR-G54S",
250 { .name = "reset", .gpio = 1 << 4 },
251 { .name = "bridge", .gpio = 1 << 5 },
252 { .name = "ses", .gpio = 1 << 0 },
255 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
256 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
257 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
258 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
262 .name = "Buffalo WBR2-G54",
265 { .name = "reset", .gpio = 1 << 7 },
268 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
272 .name = "Buffalo WHR-HP-G54",
274 { .name = "reset", .gpio = 1 << 4 },
275 { .name = "bridge", .gpio = 1 << 5 },
276 { .name = "ses", .gpio = 1 << 0 },
279 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
280 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
281 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
285 .name = "Buffalo WHR2-A54G54",
287 { .name = "reset", .gpio = 1 << 4 },
290 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
294 .name = "Buffalo WLA2-G54L",
297 { .name = "reset", .gpio = 1 << 7 },
300 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
304 .name = "Buffalo WZR-G300N",
306 { .name = "reset", .gpio = 1 << 4 },
309 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
310 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
311 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
315 .name = "Buffalo WZR-RS-G54",
317 { .name = "ses", .gpio = 1 << 0 },
318 { .name = "reset", .gpio = 1 << 4 },
321 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
322 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
323 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
327 .name = "Buffalo WZR-RS-G54HP",
329 { .name = "ses", .gpio = 1 << 0 },
330 { .name = "reset", .gpio = 1 << 4 },
333 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
334 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
335 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
338 [BUFFALO_UNKNOWN] = {
339 .name = "Buffalo (unknown)",
341 { .name = "reset", .gpio = 1 << 7 },
344 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
347 [BUFFALO_UNKNOWN_4710] = {
348 .name = "Buffalo (unknown, BCM4710)",
350 { .name = "reset", .gpio = 1 << 4 },
353 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
358 .name = "Siemens SE505 V1",
360 /* No usable buttons */
363 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
364 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
368 .name = "Siemens SE505 V2",
370 /* No usable buttons */
373 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
374 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
375 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
380 .name = "U.S. Robotics USR5461",
382 /* No usable buttons */
385 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
386 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
391 .name = "Dell TrueMobile 2300",
393 { .name = "reset", .gpio = 1 << 0 },
396 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
401 .name = "Motorola WE800G",
403 { .name = "reset", .gpio = 1 << 0 },
406 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
407 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
408 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
412 .name = "Motorola WR850G V1",
414 { .name = "reset", .gpio = 1 << 0 },
417 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
418 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
419 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
420 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
421 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
425 .name = "Motorola WR850G V2/V3",
427 { .name = "reset", .gpio = 1 << 5 },
430 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
431 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
432 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
433 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
438 .name = "Belkin (unknown)",
439 /* FIXME: verify & add detection */
441 { .name = "reset", .gpio = 1 << 7 },
444 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
445 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
446 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
451 .name = "Netgear WGT634U",
453 { .name = "reset", .gpio = 1 << 2 },
456 { .name = "power", .gpio = 1 << 3, .polarity = REVERSE },
461 .name = "Trendware TEW411BRP+",
463 { /* No usable buttons */ },
466 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
467 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
468 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
473 static struct platform_t __init *platform_detect(void)
475 char *boardnum, *boardtype, *buf;
477 boardnum = getvar("boardnum");
478 boardtype = getvar("boardtype");
479 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
480 /* CFE based - newer hardware */
481 if (!strcmp(boardnum, "42")) { /* Linksys */
482 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), 1))
483 return &platforms[WRT350N];
485 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
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 ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
506 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
507 if (!strncmp(getvar("ModelId"),"WE800G", 6))
508 return &platforms[WE800G];
510 return &platforms[WR850GV1];
512 if (!strncmp(boardtype, "bcm94710dev", 11)) {
513 if (!strcmp(boardnum, "42"))
514 return &platforms[WRT54GV1];
515 if (simple_strtoul(boardnum, NULL, 0) == 2)
516 return &platforms[WAP54GV1];
518 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
519 return &platforms[WL500G];
520 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
521 /* Either WL-300g or WL-HDD, do more extensive checks */
522 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
523 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
524 return &platforms[WLHDD];
525 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
526 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
527 return &platforms[WL300G];
530 /* unknown asus stuff, probably bcm4702 */
531 if (!strncmp(boardnum, "asusX", 5))
532 return &platforms[ASUS_4702];
535 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
536 /* Buffalo hardware, check id for specific hardware matches */
537 if (!strcmp(buf, "29bb0332"))
538 return &platforms[WBR2_G54];
539 if (!strcmp(buf, "29129"))
540 return &platforms[WLA2_G54L];
541 if (!strcmp(buf, "30189"))
542 return &platforms[WHR_HP_G54];
543 if (!strcmp(buf, "30182"))
544 return &platforms[WHR_G54S];
545 if (!strcmp(buf, "290441dd"))
546 return &platforms[WHR2_A54G54];
547 if (!strcmp(buf, "31120"))
548 return &platforms[WZR_G300N];
549 if (!strcmp(buf, "30083"))
550 return &platforms[WZR_RS_G54];
551 if (!strcmp(buf, "30103"))
552 return &platforms[WZR_RS_G54HP];
555 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
556 if (!strncmp(boardtype, "bcm94710ap", 10))
557 return &platforms[BUFFALO_UNKNOWN_4710];
559 return &platforms[BUFFALO_UNKNOWN];
562 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
563 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
565 return &platforms[WR850GV2V3];
568 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
569 return &platforms[TEW411BRPP];
576 static void register_buttons(struct button_t *b)
579 platform.button_mask |= b->gpio;
581 platform.button_mask &= ~gpiomask;
583 gpio_outen(platform.button_mask, 0);
584 gpio_control(platform.button_mask, 0);
585 platform.button_polarity = gpio_in() & platform.button_mask;
586 gpio_intpolarity(platform.button_mask, platform.button_polarity);
587 gpio_intmask(platform.button_mask, platform.button_mask);
589 gpio_set_irqenable(1, button_handler);
592 static void unregister_buttons(struct button_t *b)
594 gpio_intmask(platform.button_mask, 0);
596 gpio_set_irqenable(0, button_handler);
599 static void hotplug_button(struct event_t *event)
601 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
602 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
604 call_usermodehelper (event->argv[0], event->argv, event->envp);
609 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
614 in = gpio_in() & platform.button_mask;
615 gpio_intpolarity(platform.button_mask, in);
616 changed = platform.button_polarity ^ in;
617 platform.button_polarity = in;
619 changed &= ~gpio_outen(0, 0);
621 for (b = platform.buttons; b->name; b++) {
622 struct event_t *event;
624 if (!(b->gpio & changed)) continue;
628 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
630 char *scratch = event->buf;
633 event->argv[i++] = hotplug_path;
634 event->argv[i++] = "button";
638 event->envp[i++] = "HOME=/";
639 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
640 event->envp[i++] = scratch;
641 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
642 event->envp[i++] = scratch;
643 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
644 event->envp[i++] = scratch;
645 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
648 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
649 schedule_work(&event->wq);
657 static void register_leds(struct led_t *l)
659 struct proc_dir_entry *p;
663 leds = proc_mkdir("led", diag);
667 for(; l->name; l++) {
668 if (l->gpio & gpiomask)
671 if (l->gpio & GPIO_TYPE_EXTIF) {
676 val |= (l->polarity == NORMAL)?0:l->gpio;
679 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
680 l->proc.type = PROC_LED;
682 p->data = (void *) &l->proc;
683 p->proc_fops = &diag_proc_fops;
687 gpio_outen(mask, mask);
688 gpio_control(mask, 0);
692 static void unregister_leds(struct led_t *l)
695 remove_proc_entry(l->name, leds);
697 remove_proc_entry("led", diag);
700 static void set_led_extif(struct led_t *led)
702 gpio_set_extif(led->gpio, led->state);
705 static void led_flash(unsigned long dummy) {
710 for (l = platform.leds; l->name; l++) {
712 if (l->gpio & GPIO_TYPE_EXTIF) {
714 l->state = !l->state;
724 u32 val = ~gpio_in();
726 gpio_outen(mask, mask);
727 gpio_control(mask, 0);
730 if (mask || extif_blink) {
731 mod_timer(&led_timer, jiffies + FLASH_TIME);
735 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
738 struct inode *inode = file->f_dentry->d_inode;
739 struct proc_dir_entry *dent = inode->u.generic_ip;
741 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
746 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
749 if (dent->data != NULL) {
750 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
751 switch (handler->type) {
753 struct led_t * led = (struct led_t *) handler->ptr;
755 len = sprintf(page, "f\n");
757 if (led->gpio & GPIO_TYPE_EXTIF) {
758 len = sprintf(page, "%d\n", led->state);
760 u32 in = (gpio_in() & led->gpio ? 1 : 0);
761 u8 p = (led->polarity == NORMAL ? 0 : 1);
762 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
768 len = sprintf(page, "%s\n", platform.name);
771 len = sprintf(page, "0x%04x\n", gpiomask);
778 len = min_t(int, len - *ppos, count);
779 if (copy_to_user(buf, (page + *ppos), len)) {
792 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
795 struct inode *inode = file->f_dentry->d_inode;
796 struct proc_dir_entry *dent = inode->u.generic_ip;
798 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
803 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
806 if (copy_from_user(page, buf, count)) {
812 if (dent->data != NULL) {
813 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
814 switch (handler->type) {
816 struct led_t *led = (struct led_t *) handler->ptr;
817 int p = (led->polarity == NORMAL ? 0 : 1);
819 if (page[0] == 'f') {
824 if (led->gpio & GPIO_TYPE_EXTIF) {
825 led->state = p ^ ((page[0] == '1') ? 1 : 0);
828 gpio_outen(led->gpio, led->gpio);
829 gpio_control(led->gpio, 0);
830 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
836 gpiomask = simple_strtoul(page, NULL, 0);
838 if (platform.buttons) {
839 unregister_buttons(platform.buttons);
840 register_buttons(platform.buttons);
844 unregister_leds(platform.leds);
845 register_leds(platform.leds);
856 static int __init diag_init(void)
858 static struct proc_dir_entry *p;
859 static struct platform_t *detected;
861 detected = platform_detect();
863 printk(MODULE_NAME ": Router model not detected.\n");
866 memcpy(&platform, detected, sizeof(struct platform_t));
868 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
870 if (!(diag = proc_mkdir("diag", NULL))) {
871 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
875 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
876 p->data = (void *) &proc_model;
877 p->proc_fops = &diag_proc_fops;
880 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
881 p->data = (void *) &proc_gpiomask;
882 p->proc_fops = &diag_proc_fops;
885 if (platform.buttons)
886 register_buttons(platform.buttons);
889 register_leds(platform.leds);
894 static void __exit diag_exit(void)
897 del_timer(&led_timer);
899 if (platform.buttons)
900 unregister_buttons(platform.buttons);
903 unregister_leds(platform.leds);
905 remove_proc_entry("model", diag);
906 remove_proc_entry("gpiomask", diag);
907 remove_proc_entry("diag", NULL);
910 module_init(diag_init);
911 module_exit(diag_exit);
913 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
914 MODULE_LICENSE("GPL");