fp@1242: /****************************************************************************** fp@2383: * fp@1326: * $Id$ fp@2383: * fp@2433: * Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH fp@2383: * fp@1326: * This file is part of the IgH EtherCAT master userspace library. fp@2383: * fp@1326: * The IgH EtherCAT master userspace library is free software; you can fp@1326: * redistribute it and/or modify it under the terms of the GNU Lesser General fp@1326: * Public License as published by the Free Software Foundation; version 2.1 fp@1326: * of the License. fp@1287: * fp@1326: * The IgH EtherCAT master userspace library is distributed in the hope that fp@1326: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied fp@1326: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1326: * GNU Lesser General Public License for more details. fp@1287: * fp@1326: * You should have received a copy of the GNU Lesser General Public License fp@1326: * along with the IgH EtherCAT master userspace library. If not, see fp@1326: * . fp@2383: * fp@1363: * --- fp@2383: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1242: * fp@1242: *****************************************************************************/ fp@1242: fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1258: #include fp@1975: #include fp@1975: #include fp@1242: fp@2433: #include "ioctl.h" fp@1244: #include "master.h" fp@1242: fp@1244: /*****************************************************************************/ fp@1244: fp@1244: unsigned int ecrt_version_magic(void) fp@1244: { fp@1244: return ECRT_VERSION_MAGIC; fp@1244: } fp@1242: fp@1242: /*****************************************************************************/ fp@1242: fp@1497: ec_master_t *ecrt_request_master(unsigned int master_index) fp@1497: { fp@1497: ec_master_t *master = ecrt_open_master(master_index); fp@1497: if (master) { fp@1497: if (ecrt_master_reserve(master) < 0) { fp@1959: ec_master_clear(master); fp@1497: free(master); fp@1959: master = NULL; fp@1497: } fp@1497: } fp@1497: fp@1497: return master; fp@1497: } fp@1497: fp@1497: /*****************************************************************************/ fp@1497: fp@1495: #define MAX_PATH_LEN 64 fp@1495: fp@1497: ec_master_t *ecrt_open_master(unsigned int master_index) fp@1242: { fp@1495: char path[MAX_PATH_LEN]; fp@1826: ec_master_t *master = NULL; fp@1826: ec_ioctl_module_t module_data; fp@2433: int ret; fp@1495: fp@1495: master = malloc(sizeof(ec_master_t)); fp@1495: if (!master) { fp@1495: fprintf(stderr, "Failed to allocate memory.\n"); fp@1495: return 0; fp@1495: } fp@1495: fp@1495: master->process_data = NULL; fp@1495: master->process_data_size = 0; fp@1959: master->first_domain = NULL; fp@1959: master->first_config = NULL; fp@1495: fp@2433: snprintf(path, MAX_PATH_LEN - 1, fp@2433: "/dev/EtherCAT%u", fp@2433: master_index); fp@1495: fp@1495: master->fd = open(path, O_RDWR); fp@2433: if (EC_IOCTL_IS_ERROR(master->fd)) { fp@2433: fprintf(stderr, "Failed to open %s: %s\n", path, fp@2433: strerror(EC_IOCTL_ERRNO(master->fd))); fp@1959: goto out_clear; fp@1826: } fp@1826: fp@2433: ret = ioctl(master->fd, EC_IOCTL_MODULE, &module_data); fp@2433: if (EC_IOCTL_IS_ERROR(ret)) { fp@1826: fprintf(stderr, "Failed to get module information from %s: %s\n", fp@2433: path, strerror(EC_IOCTL_ERRNO(ret))); fp@1959: goto out_clear; fp@1826: } fp@1826: fp@1826: if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) { fp@1826: fprintf(stderr, "ioctl() version magic is differing:" fp@1826: " %s: %u, libethercat: %u.\n", fp@1826: path, module_data.ioctl_version_magic, fp@1826: EC_IOCTL_VERSION_MAGIC); fp@1959: goto out_clear; fp@1495: } fp@1495: fp@1242: return master; fp@1826: fp@1959: out_clear: fp@1959: ec_master_clear(master); fp@1826: free(master); fp@1826: return 0; fp@1242: } fp@1242: fp@1242: /*****************************************************************************/ fp@1242: fp@1242: void ecrt_release_master(ec_master_t *master) fp@1242: { fp@1959: ec_master_clear(master); fp@1242: free(master); fp@1242: } fp@1242: fp@1242: /*****************************************************************************/