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