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);
103 static struct platform_t __initdata platforms[] = {
106 .name = "Linksys WAP54G V1",
108 { .name = "reset", .gpio = 1 << 0 },
111 { .name = "diag", .gpio = 1 << 3 },
112 { .name = "wlan", .gpio = 1 << 4 },
116 .name = "Linksys WAP54G V3",
118 /* FIXME: verify this */
119 { .name = "reset", .gpio = 1 << 7 },
120 { .name = "ses", .gpio = 1 << 0 },
124 { .name = "ses", .gpio = 1 << 1 },
128 .name = "Linksys WRT54G V1.x",
130 { .name = "reset", .gpio = 1 << 6 },
133 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
134 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
138 .name = "Linksys WRT54G/GS/GL",
140 { .name = "reset", .gpio = 1 << 6 },
141 { .name = "ses", .gpio = 1 << 4 },
144 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
145 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
146 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
147 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
148 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
152 .name = "Linksys WRTSL54GS",
154 { .name = "reset", .gpio = 1 << 6 },
155 { .name = "ses", .gpio = 1 << 4 },
158 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
159 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
160 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
161 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
165 .name = "Linksys WRT54G3G",
167 { .name = "reset", .gpio = 1 << 6 },
168 { .name = "3g", .gpio = 1 << 4 },
171 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
172 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
173 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
174 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
175 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
179 .name = "Linksys WRT350N",
181 { .name = "reset", .gpio = 1 << 6 },
182 { .name = "ses", .gpio = 1 << 8 },
185 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
186 { .name = "ses", .gpio = 1 << 3, .polarity = REVERSE },
191 .name = "ASUS WL-HDD",
193 { .name = "reset", .gpio = 1 << 6 },
196 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
200 .name = "ASUS WL-300g",
202 { .name = "reset", .gpio = 1 << 6 },
205 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
209 .name = "ASUS WL-500g",
211 { .name = "reset", .gpio = 1 << 6 },
214 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
218 .name = "ASUS WL-500g Deluxe",
220 { .name = "reset", .gpio = 1 << 6 },
223 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
227 .name = "ASUS WL-500g Premium",
229 { .name = "reset", .gpio = 1 << 0 },
230 { .name = "ses", .gpio = 1 << 4 },
233 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
237 .name = "ASUS (unknown, BCM4702)",
239 { .name = "reset", .gpio = 1 << 6 },
242 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
247 .name = "Buffalo WHR-G54S",
249 { .name = "reset", .gpio = 1 << 4 },
250 { .name = "bridge", .gpio = 1 << 5 },
251 { .name = "ses", .gpio = 1 << 0 },
254 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
255 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
256 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
257 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
261 .name = "Buffalo WBR2-G54",
264 { .name = "reset", .gpio = 1 << 7 },
267 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
271 .name = "Buffalo WHR-HP-G54",
273 { .name = "reset", .gpio = 1 << 4 },
274 { .name = "bridge", .gpio = 1 << 5 },
275 { .name = "ses", .gpio = 1 << 0 },
278 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
279 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
280 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
284 .name = "Buffalo WHR2-A54G54",
286 { .name = "reset", .gpio = 1 << 4 },
289 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
293 .name = "Buffalo WLA2-G54L",
296 { .name = "reset", .gpio = 1 << 7 },
299 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
303 .name = "Buffalo WZR-RS-G54",
305 { .name = "ses", .gpio = 1 << 0 },
306 { .name = "reset", .gpio = 1 << 4 },
309 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
310 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
311 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
315 .name = "Buffalo WZR-RS-G54HP",
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 },
326 [BUFFALO_UNKNOWN] = {
327 .name = "Buffalo (unknown)",
329 { .name = "reset", .gpio = 1 << 7 },
332 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
335 [BUFFALO_UNKNOWN_4710] = {
336 .name = "Buffalo (unknown, BCM4710)",
338 { .name = "reset", .gpio = 1 << 4 },
341 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
346 .name = "Siemens SE505 V1",
348 /* No usable buttons */
351 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
352 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
356 .name = "Siemens SE505 V2",
358 /* No usable buttons */
361 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
362 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
363 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
368 .name = "U.S. Robotics USR5461",
370 /* No usable buttons */
373 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
374 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
379 .name = "Dell TrueMobile 2300",
381 { .name = "reset", .gpio = 1 << 0 },
384 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
389 .name = "Motorola WE800G",
391 { .name = "reset", .gpio = 1 << 0 },
394 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
395 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
396 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
400 .name = "Motorola WR850G V1",
402 { .name = "reset", .gpio = 1 << 0 },
405 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
406 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
407 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
408 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
409 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
413 .name = "Motorola WR850G V2/V3",
415 { .name = "reset", .gpio = 1 << 5 },
418 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
419 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
420 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
421 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
426 .name = "Belkin (unknown)",
427 /* FIXME: verify & add detection */
429 { .name = "reset", .gpio = 1 << 7 },
432 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
433 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
434 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
439 .name = "Netgear WGT634U",
441 { .name = "reset", .gpio = 1 << 2 },
444 { .name = "power", .gpio = 1 << 3, .polarity = REVERSE },
449 .name = "Trendware TEW411BRP+",
451 { /* No usable buttons */ },
454 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
455 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
456 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
461 static struct platform_t __init *platform_detect(void)
463 char *boardnum, *boardtype, *buf;
465 boardnum = getvar("boardnum");
466 boardtype = getvar("boardtype");
467 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
468 /* CFE based - newer hardware */
469 if (!strcmp(boardnum, "42")) { /* Linksys */
470 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), 1))
471 return &platforms[WRT350N];
473 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
474 return &platforms[WRT54G3G];
476 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
477 return &platforms[WRTSL54GS];
479 /* default to WRT54G */
480 return &platforms[WRT54G];
483 if (!strcmp(boardnum, "45")) { /* ASUS */
484 if (!strcmp(boardtype,"0x042f"))
485 return &platforms[WL500GP];
487 return &platforms[WL500GD];
490 if (!strcmp(boardnum, "10496"))
491 return &platforms[USR5461];
492 } else { /* PMON based - old stuff */
493 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
494 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
495 if (!strncmp(getvar("ModelId"),"WE800G", 6))
496 return &platforms[WE800G];
498 return &platforms[WR850GV1];
500 if (!strncmp(boardtype, "bcm94710dev", 11)) {
501 if (!strcmp(boardnum, "42"))
502 return &platforms[WRT54GV1];
503 if (simple_strtoul(boardnum, NULL, 0) == 2)
504 return &platforms[WAP54GV1];
506 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
507 return &platforms[WL500G];
508 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
509 /* Either WL-300g or WL-HDD, do more extensive checks */
510 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
511 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
512 return &platforms[WLHDD];
513 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
514 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
515 return &platforms[WL300G];
518 /* unknown asus stuff, probably bcm4702 */
519 if (!strncmp(boardnum, "asusX", 5))
520 return &platforms[ASUS_4702];
523 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
524 /* Buffalo hardware, check id for specific hardware matches */
525 if (!strcmp(buf, "29bb0332"))
526 return &platforms[WBR2_G54];
527 if (!strcmp(buf, "29129"))
528 return &platforms[WLA2_G54L];
529 if (!strcmp(buf, "30189"))
530 return &platforms[WHR_HP_G54];
531 if (!strcmp(buf, "30182"))
532 return &platforms[WHR_G54S];
533 if (!strcmp(buf, "290441dd"))
534 return &platforms[WHR2_A54G54];
535 if (!strcmp(buf, "30083"))
536 return &platforms[WZR_RS_G54];
537 if (!strcmp(buf, "30103"))
538 return &platforms[WZR_RS_G54HP];
541 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
542 if (!strncmp(boardtype, "bcm94710ap", 10))
543 return &platforms[BUFFALO_UNKNOWN_4710];
545 return &platforms[BUFFALO_UNKNOWN];
548 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
549 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
551 return &platforms[WR850GV2V3];
554 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
555 return &platforms[TEW411BRPP];
562 static void register_buttons(struct button_t *b)
565 platform.button_mask |= b->gpio;
567 platform.button_mask &= ~gpiomask;
569 gpio_outen(platform.button_mask, 0);
570 gpio_control(platform.button_mask, 0);
571 platform.button_polarity = gpio_in() & platform.button_mask;
572 gpio_intpolarity(platform.button_mask, platform.button_polarity);
573 gpio_intmask(platform.button_mask, platform.button_mask);
575 gpio_set_irqenable(1, button_handler);
578 static void unregister_buttons(struct button_t *b)
580 gpio_intmask(platform.button_mask, 0);
582 gpio_set_irqenable(0, button_handler);
585 static void hotplug_button(struct event_t *event)
587 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
588 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
590 call_usermodehelper (event->argv[0], event->argv, event->envp);
595 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
600 in = gpio_in() & platform.button_mask;
601 gpio_intpolarity(platform.button_mask, in);
602 changed = platform.button_polarity ^ in;
603 platform.button_polarity = in;
605 changed &= ~gpio_outen(0, 0);
607 for (b = platform.buttons; b->name; b++) {
608 struct event_t *event;
610 if (!(b->gpio & changed)) continue;
614 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
616 char *scratch = event->buf;
619 event->argv[i++] = hotplug_path;
620 event->argv[i++] = "button";
624 event->envp[i++] = "HOME=/";
625 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
626 event->envp[i++] = scratch;
627 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
628 event->envp[i++] = scratch;
629 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
630 event->envp[i++] = scratch;
631 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
634 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
635 schedule_work(&event->wq);
643 static void register_leds(struct led_t *l)
645 struct proc_dir_entry *p;
649 leds = proc_mkdir("led", diag);
653 for(; l->name; l++) {
654 if (l->gpio & gpiomask)
657 if (l->gpio & GPIO_TYPE_EXTIF) {
662 val |= (l->polarity == NORMAL)?0:l->gpio;
665 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
666 l->proc.type = PROC_LED;
668 p->data = (void *) &l->proc;
669 p->proc_fops = &diag_proc_fops;
673 gpio_outen(mask, mask);
674 gpio_control(mask, 0);
678 static void unregister_leds(struct led_t *l)
681 remove_proc_entry(l->name, leds);
683 remove_proc_entry("led", diag);
686 static void set_led_extif(struct led_t *led)
688 gpio_set_extif(led->gpio, led->state);
691 static void led_flash(unsigned long dummy) {
696 for (l = platform.leds; l->name; l++) {
698 if (l->gpio & GPIO_TYPE_EXTIF) {
700 l->state = !l->state;
710 u32 val = ~gpio_in();
712 gpio_outen(mask, mask);
713 gpio_control(mask, 0);
716 if (mask || extif_blink) {
717 mod_timer(&led_timer, jiffies + FLASH_TIME);
721 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
724 struct inode *inode = file->f_dentry->d_inode;
725 struct proc_dir_entry *dent = inode->u.generic_ip;
727 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
732 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
735 if (dent->data != NULL) {
736 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
737 switch (handler->type) {
739 struct led_t * led = (struct led_t *) handler->ptr;
741 len = sprintf(page, "f\n");
743 if (led->gpio & GPIO_TYPE_EXTIF) {
744 len = sprintf(page, "%d\n", led->state);
746 u32 in = (gpio_in() & led->gpio ? 1 : 0);
747 u8 p = (led->polarity == NORMAL ? 0 : 1);
748 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
754 len = sprintf(page, "%s\n", platform.name);
757 len = sprintf(page, "0x%04x\n", gpiomask);
764 len = min_t(int, len - *ppos, count);
765 if (copy_to_user(buf, (page + *ppos), len)) {
778 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
781 struct inode *inode = file->f_dentry->d_inode;
782 struct proc_dir_entry *dent = inode->u.generic_ip;
784 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
789 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
792 if (copy_from_user(page, buf, count)) {
798 if (dent->data != NULL) {
799 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
800 switch (handler->type) {
802 struct led_t *led = (struct led_t *) handler->ptr;
803 int p = (led->polarity == NORMAL ? 0 : 1);
805 if (page[0] == 'f') {
810 if (led->gpio & GPIO_TYPE_EXTIF) {
811 led->state = p ^ ((page[0] == '1') ? 1 : 0);
814 gpio_outen(led->gpio, led->gpio);
815 gpio_control(led->gpio, 0);
816 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
822 gpiomask = simple_strtoul(page, NULL, 0);
824 if (platform.buttons) {
825 unregister_buttons(platform.buttons);
826 register_buttons(platform.buttons);
830 unregister_leds(platform.leds);
831 register_leds(platform.leds);
842 static int __init diag_init(void)
844 static struct proc_dir_entry *p;
845 static struct platform_t *detected;
847 detected = platform_detect();
849 printk(MODULE_NAME ": Router model not detected.\n");
852 memcpy(&platform, detected, sizeof(struct platform_t));
854 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
856 if (!(diag = proc_mkdir("diag", NULL))) {
857 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
861 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
862 p->data = (void *) &proc_model;
863 p->proc_fops = &diag_proc_fops;
866 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
867 p->data = (void *) &proc_gpiomask;
868 p->proc_fops = &diag_proc_fops;
871 if (platform.buttons)
872 register_buttons(platform.buttons);
875 register_leds(platform.leds);
880 static void __exit diag_exit(void)
883 del_timer(&led_timer);
885 if (platform.buttons)
886 unregister_buttons(platform.buttons);
889 unregister_leds(platform.leds);
891 remove_proc_entry("model", diag);
892 remove_proc_entry("gpiomask", diag);
893 remove_proc_entry("diag", NULL);
896 module_init(diag_init);
897 module_exit(diag_exit);
899 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
900 MODULE_LICENSE("GPL");