fp@1732: /****************************************************************************** fp@1732: * fp@1732: * $Id$ fp@1732: * fp@1732: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1732: * fp@1732: * This file is part of the IgH EtherCAT Master. fp@1732: * fp@1732: * The IgH EtherCAT Master is free software; you can redistribute it fp@1732: * and/or modify it under the terms of the GNU General Public License fp@1732: * as published by the Free Software Foundation; either version 2 of the fp@1732: * License, or (at your option) any later version. fp@1732: * fp@1732: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1732: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1732: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1732: * GNU General Public License for more details. fp@1732: * fp@1732: * You should have received a copy of the GNU General Public License fp@1732: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1732: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1732: * fp@1732: * The right to use EtherCAT Technology is granted and comes free of fp@1732: * charge under condition of compatibility of product made by fp@1732: * Licensee. People intending to distribute/sell products based on the fp@1732: * code, have to sign an agreement to guarantee that products using fp@1732: * software based on IgH EtherCAT master stay compatible with the actual fp@1732: * EtherCAT specification (which are released themselves as an open fp@1732: * standard) as the (only) precondition to have the right to use EtherCAT fp@1732: * Technology, IP and trade marks. fp@1732: * fp@1732: *****************************************************************************/ fp@1732: fp@1732: /** fp@1732: \file fp@1732: Canopen-over-EtherCAT functions. fp@1732: */ fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: #include fp@1732: fp@1732: #include "canopen.h" fp@1732: #include "master.h" fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_show_sdo_attribute(struct kobject *, struct attribute *, char *); fp@1732: ssize_t ec_show_sdo_entry_attribute(struct kobject *, struct attribute *, fp@1732: char *); fp@1732: void ec_sdo_clear(struct kobject *); fp@1732: void ec_sdo_entry_clear(struct kobject *); fp@1732: fp@1732: void ec_sdo_request_init_read(ec_sdo_request_t *, ec_sdo_t *, fp@1732: ec_sdo_entry_t *); fp@1732: void ec_sdo_request_clear(ec_sdo_request_t *); fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** \cond */ fp@1732: fp@1732: EC_SYSFS_READ_ATTR(info); fp@1732: EC_SYSFS_READ_ATTR(value); fp@1732: fp@1732: static struct attribute *sdo_def_attrs[] = { fp@1732: &attr_info, fp@1732: NULL, fp@1732: }; fp@1732: fp@1732: static struct sysfs_ops sdo_sysfs_ops = { fp@1732: .show = &ec_show_sdo_attribute, fp@1732: .store = NULL fp@1732: }; fp@1732: fp@1732: static struct kobj_type ktype_ec_sdo = { fp@1732: .release = ec_sdo_clear, fp@1732: .sysfs_ops = &sdo_sysfs_ops, fp@1732: .default_attrs = sdo_def_attrs fp@1732: }; fp@1732: fp@1732: static struct attribute *sdo_entry_def_attrs[] = { fp@1732: &attr_info, fp@1732: &attr_value, fp@1732: NULL, fp@1732: }; fp@1732: fp@1732: static struct sysfs_ops sdo_entry_sysfs_ops = { fp@1732: .show = &ec_show_sdo_entry_attribute, fp@1732: .store = NULL fp@1732: }; fp@1732: fp@1732: static struct kobj_type ktype_ec_sdo_entry = { fp@1732: .release = ec_sdo_entry_clear, fp@1732: .sysfs_ops = &sdo_entry_sysfs_ops, fp@1732: .default_attrs = sdo_entry_def_attrs fp@1732: }; fp@1732: fp@1732: /** \endcond */ fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: SDO constructor. fp@1732: */ fp@1732: fp@1732: int ec_sdo_init(ec_sdo_t *sdo, /**< SDO */ fp@1732: uint16_t index, /**< SDO index */ fp@1732: ec_slave_t *slave /**< parent slave */ fp@1732: ) fp@1732: { fp@1732: sdo->slave = slave; fp@1732: sdo->index = index; fp@1732: sdo->object_code = 0x00; fp@1732: sdo->name = NULL; fp@1732: sdo->subindices = 0; fp@1732: INIT_LIST_HEAD(&sdo->entries); fp@1732: fp@1732: // init kobject and add it to the hierarchy fp@1732: memset(&sdo->kobj, 0x00, sizeof(struct kobject)); fp@1732: kobject_init(&sdo->kobj); fp@1732: sdo->kobj.ktype = &ktype_ec_sdo; fp@1732: sdo->kobj.parent = &slave->sdo_kobj; fp@1732: if (kobject_set_name(&sdo->kobj, "%4X", sdo->index)) { fp@1732: EC_ERR("Failed to set kobj name.\n"); fp@1732: kobject_put(&sdo->kobj); fp@1732: return -1; fp@1732: } fp@1732: if (kobject_add(&sdo->kobj)) { fp@1732: EC_ERR("Failed to add SDO kobject.\n"); fp@1732: kobject_put(&sdo->kobj); fp@1732: return -1; fp@1732: } fp@1732: fp@1732: return 0; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: SDO destructor. fp@1732: Clears and frees an SDO object. fp@1732: */ fp@1732: fp@1732: void ec_sdo_destroy(ec_sdo_t *sdo /**< SDO */) fp@1732: { fp@1732: ec_sdo_entry_t *entry, *next; fp@1732: fp@1732: // free all entries fp@1732: list_for_each_entry_safe(entry, next, &sdo->entries, list) { fp@1732: list_del(&entry->list); fp@1732: ec_sdo_entry_destroy(entry); fp@1732: } fp@1732: fp@1732: // destroy self fp@1732: kobject_del(&sdo->kobj); fp@1732: kobject_put(&sdo->kobj); fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: Clear and free SDO. fp@1732: This method is called by the kobject, fp@1732: once there are no more references to it. fp@1732: */ fp@1732: fp@1732: void ec_sdo_clear(struct kobject *kobj /**< SDO's kobject */) fp@1732: { fp@1732: ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj); fp@1732: fp@1732: if (sdo->name) kfree(sdo->name); fp@1732: fp@1732: kfree(sdo); fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_sdo_info(ec_sdo_t *sdo, /**< SDO */ fp@1732: char *buffer /**< target buffer */ fp@1732: ) fp@1732: { fp@1732: off_t off = 0; fp@1732: fp@1732: off += sprintf(buffer + off, "Index: 0x%04X\n", sdo->index); fp@1732: off += sprintf(buffer + off, "Name: %s\n", sdo->name ? sdo->name : ""); fp@1732: off += sprintf(buffer + off, "Subindices: %i\n", sdo->subindices); fp@1732: fp@1732: return off; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_show_sdo_attribute(struct kobject *kobj, /**< kobject */ fp@1732: struct attribute *attr, fp@1732: char *buffer fp@1732: ) fp@1732: { fp@1732: ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj); fp@1732: fp@1732: if (attr == &attr_info) { fp@1732: return ec_sdo_info(sdo, buffer); fp@1732: } fp@1732: fp@1732: return 0; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: SDO entry constructor. fp@1732: */ fp@1732: fp@1732: int ec_sdo_entry_init(ec_sdo_entry_t *entry, /**< SDO entry */ fp@1732: uint8_t subindex, /**< SDO entry subindex */ fp@1732: ec_sdo_t *sdo /**< parent SDO */ fp@1732: ) fp@1732: { fp@1732: entry->sdo = sdo; fp@1732: entry->subindex = subindex; fp@1732: entry->data_type = 0x0000; fp@1732: entry->bit_length = 0; fp@1732: entry->description = NULL; fp@1732: fp@1732: // init kobject and add it to the hierarchy fp@1732: memset(&entry->kobj, 0x00, sizeof(struct kobject)); fp@1732: kobject_init(&entry->kobj); fp@1732: entry->kobj.ktype = &ktype_ec_sdo_entry; fp@1732: entry->kobj.parent = &sdo->kobj; fp@1732: if (kobject_set_name(&entry->kobj, "%i", entry->subindex)) { fp@1732: EC_ERR("Failed to set kobj name.\n"); fp@1732: kobject_put(&entry->kobj); fp@1732: return -1; fp@1732: } fp@1732: if (kobject_add(&entry->kobj)) { fp@1732: EC_ERR("Failed to add entry kobject.\n"); fp@1732: kobject_put(&entry->kobj); fp@1732: return -1; fp@1732: } fp@1732: fp@1732: return 0; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: SDO entry destructor. fp@1732: Clears and frees an SDO entry object. fp@1732: */ fp@1732: fp@1732: void ec_sdo_entry_destroy(ec_sdo_entry_t *entry /**< SDO entry */) fp@1732: { fp@1732: // destroy self fp@1732: kobject_del(&entry->kobj); fp@1732: kobject_put(&entry->kobj); fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: Clear and free SDO entry. fp@1732: This method is called by the kobject, fp@1732: once there are no more references to it. fp@1732: */ fp@1732: fp@1732: void ec_sdo_entry_clear(struct kobject *kobj /**< SDO entry's kobject */) fp@1732: { fp@1732: ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj); fp@1732: fp@1732: if (entry->description) kfree(entry->description); fp@1732: fp@1732: kfree(entry); fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_sdo_entry_info(ec_sdo_entry_t *entry, /**< SDO entry */ fp@1732: char *buffer /**< target buffer */ fp@1732: ) fp@1732: { fp@1732: off_t off = 0; fp@1732: fp@1732: off += sprintf(buffer + off, "Subindex: 0x%02X\n", entry->subindex); fp@1732: off += sprintf(buffer + off, "Description: %s\n", fp@1732: entry->description ? entry->description : ""); fp@1732: off += sprintf(buffer + off, "Data type: 0x%04X\n", entry->data_type); fp@1732: off += sprintf(buffer + off, "Bit length: %i\n", entry->bit_length); fp@1732: fp@1732: return off; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_sdo_entry_format_data(ec_sdo_entry_t *entry, /**< SDO entry */ fp@1732: ec_sdo_request_t *request, /**< SDO request */ fp@1732: char *buffer /**< target buffer */ fp@1732: ) fp@1732: { fp@1732: off_t off = 0; fp@1732: unsigned int i; fp@1732: fp@1732: if (entry->data_type == 0x0002 && entry->bit_length == 8) { // int8 fp@1732: off += sprintf(buffer + off, "%i\n", *((int8_t *) request->data)); fp@1732: } fp@1732: else if (entry->data_type == 0x0003 && entry->bit_length == 16) { // int16 fp@1732: off += sprintf(buffer + off, "%i\n", *((int16_t *) request->data)); fp@1732: } fp@1732: else if (entry->data_type == 0x0004 && entry->bit_length == 32) { // int32 fp@1732: off += sprintf(buffer + off, "%i\n", *((int32_t *) request->data)); fp@1732: } fp@1732: else if (entry->data_type == 0x0005 && entry->bit_length == 8) { // uint8 fp@1732: off += sprintf(buffer + off, "%i\n", *((uint8_t *) request->data)); fp@1732: } fp@1732: else if (entry->data_type == 0x0006 && entry->bit_length == 16) { // uint16 fp@1732: off += sprintf(buffer + off, "%i\n", *((uint16_t *) request->data)); fp@1732: } fp@1732: else if (entry->data_type == 0x0007 && entry->bit_length == 32) { // uint32 fp@1732: off += sprintf(buffer + off, "%i\n", *((uint32_t *) request->data)); fp@1732: } fp@1732: else if (entry->data_type == 0x0009) { // string fp@1732: off += sprintf(buffer + off, "%s\n", request->data); fp@1732: } fp@1732: else { fp@1732: for (i = 0; i < request->size; i++) fp@1732: off += sprintf(buffer + off, "%02X (%c)\n", fp@1732: request->data[i], request->data[i]); fp@1732: } fp@1732: fp@1732: return off; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_sdo_entry_read_value(ec_sdo_entry_t *entry, /**< SDO entry */ fp@1732: char *buffer /**< target buffer */ fp@1732: ) fp@1732: { fp@1732: ec_sdo_t *sdo = entry->sdo; fp@1732: ec_master_t *master = sdo->slave->master; fp@1732: off_t off = 0; fp@1732: ec_sdo_request_t request; fp@1732: fp@1732: if (down_interruptible(&master->sdo_sem)) { fp@1732: // interrupted by signal fp@1732: return -ERESTARTSYS; fp@1732: } fp@1732: fp@1732: ec_sdo_request_init_read(&request, sdo, entry); fp@1732: fp@1732: // this is necessary, because the completion object fp@1732: // is completed by the ec_master_flush_sdo_requests() function. fp@1732: INIT_COMPLETION(master->sdo_complete); fp@1732: fp@1732: master->sdo_request = &request; fp@1732: master->sdo_seq_user++; fp@1732: master->sdo_timer.expires = jiffies + 10; fp@1732: add_timer(&master->sdo_timer); fp@1732: fp@1732: wait_for_completion(&master->sdo_complete); fp@1732: fp@1732: master->sdo_request = NULL; fp@1732: up(&master->sdo_sem); fp@1732: fp@1732: if (request.return_code == 1 && request.data) { fp@1732: off += ec_sdo_entry_format_data(entry, &request, buffer); fp@1732: } fp@1732: else { fp@1732: off = -EINVAL; fp@1732: } fp@1732: fp@1732: ec_sdo_request_clear(&request); fp@1732: return off; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: ssize_t ec_show_sdo_entry_attribute(struct kobject *kobj, /**< kobject */ fp@1732: struct attribute *attr, fp@1732: char *buffer fp@1732: ) fp@1732: { fp@1732: ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj); fp@1732: fp@1732: if (attr == &attr_info) { fp@1732: return ec_sdo_entry_info(entry, buffer); fp@1732: } fp@1732: else if (attr == &attr_value) { fp@1732: return ec_sdo_entry_read_value(entry, buffer); fp@1732: } fp@1732: fp@1732: return 0; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: SDO request constructor. fp@1732: */ fp@1732: fp@1732: void ec_sdo_request_init_read(ec_sdo_request_t *req, /**< SDO request */ fp@1732: ec_sdo_t *sdo, /**< SDO */ fp@1732: ec_sdo_entry_t *entry /**< SDO entry */ fp@1732: ) fp@1732: { fp@1732: req->sdo = sdo; fp@1732: req->entry = entry; fp@1732: req->data = NULL; fp@1732: req->size = 0; fp@1732: req->return_code = 0; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: SDO request destructor. fp@1732: */ fp@1732: fp@1732: void ec_sdo_request_clear(ec_sdo_request_t *req /**< SDO request */) fp@1732: { fp@1732: if (req->data) kfree(req->data); fp@1732: } fp@1732: fp@1732: /*****************************************************************************/