-
Notifications
You must be signed in to change notification settings - Fork 22
/
usb.c
2209 lines (1720 loc) · 56.9 KB
/
usb.c
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
#include <linux/etherdevice.h>
#include <drv_types.h>
#include <usb_ops.h>
#include <rtw_ap.h>
#include <rtl8812a_recv.h>
#include <../rtl8821au/trx.h>
#include <../rtl8821au/fw.h>
#include "debug.h"
#undef DBG_871X
static inline void DBG_871X(const char *fmt, ...)
{
}
#undef DBG_8192C
static inline void DBG_8192C(const char *fmt, ...)
{
}
/*
* Increase and check if the continual_urb_error of this @param dvobjprive is larger than MAX_CONTINUAL_URB_ERR
* @return true:
* @return false:
*/
/* ULLI : currently wrong functions for this prototypes, we change this (maybe) later */
static uint32_t _rtl_usb_receive(struct rtl_priv *rtlpriv, uint32_t cnt, uint8_t *rmem);
static inline int rtw_inc_and_chk_continual_urb_error(struct rtl_usb *dvobj)
{
int ret = false;
int value;
if( (value=atomic_inc_return(&dvobj->continual_urb_error)) > MAX_CONTINUAL_URB_ERR) {
DBG_871X("[dvobj:%p][ERROR] continual_urb_error:%d > %d\n", dvobj, value, MAX_CONTINUAL_URB_ERR);
ret = true;
} else {
//DBG_871X("[dvobj:%p] continual_urb_error:%d\n", dvobj, value);
}
return ret;
}
int rtw_resume_process(struct rtl_priv *rtlpriv);
static int usbctrl_vendorreq(struct rtl_priv *rtlpriv, uint8_t request, u16 value, u16 index, void *pdata, u16 len, uint8_t requesttype)
{
struct rtl_usb *rtlusb = rtl_usbdev(rtlpriv);
struct usb_device *udev = rtlusb->udev;
int _unused;
unsigned int pipe;
int status = 0;
u32 tmp_buflen=0;
uint8_t reqtype;
uint8_t *pIo_buf;
int vendorreq_times = 0;
uint8_t tmp_buf[MAX_USB_IO_CTL_SIZE];
/* DBG_871X("%s %s:%d\n",__FUNCTION__, current->comm, current->pid); */
if((rtlpriv->bSurpriseRemoved) ||(rtlpriv->pwrctrlpriv.pnp_bstop_trx)){
status = -EPERM;
goto exit;
}
if(len>MAX_VENDOR_REQ_CMD_SIZE){
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "[%s] Buffer len error ,vendor request failed\n", __FUNCTION__ );
status = -EINVAL;
goto exit;
}
#ifdef CONFIG_USB_VENDOR_REQ_MUTEX
_unused = mutex_lock_interruptible(&rtlusb->usb_vendor_req_mutex);
#endif
/* Acquire IO memory for vendorreq */
#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
pIo_buf = rtlusb->usb_vendor_req_buf;
#else
tmp_buflen = MAX_USB_IO_CTL_SIZE;
/*
* Added by Albert 2010/02/09
* For mstar platform, mstar suggests the address for USB IO should be 16 bytes alignment.
* Trying to fix it here.
*/
pIo_buf = (tmp_buf==NULL)?NULL:tmp_buf + ALIGNMENT_UNIT -((__kernel_size_t)(tmp_buf) & 0x0f );
#endif
if ( pIo_buf== NULL) {
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "[%s] pIo_buf == NULL \n", __FUNCTION__ );
status = -ENOMEM;
goto release_mutex;
}
while (++vendorreq_times<= MAX_USBCTRL_VENDORREQ_TIMES) {
memset(pIo_buf, 0, len);
if (requesttype == 0x01) {
pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
reqtype = REALTEK_USB_VENQT_READ;
} else {
pipe = usb_sndctrlpipe(udev, 0); /* write_out */
reqtype = REALTEK_USB_VENQT_WRITE;
memcpy( pIo_buf, pdata, len);
}
status = usb_control_msg(udev, pipe, request, reqtype, value, index, pIo_buf, len, RTW_USB_CONTROL_MSG_TIMEOUT);
if (status == len) { // Success this control transfer. */
rtw_reset_continual_urb_error(rtlusb);
if (requesttype == 0x01) {
/* For Control read transfer, we have to copy the read data from pIo_buf to pdata. */
memcpy(pdata, pIo_buf, len);
}
} else {
/* error cases */
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "reg 0x%x, usb %s %u fail, status:%d value=0x%x, vendorreq_times:%d\n"
, value,(requesttype == 0x01)?"read":"write" , len, status, *(u32*)pdata, vendorreq_times);
if (status < 0) {
if(status == (-ESHUTDOWN) || status == -ENODEV) {
rtlpriv->bSurpriseRemoved = true;
} else {
;
}
} else {
/* status != len && status >= 0 */
if(status > 0) {
if ( requesttype == 0x01 ) {
/* For Control read transfer, we have to copy the read data from pIo_buf to pdata. */
memcpy( pdata, pIo_buf, len );
}
}
}
if(rtw_inc_and_chk_continual_urb_error(rtlusb) == true ){
rtlpriv->bSurpriseRemoved = true;
break;
}
}
/* firmware download is checksumed, don't retry */
if( (value >= FW_8821AU_START_ADDRESS ) || status == len )
break;
}
/* release IO memory used by vendorreq */
release_mutex:
#ifdef CONFIG_USB_VENDOR_REQ_MUTEX
mutex_unlock(&rtlusb->usb_vendor_req_mutex);
#endif
exit:
return status;
}
static uint8_t usb_read8(struct rtl_priv *rtlpriv, uint32_t addr)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
uint8_t data = 0;
request = 0x05;
requesttype = 0x01; /* read_in */
index = 0; /* n/a */
wvalue = (u16) (addr&0x0000ffff);
len = 1;
usbctrl_vendorreq(rtlpriv, request, wvalue, index, &data, len, requesttype);
return data;
}
static u16 usb_read16(struct rtl_priv *rtlpriv, uint32_t addr)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
u16 data = 0;
request = 0x05;
requesttype = 0x01; /* read_in */
index = 0; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
len = 2;
usbctrl_vendorreq(rtlpriv, request, wvalue, index, &data, len, requesttype);
return data;
}
static uint32_t usb_read32(struct rtl_priv *rtlpriv, uint32_t addr)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
uint32_t data = 0;
request = 0x05;
requesttype = 0x01; /* read_in */
index = 0; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
len = 4;
usbctrl_vendorreq(rtlpriv, request, wvalue, index, &data, len, requesttype);
return data;
}
static void usb_write8(struct rtl_priv *rtlpriv, uint32_t addr, uint8_t val)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
uint8_t data;
request = 0x05;
requesttype = 0x00; /* write_out */
index = 0; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
len = 1;
data = val;
usbctrl_vendorreq(rtlpriv, request, wvalue, index, &data, len, requesttype);
}
static void usb_write16(struct rtl_priv *rtlpriv, uint32_t addr, u16 val)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
u16 data;
request = 0x05;
requesttype = 0x00; /* write_out */
index = 0; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
len = 2;
data = val;
usbctrl_vendorreq(rtlpriv, request, wvalue, index, &data, len, requesttype);
}
static void usb_write32(struct rtl_priv *rtlpriv, uint32_t addr, uint32_t val)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
uint32_t data;
request = 0x05;
requesttype = 0x00; /* write_out */
index = 0; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
len = 4;
data = val;
usbctrl_vendorreq(rtlpriv, request, wvalue, index, &data, len, requesttype);
}
static void usb_writeN(struct rtl_priv *rtlpriv, uint32_t addr, void *pdata, u16 length)
{
uint8_t request;
uint8_t requesttype;
u16 wvalue;
u16 index;
u16 len;
uint8_t buf[VENDOR_CMD_MAX_DATA_LEN] = {0};
request = 0x05;
requesttype = 0x00; /* write_out */
index = 0; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
len = length;
memcpy(buf, pdata, len);
usbctrl_vendorreq(rtlpriv, request, wvalue, index, buf, len, requesttype);
}
static void _rtl_usb_io_handler_init(struct device *dev,
struct rtl_priv *rtlpriv)
{
rtlpriv->io.dev = dev;
mutex_init(&rtlpriv->io.bb_mutex);
rtlpriv->io.write8_async = usb_write8;
rtlpriv->io.write16_async = usb_write16;
rtlpriv->io.write32_async = usb_write32;
rtlpriv->io.read8_sync = usb_read8;
rtlpriv->io.read16_sync = usb_read16;
rtlpriv->io.read32_sync = usb_read32;
rtlpriv->io.writeN_sync = usb_writeN;
}
static void _rtl_usb_io_handler_release(struct rtl_priv *rtlpriv)
{
mutex_destroy(&rtlpriv->io.bb_mutex);
}
static void _rtl_tx_complete(struct urb *purb)
{
unsigned long flags;
int i;
struct xmit_buf *pxmitbuf = (struct xmit_buf *)purb->context;
/* struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitbuf->priv_data; */
/* struct rtl_priv *rtlpriv = pxmitframe->rtlpriv; */
struct rtl_priv *rtlpriv = pxmitbuf->rtlpriv;
struct xmit_priv *pxmitpriv = &rtlpriv->xmitpriv;
/* struct pkt_attrib *pattrib = &pxmitframe->attrib; */
switch(pxmitbuf->flags) {
case RTL_TXQ_VO:
break;
case RTL_TXQ_VI:
break;
case RTL_TXQ_BE:
break;
case RTL_TXQ_BK:
break;
case RTL_TXQ_HI:
#ifdef CONFIG_AP_MODE
rtw_chk_hi_queue_cmd(rtlpriv);
#endif
break;
default:
break;
}
/* rtw_free_xmitframe(pxmitpriv, pxmitframe); */
if (rtlpriv->bSurpriseRemoved || rtlpriv->bDriverStopped ||rtlpriv->bWritePortCancel) {
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "%s(): TX Warning! bDriverStopped(%d) OR bSurpriseRemoved(%d) bWritePortCancel(%d) pxmitbuf->buf_tag(%x) \n",
__FUNCTION__,rtlpriv->bDriverStopped, rtlpriv->bSurpriseRemoved,rtlpriv->bReadPortCancel,pxmitbuf->buf_tag);
goto check_completion;
}
if (purb->status==0) {
} else {
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "###=> urb_write_port_complete status(%d)\n",purb->status);
if((purb->status==-EPIPE)||(purb->status==-EPROTO)) {
/*
* usb_clear_halt(pusbdev, purb->pipe);
* msleep(10);
*/
} else if (purb->status == -EINPROGRESS) {
goto check_completion;
} else if (purb->status == -ENOENT) {
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "%s: -ENOENT\n", __func__);
goto check_completion;
} else if (purb->status == -ECONNRESET) {
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "%s: -ECONNRESET\n", __func__);
goto check_completion;
} else if (purb->status == -ESHUTDOWN) {
rtlpriv->bDriverStopped=true;
goto check_completion;
} else {
rtlpriv->bSurpriseRemoved=true;
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, "bSurpriseRemoved=true\n");
/* rtl8192cu_trigger_gpio_0(rtlpriv); */
goto check_completion;
}
}
check_completion:
spin_lock_irqsave(&pxmitpriv->lock_sctx, flags);
rtw_sctx_done_err(&pxmitbuf->sctx,
purb->status ? RTW_SCTX_DONE_WRITE_PORT_ERR : RTW_SCTX_DONE_SUCCESS);
spin_unlock_irqrestore(&pxmitpriv->lock_sctx, flags);
rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
/* if(rtw_txframes_pending(rtlpriv)) */
{
tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
}
}
/* ULLI : _rtlw* prefix because of rtlwifi namespace issues */
u32 _rtlw_usb_transmit(struct rtl_priv *rtlpriv, u32 queue_idx, u32 cnt, struct xmit_buf *pxmitbuf)
{
u32 ep_num;
unsigned long flags;
unsigned int pipe;
int status;
u32 ret = _FAIL, bwritezero = false;
struct urb *purb = NULL;
struct rtl_usb *rtlusb = rtl_usbdev(rtlpriv);
struct xmit_priv *pxmitpriv = &rtlpriv->xmitpriv;
struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitbuf->priv_data;
struct usb_device *pusbd = rtlusb->udev;
struct tx_pkt_attrib *pattrib = &pxmitframe->tx_attrib;
if ((rtlpriv->bDriverStopped) || (rtlpriv->bSurpriseRemoved) ||(rtlpriv->pwrctrlpriv.pnp_bstop_trx)) {
#ifdef DBG_TX
DBG_871X(" DBG_TX %s:%d bDriverStopped%d, bSurpriseRemoved:%d, pnp_bstop_trx:%d\n",__FUNCTION__, __LINE__
,rtlpriv->bDriverStopped, rtlpriv->bSurpriseRemoved, rtlpriv->pwrctrlpriv.pnp_bstop_trx );
#endif
rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_TX_DENY);
goto exit;
}
spin_lock_irqsave(&pxmitpriv->lock, flags);
switch(queue_idx) {
case RTL_TXQ_VO:
pxmitbuf->flags = RTL_TXQ_VO;
break;
case RTL_TXQ_VI:
pxmitbuf->flags = RTL_TXQ_VI;
break;
case RTL_TXQ_BE:
pxmitbuf->flags = RTL_TXQ_BE;
break;
case RTL_TXQ_BK:
pxmitbuf->flags = RTL_TXQ_BK;
break;
case RTL_TXQ_HI:
pxmitbuf->flags = RTL_TXQ_HI;
break;
default:
pxmitbuf->flags = RTL_TXQ_MGT;
break;
}
spin_unlock_irqrestore(&pxmitpriv->lock, flags);
purb = pxmitbuf->pxmit_urb[0];
/* translate DMA FIFO addr to pipehandle */
ep_num = rtlusb->ep_map.ep_mapping[queue_idx];
pipe = usb_sndbulkpipe(pusbd, ep_num);
#ifdef CONFIG_REDUCE_USB_TX_INT
if ((pxmitpriv->free_xmitbuf_cnt%NR_XMITBUFF == 0) ||
(pxmitbuf->buf_tag > XMITBUF_DATA)) {
purb->transfer_flags &= (~URB_NO_INTERRUPT);
} else {
purb->transfer_flags |= URB_NO_INTERRUPT;
/* DBG_8192C("URB_NO_INTERRUPT "); */
}
#endif
usb_fill_bulk_urb(purb, pusbd, pipe,
pxmitframe->buf_addr, /* = pxmitbuf->pbuf */
cnt,
_rtl_tx_complete,
pxmitbuf); /* context is pxmitbuf */
#if 0
if (bwritezero) {
purb->transfer_flags |= URB_ZERO_PACKET;
}
#endif
status = usb_submit_urb(purb, GFP_ATOMIC);
if (!status) {
} else {
rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_WRITE_PORT_ERR);
RT_TRACE(rtlpriv, COMP_USB, DBG_LOUD, " _rtlw_usb_transmit, status=%d\n", status);
switch (status) {
case -ENODEV:
rtlpriv->bDriverStopped=true;
break;
default:
break;
}
goto exit;
}
ret= _SUCCESS;
/*
* Commented by Albert 2009/10/13
* We add the URB_ZERO_PACKET flag to urb so that the host will send the zero packet automatically.
*/
/*
if(bwritezero == true)
{
usb_bulkout_zero(pintfhdl, addr);
}
*/
exit:
if (ret != _SUCCESS)
rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
return ret;
}
void usb_write_port_cancel(struct rtl_priv *rtlpriv)
{
int i, j;
struct xmit_buf *pxmitbuf = (struct xmit_buf *)rtlpriv->xmitpriv.pxmitbuf;
rtlpriv->bWritePortCancel = true;
for (i=0; i<NR_XMITBUFF; i++) {
for (j=0; j<8; j++) {
if (pxmitbuf->pxmit_urb[j]) {
usb_kill_urb(pxmitbuf->pxmit_urb[j]);
}
}
pxmitbuf++;
}
pxmitbuf = (struct xmit_buf*)rtlpriv->xmitpriv.pxmit_extbuf;
for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
for (j=0; j<8; j++) {
if(pxmitbuf->pxmit_urb[j]) {
usb_kill_urb(pxmitbuf->pxmit_urb[j]);
}
}
pxmitbuf++;
}
}
static void _rtl_rx_complete(struct urb *purb)
{
uint isevt, *pbuf;
struct recv_buf *precvbuf = (struct recv_buf *) purb->context;
struct rtl_priv *rtlpriv = (struct rtl_priv *) precvbuf->rtlpriv;
struct recv_priv *precvpriv = &rtlpriv->recvpriv;
/*
* spin_lock_irqsave(&precvpriv->lock, &irqL);
* precvbuf->irp_pending=false;
* precvpriv->rx_pending_cnt --;
* spin_unlock_irqrestore(&precvpriv->lock, &irqL);
*/
precvpriv->rx_pending_cnt--;
/*
* if(precvpriv->rx_pending_cnt== 0) {
* RT_TRACE(_module_hci_ops_os_c_,_drv_err_,("usb_read_port_complete: rx_pending_cnt== 0, set allrxreturnevt!\n"));
* up(&precvpriv->allrxreturnevt);
* }
*/
if (rtlpriv->bSurpriseRemoved || rtlpriv->bDriverStopped
|| rtlpriv->bReadPortCancel) {
precvbuf->reuse = true;
DBG_8192C("%s() RX Warning! bDriverStopped(%d) OR bSurpriseRemoved(%d) bReadPortCancel(%d)\n",
__FUNCTION__, rtlpriv->bDriverStopped, rtlpriv->bSurpriseRemoved, rtlpriv->bReadPortCancel);
goto exit;
}
if (purb->status == 0) {
/* SUCCESS */
if ((purb->actual_length > MAX_RECVBUF_SZ)
|| (purb->actual_length < RXDESC_SIZE)) {
precvbuf->reuse = true;
_rtl_usb_receive(rtlpriv, 0, (unsigned char *)precvbuf);
DBG_8192C("%s()-%d: RX Warning!\n", __FUNCTION__, __LINE__);
} else {
rtw_reset_continual_urb_error(rtl_usbdev(rtlpriv));
skb_put(precvbuf->skb, purb->actual_length);
skb_queue_tail(&precvpriv->rx_skb_queue, precvbuf->skb);
if (skb_queue_len(&precvpriv->rx_skb_queue) <= 1)
tasklet_schedule(&precvpriv->recv_tasklet);
precvbuf->skb = NULL;
precvbuf->reuse = false;
_rtl_usb_receive(rtlpriv, 0, (unsigned char *)precvbuf);
}
} else {
DBG_8192C("###=> usb_read_port_complete => urb status(%d)\n", purb->status);
if (rtw_inc_and_chk_continual_urb_error(rtl_usbdev(rtlpriv)) == true) {
rtlpriv->bSurpriseRemoved = true;
}
switch (purb->status) {
case -EINVAL:
case -EPIPE:
case -ENODEV:
case -ESHUTDOWN:
/* rtlpriv->bSurpriseRemoved=true; */
case -ENOENT:
rtlpriv->bDriverStopped = true;
break;
case -EPROTO:
case -EILSEQ:
case -ETIME:
case -ECOMM:
case -EOVERFLOW:
precvbuf->reuse = true;
_rtl_usb_receive(rtlpriv, 0, (unsigned char *)precvbuf);
break;
case -EINPROGRESS:
DBG_8192C("ERROR: URB IS IN PROGRESS!/n");
break;
default:
break;
}
}
exit:
;
}
void rtw_os_read_port(struct rtl_priv *rtlpriv, struct recv_buf *precvbuf)
{
struct recv_priv *precvpriv = &rtlpriv->recvpriv;
precvbuf->ref_cnt--;
/* free skb in recv_buf */
dev_kfree_skb_any(precvbuf->skb);
precvbuf->skb = NULL;
precvbuf->reuse = false;
if (precvbuf->irp_pending == false) {
_rtl_usb_receive(rtlpriv, 0, (unsigned char *)precvbuf);
}
}
static uint32_t _rtl_usb_receive (struct rtl_priv *rtlpriv, uint32_t cnt, uint8_t *rmem)
{
int err;
unsigned int pipe;
__kernel_size_t tmpaddr = 0;
__kernel_size_t alignment = 0;
uint32_t ret = _SUCCESS;
struct urb *purb = NULL;
struct recv_buf *precvbuf = (struct recv_buf *) rmem;
struct rtl_usb *rtlusb = rtl_usbdev(rtlpriv);
struct recv_priv *precvpriv = &rtlpriv->recvpriv;
struct usb_device *pusbd = rtlusb->udev;
uint32_t addr = RECV_BULK_IN_ADDR;
if (rtlpriv->bDriverStopped || rtlpriv->bSurpriseRemoved
|| rtlpriv->pwrctrlpriv.pnp_bstop_trx) {
return _FAIL;
}
if ((precvbuf->reuse == false) || (precvbuf->skb == NULL)) {
precvbuf->skb = skb_dequeue(&precvpriv->free_recv_skb_queue);
if (precvbuf->skb != NULL) {
precvbuf->reuse = true;
}
}
if (precvbuf != NULL) {
precvbuf->len = 0;
precvbuf->ref_cnt = 0;
/* re-assign for linux based on skb */
if ((precvbuf->reuse == false) || (precvbuf->skb == NULL)) {
/* precvbuf->pskb = alloc_skb(MAX_RECVBUF_SZ, GFP_ATOMIC);//don't use this after v2.6.25 */
precvbuf->skb = netdev_alloc_skb(rtlpriv->ndev, MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
if (precvbuf->skb == NULL) {
DBG_8192C("#### usb_read_port() alloc_skb fail!#####\n");
return _FAIL;
}
tmpaddr = (__kernel_size_t) precvbuf->skb->data;
alignment = tmpaddr & (RECVBUFF_ALIGN_SZ-1);
skb_reserve(precvbuf->skb, (RECVBUFF_ALIGN_SZ - alignment));
} else {
/* reuse skb */
precvbuf->reuse = false;
}
/*
* spin_lock_irqsave(&precvpriv->lock, &irqL);
* precvpriv->rx_pending_cnt++;
* precvbuf->irp_pending = true;
* spin_unlock_irqrestore(&precvpriv->lock, &irqL);
*/
precvpriv->rx_pending_cnt++;
purb = precvbuf->purb;
/* translate DMA FIFO addr to pipehandle */
pipe = usb_rcvbulkpipe(pusbd, rtlusb->RtInPipe[0]);
usb_fill_bulk_urb(purb, pusbd, pipe,
precvbuf->skb->data,
MAX_RECVBUF_SZ,
_rtl_rx_complete,
precvbuf); /* context is precvbuf */
err = usb_submit_urb(purb, GFP_ATOMIC);
if ((err) && (err != (-EPERM))) {
DBG_8192C("cannot submit rx in-token(err = 0x%08x),urb_status = %d\n", err, purb->status);
ret = _FAIL;
}
} else {
ret = _FAIL;
}
return ret;
}
static inline int RT_usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
}
static inline int RT_usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
}
static inline int RT_usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT);
}
static inline int RT_usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK);
}
static inline int RT_usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd)
{
return (RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_in(epd));
}
static inline int RT_usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd)
{
return (RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_out(epd));
}
static inline int RT_usb_endpoint_num(const struct usb_endpoint_descriptor *epd)
{
return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
}
static uint8_t rtw_init_intf_priv(struct rtl_usb *dvobj)
{
uint8_t rst = _SUCCESS;
#ifdef CONFIG_USB_VENDOR_REQ_MUTEX
mutex_init(&dvobj->usb_vendor_req_mutex);
#endif
#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
dvobj->usb_alloc_vendor_req_buf = rtw_zmalloc(MAX_USB_IO_CTL_SIZE);
if (dvobj->usb_alloc_vendor_req_buf == NULL) {
DBG_871X("alloc usb_vendor_req_buf failed... /n");
rst = _FAIL;
goto exit;
}
dvobj->usb_vendor_req_buf =
(uint8_t *)N_BYTE_ALIGMENT((__kernel_size_t)(dvobj->usb_alloc_vendor_req_buf ), ALIGNMENT_UNIT);
exit:
#endif
return rst;
}
static uint8_t rtw_deinit_intf_priv(struct rtl_usb *dvobj)
{
uint8_t rst = _SUCCESS;
#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
if(dvobj->usb_vendor_req_buf)
rtw_mfree(dvobj->usb_alloc_vendor_req_buf);
#endif
#ifdef CONFIG_USB_VENDOR_REQ_MUTEX
mutex_destroy(&dvobj->usb_vendor_req_mutex);
#endif
return rst;
}
static struct rtl_usb *usb_dvobj_init(struct usb_interface *usb_intf, struct rtl_usb *rtlusb)
{
int i;
uint8_t val8;
int status = _FAIL;
struct usb_device_descriptor *pdev_desc;
struct usb_host_config *phost_conf;
struct usb_config_descriptor *pconf_desc;
struct usb_host_interface *phost_iface;
struct usb_interface_descriptor *piface_desc;
struct usb_host_endpoint *phost_endp;
struct usb_endpoint_descriptor *pendp_desc;
struct usb_device *pusbd;
mutex_init(&rtlusb->hw_init_mutex);
mutex_init(&rtlusb->h2c_fwcmd_mutex);
mutex_init(&rtlusb->setch_mutex);
mutex_init(&rtlusb->setbw_mutex);
spin_lock_init(&rtlusb->lock);
rtlusb->macid[1] = true; /* macid=1 for bc/mc stainfo */
rtlusb->intf = usb_intf ;
pusbd = rtlusb->udev = interface_to_usbdev(usb_intf);
usb_set_intfdata(usb_intf, rtlusb);
rtlusb->RtNumInPipes = 0;
rtlusb->RtNumOutPipes = 0;
/*
* rtlpriv->EepromAddressSize = 6;
* rtlusb->nr_endpoint = 6;
*/
pdev_desc = &pusbd->descriptor;
phost_conf = pusbd->actconfig;
pconf_desc = &phost_conf->desc;
/*
* DBG_871X("\n****** num of altsetting = (%d) ******\n", pusb_interface->num_altsetting);
*/
phost_iface = &usb_intf->altsetting[0];
piface_desc = &phost_iface->desc;
rtlusb->nr_endpoint = piface_desc->bNumEndpoints;
/* DBG_871X("\ndump usb_endpoint_descriptor:\n"); */
for (i = 0; i < rtlusb->nr_endpoint; i++) {
phost_endp = phost_iface->endpoint + i;
if (phost_endp) {
pendp_desc = &phost_endp->desc;
DBG_871X("\nusb_endpoint_descriptor(%d):\n", i);
DBG_871X("bLength=%x\n",pendp_desc->bLength);
DBG_871X("bDescriptorType=%x\n",pendp_desc->bDescriptorType);
DBG_871X("bEndpointAddress=%x\n",pendp_desc->bEndpointAddress);
/* DBG_871X("bmAttributes=%x\n",pendp_desc->bmAttributes); */
DBG_871X("wMaxPacketSize=%d\n",le16_to_cpu(pendp_desc->wMaxPacketSize));
DBG_871X("bInterval=%x\n",pendp_desc->bInterval);
/* DBG_871X("bRefresh=%x\n",pendp_desc->bRefresh); */
/* DBG_871X("bSynchAddress=%x\n",pendp_desc->bSynchAddress); */
if (RT_usb_endpoint_is_bulk_in(pendp_desc)) {
DBG_871X("RT_usb_endpoint_is_bulk_in = %x\n", RT_usb_endpoint_num(pendp_desc));
rtlusb->RtInPipe[rtlusb->RtNumInPipes] = RT_usb_endpoint_num(pendp_desc);
rtlusb->RtNumInPipes++;
} else if (RT_usb_endpoint_is_bulk_out(pendp_desc)) {
DBG_871X("RT_usb_endpoint_is_bulk_out = %x\n", RT_usb_endpoint_num(pendp_desc));
rtlusb->RtNumOutPipes++;
}
rtlusb->ep_num[i] = RT_usb_endpoint_num(pendp_desc);
}
}
DBG_871X("nr_endpoint=%d, in_num=%d, out_num=%d\n\n", rtlusb->nr_endpoint, rtlusb->RtNumInPipes, rtlusb->RtNumOutPipes);
switch(pusbd->speed) {
case USB_SPEED_LOW:
DBG_871X("USB_SPEED_LOW\n");
rtlusb->usb_speed = RTW_USB_SPEED_1_1;
break;
case USB_SPEED_FULL:
DBG_871X("USB_SPEED_FULL\n");
rtlusb->usb_speed = RTW_USB_SPEED_1_1;
break;
case USB_SPEED_HIGH:
DBG_871X("USB_SPEED_HIGH\n");
rtlusb->usb_speed = RTW_USB_SPEED_2;
break;
case USB_SPEED_SUPER:
DBG_871X("USB_SPEED_SUPER\n");
rtlusb->usb_speed = RTW_USB_SPEED_3;
break;
default:
DBG_871X("USB_SPEED_UNKNOWN(%x)\n",pusbd->speed);
rtlusb->usb_speed = RTW_USB_SPEED_UNKNOWN;
break;
}
if (rtlusb->usb_speed == RTW_USB_SPEED_UNKNOWN) {
DBG_871X("UNKNOWN USB SPEED MODE, ERROR !!!\n");
goto free_dvobj;
}
if (rtw_init_intf_priv(rtlusb) == _FAIL) {
goto free_dvobj;
}
/* .3 misc */
rtw_reset_continual_urb_error(rtlusb);
usb_get_dev(pusbd);
status = _SUCCESS;
free_dvobj:
if (status != _SUCCESS && rtlusb) {
usb_set_intfdata(usb_intf, NULL);
mutex_destroy(&rtlusb->hw_init_mutex);
mutex_destroy(&rtlusb->h2c_fwcmd_mutex);
mutex_destroy(&rtlusb->setch_mutex);
mutex_destroy(&rtlusb->setbw_mutex);
rtlusb = NULL;
}
exit:
return rtlusb;
}
void usb_dvobj_deinit(struct usb_interface *usb_intf)
{
struct rtl_usb *rtlusb = usb_get_intfdata(usb_intf);
usb_set_intfdata(usb_intf, NULL);
if (rtlusb) {
if (interface_to_usbdev(usb_intf)->state != USB_STATE_NOTATTACHED) {
/*
* If we didn't unplug usb dongle and remove/insert modlue, driver fails on sitesurvey for the first time when device is up .
* Reset usb port for sitesurvey fail issue. 2009.8.13, by Thomas
*/
DBG_871X("usb attached..., try to reset usb device\n");
usb_reset_device(interface_to_usbdev(usb_intf));
}
rtw_deinit_intf_priv(rtlusb);
mutex_destroy(&rtlusb->hw_init_mutex);
mutex_destroy(&rtlusb->h2c_fwcmd_mutex);
mutex_destroy(&rtlusb->setch_mutex);
mutex_destroy(&rtlusb->setbw_mutex);
}
/* DBG_871X("%s %d\n", __func__, atomic_read(&usb_intf->dev.kobj.kref.refcount)); */
usb_put_dev(interface_to_usbdev(usb_intf));
}
void usb_intf_start(struct rtl_priv *rtlpriv)
{
uint8_t i;
struct recv_buf *precvbuf;