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