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);
96 static struct platform_t __initdata platforms[] = {
99 .name = "Linksys WAP54G V1",
101 { .name = "reset", .gpio = 1 << 0 },
104 { .name = "diag", .gpio = 1 << 3 },
105 { .name = "wlan", .gpio = 1 << 4 },
109 .name = "Linksys WAP54G V3",
111 /* FIXME: verify this */
112 { .name = "reset", .gpio = 1 << 7 },
113 { .name = "ses", .gpio = 1 << 0 },
117 { .name = "ses", .gpio = 1 << 1 },
121 .name = "Linksys WRT54G V1.x",
123 { .name = "reset", .gpio = 1 << 6 },
126 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
127 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
131 .name = "Linksys WRT54G/GS/GL",
133 { .name = "reset", .gpio = 1 << 6 },
134 { .name = "ses", .gpio = 1 << 4 },
137 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
138 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
139 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
140 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
141 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
145 .name = "Linksys WRTSL54GS",
147 { .name = "reset", .gpio = 1 << 6 },
148 { .name = "ses", .gpio = 1 << 4 },
151 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
152 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
153 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
154 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
158 .name = "Linksys WRT54G3G",
160 { .name = "reset", .gpio = 1 << 6 },
161 { .name = "3g", .gpio = 1 << 4 },
164 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
165 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
166 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
167 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
168 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
173 .name = "ASUS WL-HDD",
175 { .name = "reset", .gpio = 1 << 6 },
178 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
182 .name = "ASUS WL-300g",
184 { .name = "reset", .gpio = 1 << 6 },
187 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
191 .name = "ASUS WL-500g",
193 { .name = "reset", .gpio = 1 << 6 },
196 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
200 .name = "ASUS WL-500g Deluxe",
202 { .name = "reset", .gpio = 1 << 6 },
205 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
209 .name = "ASUS WL-500g Premium",
211 { .name = "reset", .gpio = 1 << 0 },
212 { .name = "ses", .gpio = 1 << 4 },
215 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
219 .name = "ASUS (unknown, BCM4702)",
221 { .name = "reset", .gpio = 1 << 6 },
224 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
229 .name = "Buffalo WHR-G54S",
231 { .name = "reset", .gpio = 1 << 4 },
232 { .name = "bridge", .gpio = 1 << 5 },
233 { .name = "ses", .gpio = 1 << 0 },
236 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
237 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
238 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
239 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
243 .name = "Buffalo WBR2-G54",
246 { .name = "reset", .gpio = 1 << 7 },
249 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
253 .name = "Buffalo WHR-HP-G54",
255 { .name = "reset", .gpio = 1 << 4 },
256 { .name = "bridge", .gpio = 1 << 5 },
257 { .name = "ses", .gpio = 1 << 0 },
260 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
261 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
262 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
266 .name = "Buffalo WHR2-A54G54",
268 { .name = "reset", .gpio = 1 << 4 },
271 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
275 .name = "Buffalo WLA2-G54L",
278 { .name = "reset", .gpio = 1 << 7 },
281 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
285 .name = "Buffalo WZR-RS-G54",
287 { .name = "ses", .gpio = 1 << 0 },
288 { .name = "reset", .gpio = 1 << 4 },
291 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
292 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
293 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
297 .name = "Buffalo WZR-RS-G54HP",
299 { .name = "ses", .gpio = 1 << 0 },
300 { .name = "reset", .gpio = 1 << 4 },
303 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
304 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
305 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
308 [BUFFALO_UNKNOWN] = {
309 .name = "Buffalo (unknown)",
311 { .name = "reset", .gpio = 1 << 7 },
314 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
317 [BUFFALO_UNKNOWN_4710] = {
318 .name = "Buffalo (unknown, BCM4710)",
320 { .name = "reset", .gpio = 1 << 4 },
323 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
328 .name = "Siemens SE505 V1",
330 /* No usable buttons */
333 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
334 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
338 .name = "Siemens SE505 V2",
340 /* No usable buttons */
343 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
344 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
345 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
350 .name = "U.S. Robotics USR5461",
352 /* No usable buttons */
355 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
356 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
361 .name = "Dell TrueMobile 2300",
363 { .name = "reset", .gpio = 1 << 0 },
366 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
371 .name = "Motorola WE800G",
373 { .name = "reset", .gpio = 1 << 0 },
376 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
377 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
378 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
382 .name = "Motorola WR850G V1",
384 { .name = "reset", .gpio = 1 << 0 },
387 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
388 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
389 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
390 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
391 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
395 .name = "Motorola WR850G V2/V3",
397 { .name = "reset", .gpio = 1 << 5 },
400 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
401 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
402 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
403 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
408 .name = "Belkin (unknown)",
409 /* FIXME: verify & add detection */
411 { .name = "reset", .gpio = 1 << 7 },
414 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
415 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
416 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
421 static struct platform_t __init *platform_detect(void)
423 char *boardnum, *boardtype, *buf;
425 boardnum = getvar("boardnum");
426 boardtype = getvar("boardtype");
427 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
428 /* CFE based - newer hardware */
429 if (!strcmp(boardnum, "42")) { /* Linksys */
430 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
431 return &platforms[WRT54G3G];
433 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
434 return &platforms[WRTSL54GS];
436 /* default to WRT54G */
437 return &platforms[WRT54G];
440 if (!strcmp(boardnum, "45")) { /* ASUS */
441 if (!strcmp(boardtype,"0x042f"))
442 return &platforms[WL500GP];
444 return &platforms[WL500GD];
447 if (!strcmp(boardnum, "10496"))
448 return &platforms[USR5461];
449 } else { /* PMON based - old stuff */
450 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
451 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
452 if (!strncmp(getvar("ModelId"),"WE800G", 6))
453 return &platforms[WE800G];
455 return &platforms[WR850GV1];
457 if (!strncmp(boardtype, "bcm94710dev", 11)) {
458 if (!strcmp(boardnum, "42"))
459 return &platforms[WRT54GV1];
460 if (simple_strtoul(boardnum, NULL, 0) == 2)
461 return &platforms[WAP54GV1];
463 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
464 return &platforms[WL500G];
465 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
466 /* Either WL-300g or WL-HDD, do more extensive checks */
467 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
468 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
469 return &platforms[WLHDD];
470 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
471 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
472 return &platforms[WL300G];
475 /* unknown asus stuff, probably bcm4702 */
476 if (!strncmp(boardnum, "asusX", 5))
477 return &platforms[ASUS_4702];
480 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
481 /* Buffalo hardware, check id for specific hardware matches */
482 if (!strcmp(buf, "29bb0332"))
483 return &platforms[WBR2_G54];
484 if (!strcmp(buf, "29129"))
485 return &platforms[WLA2_G54L];
486 if (!strcmp(buf, "30189"))
487 return &platforms[WHR_HP_G54];
488 if (!strcmp(buf, "30182"))
489 return &platforms[WHR_G54S];
490 if (!strcmp(buf, "290441dd"))
491 return &platforms[WHR2_A54G54];
492 if (!strcmp(buf, "30083"))
493 return &platforms[WZR_RS_G54];
494 if (!strcmp(buf, "30103"))
495 return &platforms[WZR_RS_G54HP];
498 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
499 if (!strncmp(boardtype, "bcm94710ap", 10))
500 return &platforms[BUFFALO_UNKNOWN_4710];
502 return &platforms[BUFFALO_UNKNOWN];
506 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
507 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
509 return &platforms[WR850GV2V3];
516 static void register_buttons(struct button_t *b)
519 platform.button_mask |= b->gpio;
521 platform.button_mask &= ~gpiomask;
523 gpio_outen(platform.button_mask, 0);
524 gpio_control(platform.button_mask, 0);
525 platform.button_polarity = gpio_in() & platform.button_mask;
526 gpio_intpolarity(platform.button_mask, platform.button_polarity);
527 gpio_intmask(platform.button_mask, platform.button_mask);
529 gpio_set_irqenable(1, button_handler);
532 static void unregister_buttons(struct button_t *b)
534 gpio_intmask(platform.button_mask, 0);
536 gpio_set_irqenable(0, button_handler);
539 static void hotplug_button(struct event_t *event)
541 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
542 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
544 call_usermodehelper (event->argv[0], event->argv, event->envp);
549 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
554 in = gpio_in() & platform.button_mask;
555 gpio_intpolarity(platform.button_mask, in);
556 changed = platform.button_polarity ^ in;
557 platform.button_polarity = in;
559 changed &= ~gpio_outen(0, 0);
561 for (b = platform.buttons; b->name; b++) {
562 struct event_t *event;
564 if (!(b->gpio & changed)) continue;
568 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
570 char *scratch = event->buf;
573 event->argv[i++] = hotplug_path;
574 event->argv[i++] = "button";
578 event->envp[i++] = "HOME=/";
579 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
580 event->envp[i++] = scratch;
581 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
582 event->envp[i++] = scratch;
583 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
584 event->envp[i++] = scratch;
585 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
588 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
589 schedule_work(&event->wq);
597 static void register_leds(struct led_t *l)
599 struct proc_dir_entry *p;
603 leds = proc_mkdir("led", diag);
607 for(; l->name; l++) {
608 if (l->gpio & gpiomask)
611 if (l->gpio & GPIO_TYPE_EXTIF) {
616 val |= (l->polarity == NORMAL)?0:l->gpio;
619 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
620 l->proc.type = PROC_LED;
622 p->data = (void *) &l->proc;
623 p->proc_fops = &diag_proc_fops;
627 gpio_outen(mask, mask);
628 gpio_control(mask, 0);
632 static void unregister_leds(struct led_t *l)
635 remove_proc_entry(l->name, leds);
637 remove_proc_entry("led", diag);
640 static void set_led_extif(struct led_t *led)
642 gpio_set_extif(led->gpio, led->state);
645 static void led_flash(unsigned long dummy) {
650 for (l = platform.leds; l->name; l++) {
652 if (l->gpio & GPIO_TYPE_EXTIF) {
654 l->state = !l->state;
664 u32 val = ~gpio_in();
666 gpio_outen(mask, mask);
667 gpio_control(mask, 0);
670 if (mask || extif_blink) {
671 mod_timer(&led_timer, jiffies + FLASH_TIME);
675 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
678 struct inode *inode = file->f_dentry->d_inode;
679 struct proc_dir_entry *dent = inode->u.generic_ip;
681 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
686 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
689 if (dent->data != NULL) {
690 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
691 switch (handler->type) {
693 struct led_t * led = (struct led_t *) handler->ptr;
695 len = sprintf(page, "f\n");
697 if (led->gpio & GPIO_TYPE_EXTIF) {
698 len = sprintf(page, "%d\n", led->state);
700 u32 in = (gpio_in() & led->gpio ? 1 : 0);
701 u8 p = (led->polarity == NORMAL ? 0 : 1);
702 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
708 len = sprintf(page, "%s\n", platform.name);
711 len = sprintf(page, "0x%04x\n", gpiomask);
718 len = min_t(int, len - *ppos, count);
719 if (copy_to_user(buf, (page + *ppos), len)) {
732 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
735 struct inode *inode = file->f_dentry->d_inode;
736 struct proc_dir_entry *dent = inode->u.generic_ip;
738 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
743 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
746 if (copy_from_user(page, buf, count)) {
752 if (dent->data != NULL) {
753 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
754 switch (handler->type) {
756 struct led_t *led = (struct led_t *) handler->ptr;
757 int p = (led->polarity == NORMAL ? 0 : 1);
759 if (page[0] == 'f') {
764 if (led->gpio & GPIO_TYPE_EXTIF) {
765 led->state = p ^ ((page[0] == '1') ? 1 : 0);
768 gpio_outen(led->gpio, led->gpio);
769 gpio_control(led->gpio, 0);
770 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
776 gpiomask = simple_strtoul(page, NULL, 0);
778 if (platform.buttons) {
779 unregister_buttons(platform.buttons);
780 register_buttons(platform.buttons);
784 unregister_leds(platform.leds);
785 register_leds(platform.leds);
796 static int __init diag_init(void)
798 static struct proc_dir_entry *p;
799 static struct platform_t *detected;
801 detected = platform_detect();
803 printk(MODULE_NAME ": Router model not detected.\n");
806 memcpy(&platform, detected, sizeof(struct platform_t));
808 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
810 if (!(diag = proc_mkdir("diag", NULL))) {
811 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
815 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
816 p->data = (void *) &proc_model;
817 p->proc_fops = &diag_proc_fops;
820 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
821 p->data = (void *) &proc_gpiomask;
822 p->proc_fops = &diag_proc_fops;
825 if (platform.buttons)
826 register_buttons(platform.buttons);
829 register_leds(platform.leds);
834 static void __exit diag_exit(void)
837 del_timer(&led_timer);
839 if (platform.buttons)
840 unregister_buttons(platform.buttons);
843 unregister_leds(platform.leds);
845 remove_proc_entry("model", diag);
846 remove_proc_entry("gpiomask", diag);
847 remove_proc_entry("diag", NULL);
850 module_init(diag_init);
851 module_exit(diag_exit);
853 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
854 MODULE_LICENSE("GPL");