IgH EtherCAT Master  1.6.2
foe_request.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2008 Olav Zarges, imc Messsysteme GmbH
4  * Copyright (C) 2020 Florian Pose, IgH
5  *
6  * This file is part of the IgH EtherCAT Master.
7  *
8  * The IgH EtherCAT Master is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2, as
10  * published by the Free Software Foundation.
11  *
12  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with the IgH EtherCAT Master; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  ****************************************************************************/
22 
27 /****************************************************************************/
28 
29 #include <linux/module.h>
30 #include <linux/jiffies.h>
31 #include <linux/slab.h>
32 #include <linux/vmalloc.h>
33 
34 #include "foe_request.h"
35 #include "foe.h"
36 
37 /****************************************************************************/
38 
41 #define EC_FOE_REQUEST_RESPONSE_TIMEOUT 3000
42 
43 /****************************************************************************/
44 
45 // prototypes for private methods
47 
48 /****************************************************************************/
49 
53  ec_foe_request_t *req,
54  uint8_t* file_name )
55 {
56  INIT_LIST_HEAD(&req->list);
57  req->buffer = NULL;
58  req->file_name = file_name;
59  req->buffer_size = 0;
60  req->data_size = 0;
61  req->dir = EC_DIR_INVALID;
62  req->issue_timeout = 0; // no timeout
64  req->state = EC_INT_REQUEST_INIT;
65  req->result = FOE_BUSY;
66  req->error_code = 0x00000000;
67 }
68 
69 /****************************************************************************/
70 
74  ec_foe_request_t *req
75  )
76 {
78 }
79 
80 /****************************************************************************/
81 
85  ec_foe_request_t *req
86  )
87 {
88  if (req->buffer) {
89  vfree(req->buffer);
90  req->buffer = NULL;
91  }
92 
93  req->buffer_size = 0;
94  req->data_size = 0;
95 }
96 
97 /****************************************************************************/
98 
107  ec_foe_request_t *req,
108  size_t size
109  )
110 {
111  if (size <= req->buffer_size) {
112  return 0;
113  }
114 
116 
117  if (!(req->buffer = (uint8_t *) vmalloc(size))) {
118  EC_ERR("Failed to allocate %zu bytes of FoE memory.\n", size);
119  return -ENOMEM;
120  }
121 
122  req->buffer_size = size;
123  req->data_size = 0;
124  return 0;
125 }
126 
127 /****************************************************************************/
128 
136  ec_foe_request_t *req,
137  const uint8_t *source,
138  size_t size
139  )
140 {
141  int ret;
142 
143  ret = ec_foe_request_alloc(req, size);
144  if (ret) {
145  return ret;
146  }
147 
148  memcpy(req->buffer, source, size);
149  req->data_size = size;
150  return 0;
151 }
152 
153 /****************************************************************************/
154 
160  const ec_foe_request_t *req
161  )
162 {
163  return req->issue_timeout
164  && jiffies - req->jiffies_start > HZ * req->issue_timeout / 1000;
165 }
166 
167 /****************************************************************************/
168 
172  ec_foe_request_t *req
173  )
174 {
175  req->dir = EC_DIR_INPUT;
176  req->state = EC_INT_REQUEST_QUEUED;
177  req->result = FOE_BUSY;
178  req->jiffies_start = jiffies;
179 }
180 
181 /****************************************************************************/
182 
186  ec_foe_request_t *req
187  )
188 {
189  req->dir = EC_DIR_OUTPUT;
190  req->state = EC_INT_REQUEST_QUEUED;
191  req->result = FOE_BUSY;
192  req->jiffies_start = jiffies;
193 }
194 
195 /****************************************************************************/
ec_direction_t dir
Direction.
Definition: foe_request.h:52
int ec_foe_request_timed_out(const ec_foe_request_t *req)
Checks, if the timeout was exceeded.
Definition: foe_request.c:159
void ec_foe_request_write(ec_foe_request_t *req)
Prepares a write request (master to slave).
Definition: foe_request.c:185
int ec_foe_request_alloc(ec_foe_request_t *req, size_t size)
Pre-allocates the data memory.
Definition: foe_request.c:106
uint32_t result
FoE request abort code.
Definition: foe_request.h:60
uint32_t response_timeout
Maximum time in ms, the transfer is retried, if the slave does not respond.
Definition: foe_request.h:50
void ec_foe_request_clear_data(ec_foe_request_t *)
FoE request destructor.
Definition: foe_request.c:84
void ec_foe_request_clear(ec_foe_request_t *req)
FoE request destructor.
Definition: foe_request.c:73
size_t buffer_size
Size of FoE data memory.
Definition: foe_request.h:45
#define EC_FOE_REQUEST_RESPONSE_TIMEOUT
Default timeout in ms to wait for FoE transfer responses.
Definition: foe_request.c:41
uint8_t * buffer
Pointer to FoE data.
Definition: foe_request.h:44
Busy.
Definition: foe.h:34
EtherCAT FoE request structure.
void ec_foe_request_init(ec_foe_request_t *req, uint8_t *file_name)
FoE request constructor.
Definition: foe_request.c:52
int ec_foe_request_copy_data(ec_foe_request_t *req, const uint8_t *source, size_t size)
Copies FoE data from an external source.
Definition: foe_request.c:135
Values read by the master.
Definition: ecrt.h:507
FoE defines.
size_t data_size
Size of FoE data.
Definition: foe_request.h:46
uint8_t * file_name
Pointer to the filename.
Definition: foe_request.h:59
FoE request.
Definition: foe_request.h:42
Invalid direction.
Definition: ecrt.h:505
#define EC_ERR(fmt, args...)
Convenience macro for printing EtherCAT-specific errors to syslog.
Definition: globals.h:224
unsigned long jiffies_start
Jiffies, when the request was issued.
Definition: foe_request.h:56
void ec_foe_request_read(ec_foe_request_t *req)
Prepares a read request (slave to master).
Definition: foe_request.c:171
uint32_t error_code
Error code from an FoE Error Request.
Definition: foe_request.h:61
ec_internal_request_state_t state
FoE request state.
Definition: foe_request.h:55
Values written by the master.
Definition: ecrt.h:506
uint32_t issue_timeout
Maximum time in ms, the processing of the request may take.
Definition: foe_request.h:48
struct list_head list
List item.
Definition: foe_request.h:43