AFHBA404
AFHBA404 connects ACQ2106 to PCI-Express
mmap.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * File: mmap.c
4  *
5  * $RCSfile: mmap.c,v $
6  *
7  * Copyright (C) 1999 D-TACQ Solutions Ltd
8  * not to be used without owner's permission
9  *
10  * Description:
11  *
12  * $Id: mmap.c,v 1.4 2001/10/07 10:01:42 pgm Exp $
13  * $Log: mmap.c,v $
14  * Revision 1.4 2001/10/07 10:01:42 pgm
15  * *** empty log message ***
16  *
17  * Revision 1.3 2001/03/23 19:44:46 pgm
18  * map only opt, debugs added
19  *
20  * Revision 1.2 2000/10/05 21:29:55 pgm
21  * mmap can read,write,fill
22  *
23  * Revision 1.1 2000/10/05 20:56:38 pgm
24  * *** empty log message ***
25  *
26  * Revision 1.1 1999/10/22 16:26:49 pgm
27  * first entry to cvs
28  *
29  *
30 \*****************************************************************************/
31 
32 
33 
34 
35 
36 #undef __KERNEL__
37 
38 #include "local.h"
39 
40 #include <assert.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 
51 #include <popt.h>
52 
53 
54 #include <signal.h>
55 #include <sys/time.h>
56 #include <unistd.h>
57 
58 #define FNAME "/dev/acq32/acq32.1.raw"
59 
60 #define HELP \
61  "mmap $Revision: 1.4 $\n" \
62  "usage: mmap opts\n" \
63  " -f device name\n" \
64  " -r read [default]\n" \
65  " -w write\n" \
66  " -n nop (just block, holding the mapping)\n" \
67  " -o offset\n" \
68  " -l length\n" \
69  "maps device [ram] space and either\n" \
70  " reads from ram to stdout\n" \
71  "-or-writes to ram from stdin\n" \
72  " -b [-v value] = block fill [default 0xdeadbeef]\n" \
73  " -T [list of test regs] registers walking bit test\n" \
74  ""
75 
77 
78 int doRegsTest(volatile unsigned* regs, unsigned* offsets, int nregs)
79 {
80  int ir;
81  int irr;
82  int pass = 0;
83  int fail = 0;
84 
85  dbg(2, "testing ..");
86 
87  for (ir = 0; ir != nregs; ++ir){
88  regs[offsets[ir]] = 0;
89  }
90  for (ir = 0; ir != nregs; ++ir){
91  int cursor;
92  for (cursor = 0x1; cursor; cursor <<= 1){
93  unsigned wanted, got;
94 
95  dbg(3, "regs[%d] := %08x", offsets[ir], cursor);
96 
97  regs[offsets[ir]] = cursor;
98 
99  for (irr = 0; irr != nregs; ++irr){
100  got = regs[offsets[irr]];
101 
102  dbg(3, "regs[%d] = %08x", irr, got);
103  if (irr == ir){
104  wanted = cursor;
105  }else{
106  wanted = 0;
107  }
108  if (wanted != got){
109  err("regs[%d] wanted 0x%08x got 0x%08x",
110  offsets[irr], wanted, got);
111  ++fail;
112  }else{
113  ++pass;
114  }
115 
116  }
117  dbg(3, "\n");
118  }
119  regs[offsets[ir]] = cursor;
120  }
121 
122  dbg(2, "complete %d fail %d pass", fail, pass);
123 
124  return fail != 0;
125 }
126 
127 int G_fail;
128 int G_pass;
131 
132 int regsTest(void* pmem, const char* argv[])
133 {
134  /* argv is a list of register offsets */
135  int ii;
136  int nargs;
137  unsigned* offsets;
138  int iter = 0;
139 
140  for (nargs = 0; argv[nargs] != NULL; ++nargs){
141  ;
142  }
143 
144  info("pmem %p nargs:%d argv[0] \"%s\"", pmem, nargs, argv[0]);
145 
146  offsets = calloc(nargs, sizeof(unsigned));
147 
148  for (ii = 0; ii != nargs; ++ii){
149  offsets[ii] = strtoul(argv[ii], 0, 0);
150  offsets[ii] /= sizeof(unsigned);
151  dbg(2, "offsets[%d] (%s) is %d", ii, argv[ii], offsets[ii]);
152  }
153 
154  while(!please_quit){
155  if (doRegsTest((unsigned*)pmem, offsets, nargs)){
156  G_fail++;
157  }else{
158  G_pass++;
159  }
160  if ((++iter&0x01ff) == 0 || alarm_set||please_quit){
161  info("FAIL:%10d PASS:%10d %s",
162  G_fail, G_pass,
163  G_fail==0? "*** NO ERRORS ***": ":-( :-( :-(");
164  alarm_set = 0;
165  }
166  }
167 
168  return G_fail != 0;
169 }
170 
171 static void alarm_handler(int signum) {
172  alarm_set = 1;
173 }
174 
175 static void quit_handler(int signum){
176  alarm_handler(signum);
177  please_quit = 1;
178 }
179 static void install_handlers(void) {
180  struct sigaction sa;
181  memset(&sa, 0, sizeof(sa));
182  sa.sa_handler = alarm_handler;
183 
184  struct sigaction saq;
185  memset(&saq, 0, sizeof(saq));
186  sa.sa_handler = quit_handler;
187 
188  if (sigaction(SIGALRM, &sa, NULL)) perror ("sigaction");
189  if (sigaction(SIGINT, &saq, NULL)) perror ("sigaction");
190 }
191 
192 
193 int main( int argc, const char* argv[] )
194 {
195  /* WORKTODO ... args handling */
196 
197  int fd;
198  void* region;
199  char* fname = FNAME;
200  unsigned offset = 0;
201  unsigned length = 0x100000;
202  int rc;
203  unsigned fill_value = 0xdeadbeef;
204  enum MODE { M_READ, M_WRITE, M_FILL, M_TEST, M_NOP } mode = M_READ;
205 
206  struct poptOption opt_table[] = {
207  { "device", 'f', POPT_ARG_STRING, &fname, 0 },
208  { "help", 'h', POPT_ARG_NONE, 0, 'h' },
209  { "read", 'r', POPT_ARG_NONE, 0, 'r' },
210  { "write", 'w', POPT_ARG_NONE, 0, 'w' },
211  { "nop", 'n', POPT_ARG_NONE, 0, 'n' },
212  { "fill", 'b', POPT_ARG_NONE, 0, 'f' },
213  { "offset", 'o', POPT_ARG_INT, &offset, 'o' },
214  { "length", 'l', POPT_ARG_INT, &length, 'l' },
215  { "value", 'v', POPT_ARG_INT, &fill_value, 0 },
216  { "regstest", 'T', POPT_ARG_NONE, 0, 'T' },
217  { "verbose", 'V', POPT_ARG_INT, &acq200_debug, 0 },
218  { }
219  };
220 
221  poptContext opt_context;
222 
223  opt_context = poptGetContext( argv[0], argc, argv, opt_table, 0 );
224 
225  int open_mode = O_RDONLY;
226  int mmap_mode = PROT_READ;
227 
228  while ( (rc = poptGetNextOpt( opt_context )) > 0 ){
229  switch( rc ){
230  case 'h':
231  fprintf( stderr, HELP );
232  return 1;
233  case 'r':
234  mode = M_READ;
235  break;
236  case 'w':
237  open_mode = O_RDWR;
238  mmap_mode = PROT_READ|PROT_WRITE;
239  mode = M_WRITE;
240  break;
241  case 'n':
242  mode = M_NOP;
243  break;
244  case 'f':
245  open_mode = O_RDWR;
246  mmap_mode = PROT_READ|PROT_WRITE;
247  mode = M_FILL;
248  break;
249  case 'T':
250  mode = M_TEST;
251  break;
252  }
253  } // processes all other opts via arg pointers
254 
255 
256  if ( (fd = open( fname, open_mode)) < 0 ){
257  fprintf( stderr, "mmap: failed to open device \"%s\" - ", fname );
258  perror( "" );
259  return 1;
260  }
261 
262  region = mmap( NULL, length, mmap_mode, MAP_SHARED, fd, 0 );
263 
264  if ( region == (caddr_t)-1 ){
265  perror( "mmap" );
266  return 1;
267  }
268 
269  switch( mode ){
270  default:
271  case M_READ:
272 
273  // spew to stdout in one big blurt
274 
275  write( 1, (char*)region+offset, length );
276  break;
277  case M_FILL: {
278  unsigned* praw = (unsigned*)&((char*)region)[offset];
279  int iwrite;
280  int imax = length/sizeof(unsigned);
281 
282  for ( iwrite = 0; iwrite != imax; ++iwrite ){
283  praw[iwrite] = fill_value;
284  }
285  break;
286  }
287  case M_WRITE:{
288 
289  // write to memory - read input until first of EOF or length
290  // it's slow char at a time stuff but who cares?
291 
292  int iwrite;
293  int cc;
294  char* praw = &((char*)region)[offset];
295 
296  for ( iwrite=0; (cc = getchar()) != -1 && iwrite != length; ++iwrite ){
297  praw[iwrite] = cc;
298  }
299  break;
300  }
301  case M_NOP :
302  fprintf( stderr,
303  "mmap: blocking. Hit any key to continue, q to quit\n" );
304 
305  while ( getchar() != 'q' ){
306  char aline[128];
307  FILE* fp = fopen( "/proc/self/maps", "r" );
308  assert( fp );
309 
310  while( fgets( aline, sizeof(aline), fp ) != NULL ) {
311  fputs(aline, stderr);
312  }
313  fclose( fp );
314  }
315  break;
316  case M_TEST:
317  install_handlers();
318 // alarm(2);
319  return regsTest(region, poptGetArgs(opt_context));
320  break;
321  }
322 
323  return 0;
324 }
325 
326 
327 
328 
329 
330 
please_quit
int please_quit
Definition: mmap.c:129
local.h
info
#define info(format, arg...)
Definition: local.h:118
G_pass
int G_pass
Definition: mmap.c:128
alarm_set
int alarm_set
Definition: mmap.c:130
main
int main(int argc, const char *argv[])
Definition: mmap.c:193
err
#define err(format, arg...)
Definition: local.h:121
acq200_debug
int acq200_debug
Definition: mmap.c:76
dbg
#define dbg(lvl, format, arg...)
Definition: local.h:124
FNAME
#define FNAME
Definition: mmap.c:58
assert
#define assert(p)
Definition: acq-fiber-hba.h:55
regsTest
int regsTest(void *pmem, const char *argv[])
Definition: mmap.c:132
HELP
#define HELP
Definition: mmap.c:60
G_fail
int G_fail
Definition: mmap.c:127
doRegsTest
int doRegsTest(volatile unsigned *regs, unsigned *offsets, int nregs)
Definition: mmap.c:78