IgH EtherCAT Master  1.5.2
mbox_gateway_request.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * $Id$
4  *
5  * Copyright (C) 2019 Florian Pose, Ingenieurgemeinschaft IgH
6  *
7  * This file is part of the IgH EtherCAT Master.
8  *
9  * The IgH EtherCAT Master is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2, as
11  * published by the Free Software Foundation.
12  *
13  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with the IgH EtherCAT Master; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * ---
23  *
24  * The license mentioned above concerns the source code only. Using the
25  * EtherCAT technology and brand is only permitted in compliance with the
26  * industrial property and similar rights of Beckhoff Automation GmbH.
27  *
28  *****************************************************************************/
29 
34 /*****************************************************************************/
35 
36 #include <linux/module.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 
40 #include "mbox_gateway_request.h"
41 
42 /*****************************************************************************/
43 
46 #define EC_MBG_REQUEST_RESPONSE_TIMEOUT 1000
47 
48 /*****************************************************************************/
49 
51 
52 /*****************************************************************************/
53 
57  ec_mbg_request_t *req
58  )
59 {
60  INIT_LIST_HEAD(&req->list);
61  req->data = NULL;
62  req->mem_size = 0;
63  req->data_size = 0;
65  req->state = EC_INT_REQUEST_INIT;
66  req->jiffies_sent = 0U;
67  req->error_code = 0x0000;
68 }
69 
70 /*****************************************************************************/
71 
75  ec_mbg_request_t *req
76  )
77 {
79 }
80 
81 /*****************************************************************************/
82 
86  ec_mbg_request_t *req
87  )
88 {
89  if (req->data) {
90  kfree(req->data);
91  req->data = NULL;
92  }
93 
94  req->mem_size = 0;
95  req->data_size = 0;
96 }
97 
98 /*****************************************************************************/
99 
107  ec_mbg_request_t *req,
108  size_t size
109  )
110 {
111  if (size <= req->mem_size)
112  return 0;
113 
115 
116  if (!(req->data = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
117  EC_ERR("Failed to allocate %zu bytes of Mbox Gateway memory.\n", size);
118  return -ENOMEM;
119  }
120 
121  req->mem_size = size;
122  req->data_size = 0;
123  return 0;
124 }
125 
126 /*****************************************************************************/
127 
136  ec_mbg_request_t *req,
137  const uint8_t *source,
138  size_t size
139  )
140 {
141  int ret = ec_mbg_request_alloc(req, size);
142  if (ret < 0)
143  return ret;
144 
145  memcpy(req->data, source, size);
146  req->data_size = size;
147  return 0;
148 }
149 
150 /*****************************************************************************/
151 
155  ec_mbg_request_t *req
156  )
157 {
158  req->state = EC_INT_REQUEST_QUEUED;
159  req->error_code = 0x0000;
160 }
161 
162 /*****************************************************************************/
ec_internal_request_state_t state
Request state.
EtherCAT Mailbox Gateway request.
size_t data_size
Size of MBox request data.
uint32_t response_timeout
Maximum time in ms, the transfer is retried, if the slave does not respond.
size_t mem_size
Size of MBox request data memory.
uint8_t * data
Pointer to MBox request data.
uint16_t error_code
MBox Gateway error code.
int ec_mbg_request_copy_data(ec_mbg_request_t *req, const uint8_t *source, size_t size)
Copies Mbox Gateway data from an external source.
struct list_head list
List item.
void ec_mbg_request_init(ec_mbg_request_t *req)
Mbox Gateway request constructor.
EtherCAT Mailbox Gateway request structure.
void ec_mbg_request_clear(ec_mbg_request_t *req)
Mbox Gateway request destructor.
#define EC_MBG_REQUEST_RESPONSE_TIMEOUT
Default timeout in ms to wait for Mbox Gateway responses.
#define EC_ERR(fmt, args...)
Convenience macro for printing EtherCAT-specific errors to syslog.
Definition: globals.h:262
void ec_mbg_request_clear_data(ec_mbg_request_t *)
Free allocated memory.
unsigned long jiffies_sent
Jiffies, when the upload/download request was sent.
int ec_mbg_request_alloc(ec_mbg_request_t *req, size_t size)
Pre-allocates the data memory.
void ec_mbg_request_run(ec_mbg_request_t *req)
Request to run.