fp@1242: /****************************************************************************** fp@1266: * fp@1266: * $Id$ fp@1266: * fp@1266: * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH fp@1266: * fp@1266: * This file is part of the IgH EtherCAT master userspace library. fp@1266: * fp@1287: * The IgH EtherCAT master userspace library is free software; you can fp@1266: * redistribute it and/or modify it under the terms of the GNU Lesser General fp@1287: * Public License as published by the Free Software Foundation; version 2.1 of fp@1266: * the License. fp@1287: * fp@1266: * The IgH EtherCAT master userspace library is distributed in the hope that fp@1266: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied fp@1266: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1266: * GNU Lesser General Public License for more details. fp@1287: * fp@1266: * You should have received a copy of the GNU Lesser General Public License fp@1266: * along with the IgH EtherCAT master userspace library. If not, see fp@1266: * . fp@1266: * fp@1266: * The right to use EtherCAT Technology is granted and comes free of charge fp@1266: * under condition of compatibility of product made by Licensee. People fp@1266: * intending to distribute/sell products based on the code, have to sign an fp@1266: * agreement to guarantee that products using software based on IgH EtherCAT fp@1266: * master stay compatible with the actual EtherCAT specification (which are fp@1266: * released themselves as an open standard) as the (only) precondition to have fp@1266: * the right to use EtherCAT Technology, IP and trade marks. 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@1242: #include fp@1244: #include fp@1258: #include fp@1242: fp@1244: #include "master.h" fp@1242: #include "master/ioctl.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@1242: #define MAX_PATH_LEN 64 fp@1242: fp@1242: ec_master_t *ecrt_request_master(unsigned int master_index) fp@1242: { fp@1242: char path[MAX_PATH_LEN]; fp@1242: ec_master_t *master; fp@1242: fp@1242: master = malloc(sizeof(ec_master_t)); fp@1242: if (!master) { fp@1242: fprintf(stderr, "Failed to allocate memory.\n"); fp@1242: return 0; fp@1242: } fp@1242: fp@1258: master->process_data = NULL; fp@1258: master->process_data_size = 0; fp@1258: fp@1242: snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index); fp@1242: fp@1242: master->fd = open(path, O_RDWR); fp@1242: if (master->fd == -1) { fp@1242: fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); fp@1242: free(master); fp@1242: return 0; fp@1242: } fp@1242: fp@1244: if (ioctl(master->fd, EC_IOCTL_REQUEST, NULL) == -1) { fp@1242: fprintf(stderr, "Failed to request master %u: %s\n", fp@1242: master_index, strerror(errno)); fp@1242: close(master->fd); fp@1242: free(master); fp@1242: return 0; fp@1242: } fp@1242: fp@1242: return master; fp@1242: } fp@1242: fp@1242: /*****************************************************************************/ fp@1242: fp@1242: void ecrt_release_master(ec_master_t *master) fp@1242: { fp@1258: if (master->process_data) { fp@1258: munmap(master->process_data, master->process_data_size); fp@1258: } fp@1258: fp@1242: close(master->fd); fp@1242: free(master); fp@1242: } fp@1242: fp@1242: /*****************************************************************************/