add diag support for the Buffalo WZR-G300N
[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 * 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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
32 #include <linux/kobject.h>
33 #include <linux/workqueue.h>
34 #define hotplug_path uevent_helper
35 #else
36 #include <linux/tqueue.h>
37 #define INIT_WORK INIT_TQUEUE
38 #define schedule_work schedule_task
39 #define work_struct tq_struct
40 #endif
41
42 #include "gpio.h"
43 #include "diag.h"
44 #define getvar(str) (nvram_get(str)?:"")
45
46 static unsigned int gpiomask = 0;
47 module_param(gpiomask, int, 0644);
48
49 enum {
50 /* Linksys */
51 WAP54GV1,
52 WAP54GV3,
53 WRT54GV1,
54 WRT54G,
55 WRTSL54GS,
56 WRT54G3G,
57 WRT350N,
58
59 /* ASUS */
60 WLHDD,
61 WL300G,
62 WL500G,
63 WL500GD,
64 WL500GP,
65 ASUS_4702,
66
67 /* Buffalo */
68 WBR2_G54,
69 WHR_G54S,
70 WHR_HP_G54,
71 WHR2_A54G54,
72 WLA2_G54L,
73 WZR_G300N,
74 WZR_RS_G54,
75 WZR_RS_G54HP,
76 BUFFALO_UNKNOWN,
77 BUFFALO_UNKNOWN_4710,
78
79 /* Siemens */
80 SE505V1,
81 SE505V2,
82
83 /* US Robotics */
84 USR5461,
85
86 /* Dell */
87 TM2300,
88
89 /* Motorola */
90 WE800G,
91 WR850GV1,
92 WR850GV2V3,
93
94 /* Belkin */
95 BELKIN_UNKNOWN,
96
97 /* Netgear */
98 WGT634U,
99
100 /* Trendware */
101 TEW411BRPP,
102 };
103
104 static struct platform_t __initdata platforms[] = {
105 /* Linksys */
106 [WAP54GV1] = {
107 .name = "Linksys WAP54G V1",
108 .buttons = {
109 { .name = "reset", .gpio = 1 << 0 },
110 },
111 .leds = {
112 { .name = "diag", .gpio = 1 << 3 },
113 { .name = "wlan", .gpio = 1 << 4 },
114 },
115 },
116 [WAP54GV3] = {
117 .name = "Linksys WAP54G V3",
118 .buttons = {
119 /* FIXME: verify this */
120 { .name = "reset", .gpio = 1 << 7 },
121 { .name = "ses", .gpio = 1 << 0 },
122 },
123 .leds = {
124 /* FIXME: diag? */
125 { .name = "ses", .gpio = 1 << 1 },
126 },
127 },
128 [WRT54GV1] = {
129 .name = "Linksys WRT54G V1.x",
130 .buttons = {
131 { .name = "reset", .gpio = 1 << 6 },
132 },
133 .leds = {
134 { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
135 { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
136 },
137 },
138 [WRT54G] = {
139 .name = "Linksys WRT54G/GS/GL",
140 .buttons = {
141 { .name = "reset", .gpio = 1 << 6 },
142 { .name = "ses", .gpio = 1 << 4 },
143 },
144 .leds = {
145 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
146 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
147 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
148 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
149 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
150 },
151 },
152 [WRTSL54GS] = {
153 .name = "Linksys WRTSL54GS",
154 .buttons = {
155 { .name = "reset", .gpio = 1 << 6 },
156 { .name = "ses", .gpio = 1 << 4 },
157 },
158 .leds = {
159 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
160 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
161 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
162 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
163 },
164 },
165 [WRT54G3G] = {
166 .name = "Linksys WRT54G3G",
167 .buttons = {
168 { .name = "reset", .gpio = 1 << 6 },
169 { .name = "3g", .gpio = 1 << 4 },
170 },
171 .leds = {
172 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
173 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
174 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
175 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
176 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
177 },
178 },
179 [WRT350N] = {
180 .name = "Linksys WRT350N",
181 .buttons = {
182 { .name = "reset", .gpio = 1 << 6 },
183 { .name = "ses", .gpio = 1 << 8 },
184 },
185 .leds = {
186 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
187 { .name = "ses", .gpio = 1 << 3, .polarity = REVERSE },
188 },
189 },
190 /* Asus */
191 [WLHDD] = {
192 .name = "ASUS WL-HDD",
193 .buttons = {
194 { .name = "reset", .gpio = 1 << 6 },
195 },
196 .leds = {
197 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
198 },
199 },
200 [WL300G] = {
201 .name = "ASUS WL-300g",
202 .buttons = {
203 { .name = "reset", .gpio = 1 << 6 },
204 },
205 .leds = {
206 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
207 },
208 },
209 [WL500G] = {
210 .name = "ASUS WL-500g",
211 .buttons = {
212 { .name = "reset", .gpio = 1 << 6 },
213 },
214 .leds = {
215 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
216 },
217 },
218 [WL500GD] = {
219 .name = "ASUS WL-500g Deluxe",
220 .buttons = {
221 { .name = "reset", .gpio = 1 << 6 },
222 },
223 .leds = {
224 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
225 },
226 },
227 [WL500GP] = {
228 .name = "ASUS WL-500g Premium",
229 .buttons = {
230 { .name = "reset", .gpio = 1 << 0 },
231 { .name = "ses", .gpio = 1 << 4 },
232 },
233 .leds = {
234 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
235 },
236 },
237 [ASUS_4702] = {
238 .name = "ASUS (unknown, BCM4702)",
239 .buttons = {
240 { .name = "reset", .gpio = 1 << 6 },
241 },
242 .leds = {
243 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
244 },
245 },
246 /* Buffalo */
247 [WHR_G54S] = {
248 .name = "Buffalo WHR-G54S",
249 .buttons = {
250 { .name = "reset", .gpio = 1 << 4 },
251 { .name = "bridge", .gpio = 1 << 5 },
252 { .name = "ses", .gpio = 1 << 0 },
253 },
254 .leds = {
255 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
256 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
257 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
258 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
259 },
260 },
261 [WBR2_G54] = {
262 .name = "Buffalo WBR2-G54",
263 /* FIXME: verify */
264 .buttons = {
265 { .name = "reset", .gpio = 1 << 7 },
266 },
267 .leds = {
268 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
269 },
270 },
271 [WHR_HP_G54] = {
272 .name = "Buffalo WHR-HP-G54",
273 .buttons = {
274 { .name = "reset", .gpio = 1 << 4 },
275 { .name = "bridge", .gpio = 1 << 5 },
276 { .name = "ses", .gpio = 1 << 0 },
277 },
278 .leds = {
279 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
280 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
281 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
282 },
283 },
284 [WHR2_A54G54] = {
285 .name = "Buffalo WHR2-A54G54",
286 .buttons = {
287 { .name = "reset", .gpio = 1 << 4 },
288 },
289 .leds = {
290 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
291 },
292 },
293 [WLA2_G54L] = {
294 .name = "Buffalo WLA2-G54L",
295 /* FIXME: verify */
296 .buttons = {
297 { .name = "reset", .gpio = 1 << 7 },
298 },
299 .leds = {
300 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
301 },
302 },
303 [WZR_G300N] = {
304 .name = "Buffalo WZR-G300N",
305 .buttons = {
306 { .name = "reset", .gpio = 1 << 4 },
307 },
308 .leds = {
309 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
310 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
311 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
312 },
313 },
314 [WZR_RS_G54] = {
315 .name = "Buffalo WZR-RS-G54",
316 .buttons = {
317 { .name = "ses", .gpio = 1 << 0 },
318 { .name = "reset", .gpio = 1 << 4 },
319 },
320 .leds = {
321 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
322 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
323 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
324 },
325 },
326 [WZR_RS_G54HP] = {
327 .name = "Buffalo WZR-RS-G54HP",
328 .buttons = {
329 { .name = "ses", .gpio = 1 << 0 },
330 { .name = "reset", .gpio = 1 << 4 },
331 },
332 .leds = {
333 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
334 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
335 { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
336 },
337 },
338 [BUFFALO_UNKNOWN] = {
339 .name = "Buffalo (unknown)",
340 .buttons = {
341 { .name = "reset", .gpio = 1 << 7 },
342 },
343 .leds = {
344 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
345 },
346 },
347 [BUFFALO_UNKNOWN_4710] = {
348 .name = "Buffalo (unknown, BCM4710)",
349 .buttons = {
350 { .name = "reset", .gpio = 1 << 4 },
351 },
352 .leds = {
353 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
354 },
355 },
356 /* Siemens */
357 [SE505V1] = {
358 .name = "Siemens SE505 V1",
359 .buttons = {
360 /* No usable buttons */
361 },
362 .leds = {
363 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
364 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
365 },
366 },
367 [SE505V2] = {
368 .name = "Siemens SE505 V2",
369 .buttons = {
370 /* No usable buttons */
371 },
372 .leds = {
373 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
374 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
375 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
376 },
377 },
378 /* US Robotics */
379 [USR5461] = {
380 .name = "U.S. Robotics USR5461",
381 .buttons = {
382 /* No usable buttons */
383 },
384 .leds = {
385 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
386 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
387 },
388 },
389 /* Dell */
390 [TM2300] = {
391 .name = "Dell TrueMobile 2300",
392 .buttons = {
393 { .name = "reset", .gpio = 1 << 0 },
394 },
395 .leds = {
396 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
397 },
398 },
399 /* Motorola */
400 [WE800G] = {
401 .name = "Motorola WE800G",
402 .buttons = {
403 { .name = "reset", .gpio = 1 << 0 },
404 },
405 .leds = {
406 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
407 { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
408 { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
409 },
410 },
411 [WR850GV1] = {
412 .name = "Motorola WR850G V1",
413 .buttons = {
414 { .name = "reset", .gpio = 1 << 0 },
415 },
416 .leds = {
417 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
418 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
419 { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
420 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
421 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
422 },
423 },
424 [WR850GV2V3] = {
425 .name = "Motorola WR850G V2/V3",
426 .buttons = {
427 { .name = "reset", .gpio = 1 << 5 },
428 },
429 .leds = {
430 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
431 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
432 { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
433 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
434 },
435 },
436 /* Belkin */
437 [BELKIN_UNKNOWN] = {
438 .name = "Belkin (unknown)",
439 /* FIXME: verify & add detection */
440 .buttons = {
441 { .name = "reset", .gpio = 1 << 7 },
442 },
443 .leds = {
444 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
445 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
446 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
447 },
448 },
449 /* Netgear */
450 [WGT634U] = {
451 .name = "Netgear WGT634U",
452 .buttons = {
453 { .name = "reset", .gpio = 1 << 2 },
454 },
455 .leds = {
456 { .name = "power", .gpio = 1 << 3, .polarity = REVERSE },
457 },
458 },
459 /* Trendware */
460 [TEW411BRPP] = {
461 .name = "Trendware TEW411BRP+",
462 .buttons = {
463 { /* No usable buttons */ },
464 },
465 .leds = {
466 { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
467 { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
468 { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
469 },
470 },
471 };
472
473 static struct platform_t __init *platform_detect(void)
474 {
475 char *boardnum, *boardtype, *buf;
476
477 boardnum = getvar("boardnum");
478 boardtype = getvar("boardtype");
479 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
480 /* CFE based - newer hardware */
481 if (!strcmp(boardnum, "42")) { /* Linksys */
482 if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), 1))
483 return &platforms[WRT350N];
484
485 if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
486 return &platforms[WRT54G3G];
487
488 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
489 return &platforms[WRTSL54GS];
490
491 /* default to WRT54G */
492 return &platforms[WRT54G];
493 }
494
495 if (!strcmp(boardnum, "45")) { /* ASUS */
496 if (!strcmp(boardtype,"0x042f"))
497 return &platforms[WL500GP];
498 else
499 return &platforms[WL500GD];
500 }
501
502 if (!strcmp(boardnum, "10496"))
503 return &platforms[USR5461];
504 } else { /* PMON based - old stuff */
505 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
506 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
507 if (!strncmp(getvar("ModelId"),"WE800G", 6))
508 return &platforms[WE800G];
509 else
510 return &platforms[WR850GV1];
511 }
512 if (!strncmp(boardtype, "bcm94710dev", 11)) {
513 if (!strcmp(boardnum, "42"))
514 return &platforms[WRT54GV1];
515 if (simple_strtoul(boardnum, NULL, 0) == 2)
516 return &platforms[WAP54GV1];
517 }
518 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
519 return &platforms[WL500G];
520 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
521 /* Either WL-300g or WL-HDD, do more extensive checks */
522 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
523 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
524 return &platforms[WLHDD];
525 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
526 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
527 return &platforms[WL300G];
528 }
529
530 /* unknown asus stuff, probably bcm4702 */
531 if (!strncmp(boardnum, "asusX", 5))
532 return &platforms[ASUS_4702];
533 }
534
535 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
536 /* Buffalo hardware, check id for specific hardware matches */
537 if (!strcmp(buf, "29bb0332"))
538 return &platforms[WBR2_G54];
539 if (!strcmp(buf, "29129"))
540 return &platforms[WLA2_G54L];
541 if (!strcmp(buf, "30189"))
542 return &platforms[WHR_HP_G54];
543 if (!strcmp(buf, "30182"))
544 return &platforms[WHR_G54S];
545 if (!strcmp(buf, "290441dd"))
546 return &platforms[WHR2_A54G54];
547 if (!strcmp(buf, "31120"))
548 return &platforms[WZR_G300N];
549 if (!strcmp(buf, "30083"))
550 return &platforms[WZR_RS_G54];
551 if (!strcmp(buf, "30103"))
552 return &platforms[WZR_RS_G54HP];
553 }
554
555 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
556 if (!strncmp(boardtype, "bcm94710ap", 10))
557 return &platforms[BUFFALO_UNKNOWN_4710];
558 else
559 return &platforms[BUFFALO_UNKNOWN];
560 }
561
562 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
563 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
564
565 return &platforms[WR850GV2V3];
566 }
567
568 if (!strcmp(boardnum, "44")) { /* Trendware TEW-411BRP+ */
569 return &platforms[TEW411BRPP];
570 }
571
572 /* not found */
573 return NULL;
574 }
575
576 static void register_buttons(struct button_t *b)
577 {
578 for (; b->name; b++)
579 platform.button_mask |= b->gpio;
580
581 platform.button_mask &= ~gpiomask;
582
583 gpio_outen(platform.button_mask, 0);
584 gpio_control(platform.button_mask, 0);
585 platform.button_polarity = gpio_in() & platform.button_mask;
586 gpio_intpolarity(platform.button_mask, platform.button_polarity);
587 gpio_intmask(platform.button_mask, platform.button_mask);
588
589 gpio_set_irqenable(1, button_handler);
590 }
591
592 static void unregister_buttons(struct button_t *b)
593 {
594 gpio_intmask(platform.button_mask, 0);
595
596 gpio_set_irqenable(0, button_handler);
597 }
598
599 static void hotplug_button(struct event_t *event)
600 {
601 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
602 call_usermodehelper (event->argv[0], event->argv, event->envp, 1);
603 #else
604 call_usermodehelper (event->argv[0], event->argv, event->envp);
605 #endif
606 kfree(event);
607 }
608
609 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
610 {
611 struct button_t *b;
612 u32 in, changed;
613
614 in = gpio_in() & platform.button_mask;
615 gpio_intpolarity(platform.button_mask, in);
616 changed = platform.button_polarity ^ in;
617 platform.button_polarity = in;
618
619 changed &= ~gpio_outen(0, 0);
620
621 for (b = platform.buttons; b->name; b++) {
622 struct event_t *event;
623
624 if (!(b->gpio & changed)) continue;
625
626 b->pressed ^= 1;
627
628 if ((event = (struct event_t *)kmalloc (sizeof(struct event_t), GFP_ATOMIC))) {
629 int i;
630 char *scratch = event->buf;
631
632 i = 0;
633 event->argv[i++] = hotplug_path;
634 event->argv[i++] = "button";
635 event->argv[i] = 0;
636
637 i = 0;
638 event->envp[i++] = "HOME=/";
639 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
640 event->envp[i++] = scratch;
641 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
642 event->envp[i++] = scratch;
643 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
644 event->envp[i++] = scratch;
645 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
646 event->envp[i] = 0;
647
648 INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
649 schedule_work(&event->wq);
650 }
651
652 b->seen = jiffies;
653 }
654 return IRQ_HANDLED;
655 }
656
657 static void register_leds(struct led_t *l)
658 {
659 struct proc_dir_entry *p;
660 u32 mask = 0;
661 u32 val = 0;
662
663 leds = proc_mkdir("led", diag);
664 if (!leds)
665 return;
666
667 for(; l->name; l++) {
668 if (l->gpio & gpiomask)
669 continue;
670
671 if (l->gpio & GPIO_TYPE_EXTIF) {
672 l->state = 0;
673 set_led_extif(l);
674 } else {
675 mask |= l->gpio;
676 val |= (l->polarity == NORMAL)?0:l->gpio;
677 }
678
679 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
680 l->proc.type = PROC_LED;
681 l->proc.ptr = l;
682 p->data = (void *) &l->proc;
683 p->proc_fops = &diag_proc_fops;
684 }
685 }
686
687 gpio_outen(mask, mask);
688 gpio_control(mask, 0);
689 gpio_out(mask, val);
690 }
691
692 static void unregister_leds(struct led_t *l)
693 {
694 for(; l->name; l++)
695 remove_proc_entry(l->name, leds);
696
697 remove_proc_entry("led", diag);
698 }
699
700 static void set_led_extif(struct led_t *led)
701 {
702 gpio_set_extif(led->gpio, led->state);
703 }
704
705 static void led_flash(unsigned long dummy) {
706 struct led_t *l;
707 u32 mask = 0;
708 u8 extif_blink = 0;
709
710 for (l = platform.leds; l->name; l++) {
711 if (l->flash) {
712 if (l->gpio & GPIO_TYPE_EXTIF) {
713 extif_blink = 1;
714 l->state = !l->state;
715 set_led_extif(l);
716 } else {
717 mask |= l->gpio;
718 }
719 }
720 }
721
722 mask &= ~gpiomask;
723 if (mask) {
724 u32 val = ~gpio_in();
725
726 gpio_outen(mask, mask);
727 gpio_control(mask, 0);
728 gpio_out(mask, val);
729 }
730 if (mask || extif_blink) {
731 mod_timer(&led_timer, jiffies + FLASH_TIME);
732 }
733 }
734
735 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
736 {
737 #ifdef LINUX_2_4
738 struct inode *inode = file->f_dentry->d_inode;
739 struct proc_dir_entry *dent = inode->u.generic_ip;
740 #else
741 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
742 #endif
743 char *page;
744 int len = 0;
745
746 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
747 return -ENOBUFS;
748
749 if (dent->data != NULL) {
750 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
751 switch (handler->type) {
752 case PROC_LED: {
753 struct led_t * led = (struct led_t *) handler->ptr;
754 if (led->flash) {
755 len = sprintf(page, "f\n");
756 } else {
757 if (led->gpio & GPIO_TYPE_EXTIF) {
758 len = sprintf(page, "%d\n", led->state);
759 } else {
760 u32 in = (gpio_in() & led->gpio ? 1 : 0);
761 u8 p = (led->polarity == NORMAL ? 0 : 1);
762 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
763 }
764 }
765 break;
766 }
767 case PROC_MODEL:
768 len = sprintf(page, "%s\n", platform.name);
769 break;
770 case PROC_GPIOMASK:
771 len = sprintf(page, "0x%04x\n", gpiomask);
772 break;
773 }
774 }
775 len += 1;
776
777 if (*ppos < len) {
778 len = min_t(int, len - *ppos, count);
779 if (copy_to_user(buf, (page + *ppos), len)) {
780 kfree(page);
781 return -EFAULT;
782 }
783 *ppos += len;
784 } else {
785 len = 0;
786 }
787
788 return len;
789 }
790
791
792 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
793 {
794 #ifdef LINUX_2_4
795 struct inode *inode = file->f_dentry->d_inode;
796 struct proc_dir_entry *dent = inode->u.generic_ip;
797 #else
798 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
799 #endif
800 char *page;
801 int ret = -EINVAL;
802
803 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
804 return -ENOBUFS;
805
806 if (copy_from_user(page, buf, count)) {
807 kfree(page);
808 return -EINVAL;
809 }
810 page[count] = 0;
811
812 if (dent->data != NULL) {
813 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
814 switch (handler->type) {
815 case PROC_LED: {
816 struct led_t *led = (struct led_t *) handler->ptr;
817 int p = (led->polarity == NORMAL ? 0 : 1);
818
819 if (page[0] == 'f') {
820 led->flash = 1;
821 led_flash(0);
822 } else {
823 led->flash = 0;
824 if (led->gpio & GPIO_TYPE_EXTIF) {
825 led->state = p ^ ((page[0] == '1') ? 1 : 0);
826 set_led_extif(led);
827 } else {
828 gpio_outen(led->gpio, led->gpio);
829 gpio_control(led->gpio, 0);
830 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
831 }
832 }
833 break;
834 }
835 case PROC_GPIOMASK:
836 gpiomask = simple_strtoul(page, NULL, 0);
837
838 if (platform.buttons) {
839 unregister_buttons(platform.buttons);
840 register_buttons(platform.buttons);
841 }
842
843 if (platform.leds) {
844 unregister_leds(platform.leds);
845 register_leds(platform.leds);
846 }
847 break;
848 }
849 ret = count;
850 }
851
852 kfree(page);
853 return ret;
854 }
855
856 static int __init diag_init(void)
857 {
858 static struct proc_dir_entry *p;
859 static struct platform_t *detected;
860
861 detected = platform_detect();
862 if (!detected) {
863 printk(MODULE_NAME ": Router model not detected.\n");
864 return -ENODEV;
865 }
866 memcpy(&platform, detected, sizeof(struct platform_t));
867
868 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
869
870 if (!(diag = proc_mkdir("diag", NULL))) {
871 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
872 return -EINVAL;
873 }
874
875 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
876 p->data = (void *) &proc_model;
877 p->proc_fops = &diag_proc_fops;
878 }
879
880 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
881 p->data = (void *) &proc_gpiomask;
882 p->proc_fops = &diag_proc_fops;
883 }
884
885 if (platform.buttons)
886 register_buttons(platform.buttons);
887
888 if (platform.leds)
889 register_leds(platform.leds);
890
891 return 0;
892 }
893
894 static void __exit diag_exit(void)
895 {
896
897 del_timer(&led_timer);
898
899 if (platform.buttons)
900 unregister_buttons(platform.buttons);
901
902 if (platform.leds)
903 unregister_leds(platform.leds);
904
905 remove_proc_entry("model", diag);
906 remove_proc_entry("gpiomask", diag);
907 remove_proc_entry("diag", NULL);
908 }
909
910 module_init(diag_init);
911 module_exit(diag_exit);
912
913 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
914 MODULE_LICENSE("GPL");