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