lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*************************************************************************************
 *Description: usb download
 *Seekwave tech LTD
 *Author: jiayong.yang/junwei.jiang
 *Date:20210527
 *Modify:
 * ***********************************************************************************/
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/errno.h>
#ifdef CONFIG_DEBUG_FS
#include <linux/debugfs.h>
#endif
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/ctype.h>
#include "skw_usb_log.h"
#include "usb_boot.h"
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int dl_mps;
static int dloader_port = 0;
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int check_modem_status_from_connect_message(void)
{
   struct connect_ack *ack = (void *)&connect_ack[12];
   memcpy(skw_chipid,ack->chip_id,16);
   dl_mps = ack->packet_size;
   if(ack->flags.bitmap.boot)
       return NORMAL_BOOT;
   else
       return NORMAL_BOOT;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int dloader_write(char *msg, int msg_len, int *actual, int timeout)
{
   return bulkout_write_timeout(dloader_port, msg, msg_len, actual, timeout);
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int dloader_read(char *msg, int msg_len, int *actual, int timeout)
{
   return bulkin_read_timeout(dloader_port, msg, msg_len, actual, timeout);
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int compare_msg(const char *src, const char *dst, size_t count)
{
   unsigned char c1, c2;
 
   while (count) {
       c1 = *src++;
       c2 = *dst++;
       if (c1 != c2)
           return c1 < c2 ? -1 : 1;
       count--;
   }
   return 0;
}
 
static int dloader_send_data(const char *command, int command_len, const char *ack, int ack_len)
{
   int actual_len = 0;
   int ret;
   void *data;
   int data_size = 128;
 
   data = kzalloc(data_size, GFP_KERNEL);
 
   if (!data)
       return -ENOMEM;
   /* send command */
   ret = dloader_write((char *)command, command_len, &actual_len, 3000);
   if (ret <0 || actual_len != command_len) {
       skw_usb_err(" send cmd error ret %d actual_len %d command_len %d\n",
               ret, actual_len, command_len);
   } else {
       if (ack == NULL)
           goto OUT;
 
       /* read ack and check it */
       ret = dloader_read(data, data_size, &actual_len, 3000);
       if (ret <0 || ack_len > actual_len || compare_msg(ack, data, ack_len)) {
           skw_usb_err(" ack is NACK:ret == %d\n", ret);
           print_hex_dump(KERN_ERR, "ACK ERR:", 0, 16, 1, data, ack_len, 1);
           ret = -EIO;
       }
   }
OUT:
   kfree(data);
   return ret;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int dloader_send_command(const char *command,  int command_len, const char *ack, int ack_len)
{
   int actual_len = 0;
   int ret;
   void *data;
   int data_size = 128;
 
   data = kzalloc(data_size, GFP_KERNEL);
   if (!data)
       return -ENOMEM;
   /* send command */
   memcpy(data, (char*)command, command_len);
   ret = dloader_write(data, command_len, &actual_len, 3000);
   if (ret <0 || actual_len != command_len) {
       skw_usb_err(" send cmd error ret %d actual_len %d command_len %d\n",
               ret, actual_len, command_len);
   } else {
       /* read ack */
       ret = dloader_read(data, data_size, &actual_len, 3000);
       if (ret <0) {
           skw_usb_warn(" ack is NACK: acklen ===%d- actual_len ==%d--ret == %d\n",
               ret, ack_len, actual_len);
       }
   }
   if ((command_len > 8) &&(0 == command[8])){
       if(actual_len > sizeof(connect_ack))
           actual_len = sizeof(connect_ack);
       memcpy(connect_ack, data, actual_len);
   }
   kfree(data);
   return ret;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static unsigned short crc16_calculate(unsigned char *buf, int len)
{
   unsigned int i;
   unsigned short crc = 0;
 
   while (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;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int dloader_command_start_download(unsigned int addr, unsigned int len)
{
 
   int command_len = 20;
   char command[20] = {0x7E, 0x7E, 0x7E, 0x7E,/* head */
       0x08, 0x00, 0x00, 0x00, /*length */
       0x01, 0x00, /* message type, 01: start command */
       0x00, 0x00, /*crc for data body, excludes message header */
       0x00, 0x00, 0x10, 0x00,/*addr*/
       0x60, 0xb3, 0x06, 0x00 /*image size*/};
 
   *((u32 *)&command[12]) = addr;
   *((u32 *)&command[16]) = len;
 
   *((u16 *)&command[10]) = cpu_to_be16(crc16_calculate(&command[12], command_len - 12));
   return dloader_send_command(command, command_len, common_ack, sizeof(common_ack));
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int dloader_command_exec(unsigned int addr)
{
   unsigned short command_len = 16;
 
   char command[16] = {0x7E,0x7E,0x7E,0x7E, /* head */
       0x04, 0x00, 0x00, 0x00,
       0x04, 0x00, /*command type */
       0x43, 0x63, /*command len */
       0x00, 0x00, 0x10, 0x00 /*addr*/
       };
   *((u32 *)&command[12]) = addr;
   *((u16 *)&command[10]) = crc16_calculate(&command[12], 4);
   return dloader_send_command(command, command_len, exec_ack, sizeof(exec_ack));
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int dloader_setup_usb_connection(struct usb_port_struct *port)
{
   int ret;
 
   dloader_command_client_probe();
   if (ret < 0) {
       dev_err(&port->udev->dev, "get version error\n");
       return ret;
   }
   dloader_command_connect();
   if (ret < 0) {
       dev_err(&port->udev->dev, "connection  error\n");
       return ret;
   }
   dev_info(&port->udev->dev,"dloader connect susscess...\n");
   return 0;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int dloader_execute_image(struct usb_port_struct *port,unsigned int addr)
{
   int ret;
 
   ret = dloader_command_exec(addr);
   if (ret < 0) {
       dev_err(&port->udev->dev, "exec command is error\n");
       return ret;
   }
   return 0;
}
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:junwei.jiang
 *Date:
 *Modify:
 * ************************************************************************/
static unsigned int dloader_send_pdata(char* buf, const void *pdata, unsigned int len)
{
   PACKET_T *packet_ptr = (PACKET_T *)buf;
   int command_len = len + PACKET_HEADER_SIZE;
 
   packet_ptr->magic = PACKET_MAGIC;
   packet_ptr->type = 0x0002;
   packet_ptr->size = len;
   packet_ptr->crc = 0x0000;
   memset(packet_ptr->content, 0 , len);
   memcpy(packet_ptr->content,pdata, len);
 
   //crc check sum
   packet_ptr->crc = cpu_to_be16(crc16_calculate((char*)(&(packet_ptr->content)), command_len));
 
   return command_len;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int usb_download_image(struct usb_port_struct *port, unsigned int addr, unsigned int len)
{
   int ret;
   int size;
   int offset = 0;
   int img_size = 0;
   int temp_size = 0;
 
   /*the first command connect*/
   ret = dloader_command_start_download(addr, len);
   if (ret < 0) {
       dev_err(&port->udev->dev,"start download command failed\n");
       return ret;
   }
   /*get the data and the sv6160.bin size*/
   img_size = len;
 
   while (img_size > 0) {
       temp_size = MIN(dl_mps, img_size-offset);
       if(!temp_size)
           return 0;
       size  = dloader_send_pdata(port->read_buffer, (void*)(firmware_data)+offset, temp_size);
       if (size%512==0 && temp_size < dl_mps) {
           temp_size = temp_size>>1;
           size  = dloader_send_pdata(port->read_buffer, (void*)(firmware_data)+offset, temp_size);
       }  
       ret = dloader_send_data(port->read_buffer, size, common_ack, sizeof(common_ack));
       if (ret < 0) {
           dev_err(&port->udev->dev, "donwload img  error\n");
           return ret;
       }
       offset += temp_size;
   }
   return 0;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int dloader_get_chip_id(void *buf, unsigned int buf_size)
{
   int len = strlen(usb_ports[0]->udev->product);
   memcpy(buf, usb_ports[0]->udev->product, strlen(usb_ports[0]->udev->product));
   return len;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
int dloader_dump_from_romcode_usb(unsigned int addr, void *buf, int len)
{
   int ret;
   unsigned short command_len = 16;
   char command[20] = {0x7E, 0x7E, 0x7E, 0x7E,/* head */
       0x08, 0x00, 0x00, 0x00, /*for command data len */
       0x00, 0x09, /*command type */
       0x00, 0x00, /*for crc*/
       0x00, 0x00, 0x00, 0x00,/*addr*/
       0x00, 0x00, 0x00, 0x00,/*data len*/
       };
   int actual_len = 0;
   int size;
 
   //*((u32 *)&command[12]) = cpu_to_be32(addr);
   //*((u32 *)&command[16]) = cpu_to_be32(len);
   *((u32 *)&command[12]) = addr;
   *((u32 *)&command[16]) = len;
 
   *((u16 *)&command[10]) = cpu_to_be16(crc16_calculate(&command[1], command_len - 4));
 
 
   ret = dloader_send_command(command, command_len, NULL, 0);
   if (ret < 0) {
       skw_usb_err(" send command error\n");
       return -EIO;
   }
 
   size = dl_mps;
   while(len > 0) {
       if (len < size)
           size = len;
       ret = dloader_read(buf, size, &actual_len, 3000);
       if (ret < 0)
           skw_usb_err("dloader_read_ack dump memory error\n");
       else len -= actual_len;
   }
   return ret;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static int dloader_dump_read_usb(struct usb_port_struct *port)
{
   int ret;
   ret = dloader_dump_from_romcode_usb(START_ADDR, port->read_buffer, MAX_IMAGE_SIZE);
   return ret;
}
 
/***************************************************************************
 * Description:
 *Seekwave tech LTD
 *Author:
 *Date:
 *Modify:
 * ************************************************************************/
static void dloader_work(struct work_struct *work)
{
   struct usb_port_struct *port = container_of(work, struct usb_port_struct, work);
   int ret;
   dloader_port = port->portno;
   dloader_setup_usb_connection(port);
 
   ret = check_modem_status_from_connect_message();
   if (ret == HANG_REBOOT){
       dloader_dump_read_usb(port);
   }
   if(usb_boot_data->dram_dl_size > 0){
       firmware_data = usb_boot_data->dram_img_data;
       ret = usb_download_image(port, usb_boot_data->dram_dl_addr, usb_boot_data->dram_dl_size);
       if(ret <0)
           skw_usb_warn(" dram download img fail !!!!\n");
   }
 
   if(!ret && usb_boot_data->iram_dl_size > 0){
       firmware_data = usb_boot_data->iram_img_data;
       ret = usb_download_image(port, usb_boot_data->iram_dl_addr, usb_boot_data->iram_dl_size);
   }
   modem_notify_event(DEVICE_BOOTUP_EVENT);
   if (!ret)
       ret = dloader_execute_image(port, START_ADDR);
 
   if (ret < 0 && chip_en_gpio >= 0) {
       modem_status = MODEM_DOWNLOAD_FAILED;
       skw_usb_info("download failed! power off device\n");
       gpio_set_value(chip_en_gpio, 0);
       msleep(10);
   }
}