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