server.cpp (2532B)
1 #include <blt/Message.h> 2 #include <blt/Connection.h> 3 #include <blt/namer.h> 4 #include <blt/syscall.h> 5 6 using namespace BLT; 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 #include "pci.h" 13 14 static pci_cfg *pci_dev[128]; 15 static int pci_count = 0; 16 17 void server(void *data) 18 { 19 Connection *cnxn = (Connection *) data; 20 Message msg,reply; 21 int res; 22 23 while(cnxn->Recv(&msg) == 0){ 24 int32 op = -1; 25 msg.GetInt32('code',&op); 26 27 reply.Empty(); 28 29 switch(op){ 30 case 1: { // get_nth_pci_cfg 31 int32 n = -1; 32 msg.GetInt32('arg0', &n); 33 if((n >= 0) && (n < pci_count)){ 34 reply.PutData('ret0','pcic',pci_dev[n],sizeof(pci_cfg)); 35 res = 0; 36 } else { 37 res = -1; 38 } 39 break; 40 } 41 42 case 2: { // pci_read 43 int32 bus = 0; 44 int32 dev = 0; 45 int32 func = 0; 46 int32 reg = 0; 47 int32 size = 0; 48 msg.GetInt32('arg0',&bus); 49 msg.GetInt32('arg1',&dev); 50 msg.GetInt32('arg2',&func); 51 msg.GetInt32('arg3',®); 52 msg.GetInt32('arg4',&size); 53 54 printf("pci: read(%d,%d,%d,%d,%d)\n",bus,dev,func,reg,size); 55 size = (int32) pci_read(bus,dev,func,reg,size); 56 57 reply.PutInt32('ret0',size); 58 res = 0; 59 break; 60 } 61 62 case 3: { // pci_write 63 int32 bus = 0; 64 int32 dev = 0; 65 int32 func = 0; 66 int32 reg = 0; 67 int32 size = 0; 68 int32 val = 0; 69 70 msg.GetInt32('arg0',&bus); 71 msg.GetInt32('arg1',&dev); 72 msg.GetInt32('arg2',&func); 73 msg.GetInt32('arg3',®); 74 msg.GetInt32('arg4',&val); 75 msg.GetInt32('arg5',&size); 76 pci_write(bus,dev,func,reg,(uint32)val,size); 77 res = 0; 78 break; 79 } 80 81 default: 82 res = -1; 83 } 84 85 reply.PutInt32('resp',res); 86 res = msg.Reply(&reply); 87 } 88 } 89 90 int pci_scan() 91 { 92 pci_cfg cfg; 93 int bus,dev,func; 94 95 for(bus=0;bus<255;bus++){ 96 for(dev=0;dev<32;dev++) { 97 if(pci_probe(bus,dev,0,&cfg)) continue; 98 pci_dev[pci_count] = (pci_cfg*) malloc(sizeof(pci_cfg)); 99 memcpy(pci_dev[pci_count],&cfg,sizeof(pci_cfg)); 100 pci_count++; 101 if(cfg.header_type & 0x80){ 102 for(func=1;func<8;func++){ 103 if(!pci_probe(bus,dev,func,&cfg)){ 104 pci_dev[pci_count] = (pci_cfg*) malloc(sizeof(pci_cfg)); 105 memcpy(pci_dev[pci_count],&cfg,sizeof(pci_cfg)); 106 pci_count++; 107 } 108 } 109 } 110 } 111 } 112 } 113 114 int main(int argc, char **argv[]) 115 { 116 Connection *cnxn; 117 118 if(namer_find("pci",0) > 0) { 119 // printf("pci: server is already running\n"); 120 return 0; 121 } 122 123 cnxn = Connection::CreateService("pci"); 124 125 pci_scan(); 126 127 if(cnxn){ 128 // printf("pci: server started\n",cnxn); 129 thr_resume(thr_create(server,cnxn,"pci server")); 130 } 131 return 0; 132 }