summaryrefslogtreecommitdiff
path: root/kernel/wctdm24xxp/GpakApi.c
blob: de50563bcdae4e368369cf83220dcac0650fc8c4 (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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
/*
 * Copyright (c) 2005, Adaptive Digital Technologies, Inc.
 *
 * File Name: GpakApi.c
 *
 * Description:
 *   This file contains user API functions to communicate with DSPs executing
 *   G.PAK software. The file is integrated into the host processor connected
 *   to C55X G.PAK DSPs via a Host Port Interface.
 *
 * Version: 1.0
 *
 * Revision History:
 *   06/15/05 - Initial release.
 *   11/15/2006  - 24 TDM-TDM Channels EC release
 *
 * This program has been released under the terms of the GPL version 2 by
 * permission of Adaptive Digital Technologies, Inc.
 *
 */

/*
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2 as published by the
 * Free Software Foundation. See the LICENSE file included with
 * this program for more details.
 */

#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#include <linux/semaphore.h>
#else
#include <asm/semaphore.h>
#endif

#include <linux/workqueue.h>

#include "zaptel.h"

#include "GpakHpi.h"
#include "GpakCust.h"
#include "GpakApi.h"
#include "gpakenum.h"

/* DSP to Host interface block offsets. */
#define REPLY_MSG_PNTR_OFFSET 0     /* I/F blk offset to Reply Msg Pointer */
#define CMD_MSG_PNTR_OFFSET 2       /* I/F blk offset to Command Msg Pointer */
#define EVENT_MSG_PNTR_OFFSET 4     /* I/F blk offset to Event Msg Pointer */
#define PKT_BUFR_MEM_OFFSET 6       /* I/F blk offset to Packet Buffer memory */
#define DSP_STATUS_OFFSET 8         /* I/F blk offset to DSP Status */
#define VERSION_ID_OFFSET 9         /* I/F blk offset to G.PAK Version Id */
#define MAX_CMD_MSG_LEN_OFFSET 10   /* I/F blk offset to Max Cmd Msg Length */
#define CMD_MSG_LEN_OFFSET 11       /* I/F blk offset to Command Msg Length */
#define REPLY_MSG_LEN_OFFSET 12     /* I/F blk offset to Reply Msg Length */
#define NUM_CHANNELS_OFFSET 13      /* I/F blk offset to Num Built Channels */
#define NUM_PKT_CHANNELS_OFFSET 14  /* I/F blk offset to Num Pkt Channels */
#define NUM_CONFERENCES_OFFSET 15   /* I/F blk offset to Num Conferences */
//#define CPU_USAGE_OFFSET_1MS 16     /* I/F blk offset to CPU Usage statistics */
#define CPU_USAGE_OFFSET 18         /* I/F blk offset to CPU Usage statistics */
//#define CPU_USAGE_OFFSET_10MS 20    /* I/F blk offset to CPU Usage statistics */
#define FRAMING_STATS_OFFSET 22     /* I/F blk offset to Framing statistics */

//#define GPAK_RELEASE_Rate rate10ms
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
// Macro to reconstruct a 32-bit value from two 16-bit values.
// Parameter p32: 32-bit-wide destination
// Parameter p16: 16-bit-wide source array of length 2 words
#define RECONSTRUCT_LONGWORD(p32, p16) p32 = (DSP_ADDRESS)p16[0]<<16; \
								       p32 |= (unsigned long)p16[1]
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

/* DSP Status value definitions. */
#define DSP_INIT_STATUS 0x5555		/* DSP Initialized status value */
#define HOST_INIT_STATUS 0xAAAA		/* Host Initialized status value */

/* Circular packet buffer information structure offsets. */
#define CB_BUFR_BASE 0              /* pointer to base of circular buffer */
#define CB_BUFR_SIZE 2              /* size of buffer (words) */
#define CB_BUFR_PUT_INDEX 3         /* offset in buffer for next write */
#define CB_BUFR_TAKE_INDEX 4        /* offset in buffer for next read */
#define CIRC_BUFFER_INFO_STRUCT_SIZE 6 

/* Miscellaneous definitions. */
#define MSG_BUFFER_SIZE 100 /* size (words) of Host msg buffer */
#define WORD_BUFFER_SIZE 84  /* size of DSP Word buffer (words) */

#ifdef __TMS320C55XX__ // debug sections if not on host
#pragma DATA_SECTION(pDspIfBlk,"GPAKAPIDEBUG_SECT")
#pragma DATA_SECTION(MaxCmdMsgLen,"GPAKAPIDEBUG_SECT")
#pragma DATA_SECTION(MaxChannels,"GPAKAPIDEBUG_SECT")
#pragma DATA_SECTION(DlByteBufr,"GPAKAPIDEBUG_SECT")
#pragma DATA_SECTION(DlWordBufr,"GPAKAPIDEBUG_SECT")
#pragma DATA_SECTION(pEventFifoAddress,"GPAKAPIDEBUG_SECT")
#endif

/* Host variables related to Host to DSP interface. */
static DSP_ADDRESS pDspIfBlk[MAX_DSP_CORES];    /* DSP address of I/F block */
static DSP_WORD MaxCmdMsgLen[MAX_DSP_CORES]; /* max Cmd msg length (octets) */
static unsigned short int MaxChannels[MAX_DSP_CORES];    /* max num channels */

//static unsigned short int MaxPktChannels[MAX_DSP_CORES]; /* max num pkt channels */
//static unsigned short int MaxConfs[MAX_DSP_CORES];    /* max num conferences */
//static DSP_ADDRESS pPktInBufr[MAX_DSP_CORES][MAX_PKT_CHANNELS];  /* Pkt In buffer */
//static DSP_ADDRESS pPktOutBufr[MAX_DSP_CORES][MAX_PKT_CHANNELS]; /* Pkt Out buffer */
static DSP_ADDRESS pEventFifoAddress[MAX_DSP_CORES]; /* event fifo */

static unsigned char DlByteBufr[DOWNLOAD_BLOCK_SIZE * 2]; /* Dowload byte buf */
static DSP_WORD DlWordBufr[DOWNLOAD_BLOCK_SIZE];      /* Dowload word buffer */


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * CheckDspReset - Check if the DSP was reset.
 *
 * FUNCTION
 *  This function determines if the DSP was reset and is ready. If reset
 *  occurred, it reads interface parameters and calculates DSP addresses.
 *
 * RETURNS
 *  -1 = DSP is not ready.
 *   0 = Reset did not occur.
 *   1 = Reset occurred.
 *
 */
static int CheckDspReset(
    int DspId               /* DSP Identifier (0 to MaxDSPCores-1) */
    )
{
    DSP_ADDRESS IfBlockPntr; /* Interface Block pointer */
    DSP_WORD DspStatus;      /* DSP Status */
    DSP_WORD DspChannels;    /* number of DSP channels */
    DSP_WORD  Temp[2];
#if 0
    DSP_WORD DspConfs;       /* number of DSP conferences */
    DSP_ADDRESS PktBufrMem;  /* address of Packet Buffer */
    unsigned short int i;    /* loop index / counter */
#endif

    /* Read the pointer to the Interface Block. */
    gpakReadDspMemory(DspId, DSP_IFBLK_ADDRESS, 2, Temp);
    RECONSTRUCT_LONGWORD(IfBlockPntr, Temp);

    /* If the pointer is zero, return with an indication the DSP is not
       ready. */
    if (IfBlockPntr == 0)
        return (-1);

    /* Read the DSP's Status. */
    gpakReadDspMemory(DspId, IfBlockPntr + DSP_STATUS_OFFSET, 1, &DspStatus);

    /* If status indicates the DSP was reset, read the DSP's interface
       parameters and calculate DSP addresses. */
    if (DspStatus == DSP_INIT_STATUS ||
        ((DspStatus == HOST_INIT_STATUS) && (pDspIfBlk[DspId] == 0))) 
    {
        /* Save the address of the DSP's Interface Block. */
        pDspIfBlk[DspId] = IfBlockPntr;

        /* Read the DSP's interface parameters. */
        gpakReadDspMemory(DspId, IfBlockPntr + MAX_CMD_MSG_LEN_OFFSET, 1,
                          &(MaxCmdMsgLen[DspId]));

        /* read the number of configured DSP channels */
        gpakReadDspMemory(DspId, IfBlockPntr + NUM_CHANNELS_OFFSET, 1,
                          &DspChannels);
        if (DspChannels > MAX_CHANNELS)
            MaxChannels[DspId] = MAX_CHANNELS;
        else
            MaxChannels[DspId] = (unsigned short int) DspChannels;
#if 0
        /* read the number of configured DSP conferences */
        gpakReadDspMemory(DspId, IfBlockPntr + NUM_CONFERENCES_OFFSET, 1,
                          &DspConfs);
        if (DspConfs > MAX_CONFS)
            MaxConfs[DspId] = MAX_CONFS;
        else
            MaxConfs[DspId] = (unsigned short int) DspConfs;


        /* read the number of configured DSP packet channels */
        gpakReadDspMemory(DspId, IfBlockPntr + NUM_PKT_CHANNELS_OFFSET, 1,
                          &DspChannels);
        if (DspChannels > MAX_PKT_CHANNELS)
            MaxPktChannels[DspId] = MAX_PKT_CHANNELS;
        else
            MaxPktChannels[DspId] = (unsigned short int) DspChannels;


        /* read the pointer to the circular buffer infor struct table */
        gpakReadDspMemory(DspId, IfBlockPntr + PKT_BUFR_MEM_OFFSET, 2, Temp);
        RECONSTRUCT_LONGWORD(PktBufrMem, Temp);


        /* Determine the addresses of each channel's Packet buffers. */
        for (i = 0; i < MaxPktChannels[DspId]; i++)
        {
            pPktInBufr[DspId][i] = PktBufrMem;
            pPktOutBufr[DspId][i] = PktBufrMem + CIRC_BUFFER_INFO_STRUCT_SIZE;
            PktBufrMem += (CIRC_BUFFER_INFO_STRUCT_SIZE*2);
        }
#endif

        /* read the pointer to the event fifo info struct */
        gpakReadDspMemory(DspId, IfBlockPntr + EVENT_MSG_PNTR_OFFSET, 2, Temp);
        RECONSTRUCT_LONGWORD(pEventFifoAddress[DspId], Temp);

        /* Set the DSP Status to indicate the host recognized the reset. */
        DspStatus = HOST_INIT_STATUS;
        gpakWriteDspMemory(DspId, IfBlockPntr + DSP_STATUS_OFFSET, 1,
                           &DspStatus);

        /* Return with an indication that a reset occurred. */
        return (1);
    }

    /* If status doesn't indicate the host recognized a reset, return with an
       indication the DSP is not ready. */
    if ((DspStatus != HOST_INIT_STATUS) || (pDspIfBlk[DspId] == 0))
        return (-1);

    /* Return with an indication that a reset did not occur. */
    return (0);
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * WriteDspCmdMessage - Write a Host Command/Request message to DSP.
 *
 * FUNCTION
 *  This function writes a Host Command/Request message into DSP memory and
 *  informs the DSP of the presence of the message.
 *
 * RETURNS
 *  -1 = Unable to write message (msg len or DSP Id invalid or DSP not ready)
 *   0 = Temporarily unable to write message (previous Cmd Msg busy)
 *   1 = Message written successfully
 *
 */
static int WriteDspCmdMessage(
    int DspId,                      /* DSP Identifier (0 to MaxDSPCores-1) */
    DSP_WORD *pMessage,          /* pointer to Command message */
    DSP_WORD MsgLength              /* length of message (octets) */
    )
{
    DSP_WORD CmdMsgLength;      /* current Cmd message length */
    DSP_WORD Temp[2];
    DSP_ADDRESS BufferPointer;     /* message buffer pointer */

    /* Check if the DSP was reset and is ready. */
    if (CheckDspReset(DspId) == -1)
        return (-1);

    /* Make sure the message length is valid. */
    if ((MsgLength < 1) || (MsgLength > MaxCmdMsgLen[DspId]))
        return (-1);

    /* Make sure a previous Command message is not in use by the DSP. */
    gpakReadDspMemory(DspId, pDspIfBlk[DspId] + CMD_MSG_LEN_OFFSET, 1,
                      &CmdMsgLength);
    if (CmdMsgLength != 0)
        return (0);

    /* Purge any previous Reply message that wasn't read. */
    gpakWriteDspMemory(DspId, pDspIfBlk[DspId] + REPLY_MSG_LEN_OFFSET, 1,
                       &CmdMsgLength);

    /* Copy the Command message into DSP memory. */
    gpakReadDspMemory(DspId, pDspIfBlk[DspId] + CMD_MSG_PNTR_OFFSET, 2, Temp);
    RECONSTRUCT_LONGWORD(BufferPointer, Temp);
    gpakWriteDspMemory(DspId, BufferPointer, (MsgLength + 1) / 2, pMessage);

    /* Store the message length in DSP's Command message length (flags DSP that
       a Command message is ready). */
    CmdMsgLength = MsgLength;
    gpakWriteDspMemory(DspId, pDspIfBlk[DspId] + CMD_MSG_LEN_OFFSET, 1,
                       &CmdMsgLength);

    /* Return with an indication the message was written. */
    return (1);
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * ReadDspReplyMessage - Read a DSP Reply message from DSP.
 *
 * FUNCTION
 *  This function reads a DSP Reply message from DSP memory.
 *
 * RETURNS
 *  -1 = Unable to write message (msg len or DSP Id invalid or DSP not ready)
 *   0 = No message available (DSP Reply message empty)
 *   1 = Message read successfully (message and length stored in variables)
 *
 */
static int ReadDspReplyMessage(
    int DspId,                      /* DSP Identifier (0 to MaxDSPCores-1) */
    DSP_WORD *pMessage,             /* pointer to Reply message buffer */
    DSP_WORD *pMsgLength            /* pointer to msg length var (octets) */
    )
{
    DSP_WORD MsgLength;         /* message length */
    DSP_ADDRESS BufferPointer;  /* message buffer pointer */
    DSP_WORD Temp[2];

    /* Check if the DSP was reset and is ready. */
    if (CheckDspReset(DspId) == -1)
        return (-1);

    /* Check if a Reply message is ready. */
    gpakReadDspMemory(DspId, pDspIfBlk[DspId] + REPLY_MSG_LEN_OFFSET, 1,
                      &MsgLength);
    if (MsgLength == 0)
        return (0);

    /* Make sure the message length is valid. */
    if (MsgLength > *pMsgLength)
        return (-1);

    /* Copy the Reply message from DSP memory. */
    gpakReadDspMemory(DspId, pDspIfBlk[DspId] + REPLY_MSG_PNTR_OFFSET, 2, Temp);
    RECONSTRUCT_LONGWORD(BufferPointer, Temp);
    gpakReadDspMemory(DspId, BufferPointer, (MsgLength + 1) / 2, pMessage);

    /* Store the message length in the message length variable. */
    *pMsgLength = MsgLength;

    /* Indicate a Reply message is not ready. */
    MsgLength = 0;
    gpakWriteDspMemory(DspId, pDspIfBlk[DspId] + REPLY_MSG_LEN_OFFSET, 1,
                       &MsgLength);

    /* Return with an indication the message was read. */
    return (1);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * ReadCircBuffer - Read from a DSP circular buffer.
 *
 * FUNCTION
 *  This function reads a block of words from a DSP circular buffer. The Take
 *  address is incremented by the number of words read adjusting for buffer
 *  wrap.
 *
 * RETURNS
 *  nothing
 *
 */
static void ReadCircBuffer(
    int DspId,                  /* DSP Identifier (0 to MaxDSPCores-1) */
    DSP_ADDRESS BufrBaseAddress,   /* address of base of circular buffer */
    DSP_ADDRESS BufrLastAddress,   /* address of last word in buffer */
    DSP_ADDRESS *TakeAddress,      /* pointer to address in buffer for read */
    DSP_WORD *pWordBuffer,      /* pointer to buffer for words read */
    DSP_WORD NumWords           /* number of words to read */
    )
{
    DSP_WORD WordsTillEnd;      /* number of words until end of buffer */

    /* Determine the number of words from the start address until the end of the
       buffer. */
    WordsTillEnd = BufrLastAddress - *TakeAddress + 1;

    /* If a buffer wrap will occur, read the first part at the end of the
       buffer followed by the second part at the beginning of the buffer. */
    if (NumWords > WordsTillEnd)
    {
        gpakReadDspMemory(DspId, *TakeAddress, WordsTillEnd, pWordBuffer);
        gpakReadDspMemory(DspId, BufrBaseAddress, NumWords - WordsTillEnd,
                           &(pWordBuffer[WordsTillEnd]));
        *TakeAddress = BufrBaseAddress + NumWords - WordsTillEnd;
    }

    /* If a buffer wrap will not occur, read all words starting at the current
       take address in the buffer. */
    else
    {
        gpakReadDspMemory(DspId, *TakeAddress, NumWords, pWordBuffer);
        if (NumWords == WordsTillEnd)
            *TakeAddress = BufrBaseAddress;
        else
            *TakeAddress = *TakeAddress + NumWords;
    }
    return;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * VerifyReply - Verify the reply message is correct for the command sent.
 *
 * FUNCTION
 *  This function verifies correct reply message content for the command that
 *  was just sent.
 *
 * RETURNS
 *  0 = Incorrect
 *  1 = Correct
 *
 */
static int VerifyReply(
    DSP_WORD *pMsgBufr,     /* pointer to Reply message buffer */
    int CheckType,          /* reply check type */
    DSP_WORD CheckValue     /* reply check value */
    )
{

    /* Verify Channel or Conference Id. */
    if (CheckType == 1)
    {
        if (((pMsgBufr[1] >> 8) & 0xFF) != CheckValue)
            return (0);
    }

    /* Verify Test Mode Id. */
    else if (CheckType == 2)
    {
        if (pMsgBufr[1] != CheckValue)
            return (0);
    }

    /* Return with an indication of correct reply. */
    return (1);
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * TransactCmd - Send a command to the DSP and receive it's reply.
 *
 * FUNCTION
 *  This function sends the specified command to the DSP and receives the DSP's
 *  reply.
 *
 * RETURNS
 *  Length of reply message (0 = Failure)
 *
 */
static unsigned int TransactCmd(
    int DspId,                  /* DSP Identifier (0 to MaxDSPCores-1) */
    DSP_WORD *pMsgBufr,         /* pointer to Cmd/Reply message buffer */
    DSP_WORD CmdLength,         /* length of command message (octets) */
    DSP_WORD ReplyType,         /* required type of reply message */
    DSP_WORD ReplyLength,       /* required length of reply message (octets) */
    int ReplyCheckType,         /* reply check type */
    DSP_WORD ReplyCheckValue    /* reply check value */
    )
{
    int FuncStatus;             /* function status */
    int LoopCount;              /* wait loop counter */
    DSP_WORD RcvReplyLength;    /* received Reply message length */
    DSP_WORD RcvReplyType;      /* received Reply message type code */
    DSP_WORD RetValue;          /* return value */

    /* Default the return value to indicate a failure. */
    RetValue = 0;

    /* Lock access to the DSP. */
    gpakLockAccess(DspId);

    /* Attempt to write the command message to the DSP. */
    LoopCount = 0;
    while ((FuncStatus = WriteDspCmdMessage(DspId, pMsgBufr, CmdLength)) != 1)
    {
        if (FuncStatus == -1)
            break;
        if (++LoopCount > MAX_WAIT_LOOPS)
            break;
        gpakHostDelay();
    }

    /* Attempt to read the reply message from the DSP if the command message was
       sent successfully. */
    if (FuncStatus == 1)
    {
        for (LoopCount = 0; LoopCount < MAX_WAIT_LOOPS; LoopCount++)
        {
            RcvReplyLength = MSG_BUFFER_SIZE * 2;
            FuncStatus = ReadDspReplyMessage(DspId, pMsgBufr, &RcvReplyLength);
            if (FuncStatus == 1)
            {
                RcvReplyType = (pMsgBufr[0] >> 8) & 0xFF;
                if ((RcvReplyLength >= ReplyLength) &&
                    (RcvReplyType == ReplyType) &&
                    VerifyReply(pMsgBufr, ReplyCheckType, ReplyCheckValue))
                {
                    RetValue = RcvReplyLength;
                    break;
                }
                else if (RcvReplyType == MSG_NULL_REPLY)
                    break;
            }
            else if (FuncStatus == -1)
                break;
            gpakHostDelay();
        }
    }

    /* Unlock access to the DSP. */
    gpakUnlockAccess(DspId);

    /* Return the length of the reply message (0 = failure). */
    return (RetValue);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakConfigurePorts - Configure a DSP's serial ports.
 *
 * FUNCTION
 *  This function configures a DSP's serial ports.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakConfigPortStatus_t gpakConfigurePorts(
    unsigned short int DspId,       /* DSP Id (0 to MaxDSPCores-1) */
    GpakPortConfig_t *pPortConfig,  /* pointer to Port Config info */
    GPAK_PortConfigStat_t *pStatus  /* pointer to Port Config Status */
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (CpsInvalidDsp);

    /* Build the Configure Serial Ports message. */
    MsgBuffer[0] = MSG_CONFIGURE_PORTS << 8;
    MsgBuffer[1] = (DSP_WORD)
                   ((pPortConfig->SlotsSelect1 << 12) |
                    ((pPortConfig->FirstBlockNum1 << 8) & 0x0F00) |
                    ((pPortConfig->SecBlockNum1 << 4) & 0x00F0));
    MsgBuffer[2] = (DSP_WORD) pPortConfig->FirstSlotMask1;
    MsgBuffer[3] = (DSP_WORD) pPortConfig->SecSlotMask1;
    MsgBuffer[4] = (DSP_WORD)
                   ((pPortConfig->SlotsSelect2 << 12) |
                    ((pPortConfig->FirstBlockNum2 << 8) & 0x0F00) |
                    ((pPortConfig->SecBlockNum2 << 4) & 0x00F0));
    MsgBuffer[5] = (DSP_WORD) pPortConfig->FirstSlotMask2;
    MsgBuffer[6] = (DSP_WORD) pPortConfig->SecSlotMask2;
    MsgBuffer[7] = (DSP_WORD)
                   ((pPortConfig->SlotsSelect3 << 12) |
                    ((pPortConfig->FirstBlockNum3 << 8) & 0x0F00) |
                    ((pPortConfig->SecBlockNum3 << 4) & 0x00F0));
    MsgBuffer[8] = (DSP_WORD) pPortConfig->FirstSlotMask3;
    MsgBuffer[9] = (DSP_WORD) pPortConfig->SecSlotMask3;

    MsgBuffer[10] = (DSP_WORD)
                   (((pPortConfig->DxDelay1 << 11) & 0x0800) |
                    ((pPortConfig->RxDataDelay1 << 9) & 0x0600) |
                    ((pPortConfig->TxDataDelay1 << 7) & 0x0180) |
                    ((pPortConfig->RxClockPolarity1 << 6) & 0x0040) |
                    ((pPortConfig->TxClockPolarity1 << 5) & 0x0020) |
                    ((pPortConfig->RxFrameSyncPolarity1 << 4) & 0x0010) |
                    ((pPortConfig->TxFrameSyncPolarity1 << 3) & 0x0008) |
                    ((pPortConfig->CompandingMode1 << 1) & 0x0006) |
                    (pPortConfig->SerialWordSize1 & 0x0001));

    MsgBuffer[11] = (DSP_WORD)
                   (((pPortConfig->DxDelay2 << 11) & 0x0800) |
                    ((pPortConfig->RxDataDelay2 << 9) & 0x0600) |
                    ((pPortConfig->TxDataDelay2 << 7) & 0x0180) |
                    ((pPortConfig->RxClockPolarity2 << 6) & 0x0040) |
                    ((pPortConfig->TxClockPolarity2 << 5) & 0x0020) |
                    ((pPortConfig->RxFrameSyncPolarity2 << 4) & 0x0010) |
                    ((pPortConfig->TxFrameSyncPolarity2 << 3) & 0x0008) |
                    ((pPortConfig->CompandingMode2 << 1) & 0x0006) |
                    (pPortConfig->SerialWordSize1 & 0x0001));

    MsgBuffer[12] = (DSP_WORD)
                   (((pPortConfig->DxDelay3 << 11) & 0x0800) |
                    ((pPortConfig->RxDataDelay3 << 9) & 0x0600) |
                    ((pPortConfig->TxDataDelay3 << 7) & 0x0180) |
                    ((pPortConfig->RxClockPolarity3 << 6) & 0x0040) |
                    ((pPortConfig->TxClockPolarity3 << 5) & 0x0020) |
                    ((pPortConfig->RxFrameSyncPolarity3 << 4) & 0x0010) |
                    ((pPortConfig->TxFrameSyncPolarity3 << 3) & 0x0008) |
                    ((pPortConfig->CompandingMode3 << 1) & 0x0006) |
                    (pPortConfig->SerialWordSize3 & 0x0001));

    MsgBuffer[13] = (DSP_WORD) pPortConfig->ThirdSlotMask1;
    MsgBuffer[14] = (DSP_WORD) pPortConfig->FouthSlotMask1;
    MsgBuffer[15] = (DSP_WORD) pPortConfig->FifthSlotMask1;
    MsgBuffer[16] = (DSP_WORD) pPortConfig->SixthSlotMask1;
    MsgBuffer[17] = (DSP_WORD) pPortConfig->SevenSlotMask1;
    MsgBuffer[18] = (DSP_WORD) pPortConfig->EightSlotMask1;
    
    MsgBuffer[19] = (DSP_WORD) pPortConfig->ThirdSlotMask2;;
    MsgBuffer[20] = (DSP_WORD) pPortConfig->FouthSlotMask2;            
    MsgBuffer[21] = (DSP_WORD) pPortConfig->FifthSlotMask2;;             
    MsgBuffer[22] = (DSP_WORD) pPortConfig->SixthSlotMask2;            
    MsgBuffer[23] = (DSP_WORD) pPortConfig->SevenSlotMask2;;             
    MsgBuffer[24] = (DSP_WORD) pPortConfig->EightSlotMask2;            

    MsgBuffer[25] = (DSP_WORD) pPortConfig->ThirdSlotMask3;;
    MsgBuffer[26] = (DSP_WORD) pPortConfig->FouthSlotMask3;            
    MsgBuffer[27] = (DSP_WORD) pPortConfig->FifthSlotMask3;;             
    MsgBuffer[28] = (DSP_WORD) pPortConfig->SixthSlotMask3;            
    MsgBuffer[29] = (DSP_WORD) pPortConfig->SevenSlotMask3;;             
    MsgBuffer[30] = (DSP_WORD) pPortConfig->EightSlotMask3;            


    /* Attempt to send the Configure Serial Ports message to the DSP and receive
       it's reply. */
	if (!TransactCmd(DspId, MsgBuffer, 62, MSG_CONFIG_PORTS_REPLY, 4, 0, 0))
        return (CpsDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    *pStatus = (GPAK_PortConfigStat_t) (MsgBuffer[1] & 0xFF);
    if (*pStatus == Pc_Success)
        return (CpsSuccess);
    else
        return (CpsParmError);
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakConfigureChannel - Configure a DSP's Channel.
 *
 * FUNCTION
 *  This function configures a DSP's Channel.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakConfigChanStatus_t gpakConfigureChannel(
    unsigned short int DspId,           /* DSP Id (0 to MaxDSPCores-1) */
    unsigned short int ChannelId,       /* Channel Id (0 to MaxChannels-1) */
    GpakChanType ChannelType,           /* Channel Type */
    GpakChannelConfig_t *pChanConfig,   /* pointer to Channel Config info */
    GPAK_ChannelConfigStat_t *pStatus   /* pointer to Channel Config Status */
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD MsgLength;                     /* message length */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (CcsInvalidDsp);

    /* Make sure the Channel Id is valid. */
    if (ChannelId >= MaxChannels[DspId])
        return (CcsInvalidChannel);

    /* Build the Configure Channel message based on the Channel Type. */
    switch (ChannelType)
    {

    /* PCM to Packet channel type. */
    case tdmToTdm:

        MsgBuffer[2] = (DSP_WORD)
                       ((pChanConfig->PcmInPortA << 8) |
                        (pChanConfig->PcmInSlotA & 0xFF));
        MsgBuffer[3] = (DSP_WORD)
                       ((pChanConfig->PcmOutPortA << 8) |
                        (pChanConfig->PcmOutSlotA & 0xFF));

        MsgBuffer[4] = (DSP_WORD)
                       ((pChanConfig->PcmInPortB << 8) |
                        (pChanConfig->PcmInSlotB & 0xFF));
        MsgBuffer[5] = (DSP_WORD)
                       ((pChanConfig->PcmOutPortB << 8) |
                        (pChanConfig->PcmOutSlotB & 0xFF));

        MsgBuffer[6] = (DSP_WORD)
                       (
                       ((pChanConfig->FaxCngDetB <<11) & 0x0800)  |
                       ((pChanConfig->FaxCngDetA <<10) & 0x0400)  |
                       ((pChanConfig->MuteToneB << 9) & 0x0200)  |
                       ((pChanConfig->MuteToneA << 8) & 0x0100)  |
                       ((pChanConfig->FrameRate << 6)  & 0x00C0) |  
                       ((pChanConfig->ToneTypesB << 5) & 0x0020) |
                       ((pChanConfig->ToneTypesA << 4) & 0x0010) |
                       ((pChanConfig->SoftwareCompand & 3) << 2) |
                        (pChanConfig->EcanEnableB << 1) | 
                        (pChanConfig->EcanEnableA & 1)
                        );
                        
        MsgBuffer[7]   = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanTapLength;       
        MsgBuffer[8]   = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanNlpType;         
        MsgBuffer[9]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanAdaptEnable;     
        MsgBuffer[10]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanG165DetEnable;   
        MsgBuffer[11]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanDblTalkThresh;   
        MsgBuffer[12]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanNlpThreshold;    
        MsgBuffer[13]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanNlpConv;    
        MsgBuffer[14]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanNlpUnConv;    
        MsgBuffer[15]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanNlpMaxSuppress;    

        MsgBuffer[16]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanCngThreshold;    
        MsgBuffer[17]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanAdaptLimit;      
        MsgBuffer[18]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanCrossCorrLimit;  
        MsgBuffer[19]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanNumFirSegments;  
        MsgBuffer[20]  = (DSP_WORD)
                         pChanConfig->EcanParametersA.EcanFirSegmentLen;   

        MsgBuffer[21]   = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanTapLength;       
        MsgBuffer[22]   = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanNlpType;         
        MsgBuffer[23]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanAdaptEnable;     
        MsgBuffer[24]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanG165DetEnable;   
        MsgBuffer[25]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanDblTalkThresh;   
        MsgBuffer[26]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanNlpThreshold;    
        MsgBuffer[27]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanNlpConv;    
        MsgBuffer[28]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanNlpUnConv;    
        MsgBuffer[29]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanNlpMaxSuppress;    
        MsgBuffer[30]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanCngThreshold;    
        MsgBuffer[31]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanAdaptLimit;      
        MsgBuffer[32]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanCrossCorrLimit;  
        MsgBuffer[33]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanNumFirSegments;  
        MsgBuffer[34]  = (DSP_WORD)
                         pChanConfig->EcanParametersB.EcanFirSegmentLen;   

        MsgLength = 70; // byte number == 35*2 
        break;


    /* Unknown (invalid) channel type. */
    default:
        *pStatus = Cc_InvalidChannelType;
        return (CcsParmError);
    }

    MsgBuffer[0] = MSG_CONFIGURE_CHANNEL << 8;
    MsgBuffer[1] = (DSP_WORD) ((ChannelId << 8) | (ChannelType & 0xFF));

    /* Attempt to send the Configure Channel message to the DSP and receive it's
       reply. */
	if (!TransactCmd(DspId, MsgBuffer, MsgLength, MSG_CONFIG_CHAN_REPLY, 4, 1,
                     (DSP_WORD) ChannelId))
        return (CcsDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    *pStatus = (GPAK_ChannelConfigStat_t) (MsgBuffer[1] & 0xFF);
    if (*pStatus == Cc_Success)
        return (CcsSuccess);
    else
        return (CcsParmError);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakTearDownChannel - Tear Down a DSP's Channel.
 *
 * FUNCTION
 *  This function tears down a DSP's Channel.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakTearDownStatus_t gpakTearDownChannel(
    unsigned short int DspId,           /* DSP Id (0 to MaxDSPCores-1) */
    unsigned short int ChannelId,       /* Channel Id (0 to MaxChannels-1) */
    GPAK_TearDownChanStat_t *pStatus    /* pointer to Tear Down Status */
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (TdsInvalidDsp);

    /* Make sure the Channel Id is valid. */
    if (ChannelId >= MaxChannels[DspId])
        return (TdsInvalidChannel);

    /* Build the Tear Down Channel message. */
    MsgBuffer[0] = MSG_TEAR_DOWN_CHANNEL << 8;
    MsgBuffer[1] = (DSP_WORD) (ChannelId << 8);

    /* Attempt to send the Tear Down Channel message to the DSP and receive it's
       reply. */
	if (!TransactCmd(DspId, MsgBuffer, 3, MSG_TEAR_DOWN_REPLY, 4, 1,
	                 (DSP_WORD) ChannelId))
        return (TdsDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    *pStatus = (GPAK_TearDownChanStat_t) (MsgBuffer[1] & 0xFF);
    if (*pStatus == Td_Success)
        return (TdsSuccess);
    else
        return (TdsError);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakAlgControl - Control an Algorithm.
 *
 * FUNCTION
 *  This function controls an Algorithm
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakAlgControlStat_t gpakAlgControl(
    unsigned short int  DspId,          // DSP identifier
    unsigned short int  ChannelId,		// channel identifier
    GpakAlgCtrl_t       ControlCode,    // algorithm control code
    GPAK_AlgControlStat_t *pStatus      // pointer to return status
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    
    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (AcInvalidDsp);

    /* Make sure the Channel Id is valid. */
    if (ChannelId >= MaxChannels[DspId])
        return (AcInvalidChannel);

    MsgBuffer[0] = MSG_ALG_CONTROL << 8;
    MsgBuffer[1] = (DSP_WORD) ((ChannelId << 8) | (ControlCode & 0xFF));

    /* Attempt to send the Tear Down Channel message to the DSP and receive it's
       reply. */
	//need_reply_len;       
	if (!TransactCmd(DspId, MsgBuffer, 4, MSG_ALG_CONTROL_REPLY, 4, 1,  
	                 (DSP_WORD) ChannelId))
        return (AcDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    *pStatus = (GPAK_AlgControlStat_t) (MsgBuffer[1] & 0xFF);
    if (*pStatus == Ac_Success)
        return (AcSuccess);
    else
        return (AcParmError);

}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakReadEventFIFOMessage - read from the event fifo
 * 
 * FUNCTION
 *  This function reads a single event from the event fifo if one is available
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 *
 * Notes: This function should be called in a loop until the return status 
 *        indicates that the fifo is empty.
 *      
 *        If the event code equals "EventLoopbackTeardownComplete", then the 
 *        contents of *pChannelId hold the coderBlockId that was assigned to
 *        the loopback coder that was torn down.
 */
gpakReadEventFIFOMessageStat_t gpakReadEventFIFOMessage(
    unsigned short int      DspId,          // DSP identifier
    unsigned short int      *pChannelId,    // pointer to channel identifier
    GpakAsyncEventCode_t    *pEventCode,    // pointer to Event Code
    GpakAsyncEventData_t    *pEventData     // pointer to Event Data Struct
    )
{
    DSP_WORD WordBuffer[WORD_BUFFER_SIZE];  /* DSP words buffer */
    GpakAsyncEventCode_t EventCode;         /* DSP's event code */
    DSP_WORD EventDataLength;               /* Length of event to read */
    DSP_WORD ChannelId;                     /* DSP's channel Id */
    DSP_ADDRESS EventInfoAddress;     /* address of EventFIFO info structure */
    DSP_ADDRESS BufrBaseAddress;      /* base address of EventFIFO buffer */
    DSP_ADDRESS BufrLastAddress;      /* last address of EventFIFO buffer */
    DSP_ADDRESS TakeAddress;          /* current take address in fifo buffer */
    DSP_WORD BufrSize;      /* size (in words) of event FIFO buffer */
    DSP_WORD PutIndex;      /* event fifo put index */
    DSP_WORD TakeIndex;     /* event fifo take index */
    DSP_WORD WordsReady;    /* number words ready for read out of event fifo */
    DSP_WORD EventError;    /* flag indicating error with event fifo msg  */
#if 0
    DSP_WORD *pDebugData;   /* debug data buffer pointer in event data struct */
#endif

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RefInvalidDsp);

    /* Lock access to the DSP. */
    gpakLockAccess(DspId);

    /* Check if the DSP was reset and is ready. */
    if (CheckDspReset(DspId) == -1)
    {
        gpakUnlockAccess(DspId);
        return (RefDspCommFailure);
    }

    /* Check if an event message is ready in the DSP. */
    EventInfoAddress = pEventFifoAddress[DspId];
    gpakReadDspMemory(DspId, EventInfoAddress, CIRC_BUFFER_INFO_STRUCT_SIZE, 
                                                                 WordBuffer);
	RECONSTRUCT_LONGWORD(BufrBaseAddress, ((DSP_WORD *)&WordBuffer[CB_BUFR_BASE]));
    BufrSize = WordBuffer[CB_BUFR_SIZE];
    PutIndex = WordBuffer[CB_BUFR_PUT_INDEX];
    TakeIndex = WordBuffer[CB_BUFR_TAKE_INDEX];
    if (PutIndex >= TakeIndex)
        WordsReady = PutIndex - TakeIndex;
    else
        WordsReady = PutIndex + BufrSize - TakeIndex;

    if (WordsReady < 2)
    {
        gpakUnlockAccess(DspId);
        return (RefNoEventAvail);
    }

    /* Read the event header from the DSP's Event FIFO. */
    TakeAddress = BufrBaseAddress + TakeIndex;
    BufrLastAddress = BufrBaseAddress + BufrSize - 1;
    ReadCircBuffer(DspId, BufrBaseAddress, BufrLastAddress, &TakeAddress,
                   WordBuffer, 2);
    TakeIndex += 2;
    if (TakeIndex >= BufrSize)
        TakeIndex -= BufrSize;

    ChannelId = (WordBuffer[0] >> 8) & 0xFF;
    EventCode = (GpakAsyncEventCode_t)(WordBuffer[0] & 0xFF);
    EventDataLength = WordBuffer[1];
    EventError = 0;

    switch (EventCode)
    {
        case EventToneDetect:
            if (EventDataLength > WORD_BUFFER_SIZE)
            {
                gpakUnlockAccess(DspId);
#if 0
		printk("EventDataLength > WORD_BUFFER_SIZE (%d)\n", EventDataLength);
#endif
                return (RefInvalidEvent);
            }
            ReadCircBuffer(DspId, BufrBaseAddress, BufrLastAddress, &TakeAddress,
                   WordBuffer, EventDataLength);
            pEventData->toneEvent.ToneCode = (GpakToneCodes_t)
                                                (WordBuffer[0] & 0xFF);
            pEventData->toneEvent.ToneDuration = WordBuffer[1];
            pEventData->toneEvent.Direction = WordBuffer[2];
            pEventData->toneEvent.DebugToneStatus = WordBuffer[3];
            TakeIndex += EventDataLength;
            if (TakeIndex >= BufrSize)
                TakeIndex -= BufrSize;
            if (EventDataLength != 4) {
#if 0
		    printk("EventDataLength != 4 it's %d\n", EventDataLength);
#endif
                EventError = 1;
	    }
            break;

        default:
#if 0
	    printk("Event Code not in switch\n");
#endif
            EventError = 1;
            break;
    };

    /* Update the Take index in the DSP's Packet Out buffer information. */
    gpakWriteDspMemory(DspId, EventInfoAddress + CB_BUFR_TAKE_INDEX, 1,
                       &TakeIndex);

    /* Unlock access to the DSP. */
    gpakUnlockAccess(DspId);

    if (EventError)
        return(RefInvalidEvent);

    *pChannelId = ChannelId;
    *pEventCode = EventCode;
    return(RefEventAvail);

}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakPingDsp - ping the DSP to see if it's alive
 * 
 * FUNCTION
 *  This function checks if the DSP is still communicating with the host
 *  and returns the DSP SW version
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */
gpakPingDspStat_t gpakPingDsp(
    unsigned short int      DspId,          // DSP identifier
    unsigned short int      *pDspSwVersion  // DSP software version
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (PngInvalidDsp);

    /* send value of 1, DSP increments it */
    MsgBuffer[0] = (MSG_PING << 8);

    /* Attempt to send the ping message to the DSP and receive it's
       reply. */
	if (!TransactCmd(DspId, MsgBuffer, 1, MSG_PING_REPLY, 6, 0, 0))
        return (PngDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
    {
        *pDspSwVersion = MsgBuffer[2];
        return (PngSuccess);
    }
    else
        return (PngDspCommFailure);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakSerialTxFixedValue - transmit a fixed value on a timeslot
 * 
 * FUNCTION
 *  This function controls transmission of a fixed value out onto a serial 
 *  port's timeslot.
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */
gpakSerialTxFixedValueStat_t gpakSerialTxFixedValue(
    unsigned short int      DspId,          // DSP identifier
    unsigned short int      ChannelId,      // channel identifier
    GpakSerialPort_t        PcmOutPort,     // PCM Output Serial Port Id
    unsigned short int      PcmOutSlot,     // PCM Output Time Slot
    unsigned short int      Value,          // 16-bit value 
    GpakActivation          State 		    // activation state
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (TfvInvalidDsp);

    /* Make sure the Channel Id is valid. */
    if (ChannelId >= MaxChannels[DspId])
        return (TfvInvalidChannel);


    /* Build the message. */
    MsgBuffer[0] = MSG_SERIAL_TXVAL << 8;
    MsgBuffer[1] = (DSP_WORD) ((ChannelId << 8) | (State & 0xFF));
    MsgBuffer[2] = (DSP_WORD) ((PcmOutPort << 8) | (PcmOutSlot & 0xFF));
    MsgBuffer[3] = (DSP_WORD) Value;

    /* Attempt to send the message to the DSP and receive it's
       reply. */
    //need_reply_len;
	if (!TransactCmd(DspId, MsgBuffer, 8, MSG_SERIAL_TXVAL_REPLY, 4,
	                                                    1, ChannelId))
        return (TfvDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
        return (TfvSuccess);
    else
        return (TfvDspCommFailure);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakControlTdmLoopBack - control a serial port's loopback state
 * 
 * FUNCTION
 *  This function enables/disables the tdm input to output looback mode on a
 *  serial port
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
gpakControlTdmLoopBackStat_t gpakControlTdmLoopBack(
    unsigned short int      DspId,          // DSP identifier
    GpakSerialPort_t        SerialPort,     // Serial Port Id
    GpakActivation          LoopBackState   // Loopback State
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (ClbInvalidDsp);

    /* Build the message. */
    MsgBuffer[0] = MSG_TDM_LOOPBACK << 8;
    MsgBuffer[1] = (DSP_WORD) ((SerialPort << 8) | (LoopBackState & 0xFF));

    /* Attempt to send the message to the DSP and receive it's
       reply. */
    //need_reply_len;
	if (!TransactCmd(DspId, MsgBuffer, 4, MSG_TDM_LOOPBACK_REPLY, 4, 0, 0))
        return (ClbDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
        return (ClbSuccess);
    else
        return (ClbDspCommFailure);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakReadCpuUsage - Read CPU usage statistics from a DSP.
 *
 * FUNCTION
 *  This function reads the CPU usage statistics from a DSP's memory. The
 *  average CPU usage in units of .1 percent are obtained for each of the frame
 *  rates.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakReadCpuUsageStat_t gpakReadCpuUsage(
    unsigned short int  DspId,              // Dsp Identifier
    unsigned short int  *pPeakUsage,        // pointer to peak usage variable
    unsigned short int  *pPrev1SecPeakUsage // peak usage over previous 1 second   
    )
{
    DSP_WORD ReadBuffer[2];     /* DSP read buffer */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RcuInvalidDsp);

    /* Lock access to the DSP. */
    gpakLockAccess(DspId);

    /* Check if the DSP was reset and is ready. */
    if (CheckDspReset(DspId) == -1)
        return (RcuDspCommFailure);

    /* Read the CPU Usage statistics from the DSP. */
    gpakReadDspMemory(DspId, pDspIfBlk[DspId] + CPU_USAGE_OFFSET, 2,
                      ReadBuffer);

    /* Unlock access to the DSP. */
    gpakUnlockAccess(DspId);

    /* Store the usage statistics in the specified variables. */
    *pPrev1SecPeakUsage = ReadBuffer[0];
    *pPeakUsage = ReadBuffer[1];
    
    /* Return with an indication the usage staistics were read successfully. */
    return (RcuSuccess);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakResetCpuUsageStats - reset the cpu usage statistics
 * 
 * FUNCTION
 *  This function resets the cpu utilization statistics
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */
gpakResetCpuUsageStat_t gpakResetCpuUsageStats(
    unsigned short int  DspId              // DSP identifier
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RstcInvalidDsp);

    MsgBuffer[0] = (MSG_RESET_USAGE_STATS << 8);

    /* Attempt to send the message to the DSP and receive it's reply. */
    //need_reply_len;
	if (!TransactCmd(DspId, MsgBuffer, 2, MSG_RESET_USAGE_STATS_REPLY, 4, 0, 0))
        return (RstcDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
        return (RstcSuccess);
    else
        return (RstcDspCommFailure);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakReadFramingStats
 * 
 * FUNCTION
 *  This function reads a DSP's framing interrupt statistics
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */
gpakReadFramingStatsStatus_t gpakReadFramingStats(
    unsigned short int  DspId,                // DSP identifier
    unsigned short int  *pFramingError1Count, // port 1 Framing error count
    unsigned short int  *pFramingError2Count, // port 2 Framing error count
    unsigned short int  *pFramingError3Count, // port 3 Framing error count
    unsigned short int  *pDmaStopErrorCount,   // DMA-stoppage error count
    unsigned short int  *pDmaSlipStatsBuffer   // DMA slips count
    )
{
    DSP_WORD ReadBuffer[10];     /* DSP read buffer */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RfsInvalidDsp);

    /* Lock access to the DSP. */
    gpakLockAccess(DspId);

    /* Check if the DSP was reset and is ready. */
    if (CheckDspReset(DspId) == -1)
        return (RfsDspCommFailure);

    /* Read the framing interrupt statistics from the DSP. */
    gpakReadDspMemory(DspId, pDspIfBlk[DspId] + FRAMING_STATS_OFFSET, 10,
                      ReadBuffer);

    /* Unlock access to the DSP. */
    gpakUnlockAccess(DspId);

    /* Store the framing statistics in the specified variables. */
    *pFramingError1Count = ReadBuffer[0];
    *pFramingError2Count = ReadBuffer[1];
    *pFramingError3Count = ReadBuffer[2];
    *pDmaStopErrorCount  = ReadBuffer[3];
    
    if(pDmaSlipStatsBuffer != 0) 
    // If users want to get the DMA slips count
    {
	    pDmaSlipStatsBuffer[0] = ReadBuffer[4];
    	pDmaSlipStatsBuffer[1] = ReadBuffer[5];
    	pDmaSlipStatsBuffer[2] = ReadBuffer[6];
    	pDmaSlipStatsBuffer[3] = ReadBuffer[7];
    	pDmaSlipStatsBuffer[4] = ReadBuffer[8];
    	pDmaSlipStatsBuffer[5] = ReadBuffer[9];
    
    }
    /* Return with an indication the statistics were read successfully. */
    return (RfsSuccess);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakResetFramingStats - reset a DSP's framing interrupt statistics
 * 
 * FUNCTION
 *  This function resets a DSP's framing interrupt statistics
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */
gpakResetFramingStatsStatus_t gpakResetFramingStats(
    unsigned short int      DspId          // DSP identifier
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RstfInvalidDsp);

    MsgBuffer[0] = (MSG_RESET_FRAME_STATS << 8);

    /* Attempt to send the message to the DSP and receive it's reply. */
    //need_reply_len;
	if (!TransactCmd(DspId, MsgBuffer, 2, MSG_RESET_FRAME_STATS_REPLY, 4, 0, 0))
        return (RstfDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
        return (RstfSuccess);
    else
        return (RstfDspCommFailure);
}

/*
 * gpakDownloadDsp - Download a DSP's Program and initialized Data memory.
 *
 * FUNCTION
 *  This function reads a DSP's Program and Data memory image from the
 *  specified file and writes the image to the DSP's memory.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakDownloadStatus_t gpakDownloadDsp(
    unsigned short DspId,   /* DSP Identifier (0 to MaxDSPCores-1) */
    GPAK_FILE_ID FileId     /* G.PAK Download File Identifier */
    )
{
    gpakDownloadStatus_t RetStatus;     /* function return status */
    int NumRead;                        /* number of file bytes read */
    DSP_ADDRESS Address;                /* DSP address */
    unsigned int WordCount;             /* number of words in record */
    unsigned int NumWords;              /* number of words to read/write */
    unsigned int i;                     /* loop index / counter */
    unsigned int j;                     /* loop index */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (GdlInvalidDsp);

    /* Lock access to the DSP. */
    gpakLockAccess(DspId);

    RetStatus = GdlSuccess;
    while (RetStatus == GdlSuccess)
    {

        /* Read a record header from the file. */
        NumRead = gpakReadFile(FileId, DlByteBufr, 6);
        if (NumRead == -1)
        {
            RetStatus = GdlFileReadError;
            break;
        }
        if (NumRead != 6)
        {
            RetStatus = GdlInvalidFile;
            break;
        }
        Address = (((DSP_ADDRESS) DlByteBufr[1]) << 16) |
                  (((DSP_ADDRESS) DlByteBufr[2]) << 8) |
                  ((DSP_ADDRESS) DlByteBufr[3]);
        WordCount = (((unsigned int) DlByteBufr[4]) << 8) |
                    ((unsigned int) DlByteBufr[5]);

        /* Check for the End Of File record. */
        if (DlByteBufr[0] == 0xFF)
            break;

        /* Verify the record is for a valid memory type. */
        if ((DlByteBufr[0] != 0x00) && (DlByteBufr[0] != 0x01))
        {
            RetStatus = GdlInvalidFile;
            break;
        }

        /* Read a block of words at a time from the file and write to the
           DSP's memory .*/
        while (WordCount != 0)
        {
            if (WordCount < DOWNLOAD_BLOCK_SIZE)
                NumWords = WordCount;
            else
                NumWords = DOWNLOAD_BLOCK_SIZE;
            WordCount -= NumWords;
            NumRead = gpakReadFile(FileId, DlByteBufr, NumWords * 2);
            if (NumRead == -1)
            {
                RetStatus = GdlFileReadError;
                break;
            }
            if (NumRead != (NumWords * 2))
            {
                RetStatus = GdlInvalidFile;
                break;
            }
            for (i = 0, j = 0; i < NumWords; i++, j += 2)
                DlWordBufr[i] = (((DSP_WORD) DlByteBufr[j]) << 8) |
                                ((DSP_WORD) DlByteBufr[j + 1]);
            gpakWriteDspMemory(DspId, Address, NumWords, DlWordBufr);
            Address += ((DSP_ADDRESS) NumWords);
        }
    }

    /* Unlock access to the DSP. */
    gpakUnlockAccess(DspId);

    /* Return with an indication of success or failure. */
    return (RetStatus);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakReadCpuUsage - Read CPU usage statistics from a DSP.
 *
 * FUNCTION
 *  This function reads the memory map register section of DSP memory. 
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakReadDSPMemoryStat_t gpakReadDSPMemoryMap(
    unsigned short int  DspId,         // Dsp Identifier
    unsigned short int  *pDest,        // Buffer on host to hold DSP memory map
	DSP_ADDRESS BufrBaseAddress,       // DSP memory users want to read out
    unsigned short int   MemoryLength_Word16 // Length of memory section read out, unit is 16-bit word
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP reply's status */
    int i;                                  /* loop index / counter */

    
    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RmmInvalidDsp);

    /* Verify the message buffer is large enough  */
    if (MSG_BUFFER_SIZE < MemoryLength_Word16 )
        return (RmmSizeTooBig);

    MsgBuffer[0] = MSG_READ_DSP_MEMORY << 8;
    MsgBuffer[1] = (DSP_WORD) ((BufrBaseAddress >> 16) & 0xFFFF);
    MsgBuffer[2] = (DSP_WORD) (BufrBaseAddress & 0xFFFF);
    MsgBuffer[3] = (DSP_WORD) MemoryLength_Word16;

    /* Attempt to send the Read memory section message to the DSP and receive it's
       reply. */
	//need_reply_len;       
	if (!TransactCmd(DspId, MsgBuffer, 8, MSG_READ_DSP_MEMORY_REPLY, 
			(MemoryLength_Word16+2)*2, 0,  0) )
        return (RmmInvalidAddress);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus != 0)
        return (RmmFailure);

	for (i = 0; i < MemoryLength_Word16; i++)
        pDest[i] = (short int) MsgBuffer[2 + i];


    return (RmmSuccess);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakAccessGPIO - change Direction/read/write the GPIO on DSP 
 * 
 * FUNCTION
 *  This function read/write GPIO and change the GPIO direction
 *  
 * 
 * RETURNS
 *  Status  code indicating success or a specific error.
 */
gpakAccessGPIOStat_t gpakAccessGPIO(
    unsigned short int      DspId,          // DSP identifier
	GpakGPIOCotrol_t        gpakControlGPIO,// select oeration, changeDIR/write/read
    unsigned short int      *pGPIOValue  // DSP software version
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (GPIOInvalidDsp);

    /* send value of 1, DSP increments it */
    MsgBuffer[0] = (MSG_ACCESSGPIO << 8);
	MsgBuffer[1] = (DSP_WORD) ((gpakControlGPIO << 8) | (*pGPIOValue & 0xFF) );
    /* Attempt to send the ping message to the DSP and receive it's
       reply. */
	if (!TransactCmd(DspId, MsgBuffer, 4, MSG_ACCESSGPIO_REPLY, 6, 0, 0))
        return (GPIODspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
    {
        *pGPIOValue = MsgBuffer[2];
        return (GPIOSuccess);
    }
    else
        return (GPIODspCommFailure);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakWriteSystemParms - Write a DSP's System Parameters.
 *
 * FUNCTION
 *  This function writes a DSP's System Parameters information.
 *
 *    Note:
 *      Or-together the desired bit-mask #defines that are listed below. Only
 *      those algorithm parameters whose bit-mask is selected in the UpdateBits
 *      function parameter will be updated.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */

gpakWriteSysParmsStatus_t gpakWriteSystemParms(
    unsigned short int      DspId,  // DSP identifier
    GpakSystemParms_t *pSysParms,   /* pointer to System Parms info var */
    unsigned short int UpdateBits, /* input: flags indicating which parms to update */
    GPAK_SysParmsStat_t *pStatus    /* pointer to Write System Parms Status */
    )
{
    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */
    DSP_WORD DspStatus;                     /* DSP's reply status */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (WspInvalidDsp);

    /* Build the Write System Parameters message. */
    MsgBuffer[0] = MSG_WRITE_SYS_PARMS << 8;

    if (UpdateBits & DTMF_UPDATE_MASK)
    {
        MsgBuffer[1] |= DTMF_UPDATE_MASK;
        MsgBuffer[8] = (DSP_WORD) pSysParms->MinSigLevel;
        MsgBuffer[9] = (DSP_WORD) (pSysParms->FreqDeviation & 0xff);
        if (pSysParms->SNRFlag)
            MsgBuffer[9] |= (1<<8);
    }

    MsgBuffer[10] = (DSP_WORD) 0;
    if (UpdateBits & DTMF_TWIST_UPDATE_MASK)
    {
        MsgBuffer[1] |= DTMF_TWIST_UPDATE_MASK;
        MsgBuffer[10] |= (DSP_WORD) (pSysParms->DtmfFwdTwist & 0x000f);
        MsgBuffer[10] |= (DSP_WORD) ((pSysParms->DtmfRevTwist << 4) & 0x00f0);
    }


    if (UpdateBits & DTMF_VALID_MASK)
    {
        MsgBuffer[1] |= DTMF_VALID_MASK;
	    MsgBuffer[11] = (DSP_WORD) (pSysParms->DtmfValidityMask & 0x00ff);
	}

    /* Attempt to send the ping message to the DSP and receive it's
       reply. */
	if (!TransactCmd(DspId, MsgBuffer, 24, MSG_WRITE_SYS_PARMS_REPLY, 6, 0, 0))
        return (WspDspCommFailure);

    /* Return with an indication of success or failure based on the return
       status in the reply message. */
    *pStatus = (GPAK_SysParmsStat_t) (MsgBuffer[2] );

    DspStatus = (MsgBuffer[1] & 0xFF);
    if (DspStatus == 0)
        return (WspSuccess);
    else
        return (WspDspCommFailure);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * gpakReadSystemParms - Read a DSP's System Parameters.
 *
 * FUNCTION
 *  This function reads a DSP's System Parameters information.
 *
 * RETURNS
 *  Status code indicating success or a specific error.
 *
 */
gpakReadSysParmsStatus_t gpakReadSystemParms(
    unsigned short int      DspId,  // DSP identifier
    GpakSystemParms_t *pSysParms    /* pointer to System Parms info var */
    )
{

    DSP_WORD MsgBuffer[MSG_BUFFER_SIZE];    /* message buffer */

    /* Make sure the DSP Id is valid. */
    if (DspId >= MAX_DSP_CORES)
        return (RspInvalidDsp);

    /* Build the Read System Parameters message. */
    MsgBuffer[0] = MSG_READ_SYS_PARMS << 8;

    /* Attempt to send the ping message to the DSP and receive it's
       reply. */
	if (!TransactCmd(DspId, MsgBuffer, 2, MSG_READ_SYS_PARMS_REPLY, 22, 0, 0))
        return (RspDspCommFailure);

    /* Extract the System Parameters information from the message. */
    pSysParms->DtmfValidityMask = (short int)(MsgBuffer[7]) ;

    pSysParms->MinSigLevel   =  (short int)MsgBuffer[8];
    pSysParms->SNRFlag       = (short int)((MsgBuffer[9]>>8) & 0x1);
    pSysParms->FreqDeviation =  (short int)(MsgBuffer[9] & 0xff);
    pSysParms->DtmfFwdTwist  = (short int)MsgBuffer[10] & 0x000f;
    pSysParms->DtmfRevTwist  = (short int)(MsgBuffer[10] >> 4) & 0x000f;

    /* Return with an indication that System Parameters info was obtained. */
    return (RspSuccess);
}