master/xmldev.c
changeset 361 29af81543ce1
child 362 ae38aeb6fde9
equal deleted inserted replaced
360:a7a8ed41b50a 361:29af81543ce1
       
     1 /******************************************************************************
       
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     9  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    10  *  and/or modify it under the terms of the GNU General Public License
       
    11  *  as published by the Free Software Foundation; either version 2 of the
       
    12  *  License, or (at your option) any later version.
       
    13  *
       
    14  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    15  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  *  GNU General Public License for more details.
       
    18  *
       
    19  *  You should have received a copy of the GNU General Public License
       
    20  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    22  *
       
    23  *  The right to use EtherCAT Technology is granted and comes free of
       
    24  *  charge under condition of compatibility of product made by
       
    25  *  Licensee. People intending to distribute/sell products based on the
       
    26  *  code, have to sign an agreement to guarantee that products using
       
    27  *  software based on IgH EtherCAT master stay compatible with the actual
       
    28  *  EtherCAT specification (which are released themselves as an open
       
    29  *  standard) as the (only) precondition to have the right to use EtherCAT
       
    30  *  Technology, IP and trade marks.
       
    31  *
       
    32  *****************************************************************************/
       
    33 
       
    34 /**
       
    35    \file
       
    36    EtherCAT XML device.
       
    37 */
       
    38 
       
    39 /*****************************************************************************/
       
    40 
       
    41 #include <linux/module.h>
       
    42 
       
    43 #include "master.h"
       
    44 #include "xmldev.h"
       
    45 
       
    46 /*****************************************************************************/
       
    47 
       
    48 static char *test_str = "hello world!\n";
       
    49 
       
    50 int ecxmldev_open(struct inode *, struct file *);
       
    51 int ecxmldev_release(struct inode *, struct file *);
       
    52 ssize_t ecxmldev_read(struct file *, char __user *, size_t, loff_t *);
       
    53 ssize_t ecxmldev_write(struct file *, const char __user *, size_t, loff_t *);
       
    54 
       
    55 /*****************************************************************************/
       
    56 
       
    57 static struct file_operations fops = {
       
    58     .owner   = THIS_MODULE,
       
    59     .open    = ecxmldev_open,
       
    60     .release = ecxmldev_release,
       
    61     .read    = ecxmldev_read,
       
    62     .write   = ecxmldev_write
       
    63 };
       
    64 
       
    65 /*****************************************************************************/
       
    66 
       
    67 /**
       
    68    XML device constructor.
       
    69    \return 0 in case of success, else < 0
       
    70 */
       
    71 
       
    72 int ec_xmldev_init(ec_xmldev_t *xmldev, /**< EtherCAT XML device */
       
    73                    ec_master_t *master, /**< EtherCAT master */
       
    74                    dev_t dev_num /**< device number */
       
    75                    )
       
    76 {
       
    77     cdev_init(&xmldev->cdev, &fops);
       
    78     xmldev->cdev.owner = THIS_MODULE;
       
    79     if (cdev_add(&xmldev->cdev,
       
    80 		 MKDEV(MAJOR(dev_num), master->index), 1)) {
       
    81 	EC_ERR("Failed to add character device!\n");
       
    82 	return -1;
       
    83     }
       
    84     return 0;
       
    85 }
       
    86 
       
    87 /*****************************************************************************/
       
    88 
       
    89 /**
       
    90    XML device destructor.
       
    91 */
       
    92 
       
    93 void ec_xmldev_clear(ec_xmldev_t *xmldev /**< EtherCAT XML device */)
       
    94 {
       
    95     cdev_del(&xmldev->cdev);
       
    96 }
       
    97 
       
    98 /*****************************************************************************/
       
    99 
       
   100 int ecxmldev_open(struct inode *inode, struct file *filp)
       
   101 {
       
   102     ec_xmldev_t *xmldev;
       
   103 
       
   104     xmldev = container_of(inode->i_cdev, ec_xmldev_t, cdev);
       
   105     filp->private_data = xmldev;
       
   106 
       
   107     EC_DBG("File opened.\n");
       
   108 
       
   109     return 0;
       
   110 }
       
   111 
       
   112 /*****************************************************************************/
       
   113 
       
   114 int ecxmldev_release(struct inode *inode, struct file *filp)
       
   115 {
       
   116     EC_DBG("File closed.\n");
       
   117     return 0;
       
   118 }
       
   119 
       
   120 /*****************************************************************************/
       
   121 
       
   122 ssize_t ecxmldev_read(struct file *filp, char __user *buf,
       
   123 		      size_t count, loff_t *f_pos)
       
   124 {
       
   125     size_t len = strlen(test_str);
       
   126 
       
   127     if (*f_pos >= len) return 0;
       
   128     if (*f_pos + count > len) count = len - *f_pos;
       
   129 
       
   130     if (copy_to_user(buf, test_str + *f_pos, count)) return -EFAULT;
       
   131 
       
   132     *f_pos += count;
       
   133 
       
   134     return count;
       
   135 }
       
   136 
       
   137 /*****************************************************************************/
       
   138 
       
   139 ssize_t ecxmldev_write(struct file *filp,
       
   140 		       const char __user *buf,
       
   141 		       size_t count,
       
   142 		       loff_t *f_pos)
       
   143 {
       
   144     return -EFAULT;
       
   145 }
       
   146 
       
   147 /*****************************************************************************/