Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
447 lines (384 loc) · 8.02 KB
/
Copy pathmain.c
File metadata and controls
447 lines (384 loc) · 8.02 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
main.c
*/
#include "../drivers.h"
#include <ibm/pci.h>
#include "pci.h"
#define NR_DRIVERS 16
PRIVATE struct name
{
char name[M3_STRING];
int tasknr;
} names[NR_DRIVERS];
FORWARD _PROTOTYPE( void do_init, (message *mp) );
FORWARD _PROTOTYPE( void do_sig_handler, (void) );
FORWARD _PROTOTYPE( void do_first_dev, (message *mp) );
FORWARD _PROTOTYPE( void do_next_dev, (message *mp) );
FORWARD _PROTOTYPE( void do_find_dev, (message *mp) );
FORWARD _PROTOTYPE( void do_ids, (message *mp) );
FORWARD _PROTOTYPE( void do_dev_name, (message *mp) );
FORWARD _PROTOTYPE( void do_slot_name, (message *mp) );
FORWARD _PROTOTYPE( void do_reserve, (message *mp) );
FORWARD _PROTOTYPE( void do_attr_r8, (message *mp) );
FORWARD _PROTOTYPE( void do_attr_r16, (message *mp) );
FORWARD _PROTOTYPE( void do_attr_r32, (message *mp) );
FORWARD _PROTOTYPE( void do_attr_w8, (message *mp) );
FORWARD _PROTOTYPE( void do_attr_w16, (message *mp) );
FORWARD _PROTOTYPE( void do_attr_w32, (message *mp) );
FORWARD _PROTOTYPE( void do_rescan_bus, (message *mp) );
int main(void)
{
int i, r;
message m;
pci_init();
for (i= 0; i<NR_DRIVERS; i++)
names[i].tasknr= ANY;
for(;;)
{
r= receive(ANY, &m);
if (r < 0)
{
printf("PCI: receive from ANY failed: %d\n", r);
break;
}
switch(m.m_type)
{
case BUSC_PCI_INIT: do_init(&m); break;
case BUSC_PCI_FIRST_DEV: do_first_dev(&m); break;
case
8000
BUSC_PCI_NEXT_DEV: do_next_dev(&m); break;
case BUSC_PCI_FIND_DEV: do_find_dev(&m); break;
case BUSC_PCI_IDS: do_ids(&m); break;
case BUSC_PCI_DEV_NAME: do_dev_name(&m); break;
case BUSC_PCI_SLOT_NAME: do_slot_name(&m); break;
case BUSC_PCI_RESERVE: do_reserve(&m); break;
case BUSC_PCI_ATTR_R8: do_attr_r8(&m); break;
case BUSC_PCI_ATTR_R16: do_attr_r16(&m); break;
case BUSC_PCI_ATTR_R32: do_attr_r32(&m); break;
case BUSC_PCI_ATTR_W8: do_attr_w8(&m); break;
case BUSC_PCI_ATTR_W16: do_attr_w16(&m); break;
case BUSC_PCI_ATTR_W32: do_attr_w32(&m); break;
case BUSC_PCI_RESCAN: do_rescan_bus(&m); break;
case PROC_EVENT: do_sig_handler(); break;
default:
printf("PCI: got message from %d, type %d\n",
m.m_source, m.m_type);
break;
}
}
return 0;
}
/*===========================================================================*
* do_sig_handler *
*===========================================================================*/
PRIVATE void do_sig_handler()
{
sigset_t sigset;
int sig;
/* Try to obtain signal set from PM. */
if (getsigset(&sigset) != 0) return;
/* Check for known signals. */
if (sigismember(&sigset, SIGTERM)) {
exit(0);
}
}
PRIVATE void do_init(mp)
message *mp;
{
int i, r, empty;
#if DEBUG
printf("pci_init: called by '%s'\n", mp->m3_ca1);
#endif
empty= -1;
for (i= 0; i<NR_DRIVERS; i++)
{
if (empty == -1 && names[i].tasknr == ANY)
empty= i;
if (strcmp(names[i].name, mp->m3_ca1) == 0)
break;
}
if (i < NR_DRIVERS)
pci_release(names[i].name);
else
{
i= empty;
strcpy(names[i].name, mp->m3_ca1);
}
names[i].tasknr= mp->m_source;
mp->m_type= 0;
r= send(mp->m_source, mp);
if (r != 0)
printf("do_init: unable to send to %d: %d\n", mp->m_source, r);
}
PRIVATE void do_first_dev(mp)
message *mp;
{
int r, devind;
u16_t vid, did;
r= pci_first_dev(&devind, &vid, &did);
if (r == 1)
{
mp->m1_i1= devind;
mp->m1_i2= vid;
mp->m1_i3= did;
}
mp->m_type= r;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_first_dev: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_next_dev(mp)
message *mp;
{
int r, devind;
u16_t vid, did;
devind= mp->m1_i1;
r= pci_next_dev(&devind, &vid, &did);
if (r == 1)
{
mp->m1_i1= devind;
mp->m1_i2= vid;
mp->m1_i3= did;
}
mp->m_type= r;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_next_dev: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_find_dev(mp)
message *mp;
{
int r, devind;
u8_t bus, dev, func;
bus= mp->m1_i1;
dev= mp->m1_i2;
func= mp->m1_i3;
r= pci_find_dev(bus, dev, func, &devind);
if (r == 1)
mp->m1_i1= devind;
mp->m_type= r;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_find_dev: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_ids(mp)
message *mp;
{
int r, devind;
u16_t vid, did;
devind= mp->m1_i1;
pci_ids(devind, &vid, &did);
mp->m1_i1= vid;
mp->m1_i2= did;
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_ids: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_dev_name(mp)
message *mp;
{
int r, name_len, len;
u16_t vid, did;
char *name_ptr, *name;
vid= mp->m1_i1;
did= mp->m1_i2;
name_len= mp->m1_i3;
name_ptr= mp->m1_p1;
name= pci_dev_name(vid, did);
if (name == NULL)
{
/* No name */
r= ENOENT;
}
else
{
len= strlen(name)+1;
if (len > name_len)
len= name_len;
r= sys_vircopy(SELF, D, (vir_bytes)name, mp->m_source, D,
(vir_bytes)name_ptr, len);
}
mp->m_type= r;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_dev_name: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_slot_name(mp)
message *mp;
{
int r, devind, name_len, len;
char *name_ptr, *name;
devind= mp->m1_i1;
name_len= mp->m1_i2;
name_ptr= mp->m1_p1;
name= pci_slot_name(devind);
len= strlen(name)+1;
if (len > name_len)
len= name_len;
r= sys_vircopy(SELF, D, (vir_bytes)name, mp->m_source, D,
(vir_bytes)name_ptr, len);
mp->m_type= r;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_slot_name: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_reserve(mp)
message *mp;
{
int i, r, devind;
/* Find the name of the caller */
for (i= 0; i<NR_DRIVERS; i++)
{
if (names[i].tasknr == mp->m_source)
break;
}
if (i >= NR_DRIVERS)
{
printf("pci`do_reserve: task %d did not call pci_init\n",
mp->m_source);
return;
}
devind= mp->m1_i1;
pci_reserve3(devind, mp->m_source, names[i].name);
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_reserve: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_attr_r8(mp)
message *mp;
{
int r, devind, port;
u8_t v;
devind= mp->m2_i1;
port= mp->m2_i2;
v= pci_attr_r8(devind, port);
mp->m2_l1= v;
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_attr_r8: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_attr_r16(mp)
message *mp;
{
int r, devind, port;
u32_t v;
devind= mp->m2_i1;
port= mp->m2_i2;
v= pci_attr_r16(devind, port);
mp->m2_l1= v;
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_attr_r16: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_attr_r32(mp)
message *mp;
{
int r, devind, port;
u32_t v;
devind= mp->m2_i1;
port= mp->m2_i2;
v= pci_attr_r32(devind, port);
mp->m2_l1= v;
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_attr_r32: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_attr_w8(mp)
message *mp;
{
int r, devind, port;
u8_t v;
devind= mp->m2_i1;
port= mp->m2_i2;
v= mp->m2_l1;
pci_attr_w8(devind, port, v);
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_attr_w8: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_attr_w16(mp)
message *mp;
{
int r, devind, port;
u16_t v;
devind= mp->m2_i1;
port= mp->m2_i2;
v= mp->m2_l1;
pci_attr_w16(devind, port, v);
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_attr_w16: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_attr_w32(mp)
message *mp;
{
int r, devind, port;
u32_t v;
devind= mp->m2_i1;
port= mp->m2_i2;
v= mp->m2_l1;
pci_attr_w32(devind, port, v);
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_attr_w32: unable to send to %d: %d\n",
mp->m_source, r);
}
}
PRIVATE void do_rescan_bus(mp)
message *mp;
{
int r, busnr;
busnr= mp->m2_i1;
pci_rescan_bus(busnr);
mp->m_type= OK;
r= send(mp->m_source, mp);
if (r != 0)
{
printf("do_rescan_bus: unable to send to %d: %d\n",
mp->m_source, r);
}
}