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