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