summaryrefslogtreecommitdiff
path: root/drivers/dahdi/xpp/card_echo.c
blob: 200dd559c45bf0b9253cc115c472ba1a8c7b54bc (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
/*
 * Written by Oron Peled <oron@actcom.co.il>
 * Copyright (C) 2011, 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 <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include "xpd.h"
#include "xproto.h"
#include "card_echo.h"
#include "xpp_dahdi.h"
#include "dahdi_debug.h"
#include "xpd.h"
#include "xbus-core.h"

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

/* must be before dahdi_debug.h: */
static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");

/*---------------- ECHO Protocol Commands ----------------------------------*/

static bool echo_packet_is_valid(xpacket_t *pack);
static void echo_packet_dump(const char *msg, xpacket_t *pack);

DEF_RPACKET_DATA(ECHO, SET,
	byte timeslots[ECHO_TIMESLOTS];
	);

DEF_RPACKET_DATA(ECHO, SET_REPLY,
	byte status;
	byte reserved;
	);

struct ECHO_priv_data {
};

static xproto_table_t	PROTO_TABLE(ECHO);

/*---------------- ECHO: Methods -------------------------------------------*/

static xpd_t *ECHO_card_new(xbus_t *xbus, int unit, int subunit,
		const xproto_table_t *proto_table, byte subtype,
		int subunits, int subunit_ports, bool to_phone)
{
	xpd_t *xpd = NULL;
	int channels = 0;

	if (subunit_ports != 1) {
		XBUS_ERR(xbus, "Bad subunit_ports=%d\n", subunit_ports);
		return NULL;
	}
	XBUS_DBG(GENERAL, xbus, "\n");
	xpd = xpd_alloc(xbus, unit, subunit, subtype, subunits,
		sizeof(struct ECHO_priv_data), proto_table, channels);
	if (!xpd)
		return NULL;
	xpd->type_name = "ECHO";
	return xpd;
}

static int ECHO_card_init(xbus_t *xbus, xpd_t *xpd)
{
	int ret = 0;

	BUG_ON(!xpd);
	XPD_DBG(GENERAL, xpd, "\n");
	xpd->type = XPD_TYPE_ECHO;
	XPD_DBG(DEVICES, xpd, "%s\n", xpd->type_name);
	ret = CALL_EC_METHOD(ec_update, xbus, xbus);
	return ret;
}

static int ECHO_card_remove(xbus_t *xbus, xpd_t *xpd)
{
	BUG_ON(!xpd);
	XPD_DBG(GENERAL, xpd, "\n");
	return 0;
}

static int ECHO_card_tick(xbus_t *xbus, xpd_t *xpd)
{
	struct ECHO_priv_data *priv;

	BUG_ON(!xpd);
	priv = xpd->priv;
	BUG_ON(!priv);
	return 0;
}

static int ECHO_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
{
	unsigned long flags;
	struct xpd_addr addr;
	xpd_t *orig_xpd;

	/* Map UNIT + PORTNUM to XPD */
	orig_xpd = xpd;
	addr.unit = orig_xpd->addr.unit;
	addr.subunit = info->portnum;
	xpd = xpd_byaddr(xbus, addr.unit, addr.subunit);
	if (!xpd) {
		static int rate_limit;

		if ((rate_limit++ % 1003) < 5)
			notify_bad_xpd(__func__, xbus, addr,
					orig_xpd->xpdname);
		return -EPROTO;
	}
	spin_lock_irqsave(&xpd->lock, flags);
	/* Update /proc info only if reply related to last reg read request */
	if (
			REG_FIELD(&xpd->requested_reply, regnum) ==
				REG_FIELD(info, regnum) &&
			REG_FIELD(&xpd->requested_reply, do_subreg) ==
				REG_FIELD(info, do_subreg) &&
			REG_FIELD(&xpd->requested_reply, subreg) ==
				REG_FIELD(info, subreg)) {
		xpd->last_reply = *info;
	}
	spin_unlock_irqrestore(&xpd->lock, flags);
	return 0;
}

/*---------------- ECHO: HOST COMMANDS -------------------------------------*/

static /* 0x39 */ HOSTCMD(ECHO, SET)
{
	struct xbus_echo_state *es;
	byte *ts;
	xframe_t *xframe;
	xpacket_t *pack;
	int ret;
	uint16_t frm_len;
	int xpd_idx;

	BUG_ON(!xbus);
	/*
	 * Find echo canceller XPD address
	 */
	es = &xbus->echo_state;
	xpd_idx = es->xpd_idx;
	XFRAME_NEW_CMD(xframe, pack, xbus, ECHO, SET, xpd_idx);
	ts = RPACKET_FIELD(pack, ECHO, SET, timeslots);
	memcpy(ts, es->timeslots, ECHO_TIMESLOTS);
	frm_len = XFRAME_LEN(xframe);
	XBUS_DBG(GENERAL, xbus, "ECHO SET: (len = %d)\n", frm_len);
	ret = send_cmd_frame(xbus, xframe);
	return ret;
}

static int ECHO_ec_set(xpd_t *xpd, int pos, bool on)
{
	int ts_number;
	int ts_mask;
	byte *ts;

	ts = xpd->xbus->echo_state.timeslots;
	/*
	 * ts_number = PCM time slot ("channel number" in the PCM XPP packet)
	 *
	 * Bit 0 is for UNIT=0
	 * PRI: ts_number * 4 + SUBUNIT
	 * BRI: ts_number
	 * FXS/FXO(all units): UNIT * 32 + ts_number
	 *
	 * Bit 1 is for UNIT=1-3: FXS/FXO
	 *
	 */
	ts_mask = (xpd->addr.unit == 0) ? 0x1 : 0x2;	/* Which bit? */
	ts_number = CALL_PHONE_METHOD(echocancel_timeslot, xpd, pos);
	if (ts_number >= ECHO_TIMESLOTS || ts_number < 0) {
		XPD_ERR(xpd, "Bad ts_number=%d\n", ts_number);
		return -EINVAL;
	} else {
		if (on)
			ts[ts_number] |= ts_mask;
		else
			ts[ts_number] &= ~ts_mask;
	}
	LINE_DBG(GENERAL, xpd, pos, "%s = %d -- ts_number=%d ts_mask=0x%X\n",
		__func__, on, ts_number, ts_mask);
	return 0;
}

static int ECHO_ec_get(xpd_t *xpd, int pos)
{
	int ts_number;
	int ts_mask;
	int is_on;
	byte *ts;

	ts = xpd->xbus->echo_state.timeslots;
	ts_mask = (xpd->addr.unit == 0) ? 0x1 : 0x2;	/* Which bit? */
	ts_number = CALL_PHONE_METHOD(echocancel_timeslot, xpd, pos);
	if (ts_number >= ECHO_TIMESLOTS || ts_number < 0) {
		XPD_ERR(xpd, "Bad ts_number=%d\n", ts_number);
		return -EINVAL;
	} else {
		is_on = ts[ts_number] & ts_mask;
	}
#if 0
	LINE_DBG(GENERAL, xpd, pos, "ec_get=%d -- ts_number=%d ts_mask=0x%X\n",
		is_on, ts_number, ts_mask);
#endif
	return is_on;
}

static void ECHO_ec_dump(xbus_t *xbus)
{
	byte *ts;
	int i;

	ts = xbus->echo_state.timeslots;
	for (i = 0; i + 15 < ECHO_TIMESLOTS; i += 16) {
		XBUS_DBG(GENERAL, xbus,
			"EC-DUMP[%03d]: "
			"0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X "
			"0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
			i,
			ts[i+0], ts[i+1], ts[i+2], ts[i+3], ts[i+4], ts[i+5],
				ts[i+6], ts[i+7],
			ts[i+8], ts[i+9], ts[i+10], ts[i+11], ts[i+12],
				ts[i+13], ts[i+14], ts[i+15]
			);
	}
}

static int ECHO_ec_update(xbus_t *xbus)
{
	XBUS_DBG(GENERAL, xbus, "%s\n", __func__);
	//ECHO_ec_dump(xbus);
	return CALL_PROTO(ECHO, SET, xbus, NULL);
}

/*---------------- ECHO: Astribank Reply Handlers --------------------------*/
HANDLER_DEF(ECHO, SET_REPLY)
{
	byte	status;

	BUG_ON(!xpd);
	status = RPACKET_FIELD(pack, ECHO, SET_REPLY, status);
	XPD_DBG(GENERAL, xpd, "status=0x%X\n", status);
	return 0;
}

static const struct xops	echo_xops = {
	.card_new		= ECHO_card_new,
	.card_init		= ECHO_card_init,
	.card_remove		= ECHO_card_remove,
	.card_tick		= ECHO_card_tick,
	.card_register_reply	= ECHO_card_register_reply,
};

static const struct echoops	echoops = {
	.ec_set		= ECHO_ec_set,
	.ec_get		= ECHO_ec_get,
	.ec_update	= ECHO_ec_update,
	.ec_dump	= ECHO_ec_dump,
};

static xproto_table_t PROTO_TABLE(ECHO) = {
	.owner = THIS_MODULE,
	.entries = {
		/*	Table	Card	Opcode		*/
		XENTRY(ECHO,	ECHO,	SET_REPLY),
	},
	.name = "ECHO",
	.ports_per_subunit = 1,
	.type = XPD_TYPE_ECHO,
	.xops = &echo_xops,
	.echoops = &echoops,
	.packet_is_valid = echo_packet_is_valid,
	.packet_dump = echo_packet_dump,
};

static bool echo_packet_is_valid(xpacket_t *pack)
{
	const xproto_entry_t	*xe = NULL;
	// DBG(GENERAL, "\n");
	xe = xproto_card_entry(&PROTO_TABLE(ECHO), XPACKET_OP(pack));
	return xe != NULL;
}

static void echo_packet_dump(const char *msg, xpacket_t *pack)
{
	DBG(GENERAL, "%s\n", msg);
}

/*------------------------- sysfs stuff --------------------------------*/
static int echo_xpd_probe(struct device *dev)
{
	xpd_t	*ec_xpd;
	int	ret = 0;

	ec_xpd = dev_to_xpd(dev);
	/* Is it our device? */
	if (ec_xpd->type != XPD_TYPE_ECHO) {
		XPD_ERR(ec_xpd, "drop suggestion for %s (%d)\n",
			dev_name(dev), ec_xpd->type);
		return -EINVAL;
	}
	XPD_DBG(DEVICES, ec_xpd, "SYSFS\n");
	return ret;
}

static int echo_xpd_remove(struct device *dev)
{
	xpd_t	*ec_xpd;

	ec_xpd = dev_to_xpd(dev);
	XPD_DBG(DEVICES, ec_xpd, "SYSFS\n");
	return 0;
}

static struct xpd_driver	echo_driver = {
	.type		= XPD_TYPE_ECHO,
	.driver		= {
		.name = "echo",
#ifndef OLD_HOTPLUG_SUPPORT
		.owner = THIS_MODULE,
#endif
		.probe = echo_xpd_probe,
		.remove = echo_xpd_remove
	}
};

static int __init card_echo_startup(void)
{
	int ret;

	ret = xpd_driver_register(&echo_driver.driver);
	if (ret < 0)
		return ret;
	INFO("revision %s\n", XPP_VERSION);
	INFO("FEATURE: WITH Octasic echo canceller\n");
	xproto_register(&PROTO_TABLE(ECHO));
	return 0;
}

static void __exit card_echo_cleanup(void)
{
	DBG(GENERAL, "\n");
	xproto_unregister(&PROTO_TABLE(ECHO));
	xpd_driver_unregister(&echo_driver.driver);
}

MODULE_DESCRIPTION("XPP ECHO Card Driver");
MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
MODULE_LICENSE("GPL");
MODULE_VERSION(XPP_VERSION);
MODULE_ALIAS_XPD(XPD_TYPE_ECHO);

module_init(card_echo_startup);
module_exit(card_echo_cleanup);