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 << 0, .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 ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
425 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
426 if (!strncmp(getvar("ModelId"),"WE800G", 6))
427 return &platforms[WE800G];
429 return &platforms[WR850GV1];
431 if (!strncmp(boardtype, "bcm94710dev", 11)) {
432 if (!strcmp(boardnum, "42"))
433 return &platforms[WRT54GV1];
434 if (simple_strtoul(boardnum, NULL, 0) == 2)
435 return &platforms[WAP54GV1];
437 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
438 return &platforms[WL500G];
439 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
440 /* Either WL-300g or WL-HDD, do more extensive checks */
441 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
442 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
443 return &platforms[WLHDD];
444 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
445 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
446 return &platforms[WL300G];
449 /* unknown asus stuff, probably bcm4702 */
450 if (!strncmp(boardnum, "asusX", 5))
451 return &platforms[ASUS_4702];
454 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
455 /* Buffalo hardware, check id for specific hardware matches */
456 if (!strcmp(buf, "29bb0332"))
457 return &platforms[WBR2_G54];
458 if (!strcmp(buf, "29129"))
459 return &platforms[WLA2_G54L];
460 if (!strcmp(buf, "30189"))
461 return &platforms[WHR_HP_G54];
462 if (!strcmp(buf, "30182"))
463 return &platforms[WHR_G54S];
464 if (!strcmp(buf, "290441dd"))
465 return &platforms[WHR2_A54G54];
468 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
469 if (!strncmp(boardtype, "bcm94710ap", 10))
470 return &platforms[BUFFALO_UNKNOWN_4710];
472 return &platforms[BUFFALO_UNKNOWN];
476 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
477 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
479 return &platforms[WR850GV2V3];
486 static void register_buttons(struct button_t *b)
489 platform.button_mask |= b->gpio;
491 platform.button_mask &= ~gpiomask;
493 gpio_outen(platform.button_mask, 0);
494 gpio_control(platform.button_mask, 0);
495 platform.button_polarity = gpio_in() & platform.button_mask;
496 gpio_intpolarity(platform.button_mask, platform.button_polarity);
497 gpio_intmask(platform.button_mask, platform.button_mask);
499 gpio_set_irqenable(1, button_handler);
502 static void unregister_buttons(struct button_t *b)
504 gpio_intmask(platform.button_mask, 0);
506 gpio_set_irqenable(0, button_handler);
509 static void hotplug_button(struct event_t *event)
511 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
512 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
514 call_usermodehelper (event->argv[0], event->argv, event->envp);
519 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
524 in = gpio_in() & platform.button_mask;
525 gpio_intpolarity(platform.button_mask, in);
526 changed = platform.button_polarity ^ in;
527 platform.button_polarity = in;
529 for (b = platform.buttons; b->name; b++) {
530 struct event_t *event;
532 if (!(b->gpio & changed)) continue;
536 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
538 char *scratch = event->buf;
541 event->argv[i++] = hotplug_path;
542 event->argv[i++] = "button";
546 event->envp[i++] = "HOME=/";
547 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
548 event->envp[i++] = scratch;
549 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
550 event->envp[i++] = scratch;
551 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
552 event->envp[i++] = scratch;
553 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
556 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
557 schedule_work(&event->wq);
565 static void register_leds(struct led_t *l)
567 struct proc_dir_entry *p;
571 leds = proc_mkdir("led", diag);
575 for(; l->name; l++) {
576 if (l->gpio & gpiomask)
579 if (l->gpio & GPIO_TYPE_EXTIF) {
584 val |= (l->polarity == NORMAL)?0:l->gpio;
587 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
588 l->proc.type = PROC_LED;
590 p->data = (void *) &l->proc;
591 p->proc_fops = &diag_proc_fops;
595 gpio_outen(mask, mask);
596 gpio_control(mask, 0);
600 static void unregister_leds(struct led_t *l)
603 remove_proc_entry(l->name, leds);
605 remove_proc_entry("led", diag);
608 static void set_led_extif(struct led_t *led)
610 gpio_set_extif(led->gpio, led->state);
613 static void led_flash(unsigned long dummy) {
618 for (l = platform.leds; l->name; l++) {
620 if (l->gpio & GPIO_TYPE_EXTIF) {
622 l->state = !l->state;
632 u32 val = ~gpio_in();
634 gpio_outen(mask, mask);
635 gpio_control(mask, 0);
638 if (mask || extif_blink) {
639 mod_timer(&led_timer, jiffies + FLASH_TIME);
643 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
646 struct inode *inode = file->f_dentry->d_inode;
647 struct proc_dir_entry *dent = inode->u.generic_ip;
649 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
654 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
657 if (dent->data != NULL) {
658 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
659 switch (handler->type) {
661 struct led_t * led = (struct led_t *) handler->ptr;
663 len = sprintf(page, "f\n");
665 if (led->gpio & GPIO_TYPE_EXTIF) {
666 len = sprintf(page, "%d\n", led->state);
668 u32 in = (gpio_in() & led->gpio ? 1 : 0);
669 u8 p = (led->polarity == NORMAL ? 0 : 1);
670 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
676 len = sprintf(page, "%s\n", platform.name);
679 len = sprintf(page, "0x%04x\n", gpiomask);
686 len = min_t(int, len - *ppos, count);
687 if (copy_to_user(buf, (page + *ppos), len)) {
700 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
703 struct inode *inode = file->f_dentry->d_inode;
704 struct proc_dir_entry *dent = inode->u.generic_ip;
706 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
711 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
714 if (copy_from_user(page, buf, count)) {
720 if (dent->data != NULL) {
721 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
722 switch (handler->type) {
724 struct led_t *led = (struct led_t *) handler->ptr;
725 int p = (led->polarity == NORMAL ? 0 : 1);
727 if (page[0] == 'f') {
732 if (led->gpio & GPIO_TYPE_EXTIF) {
733 led->state = p ^ ((page[0] == '1') ? 1 : 0);
736 gpio_outen(led->gpio, led->gpio);
737 gpio_control(led->gpio, 0);
738 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
744 gpiomask = simple_strtoul(page, NULL, 0);
746 if (platform.buttons) {
747 unregister_buttons(platform.buttons);
748 register_buttons(platform.buttons);
752 unregister_leds(platform.leds);
753 register_leds(platform.leds);
764 static int __init diag_init(void)
766 static struct proc_dir_entry *p;
767 static struct platform_t *detected;
769 detected = platform_detect();
771 printk(MODULE_NAME ": Router model not detected.\n");
774 memcpy(&platform, detected, sizeof(struct platform_t));
776 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
778 if (!(diag = proc_mkdir("diag", NULL))) {
779 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
783 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
784 p->data = (void *) &proc_model;
785 p->proc_fops = &diag_proc_fops;
788 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
789 p->data = (void *) &proc_gpiomask;
790 p->proc_fops = &diag_proc_fops;
793 if (platform.buttons)
794 register_buttons(platform.buttons);
797 register_leds(platform.leds);
802 static void __exit diag_exit(void)
805 del_timer(&led_timer);
807 if (platform.buttons)
808 unregister_buttons(platform.buttons);
811 unregister_leds(platform.leds);
813 remove_proc_entry("model", diag);
814 remove_proc_entry("gpiomask", diag);
815 remove_proc_entry("diag", NULL);
818 module_init(diag_init);
819 module_exit(diag_exit);
821 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
822 MODULE_LICENSE("GPL");