master/reg_request.c
author Florian Pose <fp@igh-essen.com>
Wed, 06 Feb 2013 17:25:08 +0100
branchstable-1.5
changeset 2508 6b21b3f88a9a
parent 2443 2c3ccdde3919
child 2521 3d68bb0047a1
permissions -rw-r--r--
Introduced --enable-sii-assign.
/******************************************************************************
 *
 *  $Id$
 *
 *  Copyright (C) 2012  Florian Pose, Ingenieurgemeinschaft IgH
 *
 *  This file is part of the IgH EtherCAT Master.
 *
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License version 2, as
 *  published by the Free Software Foundation.
 *
 *  The IgH EtherCAT Master 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 the IgH EtherCAT Master; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  ---
 *
 *  The license mentioned above concerns the source code only. Using the
 *  EtherCAT technology and brand is only permitted in compliance with the
 *  industrial property and similar rights of Beckhoff Automation GmbH.
 *
 *****************************************************************************/

/** \file
 * Register request functions.
 */

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

#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/slab.h>

#include "reg_request.h"

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

void ec_reg_request_clear_data(ec_reg_request_t *);

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

/** Register request constructor.
 */
int ec_reg_request_init(
        ec_reg_request_t *reg, /**< Register request. */
        size_t size /**< Memory size. */
        )
{
    if (!(reg->data = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
        EC_ERR("Failed to allocate %zu bytes of register memory.\n", size);
        return -ENOMEM;
    }

    INIT_LIST_HEAD(&reg->list);
    reg->mem_size = size;
    memset(reg->data, 0x00, size);
    reg->dir = EC_DIR_INVALID;
    reg->address = 0;
    reg->transfer_size = 0;
    reg->state = EC_INT_REQUEST_INIT;
    return 0;
}

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

/** Register request destructor.
 */
void ec_reg_request_clear(
        ec_reg_request_t *reg /**< Register request. */
        )
{
    if (reg->data) {
        kfree(reg->data);
    }
}

/*****************************************************************************
 * Application interface.
 ****************************************************************************/

uint8_t *ecrt_reg_request_data(ec_reg_request_t *reg)
{
    return reg->data;
}

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

ec_request_state_t ecrt_reg_request_state(const ec_reg_request_t *reg)
{
   return ec_request_state_translation_table[reg->state];
}

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

void ecrt_reg_request_write(ec_reg_request_t *reg, uint16_t address,
        size_t size)
{
    reg->dir = EC_DIR_OUTPUT;
    reg->address = address;
    reg->transfer_size = min(size, reg->mem_size);
    reg->state = EC_INT_REQUEST_QUEUED;
}

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

void ecrt_reg_request_read(ec_reg_request_t *reg, uint16_t address,
        size_t size)
{
    reg->dir = EC_DIR_INPUT;
    reg->address = address;
    reg->transfer_size = min(size, reg->mem_size);
    reg->state = EC_INT_REQUEST_QUEUED;
}

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

/** \cond */

EXPORT_SYMBOL(ecrt_reg_request_data);
EXPORT_SYMBOL(ecrt_reg_request_state);
EXPORT_SYMBOL(ecrt_reg_request_write);
EXPORT_SYMBOL(ecrt_reg_request_read);

/** \endcond */

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