master/module.c
changeset 73 9f4ea66d89a3
parent 64 ea6ccf12c612
child 78 3d74183d6c6b
equal deleted inserted replaced
72:7c986b717411 73:9f4ea66d89a3
    67    oder zu wenig Speicher.
    67    oder zu wenig Speicher.
    68 */
    68 */
    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     printk(KERN_ERR "EtherCAT: 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         printk(KERN_ERR "EtherCAT: Error - Illegal"
    78            " ec_master_count: %i\n", ec_master_count);
    78                " ec_master_count: %i\n", ec_master_count);
    79     return -1;
    79         return -1;
    80   }
    80     }
    81 
    81 
    82   printk(KERN_ERR "EtherCAT: Initializing %i EtherCAT master(s)...\n",
    82     printk(KERN_ERR "EtherCAT: Initializing %i EtherCAT master(s)...\n",
    83          ec_master_count);
    83            ec_master_count);
    84 
    84 
    85   if ((ec_masters =
    85     if ((ec_masters =
    86        (ec_master_t *) kmalloc(sizeof(ec_master_t)
    86          (ec_master_t *) kmalloc(sizeof(ec_master_t)
    87                                * ec_master_count,
    87                                  * ec_master_count,
    88                                GFP_KERNEL)) == NULL) {
    88                                  GFP_KERNEL)) == NULL) {
    89     printk(KERN_ERR "EtherCAT: Could not allocate"
    89         printk(KERN_ERR "EtherCAT: Could not allocate"
    90            " memory for EtherCAT master(s)!\n");
    90                " memory for EtherCAT master(s)!\n");
    91     return -1;
    91         return -1;
    92   }
    92     }
    93 
    93 
    94   if ((ec_masters_reserved =
    94     if ((ec_masters_reserved =
    95        (int *) kmalloc(sizeof(int) * ec_master_count,
    95          (int *) kmalloc(sizeof(int) * ec_master_count,
    96                        GFP_KERNEL)) == NULL) {
    96                          GFP_KERNEL)) == NULL) {
    97     printk(KERN_ERR "EtherCAT: Could not allocate"
    97         printk(KERN_ERR "EtherCAT: Could not allocate"
    98            " memory for reservation flags!\n");
    98                " memory for reservation flags!\n");
    99     kfree(ec_masters);
    99         kfree(ec_masters);
   100     return -1;
   100         return -1;
   101   }
   101     }
   102 
   102 
   103   for (i = 0; i < ec_master_count; i++)
   103     for (i = 0; i < ec_master_count; i++) {
   104   {
   104         ec_master_init(ec_masters + i);
   105     ec_master_init(&ec_masters[i]);
   105         ec_masters_reserved[i] = 0;
   106     ec_masters_reserved[i] = 0;
   106     }
   107   }
   107 
   108 
   108     printk(KERN_ERR "EtherCAT: Master driver initialized.\n");
   109   printk(KERN_ERR "EtherCAT: Master driver initialized.\n");
   109 
   110 
   110     return 0;
   111   return 0;
       
   112 }
   111 }
   113 
   112 
   114 /*****************************************************************************/
   113 /*****************************************************************************/
   115 
   114 
   116 /**
   115 /**
   119    Entfernt alle Master-Instanzen.
   118    Entfernt alle Master-Instanzen.
   120 */
   119 */
   121 
   120 
   122 void __exit ec_cleanup_module(void)
   121 void __exit ec_cleanup_module(void)
   123 {
   122 {
   124   unsigned int i;
   123     unsigned int i;
   125 
   124 
   126   printk(KERN_ERR "EtherCAT: Cleaning up master driver...\n");
   125     printk(KERN_ERR "EtherCAT: Cleaning up master driver...\n");
   127 
   126 
   128   if (ec_masters)
   127     if (ec_masters) {
   129   {
   128         for (i = 0; i < ec_master_count; i++) {
   130     for (i = 0; i < ec_master_count; i++)
   129             if (ec_masters_reserved[i]) {
   131     {
   130                 printk(KERN_WARNING "EtherCAT: Warning -"
   132       if (ec_masters_reserved[i]) {
   131                        " Master %i is still in use!\n", i);
   133         printk(KERN_WARNING "EtherCAT: Warning -"
   132             }
   134                " Master %i is still in use!\n", i);
   133             ec_master_clear(&ec_masters[i]);
   135       }
   134         }
   136 
   135         kfree(ec_masters);
   137       ec_master_clear(&ec_masters[i]);
   136     }
   138     }
   137 
   139 
   138     printk(KERN_ERR "EtherCAT: Master driver cleaned up.\n");
   140     kfree(ec_masters);
       
   141   }
       
   142 
       
   143   printk(KERN_ERR "EtherCAT: Master driver cleaned up.\n");
       
   144 }
   139 }
   145 
   140 
   146 /******************************************************************************
   141 /******************************************************************************
   147  *
   142  *
   148  * Treiberschnittstelle
   143  * Treiberschnittstelle
   156    @param dev Das net_device des EtherCAT-Geraetes
   151    @param dev Das net_device des EtherCAT-Geraetes
   157    @param isr Funktionszeiger auf die Interrupt-Service-Routine
   152    @param isr Funktionszeiger auf die Interrupt-Service-Routine
   158    @param module Zeiger auf das Modul (fuer try_module_lock())
   153    @param module Zeiger auf das Modul (fuer try_module_lock())
   159 
   154 
   160    @return 0, wenn alles o.k.,
   155    @return 0, wenn alles o.k.,
   161            < 0, wenn bereits ein Geraet registriert oder das Geraet nicht
   156    < 0, wenn bereits ein Geraet registriert oder das Geraet nicht
   162                 geoeffnet werden konnte.
   157    geoeffnet werden konnte.
   163 */
   158 */
   164 
   159 
   165 ec_device_t *EtherCAT_dev_register(unsigned int master_index,
   160 ec_device_t *EtherCAT_dev_register(unsigned int master_index,
   166                                    struct net_device *dev,
   161                                    struct net_device *dev,
   167                                    irqreturn_t (*isr)(int, void *,
   162                                    irqreturn_t (*isr)(int, void *,
   168                                                       struct pt_regs *),
   163                                                       struct pt_regs *),
   169                                    struct module *module)
   164                                    struct module *module)
   170 {
   165 {
   171   ec_device_t *ecd;
   166     ec_device_t *ecd;
   172   ec_master_t *master;
   167     ec_master_t *master;
   173 
   168 
   174   if (master_index >= ec_master_count) {
   169     if (master_index >= ec_master_count) {
   175     printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", master_index);
   170         printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", master_index);
   176     return NULL;
   171         return NULL;
   177   }
   172     }
   178 
   173 
   179   if (!dev) {
   174     if (!dev) {
   180     printk("EtherCAT: Device is NULL!\n");
   175         printk("EtherCAT: Device is NULL!\n");
   181     return NULL;
   176         return NULL;
   182   }
   177     }
   183 
   178 
   184   master = ec_masters + master_index;
   179     master = ec_masters + master_index;
   185 
   180 
   186   if (master->device_registered) {
   181     if (master->device_registered) {
   187     printk(KERN_ERR "EtherCAT: Master %i already has a device!\n",
   182         printk(KERN_ERR "EtherCAT: Master %i already has a device!\n",
   188            master_index);
   183                master_index);
   189     return NULL;
   184         return NULL;
   190   }
   185     }
   191 
   186 
   192   ecd = &master->device;
   187     ecd = &master->device;
   193 
   188 
   194   if (ec_device_init(ecd) < 0) {
   189     if (ec_device_init(ecd) < 0) return NULL;
   195     return NULL;
   190 
   196   }
   191     ecd->dev = dev;
   197 
   192     ecd->tx_skb->dev = dev;
   198   ecd->dev = dev;
   193     ecd->isr = isr;
   199   ecd->tx_skb->dev = dev;
   194     ecd->module = module;
   200   ecd->rx_skb->dev = dev;
   195 
   201   ecd->isr = isr;
   196     master->device_registered = 1;
   202   ecd->module = module;
   197 
   203 
   198     return ecd;
   204   master->device_registered = 1;
       
   205 
       
   206   return ecd;
       
   207 }
   199 }
   208 
   200 
   209 /*****************************************************************************/
   201 /*****************************************************************************/
   210 
   202 
   211 /**
   203 /**
   215    @param ecd Das EtherCAT-Geraet
   207    @param ecd Das EtherCAT-Geraet
   216 */
   208 */
   217 
   209 
   218 void EtherCAT_dev_unregister(unsigned int master_index, ec_device_t *ecd)
   210 void EtherCAT_dev_unregister(unsigned int master_index, ec_device_t *ecd)
   219 {
   211 {
   220   ec_master_t *master;
   212     ec_master_t *master;
   221 
   213 
   222   if (master_index >= ec_master_count) {
   214     if (master_index >= ec_master_count) {
   223     printk(KERN_WARNING "EtherCAT: Master %i does not exist!\n", master_index);
   215         printk(KERN_WARNING "EtherCAT: Master %i does not exist!\n",
   224     return;
   216                master_index);
   225   }
   217         return;
   226 
   218     }
   227   master = ec_masters + master_index;
   219 
   228 
   220     master = ec_masters + master_index;
   229   if (!master->device_registered || &master->device != ecd) {
   221 
   230     printk(KERN_ERR "EtherCAT: Unable to unregister device!\n");
   222     if (!master->device_registered || &master->device != ecd) {
   231     return;
   223         printk(KERN_ERR "EtherCAT: Unable to unregister device!\n");
   232   }
   224         return;
   233 
   225     }
   234   master->device_registered = 0;
   226 
   235   ec_device_clear(ecd);
   227     master->device_registered = 0;
       
   228     ec_device_clear(ecd);
   236 }
   229 }
   237 
   230 
   238 /******************************************************************************
   231 /******************************************************************************
   239  *
   232  *
   240  * Echtzeitschnittstelle
   233  * Echtzeitschnittstelle
   250    @returns Zeiger auf EtherCAT-Master oder NULL, wenn Parameter ungueltig.
   243    @returns Zeiger auf EtherCAT-Master oder NULL, wenn Parameter ungueltig.
   251 */
   244 */
   252 
   245 
   253 ec_master_t *EtherCAT_rt_request_master(unsigned int index)
   246 ec_master_t *EtherCAT_rt_request_master(unsigned int index)
   254 {
   247 {
   255   ec_master_t *master;
   248     ec_master_t *master;
   256 
   249 
   257   if (index < 0 || index >= ec_master_count) {
   250     if (index < 0 || index >= ec_master_count) {
   258     printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", index);
   251         printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", index);
   259     goto req_return;
   252         goto req_return;
   260   }
   253     }
   261 
   254 
   262   if (ec_masters_reserved[index]) {
   255     if (ec_masters_reserved[index]) {
   263     printk(KERN_ERR "EtherCAT: Master %i already in use!\n", index);
   256         printk(KERN_ERR "EtherCAT: Master %i already in use!\n", index);
   264     goto req_return;
   257         goto req_return;
   265   }
   258     }
   266 
   259 
   267   master = &ec_masters[index];
   260     master = &ec_masters[index];
   268 
   261 
   269   if (!master->device_registered) {
   262     if (!master->device_registered) {
   270     printk(KERN_ERR "EtherCAT: Master %i has no device assigned yet!\n",
   263         printk(KERN_ERR "EtherCAT: Master %i has no device assigned yet!\n",
   271            index);
   264                index);
   272     goto req_return;
   265         goto req_return;
   273   }
   266     }
   274 
   267 
   275   if (!try_module_get(master->device.module)) {
   268     if (!try_module_get(master->device.module)) {
   276     printk(KERN_ERR "EtherCAT: Could not reserve device module!\n");
   269         printk(KERN_ERR "EtherCAT: Failed to reserve device module!\n");
   277     goto req_return;
   270         goto req_return;
   278   }
   271     }
   279 
   272 
   280   if (ec_master_open(master) < 0) {
   273     if (ec_master_open(master) < 0) {
   281     printk(KERN_ERR "EtherCAT: Could not open device!\n");
   274         printk(KERN_ERR "EtherCAT: Failed to open device!\n");
   282     goto req_module_put;
   275         goto req_module_put;
   283   }
   276     }
   284 
   277 
   285   if (ec_scan_for_slaves(master) != 0) {
   278     if (ec_scan_for_slaves(master) != 0) {
   286     printk(KERN_ERR "EtherCAT: Could not scan for slaves!\n");
   279         printk(KERN_ERR "EtherCAT: Bus scan failed!\n");
   287     goto req_close;
   280         goto req_close;
   288   }
   281     }
   289 
   282 
   290   ec_masters_reserved[index] = 1;
   283     ec_masters_reserved[index] = 1;
   291   printk(KERN_INFO "EtherCAT: Reserved master %i.\n", index);
   284     printk(KERN_INFO "EtherCAT: Reserved master %i.\n", index);
   292 
   285 
   293   return master;
   286     return master;
   294 
   287 
   295  req_close:
   288  req_close:
   296   ec_master_close(master);
   289     ec_master_close(master);
   297 
   290 
   298  req_module_put:
   291  req_module_put:
   299   module_put(master->device.module);
   292     module_put(master->device.module);
   300 
   293 
   301  req_return:
   294  req_return:
   302   return NULL;
   295     return NULL;
   303 }
   296 }
   304 
   297 
   305 /*****************************************************************************/
   298 /*****************************************************************************/
   306 
   299 
   307 /**
   300 /**
   310    @param master Zeiger auf den freizugebenden EtherCAT-Master.
   303    @param master Zeiger auf den freizugebenden EtherCAT-Master.
   311 */
   304 */
   312 
   305 
   313 void EtherCAT_rt_release_master(ec_master_t *master)
   306 void EtherCAT_rt_release_master(ec_master_t *master)
   314 {
   307 {
   315   unsigned int i;
   308     unsigned int i;
   316 
   309 
   317   for (i = 0; i < ec_master_count; i++)
   310     for (i = 0; i < ec_master_count; i++)
   318   {
       
   319     if (&ec_masters[i] == master)
       
   320     {
   311     {
   321       if (!master->device_registered) {
   312         if (&ec_masters[i] == master)
   322         printk(KERN_WARNING "EtherCAT: Could not release device"
   313         {
   323                "module because of no device!\n");
   314             if (!master->device_registered) {
   324         return;
   315                 printk(KERN_WARNING "EtherCAT: Failed to release device"
   325       }
   316                        "module because of no device!\n");
   326 
   317                 return;
   327       ec_master_close(master);
   318             }
   328       ec_master_reset(master);
   319 
   329 
   320             ec_master_close(master);
   330       module_put(master->device.module);
   321             ec_master_reset(master);
   331       ec_masters_reserved[i] = 0;
   322 
   332 
   323             module_put(master->device.module);
   333       printk(KERN_INFO "EtherCAT: Released master %i.\n", i);
   324             ec_masters_reserved[i] = 0;
   334 
   325 
   335       return;
   326             printk(KERN_INFO "EtherCAT: Released master %i.\n", i);
   336     }
   327 
   337   }
   328             return;
   338 
   329         }
   339   printk(KERN_WARNING "EtherCAT: Master %X was never requested!\n",
   330     }
   340          (unsigned int) master);
   331 
       
   332     printk(KERN_WARNING "EtherCAT: Master %X was never requested!\n",
       
   333            (unsigned int) master);
   341 }
   334 }
   342 
   335 
   343 /*****************************************************************************/
   336 /*****************************************************************************/
   344 
   337 
   345 module_init(ec_init_module);
   338 module_init(ec_init_module);
   352 
   345 
   353 /*****************************************************************************/
   346 /*****************************************************************************/
   354 
   347 
   355 /* Emacs-Konfiguration
   348 /* Emacs-Konfiguration
   356 ;;; Local Variables: ***
   349 ;;; Local Variables: ***
   357 ;;; c-basic-offset:2 ***
   350 ;;; c-basic-offset:4 ***
   358 ;;; End: ***
   351 ;;; End: ***
   359 */
   352 */