master/module.c
changeset 84 b4ae98855cea
parent 79 319a97c1f0f9
child 85 eb24637883ef
equal deleted inserted replaced
83:e8b76a509bc9 84:b4ae98855cea
    69 
    69 
    70 int __init ec_init_module(void)
    70 int __init ec_init_module(void)
    71 {
    71 {
    72     unsigned int i;
    72     unsigned int i;
    73 
    73 
    74     printk(KERN_ERR "EtherCAT: Master driver, %s\n", COMPILE_INFO);
    74     EC_INFO("Master driver, %s\n", COMPILE_INFO);
    75 
    75 
    76     if (ec_master_count < 1) {
    76     if (ec_master_count < 1) {
    77         printk(KERN_ERR "EtherCAT: Error - Illegal"
    77         EC_ERR("Error - Illegal ec_master_count: %i\n", ec_master_count);
    78                " ec_master_count: %i\n", ec_master_count);
       
    79         return -1;
    78         return -1;
    80     }
    79     }
    81 
    80 
    82     printk(KERN_ERR "EtherCAT: Initializing %i EtherCAT master(s)...\n",
    81     EC_INFO("Initializing %i EtherCAT master(s)...\n", ec_master_count);
    83            ec_master_count);
    82 
    84 
    83     if ((ec_masters = (ec_master_t *) kmalloc(sizeof(ec_master_t)
    85     if ((ec_masters =
    84                                               * ec_master_count,
    86          (ec_master_t *) kmalloc(sizeof(ec_master_t)
    85                                               GFP_KERNEL)) == NULL) {
    87                                  * ec_master_count,
    86         EC_ERR("Could not allocate memory for EtherCAT master(s)!\n");
    88                                  GFP_KERNEL)) == NULL) {
       
    89         printk(KERN_ERR "EtherCAT: Could not allocate"
       
    90                " memory for EtherCAT master(s)!\n");
       
    91         return -1;
    87         return -1;
    92     }
    88     }
    93 
    89 
    94     if ((ec_masters_reserved =
    90     if ((ec_masters_reserved =
    95          (int *) kmalloc(sizeof(int) * ec_master_count,
    91          (int *) kmalloc(sizeof(int) * ec_master_count, GFP_KERNEL)) == NULL) {
    96                          GFP_KERNEL)) == NULL) {
    92         EC_ERR("Could not allocate memory for reservation flags!\n");
    97         printk(KERN_ERR "EtherCAT: Could not allocate"
       
    98                " memory for reservation flags!\n");
       
    99         kfree(ec_masters);
    93         kfree(ec_masters);
   100         return -1;
    94         return -1;
   101     }
    95     }
   102 
    96 
   103     for (i = 0; i < ec_master_count; i++) {
    97     for (i = 0; i < ec_master_count; i++) {
   104         ec_master_init(ec_masters + i);
    98         ec_master_init(ec_masters + i);
   105         ec_masters_reserved[i] = 0;
    99         ec_masters_reserved[i] = 0;
   106     }
   100     }
   107 
   101 
   108     printk(KERN_ERR "EtherCAT: Master driver initialized.\n");
   102     EC_INFO("Master driver initialized.\n");
   109 
   103 
   110     return 0;
   104     return 0;
   111 }
   105 }
   112 
   106 
   113 /*****************************************************************************/
   107 /*****************************************************************************/
   120 
   114 
   121 void __exit ec_cleanup_module(void)
   115 void __exit ec_cleanup_module(void)
   122 {
   116 {
   123     unsigned int i;
   117     unsigned int i;
   124 
   118 
   125     printk(KERN_ERR "EtherCAT: Cleaning up master driver...\n");
   119     EC_INFO("Cleaning up master driver...\n");
   126 
   120 
   127     if (ec_masters) {
   121     if (ec_masters) {
   128         for (i = 0; i < ec_master_count; i++) {
   122         for (i = 0; i < ec_master_count; i++) {
   129             if (ec_masters_reserved[i]) {
   123             if (ec_masters_reserved[i]) {
   130                 printk(KERN_WARNING "EtherCAT: Warning -"
   124                 EC_WARN("Master %i is still in use!\n", i);
   131                        " Master %i is still in use!\n", i);
       
   132             }
   125             }
   133             ec_master_clear(&ec_masters[i]);
   126             ec_master_clear(&ec_masters[i]);
   134         }
   127         }
   135         kfree(ec_masters);
   128         kfree(ec_masters);
   136     }
   129     }
   137 
   130 
   138     printk(KERN_ERR "EtherCAT: Master driver cleaned up.\n");
   131     EC_INFO("Master driver cleaned up.\n");
   139 }
   132 }
   140 
   133 
   141 /******************************************************************************
   134 /******************************************************************************
   142  *
   135  *
   143  * Treiberschnittstelle
   136  * Treiberschnittstelle
   165 {
   158 {
   166     ec_device_t *ecd;
   159     ec_device_t *ecd;
   167     ec_master_t *master;
   160     ec_master_t *master;
   168 
   161 
   169     if (master_index >= ec_master_count) {
   162     if (master_index >= ec_master_count) {
   170         printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", master_index);
   163         EC_ERR("Master %i does not exist!\n", master_index);
   171         return NULL;
   164         return NULL;
   172     }
   165     }
   173 
   166 
   174     if (!dev) {
   167     if (!dev) {
   175         printk("EtherCAT: Device is NULL!\n");
   168         EC_WARN("Device is NULL!\n");
   176         return NULL;
   169         return NULL;
   177     }
   170     }
   178 
   171 
   179     master = ec_masters + master_index;
   172     master = ec_masters + master_index;
   180 
   173 
   181     if (master->device_registered) {
   174     if (master->device_registered) {
   182         printk(KERN_ERR "EtherCAT: Master %i already has a device!\n",
   175         EC_ERR("Master %i already has a device!\n", master_index);
   183                master_index);
       
   184         return NULL;
   176         return NULL;
   185     }
   177     }
   186 
   178 
   187     ecd = &master->device;
   179     ecd = &master->device;
   188 
   180 
   210 void EtherCAT_dev_unregister(unsigned int master_index, ec_device_t *ecd)
   202 void EtherCAT_dev_unregister(unsigned int master_index, ec_device_t *ecd)
   211 {
   203 {
   212     ec_master_t *master;
   204     ec_master_t *master;
   213 
   205 
   214     if (master_index >= ec_master_count) {
   206     if (master_index >= ec_master_count) {
   215         printk(KERN_WARNING "EtherCAT: Master %i does not exist!\n",
   207         EC_WARN("Master %i does not exist!\n", master_index);
   216                master_index);
       
   217         return;
   208         return;
   218     }
   209     }
   219 
   210 
   220     master = ec_masters + master_index;
   211     master = ec_masters + master_index;
   221 
   212 
   222     if (!master->device_registered || &master->device != ecd) {
   213     if (!master->device_registered || &master->device != ecd) {
   223         printk(KERN_ERR "EtherCAT: Unable to unregister device!\n");
   214         EC_WARN("Unable to unregister device!\n");
   224         return;
   215         return;
   225     }
   216     }
   226 
   217 
   227     master->device_registered = 0;
   218     master->device_registered = 0;
   228     ec_device_clear(ecd);
   219     ec_device_clear(ecd);
   246 ec_master_t *EtherCAT_rt_request_master(unsigned int index)
   237 ec_master_t *EtherCAT_rt_request_master(unsigned int index)
   247 {
   238 {
   248     ec_master_t *master;
   239     ec_master_t *master;
   249 
   240 
   250     if (index < 0 || index >= ec_master_count) {
   241     if (index < 0 || index >= ec_master_count) {
   251         printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", index);
   242         EC_ERR("Master %i does not exist!\n", index);
   252         goto req_return;
   243         goto req_return;
   253     }
   244     }
   254 
   245 
   255     if (ec_masters_reserved[index]) {
   246     if (ec_masters_reserved[index]) {
   256         printk(KERN_ERR "EtherCAT: Master %i already in use!\n", index);
   247         EC_ERR("Master %i already in use!\n", index);
   257         goto req_return;
   248         goto req_return;
   258     }
   249     }
   259 
   250 
   260     master = &ec_masters[index];
   251     master = &ec_masters[index];
   261 
   252 
   262     if (!master->device_registered) {
   253     if (!master->device_registered) {
   263         printk(KERN_ERR "EtherCAT: Master %i has no device assigned yet!\n",
   254         EC_ERR("Master %i has no device assigned yet!\n", index);
   264                index);
       
   265         goto req_return;
   255         goto req_return;
   266     }
   256     }
   267 
   257 
   268     if (!try_module_get(master->device.module)) {
   258     if (!try_module_get(master->device.module)) {
   269         printk(KERN_ERR "EtherCAT: Failed to reserve device module!\n");
   259         EC_ERR("Failed to reserve device module!\n");
   270         goto req_return;
   260         goto req_return;
   271     }
   261     }
   272 
   262 
   273     if (ec_master_open(master) < 0) {
   263     if (ec_master_open(master) < 0) {
   274         printk(KERN_ERR "EtherCAT: Failed to open device!\n");
   264         EC_ERR("Failed to open device!\n");
   275         goto req_module_put;
   265         goto req_module_put;
   276     }
   266     }
   277 
   267 
   278     if (ec_scan_for_slaves(master) != 0) {
   268     if (ec_scan_for_slaves(master) != 0) {
   279         printk(KERN_ERR "EtherCAT: Bus scan failed!\n");
   269         EC_ERR("Bus scan failed!\n");
   280         goto req_close;
   270         goto req_close;
   281     }
   271     }
   282 
   272 
   283     ec_masters_reserved[index] = 1;
   273     ec_masters_reserved[index] = 1;
   284     printk(KERN_INFO "EtherCAT: Reserved master %i.\n", index);
   274     EC_INFO("Reserved master %i.\n", index);
   285 
   275 
   286     return master;
   276     return master;
   287 
   277 
   288  req_close:
   278  req_close:
   289     ec_master_close(master);
   279     ec_master_close(master);
   310     for (i = 0; i < ec_master_count; i++)
   300     for (i = 0; i < ec_master_count; i++)
   311     {
   301     {
   312         if (&ec_masters[i] == master)
   302         if (&ec_masters[i] == master)
   313         {
   303         {
   314             if (!master->device_registered) {
   304             if (!master->device_registered) {
   315                 printk(KERN_WARNING "EtherCAT: Failed to release device"
   305                 EC_WARN("Failed to release device module: No device!\n");
   316                        "module because of no device!\n");
       
   317                 return;
   306                 return;
   318             }
   307             }
   319 
   308 
   320             ec_master_close(master);
   309             ec_master_close(master);
   321             ec_master_reset(master);
   310             ec_master_reset(master);
   322 
   311 
   323             module_put(master->device.module);
   312             module_put(master->device.module);
   324             ec_masters_reserved[i] = 0;
   313             ec_masters_reserved[i] = 0;
   325 
   314 
   326             printk(KERN_INFO "EtherCAT: Released master %i.\n", i);
   315             EC_INFO("Released master %i.\n", i);
   327 
   316 
   328             return;
   317             return;
   329         }
   318         }
   330     }
   319     }
   331 
   320 
   332     printk(KERN_WARNING "EtherCAT: Master %X was never requested!\n",
   321     EC_WARN("Master %X was never requested!\n", (u32) master);
   333            (unsigned int) master);
       
   334 }
   322 }
   335 
   323 
   336 /*****************************************************************************/
   324 /*****************************************************************************/
   337 
   325 
   338 module_init(ec_init_module);
   326 module_init(ec_init_module);