File: ibutil.c

package info (click to toggle)
linux-gpib-user 4.3.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,760 kB
  • sloc: ansic: 10,381; perl: 1,120; xml: 375; makefile: 335; yacc: 335; tcl: 308; python: 173; php: 157; lex: 144; sh: 134; lisp: 94
file content (656 lines) | stat: -rw-r--r-- 13,854 bytes parent folder | download | duplicates (2)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/***************************************************************************
                          lib/ibutil.c
                             -------------------

	copyright            : (C) 2001,2002 by Frank Mori Hess
    email                : fmhess@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#define _GNU_SOURCE

#include "ib_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include "parse.h"

ibConf_t *ibConfigs[GPIB_CONFIGS_LENGTH] = {NULL};
ibConf_t ibFindConfigs[FIND_CONFIGS_LENGTH];

int insert_descriptor(ibConf_t p, int ud)
{
	int i;

	if (ud < 0) {
		for (i = GPIB_MAX_NUM_BOARDS; i < GPIB_CONFIGS_LENGTH; i++) {
			if (ibConfigs[ i ] == NULL) break;
		}
		if (i == GPIB_CONFIGS_LENGTH) {
			fprintf(stderr, "libgpib: out of room in ibConfigs[]\n");
			setIberr(ETAB);
			return -1;
		}
		ud = i;
	} else {
		if (ud >= GPIB_CONFIGS_LENGTH) {
			fprintf(stderr, "libgpib: bug! tried to allocate past end if ibConfigs array\n");
			setIberr(EDVR);
			setIbcnt(EINVAL);
			return -1;
		}
		if (ibConfigs[ud]) {
			fprintf(stderr, "libgpib: bug! tried to allocate board descriptor twice\n");
			setIberr(EDVR);
			setIbcnt(EINVAL);
			return -1;
		}
	}
	ibConfigs[ud] = malloc(sizeof(ibConf_t));
	if (ibConfigs[ud] == NULL) {
		fprintf(stderr, "libgpib: out of memory\n");
		setIberr(EDVR);
		setIbcnt(ENOMEM);
		return -1;
	}
	/* put entry to the table */
	*ibConfigs[ud] = p;

	return ud;
}

int release_descriptor(int ud)
{
	if (ud >= GPIB_MAX_NUM_BOARDS  && ud < GPIB_CONFIGS_LENGTH && ibConfigs[ud]) {
		// need to take more care to clean up before freeing XXX
		free(ibConfigs[ud]);
		ibConfigs[ud] = NULL;
		return 0;
	}
	return -1;
}

int setup_global_board_descriptors(void)
{
	int i;
	int retval = 0;

	for (i = 0; i < FIND_CONFIGS_LENGTH; i++) {
		if (ibFindConfigs[i].is_interface && ibFindConfigs[i].settings.board >= 0 &&
		    ibFindConfigs[i].settings.board < GPIB_MAX_NUM_BOARDS) {
			if (insert_descriptor(ibFindConfigs[i], ibFindConfigs[i].settings.board) < 0)
				retval = -1;
		}
	}
	/* boards use handle 0 */
	for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++)
		if (ibConfigs[i])
			ibConfigs[i]->handle = 0;
	return retval;
}

static pthread_mutex_t config_lock = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;

static void gpib_atfork_prepare(void)
{
	int i;

	for (i = 0; i < GPIB_CONFIGS_LENGTH; i++)
		if (ibConfigs[i]) {
			pthread_mutex_lock(&ibConfigs[i]->async.lock);
			pthread_mutex_lock(&ibConfigs[i]->async.join_lock);
		}
	pthread_mutex_lock(&config_lock);
}

static void gpib_atfork_parent(void)
{
	int i;

	pthread_mutex_unlock(&config_lock);
	for (i = 0; i < GPIB_CONFIGS_LENGTH; i++)
		if (ibConfigs[i]) {
			pthread_mutex_unlock(&ibConfigs[i]->async.join_lock);
			pthread_mutex_unlock(&ibConfigs[i]->async.lock);
		}
}

static void gpib_atfork_child(void)
{
	int i;

	pthread_mutex_init(&config_lock, NULL);
	for (i = 0; i < GPIB_CONFIGS_LENGTH; i++)
		if (ibConfigs[i]) {
			pthread_mutex_init(&ibConfigs[i]->async.join_lock, NULL);
			pthread_mutex_init(&ibConfigs[i]->async.lock, NULL);
		}
}

int ibParseConfigFile(int minor)
{
	int retval = 0;
	static volatile int config_parsed = 0;
	static volatile int config_error = 0;
	char *filename, *envptr;
	ibBoard_t *board;
	ibConf_t *conf;
	struct gpib_board_info_ioctl info;

	pthread_mutex_lock(&config_lock);
	if (config_parsed) {
		pthread_mutex_unlock(&config_lock);
		return config_error;
	}

	envptr = getenv("IB_CONFIG");
	if (envptr)
		filename = envptr;
	else
		filename = DEFAULT_CONFIG_FILE;

	retval = parse_gpib_conf(filename, ibFindConfigs, FIND_CONFIGS_LENGTH,
				 ibBoard, GPIB_MAX_NUM_BOARDS ,minor);
	if (retval < 0)	{
		setIberr(ECNF);
		pthread_mutex_unlock(&config_lock);
		return retval;
	}
	retval = setup_global_board_descriptors();

	if (minor < 0)  /* called from ibfind so interface entry must be present */
		goto done;

	/* check whether parse_gpib_config created an interface entry and if so
	   update the details set by gpib_config from driver */
	conf = ibConfigs[minor];
	if (conf->settings.pad == -1) {
		if (conf->settings.board != minor) {
			fprintf(stderr,"Inconsistent config detected board %d != minor %d\n",
				conf->settings.board, minor);
			setIberr(ECNF);
			retval = -1;
			goto done;
		}
		board = interfaceBoard(conf);
		retval = ibBoardOpen(board, 1);
		if (retval < 0)
			goto done;

		retval = ioctl(board->fileno, IBBOARD_INFO, &info);
		if (retval < 0) {
			setIberr(EDVR);
			setIbcnt(errno);
			goto done;
		}
		ibBoardClose(board);
		conf->defaults.pad = info.pad;
		conf->defaults.sad = info.sad;
		conf->settings = conf->defaults;
		board->is_system_controller = info.is_system_controller;
		// fprintf(stderr, "updated config pad %d, sad %d, sc %d\n",
		//	info.pad, info.sad, info.is_system_controller);
	}
done:
	config_parsed = 1;
	config_error  = retval;
	/* be extra safe about dealing with forks */
	pthread_atfork(gpib_atfork_prepare, gpib_atfork_parent, gpib_atfork_child);
	pthread_mutex_unlock(&config_lock);

	return retval;
}


int ibFindDevIndex(const char *name)
{
	int i;

	if (strcmp("", name) == 0)
		return -1;

	for (i = 0; i < FIND_CONFIGS_LENGTH; i++) {
		if (!strcmp(ibFindConfigs[i].name, name))
			return i;
	}

	return -1;
}

static int ibCheckDescriptor(int ud)
{
	if (ud < 0 || ud >= GPIB_CONFIGS_LENGTH || ibConfigs[ud] == NULL) {
		setIberr(EARG);
		return -1;
	}

	return 0;
}

void init_descriptor_settings(descriptor_settings_t *settings)
{
	settings->pad = -1;
	settings->sad = -1;
	settings->board = -1;
	settings->usec_timeout = 3000000;
	settings->spoll_usec_timeout = 1000000;
	settings->ppoll_usec_timeout = 2;
	settings->eos = 0;
	settings->eos_flags = 0;
	settings->ppoll_config = 0;
	settings->send_eoi = 1;
	settings->local_lockout = 0;
	settings->readdr = 0;
	settings->send_unt_unl = 0;
}

void init_ibconf(ibConf_t *conf)
{
	conf->handle = -1;
	memset(conf->name, 0, sizeof(conf->name));
	init_descriptor_settings(&conf->defaults);
	init_descriptor_settings(&conf->settings);
	memset(conf->init_string, 0, sizeof(conf->init_string));
	conf->flags = 0;
	init_async_op(&conf->async);
	conf->end = 0;
	conf->is_interface = 0;
	conf->board_is_open = 0;
	conf->has_lock = 0;
	conf->timed_out = 0;
	conf->error_msg_disable = 0;
}

int open_gpib_handle(ibConf_t *conf)
{
	struct gpib_open_dev_ioctl open_cmd;
	int retval;
	ibBoard_t *board;

	if (conf->handle >= 0) return 0;

	board = interfaceBoard(conf);

	open_cmd.handle = 0;
	open_cmd.pad = conf->settings.pad;
	open_cmd.sad = conf->settings.sad;
	open_cmd.is_board = conf->is_interface;
	retval = ioctl(board->fileno, IBOPENDEV, &open_cmd);
	if (retval < 0)	{
		if (!conf->error_msg_disable)
			fprintf(stderr, "libgpib: IBOPENDEV ioctl failed\n");
		setIberr(EDVR);
		setIbcnt(errno);
		return retval;
	}

	conf->handle = open_cmd.handle;

	return 0;
}

int close_gpib_handle(ibConf_t *conf)
{
	struct gpib_close_dev_ioctl close_cmd;
	int retval;
	ibBoard_t *board;

	if (conf->handle < 0)
		return 0;

	board = interfaceBoard(conf);

	close_cmd.handle = conf->handle;
	retval = ioctl(board->fileno, IBCLOSEDEV, &close_cmd);
	if (retval < 0)	{
		setIberr(EDVR);
		setIbcnt(errno);
		return retval;
	}

	conf->handle = -1;

	return 0;
}

int lock_board_mutex(ibBoard_t *board)
{
	static const int lock = 1;
	int retval;

	retval = ioctl(board->fileno, IBMUTEX, &lock);
	if (retval < 0)	{
		fprintf(stderr, "libgpib: error locking board mutex!\n");
		setIberr(EDVR);
		setIbcnt(errno);
	}

	return retval;
}

int unlock_board_mutex(ibBoard_t *board)
{
	static const int unlock = 0;
	int retval;

	retval = ioctl(board->fileno, IBMUTEX, &unlock);
	if (retval < 0)	{
		fprintf(stderr, "libgpib: error unlocking board mutex!\n");
		setIberr(EDVR);
		setIbcnt(errno);
	}
	return retval;
}

int conf_lock_board(ibConf_t *conf)
{
	ibBoard_t *board;
	int retval;

	board = interfaceBoard(conf);

	assert(conf->has_lock == 0);

	retval = lock_board_mutex(board);
	if (retval < 0)
		return retval;

	conf->has_lock = 1;

	return retval;
}

void conf_unlock_board(ibConf_t *conf)
{
	ibBoard_t *board;
	int retval;

	board = interfaceBoard(conf);

	assert(conf->has_lock);

	conf->has_lock = 0;

	retval = unlock_board_mutex(board);
	assert(retval == 0);
}

ibConf_t * enter_library(int ud)
{
	return general_enter_library(ud, 0, 0);
}

ibConf_t * general_enter_library(int ud, int no_lock_board, int ignore_eoip)
{
	ibConf_t *conf;
	int retval;

	retval = ibParseConfigFile(ud);
	if (retval < 0)
		return NULL;

	setIberr(0);
	setIbcnt(0);

	if (ibCheckDescriptor(ud) < 0)
		return NULL;
	conf = ibConfigs[ud];

	retval = conf_online(conf, 1);
	if (retval < 0)
		return NULL;

	conf->timed_out = 0;

	if (no_lock_board == 0)	{
		if (ignore_eoip == 0) {
			pthread_mutex_lock(&conf->async.lock);
			if (conf->async.in_progress) {
				pthread_mutex_unlock(&conf->async.lock);
				setIberr(EOIP);
				return NULL;
			}
			pthread_mutex_unlock(&conf->async.lock);
		}

		retval = conf_lock_board(conf);
		if (retval < 0)
			return NULL;
	}

	return conf;
}

int ibstatus(ibConf_t *conf, int error, int clear_mask, int set_mask)
{
	int status = 0;
	int retval;

	if (!error) {
		retval = my_wait(conf, 0, clear_mask, set_mask, &status);
		if (retval < 0)
			error = 1;
	}

	if (error)
		status |= ERR;
	if (conf->timed_out)
		status |= TIMO;
	if (conf->end)
		status |= END;

	setIbsta(status);

	return status;
}

int exit_library(int ud, int error)
{
	return general_exit_library(ud, error, 0, 0, 0, 0, 0);
}

int general_exit_library(int ud, int error, int no_sync_globals, int no_update_ibsta,
	int status_clear_mask, int status_set_mask, int no_unlock_board)
{
	ibConf_t *conf;
	int status;

	if (ibCheckDescriptor(ud) < 0) {
		setIbsta(ERR);
		if (no_sync_globals == 0)
			sync_globals();
		return ERR;
	}

	conf = ibConfigs[ud];

	if (no_update_ibsta)
		status = ThreadIbsta();
	else
		status = ibstatus(conf, error, status_clear_mask, status_set_mask);

	if (no_unlock_board == 0 && conf->has_lock)
		conf_unlock_board(conf);

	if (no_sync_globals == 0)
		sync_globals();

	return status;
}

int extractPAD(Addr4882_t address)
{
	int pad = address & 0xff;

	if (address == NOADDR)
		return ADDR_INVALID;

	if (pad < 0 || pad > gpib_addr_max)
		return ADDR_INVALID;

	return pad;
}

int extractSAD(Addr4882_t address)
{
	int sad = (address >> 8) & 0xff;

	if (address == NOADDR)
		return ADDR_INVALID;

	if (sad == NO_SAD)
		return SAD_DISABLED;

	if ((sad & 0x60) == 0)
		return ADDR_INVALID;

	sad &= ~0x60;

	if (sad < 0 || sad > gpib_sad_max)
		return ADDR_INVALID;

	return sad;
}

Addr4882_t packAddress(unsigned int pad, int sad)
{
	Addr4882_t address;

	address = 0;
	address |= pad & 0xff;
	if (sad >= 0)
		address |= ((sad | sad_offset) << 8) & 0xff00;

	return address;
}

int addressIsValid(Addr4882_t address)
{
	if (address == NOADDR)
		return 1;

	if (extractPAD(address) == ADDR_INVALID ||
	    extractSAD(address) == ADDR_INVALID) {
		setIberr(EARG);
		return 0;
	}

	return 1;
}

int addressListIsValid(const Addr4882_t addressList[])
{
	int i;

	if (addressList == NULL) return 1;

	for (i = 0; addressList[i] != NOADDR; i++) {
		if (!addressIsValid(addressList[i])) {
			setIbcnt(i);
			return 0;
		}
	}

	return 1;
}

unsigned int numAddresses(const Addr4882_t addressList[])
{
	unsigned int count;

	if (addressList == NULL)
		return 0;

	count = 0;
	while (addressList[ count ] != NOADDR)
		count++;

	return count;
}

int is_cic(const ibBoard_t *board)
{
	int retval;
	struct gpib_wait_ioctl cmd;

	cmd.usec_timeout = 0;
	cmd.wait_mask = 0;
	cmd.clear_mask = 0;
	cmd.set_mask = 0;
	cmd.pad = NOADDR;
	cmd.sad = NOADDR;
	cmd.handle = 0;
	cmd.ibsta = 0;
	retval = ioctl(board->fileno, IBWAIT, &cmd);
	if (retval < 0)	{
		setIberr(EDVR);
		setIbcnt(errno);
//		fprintf(stderr, "libgpib: error in is_cic()!\n");
		return -1;
	}

	if (cmd.ibsta & CIC)
		return 1;

	return 0;
}

int is_system_controller(const ibBoard_t *board)
{
	int retval;
	struct gpib_board_info_ioctl info;

	retval = ioctl(board->fileno, IBBOARD_INFO, &info);
	if (retval < 0)	{
		setIberr(EDVR);
		setIbcnt(errno);
//	fprintf(stderr, "libgpib: error in is_system_controller()!\n");
		return -1;
	}

	return info.is_system_controller;
}

const char* gpib_error_string(int error)
{
	static const char* error_descriptions[] =
	{
		"EDVR 0: OS error",
		"ECIC 1: Board not controller in charge",
		"ENOL 2: No listeners",
		"EADR 3: Improper addressing",
		"EARG 4: Bad argument",
		"ESAC 5: Board not system controller",
		"EABO 6: Operation aborted",
		"ENEB 7: Non-existant board",
		"EDMA 8: DMA error",
		"libgpib: Unknown error code 9",
		"EOIP 10: IO operation in progress",
		"ECAP 11: Capability does not exist",
		"EFSO 12: File system error",
		"libgpib: Unknown error code 13",
		"EBUS 14: Bus error",
		"ESTB 15: Lost status byte",
		"ESRQ 16: Stuck service request",
		"ECNF 17: Configuration file error",
		"libgpib: Unknown error code 18",
		"libgpib: Unknown error code 19",
		"ETAB 20: Table problem",
	};
	static const int max_error_code = ETAB;

	if (error < 0 || error > max_error_code)
		return "libgpib: Unknown error code";

	return error_descriptions[error];
}