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