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