diff -r 7c986b717411 -r 9f4ea66d89a3 master/module.c --- a/master/module.c Wed Feb 22 17:36:28 2006 +0000 +++ b/master/module.c Thu Feb 23 09:58:50 2006 +0000 @@ -69,46 +69,45 @@ int __init ec_init_module(void) { - unsigned int i; - - printk(KERN_ERR "EtherCAT: Master driver, %s\n", COMPILE_INFO); - - if (ec_master_count < 1) { - printk(KERN_ERR "EtherCAT: Error - Illegal" - " ec_master_count: %i\n", ec_master_count); - return -1; - } - - printk(KERN_ERR "EtherCAT: Initializing %i EtherCAT master(s)...\n", - ec_master_count); - - if ((ec_masters = - (ec_master_t *) kmalloc(sizeof(ec_master_t) - * ec_master_count, - GFP_KERNEL)) == NULL) { - printk(KERN_ERR "EtherCAT: Could not allocate" - " memory for EtherCAT master(s)!\n"); - return -1; - } - - if ((ec_masters_reserved = - (int *) kmalloc(sizeof(int) * ec_master_count, - GFP_KERNEL)) == NULL) { - printk(KERN_ERR "EtherCAT: Could not allocate" - " memory for reservation flags!\n"); - kfree(ec_masters); - return -1; - } - - for (i = 0; i < ec_master_count; i++) - { - ec_master_init(&ec_masters[i]); - ec_masters_reserved[i] = 0; - } - - printk(KERN_ERR "EtherCAT: Master driver initialized.\n"); - - return 0; + unsigned int i; + + printk(KERN_ERR "EtherCAT: Master driver, %s\n", COMPILE_INFO); + + if (ec_master_count < 1) { + printk(KERN_ERR "EtherCAT: Error - Illegal" + " ec_master_count: %i\n", ec_master_count); + return -1; + } + + printk(KERN_ERR "EtherCAT: Initializing %i EtherCAT master(s)...\n", + ec_master_count); + + if ((ec_masters = + (ec_master_t *) kmalloc(sizeof(ec_master_t) + * ec_master_count, + GFP_KERNEL)) == NULL) { + printk(KERN_ERR "EtherCAT: Could not allocate" + " memory for EtherCAT master(s)!\n"); + return -1; + } + + if ((ec_masters_reserved = + (int *) kmalloc(sizeof(int) * ec_master_count, + GFP_KERNEL)) == NULL) { + printk(KERN_ERR "EtherCAT: Could not allocate" + " memory for reservation flags!\n"); + kfree(ec_masters); + return -1; + } + + for (i = 0; i < ec_master_count; i++) { + ec_master_init(ec_masters + i); + ec_masters_reserved[i] = 0; + } + + printk(KERN_ERR "EtherCAT: Master driver initialized.\n"); + + return 0; } /*****************************************************************************/ @@ -121,26 +120,22 @@ void __exit ec_cleanup_module(void) { - unsigned int i; - - printk(KERN_ERR "EtherCAT: Cleaning up master driver...\n"); - - if (ec_masters) - { - for (i = 0; i < ec_master_count; i++) - { - if (ec_masters_reserved[i]) { - printk(KERN_WARNING "EtherCAT: Warning -" - " Master %i is still in use!\n", i); - } - - ec_master_clear(&ec_masters[i]); - } - - kfree(ec_masters); - } - - printk(KERN_ERR "EtherCAT: Master driver cleaned up.\n"); + unsigned int i; + + printk(KERN_ERR "EtherCAT: Cleaning up master driver...\n"); + + if (ec_masters) { + for (i = 0; i < ec_master_count; i++) { + if (ec_masters_reserved[i]) { + printk(KERN_WARNING "EtherCAT: Warning -" + " Master %i is still in use!\n", i); + } + ec_master_clear(&ec_masters[i]); + } + kfree(ec_masters); + } + + printk(KERN_ERR "EtherCAT: Master driver cleaned up.\n"); } /****************************************************************************** @@ -158,8 +153,8 @@ @param module Zeiger auf das Modul (fuer try_module_lock()) @return 0, wenn alles o.k., - < 0, wenn bereits ein Geraet registriert oder das Geraet nicht - geoeffnet werden konnte. + < 0, wenn bereits ein Geraet registriert oder das Geraet nicht + geoeffnet werden konnte. */ ec_device_t *EtherCAT_dev_register(unsigned int master_index, @@ -168,42 +163,39 @@ struct pt_regs *), struct module *module) { - ec_device_t *ecd; - ec_master_t *master; - - if (master_index >= ec_master_count) { - printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", master_index); - return NULL; - } - - if (!dev) { - printk("EtherCAT: Device is NULL!\n"); - return NULL; - } - - master = ec_masters + master_index; - - if (master->device_registered) { - printk(KERN_ERR "EtherCAT: Master %i already has a device!\n", - master_index); - return NULL; - } - - ecd = &master->device; - - if (ec_device_init(ecd) < 0) { - return NULL; - } - - ecd->dev = dev; - ecd->tx_skb->dev = dev; - ecd->rx_skb->dev = dev; - ecd->isr = isr; - ecd->module = module; - - master->device_registered = 1; - - return ecd; + ec_device_t *ecd; + ec_master_t *master; + + if (master_index >= ec_master_count) { + printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", master_index); + return NULL; + } + + if (!dev) { + printk("EtherCAT: Device is NULL!\n"); + return NULL; + } + + master = ec_masters + master_index; + + if (master->device_registered) { + printk(KERN_ERR "EtherCAT: Master %i already has a device!\n", + master_index); + return NULL; + } + + ecd = &master->device; + + if (ec_device_init(ecd) < 0) return NULL; + + ecd->dev = dev; + ecd->tx_skb->dev = dev; + ecd->isr = isr; + ecd->module = module; + + master->device_registered = 1; + + return ecd; } /*****************************************************************************/ @@ -217,22 +209,23 @@ void EtherCAT_dev_unregister(unsigned int master_index, ec_device_t *ecd) { - ec_master_t *master; - - if (master_index >= ec_master_count) { - printk(KERN_WARNING "EtherCAT: Master %i does not exist!\n", master_index); - return; - } - - master = ec_masters + master_index; - - if (!master->device_registered || &master->device != ecd) { - printk(KERN_ERR "EtherCAT: Unable to unregister device!\n"); - return; - } - - master->device_registered = 0; - ec_device_clear(ecd); + ec_master_t *master; + + if (master_index >= ec_master_count) { + printk(KERN_WARNING "EtherCAT: Master %i does not exist!\n", + master_index); + return; + } + + master = ec_masters + master_index; + + if (!master->device_registered || &master->device != ecd) { + printk(KERN_ERR "EtherCAT: Unable to unregister device!\n"); + return; + } + + master->device_registered = 0; + ec_device_clear(ecd); } /****************************************************************************** @@ -252,54 +245,54 @@ ec_master_t *EtherCAT_rt_request_master(unsigned int index) { - ec_master_t *master; - - if (index < 0 || index >= ec_master_count) { - printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", index); - goto req_return; - } - - if (ec_masters_reserved[index]) { - printk(KERN_ERR "EtherCAT: Master %i already in use!\n", index); - goto req_return; - } - - master = &ec_masters[index]; - - if (!master->device_registered) { - printk(KERN_ERR "EtherCAT: Master %i has no device assigned yet!\n", - index); - goto req_return; - } - - if (!try_module_get(master->device.module)) { - printk(KERN_ERR "EtherCAT: Could not reserve device module!\n"); - goto req_return; - } - - if (ec_master_open(master) < 0) { - printk(KERN_ERR "EtherCAT: Could not open device!\n"); - goto req_module_put; - } - - if (ec_scan_for_slaves(master) != 0) { - printk(KERN_ERR "EtherCAT: Could not scan for slaves!\n"); - goto req_close; - } - - ec_masters_reserved[index] = 1; - printk(KERN_INFO "EtherCAT: Reserved master %i.\n", index); - - return master; + ec_master_t *master; + + if (index < 0 || index >= ec_master_count) { + printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", index); + goto req_return; + } + + if (ec_masters_reserved[index]) { + printk(KERN_ERR "EtherCAT: Master %i already in use!\n", index); + goto req_return; + } + + master = &ec_masters[index]; + + if (!master->device_registered) { + printk(KERN_ERR "EtherCAT: Master %i has no device assigned yet!\n", + index); + goto req_return; + } + + if (!try_module_get(master->device.module)) { + printk(KERN_ERR "EtherCAT: Failed to reserve device module!\n"); + goto req_return; + } + + if (ec_master_open(master) < 0) { + printk(KERN_ERR "EtherCAT: Failed to open device!\n"); + goto req_module_put; + } + + if (ec_scan_for_slaves(master) != 0) { + printk(KERN_ERR "EtherCAT: Bus scan failed!\n"); + goto req_close; + } + + ec_masters_reserved[index] = 1; + printk(KERN_INFO "EtherCAT: Reserved master %i.\n", index); + + return master; req_close: - ec_master_close(master); + ec_master_close(master); req_module_put: - module_put(master->device.module); + module_put(master->device.module); req_return: - return NULL; + return NULL; } /*****************************************************************************/ @@ -312,32 +305,32 @@ void EtherCAT_rt_release_master(ec_master_t *master) { - unsigned int i; - - for (i = 0; i < ec_master_count; i++) - { - if (&ec_masters[i] == master) + unsigned int i; + + for (i = 0; i < ec_master_count; i++) { - if (!master->device_registered) { - printk(KERN_WARNING "EtherCAT: Could not release device" - "module because of no device!\n"); - return; - } - - ec_master_close(master); - ec_master_reset(master); - - module_put(master->device.module); - ec_masters_reserved[i] = 0; - - printk(KERN_INFO "EtherCAT: Released master %i.\n", i); - - return; - } - } - - printk(KERN_WARNING "EtherCAT: Master %X was never requested!\n", - (unsigned int) master); + if (&ec_masters[i] == master) + { + if (!master->device_registered) { + printk(KERN_WARNING "EtherCAT: Failed to release device" + "module because of no device!\n"); + return; + } + + ec_master_close(master); + ec_master_reset(master); + + module_put(master->device.module); + ec_masters_reserved[i] = 0; + + printk(KERN_INFO "EtherCAT: Released master %i.\n", i); + + return; + } + } + + printk(KERN_WARNING "EtherCAT: Master %X was never requested!\n", + (unsigned int) master); } /*****************************************************************************/ @@ -354,6 +347,6 @@ /* Emacs-Konfiguration ;;; Local Variables: *** -;;; c-basic-offset:2 *** +;;; c-basic-offset:4 *** ;;; End: *** */