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