continue massive kernel menuconfig reorganization
[openwrt.git] / target / linux / package / diag / diag_led.c
1 /*
2  * diag_led.c - replacement diag module
3  *
4  * Copyright (C) 2004 Mike Baker,
5  *                    Imre Kaloz <kaloz@dune.hu>
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
24 /*
25  * ChangeLog:
26  * 2004/03/28 initial release 
27  * 2004/08/26 asus & buffalo support added
28  * 2005/03/14 asus wl-500g deluxe and buffalo v2 support added
29  * 2005/04/13 added licensing informations
30  * 2005/04/18 base reset polarity off initial readings
31  */
32
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/kernel.h>
36 #include <linux/sysctl.h>
37 #include <asm/io.h>
38 #include <typedefs.h>
39 #include <bcmdevs.h>
40 #include <sbutils.h>
41
42 extern char * nvram_get(const char *name);
43 static void *sbh;
44
45 // v2.x - - - - -
46 #define DIAG_GPIO (1<<1)
47 #define DMZ_GPIO  (1<<7)
48
49 static void set_gpio(uint32 mask, uint32 value) {
50         sb_gpiocontrol(sbh,mask,0);
51         sb_gpioouten(sbh,mask,mask);
52         sb_gpioout(sbh,mask,value);
53 }
54
55 static void v2_set_diag(u8 state) {
56         set_gpio(DIAG_GPIO,state);
57 }
58 static void v2_set_dmz(u8 state) {
59         set_gpio(DMZ_GPIO,state);
60 }
61
62 // v1.x - - - - -
63 #define LED_DIAG   0x13
64 #define LED_DMZ    0x12
65
66 static void v1_set_diag(u8 state) {
67         if (!state) {
68                 *(volatile u8*)(KSEG1ADDR(BCM4710_EUART)+LED_DIAG)=0xFF;
69         } else {
70                 *(volatile u8*)(KSEG1ADDR(BCM4710_EUART)+LED_DIAG);
71         }
72 }
73 static void v1_set_dmz(u8 state) {
74         if (!state) {
75                 *(volatile u8*)(KSEG1ADDR(BCM4710_EUART)+LED_DMZ)=0xFF;
76         } else {
77                 *(volatile u8*)(KSEG1ADDR(BCM4710_EUART)+LED_DMZ);
78         }
79 }
80
81 // - - - - -
82 static void ignore(u8 ignored) {};
83
84 // - - - - -
85 #define BIT_DMZ         0x01
86 #define BIT_DIAG        0x04
87
88 void (*set_diag)(u8 state);
89 void (*set_dmz)(u8 state);
90
91 static unsigned int diag = 0;
92
93 static void diag_change()
94 {
95         set_diag(0xFF); // off
96         set_dmz(0xFF); // off
97
98         if(diag & BIT_DIAG)
99                 set_diag(0x00); // on
100         if(diag & BIT_DMZ)
101                 set_dmz(0x00); // on
102 }
103
104 static int proc_diag(ctl_table *table, int write, struct file *filp,
105                 void *buffer, size_t *lenp)
106 {
107         int r;
108         r = proc_dointvec(table, write, filp, buffer, lenp);
109         if (write && !r) {
110                 diag_change();
111         }
112         return r;
113 }
114
115 // - - - - -
116 static unsigned char reset_gpio = 0;
117 static unsigned char reset_polarity = 0;
118 static unsigned int reset = 0;
119
120 static int proc_reset(ctl_table *table, int write, struct file *filp,
121                 void *buffer, size_t *lenp)
122 {
123
124         if (reset_gpio) {
125                 sb_gpiocontrol(sbh,reset_gpio,reset_gpio);
126                 sb_gpioouten(sbh,reset_gpio,0);
127                 reset=!(sb_gpioin(sbh)&reset_gpio);
128
129                 if (reset_polarity) reset=!reset;
130         } else {
131                 reset=0;
132         }
133
134         return proc_dointvec(table, write, filp, buffer, lenp);
135 }
136
137 // - - - - -
138 static struct ctl_table_header *diag_sysctl_header;
139
140 static ctl_table sys_diag[] = {
141          { 
142            ctl_name: 2000,
143            procname: "diag", 
144            data: &diag,
145            maxlen: sizeof(diag), 
146            mode: 0644,
147            proc_handler: proc_diag
148          },
149          {
150            ctl_name: 2001,
151            procname: "reset",
152            data: &reset,
153            maxlen: sizeof(reset),
154            mode: 0444,
155            proc_handler: proc_reset 
156          },
157          { 0 }
158 };
159
160 static int __init diag_init()
161 {
162         char *buf;
163         u32 board_type;
164         sbh = sb_kattach();
165         sb_gpiosetcore(sbh);
166
167         board_type = sb_boardtype(sbh);
168         printk(KERN_INFO "diag boardtype: %08x\n",board_type);
169
170         set_diag=ignore;
171         set_dmz=ignore;
172         
173         buf=nvram_get("pmon_ver") ?: "";
174         if (((board_type & 0xf00) == 0x400) && (strncmp(buf, "CFE", 3) != 0)) {
175                 buf=nvram_get("boardtype")?:"";
176                 if (!strcmp(buf,"bcm94710dev")) {
177                         buf=nvram_get("boardnum")?:"";
178                         if (!strcmp(buf,"42")) {
179                                 // wrt54g v1.x
180                                 set_diag=v1_set_diag;
181                                 set_dmz=v1_set_dmz;
182                                 reset_gpio=(1<<6);
183                         }
184                         if (!strcmp(buf,"asusX")) {
185                                 //asus wl-500g
186                                 reset_gpio=(1<<6);
187                         }
188                 }
189                 if (!strcmp(buf,"bcm94710ap")) {
190                         buf=nvram_get("boardnum")?:"";
191                         if (!strcmp(buf,"42")) {
192                                 // buffalo
193                                 set_dmz=v2_set_dmz;
194                                 reset_gpio=(1<<4);
195                         }
196                         if (!strcmp(buf,"44")) {
197                                 //dell truemobile
198                                 set_dmz=v2_set_dmz;
199                                 reset_gpio=(1<<0);
200                         }
201                 }
202         } else {
203                 buf=nvram_get("boardnum")?:"";
204                 if (!strcmp(buf,"42")) {
205                         //linksys
206                         set_diag=v2_set_diag;
207                         set_dmz=v2_set_dmz;
208                         reset_gpio=(1<<6);
209                 }
210                 if (!strcmp(buf,"44")) {
211                         //motorola
212                         reset_gpio=(1<<5);
213                 }
214                 if (!strcmp(buf,"00")) {
215                         //buffalo
216                         reset_gpio=(1<<7);
217                 }
218                 if (!strcmp(buf,"45")) {
219                         //wl-500g deluxe
220                         reset_gpio=(1<<6);
221                 }
222         }
223
224         
225         sb_gpiocontrol(sbh,reset_gpio,reset_gpio);
226         sb_gpioouten(sbh,reset_gpio,0);
227         reset_polarity=!(sb_gpioin(sbh)&reset_gpio);
228
229         diag_sysctl_header = register_sysctl_table(sys_diag, 0);
230         diag_change();
231
232         return 0;
233 }
234
235 static void __exit diag_exit()
236 {
237         unregister_sysctl_table(diag_sysctl_header);
238 }
239
240 EXPORT_NO_SYMBOLS;
241 MODULE_AUTHOR("openwrt.org");
242 MODULE_LICENSE("GPL");
243
244 module_init(diag_init);
245 module_exit(diag_exit);