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: /*****************************************************************************/ 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@1746: /** fp@1746: * Get and SDO entry from an SDO via its subindex. fp@1746: * \return pointer to SDO entry, or NULL. fp@1746: */ fp@1746: fp@1746: ec_sdo_entry_t *ec_sdo_get_entry( fp@1746: ec_sdo_t *sdo, /**< SDO */ fp@1746: uint8_t subindex /**< entry subindex */ fp@1746: ) fp@1746: { fp@1746: ec_sdo_entry_t *entry; fp@1746: fp@1746: list_for_each_entry(entry, &sdo->entries, list) { fp@1746: if (entry->subindex != subindex) continue; fp@1746: return entry; fp@1746: } fp@1746: fp@1746: return NULL; fp@1746: } fp@1746: fp@1746: /*****************************************************************************/ fp@1746: fp@1746: /** fp@1746: * Print SDO information to a buffer. fp@1746: * /return size of bytes written to buffer. fp@1746: */ fp@1746: 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@1746: /** fp@1746: * Show an SDO as Sysfs attribute. fp@1746: * /return size of bytes written to buffer. fp@1746: */ fp@1746: 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@1746: fp@1746: /** fp@1746: * Print SDO entry information to a buffer. fp@1746: * \return number of bytes written. fp@1746: */ 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@1746: /** fp@1746: * Format entry data based on the CANopen data type and print it to a buffer. fp@1746: * \return number of bytes written. fp@1746: */ fp@1746: 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@1744: if (entry->data_type == 0x0002) { // int8 fp@1744: int8_t value; fp@1744: if (entry->bit_length != 8) fp@1744: goto not_fit; fp@1744: value = EC_READ_S8(request->data); fp@1744: off += sprintf(buffer + off, "%i (0x%02X)\n", value, value); fp@1744: } fp@1744: else if (entry->data_type == 0x0003) { // int16 fp@1744: int16_t value; fp@1744: if (entry->bit_length != 16) fp@1744: goto not_fit; fp@1744: value = EC_READ_S16(request->data); fp@1744: off += sprintf(buffer + off, "%i (0x%04X)\n", value, value); fp@1744: } fp@1744: else if (entry->data_type == 0x0004) { // int32 fp@1744: int32_t value; fp@1744: if (entry->bit_length != 32) fp@1744: goto not_fit; fp@1744: value = EC_READ_S16(request->data); fp@1744: off += sprintf(buffer + off, "%i (0x%08X)\n", value, value); fp@1744: } fp@1744: else if (entry->data_type == 0x0005) { // uint8 fp@1744: uint8_t value; fp@1744: if (entry->bit_length != 8) fp@1744: goto not_fit; fp@1744: value = EC_READ_U8(request->data); fp@1744: off += sprintf(buffer + off, "%u (0x%02X)\n", value, value); fp@1744: } fp@1744: else if (entry->data_type == 0x0006) { // uint16 fp@1744: uint16_t value; fp@1744: if (entry->bit_length != 16) fp@1744: goto not_fit; fp@1744: value = EC_READ_U16(request->data); fp@1744: off += sprintf(buffer + off, "%u (0x%04X)\n", value, value); fp@1744: } fp@1744: else if (entry->data_type == 0x0007) { // uint32 fp@1744: uint32_t value; fp@1744: if (entry->bit_length != 32) fp@1744: goto not_fit; fp@1744: value = EC_READ_U32(request->data); fp@1744: off += sprintf(buffer + off, "%i (0x%08X)\n", value, value); 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@1744: off += sprintf(buffer + off, "Unknown data type %04X. Data:\n", fp@1744: entry->data_type); fp@1744: goto raw_data; fp@1744: } fp@1744: return off; fp@1744: fp@1744: not_fit: fp@1744: off += sprintf(buffer + off, fp@1744: "Invalid bit length %u for data type 0x%04X. Data:\n", fp@1744: entry->bit_length, entry->data_type); fp@1744: raw_data: fp@1744: for (i = 0; i < request->size; i++) fp@1744: off += sprintf(buffer + off, "%02X (%c)\n", fp@1744: request->data[i], request->data[i]); fp@1732: return off; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1746: /** fp@1746: * Start SDO entry reading. fp@1746: * This function blocks, until reading is finished, and is interruptible as fp@1746: * long as the master state machine has not begun with reading. fp@1746: * \return number of bytes written to buffer, or error code. fp@1746: */ fp@1746: 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@1746: ec_master_t *master = entry->sdo->slave->master; fp@1732: off_t off = 0; fp@1732: ec_sdo_request_t request; fp@1732: fp@1746: ec_sdo_request_init_read(&request, entry); fp@1744: fp@1744: // schedule request. fp@1744: down(&master->sdo_sem); fp@1744: list_add_tail(&request.list, &master->sdo_requests); fp@1744: up(&master->sdo_sem); fp@1744: fp@1744: // wait for processing through FSM fp@1744: if (wait_event_interruptible(master->sdo_queue, fp@1744: request.state != EC_REQUEST_QUEUED)) { fp@1732: // interrupted by signal fp@1744: down(&master->sdo_sem); fp@1744: if (request.state == EC_REQUEST_QUEUED) { fp@1744: list_del(&request.list); fp@1744: up(&master->sdo_sem); fp@1744: return -EINTR; fp@1744: } fp@1744: // request already processing: interrupt not possible. fp@1744: up(&master->sdo_sem); fp@1744: } fp@1744: fp@1744: // wait until master FSM has finished processing fp@1744: wait_event(master->sdo_queue, request.state != EC_REQUEST_IN_PROGRESS); fp@1744: fp@1744: if (request.state != EC_REQUEST_COMPLETE) fp@1744: return -EIO; fp@1744: fp@1744: off += ec_sdo_entry_format_data(entry, &request, buffer); fp@1732: fp@1732: ec_sdo_request_clear(&request); fp@1732: return off; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1746: /** fp@1746: * Show an SDO entry as Sysfs attribute. fp@1746: * /return size of bytes written to buffer. fp@1746: */ fp@1746: 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_entry_t *entry /**< SDO entry */ fp@1732: ) fp@1732: { fp@1732: req->entry = entry; fp@1732: req->data = NULL; fp@1732: req->size = 0; fp@1744: req->state = EC_REQUEST_QUEUED; 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: /*****************************************************************************/