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