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