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@409: Canopen-over-EtherCAT functions. fp@409: */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: #include fp@409: fp@409: #include "canopen.h" fp@430: #include "master.h" fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_show_sdo_attribute(struct kobject *, struct attribute *, char *); fp@409: ssize_t ec_show_sdo_entry_attribute(struct kobject *, struct attribute *, fp@409: char *); fp@409: void ec_sdo_clear(struct kobject *); fp@409: void ec_sdo_entry_clear(struct kobject *); fp@409: fp@430: void ec_sdo_request_init_read(ec_sdo_request_t *, ec_sdo_t *, fp@430: ec_sdo_entry_t *); fp@430: void ec_sdo_request_clear(ec_sdo_request_t *); fp@430: fp@409: /*****************************************************************************/ fp@409: fp@409: /** \cond */ fp@409: fp@409: EC_SYSFS_READ_ATTR(info); fp@430: EC_SYSFS_READ_ATTR(value); 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: static struct attribute *sdo_entry_def_attrs[] = { fp@409: &attr_info, fp@430: &attr_value, fp@409: NULL, fp@409: }; fp@409: fp@409: static struct sysfs_ops sdo_entry_sysfs_ops = { fp@409: .show = &ec_show_sdo_entry_attribute, fp@409: .store = NULL fp@409: }; fp@409: fp@409: static struct kobj_type ktype_ec_sdo_entry = { fp@409: .release = ec_sdo_entry_clear, fp@409: .sysfs_ops = &sdo_entry_sysfs_ops, fp@409: .default_attrs = sdo_entry_def_attrs fp@409: }; fp@409: fp@409: /** \endcond */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** fp@409: SDO constructor. fp@409: */ fp@409: fp@409: int ec_sdo_init(ec_sdo_t *sdo, /**< SDO */ fp@409: uint16_t index, /**< SDO index */ fp@409: ec_slave_t *slave /**< parent slave */ fp@409: ) 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@409: // 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@484: 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@409: /** fp@409: SDO destructor. fp@448: Clears and frees an SDO object. fp@448: */ fp@448: fp@448: void ec_sdo_destroy(ec_sdo_t *sdo /**< SDO */) 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@448: /** fp@448: Clear and free SDO. fp@448: This method is called by the kobject, fp@448: once there are no more references to it. fp@448: */ fp@448: fp@448: void ec_sdo_clear(struct kobject *kobj /**< SDO's kobject */) 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@409: ssize_t ec_sdo_info(ec_sdo_t *sdo, /**< SDO */ fp@409: char *buffer /**< target buffer */ fp@409: ) 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@409: ssize_t ec_show_sdo_attribute(struct kobject *kobj, /**< kobject */ fp@409: struct attribute *attr, fp@409: char *buffer fp@409: ) 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: /*****************************************************************************/ fp@409: fp@409: /** fp@409: SDO entry constructor. fp@409: */ fp@409: fp@409: int ec_sdo_entry_init(ec_sdo_entry_t *entry, /**< SDO entry */ fp@409: uint8_t subindex, /**< SDO entry subindex */ fp@409: ec_sdo_t *sdo /**< parent SDO */ fp@409: ) fp@409: { fp@430: entry->sdo = sdo; fp@409: entry->subindex = subindex; fp@409: entry->data_type = 0x0000; fp@409: entry->bit_length = 0; fp@409: entry->description = NULL; fp@409: fp@409: // init kobject and add it to the hierarchy fp@409: memset(&entry->kobj, 0x00, sizeof(struct kobject)); fp@409: kobject_init(&entry->kobj); fp@409: entry->kobj.ktype = &ktype_ec_sdo_entry; fp@409: entry->kobj.parent = &sdo->kobj; fp@419: if (kobject_set_name(&entry->kobj, "%i", entry->subindex)) { fp@409: EC_ERR("Failed to set kobj name.\n"); fp@484: kobject_put(&entry->kobj); fp@484: return -1; fp@484: } fp@484: if (kobject_add(&entry->kobj)) { fp@484: EC_ERR("Failed to add entry kobject.\n"); fp@484: kobject_put(&entry->kobj); fp@409: return -1; fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** fp@448: SDO entry destructor. fp@448: Clears and frees an SDO entry object. fp@448: */ fp@448: fp@448: void ec_sdo_entry_destroy(ec_sdo_entry_t *entry /**< SDO entry */) fp@448: { fp@448: // destroy self fp@448: kobject_del(&entry->kobj); fp@448: kobject_put(&entry->kobj); fp@448: } fp@448: fp@448: /*****************************************************************************/ fp@448: fp@448: /** fp@448: Clear and free SDO entry. fp@448: This method is called by the kobject, fp@448: once there are no more references to it. fp@409: */ fp@409: fp@409: void ec_sdo_entry_clear(struct kobject *kobj /**< SDO entry's kobject */) fp@409: { fp@409: ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj); fp@409: fp@409: if (entry->description) kfree(entry->description); fp@409: fp@409: kfree(entry); fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_sdo_entry_info(ec_sdo_entry_t *entry, /**< SDO entry */ fp@409: char *buffer /**< target buffer */ fp@409: ) fp@409: { fp@409: off_t off = 0; fp@409: fp@409: off += sprintf(buffer + off, "Subindex: 0x%02X\n", entry->subindex); fp@409: off += sprintf(buffer + off, "Description: %s\n", fp@409: entry->description ? entry->description : ""); fp@409: off += sprintf(buffer + off, "Data type: 0x%04X\n", entry->data_type); fp@409: off += sprintf(buffer + off, "Bit length: %i\n", entry->bit_length); fp@409: fp@409: return off; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@430: ssize_t ec_sdo_entry_format_data(ec_sdo_entry_t *entry, /**< SDO entry */ fp@430: ec_sdo_request_t *request, /**< SDO request */ fp@430: char *buffer /**< target buffer */ fp@430: ) fp@430: { fp@430: off_t off = 0; fp@430: unsigned int i; fp@430: fp@430: if (entry->data_type == 0x0002 && entry->bit_length == 8) { // int8 fp@430: off += sprintf(buffer + off, "%i\n", *((int8_t *) request->data)); fp@430: } fp@430: else if (entry->data_type == 0x0003 && entry->bit_length == 16) { // int16 fp@430: off += sprintf(buffer + off, "%i\n", *((int16_t *) request->data)); fp@430: } fp@430: else if (entry->data_type == 0x0004 && entry->bit_length == 32) { // int32 fp@430: off += sprintf(buffer + off, "%i\n", *((int32_t *) request->data)); fp@430: } fp@430: else if (entry->data_type == 0x0005 && entry->bit_length == 8) { // uint8 fp@430: off += sprintf(buffer + off, "%i\n", *((uint8_t *) request->data)); fp@430: } fp@430: else if (entry->data_type == 0x0006 && entry->bit_length == 16) { // uint16 fp@430: off += sprintf(buffer + off, "%i\n", *((uint16_t *) request->data)); fp@430: } fp@430: else if (entry->data_type == 0x0007 && entry->bit_length == 32) { // uint32 fp@430: off += sprintf(buffer + off, "%i\n", *((uint32_t *) request->data)); fp@430: } fp@430: else if (entry->data_type == 0x0009) { // string fp@430: off += sprintf(buffer + off, "%s\n", request->data); fp@430: } fp@430: else { fp@430: for (i = 0; i < request->size; i++) fp@430: off += sprintf(buffer + off, "%02X (%c)\n", fp@430: request->data[i], request->data[i]); fp@430: } fp@430: fp@430: return off; fp@430: } fp@430: fp@430: /*****************************************************************************/ fp@430: fp@430: ssize_t ec_sdo_entry_read_value(ec_sdo_entry_t *entry, /**< SDO entry */ fp@430: char *buffer /**< target buffer */ fp@430: ) fp@430: { fp@430: ec_sdo_t *sdo = entry->sdo; fp@430: ec_master_t *master = sdo->slave->master; fp@430: off_t off = 0; fp@430: ec_sdo_request_t request; fp@430: fp@441: if (down_interruptible(&master->sdo_sem)) { fp@441: // interrupted by signal fp@441: return -ERESTARTSYS; fp@441: } fp@441: fp@430: ec_sdo_request_init_read(&request, sdo, entry); fp@441: fp@441: // this is necessary, because the completion object fp@441: // is completed by the ec_master_flush_sdo_requests() function. fp@441: INIT_COMPLETION(master->sdo_complete); fp@441: fp@441: master->sdo_request = &request; fp@441: master->sdo_seq_user++; fp@441: master->sdo_timer.expires = jiffies + 10; fp@441: add_timer(&master->sdo_timer); fp@441: fp@441: wait_for_completion(&master->sdo_complete); fp@441: fp@441: master->sdo_request = NULL; fp@441: up(&master->sdo_sem); fp@430: fp@430: if (request.return_code == 1 && request.data) { fp@430: off += ec_sdo_entry_format_data(entry, &request, buffer); fp@430: } fp@441: else { fp@430: off = -EINVAL; fp@430: } fp@430: fp@430: ec_sdo_request_clear(&request); fp@430: return off; fp@430: } fp@430: fp@430: /*****************************************************************************/ fp@430: fp@409: ssize_t ec_show_sdo_entry_attribute(struct kobject *kobj, /**< kobject */ fp@409: struct attribute *attr, fp@409: char *buffer fp@409: ) fp@409: { fp@409: ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj); fp@409: fp@409: if (attr == &attr_info) { fp@409: return ec_sdo_entry_info(entry, buffer); fp@409: } fp@430: else if (attr == &attr_value) { fp@430: return ec_sdo_entry_read_value(entry, buffer); fp@430: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@430: /** fp@430: SDO request constructor. fp@430: */ fp@430: fp@430: void ec_sdo_request_init_read(ec_sdo_request_t *req, /**< SDO request */ fp@430: ec_sdo_t *sdo, /**< SDO */ fp@430: ec_sdo_entry_t *entry /**< SDO entry */ fp@430: ) fp@430: { fp@430: req->sdo = sdo; fp@430: req->entry = entry; fp@430: req->data = NULL; fp@430: req->size = 0; fp@430: req->return_code = 0; fp@430: } fp@430: fp@430: /*****************************************************************************/ fp@430: fp@430: /** fp@430: SDO request destructor. fp@430: */ fp@430: fp@430: void ec_sdo_request_clear(ec_sdo_request_t *req /**< SDO request */) fp@430: { fp@430: if (req->data) kfree(req->data); fp@430: } fp@430: fp@430: /*****************************************************************************/