1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* drivers/tty/smux_loopback.c
*
* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/types.h>
#include <linux/err.h>
#include <linux/workqueue.h>
#include <linux/kfifo.h>
#include <linux/slab.h>
#include <linux/smux.h>
#include "smux_private.h"
#define SMUX_LOOP_FIFO_SIZE 128
static void smux_loopback_rx_worker(struct work_struct *work);
static struct workqueue_struct *smux_loopback_wq;
static DECLARE_WORK(smux_loopback_work, smux_loopback_rx_worker);
static struct kfifo smux_loop_pkt_fifo;
static DEFINE_SPINLOCK(hw_fn_lock);
/**
* Initialize loopback framework (called by n_smux.c).
*/
int smux_loopback_init(void)
{
int ret = 0;
spin_lock_init(&hw_fn_lock);
smux_loopback_wq = create_singlethread_workqueue("smux_loopback_wq");
if (IS_ERR(smux_loopback_wq)) {
pr_err("%s: failed to create workqueue\n", __func__);
return -ENOMEM;
}
ret |= kfifo_alloc(&smux_loop_pkt_fifo,
SMUX_LOOP_FIFO_SIZE * sizeof(struct smux_pkt_t *),
GFP_KERNEL);
return ret;
}
/**
* Simulate a write to the TTY hardware by duplicating
* the TX packet and putting it into the RX queue.
*
* @pkt Packet to write
*
* @returns 0 on success
*/
int smux_tx_loopback(struct smux_pkt_t *pkt_ptr)
{
struct smux_pkt_t *send_pkt;
unsigned long flags;
int i;
int ret;
/* duplicate packet */
send_pkt = smux_alloc_pkt();
send_pkt->hdr = pkt_ptr->hdr;
if (pkt_ptr->hdr.payload_len) {
ret = smux_alloc_pkt_payload(send_pkt);
if (ret) {
ret = -ENOMEM;
goto out;
}
memcpy(send_pkt->payload, pkt_ptr->payload,
pkt_ptr->hdr.payload_len);
}
/* queue duplicate as pseudo-RX data */
spin_lock_irqsave(&hw_fn_lock, flags);
i = kfifo_avail(&smux_loop_pkt_fifo);
if (i < sizeof(struct smux_pkt_t *)) {
pr_err("%s: no space in fifo\n", __func__);
ret = -ENOMEM;
goto unlock;
}
i = kfifo_in(&smux_loop_pkt_fifo,
&send_pkt,
sizeof(struct smux_pkt_t *));
if (i < 0) {
pr_err("%s: fifo error\n", __func__);
ret = -ENOMEM;
goto unlock;
}
queue_work(smux_loopback_wq, &smux_loopback_work);
ret = 0;
unlock:
spin_unlock_irqrestore(&hw_fn_lock, flags);
out:
return ret;
}
/**
* Receive loopback byte processor.
*
* @pkt Incoming packet
*/
static void smux_loopback_rx_byte(struct smux_pkt_t *pkt)
{
static int simulated_retry_cnt;
const char ack = SMUX_WAKEUP_ACK;
switch (pkt->hdr.flags) {
case SMUX_WAKEUP_REQ:
/* reply with ACK after appropriate delays */
++simulated_retry_cnt;
if (simulated_retry_cnt >= smux_simulate_wakeup_delay) {
pr_err("%s: completed %d of %d\n",
__func__, simulated_retry_cnt,
smux_simulate_wakeup_delay);
pr_err("%s: simulated wakeup\n", __func__);
simulated_retry_cnt = 0;
smux_rx_state_machine(&ack, 1, 0);
} else {
/* force retry */
pr_err("%s: dropping wakeup request %d of %d\n",
__func__, simulated_retry_cnt,
smux_simulate_wakeup_delay);
}
break;
case SMUX_WAKEUP_ACK:
/* this shouldn't happen since we don't send requests */
pr_err("%s: wakeup ACK unexpected\n", __func__);
break;
default:
/* invalid character */
pr_err("%s: invalid character 0x%x\n",
__func__, (unsigned)pkt->hdr.flags);
break;
}
}
/**
* Simulated remote hardware used for local loopback testing.
*
* @work Not used
*/
static void smux_loopback_rx_worker(struct work_struct *work)
{
struct smux_pkt_t *pkt;
struct smux_pkt_t reply_pkt;
char *data;
int len;
int lcid;
int i;
unsigned long flags;
data = kzalloc(SMUX_MAX_PKT_SIZE, GFP_ATOMIC);
spin_lock_irqsave(&hw_fn_lock, flags);
while (kfifo_len(&smux_loop_pkt_fifo) >= sizeof(struct smux_pkt_t *)) {
i = kfifo_out(&smux_loop_pkt_fifo, &pkt,
sizeof(struct smux_pkt_t *));
spin_unlock_irqrestore(&hw_fn_lock, flags);
if (pkt->hdr.magic != SMUX_MAGIC) {
pr_err("%s: invalid magic %x\n", __func__,
pkt->hdr.magic);
return;
}
lcid = pkt->hdr.lcid;
switch (pkt->hdr.cmd) {
case SMUX_CMD_OPEN_LCH:
if (smux_assert_lch_id(lcid)) {
pr_err("%s: invalid channel id %d\n",
__func__, lcid);
break;
}
if (pkt->hdr.flags & SMUX_CMD_OPEN_ACK)
break;
/* Reply with Open ACK */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = lcid;
reply_pkt.hdr.cmd = SMUX_CMD_OPEN_LCH;
reply_pkt.hdr.flags = SMUX_CMD_OPEN_ACK
| SMUX_CMD_OPEN_POWER_COLLAPSE;
reply_pkt.hdr.payload_len = 0;
reply_pkt.hdr.pad_len = 0;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
/* Send Remote Open */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = lcid;
reply_pkt.hdr.cmd = SMUX_CMD_OPEN_LCH;
reply_pkt.hdr.flags = SMUX_CMD_OPEN_POWER_COLLAPSE;
reply_pkt.hdr.payload_len = 0;
reply_pkt.hdr.pad_len = 0;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
break;
case SMUX_CMD_CLOSE_LCH:
if (smux_assert_lch_id(lcid)) {
pr_err("%s: invalid channel id %d\n",
__func__, lcid);
break;
}
if (pkt->hdr.flags == SMUX_CMD_CLOSE_ACK)
break;
/* Reply with Close ACK */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = lcid;
reply_pkt.hdr.cmd = SMUX_CMD_CLOSE_LCH;
reply_pkt.hdr.flags = SMUX_CMD_CLOSE_ACK;
reply_pkt.hdr.payload_len = 0;
reply_pkt.hdr.pad_len = 0;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
/* Send Remote Close */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = lcid;
reply_pkt.hdr.cmd = SMUX_CMD_CLOSE_LCH;
reply_pkt.hdr.flags = 0;
reply_pkt.hdr.payload_len = 0;
reply_pkt.hdr.pad_len = 0;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
break;
case SMUX_CMD_DATA:
if (smux_assert_lch_id(lcid)) {
pr_err("%s: invalid channel id %d\n",
__func__, lcid);
break;
}
/* Echo back received data */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = lcid;
reply_pkt.hdr.cmd = SMUX_CMD_DATA;
reply_pkt.hdr.flags = 0;
reply_pkt.hdr.payload_len = pkt->hdr.payload_len;
reply_pkt.payload = pkt->payload;
reply_pkt.hdr.pad_len = pkt->hdr.pad_len;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
break;
case SMUX_CMD_STATUS:
if (smux_assert_lch_id(lcid)) {
pr_err("%s: invalid channel id %d\n",
__func__, lcid);
break;
}
/* Echo back received status */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = lcid;
reply_pkt.hdr.cmd = SMUX_CMD_STATUS;
reply_pkt.hdr.flags = pkt->hdr.flags;
reply_pkt.hdr.payload_len = 0;
reply_pkt.payload = NULL;
reply_pkt.hdr.pad_len = pkt->hdr.pad_len;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
break;
case SMUX_CMD_PWR_CTL:
/* reply with ack */
smux_init_pkt(&reply_pkt);
reply_pkt.hdr.lcid = SMUX_BROADCAST_LCID;
reply_pkt.hdr.cmd = SMUX_CMD_PWR_CTL;
reply_pkt.hdr.flags = SMUX_CMD_PWR_CTL_ACK;
reply_pkt.hdr.payload_len = 0;
reply_pkt.payload = NULL;
reply_pkt.hdr.pad_len = pkt->hdr.pad_len;
smux_serialize(&reply_pkt, data, &len);
smux_rx_state_machine(data, len, 0);
break;
case SMUX_CMD_BYTE:
smux_loopback_rx_byte(pkt);
break;
default:
pr_err("%s: unknown command %d\n",
__func__, pkt->hdr.cmd);
break;
};
smux_free_pkt(pkt);
spin_lock_irqsave(&hw_fn_lock, flags);
}
spin_unlock_irqrestore(&hw_fn_lock, flags);
kfree(data);
}
|