/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
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
447
448
449
450
451
452
453
454
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
#include <time.h>

#include "pci.h"

#include "tools.h"
#include "error.h"

#ifndef __timespec_defined
struct timespec {
    time_t tv_sec;
    long tv_nsec;
};
#endif /* __timespec_defined */


pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event) {
    int i;

    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_description_t *events = model_info->events;

    for (i = 0; events[i].name; i++) {
	if (!strcasecmp(events[i].name, event)) return events[i].evid;
    }

    return (pcilib_event_t)-1;
}

pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type) {
    int i;
    
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_data_type_description_t *data_types = model_info->data_types;
    
    for (i = 0; data_types[i].name; i++) {
	if ((data_types[i].evid&event)&&(!strcasecmp(data_types[i].name, data_type))) return data_types[i].data_type;
    }

    return (pcilib_event_data_type_t)-1;
}

int pcilib_init_event_engine(pcilib_t *ctx) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;

    if ((api)&&(api->init)) {
	ctx->event_ctx = api->init(ctx);
	if (ctx->event_ctx) {
	    ctx->event_ctx->pcilib = ctx;
	}
    }
    
    return 0;
}

pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx) {
    return ctx->event_ctx;
}

int pcilib_reset(pcilib_t *ctx) {
    const pcilib_event_api_description_t *api;
    
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }
    
    if (api->reset) 
	return api->reset(ctx->event_ctx);
	
    return 0;
}

int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    ctx->event_ctx->params.rawdata.callback = callback;
    ctx->event_ctx->params.rawdata.user = user;
    
    return 0;    
}

int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    ctx->event_ctx->params.autostop.max_events = max_events;
    ctx->event_ctx->params.autostop.duration = duration;
    
    return 0;    
}

int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
	/* To support hardware without autotriggering, we need to provide in event.c a code 
	to generate multiple triggers in a thread (available in cli). The function should 
	be re-enabled afterwards: just set parameters and let implementation decide if it 
	can make triggering in hardware or will use our emulation */
	
    return PCILIB_ERROR_NOTSUPPORTED;
}

int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    ctx->event_ctx->params.parallel.max_threads = max_threads;

    return 0;
}

int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
    int err;
    pcilib_register_value_t max_threads;

    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    err = pcilib_read_register(ctx, "conf", "max_threads", &max_threads);
    if (!err) {
	err = pcilib_configure_preprocessing_threads(ctx, max_threads);
	if (err) pcilib_warning("Error (%i) configuring number of preprocessing threads", err);
    }

    if (api->start) 
	return api->start(ctx->event_ctx, event_mask, flags);

    return 0;
}

int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->stop) 
	return api->stop(ctx->event_ctx, flags);

    return 0;
}

int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->stream)
	return api->stream(ctx->event_ctx, callback, user);

    if (api->next_event) {
	pcilib_error("Streaming using next_event API is not implemented yet");
    }

    pcilib_error("Event enumeration is not suppored by API");
    return PCILIB_ERROR_NOTSUPPORTED;
}
/*
typedef struct {
    pcilib_event_id_t event_id;
    pcilib_event_info_t *info;
} pcilib_return_event_callback_context_t;

static int pcilib_return_event_callback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
    pcilib_return_event_callback_context_t *ctx = (pcilib_return_event_callback_context_t*)user;
    ctx->event_id = event_id;
    ctx->info = info;
}
*/

int pcilib_get_next_event(pcilib_t *ctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
    const pcilib_event_api_description_t *api;
//    pcilib_return_event_callback_context_t user;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->next_event) 
	return api->next_event(ctx->event_ctx, timeout, evid, info_size, info);

/*	
    if (api->stream) {
	err = api->stream(ctx->event_ctx, 1, timeout, pcilib_return_event_callback, &user);
	if (err) return err;
	
	if (evid) *evid = user->event_id;
	if (info) *info = user->info;

	return 0;
    }
*/

    pcilib_error("Event enumeration is not suppored by API");
    return PCILIB_ERROR_NOTSUPPORTED;
}

int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
    const pcilib_event_api_description_t *api;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);

    api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->trigger) 
	return api->trigger(ctx->event_ctx, event, trigger_size, trigger_data);

    pcilib_error("Self triggering is not supported by the selected model");
    return PCILIB_ERROR_NOTSUPPORTED;
}


void *pcilib_get_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t *size) {
    int err;
    void *res = NULL;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_api_description_t *api = model_info->api;
    if (!api) {
	if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
	pcilib_error("Event API is not supported by the selected model");
	return NULL;
    }

    if (api->get_data) {
	err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, size, &res);
	if (err) {
	    if (size) *size = (size_t)err;
	    return NULL;
	}
	return res;
    }
    
    if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
    return NULL;
}

int pcilib_copy_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t size, void *buf, size_t *retsize) {
    int err;
    void *res = buf;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_api_description_t *api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->get_data) {
	err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, &size, &res);
	if (err) return err;
	
	if (buf != res) memcpy(buf, res, size);
	
	if (retsize) *retsize = size;
	return 0;
    }	

    return PCILIB_ERROR_NOTSUPPORTED;
}


void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size) {
    int err;
    void *res = NULL;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_api_description_t *api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
	return NULL;
    }

    if (api->get_data) {
	err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, size, &res);
	if (err) {
	    if (size) *size = (size_t)err;
	    return NULL;
	}
	return res;
    }

    if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
    return NULL;
}

int pcilib_copy_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t size, void *buf, size_t *ret_size) {
    int err;
    void *res = buf;
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_api_description_t *api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->get_data) {
	err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, &size, &res);
	if (err) return err;
	
	if (buf != res) memcpy(buf, res, size);

	if (ret_size) *ret_size = size;
	return 0;
    }

    return PCILIB_ERROR_NOTSUPPORTED;
}

int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data) {
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    const pcilib_event_api_description_t *api = model_info->api;
    if (!api) {
	pcilib_error("Event API is not supported by the selected model");
	return PCILIB_ERROR_NOTSUPPORTED;
    }

    if (api->return_data) 
	return api->return_data(ctx->event_ctx, event_id, data_type, data);

    return 0;
}


typedef struct {
    pcilib_t *ctx;
    
    size_t *size;
    void **data;
} pcilib_grab_callback_user_data_t;


static int pcilib_grab_callback(pcilib_event_t event, pcilib_event_id_t event_id, void *vuser) {
    int err;
    void *data;
    size_t size;
    int allocated = 0;

    pcilib_grab_callback_user_data_t *user = (pcilib_grab_callback_user_data_t*)vuser;

    data = pcilib_get_data(user->ctx, event_id, PCILIB_EVENT_DATA, &size);
    if (!data) {
	pcilib_error("Error getting event data");
	return -(int)size;
    }
    
    if (*(user->data)) {
	if ((user->size)&&(*(user->size) < size)) {
	    pcilib_error("The supplied buffer does not have enough space to hold the event data. Buffer size is %z, but %z is required", user->size, size);
	    return -PCILIB_ERROR_MEMORY;
	}

	*(user->size) = size;
    } else {
	*(user->data) = malloc(size);
	if (!*(user->data)) {
	    pcilib_error("Memory allocation (%i bytes) for event data is failed");
	    return -PCILIB_ERROR_MEMORY;
	}
	if (*(user->size)) *(user->size) = size;
	allocated = 1;
    }
    
    memcpy(*(user->data), data, size);
    
    err = pcilib_return_data(user->ctx, event_id, PCILIB_EVENT_DATA, data);
    if (err) {
	if (allocated) {
	    free(*(user->data));
	    *(user->data) = NULL;
	}
	pcilib_error("The event data had been overwritten before it was returned, data corruption may occur");
	return -err;
    }
    
    return PCILIB_STREAMING_CONTINUE;
}

int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout) {
    int err;
    pcilib_event_id_t eid;
    
    pcilib_grab_callback_user_data_t user = {ctx, size, data};
    
    err = pcilib_start(ctx, event_mask, PCILIB_EVENT_FLAGS_DEFAULT);
    if (!err) {
	if (timeout == PCILIB_TIMEOUT_IMMEDIATE) {
	     err = pcilib_trigger(ctx, event_mask, 0, NULL);
	     timeout = PCILIB_EVENT_TIMEOUT;
	}
    }
    if (!err) {
	err = pcilib_get_next_event(ctx, timeout, &eid, 0, NULL);
	if (!err) pcilib_grab_callback(event_mask, eid, &user);
    }
    pcilib_stop(ctx, PCILIB_EVENT_FLAGS_DEFAULT);
    return err;
}