1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #define pr_fmt(fmt) "R8188EU: " fmt
9 #include <osdep_service.h>
10 #include <drv_types.h>
11 #include <recv_osdep.h>
12 #include <xmit_osdep.h>
13 #include <hal_intf.h>
14 #include <linux/usb.h>
15 #include <linux/vmalloc.h>
16 #include <mon.h>
17 #include <osdep_intf.h>
18 
19 #include <usb_ops_linux.h>
20 #include <rtw_ioctl.h>
21 
22 #include "rtl8188e_hal.h"
23 
24 #define USB_VENDER_ID_REALTEK		0x0bda
25 
26 /* DID_USB_v916_20130116 */
27 static const struct usb_device_id rtw_usb_id_tbl[] = {
28 	/*=== Realtek demoboard ===*/
29 	{USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8179)}, /* 8188EUS */
30 	{USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0179)}, /* 8188ETV */
31 	/*=== Customer ID ===*/
32 	/****** 8188EUS ********/
33 	{USB_DEVICE(0x056e, 0x4008)}, /* Elecom WDC-150SU2M */
34 	{USB_DEVICE(0x07b8, 0x8179)}, /* Abocom - Abocom */
35 	{USB_DEVICE(0x2001, 0x330F)}, /* DLink DWA-125 REV D1 */
36 	{USB_DEVICE(0x2001, 0x3310)}, /* Dlink DWA-123 REV D1 */
37 	{USB_DEVICE(0x2001, 0x3311)}, /* DLink GO-USB-N150 REV B1 */
38 	{USB_DEVICE(0x2357, 0x010c)}, /* TP-Link TL-WN722N v2 */
39 	{USB_DEVICE(0x0df6, 0x0076)}, /* Sitecom N150 v2 */
40 	{USB_DEVICE(USB_VENDER_ID_REALTEK, 0xffef)}, /* Rosewill RNX-N150NUB */
41 	{}	/* Terminating entry */
42 };
43 
44 MODULE_DEVICE_TABLE(usb, rtw_usb_id_tbl);
45 
usb_dvobj_init(struct usb_interface * usb_intf)46 static struct dvobj_priv *usb_dvobj_init(struct usb_interface *usb_intf)
47 {
48 	int	i;
49 	struct dvobj_priv *pdvobjpriv;
50 	struct usb_host_config		*phost_conf;
51 	struct usb_config_descriptor	*pconf_desc;
52 	struct usb_host_interface	*phost_iface;
53 	struct usb_interface_descriptor	*piface_desc;
54 	struct usb_endpoint_descriptor	*pendp_desc;
55 	struct usb_device	*pusbd;
56 
57 	pdvobjpriv = kzalloc(sizeof(*pdvobjpriv), GFP_KERNEL);
58 	if (!pdvobjpriv)
59 		return NULL;
60 
61 	pdvobjpriv->pusbintf = usb_intf;
62 	pusbd = interface_to_usbdev(usb_intf);
63 	pdvobjpriv->pusbdev = pusbd;
64 	usb_set_intfdata(usb_intf, pdvobjpriv);
65 
66 	pdvobjpriv->RtNumInPipes = 0;
67 	pdvobjpriv->RtNumOutPipes = 0;
68 
69 	phost_conf = pusbd->actconfig;
70 	pconf_desc = &phost_conf->desc;
71 
72 	phost_iface = &usb_intf->altsetting[0];
73 	piface_desc = &phost_iface->desc;
74 
75 	pdvobjpriv->NumInterfaces = pconf_desc->bNumInterfaces;
76 	pdvobjpriv->InterfaceNumber = piface_desc->bInterfaceNumber;
77 
78 	for (i = 0; i < piface_desc->bNumEndpoints; i++) {
79 		int ep_num;
80 
81 		pendp_desc = &phost_iface->endpoint[i].desc;
82 
83 		ep_num = usb_endpoint_num(pendp_desc);
84 
85 		if (usb_endpoint_is_bulk_in(pendp_desc)) {
86 			pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = ep_num;
87 			pdvobjpriv->RtNumInPipes++;
88 		} else if (usb_endpoint_is_int_in(pendp_desc)) {
89 			pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = ep_num;
90 			pdvobjpriv->RtNumInPipes++;
91 		} else if (usb_endpoint_is_bulk_out(pendp_desc)) {
92 			pdvobjpriv->RtOutPipe[pdvobjpriv->RtNumOutPipes] =
93 				ep_num;
94 			pdvobjpriv->RtNumOutPipes++;
95 		}
96 	}
97 
98 	if (pusbd->speed == USB_SPEED_HIGH)
99 		pdvobjpriv->ishighspeed = true;
100 	else
101 		pdvobjpriv->ishighspeed = false;
102 
103 	mutex_init(&pdvobjpriv->usb_vendor_req_mutex);
104 	usb_get_dev(pusbd);
105 
106 	return pdvobjpriv;
107 }
108 
usb_dvobj_deinit(struct usb_interface * usb_intf)109 static void usb_dvobj_deinit(struct usb_interface *usb_intf)
110 {
111 	struct dvobj_priv *dvobj = usb_get_intfdata(usb_intf);
112 
113 	usb_set_intfdata(usb_intf, NULL);
114 	if (dvobj) {
115 		/* Modify condition for 92DU DMDP 2010.11.18, by Thomas */
116 		if ((dvobj->NumInterfaces != 2 &&
117 		    dvobj->NumInterfaces != 3) ||
118 		    (dvobj->InterfaceNumber == 1)) {
119 			if (interface_to_usbdev(usb_intf)->state !=
120 			    USB_STATE_NOTATTACHED) {
121 				/* If we didn't unplug usb dongle and
122 				 * remove/insert module, driver fails
123 				 * on sitesurvey for the first time when
124 				 * device is up . Reset usb port for sitesurvey
125 				 * fail issue. */
126 				pr_debug("usb attached..., try to reset usb device\n");
127 				usb_reset_device(interface_to_usbdev(usb_intf));
128 			}
129 		}
130 
131 		mutex_destroy(&dvobj->usb_vendor_req_mutex);
132 		kfree(dvobj);
133 	}
134 
135 	usb_put_dev(interface_to_usbdev(usb_intf));
136 }
137 
usb_intf_stop(struct adapter * padapter)138 void usb_intf_stop(struct adapter *padapter)
139 {
140 	RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+usb_intf_stop\n"));
141 
142 	/* disable_hw_interrupt */
143 	if (!padapter->bSurpriseRemoved) {
144 		/* device still exists, so driver can do i/o operation */
145 		/* TODO: */
146 		RT_TRACE(_module_hci_intfs_c_, _drv_err_,
147 			 ("SurpriseRemoved == false\n"));
148 	}
149 
150 	/* cancel in irp */
151 	rtw_hal_inirp_deinit(padapter);
152 
153 	/* cancel out irp */
154 	usb_write_port_cancel(padapter);
155 
156 	/* todo:cancel other irps */
157 
158 	RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("-usb_intf_stop\n"));
159 }
160 
rtw_dev_unload(struct adapter * padapter)161 static void rtw_dev_unload(struct adapter *padapter)
162 {
163 	RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+rtw_dev_unload\n"));
164 
165 	if (padapter->bup) {
166 		pr_debug("===> rtw_dev_unload\n");
167 		padapter->bDriverStopped = true;
168 		if (padapter->xmitpriv.ack_tx)
169 			rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP);
170 		/* s3. */
171 		usb_intf_stop(padapter);
172 		/* s4. */
173 		if (!padapter->pwrctrlpriv.bInternalAutoSuspend)
174 			rtw_stop_drv_threads(padapter);
175 
176 		/* s5. */
177 		if (!padapter->bSurpriseRemoved) {
178 			rtw_hal_deinit(padapter);
179 			padapter->bSurpriseRemoved = true;
180 		}
181 
182 		padapter->bup = false;
183 	} else {
184 		RT_TRACE(_module_hci_intfs_c_, _drv_err_,
185 			 ("r871x_dev_unload():padapter->bup == false\n"));
186 	}
187 
188 	pr_debug("<=== rtw_dev_unload\n");
189 
190 	RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("-rtw_dev_unload\n"));
191 }
192 
rtw_suspend(struct usb_interface * pusb_intf,pm_message_t message)193 static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message)
194 {
195 	struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
196 	struct adapter *padapter = dvobj->if1;
197 	struct net_device *pnetdev = padapter->pnetdev;
198 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
199 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
200 	unsigned long start_time = jiffies;
201 
202 	pr_debug("==> %s (%s:%d)\n", __func__, current->comm, current->pid);
203 
204 	if ((!padapter->bup) || (padapter->bDriverStopped) ||
205 	    (padapter->bSurpriseRemoved)) {
206 		pr_debug("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
207 			padapter->bup, padapter->bDriverStopped,
208 			padapter->bSurpriseRemoved);
209 		goto exit;
210 	}
211 
212 	pwrpriv->bInSuspend = true;
213 	rtw_cancel_all_timer(padapter);
214 	LeaveAllPowerSaveMode(padapter);
215 
216 	mutex_lock(&pwrpriv->mutex_lock);
217 	/* s1. */
218 	if (pnetdev) {
219 		netif_carrier_off(pnetdev);
220 		netif_tx_stop_all_queues(pnetdev);
221 	}
222 
223 	/* s2. */
224 	rtw_disassoc_cmd(padapter, 0, false);
225 
226 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
227 	    check_fwstate(pmlmepriv, _FW_LINKED)) {
228 		pr_debug("%s:%d %s(%pM), length:%d assoc_ssid.length:%d\n",
229 			__func__, __LINE__,
230 			pmlmepriv->cur_network.network.Ssid.Ssid,
231 			pmlmepriv->cur_network.network.MacAddress,
232 			pmlmepriv->cur_network.network.Ssid.SsidLength,
233 			pmlmepriv->assoc_ssid.SsidLength);
234 
235 		pmlmepriv->to_roaming = 1;
236 	}
237 	/* s2-2.  indicate disconnect to os */
238 	rtw_indicate_disconnect(padapter);
239 	/* s2-3. */
240 	rtw_free_assoc_resources(padapter);
241 	/* s2-4. */
242 	rtw_free_network_queue(padapter, true);
243 
244 	rtw_dev_unload(padapter);
245 	mutex_unlock(&pwrpriv->mutex_lock);
246 
247 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
248 		rtw_indicate_scan_done(padapter, 1);
249 
250 	if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING))
251 		rtw_indicate_disconnect(padapter);
252 
253 exit:
254 	pr_debug("<===  %s .............. in %dms\n", __func__,
255 		 jiffies_to_msecs(jiffies - start_time));
256 
257 	return 0;
258 }
259 
rtw_resume_process(struct adapter * padapter)260 static int rtw_resume_process(struct adapter *padapter)
261 {
262 	struct net_device *pnetdev;
263 	struct pwrctrl_priv *pwrpriv = NULL;
264 	int ret = -1;
265 	unsigned long start_time = jiffies;
266 
267 	pr_debug("==> %s (%s:%d)\n", __func__, current->comm, current->pid);
268 
269 	if (padapter) {
270 		pnetdev = padapter->pnetdev;
271 		pwrpriv = &padapter->pwrctrlpriv;
272 	} else {
273 		goto exit;
274 	}
275 
276 	mutex_lock(&pwrpriv->mutex_lock);
277 	rtw_reset_drv_sw(padapter);
278 	pwrpriv->bkeepfwalive = false;
279 
280 	pr_debug("bkeepfwalive(%x)\n", pwrpriv->bkeepfwalive);
281 	if (netdev_open(pnetdev) != 0) {
282 		mutex_unlock(&pwrpriv->mutex_lock);
283 		goto exit;
284 	}
285 
286 	netif_device_attach(pnetdev);
287 	netif_carrier_on(pnetdev);
288 
289 	mutex_unlock(&pwrpriv->mutex_lock);
290 
291 	rtw_roaming(padapter, NULL);
292 
293 	ret = 0;
294 exit:
295 	if (pwrpriv)
296 		pwrpriv->bInSuspend = false;
297 	pr_debug("<===  %s return %d.............. in %dms\n", __func__,
298 		ret, jiffies_to_msecs(jiffies - start_time));
299 
300 	return ret;
301 }
302 
rtw_resume(struct usb_interface * pusb_intf)303 static int rtw_resume(struct usb_interface *pusb_intf)
304 {
305 	struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
306 	struct adapter *padapter = dvobj->if1;
307 
308 	return rtw_resume_process(padapter);
309 }
310 
311 /*
312  * drv_init() - a device potentially for us
313  *
314  * notes: drv_init() is called when the bus driver has located
315  * a card for us to support.
316  *        We accept the new device by returning 0.
317  */
318 
rtw_usb_if1_init(struct dvobj_priv * dvobj,struct usb_interface * pusb_intf,const struct usb_device_id * pdid)319 static struct adapter *rtw_usb_if1_init(struct dvobj_priv *dvobj,
320 	struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
321 {
322 	struct adapter *padapter = NULL;
323 	struct net_device *pnetdev = NULL;
324 	struct net_device *pmondev;
325 	int status = _FAIL;
326 
327 	padapter = vzalloc(sizeof(*padapter));
328 	if (!padapter)
329 		goto exit;
330 	padapter->dvobj = dvobj;
331 	dvobj->if1 = padapter;
332 
333 	padapter->bDriverStopped = true;
334 	mutex_init(&padapter->hw_init_mutex);
335 
336 	pnetdev = rtw_init_netdev(padapter);
337 	if (!pnetdev)
338 		goto free_adapter;
339 	SET_NETDEV_DEV(pnetdev, dvobj_to_dev(dvobj));
340 	padapter = rtw_netdev_priv(pnetdev);
341 
342 	if (padapter->registrypriv.monitor_enable) {
343 		pmondev = rtl88eu_mon_init();
344 		if (!pmondev)
345 			netdev_warn(pnetdev, "Failed to initialize monitor interface");
346 		padapter->pmondev = pmondev;
347 	}
348 
349 	padapter->HalData = kzalloc(sizeof(struct hal_data_8188e), GFP_KERNEL);
350 	if (!padapter->HalData)
351 		DBG_88E("cant not alloc memory for HAL DATA\n");
352 
353 	/* step read_chip_version */
354 	rtw_hal_read_chip_version(padapter);
355 
356 	/* step usb endpoint mapping */
357 	rtw_hal_chip_configure(padapter);
358 
359 	/* step read efuse/eeprom data and get mac_addr */
360 	rtw_hal_read_chip_info(padapter);
361 
362 	/* step 5. */
363 	if (rtw_init_drv_sw(padapter) == _FAIL) {
364 		RT_TRACE(_module_hci_intfs_c_, _drv_err_,
365 			 ("Initialize driver software resource Failed!\n"));
366 		goto free_hal_data;
367 	}
368 
369 #ifdef CONFIG_PM
370 	if (padapter->pwrctrlpriv.bSupportRemoteWakeup) {
371 		dvobj->pusbdev->do_remote_wakeup = 1;
372 		pusb_intf->needs_remote_wakeup = 1;
373 		device_init_wakeup(&pusb_intf->dev, 1);
374 		pr_debug("\n  padapter->pwrctrlpriv.bSupportRemoteWakeup~~~~~~\n");
375 		pr_debug("\n  padapter->pwrctrlpriv.bSupportRemoteWakeup~~~[%d]~~~\n",
376 			device_may_wakeup(&pusb_intf->dev));
377 	}
378 #endif
379 
380 	/* 2012-07-11 Move here to prevent the 8723AS-VAU BT auto
381 	 * suspend influence */
382 	if (usb_autopm_get_interface(pusb_intf) < 0)
383 		pr_debug("can't get autopm:\n");
384 
385 	/*  alloc dev name after read efuse. */
386 	rtw_init_netdev_name(pnetdev, padapter->registrypriv.ifname);
387 	rtw_macaddr_cfg(padapter->eeprompriv.mac_addr);
388 	memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
389 	pr_debug("MAC Address from pnetdev->dev_addr =  %pM\n",
390 		pnetdev->dev_addr);
391 
392 	/* step 6. Tell the network stack we exist */
393 	if (register_netdev(pnetdev) != 0) {
394 		RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("register_netdev() failed\n"));
395 		goto free_hal_data;
396 	}
397 
398 	pr_debug("bDriverStopped:%d, bSurpriseRemoved:%d, bup:%d, hw_init_completed:%d\n"
399 		, padapter->bDriverStopped
400 		, padapter->bSurpriseRemoved
401 		, padapter->bup
402 		, padapter->hw_init_completed
403 	);
404 
405 	status = _SUCCESS;
406 
407 free_hal_data:
408 	if (status != _SUCCESS)
409 		kfree(padapter->HalData);
410 free_adapter:
411 	if (status != _SUCCESS) {
412 		if (pnetdev)
413 			rtw_free_netdev(pnetdev);
414 		else
415 			vfree(padapter);
416 		padapter = NULL;
417 	}
418 exit:
419 	return padapter;
420 }
421 
rtw_usb_if1_deinit(struct adapter * if1)422 static void rtw_usb_if1_deinit(struct adapter *if1)
423 {
424 	struct net_device *pnetdev = if1->pnetdev;
425 	struct mlme_priv *pmlmepriv = &if1->mlmepriv;
426 
427 	if (check_fwstate(pmlmepriv, _FW_LINKED))
428 		rtw_disassoc_cmd(if1, 0, false);
429 
430 #ifdef CONFIG_88EU_AP_MODE
431 	free_mlme_ap_info(if1);
432 #endif
433 
434 	if (pnetdev)
435 		unregister_netdev(pnetdev); /* will call netdev_close() */
436 
437 	rtl88eu_mon_deinit(if1->pmondev);
438 	rtw_cancel_all_timer(if1);
439 
440 	rtw_dev_unload(if1);
441 	pr_debug("+r871xu_dev_remove, hw_init_completed=%d\n",
442 		if1->hw_init_completed);
443 	rtw_free_drv_sw(if1);
444 	rtw_free_netdev(pnetdev);
445 }
446 
rtw_drv_init(struct usb_interface * pusb_intf,const struct usb_device_id * pdid)447 static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
448 {
449 	struct adapter *if1 = NULL;
450 	struct dvobj_priv *dvobj;
451 
452 	/* Initialize dvobj_priv */
453 	dvobj = usb_dvobj_init(pusb_intf);
454 	if (!dvobj) {
455 		RT_TRACE(_module_hci_intfs_c_, _drv_err_,
456 			 ("initialize device object priv Failed!\n"));
457 		goto exit;
458 	}
459 
460 	if1 = rtw_usb_if1_init(dvobj, pusb_intf, pdid);
461 	if (!if1) {
462 		pr_debug("rtw_init_primarystruct adapter Failed!\n");
463 		goto free_dvobj;
464 	}
465 
466 	return 0;
467 
468 free_dvobj:
469 	usb_dvobj_deinit(pusb_intf);
470 exit:
471 	return -ENODEV;
472 }
473 
474 /*
475  * dev_remove() - our device is being removed
476 */
477 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both */
rtw_dev_remove(struct usb_interface * pusb_intf)478 static void rtw_dev_remove(struct usb_interface *pusb_intf)
479 {
480 	struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
481 	struct adapter *padapter = dvobj->if1;
482 
483 	pr_debug("+rtw_dev_remove\n");
484 	RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+dev_remove()\n"));
485 
486 	if (!pusb_intf->unregistering)
487 		padapter->bSurpriseRemoved = true;
488 
489 	rtw_pm_set_ips(padapter, IPS_NONE);
490 	rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
491 
492 	LeaveAllPowerSaveMode(padapter);
493 
494 	rtw_usb_if1_deinit(padapter);
495 
496 	usb_dvobj_deinit(pusb_intf);
497 
498 	RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("-dev_remove()\n"));
499 	pr_debug("-r871xu_dev_remove, done\n");
500 }
501 
502 static struct usb_driver rtl8188e_usb_drv = {
503 	.name = "r8188eu",
504 	.probe = rtw_drv_init,
505 	.disconnect = rtw_dev_remove,
506 	.id_table = rtw_usb_id_tbl,
507 	.suspend =  rtw_suspend,
508 	.resume = rtw_resume,
509 	.reset_resume = rtw_resume,
510 };
511 
512 module_usb_driver(rtl8188e_usb_drv)
513