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);
95 static struct platform_t __initdata platforms[] = {
98 .name = "Linksys WAP54G V1",
100 { .name = "reset", .gpio = 1 << 0 },
103 { .name = "diag", .gpio = 1 << 3 },
104 { .name = "wlan", .gpio = 1 << 4 },
108 .name = "Linksys WAP54G V3",
110 /* FIXME: verify this */
111 { .name = "reset", .gpio = 1 << 7 },
112 { .name = "ses", .gpio = 1 << 0 },
116 { .name = "ses", .gpio = 1 << 1 },
120 .name = "Linksys WRT54G V1.x",
122 { .name = "reset", .gpio = 1 << 6 },
125 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
126 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
130 .name = "Linksys WRT54G/GS/GL",
132 { .name = "reset", .gpio = 1 << 6 },
133 { .name = "ses", .gpio = 1 << 4 },
136 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
137 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
138 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
139 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
140 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
144 .name = "Linksys WRTSL54GS",
146 { .name = "reset", .gpio = 1 << 6 },
147 { .name = "ses", .gpio = 1 << 4 },
150 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
151 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
152 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
153 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
157 .name = "Linksys WRT54G3G",
159 { .name = "reset", .gpio = 1 << 6 },
160 { .name = "3g", .gpio = 1 << 4 },
163 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
164 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
165 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
166 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
167 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
172 .name = "ASUS WL-HDD",
174 { .name = "reset", .gpio = 1 << 6 },
177 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
181 .name = "ASUS WL-300g",
183 { .name = "reset", .gpio = 1 << 6 },
186 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
190 .name = "ASUS WL-500g",
192 { .name = "reset", .gpio = 1 << 6 },
195 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
199 .name = "ASUS WL-500g Deluxe",
201 { .name = "reset", .gpio = 1 << 6 },
204 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
208 .name = "ASUS WL-500g Premium",
210 { .name = "reset", .gpio = 1 << 0 },
211 { .name = "ses", .gpio = 1 << 4 },
214 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
218 .name = "ASUS (unknown, BCM4702)",
220 { .name = "reset", .gpio = 1 << 6 },
223 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
228 .name = "Buffalo WHR-G54S",
230 { .name = "reset", .gpio = 1 << 4 },
231 { .name = "bridge", .gpio = 1 << 5 },
232 { .name = "ses", .gpio = 1 << 0 },
235 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
236 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
237 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
238 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
242 .name = "Buffalo WBR2-G54",
245 { .name = "reset", .gpio = 1 << 7 },
248 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
252 .name = "Buffalo WHR-HP-G54",
254 { .name = "reset", .gpio = 1 << 4 },
255 { .name = "bridge", .gpio = 1 << 5 },
256 { .name = "ses", .gpio = 1 << 0 },
259 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
260 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
261 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
265 .name = "Buffalo WHR2-A54G54",
267 { .name = "reset", .gpio = 1 << 4 },
270 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
274 .name = "Buffalo WLA2-G54L",
277 { .name = "reset", .gpio = 1 << 7 },
280 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
284 .name = "Buffalo WZR-RS-G54",
286 { .name = "ses", .gpio = 1 << 0 },
287 { .name = "reset", .gpio = 1 << 7 },
290 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
293 [BUFFALO_UNKNOWN] = {
294 .name = "Buffalo (unknown)",
296 { .name = "reset", .gpio = 1 << 7 },
299 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
302 [BUFFALO_UNKNOWN_4710] = {
303 .name = "Buffalo (unknown, BCM4710)",
305 { .name = "reset", .gpio = 1 << 4 },
308 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
313 .name = "Siemens SE505 V1",
315 /* No usable buttons */
318 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
319 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
323 .name = "Siemens SE505 V2",
325 /* No usable buttons */
328 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
329 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
330 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
335 .name = "U.S. Robotics USR5461",
337 /* No usable buttons */
340 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
341 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
346 .name = "Dell TrueMobile 2300",
348 { .name = "reset", .gpio = 1 << 0 },
351 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
356 .name = "Motorola WE800G",
358 { .name = "reset", .gpio = 1 << 0 },
361 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
362 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
363 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
367 .name = "Motorola WR850G V1",
369 { .name = "reset", .gpio = 1 << 0 },
372 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
373 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
374 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
375 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
376 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
380 .name = "Motorola WR850G V2/V3",
382 { .name = "reset", .gpio = 1 << 5 },
385 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
386 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
387 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
388 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
393 .name = "Belkin (unknown)",
394 /* FIXME: verify & add detection */
396 { .name = "reset", .gpio = 1 << 7 },
399 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
400 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
401 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
406 static struct platform_t __init *platform_detect(void)
408 char *boardnum, *boardtype, *buf;
410 boardnum = getvar("boardnum");
411 boardtype = getvar("boardtype");
412 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
413 /* CFE based - newer hardware */
414 if (!strcmp(boardnum, "42")) { /* Linksys */
415 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
416 return &platforms[WRT54G3G];
418 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
419 return &platforms[WRTSL54GS];
421 /* default to WRT54G */
422 return &platforms[WRT54G];
425 if (!strcmp(boardnum, "45")) { /* ASUS */
426 if (!strcmp(boardtype,"0x042f"))
427 return &platforms[WL500GP];
429 return &platforms[WL500GD];
432 if (!strcmp(boardnum, "10496"))
433 return &platforms[USR5461];
434 } else { /* PMON based - old stuff */
435 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
436 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
437 if (!strncmp(getvar("ModelId"),"WE800G", 6))
438 return &platforms[WE800G];
440 return &platforms[WR850GV1];
442 if (!strncmp(boardtype, "bcm94710dev", 11)) {
443 if (!strcmp(boardnum, "42"))
444 return &platforms[WRT54GV1];
445 if (simple_strtoul(boardnum, NULL, 0) == 2)
446 return &platforms[WAP54GV1];
448 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
449 return &platforms[WL500G];
450 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
451 /* Either WL-300g or WL-HDD, do more extensive checks */
452 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
453 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
454 return &platforms[WLHDD];
455 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
456 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
457 return &platforms[WL300G];
460 /* unknown asus stuff, probably bcm4702 */
461 if (!strncmp(boardnum, "asusX", 5))
462 return &platforms[ASUS_4702];
465 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
466 /* Buffalo hardware, check id for specific hardware matches */
467 if (!strcmp(buf, "29bb0332"))
468 return &platforms[WBR2_G54];
469 if (!strcmp(buf, "29129"))
470 return &platforms[WLA2_G54L];
471 if (!strcmp(buf, "30189"))
472 return &platforms[WHR_HP_G54];
473 if (!strcmp(buf, "30182"))
474 return &platforms[WHR_G54S];
475 if (!strcmp(buf, "290441dd"))
476 return &platforms[WHR2_A54G54];
477 if (!strcmp(buf, "30083"))
478 return &platforms[WZR_RS_G54];
481 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
482 if (!strncmp(boardtype, "bcm94710ap", 10))
483 return &platforms[BUFFALO_UNKNOWN_4710];
485 return &platforms[BUFFALO_UNKNOWN];
489 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
490 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
492 return &platforms[WR850GV2V3];
499 static void register_buttons(struct button_t *b)
502 platform.button_mask |= b->gpio;
504 platform.button_mask &= ~gpiomask;
506 gpio_outen(platform.button_mask, 0);
507 gpio_control(platform.button_mask, 0);
508 platform.button_polarity = gpio_in() & platform.button_mask;
509 gpio_intpolarity(platform.button_mask, platform.button_polarity);
510 gpio_intmask(platform.button_mask, platform.button_mask);
512 gpio_set_irqenable(1, button_handler);
515 static void unregister_buttons(struct button_t *b)
517 gpio_intmask(platform.button_mask, 0);
519 gpio_set_irqenable(0, button_handler);
522 static void hotplug_button(struct event_t *event)
524 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
525 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
527 call_usermodehelper (event->argv[0], event->argv, event->envp);
532 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
537 in = gpio_in() & platform.button_mask;
538 gpio_intpolarity(platform.button_mask, in);
539 changed = platform.button_polarity ^ in;
540 platform.button_polarity = in;
542 for (b = platform.buttons; b->name; b++) {
543 struct event_t *event;
545 if (!(b->gpio & changed)) continue;
549 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
551 char *scratch = event->buf;
554 event->argv[i++] = hotplug_path;
555 event->argv[i++] = "button";
559 event->envp[i++] = "HOME=/";
560 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
561 event->envp[i++] = scratch;
562 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
563 event->envp[i++] = scratch;
564 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
565 event->envp[i++] = scratch;
566 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
569 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
570 schedule_work(&event->wq);
578 static void register_leds(struct led_t *l)
580 struct proc_dir_entry *p;
584 leds = proc_mkdir("led", diag);
588 for(; l->name; l++) {
589 if (l->gpio & gpiomask)
592 if (l->gpio & GPIO_TYPE_EXTIF) {
597 val |= (l->polarity == NORMAL)?0:l->gpio;
600 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
601 l->proc.type = PROC_LED;
603 p->data = (void *) &l->proc;
604 p->proc_fops = &diag_proc_fops;
608 gpio_outen(mask, mask);
609 gpio_control(mask, 0);
613 static void unregister_leds(struct led_t *l)
616 remove_proc_entry(l->name, leds);
618 remove_proc_entry("led", diag);
621 static void set_led_extif(struct led_t *led)
623 gpio_set_extif(led->gpio, led->state);
626 static void led_flash(unsigned long dummy) {
631 for (l = platform.leds; l->name; l++) {
633 if (l->gpio & GPIO_TYPE_EXTIF) {
635 l->state = !l->state;
645 u32 val = ~gpio_in();
647 gpio_outen(mask, mask);
648 gpio_control(mask, 0);
651 if (mask || extif_blink) {
652 mod_timer(&led_timer, jiffies + FLASH_TIME);
656 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
659 struct inode *inode = file->f_dentry->d_inode;
660 struct proc_dir_entry *dent = inode->u.generic_ip;
662 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
667 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
670 if (dent->data != NULL) {
671 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
672 switch (handler->type) {
674 struct led_t * led = (struct led_t *) handler->ptr;
676 len = sprintf(page, "f\n");
678 if (led->gpio & GPIO_TYPE_EXTIF) {
679 len = sprintf(page, "%d\n", led->state);
681 u32 in = (gpio_in() & led->gpio ? 1 : 0);
682 u8 p = (led->polarity == NORMAL ? 0 : 1);
683 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
689 len = sprintf(page, "%s\n", platform.name);
692 len = sprintf(page, "0x%04x\n", gpiomask);
699 len = min_t(int, len - *ppos, count);
700 if (copy_to_user(buf, (page + *ppos), len)) {
713 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
716 struct inode *inode = file->f_dentry->d_inode;
717 struct proc_dir_entry *dent = inode->u.generic_ip;
719 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
724 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
727 if (copy_from_user(page, buf, count)) {
733 if (dent->data != NULL) {
734 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
735 switch (handler->type) {
737 struct led_t *led = (struct led_t *) handler->ptr;
738 int p = (led->polarity == NORMAL ? 0 : 1);
740 if (page[0] == 'f') {
745 if (led->gpio & GPIO_TYPE_EXTIF) {
746 led->state = p ^ ((page[0] == '1') ? 1 : 0);
749 gpio_outen(led->gpio, led->gpio);
750 gpio_control(led->gpio, 0);
751 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
757 gpiomask = simple_strtoul(page, NULL, 0);
759 if (platform.buttons) {
760 unregister_buttons(platform.buttons);
761 register_buttons(platform.buttons);
765 unregister_leds(platform.leds);
766 register_leds(platform.leds);
777 static int __init diag_init(void)
779 static struct proc_dir_entry *p;
780 static struct platform_t *detected;
782 detected = platform_detect();
784 printk(MODULE_NAME ": Router model not detected.\n");
787 memcpy(&platform, detected, sizeof(struct platform_t));
789 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
791 if (!(diag = proc_mkdir("diag", NULL))) {
792 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
796 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
797 p->data = (void *) &proc_model;
798 p->proc_fops = &diag_proc_fops;
801 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
802 p->data = (void *) &proc_gpiomask;
803 p->proc_fops = &diag_proc_fops;
806 if (platform.buttons)
807 register_buttons(platform.buttons);
810 register_leds(platform.leds);
815 static void __exit diag_exit(void)
818 del_timer(&led_timer);
820 if (platform.buttons)
821 unregister_buttons(platform.buttons);
824 unregister_leds(platform.leds);
826 remove_proc_entry("model", diag);
827 remove_proc_entry("gpiomask", diag);
828 remove_proc_entry("diag", NULL);
831 module_init(diag_init);
832 module_exit(diag_exit);
834 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
835 MODULE_LICENSE("GPL");