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