1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 #include "../include/osdep_service.h"
5 #include "../include/drv_types.h"
6 #include "../include/osdep_intf.h"
7 #include "../include/usb_ops.h"
8 #include "../include/rtl8188e_hal.h"
9 
usb_read(struct intf_hdl * intf,u16 value,void * data,u8 size)10 static int usb_read(struct intf_hdl *intf, u16 value, void *data, u8 size)
11 {
12 	struct adapter *adapt = intf->padapter;
13 	struct dvobj_priv *dvobjpriv = adapter_to_dvobj(adapt);
14 	struct usb_device *udev = dvobjpriv->pusbdev;
15 	int status;
16 	u8 io_buf[4];
17 
18 	if (adapt->bSurpriseRemoved)
19 		return -EPERM;
20 
21 	status = usb_control_msg_recv(udev, 0, REALTEK_USB_VENQT_CMD_REQ,
22 				      REALTEK_USB_VENQT_READ, value,
23 				      REALTEK_USB_VENQT_CMD_IDX, io_buf,
24 				      size, RTW_USB_CONTROL_MSG_TIMEOUT,
25 				      GFP_KERNEL);
26 
27 	if (status == -ESHUTDOWN ||
28 	    status == -ENODEV ||
29 	    status == -ENOENT) {
30 		/*
31 		 * device or controller has been disabled due to
32 		 * some problem that could not be worked around,
33 		 * device or bus doesn’t exist, endpoint does not
34 		 * exist or is not enabled.
35 		 */
36 		adapt->bSurpriseRemoved = true;
37 		return status;
38 	}
39 
40 	if (status < 0) {
41 		if (rtw_inc_and_chk_continual_urb_error(dvobjpriv))
42 			adapt->bSurpriseRemoved = true;
43 
44 		return status;
45 	}
46 
47 	rtw_reset_continual_urb_error(dvobjpriv);
48 	memcpy(data, io_buf, size);
49 
50 	return status;
51 }
52 
usb_write(struct intf_hdl * intf,u16 value,void * data,u8 size)53 static int usb_write(struct intf_hdl *intf, u16 value, void *data, u8 size)
54 {
55 	struct adapter *adapt = intf->padapter;
56 	struct dvobj_priv *dvobjpriv = adapter_to_dvobj(adapt);
57 	struct usb_device *udev = dvobjpriv->pusbdev;
58 	int status;
59 	u8 io_buf[VENDOR_CMD_MAX_DATA_LEN];
60 
61 	if (adapt->bSurpriseRemoved)
62 		return -EPERM;
63 
64 	memcpy(io_buf, data, size);
65 	status = usb_control_msg_send(udev, 0, REALTEK_USB_VENQT_CMD_REQ,
66 				      REALTEK_USB_VENQT_WRITE, value,
67 				      REALTEK_USB_VENQT_CMD_IDX, io_buf,
68 				      size, RTW_USB_CONTROL_MSG_TIMEOUT,
69 				      GFP_KERNEL);
70 
71 	if (status == -ESHUTDOWN ||
72 	    status == -ENODEV ||
73 	    status == -ENOENT) {
74 		/*
75 		 * device or controller has been disabled due to
76 		 * some problem that could not be worked around,
77 		 * device or bus doesn’t exist, endpoint does not
78 		 * exist or is not enabled.
79 		 */
80 		adapt->bSurpriseRemoved = true;
81 		return status;
82 	}
83 
84 	if (status < 0) {
85 		if (rtw_inc_and_chk_continual_urb_error(dvobjpriv))
86 			adapt->bSurpriseRemoved = true;
87 
88 		return status;
89 	}
90 
91 	rtw_reset_continual_urb_error(dvobjpriv);
92 
93 	return status;
94 }
95 
rtw_read8(struct adapter * adapter,u32 addr,u8 * data)96 int __must_check rtw_read8(struct adapter *adapter, u32 addr, u8 *data)
97 {
98 	struct io_priv *io_priv = &adapter->iopriv;
99 	struct intf_hdl *intf = &io_priv->intf;
100 	u16 value = addr & 0xffff;
101 
102 	return usb_read(intf, value, data, 1);
103 }
104 
rtw_read16(struct adapter * adapter,u32 addr,u16 * data)105 int __must_check rtw_read16(struct adapter *adapter, u32 addr, u16 *data)
106 {
107 	struct io_priv *io_priv = &adapter->iopriv;
108 	struct intf_hdl *intf = &io_priv->intf;
109 	u16 value = addr & 0xffff;
110 	__le16 le_data;
111 	int res;
112 
113 	res = usb_read(intf, value, &le_data, 2);
114 	if (res)
115 		return res;
116 
117 	*data = le16_to_cpu(le_data);
118 
119 	return 0;
120 }
121 
rtw_read32(struct adapter * adapter,u32 addr,u32 * data)122 int __must_check rtw_read32(struct adapter *adapter, u32 addr, u32 *data)
123 {
124 	struct io_priv *io_priv = &adapter->iopriv;
125 	struct intf_hdl *intf = &io_priv->intf;
126 	u16 value = addr & 0xffff;
127 	__le32 le_data;
128 	int res;
129 
130 	res = usb_read(intf, value, &le_data, 4);
131 	if (res)
132 		return res;
133 
134 	*data = le32_to_cpu(le_data);
135 
136 	return 0;
137 }
138 
rtw_write8(struct adapter * adapter,u32 addr,u8 val)139 int rtw_write8(struct adapter *adapter, u32 addr, u8 val)
140 {
141 	struct io_priv *io_priv = &adapter->iopriv;
142 	struct intf_hdl *intf = &io_priv->intf;
143 	u16 value = addr & 0xffff;
144 	int ret;
145 
146 	ret = usb_write(intf, value, &val, 1);
147 
148 	return RTW_STATUS_CODE(ret);
149 }
150 
rtw_write16(struct adapter * adapter,u32 addr,u16 val)151 int rtw_write16(struct adapter *adapter, u32 addr, u16 val)
152 {
153 	struct io_priv *io_priv = &adapter->iopriv;
154 	struct intf_hdl *intf = &io_priv->intf;
155 	u16 value = addr & 0xffff;
156 	__le16 data = cpu_to_le16(val);
157 	int ret;
158 
159 	ret = usb_write(intf, value, &data, 2);
160 
161 	return RTW_STATUS_CODE(ret);
162 }
163 
rtw_write32(struct adapter * adapter,u32 addr,u32 val)164 int rtw_write32(struct adapter *adapter, u32 addr, u32 val)
165 {
166 	struct io_priv *io_priv = &adapter->iopriv;
167 	struct intf_hdl *intf = &io_priv->intf;
168 	u16 value = addr & 0xffff;
169 	__le32 data = cpu_to_le32(val);
170 	int ret;
171 
172 	ret = usb_write(intf, value, &data, 4);
173 
174 	return RTW_STATUS_CODE(ret);
175 }
176 
rtw_writeN(struct adapter * adapter,u32 addr,u32 length,u8 * data)177 int rtw_writeN(struct adapter *adapter, u32 addr, u32 length, u8 *data)
178 {
179 	struct io_priv *io_priv = &adapter->iopriv;
180 	struct intf_hdl *intf = &io_priv->intf;
181 	u16 value = addr & 0xffff;
182 	int ret;
183 
184 	if (length > VENDOR_CMD_MAX_DATA_LEN)
185 		return _FAIL;
186 
187 	ret = usb_write(intf, value, data, length);
188 
189 	return RTW_STATUS_CODE(ret);
190 }
191 
handle_txrpt_ccx_88e(struct adapter * adapter,u8 * buf)192 static void handle_txrpt_ccx_88e(struct adapter *adapter, u8 *buf)
193 {
194 	struct txrpt_ccx_88e *txrpt_ccx = (struct txrpt_ccx_88e *)buf;
195 
196 	if (txrpt_ccx->int_ccx) {
197 		if (txrpt_ccx->pkt_ok)
198 			rtw_ack_tx_done(&adapter->xmitpriv,
199 					RTW_SCTX_DONE_SUCCESS);
200 		else
201 			rtw_ack_tx_done(&adapter->xmitpriv,
202 					RTW_SCTX_DONE_CCX_PKT_FAIL);
203 	}
204 }
205 
recvbuf2recvframe(struct adapter * adapt,struct sk_buff * pskb)206 static int recvbuf2recvframe(struct adapter *adapt, struct sk_buff *pskb)
207 {
208 	u8	*pbuf;
209 	u8	shift_sz = 0;
210 	u16	pkt_cnt;
211 	u32	pkt_offset, skb_len, alloc_sz;
212 	s32	transfer_len;
213 	struct recv_stat	*prxstat;
214 	struct phy_stat	*pphy_status = NULL;
215 	struct sk_buff *pkt_copy = NULL;
216 	struct recv_frame	*precvframe = NULL;
217 	struct rx_pkt_attrib	*pattrib = NULL;
218 	struct hal_data_8188e *haldata = &adapt->haldata;
219 	struct recv_priv	*precvpriv = &adapt->recvpriv;
220 	struct __queue *pfree_recv_queue = &precvpriv->free_recv_queue;
221 
222 	transfer_len = (s32)pskb->len;
223 	pbuf = pskb->data;
224 
225 	prxstat = (struct recv_stat *)pbuf;
226 	pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
227 
228 	do {
229 		prxstat = (struct recv_stat *)pbuf;
230 
231 		precvframe = rtw_alloc_recvframe(pfree_recv_queue);
232 		if (!precvframe)
233 			goto _exit_recvbuf2recvframe;
234 
235 		INIT_LIST_HEAD(&precvframe->list);
236 		precvframe->precvbuf = NULL;	/* can't access the precvbuf for new arch. */
237 		precvframe->len = 0;
238 
239 		update_recvframe_attrib_88e(precvframe, prxstat);
240 
241 		pattrib = &precvframe->attrib;
242 
243 		if ((pattrib->crc_err) || (pattrib->icv_err)) {
244 			rtw_free_recvframe(precvframe, pfree_recv_queue);
245 			goto _exit_recvbuf2recvframe;
246 		}
247 
248 		if ((pattrib->physt) && (pattrib->pkt_rpt_type == NORMAL_RX))
249 			pphy_status = (struct phy_stat *)(pbuf + RXDESC_OFFSET);
250 
251 		pkt_offset = RXDESC_SIZE + pattrib->drvinfo_sz + pattrib->shift_sz + pattrib->pkt_len;
252 
253 		if ((pattrib->pkt_len <= 0) || (pkt_offset > transfer_len)) {
254 			rtw_free_recvframe(precvframe, pfree_recv_queue);
255 			goto _exit_recvbuf2recvframe;
256 		}
257 
258 		/*	Modified by Albert 20101213 */
259 		/*	For 8 bytes IP header alignment. */
260 		if (pattrib->qos)	/*	Qos data, wireless lan header length is 26 */
261 			shift_sz = 6;
262 		else
263 			shift_sz = 0;
264 
265 		skb_len = pattrib->pkt_len;
266 
267 		/*  for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */
268 		/*  modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */
269 		if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
270 			if (skb_len <= 1650)
271 				alloc_sz = 1664;
272 			else
273 				alloc_sz = skb_len + 14;
274 		} else {
275 			alloc_sz = skb_len;
276 			/*	6 is for IP header 8 bytes alignment in QoS packet case. */
277 			/*	8 is for skb->data 4 bytes alignment. */
278 			alloc_sz += 14;
279 		}
280 
281 		pkt_copy = netdev_alloc_skb(adapt->pnetdev, alloc_sz);
282 		if (pkt_copy) {
283 			precvframe->pkt = pkt_copy;
284 			precvframe->rx_head = pkt_copy->data;
285 			precvframe->rx_end = pkt_copy->data + alloc_sz;
286 			skb_reserve(pkt_copy, 8 - ((size_t)(pkt_copy->data) & 7));/* force pkt_copy->data at 8-byte alignment address */
287 			skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */
288 			memcpy(pkt_copy->data, (pbuf + pattrib->drvinfo_sz + RXDESC_SIZE), skb_len);
289 			precvframe->rx_tail = pkt_copy->data;
290 			precvframe->rx_data = pkt_copy->data;
291 		} else {
292 			if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
293 				rtw_free_recvframe(precvframe, pfree_recv_queue);
294 				goto _exit_recvbuf2recvframe;
295 			}
296 			precvframe->pkt = skb_clone(pskb, GFP_ATOMIC);
297 			if (precvframe->pkt) {
298 				precvframe->rx_tail = pbuf + pattrib->drvinfo_sz + RXDESC_SIZE;
299 				precvframe->rx_head = precvframe->rx_tail;
300 				precvframe->rx_data = precvframe->rx_tail;
301 				precvframe->rx_end =  pbuf + pattrib->drvinfo_sz + RXDESC_SIZE + alloc_sz;
302 			} else {
303 				rtw_free_recvframe(precvframe, pfree_recv_queue);
304 				goto _exit_recvbuf2recvframe;
305 			}
306 		}
307 
308 		recvframe_put(precvframe, skb_len);
309 
310 		pkt_offset = (u16)round_up(pkt_offset, 128);
311 
312 		if (pattrib->pkt_rpt_type == NORMAL_RX) { /* Normal rx packet */
313 			if (pattrib->physt)
314 				update_recvframe_phyinfo_88e(precvframe, (struct phy_stat *)pphy_status);
315 			rtw_recv_entry(precvframe);
316 		} else {
317 			/* enqueue recvframe to txrtp queue */
318 			if (pattrib->pkt_rpt_type == TX_REPORT1) {
319 				/* CCX-TXRPT ack for xmit mgmt frames. */
320 				handle_txrpt_ccx_88e(adapt, precvframe->rx_data);
321 			} else if (pattrib->pkt_rpt_type == TX_REPORT2) {
322 				ODM_RA_TxRPT2Handle_8188E(
323 							&haldata->odmpriv,
324 							precvframe->rx_data,
325 							pattrib->pkt_len,
326 							pattrib->MacIDValidEntry[0],
327 							pattrib->MacIDValidEntry[1]
328 							);
329 			}
330 			rtw_free_recvframe(precvframe, pfree_recv_queue);
331 		}
332 		pkt_cnt--;
333 		transfer_len -= pkt_offset;
334 		pbuf += pkt_offset;
335 		precvframe = NULL;
336 		pkt_copy = NULL;
337 
338 		if (transfer_len > 0 && pkt_cnt == 0)
339 			pkt_cnt = (le32_to_cpu(prxstat->rxdw2) >> 16) & 0xff;
340 
341 	} while ((transfer_len > 0) && (pkt_cnt > 0));
342 
343 _exit_recvbuf2recvframe:
344 
345 	return _SUCCESS;
346 }
347 
rtl8188eu_recv_tasklet(unsigned long priv)348 void rtl8188eu_recv_tasklet(unsigned long priv)
349 {
350 	struct sk_buff *pskb;
351 	struct adapter *adapt = (struct adapter *)priv;
352 	struct recv_priv *precvpriv = &adapt->recvpriv;
353 
354 	while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) {
355 		if ((adapt->bDriverStopped) || (adapt->bSurpriseRemoved)) {
356 			dev_kfree_skb_any(pskb);
357 			break;
358 		}
359 		recvbuf2recvframe(adapt, pskb);
360 		skb_reset_tail_pointer(pskb);
361 		pskb->len = 0;
362 		skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb);
363 	}
364 }
365 
usb_read_port_complete(struct urb * purb,struct pt_regs * regs)366 static void usb_read_port_complete(struct urb *purb, struct pt_regs *regs)
367 {
368 	struct recv_buf	*precvbuf = (struct recv_buf *)purb->context;
369 	struct adapter	*adapt = (struct adapter *)precvbuf->adapter;
370 	struct recv_priv *precvpriv = &adapt->recvpriv;
371 
372 	precvpriv->rx_pending_cnt--;
373 
374 	if (adapt->bSurpriseRemoved || adapt->bDriverStopped || adapt->bReadPortCancel) {
375 		precvbuf->reuse = true;
376 		return;
377 	}
378 
379 	if (purb->status == 0) { /* SUCCESS */
380 		if ((purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)) {
381 			precvbuf->reuse = true;
382 			rtw_read_port(adapt, (unsigned char *)precvbuf);
383 		} else {
384 			rtw_reset_continual_urb_error(adapter_to_dvobj(adapt));
385 
386 			skb_put(precvbuf->pskb, purb->actual_length);
387 			skb_queue_tail(&precvpriv->rx_skb_queue, precvbuf->pskb);
388 
389 			if (skb_queue_len(&precvpriv->rx_skb_queue) <= 1)
390 				tasklet_schedule(&precvpriv->recv_tasklet);
391 
392 			precvbuf->pskb = NULL;
393 			precvbuf->reuse = false;
394 			rtw_read_port(adapt, (unsigned char *)precvbuf);
395 		}
396 	} else {
397 		skb_put(precvbuf->pskb, purb->actual_length);
398 		precvbuf->pskb = NULL;
399 
400 		if (rtw_inc_and_chk_continual_urb_error(adapter_to_dvobj(adapt)))
401 			adapt->bSurpriseRemoved = true;
402 
403 		switch (purb->status) {
404 		case -EINVAL:
405 		case -EPIPE:
406 		case -ENODEV:
407 		case -ESHUTDOWN:
408 		case -ENOENT:
409 			adapt->bDriverStopped = true;
410 			break;
411 		case -EPROTO:
412 		case -EOVERFLOW:
413 			precvbuf->reuse = true;
414 			rtw_read_port(adapt, (unsigned char *)precvbuf);
415 			break;
416 		case -EINPROGRESS:
417 			break;
418 		default:
419 			break;
420 		}
421 	}
422 }
423 
rtw_read_port(struct adapter * adapter,u8 * rmem)424 u32 rtw_read_port(struct adapter *adapter, u8 *rmem)
425 {
426 	struct urb *purb = NULL;
427 	struct recv_buf	*precvbuf = (struct recv_buf *)rmem;
428 	struct dvobj_priv	*pdvobj = adapter_to_dvobj(adapter);
429 	struct recv_priv	*precvpriv = &adapter->recvpriv;
430 	struct usb_device	*pusbd = pdvobj->pusbdev;
431 	int err;
432 	unsigned int pipe;
433 	size_t tmpaddr = 0;
434 	size_t alignment = 0;
435 	u32 ret = _SUCCESS;
436 
437 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
438 		return _FAIL;
439 
440 	if (!precvbuf)
441 		return _FAIL;
442 
443 	if (!precvbuf->reuse || !precvbuf->pskb) {
444 		precvbuf->pskb = skb_dequeue(&precvpriv->free_recv_skb_queue);
445 		if (precvbuf->pskb)
446 			precvbuf->reuse = true;
447 	}
448 
449 	/* re-assign for linux based on skb */
450 	if (!precvbuf->reuse || !precvbuf->pskb) {
451 		precvbuf->pskb = netdev_alloc_skb(adapter->pnetdev, MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
452 		if (!precvbuf->pskb)
453 			return _FAIL;
454 
455 		tmpaddr = (size_t)precvbuf->pskb->data;
456 		alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
457 		skb_reserve(precvbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
458 	} else { /* reuse skb */
459 		precvbuf->reuse = false;
460 	}
461 
462 	precvpriv->rx_pending_cnt++;
463 
464 	purb = precvbuf->purb;
465 
466 	/* translate DMA FIFO addr to pipehandle */
467 	pipe = usb_rcvbulkpipe(pusbd, pdvobj->RtInPipe);
468 
469 	usb_fill_bulk_urb(purb, pusbd, pipe,
470 			  precvbuf->pskb->data,
471 			  MAX_RECVBUF_SZ,
472 			  usb_read_port_complete,
473 			  precvbuf);/* context is precvbuf */
474 
475 	err = usb_submit_urb(purb, GFP_ATOMIC);
476 	if ((err) && (err != (-EPERM)))
477 		ret = _FAIL;
478 
479 	return ret;
480 }
481 
rtl8188eu_xmit_tasklet(unsigned long priv)482 void rtl8188eu_xmit_tasklet(unsigned long priv)
483 {
484 	int ret = false;
485 	struct adapter *adapt = (struct adapter *)priv;
486 	struct xmit_priv *pxmitpriv = &adapt->xmitpriv;
487 
488 	if (check_fwstate(&adapt->mlmepriv, _FW_UNDER_SURVEY))
489 		return;
490 
491 	while (1) {
492 		if ((adapt->bDriverStopped) ||
493 		    (adapt->bSurpriseRemoved) ||
494 		    (adapt->bWritePortCancel))
495 			break;
496 
497 		ret = rtl8188eu_xmitframe_complete(adapt, pxmitpriv, NULL);
498 
499 		if (!ret)
500 			break;
501 	}
502 }
503