/*
 * Copyright 2010 Trident Microsystems (Far East) Ltd. All rights reserved.
 * All prior copyright rights in this work and the accompanying software
 * products transferred to Trident Microsystems (Far East) Ltd. by written
 * agreement.
 */
/*****************************************************************************/
/* Copyright (c) 2010 NXP B.V.  All rights are reserved.                     */
/*                                                                           */
/* Redistribution and use in source and binary forms, with or without        */
/* modification, are permitted provided that the following conditions        */
/* are met:                                                                  */
/*                                                                           */
/* Redistributions of source code must retain the above copyright            */
/* notice, this list of conditions and the following disclaimer.             */
/*                                                                           */
/* Redistributions in binary form must reproduce the above copyright         */
/* notice, this list of conditions and the following disclaimer in the       */
/* documentation and/or other materials provided with the distribution.      */
/*                                                                           */
/* Neither the name of NXP nor the names of its                              */
/* contributors may be used to endorse or promote products derived from      */
/* this software without specific prior written permission.                  */
/*                                                                           */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     */
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          */
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     */
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     */
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE         */
/* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH          */
/* DAMAGE.                                                                   */
/*                                                                           */
/*****************************************************************************/
/**
    @file   phStbKBufMgr_Module.c
    @brief  Kernel Buffer Manager component

    Module specific code for the Kernel Buffer Manager.
    This is totally Linux specific.

    Set your editor for 4 space indentation.
*//*

Rev Date        Author      Comments
--------------------------------------------------------------------------------
1   20061129    burningh    Original
2   20061129    burningh    Add further implementation
3   20061205    steel       Add static allocation for hw buffers
4   20061213    steel       resize buffers for vmsp
5   20060111    burningh    Resize framestores & add parameter
6   20070117    iwillis     Moved firmware to end of memory and increased to 4MB
7   20070125    iwillis     Increased ES buffer to 8MB
8   20070125    iwillis     Updated header comment
9   20070126    neill       Revert back to 4MB ES Buffer
9.1.1 20070213  burningh    Make frame buffers static, and improve prints
10  20070212    barnish     Added AIO buffers
11  20070213    burningh    Merge
12  20070213    barnish     Added audio ADEC buffers for audio decoders
13  20070302    barnish     Added types for audio parser buffers
14  20070504    awest       Added demuxSW buffers
15  20070606    awest       update to demuxsw buffers
13.1.2.1.1 20070608 iwillis Made more parametisable for SD memory savings
13.1.2.1.2 20070611 iwillis Aligned VSECT and VFILT buffers
13.1.3  20070607    awest   add WMV and WMDRMPD diversity
12.1.4  20070611    awest   catchup
13.1.5  20070716    steel   realign for additional framestore
14  20070806    burningh    Add control of MSVD firmware buffer size
15  20070820    barnish     added sbm source buffers
16  20070830    barnish     added 64K audio in buffers
16  20070831    awest       added DMXSW_AUD2 buffer
16.1.2 20070906 steel       increase pts buffer size
16.1.3 20070925 batelaan    add phStbNetDev pool (64K)
17  20071008    batelaan    Refactor for portability.
18  20071127    iwillis     Added configuration of audio ES buffer size
19  20080121    steel       add extra buffers for pvr recording
20  20080205    neill       add support for HD to SD downscale
21  20080414    burningh    Add support for dual decode
22  20080509    griffiths   Add extra buffer for auxiliary demuxer
23  20080619    griffiths   Add extra buffer for alternate directfb allocation
24  20080805    laird       Add a debug buffer region.
25  20080910    batelaan    New buildtime setup mechanism. Rm deinit.
26  20090120    batelaan    Update file copyright/license text
27  20090402    batelaan    Add /proc/stbkbufmgr
--------------------------------------------------------------------------------
    For consistency and standardisation retain the Section Separators.
*/

/**************************************************************
* INCLUDE FILES                                               *
***************************************************************/

#include "phStbKBufMgr_Int.h"

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>

/**************************************************************
* LOCAL MACROS                                                *
***************************************************************/

/* Define this to enable the proc file interface. */
#define PH_KBUFMGR_ENABLE_PROC_FILE

#define PH_KBUFMGR_PROC_ENTRY "stbkbufmgr"

/**************************************************************
* LOCAL TYPEDEFS                                              *
***************************************************************/

/**************************************************************
* STATIC FUNCTION PROTOTYPES                                  *
***************************************************************/

#ifdef PH_KBUFMGR_ENABLE_PROC_FILE
static int StbKBufMgr_ProcRead(char *page, char **start, off_t off,
                               int count, int *eof, void *data);
#endif /* PH_KBUFMGR_ENABLE_PROC_FILE */

/**************************************************************
* STATIC DATA                                                 *
***************************************************************/

MODULE_AUTHOR("Neil Burningham, NXP Semiconductors B.V.");
MODULE_LICENSE("Proprietary");

/**************************************************************
* EXPORTED DATA                                               *
***************************************************************/

/**************************************************************
* FUNCTION IMPLEMENTATION                                     *
***************************************************************/

/** Module initialisation function for the phStbKBufMgr module.
    Also creates the kernelifce proc entry /proc/stbkbufmgr .
 */
static int phStbKBufMgr_Module_Init(void)
{
    tmErrorCode_t status;
#ifdef PH_KBUFMGR_ENABLE_PROC_FILE
    struct proc_dir_entry *procEntry;
#endif /* PH_KBUFMGR_ENABLE_PROC_FILE */

    /* Initialise the manager and kernel interface. */
    status = phStbKBufMgr_Init();
    if (status != TM_OK)
    {
        return -EINVAL;
    }

#ifdef PH_KBUFMGR_ENABLE_PROC_FILE
    procEntry = create_proc_entry(
        PH_KBUFMGR_PROC_ENTRY, 0444, NULL);
    if(procEntry == NULL) {
        return -ENOMEM;
    }

    procEntry->owner = THIS_MODULE;
    procEntry->read_proc = &StbKBufMgr_ProcRead;
#endif /* PH_KBUFMGR_ENABLE_PROC_FILE */

    status = phStbKBufMgr_KI_Init();
    if (status != TM_OK)
    {
        return -EINVAL;
    }

    return 0;
}

module_init(phStbKBufMgr_Module_Init);

#ifdef PH_KBUFMGR_ENABLE_PROC_FILE
/******************************************************************************/
/**
 * Read the /proc file.
 * The format of the proc file is:
 * <pre>
 *   size        align       count   free    name
 * </pre>
 */
static
ssize_t StbKBufMgr_ProcRead(char *page, char **start, off_t off,
                            int count, int *eof, void *data)
{
    int len;
    len = phStbKBufMgr_PrintSummaryTo(page, count);
    return len;
}
#endif /* PH_KBUFMGR_ENABLE_PROC_FILE */

/******************************************************************************/
