master/module.c
changeset 922 fede1d8f5b71
parent 919 d30959d74f54
child 954 4e7be3b24622
equal deleted inserted replaced
921:c8c2caf0d667 922:fede1d8f5b71
    38 /*****************************************************************************/
    38 /*****************************************************************************/
    39 
    39 
    40 #include <linux/module.h>
    40 #include <linux/module.h>
    41 #include <linux/kernel.h>
    41 #include <linux/kernel.h>
    42 #include <linux/init.h>
    42 #include <linux/init.h>
       
    43 #include <linux/fs.h>
    43 
    44 
    44 #include "globals.h"
    45 #include "globals.h"
    45 #include "master.h"
    46 #include "master.h"
    46 #include "device.h"
    47 #include "device.h"
    47 
    48 
    66 static ec_master_t *masters; /**< Array of masters. */
    67 static ec_master_t *masters; /**< Array of masters. */
    67 static struct semaphore master_sem; /**< Master semaphore. */
    68 static struct semaphore master_sem; /**< Master semaphore. */
    68 static unsigned int master_count; /**< Number of masters. */
    69 static unsigned int master_count; /**< Number of masters. */
    69 static unsigned int backup_count; /**< Number of backup devices. */
    70 static unsigned int backup_count; /**< Number of backup devices. */
    70 
    71 
       
    72 static dev_t device_number; /**< Device number for cdevs. */
       
    73 
    71 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    74 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    72 
    75 
    73 char *ec_master_version_str = EC_MASTER_VERSION; /**< Version string. */
    76 char *ec_master_version_str = EC_MASTER_VERSION; /**< Version string. */
    74 
    77 
    75 /*****************************************************************************/
    78 /*****************************************************************************/
    90 
    93 
    91 /*****************************************************************************/
    94 /*****************************************************************************/
    92 
    95 
    93 /** Module initialization.
    96 /** Module initialization.
    94  *
    97  *
    95  * Initializes \a ec_master_count masters.
    98  * Initializes \a master_count masters.
    96  * \return 0 on success, else < 0
    99  * \return 0 on success, else < 0
    97  */
   100  */
    98 int __init ec_init_module(void)
   101 int __init ec_init_module(void)
    99 {
   102 {
   100     int i, ret = 0;
   103     int i, ret = 0;
   117         EC_ERR("Failed to add module kobject.\n");
   120         EC_ERR("Failed to add module kobject.\n");
   118         ret = -EEXIST;
   121         ret = -EEXIST;
   119         goto out_put;
   122         goto out_put;
   120     }
   123     }
   121     
   124     
       
   125     if (alloc_chrdev_region(&device_number, 0, master_count, "EtherCAT")) {
       
   126         EC_ERR("Failed to obtain device number(s)!\n");
       
   127         ret = -EBUSY;
       
   128         goto out_del;
       
   129     }
       
   130 
   122     // zero MAC addresses
   131     // zero MAC addresses
   123     memset(macs, 0x00, sizeof(uint8_t) * MAX_MASTERS * 2 * ETH_ALEN);
   132     memset(macs, 0x00, sizeof(uint8_t) * MAX_MASTERS * 2 * ETH_ALEN);
   124 
   133 
   125     // process MAC parameters
   134     // process MAC parameters
   126     for (i = 0; i < master_count; i++) {
   135     for (i = 0; i < master_count; i++) {
   127         if (ec_mac_parse(macs[i][0], main_devices[i], 0)) {
   136         if (ec_mac_parse(macs[i][0], main_devices[i], 0)) {
   128             ret = -EINVAL;
   137             ret = -EINVAL;
   129             goto out_del;
   138             goto out_cdev;
   130         }
   139         }
   131         
   140         
   132         if (i < backup_count && ec_mac_parse(macs[i][1], backup_devices[i], 1)) {
   141         if (i < backup_count && ec_mac_parse(macs[i][1], backup_devices[i], 1)) {
   133             ret = -EINVAL;
   142             ret = -EINVAL;
   134             goto out_del;
   143             goto out_cdev;
   135         }
   144         }
   136     }
   145     }
   137     
   146     
   138     if (master_count) {
   147     if (master_count) {
   139         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,
   148         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,
   140                         GFP_KERNEL))) {
   149                         GFP_KERNEL))) {
   141             EC_ERR("Failed to allocate memory for EtherCAT masters.\n");
   150             EC_ERR("Failed to allocate memory for EtherCAT masters.\n");
   142             ret = -ENOMEM;
   151             ret = -ENOMEM;
   143             goto out_del;
   152             goto out_cdev;
   144         }
   153         }
   145     }
   154     }
   146     
   155     
   147     for (i = 0; i < master_count; i++) {
   156     for (i = 0; i < master_count; i++) {
   148         if (ec_master_init(&masters[i], &kobj, i, macs[i][0], macs[i][1])) {
   157         if (ec_master_init(&masters[i], &kobj, i, macs[i][0], macs[i][1],
       
   158                 device_number)) {
   149             ret = -EIO;
   159             ret = -EIO;
   150             goto out_free_masters;
   160             goto out_free_masters;
   151         }
   161         }
   152     }
   162     }
   153     
   163     
   156     return ret;
   166     return ret;
   157 
   167 
   158 out_free_masters:
   168 out_free_masters:
   159     for (i--; i >= 0; i--) ec_master_clear(&masters[i]);
   169     for (i--; i >= 0; i--) ec_master_clear(&masters[i]);
   160     kfree(masters);
   170     kfree(masters);
       
   171 out_cdev:
       
   172     unregister_chrdev_region(device_number, master_count);
   161 out_del:
   173 out_del:
   162     kobject_del(&kobj);
   174     kobject_del(&kobj);
   163 out_put:
   175 out_put:
   164     kobject_put(&kobj);
   176     kobject_put(&kobj);
   165     return ret;
   177     return ret;
   178     for (i = 0; i < master_count; i++) {
   190     for (i = 0; i < master_count; i++) {
   179         ec_master_clear(&masters[i]);
   191         ec_master_clear(&masters[i]);
   180     }
   192     }
   181     if (master_count)
   193     if (master_count)
   182         kfree(masters);
   194         kfree(masters);
       
   195 
       
   196     unregister_chrdev_region(device_number, master_count);
   183     
   197     
   184     kobject_del(&kobj);
   198     kobject_del(&kobj);
   185     kobject_put(&kobj);
   199     kobject_put(&kobj);
   186 
   200 
   187     EC_INFO("Master module cleaned up.\n");
   201     EC_INFO("Master module cleaned up.\n");