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