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);
94 static struct platform_t __initdata platforms[] = {
97 .name = "Linksys WAP54G V1",
99 { .name = "reset", .gpio = 1 << 0 },
102 { .name = "diag", .gpio = 1 << 3 },
103 { .name = "wlan", .gpio = 1 << 4 },
107 .name = "Linksys WAP54G V3",
109 /* FIXME: verify this */
110 { .name = "reset", .gpio = 1 << 7 },
111 { .name = "ses", .gpio = 1 << 0 },
115 { .name = "ses", .gpio = 1 << 1 },
119 .name = "Linksys WRT54G V1.x",
121 { .name = "reset", .gpio = 1 << 6 },
124 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
125 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
129 .name = "Linksys WRT54G/GS/GL",
131 { .name = "reset", .gpio = 1 << 6 },
132 { .name = "ses", .gpio = 1 << 4 },
135 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
136 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
137 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
138 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
139 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
143 .name = "Linksys WRTSL54GS",
145 { .name = "reset", .gpio = 1 << 6 },
146 { .name = "ses", .gpio = 1 << 4 },
149 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
150 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
151 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
152 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
156 .name = "Linksys WRT54G3G",
158 { .name = "reset", .gpio = 1 << 6 },
159 { .name = "3g", .gpio = 1 << 4 },
162 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
163 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
164 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
165 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
166 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
171 .name = "ASUS WL-HDD",
173 { .name = "reset", .gpio = 1 << 6 },
176 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
180 .name = "ASUS WL-300g",
182 { .name = "reset", .gpio = 1 << 6 },
185 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
189 .name = "ASUS WL-500g",
191 { .name = "reset", .gpio = 1 << 6 },
194 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
198 .name = "ASUS WL-500g Deluxe",
200 { .name = "reset", .gpio = 1 << 6 },
203 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
207 .name = "ASUS WL-500g Premium",
209 { .name = "reset", .gpio = 1 << 0 },
210 { .name = "ses", .gpio = 1 << 4 },
213 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
217 .name = "ASUS (unknown, BCM4702)",
219 { .name = "reset", .gpio = 1 << 6 },
222 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
227 .name = "Buffalo WHR-G54S",
229 { .name = "reset", .gpio = 1 << 4 },
230 { .name = "bridge", .gpio = 1 << 5 },
231 { .name = "ses", .gpio = 1 << 0 },
234 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
235 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
236 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
237 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
241 .name = "Buffalo WBR2-G54",
244 { .name = "reset", .gpio = 1 << 7 },
247 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
251 .name = "Buffalo WHR-HP-G54",
253 { .name = "reset", .gpio = 1 << 4 },
254 { .name = "bridge", .gpio = 1 << 5 },
255 { .name = "ses", .gpio = 1 << 0 },
258 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
259 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
260 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
264 .name = "Buffalo WHR2-A54G54",
266 { .name = "reset", .gpio = 1 << 4 },
269 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
273 .name = "Buffalo WLA2-G54L",
276 { .name = "reset", .gpio = 1 << 7 },
279 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
282 [BUFFALO_UNKNOWN] = {
283 .name = "Buffalo (unknown)",
285 { .name = "reset", .gpio = 1 << 7 },
288 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
291 [BUFFALO_UNKNOWN_4710] = {
292 .name = "Buffalo (unknown, BCM4710)",
294 { .name = "reset", .gpio = 1 << 4 },
297 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
302 .name = "Siemens SE505 V1",
304 /* No usable buttons */
307 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
308 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
312 .name = "Siemens SE505 V2",
314 /* No usable buttons */
317 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
318 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
319 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
324 .name = "U.S. Robotics USR5461",
326 /* No usable buttons */
329 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
330 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
335 .name = "Dell TrueMobile 2300",
337 { .name = "reset", .gpio = 1 << 0 },
340 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
345 .name = "Motorola WE800G",
347 { .name = "reset", .gpio = 1 << 0 },
350 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
351 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
352 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
356 .name = "Motorola WR850G V1",
358 { .name = "reset", .gpio = 1 << 0 },
361 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
362 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
363 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
364 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
365 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
369 .name = "Motorola WR850G V2/V3",
371 { .name = "reset", .gpio = 1 << 5 },
374 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
375 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
376 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
377 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
382 .name = "Belkin (unknown)",
383 /* FIXME: verify & add detection */
385 { .name = "reset", .gpio = 1 << 7 },
388 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
389 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
390 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
395 static struct platform_t __init *platform_detect(void)
397 char *boardnum, *boardtype, *buf;
399 boardnum = getvar("boardnum");
400 boardtype = getvar("boardtype");
401 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
402 /* CFE based - newer hardware */
403 if (!strcmp(boardnum, "42")) { /* Linksys */
404 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
405 return &platforms[WRT54G3G];
407 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
408 return &platforms[WRTSL54GS];
410 /* default to WRT54G */
411 return &platforms[WRT54G];
414 if (!strcmp(boardnum, "45")) { /* ASUS */
415 if (!strcmp(boardtype,"0x042f"))
416 return &platforms[WL500GP];
418 return &platforms[WL500GD];
421 if (!strcmp(boardnum, "10496"))
422 return &platforms[USR5461];
423 } else { /* PMON based - old stuff */
424 if (!strncmp(boardtype, "bcm94710dev", 11)) {
425 if (!strcmp(boardnum, "42"))
426 return &platforms[WRT54GV1];
427 if (simple_strtoul(boardnum, NULL, 0) == 2)
428 return &platforms[WAP54GV1];
430 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
431 return &platforms[WL500G];
432 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
433 /* Either WL-300g or WL-HDD, do more extensive checks */
434 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
435 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
436 return &platforms[WLHDD];
437 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
438 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
439 return &platforms[WL300G];
442 /* unknown asus stuff, probably bcm4702 */
443 if (!strncmp(boardnum, "asusX", 5))
444 return &platforms[ASUS_4702];
446 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
447 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
448 if (!strncmp(getvar("ModelId"),"WE800G", 6))
449 return &platforms[WE800G];
451 return &platforms[WR850GV1];
455 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
456 /* Buffalo hardware, check id for specific hardware matches */
457 if (!strcmp(buf, "29bb0332"))
458 return &platforms[WBR2_G54];
459 if (!strcmp(buf, "29129"))
460 return &platforms[WLA2_G54L];
461 if (!strcmp(buf, "30189"))
462 return &platforms[WHR_HP_G54];
463 if (!strcmp(buf, "30182"))
464 return &platforms[WHR_G54S];
465 if (!strcmp(buf, "290441dd"))
466 return &platforms[WHR2_A54G54];
469 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
470 if (!strncmp(boardtype, "bcm94710ap", 10))
471 return &platforms[BUFFALO_UNKNOWN_4710];
473 return &platforms[BUFFALO_UNKNOWN];
477 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
478 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
480 return &platforms[WR850GV2V3];
487 static void register_buttons(struct button_t *b)
490 platform.button_mask |= b->gpio;
492 platform.button_mask &= ~gpiomask;
494 gpio_outen(platform.button_mask, 0);
495 gpio_control(platform.button_mask, 0);
496 platform.button_polarity = gpio_in() & platform.button_mask;
497 gpio_intpolarity(platform.button_mask, platform.button_polarity);
498 gpio_intmask(platform.button_mask, platform.button_mask);
500 gpio_set_irqenable(1, button_handler);
503 static void unregister_buttons(struct button_t *b)
505 gpio_intmask(platform.button_mask, 0);
507 gpio_set_irqenable(0, button_handler);
510 static void hotplug_button(struct event_t *event)
512 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
513 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
515 call_usermodehelper (event->argv[0], event->argv, event->envp);
520 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
525 in = gpio_in() & platform.button_mask;
526 gpio_intpolarity(platform.button_mask, in);
527 changed = platform.button_polarity ^ in;
528 platform.button_polarity = in;
530 for (b = platform.buttons; b->name; b++) {
531 struct event_t *event;
533 if (!(b->gpio & changed)) continue;
537 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
539 char *scratch = event->buf;
542 event->argv[i++] = hotplug_path;
543 event->argv[i++] = "button";
547 event->envp[i++] = "HOME=/";
548 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
549 event->envp[i++] = scratch;
550 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
551 event->envp[i++] = scratch;
552 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
553 event->envp[i++] = scratch;
554 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
557 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
558 schedule_work(&event->wq);
566 static void register_leds(struct led_t *l)
568 struct proc_dir_entry *p;
572 leds = proc_mkdir("led", diag);
576 for(; l->name; l++) {
577 if (l->gpio & gpiomask)
580 if (l->gpio & GPIO_TYPE_EXTIF) {
585 val |= (l->polarity == NORMAL)?0:l->gpio;
588 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
589 l->proc.type = PROC_LED;
591 p->data = (void *) &l->proc;
592 p->proc_fops = &diag_proc_fops;
596 gpio_outen(mask, mask);
597 gpio_control(mask, 0);
601 static void unregister_leds(struct led_t *l)
604 remove_proc_entry(l->name, leds);
606 remove_proc_entry("led", diag);
609 static void set_led_extif(struct led_t *led)
611 gpio_set_extif(led->gpio, led->state);
614 static void led_flash(unsigned long dummy) {
619 for (l = platform.leds; l->name; l++) {
621 if (l->gpio & GPIO_TYPE_EXTIF) {
623 l->state = !l->state;
633 u32 val = ~gpio_in();
635 gpio_outen(mask, mask);
636 gpio_control(mask, 0);
639 if (mask || extif_blink) {
640 mod_timer(&led_timer, jiffies + FLASH_TIME);
644 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
647 struct inode *inode = file->f_dentry->d_inode;
648 struct proc_dir_entry *dent = inode->u.generic_ip;
650 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
655 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
658 if (dent->data != NULL) {
659 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
660 switch (handler->type) {
662 struct led_t * led = (struct led_t *) handler->ptr;
664 len = sprintf(page, "f\n");
666 if (led->gpio & GPIO_TYPE_EXTIF) {
667 len = sprintf(page, "%d\n", led->state);
669 u32 in = (gpio_in() & led->gpio ? 1 : 0);
670 u8 p = (led->polarity == NORMAL ? 0 : 1);
671 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
677 len = sprintf(page, "%s\n", platform.name);
680 len = sprintf(page, "0x%04x\n", gpiomask);
687 len = min_t(int, len - *ppos, count);
688 if (copy_to_user(buf, (page + *ppos), len)) {
701 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
704 struct inode *inode = file->f_dentry->d_inode;
705 struct proc_dir_entry *dent = inode->u.generic_ip;
707 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
712 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
715 if (copy_from_user(page, buf, count)) {
721 if (dent->data != NULL) {
722 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
723 switch (handler->type) {
725 struct led_t *led = (struct led_t *) handler->ptr;
726 int p = (led->polarity == NORMAL ? 0 : 1);
728 if (page[0] == 'f') {
733 if (led->gpio & GPIO_TYPE_EXTIF) {
734 led->state = p ^ ((page[0] == '1') ? 1 : 0);
737 gpio_outen(led->gpio, led->gpio);
738 gpio_control(led->gpio, 0);
739 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
745 gpiomask = simple_strtoul(page, NULL, 0);
747 if (platform.buttons) {
748 unregister_buttons(platform.buttons);
749 register_buttons(platform.buttons);
753 unregister_leds(platform.leds);
754 register_leds(platform.leds);
765 static int __init diag_init(void)
767 static struct proc_dir_entry *p;
768 static struct platform_t *detected;
770 detected = platform_detect();
772 printk(MODULE_NAME ": Router model not detected.\n");
775 memcpy(&platform, detected, sizeof(struct platform_t));
777 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
779 if (!(diag = proc_mkdir("diag", NULL))) {
780 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
784 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
785 p->data = (void *) &proc_model;
786 p->proc_fops = &diag_proc_fops;
789 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
790 p->data = (void *) &proc_gpiomask;
791 p->proc_fops = &diag_proc_fops;
794 if (platform.buttons)
795 register_buttons(platform.buttons);
798 register_leds(platform.leds);
803 static void __exit diag_exit(void)
806 del_timer(&led_timer);
808 if (platform.buttons)
809 unregister_buttons(platform.buttons);
812 unregister_leds(platform.leds);
814 remove_proc_entry("model", diag);
815 remove_proc_entry("gpiomask", diag);
816 remove_proc_entry("diag", NULL);
819 module_init(diag_init);
820 module_exit(diag_exit);
822 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
823 MODULE_LICENSE("GPL");