source: trunk/minix/drivers/pci/main.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 8.0 KB
Line 
1/*
2main.c
3*/
4
5#include "../drivers.h"
6
7#include <ibm/pci.h>
8
9#include "pci.h"
10
11#define NR_DRIVERS 16
12
13PRIVATE struct name
14{
15 char name[M3_STRING];
16 int tasknr;
17} names[NR_DRIVERS];
18
19FORWARD _PROTOTYPE( void do_init, (message *mp) );
20FORWARD _PROTOTYPE( void do_sig_handler, (void) );
21FORWARD _PROTOTYPE( void do_first_dev, (message *mp) );
22FORWARD _PROTOTYPE( void do_next_dev, (message *mp) );
23FORWARD _PROTOTYPE( void do_find_dev, (message *mp) );
24FORWARD _PROTOTYPE( void do_ids, (message *mp) );
25FORWARD _PROTOTYPE( void do_dev_name, (message *mp) );
26FORWARD _PROTOTYPE( void do_slot_name, (message *mp) );
27FORWARD _PROTOTYPE( void do_reserve, (message *mp) );
28FORWARD _PROTOTYPE( void do_attr_r8, (message *mp) );
29FORWARD _PROTOTYPE( void do_attr_r16, (message *mp) );
30FORWARD _PROTOTYPE( void do_attr_r32, (message *mp) );
31FORWARD _PROTOTYPE( void do_attr_w8, (message *mp) );
32FORWARD _PROTOTYPE( void do_attr_w16, (message *mp) );
33FORWARD _PROTOTYPE( void do_attr_w32, (message *mp) );
34FORWARD _PROTOTYPE( void do_rescan_bus, (message *mp) );
35
36int main(void)
37{
38 int i, r;
39 message m;
40
41 pci_init();
42
43 for (i= 0; i<NR_DRIVERS; i++)
44 names[i].tasknr= ANY;
45
46 for(;;)
47 {
48 r= receive(ANY, &m);
49 if (r < 0)
50 {
51 printf("PCI: receive from ANY failed: %d\n", r);
52 break;
53 }
54 switch(m.m_type)
55 {
56 case BUSC_PCI_INIT: do_init(&m); break;
57 case BUSC_PCI_FIRST_DEV: do_first_dev(&m); break;
58 case BUSC_PCI_NEXT_DEV: do_next_dev(&m); break;
59 case BUSC_PCI_FIND_DEV: do_find_dev(&m); break;
60 case BUSC_PCI_IDS: do_ids(&m); break;
61 case BUSC_PCI_DEV_NAME: do_dev_name(&m); break;
62 case BUSC_PCI_SLOT_NAME: do_slot_name(&m); break;
63 case BUSC_PCI_RESERVE: do_reserve(&m); break;
64 case BUSC_PCI_ATTR_R8: do_attr_r8(&m); break;
65 case BUSC_PCI_ATTR_R16: do_attr_r16(&m); break;
66 case BUSC_PCI_ATTR_R32: do_attr_r32(&m); break;
67 case BUSC_PCI_ATTR_W8: do_attr_w8(&m); break;
68 case BUSC_PCI_ATTR_W16: do_attr_w16(&m); break;
69 case BUSC_PCI_ATTR_W32: do_attr_w32(&m); break;
70 case BUSC_PCI_RESCAN: do_rescan_bus(&m); break;
71 case PROC_EVENT: do_sig_handler(); break;
72 default:
73 printf("PCI: got message from %d, type %d\n",
74 m.m_source, m.m_type);
75 break;
76 }
77 }
78
79 return 0;
80}
81
82/*===========================================================================*
83 * do_sig_handler *
84 *===========================================================================*/
85PRIVATE void do_sig_handler()
86{
87 sigset_t sigset;
88 int sig;
89
90 /* Try to obtain signal set from PM. */
91 if (getsigset(&sigset) != 0) return;
92
93 /* Check for known signals. */
94 if (sigismember(&sigset, SIGTERM)) {
95 exit(0);
96 }
97}
98
99PRIVATE void do_init(mp)
100message *mp;
101{
102 int i, r, empty;
103
104#if DEBUG
105 printf("pci_init: called by '%s'\n", mp->m3_ca1);
106#endif
107 empty= -1;
108 for (i= 0; i<NR_DRIVERS; i++)
109 {
110 if (empty == -1 && names[i].tasknr == ANY)
111 empty= i;
112 if (strcmp(names[i].name, mp->m3_ca1) == 0)
113 break;
114 }
115 if (i < NR_DRIVERS)
116 pci_release(names[i].name);
117 else
118 {
119 i= empty;
120 strcpy(names[i].name, mp->m3_ca1);
121 }
122 names[i].tasknr= mp->m_source;
123
124 mp->m_type= 0;
125 r= send(mp->m_source, mp);
126 if (r != 0)
127 printf("do_init: unable to send to %d: %d\n", mp->m_source, r);
128}
129
130PRIVATE void do_first_dev(mp)
131message *mp;
132{
133 int r, devind;
134 u16_t vid, did;
135
136 r= pci_first_dev(&devind, &vid, &did);
137 if (r == 1)
138 {
139 mp->m1_i1= devind;
140 mp->m1_i2= vid;
141 mp->m1_i3= did;
142 }
143 mp->m_type= r;
144 r= send(mp->m_source, mp);
145 if (r != 0)
146 {
147 printf("do_first_dev: unable to send to %d: %d\n",
148 mp->m_source, r);
149 }
150}
151
152PRIVATE void do_next_dev(mp)
153message *mp;
154{
155 int r, devind;
156 u16_t vid, did;
157
158 devind= mp->m1_i1;
159
160 r= pci_next_dev(&devind, &vid, &did);
161 if (r == 1)
162 {
163 mp->m1_i1= devind;
164 mp->m1_i2= vid;
165 mp->m1_i3= did;
166 }
167 mp->m_type= r;
168 r= send(mp->m_source, mp);
169 if (r != 0)
170 {
171 printf("do_next_dev: unable to send to %d: %d\n",
172 mp->m_source, r);
173 }
174}
175
176PRIVATE void do_find_dev(mp)
177message *mp;
178{
179 int r, devind;
180 u8_t bus, dev, func;
181
182 bus= mp->m1_i1;
183 dev= mp->m1_i2;
184 func= mp->m1_i3;
185
186 r= pci_find_dev(bus, dev, func, &devind);
187 if (r == 1)
188 mp->m1_i1= devind;
189 mp->m_type= r;
190 r= send(mp->m_source, mp);
191 if (r != 0)
192 {
193 printf("do_find_dev: unable to send to %d: %d\n",
194 mp->m_source, r);
195 }
196}
197
198PRIVATE void do_ids(mp)
199message *mp;
200{
201 int r, devind;
202 u16_t vid, did;
203
204 devind= mp->m1_i1;
205
206 pci_ids(devind, &vid, &did);
207 mp->m1_i1= vid;
208 mp->m1_i2= did;
209 mp->m_type= OK;
210 r= send(mp->m_source, mp);
211 if (r != 0)
212 {
213 printf("do_ids: unable to send to %d: %d\n",
214 mp->m_source, r);
215 }
216}
217
218PRIVATE void do_dev_name(mp)
219message *mp;
220{
221 int r, name_len, len;
222 u16_t vid, did;
223 char *name_ptr, *name;
224
225 vid= mp->m1_i1;
226 did= mp->m1_i2;
227 name_len= mp->m1_i3;
228 name_ptr= mp->m1_p1;
229
230 name= pci_dev_name(vid, did);
231 if (name == NULL)
232 {
233 /* No name */
234 r= ENOENT;
235 }
236 else
237 {
238 len= strlen(name)+1;
239 if (len > name_len)
240 len= name_len;
241 r= sys_vircopy(SELF, D, (vir_bytes)name, mp->m_source, D,
242 (vir_bytes)name_ptr, len);
243 }
244
245 mp->m_type= r;
246 r= send(mp->m_source, mp);
247 if (r != 0)
248 {
249 printf("do_dev_name: unable to send to %d: %d\n",
250 mp->m_source, r);
251 }
252}
253
254PRIVATE void do_slot_name(mp)
255message *mp;
256{
257 int r, devind, name_len, len;
258 char *name_ptr, *name;
259
260 devind= mp->m1_i1;
261 name_len= mp->m1_i2;
262 name_ptr= mp->m1_p1;
263
264 name= pci_slot_name(devind);
265
266 len= strlen(name)+1;
267 if (len > name_len)
268 len= name_len;
269 r= sys_vircopy(SELF, D, (vir_bytes)name, mp->m_source, D,
270 (vir_bytes)name_ptr, len);
271
272 mp->m_type= r;
273 r= send(mp->m_source, mp);
274 if (r != 0)
275 {
276 printf("do_slot_name: unable to send to %d: %d\n",
277 mp->m_source, r);
278 }
279}
280
281PRIVATE void do_reserve(mp)
282message *mp;
283{
284 int i, r, devind;
285
286 /* Find the name of the caller */
287 for (i= 0; i<NR_DRIVERS; i++)
288 {
289 if (names[i].tasknr == mp->m_source)
290 break;
291 }
292 if (i >= NR_DRIVERS)
293 {
294 printf("pci`do_reserve: task %d did not call pci_init\n",
295 mp->m_source);
296 return;
297 }
298
299 devind= mp->m1_i1;
300
301 pci_reserve3(devind, mp->m_source, names[i].name);
302 mp->m_type= OK;
303 r= send(mp->m_source, mp);
304 if (r != 0)
305 {
306 printf("do_reserve: unable to send to %d: %d\n",
307 mp->m_source, r);
308 }
309}
310
311PRIVATE void do_attr_r8(mp)
312message *mp;
313{
314 int r, devind, port;
315 u8_t v;
316
317 devind= mp->m2_i1;
318 port= mp->m2_i2;
319
320 v= pci_attr_r8(devind, port);
321 mp->m2_l1= v;
322 mp->m_type= OK;
323 r= send(mp->m_source, mp);
324 if (r != 0)
325 {
326 printf("do_attr_r8: unable to send to %d: %d\n",
327 mp->m_source, r);
328 }
329}
330
331PRIVATE void do_attr_r16(mp)
332message *mp;
333{
334 int r, devind, port;
335 u32_t v;
336
337 devind= mp->m2_i1;
338 port= mp->m2_i2;
339
340 v= pci_attr_r16(devind, port);
341 mp->m2_l1= v;
342 mp->m_type= OK;
343 r= send(mp->m_source, mp);
344 if (r != 0)
345 {
346 printf("do_attr_r16: unable to send to %d: %d\n",
347 mp->m_source, r);
348 }
349}
350
351PRIVATE void do_attr_r32(mp)
352message *mp;
353{
354 int r, devind, port;
355 u32_t v;
356
357 devind= mp->m2_i1;
358 port= mp->m2_i2;
359
360 v= pci_attr_r32(devind, port);
361 mp->m2_l1= v;
362 mp->m_type= OK;
363 r= send(mp->m_source, mp);
364 if (r != 0)
365 {
366 printf("do_attr_r32: unable to send to %d: %d\n",
367 mp->m_source, r);
368 }
369}
370
371PRIVATE void do_attr_w8(mp)
372message *mp;
373{
374 int r, devind, port;
375 u8_t v;
376
377 devind= mp->m2_i1;
378 port= mp->m2_i2;
379 v= mp->m2_l1;
380
381 pci_attr_w8(devind, port, v);
382 mp->m_type= OK;
383 r= send(mp->m_source, mp);
384 if (r != 0)
385 {
386 printf("do_attr_w8: unable to send to %d: %d\n",
387 mp->m_source, r);
388 }
389}
390
391PRIVATE void do_attr_w16(mp)
392message *mp;
393{
394 int r, devind, port;
395 u16_t v;
396
397 devind= mp->m2_i1;
398 port= mp->m2_i2;
399 v= mp->m2_l1;
400
401 pci_attr_w16(devind, port, v);
402 mp->m_type= OK;
403 r= send(mp->m_source, mp);
404 if (r != 0)
405 {
406 printf("do_attr_w16: unable to send to %d: %d\n",
407 mp->m_source, r);
408 }
409}
410
411PRIVATE void do_attr_w32(mp)
412message *mp;
413{
414 int r, devind, port;
415 u32_t v;
416
417 devind= mp->m2_i1;
418 port= mp->m2_i2;
419 v= mp->m2_l1;
420
421 pci_attr_w32(devind, port, v);
422 mp->m_type= OK;
423 r= send(mp->m_source, mp);
424 if (r != 0)
425 {
426 printf("do_attr_w32: unable to send to %d: %d\n",
427 mp->m_source, r);
428 }
429}
430
431PRIVATE void do_rescan_bus(mp)
432message *mp;
433{
434 int r, busnr;
435
436 busnr= mp->m2_i1;
437
438 pci_rescan_bus(busnr);
439 mp->m_type= OK;
440 r= send(mp->m_source, mp);
441 if (r != 0)
442 {
443 printf("do_rescan_bus: unable to send to %d: %d\n",
444 mp->m_source, r);
445 }
446}
447
Note: See TracBrowser for help on using the repository browser.