hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* SPDX-License-Identifier: GPL-2.0 */
#include "usb_boot.h"
 
#define DOWNLOAD_TX_CHN 2
#define DOWNLOAD_RX_CHN 18
 
static int ackncmp(const char *cs, const char *ct, size_t count)
{
   unsigned char c1, c2;
 
   while (count) {
       c1 = *cs++;
       c2 = *ct++;
       if (c1 != c2)
           return c1 < c2 ? -1 : 1;
       count--;
   }
   return 0;
}
 
static int usb_download_sent_command(const char *command,
       unsigned int command_len, const char *ack, unsigned int ack_len)
{
   struct wcn_usb_ep *tx_ep = wcn_usb_store_get_epFRchn(DOWNLOAD_TX_CHN);
   struct wcn_usb_ep *rx_ep = wcn_usb_store_get_epFRchn(DOWNLOAD_RX_CHN);
   int actual_len;
   int ret;
   char *command_temp;
   void *data;
   int data_size = 128;
 
   if (!tx_ep || !rx_ep) {
       wcn_usb_err("no ep\n");
       return -EIO;
   }
 
   data = kzalloc(data_size, GFP_KERNEL);
   if (!data)
       return -ENOMEM;
 
   command_temp = kmalloc(command_len, GFP_KERNEL);
   if (!command_temp) {
       ret = -ENOMEM;
       goto OUT_FREE_DATA;
   }
 
   memcpy(command_temp, command, command_len);
   /* sent command */
   ret = wcn_usb_msg(tx_ep, command_temp, command_len, &actual_len, 3000);
   if (ret || actual_len != command_len) {
       wcn_usb_err("wcn %s sent msg is error [ret %d actual_len %d command_len %d\n",
               __func__, ret, actual_len, command_len);
       ret = -EIO;
       goto OUT;
   }
 
   if (ack == NULL)
       goto OUT;
 
   /* get ack and compare */
   ret = wcn_usb_msg(rx_ep, data, data_size, &actual_len, 3000);
   if (ret || ack_len > actual_len || ackncmp(ack, data, ack_len)) {
       wcn_usb_err("wcn %s ack is not ok\n", __func__);
       print_hex_dump(KERN_DEBUG, "WCN ack", 0, 16, 1,
               data, data_size, 1);
       print_hex_dump(KERN_DEBUG, "WCN command", 0, 16, 1,
               command, command_len, 1);
       ret = -EIO;
       goto OUT;
   }
 
OUT:
   kfree(command_temp);
OUT_FREE_DATA:
   kfree(data);
 
   return ret;
}
 
static unsigned short crc16_xmodem_cal(unsigned char *buf, unsigned int buf_len)
{
   unsigned int i;
   unsigned short crc = 0;
 
   while (buf_len-- != 0) {
       for (i = 0x80; i != 0; i = i >> 1) {
           if ((crc & 0x8000) != 0) {
               crc = crc << 1;
               crc = crc ^ 0x1021;
           } else {
               crc = crc << 1;
           }
 
           if ((*buf & i) != 0)
               crc = crc ^ 0x1021;
       }
 
       buf++;
   }
 
   return crc;
}
 
static const char get_version[] = { 0x7E };
static const char get_version_ack[] = {
   0x7E, 0x00, 0x81, 0x00, 0x06, 0x53, 0x50,
   0x52, 0x44, 0x33, 0x00, 0x57, 0x0A, 0x7E};
static const char connect[] = {
   0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E};
static const char common_ack[] = {
   0x7E, 0x00, 0x80, 0x00, 0x00, 0x3B, 0x5A, 0x7E};
static const char end_download[] = {
   0x7E, 0x00, 0x03, 0x00, 0x00, 0x59, 0x50, 0x7E};
 
unsigned short usb_download_command_replace(char *command, unsigned short len)
{
   unsigned short tail = len - 1;
   unsigned short i;
   unsigned short j;
 
   /* ho head and no tail */
   for (i = len - 2; i > 0; i--) {
       if (command[i] == 0x7E || command[i] == 0x7D) {
           tail += 1;
           for (j = tail; j > i; j--)
               command[j] = command[j - 1];
           command[i + 1] = 0x50 | (command[i + 1] & 0x0F);
           command[i] = 0x7D;
       }
   }
 
   return tail + 1;
}
 
int usb_download_command_start_download(unsigned int addr, unsigned int len)
{
   unsigned short command_len = 16;
   char command[20] = {0x7E,/* head */
       0x00, 0x01, /*command type */
       0x00, 0x08, /*for command data len */
       0x00, 0x00, 0x00, 0x00,/*addr*/
       0x00, 0x00, 0x00, 0x00,/*data len*/
       0x00, 0x00, /*for crc*/
       0x7E /*tail*/};
 
 
   *((u32 *)&command[5]) = cpu_to_be32(addr);
   *((u32 *)&command[9]) = cpu_to_be32(len);
 
   *((u16 *)&command[command_len - 3]) =
       cpu_to_be16(crc16_xmodem_cal(&command[1], command_len - 4));
 
   command_len = usb_download_command_replace(command, 16);
 
   return usb_download_sent_command(command, command_len,
           common_ack, sizeof(common_ack));
}
 
int usb_download_command_exec(unsigned int addr)
{
   unsigned short command_len = 12;
   char command[16] = {0x7E,/* head */
       0x00, 0x04, /*command type */
       0x00, 0x04, /*command len */
       0x00, 0x00, 0x00, 0x00, /*addr*/
       0x00, 0x00, /*for crc*/
       0x7E /*tail*/};
 
   *((u32 *)&command[5]) = cpu_to_be32(addr);
 
   *((u16 *)&command[command_len - 3]) =
       cpu_to_be16(crc16_xmodem_cal(&command[1], command_len - 4));
 
   command_len = usb_download_command_replace(command, 12);
 
   return usb_download_sent_command(command, command_len,
           common_ack, sizeof(common_ack));
}
 
#define usb_download_command_get_version()                \
   usb_download_sent_command(get_version, sizeof(get_version),    \
           get_version_ack, sizeof(get_version_ack))
#define usb_download_command_connect()                \
   usb_download_sent_command(connect, sizeof(connect),    \
           common_ack, sizeof(common_ack))
#define usb_download_command_end_download()                \
   usb_download_sent_command(end_download, sizeof(end_download),    \
           common_ack, sizeof(common_ack))
 
int marlin_get_version(void)
{
   return usb_download_command_get_version();
}
 
int marlin_connet(void)
{
   return usb_download_command_connect();
}
 
int marlin_firmware_download_start_usb(void)
{
   int ret;
 
   ret = usb_download_command_get_version();
   if (ret) {
       wcn_usb_err("%s start command is error\n", __func__);
       return ret;
   }
 
   ret = usb_download_command_connect();
   if (ret) {
       wcn_usb_err("%s connect command is error\n", __func__);
       return ret;
   }
 
   return 0;
}
 
int marlin_firmware_download_exec_usb(unsigned int addr)
{
   int ret;
 
   ret = usb_download_command_exec(addr);
   if (ret) {
       wcn_usb_err("%s exec command is error\n", __func__);
       return ret;
   }
 
   return 0;
}
 
int marlin_firmware_download_usb(unsigned int addr, const void *buf,
       unsigned int len, unsigned int packet_max)
{
   int ret;
 
   ret = usb_download_command_start_download(addr, len);
   if (ret) {
       wcn_usb_err("start download command is error\n");
       return ret;
   }
 
   ret = usb_download_sent_command(buf, len,
           common_ack, sizeof(common_ack));
   if (ret) {
       wcn_usb_err("donwload img is error\n");
       return ret;
   }
 
   ret = usb_download_command_end_download();
   if (ret) {
       wcn_usb_err("end donwload command is error\n");
       return ret;
   }
 
   return 0;
}
 
int marlin_firmware_get_chip_id(void *buf, unsigned int buf_size)
{
   int ret;
   unsigned short command_len = 8;
   char command[16] = {0x7E,/* head */
       0x00, 0x08, /*command type */
       0x00, 0x00, /*command len */
       0x00, 0x00, /*for crc*/
       0x7E /*tail*/};
   struct wcn_usb_ep *rx_ep = wcn_usb_store_get_epFRchn(DOWNLOAD_RX_CHN);
   int actual_len;
   char *temp_buf;
#define temp_buf_size 16
 
   if (!rx_ep) {
       wcn_usb_err("%s no rx_ep\n", __func__);
       return -EIO;
   }
 
   temp_buf = kzalloc(temp_buf_size, GFP_KERNEL);
   if (!temp_buf)
       return -ENOMEM;
 
   *((u16 *)&command[command_len - 3]) =
       cpu_to_be16(crc16_xmodem_cal(&command[1], command_len - 4));
 
   command_len = usb_download_command_replace(command, 8);
 
   ret = usb_download_sent_command(command, command_len, NULL, 0);
   if (ret) {
       wcn_usb_err("%s send command error\n", __func__);
       kfree(temp_buf);
       return -EIO;
   }
 
   ret = wcn_usb_msg(rx_ep, temp_buf, temp_buf_size, &actual_len, 3000);
   if (ret) {
       wcn_usb_err("%s get chip id error\n", __func__);
       kfree(temp_buf);
       return ret;
   }
 
   if (temp_buf[0] != 0x7e || temp_buf[2] != 0x08) {
       wcn_usb_err("%s get chip id error\n", __func__);
       print_hex_dump(KERN_ERR, "WCN ack", 0, 16, 1,
               temp_buf, temp_buf_size, 1);
       kfree(temp_buf);
       return ret;
   }
 
   memcpy(buf, &temp_buf[5], buf_size);
 
   kfree(temp_buf);
   return ret;
 
}
 
 
int marlin_dump_from_romcode_usb(unsigned int addr, void *buf, int len)
{
   int ret;
   unsigned short command_len = 16;
   char command[20] = {0x7E,/* head */
       0x00, 0x09, /*command type */
       0x00, 0x08, /*for command data len */
       0x00, 0x00, 0x00, 0x00,/*addr*/
       0x00, 0x00, 0x00, 0x00,/*data len*/
       0x00, 0x00, /*for crc*/
       0x7E /*tail*/};
   struct wcn_usb_ep *rx_ep = wcn_usb_store_get_epFRchn(DOWNLOAD_RX_CHN);
   int actual_len;
 
   if (!rx_ep) {
       wcn_usb_err("%s no rx_ep\n", __func__);
       return -EIO;
   }
 
   wcn_usb_info("%s addr is 0x%x len is 0x%x\n", __func__, addr, len);
   *((u32 *)&command[5]) = cpu_to_be32(addr);
   *((u32 *)&command[9]) = cpu_to_be32(len);
 
   *((u16 *)&command[command_len - 3]) =
       cpu_to_be16(crc16_xmodem_cal(&command[1], command_len - 4));
 
   command_len = usb_download_command_replace(command, 16);
 
   ret = usb_download_sent_command(command, command_len, NULL, 0);
   if (ret) {
       wcn_usb_err("%s send command error\n", __func__);
       return -EIO;
   }
 
   ret = wcn_usb_msg(rx_ep, buf, len, &actual_len, 3000);
   if (ret)
       wcn_usb_err("%s dump memory error\n", __func__);
 
   return ret;
}
 
int marlin_dump_read_usb(unsigned int addr, char *buf, int len)
{
   int ret;
   static int first_pac;
 
   if (first_pac == 0) {
       first_pac = 1;
       marlin_firmware_download_start_usb();
   }
 
   ret = marlin_dump_from_romcode_usb(addr, buf, len);
 
   return ret;
}