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