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(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(0x2001, 0x331B)}, /* D-Link DWA-121 rev B1 */
39 {USB_DEVICE(0x2357, 0x010c)}, /* TP-Link TL-WN722N v2 */
40 {USB_DEVICE(0x0df6, 0x0076)}, /* Sitecom N150 v2 */
41 {USB_DEVICE(USB_VENDER_ID_REALTEK
, 0xffef)}, /* Rosewill RNX-N150NUB */
42 {} /* Terminating entry */
45 MODULE_DEVICE_TABLE(usb
, rtw_usb_id_tbl
);
47 static struct dvobj_priv
*usb_dvobj_init(struct usb_interface
*usb_intf
)
50 struct dvobj_priv
*pdvobjpriv
;
51 struct usb_host_config
*phost_conf
;
52 struct usb_config_descriptor
*pconf_desc
;
53 struct usb_host_interface
*phost_iface
;
54 struct usb_interface_descriptor
*piface_desc
;
55 struct usb_endpoint_descriptor
*pendp_desc
;
56 struct usb_device
*pusbd
;
58 pdvobjpriv
= kzalloc(sizeof(*pdvobjpriv
), GFP_KERNEL
);
62 pdvobjpriv
->pusbintf
= usb_intf
;
63 pusbd
= interface_to_usbdev(usb_intf
);
64 pdvobjpriv
->pusbdev
= pusbd
;
65 usb_set_intfdata(usb_intf
, pdvobjpriv
);
67 pdvobjpriv
->RtNumInPipes
= 0;
68 pdvobjpriv
->RtNumOutPipes
= 0;
70 phost_conf
= pusbd
->actconfig
;
71 pconf_desc
= &phost_conf
->desc
;
73 phost_iface
= &usb_intf
->altsetting
[0];
74 piface_desc
= &phost_iface
->desc
;
76 pdvobjpriv
->NumInterfaces
= pconf_desc
->bNumInterfaces
;
77 pdvobjpriv
->InterfaceNumber
= piface_desc
->bInterfaceNumber
;
79 for (i
= 0; i
< piface_desc
->bNumEndpoints
; i
++) {
82 pendp_desc
= &phost_iface
->endpoint
[i
].desc
;
84 ep_num
= usb_endpoint_num(pendp_desc
);
86 if (usb_endpoint_is_bulk_in(pendp_desc
)) {
87 pdvobjpriv
->RtInPipe
[pdvobjpriv
->RtNumInPipes
] = ep_num
;
88 pdvobjpriv
->RtNumInPipes
++;
89 } else if (usb_endpoint_is_int_in(pendp_desc
)) {
90 pdvobjpriv
->RtInPipe
[pdvobjpriv
->RtNumInPipes
] = ep_num
;
91 pdvobjpriv
->RtNumInPipes
++;
92 } else if (usb_endpoint_is_bulk_out(pendp_desc
)) {
93 pdvobjpriv
->RtOutPipe
[pdvobjpriv
->RtNumOutPipes
] =
95 pdvobjpriv
->RtNumOutPipes
++;
99 if (pusbd
->speed
== USB_SPEED_HIGH
)
100 pdvobjpriv
->ishighspeed
= true;
102 pdvobjpriv
->ishighspeed
= false;
104 mutex_init(&pdvobjpriv
->usb_vendor_req_mutex
);
110 static void usb_dvobj_deinit(struct usb_interface
*usb_intf
)
112 struct dvobj_priv
*dvobj
= usb_get_intfdata(usb_intf
);
114 usb_set_intfdata(usb_intf
, NULL
);
116 /* Modify condition for 92DU DMDP 2010.11.18, by Thomas */
117 if ((dvobj
->NumInterfaces
!= 2 &&
118 dvobj
->NumInterfaces
!= 3) ||
119 (dvobj
->InterfaceNumber
== 1)) {
120 if (interface_to_usbdev(usb_intf
)->state
!=
121 USB_STATE_NOTATTACHED
) {
122 /* If we didn't unplug usb dongle and
123 * remove/insert module, driver fails
124 * on sitesurvey for the first time when
125 * device is up . Reset usb port for sitesurvey
127 pr_debug("usb attached..., try to reset usb device\n");
128 usb_reset_device(interface_to_usbdev(usb_intf
));
132 mutex_destroy(&dvobj
->usb_vendor_req_mutex
);
136 usb_put_dev(interface_to_usbdev(usb_intf
));
139 void usb_intf_stop(struct adapter
*padapter
)
141 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("+usb_intf_stop\n"));
143 /* disable_hw_interrupt */
144 if (!padapter
->bSurpriseRemoved
) {
145 /* device still exists, so driver can do i/o operation */
147 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
,
148 ("SurpriseRemoved == false\n"));
152 rtw_hal_inirp_deinit(padapter
);
155 usb_write_port_cancel(padapter
);
157 /* todo:cancel other irps */
159 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("-usb_intf_stop\n"));
162 static void rtw_dev_unload(struct adapter
*padapter
)
164 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("+rtw_dev_unload\n"));
167 pr_debug("===> rtw_dev_unload\n");
168 padapter
->bDriverStopped
= true;
169 if (padapter
->xmitpriv
.ack_tx
)
170 rtw_ack_tx_done(&padapter
->xmitpriv
, RTW_SCTX_DONE_DRV_STOP
);
172 usb_intf_stop(padapter
);
174 if (!padapter
->pwrctrlpriv
.bInternalAutoSuspend
)
175 rtw_stop_drv_threads(padapter
);
178 if (!padapter
->bSurpriseRemoved
) {
179 rtw_hal_deinit(padapter
);
180 padapter
->bSurpriseRemoved
= true;
183 padapter
->bup
= false;
185 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
,
186 ("r871x_dev_unload():padapter->bup == false\n"));
189 pr_debug("<=== rtw_dev_unload\n");
191 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("-rtw_dev_unload\n"));
194 static int rtw_suspend(struct usb_interface
*pusb_intf
, pm_message_t message
)
196 struct dvobj_priv
*dvobj
= usb_get_intfdata(pusb_intf
);
197 struct adapter
*padapter
= dvobj
->if1
;
198 struct net_device
*pnetdev
= padapter
->pnetdev
;
199 struct mlme_priv
*pmlmepriv
= &padapter
->mlmepriv
;
200 struct pwrctrl_priv
*pwrpriv
= &padapter
->pwrctrlpriv
;
201 unsigned long start_time
= jiffies
;
203 pr_debug("==> %s (%s:%d)\n", __func__
, current
->comm
, current
->pid
);
205 if ((!padapter
->bup
) || (padapter
->bDriverStopped
) ||
206 (padapter
->bSurpriseRemoved
)) {
207 pr_debug("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
208 padapter
->bup
, padapter
->bDriverStopped
,
209 padapter
->bSurpriseRemoved
);
213 pwrpriv
->bInSuspend
= true;
214 rtw_cancel_all_timer(padapter
);
215 LeaveAllPowerSaveMode(padapter
);
217 mutex_lock(&pwrpriv
->mutex_lock
);
220 netif_carrier_off(pnetdev
);
221 netif_tx_stop_all_queues(pnetdev
);
225 rtw_disassoc_cmd(padapter
, 0, false);
227 if (check_fwstate(pmlmepriv
, WIFI_STATION_STATE
) &&
228 check_fwstate(pmlmepriv
, _FW_LINKED
)) {
229 pr_debug("%s:%d %s(%pM), length:%d assoc_ssid.length:%d\n",
231 pmlmepriv
->cur_network
.network
.ssid
.ssid
,
232 pmlmepriv
->cur_network
.network
.MacAddress
,
233 pmlmepriv
->cur_network
.network
.ssid
.ssid_length
,
234 pmlmepriv
->assoc_ssid
.ssid_length
);
236 pmlmepriv
->to_roaming
= 1;
238 /* s2-2. indicate disconnect to os */
239 rtw_indicate_disconnect(padapter
);
241 rtw_free_assoc_resources(padapter
);
243 rtw_free_network_queue(padapter
, true);
245 rtw_dev_unload(padapter
);
246 mutex_unlock(&pwrpriv
->mutex_lock
);
248 if (check_fwstate(pmlmepriv
, _FW_UNDER_SURVEY
))
249 rtw_indicate_scan_done(padapter
, 1);
251 if (check_fwstate(pmlmepriv
, _FW_UNDER_LINKING
))
252 rtw_indicate_disconnect(padapter
);
255 pr_debug("<=== %s .............. in %dms\n", __func__
,
256 jiffies_to_msecs(jiffies
- start_time
));
261 static int rtw_resume_process(struct adapter
*padapter
)
263 struct net_device
*pnetdev
;
264 struct pwrctrl_priv
*pwrpriv
= NULL
;
266 unsigned long start_time
= jiffies
;
268 pr_debug("==> %s (%s:%d)\n", __func__
, current
->comm
, current
->pid
);
271 pnetdev
= padapter
->pnetdev
;
272 pwrpriv
= &padapter
->pwrctrlpriv
;
277 mutex_lock(&pwrpriv
->mutex_lock
);
278 rtw_reset_drv_sw(padapter
);
279 pwrpriv
->bkeepfwalive
= false;
281 pr_debug("bkeepfwalive(%x)\n", pwrpriv
->bkeepfwalive
);
282 if (netdev_open(pnetdev
) != 0) {
283 mutex_unlock(&pwrpriv
->mutex_lock
);
287 netif_device_attach(pnetdev
);
288 netif_carrier_on(pnetdev
);
290 mutex_unlock(&pwrpriv
->mutex_lock
);
292 rtw_roaming(padapter
, NULL
);
297 pwrpriv
->bInSuspend
= false;
298 pr_debug("<=== %s return %d.............. in %dms\n", __func__
,
299 ret
, jiffies_to_msecs(jiffies
- start_time
));
304 static int rtw_resume(struct usb_interface
*pusb_intf
)
306 struct dvobj_priv
*dvobj
= usb_get_intfdata(pusb_intf
);
307 struct adapter
*padapter
= dvobj
->if1
;
309 return rtw_resume_process(padapter
);
313 * drv_init() - a device potentially for us
315 * notes: drv_init() is called when the bus driver has located
316 * a card for us to support.
317 * We accept the new device by returning 0.
320 static struct adapter
*rtw_usb_if1_init(struct dvobj_priv
*dvobj
,
321 struct usb_interface
*pusb_intf
, const struct usb_device_id
*pdid
)
323 struct adapter
*padapter
= NULL
;
324 struct net_device
*pnetdev
= NULL
;
325 struct net_device
*pmondev
;
328 padapter
= vzalloc(sizeof(*padapter
));
331 padapter
->dvobj
= dvobj
;
332 dvobj
->if1
= padapter
;
334 padapter
->bDriverStopped
= true;
335 mutex_init(&padapter
->hw_init_mutex
);
337 pnetdev
= rtw_init_netdev(padapter
);
340 SET_NETDEV_DEV(pnetdev
, dvobj_to_dev(dvobj
));
341 padapter
= rtw_netdev_priv(pnetdev
);
343 if (padapter
->registrypriv
.monitor_enable
) {
344 pmondev
= rtl88eu_mon_init();
346 netdev_warn(pnetdev
, "Failed to initialize monitor interface");
347 padapter
->pmondev
= pmondev
;
350 padapter
->HalData
= kzalloc(sizeof(struct hal_data_8188e
), GFP_KERNEL
);
351 if (!padapter
->HalData
) {
352 DBG_88E("Failed to allocate memory for HAL data\n");
356 /* step read_chip_version */
357 rtw_hal_read_chip_version(padapter
);
359 /* step usb endpoint mapping */
360 rtw_hal_chip_configure(padapter
);
362 /* step read efuse/eeprom data and get mac_addr */
363 rtw_hal_read_chip_info(padapter
);
366 if (rtw_init_drv_sw(padapter
) == _FAIL
) {
367 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
,
368 ("Initialize driver software resource Failed!\n"));
373 if (padapter
->pwrctrlpriv
.bSupportRemoteWakeup
) {
374 dvobj
->pusbdev
->do_remote_wakeup
= 1;
375 pusb_intf
->needs_remote_wakeup
= 1;
376 device_init_wakeup(&pusb_intf
->dev
, 1);
377 pr_debug("\n padapter->pwrctrlpriv.bSupportRemoteWakeup~~~~~~\n");
378 pr_debug("\n padapter->pwrctrlpriv.bSupportRemoteWakeup~~~[%d]~~~\n",
379 device_may_wakeup(&pusb_intf
->dev
));
383 /* 2012-07-11 Move here to prevent the 8723AS-VAU BT auto
384 * suspend influence */
385 if (usb_autopm_get_interface(pusb_intf
) < 0)
386 pr_debug("can't get autopm:\n");
388 /* alloc dev name after read efuse. */
389 rtw_init_netdev_name(pnetdev
, padapter
->registrypriv
.ifname
);
390 rtw_macaddr_cfg(padapter
->eeprompriv
.mac_addr
);
391 memcpy(pnetdev
->dev_addr
, padapter
->eeprompriv
.mac_addr
, ETH_ALEN
);
392 pr_debug("MAC Address from pnetdev->dev_addr = %pM\n",
395 /* step 6. Tell the network stack we exist */
396 if (register_netdev(pnetdev
) != 0) {
397 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("register_netdev() failed\n"));
401 pr_debug("bDriverStopped:%d, bSurpriseRemoved:%d, bup:%d, hw_init_completed:%d\n"
402 , padapter
->bDriverStopped
403 , padapter
->bSurpriseRemoved
405 , padapter
->hw_init_completed
411 if (status
!= _SUCCESS
)
412 kfree(padapter
->HalData
);
414 if (status
!= _SUCCESS
) {
416 rtw_free_netdev(pnetdev
);
425 static void rtw_usb_if1_deinit(struct adapter
*if1
)
427 struct net_device
*pnetdev
= if1
->pnetdev
;
428 struct mlme_priv
*pmlmepriv
= &if1
->mlmepriv
;
430 if (check_fwstate(pmlmepriv
, _FW_LINKED
))
431 rtw_disassoc_cmd(if1
, 0, false);
433 #ifdef CONFIG_88EU_AP_MODE
434 free_mlme_ap_info(if1
);
438 unregister_netdev(pnetdev
); /* will call netdev_close() */
440 rtl88eu_mon_deinit(if1
->pmondev
);
441 rtw_cancel_all_timer(if1
);
444 pr_debug("+r871xu_dev_remove, hw_init_completed=%d\n",
445 if1
->hw_init_completed
);
446 rtw_free_drv_sw(if1
);
447 rtw_free_netdev(pnetdev
);
450 static int rtw_drv_init(struct usb_interface
*pusb_intf
, const struct usb_device_id
*pdid
)
452 struct adapter
*if1
= NULL
;
453 struct dvobj_priv
*dvobj
;
455 /* Initialize dvobj_priv */
456 dvobj
= usb_dvobj_init(pusb_intf
);
458 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
,
459 ("initialize device object priv Failed!\n"));
463 if1
= rtw_usb_if1_init(dvobj
, pusb_intf
, pdid
);
465 pr_debug("rtw_init_primarystruct adapter Failed!\n");
472 usb_dvobj_deinit(pusb_intf
);
478 * dev_remove() - our device is being removed
480 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both */
481 static void rtw_dev_remove(struct usb_interface
*pusb_intf
)
483 struct dvobj_priv
*dvobj
= usb_get_intfdata(pusb_intf
);
484 struct adapter
*padapter
= dvobj
->if1
;
486 pr_debug("+rtw_dev_remove\n");
487 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("+dev_remove()\n"));
489 if (!pusb_intf
->unregistering
)
490 padapter
->bSurpriseRemoved
= true;
492 rtw_pm_set_ips(padapter
, IPS_NONE
);
493 rtw_pm_set_lps(padapter
, PS_MODE_ACTIVE
);
495 LeaveAllPowerSaveMode(padapter
);
497 rtw_usb_if1_deinit(padapter
);
499 usb_dvobj_deinit(pusb_intf
);
501 RT_TRACE(_module_hci_intfs_c_
, _drv_err_
, ("-dev_remove()\n"));
502 pr_debug("-r871xu_dev_remove, done\n");
505 static struct usb_driver rtl8188e_usb_drv
= {
507 .probe
= rtw_drv_init
,
508 .disconnect
= rtw_dev_remove
,
509 .id_table
= rtw_usb_id_tbl
,
510 .suspend
= rtw_suspend
,
511 .resume
= rtw_resume
,
512 .reset_resume
= rtw_resume
,
515 module_usb_driver(rtl8188e_usb_drv
)