2 * diag.c - GPIO interface driver for Broadcom boards
4 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
5 * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * 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>
30 #include <linux/workqueue.h>
31 #include <linux/skbuff.h>
32 #include <linux/netlink.h>
33 #include <linux/kobject.h>
35 #include <bcm47xx_board.h>
36 extern u64
uevent_next_seqnum(void);
40 #define getvar(str) (nvram_get(str)?:"")
42 static inline int startswith (char *source
, char *cmp
) { return !strncmp(source
,cmp
,strlen(cmp
)); }
43 static int fill_event(struct event_t
*);
44 static unsigned int gpiomask
= 0;
45 module_param(gpiomask
, int, 0644);
47 extern char *nvram_get(char *str
);
48 static void led_flash(unsigned long dummy
);
50 static struct platform_t platform
;
52 static struct timer_list led_timer
= TIMER_INITIALIZER(&led_flash
, 0, 0);
54 static struct proc_dir_entry
*diag
, *leds
;
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
57 static inline struct inode
*file_inode(struct file
*f
)
59 return f
->f_path
.dentry
->d_inode
;
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
64 static inline void *PDE_DATA(const struct inode
*inode
)
66 return PDE(inode
)->data
;
123 /* Western Digital */
141 static void __init
bcm4780_init(void) {
144 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
145 printk(MODULE_NAME
": Spinning up HDD and enabling leds\n");
146 bcm47xx_gpio_outen(pin
, pin
);
147 bcm47xx_gpio_control(pin
, 0);
148 bcm47xx_gpio_out(pin
, pin
);
150 /* Wait 5s, so the HDD can spin up */
151 set_current_state(TASK_INTERRUPTIBLE
);
152 schedule_timeout(HZ
* 5);
155 static void __init
NetCenter_init(void) {
156 /* unset pin 6 (+12V) */
158 bcm47xx_gpio_outen(pin
, pin
);
159 bcm47xx_gpio_control(pin
, 0);
160 bcm47xx_gpio_out(pin
, pin
);
161 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
163 bcm47xx_gpio_outen(pin
, pin
);
164 bcm47xx_gpio_control(pin
, 0);
165 bcm47xx_gpio_out(pin
, pin
);
166 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
170 static struct platform_t __initdata platforms
[] = {
173 .name
= "Linksys WAP54G V1",
175 { .name
= "reset", .gpio
= 1 << 0 },
178 { .name
= "diag", .gpio
= 1 << 3 },
179 { .name
= "wlan", .gpio
= 1 << 4 },
183 .name
= "Linksys WAP54G V2",
185 { .name
= "reset", .gpio
= 1 << 0 },
188 { .name
= "wlan", .gpio
= 1 << 5, .polarity
= REVERSE
},
189 /* GPIO 6 is b44 (eth0, LAN) PHY power */
193 .name
= "Linksys WAP54G V3",
195 /* FIXME: verify this */
196 { .name
= "reset", .gpio
= 1 << 7 },
197 { .name
= "ses", .gpio
= 1 << 0 },
201 { .name
= "ses", .gpio
= 1 << 1 },
205 .name
= "Linksys WRT54G V1.x",
207 { .name
= "reset", .gpio
= 1 << 6 },
210 { .name
= "diag", .gpio
= 0x13 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
211 { .name
= "dmz", .gpio
= 0x12 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
215 .name
= "Linksys WRT54G/GS/GL",
217 { .name
= "reset", .gpio
= 1 << 6 },
218 { .name
= "ses", .gpio
= 1 << 4 },
221 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
222 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
223 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
224 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
225 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
229 .name
= "Linksys WRT54G3G",
231 { .name
= "reset", .gpio
= 1 << 6 },
232 { .name
= "3g", .gpio
= 1 << 4 },
235 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
236 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
237 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
238 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
239 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
243 .name
= "Linksys WRT350N",
245 { .name
= "reset", .gpio
= 1 << 6 },
246 { .name
= "ses", .gpio
= 1 << 8 },
249 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
250 { .name
= "ses_amber", .gpio
= 1 << 3, .polarity
= REVERSE
},
251 { .name
= "ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
},
252 { .name
= "usb_blink", .gpio
= 1 << 10, .polarity
= REVERSE
},
253 { .name
= "usb", .gpio
= 1 << 11, .polarity
= REVERSE
},
257 .name
= "Linksys WRT600N",
259 { .name
= "reset", .gpio
= 1 << 6 },
260 { .name
= "ses", .gpio
= 1 << 7 },
263 { .name
= "power", .gpio
= 1 << 2, .polarity
= REVERSE
}, // Power LED
264 { .name
= "usb", .gpio
= 1 << 3, .polarity
= REVERSE
}, // USB LED
265 { .name
= "wl0_ses_amber", .gpio
= 1 << 8, .polarity
= REVERSE
}, // 2.4Ghz LED Amber
266 { .name
= "wl0_ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
}, // 2.4Ghz LED Green
267 { .name
= "wl1_ses_amber", .gpio
= 1 << 10, .polarity
= REVERSE
}, // 5.6Ghz LED Amber
268 { .name
= "wl1_ses_green", .gpio
= 1 << 11, .polarity
= REVERSE
}, // 5.6Ghz LED Green
272 .name
= "Linksys WRT600N V1.1",
274 { .name
= "reset", .gpio
= 1 << 6 },
275 { .name
= "ses", .gpio
= 1 << 7 },
278 { .name
= "power", .gpio
= 1 << 2, .polarity
= REVERSE
}, // Power LED
279 { .name
= "usb", .gpio
= 1 << 3, .polarity
= REVERSE
}, // USB LED
280 { .name
= "wl0_ses_amber", .gpio
= 1 << 8, .polarity
= REVERSE
}, // 2.4Ghz LED Amber
281 { .name
= "wl0_ses_green", .gpio
= 1 << 9, .polarity
= REVERSE
}, // 2.4Ghz LED Green
282 { .name
= "wl1_ses_amber", .gpio
= 1 << 10, .polarity
= REVERSE
}, // 5.6Ghz LED Amber
283 { .name
= "wl1_ses_green", .gpio
= 1 << 11, .polarity
= REVERSE
}, // 5.6Ghz LED Green
288 .name
= "ASUS WL-500g",
290 { .name
= "reset", .gpio
= 1 << 6 },
293 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
297 .name
= "ASUS (unknown, BCM4702)",
299 { .name
= "reset", .gpio
= 1 << 6 },
302 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
307 .name
= "Buffalo WBR2-G54",
310 { .name
= "reset", .gpio
= 1 << 7 },
313 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
317 .name
= "Buffalo WLA2-G54L",
320 { .name
= "reset", .gpio
= 1 << 7 },
323 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
326 [BUFFALO_UNKNOWN_4710
] = {
327 .name
= "Buffalo (unknown, BCM4710)",
329 { .name
= "reset", .gpio
= 1 << 4 },
332 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
337 .name
= "Siemens SE505 V1",
339 /* No usable buttons */
342 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
343 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
}, // actual name WWW
344 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
348 .name
= "Siemens SE505 V2",
350 /* No usable buttons */
353 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
354 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
}, // actual name WWW
355 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
360 .name
= "U.S. Robotics USR5461",
362 /* No usable buttons */
365 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
366 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
371 .name
= "Dell TrueMobile 2300 v2",
373 { .name
= "reset", .gpio
= 1 << 0 },
376 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
377 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
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
},
397 .name
= "Netgear WGT634U",
399 { .name
= "reset", .gpio
= 1 << 2 },
402 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
407 .name
= "Netgear WNR834B V1",
408 .buttons
= { /* TODO: add reset button and confirm LEDs - GPIO from dd-wrt */ },
410 { .name
= "power", .gpio
= 1 << 4, .polarity
= REVERSE
},
411 { .name
= "diag", .gpio
= 1 << 5, .polarity
= REVERSE
},
412 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
417 .name
= "Netgear WNDR3700 V3",
419 /* { .name = "usb", .gpio = 1 << 1 }, */ /* this button doesn't seem to exist. */
420 { .name
= "wlan", .gpio
= 1 << 2 },
421 { .name
= "reset", .gpio
= 1 << 3 },
422 { .name
= "wps", .gpio
= 1 << 4 },
423 /* { .name = "switch", .gpio = 1 << 5 },*/ /* nvram get gpio5=robo_reset */
426 { .name
= "power", .gpio
= (1 << 0) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
427 { .name
= "diag", .gpio
= (1 << 1) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
428 /* WAN LED doesn't respond to GPIO control. The switch is probably driving it.
429 * { .name = "wan", .gpio = (1 << 2) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
431 { .name
= "wlan2g", .gpio
= (1 << 3) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
432 { .name
= "wlan5g", .gpio
= (1 << 4) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
433 { .name
= "usb", .gpio
= (1 << 5) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
434 { .name
= "wps", .gpio
= (1 << 6) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
435 { .name
= "wlan", .gpio
= (1 << 7) | GPIO_TYPE_SHIFT
, .polarity
= REVERSE
},
440 .name
= "Trendware TEW411BRP+",
442 { /* No usable buttons */ },
445 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
446 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
447 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
452 .name
= "SimpleTech SimpleShare NAS",
454 { .name
= "reset", .gpio
= 1 << 0 }, // Power button on back, named reset to enable failsafe.
457 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
459 .platform_init
= bcm4780_init
,
463 .name
= "D-Link DIR-320",
465 { .name
= "reserved", .gpio
= 1 << 6},
466 { .name
= "reset", .gpio
= 1 << 7},
469 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= NORMAL
},
470 { .name
= "diag", .gpio
= 1 << 1, .polarity
= NORMAL
}, /* "status led */
471 { .name
= "red", .gpio
= 1 << 3, .polarity
= REVERSE
},
472 { .name
= "blue", .gpio
= 1 << 4, .polarity
= REVERSE
},
473 { .name
= "usb", .gpio
= 1 << 5, .polarity
= NORMAL
},
477 .name
= "D-Link DWL-3150",
479 { .name
= "reset", .gpio
= 1 << 7},
482 { .name
= "diag", .gpio
= 1 << 2},
483 { .name
= "status", .gpio
= 1 << 1},
488 .name
= "Sitecom WL-105b",
490 { .name
= "reset", .gpio
= 1 << 10},
493 { .name
= "wlan", .gpio
= 1 << 4},
494 { .name
= "power", .gpio
= 1 << 3},
497 /* Western Digital Net Center */
499 .name
= "Western Digital NetCenter",
501 { .name
= "power", .gpio
= 1 << 0},
502 { .name
= "reset", .gpio
= 1 << 7},
504 .platform_init
= NetCenter_init
,
506 /* Askey (and clones) */
508 .name
= "Askey RT210W",
510 /* Power button is hard-wired to hardware reset */
511 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
512 { .name
= "power", .gpio
= 1 << 7},
515 /* These were verified and named based on Belkin F5D4230-4 v1112 */
516 { .name
= "connected", .gpio
= 1 << 0, .polarity
= REVERSE
},
517 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
518 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
522 .name
= "OvisLink WL-1600GL",
524 { .name
= "reset", .gpio
= 1 << 3 },
525 { .name
= "ses", .gpio
= 1 << 4 },
528 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
529 { .name
= "wps", .gpio
= 1 << 2, .polarity
= REVERSE
},
530 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= REVERSE
},
531 { .name
= "connected", .gpio
= 1 << 0, .polarity
= REVERSE
},
536 .name
= "Microsoft MN-700",
538 { .name
= "reset", .gpio
= 1 << 7 },
541 { .name
= "power", .gpio
= 1 << 6, .polarity
= NORMAL
},
546 .name
= "Edimax PS-1208MFG",
548 { .name
= "reset", .gpio
= 1 << 4 },
551 { .name
= "status", .gpio
= 1 << 1, .polarity
= NORMAL
},
552 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= NORMAL
},
558 static struct platform_t __init
*platform_detect_legacy(void)
560 char *boardnum
, *boardtype
;
562 if (strcmp(getvar("nvram_type"), "cfe") == 0)
563 return &platforms
[WGT634U
];
566 /* no easy model number, attempt to guess */
567 boardnum
= getvar("boardnum");
568 boardtype
= getvar("boardtype");
570 if (!strcmp(boardnum
, "20070615")) { /* Linksys WRT600N v1/V1.1 */
571 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
572 return &platforms
[WRT600NV11
];
574 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "0"))
575 return &platforms
[WRT600N
];
578 if (startswith(getvar("pmon_ver"), "CFE")) {
579 /* CFE based - newer hardware */
580 if (!strcmp(boardnum
, "42")) { /* Linksys */
581 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
582 return &platforms
[WRT350N
];
584 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
585 return &platforms
[WRT54G3G
];
587 /* default to WRT54G if no boot_hw_model is set */
588 if (nvram_get("boot_hw_model") == NULL
)
589 return &platforms
[WRT54G
];
591 if (!strcmp(boardnum
, "1024") && !strcmp(boardtype
, "0x0446"))
592 return &platforms
[WAP54GV2
];
594 if (!strcmp(boardnum
, "8") && !strcmp(boardtype
, "0x048e"))
595 return &platforms
[WL1600GL
];
598 if (!strcmp(boardnum
, "44") || !strcmp(boardnum
, "44\r")) {
599 if (!strcmp(boardtype
,"0x0101") || !strcmp(boardtype
, "0x0101\r"))
600 return &platforms
[TM2300V2
]; /* Dell TrueMobile 2300 v2 */
603 if (!strcmp(boardnum
, "10496"))
604 return &platforms
[USR5461
];
606 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
607 return &platforms
[SE505V2
];
609 if (!strcmp(boardtype
, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
610 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
611 return &platforms
[DIR320
];
613 if (!strncmp(boardnum
, "TH",2) && !strcmp(boardtype
,"0x042f")) {
614 return &platforms
[WDNetCenter
];
617 if (!strcmp(boardtype
,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B V1 and V2*/
618 if (!strcmp(boardnum
, "08") || !strcmp(boardnum
, "8"))
619 return &platforms
[WNR834BV1
];
622 } else { /* PMON based - old stuff */
623 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
624 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
625 return &platforms
[WR850GV1
];
627 if (startswith(boardtype
, "bcm94710dev")) {
628 if (!strcmp(boardnum
, "42"))
629 return &platforms
[WRT54GV1
];
630 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
631 return &platforms
[WAP54GV1
];
633 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
634 if (startswith(getvar("hardware_version"), "WL500-")) {
635 if (!strcmp(getvar("boardnum"), "mn700"))
636 return &platforms
[MN700
];
638 return &platforms
[WL500G
];
640 /* Sitecom WL-105b */
641 if (startswith(boardnum
, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 1)
642 return &platforms
[WL105B
];
644 /* unknown asus stuff, probably bcm4702 */
645 if (startswith(boardnum
, "asusX"))
646 return &platforms
[ASUS_4702
];
648 /* bcm4702 based Askey RT210W clones, Including:
649 * Askey RT210W (duh?)
651 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
653 if (!strcmp(boardtype
,"bcm94710r4")
654 && !strcmp(boardnum
,"100")
655 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
657 return &platforms
[RT210W
];
661 if (boardnum
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
662 if (startswith(boardtype
, "bcm94710ap"))
663 return &platforms
[BUFFALO_UNKNOWN_4710
];
666 if (!strcmp(boardnum
, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
667 return &platforms
[TEW411BRPP
];
670 if (startswith(boardnum
, "04FN")) /* SimpleTech SimpleShare */
671 return &platforms
[STI_NAS
];
673 if (!strcmp(boardnum
, "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
674 return &platforms
[DWL3150
];
676 if (!strcmp(boardnum
, "01") && !strcmp(boardtype
, "0x048e") && /* Edimax PS1208MFG */
677 !strcmp(getvar("status_gpio"), "1")) /* gpio based detection */
678 return &platforms
[PS1208MFG
];
684 static struct platform_t __init
*platform_detect(void)
686 enum bcm47xx_board board
;
687 const char *board_name
;
690 board
= bcm47xx_board_get();
691 board_name
= bcm47xx_board_get_name();
694 case BCM47XX_BOARD_BUFFALO_WBR2_G54
:
695 return &platforms
[WBR2_G54
];
696 case BCM47XX_BOARD_BUFFALO_WLA2_G54L
:
697 return &platforms
[WLA2_G54L
];
698 case BCM47XX_BOARD_LINKSYS_WRT54GSV1
:
699 return &platforms
[WRT54G
];
700 case BCM47XX_BOARD_NETGEAR_WNDR3700V3
:
701 return &platforms
[WNDR3700V3
];
702 case BCM47XX_BOARD_UNKNOWN
:
703 case BCM47XX_BOARD_NO
:
704 printk(MODULE_NAME
": unknown board found, try legacy detect\n");
705 printk(MODULE_NAME
": please open a ticket at https://dev.openwrt.org and attach the complete nvram\n");
706 return platform_detect_legacy();
708 printk(MODULE_NAME
": board was detected as \"%s\", and kernel should handle it\n", board_name
);
713 static inline void ssb_maskset32(struct ssb_device
*dev
,
714 u16 offset
, u32 mask
, u32 set
)
716 ssb_write32(dev
, offset
, (ssb_read32(dev
, offset
) & mask
) | set
);
719 static void gpio_set_irqenable(int enabled
, irqreturn_t (*handler
)(int, void *))
724 irq
= gpio_to_irq(0);
726 pr_err("no irq for gpio available\n");
731 err
= request_irq(irq
, handler
, IRQF_SHARED
, "gpio", handler
);
733 pr_err("can not reqeust irq\n");
737 free_irq(irq
, handler
);
740 switch (bcm47xx_bus_type
) {
741 #ifdef CONFIG_BCM47XX_SSB
742 case BCM47XX_BUS_TYPE_SSB
:
743 if (bcm47xx_bus
.ssb
.chipco
.dev
)
744 ssb_maskset32(bcm47xx_bus
.ssb
.chipco
.dev
, SSB_CHIPCO_IRQMASK
, ~SSB_CHIPCO_IRQ_GPIO
, (enabled
? SSB_CHIPCO_IRQ_GPIO
: 0));
747 #ifdef CONFIG_BCM47XX_BCMA
748 case BCM47XX_BUS_TYPE_BCMA
:
749 if (bcm47xx_bus
.bcma
.bus
.drv_cc
.core
)
750 bcma_maskset32(bcm47xx_bus
.bcma
.bus
.drv_cc
.core
, BCMA_CC_IRQMASK
, ~BCMA_CC_IRQ_GPIO
, (enabled
? BCMA_CC_IRQ_GPIO
: 0));
756 static void hotplug_button(struct work_struct
*work
)
758 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
761 event
->skb
= alloc_skb(2048, GFP_KERNEL
);
763 s
= skb_put(event
->skb
, strlen(event
->action
) + 2);
764 sprintf(s
, "%s@", event
->action
);
767 NETLINK_CB(event
->skb
).dst_group
= 1;
768 broadcast_uevent(event
->skb
, 0, 1, GFP_KERNEL
);
773 static irqreturn_t
button_handler(int irq
, void *dev_id
)
778 in
= bcm47xx_gpio_in(~0) & platform
.button_mask
;
779 bcm47xx_gpio_polarity(platform
.button_mask
, in
);
780 changed
= platform
.button_polarity
^ in
;
781 platform
.button_polarity
= in
;
783 changed
&= ~bcm47xx_gpio_outen(0, 0);
785 for (b
= platform
.buttons
; b
->name
; b
++) {
786 struct event_t
*event
;
788 if (!(b
->gpio
& changed
)) continue;
792 if ((event
= (struct event_t
*)kzalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
793 event
->seen
= (jiffies
- b
->seen
)/HZ
;
794 event
->name
= b
->name
;
795 event
->action
= b
->pressed
? "pressed" : "released";
796 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
797 schedule_work(&event
->wq
);
805 static void register_buttons(struct button_t
*b
)
808 platform
.button_mask
|= b
->gpio
;
810 platform
.button_mask
&= ~gpiomask
;
812 bcm47xx_gpio_outen(platform
.button_mask
, 0);
813 bcm47xx_gpio_control(platform
.button_mask
, 0);
814 platform
.button_polarity
= bcm47xx_gpio_in(~0) & platform
.button_mask
;
815 bcm47xx_gpio_polarity(platform
.button_mask
, platform
.button_polarity
);
816 bcm47xx_gpio_intmask(platform
.button_mask
, platform
.button_mask
);
818 gpio_set_irqenable(1, button_handler
);
821 static void unregister_buttons(struct button_t
*b
)
823 bcm47xx_gpio_intmask(platform
.button_mask
, 0);
825 gpio_set_irqenable(0, button_handler
);
829 static void add_msg(struct event_t
*event
, char *msg
, int argv
)
836 s
= skb_put(event
->skb
, strlen(msg
) + 1);
840 static int fill_event (struct event_t
*event
)
842 static char buf
[128];
844 add_msg(event
, "HOME=/", 0);
845 add_msg(event
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
846 add_msg(event
, "SUBSYSTEM=button", 0);
847 snprintf(buf
, 128, "ACTION=%s", event
->action
);
848 add_msg(event
, buf
, 0);
849 snprintf(buf
, 128, "BUTTON=%s", event
->name
);
850 add_msg(event
, buf
, 0);
851 snprintf(buf
, 128, "SEEN=%ld", event
->seen
);
852 add_msg(event
, buf
, 0);
853 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
854 add_msg(event
, buf
, 0);
860 * This should be extended to allow the platform to specify the pins and width
861 * of the shift register. They're hardcoded for now because only the WNDR3700v3
864 static void shiftreg_output(unsigned int val
)
868 bcm47xx_gpio_out(SHIFTREG_DATA
, SHIFTREG_DATA
); /* init off, pull high */
869 bcm47xx_gpio_out(SHIFTREG_CLK
, 0); /* init reset */
872 for(mask
= 1 << (SHIFTREG_MAX_BITS
-1); mask
; mask
>>= 1)
874 bcm47xx_gpio_out(SHIFTREG_DATA
, (val
& mask
) ? SHIFTREG_DATA
: 0);
875 bcm47xx_gpio_out(SHIFTREG_CLK
, SHIFTREG_CLK
); /* pull high to trigger */
876 bcm47xx_gpio_out(SHIFTREG_CLK
, 0); /* reset to low */
880 static void set_led_shift(struct led_t
*led
)
882 static u32 shiftreg
= 0;
884 u32 pin
= (led
->gpio
& ~GPIO_TYPE_MASK
);
892 /* Clock the bits out. */
893 if (shiftreg
!= old
) {
894 shiftreg_output(shiftreg
);
898 static void set_led_extif(struct led_t
*led
)
900 volatile u8
*addr
= (volatile u8
*) KSEG1ADDR(EXTIF_UART
) + (led
->gpio
& ~GPIO_TYPE_MASK
);
908 static void led_flash(unsigned long dummy
) {
913 for (l
= platform
.leds
; l
->name
; l
++) {
914 if (!l
->flash
) continue;
915 switch (l
->gpio
& GPIO_TYPE_MASK
) {
916 case GPIO_TYPE_EXTIF
:
918 l
->state
= !l
->state
;
921 case GPIO_TYPE_SHIFT
:
923 l
->state
= !l
->state
;
926 case GPIO_TYPE_NORMAL
:
935 u32 val
= ~bcm47xx_gpio_in(~0);
937 bcm47xx_gpio_outen(mask
, mask
);
938 bcm47xx_gpio_control(mask
, 0);
939 bcm47xx_gpio_out(mask
, val
);
941 if (mask
|| extif_blink
) {
942 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
946 static int diag_led_show(struct seq_file
*m
, void *v
)
948 struct led_t
* led
= m
->private;
950 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
952 return seq_printf(m
, "f\n");
953 } else if ((led
->gpio
& GPIO_TYPE_MASK
) != GPIO_TYPE_NORMAL
) {
954 return seq_printf(m
, "%d\n", ((led
->state
^ p
) ? 1 : 0));
956 u32 in
= (bcm47xx_gpio_in(~0) & led
->gpio
? 1 : 0);
957 return seq_printf(m
, "%d\n", ((in
^ p
) ? 1 : 0));
961 static int diag_led_open(struct inode
*inode
, struct file
*file
)
963 return single_open(file
, diag_led_show
, PDE_DATA(inode
));
966 static ssize_t
diag_led_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
968 struct led_t
*led
= PDE_DATA(file_inode(file
));
973 len
= min(count
, sizeof(cmd
) - 1);
974 if (copy_from_user(cmd
, buf
, len
))
979 p
= (led
->polarity
== NORMAL
? 0 : 1);
985 if ((led
->gpio
& GPIO_TYPE_MASK
) == GPIO_TYPE_EXTIF
) {
986 led
->state
= p
^ ((cmd
[0] == '1') ? 1 : 0);
988 } else if ((led
->gpio
& GPIO_TYPE_MASK
) == GPIO_TYPE_SHIFT
) {
989 led
->state
= p
^ ((cmd
[0] == '1') ? 1 : 0);
992 bcm47xx_gpio_outen(led
->gpio
, led
->gpio
);
993 bcm47xx_gpio_control(led
->gpio
, 0);
994 bcm47xx_gpio_out(led
->gpio
, ((p
^ (cmd
[0] == '1')) ? led
->gpio
: 0));
1000 static const struct file_operations diag_led_fops
= {
1001 .open
= diag_led_open
,
1003 .llseek
= seq_lseek
,
1004 .write
= diag_led_write
1007 static void register_leds(struct led_t
*l
)
1009 struct proc_dir_entry
*p
;
1014 leds
= proc_mkdir("led", diag
);
1018 for(; l
->name
; l
++) {
1019 if (l
->gpio
& gpiomask
)
1022 switch (l
->gpio
& GPIO_TYPE_MASK
) {
1023 case GPIO_TYPE_EXTIF
:
1027 case GPIO_TYPE_SHIFT
:
1028 mask
|= (SHIFTREG_DATA
| SHIFTREG_CLK
);
1029 oe_mask
|= (SHIFTREG_DATA
| SHIFTREG_CLK
);
1030 l
->state
= (l
->polarity
!= NORMAL
);
1033 case GPIO_TYPE_NORMAL
:
1035 if (l
->polarity
!= INPUT
) oe_mask
|= l
->gpio
;
1037 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
1041 if (l
->polarity
== INPUT
) continue;
1043 p
= proc_create_data(l
->name
, S_IRUSR
, leds
, &diag_led_fops
, l
);
1046 bcm47xx_gpio_outen(mask
, oe_mask
);
1047 bcm47xx_gpio_control(mask
, 0);
1048 bcm47xx_gpio_out(mask
, val
);
1049 bcm47xx_gpio_intmask(mask
, 0);
1052 static void unregister_leds(struct led_t
*l
)
1055 remove_proc_entry(l
->name
, leds
);
1057 remove_proc_entry("led", diag
);
1060 static int diag_model_show(struct seq_file
*m
, void *v
)
1062 return seq_printf(m
, "%s\n", platform
.name
);
1065 static int diag_model_open(struct inode
*inode
, struct file
*file
)
1067 return single_open(file
, diag_model_show
, PDE_DATA(inode
));
1070 static const struct file_operations diag_model_fops
= {
1071 .open
= diag_model_open
,
1076 static int diag_gpiomask_show(struct seq_file
*m
, void *v
)
1078 return seq_printf(m
, "0x%04x\n", gpiomask
);
1081 static int diag_gpiomask_open(struct inode
*inode
, struct file
*file
)
1083 return single_open(file
, diag_gpiomask_show
, PDE_DATA(inode
));
1086 static ssize_t
diag_gpiomask_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
1088 int err
= kstrtouint_from_user(buf
, count
, 0, &gpiomask
);
1092 if (platform
.buttons
) {
1093 unregister_buttons(platform
.buttons
);
1094 register_buttons(platform
.buttons
);
1097 if (platform
.leds
) {
1098 unregister_leds(platform
.leds
);
1099 register_leds(platform
.leds
);
1105 static const struct file_operations diag_gpiomask_fops
= {
1106 .open
= diag_gpiomask_open
,
1108 .llseek
= seq_lseek
,
1109 .write
= diag_gpiomask_write
1112 static int __init
diag_init(void)
1114 static struct proc_dir_entry
*p
;
1115 static struct platform_t
*detected
;
1117 detected
= platform_detect();
1119 printk(MODULE_NAME
": Router model not detected.\n");
1122 memcpy(&platform
, detected
, sizeof(struct platform_t
));
1124 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
1125 if (platform
.platform_init
!= NULL
) {
1126 platform
.platform_init();
1129 if (!(diag
= proc_mkdir("diag", NULL
))) {
1130 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1134 p
= proc_create("model", S_IRUSR
, diag
, &diag_model_fops
);
1136 remove_proc_entry("diag", NULL
);
1140 p
= proc_create("gpiomask", S_IRUSR
| S_IWUSR
, diag
, &diag_gpiomask_fops
);
1142 remove_proc_entry("model", diag
);
1143 remove_proc_entry("diag", NULL
);
1147 if (platform
.buttons
)
1148 register_buttons(platform
.buttons
);
1151 register_leds(platform
.leds
);
1156 static void __exit
diag_exit(void)
1158 del_timer(&led_timer
);
1160 if (platform
.buttons
)
1161 unregister_buttons(platform
.buttons
);
1164 unregister_leds(platform
.leds
);
1166 remove_proc_entry("model", diag
);
1167 remove_proc_entry("gpiomask", diag
);
1168 remove_proc_entry("diag", NULL
);
1171 module_init(diag_init
);
1172 module_exit(diag_exit
);
1174 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1175 MODULE_LICENSE("GPL");