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