fp@27: /****************************************************************************** fp@27: * fp@54: * m o d u l e . c fp@27: * fp@195: * EtherCAT master driver module. fp@195: * fp@195: * Author: Florian Pose fp@27: * fp@27: * $Id$ fp@27: * fp@27: * (C) Copyright IgH 2005 fp@27: * Ingenieurgemeinschaft IgH fp@27: * Heinz-Bäcker Str. 34 fp@27: * D-45356 Essen fp@27: * Tel.: +49 201/61 99 31 fp@27: * Fax.: +49 201/61 98 36 fp@27: * E-mail: sp@igh-essen.com fp@27: * fp@39: *****************************************************************************/ fp@27: fp@27: #include fp@27: #include fp@27: #include fp@27: fp@184: #include "globals.h" fp@54: #include "master.h" fp@54: #include "device.h" fp@54: fp@54: /*****************************************************************************/ fp@54: fp@54: int __init ec_init_module(void); fp@54: void __exit ec_cleanup_module(void); fp@52: fp@39: /*****************************************************************************/ fp@39: fp@1617: #define COMPILE_INFO EC_STR(EC_MASTER_VERSION_MAIN) \ fp@1617: "." EC_STR(EC_MASTER_VERSION_SUB) \ fp@1617: " (" EC_MASTER_VERSION_EXTRA ")" \ fp@1617: " - rev. " EC_STR(SVNREV) \ fp@98: ", compiled by " EC_STR(USER) \ fp@98: " at " __DATE__ " " __TIME__ fp@39: fp@39: /*****************************************************************************/ fp@27: fp@178: static int ec_master_count = 1; fp@178: static struct list_head ec_masters; fp@27: fp@39: /*****************************************************************************/ fp@39: fp@64: MODULE_AUTHOR ("Florian Pose "); fp@27: MODULE_DESCRIPTION ("EtherCAT master driver module"); fp@27: MODULE_LICENSE("GPL"); fp@34: MODULE_VERSION(COMPILE_INFO); fp@27: fp@54: module_param(ec_master_count, int, 1); fp@195: MODULE_PARM_DESC(ec_master_count, "number of EtherCAT masters to initialize"); fp@195: fp@195: /*****************************************************************************/ fp@195: fp@195: /** fp@195: Module initialization. fp@195: Initializes \a ec_master_count masters. fp@195: \return 0 on success, else < 0 fp@54: */ fp@54: fp@54: int __init ec_init_module(void) fp@27: { fp@73: unsigned int i; fp@178: ec_master_t *master, *next; fp@73: fp@84: EC_INFO("Master driver, %s\n", COMPILE_INFO); fp@73: fp@73: if (ec_master_count < 1) { fp@111: EC_ERR("Error - Invalid ec_master_count: %i\n", ec_master_count); fp@127: goto out_return; fp@73: } fp@73: fp@84: EC_INFO("Initializing %i EtherCAT master(s)...\n", ec_master_count); fp@84: fp@178: INIT_LIST_HEAD(&ec_masters); fp@73: fp@73: for (i = 0; i < ec_master_count; i++) { fp@178: if (!(master = fp@178: (ec_master_t *) kmalloc(sizeof(ec_master_t), GFP_KERNEL))) { fp@178: EC_ERR("Failed to allocate memory for EtherCAT master %i.\n", i); fp@178: goto out_free; fp@178: } fp@178: fp@178: if (ec_master_init(master, i)) // kobject_put is done inside... fp@178: goto out_free; fp@178: fp@178: if (kobject_add(&master->kobj)) { fp@178: EC_ERR("Failed to add kobj.\n"); fp@178: kobject_put(&master->kobj); // free master fp@178: goto out_free; fp@178: } fp@178: fp@178: list_add_tail(&master->list, &ec_masters); fp@73: } fp@73: fp@84: EC_INFO("Master driver initialized.\n"); fp@73: return 0; fp@127: fp@178: out_free: fp@178: list_for_each_entry_safe(master, next, &ec_masters, list) { fp@178: list_del(&master->list); fp@178: kobject_del(&master->kobj); fp@178: kobject_put(&master->kobj); fp@178: } fp@127: out_return: fp@127: return -1; fp@27: } fp@27: fp@39: /*****************************************************************************/ fp@27: fp@27: /** fp@195: Module cleanup. fp@195: Clears all master instances. fp@27: */ fp@27: fp@54: void __exit ec_cleanup_module(void) fp@27: { fp@178: ec_master_t *master, *next; fp@73: fp@84: EC_INFO("Cleaning up master driver...\n"); fp@73: fp@178: list_for_each_entry_safe(master, next, &ec_masters, list) { fp@178: list_del(&master->list); fp@178: kobject_del(&master->kobj); fp@178: kobject_put(&master->kobj); // free master fp@178: } fp@73: fp@84: EC_INFO("Master driver cleaned up.\n"); fp@27: } fp@27: fp@178: /*****************************************************************************/ fp@178: fp@178: /** fp@178: Gets a handle to a certain master. fp@178: \returns pointer to master fp@178: */ fp@178: fp@195: ec_master_t *ec_find_master(unsigned int master_index /**< master index */) fp@178: { fp@178: ec_master_t *master; fp@178: fp@178: list_for_each_entry(master, &ec_masters, list) { fp@178: if (master->index == master_index) return master; fp@178: } fp@178: fp@178: EC_ERR("Master %i does not exist!\n", master_index); fp@178: return NULL; fp@178: } fp@178: fp@1617: /*****************************************************************************/ fp@1617: fp@1617: /** fp@1617: Outputs frame contents for debugging purposes. fp@1617: */ fp@1617: fp@1617: void ec_print_data(const uint8_t *data, /**< pointer to data */ fp@1617: size_t size /**< number of bytes to output */ fp@1617: ) fp@1617: { fp@1617: unsigned int i; fp@1617: fp@1617: EC_DBG(""); fp@1617: for (i = 0; i < size; i++) { fp@1617: printk("%02X ", data[i]); fp@1617: if ((i + 1) % 16 == 0) { fp@1617: printk("\n"); fp@1617: EC_DBG(""); fp@1617: } fp@1617: } fp@1617: printk("\n"); fp@1617: } fp@1617: fp@1617: /*****************************************************************************/ fp@1617: fp@1617: /** fp@1617: Outputs frame contents and differences for debugging purposes. fp@1617: */ fp@1617: fp@1617: void ec_print_data_diff(const uint8_t *d1, /**< first data */ fp@1617: const uint8_t *d2, /**< second data */ fp@1617: size_t size /** number of bytes to output */ fp@1617: ) fp@1617: { fp@1617: unsigned int i; fp@1617: fp@1617: EC_DBG(""); fp@1617: for (i = 0; i < size; i++) { fp@1617: if (d1[i] == d2[i]) printk(".. "); fp@1617: else printk("%02X ", d2[i]); fp@1617: if ((i + 1) % 16 == 0) { fp@1617: printk("\n"); fp@1617: EC_DBG(""); fp@1617: } fp@1617: } fp@1617: printk("\n"); fp@1617: } fp@1617: fp@54: /****************************************************************************** fp@195: * Device interface fp@54: *****************************************************************************/ fp@33: fp@33: /** fp@195: Registeres an EtherCAT device for a certain master. fp@195: \return 0 on success, else < 0 fp@195: */ fp@195: fp@195: ec_device_t *ecdev_register(unsigned int master_index, /**< master index */ fp@195: struct net_device *net_dev, /**< net_device of fp@195: the device */ fp@195: ec_isr_t isr, /**< interrupt service routine */ fp@195: struct module *module /**< pointer to the module */ fp@104: ) fp@91: { fp@73: ec_master_t *master; fp@73: fp@98: if (!net_dev) { fp@98: EC_WARN("Device is NULL!\n"); fp@191: goto out_return; fp@98: } fp@98: fp@178: if (!(master = ec_find_master(master_index))) return NULL; fp@178: fp@178: // critical section start fp@98: if (master->device) { fp@84: EC_ERR("Master %i already has a device!\n", master_index); fp@191: // critical section leave fp@191: goto out_return; fp@73: } fp@73: fp@178: if (!(master->device = fp@178: (ec_device_t *) kmalloc(sizeof(ec_device_t), GFP_KERNEL))) { fp@161: EC_ERR("Failed to allocate device!\n"); fp@191: // critical section leave fp@191: goto out_return; fp@98: } fp@178: // critical section end fp@98: fp@127: if (ec_device_init(master->device, master, net_dev, isr, module)) { fp@191: EC_ERR("Failed to init device!\n"); fp@191: goto out_free; fp@127: } fp@98: fp@98: return master->device; fp@191: fp@191: out_free: fp@191: kfree(master->device); fp@191: master->device = NULL; fp@191: out_return: fp@191: return NULL; fp@91: } fp@91: fp@91: /*****************************************************************************/ fp@91: fp@91: /** fp@195: Unregisteres an EtherCAT device. fp@195: */ fp@195: fp@195: void ecdev_unregister(unsigned int master_index, /**< master index */ fp@195: ec_device_t *device /**< EtherCAT device */ fp@104: ) fp@55: { fp@73: ec_master_t *master; fp@73: fp@178: if (!(master = ec_find_master(master_index))) return; fp@73: fp@98: if (!master->device || master->device != device) { fp@84: EC_WARN("Unable to unregister device!\n"); fp@73: return; fp@73: } fp@73: fp@98: ec_device_clear(master->device); fp@98: kfree(master->device); fp@98: master->device = NULL; fp@54: } fp@54: fp@191: /*****************************************************************************/ fp@191: fp@191: /** fp@195: Starts the master associated with the device. fp@195: */ fp@195: fp@195: int ecdev_start(unsigned int master_index /**< master index */) fp@191: { fp@191: ec_master_t *master; fp@191: if (!(master = ec_find_master(master_index))) return -1; fp@191: fp@191: if (ec_device_open(master->device)) { fp@191: EC_ERR("Failed to open device!\n"); fp@191: return -1; fp@191: } fp@191: fp@191: ec_master_freerun_start(master); fp@191: return 0; fp@191: } fp@191: fp@191: /*****************************************************************************/ fp@191: fp@191: /** fp@195: Stops the master associated with the device. fp@195: */ fp@195: fp@195: void ecdev_stop(unsigned int master_index /**< master index */) fp@191: { fp@191: ec_master_t *master; fp@191: if (!(master = ec_find_master(master_index))) return; fp@191: fp@191: ec_master_freerun_stop(master); fp@191: fp@191: if (ec_device_close(master->device)) fp@191: EC_WARN("Failed to close device!\n"); fp@191: } fp@191: fp@54: /****************************************************************************** fp@195: * Realtime interface fp@54: *****************************************************************************/ fp@33: fp@33: /** fp@195: Reserves an EtherCAT master for realtime operation. fp@195: \return pointer to reserved master, or NULL on error fp@91: */ fp@91: fp@178: ec_master_t *ecrt_request_master(unsigned int master_index fp@195: /**< master index */ fp@104: ) fp@54: { fp@73: ec_master_t *master; fp@73: fp@178: EC_INFO("Requesting master %i...\n", master_index); fp@178: fp@191: if (!(master = ec_find_master(master_index))) goto out_return; fp@178: fp@178: // begin critical section fp@178: if (master->reserved) { fp@178: EC_ERR("Master %i is already in use!\n", master_index); fp@191: goto out_return; fp@73: } fp@178: master->reserved = 1; fp@178: // end critical section fp@73: fp@98: if (!master->device) { fp@178: EC_ERR("Master %i has no assigned device!\n", master_index); fp@191: goto out_release; fp@73: } fp@73: fp@98: if (!try_module_get(master->device->module)) { fp@84: EC_ERR("Failed to reserve device module!\n"); fp@191: goto out_release; fp@191: } fp@191: fp@191: ec_master_freerun_stop(master); fp@191: ec_master_reset(master); fp@191: master->mode = EC_MASTER_MODE_RUNNING; fp@73: fp@178: if (!master->device->link_state) EC_WARN("Link is DOWN.\n"); fp@178: fp@178: if (ec_master_bus_scan(master)) { fp@84: EC_ERR("Bus scan failed!\n"); fp@191: goto out_module_put; fp@73: } fp@73: fp@178: EC_INFO("Master %i is ready.\n", master_index); fp@73: return master; fp@54: fp@191: out_module_put: fp@98: module_put(master->device->module); fp@85: ec_master_reset(master); fp@191: out_release: fp@178: master->reserved = 0; fp@191: out_return: fp@178: EC_ERR("Failed requesting master %i.\n", master_index); fp@73: return NULL; fp@27: } fp@27: fp@39: /*****************************************************************************/ fp@33: fp@33: /** fp@195: Releases a reserved EtherCAT master. fp@195: */ fp@195: fp@195: void ecrt_release_master(ec_master_t *master /**< EtherCAT master */) fp@33: { fp@178: EC_INFO("Releasing master %i...\n", master->index); fp@178: fp@178: if (!master->reserved) { fp@178: EC_ERR("Master %i was never requested!\n", master->index); fp@88: return; fp@88: } fp@88: fp@88: ec_master_reset(master); fp@88: fp@191: master->mode = EC_MASTER_MODE_IDLE; fp@191: ec_master_freerun_start(master); fp@151: fp@98: module_put(master->device->module); fp@178: master->reserved = 0; fp@178: fp@178: EC_INFO("Released master %i.\n", master->index); fp@88: return; fp@33: } fp@33: fp@39: /*****************************************************************************/ fp@54: fp@54: module_init(ec_init_module); fp@54: module_exit(ec_cleanup_module); fp@54: fp@104: EXPORT_SYMBOL(ecdev_register); fp@104: EXPORT_SYMBOL(ecdev_unregister); fp@191: EXPORT_SYMBOL(ecdev_start); fp@191: EXPORT_SYMBOL(ecdev_stop); fp@104: EXPORT_SYMBOL(ecrt_request_master); fp@104: EXPORT_SYMBOL(ecrt_release_master); fp@54: fp@54: /*****************************************************************************/