aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/c2k_ccci/rawbulk_transfer.c
blob: aadf0db9e17cd9e32e35b9027bd6922eb7fa3050 (plain) (blame)
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
/*
 * Rawbulk Driver from VIA Telecom
 * Copyright (C) 2011 VIA Telecom, Inc.
 * Author: Karfield Chen (kfchen@via-telecom.com)
 * Copyright (C) 2012 VIA Telecom, Inc.
 * Author: Juelun Guo (jlguo@via-telecom.com)
 * Changes:
 *
 * Sep 2012: Juelun Guo <jlguo@via-telecom.com>
 *           Version 1.0.4
 *           changed to support for sdio bypass.
 * 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.
 *
 *
 *
 * Rawbulk is transfer performer between CBP host driver and Gadget driver
 *
 *
 * upstream:    CBP Driver ---> Gadget IN
 * downstream:  Gadget OUT ---> CBP Driver
 *
 *
 */
 
/* #define DEBUG */
/* #define VERBOSE_DEBUG */

#define DRIVER_AUTHOR   "Juelun Guo <jlguo@via-telecom.com>"
#define DRIVER_DESC     "Rawbulk Driver - perform bypass for QingCheng"
#define DRIVER_VERSION  "1.0.4"

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <mach/viatel_rawbulk.h>
#include <linux/moduleparam.h>
#include "modem_sdio.h"

#define terr(t, fmt, args...) \
    printk(KERN_ERR "Rawbulk [%s]:" fmt "\n", t->name,  ##args)
#define STOP_UPSTREAM   0x1
#define STOP_DOWNSTREAM 0x2

extern int modem_buffer_push(int port_num, const unsigned char *buf, int count);
#ifdef CONFIG_EVDO_DT_VIA_SUPPORT
char * transfer_name[] = {"modem", "ets", "at", "pcv", "gps"};
#else
char * transfer_name[] = {"pcv", "modem", "ets", "at", "gps"};
#endif
	
unsigned int upstream_data[_MAX_TID] = {0};
unsigned int upstream_cnt[_MAX_TID] = {0};
unsigned int total_drop[_MAX_TID] = {0};
unsigned int alloc_fail[_MAX_TID] = {0};
unsigned int total_tran[_MAX_TID] = {0};

extern unsigned long volatile jiffies;
static unsigned long drop_check_timeout;
static unsigned int udata[_MAX_TID] = {0};
static unsigned int ucnt[_MAX_TID] = {0};

struct rawbulk_transfer {
    enum transfer_id id;
    spinlock_t lock;
    int control;
    
    struct usb_function *function;
    struct usb_interface *interface;
    rawbulk_autoreconn_callback_t autoreconn;
    struct {
        int ntrans;
        struct list_head transactions;
        struct usb_ep *ep;
    } upstream, downstream, repush2modem, cache_buf_lists;
    
    int sdio_block;
    int down_flow;
    spinlock_t usb_down_lock;
    spinlock_t modem_block_lock;
    struct delayed_work delayed;
    struct workqueue_struct *flow_wq;
    
    struct work_struct      read_work;
    struct work_struct      write_work;    
    struct workqueue_struct *rx_wq;
    struct workqueue_struct *tx_wq;
    struct mutex modem_up_mutex;
    struct mutex usb_up_mutex;
    struct timer_list timer;
    spinlock_t flow_lock;
};

static inline int get_epnum(struct usb_host_endpoint *ep) {
    return (int)(ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
}

static inline int get_maxpacksize(struct usb_host_endpoint *ep) {
    return (int)(le16_to_cpu(ep->desc.wMaxPacketSize));
}

struct cache_buf {
        int length;
        struct list_head clist;
        struct rawbulk_transfer *transfer;
        int state;
        //unsigned char buffer[0];
        char *buffer;
};

#define MAX_RESPONSE    32
struct rawbulk_transfer_model {
    struct usb_device *udev;
    struct usb_composite_dev *cdev;
    char ctrl_response[MAX_RESPONSE];
    struct rawbulk_transfer transfer[_MAX_TID];
};
static struct rawbulk_transfer_model *rawbulk;

static struct rawbulk_transfer *id_to_transfer(int transfer_id) {
    if (transfer_id < 0 || transfer_id >= _MAX_TID)
        return NULL;
    return &rawbulk->transfer[transfer_id];
}

//extern int rawbulk_usb_state_check(void);

/*
 * upstream
 */

#define UPSTREAM_STAT_FREE          0
#define UPSTREAM_STAT_UPLOADING     2

struct upstream_transaction {
    int state;
    int stalled;
    char name[32];
    struct list_head tlist;
    struct delayed_work delayed;
    struct rawbulk_transfer *transfer;
    struct usb_request *req;
    int buffer_length;
    //unsigned char buffer[0];
    char *buffer;
};

static unsigned int dump_mask = 0;
static unsigned int full_dump = 0;
static unsigned int max_cache_cnt = 2048;
static unsigned int base_cache_cnt = 1024;
static unsigned int up_note_sz = 1024*1024;
static unsigned int drop_check_interval = 1;
unsigned int c2k_usb_dbg_level = C2K_LOG_NOTICE;

module_param(c2k_usb_dbg_level, uint, S_IRUGO | S_IWUSR);
module_param(dump_mask, uint, S_IRUGO | S_IWUSR);
module_param(full_dump, uint, S_IRUGO | S_IWUSR);
module_param(max_cache_cnt, uint, S_IRUGO | S_IWUSR);
module_param(base_cache_cnt, uint, S_IRUGO | S_IWUSR);
module_param(drop_check_interval, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(dump_mask, "Set data dump mask for each transfers");

#ifdef C2K_USB_UT
int delay_set = 1200;
module_param(delay_set, uint, S_IRUGO | S_IWUSR);
#endif

static inline void dump_data(struct rawbulk_transfer *trans,
        const char *str, const unsigned char *data, int size) {
	int i;
	char verb[128], *pbuf;
    if (!(dump_mask & (1 << trans->id)))
        return;

	pbuf = verb;
	pbuf += sprintf(pbuf, "DUMP tid = %d, %s: len = %d, chars = \"",
            trans->id, str, size);

	/* data in ascii */
#if 0	
    for (i = 0; i < size; ++i) {
        char c = data[i];
        if (c > 0x20 && c < 0x7e) {
			pbuf += sprintf(pbuf, "%c", c);
        } else {
			pbuf += sprintf(pbuf, ".");
        }
        if (i > 7)
            break;
    }
#endif

	pbuf += sprintf(pbuf, "\", data = ");
    for (i = 0; i < size; ++i) {
		pbuf += sprintf(pbuf, "%.2x ", data[i]);
		if(!full_dump){
			if (i > 7)
				break;
		}
    }
    if (full_dump || size < 8) {
		/* buf to printk */
		printk(KERN_WARNING "%s\n", verb);
        return;
    }
	/* data in tail */
#if 1	
	else if (i < size - 8) {
		pbuf += sprintf(pbuf, "... ");
        i = size - 8;
    }
    for (; i < size; ++i)
		pbuf += sprintf(pbuf, "%.2x ", data[i]);
#endif
	/* buf to printk */
	printk(KERN_WARNING "%s\n", verb);
}

static struct upstream_transaction *
alloc_upstream_transaction(struct rawbulk_transfer *transfer, int bufsz)
{
    struct upstream_transaction *t;
    C2K_NOTE("%s\n", __func__);
    
    //t = kmalloc(sizeof *t + bufsz * sizeof(unsigned char), GFP_KERNEL);
    t = kmalloc(sizeof(struct upstream_transaction), GFP_KERNEL);
    if (!t)
        return NULL;
        
    t->buffer = (char *)__get_free_page(GFP_KERNEL);
    //t->buffer = kmalloc(bufsz, GFP_KERNEL);
    if (!t->buffer) {
        kfree(t);
        return NULL;
    }   
    t->buffer_length = bufsz;

    t->req = usb_ep_alloc_request(transfer->upstream.ep, GFP_KERNEL);
    if (!t->req)
        goto failto_alloc_usb_request;
    t->req->context = t;
    t->name[0] = 0;
    sprintf(t->name, "U%d ( G:%s)", transfer->upstream.ntrans,
            transfer->upstream.ep->name);         

    INIT_LIST_HEAD(&t->tlist);
    list_add_tail(&t->tlist, &transfer->upstream.transactions);
    transfer->upstream.ntrans ++;
    t->transfer = transfer;
    t->state = UPSTREAM_STAT_FREE;
    return t;

failto_alloc_usb_request:
    //kfree(t->buffer);
    free_page((unsigned long) t->buffer);
    kfree(t);
    return NULL;
}

static void free_upstream_transaction(struct rawbulk_transfer *transfer) {
    struct list_head *p, *n;
    C2K_NOTE("%s\n", __func__);
    
    mutex_lock(&transfer->usb_up_mutex);
    list_for_each_safe(p, n, &transfer->upstream.transactions) {
        struct upstream_transaction *t = list_entry(p, struct
                upstream_transaction, tlist);

        list_del(p);
        //kfree(t->buffer);
        free_page((unsigned long) t->buffer);
        usb_ep_free_request(transfer->upstream.ep, t->req);
        kfree(t);

        transfer->upstream.ntrans --;
    }
    mutex_unlock(&transfer->usb_up_mutex);
}

static void free_upstream_sdio_buf(struct rawbulk_transfer *transfer) {    
    struct list_head *p, *n;
    C2K_NOTE("%s\n", __func__);
    
    mutex_lock(&transfer->modem_up_mutex);
    list_for_each_safe(p, n, &transfer->cache_buf_lists.transactions) {
        struct cache_buf *c = list_entry(p, struct
                cache_buf, clist);
        list_del(p);
        //kfree(c->buffer);
        free_page((unsigned long) c->buffer);
        kfree(c);
        transfer->cache_buf_lists.ntrans--;
    }
    mutex_unlock(&transfer->modem_up_mutex);
}

static void upstream_complete(struct usb_ep *ep, struct usb_request
        *req);

static void start_upstream(struct work_struct * work) {
    int ret = -1, got = 0;
    struct upstream_transaction *t;
    struct rawbulk_transfer *transfer = container_of(work, struct rawbulk_transfer, write_work);
    struct cache_buf *c;
    int length;
    char * buffer;
    int part1_sz,part2_sz;
    int stop, retry = 0;
    int flag, temp;
    int i;
    struct usb_request *req;
    C2K_DBG("%s\n", __func__);

    mutex_lock(&transfer->modem_up_mutex);

    list_for_each_entry(c, &transfer->cache_buf_lists.transactions, clist) {
        if (c && (c->state == UPSTREAM_STAT_UPLOADING) && !(transfer->control & STOP_UPSTREAM)) {               
            ret = 0;
            break;
        }
    }
    mutex_unlock(&transfer->modem_up_mutex);
            
    if (ret < 0) {
            return;
    }
      
    if (!c){
        return;
    }
    
    length = c->length;
    buffer = c->buffer;
   
reget: 
      mutex_lock(&transfer->usb_up_mutex);     
      list_for_each_entry(t, &transfer->upstream.transactions, tlist) {
            if (t && (t->state == UPSTREAM_STAT_FREE) && !(transfer->control & STOP_UPSTREAM)) {                
                ret = 0;
                retry = 0;
				got = 1;
                break;
            }           
      }
      mutex_unlock(&transfer->usb_up_mutex);
      if (ret < 0) {            
            if(transfer->control & STOP_UPSTREAM) {             
                return;
            }            
            retry = 1;
      }
      
      if (retry) {           
             C2K_NOTE("%s: up request is buzy, try to get usb request\n", __func__);          
             goto reget;
      }
      if (!t->req || got == 0) {
            return;
      }     
      req = t->req;

      memcpy(t->buffer, buffer, length);
      dump_data(transfer, "pushing up", t->buffer, length);
      req->length = length;
      req->buf = t->buffer;
      req->complete = upstream_complete;
      req->zero = ((length % transfer->upstream.ep->maxpacket) == 0);
      t->state = UPSTREAM_STAT_UPLOADING;      
      //if(rawbulk_usb_state_check()) {
           ret = usb_ep_queue(transfer->upstream.ep, req, GFP_ATOMIC);
      //} else
      //     return;
      if(ret < 0) {
           terr(t, "fail to queue request, %d", ret);
           t->state = UPSTREAM_STAT_FREE;
           return;
      }  	    
	  c->state = UPSTREAM_STAT_FREE;
/*    length = c->length;
    buffer = c->buffer;
    part1_sz = length - (length & 511);
    part2_sz = length & 511;
    
    for(i = 0; i < 2; i++) {
        //printk("%s i = %d**3**\n", __func__, i);  
        if (i == 0) {
            flag = 1;
            temp = part1_sz;
            if(part1_sz <= 0)
             continue;
        } else {
            flag = 0;
            temp = part2_sz;
            if(part2_sz <= 0)
             continue;
        } 
        ret = -1;
reget: 
      mutex_lock(&transfer->usb_up_mutex);      
      list_for_each_entry(t, &transfer->upstream.transactions, tlist) {
            if (t && (t->state == UPSTREAM_STAT_FREE) && !(transfer->control & STOP_UPSTREAM)) {                
                ret = 0;
                retry = 0;
                break;
            }           
      }
      mutex_unlock(&transfer->usb_up_mutex);
      if (ret < 0) {            
            if(transfer->control & STOP_UPSTREAM) {             
                return;
            }            
            retry = 1;
      }
      
    
      if (retry) {
             printk("%s goto reget usb request\n", __func__);           
             goto reget;
      }
      if (!t->req) {
            return;
      }     
      req = t->req;

      memcpy(t->buffer, buffer, temp);
      dump_data(transfer, "pushing up", t->buffer, temp);
      buffer += temp;
      length -= temp;
      req->length = temp;
      req->buf = t->buffer;
      req->complete = upstream_complete;
      //req->zero = ((length % transfer->upstream.ep->maxpacket) == 0);
      req->short_not_ok = flag;
      t->state = UPSTREAM_STAT_UPLOADING;      
      if(rawbulk_usb_state_check()) {
           ret = usb_ep_queue(transfer->upstream.ep, req, GFP_ATOMIC);
           //printk("%s %d: after requeue\n", __func__, __LINE__);
      } else
           return;
      if(ret < 0) {
           terr(t, "fail to queue request, %d", ret);
           t->state = UPSTREAM_STAT_FREE;
           return;
      }  	    
      c->state = UPSTREAM_STAT_FREE; 
   }*/
}

static void upstream_complete(struct usb_ep *ep,
        struct usb_request *req) {
    struct upstream_transaction *t = req->context;
    struct rawbulk_transfer *transfer = t->transfer;
    C2K_DBG("%s\n", __func__);
    
    t->state = UPSTREAM_STAT_FREE; 
           
    if (req->status < 0) {            
        /*if (req->status == -ESHUTDOWN)
            return;
        else
            terr(t, "req status %d", req->status);*/
        printk(" %s: req status %d\n", __func__, req->status);
        return;
    }

    if (!req->actual)
        terr(t, "req actual 0");

	/* update statistics */
	upstream_data[transfer->id] += req->actual;
	upstream_cnt[transfer->id]++;
	udata[transfer->id] += req->actual;
	ucnt[transfer->id]++;

	if(udata[transfer->id] >= up_note_sz){
		C2K_NOTE("t<%d>,%d Bytes upload\n", transfer->id, udata[transfer->id]);
		udata[transfer->id] = 0;
		ucnt[transfer->id] = 0;
	}

    queue_work(transfer->tx_wq, &transfer->write_work);
}

static void stop_upstream(struct upstream_transaction *t) {
    struct rawbulk_transfer *transfer = t->transfer;
    C2K_NOTE("%s, %p, %p\n", __func__, transfer->upstream.ep, t->req);
   	
    if (t->state == UPSTREAM_STAT_UPLOADING) {
		usb_ep_dequeue(transfer->upstream.ep, t->req);
    }
    t->state = UPSTREAM_STAT_FREE;
}

int rawbulk_push_upstream_buffer(int transfer_id, const void *buffer,
        unsigned int length) {
    int ret = -ENOENT;
    struct rawbulk_transfer *transfer;
    int count = length;
    struct cache_buf *c;
    C2K_DBG("%s\n", __func__);
    
    if(transfer_id > (FLS_CH_ID-1))
         transfer_id--;
    else if(transfer_id == (FLS_CH_ID-1)) {
         C2K_ERR("channal %d is flashless, no nessesory to bypass\n", (FLS_CH_ID-1));
         return 0;
    }
    
    C2K_DBG("%s:transfer_id = %d, length = %d\n", __func__, transfer_id, length);

    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return -ENODEV;
    
    mutex_lock(&transfer->modem_up_mutex);
	list_for_each_entry(c, &transfer->cache_buf_lists.transactions, clist) {
		if (c && (c->state == UPSTREAM_STAT_FREE) && !(transfer->control & STOP_UPSTREAM)) {
			list_move_tail(&c->clist, &transfer->cache_buf_lists.transactions);

			c->state= UPSTREAM_STAT_UPLOADING;
			ret = 0;
			break;
		}
	}

	/* dynamic got cache pool */
	if(ret < 0 && transfer->cache_buf_lists.ntrans < max_cache_cnt){
		c = kmalloc(sizeof(struct cache_buf), GFP_KERNEL);
		if (!c) {
			C2K_NOTE("fail to allocate upstream sdio buf n %d\n", transfer_id);
		}

		c->buffer = (char *)__get_free_page(GFP_KERNEL);
		//c->buffer = kmalloc(upsz, GFP_KERNEL);
		if (!c) {
			kfree(c);
			C2K_NOTE("fail to allocate upstream sdio buf n %d\n", transfer_id);
		}
		c->state= UPSTREAM_STAT_UPLOADING;
		INIT_LIST_HEAD(&c->clist);
		list_add_tail(&c->clist, &transfer->cache_buf_lists.transactions);
        transfer->cache_buf_lists.ntrans++;
		total_tran[transfer_id] = transfer->cache_buf_lists.ntrans;
		C2K_NOTE("new cache, t<%d>, trans<%d>, alloc_fail<%d>, upstream<%d,%d>\n", 
				transfer_id, 
				transfer->cache_buf_lists.ntrans, 
				alloc_fail[transfer_id], 
				upstream_data[transfer_id], 
				upstream_cnt[transfer_id]);
		ret = 0;
	}

    if (ret < 0) {
		total_drop[transfer_id] += length;
	
		if(time_after(jiffies, drop_check_timeout)){	
			C2K_NOTE("cahce full, t<%d>, drop<%d>, total_drop<%d>, trans<%d>, alloc_fail<%d>, upstream<%d,%d>\n", 
					transfer_id, 
					length, 
					total_drop[transfer_id], 
					transfer->cache_buf_lists.ntrans,
					alloc_fail[transfer_id], 
					upstream_data[transfer_id], 
					upstream_cnt[transfer_id]);

			drop_check_timeout  = jiffies + HZ * drop_check_interval;
		}
		mutex_unlock(&transfer->modem_up_mutex);
		return -ENOMEM;
	}
    mutex_unlock(&transfer->modem_up_mutex);

    memcpy(c->buffer, buffer, count);  
    c->length = count;
    dump_data(transfer, "pushing up", c->buffer, count);
    queue_work(transfer->tx_wq, &transfer->write_work);
    return count;
}
EXPORT_SYMBOL_GPL(rawbulk_push_upstream_buffer);

/*
 * downstream
 */

#define DOWNSTREAM_STAT_FREE        0
#define DOWNSTREAM_STAT_DOWNLOADING 2

struct downstream_transaction {
    int state;
    int stalled;
    char name[32];
    struct list_head tlist;
    struct rawbulk_transfer *transfer;
    struct usb_request *req;
    int buffer_length;
    //unsigned char buffer[0];
    char *buffer;
};

static void downstream_delayed_work(struct work_struct *work);

static void downstream_complete(struct usb_ep *ep,
        struct usb_request *req);

static struct downstream_transaction *alloc_downstream_transaction(
        struct rawbulk_transfer *transfer, int bufsz) {
    struct downstream_transaction *t;
    C2K_NOTE("%s\n", __func__);
    
    //t = kzalloc(sizeof *t + bufsz * sizeof(unsigned char), GFP_ATOMIC);
    t = kmalloc(sizeof(struct downstream_transaction), GFP_ATOMIC);
    if (!t)
        return NULL;
        
    t->buffer = (char *)__get_free_page(GFP_ATOMIC);
    //t->buffer = kmalloc(bufsz, GFP_ATOMIC);
    if (!t->buffer) {
        kfree(t);
        return NULL;
    }
    t->buffer_length = bufsz;
    t->req = usb_ep_alloc_request(transfer->downstream.ep, GFP_ATOMIC);
    if (!t->req)
        goto failto_alloc_usb_request;

    t->name[0] = 0;

    INIT_LIST_HEAD(&t->tlist);
    list_add_tail(&t->tlist, &transfer->downstream.transactions);
   
    transfer->downstream.ntrans ++;
    t->transfer = transfer;
    t->state = DOWNSTREAM_STAT_FREE;
    t->stalled = 0;
    t->req->context = t;
    
    return t;

failto_alloc_usb_request:
    //kfree(t->buffer);
    free_page((unsigned long) t->buffer);
    kfree(t);
    return NULL;
}

static void free_downstream_transaction(struct rawbulk_transfer *transfer) {
    struct list_head *p, *n;
    unsigned long flags;
    C2K_NOTE("%s\n", __func__);
    
    spin_lock_irqsave(&transfer->usb_down_lock, flags);
    list_for_each_safe(p, n, &transfer->downstream.transactions) {
        struct downstream_transaction *t = list_entry(p, struct
                downstream_transaction, tlist);

        list_del(p);
        //kfree(t->buffer);
        if(t->buffer)/*NULL pointer when ETS switch*/
            free_page((unsigned long) t->buffer);
        usb_ep_free_request(transfer->downstream.ep, t->req);
        kfree(t);

        transfer->downstream.ntrans --;
    }
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
}

static void stop_downstream(struct downstream_transaction *t) {
    struct rawbulk_transfer *transfer = t->transfer;
    
    if (t->state == DOWNSTREAM_STAT_DOWNLOADING) {
        usb_ep_dequeue(transfer->downstream.ep, t->req);
        t->state = DOWNSTREAM_STAT_FREE;
    }
}

static int queue_downstream(struct downstream_transaction *t) {
    int rc=0;
    struct rawbulk_transfer *transfer = t->transfer;
    struct usb_request *req = t->req;
    C2K_DBG("%s\n", __func__);
    
    req->buf = t->buffer;
    req->length = t->buffer_length;
    req->complete = downstream_complete;
    //if (rawbulk_usb_state_check())
        rc = usb_ep_queue(transfer->downstream.ep, req, GFP_ATOMIC);
    //else
    //    return;
    if (rc < 0) {
        return rc;
    }

    t->state = DOWNSTREAM_STAT_DOWNLOADING;
    return 0;
}

static int start_downstream(struct downstream_transaction *t) {
    int rc=0;
    struct rawbulk_transfer *transfer = t->transfer;
    struct usb_request *req = t->req;
    int time_delayed = msecs_to_jiffies(1);
    C2K_DBG("%s\n", __func__);
       
    if (transfer->control & STOP_DOWNSTREAM) {
        //t->state = DOWNSTREAM_STAT_FREE;
        return -EPIPE;
    }
    rc = modem_buffer_push(transfer->id, t->req->buf,  t->req->actual);
    if (rc < 0) {		   
          if(rc == -ENOMEM) {
			  spin_lock(&transfer->modem_block_lock);	       
			  transfer->sdio_block = 1;
			  spin_unlock(&transfer->modem_block_lock);	         
			  spin_lock(&transfer->usb_down_lock);		         
			  list_move_tail(&t->tlist, &transfer->repush2modem.transactions);
			  spin_unlock(&transfer->usb_down_lock);
			  transfer->repush2modem.ntrans++;
			  transfer->downstream.ntrans--;
			  queue_delayed_work(transfer->flow_wq, &transfer->delayed, time_delayed);
			  return -EPIPE;
	} else
		return -EPIPE;
    }

    req->buf = t->buffer;
    req->length = t->buffer_length;
    req->complete = downstream_complete;
    //if (rawbulk_usb_state_check())
        rc = usb_ep_queue(transfer->downstream.ep, req, GFP_ATOMIC);
    //else
    //    return;
    if (rc < 0) {
        terr(t, "fail to queue request, %d", rc);
        return rc;
    }

    t->state = DOWNSTREAM_STAT_DOWNLOADING;
    return 0;
}

static void downstream_complete(struct usb_ep *ep,
        struct usb_request *req) {
   
    //struct downstream_transaction *t = container_of(req->buf,
    //        struct downstream_transaction, buffer);
    
    //struct downstream_transaction *t = container_of(req->buf,
    //    struct downstream_transaction, buffer);
    struct downstream_transaction *t = req->context;
    struct rawbulk_transfer *transfer = t->transfer;
    C2K_DBG("%s\n", __func__);
    
    t->state = DOWNSTREAM_STAT_FREE;
    
    if (req->status < 0) {
        /*if (req->status == -ESHUTDOWN)
            return;
        else
            terr(t, "req status %d", req->status);*/
		C2K_WARN("req status %d\n", req->status);
        return;             
    }

#ifdef C2K_USB_UT
#define PRINT_LIMIT 8
	int i;
	static unsigned char last_c = 0;
	unsigned char c;
	char *ptr = (char *)t->req->buf;
	char verb[64];
	char *pbuf = (char *)verb;
	char compare_val;
	extern int ut_err;

	pbuf += sprintf(pbuf, "down len(%d), %d, ", t->req->actual, (int)sizeof(unsigned char));
	for(i = 0; i < t->req->actual; i++){
		c = *(ptr + i);
		if(last_c == 0xff)
			compare_val = 0;
		else
			compare_val = last_c + 1;
		if(c != compare_val || ut_err == 1){
			if( c != compare_val){
				C2K_NOTE("<%x,%x, %x>, sizeof(unsigned char):%d\n", c, last_c, compare_val, (int)sizeof(unsigned char));
			}
			ut_err = 1;
		}
		
		if(i < PRINT_LIMIT){
			pbuf += sprintf(pbuf, "%c ", c);
		}
		last_c = c;									// keep updating data
	}
	C2K_DBG("%s, last_c(%x)\n", verb, last_c);
	if(ut_err){
		C2K_NOTE("errrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n");
	}
#endif

    dump_data(transfer, "downstream", t->buffer, req->actual); 
    
    spin_lock(&transfer->modem_block_lock);   
    if(!!transfer->sdio_block) {
                spin_unlock(&transfer->modem_block_lock);
                
                spin_lock(&transfer->usb_down_lock);
                list_move_tail(&t->tlist, &transfer->repush2modem.transactions);
                spin_unlock(&transfer->usb_down_lock);
                transfer->repush2modem.ntrans++;
                transfer->downstream.ntrans--;
                return;
    } else{
        spin_unlock(&transfer->modem_block_lock); 
        start_downstream(t);
    }       
}

static void downstream_delayed_work(struct work_struct *work) {
    int rc = 0;
    unsigned long flags;

    struct downstream_transaction *downstream , *downstream_copy;
    struct usb_request *req;
    int time_delayed = msecs_to_jiffies(1);
    
    struct  rawbulk_transfer *transfer = container_of (work, struct
            rawbulk_transfer, delayed.work);
    C2K_NOTE("%s\n", __func__);
    
    spin_lock_irqsave(&transfer->usb_down_lock, flags);
    list_for_each_entry_safe(downstream, downstream_copy, &transfer->repush2modem.transactions, tlist) {
        spin_unlock_irqrestore(&transfer->usb_down_lock, flags);

        rc = modem_buffer_push(transfer->id, downstream->req->buf,  downstream->req->actual);
        if (rc < 0) {
             if ( rc != -ENOMEM){
                 terr(downstream, "port is not presence\n");
             }             
             if (!(transfer->control & STOP_DOWNSTREAM)) { 
                queue_delayed_work(transfer->flow_wq, &transfer->delayed, time_delayed);
             }
             return;
        }
        spin_lock_irqsave(&transfer->usb_down_lock, flags);
	    list_move_tail(&downstream->tlist, &transfer->downstream.transactions);
        spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
        downstream->stalled = 0;
        downstream->state = DOWNSTREAM_STAT_FREE;
        
        req = downstream->req;
        req->buf = downstream->buffer;
        req->length = downstream->buffer_length;
        req->complete = downstream_complete;
        //if (rawbulk_usb_state_check())
            rc = usb_ep_queue(transfer->downstream.ep, req, GFP_ATOMIC);
        //else
        //    return;
        if (rc < 0) {
            terr(downstream, "fail to queue request, %d", rc);
            downstream->stalled = 1;
            return;
        }
        downstream->state = DOWNSTREAM_STAT_DOWNLOADING;
        transfer->repush2modem.ntrans--;
        transfer->downstream.ntrans++;
        spin_lock_irqsave(&transfer->usb_down_lock, flags);
    }
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
      
    spin_lock_irqsave(&transfer->modem_block_lock, flags);       	
    transfer->sdio_block = 0;
    spin_unlock_irqrestore(&transfer->modem_block_lock, flags);
}

int rawbulk_start_transactions(int transfer_id, int nups, int ndowns, int upsz,
        int downsz) {
    int n;
    int rc, ret, up_cache_cnt;
    unsigned long flags;
    struct rawbulk_transfer *transfer;
    struct upstream_transaction *upstream; //upstream_copy;
    struct downstream_transaction *downstream, *downstream_copy;
    struct cache_buf  *c;
    C2K_NOTE("%s\n", __func__);

    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return -ENODEV;

    if (!rawbulk->cdev)
        return -ENODEV;
        
    if (!transfer->function)
        return -ENODEV;

    C2K_NOTE("start transactions on id %d, nups %d ndowns %d upsz %d downsz %d\n",
            transfer_id, nups, ndowns, upsz, downsz);
    
    /* stop host transfer 1stly */
    ret = sdio_rawbulk_intercept(transfer->id, 1);    
    if(ret < 0) {
        C2K_ERR("bypass sdio failed, channel id = %d\n", transfer->id);
        return ret;
    }    
    transfer->sdio_block = 0;
    
	spin_lock(&transfer->flow_lock);
	transfer->down_flow = 0;
	spin_unlock(&transfer->flow_lock);
	
    mutex_lock(&transfer->usb_up_mutex);
    for (n = 0; n < nups; n ++) {
        upstream = alloc_upstream_transaction(transfer, upsz);
        if (!upstream) {
            rc = -ENOMEM;
            mutex_unlock(&transfer->usb_up_mutex);
            C2K_NOTE("fail to allocate upstream transaction n %d", n);
            goto failto_alloc_upstream;
        }
    }
    mutex_unlock(&transfer->usb_up_mutex);
    
    
    mutex_lock(&transfer->modem_up_mutex);

	if(transfer_id == RAWBULK_TID_ETS || transfer_id == RAWBULK_TID_MODEM){
		up_cache_cnt = base_cache_cnt;
	}else{
		up_cache_cnt = 8*nups;
	}
    C2K_NOTE("t<%d>, up_cache_cnt<%d>\n", transfer_id, up_cache_cnt);
    for (n = 0; n < up_cache_cnt; n++) {        
        //c = kzalloc(sizeof *c + upsz * sizeof(unsigned char), GFP_KERNEL);
        c = kmalloc(sizeof(struct cache_buf), GFP_KERNEL);
        if (!c) {
            rc = -ENOMEM;
            mutex_unlock(&transfer->modem_up_mutex);
            C2K_NOTE("fail to allocate upstream sdio buf n %d", n);
			alloc_fail[transfer_id] = 1;
            goto failto_alloc_up_sdiobuf;
        }
        
        c->buffer = (char *)__get_free_page(GFP_KERNEL);
        //c->buffer = kmalloc(upsz, GFP_KERNEL);
        if (!c) {
            rc = -ENOMEM;
            kfree(c);
            mutex_unlock(&transfer->modem_up_mutex);
            C2K_NOTE("fail to allocate upstream sdio buf n %d", n);
			alloc_fail[transfer_id] = 1;
            goto failto_alloc_up_sdiobuf;
        }
        c->state = UPSTREAM_STAT_FREE;
        INIT_LIST_HEAD(&c->clist);
        list_add_tail(&c->clist, &transfer->cache_buf_lists.transactions);
        transfer->cache_buf_lists.ntrans++;
    }
	total_tran[transfer_id] = transfer->cache_buf_lists.ntrans;
    mutex_unlock(&transfer->modem_up_mutex);
    
    spin_lock_irqsave(&transfer->usb_down_lock, flags);
    for (n = 0; n < ndowns; n ++) {
        downstream = alloc_downstream_transaction(transfer, downsz);
        if (!downstream) {
            rc = -ENOMEM;
            spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
            C2K_NOTE("fail to allocate downstream transaction n %d", n);
            goto failto_alloc_downstream;
        }
    }

    transfer->control &= ~STOP_UPSTREAM;
    transfer->control &= ~STOP_DOWNSTREAM;
         
    list_for_each_entry_safe(downstream, downstream_copy, &transfer->downstream.transactions, tlist) {
        if (downstream->state == DOWNSTREAM_STAT_FREE && !downstream->stalled) {
            rc = queue_downstream(downstream);
            if (rc < 0) {
                spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
                C2K_NOTE("fail to start downstream %s rc %d\n", downstream->name, rc);
                goto failto_start_downstream;
            }
        }
    }
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
    return 0;

failto_start_downstream:
    spin_lock_irqsave(&transfer->usb_down_lock, flags); 
    list_for_each_entry(downstream, &transfer->downstream.transactions, tlist)
        stop_downstream(downstream);
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);                
failto_alloc_up_sdiobuf:
    free_upstream_sdio_buf(transfer);         
failto_alloc_downstream:  
    free_downstream_transaction(transfer);   
failto_alloc_upstream:
    free_upstream_transaction(transfer);    
    /* recover host transfer */
    sdio_rawbulk_intercept(transfer->id, 0);
    return rc;
}

EXPORT_SYMBOL_GPL(rawbulk_start_transactions);

void rawbulk_stop_transactions(int transfer_id) {
    unsigned long flags;
    struct rawbulk_transfer *transfer;
    struct upstream_transaction *upstream;
    struct downstream_transaction *downstream, *downstream_copy;
    struct list_head *p, *n;
    C2K_NOTE("t-%d\n", transfer_id);
    
    transfer = id_to_transfer(transfer_id);
    if (!transfer){
		C2K_NOTE("t-%d, NULL\n", transfer_id);
        return;
	}
    if(transfer->control){
		C2K_NOTE("t-%d,ctrl:%d\n", transfer_id, transfer->control);
		return;
	}
        
    spin_lock(&transfer->lock);
    transfer->control |= (STOP_UPSTREAM | STOP_DOWNSTREAM);
    spin_unlock(&transfer->lock);
    
    sdio_rawbulk_intercept(transfer->id, 0);
    
    cancel_delayed_work(&transfer->delayed);
    flush_workqueue(transfer->flow_wq);
    flush_workqueue(transfer->tx_wq);

    mutex_lock(&transfer->usb_up_mutex);
    list_for_each_entry(upstream, &transfer->upstream.transactions, tlist) {
		C2K_NOTE("t-%d,upstresm<%p>\n", transfer_id, upstream);
		stop_upstream(upstream);
    } 
    mutex_unlock(&transfer->usb_up_mutex);
  	/* this one got lock inside */ 
    free_upstream_transaction(transfer);

    free_upstream_sdio_buf(transfer);
         
    list_for_each_entry_safe(downstream, downstream_copy, &transfer->downstream.transactions, tlist) {
        stop_downstream(downstream);
    } 

    spin_lock_irqsave(&transfer->usb_down_lock, flags);
    list_for_each_safe(p, n, &transfer->repush2modem.transactions) {
        struct downstream_transaction *delayed_t = list_entry(p, struct
                downstream_transaction, tlist);                               
        list_move_tail(&delayed_t->tlist, &transfer->downstream.transactions);
    }
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
    
    spin_lock_irqsave(&transfer->modem_block_lock, flags);       	
    transfer->sdio_block = 0;
    spin_unlock_irqrestore(&transfer->modem_block_lock, flags);
    
    free_downstream_transaction(transfer);   
   
}

EXPORT_SYMBOL_GPL(rawbulk_stop_transactions);

static char *state2string(int state, int upstream) {
    if (upstream) {
        switch (state) {
            case UPSTREAM_STAT_FREE:
                return "FREE";
            case UPSTREAM_STAT_UPLOADING:
                return "UPLOADING";
            default:
                return "UNKNOW";
        }
    } else {
        switch (state) {
            case DOWNSTREAM_STAT_FREE:
                return "FREE";
            case DOWNSTREAM_STAT_DOWNLOADING:
                return "DOWNLOADING";
            default:
                return "UNKNOW";
        }
    }
}

int rawbulk_transfer_statistics(int transfer_id, char *buf) {
    char *pbuf = buf;
    struct rawbulk_transfer *transfer;
    struct upstream_transaction *upstream;
    struct downstream_transaction *downstream;
    struct cache_buf *c;
    unsigned long flags;
    C2K_NOTE("%s\n", __func__);
    
    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return sprintf(pbuf, "-ENODEV, id %d\n", transfer_id);

    pbuf += sprintf(pbuf, "rawbulk statistics:\n");
    if (rawbulk->cdev && rawbulk->cdev->config)
        pbuf += sprintf(pbuf, " gadget device: %s\n",
                rawbulk->cdev->config->label);
    else
        pbuf += sprintf(pbuf, " gadget device: -ENODEV\n");
    pbuf += sprintf(pbuf, " upstreams (total %d transactions)\n",
            transfer->upstream.ntrans);
    mutex_lock(&transfer->usb_up_mutex);        
    list_for_each_entry(upstream, &transfer->upstream.transactions, tlist) {
        pbuf += sprintf(pbuf, "  %s state: %s", upstream->name,
                state2string(upstream->state, 1));
        pbuf += sprintf(pbuf, ", maxbuf: %d bytes", upstream->buffer_length);
        if (upstream->stalled)
            pbuf += sprintf(pbuf, " (stalled!)");
        pbuf += sprintf(pbuf, "\n");
    }
    mutex_unlock(&transfer->usb_up_mutex);
    
    pbuf += sprintf(pbuf, " cache_buf_lists (total %d transactions)\n",
            transfer->cache_buf_lists.ntrans);
    mutex_lock(&transfer->modem_up_mutex);        
    list_for_each_entry(c, &transfer->cache_buf_lists.transactions, clist) {        
        pbuf += sprintf(pbuf, "  %s state:", state2string(c->state, 1));
        pbuf += sprintf(pbuf, ", maxbuf: %d bytes", c->length);
        pbuf += sprintf(pbuf, "\n");
    }
    mutex_unlock(&transfer->modem_up_mutex);
    
    pbuf += sprintf(pbuf, " downstreams (total %d transactions)\n",
            transfer->downstream.ntrans);
    spin_lock_irqsave(&transfer->usb_down_lock, flags);        
    list_for_each_entry(downstream, &transfer->downstream.transactions, tlist) {
        spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
        pbuf += sprintf(pbuf, "  %s state: %s", downstream->name,
                state2string(downstream->state, 0));
        pbuf += sprintf(pbuf, ", maxbuf: %d bytes", downstream->buffer_length);
        if (downstream->stalled)
            pbuf += sprintf(pbuf, " (stalled!)");
        pbuf += sprintf(pbuf, "\n");
        spin_lock_irqsave(&transfer->usb_down_lock, flags);
    }
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
    
    pbuf += sprintf(pbuf, " repush2modem (total %d transactions)\n",
            transfer->downstream.ntrans);
    spin_lock_irqsave(&transfer->usb_down_lock, flags);        
    list_for_each_entry(downstream, &transfer->repush2modem.transactions, tlist) {
        spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
        pbuf += sprintf(pbuf, "  %s state: %s", downstream->name,
                state2string(downstream->state, 0));
        pbuf += sprintf(pbuf, ", maxbuf: %d bytes", downstream->buffer_length);
        if (downstream->stalled)
            pbuf += sprintf(pbuf, " (stalled!)");
        pbuf += sprintf(pbuf, "\n");
        spin_lock_irqsave(&transfer->usb_down_lock, flags);
    }
    spin_unlock_irqrestore(&transfer->usb_down_lock, flags);
    
    return (int)(pbuf - buf);
}

EXPORT_SYMBOL_GPL(rawbulk_transfer_statistics);

int rawbulk_bind_function(int transfer_id, struct usb_function *function, struct
        usb_ep *bulk_out, struct usb_ep *bulk_in, 
        rawbulk_autoreconn_callback_t autoreconn_callback) {
            
    struct rawbulk_transfer *transfer;
    C2K_NOTE("%s\n", __func__);
    
    if (!function || !bulk_out || !bulk_in)
        return -EINVAL;

    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return -ENODEV;

    transfer->downstream.ep = bulk_out;
    transfer->upstream.ep = bulk_in;
    transfer->function = function;
    rawbulk->cdev = function->config->cdev;

    transfer->autoreconn = autoreconn_callback;
    return 0;
}

EXPORT_SYMBOL_GPL(rawbulk_bind_function);

void rawbulk_unbind_function(int transfer_id) {
    int n;
    int no_functions = 1;
    struct rawbulk_transfer *transfer;
    C2K_NOTE("%s\n", __func__);

    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return;

    rawbulk_stop_transactions(transfer_id);

	/* mark this for disable->work->stop_transaction not compelte */
    //transfer->downstream.ep = NULL;
    //transfer->upstream.ep = NULL;

    transfer->function = NULL;

    for (n = 0; n < _MAX_TID; n ++) {
        if (!!rawbulk->transfer[n].function)
            no_functions = 0;
    }

    if (no_functions)
        rawbulk->cdev = NULL;
}

EXPORT_SYMBOL_GPL(rawbulk_unbind_function);

int rawbulk_bind_sdio_channel(int transfer_id) {  
    struct rawbulk_transfer *transfer;
    struct rawbulk_function *fn;
    
    C2K_NOTE("%d\n", transfer_id);
    
    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return -ENODEV;
    fn = rawbulk_lookup_function(transfer_id);
    if(fn)
        fn->cbp_reset = 0;
    if (transfer->autoreconn)
        transfer->autoreconn(transfer->id);
    return 0;
}

EXPORT_SYMBOL_GPL(rawbulk_bind_sdio_channel);

void rawbulk_unbind_sdio_channel(int transfer_id) {
    struct rawbulk_transfer *transfer;
    struct rawbulk_function *fn;
    
    C2K_NOTE("%d\n", transfer_id);
    transfer = id_to_transfer(transfer_id);
    if (!transfer)
        return;
    rawbulk_stop_transactions(transfer_id);
    fn = rawbulk_lookup_function(transfer_id);
    if(fn) {
        fn->cbp_reset = 1;
        rawbulk_disable_function(fn);
    }
}

EXPORT_SYMBOL_GPL(rawbulk_unbind_sdio_channel);

static __init int rawbulk_init(void) {
    int n;
    char name[20];
    C2K_NOTE("%s\n", __func__);
	drop_check_timeout = jiffies; 
    
    rawbulk = kzalloc(sizeof *rawbulk, GFP_KERNEL);
    if (!rawbulk)
        return -ENOMEM;
   
    for (n = 0; n < _MAX_TID; n ++) {
        struct rawbulk_transfer *t = &rawbulk->transfer[n];

        t->id = n;
        INIT_LIST_HEAD(&t->upstream.transactions);
        INIT_LIST_HEAD(&t->downstream.transactions);
        INIT_LIST_HEAD(&t->repush2modem.transactions);      
        INIT_LIST_HEAD(&t->cache_buf_lists.transactions);      
        INIT_DELAYED_WORK(&t->delayed, downstream_delayed_work);
        memset(name, 0, 20);
        sprintf(name, "%s_flow_ctrl", transfer_name[n]);
        t->flow_wq = create_singlethread_workqueue(name);
        if (!t->flow_wq)
	        return -ENOMEM;
	    	      
	    INIT_WORK(&t->write_work, start_upstream);
	    memset(name, 0, 20);
	    sprintf(name, "%s_tx_wq", transfer_name[n]);
        t->tx_wq = create_singlethread_workqueue(name);
        if (!t->tx_wq)
	        return -ENOMEM;
	                   
        mutex_init(&t->modem_up_mutex);
        mutex_init(&t->usb_up_mutex);
        spin_lock_init(&t->lock);
        spin_lock_init(&t->usb_down_lock);
        spin_lock_init(&t->modem_block_lock);
        spin_lock_init(&t->flow_lock);
        
        t->control = STOP_UPSTREAM | STOP_DOWNSTREAM;
    }

    return 0;
}

module_init (rawbulk_init);

static __exit void rawbulk_exit(void) {
    int n;
    struct rawbulk_transfer *t;
    for (n = 0; n < _MAX_TID; n ++) {
        t = &rawbulk->transfer[n];
        rawbulk_stop_transactions(n);
        destroy_workqueue(t->flow_wq);
        destroy_workqueue(t->tx_wq);
    }
    kfree(rawbulk);
}

module_exit (rawbulk_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");