summaryrefslogtreecommitdiff
path: root/xpp/cards.c
blob: dec05706d07d7bb3fc8adcdd51fc0a8e2dcd65df (plain)
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
#include <linux/module.h>
#include "xpd.h"
#include "xpp_zap.h"
#include "xpp_proto.h"
#include "cards.h"

static char rcsid[] = "$Id$";

extern	int print_dbg;
#include "zap_debug.h"

#define	MAX_SLIC_REGISTERS	100

struct FXS_private_data {
	slic_reply_t	last_slic_reply;
};

/*------------------------- FXS Functions --------------------------*/
int	FXS_card_new(xpd_t *xpd)
{
	xpd->direction = TO_PHONE;
	xpd->channels = min(8, CHANNELS_PERXPD);
	if(xpd->id == 0) {
		DBG("First XPD detected. Initialize digital outputs\n");
		xpd->channels += 2;
		xpd->digital_outputs = BIT(8) | BIT(9);	// Two extra channels
	}
	return 0;
}

int	FXS_card_remove(xpd_t *xpd)
{
	return 0;
}

static int FXS_card_startup(struct zt_span *span)
{
	DBG("\n");
	return 0;
}

/*
 * Called only for 'span' keyword in /etc/zaptel.conf
 */
static int FXS_card_spanconfig(struct zt_span *span, struct zt_lineconfig *lc)
{
	xpd_t *xpd = span->pvt;

	DBG("%s\n", xpd->xpdname);
	return 0;
}

/* Set signalling type (if appropriate) */
static int FXS_card_chanconfig(struct zt_chan *chan, int sigtype)
{
	DBG("channel %d (%s), sigtype %d.\n", chan->channo, chan->name, sigtype);
	dump_sigtype(print_dbg, "  ", sigtype);
	// FIXME: sanity checks:
	// - should be supported (within the sigcap)
	// - should not replace fxs <->fxo ??? (covered by previous?)
	return 0;
}

/*
 * Called only for 'span' keyword in /etc/zaptel.conf
 */
static int FXS_card_shutdown(struct zt_span *span)
{
	xpd_t *xpd = span->pvt;

	DBG("%s\n", xpd->xpdname);
	return 0;
}

static int FXS_card_sethook(struct zt_chan *chan, int hookstate)
{
	int pos = chan->chanpos - 1;
	xpd_t	*xpd = chan->pvt;
	xbus_t	*xbus;
	int ret = 0;

	if(!xpd) {
		ERR("%s: channel=%d without an XPD!\n", __FUNCTION__, pos);
		return -EINVAL;
	}
	xbus = xpd->xbus;
	// DBG("%s (%d) (old=0x%04X, hook-command=%d)\n", chan->name, pos, xpd->hookstate, hookstate);
	switch(hookstate) {
		/* On-hook, off-hook: The PBX is playing a phone on an FXO line. 
		 * Can be ignored for an FXS line
		 */
		case ZT_ONHOOK:
			if(IS_SET(xpd->digital_outputs, pos)) {
				DBG("ZT_ONHOOK %s digital output OFF\n", chan->name);
				ret = CALL_PROTO(RELAY_OUT, xpd->xbus, xpd, pos-8, 0);
				return ret;
			}
			DBG("ZT_ONHOOK: %s hookstate=0x%04X (stop ringing pos=%d)\n", chan->name, xpd->hookstate, pos);
			xpd->ringing[pos] = 0;
#if 1	// FIXME: Not needed -- verify
			ret = CALL_PROTO(RING, xbus, xpd, pos, 0);			// RING off
#endif
			ret = CALL_PROTO(LED, xpd->xbus, xpd, BIT(pos), LED_GREEN, 0);
			ret = CALL_PROTO(CHAN_POWER, xbus, xpd, BIT(pos), 0);		// Power down (prevent overheating!!!)
			if(ret) {
				DBG("ZT_ONHOOK(stop ring) Failed: ret=0x%02X\n", ret);
				break;
			}
			break;
		case ZT_START:
			DBG("ZT_START: %s hookstate=0x%04X (fall through ZT_OFFHOOK)\n", chan->name, xpd->hookstate);
			// Fall through
		case ZT_OFFHOOK:
			DBG("ZT_OFFHOOK: %s hookstate=0x%04X -- ignoring (FXS)\n", chan->name, xpd->hookstate);
			break;
		case ZT_WINK:
			DBG("ZT_WINK %s\n", chan->name);
			break;
		case ZT_FLASH:
			DBG("ZT_FLASH %s\n", chan->name);
			break;
		case ZT_RING:
			DBG("ZT_RING %s pos=%d (ringing[pos]=%d)\n", chan->name, pos, xpd->ringing[pos]);
			if(IS_SET(xpd->digital_outputs, pos)) {
				DBG("ZT_ONHOOK %s digital output ON\n", chan->name);
				ret = CALL_PROTO(RELAY_OUT, xpd->xbus, xpd, pos-8, 1);
				return ret;
			}
			xpd->ringing[pos] = RINGS_NUM*2;
			ret = CALL_PROTO(CHAN_POWER, xbus, xpd, (1 << pos), 1);	// Power up (for ring)
			ret = CALL_PROTO(RING, xbus, xpd, pos, 1);			// RING on
			if(ret) {
				DBG("ZT_RING Failed: ret=0x%02X\n", ret);
			}
			break;
		case ZT_RINGOFF:
			DBG("ZT_RINGOFF %s\n", chan->name);
			break;
		default:
			DBG("UNKNOWN hookstate=0x%X\n", hookstate);
	}
	return ret;
}

static int FXS_card_ioctl(struct zt_chan *chan, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	default:
		return xpp_ioctl(chan, cmd, arg);
	}
	return 0;
}

#if 0
int FXS_zaptel_setup(xpd_t *xpd)
{
	struct zt_chan	*cur_chan;
	struct zt_span	*span;
	xbus_t		*xbus;
	int		i;
	int		cn;

	BUG_ON(!xpd);

	sigfxs = ! (xpd->direction == TO_PHONE);	/* signaling is opposite */
	cn = xpd->channels;
	DBG("Initializing span: xpd %d have %d channels.\n", xpd->id, cn);

	xpd->chans = kmalloc(sizeof(struct zt_chan)*cn, GFP_ATOMIC);
	if (xpd->chans == NULL) {
		ERR("xpd: Unable to allocate channels\n");
		return -ENOMEM;
	}
	memset(xpd->chans, 0, sizeof(struct zt_chan)*cn);
	memset(&xpd->span, 0, sizeof(struct zt_span));

	span = &xpd->span;
	xbus = xpd->xbus;
	snprintf(span->name, MAX_SPANNAME, "%s/%s",
			xbus->busname, xpd->xpdname);
	{
		char tmp[MAX_SPANNAME];
		struct xpd_sim	*sim = &xbus->sim[xpd->id];

		if(sim->simulated)
			snprintf(tmp, MAX_SPANNAME, " (sim to=%d)", sim->loopto);
		else
			tmp[0] = '\0';

		snprintf(span->desc, MAX_SPANDESC, "Xorcom XPD #%d/%d: %s%s",
				xbus->num, xpd->id,
				(xpd->direction == TO_PHONE) ? "FXS" : "FXO",
				tmp
				);
	}
	for(i = 0; i < cn; i++) {
		
		cur_chan = &xpd->chans[i];
		DBG("setting channel %d\n", i);
		snprintf(cur_chan->name, MAX_CHANNAME, "XPP_FXS/%d-%d", xpd->id, i);
		cur_chan->chanpos = i + 1;
		cur_chan->pvt = xpd;
		cur_chan->sigcap =
#if 1
				ZT_SIG_FXOKS	|
				ZT_SIG_FXOLS	|
				ZT_SIG_FXOGS	|
#else
				ZT_SIG_SF	|
				ZT_SIG_EM	|
#endif
				0;
	}
	span->deflaw = ZT_LAW_MULAW;
	init_waitqueue_head(&span->maintq);
	span->pvt = xpd;
	span->channels = cn;
	span->chans = xpd->chans;

	span->startup = FXS_xpp_startup;
	span->shutdown = FXS_xpp_shutdown;
	span->spanconfig = FXS_xpp_spanconfig;
	span->chanconfig = FXS_xpp_chanconfig;
	span->open = xpp_open;
	span->close = xpp_close;
#ifdef	WITH_RBS
	span->flags = ZT_FLAG_RBS;
	span->hooksig = xpp_hooksig;	/* Only with RBS bits */
#else
	span->sethook = FXS_xpp_sethook;
#endif
	span->ioctl = FXS_xpp_ioctl;
	span->maint = xpp_maint;
#ifdef	CONFIG_ZAPTEL_WATCHDOG
	span->watchdog = xpp_watchdog;
#endif

	return 0;
}
#endif

int FXS_zaptel_cleanup(xpd_t *xpd)
{
	return 0;
}

/*------------------------- FXO Functions --------------------------*/
static int FXO_card_new(xpd_t *xpd)
{
	xpd->direction = TO_TRUNK;
	xpd->channels = min(8, CHANNELS_PERXPD);
	return 0;
}

static int FXO_card_remove(xpd_t *xpd)
{
	return 0;
}

static int FXO_card_startup(struct zt_span *span)
{
	DBG("\n");
	return 0;
}

/*
 * Called only for 'span' keyword in /etc/zaptel.conf
 */
static int FXO_card_spanconfig(struct zt_span *span, struct zt_lineconfig *lc)
{
	xpd_t *xpd = span->pvt;

	DBG("%s\n", xpd->xpdname);
	return 0;
}

/* Set signalling type (if appropriate) */
static int FXO_card_chanconfig(struct zt_chan *chan, int sigtype)
{
	DBG("channel %d (%s), sigtype %d.\n", chan->channo, chan->name, sigtype);
	dump_sigtype(print_dbg, "  ", sigtype);
	// FIXME: sanity checks:
	// - should be supported (within the sigcap)
	// - should not replace fxs <->fxo ??? (covered by previous?)
	return 0;
}

/*
 * Called only for 'span' keyword in /etc/zaptel.conf
 */
static int FXO_card_shutdown(struct zt_span *span)
{
	xpd_t *xpd = span->pvt;

	DBG("%s\n", xpd->xpdname);
	return 0;
}


static int FXO_card_sethook(struct zt_chan *chan, int hookstate)
{
	int pos = chan->chanpos - 1;
	xpd_t	*xpd = chan->pvt;
	xbus_t	*xbus;
	int ret = 0;

	BUG_ON(!xpd);
	xbus = xpd->xbus;
	// DBG("%s (%d) (old=0x%04X, hook-command=%d)\n", chan->name, pos, xpd->hookstate, hookstate);
	switch(hookstate) {
		/* On-hook, off-hook: The PBX is playing a phone on an FXO line. 
		 * Can be ignored for an FXS line
		 */
		case ZT_ONHOOK:
			if(IS_SET(xpd->digital_outputs, pos)) {
				DBG("ZT_ONHOOK %s digital output OFF\n", chan->name);
				ret = CALL_PROTO(RELAY_OUT, xpd->xbus, xpd, pos-8, 0);
				return ret;
			}
			DBG("ZT_ONHOOK: %s hookstate=0x%04X (pos=%d)\n", chan->name, xpd->hookstate, pos);
			xpd->ringing[pos] = 0;
			BIT_CLR(xpd->hookstate, pos);
			ret = CALL_PROTO(SETHOOK, xbus, xpd, xpd->hookstate);
			if(ret) {
				DBG("ZT_ONHOOK Failed: ret=0x%02X\n", ret);
				break;
			}
			break;
		case ZT_START:
			DBG("ZT_START: %s hookstate=0x%04X (fall through ZT_OFFHOOK)\n", chan->name, xpd->hookstate);
			// Fall through
		case ZT_OFFHOOK:
			DBG("ZT_OFFHOOK: %s hookstate=0x%04X (pos=%d)\n", chan->name, xpd->hookstate, pos);
			BIT_SET(xpd->hookstate, pos);
			xpd->ringing[pos] = 0;
			ret = CALL_PROTO(SETHOOK, xbus, xpd, xpd->hookstate);
			if(ret) {
				DBG("ZT_OFFHOOK Failed: ret=0x%02X\n", ret);
				break;
			}
			break;
		case ZT_WINK:
			DBG("ZT_WINK %s\n", chan->name);
			break;
		case ZT_FLASH:
			DBG("ZT_FLASH %s\n", chan->name);
			break;
		case ZT_RING:
			DBG("ZT_RING %s pos=%d (ringing[pos]=%d)\n", chan->name, pos, xpd->ringing[pos]);
			break;
		case ZT_RINGOFF:
			DBG("ZT_RINGOFF %s\n", chan->name);
			break;
		default:
			DBG("UNKNOWN hookstate=0x%X\n", hookstate);
	}
	return ret;
}

static int FXO_card_ioctl(struct zt_chan *chan, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	default:
		return xpp_ioctl(chan, cmd, arg);
	}
	return 0;
}


/*------------------------- Function table -------------------------*/

#define	DEF_(t)	\
	[XPD_TYPE_ ## t] {					\
		.card_new = t ## _card_new,			\
		.card_remove = t ## _card_remove,		\
		.card_startup = t ## _card_startup,		\
		.card_shutdown = t ## _card_shutdown,		\
		.card_spanconfig = t ## _card_spanconfig,	\
		.card_chanconfig = t ## _card_chanconfig,	\
		.card_sethook = t ## _card_sethook,		\
		.card_ioctl = t ## _card_ioctl,			\
		}

xops_t	xpd_card_ops[XPD_TYPE_NOMODULE] = {
	DEF_(FXS),
	DEF_(FXO)
};

xops_t	*get_xops(xpd_type_t xpd_type)
{
	if(xpd_type >= XPD_TYPE_NOMODULE)
		return NULL;
	return &xpd_card_ops[xpd_type];
}