36 #include <linux/module.h> 37 #include <linux/device.h> 38 #include <linux/err.h> 46 #define MAX_MASTERS 32 83 MODULE_AUTHOR(
"Florian Pose <fp@igh-essen.com>");
84 MODULE_DESCRIPTION(
"EtherCAT master driver module");
85 MODULE_LICENSE(
"GPL");
86 MODULE_VERSION(EC_MASTER_VERSION);
89 MODULE_PARM_DESC(
main_devices,
"MAC addresses of main devices");
91 MODULE_PARM_DESC(
backup_devices,
"MAC addresses of backup devices");
101 MODULE_PARM_DESC(
pcap_size,
"Pcap buffer size");
116 EC_INFO(
"Master driver %s\n", EC_MASTER_VERSION);
123 EC_ERR(
"Failed to obtain device number(s)!\n");
129 class = class_create(THIS_MODULE, "EtherCAT");
131 EC_ERR(
"Failed to create device class.\n");
132 ret = PTR_ERR(
class);
156 if (!(masters = kmalloc(
sizeof(
ec_master_t) * master_count,
158 EC_ERR(
"Failed to allocate memory" 159 " for EtherCAT masters.\n");
169 goto out_free_masters;
172 EC_INFO(
"%u master%s waiting for devices.\n",
173 master_count, (master_count == 1 ?
"" :
"s"));
177 for (i--; i >= 0; i--)
181 class_destroy(
class);
206 class_destroy(
class);
211 EC_INFO(
"Master module cleaned up.\n");
237 for (i = 0; i < ETH_ALEN; i++)
238 if (mac1[i] != mac2[i])
248 #define EC_MAX_MAC_STRING_SIZE (3 * ETH_ALEN) 264 for (i = 0; i < ETH_ALEN; i++) {
265 off += sprintf(buffer + off,
"%02X", mac[i]);
266 if (i < ETH_ALEN - 1) off += sprintf(buffer + off,
":");
283 for (i = 0; i < ETH_ALEN; i++)
301 for (i = 0; i < ETH_ALEN; i++)
317 static int ec_mac_parse(uint8_t *mac,
const char *src,
int allow_empty)
319 unsigned int i, value;
320 const char *orig = src;
327 EC_ERR(
"MAC address may not be empty.\n");
332 for (i = 0; i < ETH_ALEN; i++) {
333 value = simple_strtoul(src, &rem, 16);
336 || (i < ETH_ALEN - 1 && *rem !=
':')) {
337 EC_ERR(
"Invalid MAC address \"%s\".\n", orig);
341 if (i < ETH_ALEN - 1) {
362 for (i = 0; i < size; i++) {
363 printk(KERN_CONT
"%02X ", data[i]);
365 if ((i + 1) % 16 == 0 && i < size - 1) {
366 printk(KERN_CONT
"\n");
370 if (i + 1 == 128 && size > 256) {
371 printk(KERN_CONT
"dropped %zu bytes\n", size - 128 - i);
376 printk(KERN_CONT
"\n");
391 for (i = 0; i < size; i++) {
392 if (d1[i] == d2[i]) printk(KERN_CONT
".. ");
393 else printk(KERN_CONT
"%02X ", d2[i]);
394 if ((i + 1) % 16 == 0) {
395 printk(KERN_CONT
"\n");
399 printk(KERN_CONT
"\n");
415 unsigned int first = 1;
418 off += sprintf(buffer + off,
"(unknown)");
424 off += sprintf(buffer + off,
"INIT");
428 if (!first) off += sprintf(buffer + off,
", ");
429 off += sprintf(buffer + off,
"PREOP");
433 if (!first) off += sprintf(buffer + off,
", ");
434 off += sprintf(buffer + off,
"SAFEOP");
438 if (!first) off += sprintf(buffer + off,
", ");
439 off += sprintf(buffer + off,
"OP");
443 off += sprintf(buffer + off,
"INIT");
445 off += sprintf(buffer + off,
"PREOP");
447 off += sprintf(buffer + off,
"BOOT");
449 off += sprintf(buffer + off,
"SAFEOP");
451 off += sprintf(buffer + off,
"OP");
453 off += sprintf(buffer + off,
"(invalid)");
459 if (!first) off += sprintf(buffer + off,
" + ");
460 off += sprintf(buffer + off,
"ERROR");
488 struct net_device *net_dev,
490 struct module *module
495 unsigned int i, dev_idx;
498 master = &masters[i];
501 if (ec_lock_down_interruptible(&master->
device_sem)) {
512 EC_INFO(
"Accepting %s as %s device for master %u.\n",
516 net_dev, poll, module);
519 snprintf(net_dev->name, IFNAMSIZ,
"ec%c%u",
522 return &master->
devices[dev_idx];
528 EC_MASTER_DBG(master, 1,
"Master declined device %s.\n", str);
545 unsigned int master_index
551 EC_INFO(
"Requesting master %u...\n", master_index);
554 EC_ERR(
"Invalid master index %u.\n", master_index);
555 errptr = ERR_PTR(-EINVAL);
558 master = &masters[master_index];
560 if (ec_lock_down_interruptible(&
master_sem)) {
561 errptr = ERR_PTR(-EINTR);
568 errptr = ERR_PTR(-EBUSY);
574 if (ec_lock_down_interruptible(&master->
device_sem)) {
575 errptr = ERR_PTR(-EINTR);
581 EC_MASTER_ERR(master,
"Master still waiting for devices!\n");
582 errptr = ERR_PTR(-ENODEV);
588 if (!try_module_get(device->
module)) {
591 errptr = ERR_PTR(-ENODEV);
600 errptr = ERR_PTR(-EIO);
604 EC_INFO(
"Successfully requested master %u.\n", master_index);
608 for (; dev_idx > 0; dev_idx--) {
610 module_put(device->
module);
623 return IS_ERR(master) ? NULL : master;
630 unsigned int dev_idx;
unsigned int reserved
True, if the master is in use.
void ec_print_data(const uint8_t *data, size_t size)
Outputs frame contents for debugging purposes.
#define EC_SLAVE_STATE_MASK
Slave state mask.
static char * backup_devices[MAX_MASTERS]
Backup devices parameter.
size_t ec_state_string(uint8_t states, char *buffer, uint8_t multi)
Prints slave states in clear text.
int ec_mac_is_broadcast(const uint8_t *mac)
void ecrt_release_master(ec_master_t *master)
Releases a requested EtherCAT master.
#define MAX_EOE
Maximum number of EOE interfaces that can be defined at startup.
void ec_master_clear(ec_master_t *master)
Destructor.
OP (mailbox communication and input/output update)
void ec_master_leave_operation_phase(ec_master_t *master)
Transition function from OPERATION to IDLE phase.
static ec_lock_t master_sem
Master semaphore.
#define ec_master_num_devices(MASTER)
Number of Ethernet devices.
#define EC_MAX_MAC_STRING_SIZE
Maximum MAC string size.
struct module * module
pointer to the device's owning module
dev_t device_number
Device number for master cdevs.
Bootstrap state (mailbox communication, firmware update)
int ec_mac_is_zero(const uint8_t *mac)
#define MAX_MASTERS
Maximum number of masters.
static ec_master_t * masters
Array of masters.
char * ec_master_version_str
Version string.
Acknowledge/Error bit (no actual state)
static unsigned int debug_level
Debug level parameter.
const uint8_t * macs[EC_MAX_NUM_DEVICES]
Device MAC addresses.
void ec_print_data_diff(const uint8_t *d1, const uint8_t *d2, size_t size)
Outputs frame contents and differences for debugging purposes.
ec_master_t * ecrt_request_master_err(unsigned int master_index)
Request a master.
Global definitions and macros.
EtherCAT master structure.
void __exit ec_cleanup_module(void)
Module cleanup.
SAFEOP (mailbox communication and input update)
#define EC_MASTER_DBG(master, level, fmt, args...)
Convenience macro for printing master-specific debug messages to syslog.
ec_lock_t device_sem
Device semaphore.
unsigned int eoe_count
Number of EOE interfaces.
ec_master_t * ecrt_request_master(unsigned int master_index)
Requests an EtherCAT master for realtime operation.
Request is being processed.
static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]
MAC addresses.
ec_master_phase_t phase
Master phase.
static unsigned int master_count
Number of masters.
void(* ec_pollfunc_t)(struct net_device *)
Device poll function type.
int __init ec_init_module(void)
Module initialization.
unsigned int ecrt_version_magic(void)
Returns the version magic of the realtime interface.
int ec_master_init(ec_master_t *master, unsigned int index, const uint8_t *main_mac, const uint8_t *backup_mac, dev_t device_number, struct class *class, unsigned int debug_level)
Master constructor.
#define EC_MASTER_WARN(master, fmt, args...)
Convenience macro for printing master-specific warnings to syslog.
int ec_master_enter_operation_phase(ec_master_t *master)
Transition function from IDLE to OPERATION phase.
struct class * class
Device class.
#define EC_MASTER_ERR(master, fmt, args...)
Convenience macro for printing master-specific errors to syslog.
int ec_mac_equal(const uint8_t *mac1, const uint8_t *mac2)
char * eoe_interfaces[MAX_EOE]
EOE interfaces parameter.
INIT state (no mailbox communication, no IO)
#define EC_DBG(fmt, args...)
Convenience macro for printing EtherCAT debug messages to syslog.
#define EC_INFO(fmt, args...)
Convenience macro for printing EtherCAT-specific information to syslog.
#define EC_ERR(fmt, args...)
Convenience macro for printing EtherCAT-specific errors to syslog.
ec_device_t * ecdev_offer(struct net_device *net_dev, ec_pollfunc_t poll, struct module *module)
Offers an EtherCAT device to a certain master.
unsigned int ec_master_count(void)
Get the number of masters.
const ec_request_state_t ec_request_state_translation_table[]
Global request state type translation table.
void ec_device_attach(ec_device_t *device, struct net_device *net_dev, ec_pollfunc_t poll, struct module *module)
Associate with net_device.
EtherCAT device structure.
static int ec_mac_parse(uint8_t *, const char *, int)
Parse a MAC address from a string.
struct net_device * dev
pointer to the assigned net_device
Request was processed successfully.
static unsigned int backup_count
Number of backup devices.
ec_request_state_t
Request state.
bool eoe_autocreate
Auto-create EOE interfaces.
PREOP state (mailbox communication, no IO)
#define EC_MASTER_INFO(master, fmt, args...)
Convenience macro for printing master-specific information to syslog.
static char * main_devices[MAX_MASTERS]
Main devices parameter.
Request processing failed.
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
unsigned long pcap_size
Pcap buffer size in bytes.
size_t ec_mac_print(const uint8_t *mac, char *buffer)
Print a MAC address to a buffer.
void ec_master_init_static(void)
Static variables initializer.
#define ECRT_VERSION_MAGIC
EtherCAT realtime interface version word.
const char * ec_device_names[2]
Device names.