1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3
4 #include <linux/usb.h>
5 #include "../include/osdep_service.h"
6 #include "../include/drv_types.h"
7 #include "../include/hal_intf.h"
8 #include "../include/osdep_intf.h"
9 #include "../include/usb_ops.h"
10 #include "../include/usb_osintf.h"
11 #include "../include/rtw_ioctl.h"
12 #include "../include/rtl8188e_hal.h"
13
14 int ui_pid[3] = {0, 0, 0};
15
16 static int rtw_suspend(struct usb_interface *intf, pm_message_t message);
17 static int rtw_resume(struct usb_interface *intf);
18
19 static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid);
20 static void rtw_dev_remove(struct usb_interface *pusb_intf);
21
22 #define USB_VENDER_ID_REALTEK 0x0bda
23
24 /* DID_USB_v916_20130116 */
25 static struct usb_device_id rtw_usb_id_tbl[] = {
26 /*=== Realtek demoboard ===*/
27 {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8179)}, /* 8188EUS */
28 {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0179)}, /* 8188ETV */
29 {USB_DEVICE(USB_VENDER_ID_REALTEK, 0xffef)}, /* Rosewill USB-N150 Nano */
30 /*=== Customer ID ===*/
31 /****** 8188EUS ********/
32 {USB_DEVICE(0x07B8, 0x8179)}, /* Abocom - Abocom */
33 {USB_DEVICE(0x0DF6, 0x0076)}, /* Sitecom N150 v2 */
34 {USB_DEVICE(0x2001, 0x330F)}, /* DLink DWA-125 REV D1 */
35 {USB_DEVICE(0x2001, 0x3310)}, /* Dlink DWA-123 REV D1 */
36 {USB_DEVICE(0x2001, 0x3311)}, /* DLink GO-USB-N150 REV B1 */
37 {USB_DEVICE(0x2001, 0x331B)}, /* D-Link DWA-121 rev B1 */
38 {USB_DEVICE(0x056E, 0x4008)}, /* Elecom WDC-150SU2M */
39 {USB_DEVICE(0x2357, 0x010c)}, /* TP-Link TL-WN722N v2 */
40 {USB_DEVICE(0x2357, 0x0111)}, /* TP-Link TL-WN727N v5.21 */
41 {USB_DEVICE(0x2C4E, 0x0102)}, /* MERCUSYS MW150US v2 */
42 {USB_DEVICE(0x0B05, 0x18F0)}, /* ASUS USB-N10 Nano B1 */
43 {USB_DEVICE(0x7392, 0xb811)}, /* Edimax EW-7811Un V2 */
44 {} /* Terminating entry */
45 };
46
47 MODULE_DEVICE_TABLE(usb, rtw_usb_id_tbl);
48
49 struct rtw_usb_drv {
50 struct usb_driver usbdrv;
51 int drv_registered;
52 struct mutex hw_init_mutex;
53 };
54
55 static struct rtw_usb_drv rtl8188e_usb_drv = {
56 .usbdrv.name = KBUILD_MODNAME,
57 .usbdrv.probe = rtw_drv_init,
58 .usbdrv.disconnect = rtw_dev_remove,
59 .usbdrv.id_table = rtw_usb_id_tbl,
60 .usbdrv.suspend = rtw_suspend,
61 .usbdrv.resume = rtw_resume,
62 .usbdrv.reset_resume = rtw_resume,
63 };
64
65 static struct rtw_usb_drv *usb_drv = &rtl8188e_usb_drv;
66
usb_dvobj_init(struct usb_interface * usb_intf)67 static struct dvobj_priv *usb_dvobj_init(struct usb_interface *usb_intf)
68 {
69 int i;
70 u8 rt_num_in_pipes = 0;
71 struct dvobj_priv *pdvobjpriv;
72 struct usb_host_config *phost_conf;
73 struct usb_config_descriptor *pconf_desc;
74 struct usb_host_interface *phost_iface;
75 struct usb_interface_descriptor *piface_desc;
76 struct usb_endpoint_descriptor *pendp_desc;
77 struct usb_device *pusbd;
78
79 pdvobjpriv = kzalloc(sizeof(*pdvobjpriv), GFP_KERNEL);
80 if (!pdvobjpriv)
81 goto err;
82
83 pdvobjpriv->pusbintf = usb_intf;
84 pusbd = interface_to_usbdev(usb_intf);
85 pdvobjpriv->pusbdev = pusbd;
86 usb_set_intfdata(usb_intf, pdvobjpriv);
87
88 pdvobjpriv->RtNumOutPipes = 0;
89
90 phost_conf = pusbd->actconfig;
91 pconf_desc = &phost_conf->desc;
92
93 phost_iface = &usb_intf->altsetting[0];
94 piface_desc = &phost_iface->desc;
95
96 pdvobjpriv->NumInterfaces = pconf_desc->bNumInterfaces;
97 pdvobjpriv->InterfaceNumber = piface_desc->bInterfaceNumber;
98
99 for (i = 0; i < piface_desc->bNumEndpoints; i++) {
100 int ep_num;
101 pendp_desc = &phost_iface->endpoint[i].desc;
102
103 ep_num = usb_endpoint_num(pendp_desc);
104
105 if (usb_endpoint_is_bulk_in(pendp_desc)) {
106 pdvobjpriv->RtInPipe = ep_num;
107 rt_num_in_pipes++;
108 } else if (usb_endpoint_is_bulk_out(pendp_desc)) {
109 pdvobjpriv->RtOutPipe[pdvobjpriv->RtNumOutPipes] =
110 ep_num;
111 pdvobjpriv->RtNumOutPipes++;
112 }
113 }
114
115 if (rt_num_in_pipes != 1)
116 goto err;
117
118 /* 3 misc */
119 rtw_reset_continual_urb_error(pdvobjpriv);
120
121 usb_get_dev(pusbd);
122 return pdvobjpriv;
123
124 err:
125 kfree(pdvobjpriv);
126 return NULL;
127 }
128
usb_dvobj_deinit(struct usb_interface * usb_intf)129 static void usb_dvobj_deinit(struct usb_interface *usb_intf)
130 {
131 struct dvobj_priv *dvobj = usb_get_intfdata(usb_intf);
132
133 usb_set_intfdata(usb_intf, NULL);
134 if (dvobj) {
135 /* Modify condition for 92DU DMDP 2010.11.18, by Thomas */
136 if ((dvobj->NumInterfaces != 2 &&
137 dvobj->NumInterfaces != 3) ||
138 (dvobj->InterfaceNumber == 1)) {
139 if (interface_to_usbdev(usb_intf)->state !=
140 USB_STATE_NOTATTACHED)
141 /* If we didn't unplug usb dongle and
142 * remove/insert module, driver fails
143 * on sitesurvey for the first time when
144 * device is up . Reset usb port for sitesurvey
145 * fail issue. */
146 usb_reset_device(interface_to_usbdev(usb_intf));
147 }
148 kfree(dvobj);
149 }
150
151 usb_put_dev(interface_to_usbdev(usb_intf));
152
153 }
154
usb_intf_start(struct adapter * padapter)155 static void usb_intf_start(struct adapter *padapter)
156 {
157 rtl8188eu_inirp_init(padapter);
158 }
159
usb_intf_stop(struct adapter * padapter)160 static void usb_intf_stop(struct adapter *padapter)
161 {
162 /* cancel in irp */
163 rtw_read_port_cancel(padapter);
164
165 /* cancel out irp */
166 rtw_write_port_cancel(padapter);
167
168 /* todo:cancel other irps */
169 }
170
rtw_dev_unload(struct adapter * padapter)171 static void rtw_dev_unload(struct adapter *padapter)
172 {
173 if (padapter->bup) {
174 padapter->bDriverStopped = true;
175 if (padapter->xmitpriv.ack_tx)
176 rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP);
177 /* s3. */
178 if (padapter->intf_stop)
179 padapter->intf_stop(padapter);
180 /* s4. */
181 rtw_stop_drv_threads(padapter);
182
183 /* s5. */
184 if (!padapter->bSurpriseRemoved) {
185 rtw_hal_deinit(padapter);
186 padapter->bSurpriseRemoved = true;
187 }
188
189 padapter->bup = false;
190 }
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
201 if ((!padapter->bup) || (padapter->bDriverStopped) ||
202 (padapter->bSurpriseRemoved))
203 goto exit;
204
205 pwrpriv->bInSuspend = true;
206 rtw_cancel_all_timer(padapter);
207 LeaveAllPowerSaveMode(padapter);
208
209 mutex_lock(&pwrpriv->lock);
210 /* s1. */
211 if (pnetdev) {
212 netif_carrier_off(pnetdev);
213 netif_tx_stop_all_queues(pnetdev);
214 }
215
216 /* s2. */
217 rtw_disassoc_cmd(padapter, 0, false);
218
219 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) &&
220 check_fwstate(pmlmepriv, _FW_LINKED))
221 pmlmepriv->to_roaming = 1;
222 /* s2-2. indicate disconnect to os */
223 rtw_indicate_disconnect(padapter);
224 /* s2-3. */
225 rtw_free_assoc_resources(padapter, 1);
226 /* s2-4. */
227 rtw_free_network_queue(padapter, true);
228
229 rtw_dev_unload(padapter);
230 mutex_unlock(&pwrpriv->lock);
231
232 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
233 rtw_indicate_scan_done(padapter);
234
235 if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING))
236 rtw_indicate_disconnect(padapter);
237
238 exit:
239 return 0;
240 }
241
rtw_resume(struct usb_interface * pusb_intf)242 static int rtw_resume(struct usb_interface *pusb_intf)
243 {
244 struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
245 struct adapter *padapter = dvobj->if1;
246 struct net_device *pnetdev;
247 struct pwrctrl_priv *pwrpriv = NULL;
248 int ret = -1;
249
250 pnetdev = padapter->pnetdev;
251 pwrpriv = &padapter->pwrctrlpriv;
252
253 mutex_lock(&pwrpriv->lock);
254 rtw_reset_drv_sw(padapter);
255 if (pwrpriv)
256 pwrpriv->bkeepfwalive = false;
257
258 if (netdev_open(pnetdev) != 0) {
259 mutex_unlock(&pwrpriv->lock);
260 goto exit;
261 }
262
263 netif_device_attach(pnetdev);
264 netif_carrier_on(pnetdev);
265
266 mutex_unlock(&pwrpriv->lock);
267
268 if (padapter->pid[1] != 0)
269 rtw_signal_process(padapter->pid[1], SIGUSR2);
270
271 rtw_roaming(padapter, NULL);
272
273 ret = 0;
274 exit:
275 if (pwrpriv)
276 pwrpriv->bInSuspend = false;
277
278 return ret;
279 }
280
281 /*
282 * drv_init() - a device potentially for us
283 *
284 * notes: drv_init() is called when the bus driver has located
285 * a card for us to support.
286 * We accept the new device by returning 0.
287 */
288
rtw_usb_if1_init(struct dvobj_priv * dvobj,struct usb_interface * pusb_intf)289 static int rtw_usb_if1_init(struct dvobj_priv *dvobj, struct usb_interface *pusb_intf)
290 {
291 struct adapter *padapter = NULL;
292 struct net_device *pnetdev = NULL;
293 struct io_priv *piopriv;
294 struct intf_hdl *pintf;
295 int ret;
296
297 padapter = vzalloc(sizeof(*padapter));
298 if (!padapter)
299 return -ENOMEM;
300
301 padapter->dvobj = dvobj;
302 dvobj->if1 = padapter;
303
304 padapter->bDriverStopped = true;
305
306 padapter->hw_init_mutex = &usb_drv->hw_init_mutex;
307
308 rtw_handle_dualmac(padapter, 1);
309
310 pnetdev = rtw_init_netdev(padapter);
311 if (!pnetdev) {
312 ret = -ENODEV;
313 goto handle_dualmac;
314 }
315 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(dvobj));
316 padapter = rtw_netdev_priv(pnetdev);
317
318 padapter->intf_start = &usb_intf_start;
319 padapter->intf_stop = &usb_intf_stop;
320
321 /* step init_io_priv */
322 piopriv = &padapter->iopriv;
323 pintf = &piopriv->intf;
324 piopriv->padapter = padapter;
325 pintf->padapter = padapter;
326 pintf->pintf_dev = adapter_to_dvobj(padapter);
327
328 /* step read_chip_version */
329 rtl8188e_read_chip_version(padapter);
330
331 /* step usb endpoint mapping */
332 ret = rtl8188eu_interface_configure(padapter);
333 if (ret)
334 goto handle_dualmac;
335
336 /* step read efuse/eeprom data and get mac_addr */
337 ret = ReadAdapterInfo8188EU(padapter);
338 if (ret)
339 goto handle_dualmac;
340
341 /* step 5. */
342 if (rtw_init_drv_sw(padapter) == _FAIL) {
343 ret = -ENODEV;
344 goto handle_dualmac;
345 }
346
347 #ifdef CONFIG_PM
348 if (padapter->pwrctrlpriv.bSupportRemoteWakeup) {
349 dvobj->pusbdev->do_remote_wakeup = 1;
350 pusb_intf->needs_remote_wakeup = 1;
351 device_init_wakeup(&pusb_intf->dev, 1);
352 }
353 #endif
354
355 /* 2012-07-11 Move here to prevent the 8723AS-VAU BT auto
356 * suspend influence */
357 usb_autopm_get_interface(pusb_intf);
358
359 /* alloc dev name after read efuse. */
360 ret = rtw_init_netdev_name(pnetdev, padapter->registrypriv.ifname);
361 if (ret)
362 goto free_drv_sw;
363 rtw_macaddr_cfg(padapter->eeprompriv.mac_addr);
364 rtw_init_wifidirect_addrs(padapter, padapter->eeprompriv.mac_addr,
365 padapter->eeprompriv.mac_addr);
366 eth_hw_addr_set(pnetdev, padapter->eeprompriv.mac_addr);
367
368 /* step 6. Tell the network stack we exist */
369 ret = register_netdev(pnetdev);
370 if (ret)
371 goto free_drv_sw;
372
373 return 0;
374
375 free_drv_sw:
376 rtw_cancel_all_timer(padapter);
377 rtw_free_drv_sw(padapter);
378 handle_dualmac:
379 rtw_handle_dualmac(padapter, 0);
380 if (pnetdev)
381 rtw_free_netdev(pnetdev);
382 else
383 vfree(padapter);
384
385 return ret;
386 }
387
rtw_usb_if1_deinit(struct adapter * if1)388 static void rtw_usb_if1_deinit(struct adapter *if1)
389 {
390 struct net_device *pnetdev = if1->pnetdev;
391 struct mlme_priv *pmlmepriv = &if1->mlmepriv;
392
393 if (check_fwstate(pmlmepriv, _FW_LINKED))
394 rtw_disassoc_cmd(if1, 0, false);
395
396 free_mlme_ap_info(if1);
397
398 if (pnetdev) {
399 /* will call netdev_close() */
400 unregister_netdev(pnetdev);
401 }
402 rtw_cancel_all_timer(if1);
403
404 rtw_dev_unload(if1);
405 rtw_handle_dualmac(if1, 0);
406 rtw_free_drv_sw(if1);
407 if (pnetdev)
408 rtw_free_netdev(pnetdev);
409 }
410
rtw_drv_init(struct usb_interface * pusb_intf,const struct usb_device_id * pdid)411 static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
412 {
413 struct dvobj_priv *dvobj;
414 int ret;
415
416 /* Initialize dvobj_priv */
417 dvobj = usb_dvobj_init(pusb_intf);
418 if (!dvobj)
419 return -ENODEV;
420
421 ret = rtw_usb_if1_init(dvobj, pusb_intf);
422 if (ret) {
423 usb_dvobj_deinit(pusb_intf);
424 return ret;
425 }
426
427 if (ui_pid[1] != 0)
428 rtw_signal_process(ui_pid[1], SIGUSR2);
429
430 return 0;
431 }
432
433 /*
434 * dev_remove() - our device is being removed
435 */
436 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both */
rtw_dev_remove(struct usb_interface * pusb_intf)437 static void rtw_dev_remove(struct usb_interface *pusb_intf)
438 {
439 struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
440 struct adapter *padapter = dvobj->if1;
441
442 if (usb_drv->drv_registered)
443 padapter->bSurpriseRemoved = true;
444
445 rtw_pm_set_ips(padapter, IPS_NONE);
446 rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
447
448 LeaveAllPowerSaveMode(padapter);
449
450 rtw_usb_if1_deinit(padapter);
451
452 usb_dvobj_deinit(pusb_intf);
453 }
454
rtw_drv_entry(void)455 static int __init rtw_drv_entry(void)
456 {
457 mutex_init(&usb_drv->hw_init_mutex);
458
459 usb_drv->drv_registered = true;
460 return usb_register(&usb_drv->usbdrv);
461 }
462
rtw_drv_halt(void)463 static void __exit rtw_drv_halt(void)
464 {
465 usb_drv->drv_registered = false;
466 usb_deregister(&usb_drv->usbdrv);
467
468 mutex_destroy(&usb_drv->hw_init_mutex);
469 }
470
471 module_init(rtw_drv_entry);
472 module_exit(rtw_drv_halt);
473