support different wifi bt chip auto compatible
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723bu / os_dep / linux / usb_intf.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _HCI_INTF_C_
21
22 #include <drv_types.h>
23 #include <platform_ops.h>
24
25 #ifndef CONFIG_USB_HCI
26 #error "CONFIG_USB_HCI shall be on!\n"
27 #endif
28
29 #if defined (PLATFORM_LINUX) && defined (PLATFORM_WINDOWS)
30 #error "Shall be Linux or Windows, but not both!\n"
31 #endif
32
33 #ifdef CONFIG_80211N_HT
34 extern int rtw_ht_enable;
35 extern int rtw_bw_mode;
36 extern int rtw_ampdu_enable;//for enable tx_ampdu
37 #endif
38
39 #ifdef CONFIG_GLOBAL_UI_PID
40 int ui_pid[3] = {0, 0, 0};
41 #endif
42
43
44 extern int pm_netdev_open(struct net_device *pnetdev,u8 bnormal);
45 static int rtw_suspend(struct usb_interface *intf, pm_message_t message);
46 static int rtw_resume(struct usb_interface *intf);
47
48
49 static int rtw_drv_init(struct usb_interface *pusb_intf,const struct usb_device_id *pdid);
50 static void rtw_dev_remove(struct usb_interface *pusb_intf);
51
52 static void rtw_dev_shutdown(struct device *dev)
53 {
54         struct usb_interface *usb_intf = container_of(dev, struct usb_interface, dev);
55         struct dvobj_priv *dvobj = NULL;
56         _adapter *adapter = NULL;
57         int i;
58
59         DBG_871X("%s\n", __func__);
60
61         if(usb_intf)
62         {
63                 dvobj = usb_get_intfdata(usb_intf);
64                 if (dvobj)
65                 {
66                         for (i = 0; i<dvobj->iface_nums; i++)
67                         {
68                                 adapter = dvobj->padapters[i];
69                                 if (adapter)
70                                 {
71                                         adapter->bSurpriseRemoved = _TRUE;
72                                 }
73                         }
74
75                         ATOMIC_SET(&dvobj->continual_io_error, MAX_CONTINUAL_IO_ERR+1);
76                 }
77         }
78 }
79
80 #if (LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,23))
81 /* Some useful macros to use to create struct usb_device_id */
82  #define USB_DEVICE_ID_MATCH_VENDOR                      0x0001
83  #define USB_DEVICE_ID_MATCH_PRODUCT                     0x0002
84  #define USB_DEVICE_ID_MATCH_DEV_LO                      0x0004
85  #define USB_DEVICE_ID_MATCH_DEV_HI                      0x0008
86  #define USB_DEVICE_ID_MATCH_DEV_CLASS                   0x0010
87  #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS                0x0020
88  #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL                0x0040
89  #define USB_DEVICE_ID_MATCH_INT_CLASS                   0x0080
90  #define USB_DEVICE_ID_MATCH_INT_SUBCLASS                0x0100
91  #define USB_DEVICE_ID_MATCH_INT_PROTOCOL                0x0200
92  #define USB_DEVICE_ID_MATCH_INT_NUMBER                  0x0400
93
94
95 #define USB_DEVICE_ID_MATCH_INT_INFO \
96                                  (USB_DEVICE_ID_MATCH_INT_CLASS | \
97                                  USB_DEVICE_ID_MATCH_INT_SUBCLASS | \
98                                  USB_DEVICE_ID_MATCH_INT_PROTOCOL)
99
100
101 #define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, cl, sc, pr) \
102                  .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \
103                                  | USB_DEVICE_ID_MATCH_DEVICE, \
104                  .idVendor = (vend), \
105                  .idProduct = (prod), \
106                  .bInterfaceClass = (cl), \
107                  .bInterfaceSubClass = (sc), \
108                  .bInterfaceProtocol = (pr)
109
110  /**
111   * USB_VENDOR_AND_INTERFACE_INFO - describe a specific usb vendor with a class of usb interfaces
112   * @vend: the 16 bit USB Vendor ID
113   * @cl: bInterfaceClass value
114   * @sc: bInterfaceSubClass value
115   * @pr: bInterfaceProtocol value
116   *
117   * This macro is used to create a struct usb_device_id that matches a
118   * specific vendor with a specific class of interfaces.
119   *
120   * This is especially useful when explicitly matching devices that have
121   * vendor specific bDeviceClass values, but standards-compliant interfaces.
122   */
123 #define USB_VENDOR_AND_INTERFACE_INFO(vend, cl, sc, pr) \
124                  .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \
125                                  | USB_DEVICE_ID_MATCH_VENDOR, \
126                  .idVendor = (vend), \
127                  .bInterfaceClass = (cl), \
128                  .bInterfaceSubClass = (sc), \
129                  .bInterfaceProtocol = (pr)
130
131 /* ----------------------------------------------------------------------- */
132 #endif
133
134
135 #define USB_VENDER_ID_REALTEK           0x0BDA
136
137
138 /* DID_USB_v916_20130116 */
139 static struct usb_device_id rtw_usb_id_tbl[] ={
140
141 #ifdef CONFIG_RTL8192C
142         /*=== Realtek demoboard ===*/
143         /****** 8188CUS ********/
144         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8176),.driver_info = RTL8188C_8192C},/* 8188cu 1*1 dongole */
145         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8170),.driver_info = RTL8188C_8192C},/* 8188CE-VAU USB minCard */
146         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817E),.driver_info = RTL8188C_8192C},/* 8188CE-VAU USB minCard */
147         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817A),.driver_info = RTL8188C_8192C},/* 8188cu Slim Solo */
148         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817B),.driver_info = RTL8188C_8192C},/* 8188cu Slim Combo */
149         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817D),.driver_info = RTL8188C_8192C},/* 8188RU High-power USB Dongle */
150         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8754),.driver_info = RTL8188C_8192C},/* 8188 Combo for BC4 */
151         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817F),.driver_info = RTL8188C_8192C},/* 8188RU */
152         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x818A),.driver_info = RTL8188C_8192C},/* RTL8188CUS-VL */
153         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x018A),.driver_info = RTL8188C_8192C},/* RTL8188CTV */
154         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x17C0),.driver_info = RTL8188C_8192C},/* RTK demoboard - USB-N10E */
155         /****** 8192CUS ********/
156         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8177),.driver_info = RTL8188C_8192C},/* 8191cu 1*2 */
157         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8178),.driver_info = RTL8188C_8192C},/* 8192cu 2*2 */
158         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817C),.driver_info = RTL8188C_8192C},/* 8192CE-VAU USB minCard */
159         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8191),.driver_info = RTL8188C_8192C},/* 8192CU 2*2 */
160         {USB_DEVICE(0x1058, 0x0631),.driver_info = RTL8188C_8192C},/* Alpha, 8192CU */
161         /*=== Customer ID ===*/
162         /****** 8188CUS Dongle ********/
163         {USB_DEVICE(0x2019, 0xED17),.driver_info = RTL8188C_8192C},/* PCI - Edimax */
164         {USB_DEVICE(0x0DF6, 0x0052),.driver_info = RTL8188C_8192C},/* Sitecom - Edimax */
165         {USB_DEVICE(0x7392, 0x7811),.driver_info = RTL8188C_8192C},/* Edimax - Edimax */
166         {USB_DEVICE(0x07B8, 0x8189),.driver_info = RTL8188C_8192C},/* Abocom - Abocom */
167         {USB_DEVICE(0x0EB0, 0x9071),.driver_info = RTL8188C_8192C},/* NO Brand - Etop */
168         {USB_DEVICE(0x06F8, 0xE033),.driver_info = RTL8188C_8192C},/* Hercules - Edimax */
169         {USB_DEVICE(0x103C, 0x1629),.driver_info = RTL8188C_8192C},/* HP - Lite-On ,8188CUS Slim Combo */
170         {USB_DEVICE(0x2001, 0x3308),.driver_info = RTL8188C_8192C},/* D-Link - Alpha */
171         {USB_DEVICE(0x050D, 0x1102),.driver_info = RTL8188C_8192C},/* Belkin - Edimax */
172         {USB_DEVICE(0x2019, 0xAB2A),.driver_info = RTL8188C_8192C},/* Planex - Abocom */
173         {USB_DEVICE(0x20F4, 0x648B),.driver_info = RTL8188C_8192C},/* TRENDnet - Cameo */
174         {USB_DEVICE(0x4855, 0x0090),.driver_info = RTL8188C_8192C},/*  - Feixun */
175         {USB_DEVICE(0x13D3, 0x3357),.driver_info = RTL8188C_8192C},/*  - AzureWave */
176         {USB_DEVICE(0x0DF6, 0x005C),.driver_info = RTL8188C_8192C},/* Sitecom - Edimax */
177         {USB_DEVICE(0x0BDA, 0x5088),.driver_info = RTL8188C_8192C},/* Thinkware - CC&C */
178         {USB_DEVICE(0x4856, 0x0091),.driver_info = RTL8188C_8192C},/* NetweeN - Feixun */
179         {USB_DEVICE(0x0846, 0x9041),.driver_info = RTL8188C_8192C}, /* Netgear - Cameo */
180         {USB_DEVICE(0x2019, 0x4902),.driver_info = RTL8188C_8192C},/* Planex - Etop */
181         {USB_DEVICE(0x2019, 0xAB2E),.driver_info = RTL8188C_8192C},/* SW-WF02-AD15 -Abocom */
182         {USB_DEVICE(0x2001, 0x330B),.driver_info = RTL8188C_8192C}, /* D-LINK - T&W */
183         {USB_DEVICE(0xCDAB, 0x8010),.driver_info = RTL8188C_8192C}, /* - - compare */
184         {USB_DEVICE(0x0B05, 0x17BA),.driver_info = RTL8188C_8192C}, /* ASUS - Edimax */
185         {USB_DEVICE(0x0BDA, 0x1E1E),.driver_info = RTL8188C_8192C}, /* Intel - - */
186         {USB_DEVICE(0x04BB, 0x094c),.driver_info = RTL8188C_8192C}, /* I-O DATA - Edimax */
187         /****** 8188CTV ********/
188         {USB_DEVICE(0xCDAB, 0x8011),.driver_info = RTL8188C_8192C}, /* - - compare */
189         {USB_DEVICE(0x0BDA, 0x0A8A),.driver_info = RTL8188C_8192C}, /* Sony - Foxconn */
190         /****** 8188 RU ********/
191         {USB_DEVICE(0x0BDA, 0x317F),.driver_info = RTL8188C_8192C},/* Netcore,Netcore */
192         /****** 8188CE-VAU ********/
193         {USB_DEVICE(0x13D3, 0x3359),.driver_info = RTL8188C_8192C},/*  - Azwave */
194         {USB_DEVICE(0x13D3, 0x3358),.driver_info = RTL8188C_8192C},/*  - Azwave */
195         /****** 8188CUS Slim Solo********/
196         {USB_DEVICE(0x04F2, 0xAFF7),.driver_info = RTL8188C_8192C},/* XAVI - XAVI */
197         {USB_DEVICE(0x04F2, 0xAFF9),.driver_info = RTL8188C_8192C},/* XAVI - XAVI */
198         {USB_DEVICE(0x04F2, 0xAFFA),.driver_info = RTL8188C_8192C},/* XAVI - XAVI */
199         /****** 8188CUS Slim Combo ********/
200         {USB_DEVICE(0x04F2, 0xAFF8),.driver_info = RTL8188C_8192C},/* XAVI - XAVI */
201         {USB_DEVICE(0x04F2, 0xAFFB),.driver_info = RTL8188C_8192C},/* XAVI - XAVI */
202         {USB_DEVICE(0x04F2, 0xAFFC),.driver_info = RTL8188C_8192C},/* XAVI - XAVI */
203         {USB_DEVICE(0x2019, 0x1201),.driver_info = RTL8188C_8192C},/* Planex - Vencer */
204         /****** 8192CUS Dongle ********/
205         {USB_DEVICE(0x2001, 0x3307),.driver_info = RTL8188C_8192C},/* D-Link - Cameo */
206         {USB_DEVICE(0x2001, 0x330A),.driver_info = RTL8188C_8192C},/* D-Link - Alpha */
207         {USB_DEVICE(0x2001, 0x3309),.driver_info = RTL8188C_8192C},/* D-Link - Alpha */
208         {USB_DEVICE(0x0586, 0x341F),.driver_info = RTL8188C_8192C},/* Zyxel - Abocom */
209         {USB_DEVICE(0x7392, 0x7822),.driver_info = RTL8188C_8192C},/* Edimax - Edimax */
210         {USB_DEVICE(0x2019, 0xAB2B),.driver_info = RTL8188C_8192C},/* Planex - Abocom */
211         {USB_DEVICE(0x07B8, 0x8178),.driver_info = RTL8188C_8192C},/* Abocom - Abocom */
212         {USB_DEVICE(0x07AA, 0x0056),.driver_info = RTL8188C_8192C},/* ATKK - Gemtek */
213         {USB_DEVICE(0x4855, 0x0091),.driver_info = RTL8188C_8192C},/*  - Feixun */
214         {USB_DEVICE(0x050D, 0x2102),.driver_info = RTL8188C_8192C},/* Belkin - Sercomm */
215         {USB_DEVICE(0x050D, 0x2103),.driver_info = RTL8188C_8192C},/* Belkin - Edimax */
216         {USB_DEVICE(0x20F4, 0x624D),.driver_info = RTL8188C_8192C},/* TRENDnet */
217         {USB_DEVICE(0x0DF6, 0x0061),.driver_info = RTL8188C_8192C},/* Sitecom - Edimax */
218         {USB_DEVICE(0x0B05, 0x17AB),.driver_info = RTL8188C_8192C},/* ASUS - Edimax */
219         {USB_DEVICE(0x0846, 0x9021),.driver_info = RTL8188C_8192C},/* Netgear - Sercomm */
220         {USB_DEVICE(0x0846, 0xF001),.driver_info = RTL8188C_8192C}, /* Netgear - Sercomm */
221         {USB_DEVICE(0x0E66, 0x0019),.driver_info = RTL8188C_8192C},/* Hawking,Edimax */
222         {USB_DEVICE(0x0E66, 0x0020),.driver_info = RTL8188C_8192C}, /* Hawking  - Edimax */
223         {USB_DEVICE(0x050D, 0x1004),.driver_info = RTL8188C_8192C}, /* Belkin - Edimax */
224         {USB_DEVICE(0x0BDA, 0x2E2E),.driver_info = RTL8188C_8192C}, /* Intel - - */
225         {USB_DEVICE(0x2357, 0x0100),.driver_info = RTL8188C_8192C}, /* TP-Link - TP-Link */
226         {USB_DEVICE(0x06F8, 0xE035),.driver_info = RTL8188C_8192C}, /* Hercules - Edimax */
227         {USB_DEVICE(0x04BB, 0x0950),.driver_info = RTL8188C_8192C}, /* IO-DATA - Edimax */
228         {USB_DEVICE(0x0DF6, 0x0070),.driver_info = RTL8188C_8192C}, /* Sitecom - Edimax */
229         {USB_DEVICE(0x0789, 0x016D),.driver_info = RTL8188C_8192C}, /* LOGITEC - Edimax */
230         /****** 8192CE-VAU  ********/
231         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8186),.driver_info = RTL8188C_8192C},/* Intel-Xavi( Azwave) */
232 #endif
233
234 #ifdef CONFIG_RTL8192D
235         /*=== Realtek demoboard ===*/
236         /****** 8192DU ********/
237         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8193),.driver_info = RTL8192D},/* 8192DU-VC */
238         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8194),.driver_info = RTL8192D},/* 8192DU-VS */
239         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8111),.driver_info = RTL8192D},/* Realtek 5G dongle for WiFi Display */
240         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0193),.driver_info = RTL8192D},/* 8192DE-VAU */
241         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8171),.driver_info = RTL8192D},/* 8192DU-VC */
242         /*=== Customer ID ===*/
243         /****** 8192DU-VC ********/
244         {USB_DEVICE(0x2019, 0xAB2C),.driver_info = RTL8192D},/* PCI - Abocm */
245         {USB_DEVICE(0x2019, 0x4903),.driver_info = RTL8192D},/* PCI - ETOP */
246         {USB_DEVICE(0x2019, 0x4904),.driver_info = RTL8192D},/* PCI - ETOP */
247         {USB_DEVICE(0x07B8, 0x8193),.driver_info = RTL8192D},/* Abocom - Abocom */
248         /****** 8192DU-VS ********/
249         {USB_DEVICE(0x20F4, 0x664B),.driver_info = RTL8192D},/* TRENDnet */
250         {USB_DEVICE(0x04DD, 0x954F),.driver_info = RTL8192D},  /* Sharp */
251         {USB_DEVICE(0x04DD, 0x96A6),.driver_info = RTL8192D},  /* Sharp */
252         {USB_DEVICE(0x050D, 0x110A),.driver_info = RTL8192D}, /* Belkin - Edimax */
253         {USB_DEVICE(0x050D, 0x1105),.driver_info = RTL8192D}, /* Belkin - Edimax */
254         {USB_DEVICE(0x050D, 0x120A),.driver_info = RTL8192D}, /* Belkin - Edimax */
255         {USB_DEVICE(0x1668, 0x8102),.driver_info = RTL8192D}, /*  -  */
256         {USB_DEVICE(0x0BDA, 0xE194),.driver_info = RTL8192D}, /*  - Edimax */
257         /****** 8192DU-WiFi Display Dongle ********/
258         {USB_DEVICE(0x2019, 0xAB2D),.driver_info = RTL8192D},/* Planex - Abocom ,5G dongle for WiFi Display */
259 #endif
260
261 #ifdef CONFIG_RTL8723A
262         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x8724,0xff,0xff,0xff),.driver_info = RTL8723A}, /* 8723AU 1*1 */
263         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x1724,0xff,0xff,0xff),.driver_info = RTL8723A}, /* 8723AU 1*1 */
264         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x0724,0xff,0xff,0xff),.driver_info = RTL8723A}, /* 8723AU 1*1 */
265 #endif
266
267 #ifdef CONFIG_RTL8188E
268         /*=== Realtek demoboard ===*/
269         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8179),.driver_info = RTL8188E}, /* 8188EUS */
270         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0179),.driver_info = RTL8188E}, /* 8188ETV */
271         /*=== Customer ID ===*/
272         /****** 8188EUS ********/
273         {USB_DEVICE(0x07B8, 0x8179),.driver_info = RTL8188E}, /* Abocom - Abocom */
274 #endif
275
276 #ifdef CONFIG_RTL8812A
277         /*=== Realtek demoboard ===*/
278         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8812),.driver_info = RTL8812},/* Default ID */
279         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x881A),.driver_info = RTL8812},/* Default ID */
280         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x881B),.driver_info = RTL8812},/* Default ID */
281         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x881C),.driver_info = RTL8812},/* Default ID */
282         /*=== Customer ID ===*/
283         {USB_DEVICE(0x050D, 0x1106),.driver_info = RTL8812}, /* Belkin - sercomm */
284         {USB_DEVICE(0x2001, 0x330E),.driver_info = RTL8812}, /* D-Link - ALPHA */
285         {USB_DEVICE(0x7392, 0xA822),.driver_info = RTL8812}, /* Edimax - Edimax */
286         {USB_DEVICE(0x0DF6, 0x0074),.driver_info = RTL8812}, /* Sitecom - Edimax */
287         {USB_DEVICE(0x04BB, 0x0952),.driver_info = RTL8812}, /* I-O DATA - Edimax */
288         {USB_DEVICE(0x0789, 0x016E),.driver_info = RTL8812}, /* Logitec - Edimax */
289         {USB_DEVICE(0x0409, 0x0408),.driver_info = RTL8812}, /* NEC - */
290         {USB_DEVICE(0x0B05, 0x17D2),.driver_info = RTL8812}, /* ASUS - Edimax */
291         {USB_DEVICE(0x0E66, 0x0022),.driver_info = RTL8812}, /* HAWKING - Edimax */
292         {USB_DEVICE(0x0586, 0x3426),.driver_info = RTL8812}, /* ZyXEL - */
293         {USB_DEVICE(0x2001, 0x3313),.driver_info = RTL8812}, /* D-Link - ALPHA */
294         {USB_DEVICE(0x1058, 0x0632),.driver_info = RTL8812}, /* WD - Cybertan*/
295         {USB_DEVICE(0x1740, 0x0100),.driver_info = RTL8812}, /* EnGenius - EnGenius */
296         {USB_DEVICE(0x2019, 0xAB30),.driver_info = RTL8812}, /* Planex - Abocom */
297         {USB_DEVICE(0x07B8, 0x8812),.driver_info = RTL8812}, /* Abocom - Abocom */
298         {USB_DEVICE(0x2001, 0x3315),.driver_info = RTL8812}, /* D-Link - Cameo */
299         {USB_DEVICE(0x2001, 0x3316),.driver_info = RTL8812}, /* D-Link - Cameo */
300 #endif
301
302 #ifdef CONFIG_RTL8821A
303         /*=== Realtek demoboard ===*/
304         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0811),.driver_info = RTL8821},/* Default ID */
305         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0821),.driver_info = RTL8821},/* Default ID */
306         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8822),.driver_info = RTL8821},/* Default ID */
307         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x0820,0xff,0xff,0xff),.driver_info = RTL8821}, /* 8821AU */
308         /*=== Customer ID ===*/
309         {USB_DEVICE(0x7392, 0xA811),.driver_info = RTL8821}, /* Edimax - Edimax */
310         {USB_DEVICE(0x04BB, 0x0953),.driver_info = RTL8821}, /* I-O DATA - Edimax */
311         {USB_DEVICE(0x2001, 0x3314),.driver_info = RTL8821}, /* D-Link - Cameo */
312         {USB_DEVICE(0x2001, 0x3318),.driver_info = RTL8821}, /* D-Link - Cameo */
313         {USB_DEVICE(0x0E66, 0x0023),.driver_info = RTL8821}, /* HAWKING - Edimax */
314 #endif
315
316 #ifdef CONFIG_RTL8192E
317         /*=== Realtek demoboard ===*/
318         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x818B,0xff,0xff,0xff),.driver_info = RTL8192E},/* Default ID */
319         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x818C,0xff,0xff,0xff),.driver_info = RTL8192E},/* Default ID */
320 #endif
321
322 #ifdef CONFIG_RTL8723B
323         //*=== Realtek demoboard ===*/
324         {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB720,0xff,0xff,0xff),.driver_info = RTL8723B}, /* 8723BU 1*1 */
325         //{USB_DEVICE(USB_VENDER_ID_REALTEK, 0xB720),.driver_info = RTL8723B}, /* 8723BU */
326 #endif
327         {}      /* Terminating entry */
328 };
329
330 MODULE_DEVICE_TABLE(usb, rtw_usb_id_tbl);
331
332 int const rtw_usb_id_len = sizeof(rtw_usb_id_tbl) / sizeof(struct usb_device_id);
333
334 static struct specific_device_id specific_device_id_tbl[] = {
335         {.idVendor=USB_VENDER_ID_REALTEK, .idProduct=0x8177, .flags=SPEC_DEV_ID_DISABLE_HT},//8188cu 1*1 dongole, (b/g mode only)
336         {.idVendor=USB_VENDER_ID_REALTEK, .idProduct=0x817E, .flags=SPEC_DEV_ID_DISABLE_HT},//8188CE-VAU USB minCard (b/g mode only)
337         {.idVendor=0x0b05, .idProduct=0x1791, .flags=SPEC_DEV_ID_DISABLE_HT},
338         {.idVendor=0x13D3, .idProduct=0x3311, .flags=SPEC_DEV_ID_DISABLE_HT},
339         {.idVendor=0x13D3, .idProduct=0x3359, .flags=SPEC_DEV_ID_DISABLE_HT},//Russian customer -Azwave (8188CE-VAU  g mode)
340 #ifdef RTK_DMP_PLATFORM
341         {.idVendor=USB_VENDER_ID_REALTEK, .idProduct=0x8111, .flags=SPEC_DEV_ID_ASSIGN_IFNAME}, // Realtek 5G dongle for WiFi Display
342         {.idVendor=0x2019, .idProduct=0xAB2D, .flags=SPEC_DEV_ID_ASSIGN_IFNAME}, // PCI-Abocom 5G dongle for WiFi Display
343 #endif /* RTK_DMP_PLATFORM */
344         {}
345 };
346
347 struct rtw_usb_drv {
348         struct usb_driver usbdrv;
349         int drv_registered;
350         u8 hw_type;
351 };
352
353 struct rtw_usb_drv usb_drv = {
354         .usbdrv.name =(char*)DRV_NAME,
355         .usbdrv.probe = rtw_drv_init,
356         .usbdrv.disconnect = rtw_dev_remove,
357         .usbdrv.id_table = rtw_usb_id_tbl,
358         .usbdrv.suspend =  rtw_suspend,
359         .usbdrv.resume = rtw_resume,
360         #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
361         .usbdrv.reset_resume   = rtw_resume,
362         #endif
363         #ifdef CONFIG_AUTOSUSPEND
364         .usbdrv.supports_autosuspend = 1,
365         #endif
366
367         #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19))
368         .usbdrv.drvwrap.driver.shutdown = rtw_dev_shutdown,
369         #else
370         .usbdrv.driver.shutdown = rtw_dev_shutdown,
371         #endif
372 };
373
374 static inline int RT_usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
375 {
376         return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
377 }
378
379 static inline int RT_usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd)
380 {
381         return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
382 }
383
384 static inline int RT_usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd)
385 {
386         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT);
387 }
388
389 static inline int RT_usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd)
390 {
391         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK);
392 }
393
394 static inline int RT_usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd)
395 {
396         return (RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_in(epd));
397 }
398
399 static inline int RT_usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd)
400 {
401         return (RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_out(epd));
402 }
403
404 static inline int RT_usb_endpoint_is_int_in(const struct usb_endpoint_descriptor *epd)
405 {
406         return (RT_usb_endpoint_xfer_int(epd) && RT_usb_endpoint_dir_in(epd));
407 }
408
409 static inline int RT_usb_endpoint_num(const struct usb_endpoint_descriptor *epd)
410 {
411         return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
412 }
413
414 static u8 rtw_init_intf_priv(struct dvobj_priv *dvobj)
415 {
416         u8 rst = _SUCCESS;
417
418         #ifdef CONFIG_USB_VENDOR_REQ_MUTEX
419         _rtw_mutex_init(&dvobj->usb_vendor_req_mutex);
420         #endif
421
422
423         #ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
424         dvobj->usb_alloc_vendor_req_buf = rtw_zmalloc(MAX_USB_IO_CTL_SIZE);
425         if (dvobj->usb_alloc_vendor_req_buf == NULL) {
426                 DBG_871X("alloc usb_vendor_req_buf failed... /n");
427                 rst = _FAIL;
428                 goto exit;
429         }
430         dvobj->usb_vendor_req_buf  =
431                 (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(dvobj->usb_alloc_vendor_req_buf ), ALIGNMENT_UNIT);
432 exit:
433         #endif
434
435         return rst;
436
437 }
438
439 static u8 rtw_deinit_intf_priv(struct dvobj_priv *dvobj)
440 {
441         u8 rst = _SUCCESS;
442
443         #ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
444         if(dvobj->usb_vendor_req_buf)
445                 rtw_mfree(dvobj->usb_alloc_vendor_req_buf, MAX_USB_IO_CTL_SIZE);
446         #endif
447
448         #ifdef CONFIG_USB_VENDOR_REQ_MUTEX
449         _rtw_mutex_free(&dvobj->usb_vendor_req_mutex);
450         #endif
451
452         return rst;
453 }
454
455 static struct dvobj_priv *usb_dvobj_init(struct usb_interface *usb_intf)
456 {
457         int     i;
458         u8      val8;
459         int     status = _FAIL;
460         struct dvobj_priv *pdvobjpriv;
461         struct usb_device_descriptor    *pdev_desc;
462         struct usb_host_config                  *phost_conf;
463         struct usb_config_descriptor            *pconf_desc;
464         struct usb_host_interface               *phost_iface;
465         struct usb_interface_descriptor *piface_desc;
466         struct usb_host_endpoint                *phost_endp;
467         struct usb_endpoint_descriptor  *pendp_desc;
468         struct usb_device                               *pusbd;
469
470 _func_enter_;
471
472
473         if((pdvobjpriv = devobj_init()) == NULL) {
474                 goto exit;
475         }
476
477
478         pdvobjpriv->pusbintf = usb_intf ;
479         pusbd = pdvobjpriv->pusbdev = interface_to_usbdev(usb_intf);
480         usb_set_intfdata(usb_intf, pdvobjpriv);
481
482         pdvobjpriv->RtNumInPipes = 0;
483         pdvobjpriv->RtNumOutPipes = 0;
484
485         //padapter->EepromAddressSize = 6;
486         //pdvobjpriv->nr_endpoint = 6;
487
488         pdev_desc = &pusbd->descriptor;
489
490 #if 0
491         DBG_871X("\n8712_usb_device_descriptor:\n");
492         DBG_871X("bLength=%x\n", pdev_desc->bLength);
493         DBG_871X("bDescriptorType=%x\n", pdev_desc->bDescriptorType);
494         DBG_871X("bcdUSB=%x\n", pdev_desc->bcdUSB);
495         DBG_871X("bDeviceClass=%x\n", pdev_desc->bDeviceClass);
496         DBG_871X("bDeviceSubClass=%x\n", pdev_desc->bDeviceSubClass);
497         DBG_871X("bDeviceProtocol=%x\n", pdev_desc->bDeviceProtocol);
498         DBG_871X("bMaxPacketSize0=%x\n", pdev_desc->bMaxPacketSize0);
499         DBG_871X("idVendor=%x\n", pdev_desc->idVendor);
500         DBG_871X("idProduct=%x\n", pdev_desc->idProduct);
501         DBG_871X("bcdDevice=%x\n", pdev_desc->bcdDevice);
502         DBG_871X("iManufacturer=%x\n", pdev_desc->iManufacturer);
503         DBG_871X("iProduct=%x\n", pdev_desc->iProduct);
504         DBG_871X("iSerialNumber=%x\n", pdev_desc->iSerialNumber);
505         DBG_871X("bNumConfigurations=%x\n", pdev_desc->bNumConfigurations);
506 #endif
507
508         phost_conf = pusbd->actconfig;
509         pconf_desc = &phost_conf->desc;
510
511 #if 0
512         DBG_871X("\n8712_usb_configuration_descriptor:\n");
513         DBG_871X("bLength=%x\n", pconf_desc->bLength);
514         DBG_871X("bDescriptorType=%x\n", pconf_desc->bDescriptorType);
515         DBG_871X("wTotalLength=%x\n", pconf_desc->wTotalLength);
516         DBG_871X("bNumInterfaces=%x\n", pconf_desc->bNumInterfaces);
517         DBG_871X("bConfigurationValue=%x\n", pconf_desc->bConfigurationValue);
518         DBG_871X("iConfiguration=%x\n", pconf_desc->iConfiguration);
519         DBG_871X("bmAttributes=%x\n", pconf_desc->bmAttributes);
520         DBG_871X("bMaxPower=%x\n", pconf_desc->bMaxPower);
521 #endif
522
523         //DBG_871X("\n/****** num of altsetting = (%d) ******/\n", pusb_interface->num_altsetting);
524
525         phost_iface = &usb_intf->altsetting[0];
526         piface_desc = &phost_iface->desc;
527
528 #if 0
529         DBG_871X("\n8712_usb_interface_descriptor:\n");
530         DBG_871X("bLength=%x\n", piface_desc->bLength);
531         DBG_871X("bDescriptorType=%x\n", piface_desc->bDescriptorType);
532         DBG_871X("bInterfaceNumber=%x\n", piface_desc->bInterfaceNumber);
533         DBG_871X("bAlternateSetting=%x\n", piface_desc->bAlternateSetting);
534         DBG_871X("bNumEndpoints=%x\n", piface_desc->bNumEndpoints);
535         DBG_871X("bInterfaceClass=%x\n", piface_desc->bInterfaceClass);
536         DBG_871X("bInterfaceSubClass=%x\n", piface_desc->bInterfaceSubClass);
537         DBG_871X("bInterfaceProtocol=%x\n", piface_desc->bInterfaceProtocol);
538         DBG_871X("iInterface=%x\n", piface_desc->iInterface);
539 #endif
540
541         pdvobjpriv->NumInterfaces = pconf_desc->bNumInterfaces;
542         pdvobjpriv->InterfaceNumber = piface_desc->bInterfaceNumber;
543         pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
544
545         //DBG_871X("\ndump usb_endpoint_descriptor:\n");
546
547         for (i = 0; i < pdvobjpriv->nr_endpoint; i++)
548         {
549                 phost_endp = phost_iface->endpoint + i;
550                 if (phost_endp)
551                 {
552                         pendp_desc = &phost_endp->desc;
553
554                         DBG_871X("\nusb_endpoint_descriptor(%d):\n", i);
555                         DBG_871X("bLength=%x\n",pendp_desc->bLength);
556                         DBG_871X("bDescriptorType=%x\n",pendp_desc->bDescriptorType);
557                         DBG_871X("bEndpointAddress=%x\n",pendp_desc->bEndpointAddress);
558                         //DBG_871X("bmAttributes=%x\n",pendp_desc->bmAttributes);
559                         DBG_871X("wMaxPacketSize=%d\n",le16_to_cpu(pendp_desc->wMaxPacketSize));
560                         DBG_871X("bInterval=%x\n",pendp_desc->bInterval);
561                         //DBG_871X("bRefresh=%x\n",pendp_desc->bRefresh);
562                         //DBG_871X("bSynchAddress=%x\n",pendp_desc->bSynchAddress);
563
564                         if (RT_usb_endpoint_is_bulk_in(pendp_desc))
565                         {
566                                 DBG_871X("RT_usb_endpoint_is_bulk_in = %x\n", RT_usb_endpoint_num(pendp_desc));
567                                 pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = RT_usb_endpoint_num(pendp_desc);
568                                 pdvobjpriv->RtNumInPipes++;
569                         }
570                         else if (RT_usb_endpoint_is_int_in(pendp_desc))
571                         {
572                                 DBG_871X("RT_usb_endpoint_is_int_in = %x, Interval = %x\n", RT_usb_endpoint_num(pendp_desc),pendp_desc->bInterval);
573                                 pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = RT_usb_endpoint_num(pendp_desc);
574                                 pdvobjpriv->RtNumInPipes++;
575                         }
576                         else if (RT_usb_endpoint_is_bulk_out(pendp_desc))
577                         {
578                                 DBG_871X("RT_usb_endpoint_is_bulk_out = %x\n", RT_usb_endpoint_num(pendp_desc));
579                                 pdvobjpriv->RtOutPipe[pdvobjpriv->RtNumOutPipes] = RT_usb_endpoint_num(pendp_desc);
580                                 pdvobjpriv->RtNumOutPipes++;
581                         }
582                         pdvobjpriv->ep_num[i] = RT_usb_endpoint_num(pendp_desc);
583                 }
584         }
585
586         DBG_871X("nr_endpoint=%d, in_num=%d, out_num=%d\n\n", pdvobjpriv->nr_endpoint, pdvobjpriv->RtNumInPipes, pdvobjpriv->RtNumOutPipes);
587
588         switch(pusbd->speed) {
589                 case USB_SPEED_LOW:
590                         DBG_871X("USB_SPEED_LOW\n");
591                         pdvobjpriv->usb_speed = RTW_USB_SPEED_1_1;
592                         break;
593                 case USB_SPEED_FULL:
594                         DBG_871X("USB_SPEED_FULL\n");
595                         pdvobjpriv->usb_speed = RTW_USB_SPEED_1_1;
596                         break;
597                 case USB_SPEED_HIGH:
598                         DBG_871X("USB_SPEED_HIGH\n");
599                         pdvobjpriv->usb_speed = RTW_USB_SPEED_2;
600                         break;
601 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31))
602                 case USB_SPEED_SUPER:
603                         DBG_871X("USB_SPEED_SUPER\n");
604                         pdvobjpriv->usb_speed = RTW_USB_SPEED_3;
605                         break;
606 #endif
607                 default:
608                         DBG_871X("USB_SPEED_UNKNOWN(%x)\n",pusbd->speed);
609                         pdvobjpriv->usb_speed = RTW_USB_SPEED_UNKNOWN;
610                         break;
611         }
612
613         if (pdvobjpriv->usb_speed == RTW_USB_SPEED_UNKNOWN) {
614                 DBG_871X("UNKNOWN USB SPEED MODE, ERROR !!!\n");
615                 goto free_dvobj;
616         }
617
618         if (rtw_init_intf_priv(pdvobjpriv) == _FAIL) {
619                 RT_TRACE(_module_os_intfs_c_,_drv_err_,("\n Can't INIT rtw_init_intf_priv\n"));
620                 goto free_dvobj;
621         }
622
623         //.3 misc
624         _rtw_init_sema(&(pdvobjpriv->usb_suspend_sema), 0);
625         rtw_reset_continual_io_error(pdvobjpriv);
626
627         usb_get_dev(pusbd);
628
629         status = _SUCCESS;
630
631 free_dvobj:
632         if (status != _SUCCESS && pdvobjpriv) {
633                 usb_set_intfdata(usb_intf, NULL);
634                 
635                 devobj_deinit(pdvobjpriv);
636                 
637                 pdvobjpriv = NULL;
638         }
639 exit:
640 _func_exit_;
641         return pdvobjpriv;
642 }
643
644 static void usb_dvobj_deinit(struct usb_interface *usb_intf)
645 {
646         struct dvobj_priv *dvobj = usb_get_intfdata(usb_intf);
647
648 _func_enter_;
649
650         usb_set_intfdata(usb_intf, NULL);
651         if (dvobj) {
652                 //Modify condition for 92DU DMDP 2010.11.18, by Thomas
653                 if ((dvobj->NumInterfaces != 2 && dvobj->NumInterfaces != 3)
654                         || (dvobj->InterfaceNumber == 1)) {
655                         if (interface_to_usbdev(usb_intf)->state != USB_STATE_NOTATTACHED) {
656                                 //If we didn't unplug usb dongle and remove/insert modlue, driver fails on sitesurvey for the first time when device is up .
657                                 //Reset usb port for sitesurvey fail issue. 2009.8.13, by Thomas
658                                 DBG_871X("usb attached..., try to reset usb device\n");
659                                 usb_reset_device(interface_to_usbdev(usb_intf));
660                         }
661                 }
662
663                 rtw_deinit_intf_priv(dvobj);
664         
665                 devobj_deinit(dvobj);           
666         }
667
668         //DBG_871X("%s %d\n", __func__, ATOMIC_READ(&usb_intf->dev.kobj.kref.refcount));
669         usb_put_dev(interface_to_usbdev(usb_intf));
670
671 _func_exit_;
672 }
673
674 static void rtw_decide_chip_type_by_usb_info(_adapter *padapter, const struct usb_device_id *pdid)
675 {
676         padapter->chip_type = pdid->driver_info;
677
678         #ifdef CONFIG_RTL8192C
679         if(padapter->chip_type == RTL8188C_8192C)
680                 rtl8192cu_set_hw_type(padapter);
681         #endif
682
683         #ifdef CONFIG_RTL8192D
684         if(padapter->chip_type == RTL8192D)
685                 rtl8192du_set_hw_type(padapter);
686         #endif
687
688         #ifdef CONFIG_RTL8723A
689         if(padapter->chip_type == RTL8723A)
690                 rtl8723au_set_hw_type(padapter);
691         #endif
692
693         #ifdef CONFIG_RTL8188E
694         if(padapter->chip_type == RTL8188E)
695                 rtl8188eu_set_hw_type(padapter);
696         #endif
697
698         #if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)
699         if(padapter->chip_type == RTL8812 || padapter->chip_type == RTL8821)
700                 rtl8812au_set_hw_type(padapter);
701         #endif
702
703         #ifdef CONFIG_RTL8192E
704         if(padapter->chip_type == RTL8192E)
705                 rtl8192eu_set_hw_type(padapter);
706         #endif
707
708         #ifdef CONFIG_RTL8723B
709         if(padapter->chip_type == RTL8723B)
710                 rtl8723bu_set_hw_type(padapter);
711         #endif
712
713 }
714 void rtw_set_hal_ops(_adapter *padapter)
715 {
716         //alloc memory for HAL DATA
717         rtw_hal_data_init(padapter);
718         
719         #ifdef CONFIG_RTL8192C
720         if(padapter->chip_type == RTL8188C_8192C)
721                 rtl8192cu_set_hal_ops(padapter);
722         #endif
723
724         #ifdef CONFIG_RTL8192D
725         if(padapter->chip_type == RTL8192D)
726                 rtl8192du_set_hal_ops(padapter);
727         #endif
728
729         #ifdef CONFIG_RTL8723A
730         if(padapter->chip_type == RTL8723A)
731                 rtl8723au_set_hal_ops(padapter);
732         #endif
733
734         #ifdef CONFIG_RTL8188E
735         if(padapter->chip_type == RTL8188E)
736                 rtl8188eu_set_hal_ops(padapter);
737         #endif
738
739         #if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)
740         if(padapter->chip_type == RTL8812 || padapter->chip_type == RTL8821)
741                 rtl8812au_set_hal_ops(padapter);
742         #endif
743
744         #ifdef CONFIG_RTL8192E
745         if(padapter->chip_type == RTL8192E)
746                 rtl8192eu_set_hal_ops(padapter);
747         #endif
748         #ifdef CONFIG_RTL8723B
749         if(padapter->chip_type == RTL8723B)
750                 rtl8723bu_set_hal_ops(padapter);
751         #endif
752 }
753
754 void usb_set_intf_ops(_adapter *padapter,struct _io_ops *pops)
755 {
756         #ifdef CONFIG_RTL8192C
757         if(padapter->chip_type == RTL8188C_8192C)
758                 rtl8192cu_set_intf_ops(pops);
759         #endif
760
761         #ifdef CONFIG_RTL8192D
762         if(padapter->chip_type == RTL8192D)
763                 rtl8192du_set_intf_ops(pops);
764         #endif
765
766         #ifdef CONFIG_RTL8723A
767         if(padapter->chip_type == RTL8723A)
768                 rtl8723au_set_intf_ops(pops);
769         #endif
770
771         #ifdef CONFIG_RTL8188E
772         if(padapter->chip_type == RTL8188E)
773                 rtl8188eu_set_intf_ops(pops);
774         #endif
775
776         #if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)
777         if(padapter->chip_type == RTL8812 || padapter->chip_type == RTL8821)
778                 rtl8812au_set_intf_ops(pops);
779         #endif
780
781         #ifdef CONFIG_RTL8192E
782         if(padapter->chip_type == RTL8192E)
783                 rtl8192eu_set_intf_ops(pops);
784         #endif
785
786         #ifdef CONFIG_RTL8723B
787         if(padapter->chip_type == RTL8723B)
788                 rtl8723bu_set_intf_ops(pops);
789         #endif
790 }
791
792
793 static void usb_intf_start(_adapter *padapter)
794 {
795
796         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+usb_intf_start\n"));
797
798         rtw_hal_inirp_init(padapter);
799
800         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-usb_intf_start\n"));
801
802 }
803
804 static void usb_intf_stop(_adapter *padapter)
805 {
806
807         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+usb_intf_stop\n"));
808
809         //disabel_hw_interrupt
810         if(padapter->bSurpriseRemoved == _FALSE)
811         {
812                 //device still exists, so driver can do i/o operation
813                 //TODO:
814                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("SurpriseRemoved==_FALSE\n"));
815         }
816
817         //cancel in irp
818         rtw_hal_inirp_deinit(padapter);
819
820         //cancel out irp
821         rtw_write_port_cancel(padapter);
822
823         //todo:cancel other irps
824
825         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-usb_intf_stop\n"));
826
827 }
828
829 static void process_spec_devid(const struct usb_device_id *pdid)
830 {
831         u16 vid, pid;
832         u32 flags;
833         int i;
834         int num = sizeof(specific_device_id_tbl)/sizeof(struct specific_device_id);
835
836         for(i=0; i<num; i++)
837         {
838                 vid = specific_device_id_tbl[i].idVendor;
839                 pid = specific_device_id_tbl[i].idProduct;
840                 flags = specific_device_id_tbl[i].flags;
841
842 #ifdef CONFIG_80211N_HT
843                 if((pdid->idVendor==vid) && (pdid->idProduct==pid) && (flags&SPEC_DEV_ID_DISABLE_HT))
844                 {
845                          rtw_ht_enable = 0;
846                          rtw_bw_mode = 0;
847                          rtw_ampdu_enable = 0;
848                 }
849 #endif
850
851 #ifdef RTK_DMP_PLATFORM
852                 // Change the ifname to wlan10 when PC side WFD dongle plugin on DMP platform.
853                 // It is used to distinguish between normal and PC-side wifi dongle/module.
854                 if((pdid->idVendor==vid) && (pdid->idProduct==pid) && (flags&SPEC_DEV_ID_ASSIGN_IFNAME))
855                 {
856                         extern char* ifname;
857                         strncpy(ifname, "wlan10", 6);
858                         //DBG_871X("%s()-%d: ifname=%s, vid=%04X, pid=%04X\n", __FUNCTION__, __LINE__, ifname, vid, pid);
859                 }
860 #endif /* RTK_DMP_PLATFORM */
861
862         }
863 }
864
865 #ifdef SUPPORT_HW_RFOFF_DETECTED
866 int rtw_hw_suspend(_adapter *padapter )
867 {
868         struct pwrctrl_priv *pwrpriv;
869         struct usb_interface *pusb_intf;
870         struct net_device *pnetdev;
871
872         _func_enter_;
873         if(NULL==padapter)
874                 goto error_exit;
875
876         if((_FALSE==padapter->bup) || (_TRUE == padapter->bDriverStopped)||(_TRUE==padapter->bSurpriseRemoved))
877         {
878                 DBG_871X("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
879                         padapter->bup, padapter->bDriverStopped,padapter->bSurpriseRemoved);
880                 goto error_exit;
881         }
882         
883         pwrpriv = adapter_to_pwrctl(padapter);
884         pusb_intf = adapter_to_dvobj(padapter)->pusbintf;
885         pnetdev = padapter->pnetdev;
886         
887         LeaveAllPowerSaveMode(padapter);
888
889         DBG_871X("==> rtw_hw_suspend\n");
890         _enter_pwrlock(&pwrpriv->lock);
891         pwrpriv->bips_processing = _TRUE;
892         //padapter->net_closed = _TRUE;
893         //s1.
894         if(pnetdev)
895         {
896                 netif_carrier_off(pnetdev);
897                 rtw_netif_stop_queue(pnetdev);
898         }
899
900         //s2.
901         rtw_disassoc_cmd(padapter, 500, _FALSE);
902
903         //s2-2.  indicate disconnect to os
904         //rtw_indicate_disconnect(padapter);
905         {
906                 struct  mlme_priv *pmlmepriv = &padapter->mlmepriv;
907                 if(check_fwstate(pmlmepriv, _FW_LINKED))
908                 {
909                         _clr_fwstate_(pmlmepriv, _FW_LINKED);
910                         rtw_led_control(padapter, LED_CTL_NO_LINK);
911
912                         rtw_os_indicate_disconnect(padapter);
913
914                         #ifdef CONFIG_LPS
915                         //donnot enqueue cmd
916                         rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 0);
917                         #endif
918                 }
919         }
920         //s2-3.
921         rtw_free_assoc_resources(padapter, 1);
922
923         //s2-4.
924         rtw_free_network_queue(padapter,_TRUE);
925         #ifdef CONFIG_IPS
926         rtw_ips_dev_unload(padapter);
927         #endif
928         pwrpriv->rf_pwrstate = rf_off;
929         pwrpriv->bips_processing = _FALSE;
930         _exit_pwrlock(&pwrpriv->lock);
931         
932         _func_exit_;
933         return 0;
934
935 error_exit:
936         DBG_871X("%s, failed \n",__FUNCTION__);
937         return (-1);
938
939 }
940
941 int rtw_hw_resume(_adapter *padapter)
942 {
943         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
944         struct usb_interface *pusb_intf = adapter_to_dvobj(padapter)->pusbintf;
945         struct net_device *pnetdev = padapter->pnetdev;
946
947         _func_enter_;   
948         DBG_871X("==> rtw_hw_resume\n");
949         _enter_pwrlock(&pwrpriv->lock);
950         pwrpriv->bips_processing = _TRUE;
951         rtw_reset_drv_sw(padapter);
952
953         if(pm_netdev_open(pnetdev,_FALSE) != 0)
954         {
955                 _exit_pwrlock(&pwrpriv->lock);
956                 goto error_exit;
957         }
958
959         netif_device_attach(pnetdev);
960         netif_carrier_on(pnetdev);
961
962         if(!rtw_netif_queue_stopped(pnetdev))
963                 rtw_netif_start_queue(pnetdev);
964         else
965                 rtw_netif_wake_queue(pnetdev);
966
967         pwrpriv->bkeepfwalive = _FALSE;
968         pwrpriv->brfoffbyhw = _FALSE;
969
970         pwrpriv->rf_pwrstate = rf_on;
971         pwrpriv->bips_processing = _FALSE;
972         _exit_pwrlock(&pwrpriv->lock);
973
974         _func_exit_;
975
976         return 0;
977 error_exit:
978         DBG_871X("%s, Open net dev failed \n",__FUNCTION__);
979         return (-1);
980 }
981 #endif
982
983 static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message)
984 {
985         struct dvobj_priv *dvobj;
986         struct pwrctrl_priv *pwrpriv;
987         struct debug_priv *pdbgpriv;
988         PADAPTER padapter;
989         int ret = 0;
990
991
992         dvobj = usb_get_intfdata(pusb_intf);
993         pwrpriv = dvobj_to_pwrctl(dvobj);
994         pdbgpriv = &dvobj->drv_dbg;
995         padapter = dvobj->if1;
996
997         if (pwrpriv->bInSuspend == _TRUE) {
998                 DBG_871X("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend);
999                 pdbgpriv->dbg_suspend_error_cnt++;
1000                 goto exit;
1001         }
1002
1003         if((padapter->bup) || (padapter->bDriverStopped == _FALSE)||(padapter->bSurpriseRemoved == _FALSE))
1004         {
1005 #ifdef CONFIG_AUTOSUSPEND
1006                 if(pwrpriv->bInternalAutoSuspend ){
1007
1008                         #ifdef SUPPORT_HW_RFOFF_DETECTED
1009                         // The FW command register update must after MAC and FW init ready.
1010                         if((padapter->bFWReady) && (pwrpriv->bHWPwrPindetect ) && (padapter->registrypriv.usbss_enable ))
1011                         {
1012                                 u8 bOpen = _TRUE;
1013                                 rtw_interface_ps_func(padapter,HAL_USB_SELECT_SUSPEND,&bOpen);
1014                                 //rtl8192c_set_FwSelectSuspend_cmd(padapter,_TRUE ,500);//note fw to support hw power down ping detect
1015                         }
1016                         #endif//SUPPORT_HW_RFOFF_DETECTED
1017                 }
1018 #endif//CONFIG_AUTOSUSPEND
1019         }
1020
1021         ret =  rtw_suspend_common(padapter);
1022
1023 exit:
1024         return ret;
1025 }
1026
1027 int rtw_resume_process(_adapter *padapter)
1028 {
1029         int ret,pm_cnt = 0;
1030         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
1031         struct dvobj_priv *pdvobj = padapter->dvobj;
1032         struct debug_priv *pdbgpriv = &pdvobj->drv_dbg;
1033
1034
1035         if (pwrpriv->bInSuspend == _FALSE)
1036         {
1037                 pdbgpriv->dbg_resume_error_cnt++;
1038                 DBG_871X("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend);
1039                 return -1;
1040         }
1041
1042 #if defined(CONFIG_BT_COEXIST) && defined(CONFIG_AUTOSUSPEND) //add by amy for 8723as-vau
1043 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1044         DBG_871X("%s...pm_usage_cnt(%d)  pwrpriv->bAutoResume=%x.  ....\n",__func__,atomic_read(&(adapter_to_dvobj(padapter)->pusbintf->pm_usage_cnt)),pwrpriv->bAutoResume);
1045         pm_cnt=atomic_read(&(adapter_to_dvobj(padapter)->pusbintf->pm_usage_cnt));
1046 #else // kernel < 2.6.32
1047         DBG_871X("...pm_usage_cnt(%d).....\n", adapter_to_dvobj(padapter)->pusbintf->pm_usage_cnt);
1048         pm_cnt = adapter_to_dvobj(padapter)->pusbintf->pm_usage_cnt;
1049 #endif // kernel < 2.6.32
1050
1051         DBG_871X("pwrpriv->bAutoResume (%x)\n",pwrpriv->bAutoResume );
1052         if( _TRUE == pwrpriv->bAutoResume ){
1053                 pwrpriv->bInternalAutoSuspend = _FALSE;
1054                 pwrpriv->bAutoResume=_FALSE;
1055                 DBG_871X("pwrpriv->bAutoResume (%x)  pwrpriv->bInternalAutoSuspend(%x)\n",pwrpriv->bAutoResume,pwrpriv->bInternalAutoSuspend );
1056
1057         }
1058 #endif //#ifdef CONFIG_BT_COEXIST &CONFIG_AUTOSUSPEND&
1059
1060 #if defined (CONFIG_WOWLAN) || defined (CONFIG_AP_WOWLAN)
1061         /*
1062          * Due to usb wow suspend flow will cancel read/write port via intf_stop and
1063          * bReadPortCancel and bWritePortCancel are set _TRUE in intf_stop.
1064          * But they will not be clear in intf_start during wow resume flow. 
1065          * It should move to os_intf in the feature.
1066          */
1067         RTW_ENABLE_FUNC(padapter, DF_RX_BIT);
1068         RTW_ENABLE_FUNC(padapter, DF_TX_BIT);
1069 #endif
1070
1071         ret =  rtw_resume_common(padapter);
1072
1073         #ifdef CONFIG_AUTOSUSPEND
1074         if(pwrpriv->bInternalAutoSuspend )
1075         {
1076                 #ifdef SUPPORT_HW_RFOFF_DETECTED
1077                         // The FW command register update must after MAC and FW init ready.
1078                 if((padapter->bFWReady) && (pwrpriv->bHWPwrPindetect) && (padapter->registrypriv.usbss_enable ))
1079                 {
1080                         //rtl8192c_set_FwSelectSuspend_cmd(padapter,_FALSE ,500);//note fw to support hw power down ping detect
1081                         u8 bOpen = _FALSE;
1082                         rtw_interface_ps_func(padapter,HAL_USB_SELECT_SUSPEND,&bOpen);
1083                 }       
1084                 #endif
1085                 #ifdef CONFIG_BT_COEXIST // for 8723as-vau
1086                 DBG_871X("pwrpriv->bAutoResume (%x)\n",pwrpriv->bAutoResume );
1087                 if( _TRUE == pwrpriv->bAutoResume ){
1088                 pwrpriv->bInternalAutoSuspend = _FALSE;
1089                         pwrpriv->bAutoResume=_FALSE;
1090                         DBG_871X("pwrpriv->bAutoResume (%x)  pwrpriv->bInternalAutoSuspend(%x)\n",pwrpriv->bAutoResume,pwrpriv->bInternalAutoSuspend );
1091                 }
1092
1093                 #else   //#ifdef CONFIG_BT_COEXIST
1094                 pwrpriv->bInternalAutoSuspend = _FALSE;
1095                 #endif  //#ifdef CONFIG_BT_COEXIST
1096                 pwrpriv->brfoffbyhw = _FALSE;
1097         }
1098         #endif//CONFIG_AUTOSUSPEND
1099
1100
1101         return ret;
1102 }
1103
1104 static int rtw_resume(struct usb_interface *pusb_intf)
1105 {
1106         struct dvobj_priv *dvobj;
1107         struct pwrctrl_priv *pwrpriv;
1108         struct debug_priv *pdbgpriv;
1109         PADAPTER padapter;
1110         struct mlme_ext_priv *pmlmeext;
1111         int ret = 0;
1112
1113
1114         dvobj = usb_get_intfdata(pusb_intf);
1115         pwrpriv = dvobj_to_pwrctl(dvobj);
1116         pdbgpriv = &dvobj->drv_dbg;
1117         padapter = dvobj->if1;
1118         pmlmeext = &padapter->mlmeextpriv;
1119
1120         DBG_871X("==> %s (%s:%d)\n", __FUNCTION__, current->comm, current->pid);
1121         pdbgpriv->dbg_resume_cnt++;
1122
1123         if(pwrpriv->bInternalAutoSuspend)
1124         {
1125                 ret = rtw_resume_process(padapter);
1126         }
1127         else
1128         {
1129                 if(pwrpriv->wowlan_mode || pwrpriv->wowlan_ap_mode)
1130                 {
1131                         rtw_resume_lock_suspend();                      
1132                         ret = rtw_resume_process(padapter);
1133                         rtw_resume_unlock_suspend();
1134                 }
1135                 else
1136                 {
1137 #ifdef CONFIG_RESUME_IN_WORKQUEUE
1138                         rtw_resume_in_workqueue(pwrpriv);
1139 #else                   
1140                         if (rtw_is_earlysuspend_registered(pwrpriv))
1141                         {
1142                                 /* jeff: bypass resume here, do in late_resume */
1143                                 rtw_set_do_late_resume(pwrpriv, _TRUE);
1144                         }       
1145                         else
1146                         {
1147                                 rtw_resume_lock_suspend();                      
1148                                 ret = rtw_resume_process(padapter);
1149                                 rtw_resume_unlock_suspend();
1150                         }
1151 #endif
1152                 }
1153         }
1154
1155         pmlmeext->last_scan_time = rtw_get_current_time();
1156         DBG_871X("<========  %s return %d\n", __FUNCTION__, ret);
1157
1158         return ret;
1159 }
1160
1161
1162
1163 #ifdef CONFIG_AUTOSUSPEND
1164 void autosuspend_enter(_adapter* padapter)
1165 {
1166         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1167         struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1168
1169         DBG_871X("==>autosuspend_enter...........\n");
1170
1171         pwrpriv->bInternalAutoSuspend = _TRUE;
1172         pwrpriv->bips_processing = _TRUE;
1173
1174         if(rf_off == pwrpriv->change_rfpwrstate )
1175         {
1176 #ifndef CONFIG_BT_COEXIST
1177                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1178                 usb_enable_autosuspend(dvobj->pusbdev);
1179                 #else
1180                 dvobj->pusbdev->autosuspend_disabled = 0;//autosuspend disabled by the user
1181                 #endif
1182
1183                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1184                         usb_autopm_put_interface(dvobj->pusbintf);
1185                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
1186                         usb_autopm_enable(dvobj->pusbintf);
1187                 #else
1188                         usb_autosuspend_device(dvobj->pusbdev, 1);
1189                 #endif
1190 #else   //#ifndef       CONFIG_BT_COEXIST
1191                 if(1==pwrpriv->autopm_cnt){
1192                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1193                 usb_enable_autosuspend(dvobj->pusbdev);
1194                 #else
1195                 dvobj->pusbdev->autosuspend_disabled = 0;//autosuspend disabled by the user
1196                 #endif
1197
1198                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1199                         usb_autopm_put_interface(dvobj->pusbintf);
1200                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
1201                         usb_autopm_enable(dvobj->pusbintf);
1202                 #else
1203                         usb_autosuspend_device(dvobj->pusbdev, 1);
1204                 #endif
1205                         pwrpriv->autopm_cnt --;
1206                 }
1207                 else
1208                 DBG_871X("0!=pwrpriv->autopm_cnt[%d]   didn't usb_autopm_put_interface\n", pwrpriv->autopm_cnt);
1209
1210 #endif  //#ifndef       CONFIG_BT_COEXIST
1211         }
1212         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1213         DBG_871X("...pm_usage_cnt(%d).....\n", atomic_read(&(dvobj->pusbintf->pm_usage_cnt)));
1214         #else
1215         DBG_871X("...pm_usage_cnt(%d).....\n", dvobj->pusbintf->pm_usage_cnt);
1216         #endif
1217
1218 }
1219
1220 int autoresume_enter(_adapter* padapter)
1221 {
1222         int result = _SUCCESS;
1223         struct security_priv* psecuritypriv=&(padapter->securitypriv);
1224         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1225         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1226         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1227         struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1228
1229         DBG_871X("====> autoresume_enter \n");
1230
1231         if(rf_off == pwrpriv->rf_pwrstate )
1232         {
1233                 pwrpriv->ps_flag = _FALSE;
1234 #ifndef CONFIG_BT_COEXIST
1235                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1236                         if (usb_autopm_get_interface(dvobj->pusbintf) < 0)
1237                         {
1238                                 DBG_871X( "can't get autopm: %d\n", result);
1239                                 result = _FAIL;
1240                                 goto error_exit;
1241                         }
1242                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
1243                         usb_autopm_disable(dvobj->pusbintf);
1244                 #else
1245                         usb_autoresume_device(dvobj->pusbdev, 1);
1246                 #endif
1247
1248                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1249                 DBG_871X("...pm_usage_cnt(%d).....\n", atomic_read(&(dvobj->pusbintf->pm_usage_cnt)));
1250                 #else
1251                 DBG_871X("...pm_usage_cnt(%d).....\n", dvobj->pusbintf->pm_usage_cnt);
1252                 #endif
1253 #else   //#ifndef       CONFIG_BT_COEXIST
1254                 pwrpriv->bAutoResume=_TRUE;
1255                 if(0==pwrpriv->autopm_cnt){
1256                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1257                         if (usb_autopm_get_interface(dvobj->pusbintf) < 0)
1258                         {
1259                                 DBG_871X( "can't get autopm: %d\n", result);
1260                                 result = _FAIL;
1261                                 goto error_exit;
1262                         }
1263                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
1264                         usb_autopm_disable(dvobj->pusbintf);
1265                 #else
1266                         usb_autoresume_device(dvobj->pusbdev, 1);
1267                 #endif
1268                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1269                         DBG_871X("...pm_usage_cnt(%d).....\n", atomic_read(&(dvobj->pusbintf->pm_usage_cnt)));
1270                 #else
1271                         DBG_871X("...pm_usage_cnt(%d).....\n", dvobj->pusbintf->pm_usage_cnt);
1272                 #endif
1273                         pwrpriv->autopm_cnt++;
1274                 }
1275                 else
1276                         DBG_871X("0!=pwrpriv->autopm_cnt[%d]   didn't usb_autopm_get_interface\n",pwrpriv->autopm_cnt);
1277 #endif //#ifndef        CONFIG_BT_COEXIST
1278         }
1279         DBG_871X("<==== autoresume_enter \n");
1280 error_exit:
1281
1282         return result;
1283 }
1284 #endif
1285
1286 #ifdef CONFIG_PLATFORM_RTD2880B
1287 extern void rtd2885_wlan_netlink_sendMsg(char *action_string, char *name);
1288 #endif
1289
1290 /*
1291  * drv_init() - a device potentially for us
1292  *
1293  * notes: drv_init() is called when the bus driver has located a card for us to support.
1294  *        We accept the new device by returning 0.
1295 */
1296
1297 _adapter  *rtw_sw_export = NULL;
1298
1299 _adapter *rtw_usb_if1_init(struct dvobj_priv *dvobj,
1300         struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
1301 {
1302         _adapter *padapter = NULL;
1303         struct net_device *pnetdev = NULL;
1304         int status = _FAIL;
1305
1306         if ((padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter))) == NULL) {
1307                 goto exit;
1308         }
1309         padapter->dvobj = dvobj;
1310         dvobj->if1 = padapter;
1311
1312         padapter->bDriverStopped=_TRUE;
1313
1314         dvobj->padapters[dvobj->iface_nums++] = padapter;
1315         padapter->iface_id = IFACE_ID0;
1316
1317 #if defined(CONFIG_CONCURRENT_MODE) || defined(CONFIG_DUALMAC_CONCURRENT)
1318         //set adapter_type/iface type for primary padapter
1319         padapter->isprimary = _TRUE;
1320         padapter->adapter_type = PRIMARY_ADAPTER;
1321         #ifndef CONFIG_HWPORT_SWAP
1322         padapter->iface_type = IFACE_PORT0;
1323         #else
1324         padapter->iface_type = IFACE_PORT1;
1325         #endif  
1326 #endif
1327
1328         //step 1-1., decide the chip_type via driver_info
1329         padapter->interface_type = RTW_USB;
1330         rtw_decide_chip_type_by_usb_info(padapter, pdid);
1331
1332         if (rtw_handle_dualmac(padapter, 1) != _SUCCESS)
1333                 goto free_adapter;
1334
1335         if((pnetdev = rtw_init_netdev(padapter)) == NULL) {
1336                 goto handle_dualmac;
1337         }
1338         SET_NETDEV_DEV(pnetdev, dvobj_to_dev(dvobj));
1339         padapter = rtw_netdev_priv(pnetdev);
1340
1341 #ifdef CONFIG_IOCTL_CFG80211
1342         if(rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj)) != 0) {
1343                 goto handle_dualmac;
1344         }
1345 #endif
1346
1347         //step 2. hook HalFunc, allocate HalData
1348         //hal_set_hal_ops(padapter);
1349         rtw_set_hal_ops(padapter);
1350
1351         padapter->intf_start=&usb_intf_start;
1352         padapter->intf_stop=&usb_intf_stop;
1353
1354         //step init_io_priv
1355         rtw_init_io_priv(padapter,usb_set_intf_ops);
1356
1357         //step read_chip_version
1358         rtw_hal_read_chip_version(padapter);
1359
1360         //step usb endpoint mapping
1361         rtw_hal_chip_configure(padapter);
1362
1363 #ifdef CONFIG_BT_COEXIST
1364         rtw_btcoex_Initialize(padapter);
1365 #endif // CONFIG_BT_COEXIST
1366
1367         //step read efuse/eeprom data and get mac_addr
1368         rtw_hal_read_chip_info(padapter);
1369
1370         //step 5.
1371         if(rtw_init_drv_sw(padapter) ==_FAIL) {
1372                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("Initialize driver software resource Failed!\n"));
1373                 goto free_hal_data;
1374         }
1375
1376 #ifdef CONFIG_PM
1377 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1378         if(dvobj_to_pwrctl(dvobj)->bSupportRemoteWakeup)
1379         {
1380                 dvobj->pusbdev->do_remote_wakeup=1;
1381                 pusb_intf->needs_remote_wakeup = 1;
1382                 device_init_wakeup(&pusb_intf->dev, 1);
1383                 DBG_871X("pwrctrlpriv.bSupportRemoteWakeup~~~~~~\n");
1384                 DBG_871X("pwrctrlpriv.bSupportRemoteWakeup~~~[%d]~~~\n",device_may_wakeup(&pusb_intf->dev));
1385         }
1386 #endif
1387 #endif
1388
1389 #ifdef CONFIG_AUTOSUSPEND
1390         if( padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE )
1391         {
1392                 if(padapter->registrypriv.usbss_enable ){       /* autosuspend (2s delay) */
1393                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38))
1394                         dvobj->pusbdev->dev.power.autosuspend_delay = 0 * HZ;//15 * HZ; idle-delay time
1395                         #else
1396                         dvobj->pusbdev->autosuspend_delay = 0 * HZ;//15 * HZ; idle-delay time
1397                         #endif
1398
1399                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1400                         usb_enable_autosuspend(dvobj->pusbdev);
1401                         #elif  (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
1402                         padapter->bDisableAutosuspend = dvobj->pusbdev->autosuspend_disabled ;
1403                         dvobj->pusbdev->autosuspend_disabled = 0;//autosuspend disabled by the user
1404                         #endif
1405
1406                         //usb_autopm_get_interface(adapter_to_dvobj(padapter)->pusbintf );//init pm_usage_cnt ,let it start from 1
1407
1408                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1409                         DBG_871X("%s...pm_usage_cnt(%d).....\n",__FUNCTION__,atomic_read(&(dvobj->pusbintf ->pm_usage_cnt)));
1410                         #else
1411                         DBG_871X("%s...pm_usage_cnt(%d).....\n",__FUNCTION__,dvobj->pusbintf ->pm_usage_cnt);
1412                         #endif
1413                 }
1414         }
1415 #endif
1416         //2012-07-11 Move here to prevent the 8723AS-VAU BT auto suspend influence
1417         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1418                         if (usb_autopm_get_interface(pusb_intf) < 0)
1419                                 {
1420                                         DBG_871X( "can't get autopm: \n");
1421                                 }
1422         #endif
1423 #ifdef  CONFIG_BT_COEXIST
1424         dvobj_to_pwrctl(dvobj)->autopm_cnt=1;
1425 #endif
1426
1427         // set mac addr
1428         rtw_macaddr_cfg(padapter->eeprompriv.mac_addr);
1429 #ifdef CONFIG_P2P       
1430         rtw_init_wifidirect_addrs(padapter, padapter->eeprompriv.mac_addr, padapter->eeprompriv.mac_addr);
1431 #endif // CONFIG_P2P
1432         DBG_871X("bDriverStopped:%d, bSurpriseRemoved:%d, bup:%d, hw_init_completed:%d\n"
1433                 , padapter->bDriverStopped
1434                 , padapter->bSurpriseRemoved
1435                 , padapter->bup
1436                 , padapter->hw_init_completed
1437         );
1438
1439         status = _SUCCESS;
1440
1441 free_hal_data:
1442         if(status != _SUCCESS && padapter->HalData)
1443                 rtw_mfree(padapter->HalData, sizeof(*(padapter->HalData)));
1444 free_wdev:
1445         if(status != _SUCCESS) {
1446                 #ifdef CONFIG_IOCTL_CFG80211
1447                 rtw_wdev_unregister(padapter->rtw_wdev);
1448                 rtw_wdev_free(padapter->rtw_wdev);
1449                 #endif
1450         }
1451 handle_dualmac:
1452         if (status != _SUCCESS)
1453                 rtw_handle_dualmac(padapter, 0);
1454 free_adapter:
1455         if (status != _SUCCESS) {
1456                 if (pnetdev)
1457                         rtw_free_netdev(pnetdev);
1458                 else
1459                         rtw_vmfree((u8*)padapter, sizeof(*padapter));
1460                 padapter = NULL;
1461         }
1462 exit:
1463         return padapter;
1464 }
1465
1466 static void rtw_usb_if1_deinit(_adapter *if1)
1467 {
1468         struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(if1);
1469         struct net_device *pnetdev = if1->pnetdev;
1470         struct mlme_priv *pmlmepriv= &if1->mlmepriv;
1471
1472         if(check_fwstate(pmlmepriv, _FW_LINKED))
1473                 rtw_disassoc_cmd(if1, 0, _FALSE);
1474
1475
1476 #ifdef CONFIG_AP_MODE
1477         free_mlme_ap_info(if1);
1478         #ifdef CONFIG_HOSTAPD_MLME
1479         hostapd_mode_unload(if1);
1480         #endif
1481 #endif
1482
1483         rtw_cancel_all_timer(if1);
1484
1485 #ifdef CONFIG_WOWLAN
1486         pwrctl->wowlan_mode=_FALSE;
1487 #endif //CONFIG_WOWLAN
1488
1489         rtw_dev_unload(if1);
1490
1491         DBG_871X("+r871xu_dev_remove, hw_init_completed=%d\n", if1->hw_init_completed);
1492
1493         rtw_handle_dualmac(if1, 0);
1494
1495 #ifdef CONFIG_IOCTL_CFG80211
1496         if(if1->rtw_wdev) {
1497                 rtw_wdev_free(if1->rtw_wdev);
1498         }
1499 #endif
1500
1501 #ifdef CONFIG_BT_COEXIST
1502         if(1 == pwrctl->autopm_cnt){
1503                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1504                         usb_autopm_put_interface(adapter_to_dvobj(if1)->pusbintf);
1505                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
1506                         usb_autopm_enable(adapter_to_dvobj(if1)->pusbintf);
1507                 #else
1508                         usb_autosuspend_device(adapter_to_dvobj(if1)->pusbdev, 1);
1509                 #endif
1510                 pwrctl->autopm_cnt --;
1511         }
1512 #endif
1513
1514         rtw_free_drv_sw(if1);
1515
1516         if(pnetdev)
1517                 rtw_free_netdev(pnetdev);
1518
1519 #ifdef CONFIG_PLATFORM_RTD2880B
1520         DBG_871X("wlan link down\n");
1521         rtd2885_wlan_netlink_sendMsg("linkdown", "8712");
1522 #endif
1523
1524 }
1525
1526 static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
1527 {
1528         _adapter *if1 = NULL, *if2 = NULL;
1529         int status = _FAIL;
1530         struct dvobj_priv *dvobj;
1531 #ifdef CONFIG_MULTI_VIR_IFACES
1532         int i;
1533 #endif //CONFIG_MULTI_VIR_IFACES
1534
1535         RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+rtw_drv_init\n"));
1536         //DBG_871X("+rtw_drv_init\n");
1537
1538         //step 0.
1539         process_spec_devid(pdid);
1540
1541         /* Initialize dvobj_priv */
1542         if ((dvobj = usb_dvobj_init(pusb_intf)) == NULL) {
1543                 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("initialize device object priv Failed!\n"));
1544                 goto exit;
1545         }
1546
1547         if ((if1 = rtw_usb_if1_init(dvobj, pusb_intf, pdid)) == NULL) {
1548                 DBG_871X("rtw_usb_if1_init Failed!\n");
1549                 goto free_dvobj;
1550         }
1551
1552 #ifdef CONFIG_CONCURRENT_MODE
1553         if((if2 = rtw_drv_if2_init(if1, usb_set_intf_ops)) == NULL) {
1554                 goto free_if1;
1555         }
1556 #ifdef CONFIG_MULTI_VIR_IFACES
1557         for(i=0; i<if1->registrypriv.ext_iface_num;i++)
1558         {
1559                 if(rtw_drv_add_vir_if(if1, usb_set_intf_ops) == NULL)
1560                 {
1561                         DBG_871X("rtw_drv_add_iface failed! (%d)\n", i);
1562                         goto free_if2;
1563                 }
1564         }
1565 #endif //CONFIG_MULTI_VIR_IFACES
1566 #endif
1567
1568 #ifdef CONFIG_INTEL_PROXIM
1569         rtw_sw_export=if1;
1570 #endif
1571
1572 #ifdef CONFIG_GLOBAL_UI_PID
1573         if(ui_pid[1]!=0) {
1574                 DBG_871X("ui_pid[1]:%d\n",ui_pid[1]);
1575                 rtw_signal_process(ui_pid[1], SIGUSR2);
1576         }
1577 #endif
1578
1579         //dev_alloc_name && register_netdev
1580         if((status = rtw_drv_register_netdev(if1)) != _SUCCESS) {
1581                 goto free_if2;
1582         }
1583
1584 #ifdef CONFIG_HOSTAPD_MLME
1585         hostapd_mode_init(if1);
1586 #endif
1587
1588 #ifdef CONFIG_PLATFORM_RTD2880B
1589         DBG_871X("wlan link up\n");
1590         rtd2885_wlan_netlink_sendMsg("linkup", "8712");
1591 #endif
1592
1593         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-871x_drv - drv_init, success!\n"));
1594
1595         status = _SUCCESS;
1596
1597 free_if2:
1598         if(status != _SUCCESS && if2) {
1599                 #ifdef CONFIG_CONCURRENT_MODE
1600                 rtw_drv_if2_stop(if2);
1601                 rtw_drv_if2_free(if2);
1602                 #endif
1603         }
1604 free_if1:
1605         if (status != _SUCCESS && if1) {
1606                 rtw_usb_if1_deinit(if1);
1607         }
1608 free_dvobj:
1609         if (status != _SUCCESS)
1610                 usb_dvobj_deinit(pusb_intf);
1611 exit:
1612         return status == _SUCCESS?0:-ENODEV;
1613 }
1614
1615 /*
1616  * dev_remove() - our device is being removed
1617 */
1618 //rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both
1619 static void rtw_dev_remove(struct usb_interface *pusb_intf)
1620 {
1621         struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf);
1622         struct pwrctrl_priv *pwrctl = dvobj_to_pwrctl(dvobj);
1623         _adapter *padapter = dvobj->if1;
1624         struct net_device *pnetdev = padapter->pnetdev;
1625         struct mlme_priv *pmlmepriv= &padapter->mlmepriv;
1626
1627 _func_enter_;
1628
1629         DBG_871X("+rtw_dev_remove\n");
1630         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+dev_remove()\n"));
1631
1632         dvobj->processing_dev_remove = _TRUE;
1633
1634         rtw_unregister_netdevs(dvobj);
1635
1636         if(usb_drv.drv_registered == _TRUE)
1637         {
1638                 //DBG_871X("r871xu_dev_remove():padapter->bSurpriseRemoved == _TRUE\n");
1639                 padapter->bSurpriseRemoved = _TRUE;
1640         }
1641         /*else
1642         {
1643                 //DBG_871X("r871xu_dev_remove():module removed\n");
1644                 padapter->hw_init_completed = _FALSE;
1645         }*/
1646
1647
1648 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
1649         rtw_unregister_early_suspend(pwrctl);
1650 #endif
1651
1652         rtw_pm_set_ips(padapter, IPS_NONE);
1653         rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
1654
1655         LeaveAllPowerSaveMode(padapter);
1656
1657 #ifdef CONFIG_CONCURRENT_MODE
1658 #ifdef CONFIG_MULTI_VIR_IFACES
1659         rtw_drv_stop_vir_ifaces(dvobj);
1660 #endif //CONFIG_MULTI_VIR_IFACES
1661         rtw_drv_if2_stop(dvobj->if2);
1662 #endif //CONFIG_CONCURRENT_MODE
1663
1664         #ifdef CONFIG_BT_COEXIST
1665         rtw_btcoex_HaltNotify(padapter);
1666         #endif
1667
1668         rtw_usb_if1_deinit(padapter);
1669
1670 #ifdef CONFIG_CONCURRENT_MODE
1671 #ifdef CONFIG_MULTI_VIR_IFACES
1672         rtw_drv_free_vir_ifaces(dvobj);
1673 #endif //CONFIG_MULTI_VIR_IFACES
1674         rtw_drv_if2_free(dvobj->if2);
1675 #endif //CONFIG_CONCURRENT_MODE
1676
1677         usb_dvobj_deinit(pusb_intf);
1678
1679         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-dev_remove()\n"));
1680         DBG_871X("-r871xu_dev_remove, done\n");
1681
1682
1683 #ifdef CONFIG_INTEL_PROXIM
1684         rtw_sw_export=NULL;
1685 #endif
1686
1687 _func_exit_;
1688
1689         return;
1690
1691 }
1692 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
1693 extern int console_suspend_enabled;
1694 #endif
1695
1696 static int rtw_drv_entry(void)
1697 {
1698         int ret = 0;
1699
1700         DBG_871X_LEVEL(_drv_always_, "module init start\n");
1701         dump_drv_version(RTW_DBGDUMP);
1702 #ifdef BTCOEXVERSION
1703         DBG_871X_LEVEL(_drv_always_, DRV_NAME" BT-Coex version = %s\n", BTCOEXVERSION);
1704 #endif // BTCOEXVERSION
1705
1706         ret = platform_wifi_power_on();
1707         if(ret != 0)
1708         {
1709                 DBG_871X("%s: power on failed!!(%d)\n", __FUNCTION__, ret);
1710                 ret = -1;
1711                 goto exit;
1712         }
1713 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
1714         //console_suspend_enabled=0;
1715 #endif
1716
1717         usb_drv.drv_registered = _TRUE;
1718         rtw_suspend_lock_init();
1719         rtw_drv_proc_init();
1720         rtw_ndev_notifier_register();
1721
1722         ret = usb_register(&usb_drv.usbdrv);
1723
1724         if (ret != 0) {
1725                 usb_drv.drv_registered = _FALSE;
1726                 rtw_suspend_lock_uninit();
1727                 rtw_drv_proc_deinit();
1728                 rtw_ndev_notifier_unregister();
1729                 goto exit;
1730         }
1731
1732 exit:
1733         DBG_871X_LEVEL(_drv_always_, "module init ret=%d\n", ret);
1734         return ret;
1735 }
1736
1737 static void rtw_drv_halt(void)
1738 {
1739         DBG_871X_LEVEL(_drv_always_, "module exit start\n");
1740
1741         usb_drv.drv_registered = _FALSE;
1742
1743         usb_deregister(&usb_drv.usbdrv);
1744
1745         platform_wifi_power_off();
1746
1747         rtw_suspend_lock_uninit();
1748         rtw_drv_proc_deinit();
1749         rtw_ndev_notifier_unregister();
1750
1751         DBG_871X_LEVEL(_drv_always_, "module exit success\n");
1752
1753         rtw_mstat_dump(RTW_DBGDUMP);
1754 }
1755
1756 #include "wifi_version.h"
1757
1758 extern int rockchip_wifi_power(int on);
1759 extern int rockchip_wifi_set_carddetect(int val);
1760
1761 int rockchip_wifi_init_module_rtkwifi(void)
1762 {
1763     printk("\n");
1764     printk("=======================================================\n");
1765     printk("==== Launching Wi-Fi driver! (Powered by Rockchip) ====\n");
1766     printk("=======================================================\n");
1767     printk("Realtek 8723BU USB WiFi driver (Powered by Rockchip,Ver %s) init.\n", RTL8723BU_DRV_VERSION);
1768
1769     rockchip_wifi_power(1);
1770     return rtw_drv_entry();
1771 }
1772
1773 void rockchip_wifi_exit_module_rtkwifi(void)
1774 {
1775     printk("\n");
1776     printk("=======================================================\n");
1777     printk("==== Dislaunching Wi-Fi driver! (Powered by Rockchip) ====\n");
1778     printk("=======================================================\n");
1779     printk("Realtek 8723BU USB WiFi driver (Powered by Rockchip,Ver %s) init.\n", RTL8723BU_DRV_VERSION);
1780
1781     rtw_drv_halt();
1782     rockchip_wifi_power(0);
1783 }
1784
1785 module_init(rockchip_wifi_init_module_rtkwifi);
1786 module_exit(rockchip_wifi_exit_module_rtkwifi);
1787
1788 //module_init(rtw_drv_entry);
1789 //module_exit(rtw_drv_halt);
1790
1791 #ifdef CONFIG_INTEL_PROXIM
1792 _adapter  *rtw_usb_get_sw_pointer(void)
1793 {
1794         return rtw_sw_export;
1795 }
1796 EXPORT_SYMBOL(rtw_usb_get_sw_pointer);
1797 #endif  //CONFIG_INTEL_PROXIM
1798