1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
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>
14 #include <linux/usb.h>
15 #include <linux/vmalloc.h>
17 #include <osdep_intf.h>
19 #include <usb_ops_linux.h>
20 #include <rtw_ioctl.h>
22 #include "rtl8188e_hal.h"
24 #define USB_VENDER_ID_REALTEK 0x0bda
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(0x0B05, 0x18F0)}, /* ASUS USB-N10 Nano B1 */
36 {USB_DEVICE(0x2001, 0x330F)}, /* DLink DWA-125 REV D1 */
37 {USB_DEVICE(0x2001, 0x3310)}, /* Dlink DWA-123 REV D1 */
38 {USB_DEVICE(0x2001, 0x3311)}, /* DLink GO-USB-N150 REV B1 */
39 {USB_DEVICE(0x2001, 0x331B)}, /* D-Link DWA-121 rev B1 */
40 {USB_DEVICE(0x2357, 0x010c)}, /* TP-Link TL-WN722N v2 */
41 {USB_DEVICE(0x2357, 0x0111)}, /* TP-Link TL-WN727N v5.21 */
42 {USB_DEVICE(0x2C4E, 0x0102)}, /* MERCUSYS MW150US v2 */
43 {USB_DEVICE(0x0df6, 0x0076)}, /* Sitecom N150 v2 */
44 {USB_DEVICE(0x7392, 0xb811)}, /* Edimax EW-7811UN V2 */
45 {USB_DEVICE(USB_VENDER_ID_REALTEK, 0xffef)}, /* Rosewill RNX-N150NUB */
46 {} /* Terminating entry */
49 MODULE_DEVICE_TABLE(usb, rtw_usb_id_tbl);
51 static struct dvobj_priv *usb_dvobj_init(struct usb_interface *usb_intf)
54 struct dvobj_priv *pdvobjpriv;
55 struct usb_host_config *phost_conf;
56 struct usb_config_descriptor *pconf_desc;
57 struct usb_host_interface *phost_iface;
58 struct usb_interface_descriptor *piface_desc;
59 struct usb_endpoint_descriptor *pendp_desc;
60 struct usb_device *pusbd;
62 pdvobjpriv = kzalloc(sizeof(*pdvobjpriv), GFP_KERNEL);
66 pdvobjpriv->pusbintf = usb_intf;
67 pusbd = interface_to_usbdev(usb_intf);
68 pdvobjpriv->pusbdev = pusbd;
69 usb_set_intfdata(usb_intf, pdvobjpriv);
71 pdvobjpriv->RtNumInPipes = 0;
72 pdvobjpriv->RtNumOutPipes = 0;
74 phost_conf = pusbd->actconfig;
75 pconf_desc = &phost_conf->desc;
77 phost_iface = usb_intf->cur_altsetting;
78 piface_desc = &phost_iface->desc;
80 pdvobjpriv->NumInterfaces = pconf_desc->bNumInterfaces;
81 pdvobjpriv->InterfaceNumber = piface_desc->bInterfaceNumber;
83 for (i = 0; i < piface_desc->bNumEndpoints; i++) {
86 pendp_desc = &phost_iface->endpoint[i].desc;
88 ep_num = usb_endpoint_num(pendp_desc);
90 if (usb_endpoint_is_bulk_in(pendp_desc)) {
91 pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = ep_num;
92 pdvobjpriv->RtNumInPipes++;
93 } else if (usb_endpoint_is_int_in(pendp_desc)) {
94 pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = ep_num;
95 pdvobjpriv->RtNumInPipes++;
96 } else if (usb_endpoint_is_bulk_out(pendp_desc)) {
97 pdvobjpriv->RtOutPipe[pdvobjpriv->RtNumOutPipes] =
99 pdvobjpriv->RtNumOutPipes++;
103 if (pusbd->speed == USB_SPEED_HIGH)
104 pdvobjpriv->ishighspeed = true;
106 pdvobjpriv->ishighspeed = false;
108 mutex_init(&pdvobjpriv->usb_vendor_req_mutex);
114 static void usb_dvobj_deinit(struct usb_interface *usb_intf)
116 struct dvobj_priv *dvobj = usb_get_intfdata(usb_intf);
118 usb_set_intfdata(usb_intf, NULL);
120 /* Modify condition for 92DU DMDP 2010.11.18, by Thomas */
121 if ((dvobj->NumInterfaces != 2 &&
122 dvobj->NumInterfaces != 3) ||
123 (dvobj->InterfaceNumber == 1)) {
124 if (interface_to_usbdev(usb_intf)->state !=
125 USB_STATE_NOTATTACHED) {
126 /* If we didn't unplug usb dongle and
127 * remove/insert module, driver fails
128 * on sitesurvey for the first time when
129 * device is up . Reset usb port for sitesurvey
131 pr_debug("usb attached..., try to reset usb device\n");
132 usb_reset_device(interface_to_usbdev(usb_intf));
136 mutex_destroy(&dvobj->usb_vendor_req_mutex);
140 usb_put_dev(interface_to_usbdev(usb_intf));
143 void usb_intf_stop(struct adapter *padapter)
145 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+usb_intf_stop\n"));
147 /* disable_hw_interrupt */
148 if (!padapter->bSurpriseRemoved) {
149 /* device still exists, so driver can do i/o operation */
151 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
152 ("SurpriseRemoved == false\n"));
156 rtw_hal_inirp_deinit(padapter);
159 usb_write_port_cancel(padapter);
161 /* todo:cancel other irps */
163 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("-usb_intf_stop\n"));
166 static void rtw_dev_unload(struct adapter *padapter)
168 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+rtw_dev_unload\n"));
171 pr_debug("===> rtw_dev_unload\n");
172 padapter->bDriverStopped = true;
173 if (padapter->xmitpriv.ack_tx)
174 rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP);
176 usb_intf_stop(padapter);
178 if (!padapter->pwrctrlpriv.bInternalAutoSuspend)
179 rtw_stop_drv_threads(padapter);
182 if (!padapter->bSurpriseRemoved) {
183 rtw_hal_deinit(padapter);
184 padapter->bSurpriseRemoved = true;
187 padapter->bup = false;
189 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
190 ("r871x_dev_unload():padapter->bup == false\n"));
193 pr_debug("<=== rtw_dev_unload\n");
195 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("-rtw_dev_unload\n"));
198 static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message)
200 struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
201 struct adapter *padapter = dvobj->if1;
202 struct net_device *pnetdev = padapter->pnetdev;
203 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
204 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
205 unsigned long start_time = jiffies;
207 pr_debug("==> %s (%s:%d)\n", __func__, current->comm, current->pid);
209 if ((!padapter->bup) || (padapter->bDriverStopped) ||
210 (padapter->bSurpriseRemoved)) {
211 pr_debug("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
212 padapter->bup, padapter->bDriverStopped,
213 padapter->bSurpriseRemoved);
217 pwrpriv->bInSuspend = true;
218 rtw_cancel_all_timer(padapter);
219 LeaveAllPowerSaveMode(padapter);
221 mutex_lock(&pwrpriv->mutex_lock);
224 netif_carrier_off(pnetdev);
225 netif_tx_stop_all_queues(pnetdev);
229 rtw_disassoc_cmd(padapter, 0, false);
231 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
232 check_fwstate(pmlmepriv, _FW_LINKED)) {
233 pr_debug("%s:%d %s(%pM), length:%d assoc_ssid.length:%d\n",
235 pmlmepriv->cur_network.network.Ssid.Ssid,
236 pmlmepriv->cur_network.network.MacAddress,
237 pmlmepriv->cur_network.network.Ssid.SsidLength,
238 pmlmepriv->assoc_ssid.SsidLength);
240 pmlmepriv->to_roaming = 1;
242 /* s2-2. indicate disconnect to os */
243 rtw_indicate_disconnect(padapter);
245 rtw_free_assoc_resources(padapter);
247 rtw_free_network_queue(padapter, true);
249 rtw_dev_unload(padapter);
250 mutex_unlock(&pwrpriv->mutex_lock);
252 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
253 rtw_indicate_scan_done(padapter, 1);
255 if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING))
256 rtw_indicate_disconnect(padapter);
259 pr_debug("<=== %s .............. in %dms\n", __func__,
260 jiffies_to_msecs(jiffies - start_time));
265 static int rtw_resume_process(struct adapter *padapter)
267 struct net_device *pnetdev;
268 struct pwrctrl_priv *pwrpriv = NULL;
270 unsigned long start_time = jiffies;
272 pr_debug("==> %s (%s:%d)\n", __func__, current->comm, current->pid);
275 pnetdev = padapter->pnetdev;
276 pwrpriv = &padapter->pwrctrlpriv;
281 mutex_lock(&pwrpriv->mutex_lock);
282 rtw_reset_drv_sw(padapter);
283 pwrpriv->bkeepfwalive = false;
285 pr_debug("bkeepfwalive(%x)\n", pwrpriv->bkeepfwalive);
286 if (netdev_open(pnetdev) != 0) {
287 mutex_unlock(&pwrpriv->mutex_lock);
291 netif_device_attach(pnetdev);
292 netif_carrier_on(pnetdev);
294 mutex_unlock(&pwrpriv->mutex_lock);
296 rtw_roaming(padapter, NULL);
301 pwrpriv->bInSuspend = false;
302 pr_debug("<=== %s return %d.............. in %dms\n", __func__,
303 ret, jiffies_to_msecs(jiffies - start_time));
308 static int rtw_resume(struct usb_interface *pusb_intf)
310 struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
311 struct adapter *padapter = dvobj->if1;
313 return rtw_resume_process(padapter);
317 * drv_init() - a device potentially for us
319 * notes: drv_init() is called when the bus driver has located
320 * a card for us to support.
321 * We accept the new device by returning 0.
324 static struct adapter *rtw_usb_if1_init(struct dvobj_priv *dvobj,
325 struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
327 struct adapter *padapter = NULL;
328 struct net_device *pnetdev = NULL;
329 struct net_device *pmondev;
332 padapter = vzalloc(sizeof(*padapter));
335 padapter->dvobj = dvobj;
336 dvobj->if1 = padapter;
338 padapter->bDriverStopped = true;
339 mutex_init(&padapter->hw_init_mutex);
341 pnetdev = rtw_init_netdev(padapter);
344 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(dvobj));
345 padapter = rtw_netdev_priv(pnetdev);
347 if (padapter->registrypriv.monitor_enable) {
348 pmondev = rtl88eu_mon_init();
350 netdev_warn(pnetdev, "Failed to initialize monitor interface");
351 padapter->pmondev = pmondev;
354 padapter->HalData = kzalloc(sizeof(struct hal_data_8188e), GFP_KERNEL);
355 if (!padapter->HalData) {
356 DBG_88E("Failed to allocate memory for HAL data\n");
360 /* step read_chip_version */
361 rtw_hal_read_chip_version(padapter);
363 /* step usb endpoint mapping */
364 rtw_hal_chip_configure(padapter);
366 /* step read efuse/eeprom data and get mac_addr */
367 rtw_hal_read_chip_info(padapter);
370 if (rtw_init_drv_sw(padapter) == _FAIL) {
371 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
372 ("Initialize driver software resource Failed!\n"));
377 if (padapter->pwrctrlpriv.bSupportRemoteWakeup) {
378 dvobj->pusbdev->do_remote_wakeup = 1;
379 pusb_intf->needs_remote_wakeup = 1;
380 device_init_wakeup(&pusb_intf->dev, 1);
381 pr_debug("\n padapter->pwrctrlpriv.bSupportRemoteWakeup~~~~~~\n");
382 pr_debug("\n padapter->pwrctrlpriv.bSupportRemoteWakeup~~~[%d]~~~\n",
383 device_may_wakeup(&pusb_intf->dev));
387 /* 2012-07-11 Move here to prevent the 8723AS-VAU BT auto
388 * suspend influence */
389 if (usb_autopm_get_interface(pusb_intf) < 0)
390 pr_debug("can't get autopm:\n");
392 /* alloc dev name after read efuse. */
393 rtw_init_netdev_name(pnetdev, padapter->registrypriv.ifname);
394 rtw_macaddr_cfg(padapter->eeprompriv.mac_addr);
395 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
396 pr_debug("MAC Address from pnetdev->dev_addr = %pM\n",
399 /* step 6. Tell the network stack we exist */
400 if (register_netdev(pnetdev) != 0) {
401 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("register_netdev() failed\n"));
405 pr_debug("bDriverStopped:%d, bSurpriseRemoved:%d, bup:%d, hw_init_completed:%d\n"
406 , padapter->bDriverStopped
407 , padapter->bSurpriseRemoved
409 , padapter->hw_init_completed
415 if (status != _SUCCESS)
416 kfree(padapter->HalData);
418 if (status != _SUCCESS) {
420 rtw_free_netdev(pnetdev);
429 static void rtw_usb_if1_deinit(struct adapter *if1)
431 struct net_device *pnetdev = if1->pnetdev;
432 struct mlme_priv *pmlmepriv = &if1->mlmepriv;
434 if (check_fwstate(pmlmepriv, _FW_LINKED))
435 rtw_disassoc_cmd(if1, 0, false);
437 #ifdef CONFIG_88EU_AP_MODE
438 free_mlme_ap_info(if1);
442 unregister_netdev(pnetdev); /* will call netdev_close() */
444 rtl88eu_mon_deinit(if1->pmondev);
445 rtw_cancel_all_timer(if1);
448 pr_debug("+r871xu_dev_remove, hw_init_completed=%d\n",
449 if1->hw_init_completed);
450 rtw_free_drv_sw(if1);
451 rtw_free_netdev(pnetdev);
454 static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
456 struct adapter *if1 = NULL;
457 struct dvobj_priv *dvobj;
459 /* Initialize dvobj_priv */
460 dvobj = usb_dvobj_init(pusb_intf);
462 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
463 ("initialize device object priv Failed!\n"));
467 if1 = rtw_usb_if1_init(dvobj, pusb_intf, pdid);
469 pr_debug("rtw_init_primarystruct adapter Failed!\n");
476 usb_dvobj_deinit(pusb_intf);
482 * dev_remove() - our device is being removed
484 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both */
485 static void rtw_dev_remove(struct usb_interface *pusb_intf)
487 struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
488 struct adapter *padapter = dvobj->if1;
490 pr_debug("+rtw_dev_remove\n");
491 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+dev_remove()\n"));
493 if (!pusb_intf->unregistering)
494 padapter->bSurpriseRemoved = true;
496 rtw_pm_set_ips(padapter, IPS_NONE);
497 rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
499 LeaveAllPowerSaveMode(padapter);
501 rtw_usb_if1_deinit(padapter);
503 usb_dvobj_deinit(pusb_intf);
505 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("-dev_remove()\n"));
506 pr_debug("-r871xu_dev_remove, done\n");
509 static struct usb_driver rtl8188e_usb_drv = {
511 .probe = rtw_drv_init,
512 .disconnect = rtw_dev_remove,
513 .id_table = rtw_usb_id_tbl,
514 .suspend = rtw_suspend,
515 .resume = rtw_resume,
516 .reset_resume = rtw_resume,
519 module_usb_driver(rtl8188e_usb_drv)