summaryrefslogtreecommitdiff
path: root/xpp/xpd.h
blob: ca2d357488266bf3523c4e6ca5c5ad381bd4ffb0 (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
#ifndef	XPD_H
#define	XPD_H

/*
 * Written by Oron Peled <oron@actcom.co.il>
 * Copyright (C) 2004-2006, Xorcom
 *
 * All rights reserved.
 *
 * 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.
 *
 * 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 have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "xdefs.h"
#include "xproto.h"

#ifdef	__KERNEL__
#include <linux/kernel.h>
#include <linux/device.h>
#include <asm/atomic.h>
#include <asm/semaphore.h>
#include <linux/moduleparam.h>
#ifdef	XPP_DEBUGFS
#ifndef	CONFIG_DEBUG_FS
#warning kernel does not include CONFIG_DEBUG_FS, canceling XPP_DEBUGFS support
#undef	XPP_DEBUGFS
#else
#include <linux/debugfs.h>
#endif
#endif
#endif	/* __KERNEL__ */

#include <zaptel.h>

#ifdef __KERNEL__
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
/* also added in RHEL kernels with the OpenInfiniband backport: */
#if LINUX_VERSION_CODE != KERNEL_VERSION(2,6,9) || !defined(DEFINE_SPINLOCK)
typedef	unsigned gfp_t;		/* Added in 2.6.14 */
#endif
#endif

/*
 * FIXME: Kludge for 2.6.19
 * bool is now defined as a proper boolean type (gcc _Bool)
 * but the command line parsing framework handles it as int.
 */
#define	DEF_PARM_BOOL(name,init,perm,desc)	\
	int name = init;	\
	module_param(name, bool, perm);		\
	MODULE_PARM_DESC(name, desc " [default " #init "]")

#define	DEF_PARM(type,name,init,perm,desc)	\
	type name = init;	\
	module_param(name, type, perm);		\
	MODULE_PARM_DESC(name, desc " [default " #init "]")

#if	LINUX_VERSION_CODE	< KERNEL_VERSION(2,6,10)
/*
 * Old 2.6 kernels had module_param_array() macro that receive the counter
 * by value.
 */
#define	DEF_ARRAY(type,name,count,init,desc)	\
	unsigned int name ## _num_values;	\
	type name[count] = { [0 ... ((count)-1)] = (init) };			\
	module_param_array(name, type, name ## _num_values, 0600);	\
	MODULE_PARM_DESC(name, desc " ( 1-" __MODULE_STRING(count) ")")
#else
#define	DEF_ARRAY(type,name,count,init,desc)	\
	unsigned int name ## _num_values;	\
	type name[count] = {[0 ... ((count)-1)] = init};			\
	module_param_array(name, type, &name ## _num_values, 0600);	\
	MODULE_PARM_DESC(name, desc " ( 1-" __MODULE_STRING(count) ")")
#endif
#endif	// __KERNEL__


typedef	struct xbus_ops		xbus_ops_t;

typedef enum xbus_type {
		FIRMWARE_LOOPBACK = 1,
		FIRMWARE_XPP = 2,
} xbus_type_t;

#ifdef	__KERNEL__


typedef struct packet_queue {
	char			qname[XPD_NAMELEN];
	struct list_head	head;
	unsigned int		count;
	unsigned int		worst_count;
	unsigned int		overflows;
	spinlock_t		lock;
} packet_queue_t;

struct xbus_ops {
	int (*xframe_send)(xbus_t *xbus, xframe_t *xframe);
	xframe_t *(*xframe_new)(xbus_t *xbus, gfp_t flags);
	void (*xframe_free)(xbus_t *xbus, xframe_t *p);
};

/*
 * XBUS statistics counters
 */
enum {
	XBUS_N_DESC_REQ,
	XBUS_N_DEV_DESC_FULL,
	XBUS_N_DEV_DESC_EMPTY,
	XBUS_N_TX_XFRAME_PCM,
	XBUS_N_RX_XFRAME_PCM,
	XBUS_N_TX_PACK_PCM,
	XBUS_N_RX_PACK_PCM,
	XBUS_N_TX_BYTES,
	XBUS_N_RX_BYTES,
	XBUS_N_TX_PCM_FRAG,
};

#define	XBUS_COUNTER(xbus, counter)	((xbus)->counters[XBUS_N_ ## counter])

#define	C_(x)	[ XBUS_N_ ## x ] = { #x }

/* yucky, make an instance so we can size it... */
static struct xbus_counters {
	char	*name;
} xbus_counters[] = {
	C_(DESC_REQ),
	C_(DEV_DESC_FULL),
	C_(DEV_DESC_EMPTY),
	C_(TX_XFRAME_PCM),
	C_(RX_XFRAME_PCM),
	C_(TX_PACK_PCM),
	C_(RX_PACK_PCM),
	C_(TX_BYTES),
	C_(RX_BYTES),
	C_(TX_PCM_FRAG),
};

#undef C_

#define	XBUS_COUNTER_MAX	ARRAY_SIZE(xbus_counters)

#define	CARD_DESC_MAGIC	0xca9dde5c

struct	card_desc_struct {
	struct list_head	card_list;
	u32			magic;
	xbus_t			*xbus;
	byte			rev;		/* Revision number */
	byte			type;		/* LSB: 1 - to_phone, 0 - to_line */
	byte			subtype;
	xpd_addr_t		xpd_addr;
	xpp_line_t		line_status;	/* Initial line status (offhook) */
};

#ifdef XPP_DEBUGFS
/* definition in xbus-core.c */
struct debugfs_data;
#endif

/*
 * Encapsulate all poll related data of a single xbus.
 */
struct xbus_poller {
	/*
	 * Bus scanning
	 */
	xbus_t			*xbus;
	struct workqueue_struct	*wq;
	bool			is_polling;
	atomic_t		count_poll_answers;
	struct list_head	poll_results;
	wait_queue_head_t	wait_for_polls;

	struct work_struct	xpds_init_work;

	atomic_t		count_xpds_to_initialize;
	atomic_t		count_xpds_initialized;
	wait_queue_head_t	wait_for_xpd_initialization;
};

/*
 * An xbus is a transport layer for Xorcom Protocol commands
 */
struct xbus {
	char			busname[XBUS_NAMELEN];	/* only xbus_new set this */
	char			busdesc[XBUS_DESCLEN];	/* lowlevel drivers set this */
	int			num;
	xbus_ops_t		*ops;
	struct xpd		*xpds[MAX_XPDS];
	int			max_packet_size;

	/* Device-Model */
	struct device		astribank;
#define	dev_to_xbus(dev)	container_of(dev, struct xbus, astribank)

	/* Simulator data */
	xbus_type_t		bus_type;

	spinlock_t		lock;
	atomic_t		pcm_nesting;

	bool			hardware_exists;	/* Hardware is functional */
	int			open_counter;		/* Number of open channels */
	atomic_t		packet_counter;		/* Allocated packets */
	wait_queue_head_t	packet_cache_empty;

	/* PCM metrics */
	struct timeval		last_tx_sync;
	struct timeval		last_rx_sync;
	unsigned long		max_tx_sync;
	unsigned long		min_tx_sync;
	unsigned long		max_rx_sync;
	unsigned long		min_rx_sync;

	struct xbus_poller	*poller;

	/*
	 * Sync adjustment
	 */
	int			sync_adjustment;
	int			sync_adjustment_offset;
	long			pll_updated_at;

	struct	rw_semaphore	in_use;
	int			num_xpds;
	void			*priv;			/* Pointer to transport level data structures */

#ifdef	XPP_DEBUGFS
	struct dentry		*debugfs_dir;
	struct dentry		*debugfs_file;
	struct debugfs_data	*debugfs_data;
#endif
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry	*proc_xbus_dir;
	struct proc_dir_entry	*proc_xbus_summary;
	struct proc_dir_entry	*proc_xbus_waitfor_xpds;
#ifdef	PROTOCOL_DEBUG
	struct proc_dir_entry	*proc_xbus_command;
#endif
#endif

	/* statistics */
	int		counters[XBUS_COUNTER_MAX];
};
#endif

typedef enum xpd_direction {
	TO_PSTN = 0,
	TO_PHONE = 1,
} xpd_direction_t;

#ifdef	__KERNEL__

/*
 * XPD statistics counters
 */
enum {
	XPD_N_PCM_READ,
	XPD_N_PCM_WRITE,
	XPD_N_RECV_ERRORS,
};

#define	XPD_COUNTER(xpd, counter)	((xpd)->counters[XPD_N_ ## counter])

#define	C_(x)	[ XPD_N_ ## x ] = { #x }

/* yucky, make an instance so we can size it... */
static struct xpd_counters {
	char	*name;
} xpd_counters[] = {
	C_(PCM_READ),
	C_(PCM_WRITE),
	C_(RECV_ERRORS),
};

#undef C_

#define	XPD_COUNTER_MAX	(sizeof(xpd_counters)/sizeof(xpd_counters[0]))

/*
 * An XPD is a single Xorcom Protocol Device
 */
struct xpd {
	char xpdname[XPD_NAMELEN];
	struct zt_span	span;
	struct zt_chan	*chans;
	int channels;
	xpd_type_t	type;
	const char	*type_name;
	byte		subtype;
	byte		revision;		/* Card revision */
	xpd_direction_t	direction;		/* TO_PHONE, TO_PSTN */
	xpp_line_t	no_pcm;			/* Temporary: disable PCM (for USB-1) */
	xpp_line_t	offhook;		/* Actual chip state: 0 - ONHOOK, 1 - OFHOOK */
	xpp_line_t	cid_on;
	xpp_line_t	msg_waiting;		/* Voice Mail Waiting Indication */
	xpp_line_t	digital_outputs;	/* 0 - no, 1 - yes */
	xpp_line_t	digital_inputs;		/* 0 - no, 1 - yes */
	xpp_line_t	digital_signalling;	/* PRI/BRI signalling channels */
	uint		timing_priority;	/* from 'span' directives in zapata.conf */

	/* maintained by card drivers */
	uint		pcm_len;		/* allocation length of PCM packet (dynamic) */
	xpp_line_t	wanted_pcm_mask;

	bool		ringing[CHANNELS_PERXPD];

	xbus_t *xbus;			/* The XBUS we are connected to */

	spinlock_t	lock;
	atomic_t	zt_registered;	/* Am I fully registered with zaptel */
	atomic_t	open_counter;	/* Number of open channels */

	int		flags;
	bool		blink_mode;	/* for visual identification */
#define	DEFAULT_LED_PERIOD	(1000/8)	/* in tick */

#ifdef CONFIG_PROC_FS
	struct proc_dir_entry	*proc_xpd_dir;
	struct proc_dir_entry	*proc_xpd_summary;
	struct proc_dir_entry	*proc_xpd_ztregister;
	struct proc_dir_entry	*proc_xpd_blink;
#endif

	int		counters[XPD_COUNTER_MAX];

	const xproto_table_t	*xproto;	/* Card level protocol table */
	const xops_t	*xops;			/* Card level operations */
	void		*priv;			/* Card level private data */
	bool		card_present;
	reg_cmd_t	requested_reply;
	reg_cmd_t	last_reply;

	unsigned long	last_response;	/* in jiffies */
	unsigned	xbus_idx;	/* index in xbus->xpds[] */
	xpd_addr_t	addr;
	struct list_head xpd_list;
	unsigned int	timer_count;
	volatile u_char *readchunk;	/* Double-word aligned read memory */
	/* Echo cancelation */
	u_char ec_chunk1[CHANNELS_PERXPD][ZT_CHUNKSIZE];
	u_char ec_chunk2[CHANNELS_PERXPD][ZT_CHUNKSIZE];
};

#define	for_each_line(xpd,i)	for((i) = 0; (i) < (xpd)->channels; (i)++)
#define	IS_BRI(xpd)		((xpd)->type == XPD_TYPE_BRI_NT || (xpd)->type == XPD_TYPE_BRI_TE)
#define	TICK_TOLERANCE		500 /* usec */

#ifdef	DEBUG_SYNC_PARPORT
void xbus_flip_bit(xbus_t *xbus, unsigned int bitnum0, unsigned int bitnum1);
#else
#define	xbus_flip_bit(xbus, bitnum0, bitnum1)
#endif

#endif

#endif	/* XPD_H */