net:wireless:Support eswin usb wifi ECR6600U
[platform/kernel/linux-starfive.git] / drivers / net / wireless / eswin / usb / usb.c
1 /**
2  ******************************************************************************
3  *
4  * @file usb.c
5  *
6  * @brief usb driver function definitions
7  *
8  * Copyright (C) ESWIN 2015-2020
9  *
10  ******************************************************************************
11  */
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/kthread.h>
15 #include <linux/gpio.h>
16 #include <linux/timer.h>
17 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
18 #include <uapi/linux/sched/types.h>
19 #endif
20 //#include <linux/usb.h>
21 #include "usb.h"
22
23 #include "core.h"
24 //#include "debug.h"
25 #include "ecrnx_usb.h"
26
27 #include "usb_host_interface.h"
28 #include "ecrnx_compat.h"
29 #include "fw_head_check.h"
30 #include "slave_log_buf.h"
31
32 #define USB_RX_TIMER_TIMEOUT_US          (200)
33 //TCP PKG MAX LEN:1594; UDP PKG MAX LEN:1582
34 #define USB_RX_LENGTH_THD                (1594)
35 #define USB_RX_UPLOAD_THD                (12)
36
37 static struct eswin_usb * g_usb;
38 static unsigned int rx_packets =0;
39 static struct timer_list usb_rx_timer = {0};
40
41 static void usb_refill_recv_transfer(struct usb_infac_pipe * pipe);
42
43
44 #define USB_LOG_MEM_SIZE        (512*8)//(512*16)
45 #define USB_LOG_MAX_SIZE        512
46
47 struct ring_buffer buf_handle = {0};
48
49 #if defined(CONFIG_ECRNX_DEBUGFS_CUSTOM)
50 struct ring_buffer *usb_dbg_buf_get(void)
51 {
52     if(buf_handle.init == false)
53     {
54         ring_buffer_init(&buf_handle, USB_LOG_MEM_SIZE);
55     }
56     return &buf_handle;
57 }
58 #endif
59
60 void usb_dbg_printf(void * data, int len)
61 {
62     if(buf_handle.init == false)
63     {
64         ring_buffer_init(&buf_handle, USB_LOG_MEM_SIZE);
65     }
66     ring_buffer_put(&buf_handle, data, len);
67 }
68
69
70 static struct usb_urb_context *
71 usb_alloc_urb_from_pipe(struct usb_infac_pipe *pipe)
72 {
73         struct usb_urb_context *urb_context = NULL;
74         unsigned long flags;
75
76         spin_lock_irqsave(&g_usb->cs_lock, flags);
77         if (!list_empty(&pipe->urb_list_head)) {
78                 urb_context = list_first_entry(&pipe->urb_list_head,
79                                                struct usb_urb_context, link);
80                 list_del(&urb_context->link);
81                 pipe->urb_cnt--;
82         }
83         spin_unlock_irqrestore(&g_usb->cs_lock, flags);
84
85         return urb_context;
86 }
87
88
89
90 static void usb_free_urb_to_infac(struct usb_infac_pipe * pipe,
91                                         struct usb_urb_context *urb_context)
92 {
93         unsigned long flags;
94
95         spin_lock_irqsave(&g_usb->cs_lock, flags);
96         pipe->urb_cnt++;
97         list_add(&urb_context->link, &pipe->urb_list_head);
98     if(urb_context->urb)
99     {
100         usb_unanchor_urb(urb_context->urb);
101         usb_free_urb(urb_context->urb);
102         urb_context->urb = NULL;
103     }
104         spin_unlock_irqrestore(&g_usb->cs_lock, flags);
105 }
106
107 static void usb_cleanup_recv_urb(struct usb_urb_context *urb_context)
108 {
109         dev_kfree_skb(urb_context->skb);
110         urb_context->skb = NULL;
111
112         usb_free_urb_to_infac(urb_context->pipe, urb_context);
113 }
114
115 static void usb_free_pipe_resources(struct usb_infac_pipe *pipe)
116 {
117         struct usb_urb_context *urb_context;
118
119         for (;;) {
120                 urb_context = usb_alloc_urb_from_pipe(pipe);
121
122                 if (!urb_context)
123                         break;
124         if(urb_context->urb)
125         {
126             usb_unanchor_urb(urb_context->urb);
127             usb_free_urb(urb_context->urb);
128             urb_context->urb = NULL;
129         }
130                 kfree(urb_context);
131         }
132 }
133
134 static void usb_transmit_complete(struct urb *urb)
135 {
136         struct usb_urb_context *urb_context = urb->context;
137         struct usb_infac_pipe *pipe = urb_context->pipe;
138         struct sk_buff *skb;
139         struct txdesc_api *tx_desc;
140         u32_l flag = 0;
141
142         //ECRNX_DBG(" %s entry, dir: %d!!", __func__, pipe->dir);
143
144         if (urb->status != 0) {
145         pipe->err_count++;
146         if(pipe->err_count%100 == 1)
147         {
148             ECRNX_PRINT( "pipe-dir: %d, failed:%d\n",
149                         pipe->dir, urb->status);
150         }
151         if((pipe->err_status != urb->status)||(pipe->err_count == 10000))
152         {
153             pipe->err_status = urb->status;
154             pipe->err_count = 0;
155         }
156         }
157
158         skb = urb_context->skb;
159         urb_context->skb = NULL;
160         usb_free_urb_to_infac(urb_context->pipe, urb_context);
161
162         flag = *(u32_l *)skb->data;
163         if((u8_l)(flag & FLAG_MSG_TYPE_MASK) == TX_FLAG_TX_DESC)
164         {
165                 tx_desc = (struct txdesc_api *)((u32_l *)skb->data + 1);
166                 if (tx_desc->host.flags & TXU_CNTRL_MGMT)
167                 {
168                         return;
169                 }
170         }
171
172         skb_queue_tail(&pipe->io_comp_queue, skb);
173 #ifdef CONFIG_ECRNX_KTHREAD
174         wake_up_interruptible(&g_usb->wait_tx_comp);
175 #endif
176 #ifdef CONFIG_ECRNX_WORKQUEUE
177         schedule_work(&pipe->io_complete_work);
178 #endif
179 #ifdef CONFIG_ECRNX_TASKLET
180         tasklet_schedule(&pipe->tx_tasklet);
181 #endif
182 }
183
184
185 void usb_rx_timer_handle(struct timer_list *time)
186 {
187     if (rx_packets)
188     {
189         rx_packets = 0;
190         #ifdef CONFIG_ECRNX_KTHREAD
191             wake_up_interruptible(&g_usb->wait_rx_comp);
192         #endif
193         #ifdef CONFIG_ECRNX_WORKQUEUE
194             schedule_work(&g_usb->infac_data.pipe_rx.io_complete_work);
195         #endif
196         #ifdef CONFIG_ECRNX_TASKLET
197             tasklet_schedule(&g_usb->infac_data.pipe_rx.rx_tasklet);
198         #endif
199     }
200 }
201
202 static void usb_recv_complete(struct urb *urb)
203 {
204         struct usb_urb_context *urb_context = urb->context;
205         struct usb_infac_pipe *pipe = urb_context->pipe;
206         struct sk_buff *skb;
207         int status = 0;
208
209         //ECRNX_PRINT(" %s entry, dir: %d!!", __func__, pipe->dir);
210
211
212         //ECRNX_PRINT( " usb recv pipe-dir %d stat %d len %d urb 0x%pK\n",
213         //         pipe->dir, urb->status, urb->actual_length,
214         //         urb);
215
216         if (urb->status != 0) {
217                 status = -EIO;
218                 switch (urb->status) {
219                 case -ECONNRESET:
220                 case -ENOENT:
221                 case -ESHUTDOWN:
222                         /* no need to spew these errors when device
223                          * removed or urb killed due to driver shutdown
224                          */
225                         status = -ECANCELED;
226                         break;
227                 default:
228             pipe->err_count++;
229             if(pipe->err_count%100 == 1)
230             {
231                             ECRNX_PRINT("usb redcv pipe-dir %d  failed: %d\n",
232                                    pipe->dir, urb->status);
233             }
234             if((pipe->err_status != status)||(pipe->err_count == 10000))
235             {
236                 pipe->err_status = status;
237                 pipe->err_count = 0;
238             }
239                         break;
240                 }
241                 goto cleanup_recv_urb;
242         }
243
244         if (urb->actual_length == 0)
245                 goto cleanup_recv_urb;
246
247         skb = urb_context->skb;
248
249         /* we are going to pass it up */
250         urb_context->skb = NULL;
251         skb_put(skb, urb->actual_length);
252
253         /* note: queue implements a lock */
254         skb_queue_tail(&pipe->io_comp_queue, skb);
255         usb_free_urb_to_infac(pipe, urb_context);
256
257 #if 0
258     usb_rx_cnt++;
259     //printk("skb->len:%d %d %d\n", skb->len, usb_rx_cnt, urb->actual_length);
260     if (skb->len < 1500 ||usb_rx_cnt > 12)
261         {
262             usb_rx_cnt = 0;
263             schedule_work(&pipe->io_complete_work);
264     }
265 #else
266
267     rx_packets++;
268     if (skb->len < USB_RX_LENGTH_THD || rx_packets > USB_RX_UPLOAD_THD)
269     {
270         rx_packets = 0;
271         #ifdef CONFIG_ECRNX_KTHREAD
272         wake_up_interruptible(&g_usb->wait_rx_comp);
273         #endif
274         #ifdef CONFIG_ECRNX_WORKQUEUE
275         schedule_work(&pipe->io_complete_work);
276         #endif
277         #ifdef CONFIG_ECRNX_TASKLET
278         tasklet_schedule(&pipe->rx_tasklet);
279         #endif
280     }
281     else
282     {
283         mod_timer(&usb_rx_timer, jiffies + usecs_to_jiffies(USB_RX_TIMER_TIMEOUT_US));
284     }
285 #endif
286
287         //ECRNX_DBG(" entry %s, len: %d\n", __func__, urb->actual_length);      
288         //print_hex_dump(KERN_INFO, "READ:", DUMP_PREFIX_ADDRESS, 32, 1, skb->data, urb->actual_length, false);
289
290         usb_refill_recv_transfer(pipe);
291         return;
292
293 cleanup_recv_urb:
294         usb_cleanup_recv_urb(urb_context);
295 }
296
297 #ifdef CONFIG_ECRNX_TASKLET
298 static void usb_tx_comp_tasklet(unsigned long data)
299 {
300         struct usb_infac_pipe *pipe = (struct usb_infac_pipe *)data;
301         struct sk_buff *skb;
302         struct txdesc_api *tx_desc;
303         u32_l flag = 0;
304         ptr_addr host_id;
305
306         while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
307                 //ECRNX_DBG(" %s skb dequeue, skb:0x%08x, skb len: %d!!", __func__, skb, skb->len);
308
309                 flag = *(u32_l *)skb->data;
310                 if((u8_l)(flag & FLAG_MSG_TYPE_MASK) != TX_FLAG_TX_DESC)
311                 {
312                         dev_kfree_skb(skb);
313                         continue;
314                 }
315                 tx_desc = (struct txdesc_api *)((u32_l *)skb->data + 1);
316                 if (g_usb->p_eswin->data_cfm_callback && ((u8_l)(flag & FLAG_MSG_TYPE_MASK) == TX_FLAG_TX_DESC) && !(tx_desc->host.flags & TXU_CNTRL_MGMT))
317                 {
318                         memcpy(&host_id, tx_desc->host.packet_addr, sizeof(ptr_addr));
319                         g_usb->p_eswin->data_cfm_callback(g_usb->p_eswin->umac_priv, (void*)host_id);
320                 }
321         }
322 }
323 #endif
324
325 #ifdef CONFIG_ECRNX_WORKQUEUE
326 static void usb_tx_comp_work(struct work_struct *work)
327 {
328     struct usb_infac_pipe *pipe = container_of(work,
329                                                struct usb_infac_pipe,
330                                                io_complete_work);
331     struct sk_buff *skb;
332     struct txdesc_api *tx_desc;
333     u32_l flag = 0;
334     ptr_addr host_id;
335
336     while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
337         //ECRNX_DBG(" %s skb dequeue, skb:0x%08x, skb len: %d!!", __func__, skb, skb->len);
338
339         flag = *(u32_l *)skb->data;
340         if((u8_l)(flag & FLAG_MSG_TYPE_MASK) != TX_FLAG_TX_DESC)
341         {
342             dev_kfree_skb(skb);
343             continue;
344         }
345         tx_desc = (struct txdesc_api *)((u32_l *)skb->data + 1);
346         if (g_usb->p_eswin->data_cfm_callback && ((u8_l)(flag & FLAG_MSG_TYPE_MASK) == TX_FLAG_TX_DESC) && !(tx_desc->host.flags & TXU_CNTRL_MGMT))
347         {
348             memcpy(&host_id, tx_desc->host.packet_addr, sizeof(ptr_addr));
349             g_usb->p_eswin->data_cfm_callback(g_usb->p_eswin->umac_priv, (void*)host_id);
350         }
351     }
352 }
353 #endif
354
355 #ifdef CONFIG_ECRNX_KTHREAD
356 void usb_tx_comp_cb(struct sk_buff_head *queue)
357 {
358     struct sk_buff *skb;
359     struct txdesc_api *tx_desc;
360     u32_l flag = 0;
361     ptr_addr host_id;
362
363     while ((skb = skb_dequeue(queue))) {
364         //ECRNX_DBG(" %s skb dequeue, skb:0x%08x, skb len: %d!!", __func__, skb, skb->len);
365
366         flag = *(u32_l *)skb->data;
367         if((u8_l)(flag & FLAG_MSG_TYPE_MASK) != TX_FLAG_TX_DESC)
368         {
369             dev_kfree_skb(skb);
370             continue;
371         }
372         tx_desc = (struct txdesc_api *)((u32_l *)skb->data + 1);
373         if (g_usb->p_eswin->data_cfm_callback && ((u8_l)(flag & FLAG_MSG_TYPE_MASK) == TX_FLAG_TX_DESC) && !(tx_desc->host.flags & TXU_CNTRL_MGMT))
374         {
375             memcpy(&host_id, tx_desc->host.packet_addr, sizeof(ptr_addr));
376             g_usb->p_eswin->data_cfm_callback(g_usb->p_eswin->umac_priv, (void*)host_id);
377         }
378     }
379 }
380
381 void usb_rx_comp_cb(struct usb_infac_pipe *pipe)
382 {
383     struct sk_buff *skb;
384     while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
385         if (g_usb->p_eswin->rx_callback)
386                 {
387                         g_usb->p_eswin->rx_callback(g_usb->p_eswin->umac_priv, skb, usb_pipeendpoint(pipe->usb_pipe_handle));
388                 }
389         else
390         {
391             if (skb)
392                         { // free the skb
393                 ECRNX_DBG("%s, skb free: 0x%x !! \n",__func__);
394                 dev_kfree_skb(skb);
395             }
396
397         }
398     }
399 }
400
401 static int eswin_tx_comp_thread(void *data)
402 {
403     struct eswin_usb * p_usb = (struct eswin_usb *)data;
404     int ret;
405
406 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 9, 0)
407     struct sched_param param = { .sched_priority = 1 };
408     param.sched_priority = 56;
409     sched_setscheduler(get_current(), SCHED_FIFO, &param);
410 #else
411     sched_set_fifo(get_current());
412 #endif
413     ECRNX_PRINT("eswin_tx_comp_thread entry\n");
414
415     while (!kthread_should_stop())
416     {
417         ret = wait_event_interruptible(p_usb->wait_tx_comp, skb_queue_len(&g_usb->infac_data.pipe_tx.io_comp_queue) != 0 ||
418                 skb_queue_len(&g_usb->infac_msg.pipe_tx.io_comp_queue) != 0 || kthread_should_stop());
419         if (ret < 0)
420         {
421             ECRNX_ERR("usb tx pkg thread error!\n");
422             return 0;
423         }
424         if(kthread_should_stop())
425         {
426             continue;
427         }
428         usb_tx_comp_cb(&g_usb->infac_msg.pipe_tx.io_comp_queue);
429         usb_tx_comp_cb(&g_usb->infac_data.pipe_tx.io_comp_queue);
430     }
431     ECRNX_PRINT("tx pkg thread exit\n");
432     return 0;
433 }
434
435 static int eswin_rx_comp_thread(void *data)
436 {
437     struct eswin_usb * p_usb = (struct eswin_usb *)data;
438     int ret;
439
440 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 9, 0)
441     struct sched_param param = { .sched_priority = 1 };
442     param.sched_priority = 56;
443     sched_setscheduler(get_current(), SCHED_FIFO, &param);
444 #else
445     sched_set_fifo(get_current());
446 #endif
447     ECRNX_PRINT("eswin_rx_comp_thread entry\n");
448
449     //ECRNX_DBG(" %s entry, dir: %d!!", __func__, pipe->dir);
450     while (!kthread_should_stop())
451     {
452         ret = wait_event_interruptible(p_usb->wait_rx_comp, skb_queue_len(&g_usb->infac_data.pipe_rx.io_comp_queue) != 0 ||
453                 skb_queue_len(&g_usb->infac_msg.pipe_rx.io_comp_queue) != 0|| kthread_should_stop());
454
455         if (ret < 0)
456         {
457             ECRNX_ERR("usb tx pkg thread error!\n");
458             return 0;
459         }
460         if(kthread_should_stop())
461         {
462             continue;
463         }
464         usb_rx_comp_cb(&g_usb->infac_msg.pipe_rx);
465         usb_rx_comp_cb(&g_usb->infac_data.pipe_rx);
466     }
467     ECRNX_PRINT("rx pkg thread exit\n");
468     return 0;
469 }
470 #endif
471
472 #ifdef CONFIG_ECRNX_TASKLET
473 static void usb_rx_comp_tasklet(unsigned long data)
474 {
475         struct usb_infac_pipe *pipe = (struct usb_infac_pipe *)data;
476         struct sk_buff *skb;
477
478         //ECRNX_DBG(" %s entry, dir: %d!!", __func__, pipe->dir);
479
480         while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
481                 if (g_usb->p_eswin->rx_callback)
482                 {
483                         g_usb->p_eswin->rx_callback(g_usb->p_eswin->umac_priv, skb, usb_pipeendpoint(pipe->usb_pipe_handle));
484                 }
485         else
486         {
487             if (skb)
488                         { // free the skb
489                 ECRNX_DBG("%s, skb free: 0x%x !! \n",__func__);
490                 dev_kfree_skb(skb);
491             }
492
493         }
494
495         }
496 }
497
498 #endif
499
500 #ifdef CONFIG_ECRNX_WORKQUEUE
501 static void usb_rx_comp_work(struct work_struct *work)
502 {
503     struct usb_infac_pipe *pipe = container_of(work,
504                                 struct usb_infac_pipe,
505                                 io_complete_work);
506     struct sk_buff *skb;
507     //ECRNX_DBG(" %s entry, dir: %d!!", __func__, pipe->dir);
508
509     while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
510         if (g_usb->p_eswin->rx_callback)
511                 {
512                         g_usb->p_eswin->rx_callback(g_usb->p_eswin->umac_priv, skb, usb_pipeendpoint(pipe->usb_pipe_handle));
513                 }
514         else
515         {
516             if (skb)
517                         { // free the skb
518                 ECRNX_DBG("%s, skb free: 0x%x !! \n",__func__);
519                 dev_kfree_skb(skb);
520             }
521
522         }
523     }
524 }
525 #endif
526
527 static void usb_flush_pipe(struct usb_infac_pipe * pipe)
528 {
529         usb_kill_anchored_urbs(&pipe->urb_submitted);
530     if (pipe->dir == USB_DIR_TX) {
531         #ifdef CONFIG_ECRNX_KTHREAD
532         if(g_usb->kthread_tx_comp)
533         {
534             kthread_stop(g_usb->kthread_tx_comp);
535             wake_up_interruptible(&g_usb->wait_tx_comp);
536             g_usb->kthread_tx_comp = NULL;
537         }
538         #endif
539         #ifdef CONFIG_ECRNX_TASKLET
540         tasklet_kill(&pipe->tx_tasklet);
541         #endif
542         #ifdef CONFIG_ECRNX_WORKQUEUE
543         cancel_work_sync(&pipe->io_complete_work);
544         #endif
545     } else {
546         #ifdef CONFIG_ECRNX_KTHREAD
547         if(g_usb->kthread_rx_comp)
548         {
549             kthread_stop(g_usb->kthread_rx_comp);
550             wake_up_interruptible(&g_usb->wait_rx_comp);
551             g_usb->kthread_rx_comp = NULL;
552         }
553         #endif
554         #ifdef CONFIG_ECRNX_TASKLET
555         tasklet_kill(&pipe->rx_tasklet);
556         #endif
557         #ifdef CONFIG_ECRNX_WORKQUEUE
558         cancel_work_sync(&pipe->io_complete_work);
559         #endif
560     }
561 }
562
563 static void usb_flush_all(struct eswin_usb * p_usb)
564 {
565         usb_flush_pipe(&p_usb->infac_data.pipe_rx);
566         usb_flush_pipe(&p_usb->infac_data.pipe_tx);
567         usb_flush_pipe(&p_usb->infac_msg.pipe_rx);
568         usb_flush_pipe(&p_usb->infac_msg.pipe_tx);
569 }
570
571
572 static void usb_refill_recv_transfer(struct usb_infac_pipe * pipe)
573 {
574         struct usb_urb_context *urb_context;
575         struct urb *urb;
576         int usb_status;
577
578         for ( ;; )  {
579                 urb_context = usb_alloc_urb_from_pipe(pipe);
580                 if (!urb_context)
581                         break;
582                 
583                 urb_context->skb = dev_alloc_skb(USB_RX_MAX_BUF_SIZE);
584                 if (!urb_context->skb)
585                         goto err;
586
587                 urb = usb_alloc_urb(0, GFP_ATOMIC);
588                 if (!urb)
589                         goto err;
590         urb_context->urb = urb;
591
592                 usb_fill_bulk_urb(urb,
593                                   pipe->infac->udev,
594                                   pipe->usb_pipe_handle,
595                                   urb_context->skb->data,
596                                   USB_RX_MAX_BUF_SIZE,
597                                   usb_recv_complete, urb_context);
598
599                 usb_anchor_urb(urb, &pipe->urb_submitted);
600                 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
601
602                 if (usb_status) {
603                         ECRNX_PRINT("usb_refill_recv_transfer, usb bulk recv failed: %d\n", 
604                                 usb_status);
605                         //usb_unanchor_urb(urb);
606                         //usb_free_urb(urb);
607                         goto err;
608                 }
609                 //usb_free_urb(urb);
610         }
611                 
612         return;
613
614 err:
615         usb_cleanup_recv_urb(urb_context);
616 }
617
618
619
620 static int usb_hif_start(struct eswin *tr)
621 {
622         struct eswin_usb * p_usb =  (struct eswin_usb *)tr->drv_priv;
623
624         ECRNX_DBG("%s entry!!", __func__);
625
626         usb_refill_recv_transfer(&p_usb->infac_data.pipe_rx);
627         usb_refill_recv_transfer(&p_usb->infac_msg.pipe_rx);
628         timer_setup(&usb_rx_timer, usb_rx_timer_handle, 0);
629
630         return 0;
631 }
632
633
634 int usb_hif_xmit(struct eswin *tr, struct sk_buff *skb)
635 {
636
637     unsigned int req_type = TX_FLAG_MAX;
638         struct eswin_usb * p_usb =  (struct eswin_usb *)tr->drv_priv;
639         struct usb_infac_data_t * infac_data = NULL;
640         struct usb_infac_pipe * pipe = NULL;
641         struct usb_urb_context *urb_context;
642         struct urb *urb;
643     int ret = -1;
644
645     req_type = *((unsigned int*)(skb->data));
646     if((req_type & FLAG_MSG_TYPE_MASK) == TX_FLAG_TX_DESC)
647     {
648         infac_data = &p_usb->infac_data;
649         pipe = &infac_data->pipe_tx;
650     }
651     else
652     {
653         infac_data = &p_usb->infac_msg;
654         pipe = &infac_data->pipe_tx;
655     }
656
657         urb_context = usb_alloc_urb_from_pipe(pipe);
658         if (!urb_context) {
659                 ret = -ENOMEM;
660                 goto err;
661         }
662         
663         urb_context->skb = skb;
664
665         urb = usb_alloc_urb(0, GFP_ATOMIC);
666         if (!urb) {
667                 ret = -ENOMEM;
668                 goto err_free_urb_to_pipe;
669         }
670
671     urb_context->urb = urb;
672         usb_fill_bulk_urb(urb,
673                           infac_data->udev,
674                           pipe->usb_pipe_handle,
675                           skb->data,
676                           skb->len,
677                           usb_transmit_complete, urb_context);
678
679         if (!(skb->len % infac_data->max_packet_size)) {
680                 /* hit a max packet boundary on this pipe */
681                 urb->transfer_flags |= URB_ZERO_PACKET;
682         }
683
684         usb_anchor_urb(urb, &pipe->urb_submitted);
685         ret = usb_submit_urb(urb, GFP_ATOMIC);
686         if (ret) {
687                 ECRNX_PRINT("usb_hif_xmit, usb bulk transmit failed: %d\n", ret);
688                 //usb_unanchor_urb(urb);
689                 ret = -EINVAL;
690                 goto err_free_urb_to_pipe;
691         }
692
693         //usb_free_urb(urb);
694
695         return 0;
696
697 err_free_urb_to_pipe:
698         usb_free_urb_to_infac(urb_context->pipe, urb_context);
699 err:
700         ECRNX_PRINT("usb_hif_xmit, pkg miss due to urb.\n");
701         skb_queue_tail(&pipe->io_comp_queue, skb);
702         #ifdef CONFIG_ECRNX_KTHREAD
703         wake_up_interruptible(&g_usb->wait_tx_comp);
704         #endif
705         #ifdef CONFIG_ECRNX_WORKQUEUE
706         schedule_work(&pipe->io_complete_work);
707         #endif
708         #ifdef CONFIG_ECRNX_TASKLET
709         tasklet_schedule(&pipe->tx_tasklet);
710         #endif
711         return ret;
712 }
713
714 static int usb_hif_write_raw(struct eswin *tr, const void* data, const u32 len)
715 {
716         struct eswin_usb * p_usb =  (struct eswin_usb *)tr->drv_priv;
717         struct usb_infac_data_t * infac_data = &p_usb->infac_data;
718         struct usb_infac_pipe * pipe = &infac_data->pipe_tx;
719
720         return usb_bulk_msg(infac_data->udev, pipe->usb_pipe_handle, (void*)data, len, NULL, 20000);
721 }
722
723 static int usb_hif_wait_ack(struct eswin *tr, void* data, const u32 len)
724 {
725         struct eswin_usb * p_usb =  (struct eswin_usb *)tr->drv_priv;
726         struct usb_infac_data_t * infac_data = &p_usb->infac_data;
727         struct usb_infac_pipe * pipe = &infac_data->pipe_rx;
728
729         return usb_bulk_msg(infac_data->udev, pipe->usb_pipe_handle, data, len, NULL, 20000);
730 }
731
732 #ifdef CONFIG_PM
733 static int usb_hif_suspend(struct eswin *tr)
734 {
735         return -EOPNOTSUPP;
736 }
737
738 static int usb_hif_resume(struct eswin *tr)
739 {
740         return -EOPNOTSUPP;
741 }
742 #endif
743
744 static struct usb_ops eswin_usb_hif_ops = {
745         .xmit                   = usb_hif_xmit,
746         .start                  = usb_hif_start,
747         .write                  = usb_hif_write_raw,
748         .wait_ack                       = usb_hif_wait_ack,
749         .suspend                = usb_hif_suspend,
750         .resume         = usb_hif_resume,
751 };
752
753 static int usb_create_pipe(struct usb_infac_pipe * pipe, int dir, bool flag)
754 {
755         int i;
756         struct usb_urb_context *urb_context;
757         ECRNX_DBG("%s entry, dir: %d!!", __func__, dir);
758
759         pipe->dir = dir;
760     pipe->err_count = 0;
761         init_usb_anchor(&pipe->urb_submitted);
762         INIT_LIST_HEAD(&pipe->urb_list_head);
763
764         for (i = 0; i < (flag ? USB_MSG_URB_NUM : USB_DATA_URB_NUM); i++) {
765                 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
766                 if (!urb_context)
767                         return -ENOMEM;
768
769                 urb_context->pipe = pipe;
770                 //pipe->urb_cnt++;
771                 
772                 usb_free_urb_to_infac(pipe, urb_context);
773         }
774
775         if (dir) {
776                 #ifdef CONFIG_ECRNX_WORKQUEUE
777                 INIT_WORK(&pipe->io_complete_work,  usb_tx_comp_work);
778                 #endif
779                 #ifdef CONFIG_ECRNX_TASKLET
780                 tasklet_init(&pipe->tx_tasklet, usb_tx_comp_tasklet, (unsigned long) pipe);
781                 #endif
782         } else {
783                 #ifdef CONFIG_ECRNX_WORKQUEUE
784                 INIT_WORK(&pipe->io_complete_work,  usb_rx_comp_work);
785                 #endif
786                 #ifdef CONFIG_ECRNX_TASKLET
787                 tasklet_init(&pipe->rx_tasklet, usb_rx_comp_tasklet, (unsigned long) pipe);
788                 #endif
789         }
790
791         skb_queue_head_init(&pipe->io_comp_queue);
792         return 0;
793 }
794
795 static int usb_create_infac(struct usb_interface *interface, struct usb_infac_data_t  * p_infac, bool flag)
796 {
797         struct usb_device *dev = interface_to_usbdev(interface);
798         struct usb_host_interface *iface_desc = interface->cur_altsetting;
799         struct usb_endpoint_descriptor *endpoint = NULL;
800
801         int i;
802         ECRNX_DBG("%s entry %d!!", __func__, iface_desc->desc.bNumEndpoints);
803
804         usb_get_dev(dev);
805         usb_set_intfdata(interface, p_infac);
806
807         p_infac->udev = dev;
808         p_infac->interface = interface;
809
810         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
811                 endpoint = &iface_desc->endpoint[i].desc;
812                 if (endpoint->bEndpointAddress & USB_DIR_MASK) {
813                         p_infac->pipe_rx.usb_pipe_handle = usb_rcvbulkpipe(dev, endpoint->bEndpointAddress);
814                         p_infac->pipe_rx.infac = p_infac;
815                         usb_create_pipe(&p_infac->pipe_rx, USB_DIR_RX, flag);
816                 } else {
817                         p_infac->pipe_tx.usb_pipe_handle = usb_sndbulkpipe(dev, endpoint->bEndpointAddress);
818                         p_infac->pipe_tx.infac = p_infac;
819                         usb_create_pipe(&p_infac->pipe_tx, USB_DIR_TX, flag);
820                 }
821         }
822         p_infac->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);;
823
824         return 0;
825 }
826
827 extern bool usb_status;
828 extern struct eswin *pEswin;
829 static int eswin_usb_probe(struct usb_interface *interface,
830                             const struct usb_device_id *id)
831 {
832         int ret;
833         struct eswin_usb * p_usb;
834         struct eswin* p_eswin;
835         struct usb_host_interface *iface_desc = interface->cur_altsetting;
836     struct usb_device *dev = interface_to_usbdev(interface);
837
838      if((usb_status == false) && dl_fw)
839      {
840         ECRNX_PRINT("%s entry, reset slave !!", __func__);
841         usb_control_msg(dev,
842             usb_sndctrlpipe(dev, 0),
843             0x2,
844             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
845             0,
846             0,
847             NULL, 0,10);
848         msleep(200);
849      }
850
851         ECRNX_PRINT("%s entry, func: %d, g_usb: %p !!", __func__, iface_desc->desc.bInterfaceNumber, g_usb);
852
853         if (iface_desc->desc.bInterfaceNumber == USB_INFAC_DATA) {
854                 if (g_usb) {
855                         p_usb = g_usb;
856             pEswin->dev = &interface->dev;
857                 } else {
858                         p_eswin = eswin_core_create(sizeof(struct eswin_usb), &interface->dev, &eswin_usb_hif_ops);
859                         if(!p_eswin) {
860                                 dev_err(&interface->dev, "failed to allocate core\n");
861                                 return -ENOMEM;
862                         }
863
864                         p_usb = (struct eswin_usb *)p_eswin->drv_priv;
865                         p_usb->p_eswin = p_eswin;
866                         g_usb = p_usb;
867                         spin_lock_init(&p_usb->cs_lock);
868                 }
869
870                 p_usb->dev = &interface->dev;
871
872                 usb_create_infac(interface, &p_usb->infac_data, 0);
873         } else {
874                 usb_create_infac(interface, &g_usb->infac_msg, 1);
875                 //usb_hif_start(g_usb->p_eswin);
876
877                 if (!g_usb->usb_enum_already) {
878                         ret = eswin_core_register(g_usb->p_eswin);
879                         if(ret) {
880                                 ECRNX_PRINT("failed to register core\n");
881                                 return ret;
882                         }
883                 } else {
884                         g_usb->usb_enum_already = 1;
885                 }
886                 return 0;
887         }
888
889         #ifdef CONFIG_ECRNX_KTHREAD
890         if(iface_desc->desc.bInterfaceNumber == USB_INFAC_DATA)
891         {
892                 init_waitqueue_head(&g_usb->wait_tx_comp);
893                 init_waitqueue_head(&g_usb->wait_rx_comp);
894
895                 g_usb->kthread_tx_comp = kthread_run(eswin_tx_comp_thread, g_usb, "tx_comp");
896                 g_usb->kthread_rx_comp = kthread_run(eswin_rx_comp_thread, g_usb, "rx_comp");
897
898                 if (IS_ERR(g_usb->kthread_tx_comp)) {
899                         g_usb->kthread_tx_comp = NULL;
900                         ECRNX_PRINT("kthread_tx_comp run fail\n");
901                 }
902
903                 if (IS_ERR(g_usb->kthread_rx_comp)) {
904                         g_usb->kthread_rx_comp = NULL;
905                         ECRNX_PRINT("kthread_rx_comp run fail\n");
906                 }
907                 ECRNX_PRINT("%s kthread_run success.", __func__);
908         }
909         #endif
910         ECRNX_PRINT("%s exit!!", __func__);
911         return 0;
912 }
913
914 static void eswin_usb_remove(struct usb_interface *interface)
915 {
916         struct usb_infac_data_t  * p_infac;
917         struct usb_host_interface *iface_desc = interface->cur_altsetting;
918
919         ECRNX_PRINT("%s entry!!", __func__);
920
921         p_infac = usb_get_intfdata(interface);
922
923         if (!p_infac) {
924                 ECRNX_PRINT("%s, p_infa is null!!", __func__);
925                 return;
926         }
927
928         if (iface_desc->desc.bInterfaceNumber == USB_INFAC_DATA) {
929                 eswin_core_unregister(g_usb->p_eswin);
930         } 
931
932         del_timer(&usb_rx_timer);
933
934         usb_flush_pipe(&p_infac->pipe_rx);
935         usb_flush_pipe(&p_infac->pipe_tx);
936         usb_free_pipe_resources(&p_infac->pipe_rx);
937         usb_free_pipe_resources(&p_infac->pipe_tx);
938
939         usb_set_intfdata(interface, NULL);
940         usb_put_dev(interface_to_usbdev(interface));
941         dl_fw = true;
942         offset = 0;
943 }
944
945
946 #ifdef CONFIG_PM
947 static int eswin_usb_pm_suspend(struct usb_interface *interface,        pm_message_t message)
948 {
949         ECRNX_PRINT("entry %s\n", __func__);
950         usb_flush_all(g_usb);
951         return 0;
952 }
953
954 static int eswin_usb_pm_resume(struct usb_interface *interface)
955 {
956         ECRNX_PRINT("entry %s\n", __func__);
957         usb_refill_recv_transfer(&g_usb->infac_data.pipe_rx);
958         usb_refill_recv_transfer(&g_usb->infac_msg.pipe_rx);
959         return 0;
960 }
961
962 #else
963 #define eswin_usb_pm_suspend NULL
964 #define eswin_usb_pm_resume NULL
965 #endif
966
967 /* table of devices that work with this driver */
968 static struct usb_device_id eswin_usb_ids[] = {
969         {USB_DEVICE(0x3452, 0x6600)},
970         { /* Terminating entry */ },
971 };
972
973 MODULE_DEVICE_TABLE(usb, eswin_usb_ids);
974 static struct usb_driver eswin_usb_driver = {
975         .name = "eswin_usb",
976         .probe = eswin_usb_probe,
977         .suspend = eswin_usb_pm_suspend,
978         .resume = eswin_usb_pm_resume,
979         .disconnect = eswin_usb_remove,
980         .id_table = eswin_usb_ids,
981         .supports_autosuspend = true,
982 };
983
984 static int __init eswin_usb_init(void)
985 {
986         int ret;
987
988         #ifdef CONFIG_POWERKEY_GPIO
989         int pk_gpio = 4;
990         pk_gpio = CONFIG_POWERKEY_GPIO;
991         gpio_direction_output(pk_gpio,0);
992         msleep(1000);
993         gpio_direction_output(pk_gpio,1);
994         #endif
995
996         ECRNX_PRINT("%s entry !!\n", __func__);
997         ret = usb_register(&eswin_usb_driver);
998         if (ret)
999                 ECRNX_PRINT("sdio driver registration failed: %d\n", ret);
1000
1001         ECRNX_PRINT("%s exit !!\n", __func__);
1002         return ret;
1003 }
1004
1005 static void __exit eswin_usb_exit(void)
1006 {
1007         ECRNX_PRINT("%s entry !!\n", __func__);
1008         usb_deregister(&eswin_usb_driver);
1009         ECRNX_PRINT("%s exit !!\n", __func__);
1010 }
1011
1012 void ecrnx_usb_reset_sync_fw(void)
1013 {
1014     usb_control_msg(g_usb->infac_msg.udev,
1015         usb_sndctrlpipe(g_usb->infac_msg.udev, 0),
1016         0x2,
1017         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1018         0,
1019         0,
1020         NULL, 0,10);
1021 }
1022
1023 int ecrnx_usb_register_drv(void)
1024 {
1025     return eswin_usb_init();
1026 }
1027
1028 void ecrnx_usb_unregister_drv(void)
1029 {
1030     eswin_core_unregister(g_usb->p_eswin);
1031     ecrnx_usb_reset_sync_fw();
1032     return eswin_usb_exit();
1033 }
1034
1035 struct device *eswin_usb_get_dev(void *plat)
1036 {
1037     struct eswin* tr = (struct eswin*)plat;
1038     
1039     return tr->dev;
1040 }
1041