File: ibconfig.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 (380 lines) | stat: -rw-r--r-- 9,069 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
/***************************************************************************
                          lib/ibconfig.c
                             -------------------

    copyright            : (C) 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ib_internal.h"

static int set_spoll_timeout(ibConf_t *conf, int timeout)
{
	if (timeout < TNONE || timeout > T1000s) {
		setIberr(EARG);
		return -1;
	}

	conf->settings.spoll_usec_timeout = timeout_to_usec(timeout);

	return 0;
}

static int set_ppoll_timeout(ibConf_t *conf, int timeout)
{
	if (timeout < TNONE || timeout > T1000s) {
		setIberr(EARG);
		return -1;
	}

	conf->settings.ppoll_usec_timeout = ppoll_timeout_to_usec(timeout);

	return 0;
}

static int set_t1_delay(ibBoard_t *board, int delay)
{
	unsigned int nano_sec;
	int retval;

	switch (delay) {
		case T1_DELAY_2000ns:
			nano_sec = 2000;
			break;
		case T1_DELAY_500ns:
			nano_sec = 500;
			break;
		case T1_DELAY_350ns:
			nano_sec = 350;
			break;
		default:
			fprintf(stderr, "libgpib: invalid T1 delay selection\n");
			setIberr(EARG);
			return -1;
			break;
	}

	retval = ioctl(board->fileno, IB_T1_DELAY, &nano_sec);
	if (retval < 0)	{
		setIberr(EDVR);
		setIbcnt(errno);
		return -1;
	}

	return 0;
}

static int set_local_parallel_poll_mode(ibBoard_t *board, int local)
{
	int retval;
	short local_mode;

	local_mode = local != 0;

	retval = ioctl(board->fileno, IBPP2_SET, &local_mode);
	if (retval < 0)	{
		setIberr(EDVR);
		setIbcnt(errno);
		return -1;
	}

	return 0;
}

/* Send CFE and CFGn to enable noninterlocked handshaking */
static int set_cable_length (ibConf_t *conf, int num_meters)
{
	int retval;
	uint8_t cmd[2];
	size_t length=0;

	if (num_meters < 0 || num_meters > 15) {
		setIberr (EARG);
		return -1;
	}

	cmd [length++] = CFE;
	if (num_meters)
		cmd [length++] = CFGn(num_meters);
	retval = my_ibcmd (conf, conf->settings.usec_timeout, cmd, length);
	if (retval != length)
		return -1;

	return 0;
}

int ibconfig(int ud, int option, int value)
{
	ibConf_t *conf;
	int retval;

	conf = enter_library(ud);
	if (!conf)
		return exit_library(ud, 1);

	switch (option) {
		case IbcPAD:
			retval = internal_ibpad(conf, value);
			if (retval < 0)
				return exit_library(ud, 1);
			return exit_library(ud, 0);
			break;
		case IbcSAD:
			retval = internal_ibsad(conf, value);
			if (retval < 0)
				return exit_library(ud, 1);
			return exit_library(ud, 0);
			break;
		case IbcTMO:
			retval = internal_ibtmo(conf, value);
			if (retval < 0)
				return exit_library(ud, 1);
			return exit_library(ud, 0);
			break;
		case IbcEOT:
			internal_ibeot(conf, value);
			return exit_library(ud, 0);
			break;
		case IbcEOSrd:
			if (value)
				conf->settings.eos_flags |= REOS;
			else
				conf->settings.eos_flags &= ~REOS;
			return exit_library(ud, 0);
			break;
		case IbcEOSwrt:
			if (value)
				conf->settings.eos_flags |= XEOS;
			else
				conf->settings.eos_flags &= ~XEOS;
			return exit_library(ud, 0);
			break;
		case IbcEOScmp:
			if (value)
				conf->settings.eos_flags |= BIN;
			else
				conf->settings.eos_flags &= ~BIN;
			return exit_library(ud, 0);
			break;
		case IbcEOSchar:
			if ((value & 0xff) != value) {
				setIberr(EARG);
				return exit_library(ud, 1);
			}
			conf->settings.eos = value;
			return exit_library(ud, 0);
			break;
		case IbcReadAdjust:
			// XXX
			if (value) {
				fprintf(stderr, "libgpib: byte swapping on reads not implemented\n");
				setIberr(ECAP);
				return exit_library(ud, 1);
			} else {
				return exit_library(ud, 0);
			}
			break;
		case IbcWriteAdjust:
			// XXX
			if (value) {
				fprintf(stderr, "libgpib: byte swapping on writes not implemented\n");
				setIberr(ECAP);
				return exit_library(ud, 1);
			} else {
				return exit_library(ud, 0);
			}
			break;
		case IbcEndBitIsNormal:
			if (value) {
				return exit_library(ud, 0);
			} else {
				fprintf(stderr, "libgpib: no support for END on EOI only yet \n");
				setIberr(ECAP);
				return exit_library(ud, 1);
			}
			break;
		default:
			break;
	}

	if (conf->is_interface)	{
		switch (option) {
			case IbcPPC:
				retval = internal_ibppc(conf, value);
				if (retval < 0)
					return exit_library(ud, 1);
				return exit_library(ud, 0);
				break;
			case IbcAUTOPOLL:
				retval = configure_autospoll(conf, value);
				if (retval < 0)
					return exit_library(ud, 1);
				return exit_library(ud, 0);
				break;
			case IbcCICPROT:
				// XXX
				if (value) {
					fprintf(stderr, "libgpib: pass control protocol not supported\n");
					setIberr(ECAP);
					return exit_library(ud, 1);
				}else
					return exit_library(ud, 0);
				break;
			case IbcIRQ:
				// XXX
				if (value == 0)	{
					fprintf(stderr, "libgpib: disabling interrupts not supported\n");
					setIberr(ECAP);
					return exit_library(ud, 1);
				} else {
					return exit_library(ud, 0);
				}
				break;
			case IbcSC:
				retval = internal_ibrsc(conf, value);
				if (retval < 0)
					return exit_library(ud, 1);
				else
					return exit_library(ud, 0);
				break;
			case IbcSRE:
				if (value)
					interfaceBoard(conf)->set_ren_on_sc = 1;
				else
					interfaceBoard(conf)->set_ren_on_sc = 0;
				return exit_library(ud, 0);
				break;
			case IbcPP2:
				retval = set_local_parallel_poll_mode(interfaceBoard(conf), value);
				if (retval < 0)
					return exit_library(ud, 1);
				return exit_library(ud, 0);
				break;
			case IbcTIMING:
				if (set_t1_delay(interfaceBoard(conf), value) < 0)
					return exit_library(ud, 1);
				return exit_library(ud, 0);
				break;
			case IbcDMA:
				// XXX
				if (value) {
					return exit_library(ud, 0);
				} else {
					fprintf(stderr, "libgpib: disabling DMA not supported\n");
					setIberr(ECAP);
					return exit_library(ud, 1);
				}
				break;
			case IbcEventQueue:
				if (value) {
					interfaceBoard(conf)->use_event_queue = 1;
				} else {
					interfaceBoard(conf)->use_event_queue = 0;
				}
				return exit_library(ud, 0);
				break;
			case IbcSPollBit:
/*				fprintf(stderr, "libgpib: SPOLL bit support not implemented\n");
				setIberr(ECAP);*/
				/* we currently have some support for SPOLL in the nec7210 driver,
				which is always enabled.  It can only detect the occurance of
				a serial poll if the board was requesting service when the
				poll occurred. */
				return exit_library(ud, 0);
				break;
			case IbcSendLLO:
				// XXX
				if (value) {
					fprintf(stderr, "libgpib: sending local lockout on device open not implemented\n");
					setIberr(ECAP);
					return exit_library(ud, 1);
				} else {
					return exit_library(ud, 0);
				}
				break;
			case IbcPPollTime:
				retval = set_ppoll_timeout(conf, value);
				if (retval < 0) {
					setIberr(EARG);
					return exit_library(ud, 1);
				} else {
					return exit_library(ud, 0);
				}
				break;
			case IbcHSCableLength:
				if (set_cable_length (conf, value) < 0) {
					return exit_library(ud, 1);
				} else {
					return exit_library(ud, 0);
				}
				break;
			case IbcIst:
				retval = internal_ibist(conf, value);
				if (retval < 0)
					return exit_library(ud, 1);
				else
					return exit_library(ud, 0);
				break;
			case IbcRsv:
				retval = internal_ibrsv2(conf, value, value & request_service_bit);
				if (retval < 0)
					return exit_library(ud, 1);
				else
					return exit_library(ud, 0);
				break;
			default:
				break;
		}
	} else {
		switch (option)	{
			case IbcREADDR:
				/* We always re-address.  To support only
				 * readdressing when necessary would require
				 * making the driver keep track of current addressing
				 * state.  Maybe someday, but low priority. */
				if (value)
					conf->settings.readdr = 1;
				else
					conf->settings.readdr = 0;
				return exit_library(ud, 0);
				break;
			case IbcSPollTime:
				retval = set_spoll_timeout(conf, value);
				if (retval < 0)	{
					setIberr(EARG);
					return exit_library(ud, 1);
				} else {
					return exit_library(ud, 0);
				}
				break;
			case IbcUnAddr:
				if (value)
					conf->settings.send_unt_unl = 1;
				else
					conf->settings.send_unt_unl = 0;
				return exit_library(ud, 0);
				break;
			case IbcBNA:
				retval = my_ibbna(conf, value);
				if (retval < 0)
					return exit_library(ud, 1);
				else
					return exit_library(ud, 0);
				break;
			default:
				break;
		}
	}

	setIberr(EARG);
	return exit_library(ud, 1);
}