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