1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  * usb_intf.c
4  *
5  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6  * Linux device driver for RTL8192SU
7  *
8  * Modifications for inclusion into the Linux staging tree are
9  * Copyright(c) 2010 Larry Finger. All rights reserved.
10  *
11  * Contact information:
12  * WLAN FAE <wlanfae@realtek.com>
13  * Larry Finger <Larry.Finger@lwfinger.net>
14  *
15  ******************************************************************************/
16 
17 #define _HCI_INTF_C_
18 
19 #include <linux/usb.h>
20 #include <linux/module.h>
21 #include <linux/firmware.h>
22 
23 #include "osdep_service.h"
24 #include "drv_types.h"
25 #include "recv_osdep.h"
26 #include "xmit_osdep.h"
27 #include "rtl8712_efuse.h"
28 #include "usb_ops.h"
29 #include "usb_osintf.h"
30 
31 static struct usb_interface *pintf;
32 
33 static int r871xu_drv_init(struct usb_interface *pusb_intf,
34 			   const struct usb_device_id *pdid);
35 
36 static void r871xu_dev_remove(struct usb_interface *pusb_intf);
37 
38 static const struct usb_device_id rtl871x_usb_id_tbl[] = {
39 /* RTL8188SU */
40 	/* Realtek */
41 	{USB_DEVICE(0x0BDA, 0x8171)},
42 	{USB_DEVICE(0x0bda, 0x8173)},
43 	{USB_DEVICE(0x0bda, 0x8712)},
44 	{USB_DEVICE(0x0bda, 0x8713)},
45 	{USB_DEVICE(0x0bda, 0xC512)},
46 	/* Abocom */
47 	{USB_DEVICE(0x07B8, 0x8188)},
48 	/* ASUS */
49 	{USB_DEVICE(0x0B05, 0x1786)},
50 	{USB_DEVICE(0x0B05, 0x1791)}, /* 11n mode disable */
51 	/* Belkin */
52 	{USB_DEVICE(0x050D, 0x945A)},
53 	/* ISY IWL - Belkin clone */
54 	{USB_DEVICE(0x050D, 0x11F1)},
55 	/* Corega */
56 	{USB_DEVICE(0x07AA, 0x0047)},
57 	/* D-Link */
58 	{USB_DEVICE(0x2001, 0x3306)},
59 	{USB_DEVICE(0x07D1, 0x3306)}, /* 11n mode disable */
60 	/* Edimax */
61 	{USB_DEVICE(0x7392, 0x7611)},
62 	/* EnGenius */
63 	{USB_DEVICE(0x1740, 0x9603)},
64 	/* Hawking */
65 	{USB_DEVICE(0x0E66, 0x0016)},
66 	/* Hercules */
67 	{USB_DEVICE(0x06F8, 0xE034)},
68 	{USB_DEVICE(0x06F8, 0xE032)},
69 	/* Logitec */
70 	{USB_DEVICE(0x0789, 0x0167)},
71 	/* PCI */
72 	{USB_DEVICE(0x2019, 0xAB28)},
73 	{USB_DEVICE(0x2019, 0xED16)},
74 	/* Sitecom */
75 	{USB_DEVICE(0x0DF6, 0x0057)},
76 	{USB_DEVICE(0x0DF6, 0x0045)},
77 	{USB_DEVICE(0x0DF6, 0x0059)}, /* 11n mode disable */
78 	{USB_DEVICE(0x0DF6, 0x004B)},
79 	{USB_DEVICE(0x0DF6, 0x005B)},
80 	{USB_DEVICE(0x0DF6, 0x005D)},
81 	{USB_DEVICE(0x0DF6, 0x0063)},
82 	/* Sweex */
83 	{USB_DEVICE(0x177F, 0x0154)},
84 	/* Thinkware */
85 	{USB_DEVICE(0x0BDA, 0x5077)},
86 	/* Toshiba */
87 	{USB_DEVICE(0x1690, 0x0752)},
88 	/* - */
89 	{USB_DEVICE(0x20F4, 0x646B)},
90 	{USB_DEVICE(0x083A, 0xC512)},
91 	{USB_DEVICE(0x25D4, 0x4CA1)},
92 	{USB_DEVICE(0x25D4, 0x4CAB)},
93 
94 /* RTL8191SU */
95 	/* Realtek */
96 	{USB_DEVICE(0x0BDA, 0x8172)},
97 	{USB_DEVICE(0x0BDA, 0x8192)},
98 	/* Amigo */
99 	{USB_DEVICE(0x0EB0, 0x9061)},
100 	/* ASUS/EKB */
101 	{USB_DEVICE(0x13D3, 0x3323)},
102 	{USB_DEVICE(0x13D3, 0x3311)}, /* 11n mode disable */
103 	{USB_DEVICE(0x13D3, 0x3342)},
104 	/* ASUS/EKBLenovo */
105 	{USB_DEVICE(0x13D3, 0x3333)},
106 	{USB_DEVICE(0x13D3, 0x3334)},
107 	{USB_DEVICE(0x13D3, 0x3335)}, /* 11n mode disable */
108 	{USB_DEVICE(0x13D3, 0x3336)}, /* 11n mode disable */
109 	/* ASUS/Media BOX */
110 	{USB_DEVICE(0x13D3, 0x3309)},
111 	/* Belkin */
112 	{USB_DEVICE(0x050D, 0x815F)},
113 	/* D-Link */
114 	{USB_DEVICE(0x07D1, 0x3302)},
115 	{USB_DEVICE(0x07D1, 0x3300)},
116 	{USB_DEVICE(0x07D1, 0x3303)},
117 	/* Edimax */
118 	{USB_DEVICE(0x7392, 0x7612)},
119 	/* EnGenius */
120 	{USB_DEVICE(0x1740, 0x9605)},
121 	/* Guillemot */
122 	{USB_DEVICE(0x06F8, 0xE031)},
123 	/* Hawking */
124 	{USB_DEVICE(0x0E66, 0x0015)},
125 	/* Mediao */
126 	{USB_DEVICE(0x13D3, 0x3306)},
127 	/* PCI */
128 	{USB_DEVICE(0x2019, 0xED18)},
129 	{USB_DEVICE(0x2019, 0x4901)},
130 	/* Sitecom */
131 	{USB_DEVICE(0x0DF6, 0x0058)},
132 	{USB_DEVICE(0x0DF6, 0x0049)},
133 	{USB_DEVICE(0x0DF6, 0x004C)},
134 	{USB_DEVICE(0x0DF6, 0x006C)},
135 	{USB_DEVICE(0x0DF6, 0x0064)},
136 	/* Skyworth */
137 	{USB_DEVICE(0x14b2, 0x3300)},
138 	{USB_DEVICE(0x14b2, 0x3301)},
139 	{USB_DEVICE(0x14B2, 0x3302)},
140 	/* - */
141 	{USB_DEVICE(0x04F2, 0xAFF2)},
142 	{USB_DEVICE(0x04F2, 0xAFF5)},
143 	{USB_DEVICE(0x04F2, 0xAFF6)},
144 	{USB_DEVICE(0x13D3, 0x3339)},
145 	{USB_DEVICE(0x13D3, 0x3340)}, /* 11n mode disable */
146 	{USB_DEVICE(0x13D3, 0x3341)}, /* 11n mode disable */
147 	{USB_DEVICE(0x13D3, 0x3310)},
148 	{USB_DEVICE(0x13D3, 0x3325)},
149 
150 /* RTL8192SU */
151 	/* Realtek */
152 	{USB_DEVICE(0x0BDA, 0x8174)},
153 	/* Belkin */
154 	{USB_DEVICE(0x050D, 0x845A)},
155 	/* Corega */
156 	{USB_DEVICE(0x07AA, 0x0051)},
157 	/* Edimax */
158 	{USB_DEVICE(0x7392, 0x7622)},
159 	/* NEC */
160 	{USB_DEVICE(0x0409, 0x02B6)},
161 	{}
162 };
163 
164 MODULE_DEVICE_TABLE(usb, rtl871x_usb_id_tbl);
165 
166 static struct specific_device_id specific_device_id_tbl[] = {
167 	{.idVendor = 0x0b05, .idProduct = 0x1791,
168 		 .flags = SPEC_DEV_ID_DISABLE_HT},
169 	{.idVendor = 0x0df6, .idProduct = 0x0059,
170 		 .flags = SPEC_DEV_ID_DISABLE_HT},
171 	{.idVendor = 0x13d3, .idProduct = 0x3306,
172 		 .flags = SPEC_DEV_ID_DISABLE_HT},
173 	{.idVendor = 0x13D3, .idProduct = 0x3311,
174 		 .flags = SPEC_DEV_ID_DISABLE_HT},
175 	{.idVendor = 0x13d3, .idProduct = 0x3335,
176 		 .flags = SPEC_DEV_ID_DISABLE_HT},
177 	{.idVendor = 0x13d3, .idProduct = 0x3336,
178 		 .flags = SPEC_DEV_ID_DISABLE_HT},
179 	{.idVendor = 0x13d3, .idProduct = 0x3340,
180 		 .flags = SPEC_DEV_ID_DISABLE_HT},
181 	{.idVendor = 0x13d3, .idProduct = 0x3341,
182 		 .flags = SPEC_DEV_ID_DISABLE_HT},
183 	{}
184 };
185 
186 struct drv_priv {
187 	struct usb_driver r871xu_drv;
188 	int drv_registered;
189 };
190 
191 #ifdef CONFIG_PM
192 static int r871x_suspend(struct usb_interface *pusb_intf, pm_message_t state)
193 {
194 	struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
195 	struct _adapter *padapter = netdev_priv(pnetdev);
196 
197 	netdev_info(pnetdev, "Suspending...\n");
198 	padapter->suspended = true;
199 	rtl871x_intf_stop(padapter);
200 	if (pnetdev->netdev_ops->ndo_stop)
201 		pnetdev->netdev_ops->ndo_stop(pnetdev);
202 	mdelay(10);
203 	netif_device_detach(pnetdev);
204 	return 0;
205 }
206 
207 static void rtl871x_intf_resume(struct _adapter *padapter)
208 {
209 	if (padapter->dvobjpriv.inirp_init)
210 		padapter->dvobjpriv.inirp_init(padapter);
211 }
212 
213 static int r871x_resume(struct usb_interface *pusb_intf)
214 {
215 	struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
216 	struct _adapter *padapter = netdev_priv(pnetdev);
217 
218 	netdev_info(pnetdev,  "Resuming...\n");
219 	netif_device_attach(pnetdev);
220 	if (pnetdev->netdev_ops->ndo_open)
221 		pnetdev->netdev_ops->ndo_open(pnetdev);
222 	padapter->suspended = false;
223 	rtl871x_intf_resume(padapter);
224 	return 0;
225 }
226 #endif
227 
228 static struct drv_priv drvpriv = {
229 	.r871xu_drv.name = "r8712u",
230 	.r871xu_drv.id_table = rtl871x_usb_id_tbl,
231 	.r871xu_drv.probe = r871xu_drv_init,
232 	.r871xu_drv.disconnect = r871xu_dev_remove,
233 #ifdef CONFIG_PM
234 	.r871xu_drv.suspend = r871x_suspend,
235 	.r871xu_drv.resume = r871x_resume,
236 #endif
237 };
238 
239 static uint r8712_usb_dvobj_init(struct _adapter *padapter)
240 {
241 	uint	status = _SUCCESS;
242 	struct	usb_host_interface		*phost_iface;
243 	struct	usb_interface_descriptor	*piface_desc;
244 	struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
245 	struct usb_device *pusbd = pdvobjpriv->pusbdev;
246 
247 	pdvobjpriv->padapter = padapter;
248 	padapter->eeprom_address_size = 6;
249 	phost_iface = pintf->cur_altsetting;
250 	piface_desc = &phost_iface->desc;
251 	pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
252 	if (pusbd->speed == USB_SPEED_HIGH) {
253 		pdvobjpriv->ishighspeed = true;
254 		dev_info(&pusbd->dev, "r8712u: USB_SPEED_HIGH with %d endpoints\n",
255 			 pdvobjpriv->nr_endpoint);
256 	} else {
257 		pdvobjpriv->ishighspeed = false;
258 		dev_info(&pusbd->dev, "r8712u: USB_SPEED_LOW with %d endpoints\n",
259 			 pdvobjpriv->nr_endpoint);
260 	}
261 	if ((r8712_alloc_io_queue(padapter)) == _FAIL)
262 		status = _FAIL;
263 	return status;
264 }
265 
266 static void r8712_usb_dvobj_deinit(struct _adapter *padapter)
267 {
268 }
269 
270 void rtl871x_intf_stop(struct _adapter *padapter)
271 {
272 	/*disable_hw_interrupt*/
273 	if (!padapter->surprise_removed) {
274 		/*device still exists, so driver can do i/o operation
275 		 * TODO:
276 		 */
277 	}
278 
279 	/* cancel in irp */
280 	if (padapter->dvobjpriv.inirp_deinit)
281 		padapter->dvobjpriv.inirp_deinit(padapter);
282 	/* cancel out irp */
283 	r8712_usb_write_port_cancel(padapter);
284 	/* TODO:cancel other irps */
285 }
286 
287 void r871x_dev_unload(struct _adapter *padapter)
288 {
289 	if (padapter->bup) {
290 		/*s1.*/
291 		padapter->driver_stopped = true;
292 
293 		/*s3.*/
294 		rtl871x_intf_stop(padapter);
295 
296 		/*s4.*/
297 		r8712_stop_drv_threads(padapter);
298 
299 		/*s5.*/
300 		if (!padapter->surprise_removed) {
301 			padapter->hw_init_completed = false;
302 			rtl8712_hal_deinit(padapter);
303 		}
304 
305 		/*s6.*/
306 		if (padapter->dvobj_deinit)
307 			padapter->dvobj_deinit(padapter);
308 		padapter->bup = false;
309 	}
310 }
311 
312 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
313 				      struct _adapter *padapter)
314 {
315 	u16 vid, pid;
316 	u32 flags;
317 	int i;
318 	int num = ARRAY_SIZE(specific_device_id_tbl);
319 
320 	for (i = 0; i < num; i++) {
321 		vid = specific_device_id_tbl[i].idVendor;
322 		pid = specific_device_id_tbl[i].idProduct;
323 		flags = specific_device_id_tbl[i].flags;
324 
325 		if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
326 		    (flags & SPEC_DEV_ID_DISABLE_HT)) {
327 			padapter->registrypriv.ht_enable = 0;
328 			padapter->registrypriv.cbw40_enable = 0;
329 			padapter->registrypriv.ampdu_enable = 0;
330 		}
331 	}
332 }
333 
334 static const struct device_type wlan_type = {
335 	.name = "wlan",
336 };
337 
338 /*
339  * drv_init() - a device potentially for us
340  *
341  * notes: drv_init() is called when the bus driver has located a card for us
342  * to support. We accept the new device by returning 0.
343  */
344 static int r871xu_drv_init(struct usb_interface *pusb_intf,
345 			   const struct usb_device_id *pdid)
346 {
347 	uint status;
348 	struct _adapter *padapter = NULL;
349 	struct dvobj_priv *pdvobjpriv;
350 	struct net_device *pnetdev;
351 	struct usb_device *udev;
352 
353 	/* In this probe function, O.S. will provide the usb interface pointer
354 	 * to driver. We have to increase the reference count of the usb device
355 	 * structure by using the usb_get_dev function.
356 	 */
357 	udev = interface_to_usbdev(pusb_intf);
358 	usb_get_dev(udev);
359 	pintf = pusb_intf;
360 	/* step 1. */
361 	pnetdev = r8712_init_netdev();
362 	if (!pnetdev)
363 		goto put_dev;
364 	padapter = netdev_priv(pnetdev);
365 	disable_ht_for_spec_devid(pdid, padapter);
366 	pdvobjpriv = &padapter->dvobjpriv;
367 	pdvobjpriv->padapter = padapter;
368 	padapter->dvobjpriv.pusbdev = udev;
369 	padapter->pusb_intf = pusb_intf;
370 	usb_set_intfdata(pusb_intf, pnetdev);
371 	SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
372 	pnetdev->dev.type = &wlan_type;
373 	/* step 2. */
374 	padapter->dvobj_init = r8712_usb_dvobj_init;
375 	padapter->dvobj_deinit = r8712_usb_dvobj_deinit;
376 	padapter->halpriv.hal_bus_init = r8712_usb_hal_bus_init;
377 	padapter->dvobjpriv.inirp_init = r8712_usb_inirp_init;
378 	padapter->dvobjpriv.inirp_deinit = r8712_usb_inirp_deinit;
379 	/* step 3.
380 	 * initialize the dvobj_priv
381 	 */
382 
383 	status = padapter->dvobj_init(padapter);
384 	if (status != _SUCCESS)
385 		goto free_netdev;
386 
387 	/* step 4. */
388 	status = r8712_init_drv_sw(padapter);
389 	if (status)
390 		goto dvobj_deinit;
391 	/* step 5. read efuse/eeprom data and get mac_addr */
392 	{
393 		int i, offset;
394 		u8 mac[6];
395 		u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
396 		u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
397 
398 		tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
399 
400 		/* To check system boot selection.*/
401 		dev_info(&udev->dev, "r8712u: Boot from %s: Autoload %s\n",
402 			 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
403 			 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
404 
405 		/* To check autoload success or not.*/
406 		if (tmpU1b & _EEPROM_EN) {
407 			AutoloadFail = true;
408 			/* The following operations prevent Efuse leakage by
409 			 * turning on 2.5V.
410 			 */
411 			tmpU1b = r8712_read8(padapter, EFUSE_TEST + 3);
412 			r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
413 			msleep(20);
414 			r8712_write8(padapter, EFUSE_TEST + 3,
415 				     (tmpU1b & (~BIT(7))));
416 
417 			/* Retrieve Chip version.
418 			 * Recognize IC version by Reg0x4 BIT15.
419 			 */
420 			tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
421 						    0x1F);
422 			if (tmpU1b == 0x3)
423 				padapter->registrypriv.chip_version =
424 				     RTL8712_3rdCUT;
425 			else
426 				padapter->registrypriv.chip_version =
427 				     (tmpU1b >> 1) + 1;
428 			switch (padapter->registrypriv.chip_version) {
429 			case RTL8712_1stCUT:
430 			case RTL8712_2ndCUT:
431 			case RTL8712_3rdCUT:
432 				break;
433 			default:
434 				padapter->registrypriv.chip_version =
435 				     RTL8712_2ndCUT;
436 				break;
437 			}
438 
439 			for (i = 0, offset = 0; i < 128; i += 8, offset++)
440 				r8712_efuse_pg_packet_read(padapter, offset,
441 						     &pdata[i]);
442 
443 			if (!r8712_initmac || !mac_pton(r8712_initmac, mac)) {
444 				/* Use the mac address stored in the Efuse
445 				 * offset = 0x12 for usb in efuse
446 				 */
447 				ether_addr_copy(mac, &pdata[0x12]);
448 			}
449 			eeprom_CustomerID = pdata[0x52];
450 			switch (eeprom_CustomerID) {
451 			case EEPROM_CID_ALPHA:
452 				padapter->eeprompriv.CustomerID =
453 						 RT_CID_819x_ALPHA;
454 				break;
455 			case EEPROM_CID_CAMEO:
456 				padapter->eeprompriv.CustomerID =
457 						 RT_CID_819x_CAMEO;
458 				break;
459 			case EEPROM_CID_SITECOM:
460 				padapter->eeprompriv.CustomerID =
461 						 RT_CID_819x_Sitecom;
462 				break;
463 			case EEPROM_CID_COREGA:
464 				padapter->eeprompriv.CustomerID =
465 						 RT_CID_COREGA;
466 				break;
467 			case EEPROM_CID_Senao:
468 				padapter->eeprompriv.CustomerID =
469 						 RT_CID_819x_Senao;
470 				break;
471 			case EEPROM_CID_EDIMAX_BELKIN:
472 				padapter->eeprompriv.CustomerID =
473 						 RT_CID_819x_Edimax_Belkin;
474 				break;
475 			case EEPROM_CID_SERCOMM_BELKIN:
476 				padapter->eeprompriv.CustomerID =
477 						 RT_CID_819x_Sercomm_Belkin;
478 				break;
479 			case EEPROM_CID_WNC_COREGA:
480 				padapter->eeprompriv.CustomerID =
481 						 RT_CID_819x_WNC_COREGA;
482 				break;
483 			case EEPROM_CID_WHQL:
484 				break;
485 			case EEPROM_CID_NetCore:
486 				padapter->eeprompriv.CustomerID =
487 						 RT_CID_819x_Netcore;
488 				break;
489 			case EEPROM_CID_CAMEO1:
490 				padapter->eeprompriv.CustomerID =
491 						 RT_CID_819x_CAMEO1;
492 				break;
493 			case EEPROM_CID_CLEVO:
494 				padapter->eeprompriv.CustomerID =
495 						 RT_CID_819x_CLEVO;
496 				break;
497 			default:
498 				padapter->eeprompriv.CustomerID =
499 						 RT_CID_DEFAULT;
500 				break;
501 			}
502 			dev_info(&udev->dev, "r8712u: CustomerID = 0x%.4x\n",
503 				 padapter->eeprompriv.CustomerID);
504 			/* Led mode */
505 			switch (padapter->eeprompriv.CustomerID) {
506 			case RT_CID_DEFAULT:
507 			case RT_CID_819x_ALPHA:
508 			case RT_CID_819x_CAMEO:
509 				padapter->ledpriv.LedStrategy = SW_LED_MODE1;
510 				padapter->ledpriv.bRegUseLed = true;
511 				break;
512 			case RT_CID_819x_Sitecom:
513 				padapter->ledpriv.LedStrategy = SW_LED_MODE2;
514 				padapter->ledpriv.bRegUseLed = true;
515 				break;
516 			case RT_CID_COREGA:
517 			case RT_CID_819x_Senao:
518 				padapter->ledpriv.LedStrategy = SW_LED_MODE3;
519 				padapter->ledpriv.bRegUseLed = true;
520 				break;
521 			case RT_CID_819x_Edimax_Belkin:
522 				padapter->ledpriv.LedStrategy = SW_LED_MODE4;
523 				padapter->ledpriv.bRegUseLed = true;
524 				break;
525 			case RT_CID_819x_Sercomm_Belkin:
526 				padapter->ledpriv.LedStrategy = SW_LED_MODE5;
527 				padapter->ledpriv.bRegUseLed = true;
528 				break;
529 			case RT_CID_819x_WNC_COREGA:
530 				padapter->ledpriv.LedStrategy = SW_LED_MODE6;
531 				padapter->ledpriv.bRegUseLed = true;
532 				break;
533 			default:
534 				padapter->ledpriv.LedStrategy = SW_LED_MODE0;
535 				padapter->ledpriv.bRegUseLed = false;
536 				break;
537 			}
538 		} else {
539 			AutoloadFail = false;
540 		}
541 		if (((mac[0] == 0xff) && (mac[1] == 0xff) &&
542 		     (mac[2] == 0xff) && (mac[3] == 0xff) &&
543 		     (mac[4] == 0xff) && (mac[5] == 0xff)) ||
544 		    ((mac[0] == 0x00) && (mac[1] == 0x00) &&
545 		     (mac[2] == 0x00) && (mac[3] == 0x00) &&
546 		     (mac[4] == 0x00) && (mac[5] == 0x00)) ||
547 		     (!AutoloadFail)) {
548 			mac[0] = 0x00;
549 			mac[1] = 0xe0;
550 			mac[2] = 0x4c;
551 			mac[3] = 0x87;
552 			mac[4] = 0x00;
553 			mac[5] = 0x00;
554 		}
555 		if (r8712_initmac) {
556 			/* Make sure the user did not select a multicast
557 			 * address by setting bit 1 of first octet.
558 			 */
559 			mac[0] &= 0xFE;
560 			dev_info(&udev->dev,
561 				"r8712u: MAC Address from user = %pM\n", mac);
562 		} else {
563 			dev_info(&udev->dev,
564 				"r8712u: MAC Address from efuse = %pM\n", mac);
565 		}
566 		ether_addr_copy(pnetdev->dev_addr, mac);
567 	}
568 	/* step 6. Load the firmware asynchronously */
569 	if (rtl871x_load_fw(padapter))
570 		goto deinit_drv_sw;
571 	spin_lock_init(&padapter->lock_rx_ff0_filter);
572 	mutex_init(&padapter->mutex_start);
573 	return 0;
574 
575 deinit_drv_sw:
576 	r8712_free_drv_sw(padapter);
577 dvobj_deinit:
578 	padapter->dvobj_deinit(padapter);
579 free_netdev:
580 	free_netdev(pnetdev);
581 put_dev:
582 	usb_put_dev(udev);
583 	usb_set_intfdata(pusb_intf, NULL);
584 	return -ENODEV;
585 }
586 
587 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove()
588  * => how to recognize both
589  */
590 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
591 {
592 	struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
593 	struct usb_device *udev = interface_to_usbdev(pusb_intf);
594 
595 	if (pnetdev) {
596 		struct _adapter *padapter = netdev_priv(pnetdev);
597 
598 		/* never exit with a firmware callback pending */
599 		wait_for_completion(&padapter->rtl8712_fw_ready);
600 		pnetdev = usb_get_intfdata(pusb_intf);
601 		usb_set_intfdata(pusb_intf, NULL);
602 		if (!pnetdev)
603 			goto firmware_load_fail;
604 		release_firmware(padapter->fw);
605 		if (drvpriv.drv_registered)
606 			padapter->surprise_removed = true;
607 		if (pnetdev->reg_state != NETREG_UNINITIALIZED)
608 			unregister_netdev(pnetdev); /* will call netdev_close() */
609 		flush_scheduled_work();
610 		udelay(1);
611 		/* Stop driver mlme relation timer */
612 		r8712_stop_drv_timers(padapter);
613 		r871x_dev_unload(padapter);
614 		r8712_free_drv_sw(padapter);
615 		free_netdev(pnetdev);
616 
617 		/* decrease the reference count of the usb device structure
618 		 * when disconnect
619 		 */
620 		usb_put_dev(udev);
621 	}
622 firmware_load_fail:
623 	/* If we didn't unplug usb dongle and remove/insert module, driver
624 	 * fails on sitesurvey for the first time when device is up.
625 	 * Reset usb port for sitesurvey fail issue.
626 	 */
627 	if (udev->state != USB_STATE_NOTATTACHED)
628 		usb_reset_device(udev);
629 }
630 
631 static int __init r8712u_drv_entry(void)
632 {
633 	drvpriv.drv_registered = true;
634 	return usb_register(&drvpriv.r871xu_drv);
635 }
636 
637 static void __exit r8712u_drv_halt(void)
638 {
639 	drvpriv.drv_registered = false;
640 	usb_deregister(&drvpriv.r871xu_drv);
641 }
642 
643 module_init(r8712u_drv_entry);
644 module_exit(r8712u_drv_halt);
645