Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <inttypes.h>
  5 #include <ctype.h>
  6 #include <sys/ioctl.h>
  7 
  8 #include "struct-dump.h"
  9 
 10 /* ---------------------------------------------------------------------- */
 11 
 12 struct struct_desc desc_int[] = {{
 13         .type   = SINT32,
 14         .name   = "int",
 15 },{
 16         /* end of list */
 17 }};
 18 
 19 struct struct_desc desc_long[] = {{
 20         .type   = SINT32,
 21         .name   = "long",
 22 },{
 23         /* end of list */
 24 }};
 25 
 26 struct struct_desc desc_timeval[] = {{
 27         /* FIXME */
 28         /* end of list */
 29 }};
 30 
 31 /* ---------------------------------------------------------------------- */
 32 
 33 int print_struct(FILE *fp, struct struct_desc *desc, void *data,
 34                  char *prefix, int tab)
 35 {
 36         char name[256];
 37         unsigned char *ptr = data;
 38         uint64_t u64;
 39         int64_t  s64;
 40         uint32_t u32;
 41         int32_t  s32;
 42         uint16_t u16;
 43         int16_t  s16;
 44         uint8_t  u8;
 45         int8_t   s8;
 46         int al = sizeof(long)-1; /* struct + union + 64bit alignment */
 47         void *p;
 48         unsigned int i,j,first;
 49 
 50         for (i = 0; desc[i].name != NULL; i++) {
 51                 sprintf(name,"%s%s",prefix,desc[i].name);
 52                 if (STRUCT == desc[i].type) {
 53                         strcat(name,".");
 54                         ptr = (void*)(((intptr_t)ptr + al) & ~al);
 55                         print_struct(fp,desc[i].desc, ptr, name, tab);
 56                         ptr += desc[i].length;
 57                         if (!tab && desc[i+1].name != NULL)
 58                                 fprintf(fp,";");
 59                         continue;
 60                 }
 61                 if (UNION == desc[i].type) {
 62                         u32 = *((uint32_t*)(ptr-4));
 63                         ptr = (void*)(((intptr_t)ptr + al) & ~al);
 64                         for (j = 0; desc[i].u[j].name != NULL; j++)
 65                                 if (desc[i].u[j].value == u32)
 66                                         break;
 67                         if (desc[i].u[j].name != NULL) {
 68                                 strcat(name,".");
 69                                 strcat(name,desc[i].u[j].name);
 70                                 strcat(name,".");
 71                                 print_struct(fp,desc[i].u[j].desc,
 72                                              ptr, name, tab);
 73                         }
 74                         return 0; /* FIXME */
 75                 }
 76                 if (tab)
 77                         fprintf(fp,"\t%-24s: ",name);
 78                 else
 79                         fprintf(fp,"%s=",name);
 80                 switch (desc[i].type) {
 81                 case STRING:
 82                         fprintf(fp,"\"%-.*s\"",desc[i].length,ptr);
 83                         ptr += desc[i].length;
 84                         break;
 85                 case PTR:
 86                         p = *(void**)ptr;
 87                         fprintf(fp,"%p",p);
 88                         ptr += sizeof(p);
 89                         break;
 90                 case VER:
 91                         u32 = *((uint32_t*)ptr);
 92                         fprintf(fp,"%d.%d.%d",
 93                                 (u32 >> 16) & 0xff,
 94                                 (u32 >>  8) & 0xff,
 95                                 u32         & 0xff);
 96                         ptr += 4;
 97                         break;
 98                 case FOURCC:
 99                         u32 = *((uint32_t*)ptr);
100                         fprintf(fp,"0x%08x [%c%c%c%c]", u32,
101                                 isprint(ptr[0]) ? ptr[0] : '.',
102                                 isprint(ptr[1]) ? ptr[1] : '.',
103                                 isprint(ptr[2]) ? ptr[2] : '.',
104                                 isprint(ptr[3]) ? ptr[3] : '.');
105                         ptr += 4;
106                         break;
107 
108                 case ENUM16:
109                         u16 = *((uint16_t*)ptr);
110                         fprintf(fp,"%s", (u16 < desc[i].length && desc[i].enums[u16])
111                                 ? desc[i].enums[u16] : "unknown");
112                         ptr += 2;
113                         break;
114                 case ENUM32:
115                         u32 = *((uint32_t*)ptr);
116                         fprintf(fp,"%s", (u32 < desc[i].length && desc[i].enums[u32])
117                                 ? desc[i].enums[u32] : "unknown");
118                         ptr += 4;
119                         break;
120 
121                 case BITS16:
122                         u16 = *((uint16_t*)ptr);
123                         first = 1;
124                         fprintf(fp,"0x%x [",u16);
125                         for (j = 0; j < 16; j++) {
126                                 if (0 == (u16 & (1 << j)))
127                                         continue;
128                                 fprintf(fp,"%s%s",
129                                         first ? "" : ",",
130                                         desc[i].bits[j]);
131                                 first = 0;
132                         }
133                         fprintf(fp,"]");
134                         ptr += 2;
135                         break;
136                 case BITS32:
137                         u32 = *((uint32_t*)ptr);
138                         first = 1;
139                         fprintf(fp,"0x%x [",u32);
140                         for (j = 0; j < 32; j++) {
141                                 if (0 == (u32 & (1 << j)))
142                                         continue;
143                                 fprintf(fp,"%s%s",
144                                         first ? "" : ",",
145                                         desc[i].bits[j]);
146                                 first = 0;
147                         }
148                         fprintf(fp,"]");
149                         ptr += 4;
150                         break;
151                 case BITS64:
152                         ptr = (void*)(((intptr_t)ptr + al) & ~al);
153                         u64 = *((uint64_t*)ptr);
154                         first = 1;
155                         fprintf(fp,"0x%" PRIx64 " [",u64);
156                         for (j = 0; j < 64; j++) {
157                                 if (0 == (u64 & ((int64_t)1 << j)))
158                                         continue;
159                                 fprintf(fp,"%s%s",
160                                         first ? "" : ",",
161                                         desc[i].bits[j]);
162                                 first = 0;
163                         }
164                         fprintf(fp,"]");
165                         ptr += 8;
166                         break;
167 
168                 case UINT64:
169                         ptr = (void*)(((intptr_t)ptr + al) & ~al);
170                         u64 = *((uint64_t*)ptr);
171                         fprintf(fp,"%" PRIu64,u64);
172                         ptr += 8;
173                         break;
174                 case SINT64:
175                         ptr = (void*)(((intptr_t)ptr + al) & ~al);
176                         s64 = *((int64_t*)ptr);
177                         fprintf(fp,"%" PRId64,s64);
178                         ptr += 8;
179                         break;
180                 case UINT32:
181                         u32 = *((uint32_t*)ptr);
182                         fprintf(fp,"%u",u32);
183                         ptr += 4;
184                         break;
185                 case SINT32:
186                         s32 = *((int32_t*)ptr);
187                         fprintf(fp,"%d",s32);
188                         ptr += 4;
189                         break;
190                 case UINT16:
191                         u16 = *((uint16_t*)ptr);
192                         fprintf(fp,"%u",u16);
193                         ptr += 2;
194                         break;
195                 case SINT16:
196                         s16 = *((int16_t*)ptr);
197                         fprintf(fp,"%d",s16);
198                         ptr += 2;
199                         break;
200                 case UINT8:
201                         u8 = *((uint8_t*)ptr);
202                         fprintf(fp,"%u",u8);
203                         ptr += 1;
204                         break;
205                 case SINT8:
206                         s8 = *((int8_t*)ptr);
207                         fprintf(fp,"%d",s8);
208                         ptr += 1;
209                         break;
210 
211                 case PADDING:
212                         ptr += desc[i].length;
213                         break;
214 
215                 case STRUCT:
216                 case UNION:
217                         /* shouldn't happen */
218                         fprintf(fp,"FIXME [type=%d]\n",desc[i].type);
219                         exit(1);
220                 }
221                 if  (tab)
222                         fprintf(fp,"\n");
223                 else if (desc[i+1].name != NULL)
224                         fprintf(fp,";");
225         }
226         return 0;
227 }
228 
229 /* ---------------------------------------------------------------------- */
230 
231 int print_ioctl(FILE *fp, struct ioctl_desc *ioctls, char *prefix,
232                 int cmd, void *ptr)
233 {
234         int  index               = _IOC_NR(cmd);
235         char *name               = ioctls[index].name;
236         struct struct_desc *desc = ioctls[index].desc;
237 
238         fprintf(fp,"%s%s(", prefix, name ? name : "UNKNOWN");
239         if (desc) {
240                 print_struct(fp,desc,ptr,"",0);
241         } else {
242                 fprintf(stderr,"???");
243         }
244         fprintf(fp,")");
245         return 0;
246 }
247 
248 /* ---------------------------------------------------------------------- */
249 /*
250  * Local variables:
251  * c-basic-offset: 8
252  * End:
253  */
254 
  This page was automatically generated by the LXR engine.