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