]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/hal/usb_ops_linux.c
Merge tag 'llvmlinux-for-v3.16' of git://git.linuxfoundation.org/llvmlinux/kernel
[karo-tx-linux.git] / drivers / staging / rtl8188eu / hal / usb_ops_linux.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _HCI_OPS_OS_C_
21
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <osdep_intf.h>
25 #include <usb_ops.h>
26 #include <recv_osdep.h>
27 #include <rtl8188e_hal.h>
28
29 static int usbctrl_vendorreq(struct intf_hdl *pintfhdl, u8 request, u16 value, u16 index, void *pdata, u16 len, u8 requesttype)
30 {
31         struct adapter  *adapt = pintfhdl->padapter;
32         struct dvobj_priv  *dvobjpriv = adapter_to_dvobj(adapt);
33         struct usb_device *udev = dvobjpriv->pusbdev;
34         unsigned int pipe;
35         int status = 0;
36         u8 reqtype;
37         u8 *pIo_buf;
38         int vendorreq_times = 0;
39
40         if ((adapt->bSurpriseRemoved) || (adapt->pwrctrlpriv.pnp_bstop_trx)) {
41                 RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usbctrl_vendorreq:(adapt->bSurpriseRemoved ||adapter->pwrctrlpriv.pnp_bstop_trx)!!!\n"));
42                 status = -EPERM;
43                 goto exit;
44         }
45
46         if (len > MAX_VENDOR_REQ_CMD_SIZE) {
47                 DBG_88E("[%s] Buffer len error ,vendor request failed\n", __func__);
48                 status = -EINVAL;
49                 goto exit;
50         }
51
52         _enter_critical_mutex(&dvobjpriv->usb_vendor_req_mutex, NULL);
53
54         /*  Acquire IO memory for vendorreq */
55         pIo_buf = dvobjpriv->usb_vendor_req_buf;
56
57         if (pIo_buf == NULL) {
58                 DBG_88E("[%s] pIo_buf == NULL\n", __func__);
59                 status = -ENOMEM;
60                 goto release_mutex;
61         }
62
63         while (++vendorreq_times <= MAX_USBCTRL_VENDORREQ_TIMES) {
64                 _rtw_memset(pIo_buf, 0, len);
65
66                 if (requesttype == 0x01) {
67                         pipe = usb_rcvctrlpipe(udev, 0);/* read_in */
68                         reqtype =  REALTEK_USB_VENQT_READ;
69                 } else {
70                         pipe = usb_sndctrlpipe(udev, 0);/* write_out */
71                         reqtype =  REALTEK_USB_VENQT_WRITE;
72                         memcpy(pIo_buf, pdata, len);
73                 }
74
75                 status = rtw_usb_control_msg(udev, pipe, request, reqtype, value, index, pIo_buf, len, RTW_USB_CONTROL_MSG_TIMEOUT);
76
77                 if (status == len) {   /*  Success this control transfer. */
78                         rtw_reset_continual_urb_error(dvobjpriv);
79                         if (requesttype == 0x01)
80                                 memcpy(pdata, pIo_buf,  len);
81                 } else { /*  error cases */
82                         DBG_88E("reg 0x%x, usb %s %u fail, status:%d value=0x%x, vendorreq_times:%d\n",
83                                 value, (requesttype == 0x01) ? "read" : "write",
84                                 len, status, *(u32 *)pdata, vendorreq_times);
85
86                         if (status < 0) {
87                                 if (status == (-ESHUTDOWN) || status == -ENODEV) {
88                                         adapt->bSurpriseRemoved = true;
89                                 } else {
90                                         struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
91                                         haldata->srestpriv.Wifi_Error_Status = USB_VEN_REQ_CMD_FAIL;
92                                 }
93                         } else { /*  status != len && status >= 0 */
94                                 if (status > 0) {
95                                         if (requesttype == 0x01) {
96                                                 /*  For Control read transfer, we have to copy the read data from pIo_buf to pdata. */
97                                                 memcpy(pdata, pIo_buf,  len);
98                                         }
99                                 }
100                         }
101
102                         if (rtw_inc_and_chk_continual_urb_error(dvobjpriv)) {
103                                 adapt->bSurpriseRemoved = true;
104                                 break;
105                         }
106
107                 }
108
109                 /*  firmware download is checksumed, don't retry */
110                 if ((value >= FW_8188E_START_ADDRESS && value <= FW_8188E_END_ADDRESS) || status == len)
111                         break;
112         }
113 release_mutex:
114         mutex_unlock(&dvobjpriv->usb_vendor_req_mutex);
115 exit:
116         return status;
117 }
118
119 static u8 usb_read8(struct intf_hdl *pintfhdl, u32 addr)
120 {
121         u8 request;
122         u8 requesttype;
123         u16 wvalue;
124         u16 index;
125         u16 len;
126         u8 data = 0;
127
128
129         request = 0x05;
130         requesttype = 0x01;/* read_in */
131         index = 0;/* n/a */
132
133         wvalue = (u16)(addr&0x0000ffff);
134         len = 1;
135
136         usbctrl_vendorreq(pintfhdl, request, wvalue, index, &data, len, requesttype);
137
138
139         return data;
140
141 }
142
143 static u16 usb_read16(struct intf_hdl *pintfhdl, u32 addr)
144 {
145         u8 request;
146         u8 requesttype;
147         u16 wvalue;
148         u16 index;
149         u16 len;
150         __le32 data;
151
152         request = 0x05;
153         requesttype = 0x01;/* read_in */
154         index = 0;/* n/a */
155         wvalue = (u16)(addr&0x0000ffff);
156         len = 2;
157         usbctrl_vendorreq(pintfhdl, request, wvalue, index, &data, len, requesttype);
158
159         return (u16)(le32_to_cpu(data)&0xffff);
160 }
161
162 static u32 usb_read32(struct intf_hdl *pintfhdl, u32 addr)
163 {
164         u8 request;
165         u8 requesttype;
166         u16 wvalue;
167         u16 index;
168         u16 len;
169         __le32 data;
170
171
172         request = 0x05;
173         requesttype = 0x01;/* read_in */
174         index = 0;/* n/a */
175
176         wvalue = (u16)(addr&0x0000ffff);
177         len = 4;
178
179         usbctrl_vendorreq(pintfhdl, request, wvalue, index, &data, len, requesttype);
180
181
182         return le32_to_cpu(data);
183 }
184
185 static int usb_write8(struct intf_hdl *pintfhdl, u32 addr, u8 val)
186 {
187         u8 request;
188         u8 requesttype;
189         u16 wvalue;
190         u16 index;
191         u16 len;
192         u8 data;
193         int ret;
194
195         request = 0x05;
196         requesttype = 0x00;/* write_out */
197         index = 0;/* n/a */
198         wvalue = (u16)(addr&0x0000ffff);
199         len = 1;
200         data = val;
201         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index, &data, len, requesttype);
202         return ret;
203 }
204
205 static int usb_write16(struct intf_hdl *pintfhdl, u32 addr, u16 val)
206 {
207         u8 request;
208         u8 requesttype;
209         u16 wvalue;
210         u16 index;
211         u16 len;
212         __le32 data;
213         int ret;
214
215
216         request = 0x05;
217         requesttype = 0x00;/* write_out */
218         index = 0;/* n/a */
219
220         wvalue = (u16)(addr&0x0000ffff);
221         len = 2;
222
223         data = cpu_to_le32(val & 0x0000ffff);
224
225         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index, &data, len, requesttype);
226
227
228         return ret;
229 }
230
231 static int usb_write32(struct intf_hdl *pintfhdl, u32 addr, u32 val)
232 {
233         u8 request;
234         u8 requesttype;
235         u16 wvalue;
236         u16 index;
237         u16 len;
238         __le32 data;
239         int ret;
240
241
242         request = 0x05;
243         requesttype = 0x00;/* write_out */
244         index = 0;/* n/a */
245
246         wvalue = (u16)(addr&0x0000ffff);
247         len = 4;
248         data = cpu_to_le32(val);
249
250         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index, &data, len, requesttype);
251
252
253         return ret;
254 }
255
256 static int usb_writeN(struct intf_hdl *pintfhdl, u32 addr, u32 length, u8 *pdata)
257 {
258         u8 request;
259         u8 requesttype;
260         u16 wvalue;
261         u16 index;
262         u16 len;
263         u8 buf[VENDOR_CMD_MAX_DATA_LEN] = {0};
264         int ret;
265
266
267         request = 0x05;
268         requesttype = 0x00;/* write_out */
269         index = 0;/* n/a */
270
271         wvalue = (u16)(addr&0x0000ffff);
272         len = length;
273          memcpy(buf, pdata, len);
274
275         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index, buf, len, requesttype);
276
277
278         return ret;
279 }
280
281 static void interrupt_handler_8188eu(struct adapter *adapt, u16 pkt_len, u8 *pbuf)
282 {
283         struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
284
285         if (pkt_len != INTERRUPT_MSG_FORMAT_LEN) {
286                 DBG_88E("%s Invalid interrupt content length (%d)!\n", __func__, pkt_len);
287                 return;
288         }
289
290         /*  HISR */
291         memcpy(&(haldata->IntArray[0]), &(pbuf[USB_INTR_CONTENT_HISR_OFFSET]), 4);
292         memcpy(&(haldata->IntArray[1]), &(pbuf[USB_INTR_CONTENT_HISRE_OFFSET]), 4);
293
294         /*  C2H Event */
295         if (pbuf[0] != 0)
296                 memcpy(&(haldata->C2hArray[0]), &(pbuf[USB_INTR_CONTENT_C2H_OFFSET]), 16);
297 }
298
299 static int recvbuf2recvframe(struct adapter *adapt, struct sk_buff *pskb)
300 {
301         u8      *pbuf;
302         u8      shift_sz = 0;
303         u16     pkt_cnt;
304         u32     pkt_offset, skb_len, alloc_sz;
305         s32     transfer_len;
306         struct recv_stat        *prxstat;
307         struct phy_stat *pphy_status = NULL;
308         struct sk_buff *pkt_copy = NULL;
309         struct recv_frame       *precvframe = NULL;
310         struct rx_pkt_attrib    *pattrib = NULL;
311         struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
312         struct recv_priv        *precvpriv = &adapt->recvpriv;
313         struct __queue *pfree_recv_queue = &precvpriv->free_recv_queue;
314
315         transfer_len = (s32)pskb->len;
316         pbuf = pskb->data;
317
318         prxstat = (struct recv_stat *)pbuf;
319         pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
320
321         do {
322                 RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
323                          ("recvbuf2recvframe: rxdesc=offsset 0:0x%08x, 4:0x%08x, 8:0x%08x, C:0x%08x\n",
324                           prxstat->rxdw0, prxstat->rxdw1, prxstat->rxdw2, prxstat->rxdw4));
325
326                 prxstat = (struct recv_stat *)pbuf;
327
328                 precvframe = rtw_alloc_recvframe(pfree_recv_queue);
329                 if (precvframe == NULL) {
330                         RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvbuf2recvframe: precvframe==NULL\n"));
331                         DBG_88E("%s()-%d: rtw_alloc_recvframe() failed! RX Drop!\n", __func__, __LINE__);
332                         goto _exit_recvbuf2recvframe;
333                 }
334
335                 _rtw_init_listhead(&precvframe->list);
336                 precvframe->len = 0;
337
338                 update_recvframe_attrib_88e(precvframe, prxstat);
339
340                 pattrib = &precvframe->attrib;
341
342                 if ((pattrib->crc_err) || (pattrib->icv_err)) {
343                         DBG_88E("%s: RX Warning! crc_err=%d icv_err=%d, skip!\n", __func__, pattrib->crc_err, pattrib->icv_err);
344
345                         rtw_free_recvframe(precvframe, pfree_recv_queue);
346                         goto _exit_recvbuf2recvframe;
347                 }
348
349                 if ((pattrib->physt) && (pattrib->pkt_rpt_type == NORMAL_RX))
350                         pphy_status = (struct phy_stat *)(pbuf + RXDESC_OFFSET);
351
352                 pkt_offset = RXDESC_SIZE + pattrib->drvinfo_sz + pattrib->shift_sz + pattrib->pkt_len;
353
354                 if ((pattrib->pkt_len <= 0) || (pkt_offset > transfer_len)) {
355                         RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recvbuf2recvframe: pkt_len<=0\n"));
356                         DBG_88E("%s()-%d: RX Warning!,pkt_len<=0 or pkt_offset> transfoer_len\n", __func__, __LINE__);
357                         rtw_free_recvframe(precvframe, pfree_recv_queue);
358                         goto _exit_recvbuf2recvframe;
359                 }
360
361                 /*      Modified by Albert 20101213 */
362                 /*      For 8 bytes IP header alignment. */
363                 if (pattrib->qos)       /*      Qos data, wireless lan header length is 26 */
364                         shift_sz = 6;
365                 else
366                         shift_sz = 0;
367
368                 skb_len = pattrib->pkt_len;
369
370                 /*  for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */
371                 /*  modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */
372                 if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
373                         if (skb_len <= 1650)
374                                 alloc_sz = 1664;
375                         else
376                                 alloc_sz = skb_len + 14;
377                 } else {
378                         alloc_sz = skb_len;
379                         /*      6 is for IP header 8 bytes alignment in QoS packet case. */
380                         /*      8 is for skb->data 4 bytes alignment. */
381                         alloc_sz += 14;
382                 }
383
384                 pkt_copy = netdev_alloc_skb(adapt->pnetdev, alloc_sz);
385                 if (pkt_copy) {
386                         pkt_copy->dev = adapt->pnetdev;
387                         precvframe->pkt = pkt_copy;
388                         precvframe->rx_head = pkt_copy->data;
389                         precvframe->rx_end = pkt_copy->data + alloc_sz;
390                         skb_reserve(pkt_copy, 8 - ((size_t)(pkt_copy->data) & 7));/* force pkt_copy->data at 8-byte alignment address */
391                         skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */
392                         memcpy(pkt_copy->data, (pbuf + pattrib->drvinfo_sz + RXDESC_SIZE), skb_len);
393                         precvframe->rx_tail = pkt_copy->data;
394                         precvframe->rx_data = pkt_copy->data;
395                 } else {
396                         if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
397                                 DBG_88E("recvbuf2recvframe: alloc_skb fail , drop frag frame\n");
398                                 rtw_free_recvframe(precvframe, pfree_recv_queue);
399                                 goto _exit_recvbuf2recvframe;
400                         }
401                         precvframe->pkt = skb_clone(pskb, GFP_ATOMIC);
402                         if (precvframe->pkt) {
403                                 precvframe->rx_tail = pbuf + pattrib->drvinfo_sz + RXDESC_SIZE;
404                                 precvframe->rx_head = precvframe->rx_tail;
405                                 precvframe->rx_data = precvframe->rx_tail;
406                                 precvframe->rx_end =  pbuf + pattrib->drvinfo_sz + RXDESC_SIZE + alloc_sz;
407                         } else {
408                                 DBG_88E("recvbuf2recvframe: skb_clone fail\n");
409                                 rtw_free_recvframe(precvframe, pfree_recv_queue);
410                                 goto _exit_recvbuf2recvframe;
411                         }
412                 }
413
414                 recvframe_put(precvframe, skb_len);
415
416                 switch (haldata->UsbRxAggMode) {
417                 case USB_RX_AGG_DMA:
418                 case USB_RX_AGG_MIX:
419                         pkt_offset = (u16)_RND128(pkt_offset);
420                         break;
421                 case USB_RX_AGG_USB:
422                         pkt_offset = (u16)_RND4(pkt_offset);
423                         break;
424                 case USB_RX_AGG_DISABLE:
425                 default:
426                         break;
427                 }
428                 if (pattrib->pkt_rpt_type == NORMAL_RX) { /* Normal rx packet */
429                         if (pattrib->physt)
430                                 update_recvframe_phyinfo_88e(precvframe, (struct phy_stat *)pphy_status);
431                         if (rtw_recv_entry(precvframe) != _SUCCESS) {
432                                 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
433                                         ("recvbuf2recvframe: rtw_recv_entry(precvframe) != _SUCCESS\n"));
434                         }
435                 } else {
436                         /* enqueue recvframe to txrtp queue */
437                         if (pattrib->pkt_rpt_type == TX_REPORT1) {
438                                 /* CCX-TXRPT ack for xmit mgmt frames. */
439                                 handle_txrpt_ccx_88e(adapt, precvframe->rx_data);
440                         } else if (pattrib->pkt_rpt_type == TX_REPORT2) {
441                                 ODM_RA_TxRPT2Handle_8188E(
442                                                         &haldata->odmpriv,
443                                                         precvframe->rx_data,
444                                                         pattrib->pkt_len,
445                                                         pattrib->MacIDValidEntry[0],
446                                                         pattrib->MacIDValidEntry[1]
447                                                         );
448                         } else if (pattrib->pkt_rpt_type == HIS_REPORT) {
449                                 interrupt_handler_8188eu(adapt, pattrib->pkt_len, precvframe->rx_data);
450                         }
451                         rtw_free_recvframe(precvframe, pfree_recv_queue);
452                 }
453                 pkt_cnt--;
454                 transfer_len -= pkt_offset;
455                 pbuf += pkt_offset;
456                 precvframe = NULL;
457                 pkt_copy = NULL;
458
459                 if (transfer_len > 0 && pkt_cnt == 0)
460                         pkt_cnt = (le32_to_cpu(prxstat->rxdw2)>>16) & 0xff;
461
462         } while ((transfer_len > 0) && (pkt_cnt > 0));
463
464 _exit_recvbuf2recvframe:
465
466         return _SUCCESS;
467 }
468
469 void rtl8188eu_recv_tasklet(void *priv)
470 {
471         struct sk_buff *pskb;
472         struct adapter *adapt = (struct adapter *)priv;
473         struct recv_priv *precvpriv = &adapt->recvpriv;
474
475         while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) {
476                 if ((adapt->bDriverStopped) || (adapt->bSurpriseRemoved)) {
477                         DBG_88E("recv_tasklet => bDriverStopped or bSurpriseRemoved\n");
478                         dev_kfree_skb_any(pskb);
479                         break;
480                 }
481                 recvbuf2recvframe(adapt, pskb);
482                 skb_reset_tail_pointer(pskb);
483                 pskb->len = 0;
484                 skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
485         }
486 }
487
488 static void usb_read_port_complete(struct urb *purb, struct pt_regs *regs)
489 {
490         struct recv_buf *precvbuf = (struct recv_buf *)purb->context;
491         struct adapter  *adapt = (struct adapter *)precvbuf->adapter;
492         struct recv_priv *precvpriv = &adapt->recvpriv;
493
494         RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete!!!\n"));
495
496         precvpriv->rx_pending_cnt--;
497
498         if (adapt->bSurpriseRemoved || adapt->bDriverStopped || adapt->bReadPortCancel) {
499                 RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
500                          ("usb_read_port_complete:bDriverStopped(%d) OR bSurpriseRemoved(%d)\n",
501                          adapt->bDriverStopped, adapt->bSurpriseRemoved));
502
503                 precvbuf->reuse = true;
504                 DBG_88E("%s() RX Warning! bDriverStopped(%d) OR bSurpriseRemoved(%d) bReadPortCancel(%d)\n",
505                         __func__, adapt->bDriverStopped,
506                         adapt->bSurpriseRemoved, adapt->bReadPortCancel);
507                 return;
508         }
509
510         if (purb->status == 0) { /* SUCCESS */
511                 if ((purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)) {
512                         RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
513                                  ("usb_read_port_complete: (purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)\n"));
514                         precvbuf->reuse = true;
515                         rtw_read_port(adapt, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
516                         DBG_88E("%s()-%d: RX Warning!\n", __func__, __LINE__);
517                 } else {
518                         rtw_reset_continual_urb_error(adapter_to_dvobj(adapt));
519
520                         skb_put(precvbuf->pskb, purb->actual_length);
521                         skb_queue_tail(&precvpriv->rx_skb_queue, precvbuf->pskb);
522
523                         if (skb_queue_len(&precvpriv->rx_skb_queue) <= 1)
524                                 tasklet_schedule(&precvpriv->recv_tasklet);
525
526                         precvbuf->pskb = NULL;
527                         precvbuf->reuse = false;
528                         rtw_read_port(adapt, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
529                 }
530         } else {
531                 RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete : purb->status(%d) != 0\n", purb->status));
532
533                 DBG_88E("###=> usb_read_port_complete => urb status(%d)\n", purb->status);
534                 skb_put(precvbuf->pskb, purb->actual_length);
535                 precvbuf->pskb = NULL;
536
537                 if (rtw_inc_and_chk_continual_urb_error(adapter_to_dvobj(adapt)))
538                         adapt->bSurpriseRemoved = true;
539
540                 switch (purb->status) {
541                 case -EINVAL:
542                 case -EPIPE:
543                 case -ENODEV:
544                 case -ESHUTDOWN:
545                         RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete:bSurpriseRemoved=true\n"));
546                 case -ENOENT:
547                         adapt->bDriverStopped = true;
548                         RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete:bDriverStopped=true\n"));
549                         break;
550                 case -EPROTO:
551                 case -EOVERFLOW:
552                         {
553                                 struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
554                                 haldata->srestpriv.Wifi_Error_Status = USB_READ_PORT_FAIL;
555                         }
556                         precvbuf->reuse = true;
557                         rtw_read_port(adapt, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
558                         break;
559                 case -EINPROGRESS:
560                         DBG_88E("ERROR: URB IS IN PROGRESS!\n");
561                         break;
562                 default:
563                         break;
564                 }
565         }
566 }
567
568 static u32 usb_read_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *rmem)
569 {
570         struct urb *purb = NULL;
571         struct recv_buf *precvbuf = (struct recv_buf *)rmem;
572         struct adapter          *adapter = pintfhdl->padapter;
573         struct dvobj_priv       *pdvobj = adapter_to_dvobj(adapter);
574         struct recv_priv        *precvpriv = &adapter->recvpriv;
575         struct usb_device       *pusbd = pdvobj->pusbdev;
576         int err;
577         unsigned int pipe;
578         size_t tmpaddr = 0;
579         size_t alignment = 0;
580         u32 ret = _SUCCESS;
581
582
583         if (adapter->bDriverStopped || adapter->bSurpriseRemoved ||
584             adapter->pwrctrlpriv.pnp_bstop_trx) {
585                 RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
586                          ("usb_read_port:(adapt->bDriverStopped ||adapt->bSurpriseRemoved ||adapter->pwrctrlpriv.pnp_bstop_trx)!!!\n"));
587                 return _FAIL;
588         }
589
590         if (!precvbuf) {
591                 RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
592                          ("usb_read_port:precvbuf==NULL\n"));
593                 return _FAIL;
594         }
595
596         if ((!precvbuf->reuse) || (precvbuf->pskb == NULL)) {
597                 precvbuf->pskb = skb_dequeue(&precvpriv->free_recv_skb_queue);
598                 if (NULL != precvbuf->pskb)
599                         precvbuf->reuse = true;
600         }
601
602         /* re-assign for linux based on skb */
603         if ((!precvbuf->reuse) || (precvbuf->pskb == NULL)) {
604                 precvbuf->pskb = netdev_alloc_skb(adapter->pnetdev, MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
605                 if (precvbuf->pskb == NULL) {
606                         RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("init_recvbuf(): alloc_skb fail!\n"));
607                         DBG_88E("#### usb_read_port() alloc_skb fail!#####\n");
608                         return _FAIL;
609                 }
610
611                 tmpaddr = (size_t)precvbuf->pskb->data;
612                 alignment = tmpaddr & (RECVBUFF_ALIGN_SZ-1);
613                 skb_reserve(precvbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
614         } else { /* reuse skb */
615                 precvbuf->reuse = false;
616         }
617
618         precvpriv->rx_pending_cnt++;
619
620         purb = precvbuf->purb;
621
622         /* translate DMA FIFO addr to pipehandle */
623         pipe = ffaddr2pipehdl(pdvobj, addr);
624
625         usb_fill_bulk_urb(purb, pusbd, pipe,
626                           precvbuf->pskb->data,
627                           MAX_RECVBUF_SZ,
628                           usb_read_port_complete,
629                           precvbuf);/* context is precvbuf */
630
631         err = usb_submit_urb(purb, GFP_ATOMIC);
632         if ((err) && (err != (-EPERM))) {
633                 RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
634                          ("cannot submit rx in-token(err=0x%.8x), URB_STATUS =0x%.8x",
635                          err, purb->status));
636                 DBG_88E("cannot submit rx in-token(err = 0x%08x),urb_status = %d\n",
637                         err, purb->status);
638                 ret = _FAIL;
639         }
640
641         return ret;
642 }
643
644 void rtl8188eu_xmit_tasklet(void *priv)
645 {
646         int ret = false;
647         struct adapter *adapt = (struct adapter *)priv;
648         struct xmit_priv *pxmitpriv = &adapt->xmitpriv;
649
650         if (check_fwstate(&adapt->mlmepriv, _FW_UNDER_SURVEY))
651                 return;
652
653         while (1) {
654                 if ((adapt->bDriverStopped) ||
655                     (adapt->bSurpriseRemoved) ||
656                     (adapt->bWritePortCancel)) {
657                         DBG_88E("xmit_tasklet => bDriverStopped or bSurpriseRemoved or bWritePortCancel\n");
658                         break;
659                 }
660
661                 ret = rtl8188eu_xmitframe_complete(adapt, pxmitpriv, NULL);
662
663                 if (!ret)
664                         break;
665         }
666 }
667
668 void rtl8188eu_set_intf_ops(struct _io_ops      *pops)
669 {
670         _rtw_memset((u8 *)pops, 0, sizeof(struct _io_ops));
671         pops->_read8 = &usb_read8;
672         pops->_read16 = &usb_read16;
673         pops->_read32 = &usb_read32;
674         pops->_read_mem = &usb_read_mem;
675         pops->_read_port = &usb_read_port;
676         pops->_write8 = &usb_write8;
677         pops->_write16 = &usb_write16;
678         pops->_write32 = &usb_write32;
679         pops->_writeN = &usb_writeN;
680         pops->_write_mem = &usb_write_mem;
681         pops->_write_port = &usb_write_port;
682         pops->_read_port_cancel = &usb_read_port_cancel;
683         pops->_write_port_cancel = &usb_write_port_cancel;
684 }