File: sccinit.c

package info (click to toggle)
z8530-utils2 3.0-1-8
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 252 kB
  • ctags: 113
  • sloc: ansic: 1,217; makefile: 94; sh: 6
file content (489 lines) | stat: -rw-r--r-- 9,568 bytes parent folder | download | duplicates (8)
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
/* 
	   sccinit.c - Initialize the z8530 SCC driver
   
   Copyright 1994-1996, by Joerg Reuter dl1bke@lykos.oche.de

   This program is intended for Amateur Radio use. If you are running it
   for commercial purposes, please drop me a note. I am nosy...

   ! You  m u s t  recognize the appropriate legislations of your country !
   ! before you connect a radio to the SCC board and start to transmit or !
   ! receive. The GPL allows you to use the  d r i v e r,  NOT the RADIO! !

   For non-Amateur-Radio use please note that you might need a special
   allowance/licence from the designer of the SCC Board and/or the
   MODEM. 

   This program is free software; you can redistribute it and/or modify 
   it under the terms of the (modified) GNU General Public License 
   delivered with the LinuX kernel source.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should find a copy of the GNU General Public License in 
   /usr/src/linux/COPYING; 

*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <ctype.h>
#include <sys/ioctl.h>
#ifdef CONFIG_SCC_STANDALONE
#include "scc.h"
#else
#include <linux/scc.h>
#endif
#include <linux/tty.h>
#include <net/if.h>
#include <sys/socket.h>

#include "sccutils.h"

#define RCS_ID "$Id: sccinit.c,v 1.7 1995/11/17 01:10:17 jreuter Exp jreuter $"

struct cmds {
		char *name;
		int  token;
	    };
	    
#define I_DEVICE	0x80
#define I_MODE		0x81
#define I_CLOCK		0x82
#define I_SPEED		0x83
#define I_BOARD		0x84
#define I_CTRLPA	0x85
#define I_CTRLPB	0x86
#define I_DATAPA	0x87
#define I_DATAPB	0x88
#define I_ESCC		0x89
#define I_IRQ		0x8a
#define I_OPTION	0x8b
#define I_PCLOCK	0x8c
#define I_SPECIAL	0x8d
#define I_VECTOR	0x8e
#define I_CHIP		0x8f
#define I_BUFSIZE	0x90
#define I_RXBUFFERS	0x91
#define I_TXBUFFERS	0x92
#define I_WARN		0x93

#define P_OFF		-1
#define P_NULL		0
#define P_ON		1
#define P_YES		1
#define P_NRZ		4
#define P_NRZI		5
#define P_DEVICE	6
    
struct cmds clist[] =  {{"#",		0},
			{"board",	I_BOARD},
			{"bufsize",	I_BUFSIZE},
			{"chip",	I_CHIP},
			{"clocksrc",	I_CLOCK},
			{"ctrl_a",	I_CTRLPA},
			{"ctrl_b",	I_CTRLPB},
			{"data_a",	I_DATAPA},
			{"data_b",	I_DATAPB},
			{"device",	I_DEVICE},
			{"escc",	I_ESCC},
			{"fullduplex", 	PARAM_FULLDUP},
			{"group",	PARAM_GROUP},
			{"idletime",	PARAM_IDLE},
			{"irq",		I_IRQ},
			{"maxdefer",	PARAM_MAXDEFER},
			{"maxkeyup",	PARAM_MAXKEY},
			{"mintime",	PARAM_MIN},
			{"mode",	I_MODE},
			{"option",	I_OPTION},
			{"pclock",	I_PCLOCK},
			{"persistence",	PARAM_PERSIST},
			{"rxbuffers",	I_WARN},
			{"slottime",	PARAM_SLOTTIME},
			{"slip",	I_WARN},
			{"special",	I_SPECIAL},
			{"speed",	I_SPEED},
			{"softdcd",	PARAM_SOFTDCD},
			{"tailtime",	PARAM_TXTAIL},
			{"txbuffers",	I_WARN},
			{"txdelay",	PARAM_TXDELAY},
			{"txoff",	PARAM_TX},
			{"txtailtime",	PARAM_TXTAIL},
			{"vector",	I_VECTOR},
			{"waittime",	PARAM_WAIT},
			{"", 0}
};

struct cmds plist[] = {	{"divider",	CLK_DIVIDER},
			{"dpll",	CLK_DPLL},
			{"external",	CLK_EXTERNAL},
			{"no", 		P_OFF},
			{"normal",	CLK_DPLL},
			{"nrz",		P_NRZ},
			{"nrzi",	P_NRZI},
			{"off", 	P_OFF},
			{"on", 		P_ON}, 
			{"yes", 	P_ON},
			{"pa0hzp",      PA0HZP},
			{"eagle",	EAGLE},
			{"pc100",	PC100},
			{"primus",	PRIMUS},
			{"drsi",	DRSI},
			{"baycom",	BAYCOM},
			{"", 0}
};

char Board = 0;
io_port Vector = 0;
long Clock = 4915200;
int Irq = 0;

int parse_param(struct cmds *list, char *s)
{
	struct cmds *c;
	
	if (!*s) return 0;
	
	for (c = &list[0]; *(c->name) != '\0' ; c++)
	{
		if (!strncmp(s, c->name, strlen(s)))
			return c->token;
	}
	
	return -2;
}

#define IS_WHITESPACE(x) ((x == ' ') || (x == '\t'))

void scan_line(char *s, char *cmd, char *par)
{
	char *p, *q;
	char buf[256];
	int k, n;
	
	*cmd='\0';
	*par='\0';
	
	p=s; 
	while(*p)
	{
		*p=tolower(*p);
		p++;
	}
	
	for (p=s, k=0; k < 2; k++)
	{
		n = 0;
		
		while (*p)
		{
			if (!IS_WHITESPACE(*p)) break;
			p++;
		}
		
		if (!*p) return;
		
		q = p;
		
		while (*p)
		{
			if (IS_WHITESPACE(*p) || *p == '\n') break;
			p++; n++;
		}
		
		if (!n) return;
		
		n = (n>19? 19:n);
		strncpy(buf, q, n);
		buf[n] = '\0';
		
		strcpy(k? par:cmd, buf);
	}
}
		

#define ISDIGIT(x) ((x >= '0') && (x <= '9'))
#define WRNGPARA "sccinit(): wrong parameter '%s' to '%s' in line %u\n"

int main(int argc, char **argv)
{
	FILE *fp;
	int  fd;
	char s[80];
	char cmd[20];
	char val[20];
	char flag, hwinit;
	int  k, command, param;
	
	struct scc_modem modem;
	struct scc_hw_config hwcfg;
	struct scc_mem_config memcfg;
	struct scc_kiss_cmd kcmd;

	struct ifreq ifr;
	
	check_version();
	
	fp = fopen(CONFIGFILE,"r");
	fd = 0;
	flag = 1;
	hwinit = 1;
	
	memset(&hwcfg, 0, sizeof(hwcfg));
	memset(&memcfg, 0, sizeof(memcfg));
	memset(&modem, 0, sizeof(modem));
	
	fd = socket(AF_AX25, SOCK_DGRAM, 0);
	strcpy(ifr.ifr_name, "scc0");
	
	k = 0;
	
	while (!feof(fp))
	{
		k++;

		fgets(s, 255, fp);

		scan_line(s, cmd, val);
		if ((command = parse_param(clist, cmd)) < 0)
		{
			printf("sccinit(): unknown command %s in line %u\n", cmd, k);
			continue;
		}

		if (command == 0) continue;

		if (!*val)
		{
			printf("sccinit(): parameter is missing in line %u\n", k);
			continue;
		}		

		if (ISDIGIT(*val))
		{
			if (*(val+1) == 'x')
				sscanf(val, "%x", &param);
			else
				param = atoi(val);
		} else {
			param = parse_param(plist, val);

			if ((param < -1) && (command != I_DEVICE))
			{
				printf(WRNGPARA, val, cmd, k);
				continue;
			}
		}

		if (command == I_DEVICE)
		{		
			if (hwinit)
			{
				ifr.ifr_data = (caddr_t) &hwcfg;
				ioctl(fd, SIOCSCCCFG, &ifr);
				ifr.ifr_data = (caddr_t) &k;

				if (ioctl(fd, SIOCSCCINI, &ifr))
				{
					perror("SIOCSCCINI: ");
					exit(1);
				}

				hwinit = 0;
			}
			
			if (*val == '/')
				strcpy(ifr.ifr_name, val+5);
			else
				strcpy(ifr.ifr_name, val);
		}
		
		if (command == I_CLOCK)
		{
			switch (param)
			{
				case CLK_DPLL:
				case CLK_EXTERNAL:
				case CLK_DIVIDER: break;
				
				case P_OFF:	  param = CLK_DPLL; break;
				
				default:	printf(WRNGPARA, val, cmd, k);
					 	param=0;
					 	continue;
			}
		}
		
		if (command == I_MODE)
		{
			switch (param)
			{
				case P_NRZI:	param=0; break;
				case P_NRZ:	param=1; break;
				default: 	printf(WRNGPARA, val, cmd, k);
					 	param=0;
					 	continue;
			}
		}
		
		switch(param)
		{
			case P_OFF:
				switch(command)
				{
					case PARAM_MAXKEY:
					case PARAM_MIN:
					case PARAM_MAXDEFER:
					case PARAM_IDLE: param = TIMER_OFF; break;
					case PARAM_PERSIST: param = 255; break;
					default: param = 0;
				}
				break;
				
			case P_NULL:
				switch(command)
				{
					case PARAM_MAXKEY:
					case PARAM_MIN:
					case PARAM_MAXDEFER: param = TIMER_OFF; break;
				}
				break;
		}
		
		
		if (fd)
		{
			if (hwinit)
			{
				switch (command)
				{
					case I_CHIP:
						if (!flag)
						{
							ifr.ifr_data = (caddr_t) &hwcfg;
							if (ioctl(fd, SIOCSCCCFG, &ifr) < 0)
							{
								perror("FATAL: ioctl(SIOCSCCCFG) on scc0: ");
								fprintf(stderr,"Sccinit aborted\n");
								exit(1);
							}
						}
						

						memset(&hwcfg, 0, sizeof(hwcfg));
						hwcfg.irq = Irq;
						hwcfg.brand = Board;
						hwcfg.vector_latch = Vector;
						hwcfg.clock = Clock;
						flag = 0;
						
						break;
						
					case I_BOARD:
						Board = (char) param;
						hwcfg.brand = (char) param;
						break;
					case I_CTRLPA:
						hwcfg.ctrl_a = (io_port) param;
						break;
					case I_CTRLPB:
						hwcfg.ctrl_b = (io_port) param;
						break;
					case I_DATAPA:
						hwcfg.data_a = (io_port) param;
						break;
					case I_DATAPB:
						hwcfg.data_b = (io_port) param;
						break;
					case I_PCLOCK:
						hwcfg.clock = (long) param;
						break;
					case I_IRQ:
						Irq = (int) param;
						hwcfg.irq = Irq;
						break;
					case I_SPECIAL:
						hwcfg.special = (io_port) param;
						break;
					case I_OPTION:
						hwcfg.option = (char) param;
						break;
					case I_VECTOR:
						hwcfg.vector_latch = (io_port) param;
						break;
					case I_ESCC:
						hwcfg.escc = (char) param;
						break;
					default:
						break;/* error */
				}
			} else {
				switch (command)
				{
					case I_DEVICE: 
						flag = 0;
						memset(&modem, 0, sizeof(modem));
						break;
					
					case I_MODE:
						modem.nrz = (char) param;
						break;
						
					case I_CLOCK:
						modem.clocksrc = (char) param;
						break;
					
					case I_SPEED:
						modem.speed = (long) param;
						break;
						
					case I_BUFSIZE:
						memcfg.bufsize = (unsigned int) param;
						break;
					
					case I_WARN:
						fprintf(stderr, "Warning: parameter '%s' now unsupported\n", cmd);
						break;
					
					default:
						if (!flag)
						{
							if (!modem.speed) 
								modem.speed=1200;
							
							ifr.ifr_data = (caddr_t) &modem;
							ioctl(fd, SIOCSCCCHANINI, &ifr);
							
							if (!memcfg.bufsize)
								memcfg.bufsize = 384;
		
							ifr.ifr_data = (caddr_t) &memcfg;
							ioctl(fd, SIOCSCCSMEM, &ifr);
		
							memset(&memcfg, 0, sizeof(memcfg));
							flag = 1;
	
						}
						
						kcmd.command = command;
						kcmd.param = (long) param;
						
						ifr.ifr_data = (caddr_t) &kcmd;
						ioctl(fd, SIOCSCCSKISS, &ifr);
				}
			}
		} else {
			printf("no device: %s ignored",s);
		}
	}
	
	fclose(fp);
	return 0;
}