00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00035
00036
00037 #ifndef __EC_MASTER_H__
00038 #define __EC_MASTER_H__
00039
00040 #include <linux/version.h>
00041 #include <linux/list.h>
00042 #include <linux/timer.h>
00043 #include <linux/wait.h>
00044 #include <linux/kthread.h>
00045
00046 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
00047 #include <linux/semaphore.h>
00048 #else
00049 #include <asm/semaphore.h>
00050 #endif
00051
00052 #include "device.h"
00053 #include "domain.h"
00054 #include "ethernet.h"
00055 #include "fsm_master.h"
00056 #include "cdev.h"
00057
00058 #ifdef EC_RTDM
00059 #include "rtdm.h"
00060 #endif
00061
00062
00063
00073 #define EC_MASTER_INFO(master, fmt, args...) \
00074 printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
00075
00085 #define EC_MASTER_ERR(master, fmt, args...) \
00086 printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
00087
00097 #define EC_MASTER_WARN(master, fmt, args...) \
00098 printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
00099
00111 #define EC_MASTER_DBG(master, level, fmt, args...) \
00112 do { \
00113 if (master->debug_level >= level) { \
00114 printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
00115 master->index, ##args); \
00116 } \
00117 } while (0)
00118
00119
00124 #define EC_EXT_RING_SIZE 32
00125
00126
00127
00130 typedef enum {
00131 EC_ORPHANED,
00133 EC_IDLE,
00135 EC_OPERATION
00137 } ec_master_phase_t;
00138
00139
00140
00143 typedef struct {
00144 unsigned int timeouts;
00145 unsigned int corrupted;
00146 unsigned int unmatched;
00148 unsigned long output_jiffies;
00149 } ec_stats_t;
00150
00151
00152
00155 typedef struct {
00156 u64 tx_count;
00157 u64 last_tx_count;
00158 u64 rx_count;
00159 u64 last_rx_count;
00161 u64 tx_bytes;
00162 u64 last_tx_bytes;
00163 u64 rx_bytes;
00164 u64 last_rx_bytes;
00166 u64 last_loss;
00167 s32 tx_frame_rates[EC_RATE_COUNT];
00170 s32 rx_frame_rates[EC_RATE_COUNT];
00173 s32 tx_byte_rates[EC_RATE_COUNT];
00175 s32 rx_byte_rates[EC_RATE_COUNT];
00177 s32 loss_rates[EC_RATE_COUNT];
00179 unsigned long jiffies;
00180 } ec_device_stats_t;
00181
00182
00183
00184 #if EC_MAX_NUM_DEVICES < 1
00185 #error Invalid number of devices
00186 #endif
00187
00188
00189
00194 struct ec_master {
00195 unsigned int index;
00196 unsigned int reserved;
00198 ec_cdev_t cdev;
00199 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
00200 struct device *class_device;
00201 #else
00202 struct class_device *class_device;
00203 #endif
00204
00205 #ifdef EC_RTDM
00206 ec_rtdm_dev_t rtdm_dev;
00207 #endif
00208
00209 struct semaphore master_sem;
00211 ec_device_t devices[EC_MAX_NUM_DEVICES];
00212 const uint8_t *macs[EC_MAX_NUM_DEVICES];
00213 #if EC_MAX_NUM_DEVICES > 1
00214 unsigned int num_devices;
00217 #endif
00218 struct semaphore device_sem;
00219 ec_device_stats_t device_stats;
00221 ec_fsm_master_t fsm;
00222 ec_datagram_t fsm_datagram;
00223 ec_master_phase_t phase;
00224 unsigned int active;
00225 unsigned int config_changed;
00226 unsigned int injection_seq_fsm;
00228 unsigned int injection_seq_rt;
00231 ec_slave_t *slaves;
00232 unsigned int slave_count;
00234
00235 struct list_head configs;
00236 struct list_head domains;
00238 u64 app_time;
00239 u64 app_start_time;
00240 u8 has_app_time;
00241 ec_datagram_t ref_sync_datagram;
00243 ec_datagram_t sync_datagram;
00245 ec_datagram_t sync_mon_datagram;
00247 ec_slave_config_t *dc_ref_config;
00249 ec_slave_t *dc_ref_clock;
00251 unsigned int scan_busy;
00252 unsigned int allow_scan;
00253 struct semaphore scan_sem;
00255 wait_queue_head_t scan_queue;
00258 unsigned int config_busy;
00259 struct semaphore config_sem;
00261 wait_queue_head_t config_queue;
00264 struct list_head datagram_queue;
00265 uint8_t datagram_index;
00267 struct list_head ext_datagram_queue;
00269 struct semaphore ext_queue_sem;
00272 ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE];
00274 unsigned int ext_ring_idx_rt;
00276 unsigned int ext_ring_idx_fsm;
00278 unsigned int send_interval;
00280 size_t max_queue_size;
00282 ec_slave_t *fsm_slave;
00283 struct list_head fsm_exec_list;
00284 unsigned int fsm_exec_count;
00286 unsigned int debug_level;
00287 ec_stats_t stats;
00289 struct task_struct *thread;
00291 #ifdef EC_EOE
00292 struct task_struct *eoe_thread;
00293 struct list_head eoe_handlers;
00294 #endif
00295
00296 struct semaphore io_sem;
00298 void (*send_cb)(void *);
00299 void (*receive_cb)(void *);
00300 void *cb_data;
00301 void (*app_send_cb)(void *);
00303 void (*app_receive_cb)(void *);
00305 void *app_cb_data;
00307 struct list_head sii_requests;
00308 struct list_head emerg_reg_requests;
00311 wait_queue_head_t request_queue;
00313 };
00314
00315
00316
00317
00318 void ec_master_init_static(void);
00319
00320
00321 int ec_master_init(ec_master_t *, unsigned int, const uint8_t *,
00322 const uint8_t *, dev_t, struct class *, unsigned int);
00323 void ec_master_clear(ec_master_t *);
00324
00327 #if EC_MAX_NUM_DEVICES > 1
00328 #define ec_master_num_devices(MASTER) ((MASTER)->num_devices)
00329 #else
00330 #define ec_master_num_devices(MASTER) 1
00331 #endif
00332
00333
00334 int ec_master_enter_idle_phase(ec_master_t *);
00335 void ec_master_leave_idle_phase(ec_master_t *);
00336 int ec_master_enter_operation_phase(ec_master_t *);
00337 void ec_master_leave_operation_phase(ec_master_t *);
00338
00339 #ifdef EC_EOE
00340
00341 void ec_master_eoe_start(ec_master_t *);
00342 void ec_master_eoe_stop(ec_master_t *);
00343 #endif
00344
00345
00346 void ec_master_receive_datagrams(ec_master_t *, ec_device_t *,
00347 const uint8_t *, size_t);
00348 void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *);
00349 void ec_master_queue_datagram_ext(ec_master_t *, ec_datagram_t *);
00350
00351
00352 void ec_master_set_send_interval(ec_master_t *, unsigned int);
00353 void ec_master_attach_slave_configs(ec_master_t *);
00354 ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
00355 const ec_slave_t *ec_master_find_slave_const(const ec_master_t *, uint16_t,
00356 uint16_t);
00357 void ec_master_output_stats(ec_master_t *);
00358 #ifdef EC_EOE
00359 void ec_master_clear_eoe_handlers(ec_master_t *);
00360 #endif
00361 void ec_master_clear_slaves(ec_master_t *);
00362
00363 unsigned int ec_master_config_count(const ec_master_t *);
00364 ec_slave_config_t *ec_master_get_config(
00365 const ec_master_t *, unsigned int);
00366 const ec_slave_config_t *ec_master_get_config_const(
00367 const ec_master_t *, unsigned int);
00368 unsigned int ec_master_domain_count(const ec_master_t *);
00369 ec_domain_t *ec_master_find_domain(ec_master_t *, unsigned int);
00370 const ec_domain_t *ec_master_find_domain_const(const ec_master_t *,
00371 unsigned int);
00372 #ifdef EC_EOE
00373 uint16_t ec_master_eoe_handler_count(const ec_master_t *);
00374 const ec_eoe_t *ec_master_get_eoe_handler_const(const ec_master_t *, uint16_t);
00375 #endif
00376
00377 int ec_master_debug_level(ec_master_t *, unsigned int);
00378
00379 ec_domain_t *ecrt_master_create_domain_err(ec_master_t *);
00380 ec_slave_config_t *ecrt_master_slave_config_err(ec_master_t *, uint16_t,
00381 uint16_t, uint32_t, uint32_t);
00382
00383 void ec_master_calc_dc(ec_master_t *);
00384 void ec_master_request_op(ec_master_t *);
00385
00386 void ec_master_internal_send_cb(void *);
00387 void ec_master_internal_receive_cb(void *);
00388
00389 extern const unsigned int rate_intervals[EC_RATE_COUNT];
00390
00391
00392
00393 #endif