fp@409: /****************************************************************************** fp@409: * fp@409: * $Id$ fp@409: * fp@409: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@409: * fp@409: * This file is part of the IgH EtherCAT Master. fp@409: * fp@409: * The IgH EtherCAT Master is free software; you can redistribute it fp@409: * and/or modify it under the terms of the GNU General Public License fp@409: * as published by the Free Software Foundation; either version 2 of the fp@409: * License, or (at your option) any later version. fp@409: * fp@409: * The IgH EtherCAT Master is distributed in the hope that it will be fp@409: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@409: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@409: * GNU General Public License for more details. fp@409: * fp@409: * You should have received a copy of the GNU General Public License fp@409: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@409: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@409: * fp@409: * The right to use EtherCAT Technology is granted and comes free of fp@409: * charge under condition of compatibility of product made by fp@409: * Licensee. People intending to distribute/sell products based on the fp@409: * code, have to sign an agreement to guarantee that products using fp@409: * software based on IgH EtherCAT master stay compatible with the actual fp@409: * EtherCAT specification (which are released themselves as an open fp@409: * standard) as the (only) precondition to have the right to use EtherCAT fp@409: * Technology, IP and trade marks. fp@409: * fp@409: *****************************************************************************/ fp@409: fp@409: /** fp@409: \file fp@847: CANopen Sdo functions. fp@409: */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: #include fp@409: fp@430: #include "master.h" fp@409: fp@847: #include "sdo.h" fp@847: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_show_sdo_attribute(struct kobject *, struct attribute *, char *); fp@409: void ec_sdo_clear(struct kobject *); fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** \cond */ fp@409: fp@409: EC_SYSFS_READ_ATTR(info); fp@409: fp@409: static struct attribute *sdo_def_attrs[] = { fp@409: &attr_info, fp@409: NULL, fp@409: }; fp@409: fp@409: static struct sysfs_ops sdo_sysfs_ops = { fp@409: .show = &ec_show_sdo_attribute, fp@409: .store = NULL fp@409: }; fp@409: fp@409: static struct kobj_type ktype_ec_sdo = { fp@409: .release = ec_sdo_clear, fp@409: .sysfs_ops = &sdo_sysfs_ops, fp@409: .default_attrs = sdo_def_attrs fp@409: }; fp@409: fp@409: /** \endcond */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@847: /** Sdo constructor. fp@847: * fp@847: * \todo Turn parameters. fp@847: */ fp@847: int ec_sdo_init( fp@847: ec_sdo_t *sdo, /**< Sdo. */ fp@847: uint16_t index, /**< Sdo index. */ fp@847: ec_slave_t *slave /**< Parent slave. */ fp@847: ) fp@409: { fp@430: sdo->slave = slave; fp@409: sdo->index = index; fp@409: sdo->object_code = 0x00; fp@409: sdo->name = NULL; fp@409: sdo->subindices = 0; fp@409: INIT_LIST_HEAD(&sdo->entries); fp@409: fp@847: // Init kobject and add it to the hierarchy fp@409: memset(&sdo->kobj, 0x00, sizeof(struct kobject)); fp@409: kobject_init(&sdo->kobj); fp@409: sdo->kobj.ktype = &ktype_ec_sdo; fp@419: sdo->kobj.parent = &slave->sdo_kobj; fp@419: if (kobject_set_name(&sdo->kobj, "%4X", sdo->index)) { fp@409: EC_ERR("Failed to set kobj name.\n"); fp@484: kobject_put(&sdo->kobj); fp@484: return -1; fp@484: } fp@484: if (kobject_add(&sdo->kobj)) { fp@814: EC_ERR("Failed to add Sdo kobject.\n"); fp@484: kobject_put(&sdo->kobj); fp@409: return -1; fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@847: /** Sdo destructor. fp@847: * fp@847: * Clears and frees an Sdo object. fp@847: */ fp@847: void ec_sdo_destroy( fp@847: ec_sdo_t *sdo /**< Sdo. */ fp@847: ) fp@448: { fp@409: ec_sdo_entry_t *entry, *next; fp@409: fp@409: // free all entries fp@409: list_for_each_entry_safe(entry, next, &sdo->entries, list) { fp@409: list_del(&entry->list); fp@448: ec_sdo_entry_destroy(entry); fp@448: } fp@448: fp@448: // destroy self fp@448: kobject_del(&sdo->kobj); fp@448: kobject_put(&sdo->kobj); fp@448: } fp@448: fp@448: /*****************************************************************************/ fp@448: fp@847: /** Clear and free Sdo. fp@847: * fp@847: * This method is called by the kobject, fp@847: * once there are no more references to it. fp@847: */ fp@847: void ec_sdo_clear( fp@847: struct kobject *kobj /**< Sdo's kobject. */ fp@847: ) fp@448: { fp@448: ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj); fp@409: fp@409: if (sdo->name) kfree(sdo->name); fp@409: fp@409: kfree(sdo); fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@847: /** Get and Sdo entry from an Sdo via its subindex. fp@847: * fp@847: * \retval >0 Pointer to the requested Sdo entry. fp@847: * \retval NULL Sdo entry not found. fp@847: */ fp@740: ec_sdo_entry_t *ec_sdo_get_entry( fp@847: ec_sdo_t *sdo, /**< Sdo. */ fp@847: uint8_t subindex /**< Entry subindex. */ fp@740: ) fp@740: { fp@740: ec_sdo_entry_t *entry; fp@740: fp@740: list_for_each_entry(entry, &sdo->entries, list) { fp@740: if (entry->subindex != subindex) continue; fp@740: return entry; fp@740: } fp@740: fp@740: return NULL; fp@740: } fp@740: fp@740: /*****************************************************************************/ fp@740: fp@847: /** Print Sdo information to a buffer. fp@847: * fp@758: * /return size of bytes written to buffer. fp@758: */ fp@847: ssize_t ec_sdo_info( fp@847: ec_sdo_t *sdo, /**< Sdo. */ fp@847: char *buffer /**< Target buffer. */ fp@847: ) fp@409: { fp@409: off_t off = 0; fp@409: fp@409: off += sprintf(buffer + off, "Index: 0x%04X\n", sdo->index); fp@409: off += sprintf(buffer + off, "Name: %s\n", sdo->name ? sdo->name : ""); fp@409: off += sprintf(buffer + off, "Subindices: %i\n", sdo->subindices); fp@409: fp@409: return off; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@847: /** Show a Sysfs attribute of an Sdo. fp@847: * fp@847: * /return Number of bytes written to buffer. fp@758: */ fp@847: ssize_t ec_show_sdo_attribute( fp@847: struct kobject *kobj, /**< kobject */ fp@847: struct attribute *attr, /**< Requested attribute. */ fp@847: char *buffer /**< Buffer to write the data in. */ fp@847: ) fp@409: { fp@409: ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj); fp@409: fp@409: if (attr == &attr_info) { fp@409: return ec_sdo_info(sdo, buffer); fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/