fp@922: /****************************************************************************** fp@922: * fp@922: * $Id$ fp@922: * fp@922: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@922: * fp@922: * This file is part of the IgH EtherCAT Master. fp@922: * fp@922: * The IgH EtherCAT Master is free software; you can redistribute it fp@922: * and/or modify it under the terms of the GNU General Public License fp@922: * as published by the Free Software Foundation; either version 2 of the fp@922: * License, or (at your option) any later version. fp@922: * fp@922: * The IgH EtherCAT Master is distributed in the hope that it will be fp@922: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@922: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@922: * GNU General Public License for more details. fp@922: * fp@922: * You should have received a copy of the GNU General Public License fp@922: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@922: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@922: * fp@922: * The right to use EtherCAT Technology is granted and comes free of fp@922: * charge under condition of compatibility of product made by fp@922: * Licensee. People intending to distribute/sell products based on the fp@922: * code, have to sign an agreement to guarantee that products using fp@922: * software based on IgH EtherCAT master stay compatible with the actual fp@922: * EtherCAT specification (which are released themselves as an open fp@922: * standard) as the (only) precondition to have the right to use EtherCAT fp@922: * Technology, IP and trade marks. fp@922: * fp@922: *****************************************************************************/ fp@922: fp@922: /** fp@922: \file fp@922: EtherCAT master character device. fp@922: */ fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: #include fp@922: fp@922: #include "cdev.h" fp@922: #include "master.h" fp@922: #include "ioctl.h" fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: /** \cond */ fp@922: fp@922: int eccdev_open(struct inode *, struct file *); fp@922: int eccdev_release(struct inode *, struct file *); fp@922: ssize_t eccdev_read(struct file *, char __user *, size_t, loff_t *); fp@922: ssize_t eccdev_write(struct file *, const char __user *, size_t, loff_t *); fp@922: int eccdev_ioctl(struct inode *, struct file *, unsigned int, unsigned long); fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: static struct file_operations eccdev_fops = { fp@922: .owner = THIS_MODULE, fp@922: .open = eccdev_open, fp@922: .release = eccdev_release, fp@922: .ioctl = eccdev_ioctl fp@922: }; fp@922: fp@922: /** \endcond */ fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: /** Constructor. fp@922: * fp@922: * \return 0 in case of success, else < 0 fp@922: */ fp@922: int ec_cdev_init( fp@922: ec_cdev_t *cdev, /**< EtherCAT master character device. */ fp@922: ec_master_t *master, /**< Parent master. */ fp@922: dev_t dev_num /**< Device number. */ fp@922: ) fp@922: { fp@922: cdev->master = master; fp@922: fp@922: cdev_init(&cdev->cdev, &eccdev_fops); fp@922: cdev->cdev.owner = THIS_MODULE; fp@922: fp@922: if (cdev_add(&cdev->cdev, fp@922: MKDEV(MAJOR(dev_num), master->index), 1)) { fp@922: EC_ERR("Failed to add character device!\n"); fp@922: return -1; fp@922: } fp@922: fp@922: return 0; fp@922: } fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: /** Destructor. fp@922: */ fp@922: void ec_cdev_clear(ec_cdev_t *cdev /**< EtherCAT XML device */) fp@922: { fp@922: cdev_del(&cdev->cdev); fp@922: } fp@922: fp@922: /****************************************************************************** fp@922: * File operations fp@922: *****************************************************************************/ fp@922: fp@922: int eccdev_open(struct inode *inode, struct file *filp) fp@922: { fp@922: ec_cdev_t *cdev = container_of(inode->i_cdev, ec_cdev_t, cdev); fp@922: fp@922: filp->private_data = cdev; fp@922: EC_DBG("File opened.\n"); fp@922: return 0; fp@922: } fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: int eccdev_release(struct inode *inode, struct file *filp) fp@922: { fp@922: //ec_cdev_t *cdev = container_of(inode->i_cdev, ec_cdev_t, cdev); fp@922: fp@922: EC_DBG("File closed.\n"); fp@922: return 0; fp@922: } fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: int eccdev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, fp@922: unsigned long arg) fp@922: { fp@922: ec_cdev_t *cdev = container_of(inode->i_cdev, ec_cdev_t, cdev); fp@922: ec_master_t *master = cdev->master; fp@922: fp@922: if (master->debug_level) fp@922: EC_DBG("ioctl(inode = %x, filp = %x, cmd = %u, arg = %u)\n", fp@922: (u32) inode, (u32) filp, (u32) cmd, (u32) arg); fp@922: fp@922: switch (cmd) { fp@922: case EC_IOCTL_SLAVE_COUNT: fp@922: { fp@922: unsigned int slave_count = master->slave_count; fp@922: EC_INFO("EC_IOCTL_SLAVE_COUNT\n"); fp@922: if (!arg) fp@922: return -EFAULT; fp@922: if (copy_to_user((void __user *) arg, &slave_count, fp@922: sizeof(unsigned int))) fp@922: return -EFAULT; fp@922: return 0; fp@922: } fp@922: fp@922: case EC_IOCTL_SLAVE_INFO: fp@922: { fp@922: struct ec_ioctl_slave_info *infos, *info; fp@922: unsigned int slave_count = master->slave_count; fp@922: const ec_slave_t *slave; fp@922: unsigned int i = 0; fp@922: fp@922: if (master->debug_level) fp@922: EC_DBG("EC_IOCTL_SLAVE_INFOS\n"); fp@922: fp@922: if (!slave_count) fp@922: return 0; fp@922: fp@922: if (!arg) fp@922: return -EFAULT; fp@922: fp@922: if (!(infos = kmalloc(slave_count * fp@922: sizeof(struct ec_ioctl_slave_info), fp@922: GFP_KERNEL))) fp@922: return -ENOMEM; fp@922: fp@922: list_for_each_entry(slave, &master->slaves, list) { fp@922: info = &infos[i++]; fp@922: info->vendor_id = slave->sii.vendor_id; fp@922: info->product_code = slave->sii.product_code; fp@922: info->alias = slave->sii.alias; fp@922: info->ring_position = slave->ring_position; fp@922: info->state = slave->current_state; fp@922: if (slave->sii.name) { fp@922: strncpy(info->description, slave->sii.name, fp@922: EC_IOCTL_SLAVE_INFO_DESC_SIZE); fp@922: info->description[EC_IOCTL_SLAVE_INFO_DESC_SIZE - 1] fp@922: = 0; fp@922: } else { fp@922: info->description[0] = 0; fp@922: } fp@922: } fp@922: fp@922: if (copy_to_user((void __user *) arg, infos, slave_count * fp@922: sizeof(struct ec_ioctl_slave_info))) { fp@922: kfree(infos); fp@922: return -EFAULT; fp@922: } fp@922: fp@922: kfree(infos); fp@922: return 0; fp@922: } fp@922: fp@922: default: fp@922: return -ENOIOCTLCMD; fp@922: } fp@922: } fp@922: fp@922: /*****************************************************************************/