master/module.c
changeset 919 d30959d74f54
parent 840 64beeb6ce8e3
child 922 fede1d8f5b71
equal deleted inserted replaced
918:251e1e0d272c 919:d30959d74f54
    42 #include <linux/init.h>
    42 #include <linux/init.h>
    43 
    43 
    44 #include "globals.h"
    44 #include "globals.h"
    45 #include "master.h"
    45 #include "master.h"
    46 #include "device.h"
    46 #include "device.h"
    47 #include "xmldev.h"
       
    48 
    47 
    49 /*****************************************************************************/
    48 /*****************************************************************************/
    50 
    49 
    51 #define MAX_MASTERS 5 /**< Maximum number of masters. */
    50 #define MAX_MASTERS 5 /**< Maximum number of masters. */
    52 
    51 
    69 static unsigned int master_count; /**< Number of masters. */
    68 static unsigned int master_count; /**< Number of masters. */
    70 static unsigned int backup_count; /**< Number of backup devices. */
    69 static unsigned int backup_count; /**< Number of backup devices. */
    71 
    70 
    72 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    71 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    73 
    72 
    74 static dev_t device_number; /**< XML character device number. */
       
    75 ec_xmldev_t xmldev; /**< XML character device. */
       
    76 
       
    77 char *ec_master_version_str = EC_MASTER_VERSION; /**< Version string. */
    73 char *ec_master_version_str = EC_MASTER_VERSION; /**< Version string. */
    78 
    74 
    79 /*****************************************************************************/
    75 /*****************************************************************************/
    80 
    76 
    81 /** \cond */
    77 /** \cond */
   121         EC_ERR("Failed to add module kobject.\n");
   117         EC_ERR("Failed to add module kobject.\n");
   122         ret = -EEXIST;
   118         ret = -EEXIST;
   123         goto out_put;
   119         goto out_put;
   124     }
   120     }
   125     
   121     
   126     if (alloc_chrdev_region(&device_number, 0, 1, "EtherCAT")) {
       
   127         EC_ERR("Failed to obtain device number!\n");
       
   128         ret = -EBUSY;
       
   129         goto out_del;
       
   130     }
       
   131 
       
   132     // zero MAC addresses
   122     // zero MAC addresses
   133     memset(macs, 0x00, sizeof(uint8_t) * MAX_MASTERS * 2 * ETH_ALEN);
   123     memset(macs, 0x00, sizeof(uint8_t) * MAX_MASTERS * 2 * ETH_ALEN);
   134 
   124 
   135     // process MAC parameters
   125     // process MAC parameters
   136     for (i = 0; i < master_count; i++) {
   126     for (i = 0; i < master_count; i++) {
   137         if (ec_mac_parse(macs[i][0], main_devices[i], 0)) {
   127         if (ec_mac_parse(macs[i][0], main_devices[i], 0)) {
   138             ret = -EINVAL;
   128             ret = -EINVAL;
   139             goto out_cdev;
   129             goto out_del;
   140         }
   130         }
   141         
   131         
   142         if (i < backup_count && ec_mac_parse(macs[i][1], backup_devices[i], 1)) {
   132         if (i < backup_count && ec_mac_parse(macs[i][1], backup_devices[i], 1)) {
   143             ret = -EINVAL;
   133             ret = -EINVAL;
   144             goto out_cdev;
   134             goto out_del;
   145         }
   135         }
   146     }
   136     }
   147     
   137     
   148     if (master_count) {
   138     if (master_count) {
   149         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,
   139         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,
   150                         GFP_KERNEL))) {
   140                         GFP_KERNEL))) {
   151             EC_ERR("Failed to allocate memory for EtherCAT masters.\n");
   141             EC_ERR("Failed to allocate memory for EtherCAT masters.\n");
   152             ret = -ENOMEM;
   142             ret = -ENOMEM;
   153             goto out_cdev;
   143             goto out_del;
   154         }
   144         }
   155     }
   145     }
   156     
   146     
   157     for (i = 0; i < master_count; i++) {
   147     for (i = 0; i < master_count; i++) {
   158         if (ec_master_init(&masters[i], &kobj, i, macs[i][0], macs[i][1])) {
   148         if (ec_master_init(&masters[i], &kobj, i, macs[i][0], macs[i][1])) {
   166     return ret;
   156     return ret;
   167 
   157 
   168 out_free_masters:
   158 out_free_masters:
   169     for (i--; i >= 0; i--) ec_master_clear(&masters[i]);
   159     for (i--; i >= 0; i--) ec_master_clear(&masters[i]);
   170     kfree(masters);
   160     kfree(masters);
   171 out_cdev:
       
   172     unregister_chrdev_region(device_number, 1);
       
   173 out_del:
   161 out_del:
   174     kobject_del(&kobj);
   162     kobject_del(&kobj);
   175 out_put:
   163 out_put:
   176     kobject_put(&kobj);
   164     kobject_put(&kobj);
   177     return ret;
   165     return ret;
   191         ec_master_clear(&masters[i]);
   179         ec_master_clear(&masters[i]);
   192     }
   180     }
   193     if (master_count)
   181     if (master_count)
   194         kfree(masters);
   182         kfree(masters);
   195     
   183     
   196     unregister_chrdev_region(device_number, 1);
       
   197     kobject_del(&kobj);
   184     kobject_del(&kobj);
   198     kobject_put(&kobj);
   185     kobject_put(&kobj);
   199 
   186 
   200     EC_INFO("Master module cleaned up.\n");
   187     EC_INFO("Master module cleaned up.\n");
   201 }
   188 }