81fb649bfb8c08e67ed2664746009ce913da75d5
[openwrt/svn-archive/archive.git] / package / kernel / broadcom-diag / src / diag.c
1 /*
2 * diag.c - GPIO interface driver for Broadcom boards
3 *
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>
7 *
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.
12 *
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.
17 *
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.
21 *
22 */
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>
34 #include <net/sock.h>
35 #include <bcm47xx_board.h>
36 extern u64 uevent_next_seqnum(void);
37
38 #include "gpio.h"
39 #include "diag.h"
40 #define getvar(str) (nvram_get(str)?:"")
41
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);
46
47 extern char *nvram_get(char *str);
48 static void led_flash(unsigned long dummy);
49
50 static struct platform_t platform;
51
52 static struct timer_list led_timer = TIMER_INITIALIZER(&led_flash, 0, 0);
53
54 static struct proc_dir_entry *diag, *leds;
55
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
57 static inline struct inode *file_inode(struct file *f)
58 {
59 return f->f_path.dentry->d_inode;
60 }
61 #endif
62
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
64 static inline void *PDE_DATA(const struct inode *inode)
65 {
66 return PDE(inode)->data;
67 }
68 #endif
69
70
71 enum {
72 /* Linksys */
73 WAP54GV1,
74 WAP54GV2,
75 WAP54GV3,
76 WRT54GV1,
77 WRT54G,
78 WRT54G3G,
79 WRT350N,
80 WRT600N,
81 WRT600NV11,
82
83 /* ASUS */
84 WL500G,
85 ASUS_4702,
86
87 /* Buffalo */
88 WBR2_G54,
89 WLA2_G54L,
90 BUFFALO_UNKNOWN_4710,
91
92 /* Siemens */
93 SE505V1,
94 SE505V2,
95
96 /* US Robotics */
97 USR5461,
98
99 /* Dell */
100 TM2300V2,
101
102 /* Motorola */
103 WR850GV1,
104
105 /* Netgear */
106 WGT634U,
107 WNR834BV1,
108 WNDR3700V3,
109
110 /* Trendware */
111 TEW411BRPP,
112
113 /* SimpleTech */
114 STI_NAS,
115
116 /* D-Link */
117 DIR320,
118 DWL3150,
119
120 /* Sitecom */
121 WL105B,
122
123 /* Western Digital */
124 WDNetCenter,
125
126 /* Askey */
127 RT210W,
128
129 /* OvisLink */
130 WL1600GL,
131
132 /* Microsoft */
133 MN700,
134
135 /* Edimax */
136 PS1208MFG,
137
138 /* Huawei */
139 };
140
141 static void __init bcm4780_init(void) {
142 int pin = 1 << 3;
143
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);
149
150 /* Wait 5s, so the HDD can spin up */
151 set_current_state(TASK_INTERRUPTIBLE);
152 schedule_timeout(HZ * 5);
153 }
154
155 static void __init NetCenter_init(void) {
156 /* unset pin 6 (+12V) */
157 int pin = 1 << 6;
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) */
162 pin = 1 << 1;
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) */
167 bcm4780_init();
168 }
169
170 static struct platform_t __initdata platforms[] = {
171 /* Linksys */
172 [WAP54GV1] = {
173 .name = "Linksys WAP54G V1",
174 .buttons = {
175 { .name = "reset", .gpio = 1 << 0 },
176 },
177 .leds = {
178 { .name = "diag", .gpio = 1 << 3 },
179 { .name = "wlan", .gpio = 1 << 4 },
180 },
181 },
182 [WAP54GV2] = {
183 .name = "Linksys WAP54G V2",
184 .buttons = {
185 { .name = "reset", .gpio = 1 << 0 },
186 },
187 .leds = {
188 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
189 /* GPIO 6 is b44 (eth0, LAN) PHY power */
190 },
191 },
192 [WAP54GV3] = {
193 .name = "Linksys WAP54G V3",
194 .buttons = {
195 /* FIXME: verify this */
196 { .name = "reset", .gpio = 1 << 7 },
197 { .name = "ses", .gpio = 1 << 0 },
198 },
199 .leds = {
200 /* FIXME: diag? */
201 { .name = "ses", .gpio = 1 << 1 },
202 },
203 },
204 [WRT54GV1] = {
205 .name = "Linksys WRT54G V1.x",
206 .buttons = {
207 { .name = "reset", .gpio = 1 << 6 },
208 },
209 .leds = {
210 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
211 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
212 },
213 },
214 [WRT54G] = {
215 .name = "Linksys WRT54G/GS/GL",
216 .buttons = {
217 { .name = "reset", .gpio = 1 << 6 },
218 { .name = "ses", .gpio = 1 << 4 },
219 },
220 .leds = {
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 },
226 },
227 },
228 [WRT54G3G] = {
229 .name = "Linksys WRT54G3G",
230 .buttons = {
231 { .name = "reset", .gpio = 1 << 6 },
232 { .name = "3g", .gpio = 1 << 4 },
233 },
234 .leds = {
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 },
240 },
241 },
242 [WRT350N] = {
243 .name = "Linksys WRT350N",
244 .buttons = {
245 { .name = "reset", .gpio = 1 << 6 },
246 { .name = "ses", .gpio = 1 << 8 },
247 },
248 .leds = {
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 },
254 },
255 },
256 [WRT600N] = {
257 .name = "Linksys WRT600N",
258 .buttons = {
259 { .name = "reset", .gpio = 1 << 6 },
260 { .name = "ses", .gpio = 1 << 7 },
261 },
262 .leds = {
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
269 },
270 },
271 [WRT600NV11] = {
272 .name = "Linksys WRT600N V1.1",
273 .buttons = {
274 { .name = "reset", .gpio = 1 << 6 },
275 { .name = "ses", .gpio = 1 << 7 },
276 },
277 .leds = {
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
284 },
285 },
286 /* Asus */
287 [WL500G] = {
288 .name = "ASUS WL-500g",
289 .buttons = {
290 { .name = "reset", .gpio = 1 << 6 },
291 },
292 .leds = {
293 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
294 },
295 },
296 [ASUS_4702] = {
297 .name = "ASUS (unknown, BCM4702)",
298 .buttons = {
299 { .name = "reset", .gpio = 1 << 6 },
300 },
301 .leds = {
302 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
303 },
304 },
305 /* Buffalo */
306 [WBR2_G54] = {
307 .name = "Buffalo WBR2-G54",
308 /* FIXME: verify */
309 .buttons = {
310 { .name = "reset", .gpio = 1 << 7 },
311 },
312 .leds = {
313 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
314 },
315 },
316 [WLA2_G54L] = {
317 .name = "Buffalo WLA2-G54L",
318 /* FIXME: verify */
319 .buttons = {
320 { .name = "reset", .gpio = 1 << 7 },
321 },
322 .leds = {
323 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
324 },
325 },
326 [BUFFALO_UNKNOWN_4710] = {
327 .name = "Buffalo (unknown, BCM4710)",
328 .buttons = {
329 { .name = "reset", .gpio = 1 << 4 },
330 },
331 .leds = {
332 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
333 },
334 },
335 /* Siemens */
336 [SE505V1] = {
337 .name = "Siemens SE505 V1",
338 .buttons = {
339 /* No usable buttons */
340 },
341 .leds = {
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 },
345 },
346 },
347 [SE505V2] = {
348 .name = "Siemens SE505 V2",
349 .buttons = {
350 /* No usable buttons */
351 },
352 .leds = {
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 },
356 },
357 },
358 /* US Robotics */
359 [USR5461] = {
360 .name = "U.S. Robotics USR5461",
361 .buttons = {
362 /* No usable buttons */
363 },
364 .leds = {
365 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
366 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
367 },
368 },
369 /* Dell */
370 [TM2300V2] = {
371 .name = "Dell TrueMobile 2300 v2",
372 .buttons = {
373 { .name = "reset", .gpio = 1 << 0 },
374 },
375 .leds = {
376 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
377 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
378 },
379 },
380 /* Motorola */
381 [WR850GV1] = {
382 .name = "Motorola WR850G V1",
383 .buttons = {
384 { .name = "reset", .gpio = 1 << 0 },
385 },
386 .leds = {
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 },
392 },
393 },
394
395 /* Netgear */
396 [WGT634U] = {
397 .name = "Netgear WGT634U",
398 .buttons = {
399 { .name = "reset", .gpio = 1 << 2 },
400 },
401 .leds = {
402 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
403 },
404 },
405 /* Netgear */
406 [WNR834BV1] = {
407 .name = "Netgear WNR834B V1",
408 .buttons = { /* TODO: add reset button and confirm LEDs - GPIO from dd-wrt */ },
409 .leds = {
410 { .name = "power", .gpio = 1 << 4, .polarity = REVERSE },
411 { .name = "diag", .gpio = 1 << 5, .polarity = REVERSE },
412 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
413 },
414 },
415 /* Netgear */
416 [WNDR3700V3] = {
417 .name = "Netgear WNDR3700 V3",
418 .buttons = {
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 */
424 },
425 .leds = {
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 },
430 */
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 },
436 },
437 },
438 /* Trendware */
439 [TEW411BRPP] = {
440 .name = "Trendware TEW411BRP+",
441 .buttons = {
442 { /* No usable buttons */ },
443 },
444 .leds = {
445 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
446 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
447 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
448 },
449 },
450 /* SimpleTech */
451 [STI_NAS] = {
452 .name = "SimpleTech SimpleShare NAS",
453 .buttons = {
454 { .name = "reset", .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
455 },
456 .leds = {
457 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
458 },
459 .platform_init = bcm4780_init,
460 },
461 /* D-Link */
462 [DIR320] = {
463 .name = "D-Link DIR-320",
464 .buttons = {
465 { .name = "reserved", .gpio = 1 << 6},
466 { .name = "reset", .gpio = 1 << 7},
467 },
468 .leds = {
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 },
474 },
475 },
476 [DWL3150] = {
477 .name = "D-Link DWL-3150",
478 .buttons = {
479 { .name = "reset", .gpio = 1 << 7},
480 },
481 .leds = {
482 { .name = "diag", .gpio = 1 << 2},
483 { .name = "status", .gpio = 1 << 1},
484 },
485 },
486 /* Double check */
487 [WL105B] = {
488 .name = "Sitecom WL-105b",
489 .buttons = {
490 { .name = "reset", .gpio = 1 << 10},
491 },
492 .leds = {
493 { .name = "wlan", .gpio = 1 << 4},
494 { .name = "power", .gpio = 1 << 3},
495 },
496 },
497 /* Western Digital Net Center */
498 [WDNetCenter] = {
499 .name = "Western Digital NetCenter",
500 .buttons = {
501 { .name = "power", .gpio = 1 << 0},
502 { .name = "reset", .gpio = 1 << 7},
503 },
504 .platform_init = NetCenter_init,
505 },
506 /* Askey (and clones) */
507 [RT210W] = {
508 .name = "Askey RT210W",
509 .buttons = {
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},
513 },
514 .leds = {
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 },
519 },
520 },
521 [WL1600GL] = {
522 .name = "OvisLink WL-1600GL",
523 .buttons = {
524 { .name = "reset", .gpio = 1 << 3 },
525 { .name = "ses", .gpio = 1 << 4 },
526 },
527 .leds = {
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 },
532 },
533 },
534 /* Microsoft */
535 [MN700] = {
536 .name = "Microsoft MN-700",
537 .buttons = {
538 { .name = "reset", .gpio = 1 << 7 },
539 },
540 .leds = {
541 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
542 },
543 },
544 /* Edimax */
545 [PS1208MFG] = {
546 .name = "Edimax PS-1208MFG",
547 .buttons = {
548 { .name = "reset", .gpio = 1 << 4 },
549 },
550 .leds = {
551 { .name = "status", .gpio = 1 << 1, .polarity = NORMAL },
552 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
553 },
554 },
555 /* Huawei */
556 };
557
558 static struct platform_t __init *platform_detect_legacy(void)
559 {
560 char *boardnum, *boardtype;
561
562 if (strcmp(getvar("nvram_type"), "cfe") == 0)
563 return &platforms[WGT634U];
564
565
566 /* no easy model number, attempt to guess */
567 boardnum = getvar("boardnum");
568 boardtype = getvar("boardtype");
569
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];
573
574 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
575 return &platforms[WRT600N];
576 }
577
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];
583
584 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
585 return &platforms[WRT54G3G];
586
587 /* default to WRT54G if no boot_hw_model is set */
588 if (nvram_get("boot_hw_model") == NULL)
589 return &platforms[WRT54G];
590 }
591 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
592 return &platforms[WAP54GV2];
593
594 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
595 return &platforms[WL1600GL];
596
597
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 */
601 }
602
603 if (!strcmp(boardnum, "10496"))
604 return &platforms[USR5461];
605
606 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
607 return &platforms[SE505V2];
608
609 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
610 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
611 return &platforms[DIR320];
612
613 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
614 return &platforms[WDNetCenter];
615 }
616
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];
620 }
621
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];
626 }
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];
632 }
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];
637 else
638 return &platforms[WL500G];
639 }
640 /* Sitecom WL-105b */
641 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
642 return &platforms[WL105B];
643
644 /* unknown asus stuff, probably bcm4702 */
645 if (startswith(boardnum, "asusX"))
646 return &platforms[ASUS_4702];
647
648 /* bcm4702 based Askey RT210W clones, Including:
649 * Askey RT210W (duh?)
650 * Siemens SE505v1
651 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
652 */
653 if (!strcmp(boardtype,"bcm94710r4")
654 && !strcmp(boardnum ,"100")
655 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
656 ){
657 return &platforms[RT210W];
658 }
659 }
660
661 if (boardnum || !strcmp(boardnum, "00")) {/* probably buffalo */
662 if (startswith(boardtype, "bcm94710ap"))
663 return &platforms[BUFFALO_UNKNOWN_4710];
664 }
665
666 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
667 return &platforms[TEW411BRPP];
668 }
669
670 if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
671 return &platforms[STI_NAS];
672
673 if (!strcmp(boardnum, "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
674 return &platforms[DWL3150];
675
676 if (!strcmp(boardnum, "01") && !strcmp(boardtype, "0x048e") && /* Edimax PS1208MFG */
677 !strcmp(getvar("status_gpio"), "1")) /* gpio based detection */
678 return &platforms[PS1208MFG];
679
680 /* not found */
681 return NULL;
682 }
683
684 static struct platform_t __init *platform_detect(void)
685 {
686 enum bcm47xx_board board;
687 const char *board_name;
688
689
690 board = bcm47xx_board_get();
691 board_name = bcm47xx_board_get_name();
692
693 switch(board) {
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();
707 default:
708 printk(MODULE_NAME ": board was detected as \"%s\", and kernel should handle it\n", board_name);
709 return NULL;
710 }
711 }
712
713 static inline void ssb_maskset32(struct ssb_device *dev,
714 u16 offset, u32 mask, u32 set)
715 {
716 ssb_write32(dev, offset, (ssb_read32(dev, offset) & mask) | set);
717 }
718
719 static void gpio_set_irqenable(int enabled, irqreturn_t (*handler)(int, void *))
720 {
721 int irq;
722 int err;
723
724 irq = gpio_to_irq(0);
725 if (irq < 0) {
726 pr_err("no irq for gpio available\n");
727 return;
728 }
729
730 if (enabled) {
731 err = request_irq(irq, handler, IRQF_SHARED, "gpio", handler);
732 if (err) {
733 pr_err("can not reqeust irq\n");
734 return;
735 }
736 } else {
737 free_irq(irq, handler);
738 }
739
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));
745 break;
746 #endif
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));
751 break;
752 #endif
753 }
754 }
755
756 static void hotplug_button(struct work_struct *work)
757 {
758 struct event_t *event = container_of(work, struct event_t, wq);
759 char *s;
760
761 event->skb = alloc_skb(2048, GFP_KERNEL);
762
763 s = skb_put(event->skb, strlen(event->action) + 2);
764 sprintf(s, "%s@", event->action);
765 fill_event(event);
766
767 NETLINK_CB(event->skb).dst_group = 1;
768 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
769
770 kfree(event);
771 }
772
773 static irqreturn_t button_handler(int irq, void *dev_id)
774 {
775 struct button_t *b;
776 u32 in, changed;
777
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;
782
783 changed &= ~bcm47xx_gpio_outen(0, 0);
784
785 for (b = platform.buttons; b->name; b++) {
786 struct event_t *event;
787
788 if (!(b->gpio & changed)) continue;
789
790 b->pressed ^= 1;
791
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);
798 }
799
800 b->seen = jiffies;
801 }
802 return IRQ_HANDLED;
803 }
804
805 static void register_buttons(struct button_t *b)
806 {
807 for (; b->name; b++)
808 platform.button_mask |= b->gpio;
809
810 platform.button_mask &= ~gpiomask;
811
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);
817
818 gpio_set_irqenable(1, button_handler);
819 }
820
821 static void unregister_buttons(struct button_t *b)
822 {
823 bcm47xx_gpio_intmask(platform.button_mask, 0);
824
825 gpio_set_irqenable(0, button_handler);
826 }
827
828
829 static void add_msg(struct event_t *event, char *msg, int argv)
830 {
831 char *s;
832
833 if (argv)
834 return;
835
836 s = skb_put(event->skb, strlen(msg) + 1);
837 strcpy(s, msg);
838 }
839
840 static int fill_event (struct event_t *event)
841 {
842 static char buf[128];
843
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);
855
856 return 0;
857 }
858
859 /*
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
862 * uses it.
863 */
864 static void shiftreg_output(unsigned int val)
865 {
866 unsigned int mask;
867
868 bcm47xx_gpio_out(SHIFTREG_DATA, SHIFTREG_DATA); /* init off, pull high */
869 bcm47xx_gpio_out(SHIFTREG_CLK, 0); /* init reset */
870
871 /* shift 8 times */
872 for(mask = 1 << (SHIFTREG_MAX_BITS-1); mask; mask >>= 1)
873 {
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 */
877 }
878 }
879
880 static void set_led_shift(struct led_t *led)
881 {
882 static u32 shiftreg = 0;
883 u32 old = shiftreg;
884 u32 pin = (led->gpio & ~GPIO_TYPE_MASK);
885
886 if (led->state) {
887 shiftreg |= pin;
888 } else {
889 shiftreg &= ~pin;
890 }
891
892 /* Clock the bits out. */
893 if (shiftreg != old) {
894 shiftreg_output(shiftreg);
895 }
896 }
897
898 static void set_led_extif(struct led_t *led)
899 {
900 volatile u8 *addr = (volatile u8 *) KSEG1ADDR(EXTIF_UART) + (led->gpio & ~GPIO_TYPE_MASK);
901 if (led->state)
902 *addr = 0xFF;
903 else
904 *addr;
905 }
906
907
908 static void led_flash(unsigned long dummy) {
909 struct led_t *l;
910 u32 mask = 0;
911 u8 extif_blink = 0;
912
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:
917 extif_blink = 1;
918 l->state = !l->state;
919 set_led_extif(l);
920 break;
921 case GPIO_TYPE_SHIFT:
922 extif_blink = 1;
923 l->state = !l->state;
924 set_led_shift(l);
925 break;
926 case GPIO_TYPE_NORMAL:
927 default:
928 mask |= l->gpio;
929 break;
930 }
931 }
932
933 mask &= ~gpiomask;
934 if (mask) {
935 u32 val = ~bcm47xx_gpio_in(~0);
936
937 bcm47xx_gpio_outen(mask, mask);
938 bcm47xx_gpio_control(mask, 0);
939 bcm47xx_gpio_out(mask, val);
940 }
941 if (mask || extif_blink) {
942 mod_timer(&led_timer, jiffies + FLASH_TIME);
943 }
944 }
945
946 static int diag_led_show(struct seq_file *m, void *v)
947 {
948 struct led_t * led = m->private;
949
950 u8 p = (led->polarity == NORMAL ? 0 : 1);
951 if (led->flash) {
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));
955 } else {
956 u32 in = (bcm47xx_gpio_in(~0) & led->gpio ? 1 : 0);
957 return seq_printf(m, "%d\n", ((in ^ p) ? 1 : 0));
958 }
959 }
960
961 static int diag_led_open(struct inode *inode, struct file *file)
962 {
963 return single_open(file, diag_led_show, PDE_DATA(inode));
964 }
965
966 static ssize_t diag_led_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
967 {
968 struct led_t *led = PDE_DATA(file_inode(file));
969 char cmd[5];
970 size_t len;
971 int p;
972
973 len = min(count, sizeof(cmd) - 1);
974 if (copy_from_user(cmd, buf, len))
975 return -EFAULT;
976
977 cmd[len] = 0;
978
979 p = (led->polarity == NORMAL ? 0 : 1);
980 if (cmd[0] == 'f') {
981 led->flash = 1;
982 led_flash(0);
983 } else {
984 led->flash = 0;
985 if ((led->gpio & GPIO_TYPE_MASK) == GPIO_TYPE_EXTIF) {
986 led->state = p ^ ((cmd[0] == '1') ? 1 : 0);
987 set_led_extif(led);
988 } else if ((led->gpio & GPIO_TYPE_MASK) == GPIO_TYPE_SHIFT) {
989 led->state = p ^ ((cmd[0] == '1') ? 1 : 0);
990 set_led_shift(led);
991 } else {
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));
995 }
996 }
997 return count;
998 }
999
1000 static const struct file_operations diag_led_fops = {
1001 .open = diag_led_open,
1002 .read = seq_read,
1003 .llseek = seq_lseek,
1004 .write = diag_led_write
1005 };
1006
1007 static void register_leds(struct led_t *l)
1008 {
1009 struct proc_dir_entry *p;
1010 u32 mask = 0;
1011 u32 oe_mask = 0;
1012 u32 val = 0;
1013
1014 leds = proc_mkdir("led", diag);
1015 if (!leds)
1016 return;
1017
1018 for(; l->name; l++) {
1019 if (l->gpio & gpiomask)
1020 continue;
1021
1022 switch (l->gpio & GPIO_TYPE_MASK) {
1023 case GPIO_TYPE_EXTIF:
1024 l->state = 0;
1025 set_led_extif(l);
1026 break;
1027 case GPIO_TYPE_SHIFT:
1028 mask |= (SHIFTREG_DATA | SHIFTREG_CLK);
1029 oe_mask |= (SHIFTREG_DATA | SHIFTREG_CLK);
1030 l->state = (l->polarity != NORMAL);
1031 set_led_shift(l);
1032 break;
1033 case GPIO_TYPE_NORMAL:
1034 default:
1035 if (l->polarity != INPUT) oe_mask |= l->gpio;
1036 mask |= l->gpio;
1037 val |= (l->polarity == NORMAL)?0:l->gpio;
1038 break;
1039 }
1040
1041 if (l->polarity == INPUT) continue;
1042
1043 p = proc_create_data(l->name, S_IRUSR, leds, &diag_led_fops, l);
1044 }
1045
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);
1050 }
1051
1052 static void unregister_leds(struct led_t *l)
1053 {
1054 for(; l->name; l++)
1055 remove_proc_entry(l->name, leds);
1056
1057 remove_proc_entry("led", diag);
1058 }
1059
1060 static int diag_model_show(struct seq_file *m, void *v)
1061 {
1062 return seq_printf(m, "%s\n", platform.name);
1063 }
1064
1065 static int diag_model_open(struct inode *inode, struct file *file)
1066 {
1067 return single_open(file, diag_model_show, PDE_DATA(inode));
1068 }
1069
1070 static const struct file_operations diag_model_fops = {
1071 .open = diag_model_open,
1072 .read = seq_read,
1073 .llseek = seq_lseek
1074 };
1075
1076 static int diag_gpiomask_show(struct seq_file *m, void *v)
1077 {
1078 return seq_printf(m, "0x%04x\n", gpiomask);
1079 }
1080
1081 static int diag_gpiomask_open(struct inode *inode, struct file *file)
1082 {
1083 return single_open(file, diag_gpiomask_show, PDE_DATA(inode));
1084 }
1085
1086 static ssize_t diag_gpiomask_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1087 {
1088 int err = kstrtouint_from_user(buf, count, 0, &gpiomask);
1089 if (err)
1090 return err;
1091
1092 if (platform.buttons) {
1093 unregister_buttons(platform.buttons);
1094 register_buttons(platform.buttons);
1095 }
1096
1097 if (platform.leds) {
1098 unregister_leds(platform.leds);
1099 register_leds(platform.leds);
1100 }
1101
1102 return count;
1103 }
1104
1105 static const struct file_operations diag_gpiomask_fops = {
1106 .open = diag_gpiomask_open,
1107 .read = seq_read,
1108 .llseek = seq_lseek,
1109 .write = diag_gpiomask_write
1110 };
1111
1112 static int __init diag_init(void)
1113 {
1114 static struct proc_dir_entry *p;
1115 static struct platform_t *detected;
1116
1117 detected = platform_detect();
1118 if (!detected) {
1119 printk(MODULE_NAME ": Router model not detected.\n");
1120 return -ENODEV;
1121 }
1122 memcpy(&platform, detected, sizeof(struct platform_t));
1123
1124 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1125 if (platform.platform_init != NULL) {
1126 platform.platform_init();
1127 }
1128
1129 if (!(diag = proc_mkdir("diag", NULL))) {
1130 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1131 return -EINVAL;
1132 }
1133
1134 p = proc_create("model", S_IRUSR, diag, &diag_model_fops);
1135 if (!p) {
1136 remove_proc_entry("diag", NULL);
1137 return -EINVAL;
1138 }
1139
1140 p = proc_create("gpiomask", S_IRUSR | S_IWUSR, diag, &diag_gpiomask_fops);
1141 if (!p) {
1142 remove_proc_entry("model", diag);
1143 remove_proc_entry("diag", NULL);
1144 return -EINVAL;
1145 }
1146
1147 if (platform.buttons)
1148 register_buttons(platform.buttons);
1149
1150 if (platform.leds)
1151 register_leds(platform.leds);
1152
1153 return 0;
1154 }
1155
1156 static void __exit diag_exit(void)
1157 {
1158 del_timer(&led_timer);
1159
1160 if (platform.buttons)
1161 unregister_buttons(platform.buttons);
1162
1163 if (platform.leds)
1164 unregister_leds(platform.leds);
1165
1166 remove_proc_entry("model", diag);
1167 remove_proc_entry("gpiomask", diag);
1168 remove_proc_entry("diag", NULL);
1169 }
1170
1171 module_init(diag_init);
1172 module_exit(diag_exit);
1173
1174 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1175 MODULE_LICENSE("GPL");