master/module.c
changeset 1012 89f87a901ce5
parent 1011 a0759d0dded4
child 1013 52256b75f975
equal deleted inserted replaced
1011:a0759d0dded4 1012:89f87a901ce5
    36  */
    36  */
    37 
    37 
    38 /*****************************************************************************/
    38 /*****************************************************************************/
    39 
    39 
    40 #include <linux/module.h>
    40 #include <linux/module.h>
       
    41 #include <linux/sysfs.h>
    41 
    42 
    42 #include "globals.h"
    43 #include "globals.h"
    43 #include "master.h"
    44 #include "master.h"
    44 #include "device.h"
    45 #include "device.h"
    45 
    46 
    63 static struct semaphore master_sem; /**< Master semaphore. */
    64 static struct semaphore master_sem; /**< Master semaphore. */
    64 static unsigned int master_count; /**< Number of masters. */
    65 static unsigned int master_count; /**< Number of masters. */
    65 static unsigned int backup_count; /**< Number of backup devices. */
    66 static unsigned int backup_count; /**< Number of backup devices. */
    66 
    67 
    67 dev_t device_number; /**< Device number for master cdevs. */
    68 dev_t device_number; /**< Device number for master cdevs. */
       
    69 struct class *class; /**< Device class. */
    68 
    70 
    69 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    71 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    70 
    72 
    71 char *ec_master_version_str = EC_MASTER_VERSION; /**< Version string. */
    73 char *ec_master_version_str = EC_MASTER_VERSION; /**< Version string. */
    72 
    74 
   107             ret = -EBUSY;
   109             ret = -EBUSY;
   108             goto out_return;
   110             goto out_return;
   109         }
   111         }
   110     }
   112     }
   111 
   113 
       
   114     class = class_create(THIS_MODULE, "EtherCAT");
       
   115     if (IS_ERR(class)) {
       
   116         EC_ERR("Failed to create device class.\n");
       
   117         goto out_cdev;
       
   118     }
       
   119 
   112     // zero MAC addresses
   120     // zero MAC addresses
   113     memset(macs, 0x00, sizeof(uint8_t) * MAX_MASTERS * 2 * ETH_ALEN);
   121     memset(macs, 0x00, sizeof(uint8_t) * MAX_MASTERS * 2 * ETH_ALEN);
   114 
   122 
   115     // process MAC parameters
   123     // process MAC parameters
   116     for (i = 0; i < master_count; i++) {
   124     for (i = 0; i < master_count; i++) {
   117         if (ec_mac_parse(macs[i][0], main_devices[i], 0)) {
   125         if (ec_mac_parse(macs[i][0], main_devices[i], 0)) {
   118             ret = -EINVAL;
   126             ret = -EINVAL;
   119             goto out_cdev;
   127             goto out_class;
   120         }
   128         }
   121         
   129         
   122         if (i < backup_count && ec_mac_parse(macs[i][1], backup_devices[i], 1)) {
   130         if (i < backup_count && ec_mac_parse(macs[i][1], backup_devices[i], 1)) {
   123             ret = -EINVAL;
   131             ret = -EINVAL;
   124             goto out_cdev;
   132             goto out_class;
   125         }
   133         }
   126     }
   134     }
   127     
   135     
   128     if (master_count) {
   136     if (master_count) {
   129         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,
   137         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,
   130                         GFP_KERNEL))) {
   138                         GFP_KERNEL))) {
   131             EC_ERR("Failed to allocate memory for EtherCAT masters.\n");
   139             EC_ERR("Failed to allocate memory for EtherCAT masters.\n");
   132             ret = -ENOMEM;
   140             ret = -ENOMEM;
   133             goto out_cdev;
   141             goto out_class;
   134         }
   142         }
   135     }
   143     }
   136     
   144     
   137     for (i = 0; i < master_count; i++) {
   145     for (i = 0; i < master_count; i++) {
   138         if (ec_master_init(&masters[i], i, macs[i][0], macs[i][1],
   146         if (ec_master_init(&masters[i], i, macs[i][0], macs[i][1],
   148 
   156 
   149 out_free_masters:
   157 out_free_masters:
   150     for (i--; i >= 0; i--)
   158     for (i--; i >= 0; i--)
   151         ec_master_clear(&masters[i]);
   159         ec_master_clear(&masters[i]);
   152     kfree(masters);
   160     kfree(masters);
       
   161 out_class:
       
   162     class_destroy(class);
   153 out_cdev:
   163 out_cdev:
   154     unregister_chrdev_region(device_number, master_count);
   164     if (master_count)
       
   165         unregister_chrdev_region(device_number, master_count);
   155 out_return:
   166 out_return:
   156     return ret;
   167     return ret;
   157 }
   168 }
   158 
   169 
   159 /*****************************************************************************/
   170 /*****************************************************************************/
   167     unsigned int i;
   178     unsigned int i;
   168 
   179 
   169     for (i = 0; i < master_count; i++) {
   180     for (i = 0; i < master_count; i++) {
   170         ec_master_clear(&masters[i]);
   181         ec_master_clear(&masters[i]);
   171     }
   182     }
   172     if (master_count) {
   183 
       
   184     if (master_count)
   173         kfree(masters);
   185         kfree(masters);
       
   186     
       
   187     class_destroy(class);
       
   188     
       
   189     if (master_count)
   174         unregister_chrdev_region(device_number, master_count);
   190         unregister_chrdev_region(device_number, master_count);
   175     }
       
   176     
   191     
   177     EC_INFO("Master module cleaned up.\n");
   192     EC_INFO("Master module cleaned up.\n");
   178 }
   193 }
   179 
   194 
   180 /*****************************************************************************
   195 /*****************************************************************************