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-over-EtherCAT Sdo entry functions. fp@409: */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: #include fp@409: fp@847: #include "sdo.h" fp@847: #include "sdo_request.h" fp@430: #include "master.h" fp@409: fp@847: #include "sdo_entry.h" fp@847: fp@847: /*****************************************************************************/ fp@847: fp@409: ssize_t ec_show_sdo_entry_attribute(struct kobject *, struct attribute *, fp@409: char *); fp@409: void ec_sdo_entry_clear(struct kobject *); fp@409: 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_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@847: /** Sdo entry constructor. fp@847: * fp@847: * \todo Turn parameters. fp@847: */ fp@847: int ec_sdo_entry_init( fp@847: ec_sdo_entry_t *entry, /**< Sdo entry. */ fp@847: uint8_t subindex, /**< Subindex. */ fp@847: ec_sdo_t *sdo /**< Parent Sdo. */ fp@847: ) 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@847: // 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@847: /** Sdo entry destructor. fp@847: * fp@847: * Clears and frees an Sdo entry object. fp@847: */ fp@847: void ec_sdo_entry_destroy( fp@847: ec_sdo_entry_t *entry /**< Sdo entry. */ fp@847: ) 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@847: /** Clear and free the Sdo entry. 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_entry_clear( fp@847: struct kobject *kobj /**< Sdo entry's kobject. */ fp@847: ) 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@758: fp@847: /** Print Sdo entry information to a buffer. fp@847: * fp@847: * \return Number of bytes written. fp@847: */ fp@847: ssize_t ec_sdo_entry_info( fp@847: ec_sdo_entry_t *entry, /**< Sdo entry. */ fp@847: char *buffer /**< Target buffer. */ fp@847: ) 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@847: /** Format entry data based on the CANopen data type and print it to a buffer. fp@847: * fp@758: * \return number of bytes written. fp@758: */ fp@847: ssize_t ec_sdo_entry_format_data( fp@847: ec_sdo_entry_t *entry, /**< Sdo entry. */ fp@847: ec_sdo_request_t *request, /**< Sdo request. */ fp@847: char *buffer /**< Target buffer. */ fp@847: ) fp@430: { fp@430: off_t off = 0; fp@430: unsigned int i; fp@430: fp@657: if (entry->data_type == 0x0002) { // int8 fp@657: int8_t value; fp@657: if (entry->bit_length != 8) fp@657: goto not_fit; fp@657: value = EC_READ_S8(request->data); fp@657: off += sprintf(buffer + off, "%i (0x%02X)\n", value, value); fp@657: } fp@657: else if (entry->data_type == 0x0003) { // int16 fp@657: int16_t value; fp@657: if (entry->bit_length != 16) fp@657: goto not_fit; fp@657: value = EC_READ_S16(request->data); fp@657: off += sprintf(buffer + off, "%i (0x%04X)\n", value, value); fp@657: } fp@657: else if (entry->data_type == 0x0004) { // int32 fp@657: int32_t value; fp@657: if (entry->bit_length != 32) fp@657: goto not_fit; fp@657: value = EC_READ_S16(request->data); fp@657: off += sprintf(buffer + off, "%i (0x%08X)\n", value, value); fp@657: } fp@657: else if (entry->data_type == 0x0005) { // uint8 fp@657: uint8_t value; fp@657: if (entry->bit_length != 8) fp@657: goto not_fit; fp@657: value = EC_READ_U8(request->data); fp@657: off += sprintf(buffer + off, "%u (0x%02X)\n", value, value); fp@657: } fp@657: else if (entry->data_type == 0x0006) { // uint16 fp@657: uint16_t value; fp@657: if (entry->bit_length != 16) fp@657: goto not_fit; fp@657: value = EC_READ_U16(request->data); fp@657: off += sprintf(buffer + off, "%u (0x%04X)\n", value, value); fp@657: } fp@657: else if (entry->data_type == 0x0007) { // uint32 fp@657: uint32_t value; fp@657: if (entry->bit_length != 32) fp@657: goto not_fit; fp@657: value = EC_READ_U32(request->data); fp@657: off += sprintf(buffer + off, "%i (0x%08X)\n", value, value); 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@657: off += sprintf(buffer + off, "Unknown data type %04X. Data:\n", fp@657: entry->data_type); fp@657: goto raw_data; fp@657: } fp@657: return off; fp@657: fp@657: not_fit: fp@657: off += sprintf(buffer + off, fp@657: "Invalid bit length %u for data type 0x%04X. Data:\n", fp@657: entry->bit_length, entry->data_type); fp@657: raw_data: fp@854: for (i = 0; i < request->data_size; i++) fp@657: off += sprintf(buffer + off, "%02X (%c)\n", fp@657: request->data[i], request->data[i]); fp@430: return off; fp@430: } fp@430: fp@430: /*****************************************************************************/ fp@430: fp@847: /** Start blocking Sdo entry reading. fp@847: * fp@758: * This function blocks, until reading is finished, and is interruptible as fp@758: * long as the master state machine has not begun with reading. fp@847: * fp@758: * \return number of bytes written to buffer, or error code. fp@758: */ fp@847: ssize_t ec_sdo_entry_read_value( fp@847: ec_sdo_entry_t *entry, /**< Sdo entry. */ fp@847: char *buffer /**< Target buffer. */ fp@847: ) fp@430: { fp@739: ec_master_t *master = entry->sdo->slave->master; fp@430: off_t off = 0; fp@849: ec_master_sdo_request_t request; fp@849: fp@849: request.slave = entry->sdo->slave; fp@852: ec_sdo_request_init(&request.req); fp@854: ec_sdo_request_address(&request.req, entry->sdo->index, entry->subindex); fp@858: ecrt_sdo_request_read(&request.req); fp@646: fp@646: // schedule request. fp@646: down(&master->sdo_sem); fp@849: list_add_tail(&request.list, &master->slave_sdo_requests); fp@646: up(&master->sdo_sem); fp@646: fp@646: // wait for processing through FSM fp@646: if (wait_event_interruptible(master->sdo_queue, fp@849: request.req.state != EC_REQUEST_QUEUED)) { fp@441: // interrupted by signal fp@646: down(&master->sdo_sem); fp@849: if (request.req.state == EC_REQUEST_QUEUED) { fp@849: list_del(&request.req.list); fp@646: up(&master->sdo_sem); fp@646: return -EINTR; fp@646: } fp@646: // request already processing: interrupt not possible. fp@646: up(&master->sdo_sem); fp@646: } fp@646: fp@646: // wait until master FSM has finished processing fp@861: wait_event(master->sdo_queue, request.req.state != EC_REQUEST_BUSY); fp@861: fp@861: if (request.req.state != EC_REQUEST_SUCCESS) fp@646: return -EIO; fp@646: fp@849: off += ec_sdo_entry_format_data(entry, &request.req, buffer); fp@849: fp@849: ec_sdo_request_clear(&request.req); fp@430: return off; fp@430: } fp@430: fp@430: /*****************************************************************************/ fp@430: fp@847: /** Show the Sysfs attribute of an Sdo entry. fp@847: * fp@847: * /return Number of bytes written to buffer. fp@758: */ fp@847: ssize_t ec_show_sdo_entry_attribute( fp@847: struct kobject *kobj, /**< kobject. */ fp@847: struct attribute *attr, /**< Sysfs attribute. */ fp@847: char *buffer /**< Target buffer. */ fp@847: ) 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: /*****************************************************************************/