source: svn/trunk/newcon3bcm2_21bu/magnum/syslib/vbilib/bvbilib_list.c

Last change on this file was 2, checked in by jglee, 11 years ago

first commit

  • Property svn:executable set to *
File size: 14.3 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2008, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bvbilib_list.c $
11 * $brcm_Revision: Hydra_Software_Devel/18 $
12 * $brcm_Date: 10/31/08 5:49p $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log: /magnum/syslib/vbilib/bvbilib_list.c $
19 *
20 * Hydra_Software_Devel/18   10/31/08 5:49p darnstein
21 * PR41250,PR41252: port over conventional comments from 7401 branch.
22 *
23 * Hydra_Software_Devel/17   6/6/08 5:38p darnstein
24 * PR38956: allocate space for CGMS-B data in field handles.
25 *
26 * Hydra_Software_Devel/16   4/3/08 5:49p darnstein
27 * PR38956,PR40709: New API elements supporting CGMS-B encoding.
28 *
29 * Hydra_Software_Devel/15   11/13/07 12:46p darnstein
30 * PR35092: Fix the logic error discovered by Coverity in version
31 * Refsw_Dedicated_vbi_7401/1.
32 *
33 * Hydra_Software_Devel/14   9/11/07 5:17p darnstein
34 * PR25708: First release of SCTE encoder software.
35 *
36 * Hydra_Software_Devel/13   6/6/07 12:50p darnstein
37 * PR30411: multi-line closed caption encoding is ready for test.
38 *
39 * Hydra_Software_Devel/12   1/18/07 3:16p darnstein
40 * PR26464: correctly handle teletext output to multiple VECs
41 *
42 * Hydra_Software_Devel/12   1/18/07 3:13p darnstein
43 * PR26464: correctly handle teletext output to multiple VECs
44 *
45 * Hydra_Software_Devel/11   4/11/06 7:26p darnstein
46 * PR20429: first cut at AMOL encoder software.
47 *
48 * Hydra_Software_Devel/10   10/21/05 7:46p darnstein
49 * PR17732: Gemstar encoder can output a waveform, but I can't make it
50 * change yet.
51 *
52 * Hydra_Software_Devel/9   12/13/04 7:41p darnstein
53 * PR 13255: When recycling a field handle, clear out all its attributes.
54 *
55 * Hydra_Software_Devel/8   11/16/04 1:01p shyam
56 * PR 8365 : Added BERR_TRACE for error returns
57 *
58 * Hydra_Software_Devel/7   7/27/04 11:11a darnstein
59 * PR 9080: don't use private functions to access field handle usage count
60 * in BVBI porting interface.  It is against the rules.
61 *
62 * Hydra_Software_Devel/6   5/11/04 6:58p darnstein
63 * PR 9080: fix a stupid typo.
64 *
65 * Hydra_Software_Devel/5   5/11/04 6:54p darnstein
66 * PR 9080: add support for user processing of VBI data, simultaneously
67 * with hardware encoding of same VBI data.
68 *
69 * Hydra_Software_Devel/4   2/27/04 6:17p darnstein
70 * PR 9080: fix misuse of BDBG_ASSERT macro.
71 *
72 * Hydra_Software_Devel/3   11/19/03 7:45p darnstein
73 * Fix mis-spelling of BVBIlib_List_Create, another minor typo too.
74 *
75 * Hydra_Software_Devel/2   10/21/03 3:01p darnstein
76 * Fix usage of BDBG_ENTER(), BDBG_LEAVE().
77 *
78 * Hydra_Software_Devel/1   10/14/03 11:32a darnstein
79 * Initial version
80 *
81 ***************************************************************************/
82#include "bstd.h"
83#include "berr.h"
84#include "berr_ids.h"
85#include "bdbg.h"                /* Dbglib */
86#include "bkni.h"                /* malloc */
87#include "bvbi.h"                /* VBI p.i. API */
88#include "bvbi_prot.h"           /* VBI p.i. protected data structures */
89#include "bvbilib.h"             /* This module. */
90#include "bvbilib_priv.h"        /* VBI lib internal data structures */
91
92BDBG_MODULE(BVBIlib);
93
94/***************************************************************************
95* Forward declarations of static (private) functions
96***************************************************************************/
97
98/***************************************************************************
99* Local (private) data
100***************************************************************************/
101static const BVBIlib_List_Settings sDefaultSettings = {
102        false, false, false, false, false, false, 0, 0, 0, 0
103};
104
105/***************************************************************************
106* Implementation of "BVBI_" API functions
107***************************************************************************/
108
109/***************************************************************************
110 *
111 */
112BERR_Code BVBIlib_List_GetDefaultSettings (BVBIlib_List_Settings*  pSettings)
113{
114        if (!pSettings)
115        {
116                BDBG_ERR(("Invalid parameter\n"));
117                return BERR_TRACE(BERR_INVALID_PARAMETER);
118        }
119
120        *pSettings = sDefaultSettings;
121
122        return BERR_SUCCESS;
123}
124
125/***************************************************************************
126 *
127 */
128BERR_Code BVBIlib_List_Create(
129        BVBIlib_List_Handle *pVbillHandle,     
130        BVBI_Handle             vbiHandle,
131        int                      nHandles,
132        const BVBIlib_List_Settings*  pSettings
133)
134{
135        BVBIlib_P_List_Handle *pVbilibl;
136        int iHandle;
137        BVBIlib_P_FieldHanger *hanger;
138        BVBI_Field_Handle hField;
139        BERR_Code eErr;
140
141        BDBG_ENTER(BVBIlib_List_Create);
142
143        if((!pVbillHandle) ||
144           (nHandles <= 0)   )
145        {
146                BDBG_ERR(("Invalid parameter\n"));
147                return BERR_TRACE(BERR_INVALID_PARAMETER);
148        }
149
150        /* Alloc the main VBI context. */
151        pVbilibl = 
152                (BVBIlib_P_List_Handle*)(BKNI_Malloc(sizeof(BVBIlib_P_List_Handle)));
153
154        if(!pVbilibl)
155        {
156                return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY);
157        }
158
159        /* Clear out the context and set defaults. */
160        BKNI_Memset((void*)pVbilibl, 0x0, sizeof(BVBIlib_P_List_Handle));
161
162        /* Initialize magic number to the size of the struct */
163        pVbilibl->ulBlackMagic = sizeof(BVBIlib_P_List_Handle);
164
165        /* Store memory handle to allocate teletext data as necessary */
166        pVbilibl->hVbi = vbiHandle;
167
168        /* Store various settings */
169        if (!pSettings)
170                pSettings = &sDefaultSettings;
171        pVbilibl->settings = *pSettings;
172        /* Programming note: these settings are not really needed. But in a future
173           ----------------  version of the module, they might be? */
174
175        /* Initialize empty lists of field handle hangers */
176        BLST_Q_INIT(&pVbilibl->field_contexts);
177        BLST_Q_INIT(&pVbilibl->empty_hangers);
178
179        /* Loop over handles to be created */
180        for (iHandle = 0 ; iHandle < nHandles ; ++iHandle)
181        {
182                /* Create a field handle */
183                if ((eErr = BVBI_Field_Create (&hField, vbiHandle)) != BERR_SUCCESS)
184                {
185                        BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
186                        BDBG_LEAVE(BVBIlib_List_Create);
187                        return eErr;
188                }
189
190                /* Create a field handle hanger to hold the above handle */
191                hanger = 
192                        (BVBIlib_P_FieldHanger*)(BKNI_Malloc(
193                                sizeof(BVBIlib_P_FieldHanger)));
194                if(!hanger)
195                {
196                        BVBI_Field_Destroy (hField);
197                        BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
198                        BDBG_LEAVE(BVBIlib_List_Create);
199                        return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY);
200                }
201                hanger->hField = hField;
202
203                /* Add field/hanger assembly to the free list */
204                BLST_Q_INSERT_HEAD (&pVbilibl->field_contexts, hanger, link);
205
206                /* Allocate for teletext if necessary */
207                if (pSettings->bAllowTeletext)
208                {
209                        if ((eErr = 
210                                BVBI_Field_TT_Allocate (
211                                        hField, BVBI_TT_MAX_LINES, BVBI_TT_MAX_LINELENGTH)) !=
212                                BERR_SUCCESS)
213                        {
214                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
215                                BDBG_LEAVE(BVBIlib_List_Create);
216                                return eErr;
217                        }
218                }
219
220                /* Allocate for VPS if necessary */
221                if (pSettings->bAllowVPS)
222                {
223                        if ((eErr = BVBI_Field_VPS_Allocate (hField, true)) != BERR_SUCCESS)
224                        {
225                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
226                                BDBG_LEAVE(BVBIlib_List_Create);
227                                return eErr;
228                        }
229                }
230
231                /* Allocate for Gemstar if necessary */
232                if (pSettings->bAllowGemstar)
233                {
234                        if ((eErr = BVBI_Field_GS_Allocate (hField, true)) != BERR_SUCCESS)
235                        {
236                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
237                                BDBG_LEAVE(BVBIlib_List_Create);
238                                return eErr;
239                        }
240                }
241
242                /* Allocate for AMOL if necessary */
243                if (pSettings->bAllowAmol)
244                {
245                        if ((eErr = 
246                                BVBI_Field_AMOL_Allocate (hField, BVBI_AMOL_Type_II_Highrate)) 
247                                != BERR_SUCCESS)
248                        {
249                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
250                                BDBG_LEAVE(BVBIlib_List_Create);
251                                return eErr;
252                        }
253                }
254
255                /* Allocate for multi-line closed caption, if necessary */
256                if (pSettings->bAllowMcc)
257                {
258                        if ((eErr = BVBI_Field_MCC_Allocate (hField, true)) != BERR_SUCCESS)
259                        {
260                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
261                                BDBG_LEAVE(BVBIlib_List_Create);
262                                return eErr;
263                        }
264                }
265
266                /* Allocate for CGMS-B, if necessary */
267                if (pSettings->bAllowCgmsB)
268                {
269                        if ((eErr = BVBI_Field_CGMSB_Allocate (hField, true)) != 
270                                BERR_SUCCESS)
271                        {
272                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
273                                BDBG_LEAVE(BVBIlib_List_Create);
274                                return eErr;
275                        }
276                }
277
278                /* Allocate for SCTE if necessary.  Not space efficient. */
279                if ((pSettings->scteCcSize  != 0) || (pSettings->scteNrtvSize != 0) ||
280                        (pSettings->sctePamSize != 0) || (pSettings->scteMonoSize != 0)   )
281                {
282                        if ((eErr = 
283                                BVBI_Field_SCTE_Allocate (hField, 
284                                        pSettings->scteCcSize,  pSettings->scteNrtvSize, 
285                                        pSettings->sctePamSize, pSettings->scteMonoSize)) 
286                                != BERR_SUCCESS)
287                        {
288                                BVBIlib_List_Destroy ((BVBIlib_List_Handle)pVbilibl);
289                                BDBG_LEAVE(BVBIlib_List_Create);
290                                return eErr;
291                        }
292                }
293        }
294
295        /* Initialize status counters */
296        pVbilibl->nAllocated = nHandles;
297        pVbilibl->nInUse     =        0;
298
299        /* All done. now return the new fresh context to user. */
300        *pVbillHandle = (BVBIlib_List_Handle)pVbilibl;
301
302        BDBG_LEAVE(BVBIlib_List_Create);
303        return BERR_SUCCESS;
304}
305
306
307/***************************************************************************
308 *
309 */
310BERR_Code BVBIlib_List_Destroy( BVBIlib_List_Handle listHandle )
311{
312        BVBIlib_P_List_Handle *pVbill;
313        BVBIlib_P_FieldHanger* hanger;
314        BVBI_Field_Handle      hField;
315        BERR_Code eErr      = BERR_SUCCESS;
316        BERR_Code eErrFirst = BERR_SUCCESS;
317
318        BDBG_ENTER(BVBIlib_List_Destroy);
319
320        /* check parameters */
321        BVBILIB_P_GET_LIST_CONTEXT(listHandle, pVbill);
322        if(!pVbill)
323        {
324                BDBG_ERR(("Invalid parameter\n"));
325                return BERR_TRACE(BERR_INVALID_PARAMETER);
326        }
327
328        /* Determine if user has failed to turn in all handles */
329        if (pVbill->nInUse != 0)
330        {
331                BDBG_ERR(("Field handle leak\n"));
332                return BERR_TRACE(BVBIlib_USER_LEAK);
333        }
334
335        /* Clear out list of free handles */
336        /* coverity[alias] */
337        /* coverity[use_after_free] */
338        while ((hanger = BLST_Q_FIRST (&pVbill->field_contexts)) != NULL)
339        {
340                BLST_Q_REMOVE (&pVbill->field_contexts, hanger, link);
341                hField = hanger->hField;
342                if ((eErr = BVBI_Field_Destroy (hField)) != BERR_SUCCESS)
343                {
344                        if (eErrFirst == BERR_SUCCESS)
345                        {
346                                eErrFirst = eErr;
347                        }
348                }
349                BKNI_Free ((void*)hanger);
350        }
351
352        /* Clear out list of unused field handle hangers */
353        /* coverity[alias] */
354        /* coverity[use_after_free] */
355        while ((hanger = BLST_Q_FIRST (&pVbill->empty_hangers)) != NULL)
356        {
357                BLST_Q_REMOVE (&pVbill->empty_hangers, hanger, link);
358                /* coverity[freed_arg] */
359                BKNI_Free ((void*)hanger);
360        }
361
362        /* The handle is now invalid */
363        pVbill->ulBlackMagic = 0;
364
365        /* Release context in system memory */
366        BKNI_Free((void*)pVbill);
367
368        BDBG_LEAVE(BVBIlib_List_Destroy);
369        return eErrFirst;
370}
371
372
373/***************************************************************************
374 *
375 */
376BERR_Code BVBIlib_List_Obtain_isr(
377        BVBIlib_List_Handle   vbillHandle,
378        BVBI_Field_Handle *  pFieldHandle
379)
380{
381        BVBIlib_P_List_Handle *pVbill;
382        BVBIlib_P_FieldHanger* hanger;
383        BERR_Code eErr      = BERR_SUCCESS;
384
385        BDBG_ENTER(BVBIlib_List_Obtain_isr);
386
387        /* check parameters */
388        BVBILIB_P_GET_LIST_CONTEXT(vbillHandle, pVbill);
389        if(!pVbill)
390        {
391                BDBG_ERR(("Invalid parameter\n"));
392                return BERR_TRACE(BERR_INVALID_PARAMETER);
393        }
394
395        /* If there is a handle available */
396        if ((hanger = BLST_Q_FIRST (&pVbill->field_contexts)) != NULL)
397        {
398                /* Take it out of the free list */
399                BLST_Q_REMOVE (&pVbill->field_contexts, hanger, link);
400
401                /* Adjust attributes */
402                BVBI_Field_Zero_UsageCount_isr (hanger->hField);
403                BVBI_Field_ClearState_isr      (hanger->hField);
404
405                /* Give it to the caller */
406                *pFieldHandle = hanger->hField;
407
408                /* Hold on to the empty hanger for use later */
409                hanger->hField = NULL;
410                BLST_Q_INSERT_TAIL (&pVbill->empty_hangers, hanger, link);
411
412                /* Adjust statistic */
413                ++(pVbill->nInUse);
414        }
415        else
416        {
417                /* Complain */
418                eErr = BERR_TRACE(BVBIlib_NO_HANDLES);
419        }
420
421        BDBG_LEAVE(BVBIlib_List_Obtain_isr);
422        return eErr;
423}
424
425
426/***************************************************************************
427 *
428 */
429BERR_Code BVBIlib_List_Return_isr(
430        BVBIlib_List_Handle    vbillHandle,
431        BVBI_Field_Handle     fieldHandle
432)
433{
434        BVBIlib_P_List_Handle *pVbill;
435        BVBIlib_P_FieldHanger *hanger;
436
437        BDBG_ENTER(BVBIlib_List_Return_isr);
438
439        /* check parameters */
440        BVBILIB_P_GET_LIST_CONTEXT(vbillHandle, pVbill);
441        if(!pVbill)
442        {
443                BDBG_ERR(("Invalid parameter\n"));
444                return BERR_TRACE(BERR_INVALID_PARAMETER);
445        }
446
447        /* Usage check */
448        if (BVBI_Field_Get_UsageCount_isr (fieldHandle) != 0)
449                return BVBIlib_USER_LEAK;
450
451        /* If there are no empty hangers left, then the list is corrupted */
452        hanger = BLST_Q_FIRST (&pVbill->empty_hangers);
453        BDBG_ASSERT (hanger != NULL);
454        BLST_Q_REMOVE (&pVbill->empty_hangers, hanger, link);
455
456        /* Hook up the user's field hanger and file it */
457        hanger->hField = fieldHandle;
458        BLST_Q_INSERT_HEAD (&pVbill->field_contexts, hanger, link);
459
460        /* Adjust statistic */
461        --(pVbill->nInUse);
462
463        BDBG_LEAVE(BVBIlib_List_Return_isr);
464        return BERR_SUCCESS;
465}
466
467
468/***************************************************************************
469 *
470 */
471void BVBIlib_List_IncrementUsage_isr(
472        BVBI_Field_Handle     fieldHandle
473)
474{
475        BDBG_ENTER(BVBIlib_List_IncrementUsage_isr);
476
477        /* Just increment usage count */
478        BVBI_Field_Increment_UsageCount_isr (fieldHandle);
479
480        BDBG_LEAVE(BVBIlib_List_IncrementUsage_isr);
481}
482
483
484/***************************************************************************
485 *
486 */
487BERR_Code BVBIlib_List_DecrementUsage_isr(
488        BVBIlib_List_Handle    vbillHandle,
489        BVBI_Field_Handle     fieldHandle
490)
491{
492        BERR_Code eStatus = BERR_SUCCESS;
493
494        BDBG_ENTER(BVBIlib_List_DecrementUsage_isr);
495
496        /* Decrement usage count */
497        BVBI_Field_Decrement_UsageCount_isr (fieldHandle);
498
499        /* Recycle if usage count is zero */
500        if (BVBI_Field_Get_UsageCount_isr (fieldHandle) == 0)
501        {
502                eStatus = BVBIlib_List_Return_isr (vbillHandle, fieldHandle);
503        }
504
505        BDBG_LEAVE(BVBIlib_List_DecrementUsage_isr);
506        return eStatus;
507}
508
509
510/***************************************************************************
511 *
512 */
513BERR_Code BVBIlib_List_DecrementUsage(
514        BVBIlib_List_Handle    vbillHandle,
515        BVBI_Field_Handle     fieldHandle
516)
517{
518        BERR_Code eStatus;
519
520        BDBG_ENTER(BVBIlib_List_DecrementUsage);
521
522        BKNI_EnterCriticalSection();
523        eStatus = BVBIlib_List_DecrementUsage_isr (vbillHandle, fieldHandle);
524        BKNI_LeaveCriticalSection();
525
526        BDBG_LEAVE(BVBIlib_List_DecrementUsage);
527        return eStatus;
528}
529
530/* End of File */
Note: See TracBrowser for help on using the repository browser.