2 * diag.c - GPIO interface driver for Broadcom boards
4 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
5 * Felix Fietkau <nbd@openwrt.org>
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.
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.
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.
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>
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
36 #include <linux/tqueue.h>
37 #define INIT_WORK INIT_TQUEUE
38 #define schedule_work schedule_task
39 #define work_struct tq_struct
44 #define getvar(str) (nvram_get(str)?:"")
46 static unsigned int gpiomask
= 0;
47 module_param(gpiomask
, int, 0644);
113 static void __init
bcm4780_init(void) {
116 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
117 printk(MODULE_NAME
": Spinning up HDD and enabling leds\n");
118 gpio_outen(pin
, pin
);
119 gpio_control(pin
, 0);
122 /* Wait 5s, so the HDD can spin up */
123 set_current_state(TASK_INTERRUPTIBLE
);
124 schedule_timeout(HZ
* 5);
127 static struct platform_t __initdata platforms
[] = {
130 .name
= "Linksys WAP54G V1",
132 { .name
= "reset", .gpio
= 1 << 0 },
135 { .name
= "diag", .gpio
= 1 << 3 },
136 { .name
= "wlan", .gpio
= 1 << 4 },
140 .name
= "Linksys WAP54G V3",
142 /* FIXME: verify this */
143 { .name
= "reset", .gpio
= 1 << 7 },
144 { .name
= "ses", .gpio
= 1 << 0 },
148 { .name
= "ses", .gpio
= 1 << 1 },
152 .name
= "Linksys WRT54G V1.x",
154 { .name
= "reset", .gpio
= 1 << 6 },
157 { .name
= "diag", .gpio
= 0x13 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
158 { .name
= "dmz", .gpio
= 0x12 | GPIO_TYPE_EXTIF
, .polarity
= NORMAL
},
162 .name
= "Linksys WRT54G/GS/GL",
164 { .name
= "reset", .gpio
= 1 << 6 },
165 { .name
= "ses", .gpio
= 1 << 4 },
168 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
169 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
170 { .name
= "ses_white", .gpio
= 1 << 2, .polarity
= REVERSE
},
171 { .name
= "ses_orange", .gpio
= 1 << 3, .polarity
= REVERSE
},
172 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
176 .name
= "Linksys WRTSL54GS",
178 { .name
= "reset", .gpio
= 1 << 6 },
179 { .name
= "ses", .gpio
= 1 << 4 },
182 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
183 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
184 { .name
= "ses_white", .gpio
= 1 << 5, .polarity
= REVERSE
},
185 { .name
= "ses_orange", .gpio
= 1 << 7, .polarity
= REVERSE
},
189 .name
= "Linksys WRT54G3G",
191 { .name
= "reset", .gpio
= 1 << 6 },
192 { .name
= "3g", .gpio
= 1 << 4 },
195 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
196 { .name
= "dmz", .gpio
= 1 << 7, .polarity
= REVERSE
},
197 { .name
= "3g_green", .gpio
= 1 << 2, .polarity
= NORMAL
},
198 { .name
= "3g_blue", .gpio
= 1 << 3, .polarity
= NORMAL
},
199 { .name
= "3g_blink", .gpio
= 1 << 5, .polarity
= NORMAL
},
203 .name
= "Linksys WRT350N",
205 { .name
= "reset", .gpio
= 1 << 6 },
206 { .name
= "ses", .gpio
= 1 << 8 },
209 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
210 { .name
= "ses", .gpio
= 1 << 3, .polarity
= REVERSE
},
215 .name
= "ASUS WL-HDD",
217 { .name
= "reset", .gpio
= 1 << 6 },
220 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
221 { .name
= "usb", .gpio
= 1 << 2, .polarity
= NORMAL
},
225 .name
= "ASUS WL-300g",
227 { .name
= "reset", .gpio
= 1 << 6 },
230 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
234 .name
= "ASUS WL-500g",
236 { .name
= "reset", .gpio
= 1 << 6 },
239 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
243 .name
= "ASUS WL-500g Deluxe",
245 { .name
= "reset", .gpio
= 1 << 6 },
248 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
252 .name
= "ASUS WL-500g Premium",
254 { .name
= "reset", .gpio
= 1 << 0 },
255 { .name
= "ses", .gpio
= 1 << 4 },
258 { .name
= "power", .gpio
= 1 << 1, .polarity
= REVERSE
},
262 .name
= "ASUS WL-500W",
264 { .name
= "reset", .gpio
= 1 << 6 },
265 { .name
= "ses", .gpio
= 1 << 7 },
268 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
272 .name
= "ASUS (unknown, BCM4702)",
274 { .name
= "reset", .gpio
= 1 << 6 },
277 { .name
= "power", .gpio
= 1 << 0, .polarity
= REVERSE
},
281 .name
= "ASUS WL-700gE",
283 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
284 { .name
= "ses", .gpio
= 1 << 4 }, // on back, actual name ezsetup
285 { .name
= "power", .gpio
= 1 << 0 }, // on front
286 { .name
= "copy", .gpio
= 1 << 6 }, // on front
290 // GPIO that controls power led also enables/disables some essential functions
293 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
}, // actual name power
295 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
297 .platform_init
= bcm4780_init
,
301 .name
= "Buffalo WHR-G54S",
303 { .name
= "reset", .gpio
= 1 << 4 },
304 { .name
= "bridge", .gpio
= 1 << 5 },
305 { .name
= "ses", .gpio
= 1 << 0 },
308 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
309 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
310 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
311 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
312 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
316 .name
= "Buffalo WBR2-G54",
319 { .name
= "reset", .gpio
= 1 << 7 },
322 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
326 .name
= "Buffalo WHR-HP-G54",
328 { .name
= "reset", .gpio
= 1 << 4 },
329 { .name
= "bridge", .gpio
= 1 << 5 },
330 { .name
= "ses", .gpio
= 1 << 0 },
333 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
334 { .name
= "internal", .gpio
= 1 << 3, .polarity
= REVERSE
},
335 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
336 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
337 { .name
= "wlan", .gpio
= 1 << 2, .polarity
= REVERSE
},
341 .name
= "Buffalo WHR2-A54G54",
343 { .name
= "reset", .gpio
= 1 << 4 },
346 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
350 .name
= "Buffalo WLA2-G54L",
353 { .name
= "reset", .gpio
= 1 << 7 },
356 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
360 .name
= "Buffalo WZR-G300N",
362 { .name
= "reset", .gpio
= 1 << 4 },
365 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
366 { .name
= "bridge", .gpio
= 1 << 1, .polarity
= REVERSE
},
367 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
371 .name
= "Buffalo WZR-RS-G54",
373 { .name
= "ses", .gpio
= 1 << 0 },
374 { .name
= "reset", .gpio
= 1 << 4 },
377 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
378 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
379 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
383 .name
= "Buffalo WZR-RS-G54HP",
385 { .name
= "ses", .gpio
= 1 << 0 },
386 { .name
= "reset", .gpio
= 1 << 4 },
389 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
390 { .name
= "ses", .gpio
= 1 << 6, .polarity
= REVERSE
},
391 { .name
= "vpn", .gpio
= 1 << 1, .polarity
= REVERSE
},
394 [BUFFALO_UNKNOWN
] = {
395 .name
= "Buffalo (unknown)",
397 { .name
= "reset", .gpio
= 1 << 7 },
400 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
403 [BUFFALO_UNKNOWN_4710
] = {
404 .name
= "Buffalo (unknown, BCM4710)",
406 { .name
= "reset", .gpio
= 1 << 4 },
409 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
},
414 .name
= "Siemens SE505 V1",
416 /* No usable buttons */
419 { .name
= "dmz", .gpio
= 1 << 4, .polarity
= REVERSE
},
420 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
424 .name
= "Siemens SE505 V2",
426 /* No usable buttons */
429 { .name
= "power", .gpio
= 1 << 5, .polarity
= REVERSE
},
430 { .name
= "dmz", .gpio
= 1 << 0, .polarity
= REVERSE
},
431 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= REVERSE
},
436 .name
= "U.S. Robotics USR5461",
438 /* No usable buttons */
441 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
442 { .name
= "printer", .gpio
= 1 << 1, .polarity
= REVERSE
},
447 .name
= "Dell TrueMobile 2300",
449 { .name
= "reset", .gpio
= 1 << 0 },
452 { .name
= "wlan", .gpio
= 1 << 6, .polarity
= REVERSE
},
453 { .name
= "power", .gpio
= 1 << 7, .polarity
= REVERSE
},
458 .name
= "Motorola WE800G",
460 { .name
= "reset", .gpio
= 1 << 0 },
463 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
464 { .name
= "diag", .gpio
= 1 << 2, .polarity
= REVERSE
},
465 { .name
= "wlan_amber", .gpio
= 1 << 1, .polarity
= NORMAL
},
469 .name
= "Motorola WR850G V1",
471 { .name
= "reset", .gpio
= 1 << 0 },
474 { .name
= "power", .gpio
= 1 << 4, .polarity
= NORMAL
},
475 { .name
= "diag", .gpio
= 1 << 3, .polarity
= REVERSE
},
476 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= NORMAL
},
477 { .name
= "wlan_red", .gpio
= 1 << 5, .polarity
= REVERSE
},
478 { .name
= "wlan_green", .gpio
= 1 << 7, .polarity
= REVERSE
},
482 .name
= "Motorola WR850G V2/V3",
484 { .name
= "reset", .gpio
= 1 << 5 },
487 { .name
= "power", .gpio
= 1 << 1, .polarity
= NORMAL
},
488 { .name
= "wlan", .gpio
= 1 << 0, .polarity
= REVERSE
},
489 { .name
= "dmz", .gpio
= 1 << 6, .polarity
= REVERSE
},
490 { .name
= "diag", .gpio
= 1 << 7, .polarity
= REVERSE
},
495 .name
= "Belkin (unknown)",
496 /* FIXME: verify & add detection */
498 { .name
= "reset", .gpio
= 1 << 7 },
501 { .name
= "power", .gpio
= 1 << 5, .polarity
= NORMAL
},
502 { .name
= "wlan", .gpio
= 1 << 3, .polarity
= NORMAL
},
503 { .name
= "connected", .gpio
= 1 << 0, .polarity
= NORMAL
},
508 .name
= "Netgear WGT634U",
510 { .name
= "reset", .gpio
= 1 << 2 },
513 { .name
= "power", .gpio
= 1 << 3, .polarity
= NORMAL
},
518 .name
= "Trendware TEW411BRP+",
520 { /* No usable buttons */ },
523 { .name
= "power", .gpio
= 1 << 7, .polarity
= NORMAL
},
524 { .name
= "wlan", .gpio
= 1 << 1, .polarity
= NORMAL
},
525 { .name
= "bridge", .gpio
= 1 << 6, .polarity
= NORMAL
},
530 .name
= "SimpleTech SimpleShare NAS",
532 { .name
= "reset", .gpio
= 1 << 7 }, // on back, hardwired, always resets device regardless OS state
533 { .name
= "power", .gpio
= 1 << 0 }, // on back
536 { .name
= "diag", .gpio
= 1 << 1, .polarity
= REVERSE
}, // actual name ready
538 .platform_init
= bcm4780_init
,
542 .name
= "D-Link DIR-130",
544 { .name
= "reset", .gpio
= 1 << 3},
545 { .name
= "reserved", .gpio
= 1 << 7},
548 { .name
= "diag", .gpio
= 1 << 0},
549 { .name
= "blue", .gpio
= 1 << 6},
553 .name
= "D-Link DIR-330",
555 { .name
= "reset", .gpio
= 1 << 3},
556 { .name
= "reserved", .gpio
= 1 << 7},
559 { .name
= "diag", .gpio
= 1 << 0},
560 { .name
= "usb", .gpio
= 1 << 4},
561 { .name
= "blue", .gpio
= 1 << 6},
566 static struct platform_t __init
*platform_detect(void)
568 char *boardnum
, *boardtype
, *buf
;
570 boardnum
= getvar("boardnum");
571 boardtype
= getvar("boardtype");
573 if (strcmp(getvar("nvram_type"), "cfe") == 0)
574 return &platforms
[WGT634U
];
576 if (strncmp(getvar("model_no"), "WL700",5) == 0)
577 return &platforms
[WL700GE
];
579 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
580 /* CFE based - newer hardware */
581 if (!strcmp(boardnum
, "42")) { /* Linksys */
582 if (!strcmp(boardtype
, "0x478") && !strcmp(getvar("cardbus"), "1"))
583 return &platforms
[WRT350N
];
585 if (!strcmp(boardtype
, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
586 return &platforms
[WRT54G3G
];
588 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
589 return &platforms
[WRTSL54GS
];
591 /* default to WRT54G */
592 return &platforms
[WRT54G
];
595 if (!strcmp(boardnum
, "45")) { /* ASUS */
596 if (!strcmp(boardtype
,"0x042f"))
597 return &platforms
[WL500GP
];
598 else if (!strcmp(boardtype
,"0x0472"))
599 return &platforms
[WL500W
];
601 return &platforms
[WL500GD
];
604 if (!strcmp(boardnum
, "10496"))
605 return &platforms
[USR5461
];
608 if (!strcmp(getvar("model_name"), "DIR-130"))
609 return &platforms
[DIR130
];
610 if (!strcmp(getvar("model_name"), "DIR-330"))
611 return &platforms
[DIR330
];
613 } else { /* PMON based - old stuff */
615 /* Dell TrueMobile 2300 */
616 if (!strcmp(getvar("ModelId"),"WX-5565"))
617 return &platforms
[TM2300
];
619 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL
, 0) == 9) &&
620 (simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 30)) {
621 if (!strncmp(getvar("ModelId"),"WE800G", 6))
622 return &platforms
[WE800G
];
624 return &platforms
[WR850GV1
];
626 if (!strncmp(boardtype
, "bcm94710dev", 11)) {
627 if (!strcmp(boardnum
, "42"))
628 return &platforms
[WRT54GV1
];
629 if (simple_strtoul(boardnum
, NULL
, 0) == 2)
630 return &platforms
[WAP54GV1
];
632 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
633 return &platforms
[WL500G
];
634 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
635 /* Either WL-300g or WL-HDD, do more extensive checks */
636 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
637 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 1))
638 return &platforms
[WLHDD
];
639 if ((simple_strtoul(getvar("et0phyaddr"), NULL
, 0) == 0) &&
640 (simple_strtoul(getvar("et1phyaddr"), NULL
, 0) == 10))
641 return &platforms
[WL300G
];
644 /* unknown asus stuff, probably bcm4702 */
645 if (!strncmp(boardnum
, "asusX", 5))
646 return &platforms
[ASUS_4702
];
649 if ((buf
= (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
650 /* Buffalo hardware, check id for specific hardware matches */
651 if (!strcmp(buf
, "29bb0332"))
652 return &platforms
[WBR2_G54
];
653 if (!strcmp(buf
, "29129"))
654 return &platforms
[WLA2_G54L
];
655 if (!strcmp(buf
, "30189"))
656 return &platforms
[WHR_HP_G54
];
657 if (!strcmp(buf
, "30182"))
658 return &platforms
[WHR_G54S
];
659 if (!strcmp(buf
, "290441dd"))
660 return &platforms
[WHR2_A54G54
];
661 if (!strcmp(buf
, "31120"))
662 return &platforms
[WZR_G300N
];
663 if (!strcmp(buf
, "30083"))
664 return &platforms
[WZR_RS_G54
];
665 if (!strcmp(buf
, "30103"))
666 return &platforms
[WZR_RS_G54HP
];
669 if (buf
|| !strcmp(boardnum
, "00")) {/* probably buffalo */
670 if (!strncmp(boardtype
, "bcm94710ap", 10))
671 return &platforms
[BUFFALO_UNKNOWN_4710
];
673 return &platforms
[BUFFALO_UNKNOWN
];
676 if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
677 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
679 return &platforms
[WR850GV2V3
];
682 if (!strcmp(boardnum
, "44")) { /* Trendware TEW-411BRP+ */
683 return &platforms
[TEW411BRPP
];
686 if (!strncmp(boardnum
, "04FN52", 6)) /* SimpleTech SimpleShare */
687 return &platforms
[STI_NAS
];
693 static void register_buttons(struct button_t
*b
)
696 platform
.button_mask
|= b
->gpio
;
698 platform
.button_mask
&= ~gpiomask
;
700 gpio_outen(platform
.button_mask
, 0);
701 gpio_control(platform
.button_mask
, 0);
702 platform
.button_polarity
= gpio_in() & platform
.button_mask
;
703 gpio_intpolarity(platform
.button_mask
, platform
.button_polarity
);
704 gpio_intmask(platform
.button_mask
, platform
.button_mask
);
706 gpio_set_irqenable(1, button_handler
);
709 static void unregister_buttons(struct button_t
*b
)
711 gpio_intmask(platform
.button_mask
, 0);
713 gpio_set_irqenable(0, button_handler
);
716 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
717 static void hotplug_button(struct work_struct
*work
)
719 struct event_t
*event
= container_of(work
, struct event_t
, wq
);
721 static void hotplug_button(struct event_t
*event
)
724 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
725 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
, 1);
727 call_usermodehelper (event
->argv
[0], event
->argv
, event
->envp
);
732 static irqreturn_t
button_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
737 in
= gpio_in() & platform
.button_mask
;
738 gpio_intpolarity(platform
.button_mask
, in
);
739 changed
= platform
.button_polarity
^ in
;
740 platform
.button_polarity
= in
;
742 changed
&= ~gpio_outen(0, 0);
744 for (b
= platform
.buttons
; b
->name
; b
++) {
745 struct event_t
*event
;
747 if (!(b
->gpio
& changed
)) continue;
751 if ((event
= (struct event_t
*)kmalloc (sizeof(struct event_t
), GFP_ATOMIC
))) {
753 char *scratch
= event
->buf
;
756 event
->argv
[i
++] = hotplug_path
;
757 event
->argv
[i
++] = "button";
761 event
->envp
[i
++] = "HOME=/";
762 event
->envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
763 event
->envp
[i
++] = scratch
;
764 scratch
+= sprintf (scratch
, "ACTION=%s", b
->pressed
?"pressed":"released") + 1;
765 event
->envp
[i
++] = scratch
;
766 scratch
+= sprintf (scratch
, "BUTTON=%s", b
->name
) + 1;
767 event
->envp
[i
++] = scratch
;
768 scratch
+= sprintf (scratch
, "SEEN=%ld", (jiffies
- b
->seen
)/HZ
) + 1;
771 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
772 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
774 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
, (void *)event
);
776 schedule_work(&event
->wq
);
784 static void register_leds(struct led_t
*l
)
786 struct proc_dir_entry
*p
;
790 leds
= proc_mkdir("led", diag
);
794 for(; l
->name
; l
++) {
795 if (l
->gpio
& gpiomask
)
798 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
803 val
|= (l
->polarity
== NORMAL
)?0:l
->gpio
;
806 if ((p
= create_proc_entry(l
->name
, S_IRUSR
, leds
))) {
807 l
->proc
.type
= PROC_LED
;
809 p
->data
= (void *) &l
->proc
;
810 p
->proc_fops
= &diag_proc_fops
;
814 gpio_outen(mask
, mask
);
815 gpio_control(mask
, 0);
819 static void unregister_leds(struct led_t
*l
)
822 remove_proc_entry(l
->name
, leds
);
824 remove_proc_entry("led", diag
);
827 static void set_led_extif(struct led_t
*led
)
829 gpio_set_extif(led
->gpio
, led
->state
);
832 static void led_flash(unsigned long dummy
) {
837 for (l
= platform
.leds
; l
->name
; l
++) {
839 if (l
->gpio
& GPIO_TYPE_EXTIF
) {
841 l
->state
= !l
->state
;
851 u32 val
= ~gpio_in();
853 gpio_outen(mask
, mask
);
854 gpio_control(mask
, 0);
857 if (mask
|| extif_blink
) {
858 mod_timer(&led_timer
, jiffies
+ FLASH_TIME
);
862 static ssize_t
diag_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
865 struct inode
*inode
= file
->f_dentry
->d_inode
;
866 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
868 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
873 if ((page
= kmalloc(1024, GFP_KERNEL
)) == NULL
)
876 if (dent
->data
!= NULL
) {
877 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
878 switch (handler
->type
) {
880 struct led_t
* led
= (struct led_t
*) handler
->ptr
;
882 len
= sprintf(page
, "f\n");
884 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
885 len
= sprintf(page
, "%d\n", led
->state
);
887 u32 in
= (gpio_in() & led
->gpio
? 1 : 0);
888 u8 p
= (led
->polarity
== NORMAL
? 0 : 1);
889 len
= sprintf(page
, "%d\n", ((in
^ p
) ? 1 : 0));
895 len
= sprintf(page
, "%s\n", platform
.name
);
898 len
= sprintf(page
, "0x%04x\n", gpiomask
);
905 len
= min_t(int, len
- *ppos
, count
);
906 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
920 static ssize_t
diag_proc_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
923 struct inode
*inode
= file
->f_dentry
->d_inode
;
924 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
926 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
931 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
934 if (copy_from_user(page
, buf
, count
)) {
940 if (dent
->data
!= NULL
) {
941 struct prochandler_t
*handler
= (struct prochandler_t
*) dent
->data
;
942 switch (handler
->type
) {
944 struct led_t
*led
= (struct led_t
*) handler
->ptr
;
945 int p
= (led
->polarity
== NORMAL
? 0 : 1);
947 if (page
[0] == 'f') {
952 if (led
->gpio
& GPIO_TYPE_EXTIF
) {
953 led
->state
= p
^ ((page
[0] == '1') ? 1 : 0);
956 gpio_outen(led
->gpio
, led
->gpio
);
957 gpio_control(led
->gpio
, 0);
958 gpio_out(led
->gpio
, ((p
^ (page
[0] == '1')) ? led
->gpio
: 0));
964 gpiomask
= simple_strtoul(page
, NULL
, 0);
966 if (platform
.buttons
) {
967 unregister_buttons(platform
.buttons
);
968 register_buttons(platform
.buttons
);
972 unregister_leds(platform
.leds
);
973 register_leds(platform
.leds
);
984 static int __init
diag_init(void)
986 static struct proc_dir_entry
*p
;
987 static struct platform_t
*detected
;
989 detected
= platform_detect();
991 printk(MODULE_NAME
": Router model not detected.\n");
994 memcpy(&platform
, detected
, sizeof(struct platform_t
));
996 printk(MODULE_NAME
": Detected '%s'\n", platform
.name
);
997 if (platform
.platform_init
!= NULL
) {
998 platform
.platform_init();
1001 if (!(diag
= proc_mkdir("diag", NULL
))) {
1002 printk(MODULE_NAME
": proc_mkdir on /proc/diag failed\n");
1006 if ((p
= create_proc_entry("model", S_IRUSR
, diag
))) {
1007 p
->data
= (void *) &proc_model
;
1008 p
->proc_fops
= &diag_proc_fops
;
1011 if ((p
= create_proc_entry("gpiomask", S_IRUSR
| S_IWUSR
, diag
))) {
1012 p
->data
= (void *) &proc_gpiomask
;
1013 p
->proc_fops
= &diag_proc_fops
;
1016 if (platform
.buttons
)
1017 register_buttons(platform
.buttons
);
1020 register_leds(platform
.leds
);
1025 static void __exit
diag_exit(void)
1028 del_timer(&led_timer
);
1030 if (platform
.buttons
)
1031 unregister_buttons(platform
.buttons
);
1034 unregister_leds(platform
.leds
);
1036 remove_proc_entry("model", diag
);
1037 remove_proc_entry("gpiomask", diag
);
1038 remove_proc_entry("diag", NULL
);
1041 module_init(diag_init
);
1042 module_exit(diag_exit
);
1044 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1045 MODULE_LICENSE("GPL");