master/module.c
changeset 2589 2b9c78543663
parent 2411 01370d59233d
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
    59 static char *backup_devices[MAX_MASTERS]; /**< Backup devices parameter. */
    59 static char *backup_devices[MAX_MASTERS]; /**< Backup devices parameter. */
    60 static unsigned int backup_count; /**< Number of backup devices. */
    60 static unsigned int backup_count; /**< Number of backup devices. */
    61 static unsigned int debug_level;  /**< Debug level parameter. */
    61 static unsigned int debug_level;  /**< Debug level parameter. */
    62 
    62 
    63 static ec_master_t *masters; /**< Array of masters. */
    63 static ec_master_t *masters; /**< Array of masters. */
    64 static struct ec_mutex_t master_mutex; /**< Master mutex. */
    64 static struct semaphore master_sem; /**< Master semaphore. */
    65 
    65 
    66 dev_t device_number; /**< Device number for master cdevs. */
    66 dev_t device_number; /**< Device number for master cdevs. */
    67 struct class *class; /**< Device class. */
    67 struct class *class; /**< Device class. */
    68 
    68 
    69 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    69 static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]; /**< MAC addresses. */
    99 {
    99 {
   100     int i, ret = 0;
   100     int i, ret = 0;
   101 
   101 
   102     EC_INFO("Master driver %s\n", EC_MASTER_VERSION);
   102     EC_INFO("Master driver %s\n", EC_MASTER_VERSION);
   103 
   103 
   104     ec_mutex_init(&master_mutex);
   104     sema_init(&master_sem, 1);
   105 
   105 
   106     if (master_count) {
   106     if (master_count) {
   107         if (alloc_chrdev_region(&device_number,
   107         if (alloc_chrdev_region(&device_number,
   108                     0, master_count, "EtherCAT")) {
   108                     0, master_count, "EtherCAT")) {
   109             EC_ERR("Failed to obtain device number(s)!\n");
   109             EC_ERR("Failed to obtain device number(s)!\n");
   227     return 1;
   227     return 1;
   228 }
   228 }
   229 
   229 
   230 /*****************************************************************************/
   230 /*****************************************************************************/
   231 
   231 
       
   232 /** Maximum MAC string size.
       
   233  */
       
   234 #define EC_MAX_MAC_STRING_SIZE (3 * ETH_ALEN)
       
   235 
   232 /** Print a MAC address to a buffer.
   236 /** Print a MAC address to a buffer.
       
   237  *
       
   238  * The buffer size must be at least EC_MAX_MAC_STRING_SIZE.
   233  *
   239  *
   234  * \return number of bytes written.
   240  * \return number of bytes written.
   235  */
   241  */
   236 ssize_t ec_mac_print(
   242 ssize_t ec_mac_print(
   237         const uint8_t *mac, /**< MAC address */
   243         const uint8_t *mac, /**< MAC address */
   238         char *buffer /**< target buffer */
   244         char *buffer /**< Target buffer. */
   239         )
   245         )
   240 {
   246 {
   241     off_t off = 0;
   247     off_t off = 0;
   242     unsigned int i;
   248     unsigned int i;
   243 
   249 
   287 
   293 
   288 /*****************************************************************************/
   294 /*****************************************************************************/
   289 
   295 
   290 /** Parse a MAC address from a string.
   296 /** Parse a MAC address from a string.
   291  *
   297  *
   292  * The MAC address must follow the regexp
   298  * The MAC address must match the regular expression
   293  * "([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}".
   299  * "([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}".
   294  *
   300  *
   295  * \return 0 on success, else < 0
   301  * \return 0 on success, else < 0
   296  */
   302  */
   297 static int ec_mac_parse(uint8_t *mac, const char *src, int allow_empty)
   303 static int ec_mac_parse(uint8_t *mac, const char *src, int allow_empty)
   301     char *rem;
   307     char *rem;
   302 
   308 
   303     if (!strlen(src)) {
   309     if (!strlen(src)) {
   304         if (allow_empty){
   310         if (allow_empty){
   305             return 0;
   311             return 0;
   306         }
   312         } else {
   307         else {
       
   308             EC_ERR("MAC address may not be empty.\n");
   313             EC_ERR("MAC address may not be empty.\n");
   309             return -EINVAL;
   314             return -EINVAL;
   310         }
   315         }
   311     }
   316     }
   312 
   317 
   317                 || (i < ETH_ALEN - 1 && *rem != ':')) {
   322                 || (i < ETH_ALEN - 1 && *rem != ':')) {
   318             EC_ERR("Invalid MAC address \"%s\".\n", orig);
   323             EC_ERR("Invalid MAC address \"%s\".\n", orig);
   319             return -EINVAL;
   324             return -EINVAL;
   320         }
   325         }
   321         mac[i] = value;
   326         mac[i] = value;
   322         if (i < ETH_ALEN - 1)
   327         if (i < ETH_ALEN - 1) {
   323             src = rem + 1; // skip colon
   328             src = rem + 1; // skip colon
       
   329         }
   324     }
   330     }
   325 
   331 
   326     return 0;
   332     return 0;
   327 }
   333 }
   328 
   334 
   380 }
   386 }
   381 
   387 
   382 /*****************************************************************************/
   388 /*****************************************************************************/
   383 
   389 
   384 /** Prints slave states in clear text.
   390 /** Prints slave states in clear text.
       
   391  *
       
   392  * \return Size of the created string.
   385  */
   393  */
   386 size_t ec_state_string(uint8_t states, /**< slave states */
   394 size_t ec_state_string(uint8_t states, /**< slave states */
   387                        char *buffer, /**< target buffer
   395                        char *buffer, /**< target buffer
   388                                        (min. EC_STATE_STRING_SIZE bytes) */
   396                                        (min. EC_STATE_STRING_SIZE bytes) */
   389                        uint8_t multi /**< Show multi-state mask. */
   397                        uint8_t multi /**< Show multi-state mask. */
   443 
   451 
   444 /******************************************************************************
   452 /******************************************************************************
   445  *  Device interface
   453  *  Device interface
   446  *****************************************************************************/
   454  *****************************************************************************/
   447 
   455 
       
   456 /** Device names.
       
   457  */
       
   458 const char *ec_device_names[2] = {
       
   459     "main",
       
   460     "backup"
       
   461 };
       
   462 
   448 /** Offers an EtherCAT device to a certain master.
   463 /** Offers an EtherCAT device to a certain master.
   449  *
   464  *
   450  * The master decides, if it wants to use the device for EtherCAT operation,
   465  * The master decides, if it wants to use the device for EtherCAT operation,
   451  * or not. It is important, that the offered net_device is not used by the
   466  * or not. It is important, that the offered net_device is not used by the
   452  * kernel IP stack. If the master, accepted the offer, the address of the
   467  * kernel IP stack. If the master, accepted the offer, the address of the
   460         ec_pollfunc_t poll, /**< device poll function */
   475         ec_pollfunc_t poll, /**< device poll function */
   461         struct module *module /**< pointer to the module */
   476         struct module *module /**< pointer to the module */
   462         )
   477         )
   463 {
   478 {
   464     ec_master_t *master;
   479     ec_master_t *master;
   465     char str[20];
   480     char str[EC_MAX_MAC_STRING_SIZE];
   466     unsigned int i;
   481     unsigned int i, dev_idx;
   467 
   482 
   468     for (i = 0; i < master_count; i++) {
   483     for (i = 0; i < master_count; i++) {
   469         master = &masters[i];
   484         master = &masters[i];
   470 
   485         ec_mac_print(net_dev->dev_addr, str);
   471         ec_mutex_lock(&master->device_mutex);
   486 
   472         if (master->main_device.dev) { // master already has a device
   487         if (down_interruptible(&master->device_sem)) {
   473             ec_mutex_unlock(&master->device_mutex);
   488             EC_MASTER_WARN(master, "%s() interrupted!\n", __func__);
   474             continue;
   489             return NULL;
   475         }
   490         }
   476 
   491 
   477         if (ec_mac_equal(master->main_mac, net_dev->dev_addr)
   492         for (dev_idx = EC_DEVICE_MAIN;
   478                 || ec_mac_is_broadcast(master->main_mac)) {
   493                 dev_idx < ec_master_num_devices(master); dev_idx++) {
   479             ec_mac_print(net_dev->dev_addr, str);
   494             if (!master->devices[dev_idx].dev
   480             EC_INFO("Accepting device %s for master %u.\n",
   495                 && (ec_mac_equal(master->macs[dev_idx], net_dev->dev_addr)
   481                     str, master->index);
   496                     || ec_mac_is_broadcast(master->macs[dev_idx]))) {
   482 
   497 
   483             ec_device_attach(&master->main_device, net_dev, poll, module);
   498                 EC_INFO("Accepting %s as %s device for master %u.\n",
   484             ec_mutex_unlock(&master->device_mutex);
   499                         str, ec_device_names[dev_idx != 0], master->index);
   485 
   500 
   486             snprintf(net_dev->name, IFNAMSIZ, "ec%u", master->index);
   501                 ec_device_attach(&master->devices[dev_idx],
   487 
   502                         net_dev, poll, module);
   488             return &master->main_device; // offer accepted
   503                 up(&master->device_sem);
   489         }
   504 
   490         else {
   505                 snprintf(net_dev->name, IFNAMSIZ, "ec%c%u",
   491             ec_mutex_unlock(&master->device_mutex);
   506                         ec_device_names[dev_idx != 0][0], master->index);
   492 
   507 
   493             if (master->debug_level) {
   508                 return &master->devices[dev_idx]; // offer accepted
   494                 ec_mac_print(net_dev->dev_addr, str);
       
   495                 EC_MASTER_DBG(master, 0, "Master declined device %s.\n",
       
   496                         str);
       
   497             }
   509             }
   498         }
   510         }
       
   511 
       
   512         up(&master->device_sem);
       
   513 
       
   514         EC_MASTER_DBG(master, 1, "Master declined device %s.\n", str);
   499     }
   515     }
   500 
   516 
   501     return NULL; // offer declined
   517     return NULL; // offer declined
   502 }
   518 }
   503 
   519 
   504 /******************************************************************************
   520 /******************************************************************************
   505  *  Realtime interface
   521  * Application interface
   506  *****************************************************************************/
   522  *****************************************************************************/
   507 
   523 
   508 /** Request a master.
   524 /** Request a master.
   509  *
   525  *
   510  * Same as ecrt_request_master(), but with ERR_PTR() return value.
   526  * Same as ecrt_request_master(), but with ERR_PTR() return value.
       
   527  *
       
   528  * \return Requested master.
   511  */
   529  */
   512 ec_master_t *ecrt_request_master_err(
   530 ec_master_t *ecrt_request_master_err(
   513         unsigned int master_index /**< Master index. */
   531         unsigned int master_index /**< Master index. */
   514         )
   532         )
   515 {
   533 {
   516     ec_master_t *master, *errptr = NULL;
   534     ec_master_t *master, *errptr = NULL;
       
   535     unsigned int dev_idx = EC_DEVICE_MAIN;
   517 
   536 
   518     EC_INFO("Requesting master %u...\n", master_index);
   537     EC_INFO("Requesting master %u...\n", master_index);
   519 
   538 
   520     if (master_index >= master_count) {
   539     if (master_index >= master_count) {
   521         EC_ERR("Invalid master index %u.\n", master_index);
   540         EC_ERR("Invalid master index %u.\n", master_index);
   522         errptr = ERR_PTR(-EINVAL);
   541         errptr = ERR_PTR(-EINVAL);
   523         goto out_return;
   542         goto out_return;
   524     }
   543     }
   525     master = &masters[master_index];
   544     master = &masters[master_index];
   526 
   545 
   527     if (ec_mutex_lock_interruptible(&master_mutex)) {
   546     if (down_interruptible(&master_sem)) {
   528         errptr = ERR_PTR(-EINTR);
   547         errptr = ERR_PTR(-EINTR);
   529         goto out_return;
   548         goto out_return;
   530     }
   549     }
   531 
   550 
   532     if (master->reserved) {
   551     if (master->reserved) {
   533         ec_mutex_unlock(&master_mutex);
   552         up(&master_sem);
   534         EC_MASTER_ERR(master, "Master already in use!\n");
   553         EC_MASTER_ERR(master, "Master already in use!\n");
   535         errptr = ERR_PTR(-EBUSY);
   554         errptr = ERR_PTR(-EBUSY);
   536         goto out_return;
   555         goto out_return;
   537     }
   556     }
   538     master->reserved = 1;
   557     master->reserved = 1;
   539     ec_mutex_unlock(&master_mutex);
   558     up(&master_sem);
   540 
   559 
   541     if (ec_mutex_lock_interruptible(&master->device_mutex)) {
   560     if (down_interruptible(&master->device_sem)) {
   542         errptr = ERR_PTR(-EINTR);
   561         errptr = ERR_PTR(-EINTR);
   543         goto out_release;
   562         goto out_release;
   544     }
   563     }
   545 
   564 
   546     if (master->phase != EC_IDLE) {
   565     if (master->phase != EC_IDLE) {
   547         ec_mutex_unlock(&master->device_mutex);
   566         up(&master->device_sem);
   548         EC_MASTER_ERR(master, "Master still waiting for devices!\n");
   567         EC_MASTER_ERR(master, "Master still waiting for devices!\n");
   549         errptr = ERR_PTR(-ENODEV);
   568         errptr = ERR_PTR(-ENODEV);
   550         goto out_release;
   569         goto out_release;
   551     }
   570     }
   552 
   571 
   553     if (!try_module_get(master->main_device.module)) {
   572     for (; dev_idx < ec_master_num_devices(master); dev_idx++) {
   554         ec_mutex_unlock(&master->device_mutex);
   573         ec_device_t *device = &master->devices[dev_idx];
   555         EC_ERR("Device module is unloading!\n");
   574         if (!try_module_get(device->module)) {
   556         errptr = ERR_PTR(-ENODEV);
   575             up(&master->device_sem);
   557         goto out_release;
   576             EC_MASTER_ERR(master, "Device module is unloading!\n");
   558     }
   577             errptr = ERR_PTR(-ENODEV);
   559 
   578             goto out_module_put;
   560     ec_mutex_unlock(&master->device_mutex);
   579         }
       
   580     }
       
   581 
       
   582     up(&master->device_sem);
   561 
   583 
   562     if (ec_master_enter_operation_phase(master)) {
   584     if (ec_master_enter_operation_phase(master)) {
   563         EC_MASTER_ERR(master, "Failed to enter OPERATION phase!\n");
   585         EC_MASTER_ERR(master, "Failed to enter OPERATION phase!\n");
   564         errptr = ERR_PTR(-EIO);
   586         errptr = ERR_PTR(-EIO);
   565         goto out_module_put;
   587         goto out_module_put;
   567 
   589 
   568     EC_INFO("Successfully requested master %u.\n", master_index);
   590     EC_INFO("Successfully requested master %u.\n", master_index);
   569     return master;
   591     return master;
   570 
   592 
   571  out_module_put:
   593  out_module_put:
   572     module_put(master->main_device.module);
   594     for (; dev_idx > 0; dev_idx--) {
       
   595         ec_device_t *device = &master->devices[dev_idx - 1];
       
   596         module_put(device->module);
       
   597     }
   573  out_release:
   598  out_release:
   574     master->reserved = 0;
   599     master->reserved = 0;
   575  out_return:
   600  out_return:
   576     return errptr;
   601     return errptr;
   577 }
   602 }
   586 
   611 
   587 /*****************************************************************************/
   612 /*****************************************************************************/
   588 
   613 
   589 void ecrt_release_master(ec_master_t *master)
   614 void ecrt_release_master(ec_master_t *master)
   590 {
   615 {
       
   616     unsigned int dev_idx;
       
   617 
   591     EC_MASTER_INFO(master, "Releasing master...\n");
   618     EC_MASTER_INFO(master, "Releasing master...\n");
   592 
   619 
   593     if (!master->reserved) {
   620     if (!master->reserved) {
   594         EC_MASTER_WARN(master, "%s(): Master was was not requested!\n",
   621         EC_MASTER_WARN(master, "%s(): Master was was not requested!\n",
   595                 __func__);
   622                 __func__);
   596         return;
   623         return;
   597     }
   624     }
   598 
   625 
   599     ec_master_leave_operation_phase(master);
   626     ec_master_leave_operation_phase(master);
   600 
   627 
   601     module_put(master->main_device.module);
   628     for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
       
   629             dev_idx++) {
       
   630         module_put(master->devices[dev_idx].module);
       
   631     }
       
   632 
   602     master->reserved = 0;
   633     master->reserved = 0;
   603 
   634 
   604     EC_MASTER_INFO(master, "Released.\n");
   635     EC_MASTER_INFO(master, "Released.\n");
   605 }
   636 }
   606 
   637 
   607 /*****************************************************************************/
   638 /*****************************************************************************/
   608 
   639 
   609 unsigned int ecrt_version_magic(void)
   640 unsigned int ecrt_version_magic(void)
   610 {
   641 {
   611     return ECRT_VERSION_MAGIC;
   642     return ECRT_VERSION_MAGIC;
   612 }
       
   613 
       
   614 /*****************************************************************************/
       
   615 
       
   616 /** Return pointer to running master
       
   617  */
       
   618 ec_master_t *ecrt_attach_master(unsigned int master_index)
       
   619 {
       
   620     ec_master_t *master = NULL;
       
   621 
       
   622     EC_INFO("Requesting master %u...\n", master_index);
       
   623 
       
   624     if (master_index >= master_count) {
       
   625         EC_ERR("Invalid master index %u.\n", master_index);
       
   626         return master;
       
   627     }
       
   628 
       
   629     master = &masters[master_index];
       
   630     if (master->reserved) {
       
   631        // ok master is attached
       
   632         EC_INFO("attaching Master %u!\n", master_index);
       
   633     } else {
       
   634         EC_ERR("No Master %u in use!\n", master_index);
       
   635         master = NULL;
       
   636     }
       
   637     return master;
       
   638 }
   643 }
   639 
   644 
   640 /*****************************************************************************/
   645 /*****************************************************************************/
   641 
   646 
   642 /** Global request state type translation table.
   647 /** Global request state type translation table.
   661 EXPORT_SYMBOL(ecdev_offer);
   666 EXPORT_SYMBOL(ecdev_offer);
   662 
   667 
   663 EXPORT_SYMBOL(ecrt_request_master);
   668 EXPORT_SYMBOL(ecrt_request_master);
   664 EXPORT_SYMBOL(ecrt_release_master);
   669 EXPORT_SYMBOL(ecrt_release_master);
   665 EXPORT_SYMBOL(ecrt_version_magic);
   670 EXPORT_SYMBOL(ecrt_version_magic);
   666 EXPORT_SYMBOL(ecrt_attach_master);
       
   667 
   671 
   668 /** \endcond */
   672 /** \endcond */
   669 
   673 
   670 /*****************************************************************************/
   674 /*****************************************************************************/