summaryrefslogtreecommitdiff
path: root/tdm/xivo_tdm.c
blob: 067dcbf18d549dd3f88d5c357e00070dc7ad9ed5 (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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>

#include <IxOsal.h>
#include <icp.h>
#include <icp_hssdrv_common.h>

#include "xivo_tdm_api.h"


MODULE_LICENSE("GPL");


#define MIN_HSS_PORTS_WANTED		(2)
#define MIN_HSS_CHANS_WANTED		(10)

#define TDM_SAMPLE_SIZE_FOR_DAHDI	(8)

#define RX_BUF_NUM			(2048)

/* for Linux < 2.6.19; WARNING: DO NOT USE IN INTERFACES IN THIS CASE!
 * (no ifdef so that this warning can be respected when porting
 *  to modern kernel) thanks to a clash between this definition of 'bool'
 * and those of Linux
 */
typedef unsigned char bool;
enum { false = 0, true = 1 };

struct xivo_tdm_callbacks_struct {

	/* all contexts including irq */
	void (*tick)(void *data);
	void *tick_data;

	/* process context */
	void (*port0_started)(void *data);
	void *port0_started_data;
};
static struct xivo_tdm_callbacks_struct xivo_tdm_callbacks;

static void xivo_tdm_audio_work_func(void *data);
static DECLARE_WORK(xivo_tdm_audio_work, xivo_tdm_audio_work_func, NULL);

struct xivo_tdm_port {
	int port_num;
	bool allocated;

	u32 ts_allocated;
	u32 ts_started;
	unsigned int hss_chan_ids[XIVO_TDM_TS_NUM];

	spinlock_t lock;

	/* WARNING: this file is a big hack, coherency of following chunks is
	 * not guaranteed.  Blame Intel for their shitty nearly unusable code.
	 */
	u8 tx_bufs[XIVO_TDM_TS_NUM][8];
	u8 rx_bufs[XIVO_TDM_TS_NUM][8];

	unsigned tx_errs;
};

static struct xivo_tdm_port xivo_tdm_ports[MIN_HSS_PORTS_WANTED];

static DECLARE_MUTEX(xivo_tdm_mutex);
static struct workqueue_struct *xivo_tdm_wq;

/* INTERNAL FUNCS */

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! USER CONTEXT ONLY !!!!!!!!!!!!!!!! */
static IX_OSAL_MBUF *alloc_init_osal_buf(
	unsigned int data_buf_length)
{
	IX_OSAL_MBUF *osal_buf;
	char *data_buf;

	osal_buf = kmalloc(sizeof(IX_OSAL_MBUF), GFP_KERNEL);
	if (!osal_buf)
		return NULL;

	data_buf = kmalloc(data_buf_length, GFP_KERNEL);
	if (!data_buf) {
		kfree(osal_buf);
		return NULL;
	}

	IX_OSAL_MBUF_ALLOCATED_BUFF_LEN(osal_buf) = data_buf_length;

	/*Fill in the allocated size of the data buf */
	IX_OSAL_MBUF_ALLOCATED_BUFF_LEN(osal_buf) = data_buf_length;
	IX_OSAL_MBUF_MLEN(osal_buf) = data_buf_length;

	IX_OSAL_MBUF_NEXT_PKT_IN_CHAIN_PTR(osal_buf) = NULL;
	IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR(osal_buf) = NULL;

	/*Attach the data buf to the OSAL buf */
	IX_OSAL_MBUF_MDATA(osal_buf) = data_buf;

	return osal_buf;
}

static void free_osal_buf(
	IX_OSAL_MBUF *osal_buf)
{
	if (osal_buf) {
		char *data_buf = IX_OSAL_MBUF_MDATA(osal_buf);
		kfree(data_buf);

		kfree(osal_buf);
	}
}

static void dealloc_one_chan(
	struct xivo_tdm_port* xtp,
	const int cnum)
{
	icp_status_t status = icp_HssAccChannelDelete(xtp->hss_chan_ids[cnum]);
	if (status != ICP_STATUS_SUCCESS)
		printk(KERN_ERR "%s: icp_HssAccChannelDelete returned "
				"error %d\n", __func__, (int) status);
	xtp->ts_allocated &= ~(1u << cnum);
	xtp->hss_chan_ids[cnum] = 0;
}

static int add_one_chan(
	struct xivo_tdm_port* xtp,
	const int cnum)
{
        icp_status_t status;
	icp_hssacc_timeslot_map_t ts_map = {0};

	if (xtp->ts_allocated & (1u << cnum)) {
		printk(KERN_ERR "%s: EVIL BUG, DIE!\n", __func__);
		return -42;
	}

	ts_map.line0_timeslot_bit_map = (1u << cnum);

        /*Allocate the channel */
        status = icp_HssAccChannelAllocate(
			&xtp->hss_chan_ids[cnum],
			xtp->port_num,
			ts_map,
			ICP_HSSACC_CHAN_TYPE_VOICE);
        if (status != ICP_STATUS_SUCCESS) {
                printk(KERN_ERR "%s: icp_HssAccChannelAllocate returned "
				"error %d\n", __func__, (int) status);
		return -EIO;
	}

	status = icp_HssAccChannelConfigure(
			xtp->hss_chan_ids[cnum],
			/* channelDataInvert */		0,
			/* channelBitEndianness */	1, /* MSb first */
			/* channelByteSwap */		0,
			ICP_FALSE, ICP_FALSE, ICP_FALSE);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccChannelConfigure returned "
				"error %d\n", __func__, (int) status);
		dealloc_one_chan(xtp, cnum);
		return -EIO;
	}

	status = icp_HssAccChannelVoiceServiceConfigure(
			xtp->hss_chan_ids[cnum],
			ICP_HSSACC_VOICE_TX_IDLE_REPEAT_LAST_FRAME,
			0xD5,
			TDM_SAMPLE_SIZE_FOR_DAHDI);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccChannelVoiceServiceConfigure "
				"returned error %d\n", __func__, (int) status);
		dealloc_one_chan(xtp, cnum);
		return -EIO;
	}

	xtp->ts_allocated |= (1u << cnum);

	return 0;
}

static int xivo_tdm_common_rx(
	struct xivo_tdm_port* xtp,
	const unsigned int cnum,
	u8 *rx_buf)
{
	IX_OSAL_MBUF *osal_buf = NULL;
	icp_status_t status;
	int nb_read = 0;
	const unsigned int hss_chan_id = xtp->hss_chan_ids[cnum];

	while (1) {
		char *data_buf;
		status = icp_HssAccReceive(hss_chan_id, &osal_buf);
		if (status != ICP_STATUS_SUCCESS || osal_buf == NULL)
			return nb_read;
		nb_read++;
		data_buf = IX_OSAL_MBUF_MDATA(osal_buf);
		if (likely(rx_buf))
			memcpy(rx_buf, data_buf, 8 /* XXX is this right? */ );
		IX_OSAL_MBUF_MLEN(osal_buf) = IX_OSAL_MBUF_ALLOCATED_BUFF_LEN(osal_buf);
		status = icp_HssAccRxFreeReplenish(ICP_HSSACC_CHAN_TYPE_VOICE, osal_buf);
		if (status != ICP_STATUS_SUCCESS) {
			if (printk_ratelimit())
				printk(KERN_ERR "%s: icp_HssAccRxFreeReplenish "
						"err %d cnum %u\n",
				       __func__, (int) status, cnum);
			free_osal_buf(osal_buf);
		}
		osal_buf = NULL;
	}
}

static void xivo_tdm_common_tx(
	struct xivo_tdm_port* xtp,
	const unsigned int cnum,
	const u8 *tx_buf)
{
	IX_OSAL_MBUF *osal_buf = NULL;
	icp_status_t status;
	const unsigned int hss_chan_id = xtp->hss_chan_ids[cnum];

	status = icp_HssAccTxDoneRetrieve(hss_chan_id, &osal_buf);
	if (status != ICP_STATUS_SUCCESS) {
		if (status == ICP_STATUS_UNDERFLOW) {
			osal_buf = alloc_init_osal_buf(TDM_SAMPLE_SIZE_FOR_DAHDI);
			if (!osal_buf && printk_ratelimit())
				printk(KERN_ERR
				       "%s: osal_buf alloc failed - cnum %u\n",
				       __func__, cnum);
		} else {
			osal_buf = NULL;
			if (printk_ratelimit())
				printk(KERN_ERR "%s: icp_HssAccTxDoneRetrieve "
						"err %d cnum %u\n",
				       __func__, (int) status, cnum);
		}
	}

	if (!osal_buf)
		return;

	if (likely(tx_buf))
		memcpy(IX_OSAL_MBUF_MDATA(osal_buf), tx_buf, 8);
	IX_OSAL_MBUF_MLEN(osal_buf) = 8;
	IX_OSAL_MBUF_PKT_LEN(osal_buf) = 8;

	status = icp_HssAccTransmit(hss_chan_id, osal_buf);
	if (status != ICP_STATUS_SUCCESS) {
		xtp->tx_errs++;
		if (printk_ratelimit())
			printk("%s%s: icp_HssAccTransmit err %d cnum %u "
			       "tx_errs %u\n",
			       status == ICP_STATUS_OVERFLOW ?
					KERN_DEBUG :
					KERN_ERR,
			       __func__, (int) status, cnum, xtp->tx_errs);
		free_osal_buf(osal_buf);
	}
}

static int start_one_chan(
	struct xivo_tdm_port* xtp,
	const int cnum)
{
	const unsigned int hss_chan_id = xtp->hss_chan_ids[cnum];
	unsigned long flags;
	icp_status_t status;

	status = icp_HssAccChannelUp(hss_chan_id);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccChannelUp returned error %d \n",
				__func__, status);
		return -EIO;
	}

	spin_lock_irqsave(&xtp->lock, flags);
	xtp->ts_started |= (1u << cnum);
	spin_unlock_irqrestore(&xtp->lock, flags);

	return 0;
}

static int stop_one_chan(
	struct xivo_tdm_port* xtp,
	const int cnum)
{
	icp_status_t status;
	unsigned int hss_chan_id = xtp->hss_chan_ids[cnum];
	unsigned long flags;

	spin_lock_irqsave(&xtp->lock, flags);
	xtp->ts_started &= ~(1u << cnum);
	spin_unlock_irqrestore(&xtp->lock, flags);

	flush_workqueue(xivo_tdm_wq);

	status = icp_HssAccChannelDown(hss_chan_id);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccChannelDown returned %d\n",
				__func__, (int) status);
		return -EIO;
	}

	return 0;
}

static void xivo_hss_port_error_cb(
	icp_user_context_t user_context,
	icp_hssacc_port_error_t error_type)
{
	struct xivo_tdm_port* xtp = (struct xivo_tdm_port*) user_context;
	unsigned port_num = xtp->port_num;

	switch (error_type) {
	case ICP_HSSACC_PORT_ERROR_TX_LOS:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Transmit loss of sync\n",
				__func__, port_num);
		break;
	case ICP_HSSACC_PORT_ERROR_RX_LOS:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Receive loss of sync\n",
				__func__, port_num);
		break;
	case ICP_HSSACC_PORT_ERROR_TX_UNDERFLOW:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Transmit underflow\n",
				__func__, port_num);
		break;
	case ICP_HSSACC_PORT_ERROR_RX_OVERFLOW:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Receive overflow\n",
				__func__, port_num);
		break;
	case ICP_HSSACC_PORT_ERROR_TX_PARITY:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Tx Parity Error\n",
				__func__, port_num);
		break;
	case ICP_HSSACC_PORT_ERROR_RX_PARITY:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Rx Parity Error\n",
				__func__, port_num);
		break;
	default:
		printk(KERN_ERR "%s - Port error for port %u :"
				" Unidentified error %u\n",
				__func__, port_num, (unsigned) error_type);
		break;
	}
}

static void xivo_hss_error_cb(
	icp_user_context_t user_context,
	icp_hssacc_error_t error_type)
{
	(void) user_context;

	switch (error_type) {
	case ICP_HSSACC_ERROR_RX_OVERFLOW:
		printk(KERN_ERR "%s - Error: Receive Queue Overflow "
				"reported\n",
				__func__);
		break;
	case ICP_HSSACC_ERROR_RX_FREE_UNDERFLOW:
		printk(KERN_ERR "%s - Error: Receive Free Queue Underflow "
				" reported\n",
				__func__);
		break;
	case ICP_HSSACC_ERROR_MESSAGE_FIFO_OVERFLOW:
		printk(KERN_ERR "%s - Error: Messaging FIFO Overflow "
				"reported\n",
				__func__);
		break;
	default:
		printk(KERN_ERR "%s - Unidentified Error %u\n",
				__func__, (unsigned) error_type);
		break;
	}
}

static int xivo_tdm_deferred_receive(
	struct xivo_tdm_port* xtp,
	const unsigned int cnum)
{
	u8 *rx_buf = xtp->rx_bufs[cnum];
	return xivo_tdm_common_rx(xtp, cnum, rx_buf);
}

static void xivo_tdm_deferred_transmit(
	struct xivo_tdm_port* xtp,
	const unsigned int cnum)
{
	const u8 *tx_buf = xtp->tx_bufs[cnum];
	xivo_tdm_common_tx(xtp, cnum, tx_buf);
}

static void xivo_tdm_work_port(struct xivo_tdm_port *xtp)
{
	unsigned long flags;
	u32 ts_started;
	u32 scan;
	int cnum;

	spin_lock_irqsave(&xtp->lock, flags);
	ts_started = xtp->ts_started;
	spin_unlock_irqrestore(&xtp->lock, flags);

	for (cnum = 0, scan = 1; scan; scan <<= 1, cnum++) {
		if (ts_started & scan) {
			/* XXX use ret value: */
			(void) xivo_tdm_deferred_receive(xtp, cnum);
			xivo_tdm_deferred_transmit(xtp, cnum);
		}
	}
}

static void xivo_tdm_audio_work_func(void *data)
{
	int i;

	(void) data;

	for (i = 0; i < (int)ARRAY_SIZE(xivo_tdm_ports); i++) {
		xivo_tdm_work_port(&xivo_tdm_ports[i]);
	}
}


/* EXPORTED FUNCS */

struct xivo_tdm_port *xivo_tdm_get_port(int tdm_port_num)
{
	struct xivo_tdm_port *xtp;

	if (tdm_port_num < 0 || tdm_port_num >= MIN_HSS_PORTS_WANTED) {
		printk(KERN_ERR "%s: attempt to allocate an out of range TDM "
				"port %d\n", __func__, tdm_port_num);
		return NULL;
	}

	down(&xivo_tdm_mutex);
	if (xivo_tdm_ports[tdm_port_num].allocated) {
		xtp = NULL;
		printk(KERN_ERR "%s: attempt to allocate TDM port %d, which "
				"is already allocated\n",
				__func__, tdm_port_num);
	} else {
		xivo_tdm_ports[tdm_port_num].allocated = true;
		xtp = &xivo_tdm_ports[tdm_port_num];
	}
	up(&xivo_tdm_mutex);

	return xtp;
}
EXPORT_SYMBOL(xivo_tdm_get_port);

void xivo_tdm_put_port(struct xivo_tdm_port *xtp)
{
	unsigned long xtp_addr = (unsigned long)xtp;

	unsigned idx = (xtp_addr - (unsigned long)xivo_tdm_ports) / sizeof(*xtp);

	if (xtp_addr == 0
	    || xtp_addr < (unsigned long)xivo_tdm_ports
	    || xtp_addr > (unsigned long)(xivo_tdm_ports + MIN_HSS_PORTS_WANTED - 1)
	    || xtp_addr != (unsigned long)(xivo_tdm_ports + idx)) {
		printk(KERN_ERR "%s: attempt to free an invalid struct "
				"xivo_tdm_port pointer: %p\n",
				__func__, (void *) xtp);
		return;
	}

	down(&xivo_tdm_mutex);
	if (!xtp->allocated) {
		printk(KERN_ERR "%s: attempt to free TDM port %u, which is not "
				"allocated\n",
				__func__, idx);
	} else {
		xivo_tdm_stop_chans(xtp);
		xtp->allocated = false;
	}
	up(&xivo_tdm_mutex);
}
EXPORT_SYMBOL(xivo_tdm_put_port);

int xivo_tdm_config_port(
	struct xivo_tdm_port* xtp,
	unsigned int port_config)
{
	icp_status_t status;
        icp_hssacc_port_config_params_t hss_port_config;

	if (port_config >= ICP_HSSDRV_PORT_CONFIG_DELIMITER) {
		printk(KERN_ERR "%s: invalid port config %u\n",
				__func__, port_config);
		return -EINVAL;
	}

	port_config_create(port_config,
			ICP_HSSDRV_NO_LOOPBACK,
			&hss_port_config);

	status = icp_HssAccPortConfig(xtp->port_num, &hss_port_config);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccPortConfig returned error %d\n",
				__func__, (int) status);
		return -EIO;
	}

	status = icp_HssAccPortErrorCallbackRegister(	/* XXX to unreg? */
			xtp->port_num,
			ICP_HSSACC_CHAN_TYPE_VOICE,
			(icp_user_context_t) xtp,
			xivo_hss_port_error_cb);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccPortErrorCallbackRegister "
				"returned error %d\n",
				__func__, (int) status);
		return -EIO;
	}

	status = icp_HssAccPortUp(xtp->port_num);
        if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccPortUp returned error %d\n",
				__func__, (int) status);
		return -EIO;
	}

	return 0;
}
EXPORT_SYMBOL(xivo_tdm_config_port);

void xivo_tdm_register_port0_started(
	struct xivo_tdm_port* xtp,
	void (*port0_started)(void *),
	void *port0_started_data)
{
	unsigned long flags;

	(void) xtp;

	spin_lock_irqsave(&xivo_tdm_ports[0].lock, flags);
	xivo_tdm_callbacks.port0_started = port0_started;
	xivo_tdm_callbacks.port0_started_data = port0_started_data;
	spin_unlock_irqrestore(&xivo_tdm_ports[0].lock, flags);
}
EXPORT_SYMBOL(xivo_tdm_register_port0_started);

int xivo_tdm_start_chans(
	struct xivo_tdm_port* xtp,
	const u32 chans,
	struct xivo_tdm_tick_cb_struct *tick_cb)
{
	unsigned long flags;
	u32 scan;
	int cnum;

	for (cnum = 0, scan = 1; scan; scan <<= 1, cnum++) {
		if (scan & chans) {
			printk(KERN_INFO "%s: adding chan %d\n",
					__func__, cnum);
			/* XXX retval */ (void) add_one_chan(xtp, cnum);
		}
	}

	if (tick_cb) {
		spin_lock_irqsave(&xtp->lock, flags);
		xivo_tdm_callbacks.tick = tick_cb->tick;
		xivo_tdm_callbacks.tick_data = tick_cb->tick_data;
		spin_unlock_irqrestore(&xtp->lock, flags);
	}

	for (cnum = 0, scan = 1; scan; scan <<= 1, cnum++) {
		if (scan & chans) {
			printk(KERN_INFO "%s: starting chan %d\n",
					__func__, cnum);
			/* XXX retval */ (void) start_one_chan(xtp, cnum);
		}
	}

	printk(KERN_INFO "%s: DONE\n", __func__);

	if (xivo_tdm_callbacks.port0_started)
		xivo_tdm_callbacks.port0_started(
				xivo_tdm_callbacks.port0_started_data);

	return 0;
}
EXPORT_SYMBOL(xivo_tdm_start_chans);

void xivo_tdm_stop_chans(
	struct xivo_tdm_port* xtp)
{
	u32 scan;
	int cnum;
	for (cnum = 0, scan = 1; scan; scan <<= 1, cnum++) {
		if (scan & xtp->ts_started) {
			printk(KERN_INFO "%s: stopping chan %d\n",
					__func__, cnum);
			(void) stop_one_chan(xtp, cnum);
		}
	}
	for (cnum = 0, scan = 1; scan; scan <<= 1, cnum++) {
		if (scan & xtp->ts_allocated) {
			printk(KERN_INFO "%s: removing chan %d\n",
					__func__, cnum);
			(void) dealloc_one_chan(xtp, cnum);
		}
	}
}
EXPORT_SYMBOL(xivo_tdm_stop_chans);

void xivo_tdm_receive(
	struct xivo_tdm_port* xtp,
	const unsigned int cnum,
	u8 samples[8])
{
	memcpy(samples, xtp->rx_bufs[cnum], 8);
}
EXPORT_SYMBOL(xivo_tdm_receive);

void xivo_tdm_transmit(
	struct xivo_tdm_port* xtp,
	const unsigned int cnum,
	const u8 samples[8])
{
	memcpy(xtp->tx_bufs[cnum], samples, 8);
}
EXPORT_SYMBOL(xivo_tdm_transmit);

/* hardirq: yes -- softirq: yes -- user: yes */
void xivo_tdm_tick(
	struct xivo_tdm_port* xtp)
{
	if (xivo_tdm_callbacks.tick)
		xivo_tdm_callbacks.tick(xivo_tdm_callbacks.tick_data);

	queue_work(xivo_tdm_wq, &xivo_tdm_audio_work);
}
EXPORT_SYMBOL(xivo_tdm_tick);


/* INIT / CLEANUP */

static void xivo_internal_cleanup(void)
{
	if (xivo_tdm_wq) {
		/* XXX we should prevent queueing here */
		destroy_workqueue(xivo_tdm_wq);
		xivo_tdm_wq = NULL;
	}
}

static int __init xivo_internal_init(void)
{
	int i;

	xivo_tdm_wq = create_singlethread_workqueue("xivo_tdm");
	if (!xivo_tdm_wq) {
		printk(KERN_ERR "%s: create_singlethread_worqueue "
				"returned NULL\n", __func__);
		return -ENOMEM;
	}

	for (i = 0; i < (int)ARRAY_SIZE(xivo_tdm_ports); i++) {
		xivo_tdm_ports[i].port_num = i;
		xivo_tdm_ports[i].allocated = false;

		xivo_tdm_ports[i].ts_allocated = 0;
		xivo_tdm_ports[i].ts_started = 0;
		memset(xivo_tdm_ports[i].hss_chan_ids, 0,
		       sizeof xivo_tdm_ports[i].hss_chan_ids);

		spin_lock_init(&xivo_tdm_ports[i].lock);

		memset(xivo_tdm_ports[i].tx_bufs, 0xD5,
		       sizeof xivo_tdm_ports[i].tx_bufs);
		memset(xivo_tdm_ports[i].rx_bufs, 0xD5,
		       sizeof xivo_tdm_ports[i].rx_bufs);

		xivo_tdm_ports[i].tx_errs = 0;
	}

	return 0;
}

static int __init xivo_icp_Hss_init(void)
{
	icp_status_t status;
	int hss_port_number;
	int hss_chan_number;
	int i;

	hss_port_number = icp_HssAccNumSupportedPortsGet();
	if (hss_port_number < MIN_HSS_PORTS_WANTED) {
		printk(KERN_ERR "%s: wants %d HSS (TDM) ports but the system "
			"has only %d\n",
			__func__, MIN_HSS_PORTS_WANTED, hss_port_number);
		return -ENXIO;
	}
	hss_chan_number = icp_HssAccNumSupportedChannelsGet();
	if (hss_chan_number < MIN_HSS_CHANS_WANTED) {
		printk(KERN_ERR "%s: wants %d HSS (TDM) chans but the system "
			"can only use %d\n",
			__func__, MIN_HSS_CHANS_WANTED, hss_chan_number);
		return -ENXIO;
	}

	status = icp_HssAccVoiceInit(TDM_SAMPLE_SIZE_FOR_DAHDI,
					/* intGenerationEnable */ ICP_TRUE);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccVoiceInit() returned error %d\n",
				__func__, (int) status);
		return -EIO;
	}

	status = icp_HssAccErrorCallbackRegister(ICP_HSSACC_CHAN_TYPE_VOICE,
						 NULL, xivo_hss_error_cb);
	if (status != ICP_STATUS_SUCCESS) {
		printk(KERN_ERR "%s: icp_HssAccErrorCallbackRegister() "
				"returned error %d\n",
				__func__, (int) status);
		return -EIO;
	}

	/* GIVE ME A spare_wheel_osal_buf AND A PONEY (and LSD) */
	for (i = 0; i < RX_BUF_NUM; i++) {
		IX_OSAL_MBUF* osal_buf =
				alloc_init_osal_buf(TDM_SAMPLE_SIZE_FOR_DAHDI);
		if (!osal_buf) {
			printk(KERN_ERR "BUGBUG WARNING MEMORY LEAK YOU'LL DIE "
					"(triggered by memory alloc failure in "
					"alloc_init_osal_buf)\n");
			return -ENOMEM;
		}

		status = icp_HssAccRxFreeReplenish(ICP_HSSACC_CHAN_TYPE_VOICE,
						   osal_buf);
		if (status != ICP_STATUS_SUCCESS) {
			printk(KERN_WARNING "the hss soft is not hungry "
					    "anymore, giving up feeding\n");
			break;
		}
	}

	return 0;
}

static int __init xivo_tdm_init(void)
{
	int rc;

	if ((rc = xivo_internal_init()) < 0)
		return rc;

	if ((rc = xivo_icp_Hss_init()) < 0) {
		xivo_internal_cleanup();
		return rc;
	}

	printk(KERN_NOTICE "%s DONE\n", __func__);
	return 0;
}

static void __exit xivo_tdm_exit(void)
{
	printk(KERN_ERR "BUGBUG WARNING MEMORY LEAK YOU'LL DIE "
			"(triggered by module unload)\n");
	/* XXX */
	xivo_internal_cleanup();
}

module_init(xivo_tdm_init);
module_exit(xivo_tdm_exit);