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 = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
397 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
402 .name = "Motorola WE800G",
404 { .name = "reset", .gpio = 1 << 0 },
407 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
408 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
409 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
413 .name = "Motorola WR850G V1",
415 { .name = "reset", .gpio = 1 << 0 },
418 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
419 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
420 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
421 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
422 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
426 .name = "Motorola WR850G V2/V3",
428 { .name = "reset", .gpio = 1 << 5 },
431 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
432 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
433 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
434 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
439 .name = "Belkin (unknown)",
440 /* FIXME: verify & add detection */
442 { .name = "reset", .gpio = 1 << 7 },
445 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
446 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
447 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
452 .name = "Netgear WGT634U",
454 { .name = "reset", .gpio = 1 << 2 },
457 { .name = "power", .gpio = 1 << 3, .polarity = REVERSE },
462 .name = "Trendware TEW411BRP+",
464 { /* No usable buttons */ },
467 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
468 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
469 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
474 static struct platform_t __init *platform_detect(void)
476 char *boardnum, *boardtype, *buf;
478 boardnum = getvar("boardnum");
479 boardtype = getvar("boardtype");
480 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
481 /* CFE based - newer hardware */
482 if (!strcmp(boardnum, "42")) { /* Linksys */
483 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), 1))
484 return &platforms[WRT350N];
486 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
487 return &platforms[WRT54G3G];
489 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
490 return &platforms[WRTSL54GS];
492 /* default to WRT54G */
493 return &platforms[WRT54G];
496 if (!strcmp(boardnum, "45")) { /* ASUS */
497 if (!strcmp(boardtype,"0x042f"))
498 return &platforms[WL500GP];
500 return &platforms[WL500GD];
503 if (!strcmp(boardnum, "10496"))
504 return &platforms[USR5461];
505 } else { /* PMON based - old stuff */
507 /* Dell TrueMobile 2300 */
508 if (!strcmp(getvar("ModelId"),"WX-5565"))
509 return &platforms[TM2300];
511 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
512 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
513 if (!strncmp(getvar("ModelId"),"WE800G", 6))
514 return &platforms[WE800G];
516 return &platforms[WR850GV1];
518 if (!strncmp(boardtype, "bcm94710dev", 11)) {
519 if (!strcmp(boardnum, "42"))
520 return &platforms[WRT54GV1];
521 if (simple_strtoul(boardnum, NULL, 0) == 2)
522 return &platforms[WAP54GV1];
524 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
525 return &platforms[WL500G];
526 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
527 /* Either WL-300g or WL-HDD, do more extensive checks */
528 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
529 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
530 return &platforms[WLHDD];
531 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
532 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
533 return &platforms[WL300G];
536 /* unknown asus stuff, probably bcm4702 */
537 if (!strncmp(boardnum, "asusX", 5))
538 return &platforms[ASUS_4702];
541 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
542 /* Buffalo hardware, check id for specific hardware matches */
543 if (!strcmp(buf, "29bb0332"))
544 return &platforms[WBR2_G54];
545 if (!strcmp(buf, "29129"))
546 return &platforms[WLA2_G54L];
547 if (!strcmp(buf, "30189"))
548 return &platforms[WHR_HP_G54];
549 if (!strcmp(buf, "30182"))
550 return &platforms[WHR_G54S];
551 if (!strcmp(buf, "290441dd"))
552 return &platforms[WHR2_A54G54];
553 if (!strcmp(buf, "31120"))
554 return &platforms[WZR_G300N];
555 if (!strcmp(buf, "30083"))
556 return &platforms[WZR_RS_G54];
557 if (!strcmp(buf, "30103"))
558 return &platforms[WZR_RS_G54HP];
561 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
562 if (!strncmp(boardtype, "bcm94710ap", 10))
563 return &platforms[BUFFALO_UNKNOWN_4710];
565 return &platforms[BUFFALO_UNKNOWN];
568 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
569 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
571 return &platforms[WR850GV2V3];
574 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
575 return &platforms[TEW411BRPP];
582 static void register_buttons(struct button_t *b)
585 platform.button_mask |= b->gpio;
587 platform.button_mask &= ~gpiomask;
589 gpio_outen(platform.button_mask, 0);
590 gpio_control(platform.button_mask, 0);
591 platform.button_polarity = gpio_in() & platform.button_mask;
592 gpio_intpolarity(platform.button_mask, platform.button_polarity);
593 gpio_intmask(platform.button_mask, platform.button_mask);
595 gpio_set_irqenable(1, button_handler);
598 static void unregister_buttons(struct button_t *b)
600 gpio_intmask(platform.button_mask, 0);
602 gpio_set_irqenable(0, button_handler);
605 static void hotplug_button(struct event_t *event)
607 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
608 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
610 call_usermodehelper (event->argv[0], event->argv, event->envp);
615 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
620 in = gpio_in() & platform.button_mask;
621 gpio_intpolarity(platform.button_mask, in);
622 changed = platform.button_polarity ^ in;
623 platform.button_polarity = in;
625 changed &= ~gpio_outen(0, 0);
627 for (b = platform.buttons; b->name; b++) {
628 struct event_t *event;
630 if (!(b->gpio & changed)) continue;
634 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
636 char *scratch = event->buf;
639 event->argv[i++] = hotplug_path;
640 event->argv[i++] = "button";
644 event->envp[i++] = "HOME=/";
645 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
646 event->envp[i++] = scratch;
647 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
648 event->envp[i++] = scratch;
649 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
650 event->envp[i++] = scratch;
651 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
654 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
655 schedule_work(&event->wq);
663 static void register_leds(struct led_t *l)
665 struct proc_dir_entry *p;
669 leds = proc_mkdir("led", diag);
673 for(; l->name; l++) {
674 if (l->gpio & gpiomask)
677 if (l->gpio & GPIO_TYPE_EXTIF) {
682 val |= (l->polarity == NORMAL)?0:l->gpio;
685 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
686 l->proc.type = PROC_LED;
688 p->data = (void *) &l->proc;
689 p->proc_fops = &diag_proc_fops;
693 gpio_outen(mask, mask);
694 gpio_control(mask, 0);
698 static void unregister_leds(struct led_t *l)
701 remove_proc_entry(l->name, leds);
703 remove_proc_entry("led", diag);
706 static void set_led_extif(struct led_t *led)
708 gpio_set_extif(led->gpio, led->state);
711 static void led_flash(unsigned long dummy) {
716 for (l = platform.leds; l->name; l++) {
718 if (l->gpio & GPIO_TYPE_EXTIF) {
720 l->state = !l->state;
730 u32 val = ~gpio_in();
732 gpio_outen(mask, mask);
733 gpio_control(mask, 0);
736 if (mask || extif_blink) {
737 mod_timer(&led_timer, jiffies + FLASH_TIME);
741 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
744 struct inode *inode = file->f_dentry->d_inode;
745 struct proc_dir_entry *dent = inode->u.generic_ip;
747 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
752 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
755 if (dent->data != NULL) {
756 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
757 switch (handler->type) {
759 struct led_t * led = (struct led_t *) handler->ptr;
761 len = sprintf(page, "f\n");
763 if (led->gpio & GPIO_TYPE_EXTIF) {
764 len = sprintf(page, "%d\n", led->state);
766 u32 in = (gpio_in() & led->gpio ? 1 : 0);
767 u8 p = (led->polarity == NORMAL ? 0 : 1);
768 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
774 len = sprintf(page, "%s\n", platform.name);
777 len = sprintf(page, "0x%04x\n", gpiomask);
784 len = min_t(int, len - *ppos, count);
785 if (copy_to_user(buf, (page + *ppos), len)) {
798 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
801 struct inode *inode = file->f_dentry->d_inode;
802 struct proc_dir_entry *dent = inode->u.generic_ip;
804 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
809 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
812 if (copy_from_user(page, buf, count)) {
818 if (dent->data != NULL) {
819 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
820 switch (handler->type) {
822 struct led_t *led = (struct led_t *) handler->ptr;
823 int p = (led->polarity == NORMAL ? 0 : 1);
825 if (page[0] == 'f') {
830 if (led->gpio & GPIO_TYPE_EXTIF) {
831 led->state = p ^ ((page[0] == '1') ? 1 : 0);
834 gpio_outen(led->gpio, led->gpio);
835 gpio_control(led->gpio, 0);
836 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
842 gpiomask = simple_strtoul(page, NULL, 0);
844 if (platform.buttons) {
845 unregister_buttons(platform.buttons);
846 register_buttons(platform.buttons);
850 unregister_leds(platform.leds);
851 register_leds(platform.leds);
862 static int __init diag_init(void)
864 static struct proc_dir_entry *p;
865 static struct platform_t *detected;
867 detected = platform_detect();
869 printk(MODULE_NAME ": Router model not detected.\n");
872 memcpy(&platform, detected, sizeof(struct platform_t));
874 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
876 if (!(diag = proc_mkdir("diag", NULL))) {
877 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
881 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
882 p->data = (void *) &proc_model;
883 p->proc_fops = &diag_proc_fops;
886 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
887 p->data = (void *) &proc_gpiomask;
888 p->proc_fops = &diag_proc_fops;
891 if (platform.buttons)
892 register_buttons(platform.buttons);
895 register_leds(platform.leds);
900 static void __exit diag_exit(void)
903 del_timer(&led_timer);
905 if (platform.buttons)
906 unregister_buttons(platform.buttons);
909 unregister_leds(platform.leds);
911 remove_proc_entry("model", diag);
912 remove_proc_entry("gpiomask", diag);
913 remove_proc_entry("diag", NULL);
916 module_init(diag_init);
917 module_exit(diag_exit);
919 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
920 MODULE_LICENSE("GPL");