c0ef6ac5673fc1632f40beb4b8b7509ff01672de
[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 WRTSL54GS,
79 WRT54G3G,
80 WRT54G3GV2_VF,
81 WRT150NV1,
82 WRT150NV11,
83 WRT160NV1,
84 WRT160NV3,
85 WRT300NV11,
86 WRT350N,
87 WRT600N,
88 WRT600NV11,
89 WRT610N,
90 WRT610NV2,
91 E1000V1,
92 E1000V21,
93 E3000V1,
94 E3200V1,
95 E4200V1,
96
97 /* ASUS */
98 WLHDD,
99 WL300G,
100 WL320GE,
101 WL330GE,
102 WL500G,
103 WL500GD,
104 WL500GP,
105 WL500GPV2,
106 WL500W,
107 WL520GC,
108 WL520GU,
109 ASUS_4702,
110 WL700GE,
111 RTN12,
112 RTN16,
113 RTN66U,
114
115 /* Buffalo */
116 WBR2_G54,
117 WHR_G54S,
118 WHR_HP_G54,
119 WHR_G125,
120 WHR2_A54G54,
121 WLA2_G54L,
122 WZR_G300N,
123 WZR_RS_G54,
124 WZR_RS_G54HP,
125 BUFFALO_UNKNOWN_4710,
126
127 /* Siemens */
128 SE505V1,
129 SE505V2,
130
131 /* US Robotics */
132 USR5461,
133
134 /* Dell */
135 TM2300,
136 TM2300V2,
137
138 /* Motorola */
139 WE800G,
140 WR850GV1,
141 WR850GV2V3,
142 WR850GP,
143
144 /* Belkin */
145 BELKIN_UNKNOWN,
146 BELKIN_F7D4301,
147
148 /* Netgear */
149 WGT634U,
150 WNR834BV1,
151 WNR834BV2,
152 WNDR3400V1,
153 WNDR3700V3,
154
155 /* Trendware */
156 TEW411BRPP,
157
158 /* SimpleTech */
159 STI_NAS,
160
161 /* D-Link */
162 DIR130,
163 DIR320,
164 DIR330,
165 DWL3150,
166
167 /* Sitecom */
168 WL105B,
169
170 /* Western Digital */
171 WDNetCenter,
172
173 /* Askey */
174 RT210W,
175
176 /* OvisLink */
177 WL1600GL,
178
179 /* Microsoft */
180 MN700,
181
182 /* Edimax */
183 PS1208MFG,
184
185 /* Huawei */
186 HUAWEI_E970,
187 };
188
189 static void __init bcm4780_init(void) {
190 int pin = 1 << 3;
191
192 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
193 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
194 bcm47xx_gpio_outen(pin, pin);
195 bcm47xx_gpio_control(pin, 0);
196 bcm47xx_gpio_out(pin, pin);
197
198 /* Wait 5s, so the HDD can spin up */
199 set_current_state(TASK_INTERRUPTIBLE);
200 schedule_timeout(HZ * 5);
201 }
202
203 static void __init NetCenter_init(void) {
204 /* unset pin 6 (+12V) */
205 int pin = 1 << 6;
206 bcm47xx_gpio_outen(pin, pin);
207 bcm47xx_gpio_control(pin, 0);
208 bcm47xx_gpio_out(pin, pin);
209 /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
210 pin = 1 << 1;
211 bcm47xx_gpio_outen(pin, pin);
212 bcm47xx_gpio_control(pin, 0);
213 bcm47xx_gpio_out(pin, pin);
214 /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
215 bcm4780_init();
216 }
217
218 static void __init bcm57xx_init(void) {
219 int pin = 1 << 2;
220
221 /* FIXME: switch comes up, but port mappings/vlans not right */
222 bcm47xx_gpio_outen(pin, pin);
223 bcm47xx_gpio_control(pin, 0);
224 bcm47xx_gpio_out(pin, pin);
225 }
226
227 static struct platform_t __initdata platforms[] = {
228 /* Linksys */
229 [WAP54GV1] = {
230 .name = "Linksys WAP54G V1",
231 .buttons = {
232 { .name = "reset", .gpio = 1 << 0 },
233 },
234 .leds = {
235 { .name = "diag", .gpio = 1 << 3 },
236 { .name = "wlan", .gpio = 1 << 4 },
237 },
238 },
239 [WAP54GV2] = {
240 .name = "Linksys WAP54G V2",
241 .buttons = {
242 { .name = "reset", .gpio = 1 << 0 },
243 },
244 .leds = {
245 { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
246 /* GPIO 6 is b44 (eth0, LAN) PHY power */
247 },
248 },
249 [WAP54GV3] = {
250 .name = "Linksys WAP54G V3",
251 .buttons = {
252 /* FIXME: verify this */
253 { .name = "reset", .gpio = 1 << 7 },
254 { .name = "ses", .gpio = 1 << 0 },
255 },
256 .leds = {
257 /* FIXME: diag? */
258 { .name = "ses", .gpio = 1 << 1 },
259 },
260 },
261 [WRT54GV1] = {
262 .name = "Linksys WRT54G V1.x",
263 .buttons = {
264 { .name = "reset", .gpio = 1 << 6 },
265 },
266 .leds = {
267 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
268 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
269 },
270 },
271 [WRT54G] = {
272 .name = "Linksys WRT54G/GS/GL",
273 .buttons = {
274 { .name = "reset", .gpio = 1 << 6 },
275 { .name = "ses", .gpio = 1 << 4 },
276 },
277 .leds = {
278 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
279 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
280 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
281 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
282 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
283 },
284 },
285 [WRTSL54GS] = {
286 .name = "Linksys WRTSL54GS",
287 .buttons = {
288 { .name = "reset", .gpio = 1 << 6 },
289 { .name = "ses", .gpio = 1 << 4 },
290 },
291 .leds = {
292 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
293 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
294 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
295 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
296 },
297 },
298 [WRT54G3G] = {
299 .name = "Linksys WRT54G3G",
300 .buttons = {
301 { .name = "reset", .gpio = 1 << 6 },
302 { .name = "3g", .gpio = 1 << 4 },
303 },
304 .leds = {
305 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
306 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
307 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
308 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
309 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
310 },
311 },
312 [WRT54G3GV2_VF] = {
313 .name = "Linksys WRT54G3GV2-VF",
314 .buttons = {
315 { .name = "reset", .gpio = 1 << 6 },
316 { .name = "3g", .gpio = 1 << 5 },
317 },
318 .leds = {
319 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
320 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
321 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
322 },
323 },
324 [WRT150NV1] = {
325 .name = "Linksys WRT150N V1",
326 .buttons = {
327 { .name = "reset", .gpio = 1 << 6 },
328 { .name = "ses", .gpio = 1 << 4 },
329 },
330 .leds = {
331 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
332 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE },
333 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
334 },
335 },
336 [WRT150NV11] = {
337 .name = "Linksys WRT150N V1.1",
338 .buttons = {
339 { .name = "reset", .gpio = 1 << 6 },
340 { .name = "ses", .gpio = 1 << 4 },
341 },
342 .leds = {
343 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
344 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE },
345 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
346 },
347 },
348 [WRT160NV1] = {
349 .name = "Linksys WRT160N v1.x",
350 .buttons = {
351 { .name = "reset", .gpio = 1 << 6 },
352 { .name = "ses", .gpio = 1 << 4 },
353 },
354 .leds = {
355 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
356 { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
357 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
358 },
359 },
360 [WRT160NV3] = {
361 .name = "Linksys WRT160N V3",
362 .buttons = {
363 { .name = "reset", .gpio = 1 << 6 },
364 { .name = "ses", .gpio = 1 << 5 },
365 },
366 .leds = {
367 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
368 { .name = "ses_blue", .gpio = 1 << 4, .polarity = REVERSE },
369 { .name = "ses_orange", .gpio = 1 << 2, .polarity = REVERSE },
370 },
371 },
372 [WRT300NV11] = {
373 .name = "Linksys WRT300N V1.1",
374 .buttons = {
375 { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
376 { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
377 },
378 .leds = {
379 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
380 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
381 { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
382 },
383 .platform_init = bcm57xx_init,
384 },
385 [WRT350N] = {
386 .name = "Linksys WRT350N",
387 .buttons = {
388 { .name = "reset", .gpio = 1 << 6 },
389 { .name = "ses", .gpio = 1 << 8 },
390 },
391 .leds = {
392 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
393 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
394 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
395 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
396 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
397 },
398 .platform_init = bcm57xx_init,
399 },
400 [WRT600N] = {
401 .name = "Linksys WRT600N",
402 .buttons = {
403 { .name = "reset", .gpio = 1 << 6 },
404 { .name = "ses", .gpio = 1 << 7 },
405 },
406 .leds = {
407 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
408 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
409 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
410 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
411 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
412 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
413 },
414 .platform_init = bcm57xx_init,
415 },
416 [WRT600NV11] = {
417 .name = "Linksys WRT600N V1.1",
418 .buttons = {
419 { .name = "reset", .gpio = 1 << 6 },
420 { .name = "ses", .gpio = 1 << 7 },
421 },
422 .leds = {
423 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
424 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
425 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
426 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
427 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
428 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
429 },
430 .platform_init = bcm57xx_init,
431 },
432 [WRT610N] = {
433 .name = "Linksys WRT610N",
434 .buttons = {
435 { .name = "reset", .gpio = 1 << 6 },
436 { .name = "ses", .gpio = 1 << 8 },
437 },
438 .leds = {
439 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
440 { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
441 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
442 { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
443 },
444 },
445 [WRT610NV2] = {
446 .name = "Linksys WRT610N V2",
447 .buttons = {
448 { .name = "reset", .gpio = 1 << 6 },
449 { .name = "ses", .gpio = 1 << 4 },
450 },
451 .leds = {
452 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL }, // Power LED
453 { .name = "usb", .gpio = 1 << 7, .polarity = NORMAL }, // USB LED
454 { .name = "ses_amber", .gpio = 1 << 0, .polarity = REVERSE }, // WiFi protected setup LED amber
455 { .name = "ses_blue", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED blue
456 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL }, // Wireless LED
457 },
458 },
459 /* same hardware as WRT160NV3 and Cisco Valet M10V1, but different board detection, combine? */
460 [E1000V1] = {
461 .name = "Linksys E1000 V1",
462 .buttons = {
463 { .name = "reset", .gpio = 1 << 6 },
464 { .name = "wps", .gpio = 1 << 5 }, /* nvram get gpio5=wps_button */
465 },
466 .leds = {
467 /** turns on leds for all ethernet ports (wan too)
468 * this also disconnects some, or maybe all, ethernet ports
469 * 1: leds work normally
470 * 0: all lit all the time */
471 /* nvram get gpio3=robo_reset */
472 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
473 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
474 { .name = "ses_blue", .gpio = 1 << 4, .polarity = REVERSE }, /* nvram get gpio4=wps_led */
475 { .name = "ses_orange", .gpio = 1 << 2, .polarity = REVERSE }, /* nvram get gpio2=wps_status_led */
476 },
477 },
478 [E1000V21] = {
479 .name = "Linksys E1000 V2.1",
480 .buttons = {
481 { .name = "reset", .gpio = 1 << 10 }, /* nvram get reset_gpio=10 */
482 { .name = "wps", .gpio = 1 << 9 }, /* nvram get gpio9=wps_button */
483 },
484 .leds = {
485 { .name = "power", .gpio = 1 << 6, .polarity = REVERSE },
486 { .name = "wlan", .gpio = 1 << 5, .polarity = NORMAL },
487 { .name = "ses_blue", .gpio = 1 << 8, .polarity = NORMAL }, /* nvram get gpio8=wps_led */
488 { .name = "ses_orange", .gpio = 1 << 7, .polarity = NORMAL }, /* nvram get gpio7=wps_status_led */
489 },
490 },
491 [E3000V1] = {
492 .name = "Linksys E3000 V1",
493 .buttons = {
494 { .name = "reset", .gpio = 1 << 6 },
495 { .name = "ses", .gpio = 1 << 4 },
496 },
497 .leds = {
498 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL }, // Power LED
499 { .name = "usb", .gpio = 1 << 7, .polarity = NORMAL }, // USB LED
500 { .name = "ses_amber", .gpio = 1 << 0, .polarity = REVERSE }, // WiFi protected setup LED amber
501 { .name = "ses_blue", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED blue
502 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL }, // Wireless LED
503 },
504 },
505 [E3200V1] = {
506 .name = "Linksys E3200 V1",
507 .buttons = {
508 /* { .name = "switch", .gpio = 1 << 4 },*/ /* nvram get gpio4=robo_reset */
509 { .name = "reset", .gpio = 1 << 5 }, /* nvram get reset_gpio=5 */
510 { .name = "wps", .gpio = 1 << 8 }, /* nvram get gpio8=wps_button */
511 /* { .name = "wombo", .gpio = 1 << 23 },*/ /* nvram get gpio23=wombo_reset - wireless on motherboard */
512 },
513 .leds = {
514 { .name = "power", .gpio = 1 << 3, .polarity = REVERSE }, /* Power LED */
515 },
516 },
517 [E4200V1] = {
518 .name = "Linksys E4200 V1",
519 .buttons = {
520 { .name = "reset", .gpio = 1 << 6 },
521 { .name = "wps", .gpio = 1 << 4 },
522 },
523 .leds = {
524 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
525 },
526 },
527 /* Asus */
528 [WLHDD] = {
529 .name = "ASUS WL-HDD",
530 .buttons = {
531 { .name = "reset", .gpio = 1 << 6 },
532 },
533 .leds = {
534 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
535 { .name = "usb", .gpio = 1 << 2, .polarity = REVERSE },
536 },
537 },
538 [WL300G] = {
539 .name = "ASUS WL-300g",
540 .buttons = {
541 { .name = "reset", .gpio = 1 << 6 },
542 },
543 .leds = {
544 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
545 },
546 },
547 [WL320GE] = {
548 .name = "ASUS WL-320gE/WL-320gP",
549 .buttons = {
550 { .name = "reset", .gpio = 1 << 6 },
551 },
552 .leds = {
553 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
554 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
555 { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
556 },
557 },
558 [WL330GE] = {
559 .name = "ASUS WL-330gE",
560 .buttons = {
561 { .name = "reset", .gpio = 1 << 2 },
562 },
563 .leds = {
564 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
565 },
566 },
567 [WL500G] = {
568 .name = "ASUS WL-500g",
569 .buttons = {
570 { .name = "reset", .gpio = 1 << 6 },
571 },
572 .leds = {
573 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
574 },
575 },
576 [WL500GD] = {
577 .name = "ASUS WL-500g Deluxe",
578 .buttons = {
579 { .name = "reset", .gpio = 1 << 6 },
580 },
581 .leds = {
582 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
583 },
584 },
585 [WL500GP] = {
586 .name = "ASUS WL-500g Premium",
587 .buttons = {
588 { .name = "reset", .gpio = 1 << 0 },
589 { .name = "ses", .gpio = 1 << 4 },
590 },
591 .leds = {
592 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
593 },
594 },
595 [WL500GPV2] = {
596 .name = "ASUS WL-500g Premium V2",
597 .buttons = {
598 { .name = "reset", .gpio = 1 << 2 },
599 { .name = "ses", .gpio = 1 << 3 },
600 },
601 .leds = {
602 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
603 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
604 },
605 },
606 [WL500W] = {
607 .name = "ASUS WL-500W",
608 .buttons = {
609 { .name = "reset", .gpio = 1 << 6 },
610 { .name = "ses", .gpio = 1 << 7 },
611 },
612 .leds = {
613 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
614 },
615 },
616 [WL520GC] = {
617 .name = "ASUS WL-520GC",
618 .buttons = {
619 { .name = "reset", .gpio = 1 << 2 },
620 { .name = "ses", .gpio = 1 << 3 },
621 },
622 .leds = {
623 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
624 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
625 },
626 },
627 [WL520GU] = {
628 .name = "ASUS WL-520gU",
629 .buttons = {
630 { .name = "reset", .gpio = 1 << 2 },
631 { .name = "ses", .gpio = 1 << 3 },
632 },
633 .leds = {
634 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
635 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
636 },
637 },
638 [ASUS_4702] = {
639 .name = "ASUS (unknown, BCM4702)",
640 .buttons = {
641 { .name = "reset", .gpio = 1 << 6 },
642 },
643 .leds = {
644 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
645 },
646 },
647 [WL700GE] = {
648 .name = "ASUS WL-700gE",
649 .buttons = {
650 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
651 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
652 { .name = "power", .gpio = 1 << 0 }, // on front
653 { .name = "copy", .gpio = 1 << 6 }, // on front
654 },
655 .leds = {
656 #if 0
657 // GPIO that controls power led also enables/disables some essential functions
658 // - power to HDD
659 // - switch leds
660 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
661 #endif
662 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
663 },
664 .platform_init = bcm4780_init,
665 },
666 [RTN12] = {
667 .name = "ASUS RT-N12",
668 .buttons = {
669 { .name = "wps", .gpio = 1 << 0 },
670 { .name = "reset", .gpio = 1 << 1 },
671 // this is the router/repeater/ap switch
672 { .name = "sw1", .gpio = 1 << 4 },
673 { .name = "sw2", .gpio = 1 << 5 },
674 { .name = "sw3", .gpio = 1 << 6 },
675 },
676 .leds = {
677 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
678 { .name = "wlan", .gpio = 1 << 7, .polarity = NORMAL },
679 // gpio3 forces WAN and LAN1-4 all on
680 //{ .name = "eth", .gpio = 1 << 3, .polarity = REVERSE },
681 },
682 },
683 [RTN16] = {
684 .name = "ASUS RT-N16",
685 .buttons = {
686 { .name = "reset", .gpio = 1 << 8 },
687 { .name = "ses", .gpio = 1 << 5 },
688 },
689 .leds = {
690 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
691 { .name = "wlan", .gpio = 1 << 7, .polarity = NORMAL },
692 },
693 },
694 [RTN66U] = {
695 .name = "ASUS RT-N66U",
696 .buttons = {
697 { .name = "reset", .gpio = 1 << 9 },
698 { .name = "wps", .gpio = 1 << 4 },
699 },
700 .leds = {
701 { .name = "power", .gpio = 1 << 12, .polarity = REVERSE },
702 { .name = "usb", .gpio = 1 << 15, .polarity = REVERSE },
703 },
704 },
705 /* Buffalo */
706 [WHR_G54S] = {
707 .name = "Buffalo WHR-G54S",
708 .buttons = {
709 { .name = "reset", .gpio = 1 << 4 },
710 { .name = "bridge", .gpio = 1 << 5 },
711 { .name = "ses", .gpio = 1 << 0 },
712 },
713 .leds = {
714 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
715 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
716 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
717 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
718 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
719 },
720 },
721 [WBR2_G54] = {
722 .name = "Buffalo WBR2-G54",
723 /* FIXME: verify */
724 .buttons = {
725 { .name = "reset", .gpio = 1 << 7 },
726 },
727 .leds = {
728 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
729 },
730 },
731 [WHR_HP_G54] = {
732 .name = "Buffalo WHR-HP-G54",
733 .buttons = {
734 { .name = "reset", .gpio = 1 << 4 },
735 { .name = "bridge", .gpio = 1 << 5 },
736 { .name = "ses", .gpio = 1 << 0 },
737 },
738 .leds = {
739 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
740 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
741 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
742 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
743 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
744 },
745 },
746 [WHR_G125] = {
747 .name = "Buffalo WHR-G125",
748 .buttons = {
749 { .name = "reset", .gpio = 1 << 4 },
750 { .name = "bridge", .gpio = 1 << 5 },
751 { .name = "ses", .gpio = 1 << 0 },
752 },
753 .leds = {
754 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
755 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
756 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
757 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
758 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
759 },
760 },
761 [WHR2_A54G54] = {
762 .name = "Buffalo WHR2-A54G54",
763 .buttons = {
764 { .name = "reset", .gpio = 1 << 4 },
765 },
766 .leds = {
767 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
768 },
769 },
770 [WLA2_G54L] = {
771 .name = "Buffalo WLA2-G54L",
772 /* FIXME: verify */
773 .buttons = {
774 { .name = "reset", .gpio = 1 << 7 },
775 },
776 .leds = {
777 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
778 },
779 },
780 [WZR_G300N] = {
781 .name = "Buffalo WZR-G300N",
782 .buttons = {
783 { .name = "reset", .gpio = 1 << 4 },
784 },
785 .leds = {
786 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
787 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
788 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
789 },
790 },
791 [WZR_RS_G54] = {
792 .name = "Buffalo WZR-RS-G54",
793 .buttons = {
794 { .name = "ses", .gpio = 1 << 0 },
795 { .name = "reset", .gpio = 1 << 4 },
796 },
797 .leds = {
798 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
799 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
800 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
801 },
802 },
803 [WZR_RS_G54HP] = {
804 .name = "Buffalo WZR-RS-G54HP",
805 .buttons = {
806 { .name = "ses", .gpio = 1 << 0 },
807 { .name = "reset", .gpio = 1 << 4 },
808 },
809 .leds = {
810 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
811 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
812 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
813 },
814 },
815 [BUFFALO_UNKNOWN_4710] = {
816 .name = "Buffalo (unknown, BCM4710)",
817 .buttons = {
818 { .name = "reset", .gpio = 1 << 4 },
819 },
820 .leds = {
821 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
822 },
823 },
824 /* Siemens */
825 [SE505V1] = {
826 .name = "Siemens SE505 V1",
827 .buttons = {
828 /* No usable buttons */
829 },
830 .leds = {
831 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
832 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
833 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
834 },
835 },
836 [SE505V2] = {
837 .name = "Siemens SE505 V2",
838 .buttons = {
839 /* No usable buttons */
840 },
841 .leds = {
842 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
843 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
844 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
845 },
846 },
847 /* US Robotics */
848 [USR5461] = {
849 .name = "U.S. Robotics USR5461",
850 .buttons = {
851 /* No usable buttons */
852 },
853 .leds = {
854 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
855 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
856 },
857 },
858 /* Dell */
859 [TM2300] = {
860 .name = "Dell TrueMobile 2300",
861 .buttons = {
862 { .name = "reset", .gpio = 1 << 0 },
863 },
864 .leds = {
865 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
866 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
867 },
868 },
869 [TM2300V2] = {
870 .name = "Dell TrueMobile 2300 v2",
871 .buttons = {
872 { .name = "reset", .gpio = 1 << 0 },
873 },
874 .leds = {
875 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
876 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
877 },
878 },
879 /* Motorola */
880 [WE800G] = {
881 .name = "Motorola WE800G",
882 .buttons = {
883 { .name = "reset", .gpio = 1 << 0 },
884 },
885 .leds = {
886 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
887 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
888 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
889 },
890 },
891 [WR850GV1] = {
892 .name = "Motorola WR850G V1",
893 .buttons = {
894 { .name = "reset", .gpio = 1 << 0 },
895 },
896 .leds = {
897 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
898 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
899 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
900 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
901 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
902 },
903 },
904 [WR850GV2V3] = {
905 .name = "Motorola WR850G V2/V3",
906 .buttons = {
907 { .name = "reset", .gpio = 1 << 5 },
908 },
909 .leds = {
910 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
911 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
912 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
913 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
914 },
915 },
916 [WR850GP] = {
917 .name = "Motorola WR850GP",
918 .buttons = {
919 { .name = "reset", .gpio = 1 << 5 },
920 },
921 .leds = {
922 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
923 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
924 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
925 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
926 },
927 },
928
929 /* Belkin */
930 [BELKIN_UNKNOWN] = {
931 .name = "Belkin (unknown)",
932 /* FIXME: verify & add detection */
933 .buttons = {
934 { .name = "reset", .gpio = 1 << 7 },
935 },
936 .leds = {
937 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
938 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
939 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
940 },
941 },
942 [BELKIN_F7D4301] = {
943 .name = "Belkin PlayMax F7D4301",
944 .buttons = {
945 { .name = "reset", .gpio = 1 << 6 },
946 { .name = "wps", .gpio = 1 << 8 },
947 },
948 .leds = {
949 { .name = "power", .gpio = 1 << 11, .polarity = REVERSE },
950 { .name = "wlan", .gpio = 1 << 13, .polarity = REVERSE },
951 { .name = "led0", .gpio = 1 << 14, .polarity = REVERSE },
952 { .name = "led1", .gpio = 1 << 15, .polarity = REVERSE },
953 },
954 },
955 /* Netgear */
956 [WGT634U] = {
957 .name = "Netgear WGT634U",
958 .buttons = {
959 { .name = "reset", .gpio = 1 << 2 },
960 },
961 .leds = {
962 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
963 },
964 },
965 /* Netgear */
966 [WNR834BV1] = {
967 .name = "Netgear WNR834B V1",
968 .buttons = { /* TODO: add reset button and confirm LEDs - GPIO from dd-wrt */ },
969 .leds = {
970 { .name = "power", .gpio = 1 << 4, .polarity = REVERSE },
971 { .name = "diag", .gpio = 1 << 5, .polarity = REVERSE },
972 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
973 },
974 },
975 /* Netgear */
976 [WNR834BV2] = {
977 .name = "Netgear WNR834B V2",
978 .buttons = {
979 { .name = "reset", .gpio = 1 << 6 },
980 },
981 .leds = {
982 { .name = "power", .gpio = 1 << 2, .polarity = NORMAL },
983 { .name = "diag", .gpio = 1 << 3, .polarity = NORMAL },
984 { .name = "connected", .gpio = 1 << 7, .polarity = NORMAL },
985 },
986 },
987 [WNDR3400V1] = {
988 .name = "Netgear WNDR3400 V1",
989 .buttons = {
990 /* nvram get gpio5=robo_reset */
991 { .name = "reset", .gpio = 1 << 4 },
992 { .name = "wps", .gpio = 1 << 6 },
993 { .name = "wlan", .gpio = 1 << 8 },
994 },
995 .leds = {
996 { .name = "wlan", .gpio = 0 << 0, .polarity = NORMAL },
997 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
998 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
999 { .name = "diag", .gpio = 1 << 7, .polarity = NORMAL },
1000 { .name = "usb", .gpio = 1 << 2, .polarity = REVERSE },
1001 },
1002 },
1003 [WNDR3700V3] = {
1004 .name = "Netgear WNDR3700 V3",
1005 .buttons = {
1006 /* { .name = "usb", .gpio = 1 << 1 }, */ /* this button doesn't seem to exist. */
1007 { .name = "wlan", .gpio = 1 << 2 },
1008 { .name = "reset", .gpio = 1 << 3 },
1009 { .name = "wps", .gpio = 1 << 4 },
1010 /* { .name = "switch", .gpio = 1 << 5 },*/ /* nvram get gpio5=robo_reset */
1011 },
1012 .leds = {
1013 { .name = "power", .gpio = (1 << 0) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1014 { .name = "diag", .gpio = (1 << 1) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1015 /* WAN LED doesn't respond to GPIO control. The switch is probably driving it.
1016 * { .name = "wan", .gpio = (1 << 2) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1017 */
1018 { .name = "wlan2g", .gpio = (1 << 3) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1019 { .name = "wlan5g", .gpio = (1 << 4) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1020 { .name = "usb", .gpio = (1 << 5) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1021 { .name = "wps", .gpio = (1 << 6) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1022 { .name = "wlan", .gpio = (1 << 7) | GPIO_TYPE_SHIFT, .polarity = REVERSE },
1023 },
1024 },
1025 /* Trendware */
1026 [TEW411BRPP] = {
1027 .name = "Trendware TEW411BRP+",
1028 .buttons = {
1029 { /* No usable buttons */ },
1030 },
1031 .leds = {
1032 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
1033 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
1034 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
1035 },
1036 },
1037 /* SimpleTech */
1038 [STI_NAS] = {
1039 .name = "SimpleTech SimpleShare NAS",
1040 .buttons = {
1041 { .name = "reset", .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
1042 },
1043 .leds = {
1044 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
1045 },
1046 .platform_init = bcm4780_init,
1047 },
1048 /* D-Link */
1049 [DIR130] = {
1050 .name = "D-Link DIR-130",
1051 .buttons = {
1052 { .name = "reset", .gpio = 1 << 3},
1053 { .name = "reserved", .gpio = 1 << 7},
1054 },
1055 .leds = {
1056 { .name = "diag", .gpio = 1 << 0},
1057 { .name = "blue", .gpio = 1 << 6},
1058 },
1059 },
1060 [DIR320] = {
1061 .name = "D-Link DIR-320",
1062 .buttons = {
1063 { .name = "reserved", .gpio = 1 << 6},
1064 { .name = "reset", .gpio = 1 << 7},
1065 },
1066 .leds = {
1067 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
1068 { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
1069 { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
1070 { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
1071 { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
1072 },
1073 },
1074 [DIR330] = {
1075 .name = "D-Link DIR-330",
1076 .buttons = {
1077 { .name = "reset", .gpio = 1 << 3},
1078 { .name = "reserved", .gpio = 1 << 7},
1079 },
1080 .leds = {
1081 { .name = "diag", .gpio = 1 << 0},
1082 { .name = "usb", .gpio = 1 << 4},
1083 { .name = "blue", .gpio = 1 << 6},
1084 },
1085 },
1086 [DWL3150] = {
1087 .name = "D-Link DWL-3150",
1088 .buttons = {
1089 { .name = "reset", .gpio = 1 << 7},
1090 },
1091 .leds = {
1092 { .name = "diag", .gpio = 1 << 2},
1093 { .name = "status", .gpio = 1 << 1},
1094 },
1095 },
1096 /* Double check */
1097 [WL105B] = {
1098 .name = "Sitecom WL-105b",
1099 .buttons = {
1100 { .name = "reset", .gpio = 1 << 10},
1101 },
1102 .leds = {
1103 { .name = "wlan", .gpio = 1 << 4},
1104 { .name = "power", .gpio = 1 << 3},
1105 },
1106 },
1107 /* Western Digital Net Center */
1108 [WDNetCenter] = {
1109 .name = "Western Digital NetCenter",
1110 .buttons = {
1111 { .name = "power", .gpio = 1 << 0},
1112 { .name = "reset", .gpio = 1 << 7},
1113 },
1114 .platform_init = NetCenter_init,
1115 },
1116 /* Askey (and clones) */
1117 [RT210W] = {
1118 .name = "Askey RT210W",
1119 .buttons = {
1120 /* Power button is hard-wired to hardware reset */
1121 /* but is also connected to GPIO 7 (probably for bootloader recovery) */
1122 { .name = "power", .gpio = 1 << 7},
1123 },
1124 .leds = {
1125 /* These were verified and named based on Belkin F5D4230-4 v1112 */
1126 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
1127 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
1128 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
1129 },
1130 },
1131 [WL1600GL] = {
1132 .name = "OvisLink WL-1600GL",
1133 .buttons = {
1134 { .name = "reset", .gpio = 1 << 3 },
1135 { .name = "ses", .gpio = 1 << 4 },
1136 },
1137 .leds = {
1138 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
1139 { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
1140 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
1141 { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
1142 },
1143 },
1144 /* Microsoft */
1145 [MN700] = {
1146 .name = "Microsoft MN-700",
1147 .buttons = {
1148 { .name = "reset", .gpio = 1 << 7 },
1149 },
1150 .leds = {
1151 { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
1152 },
1153 },
1154 /* Edimax */
1155 [PS1208MFG] = {
1156 .name = "Edimax PS-1208MFG",
1157 .buttons = {
1158 { .name = "reset", .gpio = 1 << 4 },
1159 },
1160 .leds = {
1161 { .name = "status", .gpio = 1 << 1, .polarity = NORMAL },
1162 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
1163 },
1164 },
1165 /* Huawei */
1166 [HUAWEI_E970] = {
1167 .name = "Huawei E970",
1168 .buttons = {
1169 { .name = "reset", .gpio = 1 << 6 },
1170 },
1171 .leds = {
1172 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
1173 },
1174 },
1175 };
1176
1177 static struct platform_t __init *platform_detect_legacy(void)
1178 {
1179 char *boardnum, *boardtype;
1180
1181 if (strcmp(getvar("nvram_type"), "cfe") == 0)
1182 return &platforms[WGT634U];
1183
1184
1185 /* no easy model number, attempt to guess */
1186 boardnum = getvar("boardnum");
1187 boardtype = getvar("boardtype");
1188
1189 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
1190 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
1191 return &platforms[WRT600NV11];
1192
1193 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
1194 return &platforms[WRT600N];
1195 }
1196
1197 if (startswith(getvar("pmon_ver"), "CFE")) {
1198 /* CFE based - newer hardware */
1199 if (!strcmp(boardnum, "42")) { /* Linksys */
1200 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
1201 return &platforms[WRT350N];
1202
1203 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
1204 return &platforms[WRT54G3G];
1205
1206 /* default to WRT54G if no boot_hw_model is set */
1207 if (nvram_get("boot_hw_model") == NULL)
1208 return &platforms[WRT54G];
1209 }
1210 if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
1211 return &platforms[WAP54GV2];
1212
1213 if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
1214 return &platforms[WL1600GL];
1215
1216
1217 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1218 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1219 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1220 }
1221
1222 if (!strcmp(boardnum, "10496"))
1223 return &platforms[USR5461];
1224
1225 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1226 return &platforms[SE505V2];
1227
1228 if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1229 !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1230 return &platforms[DIR320];
1231
1232 if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1233 return &platforms[WDNetCenter];
1234 }
1235
1236 if (!strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B V1 and V2*/
1237 if (!strcmp(boardnum, "08") || !strcmp(boardnum, "8"))
1238 return &platforms[WNR834BV1];
1239 if (!strcmp(boardnum, "01") || !strcmp(boardnum, "1"))
1240 return &platforms[WNR834BV2];
1241 }
1242
1243 } else { /* PMON based - old stuff */
1244 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1245 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1246 return &platforms[WR850GV1];
1247 }
1248 if (startswith(boardtype, "bcm94710dev")) {
1249 if (!strcmp(boardnum, "42"))
1250 return &platforms[WRT54GV1];
1251 if (simple_strtoul(boardnum, NULL, 0) == 2)
1252 return &platforms[WAP54GV1];
1253 }
1254 /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1255 if (startswith(getvar("hardware_version"), "WL500-")) {
1256 if (!strcmp(getvar("boardnum"), "mn700"))
1257 return &platforms[MN700];
1258 else
1259 return &platforms[WL500G];
1260 }
1261 /* Sitecom WL-105b */
1262 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1263 return &platforms[WL105B];
1264
1265 /* unknown asus stuff, probably bcm4702 */
1266 if (startswith(boardnum, "asusX"))
1267 return &platforms[ASUS_4702];
1268
1269 /* bcm4702 based Askey RT210W clones, Including:
1270 * Askey RT210W (duh?)
1271 * Siemens SE505v1
1272 * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1273 */
1274 if (!strcmp(boardtype,"bcm94710r4")
1275 && !strcmp(boardnum ,"100")
1276 && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1277 ){
1278 return &platforms[RT210W];
1279 }
1280 }
1281
1282 if (boardnum || !strcmp(boardnum, "00")) {/* probably buffalo */
1283 if (startswith(boardtype, "bcm94710ap"))
1284 return &platforms[BUFFALO_UNKNOWN_4710];
1285 }
1286
1287 if (startswith(getvar("CFEver"), "MotoWRv2") ||
1288 startswith(getvar("CFEver"), "MotoWRv3") ||
1289 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1290
1291 return &platforms[WR850GV2V3];
1292 }
1293
1294 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1295 return &platforms[TEW411BRPP];
1296 }
1297
1298 if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1299 return &platforms[STI_NAS];
1300
1301 if (!strcmp(boardnum, "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1302 return &platforms[DWL3150];
1303
1304 if (!strcmp(boardnum, "01") && !strcmp(boardtype, "0x048e") && /* Edimax PS1208MFG */
1305 !strcmp(getvar("status_gpio"), "1")) /* gpio based detection */
1306 return &platforms[PS1208MFG];
1307
1308 /* not found */
1309 return NULL;
1310 }
1311
1312 static struct platform_t __init *platform_detect(void)
1313 {
1314 enum bcm47xx_board board;
1315 const char *board_name;
1316
1317
1318 board = bcm47xx_board_get();
1319 board_name = bcm47xx_board_get_name();
1320 if (board != BCM47XX_BOARD_UNKNOWN && board != BCM47XX_BOARD_NON)
1321 printk(MODULE_NAME ": kernel found a \"%s\"\n", board_name);
1322
1323 switch(board) {
1324 case BCM47XX_BOARD_ASUS_RTN12:
1325 return &platforms[RTN12];
1326 case BCM47XX_BOARD_ASUS_RTN16:
1327 return &platforms[RTN16];
1328 case BCM47XX_BOARD_ASUS_RTN66U:
1329 return &platforms[RTN66U];
1330 case BCM47XX_BOARD_ASUS_WL300G:
1331 return &platforms[WL300G];
1332 case BCM47XX_BOARD_ASUS_WL320GE:
1333 return &platforms[WL320GE];
1334 case BCM47XX_BOARD_ASUS_WL330GE:
1335 return &platforms[WL330GE];
1336 case BCM47XX_BOARD_ASUS_WL500GD:
1337 return &platforms[WL500GD];
1338 case BCM47XX_BOARD_ASUS_WL500GPV1:
1339 return &platforms[WL500GP];
1340 case BCM47XX_BOARD_ASUS_WL500GPV2:
1341 return &platforms[WL500GPV2];
1342 case BCM47XX_BOARD_ASUS_WL500W:
1343 return &platforms[WL500W];
1344 case BCM47XX_BOARD_ASUS_WL520GC:
1345 return &platforms[WL520GC];
1346 case BCM47XX_BOARD_ASUS_WL520GU:
1347 return &platforms[WL520GU];
1348 case BCM47XX_BOARD_ASUS_WL700GE:
1349 return &platforms[WL700GE];
1350 case BCM47XX_BOARD_ASUS_WLHDD:
1351 return &platforms[WLHDD];
1352 case BCM47XX_BOARD_BELKIN_F7D4301:
1353 return &platforms[BELKIN_F7D4301];
1354 case BCM47XX_BOARD_BUFFALO_WBR2_G54:
1355 return &platforms[WBR2_G54];
1356 case BCM47XX_BOARD_BUFFALO_WHR2_A54G54:
1357 return &platforms[WHR2_A54G54];
1358 case BCM47XX_BOARD_BUFFALO_WHR_G125:
1359 return &platforms[WHR_G125];
1360 case BCM47XX_BOARD_BUFFALO_WHR_G54S:
1361 return &platforms[WHR_G54S];
1362 case BCM47XX_BOARD_BUFFALO_WHR_HP_G54:
1363 return &platforms[WHR_HP_G54];
1364 case BCM47XX_BOARD_BUFFALO_WLA2_G54L:
1365 return &platforms[WLA2_G54L];
1366 case BCM47XX_BOARD_BUFFALO_WZR_G300N:
1367 return &platforms[WZR_G300N];
1368 case BCM47XX_BOARD_BUFFALO_WZR_RS_G54:
1369 return &platforms[WZR_RS_G54];
1370 case BCM47XX_BOARD_BUFFALO_WZR_RS_G54HP:
1371 return &platforms[WZR_RS_G54HP];
1372 case BCM47XX_BOARD_DELL_TM2300:
1373 return &platforms[TM2300];
1374 case BCM47XX_BOARD_DLINK_DIR130:
1375 return &platforms[DIR130];
1376 case BCM47XX_BOARD_DLINK_DIR330:
1377 return &platforms[DIR330];
1378 case BCM47XX_BOARD_HUAWEI_E970:
1379 return &platforms[HUAWEI_E970];
1380 case BCM47XX_BOARD_LINKSYS_E1000V1:
1381 return &platforms[E1000V1];
1382 case BCM47XX_BOARD_LINKSYS_E1000V21:
1383 return &platforms[E1000V21];
1384 case BCM47XX_BOARD_LINKSYS_E3000V1:
1385 return &platforms[E3000V1];
1386 case BCM47XX_BOARD_LINKSYS_E3200V1:
1387 return &platforms[E3200V1];
1388 case BCM47XX_BOARD_LINKSYS_E4200V1:
1389 return &platforms[E4200V1];
1390 case BCM47XX_BOARD_LINKSYS_WRT150NV1:
1391 return &platforms[WRT150NV1];
1392 case BCM47XX_BOARD_LINKSYS_WRT150NV11:
1393 return &platforms[WRT150NV11];
1394 case BCM47XX_BOARD_LINKSYS_WRT160NV1:
1395 return &platforms[WRT160NV1];
1396 case BCM47XX_BOARD_LINKSYS_WRT160NV3:
1397 return &platforms[WRT160NV3];
1398 case BCM47XX_BOARD_LINKSYS_WRT300NV11:
1399 return &platforms[WRT300NV11];
1400 case BCM47XX_BOARD_LINKSYS_WRT54G3GV2:
1401 return &platforms[WRT54G3GV2_VF];
1402 case BCM47XX_BOARD_LINKSYS_WRT610NV1:
1403 return &platforms[WRT610N];
1404 case BCM47XX_BOARD_LINKSYS_WRT610NV2:
1405 return &platforms[WRT610NV2];
1406 case BCM47XX_BOARD_LINKSYS_WRTSL54GS:
1407 return &platforms[WRTSL54GS];
1408 case BCM47XX_BOARD_MOTOROLA_WE800G:
1409 return &platforms[WE800G];
1410 case BCM47XX_BOARD_MOTOROLA_WR850GP:
1411 return &platforms[WR850GP];
1412 case BCM47XX_BOARD_MOTOROLA_WR850GV2V3:
1413 return &platforms[WR850GV2V3];
1414 case BCM47XX_BOARD_NETGEAR_WNDR3400V1:
1415 return &platforms[WNDR3400V1];
1416 case BCM47XX_BOARD_NETGEAR_WNDR3700V3:
1417 return &platforms[WNDR3700V3];
1418 case BCM47XX_BOARD_UNKNOWN:
1419 case BCM47XX_BOARD_NON:
1420 printk(MODULE_NAME ": unknown board found, try legacy detect\n");
1421 printk(MODULE_NAME ": please open a ticket at https://dev.openwrt.org and attach the complete nvram\n");
1422 return platform_detect_legacy();
1423 default:
1424 printk(MODULE_NAME ": board was detected as \"%s\", but not gpio configuration available\n", board_name);
1425 printk(MODULE_NAME ": now trying legacy detect\n");
1426 return platform_detect_legacy();
1427 }
1428 }
1429
1430 static inline void ssb_maskset32(struct ssb_device *dev,
1431 u16 offset, u32 mask, u32 set)
1432 {
1433 ssb_write32(dev, offset, (ssb_read32(dev, offset) & mask) | set);
1434 }
1435
1436 static void gpio_set_irqenable(int enabled, irqreturn_t (*handler)(int, void *))
1437 {
1438 int irq;
1439 int err;
1440
1441 irq = gpio_to_irq(0);
1442 if (irq < 0) {
1443 pr_err("no irq for gpio available\n");
1444 return;
1445 }
1446
1447 if (enabled) {
1448 err = request_irq(irq, handler, IRQF_SHARED, "gpio", handler);
1449 if (err) {
1450 pr_err("can not reqeust irq\n");
1451 return;
1452 }
1453 } else {
1454 free_irq(irq, handler);
1455 }
1456
1457 switch (bcm47xx_bus_type) {
1458 #ifdef CONFIG_BCM47XX_SSB
1459 case BCM47XX_BUS_TYPE_SSB:
1460 if (bcm47xx_bus.ssb.chipco.dev)
1461 ssb_maskset32(bcm47xx_bus.ssb.chipco.dev, SSB_CHIPCO_IRQMASK, ~SSB_CHIPCO_IRQ_GPIO, (enabled ? SSB_CHIPCO_IRQ_GPIO : 0));
1462 break;
1463 #endif
1464 #ifdef CONFIG_BCM47XX_BCMA
1465 case BCM47XX_BUS_TYPE_BCMA:
1466 if (bcm47xx_bus.bcma.bus.drv_cc.core)
1467 bcma_maskset32(bcm47xx_bus.bcma.bus.drv_cc.core, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO, (enabled ? BCMA_CC_IRQ_GPIO : 0));
1468 break;
1469 #endif
1470 }
1471 }
1472
1473 static void hotplug_button(struct work_struct *work)
1474 {
1475 struct event_t *event = container_of(work, struct event_t, wq);
1476 char *s;
1477
1478 event->skb = alloc_skb(2048, GFP_KERNEL);
1479
1480 s = skb_put(event->skb, strlen(event->action) + 2);
1481 sprintf(s, "%s@", event->action);
1482 fill_event(event);
1483
1484 NETLINK_CB(event->skb).dst_group = 1;
1485 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
1486
1487 kfree(event);
1488 }
1489
1490 static irqreturn_t button_handler(int irq, void *dev_id)
1491 {
1492 struct button_t *b;
1493 u32 in, changed;
1494
1495 in = bcm47xx_gpio_in(~0) & platform.button_mask;
1496 bcm47xx_gpio_polarity(platform.button_mask, in);
1497 changed = platform.button_polarity ^ in;
1498 platform.button_polarity = in;
1499
1500 changed &= ~bcm47xx_gpio_outen(0, 0);
1501
1502 for (b = platform.buttons; b->name; b++) {
1503 struct event_t *event;
1504
1505 if (!(b->gpio & changed)) continue;
1506
1507 b->pressed ^= 1;
1508
1509 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1510 event->seen = (jiffies - b->seen)/HZ;
1511 event->name = b->name;
1512 event->action = b->pressed ? "pressed" : "released";
1513 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1514 schedule_work(&event->wq);
1515 }
1516
1517 b->seen = jiffies;
1518 }
1519 return IRQ_HANDLED;
1520 }
1521
1522 static void register_buttons(struct button_t *b)
1523 {
1524 for (; b->name; b++)
1525 platform.button_mask |= b->gpio;
1526
1527 platform.button_mask &= ~gpiomask;
1528
1529 bcm47xx_gpio_outen(platform.button_mask, 0);
1530 bcm47xx_gpio_control(platform.button_mask, 0);
1531 platform.button_polarity = bcm47xx_gpio_in(~0) & platform.button_mask;
1532 bcm47xx_gpio_polarity(platform.button_mask, platform.button_polarity);
1533 bcm47xx_gpio_intmask(platform.button_mask, platform.button_mask);
1534
1535 gpio_set_irqenable(1, button_handler);
1536 }
1537
1538 static void unregister_buttons(struct button_t *b)
1539 {
1540 bcm47xx_gpio_intmask(platform.button_mask, 0);
1541
1542 gpio_set_irqenable(0, button_handler);
1543 }
1544
1545
1546 static void add_msg(struct event_t *event, char *msg, int argv)
1547 {
1548 char *s;
1549
1550 if (argv)
1551 return;
1552
1553 s = skb_put(event->skb, strlen(msg) + 1);
1554 strcpy(s, msg);
1555 }
1556
1557 static int fill_event (struct event_t *event)
1558 {
1559 static char buf[128];
1560
1561 add_msg(event, "HOME=/", 0);
1562 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1563 add_msg(event, "SUBSYSTEM=button", 0);
1564 snprintf(buf, 128, "ACTION=%s", event->action);
1565 add_msg(event, buf, 0);
1566 snprintf(buf, 128, "BUTTON=%s", event->name);
1567 add_msg(event, buf, 0);
1568 snprintf(buf, 128, "SEEN=%ld", event->seen);
1569 add_msg(event, buf, 0);
1570 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1571 add_msg(event, buf, 0);
1572
1573 return 0;
1574 }
1575
1576 /*
1577 * This should be extended to allow the platform to specify the pins and width
1578 * of the shift register. They're hardcoded for now because only the WNDR3700v3
1579 * uses it.
1580 */
1581 static void shiftreg_output(unsigned int val)
1582 {
1583 unsigned int mask;
1584
1585 bcm47xx_gpio_out(SHIFTREG_DATA, SHIFTREG_DATA); /* init off, pull high */
1586 bcm47xx_gpio_out(SHIFTREG_CLK, 0); /* init reset */
1587
1588 /* shift 8 times */
1589 for(mask = 1 << (SHIFTREG_MAX_BITS-1); mask; mask >>= 1)
1590 {
1591 bcm47xx_gpio_out(SHIFTREG_DATA, (val & mask) ? SHIFTREG_DATA : 0);
1592 bcm47xx_gpio_out(SHIFTREG_CLK, SHIFTREG_CLK); /* pull high to trigger */
1593 bcm47xx_gpio_out(SHIFTREG_CLK, 0); /* reset to low */
1594 }
1595 }
1596
1597 static void set_led_shift(struct led_t *led)
1598 {
1599 static u32 shiftreg = 0;
1600 u32 old = shiftreg;
1601 u32 pin = (led->gpio & ~GPIO_TYPE_MASK);
1602
1603 if (led->state) {
1604 shiftreg |= pin;
1605 } else {
1606 shiftreg &= ~pin;
1607 }
1608
1609 /* Clock the bits out. */
1610 if (shiftreg != old) {
1611 shiftreg_output(shiftreg);
1612 }
1613 }
1614
1615 static void set_led_extif(struct led_t *led)
1616 {
1617 volatile u8 *addr = (volatile u8 *) KSEG1ADDR(EXTIF_UART) + (led->gpio & ~GPIO_TYPE_MASK);
1618 if (led->state)
1619 *addr = 0xFF;
1620 else
1621 *addr;
1622 }
1623
1624
1625 static void led_flash(unsigned long dummy) {
1626 struct led_t *l;
1627 u32 mask = 0;
1628 u8 extif_blink = 0;
1629
1630 for (l = platform.leds; l->name; l++) {
1631 if (!l->flash) continue;
1632 switch (l->gpio & GPIO_TYPE_MASK) {
1633 case GPIO_TYPE_EXTIF:
1634 extif_blink = 1;
1635 l->state = !l->state;
1636 set_led_extif(l);
1637 break;
1638 case GPIO_TYPE_SHIFT:
1639 extif_blink = 1;
1640 l->state = !l->state;
1641 set_led_shift(l);
1642 break;
1643 case GPIO_TYPE_NORMAL:
1644 default:
1645 mask |= l->gpio;
1646 break;
1647 }
1648 }
1649
1650 mask &= ~gpiomask;
1651 if (mask) {
1652 u32 val = ~bcm47xx_gpio_in(~0);
1653
1654 bcm47xx_gpio_outen(mask, mask);
1655 bcm47xx_gpio_control(mask, 0);
1656 bcm47xx_gpio_out(mask, val);
1657 }
1658 if (mask || extif_blink) {
1659 mod_timer(&led_timer, jiffies + FLASH_TIME);
1660 }
1661 }
1662
1663 static int diag_led_show(struct seq_file *m, void *v)
1664 {
1665 struct led_t * led = m->private;
1666
1667 u8 p = (led->polarity == NORMAL ? 0 : 1);
1668 if (led->flash) {
1669 return seq_printf(m, "f\n");
1670 } else if ((led->gpio & GPIO_TYPE_MASK) != GPIO_TYPE_NORMAL) {
1671 return seq_printf(m, "%d\n", ((led->state ^ p) ? 1 : 0));
1672 } else {
1673 u32 in = (bcm47xx_gpio_in(~0) & led->gpio ? 1 : 0);
1674 return seq_printf(m, "%d\n", ((in ^ p) ? 1 : 0));
1675 }
1676 }
1677
1678 static int diag_led_open(struct inode *inode, struct file *file)
1679 {
1680 return single_open(file, diag_led_show, PDE_DATA(inode));
1681 }
1682
1683 static ssize_t diag_led_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1684 {
1685 struct led_t *led = PDE_DATA(file_inode(file));
1686 char cmd[5];
1687 size_t len;
1688 int p;
1689
1690 len = min(count, sizeof(cmd) - 1);
1691 if (copy_from_user(cmd, buf, len))
1692 return -EFAULT;
1693
1694 cmd[len] = 0;
1695
1696 p = (led->polarity == NORMAL ? 0 : 1);
1697 if (cmd[0] == 'f') {
1698 led->flash = 1;
1699 led_flash(0);
1700 } else {
1701 led->flash = 0;
1702 if ((led->gpio & GPIO_TYPE_MASK) == GPIO_TYPE_EXTIF) {
1703 led->state = p ^ ((cmd[0] == '1') ? 1 : 0);
1704 set_led_extif(led);
1705 } else if ((led->gpio & GPIO_TYPE_MASK) == GPIO_TYPE_SHIFT) {
1706 led->state = p ^ ((cmd[0] == '1') ? 1 : 0);
1707 set_led_shift(led);
1708 } else {
1709 bcm47xx_gpio_outen(led->gpio, led->gpio);
1710 bcm47xx_gpio_control(led->gpio, 0);
1711 bcm47xx_gpio_out(led->gpio, ((p ^ (cmd[0] == '1')) ? led->gpio : 0));
1712 }
1713 }
1714 return count;
1715 }
1716
1717 static const struct file_operations diag_led_fops = {
1718 .open = diag_led_open,
1719 .read = seq_read,
1720 .llseek = seq_lseek,
1721 .write = diag_led_write
1722 };
1723
1724 static void register_leds(struct led_t *l)
1725 {
1726 struct proc_dir_entry *p;
1727 u32 mask = 0;
1728 u32 oe_mask = 0;
1729 u32 val = 0;
1730
1731 leds = proc_mkdir("led", diag);
1732 if (!leds)
1733 return;
1734
1735 for(; l->name; l++) {
1736 if (l->gpio & gpiomask)
1737 continue;
1738
1739 switch (l->gpio & GPIO_TYPE_MASK) {
1740 case GPIO_TYPE_EXTIF:
1741 l->state = 0;
1742 set_led_extif(l);
1743 break;
1744 case GPIO_TYPE_SHIFT:
1745 mask |= (SHIFTREG_DATA | SHIFTREG_CLK);
1746 oe_mask |= (SHIFTREG_DATA | SHIFTREG_CLK);
1747 l->state = (l->polarity != NORMAL);
1748 set_led_shift(l);
1749 break;
1750 case GPIO_TYPE_NORMAL:
1751 default:
1752 if (l->polarity != INPUT) oe_mask |= l->gpio;
1753 mask |= l->gpio;
1754 val |= (l->polarity == NORMAL)?0:l->gpio;
1755 break;
1756 }
1757
1758 if (l->polarity == INPUT) continue;
1759
1760 p = proc_create_data(l->name, S_IRUSR, leds, &diag_led_fops, l);
1761 }
1762
1763 bcm47xx_gpio_outen(mask, oe_mask);
1764 bcm47xx_gpio_control(mask, 0);
1765 bcm47xx_gpio_out(mask, val);
1766 bcm47xx_gpio_intmask(mask, 0);
1767 }
1768
1769 static void unregister_leds(struct led_t *l)
1770 {
1771 for(; l->name; l++)
1772 remove_proc_entry(l->name, leds);
1773
1774 remove_proc_entry("led", diag);
1775 }
1776
1777 static int diag_model_show(struct seq_file *m, void *v)
1778 {
1779 return seq_printf(m, "%s\n", platform.name);
1780 }
1781
1782 static int diag_model_open(struct inode *inode, struct file *file)
1783 {
1784 return single_open(file, diag_model_show, PDE_DATA(inode));
1785 }
1786
1787 static const struct file_operations diag_model_fops = {
1788 .open = diag_model_open,
1789 .read = seq_read,
1790 .llseek = seq_lseek
1791 };
1792
1793 static int diag_gpiomask_show(struct seq_file *m, void *v)
1794 {
1795 return seq_printf(m, "0x%04x\n", gpiomask);
1796 }
1797
1798 static int diag_gpiomask_open(struct inode *inode, struct file *file)
1799 {
1800 return single_open(file, diag_gpiomask_show, PDE_DATA(inode));
1801 }
1802
1803 static ssize_t diag_gpiomask_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1804 {
1805 int err = kstrtouint_from_user(buf, count, 0, &gpiomask);
1806 if (err)
1807 return err;
1808
1809 if (platform.buttons) {
1810 unregister_buttons(platform.buttons);
1811 register_buttons(platform.buttons);
1812 }
1813
1814 if (platform.leds) {
1815 unregister_leds(platform.leds);
1816 register_leds(platform.leds);
1817 }
1818
1819 return count;
1820 }
1821
1822 static const struct file_operations diag_gpiomask_fops = {
1823 .open = diag_gpiomask_open,
1824 .read = seq_read,
1825 .llseek = seq_lseek,
1826 .write = diag_gpiomask_write
1827 };
1828
1829 static int __init diag_init(void)
1830 {
1831 static struct proc_dir_entry *p;
1832 static struct platform_t *detected;
1833
1834 detected = platform_detect();
1835 if (!detected) {
1836 printk(MODULE_NAME ": Router model not detected.\n");
1837 return -ENODEV;
1838 }
1839 memcpy(&platform, detected, sizeof(struct platform_t));
1840
1841 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1842 if (platform.platform_init != NULL) {
1843 platform.platform_init();
1844 }
1845
1846 if (!(diag = proc_mkdir("diag", NULL))) {
1847 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1848 return -EINVAL;
1849 }
1850
1851 p = proc_create("model", S_IRUSR, diag, &diag_model_fops);
1852 if (!p) {
1853 remove_proc_entry("diag", NULL);
1854 return -EINVAL;
1855 }
1856
1857 p = proc_create("gpiomask", S_IRUSR | S_IWUSR, diag, &diag_gpiomask_fops);
1858 if (!p) {
1859 remove_proc_entry("model", diag);
1860 remove_proc_entry("diag", NULL);
1861 return -EINVAL;
1862 }
1863
1864 if (platform.buttons)
1865 register_buttons(platform.buttons);
1866
1867 if (platform.leds)
1868 register_leds(platform.leds);
1869
1870 return 0;
1871 }
1872
1873 static void __exit diag_exit(void)
1874 {
1875 del_timer(&led_timer);
1876
1877 if (platform.buttons)
1878 unregister_buttons(platform.buttons);
1879
1880 if (platform.leds)
1881 unregister_leds(platform.leds);
1882
1883 remove_proc_entry("model", diag);
1884 remove_proc_entry("gpiomask", diag);
1885 remove_proc_entry("diag", NULL);
1886 }
1887
1888 module_init(diag_init);
1889 module_exit(diag_exit);
1890
1891 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1892 MODULE_LICENSE("GPL");