/*****************************************************************************/
/* Copyright (c) 2009 NXP B.V.                                               */
/*                                                                           */
/* This program is free software; you can redistribute it and/or modify      */
/* it under the terms of the GNU General Public License as published by      */
/* the Free Software Foundation, using version 2 of the License.             */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              */
/* GNU General Public License for more details.                              */
/*                                                                           */
/* You should have received a copy of the GNU General Public License         */
/* along with this program; if not, write to the Free Software               */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307       */
/* USA.                                                                      */
/*                                                                           */
/*****************************************************************************/

/**
    @file   phStbDemux_ca.c
    @brief  DVB adapter driver.

    Based on the bt8xx driver by Florian Schirmer <jolt@tuxbox.org>

    Set your editor for 4 space indentation.
*//*
Rev Date        Author      Comments
--------------------------------------------------------------------------------
  6 20090120    batelaan    Update file copyright/license text

--------------------------------------------------------------------------------
    For consistency and standardisation retain the Section Separators.
*/
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/timer.h>
#include <linux/poll.h>
#include <linux/byteorder/swabb.h>
#include <linux/smp_lock.h>
#include <linux/dvb/ca.h>
#include <gpio.h>

#include "dvbdev.h"
#include "dvb_ci_phys.h"
#include "phStbDemux_common.h"
#include "phStbDemux_dvb.h"
#include "phStbDemux_ca.h"
#include "prjconfig.h"

static int debug;

#ifdef CONFIG_COMMON_INTERFACE_PRESENT
static struct dvb_ci * ci;
#define CI_SLOT_MAX 1
#endif

/*------------------------------------------------------------------------------
 * public functions
 *----------------------------------------------------------------------------*/
int
phStbDemux_ca_register(struct dvb_adapter *adapter, 
                       struct dvb_device **ca, 
                       struct dvb_phStb_card *card)
{
#ifdef CONFIG_COMMON_INTERFACE_PRESENT
    if(ci){
        printk("CI already registered\n");
        return 0;
    }
    dprintk("%s\n", __FUNCTION__);

    ci = kmalloc(sizeof(struct dvb_ci), GFP_KERNEL);
    if(!ci){
        printk("phStbCa init failed: no enough memory\n");
        return -1;
    }

    ci->owner = card->dvb_adapter.module;

    dvb_ci_phys_init(&(card->dvb_adapter),
                     ci,
                     CI_SLOT_MAX);

#else
    (void)adapter;
    (void)ca;
    (void)card;
#endif
    return 0;
}

void phStbDemux_ca_unregister(struct dvb_device *ca)
{
#ifdef CONFIG_COMMON_INTERFACE_PRESENT
    dprintk("%s\n", __FUNCTION__);

    dvb_ci_phys_release();

    if(ci){
        kfree(ci);
    }
#else
    (void)ca;
#endif
}

void phStbDemux_ca_attach(struct dvb_ci * cmds)
{
#ifdef CONFIG_COMMON_INTERFACE_PRESENT
    int i;

    if(ci == NULL){
        printk("Error!!!!!!!, phStbDemux_ca_attach called before register\n");
    }
    ci->reset = cmds->reset;
    ci->detect = cmds->detect;
    ci->bypass = cmds->bypass;
    ci->memory_switch = cmds->memory_switch;
    ci->attr_read = cmds->attr_read;
    ci->attr_write = cmds->attr_write;
    ci->io_read = cmds->io_read;
    ci->io_write = cmds->io_write;

    for(i=0; i<CI_SLOT_MAX; i++){
        ci->bypass(i, DVB_CI_BYPASS);
    }
#else
    (void)cmds;
#endif
}

EXPORT_SYMBOL(phStbDemux_ca_attach);
EXPORT_SYMBOL(phStbDemux_ca_register);
EXPORT_SYMBOL(phStbDemux_ca_unregister);

