fp@627: /****************************************************************************** fp@627: * fp@627: * $Id$ fp@627: * fp@1326: * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH fp@627: * fp@627: * This file is part of the IgH EtherCAT Master. fp@627: * fp@1326: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1326: * modify it under the terms of the GNU General Public License version 2, as fp@1326: * published by the Free Software Foundation. fp@1326: * fp@1326: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1326: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1326: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1326: * Public License for more details. fp@1326: * fp@1326: * You should have received a copy of the GNU General Public License along fp@1326: * with the IgH EtherCAT Master; if not, write to the Free Software fp@627: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@627: * fp@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@627: * fp@627: *****************************************************************************/ fp@627: fp@627: /** fp@627: \file fp@627: EtherCAT process data object methods. fp@627: */ fp@627: fp@627: /*****************************************************************************/ fp@627: fp@627: #include fp@1313: #include fp@627: fp@627: #include "pdo.h" fp@627: fp@627: /*****************************************************************************/ fp@627: fp@1327: /** PDO constructor. fp@792: */ fp@792: void ec_pdo_init( fp@1327: ec_pdo_t *pdo /**< EtherCAT PDO */ fp@792: ) fp@792: { fp@2421: pdo->sync_index = -1; // not assigned fp@627: pdo->name = NULL; fp@627: INIT_LIST_HEAD(&pdo->entries); fp@627: } fp@627: fp@627: /*****************************************************************************/ fp@627: fp@1327: /** PDO copy constructor. fp@1313: * fp@1313: * \retval 0 Success. fp@1313: * \retval <0 Error code. fp@792: */ fp@1332: int ec_pdo_init_copy( fp@1332: ec_pdo_t *pdo, /**< PDO to create. */ fp@1332: const ec_pdo_t *other_pdo /**< PDO to copy from. */ fp@1332: ) fp@792: { fp@1313: int ret = 0; fp@1313: fp@792: pdo->index = other_pdo->index; fp@792: pdo->sync_index = other_pdo->sync_index; fp@792: pdo->name = NULL; fp@792: INIT_LIST_HEAD(&pdo->entries); fp@792: fp@1313: ret = ec_pdo_set_name(pdo, other_pdo->name); fp@1313: if (ret < 0) fp@792: goto out_return; fp@792: fp@1313: ret = ec_pdo_copy_entries(pdo, other_pdo); fp@1313: if (ret < 0) fp@792: goto out_clear; fp@792: fp@792: return 0; fp@792: fp@792: out_clear: fp@792: ec_pdo_clear(pdo); fp@792: out_return: fp@1313: return ret; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@1327: /** PDO destructor. fp@1327: */ fp@1327: void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT PDO. */) fp@627: { fp@792: if (pdo->name) fp@792: kfree(pdo->name); fp@792: fp@792: ec_pdo_clear_entries(pdo); fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@1327: /** Clear PDO entry list. fp@1327: */ fp@1327: void ec_pdo_clear_entries(ec_pdo_t *pdo /**< EtherCAT PDO. */) fp@792: { fp@627: ec_pdo_entry_t *entry, *next; fp@627: fp@1327: // free all PDO entries fp@627: list_for_each_entry_safe(entry, next, &pdo->entries, list) { fp@627: list_del(&entry->list); fp@792: ec_pdo_entry_clear(entry); fp@627: kfree(entry); fp@627: } fp@627: } fp@627: fp@627: /*****************************************************************************/ fp@627: fp@1327: /** Set PDO name. fp@1313: * fp@1313: * \retval 0 Success. fp@1313: * \retval <0 Error code. fp@792: */ fp@792: int ec_pdo_set_name( fp@1327: ec_pdo_t *pdo, /**< PDO. */ fp@792: const char *name /**< New name. */ fp@792: ) fp@792: { fp@792: unsigned int len; fp@792: fp@1186: if (pdo->name && name && !strcmp(pdo->name, name)) fp@1186: return 0; fp@2421: fp@792: if (pdo->name) fp@792: kfree(pdo->name); fp@792: fp@792: if (name && (len = strlen(name))) { fp@792: if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) { fp@1327: EC_ERR("Failed to allocate PDO name.\n"); fp@1313: return -ENOMEM; fp@792: } fp@792: memcpy(pdo->name, name, len + 1); fp@792: } else { fp@792: pdo->name = NULL; fp@792: } fp@792: fp@792: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@1327: /** Add a new PDO entry to the configuration. fp@1313: * fp@1313: * \retval Pointer to the added entry, otherwise a ERR_PTR() code. fp@842: */ fp@842: ec_pdo_entry_t *ec_pdo_add_entry( fp@1332: ec_pdo_t *pdo, /**< PDO. */ fp@1332: uint16_t index, /**< New entry's index. */ fp@1332: uint8_t subindex, /**< New entry's subindex. */ fp@1332: uint8_t bit_length /**< New entry's bit length. */ fp@842: ) fp@842: { fp@842: ec_pdo_entry_t *entry; fp@842: fp@842: if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { fp@1327: EC_ERR("Failed to allocate memory for PDO entry.\n"); fp@1313: return ERR_PTR(-ENOMEM); fp@842: } fp@842: fp@842: ec_pdo_entry_init(entry); fp@842: entry->index = index; fp@842: entry->subindex = subindex; fp@842: entry->bit_length = bit_length; fp@842: list_add_tail(&entry->list, &pdo->entries); fp@842: return entry; fp@842: } fp@842: fp@842: /*****************************************************************************/ fp@842: fp@1327: /** Copy PDO entries from another PDO. fp@1313: * fp@1313: * \retval 0 Success. fp@1313: * \retval <0 Error code. fp@792: */ fp@1332: int ec_pdo_copy_entries( fp@1332: ec_pdo_t *pdo, /**< PDO whos entries shall be replaced. */ fp@1332: const ec_pdo_t *other /**< Pdo with entries to copy. */ fp@1332: ) fp@792: { fp@792: ec_pdo_entry_t *entry, *other_entry; fp@1313: int ret; fp@792: fp@792: ec_pdo_clear_entries(pdo); fp@792: fp@792: list_for_each_entry(other_entry, &other->entries, list) { fp@627: if (!(entry = (ec_pdo_entry_t *) fp@627: kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { fp@1327: EC_ERR("Failed to allocate memory for PDO entry copy.\n"); fp@1313: return -ENOMEM; fp@792: } fp@792: fp@1313: ret = ec_pdo_entry_init_copy(entry, other_entry); fp@1313: if (ret < 0) { fp@792: kfree(entry); fp@1313: return ret; fp@792: } fp@792: fp@627: list_add_tail(&entry->list, &pdo->entries); fp@627: } fp@627: fp@627: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@1327: /** Compares the entries of two PDOs. fp@1327: * fp@1327: * \retval 1 The entries of the given PDOs are equal. fp@1327: * \retval 0 The entries of the given PDOs differ. fp@799: */ fp@799: int ec_pdo_equal_entries( fp@1327: const ec_pdo_t *pdo1, /**< First PDO. */ fp@1327: const ec_pdo_t *pdo2 /**< Second PDO. */ fp@799: ) fp@799: { fp@799: const struct list_head *head1, *head2, *item1, *item2; fp@799: const ec_pdo_entry_t *entry1, *entry2; fp@799: fp@799: head1 = item1 = &pdo1->entries; fp@799: head2 = item2 = &pdo2->entries; fp@799: fp@799: while (1) { fp@799: item1 = item1->next; fp@799: item2 = item2->next; fp@799: fp@799: if ((item1 == head1) ^ (item2 == head2)) // unequal lengths fp@799: return 0; fp@826: if (item1 == head1) // both finished fp@799: break; fp@799: fp@799: entry1 = list_entry(item1, ec_pdo_entry_t, list); fp@799: entry2 = list_entry(item2, ec_pdo_entry_t, list); fp@799: if (!ec_pdo_entry_equal(entry1, entry2)) fp@799: return 0; fp@799: } fp@799: fp@799: return 1; fp@799: } fp@799: fp@799: /*****************************************************************************/ fp@934: fp@1327: /** Get the number of PDO entries. fp@1327: * fp@1327: * \return Number of PDO entries. fp@934: */ fp@934: unsigned int ec_pdo_entry_count( fp@1327: const ec_pdo_t *pdo /**< PDO. */ fp@934: ) fp@934: { fp@934: const ec_pdo_entry_t *entry; fp@934: unsigned int num = 0; fp@934: fp@934: list_for_each_entry(entry, &pdo->entries, list) { fp@934: num++; fp@934: } fp@934: fp@934: return num; fp@934: } fp@934: fp@934: /*****************************************************************************/ fp@934: fp@1327: /** Finds a PDO entry via its position in the list. fp@934: * fp@934: * Const version. fp@2522: * fp@2522: * \return Search result, or NULL. fp@934: */ fp@934: const ec_pdo_entry_t *ec_pdo_find_entry_by_pos_const( fp@1327: const ec_pdo_t *pdo, /**< PDO. */ fp@934: unsigned int pos /**< Position in the list. */ fp@934: ) fp@934: { fp@934: const ec_pdo_entry_t *entry; fp@934: fp@934: list_for_each_entry(entry, &pdo->entries, list) { fp@934: if (pos--) fp@934: continue; fp@934: return entry; fp@934: } fp@934: fp@934: return NULL; fp@934: } fp@934: fp@934: /*****************************************************************************/ fp@1823: fp@1823: /** Outputs the PDOs in the list. fp@1823: */ fp@1823: void ec_pdo_print_entries( fp@1823: const ec_pdo_t *pdo /**< PDO. */ fp@1823: ) fp@1823: { fp@1823: const ec_pdo_entry_t *entry; fp@1823: fp@1823: if (list_empty(&pdo->entries)) { fp@1823: printk("(none)"); fp@1823: } else { fp@1823: list_for_each_entry(entry, &pdo->entries, list) { fp@1823: printk("0x%04X:%02X/%u", fp@1823: entry->index, entry->subindex, entry->bit_length); fp@1823: if (entry->list.next != &pdo->entries) fp@1823: printk(" "); fp@1823: } fp@1823: } fp@1823: } fp@1823: fp@1823: /*****************************************************************************/