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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* 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/slab.h>
#include <linux/delay.h>
#include <linux/diagchar.h>
#include <linux/kmemleak.h>
#include <linux/err.h>
#include <linux/workqueue.h>
#include <linux/ratelimit.h>
#include <linux/platform_device.h>
#include "diag_mux.h"
#include "diagfwd_bridge.h"
#ifdef CONFIG_USB_QCOM_DIAG_BRIDGE
#include "diagfwd_hsic.h"
#include "diagfwd_sdio.h"
#endif
#ifdef CONFIG_MSM_MHI
#include "diagfwd_mhi.h"
#endif
#include "diag_dci.h"
#ifndef CONFIG_USB_QCOM_DIAG_BRIDGE
static int diag_hsic_init(void)
{
return -EINVAL;
}
#endif
#ifndef CONFIG_MSM_MHI
static int diag_mhi_init(void)
{
return -EINVAL;
}
#endif
#ifndef CONFIG_QTI_SDIO_CLIENT
static int diag_sdio_init(void)
{
return -EINVAL;
}
#endif
#define BRIDGE_TO_MUX(x) (x + DIAG_MUX_BRIDGE_BASE)
struct diagfwd_bridge_info bridge_info[NUM_REMOTE_DEV] = {
{
.id = DIAGFWD_MDM,
.type = DIAG_DATA_TYPE,
.name = "MDM",
.inited = 0,
.ctxt = 0,
.dev_ops = NULL,
.dci_read_ptr = NULL,
.dci_read_buf = NULL,
.dci_read_len = 0,
.dci_wq = NULL,
},
{
.id = DIAGFWD_SMUX,
.type = DIAG_DATA_TYPE,
.name = "SMUX",
.inited = 0,
.ctxt = 0,
.dci_read_ptr = NULL,
.dev_ops = NULL,
.dci_read_buf = NULL,
.dci_read_len = 0,
.dci_wq = NULL,
},
{
.id = DIAGFWD_MDM_DCI,
.type = DIAG_DCI_TYPE,
.name = "MDM_DCI",
.inited = 0,
.ctxt = 0,
.dci_read_ptr = NULL,
.dev_ops = NULL,
.dci_read_buf = NULL,
.dci_read_len = 0,
.dci_wq = NULL,
},
};
static int diagfwd_bridge_mux_connect(int id, int mode)
{
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
if (bridge_info[id].dev_ops && bridge_info[id].dev_ops->open)
bridge_info[id].dev_ops->open(bridge_info[id].ctxt);
return 0;
}
static int diagfwd_bridge_mux_disconnect(int id, int mode)
{
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
if ((mode == DIAG_USB_MODE &&
driver->logging_mode == DIAG_MEMORY_DEVICE_MODE) ||
(mode == DIAG_MEMORY_DEVICE_MODE &&
driver->logging_mode == DIAG_USB_MODE)) {
/*
* Don't close the MHI channels when usb is disconnected
* and a process is running in memory device mode.
*/
return 0;
}
if (bridge_info[id].dev_ops && bridge_info[id].dev_ops->close)
bridge_info[id].dev_ops->close(bridge_info[id].ctxt);
return 0;
}
static int diagfwd_bridge_mux_read_done(unsigned char *buf, int len, int id)
{
return diagfwd_bridge_write(id, buf, len);
}
static int diagfwd_bridge_mux_write_done(unsigned char *buf, int len,
int buf_ctx, int id)
{
struct diagfwd_bridge_info *ch = NULL;
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
ch = &bridge_info[buf_ctx];
if (ch->dev_ops && ch->dev_ops->fwd_complete)
ch->dev_ops->fwd_complete(ch->ctxt, buf, len, 0);
return 0;
}
static struct diag_mux_ops diagfwd_bridge_mux_ops = {
.open = diagfwd_bridge_mux_connect,
.close = diagfwd_bridge_mux_disconnect,
.read_done = diagfwd_bridge_mux_read_done,
.write_done = diagfwd_bridge_mux_write_done
};
static void bridge_dci_read_work_fn(struct work_struct *work)
{
struct diagfwd_bridge_info *ch = container_of(work,
struct diagfwd_bridge_info,
dci_read_work);
if (!ch)
return;
diag_process_remote_dci_read_data(ch->id, ch->dci_read_buf,
ch->dci_read_len);
if (ch->dev_ops && ch->dev_ops->fwd_complete) {
ch->dev_ops->fwd_complete(ch->ctxt, ch->dci_read_ptr,
ch->dci_read_len, 0);
}
}
int diagfwd_bridge_register(int id, int ctxt, struct diag_remote_dev_ops *ops)
{
int err = 0;
struct diagfwd_bridge_info *ch = NULL;
char wq_name[DIAG_BRIDGE_NAME_SZ + 10];
if (!ops) {
pr_err("diag: Invalid pointers ops: %pK ctxt: %d\n", ops, ctxt);
return -EINVAL;
}
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
ch = &bridge_info[id];
ch->ctxt = ctxt;
ch->dev_ops = ops;
switch (ch->type) {
case DIAG_DATA_TYPE:
err = diag_mux_register(BRIDGE_TO_MUX(id), id,
&diagfwd_bridge_mux_ops);
if (err)
return err;
break;
case DIAG_DCI_TYPE:
ch->dci_read_buf = kzalloc(DIAG_MDM_BUF_SIZE, GFP_KERNEL);
if (!ch->dci_read_buf)
return -ENOMEM;
ch->dci_read_len = 0;
strlcpy(wq_name, "diag_dci_", 10);
strlcat(wq_name, ch->name, sizeof(ch->name));
INIT_WORK(&(ch->dci_read_work), bridge_dci_read_work_fn);
ch->dci_wq = create_singlethread_workqueue(wq_name);
if (!ch->dci_wq) {
kfree(ch->dci_read_buf);
return -ENOMEM;
}
break;
default:
pr_err("diag: Invalid channel type %d in %s\n", ch->type,
__func__);
return -EINVAL;
}
return 0;
}
int diag_remote_dev_open(int id)
{
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
bridge_info[id].inited = 1;
if (bridge_info[id].type == DIAG_DATA_TYPE)
return diag_mux_queue_read(BRIDGE_TO_MUX(id));
else if (bridge_info[id].type == DIAG_DCI_TYPE)
return diag_dci_send_handshake_pkt(bridge_info[id].id);
return 0;
}
void diag_remote_dev_close(int id)
{
return;
}
int diag_remote_dev_read_done(int id, unsigned char *buf, int len)
{
int err = 0;
struct diagfwd_bridge_info *ch = NULL;
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
ch = &bridge_info[id];
if (ch->type == DIAG_DATA_TYPE) {
err = diag_mux_write(BRIDGE_TO_MUX(id), buf, len, id);
if (ch->dev_ops && ch->dev_ops->queue_read)
ch->dev_ops->queue_read(ch->ctxt);
return err;
}
/*
* For DCI channels copy to the internal buffer. Don't queue any
* further reads. A read should be queued once we are done processing
* the current packet
*/
if (len <= 0 || len > DIAG_MDM_BUF_SIZE) {
pr_err_ratelimited("diag: Invalid len %d in %s, ch: %s\n",
len, __func__, ch->name);
return -EINVAL;
}
ch->dci_read_ptr = buf;
memcpy(ch->dci_read_buf, buf, len);
ch->dci_read_len = len;
queue_work(ch->dci_wq, &ch->dci_read_work);
return 0;
}
int diag_remote_dev_write_done(int id, unsigned char *buf, int len, int ctxt)
{
int err = 0;
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
if (bridge_info[id].type == DIAG_DATA_TYPE) {
if (buf == driver->hdlc_encode_buf)
driver->hdlc_encode_buf_len = 0;
/*
* For remote processor, the token offset is stripped from the
* buffer. Account for the token offset while checking against
* the original buffer
*/
if (buf == (driver->user_space_data_buf + sizeof(int)))
driver->user_space_data_busy = 0;
err = diag_mux_queue_read(BRIDGE_TO_MUX(id));
} else {
err = diag_dci_write_done_bridge(id, buf, len);
}
return err;
}
int diagfwd_bridge_init(int xprt)
{
int err = 0;
if (xprt == 1)
err = diag_mhi_init();
else if (xprt == 2)
err = diag_sdio_init();
else
err = diag_hsic_init();
if (err)
goto fail;
return 0;
fail:
pr_err("diag: Unable to initialze diagfwd bridge, err: %d\n", err);
return err;
}
void diagfwd_bridge_exit(void)
{
#ifdef USB_QCOM_DIAG_BRIDGE
diag_hsic_exit();
#endif
}
int diagfwd_bridge_close(int id)
{
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
if (bridge_info[id].dev_ops && bridge_info[id].dev_ops->close)
return bridge_info[id].dev_ops->close(bridge_info[id].ctxt);
return 0;
}
int diagfwd_bridge_write(int id, unsigned char *buf, int len)
{
if (id < 0 || id >= NUM_REMOTE_DEV)
return -EINVAL;
if (bridge_info[id].dev_ops && bridge_info[id].dev_ops->write) {
return bridge_info[id].dev_ops->write(bridge_info[id].ctxt,
buf, len, 0);
}
return 0;
}
uint16_t diag_get_remote_device_mask(void)
{
int i;
uint16_t remote_dev = 0;
for (i = 0; i < NUM_REMOTE_DEV; i++) {
if (bridge_info[i].inited &&
bridge_info[i].type == DIAG_DATA_TYPE) {
remote_dev |= 1 << i;
}
}
return remote_dev;
}
|