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);
99 static struct platform_t __initdata platforms[] = {
102 .name = "Linksys WAP54G V1",
104 { .name = "reset", .gpio = 1 << 0 },
107 { .name = "diag", .gpio = 1 << 3 },
108 { .name = "wlan", .gpio = 1 << 4 },
112 .name = "Linksys WAP54G V3",
114 /* FIXME: verify this */
115 { .name = "reset", .gpio = 1 << 7 },
116 { .name = "ses", .gpio = 1 << 0 },
120 { .name = "ses", .gpio = 1 << 1 },
124 .name = "Linksys WRT54G V1.x",
126 { .name = "reset", .gpio = 1 << 6 },
129 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
130 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
134 .name = "Linksys WRT54G/GS/GL",
136 { .name = "reset", .gpio = 1 << 6 },
137 { .name = "ses", .gpio = 1 << 4 },
140 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
141 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
142 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
143 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
144 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
148 .name = "Linksys WRTSL54GS",
150 { .name = "reset", .gpio = 1 << 6 },
151 { .name = "ses", .gpio = 1 << 4 },
154 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
155 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
156 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
157 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
161 .name = "Linksys WRT54G3G",
163 { .name = "reset", .gpio = 1 << 6 },
164 { .name = "3g", .gpio = 1 << 4 },
167 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
168 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
169 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
170 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
171 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
176 .name = "ASUS WL-HDD",
178 { .name = "reset", .gpio = 1 << 6 },
181 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
185 .name = "ASUS WL-300g",
187 { .name = "reset", .gpio = 1 << 6 },
190 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
194 .name = "ASUS WL-500g",
196 { .name = "reset", .gpio = 1 << 6 },
199 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
203 .name = "ASUS WL-500g Deluxe",
205 { .name = "reset", .gpio = 1 << 6 },
208 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
212 .name = "ASUS WL-500g Premium",
214 { .name = "reset", .gpio = 1 << 0 },
215 { .name = "ses", .gpio = 1 << 4 },
218 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
222 .name = "ASUS (unknown, BCM4702)",
224 { .name = "reset", .gpio = 1 << 6 },
227 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
232 .name = "Buffalo WHR-G54S",
234 { .name = "reset", .gpio = 1 << 4 },
235 { .name = "bridge", .gpio = 1 << 5 },
236 { .name = "ses", .gpio = 1 << 0 },
239 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
240 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
241 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
242 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
246 .name = "Buffalo WBR2-G54",
249 { .name = "reset", .gpio = 1 << 7 },
252 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
256 .name = "Buffalo WHR-HP-G54",
258 { .name = "reset", .gpio = 1 << 4 },
259 { .name = "bridge", .gpio = 1 << 5 },
260 { .name = "ses", .gpio = 1 << 0 },
263 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
264 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
265 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
269 .name = "Buffalo WHR2-A54G54",
271 { .name = "reset", .gpio = 1 << 4 },
274 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
278 .name = "Buffalo WLA2-G54L",
281 { .name = "reset", .gpio = 1 << 7 },
284 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
288 .name = "Buffalo WZR-RS-G54",
290 { .name = "ses", .gpio = 1 << 0 },
291 { .name = "reset", .gpio = 1 << 4 },
294 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
295 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
296 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
300 .name = "Buffalo WZR-RS-G54HP",
302 { .name = "ses", .gpio = 1 << 0 },
303 { .name = "reset", .gpio = 1 << 4 },
306 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
307 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
308 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
311 [BUFFALO_UNKNOWN] = {
312 .name = "Buffalo (unknown)",
314 { .name = "reset", .gpio = 1 << 7 },
317 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
320 [BUFFALO_UNKNOWN_4710] = {
321 .name = "Buffalo (unknown, BCM4710)",
323 { .name = "reset", .gpio = 1 << 4 },
326 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
331 .name = "Siemens SE505 V1",
333 /* No usable buttons */
336 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
337 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
341 .name = "Siemens SE505 V2",
343 /* No usable buttons */
346 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
347 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
348 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
353 .name = "U.S. Robotics USR5461",
355 /* No usable buttons */
358 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
359 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
364 .name = "Dell TrueMobile 2300",
366 { .name = "reset", .gpio = 1 << 0 },
369 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
374 .name = "Motorola WE800G",
376 { .name = "reset", .gpio = 1 << 0 },
379 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
380 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
381 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
385 .name = "Motorola WR850G V1",
387 { .name = "reset", .gpio = 1 << 0 },
390 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
391 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
392 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
393 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
394 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
398 .name = "Motorola WR850G V2/V3",
400 { .name = "reset", .gpio = 1 << 5 },
403 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
404 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
405 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
406 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
411 .name = "Belkin (unknown)",
412 /* FIXME: verify & add detection */
414 { .name = "reset", .gpio = 1 << 7 },
417 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
418 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
419 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
424 .name = "Trendware TEW411BRP+",
426 { /* No usable buttons */ },
429 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
430 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
431 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
436 static struct platform_t __init *platform_detect(void)
438 char *boardnum, *boardtype, *buf;
440 boardnum = getvar("boardnum");
441 boardtype = getvar("boardtype");
442 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
443 /* CFE based - newer hardware */
444 if (!strcmp(boardnum, "42")) { /* Linksys */
445 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
446 return &platforms[WRT54G3G];
448 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
449 return &platforms[WRTSL54GS];
451 /* default to WRT54G */
452 return &platforms[WRT54G];
455 if (!strcmp(boardnum, "45")) { /* ASUS */
456 if (!strcmp(boardtype,"0x042f"))
457 return &platforms[WL500GP];
459 return &platforms[WL500GD];
462 if (!strcmp(boardnum, "10496"))
463 return &platforms[USR5461];
464 } else { /* PMON based - old stuff */
465 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
466 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
467 if (!strncmp(getvar("ModelId"),"WE800G", 6))
468 return &platforms[WE800G];
470 return &platforms[WR850GV1];
472 if (!strncmp(boardtype, "bcm94710dev", 11)) {
473 if (!strcmp(boardnum, "42"))
474 return &platforms[WRT54GV1];
475 if (simple_strtoul(boardnum, NULL, 0) == 2)
476 return &platforms[WAP54GV1];
478 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
479 return &platforms[WL500G];
480 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
481 /* Either WL-300g or WL-HDD, do more extensive checks */
482 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
483 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
484 return &platforms[WLHDD];
485 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
486 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
487 return &platforms[WL300G];
490 /* unknown asus stuff, probably bcm4702 */
491 if (!strncmp(boardnum, "asusX", 5))
492 return &platforms[ASUS_4702];
495 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
496 /* Buffalo hardware, check id for specific hardware matches */
497 if (!strcmp(buf, "29bb0332"))
498 return &platforms[WBR2_G54];
499 if (!strcmp(buf, "29129"))
500 return &platforms[WLA2_G54L];
501 if (!strcmp(buf, "30189"))
502 return &platforms[WHR_HP_G54];
503 if (!strcmp(buf, "30182"))
504 return &platforms[WHR_G54S];
505 if (!strcmp(buf, "290441dd"))
506 return &platforms[WHR2_A54G54];
507 if (!strcmp(buf, "30083"))
508 return &platforms[WZR_RS_G54];
509 if (!strcmp(buf, "30103"))
510 return &platforms[WZR_RS_G54HP];
513 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
514 if (!strncmp(boardtype, "bcm94710ap", 10))
515 return &platforms[BUFFALO_UNKNOWN_4710];
517 return &platforms[BUFFALO_UNKNOWN];
521 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
522 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
524 return &platforms[WR850GV2V3];
527 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
528 return &platforms[TEW411BRPP];
535 static void register_buttons(struct button_t *b)
538 platform.button_mask |= b->gpio;
540 platform.button_mask &= ~gpiomask;
542 gpio_outen(platform.button_mask, 0);
543 gpio_control(platform.button_mask, 0);
544 platform.button_polarity = gpio_in() & platform.button_mask;
545 gpio_intpolarity(platform.button_mask, platform.button_polarity);
546 gpio_intmask(platform.button_mask, platform.button_mask);
548 gpio_set_irqenable(1, button_handler);
551 static void unregister_buttons(struct button_t *b)
553 gpio_intmask(platform.button_mask, 0);
555 gpio_set_irqenable(0, button_handler);
558 static void hotplug_button(struct event_t *event)
560 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
561 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
563 call_usermodehelper (event->argv[0], event->argv, event->envp);
568 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
573 in = gpio_in() & platform.button_mask;
574 gpio_intpolarity(platform.button_mask, in);
575 changed = platform.button_polarity ^ in;
576 platform.button_polarity = in;
578 changed &= ~gpio_outen(0, 0);
580 for (b = platform.buttons; b->name; b++) {
581 struct event_t *event;
583 if (!(b->gpio & changed)) continue;
587 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
589 char *scratch = event->buf;
592 event->argv[i++] = hotplug_path;
593 event->argv[i++] = "button";
597 event->envp[i++] = "HOME=/";
598 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
599 event->envp[i++] = scratch;
600 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
601 event->envp[i++] = scratch;
602 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
603 event->envp[i++] = scratch;
604 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
607 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
608 schedule_work(&event->wq);
616 static void register_leds(struct led_t *l)
618 struct proc_dir_entry *p;
622 leds = proc_mkdir("led", diag);
626 for(; l->name; l++) {
627 if (l->gpio & gpiomask)
630 if (l->gpio & GPIO_TYPE_EXTIF) {
635 val |= (l->polarity == NORMAL)?0:l->gpio;
638 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
639 l->proc.type = PROC_LED;
641 p->data = (void *) &l->proc;
642 p->proc_fops = &diag_proc_fops;
646 gpio_outen(mask, mask);
647 gpio_control(mask, 0);
651 static void unregister_leds(struct led_t *l)
654 remove_proc_entry(l->name, leds);
656 remove_proc_entry("led", diag);
659 static void set_led_extif(struct led_t *led)
661 gpio_set_extif(led->gpio, led->state);
664 static void led_flash(unsigned long dummy) {
669 for (l = platform.leds; l->name; l++) {
671 if (l->gpio & GPIO_TYPE_EXTIF) {
673 l->state = !l->state;
683 u32 val = ~gpio_in();
685 gpio_outen(mask, mask);
686 gpio_control(mask, 0);
689 if (mask || extif_blink) {
690 mod_timer(&led_timer, jiffies + FLASH_TIME);
694 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
697 struct inode *inode = file->f_dentry->d_inode;
698 struct proc_dir_entry *dent = inode->u.generic_ip;
700 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
705 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
708 if (dent->data != NULL) {
709 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
710 switch (handler->type) {
712 struct led_t * led = (struct led_t *) handler->ptr;
714 len = sprintf(page, "f\n");
716 if (led->gpio & GPIO_TYPE_EXTIF) {
717 len = sprintf(page, "%d\n", led->state);
719 u32 in = (gpio_in() & led->gpio ? 1 : 0);
720 u8 p = (led->polarity == NORMAL ? 0 : 1);
721 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
727 len = sprintf(page, "%s\n", platform.name);
730 len = sprintf(page, "0x%04x\n", gpiomask);
737 len = min_t(int, len - *ppos, count);
738 if (copy_to_user(buf, (page + *ppos), len)) {
751 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
754 struct inode *inode = file->f_dentry->d_inode;
755 struct proc_dir_entry *dent = inode->u.generic_ip;
757 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
762 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
765 if (copy_from_user(page, buf, count)) {
771 if (dent->data != NULL) {
772 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
773 switch (handler->type) {
775 struct led_t *led = (struct led_t *) handler->ptr;
776 int p = (led->polarity == NORMAL ? 0 : 1);
778 if (page[0] == 'f') {
783 if (led->gpio & GPIO_TYPE_EXTIF) {
784 led->state = p ^ ((page[0] == '1') ? 1 : 0);
787 gpio_outen(led->gpio, led->gpio);
788 gpio_control(led->gpio, 0);
789 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
795 gpiomask = simple_strtoul(page, NULL, 0);
797 if (platform.buttons) {
798 unregister_buttons(platform.buttons);
799 register_buttons(platform.buttons);
803 unregister_leds(platform.leds);
804 register_leds(platform.leds);
815 static int __init diag_init(void)
817 static struct proc_dir_entry *p;
818 static struct platform_t *detected;
820 detected = platform_detect();
822 printk(MODULE_NAME ": Router model not detected.\n");
825 memcpy(&platform, detected, sizeof(struct platform_t));
827 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
829 if (!(diag = proc_mkdir("diag", NULL))) {
830 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
834 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
835 p->data = (void *) &proc_model;
836 p->proc_fops = &diag_proc_fops;
839 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
840 p->data = (void *) &proc_gpiomask;
841 p->proc_fops = &diag_proc_fops;
844 if (platform.buttons)
845 register_buttons(platform.buttons);
848 register_leds(platform.leds);
853 static void __exit diag_exit(void)
856 del_timer(&led_timer);
858 if (platform.buttons)
859 unregister_buttons(platform.buttons);
862 unregister_leds(platform.leds);
864 remove_proc_entry("model", diag);
865 remove_proc_entry("gpiomask", diag);
866 remove_proc_entry("diag", NULL);
869 module_init(diag_init);
870 module_exit(diag_exit);
872 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
873 MODULE_LICENSE("GPL");