• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 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 #define _USB_OPS_LINUX_C_
20 
21 #include <drv_types.h>
22 #include <recv_osdep.h>
23 #include <rtw_sreset.h>
24 
interrupt_handler_8188eu(struct adapter * adapt,u16 pkt_len,u8 * pbuf)25 static void interrupt_handler_8188eu(struct adapter *adapt, u16 pkt_len, u8 *pbuf)
26 {
27 	struct hal_data_8188e	*haldata = GET_HAL_DATA(adapt);
28 
29 	if (pkt_len != INTERRUPT_MSG_FORMAT_LEN) {
30 		DBG_88E("%s Invalid interrupt content length (%d)!\n", __func__, pkt_len);
31 		return;
32 	}
33 
34 	/*  HISR */
35 	memcpy(&(haldata->IntArray[0]), &(pbuf[USB_INTR_CONTENT_HISR_OFFSET]), 4);
36 	memcpy(&(haldata->IntArray[1]), &(pbuf[USB_INTR_CONTENT_HISRE_OFFSET]), 4);
37 
38 	/*  C2H Event */
39 	if (pbuf[0] != 0)
40 		memcpy(&(haldata->C2hArray[0]), &(pbuf[USB_INTR_CONTENT_C2H_OFFSET]), 16);
41 }
42 
recvbuf2recvframe(struct adapter * adapt,struct sk_buff * pskb)43 static int recvbuf2recvframe(struct adapter *adapt, struct sk_buff *pskb)
44 {
45 	u8	*pbuf;
46 	u8	shift_sz = 0;
47 	u16	pkt_cnt;
48 	u32	pkt_offset, skb_len, alloc_sz;
49 	s32	transfer_len;
50 	struct recv_stat	*prxstat;
51 	struct phy_stat	*pphy_status = NULL;
52 	struct sk_buff *pkt_copy = NULL;
53 	struct recv_frame	*precvframe = NULL;
54 	struct rx_pkt_attrib	*pattrib = NULL;
55 	struct hal_data_8188e	*haldata = GET_HAL_DATA(adapt);
56 	struct recv_priv	*precvpriv = &adapt->recvpriv;
57 	struct __queue *pfree_recv_queue = &precvpriv->free_recv_queue;
58 
59 	transfer_len = (s32)pskb->len;
60 	pbuf = pskb->data;
61 
62 	prxstat = (struct recv_stat *)pbuf;
63 	pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
64 
65 	do {
66 		RT_TRACE(_module_rtl871x_recv_c_, _drv_info_,
67 			 ("recvbuf2recvframe: rxdesc=offsset 0:0x%08x, 4:0x%08x, 8:0x%08x, C:0x%08x\n",
68 			  prxstat->rxdw0, prxstat->rxdw1, prxstat->rxdw2, prxstat->rxdw4));
69 
70 		prxstat = (struct recv_stat *)pbuf;
71 
72 		precvframe = rtw_alloc_recvframe(pfree_recv_queue);
73 		if (precvframe == NULL) {
74 			RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("recvbuf2recvframe: precvframe==NULL\n"));
75 			DBG_88E("%s()-%d: rtw_alloc_recvframe() failed! RX Drop!\n", __func__, __LINE__);
76 			goto _exit_recvbuf2recvframe;
77 		}
78 
79 		INIT_LIST_HEAD(&precvframe->list);
80 		precvframe->len = 0;
81 
82 		update_recvframe_attrib_88e(precvframe, prxstat);
83 
84 		pattrib = &precvframe->attrib;
85 
86 		if ((pattrib->crc_err) || (pattrib->icv_err)) {
87 			DBG_88E("%s: RX Warning! crc_err=%d icv_err=%d, skip!\n", __func__, pattrib->crc_err, pattrib->icv_err);
88 
89 			rtw_free_recvframe(precvframe, pfree_recv_queue);
90 			goto _exit_recvbuf2recvframe;
91 		}
92 
93 		if ((pattrib->physt) && (pattrib->pkt_rpt_type == NORMAL_RX))
94 			pphy_status = (struct phy_stat *)(pbuf + RXDESC_OFFSET);
95 
96 		pkt_offset = RXDESC_SIZE + pattrib->drvinfo_sz + pattrib->shift_sz + pattrib->pkt_len;
97 
98 		if ((pattrib->pkt_len <= 0) || (pkt_offset > transfer_len)) {
99 			RT_TRACE(_module_rtl871x_recv_c_, _drv_info_, ("recvbuf2recvframe: pkt_len<=0\n"));
100 			DBG_88E("%s()-%d: RX Warning!,pkt_len<=0 or pkt_offset> transfoer_len\n", __func__, __LINE__);
101 			rtw_free_recvframe(precvframe, pfree_recv_queue);
102 			goto _exit_recvbuf2recvframe;
103 		}
104 
105 		/*	Modified by Albert 20101213 */
106 		/*	For 8 bytes IP header alignment. */
107 		if (pattrib->qos)	/*	Qos data, wireless lan header length is 26 */
108 			shift_sz = 6;
109 		else
110 			shift_sz = 0;
111 
112 		skb_len = pattrib->pkt_len;
113 
114 		/*  for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */
115 		/*  modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */
116 		if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
117 			if (skb_len <= 1650)
118 				alloc_sz = 1664;
119 			else
120 				alloc_sz = skb_len + 14;
121 		} else {
122 			alloc_sz = skb_len;
123 			/*	6 is for IP header 8 bytes alignment in QoS packet case. */
124 			/*	8 is for skb->data 4 bytes alignment. */
125 			alloc_sz += 14;
126 		}
127 
128 		pkt_copy = netdev_alloc_skb(adapt->pnetdev, alloc_sz);
129 		if (pkt_copy) {
130 			pkt_copy->dev = adapt->pnetdev;
131 			precvframe->pkt = pkt_copy;
132 			precvframe->rx_head = pkt_copy->data;
133 			precvframe->rx_end = pkt_copy->data + alloc_sz;
134 			skb_reserve(pkt_copy, 8 - ((size_t)(pkt_copy->data) & 7));/* force pkt_copy->data at 8-byte alignment address */
135 			skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */
136 			memcpy(pkt_copy->data, (pbuf + pattrib->drvinfo_sz + RXDESC_SIZE), skb_len);
137 			precvframe->rx_tail = pkt_copy->data;
138 			precvframe->rx_data = pkt_copy->data;
139 		} else {
140 			if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
141 				DBG_88E("recvbuf2recvframe: alloc_skb fail , drop frag frame\n");
142 				rtw_free_recvframe(precvframe, pfree_recv_queue);
143 				goto _exit_recvbuf2recvframe;
144 			}
145 			precvframe->pkt = skb_clone(pskb, GFP_ATOMIC);
146 			if (precvframe->pkt) {
147 				precvframe->rx_tail = pbuf + pattrib->drvinfo_sz + RXDESC_SIZE;
148 				precvframe->rx_head = precvframe->rx_tail;
149 				precvframe->rx_data = precvframe->rx_tail;
150 				precvframe->rx_end =  pbuf + pattrib->drvinfo_sz + RXDESC_SIZE + alloc_sz;
151 			} else {
152 				DBG_88E("recvbuf2recvframe: skb_clone fail\n");
153 				rtw_free_recvframe(precvframe, pfree_recv_queue);
154 				goto _exit_recvbuf2recvframe;
155 			}
156 		}
157 
158 		recvframe_put(precvframe, skb_len);
159 
160 		switch (haldata->UsbRxAggMode) {
161 		case USB_RX_AGG_DMA:
162 		case USB_RX_AGG_MIX:
163 			pkt_offset = (u16) round_up(pkt_offset, 128);
164 			break;
165 		case USB_RX_AGG_USB:
166 			pkt_offset = (u16) round_up(pkt_offset, 4);
167 			break;
168 		case USB_RX_AGG_DISABLE:
169 		default:
170 			break;
171 		}
172 		if (pattrib->pkt_rpt_type == NORMAL_RX) { /* Normal rx packet */
173 			if (pattrib->physt)
174 				update_recvframe_phyinfo_88e(precvframe, (struct phy_stat *)pphy_status);
175 			if (rtw_recv_entry(precvframe) != _SUCCESS) {
176 				RT_TRACE(_module_rtl871x_recv_c_, _drv_err_,
177 					("recvbuf2recvframe: rtw_recv_entry(precvframe) != _SUCCESS\n"));
178 			}
179 		} else {
180 			/* enqueue recvframe to txrtp queue */
181 			if (pattrib->pkt_rpt_type == TX_REPORT1) {
182 				/* CCX-TXRPT ack for xmit mgmt frames. */
183 				handle_txrpt_ccx_88e(adapt, precvframe->rx_data);
184 			} else if (pattrib->pkt_rpt_type == TX_REPORT2) {
185 				ODM_RA_TxRPT2Handle_8188E(
186 							&haldata->odmpriv,
187 							precvframe->rx_data,
188 							pattrib->pkt_len,
189 							pattrib->MacIDValidEntry[0],
190 							pattrib->MacIDValidEntry[1]
191 							);
192 			} else if (pattrib->pkt_rpt_type == HIS_REPORT) {
193 				interrupt_handler_8188eu(adapt, pattrib->pkt_len, precvframe->rx_data);
194 			}
195 			rtw_free_recvframe(precvframe, pfree_recv_queue);
196 		}
197 		pkt_cnt--;
198 		transfer_len -= pkt_offset;
199 		pbuf += pkt_offset;
200 		precvframe = NULL;
201 		pkt_copy = NULL;
202 
203 		if (transfer_len > 0 && pkt_cnt == 0)
204 			pkt_cnt = (le32_to_cpu(prxstat->rxdw2)>>16) & 0xff;
205 
206 	} while ((transfer_len > 0) && (pkt_cnt > 0));
207 
208 _exit_recvbuf2recvframe:
209 
210 	return _SUCCESS;
211 }
212 
ffaddr2pipehdl(struct dvobj_priv * pdvobj,u32 addr)213 unsigned int ffaddr2pipehdl(struct dvobj_priv *pdvobj, u32 addr)
214 {
215 	unsigned int pipe = 0, ep_num = 0;
216 	struct usb_device *pusbd = pdvobj->pusbdev;
217 
218 	if (addr == RECV_BULK_IN_ADDR) {
219 		pipe = usb_rcvbulkpipe(pusbd, pdvobj->RtInPipe[0]);
220 	} else if (addr == RECV_INT_IN_ADDR) {
221 		pipe = usb_rcvbulkpipe(pusbd, pdvobj->RtInPipe[1]);
222 	} else if (addr < HW_QUEUE_ENTRY) {
223 		ep_num = pdvobj->Queue2Pipe[addr];
224 		pipe = usb_sndbulkpipe(pusbd, ep_num);
225 	}
226 
227 	return pipe;
228 }
229 
usbctrl_vendorreq(struct adapter * adapt,u8 request,u16 value,u16 index,void * pdata,u16 len,u8 requesttype)230 static int usbctrl_vendorreq(struct adapter *adapt, u8 request, u16 value, u16 index, void *pdata, u16 len, u8 requesttype)
231 {
232 	struct dvobj_priv  *dvobjpriv = adapter_to_dvobj(adapt);
233 	struct usb_device *udev = dvobjpriv->pusbdev;
234 	unsigned int pipe;
235 	int status = 0;
236 	u8 reqtype;
237 	u8 *pIo_buf;
238 	int vendorreq_times = 0;
239 
240 	if ((adapt->bSurpriseRemoved) || (adapt->pwrctrlpriv.pnp_bstop_trx)) {
241 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usbctrl_vendorreq:(adapt->bSurpriseRemoved ||adapter->pwrctrlpriv.pnp_bstop_trx)!!!\n"));
242 		status = -EPERM;
243 		goto exit;
244 	}
245 
246 	if (len > MAX_VENDOR_REQ_CMD_SIZE) {
247 		DBG_88E("[%s] Buffer len error ,vendor request failed\n", __func__);
248 		status = -EINVAL;
249 		goto exit;
250 	}
251 
252 	_enter_critical_mutex(&dvobjpriv->usb_vendor_req_mutex, NULL);
253 
254 	/*  Acquire IO memory for vendorreq */
255 	pIo_buf = dvobjpriv->usb_vendor_req_buf;
256 
257 	if (pIo_buf == NULL) {
258 		DBG_88E("[%s] pIo_buf == NULL\n", __func__);
259 		status = -ENOMEM;
260 		goto release_mutex;
261 	}
262 
263 	while (++vendorreq_times <= MAX_USBCTRL_VENDORREQ_TIMES) {
264 		memset(pIo_buf, 0, len);
265 
266 		if (requesttype == 0x01) {
267 			pipe = usb_rcvctrlpipe(udev, 0);/* read_in */
268 			reqtype =  REALTEK_USB_VENQT_READ;
269 		} else {
270 			pipe = usb_sndctrlpipe(udev, 0);/* write_out */
271 			reqtype =  REALTEK_USB_VENQT_WRITE;
272 			memcpy(pIo_buf, pdata, len);
273 		}
274 
275 		status = usb_control_msg(udev, pipe, request, reqtype, value, index, pIo_buf, len, RTW_USB_CONTROL_MSG_TIMEOUT);
276 
277 		if (status == len) {   /*  Success this control transfer. */
278 			if (requesttype == 0x01)
279 				memcpy(pdata, pIo_buf,  len);
280 		} else { /*  error cases */
281 			DBG_88E("reg 0x%x, usb %s %u fail, status:%d value=0x%x, vendorreq_times:%d\n",
282 				value, (requesttype == 0x01) ? "read" : "write",
283 				len, status, *(u32 *)pdata, vendorreq_times);
284 
285 			if (status < 0) {
286 				if (status == (-ESHUTDOWN) || status == -ENODEV) {
287 					adapt->bSurpriseRemoved = true;
288 				} else {
289 					struct hal_data_8188e	*haldata = GET_HAL_DATA(adapt);
290 					haldata->srestpriv.Wifi_Error_Status = USB_VEN_REQ_CMD_FAIL;
291 				}
292 			} else { /*  status != len && status >= 0 */
293 				if (status > 0) {
294 					if (requesttype == 0x01) {
295 						/*  For Control read transfer, we have to copy the read data from pIo_buf to pdata. */
296 						memcpy(pdata, pIo_buf,  len);
297 					}
298 				}
299 			}
300 
301 		}
302 
303 		/*  firmware download is checksumed, don't retry */
304 		if ((value >= FW_8188E_START_ADDRESS && value <= FW_8188E_END_ADDRESS) || status == len)
305 			break;
306 	}
307 release_mutex:
308 	mutex_unlock(&dvobjpriv->usb_vendor_req_mutex);
309 exit:
310 	return status;
311 }
312 
usb_read8(struct adapter * adapter,u32 addr)313 u8 usb_read8(struct adapter *adapter, u32 addr)
314 {
315 	u8 request;
316 	u8 requesttype;
317 	u16 wvalue;
318 	u16 index;
319 	u16 len;
320 	u8 data = 0;
321 
322 
323 	request = 0x05;
324 	requesttype = 0x01;/* read_in */
325 	index = 0;/* n/a */
326 
327 	wvalue = (u16)(addr&0x0000ffff);
328 	len = 1;
329 
330 	usbctrl_vendorreq(adapter, request, wvalue, index, &data, len, requesttype);
331 
332 
333 	return data;
334 
335 }
336 
usb_read16(struct adapter * adapter,u32 addr)337 u16 usb_read16(struct adapter *adapter, u32 addr)
338 {
339 	u8 request;
340 	u8 requesttype;
341 	u16 wvalue;
342 	u16 index;
343 	u16 len;
344 	__le32 data;
345 
346 	request = 0x05;
347 	requesttype = 0x01;/* read_in */
348 	index = 0;/* n/a */
349 	wvalue = (u16)(addr&0x0000ffff);
350 	len = 2;
351 	usbctrl_vendorreq(adapter, request, wvalue, index, &data, len, requesttype);
352 
353 	return (u16)(le32_to_cpu(data)&0xffff);
354 }
355 
usb_read32(struct adapter * adapter,u32 addr)356 u32 usb_read32(struct adapter *adapter, u32 addr)
357 {
358 	u8 request;
359 	u8 requesttype;
360 	u16 wvalue;
361 	u16 index;
362 	u16 len;
363 	__le32 data;
364 
365 
366 	request = 0x05;
367 	requesttype = 0x01;/* read_in */
368 	index = 0;/* n/a */
369 
370 	wvalue = (u16)(addr&0x0000ffff);
371 	len = 4;
372 
373 	usbctrl_vendorreq(adapter, request, wvalue, index, &data, len, requesttype);
374 
375 
376 	return le32_to_cpu(data);
377 }
378 
usb_read_port_complete(struct urb * purb,struct pt_regs * regs)379 static void usb_read_port_complete(struct urb *purb, struct pt_regs *regs)
380 {
381 	struct recv_buf	*precvbuf = (struct recv_buf *)purb->context;
382 	struct adapter	*adapt = (struct adapter *)precvbuf->adapter;
383 	struct recv_priv *precvpriv = &adapt->recvpriv;
384 
385 	RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete!!!\n"));
386 
387 	precvpriv->rx_pending_cnt--;
388 
389 	if (adapt->bSurpriseRemoved || adapt->bDriverStopped || adapt->bReadPortCancel) {
390 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
391 			 ("usb_read_port_complete:bDriverStopped(%d) OR bSurpriseRemoved(%d)\n",
392 			 adapt->bDriverStopped, adapt->bSurpriseRemoved));
393 
394 		precvbuf->reuse = true;
395 		DBG_88E("%s() RX Warning! bDriverStopped(%d) OR bSurpriseRemoved(%d) bReadPortCancel(%d)\n",
396 			__func__, adapt->bDriverStopped,
397 			adapt->bSurpriseRemoved, adapt->bReadPortCancel);
398 		return;
399 	}
400 
401 	if (purb->status == 0) { /* SUCCESS */
402 		if ((purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)) {
403 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
404 				 ("usb_read_port_complete: (purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)\n"));
405 			precvbuf->reuse = true;
406 			usb_read_port(adapt, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
407 			DBG_88E("%s()-%d: RX Warning!\n", __func__, __LINE__);
408 		} else {
409 			skb_put(precvbuf->pskb, purb->actual_length);
410 			skb_queue_tail(&precvpriv->rx_skb_queue, precvbuf->pskb);
411 
412 			if (skb_queue_len(&precvpriv->rx_skb_queue) <= 1)
413 				tasklet_schedule(&precvpriv->recv_tasklet);
414 
415 			precvbuf->pskb = NULL;
416 			precvbuf->reuse = false;
417 			usb_read_port(adapt, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
418 		}
419 	} else {
420 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete : purb->status(%d) != 0\n", purb->status));
421 
422 		DBG_88E("###=> usb_read_port_complete => urb status(%d)\n", purb->status);
423 		skb_put(precvbuf->pskb, purb->actual_length);
424 		precvbuf->pskb = NULL;
425 
426 		switch (purb->status) {
427 		case -EINVAL:
428 		case -EPIPE:
429 		case -ENODEV:
430 		case -ESHUTDOWN:
431 			adapt->bSurpriseRemoved = true;
432 		case -ENOENT:
433 			adapt->bDriverStopped = true;
434 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_read_port_complete:bDriverStopped=true\n"));
435 			break;
436 		case -EPROTO:
437 		case -EOVERFLOW:
438 			{
439 				struct hal_data_8188e	*haldata = GET_HAL_DATA(adapt);
440 				haldata->srestpriv.Wifi_Error_Status = USB_READ_PORT_FAIL;
441 			}
442 			precvbuf->reuse = true;
443 			usb_read_port(adapt, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
444 			break;
445 		case -EINPROGRESS:
446 			DBG_88E("ERROR: URB IS IN PROGRESS!\n");
447 			break;
448 		default:
449 			break;
450 		}
451 	}
452 }
453 
usb_read_port(struct adapter * adapter,u32 addr,u32 cnt,u8 * rmem)454 u32 usb_read_port(struct adapter *adapter, u32 addr, u32 cnt, u8 *rmem)
455 {
456 	struct urb *purb = NULL;
457 	struct recv_buf	*precvbuf = (struct recv_buf *)rmem;
458 	struct dvobj_priv	*pdvobj = adapter_to_dvobj(adapter);
459 	struct recv_priv	*precvpriv = &adapter->recvpriv;
460 	struct usb_device	*pusbd = pdvobj->pusbdev;
461 	int err;
462 	unsigned int pipe;
463 	size_t tmpaddr = 0;
464 	size_t alignment = 0;
465 	u32 ret = _SUCCESS;
466 
467 
468 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved ||
469 	    adapter->pwrctrlpriv.pnp_bstop_trx) {
470 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
471 			 ("usb_read_port:(adapt->bDriverStopped ||adapt->bSurpriseRemoved ||adapter->pwrctrlpriv.pnp_bstop_trx)!!!\n"));
472 		return _FAIL;
473 	}
474 
475 	if (!precvbuf) {
476 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
477 			 ("usb_read_port:precvbuf==NULL\n"));
478 		return _FAIL;
479 	}
480 
481 	if ((!precvbuf->reuse) || (precvbuf->pskb == NULL)) {
482 		precvbuf->pskb = skb_dequeue(&precvpriv->free_recv_skb_queue);
483 		if (NULL != precvbuf->pskb)
484 			precvbuf->reuse = true;
485 	}
486 
487 	/* re-assign for linux based on skb */
488 	if ((!precvbuf->reuse) || (precvbuf->pskb == NULL)) {
489 		precvbuf->pskb = netdev_alloc_skb(adapter->pnetdev, MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
490 		if (precvbuf->pskb == NULL) {
491 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("init_recvbuf(): alloc_skb fail!\n"));
492 			DBG_88E("#### usb_read_port() alloc_skb fail!#####\n");
493 			return _FAIL;
494 		}
495 
496 		tmpaddr = (size_t)precvbuf->pskb->data;
497 		alignment = tmpaddr & (RECVBUFF_ALIGN_SZ-1);
498 		skb_reserve(precvbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
499 	} else { /* reuse skb */
500 		precvbuf->reuse = false;
501 	}
502 
503 	precvpriv->rx_pending_cnt++;
504 
505 	purb = precvbuf->purb;
506 
507 	/* translate DMA FIFO addr to pipehandle */
508 	pipe = ffaddr2pipehdl(pdvobj, addr);
509 
510 	usb_fill_bulk_urb(purb, pusbd, pipe,
511 			  precvbuf->pskb->data,
512 			  MAX_RECVBUF_SZ,
513 			  usb_read_port_complete,
514 			  precvbuf);/* context is precvbuf */
515 
516 	err = usb_submit_urb(purb, GFP_ATOMIC);
517 	if ((err) && (err != (-EPERM))) {
518 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
519 			 ("cannot submit rx in-token(err=0x%.8x), URB_STATUS =0x%.8x",
520 			 err, purb->status));
521 		DBG_88E("cannot submit rx in-token(err = 0x%08x),urb_status = %d\n",
522 			err, purb->status);
523 		ret = _FAIL;
524 	}
525 
526 	return ret;
527 }
528 
usb_read_port_cancel(struct adapter * padapter)529 void usb_read_port_cancel(struct adapter *padapter)
530 {
531 	int i;
532 	struct recv_buf *precvbuf;
533 	precvbuf = (struct recv_buf *)padapter->recvpriv.precv_buf;
534 
535 	DBG_88E("%s\n", __func__);
536 
537 	padapter->bReadPortCancel = true;
538 
539 	for (i = 0; i < NR_RECVBUFF; i++) {
540 		precvbuf->reuse = true;
541 		if (precvbuf->purb)
542 			usb_kill_urb(precvbuf->purb);
543 		precvbuf++;
544 	}
545 }
546 
usb_write8(struct adapter * adapter,u32 addr,u8 val)547 int usb_write8(struct adapter *adapter, u32 addr, u8 val)
548 {
549 	u8 request;
550 	u8 requesttype;
551 	u16 wvalue;
552 	u16 index;
553 	u16 len;
554 	u8 data;
555 	int ret;
556 
557 	request = 0x05;
558 	requesttype = 0x00;/* write_out */
559 	index = 0;/* n/a */
560 	wvalue = (u16)(addr&0x0000ffff);
561 	len = 1;
562 	data = val;
563 	ret = usbctrl_vendorreq(adapter, request, wvalue, index, &data, len, requesttype);
564 	return ret;
565 }
566 
usb_write16(struct adapter * adapter,u32 addr,u16 val)567 int usb_write16(struct adapter *adapter, u32 addr, u16 val)
568 {
569 	u8 request;
570 	u8 requesttype;
571 	u16 wvalue;
572 	u16 index;
573 	u16 len;
574 	__le32 data;
575 	int ret;
576 
577 
578 	request = 0x05;
579 	requesttype = 0x00;/* write_out */
580 	index = 0;/* n/a */
581 
582 	wvalue = (u16)(addr&0x0000ffff);
583 	len = 2;
584 
585 	data = cpu_to_le32(val & 0x0000ffff);
586 
587 	ret = usbctrl_vendorreq(adapter, request, wvalue, index, &data, len, requesttype);
588 
589 
590 	return ret;
591 }
592 
usb_write32(struct adapter * adapter,u32 addr,u32 val)593 int usb_write32(struct adapter *adapter, u32 addr, u32 val)
594 {
595 	u8 request;
596 	u8 requesttype;
597 	u16 wvalue;
598 	u16 index;
599 	u16 len;
600 	__le32 data;
601 	int ret;
602 
603 
604 	request = 0x05;
605 	requesttype = 0x00;/* write_out */
606 	index = 0;/* n/a */
607 
608 	wvalue = (u16)(addr&0x0000ffff);
609 	len = 4;
610 	data = cpu_to_le32(val);
611 
612 	ret = usbctrl_vendorreq(adapter, request, wvalue, index, &data, len, requesttype);
613 
614 
615 	return ret;
616 }
617 
usb_writeN(struct adapter * adapter,u32 addr,u32 length,u8 * pdata)618 int usb_writeN(struct adapter *adapter, u32 addr, u32 length, u8 *pdata)
619 {
620 	u8 request;
621 	u8 requesttype;
622 	u16 wvalue;
623 	u16 index;
624 	u16 len;
625 	u8 buf[VENDOR_CMD_MAX_DATA_LEN] = {0};
626 	int ret;
627 
628 
629 	request = 0x05;
630 	requesttype = 0x00;/* write_out */
631 	index = 0;/* n/a */
632 
633 	wvalue = (u16)(addr&0x0000ffff);
634 	len = length;
635 	 memcpy(buf, pdata, len);
636 
637 	ret = usbctrl_vendorreq(adapter, request, wvalue, index, buf, len, requesttype);
638 
639 
640 	return RTW_STATUS_CODE(ret);
641 }
642 
643 
644 
usb_write_port_complete(struct urb * purb,struct pt_regs * regs)645 static void usb_write_port_complete(struct urb *purb, struct pt_regs *regs)
646 {
647 	struct xmit_buf *pxmitbuf = (struct xmit_buf *)purb->context;
648 	struct adapter	*padapter = pxmitbuf->padapter;
649 	struct xmit_priv	*pxmitpriv = &padapter->xmitpriv;
650 
651 	switch (pxmitbuf->flags) {
652 	case VO_QUEUE_INX:
653 		pxmitpriv->voq_cnt--;
654 		break;
655 	case VI_QUEUE_INX:
656 		pxmitpriv->viq_cnt--;
657 		break;
658 	case BE_QUEUE_INX:
659 		pxmitpriv->beq_cnt--;
660 		break;
661 	case BK_QUEUE_INX:
662 		pxmitpriv->bkq_cnt--;
663 		break;
664 	case HIGH_QUEUE_INX:
665 #ifdef CONFIG_88EU_AP_MODE
666 		rtw_chk_hi_queue_cmd(padapter);
667 #endif
668 		break;
669 	default:
670 		break;
671 	}
672 
673 	if (padapter->bSurpriseRemoved || padapter->bDriverStopped ||
674 	    padapter->bWritePortCancel) {
675 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
676 			 ("usb_write_port_complete:bDriverStopped(%d) OR bSurpriseRemoved(%d)",
677 			 padapter->bDriverStopped, padapter->bSurpriseRemoved));
678 		DBG_88E("%s(): TX Warning! bDriverStopped(%d) OR bSurpriseRemoved(%d) bWritePortCancel(%d) pxmitbuf->ext_tag(%x)\n",
679 			__func__, padapter->bDriverStopped,
680 			padapter->bSurpriseRemoved, padapter->bReadPortCancel,
681 			pxmitbuf->ext_tag);
682 
683 		goto check_completion;
684 	}
685 
686 	if (purb->status) {
687 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_write_port_complete : purb->status(%d) != 0\n", purb->status));
688 		DBG_88E("###=> urb_write_port_complete status(%d)\n", purb->status);
689 		if ((purb->status == -EPIPE) || (purb->status == -EPROTO)) {
690 			sreset_set_wifi_error_status(padapter, USB_WRITE_PORT_FAIL);
691 		} else if (purb->status == -EINPROGRESS) {
692 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_write_port_complete: EINPROGESS\n"));
693 			goto check_completion;
694 		} else if (purb->status == -ENOENT) {
695 			DBG_88E("%s: -ENOENT\n", __func__);
696 			goto check_completion;
697 		} else if (purb->status == -ECONNRESET) {
698 			DBG_88E("%s: -ECONNRESET\n", __func__);
699 			goto check_completion;
700 		} else if (purb->status == -ESHUTDOWN) {
701 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_write_port_complete: ESHUTDOWN\n"));
702 			padapter->bDriverStopped = true;
703 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_write_port_complete:bDriverStopped = true\n"));
704 			goto check_completion;
705 		} else {
706 			padapter->bSurpriseRemoved = true;
707 			DBG_88E("bSurpriseRemoved = true\n");
708 			RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_write_port_complete:bSurpriseRemoved = true\n"));
709 
710 			goto check_completion;
711 		}
712 	}
713 
714 check_completion:
715 	rtw_sctx_done_err(&pxmitbuf->sctx,
716 			  purb->status ? RTW_SCTX_DONE_WRITE_PORT_ERR :
717 			  RTW_SCTX_DONE_SUCCESS);
718 
719 	rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
720 
721 	tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
722 }
723 
usb_write_port(struct adapter * padapter,u32 addr,u32 cnt,u8 * wmem)724 u32 usb_write_port(struct adapter *padapter, u32 addr, u32 cnt, u8 *wmem)
725 {
726 	unsigned long irqL;
727 	unsigned int pipe;
728 	int status;
729 	u32 ret = _FAIL;
730 	struct urb *purb = NULL;
731 	struct dvobj_priv	*pdvobj = adapter_to_dvobj(padapter);
732 	struct xmit_priv	*pxmitpriv = &padapter->xmitpriv;
733 	struct xmit_buf *pxmitbuf = (struct xmit_buf *)wmem;
734 	struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitbuf->priv_data;
735 	struct usb_device *pusbd = pdvobj->pusbdev;
736 
737 
738 	RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("+usb_write_port\n"));
739 
740 	if ((padapter->bDriverStopped) || (padapter->bSurpriseRemoved) ||
741 	    (padapter->pwrctrlpriv.pnp_bstop_trx)) {
742 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_,
743 			 ("usb_write_port:( padapter->bDriverStopped ||padapter->bSurpriseRemoved ||adapter->pwrctrlpriv.pnp_bstop_trx)!!!\n"));
744 		rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_TX_DENY);
745 		goto exit;
746 	}
747 
748 	spin_lock_irqsave(&pxmitpriv->lock, irqL);
749 
750 	switch (addr) {
751 	case VO_QUEUE_INX:
752 		pxmitpriv->voq_cnt++;
753 		pxmitbuf->flags = VO_QUEUE_INX;
754 		break;
755 	case VI_QUEUE_INX:
756 		pxmitpriv->viq_cnt++;
757 		pxmitbuf->flags = VI_QUEUE_INX;
758 		break;
759 	case BE_QUEUE_INX:
760 		pxmitpriv->beq_cnt++;
761 		pxmitbuf->flags = BE_QUEUE_INX;
762 		break;
763 	case BK_QUEUE_INX:
764 		pxmitpriv->bkq_cnt++;
765 		pxmitbuf->flags = BK_QUEUE_INX;
766 		break;
767 	case HIGH_QUEUE_INX:
768 		pxmitbuf->flags = HIGH_QUEUE_INX;
769 		break;
770 	default:
771 		pxmitbuf->flags = MGT_QUEUE_INX;
772 		break;
773 	}
774 
775 	spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
776 
777 	purb	= pxmitbuf->pxmit_urb[0];
778 
779 	/* translate DMA FIFO addr to pipehandle */
780 	pipe = ffaddr2pipehdl(pdvobj, addr);
781 
782 	usb_fill_bulk_urb(purb, pusbd, pipe,
783 			  pxmitframe->buf_addr, /*  pxmitbuf->pbuf */
784 			  cnt,
785 			  usb_write_port_complete,
786 			  pxmitbuf);/* context is pxmitbuf */
787 
788 	status = usb_submit_urb(purb, GFP_ATOMIC);
789 	if (status) {
790 		rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_WRITE_PORT_ERR);
791 		DBG_88E("usb_write_port, status =%d\n", status);
792 		RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("usb_write_port(): usb_submit_urb, status =%x\n", status));
793 
794 		switch (status) {
795 		case -ENODEV:
796 			padapter->bDriverStopped = true;
797 			break;
798 		default:
799 			break;
800 		}
801 		goto exit;
802 	}
803 
804 	ret = _SUCCESS;
805 
806 /*    We add the URB_ZERO_PACKET flag to urb so that the host will send the zero packet automatically. */
807 
808 	RT_TRACE(_module_hci_ops_os_c_, _drv_err_, ("-usb_write_port\n"));
809 
810 exit:
811 	if (ret != _SUCCESS)
812 		rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
813 	return ret;
814 }
815 
usb_write_port_cancel(struct adapter * padapter)816 void usb_write_port_cancel(struct adapter *padapter)
817 {
818 	int i, j;
819 	struct xmit_buf *pxmitbuf = (struct xmit_buf *)padapter->xmitpriv.pxmitbuf;
820 
821 	DBG_88E("%s\n", __func__);
822 
823 	padapter->bWritePortCancel = true;
824 
825 	for (i = 0; i < NR_XMITBUFF; i++) {
826 		for (j = 0; j < 8; j++) {
827 			if (pxmitbuf->pxmit_urb[j])
828 				usb_kill_urb(pxmitbuf->pxmit_urb[j]);
829 		}
830 		pxmitbuf++;
831 	}
832 
833 	pxmitbuf = (struct xmit_buf *)padapter->xmitpriv.pxmit_extbuf;
834 	for (i = 0; i < NR_XMIT_EXTBUFF; i++) {
835 		for (j = 0; j < 8; j++) {
836 			if (pxmitbuf->pxmit_urb[j])
837 				usb_kill_urb(pxmitbuf->pxmit_urb[j]);
838 		}
839 		pxmitbuf++;
840 	}
841 }
842 
rtl8188eu_recv_tasklet(void * priv)843 void rtl8188eu_recv_tasklet(void *priv)
844 {
845 	struct sk_buff *pskb;
846 	struct adapter *adapt = (struct adapter *)priv;
847 	struct recv_priv *precvpriv = &adapt->recvpriv;
848 
849 	while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) {
850 		if ((adapt->bDriverStopped) || (adapt->bSurpriseRemoved)) {
851 			DBG_88E("recv_tasklet => bDriverStopped or bSurpriseRemoved\n");
852 			dev_kfree_skb_any(pskb);
853 			break;
854 		}
855 		recvbuf2recvframe(adapt, pskb);
856 		skb_reset_tail_pointer(pskb);
857 		pskb->len = 0;
858 		skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
859 	}
860 }
861 
rtl8188eu_xmit_tasklet(void * priv)862 void rtl8188eu_xmit_tasklet(void *priv)
863 {
864 	int ret = false;
865 	struct adapter *adapt = (struct adapter *)priv;
866 	struct xmit_priv *pxmitpriv = &adapt->xmitpriv;
867 
868 	if (check_fwstate(&adapt->mlmepriv, _FW_UNDER_SURVEY))
869 		return;
870 
871 	while (1) {
872 		if ((adapt->bDriverStopped) ||
873 		    (adapt->bSurpriseRemoved) ||
874 		    (adapt->bWritePortCancel)) {
875 			DBG_88E("xmit_tasklet => bDriverStopped or bSurpriseRemoved or bWritePortCancel\n");
876 			break;
877 		}
878 
879 		ret = rtl8188eu_xmitframe_complete(adapt, pxmitpriv, NULL);
880 
881 		if (!ret)
882 			break;
883 	}
884 }
885