fp@2589: /****************************************************************************** fp@2589: * fp@2589: * $Id$ fp@2589: * fp@2589: * Copyright (C) 2012 Florian Pose, Ingenieurgemeinschaft IgH fp@2589: * fp@2589: * This file is part of the IgH EtherCAT Master. fp@2589: * fp@2589: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@2589: * modify it under the terms of the GNU General Public License version 2, as fp@2589: * published by the Free Software Foundation. fp@2589: * fp@2589: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@2589: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@2589: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@2589: * Public License for more details. fp@2589: * fp@2589: * You should have received a copy of the GNU General Public License along fp@2589: * with the IgH EtherCAT Master; if not, write to the Free Software fp@2589: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@2589: * fp@2589: * --- fp@2589: * fp@2589: * The license mentioned above concerns the source code only. Using the fp@2589: * EtherCAT technology and brand is only permitted in compliance with the fp@2589: * industrial property and similar rights of Beckhoff Automation GmbH. fp@2589: * fp@2589: * vim: expandtab fp@2589: * fp@2589: *****************************************************************************/ fp@2589: fp@2589: /** \file fp@2589: * EtherCAT CoE emergency ring buffer methods. fp@2589: */ fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: #include fp@2589: fp@2589: #include "coe_emerg_ring.h" fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Emergency ring buffer constructor. fp@2589: */ fp@2589: void ec_coe_emerg_ring_init( fp@2589: ec_coe_emerg_ring_t *ring, /**< Emergency ring. */ fp@2589: ec_slave_config_t *sc /**< Slave configuration. */ fp@2589: ) fp@2589: { fp@2589: ring->sc = sc; fp@2589: ring->msgs = NULL; fp@2589: ring->size = 0; fp@2589: ring->read_index = 0; fp@2589: ring->write_index = 0; fp@2589: ring->overruns = 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Emergency ring buffer destructor. fp@2589: */ fp@2589: void ec_coe_emerg_ring_clear( fp@2589: ec_coe_emerg_ring_t *ring /**< Emergency ring. */ fp@2589: ) fp@2589: { fp@2589: if (ring->msgs) { fp@2589: kfree(ring->msgs); fp@2589: } fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Set the ring size. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: int ec_coe_emerg_ring_size( fp@2589: ec_coe_emerg_ring_t *ring, /**< Emergency ring. */ fp@2589: size_t size /**< Maximum number of messages in the ring. */ fp@2589: ) fp@2589: { fp@2589: ring->size = 0; fp@2589: fp@2589: if (size < 0) { fp@2589: size = 0; fp@2589: } fp@2589: fp@2589: ring->read_index = ring->write_index = 0; fp@2589: fp@2589: if (ring->msgs) { fp@2589: kfree(ring->msgs); fp@2589: } fp@2589: ring->msgs = NULL; fp@2589: fp@2589: if (size == 0) { fp@2589: return 0; fp@2589: } fp@2589: fp@2589: ring->msgs = kmalloc(sizeof(ec_coe_emerg_msg_t) * (size + 1), GFP_KERNEL); fp@2589: if (!ring->msgs) { fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: ring->size = size; fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Add a new emergency message. fp@2589: */ fp@2589: void ec_coe_emerg_ring_push( fp@2589: ec_coe_emerg_ring_t *ring, /**< Emergency ring. */ fp@2589: const u8 *msg /**< Emergency message. */ fp@2589: ) fp@2589: { fp@2589: if (!ring->size || fp@2589: (ring->write_index + 1) % (ring->size + 1) == ring->read_index) { fp@2589: ring->overruns++; fp@2589: return; fp@2589: } fp@2589: fp@2589: memcpy(ring->msgs[ring->write_index].data, msg, fp@2589: EC_COE_EMERGENCY_MSG_SIZE); fp@2589: ring->write_index = (ring->write_index + 1) % (ring->size + 1); fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Remove an emergency message from the ring. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: int ec_coe_emerg_ring_pop( fp@2589: ec_coe_emerg_ring_t *ring, /**< Emergency ring. */ fp@2589: u8 *msg /**< Memory to store the emergency message. */ fp@2589: ) fp@2589: { fp@2589: if (ring->read_index == ring->write_index) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: memcpy(msg, ring->msgs[ring->read_index].data, EC_COE_EMERGENCY_MSG_SIZE); fp@2589: ring->read_index = (ring->read_index + 1) % (ring->size + 1); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Clear the ring. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: int ec_coe_emerg_ring_clear_ring( fp@2589: ec_coe_emerg_ring_t *ring /**< Emergency ring. */ fp@2589: ) fp@2589: { fp@2589: ring->read_index = ring->write_index; fp@2589: ring->overruns = 0; fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read the number of overruns. fp@2589: * fp@2589: * \return Number of overruns. fp@2589: */ fp@2589: int ec_coe_emerg_ring_overruns( fp@2589: ec_coe_emerg_ring_t *ring /**< Emergency ring. */ fp@2589: ) fp@2589: { fp@2589: return ring->overruns; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/