-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpowerled.c
345 lines (317 loc) · 6.29 KB
/
powerled.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/io.h>
#include <stdint.h>
#define REG 0x2E
#define VAL 0x2F
#define LDNREG 0x07
#define GPIOLDN 0x07
#define CMDF9 0xF9
#define CMDF8 0xF8
#define CMD66 0x66
#define CMD62 0x62
#define ADR 0xF8
#define CMD81 0x81
#define CMD80 0x80
//-------------------n100 start-------------------
void enter_conf_mode()
{
if (iopl(3))
{
fprintf(stderr, "Can't get IO permissions\n");
exit(1);
}
outb(0x87, REG);
outb(0x01, REG);
outb(0x55, REG);
outb(0x55, REG);
}
void select_ldn(unsigned char ldn)
{
outb(LDNREG, REG);
outb(ldn, VAL);
}
unsigned char read_reg(unsigned char reg)
{
outb(reg, REG);
return inb(VAL);
}
void write_reg(unsigned char reg, unsigned char val)
{
outb(reg, REG);
outb(val, VAL);
}
void exit_conf_mode()
{
outb(0x02, REG);
outb(0x02, VAL);
if (iopl(0))
{
fprintf(stderr, "Can't drop IO permissions\n");
exit(1);
}
}
void cmd_on()
{
enter_conf_mode();
select_ldn(GPIOLDN);
write_reg(ADR, 0x1F);
write_reg(CMDF9, 0x01);
exit_conf_mode();
}
void cmd_off()
{
enter_conf_mode();
select_ldn(GPIOLDN);
write_reg(ADR, 0x1E);
exit_conf_mode();
}
void cmd_blink(char *interval)
{
unsigned char val;
if (strcmp(interval, "slow") == 0)
{
val = 0x04;
}
else if (strcmp(interval, "medium") == 0)
{
val = 0x02;
}
else if (strcmp(interval, "fast") == 0)
{
val = 0x00;
}
else
{
fprintf(stderr, "Invalid interval\n");
exit(1);
}
enter_conf_mode();
select_ldn(GPIOLDN);
write_reg(ADR, 0x1F);
write_reg(CMDF9, val);
exit_conf_mode();
}
void cmd_status()
{
enter_conf_mode();
select_ldn(GPIOLDN);
unsigned char val = read_reg(CMDF9);
exit_conf_mode();
value = read_req(CMDF8) & bit0;
printf("value: %x\n", value);
if (value == bit0)
{
printf("Power LED is on\n");
}
else if (value == 0)
{
printf("Power LED is off\n");
}
else
{
if (val == 0x04)
{
printf("Power LED is blinking slowly\n");
}
else if (val == 0x02)
{
printf("Power LED is blinking medium\n");
}
else if (val == 0x00)
{
printf("Power LED is blinking fast\n");
}
else
{
printf("Power LED is %x (unknown)\n", val);
}
}
}
//----------------------------------n100 end / 1235u start--------------------------------
void OemEcIbFree()
{
if (iopl(3))
{
fprintf(stderr, "Can't get IO permissions\n");
exit(1);
}
unsigned char Status;
do
{
Status = inb(CMD66);
outb(CMD80, 0xEB);
} while (Status & 2);
}
void OemEcObFull()
{
unsigned char Status;
do
{
Status = inb(CMD66);
outb(CMD80, 0xEC);
} while (!(Status & 1));
}
void uexit_conf_mode()
{
if (iopl(0))
{
fprintf(stderr, "Can't drop IO permissions\n");
exit(1);
}
}
unsigned char uread_reg(unsigned char reg)
{
OemEcIbFree();
outb(CMD80, CMD66);
OemEcIbFree();
outb(ADR, CMD62);
OemEcObFull();
return inb(CMD62);
}
void uwrite_reg(unsigned char cmd, unsigned char adr, unsigned char val)
{
OemEcIbFree();
outb(cmd, CMD66);
OemEcIbFree();
outb(adr, CMD62);
OemEcIbFree();
outb(val, CMD62);
}
void ucmd_on()
{
uwrite_reg(CMD81, ADR, 0x00);
uexit_conf_mode();
}
void ucmd_off()
{
uwrite_reg(CMD81, ADR, 0x01);
uexit_conf_mode();
}
void ucmd_blink(char *interval)
{
unsigned char val;
if (strcmp(interval, "slow") == 0)
{
val = 0x02;
}
else if (strcmp(interval, "medium") == 0)
{
val = 0x04;
}
else if (strcmp(interval, "fast") == 0)
{
val = 0x08;
}
else
{
fprintf(stderr, "Invalid interval\n");
exit(1);
}
uwrite_reg(CMD81, ADR, val);
uexit_conf_mode();
}
void ucmd_status()
{
unsigned char val = uread_reg(CMD81);
if (val == 0x00)
{
printf("Power LED is on\n");
}
else if (val == 0x02)
{
printf("Power LED is blinking slowly\n");
}
else if (val == 0x04)
{
printf("Power LED is blinking medium\n");
}
else if (val == 0x08)
{
printf("Power LED is blinking fast\n");
}
else if (val == 0x01)
{
printf("Power LED is off\n");
}
else
{
printf("Power LED is %x (unknown)\n", val);
}
}
//----------------------------------1235u end--------------------------------
int main(int argc, char **argv)
{
if (geteuid() != 0)
{
fprintf(stderr, "The program is not running with root privileges. Please run as root.\n");
exit(1);
}
if (argc < 3)
{
fprintf(stderr, "Usage: %s <project> <command> [args]\n", argv[0]);
exit(1);
}
if (strcmp(argv[1], "pro") == 0)
{
if (strcmp(argv[2], "on") == 0)
{
ucmd_on();
}
else if (strcmp(argv[2], "blink") == 0)
{
if (argc < 4)
{
fprintf(stderr, "Usage: %s blink <interval>\n", argv[0]);
exit(1);
}
ucmd_blink(argv[3]);
}
else if (strcmp(argv[2], "status") == 0)
{
ucmd_status();
}
else if (strcmp(argv[2], "off") == 0)
{
ucmd_off();
}
else
{
fprintf(stderr, "Invalid command: %s\n", argv[2]);
exit(1);
}
return 0;
}
else
{
if (strcmp(argv[2], "on") == 0)
{
cmd_on();
}
else if (strcmp(argv[2], "blink") == 0)
{
if (argc < 4)
{
fprintf(stderr, "Usage: %s blink <interval>\n", argv[0]);
exit(1);
}
cmd_blink(argv[3]);
}
else if (strcmp(argv[2], "status") == 0)
{
cmd_status();
}
else if (strcmp(argv[2], "off") == 0)
{
cmd_off();
}
else
{
fprintf(stderr, "Invalid command: %s\n", argv[2]);
exit(1);
}
}
return 0;
}