cde681a0f2e3744fbe22a7d113f50ad895322a7e
[openwrt/svn-archive/archive.git] / package / 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 * $Id$
23 */
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/kmod.h>
27 #include <linux/proc_fs.h>
28 #include <linux/timer.h>
29 #include <linux/version.h>
30 #include <asm/uaccess.h>
31
32 #ifndef LINUX_2_4
33 #include <linux/workqueue.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
36 #include <net/sock.h>
37 extern struct sock *uevent_sock;
38 extern u64 uevent_next_seqnum(void);
39 #else
40 #include <linux/tqueue.h>
41 #define INIT_WORK INIT_TQUEUE
42 #define schedule_work schedule_task
43 #define work_struct tq_struct
44 #endif
45
46 #include "gpio.h"
47 #include "diag.h"
48 #define getvar(str) (nvram_get(str)?:"")
49
50 static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
51 static int fill_event(struct event_t *);
52 static unsigned int gpiomask = 0;
53 module_param(gpiomask, int, 0644);
54
55 enum {
56 /* Linksys */
57 WAP54GV1,
58 WAP54GV3,
59 WRT54GV1,
60 WRT54G,
61 WRTSL54GS,
62 WRT54G3G,
63 WRT350N,
64 WRT600N,
65 WRT600NV11,
66
67 /* ASUS */
68 WLHDD,
69 WL300G,
70 WL500G,
71 WL500GD,
72 WL500GP,
73 WL500GPV2,
74 WL500W,
75 WL520GC,
76 WL520GU,
77 ASUS_4702,
78 WL700GE,
79
80 /* Buffalo */
81 WBR2_G54,
82 WHR_G54S,
83 WHR_HP_G54,
84 WHR_G125,
85 WHR2_A54G54,
86 WLA2_G54L,
87 WZR_G300N,
88 WZR_RS_G54,
89 WZR_RS_G54HP,
90 BUFFALO_UNKNOWN,
91 BUFFALO_UNKNOWN_4710,
92
93 /* Siemens */
94 SE505V1,
95 SE505V2,
96
97 /* US Robotics */
98 USR5461,
99
100 /* Dell */
101 TM2300,
102 TM2300V2,
103
104 /* Motorola */
105 WE800G,
106 WR850GV1,
107 WR850GV2V3,
108 WR850GP,
109
110 /* Belkin */
111 BELKIN_UNKNOWN,
112
113 /* Netgear */
114 WGT634U,
115
116 /* Trendware */
117 TEW411BRPP,
118
119 /* SimpleTech */
120 STI_NAS,
121
122 /* D-Link */
123 DIR130,
124 DIR330,
125 DWL3150,
126
127 /* Sitecom */
128 WL105B,
129 };
130
131 static void __init bcm4780_init(void) {
132 int pin = 1 << 3;
133
134 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
135 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
136 gpio_outen(pin, pin);
137 gpio_control(pin, 0);
138 gpio_out(pin, pin);
139
140 /* Wait 5s, so the HDD can spin up */
141 set_current_state(TASK_INTERRUPTIBLE);
142 schedule_timeout(HZ * 5);
143 }
144
145 static void __init bcm57xx_init(void) {
146 int pin = 1 << 2;
147
148 #ifndef LINUX_2_4
149 /* FIXME: switch comes up, but port mappings/vlans not right */
150 gpio_outen(pin, pin);
151 gpio_control(pin, 0);
152 gpio_out(pin, pin);
153 #endif
154 }
155
156 static struct platform_t __initdata platforms[] = {
157 /* Linksys */
158 [WAP54GV1] = {
159 .name = "Linksys WAP54G V1",
160 .buttons = {
161 { .name = "reset", .gpio = 1 << 0 },
162 },
163 .leds = {
164 { .name = "diag", .gpio = 1 << 3 },
165 { .name = "wlan", .gpio = 1 << 4 },
166 },
167 },
168 [WAP54GV3] = {
169 .name = "Linksys WAP54G V3",
170 .buttons = {
171 /* FIXME: verify this */
172 { .name = "reset", .gpio = 1 << 7 },
173 { .name = "ses", .gpio = 1 << 0 },
174 },
175 .leds = {
176 /* FIXME: diag? */
177 { .name = "ses", .gpio = 1 << 1 },
178 },
179 },
180 [WRT54GV1] = {
181 .name = "Linksys WRT54G V1.x",
182 .buttons = {
183 { .name = "reset", .gpio = 1 << 6 },
184 },
185 .leds = {
186 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
187 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
188 },
189 },
190 [WRT54G] = {
191 .name = "Linksys WRT54G/GS/GL",
192 .buttons = {
193 { .name = "reset", .gpio = 1 << 6 },
194 { .name = "ses", .gpio = 1 << 4 },
195 },
196 .leds = {
197 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
198 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
199 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
200 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
201 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
202 },
203 },
204 [WRTSL54GS] = {
205 .name = "Linksys WRTSL54GS",
206 .buttons = {
207 { .name = "reset", .gpio = 1 << 6 },
208 { .name = "ses", .gpio = 1 << 4 },
209 },
210 .leds = {
211 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
212 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
213 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
214 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
215 },
216 },
217 [WRT54G3G] = {
218 .name = "Linksys WRT54G3G",
219 .buttons = {
220 { .name = "reset", .gpio = 1 << 6 },
221 { .name = "3g", .gpio = 1 << 4 },
222 },
223 .leds = {
224 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
225 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
226 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
227 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
228 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
229 },
230 },
231 [WRT350N] = {
232 .name = "Linksys WRT350N",
233 .buttons = {
234 { .name = "reset", .gpio = 1 << 6 },
235 { .name = "ses", .gpio = 1 << 8 },
236 },
237 .leds = {
238 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
239 { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
240 { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
241 { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
242 { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
243 },
244 .platform_init = bcm57xx_init,
245 },
246 [WRT600N] = {
247 .name = "Linksys WRT600N",
248 .buttons = {
249 { .name = "reset", .gpio = 1 << 6 },
250 { .name = "ses", .gpio = 1 << 7 },
251 },
252 .leds = {
253 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
254 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
255 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
256 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
257 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
258 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
259 },
260 .platform_init = bcm57xx_init,
261 },
262 [WRT600NV11] = {
263 .name = "Linksys WRT600N V1.1",
264 .buttons = {
265 { .name = "reset", .gpio = 1 << 6 },
266 { .name = "ses", .gpio = 1 << 7 },
267 },
268 .leds = {
269 { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
270 { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
271 { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
272 { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
273 { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
274 { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
275 },
276 .platform_init = bcm57xx_init,
277 },
278 /* Asus */
279 [WLHDD] = {
280 .name = "ASUS WL-HDD",
281 .buttons = {
282 { .name = "reset", .gpio = 1 << 6 },
283 },
284 .leds = {
285 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
286 { .name = "usb", .gpio = 1 << 2, .polarity = NORMAL },
287 },
288 },
289 [WL300G] = {
290 .name = "ASUS WL-300g",
291 .buttons = {
292 { .name = "reset", .gpio = 1 << 6 },
293 },
294 .leds = {
295 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
296 },
297 },
298 [WL500G] = {
299 .name = "ASUS WL-500g",
300 .buttons = {
301 { .name = "reset", .gpio = 1 << 6 },
302 },
303 .leds = {
304 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
305 },
306 },
307 [WL500GD] = {
308 .name = "ASUS WL-500g Deluxe",
309 .buttons = {
310 { .name = "reset", .gpio = 1 << 6 },
311 },
312 .leds = {
313 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
314 },
315 },
316 [WL500GP] = {
317 .name = "ASUS WL-500g Premium",
318 .buttons = {
319 { .name = "reset", .gpio = 1 << 0 },
320 { .name = "ses", .gpio = 1 << 4 },
321 },
322 .leds = {
323 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
324 },
325 },
326 [WL500GPV2] = {
327 .name = "ASUS WL-500g Premium V2",
328 .buttons = {
329 { .name = "reset", .gpio = 1 << 2 },
330 { .name = "ses", .gpio = 1 << 3 },
331 },
332 .leds = {
333 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
334 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
335 },
336 },
337 [WL500W] = {
338 .name = "ASUS WL-500W",
339 .buttons = {
340 { .name = "reset", .gpio = 1 << 6 },
341 { .name = "ses", .gpio = 1 << 7 },
342 },
343 .leds = {
344 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
345 },
346 },
347 [WL520GC] = {
348 .name = "ASUS WL-520GC",
349 .buttons = {
350 { .name = "reset", .gpio = 1 << 2 },
351 { .name = "ses", .gpio = 1 << 3 },
352 },
353 .leds = {
354 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
355 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
356 },
357 },
358 [WL520GU] = {
359 .name = "ASUS WL-520gU",
360 .buttons = {
361 { .name = "reset", .gpio = 1 << 2 },
362 { .name = "ses", .gpio = 1 << 3 },
363 },
364 .leds = {
365 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
366 { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
367 },
368 },
369 [ASUS_4702] = {
370 .name = "ASUS (unknown, BCM4702)",
371 .buttons = {
372 { .name = "reset", .gpio = 1 << 6 },
373 },
374 .leds = {
375 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
376 },
377 },
378 [WL700GE] = {
379 .name = "ASUS WL-700gE",
380 .buttons = {
381 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
382 { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
383 { .name = "power", .gpio = 1 << 0 }, // on front
384 { .name = "copy", .gpio = 1 << 6 }, // on front
385 },
386 .leds = {
387 #if 0
388 // GPIO that controls power led also enables/disables some essential functions
389 // - power to HDD
390 // - switch leds
391 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
392 #endif
393 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
394 },
395 .platform_init = bcm4780_init,
396 },
397 /* Buffalo */
398 [WHR_G54S] = {
399 .name = "Buffalo WHR-G54S",
400 .buttons = {
401 { .name = "reset", .gpio = 1 << 4 },
402 { .name = "bridge", .gpio = 1 << 5 },
403 { .name = "ses", .gpio = 1 << 0 },
404 },
405 .leds = {
406 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
407 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
408 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
409 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
410 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
411 },
412 },
413 [WBR2_G54] = {
414 .name = "Buffalo WBR2-G54",
415 /* FIXME: verify */
416 .buttons = {
417 { .name = "reset", .gpio = 1 << 7 },
418 },
419 .leds = {
420 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
421 },
422 },
423 [WHR_HP_G54] = {
424 .name = "Buffalo WHR-HP-G54",
425 .buttons = {
426 { .name = "reset", .gpio = 1 << 4 },
427 { .name = "bridge", .gpio = 1 << 5 },
428 { .name = "ses", .gpio = 1 << 0 },
429 },
430 .leds = {
431 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
432 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
433 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
434 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
435 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
436 },
437 },
438 [WHR_G125] = {
439 .name = "Buffalo WHR-G125",
440 .buttons = {
441 { .name = "reset", .gpio = 1 << 4 },
442 { .name = "bridge", .gpio = 1 << 5 },
443 { .name = "ses", .gpio = 1 << 0 },
444 },
445 .leds = {
446 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
447 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
448 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
449 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
450 { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
451 },
452 },
453 [WHR2_A54G54] = {
454 .name = "Buffalo WHR2-A54G54",
455 .buttons = {
456 { .name = "reset", .gpio = 1 << 4 },
457 },
458 .leds = {
459 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
460 },
461 },
462 [WLA2_G54L] = {
463 .name = "Buffalo WLA2-G54L",
464 /* FIXME: verify */
465 .buttons = {
466 { .name = "reset", .gpio = 1 << 7 },
467 },
468 .leds = {
469 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
470 },
471 },
472 [WZR_G300N] = {
473 .name = "Buffalo WZR-G300N",
474 .buttons = {
475 { .name = "reset", .gpio = 1 << 4 },
476 },
477 .leds = {
478 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
479 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
480 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
481 },
482 },
483 [WZR_RS_G54] = {
484 .name = "Buffalo WZR-RS-G54",
485 .buttons = {
486 { .name = "ses", .gpio = 1 << 0 },
487 { .name = "reset", .gpio = 1 << 4 },
488 },
489 .leds = {
490 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
491 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
492 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
493 },
494 },
495 [WZR_RS_G54HP] = {
496 .name = "Buffalo WZR-RS-G54HP",
497 .buttons = {
498 { .name = "ses", .gpio = 1 << 0 },
499 { .name = "reset", .gpio = 1 << 4 },
500 },
501 .leds = {
502 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
503 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
504 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
505 },
506 },
507 [BUFFALO_UNKNOWN] = {
508 .name = "Buffalo (unknown)",
509 .buttons = {
510 { .name = "reset", .gpio = 1 << 7 },
511 },
512 .leds = {
513 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
514 },
515 },
516 [BUFFALO_UNKNOWN_4710] = {
517 .name = "Buffalo (unknown, BCM4710)",
518 .buttons = {
519 { .name = "reset", .gpio = 1 << 4 },
520 },
521 .leds = {
522 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
523 },
524 },
525 /* Siemens */
526 [SE505V1] = {
527 .name = "Siemens SE505 V1",
528 .buttons = {
529 /* No usable buttons */
530 },
531 .leds = {
532 // { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
533 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
534 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
535 },
536 },
537 [SE505V2] = {
538 .name = "Siemens SE505 V2",
539 .buttons = {
540 /* No usable buttons */
541 },
542 .leds = {
543 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
544 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
545 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
546 },
547 },
548 /* US Robotics */
549 [USR5461] = {
550 .name = "U.S. Robotics USR5461",
551 .buttons = {
552 /* No usable buttons */
553 },
554 .leds = {
555 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
556 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
557 },
558 },
559 /* Dell */
560 [TM2300] = {
561 .name = "Dell TrueMobile 2300",
562 .buttons = {
563 { .name = "reset", .gpio = 1 << 0 },
564 },
565 .leds = {
566 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
567 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
568 },
569 },
570 [TM2300V2] = {
571 .name = "Dell TrueMobile 2300 v2",
572 .buttons = {
573 { .name = "reset", .gpio = 1 << 0 },
574 },
575 .leds = {
576 { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
577 { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
578 },
579 },
580 /* Motorola */
581 [WE800G] = {
582 .name = "Motorola WE800G",
583 .buttons = {
584 { .name = "reset", .gpio = 1 << 0 },
585 },
586 .leds = {
587 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
588 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
589 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
590 },
591 },
592 [WR850GV1] = {
593 .name = "Motorola WR850G V1",
594 .buttons = {
595 { .name = "reset", .gpio = 1 << 0 },
596 },
597 .leds = {
598 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
599 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
600 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
601 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
602 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
603 },
604 },
605 [WR850GV2V3] = {
606 .name = "Motorola WR850G V2/V3",
607 .buttons = {
608 { .name = "reset", .gpio = 1 << 5 },
609 },
610 .leds = {
611 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
612 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
613 { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
614 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
615 },
616 },
617 [WR850GP] = {
618 .name = "Motorola WR850GP",
619 .buttons = {
620 { .name = "reset", .gpio = 1 << 5 },
621 },
622 .leds = {
623 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
624 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
625 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
626 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
627 },
628 },
629
630 /* Belkin */
631 [BELKIN_UNKNOWN] = {
632 .name = "Belkin (unknown)",
633 /* FIXME: verify & add detection */
634 .buttons = {
635 { .name = "reset", .gpio = 1 << 7 },
636 },
637 .leds = {
638 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
639 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
640 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
641 },
642 },
643 /* Netgear */
644 [WGT634U] = {
645 .name = "Netgear WGT634U",
646 .buttons = {
647 { .name = "reset", .gpio = 1 << 2 },
648 },
649 .leds = {
650 { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
651 },
652 },
653 /* Trendware */
654 [TEW411BRPP] = {
655 .name = "Trendware TEW411BRP+",
656 .buttons = {
657 { /* No usable buttons */ },
658 },
659 .leds = {
660 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
661 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
662 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
663 },
664 },
665 /* SimpleTech */
666 [STI_NAS] = {
667 .name = "SimpleTech SimpleShare NAS",
668 .buttons = {
669 { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
670 { .name = "power", .gpio = 1 << 0 }, // on back
671 },
672 .leds = {
673 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
674 },
675 .platform_init = bcm4780_init,
676 },
677 /* D-Link */
678 [DIR130] = {
679 .name = "D-Link DIR-130",
680 .buttons = {
681 { .name = "reset", .gpio = 1 << 3},
682 { .name = "reserved", .gpio = 1 << 7},
683 },
684 .leds = {
685 { .name = "diag", .gpio = 1 << 0},
686 { .name = "blue", .gpio = 1 << 6},
687 },
688 },
689 [DIR330] = {
690 .name = "D-Link DIR-330",
691 .buttons = {
692 { .name = "reset", .gpio = 1 << 3},
693 { .name = "reserved", .gpio = 1 << 7},
694 },
695 .leds = {
696 { .name = "diag", .gpio = 1 << 0},
697 { .name = "usb", .gpio = 1 << 4},
698 { .name = "blue", .gpio = 1 << 6},
699 },
700 },
701 [DWL3150] = {
702 .name = "D-Link DWL-3150",
703 .buttons = {
704 { .name = "reset", .gpio = 1 << 7},
705 },
706 .leds = {
707 { .name = "diag", .gpio = 1 << 2},
708 { .name = "status", .gpio = 1 << 1},
709 },
710 },
711 /* Double check */
712 [WL105B] = {
713 .name = "Sitecom WL-105b",
714 .buttons = {
715 { .name = "reset", .gpio = 1 << 10},
716 },
717 .leds = {
718 { .name = "wlan", .gpio = 1 << 4},
719 { .name = "power", .gpio = 1 << 3},
720 },
721 },
722 };
723
724 static struct platform_t __init *platform_detect(void)
725 {
726 char *boardnum, *boardtype, *buf;
727
728 if (strcmp(getvar("nvram_type"), "cfe") == 0)
729 return &platforms[WGT634U];
730
731 /* Look for a model identifier */
732
733 /* Based on "model_name" */
734 if ((buf = nvram_get("model_name"))) {
735 if (!strcmp(buf, "DIR-130"))
736 return &platforms[DIR130];
737 if (!strcmp(buf, "DIR-330"))
738 return &platforms[DIR330];
739 }
740
741 /* Based on "model_no" */
742 if ((buf = nvram_get("model_no"))) {
743 if (startswith(buf,"WL700")) /* WL700* */
744 return &platforms[WL700GE];
745 }
746
747 /* Based on "hardware_version" */
748 if ((buf = nvram_get("hardware_version"))) {
749 if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
750 return &platforms[WL500GPV2];
751 if (startswith(buf,"WL520GC-")) /* WL520GU-* */
752 return &platforms[WL520GC];
753 if (startswith(buf,"WL520GU-")) /* WL520GU-* */
754 return &platforms[WL520GU];
755 }
756
757 /* Based on "ModelId" */
758 if ((buf = nvram_get("ModelId"))) {
759 if (!strcmp(buf, "WR850GP"))
760 return &platforms[WR850GP];
761 if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
762 return &platforms[TM2300]; /* Dell TrueMobile 2300 */
763 if (startswith(buf,"WE800G")) /* WE800G* */
764 return &platforms[WE800G];
765 }
766
767 /* Buffalo */
768 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
769 /* Buffalo hardware, check id for specific hardware matches */
770 if (!strcmp(buf, "29bb0332"))
771 return &platforms[WBR2_G54];
772 if (!strcmp(buf, "29129"))
773 return &platforms[WLA2_G54L];
774 if (!strcmp(buf, "30189"))
775 return &platforms[WHR_HP_G54];
776 if (!strcmp(buf, "32093"))
777 return &platforms[WHR_G125];
778 if (!strcmp(buf, "30182"))
779 return &platforms[WHR_G54S];
780 if (!strcmp(buf, "290441dd"))
781 return &platforms[WHR2_A54G54];
782 if (!strcmp(buf, "31120"))
783 return &platforms[WZR_G300N];
784 if (!strcmp(buf, "30083"))
785 return &platforms[WZR_RS_G54];
786 if (!strcmp(buf, "30103"))
787 return &platforms[WZR_RS_G54HP];
788 }
789
790 /* no easy model number, attempt to guess */
791 boardnum = getvar("boardnum");
792 boardtype = getvar("boardtype");
793
794 if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
795 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
796 return &platforms[WRT600NV11];
797
798 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
799 return &platforms[WRT600N];
800 }
801
802 if (startswith(getvar("pmon_ver"), "CFE")) {
803 /* CFE based - newer hardware */
804 if (!strcmp(boardnum, "42")) { /* Linksys */
805 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
806 return &platforms[WRT350N];
807
808 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
809 return &platforms[WRT54G3G];
810
811 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
812 return &platforms[WRTSL54GS];
813
814 /* default to WRT54G */
815 return &platforms[WRT54G];
816 }
817
818 if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
819 if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
820 return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
821 }
822
823 if (!strcmp(boardnum, "45")) { /* ASUS */
824 if (!strcmp(boardtype,"0x042f"))
825 return &platforms[WL500GP];
826 else if (!strcmp(boardtype,"0x0472"))
827 return &platforms[WL500W];
828 else
829 return &platforms[WL500GD];
830 }
831
832 if (!strcmp(boardnum, "10496"))
833 return &platforms[USR5461];
834
835 if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
836 return &platforms[SE505V2];
837
838 } else { /* PMON based - old stuff */
839 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
840 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
841 return &platforms[WR850GV1];
842 }
843 if (startswith(boardtype, "bcm94710dev")) {
844 if (!strcmp(boardnum, "42"))
845 return &platforms[WRT54GV1];
846 if (simple_strtoul(boardnum, NULL, 0) == 2)
847 return &platforms[WAP54GV1];
848 }
849 if (startswith(getvar("hardware_version"), "WL500-"))
850 return &platforms[WL500G];
851 if (startswith(getvar("hardware_version"), "WL300-")) {
852 /* Either WL-300g or WL-HDD, do more extensive checks */
853 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
854 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
855 return &platforms[WLHDD];
856 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
857 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
858 return &platforms[WL300G];
859 }
860 /* Sitecom WL-105b */
861 if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
862 return &platforms[WL105B];
863
864 /* unknown asus stuff, probably bcm4702 */
865 if (startswith(boardnum, "asusX"))
866 return &platforms[ASUS_4702];
867 }
868
869 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
870 if (startswith(boardtype, "bcm94710ap"))
871 return &platforms[BUFFALO_UNKNOWN_4710];
872 else
873 return &platforms[BUFFALO_UNKNOWN];
874 }
875
876 if (startswith(getvar("CFEver"), "MotoWRv2") ||
877 startswith(getvar("CFEver"), "MotoWRv3") ||
878 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
879
880 return &platforms[WR850GV2V3];
881 }
882
883 if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
884 return &platforms[TEW411BRPP];
885 }
886
887 if (startswith(boardnum, "04FN52")) /* SimpleTech SimpleShare */
888 return &platforms[STI_NAS];
889
890 if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
891 return &platforms[DWL3150];
892
893 /* not found */
894 return NULL;
895 }
896
897 static void register_buttons(struct button_t *b)
898 {
899 for (; b->name; b++)
900 platform.button_mask |= b->gpio;
901
902 platform.button_mask &= ~gpiomask;
903
904 gpio_outen(platform.button_mask, 0);
905 gpio_control(platform.button_mask, 0);
906 platform.button_polarity = gpio_in() & platform.button_mask;
907 gpio_intpolarity(platform.button_mask, platform.button_polarity);
908 gpio_intmask(platform.button_mask, platform.button_mask);
909
910 gpio_set_irqenable(1, button_handler);
911 }
912
913 static void unregister_buttons(struct button_t *b)
914 {
915 gpio_intmask(platform.button_mask, 0);
916
917 gpio_set_irqenable(0, button_handler);
918 }
919
920
921 #ifndef LINUX_2_4
922 static void add_msg(struct event_t *event, char *msg, int argv)
923 {
924 char *s;
925
926 if (argv)
927 return;
928
929 s = skb_put(event->skb, strlen(msg) + 1);
930 strcpy(s, msg);
931 }
932
933 static void hotplug_button(struct work_struct *work)
934 {
935 struct event_t *event = container_of(work, struct event_t, wq);
936 char *s;
937
938 if (!uevent_sock)
939 return;
940
941 event->skb = alloc_skb(2048, GFP_KERNEL);
942
943 s = skb_put(event->skb, strlen(event->action) + 2);
944 sprintf(s, "%s@", event->action);
945 fill_event(event);
946
947 NETLINK_CB(event->skb).dst_group = 1;
948 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
949
950 kfree(event);
951 }
952
953 #else /* !LINUX_2_4 */
954 static inline char *kzalloc(unsigned int size, unsigned int gfp)
955 {
956 char *p;
957
958 p = kmalloc(size, gfp);
959 if (p == NULL)
960 return NULL;
961
962 memset(p, 0, size);
963
964 return p;
965 }
966
967 static void add_msg(struct event_t *event, char *msg, int argv)
968 {
969 if (argv)
970 event->argv[event->anr++] = event->scratch;
971 else
972 event->envp[event->enr++] = event->scratch;
973
974 event->scratch += sprintf(event->scratch, "%s", msg) + 1;
975 }
976
977 static void hotplug_button(struct event_t *event)
978 {
979 char *scratch = kzalloc(256, GFP_KERNEL);
980 event->scratch = scratch;
981
982 add_msg(event, hotplug_path, 1);
983 add_msg(event, "button", 1);
984 fill_event(event);
985 call_usermodehelper (event->argv[0], event->argv, event->envp);
986 kfree(scratch);
987 kfree(event);
988 }
989 #endif /* !LINUX_2_4 */
990
991 static int fill_event (struct event_t *event)
992 {
993 static char buf[128];
994
995 add_msg(event, "HOME=/", 0);
996 add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
997 add_msg(event, "SUBSYSTEM=button", 0);
998 snprintf(buf, 128, "ACTION=%s", event->action);
999 add_msg(event, buf, 0);
1000 snprintf(buf, 128, "BUTTON=%s", event->name);
1001 add_msg(event, buf, 0);
1002 snprintf(buf, 128, "SEEN=%ld", event->seen);
1003 add_msg(event, buf, 0);
1004 #ifndef LINUX_2_4
1005 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1006 add_msg(event, buf, 0);
1007 #endif
1008
1009 return 0;
1010 }
1011
1012
1013 #ifndef LINUX_2_4
1014 static irqreturn_t button_handler(int irq, void *dev_id)
1015 #else
1016 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1017 #endif
1018 {
1019 struct button_t *b;
1020 u32 in, changed;
1021
1022 in = gpio_in() & platform.button_mask;
1023 gpio_intpolarity(platform.button_mask, in);
1024 changed = platform.button_polarity ^ in;
1025 platform.button_polarity = in;
1026
1027 changed &= ~gpio_outen(0, 0);
1028
1029 for (b = platform.buttons; b->name; b++) {
1030 struct event_t *event;
1031
1032 if (!(b->gpio & changed)) continue;
1033
1034 b->pressed ^= 1;
1035
1036 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1037 event->seen = (jiffies - b->seen)/HZ;
1038 event->name = b->name;
1039 event->action = b->pressed ? "pressed" : "released";
1040 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1041 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1042 #else
1043 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1044 #endif
1045 schedule_work(&event->wq);
1046 }
1047
1048 b->seen = jiffies;
1049 }
1050 return IRQ_HANDLED;
1051 }
1052
1053 static void register_leds(struct led_t *l)
1054 {
1055 struct proc_dir_entry *p;
1056 u32 mask = 0;
1057 u32 oe_mask = 0;
1058 u32 val = 0;
1059
1060 leds = proc_mkdir("led", diag);
1061 if (!leds)
1062 return;
1063
1064 for(; l->name; l++) {
1065 if (l->gpio & gpiomask)
1066 continue;
1067
1068 if (l->gpio & GPIO_TYPE_EXTIF) {
1069 l->state = 0;
1070 set_led_extif(l);
1071 } else {
1072 if (l->polarity != INPUT) oe_mask |= l->gpio;
1073 mask |= l->gpio;
1074 val |= (l->polarity == NORMAL)?0:l->gpio;
1075 }
1076
1077 if (l->polarity == INPUT) continue;
1078
1079 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1080 l->proc.type = PROC_LED;
1081 l->proc.ptr = l;
1082 p->data = (void *) &l->proc;
1083 p->proc_fops = &diag_proc_fops;
1084 }
1085 }
1086
1087 gpio_outen(mask, oe_mask);
1088 gpio_control(mask, 0);
1089 gpio_out(mask, val);
1090 gpio_intmask(mask, 0);
1091 }
1092
1093 static void unregister_leds(struct led_t *l)
1094 {
1095 for(; l->name; l++)
1096 remove_proc_entry(l->name, leds);
1097
1098 remove_proc_entry("led", diag);
1099 }
1100
1101 static void set_led_extif(struct led_t *led)
1102 {
1103 gpio_set_extif(led->gpio, led->state);
1104 }
1105
1106 static void led_flash(unsigned long dummy) {
1107 struct led_t *l;
1108 u32 mask = 0;
1109 u8 extif_blink = 0;
1110
1111 for (l = platform.leds; l->name; l++) {
1112 if (l->flash) {
1113 if (l->gpio & GPIO_TYPE_EXTIF) {
1114 extif_blink = 1;
1115 l->state = !l->state;
1116 set_led_extif(l);
1117 } else {
1118 mask |= l->gpio;
1119 }
1120 }
1121 }
1122
1123 mask &= ~gpiomask;
1124 if (mask) {
1125 u32 val = ~gpio_in();
1126
1127 gpio_outen(mask, mask);
1128 gpio_control(mask, 0);
1129 gpio_out(mask, val);
1130 }
1131 if (mask || extif_blink) {
1132 mod_timer(&led_timer, jiffies + FLASH_TIME);
1133 }
1134 }
1135
1136 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1137 {
1138 #ifdef LINUX_2_4
1139 struct inode *inode = file->f_dentry->d_inode;
1140 struct proc_dir_entry *dent = inode->u.generic_ip;
1141 #else
1142 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1143 #endif
1144 char *page;
1145 int len = 0;
1146
1147 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1148 return -ENOBUFS;
1149
1150 if (dent->data != NULL) {
1151 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1152 switch (handler->type) {
1153 case PROC_LED: {
1154 struct led_t * led = (struct led_t *) handler->ptr;
1155 if (led->flash) {
1156 len = sprintf(page, "f\n");
1157 } else {
1158 if (led->gpio & GPIO_TYPE_EXTIF) {
1159 len = sprintf(page, "%d\n", led->state);
1160 } else {
1161 u32 in = (gpio_in() & led->gpio ? 1 : 0);
1162 u8 p = (led->polarity == NORMAL ? 0 : 1);
1163 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1164 }
1165 }
1166 break;
1167 }
1168 case PROC_MODEL:
1169 len = sprintf(page, "%s\n", platform.name);
1170 break;
1171 case PROC_GPIOMASK:
1172 len = sprintf(page, "0x%04x\n", gpiomask);
1173 break;
1174 }
1175 }
1176 len += 1;
1177
1178 if (*ppos < len) {
1179 len = min_t(int, len - *ppos, count);
1180 if (copy_to_user(buf, (page + *ppos), len)) {
1181 kfree(page);
1182 return -EFAULT;
1183 }
1184 *ppos += len;
1185 } else {
1186 len = 0;
1187 }
1188
1189 kfree(page);
1190 return len;
1191 }
1192
1193
1194 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1195 {
1196 #ifdef LINUX_2_4
1197 struct inode *inode = file->f_dentry->d_inode;
1198 struct proc_dir_entry *dent = inode->u.generic_ip;
1199 #else
1200 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1201 #endif
1202 char *page;
1203 int ret = -EINVAL;
1204
1205 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1206 return -ENOBUFS;
1207
1208 if (copy_from_user(page, buf, count)) {
1209 kfree(page);
1210 return -EINVAL;
1211 }
1212 page[count] = 0;
1213
1214 if (dent->data != NULL) {
1215 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1216 switch (handler->type) {
1217 case PROC_LED: {
1218 struct led_t *led = (struct led_t *) handler->ptr;
1219 int p = (led->polarity == NORMAL ? 0 : 1);
1220
1221 if (page[0] == 'f') {
1222 led->flash = 1;
1223 led_flash(0);
1224 } else {
1225 led->flash = 0;
1226 if (led->gpio & GPIO_TYPE_EXTIF) {
1227 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1228 set_led_extif(led);
1229 } else {
1230 gpio_outen(led->gpio, led->gpio);
1231 gpio_control(led->gpio, 0);
1232 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1233 }
1234 }
1235 break;
1236 }
1237 case PROC_GPIOMASK:
1238 gpiomask = simple_strtoul(page, NULL, 0);
1239
1240 if (platform.buttons) {
1241 unregister_buttons(platform.buttons);
1242 register_buttons(platform.buttons);
1243 }
1244
1245 if (platform.leds) {
1246 unregister_leds(platform.leds);
1247 register_leds(platform.leds);
1248 }
1249 break;
1250 }
1251 ret = count;
1252 }
1253
1254 kfree(page);
1255 return ret;
1256 }
1257
1258 static int __init diag_init(void)
1259 {
1260 static struct proc_dir_entry *p;
1261 static struct platform_t *detected;
1262
1263 detected = platform_detect();
1264 if (!detected) {
1265 printk(MODULE_NAME ": Router model not detected.\n");
1266 return -ENODEV;
1267 }
1268 memcpy(&platform, detected, sizeof(struct platform_t));
1269
1270 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1271 if (platform.platform_init != NULL) {
1272 platform.platform_init();
1273 }
1274
1275 if (!(diag = proc_mkdir("diag", NULL))) {
1276 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1277 return -EINVAL;
1278 }
1279
1280 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1281 p->data = (void *) &proc_model;
1282 p->proc_fops = &diag_proc_fops;
1283 }
1284
1285 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1286 p->data = (void *) &proc_gpiomask;
1287 p->proc_fops = &diag_proc_fops;
1288 }
1289
1290 if (platform.buttons)
1291 register_buttons(platform.buttons);
1292
1293 if (platform.leds)
1294 register_leds(platform.leds);
1295
1296 return 0;
1297 }
1298
1299 static void __exit diag_exit(void)
1300 {
1301 del_timer(&led_timer);
1302
1303 if (platform.buttons)
1304 unregister_buttons(platform.buttons);
1305
1306 if (platform.leds)
1307 unregister_leds(platform.leds);
1308
1309 remove_proc_entry("model", diag);
1310 remove_proc_entry("gpiomask", diag);
1311 remove_proc_entry("diag", NULL);
1312 }
1313
1314 module_init(diag_init);
1315 module_exit(diag_exit);
1316
1317 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1318 MODULE_LICENSE("GPL");