fp@2589: /****************************************************************************** fp@2589: * fp@2589: * $Id$ fp@2589: * fp@2589: * Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH fp@2589: * fp@2589: * This file is part of the IgH EtherCAT Master. fp@2589: * fp@2589: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@2589: * modify it under the terms of the GNU General Public License version 2, as fp@2589: * published by the Free Software Foundation. fp@2589: * fp@2589: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@2589: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@2589: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@2589: * Public License for more details. fp@2589: * fp@2589: * You should have received a copy of the GNU General Public License along fp@2589: * with the IgH EtherCAT Master; if not, write to the Free Software fp@2589: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@2589: * fp@2589: * --- fp@2589: * fp@2589: * The license mentioned above concerns the source code only. Using the fp@2589: * EtherCAT technology and brand is only permitted in compliance with the fp@2589: * industrial property and similar rights of Beckhoff Automation GmbH. fp@2589: * fp@2589: *****************************************************************************/ fp@2589: fp@2589: /** fp@2589: \file fp@2589: EtherCAT master character device. fp@2589: */ fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: #include fp@2589: #include fp@2589: fp@2589: #include "master.h" fp@2589: #include "slave_config.h" fp@2589: #include "voe_handler.h" fp@2589: #include "ethernet.h" fp@2589: #include "ioctl.h" fp@2589: fp@2589: /** Set to 1 to enable ioctl() latency tracing. fp@2589: * fp@2589: * Requires CPU timestamp counter! fp@2589: */ fp@2589: #define DEBUG_LATENCY 0 fp@2589: fp@2589: /** Optional compiler attributes fo ioctl() functions. fp@2589: */ fp@2589: #if 0 fp@2589: #define ATTRIBUTES __attribute__ ((__noinline__)) fp@2589: #else fp@2589: #define ATTRIBUTES fp@2589: #endif fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Copies a string to an ioctl structure. fp@2589: */ fp@2589: static void ec_ioctl_strcpy( fp@2589: char *target, /**< Target. */ fp@2589: const char *source /**< Source. */ fp@2589: ) fp@2589: { fp@2589: if (source) { fp@2589: strncpy(target, source, EC_IOCTL_STRING_SIZE); fp@2589: target[EC_IOCTL_STRING_SIZE - 1] = 0; fp@2589: } else { fp@2589: target[0] = 0; fp@2589: } fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get module information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_module( fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_module_t data; fp@2589: fp@2589: data.ioctl_version_magic = EC_IOCTL_VERSION_MAGIC; fp@2589: data.master_count = ec_master_count(); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get master information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_master( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_master_t io; fp@2589: unsigned int dev_idx, j; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: io.slave_count = master->slave_count; fp@2589: io.config_count = ec_master_config_count(master); fp@2589: io.domain_count = ec_master_domain_count(master); fp@2589: #ifdef EC_EOE fp@2589: io.eoe_handler_count = ec_master_eoe_handler_count(master); fp@2589: #endif fp@2589: io.phase = (uint8_t) master->phase; fp@2589: io.active = (uint8_t) master->active; fp@2589: io.scan_busy = master->scan_busy; fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (down_interruptible(&master->device_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: for (dev_idx = EC_DEVICE_MAIN; fp@2589: dev_idx < ec_master_num_devices(master); dev_idx++) { fp@2589: ec_device_t *device = &master->devices[dev_idx]; fp@2589: fp@2589: if (device->dev) { fp@2589: memcpy(io.devices[dev_idx].address, device->dev->dev_addr, fp@2589: ETH_ALEN); fp@2589: } else { fp@2589: memcpy(io.devices[dev_idx].address, master->macs[dev_idx], fp@2589: ETH_ALEN); fp@2589: } fp@2589: io.devices[dev_idx].attached = device->dev ? 1 : 0; fp@2589: io.devices[dev_idx].link_state = device->link_state ? 1 : 0; fp@2589: io.devices[dev_idx].tx_count = device->tx_count; fp@2589: io.devices[dev_idx].rx_count = device->rx_count; fp@2589: io.devices[dev_idx].tx_bytes = device->tx_bytes; fp@2589: io.devices[dev_idx].rx_bytes = device->rx_bytes; fp@2589: io.devices[dev_idx].tx_errors = device->tx_errors; fp@2589: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2589: io.devices[dev_idx].tx_frame_rates[j] = fp@2589: device->tx_frame_rates[j]; fp@2589: io.devices[dev_idx].rx_frame_rates[j] = fp@2589: device->rx_frame_rates[j]; fp@2589: io.devices[dev_idx].tx_byte_rates[j] = fp@2589: device->tx_byte_rates[j]; fp@2589: io.devices[dev_idx].rx_byte_rates[j] = fp@2589: device->rx_byte_rates[j]; fp@2589: } fp@2589: } fp@2589: io.num_devices = ec_master_num_devices(master); fp@2589: fp@2589: io.tx_count = master->device_stats.tx_count; fp@2589: io.rx_count = master->device_stats.rx_count; fp@2589: io.tx_bytes = master->device_stats.tx_bytes; fp@2589: io.rx_bytes = master->device_stats.rx_bytes; fp@2589: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2589: io.tx_frame_rates[j] = fp@2589: master->device_stats.tx_frame_rates[j]; fp@2589: io.rx_frame_rates[j] = fp@2589: master->device_stats.rx_frame_rates[j]; fp@2589: io.tx_byte_rates[j] = fp@2589: master->device_stats.tx_byte_rates[j]; fp@2589: io.rx_byte_rates[j] = fp@2589: master->device_stats.rx_byte_rates[j]; fp@2589: io.loss_rates[j] = fp@2589: master->device_stats.loss_rates[j]; fp@2589: } fp@2589: fp@2589: up(&master->device_sem); fp@2589: fp@2589: io.app_time = master->app_time; fp@2589: io.ref_clock = fp@2589: master->dc_ref_clock ? master->dc_ref_clock->ring_position : 0xffff; fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_t data; fp@2589: const ec_slave_t *slave; fp@2589: int i; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", data.position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.device_index = slave->device_index; fp@2589: data.vendor_id = slave->sii.vendor_id; fp@2589: data.product_code = slave->sii.product_code; fp@2589: data.revision_number = slave->sii.revision_number; fp@2589: data.serial_number = slave->sii.serial_number; fp@2589: data.alias = slave->effective_alias; fp@2589: data.boot_rx_mailbox_offset = slave->sii.boot_rx_mailbox_offset; fp@2589: data.boot_rx_mailbox_size = slave->sii.boot_rx_mailbox_size; fp@2589: data.boot_tx_mailbox_offset = slave->sii.boot_tx_mailbox_offset; fp@2589: data.boot_tx_mailbox_size = slave->sii.boot_tx_mailbox_size; fp@2589: data.std_rx_mailbox_offset = slave->sii.std_rx_mailbox_offset; fp@2589: data.std_rx_mailbox_size = slave->sii.std_rx_mailbox_size; fp@2589: data.std_tx_mailbox_offset = slave->sii.std_tx_mailbox_offset; fp@2589: data.std_tx_mailbox_size = slave->sii.std_tx_mailbox_size; fp@2589: data.mailbox_protocols = slave->sii.mailbox_protocols; fp@2589: data.has_general_category = slave->sii.has_general; fp@2589: data.coe_details = slave->sii.coe_details; fp@2589: data.general_flags = slave->sii.general_flags; fp@2589: data.current_on_ebus = slave->sii.current_on_ebus; fp@2589: for (i = 0; i < EC_MAX_PORTS; i++) { fp@2589: data.ports[i].desc = slave->ports[i].desc; fp@2589: data.ports[i].link.link_up = slave->ports[i].link.link_up; fp@2589: data.ports[i].link.loop_closed = slave->ports[i].link.loop_closed; fp@2589: data.ports[i].link.signal_detected = fp@2589: slave->ports[i].link.signal_detected; fp@2589: data.ports[i].receive_time = slave->ports[i].receive_time; fp@2589: if (slave->ports[i].next_slave) { fp@2589: data.ports[i].next_slave = fp@2589: slave->ports[i].next_slave->ring_position; fp@2589: } else { fp@2589: data.ports[i].next_slave = 0xffff; fp@2589: } fp@2589: data.ports[i].delay_to_next_dc = slave->ports[i].delay_to_next_dc; fp@2589: } fp@2589: data.fmmu_bit = slave->base_fmmu_bit_operation; fp@2589: data.dc_supported = slave->base_dc_supported; fp@2589: data.dc_range = slave->base_dc_range; fp@2589: data.has_dc_system_time = slave->has_dc_system_time; fp@2589: data.transmission_delay = slave->transmission_delay; fp@2589: data.al_state = slave->current_state; fp@2589: data.error_flag = slave->error_flag; fp@2589: fp@2589: data.sync_count = slave->sii.sync_count; fp@2589: data.sdo_count = ec_slave_sdo_count(slave); fp@2589: data.sii_nwords = slave->sii_nwords; fp@2589: ec_ioctl_strcpy(data.group, slave->sii.group); fp@2589: ec_ioctl_strcpy(data.image, slave->sii.image); fp@2589: ec_ioctl_strcpy(data.order, slave->sii.order); fp@2589: ec_ioctl_strcpy(data.name, slave->sii.name); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave sync manager information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sync( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sync_t data; fp@2589: const ec_slave_t *slave; fp@2589: const ec_sync_t *sync; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (data.sync_index >= slave->sii.sync_count) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "Sync manager %u does not exist!\n", fp@2589: data.sync_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: sync = &slave->sii.syncs[data.sync_index]; fp@2589: fp@2589: data.physical_start_address = sync->physical_start_address; fp@2589: data.default_size = sync->default_length; fp@2589: data.control_register = sync->control_register; fp@2589: data.enable = sync->enable; fp@2589: data.pdo_count = ec_pdo_list_count(&sync->pdos); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave sync manager PDO information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sync_pdo( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sync_pdo_t data; fp@2589: const ec_slave_t *slave; fp@2589: const ec_sync_t *sync; fp@2589: const ec_pdo_t *pdo; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (data.sync_index >= slave->sii.sync_count) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "Sync manager %u does not exist!\n", fp@2589: data.sync_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: sync = &slave->sii.syncs[data.sync_index]; fp@2589: if (!(pdo = ec_pdo_list_find_pdo_by_pos_const( fp@2589: &sync->pdos, data.pdo_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "Sync manager %u does not contain a PDO with " fp@2589: "position %u!\n", data.sync_index, data.pdo_pos); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.index = pdo->index; fp@2589: data.entry_count = ec_pdo_entry_count(pdo); fp@2589: ec_ioctl_strcpy(data.name, pdo->name); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave sync manager PDO entry information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sync_pdo_entry( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sync_pdo_entry_t data; fp@2589: const ec_slave_t *slave; fp@2589: const ec_sync_t *sync; fp@2589: const ec_pdo_t *pdo; fp@2589: const ec_pdo_entry_t *entry; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (data.sync_index >= slave->sii.sync_count) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "Sync manager %u does not exist!\n", fp@2589: data.sync_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: sync = &slave->sii.syncs[data.sync_index]; fp@2589: if (!(pdo = ec_pdo_list_find_pdo_by_pos_const( fp@2589: &sync->pdos, data.pdo_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "Sync manager %u does not contain a PDO with " fp@2589: "position %u!\n", data.sync_index, data.pdo_pos); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(entry = ec_pdo_find_entry_by_pos_const( fp@2589: pdo, data.entry_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "PDO 0x%04X does not contain an entry with " fp@2589: "position %u!\n", data.pdo_pos, data.entry_pos); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.index = entry->index; fp@2589: data.subindex = entry->subindex; fp@2589: data.bit_length = entry->bit_length; fp@2589: ec_ioctl_strcpy(data.name, entry->name); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get domain information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_domain_t data; fp@2589: const ec_domain_t *domain; fp@2589: unsigned int dev_idx; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(domain = ec_master_find_domain_const(master, data.index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Domain %u does not exist!\n", data.index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.data_size = domain->data_size; fp@2589: data.logical_base_address = domain->logical_base_address; fp@2589: for (dev_idx = EC_DEVICE_MAIN; fp@2589: dev_idx < ec_master_num_devices(domain->master); dev_idx++) { fp@2589: data.working_counter[dev_idx] = domain->working_counter[dev_idx]; fp@2589: } fp@2589: data.expected_working_counter = domain->expected_working_counter; fp@2589: data.fmmu_count = ec_domain_fmmu_count(domain); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get domain FMMU information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_fmmu( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_domain_fmmu_t data; fp@2589: const ec_domain_t *domain; fp@2589: const ec_fmmu_config_t *fmmu; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(domain = ec_master_find_domain_const(master, data.domain_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Domain %u does not exist!\n", fp@2589: data.domain_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(fmmu = ec_domain_find_fmmu(domain, data.fmmu_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Domain %u has less than %u" fp@2589: " fmmu configurations.\n", fp@2589: data.domain_index, data.fmmu_index + 1); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.slave_config_alias = fmmu->sc->alias; fp@2589: data.slave_config_position = fmmu->sc->position; fp@2589: data.sync_index = fmmu->sync_index; fp@2589: data.dir = fmmu->dir; fp@2589: data.logical_address = fmmu->logical_start_address; fp@2589: data.data_size = fmmu->data_size; fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get domain data. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_data( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< Userspace address to store the results. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_domain_data_t data; fp@2589: const ec_domain_t *domain; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(domain = ec_master_find_domain_const(master, data.domain_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Domain %u does not exist!\n", fp@2589: data.domain_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (domain->data_size != data.data_size) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Data size mismatch %u/%zu!\n", fp@2589: data.data_size, domain->data_size); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) data.target, domain->data, fp@2589: domain->data_size)) { fp@2589: up(&master->master_sem); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Set master debug level. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_master_debug( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: return ec_master_debug_level(master, (unsigned long) arg); fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Issue a bus scan. fp@2589: * fp@2589: * \return Always zero (success). fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_master_rescan( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: master->fsm.rescan_required = 1; fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Set slave state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_state_t data; fp@2589: ec_slave_t *slave; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: ec_slave_request_state(slave, data.al_state); fp@2589: fp@2589: up(&master->master_sem); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave SDO information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sdo( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sdo_t data; fp@2589: const ec_slave_t *slave; fp@2589: const ec_sdo_t *sdo; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(sdo = ec_slave_get_sdo_by_pos_const( fp@2589: slave, data.sdo_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "SDO %u does not exist!\n", data.sdo_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.sdo_index = sdo->index; fp@2589: data.max_subindex = sdo->max_subindex; fp@2589: ec_ioctl_strcpy(data.name, sdo->name); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave SDO entry information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sdo_entry( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sdo_entry_t data; fp@2589: const ec_slave_t *slave; fp@2589: const ec_sdo_t *sdo; fp@2589: const ec_sdo_entry_t *entry; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (data.sdo_spec <= 0) { fp@2589: if (!(sdo = ec_slave_get_sdo_by_pos_const( fp@2589: slave, -data.sdo_spec))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "SDO %u does not exist!\n", -data.sdo_spec); fp@2589: return -EINVAL; fp@2589: } fp@2589: } else { fp@2589: if (!(sdo = ec_slave_get_sdo_const( fp@2589: slave, data.sdo_spec))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "SDO 0x%04X does not exist!\n", fp@2589: data.sdo_spec); fp@2589: return -EINVAL; fp@2589: } fp@2589: } fp@2589: fp@2589: if (!(entry = ec_sdo_get_entry_const( fp@2589: sdo, data.sdo_entry_subindex))) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "SDO entry 0x%04X:%02X does not exist!\n", fp@2589: sdo->index, data.sdo_entry_subindex); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.data_type = entry->data_type; fp@2589: data.bit_length = entry->bit_length; fp@2589: data.read_access[EC_SDO_ENTRY_ACCESS_PREOP] = fp@2589: entry->read_access[EC_SDO_ENTRY_ACCESS_PREOP]; fp@2589: data.read_access[EC_SDO_ENTRY_ACCESS_SAFEOP] = fp@2589: entry->read_access[EC_SDO_ENTRY_ACCESS_SAFEOP]; fp@2589: data.read_access[EC_SDO_ENTRY_ACCESS_OP] = fp@2589: entry->read_access[EC_SDO_ENTRY_ACCESS_OP]; fp@2589: data.write_access[EC_SDO_ENTRY_ACCESS_PREOP] = fp@2589: entry->write_access[EC_SDO_ENTRY_ACCESS_PREOP]; fp@2589: data.write_access[EC_SDO_ENTRY_ACCESS_SAFEOP] = fp@2589: entry->write_access[EC_SDO_ENTRY_ACCESS_SAFEOP]; fp@2589: data.write_access[EC_SDO_ENTRY_ACCESS_OP] = fp@2589: entry->write_access[EC_SDO_ENTRY_ACCESS_OP]; fp@2589: ec_ioctl_strcpy(data.description, entry->description); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Upload SDO. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sdo_upload( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sdo_upload_t data; fp@2589: uint8_t *target; fp@2589: int ret; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (!(target = kmalloc(data.target_size, GFP_KERNEL))) { fp@2589: EC_MASTER_ERR(master, "Failed to allocate %zu bytes" fp@2589: " for SDO upload.\n", data.target_size); fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: ret = ecrt_master_sdo_upload(master, data.slave_position, fp@2589: data.sdo_index, data.sdo_entry_subindex, target, fp@2589: data.target_size, &data.data_size, &data.abort_code); fp@2589: fp@2589: if (!ret) { fp@2589: if (copy_to_user((void __user *) data.target, fp@2589: target, data.data_size)) { fp@2589: kfree(target); fp@2589: return -EFAULT; fp@2589: } fp@2589: } fp@2589: fp@2589: kfree(target); fp@2589: fp@2589: if (__copy_to_user((void __user *) arg, &data, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Download SDO. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sdo_download( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sdo_download_t data; fp@2589: uint8_t *sdo_data; fp@2589: int retval; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (!(sdo_data = kmalloc(data.data_size, GFP_KERNEL))) { fp@2589: EC_MASTER_ERR(master, "Failed to allocate %zu bytes" fp@2589: " for SDO download.\n", data.data_size); fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(sdo_data, (void __user *) data.data, data.data_size)) { fp@2589: kfree(sdo_data); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (data.complete_access) { fp@2589: retval = ecrt_master_sdo_download_complete(master, data.slave_position, fp@2589: data.sdo_index, sdo_data, data.data_size, &data.abort_code); fp@2589: } else { fp@2589: retval = ecrt_master_sdo_download(master, data.slave_position, fp@2589: data.sdo_index, data.sdo_entry_subindex, sdo_data, fp@2589: data.data_size, &data.abort_code); fp@2589: } fp@2589: fp@2589: kfree(sdo_data); fp@2589: fp@2589: if (__copy_to_user((void __user *) arg, &data, sizeof(data))) { fp@2589: retval = -EFAULT; fp@2589: } fp@2589: fp@2589: return retval; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read a slave's SII. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sii_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sii_t data; fp@2589: const ec_slave_t *slave; fp@2589: int retval; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(slave = ec_master_find_slave_const( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!data.nwords fp@2589: || data.offset + data.nwords > slave->sii_nwords) { fp@2589: up(&master->master_sem); fp@2589: EC_SLAVE_ERR(slave, "Invalid SII read offset/size %u/%u for slave SII" fp@2589: " size %zu!\n", data.offset, data.nwords, slave->sii_nwords); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) data.words, fp@2589: slave->sii_words + data.offset, data.nwords * 2)) fp@2589: retval = -EFAULT; fp@2589: else fp@2589: retval = 0; fp@2589: fp@2589: up(&master->master_sem); fp@2589: return retval; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Write a slave's SII. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_sii_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_sii_t data; fp@2589: ec_slave_t *slave; fp@2589: unsigned int byte_size; fp@2589: uint16_t *words; fp@2589: ec_sii_write_request_t request; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (!data.nwords) { fp@2589: return 0; fp@2589: } fp@2589: fp@2589: byte_size = sizeof(uint16_t) * data.nwords; fp@2589: if (!(words = kmalloc(byte_size, GFP_KERNEL))) { fp@2589: EC_MASTER_ERR(master, "Failed to allocate %u bytes" fp@2589: " for SII contents.\n", byte_size); fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(words, fp@2589: (void __user *) data.words, byte_size)) { fp@2589: kfree(words); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: kfree(words); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(slave = ec_master_find_slave( fp@2589: master, 0, data.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: data.slave_position); fp@2589: kfree(words); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: // init SII write request fp@2589: INIT_LIST_HEAD(&request.list); fp@2589: request.slave = slave; fp@2589: request.words = words; fp@2589: request.offset = data.offset; fp@2589: request.nwords = data.nwords; fp@2589: request.state = EC_INT_REQUEST_QUEUED; fp@2589: fp@2589: // schedule SII write request. fp@2589: list_add_tail(&request.list, &master->sii_requests); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: // wait for processing through FSM fp@2589: if (wait_event_interruptible(master->request_queue, fp@2589: request.state != EC_INT_REQUEST_QUEUED)) { fp@2589: // interrupted by signal fp@2589: down(&master->master_sem); fp@2589: if (request.state == EC_INT_REQUEST_QUEUED) { fp@2589: // abort request fp@2589: list_del(&request.list); fp@2589: up(&master->master_sem); fp@2589: kfree(words); fp@2589: return -EINTR; fp@2589: } fp@2589: up(&master->master_sem); fp@2589: } fp@2589: fp@2589: // wait until master FSM has finished processing fp@2589: wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY); fp@2589: fp@2589: kfree(words); fp@2589: fp@2589: return request.state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read a slave's registers. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_reg_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_reg_t io; fp@2589: ec_slave_t *slave; fp@2589: ec_reg_request_t request; fp@2589: int ret; fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (!io.size) { fp@2589: return 0; fp@2589: } fp@2589: fp@2589: // init register request fp@2589: ret = ec_reg_request_init(&request, io.size); fp@2589: if (ret) { fp@2589: return ret; fp@2589: } fp@2589: fp@2589: ecrt_reg_request_read(&request, io.address, io.size); fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ec_reg_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(slave = ec_master_find_slave( fp@2589: master, 0, io.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: ec_reg_request_clear(&request); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: io.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: // schedule request. fp@2589: list_add_tail(&request.list, &slave->reg_requests); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: // wait for processing through FSM fp@2589: if (wait_event_interruptible(master->request_queue, fp@2589: request.state != EC_INT_REQUEST_QUEUED)) { fp@2589: // interrupted by signal fp@2589: down(&master->master_sem); fp@2589: if (request.state == EC_INT_REQUEST_QUEUED) { fp@2589: // abort request fp@2589: list_del(&request.list); fp@2589: up(&master->master_sem); fp@2589: ec_reg_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: up(&master->master_sem); fp@2589: } fp@2589: fp@2589: // wait until master FSM has finished processing fp@2589: wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY); fp@2589: fp@2589: if (request.state == EC_INT_REQUEST_SUCCESS) { fp@2589: if (copy_to_user((void __user *) io.data, request.data, io.size)) { fp@2589: return -EFAULT; fp@2589: } fp@2589: } fp@2589: ec_reg_request_clear(&request); fp@2589: fp@2589: return request.state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Write a slave's registers. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_reg_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_reg_t io; fp@2589: ec_slave_t *slave; fp@2589: ec_reg_request_t request; fp@2589: int ret; fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (!io.size) { fp@2589: return 0; fp@2589: } fp@2589: fp@2589: // init register request fp@2589: ret = ec_reg_request_init(&request, io.size); fp@2589: if (ret) { fp@2589: return ret; fp@2589: } fp@2589: fp@2589: if (copy_from_user(request.data, (void __user *) io.data, io.size)) { fp@2589: ec_reg_request_clear(&request); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ecrt_reg_request_write(&request, io.address, io.size); fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ec_reg_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (io.emergency) { fp@2589: request.ring_position = io.slave_position; fp@2589: // schedule request. fp@2589: list_add_tail(&request.list, &master->emerg_reg_requests); fp@2589: } fp@2589: else { fp@2589: if (!(slave = ec_master_find_slave(master, 0, io.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: ec_reg_request_clear(&request); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: io.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: // schedule request. fp@2589: list_add_tail(&request.list, &slave->reg_requests); fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: // wait for processing through FSM fp@2589: if (wait_event_interruptible(master->request_queue, fp@2589: request.state != EC_INT_REQUEST_QUEUED)) { fp@2589: // interrupted by signal fp@2589: down(&master->master_sem); fp@2589: if (request.state == EC_INT_REQUEST_QUEUED) { fp@2589: // abort request fp@2589: list_del(&request.list); fp@2589: up(&master->master_sem); fp@2589: ec_reg_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: up(&master->master_sem); fp@2589: } fp@2589: fp@2589: // wait until master FSM has finished processing fp@2589: wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY); fp@2589: fp@2589: ec_reg_request_clear(&request); fp@2589: fp@2589: return request.state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave configuration information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_config( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_t data; fp@2589: const ec_slave_config_t *sc; fp@2589: uint8_t i; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config_const( fp@2589: master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave config %u does not exist!\n", fp@2589: data.config_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.alias = sc->alias; fp@2589: data.position = sc->position; fp@2589: data.vendor_id = sc->vendor_id; fp@2589: data.product_code = sc->product_code; fp@2589: for (i = 0; i < EC_MAX_SYNC_MANAGERS; i++) { fp@2589: data.syncs[i].dir = sc->sync_configs[i].dir; fp@2589: data.syncs[i].watchdog_mode = sc->sync_configs[i].watchdog_mode; fp@2589: data.syncs[i].pdo_count = fp@2589: ec_pdo_list_count(&sc->sync_configs[i].pdos); fp@2589: } fp@2589: data.watchdog_divider = sc->watchdog_divider; fp@2589: data.watchdog_intervals = sc->watchdog_intervals; fp@2589: data.sdo_count = ec_slave_config_sdo_count(sc); fp@2589: data.idn_count = ec_slave_config_idn_count(sc); fp@2589: data.slave_position = sc->slave ? sc->slave->ring_position : -1; fp@2589: data.dc_assign_activate = sc->dc_assign_activate; fp@2589: for (i = 0; i < EC_SYNC_SIGNAL_COUNT; i++) { fp@2589: data.dc_sync[i] = sc->dc_sync[i]; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave configuration PDO information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_config_pdo( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_pdo_t data; fp@2589: const ec_slave_config_t *sc; fp@2589: const ec_pdo_t *pdo; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (data.sync_index >= EC_MAX_SYNC_MANAGERS) { fp@2589: EC_MASTER_ERR(master, "Invalid sync manager index %u!\n", fp@2589: data.sync_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config_const( fp@2589: master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave config %u does not exist!\n", fp@2589: data.config_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(pdo = ec_pdo_list_find_pdo_by_pos_const( fp@2589: &sc->sync_configs[data.sync_index].pdos, fp@2589: data.pdo_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Invalid PDO position!\n"); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.index = pdo->index; fp@2589: data.entry_count = ec_pdo_entry_count(pdo); fp@2589: ec_ioctl_strcpy(data.name, pdo->name); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave configuration PDO entry information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_config_pdo_entry( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_pdo_entry_t data; fp@2589: const ec_slave_config_t *sc; fp@2589: const ec_pdo_t *pdo; fp@2589: const ec_pdo_entry_t *entry; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (data.sync_index >= EC_MAX_SYNC_MANAGERS) { fp@2589: EC_MASTER_ERR(master, "Invalid sync manager index %u!\n", fp@2589: data.sync_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config_const( fp@2589: master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave config %u does not exist!\n", fp@2589: data.config_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(pdo = ec_pdo_list_find_pdo_by_pos_const( fp@2589: &sc->sync_configs[data.sync_index].pdos, fp@2589: data.pdo_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Invalid PDO position!\n"); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(entry = ec_pdo_find_entry_by_pos_const( fp@2589: pdo, data.entry_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Entry not found!\n"); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: data.index = entry->index; fp@2589: data.subindex = entry->subindex; fp@2589: data.bit_length = entry->bit_length; fp@2589: ec_ioctl_strcpy(data.name, entry->name); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave configuration SDO information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_config_sdo( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_sdo_t *ioctl; fp@2589: const ec_slave_config_t *sc; fp@2589: const ec_sdo_request_t *req; fp@2589: fp@2589: if (!(ioctl = kmalloc(sizeof(*ioctl), GFP_KERNEL))) { fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(ioctl, (void __user *) arg, sizeof(*ioctl))) { fp@2589: kfree(ioctl); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: kfree(ioctl); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config_const( fp@2589: master, ioctl->config_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave config %u does not exist!\n", fp@2589: ioctl->config_index); fp@2589: kfree(ioctl); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_get_sdo_by_pos_const( fp@2589: sc, ioctl->sdo_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Invalid SDO position!\n"); fp@2589: kfree(ioctl); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: ioctl->index = req->index; fp@2589: ioctl->subindex = req->subindex; fp@2589: ioctl->size = req->data_size; fp@2589: memcpy(ioctl->data, req->data, fp@2589: min((u32) ioctl->size, (u32) EC_MAX_SDO_DATA_SIZE)); fp@2589: ioctl->complete_access = req->complete_access; fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, ioctl, sizeof(*ioctl))) { fp@2589: kfree(ioctl); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: kfree(ioctl); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get slave configuration IDN information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_config_idn( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_idn_t *ioctl; fp@2589: const ec_slave_config_t *sc; fp@2589: const ec_soe_request_t *req; fp@2589: fp@2589: if (!(ioctl = kmalloc(sizeof(*ioctl), GFP_KERNEL))) { fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(ioctl, (void __user *) arg, sizeof(*ioctl))) { fp@2589: kfree(ioctl); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: kfree(ioctl); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config_const( fp@2589: master, ioctl->config_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave config %u does not exist!\n", fp@2589: ioctl->config_index); fp@2589: kfree(ioctl); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_get_idn_by_pos_const( fp@2589: sc, ioctl->idn_pos))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Invalid IDN position!\n"); fp@2589: kfree(ioctl); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: ioctl->drive_no = req->drive_no; fp@2589: ioctl->idn = req->idn; fp@2589: ioctl->state = req->state; fp@2589: ioctl->size = req->data_size; fp@2589: memcpy(ioctl->data, req->data, fp@2589: min((u32) ioctl->size, (u32) EC_MAX_IDN_DATA_SIZE)); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, ioctl, sizeof(*ioctl))) { fp@2589: kfree(ioctl); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: kfree(ioctl); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: #ifdef EC_EOE fp@2589: fp@2589: /** Get EoE handler information. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_eoe_handler( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_eoe_handler_t data; fp@2589: const ec_eoe_t *eoe; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(eoe = ec_master_get_eoe_handler_const(master, data.eoe_index))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "EoE handler %u does not exist!\n", fp@2589: data.eoe_index); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: if (eoe->slave) { fp@2589: data.slave_position = eoe->slave->ring_position; fp@2589: } else { fp@2589: data.slave_position = 0xffff; fp@2589: } fp@2589: snprintf(data.name, EC_DATAGRAM_NAME_SIZE, eoe->dev->name); fp@2589: data.open = eoe->opened; fp@2589: data.rx_bytes = eoe->stats.tx_bytes; fp@2589: data.rx_rate = eoe->tx_rate; fp@2589: data.tx_bytes = eoe->stats.rx_bytes; fp@2589: data.tx_rate = eoe->tx_rate; fp@2589: data.tx_queued_frames = eoe->tx_queued_frames; fp@2589: data.tx_queue_size = eoe->tx_queue_size; fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: #endif fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2597: /** Request EoE IP parameter setting. fp@2597: * fp@2597: * \return Zero on success, otherwise a negative error code. fp@2597: */ fp@2597: static ATTRIBUTES int ec_ioctl_slave_eoe_ip_param( fp@2597: ec_master_t *master, /**< EtherCAT master. */ fp@2597: void *arg /**< ioctl() argument. */ fp@2597: ) fp@2597: { fp@2597: ec_ioctl_slave_eoe_ip_t io; fp@2597: ec_eoe_request_t req; fp@2597: ec_slave_t *slave; fp@2597: fp@2597: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2597: return -EFAULT; fp@2597: } fp@2597: fp@2597: // init EoE request fp@2597: ec_eoe_request_init(&req); fp@2597: fp@2597: req.mac_address_included = io.mac_address_included; fp@2597: req.ip_address_included = io.ip_address_included; fp@2597: req.subnet_mask_included = io.subnet_mask_included; fp@2597: req.gateway_included = io.gateway_included; fp@2597: req.dns_included = io.dns_included; fp@2597: req.name_included = io.name_included; fp@2597: fp@2597: memcpy(req.mac_address, io.mac_address, ETH_ALEN); fp@2597: req.ip_address = io.ip_address; fp@2597: req.subnet_mask = io.subnet_mask; fp@2597: req.gateway = io.gateway; fp@2597: req.dns = io.dns; fp@2597: memcpy(req.name, io.name, EC_MAX_HOSTNAME_SIZE); fp@2597: fp@2597: req.state = EC_INT_REQUEST_QUEUED; fp@2597: fp@2597: if (down_interruptible(&master->master_sem)) { fp@2597: return -EINTR; fp@2597: } fp@2597: fp@2597: if (!(slave = ec_master_find_slave( fp@2597: master, 0, io.slave_position))) { fp@2597: up(&master->master_sem); fp@2597: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2597: io.slave_position); fp@2597: return -EINVAL; fp@2597: } fp@2597: fp@2597: EC_MASTER_DBG(master, 1, "Scheduling EoE request.\n"); fp@2597: fp@2597: // schedule request. fp@2597: list_add_tail(&req.list, &slave->eoe_requests); fp@2597: fp@2597: up(&master->master_sem); fp@2597: fp@2597: // wait for processing through FSM fp@2597: if (wait_event_interruptible(master->request_queue, fp@2597: req.state != EC_INT_REQUEST_QUEUED)) { fp@2597: // interrupted by signal fp@2597: down(&master->master_sem); fp@2597: if (req.state == EC_INT_REQUEST_QUEUED) { fp@2597: // abort request fp@2597: list_del(&req.list); fp@2597: up(&master->master_sem); fp@2597: return -EINTR; fp@2597: } fp@2597: up(&master->master_sem); fp@2597: } fp@2597: fp@2597: // wait until master FSM has finished processing fp@2597: wait_event(master->request_queue, req.state != EC_INT_REQUEST_BUSY); fp@2597: fp@2597: io.result = req.result; fp@2597: fp@2597: if (copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2597: return -EFAULT; fp@2597: } fp@2597: fp@2597: return req.state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO; fp@2597: } fp@2597: fp@2597: /*****************************************************************************/ fp@2597: fp@2589: /** Request the master from userspace. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_request( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_master_t *m; fp@2589: int ret = 0; fp@2589: fp@2589: m = ecrt_request_master_err(master->index); fp@2589: if (IS_ERR(m)) { fp@2589: ret = PTR_ERR(m); fp@2589: } else { fp@2589: ctx->requested = 1; fp@2589: } fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Create a domain. fp@2589: * fp@2589: * \return Domain index on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_create_domain( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_domain_t *domain; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: domain = ecrt_master_create_domain_err(master); fp@2589: if (IS_ERR(domain)) fp@2589: return PTR_ERR(domain); fp@2589: fp@2589: return domain->index; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Create a slave configuration. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_create_slave_config( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_t data; fp@2589: ec_slave_config_t *sc, *entry; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: sc = ecrt_master_slave_config_err(master, data.alias, data.position, fp@2589: data.vendor_id, data.product_code); fp@2589: if (IS_ERR(sc)) fp@2589: return PTR_ERR(sc); fp@2589: fp@2589: data.config_index = 0; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: list_for_each_entry(entry, &master->configs, list) { fp@2589: if (entry == sc) fp@2589: break; fp@2589: data.config_index++; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Select the DC reference clock. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_select_ref_clock( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: unsigned long config_index = (unsigned long) arg; fp@2589: ec_slave_config_t *sc = NULL; fp@2589: int ret = 0; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: ret = -EPERM; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ret = -EINTR; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (config_index != 0xFFFFFFFF) { fp@2589: if (!(sc = ec_master_get_config(master, config_index))) { fp@2589: ret = -ENOENT; fp@2589: goto out_up; fp@2589: } fp@2589: } fp@2589: fp@2589: ecrt_master_select_reference_clock(master, sc); fp@2589: fp@2589: out_up: fp@2589: up(&master->master_sem); fp@2589: out_return: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Activates the master. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_activate( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_master_activate_t io; fp@2589: ec_domain_t *domain; fp@2589: off_t offset; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: io.process_data = NULL; fp@2589: fp@2589: /* Get the sum of the domains' process data sizes. */ fp@2589: fp@2589: ctx->process_data_size = 0; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: list_for_each_entry(domain, &master->domains, list) { fp@2589: ctx->process_data_size += ecrt_domain_size(domain); fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: if (ctx->process_data_size) { fp@2589: ctx->process_data = vmalloc(ctx->process_data_size); fp@2589: if (!ctx->process_data) { fp@2589: ctx->process_data_size = 0; fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: /* Set the memory as external process data memory for the fp@2589: * domains. fp@2589: */ fp@2589: offset = 0; fp@2589: list_for_each_entry(domain, &master->domains, list) { fp@2589: ecrt_domain_external_memory(domain, fp@2589: ctx->process_data + offset); fp@2589: offset += ecrt_domain_size(domain); fp@2589: } fp@2589: fp@2589: #ifdef EC_IOCTL_RTDM fp@2589: /* RTDM uses a different approach for memory-mapping, which has to be fp@2589: * initiated by the kernel. fp@2589: */ fp@2589: ret = ec_rtdm_mmap(ctx, &io.process_data); fp@2589: if (ret < 0) { fp@2589: EC_MASTER_ERR(master, "Failed to map process data" fp@2589: " memory to user space (code %i).\n", ret); fp@2589: return ret; fp@2589: } fp@2589: #endif fp@2589: } fp@2589: fp@2589: io.process_data_size = ctx->process_data_size; fp@2589: fp@2589: #ifndef EC_IOCTL_RTDM fp@2589: ecrt_master_callbacks(master, ec_master_internal_send_cb, fp@2589: ec_master_internal_receive_cb, master); fp@2589: #endif fp@2589: fp@2589: ret = ecrt_master_activate(master); fp@2589: if (ret < 0) fp@2589: return ret; fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &io, fp@2589: sizeof(ec_ioctl_master_activate_t))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Deactivates the master. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_deactivate( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: ecrt_master_deactivate(master); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Set max. number of databytes in a cycle fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_set_send_interval( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: size_t send_interval; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&send_interval, (void __user *) arg, fp@2589: sizeof(send_interval))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: ec_master_set_send_interval(master, send_interval); fp@2589: fp@2589: up(&master->master_sem); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Send frames. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_send( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: ecrt_master_send(master); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Receive frames. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_receive( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: ecrt_master_receive(master); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get the master state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_master_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_master_state_t data; fp@2589: fp@2589: ecrt_master_state(master, &data); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get the link state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_master_link_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_link_state_t ioctl; fp@2589: ec_master_link_state_t state; fp@2589: int ret; fp@2589: fp@2589: if (copy_from_user(&ioctl, (void __user *) arg, sizeof(ioctl))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ret = ecrt_master_link_state(master, ioctl.dev_idx, &state); fp@2589: if (ret < 0) { fp@2589: return ret; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) ioctl.state, &state, sizeof(state))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Set the master DC application time. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_app_time( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_app_time_t data; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ecrt_master_application_time(master, data.app_time); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Sync the reference clock. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sync_ref( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: ecrt_master_sync_reference_clock(master); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Sync the slave clocks. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sync_slaves( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: ecrt_master_sync_slave_clocks(master); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get the system time of the reference clock. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_ref_clock_time( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: uint32_t time; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: ret = ecrt_master_reference_clock_time(master, &time); fp@2589: if (ret) { fp@2589: return ret; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &time, sizeof(time))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Queue the sync monitoring datagram. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sync_mon_queue( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: ecrt_master_sync_monitor_queue(master); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Processes the sync monitoring datagram. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sync_mon_process( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: uint32_t time_diff; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: time_diff = ecrt_master_sync_monitor_process(master); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &time_diff, sizeof(time_diff))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Reset configuration. fp@2589: * fp@2589: * \return Always zero (success). fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_reset( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: down(&master->master_sem); fp@2589: ecrt_master_reset(master); fp@2589: up(&master->master_sem); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Configure a sync manager. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_sync( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_t data; fp@2589: ec_slave_config_t *sc; fp@2589: unsigned int i; fp@2589: int ret = 0; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: ret = -EPERM; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: ret = -EFAULT; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ret = -EINTR; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: ret = -ENOENT; fp@2589: goto out_up; fp@2589: } fp@2589: fp@2589: for (i = 0; i < EC_MAX_SYNC_MANAGERS; i++) { fp@2589: if (data.syncs[i].config_this) { fp@2589: ret = ecrt_slave_config_sync_manager(sc, i, data.syncs[i].dir, fp@2589: data.syncs[i].watchdog_mode); fp@2589: if (ret) { fp@2589: goto out_up; fp@2589: } fp@2589: } fp@2589: } fp@2589: fp@2589: out_up: fp@2589: up(&master->master_sem); fp@2589: out_return: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Configure a slave's watchdogs. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_watchdog( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_t data; fp@2589: ec_slave_config_t *sc; fp@2589: int ret = 0; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: ret = -EPERM; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: ret = -EFAULT; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ret = -EINTR; fp@2589: goto out_return; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: ret = -ENOENT; fp@2589: goto out_up; fp@2589: } fp@2589: fp@2589: ecrt_slave_config_watchdog(sc, fp@2589: data.watchdog_divider, data.watchdog_intervals); fp@2589: fp@2589: out_up: fp@2589: up(&master->master_sem); fp@2589: out_return: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Add a PDO to the assignment. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_add_pdo( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_pdo_t data; fp@2589: ec_slave_config_t *sc; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: return ecrt_slave_config_pdo_assign_add(sc, data.sync_index, data.index); fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Clears the PDO assignment. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_clear_pdos( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_pdo_t data; fp@2589: ec_slave_config_t *sc; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: ecrt_slave_config_pdo_assign_clear(sc, data.sync_index); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Add an entry to a PDO's mapping. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_add_entry( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_add_pdo_entry_t data; fp@2589: ec_slave_config_t *sc; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: return ecrt_slave_config_pdo_mapping_add(sc, data.pdo_index, fp@2589: data.entry_index, data.entry_subindex, data.entry_bit_length); fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Clears the mapping of a PDO. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_clear_entries( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_pdo_t data; fp@2589: ec_slave_config_t *sc; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: ecrt_slave_config_pdo_mapping_clear(sc, data.index); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Registers a PDO entry. fp@2589: * fp@2589: * \return Process data offset on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_reg_pdo_entry( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_pdo_entry_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_domain_t *domain; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(domain = ec_master_find_domain(master, data.domain_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc or domain could be invalidated */ fp@2589: fp@2589: ret = ecrt_slave_config_reg_pdo_entry(sc, data.entry_index, fp@2589: data.entry_subindex, domain, &data.bit_position); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Registers a PDO entry by its position. fp@2589: * fp@2589: * \return Process data offset on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_reg_pdo_pos( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_pdo_pos_t io; fp@2589: ec_slave_config_t *sc; fp@2589: ec_domain_t *domain; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(domain = ec_master_find_domain(master, io.domain_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc or domain could be invalidated */ fp@2589: fp@2589: ret = ecrt_slave_config_reg_pdo_entry_pos(sc, io.sync_index, fp@2589: io.pdo_pos, io.entry_pos, domain, &io.bit_position); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &io, sizeof(io))) fp@2589: return -EFAULT; fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Sets the DC AssignActivate word and the sync signal times. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_dc( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_config_t data; fp@2589: ec_slave_config_t *sc; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_slave_config_dc(sc, data.dc_assign_activate, fp@2589: data.dc_sync[0].cycle_time, fp@2589: data.dc_sync[0].shift_time, fp@2589: data.dc_sync[1].cycle_time, fp@2589: data.dc_sync[1].shift_time); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Configures an SDO. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_sdo( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_sdo_t data; fp@2589: ec_slave_config_t *sc; fp@2589: uint8_t *sdo_data = NULL; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (!data.size) fp@2589: return -EINVAL; fp@2589: fp@2589: if (!(sdo_data = kmalloc(data.size, GFP_KERNEL))) { fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(sdo_data, (void __user *) data.data, data.size)) { fp@2589: kfree(sdo_data); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: kfree(sdo_data); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: up(&master->master_sem); fp@2589: kfree(sdo_data); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: if (data.complete_access) { fp@2589: ret = ecrt_slave_config_complete_sdo(sc, fp@2589: data.index, sdo_data, data.size); fp@2589: } else { fp@2589: ret = ecrt_slave_config_sdo(sc, data.index, data.subindex, sdo_data, fp@2589: data.size); fp@2589: } fp@2589: kfree(sdo_data); fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Set the emergency ring buffer size. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_emerg_size( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_emerg_t io; fp@2589: ec_slave_config_t *sc; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ret = ecrt_slave_config_emerg_size(sc, io.size); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get an emergency message from the ring. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_emerg_pop( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_emerg_t io; fp@2589: ec_slave_config_t *sc; fp@2589: u8 msg[EC_COE_EMERGENCY_MSG_SIZE]; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because configuration will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ret = ecrt_slave_config_emerg_pop(sc, msg); fp@2589: if (ret < 0) { fp@2589: return ret; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) io.target, msg, sizeof(msg))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Clear the emergency ring. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_emerg_clear( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_emerg_t io; fp@2589: ec_slave_config_t *sc; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because configuration will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: return ecrt_slave_config_emerg_clear(sc); fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get the number of emergency overruns. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_emerg_overruns( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_emerg_t io; fp@2589: ec_slave_config_t *sc; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because configuration will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ret = ecrt_slave_config_emerg_overruns(sc); fp@2589: if (ret < 0) { fp@2589: return ret; fp@2589: } fp@2589: fp@2589: io.overruns = ret; fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Create an SDO request. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_create_sdo_request( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: data.request_index = 0; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: sc = ec_master_get_config(master, data.config_index); fp@2589: if (!sc) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: list_for_each_entry(req, &sc->sdo_requests, list) { fp@2589: data.request_index++; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: req = ecrt_slave_config_create_sdo_request_err(sc, data.sdo_index, fp@2589: data.sdo_subindex, data.size); fp@2589: if (IS_ERR(req)) fp@2589: return PTR_ERR(req); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Create a register request. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_create_reg_request( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_request_t io; fp@2589: ec_slave_config_t *sc; fp@2589: ec_reg_request_t *reg; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: io.request_index = 0; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: sc = ec_master_get_config(master, io.config_index); fp@2589: if (!sc) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: list_for_each_entry(reg, &sc->reg_requests, list) { fp@2589: io.request_index++; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: reg = ecrt_slave_config_create_reg_request_err(sc, io.mem_size); fp@2589: if (IS_ERR(reg)) { fp@2589: return PTR_ERR(reg); fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Create a VoE handler. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_create_voe_handler( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: data.voe_index = 0; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) fp@2589: return -EINTR; fp@2589: fp@2589: sc = ec_master_get_config(master, data.config_index); fp@2589: if (!sc) { fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: list_for_each_entry(voe, &sc->voe_handlers, list) { fp@2589: data.voe_index++; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: voe = ecrt_slave_config_create_voe_handler_err(sc, data.size); fp@2589: if (IS_ERR(voe)) fp@2589: return PTR_ERR(voe); fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get the slave configuration's state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_state_t data; fp@2589: const ec_slave_config_t *sc; fp@2589: ec_slave_config_state_t state; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because sc will not be deleted in the fp@2589: * meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config_const(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_slave_config_state(sc, &state); fp@2589: fp@2589: if (copy_to_user((void __user *) data.state, &state, sizeof(state))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Configures an IDN. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sc_idn( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sc_idn_t ioctl; fp@2589: ec_slave_config_t *sc; fp@2589: uint8_t *data = NULL; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&ioctl, (void __user *) arg, sizeof(ioctl))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (!ioctl.size) fp@2589: return -EINVAL; fp@2589: fp@2589: if (!(data = kmalloc(ioctl.size, GFP_KERNEL))) { fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(data, (void __user *) ioctl.data, ioctl.size)) { fp@2589: kfree(data); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: kfree(data); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, ioctl.config_index))) { fp@2589: up(&master->master_sem); fp@2589: kfree(data); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: up(&master->master_sem); /** \todo sc could be invalidated */ fp@2589: fp@2589: ret = ecrt_slave_config_idn( fp@2589: sc, ioctl.drive_no, ioctl.idn, ioctl.al_state, data, ioctl.size); fp@2589: kfree(data); fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Gets the domain's data size. fp@2589: * fp@2589: * \return Domain size, or a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_size( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: const ec_domain_t *domain; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: list_for_each_entry(domain, &master->domains, list) { fp@2589: if (domain->index == (unsigned long) arg) { fp@2589: size_t size = ecrt_domain_size(domain); fp@2589: up(&master->master_sem); fp@2589: return size; fp@2589: } fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Gets the domain's offset in the total process data. fp@2589: * fp@2589: * \return Domain offset, or a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_offset( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: int offset = 0; fp@2589: const ec_domain_t *domain; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: list_for_each_entry(domain, &master->domains, list) { fp@2589: if (domain->index == (unsigned long) arg) { fp@2589: up(&master->master_sem); fp@2589: return offset; fp@2589: } fp@2589: offset += ecrt_domain_size(domain); fp@2589: } fp@2589: fp@2589: up(&master->master_sem); fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Process the domain. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_process( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_domain_t *domain; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: /* no locking of master_sem needed, because domain will not be deleted in fp@2589: * the meantime. */ fp@2589: fp@2589: if (!(domain = ec_master_find_domain(master, (unsigned long) arg))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_domain_process(domain); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Queue the domain. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_queue( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_domain_t *domain; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: /* no locking of master_sem needed, because domain will not be deleted in fp@2589: * the meantime. */ fp@2589: fp@2589: if (!(domain = ec_master_find_domain(master, (unsigned long) arg))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_domain_queue(domain); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Get the domain state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_domain_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_domain_state_t data; fp@2589: const ec_domain_t *domain; fp@2589: ec_domain_state_t state; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because domain will not be deleted in fp@2589: * the meantime. */ fp@2589: fp@2589: if (!(domain = ec_master_find_domain_const(master, data.domain_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_domain_state(domain, &state); fp@2589: fp@2589: if (copy_to_user((void __user *) data.state, &state, sizeof(state))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Sets an SDO request's SDO index and subindex. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sdo_request_index( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor req will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_find_sdo_request(sc, data.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_sdo_request_index(req, data.sdo_index, data.sdo_subindex); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Sets an SDO request's timeout. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sdo_request_timeout( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor req will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_find_sdo_request(sc, data.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_sdo_request_timeout(req, data.timeout); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Gets an SDO request's state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sdo_request_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor req will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_find_sdo_request(sc, data.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: data.state = ecrt_sdo_request_state(req); fp@2589: if (data.state == EC_REQUEST_SUCCESS && req->dir == EC_DIR_INPUT) fp@2589: data.size = ecrt_sdo_request_data_size(req); fp@2589: else fp@2589: data.size = 0; fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts an SDO read operation. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sdo_request_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor req will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_find_sdo_request(sc, data.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_sdo_request_read(req); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts an SDO write operation. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sdo_request_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: int ret; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (!data.size) { fp@2589: EC_MASTER_ERR(master, "SDO download: Data size may not be zero!\n"); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor req will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_find_sdo_request(sc, data.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ret = ec_sdo_request_alloc(req, data.size); fp@2589: if (ret) fp@2589: return ret; fp@2589: fp@2589: if (copy_from_user(req->data, (void __user *) data.data, data.size)) fp@2589: return -EFAULT; fp@2589: fp@2589: req->data_size = data.size; fp@2589: ecrt_sdo_request_write(req); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read SDO data. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_sdo_request_data( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_sdo_request_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_sdo_request_t *req; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor req will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(req = ec_slave_config_find_sdo_request(sc, data.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) data.data, ecrt_sdo_request_data(req), fp@2589: ecrt_sdo_request_data_size(req))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read register data. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_reg_request_data( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_request_t io; fp@2589: ec_slave_config_t *sc; fp@2589: ec_reg_request_t *reg; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: if (io.mem_size <= 0) { fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor reg will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(reg = ec_slave_config_find_reg_request(sc, io.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) io.data, ecrt_reg_request_data(reg), fp@2589: min(reg->mem_size, io.mem_size))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Gets an register request's state. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_reg_request_state( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_request_t io; fp@2589: ec_slave_config_t *sc; fp@2589: ec_reg_request_t *reg; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor reg will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(reg = ec_slave_config_find_reg_request(sc, io.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: io.state = ecrt_reg_request_state(reg); fp@2589: io.new_data = io.state == EC_REQUEST_SUCCESS && reg->dir == EC_DIR_INPUT; fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts an register write operation. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_reg_request_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_request_t io; fp@2589: ec_slave_config_t *sc; fp@2589: ec_reg_request_t *reg; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor reg will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(reg = ec_slave_config_find_reg_request(sc, io.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (io.transfer_size > reg->mem_size) { fp@2589: return -EOVERFLOW; fp@2589: } fp@2589: fp@2589: if (copy_from_user(reg->data, (void __user *) io.data, fp@2589: io.transfer_size)) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ecrt_reg_request_write(reg, io.address, io.transfer_size); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts an register read operation. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_reg_request_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_reg_request_t io; fp@2589: ec_slave_config_t *sc; fp@2589: ec_reg_request_t *reg; fp@2589: fp@2589: if (unlikely(!ctx->requested)) { fp@2589: return -EPERM; fp@2589: } fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor reg will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, io.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(reg = ec_slave_config_find_reg_request(sc, io.request_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (io.transfer_size > reg->mem_size) { fp@2589: return -EOVERFLOW; fp@2589: } fp@2589: fp@2589: ecrt_reg_request_read(reg, io.address, io.transfer_size); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Sets the VoE send header. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_send_header( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: uint32_t vendor_id; fp@2589: uint16_t vendor_type; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: if (get_user(vendor_id, data.vendor_id)) fp@2589: return -EFAULT; fp@2589: fp@2589: if (get_user(vendor_type, data.vendor_type)) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_voe_handler_send_header(voe, vendor_id, vendor_type); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Gets the received VoE header. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_rec_header( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: uint32_t vendor_id; fp@2589: uint16_t vendor_type; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_voe_handler_received_header(voe, &vendor_id, &vendor_type); fp@2589: fp@2589: if (likely(data.vendor_id)) fp@2589: if (put_user(vendor_id, data.vendor_id)) fp@2589: return -EFAULT; fp@2589: fp@2589: if (likely(data.vendor_type)) fp@2589: if (put_user(vendor_type, data.vendor_type)) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts a VoE read operation. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_voe_handler_read(voe); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts a VoE read operation without sending a sync message first. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_read_nosync( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: ecrt_voe_handler_read_nosync(voe); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Starts a VoE write operation. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (data.size) { fp@2589: if (data.size > ec_voe_handler_mem_size(voe)) fp@2589: return -EOVERFLOW; fp@2589: fp@2589: if (copy_from_user(ecrt_voe_handler_data(voe), fp@2589: (void __user *) data.data, data.size)) fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ecrt_voe_handler_write(voe, data.size); fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Executes the VoE state machine. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_exec( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: data.state = ecrt_voe_handler_execute(voe); fp@2589: if (data.state == EC_REQUEST_SUCCESS && voe->dir == EC_DIR_INPUT) fp@2589: data.size = ecrt_voe_handler_data_size(voe); fp@2589: else fp@2589: data.size = 0; fp@2589: fp@2589: if (copy_to_user((void __user *) arg, &data, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Reads the received VoE data. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_voe_data( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg, /**< ioctl() argument. */ fp@2589: ec_ioctl_context_t *ctx /**< Private data structure of file handle. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_voe_t data; fp@2589: ec_slave_config_t *sc; fp@2589: ec_voe_handler_t *voe; fp@2589: fp@2589: if (unlikely(!ctx->requested)) fp@2589: return -EPERM; fp@2589: fp@2589: if (copy_from_user(&data, (void __user *) arg, sizeof(data))) fp@2589: return -EFAULT; fp@2589: fp@2589: /* no locking of master_sem needed, because neither sc nor voe will not be fp@2589: * deleted in the meantime. */ fp@2589: fp@2589: if (!(sc = ec_master_get_config(master, data.config_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (!(voe = ec_slave_config_find_voe_handler(sc, data.voe_index))) { fp@2589: return -ENOENT; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) data.data, ecrt_voe_handler_data(voe), fp@2589: ecrt_voe_handler_data_size(voe))) fp@2589: return -EFAULT; fp@2589: fp@2589: return 0; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read a file from a slave via FoE. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_foe_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_foe_t io; fp@2589: ec_foe_request_t request; fp@2589: ec_slave_t *slave; fp@2589: int ret; fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ec_foe_request_init(&request, io.file_name); fp@2589: ret = ec_foe_request_alloc(&request, 10000); // FIXME fp@2589: if (ret) { fp@2589: ec_foe_request_clear(&request); fp@2589: return ret; fp@2589: } fp@2589: fp@2589: ec_foe_request_read(&request); fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ec_foe_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(slave = ec_master_find_slave(master, 0, io.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: ec_foe_request_clear(&request); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: io.slave_position); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: EC_SLAVE_DBG(slave, 1, "Scheduling FoE read request.\n"); fp@2589: fp@2589: // schedule request. fp@2589: list_add_tail(&request.list, &slave->foe_requests); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: // wait for processing through FSM fp@2589: if (wait_event_interruptible(master->request_queue, fp@2589: request.state != EC_INT_REQUEST_QUEUED)) { fp@2589: // interrupted by signal fp@2589: down(&master->master_sem); fp@2589: if (request.state == EC_INT_REQUEST_QUEUED) { fp@2589: list_del(&request.list); fp@2589: up(&master->master_sem); fp@2589: ec_foe_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: // request already processing: interrupt not possible. fp@2589: up(&master->master_sem); fp@2589: } fp@2589: fp@2589: // wait until master FSM has finished processing fp@2589: wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY); fp@2589: fp@2589: io.result = request.result; fp@2589: io.error_code = request.error_code; fp@2589: fp@2589: if (request.state != EC_INT_REQUEST_SUCCESS) { fp@2589: io.data_size = 0; fp@2589: ret = -EIO; fp@2589: } else { fp@2589: if (request.data_size > io.buffer_size) { fp@2589: EC_MASTER_ERR(master, "Buffer too small.\n"); fp@2589: ec_foe_request_clear(&request); fp@2589: return -EOVERFLOW; fp@2589: } fp@2589: io.data_size = request.data_size; fp@2589: if (copy_to_user((void __user *) io.buffer, fp@2589: request.buffer, io.data_size)) { fp@2589: ec_foe_request_clear(&request); fp@2589: return -EFAULT; fp@2589: } fp@2589: ret = 0; fp@2589: } fp@2589: fp@2589: if (__copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2589: ret = -EFAULT; fp@2589: } fp@2589: fp@2589: ec_foe_request_clear(&request); fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Write a file to a slave via FoE fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_foe_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_foe_t io; fp@2589: ec_foe_request_t request; fp@2589: ec_slave_t *slave; fp@2589: int ret; fp@2589: fp@2589: if (copy_from_user(&io, (void __user *) arg, sizeof(io))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: ec_foe_request_init(&request, io.file_name); fp@2589: fp@2589: ret = ec_foe_request_alloc(&request, io.buffer_size); fp@2589: if (ret) { fp@2589: ec_foe_request_clear(&request); fp@2589: return ret; fp@2589: } fp@2589: fp@2589: if (copy_from_user(request.buffer, fp@2589: (void __user *) io.buffer, io.buffer_size)) { fp@2589: ec_foe_request_clear(&request); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: request.data_size = io.buffer_size; fp@2589: ec_foe_request_write(&request); fp@2589: fp@2589: if (down_interruptible(&master->master_sem)) { fp@2589: ec_foe_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: fp@2589: if (!(slave = ec_master_find_slave(master, 0, io.slave_position))) { fp@2589: up(&master->master_sem); fp@2589: EC_MASTER_ERR(master, "Slave %u does not exist!\n", fp@2589: io.slave_position); fp@2589: ec_foe_request_clear(&request); fp@2589: return -EINVAL; fp@2589: } fp@2589: fp@2589: EC_SLAVE_DBG(slave, 1, "Scheduling FoE write request.\n"); fp@2589: fp@2589: // schedule FoE write request. fp@2589: list_add_tail(&request.list, &slave->foe_requests); fp@2589: fp@2589: up(&master->master_sem); fp@2589: fp@2589: // wait for processing through FSM fp@2589: if (wait_event_interruptible(master->request_queue, fp@2589: request.state != EC_INT_REQUEST_QUEUED)) { fp@2589: // interrupted by signal fp@2589: down(&master->master_sem); fp@2589: if (request.state == EC_INT_REQUEST_QUEUED) { fp@2589: // abort request fp@2589: list_del(&request.list); fp@2589: up(&master->master_sem); fp@2589: ec_foe_request_clear(&request); fp@2589: return -EINTR; fp@2589: } fp@2589: up(&master->master_sem); fp@2589: } fp@2589: fp@2589: // wait until master FSM has finished processing fp@2589: wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY); fp@2589: fp@2589: io.result = request.result; fp@2589: io.error_code = request.error_code; fp@2589: fp@2589: ret = request.state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO; fp@2589: fp@2589: if (__copy_to_user((void __user *) arg, &io, sizeof(io))) { fp@2589: ret = -EFAULT; fp@2589: } fp@2589: fp@2589: ec_foe_request_clear(&request); fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Read an SoE IDN. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_soe_read( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_soe_read_t ioctl; fp@2589: u8 *data; fp@2589: int retval; fp@2589: fp@2589: if (copy_from_user(&ioctl, (void __user *) arg, sizeof(ioctl))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: data = kmalloc(ioctl.mem_size, GFP_KERNEL); fp@2589: if (!data) { fp@2589: EC_MASTER_ERR(master, "Failed to allocate %zu bytes of IDN data.\n", fp@2589: ioctl.mem_size); fp@2589: return -ENOMEM; fp@2589: } fp@2589: fp@2589: retval = ecrt_master_read_idn(master, ioctl.slave_position, fp@2589: ioctl.drive_no, ioctl.idn, data, ioctl.mem_size, &ioctl.data_size, fp@2589: &ioctl.error_code); fp@2589: if (retval) { fp@2589: kfree(data); fp@2589: return retval; fp@2589: } fp@2589: fp@2589: if (copy_to_user((void __user *) ioctl.data, fp@2589: data, ioctl.data_size)) { fp@2589: kfree(data); fp@2589: return -EFAULT; fp@2589: } fp@2589: kfree(data); fp@2589: fp@2589: if (__copy_to_user((void __user *) arg, &ioctl, sizeof(ioctl))) { fp@2589: retval = -EFAULT; fp@2589: } fp@2589: fp@2589: EC_MASTER_DBG(master, 1, "Finished SoE read request.\n"); fp@2589: return retval; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** Write an IDN to a slave via SoE. fp@2589: * fp@2589: * \return Zero on success, otherwise a negative error code. fp@2589: */ fp@2589: static ATTRIBUTES int ec_ioctl_slave_soe_write( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: ec_ioctl_slave_soe_write_t ioctl; fp@2589: u8 *data; fp@2589: int retval; fp@2589: fp@2589: if (copy_from_user(&ioctl, (void __user *) arg, sizeof(ioctl))) { fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: data = kmalloc(ioctl.data_size, GFP_KERNEL); fp@2589: if (!data) { fp@2589: EC_MASTER_ERR(master, "Failed to allocate %zu bytes of IDN data.\n", fp@2589: ioctl.data_size); fp@2589: return -ENOMEM; fp@2589: } fp@2589: if (copy_from_user(data, (void __user *) ioctl.data, ioctl.data_size)) { fp@2589: kfree(data); fp@2589: return -EFAULT; fp@2589: } fp@2589: fp@2589: retval = ecrt_master_write_idn(master, ioctl.slave_position, fp@2589: ioctl.drive_no, ioctl.idn, data, ioctl.data_size, fp@2589: &ioctl.error_code); fp@2589: kfree(data); fp@2589: if (retval) { fp@2589: return retval; fp@2589: } fp@2589: fp@2589: if (__copy_to_user((void __user *) arg, &ioctl, sizeof(ioctl))) { fp@2589: retval = -EFAULT; fp@2589: } fp@2589: fp@2589: EC_MASTER_DBG(master, 1, "Finished SoE write request.\n"); fp@2589: return retval; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/ fp@2589: fp@2589: /** ioctl() function to use. fp@2589: */ fp@2589: #ifdef EC_IOCTL_RTDM fp@2589: #define EC_IOCTL ec_ioctl_rtdm fp@2589: #else fp@2589: #define EC_IOCTL ec_ioctl fp@2589: #endif fp@2589: fp@2589: /** Called when an ioctl() command is issued. fp@2589: * fp@2589: * \return ioctl() return code. fp@2589: */ fp@2589: long EC_IOCTL( fp@2589: ec_master_t *master, /**< EtherCAT master. */ fp@2589: ec_ioctl_context_t *ctx, /**< Device context. */ fp@2589: unsigned int cmd, /**< ioctl() command identifier. */ fp@2589: void *arg /**< ioctl() argument. */ fp@2589: ) fp@2589: { fp@2589: #if DEBUG_LATENCY fp@2589: cycles_t a = get_cycles(), b; fp@2589: unsigned int t; fp@2589: #endif fp@2589: int ret; fp@2589: fp@2589: switch (cmd) { fp@2589: case EC_IOCTL_MODULE: fp@2589: ret = ec_ioctl_module(arg); fp@2589: break; fp@2589: case EC_IOCTL_MASTER: fp@2589: ret = ec_ioctl_master(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE: fp@2589: ret = ec_ioctl_slave(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SYNC: fp@2589: ret = ec_ioctl_slave_sync(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SYNC_PDO: fp@2589: ret = ec_ioctl_slave_sync_pdo(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SYNC_PDO_ENTRY: fp@2589: ret = ec_ioctl_slave_sync_pdo_entry(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN: fp@2589: ret = ec_ioctl_domain(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_FMMU: fp@2589: ret = ec_ioctl_domain_fmmu(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_DATA: fp@2589: ret = ec_ioctl_domain_data(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_MASTER_DEBUG: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_master_debug(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_MASTER_RESCAN: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_master_rescan(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_STATE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_slave_state(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SDO: fp@2589: ret = ec_ioctl_slave_sdo(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SDO_ENTRY: fp@2589: ret = ec_ioctl_slave_sdo_entry(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SDO_UPLOAD: fp@2589: ret = ec_ioctl_slave_sdo_upload(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SDO_DOWNLOAD: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_slave_sdo_download(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SII_READ: fp@2589: ret = ec_ioctl_slave_sii_read(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SII_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_slave_sii_write(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_REG_READ: fp@2589: ret = ec_ioctl_slave_reg_read(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_REG_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_slave_reg_write(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_FOE_READ: fp@2589: ret = ec_ioctl_slave_foe_read(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_FOE_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_slave_foe_write(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_SLAVE_SOE_READ: fp@2589: ret = ec_ioctl_slave_soe_read(master, arg); fp@2589: break; fp@2597: case EC_IOCTL_SLAVE_EOE_IP_PARAM: fp@2597: if (!ctx->writable) { fp@2597: ret = -EPERM; fp@2597: break; fp@2597: } fp@2597: ret = ec_ioctl_slave_eoe_ip_param(master, arg); fp@2597: break; fp@2589: case EC_IOCTL_SLAVE_SOE_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_slave_soe_write(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_CONFIG: fp@2589: ret = ec_ioctl_config(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_CONFIG_PDO: fp@2589: ret = ec_ioctl_config_pdo(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_CONFIG_PDO_ENTRY: fp@2589: ret = ec_ioctl_config_pdo_entry(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_CONFIG_SDO: fp@2589: ret = ec_ioctl_config_sdo(master, arg); fp@2589: break; fp@2589: case EC_IOCTL_CONFIG_IDN: fp@2589: ret = ec_ioctl_config_idn(master, arg); fp@2589: break; fp@2589: #ifdef EC_EOE fp@2589: case EC_IOCTL_EOE_HANDLER: fp@2589: ret = ec_ioctl_eoe_handler(master, arg); fp@2589: break; fp@2589: #endif fp@2589: case EC_IOCTL_REQUEST: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_request(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_CREATE_DOMAIN: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_create_domain(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_CREATE_SLAVE_CONFIG: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_create_slave_config(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SELECT_REF_CLOCK: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_select_ref_clock(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_ACTIVATE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_activate(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_DEACTIVATE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_deactivate(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SEND: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_send(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_RECEIVE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_receive(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_MASTER_STATE: fp@2589: ret = ec_ioctl_master_state(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_MASTER_LINK_STATE: fp@2589: ret = ec_ioctl_master_link_state(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_APP_TIME: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_app_time(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SYNC_REF: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sync_ref(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SYNC_SLAVES: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sync_slaves(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_REF_CLOCK_TIME: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_ref_clock_time(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SYNC_MON_QUEUE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sync_mon_queue(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SYNC_MON_PROCESS: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sync_mon_process(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_RESET: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_reset(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_SYNC: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_sync(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_WATCHDOG: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_watchdog(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_ADD_PDO: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_add_pdo(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_CLEAR_PDOS: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_clear_pdos(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_ADD_ENTRY: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_add_entry(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_CLEAR_ENTRIES: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_clear_entries(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_REG_PDO_ENTRY: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_reg_pdo_entry(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_REG_PDO_POS: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_reg_pdo_pos(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_DC: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_dc(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_SDO: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_sdo(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_EMERG_SIZE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_emerg_size(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_EMERG_POP: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_emerg_pop(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_EMERG_CLEAR: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_emerg_clear(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_EMERG_OVERRUNS: fp@2589: ret = ec_ioctl_sc_emerg_overruns(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_SDO_REQUEST: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_create_sdo_request(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_REG_REQUEST: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_create_reg_request(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_VOE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_create_voe_handler(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_STATE: fp@2589: ret = ec_ioctl_sc_state(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SC_IDN: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sc_idn(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_SIZE: fp@2589: ret = ec_ioctl_domain_size(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_OFFSET: fp@2589: ret = ec_ioctl_domain_offset(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_PROCESS: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_domain_process(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_QUEUE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_domain_queue(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_DOMAIN_STATE: fp@2589: ret = ec_ioctl_domain_state(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SDO_REQUEST_INDEX: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sdo_request_index(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SDO_REQUEST_TIMEOUT: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sdo_request_timeout(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SDO_REQUEST_STATE: fp@2589: ret = ec_ioctl_sdo_request_state(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SDO_REQUEST_READ: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sdo_request_read(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SDO_REQUEST_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_sdo_request_write(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SDO_REQUEST_DATA: fp@2589: ret = ec_ioctl_sdo_request_data(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_REG_REQUEST_DATA: fp@2589: ret = ec_ioctl_reg_request_data(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_REG_REQUEST_STATE: fp@2589: ret = ec_ioctl_reg_request_state(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_REG_REQUEST_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_reg_request_write(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_REG_REQUEST_READ: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_reg_request_read(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_SEND_HEADER: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_voe_send_header(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_REC_HEADER: fp@2589: ret = ec_ioctl_voe_rec_header(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_READ: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_voe_read(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_READ_NOSYNC: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_voe_read_nosync(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_WRITE: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_voe_write(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_EXEC: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_voe_exec(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_VOE_DATA: fp@2589: ret = ec_ioctl_voe_data(master, arg, ctx); fp@2589: break; fp@2589: case EC_IOCTL_SET_SEND_INTERVAL: fp@2589: if (!ctx->writable) { fp@2589: ret = -EPERM; fp@2589: break; fp@2589: } fp@2589: ret = ec_ioctl_set_send_interval(master, arg, ctx); fp@2589: break; fp@2589: default: fp@2589: ret = -ENOTTY; fp@2589: break; fp@2589: } fp@2589: fp@2589: #if DEBUG_LATENCY fp@2589: b = get_cycles(); fp@2589: t = (unsigned int) ((b - a) * 1000LL) / cpu_khz; fp@2589: if (t > 50) { fp@2589: EC_MASTER_WARN(master, "ioctl(0x%02x) took %u us.\n", fp@2589: _IOC_NR(cmd), t); fp@2589: } fp@2589: #endif fp@2589: fp@2589: return ret; fp@2589: } fp@2589: fp@2589: /*****************************************************************************/